
// Make newform 1001.2.a.a in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1001_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_1001_2_a_a();".
// 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_1001_2_a_a();".
// 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_1001_a();"
function MakeCharacter_1001_a()
    N := 1001;
    order := 1;
    char_gens := [430, 365, 925];
    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_1001_a_Hecke(Kf)
    return MakeCharacter_1001_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 := [[-2], [-3], [-3], [-1], [1], [-1], [-8], [-4], [-9], [-8], [3], [-7], [0], [2], [-8], [6], [-7], [-4], [-9], [-5], [-10], [12], [-6], [-9], [1], [-8], [8], [8], [-14], [9], [14], [-10], [-17], [6], [-6], [12], [7], [-4], [14], [-14], [5], [-19], [25], [10], [-22], [28], [-26], [-9], [-22], [-1], [-6], [-4], [10], [-5], [-2], [-6], [6], [8], [-32], [0], [-2], [32], [12], [24], [-23], [3], [1], [-4], [4], [-16], [11], [-10], [-7], [12], [-27], [27], [21], [-10], [34], [-22], [-4], [-30], [-30], [-9], [-28], [-11], [-3], [-36], [8], [7], [11], [4], [1], [-30], [-4], [-4], [35], [-29], [34], [14], [8], [0], [10], [-14], [-8], [-19], [12], [18], [-40], [-28], [-34], [-22], [-18], [-13], [-47], [-45], [29], [39], [39], [-28], [9], [10], [-2], [24], [17], [-24], [5], [-3], [37], [18], [-14], [-6], [25], [6], [-6], [-44], [-6], [-50], [-21], [2], [-28], [48], [31], [22], [-37], [-29], [-42], [-12], [7], [-56], [0], [-21], [-4], [0], [-32], [24], [38], [14], [-8], [4], [3], [24], [10], [17], [-41], [-1], [-8], [-40], [-14], [41], [-24], [6], [-44], [32], [-49], [-21], [-30], [9], [34], [-50], [-52], [30], [9], [-20], [35], [6], [-44], [52], [-24], [-42], [27], [-48], [17], [6], [6], [-3], [60], [11], [-28], [4], [-34], [-32], [-22], [-4], [55], [15], [-7], [-24], [-18], [60], [68], [53], [-17], [12], [20], [-5], [52], [42], [-48], [5], [26], [-4], [39], [-49], [54], [40], [-18], [-24], [-72], [-36], [41], [-50], [-10], [-18], [7], [-4], [45], [22], [-45], [39], [-3], [-8], [-16], [-5], [56], [-36], [8], [0], [-56], [-2], [76], [-36], [63], [-6], [18], [42], [-50], [-52], [39], [0], [26], [-22], [22], [10], [-74], [-44], [5], [-63], [-42], [-58], [-9], [-67], [-10], [-58], [26], [-69], [-27], [50], [-34], [-24], [-66], [23], [-32], [-40], [12], [-3], [74], [-72], [-35], [28], [33], [12], [-74], [32], [-52], [-66], [11], [-51], [44], [44], [10], [-60], [60], [-44], [-13], [-83], [39], [31], [60], [64], [2], [18], [50], [29], [-34], [30], [51], [8], [66], [56], [46], [-21], [-66], [-25], [-38], [21], [1], [-41], [-24], [-32], [-76], [38], [26], [-54], [-26], [-59], [-25], [-16], [-53], [-92], [-61], [43], [30], [31], [-27], [0], [69], [-63], [-30], [57], [-2], [-3], [82], [-46], [22], [90], [65], [-52], [34], [43], [84], [42], [43], [-22], [9], [-11], [-44], [90], [-30], [-27], [-52], [-2], [0], [10], [73], [-24], [-48], [28], [-42], [-48], [-42], [73], [-24], [82], [28], [-65], [64], [31], [70], [-11], [-41], [87], [-5], [-23], [-71], [16], [0], [-78], [40], [-52], [86], [69], [38], [-10], [6], [84], [46], [8], [-44], [-77], [-88], [18], [-100], [10], [-78], [-65], [0], [69], [-2], [74], [99], [20], [96], [8], [26], [-93], [-65], [36], [91], [-6]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1001_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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