
// Make newform 966.4.a.a in Magma, downloaded from the LMFDB on 27 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_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_966_4_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_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], [-6], [7], [48], [38], [114], [56], [-23], [-162], [-16], [-46], [-342], [248], [-24], [426], [-852], [338], [488], [336], [362], [1184], [-336], [-78], [746], [30], [944], [264], [578], [282], [-856], [-348], [-2598], [-2692], [-966], [1208], [362], [3164], [2352], [-1458], [2076], [-358], [3960], [-1198], [1446], [4952], [-1084], [1856], [-168], [2858], [-3942], [6048], [-598], [-600], [882], [-4920], [942], [824], [-4906], [-3822], [2408], [-1014], [-3988], [4680], [2450], [6414], [-7540], [-2014], [-4428], [5582], [-5934], [2256], [3296], [13250], [-8128], [-8976], [-14478], [-14122], [1266], [6554], [-2016], [4322], [-11112], [12554], [416], [-10044], [-14190], [3794], [6366], [2936], [11904], [-15120], [-5344], [13908], [-7780], [5808], [8910], [19002], [-4000], [5150], [-2476], [-9798], [-1464], [9306], [-23056], [-526], [18876], [-17214], [2808], [19898], [-23344], [2282], [-28926], [-7288], [18128], [16626], [14288], [27192], [-9066], [9504], [16778], [-15982], [7674], [-12804], [-6508], [-29094], [14834], [-37200], [-29608], [5930], [19196], [1512], [7400], [-7894], [7818], [-25990], [20466], [-5248], [6234], [14970], [-35620], [-25458], [-32248], [31704], [-33010], [-264], [21710], [9546], [-40156], [-36096], [22094], [19866], [41348], [-44448], [-1144], [-25848], [-30808], [21810], [-23734], [34050], [20700], [52314], [-32968], [2160], [-46446], [24432], [3920], [5126], [314], [-41970], [-37944], [-9106], [-15720], [-49318], [-4744], [49074], [-9004], [57438], [34688], [-48790], [-37744], [53376], [-10906], [46026], [-69816], [-42030], [-46378], [-61648], [26810], [42888], [5330], [-22932], [21056], [26550], [-28560], [-64446], [40322], [-13462], [7866], [26040], [-5718], [13496], [36614], [10850], [-76848], [38814], [-18016], [-68508], [-61686], [23348], [16610], [-21330], [-48592], [56544], [-71136], [22514], [-41200], [-3726], [-62424], [37206], [46094], [-61408], [-105534], [88616], [36204], [30302], [82770], [85320], [-61216], [-42540], [-83842], [-34288], [-34360], [-8934], [33080], [47496], [81434], [102354], [-59700], [10368], [28680], [-111436], [57224], [106022], [44658], [-24552], [-115864], [9024], [-107944], [82176], [-65806], [46506], [-47304], [19322], [-26730], [23196], [-33814], [-87904], [-82554], [-108358], [30728], [-34896], [43670], [-78622], [-61470], [131744], [63402], [6210], [98840], [44790], [57854], [51824], [32762], [-17752], [-17134], [-71200], [-56340], [105110], [17786], [96408], [73008], [-29488], [52080], [7706], [-98812], [71184], [-53110], [-140190], [-119968], [-114270], [63426], [-62448], [-56022], [94272], [-9058], [73506], [139952], [-120210], [-51180], [-7276], [-50446], [-89478], [-101536], [11460], [7088], [73922], [15204], [98786], [55800], [54110], [104496], [50010], [98034], [-153436], [-71280], [149114], [11244], [10488], [-107902], [48930], [-95776], [94898], [5142], [-20176], [46314], [186890], [25520], [113804], [18912], [131970], [160190], [-126714], [117992], [201492], [73448], [-172764], [-187270], [-173670], [-186310], [98744], [160670], [-78246], [-15426], [-70360], [-127158], [140076], [16910], [67556], [20856], [60474], [-81604], [-56230], [-224274], [103232], [214202], [-188742], [154128], [-211080], [77106], [-211392], [-26134], [230970], [-214680], [54624], [-134044], [-15958], [-138522], [-139720], [132050], [-232548], [163508], [-150024], [-155934], [-160864], [9446], [-39948], [-12696], [70034], [63234], [-27286], [-197238], [247770], [68816], [-165486], [-118264], [-32856], [-25768], [-55210], [160544], [-80760], [-114718], [258198], [-72900], [-184156], [3216], [179138], [-93784], [83898], [-147784], [94638], [18758], [-221646], [166736], [-178518], [-204498], [242264], [123794], [-100350], [-49600], [15900], [-44350], [-65298], [-170472], [40424], [-53734], [-11490], [-77832], [42392], [115074], [-194304], [254586], [-218158], [-151776], [229548], [263018], [-261498], [43272], [-289014], [128396], [320976]];
    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_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_966_4_a_a(: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_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_966_4_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_966_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![6, 1]>],Snew);
    return Vf;
end function;