polynomial.py 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  1. """
  2. Functions to operate on polynomials.
  3. """
  4. from __future__ import division, absolute_import, print_function
  5. __all__ = ['poly', 'roots', 'polyint', 'polyder', 'polyadd',
  6. 'polysub', 'polymul', 'polydiv', 'polyval', 'poly1d',
  7. 'polyfit', 'RankWarning']
  8. import functools
  9. import re
  10. import warnings
  11. import numpy.core.numeric as NX
  12. from numpy.core import (isscalar, abs, finfo, atleast_1d, hstack, dot, array,
  13. ones)
  14. from numpy.core import overrides
  15. from numpy.core.overrides import set_module
  16. from numpy.lib.twodim_base import diag, vander
  17. from numpy.lib.function_base import trim_zeros
  18. from numpy.lib.type_check import iscomplex, real, imag, mintypecode
  19. from numpy.linalg import eigvals, lstsq, inv
  20. array_function_dispatch = functools.partial(
  21. overrides.array_function_dispatch, module='numpy')
  22. @set_module('numpy')
  23. class RankWarning(UserWarning):
  24. """
  25. Issued by `polyfit` when the Vandermonde matrix is rank deficient.
  26. For more information, a way to suppress the warning, and an example of
  27. `RankWarning` being issued, see `polyfit`.
  28. """
  29. pass
  30. def _poly_dispatcher(seq_of_zeros):
  31. return seq_of_zeros
  32. @array_function_dispatch(_poly_dispatcher)
  33. def poly(seq_of_zeros):
  34. """
  35. Find the coefficients of a polynomial with the given sequence of roots.
  36. Returns the coefficients of the polynomial whose leading coefficient
  37. is one for the given sequence of zeros (multiple roots must be included
  38. in the sequence as many times as their multiplicity; see Examples).
  39. A square matrix (or array, which will be treated as a matrix) can also
  40. be given, in which case the coefficients of the characteristic polynomial
  41. of the matrix are returned.
  42. Parameters
  43. ----------
  44. seq_of_zeros : array_like, shape (N,) or (N, N)
  45. A sequence of polynomial roots, or a square array or matrix object.
  46. Returns
  47. -------
  48. c : ndarray
  49. 1D array of polynomial coefficients from highest to lowest degree:
  50. ``c[0] * x**(N) + c[1] * x**(N-1) + ... + c[N-1] * x + c[N]``
  51. where c[0] always equals 1.
  52. Raises
  53. ------
  54. ValueError
  55. If input is the wrong shape (the input must be a 1-D or square
  56. 2-D array).
  57. See Also
  58. --------
  59. polyval : Compute polynomial values.
  60. roots : Return the roots of a polynomial.
  61. polyfit : Least squares polynomial fit.
  62. poly1d : A one-dimensional polynomial class.
  63. Notes
  64. -----
  65. Specifying the roots of a polynomial still leaves one degree of
  66. freedom, typically represented by an undetermined leading
  67. coefficient. [1]_ In the case of this function, that coefficient -
  68. the first one in the returned array - is always taken as one. (If
  69. for some reason you have one other point, the only automatic way
  70. presently to leverage that information is to use ``polyfit``.)
  71. The characteristic polynomial, :math:`p_a(t)`, of an `n`-by-`n`
  72. matrix **A** is given by
  73. :math:`p_a(t) = \\mathrm{det}(t\\, \\mathbf{I} - \\mathbf{A})`,
  74. where **I** is the `n`-by-`n` identity matrix. [2]_
  75. References
  76. ----------
  77. .. [1] M. Sullivan and M. Sullivan, III, "Algebra and Trignometry,
  78. Enhanced With Graphing Utilities," Prentice-Hall, pg. 318, 1996.
  79. .. [2] G. Strang, "Linear Algebra and Its Applications, 2nd Edition,"
  80. Academic Press, pg. 182, 1980.
  81. Examples
  82. --------
  83. Given a sequence of a polynomial's zeros:
  84. >>> np.poly((0, 0, 0)) # Multiple root example
  85. array([1, 0, 0, 0])
  86. The line above represents z**3 + 0*z**2 + 0*z + 0.
  87. >>> np.poly((-1./2, 0, 1./2))
  88. array([ 1. , 0. , -0.25, 0. ])
  89. The line above represents z**3 - z/4
  90. >>> np.poly((np.random.random(1.)[0], 0, np.random.random(1.)[0]))
  91. array([ 1. , -0.77086955, 0.08618131, 0. ]) #random
  92. Given a square array object:
  93. >>> P = np.array([[0, 1./3], [-1./2, 0]])
  94. >>> np.poly(P)
  95. array([ 1. , 0. , 0.16666667])
  96. Note how in all cases the leading coefficient is always 1.
  97. """
  98. seq_of_zeros = atleast_1d(seq_of_zeros)
  99. sh = seq_of_zeros.shape
  100. if len(sh) == 2 and sh[0] == sh[1] and sh[0] != 0:
  101. seq_of_zeros = eigvals(seq_of_zeros)
  102. elif len(sh) == 1:
  103. dt = seq_of_zeros.dtype
  104. # Let object arrays slip through, e.g. for arbitrary precision
  105. if dt != object:
  106. seq_of_zeros = seq_of_zeros.astype(mintypecode(dt.char))
  107. else:
  108. raise ValueError("input must be 1d or non-empty square 2d array.")
  109. if len(seq_of_zeros) == 0:
  110. return 1.0
  111. dt = seq_of_zeros.dtype
  112. a = ones((1,), dtype=dt)
  113. for k in range(len(seq_of_zeros)):
  114. a = NX.convolve(a, array([1, -seq_of_zeros[k]], dtype=dt),
  115. mode='full')
  116. if issubclass(a.dtype.type, NX.complexfloating):
  117. # if complex roots are all complex conjugates, the roots are real.
  118. roots = NX.asarray(seq_of_zeros, complex)
  119. if NX.all(NX.sort(roots) == NX.sort(roots.conjugate())):
  120. a = a.real.copy()
  121. return a
  122. def _roots_dispatcher(p):
  123. return p
  124. @array_function_dispatch(_roots_dispatcher)
  125. def roots(p):
  126. """
  127. Return the roots of a polynomial with coefficients given in p.
  128. The values in the rank-1 array `p` are coefficients of a polynomial.
  129. If the length of `p` is n+1 then the polynomial is described by::
  130. p[0] * x**n + p[1] * x**(n-1) + ... + p[n-1]*x + p[n]
  131. Parameters
  132. ----------
  133. p : array_like
  134. Rank-1 array of polynomial coefficients.
  135. Returns
  136. -------
  137. out : ndarray
  138. An array containing the roots of the polynomial.
  139. Raises
  140. ------
  141. ValueError
  142. When `p` cannot be converted to a rank-1 array.
  143. See also
  144. --------
  145. poly : Find the coefficients of a polynomial with a given sequence
  146. of roots.
  147. polyval : Compute polynomial values.
  148. polyfit : Least squares polynomial fit.
  149. poly1d : A one-dimensional polynomial class.
  150. Notes
  151. -----
  152. The algorithm relies on computing the eigenvalues of the
  153. companion matrix [1]_.
  154. References
  155. ----------
  156. .. [1] R. A. Horn & C. R. Johnson, *Matrix Analysis*. Cambridge, UK:
  157. Cambridge University Press, 1999, pp. 146-7.
  158. Examples
  159. --------
  160. >>> coeff = [3.2, 2, 1]
  161. >>> np.roots(coeff)
  162. array([-0.3125+0.46351241j, -0.3125-0.46351241j])
  163. """
  164. # If input is scalar, this makes it an array
  165. p = atleast_1d(p)
  166. if p.ndim != 1:
  167. raise ValueError("Input must be a rank-1 array.")
  168. # find non-zero array entries
  169. non_zero = NX.nonzero(NX.ravel(p))[0]
  170. # Return an empty array if polynomial is all zeros
  171. if len(non_zero) == 0:
  172. return NX.array([])
  173. # find the number of trailing zeros -- this is the number of roots at 0.
  174. trailing_zeros = len(p) - non_zero[-1] - 1
  175. # strip leading and trailing zeros
  176. p = p[int(non_zero[0]):int(non_zero[-1])+1]
  177. # casting: if incoming array isn't floating point, make it floating point.
  178. if not issubclass(p.dtype.type, (NX.floating, NX.complexfloating)):
  179. p = p.astype(float)
  180. N = len(p)
  181. if N > 1:
  182. # build companion matrix and find its eigenvalues (the roots)
  183. A = diag(NX.ones((N-2,), p.dtype), -1)
  184. A[0,:] = -p[1:] / p[0]
  185. roots = eigvals(A)
  186. else:
  187. roots = NX.array([])
  188. # tack any zeros onto the back of the array
  189. roots = hstack((roots, NX.zeros(trailing_zeros, roots.dtype)))
  190. return roots
  191. def _polyint_dispatcher(p, m=None, k=None):
  192. return (p,)
  193. @array_function_dispatch(_polyint_dispatcher)
  194. def polyint(p, m=1, k=None):
  195. """
  196. Return an antiderivative (indefinite integral) of a polynomial.
  197. The returned order `m` antiderivative `P` of polynomial `p` satisfies
  198. :math:`\\frac{d^m}{dx^m}P(x) = p(x)` and is defined up to `m - 1`
  199. integration constants `k`. The constants determine the low-order
  200. polynomial part
  201. .. math:: \\frac{k_{m-1}}{0!} x^0 + \\ldots + \\frac{k_0}{(m-1)!}x^{m-1}
  202. of `P` so that :math:`P^{(j)}(0) = k_{m-j-1}`.
  203. Parameters
  204. ----------
  205. p : array_like or poly1d
  206. Polynomial to integrate.
  207. A sequence is interpreted as polynomial coefficients, see `poly1d`.
  208. m : int, optional
  209. Order of the antiderivative. (Default: 1)
  210. k : list of `m` scalars or scalar, optional
  211. Integration constants. They are given in the order of integration:
  212. those corresponding to highest-order terms come first.
  213. If ``None`` (default), all constants are assumed to be zero.
  214. If `m = 1`, a single scalar can be given instead of a list.
  215. See Also
  216. --------
  217. polyder : derivative of a polynomial
  218. poly1d.integ : equivalent method
  219. Examples
  220. --------
  221. The defining property of the antiderivative:
  222. >>> p = np.poly1d([1,1,1])
  223. >>> P = np.polyint(p)
  224. >>> P
  225. poly1d([ 0.33333333, 0.5 , 1. , 0. ])
  226. >>> np.polyder(P) == p
  227. True
  228. The integration constants default to zero, but can be specified:
  229. >>> P = np.polyint(p, 3)
  230. >>> P(0)
  231. 0.0
  232. >>> np.polyder(P)(0)
  233. 0.0
  234. >>> np.polyder(P, 2)(0)
  235. 0.0
  236. >>> P = np.polyint(p, 3, k=[6,5,3])
  237. >>> P
  238. poly1d([ 0.01666667, 0.04166667, 0.16666667, 3. , 5. , 3. ])
  239. Note that 3 = 6 / 2!, and that the constants are given in the order of
  240. integrations. Constant of the highest-order polynomial term comes first:
  241. >>> np.polyder(P, 2)(0)
  242. 6.0
  243. >>> np.polyder(P, 1)(0)
  244. 5.0
  245. >>> P(0)
  246. 3.0
  247. """
  248. m = int(m)
  249. if m < 0:
  250. raise ValueError("Order of integral must be positive (see polyder)")
  251. if k is None:
  252. k = NX.zeros(m, float)
  253. k = atleast_1d(k)
  254. if len(k) == 1 and m > 1:
  255. k = k[0]*NX.ones(m, float)
  256. if len(k) < m:
  257. raise ValueError(
  258. "k must be a scalar or a rank-1 array of length 1 or >m.")
  259. truepoly = isinstance(p, poly1d)
  260. p = NX.asarray(p)
  261. if m == 0:
  262. if truepoly:
  263. return poly1d(p)
  264. return p
  265. else:
  266. # Note: this must work also with object and integer arrays
  267. y = NX.concatenate((p.__truediv__(NX.arange(len(p), 0, -1)), [k[0]]))
  268. val = polyint(y, m - 1, k=k[1:])
  269. if truepoly:
  270. return poly1d(val)
  271. return val
  272. def _polyder_dispatcher(p, m=None):
  273. return (p,)
  274. @array_function_dispatch(_polyder_dispatcher)
  275. def polyder(p, m=1):
  276. """
  277. Return the derivative of the specified order of a polynomial.
  278. Parameters
  279. ----------
  280. p : poly1d or sequence
  281. Polynomial to differentiate.
  282. A sequence is interpreted as polynomial coefficients, see `poly1d`.
  283. m : int, optional
  284. Order of differentiation (default: 1)
  285. Returns
  286. -------
  287. der : poly1d
  288. A new polynomial representing the derivative.
  289. See Also
  290. --------
  291. polyint : Anti-derivative of a polynomial.
  292. poly1d : Class for one-dimensional polynomials.
  293. Examples
  294. --------
  295. The derivative of the polynomial :math:`x^3 + x^2 + x^1 + 1` is:
  296. >>> p = np.poly1d([1,1,1,1])
  297. >>> p2 = np.polyder(p)
  298. >>> p2
  299. poly1d([3, 2, 1])
  300. which evaluates to:
  301. >>> p2(2.)
  302. 17.0
  303. We can verify this, approximating the derivative with
  304. ``(f(x + h) - f(x))/h``:
  305. >>> (p(2. + 0.001) - p(2.)) / 0.001
  306. 17.007000999997857
  307. The fourth-order derivative of a 3rd-order polynomial is zero:
  308. >>> np.polyder(p, 2)
  309. poly1d([6, 2])
  310. >>> np.polyder(p, 3)
  311. poly1d([6])
  312. >>> np.polyder(p, 4)
  313. poly1d([ 0.])
  314. """
  315. m = int(m)
  316. if m < 0:
  317. raise ValueError("Order of derivative must be positive (see polyint)")
  318. truepoly = isinstance(p, poly1d)
  319. p = NX.asarray(p)
  320. n = len(p) - 1
  321. y = p[:-1] * NX.arange(n, 0, -1)
  322. if m == 0:
  323. val = p
  324. else:
  325. val = polyder(y, m - 1)
  326. if truepoly:
  327. val = poly1d(val)
  328. return val
  329. def _polyfit_dispatcher(x, y, deg, rcond=None, full=None, w=None, cov=None):
  330. return (x, y, w)
  331. @array_function_dispatch(_polyfit_dispatcher)
  332. def polyfit(x, y, deg, rcond=None, full=False, w=None, cov=False):
  333. """
  334. Least squares polynomial fit.
  335. Fit a polynomial ``p(x) = p[0] * x**deg + ... + p[deg]`` of degree `deg`
  336. to points `(x, y)`. Returns a vector of coefficients `p` that minimises
  337. the squared error in the order `deg`, `deg-1`, ... `0`.
  338. The `Polynomial.fit <numpy.polynomial.polynomial.Polynomial.fit>` class
  339. method is recommended for new code as it is more stable numerically. See
  340. the documentation of the method for more information.
  341. Parameters
  342. ----------
  343. x : array_like, shape (M,)
  344. x-coordinates of the M sample points ``(x[i], y[i])``.
  345. y : array_like, shape (M,) or (M, K)
  346. y-coordinates of the sample points. Several data sets of sample
  347. points sharing the same x-coordinates can be fitted at once by
  348. passing in a 2D-array that contains one dataset per column.
  349. deg : int
  350. Degree of the fitting polynomial
  351. rcond : float, optional
  352. Relative condition number of the fit. Singular values smaller than
  353. this relative to the largest singular value will be ignored. The
  354. default value is len(x)*eps, where eps is the relative precision of
  355. the float type, about 2e-16 in most cases.
  356. full : bool, optional
  357. Switch determining nature of return value. When it is False (the
  358. default) just the coefficients are returned, when True diagnostic
  359. information from the singular value decomposition is also returned.
  360. w : array_like, shape (M,), optional
  361. Weights to apply to the y-coordinates of the sample points. For
  362. gaussian uncertainties, use 1/sigma (not 1/sigma**2).
  363. cov : bool or str, optional
  364. If given and not `False`, return not just the estimate but also its
  365. covariance matrix. By default, the covariance are scaled by
  366. chi2/sqrt(N-dof), i.e., the weights are presumed to be unreliable
  367. except in a relative sense and everything is scaled such that the
  368. reduced chi2 is unity. This scaling is omitted if ``cov='unscaled'``,
  369. as is relevant for the case that the weights are 1/sigma**2, with
  370. sigma known to be a reliable estimate of the uncertainty.
  371. Returns
  372. -------
  373. p : ndarray, shape (deg + 1,) or (deg + 1, K)
  374. Polynomial coefficients, highest power first. If `y` was 2-D, the
  375. coefficients for `k`-th data set are in ``p[:,k]``.
  376. residuals, rank, singular_values, rcond
  377. Present only if `full` = True. Residuals of the least-squares fit,
  378. the effective rank of the scaled Vandermonde coefficient matrix,
  379. its singular values, and the specified value of `rcond`. For more
  380. details, see `linalg.lstsq`.
  381. V : ndarray, shape (M,M) or (M,M,K)
  382. Present only if `full` = False and `cov`=True. The covariance
  383. matrix of the polynomial coefficient estimates. The diagonal of
  384. this matrix are the variance estimates for each coefficient. If y
  385. is a 2-D array, then the covariance matrix for the `k`-th data set
  386. are in ``V[:,:,k]``
  387. Warns
  388. -----
  389. RankWarning
  390. The rank of the coefficient matrix in the least-squares fit is
  391. deficient. The warning is only raised if `full` = False.
  392. The warnings can be turned off by
  393. >>> import warnings
  394. >>> warnings.simplefilter('ignore', np.RankWarning)
  395. See Also
  396. --------
  397. polyval : Compute polynomial values.
  398. linalg.lstsq : Computes a least-squares fit.
  399. scipy.interpolate.UnivariateSpline : Computes spline fits.
  400. Notes
  401. -----
  402. The solution minimizes the squared error
  403. .. math ::
  404. E = \\sum_{j=0}^k |p(x_j) - y_j|^2
  405. in the equations::
  406. x[0]**n * p[0] + ... + x[0] * p[n-1] + p[n] = y[0]
  407. x[1]**n * p[0] + ... + x[1] * p[n-1] + p[n] = y[1]
  408. ...
  409. x[k]**n * p[0] + ... + x[k] * p[n-1] + p[n] = y[k]
  410. The coefficient matrix of the coefficients `p` is a Vandermonde matrix.
  411. `polyfit` issues a `RankWarning` when the least-squares fit is badly
  412. conditioned. This implies that the best fit is not well-defined due
  413. to numerical error. The results may be improved by lowering the polynomial
  414. degree or by replacing `x` by `x` - `x`.mean(). The `rcond` parameter
  415. can also be set to a value smaller than its default, but the resulting
  416. fit may be spurious: including contributions from the small singular
  417. values can add numerical noise to the result.
  418. Note that fitting polynomial coefficients is inherently badly conditioned
  419. when the degree of the polynomial is large or the interval of sample points
  420. is badly centered. The quality of the fit should always be checked in these
  421. cases. When polynomial fits are not satisfactory, splines may be a good
  422. alternative.
  423. References
  424. ----------
  425. .. [1] Wikipedia, "Curve fitting",
  426. https://en.wikipedia.org/wiki/Curve_fitting
  427. .. [2] Wikipedia, "Polynomial interpolation",
  428. https://en.wikipedia.org/wiki/Polynomial_interpolation
  429. Examples
  430. --------
  431. >>> x = np.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0])
  432. >>> y = np.array([0.0, 0.8, 0.9, 0.1, -0.8, -1.0])
  433. >>> z = np.polyfit(x, y, 3)
  434. >>> z
  435. array([ 0.08703704, -0.81349206, 1.69312169, -0.03968254])
  436. It is convenient to use `poly1d` objects for dealing with polynomials:
  437. >>> p = np.poly1d(z)
  438. >>> p(0.5)
  439. 0.6143849206349179
  440. >>> p(3.5)
  441. -0.34732142857143039
  442. >>> p(10)
  443. 22.579365079365115
  444. High-order polynomials may oscillate wildly:
  445. >>> p30 = np.poly1d(np.polyfit(x, y, 30))
  446. /... RankWarning: Polyfit may be poorly conditioned...
  447. >>> p30(4)
  448. -0.80000000000000204
  449. >>> p30(5)
  450. -0.99999999999999445
  451. >>> p30(4.5)
  452. -0.10547061179440398
  453. Illustration:
  454. >>> import matplotlib.pyplot as plt
  455. >>> xp = np.linspace(-2, 6, 100)
  456. >>> _ = plt.plot(x, y, '.', xp, p(xp), '-', xp, p30(xp), '--')
  457. >>> plt.ylim(-2,2)
  458. (-2, 2)
  459. >>> plt.show()
  460. """
  461. order = int(deg) + 1
  462. x = NX.asarray(x) + 0.0
  463. y = NX.asarray(y) + 0.0
  464. # check arguments.
  465. if deg < 0:
  466. raise ValueError("expected deg >= 0")
  467. if x.ndim != 1:
  468. raise TypeError("expected 1D vector for x")
  469. if x.size == 0:
  470. raise TypeError("expected non-empty vector for x")
  471. if y.ndim < 1 or y.ndim > 2:
  472. raise TypeError("expected 1D or 2D array for y")
  473. if x.shape[0] != y.shape[0]:
  474. raise TypeError("expected x and y to have same length")
  475. # set rcond
  476. if rcond is None:
  477. rcond = len(x)*finfo(x.dtype).eps
  478. # set up least squares equation for powers of x
  479. lhs = vander(x, order)
  480. rhs = y
  481. # apply weighting
  482. if w is not None:
  483. w = NX.asarray(w) + 0.0
  484. if w.ndim != 1:
  485. raise TypeError("expected a 1-d array for weights")
  486. if w.shape[0] != y.shape[0]:
  487. raise TypeError("expected w and y to have the same length")
  488. lhs *= w[:, NX.newaxis]
  489. if rhs.ndim == 2:
  490. rhs *= w[:, NX.newaxis]
  491. else:
  492. rhs *= w
  493. # scale lhs to improve condition number and solve
  494. scale = NX.sqrt((lhs*lhs).sum(axis=0))
  495. lhs /= scale
  496. c, resids, rank, s = lstsq(lhs, rhs, rcond)
  497. c = (c.T/scale).T # broadcast scale coefficients
  498. # warn on rank reduction, which indicates an ill conditioned matrix
  499. if rank != order and not full:
  500. msg = "Polyfit may be poorly conditioned"
  501. warnings.warn(msg, RankWarning, stacklevel=2)
  502. if full:
  503. return c, resids, rank, s, rcond
  504. elif cov:
  505. Vbase = inv(dot(lhs.T, lhs))
  506. Vbase /= NX.outer(scale, scale)
  507. if cov == "unscaled":
  508. fac = 1
  509. else:
  510. if len(x) <= order:
  511. raise ValueError("the number of data points must exceed order "
  512. "to scale the covariance matrix")
  513. # note, this used to be: fac = resids / (len(x) - order - 2.0)
  514. # it was deciced that the "- 2" (originally justified by "Bayesian
  515. # uncertainty analysis") is not was the user expects
  516. # (see gh-11196 and gh-11197)
  517. fac = resids / (len(x) - order)
  518. if y.ndim == 1:
  519. return c, Vbase * fac
  520. else:
  521. return c, Vbase[:,:, NX.newaxis] * fac
  522. else:
  523. return c
  524. def _polyval_dispatcher(p, x):
  525. return (p, x)
  526. @array_function_dispatch(_polyval_dispatcher)
  527. def polyval(p, x):
  528. """
  529. Evaluate a polynomial at specific values.
  530. If `p` is of length N, this function returns the value:
  531. ``p[0]*x**(N-1) + p[1]*x**(N-2) + ... + p[N-2]*x + p[N-1]``
  532. If `x` is a sequence, then `p(x)` is returned for each element of `x`.
  533. If `x` is another polynomial then the composite polynomial `p(x(t))`
  534. is returned.
  535. Parameters
  536. ----------
  537. p : array_like or poly1d object
  538. 1D array of polynomial coefficients (including coefficients equal
  539. to zero) from highest degree to the constant term, or an
  540. instance of poly1d.
  541. x : array_like or poly1d object
  542. A number, an array of numbers, or an instance of poly1d, at
  543. which to evaluate `p`.
  544. Returns
  545. -------
  546. values : ndarray or poly1d
  547. If `x` is a poly1d instance, the result is the composition of the two
  548. polynomials, i.e., `x` is "substituted" in `p` and the simplified
  549. result is returned. In addition, the type of `x` - array_like or
  550. poly1d - governs the type of the output: `x` array_like => `values`
  551. array_like, `x` a poly1d object => `values` is also.
  552. See Also
  553. --------
  554. poly1d: A polynomial class.
  555. Notes
  556. -----
  557. Horner's scheme [1]_ is used to evaluate the polynomial. Even so,
  558. for polynomials of high degree the values may be inaccurate due to
  559. rounding errors. Use carefully.
  560. References
  561. ----------
  562. .. [1] I. N. Bronshtein, K. A. Semendyayev, and K. A. Hirsch (Eng.
  563. trans. Ed.), *Handbook of Mathematics*, New York, Van Nostrand
  564. Reinhold Co., 1985, pg. 720.
  565. Examples
  566. --------
  567. >>> np.polyval([3,0,1], 5) # 3 * 5**2 + 0 * 5**1 + 1
  568. 76
  569. >>> np.polyval([3,0,1], np.poly1d(5))
  570. poly1d([ 76.])
  571. >>> np.polyval(np.poly1d([3,0,1]), 5)
  572. 76
  573. >>> np.polyval(np.poly1d([3,0,1]), np.poly1d(5))
  574. poly1d([ 76.])
  575. """
  576. p = NX.asarray(p)
  577. if isinstance(x, poly1d):
  578. y = 0
  579. else:
  580. x = NX.asarray(x)
  581. y = NX.zeros_like(x)
  582. for i in range(len(p)):
  583. y = y * x + p[i]
  584. return y
  585. def _binary_op_dispatcher(a1, a2):
  586. return (a1, a2)
  587. @array_function_dispatch(_binary_op_dispatcher)
  588. def polyadd(a1, a2):
  589. """
  590. Find the sum of two polynomials.
  591. Returns the polynomial resulting from the sum of two input polynomials.
  592. Each input must be either a poly1d object or a 1D sequence of polynomial
  593. coefficients, from highest to lowest degree.
  594. Parameters
  595. ----------
  596. a1, a2 : array_like or poly1d object
  597. Input polynomials.
  598. Returns
  599. -------
  600. out : ndarray or poly1d object
  601. The sum of the inputs. If either input is a poly1d object, then the
  602. output is also a poly1d object. Otherwise, it is a 1D array of
  603. polynomial coefficients from highest to lowest degree.
  604. See Also
  605. --------
  606. poly1d : A one-dimensional polynomial class.
  607. poly, polyadd, polyder, polydiv, polyfit, polyint, polysub, polyval
  608. Examples
  609. --------
  610. >>> np.polyadd([1, 2], [9, 5, 4])
  611. array([9, 6, 6])
  612. Using poly1d objects:
  613. >>> p1 = np.poly1d([1, 2])
  614. >>> p2 = np.poly1d([9, 5, 4])
  615. >>> print(p1)
  616. 1 x + 2
  617. >>> print(p2)
  618. 2
  619. 9 x + 5 x + 4
  620. >>> print(np.polyadd(p1, p2))
  621. 2
  622. 9 x + 6 x + 6
  623. """
  624. truepoly = (isinstance(a1, poly1d) or isinstance(a2, poly1d))
  625. a1 = atleast_1d(a1)
  626. a2 = atleast_1d(a2)
  627. diff = len(a2) - len(a1)
  628. if diff == 0:
  629. val = a1 + a2
  630. elif diff > 0:
  631. zr = NX.zeros(diff, a1.dtype)
  632. val = NX.concatenate((zr, a1)) + a2
  633. else:
  634. zr = NX.zeros(abs(diff), a2.dtype)
  635. val = a1 + NX.concatenate((zr, a2))
  636. if truepoly:
  637. val = poly1d(val)
  638. return val
  639. @array_function_dispatch(_binary_op_dispatcher)
  640. def polysub(a1, a2):
  641. """
  642. Difference (subtraction) of two polynomials.
  643. Given two polynomials `a1` and `a2`, returns ``a1 - a2``.
  644. `a1` and `a2` can be either array_like sequences of the polynomials'
  645. coefficients (including coefficients equal to zero), or `poly1d` objects.
  646. Parameters
  647. ----------
  648. a1, a2 : array_like or poly1d
  649. Minuend and subtrahend polynomials, respectively.
  650. Returns
  651. -------
  652. out : ndarray or poly1d
  653. Array or `poly1d` object of the difference polynomial's coefficients.
  654. See Also
  655. --------
  656. polyval, polydiv, polymul, polyadd
  657. Examples
  658. --------
  659. .. math:: (2 x^2 + 10 x - 2) - (3 x^2 + 10 x -4) = (-x^2 + 2)
  660. >>> np.polysub([2, 10, -2], [3, 10, -4])
  661. array([-1, 0, 2])
  662. """
  663. truepoly = (isinstance(a1, poly1d) or isinstance(a2, poly1d))
  664. a1 = atleast_1d(a1)
  665. a2 = atleast_1d(a2)
  666. diff = len(a2) - len(a1)
  667. if diff == 0:
  668. val = a1 - a2
  669. elif diff > 0:
  670. zr = NX.zeros(diff, a1.dtype)
  671. val = NX.concatenate((zr, a1)) - a2
  672. else:
  673. zr = NX.zeros(abs(diff), a2.dtype)
  674. val = a1 - NX.concatenate((zr, a2))
  675. if truepoly:
  676. val = poly1d(val)
  677. return val
  678. @array_function_dispatch(_binary_op_dispatcher)
  679. def polymul(a1, a2):
  680. """
  681. Find the product of two polynomials.
  682. Finds the polynomial resulting from the multiplication of the two input
  683. polynomials. Each input must be either a poly1d object or a 1D sequence
  684. of polynomial coefficients, from highest to lowest degree.
  685. Parameters
  686. ----------
  687. a1, a2 : array_like or poly1d object
  688. Input polynomials.
  689. Returns
  690. -------
  691. out : ndarray or poly1d object
  692. The polynomial resulting from the multiplication of the inputs. If
  693. either inputs is a poly1d object, then the output is also a poly1d
  694. object. Otherwise, it is a 1D array of polynomial coefficients from
  695. highest to lowest degree.
  696. See Also
  697. --------
  698. poly1d : A one-dimensional polynomial class.
  699. poly, polyadd, polyder, polydiv, polyfit, polyint, polysub,
  700. polyval
  701. convolve : Array convolution. Same output as polymul, but has parameter
  702. for overlap mode.
  703. Examples
  704. --------
  705. >>> np.polymul([1, 2, 3], [9, 5, 1])
  706. array([ 9, 23, 38, 17, 3])
  707. Using poly1d objects:
  708. >>> p1 = np.poly1d([1, 2, 3])
  709. >>> p2 = np.poly1d([9, 5, 1])
  710. >>> print(p1)
  711. 2
  712. 1 x + 2 x + 3
  713. >>> print(p2)
  714. 2
  715. 9 x + 5 x + 1
  716. >>> print(np.polymul(p1, p2))
  717. 4 3 2
  718. 9 x + 23 x + 38 x + 17 x + 3
  719. """
  720. truepoly = (isinstance(a1, poly1d) or isinstance(a2, poly1d))
  721. a1, a2 = poly1d(a1), poly1d(a2)
  722. val = NX.convolve(a1, a2)
  723. if truepoly:
  724. val = poly1d(val)
  725. return val
  726. def _polydiv_dispatcher(u, v):
  727. return (u, v)
  728. @array_function_dispatch(_polydiv_dispatcher)
  729. def polydiv(u, v):
  730. """
  731. Returns the quotient and remainder of polynomial division.
  732. The input arrays are the coefficients (including any coefficients
  733. equal to zero) of the "numerator" (dividend) and "denominator"
  734. (divisor) polynomials, respectively.
  735. Parameters
  736. ----------
  737. u : array_like or poly1d
  738. Dividend polynomial's coefficients.
  739. v : array_like or poly1d
  740. Divisor polynomial's coefficients.
  741. Returns
  742. -------
  743. q : ndarray
  744. Coefficients, including those equal to zero, of the quotient.
  745. r : ndarray
  746. Coefficients, including those equal to zero, of the remainder.
  747. See Also
  748. --------
  749. poly, polyadd, polyder, polydiv, polyfit, polyint, polymul, polysub,
  750. polyval
  751. Notes
  752. -----
  753. Both `u` and `v` must be 0-d or 1-d (ndim = 0 or 1), but `u.ndim` need
  754. not equal `v.ndim`. In other words, all four possible combinations -
  755. ``u.ndim = v.ndim = 0``, ``u.ndim = v.ndim = 1``,
  756. ``u.ndim = 1, v.ndim = 0``, and ``u.ndim = 0, v.ndim = 1`` - work.
  757. Examples
  758. --------
  759. .. math:: \\frac{3x^2 + 5x + 2}{2x + 1} = 1.5x + 1.75, remainder 0.25
  760. >>> x = np.array([3.0, 5.0, 2.0])
  761. >>> y = np.array([2.0, 1.0])
  762. >>> np.polydiv(x, y)
  763. (array([ 1.5 , 1.75]), array([ 0.25]))
  764. """
  765. truepoly = (isinstance(u, poly1d) or isinstance(u, poly1d))
  766. u = atleast_1d(u) + 0.0
  767. v = atleast_1d(v) + 0.0
  768. # w has the common type
  769. w = u[0] + v[0]
  770. m = len(u) - 1
  771. n = len(v) - 1
  772. scale = 1. / v[0]
  773. q = NX.zeros((max(m - n + 1, 1),), w.dtype)
  774. r = u.astype(w.dtype)
  775. for k in range(0, m-n+1):
  776. d = scale * r[k]
  777. q[k] = d
  778. r[k:k+n+1] -= d*v
  779. while NX.allclose(r[0], 0, rtol=1e-14) and (r.shape[-1] > 1):
  780. r = r[1:]
  781. if truepoly:
  782. return poly1d(q), poly1d(r)
  783. return q, r
  784. _poly_mat = re.compile(r"[*][*]([0-9]*)")
  785. def _raise_power(astr, wrap=70):
  786. n = 0
  787. line1 = ''
  788. line2 = ''
  789. output = ' '
  790. while True:
  791. mat = _poly_mat.search(astr, n)
  792. if mat is None:
  793. break
  794. span = mat.span()
  795. power = mat.groups()[0]
  796. partstr = astr[n:span[0]]
  797. n = span[1]
  798. toadd2 = partstr + ' '*(len(power)-1)
  799. toadd1 = ' '*(len(partstr)-1) + power
  800. if ((len(line2) + len(toadd2) > wrap) or
  801. (len(line1) + len(toadd1) > wrap)):
  802. output += line1 + "\n" + line2 + "\n "
  803. line1 = toadd1
  804. line2 = toadd2
  805. else:
  806. line2 += partstr + ' '*(len(power)-1)
  807. line1 += ' '*(len(partstr)-1) + power
  808. output += line1 + "\n" + line2
  809. return output + astr[n:]
  810. @set_module('numpy')
  811. class poly1d(object):
  812. """
  813. A one-dimensional polynomial class.
  814. A convenience class, used to encapsulate "natural" operations on
  815. polynomials so that said operations may take on their customary
  816. form in code (see Examples).
  817. Parameters
  818. ----------
  819. c_or_r : array_like
  820. The polynomial's coefficients, in decreasing powers, or if
  821. the value of the second parameter is True, the polynomial's
  822. roots (values where the polynomial evaluates to 0). For example,
  823. ``poly1d([1, 2, 3])`` returns an object that represents
  824. :math:`x^2 + 2x + 3`, whereas ``poly1d([1, 2, 3], True)`` returns
  825. one that represents :math:`(x-1)(x-2)(x-3) = x^3 - 6x^2 + 11x -6`.
  826. r : bool, optional
  827. If True, `c_or_r` specifies the polynomial's roots; the default
  828. is False.
  829. variable : str, optional
  830. Changes the variable used when printing `p` from `x` to `variable`
  831. (see Examples).
  832. Examples
  833. --------
  834. Construct the polynomial :math:`x^2 + 2x + 3`:
  835. >>> p = np.poly1d([1, 2, 3])
  836. >>> print(np.poly1d(p))
  837. 2
  838. 1 x + 2 x + 3
  839. Evaluate the polynomial at :math:`x = 0.5`:
  840. >>> p(0.5)
  841. 4.25
  842. Find the roots:
  843. >>> p.r
  844. array([-1.+1.41421356j, -1.-1.41421356j])
  845. >>> p(p.r)
  846. array([ -4.44089210e-16+0.j, -4.44089210e-16+0.j])
  847. These numbers in the previous line represent (0, 0) to machine precision
  848. Show the coefficients:
  849. >>> p.c
  850. array([1, 2, 3])
  851. Display the order (the leading zero-coefficients are removed):
  852. >>> p.order
  853. 2
  854. Show the coefficient of the k-th power in the polynomial
  855. (which is equivalent to ``p.c[-(i+1)]``):
  856. >>> p[1]
  857. 2
  858. Polynomials can be added, subtracted, multiplied, and divided
  859. (returns quotient and remainder):
  860. >>> p * p
  861. poly1d([ 1, 4, 10, 12, 9])
  862. >>> (p**3 + 4) / p
  863. (poly1d([ 1., 4., 10., 12., 9.]), poly1d([ 4.]))
  864. ``asarray(p)`` gives the coefficient array, so polynomials can be
  865. used in all functions that accept arrays:
  866. >>> p**2 # square of polynomial
  867. poly1d([ 1, 4, 10, 12, 9])
  868. >>> np.square(p) # square of individual coefficients
  869. array([1, 4, 9])
  870. The variable used in the string representation of `p` can be modified,
  871. using the `variable` parameter:
  872. >>> p = np.poly1d([1,2,3], variable='z')
  873. >>> print(p)
  874. 2
  875. 1 z + 2 z + 3
  876. Construct a polynomial from its roots:
  877. >>> np.poly1d([1, 2], True)
  878. poly1d([ 1, -3, 2])
  879. This is the same polynomial as obtained by:
  880. >>> np.poly1d([1, -1]) * np.poly1d([1, -2])
  881. poly1d([ 1, -3, 2])
  882. """
  883. __hash__ = None
  884. @property
  885. def coeffs(self):
  886. """ The polynomial coefficients """
  887. return self._coeffs
  888. @coeffs.setter
  889. def coeffs(self, value):
  890. # allowing this makes p.coeffs *= 2 legal
  891. if value is not self._coeffs:
  892. raise AttributeError("Cannot set attribute")
  893. @property
  894. def variable(self):
  895. """ The name of the polynomial variable """
  896. return self._variable
  897. # calculated attributes
  898. @property
  899. def order(self):
  900. """ The order or degree of the polynomial """
  901. return len(self._coeffs) - 1
  902. @property
  903. def roots(self):
  904. """ The roots of the polynomial, where self(x) == 0 """
  905. return roots(self._coeffs)
  906. # our internal _coeffs property need to be backed by __dict__['coeffs'] for
  907. # scipy to work correctly.
  908. @property
  909. def _coeffs(self):
  910. return self.__dict__['coeffs']
  911. @_coeffs.setter
  912. def _coeffs(self, coeffs):
  913. self.__dict__['coeffs'] = coeffs
  914. # alias attributes
  915. r = roots
  916. c = coef = coefficients = coeffs
  917. o = order
  918. def __init__(self, c_or_r, r=False, variable=None):
  919. if isinstance(c_or_r, poly1d):
  920. self._variable = c_or_r._variable
  921. self._coeffs = c_or_r._coeffs
  922. if set(c_or_r.__dict__) - set(self.__dict__):
  923. msg = ("In the future extra properties will not be copied "
  924. "across when constructing one poly1d from another")
  925. warnings.warn(msg, FutureWarning, stacklevel=2)
  926. self.__dict__.update(c_or_r.__dict__)
  927. if variable is not None:
  928. self._variable = variable
  929. return
  930. if r:
  931. c_or_r = poly(c_or_r)
  932. c_or_r = atleast_1d(c_or_r)
  933. if c_or_r.ndim > 1:
  934. raise ValueError("Polynomial must be 1d only.")
  935. c_or_r = trim_zeros(c_or_r, trim='f')
  936. if len(c_or_r) == 0:
  937. c_or_r = NX.array([0.])
  938. self._coeffs = c_or_r
  939. if variable is None:
  940. variable = 'x'
  941. self._variable = variable
  942. def __array__(self, t=None):
  943. if t:
  944. return NX.asarray(self.coeffs, t)
  945. else:
  946. return NX.asarray(self.coeffs)
  947. def __repr__(self):
  948. vals = repr(self.coeffs)
  949. vals = vals[6:-1]
  950. return "poly1d(%s)" % vals
  951. def __len__(self):
  952. return self.order
  953. def __str__(self):
  954. thestr = "0"
  955. var = self.variable
  956. # Remove leading zeros
  957. coeffs = self.coeffs[NX.logical_or.accumulate(self.coeffs != 0)]
  958. N = len(coeffs)-1
  959. def fmt_float(q):
  960. s = '%.4g' % q
  961. if s.endswith('.0000'):
  962. s = s[:-5]
  963. return s
  964. for k in range(len(coeffs)):
  965. if not iscomplex(coeffs[k]):
  966. coefstr = fmt_float(real(coeffs[k]))
  967. elif real(coeffs[k]) == 0:
  968. coefstr = '%sj' % fmt_float(imag(coeffs[k]))
  969. else:
  970. coefstr = '(%s + %sj)' % (fmt_float(real(coeffs[k])),
  971. fmt_float(imag(coeffs[k])))
  972. power = (N-k)
  973. if power == 0:
  974. if coefstr != '0':
  975. newstr = '%s' % (coefstr,)
  976. else:
  977. if k == 0:
  978. newstr = '0'
  979. else:
  980. newstr = ''
  981. elif power == 1:
  982. if coefstr == '0':
  983. newstr = ''
  984. elif coefstr == 'b':
  985. newstr = var
  986. else:
  987. newstr = '%s %s' % (coefstr, var)
  988. else:
  989. if coefstr == '0':
  990. newstr = ''
  991. elif coefstr == 'b':
  992. newstr = '%s**%d' % (var, power,)
  993. else:
  994. newstr = '%s %s**%d' % (coefstr, var, power)
  995. if k > 0:
  996. if newstr != '':
  997. if newstr.startswith('-'):
  998. thestr = "%s - %s" % (thestr, newstr[1:])
  999. else:
  1000. thestr = "%s + %s" % (thestr, newstr)
  1001. else:
  1002. thestr = newstr
  1003. return _raise_power(thestr)
  1004. def __call__(self, val):
  1005. return polyval(self.coeffs, val)
  1006. def __neg__(self):
  1007. return poly1d(-self.coeffs)
  1008. def __pos__(self):
  1009. return self
  1010. def __mul__(self, other):
  1011. if isscalar(other):
  1012. return poly1d(self.coeffs * other)
  1013. else:
  1014. other = poly1d(other)
  1015. return poly1d(polymul(self.coeffs, other.coeffs))
  1016. def __rmul__(self, other):
  1017. if isscalar(other):
  1018. return poly1d(other * self.coeffs)
  1019. else:
  1020. other = poly1d(other)
  1021. return poly1d(polymul(self.coeffs, other.coeffs))
  1022. def __add__(self, other):
  1023. other = poly1d(other)
  1024. return poly1d(polyadd(self.coeffs, other.coeffs))
  1025. def __radd__(self, other):
  1026. other = poly1d(other)
  1027. return poly1d(polyadd(self.coeffs, other.coeffs))
  1028. def __pow__(self, val):
  1029. if not isscalar(val) or int(val) != val or val < 0:
  1030. raise ValueError("Power to non-negative integers only.")
  1031. res = [1]
  1032. for _ in range(val):
  1033. res = polymul(self.coeffs, res)
  1034. return poly1d(res)
  1035. def __sub__(self, other):
  1036. other = poly1d(other)
  1037. return poly1d(polysub(self.coeffs, other.coeffs))
  1038. def __rsub__(self, other):
  1039. other = poly1d(other)
  1040. return poly1d(polysub(other.coeffs, self.coeffs))
  1041. def __div__(self, other):
  1042. if isscalar(other):
  1043. return poly1d(self.coeffs/other)
  1044. else:
  1045. other = poly1d(other)
  1046. return polydiv(self, other)
  1047. __truediv__ = __div__
  1048. def __rdiv__(self, other):
  1049. if isscalar(other):
  1050. return poly1d(other/self.coeffs)
  1051. else:
  1052. other = poly1d(other)
  1053. return polydiv(other, self)
  1054. __rtruediv__ = __rdiv__
  1055. def __eq__(self, other):
  1056. if not isinstance(other, poly1d):
  1057. return NotImplemented
  1058. if self.coeffs.shape != other.coeffs.shape:
  1059. return False
  1060. return (self.coeffs == other.coeffs).all()
  1061. def __ne__(self, other):
  1062. if not isinstance(other, poly1d):
  1063. return NotImplemented
  1064. return not self.__eq__(other)
  1065. def __getitem__(self, val):
  1066. ind = self.order - val
  1067. if val > self.order:
  1068. return 0
  1069. if val < 0:
  1070. return 0
  1071. return self.coeffs[ind]
  1072. def __setitem__(self, key, val):
  1073. ind = self.order - key
  1074. if key < 0:
  1075. raise ValueError("Does not support negative powers.")
  1076. if key > self.order:
  1077. zr = NX.zeros(key-self.order, self.coeffs.dtype)
  1078. self._coeffs = NX.concatenate((zr, self.coeffs))
  1079. ind = 0
  1080. self._coeffs[ind] = val
  1081. return
  1082. def __iter__(self):
  1083. return iter(self.coeffs)
  1084. def integ(self, m=1, k=0):
  1085. """
  1086. Return an antiderivative (indefinite integral) of this polynomial.
  1087. Refer to `polyint` for full documentation.
  1088. See Also
  1089. --------
  1090. polyint : equivalent function
  1091. """
  1092. return poly1d(polyint(self.coeffs, m=m, k=k))
  1093. def deriv(self, m=1):
  1094. """
  1095. Return a derivative of this polynomial.
  1096. Refer to `polyder` for full documentation.
  1097. See Also
  1098. --------
  1099. polyder : equivalent function
  1100. """
  1101. return poly1d(polyder(self.coeffs, m=m))
  1102. # Stuff to do on module import
  1103. warnings.simplefilter('always', RankWarning)