
// Make newform 2166.2.a.w in Magma, downloaded from the LMFDB on 09 May 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_2_a_w();".
// 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_2_a_w();".
// 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
        poly := [5, 0, -5, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 0], [-3, 0, 1, 0], [0, -3, 0, 1]];
    Rf_basisdens := [1, 1, 1, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
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 := 2;
    raw_aps := [[-1, 0, 0, 0], [1, 0, 0, 0], [2, 1, 0, 0], [2, 0, 1, 0], [4, 0, 2, -1], [0, 0, -2, -2], [0, -2, -2, -2], [0, 0, 0, 0], [2, 0, -2, 2], [-1, -2, 3, 0], [4, -2, 4, 2], [-2, 4, 0, 0], [2, 2, 0, 4], [-2, -2, -2, 4], [-2, -2, -6, 0], [1, 2, -3, 0], [3, -2, 3, 4], [0, -4, -2, 2], [-6, 4, -6, -2], [4, -2, 8, 2], [-4, -4, -3, 0], [-8, 4, 0, -1], [6, -1, 8, -2], [4, 0, 0, -2], [-4, 0, -4, 8], [0, 4, -4, 4], [4, 0, 8, -1], [-2, -6, -1, -2], [-2, -4, -2, 0], [0, 0, -4, -2], [-12, -4, 4, -1], [2, 5, -4, 6], [-8, 0, -12, 2], [4, -4, 4, -4], [6, 3, 0, -4], [-12, 0, -12, -1], [-8, -4, -4, 6], [-2, 2, 0, 4], [6, 4, 4, -4], [12, -2, -1, -2], [-1, 6, -1, 4], [0, -6, -2, -4], [4, 0, -2, -10], [4, 3, 12, 0], [-4, 0, 6, -3], [9, -4, 13, 4], [2, -2, -8, -4], [4, -3, -4, -6], [-2, -6, -7, 2], [-4, 2, -2, 12], [4, 2, 6, 4], [4, -6, 0, 2], [4, 8, 0, -3], [-4, -4, -6, -5], [8, 2, -4, 8], [0, 4, 4, -8], [8, -6, -11, -2], [6, -8, 1, 0], [0, 6, -4, 6], [2, 6, -6, 0], [-4, 10, 8, -2], [8, -2, 1, 2], [-16, -6, 2, -2], [-4, 10, -2, 8], [0, -8, 7, -12], [15, -6, -1, 4], [-10, -4, -4, 0], [4, 4, -4, 4], [-12, -4, 6, -1], [0, -2, -2, 6], [-16, 0, -12, -4], [0, -8, 0, -2], [8, 12, 0, -8], [10, -6, 20, 0], [-16, -8, -10, 10], [-6, 0, 0, -12], [4, -4, 8, -8], [-6, 10, 4, -2], [12, 2, 16, -4], [0, 5, 0, -12], [-14, 11, 4, -2], [-8, 0, -16, -2], [0, 4, -12, 10], [-4, -8, -16, -3], [-12, 0, -8, -15], [2, 7, 12, 6], [-4, 6, 8, -8], [6, 0, 8, 4], [-6, -1, 8, 4], [-13, -4, -13, 0], [-6, 3, 0, -2], [-2, 6, 12, 12], [-4, 9, 0, -10], [-6, 5, -4, -10], [2, 2, -8, -2], [-22, -10, -2, -2], [-8, -6, -5, -6], [28, -2, 6, 6], [-10, 4, -8, 2], [2, 0, 10, -6], [14, 10, 18, -4], [-20, 12, 6, 1], [3, 10, -1, 12], [-8, 4, 0, -2], [-4, 6, 10, -8], [2, 16, 0, -12], [-16, 2, -24, -10], [16, 12, 22, -6], [-22, 6, 0, 6], [4, 11, 4, 4], [-20, -3, -8, -2], [-8, 4, -6, 14], [4, -8, -6, 8], [0, -14, -14, -4], [14, 8, -3, 0], [2, -4, 14, -8], [-10, 4, 2, 0], [-14, 4, -4, 6], [2, 7, -16, 4], [-1, -2, 23, 0], [-8, -14, -10, -2], [16, 0, 8, 4], [-9, 2, 11, 0], [-2, 2, 1, -14], [2, -10, -6, 10], [-4, -16, 2, -5], [36, 4, 10, -2], [-14, 0, -10, 12], [19, 0, 23, 0], [-26, 8, 0, 6], [-4, -6, 10, 10], [2, 2, 10, 10], [-12, -5, -12, -6], [30, -6, 14, 0], [2, -6, -8, -4], [-15, 4, -7, 12], [12, -2, -1, 2], [-14, 6, 0, -12], [-14, -4, -8, -12], [-6, 18, -2, -8], [20, 0, 12, -10], [4, -12, -2, -5], [10, 0, -7, 12], [17, -6, 21, 4], [-14, -16, -2, 10], [-24, 12, -20, -14], [26, -8, 2, 2], [-32, -2, -30, 6], [-14, -4, 12, 0], [6, -6, -4, -12], [8, 8, -10, 6], [2, 18, -8, -10], [2, 8, -8, 2], [-4, -10, 24, -2], [6, 4, -2, -8], [4, -8, -4, -2], [31, -4, 15, 8], [16, -2, 16, 2], [-17, -16, 3, 4], [19, 2, 11, -4], [-6, 1, 4, -6], [20, -6, 2, 14], [-5, 0, -13, -8], [-2, -6, -9, 14], [-10, 8, 26, 2], [8, 0, -10, 8], [24, -1, 12, 14], [18, 6, 4, -10], [-4, -4, 12, 1], [-14, -5, -20, -12], [-10, -14, -7, 18], [6, -18, -2, 2], [-4, -2, 2, -12], [23, 0, 19, -12], [12, 16, 4, -3], [20, 20, 10, -4], [18, -20, 6, 8], [36, -12, 22, 13], [4, 15, 8, 6], [-10, 18, -12, -8], [28, -8, -12, 4], [28, 8, 16, 4], [28, 14, 22, -20], [4, -6, -24, -14], [-6, 30, 4, -4], [28, -12, 18, 13], [-14, 22, -2, -12], [6, -14, 0, 0], [-8, 4, -8, -5], [-16, -4, -18, 2], [16, -9, 4, -12], [-20, 2, -36, -2], [22, -12, 2, 12], [10, -4, -8, -4], [-16, -8, -6, -21], [16, 10, 10, 14], [-12, -4, 1, -8], [2, 22, -16, 0], [-24, -2, -42, 0], [24, 2, 6, -22], [2, 8, -4, -12], [4, 21, -24, 10], [-18, -12, -4, 18], [-24, -4, 4, -15], [-20, 14, -4, 14], [-4, 8, -26, 1], [18, -4, 21, 16], [-24, 0, -12, -8], [22, -2, -4, 10], [-14, 4, -6, 12], [0, -4, 21, -4], [38, -1, 0, -4], [26, 8, -15, 8], [30, 14, 19, -14], [-28, 8, 8, 0], [-16, 0, -24, 11], [10, -8, 27, -4], [10, -8, 14, 2], [0, -26, 24, -6], [48, -8, 4, -4], [44, 4, -2, 6], [-16, -15, -8, 10], [-16, -20, 20, -8], [10, -8, -7, 8], [-14, 6, -19, 14], [0, -2, -14, 4], [18, -10, 20, -20], [-6, 16, -4, 14], [-8, 22, 12, 22], [12, 24, 2, -13], [-22, -2, -18, -6], [-18, -12, -16, 8], [0, 17, -8, -6], [-38, -12, -12, 22], [-6, -10, -12, -26], [-24, -6, 4, 8], [9, 4, -7, -12], [16, 0, 4, -8], [-6, -3, -20, -14], [28, 18, 20, 0], [-37, 6, -1, 16], [-8, -6, 22, 0], [1, -4, 5, -12], [-12, 0, 8, -12], [-14, 12, 12, 14], [4, 4, 22, 0], [-10, -4, -19, 20], [-14, -2, 9, -2], [8, -2, 48, -4], [-16, 0, -40, -8], [-12, -2, 20, -4], [14, 12, -30, 12], [2, -10, -14, -2], [-24, 9, 4, 16], [-8, 8, 26, -9], [22, -1, 24, 2], [-8, 14, 0, -4], [-26, -2, -10, -22], [-13, -22, -37, 0], [-61, -8, -9, 4], [-28, -4, -44, 1], [-10, 2, 7, 10], [-40, -4, -26, -10], [40, 16, 8, -16], [6, -14, -6, -30], [-6, -12, -28, -18], [32, -10, 17, 22], [-24, -18, -14, 16], [-18, 4, -14, 32], [2, -11, 0, 24], [48, 4, 0, -10], [-18, -6, 8, -2], [1, -24, -11, -4], [-27, 16, -23, -8], [-4, 16, -8, 23], [-22, -4, -3, -24], [-52, 10, -16, -10], [4, -6, 20, -22], [24, -4, -8, 8], [38, -7, 40, 18], [-22, -6, -52, -2], [-3, 0, -11, -4], [-18, -2, 14, 0], [4, -18, -8, -16], [-8, 0, 16, 6], [8, -10, 40, 2], [-21, -20, -13, -4], [36, 10, 45, -14], [66, 12, 19, -12], [22, -26, -20, -12], [18, 29, 4, 4], [-24, -14, 16, -6], [-26, 26, 20, 12], [-11, 6, -35, 16], [52, 12, 4, 0], [32, 0, 10, 1], [8, -25, 4, -10], [-28, -12, -10, -19], [39, 14, 27, -8], [-18, -4, -10, 14], [-23, 20, -3, 8], [32, 10, 15, -2], [42, 0, 11, 20], [-55, -18, -11, 12], [-8, 22, 20, 6], [-16, -25, 8, 4], [-14, -10, -9, 10], [48, -10, 44, 14], [26, 8, 12, 26], [-4, 28, 4, 10], [2, 6, -20, -14], [-10, 3, -12, -20], [-22, 10, 2, 32], [2, 10, 6, -12], [-18, 6, -20, 6], [-28, -4, -16, 32], [36, 4, 30, -15], [14, 14, 12, -16], [24, 20, 9, -28], [18, 0, 16, -14], [0, 10, -2, 30], [-1, 24, -13, 8], [30, -16, -16, 4], [48, 9, 24, -14], [-8, -8, -42, -10], [-12, -5, -20, -16], [10, 20, 2, -2], [12, -8, 4, -8], [10, -8, 4, -40], [14, -3, 32, -12], [-18, 2, 0, 28], [4, 6, -23, -2], [71, 0, 7, 0], [44, 6, 20, -14], [36, 12, 10, -22], [-44, 8, 2, 11], [-2, 24, -14, 14], [-4, -2, -12, 22], [-28, 12, -11, -20], [-44, -8, 8, -16], [-22, -14, -30, 10], [0, 16, 4, 34], [10, -12, 16, -4], [-4, 3, 20, -10], [50, -12, 44, 32], [13, -26, -39, -16], [2, -32, -2, 10], [10, 16, 2, 34], [28, 4, -10, -2], [12, -12, -8, 24], [-8, 14, -2, 10], [-20, 13, -20, -8], [32, 12, -6, 5], [24, 31, 12, -10], [-4, -6, -14, 22], [12, 30, 30, 6], [-2, -26, -6, -14], [8, -4, -14, -13], [-20, 4, -20, -10], [62, 18, 32, -20], [24, 10, 48, 4], [-6, 6, -24, 18], [26, -6, -28, 2], [17, 14, -11, -4], [22, 14, 52, 6], [-64, -8, -4, 1], [18, 23, -36, 12], [-12, 12, -36, 3], [-12, 32, -4, -7], [24, 0, 30, -9], [-62, -6, -24, -2], [-62, -14, -20, 8], [13, -10, -7, 32], [42, -28, 9, 4], [-2, 8, 18, 10], [14, 6, 19, -22], [10, 8, 4, -34], [-4, 0, -13, -12], [-4, -14, -6, 14], [12, -4, 64, 0], [3, 38, -9, 8], [22, -40, 26, 16], [3, 32, 15, 16], [0, 6, 36, 4], [40, 22, -4, -8], [32, 2, -2, 10], [39, 28, 27, -24], [-12, 28, -8, -12], [8, -28, 32, -8], [30, -2, 12, 18], [-4, -23, -8, -20], [5, 10, -27, 16], [0, -24, -22, 9], [-14, -8, -42, -28], [0, -24, -2, 24], [-44, 3, -24, -8], [-48, 6, -36, 6], [-20, 10, -8, 42], [16, 20, -14, 8], [34, 9, -4, 16], [2, -14, 32, -26], [38, -2, 34, 16], [20, -8, 28, 1], [6, 10, 26, -14], [15, 10, -29, 16], [-32, -4, -32, 4], [4, -6, -24, 8], [-36, -8, -2, -20], [-66, 12, -10, -14], [60, -10, 12, 10], [-48, -29, -20, 16], [-18, -41, 16, -4], [22, -10, 3, 6], [32, -4, 6, -2], [-15, -8, 53, -8], [-48, -8, 20, -8], [42, -2, 46, -6], [-68, -3, 12, -2], [0, 22, -36, 16], [-24, -8, 18, 16], [-6, -28, 20, -24], [8, 0, 32, 12], [28, 18, 24, -6], [-14, 2, -15, -34], [-8, 40, 12, -5], [52, -10, -19, -14], [4, 20, 60, 12], [-20, -20, -58, -6], [62, -6, 40, 10], [24, 16, -10, 22]];
    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_2_a_w();".
// 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_2_a_w(:prec:=4)
    chi := MakeCharacter_2166_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_2166_2_a_w();".
// 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_2_a_w( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2166_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![1, -12, 19, -8, 1]>,<7,R![1, -3, 1]>,<13,R![16, 16, -24, -4, 1]>,<29,R![-395, -210, -5, 10, 1]>],Snew);
    return Vf;
end function;