Method on @sym: doublec = sym2poly (p)
Method on @sym: c = sym2poly (p, x)

Return vector of coefficients of a symbolic polynomial.

In the two-input form, the second argument x specifies the free variable; in this case this function returns a row vector c of symbolic expressions. The coefficients correspond to decreasing exponent of the free variable. Example:

syms x y
sym2poly(2*x^2 + 3*x - pi, x)
   ⇒ (sym) [2  3  -π]  (1×3 matrix)
sym2poly(x^2 + y*x, x)
   ⇒ (sym) [1  y  0]  (1×3 matrix)

Warning: Using the single-argument form, the coefficient vector c is a plain numeric vector (double). This is for compatibility with the Matlab Symbolic Math Toolbox. We suggest making this clear in your code by explicitly casting to double, as in:

syms x
double(sym2poly(pi*x^3 + 3*x/2 + exp(sym(1))))
   ⇒     3.1416      0   1.5000   2.7183

You may prefer specifying X or using coeffs:

coeffs(pi*x^3 + 3*x/2 + exp(sym(1)), 'all')
   ⇒ (sym) [π  0  3/2  ℯ]  (1×4 matrix)

If p is not a polynomial the result has no warranty. SymPy can certainly deal with more general concepts of polynomial but we do not yet expose all of that here.

See also: poly2sym, @sym/coeffs, polyval, roots.

Package: symbolic