
// Make newform 576.3.q.d in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_576_q();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_576_q_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_3_q_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_576_3_q_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 := [9, -3, -2, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [-3, -2, 2, 1], [0, 5, 1, -1], [-9, 2, 1, 2]];
    Rf_basisdens := [1, 6, 3, 3];
    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_q();"
function MakeCharacter_576_q()
    N := 576;
    order := 6;
    char_gens := [127, 325, 65];
    v := [6, 6, 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_576_q_Hecke();"
function MakeCharacter_576_q_Hecke(Kf)
    N := 576;
    order := 6;
    char_gens := [127, 325, 65];
    char_values := [[1, 0, 0, 0], [1, 0, 0, 0], [0, 1, 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 := 3;
    raw_aps := [[0, 0, 0, 0], [-1, 0, 0, 1], [-4, 1, -2, 1], [-2, -1, -3, 3], [5, 7, -2, -2], [0, -1, 0, -3], [-7, 16, 1, -2], [1, 0, 3, 0], [32, -17, -2, 1], [14, 7, 7, 7], [0, 1, 0, -9], [10, 0, -12, 0], [-14, -1, -16, 8], [23, -23, 0, 0], [-12, -15, 3, 3], [-16, 16, -8, 16], [-50, 17, -16, 8], [-16, 25, 9, -9], [0, -49, 0, -18], [14, -32, -2, 4], [17, 0, -9, 0], [34, -49, -15, 15], [12, 15, -3, -3], [56, -128, -8, 16], [-101, 95, -6, 6], [-74, -73, -1, -1], [0, -95, 0, -9], [5, -32, -11, 22], [-92, 0, -18, 0], [-120, 39, -42, 21], [56, 0, 54, 0], [8, 25, 58, -29], [67, 65, 2, 2], [0, 47, 0, -6], [16, -31, -46, 23], [-164, 191, 27, -27], [0, 143, 0, -63], [-134, 0, -12, 0], [12, 15, 42, -21], [66, 87, -21, -21], [68, -176, -20, 40], [-80, 0, 42, 0], [38, 1, 37, 37], [0, 49, 0, 0], [-56, -16, -64, 128], [-118, 0, 24, 0], [0, 215, 0, 51], [4, -49, -45, 45], [153, 135, 18, 18], [0, -145, 0, -45], [-155, 272, -19, 38], [-144, 63, -18, 9], [97, -1, 96, -96], [77, -32, 61, -122], [42, -33, -24, 12], [26, 49, -23, -23], [92, -272, -44, 88], [-28, 0, -126, 0], [-328, 265, -63, 63], [76, 137, -61, -61], [0, -361, 0, -9], [-116, 65, 14, -7], [151, 0, 9, 0], [-476, 191, -94, 47], [-299, 287, -12, 12], [28, 23, 5, 5], [218, -95, 123, -123], [0, -47, 0, 114], [150, -159, -168, 84], [56, 73, 129, -129], [-257, -223, -34, -34], [270, -576, -18, 36], [-146, 47, -99, 99], [0, 143, 0, -3], [-287, 0, -93, 0], [-20, 95, 170, -85], [-110, -73, -37, -37], [-146, 0, 72, 0], [22, -1, 20, -10], [0, 97, 0, 114], [312, -87, 138, -69], [38, -215, -177, 177], [422, -800, 22, -44], [-7, 0, -81, 0], [76, 95, 171, -171], [255, 231, 24, 24], [-33, 240, 87, -174], [-113, 95, -18, 18], [-148, -137, -11, -11], [0, -431, 0, 57], [185, -320, 25, -50], [-18, 81, -99, -99], [-82, 0, 204, 0], [274, -127, 20, -10], [0, -1, 0, 162], [-120, 192, -24, 48], [-664, 337, 10, -5], [-13, 112, 43, -86], [-566, 0, 36, 0], [-98, 0, 168, 0], [155, -263, -108, 108], [46, 80, 86, -172], [-1146, 561, -24, 12], [-151, -287, 136, 136], [0, 191, 0, -258], [-127, 0, -9, 0], [61, 215, -154, -154], [-340, 736, 28, -56], [-484, 175, -134, 67], [283, -481, -198, 198], [0, 529, 0, 87], [226, 0, 108, 0], [-842, 431, 20, -10], [-547, 457, -90, 90], [638, 0, 156, 0], [-17, 113, -130, -130], [0, -577, 0, 0], [-378, 576, -90, 180], [-836, 497, 158, -79], [-8, -193, 185, 185], [0, 1103, 0, -45], [-218, 455, 237, -237], [-524, -505, -19, -19], [39, -192, -57, 114], [-706, 625, -81, 81], [-232, 304, -80, 160], [-52, -23, -75, 75], [-54, -288, -198, 396], [-536, 671, 135, -135], [0, -433, 0, 315], [-569, 0, 9, 0], [-204, 15, -174, 87], [0, 97, 0, 105], [694, 0, -144, 0], [-1368, 711, 54, -27], [0, -791, 0, 129], [156, -624, -156, 312], [0, 455, 0, 207], [660, -255, 150, -75], [551, -1136, -17, 34], [385, 0, 243, 0], [-374, -313, -61, -61], [0, -287, 0, 21], [-284, 752, 92, -184], [-386, 0, 120, 0], [204, 273, -69, -69], [-1168, 1177, 9, -9], [252, 297, -45, -45], [0, -529, 0, 12], [-226, 352, -50, 100], [0, 767, 0, -15], [532, -928, 68, -136], [397, 0, -177, 0], [748, -385, -22, 11], [-61, 265, 204, -204], [48, 33, 15, 15], [-1168, 0, 126, 0], [-180, -279, 99, 99], [-730, 0, -348, 0], [-224, 65, -94, 47], [261, 135, 126, 126], [-171, -144, -243, 486], [0, 481, 0, -387], [-108, -432, -324, 648], [302, -305, -308, 154], [-598, -623, 25, 25], [548, 0, 450, 0], [932, -887, 45, -45]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_576_q_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_576_3_q_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_576_3_q_d(:prec:=4)
    chi := MakeCharacter_576_q();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 3));
    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_3_q_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_576_3_q_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_576_q();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<5,R![324, -162, 9, 9, 1]>,<7,R![5476, -74, 75, 1, 1]>],Snew);
    return Vf;
end function;