
// Make newform 966.6.a.b in Magma, downloaded from the LMFDB on 19 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_6_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_6_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 := 6;
    raw_aps := [[4], [9], [28], [49], [-15], [-1066], [-80], [-1319], [529], [-3586], [-7850], [5074], [-17627], [10392], [-11805], [-345], [-31347], [-5019], [-26552], [-23492], [-5356], [99870], [47482], [-58182], [72722], [-7315], [26791], [180228], [170440], [31666], [-58147], [-95801], [153687], [316100], [-378667], [-97703], [-582567], [-347407], [170559], [125346], [341736], [-26378], [509995], [-265715], [-384814], [-423655], [387057], [-299642], [-1296486], [449749], [-529188], [413400], [1263519], [1160178], [-1278151], [-622739], [-1544394], [1026948], [-896465], [793958], [-220788], [-541868], [-2871224], [2389045], [850937], [-24742], [-2498135], [-3782646], [-1250946], [4068634], [2478994], [4149360], [3704545], [-57154], [-3854266], [-4729594], [-3230518], [414238], [1527517], [-2836742], [-78114], [-1585536], [-1021599], [246539], [-619792], [-5744504], [4806354], [-2654474], [6457970], [-3395351], [-3629894], [-600524], [-2406876], [-2774930], [3688716], [6329456], [2867189], [-7274742], [11733393], [3986635], [8478448], [11166618], [-3617052], [-5539479], [5991944], [-2121210], [-5734029], [-11927658], [-10837914], [6618164], [-5316444], [-4597866], [15191262], [7601444], [4708426], [345153], [10549099], [-14990212], [658304], [20991904], [668861], [2601769], [8724400], [20882248], [8868128], [-10305433], [-6853270], [-12525528], [10650491], [-18078154], [-6391220], [7485237], [-9450328], [-13256812], [14578035], [-3476934], [12095344], [-5549333], [32930478], [7095638], [15621118], [22688216], [16171231], [2971505], [-34226522], [35852638], [-21470172], [-19067927], [-38148464], [37820406], [13988735], [12858738], [28413368], [-15964560], [-17740472], [771952], [-5431870], [8300882], [-8199469], [-10127544], [14600640], [-21595505], [-14592412], [40760878], [34098555], [40137298], [-29724495], [486768], [-5934862], [25428185], [12511049], [-22951838], [-7189755], [26791744], [8858810], [-7770950], [-66188673], [68746298], [-46472255], [20221538], [28424323], [-67308306], [41630321], [34670097], [-21043455], [-51283310], [-45439903], [-58846492], [-46780775], [-74512312], [-63216294], [-21258304], [46167000], [-68155699], [-58873007], [29438176], [36890486], [-75618574], [-22927348], [-21041241], [7945691], [-97611036], [37981958], [-22871175], [28480426], [-12220909], [50385448], [-75946290], [4045456], [-88899890], [16984653], [-52463385], [-92585918], [14194342], [-29433152], [-55101582], [-124630205], [-49621710], [76321792], [-21331266], [48617675], [97303237], [-13157264], [106172948], [30360943], [-65124775], [35111710], [72338536], [-52504912], [-73689040], [135577781], [48267220], [-139523024], [-2745932], [-63305241], [-11441448], [-56195857], [-85902630], [-153272416], [-85880400], [-96925717], [32821528], [-151244432], [152190979], [-138914583], [86796433], [-45103658], [-78919758], [70744906], [36782923], [-109473788], [-159817492], [70215755], [-67844831], [-76071042], [679158], [107601188], [30986611], [96732583], [121011838], [-208806816], [-72535983], [121139078], [-39928439], [30157731], [9643561], [-5693317], [133575578], [-130755028], [-42423156], [161482276], [-360518], [96168262], [-133434834], [65804484], [78681674], [203184220], [-13341557], [28183424], [93487032], [-86193485], [-127193808], [-89497438], [-95904086], [158486746], [51001430], [173780206], [3955759], [132265052], [168680626], [269872305], [-8798], [152757282], [-18583696], [243404706], [239019028], [-39265036], [-146829389], [8440221], [-279592510], [170760926], [200323086], [101382358], [-116039436], [225357246], [315874107], [259445224], [-207021669], [-19980783], [-223188790], [159578885], [101884134], [243128659], [-90340416], [-276381463], [-16228117], [-383192855], [-336220448], [-18485561], [-401417820], [-411800441], [52917582], [-29690899], [309183299], [163857663], [-47001881], [-312514776], [411874672], [-251924691], [133114473], [71838338], [-6658044], [-211170996], [-132348369], [-161088488], [-75806335], [203417506], [270863886], [196822166], [-427562336], [140390890], [120894789], [206079951], [205399686], [-40053265], [-163736458], [-168729292], [-170848733], [257730210], [-3766686], [-67740178], [381004579], [-141978176], [-378028741], [10869734], [418487065], [-50962590], [89845658], [81036026], [67499454], [-221238028], [-531066285], [-262460172], [-347917357], [-357802892], [-92600873], [-525343209], [-46564326], [-333458438], [337767480], [-483455112], [-165541030], [-561367191], [197323819], [406852038], [-436165260], [460302707], [-284441673], [-387827612], [-667614140], [680539376], [-79177434], [-665787985], [-64585128], [208001244], [-279018839], [313788583], [645575228], [608933004], [-24141684], [-599374908], [143068709], [581621186], [333219980], [-542268929], [-149792014], [-527958259], [482582382], [-512513946], [476858206], [-167648408], [260636568], [-536253498], [-195088002], [-72182713], [408910846], [458169980], [-680598178], [-406486568], [35444652], [88308146], [156610830], [-189237531], [399678432], [392170554], [547574514], [719498686], [567979520], [571740788], [139174704], [-149501754], [554680511], [387170778], [-542030748], [65828900], [269071821], [-541068765], [-476583438], [-493083618], [-562484343]];
    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_6_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_6_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, 6));
    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_6_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_6_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_966_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<5,R![-28, 1]>],Snew);
    return Vf;
end function;