Ask Your Question

Polynomial coefficient vector to symbolic polynomial

asked 2013-11-14 16:36:07 +0200

gundamlh gravatar image

updated 2013-11-14 16:36:34 +0200

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 16:48:31 +0200

tmonteil gravatar image

updated 2013-11-15 06:32:34 +0200

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-15 06:45:53 +0200 )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-15 06:58:27 +0200 )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 07:09:14 +0200 )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 07:21:15 +0200 )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 12:04:21 +0200 )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 16:36:07 +0200

Seen: 2,754 times

Last updated: Nov 15 '13