_polynomial_impl.py 43 KB

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