
// Make newform 576.4.a.c in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_576_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_576_4_a_c();".
// 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_576_4_a_c();".
// 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_576_a();"
function MakeCharacter_576_a()
    N := 576;
    order := 1;
    char_gens := [127, 325, 65];
    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_576_a_Hecke(Kf)
    return MakeCharacter_576_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], [-16], [-12], [-64], [-58], [32], [136], [-128], [144], [20], [18], [-288], [200], [384], [-496], [128], [458], [496], [512], [-602], [1108], [-704], [-960], [206], [-432], [-68], [384], [518], [-960], [796], [-512], [1824], [2160], [688], [-844], [-118], [-3576], [384], [-2448], [4224], [510], [-384], [-3454], [3216], [1708], [-2320], [-116], [-1344], [-4594], [5056], [-3712], [-978], [-1856], [7808], [1024], [-1328], [-5812], [-8386], [-640], [-4832], [-6384], [3312], [9984], [2586], [2832], [5920], [-4674], [9024], [4362], [8768], [-6144], [4564], [8770], [1096], [10368], [3248], [6106], [7008], [1590], [192], [-9074], [-5248], [-8222], [16236], [-14528], [6304], [1958], [-4048], [16988], [-6720], [9728], [-8444], [15360], [-6624], [-6912], [19920], [3680], [11720], [-11754], [18904], [-3088], [21440], [22624], [6000], [19922], [3584], [1984], [14976], [25738], [-8548], [-8558], [-10368], [13088], [-4412], [-30176], [-21288], [17024], [-2256], [-23808], [26242], [-24590], [2864], [7616], [-2168], [18000], [-3506], [15616], [-15036], [19126], [17392], [-32384], [-27708], [37246], [-4192], [26882], [17232], [-31816], [-8272], [-9184], [19832], [-15216], [-39772], [18304], [-4906], [15360], [24802], [15072], [-1800], [7552], [-20838], [47744], [-28280], [7424], [5912], [-26240], [35620], [-3232], [-11478], [39984], [-24192], [-39456], [-41668], [51648], [55776], [-36096], [42532], [-29806], [-9250], [-47440], [13056], [-45750], [-39808], [14326], [40772], [-12544], [-21056], [-42576], [-8660], [-9158], [12692], [-40640], [2382], [18816], [-4992], [-336], [15254], [53280], [4246], [63744], [25918], [-8832], [21440], [-44944], [28416], [12032], [26338], [9206], [17792], [-47616], [-8336], [-13204], [52894], [28366], [-58560], [51600], [34316], [20928], [-38464], [32888], [18930], [76848], [-15860], [-58496], [-93952], [-60166], [-90412], [-35168], [-80256], [-79248], [9262], [-8820], [15072], [85340], [-34752], [96610], [20928], [-51456], [3996], [-50176], [-48762], [62648], [84476], [59008], [33048], [5120], [-57922], [-2512], [87232], [24448], [-94272], [-39488], [-3740], [-89350], [-85248], [-11776], [-101084], [81792], [-60640], [26624], [-4362], [59712], [92288], [77686], [100944], [127872], [57022], [-7984], [-112176], [60778], [-106964], [-11136], [-107026], [-73878], [67552], [1984], [97840], [-1216], [91288], [-36560], [95834], [-139952], [68998], [64516], [-66770], [-50068], [-74688], [81706], [5146], [55424], [-87296], [6988], [-130688], [-97934], [-107912], [131584], [145166], [-78480], [133556], [-16192], [111536], [133248], [29248], [-21888], [86010], [48944], [52604], [3024], [62016], [-49448], [128522], [54640], [43076], [38144], [112104], [-150754], [74688], [6406], [102656], [-76334], [-107904], [-12336], [-12640], [38312], [147712], [80918], [-56832], [7680], [-158290], [58176], [10784], [-20294], [-111344], [-166604], [-90656], [38622], [121512], [71496], [173056], [66288], [-93850], [-111120], [143148], [-108672], [72816], [158784], [-84502], [-207936], [-162106], [-2340], [-37342], [46176], [58704], [-127716], [138704], [92864], [9390], [-93760], [-40704], [198352], [38712], [-75610], [204944], [101964], [-169982], [-118528], [206464], [-41984], [97312], [132864], [-86030], [-133440], [-74112], [-73664], [-55216], [-8454], [-136944], [135124], [-178550], [150592], [137928], [104960], [-216688], [118180], [-52394], [-95040], [117888], [-141662], [192864], [239654], [-43536], [-239584], [-100716], [65664], [-64456], [-1152], [-215580], [194398], [-61744], [168576], [133438], [-32848], [-103680], [49896], [64128], [-11210], [-74100], [-161952], [192040], [78704], [-187190], [-151968], [-123332], [25184], [-212080], [-158828], [-32026], [102240], [129832], [-91968], [-115198], [56816], [195520], [204392], [-2682], [-131056], [-221568], [-252172], [74592], [26624], [22576], [130930], [41984], [-137024], [-248438], [-51440], [-104512], [-204480], [64248], [234112]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_576_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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