
// Make newform 6864.2.a.q in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_6864_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_6864_2_a_q();".
// 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_6864_2_a_q();".
// 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_6864_a();"
function MakeCharacter_6864_a()
    N := 6864;
    order := 1;
    char_gens := [2575, 1717, 4577, 4369, 2641];
    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_6864_a_Hecke(Kf)
    return MakeCharacter_6864_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], [1], [1], [-8], [6], [1], [1], [0], [6], [-11], [11], [12], [2], [-7], [11], [-9], [2], [-11], [-6], [-14], [0], [6], [-6], [-5], [-9], [-16], [-3], [-2], [-19], [-18], [-16], [20], [16], [-10], [5], [15], [-5], [0], [-2], [7], [10], [-12], [-19], [4], [-20], [28], [-21], [10], [17], [10], [-6], [7], [-16], [-24], [0], [-29], [-17], [-9], [-18], [6], [16], [11], [-17], [15], [-22], [-36], [4], [-2], [-25], [16], [-29], [-32], [-6], [-30], [7], [12], [-29], [-28], [15], [0], [-3], [-24], [12], [28], [-41], [4], [14], [-12], [-5], [26], [13], [-11], [-12], [-42], [3], [20], [22], [43], [-10], [0], [-10], [-5], [-16], [-9], [10], [-11], [-20], [22], [24], [38], [39], [-22], [27], [28], [-32], [38], [-24], [22], [36], [6], [33], [-8], [5], [27], [-17], [20], [-2], [-4], [47], [13], [-22], [-33], [5], [-22], [40], [36], [30], [-12], [18], [-15], [52], [26], [-2], [-16], [-44], [-4], [-40], [-48], [1], [6], [-20], [12], [-32], [4], [-28], [-28], [48], [28], [24], [9], [38], [-4], [-26], [-17], [21], [15], [-12], [42], [-55], [-12], [-47], [-13], [27], [2], [-3], [-12], [23], [26], [-56], [-14], [-9], [-14], [-24], [2], [9], [25], [57], [34], [-30], [20], [11], [-16], [38], [30], [38], [-29], [66], [6], [8], [-17], [-30], [11], [42], [4], [-39], [-6], [-46], [-38], [4], [41], [20], [32], [40], [48], [-64], [-54], [34], [-43], [61], [-60], [-8], [21], [-10], [30], [-11], [-42], [-20], [22], [-7], [-35], [-2], [-20], [72], [50], [-22], [10], [-8], [73], [-25], [13], [-10], [-23], [6], [42], [-45], [-44], [-48], [-54], [-50], [78], [19], [-41], [-58], [-68], [18], [24], [77], [4], [7], [-4], [29], [24], [-10], [-1], [16], [-22], [-74], [40], [-28], [50], [32], [-44], [26], [63], [-48], [-33], [-49], [-42], [16], [-70], [72], [-4], [65], [-82], [72], [-48], [-64], [6], [-18], [57], [-58], [-5], [79], [83], [-60], [-27], [-28], [34], [28], [16], [-70], [-30], [-42], [66], [-66], [8], [-75], [76], [47], [-8], [38], [-60], [-68], [-9], [-21], [-4], [48], [-44], [32], [-87], [50], [-81], [45], [49], [-28], [14], [-22], [80], [-76], [82], [4], [10], [4], [83], [-45], [-38], [-88], [71], [-3], [60], [48], [61], [-27], [-30], [43], [65], [88], [-82], [-54], [34], [-32], [49], [42], [-84], [-74], [48], [-38], [-80], [18], [15], [8], [-2], [3], [-6], [-53], [0], [14], [-44], [37], [-30], [10], [44], [-80], [48], [38], [16], [-44], [-57], [38], [-47], [-85], [-94], [14], [0], [46], [-36], [36], [-8], [-31], [-80], [6], [74], [16], [-63], [-77], [-50], [-30], [-44], [21], [-23], [-58], [0], [-8], [37], [-5], [-49], [39], [-16], [10], [0], [-43], [20], [10], [93], [32], [54], [-44], [30], [-36], [-10], [-63], [66], [19], [66], [-30], [1], [68]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_6864_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_6864_2_a_q();".
// 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_6864_2_a_q(:prec:=1)
    chi := MakeCharacter_6864_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_6864_2_a_q();".
// 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_6864_2_a_q( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_6864_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![3, 1]>,<7,R![1, 1]>,<17,R![8, 1]>,<19,R![-6, 1]>],Snew);
    return Vf;
end function;