
// Make newform 8008.2.a.g in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_8008_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_8008_2_a_g();".
// 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_8008_2_a_g();".
// 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 := [-1, -1, 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_8008_a();"
function MakeCharacter_8008_a()
    N := 8008;
    order := 1;
    char_gens := [6007, 4005, 3433, 4369, 4929];
    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_8008_a_Hecke(Kf)
    return MakeCharacter_8008_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, 1], [1, -1], [1, 0], [1, 0], [1, 0], [-6, 3], [1, 1], [2, -6], [2, 4], [-4, -2], [8, -6], [-4, 2], [-2, 1], [-6, -2], [2, -1], [0, -2], [-8, -3], [10, -5], [-7, 9], [2, -4], [-7, 13], [1, 1], [-18, 1], [-2, 0], [5, -1], [-4, 0], [-10, 1], [9, -7], [-5, -3], [-5, 3], [12, 4], [2, -14], [-16, 12], [-5, 15], [-12, 2], [6, 2], [-1, -3], [-22, 5], [-7, 11], [2, 2], [8, -4], [-2, -14], [-8, -5], [10, 3], [-3, 11], [-6, 7], [-4, 2], [20, -9], [-5, 9], [16, -6], [-2, -14], [0, -10], [-3, -3], [8, -18], [-27, -3], [-6, -4], [-6, 13], [2, -6], [-12, 27], [-4, -16], [10, -18], [-8, 7], [-3, 3], [20, 2], [-20, 2], [-3, 7], [-2, 10], [-1, 13], [26, -6], [-30, 8], [-24, 2], [18, -25], [-8, -12], [22, -7], [-6, 16], [-13, 3], [-20, 23], [-10, 6], [-14, 14], [0, -9], [30, -4], [-2, 0], [-4, 8], [-8, 22], [8, -28], [-2, 20], [-11, 19], [4, 2], [-12, 8], [-20, -5], [11, -31], [8, -8], [12, 4], [-19, 27], [-18, -8], [-10, 20], [0, -18], [14, -2], [26, 0], [-20, -4], [3, 15], [24, 10], [-12, -18], [-15, -9], [-22, -9], [-8, 22], [-20, -10], [-4, 30], [-2, 1], [16, -4], [6, 24], [12, -20], [-28, 2], [28, -13], [16, -11], [-20, 12], [4, 16], [18, -37], [10, -17], [23, -31], [18, -10], [12, -13], [-38, -7], [-2, -24], [10, -4], [-14, 36], [-6, 7], [-29, 13], [-12, 18], [-4, 2], [-18, 0], [-10, 20], [15, 15], [-8, 20], [34, -26], [0, 23], [-20, 21], [-8, 12], [-8, 18], [19, -17], [-6, 17], [-4, -20], [22, 2], [-2, 34], [4, 2], [0, 12], [-13, -13], [12, 0], [-24, 8], [-2, 5], [32, 2], [42, -8], [8, -4], [18, -30], [4, 30], [8, 7], [6, -15], [-35, -3], [26, 10], [2, -17], [44, 6], [-6, 12], [-21, -5], [-32, 36], [-28, 36], [48, -20], [-9, 9], [-19, 7], [-24, 46], [0, -34], [22, -24], [-17, 7], [52, -13], [-6, -21], [-16, -8], [-2, 20], [24, -6], [-18, -8], [-23, 3], [-27, -11], [-22, -16], [-25, -13], [18, -40], [-20, 47], [-4, -26], [-38, 28], [28, 14], [37, 11], [0, 16], [-28, 30], [22, -24], [-32, 30], [36, -31], [12, -4], [24, -26], [20, 15], [38, -43], [12, -36], [24, 16], [34, -39], [30, -48], [-55, 23], [23, -13], [-30, 40], [18, 0], [24, -8], [44, -20], [-11, -1], [15, -45], [28, -18], [4, -8], [44, -14], [16, -36], [33, -9], [27, -13], [28, -31], [-22, -12], [24, -39], [24, 16], [36, -26], [-44, 12], [16, -32], [31, 11], [-38, 26], [2, 2], [36, -22], [2, -46], [-24, 36], [-38, 10], [2, -1], [-52, 14], [0, 16], [24, -8], [7, -37], [46, -11], [6, -20], [39, 19], [4, -36], [6, -33], [-8, -24], [-22, 0], [-2, 7], [52, -8], [-16, 40], [-68, 8], [-24, 32], [-20, 4], [22, -6], [-16, 8], [-15, 29], [4, 22], [28, 21], [-24, 48], [-9, 21], [-28, 46], [30, -15], [-39, 17], [32, -12], [-40, 32], [32, 11], [-30, 9], [12, -28], [7, 13], [38, -45], [28, -53], [16, -24], [34, -1], [36, -30], [-22, 8], [8, 24], [17, 19], [35, -49], [-48, -11], [46, 19], [-46, 22], [33, 3], [54, 9], [-14, -4], [-12, 44], [-28, 42], [35, -13], [-8, 30], [14, 16], [28, -7], [-26, -12], [33, -13], [-6, 29], [40, -18], [-50, 33], [22, 16], [60, -12], [53, 11], [-23, 13], [52, -36], [-26, -28], [6, 41], [-58, 12], [-32, 26], [4, -32], [50, -40], [22, -37], [12, -4], [-72, 16], [6, -24], [40, -2], [-4, -8], [42, -6], [47, -45], [54, -42], [-62, 14], [-34, 56], [-4, 4], [64, -6], [-58, 0], [39, 15], [34, -20], [-36, 26], [25, -39], [8, -18], [-34, 60], [28, -10], [-36, -3], [28, -35], [-36, 3], [56, 18], [-66, 19], [-42, 28], [15, -9], [84, -10], [-50, 33], [-39, 57], [-12, 4], [21, -67], [-46, 60], [-26, 4], [22, 34], [38, 6], [39, 15], [56, -4], [64, -38], [-26, 36], [-62, -12], [-22, -10], [26, -14], [-39, -5], [68, 12], [32, 0], [16, 36], [4, -32], [60, -31], [-58, 0], [20, -62], [-8, -18], [38, 28], [-50, 0], [-36, -34], [20, 24], [12, 12], [41, -27], [46, 3], [-53, 43], [36, 4], [32, -44], [-12, 11], [65, 7], [-18, -12], [-30, 58], [18, -4], [62, -10], [41, 9], [6, 6], [66, 8], [66, -12], [-18, 17], [58, -16], [-19, 57], [-16, -24], [-66, -9], [-33, 27], [-75, 15], [-32, 58], [0, 2], [-59, 55], [8, -48], [-32, -2], [-20, -40], [-76, 28], [-74, 4], [-17, 5], [4, 38], [30, -52], [-58, 19], [-3, 9], [-28, -43], [44, -48], [35, -59], [10, -40], [0, -16], [37, -19], [-6, -38], [-12, 5], [-12, 14], [-22, -19], [6, 6], [48, -6], [2, 12], [-13, 25], [52, -36], [-6, 14], [-9, -1], [-14, 0], [-56, -2], [-41, 17], [34, 24], [-28, -26], [-70, -16], [47, 17], [23, 27], [-90, 10], [-77, 31], [0, 4], [-21, -13], [-44, -22], [-26, 5], [-69, 41], [24, -12], [-83, 27], [18, -10], [22, 4]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8008_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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