
// Make newform 192.9.e.h in Magma, downloaded from the LMFDB on 28 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_192_e();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_192_e_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_192_9_e_h();".
// 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_192_9_e_h();".
// 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 := [2, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [0, 12]];
    Rf_basisdens := [1, 1];
    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_192_e();"
function MakeCharacter_192_e()
    N := 192;
    order := 2;
    char_gens := [127, 133, 65];
    v := [2, 2, 1];
    // 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_192_e_Hecke();"
function MakeCharacter_192_e_Hecke(Kf)
    N := 192;
    order := 2;
    char_gens := [127, 133, 65];
    char_values := [[1, 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 := 9;
    raw_aps := [[0, 0], [63, 3], [0, 34], [2786, 0], [0, 1322], [13150, 0], [0, 3912], [-144002, 0], [0, -2908], [0, -36970], [728738, 0], [1964446, 0], [0, -58108], [78142, 0], [0, 207400], [0, 30762], [0, -294878], [-17578274, 0], [17136766, 0], [0, -1525620], [28139330, 0], [9182498, 0], [0, -5132914], [0, 4787868], [-128722558, 0], [0, -5526094], [-53032222, 0], [0, 11243034], [-271958690, 0], [0, -4532488], [-125416798, 0], [0, -7309754], [0, 32453044], [75926206, 0], [0, 43318714], [301636706, 0], [161241310, 0], [221232382, 0], [0, 23679452], [0, -637714], [0, 13247742], [1310259742, 0], [0, -129037664], [-1711828990, 0], [0, -155143614], [2952428258, 0], [2663491582, 0], [-204265822, 0], [0, -25111882], [305784478, 0], [0, -83035164], [0, 134797256], [4373703554, 0], [0, 105473922], [0, 130038656], [0, 104771884], [0, -351948546], [3245552930, 0], [1341374494, 0], [0, -111518468], [6882917950, 0], [0, 254187730], [8321552638, 0], [0, -643371628], [6199533122, 0], [0, -1101141562], [-3244779842, 0], [4891368194, 0], [0, 850076146], [5341687006, 0], [0, 461013424], [0, 982393212], [18645637154, 0], [-33839040482, 0], [28208827582, 0], [0, -1074830272], [0, 1145364578], [-16671938978, 0], [0, 1949472008], [-50332625470, 0], [0, -1734060650], [33324282526, 0], [0, -3313211544], [-21880696318, 0], [18798952802, 0], [0, 3851331938], [0, 931521120], [3303997250, 0], [0, 1945364510], [56311745570, 0], [0, -3171225522], [0, -4729905392], [-70709327134, 0], [0, -3576049958], [104926156030, 0], [0, 278872116], [0, -4916762], [0, 1945597044], [7604914750, 0], [118115626462, 0], [66045412606, 0], [0, 8181807150], [0, 1943568190], [0, 4219178380], [-42025000130, 0], [219502477442, 0], [0, 12097374026], [0, 5975358696], [0, -5465597308], [-118262847550, 0], [-77872533598, 0], [60400909726, 0], [0, -10961934940], [-154129926722, 0], [43513955426, 0], [0, 10296430528], [246091261438, 0], [0, 1118050668], [0, 9037191742], [0, -8675831890], [66115893790, 0], [-167142323326, 0], [0, 4610544914], [0, -7172638662], [-371272045442, 0], [0, -13901353722], [151154035870, 0], [0, -929367336], [361913268578, 0], [-57565934882, 0], [230778352510, 0], [0, 2854531004], [103467264290, 0], [-309394526690, 0], [0, 21079777772], [69115564034, 0], [0, 29240355234], [-130010317442, 0], [0, -24055902698], [0, 13156230276], [-175526149826, 0], [0, 4934668202], [-541971790750, 0], [0, -31263765342], [-465551039522, 0], [0, 46745865484], [-45505574114, 0], [0, -49997553764], [764741055934, 0], [0, 26833183056], [-842742220706, 0], [0, -66181627272], [-625329648002, 0], [0, -27885014668], [914524275262, 0], [0, -50501590664], [94749047138, 0], [0, 12162605392], [-299452249150, 0], [0, 57491191918], [0, -40397574274], [0, 10717369932], [-151798534174, 0], [0, -13604528502], [0, -65302816984], [0, 21480398020], [12728131490, 0], [1564387898782, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_192_e_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_192_9_e_h();".
// 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_192_9_e_h(:prec:=2)
    chi := MakeCharacter_192_e();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 9));
    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_192_9_e_h();".
// 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_192_9_e_h( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_192_e();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,9,sign)));
    Vf := Kernel([<5,R![332928, 0, 1]>,<7,R![-2786, 1]>],Snew);
    return Vf;
end function;