
// Make newform 8.17.d.a in Magma, downloaded from the LMFDB on 25 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_17_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_17_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 := 17;
    raw_aps := [[256], [-11966], [0], [0], [309273794], [0], [12433289474], [-28741860286], [0], [0], [0], [0], [831999729794], [6069438110402], [0], [0], [290918580565442], [0], [-617692243063486], [0], [-486139502245246], [0], [-3591943143595966], [1250855726873474], [-9681283613729278], [0], [0], [-151522179893566], [0], [-47955702202130686], [0], [-57922522031728318], [-132927810809432446], [257420702437493954], [0], [0], [0], [309996698812022594], [0], [0], [1526555201788625474], [0], [0], [-1787416808645557246], [0], [0], [-1492965495602685886], [0], [940483512505469762], [0], [-17152259824919272318], [0], [-22422741416557494526], [-31370429995726022206], [33161149320962764802], [0], [0], [0], [0], [-72851531473154824318], [32625406184209862594], [0], [-113124119422864471486], [0], [-23528286364811525758], [0], [284058411906570977474], [332619098965456219394], [-357622232980544697406], [0], [-294177780393127610878], [0], [0], [0], [-792721828432152456766], [0], [0], [0], [-991475670359460542206], [-1527223950664935762046], [1025475167814598385474], [0], [0], [2238998152567465642754], [0], [1409035034704986820034], [-1786410661826430839806], [-1945348224455450932606], [0], [0], [4333853019225591535682], [0], [0], [3145478387081772584642], [2904026282410683547202], [0], [0], [-5565707356092760142206], [-11193496099537787673406], [0], [-15632899592728591927486], [0], [-925677052475093108158], [-1156081085471403527806], [4490237375163403216322], [24569411640345713083394], [-11379180521513782947646], [29803226396298051624194], [0], [27542869716402478177154], [0], [0], [-24470835125398059189118], [-34882281770438378752318], [0], [-56906684073687244431358], [-52573938215015414097598], [0], [0], [60575325762508790033474], [0], [-84106628226390310141438], [0], [-67570387361219146206526], [-24010281880966397926846], [0], [0], [0], [0], [0], [-32932874687747900495038], [0], [0], [0], [-221985667391793877189246], [237405204851976779261954], [0], [92403293302324491723842], [0], [-43907166290186698354558], [238559499088335047367362], [0], [0], [345608810685723062826434], [0], [0], [0], [580685573890544183751554], [-405646112404668678739006], [0], [0], [156053968311174170791682], [634503029580708662670914], [0], [-815465212319868556526398], [0], [0], [148352995270860723294722], [-1188347241965450479210366], [0], [-372913463450388018118078], [1125694461986080086264194], [0], [-770608675065215286589246], [54402826517116020141314], [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_17_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_17_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, 17));
    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_17_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_17_d_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8_d();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,17,sign)));
    Vf := Kernel([<3,R![11966, 1]>],Snew);
    return Vf;
end function;