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