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