
// Make newform 1218.2.a.n in Magma, downloaded from the LMFDB on 28 September 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_1218_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_1218_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_1218_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 := [-4, -1, 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_1218_a();"
function MakeCharacter_1218_a()
    N := 1218;
    order := 1;
    char_gens := [407, 871, 379];
    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_1218_a_Hecke( : Kf := Kf)
    return MakeCharacter_1218_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]];
    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, 0], [1, 0], [-1, 1], [1, 1], [2, 0], [3, -3], [3, -3], [1, 0], [-4, 4], [1, 1], [4, -2], [2, -2], [1, 3], [-1, 3], [-3, 3], [12, -2], [-7, -1], [0, -4], [5, -3], [-8, 4], [-3, -5], [12, -2], [7, -5], [10, -4], [-3, 7], [-6, 6], [-8, -2], [-7, 5], [-5, -3], [10, 2], [-1, -1], [-12, 0], [3, -1], [-14, 6], [-2, 4], [-14, -2], [-12, 4], [10, 0], [2, -2], [-10, 8], [-2, 2], [-18, 4], [-2, 0], [-19, -1], [-6, -2], [5, -9], [12, -8], [-6, -8], [0, -6], [1, -9], [-18, 4], [-22, 2], [-10, 8], [12, -4], [9, 1], [4, 4], [8, -10], [-4, -2], [0, -12], [1, 9], [1, -1], [-8, 8], [2, 0], [14, 0], [-14, 6], [8, -6], [30, 2], [14, 8], [-10, 8], [0, 0], [4, 4], [-18, 8], [-8, 0], [16, -8], [8, 6], [11, 7], [18, -8], [-2, -8], [-4, 0], [-2, 0], [3, 5], [2, -4], [15, -3], [-7, 7], [-5, 11], [-13, 11], [11, -17], [-8, 16], [32, 4], [7, 5], [4, -4], [11, 5], [-25, -7], [-13, -7], [-26, 4], [5, -3], [6, -2], [-41, 3], [19, -3], [17, 1], [-14, -6], [-13, -5], [15, 1], [-17, 11], [-28, 0], [3, -1], [-4, -12], [-17, 3], [-2, 10], [20, 10], [30, -4], [27, -3], [8, -16], [-2, -12], [20, -8], [-2, -6], [8, 14], [16, -4], [-33, 3], [5, -7], [9, -7], [-34, 2], [24, -4], [14, 0], [34, -12], [-8, 16], [22, 2], [16, -14], [4, 4], [-8, -8], [-5, -11], [22, 0], [-23, 9], [2, 4], [14, 0], [38, 6], [6, -8], [-10, 20], [14, 6], [-7, 17], [21, -5], [23, -7], [-6, 0], [-48, 0], [24, -14], [5, -19], [-23, 15], [35, -11], [16, 6], [16, 2], [25, 7], [11, -15], [6, 18], [-4, 20], [-18, -6], [-30, -4], [-2, -20], [2, -8], [8, -4], [10, 0], [19, 13], [24, -4], [-10, -12], [-28, 4], [34, -2], [34, 8], [-3, 1], [-9, 11], [38, -6], [-18, -8], [-45, 5], [-8, 18], [-16, 8], [27, -17], [11, 5], [-6, -4], [-18, 10], [-23, 1], [4, 16], [-32, -12], [-28, -6], [9, 1], [-56, -4], [20, 6], [18, 4], [13, -5], [22, -20], [-26, 10], [-8, 18], [11, 21], [0, 8], [3, -9], [24, -12], [9, 1], [-4, 6], [28, -6], [6, 8], [-14, 6], [-16, 6], [33, 3], [-22, -16], [-19, -3], [12, 0], [-24, 26], [-12, 12], [2, 14], [18, 8], [31, -7], [0, 2], [-2, 20], [-3, 3], [28, -16], [-19, 23], [0, -6], [-34, 10], [-10, -12], [29, 3], [-8, -18], [-21, -17], [-25, -3], [-2, -12], [-15, -9], [-35, 19], [1, 1], [-22, -8], [2, -2], [-4, 4], [-50, -6], [-58, 8], [10, 2], [4, -24], [-1, 15], [46, 6], [-8, -16], [-46, 12], [48, -2], [-30, 6], [-11, -9], [31, 9], [40, -12], [-24, 24], [-33, 27], [-46, 4], [-14, 30], [49, -13], [49, 7], [28, -8], [-18, 34], [-65, -5], [27, 7], [10, 14], [47, -13], [27, -9], [-4, 28], [-17, -21], [-12, 0], [74, 0], [26, -8], [-37, 5], [68, 0], [27, -9], [38, -28], [32, 2], [20, 16], [-16, 6], [-20, 30], [22, -14], [19, -25], [-18, 8], [-4, -8], [-28, 30], [-56, 12], [-17, -5], [50, -2], [-47, -1], [-34, 0], [48, 2], [23, 9], [-46, -2], [48, 0], [-6, -18], [5, 29], [12, 16], [60, -12], [-34, 4], [38, 16], [0, -8], [43, -25], [-45, 23], [51, -11], [-32, -6], [-52, -8], [-30, -12], [-6, 0], [18, -2], [4, -10], [7, -7], [48, 4], [-26, 8], [39, 11], [-33, 25], [-31, 7], [34, -10], [35, 19], [17, 15], [-21, 15], [29, -21], [68, -6], [12, -20], [-38, 12], [-4, 6], [19, -27], [-72, 4], [0, -22], [24, 12], [-43, 27], [14, -28], [21, -23], [12, 8], [-7, -3], [-34, 12], [45, 11], [-16, -14], [25, 9], [8, 20], [54, 14], [22, 2], [-49, -5], [11, -1], [6, -8], [-25, -11], [-46, 10], [14, -22], [39, 9], [-10, 8], [-34, 12], [-66, 0], [11, 1], [-1, -29], [48, -6], [33, 17], [-36, 4], [-5, 23], [43, -11], [-34, -20], [0, -8], [-9, 5], [-7, 17], [-6, -30], [-5, 3], [16, -26], [55, -3], [29, -3], [-20, 14], [0, -8], [13, -5], [59, -5], [27, -19], [2, 20], [-1, -5], [-20, -4], [30, 2], [9, 23], [48, -6], [-62, 4], [-14, 38], [22, 4], [80, -12], [16, -28], [21, -21], [-26, 0], [5, -17], [60, -6], [22, 6], [-8, -8], [-18, -8], [-3, -3], [-46, 8], [15, 27], [-24, 26], [21, 19], [54, 12], [-2, 22], [-36, -20], [-65, 9], [-1, -13], [-44, -12], [45, -1], [-40, 26], [-4, -2], [37, 3], [-92, 0], [-10, 2], [-53, 3], [-60, -12], [8, 2], [49, -17], [20, -38], [26, -4], [-19, 1], [3, -35], [-15, -31], [26, 0], [-56, -8], [-44, -14], [37, -7], [57, 15], [27, 13], [-30, 8], [27, -17], [-52, -20], [25, -1], [40, 18], [-10, 8], [-8, 8], [65, 11], [10, 0], [-9, -11], [-53, 7], [46, -20], [80, 0], [-44, 24], [30, 28], [0, -22], [54, -6], [39, -25], [36, -16], [43, 1]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1218_a_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1218_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_1218_2_a_n(:prec:=2)
    chi := MakeCharacter_1218_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_1218_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_1218_2_a_n( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1218_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![-2, 1]>,<11,R![-4, 1, 1]>,<13,R![-2, -3, 1]>],Snew);
    return Vf;
end function;