
// Make newform 2394.2.a.w in Magma, downloaded from the LMFDB on 19 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_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_2394_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
        poly := [-1, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    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, -3], [1, 0], [-4, 1], [4, -2], [0, 4], [-1, 0], [2, -6], [7, -3], [2, 4], [3, -3], [-1, 7], [-7, 1], [2, -3], [12, 1], [7, -3], [2, 7], [6, -8], [-8, 13], [-6, 2], [-9, 7], [-4, 8], [2, 1], [2, -3], [-10, 8], [10, 2], [2, 2], [-4, 7], [4, -8], [-12, -1], [0, -10], [11, 1], [8, -4], [-4, 0], [-12, 16], [-6, -5], [4, -5], [2, 8], [14, -6], [-18, 2], [2, 10], [-14, 14], [-10, 6], [6, -8], [17, -13], [-4, 0], [-4, 14], [-8, -8], [15, -13], [-4, 19], [4, -18], [11, 3], [-12, 2], [11, -21], [8, -2], [-26, 4], [7, -3], [2, -14], [-16, 0], [16, -4], [-20, 10], [-17, 13], [-2, 1], [-8, 16], [20, -1], [-10, -12], [-16, 2], [-24, 12], [-6, 0], [-4, -20], [-18, -6], [2, 7], [-7, 19], [16, 14], [22, 2], [-10, 20], [18, -5], [-16, -8], [-22, 9], [-10, 12], [-2, 20], [4, 15], [19, -5], [2, -4], [1, -11], [-12, 26], [-12, 17], [6, 1], [-4, -2], [-18, 6], [9, 15], [31, -9], [-12, -16], [-21, 7], [9, -5], [-38, 6], [10, -4], [24, 0], [18, -6], [-6, -2], [24, -2], [2, 3], [14, 6], [-20, 13], [-30, 8], [-10, 2], [26, 6], [-12, 9], [-18, 0], [-16, -10], [20, -32], [37, 3], [-2, -26], [20, -20], [22, 10], [8, -4], [-37, -7], [-16, -6], [4, 16], [6, 16], [16, -14], [10, -16], [34, 0], [26, -18], [-6, -14], [-4, 6], [-32, 16], [2, -17], [3, 3], [17, -11], [-41, 7], [-11, 1], [-16, -12], [38, -16], [-26, 10], [-22, -6], [-1, 25], [26, -42], [15, -19], [42, 1], [-10, 12], [18, -20], [-26, 10], [8, 12], [-14, -16], [25, -31], [-2, 16], [-2, -18], [-1, -13], [-16, 15], [-4, -2], [12, -9], [26, -36], [16, -26], [3, 23], [26, 2], [-4, -6], [32, -24], [8, 26], [-20, -1], [-6, 4], [8, 28], [26, 17], [-30, -2], [4, 8], [-36, 27], [21, 5], [-16, -10], [30, -4], [42, -14], [22, 10], [-14, 42], [9, 7], [-38, 36], [46, -4], [-5, 15], [22, 2], [-22, -4], [-17, 31], [10, -6], [2, -19], [0, 11], [-2, 13], [-38, 14], [10, 5], [-47, -5], [25, 7], [38, -30], [34, -33], [-14, 4], [-5, -25], [10, -36], [32, 10], [-48, -7], [35, -17], [56, -5], [4, -22], [8, 16], [29, 7], [-34, 12], [2, -2], [-4, 2], [2, -27], [44, 12], [26, -29], [50, -13], [6, 4], [-2, 4], [2, -15], [-5, 1], [-30, 25], [-40, -10], [-17, -31], [20, -10], [37, -35], [-14, 50], [49, -27], [-7, -39], [-32, 32], [8, 31], [40, -42], [32, -40], [4, -20], [-14, 11], [-2, 40], [11, -5], [24, -56], [32, 20], [-19, -19], [-32, -18], [-54, 23], [40, -35], [-20, 34], [-22, 38], [33, -33], [0, -20], [22, 20], [7, 15], [6, 26], [-4, 30], [-48, -4], [40, -24], [16, -29], [-10, 36], [50, 7], [-11, 7], [-4, 12], [4, 22], [-28, -4], [12, -56], [-30, -20], [44, 10], [-50, 8], [-30, -24], [29, -39], [-24, 8], [46, -3], [-4, 12], [-34, 44], [-31, 29], [14, -10], [30, -5], [-26, -18], [8, -30], [-18, 13], [-50, -12], [-58, -6], [-48, 0], [16, -30], [-22, -8], [0, -8], [38, -54], [24, -42], [43, -45], [-51, 39], [-48, 16], [7, -7], [0, -36], [38, -42], [-10, 36], [20, -4], [2, 35], [40, 14], [28, -44], [-40, -2], [8, -7], [21, -33], [-70, 23], [38, -36], [32, -20], [-14, 48], [-18, 46], [27, 9], [50, -8], [-25, 43], [14, -14], [-9, 31], [-50, -17], [-12, 32], [-20, 14], [18, 4], [54, -12], [34, -46], [29, 13], [-32, 40], [-8, -2], [0, -36], [-32, 52], [-8, 28], [11, -39], [2, -4], [44, -60], [-68, -8], [50, -62], [-21, -37], [-2, -40], [31, -17], [66, -32], [-12, 5], [-2, 31], [-11, 7], [-58, 16], [16, -45], [30, 12], [55, -25], [-20, 38], [-33, 5], [-32, -35], [0, 30], [-66, 36], [-39, -1], [10, -59], [-24, -24], [23, -13], [-16, 16], [40, -37], [50, -39], [-6, -4], [-6, 1], [-60, -11], [36, 7], [-6, 32], [-76, -8], [15, -43], [-48, 30], [-6, 45], [38, -28], [2, 0], [-21, 3], [5, -17], [-38, -8], [52, -24], [-68, 10], [-7, 19], [14, -41], [-14, -15], [50, -12], [24, -27], [0, -23], [-6, 6], [-54, -12], [20, -4], [32, -34], [6, -12], [-46, -24], [13, -27], [9, -31], [4, 36], [19, 11], [11, -3], [-54, 30], [-46, 4], [-8, -8], [4, 36], [-14, 1], [82, 0], [-52, -10], [42, -44], [-1, 43], [6, -16], [40, -17], [30, 34], [33, 23], [22, 9], [22, 38], [-54, 32], [45, -39], [12, 24], [-18, -42], [2, 4], [-56, 48], [5, -63], [8, -32], [-47, 25], [-16, 8], [-18, 30], [6, -13], [16, -16], [8, -68], [-54, 20], [0, 47], [-20, 13], [-82, 25], [-24, 24], [-1, 37], [12, 40], [-54, 28], [36, -48], [-12, -8], [42, -15], [-32, 14], [-22, -44], [-28, 51], [-57, -27], [6, -43], [4, -28], [6, -18], [54, 28], [-22, 22], [-56, 37], [-40, 24], [-14, 0], [68, 20], [-66, 59], [-8, 26], [-58, -30], [-57, 69], [-64, -2], [28, -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_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_2394_2_a_w(: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_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_2394_2_a_w( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2394_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![-11, 1, 1]>,<11,R![11, 7, 1]>,<13,R![4, -6, 1]>,<17,R![-16, -4, 1]>],Snew);
    return Vf;
end function;