ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 16 Sep 2015 14:19:18 -0500Transform a list of coefficients to polynomial (in GF(2^8))http://ask.sagemath.org/question/29465/transform-a-list-of-coefficients-to-polynomial-in-gf28/ Hi there,
Currently i'm trying to convert a list of coefficients automatically to a polynom. I got a list of zeros and ones and want to transform this list to a polynom in GF(2^8). First I set up the GF(2^8) with the reduction polynom
P2.<x> = GF(2)[];
p = x^8 + x^4 + x^3 + x + 1;
GF256 = GF(2^8, 'x', modulus=p)
Next I get a list of coefficients:
coeff = [0, 1, 0, 1, 1, 1, 0, 1]
which should be the polynom:
x^6 + x^4 + x^3 + x^2 + 1
How do I get this transformation from list to polynom automatically? The native thing I think about is I set up a string and fill in the coeff. like this:
polyString = str(coeff[7])+'*x^7 + '+str(coeff[6])...
And then cast it in GF256
poly = GF256(polyString)
But I think they are smarter ways out there.. Any ideas ? :)
Wed, 16 Sep 2015 08:19:32 -0500http://ask.sagemath.org/question/29465/transform-a-list-of-coefficients-to-polynomial-in-gf28/Comment by nablahero for <p>Hi there,</p>
<p>Currently i'm trying to convert a list of coefficients automatically to a polynom. I got a list of zeros and ones and want to transform this list to a polynom in GF(2^8). First I set up the GF(2^8) with the reduction polynom</p>
<pre><code>P2.<x> = GF(2)[];
p = x^8 + x^4 + x^3 + x + 1;
GF256 = GF(2^8, 'x', modulus=p)
</code></pre>
<p>Next I get a list of coefficients:</p>
<pre><code>coeff = [0, 1, 0, 1, 1, 1, 0, 1]
</code></pre>
<p>which should be the polynom:</p>
<pre><code>x^6 + x^4 + x^3 + x^2 + 1
</code></pre>
<p>How do I get this transformation from list to polynom automatically? The native thing I think about is I set up a string and fill in the coeff. like this:</p>
<pre><code>polyString = str(coeff[7])+'*x^7 + '+str(coeff[6])...
</code></pre>
<p>And then cast it in GF256</p>
<pre><code>poly = GF256(polyString)
</code></pre>
<p>But I think they are smarter ways out there.. Any ideas ? :)</p>
http://ask.sagemath.org/question/29465/transform-a-list-of-coefficients-to-polynomial-in-gf28/?comment=29474#post-id-29474Hey. Thanks. I just need to reverse the list because the highest coefficient is the first item in my list. Thanks ;)Wed, 16 Sep 2015 12:00:08 -0500http://ask.sagemath.org/question/29465/transform-a-list-of-coefficients-to-polynomial-in-gf28/?comment=29474#post-id-29474Comment by FrédéricC for <p>Hi there,</p>
<p>Currently i'm trying to convert a list of coefficients automatically to a polynom. I got a list of zeros and ones and want to transform this list to a polynom in GF(2^8). First I set up the GF(2^8) with the reduction polynom</p>
<pre><code>P2.<x> = GF(2)[];
p = x^8 + x^4 + x^3 + x + 1;
GF256 = GF(2^8, 'x', modulus=p)
</code></pre>
<p>Next I get a list of coefficients:</p>
<pre><code>coeff = [0, 1, 0, 1, 1, 1, 0, 1]
</code></pre>
<p>which should be the polynom:</p>
<pre><code>x^6 + x^4 + x^3 + x^2 + 1
</code></pre>
<p>How do I get this transformation from list to polynom automatically? The native thing I think about is I set up a string and fill in the coeff. like this:</p>
<pre><code>polyString = str(coeff[7])+'*x^7 + '+str(coeff[6])...
</code></pre>
<p>And then cast it in GF256</p>
<pre><code>poly = GF256(polyString)
</code></pre>
<p>But I think they are smarter ways out there.. Any ideas ? :)</p>
http://ask.sagemath.org/question/29465/transform-a-list-of-coefficients-to-polynomial-in-gf28/?comment=29473#post-id-29473Like that:
sage: P2.<x> = GF(2)[];
sage: p = x^8 + x^4 + x^3 + x + 1;
sage: GF256 = GF(2^8, 'x', modulus=p)
sage: GF256([0, 1, 0, 1, 1, 1, 0, 1])Wed, 16 Sep 2015 11:15:26 -0500http://ask.sagemath.org/question/29465/transform-a-list-of-coefficients-to-polynomial-in-gf28/?comment=29473#post-id-29473Answer by Nathann for <p>Hi there,</p>
<p>Currently i'm trying to convert a list of coefficients automatically to a polynom. I got a list of zeros and ones and want to transform this list to a polynom in GF(2^8). First I set up the GF(2^8) with the reduction polynom</p>
<pre><code>P2.<x> = GF(2)[];
p = x^8 + x^4 + x^3 + x + 1;
GF256 = GF(2^8, 'x', modulus=p)
</code></pre>
<p>Next I get a list of coefficients:</p>
<pre><code>coeff = [0, 1, 0, 1, 1, 1, 0, 1]
</code></pre>
<p>which should be the polynom:</p>
<pre><code>x^6 + x^4 + x^3 + x^2 + 1
</code></pre>
<p>How do I get this transformation from list to polynom automatically? The native thing I think about is I set up a string and fill in the coeff. like this:</p>
<pre><code>polyString = str(coeff[7])+'*x^7 + '+str(coeff[6])...
</code></pre>
<p>And then cast it in GF256</p>
<pre><code>poly = GF256(polyString)
</code></pre>
<p>But I think they are smarter ways out there.. Any ideas ? :)</p>
http://ask.sagemath.org/question/29465/transform-a-list-of-coefficients-to-polynomial-in-gf28/?answer=29466#post-id-29466Here is a first solution
sage: sum(c*x**i for i,c in enumerate(reversed(coeff)))
x^6 + x^4 + x^3 + x^2 + 1
And another, with "Horner's method":
sage: p = 0
sage: for i in coeff:
....: p = p*x+i
sage: p
x^6 + x^4 + x^3 + x^2 + 1
Wed, 16 Sep 2015 09:01:29 -0500http://ask.sagemath.org/question/29465/transform-a-list-of-coefficients-to-polynomial-in-gf28/?answer=29466#post-id-29466Comment by Nathann for <p>Here is a first solution</p>
<pre><code>sage: sum(c*x**i for i,c in enumerate(reversed(coeff)))
x^6 + x^4 + x^3 + x^2 + 1
</code></pre>
<p>And another, with "Horner's method":</p>
<pre><code>sage: p = 0
sage: for i in coeff:
....: p = p*x+i
sage: p
x^6 + x^4 + x^3 + x^2 + 1
</code></pre>
http://ask.sagemath.org/question/29465/transform-a-list-of-coefficients-to-polynomial-in-gf28/?comment=29476#post-id-29476No prob, I agree with you ;-)Wed, 16 Sep 2015 14:19:18 -0500http://ask.sagemath.org/question/29465/transform-a-list-of-coefficients-to-polynomial-in-gf28/?comment=29476#post-id-29476Comment by nablahero for <p>Here is a first solution</p>
<pre><code>sage: sum(c*x**i for i,c in enumerate(reversed(coeff)))
x^6 + x^4 + x^3 + x^2 + 1
</code></pre>
<p>And another, with "Horner's method":</p>
<pre><code>sage: p = 0
sage: for i in coeff:
....: p = p*x+i
sage: p
x^6 + x^4 + x^3 + x^2 + 1
</code></pre>
http://ask.sagemath.org/question/29465/transform-a-list-of-coefficients-to-polynomial-in-gf28/?comment=29475#post-id-29475Thanks for your help. But FredericsC solutions is much more practical :)Wed, 16 Sep 2015 12:00:44 -0500http://ask.sagemath.org/question/29465/transform-a-list-of-coefficients-to-polynomial-in-gf28/?comment=29475#post-id-29475