
// Make newform 576.3.q.f in Magma, downloaded from the LMFDB on 13 May 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_f();".
// 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_f();".
// 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 := [4, 0, -2, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 0, 1, 0], [0, 2, 0, 1], [0, 4, 0, -1]];
    Rf_basisdens := [1, 2, 2, 2];
    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], [1, -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, -2, 0, 1], [3, 3, 0, 0], [0, 1, 3, 0], [-6, 3, -1, 2], [5, -5, 6, -6], [-6, 12, 4, -2], [10, 0, 0, 6], [3, 3, -1, -1], [-6, 3, 2, -4], [19, -19, 9, -9], [-32, 0, 0, -6], [-21, -21, 6, 6], [0, 23, 9, 0], [18, -9, -7, 14], [30, -60, -20, 10], [-21, -21, 13, 13], [0, -31, 18, 0], [53, -53, -9, 9], [30, -60, 16, -8], [-52, 0, 0, 18], [0, 7, 15, 0], [126, -63, -5, 10], [30, -60, -44, 22], [0, 7, 42, 0], [6, -3, 36, -72], [37, -37, -27, 27], [-60, 120, -32, 16], [16, 0, 0, 54], [9, 9, -44, -44], [-34, 0, 0, -18], [-69, -69, -27, -27], [222, -111, 10, -20], [11, -11, 39, -39], [-3, -3, -26, -26], [0, 175, -45, 0], [-109, 109, -36, 36], [58, 0, 0, -78], [-99, -99, -29, -29], [-18, 9, -20, 40], [-96, 192, -56, 28], [-140, 0, 0, -42], [-6, 3, -3, 6], [-125, 125, 90, -90], [24, -48, 56, -28], [80, 0, 0, 30], [-103, 103, -21, 21], [0, -203, -45, 0], [-54, 27, -29, 58], [143, -143, 108, -108], [-48, 96, 32, -16], [-9, -9, -17, -17], [0, -143, -24, 0], [48, -96, 64, -32], [-27, -27, -8, -8], [462, -231, -15, 30], [30, -60, 4, -2], [-34, 0, 0, -126], [0, -49, 0, 0], [-210, 105, -38, 76], [-145, 145, -81, 81], [87, 87, -32, -32], [-110, 0, 0, 18], [-93, -93, -21, -21], [0, 397, 12, 0], [330, -165, -42, 84], [0, 245, 93, 0], [-335, 335, -12, 12], [-207, -207, 53, 53], [0, 107, -84, 0], [-210, 105, 34, -68], [-36, 72, 168, -84], [0, -77, -45, 0], [113, -113, -138, 138], [328, 0, 0, 66], [-183, -183, 99, 99], [78, -39, 24, -48], [124, 0, 0, -54], [195, 195, 18, 18], [385, -385, 24, -24], [153, 153, 65, 65], [0, 269, 6, 0], [-276, 552, -104, 52], [344, 0, 0, 54], [0, 139, 171, 0], [-162, 81, 163, -326], [54, -108, -300, 150], [0, -401, -126, 0], [138, -69, 26, -52], [283, -283, 93, -93], [-60, 120, -344, 172], [558, -279, 41, -82], [-190, 0, 0, 66], [-345, -345, -83, -83], [35, -35, 243, -243], [-342, 684, 144, -72], [-195, -195, 126, 126], [-168, 336, 40, -20], [-488, 0, 0, 162], [274, 0, 0, -96], [0, 41, -207, 0], [-318, 636, 68, -34], [333, 333, -7, -7], [-6, 3, -60, 120], [-7, 7, -33, 33], [-88, 0, 0, 18], [582, -291, 15, -30], [-78, 156, 100, -50], [-483, -483, 11, 11], [0, 751, -18, 0], [745, -745, -183, 183], [-56, 0, 0, -234], [-21, -21, 54, 54], [0, -175, 9, 0], [-58, 0, 0, 42], [-318, 159, -8, 16], [593, -593, -81, 81], [-36, 72, -168, 84], [375, 375, 28, 28], [42, -21, -93, 186], [395, -395, -252, 252], [0, -137, -174, 0], [-66, 33, -96, 192], [180, -360, 384, -192], [0, 707, -135, 0], [-366, 732, -68, 34], [0, -349, 336, 0], [-180, 360, -96, 48], [0, 643, -81, 0], [-55, 55, 270, -270], [-272, 0, 0, -270], [297, 297, -125, -125], [241, -241, 429, -429], [-866, 0, 0, 72], [-369, -369, -74, -74], [157, -157, -204, 204], [234, -468, 60, -30], [359, -359, -189, 189], [-63, -63, -286, -286], [-6, 12, -116, 58], [-74, 0, 0, 270], [-174, 87, -258, 516], [1579, -1579, -15, 15], [-444, 888, -88, 44], [-980, 0, 0, 42], [-54, 27, 257, -514], [0, 77, -162, 0], [990, -495, 14, -28], [245, -245, 291, -291], [-528, 1056, 184, -92], [317, -317, 282, -282], [510, -1020, 308, -154], [838, 0, 0, 222], [87, 87, 171, 171], [0, 335, 345, 0], [198, -99, -259, 518], [38, 0, 0, 522], [558, -279, -34, 68], [896, 0, 0, -174], [-165, -165, -152, -152], [450, -225, 91, -182], [-342, 684, 60, -30], [-629, 629, -243, 243], [-90, 180, 384, -192], [-15, -15, -364, -364], [30, -15, 189, -378], [650, 0, 0, 90], [0, 275, 396, 0]];
    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_f();".
// 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_f(: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_f();".
// 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_f( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_576_q();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<5,R![27, -9, 1]>,<7,R![2809, 106, 57, -2, 1]>],Snew);
    return Vf;
end function;