
// Make newform 882.4.g.f in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_882_g();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_882_g_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_882_4_g_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_882_4_g_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 := [1, -1, 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_882_g();"
function MakeCharacter_882_g()
    N := 882;
    order := 3;
    char_gens := [785, 199];
    v := [3, 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_882_g_Hecke();"
function MakeCharacter_882_g_Hecke(Kf)
    N := 882;
    order := 3;
    char_gens := [785, 199];
    char_values := [[1, 0], [0, -1]];
    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, -2], [0, 0], [0, -6], [0, 0], [12, -12], [-38, 0], [126, -126], [0, 20], [0, 168], [-30, 0], [-88, 88], [0, -254], [42, 0], [-52, 0], [0, 96], [198, -198], [660, -660], [0, -538], [-884, 884], [-792, 0], [218, -218], [0, 520], [-492, 0], [0, -810], [-1154, 0], [618, -618], [0, 128], [0, -1476], [-1190, 1190], [462, 0], [-2536, 0], [0, -2292], [-726, 726], [-380, 0], [0, 1590], [-2432, 2432], [614, -614], [0, 1852], [-2136, 0], [0, -1758], [-540, 540], [-1982, 0], [0, -2688], [2302, -2302], [-4374, 0], [-1600, 1600], [3332, 0], [-2648, 0], [-2244, 2244], [0, -5650], [0, 4698], [1200, 0], [-718, 718], [6012, 0], [0, 2046], [-6072, 6072], [6930, -6930], [0, 1352], [1186, -1186], [-2442, 0], [2828, -2828], [4758, 0], [8476, 0], [-4632, 4632], [0, -4822], [0, -3426], [0, 2788], [434, 0], [6684, -6684], [-2630, 0], [7422, -7422], [0, -10440], [10424, -10424], [0, -3278], [6140, 0], [0, 3072], [6150, -6150], [0, -106], [0, -1758], [-3670, 3670], [-9660, 0], [8462, 0], [9792, -9792], [7342, 0], [0, 10640], [0, -17412], [1710, 0], [0, 646], [-6018, 0], [-6712, 0], [0, -5364], [-9840, 9840], [-1424, 1424], [4548, 0], [0, -6500], [12168, 0], [0, 21090], [5238, -5238], [0, 8588], [0, -3062], [-8476, 0], [-12546, 12546], [12, -12], [0, 19290], [12148, -12148], [-10366, 10366], [7644, 0], [0, -8658], [25800, -25800], [-16202, 0], [0, -24136], [4642, -4642], [6726, 0], [-21220, 21220], [29792, 0], [-10158, 10158], [-29828, 0], [-1944, 1944], [0, 26718], [-4260, 0], [22862, -22862], [-32542, 0], [0, -14214], [-7092, 7092], [0, -13228], [-28062, 0], [0, 27250], [0, 14400], [-17984, 0], [0, 16598], [-1460, 1460], [30072, 0], [0, 18088], [24734, 0], [0, 22278], [-16130, 0], [-29718, 29718], [9524, -9524], [-33906, 0], [-630, 630], [20788, 0], [0, -43098], [14272, -14272], [-13644, 0], [-2410, 2410], [23160, 0], [-32078, 0], [14406, -14406], [0, 30620], [0, 17568], [0, 21706], [-14958, 0], [-32812, 0], [0, 38856], [28276, -28276], [-8112, 0], [0, 26080], [0, -49170], [-48314, 0], [-34782, 34782], [0, -25116], [15462, 0], [-736, 0], [0, 29268], [16674, -16674], [31272, -31272], [15928, -15928], [42014, -42014]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_882_g_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_882_4_g_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_882_4_g_f(:prec:=2)
    chi := MakeCharacter_882_g();
    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_882_4_g_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_882_4_g_f( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_882_g();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![36, 6, 1]>,<11,R![144, -12, 1]>,<13,R![38, 1]>],Snew);
    return Vf;
end function;