
// Make newform 882.4.g.ba 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_ba();".
// 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_ba();".
// 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, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]];
    Rf_basisdens := [1, 1, 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_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, 0], [-1, 0, -1, 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 := 4;
    raw_aps := [[-2, 0, -2, 0], [0, 0, 0, 0], [0, 14, 0, 0], [0, 0, 0, 0], [0, 0, 14, 0], [0, 0, 0, 36], [0, 1, 0, 1], [0, -1, 0, 0], [140, 0, 140, 0], [286, 0, 0, 0], [0, 66, 0, 66], [38, 0, 38, 0], [0, 0, 0, 89], [-34, 0, 0, 0], [0, -370, 0, 0], [0, 0, 74, 0], [0, 307, 0, 307], [0, 10, 0, 0], [0, 0, 684, 0], [-588, 0, 0, 0], [0, 191, 0, 191], [-1220, 0, -1220, 0], [0, 0, 0, -299], [0, -437, 0, 0], [0, 0, 0, 1049], [0, 798, 0, 798], [0, -614, 0, 0], [-1684, 0, -1684, 0], [0, 0, -818, 0], [540, 0, 0, 0], [1720, 0, 0, 0], [0, 1227, 0, 0], [0, 0, -828, 0], [0, 0, 0, -301], [2050, 0, 2050, 0], [0, 0, -472, 0], [0, 1564, 0, 1564], [-3286, 0, -3286, 0], [0, 0, 0, 1054], [0, 1464, 0, 0], [0, 0, -540, 0], [0, 0, 0, 2676], [1028, 0, 1028, 0], [0, 0, 4592, 0], [-794, 0, 0, 0], [0, -1758, 0, -1758], [-2748, 0, 0, 0], [0, 0, 0, -2424], [0, 3741, 0, 3741], [0, -1944, 0, 0], [72, 0, 72, 0], [-4308, 0, 0, 0], [0, 1089, 0, 1089], [0, 0, 0, -659], [0, -663, 0, 0], [0, 0, -7140, 0], [0, 3260, 0, 3260], [0, -1672, 0, 0], [0, 0, 4006, 0], [5984, 0, 0, 0], [0, 3485, 0, 3485], [0, 0, 0, -1394], [0, 0, 0, -3371], [0, -4792, 0, -4792], [0, -4377, 0, 0], [9826, 0, 9826, 0], [5738, 0, 5738, 0], [-2254, 0, 0, 0], [0, 0, 1986, 0], [0, 0, 0, 4788], [0, 4945, 0, 4945], [5944, 0, 5944, 0], [0, 596, 0, 596], [5726, 0, 5726, 0], [10330, 0, 0, 0], [0, -710, 0, 0], [0, 0, -5210, 0], [0, -52, 0, 0], [-498, 0, -498, 0], [0, -2373, 0, -2373], [0, 0, 0, -10285], [10854, 0, 0, 0], [0, 0, 5364, 0], [0, 0, 0, -4587], [0, 9852, 0, 0], [5996, 0, 5996, 0], [-2622, 0, 0, 0], [-11208, 0, -11208, 0], [0, 0, 0, -6920], [3952, 0, 0, 0], [0, -12379, 0, 0], [0, 1618, 0, 1618], [0, 0, 972, 0], [7404, 0, 0, 0], [12244, 0, 12244, 0], [0, 0, 0, 1708], [0, 4036, 0, 0], [0, 1, 0, 1], [0, -8667, 0, 0], [-2050, 0, -2050, 0], [14554, 0, 0, 0], [0, 0, -6954, 0], [0, -1157, 0, -1157], [-7142, 0, -7142, 0], [0, 0, -20606, 0], [0, 6225, 0, 6225], [0, 0, 0, -4599], [0, 16363, 0, 0], [0, 0, 11296, 0], [0, 0, 0, 6171], [0, 13956, 0, 0], [0, 0, 16962, 0], [19034, 0, 0, 0], [0, 13207, 0, 13207], [-14716, 0, 0, 0], [0, 0, -4730, 0], [0, 0, 0, 13475], [0, 6606, 0, 6606], [3774, 0, 3774, 0], [21150, 0, 0, 0], [0, 7338, 0, 7338], [-1164, 0, 0, 0], [0, -19200, 0, 0], [0, 0, 16596, 0], [0, 7989, 0, 0], [2754, 0, 0, 0], [-29434, 0, -29434, 0], [0, 12494, 0, 0], [0, 0, 0, 20114], [0, 15798, 0, 0], [0, 0, 20670, 0], [25400, 0, 0, 0], [-29180, 0, -29180, 0], [-26206, 0, 0, 0], [0, -4853, 0, 0], [0, 0, 0, -6405], [0, -94, 0, -94], [0, 6173, 0, 6173], [0, 0, 0, -5316], [0, 0, -3776, 0], [0, 0, 0, -25617], [-16410, 0, -16410, 0], [0, 0, 22072, 0], [11628, 0, 0, 0], [0, 21854, 0, 21854], [0, 0, 0, -12646], [0, 0, 0, -14676], [0, 31763, 0, 31763], [0, 49, 0, 0], [-5452, 0, -5452, 0], [-31106, 0, -31106, 0], [0, 0, 0, -4203], [34796, 0, 0, 0], [0, -7046, 0, 0], [0, 0, -29756, 0], [-21440, 0, 0, 0], [8288, 0, 8288, 0], [0, 32231, 0, 0], [0, 0, 0, 8249], [0, 10, 0, 10], [14034, 0, 14034, 0], [42698, 0, 0, 0], [-48492, 0, 0, 0], [0, -37243, 0, 0], [0, 0, 55380, 0], [0, -35734, 0, -35734], [0, 0, -39712, 0], [0, -1546, 0, -1546]];
    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_ba();".
// 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_ba(:prec:=4)
    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_ba();".
// 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_ba( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_882_g();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![153664, 0, 392, 0, 1]>,<11,R![196, 14, 1]>,<13,R![-2592, 0, 1]>],Snew);
    return Vf;
end function;