
// Make newform 2366.2.a.j in Magma, downloaded from the LMFDB on 11 July 2026.
// To make the character of type GrpDrchElt, type "MakeCharacter_2366_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_2366_2_a_j();".
// 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_2366_2_a_j();".
// 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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_2366_a();"
function MakeCharacter_2366_a()
    N := 2366;
    order := 1;
    char_gens := [339, 2199];
    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_2366_a_Hecke( : Kf := Kf)
    return MakeCharacter_2366_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], [-2], [0], [-1], [0], [0], [6], [-2], [0], [-6], [4], [-2], [-6], [8], [12], [6], [6], [8], [4], [0], [-2], [8], [6], [6], [10], [0], [-4], [12], [-2], [6], [-16], [18], [-18], [14], [18], [-8], [-4], [16], [12], [-12], [-12], [20], [24], [-14], [18], [20], [-4], [-8], [-18], [4], [-6], [-24], [10], [-18], [18], [0], [-12], [16], [-10], [6], [-22], [-24], [-2], [-24], [-10], [-6], [-8], [14], [-24], [28], [-18], [24], [8], [14], [16], [-36], [18], [-20], [18], [-14], [6], [10], [-24], [-34], [8], [-12], [-18], [10], [-12], [-32], [-6], [36], [16], [-12], [4], [0], [-36], [6], [2], [-38], [8], [-6], [30], [6], [32], [-2], [42], [6], [-24], [26], [32], [-2], [-6], [-26], [16], [-18], [-14], [-12], [18], [-24], [40], [26], [-12], [12], [46], [18], [46], [12], [44], [40], [16], [-24], [-40], [2], [18], [-14], [-24], [22], [-12], [6], [-2], [-6], [-40], [36], [56], [-12], [-44], [-18], [14], [24], [22], [-54], [20], [-36], [44], [48], [56], [-6], [2], [24], [-24], [-54], [-32], [-6], [6], [36], [-16], [8], [34], [-36], [-36], [4], [0], [-26], [-4], [-30], [-44], [30], [-16], [8], [-8], [30], [-22], [6], [-48], [36], [-34], [46], [-50], [12], [2], [60], [20], [-60], [-12], [66], [-14], [26], [6], [-24], [-30], [-28], [40], [26], [-18], [24], [-20], [0], [-30], [-58], [-34], [-48], [8], [-42], [24], [-62], [32], [-18], [24], [30], [-22], [40], [-18], [-56], [-30], [26], [42], [24], [-64], [48], [50], [38], [-32], [-6], [-46], [0], [58], [-30], [-12], [0], [72], [2], [32], [34], [-42], [-12], [16], [-66], [40], [0], [58], [-66], [-48], [26], [-72], [60], [74], [10], [0], [34], [8], [48], [-32], [56], [42], [-10], [-54], [6], [4], [-6], [-76], [-56], [46], [32], [50], [16], [-72], [10], [34], [42], [-24], [40], [12], [-8], [-50], [48], [2], [-78], [-44], [18], [-18], [42], [-54], [30], [62], [84], [-64], [-60], [30], [82], [14], [78], [-16], [-48], [-40], [70], [-72], [32], [0], [38], [24], [30], [42], [32], [0], [-70], [-30], [-48], [-26], [-30], [-10], [-50], [-72], [-56], [-54], [-34], [88], [46], [-48], [-54], [10], [-78], [-28], [-54], [-92], [-18], [-2], [-18], [22], [20], [82], [-42], [12], [-64], [30], [-72], [20], [-80], [24], [-24], [-14], [70], [66], [-28], [14], [18], [36], [-42], [18], [-48], [-62], [-30], [-48], [-60], [2], [-10], [-84], [16], [38], [36], [-34], [0], [30], [-40], [38], [78], [72], [70], [-42], [-82], [-36], [-6], [-56], [-18], [-14], [-24], [-8], [32], [-20], [-12], [-62], [48], [-60], [-10], [-24], [-94], [52], [-42], [-4], [-66], [40], [-78], [-32], [18], [-60], [92], [-62], [-30], [-86], [66], [26], [42], [0], [44], [-22], [12], [24], [44], [-54], [0], [18], [-56], [96], [6], [-38], [96], [24], [54], [-38], [36]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2366_a_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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