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