
// Make newform 2166.4.a.h in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_2166_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_2166_4_a_h();".
// 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_2166_4_a_h();".
// 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_2166_a();"
function MakeCharacter_2166_a()
    N := 2166;
    order := 1;
    char_gens := [1445, 1807];
    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_2166_a_Hecke(Kf)
    return MakeCharacter_2166_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], [-19], [9], [-13], [-38], [99], [0], [68], [-130], [-262], [296], [8], [73], [-271], [502], [-540], [587], [-684], [-992], [-507], [-980], [-492], [-810], [1046], [1682], [-978], [-674], [360], [-1838], [1386], [-1083], [499], [-195], [-1485], [-332], [3814], [-3352], [-3714], [1242], [-2310], [-82], [-1313], [2352], [-3826], [2225], [-1632], [-1148], [-5794], [2525], [-1227], [675], [6068], [-3763], [-4304], [-8247], [5430], [5452], [-6311], [6158], [5853], [5892], [-1824], [9757], [-5722], [-10224], [-1712], [-934], [-6491], [3205], [-3522], [-7365], [10624], [-2528], [6810], [8222], [1725], [11669], [-9592], [-9130], [-3660], [13438], [1458], [13342], [-4490], [2713], [-2140], [-2221], [16907], [-2237], [-311], [12640], [-13324], [5552], [-15125], [-1632], [-4410], [2488], [17662], [10487], [-13174], [-14321], [7212], [-3490], [6152], [-20941], [-23331], [-18542], [2700], [-17452], [-5114], [19333], [1599], [3580], [18917], [8058], [4153], [17519], [-3957], [14890], [-11812], [-9308], [-9814], [29942], [-25353], [-5038], [30150], [-425], [-18941], [-27702], [4685], [36172], [-2912], [27259], [147], [-14645], [1632], [15776], [17256], [25845], [-10962], [627], [38153], [-23744], [18060], [8590], [-222], [16956], [11245], [-12468], [-8194], [-28833], [29413], [-24544], [26326], [42738], [-38680], [32270], [30989], [8318], [-46716], [50212], [-12336], [-40532], [19426], [-34428], [-10072], [12589], [-34480], [28203], [-28980], [13618], [-45408], [58718], [9690], [-28335], [52292], [-43893], [-3148], [-55585], [-59681], [-12082], [-22738], [17636], [-26517], [-3045], [53566], [-2488], [4270], [-23448], [-37678], [-27397], [21278], [15648], [60844], [-858], [-1963], [75138], [39194], [-60087], [-76560], [-46432], [-15124], [-75050], [44660], [-73741], [-55345], [3792], [-10785], [48778], [26294], [82622], [48753], [876], [-2310], [80688], [51504], [45048], [-30334], [-25287], [-76092], [-24920], [15130], [-61357], [-40604], [23295], [50872], [-36360], [47476], [50127], [-14037], [94750], [24918], [46808], [63743], [-3741], [19605], [65573], [-47740], [168], [-35158], [-96533], [-15932], [77020], [61182], [64365], [-50741], [7048], [48040], [28533], [-110846], [-15223], [-78196], [38380], [-13747], [24460], [18417], [8926], [-102084], [14554], [88472], [41196], [-78045], [19362], [-33411], [90170], [85170], [34797], [-102908], [25953], [139638], [-34264], [-118107], [-141295], [7756], [45648], [7179], [53870], [26168], [138447], [24192], [-127208], [-79371], [-94152], [38749], [-101313], [-90477], [100566], [-10115], [-103490], [43242], [33699], [161922], [-144512], [-84188], [96830], [108748], [-62707], [107570], [146319], [-99647], [80146], [-79895], [-107078], [56847], [19906], [-157584], [14650], [-159945], [-5902], [-43757], [20670], [6008], [-31848], [-122976], [-138210], [-129795], [-85822], [9803], [34615], [-127552], [116349], [-116682], [-123038], [-98562], [59808], [-34810], [130772], [-86104], [-158017], [116887], [-108294], [-72880], [18883], [-54303], [52589], [-74970], [22962], [92327], [61504], [-176908], [-7341], [-49290], [-198572], [31072], [52550], [-148998], [151806], [57238], [-67881], [62138], [-77564], [83357], [-64758], [-15140], [-152398], [-58000], [-63708], [34459], [-56418], [17569], [-68518], [13956], [220240], [-6421], [-58958], [-44166], [77742], [-16402], [122292], [-37350], [47513], [-13600], [-215983], [-176011], [72670], [63272], [-174097], [-105375], [-138384], [-114642], [234413], [-80141], [-53271], [139170], [91882], [138112], [89554], [-111497], [-31414], [-253640], [154042], [54360], [-7781], [58908], [62482], [222500], [-175310], [149798], [230262], [-100700], [-140867], [98286], [-159334], [38160], [161027], [-118571], [-176552], [106442], [-105775], [70742], [-39951], [25262], [16907], [195994], [-118878], [-162810], [-41684], [55554], [128522], [-86940], [21046], [148229], [-158230], [275552], [296046], [-28958], [31525], [160097], [-233995]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2166_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_2166_4_a_h();".
// 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_2166_4_a_h(:prec:=1)
    chi := MakeCharacter_2166_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_2166_4_a_h();".
// 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_2166_4_a_h( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2166_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![19, 1]>,<13,R![38, 1]>],Snew);
    return Vf;
end function;