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