
// Make newform 256.4.a.h in Magma, downloaded from the LMFDB on 05 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_256_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_256_4_a_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_a_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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_256_a();"
function MakeCharacter_256_a()
    N := 256;
    order := 1;
    char_gens := [255, 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_256_a_Hecke(Kf)
    return MakeCharacter_256_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], [10], [0], [0], [-18], [0], [90], [106], [0], [0], [0], [0], [-522], [-290], [0], [0], [846], [0], [-70], [0], [430], [0], [-1350], [-1026], [-1910], [0], [0], [1710], [0], [-270], [0], [1242], [-2250], [-1474], [0], [0], [0], [970], [0], [0], [3834], [0], [0], [2090], [0], [0], [-6118], [0], [6570], [0], [6030], [0], [-1222], [4302], [-3870], [0], [0], [0], [0], [9342], [8030], [0], [-7990], [0], [8390], [0], [-8242], [11410], [6030], [0], [4770], [0], [0], [0], [-11666], [0], [0], [0], [7002], [-16346], [16794], [0], [0], [-5510], [0], [18270], [17514], [18070], [0], [0], [-15030], [0], [0], [12222], [-18214], [0], [0], [-9162], [4750], [0], [21850], [0], [-23670], [-2394], [27038], [-19550], [10350], [-26190], [0], [14398], [0], [0], [-28530], [-30706], [0], [-6678], [-28550], [0], [0], [29754], [0], [-19190], [0], [-11970], [1978], [0], [0], [0], [0], [0], [36074], [0], [0], [0], [34182], [40106], [0], [-6950], [0], [-14346], [37582], [0], [0], [-23490], [0], [0], [0], [18630], [45646], [0], [0], [-41742], [5290], [0], [-26210], [0], [0], [-6966], [56270], [0], [-58230], [45990], [0], [-162], [17370], [0], [0], [0], [60154], [0], [-54306], [0], [0], [46190], [0], [50814], [-64402], [0], [0], [0], [0], [-71478], [0], [-70290], [0], [0], [0], [29450], [1334], [0], [-6910], [-72450], [80138], [0], [-52470], [-80370], [47698], [0], [46530], [0], [0], [0], [0], [-6014], [22446], [0], [0], [-88470], [-32274], [-29522], [-70630], [0], [0], [54270], [0], [85462], [0], [24282], [0], [0], [0], [0], [-103446], [0], [-4950], [0], [71550], [0], [0], [77598], [0], [8746], [0], [5742], [-43490], [0], [-63974], [0], [-114786], [0], [118170], [65342], [0], [0], [-111150], [0], [0], [106938], [93566], [0], [0], [-29502], [0], [-60946], [0], [95706], [0], [-109330], [0], [-37370], [0], [42570], [0], [0], [117450], [-133414], [0], [71838], [-104690], [0], [0], [138890], [-120130], [0], [-141550], [0], [132030], [0], [10798], [71082], [0], [0], [0], [0], [-154370], [0], [50330], [0], [0], [-86526], [0], [-144630], [26910], [34542], [0], [0], [0], [0], [163134], [-176870], [151670], [0], [0], [102330], [-117218], [-42550], [181710], [0], [0], [0], [0], [0], [179658], [187450], [0], [173774], [-16614], [0], [154690], [-109134], [-27142], [84710], [0], [0], [-147690], [191482], [13066], [-65090], [0], [0], [0], [0], [0], [-50310], [-94498], [116910], [0], [177570], [187342], [0], [0], [-214650], [0], [0], [0], [175626], [0], [-165490], [0], [0], [161962], [-156170], [0], [0], [0], [123390], [0], [233118], [76410], [0], [0], [195822], [0], [44046], [212330], [-214130], [0], [0], [154438], [130842], [234574], [0], [0], [0], [0], [-128934], [0], [-15550], [-262854], [77510], [0], [255510], [0], [-238590], [80746], [0], [0], [0], [220430], [0], [150626], [0], [-235314], [-276230], [0], [-168410], [0], [226134], [68542], [0], [0], [-272790], [0], [-292410], [0], [0], [0], [-204102], [295450], [146394], [-145550], [0], [-191250], [-102998], [24910], [0], [0], [0], [51930], [0], [0], [0], [0], [-161874], [-281770], [0], [-239670], [154494], [246238], [0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_256_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_256_4_a_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_a_h(:prec:=1)
    chi := MakeCharacter_256_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(2999) - 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_a_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_a_h( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_256_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<3,R![-10, 1]>,<5,R![0, 1]>,<7,R![0, 1]>],Snew);
    return Vf;
end function;