
// Make newform 2366.2.a.s in Magma, downloaded from the LMFDB on 24 April 2024.
// 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_s();".
// 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_s();".
// 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 := [-3, 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_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)
    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+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], [-1, 1], [-1, 0], [-1, 0], [1, -1], [0, 0], [-4, -1], [-2, 2], [3, -1], [-3, 0], [7, -1], [0, -3], [1, -2], [-7, -3], [-4, 4], [5, -2], [-9, -1], [-10, -1], [3, 5], [0, 8], [-1, -6], [-9, 3], [-9, 3], [-6, 2], [2, 2], [-4, 3], [2, -6], [4, 4], [-10, 0], [1, 6], [-4, 8], [-12, 4], [6, -7], [-9, 5], [12, 3], [-2, -6], [12, 1], [1, -1], [7, -1], [-14, -4], [6, 6], [2, -3], [-5, 7], [18, 3], [14, -4], [-2, -2], [-23, 1], [2, 6], [-3, 1], [14, 6], [6, -6], [-7, -13], [-5, -6], [16, 4], [10, -5], [-5, -9], [-8, -12], [5, -7], [-1, 6], [8, 5], [5, -9], [7, 6], [1, 13], [-5, -3], [-32, 0], [-12, 11], [-13, 5], [-11, 10], [3, 15], [2, 0], [-5, -18], [-6, -6], [1, 19], [-29, -2], [17, 3], [-17, -9], [7, 10], [-26, -2], [14, 11], [-9, -10], [-7, 7], [2, 7], [30, -2], [20, 1], [-21, 1], [18, 10], [18, 0], [-16, 1], [9, -10], [23, -11], [-23, -1], [-27, -3], [-14, -10], [0, -16], [13, -1], [19, 1], [-17, -6], [8, 21], [18, 2], [-10, 1], [-1, -17], [-14, -7], [1, -17], [6, -10], [-2, -2], [3, 18], [-8, -12], [17, -8], [24, 4], [-4, -11], [4, -8], [-10, 15], [28, 3], [-15, -11], [14, -2], [41, 2], [-8, -16], [0, -8], [2, 6], [-12, 12], [5, -4], [1, 12], [14, 8], [10, -2], [-8, 8], [18, -2], [16, -13], [-1, -3], [40, 8], [1, -2], [-10, 2], [-12, 4], [-23, 17], [-34, -6], [-6, 14], [-10, 6], [-22, -14], [-19, -15], [-2, -4], [17, -14], [-33, 7], [-24, -4], [-22, 14], [-9, 17], [-22, 9], [4, 16], [19, 0], [-20, 11], [7, -23], [-15, -17], [-14, 7], [2, -21], [-1, -13], [-4, 4], [-3, -11], [-2, -22], [-20, 12], [17, 22], [40, 9], [6, -10], [-41, -7], [6, 18], [-9, 13], [-10, -6], [12, 7], [-6, -22], [-9, -21], [4, -15], [-42, 0], [8, 17], [-3, 7], [15, -10], [-48, -8], [38, -7], [15, -7], [4, 13], [-14, 14], [2, -13], [-14, -10], [-22, -12], [-1, -15], [-42, 6], [-49, -2], [18, -10], [0, 20], [-26, -21], [22, 2], [8, 8], [12, 11], [24, 16], [-4, -25], [-1, 9], [-8, -16], [-2, 8], [-11, 1], [-24, 0], [16, -3], [11, -12], [31, 10], [4, 24], [28, 3], [-16, 12], [-7, 12], [-2, 9], [-8, -8], [-12, -9], [-17, -21], [-15, -27], [38, 3], [10, 10], [15, -16], [6, -24], [14, -10], [6, -34], [-8, -16], [21, -18], [-1, -9], [-54, 5], [-16, -20], [-20, 0], [-37, 6], [-22, -14], [0, -25], [14, 18], [6, 30], [1, 26], [26, -7], [-12, -20], [-14, 14], [-10, -6], [-5, -6], [16, 0], [-17, 25], [27, -14], [15, 21], [48, 0], [10, -16], [-12, 5], [-24, -20], [19, 9], [-45, 9], [-13, 17], [16, 8], [-10, -3], [-1, -12], [-7, -37], [-69, 3], [4, 8], [45, -13], [0, 8], [-10, -4], [-22, 14], [42, 14], [0, -35], [-54, 4], [-10, 18], [-30, 16], [-58, 6], [62, 8], [3, -38], [-50, -2], [-34, -6], [-19, -6], [24, -5], [17, 12], [7, -11], [34, 4], [15, 18], [6, -22], [1, 14], [2, -9], [-46, -10], [43, 10], [-6, 6], [38, 12], [-23, 17], [-27, 23], [-10, 25], [10, 25], [14, -10], [24, 8], [26, -6], [51, 1], [-15, -22], [4, -16], [72, 0], [3, 12], [-4, 7], [18, 6], [26, -1], [-10, 0], [54, 10], [42, 9], [33, -31], [-14, -42], [-48, -3], [30, 10], [-8, 15], [48, 0], [39, 11], [-20, -15], [52, -7], [-78, -6], [56, -16], [18, -10], [22, -3], [60, -8], [-26, 29], [-33, 1], [-35, -8], [30, -10], [-18, 16], [39, -12], [43, 15], [-6, -34], [8, 15], [-57, 7], [57, -9], [-22, 30], [9, -16], [-1, -3], [40, 16], [2, -3], [-16, 8], [-4, 12], [-6, -13], [-2, 14], [7, 19], [25, -15], [-15, 20], [6, 21], [1, 24], [-26, -22], [40, 28], [-40, -16], [28, 20], [-22, 7], [-2, 46], [-31, 2], [47, 13], [30, 0], [47, -26], [21, 30], [-49, 19], [-6, -32], [-33, 11], [40, 9], [-6, 6], [-3, 25], [48, -12], [58, 18], [-62, 0], [72, -3], [33, 27], [27, -8], [-6, -43], [-14, 38], [42, -6], [29, -14], [28, 8], [-10, 37], [-8, 5], [-30, 34], [39, 9], [6, 22], [22, -26], [-14, 16], [-23, -9], [11, -22], [24, 12], [27, 17], [-34, 26], [65, -2], [23, -27], [29, 12], [-36, -24], [-21, -33], [2, -10], [-30, 33], [-32, 15], [82, 0], [56, -23], [35, 33], [22, 28], [-13, 35], [34, -34], [19, -19], [-26, 28], [5, -17], [29, -37], [64, -7], [-15, -18], [21, -29], [-27, -9], [17, 35], [22, 34], [53, 13], [-50, -4], [71, -1], [2, -3], [-6, -2], [-70, 2], [72, -4], [-57, 22], [27, 14], [-1, 25], [-22, -8], [-28, 23], [15, -17], [30, -14], [6, -27], [-57, -26], [22, -22], [-8, 24], [-69, -4], [-12, -29], [-36, 24], [5, -5], [42, -14], [-5, 1], [-31, 36], [-19, 14], [-78, -2], [3, 37], [-66, -2], [-9, -22], [-81, 11], [-6, -9], [76, -12], [-5, -11]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2366_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_2366_2_a_s();".
// 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_s(:prec:=2)
    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_s();".
// 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_s( : 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, 2, 1]>,<5,R![1, 1]>],Snew);
    return Vf;
end function;