
// Make newform 2366.2.a.bb in Magma, downloaded from the LMFDB on 19 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_bb();".
// 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_bb();".
// 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 := [1, -2, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-2, 0, 1]];
    Rf_basisdens := [1, 1, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    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], [0, -1, -1], [-3, 0, -1], [-1, 0, 0], [-2, -1, 2], [0, 0, 0], [1, 5, -2], [-1, -1, 3], [0, 2, -1], [6, -6, 7], [-9, 2, -1], [-1, 2, -5], [-2, 4, -5], [-3, -1, -1], [-6, 1, 0], [-2, -3, 1], [4, -5, 4], [-2, -3, -4], [1, 5, 3], [-11, 5, -4], [-1, 3, 1], [8, -5, 9], [8, -8, 9], [-1, 2, 1], [10, -2, 1], [3, 4, -1], [-7, -1, -7], [0, -11, 8], [-8, 6, -5], [-3, 8, -7], [-7, 11, -9], [-3, 3, -2], [11, -10, 6], [8, 4, -4], [10, -5, 2], [-5, -3, -4], [-10, 8, 7], [5, -14, -1], [-2, 7, -6], [-3, -11, 2], [-13, 10, -14], [-2, -10, 2], [0, 2, -9], [-10, 3, 2], [9, -9, -5], [-11, 0, -6], [1, -9, 4], [8, -11, 13], [4, 8, -1], [-15, 7, 1], [6, -17, 6], [7, -1, -13], [-12, -3, 5], [16, -12, 13], [13, -4, 9], [-3, 14, 5], [-12, 10, 8], [-8, -7, 2], [17, -11, 8], [8, -1, -14], [-5, 7, 0], [-2, 15, 2], [-13, -9, 3], [-11, 0, 0], [-29, 5, -11], [2, 3, 12], [8, -1, -5], [2, -3, 4], [7, -1, 0], [-20, -4, -1], [14, 7, 5], [1, -10, -7], [-5, -3, -5], [23, -13, 16], [-4, -1, -4], [-2, 6, -9], [-13, 16, -1], [-13, 19, -5], [17, 5, -5], [6, -24, 10], [-20, 8, -17], [-20, -4, 5], [16, -18, 18], [7, -21, 9], [-17, 5, 0], [-24, 1, 2], [8, 12, -6], [-12, -4, -14], [0, 10, 12], [-20, 14, -3], [-27, 5, -16], [9, 0, 4], [-17, 8, -2], [-6, 4, 0], [-18, -4, -14], [-9, 14, -10], [-6, 6, -19], [1, 3, 12], [-13, -10, 0], [-15, 0, -1], [2, 9, -13], [12, -5, -11], [21, 17, -5], [12, -15, 16], [-8, 0, -9], [-19, 22, -21], [19, -8, -8], [21, -4, 20], [3, -19, 12], [6, 14, -5], [-7, -17, 15], [17, 2, -12], [-6, 0, -13], [20, -27, 18], [-15, 5, -2], [-17, 6, -7], [6, 11, 12], [2, -20, 19], [-21, 9, 2], [-17, 10, -7], [7, -12, -18], [15, 1, 11], [-16, 12, -29], [6, -22, -4], [5, 3, 26], [13, -6, -16], [-26, 1, -5], [-23, 23, -21], [-4, -5, 19], [9, -9, 31], [-10, -10, 5], [-25, 6, -12], [13, -8, 13], [24, -9, 1], [-5, -20, 11], [20, -6, -3], [-3, -24, -7], [-7, 13, -27], [0, -4, 19], [15, -31, 25], [2, 24, -15], [11, -2, 24], [33, -13, 33], [-5, 9, -3], [-29, -5, 11], [11, -1, 6], [-19, 24, -21], [-5, 18, -22], [22, -24, 32], [-16, 8, 0], [1, 19, 16], [26, -29, 13], [-23, 17, -25], [-13, 21, 8], [6, 32, -7], [-11, 0, 2], [-37, 8, -9], [0, 11, 3], [-11, -15, 1], [-4, 9, 23], [-8, 22, -10], [15, 1, 3], [29, -35, 40], [-8, -17, -3], [37, -14, 26], [-4, -19, 20], [-21, 20, -3], [9, -36, 27], [-7, 7, -2], [-23, -21, 4], [-1, 16, -5], [-14, -8, -2], [-43, 24, -48], [1, 3, 1], [-24, 39, -24], [-20, 8, -5], [-18, 15, 12], [-4, 16, -2], [-30, 33, -19], [18, -13, 12], [-26, 29, -40], [-11, 16, -26], [43, -19, 42], [19, 7, 16], [-22, -16, -5], [20, -5, 21], [-7, 9, -2], [18, 7, -2], [-8, -25, 20], [-50, 9, -10], [-2, 9, -8], [-12, 23, -3], [0, -23, 7], [-19, 27, -15], [-22, -6, 4], [20, -19, -8], [-36, 11, -15], [25, 17, 2], [5, -1, -15], [6, -26, 23], [24, 12, -14], [2, -3, 31], [25, -36, 26], [-24, -3, -26], [7, 34, -26], [-15, -3, -5], [1, 28, 6], [-5, 6, 4], [-30, -17, 2], [-17, 19, -37], [-16, 2, -21], [-8, 10, -22], [10, 5, -17], [42, -27, 35], [-12, -23, 6], [-22, 28, -5], [-2, 34, -11], [10, -19, -18], [1, 6, -2], [-44, 33, -52], [27, -6, -24], [9, -31, 3], [-21, 31, -23], [34, -5, 13], [24, -1, -1], [-44, -3, 2], [-40, -2, 17], [20, -10, 8], [-9, 31, 18], [-11, -9, -13], [13, -23, 10], [-12, -11, 17], [-13, 45, -1], [27, -8, 15], [10, 27, -10], [12, -27, -2], [12, 2, 27], [-3, 18, 26], [-11, -7, -20], [19, 0, 15], [-23, -16, -2], [33, 8, -11], [-10, -13, 25], [-7, 7, -2], [-18, -11, -6], [30, -22, 12], [37, -11, 19], [7, 2, -7], [14, 28, -1], [32, -24, 53], [-2, 20, -30], [11, -3, 12], [3, 30, -11], [27, 20, -1], [-23, 3, -9], [23, -27, -1], [-15, 26, 31], [13, -13, 1], [-6, 11, -40], [-53, 29, -37], [-1, -29, -21], [55, -33, 47], [-33, -25, 10], [-38, -16, -6], [6, -16, -17], [35, 1, -5], [53, -21, 44], [12, -43, 4], [-39, -10, -5], [15, -36, 25], [-1, 16, 30], [1, 2, 5], [8, -7, -33], [18, 1, 36], [-33, -10, 25], [8, -11, -21], [36, -12, 29], [6, -22, 27], [-10, 8, 42], [-12, 26, -5], [-25, 48, -7], [25, -19, 37], [-53, 25, -6], [33, -8, -30], [-23, -13, -4], [-25, 29, -28], [1, 40, -29], [12, 15, -23], [-14, -7, 20], [38, -16, -20], [7, 40, -29], [-27, 3, -18], [-41, 6, -29], [1, -27, -20], [5, 4, 18], [19, -18, 48], [34, 12, 5], [9, -53, 30], [-17, 3, 11], [14, -21, 29], [61, -33, 43], [-18, 15, 6], [2, 10, -38], [-38, 15, 11], [-3, -18, -22], [8, -13, 1], [17, 18, 25], [10, -40, -9], [-4, 6, 26], [11, -22, 28], [6, -6, -16], [42, -27, -28], [19, -32, -22], [-7, -29, -5], [-46, -5, 11], [-24, 24, -27], [2, -1, -17], [27, 21, -5], [8, 53, -26], [-30, 40, -46], [51, -24, 2], [-30, -15, 12], [27, 21, -23], [-3, 5, -26], [16, -21, 23], [-21, 25, -19], [-7, 20, 6], [31, -7, 31], [34, -18, -20], [39, -19, 21], [1, 19, -19], [18, -17, 21], [13, -4, 7], [-42, 20, -45], [-12, -9, -23], [-28, -17, -10], [48, -34, -1], [-15, -11, 0], [29, 4, -30], [-53, 19, -23], [-62, 27, -42], [27, -9, 34], [-33, 10, -7], [49, -1, -3], [-10, 17, 15], [48, -22, -1], [-45, 23, -44], [5, 44, -11], [51, 5, 0], [-3, 0, 7], [38, -49, 5], [73, -27, 47], [51, 2, 7], [20, -19, 6], [32, -18, -24], [48, 2, -4], [47, -25, 27], [15, -8, 49], [45, 4, -19], [-15, 2, 23], [27, -46, 44], [43, -11, 6], [-11, -21, -27], [47, -3, -4], [-62, 19, -20], [13, 20, -24], [5, -4, 10], [16, 9, 2], [-40, 27, 19], [26, 4, 51], [-14, 1, -35], [-21, 24, 20], [21, -32, 13], [74, -30, 40], [51, -32, 42], [-3, 11, 13], [43, -10, 17], [-20, 38, -23], [71, 7, 6], [-18, -11, -10], [-34, -37, 17], [41, -22, 5], [43, 10, 15], [31, -43, 40], [-44, 2, 2], [-40, -9, 10], [-58, -14, 12], [13, 12, 18], [13, -20, 21], [8, -42, -7], [7, -20, 20], [-2, 10, 0], [1, -19, -20], [-8, -9, -49], [-9, 18, -30], [49, -32, 29], [8, -6, 23], [-27, 33, -44], [-19, 39, -44], [53, -19, -18], [-26, 68, -43], [-28, 8, -48], [-3, 19, -41], [-21, 0, -26], [31, 1, -20], [-33, 56, -61], [23, 6, -18], [8, 51, -28], [31, -45, 3], [-39, 32, 40], [-34, 35, -57], [6, 26, 27], [-38, 22, 24], [29, -17, -29], [-19, -33, 11], [-13, -6, -1], [-79, -2, -16], [60, -13, 14], [56, -33, -14], [23, -28, -13], [35, 9, 12], [46, -38, 1], [-24, 1, -21], [-28, 42, -2], [-59, -11, 18], [-26, 11, -44], [25, 0, -4], [-47, 42, -7], [68, -25, -15], [-17, 43, -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_bb();".
// 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_bb(:prec:=3)
    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_bb();".
// 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_bb( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2366_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![7, -7, 0, 1]>,<5,R![13, 19, 8, 1]>],Snew);
    return Vf;
end function;