
// Make newform 2366.2.a.g in Magma, downloaded from the LMFDB on 25 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_g();".
// 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_g();".
// 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)
    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], [3], [-3], [1], [4], [0], [2], [-4], [-1], [-2], [10], [4], [12], [0], [-10], [-4], [9], [13], [2], [-15], [-2], [4], [-8], [14], [2], [-18], [6], [10], [12], [-2], [15], [5], [3], [-8], [4], [3], [-2], [-8], [-16], [-9], [10], [-5], [8], [13], [-12], [-14], [28], [12], [-21], [-26], [-3], [9], [12], [-7], [-14], [9], [-17], [2], [14], [2], [3], [-18], [-7], [2], [0], [-4], [30], [-18], [4], [-13], [-18], [-13], [-10], [-8], [-4], [-18], [2], [-9], [-10], [-30], [-24], [10], [3], [20], [-2], [6], [-15], [5], [3], [-27], [-33], [2], [7], [-20], [-2], [2], [-9], [16], [-43], [-30], [16], [-24], [-28], [27], [-32], [-4], [9], [20], [-9], [26], [34], [22], [45], [-39], [1], [-9], [-45], [36], [-6], [-10], [25], [30], [51], [36], [-27], [0], [-46], [-36], [-16], [-43], [44], [0], [-11], [-48], [-12], [-46], [46], [-25], [-9], [6], [31], [48], [-9], [12], [33], [2], [-49], [38], [44], [-21], [-14], [30], [48], [-24], [14], [-23], [-11], [-2], [16], [-15], [38], [-39], [56], [-15], [31], [-14], [5], [-27], [-39], [-45], [40], [39], [57], [9], [16], [-44], [-46], [48], [14], [-15], [37], [-28], [20], [30], [-51], [35], [32], [1], [-5], [-24], [8], [-36], [-46], [-22], [-12], [-2], [-22], [64], [12], [24], [12], [54], [-34], [-46], [60], [-59], [-62], [-24], [15], [41], [51], [-2], [-16], [-37], [-26], [-40], [23], [-4], [-31], [10], [36], [50], [50], [-32], [-51], [26], [60], [33], [28], [-14], [-12], [-4], [28], [43], [21], [-34], [14], [-64], [-42], [-6], [-58], [-20], [-4], [52], [54], [22], [-72], [75], [26], [-33], [48], [70], [-3], [-50], [14], [0], [20], [-7], [33], [-48], [-1], [38], [38], [54], [-8], [-75], [14], [50], [0], [30], [-29], [-68], [-68], [13], [-70], [42], [-34], [-38], [43], [-8], [44], [-32], [-72], [45], [51], [48], [9], [-26], [64], [6], [2], [-40], [18], [12], [-26], [15], [-38], [47], [17], [67], [16], [48], [39], [-72], [-42], [-2], [12], [61], [77], [76], [32], [-64], [-49], [40], [-3], [28], [16], [75], [-50], [-63], [-59], [43], [85], [-51], [-23], [-40], [48], [85], [48], [-66], [62], [-68], [50], [-28], [-34], [19], [22], [48], [-26], [38], [28], [-9], [-78], [-1], [36], [-12], [67], [-60], [90], [-58], [-4], [-87], [-28], [-34], [-34], [0], [60], [43], [2], [-9], [-54], [78], [-16], [16], [-28], [22], [67], [73], [-47], [96], [-35], [43], [-36], [56], [-50], [-4], [-24], [80], [4], [22], [26], [29], [-87], [39], [-41], [36], [-23], [-7], [4], [2], [17], [-15], [66], [25], [48], [-65], [-34], [50], [50], [26], [-59], [-15], [25], [60], [2], [20], [-32], [-43], [-60], [-51], [62], [26], [-90], [-30], [-67], [-77], [15], [28], [-54], [-100], [-36], [9], [-40], [45], [-86], [41], [36], [-95], [73], [48]];
    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_g();".
// 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_g(: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_g();".
// 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_g( : 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, 1]>,<5,R![3, 1]>],Snew);
    return Vf;
end function;