
// Make newform 400.8.c.a in Magma, downloaded from the LMFDB on 11 September 2025.
// 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_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_400_8_c_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
        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, 87], [0, 0], [0, -1366], [1083, 0], [0, 5468], [0, -25269], [33485, 0], [0, -5838], [-125280, 0], [73798, 0], [0, 395926], [-22683, 0], [0, -100148], [0, 1145244], [0, -354882], [1098360, 0], [-422998, 0], [0, 2558579], [2287428, 0], [0, 6372443], [-2019250, 0], [0, -7972983], [-2185935, 0], [0, 5823646], [433062, 0], [0, 10429492], [0, 21316389], [-16030430, 0], [0, 905463], [0, 16035194], [-3830772, 0], [0, -9693429], [44940605, 0], [49420800, 0], [-20107802, 0], [0, -78669434], [0, 69974917], [0, -83891796], [0, -27366792], [-79763415, 0], [52871402, 0], [103225338, 0], [0, -49965067], [0, 33385626], [92510660, 0], [-56115347, 0], [0, 77993572], [0, 131342484], [-228442340, 0], [0, 377948598], [-113186160, 0], [-200235943, 0], [462823173, 0], [0, 487751346], [0, 401821122], [-455386320, 0], [-508458602, 0], [0, -646826864], [189546462, 0], [0, -463383803], [0, 123173358], [0, 564202169], [-551416662, 0], [0, -171415702], [0, 1033492596], [-24123257, 0], [0, 875079661], [0, -83244891], [-397606670, 0], [0, -293709882], [-171667050, 0], [0, -955463356], [0, 344673458], [1452725255, 0], [0, 1878392802], [1126315770, 0], [0, 1457334316], [1958492517, 0], [1255936675, 0], [-44654775, 0], [-58312108, 0], [-2158067862, 0], [0, 2028669173], [-2516405080, 0], [0, 2567198457], [-828488505, 0], [0, -345538169], [481628172, 0], [0, -1183069868], [0, 1011914004], [3786731670, 0], [0, -720259126], [1796669628, 0], [2545397660, 0], [0, 1314007452], [2503855830, 0], [-1220282283, 0], [0, -1933184273], [-2549203888, 0], [0, 4582911689], [0, 4942073076], [0, -782258448], [-1196276025, 0], [-2410706492, 0], [0, 6453941341], [0, 527193759], [0, -8990563257], [3407202390, 0], [-4008079453, 0], [0, 3611514704], [0, 416268038], [0, 232065666], [-1509137620, 0], [-11851212602, 0], [-3894752118, 0], [0, 687186652], [0, -2241571956], [0, 3445687518], [7597527855, 0], [-549900688, 0], [0, 72947258], [0, 3970034256], [0, 11894291247], [-2089482857, 0], [12181797372, 0], [-3807262820, 0], [2744255130, 0], [0, 515574464], [0, 9167913788], [-7951336900, 0], [0, -12033305358], [14665047088, 0], [0, 18675696496], [14550158517, 0], [-7455064685, 0], [0, 18567068808], [0, -217239796], [0, -10402077714], [-15401977170, 0], [-9379140092, 0], [-14918954718, 0], [0, -17290505228], [0, -192632721], [-15519895940, 0], [10410809040, 0], [0, -15450604462], [0, -2503113189], [20584244525, 0], [0, -19107304248], [0, -33306337124], [-9313125138, 0], [0, -4385142833], [0, -17876414436], [0, -2847419236], [38464295028, 0], [10737538190, 0], [-5344928670, 0], [0, 14812454461], [21873447372, 0], [0, 24516334104], [0, 13405631073], [0, -4647400816], [32485958283, 0], [0, 6065058111], [0, 21084216402], [-42647626382, 0], [0, 15922634716]];
    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_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_400_8_c_a(: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_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_400_8_c_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_400_c();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,8,sign)));
    Vf := Kernel([<3,R![7569, 0, 1]>],Snew);
    return Vf;
end function;