
// Make newform 8.12.a.a in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_8_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_8_12_a_a();".
// 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_8_12_a_a();".
// 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_8_a();"
function MakeCharacter_8_a()
    N := 8;
    order := 1;
    char_gens := [7, 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_8_a_Hecke(Kf)
    return MakeCharacter_8_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 := 12;
    raw_aps := [[0], [-36], [-3490], [-55464], [-597004], [1373878], [10140850], [-7297396], [-32057464], [-13605402], [233160800], [-257786178], [-221438598], [-1697758892], [527509392], [3277379822], [-3001908988], [-11630023610], [-17189000548], [26169539608], [-7039021094], [-4199910416], [-39739936436], [10565331594], [-69851645662], [74519412606], [78664150648], [78848972820], [-70868029994], [19681604562], [-190251579008], [114524784988], [117101162394], [989560526196], [-797579218418], [1183229001992], [-753512032922], [834305483964], [-1015135845576], [2894963106966], [-4092633678996], [508615079022], [2999513064000], [-2181925750462], [-4774752410466], [295210163608], [4768855897804], [-9214548039008], [-5854438063236], [-14027335387394], [-5296101013318], [-13164628430384], [22589663991378], [26118547450180], [-12657981060350], [5370231542360], [-8601398737994], [-6013709361616], [-26331197092466], [-51174845289782], [8948955462308], [-26847972185154], [6624331875564], [40285233723624], [-21542297569494], [-2794787240634], [-44856017296972], [-52342753988238], [162474635223396], [56070596402598], [121433098473250], [99308194169208], [58096458156880], [10633630708910], [-194563767957820], [-219102152231808], [258688254353630], [-253970045277898], [176194300155186], [254652202822986], [-10976383980612], [-306791483729602], [-194136614783984], [-197020802576750], [-481833288711576], [-472499076448892], [-35241370012606], [541522131945818], [-263903025105418], [-34496762783632], [-588837802998324], [-173689784193632], [842617287500536], [-538407736241004], [-136233889206996], [488252435324712], [-488013674701434], [1107897067126810], [-618140078509580], [761294937204198], [-565709123299012], [-1880894854259434], [-176230678421524], [-491855357914582], [918553298629380], [-1538421618029374], [5660273583284], [-411514325733262], [3353751804185160], [1025019084513418], [-1990791782045920], [-516975009730690], [-678132802269894], [-655793062849004], [336387833915560], [-2680164396379006], [163908120381276], [-3113190842401064], [3156735277314102], [-2219965096229364], [2945325261081614], [2039719577287138], [147928819164734], [-4940163148028716], [2964030774710636], [845017864698822], [-6161944565636962], [7674671417903984], [2553817398932424], [10663897197233702], [4939672509585788], [10483772357498360], [-9786966289594416], [-493316791880914], [8971679370118762], [3726504971788802], [1123373503759710], [-13968284524139636], [17722640561243366], [-13527097038061958], [11163406521710164], [-12963905005939218], [-3202381470941464], [9928785760151044], [11793494274289990], [-290602147861224], [-15798108662290354], [16482619724756106], [-62183817984156], [-17713423825522144], [8215556356233942], [-19185753602969390], [10441168811139500], [9339120463680424], [-13597564581061004], [24491838693488048], [19471493195311624], [-37246532785572638], [-3425214069982470], [-26922797018900586], [-28732237659322260], [-17745400909311318], [24560234585384600], [-4691582779295436], [-27787204933173774], [-10951940338723128], [33627950376561568], [52710991601358334]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_8_12_a_a();".
// 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_8_12_a_a(:prec:=1)
    chi := MakeCharacter_8_a();
    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_8_12_a_a();".
// 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_8_12_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,12,sign)));
    Vf := Kernel([<3,R![36, 1]>],Snew);
    return Vf;
end function;