
// Make newform 5.8.b.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_5_b();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_5_b_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_5_8_b_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_5_8_b_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 := [29, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [0, 2]];
    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_5_b();"
function MakeCharacter_5_b()
    N := 5;
    order := 2;
    char_gens := [2];
    v := [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_5_b_Hecke();"
function MakeCharacter_5_b_Hecke(Kf)
    N := 5;
    order := 2;
    char_gens := [2];
    char_values := [[-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 := 8;
    raw_aps := [[0, 1], [0, 3], [75, -25], [0, -39], [-6828, 0], [0, -942], [0, 1456], [6860, 0], [0, 2713], [25590, 0], [82112, 0], [0, -20754], [-533118, 0], [0, 65823], [0, 541], [0, -54722], [1438980, 0], [1381022, 0], [0, -252069], [-481608, 0], [0, 137988], [-1059760, 0], [0, -241757], [5644170, 0], [0, 1115016], [5142702, 0], [0, 323553], [0, -1380089], [-20112370, 0], [0, -522392], [0, 2649501], [-33313188, 0], [0, -3974804], [11380820, 0], [40006950, 0], [-28659448, 0], [0, -2803614], [0, -8685117], [0, 5324881], [0, -4523462], [19350540, 0], [78261662, 0], [-119453568, 0], [0, 5556648], [0, 11416966], [169052600, 0], [-267605428, 0], [0, -13865187], [0, -21743549], [-131881810, 0], [0, 17030068], [-105116880, 0], [194216282, 0], [202689252, 0], [0, -1249664], [0, 13203433], [-507547890, 0], [112835792, 0], [0, 47419926], [-170458638, 0], [0, -15054657], [0, -35824802], [0, 59219811], [-727816728, 0], [0, 41490708], [0, -54880694], [584868212, 0], [0, 68649096], [0, 34434391], [1132738550, 0], [0, 77328928], [675317880, 0], [0, -167346219], [0, 86341638], [-1435448860, 0], [0, -146427107], [2244254070, 0], [0, -21043134], [-911721198, 0], [-255215470, 0], [296316060, 0], [1066760342, 0], [95316912, 0], [0, 176385168], [-1112256280, 0], [0, 299201023], [-72948150, 0], [0, 22774236], [-3256541178, 0], [0, 50923533], [0, -122457269], [1599889440, 0], [0, -181933479], [238784532, 0], [-3066416500, 0], [0, 148879753], [-1217416170, 0], [-2086349958, 0], [0, -398244537], [4911163382, 0], [0, 163830291], [0, -38383814], [0, -702924917], [-890286990, 0], [-4960894108, 0], [0, 142117776], [0, -408124529], [0, 309173848], [4530301800, 0], [-4704790798, 0], [0, 208376661], [0, 811874358], [0, -416914844], [3741008660, 0], [1935446312, 0], [5890763082, 0], [0, 293406123], [0, 1184337241], [0, -412143422], [-10852643220, 0], [8493065222, 0], [0, -390134712], [0, -609781274], [0, 586318543], [3764469932, 0], [1970827902, 0], [-9628531570, 0], [18948954960, 0], [0, -134114799], [0, -1290019182], [-11900849380, 0], [0, -1462466327], [-16064459248, 0], [0, 1754536086], [10110026922, 0], [20067748610, 0], [0, 2003370838], [0, 2154574371], [0, -62869334], [5845040730, 0], [19149142772, 0], [6170063142, 0], [0, -2091718887], [0, -565019849], [-4815879010, 0], [29263456920, 0], [0, -2016230322], [0, -373021964], [-16248686620, 0], [0, 1819425733], [0, -3415312374], [1483781562, 0], [0, 2196743643], [0, 391138321], [0, 1231739511], [7157267472, 0], [9781515560, 0], [-29207266710, 0], [0, -3454428204], [6200158182, 0], [0, 1184211091], [0, -2865631772], [0, 3087548481], [44509456692, 0], [0, -632047424], [0, 59190193], [56014656032, 0], [0, 833254566]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5_b_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_5_8_b_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_5_8_b_a(:prec:=2)
    chi := MakeCharacter_5_b();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 8));
    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_5_8_b_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_5_8_b_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_5_b();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,8,sign)));
    Vf := Kernel([],Snew);
    return Vf;
end function;