
// Make newform 16.9.c.a in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_16_c();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_16_c_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_16_9_c_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_16_9_c_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 := [9, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [-24, 48]];
    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_16_c();"
function MakeCharacter_16_c()
    N := 16;
    order := 2;
    char_gens := [15, 5];
    v := [1, 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_16_c_Hecke();"
function MakeCharacter_16_c_Hecke(Kf)
    N := 16;
    order := 2;
    char_gens := [15, 5];
    char_values := [[-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], [0, -1], [-510, 0], [0, 18], [0, -135], [-27710, 0], [50370, 0], [0, -765], [0, -1242], [54978, 0], [0, -8280], [793730, 0], [-75582, 0], [0, -3519], [0, -20196], [11166210, 0], [0, 153765], [-23826622, 0], [0, -52785], [0, 71010], [6516610, 0], [0, 343620], [0, -517293], [86795778, 0], [-46670270, 0], [65991042, 0], [0, -1161270], [0, -896751], [-156118718, 0], [236345730, 0], [0, 2589984], [0, -1523745], [-386441790, 0], [0, -2472615], [-454098558, 0], [0, 4649670], [435317570, 0], [0, -1718937], [0, -4728294], [-1767636030, 0], [0, -5331285], [627094402, 0], [0, -7601040], [-29675710, 0], [-1124840190, 0], [0, -7375230], [0, 19556595], [0, -16045416], [0, 10484343], [1694468098, 0], [521423490, 0], [0, 31037580], [-1621478462, 0], [0, 924885], [5277887490, 0], [0, -52029486], [346450242, 0], [0, -45900], [2150605570, 0], [10425577218, 0], [0, -9017955], [-2137859070, 0], [0, -38393973], [0, -75746610], [-299804350, 0], [5311718850, 0], [0, 71136585], [-18435866750, 0], [0, -90024723], [-639380542, 0], [25983720450, 0], [0, 156734730], [0, 131160492], [11968021250, 0], [0, -162542475], [0, 101009376], [-27345733758, 0], [39945642050, 0], [-21276733758, 0], [11428343362, 0], [0, -77479065], [22876587778, 0], [0, 6726780], [38222543810, 0], [0, -450951570], [0, -526572603], [25198676802, 0], [4668275650, 0], [-38809599678, 0], [0, 227791908], [0, 163327347], [0, 34055640], [0, -214020054], [0, 391928625], [0, 555307515], [0, -310659138], [10592742978, 0], [124957712322, 0], [0, -197489799], [144658748482, 0], [0, -73087353], [-5473124670, 0], [0, 307155915], [12782228802, 0], [0, 534880485], [21382702850, 0], [0, -427716855], [115977789570, 0], [0, -1694862090], [-192942334462, 0], [0, -1141253640], [176424461570, 0], [-98498609790, 0], [0, 90569745], [0, 1221691230], [-113903393982, 0], [0, 495870687], [0, -1402782030], [6499716930, 0], [0, -1556365725], [-269548777342, 0], [94447000130, 0], [80273477250, 0], [0, 2118398049], [0, 2156609835], [-273602688702, 0], [-176661556862, 0], [0, -1588257180], [0, 2021892246], [-294175865150, 0], [0, -683087625], [0, -715330710], [0, 2943241740], [182005524610, 0], [-427418636478, 0], [-509969383102, 0], [-149408131710, 0], [0, -5164257789], [302703469890, 0], [-584315872062, 0], [0, 855508545], [452469949698, 0], [0, -2160100530], [0, -2065531995], [335531923522, 0], [0, 2414631330], [-508662304510, 0], [606764266050, 0], [0, 6686798445], [0, 211173048], [-880194758590, 0], [1040846194818, 0], [0, 5355786267], [0, -2353029858], [0, -5389355367], [0, 2701263060], [0, -4808754810], [294972520002, 0], [1039413437570, 0], [126891469122, 0], [0, 4317782571], [658226513730, 0], [0, 3813653826], [0, 2500115625], [602237917890, 0], [0, -9681570090], [0, -7119069480], [-328555796350, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_16_c_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_16_9_c_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_16_9_c_a(:prec:=2)
    chi := MakeCharacter_16_c();
    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_16_9_c_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_16_9_c_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_16_c();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,9,sign)));
    Vf := Kernel([<3,R![20160, 0, 1]>],Snew);
    return Vf;
end function;