ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 04 Apr 2021 19:39:21 +0200Finite field extensionhttps://ask.sagemath.org/question/9369/finite-field-extension/How can one recover the coefficients of the polynomial
representation of a number field element when the
number field is not prime?
In the following example,
F = GF(p)
R.<x> = F[]
K.<t> = GF(p^2, modulus=x^2 + 1)
a = K.random_element()
say the value of `a` obtained is `364*t + 214`.
How can I read from Sage the integer values `(364, 214)`,
I need them for manipulation, I do not want the `t` there.Tue, 15 Jan 2013 15:13:00 +0100https://ask.sagemath.org/question/9369/finite-field-extension/Answer by achrzesz for <p>How can one recover the coefficients of the polynomial
representation of a number field element when the
number field is not prime?</p>
<p>In the following example,</p>
<pre><code>F = GF(p)
R.<x> = F[]
K.<t> = GF(p^2, modulus=x^2 + 1)
a = K.random_element()
</code></pre>
<p>say the value of <code>a</code> obtained is <code>364*t + 214</code>.</p>
<p>How can I read from Sage the integer values <code>(364, 214)</code>,
I need them for manipulation, I do not want the <code>t</code> there.</p>
https://ask.sagemath.org/question/9369/finite-field-extension/?answer=14452#post-id-14452 sage: P=127
sage: F = GF(P)
sage: R.<x> = F[]
sage: K.<t> = GF(P^2, modulus = x^2 + 1)
sage: a = K.random_element()
sage: a.polynomial().coeffs()
[5, 42]
Tue, 15 Jan 2013 15:53:54 +0100https://ask.sagemath.org/question/9369/finite-field-extension/?answer=14452#post-id-14452Answer by slelievre for <p>How can one recover the coefficients of the polynomial
representation of a number field element when the
number field is not prime?</p>
<p>In the following example,</p>
<pre><code>F = GF(p)
R.<x> = F[]
K.<t> = GF(p^2, modulus=x^2 + 1)
a = K.random_element()
</code></pre>
<p>say the value of <code>a</code> obtained is <code>364*t + 214</code>.</p>
<p>How can I read from Sage the integer values <code>(364, 214)</code>,
I need them for manipulation, I do not want the <code>t</code> there.</p>
https://ask.sagemath.org/question/9369/finite-field-extension/?answer=56491#post-id-56491This updates the answer by @achrzesz, as `coeffs` is no longer available.
The `polynomial` method of a number field element gives the associated
polynomial. The `list` method of that polynomial gives its coefficients.
Beware that polynomials also have a `coefficients` method but by default
it only gives the nonzero coefficients, so one should prefer `list`.
Define the number field:
sage: p = 127
sage: x = polygen(GF(p))
sage: K.<t> = GF(p^2, modulus=x^2 + 1)
Find the coefficients of some elements expressed as polynomials
in the number field generator:
sage: a = 364*t + 214
sage: a
110*t + 87
sage: a.polynomial()
110*t + 87
sage: a.polynomial().list()
[87, 110]
sage: b = 16*t
sage: b
16*t
sage: b.polynomial()
16*t
sage: b.polynomial().list()
[0, 16]
sage: c = K(16)
sage: c
sage: c.polynomial()
16
sage: c.polynomial().list()
[16]
The `coefficients` method is error-prone:
sage: a.polynomial().coefficients() # also works here
[87, 110]
sage: b.polynomial().coefficients()
[16]
sage: c.polynomial().coefficients()
[16]
unless one is specific about including zeros:
sage: b.polynomial().coefficients(sparse=False)
[0, 16]
sage: c.polynomial().coefficients(sparse=False)
[16]
Another option is to go through the integer representation:
sage: ZZ(a.integer_representation()).digits(base=p)
[87, 110]
sage: ZZ(b.integer_representation()).digits(base=p)
[0, 16]
sage: ZZ(c.integer_representation()).digits(base=p)
[16]
There, one can also pad with zeros so that the number of
"p-adic digits" matches the degree:
sage: ZZ(c.integer_representation()).digits(base=p, padto=2)
[16, 0]
I answered the question with `p = 127` as @achrzesz did,
but the value of `p` the original poster had in mind was
likely greater than 364.
When trying the above with `p = 367`, the `integer_representation`
method is not available. Making it available independent of the
finite field implementation is now tracked at
- [Sage Trac ticket 31605: Implement `integer_representation` method for non-givaro finite fields](https://trac.sagemath.org/ticket/31605)Sun, 04 Apr 2021 19:39:21 +0200https://ask.sagemath.org/question/9369/finite-field-extension/?answer=56491#post-id-56491