Ask Your Question

Polynomial coefficient vector to symbolic polynomial

asked 2013-11-14 09:36:07 -0500

gundamlh gravatar image

updated 2013-11-14 09:36:34 -0500

link text

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!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-11-14 09:48:31 -0500

tmonteil gravatar image

updated 2013-11-14 23:32:34 -0500

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:

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.

edit flag offensive delete link more


Thanks! The type "polynomial" is better than "symbolic expression", from my point of view. It has methods such "smith_form()", "roots()", ...

gundamlh gravatar imagegundamlh ( 2013-11-14 23:45:53 -0500 )edit

Yep, i definitely agree with that, you know where your objects live :) The same difference between `sqrt(2)` and `sqrt(QQbar(2))`.

tmonteil gravatar imagetmonteil ( 2013-11-14 23:58:27 -0500 )edit

:) I am a control Eng. student, and trying to build my own robust control toolbox using SAGE and MATLAB.

gundamlh gravatar imagegundamlh ( 2013-11-15 00:09:14 -0500 )edit

Well, Sage is far from being robust or certified. I am not an expert at all, but aren't tools like `coq` more adapted ?

tmonteil gravatar imagetmonteil ( 2013-11-15 00:21:15 -0500 )edit

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.

gundamlh gravatar imagegundamlh ( 2013-11-22 05:04:21 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2013-11-14 09:36:07 -0500

Seen: 666 times

Last updated: Nov 14 '13