
// Make newform 1334.2.a.e in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1334_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_1334_2_a_e();".
// 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_1334_2_a_e();".
// 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_1334_a();"
function MakeCharacter_1334_a()
    N := 1334;
    order := 1;
    char_gens := [465, 553];
    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_1334_a_Hecke(Kf)
    return MakeCharacter_1334_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, -1], [0, -1, -1, 2], [-1, 0, 1, 0], [-1, 1, 0, -2], [-2, 2, 1, -1], [-3, 0, 1, -1], [-4, 2, 0, -1], [1, 0, 0, 0], [-1, 0, 0, 0], [-3, -1, -2, 0], [-1, 0, -3, 2], [-3, 2, 1, 2], [1, -1, 2, -4], [-7, -1, 0, -2], [4, -1, -1, 4], [1, -5, 1, 1], [-1, -4, 1, 2], [0, -5, -4, 4], [0, 4, 2, 0], [-6, 1, -4, -1], [3, -4, 2, -1], [-3, 3, -1, 1], [4, 7, 0, -6], [1, -1, -3, 5], [-3, -4, -3, 5], [-1, -5, 1, -3], [-4, 2, 6, -4], [5, 6, 4, -10], [4, 2, -2, -4], [0, 0, -4, -2], [-3, -7, 5, -2], [6, 7, -4, 2], [0, 1, 4, -1], [6, 3, 5, -6], [-5, -1, -1, 11], [5, -4, 1, 8], [3, 2, -8, 7], [-3, -1, 5, 3], [0, -2, -2, -12], [-2, -3, 8, -3], [-7, 6, 2, 2], [-6, -4, 0, 2], [3, -1, 9, -1], [2, 2, 2, -2], [-10, -4, 4, -8], [-1, 4, 0, 3], [-7, 9, -7, -7], [11, 2, -5, 13], [6, -1, 2, -5], [7, -2, 4, -8], [-3, -2, -9, 5], [-8, 6, -7, -1], [-11, -1, -4, -8], [4, 1, 7, -8], [-1, 10, 0, -7], [-6, 3, -4, 2], [-7, -1, -6, 4], [10, 2, -4, 5], [-6, 4, 9, -5], [1, 4, -1, 9], [19, -2, -1, 4], [3, -6, 6, 7], [-8, 4, -2, -10], [-12, -6, -1, 9], [19, 4, -1, -3], [-2, 5, 7, -11], [3, -3, -3, 1], [-5, 9, 5, 1], [-10, -2, 3, -2], [-9, 1, 3, -4], [16, -7, -3, -5], [11, 3, 3, -8], [-8, 13, -3, -6], [-10, -4, 2, 8], [-7, -14, -5, 12], [-1, 1, -1, -7], [-10, 2, 3, 8], [-6, 6, 1, -8], [-12, 5, 0, -3], [5, 2, 1, 11], [-19, 1, 5, -11], [4, -10, 0, -2], [-1, 4, -1, -3], [-5, 10, 9, -3], [-11, -7, -5, 12], [-12, -5, -4, 9], [6, -14, 4, 12], [3, -8, 5, -1], [-8, -2, 0, 12], [9, -9, 0, 11], [-1, -14, -4, 7], [8, 9, 6, -22], [-5, -14, -6, 15], [-2, 0, -2, 4], [-20, 3, -3, -5], [8, 8, -1, -9], [-18, -4, -5, 7], [15, 11, -3, -11], [-13, 2, -1, 15], [-6, -9, -8, 3], [-5, 11, -3, 2], [-5, -17, -2, 10], [14, -6, 4, 16], [4, 4, 8, 2], [2, 5, -4, 5], [-21, -7, -1, 17], [-4, 7, -3, 2], [-1, 5, 2, 10], [-5, 1, -7, -7], [5, 15, 8, -19], [9, 12, 14, -16], [4, -14, 0, 16], [3, 9, 12, -5], [5, -5, -13, 11], [16, 10, -4, 0], [-10, -9, -4, 12], [5, 4, 3, 5], [0, 2, -10, 8], [9, 15, -8, 7], [0, 0, 0, -6], [-13, -8, -6, 16], [-5, 6, 15, -22], [4, 4, 12, 0], [-20, 5, 8, -11], [-1, 0, -8, 14], [20, -17, -7, -4], [-10, -2, -8, -4], [-5, 7, -1, 0], [-21, 0, 13, -14], [-28, 5, 5, -5], [-1, 5, 3, -12], [-26, 4, -4, -6], [17, 13, 5, -5], [-19, 5, -9, -8], [1, 17, 11, -19], [-6, 9, 10, -21], [4, 1, 2, 10], [-17, -5, 3, -1], [-19, -6, 7, -2], [-15, -7, -1, 21], [14, 2, 3, -18], [-4, -14, 4, -3], [9, -11, -14, 7], [1, -3, -17, 1], [3, 4, 2, 15], [-3, -11, -11, 25], [-1, -6, -12, 20], [-6, 9, -7, -9], [-6, 15, 16, -6], [-6, 16, 7, -9], [-3, 20, -1, -1], [27, -11, -3, 11], [9, 13, -8, 7], [-24, 12, 4, 4], [-2, 13, 1, -13], [24, -5, 10, -1], [23, 3, 9, -16], [-2, 10, 10, -8], [13, -4, -6, -8], [-10, -11, -7, 7], [22, -14, -9, 10], [-19, -9, 6, -5], [-26, 4, -4, 14], [-18, -12, 3, -7], [-4, 7, -1, 15], [1, -18, 9, -9], [11, -10, -1, 11], [-2, 0, -11, -9], [19, -23, -11, 3], [26, -3, -12, 2], [-4, 14, -1, -9], [2, 1, 8, 23], [9, -5, 5, -21], [2, -2, 10, -6], [12, 0, -10, 35], [-16, -3, -8, -3], [9, -10, -3, -9], [0, 7, -7, 11], [-11, -11, -9, 14], [-19, -21, -2, 12], [21, 7, 20, -5], [8, 8, 0, -20], [6, 3, 1, -2], [10, 11, 10, -29], [-6, -9, 9, -18], [3, 14, 23, -23], [-12, 16, 6, -4], [-22, 8, 6, -16], [18, 12, -4, 8], [15, -14, 4, 6], [46, 9, -1, -9], [21, -7, -14, 9], [2, -4, 2, -4], [2, -10, -2, -14], [-2, -2, 14, 10], [-11, -6, 9, -1], [1, -15, 5, -10], [-8, -2, 13, -19], [13, 11, 7, -27], [8, 9, 0, -17], [-7, 13, 7, -7], [2, -7, -10, 0], [0, 0, 2, 16], [20, -6, -4, -7], [20, 2, 7, 17], [22, 11, 5, -15], [-3, 18, 3, -28], [54, 1, -1, -2], [-27, -4, 2, 11], [25, -10, 9, 8], [6, 2, 4, -17], [6, 7, 5, -11], [5, 11, 4, -38], [0, -4, -10, -14], [2, 4, -17, -8], [17, 10, 3, -5], [-9, 15, 11, 7], [29, 18, -11, 2], [-20, 9, 0, 4], [30, -9, -12, 4], [-2, 16, 20, -12], [-22, 16, 0, 0], [-14, 21, -7, -21], [-35, -3, 5, -13], [-2, 1, -18, 6], [-7, -15, -13, 23], [28, -6, 0, 7], [7, -4, 6, -7], [9, -2, 3, -10], [15, -15, -19, 3], [-10, -13, -10, 12], [-21, 7, 9, -2], [18, 5, -8, -15], [5, 7, -11, 5], [-11, 5, -7, -2], [-1, -8, -21, 1], [28, 0, -14, 20], [12, -6, -6, -24], [9, 5, 2, -22], [-31, -11, -8, 23], [8, -2, -10, -2], [9, -27, 1, 21], [5, 18, 5, 7], [-13, -13, 15, -28], [-6, -23, -4, 16], [4, 16, 4, 4], [-9, 22, -11, 1], [-25, 1, -13, -3], [24, 5, 15, -19], [0, -2, 4, 28], [26, 6, -3, -8], [-24, -11, -5, 31], [-24, -20, -20, 34], [20, -3, 0, -14], [33, -9, -11, 17], [-23, -11, 11, -31], [37, -13, 8, 0], [18, -6, -16, 9], [10, 15, 13, -28], [15, 11, 19, 4], [-13, -15, -16, 14], [-4, -30, 14, 8], [42, -15, 8, 7], [-28, -19, 6, -5], [11, 23, -4, -10], [4, -17, -14, 13], [-28, -9, -6, 18], [6, -6, -6, 4], [-16, 12, -13, 1], [24, 26, 21, -32], [-1, 7, 11, -7], [4, -6, -1, -25], [-1, 3, 13, -32], [-35, -6, -9, 6], [-27, 9, 8, -13], [-27, -9, -5, 8], [-30, 20, -5, -1], [-45, 3, -7, 1], [-19, 16, 11, -19], [2, -31, 4, 22], [25, -14, 7, 14], [-11, 17, 9, 7], [-3, -19, 25, -6], [35, 15, 9, -40], [-6, 16, -10, 9], [-19, -16, 1, 1], [16, -17, -16, 33], [-12, -25, 6, 26], [2, 25, 1, 4], [6, -21, -19, 22], [-29, 1, -11, 9], [-29, 2, 16, -30], [-7, -1, -14, -21], [3, -4, -11, -29], [0, -11, 5, 30], [-5, -8, -2, -7], [-38, 2, -7, -5], [2, 8, 12, -26], [-10, -4, 8, -10], [-23, 16, -3, -13], [5, 17, -11, 18], [-10, 24, 0, 18], [3, -2, 8, -27], [-4, -22, -30, 28], [3, 16, -20, -4], [-10, -17, -7, -1], [-1, -8, 10, 4], [13, 2, 3, -8], [8, 12, 13, 21], [-19, 31, -9, -7], [-2, -2, 4, -16], [32, 4, -6, 16], [11, -17, -5, -15], [20, -4, 6, 22], [2, -6, -27, 12], [-57, -3, 5, -2], [-34, 9, 0, 4], [0, -12, -3, 13], [-72, 1, -6, 1], [-49, 11, 6, 6], [-26, 6, -1, 15], [10, -20, -6, -6], [27, -25, -8, 17], [50, -8, -15, 7], [-6, -29, -18, 14], [32, -3, 14, -4], [-26, 5, 6, -1], [6, 11, -19, 13], [24, 13, -3, -16], [3, -37, -7, 17], [-36, -16, -15, 10], [-17, -14, -31, 29], [-19, 3, -3, 36], [11, -13, 26, -3], [-22, -16, 2, -14], [-3, -1, 3, -6], [30, 13, 18, -20], [-29, 33, 1, -13], [24, -3, 16, 37], [-4, -18, -20, 36], [-8, 0, 8, -24], [16, -7, 18, -18], [-35, -22, -11, 48], [13, -7, -5, -2], [-2, 5, -7, 3], [-21, -5, 9, 13], [19, 1, -3, -32], [11, -24, -32, 21], [-8, -8, -2, 40], [-31, 21, 21, -23], [33, -21, -21, 2], [-14, 1, 10, -36], [-4, -21, 12, 15], [11, 16, -19, 18], [-18, 5, 16, -35], [-3, -32, -14, 35], [21, -18, -15, 5], [-13, -17, 7, 35], [-55, -12, 17, -15], [-16, 5, -3, -40], [22, -5, -19, 4], [-25, 13, -26, -3], [1, -13, 1, -1], [0, -15, -13, -9], [-64, 9, -16, -11], [-9, 9, -7, -1], [11, -20, -3, -16], [17, -18, -5, -33], [-23, 26, 0, -13], [24, 11, -2, 3], [-38, -1, 0, 6], [-46, 21, 2, -7], [33, 6, -3, 10], [-54, 10, 5, -2], [19, -17, -9, 24], [12, 1, -13, 25], [17, 2, 17, 21], [38, 28, 11, -23], [10, -1, -5, -24], [12, 3, -6, 55], [4, 6, -11, -25], [39, 9, 10, -12], [-16, -10, -8, 16], [10, -8, 12, -6], [19, 15, -1, -43], [-28, 14, 8, -1], [18, 6, 3, -10], [-3, -27, -12, 6], [-45, -3, 3, 26], [45, -7, -19, 13], [-14, 8, 15, 0], [-4, -27, 11, 31], [37, 2, 16, 11], [-22, 5, -3, -13], [-8, 8, 1, -51], [-14, 18, -18, -18], [17, -23, 15, 35], [37, 0, -3, -17], [4, -1, -4, 20], [-8, -2, -30, 6], [-4, 6, 30, -28], [2, -18, -22, -2], [-42, -30, 1, 27], [-34, -4, 23, -50], [17, 24, -9, 9], [-33, 10, -2, -8], [11, 8, 9, -10], [13, -23, -10, 22], [-12, 19, 16, 1], [-1, -1, -5, -34], [32, -18, 1, 3], [28, -4, -6, 2], [-14, -20, 18, 0], [3, 6, 13, 31], [-12, 27, -4, 16], [27, -11, 20, -1], [4, 22, 14, -38], [18, -10, 10, -20], [43, -12, -18, 21], [-33, -9, 3, 14], [-20, -13, 8, 7], [21, 13, -4, 12], [32, -15, 13, 31], [-2, -5, -21, -20], [14, 8, -3, 13], [-25, -2, 21, -29], [-24, -18, 8, 4], [11, -28, 19, -2], [-7, -27, 4, 23]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1334_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1334_2_a_e();".
// 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_1334_2_a_e(:prec:=4)
    chi := MakeCharacter_1334_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_1334_2_a_e();".
// 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_1334_2_a_e( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1334_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-1, 2, 1]>,<5,R![1, -16, -14, 0, 1]>],Snew);
    return Vf;
end function;