
// Make newform 2366.2.a.r in Magma, downloaded from the LMFDB on 23 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_r();".
// 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_r();".
// 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], [0, 1], [-2, -1], [1, 0], [2, -2], [0, 0], [0, 2], [0, 2], [5, -2], [2, -4], [-4, -2], [6, 2], [-6, 2], [4, 0], [2, 4], [0, 4], [-8, -3], [-2, -3], [2, 4], [-1, -2], [4, 2], [8, 4], [0, 6], [12, 2], [10, 4], [16, 0], [10, -4], [-4, 6], [14, -2], [2, 0], [1, -6], [-4, 3], [-3, 0], [16, -2], [-6, -6], [3, 6], [-4, -8], [10, 6], [6, 6], [-10, 3], [0, 2], [6, -11], [-8, 0], [-11, 0], [0, -12], [8, -2], [20, -4], [6, -10], [-24, 1], [12, 0], [13, -8], [-3, -10], [12, 0], [12, -7], [6, 8], [9, 2], [-6, -11], [2, -12], [-12, -2], [14, 0], [4, -5], [8, -8], [-8, -3], [-6, 8], [20, 4], [12, 4], [-2, 8], [2, 8], [6, 14], [-22, 1], [-4, -2], [15, 2], [-10, 4], [-4, 4], [8, 4], [-12, 10], [6, -4], [-14, 7], [6, 8], [-22, -4], [0, -10], [2, 8], [3, -2], [-8, 0], [2, -8], [0, -2], [11, -4], [29, 0], [-18, -1], [11, 6], [-16, 11], [0, -18], [7, 6], [-20, 8], [-28, 6], [-14, -4], [-10, -5], [30, -2], [12, 3], [-20, -6], [-14, -6], [12, 4], [-4, 6], [13, -8], [0, 0], [-24, 4], [4, -1], [22, -6], [-5, -6], [-26, 12], [0, -6], [28, 2], [17, -4], [-4, 9], [11, -14], [13, 12], [4, -13], [4, -4], [12, 14], [-6, -16], [-2, 17], [10, -20], [6, 5], [2, 18], [-4, -11], [6, 2], [-44, -2], [2, -2], [-2, 10], [-14, -23], [-30, 10], [-40, 0], [39, 2], [30, 2], [24, -8], [12, 10], [-44, 4], [-32, -3], [-30, -1], [18, 16], [-32, -3], [10, 14], [27, 10], [-30, 10], [22, 1], [34, -4], [26, -11], [10, -16], [8, -22], [-7, 22], [18, -8], [-24, -2], [-2, -26], [10, -2], [-30, -8], [3, 18], [-15, -10], [-12, 10], [-12, 4], [38, 11], [6, -4], [-19, 4], [28, -12], [-12, 15], [-3, 12], [-2, -24], [21, 18], [14, -11], [13, 4], [14, 21], [0, -24], [-18, -7], [25, 10], [35, -4], [24, 16], [-2, 10], [-8, 26], [26, 2], [-26, 0], [2, 5], [-27, -14], [4, 22], [-8, 12], [46, -8], [29, 10], [-14, -5], [22, -22], [0, -27], [-11, -16], [-12, -4], [-32, 16], [0, 20], [34, 8], [-8, -16], [34, 14], [-12, -22], [-2, 28], [-2, 14], [-20, 12], [4, 4], [-36, -4], [32, 10], [-20, -20], [34, -16], [-16, -22], [6, -9], [-10, 0], [-24, 24], [-9, 32], [12, -7], [5, 16], [0, 0], [8, 0], [-28, -11], [-54, 0], [-4, 24], [-29, 18], [-4, 12], [-37, 2], [0, -30], [-2, 18], [4, 22], [-6, 4], [44, -4], [-24, 7], [50, -12], [-10, 18], [15, -18], [-20, 4], [30, -8], [16, -8], [40, 10], [-24, -12], [-53, 0], [20, 11], [-32, -10], [2, -16], [14, 10], [-18, -8], [-10, -36], [24, 10], [16, 6], [-6, -2], [-26, 26], [28, -6], [18, -4], [-14, -6], [8, 33], [-44, -2], [3, 10], [28, -12], [10, 0], [-13, -10], [56, -6], [52, 12], [2, 30], [-24, 0], [-8, 27], [-30, 21], [14, 6], [1, 18], [-18, 0], [-44, 20], [16, -8], [42, -2], [-8, -7], [-40, -18], [4, -2], [30, -22], [0, -26], [10, -37], [-34, 14], [18, -14], [23, -6], [26, 20], [66, 4], [10, 12], [24, -30], [23, -20], [0, -6], [26, 26], [12, -36], [8, 4], [-14, 17], [-48, -15], [-32, 0], [5, -28], [16, 18], [-14, 10], [-10, -12], [36, 6], [-64, 2], [-30, -8], [52, 14], [-32, -22], [6, 19], [12, -10], [-22, 31], [-20, -31], [-32, -9], [8, 0], [-38, 14], [-9, 14], [38, -10], [10, 12], [46, 12], [-62, 2], [-30, 1], [-19, -22], [-24, 16], [-48, -16], [52, 12], [-21, 36], [-8, 4], [-5, -26], [38, -26], [-48, -14], [-71, -2], [10, -36], [5, -12], [-28, -3], [5, -8], [-18, 15], [-17, -30], [1, 8], [-14, -14], [20, -4], [-48, 1], [28, 4], [20, -22], [20, -6], [-40, 0], [-16, 34], [84, 2], [-38, 4], [-20, 21], [22, 4], [28, 24], [-54, 8], [-34, 16], [0, -12], [-39, -16], [-16, 8], [-53, -6], [4, 36], [-32, -8], [-14, -15], [-8, 12], [16, 26], [0, 40], [56, -2], [-13, 12], [-36, -8], [-52, -10], [-36, -26], [-30, 26], [48, 8], [12, -27], [42, 0], [-9, -18], [20, 30], [6, 24], [-16, -8], [70, -6], [-76, -6], [34, 0], [-6, 35], [29, 38], [-21, 20], [-60, -4], [4, 21], [37, 18], [-48, 4], [-54, 18], [-30, -12], [-12, 22], [0, -32], [10, -2], [32, -12], [-14, 4], [-24, 24], [-23, 4], [-45, 10], [-19, 36], [20, 31], [-60, 20], [-9, -10], [50, 15], [22, 14], [-40, -6], [33, -16], [2, 29], [24, 6], [8, -3], [-36, -28], [7, 8], [-18, -32], [-28, -10], [60, 6], [12, -34], [2, -21], [5, 36], [23, 38], [26, -38], [-12, -12], [32, -20], [2, -22], [-3, -48], [20, -22], [24, -17], [-14, -16], [12, -34], [-40, 18], [-68, -6], [17, 4], [18, -15], [-3, 6], [-62, -6], [36, -26], [-30, -10], [26, 2], [-46, 7], [-36, -28], [12, 13], [-8, -2], [50, -11], [-16, 28], [3, -12], [-28, -1], [52, 24]];
    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_r();".
// 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_r(: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_r();".
// 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_r( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2366_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-3, 0, 1]>,<5,R![1, 4, 1]>],Snew);
    return Vf;
end function;