
// Make newform 1143.2.a.e in Magma, downloaded from the LMFDB on 17 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1143_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_1143_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_1143_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 := [-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_1143_a();"
function MakeCharacter_1143_a()
    N := 1143;
    order := 1;
    char_gens := [128, 892];
    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_1143_a_Hecke(Kf)
    return MakeCharacter_1143_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, -1, 0], [0, 0, 0], [2, 1, -1], [-1, -1, 1], [0, -2, -1], [-1, 2, -3], [6, 1, 0], [1, -1, 1], [3, -1, 2], [-1, 3, -1], [4, 1, -3], [0, 6, -4], [4, 0, 4], [-3, -6, 6], [1, -4, -2], [-1, 6, -7], [0, 2, 1], [-1, 2, 6], [-1, 1, -1], [-1, 6, 2], [1, -5, 7], [3, 7, 0], [-4, 6, 5], [11, 5, -2], [-5, -3, 6], [6, -7, 0], [1, 1, 2], [-8, 4, -1], [7, -2, -2], [5, -3, -2], [-1, 0, 0], [0, -13, 6], [11, 0, 0], [-3, 8, -8], [-15, 6, 1], [-1, 2, -10], [-8, 5, -7], [-4, -2, -2], [1, 2, -4], [2, -2, 12], [-3, -7, 6], [-2, 3, -4], [4, 1, 5], [8, 0, 9], [-6, 1, 6], [-5, -6, 6], [-5, -5, 7], [6, -3, 16], [9, 0, 1], [-9, 1, 0], [7, -3, -3], [3, 10, -9], [-4, -9, -5], [-10, -9, 5], [5, 4, 5], [-9, -9, 12], [-1, 6, 2], [-14, -14, 9], [3, -12, 7], [-3, -12, 5], [13, -6, 7], [4, -9, -13], [-14, 6, -9], [-3, -5, -1], [-14, 11, -11], [8, -10, -6], [20, -3, -5], [7, -1, -3], [24, 0, -3], [-7, -17, 12], [4, -13, -5], [2, -1, -2], [-4, 20, -10], [16, -11, 7], [-1, 11, -4], [25, -4, -1], [16, 14, -9], [2, 9, -3], [-6, 2, 1], [0, 14, -2], [13, 0, -10], [2, -15, 2], [-9, -8, 7], [-13, -9, 14], [5, -9, 19], [11, 3, 8], [5, -4, 4], [14, -2, -11], [-7, -5, -13], [-1, -6, -6], [-16, 7, -20], [-13, -17, 9], [15, -14, 11], [4, -10, 1], [3, 16, -17], [-16, 16, -1], [-7, 13, 0], [-4, -2, -4], [15, 8, -15], [17, 5, 0], [-19, -2, -7], [12, 1, 9], [8, -13, 9], [-16, 3, -1], [-8, -10, 5], [-18, -11, 13], [23, 2, 1], [-7, -1, 1], [14, 12, -5], [-1, 17, -7], [0, 5, -19], [1, -7, 11], [23, 6, 5], [20, -13, 16], [2, -8, 2], [-1, 2, 6], [0, -20, 10], [8, 11, -17], [-10, 16, 13], [10, 8, -3], [-15, 6, -11], [13, 20, -15], [0, -9, -3], [-4, 12, 6], [-11, 15, -10], [11, -3, 20], [13, 5, -7], [7, 6, 18], [-10, -15, 25], [-28, -2, 5], [-35, 10, 5], [-14, -18, 9], [-1, 12, -8], [25, 1, 2], [28, -8, -4], [0, -5, -4], [-7, -5, -9], [31, 1, -9], [-2, -28, 18], [-1, -8, 9], [2, 24, -3], [2, 13, 11], [4, -1, 16], [-16, 5, -25], [-11, -1, -8], [-7, -16, 13], [2, -11, 22], [32, -6, -1], [7, -17, 25], [-14, 7, -16], [-21, -9, 9], [-5, 14, 14], [9, 31, -30], [12, 24, -19], [10, -20, 30], [1, 31, -28], [2, -12, -15], [-14, 7, -6], [-26, 3, -7], [0, 10, -3], [-32, 9, -23], [2, 22, -6], [18, -5, -4], [-19, -11, 3], [11, -19, 27], [-2, 19, 6], [22, 4, -3], [-6, 21, -11], [-25, -7, 0], [-2, 16, -28], [-40, 2, 9], [-3, 22, -30], [-34, 9, -16], [-17, 26, -7], [-10, -9, 20], [9, 5, -16], [21, -6, 9], [-7, -1, -4], [-25, 9, -30], [22, -11, -2], [31, -7, 14], [-14, -5, 0], [-6, -2, 9], [-26, 2, -6], [10, -14, 8], [10, 4, -13], [-6, -11, -10], [-13, 14, -6], [0, 24, -1], [9, 0, -3], [12, 13, 4], [-21, 9, -5], [-28, -26, 20], [12, 3, -9], [1, 20, 3], [13, 11, -6], [-4, -1, -19], [-12, -5, 12], [14, 21, -27], [17, -12, -2], [-1, -12, 18], [-16, 5, 12], [-14, 9, -4], [16, 17, -10], [22, 22, -19], [37, 7, -4], [-11, -13, -21], [-32, 9, -15], [0, -4, 24], [11, -7, -6], [21, -15, -4], [-18, -37, 26], [4, 5, 16], [3, -34, 23], [-24, 17, -12], [25, -18, 0], [-35, 6, -26], [15, 5, 17], [24, 4, 22], [-7, -25, 31], [-11, -8, 18], [-13, 6, -7], [-13, -30, 11], [-23, 2, 5], [-15, -28, 16], [24, 26, -11], [36, 13, -23], [17, 22, -25], [23, -15, 3], [-3, -7, 2], [-28, 14, -1], [-10, -9, -10], [-12, 10, -17], [26, 43, -25], [27, 10, 17], [-42, -11, 1], [15, -25, -12], [-15, 2, 6], [14, 22, -3], [7, -27, 3], [12, 13, -22], [-12, -23, 21], [47, -7, 16], [3, 7, 12], [24, -12, 2], [-23, -13, 6], [-22, 15, -30], [-15, 20, -16], [-3, 7, 32], [35, -23, -2], [13, 15, -18], [7, -27, 10], [-49, -6, -3], [18, -13, 19], [23, -2, 13], [17, -15, -10], [-30, 7, -32], [13, 15, -37], [-14, -10, 12], [35, -3, 1], [20, 3, 7], [22, -18, -15], [11, -4, -29], [-4, 16, -11], [22, 39, -18], [-19, 29, 16], [-61, -8, 6], [2, 4, 3], [23, -16, -3], [11, -18, 32], [-8, -42, 25], [-59, 10, 5], [0, 18, -22], [-37, 9, 2], [-17, -13, -16], [20, -23, -28], [54, 5, 5], [5, -12, 15], [-5, 4, 21], [-15, 7, -14], [-17, -5, -28], [-34, 6, -6], [-15, 11, 18], [-22, -49, 34], [13, -19, 14], [13, -2, 16], [1, 12, -13], [19, -12, -5], [21, -24, 18], [-40, -3, -9], [-38, -24, 3], [16, 6, 9], [-15, -5, -10], [-19, -7, 8], [-9, 35, -13], [-22, -17, 30], [11, -26, 19], [-3, -34, 17], [-2, 26, 0], [-39, -6, -15], [-7, 7, 3], [4, -12, -20], [-3, -3, 29], [-26, 19, -4], [-3, -27, 3], [30, -9, -5], [52, -22, 1], [-9, -9, 8], [0, -35, -13], [-7, 6, 17], [30, -20, 22], [-27, 26, 8], [-5, -12, -20], [3, 25, 4], [20, -36, 28], [-2, -20, 43], [17, -30, 39], [14, -7, -30], [-3, -23, -25], [-20, 34, -45], [-15, 12, -8], [1, 35, 0], [-44, 2, 4], [-19, 21, 6], [17, 35, -21], [40, 21, -21], [-1, 5, 23], [57, 6, 7], [-43, 9, -20], [33, 21, -18], [-6, 14, -27], [-30, -12, -25], [-6, 14, -27], [-4, -26, 3], [-23, 29, -60], [3, 29, -24], [-5, -2, -41], [22, -28, 12], [-17, -12, 35], [8, -10, 16], [-5, -16, -31], [34, -25, 25], [-21, -9, -30], [20, 7, -24], [9, 12, -23], [27, -23, 54], [-11, 30, -53], [2, 40, -53], [36, 2, 4], [15, -17, 52], [20, -16, 8], [-23, 43, 11], [-26, 11, -48], [-21, 6, 11], [-15, -27, 33], [-33, -17, 1], [28, 11, 14], [26, 26, 8], [41, -13, 19], [23, -10, -11], [11, -27, -11], [-41, -13, 3], [-9, -38, 24], [24, -29, 14], [6, -17, 12], [18, -9, 40], [40, -8, -20], [47, 10, -20], [32, -29, 33], [-16, -8, -27], [16, 41, -36], [-7, 13, 4], [-23, 16, -36], [35, 5, 13], [4, 6, 24], [17, 57, -34], [47, 14, -28], [60, 18, -22], [20, -5, -28], [-11, 20, -13], [-29, 2, 6], [25, 16, -31], [13, -18, 2], [-17, -25, 15], [-17, -25, 5], [59, -2, 0], [-45, -14, 12], [2, 28, -15], [2, -28, -5], [31, 16, 13], [-5, -7, 38], [4, 14, -7], [51, -5, -24], [7, -14, -5], [-11, 10, 29], [3, -39, -16], [28, -5, 13], [47, 29, -8], [-21, -9, -41], [-13, -5, -45], [4, -43, 5], [51, 19, -22], [47, 24, -31], [-12, -17, 49], [-7, 28, -12], [-76, -6, 0], [23, -37, -20], [-26, -42, 16], [-17, -35, -11], [-2, -10, -22], [30, 41, -12], [-35, -9, -21], [41, 11, 11], [34, 2, -22], [-20, -4, -34], [-25, 14, -53], [-13, 4, -11], [49, 11, -11], [46, -4, 31], [-4, 18, 32], [-34, -31, 13], [42, -26, 37], [35, 41, -35], [-13, 26, -22], [16, -27, 33], [-49, 8, 12], [-40, -3, 1], [38, -41, 12], [4, 0, -16], [-41, 22, -6]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1143_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1143_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_1143_2_a_e(:prec:=3)
    chi := MakeCharacter_1143_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_1143_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_1143_2_a_e( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1143_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![3, 0, -3, 1]>,<5,R![-1, 9, -6, 1]>,<7,R![-3, 0, 3, 1]>],Snew);
    return Vf;
end function;