
// Make newform 2166.2.a.n in Magma, downloaded from the LMFDB on 24 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_2_a_n();".
// 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_n();".
// 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 := [-1, -3, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-2, 0, 1]];
    Rf_basisdens := [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], [-1, 0, 0], [-2, -1, 0], [-1, 2, -1], [-4, 1, 0], [0, 3, -4], [-4, 0, -1], [0, 0, 0], [-4, -1, -2], [2, 1, 3], [1, -1, -1], [-1, -2, 2], [3, 0, -3], [-1, -3, 0], [-5, 3, 1], [1, -2, -4], [0, 2, 5], [-1, 3, -3], [2, -1, -3], [3, -3, 0], [1, -3, -7], [-3, 0, 5], [1, 11, -6], [-1, 5, -11], [4, -1, 8], [5, 2, -2], [-7, -1, 6], [5, 12, -7], [7, 1, 1], [2, 9, -1], [4, 2, -4], [2, 7, -6], [-1, -6, 8], [-1, 7, -5], [2, -2, 9], [-2, 10, -11], [14, -3, 3], [4, 1, 6], [0, 10, -11], [-5, -1, -6], [5, -4, -8], [16, 2, -1], [-4, 2, 1], [11, -1, 0], [7, 7, -1], [6, 7, -7], [3, 0, -1], [-23, -5, 4], [3, -13, 2], [10, 0, -4], [-2, 9, 1], [4, -2, 2], [13, -2, -8], [1, 9, -14], [-4, 3, 2], [-1, 15, -4], [-13, 4, -3], [-1, -12, 9], [17, 4, -5], [1, 0, 7], [-7, -11, 4], [13, -1, 0], [-5, -18, 9], [5, -1, -2], [5, 12, -9], [-5, -17, 17], [-9, 10, -12], [-2, 8, -7], [-3, 8, -10], [7, 7, 6], [-3, 4, -8], [-8, -4, 12], [-16, 2, -7], [-5, 15, 0], [4, 10, 7], [9, -10, 2], [-17, 3, -5], [-10, -9, 0], [19, -3, -5], [10, 0, -6], [1, -5, -7], [4, 3, 12], [13, -7, 6], [-8, 10, -8], [-3, 11, -8], [-11, -15, 7], [-8, 18, -2], [-14, 15, -10], [-9, 15, -21], [3, -13, -6], [-6, 5, 5], [-12, -3, 0], [7, -7, 14], [1, -8, 8], [-15, 12, -5], [-7, -26, 21], [1, 3, -2], [-14, -5, -7], [-11, 9, -15], [19, 8, -5], [-5, 9, 15], [-11, 4, -19], [5, -5, 6], [5, 18, -4], [-17, 13, -12], [-22, -7, 11], [-16, -10, 19], [-11, -3, 4], [6, 10, 13], [-17, -6, -3], [-20, -11, 7], [2, -11, 16], [-12, 16, -20], [8, -8, 10], [-5, 12, -7], [0, -10, -13], [-3, -10, 6], [-17, 7, 8], [13, -10, 21], [17, -22, -5], [22, -4, 17], [15, -8, 3], [-20, 1, -10], [34, -4, 0], [-1, 2, 20], [17, 1, 0], [30, -12, -5], [-6, 2, -7], [15, 0, 7], [-1, 23, -22], [-14, 2, -2], [9, 12, -3], [14, -14, -4], [32, -6, 6], [9, -7, -16], [-3, 7, -13], [5, -12, 14], [1, 10, -11], [17, -11, -3], [-14, -1, 9], [7, -5, 1], [33, -3, -3], [-19, -23, 19], [22, 13, -13], [9, -8, 6], [25, 14, -6], [22, 20, -2], [-26, 6, 1], [-26, -1, 4], [14, 33, -19], [2, 24, -29], [2, 0, -19], [-27, -12, 4], [-8, 23, -9], [25, -16, 14], [15, -6, -21], [0, -2, -31], [7, 18, 7], [1, 4, -15], [-4, 23, -17], [-11, 1, -22], [18, -12, 24], [-5, 8, 16], [-2, 18, -20], [5, -26, 15], [20, 17, -11], [7, 26, -4], [24, -6, 19], [37, -6, -3], [17, 7, 12], [18, 3, 24], [9, 15, -25], [25, -12, 40], [19, -13, 10], [0, -26, 18], [9, 3, 27], [-7, 19, -8], [29, 1, -18], [15, -8, 0], [1, 0, -16], [-25, 10, -11], [31, 16, -1], [15, 2, 7], [-9, 9, 21], [-9, -18, 0], [23, 8, 1], [-14, 10, -8], [-16, 3, -20], [7, -30, 9], [7, 3, 31], [-9, 4, 21], [-4, 4, -30], [-10, -18, -8], [16, -24, 19], [-15, -13, -4], [-8, -3, 19], [-37, 15, 12], [3, 11, -9], [13, -26, 32], [12, 1, -11], [32, -21, 8], [-11, -22, 11], [-6, -27, 23], [16, 14, 5], [-25, -34, 19], [-46, 10, -24], [-25, 12, -12], [-2, -27, 1], [-3, -10, -1], [-10, -14, -13], [10, -3, 5], [-10, 14, -20], [1, -38, 18], [-5, 23, -9], [-5, 4, -19], [2, 7, -7], [-4, 10, -23], [-38, -8, -4], [-28, -11, -9], [23, 24, -31], [-3, 2, 7], [15, 43, -27], [13, -3, 10], [10, 22, 9], [3, -21, 33], [-9, 4, 8], [-19, 9, 23], [-5, -28, 27], [-23, 17, -37], [41, -4, 7], [-34, 12, 0], [11, -6, 7], [34, 19, 4], [-18, 19, 10], [20, -11, 4], [25, 21, -11], [-33, 3, -23], [20, 0, -10], [3, 8, 11], [-15, -8, 19], [12, -8, -5], [-13, 3, -30], [11, -7, 29], [-19, -6, 7], [50, 1, -3], [21, 0, -27], [41, -17, 19], [4, -13, 24], [37, 17, -10], [-15, -2, 10], [0, -17, 23], [-49, 13, 0], [-12, 32, -19], [4, -2, -23], [20, 20, 4], [16, 3, -20], [-2, -21, 8], [-13, -3, 10], [-57, 7, -17], [16, 32, -11], [-30, 3, -13], [-24, 16, -26], [4, -4, 40], [16, 18, -18], [-24, 5, -16], [-13, -31, 12], [37, -5, -10], [-29, 23, -3], [25, -15, 12], [15, 3, 15], [-8, 19, 10], [-8, -8, -17], [14, 18, -27], [20, -7, -13], [-7, 14, 0], [16, -12, 47], [-19, 16, -12], [-40, 25, 11], [14, 22, -13], [13, -41, 8], [12, -10, 23], [-22, 7, -32], [-34, -9, -1], [-10, 27, 25], [2, 13, -8], [-17, 1, -25], [36, -13, 36], [0, -23, 46], [11, -7, 23], [9, 40, -32], [2, 18, 5], [-19, -13, 1], [-33, 15, -36], [-32, 17, -18], [39, 26, -11], [24, -17, 7], [-26, -33, 9], [-29, -18, 25], [-37, -34, 22], [-57, -19, 12], [-22, -2, 28], [-41, -4, 12], [-25, -19, 35], [31, 17, -12], [35, 3, 3], [-22, -1, -33], [-28, -20, 15], [-2, -32, 31], [4, -1, -6], [48, 3, -8], [24, -24, -18], [-38, -8, -10], [34, -10, -3], [-36, -20, 18], [26, 4, -21], [4, 22, 10], [6, -32, -8], [-26, -46, 27], [20, 28, -2], [6, 13, 4], [-17, 20, 29], [48, 15, -5], [-30, 16, -14], [46, -6, 21], [42, 25, -26], [-14, -3, 18], [-12, 37, -39], [-60, -1, 4], [-28, -28, 28], [13, -45, 31], [-10, -28, -1], [-11, 32, -9], [-28, -29, 7], [21, 7, 4], [20, 9, -9], [-37, -18, 17], [11, -17, -40], [-35, 25, 5], [17, -7, -4], [21, -11, 8], [31, -13, -13], [57, -16, 5], [13, -12, 13], [-5, -11, 7], [14, 19, 6], [-28, 17, -8], [20, 3, -15], [79, 0, 6], [21, -13, 23], [0, 6, 45], [8, 37, -22], [15, -15, 5], [25, 26, -39], [25, 26, -19], [-20, -43, 32], [-39, 18, 0], [8, -16, 10], [26, -17, 57], [11, -11, 33], [-1, 28, -21], [59, 4, 10], [12, -9, 18], [-10, 30, -40], [26, 45, -31], [20, -33, 12], [7, -49, 20], [17, -3, -4], [-3, 17, -29], [36, 3, -7], [-2, 2, 24], [16, -13, -4], [-1, -14, 0], [-20, 28, 35], [-3, 27, -2], [57, 10, -7], [35, 27, -37], [-20, 39, -5], [11, 1, 10], [-15, 33, -9], [-49, 17, -15], [19, -29, -1], [5, -39, 20], [-25, 33, -63], [16, -35, -31], [60, 8, 1], [15, 12, -24], [60, -18, -2], [-19, 5, 2], [50, -11, 4], [19, -18, 61], [25, 37, -2], [-1, -25, 4], [-51, -32, 10], [4, 25, -30], [42, 25, -26], [7, -53, 7], [17, -3, 22], [23, 1, -30], [5, 24, -8], [66, -9, 0], [-7, 6, -11], [-9, 9, -27], [1, 10, -26], [-7, 21, -19], [24, -35, 40], [-4, 49, -62], [-21, -26, -10], [-63, -16, 8], [31, 35, 2], [-68, 19, -13], [-14, -32, 40], [6, 16, 16], [29, -15, 47], [5, -53, 19], [-7, 16, -14], [-48, -2, 1], [-11, -8, -10], [37, 11, 26], [-53, 19, 23], [-26, 24, 7], [33, 34, -29], [16, -51, 18], [23, -28, 55], [-51, -3, -15], [-7, -7, 45], [41, -33, -25], [14, -21, 38], [-56, 3, -32], [42, -16, 39], [17, 49, -24]];
    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_n();".
// 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_n(:prec:=3)
    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_n();".
// 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_n( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2166_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![3, 9, 6, 1]>,<7,R![1, -6, 3, 1]>,<13,R![-19, -39, 0, 1]>,<29,R![51, -27, -6, 1]>],Snew);
    return Vf;
end function;