
// Make newform 2394.2.a.v in Magma, downloaded from the LMFDB on 16 April 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_v();".
// 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_v();".
// 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], [-3, 1], [-1, 1], [1, 3], [1, 0], [-3, -1], [-4, -2], [-2, 0], [-6, -2], [-6, 0], [0, 4], [0, -4], [-8, -2], [-8, 0], [4, -2], [7, -3], [-6, -2], [-8, -2], [3, -1], [10, 0], [-6, 0], [7, -1], [7, 3], [2, -4], [-10, 2], [14, 2], [-10, 0], [-3, -3], [12, 2], [2, 6], [4, 8], [-6, 0], [3, -1], [0, 6], [4, 0], [-8, -4], [2, 0], [-4, 8], [-3, -1], [-15, -1], [-18, 0], [2, 4], [2, 6], [3, -11], [24, 2], [2, -6], [-10, 4], [-10, 2], [-15, -5], [-3, 1], [-12, 6], [8, -2], [-1, -3], [-2, 4], [12, 0], [-6, -4], [-6, 0], [-4, -8], [2, -8], [18, -6], [6, -10], [-2, 4], [10, 8], [5, 11], [-16, -2], [-13, 3], [2, -12], [5, 3], [-9, 9], [-4, 0], [4, -4], [-7, -9], [8, 0], [16, -6], [-12, -6], [-30, 0], [-17, -9], [8, 10], [-8, 12], [4, 12], [-21, -1], [10, 0], [1, -3], [6, -12], [4, -14], [-13, -5], [-10, 14], [2, -8], [30, -2], [1, 13], [7, 3], [-6, -14], [22, 2], [0, 6], [-4, -10], [10, -2], [-10, 16], [-9, 9], [18, -8], [20, 0], [-6, 4], [2, 2], [8, 6], [18, 12], [19, -7], [26, 6], [-5, 3], [12, 6], [26, -8], [-10, 6], [-24, -8], [-2, -2], [30, 8], [-36, 4], [6, 18], [-4, 6], [-10, 2], [-43, -1], [-4, 18], [24, 2], [-28, 8], [0, -12], [32, -2], [42, 0], [2, 2], [8, -16], [-14, 12], [10, -6], [10, 6], [-15, 17], [18, 8], [-13, -7], [-2, -4], [32, 6], [6, 6], [-32, 6], [0, 4], [-10, 6], [2, 4], [34, 2], [16, -12], [-21, 1], [-36, 8], [-2, -4], [-14, 8], [40, 8], [-26, 10], [-6, 6], [-19, 11], [-10, -2], [32, 0], [21, -1], [2, -18], [10, 10], [13, -13], [-38, 4], [-40, -2], [-21, -17], [-34, 4], [4, -12], [-30, 2], [10, -8], [4, 0], [27, -1], [-12, -22], [-10, -4], [7, 7], [-12, -8], [1, -17], [-9, -7], [14, -12], [28, -10], [19, -7], [10, -14], [-4, -18], [-32, 6], [18, 8], [10, -6], [14, -6], [-10, 10], [-6, 12], [23, 17], [11, -5], [-24, 16], [0, 4], [-8, 2], [-8, 0], [-15, 9], [27, -1], [-37, 5], [-18, 16], [15, 7], [4, -10], [-34, 0], [-2, -4], [-7, -5], [-6, -6], [-44, 2], [-52, 6], [5, 3], [-11, 13], [-2, 4], [3, 15], [20, -12], [36, 0], [24, -4], [26, -10], [4, -26], [-33, -17], [14, 6], [-14, 10], [4, 16], [-51, -3], [-32, 8], [-48, -6], [-24, 16], [0, 26], [-54, 6], [-24, 2], [18, 0], [-10, -10], [30, 10], [30, -16], [14, -24], [10, -2], [-22, -16], [-29, 11], [6, -8], [34, 6], [-55, -3], [-22, 0], [-12, -8], [32, -16], [-20, 10], [-42, 12], [-11, 21], [-48, -8], [-2, -14], [-32, -4], [2, -22], [40, -4], [50, -4], [-30, 14], [-24, 20], [-26, 10], [61, 7], [-37, 1], [-2, 8], [-25, -19], [-43, -13], [29, 13], [61, -3], [13, 1], [-30, -8], [0, 4], [-42, 0], [4, -26], [33, -3], [8, -20], [-42, 0], [7, 17], [3, -11], [-20, -8], [-36, 10], [7, 5], [-17, -3], [44, -10], [33, -17], [-23, -9], [-22, 8], [-60, -4], [-1, 11], [54, 2], [-19, -15], [-12, 0], [-30, -24], [18, 20], [16, 4], [0, -8], [30, -6], [-33, 9], [-40, -4], [-5, -23], [-12, 6], [0, -22], [18, 26], [32, -10], [-34, -12], [-58, 4], [-26, -12], [40, 20], [-8, -4], [-23, 5], [-34, 8], [75, -1], [-24, -28], [44, 12], [-10, 16], [0, -22], [32, 8], [34, -2], [18, 18], [-32, -6], [-78, -2], [-37, -15], [5, 19], [-42, -20], [0, -12], [-8, -2], [-6, -4], [3, -23], [-11, 3], [-41, 11], [26, -28], [8, 16], [-16, 18], [66, 10], [36, 4], [16, 6], [10, 12], [-37, -21], [-20, 4], [45, -7], [25, -1], [-26, -10], [2, 14], [0, -22], [38, 0], [54, -4], [-20, 32], [-24, 10], [54, 14], [22, 20], [22, 10], [32, 10], [-32, -6], [12, 20], [28, 28], [-22, -26], [44, -18], [-3, 5], [-2, 16], [62, -14], [-2, 0], [27, 1], [-8, 12], [-33, -5], [72, -8], [42, 8], [-54, 4], [-44, -14], [0, -32], [4, -2], [44, -4], [32, -14], [56, 0], [74, -2], [38, -16], [-5, 13], [52, 4], [60, -8], [32, -12], [24, 2], [41, -23], [21, 13], [-6, 28], [-31, -27], [72, -8], [23, -19], [-6, -8], [-56, -12], [22, -4], [-14, 10], [5, -9], [-40, 6], [-13, 13], [53, -7], [-40, -6], [-16, -28], [-42, 22], [-44, 12], [4, 8], [28, -28], [-40, -24], [60, -10], [46, 6], [0, 36], [0, -26], [-46, -12], [-21, -1], [16, 16], [-8, -16], [-56, -6], [10, 12], [18, -12], [5, 7], [-16, 18], [-33, -23], [68, -8], [-57, -17], [-46, 4], [-46, -24], [-8, 16], [42, -20], [26, 4], [90, -2], [-66, 16], [15, -17], [24, -30], [12, -16], [2, -30], [24, -34], [-31, 25], [60, 4], [-8, -6], [-7, 35], [8, -8], [-34, 4], [-43, -17], [-61, 13], [-40, 16], [35, 15], [6, 36], [14, -14], [10, -26], [-36, -28], [-40, 16]];
    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_v();".
// 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_v(: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_v();".
// 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_v( : 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![4, 6, 1]>,<13,R![-4, 2, 1]>,<17,R![-44, -2, 1]>],Snew);
    return Vf;
end function;