MCPcopy Index your code
hub / github.com/numpy/numpy / polyfromroots

Function polyfromroots

numpy/polynomial/polynomial.py:152–213  ·  view source on GitHub ↗

Generate a monic polynomial with given roots. Return the coefficients of the polynomial .. math:: p(x) = (x - r_0) * (x - r_1) * ... * (x - r_n), where the :math:`r_n` are the roots specified in `roots`. If a zero has multiplicity n, then it must appear in `roots` n times. F

(roots)

Source from the content-addressed store, hash-verified

150
151
152def polyfromroots(roots):
153 """
154 Generate a monic polynomial with given roots.
155
156 Return the coefficients of the polynomial
157
158 .. math:: p(x) = (x - r_0) * (x - r_1) * ... * (x - r_n),
159
160 where the :math:`r_n` are the roots specified in `roots`. If a zero has
161 multiplicity n, then it must appear in `roots` n times. For instance,
162 if 2 is a root of multiplicity three and 3 is a root of multiplicity 2,
163 then `roots` looks something like [2, 2, 2, 3, 3]. The roots can appear
164 in any order.
165
166 If the returned coefficients are `c`, then
167
168 .. math:: p(x) = c_0 + c_1 * x + ... + x^n
169
170 The coefficient of the last term is 1 for monic polynomials in this
171 form.
172
173 Parameters
174 ----------
175 roots : array_like
176 Sequence containing the roots.
177
178 Returns
179 -------
180 out : ndarray
181 1-D array of the polynomial's coefficients If all the roots are
182 real, then `out` is also real, otherwise it is complex. (see
183 Examples below).
184
185 See Also
186 --------
187 numpy.polynomial.chebyshev.chebfromroots
188 numpy.polynomial.legendre.legfromroots
189 numpy.polynomial.laguerre.lagfromroots
190 numpy.polynomial.hermite.hermfromroots
191 numpy.polynomial.hermite_e.hermefromroots
192
193 Notes
194 -----
195 The coefficients are determined by multiplying together linear factors
196 of the form ``(x - r_i)``, i.e.
197
198 .. math:: p(x) = (x - r_0) (x - r_1) ... (x - r_n)
199
200 where ``n == len(roots) - 1``; note that this implies that ``1`` is always
201 returned for :math:`a_n`.
202
203 Examples
204 --------
205 >>> from numpy.polynomial import polynomial as P
206 >>> P.polyfromroots((-1,0,1)) # x(x - 1)(x + 1) = x^3 - x
207 array([ 0., -1., 0., 1.])
208 >>> j = complex(0,1)
209 >>> P.polyfromroots((-j,j)) # complex returned, though values are real

Callers

nothing calls this directly

Calls 1

_fromrootsMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…