
// Make newform 8001.2.a.i in Magma, downloaded from the LMFDB on 17 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_8001_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_8001_2_a_i();".
// 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_8001_2_a_i();".
// 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 := [-4, -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_8001_a();"
function MakeCharacter_8001_a()
    N := 8001;
    order := 1;
    char_gens := [3557, 1144, 7750];
    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_8001_a_Hecke(Kf)
    return MakeCharacter_8001_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, 1], [-1, 0], [2, -2], [-1, -1], [-4, -1], [4, 0], [-3, 3], [1, -2], [-3, 3], [-7, 2], [-2, 5], [6, -4], [10, -2], [11, 0], [7, -1], [-7, -1], [-2, 0], [8, 2], [-7, 1], [-8, 3], [-5, -5], [5, -5], [4, -5], [-7, -5], [-12, 0], [-4, 0], [2, 0], [-8, 2], [1, 0], [-6, -3], [17, 1], [10, 1], [-14, 0], [-2, -8], [-10, 4], [3, -10], [0, -4], [-14, 6], [8, -2], [-16, 5], [-4, -6], [2, 2], [12, 6], [-7, -1], [-21, -3], [2, -5], [-2, 6], [-12, 1], [-9, -6], [-19, 6], [-8, 1], [10, 5], [-13, 9], [-12, 10], [-8, 5], [-5, 7], [-12, 4], [-6, -1], [-8, -3], [10, 0], [-22, -5], [3, -9], [-6, 7], [18, -4], [4, -4], [20, -4], [5, 8], [-2, 9], [8, -2], [-4, -7], [21, -11], [-2, 2], [-22, -2], [16, 7], [-4, 8], [15, 9], [-1, 6], [-18, 0], [-6, 2], [-28, -4], [-2, 8], [-10, -8], [-14, 5], [-4, 0], [2, -10], [3, 0], [-18, 0], [1, 3], [9, -1], [-38, 1], [16, 4], [20, 8], [-12, -8], [-4, 3], [8, 3], [0, 2], [13, -11], [8, -4], [16, -8], [14, -2], [-20, 12], [-28, 6], [-18, 2], [3, -5], [-2, 13], [-17, -3], [-21, -10], [6, 7], [9, -5], [16, 6], [13, -18], [30, -3], [10, -8], [2, 5], [0, 12], [7, -9], [-14, 14], [-33, 1], [17, 1], [-21, 18], [-4, -7], [0, -12], [-16, 13], [-3, -7], [-26, 7], [-16, 11], [14, -11], [-1, -3], [9, -2], [19, -18], [-32, -2], [13, -18], [-1, -17], [18, -9], [-10, 13], [-13, 21], [22, -4], [-12, 2], [-1, 1], [47, 0], [-12, 15], [31, 0], [-22, 20], [-13, -15], [-10, 1], [-33, -9], [-20, 23], [-33, -2], [6, 17], [-29, -7], [5, 7], [-29, 1], [-17, -8], [16, -6], [-31, 14], [-22, 10], [4, -2], [14, 9], [0, 15], [6, -12], [6, -22], [-14, -6], [12, 16], [-2, 5], [16, 0], [26, 0], [15, -4], [10, 12], [-5, -11], [2, -23], [0, 6], [3, 19], [26, -13], [9, 13], [1, 2], [38, 0], [32, -14], [-2, 15], [27, 4], [5, -13], [-40, -6], [16, 18], [-36, 14], [-19, 13], [-8, 6], [17, -9], [-22, -7], [2, -9], [-24, 23], [-16, 1], [-52, 6], [-8, -3], [6, -12], [46, -1], [-6, 4], [23, 14], [-10, 11], [-31, -7], [21, 5], [-25, -13], [17, 3], [-4, -11], [-9, -15], [-46, 8], [-3, -3], [30, -14], [8, 0], [20, 2], [8, 22], [12, -3], [-4, -12], [-44, 15], [-20, 4], [8, 2], [36, 4], [31, -23], [20, 1], [48, 4], [4, 6], [-5, 19], [-1, 0], [-20, 9], [24, -17], [23, -6], [24, -6], [6, 16], [-56, 6], [15, 22], [-10, 16], [13, -15], [-16, 24], [22, -6], [-16, -13], [53, 1], [52, -9], [22, -31], [-20, 1], [-15, -15], [-24, 11], [-16, -20], [15, 5], [-37, 0], [-3, 7], [22, -15], [16, 3], [-4, 23], [14, -22], [-24, 7], [37, 0], [-6, 26], [-20, -17], [8, 3], [-49, 1], [28, -8], [6, 20], [47, -13], [-12, -13], [-10, 16], [46, -14], [48, -6], [-54, 12], [-27, 1], [1, -29], [-26, 6], [63, -8], [68, -9], [-8, -9], [-14, 30], [10, -25], [-20, 0], [-34, 8], [56, -10], [12, -17], [-38, -15], [18, -26], [-62, 13], [16, 10], [-25, -5], [-19, 33], [-48, 0], [-40, 23], [-52, 6], [-6, 2], [-72, 4], [18, -16], [8, -22], [62, 5], [-30, -12], [-14, -3], [-46, 2], [-22, 40], [28, 16], [14, -34], [-36, -6], [-20, 19], [42, 1], [-38, -1], [35, 15], [-13, -9], [59, -1], [11, 26], [-10, -12], [28, 4], [-44, -2], [46, -4], [-10, 10], [-49, 17], [-54, 12], [-34, -10], [54, -12], [-12, 14], [-46, 2], [45, -1], [14, -20], [49, 9], [4, -28], [-53, 17], [-2, 24], [29, 5], [-4, -20], [13, 5], [-2, -8], [22, -12], [-49, 11], [50, -24], [-8, -4], [27, -4], [39, -13], [43, -9], [-1, -14], [4, -3], [-42, 6], [8, 4], [-8, -4], [8, -10], [-2, -24], [9, -15], [1, 31], [22, -20], [-12, -10], [7, 13], [-13, -20], [0, -18], [-21, 26], [2, 4], [16, -17], [7, -21], [-18, -1], [-6, 18], [18, -28], [-53, 16], [-48, 13], [71, 0], [-48, 6], [12, -1], [-29, -13], [-40, 6], [28, -9], [32, 8], [61, -9], [28, -8], [43, -2], [-32, 15], [19, -38], [-26, 28], [-16, 4], [54, 16], [-32, 7], [4, -13], [1, -16], [-58, -10], [47, -7], [17, -19], [-63, 19], [5, -12], [-24, 27], [-42, -11], [-68, -10], [10, 31], [34, 9], [44, -27], [-34, 14], [-8, 39], [15, -31], [19, 25], [-16, -15], [-3, -25], [47, -19], [11, -10], [-2, -12], [-12, -26], [-24, 28], [-16, -12], [14, -44], [45, -7], [2, -9], [-25, -7], [74, 0], [31, -41], [30, -33], [-5, 25], [69, -17], [16, 18], [15, -25], [-60, 24], [74, 3], [-19, 5], [54, 6], [-22, -5], [34, 9], [0, -16], [82, -2], [58, 0], [65, -3], [7, 4], [-28, 27], [-42, 24], [13, 9], [-8, 42], [6, 36], [19, 21], [-28, -7], [2, -26], [-16, 21], [10, 6], [-7, -10], [40, -30], [-27, -3]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8001_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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