
// Make newform 6840.2.a.x in Magma, downloaded from the LMFDB on 20 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_6840_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_6840_2_a_x();".
// 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_6840_2_a_x();".
// 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
        poly := [-2, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_6840_a();"
function MakeCharacter_6840_a()
    N := 6840;
    order := 1;
    char_gens := [1711, 3421, 5321, 2737, 6481];
    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_6840_a_Hecke(Kf)
    return MakeCharacter_6840_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, 0], [0, 0], [1, 0], [-2, 1], [0, -1], [0, -3], [0, 2], [-1, 0], [2, -2], [2, 3], [-6, 2], [0, 5], [2, 3], [-6, -3], [-2, 2], [4, -6], [0, -2], [-8, 0], [0, 8], [8, 2], [2, 4], [-8, -4], [6, 0], [-2, -11], [0, -7], [10, -2], [-8, 0], [-8, 4], [-2, -10], [-4, 4], [0, -12], [-12, -5], [-2, -12], [-4, 6], [-6, 4], [-14, -2], [6, 10], [-10, -7], [-10, 4], [-8, 4], [-4, 14], [2, -2], [-12, 1], [16, -5], [0, 6], [0, 10], [-12, 0], [-16, 8], [-6, -8], [4, 16], [-2, 4], [8, -3], [2, 12], [-12, 9], [-12, 4], [14, -4], [-14, -7], [-12, -10], [6, 8], [-10, 3], [-14, -7], [-4, 0], [4, 2], [8, -1], [10, -14], [0, 6], [-10, 2], [8, 7], [-6, -4], [-22, 0], [-14, 0], [4, 17], [2, 5], [-4, 17], [-2, 6], [12, -8], [18, -4], [26, 0], [-10, -15], [-22, 10], [-24, 3], [14, -8], [20, -10], [16, 3], [-16, -8], [10, 10], [6, 23], [6, -10], [-2, -16], [-30, -3], [6, -10], [-20, -13], [16, -2], [-36, -3], [12, -2], [-2, -20], [-6, 5], [2, -13], [24, -10], [30, 8], [20, -6], [6, 12], [-22, 8], [14, 7], [-8, -6], [10, 6], [-10, -2], [10, -8], [0, -12], [-26, 10], [20, -10], [-14, -10], [0, -22], [-8, -2], [-8, -12], [30, -3], [6, 3], [-26, -4], [4, -6], [12, 4], [-6, -22], [-20, 7], [-20, 14], [32, 8], [0, 22], [-6, -4], [-24, -4], [-28, 13], [-10, -13], [26, 0], [20, 8], [-12, 24], [14, 10], [10, -26], [2, 12], [-14, 4], [-16, 6], [0, 6], [24, -12], [-14, -8], [-20, 20], [14, -6], [-2, -15], [-26, 0], [18, 14], [32, 6], [-22, 18], [4, 34], [16, -24], [36, 0], [36, -1], [-2, 22], [22, 9], [0, 12], [-28, -10], [20, 0], [-12, 4], [22, -2], [18, 22], [2, 17], [-14, 8], [-12, -24], [2, 7], [-12, -8], [40, 14], [18, 28], [-32, -12], [30, -14], [-26, 20], [6, -16], [16, -26], [-18, -8], [8, -19], [-30, -20], [40, -8], [6, 30], [-8, -18], [-2, -14], [-36, 10], [2, -8], [6, 31], [12, 4], [24, -17], [40, -4], [22, -6], [46, 4], [28, -3], [-24, -22], [-18, -22], [4, 23], [-4, -3], [6, 6], [-4, 0], [-10, -35], [14, 2], [-20, 8], [-32, -16], [-18, 32], [28, -14], [26, -18], [14, 17], [-8, 8], [-24, 3], [-30, 0], [20, -9], [24, -18], [-8, -40], [-26, -4], [-30, 6], [20, -20], [6, -18], [34, 6], [-14, 37], [-38, 16], [8, 14], [-50, 10], [-38, 17], [-22, -25], [30, 24], [4, 18], [2, 0], [38, -22], [22, -3], [18, -1], [50, 0], [2, 8], [24, -20], [20, 14], [-4, 24], [-4, 15], [54, -2], [18, 2], [-24, 36], [-18, -19], [-6, -37], [6, -2], [-20, 4], [2, 52], [-20, 7], [32, 18], [40, 12], [60, -8], [2, 5], [18, -22], [20, 18], [-8, -5], [34, -13], [40, 12], [-30, 26], [14, -40], [10, 34], [14, 36], [18, 24], [38, -26], [20, 6], [-16, -19], [-28, 24], [16, 8], [16, 34], [-26, -24], [48, -20], [60, -4], [26, 16], [4, 3], [-12, -38], [42, -14], [-34, -31], [-74, 2], [4, 36], [-28, -10], [30, -36], [-56, -8], [-22, 6], [48, -6], [-48, 19], [18, 11], [-2, 0], [30, 2], [-6, 6], [8, -47], [44, -20], [-68, -8], [-62, -10], [-26, 12], [-50, 11], [-8, -13], [50, 24], [-4, -22], [16, -32], [6, 7], [22, -22], [-26, -8], [16, -30], [16, 2], [44, -19], [-38, 6], [-16, -40], [-48, 22], [12, -24], [-14, -25], [-30, -12], [20, 18], [-4, 48], [14, 12], [-4, -10], [8, 7], [28, 4], [50, -4], [-48, 7], [50, -2], [18, 26], [-18, -20], [-2, -27], [36, 22], [14, -16], [10, -32], [12, 23], [16, 20], [-18, -2], [-22, -16], [-46, -30], [6, -18], [-58, 1], [8, -38], [-64, 10], [-70, 4], [36, 40], [-64, 10], [-38, 36], [16, -22], [18, 40], [76, 10], [36, -2], [10, -48], [-28, -10], [-2, 60], [34, 24], [0, -50], [-16, 48], [26, -31], [4, -25], [-30, -4], [38, -3], [-14, -42], [-52, 18], [40, 36], [8, 32], [12, -44], [40, 4], [2, 36], [6, 34], [44, 35], [-30, 34], [28, -30], [18, 34], [40, 30], [-48, -5], [-68, -13], [28, 6], [20, -32], [-6, 36], [-66, -8], [28, -20], [-28, -6], [-26, 9], [24, 13], [62, -16], [48, 34], [-50, -22], [16, -21], [36, -12], [-50, -20], [-74, -15], [-44, -12], [18, -14], [84, 8], [12, -25], [-38, 36], [-66, 10], [-24, -5], [-10, 61], [-68, -6], [6, 3], [-34, -36], [10, -26], [46, 12], [40, 38], [-18, -40], [-54, 15], [-26, -24], [-58, -14], [28, -38], [-16, 51], [-14, 51], [68, -6], [-36, 13], [42, -34], [-74, -9], [-26, 22], [34, -12], [-14, 24], [52, -6], [20, -42], [36, -8], [-66, -9], [36, -8], [-18, -14], [-2, 43], [52, -10], [16, 35], [-36, -11], [-8, -30], [-36, -12], [32, 38], [26, 8], [26, -40], [4, 36], [-44, 32], [34, 44], [-20, 16], [34, -5], [-60, 11], [-14, 22], [60, 4], [0, 11], [-16, -24], [56, -36], [-42, -32], [40, 2], [-4, -9]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_6840_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_6840_2_a_x();".
// 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_6840_2_a_x(:prec:=2)
    chi := MakeCharacter_6840_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_6840_2_a_x();".
// 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_6840_2_a_x( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_6840_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![2, 4, 1]>,<11,R![-2, 0, 1]>,<13,R![-18, 0, 1]>],Snew);
    return Vf;
end function;