
// Make newform 882.4.g.a 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_a();".
// 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_a();".
// 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, -22], [0, 0], [26, -26], [54, 0], [-74, 74], [0, 116], [0, -58], [-208, 0], [-252, 252], [0, -50], [126, 0], [164, 0], [0, 444], [12, -12], [-124, 124], [0, -162], [860, -860], [238, 0], [-146, 146], [0, 984], [656, 0], [0, 954], [-526, 0], [-1306, 1306], [0, -508], [0, 498], [614, -614], [1232, 0], [-2808, 0], [0, -520], [-2516, 2516], [2672, 0], [0, -1164], [-1672, 1672], [446, -446], [0, -428], [4, 0], [0, 590], [3534, -3534], [-1098, 0], [0, -4854], [1498, -1498], [-620, 0], [32, -32], [4268, 0], [-3464, 0], [3252, -3252], [0, 418], [0, 2084], [-1662, 0], [6182, -6182], [-996, 0], [0, 5994], [6414, -6414], [-2686, 2686], [0, 5100], [4426, -4426], [-7508, 0], [3412, -3412], [-4734, 0], [-5836, 0], [-5620, 5620], [0, 6082], [0, -7308], [0, 8020], [4590, 0], [-6546, 6546], [7994, 0], [-4650, 4650], [0, 346], [-6784, 6784], [0, 6098], [-2660, 0], [0, 760], [-104, 104], [0, 4398], [0, 13236], [-9490, 9490], [4236, 0], [918, 0], [-11814, 11814], [8374, 0], [0, 3840], [0, 10166], [-8200, 0], [0, 6074], [-2006, 0], [-3728, 0], [0, 6380], [-17180, 17180], [2728, -2728], [-2574, 0], [0, 7484], [7920, 0], [0, -7254], [17862, -17862], [0, -592], [0, 6402], [-8988, 0], [3244, -3244], [-9812, 9812], [0, -12156], [-6876, 6876], [20002, -20002], [18404, 0], [0, 9846], [9234, -9234], [1510, 0], [0, 17544], [-9246, 9246], [29212, 0], [7096, -7096], [488, 0], [8756, -8756], [3364, 0], [-21804, 21804], [0, 13488], [-28946, 0], [-20642, 20642], [-17602, 0], [0, -4266], [-26874, 26874], [0, -17128], [-11968, 0], [0, 5278], [0, -6720], [-16804, 0], [0, 27522], [-21132, 21132], [30, 0], [0, 15480], [28770, 0], [0, 12418], [-12346, 0], [-38098, 38098], [13824, -13824], [22170, 0], [16288, -16288], [8720, 0], [0, -31372], [17648, -17648], [2382, 0], [26650, -26650], [24092, 0], [8194, 0], [16962, -16962], [0, -48556], [0, -34274], [0, -7126], [-9222, 0], [37652, 0], [0, 21996], [-14844, 14844], [19446, 0], [0, 39200], [0, 15954], [-2546, 0], [-430, 430], [0, 38266], [28216, 0], [-27712, 0], [0, -32976], [-8940, 8940], [3288, -3288], [-33944, 33944], [-54562, 54562]];
    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_a();".
// 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_a(: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_a();".
// 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_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_882_g();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![484, 22, 1]>,<11,R![676, -26, 1]>,<13,R![-54, 1]>],Snew);
    return Vf;
end function;