
// Make newform 128.4.b.d in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_128_b();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_128_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_128_4_b_d();".
// 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_128_4_b_d();".
// 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 := [1, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [0, 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_128_b();"
function MakeCharacter_128_b()
    N := 128;
    order := 2;
    char_gens := [127, 5];
    v := [2, 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_128_b_Hecke();"
function MakeCharacter_128_b_Hecke(Kf)
    N := 128;
    order := 2;
    char_gens := [127, 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 := 4;
    raw_aps := [[0, 0], [0, 2], [0, 3], [32, 0], [0, 2], [0, 5], [-98, 0], [0, -22], [-32, 0], [0, -43], [256, 0], [0, 23], [-102, 0], [0, 74], [320, 0], [0, 19], [0, -102], [0, -159], [0, 138], [416, 0], [-138, 0], [64, 0], [0, 98], [582, 0], [238, 0], [0, 367], [-992, 0], [0, 146], [0, 185], [-302, 0], [1664, 0], [0, -582], [-1734, 0], [0, -758], [0, 447], [480, 0], [0, -575], [0, -398], [2208, 0], [0, -987], [0, -526], [0, -353], [-3712, 0], [1614, 0], [0, 171], [-4064, 0], [0, 530], [-2816, 0], [0, 962], [0, 163], [-3050, 0], [6336, 0], [-4610, 0], [0, -198], [-5374, 0], [3488, 0], [0, -1191], [-1344, 0], [0, -2149], [-2874, 0], [0, 722], [0, 1635], [0, -2646], [6368, 0], [-8758, 0], [0, -179], [0, -1102], [1202, 0], [0, 1290], [0, -1219], [4834, 0], [4128, 0], [4416, 0], [0, -1045], [0, 3434], [-512, 0], [0, -433], [0, 2609], [-12130, 0], [-5014, 0], [0, -2006], [0, 587], [1728, 0], [62, 0], [-14112, 0], [0, 1122], [-2482, 0], [-5894, 0], [0, -1767], [-7616, 0], [0, -3270], [13568, 0], [1696, 0], [0, -774], [0, 4802], [16224, 0], [0, -2823], [5178, 0], [0, 1714], [0, 3433], [0, -2742], [0, -6403], [0, 2442], [-22838, 0], [0, -2302], [-10878, 0], [0, -4510], [26994, 0], [-18336, 0], [9286, 0], [17536, 0], [0, 1467], [19286, 0], [0, -1310], [-15520, 0], [654, 0], [0, 2058], [-24672, 0], [0, 5513], [0, -3006], [0, 4775], [9902, 0], [0, -4921], [0, -4966], [0, -814], [0, -719], [0, -1825], [2880, 0], [8800, 0], [0, 5269], [0, 4834], [-13664, 0], [-19520, 0], [0, -5001], [-31478, 0], [7054, 0], [0, -2401], [0, 786], [0, 5521], [-14950, 0], [0, 5858], [0, -261], [-18208, 0], [0, 3122], [0, -7543], [32544, 0], [0, 6539], [-18646, 0], [0, 1450], [25088, 0], [0, -751], [43282, 0], [0, 6970], [7392, 0], [0, -7270], [-26688, 0], [19680, 0], [-48466, 0], [-13610, 0], [0, 7673], [0, -1206], [22986, 0], [17184, 0], [0, 730], [-27042, 0], [-44192, 0], [-29824, 0], [0, 2903]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_128_b_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_128_4_b_d();".
// 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_128_4_b_d(:prec:=2)
    chi := MakeCharacter_128_b();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    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_128_4_b_d();".
// 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_128_4_b_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_128_b();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<3,R![64, 0, 1]>,<7,R![-32, 1]>],Snew);
    return Vf;
end function;