
// Make newform 4022.2.a.b in Magma, downloaded from the LMFDB on 16 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4022_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_4022_2_a_b();".
// 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_4022_2_a_b();".
// 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, -4, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-3, -1, 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_4022_a();"
function MakeCharacter_4022_a()
    N := 4022;
    order := 1;
    char_gens := [3];
    v := [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_4022_a_Hecke(Kf)
    return MakeCharacter_4022_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, 0, 0], [0, 1, 0], [-1, 1, -1], [4, -1, 2], [-2, 0, -2], [-1, -3, 1], [1, -1, 0], [-2, -1, 1], [-6, -1, 1], [1, -5, 3], [-4, 1, -2], [-3, 5, -5], [-4, 0, -1], [6, -4, -2], [-3, 1, -2], [-1, 3, 4], [1, 0, 3], [5, -6, 7], [-2, 0, 2], [-3, 3, 2], [5, 5, -5], [-6, 4, -7], [-10, 5, 0], [4, 5, -3], [6, -1, 1], [-16, 2, -1], [-14, 3, -3], [-1, -1, 1], [-3, 2, -5], [-4, 4, 4], [-4, 3, -1], [6, -3, 0], [-15, 1, -3], [5, 2, 4], [-1, 5, 4], [2, 1, 5], [-3, 2, 5], [3, 3, -4], [-4, 7, 3], [0, 2, -6], [-1, 2, -2], [8, -4, 4], [-3, -7, 7], [13, -5, 5], [10, -3, 9], [-6, 2, 3], [-2, -1, -4], [-2, 2, -6], [-11, -4, 5], [0, -2, -7], [-1, 6, -5], [12, -12, 6], [-18, 8, -9], [-8, 9, -14], [-1, -11, 8], [-4, -10, 2], [2, -8, 4], [-10, 6, -9], [-11, -3, -7], [8, -5, -7], [-11, 7, -16], [-1, -8, 1], [-24, 6, -5], [2, 11, -7], [8, -6, -7], [14, -8, 8], [17, 0, 13], [-14, -2, 2], [-4, -3, -14], [-3, 5, 1], [2, -1, 12], [5, 9, 2], [14, 10, -5], [11, -13, 16], [6, -19, 6], [20, -4, 17], [-21, 5, -8], [-4, 13, 4], [31, -5, 2], [-10, -3, 4], [-6, -10, -1], [12, -6, -7], [-12, 11, -15], [22, -5, 9], [-14, 1, -19], [22, -2, -5], [-11, 17, -4], [0, 17, -5], [1, 1, -4], [-12, 4, -7], [5, -17, 11], [20, -4, 6], [8, -9, -6], [27, -4, 1], [1, -10, -1], [-5, -1, 8], [-20, 4, -6], [-25, -6, 0], [6, 16, -10], [-13, 0, -7], [-12, 20, 0], [-7, -13, 12], [13, -18, 15], [7, 11, -3], [-22, 4, -22], [-15, 21, -4], [14, -7, 1], [24, -14, -5], [-13, 1, 2], [-21, 8, 3], [0, 0, 7], [8, 4, -2], [-15, 9, -12], [-5, 13, -8], [22, 15, -10], [-16, 13, -4], [20, -3, -2], [17, -3, 25], [6, -2, 18], [12, 7, -1], [-22, 4, 1], [-2, -1, -2], [-12, -2, -11], [3, -2, 8], [33, 2, 1], [-1, -10, -2], [-20, 18, -4], [-26, 12, 1], [12, -19, 8], [16, -7, -12], [-24, -4, -4], [-24, -5, -12], [9, -6, -10], [24, -1, -8], [-31, 6, -25], [11, -13, 13], [-27, 10, -15], [0, -5, -1], [-14, 0, 7], [21, -23, 7], [-8, -8, -16], [10, -8, -9], [11, -13, 25], [30, -13, 4], [3, 4, 9], [8, 6, 10], [19, -3, 1], [20, -18, -1], [5, 14, 10], [1, 15, -19], [21, -3, 7], [5, 26, -21], [-6, -13, -4], [32, -6, -9], [26, -5, 18], [-19, 4, -10], [-12, -5, -9], [-4, 5, 11], [34, -16, -3], [-12, -14, 7], [1, 16, -24], [3, -14, 17], [-4, 2, -13], [19, -1, -5], [15, 3, 18], [-28, 21, -18], [19, -19, 19], [24, -2, -6], [-41, 6, -18], [-9, -9, -1], [4, -4, 11], [-12, 4, 15], [1, -8, -12], [-27, 11, -7], [-27, 15, -4], [-12, 15, 2], [13, 7, 1], [-10, -23, 16], [-23, 4, -13], [27, -11, 1], [25, -9, 28], [-28, 16, -7], [-10, -1, -8], [-15, -25, 17], [0, 10, 20], [12, 6, -5], [-17, 18, 4], [46, -8, 16], [4, -13, 13], [-1, -17, 6], [7, -15, 11], [4, -11, 12], [27, -6, -2], [13, 22, -21], [6, -4, 1], [-8, 7, 4], [3, 18, -11], [-24, 17, -20], [9, 2, 4], [33, -13, 10], [22, -10, 35], [-28, 13, -21], [-7, -8, -10], [-10, -5, -20], [-6, 6, 14], [-40, 4, -17], [12, -26, -1], [20, 9, -12], [27, -4, 17], [-39, 3, -3], [-14, -8, -13], [-20, 8, 13], [-6, 13, -2], [8, 3, 12], [-31, 5, 4], [11, -12, 7], [-28, -1, 13], [-9, 9, -1], [11, -27, 0], [-16, -3, -17], [-31, -10, 3], [-5, -25, 5], [-8, 19, -6], [20, -2, 25], [12, 10, -1], [-25, 6, 14], [-48, 18, 0], [-26, 23, -22], [2, 15, -20], [24, -2, 1], [-20, -8, 14], [-20, -16, 4], [-11, -6, -3], [11, 4, -13], [14, 18, 8], [-20, 30, 0], [-8, -13, 0], [11, -28, 26], [-16, 13, 10], [-4, 1, -4], [-46, 15, -1], [-1, 1, 17], [-28, 7, -32], [11, 4, 27], [7, -11, 2], [5, -2, 15], [18, -12, 8], [14, -25, 13], [-34, -9, 16], [21, 4, -11], [-26, 12, 3], [-17, 25, -35], [-2, -18, 15], [-3, -26, 24], [24, -22, 39], [-22, -11, 10], [48, -2, 6], [-1, -14, 19], [-36, -9, 14], [-36, 19, 13], [-22, 20, -5], [-41, 18, -21], [30, -11, 10], [-32, -15, -5], [-37, 7, -15], [-14, -10, 2], [29, 21, -14], [56, -4, 6], [-9, 29, -8], [-18, 9, -32], [-12, -5, -15], [-26, 0, -30], [11, -34, 24], [14, -43, 21], [-38, -13, 5], [47, 9, -9], [-38, 5, -6], [29, -6, 37], [13, 12, -14], [23, 3, 14], [6, -28, 2], [18, -9, -18], [-4, 17, -13], [21, 11, -7], [-24, 5, -32], [5, -30, 22], [-18, 36, -15], [14, -2, -1], [9, 3, -11], [-21, -19, 21], [-21, 18, -17], [60, 2, -4], [-21, 6, -14], [-25, 11, -1], [-37, 10, 16], [28, 5, 12], [16, -40, 9], [-37, 6, -17], [3, -18, 9], [-25, -6, -10], [55, 0, -1], [-3, 5, -6], [33, -7, 34], [1, 0, 0], [34, -7, 38], [9, -7, 13], [-34, 4, 5], [-16, 0, -32], [-24, -17, 5], [-8, 27, 0], [-20, 6, 17], [3, 4, 20], [6, 27, -22], [0, 14, 12], [-32, 3, 12], [28, 12, -5], [12, -18, -1], [-23, 0, 6], [6, -8, 26], [9, -44, 21], [4, -26, 20], [24, 3, 10], [8, -2, -6], [-16, 43, -24], [-35, 11, 8], [7, -27, 35], [-18, -2, 2], [-24, 13, 22], [-37, -3, -18], [9, 22, -22], [20, 15, 14], [11, 18, -22], [14, -22, 31], [-2, -4, -11], [11, -11, 34], [30, 31, -18], [-4, 7, -22], [5, 16, -17], [-49, 17, -45], [39, -33, -11], [26, -15, 26], [53, -10, 21], [5, 9, -26], [35, -15, 2], [5, 28, -9], [0, 18, -8], [-22, -4, -3], [0, 10, -9], [-13, 5, -2], [52, -19, 27], [-25, 32, -42], [49, -13, -7], [-7, -23, -3], [24, 14, -9], [-4, -15, 14], [-1, -10, -22], [13, 20, -8], [-8, 10, -12], [-76, 0, 0], [1, -9, 13], [14, 21, -9], [30, -15, -6], [31, -16, 10], [-22, -21, -7], [-44, 13, 5], [20, -30, -1], [-3, 7, 5], [-34, 18, -11], [26, -18, 37], [23, -35, 16], [-7, 25, 23], [-48, 8, -20], [21, -50, 13], [9, -21, 35], [-35, -8, -1], [-23, 40, 0], [-45, 1, 14], [38, -4, -12], [8, 0, -25], [12, 13, -29], [1, -19, -24], [28, 10, 0], [12, -5, 23], [-9, 36, -26], [-54, -13, -2], [20, 28, -8], [18, -32, -9], [-23, 41, 3], [-12, 1, -13], [-12, -16, -16], [31, -39, 26], [-39, 18, -22], [24, -35, 27], [36, -10, 3], [6, -20, 4], [-12, -1, 24], [-29, 10, 12], [-59, 16, -20], [-19, 23, 14], [26, 7, -4], [-25, 31, -16], [-32, 4, -22], [14, -3, 11], [-5, 21, -33], [41, -8, 17], [58, -27, -9], [38, -31, 30], [37, -19, 48], [2, -24, -9], [18, 12, 32], [-4, 41, -27], [-6, 9, -7], [-45, 41, -7], [-25, -5, 19], [30, -9, -2], [0, 44, -27], [40, -26, 7], [59, -10, -3], [-34, 24, -3], [18, 5, -9], [43, -22, -9], [21, -7, 17], [-5, 3, -14], [-41, 15, 20], [60, -14, 51], [17, 13, 12], [30, -21, 28], [49, 13, -5], [-27, 37, -5]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4022_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_4022_2_a_b();".
// 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_4022_2_a_b(:prec:=3)
    chi := MakeCharacter_4022_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_4022_2_a_b();".
// 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_4022_2_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4022_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![1, 1]>],Snew);
    return Vf;
end function;