
// Make newform 5194.2.a.i in Magma, downloaded from the LMFDB on 02 June 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_5194_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_5194_2_a_i();".
// 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_5194_2_a_i();".
// 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_5194_a();"
function MakeCharacter_5194_a()
    N := 5194;
    order := 1;
    char_gens := [3923, 3235];
    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_5194_a_Hecke(Kf)
    return MakeCharacter_5194_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]];
    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], [4], [0], [-3], [-1], [3], [-6], [6], [-3], [2], [-3], [-4], [11], [3], [-1], [-1], [-8], [10], [6], [14], [8], [12], [7], [14], [-4], [-4], [12], [-6], [3], [20], [3], [-4], [20], [-21], [-12], [14], [-12], [-2], [-10], [26], [4], [-8], [10], [17], [5], [13], [-5], [-3], [2], [-28], [12], [-29], [-20], [8], [6], [30], [-25], [-18], [-30], [26], [18], [28], [5], [-22], [23], [21], [-10], [-13], [12], [6], [-18], [-27], [-22], [-18], [-12], [-24], [22], [26], [-10], [-30], [-6], [33], [15], [-4], [4], [9], [-4], [9], [-6], [8], [-26], [-9], [0], [22], [-28], [44], [-39], [-25], [30], [-19], [10], [46], [-36], [-12], [18], [36], [6], [11], [24], [-16], [-20], [22], [24], [-38], [-6], [36], [0], [19], [-30], [30], [47], [6], [12], [14], [-42], [-36], [6], [5], [-27], [-14], [9], [-5], [-10], [30], [-18], [30], [-22], [-8], [30], [-11], [-22], [-47], [0], [16], [17], [-48], [47], [-7], [1], [53], [-12], [-34], [-4], [41], [0], [-10], [37], [-37], [21], [-7], [-9], [41], [-13], [2], [-31], [32], [-54], [34], [-50], [48], [4], [-13], [-58], [-24], [9], [-37], [-18], [56], [35], [-8], [56], [54], [29], [63], [-21], [-17], [39], [-54], [-8], [-50], [26], [0], [13], [40], [-36], [2], [43], [0], [-49], [21], [-44], [-38], [48], [-5], [48], [33], [15], [1], [-12], [6], [42], [-56], [-32], [7], [37], [-8], [-5], [13], [0], [44], [-10], [58], [6], [23], [-52], [-4], [-26], [43], [-46], [50], [9], [-55], [-22], [-35], [28], [-52], [-34], [-40], [2], [16], [7], [25], [48], [-54], [8], [-42], [-52], [-33], [65], [-3], [29], [8], [-56], [54], [-79], [28], [29], [15], [50], [-58], [-47], [18], [-70], [-62], [26], [61], [33], [-58], [-21], [64], [-52], [-51], [-32], [-43], [28], [-27], [-45], [42], [-52], [-28], [19], [-8], [-77], [-28], [-15], [18], [-40], [-41], [50], [-50], [49], [20], [40], [-62], [0], [-8], [-56], [26], [72], [4], [-27], [-31], [60], [-2], [-44], [24], [-65], [-49], [-32], [77], [-30], [-60], [-83], [20], [34], [78], [61], [-14], [-30], [25], [25], [-66], [14], [-30], [0], [-59], [34], [80], [-46], [-88], [55], [85], [45], [-53], [0], [15], [14], [76], [-56], [-2], [-48], [-6], [2], [-17], [65], [-66], [-36], [52], [63], [0], [28], [90], [-74], [-1], [18], [-24], [90], [-64], [53], [59], [8], [45], [-66], [44], [-22], [44], [56], [-88], [-31], [-56], [-96], [0], [-73], [-65], [0], [-37], [-90], [10], [80], [77], [1], [-48], [-50], [-24], [21], [-28], [-20], [32], [63], [-16], [39], [15], [83], [-60], [-38], [-101], [0], [-37], [33], [31], [36], [-11], [-68], [88], [46], [46], [10], [41], [12], [-61], [30], [42], [-7], [32], [-25], [52], [-101], [-28], [-30], [-53], [68], [22], [55], [-29], [-35], [-6], [-26], [66], [48]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5194_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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