
// Make newform 1166.2.a.d in Magma, downloaded from the LMFDB on 14 September 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_1166_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_1166_2_a_d();".
// 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_1166_2_a_d();".
// 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_1166_a();"
function MakeCharacter_1166_a()
    N := 1166;
    order := 1;
    char_gens := [849, 903];
    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_1166_a_Hecke( : Kf := Kf)
    return MakeCharacter_1166_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]];
    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 := 2;
    raw_aps := [[1], [-1], [-3], [4], [1], [1], [4], [0], [-8], [10], [4], [4], [3], [4], [9], [1], [0], [8], [15], [-8], [-10], [-13], [6], [-14], [14], [0], [-8], [-9], [0], [-11], [7], [-11], [-2], [16], [-11], [-8], [-15], [12], [-16], [6], [-24], [14], [4], [9], [-13], [11], [-1], [29], [-3], [0], [-21], [-1], [-16], [20], [0], [-3], [8], [2], [-2], [24], [14], [-27], [31], [25], [-10], [18], [-22], [17], [-9], [-32], [-16], [-3], [-19], [-34], [3], [30], [3], [17], [0], [6], [3], [-38], [30], [-6], [-6], [15], [-21], [7], [21], [-14], [-36], [25], [-35], [-2], [16], [12], [21], [-41], [8], [2], [-39], [-28], [28], [37], [-10], [43], [24], [-16], [-39], [10], [-12], [10], [2], [0], [34], [2], [-42], [-12], [2], [46], [26], [-18], [26], [28], [-44], [-18], [25], [36], [28], [-22], [-20], [-16], [27], [34], [3], [-2], [19], [-38], [-49], [46], [29], [0], [-13], [-48], [-2], [-27], [40], [-36], [-14], [1], [-13], [-54], [-51], [15], [-52], [15], [13], [-9], [-28], [19], [24], [-48], [-22], [-12], [-22], [-21], [-16], [22], [-57], [36], [-42], [-9], [18], [-10], [54], [-35], [41], [44], [-25], [-49], [-46], [-22], [-14], [-44], [-16], [-6], [-43], [-30], [54], [-14], [-27], [36], [41], [-2], [-14], [-6], [-1], [-32], [-53], [52], [-6], [52], [-33], [15], [54], [-42], [4], [-9], [-48], [-33], [-68], [2], [62], [-36], [24], [-15], [-43], [14], [55], [-70], [42], [-3], [50], [22], [1], [-42], [30], [24], [44], [46], [29], [-35], [-14], [22], [-38], [-7], [28], [66], [36], [14], [28], [17], [28], [-49], [-30], [-63], [18], [-35], [60], [12], [31], [48], [40], [76], [32], [69], [5], [11], [-48], [-57], [1], [-37], [-40], [-50], [-57], [-76], [-38], [-27], [22], [-14], [-5], [15], [17], [20], [-46], [44], [50], [19], [-30], [-52], [39], [-25], [59], [-61], [2], [-24], [16], [18], [-1], [-73], [66], [84], [-66], [-58], [49], [-30], [-15], [31], [-46], [-20], [-71], [-29], [56], [54], [31], [32], [-36], [56], [-24], [-4], [46], [67], [-85], [22], [-28], [5], [9], [18], [19], [-78], [-45], [45], [12], [-64], [17], [4], [42], [77], [75], [-26], [78], [26], [-44], [57], [-27], [-17], [-6], [80], [58], [-51], [-10], [-38], [32], [32], [36], [33], [48], [-74], [57], [-48], [10], [36], [68], [16], [22], [19], [-2], [12], [25], [-27], [-13], [-33], [43], [-96], [-26], [-64], [44], [16], [18], [-50], [-25], [16], [-74], [-24], [-24], [3], [50], [6], [-69], [-23], [76], [-93], [21], [-2], [16], [-49], [-76], [42], [50], [16], [80], [-76], [68], [-50], [100], [-72], [10], [30], [-22], [57], [34], [6], [-82], [8], [62], [67], [25], [-30], [-34], [-16], [-32], [3], [19], [13], [29], [74], [14], [20], [54], [-79], [-64], [40], [48], [77], [35], [-72], [97], [-76], [-1], [-72]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1166_a_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1166_2_a_d();".
// 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_1166_2_a_d(:prec:=1)
    chi := MakeCharacter_1166_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 2));
    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_1166_2_a_d();".
// 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_1166_2_a_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1166_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![1, 1]>,<7,R![-4, 1]>],Snew);
    return Vf;
end function;