# Polynomial coefficient vector to symbolic polynomial

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.

edit retag close merge delete

Sort by » oldest newest most voted

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.

more

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

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

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

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

Coq 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.