
// Make newform 2009.2.a.d in Magma, downloaded from the LMFDB on 24 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_d();".
// 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_d();".
// 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, -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_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], [0, 1], [-1, 1], [0, 0], [-2, -1], [0, -1], [3, -3], [1, -2], [-1, -2], [1, 2], [0, -1], [-2, 4], [-1, 0], [-5, -2], [-9, 0], [3, 3], [5, -2], [-2, 4], [-2, -2], [-3, 6], [7, 1], [1, -8], [7, 2], [5, -2], [7, 1], [-7, -2], [-11, 4], [0, -3], [4, 1], [6, -6], [-7, 9], [4, -1], [-15, 6], [2, -3], [12, -6], [-5, 7], [-11, 4], [5, -9], [-9, 6], [-4, -5], [-16, 1], [-2, 10], [-17, -1], [-12, 2], [-3, 3], [-9, -4], [6, -7], [-3, 5], [22, -4], [17, -9], [-2, 11], [9, 6], [-10, 6], [-15, -3], [9, 3], [14, 1], [5, 1], [11, 6], [14, -9], [5, -5], [-9, 8], [-25, 4], [2, -15], [9, 6], [-1, 9], [-9, -3], [-23, 10], [5, -9], [-24, 6], [-19, 6], [3, -12], [0, -12], [-26, 4], [-3, 11], [-17, 4], [-4, 4], [16, -7], [-16, -9], [11, -2], [-28, 0], [-14, -7], [11, -3], [17, -5], [30, 2], [28, 4], [-10, 7], [12, 12], [3, -10], [-9, 0], [-17, 1], [-3, 15], [-13, 19], [-30, -1], [4, -7], [-20, 16], [15, 6], [3, 9], [-2, 20], [17, 0], [-32, 4], [11, 9], [9, 0], [19, -4], [-4, -14], [1, 10], [16, -20], [-6, 18], [8, 4], [-11, 2], [-24, 17], [-10, 6], [-7, 0], [18, -15], [15, 5], [-6, -16], [-33, 0], [8, 0], [12, -12], [24, 6], [2, -14], [34, 4], [0, -16], [-27, 0], [37, -4], [-20, 1], [22, -16], [-27, 2], [-14, -7], [-7, -15], [-17, -14], [-11, 4], [-15, 21], [-15, -13], [4, -20], [21, 3], [-17, 10], [19, -4], [11, 6], [-15, -6], [21, 6], [-15, 11], [-16, 19], [-24, 5], [-36, 6], [-13, 6], [-1, -2], [21, -10], [37, 5], [28, -5], [19, -16], [1, -14], [39, 3], [-29, 19], [8, 4], [-41, 10], [-13, -2], [-24, 2], [-32, -10], [16, -20], [0, -18], [-49, 4], [15, -15], [-41, -5], [10, -19], [-9, 12], [-36, 15], [-23, 7], [-8, -20], [-49, 0], [6, -21], [-18, -9], [11, -6], [-24, 12], [10, 13], [-2, -8], [43, 2], [-16, 0], [23, -20], [-13, -6], [-13, 15], [11, 15], [45, 6], [1, -14], [-10, -8], [6, -9], [40, -19], [23, -18], [-16, -21], [-13, -15], [41, 4], [17, -15], [-18, 9], [-22, 12], [2, 4], [12, 9], [-20, -1], [-2, 4], [12, 17], [-15, -12], [11, 16], [-27, 24], [-37, 12], [10, -20], [42, -13], [-14, -4], [12, -30], [-6, -16], [49, 8], [30, -6], [-22, 3], [-7, 15], [15, 15], [-7, -18], [-1, -11], [-18, 0], [-27, 20], [-23, 7], [-33, 6], [-6, 0], [0, 12], [-2, 4], [-11, -11], [31, -16], [-2, 19], [-19, -2], [-25, 9], [-31, 16], [55, 5], [22, -26], [-39, 0], [4, -2], [5, 6], [-43, -12], [-30, 3], [-16, 27], [13, 14], [-31, 0], [40, -16], [0, -9], [8, 19], [5, 7], [-7, 18], [-10, -21], [47, -15], [-68, 2], [-44, 2], [33, 8], [-20, -19], [-21, -16], [18, -15], [36, 8], [28, 14], [-11, -7], [1, 19], [-48, 6], [14, 19], [13, -8], [7, 22], [-10, 13], [14, 6], [6, 14], [59, -14], [33, 2], [16, 4], [2, 10], [4, -14], [-44, 11], [-15, 12], [-34, -6], [34, -19], [-8, 10], [22, 1], [8, 3], [-13, 24], [-1, -21], [4, 1], [22, -34], [22, 7], [11, 24], [27, -21], [48, 12], [-36, 17], [18, 18], [-12, -13], [-2, -8], [-15, 30], [28, -26], [-28, 4], [-50, 7], [16, -10], [12, 18], [-41, -10], [-11, 2], [48, -24], [4, -8], [38, 1], [6, -40], [51, -12], [28, -19], [7, 19], [-6, 14], [44, -11], [-16, -18], [10, -31], [41, -12], [64, 1], [27, 15], [50, -27], [-11, 2], [14, 6], [-24, 18], [46, -31], [40, -16], [60, -16], [-80, 2], [-20, 25], [39, -30], [-32, 29], [37, -8], [27, -12], [-5, 1], [34, -5], [-62, 2], [29, -6], [-48, 18], [-11, -20], [-19, -9], [38, 9], [-29, 23], [39, 6], [-18, -7], [-3, -18], [35, -36], [64, -10], [-1, -21], [29, -20], [3, 2], [11, 22], [-11, 1], [15, 17], [-32, 4], [9, 0], [-44, 14], [-57, -4], [50, -2], [-6, 27], [-7, 42], [-42, -1], [2, -8], [80, -2], [-55, 3], [31, -8], [-29, 8], [23, 3], [-51, 23], [-7, 7], [-18, 3], [-38, 32], [-30, 6], [-13, 34], [43, 4], [8, 22], [33, 24], [-45, 0], [-4, 12], [47, 0], [-41, 26], [-5, 16], [-34, -6], [-60, 27], [40, 7], [1, -40], [-51, -6], [41, -27], [17, -12], [46, -10], [96, -3], [-56, 13], [12, 3], [-45, 20], [-29, -22], [-52, -11], [28, 1], [5, 22], [11, 9], [-60, 18], [-1, -9], [-28, 18], [-38, 28], [-27, 18], [-46, -15], [-41, 20], [-5, -16], [-11, -14], [42, -21], [14, -36], [39, -7], [-53, 38], [39, -4], [0, 0], [-4, 18], [8, -5], [29, 12], [30, -15], [27, 24], [-26, 4], [-60, -10], [-32, 14], [39, -13], [-48, 6], [-14, 19], [-67, 13], [3, 21], [-1, -3], [89, -6], [-8, 26], [36, -9], [2, -30], [0, 9], [24, -48], [-61, -2], [-27, 23], [16, 26], [-16, 31], [37, -2], [-28, 22], [-51, 0], [-51, -18], [56, -9], [-56, 38]];
    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_d();".
// 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_d(:prec:=2)
    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_d();".
// 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_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2009_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![0, 1]>,<3,R![-3, -1, 1]>],Snew);
    return Vf;
end function;