
// Make newform 2001.2.a.f in Magma, downloaded from the LMFDB on 17 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_f();".
// 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_f();".
// 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_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 := [[1, 0], [-1, 0], [-1, -1], [0, 0], [-1, -1], [-1, -1], [0, 0], [0, -2], [1, 0], [-1, 0], [-5, 1], [5, -1], [5, 1], [6, -4], [2, 2], [6, -4], [7, 1], [1, -5], [9, -1], [13, -1], [0, -2], [-2, 0], [-2, -2], [2, 6], [2, -6], [-5, -1], [-1, 1], [0, 0], [2, 0], [-2, 6], [-1, 5], [-4, 0], [12, -4], [6, -10], [1, -7], [-4, 4], [14, 2], [3, 1], [11, -7], [-10, 8], [4, -8], [-2, 8], [-17, -1], [-12, 2], [2, 4], [11, -3], [-7, 11], [-8, -8], [-14, -6], [13, -5], [4, 2], [13, -1], [8, -10], [3, 3], [6, -4], [-8, -6], [13, -7], [-11, -1], [-5, 7], [6, -4], [-7, 3], [-16, 0], [-15, 3], [22, -2], [-14, 4], [-9, 7], [20, 0], [-11, -5], [4, 8], [-11, 13], [-10, 8], [0, -2], [-6, 12], [-18, 8], [30, 0], [14, -2], [28, 4], [-26, 4], [-15, 5], [10, 4], [18, 2], [7, 5], [26, -2], [-26, 2], [-6, 14], [10, -2], [25, -3], [4, -2], [-5, -13], [0, 8], [-13, 15], [29, -3], [20, -12], [-18, 6], [-10, 6], [32, -6], [-10, 0], [-18, -4], [5, -5], [6, -4], [32, 4], [25, -11], [33, -7], [0, -12], [15, -11], [0, -2], [-16, 20], [20, -6], [-20, 16], [2, 4], [16, -20], [10, -8], [18, 10], [2, -4], [33, -1], [-8, -12], [-4, 8], [17, 3], [41, 1], [28, 2], [-8, 10], [-30, 0], [-36, -4], [7, 1], [-22, 18], [-7, 9], [-4, 2], [7, 13], [-18, -12], [-31, -1], [13, -17], [11, -13], [-16, 14], [13, 3], [22, -16], [-3, -1], [-44, 0], [-41, 9], [-14, 18], [31, -13], [-8, -4], [-12, 6], [-5, 13], [-7, -7], [16, -22], [37, 5], [-4, -6], [26, -4], [-28, 16], [-5, -7], [-18, 20], [-10, 18], [24, 12], [-16, 8], [2, 0], [-37, 3], [-1, 5], [14, 0], [-22, 16], [-1, -17], [12, -24], [-14, 16], [48, -4], [5, 5], [13, -11], [-5, -1], [-32, 8], [6, -12], [-12, 10], [22, 8], [-12, -12], [-30, 0], [-2, -18], [14, 10], [4, -4], [-3, -7], [40, 4], [18, 12], [-38, 4], [38, -8], [1, -21], [-3, -15], [25, -3], [-30, 0], [-12, 12], [3, 23], [22, 0], [26, -8], [-28, -14], [8, 0], [15, 3], [-6, 18], [24, -2], [-2, 24], [-37, 7], [-50, 8], [1, -21], [-16, -10], [6, -24], [25, 11], [-14, -6], [50, -2], [-28, -10], [2, 18], [48, -10], [12, -2], [-14, 16], [-37, 5], [-12, -2], [27, -15], [2, 0], [4, -2], [-20, 6], [-52, -2], [-16, 4], [-26, 16], [-26, 18], [13, 5], [-16, -16], [41, -7], [2, -12], [1, 19], [-21, 15], [66, 0], [-32, 12], [6, 16], [-4, -4], [42, 6], [52, -6], [-15, -17], [28, -26], [20, -20], [30, 0], [-6, 28], [51, -3], [-28, -6], [-29, 9], [-24, 0], [5, 3], [-16, 8], [28, -2], [0, 4], [-38, -10], [22, 8], [6, 0], [0, -8], [-8, 0], [-14, -6], [45, -13], [35, -17], [-38, 22], [-31, 21], [0, 30], [7, 13], [14, 24], [9, 7], [46, -10], [4, -18], [38, 4], [-53, -5], [-52, 6], [-12, -10], [-35, 9], [-13, 13], [-26, -4], [-14, 24], [30, -26], [-24, 4], [4, 10], [-28, -2], [17, 17], [44, 8], [-14, -12], [6, 20], [-16, -10], [8, -12], [8, -32], [-58, 4], [29, -25], [-64, 4], [-29, 9], [-16, 8], [-8, 4], [-8, -18], [-40, 8], [14, -10], [-50, 10], [4, -16], [24, -8], [2, -8], [-30, 12], [-54, -10], [20, -6], [-52, 14], [-32, -6], [-19, -11], [34, -20], [22, -16], [31, -7], [-4, -20], [-21, -11], [37, -23], [2, 0], [30, 22], [-32, 22], [-2, -32], [20, 12], [-4, 2], [-70, 6], [-18, 24], [-15, -5], [14, 2], [9, -23], [-58, 14], [-34, 22], [22, -28], [20, -16], [-1, 21], [-56, -10], [3, 3], [40, 14], [12, 10], [-56, -6], [-16, 12], [2, 0], [-27, -5], [7, 13], [-10, -10], [9, 9], [25, 13], [12, 6], [50, -4], [6, -14], [-66, 18], [34, 0], [20, -24], [-40, -10], [-30, -10], [14, 20], [-43, -5], [-4, 10], [-65, 3], [63, -13], [33, 19], [38, -24], [12, 24], [-4, -14], [1, 15], [-1, -9], [8, 24], [-18, 30], [-13, -9], [-23, 29], [-33, -3], [22, -6], [-19, 29], [69, 1], [58, -6], [51, -17], [-51, 15], [32, 22], [-22, 28], [-62, 14], [-44, 24], [41, -1], [-26, -24], [22, 0], [-7, -9], [-54, -2], [6, -26], [-14, 26], [24, 16], [62, 6], [-3, 15], [17, 9], [22, 10], [-11, 29], [-13, -3], [-17, -25], [30, 12], [-54, 14], [-3, -11], [11, 13], [3, 27], [28, 4], [-41, -11], [24, 4], [-34, 24], [-14, 8], [27, -33], [55, 13], [10, 20], [-68, 4], [-43, -5], [11, 11], [-88, -2], [64, 4], [-8, 0], [-9, -35], [66, -12], [-9, -5], [5, -3], [66, 0], [-37, 15], [-24, -26], [22, 18], [-52, -2], [73, -7], [-42, -8], [-5, -35], [49, 13], [6, -16], [44, -40], [12, 8], [-25, -19], [70, 4], [18, -10], [-14, -2], [44, 20], [45, -27], [-47, 13], [-62, 2], [36, 24], [42, 6], [37, 13], [72, 14], [44, -28], [18, 32], [16, 4], [64, 12]];
    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_f();".
// 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_f(:prec:=2)
    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_f();".
// 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_f( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2001_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![-1, 1]>,<5,R![-2, 3, 1]>],Snew);
    return Vf;
end function;