
// Make newform 2001.2.a.g in Magma, downloaded from the LMFDB on 23 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_g();".
// 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_g();".
// 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 := [1, -2, -5, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [-1, -4, 0, 1], [-3, -1, 1, 0], [-1, 5, 1, -1]];
    Rf_basisdens := [1, 1, 1, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    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 := [[0, 0, 0, 0], [-1, 0, 0, 0], [0, -1, 0, 0], [-1, 0, -1, 1], [-1, 1, -1, -1], [1, 0, 2, 0], [1, 0, 1, -2], [2, 0, 2, 1], [1, 0, 0, 0], [1, 0, 0, 0], [-1, 2, -1, 1], [-1, 3, -1, 0], [2, -1, 0, -2], [1, -3, -1, 0], [0, 1, 0, 0], [-4, 2, -2, 2], [-6, 2, 1, -1], [-4, -2, -2, 0], [2, 0, 2, -2], [-5, -1, 0, 2], [-5, 0, 1, -1], [-6, 2, 0, 3], [-6, 3, -4, 0], [1, -6, 3, 0], [-4, 2, -2, 0], [0, -1, 0, 2], [2, 2, 2, 2], [2, -7, 4, 0], [1, -4, 7, -3], [-2, 0, 0, -2], [-7, -2, 1, -3], [0, 6, -4, 0], [5, 0, 3, 0], [3, 1, -1, -3], [-10, 2, 0, 2], [-4, -3, 1, 1], [9, -1, -1, 4], [-4, -4, -2, 4], [-7, 7, 0, -4], [-4, -2, -5, -1], [-12, 4, 1, -3], [4, -2, -2, 2], [-4, 3, 0, 3], [6, -4, 4, -4], [-8, 4, -1, -5], [-13, 0, -5, -1], [-9, 2, -5, 5], [0, 3, -5, 1], [-6, 10, -6, 0], [12, 2, -6, 3], [-1, -3, 2, 4], [-17, 9, -2, 2], [2, 0, 6, 0], [-8, -1, 2, 1], [-11, -1, -6, -2], [-9, 7, -1, -1], [4, 0, 0, 0], [6, -14, 4, 2], [14, 1, -1, 5], [2, -6, 0, 2], [-3, -2, 3, -3], [1, -2, -1, 6], [-1, -6, -5, 5], [8, -6, 2, 0], [15, -4, -1, -5], [-16, 3, -2, -8], [6, 0, -4, 8], [-1, 1, 3, 8], [-4, 0, -1, 1], [-9, -2, -4, 4], [-11, 1, -7, 5], [-16, -5, 2, 1], [-4, 0, -2, -2], [1, 4, -1, -3], [-16, 2, 2, -1], [-2, -8, 8, -4], [6, -1, 6, -3], [19, -7, 5, -1], [16, -2, 8, -6], [-3, 8, -7, -3], [-4, -6, -2, 0], [6, -10, 8, -1], [-4, 4, -6, 10], [-11, 1, 3, -9], [6, -3, -3, -1], [12, -5, 6, -8], [-8, -8, -4, 0], [3, 0, 1, 1], [2, -10, 2, -2], [-2, -9, 1, 5], [6, -3, 8, 1], [-21, 11, -5, -9], [-3, -3, 9, 3], [4, 5, -2, 0], [-10, -3, -5, 7], [0, -5, 4, -9], [-5, -5, -10, 2], [-6, 7, 2, 6], [15, 6, 3, 1], [-24, -2, -8, 8], [0, -7, 5, -5], [-16, 1, -10, 6], [-18, 7, -10, 13], [15, -11, 7, 1], [-3, 2, -5, 1], [17, -2, -7, -1], [-1, -1, 12, 4], [-8, 2, -7, 11], [-28, 5, 0, 4], [-6, 14, -8, 2], [-7, -2, 3, 5], [-6, 10, -2, -4], [-13, 1, -13, 9], [-23, 3, -3, -2], [-16, -6, 0, -2], [2, 7, -12, -1], [8, -10, 4, -8], [-1, 5, 8, -2], [0, -1, -10, 8], [5, 0, 11, -2], [27, 2, 7, -1], [-23, -2, 0, 4], [5, 3, -3, 7], [16, -6, -4, -8], [-21, 11, -9, 5], [-12, 5, -10, -10], [29, -6, -3, -7], [34, -12, 3, -1], [19, -13, 1, 11], [6, 0, 4, 2], [10, -18, 6, 4], [-1, -6, -7, -4], [2, -10, 4, -1], [-1, 3, -7, -4], [9, 1, 8, -14], [11, -1, 1, -8], [-3, -7, -11, -5], [24, -8, 6, -10], [13, 13, 5, -1], [-2, 2, 0, -8], [-25, 11, -1, 9], [-4, 10, -11, -1], [13, 0, 17, -3], [33, -14, 3, -2], [-12, -4, -10, -4], [-12, 12, 2, 10], [20, -14, 6, 6], [-2, 2, 8, 0], [-5, 2, -5, -7], [4, 6, 8, 0], [17, 8, 2, -8], [-36, 5, -8, 5], [5, -6, -2, 4], [24, -9, 6, 2], [8, -2, 16, 9], [22, -9, 6, 1], [-12, 0, 2, 2], [-7, -5, -14, -8], [-4, -6, -10, 6], [12, -6, -4, 12], [-24, 0, 10, 4], [6, 9, -4, 4], [9, -4, -1, 3], [-19, 17, -7, -7], [-36, 3, -8, 4], [21, -8, 13, -12], [4, 0, -4, -8], [8, -10, 2, -8], [-4, -12, -2, 2], [-8, 3, -6, -6], [2, -1, -6, 4], [6, -11, 19, 3], [-38, 7, 2, -8], [-14, 12, 4, 1], [-5, -13, -5, 5], [0, -12, 6, 4], [1, 4, 0, -14], [14, 5, 14, -8], [-32, 12, -10, 10], [-30, 0, 6, -2], [-6, -4, -16, -2], [14, 13, 12, -1], [-5, 3, -3, 15], [26, -6, 3, 11], [-8, 1, -6, -12], [-32, 8, 4, 4], [13, 12, -17, 1], [-8, -12, -10, 2], [12, 8, 16, -12], [-14, -4, -6, -2], [5, 4, 4, 6], [6, 3, 10, -8], [18, -16, 12, -8], [22, 4, 8, 10], [4, 9, -8, 11], [-20, 15, 0, 4], [18, 6, 4, -10], [-8, 10, 8, 0], [28, -8, 0, -4], [10, 0, 1, 1], [-14, -3, -12, 5], [-28, 21, -11, 5], [11, -8, 5, 5], [16, 8, 8, -9], [-5, 12, 3, 0], [-1, 13, -12, 10], [24, 6, 8, 7], [-30, 12, 1, -5], [-3, 11, 0, -12], [5, -10, 11, -13], [-13, 0, 9, 1], [1, -13, -2, -8], [-17, -11, 5, 0], [19, 6, 9, 10], [-12, 5, 0, 12], [-5, 4, -7, -1], [33, -8, -2, 8], [2, 1, 2, 16], [18, -15, -4, 2], [8, -3, 18, 12], [-8, -4, -4, 4], [-14, -10, -12, 6], [-28, 10, -2, -16], [-15, -9, -3, -2], [6, -20, 17, -7], [9, -12, 1, -15], [39, 8, 3, -4], [6, -17, 2, -6], [13, 1, 13, -4], [17, -11, -5, -13], [27, 8, 7, 5], [-4, -20, 4, 4], [0, -4, 20, -7], [-10, 7, -8, 6], [-37, 14, -1, -19], [19, 7, -9, -5], [14, 6, 8, 5], [-3, 28, -11, -4], [12, -6, 7, -5], [-2, 26, 2, -6], [30, -9, 8, 3], [-4, -15, 19, 1], [-14, 9, -13, 1], [-5, 20, -1, 3], [-5, -17, -9, 15], [-35, 15, -12, 2], [9, -1, 9, -9], [-38, 5, -2, -8], [29, 10, 1, -1], [-16, 5, -14, 9], [18, 12, 12, 2], [-2, 1, 0, -10], [4, -9, -6, 13], [-2, -6, -4, 6], [28, 1, -8, -2], [-28, 6, 0, 8], [-20, -6, -18, -4], [36, 6, -6, -6], [-34, 16, -9, 5], [-4, 3, 9, 3], [19, -17, -7, -13], [39, -10, -5, 2], [-28, -5, -21, 3], [23, 11, -7, 5], [36, -19, 4, -4], [11, 1, 11, 2], [3, -4, 23, -12], [-9, 12, 13, 4], [28, 0, 2, -2], [26, -26, 12, -8], [16, 1, -7, 3], [20, 10, 22, -10], [32, -16, 6, -4], [-21, -3, 3, -4], [-4, 4, -12, -2], [10, 2, -14, 6], [10, 2, -12, -2], [17, 19, 7, -7], [27, 3, 1, -3], [12, 5, 6, 4], [-24, -4, -8, 0], [-7, 22, -7, -7], [-14, 8, -14, -6], [-1, -6, 9, 15], [-10, 10, 10, -4], [32, 7, 16, 0], [-4, -6, -8, 5], [52, -1, 10, -1], [3, -3, 3, -7], [-7, -25, 12, 0], [-10, 15, 0, -2], [10, 3, -4, 4], [20, -2, 5, -1], [-15, 10, -7, 4], [6, 20, 0, 6], [18, 18, 14, -8], [-25, 23, -7, -14], [-4, 24, -5, -9], [6, -18, 6, 2], [8, 4, 0, 10], [40, 2, 4, -5], [8, -9, 22, -8], [-45, 7, 1, -5], [-16, -10, 2, -6], [10, 0, -12, 8], [-26, 3, -15, 1], [14, 5, 2, -4], [17, 8, -13, 13], [-4, -10, -2, -8], [-10, 11, -17, -7], [14, -6, -2, 26], [-14, 4, 4, -2], [30, -4, -4, 2], [-16, -11, -13, 3], [26, 3, -4, 14], [-14, 4, -4, 18], [4, -5, 18, 14], [-15, 9, -21, -5], [0, -6, -14, 6], [-4, -19, -10, 4], [28, -8, 2, 18], [10, 22, -4, -10], [-14, 10, 15, -7], [13, 0, 27, -25], [-4, -1, 2, -20], [-30, 14, 4, -6], [-14, 24, -8, 2], [-15, -4, 4, -12], [-3, -3, 5, 17], [-10, 24, -10, 6], [-42, 14, 6, -4], [-33, -7, -2, -6], [-42, -3, 9, 1], [-12, 17, -22, 2], [-5, 7, -1, -14], [21, -31, 7, 7], [29, -10, 3, 23], [-25, 9, -5, 5], [-47, -6, -7, -1], [31, -2, 11, -7], [1, 2, -7, -13], [-20, -2, 6, -18], [-26, -16, -14, 10], [-4, -8, 20, 12], [6, 3, 0, 0], [32, -10, 16, -4], [4, 6, -20, 18], [-15, 4, 7, -1], [-5, 3, -1, 11], [33, -10, 3, -2], [40, 13, -9, -3], [-36, 3, -17, 11], [18, 4, -20, 12], [-11, 14, -21, 5], [34, -20, 16, -10], [-28, 29, -8, -6], [43, 8, -1, -4], [-48, 6, 4, -22], [-28, 1, 10, 0], [-43, 11, 4, 4], [-5, -2, 1, 15], [-33, -1, -17, 7], [4, -19, 10, 8], [20, 1, 12, -4], [0, 8, 22, 8], [-4, -22, -4, 0], [-26, -14, 20, -2], [11, -4, 3, 9], [-7, -23, -5, 10], [16, -9, 22, 2], [-22, 15, -11, -19], [7, -13, -16, 14], [5, 8, 25, -6], [-48, 24, -8, -6], [-10, 9, -27, 15], [24, -3, 24, -2], [-9, 1, 19, 11], [22, -26, -2, 13], [44, 12, 12, 0], [19, -12, 18, 6], [-8, -11, -12, 15], [44, 1, 4, -8], [63, -16, 13, -11], [10, -5, 18, 18], [33, -26, 11, -3], [5, -13, 26, -8], [3, -4, 11, 5], [-29, 25, -15, -1], [-51, 13, -1, -4], [-50, -3, -12, 3], [-33, 17, -11, -8], [-11, -11, -15, -3], [-18, 24, -4, 4], [13, -16, -5, 23], [-3, -8, 19, -10], [-24, 24, -14, -12], [48, 12, -8, 6], [-60, 7, -2, -17], [29, -20, -7, -5], [-56, 11, -8, -22], [-52, 6, -6, -2], [10, -3, 32, 0], [29, -31, 17, 10], [54, -3, 6, -6], [-3, 13, 9, 9], [-37, -22, -8, 10], [-33, 28, -19, -1], [-10, -10, -4, -18], [-19, 3, -9, 10], [8, 6, -23, 17], [19, -6, 10, 10], [-37, 19, -5, -5], [-12, 2, 12, 0], [6, 8, 2, -18], [-33, 17, -13, -14], [-40, -2, -18, -6], [-58, -8, -14, 12], [-15, -14, 12, -6], [7, -8, -25, 0], [5, 1, 9, 25], [-56, 14, -14, -20], [38, -22, 14, -21], [-36, 0, -12, -14], [16, -16, 16, 6], [32, -5, 27, -33], [-10, 10, 16, -12], [16, -20, 30, -10], [0, -4, 24, 6], [7, 7, -5, -11], [-24, 22, -4, 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_g();".
// 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_g(:prec:=4)
    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_g();".
// 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_g( : 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, -8, -6, 2, 1]>],Snew);
    return Vf;
end function;