
// Make newform 8.19.d.a in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_8_d();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_8_d_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_8_19_d_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_8_19_d_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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_8_d();"
function MakeCharacter_8_d()
    N := 8;
    order := 2;
    char_gens := [7, 5];
    v := [1, 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_8_d_Hecke();"
function MakeCharacter_8_d_Hecke(Kf)
    N := 8;
    order := 2;
    char_gens := [7, 5];
    char_values := [[-1], [-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 := 19;
    raw_aps := [[-512], [-3266], [0], [0], [-354349618], [0], [119842447106], [335013705758], [0], [0], [0], [0], [522162604887122], [1000250360894414], [0], [0], [-10228968070290322], [0], [-50467064407716994], [0], [60615173082969074], [0], [-352960460737558306], [487058048464217618], [1501067319528053666], [0], [0], [-2027595730333340914], [0], [-4687736788067391646], [0], [19015038671968130942], [1590415661915654546], [36485514939848383118], [0], [0], [0], [-25798079025414892546], [0], [0], [-284970075639655393762], [0], [0], [549021653950564655714], [0], [0], [1543351271350920402782], [0], [-387176792999453756674], [0], [-3956342467403478982606], [0], [-3035813339689188655678], [7583751130550519832302], [9570812261477330381186], [0], [0], [0], [0], [15580790784100370129042], [-22490175871735640879506], [0], [-14784430757808229188514], [0], [-24822678784103249532046], [0], [18046839394746925844942], [-81369537602843537247454], [142036928959048362449966], [0], [101143281235012771223234], [0], [0], [0], [-221819417096625569647762], [0], [0], [0], [486318308626997030016002], [384499032126530474992178], [265749378879012603338558], [0], [0], [309749500658913797212994], [0], [463503099386412419028014], [-1103651348435827351468702], [-1211413286385776707234414], [0], [0], [-680066970160654706996194], [0], [0], [3005540560512830699567822], [-3278698084876937536536802], [0], [0], [4065217241307943534050962], [-2125051259508759508789426], [0], [-8680273092517751810532034], [0], [-11018342181799161427715746], [-10789362264558290942793742], [8859903662319807525193262], [221137103753675672730626], [10195547515049401805274446], [-15638451014725980987643486], [0], [20396528108891529011427602], [0], [0], [-16192735543693711154866894], [23771360715006450924380558], [0], [-11751782030370776708866078], [-37326253134603554684201986], [0], [0], [-46248802834926843726473122], [0], [53220994279667743316765474], [0], [29913703509835514346665294], [-68031419071234260983281378], [0], [0], [0], [0], [0], [-120793135378307737934151682], [0], [0], [0], [-143584528873732636259995918], [-92153576880803519283891742], [0], [-135054538972569268230021154], [0], [96558280217812420940740178], [-253712175048704835455308018], [0], [0], [361565342269661472469943726], [0], [0], [0], [325681057817971940738190386], [-440249335511123564482611922], [0], [0], [-473095233481658338307878558], [-536480627414565274122195106], [0], [822751814235425038590204686], [0], [0], [-761266461960570571397712862], [430662579952115873061914546], [0], [1183917583957238542822723166], [-806435993150582650343236366], [0], [-1534429851709238801695143538], [257599693279612382303101826], [0], [0], [0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8_d_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_8_19_d_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_8_19_d_a(:prec:=1)
    chi := MakeCharacter_8_d();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 19));
    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_8_19_d_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_8_19_d_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8_d();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,19,sign)));
    Vf := Kernel([<3,R![3266, 1]>],Snew);
    return Vf;
end function;