
// Make newform 1666.2.a.x in Magma, downloaded from the LMFDB on 19 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1666_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_1666_2_a_x();".
// 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_1666_2_a_x();".
// 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 := [2, -4, -6, 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, -1, 1, 0], [0, -4, -1, 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_1666_a();"
function MakeCharacter_1666_a()
    N := 1666;
    order := 1;
    char_gens := [885, 785];
    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_1666_a_Hecke(Kf)
    return MakeCharacter_1666_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, 1, -1], [-2, 1, 0, 0], [0, 0, 0, 0], [-1, 0, -1, 2], [-2, -2, -2, 3], [1, 0, 0, 0], [-2, 0, -2, -1], [1, 1, 1, -3], [-1, 4, 1, -2], [-1, -5, 1, 3], [-6, -1, 0, -1], [-5, 1, -1, 1], [0, -2, 0, 4], [0, 0, -2, -3], [0, 2, 4, -2], [-4, 4, 2, -3], [2, -1, 4, -2], [0, 0, 2, -6], [0, 2, 4, 0], [-6, 2, 0, 0], [-4, 0, -4, -2], [-2, 2, -2, 1], [-2, -6, 2, 5], [-1, 7, -1, -7], [-10, -2, 0, 1], [8, 4, 0, 0], [-7, -2, 1, 2], [0, -7, -2, 1], [3, -5, 1, 3], [1, -3, -1, 10], [-8, -1, 0, 10], [-1, 1, -3, -4], [-4, 1, 4, -8], [-4, -4, 0, 6], [9, 1, -1, 0], [-2, 4, 8, -11], [-7, 0, -3, 2], [-1, -1, -3, 3], [-3, 0, 1, 5], [-6, 6, 2, -2], [-4, 7, 2, -6], [4, 4, 0, 2], [1, 7, -5, -3], [-12, -1, -6, 1], [6, 6, -6, 2], [7, 4, -5, 4], [18, -6, -4, 1], [5, 4, -1, 9], [4, 2, 2, -9], [6, -2, 0, 10], [-6, 0, -4, -10], [-4, -2, 6, -6], [-2, 10, 8, -7], [-2, 4, 2, -8], [7, -3, -1, 12], [-11, 2, -3, -7], [0, 8, 2, -5], [-4, -1, 2, -11], [-3, 3, -1, -4], [2, -7, 6, 6], [-2, 0, 6, -11], [4, 2, -6, 5], [8, -4, 4, 2], [-5, -9, -1, 7], [-17, 6, 1, -2], [0, -4, -2, -8], [-13, 3, 1, 7], [2, 17, -2, -9], [6, -2, 8, 5], [10, 0, 6, 6], [-4, 4, 4, 8], [23, -5, 5, 5], [8, -4, -10, 8], [-4, -11, 4, 3], [4, 4, -2, -6], [2, -2, 10, 0], [8, -13, -6, 2], [-9, 1, -7, 3], [-4, -10, -8, 4], [-3, -6, -1, 3], [-4, 12, -2, -2], [-5, -15, -5, 11], [-10, 2, -10, 5], [7, 3, 1, 5], [-8, 4, 10, 0], [-8, -6, -2, 0], [-2, 4, 12, -8], [-2, 2, -2, 3], [13, -5, -3, 2], [-8, -14, -2, 19], [-18, 8, 6, -10], [-25, -3, -1, 1], [2, -4, 0, -16], [-12, -3, -8, 1], [-3, 11, -5, -7], [-24, -2, -2, 1], [-2, 10, 16, -18], [-16, -4, 2, -7], [-11, 10, -1, -6], [-2, -13, 10, -3], [-2, -8, 2, -8], [14, 10, -6, 11], [22, 10, 2, -14], [-12, -7, 8, -1], [-24, 2, 4, -6], [2, -8, 4, -5], [-12, 0, 0, 21], [11, 1, 3, -16], [3, -5, -5, -5], [38, -2, -2, 0], [-2, -6, -2, 4], [19, 13, -3, -5], [-3, -10, -1, 1], [-24, 2, -2, 4], [25, -3, -1, -1], [-4, -1, -8, 18], [22, -2, 0, -11], [8, 1, -2, 25], [-10, -2, -2, 0], [6, -14, -8, -1], [6, -6, -12, 20], [-2, 1, -4, 12], [6, 1, -6, -11], [2, -3, -14, 14], [6, 0, -2, 8], [13, 8, -1, -6], [-16, -6, -4, 6], [-10, -6, 0, -7], [12, -4, -4, -7], [6, 6, -14, 10], [9, -5, -15, 9], [-23, 1, 9, -13], [22, -4, -12, 16], [-2, -4, 8, -24], [-16, -6, 10, -6], [-16, -10, 10, -1], [3, -2, 1, -5], [4, 0, 2, 11], [1, -9, -17, 17], [32, -3, 0, -12], [18, -13, -4, -3], [-4, -4, -8, -4], [-7, 0, 5, -8], [-10, 4, -2, 1], [-21, -9, 1, 9], [-30, 11, 0, 2], [3, 1, 3, 3], [-14, 6, 4, 5], [3, 1, -5, 22], [17, 2, -13, 4], [3, 19, 7, -15], [8, 22, 4, -26], [14, -2, 10, 6], [5, 2, 13, -18], [1, -1, -7, 23], [-4, -18, 2, 14], [-22, 6, -4, 8], [-16, -2, -4, 6], [-10, 9, 0, 14], [30, 11, 6, -7], [-2, 14, -10, -8], [-10, 8, 12, -2], [-2, -14, -6, 9], [22, -22, -10, 4], [0, 2, 0, -20], [-4, 10, 0, 8], [-13, 8, -17, -9], [-11, 5, -9, 9], [-5, -2, -5, 1], [12, 2, 18, 0], [-6, -10, 6, 15], [6, -18, -2, -8], [-6, -16, -12, 20], [18, 6, -10, 12], [-8, 2, -2, 0], [-7, -18, 1, 2], [-7, -12, 3, -2], [12, 0, 6, -13], [8, -18, -10, 7], [19, 7, 13, -4], [15, -12, -7, -15], [-2, -9, -8, -9], [4, -8, 8, 9], [-27, 9, -5, 8], [4, -12, -16, -5], [-5, 10, -3, -10], [-30, 0, -8, 9], [16, -4, 10, 4], [5, -5, -5, -13], [-7, -5, -7, 27], [-7, -16, -7, 12], [8, 12, -4, -26], [14, 16, 2, -11], [-4, 11, 8, -5], [19, -17, -9, 17], [11, -5, -11, -7], [-33, -14, 5, 4], [7, -13, -13, -9], [-8, 12, 18, -2], [30, -11, 12, 7], [6, 16, -2, -16], [8, -10, 4, 1], [-30, 8, 6, -6], [-26, 4, 4, -5], [-16, -2, -8, 15], [-14, 14, 4, -6], [-14, 16, -8, 0], [1, 11, 11, -25], [8, 10, -16, 9], [-19, -9, -1, 25], [-24, -2, -2, 21], [7, -17, -21, 9], [-10, 16, 4, -3], [-12, 6, 8, 24], [24, -8, 14, 16], [-21, -11, -9, -2], [-8, -16, 0, -11], [-8, 18, 4, 2], [0, -10, -12, 10], [24, 10, 14, -4], [10, 12, 2, -28], [35, 21, 9, -38], [1, 17, 17, -21], [30, 2, 10, 13], [-6, -12, -2, -12], [-22, 12, -4, -14], [-9, -13, -5, 17], [24, -8, -6, 8], [-21, 4, 7, -22], [6, 6, 16, -20], [-6, -3, -6, -20], [24, -4, 20, 2], [0, 16, 0, -20], [22, 22, -2, -11], [0, 20, 2, 1], [21, -1, -23, 21], [4, 5, -22, 11], [9, -10, -3, 2], [-22, 10, 16, 5], [-35, 14, 5, 16], [0, -2, -8, -27], [18, -10, 4, 13], [-18, 12, -4, -10], [16, 22, 14, -18], [9, -5, -9, 27], [42, 6, 10, -4], [-16, -17, -4, 6], [-28, -12, -10, 6], [0, -10, 6, 10], [26, -10, -10, 8], [8, -20, -10, 0], [-9, -7, -5, -14], [-17, -5, -9, 21], [-6, 8, 8, -35], [-2, -22, 8, 14], [17, -6, 11, -12], [33, -8, -1, 9], [21, 6, -7, -15], [32, -2, -4, 14], [27, 1, 11, -17], [14, -28, -8, 12], [15, 18, -9, 15], [36, 9, 14, -10], [15, 25, -9, -7], [8, -16, -6, 11], [-2, 10, -12, 10], [48, 10, -8, 0], [12, -7, 8, 13], [0, -8, 0, 18], [-2, 13, -8, 2], [-10, 0, 0, -20], [-8, 4, 12, -21], [-16, 10, 10, 0], [-14, 12, -2, -18], [28, 24, 10, -31], [-10, -14, -4, -14], [24, -14, 12, 18], [-4, 2, 2, 38], [8, 12, 12, -9], [44, -4, -2, 15], [4, 28, 8, -28], [11, 5, -11, 25], [8, -16, 10, 9], [43, -10, 5, -1], [3, -33, 13, 22], [-30, -8, 8, 16], [-13, -10, -11, 2], [-32, -4, 14, -10], [-2, -10, 10, -17], [3, -28, 9, 28], [-12, -11, -16, 20], [5, 21, 33, -34], [-21, -28, -7, 21], [17, -12, 3, 4], [5, -6, 13, -15], [12, 12, 14, -10], [36, 8, 18, -1], [-20, -7, -16, 4], [-20, 12, 14, 7], [6, 10, 18, -3], [26, 0, -4, -30], [20, 28, 8, -30], [16, 15, 0, -19], [-29, -6, 7, 4], [-30, 0, 12, -16], [-4, -4, 24, -20], [-7, 2, 1, -35], [7, -1, -19, 36], [4, 24, 16, -20], [-11, -3, -21, 17], [3, 18, -11, -4], [-44, -6, -14, 12], [12, 20, -12, -18], [-15, -9, 9, 26], [0, 2, -6, -2], [0, 14, 12, -31], [14, 28, 34, -36], [4, -20, -10, 14], [-31, -9, -9, 20], [-11, -18, -5, 11], [-26, -4, -4, 28], [-38, 8, -6, -5], [-19, 15, 1, -4], [-7, -11, 3, 41], [-4, 4, -4, -11], [-7, 14, -3, -16], [-18, 3, -10, -38], [-5, -5, 19, -31], [9, 26, 3, -32], [-13, -18, 1, 22], [10, 3, 12, 11], [-22, -18, -26, 22], [-8, 10, 4, -35], [-4, -11, 12, 25], [-32, -31, -8, 28], [-32, -24, -14, 38], [-2, -24, 8, 16], [-18, -4, 0, 44], [8, -16, -14, 1], [-20, 4, -10, 0], [24, -2, 10, 26], [21, -10, -11, -13], [-11, 7, -19, -4], [-2, 26, -16, -18], [33, -16, 13, -2], [-41, -14, -5, 17], [18, 6, 8, 0], [32, 6, 32, -10], [3, -12, -17, 23], [16, -8, -8, 13], [-34, -8, 4, 6], [18, -4, 4, 38], [21, 3, 23, -23], [14, -6, 2, 40], [-26, -16, 4, 16], [18, 2, 4, -5], [-38, -29, 6, 14], [-4, -2, 2, -4], [19, -17, 5, 32], [1, 24, -1, -44], [-12, 20, -6, -8], [-12, 10, 2, -32], [-33, -6, -17, -6], [-24, -2, 26, -19], [-13, 23, -17, -20], [-22, -3, 4, -10], [4, -6, 14, 16], [-25, 17, -11, -33], [42, -12, -26, 24], [-2, 15, 30, -36], [-2, -22, -38, 40], [-24, 4, 6, 14], [22, 18, -4, -15], [-5, -10, 21, -30], [-19, -22, 3, 3], [-21, 3, -1, 21], [8, -10, -18, 6], [-22, -12, 20, 0], [-30, 6, -2, -13], [28, 3, 2, 8], [-21, 7, 19, -34], [8, 18, 20, -24], [-22, 14, -4, 22], [3, 6, 9, -3], [0, -10, -4, 10], [-49, 17, 3, 14], [6, 24, 16, 1], [1, 28, 13, -40], [12, -8, -10, -12], [19, 19, -3, -33], [22, 9, -24, 30], [20, 10, -2, -22], [-10, 4, -4, -27], [7, 23, 9, -36], [4, -6, -18, -2], [10, -34, -24, 27], [4, -6, 2, -38], [-28, -5, -12, 11], [74, 0, -6, 6], [28, 15, -14, 0], [-57, -11, 21, -26], [-41, 13, 7, 22], [10, 36, 20, -30], [-8, 10, 0, -21], [-13, 9, -15, -25], [14, 4, -16, 14], [-31, -7, 1, 18], [4, 0, 14, 9], [-44, 1, 4, -22], [-41, -3, 11, -43], [14, 18, 10, 4], [-4, 2, -18, 12], [15, -40, 3, 20], [-5, 17, 25, -36], [-3, 26, 5, -41], [24, 12, 24, -16], [-5, -15, -11, -1], [44, 12, 6, 10], [-2, -26, -20, 35], [4, -6, -16, 32], [-23, 10, 13, -29], [54, -12, 22, 20], [-36, 14, -4, -33], [-70, -2, 12, -16], [38, -24, 30, 23], [-8, 11, 0, 33], [24, -22, 14, -10], [-12, 0, 28, -11], [-5, 15, 9, -47]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1666_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1666_2_a_x();".
// 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_1666_2_a_x(:prec:=4)
    chi := MakeCharacter_1666_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_1666_2_a_x();".
// 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_1666_2_a_x( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1666_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-14, -20, -2, 4, 1]>,<5,R![-14, 4, 18, 8, 1]>],Snew);
    return Vf;
end function;