
// Make newform 1089.2.a.s in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1089_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_1089_2_a_s();".
// 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_1089_2_a_s();".
// 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_1089_a();"
function MakeCharacter_1089_a()
    N := 1089;
    order := 1;
    char_gens := [848, 244];
    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_1089_a_Hecke(Kf)
    return MakeCharacter_1089_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], [0, 0], [2, -1], [-3, 0], [0, 0], [-3, -2], [1, -1], [-4, 3], [-1, 4], [2, -4], [1, -3], [-1, -2], [-7, 8], [-5, 2], [-1, 1], [9, -1], [-6, 7], [-6, 3], [-4, 9], [0, -9], [2, -2], [-7, 4], [-3, -6], [-3, -4], [-6, 13], [-3, 0], [-6, 0], [-3, 2], [0, 0], [3, 6], [0, -6], [-9, -3], [-5, 4], [-1, -3], [15, 0], [-2, 0], [0, 6], [9, -15], [2, -13], [16, 1], [1, -2], [-15, 4], [5, -4], [13, -9], [25, 1], [-13, 6], [13, -15], [-3, -6], [-15, 16], [10, 0], [20, -7], [-3, -9], [12, -18], [1, -11], [16, 7], [-12, 7], [-18, -4], [18, -15], [-25, 9], [-2, -2], [6, 10], [5, 8], [5, 9], [-5, -4], [11, -24], [7, -20], [13, -22], [4, 6], [-16, 8], [-1, -18], [8, -4], [-14, -2], [0, -9], [-3, -12], [9, 12], [-17, 16], [-15, 15], [-35, 6], [23, -5], [16, 8], [-6, 17], [-28, 15], [-29, 17], [-6, 0], [-7, -6], [30, -18], [22, -4], [-15, -11], [-18, 5], [-9, 11], [11, 2], [-25, 5], [23, 10], [11, -23], [-12, 9], [12, -26], [-23, 1], [0, -24], [-1, -21], [-14, 9], [-21, 1], [18, 5], [1, 16], [-8, 26], [-18, 27], [12, 2], [21, -28], [8, -23], [12, 6], [-19, 24], [-9, 7], [-26, 14], [-27, -12], [-15, -10], [37, -15], [-24, 19], [-33, 9], [-21, 11], [-43, 13], [-2, 14], [-13, -5], [-5, 12], [16, 4], [13, 16], [-30, -6], [6, 27], [37, -19], [9, 12], [37, -3], [34, -6], [25, 0], [29, -10], [-6, -29], [-3, -8], [-21, 16], [4, 27], [9, -36], [14, 6], [-12, -2], [14, 7], [-15, -19], [33, -12], [-33, 4], [42, -18], [1, -27], [33, -6], [5, -8], [23, -40], [17, 16], [-2, 16], [1, 12], [-17, -5], [18, 12], [3, 0], [-30, 21], [-23, -8], [-21, 42], [23, -16], [3, -22], [-23, 35], [-14, 7], [44, 10], [18, -27], [-18, 32], [17, 20], [10, -28], [12, -15], [0, -21], [3, 9], [56, -10], [10, -5], [3, -17], [-6, 1], [-18, 38], [18, 4], [36, -12], [-16, 18], [2, 32], [-34, 30], [14, 22], [9, 18], [29, -4], [-19, 36], [9, -27], [8, -29], [-24, 43], [-36, 26], [-21, -15], [2, 31], [-13, 35], [32, -11], [47, 4], [3, -12], [46, -16], [-18, 27], [1, -5], [10, 6], [35, 3], [-2, -20], [-12, -3], [-18, -14], [-22, 0], [-6, 18], [33, -6], [-33, 46], [-21, 1], [-34, 13], [19, -35], [-29, 28], [-42, -12], [18, -27], [13, -20], [-16, 0], [18, -42], [-57, 14], [-21, 51], [36, 2], [-36, -9], [-9, 2], [-10, 22], [33, 5], [-16, 12], [-19, -2], [-18, 29], [-50, 1], [23, 4], [-10, -13], [-19, 8], [19, 26], [12, 15], [26, -14], [-1, -3], [-57, 0], [-38, -10], [6, 6], [-54, -6], [15, -15], [22, 14], [38, 14], [1, -31], [18, -14], [-60, 1], [3, -48], [-38, 6], [-44, 1], [-41, 37], [22, -13], [-14, -16], [-9, -42], [-20, 8], [23, 3], [-18, 55], [-12, 48], [42, -24], [-14, 31], [-45, 30], [47, -30], [-32, 48], [7, -20], [-15, 29], [18, -39], [29, -32], [-4, 3], [-61, 9], [-22, -17], [9, 12], [-23, 16], [-46, 38], [-28, -12], [21, -9], [0, -16], [16, -31], [-29, -4], [-24, 3], [7, 0], [18, -13], [27, 0], [11, -32], [-48, 47], [37, -40], [45, -37], [-36, -14], [-63, 12], [27, -3], [56, 12], [23, -65], [-15, 23], [39, 23], [30, -30], [-45, 10], [10, -19], [24, 6], [9, -30], [1, 32], [-27, 21], [-17, -1], [-41, 36], [-53, 28], [24, -3], [62, -15], [39, -76], [11, -16], [11, -47], [-54, 45], [57, -10], [20, 16], [10, 34], [-22, 64], [-3, 41], [-42, 31], [30, -17], [-15, -30], [-5, -41], [45, -27], [-19, 4], [-9, -47], [28, -46], [15, -6], [-26, 20], [5, -40], [36, -46], [-21, -12], [-47, -2], [-11, 15], [35, -22], [72, 0], [17, -39], [-4, 1], [-46, 13], [60, -12], [-24, 14], [8, 8], [11, -17], [12, 4], [-4, -26], [45, -21], [-29, 33], [-11, 10], [-24, 27], [-4, -38], [17, -11], [-11, -4], [-50, 20], [-5, -6], [-29, -20], [18, -6], [11, 37], [18, -25], [-20, -11], [-54, 42], [18, 0], [42, -38], [37, 7], [-51, -16], [9, -53], [-78, 24], [-6, -38], [18, -37], [0, 36], [-58, -7], [-4, -33], [25, 34], [-91, 1], [52, 16], [-45, 46], [26, -24], [29, -37], [14, 39], [-52, 0], [13, 5], [-38, 6], [-21, 5], [-6, 12], [-56, 18], [-58, -3], [-13, 16], [67, 10], [-63, -2], [28, 14], [-41, 56], [50, -4], [-5, 22], [3, -42], [-30, 51], [-12, 59], [-5, -23], [6, -38], [44, 13], [43, -4], [1, -13], [40, -40], [15, 12], [-7, -41], [5, -54], [33, -75], [-69, 0], [-78, 6], [-59, 58], [12, 10], [8, -22], [-52, 34], [-5, 7], [47, -60], [22, 35], [-9, -52], [-2, 0], [3, -15], [-19, 22], [4, 20], [28, -26], [-1, -21], [54, -37], [33, -9], [68, -15], [-15, 14], [9, 33], [-51, -3], [-6, 18], [-36, -14], [-28, -46], [-6, 22], [-25, 64], [48, -42], [-28, -34], [26, 36], [16, -28], [-53, 28], [-55, 55], [-42, 33], [-39, 8]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1089_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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