
# q-expansion of newform 147.4.e.f, downloaded from the LMFDB on 19 April 2024.

# We generate the q-expansion using the Hecke eigenvalues a_p at the primes.
# Each a_p is given as a linear combination
# of the following basis for the coefficient ring.

def make_data():

    from sage.all import prod, floor, prime_powers, gcd, QQ, primes_first_n, next_prime, RR

    def discrete_log(elts, gens, mod):
        # algorithm 2.2, page 16 of https://arxiv.org/abs/0903.2785
        def table_gens(gens, mod):
            T = [1]
            n = len(gens)
            r = [None]*n
            s = [None]*n
            for i in range(n):
                beta = gens[i]
                r[i] = 1
                N = len(T)
                while beta not in T:
                    for Tj in T[:N]:
                        T.append((beta*Tj) % mod)
                    beta = (beta*gens[i]) % mod
                    r[i] += 1
                s[i] = T.index(beta)
            return T, r, s
        T, r, s = table_gens(gens, mod)
        n = len(gens)
        N = [ prod(r[:j]) for j in range(n) ]
        Z = lambda s: [ (floor(s/N[j]) % r[j]) for j in range(n)]
        return [Z(T.index(elt % mod)) for elt in elts]
    def extend_multiplicatively(an):
        for pp in prime_powers(len(an)-1):
            for k in range(1, (len(an) - 1)//pp + 1):
                if gcd(k, pp) == 1:
                    an[pp*k] = an[pp]*an[k]
    from sage.all import PolynomialRing, NumberField
    R = PolynomialRing(QQ, "x")
    f = R(poly_data)
    K = NumberField(f, "a")
    betas = [K.gens()[0]**i for i in range(len(poly_data))]
    convert_elt_to_field = lambda elt: sum(c*beta for c, beta in zip(elt, betas))
    # convert aps to K elements
    primes = primes_first_n(len(aps_data))
    good_primes = [p for p in primes if not p.divides(level)]
    aps = map(convert_elt_to_field, aps_data)
    if not hecke_ring_character_values:
        # trivial character
        char_values = dict(zip(good_primes, [1]*len(good_primes)))
    else:
        gens = [elt[0] for elt in hecke_ring_character_values]
        gens_values = [convert_elt_to_field(elt[1]) for elt in hecke_ring_character_values]
        char_values = dict([(
            p,prod(g**k for g, k in zip(gens_values, elt)))
            for p, elt in zip(good_primes, discrete_log(good_primes, gens, level))
            ])
    an_list_bound = next_prime(primes[-1])
    an = [0]*an_list_bound
    an[1] = 1
    
    from sage.all import PowerSeriesRing
    PS = PowerSeriesRing(K, "q")
    for p, ap in zip(primes, aps):
        if p.divides(level):
            euler_factor = [1, -ap]
        else:
            euler_factor = [1, -ap, p**(weight - 1) * char_values[p]]
        k = RR(an_list_bound).log(p).floor() + 1
        foo = (1/PS(euler_factor)).padded_list(k)
        for i in range(1, k):
            an[p**i] = foo[i]
    extend_multiplicatively(an)
    return PS(an)
level = 147
weight = 4
poly_data = [1, -1, 1]

# The basis for the coefficient ring is just the power basis
# in the root of the defining polynomial above.
hecke_ring_character_values = [[50, [1, 0]], [52, [0, -1]]]
aps_data = [[0, 1], [3, -3], [0, -12], [0, 0], [-20, 20], [-84, 0], [96, -96], [0, -12], [0, 176], [58, 0], [264, -264], [0, -258], [0, 0], [156, 0], [0, 408], [722, -722], [-492, 492], [0, 492], [-412, 412], [296, 0], [-240, 240], [0, -776], [924, 0], [0, 744], [-168, 0], [1524, -1524], [0, 408], [0, 820], [918, -918], [-110, 0], [16, 0], [0, -1692], [-1126, 1126], [1092, 0], [0, -1070], [120, -120], [-1836, 1836], [0, -916], [504, 0], [0, 1836], [-2372, 2372], [-1092, 0], [0, -2512], [2430, -2430], [-1762, 0], [-3096, 3096], [156, 0], [5040, 0], [-2172, 2172], [0, -2700], [0, 3802], [-4408, 0], [-3096, 3096], [-924, 0], [0, 2760], [2360, -2360], [-4020, 4020], [0, -4800], [-6446, 6446], [-2602, 0], [6900, -6900], [-4452, 0], [-2436, 0], [7488, -7488], [0, 1752], [0, 1562], [0, 7092], [366, 0], [6364, -6364], [-10500, 0], [-408, 408], [0, 11936], [2448, -2448], [0, -11374], [-5892, 0], [0, 10488], [-4514, 4514], [0, 6036], [0, 6770], [-12504, 12504], [9492, 0], [5182, 0], [5720, -5720], [13608, 0], [0, -12864], [0, 13252], [226, 0], [0, 11334], [-1596, 0], [12728, 0], [0, 3012], [4296, -4296], [8184, -8184], [-12164, 0], [0, -972], [7728, 0], [0, -11604], [10848, -10848], [0, 18132], [0, -6950], [17012, 0], [-3926, 3926], [18828, -18828], [0, -11990], [15716, -15716], [13872, -13872], [8820, 0], [0, 16872], [6056, -6056], [10752, 0], [0, -20256], [28190, -28190], [29318, 0], [-24348, 24348], [-25184, 0], [-32318, 32318], [-3948, 0], [-13848, 13848], [0, 3158], [-24596, 0], [15468, -15468], [13470, 0], [0, 9564], [-13852, 13852], [0, 324], [24922, 0], [0, 17886], [0, 6792], [1512, 0], [0, 11244], [1996, -1996], [-656, 0], [0, -1056], [-18702, 0], [0, -17904], [-7560, 0], [14292, -14292], [-26364, 26364], [17220, 0], [-16442, 16442], [-31332, 0], [0, 25810], [-12368, 12368], [6316, 0], [23868, -23868], [-48216, 0], [-27300, 0], [-8640, 8640], [0, -24372], [0, -2176], [0, 27574], [16968, 0], [-1860, 0], [0, -2280], [-36084, 36084], [24152, 0], [0, -36336], [0, -432], [22176, 0], [43524, -43524], [0, -1868], [-9238, 0], [-30616, 0], [0, 27540], [16402, -16402], [-55176, 55176], [-27096, 27096], [16812, -16812]]
