
// Make newform 6422.2.a.v in Magma, downloaded from the LMFDB on 25 April 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_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_6422_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, -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], [0, -1, 0], [-3, 1, -1], [-2, 0, -2], [0, 0, -2], [0, 0, 0], [4, 0, 3], [1, 0, 0], [4, -4, 6], [4, -4, 2], [2, -3, 2], [-4, 6, 0], [-8, 4, -2], [0, 4, 0], [-2, 4, 0], [-10, 2, -2], [-1, -5, -1], [-1, -1, 5], [4, -3, 0], [4, 2, -3], [-4, 0, -3], [-4, 6, -7], [-4, 2, 0], [-14, 4, -8], [4, -6, 8], [5, 1, 7], [5, -1, -5], [-6, 4, -3], [-8, -4, 4], [4, 2, 0], [-18, 1, -2], [-6, 0, -2], [7, 5, 3], [4, -6, 12], [-2, -2, 11], [-4, 6, -1], [10, -10, 1], [-8, 4, -6], [-10, 9, -10], [4, 10, 2], [-10, -4, -7], [8, 4, -8], [-10, 0, -6], [-2, 6, 12], [10, -2, 13], [6, -14, 2], [0, 3, 8], [0, -22, 9], [-12, 1, 0], [-9, -1, -3], [-5, -11, -1], [14, 4, -4], [-4, 16, 2], [22, -4, 0], [-18, -2, -4], [-2, -14, 2], [-2, 6, 6], [18, -4, 12], [-15, 13, -5], [-8, -12, 4], [8, -18, 16], [0, -6, 6], [-4, 9, -12], [6, -4, -10], [-1, 13, -5], [-6, 2, -14], [8, 9, -4], [-20, 12, -10], [-8, 8, 6], [-6, 3, 2], [4, -1, 12], [26, 0, -6], [12, -2, 0], [-2, 12, 8], [4, 0, 12], [-11, -9, -1], [18, -20, 20], [19, 7, -3], [-12, 8, -24], [-10, -6, 8], [2, -10, -10], [-16, -4, -4], [22, -5, 6], [-10, 22, -16], [-2, 1, -6], [-24, 18, -16], [2, -6, 8], [-27, 3, -7], [-18, 14, -5], [-30, 20, -28], [14, -16, -4], [12, 0, 8], [2, -17, -6], [2, -6, -10], [18, -10, -4], [10, 12, 10], [26, -2, 2], [8, -8, -6], [4, -4, 4], [-2, -19, -2], [-10, 0, 3], [-1, -17, 5], [24, -3, -8], [-2, -2, 2], [8, -32, 4], [-20, -3, -8], [-2, -18, 12], [2, -12, 20], [-22, 9, -6], [-4, 20, 0], [-21, 17, -27], [-34, 11, -22], [-4, -8, 13], [28, -16, 16], [-18, -14, 2], [-10, 16, -6], [-6, -18, 2], [-30, 22, -26], [-2, 10, 5], [-35, -7, 1], [-18, -10, 4], [-16, 6, -14], [20, 4, -2], [17, -19, -11], [-24, 18, -2], [-14, 5, -2], [14, 10, -9], [-12, -8, -8], [22, 0, 10], [-10, 10, -19], [-16, 12, -30], [-10, 19, 2], [-8, 20, -20], [-9, 7, -23], [-4, 28, -11], [-23, 7, 9], [6, -10, 2], [-15, 9, 21], [14, -36, 18], [-16, -5, -8], [-23, 17, -11], [-1, 11, -3], [-26, 14, 8], [22, -4, 19], [4, 22, 2], [-7, 3, -21], [25, -31, 19], [22, -10, 2], [-12, 30, -30], [32, -16, 0], [-18, 6, -4], [-12, 17, -32], [-30, -2, -2], [-2, -13, 18], [16, -4, -16], [-13, -7, 5], [22, -6, 4], [24, -19, -8], [8, 21, -12], [24, -12, -16], [-12, -10, 8], [28, -26, 6], [20, -4, 6], [-12, 21, -4], [22, -6, -22], [12, -22, 27], [-51, 7, -1], [18, -23, 6], [-24, 8, 2], [-11, 33, -13], [22, -12, -17], [14, -12, 10], [28, 4, 4], [4, -19, 8], [28, -14, 41], [-4, 15, -4], [14, -16, 18], [30, 16, 0], [-42, 3, 2], [6, 35, -22], [6, 34, -8], [-6, 28, 7], [8, -10, 2], [-2, 30, -12], [18, -32, 0], [41, 5, -1], [-12, -4, -16], [-14, 4, 9], [-10, -6, 20], [26, -16, 10], [-44, 28, -34], [14, -24, -20], [16, -9, 4], [44, -20, 28], [-12, -26, 18], [-16, 2, 16], [-25, 5, -5], [-9, 11, 17], [14, -8, -12], [22, -40, 40], [-28, 8, -34], [11, -11, -3], [32, -10, 0], [-6, -36, 22], [6, 26, 8], [29, -15, -25], [-18, -22, 4], [-24, -15, 4], [11, 17, -5], [14, -16, 29], [56, -4, -7], [18, 11, 22], [-2, -28, 10], [4, 5, -28], [2, 1, 10], [26, -2, -6], [-10, 36, -6], [0, 34, -26], [25, -33, 27], [10, 17, -10], [-44, -14, 0], [15, -27, -15], [34, -12, 28], [-8, 22, 0], [2, 28, -21], [6, -9, 2], [-12, -10, 0], [-2, -27, 18], [-20, 24, -8], [-24, 46, -24], [-34, 30, -9], [-6, -12, -19], [33, -21, 27], [-18, 8, 16], [26, -14, 32], [6, 14, -6], [-30, 8, -40], [-2, 24, -8], [-10, 22, 16], [-6, 29, 22], [-26, 16, -23], [-40, 12, -28], [-30, 18, -28], [4, 6, 12], [18, -12, 12], [0, -20, 8], [-38, 22, -28], [-20, 12, 4], [6, -4, -5], [24, -14, 6], [-22, -2, 17], [30, 0, -4], [-2, -12, 0], [-16, -20, 18], [-1, 15, 9], [66, -6, 2], [26, 22, -19], [-18, 16, 29], [-14, -4, -16], [12, -21, -32], [-2, -9, 2], [35, -9, 15], [-42, -11, 2], [14, 24, -6], [16, 12, -17], [-10, 12, -9], [44, -22, -4], [-32, 25, -12], [-10, -20, 19], [14, 43, -14], [38, -24, 46], [3, 23, 23], [-29, -3, -17], [-2, -6, -4], [40, -12, -10], [12, -28, -8], [14, -10, 0], [-32, 32, -38], [-52, 4, -20], [-24, 36, -16], [40, -34, 39], [6, -38, 16], [30, -38, 40], [-28, 4, 2], [-10, 28, -34], [-42, 22, -32], [-8, 15, -36], [8, -14, 14], [54, -16, 12], [-44, 18, -50], [1, 33, -25], [18, -22, 12], [-36, 28, -2], [56, -4, 8], [24, -6, -10], [-5, -41, 29], [26, -29, -34], [58, 0, -12], [-11, 17, -35], [50, -32, 54], [1, -29, -27], [-8, -14, 10], [10, -22, 10], [28, -24, 16], [-48, -2, -4], [-8, -16, -14], [-36, -8, -12], [-46, 18, 28], [-24, 50, -26], [50, -36, 28], [-36, -34, 14], [46, -23, 18], [32, 2, -8], [-29, -9, 23], [-30, 8, 26], [0, -30, -28], [-2, 12, -14], [24, -22, 7], [-7, -13, -19], [-67, 19, -23], [0, 17, -36], [-4, -28, 41], [-4, 4, -50], [32, -30, 1], [-24, -33, 24], [2, -10, 14], [0, -24, -20], [-8, -20, -12], [21, 19, -21], [-46, -2, -27], [18, -42, 25], [-20, -10, -24], [6, 38, -32], [-14, -12, 36], [40, -28, 44], [-30, 4, 2], [52, -8, 16], [18, -14, 22], [36, -36, 51], [46, -2, 14], [-6, -18, -36], [-19, 3, 37], [-30, 28, -18], [4, -32, 4], [26, -32, 48], [-8, 9, 16], [18, -10, -5], [2, 8, 53], [-29, 33, 25], [-27, 49, -25], [32, 21, -12], [-60, 32, -20], [-19, 9, -41], [24, 50, -21], [10, -16, 46], [-46, -6, -24], [-66, 16, 8], [-54, 38, -62], [36, -52, 55], [-31, 3, -17], [18, -4, 56], [13, -25, 41], [-26, 19, -14], [-10, -36, 13], [4, -8, -14], [4, 42, -28], [22, -21, 58], [-45, 25, 9], [-4, -38, 14], [-14, -36, 26], [-45, -1, -5], [-2, -6, -18], [20, -24, 24], [68, -22, 38], [1, 17, -9], [6, -68, 17], [-66, 14, 16], [-24, 17, -12], [-9, 45, 3], [48, -52, 50], [14, -2, 42], [36, -20, 59], [-2, -22, 20], [-35, 3, 13], [28, 5, -4], [-48, 48, -40], [-26, 52, -10], [25, -31, 27], [-2, 6, -18], [0, 18, -47], [40, -52, 44], [-10, 20, -2], [22, -10, -16], [-26, -28, -6], [62, -41, 78], [-60, -10, -20], [-28, 20, -16], [48, -24, 62], [66, 0, -11], [38, 21, -14], [-36, 48, -40], [-9, 9, -13], [-9, -11, 5], [38, -4, 6], [-12, 10, 8], [22, 6, 12], [18, -2, -13], [-8, 34, -22], [-23, 1, -31], [0, 20, -18], [-20, 16, 36], [-1, -13, -19], [-44, 57, -32], [28, -44, 24], [33, -1, -11], [-23, 25, -17], [18, 25, -10], [24, -12, -20], [-9, 69, -41], [-20, -42, -1], [-22, 24, 42], [-26, 40, 26], [-8, 10, 4], [10, 0, -47], [-90, 18, -34], [34, -52, 24], [4, -59, 24], [32, -55, 24], [-36, 8, -18], [32, -46, 54]];
    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_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_6422_2_a_v(: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_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_6422_2_a_v( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_6422_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-1, -2, 1, 1]>,<5,R![7, 14, 7, 1]>,<7,R![-8, -4, 4, 1]>],Snew);
    return Vf;
end function;