
// Make newform 2009.2.a.i in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_2009_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_2009_2_a_i();".
// 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_2009_2_a_i();".
// 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 := [3, -4, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-3, 0, 1]];
    Rf_basisdens := [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_2009_a();"
function MakeCharacter_2009_a()
    N := 2009;
    order := 1;
    char_gens := [493, 785];
    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_2009_a_Hecke(Kf)
    return MakeCharacter_2009_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, -1], [1, 0, 1], [0, 0, 0], [0, 0, 0], [-2, -2, 0], [-2, 1, 2], [-2, -1, 1], [-4, -1, -1], [-1, 2, -1], [0, 0, -2], [2, -4, 0], [-4, -1, -2], [1, 0, 0], [0, -1, 1], [2, 3, 0], [-2, -2, 0], [0, 4, 2], [0, 6, -4], [4, -4, -2], [-4, 2, 2], [4, -2, -4], [-6, 0, 0], [-4, -2, 2], [-2, 1, 2], [-7, 2, 1], [1, 6, -3], [2, 2, -4], [0, 3, -3], [0, 2, 4], [-9, 0, 3], [4, 3, 0], [-10, 2, -2], [-4, -4, 4], [-10, -4, 2], [0, 0, 6], [10, 0, 6], [-2, 7, 5], [1, -6, -3], [-12, 5, 1], [10, -2, 2], [0, 0, 10], [-7, 2, 1], [12, -6, -6], [-2, 0, -6], [10, -5, -5], [-14, 1, 4], [20, -2, -4], [-6, -6, -2], [-8, 4, 8], [1, -2, -7], [-6, 0, 0], [0, -6, 2], [-6, -6, -2], [10, -10, -2], [21, 2, 1], [10, 4, -6], [2, 0, -12], [-8, -8, 2], [15, 4, -1], [12, 0, -4], [-22, 2, 0], [-6, -4, -8], [4, -2, -4], [0, 3, -9], [-10, 5, 3], [-8, 4, 0], [-14, -4, 4], [-1, 4, -1], [10, -2, 12], [0, 0, 14], [2, -10, 4], [-20, 1, 4], [-20, -2, 0], [18, 9, -3], [15, -6, 3], [17, -4, 1], [-4, 11, -6], [-14, 4, -12], [0, -15, 2], [-16, 2, 6], [2, -2, 8], [16, -8, -4], [0, -12, 4], [20, -4, 4], [-23, 4, -7], [8, 5, -8], [-9, 0, 3], [20, -4, 10], [18, 6, 0], [4, -10, 4], [2, -10, 4], [-23, 4, -7], [0, -17, 5], [11, 2, 3], [-4, -2, 8], [15, -8, 11], [30, -9, -6], [14, 9, -6], [0, 12, 6], [22, -3, 9], [-4, 4, -4], [6, 0, -12], [-2, 13, 8], [22, -5, 15], [-2, -18, 6], [9, 6, 5], [4, 5, -11], [6, -13, 10], [4, 7, 0], [-26, 1, -1], [-32, -2, 2], [-1, -4, 7], [-6, 9, -7], [-4, 14, -4], [-8, 1, 8], [-14, 16, 4], [10, -11, 8], [20, -12, 0], [2, -10, -4], [-22, 8, 8], [-34, 8, -2], [-30, -2, 2], [10, 6, 6], [-24, 0, 0], [2, -7, 16], [12, -9, 2], [4, 12, -12], [-20, 4, -4], [0, 3, -9], [-22, -10, 8], [20, 11, 4], [-12, 3, -23], [-6, -8, -4], [-16, 6, 6], [22, -2, 14], [16, 4, -10], [10, -17, 5], [-22, 2, 0], [6, 14, -8], [24, -12, 0], [6, 18, -6], [-2, 19, 3], [-2, 8, -2], [8, -4, 4], [26, 2, 6], [5, -4, -11], [-36, 0, -8], [-14, -8, -4], [-4, 14, -18], [8, 8, 4], [-1, 16, -1], [-6, -12, -12], [16, -16, -8], [-22, -9, 12], [4, 5, -8], [4, -5, -11], [-4, 12, -6], [13, -2, 5], [-6, -15, -10], [-20, 8, -2], [-3, -6, -7], [28, -11, 10], [-12, 10, -16], [-24, 5, -11], [-6, 6, -14], [-18, -14, -4], [-16, 6, -24], [41, 2, -7], [-10, 13, 5], [-26, 17, 7], [-2, -8, 18], [-4, -10, -2], [0, -12, 0], [8, 7, -10], [0, 3, -9], [2, 8, -8], [-4, -14, 8], [-3, 12, 13], [-2, 19, 4], [-14, -8, -4], [-20, 11, 16], [-12, 0, -12], [2, -12, -6], [30, -14, 2], [-4, 5, 12], [0, -2, 20], [16, 11, -14], [2, -22, 12], [-22, 10, -4], [-6, 9, -12], [-10, 20, 0], [4, 22, -8], [20, 21, -9], [4, 18, 6], [-12, 12, 4], [14, -24, -12], [10, -6, 24], [-28, 18, -6], [9, 10, -7], [-48, 4, -4], [6, 0, -12], [-10, 8, 22], [6, 9, -21], [26, 11, -7], [30, -13, -8], [18, -20, 8], [-46, 8, 0], [-10, 2, -10], [29, 8, -11], [-26, 22, 20], [16, -18, 6], [-30, 11, -14], [-16, 17, -20], [-10, -10, -8], [-33, 20, 19], [-14, -2, 20], [20, -18, 0], [-18, -4, -20], [30, -12, 2], [-18, 24, -8], [8, -16, -8], [-2, -8, 22], [-48, -6, 4], [-22, 2, -8], [26, -10, -14], [-10, 10, 14], [-24, 2, -2], [-12, 9, -1], [4, -17, -1], [28, -11, 4], [-19, 12, -3], [-2, 1, -12], [23, -2, -1], [-19, -16, -3], [-4, -16, 16], [2, -31, 4], [2, -1, -2], [10, -2, -20], [24, -15, 32], [8, -23, -7], [20, -16, -6], [-29, 4, 15], [1, 4, -11], [-5, -16, 7], [48, -8, 2], [4, 32, -8], [4, 4, -22], [7, -20, 11], [-12, -9, -3], [-8, 4, -8], [14, -21, -9], [2, -18, 0], [-20, 13, 7], [-20, -20, 12], [2, -1, -2], [-16, -7, 11], [-36, -18, 0], [-41, -8, 11], [-9, -10, 7], [14, -13, 25], [-11, 14, 13], [-6, -6, -4], [-14, -11, -9], [22, 1, -9], [-18, -6, 0], [16, 4, 4], [20, -28, 4], [-42, 4, 2], [-16, 29, 7], [6, 24, -14], [14, -4, -16], [12, 1, -16], [38, -4, 22], [-12, -1, 4], [-14, 7, 14], [-18, 18, -2], [-2, 34, 0], [6, -12, -18], [-6, 6, -18], [25, -8, -7], [12, 3, 3], [-20, 26, 10], [-30, -6, -12], [4, 4, 20], [-18, 9, 4], [-30, 6, 14], [22, -16, 10], [-18, 12, 28], [33, -12, -15], [42, -17, 2], [14, 14, 10], [24, 20, -8], [26, -10, 6], [6, -6, -18], [14, -22, 22], [10, -3, -9], [-22, 17, 4], [30, 24, 0], [-18, -3, 12], [-1, -16, -9], [26, 14, -16], [-8, 28, -2], [4, 3, 33], [-66, 6, -2], [-12, -1, 4], [1, -4, 25], [-44, 13, -17], [-18, 6, -8], [-6, -6, -38], [-10, -18, -6], [-3, -16, -11], [-28, -4, -6], [18, 3, 15], [-16, -17, -4], [-12, 27, -11], [0, 6, 10], [64, 12, 0], [-45, -6, -17], [6, -3, 15], [22, 4, 8], [-28, 8, 4], [-26, 9, 9], [20, 30, 0], [-10, -26, -4], [24, 9, -14], [-10, -28, 4], [-24, -2, 2], [8, 11, -25], [36, 0, 14], [22, 1, 17], [28, -10, -20], [38, -16, 12], [-2, 16, -22], [-20, 13, -31], [31, -22, -5], [13, 16, -7], [38, -12, 30], [-6, -24, 24], [22, 7, 22], [-22, 20, -12], [-16, -18, 6], [-21, -36, 15], [-18, 4, -16], [-52, 4, -10], [-29, -8, 15], [-44, 19, -12], [22, -32, 6], [15, -26, -13], [-26, 21, -12], [30, -12, -18], [8, -13, -9], [-30, -1, -17], [-12, -18, -16], [28, -2, 20], [16, 14, -20], [27, -30, -9], [-6, 2, 16], [-54, -2, 8], [58, -11, 13], [12, -21, -11], [48, -13, 22], [-18, 27, -15], [-2, 10, 20], [23, 2, 23], [-27, -12, 9], [34, -4, 10], [-37, 2, -29], [-28, -17, 5], [-8, 33, -18], [6, 12, -6], [29, 8, 5], [-19, 26, 21], [10, -38, 24], [34, 4, 8], [32, 20, -8], [-32, 10, 8], [-8, 16, -16], [16, -26, 10], [14, 45, -9], [3, -6, -21], [-18, 3, -27], [14, 11, -21], [16, -16, -8], [38, 17, -3], [-36, 24, 4], [74, -2, -4], [8, 40, -4], [23, 2, 3], [64, -24, -12], [4, 40, -4], [-26, 10, -16], [-30, -15, -6], [-20, 40, 10], [24, -6, 42], [-55, 14, -7], [-20, 30, 12], [14, 23, 8], [-8, -2, 26], [-53, 10, -1], [62, 8, 6], [-72, 6, -6], [50, 2, 8], [19, -14, 11], [-11, 22, -7], [-12, 4, 32], [15, -24, 3], [-2, 5, -35], [-14, -14, 12], [-34, -1, 4], [4, 16, -16], [-44, 16, -22], [3, -24, 11], [-12, -12, -6], [-84, 8, 10], [46, -10, 4], [-30, 6, -6], [-4, -19, -4], [-32, 7, 3], [6, -9, 33], [10, 24, -6], [-36, -21, 2], [23, -22, 19], [60, 9, -4], [5, -20, -19], [-26, 22, -8], [30, 0, -12], [-8, -2, 4], [-50, -2, -4], [14, -7, 28], [-24, 9, 33]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2009_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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