
// Make newform 6422.2.a.k in Magma, downloaded from the LMFDB on 10 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_6422_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_6422_2_a_k();".
// 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_6422_2_a_k();".
// 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, -2, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-2, 0, 1]];
    Rf_basisdens := [1, 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_6422_a();"
function MakeCharacter_6422_a()
    N := 6422;
    order := 1;
    char_gens := [4903, 4733];
    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_6422_a_Hecke(Kf)
    return MakeCharacter_6422_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], [-1, 1, 1], [-2, 1, 0], [2, -2, 2], [4, -2, 3], [0, 0, 0], [-1, -3, 3], [1, 0, 0], [0, -4, -1], [-4, 2, -3], [-4, 3, -2], [-8, 4, -8], [5, 3, -3], [-6, 4, 4], [-6, 2, -4], [2, -3, 0], [0, -4, 0], [0, -8, 2], [0, 2, -6], [-4, 3, -6], [6, -6, 8], [-14, 6, -10], [9, 3, -1], [8, -4, 3], [0, -8, 5], [-2, 6, -6], [-4, 6, -6], [-2, 7, 2], [4, 2, 6], [4, 10, -2], [-6, -4, 0], [10, 0, 4], [-4, -6, -2], [0, 8, 0], [-9, 11, -13], [0, 4, 7], [-10, 8, -12], [-3, 11, -9], [5, -7, -5], [-7, 9, 5], [6, -9, -2], [1, 9, -3], [12, 5, -2], [3, 1, -1], [-10, 4, 4], [-4, 0, -9], [-8, -10, 7], [-11, -7, 3], [2, -4, 8], [16, -6, 15], [0, 1, 8], [22, -4, -2], [12, -7, 0], [-4, -4, -8], [6, 12, -10], [-15, 1, 3], [6, -8, -4], [20, 4, -2], [-12, 6, 8], [4, 0, 11], [10, -18, 10], [-10, -2, 2], [-14, 12, -14], [3, 7, -3], [-12, -1, 4], [8, 4, -2], [-4, 4, 12], [12, -10, 16], [-18, 0, 8], [-11, 5, -7], [-12, -12, -2], [4, -16, -6], [4, -9, 14], [-21, 7, -5], [4, 6, 0], [-16, 9, -10], [-4, 14, 10], [2, -1, 0], [4, -9, 4], [-10, 8, -12], [2, -8, 2], [8, 0, 14], [19, -5, -7], [-8, 4, -4], [-14, 22, -8], [6, -2, 2], [-7, 3, 1], [-6, -6, -12], [4, 10, 5], [16, -2, -8], [-20, 8, -4], [8, 2, 14], [4, -4, -7], [-26, 12, -20], [-11, -5, 15], [-20, 16, -12], [6, -6, 22], [0, -2, -6], [-10, -7, -10], [-26, -1, 8], [2, -7, -2], [32, -14, 23], [8, -2, -1], [24, -20, 26], [-12, 0, 0], [14, -6, 22], [16, -8, -4], [-4, 6, -2], [8, -2, -14], [8, -24, -6], [-4, -18, -6], [-22, 13, 0], [0, -18, 16], [2, -17, -6], [-16, 16, -8], [12, -20, 4], [11, 21, 1], [0, -13, 22], [4, 6, 0], [-15, 19, -5], [-18, 24, -22], [-26, 14, -32], [-8, 14, -15], [0, 6, 30], [4, -18, 15], [-2, 14, 8], [10, -4, -12], [-4, -8, 8], [-7, 25, -17], [-28, -2, -15], [4, -10, 19], [-44, 11, -14], [12, -32, 2], [14, 12, -10], [2, -4, 22], [-4, 16, -8], [10, -10, 6], [-16, 22, 10], [12, -14, 27], [16, -12, 33], [-34, 20, -12], [-6, -28, 12], [-12, 28, -29], [10, 10, 0], [-2, 1, -16], [-20, 8, -33], [9, -19, 13], [4, -10, 0], [10, -40, 24], [16, -16, -15], [0, -8, -20], [-16, -5, -12], [-12, 22, 2], [10, -30, 8], [34, -9, 14], [18, 6, -4], [13, -31, 19], [8, -28, -14], [-20, 5, -28], [-4, 24, -18], [14, 3, -22], [-14, 4, 24], [8, -26, 28], [-6, 5, -10], [-4, 12, 13], [-11, 13, -21], [-16, 24, -2], [10, 2, 36], [-12, 0, -39], [-30, 28, -30], [-14, 14, -12], [-2, -22, 12], [-20, 40, -33], [-28, 12, -18], [-34, -12, 4], [-25, 13, 3], [-18, 29, -18], [-4, 2, 19], [-18, 2, -16], [14, -28, 12], [28, -12, -18], [4, -24, -4], [8, -14, 5], [4, 15, -8], [-6, 32, -12], [24, 8, 2], [-30, 35, -32], [-2, -6, -8], [50, -8, 0], [-20, 26, -34], [-38, -2, -14], [10, 5, -2], [36, -18, 5], [-8, -8, 16], [30, -8, -6], [6, -2, -18], [0, 22, 6], [50, -12, 4], [-10, 22, 20], [-8, 28, -4], [10, 34, -20], [12, -12, 20], [-2, 12, -32], [-6, -12, -12], [38, -25, 38], [24, -22, -20], [-26, 26, 16], [-16, 26, -5], [-32, -14, 18], [18, 29, -14], [-36, 12, 17], [6, 6, 2], [7, 15, 9], [12, -24, 26], [4, 20, 9], [-31, 7, -15], [-32, 11, -10], [-2, 24, -10], [8, -5, 6], [21, -15, 13], [30, 0, 8], [-44, 24, -44], [36, 19, -16], [-20, -6, -30], [-28, 34, -47], [-4, -18, 24], [6, -26, 8], [20, 10, -6], [18, -20, 30], [42, 17, -2], [-40, -10, -4], [13, 35, -21], [-9, -33, 17], [-22, 26, -42], [-6, -6, 28], [-30, 40, -26], [-34, 12, -46], [-43, 21, -47], [-36, 8, -14], [30, -4, -6], [-22, 24, -26], [-16, -28, -6], [-61, -1, -7], [0, -2, -18], [12, 4, 29], [28, 5, 6], [48, -28, 38], [-28, -20, 6], [16, 10, 34], [-20, 32, -4], [22, -55, 32], [-20, 10, -8], [-16, -4, -20], [-4, 26, -14], [44, -18, 46], [16, -14, 25], [40, -4, 34], [26, -8, -2], [-18, 53, -36], [-2, -16, 4], [-64, 6, 6], [7, -15, -23], [10, 27, -20], [14, -16, 32], [-10, -14, -34], [26, -25, -10], [2, 4, 44], [12, -10, 32], [-32, -18, 16], [-28, 12, -26], [-4, -18, 1], [-20, -26, 12], [-2, 14, -16], [8, -49, 6], [-10, 46, -32], [36, 10, 4], [2, -19, 18], [18, -6, 12], [60, -32, 57], [48, -24, 36], [2, 8, 46], [10, 26, -2], [0, -5, -2], [26, -12, 18], [28, 28, 0], [-20, -23, -2], [-11, 27, 13], [6, -20, 14], [44, -18, -26], [-42, 28, -22], [6, -8, -14], [-14, 6, -16], [-36, 20, -15], [32, -24, -26], [6, -21, 12], [-10, 22, 18], [-20, -8, 16], [4, 4, 6], [-36, 40, -36], [-28, 18, -9], [-24, 36, -17], [-52, -10, 0], [13, 17, -17], [-20, 16, 8], [14, 12, -12], [-4, -27, 36], [-32, 22, 19], [8, 38, -7], [-12, 26, 14], [-22, -10, 22], [39, -17, -15], [-59, -19, 9], [2, -50, 22], [-22, -5, -6], [-46, 8, -44], [-46, 28, -38], [31, 13, 9], [-4, 16, -5], [-22, 34, -42], [44, 12, 19], [28, -18, 31], [-24, 14, -12], [-69, -1, -5], [28, -52, 13], [-46, 32, -62], [28, -20, -18], [-20, 38, 2], [-10, 32, 12], [-12, -32, 0], [-28, -6, 12], [-8, 18, 35], [4, -34, 23], [20, -28, 51], [12, 38, -9], [39, -35, 33], [-26, -23, 26], [36, 2, 14], [-30, 12, -20], [-48, 14, 6], [-8, 8, 17], [-34, 22, -24], [-48, 13, -44], [-30, 30, -18], [-16, -26, 28], [-30, 50, -4], [11, -3, -11], [-4, -2, 2], [34, -20, 14], [-12, 16, 48], [0, -26, -20], [-16, -30, -14], [-25, 25, -21], [-33, -1, 23], [-34, 18, 24], [-8, 10, 15], [-46, 24, -12], [-64, 16, -12], [57, -37, 31], [-18, 24, -8], [48, -48, 36], [24, -46, 29], [28, -52, 39], [4, -46, -12], [6, 12, -2], [-6, -28, 36], [-10, 18, 8], [-28, 30, -29], [29, 29, -25], [8, 4, -20], [72, -38, 62], [20, -12, 12], [-12, 36, -22], [22, -8, 24], [-4, 21, -18], [-14, 10, -2], [-12, 40, -22], [-17, -25, -11], [42, 6, 8], [1, 55, -23], [46, 6, 10], [12, -46, 36], [4, -6, -8], [10, -6, -10], [-4, -24, -16], [-30, 24, -6], [9, 9, 7], [-8, 34, -30], [-2, -8, 20], [20, -18, 11], [-4, -52, 22], [0, 20, -23], [14, 6, -26], [17, 19, 27], [58, -20, 54], [15, 11, 41], [18, 0, 26], [12, 8, -5], [6, 2, -40], [-35, -37, 27], [-29, 23, -9], [-26, -6, -8], [-28, -7, 8], [12, -23, -18], [24, 5, -40], [-60, 14, -46], [4, -9, -30], [40, -26, -9], [-50, 24, 12], [18, -30, 40], [48, 6, 3], [38, 10, 30], [-28, -14, 32], [-35, -17, -5], [-30, 2, -34], [4, -12, -43], [-16, 10, -10], [35, 7, -7], [0, -2, -44], [-24, -18, 16], [46, -14, 6], [-32, -10, -21], [-40, 28, -38], [28, -14, 18], [42, -19, 18], [-48, -27, 24], [22, 4, 12], [44, 0, 32], [34, 10, -26], [47, -3, 5], [56, 3, 26]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_6422_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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