
// Make newform 128.4.a.f in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_128_a();"
// 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_a_f();".
// 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_a_f();".
// 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 := [-3, 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_a();"
function MakeCharacter_128_a()
    N := 128;
    order := 1;
    char_gens := [127, 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;

function MakeCharacter_128_a_Hecke(Kf)
    return MakeCharacter_128_a();
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], [-2, 1], [2, 2], [4, 2], [-46, -1], [50, -6], [46, 12], [-2, -7], [-4, -18], [42, 10], [192, 16], [-86, -14], [-150, 8], [-150, -5], [8, -44], [-6, -14], [322, -33], [146, -70], [86, -83], [-204, 42], [206, -52], [200, 36], [-474, 13], [286, -116], [1102, 92], [-1062, -14], [1732, -14], [722, -145], [714, 42], [98, -96], [1328, -120], [-474, -171], [-710, 40], [-1094, 179], [882, 386], [-772, 302], [2082, -6], [-386, 401], [-364, -246], [-2622, 26], [678, 253], [1250, 98], [-2480, -232], [-242, -132], [-2894, 34], [-380, 162], [2806, 149], [4416, -144], [-2130, 233], [1138, 66], [2286, 428], [-936, 428], [622, -756], [3042, -49], [2322, -192], [2484, 714], [1882, 202], [1912, -244], [3642, -366], [-1458, -132], [-4566, 291], [3442, -478], [942, -127], [5596, -722], [-2150, 840], [4074, -534], [7042, -169], [3154, -304], [3042, 127], [-4358, -1046], [5010, -944], [8644, 306], [1928, 1268], [-7094, -302], [7058, 1031], [-6144, -512], [7786, -206], [3210, 1418], [5070, 236], [2266, 1256], [-8954, -683], [-1694, -990], [-9832, -340], [-10098, 172], [3228, 206], [-1494, -1229], [-3346, -4], [-4566, -1304], [-1190, -342], [-2872, 1572], [-7474, 1377], [11328, -1104], [-10732, -22], [-326, 2147], [-2954, -795], [-1940, 422], [16066, -230], [-7270, -504], [-638, 311], [-12302, -1478], [13382, -459], [-8206, 154], [-9810, -1407], [2090, -1144], [12682, -413], [2114, -240], [-12686, -1745], [7986, -1760], [5580, -1082], [-18114, -180], [6928, -1880], [-10526, -478], [17086, -740], [2018, 919], [-11860, -250], [-17874, -36], [-1098, 3773], [-11340, 2506], [-3078, -1590], [-10458, -979], [8002, 1762], [1102, 828], [2570, -398], [13146, 1491], [-11610, 765], [2834, -4006], [-3382, 2578], [-9432, 436], [3276, -3450], [13866, -1174], [-11346, -3607], [9172, -694], [2488, -2692], [16370, -1726], [13594, 2120], [-8338, -964], [8106, 82], [-7178, -2379], [37026, -230], [-16278, 328], [-9438, -1897], [7882, 1938], [4828, 1262], [7898, -3925], [12610, 2330], [-15324, 2130], [11738, 3314], [4938, 4680], [7298, 3215], [-10528, -160], [20474, -86], [-25486, -1584], [-802, -3943], [-35556, -914], [-9782, 3227], [-9192, -4420], [10332, -194], [21102, -3012], [-14162, 4588], [-7766, -3286], [39758, 337], [-52022, 488], [-10780, -6094], [-3638, -3269], [-19506, -1588], [-7892, 6486], [17264, 4408], [3690, -2478]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_128_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_128_4_a_f();".
// 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_a_f(:prec:=2)
    chi := MakeCharacter_128_a();
    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_a_f();".
// 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_a_f( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_128_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<3,R![-44, 4, 1]>,<5,R![-188, -4, 1]>],Snew);
    return Vf;
end function;