
// Make newform 3822.2.a.bt in Magma, downloaded from the LMFDB on 25 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_bt();".
// 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_bt();".
// 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 := [-10, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    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], [1, 0], [0, 1], [0, 0], [2, 1], [-1, 0], [0, -1], [-2, -1], [2, -1], [0, -1], [0, 0], [8, -1], [2, 2], [-2, 3], [8, 0], [-2, 0], [4, -4], [-4, -1], [0, -2], [-8, 0], [4, 1], [4, -2], [8, -2], [-2, -4], [-2, -4], [-2, -2], [6, 1], [0, 2], [12, 1], [-6, -4], [-12, -2], [10, 1], [-16, 1], [4, 4], [10, -2], [-2, 1], [-12, 3], [0, -2], [-6, -1], [18, 0], [4, -4], [2, 4], [10, -1], [6, -2], [2, 2], [-14, 3], [-22, 1], [-4, 2], [-16, 2], [-6, 0], [-2, 6], [16, 0], [14, -4], [10, 5], [6, 0], [-16, 4], [-6, -4], [4, 6], [6, -8], [2, -4], [-4, 0], [2, -4], [-4, -4], [-20, -2], [-6, 6], [2, -2], [16, -6], [-16, 5], [4, 0], [-30, 0], [-10, 4], [16, -4], [8, -8], [-14, 2], [4, 8], [2, 7], [14, 0], [10, 4], [-6, -4], [-8, -1], [-26, 3], [-2, -8], [4, 6], [2, 6], [14, 5], [-16, -6], [-16, -3], [6, 2], [-4, -9], [-10, 9], [18, 5], [6, 5], [-8, 0], [32, 2], [-12, -4], [28, -2], [-20, 7], [4, 1], [12, -8], [-4, 1], [-12, -8], [-30, -2], [-6, 1], [26, 0], [-12, -4], [-2, -4], [-8, 10], [6, -4], [14, 1], [10, -2], [6, 5], [20, -7], [-12, 7], [-10, -1], [6, 5], [-38, 4], [-22, 9], [8, 0], [4, 5], [-8, 10], [18, -12], [24, -7], [-6, 0], [-2, -5], [-28, 8], [14, 4], [30, -8], [4, -10], [14, -3], [-26, -2], [24, -6], [-4, -10], [24, -4], [-2, -12], [-38, -2], [-32, 7], [4, -9], [-2, 7], [46, 2], [10, 8], [30, 3], [34, 2], [16, 4], [34, -7], [12, -9], [-24, 4], [6, -6], [6, -4], [-28, 4], [-28, -2], [22, 8], [-16, -9], [22, 3], [8, 0], [-4, 8], [22, -7], [-20, -2], [-6, 10], [-18, 4], [50, 0], [18, 5], [-2, 14], [-22, 11], [-44, 4], [-12, 11], [30, -3], [-44, -2], [2, -4], [-6, 4], [-18, 6], [26, -11], [-14, 8], [40, 0], [-6, 16], [10, -5], [-34, 8], [-32, 10], [-38, -2], [32, -8], [-16, 5], [34, -5], [12, -12], [54, 0], [-26, 0], [24, -4], [22, 2], [-14, 6], [-18, 7], [-54, 0], [26, -5], [2, 6], [18, 1], [-28, 8], [0, 5], [8, 2], [48, -5], [-10, -2], [-34, -8], [-10, 12], [24, 0], [-14, 14], [14, 9], [-22, 8], [-26, -4], [-4, 0], [-30, -8], [40, 4], [-4, 8], [-12, -9], [-44, -4], [4, 3], [-34, 6], [4, 2], [40, 2], [34, -13], [40, -1], [4, -2], [0, -1], [-8, 0], [-30, 6], [-22, -2], [-56, 0], [-24, 17], [-24, -4], [38, -5], [22, 8], [-16, 3], [16, -16], [22, 1], [70, -1], [-34, 0], [-8, -2], [2, -13], [38, -6], [16, -18], [-22, -5], [40, -5], [10, -2], [0, -14], [0, 12], [-12, 0], [36, -12], [-2, 13], [6, 8], [-18, -8], [16, -4], [-12, 14], [52, 0], [0, 2], [-10, -7], [-32, 15], [26, -6], [24, 8], [-34, 4], [38, 6], [-18, 15], [18, -6], [44, 0], [18, -20], [12, 5], [48, 0], [-12, -4], [-30, 4], [20, -13], [-14, 6], [32, -14], [-34, 0], [-34, 4], [12, 12], [-42, 4], [-46, 0], [40, -14], [12, -11], [0, 8], [2, -2], [64, 4], [22, -9], [32, -1], [38, -6], [4, 16], [-24, 20], [18, -9], [-8, 8], [30, -18], [14, -13], [24, -4], [8, 9], [14, 4], [-16, 4], [-20, -15], [-60, 5], [-10, -5], [0, 1], [-28, -4], [-18, -8], [-58, 6], [-24, -8], [-30, 4], [38, 7], [-22, -7], [42, -10], [50, -2], [4, 6], [-60, 4], [-22, 5], [46, 2], [-28, 8], [-8, 17], [-20, -10], [-26, 0], [48, 0], [18, 18], [-26, 6], [22, -13], [4, 18], [-54, -2], [12, 12], [0, 0], [-24, 19], [-62, 0], [-8, -18], [6, -14], [22, 6], [-42, 9], [-8, 13], [38, -4], [-12, -12], [-2, -5], [26, -1], [44, -3], [-32, 7], [-4, -15], [40, 8], [40, -2], [-44, 0], [16, 14], [48, 7], [-14, -2], [-10, -4], [-2, 9], [6, -12], [-18, -2], [-24, 13], [36, 10], [46, -16], [-56, -6], [-32, 1], [24, -10], [-8, 8], [-62, -4], [-4, -4], [62, 6], [-54, -6], [-16, -4], [70, 0], [-16, 19], [30, 1], [0, 14], [-38, 4], [-16, 12], [44, -3], [-48, -5], [10, 7], [12, -24], [32, -14], [34, 4], [-28, 3], [-46, -1], [20, -17], [8, 2], [52, -4], [-8, -4], [-16, 3], [58, 7], [42, 10], [-4, 20], [70, 1], [0, 23], [-16, 7], [-42, 16], [66, 0], [76, -5], [58, 3], [2, 16], [-28, 8], [-46, -5], [50, -5], [-12, 23], [20, 16], [-20, 2], [-14, -16], [-14, 4], [36, 4], [-44, -8], [-8, 8], [-38, 0], [60, 10], [-48, -5], [-10, 7], [6, 16], [30, 2], [82, 0], [54, 1], [2, -14], [-14, 4], [-50, -3], [-2, 4], [8, -7], [6, 11], [-36, 12], [30, 0], [24, -1], [28, -4], [-30, -3], [60, -13], [-10, -10], [-68, -6], [-46, -5], [6, -4], [-64, 12], [20, 5], [-62, 4], [24, 4], [60, -16], [60, -11], [26, 12], [-24, -6], [2, -20], [-26, -9], [0, -12]];
    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_bt();".
// 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_bt(:prec:=2)
    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_bt();".
// 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_bt( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3822_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![-10, -1, 1]>,<11,R![-4, -5, 1]>,<17,R![-10, 1, 1]>,<29,R![-10, 1, 1]>],Snew);
    return Vf;
end function;