
// Make newform 3822.2.a.bs 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_bs();".
// 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_bs();".
// 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 := [-2, 0, 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], [-1, -1], [1, 0], [1, -3], [3, 0], [-2, 5], [5, 0], [4, -2], [2, 5], [6, 1], [2, -2], [9, 0], [1, -8], [9, 1], [-1, 3], [-9, 4], [-1, 0], [6, 5], [2, -8], [-2, 4], [-2, 3], [-2, -7], [-4, -2], [-2, -6], [8, -4], [-4, -4], [1, 5], [0, -7], [0, -7], [-6, 5], [0, 15], [6, -7], [-9, -1], [15, 5], [3, -10], [1, -8], [9, -8], [-18, -4], [-13, -1], [8, 4], [-20, -3], [-6, -11], [12, 1], [-10, 13], [3, -13], [-12, 10], [-10, -2], [9, 9], [-11, -6], [-8, 6], [10, -2], [-2, 14], [8, 11], [-15, -8], [3, 13], [-15, 1], [-4, -8], [10, -5], [-6, 9], [3, 16], [-8, -1], [4, -10], [-2, -2], [-14, 6], [23, -4], [10, 3], [4, -1], [-10, -16], [10, 18], [14, 0], [-21, -7], [-28, 4], [-2, -6], [23, 0], [-24, -3], [12, -5], [-4, -1], [-2, 18], [12, 1], [6, -6], [-19, 0], [-12, -1], [10, -9], [0, 24], [6, 15], [-8, 18], [-4, 24], [4, 3], [-17, 10], [-11, 9], [2, 20], [-30, 0], [16, -14], [-4, -12], [-14, 0], [-18, -10], [-14, -19], [-10, -3], [6, 12], [8, 6], [-27, -1], [4, 4], [-18, 0], [7, -3], [-30, -1], [12, 19], [-15, 2], [20, -5], [-16, 14], [2, 6], [18, -18], [14, -16], [36, 2], [25, -8], [6, -16], [-16, -22], [18, -9], [2, 11], [-4, -2], [31, 2], [-20, -18], [-3, -10], [40, -6], [-8, 17], [6, 20], [-36, 2], [12, 29], [-10, -12], [3, -6], [-40, -4], [39, 11], [-32, -10], [-14, -5], [10, 28], [-11, -2], [-20, 12], [11, -17], [-10, -26], [-28, -13], [-18, -7], [-19, -23], [-11, -21], [11, 6], [12, 21], [-33, 13], [20, 14], [4, 0], [22, -1], [-10, 22], [-8, -5], [30, -8], [-42, -7], [54, 0], [-6, 14], [-10, -20], [1, -28], [-4, 12], [13, -3], [11, 15], [-9, -13], [42, -11], [-6, -6], [-25, -2], [4, 8], [-23, -21], [-40, 8], [27, -2], [29, 1], [-6, -25], [6, 3], [12, 33], [-38, 17], [3, -3], [41, -2], [-16, 27], [14, 27], [-17, 13], [-27, 1], [4, 5], [-3, -11], [-2, -13], [15, -2], [22, -8], [13, -7], [36, -4], [-48, -3], [5, -6], [7, -30], [-5, 23], [22, -12], [30, -24], [-18, -24], [25, 13], [12, 19], [-52, 2], [-20, 3], [6, 33], [-28, -2], [4, 34], [-10, 1], [-12, -34], [28, 4], [4, -18], [59, 1], [10, 12], [-30, 10], [10, 20], [-25, -18], [-63, 0], [-20, 18], [-37, 9], [6, 6], [2, -41], [14, -1], [-11, -1], [-1, -26], [-10, -10], [-1, 37], [60, -6], [-28, -11], [60, 4], [16, -25], [-5, 35], [40, 6], [40, 0], [6, 24], [19, 9], [-49, -5], [-6, -39], [-61, 3], [57, -9], [54, 14], [-12, 12], [-42, 22], [-2, -25], [6, -18], [34, -23], [13, 13], [-36, -18], [56, -12], [10, 0], [56, -8], [34, 16], [25, -11], [-7, -41], [-27, -16], [-64, 0], [26, 31], [20, 42], [-49, -16], [13, -26], [-9, -22], [40, 20], [49, 3], [11, -14], [1, -28], [-30, 23], [24, -37], [24, -15], [-58, 0], [22, -8], [48, -5], [-34, 25], [-40, 18], [-72, 3], [12, -36], [13, -12], [20, 2], [63, -6], [8, 20], [12, -9], [20, 12], [-65, -11], [29, 35], [-52, 4], [38, -1], [22, 35], [30, 30], [-24, 26], [-38, 7], [2, -18], [-17, -4], [-8, -24], [41, -24], [-18, 2], [-11, 13], [-5, 33], [13, -8], [10, 39], [-28, 33], [-1, 25], [36, 22], [-17, 10], [24, -38], [21, -14], [-52, -21], [22, 22], [41, -10], [-60, -8], [26, 21], [44, 14], [24, -35], [12, 29], [56, 17], [21, 3], [-19, 36], [47, -15], [22, -17], [30, 24], [-10, 46], [-14, 12], [-15, 14], [-28, 28], [-12, 4], [45, -6], [62, -16], [-9, -33], [44, 9], [0, -21], [3, -2], [13, -41], [-32, -11], [-27, 26], [36, 20], [25, -36], [6, -48], [-21, 16], [2, -19], [-50, 20], [10, -19], [49, -21], [-21, -4], [5, 49], [-16, 7], [12, 29], [36, 11], [22, 42], [26, -22], [11, 27], [22, -4], [-48, -4], [-48, -1], [-48, 20], [-5, 25], [47, -26], [31, 16], [-16, -4], [58, -24], [12, 12], [-8, -17], [44, -6], [15, 29], [14, -24], [-31, -46], [9, -25], [-21, 25], [41, -18], [54, -5], [51, -11], [-32, 36], [7, -45], [-38, 9], [51, 18], [-12, -25], [5, -11], [33, -36], [28, -8], [36, 14], [-15, 30], [28, -44], [4, 34], [12, 34], [-15, -41], [-36, 30], [-20, 36], [0, -42], [-47, 0], [-18, -3], [-36, -19], [7, 51], [-10, -7], [-33, -6], [-20, 4], [-21, -55], [-41, 9], [7, 34], [-26, 5], [-28, 8], [64, 4], [-8, -38], [-64, -11], [27, 8], [37, 28], [-43, 13], [21, -51], [-12, 18], [-42, -1], [32, 19], [-62, 22], [-47, 23], [-60, -20], [16, 58], [-16, 8], [30, -42], [16, 40], [5, -12], [7, 25], [35, 44], [-52, -2], [-16, -3], [-40, -4], [-75, -6], [-1, -4], [22, -34], [40, -39], [-2, 19], [20, 12], [-36, 8], [20, -28], [-14, 54], [0, -31], [12, 3], [-36, 2], [-38, 1], [-22, -6], [42, -14], [-38, -7]];
    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_bs();".
// 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_bs(: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_bs();".
// 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_bs( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3822_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![-2, 0, 1]>,<11,R![-1, 2, 1]>,<17,R![-17, -2, 1]>,<29,R![-5, 1]>],Snew);
    return Vf;
end function;