
// Make newform 966.4.a.b in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_966_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_966_4_a_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_966_4_a_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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_966_a();"
function MakeCharacter_966_a()
    N := 966;
    order := 1;
    char_gens := [323, 829, 925];
    v := [1, 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_966_a_Hecke(Kf)
    return MakeCharacter_966_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 := [[2], [-3], [-9], [7], [-12], [11], [96], [-40], [-23], [-231], [-94], [47], [-27], [479], [423], [516], [882], [842], [-844], [654], [-496], [260], [-156], [-414], [1343], [270], [449], [276], [-589], [1779], [773], [570], [2355], [2333], [-2220], [-2827], [1928], [-1996], [1140], [3618], [939], [-3598], [-5004], [3095], [-621], [3191], [80], [2606], [-3699], [-6712], [5190], [4800], [7229], [-123], [5886], [357], [-8418], [4172], [-760], [-9339], [-6262], [-5754], [3971], [6156], [-4210], [-1329], [-7444], [9596], [2079], [-1582], [4191], [-11721], [9899], [-8170], [-6163], [-9168], [6834], [-5698], [-942], [-12976], [36], [659], [15573], [-15727], [11666], [14457], [5886], [-7540], [1266], [-12265], [10491], [10062], [13967], [9732], [-6190], [10026], [-14640], [11682], [-20254], [-6712], [10658], [12972], [16329], [-2823], [-21580], [11354], [16872], [-19527], [14076], [-7264], [-18394], [-8527], [-17706], [7238], [21656], [-18027], [-14218], [-18840], [-7611], [30456], [25298], [23555], [-15954], [-10284], [-193], [-6840], [-10354], [9111], [-6676], [116], [314], [9744], [-8476], [-25918], [12534], [9179], [-23121], [16436], [-18669], [-20616], [-16075], [-17010], [-1699], [35106], [-15070], [14082], [42737], [28773], [-4471], [27672], [1316], [34950], [15746], [-9240], [-11695], [48495], [24788], [-32859], [-45817], [-10737], [54225], [-4050], [-35452], [-48048], [8391], [-2322], [6476], [61238], [50318], [3888], [-17478], [32015], [-44199], [-46222], [14828], [32226], [-24262], [-58398], [-18373], [-30730], [42284], [-609], [36272], [61047], [-12045], [43563], [-29698], [-17512], [-769], [-13209], [-22726], [-16779], [-44836], [-36066], [10638], [3288], [23222], [-8950], [-71694], [-46671], [-1506], [2996], [8663], [-16366], [48291], [-15444], [9275], [-34092], [56010], [7868], [-1105], [-64818], [47720], [40104], [68625], [77774], [-83692], [-84315], [-72063], [-51975], [49796], [-4552], [37182], [-32272], [-24072], [77870], [-12252], [37896], [73361], [66480], [-52126], [-1570], [5264], [14604], [-3868], [-38688], [103025], [-99126], [-57795], [55236], [-116118], [-31660], [-10048], [66158], [33783], [-66627], [-106666], [80001], [100679], [48633], [-547], [-73554], [-40395], [78065], [77700], [72123], [64763], [-11170], [15126], [-119128], [78128], [61974], [-69649], [31754], [-16938], [64310], [-48210], [-100224], [-65881], [6546], [-84625], [21908], [56903], [-10138], [120854], [109586], [12003], [-119170], [-98866], [-149895], [-21441], [-16882], [-45894], [-121012], [94997], [156966], [-91372], [-122130], [37010], [41397], [-148848], [-56853], [-125052], [116511], [-38806], [-55590], [-108169], [75336], [17190], [129473], [14789], [137484], [-69322], [136929], [102719], [147449], [71043], [-43708], [-155049], [15698], [64065], [-82494], [60102], [123020], [-149556], [98702], [-30960], [81000], [57803], [41268], [-27430], [-183934], [-72930], [97397], [139923], [-75202], [147584], [-200779], [90303], [80748], [128348], [27609], [-70738], [139608], [-29833], [-52008], [72899], [-70518], [-51106], [-49219], [-93094], [-93147], [-43926], [131168], [-17580], [207183], [-68605], [-110914], [-97380], [66633], [42572], [-101587], [73275], [-50200], [-113425], [-145005], [-171930], [972], [-111372], [30186], [-40594], [-93123], [187356], [66708], [-142783], [228719], [90012], [-70228], [182120], [-58788], [107039], [-103590], [-140604], [-28744], [-214408], [36474], [-172101], [-192853], [195330], [19628], [24246], [89802], [-27592], [-10734], [69776], [-175167], [269795], [233723], [-4303], [-112734], [30026], [257898], [135411], [76493], [-37764], [103562], [187049], [67800], [-191437], [27657], [155879], [166182], [70454], [-77196], [-112758], [-144550], [233669], [-250536], [-93448], [154068], [171986], [-70131], [22416], [-178132], [-214], [-105120], [56514], [-154474], [182538], [4002], [150318], [-105868], [39060], [7428], [-207964], [-290196], [-95580], [-219360], [243983], [-226665]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_966_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_966_4_a_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_966_4_a_b(:prec:=1)
    chi := MakeCharacter_966_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_966_4_a_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_966_4_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_966_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![9, 1]>],Snew);
    return Vf;
end function;