
// Make newform 396.4.a.c in Magma, downloaded from the LMFDB on 11 March 2025.
// 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_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_396_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_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], [-10], [8], [11], [18], [-46], [40], [-44], [-186], [-72], [-114], [-174], [-416], [156], [62], [348], [-446], [-956], [444], [306], [-664], [124], [-602], [1522], [-1090], [-1392], [-308], [978], [-1162], [-984], [-2012], [1286], [464], [2894], [976], [1646], [3268], [-1608], [4070], [-52], [1798], [3852], [-1958], [1630], [-4504], [3776], [-1728], [-4716], [1766], [-4382], [-1264], [3010], [-5404], [-1618], [-48], [1814], [1016], [6914], [6098], [4192], [-9314], [1384], [-7916], [218], [-666], [-2500], [-6678], [1892], [-7350], [-2370], [-912], [-8464], [11890], [-9556], [-5236], [8262], [-2402], [-6290], [-6950], [-12660], [5342], [13560], [4658], [-6392], [-10772], [5606], [17050], [8438], [5064], [14268], [18312], [4376], [-17380], [11324], [-2392], [1238], [-21738], [22016], [9490], [21632], [4854], [5308], [12490], [-7448], [10994], [5148], [22314], [15588], [-21638], [-7496], [2106], [-26962], [-17740], [19360], [19158], [11228], [22628], [-28338], [17052], [-21354], [-22198], [32974], [-22572], [2700], [17382], [20454], [-10236], [9672], [-28078], [-26776], [-25280], [25160], [28910], [-10278], [-29558], [-9762], [14824], [-14946], [-14774], [22760], [-16370], [1784], [22052], [-29738], [-43956], [-6438], [2282], [-29972], [3716], [25194], [-27194], [-14300], [-4944], [-24332], [-20796], [15392], [15150], [15610], [45222], [14820], [-5334], [18400], [14460], [9998], [52548], [-7096], [45202], [-21086], [15398], [-51556], [26942], [-29376], [-1102], [-34944], [-46370], [23384], [-14010], [6984], [45866], [39400], [-58188], [29070], [-14646], [4404], [-55450], [10106], [-33540], [65946], [-10760], [-25438], [-41148], [5388], [77406], [38636], [-11194], [35730], [3902], [114], [14184], [7606], [14752], [64294], [-50646], [-23004], [52358], [14360], [14068], [-66534], [-73012], [71082], [24726], [-50384], [-60108], [-15840], [-28102], [-32672], [42394], [-1268], [34318], [-63518], [-52256], [2014], [-34640], [-436], [-10110], [23278], [-54780], [-40472], [-99684], [-71098], [-10496], [-31504], [91450], [-23884], [-112656], [-37246], [-51570], [-5100], [12892], [-94212], [-4720], [23488], [28622], [45722], [-78016], [-39552], [-40364], [-36952], [-5176], [2602], [-94374], [97732], [-25638], [62294], [-62820], [4286], [-3032], [4494], [104034], [-115400], [13764], [80250], [-126102], [66198], [38900], [-132626], [6558], [-40272], [-142258], [-36994], [-948], [-43670], [-122192], [20122], [64136], [47932], [10706], [15706], [-64468], [66072], [-16904], [-4416], [-102718], [120120], [133404], [-24254], [79614], [3816], [-28022], [41726], [25140], [-46206], [-44076], [130714], [77798], [66496], [-129730], [3636], [-44080], [-12142], [-152522], [-151280], [83388], [132076], [121218], [-32052], [-98842], [119004], [-33790], [-95808], [-159234], [65514], [120252], [51136], [133522], [-36004], [-136048], [-14094], [-106542], [-17256], [103786], [39142], [-162896], [-5198], [-112366], [84420], [-16444], [144816], [167278], [-105846], [-110226], [-179056], [-153388], [-130320], [129588], [-47586], [56042], [-165510], [27448], [-82874], [133182], [139102], [69736], [135798], [136828], [-117290], [-7540], [-81592], [-14162], [131696], [87338], [-190890], [-84600], [-179910], [-175910], [11492], [-222436], [51714], [108916], [-74902], [153778], [-107444], [99164], [-82636], [-93862], [116534], [195552], [154706], [-63804], [-155652], [-174912], [-13074], [39816], [-88282], [-34492], [93328], [-13774], [-83342], [57818], [25262], [-92658], [163424], [-111406], [-37528], [158052], [-43688], [-133970], [255120], [-115764], [-111950], [-14538], [233812], [-15972], [-15940], [97322], [231072], [-21266], [-67188], [71742], [-153422], [56334], [-163664], [114526], [139870], [136232], [250886], [162138], [183748], [110500], [185570], [208174], [17292], [87888], [100114], [125942], [307712], [-38192], [49142], [-88728], [291246], [9498], [14804], [-74676], [-96982], [-237306], [251364], [30098], [-169748], [-217872]];
    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_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_396_4_a_c(: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_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_396_4_a_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_396_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![10, 1]>,<7,R![-8, 1]>],Snew);
    return Vf;
end function;