
// Make newform 256.12.b.e in Magma, downloaded from the LMFDB on 24 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_12_b_e();".
// 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_12_b_e();".
// 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, 2]];
    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_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], [-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 := 12;
    raw_aps := [[0, 0], [0, 126], [0, -2415], [16744, 0], [0, -267306], [0, -288869], [-6905934, 0], [0, 5330710], [-18643272, 0], [0, 64203315], [-52843168, 0], [0, 91106657], [-308120442, 0], [0, 8562854], [2687348496, 0], [0, 798027849], [0, 2594601870], [0, 3478239331], [0, -7740913442], [-9791485272, 0], [-1463791322, 0], [38116845680, 0], [0, -14667549834], [24992917110, 0], [75013568546, 0], [0, -40871479551], [225755128648, 0], [0, -45120629178], [0, 36741338155], [-85146862638, 0], [-262717201024, 0], [0, 315764379966], [297198746214, 0], [0, -298396788970], [0, 557716810425], [824447297848, 0], [0, 657558377203], [0, -178916379794], [-2754833892216, 0], [0, -475193724789], [0, 840692112390], [0, 498387248009], [2762403350592, 0], [5442387685442, 0], [0, 1438045752177], [-728391402200, 0], [0, -3396584219594], [7334863021472, 0], [0, -679919782962], [0, 5912205611585], [17563353448518, 0], [-7139577462960, 0], [-231306909358, 0], [0, -6491526772626], [23961192565506, 0], [24273728464488, 0], [0, 12918853271835], [-3767932360528, 0], [0, 8209466002937], [-21035722907082, 0], [0, -8356588163266], [0, 11963429493729], [0, 7655546414278], [-49875160575912, 0], [99480832756438, 0], [0, 41684624179683], [0, 31792010962934], [121001428335986, 0], [0, 77830780539102], [0, -12821511097325], [24909815245602, 0], [-157584150853560, 0], [-177901220129584, 0], [0, 27580867011689], [0, -73231558161490], [231449571733632, 0], [0, 74935785805905], [0, 104055340136923], [-133407937691598, 0], [206167580638390, 0], [0, 36701757596910], [0, -85555966169311], [-71775829446768, 0], [99881248225682, 0], [29031220908760, 0], [0, -164184924359346], [-612368143631550, 0], [-303483032911706, 0], [0, -364653973334469], [122188164073712, 0], [0, -308690341831242], [1050837984850080, 0], [219909971761864, 0], [0, 241931564034054], [0, -54438859636250], [-506588355787752, 0], [0, 42876696644355], [-927574652509722, 0], [0, 10909325670614], [0, -847633232526029], [0, 376072375661998], [0, 93744516467403], [0, 122485642958406], [-1352425875836970, 0], [0, -716114238998386], [-877659324192574, 0], [0, 1217125250891382], [-303318212278158, 0], [1701984261329400, 0], [-2339216023326602, 0], [-2496072486898144, 0], [0, -1236504506258431], [-24336777156666, 0], [0, -2112725402574010], [4263261111420568, 0], [1008295928583042, 0], [0, 151991208080446], [-3435828424131096, 0], [0, -592693578384549], [0, -1132551187254570], [0, 2665060760381369], [4741198635421922, 0], [0, 706535819320737], [0, 1515578813538534], [0, -1373656721096954], [0, 2863732721314851], [0, -349163228909455], [9709787093595120, 0], [-2464686886685576, 0], [0, 3956426199377491], [0, -4203468089575730], [-1362869794471992, 0], [6817220120892752, 0], [0, 333524487507497], [7744079729695638, 0], [2524111226919170, 0], [0, 5572634056233489], [0, 6613552127949718], [0, 1151238879123123], [-5604717670694010, 0], [0, 2542582092512294], [0, -139555386899511], [13526517861603928, 0], [0, -136271964676338], [0, 9022958805183715], [7961834707368360, 0], [0, 7491322000695449], [22256130175851894, 0], [0, -2721183012266530], [10810987426382112, 0], [0, -14051218879735637], [4222089494633682, 0], [0, 258045997718566], [-5719060466578536, 0], [0, 42188892150422], [-11009058285505488, 0], [4863513801242680, 0], [3575343976258530, 0], [-38637331724206586, 0], [0, -17449863939203829], [0, -14256145559207802], [-40033434235820202, 0], [-18495304308397016, 0], [0, 10743832848015414], [-8738799717564174, 0], [-11892426157508232, 0], [23440857788319392, 0], [0, 10700207993699777]];
    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_12_b_e();".
// 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_12_b_e(:prec:=2)
    chi := MakeCharacter_256_b();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 12));
    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_12_b_e();".
// 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_12_b_e( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_256_b();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,12,sign)));
    Vf := Kernel([<3,R![63504, 0, 1]>,<7,R![-16744, 1]>],Snew);
    return Vf;
end function;