
// Make newform 4002.2.a.o in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4002_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_4002_2_a_o();".
// 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_4002_2_a_o();".
// 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_4002_a();"
function MakeCharacter_4002_a()
    N := 4002;
    order := 1;
    char_gens := [2669, 3133, 553];
    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_4002_a_Hecke(Kf)
    return MakeCharacter_4002_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], [-3], [0], [-2], [5], [-5], [1], [1], [-6], [-7], [-9], [1], [3], [10], [-11], [-6], [-4], [-6], [-6], [4], [16], [-6], [0], [-10], [11], [-9], [-4], [-5], [-2], [-14], [-18], [14], [-15], [15], [21], [-21], [22], [15], [-4], [2], [15], [-20], [-15], [-20], [-15], [8], [15], [27], [-4], [6], [19], [4], [-6], [1], [0], [20], [8], [-6], [16], [-30], [-12], [19], [1], [2], [-5], [22], [23], [-2], [34], [-19], [-16], [36], [20], [-8], [-20], [-20], [-2], [-38], [33], [10], [6], [-4], [-3], [-24], [31], [37], [-26], [24], [34], [24], [-15], [16], [-24], [-41], [39], [-12], [-14], [35], [-38], [-41], [-42], [15], [-28], [-2], [-13], [8], [24], [12], [2], [-46], [3], [-39], [19], [-21], [-26], [4], [30], [12], [-28], [-21], [-14], [9], [-10], [7], [4], [2], [38], [22], [12], [39], [-2], [-29], [-10], [20], [-36], [-2], [10], [-22], [-28], [-42], [-52], [-48], [-29], [1], [13], [-52], [35], [-22], [-2], [-46], [54], [36], [28], [-8], [25], [18], [-3], [46], [-54], [-24], [10], [40], [-2], [16], [-35], [-3], [55], [-42], [-4], [-24], [18], [54], [45], [26], [24], [32], [-64], [-10], [-20], [-24], [-4], [0], [-34], [30], [-61], [55], [30], [-30], [43], [12], [-16], [-22], [18], [-6], [30], [-4], [-38], [-16], [-30], [37], [25], [0], [66], [-3], [4], [-5], [-48], [52], [-62], [-35], [-36], [22], [25], [-38], [-47], [-38], [-52], [18], [-10], [-24], [-61], [-6], [52], [55], [-39], [-12], [56], [-12], [-46], [20], [-2], [23], [-68], [-49], [-42], [-50], [33], [-31], [38], [30], [-24], [35], [6], [26], [32], [-56], [0], [-72], [-2], [-48], [49], [26], [-50], [-67], [31], [-47], [54], [-26], [12], [30], [14], [38], [-35], [-15], [36], [33], [-41], [-27], [56], [38], [65], [18], [14], [-32], [12], [-80], [-20], [-61], [-50], [-13], [-18], [-60], [-17], [53], [-38], [0], [31], [2], [-78], [12], [18], [-70], [-50], [25], [-38], [-54], [37], [19], [-70], [51], [20], [36], [76], [-55], [18], [-50], [-44], [64], [62], [-82], [-18], [-84], [72], [7], [66], [22], [1], [-21], [-23], [41], [9], [20], [84], [68], [-18], [-28], [20], [-81], [-73], [-9], [-5], [-12], [-52], [27], [-62], [66], [-70], [-13], [-61], [54], [42], [55], [11], [-16], [-25], [17], [24], [-28], [56], [22], [-60], [64], [-45], [47], [-48], [-87], [-90], [-24], [-34], [-86], [-23], [24], [11], [-76], [94], [-65], [-36], [-40], [84], [46], [4], [-45], [-2], [-8], [-83], [-70], [70], [-39], [-56], [-64], [12], [35], [22], [16], [40], [-66], [-21], [32], [68], [5], [-6], [101], [11], [19], [-29], [-33], [-44], [-44], [0], [-34], [20], [16], [45], [-64], [44], [-35], [52], [1], [-46], [73], [33], [-88], [-22], [-72], [1], [57], [22], [-69], [-93], [34], [73], [-54], [10], [-70], [69], [101], [-48], [1]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4002_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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