
// Make newform 396.4.a.a in Magma, downloaded from the LMFDB on 30 December 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_396_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_396_4_a_a();".
// 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_396_4_a_a();".
// 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_396_a();"
function MakeCharacter_396_a()
    N := 396;
    order := 1;
    char_gens := [199, 353, 145];
    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_396_a_Hecke(Kf)
    return MakeCharacter_396_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]];
    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], [-22], [-20], [-11], [22], [-110], [48], [-72], [142], [184], [-194], [482], [-80], [-392], [34], [108], [382], [84], [1040], [-606], [-1292], [-356], [406], [1090], [-490], [1672], [900], [-1834], [774], [-1324], [2340], [-434], [2232], [-3058], [1340], [-3498], [1036], [2304], [-114], [-516], [686], [88], [3218], [-666], [-1744], [48], [4448], [1212], [-3074], [1314], [5656], [5418], [-4460], [2382], [-1648], [6586], [-452], [-6802], [2098], [832], [-706], [8816], [632], [666], [1186], [3716], [4010], [6420], [-8162], [-1938], [10936], [4784], [7942], [52], [7488], [-4358], [7014], [11622], [5954], [7884], [11646], [-2008], [-6846], [-5468], [-13140], [510], [1378], [-954], [-2432], [-7964], [19144], [-18080], [-13164], [-7628], [14080], [-1670], [-6162], [4160], [-10074], [10408], [-3082], [4356], [14282], [16640], [-20510], [24908], [2858], [8232], [-2550], [5780], [-106], [-6802], [-22172], [-12960], [-15570], [9308], [12344], [-21702], [-26732], [-6498], [3122], [16550], [-25372], [-692], [13110], [22750], [-18584], [1744], [3478], [12584], [7496], [1528], [9582], [-21350], [10978], [14146], [30296], [13762], [-7670], [20136], [-37370], [35832], [-32508], [22662], [17304], [26886], [-10966], [-35468], [-20832], [9214], [13798], [-23548], [3816], [-13404], [-8056], [17444], [11430], [37546], [-44034], [-34148], [-8502], [-23572], [27548], [4742], [24488], [47368], [-46666], [34250], [-61598], [-8532], [-41354], [5880], [60402], [-51232], [-18882], [-33680], [20754], [-17476], [-41634], [-61400], [-57036], [-2850], [-26006], [-17656], [-56910], [37126], [40796], [25250], [39096], [-57710], [73988], [15652], [32994], [14932], [-64730], [-11102], [-30490], [-42126], [52792], [25046], [54132], [-43602], [68650], [-58588], [-42478], [10064], [-46220], [-42150], [63980], [-73006], [-1950], [-50080], [16172], [30552], [32042], [10748], [53418], [91136], [41658], [-41986], [89676], [6614], [67200], [-69676], [57574], [87518], [-10800], [-3612], [-18156], [1334], [-65648], [70612], [4698], [17036], [77744], [104834], [59198], [65412], [-33672], [35460], [19808], [38960], [-43226], [98234], [-21400], [-87752], [52668], [107120], [-17808], [83942], [-59782], [27376], [90506], [115446], [-32628], [-59666], [15656], [-5334], [25546], [-92660], [103220], [50862], [-14818], [-40866], [43188], [96714], [34446], [-73448], [83366], [45606], [68204], [45498], [-47332], [-104166], [72672], [-55500], [87710], [-97558], [-47500], [-37432], [-44016], [-81576], [40118], [72816], [120384], [34866], [-70058], [58560], [-86454], [-14430], [-15244], [-51902], [-12916], [46926], [-12810], [-37448], [3282], [-16204], [121568], [1386], [-117354], [77556], [-31212], [41676], [17170], [128052], [103574], [73656], [156006], [-42568], [70394], [53722], [-56236], [79008], [6370], [-9148], [25008], [164786], [-5934], [111600], [143594], [29990], [-76776], [-62814], [-44110], [12564], [-89156], [-13624], [-98682], [38502], [49986], [85828], [177900], [169216], [9612], [-29226], [27210], [-118982], [-44188], [114398], [-213274], [-135826], [54296], [118938], [51548], [-119522], [56548], [34304], [132370], [143728], [-124806], [-178086], [116388], [-86618], [126266], [-9480], [-166780], [190530], [-229744], [-35130], [236818], [15936], [-5340], [70404], [-27366], [28438], [-115444], [-71334], [-115260], [-206684], [226560], [-7226], [22724], [-112762], [129324], [85208], [75346], [176178], [40594], [-146862], [-216730], [155212], [-152398], [231912], [-231840], [180120], [201102], [-7504], [-141640], [-105406], [133650], [168420], [-243148], [22440], [37858], [-146140], [42070], [-247612], [-180570], [56590], [66342], [-181900], [-174426], [-121746], [-1220], [206166], [68362], [-114716], [118316], [10370], [132286], [-145900], [-286968], [145746], [-120062], [-205032], [124936], [156350], [-82336], [-228838], [7606], [33768], [40068], [-239142], [41290], [-111284], [227442], [74804], [129376]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_396_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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