
// Make newform 576.4.d.b in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_576_d();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_576_d_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_576_4_d_b();".
// 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_576_4_d_b();".
// 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, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 4, 0, -2], [-4, 0, 8, 0], [0, 0, 0, 8]];
    Rf_basisdens := [1, 1, 1, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    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_576_d();"
function MakeCharacter_576_d()
    N := 576;
    order := 2;
    char_gens := [127, 325, 65];
    v := [2, 1, 2];
    // 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_576_d_Hecke();"
function MakeCharacter_576_d_Hecke(Kf)
    N := 576;
    order := 2;
    char_gens := [127, 325, 65];
    char_values := [[1, 0, 0, 0], [-1, 0, 0, 0], [1, 0, 0, 0]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := true, 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+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, 0, 0], [0, 0, 0, 0], [0, 0, -2, 0], [0, 1, 0, 0], [0, 0, 0, 6], [0, 0, 3, 0], [-96, 0, 0, 0], [0, 0, 0, 5], [0, -32, 0, 0], [0, 0, -2, 0], [0, 59, 0, 0], [0, 0, 43, 0], [288, 0, 0, 0], [0, 0, 0, 19], [0, 160, 0, 0], [0, 0, 26, 0], [0, 0, 0, -60], [0, 0, 109, 0], [0, 0, 0, 106], [0, -256, 0, 0], [-538, 0, 0, 0], [0, 291, 0, 0], [0, 0, 0, 54], [-1344, 0, 0, 0], [-590, 0, 0, 0], [0, 0, -142, 0], [0, 415, 0, 0], [0, 0, 0, 228], [0, 0, 151, 0], [192, 0, 0, 0], [0, 229, 0, 0], [0, 0, 0, -168], [864, 0, 0, 0], [0, 0, 0, -154], [0, 0, 414, 0], [0, -215, 0, 0], [0, 0, 89, 0], [0, 0, 0, 245], [0, 992, 0, 0], [0, 0, 274, 0], [0, 0, 0, 132], [0, 0, -283, 0], [0, -544, 0, 0], [962, 0, 0, 0], [0, 0, -678, 0], [0, -897, 0, 0], [0, 0, 0, 394], [0, -1595, 0, 0], [0, 0, 0, 306], [0, 0, -319, 0], [-5568, 0, 0, 0], [0, -1504, 0, 0], [2798, 0, 0, 0], [0, 0, 0, 582], [-1536, 0, 0, 0], [0, -384, 0, 0], [0, 0, 102, 0], [0, -1379, 0, 0], [0, 0, -847, 0], [-1536, 0, 0, 0], [0, 0, 0, -844], [0, 0, -742, 0], [0, 0, 0, 778], [0, 704, 0, 0], [934, 0, 0, 0], [0, 0, -378, 0], [0, 0, 0, -1228], [-4862, 0, 0, 0], [0, 0, 0, 66], [0, 0, -959, 0], [2112, 0, 0, 0], [0, -1792, 0, 0], [0, 19, 0, 0], [0, 0, 1179, 0], [0, 0, 0, -1181], [0, -544, 0, 0], [0, 0, -1306, 0], [0, 0, 905, 0], [-15648, 0, 0, 0], [-13642, 0, 0, 0], [0, 0, 0, 2010], [0, 0, -215, 0], [0, 1952, 0, 0], [-5474, 0, 0, 0], [0, -537, 0, 0], [0, 0, 0, 2178], [-3168, 0, 0, 0], [3878, 0, 0, 0], [0, 0, -1422, 0], [0, 4749, 0, 0], [0, 0, 0, -486], [0, 2688, 0, 0], [0, 3489, 0, 0], [0, 0, 0, 984], [0, 0, 0, 2092], [0, 448, 0, 0], [0, 0, -226, 0], [12192, 0, 0, 0], [0, 0, 0, -461], [0, 0, 1075, 0], [0, 0, 0, 119], [0, 0, 2010, 0], [0, 0, 0, -462], [-6240, 0, 0, 0], [0, 0, 0, 902], [-4754, 0, 0, 0], [0, 0, 0, 144], [17472, 0, 0, 0], [0, 7840, 0, 0], [-9142, 0, 0, 0], [0, 8341, 0, 0], [0, 0, -1325, 0], [-14208, 0, 0, 0], [0, 0, 0, -460], [0, -2551, 0, 0], [-2400, 0, 0, 0], [0, 0, 0, 23], [0, 3488, 0, 0], [0, 0, -3774, 0], [0, 0, 0, 2352], [0, 0, 2407, 0], [29326, 0, 0, 0], [0, 0, 3166, 0], [0, 0, 0, 2694], [0, 0, 0, -3355], [0, 0, -810, 0], [0, 0, -4659, 0], [0, -3648, 0, 0], [0, -5895, 0, 0], [0, 0, -89, 0], [0, 0, 0, -3158], [0, 5216, 0, 0], [0, 451, 0, 0], [0, 0, -1027, 0], [26208, 0, 0, 0], [12866, 0, 0, 0], [0, 0, 1778, 0], [0, 0, 0, 227], [0, 0, 2410, 0], [24288, 0, 0, 0], [0, 0, 0, -1019], [0, 0, -6494, 0], [0, -2103, 0, 0], [0, 0, 0, 492], [0, 0, -4065, 0], [0, -3200, 0, 0], [0, 0, -6469, 0], [39840, 0, 0, 0], [0, 0, 0, 1429], [0, 1184, 0, 0], [0, 0, 5173, 0], [-33024, 0, 0, 0], [0, 0, 0, -4507], [0, 3264, 0, 0], [0, 0, 0, 4225], [0, 8992, 0, 0], [0, -6279, 0, 0], [9312, 0, 0, 0], [37846, 0, 0, 0], [0, 0, 4162, 0], [0, 0, 0, 4332], [-52704, 0, 0, 0], [0, 9647, 0, 0], [0, 0, 0, 798], [31200, 0, 0, 0], [0, -7104, 0, 0], [0, -3797, 0, 0], [0, 0, 4419, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_576_d_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_576_4_d_b();".
// 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_576_4_d_b(:prec:=4)
    chi := MakeCharacter_576_d();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    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_576_4_d_b();".
// 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_576_4_d_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_576_d();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![192, 0, 1]>,<7,R![-12, 0, 1]>,<17,R![96, 1]>],Snew);
    return Vf;
end function;