
// Make newform 8007.2.a.b in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_8007_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_8007_2_a_b();".
// 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_8007_2_a_b();".
// 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_8007_a();"
function MakeCharacter_8007_a()
    N := 8007;
    order := 1;
    char_gens := [5339, 1414, 7855];
    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_8007_a_Hecke(Kf)
    return MakeCharacter_8007_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], [1, 0], [-2, -1], [-2, -1], [0, 2], [0, -4], [-1, 0], [-2, 2], [4, 1], [2, 5], [4, 2], [2, -2], [2, -1], [0, -8], [6, 4], [-4, 0], [-6, 0], [4, -1], [4, -8], [-8, 0], [-8, 1], [6, 7], [-2, -2], [6, 8], [0, -3], [0, 10], [4, 0], [4, 3], [-6, -10], [-6, -10], [-6, -10], [-4, 9], [0, -4], [6, 5], [4, -2], [-8, 4], [1, 0], [-2, 5], [-4, 4], [-6, -8], [-6, 4], [-4, -3], [-6, -12], [2, 4], [6, 14], [-16, -2], [6, 11], [0, 10], [-12, -3], [6, 4], [-6, 14], [18, 8], [4, -11], [-2, -12], [-16, -2], [-8, 0], [2, 11], [4, -10], [-18, 2], [-4, -18], [-12, -4], [0, 18], [0, -10], [8, 14], [2, 0], [2, 16], [10, 2], [4, -7], [16, 2], [22, 4], [16, -6], [-6, 10], [-6, -5], [-10, 14], [-26, 7], [-2, -18], [-10, 0], [4, 13], [-18, -9], [-6, 4], [0, -18], [-22, 6], [-12, -14], [2, 8], [6, 7], [-2, 2], [18, -1], [14, 0], [-16, -10], [-16, -8], [-4, -20], [4, -7], [20, -6], [6, 6], [-6, 13], [-12, -1], [-12, 14], [10, -1], [4, -8], [12, 13], [4, 4], [2, -20], [-26, 2], [-4, 4], [0, -12], [-30, 0], [-10, -2], [14, 4], [-42, 4], [-2, -8], [2, -19], [-26, 4], [-14, -12], [12, 20], [-34, -6], [-6, 4], [6, 1], [-28, -12], [6, 10], [-34, -8], [6, 16], [-12, -1], [2, 10], [36, 1], [-34, 1], [4, 20], [18, 4], [-12, -9], [14, 14], [6, 12], [12, 12], [12, 6], [-2, -31], [-10, -16], [0, -32], [12, 8], [30, 12], [6, 7], [0, -10], [-46, 3], [-30, -9], [-6, -14], [-18, -15], [-8, 0], [-32, 16], [-24, -5], [18, -16], [-26, -5], [32, 8], [6, 10], [-40, -13], [14, 3], [12, 10], [-20, -13], [12, 14], [-40, -10], [16, -4], [14, 16], [-42, -2], [30, 12], [-20, 7], [-4, 10], [-16, -12], [-30, 8], [2, 16], [0, 7], [20, 26], [-8, -35], [-22, -14], [-38, 10], [34, 6], [-18, 6], [4, -10], [-2, 2], [-4, -6], [-22, 3], [-24, 6], [-10, -25], [-22, -18], [-10, 24], [-6, -6], [12, 21], [-12, 9], [28, 0], [-18, -8], [32, 22], [-36, 13], [-4, -12], [-10, -6], [-4, -12], [-36, -17], [8, -20], [-16, 14], [2, 12], [-16, 15], [-34, 5], [-4, 1], [-12, 21], [6, 40], [30, 4], [-10, 6], [-16, -18], [44, 8], [-26, 28], [2, 0], [-28, -14], [12, 14], [2, -16], [2, 2], [2, -6], [24, 1], [16, -14], [-16, -26], [22, -28], [-8, 39], [-28, 7], [18, 30], [24, -18], [-8, 14], [30, 8], [42, -16], [28, -4], [6, -28], [60, -2], [24, 4], [-42, -8], [-6, -21], [-12, 29], [-40, 2], [4, -33], [16, 32], [16, 18], [18, 18], [8, -18], [24, -2], [-14, 28], [-58, 10], [-50, 13], [-48, -4], [6, -10], [-16, 20], [38, 6], [-8, 0], [34, -28], [-22, 8], [-32, -16], [36, 18], [28, -32], [-4, 12], [24, -36], [40, 20], [26, -18], [26, 28], [2, 38], [-44, 10], [-42, -8], [-42, 6], [16, -36], [-6, -40], [2, -12], [24, 24], [34, -10], [58, -10], [-20, 27], [30, -12], [-28, -28], [-16, -4], [20, -34], [-24, 6], [-8, -46], [2, 16], [-64, 2], [6, -10], [16, 10], [-14, 38], [0, 48], [34, -8], [10, 30], [14, 18], [2, -34], [-14, 8], [2, -45], [52, 16], [22, 30], [20, 20], [26, -12], [22, -34], [2, -35], [-12, -14], [32, 22], [-18, -2], [-40, 5], [-4, -14], [-8, -16], [42, -30], [14, 27], [-32, 4], [18, 8], [-24, -13], [-12, -10], [30, 12], [44, 2], [0, -10], [-12, -49], [-32, -6], [-64, 3], [12, -12], [-50, -12], [6, -2], [64, 0], [4, -21], [14, -17], [26, 22], [14, 22], [36, -16], [10, -32], [12, 12], [-56, -1], [-32, 33], [36, 6], [4, 2], [-28, 5], [-12, -34], [-28, -12], [-14, -25], [26, 24], [-60, 10], [34, 5], [-4, -38], [22, -27], [-12, -25], [-6, -38], [-14, -21], [-14, -18], [6, -25], [-4, -37], [22, 16], [26, 28], [-40, -7], [10, 18], [46, -30], [20, 18], [14, -18], [-18, -54], [-28, -40], [-20, -28], [-6, 22], [8, -8], [-16, 32], [-6, 43], [30, -26], [-72, -7], [12, -14], [54, -17], [-34, -4], [24, -12], [-2, -24], [44, -18], [-30, -21], [-30, -32], [14, -18], [2, 12], [34, 34], [56, 13], [40, -2], [-22, 24], [34, 20], [-18, 34], [-6, 14], [26, -12], [8, 54], [-36, 30], [-30, -4], [-16, 24], [-24, 27], [-52, 22], [16, -35], [-14, -32], [-64, 12], [2, 40], [18, 34], [14, -56], [30, -31], [22, 12], [20, 22], [92, -5], [56, 0], [22, 0], [80, 2], [42, 32], [-56, 21], [10, -11], [-32, 32], [0, 20], [2, 32], [42, 12], [-12, -20], [32, -32], [62, 15], [-8, 32], [-12, 41], [-24, 12], [28, 10], [-22, 22], [60, -30], [-4, -12], [-66, 4], [28, -38], [12, -58], [-52, 17], [-12, 11], [86, 4], [-2, 20], [16, -26], [-32, -40], [-38, -10], [-8, -21], [-26, -37], [-42, 8], [70, -2], [-36, 32], [32, 15], [-52, -30], [4, -56], [54, -24], [-40, -14], [24, -39], [-14, -30], [-28, 8], [48, -22]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8007_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_8007_2_a_b();".
// 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_8007_2_a_b(:prec:=2)
    chi := MakeCharacter_8007_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_8007_2_a_b();".
// 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_8007_2_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8007_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![-1, 2, 1]>],Snew);
    return Vf;
end function;