
// Make newform 2166.2.a.p in Magma, downloaded from the LMFDB on 16 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_p();".
// 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_p();".
// 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], [0, 3, -2], [-2, -1, 2], [-2, 0, 1], [0, 0, 0], [2, -3, 6], [-2, -1, 1], [-5, -1, 1], [1, 4, 0], [-3, 2, -3], [3, 5, -2], [-3, -1, -3], [-9, -2, 0], [-6, -4, -1], [1, 1, -3], [-4, 5, -11], [-7, -5, 2], [5, 1, -3], [-5, -4, 5], [-1, -1, 4], [-1, -7, 7], [-4, 7, -8], [-1, -10, 2], [1, 5, 0], [-3, 4, 3], [-11, 3, 1], [-2, -1, -1], [-8, 2, 0], [-2, 9, 4], [3, 0, -10], [-9, 1, 1], [-2, -2, 11], [-8, 0, -3], [-6, -3, 5], [2, 1, 10], [8, 8, -5], [5, 9, -10], [-5, 2, 0], [6, 2, -9], [0, -12, -1], [7, -5, 8], [1, 5, -11], [8, 13, -5], [5, -6, 5], [-15, 1, -6], [9, 3, 4], [-2, 4, 0], [-10, 3, -7], [-16, -2, -6], [17, -2, 4], [-9, 9, -8], [-6, -3, -4], [5, 1, -6], [1, -8, 11], [-11, 8, -7], [1, -10, 17], [-9, -6, -5], [5, -3, 2], [15, -7, 8], [17, -4, 7], [3, -3, 8], [-7, 8, 11], [-19, 5, -5], [1, -14, 10], [2, -12, 9], [15, 10, -6], [-9, -7, 10], [-3, -6, 6], [8, 4, -12], [22, -4, 5], [3, -11, 0], [-16, 8, 7], [13, 18, -16], [5, -3, 5], [0, -9, -2], [-5, 9, -7], [10, -12, 2], [13, -15, 13], [-2, 15, -18], [-5, 11, -12], [24, -2, 0], [1, 1, 10], [-7, -21, 7], [8, 14, -10], [22, 7, -6], [-19, -3, -3], [7, -15, 0], [0, -15, 17], [6, -5, 0], [-9, -9, 4], [3, 2, 12], [19, -2, 13], [1, 16, -7], [11, -7, 10], [2, -3, -11], [21, 19, -9], [-9, 6, -11], [-21, 7, -7], [-13, 16, -1], [-1, 7, 0], [5, 8, -22], [11, 1, 10], [26, 1, -5], [-2, 4, -7], [-29, -11, 4], [22, 0, 7], [23, -2, 1], [22, 3, -3], [4, 1, 10], [-28, 4, 0], [16, -12, -6], [-11, -16, 13], [-6, -2, 1], [7, -6, 0], [13, 5, -14], [-5, -14, -1], [5, -6, 1], [0, -12, -11], [-17, 4, -5], [-20, 15, -10], [26, 0, 0], [-7, 10, 18], [-17, -1, 16], [-4, 16, 3], [14, 0, -5], [13, -12, -9], [-17, 5, 6], [18, -6, -2], [5, -10, 21], [-18, -10, 4], [-16, 6, -2], [19, 5, 0], [25, 3, 3], [19, 12, -14], [7, 0, 11], [-9, 11, 11], [14, 5, 1], [-25, 1, 3], [-17, 19, 11], [-13, -7, 5], [-8, -15, 3], [19, 2, 12], [-19, 6, -8], [-22, 0, 2], [8, 34, -33], [4, 19, 4], [0, 9, -21], [12, -24, 7], [-16, 8, -17], [11, -8, 14], [10, -21, -19], [-1, -4, 12], [-9, 0, 15], [-2, 0, -7], [13, -8, 15], [1, 0, -7], [0, 5, 9], [-5, 1, 20], [-34, -12, 4], [-17, 26, 0], [6, -2, 0], [3, 0, -1], [10, -27, 7], [-33, 8, -24], [-18, -22, 7], [5, -10, 5], [-25, -11, 0], [10, 1, -10], [27, 29, -17], [21, 12, -18], [-9, 11, 18], [0, -18, 18], [9, 31, -11], [13, 7, -6], [-5, -29, 2], [-1, 2, -4], [3, 10, -22], [1, -18, 33], [3, 16, -7], [11, 8, -7], [-29, -23, 15], [3, -10, -22], [9, -4, -25], [6, -10, 4], [-34, 3, -12], [-13, -14, -7], [5, 3, 23], [3, -32, 29], [24, -24, 10], [-18, 22, 8], [-4, -20, 9], [23, -29, -10], [-36, 3, -9], [-5, -29, 0], [1, 25, -25], [21, -4, -18], [-14, 13, -13], [-44, 17, 0], [-9, 34, -13], [46, -7, 1], [-8, -10, 5], [-9, -6, 25], [18, 10, -4], [-17, 22, -24], [16, -3, -11], [15, -28, 11], [6, 20, -17], [2, -7, -31], [14, 6, -12], [-19, 8, -38], [23, -19, 31], [11, -2, 9], [-34, -5, -3], [2, 32, -11], [-34, 8, -8], [-14, 13, -39], [-27, 12, 15], [5, -32, 33], [13, 3, 3], [-17, 1, 10], [-20, 8, 13], [23, -7, -3], [-23, 6, -14], [-3, -7, 25], [-13, 14, 19], [15, 21, 5], [-19, 12, -7], [-6, 4, 4], [-7, 16, 5], [-10, -29, 30], [48, -7, 24], [-18, 1, 20], [11, -15, 41], [-21, -25, 5], [-20, 16, -6], [23, -24, -3], [1, -6, 19], [14, 18, -27], [-33, -9, -4], [9, 17, 19], [1, 44, -19], [-14, 11, -27], [-3, 22, -11], [-33, -17, 21], [-40, 9, -14], [9, 9, 8], [-3, 38, -16], [-12, 7, -27], [45, -19, -12], [-4, 34, 7], [-4, 2, 9], [-4, 8, -16], [-4, -7, -30], [20, -21, 2], [-9, -27, 32], [9, 17, 17], [24, 16, -15], [-4, 1, 25], [-20, -36, 22], [-24, -8, -8], [20, 14, -26], [2, -13, 10], [-5, -3, -22], [-29, 21, -14], [-57, -5, 7], [-3, 37, -18], [1, -23, 5], [14, -17, 24], [24, 14, -9], [-2, -26, 41], [14, -9, 21], [-7, 18, 16], [30, -10, -13], [-41, -6, -8], [-4, 1, -35], [-2, 14, -5], [15, 35, -34], [20, -24, 53], [-2, 27, 6], [-32, 11, -15], [-2, -17, -13], [-4, -39, 4], [-55, 9, 11], [24, 11, 8], [12, -13, -22], [-23, 5, 5], [-15, 14, 10], [22, -22, 39], [5, 17, -23], [5, -45, 32], [-44, -17, 16], [11, -12, -5], [-36, 5, -27], [-8, 5, -25], [-15, -6, -17], [-35, -48, 26], [-13, -47, 30], [-18, 26, 16], [-27, 32, 4], [9, -39, 5], [-19, 25, 14], [-9, -3, -39], [50, 15, -17], [22, -14, -7], [-8, 24, 3], [-6, -31, 2], [-34, -13, -10], [-28, -4, 6], [-14, -8, 26], [-8, -6, 35], [52, -20, -10], [-10, -34, 1], [24, -18, -18], [42, 4, 4], [6, 20, 9], [16, 16, -18], [16, -21, -6], [33, -22, 39], [20, -1, 43], [14, 8, 22], [-4, -40, 9], [0, 35, -16], [2, 45, -22], [-34, 19, -11], [-18, -25, 24], [32, 32, -28], [-29, -19, 37], [-32, -52, 35], [51, -20, 1], [-2, 25, -39], [-13, -1, -30], [-10, 11, -41], [-33, -46, 27], [-9, 29, 8], [-3, -11, -41], [13, 49, -36], [-11, -1, -10], [-15, 25, -37], [7, 50, -15], [11, 52, -45], [-15, 1, -3], [26, 5, 30], [-8, -5, -6], [20, -1, -5], [17, 12, 20], [31, 7, -5], [16, -22, -33], [66, -7, 6], [-5, 45, -3], [-5, -2, 3], [-61, 10, -15], [2, 1, 14], [-35, -4, -2], [24, -28, 18], [-36, 19, -11], [-37, 17, 27], [3, -2, 19], [-7, 22, 8], [14, 29, -16], [2, -10, -20], [20, 5, -39], [50, -17, -8], [-17, 11, -40], [-49, -21, -4], [-45, 9, -3], [36, -33, 13], [50, 18, -16], [-6, -53, 16], [15, -22, 30], [0, -12, 1], [-15, -11, 8], [-23, -4, -5], [-37, -15, -5], [2, 31, -11], [7, 21, -46], [-27, -3, -39], [7, -27, -23], [-27, -15, 33], [19, 9, -40], [37, 9, 15], [32, -21, 41], [4, -14, -7], [-17, 0, 6], [8, -2, 2], [13, -41, 58], [48, -27, -16], [27, -32, 5], [-43, -27, -2], [37, -3, 12], [-29, 26, 18], [-30, 33, 14], [72, -13, 26], [-5, 3, 27], [-3, -17, -16], [29, -27, 10], [-33, -44, 26], [30, 49, -44], [5, -24, 55], [3, 21, -13], [5, 20, -14], [-7, 9, 15], [-56, 19, 8], [4, -27, 20], [-23, 32, -4], [17, -50, 26], [-29, -25, -8], [22, -29, 39], [26, -4, 32], [18, -44, 28], [41, 15, -21], [17, 13, -11], [25, 20, -22], [12, 22, 7], [-39, 0, 0], [-11, 33, 36], [7, -21, 37], [38, -10, 41], [27, 2, -19], [14, 13, -16], [-13, -6, 39], [13, -29, 45], [-3, -31, 9], [-13, -47, 45], [22, -11, 28], [-42, 13, 30], [-58, 14, 11], [-5, -61, 10]];
    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_p();".
// 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_p(: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_p();".
// 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_p( : 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, 9, 6, 1]>,<7,R![57, -18, -3, 1]>,<13,R![-1, 3, 6, 1]>,<29,R![1, 9, 6, 1]>],Snew);
    return Vf;
end function;