ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 06 Aug 2019 03:49:59 -0500polynomials with bounded coefficientshttps://ask.sagemath.org/question/47407/polynomials-with-bounded-coefficients/I have a list L, and would like to construct all polynomials of degree d, with coefficients from the list. An example is the following :
SET=[];
K.<a>=FiniteField(17);
F.<x> = PolynomialRing(K,'x');
for f in F.monics( of_degree=8):
S=f.coefficients(sparse=False);
if S==([K(1),K(16)]):
SET=SET+[f];
print SET;
The above command is too time consuming. Is there a simpler way, when we can get the output in less time.Tue, 06 Aug 2019 03:05:16 -0500https://ask.sagemath.org/question/47407/polynomials-with-bounded-coefficients/Answer by tmonteil for <p>I have a list L, and would like to construct all polynomials of degree d, with coefficients from the list. An example is the following : </p>
<pre><code>SET=[];
K.<a>=FiniteField(17);
F.<x> = PolynomialRing(K,'x');
for f in F.monics( of_degree=8):
S=f.coefficients(sparse=False);
if S==([K(1),K(16)]):
SET=SET+[f];
print SET;
</code></pre>
<p>The above command is too time consuming. Is there a simpler way, when we can get the output in less time.</p>
https://ask.sagemath.org/question/47407/polynomials-with-bounded-coefficients/?answer=47408#post-id-47408You waste a lot of time in creating all polynomials and then selecting the good ones.
Ton generate only the polynomials with coefficients from a list, you can use the `product` function of the `itertools`standard module to make the product of your list `d+1` times and construct your polynomials from its elements, with the additional trick that if `F` is a polynomial ring, and `t` is a tuple of elements of the base ring of `F`, then `K(t)` is the polynomial whose coefficients are the elements of `t`. This leads to:
sage: L = [K(1), K(16)]
sage: d = 8
sage: from itertools import product
sage: P = product(L, repeat=d+1)
sage: SET = [F(coeffs) for coeffs in P]
Tue, 06 Aug 2019 03:27:52 -0500https://ask.sagemath.org/question/47407/polynomials-with-bounded-coefficients/?answer=47408#post-id-47408Comment by Abhishek for <p>You waste a lot of time in creating all polynomials and then selecting the good ones.</p>
<p>Ton generate only the polynomials with coefficients from a list, you can use the <code>product</code> function of the <code>itertools</code>standard module to make the product of your list <code>d+1</code> times and construct your polynomials from its elements, with the additional trick that if <code>F</code> is a polynomial ring, and <code>t</code> is a tuple of elements of the base ring of <code>F</code>, then <code>K(t)</code> is the polynomial whose coefficients are the elements of <code>t</code>. This leads to:</p>
<pre><code>sage: L = [K(1), K(16)]
sage: d = 8
sage: from itertools import product
sage: P = product(L, repeat=d+1)
sage: SET = [F(coeffs) for coeffs in P]
</code></pre>
https://ask.sagemath.org/question/47407/polynomials-with-bounded-coefficients/?comment=47409#post-id-47409Thanks a lot, didn't know of this function.Tue, 06 Aug 2019 03:49:59 -0500https://ask.sagemath.org/question/47407/polynomials-with-bounded-coefficients/?comment=47409#post-id-47409