
// Make newform 6358.2.a.l in Magma, downloaded from the LMFDB on 03 June 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_6358_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_6358_2_a_l();".
// 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_6358_2_a_l();".
// 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 := [-2, 0, 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_6358_a();"
function MakeCharacter_6358_a()
    N := 6358;
    order := 1;
    char_gens := [1157, 4049];
    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_6358_a_Hecke(Kf)
    return MakeCharacter_6358_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], [-1, 1], [-1, -2], [2, 1], [1, 0], [-4, 1], [0, 0], [0, 0], [1, -1], [2, 1], [0, 0], [1, 6], [-4, -4], [0, 1], [-9, -1], [0, -2], [5, 3], [-2, -7], [-12, -2], [2, 4], [0, -3], [-4, 7], [-16, 0], [-3, -8], [4, -2], [-2, -8], [-15, 3], [6, 1], [0, 1], [8, 0], [-2, -5], [0, -9], [-19, 2], [12, 7], [2, -15], [0, -8], [-7, 10], [7, 11], [6, -6], [-14, -7], [15, -1], [-5, 0], [0, 2], [-14, 0], [-4, 0], [-13, 1], [-18, 2], [3, -5], [-12, 4], [0, 14], [-2, -3], [2, 15], [-4, 15], [-3, 13], [11, -6], [2, -15], [-7, -12], [-4, 15], [-12, -7], [4, 4], [-8, 4], [2, 20], [-12, 4], [-27, 3], [4, -2], [19, 0], [7, -1], [0, -7], [12, 1], [10, -9], [-4, -22], [2, 13], [-3, 17], [10, -4], [17, -5], [7, 7], [-5, -18], [-17, -8], [-13, -8], [-26, -8], [7, 3], [-5, -18], [-8, 8], [-12, 18], [-8, 19], [-15, -11], [-15, -12], [8, 9], [-8, 19], [7, 11], [-7, -1], [-14, -9], [-7, 1], [-6, 21], [1, 19], [8, 12], [21, -2], [23, 2], [-4, 7], [26, 0], [24, -8], [-12, 3], [6, 14], [22, -8], [-20, -8], [-12, 14], [-28, 2], [24, 8], [2, -8], [-8, 12], [6, 6], [4, -7], [-15, -22], [25, -9], [19, -5], [-26, -16], [5, -21], [3, -19], [14, -16], [-2, -13], [33, 2], [-22, 16], [18, -3], [31, 3], [-2, 24], [24, -11], [35, -6], [-18, 4], [15, 5], [34, 11], [-10, -11], [-30, -6], [27, 9], [1, -20], [-2, 20], [6, -16], [5, 12], [-36, -1], [16, 6], [32, -8], [2, 31], [2, 17], [34, -4], [-14, 21], [21, 6], [23, -17], [-16, 28], [22, -12], [16, -10], [5, 17], [-8, -32], [-1, -8], [21, -1], [0, 7], [31, 7], [32, -8], [-12, 28], [4, -26], [-4, 8], [12, -8], [-8, 26], [0, 28], [10, -10], [-4, -26], [-14, 0], [20, 2], [29, 5], [-48, 5], [12, -8], [42, 8], [-12, -29], [6, 0], [12, 19], [-16, -15], [13, -5], [-1, -6], [42, 6], [-21, -6], [-38, -5], [24, -16], [-39, -13], [-18, -6], [29, 10], [-14, -9], [-21, 15], [-13, 24], [28, 0], [-11, -11], [26, 3], [20, 11], [35, -16], [-20, 5], [15, -21], [-13, 6], [-16, -29], [-45, -6], [-34, 13], [48, -6], [28, -17], [-18, 22], [8, -24], [-40, -19], [-13, 14], [-42, 8], [4, 44], [16, -2], [47, -17], [10, 5], [20, -32], [-13, -13], [-20, 1], [-48, -10], [31, 1], [29, 15], [40, -21], [-1, -32], [14, 6], [2, -11], [-9, -19], [-33, -14], [28, 16], [-10, -25], [-4, -32], [3, 1], [-38, -1], [-16, -12], [-37, 12], [15, -33], [-22, -11], [-26, -2], [27, -8], [-30, -14], [14, -18], [12, -25], [17, -7], [30, 9], [-12, 36], [-12, -8], [15, 17], [3, 23], [-1, 13], [8, 19], [-30, -12], [-13, 10], [58, -7], [16, -11], [-9, -15], [-1, -5], [-6, -2], [-64, 0], [10, 7], [-10, 12], [30, -12], [3, -22], [-32, 12], [66, 9], [-1, 16], [2, 27], [22, 40], [26, 9], [-48, -1], [-22, -14], [22, 4], [12, 21], [-19, 40], [-35, 13], [19, -26], [-17, -40], [16, 43], [-8, -9], [-47, 12], [65, -7], [27, -32], [-16, 27], [-2, 13], [59, -13], [-60, -16], [-16, 20], [-2, -33], [-32, -13], [-18, 1], [-28, -10], [20, 23], [26, -7], [2, 6], [-31, 21], [4, -12], [-38, -11], [0, 30], [-10, -3], [-23, 26], [36, -28], [-44, -9], [-12, 0], [-6, 36], [64, 5], [21, 45], [21, 16], [76, -4], [-8, -29], [8, -49], [-2, -36], [-16, -27], [29, -23], [59, 1], [-45, 8], [26, 24], [-40, -4], [-1, -11], [-14, 29], [22, -2], [-1, 6], [-70, 9], [15, 23], [26, -18], [68, -12], [40, 14], [-26, -7], [-3, 46], [-52, 15], [30, -17], [-52, 22], [-54, -20], [19, 19], [20, -20], [17, -12], [15, 35], [1, -39], [-24, 21], [-16, 20], [-22, 5], [1, -46], [34, -38], [34, -2], [-58, -18], [29, -17], [-43, 20], [14, -16], [-61, 10], [52, -12], [57, -16], [3, 20], [8, 3], [-75, -9], [1, 8], [30, 26], [37, 38], [17, -9], [-22, -22], [-17, -8], [10, -3], [12, 8], [23, 8], [-8, -53], [-48, -12], [-18, 15], [-27, -7], [-10, -33], [54, 24], [41, 17], [12, -12], [24, 41], [-45, 11], [26, 2], [-3, -31], [-29, -32], [-54, 17], [38, 34], [4, -8], [19, 53], [72, -10], [-64, 16], [28, -9], [-46, 14], [39, -40], [-4, -36], [-34, 34], [-42, 1], [-4, -20], [-34, 12], [-9, -24], [-23, 36], [30, 14], [38, -24], [-18, -46], [43, -19], [16, 28], [-51, -28], [-82, 6], [-24, 48], [3, -38], [-33, -6], [-5, -41], [-68, -4], [-42, 28], [22, -15], [24, -11], [47, -10], [-61, -15], [30, -3], [32, -31], [-24, -32], [-40, 1], [-42, 32], [-42, -33], [-46, 18], [37, 24], [12, -32], [31, 33], [21, -5], [-50, -37], [-14, -43], [-15, 15], [48, -8], [10, 9], [32, 16], [-32, 5], [-48, 2], [-3, 60], [-50, 29], [-69, -20], [-38, -43], [79, -1], [-14, 38], [-54, 0], [-56, -6], [68, 24], [-2, -13], [-61, -15], [14, 42]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_6358_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_6358_2_a_l();".
// 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_6358_2_a_l(:prec:=2)
    chi := MakeCharacter_6358_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_6358_2_a_l();".
// 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_6358_2_a_l( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_6358_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-1, 2, 1]>,<5,R![-7, 2, 1]>,<13,R![14, 8, 1]>],Snew);
    return Vf;
end function;