
// Make newform 4.9.b.b in Magma, downloaded from the LMFDB on 20 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4_b();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_4_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_4_9_b_b();".
// 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_4_9_b_b();".
// 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 := [10, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [-2, 4]];
    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_4_b();"
function MakeCharacter_4_b()
    N := 4;
    order := 2;
    char_gens := [3];
    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_4_b_Hecke();"
function MakeCharacter_4_b_Hecke(Kf)
    N := 4;
    order := 2;
    char_gens := [3];
    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 := 9;
    raw_aps := [[-10, -1], [0, -8], [610, 0], [0, -112], [0, 1480], [-5470, 0], [73090, 0], [0, 1560], [0, 18992], [-128222, 0], [0, 5440], [-3472030, 0], [2146882, 0], [0, 474632], [0, -610592], [824290, 0], [0, 298280], [-14746078, 0], [0, -1221512], [0, 95760], [-5725630, 0], [0, -2875360], [0, 4160152], [-83324222, 0], [120619010, 0], [27724642, 0], [0, -8366768], [0, -8032632], [-59071582, 0], [55084930, 0], [0, -20589312], [0, 24994040], [221979970, 0], [0, 23621320], [403602658, 0], [0, 67012400], [-271318750, 0], [0, -46317768], [0, -37479472], [-206196830, 0], [0, -11361960], [482565602, 0], [0, -79460480], [1175931650, 0], [1705383010, 0], [0, -199388400], [0, -117513320], [0, 118430912], [0, 60052408], [-2847839902, 0], [220620610, 0], [0, 323853920], [6179832962, 0], [0, -417427800], [-6136932350, 0], [0, -557979248], [2707204258, 0], [0, 639737760], [-8229648670, 0], [3081049282, 0], [0, -93764888], [4809796450, 0], [0, 279564888], [0, 1039284080], [-6311652670, 0], [16590176290, 0], [0, -439263160], [-356226430, 0], [0, 1278868328], [10363373858, 0], [-13097926910, 0], [0, -265375920], [0, -1574280352], [-21006323230, 0], [0, -244047960], [0, 1788284672], [31367951458, 0], [7657883810, 0], [-32611999358, 0], [22616819138, 0], [0, 3959195960], [-33407698078, 0], [0, 2457615840], [2884334210, 0], [0, -5543816080], [0, -1654192088], [21109220098, 0], [-20683100350, 0], [76507164322, 0], [0, -273260768], [0, -1544462952], [0, 1952655040], [0, 7452372048], [0, 170416520], [0, -833474600], [0, -3146669328], [-32511277022, 0], [1845504322, 0], [0, -4994156088], [-74591735518, 0], [0, -113315272], [-137542752350, 0], [0, 8520008792], [40242850498, 0], [0, -12036929240], [4964771330, 0], [0, -12285054392], [206035883650, 0], [0, 18465537200], [101422023362, 0], [0, 15843310528], [127158143330, 0], [-50670162110, 0], [0, 5658509960], [0, -13232390160], [112012570882, 0], [0, 21290046712], [0, -21777125232], [303789411490, 0], [0, -3351282920], [-246529193758, 0], [-315335788030, 0], [-24723635870, 0], [0, -5771361528], [0, 23652812120], [-287924946398, 0], [251685066338, 0], [0, 11117340960], [0, 14338144688], [217617899810, 0], [0, -38827051080], [0, -16293738928], [0, -18790493600], [-384882027550, 0], [239208624322, 0], [208457047298, 0], [-55446857630, 0], [0, -32498686952], [-309015085790, 0], [477958031938, 0], [0, 51041078280], [-843823597598, 0], [0, 20889987952], [0, -22311822552], [475155986978, 0], [0, 26856674320], [-97540775710, 0], [-79476876350, 0], [0, 33276075880], [0, 38431684032], [274154624930, 0], [801837841282, 0], [0, 79664411352], [0, -43718063632], [0, -52443405112], [0, 9561630880], [0, -4275333840], [-630990728702, 0], [-841436029630, 0], [-452934582878, 0], [0, 80311422808], [-1390399803710, 0], [0, 72205254928], [0, -102810784440], [1201196563330, 0], [0, -30215236688], [0, 26866518720], [-1031627512990, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4_b_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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