
// Make newform 3864.2.a.a in Magma, downloaded from the LMFDB on 20 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3864_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_3864_2_a_a();".
// 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_3864_2_a_a();".
// 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_3864_a();"
function MakeCharacter_3864_a()
    N := 3864;
    order := 1;
    char_gens := [967, 1933, 1289, 2761, 2857];
    v := [1, 1, 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_3864_a_Hecke(Kf)
    return MakeCharacter_3864_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 := [[0], [-1], [-3], [-1], [0], [-1], [-4], [0], [1], [5], [-10], [-3], [-11], [-9], [-3], [12], [10], [-10], [-12], [6], [-4], [12], [12], [6], [9], [-18], [13], [0], [1], [-3], [15], [18], [-11], [3], [12], [-9], [4], [0], [-12], [6], [13], [22], [12], [-25], [15], [11], [20], [-10], [13], [0], [18], [8], [27], [5], [-18], [-31], [-6], [20], [16], [-13], [-6], [-6], [-19], [12], [-6], [3], [-20], [4], [33], [10], [-33], [11], [7], [2], [21], [36], [2], [-18], [-10], [-16], [36], [1], [17], [-25], [-22], [15], [-38], [0], [18], [37], [3], [-26], [-11], [-28], [34], [30], [-28], [22], [-26], [40], [38], [4], [-39], [39], [-36], [-14], [-24], [-39], [-28], [36], [26], [-5], [-22], [-10], [-4], [35], [10], [16], [25], [4], [-10], [-29], [-6], [4], [41], [0], [34], [-19], [-12], [4], [-42], [16], [-28], [-34], [30], [37], [5], [-32], [-23], [52], [35], [6], [-33], [18], [-6], [14], [-27], [-43], [-49], [20], [-32], [-2], [-6], [-48], [-23], [-21], [-4], [-11], [33], [13], [-1], [18], [20], [24], [25], [-10], [-52], [38], [-62], [-60], [-18], [43], [-19], [50], [8], [22], [-2], [26], [31], [22], [28], [47], [0], [23], [-33], [17], [-26], [4], [7], [13], [26], [11], [20], [-34], [14], [56], [-58], [-2], [-2], [27], [-6], [12], [-29], [-26], [27], [0], [-25], [-44], [50], [-28], [-1], [22], [52], [-64], [-45], [-38], [-44], [21], [29], [61], [72], [16], [-54], [-4], [68], [6], [48], [24], [-3], [-24], [34], [18], [16], [-60], [16], [48], [-25], [-6], [-61], [36], [50], [-44], [-12], [18], [31], [63], [-2], [25], [39], [-3], [-57], [66], [-15], [-1], [-12], [-27], [41], [42], [30], [0], [56], [-58], [75], [-46], [-66], [10], [-34], [-72], [-41], [-30], [27], [4], [-71], [34], [70], [62], [13], [42], [50], [-31], [53], [-10], [18], [-12], [-37], [10], [-8], [46], [54], [21], [-20], [75], [8], [47], [-26], [30], [11], [-72], [-42], [-33], [27], [-8], [66], [-25], [47], [-71], [-27], [-60], [3], [34], [51], [-54], [-22], [-44], [-60], [74], [8], [-60], [77], [-4], [14], [-38], [82], [-17], [69], [34], [56], [-53], [-45], [-84], [-4], [29], [-26], [-24], [47], [-20], [9], [-82], [10], [-47], [-10], [27], [-22], [20], [32], [-15], [55], [62], [-72], [-37], [-4], [13], [47], [-56], [85], [-29], [74], [-92], [32], [86], [58], [37], [8], [4], [23], [23], [72], [0], [40], [12], [73], [10], [-64], [-64], [0], [14], [23], [29], [-14], [32], [-46], [30], [8], [-2], [-20], [-29], [97], [-33], [-7], [26], [78], [34], [-75], [-13], [-60], [6], [93], [12], [-85], [-51], [-61], [-66], [-2], [-8], [70], [70], [63], [-60], [-16], [-32], [46], [41], [8], [4], [38], [12], [10], [46], [-54], [-70], [30], [-76], [28], [48], [-64], [4], [-20], [-72], [9], [-59]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3864_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3864_2_a_a();".
// 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_3864_2_a_a(:prec:=1)
    chi := MakeCharacter_3864_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_3864_2_a_a();".
// 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_3864_2_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3864_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![3, 1]>,<11,R![0, 1]>],Snew);
    return Vf;
end function;