
// Make newform 1014.2.a.i in Magma, downloaded from the LMFDB on 17 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1014_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_1014_2_a_i();".
// 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_1014_2_a_i();".
// 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 := [-3, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    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_1014_a();"
function MakeCharacter_1014_a()
    N := 1014;
    order := 1;
    char_gens := [677, 847];
    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_1014_a_Hecke(Kf)
    return MakeCharacter_1014_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], [1, 0], [-2, 1], [1, -1], [-3, -1], [0, 0], [-4, 1], [-3, 1], [-1, -3], [-1, 2], [2, 2], [-7, -2], [-1, -6], [-1, 5], [3, 3], [-3, 2], [-8, 0], [-4, 3], [-1, 7], [-3, 1], [8, 1], [-6, -2], [-5, -3], [6, -2], [6, 0], [-5, -4], [-17, -1], [5, -3], [2, 2], [-10, -5], [4, 8], [10, 2], [-5, 4], [4, -8], [-8, 3], [-5, 1], [-8, -9], [10, -2], [6, -2], [-6, -6], [3, 11], [14, -3], [0, 4], [10, -1], [4, -8], [-9, 3], [6, -6], [20, -4], [7, -3], [-2, -8], [6, 8], [1, 5], [2, 9], [10, -2], [18, 5], [9, 11], [2, -2], [8, -8], [-4, 1], [5, 10], [17, -5], [-8, 13], [7, -9], [7, 5], [10, 2], [-12, 5], [-20, 0], [-7, 8], [21, -7], [2, 10], [1, -12], [-11, -7], [-13, -1], [-12, 1], [2, -2], [-2, -2], [-9, -12], [-10, 6], [-15, -4], [-16, 1], [-14, 2], [-5, -16], [-1, 7], [-1, -8], [-9, -5], [26, 6], [6, 10], [-10, -5], [-24, -1], [9, 11], [3, -9], [-30, 2], [-9, -7], [7, -1], [-32, 0], [15, -15], [-12, 1], [12, -17], [7, 17], [-23, -10], [1, 3], [-6, 21], [28, -4], [-4, -20], [-5, -11], [-2, 1], [-16, 0], [23, 8], [-6, -2], [1, -4], [-20, 12], [-1, -6], [-11, -14], [-14, 2], [-20, -16], [0, 15], [0, -8], [2, 14], [-14, 2], [14, 14], [-1, 6], [21, -4], [-42, 2], [24, 8], [-35, 5], [14, 10], [-27, -8], [-26, 2], [-5, 15], [-19, 0], [4, 4], [38, -6], [-17, 19], [14, -4], [6, 10], [30, -8], [42, -4], [-22, -10], [-34, 0], [-38, 9], [14, 18], [-6, -4], [0, -24], [-30, -2], [0, 7], [-28, 8], [23, -18], [10, -17], [-35, 7], [5, -7], [-17, 4], [12, 23], [36, -4], [24, -8], [26, -6], [-6, -2], [-6, -30], [15, -14], [19, 14], [22, 20], [-28, -4], [0, -24], [31, -7], [6, -14], [-39, 4], [0, 12], [7, -9], [30, 5], [-10, 16], [-55, -4], [-21, -7], [3, -20], [26, 2], [28, -1], [-36, 4], [-48, 5], [-1, 19], [-20, -19], [-12, 4], [2, -20], [-5, 1], [-2, 6], [-36, -3], [26, 8], [15, 23], [-33, 10], [-46, 8], [26, -18], [-42, -1], [-19, -19], [-5, 6], [-49, 9], [23, -11], [-44, -8], [15, -17], [14, 4], [38, -15], [0, 15], [-5, 14], [-12, 16], [18, -13], [-20, 12], [33, -4], [23, 2], [3, -19], [29, -10], [33, -13], [12, -32], [-21, 2], [-16, 24], [31, -8], [18, -22], [17, 13], [-31, -9], [26, 18], [-4, 11], [-20, -12], [24, 23], [0, 4], [22, -28], [30, -13], [0, 12], [9, 20], [38, -18], [-3, -23], [10, 9], [-14, 15], [14, 14], [8, 4], [-24, -20], [-26, -19], [4, 0], [-10, -18], [12, 3], [-46, 14], [-18, 10], [6, -16], [18, 1], [-2, -18], [33, -9], [-13, 17], [13, 7], [-5, 19], [-1, 20], [-9, -20], [10, -30], [40, 12], [14, -26], [-35, -7], [37, -13], [-30, 2], [-30, 16], [20, 4], [49, 16], [-18, 8], [9, 27], [2, 24], [-23, -31], [-26, 26], [16, 9], [24, -20], [0, 12], [49, 4], [-40, 15], [-39, 2], [-1, -19], [20, -16], [-47, 6], [11, -5], [-59, 10], [-8, -15], [-8, -20], [-2, 9], [4, 16], [-50, -2], [29, -5], [-16, -12], [-13, -14], [-10, 19], [-3, 37], [-31, 27], [-10, -6], [-8, 4], [7, 22], [17, 13], [19, 21], [3, -16], [14, -21], [5, 39], [-20, -3], [-30, -2], [-71, 9], [63, 10], [6, 10], [42, 8], [-17, -14], [-12, -36], [-27, 10], [-64, -4], [39, 3], [3, -20], [-6, -9], [-11, 1], [26, 6], [17, -21], [10, 13], [-27, 29], [22, 31], [24, -4], [-14, 3], [-41, -3], [-26, 4], [54, 13], [81, 3], [25, 5], [-13, 4], [-9, 45], [11, -25], [58, -16], [18, 5], [29, -9], [-40, 24], [21, -20], [7, 21], [42, -10], [-49, 16], [-49, 3], [83, 3], [9, 19], [-63, 6], [35, 2], [-35, -32], [-41, -21], [17, 3], [9, 29], [3, 25], [-59, 10], [-30, 2], [46, -3], [-48, -12], [70, 6], [44, -11], [-36, 3], [-47, 5], [-66, 8], [53, -3], [-40, 9], [-50, 22], [3, 7], [50, -18], [17, 9], [66, 0], [-14, 7], [-57, -9], [56, -5], [-36, -3], [27, -1], [-14, 34], [28, 7], [-25, -21], [-27, 8], [-12, -9], [24, -8], [22, -10], [-30, 6], [-26, 38], [38, -8], [-36, -20], [-23, 30], [-42, 26], [-31, -9], [1, 9], [-3, 28], [53, 9], [-24, -15], [-19, 39], [35, 1], [0, -8], [-2, 23], [-53, -8], [10, 8], [23, -2], [-31, -25], [18, 6], [-12, -40], [-9, -9], [-27, -5], [34, -24], [-24, -24], [8, -20], [-58, -5], [-6, 35], [57, 23], [11, 5], [6, -42], [-26, 30], [-13, 17], [0, 0], [-10, -42], [6, 11], [2, -40], [-30, -20], [-38, -26], [25, 28], [34, -35], [31, 15], [-18, 32], [29, 22], [7, -9], [-12, -52], [-31, 2], [-41, -8], [-1, 7], [62, -2], [-37, -12], [41, 26], [-6, -42], [-9, 7], [-50, -16], [72, 20], [41, -12], [-17, -32], [39, -9], [63, -1], [-22, -8], [18, 3], [-84, -8], [35, -6], [51, -21], [-12, 40]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1014_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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