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