
// Make newform 3822.2.a.bv in Magma, downloaded from the LMFDB on 12 May 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_bv();".
// 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_bv();".
// 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 := [-20, -15, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-10, -2, 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_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], [-1, 0, 0], [1, -1, 0], [0, 0, 0], [-1, -1, 1], [1, 0, 0], [2, 1, 2], [2, 0, 1], [0, -1, 1], [-3, 0, 0], [-5, 0, -1], [2, -1, -1], [6, -1, -1], [-4, -2, 0], [-2, -2, 1], [1, -2, -2], [-1, -1, -1], [0, 1, -2], [2, 0, -3], [-12, 0, -1], [8, 1, -1], [5, 2, 1], [3, 0, -3], [10, 1, 1], [1, -1, -2], [2, 0, -2], [0, 2, 0], [-1, 2, -3], [4, 0, 0], [-4, 3, 2], [1, -1, -4], [3, -5, -2], [-6, -1, -1], [-6, 1, -1], [-12, 1, -1], [-9, 1, -1], [2, -3, 0], [-2, 4, 1], [6, 4, -1], [4, 2, -3], [0, 4, 2], [8, 1, 2], [-6, 4, -2], [3, 3, 2], [-12, 1, -1], [-6, 1, -5], [2, 3, -1], [5, -3, -3], [1, 0, 5], [-14, -2, 2], [-6, -1, 4], [0, -2, -1], [11, -4, -1], [-7, 4, 3], [-6, -2, 2], [4, 5, -3], [1, 4, -4], [-1, -5, 5], [2, -1, -4], [-4, -4, -4], [8, -3, 3], [7, -1, 0], [24, 0, -1], [-12, 5, 1], [23, -2, 1], [21, 0, 3], [-4, 0, 6], [-3, 4, 2], [6, 3, 3], [4, 1, -3], [10, 4, 4], [-18, 0, 0], [11, -2, 3], [-2, -7, -4], [-8, 4, 0], [14, -4, 0], [16, -2, -1], [0, -1, -5], [-8, 1, -3], [5, 5, -2], [0, 2, 0], [14, -5, -3], [6, -4, 0], [12, -2, 5], [-9, -3, -4], [-5, -3, 2], [-8, -4, 8], [-3, -3, 0], [-10, -2, 4], [-16, 4, 0], [18, 3, 1], [16, 6, -7], [9, -3, -1], [1, 6, 1], [-4, 6, -4], [-14, 2, 4], [-3, 6, 3], [4, 4, -10], [4, -6, 0], [14, 3, 3], [10, -1, 3], [19, -2, 3], [-31, 0, 1], [-4, 5, 0], [8, -4, -4], [-13, 2, 3], [25, 5, 1], [6, -3, -3], [-8, 1, -3], [-5, -2, -8], [25, 5, -6], [0, 6, 2], [0, 10, -4], [-32, 0, 2], [-15, 0, 3], [8, -4, 4], [-26, 2, -3], [-12, 4, 2], [1, -2, 7], [-10, -1, 3], [-4, 9, 7], [19, 2, 1], [7, -6, 2], [5, 0, 9], [2, -2, 9], [15, 2, 9], [0, 7, -1], [10, 8, -4], [1, 4, 1], [8, 3, 7], [10, -6, -6], [2, 0, -7], [7, -6, 5], [-14, 5, -4], [12, -10, -6], [11, 5, 10], [-14, 8, -4], [-6, 2, -3], [-3, 4, -11], [-8, -1, -12], [20, 4, -2], [-13, -7, -12], [34, 1, 1], [-1, -11, -3], [18, -11, -2], [-14, -2, 7], [4, 11, -5], [10, 5, 8], [-16, 2, 2], [4, 4, -4], [4, -7, -5], [-2, -10, 6], [12, 1, -11], [20, 4, 10], [-22, -5, 3], [46, 0, 0], [16, -2, 4], [-4, -4, 2], [-5, -6, -4], [-17, -2, 1], [12, 5, -10], [44, 3, -4], [-13, -1, 5], [-7, -5, -10], [6, 2, -2], [0, 0, 3], [-19, -2, 7], [12, -9, 2], [-5, 6, 9], [23, -8, -4], [5, -5, -11], [10, 5, -7], [16, 5, 7], [-5, 3, -2], [-19, -1, 2], [-22, -9, 2], [18, 0, 7], [-15, 1, -6], [10, -13, -1], [-14, -3, 0], [-17, -1, -5], [5, -13, 4], [18, 1, 6], [8, 5, 3], [2, -8, -11], [9, 6, -7], [8, 5, 8], [6, 4, -2], [-23, 3, 0], [-14, 10, -3], [17, -4, -10], [9, 5, 1], [-12, -8, -6], [-10, 8, 4], [9, -10, -3], [16, 3, -2], [36, -1, -5], [46, 0, -2], [20, 5, -3], [32, 3, 9], [-25, -8, 7], [4, 2, -10], [2, 11, -1], [-10, -8, 2], [-20, -8, 8], [27, -2, -3], [0, 3, -14], [-17, -2, -5], [-20, -6, 4], [-10, 8, 4], [16, 6, -9], [-9, 6, -12], [21, -4, -3], [-39, -5, -1], [0, 0, -2], [-4, -5, 1], [5, -11, 4], [20, 7, -2], [32, 2, 5], [26, -6, 6], [-14, -9, -2], [-32, -2, 2], [24, -1, -9], [-17, -10, 9], [33, -3, 6], [-4, -13, -4], [-10, 12, -2], [10, 0, -2], [-3, 6, 5], [12, 1, 8], [-62, 1, 0], [-18, -5, 11], [35, 7, -7], [-20, 7, -6], [2, 10, 6], [8, 0, -4], [11, 8, -7], [-7, -3, 8], [32, 2, -4], [-8, 3, 1], [0, 3, 4], [-12, -2, 10], [19, -10, -7], [14, 12, 4], [-2, -4, -6], [14, -8, 4], [19, -7, -7], [60, -1, 4], [-26, -2, -3], [-22, 0, 2], [-18, 9, 1], [22, -6, 8], [-6, 4, 1], [4, 6, 5], [13, -8, 6], [3, -6, -3], [16, 13, 0], [24, 8, -1], [25, 10, -6], [7, 9, -10], [2, -1, 5], [23, -9, 2], [-4, 0, 10], [14, 0, 0], [-2, 5, 11], [-12, -1, 1], [-29, 16, 3], [-42, 9, 3], [-32, 4, -8], [-9, 20, -2], [20, -16, -4], [2, -8, 13], [37, 10, -1], [33, -3, -6], [7, -4, 7], [16, -13, -10], [29, 5, -9], [0, 4, 4], [-24, -1, 5], [30, -1, -5], [-16, 2, 0], [-37, -4, 7], [24, 5, -9], [28, -10, 4], [-28, 6, -3], [0, 8, 4], [-13, -14, 4], [7, 0, 9], [21, -7, -1], [4, 1, -6], [-28, -12, -5], [13, -3, 0], [14, 7, 9], [12, 3, -6], [22, -8, -2], [-21, -6, 6], [20, -6, 10], [-8, 2, 5], [10, -13, -7], [-46, -4, 4], [20, -12, -9], [-9, -6, -3], [-35, 3, -10], [-15, 12, -3], [-12, 7, -9], [-12, -15, 13], [7, 3, -14], [6, -1, 8], [20, 6, 11], [6, 15, -14], [2, 1, 5], [10, -4, 12], [38, -2, -2], [-62, 4, 4], [-14, -12, -5], [50, 2, 8], [-5, 2, -7], [46, -6, 1], [1, 10, 1], [-22, -3, 4], [30, 7, -11], [0, -17, -1], [-43, 6, 8], [8, -9, -12], [-44, 7, 3], [23, 8, 2], [30, -4, 6], [2, -6, 9], [4, -4, 18], [-37, -10, 6], [46, 7, 3], [-19, -6, 1], [10, -15, -3], [-4, 9, -14], [-2, 4, 17], [0, 7, 0], [-34, -15, 15], [36, 7, -9], [7, -15, 0], [51, -16, -3], [-4, -2, -12], [8, 3, -16], [-39, 2, -11], [55, -2, 1], [-21, 1, -16], [39, 10, -3], [-18, -3, -8], [-8, 6, 9], [-30, -12, -7], [3, -8, 3], [-8, 2, -4], [11, 2, -19], [0, -9, 15], [-13, -16, 7], [36, 5, 10], [-28, -12, 2], [-62, 4, 3], [7, 5, 5], [-2, 1, -12], [50, -10, -3], [-44, 13, -1], [-30, -1, 2], [50, 0, -10], [-8, -3, 0], [60, 3, 5], [-9, 0, 20], [8, -1, 3], [-4, 15, 12], [5, -14, 6], [27, 10, 1], [-46, 2, 0], [36, 8, -15], [-61, 0, 7], [52, -2, -2], [-2, -8, -2], [16, -1, -16], [-16, -6, -10], [19, -2, 17], [-20, -16, 8], [12, -16, 3], [16, 5, 11], [-4, 7, 3], [-48, 5, 0], [-12, 3, 17], [-16, 22, 1], [-60, -4, -12], [-28, -1, -6], [-28, -15, 14], [-30, 0, -3], [-2, -1, 19], [-7, 10, 15], [-14, -4, 2], [-1, -4, 11], [44, 15, -17], [-10, 4, -15], [23, -6, 12], [1, -3, 23], [12, 13, -12], [-2, -2, 4], [21, -23, 0], [-8, 1, -3], [4, 2, 4], [-28, 15, -4], [-22, -12, 10], [-31, -4, 9], [59, 2, 9], [-2, -18, -2], [-38, 0, 10], [16, 2, 3], [-29, -7, 7], [-1, -2, 10], [17, 18, -1], [61, -13, 0], [-26, -12, 10], [-4, -4, 3], [53, 8, -10], [38, 12, 16], [22, 1, -9], [-20, -3, -21], [40, -4, -4], [-27, 8, 9], [-54, 0, 10], [-32, -4, -18], [-21, -9, -2], [-40, 11, 3], [2, -10, -8], [21, 15, -2], [26, 6, 2], [-6, -4, 8], [-28, -5, 13]];
    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_bv();".
// 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_bv(:prec:=3)
    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_bv();".
// 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_bv( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3822_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![34, -12, -3, 1]>,<11,R![-89, -27, 3, 1]>,<17,R![382, -63, -6, 1]>,<29,R![3, 1]>],Snew);
    return Vf;
end function;