
// Make newform 540.4.a.b in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_540_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_540_4_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_540_4_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_540_a();"
function MakeCharacter_540_a()
    N := 540;
    order := 1;
    char_gens := [271, 461, 217];
    v := [1, 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_540_a_Hecke(Kf)
    return MakeCharacter_540_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 := 4;
    raw_aps := [[0], [0], [-5], [17], [30], [-61], [120], [-43], [-90], [90], [8], [317], [30], [-220], [180], [630], [840], [599], [107], [210], [-421], [353], [1350], [-1020], [-997], [960], [1181], [-330], [1454], [-1230], [1280], [2220], [1170], [-1393], [-1380], [-2659], [1850], [1121], [-300], [1620], [630], [-2299], [-900], [3461], [-4560], [-2077], [-4021], [80], [3750], [-1234], [-5880], [-5130], [-7231], [-7530], [-4560], [2100], [-3120], [3449], [3770], [6720], [-100], [-870], [3440], [5880], [1841], [-3420], [-5641], [-1057], [1290], [3467], [-7740], [-8130], [12113], [4349], [-7663], [3960], [-1320], [14390], [-8610], [7097], [8880], [-5479], [12510], [-6790], [-11176], [13860], [4740], [-1690], [-14700], [-331], [8580], [14790], [13097], [-1590], [17264], [14730], [-870], [6990], [12119], [-21511], [-10807], [20460], [4980], [-15840], [-24391], [7673], [-18930], [-14190], [12870], [19598], [-15163], [-29599], [2490], [3713], [19409], [-6480], [30260], [-21510], [540], [-29280], [-21769], [-14551], [1500], [-16530], [-25972], [10230], [-14623], [-6690], [11720], [-4750], [-30724], [-960], [22781], [32387], [-25290], [16283], [31050], [-5053], [-43680], [-1920], [37268], [-5280], [4511], [10140], [-11923], [-21300], [-40771], [-25170], [-42757], [-15300], [-23743], [27810], [-42991], [45600], [-4993], [4920], [5456], [14790], [-14023], [35250], [37920], [49080], [-33073], [6420], [-25110], [-52410], [52619], [-53890], [-3307], [-7320], [6270], [-5038], [-18000], [12491], [-64033], [30150], [34748], [44610], [-41641], [45194], [-17437], [45300], [25490], [18180], [68340], [32790], [-20413], [34169], [-34063], [-4680], [-44269], [63960], [60731], [74460], [-62400], [22230], [3719], [-13129], [-58770], [-28170], [-9900], [27101], [-69613], [-21463], [76170], [21270], [-60307], [-16290], [10950], [-87259], [46337], [52440], [-14869], [50370], [-27930], [23051], [38660], [19050], [14370], [-42030], [-39391], [-37564], [-95820], [-58480], [270], [16463], [-69540], [31590], [-11083], [20100], [-13429], [-30796], [100112], [-108030], [68789], [13140], [83693], [-73140], [-47550], [-86220], [92550], [5381], [92939], [35603], [-41340], [75450], [31763], [34680], [-25516], [94830], [-60550], [-9600], [13140], [-38287], [121350], [-96900], [63761], [-114280], [-71370], [22970], [29189], [69870], [117857], [-70291], [-46440], [-52036], [-77850], [-74310], [-340], [-23160], [3851], [31853], [-5110], [-77383], [135710], [112721], [-88950], [37826], [-9298], [98070], [-30030], [-132892], [141090], [-26569], [13553], [116160], [-137869], [47760], [20783], [58950], [138150], [142830], [-25170], [-79620], [20990], [-52680], [-53701], [-80040], [89430], [32483], [89921], [-12210], [14036], [-10080], [29639], [9230], [-166080], [87233], [49800], [-30211], [75540], [117180], [-34410], [-79639], [81390], [189626], [109380], [-42030], [-128170], [20430], [18029], [-44683], [-118050], [-48880], [-107970], [-149371], [75584], [-85660], [-103170], [-7350], [-54559], [90750], [35357], [42810], [-96748], [104370], [114287], [169830], [-20542], [71480], [-119599], [39630], [-114390], [76769], [-75480], [202260], [-2878], [-90220], [-32100], [-58890], [-48079], [158627], [158580], [-11680], [-119053], [-196890], [173190], [96270], [-26550], [-96210], [184817], [31320], [-52470], [-76380], [156557], [59489], [146280], [159041], [-217249], [38460], [-33013], [-187500], [-47370], [-49849], [99083], [-250110], [14940], [78761], [90810], [13307], [-252420], [-38790], [-43837], [-8670], [-163627], [-47340], [-59488], [57683], [170129], [-56040], [24074], [133200], [-265170], [-27913], [-165330], [115259], [-95827], [189420], [-45508], [-91830], [140186], [55650], [-4600], [131010], [-243300], [-164428], [94727], [-78510], [-144451], [136770], [138590], [-125700], [251220], [-58861], [-200557], [-89670], [-32160], [67907], [144270], [-120270], [-153990], [-110110], [-64920], [-269280], [-46870], [-97260], [-45990], [73230], [11288], [-97170]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_540_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_540_4_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_540_4_a_b(:prec:=1)
    chi := MakeCharacter_540_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    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_540_4_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_540_4_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_540_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<7,R![-17, 1]>,<11,R![-30, 1]>],Snew);
    return Vf;
end function;