
// Make newform 2394.2.a.z in Magma, downloaded from the LMFDB on 07 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_2394_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_2394_2_a_z();".
// 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_2394_2_a_z();".
// 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, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [-1, 2]];
    Rf_basisdens := [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_2394_a();"
function MakeCharacter_2394_a()
    N := 2394;
    order := 1;
    char_gens := [533, 1711, 1009];
    v := [1, 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_2394_a_Hecke(Kf)
    return MakeCharacter_2394_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], [1, 0], [2, 0], [3, -1], [2, 2], [-1, 0], [-1, -3], [-4, 2], [1, -3], [3, 1], [-2, -4], [6, 2], [-5, -1], [-4, 2], [4, -4], [8, -2], [2, -4], [-10, 2], [6, 0], [1, -5], [0, 4], [-6, -4], [6, -2], [13, -3], [3, 7], [0, -4], [5, 3], [-8, 2], [15, -3], [12, 4], [-4, -6], [-16, 0], [-1, 5], [-7, 3], [0, 2], [-12, 4], [-8, 0], [6, 8], [2, -6], [-7, 1], [15, 5], [4, 2], [-9, -3], [-10, -2], [-12, 6], [-3, -3], [16, 4], [-6, 8], [-6, 0], [-7, 7], [6, 10], [0, 0], [2, -8], [-7, -5], [-24, 2], [4, -4], [6, -8], [-8, -6], [-8, 0], [4, -2], [10, 2], [15, -1], [30, 0], [-8, 2], [-10, 4], [10, 4], [-10, 0], [0, 10], [-8, 0], [-21, -3], [-28, -4], [-5, -3], [-2, 4], [-24, 0], [5, -1], [2, 4], [16, -2], [10, 2], [-2, -10], [-9, 1], [14, 2], [-18, -2], [-3, -7], [-6, -8], [-30, 0], [-26, 0], [23, -1], [-16, 0], [-12, 8], [1, 1], [17, -1], [-28, -2], [-14, -10], [-9, -9], [20, -2], [2, -12], [2, 2], [-2, -12], [-18, 0], [3, -15], [-8, -8], [-22, -4], [-22, 2], [18, 0], [-6, 6], [4, 0], [-10, 10], [-4, 4], [-9, 15], [8, 10], [-18, 4], [0, 12], [10, -10], [38, 4], [-4, 20], [25, 9], [23, 9], [-38, 2], [-1, -17], [-32, 2], [24, -10], [-18, 10], [4, 8], [37, -1], [24, 6], [15, 15], [-6, 2], [12, 14], [24, -4], [44, 4], [-11, 19], [8, -14], [6, 10], [6, -8], [-36, -2], [-12, 12], [6, 0], [-20, 6], [-18, 6], [29, 3], [16, -16], [-2, -2], [25, 5], [-24, 8], [-24, 6], [6, 8], [36, -8], [-36, 4], [-9, -3], [28, -4], [22, 10], [-6, -22], [34, 0], [18, -2], [-4, -12], [26, -6], [2, 16], [12, -2], [8, 18], [-32, -6], [-6, 6], [0, -20], [8, 6], [14, 14], [43, -3], [44, -2], [-24, -10], [3, -5], [22, 10], [-3, 13], [-11, -9], [0, 2], [15, -1], [8, -4], [-4, 16], [23, 1], [-11, -19], [16, 6], [-18, -6], [-12, 12], [-39, -1], [2, 12], [-15, -5], [-21, -5], [-11, 23], [24, -4], [40, -6], [-15, -3], [-32, -12], [12, -2], [10, 12], [-32, 6], [-2, -8], [-38, 0], [32, 10], [-20, 6], [4, -12], [45, 5], [44, 10], [-13, -5], [47, 7], [44, -8], [-50, -6], [-27, -11], [22, 6], [6, 18], [10, -20], [0, -8], [-14, 4], [5, 21], [4, -20], [12, 20], [-8, -24], [38, -6], [-32, -8], [46, 4], [10, 22], [41, -5], [-43, 11], [9, 1], [-34, 0], [44, -4], [-64, 0], [18, 20], [-2, -20], [8, -8], [-13, 15], [26, 4], [34, 0], [24, 16], [17, -17], [-18, -8], [-12, 4], [-3, 9], [10, 4], [9, -9], [32, -2], [60, 4], [32, -8], [-12, -16], [-4, 12], [41, 3], [34, -16], [15, 7], [54, -10], [-4, -4], [-60, 2], [23, 9], [-50, 12], [52, 8], [-31, 7], [16, -28], [23, 19], [26, 8], [4, -14], [6, 2], [-24, -18], [26, 8], [19, -23], [-4, 12], [-4, -26], [-59, 9], [6, 26], [4, -16], [16, -2], [-4, -24], [48, -14], [-23, 3], [-23, 1], [-12, 2], [18, 4], [26, -2], [32, 16], [-48, -8], [-60, -6], [5, -1], [30, 4], [-2, -14], [10, 2], [24, 8], [35, -5], [-47, 5], [-20, 12], [11, -11], [-34, 0], [14, -20], [30, 14], [-34, 8], [-25, -11], [12, 8], [-24, -6], [8, 4], [-31, 3], [21, -15], [-5, 31], [-29, -5], [-20, -12], [-36, -12], [6, 0], [20, 2], [34, -10], [16, 24], [-14, -18], [-60, -2], [2, -2], [-39, 5], [1, -1], [-4, -22], [41, 5], [-1, -23], [14, 28], [-24, 18], [-23, 15], [4, -4], [-68, 0], [-34, -6], [-22, -20], [30, 20], [-54, -10], [30, 8], [-22, 16], [11, -3], [-8, 10], [-58, -6], [-10, 4], [-32, 0], [10, 14], [-21, 13], [22, -20], [-18, 16], [10, 10], [-2, 6], [-12, 4], [-36, 0], [-9, 37], [-26, 12], [26, 16], [-34, -18], [51, 5], [16, -26], [20, -6], [23, 13], [-42, 16], [-6, 2], [18, -4], [-42, -8], [0, 16], [-59, 13], [10, 14], [-32, -26], [31, -27], [-3, -11], [39, -7], [10, 36], [7, 11], [20, 12], [72, 2], [2, -18], [-10, -28], [-36, -24], [-2, 42], [6, 26], [-12, 32], [14, -8], [-43, -7], [11, 33], [52, 10], [-40, 14], [-24, 20], [43, -3], [6, 8], [-12, -4], [-48, -2], [20, 8], [-45, 1], [-26, 8], [-10, -14], [18, -2], [-40, 6], [20, -18], [14, -38], [-14, -28], [0, 28], [70, -10], [42, 14], [-36, -6], [-42, 18], [4, 12], [42, -12], [36, 2], [-60, -10], [4, 4], [-16, 24], [-4, -2], [-7, -11], [-10, -32], [-34, -20], [1, 11], [-47, 5], [0, -6], [-21, -35], [-34, 4], [-36, 30], [38, 14], [34, 4], [-46, 4], [-48, -8], [84, 8], [8, 16], [17, 19], [-52, -16], [-8, -32], [14, -8], [29, -15], [28, -28], [-77, 3], [14, -18], [34, -30], [-54, 20], [31, -25], [29, -25], [44, -8], [34, 2], [18, -4], [-48, -12], [-22, 16], [40, 0], [49, 21]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2394_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_2394_2_a_z();".
// 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_2394_2_a_z(:prec:=2)
    chi := MakeCharacter_2394_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_2394_2_a_z();".
// 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_2394_2_a_z( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2394_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![-4, -2, 1]>,<11,R![-2, 1]>,<13,R![4, -6, 1]>,<17,R![-16, -4, 1]>],Snew);
    return Vf;
end function;