
// Make newform 882.4.g.g in Magma, downloaded from the LMFDB on 19 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_g();".
// 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_g();".
// 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], [-30, 30], [-53, 0], [84, -84], [0, -97], [0, 84], [180, 0], [179, -179], [0, 145], [126, 0], [-325, 0], [0, 366], [-768, 768], [264, -264], [0, 818], [523, -523], [342, 0], [-43, 43], [0, 1171], [-810, 0], [0, 600], [-386, 0], [-618, 618], [0, 1475], [0, 1884], [-413, 413], [882, 0], [2483, 0], [0, -2118], [3012, -3012], [37, 0], [0, -1644], [-1088, 1088], [506, -506], [0, -1844], [162, 0], [0, 2724], [-1254, 1254], [1807, 0], [0, 714], [3709, -3709], [1044, 0], [-136, 136], [1484, 0], [2032, 0], [-6198, 6198], [0, -4591], [0, 4530], [-1530, 0], [5534, -5534], [-468, 0], [0, 2490], [1572, -1572], [-1806, 1806], [0, -6112], [4231, -4231], [3816, 0], [-3997, 3997], [4608, 0], [631, 0], [-3894, 3894], [0, -2185], [0, 3504], [0, -2945], [4277, 0], [7188, -7188], [9406, 0], [-3390, 3390], [0, -4812], [-7099, 7099], [0, -2963], [-11899, 0], [0, -2568], [-10146, 10146], [0, -6229], [0, -2472], [-7075, 7075], [-4158, 0], [-6595, 0], [1518, -1518], [-8567, 0], [0, 10640], [0, 7032], [14814, 0], [0, 11251], [-3852, 0], [-475, 0], [0, -5934], [13368, -13368], [-6653, 6653], [-15444, 0], [0, -683], [9882, 0], [0, -4206], [-9060, 9060], [0, -15679], [0, 7711], [4292, 0], [-9858, 9858], [13890, -13890], [0, 19038], [8053, -8053], [-17137, 17137], [18144, 0], [0, 24702], [-2172, 2172], [-4175, 0], [0, 2261], [16318, -16318], [26550, 0], [19925, -19925], [-6832, 0], [10212, -10212], [-3779, 0], [-16998, 16998], [0, -21750], [10944, 0], [10955, -10955], [25103, 0], [0, 5604], [10968, -10968], [0, 8405], [-468, 0], [0, 25066], [0, -11082], [-13481, 0], [0, 24317], [18217, -18217], [-19782, 0], [0, 4921], [18098, 0], [0, 24468], [-21719, 0], [30306, -30306], [27296, -27296], [-35100, 0], [44394, -44394], [8584, 0], [0, -9834], [-43856, 43856], [-13266, 0], [17453, -17453], [-35172, 0], [-3503, 0], [-22848, 22848], [0, -13456], [0, 40710], [0, -2906], [-19188, 0], [-17251, 0], [0, 2094], [40267, -40267], [-17604, 0], [0, -3509], [0, 34638], [17353, 0], [46920, -46920], [0, 18354], [-35568, 0], [-27343, 0], [0, -51024], [-2226, 2226], [-35304, 35304], [2341, -2341], [29015, -29015]];
    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_g();".
// 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_g(: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_g();".
// 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_g( : 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![900, 30, 1]>,<13,R![53, 1]>],Snew);
    return Vf;
end function;