
// Make newform 5415.2.a.n in Magma, downloaded from the LMFDB on 13 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_5415_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_5415_2_a_n();".
// 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_5415_2_a_n();".
// 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_5415_a();"
function MakeCharacter_5415_a()
    N := 5415;
    order := 1;
    char_gens := [3611, 2167, 5056];
    v := [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_5415_a_Hecke(Kf)
    return MakeCharacter_5415_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], [-1, 0], [-1, 0], [0, 1], [2, 3], [2, -1], [4, 2], [0, 0], [2, -4], [0, 1], [6, 2], [2, -1], [-4, -3], [8, -3], [-2, 4], [-8, 0], [-4, 6], [-4, -8], [4, -4], [8, -2], [-2, 4], [0, 0], [10, -2], [4, 7], [14, 3], [-6, -2], [-4, -8], [-8, 8], [-2, -6], [4, 10], [-8, 8], [-10, -1], [10, 0], [0, 10], [-6, 0], [-2, -6], [2, -6], [12, -7], [6, -8], [0, -14], [-8, 6], [18, -2], [6, -3], [2, 5], [4, -2], [4, -10], [4, 8], [4, 16], [2, 12], [0, 16], [-6, -4], [-14, 9], [14, -4], [-6, -3], [-4, -6], [2, 18], [-4, -5], [-8, 2], [-22, 0], [16, 1], [8, 13], [20, 6], [-24, -2], [-2, 3], [-14, -14], [4, -16], [-14, 10], [26, 1], [-2, 18], [18, 0], [-2, -4], [-2, -19], [4, 13], [-2, -1], [-6, -2], [-28, 0], [14, -12], [6, -16], [12, 3], [10, -2], [2, -1], [2, 8], [0, -22], [18, 5], [16, -4], [18, 0], [0, -19], [30, -2], [14, -4], [20, -3], [30, -4], [10, -5], [-12, 6], [-6, -3], [-8, -2], [2, -2], [-12, -9], [-12, -3], [24, 6], [22, 0], [-16, 6], [-14, 8], [2, -20], [-24, 5], [12, 2], [-6, -18], [-18, -4], [14, -16], [20, 12], [6, 10], [-8, 6], [-2, 6], [20, -6], [-12, 6], [0, 0], [-4, -25], [24, -13], [-22, 10], [40, -2], [-4, -16], [18, 18], [6, -27], [0, 12], [-24, -4], [44, -2], [18, 8], [12, -12], [6, 5], [12, -25], [-22, 0], [20, 4], [4, 0], [10, -10], [10, 30], [10, -24], [26, -12], [20, -4], [40, -2], [0, -10], [-2, 0], [-20, -8], [6, -6], [16, 29], [14, 8], [-38, 6], [24, 10], [-14, 2], [-24, -8], [-16, 24], [-20, -8], [42, 5], [14, 14], [12, 17], [8, 24], [4, -10], [-20, -8], [-12, 0], [2, -22], [-10, 14], [4, 19], [-50, -2], [28, -18], [-16, -17], [-28, -4], [-4, 20], [2, -16], [-8, -4], [10, 34], [18, -12], [26, -12], [-24, -18], [-22, 0], [-30, 5], [2, -28], [24, 0], [50, -6], [-28, -6], [-42, -14], [16, 2], [18, 0], [12, 23], [4, 8], [-2, 9], [8, 14], [6, -20], [-26, -16], [14, -13], [0, 18], [6, 6], [26, -5], [-6, 35], [-34, -4], [-52, -4], [-16, -13], [-2, 20], [20, 2], [-28, -12], [30, -8], [0, 6], [2, -16], [-8, -41], [32, 16], [38, -3], [22, 24], [-26, 3], [36, 18], [0, -28], [-26, 4], [-6, 38], [-36, -8], [-34, 6], [22, 10], [12, 13], [10, -32], [44, -6], [-26, 26], [20, 17], [36, 25], [-18, 0], [-24, -30], [-50, -4], [2, -42], [28, -25], [-24, 11], [10, -44], [42, 0], [8, 14], [-40, 10], [60, -4], [14, -37], [-2, 30], [-34, -22], [8, 4], [36, -9], [-12, 27], [6, -8], [-8, 16], [-18, 20], [-10, 35], [28, -18], [-24, 24], [-28, 20], [12, 9], [-54, -6], [0, 28], [-2, -37], [-16, -17], [4, 8], [-18, 2], [34, -18], [-22, -14], [18, -12], [30, -10], [-26, -10], [24, -26], [-26, -11], [-24, 32], [-32, 8], [-44, 0], [18, 0], [28, -4], [20, -28], [-14, 8], [2, 33], [40, 22], [54, 6], [0, 15], [18, -18], [36, 4], [-24, 30], [-14, -28], [-4, -44], [22, 38], [36, -6], [34, -27], [-8, 3], [2, 2], [46, -10], [-46, 2], [14, -35], [12, -20], [-4, 36], [42, 0], [26, 0], [0, -41], [30, 31], [2, -16], [40, 12], [-72, 4], [24, -3], [-34, -6], [-38, 6], [20, 24], [-32, 2], [-42, 3], [-50, -6], [0, 20], [-12, -10], [-56, -4], [20, -9], [-2, -12], [-48, 4], [-28, -32], [-42, -20], [8, -14], [26, -23], [-20, 12], [-18, 12], [30, -37], [18, -18], [-46, -8], [-6, 36], [4, 23], [-16, -46], [50, 18], [14, 0], [-34, -13], [-32, 28], [-2, 46], [-6, -4], [-34, -18], [-54, 6], [32, -17], [16, -42], [20, 18], [30, 0], [-36, 12], [24, 42], [-30, -36], [12, -50], [2, 24], [32, 24], [8, 26], [46, -2], [16, -30], [-6, -22], [30, -8], [4, -36], [-36, 12], [60, 13], [26, -11], [30, 16], [16, 27], [54, -10], [16, -16], [48, 0], [12, -16], [24, 24], [-60, 4], [46, -8], [34, 22], [6, -15], [-2, -34], [40, -30], [-22, 22], [44, -8], [-18, -29], [-26, 19], [8, -18], [12, -16], [22, -4], [30, 4], [-60, -4], [-12, -30], [-20, -31], [54, -17], [26, 12], [28, 42], [6, -18], [34, 3], [84, 0], [-6, 6], [64, 23], [-28, 32], [22, -30], [8, 36], [-22, -21], [-22, -4], [-54, 6], [-6, -3], [8, 35], [0, -10], [20, 15], [10, 52], [-50, -22], [-82, 4], [-20, -38], [-30, -16], [-20, 27], [-34, -12], [-10, -30], [-48, 12], [50, 15], [-44, -17], [-40, -18], [-46, -21], [-26, -18], [-32, -3], [34, 10], [22, 4], [-34, 44], [-8, -2], [44, 2], [-4, 34], [20, -31], [-60, 0], [26, -14], [-56, -31], [-24, 18], [-14, 23], [10, -61], [-52, -14], [20, -36], [-52, -6], [62, 24], [-30, -4], [32, -20], [-20, -4], [38, 32], [-28, -48], [16, 25], [-46, -39], [-30, 16], [-48, -8], [-78, -3], [-24, -18], [-24, 8], [-86, -8], [52, -22], [34, 3]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5415_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_5415_2_a_n();".
// 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_5415_2_a_n(:prec:=2)
    chi := MakeCharacter_5415_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_5415_2_a_n();".
// 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_5415_2_a_n( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_5415_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![-1, 2, 1]>,<7,R![-2, 0, 1]>,<11,R![-14, -4, 1]>,<13,R![2, -4, 1]>],Snew);
    return Vf;
end function;