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.Sat, 15 Sep 2018 19:48:14 +0200Polynomial coefficient vector to symbolic polynomialhttps://ask.sagemath.org/question/10734/polynomial-coefficient-vector-to-symbolic-polynomial/[link text](http://www.mathworks.de/de/help/symbolic/poly2sym.html)
matlab: poly2sym([1 3 2])
ans =
x^2 + 3*x + 2
I couldn't find a similar function in SAGE. Fortunately this small function is quite easy, consisting of only one loop.
Thanks in advance!Thu, 14 Nov 2013 16:36:07 +0100https://ask.sagemath.org/question/10734/polynomial-coefficient-vector-to-symbolic-polynomial/Answer by tmonteil for <p><a href="http://www.mathworks.de/de/help/symbolic/poly2sym.html">link text</a></p>
<pre><code>matlab: poly2sym([1 3 2])
ans =
x^2 + 3*x + 2
</code></pre>
<p>I couldn't find a similar function in SAGE. Fortunately this small function is quite easy, consisting of only one loop.</p>
<p>Thanks in advance!</p>
https://ask.sagemath.org/question/10734/polynomial-coefficient-vector-to-symbolic-polynomial/?answer=15698#post-id-15698I am not sure that Sage shoud contain a function for each such feature, it will lead to a huge global namespace, making tab completion unusable. Here is a quick proposition:
sage: L = [1, 3, 2]
sage: sum([b*x^a for a,b in enumerate(reversed(L))])
x^2 + 3*x + 2
Or, if you want a function, define:
sage: poly2sym = lambda L : sum([b*x^a for a,b in enumerate(reversed(L))])
sage: poly2sym([1, 3, 2])
x^2 + 3*x + 2
Now, if you want genuine polynomials (not symbolic functions), you can define the polynomial ring you want and build a polynomial from a list as follows:
sage: R.<x> = PolynomialRing(QQ)
sage: R
Univariate Polynomial Ring in x over Rational Field
sage: R([1, 3, 2])
2*x^2 + 3*x + 1
As you can see, the order is not the one you wanted, so you can do:
sage: R(list(reversed([1, 3, 2])))
x^2 + 3*x + 2
And again:
poly2sym = lambda L : R(list(reversed(L)))
But then, you should understand that the prouced object are genuine polynomials, not symbolic expressions as before.
Thu, 14 Nov 2013 16:48:31 +0100https://ask.sagemath.org/question/10734/polynomial-coefficient-vector-to-symbolic-polynomial/?answer=15698#post-id-15698Comment by dasdipayan9038 for <p>I am not sure that Sage shoud contain a function for each such feature, it will lead to a huge global namespace, making tab completion unusable. Here is a quick proposition:</p>
<pre><code>sage: L = [1, 3, 2]
sage: sum([b*x^a for a,b in enumerate(reversed(L))])
x^2 + 3*x + 2
</code></pre>
<p>Or, if you want a function, define:</p>
<pre><code>sage: poly2sym = lambda L : sum([b*x^a for a,b in enumerate(reversed(L))])
sage: poly2sym([1, 3, 2])
x^2 + 3*x + 2
</code></pre>
<p>Now, if you want genuine polynomials (not symbolic functions), you can define the polynomial ring you want and build a polynomial from a list as follows:</p>
<pre><code>sage: R.<x> = PolynomialRing(QQ)
sage: R
Univariate Polynomial Ring in x over Rational Field
sage: R([1, 3, 2])
2*x^2 + 3*x + 1
</code></pre>
<p>As you can see, the order is not the one you wanted, so you can do:</p>
<pre><code>sage: R(list(reversed([1, 3, 2])))
x^2 + 3*x + 2
</code></pre>
<p>And again:</p>
<pre><code>poly2sym = lambda L : R(list(reversed(L)))
</code></pre>
<p>But then, you should understand that the prouced object are genuine polynomials, not symbolic expressions as before.</p>
https://ask.sagemath.org/question/10734/polynomial-coefficient-vector-to-symbolic-polynomial/?comment=43679#post-id-43679Hi,
How would I get a vector from a polynomial in sage?
Say, I have f=x^2+x+1. I want to get [1,1,1] as output.Sat, 15 Sep 2018 19:48:14 +0200https://ask.sagemath.org/question/10734/polynomial-coefficient-vector-to-symbolic-polynomial/?comment=43679#post-id-43679Comment by gundamlh for <p>I am not sure that Sage shoud contain a function for each such feature, it will lead to a huge global namespace, making tab completion unusable. Here is a quick proposition:</p>
<pre><code>sage: L = [1, 3, 2]
sage: sum([b*x^a for a,b in enumerate(reversed(L))])
x^2 + 3*x + 2
</code></pre>
<p>Or, if you want a function, define:</p>
<pre><code>sage: poly2sym = lambda L : sum([b*x^a for a,b in enumerate(reversed(L))])
sage: poly2sym([1, 3, 2])
x^2 + 3*x + 2
</code></pre>
<p>Now, if you want genuine polynomials (not symbolic functions), you can define the polynomial ring you want and build a polynomial from a list as follows:</p>
<pre><code>sage: R.<x> = PolynomialRing(QQ)
sage: R
Univariate Polynomial Ring in x over Rational Field
sage: R([1, 3, 2])
2*x^2 + 3*x + 1
</code></pre>
<p>As you can see, the order is not the one you wanted, so you can do:</p>
<pre><code>sage: R(list(reversed([1, 3, 2])))
x^2 + 3*x + 2
</code></pre>
<p>And again:</p>
<pre><code>poly2sym = lambda L : R(list(reversed(L)))
</code></pre>
<p>But then, you should understand that the prouced object are genuine polynomials, not symbolic expressions as before.</p>
https://ask.sagemath.org/question/10734/polynomial-coefficient-vector-to-symbolic-polynomial/?comment=16702#post-id-16702:) I am a control Eng. student, and trying to build my own robust control toolbox using SAGE and MATLAB.Fri, 15 Nov 2013 07:09:14 +0100https://ask.sagemath.org/question/10734/polynomial-coefficient-vector-to-symbolic-polynomial/?comment=16702#post-id-16702Comment by tmonteil for <p>I am not sure that Sage shoud contain a function for each such feature, it will lead to a huge global namespace, making tab completion unusable. Here is a quick proposition:</p>
<pre><code>sage: L = [1, 3, 2]
sage: sum([b*x^a for a,b in enumerate(reversed(L))])
x^2 + 3*x + 2
</code></pre>
<p>Or, if you want a function, define:</p>
<pre><code>sage: poly2sym = lambda L : sum([b*x^a for a,b in enumerate(reversed(L))])
sage: poly2sym([1, 3, 2])
x^2 + 3*x + 2
</code></pre>
<p>Now, if you want genuine polynomials (not symbolic functions), you can define the polynomial ring you want and build a polynomial from a list as follows:</p>
<pre><code>sage: R.<x> = PolynomialRing(QQ)
sage: R
Univariate Polynomial Ring in x over Rational Field
sage: R([1, 3, 2])
2*x^2 + 3*x + 1
</code></pre>
<p>As you can see, the order is not the one you wanted, so you can do:</p>
<pre><code>sage: R(list(reversed([1, 3, 2])))
x^2 + 3*x + 2
</code></pre>
<p>And again:</p>
<pre><code>poly2sym = lambda L : R(list(reversed(L)))
</code></pre>
<p>But then, you should understand that the prouced object are genuine polynomials, not symbolic expressions as before.</p>
https://ask.sagemath.org/question/10734/polynomial-coefficient-vector-to-symbolic-polynomial/?comment=16701#post-id-16701Well, Sage is far from being robust or certified. I am not an expert at all, but aren't tools like `coq` more adapted ?Fri, 15 Nov 2013 07:21:15 +0100https://ask.sagemath.org/question/10734/polynomial-coefficient-vector-to-symbolic-polynomial/?comment=16701#post-id-16701Comment by tmonteil for <p>I am not sure that Sage shoud contain a function for each such feature, it will lead to a huge global namespace, making tab completion unusable. Here is a quick proposition:</p>
<pre><code>sage: L = [1, 3, 2]
sage: sum([b*x^a for a,b in enumerate(reversed(L))])
x^2 + 3*x + 2
</code></pre>
<p>Or, if you want a function, define:</p>
<pre><code>sage: poly2sym = lambda L : sum([b*x^a for a,b in enumerate(reversed(L))])
sage: poly2sym([1, 3, 2])
x^2 + 3*x + 2
</code></pre>
<p>Now, if you want genuine polynomials (not symbolic functions), you can define the polynomial ring you want and build a polynomial from a list as follows:</p>
<pre><code>sage: R.<x> = PolynomialRing(QQ)
sage: R
Univariate Polynomial Ring in x over Rational Field
sage: R([1, 3, 2])
2*x^2 + 3*x + 1
</code></pre>
<p>As you can see, the order is not the one you wanted, so you can do:</p>
<pre><code>sage: R(list(reversed([1, 3, 2])))
x^2 + 3*x + 2
</code></pre>
<p>And again:</p>
<pre><code>poly2sym = lambda L : R(list(reversed(L)))
</code></pre>
<p>But then, you should understand that the prouced object are genuine polynomials, not symbolic expressions as before.</p>
https://ask.sagemath.org/question/10734/polynomial-coefficient-vector-to-symbolic-polynomial/?comment=16703#post-id-16703Yep, i definitely agree with that, you know where your objects live :) The same difference between `sqrt(2)` and `sqrt(QQbar(2))`.Fri, 15 Nov 2013 06:58:27 +0100https://ask.sagemath.org/question/10734/polynomial-coefficient-vector-to-symbolic-polynomial/?comment=16703#post-id-16703Comment by gundamlh for <p>I am not sure that Sage shoud contain a function for each such feature, it will lead to a huge global namespace, making tab completion unusable. Here is a quick proposition:</p>
<pre><code>sage: L = [1, 3, 2]
sage: sum([b*x^a for a,b in enumerate(reversed(L))])
x^2 + 3*x + 2
</code></pre>
<p>Or, if you want a function, define:</p>
<pre><code>sage: poly2sym = lambda L : sum([b*x^a for a,b in enumerate(reversed(L))])
sage: poly2sym([1, 3, 2])
x^2 + 3*x + 2
</code></pre>
<p>Now, if you want genuine polynomials (not symbolic functions), you can define the polynomial ring you want and build a polynomial from a list as follows:</p>
<pre><code>sage: R.<x> = PolynomialRing(QQ)
sage: R
Univariate Polynomial Ring in x over Rational Field
sage: R([1, 3, 2])
2*x^2 + 3*x + 1
</code></pre>
<p>As you can see, the order is not the one you wanted, so you can do:</p>
<pre><code>sage: R(list(reversed([1, 3, 2])))
x^2 + 3*x + 2
</code></pre>
<p>And again:</p>
<pre><code>poly2sym = lambda L : R(list(reversed(L)))
</code></pre>
<p>But then, you should understand that the prouced object are genuine polynomials, not symbolic expressions as before.</p>
https://ask.sagemath.org/question/10734/polynomial-coefficient-vector-to-symbolic-polynomial/?comment=16704#post-id-16704Thanks! The type "polynomial" is better than "symbolic expression", from my point of view. It has methods such "smith_form()", "roots()", ...Fri, 15 Nov 2013 06:45:53 +0100https://ask.sagemath.org/question/10734/polynomial-coefficient-vector-to-symbolic-polynomial/?comment=16704#post-id-16704Comment by gundamlh for <p>I am not sure that Sage shoud contain a function for each such feature, it will lead to a huge global namespace, making tab completion unusable. Here is a quick proposition:</p>
<pre><code>sage: L = [1, 3, 2]
sage: sum([b*x^a for a,b in enumerate(reversed(L))])
x^2 + 3*x + 2
</code></pre>
<p>Or, if you want a function, define:</p>
<pre><code>sage: poly2sym = lambda L : sum([b*x^a for a,b in enumerate(reversed(L))])
sage: poly2sym([1, 3, 2])
x^2 + 3*x + 2
</code></pre>
<p>Now, if you want genuine polynomials (not symbolic functions), you can define the polynomial ring you want and build a polynomial from a list as follows:</p>
<pre><code>sage: R.<x> = PolynomialRing(QQ)
sage: R
Univariate Polynomial Ring in x over Rational Field
sage: R([1, 3, 2])
2*x^2 + 3*x + 1
</code></pre>
<p>As you can see, the order is not the one you wanted, so you can do:</p>
<pre><code>sage: R(list(reversed([1, 3, 2])))
x^2 + 3*x + 2
</code></pre>
<p>And again:</p>
<pre><code>poly2sym = lambda L : R(list(reversed(L)))
</code></pre>
<p>But then, you should understand that the prouced object are genuine polynomials, not symbolic expressions as before.</p>
https://ask.sagemath.org/question/10734/polynomial-coefficient-vector-to-symbolic-polynomial/?comment=16657#post-id-16657Coq http://en.wikipedia.org/wiki/Coq .. I have not ever heard about it before.. it seems to be a genius software.. "functional programming". In fact my control toolbox is only a kind of "toy" box.Fri, 22 Nov 2013 12:04:21 +0100https://ask.sagemath.org/question/10734/polynomial-coefficient-vector-to-symbolic-polynomial/?comment=16657#post-id-16657