
// Make newform 147.6.a.b in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_147_a();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_147_6_a_b();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose lines below.
// The precision argument determines an initial guess on how many Fourier coefficients to use.
// This guess is increased enough to uniquely determine the newform.
// To make the Hecke irreducible modular symbols subspace (type ModSym)
// containing the newform, type "MakeNewformModSym_147_6_a_b();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose line below.
// The default sign is -1.  You can change this with the optional parameter "sign".
function ConvertToHeckeField(input: pass_field := false, Kf := [])
    if not pass_field then
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_147_a();"
function MakeCharacter_147_a()
    N := 147;
    order := 1;
    char_gens := [50, 52];
    v := [1, 1];
    // chi(gens[i]) = zeta^v[i]
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    F := CyclotomicField(order);
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),[F|F.1^e:e in v]);
    return MinimalBaseRingCharacter(chi);
end function;

function MakeCharacter_147_a_Hecke(Kf)
    return MakeCharacter_147_a();
end function;


function ExtendMultiplicatively(weight, aps, character)
    prec := NextPrime(NthPrime(#aps)) - 1; // we will able to figure out a_0 ... a_prec
    primes := PrimesUpTo(prec);
    prime_powers := primes;
    assert #primes eq #aps;
    log_prec := Floor(Log(prec)/Log(2)); // prec < 2^(log_prec+1)
    F := Universe(aps);
    FXY<X, Y> := PolynomialRing(F, 2);
    // 1/(1 - a_p T + p^(weight - 1) * char(p) T^2) = 1 + a_p T + a_{p^2} T^2 + ...
    R<T> := PowerSeriesRing(FXY : Precision := log_prec + 1);
    recursion := Coefficients(1/(1 - X*T + Y*T^2));
    coeffs := [F!0: i in [1..(prec+1)]];
    coeffs[1] := 1; //a_1
    for i := 1 to #primes do
        p := primes[i];
        coeffs[p] := aps[i];
        b := p^(weight - 1) * F!character(p);
        r := 2;
        p_power := p * p;
        //deals with powers of p
        while p_power le prec do
            Append(~prime_powers, p_power);
            coeffs[p_power] := Evaluate(recursion[r + 1], [aps[i], b]);
            p_power *:= p;
            r +:= 1;
        end while;    
    end for;
    Sort(~prime_powers);
    for pp in prime_powers do
        for k := 1 to Floor(prec/pp) do
            if GCD(k, pp) eq 1 then
                coeffs[pp*k] := coeffs[pp]*coeffs[k];
            end if;
        end for;
    end for;
    return coeffs;
end function;


function qexpCoeffs()
    // To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
    weight := 6;
    raw_aps := [[-6], [9], [-78], [0], [444], [442], [126], [-2684], [4200], [-5442], [-80], [-5434], [-7962], [-11524], [13920], [-9594], [-27492], [-49478], [-59356], [32040], [61846], [-65776], [-40188], [7974], [143662], [2706], [-131768], [-128916], [-100978], [220146], [-74320], [155316], [-264246], [-224612], [-82074], [-287032], [-129878], [555284], [-43512], [18330], [-153324], [382066], [-273408], [153602], [154422], [366856], [520244], [-304736], [-288588], [-772190], [252234], [-1451136], [146398], [-607860], [-95586], [-2200344], [-1770246], [223504], [-342778], [480378], [29980], [198066], [1045636], [-1837176], [365494], [-28338], [1933916], [-1888174], [2919372], [780682], [-1334370], [1017432], [-837680], [-1519930], [2644652], [-2013360], [-726234], [-4575782], [-33870], [5861782], [-302748], [-5367082], [1177056], [3662494], [2536744], [6015036], [5659650], [-6461590], [3373530], [-4549744], [-2011356], [7604016], [673112], [-2471700], [6081524], [846216], [7667850], [9689382], [7516780], [7343246], [2182964], [12546558], [-5159724], [11745210], [-7547284], [-9284834], [-1476228], [12400686], [-3691272], [-9122234], [5679136], [-14010586], [-253686], [-4300340], [10415000], [4527138], [-14968700], [17301960], [4074702], [-3794748], [-16426142], [5506754], [-18395742], [1758348], [5363140], [-21260610], [2077286], [-4236192], [-21452360], [14889178], [6993236], [1904280], [19536128], [1251830], [-20447178], [-2210642], [-12915054], [13549924], [24595626], [15523722], [26626156], [-12389082], [-3656296], [28046268], [-21115334], [-13394712], [-30151310], [-23989386], [8875756], [-8712864], [-29578834], [-24566994], [14568212], [-16171368], [31444556], [15142656], [41487608], [17849454], [-29639882], [32228202], [48488484], [-20326374], [-3662920], [-1487412], [40793010], [9263256], [-52205056], [18660946], [-12301774], [11155314], [-1474692], [47940490], [14689320], [-45423382], [61871968], [38058438], [-25377412], [33888294], [-18619976], [-59482166], [4336592], [-53050620], [47657798], [-25882794], [-1318656], [37362354], [-40875442], [-33398924], [-29194390], [-43300368], [7389214], [31801260], [37681652], [52775754], [-70947612], [-39400458], [-34918030], [-40402210], [-51016674], [-25663464], [43794366], [23008288], [27189154], [-69957986], [74860236], [99934938], [-5869712], [-100252236], [-30368598], [-45023588], [117207218], [-112282782], [16190024], [70041372], [96710520], [82491430], [65576816], [-77011842], [-76538952], [96078750], [47335238], [-104123672], [-40743678], [-73621888], [-117763260], [-102734410], [50887014], [-47369232], [137010712], [1383708], [-21195154], [-145323884], [15372224], [-67626390], [-155842484], [75689232], [-17640098], [88613478], [44299356], [74856312], [27944580], [-134483204], [-262088], [-93584194], [-106048626], [-50948040], [-41845088], [109993620], [101828060], [36505584], [-41522194], [25757118], [-113291256], [86400022], [-59078262], [185206644], [5701430], [-18285332], [-71156142], [62226934], [-155651728], [183937908], [140249170], [1950874], [-117633138], [-158227772], [-127068066], [-209669178], [106783388], [-93159930], [35862490], [39655748], [-64578650], [104238224], [-81942578], [237915304], [51942732], [-113966770], [256074410], [-174936828], [-9685536], [-83642104], [242905560], [75392050], [55282636], [83054016], [129004562], [191802246], [-167745416], [-120146274], [-148254882], [58946148], [-24435174], [-19029300], [-295492450], [249186666], [-139003520], [160842066], [173912652], [89484772], [-60719066], [42128622], [-140423920], [-58755900], [-134283700], [92260226], [47420508], [-185605910], [110616360], [345189494], [33006768], [215974710], [-42029502], [-151889932], [-330845976], [14145334], [-114888108], [81184416], [351419134], [-211988334], [101760628], [-361813798], [-11035206], [252987104], [345357642], [-227839394], [-145290796], [-383717444], [-381015024], [-17905914], [324195710], [32305470], [-311325872], [-84823068], [-53571220], [396058140], [187100606], [414334686], [450129430], [-317922176], [-186398554], [241038138], [281948610], [-89511448], [334881246], [441981108], [116684674], [196640348], [-118154928], [-412262046], [-335411612], [-12908374], [-471255042], [303054304], [-48457018], [-204361722], [-258982800], [329961708], [-191116686], [-88370808], [-268222954], [209588982], [-10846944], [379967484], [-29513612], [-149490166], [-442520742], [506798840], [194820410], [-68633628], [446234236], [247033392], [-403542330], [299141704], [120138158], [30071748], [643397040], [-289470242], [624864510], [110531254], [-683612598], [218680746], [-522256840], [473281122], [-27549980], [-243395208], [134499632], [-446973470], [88907996], [315296688], [306538754], [-37597134], [-184351284], [-147143372], [620841000], [521226458], [597462064], [113089542], [664687052], [-70219722], [-38737286], [-513019134], [-343151920], [-792240522], [-138622590], [-309447992], [770461502], [-210071310], [727407316], [570029316], [97005550], [-308790474], [595039212], [-207675868], [656509706], [-562569606], [-168463584], [-365765432], [306764190], [337963368], [23893374], [-278743790], [-72316752], [-141399060], [425738086], [-630704598], [-515272092], [-421699014], [191887756], [490644024]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_147_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_147_6_a_b();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose lines below.
// The precision argument determines an initial guess on how many Fourier coefficients to use.
// This guess is increased enough to uniquely determine the newform.
function MakeNewformModFrm_147_6_a_b(:prec:=1)
    chi := MakeCharacter_147_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 6));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(2999) - 1;
    while true do
        trunc_vec := Vector(Kf, [0] cat [f_vec[i]: i in [1..prec]]);
        B := Basis(S, prec + 1);
        S_basismat := Matrix([AbsEltseq(g): g in B]);
        if Rank(S_basismat) eq Min(NumberOfRows(S_basismat), NumberOfColumns(S_basismat)) then
            S_basismat := ChangeRing(S_basismat,Kf);
            f_lincom := Solution(S_basismat,trunc_vec);
            f := &+[f_lincom[i]*Basis(S)[i] : i in [1..#Basis(S)]];
            return f;
        end if;
        error if prec eq maxprec, "Unable to distinguish newform within newspace";
        prec := Min(Ceiling(1.25 * prec), maxprec);
    end while;
end function;


// To make the Hecke irreducible modular symbols subspace (type ModSym)
// containing the newform, type "MakeNewformModSym_147_6_a_b();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose line below.
// The default sign is -1.  You can change this with the optional parameter "sign".
function MakeNewformModSym_147_6_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_147_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<2,R![6, 1]>,<5,R![78, 1]>],Snew);
    return Vf;
end function;