
// Make newform 1014.2.a.m in Magma, downloaded from the LMFDB on 23 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_m();".
// 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_m();".
// 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, -2, -1, 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_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, 0], [1, 0, 0], [1, 1, 1], [0, -1, 2], [0, -2, -1], [0, 0, 0], [6, -2, 4], [-4, 4, -4], [4, 2, -2], [4, -2, -3], [0, 4, -5], [6, -4, 2], [4, 0, -2], [0, -6, 2], [0, -4, 0], [1, 9, -3], [2, 1, -2], [-4, 2, 0], [8, -8, 10], [2, -6, 6], [-1, -3, -1], [4, -11, 6], [-2, 11, -2], [2, 2, -2], [-3, -1, -3], [2, -5, -4], [-9, 3, -7], [-2, 3, 2], [8, -4, 0], [4, 4, -4], [-11, 9, -9], [3, -3, 9], [-8, 4, -12], [0, -2, -2], [12, -6, -3], [-4, 0, -11], [2, -6, 2], [-6, -2, -4], [-6, 10, 6], [0, -10, 7], [7, -3, 17], [-10, 10, -6], [6, -6, -4], [2, 4, 4], [-6, 5, 0], [0, 12, -7], [-8, 6, 4], [0, 4, 4], [-17, 5, 1], [14, 6, -4], [-22, 8, -18], [-16, -2, -2], [-19, 7, -3], [-14, 15, -6], [2, -6, -4], [-10, 0, -10], [-3, -19, 9], [1, -7, 7], [-12, 10, -6], [-14, 6, -2], [-18, -4, 0], [12, -2, -1], [-10, 10, 8], [0, 6, -6], [-19, 11, -15], [6, -3, -4], [6, 4, 10], [-4, -8, -5], [10, 1, 2], [10, -2, -14], [18, -18, 20], [-4, -2, 2], [0, 5, -14], [-12, 22, -16], [-8, 8, 4], [-24, 6, -6], [24, -14, 15], [8, -2, 12], [2, 18, -16], [16, -15, 24], [0, -10, -3], [-12, -2, -4], [-14, -10, -2], [8, -16, 15], [12, -7, 18], [12, -6, -7], [-12, 4, -12], [4, -12, -5], [-10, 7, 12], [0, -16, 4], [5, 11, -13], [12, -20, 14], [-5, 15, 1], [4, -6, -7], [-4, 8, -16], [-18, 12, -14], [26, 0, 12], [2, 8, 16], [2, 8, -18], [-4, 8, -2], [4, 8, 0], [14, 0, 8], [-7, -5, 11], [0, -8, -24], [18, -24, 8], [-20, 9, -4], [20, -10, 29], [18, 0, 16], [10, -2, -8], [8, 0, -11], [32, -15, 14], [-8, 28, -2], [-4, -18, 4], [-10, 26, -6], [-16, 1, -6], [24, -18, 18], [-10, -6, -8], [-18, 16, -22], [14, -3, 16], [-14, 19, 6], [10, -24, 18], [4, -7, 20], [18, 13, 0], [-11, -5, -9], [-10, 0, -12], [-14, 4, 12], [26, -12, 26], [-18, 24, 8], [12, 9, 10], [-18, 6, -16], [-4, -4, 6], [-36, 12, -6], [28, 12, 3], [12, -12, 4], [-16, 8, -8], [-2, 4, -16], [34, -7, -12], [-16, 0, -22], [12, 14, -13], [-28, 4, 10], [-20, -8, 16], [-1, -9, -17], [-12, -23, 10], [-8, 14, -11], [-16, 14, -18], [14, 8, 22], [16, -12, 22], [12, -10, 28], [-12, -14, 16], [24, -12, 34], [40, -4, 6], [18, -2, -4], [-26, -14, 0], [-28, -8, 2], [-6, -16, 0], [34, -6, -8], [31, -9, 5], [-6, 28, -14], [-8, 0, 25], [12, -6, -7], [9, -13, -5], [-12, -6, -6], [12, 0, 7], [4, -26, 3], [-2, -14, 16], [10, -38, 4], [-15, 29, -17], [18, -10, 18], [-12, 8, -1], [-10, -8, 0], [36, 16, -8], [-18, -10, -8], [6, 28, 2], [-10, 20, 12], [0, 24, 12], [20, -34, 26], [16, 2, 14], [34, 15, -8], [-20, -12, 12], [-6, 2, 22], [5, 21, 3], [32, 26, -11], [-22, -10, -12], [-12, -18, 6], [-10, 20, -2], [22, 5, -4], [-6, 2, 4], [16, 10, 14], [-8, 41, -16], [-32, 34, -22], [4, 12, 27], [6, 1, 18], [34, -22, 8], [41, -31, 33], [-37, 29, -27], [8, 20, 4], [-40, 20, -29], [10, 14, -14], [-28, 22, 8], [6, -14, 0], [10, -28, -4], [-4, 20, 15], [48, -4, 4], [21, -9, 21], [-2, 7, -6], [-12, 46, -23], [41, -15, 7], [-14, 33, -18], [12, -10, 32], [30, -12, 38], [17, 3, -11], [0, -30, -9], [4, -20, 28], [4, -20, 24], [14, 6, 32], [-20, -1, -8], [-12, 16, 7], [16, -16, -8], [-28, 26, -14], [-35, 9, 1], [18, -40, 12], [-17, 19, -11], [-6, 20, 18], [-32, 12, -19], [0, -4, -4], [8, -2, 16], [2, 0, -40], [6, 2, 26], [7, 15, 25], [-23, 3, -1], [-52, 20, -18], [34, 8, 0], [24, -21, 22], [22, -22, -6], [-34, 46, -42], [-32, 18, -10], [-16, 33, -4], [22, 15, -8], [8, 16, 20], [48, -32, 42], [4, -6, -7], [-16, -14, -10], [-16, 12, 36], [-28, -24, 20], [-32, 32, -36], [20, -40, 30], [48, -20, 33], [8, -42, 29], [14, -16, -22], [-4, -16, 22], [30, -16, 38], [-34, -2, -16], [-28, 6, -42], [8, -7, -32], [-2, 3, -36], [50, -31, 30], [4, 10, 14], [0, -20, 34], [34, -13, 12], [-44, 4, 9], [16, -16, 3], [37, -5, 23], [-6, -14, 8], [-14, 8, 8], [-44, 10, -40], [-20, 20, 12], [-7, 33, -31], [-18, 0, -12], [-26, 34, -38], [-33, 15, -29], [32, 8, -12], [-8, -12, -14], [-37, -19, 15], [1, -31, -9], [24, -3, -8], [36, 8, 15], [59, 1, 5], [-66, 12, -24], [-8, 17, 16], [-10, 33, 6], [-16, 26, 36], [-15, -19, 7], [12, 0, -20], [-34, -8, -12], [38, 20, -16], [-12, 24, -28], [-33, -15, 11], [13, -31, 21], [-20, -4, 11], [-28, 4, -10], [26, -19, 16], [-8, -46, 23], [24, -26, -8], [23, 9, 1], [-38, -22, -2], [-30, -15, -12], [-11, 33, -25], [2, 27, 12], [-36, 10, -39], [-14, -2, -44], [-20, 32, 3], [-3, 29, 1], [20, -39, 34], [5, -5, 11], [34, -24, 20], [33, -21, 21], [-16, -28, 0], [46, -44, 52], [-8, 18, -18], [10, -28, 0], [-22, 24, -4], [-2, -23, 0], [48, -16, 42], [-62, -4, -2], [-46, 22, 16], [-40, 20, -21], [-39, 23, -17], [0, -50, 28], [18, -64, 32], [-22, -2, -4], [-30, -34, 4], [8, 8, 13], [46, -21, 40], [-60, 0, 1], [50, 6, -4], [-36, 16, 9], [-62, 2, 0], [38, -12, 8], [20, 0, -26], [2, -8, 48], [-14, 28, 30], [-9, 15, 15], [24, 28, -12], [19, 25, -11], [50, 20, -4], [29, -33, 47], [12, 16, 2], [-24, 12, 14], [12, 4, -23], [0, -16, -13], [-14, -10, -44], [-2, -4, 26], [20, 6, 3], [40, -12, 25], [28, -14, -33], [-14, 51, -46], [-34, 26, -32], [12, 4, -42], [-22, -34, -2], [5, -7, -3], [-20, 24, -4], [60, 17, -12], [-44, 26, -9], [-20, 32, -12], [10, 32, -24], [4, -18, -6], [-28, 4, -34], [-34, 19, -50], [-38, 46, 0], [-4, -24, 26], [-4, -8, -14], [4, -12, -4], [4, 32, -6], [62, -15, -2], [-6, -8, 32], [43, -15, 7], [-6, -15, 16], [-17, -1, -23], [44, -16, -9], [19, 17, 17], [28, -24, -28], [-26, 42, -48], [4, 22, 21], [11, -5, -19], [20, 16, -38], [-1, 29, -11], [42, -20, -26], [8, 25, 12], [-14, -2, 8], [-12, -37, 4], [40, -22, 33], [36, 2, 4], [-8, -20, 11], [22, -4, -14], [24, -60, 40], [12, -4, 30], [8, -11, 42], [-2, -34, 18], [-30, 10, -22], [50, -10, 28], [-36, 52, -15], [29, -31, 25], [66, -25, 34], [24, -22, 32], [-30, 14, 38], [8, -28, 9], [24, -7, 14], [-48, 18, -14], [42, 2, 10], [-28, 6, -63], [-14, -28, -10], [-16, 28, -36], [27, 23, -27], [-36, 16, -18], [-4, 38, -25], [-75, 29, -53], [66, -34, 18], [-26, 24, 8], [4, -28, 38], [-44, -12, 0], [68, -37, 44], [-48, -10, 19], [52, 6, -19], [6, 30, -10], [-11, -5, -51], [-6, -12, 36], [10, 0, -10], [-48, 36, -43], [-16, -6, 2], [28, -32, 20], [34, 13, 16], [46, -12, -10], [34, -34, 46], [-40, 20, 12], [24, 12, -35], [-42, 8, -36], [-37, 17, -47], [-26, 44, -56], [54, -26, -6], [50, -14, 26]];
    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_m();".
// 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_m(:prec:=3)
    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_m();".
// 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_m( : 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, -3, 1]>,<7,R![1, -4, 3, 1]>],Snew);
    return Vf;
end function;