
// Make newform 1716.2.a.e in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1716_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_1716_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_1716_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, 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_1716_a();"
function MakeCharacter_1716_a()
    N := 1716;
    order := 1;
    char_gens := [859, 1145, 937, 925];
    v := [1, 1, 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_1716_a_Hecke(Kf)
    return MakeCharacter_1716_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 := [[0, 0], [1, 0], [-2, 1], [0, -2], [-1, 0], [-1, 0], [-2, 3], [0, 0], [-2, 2], [-2, -1], [-4, 1], [-2, -2], [-2, -4], [4, 3], [-8, 0], [-2, -8], [0, -2], [-6, -2], [0, -3], [-8, 2], [0, 4], [0, 5], [-12, 4], [-2, 7], [-10, 2], [-6, 9], [0, 2], [4, 2], [-4, 8], [2, -12], [0, 5], [8, 0], [6, -5], [4, -3], [-6, -6], [12, 0], [12, 4], [0, 9], [0, -8], [10, -7], [2, -2], [8, -12], [-8, -4], [0, 4], [2, 10], [4, 6], [12, 1], [-12, 7], [-2, -10], [-10, -8], [10, 1], [6, -10], [-6, -4], [20, -4], [2, 2], [0, -6], [-6, 2], [-4, -4], [-2, -12], [-18, 4], [20, -5], [-10, -6], [28, 0], [-22, 2], [0, 16], [-14, -5], [16, -11], [18, 6], [-20, 4], [-2, 24], [2, -9], [-2, 10], [20, 8], [-6, -18], [0, -21], [0, 14], [2, 14], [-18, 14], [2, 15], [8, 4], [26, -2], [-26, 8], [-8, 4], [-26, 0], [8, -1], [-12, 8], [10, 1], [14, -8], [-6, -12], [20, 5], [2, 18], [10, -10], [4, 11], [4, -12], [0, -7], [-16, 6], [-6, -11], [-2, -12], [-12, -7], [18, 0], [36, 7], [18, -6], [4, 4], [-2, -11], [-4, 17], [10, -12], [-12, 14], [-22, -2], [-8, 4], [-2, -32], [-8, -15], [-4, 24], [34, 5], [8, -1], [-36, 5], [-2, -28], [8, 11], [24, -4], [-26, 2], [0, 22], [6, -2], [-6, 4], [-18, -3], [4, -2], [-24, -11], [26, -3], [-26, 16], [-24, 8], [-20, -2], [-8, -16], [12, 2], [-8, -24], [-8, 8], [4, 20], [-22, -2], [-18, 0], [-6, -1], [4, 14], [14, 6], [-2, 13], [-12, -20], [-18, 16], [-24, -18], [6, -26], [-30, 8], [-36, 4], [-6, 8], [18, -5], [-20, 2], [-16, -26], [22, -4], [30, 12], [-32, 2], [0, -6], [0, -8], [16, -20], [-40, 1], [-26, -5], [-6, -20], [30, 0], [12, 16], [34, -1], [4, -30], [-34, -2], [-2, -11], [-12, -28], [0, 18], [-18, -10], [-24, -4], [6, 22], [-4, -24], [-38, 6], [20, 6], [-30, 0], [16, -14], [-14, 6], [24, -2], [42, 7], [-8, 5], [34, -10], [20, -19], [-24, 20], [-28, -4], [-6, 36], [-4, 12], [-18, -20], [46, -12], [0, 15], [2, -4], [0, 20], [-48, -8], [4, 36], [-8, 22], [-30, 13], [24, -12], [30, -10], [6, 40], [2, 32], [-2, 8], [-44, 10], [42, -4], [-24, 25], [10, 16], [18, 10], [28, -28], [34, 10], [-20, -7], [-12, -28], [-54, -12], [0, 2], [30, -8], [-18, 8], [24, -20], [24, -12], [-18, -6], [-34, -8], [8, 19], [-46, 3], [-20, -10], [-26, -15], [-14, -12], [16, 10], [18, 35], [20, 19], [52, 10], [10, 14], [14, -6], [8, -36], [-32, 1], [4, 28], [18, -16], [4, 3], [12, -4], [-6, 1], [16, -14], [0, -28], [-50, -2], [10, -18], [2, 22], [38, 2], [-20, 6], [-4, 31], [-20, -12], [10, -2], [-30, 14], [-32, 6], [-4, -37], [-8, 12], [-12, 18], [4, 10], [38, 8], [-2, -32], [44, 2], [8, -8], [-2, 45], [-30, -2], [0, -12], [28, -8], [22, -26], [34, 8], [24, -5], [-28, -2], [38, -16], [-30, 22], [-10, 35], [-20, 28], [30, -29], [26, -27], [-36, 0], [-2, 35], [-10, 32], [-16, 39], [-14, 18], [-40, 13], [-34, 22], [-4, 37], [-4, -42], [-20, -8], [8, -20], [24, -26], [44, -24], [-4, -17], [12, -30], [-8, 20], [-24, 30], [-16, 20], [30, 12], [-2, 12], [36, -11], [-22, -3], [10, 46], [-2, 42], [-6, 38], [46, -18], [2, -34], [-6, -7], [36, -18], [-50, -8], [4, -36], [-24, -20], [-30, -16], [-18, 28], [-40, -5], [10, -34], [-20, 6], [14, -26], [-42, -30], [58, 0], [-28, -40], [-22, 26], [-48, -26], [-34, 13], [-10, -37], [48, -16], [40, 8], [-62, -18], [-40, 28], [54, 14], [-22, 6], [-46, 21], [60, -5], [-22, 40], [54, -1], [-12, -11], [-34, 10], [-24, 16], [-40, 1], [48, 11], [28, 6], [-18, 3], [-8, 16], [-34, -20], [-8, -1], [22, 18], [20, -2], [8, -18], [26, 34], [-14, -8], [-2, -6], [-8, -25], [14, -18], [42, -2], [-22, -22], [-12, -34], [34, 17], [-32, 26], [-2, -16], [24, -27], [6, 6], [34, 0], [-36, -18], [2, 54], [-50, -19], [48, -1], [-6, -22], [38, 9], [-44, 0], [-38, -2], [-2, -39], [-60, 16], [18, 52], [2, -57], [80, -4], [10, 30], [76, -12], [92, 0], [-86, -4], [-24, -16], [58, -20], [-12, -4], [-28, -2], [38, -30], [-46, 23], [16, -37], [62, -8], [-12, -36], [-20, -42], [8, 4], [-6, -7], [-46, 18], [-42, -9], [-6, 15], [0, 50], [14, -60], [64, 8], [-32, 38], [28, 39], [-18, 56], [-28, -10], [-6, -26], [-38, 14], [-30, 27], [60, 26], [-4, -56], [-32, -18], [26, -22], [56, -16], [-38, 12], [12, -42], [-58, 26], [-6, -12], [-10, -2], [60, 22], [70, -5], [-42, -28], [-56, 3], [90, 0], [14, -30], [-16, 15], [-64, 2], [-6, 0], [-22, -43], [4, 0], [36, -29], [-70, 6], [-78, 6], [-42, 42], [16, -46], [-6, 5], [-40, -12], [-30, 10], [22, -56], [44, 6], [-28, -10], [-22, 16], [-82, -15], [12, -52], [6, 18], [-56, -3], [20, 40]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1716_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1716_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_1716_2_a_e(:prec:=2)
    chi := MakeCharacter_1716_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_1716_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_1716_2_a_e( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1716_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![2, 4, 1]>,<7,R![-8, 0, 1]>],Snew);
    return Vf;
end function;