
// Make newform 1003.2.a.e in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1003_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_1003_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_1003_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, -4, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [-3, 1, 1], [-3, -1, 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_1003_a();"
function MakeCharacter_1003_a()
    N := 1003;
    order := 1;
    char_gens := [768, 120];
    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_1003_a_Hecke(Kf)
    return MakeCharacter_1003_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, 1], [-1, 1, 0], [-1, 0, -1], [0, 0, 0], [-2, -1, -1], [-1, 0, 0], [-3, 0, -1], [0, -1, -1], [1, 1, 2], [-6, 1, -1], [0, -2, -2], [3, 1, -4], [-2, -1, 1], [0, 3, -1], [-9, 1, 0], [-1, 0, 0], [0, -1, 3], [0, 0, 4], [-8, 0, -2], [-2, 0, -2], [1, -4, 1], [0, -2, 2], [-6, 1, 1], [6, -1, -3], [0, -2, 0], [2, 1, 3], [5, 0, 1], [2, -2, 0], [-8, -1, -5], [-3, 2, -3], [6, -2, 4], [7, 1, 0], [8, 0, 6], [4, 1, 7], [12, 2, -2], [-4, 1, -1], [4, 0, 2], [-5, 2, 1], [-10, 3, -3], [-12, 1, -3], [1, 1, 2], [4, -3, -3], [-1, -1, 6], [-6, 0, 4], [-9, 4, -1], [2, 3, -11], [4, 2, -2], [-6, 0, 6], [18, 2, 6], [4, -4, 0], [11, 0, -7], [9, -3, 6], [-5, 6, -1], [-11, 1, 6], [7, 0, -3], [-18, 0, -2], [17, 0, -1], [-1, -1, 6], [-5, 7, -6], [-4, 5, 1], [-11, -3, -6], [-5, 8, 1], [11, 0, -1], [-12, -3, -3], [18, 0, 4], [1, -4, -1], [-2, -2, 8], [12, 6, -2], [-6, 4, -4], [-8, 4, -10], [-3, 0, 11], [-8, -2, -6], [-6, -6, 6], [-1, 4, -9], [4, 2, 10], [-26, 0, 0], [-28, 3, -1], [14, 5, -1], [6, 1, 1], [-18, 1, -5], [22, 3, 3], [-12, -6, -2], [-1, -5, 6], [8, 2, 0], [-6, -9, -3], [-15, -5, 12], [0, -4, 6], [6, 8, -8], [0, -5, -1], [10, 0, -6], [20, 2, 4], [-5, -2, -3], [-9, 4, 5], [-7, -4, 17], [-20, -2, 6], [-8, -1, 1], [-14, 4, -4], [7, 0, -11], [-12, -1, -1], [-20, 6, -4], [7, 3, 2], [6, 2, 0], [-14, 6, -6], [-4, -12, -4], [-7, -1, 4], [-14, -1, -11], [-19, 1, -6], [21, 2, 1], [0, -6, -6], [-13, -4, 7], [-14, -3, -3], [29, -1, -4], [23, 6, 13], [8, 4, 8], [14, -2, 6], [17, 2, -9], [-13, -6, 3], [-21, -1, 10], [4, -10, -2], [-13, 7, 2], [2, 5, 3], [-30, -2, -6], [14, -5, 1], [-8, -3, -11], [-18, 1, -7], [41, 3, 4], [2, -9, 1], [12, 0, -8], [-2, 12, 0], [-18, -3, -9], [-8, 14, -8], [-2, 6, -8], [-23, -1, -4], [5, 11, -4], [4, 11, -7], [6, 0, -8], [-12, 6, -4], [-10, -1, -9], [14, 4, -10], [28, 0, 4], [18, -2, -4], [-8, 15, -5], [20, 6, -4], [21, -7, 10], [-2, 2, -8], [-19, 1, -2], [4, -11, 5], [4, 3, 7], [-26, -4, 14], [13, -1, 4], [4, 8, -4], [15, -10, -1], [-14, 4, 2], [-7, -12, 9], [11, 4, 7], [8, 4, -4], [-12, -10, 6], [-42, 0, -8], [-10, -7, -9], [5, 2, -13], [14, -6, 10], [-6, -4, -14], [-5, -12, 13], [0, -9, -7], [-26, 0, 6], [-24, -10, 2], [5, 3, 8], [4, 2, 14], [-8, 5, -15], [12, 4, 4], [-36, -3, -1], [15, 6, -15], [-26, -2, -10], [9, -8, 3], [1, 1, 10], [19, 8, -1], [-42, -7, -13], [20, 0, 0], [-17, -5, -2], [5, -8, 7], [21, 12, 9], [-4, 10, 6], [-21, 7, -10], [-28, -6, -6], [22, -7, 1], [-42, -5, 1], [-6, -6, 12], [18, 1, 3], [6, -9, 5], [24, 5, 9], [-30, -11, -9], [20, 1, 5], [-10, 6, 10], [1, -10, 3], [-22, 2, -12], [6, 12, -24], [0, -3, 21], [4, -7, 9], [14, -3, 11], [-14, -8, 16], [9, 8, 13], [-22, -6, 10], [-46, -3, 9], [23, -2, 3], [0, -2, -4], [-26, -7, 7], [2, -5, -7], [22, -16, 6], [-44, -5, -9], [-4, -2, -10], [3, -5, 2], [29, -12, 9], [-17, 8, -15], [7, 2, -15], [40, 3, 3], [-8, 2, 2], [18, -6, -2], [-6, 2, 20], [-2, 16, -12], [6, 5, 17], [-20, 6, 2], [24, 1, -5], [-5, 4, 7], [-4, -1, 11], [-4, 13, 7], [-29, 5, 0], [20, 2, 10], [2, -7, 27], [-11, 8, -3], [-48, 3, 1], [24, -4, 12], [-4, -3, 21], [20, -8, -10], [-18, -9, 9], [-13, -8, -15], [-50, -4, 6], [-10, 6, -16], [36, 1, 9], [0, 0, -4], [1, -2, -17], [-8, 8, -4], [39, 8, -3], [9, -7, -6], [-7, -11, 6], [16, -12, 14], [-32, -3, -3], [12, 5, -3], [-33, -2, 7], [-23, -12, 15], [33, 1, 6], [4, 7, -9], [22, 15, -3], [1, -3, -10], [29, -5, 16], [16, -10, 14], [-7, -5, -20], [-6, 0, -10], [-8, 6, 2], [-22, -12, 8], [-14, 5, 15], [-11, 4, 15], [-17, -5, 18], [10, -4, 8], [33, -3, -10], [12, 3, -25], [17, -1, 4], [16, 15, 5], [1, 12, -3], [-35, -5, 8], [-40, 3, 3], [20, 4, 8], [36, 10, 12], [-33, -4, -3], [11, -7, 8], [-14, -2, -4], [-36, -8, 12], [-1, -1, 26], [-28, 12, 6], [-5, 2, 15], [8, -14, 22], [-2, -9, 5], [10, 0, 22], [-24, -6, -12], [24, 4, -20], [4, 5, -11], [42, 1, 15], [-1, -11, 12], [10, 5, 15], [-13, 21, -16], [-30, -18, -20], [-28, -12, -6], [-2, 16, -20], [-20, 1, -11], [17, 13, -10], [26, -1, 5], [20, 10, 18], [5, -9, -16], [4, 2, -30], [-10, 15, -7], [-3, 11, -28], [-24, -11, -1], [18, -6, 0], [8, -18, -2], [1, -6, -5], [14, -1, 9], [-13, 16, -15], [38, -2, 8], [0, 2, 2], [-34, -5, 23], [-15, 20, -11], [3, -11, 24], [5, 15, -4], [2, -4, -10], [23, 6, 3], [-16, 1, -29], [46, -1, 1], [11, -2, 25], [18, 6, 2], [-22, 8, 12], [36, 12, 2], [-18, -5, -7], [-17, -7, -8], [-35, 2, -3], [21, 9, -10], [-6, -7, 9], [8, -7, -7], [-16, 20, -10], [-20, 3, -13], [2, -10, -8], [16, 1, 25], [-30, -12, -18], [-4, -1, -5], [-17, -10, -17], [20, 6, 28], [-29, -8, -1], [-35, -3, -22], [-14, 26, -8], [18, 8, -18], [-3, 4, 3], [22, 12, -16], [-2, 15, -21], [-14, -11, 19], [-8, 0, -8], [14, -5, 27], [36, -4, -8], [26, -17, 13], [3, 6, 15], [0, 7, -17], [-60, -7, -19], [-50, -14, -8], [25, -3, -2], [49, 9, -2], [34, -13, -3], [78, 0, 4], [22, 11, 7], [8, 15, -13], [9, -10, 15], [17, 5, -18], [-25, 0, -15], [6, -10, 16], [47, -1, -2], [5, 0, -29], [42, 0, -6], [-12, 12, -8], [26, -6, 6], [22, 14, -20], [59, 4, -3], [28, -4, 20], [-8, 24, -12], [34, -17, 13], [18, 0, 26], [-10, 4, 4], [-16, 1, -19], [-45, -5, -2], [-38, -7, 27], [-40, -9, 11], [-22, -16, -16], [-14, 24, -4], [17, 13, 14], [-19, -5, 0], [-22, 9, 1], [-16, -2, 28], [-36, 8, 0], [43, -4, 11], [-46, -9, -23], [-33, -16, 1], [-30, -12, -28], [44, 2, -20], [-18, -9, -11], [46, 14, 4], [-10, -15, 7], [-20, 5, 17], [10, -22, 20], [-20, -10, -30], [30, 6, 12], [7, -10, -1], [-45, -7, 8], [7, 2, -19], [-23, -21, -20], [2, 12, -16], [-14, 0, -12], [-23, -14, -7], [-6, -24, 4], [-3, 21, -10], [12, 0, 0], [-4, -11, 19], [11, 3, 26], [-14, 14, 8], [-16, 8, -18], [-1, -13, 2], [-58, 8, -8], [8, 7, 7], [-13, -10, 17], [-5, 11, -2], [-11, -3, 26], [36, -6, 2], [32, 9, 9], [-20, -14, 10], [33, -4, -13], [-18, -5, -5], [42, 10, 10], [-4, -14, 4], [1, 12, 11], [-25, 19, -22], [-3, -3, 6], [-28, -18, -14], [30, 14, 18], [-37, -2, 19], [16, 0, 18]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1003_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1003_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_1003_2_a_e(:prec:=3)
    chi := MakeCharacter_1003_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_1003_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_1003_2_a_e( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1003_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![1, 1]>,<3,R![2, -5, -2, 1]>],Snew);
    return Vf;
end function;