
// Make newform 2001.2.a.b in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_2001_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_2001_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_2001_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_2001_a();"
function MakeCharacter_2001_a()
    N := 2001;
    order := 1;
    char_gens := [668, 1132, 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_2001_a_Hecke(Kf)
    return MakeCharacter_2001_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], [-1], [4], [-4], [4], [-5], [-5], [5], [1], [-1], [-2], [5], [-2], [1], [6], [2], [9], [-10], [8], [-3], [8], [13], [-6], [-9], [-6], [12], [16], [18], [14], [10], [-4], [8], [-9], [0], [20], [19], [-23], [4], [-21], [5], [7], [6], [21], [14], [9], [-2], [18], [17], [-18], [19], [-9], [-29], [-26], [9], [-27], [12], [-16], [-12], [3], [-16], [-6], [1], [-2], [-24], [14], [6], [-10], [-27], [-27], [27], [26], [-3], [4], [6], [11], [10], [25], [-22], [-34], [-36], [16], [13], [-2], [-30], [-27], [-18], [-24], [-16], [-12], [-39], [3], [24], [24], [-30], [15], [37], [43], [-16], [-24], [20], [-17], [14], [1], [22], [18], [-38], [-27], [-21], [-2], [40], [32], [-26], [18], [41], [-30], [-39], [4], [-3], [6], [29], [-6], [-19], [10], [20], [52], [18], [34], [-21], [-8], [42], [-50], [13], [41], [7], [-25], [-25], [18], [-22], [42], [18], [20], [15], [-20], [-3], [28], [44], [-18], [26], [-26], [-4], [-33], [43], [51], [-20], [-1], [9], [10], [-7], [-26], [36], [-8], [-22], [-30], [-24], [-8], [21], [-16], [60], [-22], [-56], [10], [-5], [-34], [-5], [-48], [-4], [-11], [-40], [-40], [-56], [60], [-3], [22], [15], [48], [40], [60], [8], [24], [-48], [41], [12], [4], [52], [21], [-24], [-58], [-32], [-36], [41], [-61], [31], [-2], [-31], [-17], [13], [-11], [-31], [35], [50], [14], [-23], [-13], [63], [-34], [66], [-57], [-24], [48], [-46], [56], [-4], [4], [17], [-23], [44], [27], [14], [-21], [-36], [-6], [-20], [-17], [30], [-16], [-32], [51], [-3], [65], [4], [29], [-47], [-37], [40], [-30], [-21], [-40], [-54], [52], [-63], [-58], [-36], [-15], [-2], [20], [60], [2], [36], [-1], [79], [-24], [17], [-43], [-70], [54], [47], [27], [-15], [4], [2], [-45], [14], [12], [1], [-42], [8], [60], [-38], [70], [-60], [16], [-62], [12], [-34], [-6], [46], [19], [-33], [48], [27], [-68], [26], [39], [-43], [-80], [-18], [73], [69], [-12], [2], [5], [36], [64], [-46], [-52], [1], [32], [-14], [-60], [19], [-72], [10], [-64], [9], [-68], [-56], [-64], [52], [70], [-22], [-84], [4], [63], [2], [-76], [-2], [-88], [1], [-48], [48], [-22], [-13], [17], [64], [-41], [-28], [-8], [54], [34], [88], [-38], [-42], [-60], [-24], [-6], [88], [2], [-6], [40], [81], [93], [-83], [-90], [6], [-34], [56], [-77], [38], [18], [47], [-10], [-10], [-58], [24], [-56], [76], [72], [44], [-45], [4], [25], [73], [-1], [-94], [5], [76], [-56], [-11], [-60], [17], [17], [76], [34], [-2], [-38], [-63], [46], [-58], [9], [-15], [-5], [6], [-86], [74], [-83], [4], [-102], [75], [0], [-44], [14], [-4], [57], [64], [34], [95], [-2], [0], [71], [-49], [53], [42], [64], [-24], [-13], [-2], [74], [-19], [79], [84], [0], [-65], [54], [46], [-51], [94], [-54], [18], [-60], [-32]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2001_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_2001_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_2001_2_a_b(:prec:=1)
    chi := MakeCharacter_2001_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_2001_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_2001_2_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2001_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![0, 1]>,<5,R![-4, 1]>],Snew);
    return Vf;
end function;