
// Make newform 400.8.c.d in Magma, downloaded from the LMFDB on 25 August 2026.
// To make the character of type GrpDrchElt, type "MakeCharacter_400_c();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_400_c_Hecke();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_400_8_c_d();".
// 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_400_8_c_d();".
// 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
        poly := [1, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_400_c();"
function MakeCharacter_400_c()
    N := 400;
    order := 2;
    char_gens := [351, 101, 177];
    v := [2, 2, 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;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_400_c_Hecke();"
function MakeCharacter_400_c_Hecke( : Kf := false)
    N := 400;
    order := 2;
    char_gens := [351, 101, 177];
    char_values := [[1, 0], [1, 0], [-1, 0]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := ISA(Type(Kf), Fld), Kf := Kf); // the value of chi on the gens as elements in the Hecke field
    F := Universe(values);// the Hecke field
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),values);
    return chi;
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 := 8;
    raw_aps := [[0, 0], [0, 57], [0, 0], [0, 1174], [7563, 0], [0, -5372], [0, 24021], [-51235, 0], [0, -57618], [-47040, 0], [192358, 0], [0, 197066], [-237723, 0], [0, 653012], [0, 826884], [0, -569022], [1501080, 0], [-2068918, 0], [0, 3444349], [-4121052, 0], [0, 83653], [1454030, 0], [0, 1626567], [-6004335, 0], [0, 3411746], [-3955578, 0], [0, 1750892], [0, -8833941], [12216130, 0], [0, -20783607], [0, 24483814], [-32384532, 0], [0, -9347019], [8242925, 0], [-21924000, 0], [33704998, 0], [0, -47301574], [0, -71166133], [0, -40227276], [0, 18553608], [-86619975, 0], [-135591958, 0], [41832858, 0], [0, -58090517], [0, -97096794], [-179264380, 0], [-231818147, 0], [0, 188141852], [0, 8327244], [323445340, 0], [0, 147345738], [307841520, 0], [-198491143, 0], [-44259627, 0], [0, 103727886], [0, -413809698], [417686640, 0], [171957238, 0], [0, 89207696], [-269319138, 0], [0, 550134827], [0, -549003342], [0, -10302281], [398417898, 0], [0, 242289238], [0, -367441524], [189017623, 0], [0, -486113389], [0, 296818539], [-523668590, 0], [0, 364070778], [-665946090, 0], [0, -876030596], [0, 1146921358], [48809015, 0], [0, 121895262], [2165408250, 0], [0, -1073217964], [469500357, 0], [-263782685, 0], [-1972295655, 0], [-2331942508, 0], [-1625901462, 0], [0, 1228648363], [377158280, 0], [0, 1838187447], [1398495015, 0], [0, 195194681], [1842677772, 0], [0, 3754393772], [0, -1488492276], [1937318070, 0], [0, 2742688534], [-1771992612, 0], [4671916220, 0], [0, 4722922212], [663451350, 0], [-1895575083, 0], [0, 78497537], [5895366992, 0], [0, -4184042201], [0, -2391549204], [0, -791939088], [-851733945, 0], [996220708, 0], [0, 3548858531], [0, 3085542129], [0, -2687267847], [-1455102570, 0], [1110683267, 0], [0, 8186761264], [0, -3605202182], [0, 3186512766], [-6604786420, 0], [10310621158, 0], [3532532682, 0], [0, 4621518212], [0, 13029924084], [0, 6366630978], [-9439195905, 0], [5973171152, 0], [0, 3908517958], [0, 1199249136], [0, 2870907777], [-725979257, 0], [-3774454308, 0], [-14370856100, 0], [3437279130, 0], [0, -10274451296], [0, -15603075932], [-16438133860, 0], [0, -18099147858], [-2039441072, 0], [0, 1941320336], [-8346213963, 0], [12890162035, 0], [0, 17631782808], [0, 2667737044], [0, -2124072174], [-28208831250, 0], [-25324749212, 0], [4062615522, 0], [0, 1539357452], [0, -16976327391], [6611920540, 0], [4714027920, 0], [0, -3773089682], [0, 27000999141], [-10477431715, 0], [0, 25215540312], [0, 4306197476], [31213990062, 0], [0, 6519543617], [0, 28284880164], [0, -30012726716], [34344219828, 0], [7546525550, 0], [-20290717470, 0], [0, -1769960989], [12540679212, 0], [0, -29644882296], [0, 45754236063], [0, -38803436816], [43046427483, 0], [0, 39679999521], [0, -36123390738], [-47172552782, 0], [0, -3995703964]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_400_c_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_400_8_c_d();".
// 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_400_8_c_d(:prec:=2)
    chi := MakeCharacter_400_c();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 8));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(997) - 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_400_8_c_d();".
// 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_400_8_c_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_400_c();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,8,sign)));
    Vf := Kernel([<3,R![3249, 0, 1]>],Snew);
    return Vf;
end function;