
// Make newform 600.4.a.q in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_600_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_600_4_a_q();".
// 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_600_4_a_q();".
// 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_600_a();"
function MakeCharacter_600_a()
    N := 600;
    order := 1;
    char_gens := [151, 301, 401, 577];
    v := [1, 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_600_a_Hecke(Kf)
    return MakeCharacter_600_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], [3], [0], [16], [-28], [26], [62], [-68], [208], [-58], [160], [-270], [282], [-76], [280], [210], [196], [742], [-836], [-504], [1062], [768], [1052], [-726], [1406], [990], [-736], [-1212], [-1834], [2046], [-1176], [12], [790], [-924], [3022], [1736], [1322], [908], [-1296], [-2134], [1612], [3086], [-4208], [-2818], [418], [-3352], [-4276], [-4712], [732], [-5186], [3798], [-3120], [1490], [-5292], [3918], [-6624], [-2954], [-6576], [-4478], [-6358], [-860], [5794], [6860], [-6248], [-11018], [954], [9396], [-5074], [-3916], [-1818], [7118], [5304], [-5672], [-7774], [-5516], [7128], [-10722], [12122], [10482], [3850], [-5796], [3294], [1696], [12334], [376], [-8028], [8898], [-10330], [1878], [13224], [8012], [-1792], [-8272], [516], [-14020], [-1872], [8678], [18074], [20852], [-12410], [-3620], [-11734], [1372], [18922], [14596], [2302], [-23292], [16542], [7464], [-17270], [-984], [-7278], [-18090], [24740], [19720], [-16542], [10092], [14544], [-23062], [-28020], [-6738], [14430], [17890], [-10860], [-8692], [-698], [2654], [-28240], [8320], [2154], [22380], [5760], [-6192], [13666], [-32022], [22786], [-8286], [25804], [-17670], [-7398], [-28108], [30830], [-5872], [16308], [28294], [20536], [-27710], [-12858], [-3148], [-48456], [-9478], [8178], [316], [6304], [-1596], [-25792], [-9736], [-94], [8678], [28406], [-31988], [-6714], [-15312], [-8540], [8126], [-1392], [-48832], [-46926], [23570], [-36046], [50980], [-26138], [-14360], [56038], [-35872], [44970], [-35708], [17950], [23072], [-60458], [-56], [56748], [15058], [30358], [-28792], [-46610], [48202], [-5204], [26234], [33008], [-14690], [38484], [66860], [40486], [-50564], [-38986], [68946], [38346], [-4786], [26528], [34134], [12400], [-24446], [51810], [-30348], [50138], [24976], [23116], [-64326], [12308], [-53362], [-86450], [-1232], [-31500], [824], [-10758], [-5896], [-57230], [92544], [52282], [42206], [-49448], [43874], [-55992], [-13236], [-74546], [-19354], [15424], [105136], [-74300], [29498], [-58116], [5984], [-25606], [45972], [54936], [-102606], [-51246], [-37084], [54024], [-3620], [63460], [-24448], [-15434], [-66370], [18280], [-66136], [46444], [66340], [-109912], [-5334], [31106], [75504], [-124774], [-40534], [-126612], [10094], [34164], [71970], [4086], [-66280], [-79780], [83358], [-3830], [16814], [68812], [-10954], [24810], [-139260], [37282], [-26602], [100748], [69366], [-82864], [-13106], [19888], [-112844], [-18874], [-82534], [95484], [50712], [101880], [94688], [-70658], [134852], [77600], [-22610], [-94254], [-124584], [-158814], [-144490], [121548], [-90522], [-76668], [29882], [99174], [-111232], [-67662], [84548], [-29604], [-141114], [-10038], [64736], [154076], [-131292], [-5506], [-51708], [122326], [-31224], [10546], [63112], [56814], [101346], [61580], [103952], [11962], [-9172], [-19056], [54878], [-130510], [-76084], [98326], [153702], [-30568], [-57194], [-10542], [84268], [84868], [44264], [182562], [117494], [159802], [98032], [-64532], [-137612], [-66540], [-60250], [-44274], [140794], [-206664], [802], [161382], [-53794], [198584], [80506], [-217060], [103198], [202724], [-204384], [177426], [155260], [176742], [71222], [-17048], [-85234], [-64826], [160896], [-218812], [-92546], [15920], [-115310], [205850], [210104], [32580], [77628], [205862], [-167190], [167488], [-98422], [75756], [-64828], [-103288], [92910], [15528], [141994], [-54420], [31984], [-69410], [4434], [219670], [26854], [-42218], [-194592], [-151410], [51660], [-72320], [-53072], [102690], [19716], [-257720], [100162], [-228318], [-160332], [92108], [-228104], [-201098], [-159568], [76506], [-158572], [-213714], [207942], [-188274], [-57832], [-101274], [-144258], [-87144], [-20134], [81042], [257852], [91068], [-130354], [226674], [88388], [8764], [-135802], [151926], [53696], [50480], [-208098], [-267024], [-140058], [226994], [266872], [-172540], [-114042], [42], [190140], [32202], [-169084], [-119384]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_600_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_600_4_a_q();".
// 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_600_4_a_q(:prec:=1)
    chi := MakeCharacter_600_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_600_4_a_q();".
// 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_600_4_a_q( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_600_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<7,R![-16, 1]>,<11,R![28, 1]>],Snew);
    return Vf;
end function;