
// Make newform 882.4.g.s 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_s();".
// 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_s();".
// 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, 2], [0, 0], [-8, 8], [-42, 0], [-2, 2], [0, 124], [0, 76], [-254, 0], [72, -72], [0, -398], [-462, 0], [212, 0], [0, -264], [-162, 162], [-772, 772], [0, -30], [764, -764], [236, 0], [-418, 418], [0, -552], [-1036, 0], [0, 30], [-1190, 0], [1370, -1370], [0, -464], [0, -2136], [1226, -1226], [-338, 0], [2088, 0], [0, -292], [818, -818], [-2156, 0], [0, -2850], [-1672, 1672], [-446, 446], [0, -2708], [-896, 0], [0, 4034], [-3480, 3480], [-2898, 0], [0, 2652], [-146, 146], [2546, 0], [2536, -2536], [-1300, 0], [2576, 0], [-1836, 1836], [0, 1874], [0, 3730], [-2004, 0], [646, -646], [-1260, 0], [0, 5910], [2988, -2988], [-1318, 1318], [0, 5640], [-6446, 6446], [-4930, 0], [6260, -6260], [2310, 0], [196, 0], [-6736, 6736], [0, -394], [0, -6714], [0, -692], [-1566, 0], [-5328, 5328], [11326, 0], [-2130, 2130], [0, 3044], [-12416, 12416], [0, 7442], [100, 0], [0, 8080], [-5482, 5482], [0, -10446], [0, -11334], [-8594, 8594], [-10500, 0], [-12066, 0], [4332, -4332], [-1918, 0], [0, 7992], [0, 3184], [-11426, 0], [0, 16934], [17038, 0], [-13592, 0], [0, 8612], [7432, -7432], [6616, -6616], [-17040, 0], [0, 2948], [-17304, 0], [0, 4650], [16854, -16854], [0, 124], [0, -5382], [17460, 0], [-9514, 9514], [3988, -3988], [0, 11346], [8436, -8436], [-2098, 2098], [-9436, 0], [0, -1314], [-8940, 8940], [16058, 0], [0, -3936], [-174, 174], [-16018, 0], [3068, -3068], [24656, 0], [7594, -7594], [-3724, 0], [3792, -3792], [0, 24702], [20144, 0], [2522, -2522], [-10414, 0], [0, -22230], [18192, -18192], [0, -8108], [5794, 0], [0, 1954], [0, -32016], [-23072, 0], [0, -31782], [24396, -24396], [32604, 0], [0, 7680], [366, 0], [0, 29374], [-38990, 0], [-20470, 20470], [-29916, 29916], [-4914, 0], [34250, -34250], [-41804, 0], [0, 30862], [-10576, 10576], [10680, 0], [1178, -1178], [5600, 0], [-826, 0], [45918, -45918], [0, -42380], [0, -26524], [0, -20614], [23730, 0], [-9028, 0], [0, 37200], [-23988, 23988], [-15276, 0], [0, 10760], [0, -52890], [-6118, 0], [-32230, 32230], [0, -18544], [-25930, 0], [8192, 0], [0, -54444], [-25446, 25446], [33192, -33192], [-11024, 11024], [40714, -40714]];
    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_s();".
// 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_s(: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_s();".
// 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_s( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_882_g();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![4, -2, 1]>,<11,R![64, 8, 1]>,<13,R![42, 1]>],Snew);
    return Vf;
end function;