
// Make newform 3822.2.a.bx in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3822_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_3822_2_a_bx();".
// 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_3822_2_a_bx();".
// 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 := [8, 8, -7, -2, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 0], [-4, -1, 1, 0], [2, -6, -1, 1]];
    Rf_basisdens := [1, 1, 2, 2];
    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_3822_a();"
function MakeCharacter_3822_a()
    N := 3822;
    order := 1;
    char_gens := [2549, 3433, 1471];
    v := [1, 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_3822_a_Hecke(Kf)
    return MakeCharacter_3822_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, 0, 0], [-2, 1, 0, 0], [0, 0, 0, 0], [1, 0, 2, -1], [1, 0, 0, 0], [-1, 2, 2, -1], [0, 1, 0, -2], [-2, 1, 0, 2], [1, 2, -1, -1], [-1, 1, 2, 1], [2, 1, 1, -2], [-3, -1, -1, -1], [6, -1, 0, -2], [-3, -3, -1, 1], [1, 3, 1, -1], [-3, -1, 6, -1], [-3, 2, -4, 5], [2, 2, -3, 0], [-3, 1, -2, 1], [0, -1, 8, -2], [-9, 3, 2, 3], [-8, 2, -4, 0], [-6, 0, 1, -2], [0, 4, -2, -2], [-9, 3, 0, 5], [6, -1, -1, -4], [8, -2, 5, -6], [2, 3, -7, 2], [5, -3, -4, -3], [5, 1, 8, -1], [1, -4, 4, -3], [7, -2, 2, 3], [4, -4, -2, -4], [-6, 2, -2, 0], [-3, 4, -4, -1], [11, -2, 6, -1], [3, 3, 5, -5], [5, -4, -3, 5], [1, -1, 8, -3], [-4, 0, -5, 2], [8, -4, 0, 2], [2, -3, 2, 6], [11, -1, 6, 3], [11, 3, -4, 3], [10, -1, 1, 4], [2, -3, 12, -2], [10, -4, 6, -6], [-3, -7, -4, 3], [1, 5, 0, -1], [10, 2, -6, 4], [5, -3, -6, -3], [0, -2, 10, -6], [1, 4, 6, -5], [8, 0, -4, -2], [8, 4, -4, 0], [3, 1, 4, -7], [11, -5, -2, 1], [11, -5, -4, -5], [4, 2, 8, 0], [6, -2, -8, -2], [-10, 2, 6, -4], [-8, 2, -8, 4], [12, 6, -2, 0], [16, 6, -1, 0], [-9, 7, 4, -1], [-12, 8, -1, -6], [18, 1, 2, -2], [2, 2, 5, 0], [5, 9, -8, 3], [-7, 5, -5, 7], [10, -6, 6, -4], [-1, 1, -3, 3], [15, 1, 6, -5], [-6, 4, 7, -4], [-11, 0, -1, 9], [7, -3, 15, -3], [4, 2, -12, 0], [14, -2, -4, -2], [-10, 9, 0, -4], [3, -6, 12, -5], [15, -1, 9, -3], [2, -8, 0, 6], [9, -5, -11, 3], [8, 3, -3, 6], [-3, 9, 5, 1], [17, -6, -2, -7], [15, 3, -2, 7], [10, -11, -6, 4], [-5, 0, 8, 5], [7, -4, -20, -1], [-1, -2, 3, -9], [-20, -2, -12, 4], [-7, -3, 13, -3], [-14, 4, 5, 8], [7, -3, 16, 5], [-18, 1, -2, -8], [19, -6, 0, -5], [9, 1, 2, -1], [-18, 5, -5, 2], [-4, 6, 2, 0], [10, -2, 6, 8], [7, 8, -18, 5], [-4, 6, -20, -2], [-8, 2, -12, 0], [24, 0, 12, -2], [-20, 6, 14, 0], [-6, 0, 3, 6], [-22, 5, -6, -2], [9, -5, 7, -9], [0, -9, 13, -10], [6, -1, -19, -2], [21, -8, 2, -9], [-14, -9, 6, -2], [-13, 10, 16, 1], [9, 5, -4, -3], [-30, 9, 10, 6], [8, 0, 8, -12], [3, -14, 11, 1], [-17, -5, -1, -5], [-6, -4, 0, 8], [-10, 5, 26, 2], [-10, 8, -4, 10], [-19, 4, 0, -5], [6, 0, 4, -2], [25, -13, -13, -1], [-13, -5, 9, -3], [19, -3, 8, -7], [12, -5, 11, 6], [21, -5, -4, 1], [-5, -1, -3, -5], [0, -2, -16, 8], [13, -5, 0, -9], [-3, -7, 16, -7], [-25, -3, -3, 5], [-12, -11, 8, 2], [18, -9, 4, -4], [-28, -1, -2, 4], [-13, 7, -12, 11], [-9, -5, 6, 7], [2, 9, 8, 0], [6, -10, 2, -12], [-11, -13, -2, 1], [1, -6, 16, -1], [-27, 6, -8, 9], [-13, -5, -7, 7], [14, -2, 4, -4], [0, -6, 12, 6], [-8, -8, -8, 0], [-16, -2, 26, -2], [11, -15, -3, 1], [21, -4, -4, 5], [-14, -7, -16, 2], [-20, 8, -10, 0], [-2, 8, -16, -6], [-2, -9, 4, -6], [-26, 4, 2, 0], [9, -13, 7, -5], [8, -8, 21, -8], [40, 2, -2, 2], [9, -8, -20, -1], [-12, 0, -6, -6], [1, 2, -12, -1], [-2, 2, 14, -8], [-3, 22, 6, -9], [-5, 2, -11, -9], [-24, 6, -2, -2], [0, 14, 14, -2], [9, -1, 18, 5], [21, -3, 0, -13], [-35, 6, 6, -5], [-17, -1, -4, -9], [0, 10, 14, -4], [-4, 6, 14, 2], [22, -1, -5, 12], [36, -6, -8, -6], [-10, 2, 19, -8], [7, -1, 8, -5], [-5, 5, 23, -5], [-17, 4, 4, 3], [-27, -4, -20, 3], [34, 2, 6, -4], [4, -2, -24, 2], [21, -7, 13, 7], [7, -13, 10, -1], [7, -5, 8, -11], [-4, 8, -6, 6], [22, -5, 6, -14], [2, -8, -12, -8], [-23, 0, -19, 17], [1, 7, 23, 3], [-13, -4, 14, 5], [26, -4, 8, -6], [-4, -7, 18, -10], [15, -9, -15, 7], [-21, -12, -16, 11], [19, -9, 6, -5], [-23, 5, 8, -3], [-6, -8, -3, 6], [23, 7, 20, -15], [24, -8, 8, -6], [2, -1, -11, 4], [3, -9, -8, 11], [20, -20, -15, 4], [-8, 16, -8, 12], [8, 6, 4, 6], [21, 19, -12, 3], [-13, -3, -7, 3], [17, 12, 26, -9], [-9, 15, 14, -7], [28, 9, -2, 6], [-8, 4, -20, 6], [-10, 16, -16, 2], [-18, -4, 6, 6], [17, -2, 19, -3], [18, -13, -8, 4], [14, 4, 6, 8], [-5, 0, -18, 3], [11, 11, 0, -7], [-17, 7, 0, -13], [12, -4, 4, 6], [-9, -11, 4, -7], [-3, -14, 6, 7], [-22, -14, 0, 2], [17, -28, -2, 3], [-9, 7, -16, -5], [7, -10, 20, -5], [2, -2, 18, -10], [-26, 1, -1, -4], [21, -14, -12, -9], [17, 9, -12, 9], [15, -7, 2, -7], [-17, -16, -4, 1], [6, 2, 20, 8], [-8, 6, 18, 4], [7, 8, 5, -5], [-20, -7, 8, 10], [24, 4, 14, -2], [25, 1, 7, 1], [5, 9, 8, 3], [6, 8, -18, 6], [16, 4, 18, -16], [10, -11, -21, 0], [-17, 7, -7, -19], [8, -6, -11, 0], [1, 9, -22, 11], [-37, 7, 0, -7], [17, -9, -22, -1], [-9, -13, 25, -9], [-4, -1, 26, 8], [32, 3, -3, -4], [0, 0, -11, 12], [-3, -7, -24, 7], [-4, 0, -19, 0], [4, 12, 0, -4], [-27, -18, 6, -1], [52, -4, 6, 6], [-14, -12, -6, 0], [31, 9, -20, 3], [-28, -3, 2, -2], [-13, 5, 18, 15], [-8, 20, 5, 2], [22, 12, -8, 14], [3, -28, -8, 7], [-16, 0, -15, -12], [3, -7, 14, 13], [-3, -5, -12, -3], [36, -18, -7, 0], [0, -6, 9, -22], [17, 7, -21, 7], [-30, 8, 12, -4], [9, -7, -5, 9], [24, -3, 0, -2], [-48, 12, 6, -6], [-25, -3, -33, 21], [24, -8, 16, 8], [-7, 18, -2, -5], [-30, 9, -21, 26], [14, -10, -8, 12], [12, 14, -12, 8], [38, -10, 6, -10], [-23, 18, 10, 7], [-19, 1, -6, -21], [-6, 10, -16, 22], [-34, 1, -14, -6], [0, 14, 20, 4], [14, -13, 10, -10], [17, -17, -2, -19], [10, 2, 20, -10], [-21, 6, -30, -5], [-5, -2, -19, -3], [49, -4, -8, -7], [-5, 2, -2, 3], [16, -12, 4, -4], [4, -2, -30, -6], [3, -1, 44, -9], [-19, -5, 19, 1], [-18, 0, -16, -12], [3, -8, -20, 7], [-16, 9, -34, 16], [-28, 10, 3, 4], [16, -8, -6, -6], [36, -2, -4, 4], [-24, 16, -7, -6], [2, -3, -14, 22], [-17, 11, 16, 7], [17, -17, -5, -7], [-5, 4, 0, -17], [13, -9, 8, -1], [-28, 10, 20, -2], [23, -5, -12, 5], [-28, -12, -18, 18], [11, 1, 12, 7], [-4, 7, 24, -8], [2, 0, -14, 24], [3, 9, 21, -7], [8, 0, -16, -8], [33, -3, -4, 3], [2, -21, 14, 4], [-6, -4, 14, 8], [21, -9, 6, -21], [25, -19, 8, 9], [-1, -1, -8, 3], [-51, -4, -6, 7], [-7, 2, -18, 5], [5, -7, 23, 7], [-13, -3, -25, -9], [-26, 23, 8, -8], [-20, -19, -30, 16], [17, -10, -27, -13], [-22, -13, -17, 2], [17, 6, 29, -5], [5, 7, 3, -23], [-53, -5, -16, 5], [1, 15, -15, 21], [26, 8, 2, 10], [-22, 17, 5, 22], [-29, 17, 25, 5], [-34, 6, -4, 8], [30, -3, 9, 0], [-13, 5, 29, 13], [37, 3, 24, -7], [-26, 9, -10, 4], [4, 2, 34, 2], [39, 1, -10, -5], [-4, -6, -39, 6], [9, 22, -22, 13], [44, -4, 13, -18], [-27, 5, -13, -7], [-40, 6, -28, 14], [4, -18, -20, 0], [-9, -21, -2, 3], [40, 12, 10, -10], [-37, 17, 19, 3], [-19, 5, -30, 1], [7, 2, -20, 23], [-3, -8, 15, 1], [15, 19, 22, -23], [1, 21, 2, -7], [1, -3, 16, 11], [6, 17, -13, 18], [1, 10, 20, -11], [-28, 5, -36, 16], [-24, -4, 0, 0], [-10, -16, -8, 6], [68, -8, -4, 2], [-60, -3, -6, -6], [-1, 8, 2, 9], [12, -17, 26, -4], [-2, 8, -5, 0], [-5, 7, -30, -15], [33, -15, 4, 19], [-35, -10, 13, -9], [8, -7, 21, -10], [-21, -3, 18, -9], [36, -8, 10, 4], [46, 5, -22, -2], [-22, 23, -6, -8], [21, -8, -10, -7], [-6, -10, 3, -2], [-46, -14, -20, 16], [11, -20, 2, -5], [31, -10, 28, -15], [10, -24, -8, 6], [-48, 2, -12, 4], [51, -6, -21, -5], [-27, 28, 14, -1], [19, -12, 36, -25], [17, -1, -39, 5], [9, 11, -6, 19], [-8, -22, 18, -6], [30, -6, -22, -12], [10, -14, -28, 10], [-33, 7, -22, 25], [-35, -3, 4, 15], [8, 16, 28, -2], [23, 11, -6, -7], [-29, -12, 10, 7], [34, -21, -6, 2], [3, 29, 26, -17], [-12, 4, -48, 2], [27, -17, 6, -9], [-3, -10, 26, -21], [13, -9, 3, -29], [-8, 8, 48, 2], [6, 13, -11, 0], [11, -5, -7, -3], [17, 16, 18, -23], [-38, -11, 2, 14], [24, 8, 28, -4], [14, 2, 15, 6], [27, -18, 5, 5], [-43, 15, 1, -15], [-28, -5, -40, 4], [-4, 11, -10, 6], [14, -34, 4, 0], [-57, 21, 16, 5], [-2, -3, -41, 4], [1, 21, 7, -9], [20, -20, -26, 4], [-27, 4, -1, -9], [6, 12, 4, -14], [42, -2, 18, 16], [-28, 14, -10, -10], [-4, 15, 30, -14], [-44, 6, 2, 14], [-2, -4, 11, 8], [-12, -4, -48, 10], [-24, -11, -6, -8], [24, 8, 14, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3822_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3822_2_a_bx();".
// 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_3822_2_a_bx(:prec:=4)
    chi := MakeCharacter_3822_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_3822_2_a_bx();".
// 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_3822_2_a_bx( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3822_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![-4, -12, 5, 6, 1]>,<11,R![-32, 48, -17, -2, 1]>,<17,R![316, -12, -53, 2, 1]>,<29,R![254, 108, -41, -6, 1]>],Snew);
    return Vf;
end function;