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