
// Make newform 1989.2.a.h in Magma, downloaded from the LMFDB on 17 June 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1989_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_1989_2_a_h();".
// 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_1989_2_a_h();".
// 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 := [-5, -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_1989_a();"
function MakeCharacter_1989_a()
    N := 1989;
    order := 1;
    char_gens := [443, 613, 1873];
    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_1989_a_Hecke(Kf)
    return MakeCharacter_1989_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]];
    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], [0, 0], [1, 0], [-3, 1], [-2, 1], [-1, 0], [-1, 0], [2, 1], [-2, -2], [-9, 0], [5, -2], [-5, 2], [0, 0], [9, 0], [2, -2], [5, 1], [-3, -2], [9, 1], [-10, 2], [-2, 0], [3, 2], [-3, -2], [1, -2], [2, 5], [1, 5], [5, -4], [-1, 0], [2, -3], [-3, -5], [1, 1], [8, -5], [-5, -2], [-2, 1], [10, -6], [10, -5], [2, 6], [-11, 2], [13, 1], [-11, 8], [3, -4], [11, -4], [-17, -1], [7, -6], [6, -2], [-6, -6], [11, 0], [17, -2], [11, -1], [2, 2], [5, 2], [9, -10], [9, 6], [1, 6], [5, -3], [-1, 4], [2, 0], [12, 3], [25, -3], [-14, -4], [13, -9], [7, 1], [-23, -3], [18, -1], [-19, 2], [-1, 0], [-17, 0], [-11, 0], [-8, -1], [-1, 6], [16, 3], [-8, 8], [15, -1], [-11, -5], [11, -9], [-8, 6], [-11, 10], [10, 5], [-10, 4], [10, -5], [2, 4], [16, 3], [-8, -1], [28, 4], [-2, 4], [-13, 3], [3, 2], [-1, -9], [5, -11], [-17, 6], [5, -6], [-3, 1], [-19, 9], [-23, 0], [4, -5], [-6, 4], [-26, 3], [21, 2], [-17, -2], [2, 15], [0, -1], [-9, 15], [-18, -2], [-31, 5], [16, -3], [28, -4], [7, -13], [13, -5], [17, 3], [-5, 0], [-5, 17], [5, 2], [-33, 6], [-17, 2], [37, 4], [21, 4], [1, 2], [9, -11], [27, 1], [-17, 14], [7, 10], [28, -6], [14, -10], [19, -15], [4, 10], [22, 10], [18, -5], [-18, 7], [10, -4], [18, -8], [15, -4], [-7, -11], [-12, 6], [0, -8], [0, -3], [-15, -10], [-39, 1], [-8, 1], [-1, -18], [-18, 15], [20, -8], [23, -4], [-7, 22], [11, 2], [-7, 17], [10, 2], [10, 1], [-37, 4], [3, -14], [6, -7], [39, 0], [22, -3], [14, 7], [-19, 0], [42, -5], [28, 3], [31, 4], [-38, -2], [10, 3], [-11, -6], [28, -1], [4, 0], [-24, 2], [-41, 2], [-35, -5], [-25, 5], [3, -14], [-6, 1], [-17, 14], [-22, 13], [27, -3], [-29, -8], [-25, 1], [-1, -4], [27, -2], [-15, -4], [-2, -17], [20, -15], [11, 17], [-19, -4], [31, -8], [-42, -1], [19, 4], [12, -6], [23, -9], [30, -14], [13, 8], [2, -15], [-13, -15], [-1, -6], [-14, 13], [17, 6], [39, -12], [50, 4], [-5, -9], [-14, -3], [-1, 8], [-17, 9], [10, 7], [-37, -3], [-15, 18], [-20, 16], [12, 18], [-10, -17], [27, 3], [-14, -14], [31, 7], [9, 8], [-12, -17], [19, -14], [20, 3], [13, -6], [27, -12], [17, 12], [21, -19], [5, -4], [10, -8], [-9, 21], [48, 1], [34, -11], [-8, 5], [-30, -11], [-29, 2], [-7, 1], [-26, -5], [28, -21], [1, 9], [4, -13], [-16, 8], [0, 5], [43, -1], [29, -13], [-38, 14], [-12, -14], [-52, -4], [-23, -3], [13, -6], [-12, -6], [56, 1], [35, 10], [-60, 1], [-47, -7], [26, 2], [4, 21], [41, 8], [-12, 24], [-59, -6], [-37, -3], [8, -24], [10, 18], [-21, 24], [-35, -3], [42, 9], [-30, 1], [-20, -2], [4, -2], [16, -17], [-15, -9], [-5, -10], [-47, 10], [3, -22], [-27, 1], [-8, 12], [28, 1], [-29, -12], [15, -17], [-34, 3], [25, -26], [3, -6], [-10, 3], [28, -10], [6, -21], [-6, 22], [3, 2], [57, -10], [30, -25], [8, -4], [32, -7], [-36, -2], [22, 4], [20, 4], [17, -21], [4, -5], [43, -16], [-11, -6], [10, -15], [-5, -17], [-26, 27], [-40, -15], [-31, 14], [61, 4], [-3, 24], [-32, 28], [37, 4], [-14, 28], [-19, 0], [36, -24], [-52, -3], [-42, 0], [27, -24], [19, 4], [-18, 6], [10, -26], [0, -25], [-9, -7], [25, -9], [2, -14], [17, -9], [10, -20], [31, -26], [40, 3], [-65, 6], [4, -4], [0, 15], [14, 10], [-31, -19], [22, -36], [6, 16], [-17, -11], [14, -7], [14, -18], [-6, 13], [7, -29], [-11, 15], [53, -4], [6, 20], [30, 8], [-18, 14], [48, 0], [-68, 4], [-27, 15], [49, -8], [42, -10], [28, 7], [4, -9], [78, -6], [54, 11], [-12, 0], [25, 1], [36, 5], [-42, -16], [11, -5], [-71, -2], [-46, 8], [65, 0], [-17, 14], [15, 15], [-2, -9], [-23, -4], [36, -2], [53, -20], [14, -23], [46, -5], [32, -12], [18, 14], [13, 1], [39, 6], [21, -12], [-2, -4], [-40, 4], [-57, -9], [36, -19], [-10, -13], [1, 2], [-79, -2], [28, -27], [-41, -3], [3, -27], [27, -16], [-18, -2], [43, 9], [63, 12], [6, 3], [37, -8], [41, -11], [-8, 15], [14, -15], [-23, 21], [-11, 9], [55, 11], [-22, 16], [-37, -18], [5, 0], [32, 3], [-25, 5], [-15, -5], [24, 0], [10, -10], [-37, 12], [-36, -6], [23, 17], [-5, -28], [24, 17], [3, -2], [-57, 14], [-23, -2], [34, -9], [-2, -9], [-25, 22], [-54, 14], [-50, 13], [-45, 4], [-60, -12], [18, 19], [3, 3], [14, 20], [24, -36], [9, 13], [29, 15], [6, -18], [-87, 1], [-4, -29], [40, 12], [6, 5], [-8, -27], [19, -6], [-84, 9], [-29, 10], [86, -3], [58, 10], [2, -2], [2, -14], [-19, -20], [-44, -14], [12, -3], [22, -23], [-63, 6], [39, -7], [29, -20], [-8, 10], [-39, -7]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1989_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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