
// Make newform 256.4.b.h in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_256_b();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_256_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_256_4_b_h();".
// 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_256_4_b_h();".
// 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, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 0, 0, 2], [-4, 0, 8, 0], [0, 16, 0, -8]];
    Rf_basisdens := [1, 1, 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_256_b();"
function MakeCharacter_256_b()
    N := 256;
    order := 2;
    char_gens := [255, 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_256_b_Hecke();"
function MakeCharacter_256_b_Hecke(Kf)
    N := 256;
    order := 2;
    char_gens := [255, 5];
    char_values := [[1, 0, 0, 0], [-1, 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 := 4;
    raw_aps := [[0, 0, 0, 0], [0, 1, -1, 0], [0, 1, 2, 0], [-4, 0, 0, -1], [0, -23, -1, 0], [0, -25, 6, 0], [46, 0, 0, 6], [0, 1, 7, 0], [4, 0, 0, 9], [0, -21, -10, 0], [192, 0, 0, 8], [0, -43, -14, 0], [150, 0, 0, -4], [0, -75, -5, 0], [8, 0, 0, -22], [0, -3, -14, 0], [0, 161, -33, 0], [0, -73, 70, 0], [0, -43, 83, 0], [204, 0, 0, -21], [-206, 0, 0, 26], [200, 0, 0, 18], [0, 237, -13, 0], [-286, 0, 0, 58], [1102, 0, 0, 46], [0, -531, -14, 0], [-1732, 0, 0, 7], [0, 361, -145, 0], [0, -357, -42, 0], [98, 0, 0, -48], [1328, 0, 0, -60], [0, 237, 171, 0], [710, 0, 0, -20], [0, -547, 179, 0], [0, 441, 386, 0], [772, 0, 0, -151], [0, -1041, 6, 0], [0, 193, -401, 0], [364, 0, 0, 123], [0, 1311, -26, 0], [0, -339, -253, 0], [0, 625, 98, 0], [-2480, 0, 0, -116], [-242, 0, 0, -66], [0, -1447, 34, 0], [380, 0, 0, -81], [0, -1403, -149, 0], [4416, 0, 0, -72], [0, 1065, -233, 0], [0, 569, 66, 0], [-2286, 0, 0, -214], [-936, 0, 0, 214], [622, 0, 0, -378], [0, 1521, -49, 0], [2322, 0, 0, -96], [-2484, 0, 0, -357], [0, -941, -202, 0], [1912, 0, 0, -122], [0, 1821, -366, 0], [1458, 0, 0, 66], [0, -2283, 291, 0], [0, 1721, -478, 0], [0, -471, 127, 0], [-5596, 0, 0, 361], [2150, 0, 0, -420], [0, -2037, 534, 0], [0, 3521, -169, 0], [3154, 0, 0, -152], [0, 1521, 127, 0], [0, 2179, 1046, 0], [5010, 0, 0, -472], [-8644, 0, 0, -153], [1928, 0, 0, 634], [0, -3547, -302, 0], [0, 3529, 1031, 0], [-6144, 0, 0, -256], [0, 3893, -206, 0], [0, -1605, -1418, 0], [5070, 0, 0, 118], [-2266, 0, 0, -628], [0, 4477, 683, 0], [0, -847, -990, 0], [-9832, 0, 0, -170], [-10098, 0, 0, 86], [-3228, 0, 0, -103], [0, -747, -1229, 0], [-3346, 0, 0, -2], [4566, 0, 0, 652], [0, 595, 342, 0], [-2872, 0, 0, 786], [0, 3737, -1377, 0], [11328, 0, 0, -552], [10732, 0, 0, 11], [0, -163, 2147, 0], [0, 1477, 795, 0], [1940, 0, 0, -211], [0, -8033, 230, 0], [7270, 0, 0, 252], [0, -319, 311, 0], [0, 6151, 1478, 0], [0, -6691, 459, 0], [0, 4103, -154, 0], [0, 4905, 1407, 0], [-2090, 0, 0, 572], [0, 6341, -413, 0], [2114, 0, 0, -120], [0, -6343, -1745, 0], [7986, 0, 0, -880], [-5580, 0, 0, 541], [18114, 0, 0, 90], [6928, 0, 0, -940], [0, -5263, -478, 0], [-17086, 0, 0, 370], [0, 1009, 919, 0], [11860, 0, 0, 125], [-17874, 0, 0, -18], [0, 549, -3773, 0], [11340, 0, 0, -1253], [0, 1539, 1590, 0], [0, 5229, 979, 0], [0, 4001, 1762, 0], [1102, 0, 0, 414], [0, 1285, -398, 0], [0, 6573, 1491, 0], [0, 5805, -765, 0], [0, -1417, 4006, 0], [0, -1691, 2578, 0], [-9432, 0, 0, 218], [-3276, 0, 0, 1725], [0, -6933, 1174, 0], [0, 5673, 3607, 0], [-9172, 0, 0, 347], [2488, 0, 0, -1346], [0, 8185, -1726, 0], [-13594, 0, 0, -1060], [-8338, 0, 0, -482], [0, 4053, 82, 0], [0, 3589, 2379, 0], [0, -18513, 230, 0], [16278, 0, 0, -164], [0, -4719, -1897, 0], [0, 3941, 1938, 0], [-4828, 0, 0, -631], [0, 3949, -3925, 0], [0, -6305, -2330, 0], [15324, 0, 0, -1065], [0, 5869, 3314, 0], [-4938, 0, 0, -2340], [0, 3649, 3215, 0], [-10528, 0, 0, -80], [0, -10237, 86, 0], [-25486, 0, 0, -792], [0, 401, 3943, 0], [35556, 0, 0, 457], [0, -4891, 3227, 0], [-9192, 0, 0, -2210], [-10332, 0, 0, 97], [21102, 0, 0, -1506], [14162, 0, 0, -2294], [0, 3883, 3286, 0], [0, -19879, -337, 0], [52022, 0, 0, -244], [10780, 0, 0, 3047], [0, -1819, -3269, 0], [-19506, 0, 0, -794], [7892, 0, 0, -3243], [17264, 0, 0, 2204], [0, 1845, -2478, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_256_b_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_256_4_b_h();".
// 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_256_4_b_h(:prec:=4)
    chi := MakeCharacter_256_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_256_4_b_h();".
// 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_256_4_b_h( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_256_b();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<3,R![1936, 0, 104, 0, 1]>,<7,R![-176, 8, 1]>],Snew);
    return Vf;
end function;