MCPcopy Create free account
hub / github.com/numpy/numpy / roots

Function roots

numpy/lib/polynomial.py:173–261  ·  view source on GitHub ↗

Return the roots of a polynomial with coefficients given in p. .. note:: This forms part of the old polynomial API. Since version 1.4, the new polynomial API defined in `numpy.polynomial` is preferred. A summary of the differences can be found in the :doc:`trans

(p)

Source from the content-addressed store, hash-verified

171
172@array_function_dispatch(_roots_dispatcher)
173def roots(p):
174 """
175 Return the roots of a polynomial with coefficients given in p.
176
177 .. note::
178 This forms part of the old polynomial API. Since version 1.4, the
179 new polynomial API defined in `numpy.polynomial` is preferred.
180 A summary of the differences can be found in the
181 :doc:`transition guide </reference/routines.polynomials>`.
182
183 The values in the rank-1 array `p` are coefficients of a polynomial.
184 If the length of `p` is n+1 then the polynomial is described by::
185
186 p[0] * x**n + p[1] * x**(n-1) + ... + p[n-1]*x + p[n]
187
188 Parameters
189 ----------
190 p : array_like
191 Rank-1 array of polynomial coefficients.
192
193 Returns
194 -------
195 out : ndarray
196 An array containing the roots of the polynomial.
197
198 Raises
199 ------
200 ValueError
201 When `p` cannot be converted to a rank-1 array.
202
203 See also
204 --------
205 poly : Find the coefficients of a polynomial with a given sequence
206 of roots.
207 polyval : Compute polynomial values.
208 polyfit : Least squares polynomial fit.
209 poly1d : A one-dimensional polynomial class.
210
211 Notes
212 -----
213 The algorithm relies on computing the eigenvalues of the
214 companion matrix [1]_.
215
216 References
217 ----------
218 .. [1] R. A. Horn & C. R. Johnson, *Matrix Analysis*. Cambridge, UK:
219 Cambridge University Press, 1999, pp. 146-7.
220
221 Examples
222 --------
223 >>> coeff = [3.2, 2, 1]
224 >>> np.roots(coeff)
225 array([-0.3125+0.46351241j, -0.3125-0.46351241j])
226
227 """
228 # If input is scalar, this makes it an array
229 p = atleast_1d(p)
230 if p.ndim != 1:

Callers 1

rootsMethod · 0.85

Calls 7

atleast_1dFunction · 0.90
diagFunction · 0.90
eigvalsFunction · 0.90
hstackFunction · 0.90
nonzeroMethod · 0.80
astypeMethod · 0.80
ravelMethod · 0.45

Tested by

no test coverage detected