
// Make newform 2166.2.a.u in Magma, downloaded from the LMFDB on 23 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_u();".
// 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_u();".
// 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], [0, -1, -1], [-1, -1, 0], [0, -1, 1], [2, -1, 1], [2, 1, 2], [0, 0, 0], [2, 5, -3], [2, -2, -3], [3, 0, -3], [3, 6, -2], [3, -3, 4], [5, 1, -3], [1, 0, 3], [-1, 6, -8], [-4, -3, 6], [-11, 0, -1], [4, -4, 3], [5, 7, -1], [1, 2, 5], [5, -5, 10], [9, -1, 3], [-5, -4, 7], [-4, 1, -1], [-1, -8, 4], [1, 1, -7], [-7, 7, -4], [13, -2, 1], [4, 0, 5], [-8, -6, 2], [2, -5, 3], [1, 8, 0], [-5, 4, -3], [-8, 5, -2], [-8, 5, -14], [-8, -2, 5], [-16, 5, -1], [4, 5, 0], [-1, -3, 5], [13, -8, 0], [-4, -13, 4], [0, 9, -4], [-5, 5, -1], [1, 4, -11], [-10, -2, 7], [11, 3, 0], [1, 3, 1], [3, -5, 3], [-14, 4, -8], [-8, -2, 5], [-4, -8, -6], [5, 2, -10], [13, -3, 1], [-4, 15, -7], [1, -7, -3], [15, -5, -6], [3, 3, 10], [-11, -9, 6], [5, 3, 0], [-1, -11, 5], [1, -5, 13], [-13, 3, 2], [-9, -5, -3], [-3, 13, -20], [5, -2, -3], [9, 20, -16], [-2, -5, 16], [5, 2, 0], [5, -5, 5], [27, 6, -6], [8, -4, 4], [-6, 5, -6], [13, -7, 17], [6, 7, 0], [9, 2, -2], [1, 8, -11], [0, 7, -11], [-5, 2, 15], [-10, 14, -8], [-7, 4, 15], [-10, 7, 13], [-1, 13, -13], [0, 10, 10], [-23, 3, 5], [17, 8, -11], [-12, -4, -10], [-14, -25, 19], [-15, 8, -7], [3, 9, -3], [26, -6, 9], [18, -11, 7], [-13, 11, -9], [-11, -16, 12], [5, -9, 24], [1, 3, 12], [1, 21, -27], [4, 12, -13], [-3, -8, -13], [7, 19, -18], [-17, -14, 7], [-15, -1, -2], [5, -17, 15], [3, -16, 8], [21, 9, -11], [-26, -4, -7], [8, 19, -22], [11, -3, -11], [2, -11, 8], [-5, -19, 6], [-8, -10, 17], [-16, -7, -1], [0, -8, -8], [0, 6, -16], [3, -13, 6], [6, -21, 16], [-29, 2, -4], [-7, 13, -15], [5, 5, 16], [13, 19, -6], [-10, 7, -2], [-1, 25, -4], [-14, -9, 1], [30, -8, 4], [5, -8, -4], [13, -7, -13], [6, -19, -6], [2, -19, 24], [11, -7, -10], [-11, -17, 1], [22, 4, -2], [21, 5, -10], [6, -2, 2], [-8, 20, -14], [3, -27, 1], [1, -14, -1], [-5, 22, -10], [-3, -21, -2], [-21, -4, -5], [12, 12, -21], [-5, 2, 25], [-25, -16, 19], [-13, -26, 11], [10, -20, 29], [13, 0, 0], [13, -6, -2], [-22, 14, -8], [-8, -31, 12], [18, -3, -7], [12, -16, -11], [2, 17, -18], [-4, -13, 22], [3, -18, 18], [-2, 4, 11], [-9, 8, -6], [11, 21, -4], [14, 5, -6], [3, -31, 26], [-7, -21, 24], [12, 4, -1], [13, 13, -23], [6, -20, 28], [17, 24, -10], [-14, -10, -18], [-23, 5, -6], [34, 4, -7], [1, 10, 12], [20, -17, 28], [17, -3, 18], [-3, -25, 17], [18, 1, -5], [27, 6, -5], [-7, 22, -12], [11, -21, 31], [-8, 4, -6], [-19, 28, -3], [7, -5, 23], [-7, 3, -21], [13, -16, 30], [-3, 10, -24], [-17, 1, 4], [3, 13, -16], [31, 13, -12], [-25, 0, 7], [23, 4, -14], [-5, 15, -14], [30, -6, -10], [24, -11, 17], [7, -27, 2], [-27, 14, -21], [19, -5, -4], [-24, -18, 0], [-6, 2, 6], [-26, -11, -4], [37, -7, 27], [10, 36, -23], [3, 21, -25], [-11, 10, -5], [11, -28, 8], [10, -28, -15], [2, -5, 27], [-19, -1, -4], [0, -18, -15], [-16, 13, -14], [7, -29, 14], [-6, -38, 10], [33, -16, -6], [-6, 6, -27], [-7, -11, -6], [20, -25, -12], [22, 6, -19], [-38, 10, -30], [31, -4, -12], [-33, -6, 11], [7, 5, 22], [2, -8, -21], [10, 41, -14], [-2, -4, 12], [-18, -2, -19], [-13, 11, 14], [-15, -19, 16], [1, -6, 5], [19, 7, -3], [4, -3, 2], [-13, 24, -5], [3, -22, 36], [-11, -6, -5], [27, 11, 10], [-17, -10, 15], [1, -31, 36], [-10, -4, 20], [15, 1, -22], [-8, 7, -5], [-10, 19, -3], [-32, 3, -5], [15, -18, -19], [39, -4, 3], [-16, 22, 12], [-21, -7, -8], [-3, -41, 2], [8, 25, 4], [-7, -27, -5], [1, -28, -13], [17, -3, 8], [-16, -10, -7], [1, 29, -14], [23, 20, 5], [8, -19, -17], [19, -1, 17], [13, 30, -26], [-6, -12, 11], [-3, -31, 5], [12, -29, 46], [-12, -7, -6], [-16, 8, -28], [32, -11, 15], [-4, -17, -3], [-7, 35, -15], [-3, 36, 9], [8, 19, -20], [-14, 8, 7], [-32, -6, 4], [-16, 8, 16], [-4, 12, -6], [-28, -1, 15], [25, -11, 25], [25, -19, 17], [-17, -2, 29], [43, 17, -3], [-3, 20, -7], [10, -3, -27], [-22, -11, 6], [18, 21, 2], [36, -6, -7], [9, 30, 2], [26, -23, 4], [-51, -28, 16], [-10, -14, -31], [-30, -25, 10], [-3, 33, -37], [-12, -13, -16], [24, 31, -1], [4, 44, -45], [16, -26, -1], [2, -45, 31], [-27, 24, 21], [8, -43, -1], [-26, 3, -15], [5, 18, -5], [3, -2, -42], [-52, -3, -10], [13, -12, -25], [-7, 3, -41], [-4, -35, 21], [-1, -21, 12], [36, 16, -17], [-22, 2, -1], [-25, -3, 24], [-37, 0, -18], [-51, 1, 5], [10, -22, 38], [13, 32, -34], [-3, -26, 7], [-9, -5, 29], [-21, -40, 13], [-10, -34, 23], [-12, -23, -16], [-2, 25, -14], [6, -3, -15], [-10, 23, -3], [8, -18, 36], [66, -6, 4], [-48, 5, -16], [-24, -18, 28], [22, 21, -2], [-12, -4, 30], [-42, 20, -24], [18, -33, 48], [28, 10, 16], [14, -25, 35], [31, 13, -8], [24, 2, -5], [-54, 18, -12], [16, -3, 10], [-22, 17, -9], [-30, 5, -27], [30, 10, -7], [28, 9, 5], [4, 48, -8], [-13, -48, 41], [-22, -23, 14], [-15, -35, 38], [-36, -8, 19], [-11, 25, 35], [18, -16, -19], [-9, 5, 14], [69, -3, 9], [25, 4, 27], [-7, 23, -23], [5, 1, 23], [-79, 2, -1], [-3, 33, -8], [-11, 43, -22], [13, -10, 27], [16, -3, -9], [8, 19, -27], [-14, 2, -13], [-35, 16, -38], [-61, 6, -1], [22, 25, -14], [-36, 29, 3], [3, -6, 1], [-27, 25, 4], [-59, 9, -20], [38, 37, -17], [-13, -28, 28], [4, 6, -32], [2, 20, -17], [-29, -8, 19], [27, -13, 38], [-1, 4, 8], [40, 11, -15], [22, -42, -14], [-6, -10, 13], [-4, 33, -27], [-21, -11, 7], [-3, 25, -29], [35, 34, -25], [48, -28, -13], [-34, -2, 26], [4, -11, -11], [-17, 24, -18], [26, 9, -12], [-7, 25, -3], [-35, 31, 20], [23, -10, -1], [-46, 4, 11], [-25, 29, 29], [25, -14, -29], [39, 42, -15], [25, 24, 13], [-13, -11, -31], [-23, 28, -1], [70, 2, 1], [-46, -21, 22], [31, -26, 8], [-28, 16, -2], [-49, 13, -21], [-30, 23, -65], [-41, -25, 24], [21, 1, 25], [43, -29, 5], [-31, -14, -12], [-68, 1, -9], [16, -25, 51], [27, 10, -37], [9, 31, 15], [21, 7, -7], [59, -10, 6], [-24, -5, -5], [-3, 19, 12], [35, -8, -17], [19, 32, -20], [-55, 20, -9], [-2, -39, 41], [-10, -21, -3], [-21, 14, 38], [11, -14, -30], [49, -3, -9], [36, 10, 27], [10, -16, 32], [6, 0, 32], [21, 4, -35], [25, 60, -43], [-11, -54, 28], [26, 13, -10], [33, -28, -28], [17, 21, 13], [15, -32, 29], [6, 45, -58], [29, -9, -4], [62, 19, -17], [-1, -29, -10], [17, -38, 37], [65, 2, -19], [-53, 0, -35], [-10, 23, -29], [56, 9, -15], [-28, 37, -10], [-1, 27, 11]];
    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_u();".
// 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_u(: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_u();".
// 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_u( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2166_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![9, -9, 0, 1]>,<7,R![-1, 0, 3, 1]>,<13,R![-3, 9, -6, 1]>,<29,R![213, -45, -6, 1]>],Snew);
    return Vf;
end function;