
// Make newform 3822.2.a.bl in Magma, downloaded from the LMFDB on 24 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_bl();".
// 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_bl();".
// 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], [1, 1], [1, 0], [-1, 1], [-1, -2], [-3, -4], [-3, 4], [0, 3], [1, -3], [-4, -2], [-7, 0], [-2, -4], [-8, -4], [0, 7], [7, -1], [0, 1], [-2, 5], [9, 2], [6, 3], [-6, -4], [-2, 1], [4, -8], [-6, 5], [1, 5], [6, -1], [-1, 1], [-14, -3], [-10, 5], [-13, -3], [-3, -3], [4, 2], [0, -2], [-9, -3], [-7, 7], [-6, -4], [-11, -2], [-12, -3], [-4, -11], [-4, -10], [-3, 6], [-6, 13], [-2, 5], [-3, -5], [-5, -8], [10, 4], [-2, 5], [14, 7], [-8, -2], [2, 9], [18, -4], [-5, 1], [-8, 2], [4, 4], [4, 9], [10, 3], [-18, 1], [-10, 8], [8, 2], [8, -10], [6, 4], [-2, -14], [14, 5], [-6, 17], [-20, -7], [-9, -6], [-24, 1], [-2, -9], [6, -18], [-4, -18], [-4, 0], [0, 3], [-6, -13], [17, -4], [-8, 0], [18, -8], [4, -18], [17, 12], [-5, 11], [-2, -22], [18, -2], [4, 16], [-17, -15], [-2, 2], [3, 11], [-18, -3], [15, 6], [5, 19], [-19, 7], [1, 6], [-2, 28], [-2, 26], [10, 9], [4, 5], [-9, 10], [-5, 11], [18, -9], [-7, -13], [-6, -14], [0, -6], [-7, -1], [-10, -6], [2, 20], [-24, 2], [6, 22], [-6, -1], [29, -6], [4, 18], [3, -27], [-1, 5], [-27, 1], [7, -20], [19, -13], [-10, -23], [-27, 8], [28, -12], [-13, -6], [14, -18], [-18, -12], [7, 22], [-2, -26], [25, -1], [26, -2], [32, 2], [38, 2], [-12, 1], [-29, 15], [-28, 9], [6, 20], [30, 16], [32, -11], [2, -5], [-4, -2], [15, -10], [25, -12], [13, -2], [6, -9], [2, -11], [-27, 4], [20, 6], [12, -23], [-5, 23], [19, 23], [34, 16], [28, 4], [-42, -2], [-32, -12], [6, -12], [0, -6], [23, 13], [39, -12], [-28, -22], [54, -2], [7, -12], [2, -10], [16, -10], [-12, -21], [-38, 0], [17, 3], [8, 20], [-3, 1], [40, -6], [-21, -19], [25, -8], [26, 8], [30, 20], [-24, -1], [14, 19], [19, -11], [26, -13], [38, 2], [-58, 0], [-39, 9], [-6, -26], [12, -5], [18, -15], [28, 22], [3, -7], [-17, -11], [4, -2], [22, -6], [-34, -12], [14, 15], [-26, 21], [12, 32], [-33, -20], [30, -8], [61, -2], [8, 26], [7, -25], [14, 22], [-27, 0], [22, -4], [41, -13], [-18, 9], [-6, 35], [-2, -15], [54, -7], [-12, -10], [5, 15], [-22, -17], [-44, 15], [20, -4], [14, 22], [0, 19], [-8, -4], [29, 13], [-26, -23], [1, -38], [0, 14], [10, 2], [46, 0], [11, 4], [-5, 12], [-14, -10], [-19, -3], [-26, -21], [2, 41], [16, 26], [-12, -19], [-25, 17], [20, 18], [3, -11], [-6, -31], [-13, 35], [-8, 8], [-45, 9], [-25, 11], [18, -33], [8, 17], [-15, 7], [16, 0], [26, -6], [-45, -4], [11, 22], [-16, -24], [38, -12], [26, -21], [6, 40], [-12, -14], [-21, 1], [46, -6], [22, 23], [6, 29], [58, 13], [24, 3], [18, -16], [-7, 36], [-15, 31], [8, 31], [-26, 13], [-16, -21], [-32, 0], [43, 9], [-36, 20], [-30, 30], [0, -45], [-9, 32], [8, -21], [4, -41], [-54, -2], [39, -15], [20, 3], [-44, -23], [36, 3], [-46, -5], [2, 39], [-12, -14], [-38, -4], [-14, -4], [-5, -30], [-68, 0], [-64, 12], [-16, -20], [-53, -15], [-39, -13], [-44, -16], [6, 16], [24, -20], [-35, -1], [-14, 25], [4, -34], [-7, 52], [10, 16], [21, 18], [-52, -3], [72, -6], [7, -31], [-65, -4], [-29, -7], [5, -47], [52, 12], [6, 4], [2, 23], [-4, 40], [42, 0], [53, 23], [-41, 22], [-38, 21], [0, -40], [18, -40], [28, -5], [19, 6], [-14, 27], [-16, 44], [-9, -43], [56, -19], [-30, 2], [-10, 29], [44, -8], [-60, 19], [-1, -26], [-18, 30], [-60, -18], [-12, -36], [-46, 21], [3, -14], [6, 42], [-80, -5], [-50, -15], [-6, -41], [-17, -29], [-45, -17], [6, -10], [0, 10], [-5, 12], [11, 8], [17, 6], [-41, 15], [17, 18], [32, -32], [10, -9], [20, 14], [-6, 12], [25, -3], [-40, -2], [-12, -8], [47, -17], [4, 6], [20, -31], [-1, -30], [6, 16], [56, 3], [2, -49], [-9, 45], [-12, 35], [-42, -24], [14, -22], [-42, 28], [-58, 5], [-12, 40], [-56, -26], [42, -7], [-1, 31], [25, -44], [58, -7], [-6, 23], [26, -19], [-11, 23], [-19, -23], [43, 6], [68, -16], [14, -10], [12, 58], [-15, -16], [-15, -31], [-25, -32], [10, 43], [22, 5], [-22, -55], [-27, -50], [-35, 25], [52, -17], [8, 50], [41, 18], [15, -46], [-37, -5], [56, 3], [12, 40], [-17, 13], [19, 55], [-6, 34], [46, 0], [-23, 30], [-25, -37], [-33, 45], [-8, -14], [8, 9], [2, -8], [-48, 14], [-4, -26], [-42, -3], [-42, -31], [32, -14], [14, 55], [-47, 17], [9, -22], [36, -9], [-36, -10], [6, -39], [-75, -21], [-20, -6], [16, -2], [15, 7], [6, -26], [49, -5], [1, -60], [32, -28], [-24, -37], [-9, 24], [-52, 2], [11, 42], [-13, 42], [-12, -40], [56, 3], [-57, -11], [-18, 22], [32, 6], [63, 28], [-34, -34], [76, -14], [46, 4], [-67, -4], [-30, 32], [-58, 27], [-88, 10], [-5, 2], [-20, 30]];
    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_bl();".
// 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_bl(: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_bl();".
// 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_bl( : 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![-1, -2, 1]>,<17,R![-1, 2, 1]>,<29,R![-23, 6, 1]>],Snew);
    return Vf;
end function;