
// Make newform 128.3.f.b in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_128_f();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_128_f_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_3_f_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_128_3_f_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 := [8, -8, 6, -6, 3, -2, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0], [8, -2, 4, -3, 0, -1], [4, -14, 8, -5, 4, 1], [12, 2, 8, -9, 4, -3], [-20, 14, -8, 9, -4, 3], [20, -10, 16, -7, 4, -5]];
    Rf_basisdens := [1, 4, 4, 4, 4, 4];
    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_f();"
function MakeCharacter_128_f()
    N := 128;
    order := 4;
    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_f_Hecke();"
function MakeCharacter_128_f_Hecke(Kf)
    N := 128;
    order := 4;
    char_gens := [127, 5];
    char_values := [[-1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 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 := 3;
    raw_aps := [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -1], [1, 1, 0, 1, 0, 1], [0, 0, 1, 1, 0, 0], [4, -4, 2, 0, 1, 0], [1, -1, -1, 0, 3, 0], [2, 0, 1, 1, 3, 3], [-4, -4, 0, 2, 0, 1], [8, 0, -1, -1, -2, -2], [1, -1, 1, 0, -7, 0], [0, 24, -2, 2, -2, 2], [-7, -7, 0, -5, 0, 7], [0, -8, 6, -6, 6, -6], [16, -16, -8, 0, -1, 0], [0, -24, -2, 2, -10, 10], [-15, -15, 0, -1, 0, -5], [-32, 32, 4, 0, 3, 0], [-7, 7, -5, 0, -1, 0], [44, 44, 0, 14, 0, 5], [-56, 0, -1, -1, -18, -18], [0, 24, -11, 11, -13, 13], [0, -64, -4, 4, 20, -20], [-56, -56, 0, 0, 0, -9], [0, 8, -5, 5, 13, -13], [2, 0, -11, -11, 15, 15], [-23, -23, 0, 15, 0, -13], [-40, 0, 11, 11, 26, 26], [60, -60, 6, 0, 5, 0], [-71, 71, -15, 0, -15, 0], [2, 0, -2, -2, -22, -22], [0, 104, -22, 22, -6, 6], [28, 28, 0, -26, 0, -3], [0, -8, 8, -8, -20, 20], [8, -8, -20, 0, 19, 0], [57, 57, 0, -19, 0, -19], [24, 0, -21, -21, -22, -22], [-7, 7, -25, 0, -29, 0], [16, 16, 0, 16, 0, 31], [32, 0, -7, -7, 24, 24], [-111, 111, 15, 0, 35, 0], [-24, -24, 0, 8, 0, 15], [-119, -119, 0, 5, 0, -27], [0, -104, 22, -22, 6, -6], [2, 0, -7, -7, -5, -5], [97, 97, 0, 9, 0, 57], [48, 0, 21, 21, -12, -12], [32, 32, 0, -36, 0, -61], [0, -72, 6, -6, 6, -6], [0, 0, 0, 40, 0, -41], [-63, -63, 0, 37, 0, 77], [0, -8, -35, 35, -5, 5], [0, 192, -12, 12, -4, 4], [130, 0, 45, 45, 39, 39], [40, -40, 24, 0, -25, 0], [-30, 0, -26, -26, 2, 2], [248, 0, -13, -13, 2, 2], [201, -201, -31, 0, 49, 0], [0, 24, 34, -34, -22, 22], [145, 145, 0, -45, 0, -73], [0, -88, 29, -29, 3, -3], [124, -124, 18, 0, -39, 0], [-175, -175, 0, -55, 0, 9], [164, 164, 0, -18, 0, 45], [-304, 0, -19, -19, -36, -36], [0, -152, 22, -22, 30, -30], [-127, 127, -51, 0, -67, 0], [-220, 220, 18, 0, -111, 0], [34, 0, -32, -32, -96, -96], [-32, 32, -68, 0, 75, 0], [65, -65, 49, 0, 9, 0], [162, 0, -6, -6, 62, 62], [376, 0, 35, 35, -14, -14], [0, -24, 30, -30, 86, -86], [-207, -207, 0, 47, 0, 11], [128, -128, -12, 0, -77, 0], [0, -336, -36, 36, -36, 36], [137, 137, 0, -29, 0, -17], [249, -249, -3, 0, 53, 0], [-62, 0, 37, 37, 15, 15], [0, -56, -58, 58, 62, -62], [132, 132, 0, 34, 0, 89], [-159, -159, 0, 13, 0, -155], [0, 96, 28, -28, 100, -100], [-62, 0, 37, 37, -17, -17], [96, 0, 25, 25, -56, -56], [-116, 116, 82, 0, 137, 0], [66, 0, -11, -11, 79, 79], [0, 152, -58, 58, -74, 74], [281, -281, 21, 0, -35, 0], [0, -16, -8, 8, -112, 112], [-140, -140, 0, 30, 0, -147], [0, -264, -10, 10, 54, -54], [-288, 0, -39, -39, -40, -40], [72, -72, -108, 0, -85, 0], [16, 16, 0, -36, 0, -109], [-280, 0, 7, 7, -26, -26], [-135, 135, 27, 0, -97, 0], [0, -72, -64, 64, 52, -52], [44, -44, -26, 0, 117, 0], [9, -9, 27, 0, 15, 0], [28, 28, 0, -18, 0, 181], [-223, 223, 35, 0, 111, 0], [-420, -420, 0, -22, 0, 57], [0, -312, 0, 0, -52, 52], [-68, 68, -86, 0, 1, 0], [66, 0, 28, 28, 52, 52], [236, -236, -90, 0, -203, 0], [418, 0, 60, 60, 116, 116], [-128, 0, -95, -95, 16, 16], [0, 200, -17, 17, -183, 183], [0, 480, -16, 16, 32, -32], [-95, -95, 0, -59, 0, 13], [0, -552, 67, -67, -35, 35], [100, -100, 138, 0, 89, 0], [632, 0, -13, -13, 18, 18], [-62, 0, 33, 33, -109, -109], [252, 252, 0, -26, 0, -35], [296, 0, 151, 151, -26, -26], [169, -169, 41, 0, 105, 0], [-488, -488, 0, -40, 0, 79], [249, 249, 0, 117, 0, 197], [-510, 0, -47, -47, 35, 35], [57, 57, 0, 59, 0, 135], [720, -720, 104, 0, 15, 0], [208, 208, 0, 188, 0, 51], [-87, 87, 31, 0, 283, 0], [89, 89, 0, 87, 0, 91], [0, -192, 140, -140, 132, -132], [656, 0, 13, 13, -4, -4], [-175, 175, -91, 0, -11, 0], [40, 40, 0, -4, 0, -101], [304, 0, -43, -43, 148, 148], [0, -544, -84, 84, -44, 44], [-55, -55, 0, -91, 0, 69], [0, 424, 2, -2, -126, 126], [-62, 0, -55, -55, 11, 11], [121, 121, 0, 199, 0, -117], [248, 248, 0, 88, 0, 47], [265, -265, 227, 0, -25, 0], [0, 632, -30, 30, -102, 102], [-324, 324, -122, 0, 69, 0], [369, 369, 0, -105, 0, 195], [144, 0, -35, -35, 140, 140], [-356, 356, 74, 0, -63, 0], [89, -89, -37, 0, 127, 0], [-80, 0, -155, -155, -220, -220], [-191, -191, 0, -177, 0, 27], [0, 40, 52, -52, 184, -184], [112, -112, -28, 0, 147, 0], [0, -968, 86, -86, 54, -54], [361, -361, 109, 0, 21, 0], [482, 0, -36, -36, 148, 148], [-140, -140, 0, -74, 0, 85], [-464, 0, -91, -91, 52, 52], [-288, 288, -56, 0, -33, 0], [0, 280, -174, 174, -6, 6], [168, 0, 103, 103, 70, 70], [-574, 0, 109, 109, 23, 23], [0, 696, -11, 11, -29, 29], [-39, 39, -223, 0, -415, 0], [-156, -156, 0, -210, 0, -211], [0, -632, -124, 124, 56, -56], [-448, 0, -15, -15, -208, -208], [56, -56, 260, 0, 267, 0], [578, 0, -63, -63, -93, -93], [-1232, 0, 53, 53, -124, -124], [0, 416, -112, 112, 32, -32], [489, 489, 0, 103, 0, -5]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_128_f_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_128_3_f_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_128_3_f_b(:prec:=6)
    chi := MakeCharacter_128_f();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 3));
    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_3_f_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_128_3_f_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_128_f();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<3,R![8, 56, 196, 32, 2, -2, 1]>],Snew);
    return Vf;
end function;