
// Make newform 2358.2.a.w in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_2358_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_2358_2_a_w();".
// 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_2358_2_a_w();".
// 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_2358_a();"
function MakeCharacter_2358_a()
    N := 2358;
    order := 1;
    char_gens := [263, 919];
    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_2358_a_Hecke(Kf)
    return MakeCharacter_2358_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], [2], [2], [3], [3], [5], [1], [-4], [-9], [-5], [-8], [12], [-6], [8], [-12], [5], [-3], [0], [8], [-2], [-8], [14], [14], [12], [-14], [-8], [-3], [-13], [-4], [15], [1], [7], [12], [-9], [-12], [2], [-1], [-21], [11], [4], [20], [5], [19], [-6], [-17], [2], [-19], [6], [20], [-20], [-8], [8], [22], [-26], [21], [8], [4], [3], [10], [-14], [-29], [10], [33], [8], [-18], [-17], [1], [30], [-30], [-28], [14], [-20], [10], [10], [9], [21], [-14], [17], [-22], [-12], [-27], [-28], [6], [10], [-6], [-23], [-10], [6], [13], [-25], [12], [28], [-18], [40], [2], [6], [-27], [-23], [10], [-1], [-24], [24], [22], [23], [5], [33], [-19], [-12], [-19], [44], [-3], [-38], [-31], [26], [30], [21], [31], [38], [-13], [-20], [16], [-30], [-41], [8], [-50], [18], [-8], [40], [6], [-10], [12], [37], [19], [13], [-17], [53], [-4], [30], [-26], [-18], [-20], [47], [-33], [-15], [29], [-44], [45], [-20], [-5], [-42], [50], [-17], [24], [-34], [-31], [25], [-14], [17], [49], [40], [-8], [33], [-20], [-12], [0], [-46], [35], [0], [21], [-35], [24], [-12], [-10], [13], [36], [38], [-6], [-58], [10], [-44], [-7], [-23], [18], [12], [-38], [-50], [4], [59], [10], [-5], [32], [-8], [15], [-58], [45], [36], [-47], [18], [-3], [-37], [12], [43], [4], [21], [-37], [32], [14], [39], [28], [-14], [-2], [21], [54], [23], [-29], [0], [-49], [28], [-46], [10], [-64], [-6], [-46], [67], [4], [-64], [-5], [59], [-4], [51], [-53], [-65], [3], [51], [33], [53], [6], [15], [6], [0], [39], [38], [-70], [54], [-42], [37], [10], [-46], [64], [22], [6], [72], [-10], [-34], [24], [58], [76], [42], [2], [-56], [-74], [44], [22], [-46], [-4], [50], [-15], [-42], [51], [26], [22], [-60], [-72], [77], [-34], [-65], [-72], [-34], [37], [30], [-15], [64], [-26], [32], [18], [37], [32], [46], [60], [-57], [2], [34], [-74], [-46], [-2], [-64], [-69], [10], [39], [-42], [37], [20], [-15], [-32], [-83], [-53], [77], [-31], [6], [-8], [-37], [-54], [-31], [44], [-22], [84], [-12], [-86], [-20], [8], [57], [2], [65], [-53], [-29], [60], [60], [-60], [62], [-29], [47], [56], [36], [-11], [-6], [-2], [8], [-4], [-84], [36], [43], [-14], [-64], [39], [84], [29], [-96], [-60], [-31], [-50], [38], [-62], [-70], [-58], [32], [8], [3], [60], [69], [-20], [-10], [-53], [24], [-28], [86], [15], [31], [-72], [-54], [83], [-8], [52], [0], [28], [-80], [23], [10], [-58], [42], [2], [2], [14], [33], [-66], [31], [-15], [56], [92], [-24], [6], [14], [-74], [-23], [-64], [-96], [-50], [-54], [-44], [48], [-37], [93], [32], [-57], [-82], [-27], [86], [93], [4], [-90], [-62], [85], [-44], [-80], [18], [-30], [-90], [-48], [18], [-9], [36], [-43], [-15], [102], [94], [-42], [60], [66], [66], [45]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2358_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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