
// Make newform 1296.4.a.e in Magma, downloaded from the LMFDB on 30 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1296_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_1296_4_a_e();".
// 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_1296_4_a_e();".
// 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_1296_a();"
function MakeCharacter_1296_a()
    N := 1296;
    order := 1;
    char_gens := [1135, 325, 1217];
    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_1296_a_Hecke(Kf)
    return MakeCharacter_1296_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], [3], [4], [24], [-25], [-21], [52], [-168], [-177], [124], [-265], [426], [160], [540], [-258], [-528], [-505], [244], [-204], [-397], [-200], [540], [-453], [290], [1374], [-1268], [-1164], [587], [-1233], [-1496], [2004], [2775], [-2012], [-2145], [-3176], [515], [-3272], [684], [3123], [-2892], [-1426], [1596], [1115], [-1245], [700], [-704], [880], [3924], [-6145], [-3885], [-4860], [-4549], [-4800], [2511], [4548], [-2517], [-4040], [-7090], [-3165], [6748], [6495], [8980], [-3864], [5495], [-885], [10924], [-7870], [4716], [-1642], [3090], [5688], [-6776], [6158], [-7484], [-4620], [-930], [1919], [-10005], [7655], [14040], [11219], [1884], [17507], [3508], [-12420], [8658], [335], [246], [-14312], [-8184], [-18108], [-13700], [20136], [13408], [21600], [-5946], [8970], [12520], [3911], [17020], [13875], [-540], [-12513], [20416], [-1801], [5520], [-11937], [-16188], [-11941], [12460], [4670], [9435], [13960], [18904], [-21381], [1420], [21204], [23190], [9804], [-30241], [803], [-14466], [7020], [24460], [27651], [-4921], [20232], [-6500], [13718], [13588], [-34092], [940], [-5410], [-6849], [-9133], [-37845], [33976], [-23805], [6567], [8656], [5991], [-45812], [-35160], [-9898], [-4032], [18638], [37575], [6268], [44832], [14495], [25986], [-34868], [6720], [17296], [14256], [-15752], [31563], [-3901], [-53445], [-9036], [-25065], [-14444], [-50004], [-45390], [-35952], [-14156], [-51541], [41507], [24942], [23388], [-23281], [19356], [42827], [-37280], [15162], [-11360], [24939], [-49520], [-41497], [-5360], [38172], [8147], [-3654], [-56640], [5523], [14291], [23728], [-54358], [1020], [-313], [59400], [28912], [18699], [-45420], [77658], [8831], [-18970], [591], [70680], [26487], [82024], [-52306], [-15553], [-35004], [-38010], [13732], [-38280], [-56550], [63580], [-81454], [21759], [-42740], [41160], [19512], [-91126], [20104], [56331], [-20316], [22767], [-66589], [-37388], [-21813], [-30080], [-1584], [-12517], [-1782], [-67476], [65200], [-75252], [54347], [49588], [-40880], [-20517], [-78620], [-99624], [51563], [-40338], [44280], [-64596], [-6432], [-17360], [70120], [104390], [-28110], [-51888], [-36020], [68280], [-58232], [21600], [92495], [65511], [31800], [29987], [89055], [76620], [-54289], [89044], [98199], [29594], [45520], [2160], [-52597], [73670], [-69630], [-53948], [81915], [-109989], [-99560], [92763], [-35149], [19480], [-48310], [80668], [16679], [87940], [-75216], [21107], [-65005], [-28032], [17880], [50560], [36996], [-86545], [93736], [-14724], [136955], [76110], [144748], [102375], [-157626], [149100], [110283], [-68436], [149087], [-21117], [124144], [-132717], [134208], [-33536], [-122857], [-33066], [-64748], [-112152], [-57416], [61634], [107304], [45782], [66972], [-73465], [-78348], [48078], [131139], [-16580], [-92004], [-171718], [32568], [40584], [-20545], [-23742], [73792], [116426], [23979], [42712], [-155910], [51179], [-54848], [-91928], [7380], [42942], [-55306], [-126885], [-169448], [-201528], [124], [100320], [31967], [-178953], [-114406], [104920], [-39805], [-113766], [74043], [-78980], [219507], [-67980], [46991], [-13880], [113196], [28335], [-191336], [133535], [-167133], [158272], [-229357], [-37293], [98748], [189336], [-184206], [65220], [-24505], [128739], [-186984], [-117888], [110320], [45995], [159990], [-91820], [-48166], [26736], [-236120], [150792], [-230457], [235744], [15131], [-29952], [-100296], [-97585], [-50862], [-222121], [259887], [92463], [-193100], [134511], [-230168], [2568], [-162812], [-177529], [27268], [-90216], [-7945], [-149925], [480], [105160], [101664], [-277345], [98212], [-117945], [-114680], [213879], [-136357], [-37470], [200284], [116991], [-290493], [29320], [153191], [-35565], [47440], [53292], [-188377], [108471], [-181452], [27064], [84155], [-144057], [-94692], [2536], [20379], [47328], [-66570], [-65410], [146640], [-129672], [-165625], [-87465], [37500], [-181269], [-62696], [-210840]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1296_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1296_4_a_e();".
// 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_1296_4_a_e(:prec:=1)
    chi := MakeCharacter_1296_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_1296_4_a_e();".
// 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_1296_4_a_e( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1296_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![-3, 1]>],Snew);
    return Vf;
end function;