sagemath/sage

make accessing coefficients of finite-field elements easier

yyyyx4 opened this issue · 9 comments

This comes up rather frequently:

sage: F.<i> = GF(431^2, modulus=x^2+1)
sage: a = F.random_element()  # some computation
sage: coeffs = a.polynomial().padded_list(2)  # cumbersome!

For number fields, we do have a shorthand:

sage: K.<i> = NumberField(x^2+1)
sage: b = K.random_element()  # some computation
sage: coeffs = b.list()

This patch adds a .__getitem__() method to the class FinitePolyExtElement, which makes a[i] and list(a) and tuple(a) work as expected.

For compatibility with number-field elements, we also add .list(), which should behave identically to calling list() on the element.

Component: algebra

Author: Lorenz Panny

Branch/Commit: 121d0ab

Reviewer: Travis Scrimshaw

Issue created by migration from https://trac.sagemath.org/ticket/33748

Reviewer: Travis Scrimshaw

comment:2

Should we also add an __iter__ method too?

While it is not in the built documentation, if this ever gets moved into something public, the `n`th will fail.

Changed commit from d3f8af6 to 372464b

Branch pushed to git repo; I updated commit sha1. New commits:

372464bfix doc syntax

Branch pushed to git repo; I updated commit sha1. New commits:

121d0abadd .__iter__() method to finite-field elements

Changed commit from 372464b to 121d0ab

comment:5

Thanks — indeed, .__iter__() is significantly faster than the fallback through .__getitem__().

comment:6

Thanks. LGTM.