polynomial.py 43 KB

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