
// Make newform 1002.2.a.b in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1002_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_1002_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_1002_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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_1002_a();"
function MakeCharacter_1002_a()
    N := 1002;
    order := 1;
    char_gens := [335, 673];
    v := [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_1002_a_Hecke(Kf)
    return MakeCharacter_1002_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], [3], [3], [6], [2], [0], [0], [4], [-6], [-9], [-1], [2], [-8], [-1], [-3], [3], [-12], [1], [8], [10], [-12], [17], [-15], [-13], [5], [14], [6], [-6], [4], [-5], [12], [1], [1], [-11], [12], [-18], [-7], [1], [-12], [-10], [-16], [-13], [-12], [6], [24], [-18], [17], [25], [20], [-1], [-16], [-16], [0], [-10], [27], [15], [14], [3], [7], [-8], [12], [21], [-31], [14], [-6], [4], [5], [-29], [9], [-21], [-24], [-16], [25], [29], [24], [18], [26], [24], [26], [-32], [38], [13], [37], [-32], [9], [-35], [22], [22], [-22], [-14], [-24], [-4], [32], [24], [-36], [-8], [-10], [-30], [1], [-25], [22], [36], [0], [-39], [-17], [27], [-6], [23], [27], [18], [-8], [22], [-16], [9], [-18], [24], [-4], [-12], [9], [-30], [20], [22], [0], [52], [-40], [-17], [-20], [-32], [-38], [-45], [33], [8], [-10], [-5], [-10], [39], [28], [-18], [-15], [31], [18], [-50], [32], [-35], [-37], [36], [-45], [-30], [-12], [6], [-52], [2], [30], [2], [27], [1], [31], [-18], [-38], [-30], [-50], [-32], [-7], [-32], [0], [-32], [12], [9], [38], [-47], [-26], [29], [-5], [-46], [22], [24], [25], [0], [5], [37], [54], [-51], [8], [42], [14], [-18], [0], [57], [-16], [-16], [21], [30], [52], [50], [-9], [9], [34], [45], [13], [30], [33], [15], [28], [48], [-26], [-20], [-20], [36], [32], [-7], [-68], [-46], [28], [20], [29], [-58], [30], [-9], [35], [-38], [-35], [-4], [12], [68], [-26], [-57], [0], [2], [74], [-61], [20], [-58], [50], [18], [-72], [0], [20], [51], [-16], [-60], [56], [-24], [17], [50], [-64], [-19], [-39], [-34], [6], [58], [2], [58], [-2], [50], [-60], [-53], [74], [3], [61], [-56], [-21], [53], [-73], [30], [-32], [50], [-12], [-7], [54], [-50], [-2], [-66], [11], [50], [64], [-75], [-69], [12], [8], [-52], [-40], [62], [-34], [37], [-12], [-49], [-54], [51], [0], [42], [47], [-51], [-36], [18], [8], [49], [27], [73], [64], [14], [9], [-61], [-20], [40], [18], [-77], [56], [62], [56], [14], [-48], [66], [76], [24], [-10], [9], [-5], [-6], [-60], [8], [-59], [30], [-56], [-32], [-43], [-70], [80], [-79], [-4], [22], [-48], [4], [-58], [-44], [-92], [-20], [-64], [70], [-7], [-44], [-82], [-51], [92], [-54], [66], [34], [-64], [70], [18], [-56], [-8], [-81], [82], [-11], [-6], [15], [27], [90], [39], [-22], [92], [-66], [86], [-53], [82], [39], [-68], [-29], [37], [20], [-56], [68], [98], [29], [-36], [-30], [-49], [-34], [58], [34], [-35], [-24], [15], [90], [-90], [-36], [85], [32], [16], [-6], [-54], [-36], [13], [102], [60], [61], [-27], [-25], [-11], [-88], [6], [-26], [-18], [7], [-8], [75], [28], [-43], [20], [0], [22], [78], [52], [-6], [-96], [-18], [-37], [-77], [81], [-6], [61], [27], [50], [-35], [22], [-108], [29], [56], [12]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1002_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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