Add loan-advisor-agent (part 4)
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .gitattributes +7 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/chebyshev.py +2001 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/chebyshev.pyi +180 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/hermite.py +1738 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/hermite.pyi +106 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/hermite_e.py +1640 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/hermite_e.pyi +106 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/laguerre.py +1673 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/laguerre.pyi +100 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/legendre.py +1603 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/legendre.pyi +100 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/polynomial.py +1625 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/polynomial.pyi +109 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/polyutils.py +759 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/polyutils.pyi +307 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/tests/__init__.py +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/tests/test_chebyshev.py +618 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/tests/test_classes.py +613 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/tests/test_hermite.py +553 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/tests/test_hermite_e.py +554 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/tests/test_laguerre.py +535 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/tests/test_legendre.py +566 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/tests/test_polynomial.py +691 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/tests/test_polyutils.py +123 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/tests/test_printing.py +557 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/tests/test_symbol.py +217 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/py.typed +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/random/LICENSE.md +71 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/random/__init__.pxd +14 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/random/__init__.py +213 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/random/__init__.pyi +124 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/random/__pycache__/__init__.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/random/__pycache__/_pickle.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_bounded_integers.cp312-win_amd64.lib +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_bounded_integers.cp312-win_amd64.pyd +3 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_bounded_integers.pxd +38 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_bounded_integers.pyi +1 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_common.cp312-win_amd64.lib +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_common.cp312-win_amd64.pyd +3 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_common.pxd +110 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_common.pyi +16 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_examples/cffi/extending.py +44 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_examples/cffi/parse.py +53 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_examples/cython/extending.pyx +77 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_examples/cython/extending_distributions.pyx +117 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_examples/cython/meson.build +53 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_examples/numba/extending.py +86 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_examples/numba/extending_distributions.py +67 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_generator.cp312-win_amd64.lib +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_generator.cp312-win_amd64.pyd +3 -0
.gitattributes
CHANGED
|
@@ -454,3 +454,10 @@ loan-advisor-agent/.venv/Lib/site-packages/numpy/lib/__pycache__/_function_base_
|
|
| 454 |
loan-advisor-agent/.venv/Lib/site-packages/numpy/linalg/__pycache__/_linalg.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 455 |
loan-advisor-agent/.venv/Lib/site-packages/numpy/linalg/_umath_linalg.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 456 |
loan-advisor-agent/.venv/Lib/site-packages/numpy/ma/__pycache__/core.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 454 |
loan-advisor-agent/.venv/Lib/site-packages/numpy/linalg/__pycache__/_linalg.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 455 |
loan-advisor-agent/.venv/Lib/site-packages/numpy/linalg/_umath_linalg.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 456 |
loan-advisor-agent/.venv/Lib/site-packages/numpy/ma/__pycache__/core.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 457 |
+
loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_bounded_integers.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 458 |
+
loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_common.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 459 |
+
loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_generator.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 460 |
+
loan-advisor-agent/.venv/Lib/site-packages/numpy/random/bit_generator.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 461 |
+
loan-advisor-agent/.venv/Lib/site-packages/numpy/random/lib/npyrandom.lib filter=lfs diff=lfs merge=lfs -text
|
| 462 |
+
loan-advisor-agent/.venv/Lib/site-packages/numpy/random/mtrand.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 463 |
+
loan-advisor-agent/.venv/Lib/site-packages/numpy/testing/_private/__pycache__/utils.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/chebyshev.py
ADDED
|
@@ -0,0 +1,2001 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
====================================================
|
| 3 |
+
Chebyshev Series (:mod:`numpy.polynomial.chebyshev`)
|
| 4 |
+
====================================================
|
| 5 |
+
|
| 6 |
+
This module provides a number of objects (mostly functions) useful for
|
| 7 |
+
dealing with Chebyshev series, including a `Chebyshev` class that
|
| 8 |
+
encapsulates the usual arithmetic operations. (General information
|
| 9 |
+
on how this module represents and works with such polynomials is in the
|
| 10 |
+
docstring for its "parent" sub-package, `numpy.polynomial`).
|
| 11 |
+
|
| 12 |
+
Classes
|
| 13 |
+
-------
|
| 14 |
+
|
| 15 |
+
.. autosummary::
|
| 16 |
+
:toctree: generated/
|
| 17 |
+
|
| 18 |
+
Chebyshev
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
Constants
|
| 22 |
+
---------
|
| 23 |
+
|
| 24 |
+
.. autosummary::
|
| 25 |
+
:toctree: generated/
|
| 26 |
+
|
| 27 |
+
chebdomain
|
| 28 |
+
chebzero
|
| 29 |
+
chebone
|
| 30 |
+
chebx
|
| 31 |
+
|
| 32 |
+
Arithmetic
|
| 33 |
+
----------
|
| 34 |
+
|
| 35 |
+
.. autosummary::
|
| 36 |
+
:toctree: generated/
|
| 37 |
+
|
| 38 |
+
chebadd
|
| 39 |
+
chebsub
|
| 40 |
+
chebmulx
|
| 41 |
+
chebmul
|
| 42 |
+
chebdiv
|
| 43 |
+
chebpow
|
| 44 |
+
chebval
|
| 45 |
+
chebval2d
|
| 46 |
+
chebval3d
|
| 47 |
+
chebgrid2d
|
| 48 |
+
chebgrid3d
|
| 49 |
+
|
| 50 |
+
Calculus
|
| 51 |
+
--------
|
| 52 |
+
|
| 53 |
+
.. autosummary::
|
| 54 |
+
:toctree: generated/
|
| 55 |
+
|
| 56 |
+
chebder
|
| 57 |
+
chebint
|
| 58 |
+
|
| 59 |
+
Misc Functions
|
| 60 |
+
--------------
|
| 61 |
+
|
| 62 |
+
.. autosummary::
|
| 63 |
+
:toctree: generated/
|
| 64 |
+
|
| 65 |
+
chebfromroots
|
| 66 |
+
chebroots
|
| 67 |
+
chebvander
|
| 68 |
+
chebvander2d
|
| 69 |
+
chebvander3d
|
| 70 |
+
chebgauss
|
| 71 |
+
chebweight
|
| 72 |
+
chebcompanion
|
| 73 |
+
chebfit
|
| 74 |
+
chebpts1
|
| 75 |
+
chebpts2
|
| 76 |
+
chebtrim
|
| 77 |
+
chebline
|
| 78 |
+
cheb2poly
|
| 79 |
+
poly2cheb
|
| 80 |
+
chebinterpolate
|
| 81 |
+
|
| 82 |
+
See also
|
| 83 |
+
--------
|
| 84 |
+
`numpy.polynomial`
|
| 85 |
+
|
| 86 |
+
Notes
|
| 87 |
+
-----
|
| 88 |
+
The implementations of multiplication, division, integration, and
|
| 89 |
+
differentiation use the algebraic identities [1]_:
|
| 90 |
+
|
| 91 |
+
.. math::
|
| 92 |
+
T_n(x) = \\frac{z^n + z^{-n}}{2} \\\\
|
| 93 |
+
z\\frac{dx}{dz} = \\frac{z - z^{-1}}{2}.
|
| 94 |
+
|
| 95 |
+
where
|
| 96 |
+
|
| 97 |
+
.. math:: x = \\frac{z + z^{-1}}{2}.
|
| 98 |
+
|
| 99 |
+
These identities allow a Chebyshev series to be expressed as a finite,
|
| 100 |
+
symmetric Laurent series. In this module, this sort of Laurent series
|
| 101 |
+
is referred to as a "z-series."
|
| 102 |
+
|
| 103 |
+
References
|
| 104 |
+
----------
|
| 105 |
+
.. [1] A. T. Benjamin, et al., "Combinatorial Trigonometry with Chebyshev
|
| 106 |
+
Polynomials," *Journal of Statistical Planning and Inference 14*, 2008
|
| 107 |
+
(https://web.archive.org/web/20080221202153/https://www.math.hmc.edu/~benjamin/papers/CombTrig.pdf, pg. 4)
|
| 108 |
+
|
| 109 |
+
""" # noqa: E501
|
| 110 |
+
import numpy as np
|
| 111 |
+
|
| 112 |
+
from . import polyutils as pu
|
| 113 |
+
from ._polybase import ABCPolyBase
|
| 114 |
+
|
| 115 |
+
__all__ = [
|
| 116 |
+
'chebzero', 'chebone', 'chebx', 'chebdomain', 'chebline', 'chebadd',
|
| 117 |
+
'chebsub', 'chebmulx', 'chebmul', 'chebdiv', 'chebpow', 'chebval',
|
| 118 |
+
'chebder', 'chebint', 'cheb2poly', 'poly2cheb', 'chebfromroots',
|
| 119 |
+
'chebvander', 'chebfit', 'chebtrim', 'chebroots', 'chebpts1',
|
| 120 |
+
'chebpts2', 'Chebyshev', 'chebval2d', 'chebval3d', 'chebgrid2d',
|
| 121 |
+
'chebgrid3d', 'chebvander2d', 'chebvander3d', 'chebcompanion',
|
| 122 |
+
'chebgauss', 'chebweight', 'chebinterpolate']
|
| 123 |
+
|
| 124 |
+
chebtrim = pu.trimcoef
|
| 125 |
+
|
| 126 |
+
#
|
| 127 |
+
# A collection of functions for manipulating z-series. These are private
|
| 128 |
+
# functions and do minimal error checking.
|
| 129 |
+
#
|
| 130 |
+
|
| 131 |
+
def _cseries_to_zseries(c):
|
| 132 |
+
"""Convert Chebyshev series to z-series.
|
| 133 |
+
|
| 134 |
+
Convert a Chebyshev series to the equivalent z-series. The result is
|
| 135 |
+
never an empty array. The dtype of the return is the same as that of
|
| 136 |
+
the input. No checks are run on the arguments as this routine is for
|
| 137 |
+
internal use.
|
| 138 |
+
|
| 139 |
+
Parameters
|
| 140 |
+
----------
|
| 141 |
+
c : 1-D ndarray
|
| 142 |
+
Chebyshev coefficients, ordered from low to high
|
| 143 |
+
|
| 144 |
+
Returns
|
| 145 |
+
-------
|
| 146 |
+
zs : 1-D ndarray
|
| 147 |
+
Odd length symmetric z-series, ordered from low to high.
|
| 148 |
+
|
| 149 |
+
"""
|
| 150 |
+
n = c.size
|
| 151 |
+
zs = np.zeros(2 * n - 1, dtype=c.dtype)
|
| 152 |
+
zs[n - 1:] = c / 2
|
| 153 |
+
return zs + zs[::-1]
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
def _zseries_to_cseries(zs):
|
| 157 |
+
"""Convert z-series to a Chebyshev series.
|
| 158 |
+
|
| 159 |
+
Convert a z series to the equivalent Chebyshev series. The result is
|
| 160 |
+
never an empty array. The dtype of the return is the same as that of
|
| 161 |
+
the input. No checks are run on the arguments as this routine is for
|
| 162 |
+
internal use.
|
| 163 |
+
|
| 164 |
+
Parameters
|
| 165 |
+
----------
|
| 166 |
+
zs : 1-D ndarray
|
| 167 |
+
Odd length symmetric z-series, ordered from low to high.
|
| 168 |
+
|
| 169 |
+
Returns
|
| 170 |
+
-------
|
| 171 |
+
c : 1-D ndarray
|
| 172 |
+
Chebyshev coefficients, ordered from low to high.
|
| 173 |
+
|
| 174 |
+
"""
|
| 175 |
+
n = (zs.size + 1) // 2
|
| 176 |
+
c = zs[n - 1:].copy()
|
| 177 |
+
c[1:n] *= 2
|
| 178 |
+
return c
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
def _zseries_mul(z1, z2):
|
| 182 |
+
"""Multiply two z-series.
|
| 183 |
+
|
| 184 |
+
Multiply two z-series to produce a z-series.
|
| 185 |
+
|
| 186 |
+
Parameters
|
| 187 |
+
----------
|
| 188 |
+
z1, z2 : 1-D ndarray
|
| 189 |
+
The arrays must be 1-D but this is not checked.
|
| 190 |
+
|
| 191 |
+
Returns
|
| 192 |
+
-------
|
| 193 |
+
product : 1-D ndarray
|
| 194 |
+
The product z-series.
|
| 195 |
+
|
| 196 |
+
Notes
|
| 197 |
+
-----
|
| 198 |
+
This is simply convolution. If symmetric/anti-symmetric z-series are
|
| 199 |
+
denoted by S/A then the following rules apply:
|
| 200 |
+
|
| 201 |
+
S*S, A*A -> S
|
| 202 |
+
S*A, A*S -> A
|
| 203 |
+
|
| 204 |
+
"""
|
| 205 |
+
return np.convolve(z1, z2)
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
def _zseries_div(z1, z2):
|
| 209 |
+
"""Divide the first z-series by the second.
|
| 210 |
+
|
| 211 |
+
Divide `z1` by `z2` and return the quotient and remainder as z-series.
|
| 212 |
+
Warning: this implementation only applies when both z1 and z2 have the
|
| 213 |
+
same symmetry, which is sufficient for present purposes.
|
| 214 |
+
|
| 215 |
+
Parameters
|
| 216 |
+
----------
|
| 217 |
+
z1, z2 : 1-D ndarray
|
| 218 |
+
The arrays must be 1-D and have the same symmetry, but this is not
|
| 219 |
+
checked.
|
| 220 |
+
|
| 221 |
+
Returns
|
| 222 |
+
-------
|
| 223 |
+
|
| 224 |
+
(quotient, remainder) : 1-D ndarrays
|
| 225 |
+
Quotient and remainder as z-series.
|
| 226 |
+
|
| 227 |
+
Notes
|
| 228 |
+
-----
|
| 229 |
+
This is not the same as polynomial division on account of the desired form
|
| 230 |
+
of the remainder. If symmetric/anti-symmetric z-series are denoted by S/A
|
| 231 |
+
then the following rules apply:
|
| 232 |
+
|
| 233 |
+
S/S -> S,S
|
| 234 |
+
A/A -> S,A
|
| 235 |
+
|
| 236 |
+
The restriction to types of the same symmetry could be fixed but seems like
|
| 237 |
+
unneeded generality. There is no natural form for the remainder in the case
|
| 238 |
+
where there is no symmetry.
|
| 239 |
+
|
| 240 |
+
"""
|
| 241 |
+
z1 = z1.copy()
|
| 242 |
+
z2 = z2.copy()
|
| 243 |
+
lc1 = len(z1)
|
| 244 |
+
lc2 = len(z2)
|
| 245 |
+
if lc2 == 1:
|
| 246 |
+
z1 /= z2
|
| 247 |
+
return z1, z1[:1] * 0
|
| 248 |
+
elif lc1 < lc2:
|
| 249 |
+
return z1[:1] * 0, z1
|
| 250 |
+
else:
|
| 251 |
+
dlen = lc1 - lc2
|
| 252 |
+
scl = z2[0]
|
| 253 |
+
z2 /= scl
|
| 254 |
+
quo = np.empty(dlen + 1, dtype=z1.dtype)
|
| 255 |
+
i = 0
|
| 256 |
+
j = dlen
|
| 257 |
+
while i < j:
|
| 258 |
+
r = z1[i]
|
| 259 |
+
quo[i] = z1[i]
|
| 260 |
+
quo[dlen - i] = r
|
| 261 |
+
tmp = r * z2
|
| 262 |
+
z1[i:i + lc2] -= tmp
|
| 263 |
+
z1[j:j + lc2] -= tmp
|
| 264 |
+
i += 1
|
| 265 |
+
j -= 1
|
| 266 |
+
r = z1[i]
|
| 267 |
+
quo[i] = r
|
| 268 |
+
tmp = r * z2
|
| 269 |
+
z1[i:i + lc2] -= tmp
|
| 270 |
+
quo /= scl
|
| 271 |
+
rem = z1[i + 1:i - 1 + lc2].copy()
|
| 272 |
+
return quo, rem
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
def _zseries_der(zs):
|
| 276 |
+
"""Differentiate a z-series.
|
| 277 |
+
|
| 278 |
+
The derivative is with respect to x, not z. This is achieved using the
|
| 279 |
+
chain rule and the value of dx/dz given in the module notes.
|
| 280 |
+
|
| 281 |
+
Parameters
|
| 282 |
+
----------
|
| 283 |
+
zs : z-series
|
| 284 |
+
The z-series to differentiate.
|
| 285 |
+
|
| 286 |
+
Returns
|
| 287 |
+
-------
|
| 288 |
+
derivative : z-series
|
| 289 |
+
The derivative
|
| 290 |
+
|
| 291 |
+
Notes
|
| 292 |
+
-----
|
| 293 |
+
The zseries for x (ns) has been multiplied by two in order to avoid
|
| 294 |
+
using floats that are incompatible with Decimal and likely other
|
| 295 |
+
specialized scalar types. This scaling has been compensated by
|
| 296 |
+
multiplying the value of zs by two also so that the two cancels in the
|
| 297 |
+
division.
|
| 298 |
+
|
| 299 |
+
"""
|
| 300 |
+
n = len(zs) // 2
|
| 301 |
+
ns = np.array([-1, 0, 1], dtype=zs.dtype)
|
| 302 |
+
zs *= np.arange(-n, n + 1) * 2
|
| 303 |
+
d, r = _zseries_div(zs, ns)
|
| 304 |
+
return d
|
| 305 |
+
|
| 306 |
+
|
| 307 |
+
def _zseries_int(zs):
|
| 308 |
+
"""Integrate a z-series.
|
| 309 |
+
|
| 310 |
+
The integral is with respect to x, not z. This is achieved by a change
|
| 311 |
+
of variable using dx/dz given in the module notes.
|
| 312 |
+
|
| 313 |
+
Parameters
|
| 314 |
+
----------
|
| 315 |
+
zs : z-series
|
| 316 |
+
The z-series to integrate
|
| 317 |
+
|
| 318 |
+
Returns
|
| 319 |
+
-------
|
| 320 |
+
integral : z-series
|
| 321 |
+
The indefinite integral
|
| 322 |
+
|
| 323 |
+
Notes
|
| 324 |
+
-----
|
| 325 |
+
The zseries for x (ns) has been multiplied by two in order to avoid
|
| 326 |
+
using floats that are incompatible with Decimal and likely other
|
| 327 |
+
specialized scalar types. This scaling has been compensated by
|
| 328 |
+
dividing the resulting zs by two.
|
| 329 |
+
|
| 330 |
+
"""
|
| 331 |
+
n = 1 + len(zs) // 2
|
| 332 |
+
ns = np.array([-1, 0, 1], dtype=zs.dtype)
|
| 333 |
+
zs = _zseries_mul(zs, ns)
|
| 334 |
+
div = np.arange(-n, n + 1) * 2
|
| 335 |
+
zs[:n] /= div[:n]
|
| 336 |
+
zs[n + 1:] /= div[n + 1:]
|
| 337 |
+
zs[n] = 0
|
| 338 |
+
return zs
|
| 339 |
+
|
| 340 |
+
#
|
| 341 |
+
# Chebyshev series functions
|
| 342 |
+
#
|
| 343 |
+
|
| 344 |
+
|
| 345 |
+
def poly2cheb(pol):
|
| 346 |
+
"""
|
| 347 |
+
Convert a polynomial to a Chebyshev series.
|
| 348 |
+
|
| 349 |
+
Convert an array representing the coefficients of a polynomial (relative
|
| 350 |
+
to the "standard" basis) ordered from lowest degree to highest, to an
|
| 351 |
+
array of the coefficients of the equivalent Chebyshev series, ordered
|
| 352 |
+
from lowest to highest degree.
|
| 353 |
+
|
| 354 |
+
Parameters
|
| 355 |
+
----------
|
| 356 |
+
pol : array_like
|
| 357 |
+
1-D array containing the polynomial coefficients
|
| 358 |
+
|
| 359 |
+
Returns
|
| 360 |
+
-------
|
| 361 |
+
c : ndarray
|
| 362 |
+
1-D array containing the coefficients of the equivalent Chebyshev
|
| 363 |
+
series.
|
| 364 |
+
|
| 365 |
+
See Also
|
| 366 |
+
--------
|
| 367 |
+
cheb2poly
|
| 368 |
+
|
| 369 |
+
Notes
|
| 370 |
+
-----
|
| 371 |
+
The easy way to do conversions between polynomial basis sets
|
| 372 |
+
is to use the convert method of a class instance.
|
| 373 |
+
|
| 374 |
+
Examples
|
| 375 |
+
--------
|
| 376 |
+
>>> from numpy import polynomial as P
|
| 377 |
+
>>> p = P.Polynomial(range(4))
|
| 378 |
+
>>> p
|
| 379 |
+
Polynomial([0., 1., 2., 3.], domain=[-1., 1.], window=[-1., 1.], symbol='x')
|
| 380 |
+
>>> c = p.convert(kind=P.Chebyshev)
|
| 381 |
+
>>> c
|
| 382 |
+
Chebyshev([1. , 3.25, 1. , 0.75], domain=[-1., 1.], window=[-1., ...
|
| 383 |
+
>>> P.chebyshev.poly2cheb(range(4))
|
| 384 |
+
array([1. , 3.25, 1. , 0.75])
|
| 385 |
+
|
| 386 |
+
"""
|
| 387 |
+
[pol] = pu.as_series([pol])
|
| 388 |
+
deg = len(pol) - 1
|
| 389 |
+
res = 0
|
| 390 |
+
for i in range(deg, -1, -1):
|
| 391 |
+
res = chebadd(chebmulx(res), pol[i])
|
| 392 |
+
return res
|
| 393 |
+
|
| 394 |
+
|
| 395 |
+
def cheb2poly(c):
|
| 396 |
+
"""
|
| 397 |
+
Convert a Chebyshev series to a polynomial.
|
| 398 |
+
|
| 399 |
+
Convert an array representing the coefficients of a Chebyshev series,
|
| 400 |
+
ordered from lowest degree to highest, to an array of the coefficients
|
| 401 |
+
of the equivalent polynomial (relative to the "standard" basis) ordered
|
| 402 |
+
from lowest to highest degree.
|
| 403 |
+
|
| 404 |
+
Parameters
|
| 405 |
+
----------
|
| 406 |
+
c : array_like
|
| 407 |
+
1-D array containing the Chebyshev series coefficients, ordered
|
| 408 |
+
from lowest order term to highest.
|
| 409 |
+
|
| 410 |
+
Returns
|
| 411 |
+
-------
|
| 412 |
+
pol : ndarray
|
| 413 |
+
1-D array containing the coefficients of the equivalent polynomial
|
| 414 |
+
(relative to the "standard" basis) ordered from lowest order term
|
| 415 |
+
to highest.
|
| 416 |
+
|
| 417 |
+
See Also
|
| 418 |
+
--------
|
| 419 |
+
poly2cheb
|
| 420 |
+
|
| 421 |
+
Notes
|
| 422 |
+
-----
|
| 423 |
+
The easy way to do conversions between polynomial basis sets
|
| 424 |
+
is to use the convert method of a class instance.
|
| 425 |
+
|
| 426 |
+
Examples
|
| 427 |
+
--------
|
| 428 |
+
>>> from numpy import polynomial as P
|
| 429 |
+
>>> c = P.Chebyshev(range(4))
|
| 430 |
+
>>> c
|
| 431 |
+
Chebyshev([0., 1., 2., 3.], domain=[-1., 1.], window=[-1., 1.], symbol='x')
|
| 432 |
+
>>> p = c.convert(kind=P.Polynomial)
|
| 433 |
+
>>> p
|
| 434 |
+
Polynomial([-2., -8., 4., 12.], domain=[-1., 1.], window=[-1., 1.], ...
|
| 435 |
+
>>> P.chebyshev.cheb2poly(range(4))
|
| 436 |
+
array([-2., -8., 4., 12.])
|
| 437 |
+
|
| 438 |
+
"""
|
| 439 |
+
from .polynomial import polyadd, polymulx, polysub
|
| 440 |
+
|
| 441 |
+
[c] = pu.as_series([c])
|
| 442 |
+
n = len(c)
|
| 443 |
+
if n < 3:
|
| 444 |
+
return c
|
| 445 |
+
else:
|
| 446 |
+
c0 = c[-2]
|
| 447 |
+
c1 = c[-1]
|
| 448 |
+
# i is the current degree of c1
|
| 449 |
+
for i in range(n - 1, 1, -1):
|
| 450 |
+
tmp = c0
|
| 451 |
+
c0 = polysub(c[i - 2], c1)
|
| 452 |
+
c1 = polyadd(tmp, polymulx(c1) * 2)
|
| 453 |
+
return polyadd(c0, polymulx(c1))
|
| 454 |
+
|
| 455 |
+
|
| 456 |
+
#
|
| 457 |
+
# These are constant arrays are of integer type so as to be compatible
|
| 458 |
+
# with the widest range of other types, such as Decimal.
|
| 459 |
+
#
|
| 460 |
+
|
| 461 |
+
# Chebyshev default domain.
|
| 462 |
+
chebdomain = np.array([-1., 1.])
|
| 463 |
+
|
| 464 |
+
# Chebyshev coefficients representing zero.
|
| 465 |
+
chebzero = np.array([0])
|
| 466 |
+
|
| 467 |
+
# Chebyshev coefficients representing one.
|
| 468 |
+
chebone = np.array([1])
|
| 469 |
+
|
| 470 |
+
# Chebyshev coefficients representing the identity x.
|
| 471 |
+
chebx = np.array([0, 1])
|
| 472 |
+
|
| 473 |
+
|
| 474 |
+
def chebline(off, scl):
|
| 475 |
+
"""
|
| 476 |
+
Chebyshev series whose graph is a straight line.
|
| 477 |
+
|
| 478 |
+
Parameters
|
| 479 |
+
----------
|
| 480 |
+
off, scl : scalars
|
| 481 |
+
The specified line is given by ``off + scl*x``.
|
| 482 |
+
|
| 483 |
+
Returns
|
| 484 |
+
-------
|
| 485 |
+
y : ndarray
|
| 486 |
+
This module's representation of the Chebyshev series for
|
| 487 |
+
``off + scl*x``.
|
| 488 |
+
|
| 489 |
+
See Also
|
| 490 |
+
--------
|
| 491 |
+
numpy.polynomial.polynomial.polyline
|
| 492 |
+
numpy.polynomial.legendre.legline
|
| 493 |
+
numpy.polynomial.laguerre.lagline
|
| 494 |
+
numpy.polynomial.hermite.hermline
|
| 495 |
+
numpy.polynomial.hermite_e.hermeline
|
| 496 |
+
|
| 497 |
+
Examples
|
| 498 |
+
--------
|
| 499 |
+
>>> import numpy.polynomial.chebyshev as C
|
| 500 |
+
>>> C.chebline(3,2)
|
| 501 |
+
array([3, 2])
|
| 502 |
+
>>> C.chebval(-3, C.chebline(3,2)) # should be -3
|
| 503 |
+
-3.0
|
| 504 |
+
|
| 505 |
+
"""
|
| 506 |
+
if scl != 0:
|
| 507 |
+
return np.array([off, scl])
|
| 508 |
+
else:
|
| 509 |
+
return np.array([off])
|
| 510 |
+
|
| 511 |
+
|
| 512 |
+
def chebfromroots(roots):
|
| 513 |
+
"""
|
| 514 |
+
Generate a Chebyshev series with given roots.
|
| 515 |
+
|
| 516 |
+
The function returns the coefficients of the polynomial
|
| 517 |
+
|
| 518 |
+
.. math:: p(x) = (x - r_0) * (x - r_1) * ... * (x - r_n),
|
| 519 |
+
|
| 520 |
+
in Chebyshev form, where the :math:`r_n` are the roots specified in
|
| 521 |
+
`roots`. If a zero has multiplicity n, then it must appear in `roots`
|
| 522 |
+
n times. For instance, if 2 is a root of multiplicity three and 3 is a
|
| 523 |
+
root of multiplicity 2, then `roots` looks something like [2, 2, 2, 3, 3].
|
| 524 |
+
The roots can appear in any order.
|
| 525 |
+
|
| 526 |
+
If the returned coefficients are `c`, then
|
| 527 |
+
|
| 528 |
+
.. math:: p(x) = c_0 + c_1 * T_1(x) + ... + c_n * T_n(x)
|
| 529 |
+
|
| 530 |
+
The coefficient of the last term is not generally 1 for monic
|
| 531 |
+
polynomials in Chebyshev form.
|
| 532 |
+
|
| 533 |
+
Parameters
|
| 534 |
+
----------
|
| 535 |
+
roots : array_like
|
| 536 |
+
Sequence containing the roots.
|
| 537 |
+
|
| 538 |
+
Returns
|
| 539 |
+
-------
|
| 540 |
+
out : ndarray
|
| 541 |
+
1-D array of coefficients. If all roots are real then `out` is a
|
| 542 |
+
real array, if some of the roots are complex, then `out` is complex
|
| 543 |
+
even if all the coefficients in the result are real (see Examples
|
| 544 |
+
below).
|
| 545 |
+
|
| 546 |
+
See Also
|
| 547 |
+
--------
|
| 548 |
+
numpy.polynomial.polynomial.polyfromroots
|
| 549 |
+
numpy.polynomial.legendre.legfromroots
|
| 550 |
+
numpy.polynomial.laguerre.lagfromroots
|
| 551 |
+
numpy.polynomial.hermite.hermfromroots
|
| 552 |
+
numpy.polynomial.hermite_e.hermefromroots
|
| 553 |
+
|
| 554 |
+
Examples
|
| 555 |
+
--------
|
| 556 |
+
>>> import numpy.polynomial.chebyshev as C
|
| 557 |
+
>>> C.chebfromroots((-1,0,1)) # x^3 - x relative to the standard basis
|
| 558 |
+
array([ 0. , -0.25, 0. , 0.25])
|
| 559 |
+
>>> j = complex(0,1)
|
| 560 |
+
>>> C.chebfromroots((-j,j)) # x^2 + 1 relative to the standard basis
|
| 561 |
+
array([1.5+0.j, 0. +0.j, 0.5+0.j])
|
| 562 |
+
|
| 563 |
+
"""
|
| 564 |
+
return pu._fromroots(chebline, chebmul, roots)
|
| 565 |
+
|
| 566 |
+
|
| 567 |
+
def chebadd(c1, c2):
|
| 568 |
+
"""
|
| 569 |
+
Add one Chebyshev series to another.
|
| 570 |
+
|
| 571 |
+
Returns the sum of two Chebyshev series `c1` + `c2`. The arguments
|
| 572 |
+
are sequences of coefficients ordered from lowest order term to
|
| 573 |
+
highest, i.e., [1,2,3] represents the series ``T_0 + 2*T_1 + 3*T_2``.
|
| 574 |
+
|
| 575 |
+
Parameters
|
| 576 |
+
----------
|
| 577 |
+
c1, c2 : array_like
|
| 578 |
+
1-D arrays of Chebyshev series coefficients ordered from low to
|
| 579 |
+
high.
|
| 580 |
+
|
| 581 |
+
Returns
|
| 582 |
+
-------
|
| 583 |
+
out : ndarray
|
| 584 |
+
Array representing the Chebyshev series of their sum.
|
| 585 |
+
|
| 586 |
+
See Also
|
| 587 |
+
--------
|
| 588 |
+
chebsub, chebmulx, chebmul, chebdiv, chebpow
|
| 589 |
+
|
| 590 |
+
Notes
|
| 591 |
+
-----
|
| 592 |
+
Unlike multiplication, division, etc., the sum of two Chebyshev series
|
| 593 |
+
is a Chebyshev series (without having to "reproject" the result onto
|
| 594 |
+
the basis set) so addition, just like that of "standard" polynomials,
|
| 595 |
+
is simply "component-wise."
|
| 596 |
+
|
| 597 |
+
Examples
|
| 598 |
+
--------
|
| 599 |
+
>>> from numpy.polynomial import chebyshev as C
|
| 600 |
+
>>> c1 = (1,2,3)
|
| 601 |
+
>>> c2 = (3,2,1)
|
| 602 |
+
>>> C.chebadd(c1,c2)
|
| 603 |
+
array([4., 4., 4.])
|
| 604 |
+
|
| 605 |
+
"""
|
| 606 |
+
return pu._add(c1, c2)
|
| 607 |
+
|
| 608 |
+
|
| 609 |
+
def chebsub(c1, c2):
|
| 610 |
+
"""
|
| 611 |
+
Subtract one Chebyshev series from another.
|
| 612 |
+
|
| 613 |
+
Returns the difference of two Chebyshev series `c1` - `c2`. The
|
| 614 |
+
sequences of coefficients are from lowest order term to highest, i.e.,
|
| 615 |
+
[1,2,3] represents the series ``T_0 + 2*T_1 + 3*T_2``.
|
| 616 |
+
|
| 617 |
+
Parameters
|
| 618 |
+
----------
|
| 619 |
+
c1, c2 : array_like
|
| 620 |
+
1-D arrays of Chebyshev series coefficients ordered from low to
|
| 621 |
+
high.
|
| 622 |
+
|
| 623 |
+
Returns
|
| 624 |
+
-------
|
| 625 |
+
out : ndarray
|
| 626 |
+
Of Chebyshev series coefficients representing their difference.
|
| 627 |
+
|
| 628 |
+
See Also
|
| 629 |
+
--------
|
| 630 |
+
chebadd, chebmulx, chebmul, chebdiv, chebpow
|
| 631 |
+
|
| 632 |
+
Notes
|
| 633 |
+
-----
|
| 634 |
+
Unlike multiplication, division, etc., the difference of two Chebyshev
|
| 635 |
+
series is a Chebyshev series (without having to "reproject" the result
|
| 636 |
+
onto the basis set) so subtraction, just like that of "standard"
|
| 637 |
+
polynomials, is simply "component-wise."
|
| 638 |
+
|
| 639 |
+
Examples
|
| 640 |
+
--------
|
| 641 |
+
>>> from numpy.polynomial import chebyshev as C
|
| 642 |
+
>>> c1 = (1,2,3)
|
| 643 |
+
>>> c2 = (3,2,1)
|
| 644 |
+
>>> C.chebsub(c1,c2)
|
| 645 |
+
array([-2., 0., 2.])
|
| 646 |
+
>>> C.chebsub(c2,c1) # -C.chebsub(c1,c2)
|
| 647 |
+
array([ 2., 0., -2.])
|
| 648 |
+
|
| 649 |
+
"""
|
| 650 |
+
return pu._sub(c1, c2)
|
| 651 |
+
|
| 652 |
+
|
| 653 |
+
def chebmulx(c):
|
| 654 |
+
"""Multiply a Chebyshev series by x.
|
| 655 |
+
|
| 656 |
+
Multiply the polynomial `c` by x, where x is the independent
|
| 657 |
+
variable.
|
| 658 |
+
|
| 659 |
+
|
| 660 |
+
Parameters
|
| 661 |
+
----------
|
| 662 |
+
c : array_like
|
| 663 |
+
1-D array of Chebyshev series coefficients ordered from low to
|
| 664 |
+
high.
|
| 665 |
+
|
| 666 |
+
Returns
|
| 667 |
+
-------
|
| 668 |
+
out : ndarray
|
| 669 |
+
Array representing the result of the multiplication.
|
| 670 |
+
|
| 671 |
+
See Also
|
| 672 |
+
--------
|
| 673 |
+
chebadd, chebsub, chebmul, chebdiv, chebpow
|
| 674 |
+
|
| 675 |
+
Examples
|
| 676 |
+
--------
|
| 677 |
+
>>> from numpy.polynomial import chebyshev as C
|
| 678 |
+
>>> C.chebmulx([1,2,3])
|
| 679 |
+
array([1. , 2.5, 1. , 1.5])
|
| 680 |
+
|
| 681 |
+
"""
|
| 682 |
+
# c is a trimmed copy
|
| 683 |
+
[c] = pu.as_series([c])
|
| 684 |
+
# The zero series needs special treatment
|
| 685 |
+
if len(c) == 1 and c[0] == 0:
|
| 686 |
+
return c
|
| 687 |
+
|
| 688 |
+
prd = np.empty(len(c) + 1, dtype=c.dtype)
|
| 689 |
+
prd[0] = c[0] * 0
|
| 690 |
+
prd[1] = c[0]
|
| 691 |
+
if len(c) > 1:
|
| 692 |
+
tmp = c[1:] / 2
|
| 693 |
+
prd[2:] = tmp
|
| 694 |
+
prd[0:-2] += tmp
|
| 695 |
+
return prd
|
| 696 |
+
|
| 697 |
+
|
| 698 |
+
def chebmul(c1, c2):
|
| 699 |
+
"""
|
| 700 |
+
Multiply one Chebyshev series by another.
|
| 701 |
+
|
| 702 |
+
Returns the product of two Chebyshev series `c1` * `c2`. The arguments
|
| 703 |
+
are sequences of coefficients, from lowest order "term" to highest,
|
| 704 |
+
e.g., [1,2,3] represents the series ``T_0 + 2*T_1 + 3*T_2``.
|
| 705 |
+
|
| 706 |
+
Parameters
|
| 707 |
+
----------
|
| 708 |
+
c1, c2 : array_like
|
| 709 |
+
1-D arrays of Chebyshev series coefficients ordered from low to
|
| 710 |
+
high.
|
| 711 |
+
|
| 712 |
+
Returns
|
| 713 |
+
-------
|
| 714 |
+
out : ndarray
|
| 715 |
+
Of Chebyshev series coefficients representing their product.
|
| 716 |
+
|
| 717 |
+
See Also
|
| 718 |
+
--------
|
| 719 |
+
chebadd, chebsub, chebmulx, chebdiv, chebpow
|
| 720 |
+
|
| 721 |
+
Notes
|
| 722 |
+
-----
|
| 723 |
+
In general, the (polynomial) product of two C-series results in terms
|
| 724 |
+
that are not in the Chebyshev polynomial basis set. Thus, to express
|
| 725 |
+
the product as a C-series, it is typically necessary to "reproject"
|
| 726 |
+
the product onto said basis set, which typically produces
|
| 727 |
+
"unintuitive live" (but correct) results; see Examples section below.
|
| 728 |
+
|
| 729 |
+
Examples
|
| 730 |
+
--------
|
| 731 |
+
>>> from numpy.polynomial import chebyshev as C
|
| 732 |
+
>>> c1 = (1,2,3)
|
| 733 |
+
>>> c2 = (3,2,1)
|
| 734 |
+
>>> C.chebmul(c1,c2) # multiplication requires "reprojection"
|
| 735 |
+
array([ 6.5, 12. , 12. , 4. , 1.5])
|
| 736 |
+
|
| 737 |
+
"""
|
| 738 |
+
# c1, c2 are trimmed copies
|
| 739 |
+
[c1, c2] = pu.as_series([c1, c2])
|
| 740 |
+
z1 = _cseries_to_zseries(c1)
|
| 741 |
+
z2 = _cseries_to_zseries(c2)
|
| 742 |
+
prd = _zseries_mul(z1, z2)
|
| 743 |
+
ret = _zseries_to_cseries(prd)
|
| 744 |
+
return pu.trimseq(ret)
|
| 745 |
+
|
| 746 |
+
|
| 747 |
+
def chebdiv(c1, c2):
|
| 748 |
+
"""
|
| 749 |
+
Divide one Chebyshev series by another.
|
| 750 |
+
|
| 751 |
+
Returns the quotient-with-remainder of two Chebyshev series
|
| 752 |
+
`c1` / `c2`. The arguments are sequences of coefficients from lowest
|
| 753 |
+
order "term" to highest, e.g., [1,2,3] represents the series
|
| 754 |
+
``T_0 + 2*T_1 + 3*T_2``.
|
| 755 |
+
|
| 756 |
+
Parameters
|
| 757 |
+
----------
|
| 758 |
+
c1, c2 : array_like
|
| 759 |
+
1-D arrays of Chebyshev series coefficients ordered from low to
|
| 760 |
+
high.
|
| 761 |
+
|
| 762 |
+
Returns
|
| 763 |
+
-------
|
| 764 |
+
[quo, rem] : ndarrays
|
| 765 |
+
Of Chebyshev series coefficients representing the quotient and
|
| 766 |
+
remainder.
|
| 767 |
+
|
| 768 |
+
See Also
|
| 769 |
+
--------
|
| 770 |
+
chebadd, chebsub, chebmulx, chebmul, chebpow
|
| 771 |
+
|
| 772 |
+
Notes
|
| 773 |
+
-----
|
| 774 |
+
In general, the (polynomial) division of one C-series by another
|
| 775 |
+
results in quotient and remainder terms that are not in the Chebyshev
|
| 776 |
+
polynomial basis set. Thus, to express these results as C-series, it
|
| 777 |
+
is typically necessary to "reproject" the results onto said basis
|
| 778 |
+
set, which typically produces "unintuitive" (but correct) results;
|
| 779 |
+
see Examples section below.
|
| 780 |
+
|
| 781 |
+
Examples
|
| 782 |
+
--------
|
| 783 |
+
>>> from numpy.polynomial import chebyshev as C
|
| 784 |
+
>>> c1 = (1,2,3)
|
| 785 |
+
>>> c2 = (3,2,1)
|
| 786 |
+
>>> C.chebdiv(c1,c2) # quotient "intuitive," remainder not
|
| 787 |
+
(array([3.]), array([-8., -4.]))
|
| 788 |
+
>>> c2 = (0,1,2,3)
|
| 789 |
+
>>> C.chebdiv(c2,c1) # neither "intuitive"
|
| 790 |
+
(array([0., 2.]), array([-2., -4.]))
|
| 791 |
+
|
| 792 |
+
"""
|
| 793 |
+
# c1, c2 are trimmed copies
|
| 794 |
+
[c1, c2] = pu.as_series([c1, c2])
|
| 795 |
+
if c2[-1] == 0:
|
| 796 |
+
raise ZeroDivisionError # FIXME: add message with details to exception
|
| 797 |
+
|
| 798 |
+
# note: this is more efficient than `pu._div(chebmul, c1, c2)`
|
| 799 |
+
lc1 = len(c1)
|
| 800 |
+
lc2 = len(c2)
|
| 801 |
+
if lc1 < lc2:
|
| 802 |
+
return c1[:1] * 0, c1
|
| 803 |
+
elif lc2 == 1:
|
| 804 |
+
return c1 / c2[-1], c1[:1] * 0
|
| 805 |
+
else:
|
| 806 |
+
z1 = _cseries_to_zseries(c1)
|
| 807 |
+
z2 = _cseries_to_zseries(c2)
|
| 808 |
+
quo, rem = _zseries_div(z1, z2)
|
| 809 |
+
quo = pu.trimseq(_zseries_to_cseries(quo))
|
| 810 |
+
rem = pu.trimseq(_zseries_to_cseries(rem))
|
| 811 |
+
return quo, rem
|
| 812 |
+
|
| 813 |
+
|
| 814 |
+
def chebpow(c, pow, maxpower=16):
|
| 815 |
+
"""Raise a Chebyshev series to a power.
|
| 816 |
+
|
| 817 |
+
Returns the Chebyshev series `c` raised to the power `pow`. The
|
| 818 |
+
argument `c` is a sequence of coefficients ordered from low to high.
|
| 819 |
+
i.e., [1,2,3] is the series ``T_0 + 2*T_1 + 3*T_2.``
|
| 820 |
+
|
| 821 |
+
Parameters
|
| 822 |
+
----------
|
| 823 |
+
c : array_like
|
| 824 |
+
1-D array of Chebyshev series coefficients ordered from low to
|
| 825 |
+
high.
|
| 826 |
+
pow : integer
|
| 827 |
+
Power to which the series will be raised
|
| 828 |
+
maxpower : integer, optional
|
| 829 |
+
Maximum power allowed. This is mainly to limit growth of the series
|
| 830 |
+
to unmanageable size. Default is 16
|
| 831 |
+
|
| 832 |
+
Returns
|
| 833 |
+
-------
|
| 834 |
+
coef : ndarray
|
| 835 |
+
Chebyshev series of power.
|
| 836 |
+
|
| 837 |
+
See Also
|
| 838 |
+
--------
|
| 839 |
+
chebadd, chebsub, chebmulx, chebmul, chebdiv
|
| 840 |
+
|
| 841 |
+
Examples
|
| 842 |
+
--------
|
| 843 |
+
>>> from numpy.polynomial import chebyshev as C
|
| 844 |
+
>>> C.chebpow([1, 2, 3, 4], 2)
|
| 845 |
+
array([15.5, 22. , 16. , ..., 12.5, 12. , 8. ])
|
| 846 |
+
|
| 847 |
+
"""
|
| 848 |
+
# note: this is more efficient than `pu._pow(chebmul, c1, c2)`, as it
|
| 849 |
+
# avoids converting between z and c series repeatedly
|
| 850 |
+
|
| 851 |
+
# c is a trimmed copy
|
| 852 |
+
[c] = pu.as_series([c])
|
| 853 |
+
power = int(pow)
|
| 854 |
+
if power != pow or power < 0:
|
| 855 |
+
raise ValueError("Power must be a non-negative integer.")
|
| 856 |
+
elif maxpower is not None and power > maxpower:
|
| 857 |
+
raise ValueError("Power is too large")
|
| 858 |
+
elif power == 0:
|
| 859 |
+
return np.array([1], dtype=c.dtype)
|
| 860 |
+
elif power == 1:
|
| 861 |
+
return c
|
| 862 |
+
else:
|
| 863 |
+
# This can be made more efficient by using powers of two
|
| 864 |
+
# in the usual way.
|
| 865 |
+
zs = _cseries_to_zseries(c)
|
| 866 |
+
prd = zs
|
| 867 |
+
for i in range(2, power + 1):
|
| 868 |
+
prd = np.convolve(prd, zs)
|
| 869 |
+
return _zseries_to_cseries(prd)
|
| 870 |
+
|
| 871 |
+
|
| 872 |
+
def chebder(c, m=1, scl=1, axis=0):
|
| 873 |
+
"""
|
| 874 |
+
Differentiate a Chebyshev series.
|
| 875 |
+
|
| 876 |
+
Returns the Chebyshev series coefficients `c` differentiated `m` times
|
| 877 |
+
along `axis`. At each iteration the result is multiplied by `scl` (the
|
| 878 |
+
scaling factor is for use in a linear change of variable). The argument
|
| 879 |
+
`c` is an array of coefficients from low to high degree along each
|
| 880 |
+
axis, e.g., [1,2,3] represents the series ``1*T_0 + 2*T_1 + 3*T_2``
|
| 881 |
+
while [[1,2],[1,2]] represents ``1*T_0(x)*T_0(y) + 1*T_1(x)*T_0(y) +
|
| 882 |
+
2*T_0(x)*T_1(y) + 2*T_1(x)*T_1(y)`` if axis=0 is ``x`` and axis=1 is
|
| 883 |
+
``y``.
|
| 884 |
+
|
| 885 |
+
Parameters
|
| 886 |
+
----------
|
| 887 |
+
c : array_like
|
| 888 |
+
Array of Chebyshev series coefficients. If c is multidimensional
|
| 889 |
+
the different axis correspond to different variables with the
|
| 890 |
+
degree in each axis given by the corresponding index.
|
| 891 |
+
m : int, optional
|
| 892 |
+
Number of derivatives taken, must be non-negative. (Default: 1)
|
| 893 |
+
scl : scalar, optional
|
| 894 |
+
Each differentiation is multiplied by `scl`. The end result is
|
| 895 |
+
multiplication by ``scl**m``. This is for use in a linear change of
|
| 896 |
+
variable. (Default: 1)
|
| 897 |
+
axis : int, optional
|
| 898 |
+
Axis over which the derivative is taken. (Default: 0).
|
| 899 |
+
|
| 900 |
+
Returns
|
| 901 |
+
-------
|
| 902 |
+
der : ndarray
|
| 903 |
+
Chebyshev series of the derivative.
|
| 904 |
+
|
| 905 |
+
See Also
|
| 906 |
+
--------
|
| 907 |
+
chebint
|
| 908 |
+
|
| 909 |
+
Notes
|
| 910 |
+
-----
|
| 911 |
+
In general, the result of differentiating a C-series needs to be
|
| 912 |
+
"reprojected" onto the C-series basis set. Thus, typically, the
|
| 913 |
+
result of this function is "unintuitive," albeit correct; see Examples
|
| 914 |
+
section below.
|
| 915 |
+
|
| 916 |
+
Examples
|
| 917 |
+
--------
|
| 918 |
+
>>> from numpy.polynomial import chebyshev as C
|
| 919 |
+
>>> c = (1,2,3,4)
|
| 920 |
+
>>> C.chebder(c)
|
| 921 |
+
array([14., 12., 24.])
|
| 922 |
+
>>> C.chebder(c,3)
|
| 923 |
+
array([96.])
|
| 924 |
+
>>> C.chebder(c,scl=-1)
|
| 925 |
+
array([-14., -12., -24.])
|
| 926 |
+
>>> C.chebder(c,2,-1)
|
| 927 |
+
array([12., 96.])
|
| 928 |
+
|
| 929 |
+
"""
|
| 930 |
+
c = np.array(c, ndmin=1, copy=True)
|
| 931 |
+
if c.dtype.char in '?bBhHiIlLqQpP':
|
| 932 |
+
c = c.astype(np.double)
|
| 933 |
+
cnt = pu._as_int(m, "the order of derivation")
|
| 934 |
+
iaxis = pu._as_int(axis, "the axis")
|
| 935 |
+
if cnt < 0:
|
| 936 |
+
raise ValueError("The order of derivation must be non-negative")
|
| 937 |
+
iaxis = np.lib.array_utils.normalize_axis_index(iaxis, c.ndim)
|
| 938 |
+
|
| 939 |
+
if cnt == 0:
|
| 940 |
+
return c
|
| 941 |
+
|
| 942 |
+
c = np.moveaxis(c, iaxis, 0)
|
| 943 |
+
n = len(c)
|
| 944 |
+
if cnt >= n:
|
| 945 |
+
c = c[:1] * 0
|
| 946 |
+
else:
|
| 947 |
+
for i in range(cnt):
|
| 948 |
+
n = n - 1
|
| 949 |
+
c *= scl
|
| 950 |
+
der = np.empty((n,) + c.shape[1:], dtype=c.dtype)
|
| 951 |
+
for j in range(n, 2, -1):
|
| 952 |
+
der[j - 1] = (2 * j) * c[j]
|
| 953 |
+
c[j - 2] += (j * c[j]) / (j - 2)
|
| 954 |
+
if n > 1:
|
| 955 |
+
der[1] = 4 * c[2]
|
| 956 |
+
der[0] = c[1]
|
| 957 |
+
c = der
|
| 958 |
+
c = np.moveaxis(c, 0, iaxis)
|
| 959 |
+
return c
|
| 960 |
+
|
| 961 |
+
|
| 962 |
+
def chebint(c, m=1, k=[], lbnd=0, scl=1, axis=0):
|
| 963 |
+
"""
|
| 964 |
+
Integrate a Chebyshev series.
|
| 965 |
+
|
| 966 |
+
Returns the Chebyshev series coefficients `c` integrated `m` times from
|
| 967 |
+
`lbnd` along `axis`. At each iteration the resulting series is
|
| 968 |
+
**multiplied** by `scl` and an integration constant, `k`, is added.
|
| 969 |
+
The scaling factor is for use in a linear change of variable. ("Buyer
|
| 970 |
+
beware": note that, depending on what one is doing, one may want `scl`
|
| 971 |
+
to be the reciprocal of what one might expect; for more information,
|
| 972 |
+
see the Notes section below.) The argument `c` is an array of
|
| 973 |
+
coefficients from low to high degree along each axis, e.g., [1,2,3]
|
| 974 |
+
represents the series ``T_0 + 2*T_1 + 3*T_2`` while [[1,2],[1,2]]
|
| 975 |
+
represents ``1*T_0(x)*T_0(y) + 1*T_1(x)*T_0(y) + 2*T_0(x)*T_1(y) +
|
| 976 |
+
2*T_1(x)*T_1(y)`` if axis=0 is ``x`` and axis=1 is ``y``.
|
| 977 |
+
|
| 978 |
+
Parameters
|
| 979 |
+
----------
|
| 980 |
+
c : array_like
|
| 981 |
+
Array of Chebyshev series coefficients. If c is multidimensional
|
| 982 |
+
the different axis correspond to different variables with the
|
| 983 |
+
degree in each axis given by the corresponding index.
|
| 984 |
+
m : int, optional
|
| 985 |
+
Order of integration, must be positive. (Default: 1)
|
| 986 |
+
k : {[], list, scalar}, optional
|
| 987 |
+
Integration constant(s). The value of the first integral at zero
|
| 988 |
+
is the first value in the list, the value of the second integral
|
| 989 |
+
at zero is the second value, etc. If ``k == []`` (the default),
|
| 990 |
+
all constants are set to zero. If ``m == 1``, a single scalar can
|
| 991 |
+
be given instead of a list.
|
| 992 |
+
lbnd : scalar, optional
|
| 993 |
+
The lower bound of the integral. (Default: 0)
|
| 994 |
+
scl : scalar, optional
|
| 995 |
+
Following each integration the result is *multiplied* by `scl`
|
| 996 |
+
before the integration constant is added. (Default: 1)
|
| 997 |
+
axis : int, optional
|
| 998 |
+
Axis over which the integral is taken. (Default: 0).
|
| 999 |
+
|
| 1000 |
+
Returns
|
| 1001 |
+
-------
|
| 1002 |
+
S : ndarray
|
| 1003 |
+
C-series coefficients of the integral.
|
| 1004 |
+
|
| 1005 |
+
Raises
|
| 1006 |
+
------
|
| 1007 |
+
ValueError
|
| 1008 |
+
If ``m < 1``, ``len(k) > m``, ``np.ndim(lbnd) != 0``, or
|
| 1009 |
+
``np.ndim(scl) != 0``.
|
| 1010 |
+
|
| 1011 |
+
See Also
|
| 1012 |
+
--------
|
| 1013 |
+
chebder
|
| 1014 |
+
|
| 1015 |
+
Notes
|
| 1016 |
+
-----
|
| 1017 |
+
Note that the result of each integration is *multiplied* by `scl`.
|
| 1018 |
+
Why is this important to note? Say one is making a linear change of
|
| 1019 |
+
variable :math:`u = ax + b` in an integral relative to `x`. Then
|
| 1020 |
+
:math:`dx = du/a`, so one will need to set `scl` equal to
|
| 1021 |
+
:math:`1/a`- perhaps not what one would have first thought.
|
| 1022 |
+
|
| 1023 |
+
Also note that, in general, the result of integrating a C-series needs
|
| 1024 |
+
to be "reprojected" onto the C-series basis set. Thus, typically,
|
| 1025 |
+
the result of this function is "unintuitive," albeit correct; see
|
| 1026 |
+
Examples section below.
|
| 1027 |
+
|
| 1028 |
+
Examples
|
| 1029 |
+
--------
|
| 1030 |
+
>>> from numpy.polynomial import chebyshev as C
|
| 1031 |
+
>>> c = (1,2,3)
|
| 1032 |
+
>>> C.chebint(c)
|
| 1033 |
+
array([ 0.5, -0.5, 0.5, 0.5])
|
| 1034 |
+
>>> C.chebint(c,3)
|
| 1035 |
+
array([ 0.03125 , -0.1875 , 0.04166667, -0.05208333, 0.01041667, # may vary
|
| 1036 |
+
0.00625 ])
|
| 1037 |
+
>>> C.chebint(c, k=3)
|
| 1038 |
+
array([ 3.5, -0.5, 0.5, 0.5])
|
| 1039 |
+
>>> C.chebint(c,lbnd=-2)
|
| 1040 |
+
array([ 8.5, -0.5, 0.5, 0.5])
|
| 1041 |
+
>>> C.chebint(c,scl=-2)
|
| 1042 |
+
array([-1., 1., -1., -1.])
|
| 1043 |
+
|
| 1044 |
+
"""
|
| 1045 |
+
c = np.array(c, ndmin=1, copy=True)
|
| 1046 |
+
if c.dtype.char in '?bBhHiIlLqQpP':
|
| 1047 |
+
c = c.astype(np.double)
|
| 1048 |
+
if not np.iterable(k):
|
| 1049 |
+
k = [k]
|
| 1050 |
+
cnt = pu._as_int(m, "the order of integration")
|
| 1051 |
+
iaxis = pu._as_int(axis, "the axis")
|
| 1052 |
+
if cnt < 0:
|
| 1053 |
+
raise ValueError("The order of integration must be non-negative")
|
| 1054 |
+
if len(k) > cnt:
|
| 1055 |
+
raise ValueError("Too many integration constants")
|
| 1056 |
+
if np.ndim(lbnd) != 0:
|
| 1057 |
+
raise ValueError("lbnd must be a scalar.")
|
| 1058 |
+
if np.ndim(scl) != 0:
|
| 1059 |
+
raise ValueError("scl must be a scalar.")
|
| 1060 |
+
iaxis = np.lib.array_utils.normalize_axis_index(iaxis, c.ndim)
|
| 1061 |
+
|
| 1062 |
+
if cnt == 0:
|
| 1063 |
+
return c
|
| 1064 |
+
|
| 1065 |
+
c = np.moveaxis(c, iaxis, 0)
|
| 1066 |
+
k = list(k) + [0] * (cnt - len(k))
|
| 1067 |
+
for i in range(cnt):
|
| 1068 |
+
n = len(c)
|
| 1069 |
+
c *= scl
|
| 1070 |
+
if n == 1 and np.all(c[0] == 0):
|
| 1071 |
+
c[0] += k[i]
|
| 1072 |
+
else:
|
| 1073 |
+
tmp = np.empty((n + 1,) + c.shape[1:], dtype=c.dtype)
|
| 1074 |
+
tmp[0] = c[0] * 0
|
| 1075 |
+
tmp[1] = c[0]
|
| 1076 |
+
if n > 1:
|
| 1077 |
+
tmp[2] = c[1] / 4
|
| 1078 |
+
for j in range(2, n):
|
| 1079 |
+
tmp[j + 1] = c[j] / (2 * (j + 1))
|
| 1080 |
+
tmp[j - 1] -= c[j] / (2 * (j - 1))
|
| 1081 |
+
tmp[0] += k[i] - chebval(lbnd, tmp)
|
| 1082 |
+
c = tmp
|
| 1083 |
+
c = np.moveaxis(c, 0, iaxis)
|
| 1084 |
+
return c
|
| 1085 |
+
|
| 1086 |
+
|
| 1087 |
+
def chebval(x, c, tensor=True):
|
| 1088 |
+
"""
|
| 1089 |
+
Evaluate a Chebyshev series at points x.
|
| 1090 |
+
|
| 1091 |
+
If `c` is of length `n + 1`, this function returns the value:
|
| 1092 |
+
|
| 1093 |
+
.. math:: p(x) = c_0 * T_0(x) + c_1 * T_1(x) + ... + c_n * T_n(x)
|
| 1094 |
+
|
| 1095 |
+
The parameter `x` is converted to an array only if it is a tuple or a
|
| 1096 |
+
list, otherwise it is treated as a scalar. In either case, either `x`
|
| 1097 |
+
or its elements must support multiplication and addition both with
|
| 1098 |
+
themselves and with the elements of `c`.
|
| 1099 |
+
|
| 1100 |
+
If `c` is a 1-D array, then ``p(x)`` will have the same shape as `x`. If
|
| 1101 |
+
`c` is multidimensional, then the shape of the result depends on the
|
| 1102 |
+
value of `tensor`. If `tensor` is true the shape will be c.shape[1:] +
|
| 1103 |
+
x.shape. If `tensor` is false the shape will be c.shape[1:]. Note that
|
| 1104 |
+
scalars have shape (,).
|
| 1105 |
+
|
| 1106 |
+
Trailing zeros in the coefficients will be used in the evaluation, so
|
| 1107 |
+
they should be avoided if efficiency is a concern.
|
| 1108 |
+
|
| 1109 |
+
Parameters
|
| 1110 |
+
----------
|
| 1111 |
+
x : array_like, compatible object
|
| 1112 |
+
If `x` is a list or tuple, it is converted to an ndarray, otherwise
|
| 1113 |
+
it is left unchanged and treated as a scalar. In either case, `x`
|
| 1114 |
+
or its elements must support addition and multiplication with
|
| 1115 |
+
themselves and with the elements of `c`.
|
| 1116 |
+
c : array_like
|
| 1117 |
+
Array of coefficients ordered so that the coefficients for terms of
|
| 1118 |
+
degree n are contained in c[n]. If `c` is multidimensional the
|
| 1119 |
+
remaining indices enumerate multiple polynomials. In the two
|
| 1120 |
+
dimensional case the coefficients may be thought of as stored in
|
| 1121 |
+
the columns of `c`.
|
| 1122 |
+
tensor : boolean, optional
|
| 1123 |
+
If True, the shape of the coefficient array is extended with ones
|
| 1124 |
+
on the right, one for each dimension of `x`. Scalars have dimension 0
|
| 1125 |
+
for this action. The result is that every column of coefficients in
|
| 1126 |
+
`c` is evaluated for every element of `x`. If False, `x` is broadcast
|
| 1127 |
+
over the columns of `c` for the evaluation. This keyword is useful
|
| 1128 |
+
when `c` is multidimensional. The default value is True.
|
| 1129 |
+
|
| 1130 |
+
Returns
|
| 1131 |
+
-------
|
| 1132 |
+
values : ndarray, algebra_like
|
| 1133 |
+
The shape of the return value is described above.
|
| 1134 |
+
|
| 1135 |
+
See Also
|
| 1136 |
+
--------
|
| 1137 |
+
chebval2d, chebgrid2d, chebval3d, chebgrid3d
|
| 1138 |
+
|
| 1139 |
+
Notes
|
| 1140 |
+
-----
|
| 1141 |
+
The evaluation uses Clenshaw recursion, aka synthetic division.
|
| 1142 |
+
|
| 1143 |
+
"""
|
| 1144 |
+
c = np.array(c, ndmin=1, copy=True)
|
| 1145 |
+
if c.dtype.char in '?bBhHiIlLqQpP':
|
| 1146 |
+
c = c.astype(np.double)
|
| 1147 |
+
if isinstance(x, (tuple, list)):
|
| 1148 |
+
x = np.asarray(x)
|
| 1149 |
+
if isinstance(x, np.ndarray) and tensor:
|
| 1150 |
+
c = c.reshape(c.shape + (1,) * x.ndim)
|
| 1151 |
+
|
| 1152 |
+
if len(c) == 1:
|
| 1153 |
+
c0 = c[0]
|
| 1154 |
+
c1 = 0
|
| 1155 |
+
elif len(c) == 2:
|
| 1156 |
+
c0 = c[0]
|
| 1157 |
+
c1 = c[1]
|
| 1158 |
+
else:
|
| 1159 |
+
x2 = 2 * x
|
| 1160 |
+
c0 = c[-2]
|
| 1161 |
+
c1 = c[-1]
|
| 1162 |
+
for i in range(3, len(c) + 1):
|
| 1163 |
+
tmp = c0
|
| 1164 |
+
c0 = c[-i] - c1
|
| 1165 |
+
c1 = tmp + c1 * x2
|
| 1166 |
+
return c0 + c1 * x
|
| 1167 |
+
|
| 1168 |
+
|
| 1169 |
+
def chebval2d(x, y, c):
|
| 1170 |
+
"""
|
| 1171 |
+
Evaluate a 2-D Chebyshev series at points (x, y).
|
| 1172 |
+
|
| 1173 |
+
This function returns the values:
|
| 1174 |
+
|
| 1175 |
+
.. math:: p(x,y) = \\sum_{i,j} c_{i,j} * T_i(x) * T_j(y)
|
| 1176 |
+
|
| 1177 |
+
The parameters `x` and `y` are converted to arrays only if they are
|
| 1178 |
+
tuples or a lists, otherwise they are treated as a scalars and they
|
| 1179 |
+
must have the same shape after conversion. In either case, either `x`
|
| 1180 |
+
and `y` or their elements must support multiplication and addition both
|
| 1181 |
+
with themselves and with the elements of `c`.
|
| 1182 |
+
|
| 1183 |
+
If `c` is a 1-D array a one is implicitly appended to its shape to make
|
| 1184 |
+
it 2-D. The shape of the result will be c.shape[2:] + x.shape.
|
| 1185 |
+
|
| 1186 |
+
Parameters
|
| 1187 |
+
----------
|
| 1188 |
+
x, y : array_like, compatible objects
|
| 1189 |
+
The two dimensional series is evaluated at the points ``(x, y)``,
|
| 1190 |
+
where `x` and `y` must have the same shape. If `x` or `y` is a list
|
| 1191 |
+
or tuple, it is first converted to an ndarray, otherwise it is left
|
| 1192 |
+
unchanged and if it isn't an ndarray it is treated as a scalar.
|
| 1193 |
+
c : array_like
|
| 1194 |
+
Array of coefficients ordered so that the coefficient of the term
|
| 1195 |
+
of multi-degree i,j is contained in ``c[i,j]``. If `c` has
|
| 1196 |
+
dimension greater than 2 the remaining indices enumerate multiple
|
| 1197 |
+
sets of coefficients.
|
| 1198 |
+
|
| 1199 |
+
Returns
|
| 1200 |
+
-------
|
| 1201 |
+
values : ndarray, compatible object
|
| 1202 |
+
The values of the two dimensional Chebyshev series at points formed
|
| 1203 |
+
from pairs of corresponding values from `x` and `y`.
|
| 1204 |
+
|
| 1205 |
+
See Also
|
| 1206 |
+
--------
|
| 1207 |
+
chebval, chebgrid2d, chebval3d, chebgrid3d
|
| 1208 |
+
"""
|
| 1209 |
+
return pu._valnd(chebval, c, x, y)
|
| 1210 |
+
|
| 1211 |
+
|
| 1212 |
+
def chebgrid2d(x, y, c):
|
| 1213 |
+
"""
|
| 1214 |
+
Evaluate a 2-D Chebyshev series on the Cartesian product of x and y.
|
| 1215 |
+
|
| 1216 |
+
This function returns the values:
|
| 1217 |
+
|
| 1218 |
+
.. math:: p(a,b) = \\sum_{i,j} c_{i,j} * T_i(a) * T_j(b),
|
| 1219 |
+
|
| 1220 |
+
where the points `(a, b)` consist of all pairs formed by taking
|
| 1221 |
+
`a` from `x` and `b` from `y`. The resulting points form a grid with
|
| 1222 |
+
`x` in the first dimension and `y` in the second.
|
| 1223 |
+
|
| 1224 |
+
The parameters `x` and `y` are converted to arrays only if they are
|
| 1225 |
+
tuples or a lists, otherwise they are treated as a scalars. In either
|
| 1226 |
+
case, either `x` and `y` or their elements must support multiplication
|
| 1227 |
+
and addition both with themselves and with the elements of `c`.
|
| 1228 |
+
|
| 1229 |
+
If `c` has fewer than two dimensions, ones are implicitly appended to
|
| 1230 |
+
its shape to make it 2-D. The shape of the result will be c.shape[2:] +
|
| 1231 |
+
x.shape + y.shape.
|
| 1232 |
+
|
| 1233 |
+
Parameters
|
| 1234 |
+
----------
|
| 1235 |
+
x, y : array_like, compatible objects
|
| 1236 |
+
The two dimensional series is evaluated at the points in the
|
| 1237 |
+
Cartesian product of `x` and `y`. If `x` or `y` is a list or
|
| 1238 |
+
tuple, it is first converted to an ndarray, otherwise it is left
|
| 1239 |
+
unchanged and, if it isn't an ndarray, it is treated as a scalar.
|
| 1240 |
+
c : array_like
|
| 1241 |
+
Array of coefficients ordered so that the coefficient of the term of
|
| 1242 |
+
multi-degree i,j is contained in ``c[i,j]``. If `c` has dimension
|
| 1243 |
+
greater than two the remaining indices enumerate multiple sets of
|
| 1244 |
+
coefficients.
|
| 1245 |
+
|
| 1246 |
+
Returns
|
| 1247 |
+
-------
|
| 1248 |
+
values : ndarray, compatible object
|
| 1249 |
+
The values of the two dimensional Chebyshev series at points in the
|
| 1250 |
+
Cartesian product of `x` and `y`.
|
| 1251 |
+
|
| 1252 |
+
See Also
|
| 1253 |
+
--------
|
| 1254 |
+
chebval, chebval2d, chebval3d, chebgrid3d
|
| 1255 |
+
"""
|
| 1256 |
+
return pu._gridnd(chebval, c, x, y)
|
| 1257 |
+
|
| 1258 |
+
|
| 1259 |
+
def chebval3d(x, y, z, c):
|
| 1260 |
+
"""
|
| 1261 |
+
Evaluate a 3-D Chebyshev series at points (x, y, z).
|
| 1262 |
+
|
| 1263 |
+
This function returns the values:
|
| 1264 |
+
|
| 1265 |
+
.. math:: p(x,y,z) = \\sum_{i,j,k} c_{i,j,k} * T_i(x) * T_j(y) * T_k(z)
|
| 1266 |
+
|
| 1267 |
+
The parameters `x`, `y`, and `z` are converted to arrays only if
|
| 1268 |
+
they are tuples or a lists, otherwise they are treated as a scalars and
|
| 1269 |
+
they must have the same shape after conversion. In either case, either
|
| 1270 |
+
`x`, `y`, and `z` or their elements must support multiplication and
|
| 1271 |
+
addition both with themselves and with the elements of `c`.
|
| 1272 |
+
|
| 1273 |
+
If `c` has fewer than 3 dimensions, ones are implicitly appended to its
|
| 1274 |
+
shape to make it 3-D. The shape of the result will be c.shape[3:] +
|
| 1275 |
+
x.shape.
|
| 1276 |
+
|
| 1277 |
+
Parameters
|
| 1278 |
+
----------
|
| 1279 |
+
x, y, z : array_like, compatible object
|
| 1280 |
+
The three dimensional series is evaluated at the points
|
| 1281 |
+
``(x, y, z)``, where `x`, `y`, and `z` must have the same shape. If
|
| 1282 |
+
any of `x`, `y`, or `z` is a list or tuple, it is first converted
|
| 1283 |
+
to an ndarray, otherwise it is left unchanged and if it isn't an
|
| 1284 |
+
ndarray it is treated as a scalar.
|
| 1285 |
+
c : array_like
|
| 1286 |
+
Array of coefficients ordered so that the coefficient of the term of
|
| 1287 |
+
multi-degree i,j,k is contained in ``c[i,j,k]``. If `c` has dimension
|
| 1288 |
+
greater than 3 the remaining indices enumerate multiple sets of
|
| 1289 |
+
coefficients.
|
| 1290 |
+
|
| 1291 |
+
Returns
|
| 1292 |
+
-------
|
| 1293 |
+
values : ndarray, compatible object
|
| 1294 |
+
The values of the multidimensional polynomial on points formed with
|
| 1295 |
+
triples of corresponding values from `x`, `y`, and `z`.
|
| 1296 |
+
|
| 1297 |
+
See Also
|
| 1298 |
+
--------
|
| 1299 |
+
chebval, chebval2d, chebgrid2d, chebgrid3d
|
| 1300 |
+
"""
|
| 1301 |
+
return pu._valnd(chebval, c, x, y, z)
|
| 1302 |
+
|
| 1303 |
+
|
| 1304 |
+
def chebgrid3d(x, y, z, c):
|
| 1305 |
+
"""
|
| 1306 |
+
Evaluate a 3-D Chebyshev series on the Cartesian product of x, y, and z.
|
| 1307 |
+
|
| 1308 |
+
This function returns the values:
|
| 1309 |
+
|
| 1310 |
+
.. math:: p(a,b,c) = \\sum_{i,j,k} c_{i,j,k} * T_i(a) * T_j(b) * T_k(c)
|
| 1311 |
+
|
| 1312 |
+
where the points ``(a, b, c)`` consist of all triples formed by taking
|
| 1313 |
+
`a` from `x`, `b` from `y`, and `c` from `z`. The resulting points form
|
| 1314 |
+
a grid with `x` in the first dimension, `y` in the second, and `z` in
|
| 1315 |
+
the third.
|
| 1316 |
+
|
| 1317 |
+
The parameters `x`, `y`, and `z` are converted to arrays only if they
|
| 1318 |
+
are tuples or a lists, otherwise they are treated as a scalars. In
|
| 1319 |
+
either case, either `x`, `y`, and `z` or their elements must support
|
| 1320 |
+
multiplication and addition both with themselves and with the elements
|
| 1321 |
+
of `c`.
|
| 1322 |
+
|
| 1323 |
+
If `c` has fewer than three dimensions, ones are implicitly appended to
|
| 1324 |
+
its shape to make it 3-D. The shape of the result will be c.shape[3:] +
|
| 1325 |
+
x.shape + y.shape + z.shape.
|
| 1326 |
+
|
| 1327 |
+
Parameters
|
| 1328 |
+
----------
|
| 1329 |
+
x, y, z : array_like, compatible objects
|
| 1330 |
+
The three dimensional series is evaluated at the points in the
|
| 1331 |
+
Cartesian product of `x`, `y`, and `z`. If `x`, `y`, or `z` is a
|
| 1332 |
+
list or tuple, it is first converted to an ndarray, otherwise it is
|
| 1333 |
+
left unchanged and, if it isn't an ndarray, it is treated as a
|
| 1334 |
+
scalar.
|
| 1335 |
+
c : array_like
|
| 1336 |
+
Array of coefficients ordered so that the coefficients for terms of
|
| 1337 |
+
degree i,j are contained in ``c[i,j]``. If `c` has dimension
|
| 1338 |
+
greater than two the remaining indices enumerate multiple sets of
|
| 1339 |
+
coefficients.
|
| 1340 |
+
|
| 1341 |
+
Returns
|
| 1342 |
+
-------
|
| 1343 |
+
values : ndarray, compatible object
|
| 1344 |
+
The values of the two dimensional polynomial at points in the Cartesian
|
| 1345 |
+
product of `x` and `y`.
|
| 1346 |
+
|
| 1347 |
+
See Also
|
| 1348 |
+
--------
|
| 1349 |
+
chebval, chebval2d, chebgrid2d, chebval3d
|
| 1350 |
+
"""
|
| 1351 |
+
return pu._gridnd(chebval, c, x, y, z)
|
| 1352 |
+
|
| 1353 |
+
|
| 1354 |
+
def chebvander(x, deg):
|
| 1355 |
+
"""Pseudo-Vandermonde matrix of given degree.
|
| 1356 |
+
|
| 1357 |
+
Returns the pseudo-Vandermonde matrix of degree `deg` and sample points
|
| 1358 |
+
`x`. The pseudo-Vandermonde matrix is defined by
|
| 1359 |
+
|
| 1360 |
+
.. math:: V[..., i] = T_i(x),
|
| 1361 |
+
|
| 1362 |
+
where ``0 <= i <= deg``. The leading indices of `V` index the elements of
|
| 1363 |
+
`x` and the last index is the degree of the Chebyshev polynomial.
|
| 1364 |
+
|
| 1365 |
+
If `c` is a 1-D array of coefficients of length ``n + 1`` and `V` is the
|
| 1366 |
+
matrix ``V = chebvander(x, n)``, then ``np.dot(V, c)`` and
|
| 1367 |
+
``chebval(x, c)`` are the same up to roundoff. This equivalence is
|
| 1368 |
+
useful both for least squares fitting and for the evaluation of a large
|
| 1369 |
+
number of Chebyshev series of the same degree and sample points.
|
| 1370 |
+
|
| 1371 |
+
Parameters
|
| 1372 |
+
----------
|
| 1373 |
+
x : array_like
|
| 1374 |
+
Array of points. The dtype is converted to float64 or complex128
|
| 1375 |
+
depending on whether any of the elements are complex. If `x` is
|
| 1376 |
+
scalar it is converted to a 1-D array.
|
| 1377 |
+
deg : int
|
| 1378 |
+
Degree of the resulting matrix.
|
| 1379 |
+
|
| 1380 |
+
Returns
|
| 1381 |
+
-------
|
| 1382 |
+
vander : ndarray
|
| 1383 |
+
The pseudo Vandermonde matrix. The shape of the returned matrix is
|
| 1384 |
+
``x.shape + (deg + 1,)``, where The last index is the degree of the
|
| 1385 |
+
corresponding Chebyshev polynomial. The dtype will be the same as
|
| 1386 |
+
the converted `x`.
|
| 1387 |
+
|
| 1388 |
+
"""
|
| 1389 |
+
ideg = pu._as_int(deg, "deg")
|
| 1390 |
+
if ideg < 0:
|
| 1391 |
+
raise ValueError("deg must be non-negative")
|
| 1392 |
+
|
| 1393 |
+
x = np.array(x, copy=None, ndmin=1) + 0.0
|
| 1394 |
+
dims = (ideg + 1,) + x.shape
|
| 1395 |
+
dtyp = x.dtype
|
| 1396 |
+
v = np.empty(dims, dtype=dtyp)
|
| 1397 |
+
# Use forward recursion to generate the entries.
|
| 1398 |
+
v[0] = x * 0 + 1
|
| 1399 |
+
if ideg > 0:
|
| 1400 |
+
x2 = 2 * x
|
| 1401 |
+
v[1] = x
|
| 1402 |
+
for i in range(2, ideg + 1):
|
| 1403 |
+
v[i] = v[i - 1] * x2 - v[i - 2]
|
| 1404 |
+
return np.moveaxis(v, 0, -1)
|
| 1405 |
+
|
| 1406 |
+
|
| 1407 |
+
def chebvander2d(x, y, deg):
|
| 1408 |
+
"""Pseudo-Vandermonde matrix of given degrees.
|
| 1409 |
+
|
| 1410 |
+
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample
|
| 1411 |
+
points ``(x, y)``. The pseudo-Vandermonde matrix is defined by
|
| 1412 |
+
|
| 1413 |
+
.. math:: V[..., (deg[1] + 1)*i + j] = T_i(x) * T_j(y),
|
| 1414 |
+
|
| 1415 |
+
where ``0 <= i <= deg[0]`` and ``0 <= j <= deg[1]``. The leading indices of
|
| 1416 |
+
`V` index the points ``(x, y)`` and the last index encodes the degrees of
|
| 1417 |
+
the Chebyshev polynomials.
|
| 1418 |
+
|
| 1419 |
+
If ``V = chebvander2d(x, y, [xdeg, ydeg])``, then the columns of `V`
|
| 1420 |
+
correspond to the elements of a 2-D coefficient array `c` of shape
|
| 1421 |
+
(xdeg + 1, ydeg + 1) in the order
|
| 1422 |
+
|
| 1423 |
+
.. math:: c_{00}, c_{01}, c_{02} ... , c_{10}, c_{11}, c_{12} ...
|
| 1424 |
+
|
| 1425 |
+
and ``np.dot(V, c.flat)`` and ``chebval2d(x, y, c)`` will be the same
|
| 1426 |
+
up to roundoff. This equivalence is useful both for least squares
|
| 1427 |
+
fitting and for the evaluation of a large number of 2-D Chebyshev
|
| 1428 |
+
series of the same degrees and sample points.
|
| 1429 |
+
|
| 1430 |
+
Parameters
|
| 1431 |
+
----------
|
| 1432 |
+
x, y : array_like
|
| 1433 |
+
Arrays of point coordinates, all of the same shape. The dtypes
|
| 1434 |
+
will be converted to either float64 or complex128 depending on
|
| 1435 |
+
whether any of the elements are complex. Scalars are converted to
|
| 1436 |
+
1-D arrays.
|
| 1437 |
+
deg : list of ints
|
| 1438 |
+
List of maximum degrees of the form [x_deg, y_deg].
|
| 1439 |
+
|
| 1440 |
+
Returns
|
| 1441 |
+
-------
|
| 1442 |
+
vander2d : ndarray
|
| 1443 |
+
The shape of the returned matrix is ``x.shape + (order,)``, where
|
| 1444 |
+
:math:`order = (deg[0]+1)*(deg[1]+1)`. The dtype will be the same
|
| 1445 |
+
as the converted `x` and `y`.
|
| 1446 |
+
|
| 1447 |
+
See Also
|
| 1448 |
+
--------
|
| 1449 |
+
chebvander, chebvander3d, chebval2d, chebval3d
|
| 1450 |
+
"""
|
| 1451 |
+
return pu._vander_nd_flat((chebvander, chebvander), (x, y), deg)
|
| 1452 |
+
|
| 1453 |
+
|
| 1454 |
+
def chebvander3d(x, y, z, deg):
|
| 1455 |
+
"""Pseudo-Vandermonde matrix of given degrees.
|
| 1456 |
+
|
| 1457 |
+
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample
|
| 1458 |
+
points ``(x, y, z)``. If `l`, `m`, `n` are the given degrees in `x`, `y`, `z`,
|
| 1459 |
+
then The pseudo-Vandermonde matrix is defined by
|
| 1460 |
+
|
| 1461 |
+
.. math:: V[..., (m+1)(n+1)i + (n+1)j + k] = T_i(x)*T_j(y)*T_k(z),
|
| 1462 |
+
|
| 1463 |
+
where ``0 <= i <= l``, ``0 <= j <= m``, and ``0 <= j <= n``. The leading
|
| 1464 |
+
indices of `V` index the points ``(x, y, z)`` and the last index encodes
|
| 1465 |
+
the degrees of the Chebyshev polynomials.
|
| 1466 |
+
|
| 1467 |
+
If ``V = chebvander3d(x, y, z, [xdeg, ydeg, zdeg])``, then the columns
|
| 1468 |
+
of `V` correspond to the elements of a 3-D coefficient array `c` of
|
| 1469 |
+
shape (xdeg + 1, ydeg + 1, zdeg + 1) in the order
|
| 1470 |
+
|
| 1471 |
+
.. math:: c_{000}, c_{001}, c_{002},... , c_{010}, c_{011}, c_{012},...
|
| 1472 |
+
|
| 1473 |
+
and ``np.dot(V, c.flat)`` and ``chebval3d(x, y, z, c)`` will be the
|
| 1474 |
+
same up to roundoff. This equivalence is useful both for least squares
|
| 1475 |
+
fitting and for the evaluation of a large number of 3-D Chebyshev
|
| 1476 |
+
series of the same degrees and sample points.
|
| 1477 |
+
|
| 1478 |
+
Parameters
|
| 1479 |
+
----------
|
| 1480 |
+
x, y, z : array_like
|
| 1481 |
+
Arrays of point coordinates, all of the same shape. The dtypes will
|
| 1482 |
+
be converted to either float64 or complex128 depending on whether
|
| 1483 |
+
any of the elements are complex. Scalars are converted to 1-D
|
| 1484 |
+
arrays.
|
| 1485 |
+
deg : list of ints
|
| 1486 |
+
List of maximum degrees of the form [x_deg, y_deg, z_deg].
|
| 1487 |
+
|
| 1488 |
+
Returns
|
| 1489 |
+
-------
|
| 1490 |
+
vander3d : ndarray
|
| 1491 |
+
The shape of the returned matrix is ``x.shape + (order,)``, where
|
| 1492 |
+
:math:`order = (deg[0]+1)*(deg[1]+1)*(deg[2]+1)`. The dtype will
|
| 1493 |
+
be the same as the converted `x`, `y`, and `z`.
|
| 1494 |
+
|
| 1495 |
+
See Also
|
| 1496 |
+
--------
|
| 1497 |
+
chebvander, chebvander3d, chebval2d, chebval3d
|
| 1498 |
+
"""
|
| 1499 |
+
return pu._vander_nd_flat((chebvander, chebvander, chebvander), (x, y, z), deg)
|
| 1500 |
+
|
| 1501 |
+
|
| 1502 |
+
def chebfit(x, y, deg, rcond=None, full=False, w=None):
|
| 1503 |
+
"""
|
| 1504 |
+
Least squares fit of Chebyshev series to data.
|
| 1505 |
+
|
| 1506 |
+
Return the coefficients of a Chebyshev series of degree `deg` that is the
|
| 1507 |
+
least squares fit to the data values `y` given at points `x`. If `y` is
|
| 1508 |
+
1-D the returned coefficients will also be 1-D. If `y` is 2-D multiple
|
| 1509 |
+
fits are done, one for each column of `y`, and the resulting
|
| 1510 |
+
coefficients are stored in the corresponding columns of a 2-D return.
|
| 1511 |
+
The fitted polynomial(s) are in the form
|
| 1512 |
+
|
| 1513 |
+
.. math:: p(x) = c_0 + c_1 * T_1(x) + ... + c_n * T_n(x),
|
| 1514 |
+
|
| 1515 |
+
where `n` is `deg`.
|
| 1516 |
+
|
| 1517 |
+
Parameters
|
| 1518 |
+
----------
|
| 1519 |
+
x : array_like, shape (M,)
|
| 1520 |
+
x-coordinates of the M sample points ``(x[i], y[i])``.
|
| 1521 |
+
y : array_like, shape (M,) or (M, K)
|
| 1522 |
+
y-coordinates of the sample points. Several data sets of sample
|
| 1523 |
+
points sharing the same x-coordinates can be fitted at once by
|
| 1524 |
+
passing in a 2D-array that contains one dataset per column.
|
| 1525 |
+
deg : int or 1-D array_like
|
| 1526 |
+
Degree(s) of the fitting polynomials. If `deg` is a single integer,
|
| 1527 |
+
all terms up to and including the `deg`'th term are included in the
|
| 1528 |
+
fit. For NumPy versions >= 1.11.0 a list of integers specifying the
|
| 1529 |
+
degrees of the terms to include may be used instead.
|
| 1530 |
+
rcond : float, optional
|
| 1531 |
+
Relative condition number of the fit. Singular values smaller than
|
| 1532 |
+
this relative to the largest singular value will be ignored. The
|
| 1533 |
+
default value is ``len(x)*eps``, where eps is the relative precision of
|
| 1534 |
+
the float type, about 2e-16 in most cases.
|
| 1535 |
+
full : bool, optional
|
| 1536 |
+
Switch determining nature of return value. When it is False (the
|
| 1537 |
+
default) just the coefficients are returned, when True diagnostic
|
| 1538 |
+
information from the singular value decomposition is also returned.
|
| 1539 |
+
w : array_like, shape (`M`,), optional
|
| 1540 |
+
Weights. If not None, the weight ``w[i]`` applies to the unsquared
|
| 1541 |
+
residual ``y[i] - y_hat[i]`` at ``x[i]``. Ideally the weights are
|
| 1542 |
+
chosen so that the errors of the products ``w[i]*y[i]`` all have the
|
| 1543 |
+
same variance. When using inverse-variance weighting, use
|
| 1544 |
+
``w[i] = 1/sigma(y[i])``. The default value is None.
|
| 1545 |
+
|
| 1546 |
+
Returns
|
| 1547 |
+
-------
|
| 1548 |
+
coef : ndarray, shape (M,) or (M, K)
|
| 1549 |
+
Chebyshev coefficients ordered from low to high. If `y` was 2-D,
|
| 1550 |
+
the coefficients for the data in column k of `y` are in column
|
| 1551 |
+
`k`.
|
| 1552 |
+
|
| 1553 |
+
[residuals, rank, singular_values, rcond] : list
|
| 1554 |
+
These values are only returned if ``full == True``
|
| 1555 |
+
|
| 1556 |
+
- residuals -- sum of squared residuals of the least squares fit
|
| 1557 |
+
- rank -- the numerical rank of the scaled Vandermonde matrix
|
| 1558 |
+
- singular_values -- singular values of the scaled Vandermonde matrix
|
| 1559 |
+
- rcond -- value of `rcond`.
|
| 1560 |
+
|
| 1561 |
+
For more details, see `numpy.linalg.lstsq`.
|
| 1562 |
+
|
| 1563 |
+
Warns
|
| 1564 |
+
-----
|
| 1565 |
+
RankWarning
|
| 1566 |
+
The rank of the coefficient matrix in the least-squares fit is
|
| 1567 |
+
deficient. The warning is only raised if ``full == False``. The
|
| 1568 |
+
warnings can be turned off by
|
| 1569 |
+
|
| 1570 |
+
>>> import warnings
|
| 1571 |
+
>>> warnings.simplefilter('ignore', np.exceptions.RankWarning)
|
| 1572 |
+
|
| 1573 |
+
See Also
|
| 1574 |
+
--------
|
| 1575 |
+
numpy.polynomial.polynomial.polyfit
|
| 1576 |
+
numpy.polynomial.legendre.legfit
|
| 1577 |
+
numpy.polynomial.laguerre.lagfit
|
| 1578 |
+
numpy.polynomial.hermite.hermfit
|
| 1579 |
+
numpy.polynomial.hermite_e.hermefit
|
| 1580 |
+
chebval : Evaluates a Chebyshev series.
|
| 1581 |
+
chebvander : Vandermonde matrix of Chebyshev series.
|
| 1582 |
+
chebweight : Chebyshev weight function.
|
| 1583 |
+
numpy.linalg.lstsq : Computes a least-squares fit from the matrix.
|
| 1584 |
+
scipy.interpolate.UnivariateSpline : Computes spline fits.
|
| 1585 |
+
|
| 1586 |
+
Notes
|
| 1587 |
+
-----
|
| 1588 |
+
The solution is the coefficients of the Chebyshev series `p` that
|
| 1589 |
+
minimizes the sum of the weighted squared errors
|
| 1590 |
+
|
| 1591 |
+
.. math:: E = \\sum_j w_j^2 * |y_j - p(x_j)|^2,
|
| 1592 |
+
|
| 1593 |
+
where :math:`w_j` are the weights. This problem is solved by setting up
|
| 1594 |
+
as the (typically) overdetermined matrix equation
|
| 1595 |
+
|
| 1596 |
+
.. math:: V(x) * c = w * y,
|
| 1597 |
+
|
| 1598 |
+
where `V` is the weighted pseudo Vandermonde matrix of `x`, `c` are the
|
| 1599 |
+
coefficients to be solved for, `w` are the weights, and `y` are the
|
| 1600 |
+
observed values. This equation is then solved using the singular value
|
| 1601 |
+
decomposition of `V`.
|
| 1602 |
+
|
| 1603 |
+
If some of the singular values of `V` are so small that they are
|
| 1604 |
+
neglected, then a `~exceptions.RankWarning` will be issued. This means that
|
| 1605 |
+
the coefficient values may be poorly determined. Using a lower order fit
|
| 1606 |
+
will usually get rid of the warning. The `rcond` parameter can also be
|
| 1607 |
+
set to a value smaller than its default, but the resulting fit may be
|
| 1608 |
+
spurious and have large contributions from roundoff error.
|
| 1609 |
+
|
| 1610 |
+
Fits using Chebyshev series are usually better conditioned than fits
|
| 1611 |
+
using power series, but much can depend on the distribution of the
|
| 1612 |
+
sample points and the smoothness of the data. If the quality of the fit
|
| 1613 |
+
is inadequate splines may be a good alternative.
|
| 1614 |
+
|
| 1615 |
+
References
|
| 1616 |
+
----------
|
| 1617 |
+
.. [1] Wikipedia, "Curve fitting",
|
| 1618 |
+
https://en.wikipedia.org/wiki/Curve_fitting
|
| 1619 |
+
|
| 1620 |
+
Examples
|
| 1621 |
+
--------
|
| 1622 |
+
|
| 1623 |
+
"""
|
| 1624 |
+
return pu._fit(chebvander, x, y, deg, rcond, full, w)
|
| 1625 |
+
|
| 1626 |
+
|
| 1627 |
+
def chebcompanion(c):
|
| 1628 |
+
"""Return the scaled companion matrix of c.
|
| 1629 |
+
|
| 1630 |
+
The basis polynomials are scaled so that the companion matrix is
|
| 1631 |
+
symmetric when `c` is a Chebyshev basis polynomial. This provides
|
| 1632 |
+
better eigenvalue estimates than the unscaled case and for basis
|
| 1633 |
+
polynomials the eigenvalues are guaranteed to be real if
|
| 1634 |
+
`numpy.linalg.eigvalsh` is used to obtain them.
|
| 1635 |
+
|
| 1636 |
+
Parameters
|
| 1637 |
+
----------
|
| 1638 |
+
c : array_like
|
| 1639 |
+
1-D array of Chebyshev series coefficients ordered from low to high
|
| 1640 |
+
degree.
|
| 1641 |
+
|
| 1642 |
+
Returns
|
| 1643 |
+
-------
|
| 1644 |
+
mat : ndarray
|
| 1645 |
+
Scaled companion matrix of dimensions (deg, deg).
|
| 1646 |
+
"""
|
| 1647 |
+
# c is a trimmed copy
|
| 1648 |
+
[c] = pu.as_series([c])
|
| 1649 |
+
if len(c) < 2:
|
| 1650 |
+
raise ValueError('Series must have maximum degree of at least 1.')
|
| 1651 |
+
if len(c) == 2:
|
| 1652 |
+
return np.array([[-c[0] / c[1]]])
|
| 1653 |
+
|
| 1654 |
+
n = len(c) - 1
|
| 1655 |
+
mat = np.zeros((n, n), dtype=c.dtype)
|
| 1656 |
+
scl = np.array([1.] + [np.sqrt(.5)] * (n - 1))
|
| 1657 |
+
top = mat.reshape(-1)[1::n + 1]
|
| 1658 |
+
bot = mat.reshape(-1)[n::n + 1]
|
| 1659 |
+
top[0] = np.sqrt(.5)
|
| 1660 |
+
top[1:] = 1 / 2
|
| 1661 |
+
bot[...] = top
|
| 1662 |
+
mat[:, -1] -= (c[:-1] / c[-1]) * (scl / scl[-1]) * .5
|
| 1663 |
+
return mat
|
| 1664 |
+
|
| 1665 |
+
|
| 1666 |
+
def chebroots(c):
|
| 1667 |
+
"""
|
| 1668 |
+
Compute the roots of a Chebyshev series.
|
| 1669 |
+
|
| 1670 |
+
Return the roots (a.k.a. "zeros") of the polynomial
|
| 1671 |
+
|
| 1672 |
+
.. math:: p(x) = \\sum_i c[i] * T_i(x).
|
| 1673 |
+
|
| 1674 |
+
Parameters
|
| 1675 |
+
----------
|
| 1676 |
+
c : 1-D array_like
|
| 1677 |
+
1-D array of coefficients.
|
| 1678 |
+
|
| 1679 |
+
Returns
|
| 1680 |
+
-------
|
| 1681 |
+
out : ndarray
|
| 1682 |
+
Array of the roots of the series. If all the roots are real,
|
| 1683 |
+
then `out` is also real, otherwise it is complex.
|
| 1684 |
+
|
| 1685 |
+
See Also
|
| 1686 |
+
--------
|
| 1687 |
+
numpy.polynomial.polynomial.polyroots
|
| 1688 |
+
numpy.polynomial.legendre.legroots
|
| 1689 |
+
numpy.polynomial.laguerre.lagroots
|
| 1690 |
+
numpy.polynomial.hermite.hermroots
|
| 1691 |
+
numpy.polynomial.hermite_e.hermeroots
|
| 1692 |
+
|
| 1693 |
+
Notes
|
| 1694 |
+
-----
|
| 1695 |
+
The root estimates are obtained as the eigenvalues of the companion
|
| 1696 |
+
matrix, Roots far from the origin of the complex plane may have large
|
| 1697 |
+
errors due to the numerical instability of the series for such
|
| 1698 |
+
values. Roots with multiplicity greater than 1 will also show larger
|
| 1699 |
+
errors as the value of the series near such points is relatively
|
| 1700 |
+
insensitive to errors in the roots. Isolated roots near the origin can
|
| 1701 |
+
be improved by a few iterations of Newton's method.
|
| 1702 |
+
|
| 1703 |
+
The Chebyshev series basis polynomials aren't powers of `x` so the
|
| 1704 |
+
results of this function may seem unintuitive.
|
| 1705 |
+
|
| 1706 |
+
Examples
|
| 1707 |
+
--------
|
| 1708 |
+
>>> import numpy.polynomial.chebyshev as cheb
|
| 1709 |
+
>>> cheb.chebroots((-1, 1,-1, 1)) # T3 - T2 + T1 - T0 has real roots
|
| 1710 |
+
array([ -5.00000000e-01, 2.60860684e-17, 1.00000000e+00]) # may vary
|
| 1711 |
+
|
| 1712 |
+
"""
|
| 1713 |
+
# c is a trimmed copy
|
| 1714 |
+
[c] = pu.as_series([c])
|
| 1715 |
+
if len(c) < 2:
|
| 1716 |
+
return np.array([], dtype=c.dtype)
|
| 1717 |
+
if len(c) == 2:
|
| 1718 |
+
return np.array([-c[0] / c[1]])
|
| 1719 |
+
|
| 1720 |
+
# rotated companion matrix reduces error
|
| 1721 |
+
m = chebcompanion(c)[::-1, ::-1]
|
| 1722 |
+
r = np.linalg.eigvals(m)
|
| 1723 |
+
r.sort()
|
| 1724 |
+
return r
|
| 1725 |
+
|
| 1726 |
+
|
| 1727 |
+
def chebinterpolate(func, deg, args=()):
|
| 1728 |
+
"""Interpolate a function at the Chebyshev points of the first kind.
|
| 1729 |
+
|
| 1730 |
+
Returns the Chebyshev series that interpolates `func` at the Chebyshev
|
| 1731 |
+
points of the first kind in the interval [-1, 1]. The interpolating
|
| 1732 |
+
series tends to a minmax approximation to `func` with increasing `deg`
|
| 1733 |
+
if the function is continuous in the interval.
|
| 1734 |
+
|
| 1735 |
+
Parameters
|
| 1736 |
+
----------
|
| 1737 |
+
func : function
|
| 1738 |
+
The function to be approximated. It must be a function of a single
|
| 1739 |
+
variable of the form ``f(x, a, b, c...)``, where ``a, b, c...`` are
|
| 1740 |
+
extra arguments passed in the `args` parameter.
|
| 1741 |
+
deg : int
|
| 1742 |
+
Degree of the interpolating polynomial
|
| 1743 |
+
args : tuple, optional
|
| 1744 |
+
Extra arguments to be used in the function call. Default is no extra
|
| 1745 |
+
arguments.
|
| 1746 |
+
|
| 1747 |
+
Returns
|
| 1748 |
+
-------
|
| 1749 |
+
coef : ndarray, shape (deg + 1,)
|
| 1750 |
+
Chebyshev coefficients of the interpolating series ordered from low to
|
| 1751 |
+
high.
|
| 1752 |
+
|
| 1753 |
+
Examples
|
| 1754 |
+
--------
|
| 1755 |
+
>>> import numpy.polynomial.chebyshev as C
|
| 1756 |
+
>>> C.chebinterpolate(lambda x: np.tanh(x) + 0.5, 8)
|
| 1757 |
+
array([ 5.00000000e-01, 8.11675684e-01, -9.86864911e-17,
|
| 1758 |
+
-5.42457905e-02, -2.71387850e-16, 4.51658839e-03,
|
| 1759 |
+
2.46716228e-17, -3.79694221e-04, -3.26899002e-16])
|
| 1760 |
+
|
| 1761 |
+
Notes
|
| 1762 |
+
-----
|
| 1763 |
+
The Chebyshev polynomials used in the interpolation are orthogonal when
|
| 1764 |
+
sampled at the Chebyshev points of the first kind. If it is desired to
|
| 1765 |
+
constrain some of the coefficients they can simply be set to the desired
|
| 1766 |
+
value after the interpolation, no new interpolation or fit is needed. This
|
| 1767 |
+
is especially useful if it is known apriori that some of coefficients are
|
| 1768 |
+
zero. For instance, if the function is even then the coefficients of the
|
| 1769 |
+
terms of odd degree in the result can be set to zero.
|
| 1770 |
+
|
| 1771 |
+
"""
|
| 1772 |
+
deg = np.asarray(deg)
|
| 1773 |
+
|
| 1774 |
+
# check arguments.
|
| 1775 |
+
if deg.ndim > 0 or deg.dtype.kind not in 'iu' or deg.size == 0:
|
| 1776 |
+
raise TypeError("deg must be an int")
|
| 1777 |
+
if deg < 0:
|
| 1778 |
+
raise ValueError("expected deg >= 0")
|
| 1779 |
+
|
| 1780 |
+
order = deg + 1
|
| 1781 |
+
xcheb = chebpts1(order)
|
| 1782 |
+
yfunc = func(xcheb, *args)
|
| 1783 |
+
m = chebvander(xcheb, deg)
|
| 1784 |
+
c = np.dot(m.T, yfunc)
|
| 1785 |
+
c[0] /= order
|
| 1786 |
+
c[1:] /= 0.5 * order
|
| 1787 |
+
|
| 1788 |
+
return c
|
| 1789 |
+
|
| 1790 |
+
|
| 1791 |
+
def chebgauss(deg):
|
| 1792 |
+
"""
|
| 1793 |
+
Gauss-Chebyshev quadrature.
|
| 1794 |
+
|
| 1795 |
+
Computes the sample points and weights for Gauss-Chebyshev quadrature.
|
| 1796 |
+
These sample points and weights will correctly integrate polynomials of
|
| 1797 |
+
degree :math:`2*deg - 1` or less over the interval :math:`[-1, 1]` with
|
| 1798 |
+
the weight function :math:`f(x) = 1/\\sqrt{1 - x^2}`.
|
| 1799 |
+
|
| 1800 |
+
Parameters
|
| 1801 |
+
----------
|
| 1802 |
+
deg : int
|
| 1803 |
+
Number of sample points and weights. It must be >= 1.
|
| 1804 |
+
|
| 1805 |
+
Returns
|
| 1806 |
+
-------
|
| 1807 |
+
x : ndarray
|
| 1808 |
+
1-D ndarray containing the sample points.
|
| 1809 |
+
y : ndarray
|
| 1810 |
+
1-D ndarray containing the weights.
|
| 1811 |
+
|
| 1812 |
+
Notes
|
| 1813 |
+
-----
|
| 1814 |
+
The results have only been tested up to degree 100, higher degrees may
|
| 1815 |
+
be problematic. For Gauss-Chebyshev there are closed form solutions for
|
| 1816 |
+
the sample points and weights. If n = `deg`, then
|
| 1817 |
+
|
| 1818 |
+
.. math:: x_i = \\cos(\\pi (2 i - 1) / (2 n))
|
| 1819 |
+
|
| 1820 |
+
.. math:: w_i = \\pi / n
|
| 1821 |
+
|
| 1822 |
+
"""
|
| 1823 |
+
ideg = pu._as_int(deg, "deg")
|
| 1824 |
+
if ideg <= 0:
|
| 1825 |
+
raise ValueError("deg must be a positive integer")
|
| 1826 |
+
|
| 1827 |
+
x = np.cos(np.pi * np.arange(1, 2 * ideg, 2) / (2.0 * ideg))
|
| 1828 |
+
w = np.ones(ideg) * (np.pi / ideg)
|
| 1829 |
+
|
| 1830 |
+
return x, w
|
| 1831 |
+
|
| 1832 |
+
|
| 1833 |
+
def chebweight(x):
|
| 1834 |
+
"""
|
| 1835 |
+
The weight function of the Chebyshev polynomials.
|
| 1836 |
+
|
| 1837 |
+
The weight function is :math:`1/\\sqrt{1 - x^2}` and the interval of
|
| 1838 |
+
integration is :math:`[-1, 1]`. The Chebyshev polynomials are
|
| 1839 |
+
orthogonal, but not normalized, with respect to this weight function.
|
| 1840 |
+
|
| 1841 |
+
Parameters
|
| 1842 |
+
----------
|
| 1843 |
+
x : array_like
|
| 1844 |
+
Values at which the weight function will be computed.
|
| 1845 |
+
|
| 1846 |
+
Returns
|
| 1847 |
+
-------
|
| 1848 |
+
w : ndarray
|
| 1849 |
+
The weight function at `x`.
|
| 1850 |
+
"""
|
| 1851 |
+
w = 1. / (np.sqrt(1. + x) * np.sqrt(1. - x))
|
| 1852 |
+
return w
|
| 1853 |
+
|
| 1854 |
+
|
| 1855 |
+
def chebpts1(npts):
|
| 1856 |
+
"""
|
| 1857 |
+
Chebyshev points of the first kind.
|
| 1858 |
+
|
| 1859 |
+
The Chebyshev points of the first kind are the points ``cos(x)``,
|
| 1860 |
+
where ``x = [pi*(k + .5)/npts for k in range(npts)]``.
|
| 1861 |
+
|
| 1862 |
+
Parameters
|
| 1863 |
+
----------
|
| 1864 |
+
npts : int
|
| 1865 |
+
Number of sample points desired.
|
| 1866 |
+
|
| 1867 |
+
Returns
|
| 1868 |
+
-------
|
| 1869 |
+
pts : ndarray
|
| 1870 |
+
The Chebyshev points of the first kind.
|
| 1871 |
+
|
| 1872 |
+
See Also
|
| 1873 |
+
--------
|
| 1874 |
+
chebpts2
|
| 1875 |
+
"""
|
| 1876 |
+
_npts = int(npts)
|
| 1877 |
+
if _npts != npts:
|
| 1878 |
+
raise ValueError("npts must be integer")
|
| 1879 |
+
if _npts < 1:
|
| 1880 |
+
raise ValueError("npts must be >= 1")
|
| 1881 |
+
|
| 1882 |
+
x = 0.5 * np.pi / _npts * np.arange(-_npts + 1, _npts + 1, 2)
|
| 1883 |
+
return np.sin(x)
|
| 1884 |
+
|
| 1885 |
+
|
| 1886 |
+
def chebpts2(npts):
|
| 1887 |
+
"""
|
| 1888 |
+
Chebyshev points of the second kind.
|
| 1889 |
+
|
| 1890 |
+
The Chebyshev points of the second kind are the points ``cos(x)``,
|
| 1891 |
+
where ``x = [pi*k/(npts - 1) for k in range(npts)]`` sorted in ascending
|
| 1892 |
+
order.
|
| 1893 |
+
|
| 1894 |
+
Parameters
|
| 1895 |
+
----------
|
| 1896 |
+
npts : int
|
| 1897 |
+
Number of sample points desired.
|
| 1898 |
+
|
| 1899 |
+
Returns
|
| 1900 |
+
-------
|
| 1901 |
+
pts : ndarray
|
| 1902 |
+
The Chebyshev points of the second kind.
|
| 1903 |
+
"""
|
| 1904 |
+
_npts = int(npts)
|
| 1905 |
+
if _npts != npts:
|
| 1906 |
+
raise ValueError("npts must be integer")
|
| 1907 |
+
if _npts < 2:
|
| 1908 |
+
raise ValueError("npts must be >= 2")
|
| 1909 |
+
|
| 1910 |
+
x = np.linspace(-np.pi, 0, _npts)
|
| 1911 |
+
return np.cos(x)
|
| 1912 |
+
|
| 1913 |
+
|
| 1914 |
+
#
|
| 1915 |
+
# Chebyshev series class
|
| 1916 |
+
#
|
| 1917 |
+
|
| 1918 |
+
class Chebyshev(ABCPolyBase):
|
| 1919 |
+
"""A Chebyshev series class.
|
| 1920 |
+
|
| 1921 |
+
The Chebyshev class provides the standard Python numerical methods
|
| 1922 |
+
'+', '-', '*', '//', '%', 'divmod', '**', and '()' as well as the
|
| 1923 |
+
attributes and methods listed below.
|
| 1924 |
+
|
| 1925 |
+
Parameters
|
| 1926 |
+
----------
|
| 1927 |
+
coef : array_like
|
| 1928 |
+
Chebyshev coefficients in order of increasing degree, i.e.,
|
| 1929 |
+
``(1, 2, 3)`` gives ``1*T_0(x) + 2*T_1(x) + 3*T_2(x)``.
|
| 1930 |
+
domain : (2,) array_like, optional
|
| 1931 |
+
Domain to use. The interval ``[domain[0], domain[1]]`` is mapped
|
| 1932 |
+
to the interval ``[window[0], window[1]]`` by shifting and scaling.
|
| 1933 |
+
The default value is [-1., 1.].
|
| 1934 |
+
window : (2,) array_like, optional
|
| 1935 |
+
Window, see `domain` for its use. The default value is [-1., 1.].
|
| 1936 |
+
symbol : str, optional
|
| 1937 |
+
Symbol used to represent the independent variable in string
|
| 1938 |
+
representations of the polynomial expression, e.g. for printing.
|
| 1939 |
+
The symbol must be a valid Python identifier. Default value is 'x'.
|
| 1940 |
+
|
| 1941 |
+
.. versionadded:: 1.24
|
| 1942 |
+
|
| 1943 |
+
"""
|
| 1944 |
+
# Virtual Functions
|
| 1945 |
+
_add = staticmethod(chebadd)
|
| 1946 |
+
_sub = staticmethod(chebsub)
|
| 1947 |
+
_mul = staticmethod(chebmul)
|
| 1948 |
+
_div = staticmethod(chebdiv)
|
| 1949 |
+
_pow = staticmethod(chebpow)
|
| 1950 |
+
_val = staticmethod(chebval)
|
| 1951 |
+
_int = staticmethod(chebint)
|
| 1952 |
+
_der = staticmethod(chebder)
|
| 1953 |
+
_fit = staticmethod(chebfit)
|
| 1954 |
+
_line = staticmethod(chebline)
|
| 1955 |
+
_roots = staticmethod(chebroots)
|
| 1956 |
+
_fromroots = staticmethod(chebfromroots)
|
| 1957 |
+
|
| 1958 |
+
@classmethod
|
| 1959 |
+
def interpolate(cls, func, deg, domain=None, args=()):
|
| 1960 |
+
"""Interpolate a function at the Chebyshev points of the first kind.
|
| 1961 |
+
|
| 1962 |
+
Returns the series that interpolates `func` at the Chebyshev points of
|
| 1963 |
+
the first kind scaled and shifted to the `domain`. The resulting series
|
| 1964 |
+
tends to a minmax approximation of `func` when the function is
|
| 1965 |
+
continuous in the domain.
|
| 1966 |
+
|
| 1967 |
+
Parameters
|
| 1968 |
+
----------
|
| 1969 |
+
func : function
|
| 1970 |
+
The function to be interpolated. It must be a function of a single
|
| 1971 |
+
variable of the form ``f(x, a, b, c...)``, where ``a, b, c...`` are
|
| 1972 |
+
extra arguments passed in the `args` parameter.
|
| 1973 |
+
deg : int
|
| 1974 |
+
Degree of the interpolating polynomial.
|
| 1975 |
+
domain : {None, [beg, end]}, optional
|
| 1976 |
+
Domain over which `func` is interpolated. The default is None, in
|
| 1977 |
+
which case the domain is [-1, 1].
|
| 1978 |
+
args : tuple, optional
|
| 1979 |
+
Extra arguments to be used in the function call. Default is no
|
| 1980 |
+
extra arguments.
|
| 1981 |
+
|
| 1982 |
+
Returns
|
| 1983 |
+
-------
|
| 1984 |
+
polynomial : Chebyshev instance
|
| 1985 |
+
Interpolating Chebyshev instance.
|
| 1986 |
+
|
| 1987 |
+
Notes
|
| 1988 |
+
-----
|
| 1989 |
+
See `numpy.polynomial.chebinterpolate` for more details.
|
| 1990 |
+
|
| 1991 |
+
"""
|
| 1992 |
+
if domain is None:
|
| 1993 |
+
domain = cls.domain
|
| 1994 |
+
xfunc = lambda x: func(pu.mapdomain(x, cls.window, domain), *args)
|
| 1995 |
+
coef = chebinterpolate(xfunc, deg)
|
| 1996 |
+
return cls(coef, domain=domain)
|
| 1997 |
+
|
| 1998 |
+
# Virtual properties
|
| 1999 |
+
domain = np.array(chebdomain)
|
| 2000 |
+
window = np.array(chebdomain)
|
| 2001 |
+
basis_name = 'T'
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/chebyshev.pyi
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from _typeshed import ConvertibleToInt
|
| 2 |
+
from collections.abc import Callable, Iterable
|
| 3 |
+
from typing import (
|
| 4 |
+
Any,
|
| 5 |
+
ClassVar,
|
| 6 |
+
Concatenate,
|
| 7 |
+
Final,
|
| 8 |
+
Literal as L,
|
| 9 |
+
Self,
|
| 10 |
+
TypeVar,
|
| 11 |
+
overload,
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
import numpy as np
|
| 15 |
+
import numpy.typing as npt
|
| 16 |
+
from numpy._typing import _IntLike_co
|
| 17 |
+
|
| 18 |
+
from ._polybase import ABCPolyBase
|
| 19 |
+
from ._polytypes import (
|
| 20 |
+
_Array1,
|
| 21 |
+
_Array2,
|
| 22 |
+
_CoefSeries,
|
| 23 |
+
_FuncBinOp,
|
| 24 |
+
_FuncCompanion,
|
| 25 |
+
_FuncDer,
|
| 26 |
+
_FuncFit,
|
| 27 |
+
_FuncFromRoots,
|
| 28 |
+
_FuncGauss,
|
| 29 |
+
_FuncInteg,
|
| 30 |
+
_FuncLine,
|
| 31 |
+
_FuncPoly2Ortho,
|
| 32 |
+
_FuncPow,
|
| 33 |
+
_FuncRoots,
|
| 34 |
+
_FuncUnOp,
|
| 35 |
+
_FuncVal,
|
| 36 |
+
_FuncVal2D,
|
| 37 |
+
_FuncVal3D,
|
| 38 |
+
_FuncVander,
|
| 39 |
+
_FuncVander2D,
|
| 40 |
+
_FuncVander3D,
|
| 41 |
+
_FuncWeight,
|
| 42 |
+
_Series,
|
| 43 |
+
_SeriesLikeCoef_co,
|
| 44 |
+
)
|
| 45 |
+
from .polyutils import trimcoef as chebtrim
|
| 46 |
+
|
| 47 |
+
__all__ = [
|
| 48 |
+
"chebzero",
|
| 49 |
+
"chebone",
|
| 50 |
+
"chebx",
|
| 51 |
+
"chebdomain",
|
| 52 |
+
"chebline",
|
| 53 |
+
"chebadd",
|
| 54 |
+
"chebsub",
|
| 55 |
+
"chebmulx",
|
| 56 |
+
"chebmul",
|
| 57 |
+
"chebdiv",
|
| 58 |
+
"chebpow",
|
| 59 |
+
"chebval",
|
| 60 |
+
"chebder",
|
| 61 |
+
"chebint",
|
| 62 |
+
"cheb2poly",
|
| 63 |
+
"poly2cheb",
|
| 64 |
+
"chebfromroots",
|
| 65 |
+
"chebvander",
|
| 66 |
+
"chebfit",
|
| 67 |
+
"chebtrim",
|
| 68 |
+
"chebroots",
|
| 69 |
+
"chebpts1",
|
| 70 |
+
"chebpts2",
|
| 71 |
+
"Chebyshev",
|
| 72 |
+
"chebval2d",
|
| 73 |
+
"chebval3d",
|
| 74 |
+
"chebgrid2d",
|
| 75 |
+
"chebgrid3d",
|
| 76 |
+
"chebvander2d",
|
| 77 |
+
"chebvander3d",
|
| 78 |
+
"chebcompanion",
|
| 79 |
+
"chebgauss",
|
| 80 |
+
"chebweight",
|
| 81 |
+
"chebinterpolate",
|
| 82 |
+
]
|
| 83 |
+
|
| 84 |
+
_NumberOrObjectT = TypeVar("_NumberOrObjectT", bound=np.number | np.object_)
|
| 85 |
+
_CoefScalarT = TypeVar("_CoefScalarT", bound=np.number | np.bool | np.object_)
|
| 86 |
+
|
| 87 |
+
def _cseries_to_zseries(c: npt.NDArray[_NumberOrObjectT]) -> _Series[_NumberOrObjectT]: ...
|
| 88 |
+
def _zseries_to_cseries(zs: npt.NDArray[_NumberOrObjectT]) -> _Series[_NumberOrObjectT]: ...
|
| 89 |
+
def _zseries_mul(z1: npt.NDArray[_NumberOrObjectT], z2: npt.NDArray[_NumberOrObjectT]) -> _Series[_NumberOrObjectT]: ...
|
| 90 |
+
def _zseries_div(z1: npt.NDArray[_NumberOrObjectT], z2: npt.NDArray[_NumberOrObjectT]) -> _Series[_NumberOrObjectT]: ...
|
| 91 |
+
def _zseries_der(zs: npt.NDArray[_NumberOrObjectT]) -> _Series[_NumberOrObjectT]: ...
|
| 92 |
+
def _zseries_int(zs: npt.NDArray[_NumberOrObjectT]) -> _Series[_NumberOrObjectT]: ...
|
| 93 |
+
|
| 94 |
+
poly2cheb: Final[_FuncPoly2Ortho] = ...
|
| 95 |
+
cheb2poly: Final[_FuncUnOp] = ...
|
| 96 |
+
|
| 97 |
+
chebdomain: Final[_Array2[np.float64]] = ...
|
| 98 |
+
chebzero: Final[_Array1[np.int_]] = ...
|
| 99 |
+
chebone: Final[_Array1[np.int_]] = ...
|
| 100 |
+
chebx: Final[_Array2[np.int_]] = ...
|
| 101 |
+
|
| 102 |
+
chebline: Final[_FuncLine] = ...
|
| 103 |
+
chebfromroots: Final[_FuncFromRoots] = ...
|
| 104 |
+
chebadd: Final[_FuncBinOp] = ...
|
| 105 |
+
chebsub: Final[_FuncBinOp] = ...
|
| 106 |
+
chebmulx: Final[_FuncUnOp] = ...
|
| 107 |
+
chebmul: Final[_FuncBinOp] = ...
|
| 108 |
+
chebdiv: Final[_FuncBinOp] = ...
|
| 109 |
+
chebpow: Final[_FuncPow] = ...
|
| 110 |
+
chebder: Final[_FuncDer] = ...
|
| 111 |
+
chebint: Final[_FuncInteg] = ...
|
| 112 |
+
chebval: Final[_FuncVal] = ...
|
| 113 |
+
chebval2d: Final[_FuncVal2D] = ...
|
| 114 |
+
chebval3d: Final[_FuncVal3D] = ...
|
| 115 |
+
chebgrid2d: Final[_FuncVal2D] = ...
|
| 116 |
+
chebgrid3d: Final[_FuncVal3D] = ...
|
| 117 |
+
chebvander: Final[_FuncVander] = ...
|
| 118 |
+
chebvander2d: Final[_FuncVander2D] = ...
|
| 119 |
+
chebvander3d: Final[_FuncVander3D] = ...
|
| 120 |
+
chebfit: Final[_FuncFit] = ...
|
| 121 |
+
chebcompanion: Final[_FuncCompanion] = ...
|
| 122 |
+
chebroots: Final[_FuncRoots] = ...
|
| 123 |
+
chebgauss: Final[_FuncGauss] = ...
|
| 124 |
+
chebweight: Final[_FuncWeight] = ...
|
| 125 |
+
def chebpts1(npts: ConvertibleToInt) -> np.ndarray[tuple[int], np.dtype[np.float64]]: ...
|
| 126 |
+
def chebpts2(npts: ConvertibleToInt) -> np.ndarray[tuple[int], np.dtype[np.float64]]: ...
|
| 127 |
+
|
| 128 |
+
# keep in sync with `Chebyshev.interpolate` (minus `domain` parameter)
|
| 129 |
+
@overload
|
| 130 |
+
def chebinterpolate(
|
| 131 |
+
func: np.ufunc,
|
| 132 |
+
deg: _IntLike_co,
|
| 133 |
+
args: tuple[()] = (),
|
| 134 |
+
) -> npt.NDArray[np.float64 | np.complex128 | np.object_]: ...
|
| 135 |
+
@overload
|
| 136 |
+
def chebinterpolate(
|
| 137 |
+
func: Callable[[npt.NDArray[np.float64]], _CoefScalarT],
|
| 138 |
+
deg: _IntLike_co,
|
| 139 |
+
args: tuple[()] = (),
|
| 140 |
+
) -> npt.NDArray[_CoefScalarT]: ...
|
| 141 |
+
@overload
|
| 142 |
+
def chebinterpolate(
|
| 143 |
+
func: Callable[Concatenate[npt.NDArray[np.float64], ...], _CoefScalarT],
|
| 144 |
+
deg: _IntLike_co,
|
| 145 |
+
args: Iterable[Any],
|
| 146 |
+
) -> npt.NDArray[_CoefScalarT]: ...
|
| 147 |
+
|
| 148 |
+
class Chebyshev(ABCPolyBase[L["T"]]):
|
| 149 |
+
basis_name: ClassVar[L["T"]] = "T" # pyright: ignore[reportIncompatibleMethodOverride]
|
| 150 |
+
domain: _Array2[np.float64 | Any] = ... # pyright: ignore[reportIncompatibleMethodOverride]
|
| 151 |
+
window: _Array2[np.float64 | Any] = ... # pyright: ignore[reportIncompatibleMethodOverride]
|
| 152 |
+
|
| 153 |
+
@overload
|
| 154 |
+
@classmethod
|
| 155 |
+
def interpolate(
|
| 156 |
+
cls,
|
| 157 |
+
func: Callable[[npt.NDArray[np.float64]], _CoefSeries],
|
| 158 |
+
deg: _IntLike_co,
|
| 159 |
+
domain: _SeriesLikeCoef_co | None = None,
|
| 160 |
+
args: tuple[()] = (),
|
| 161 |
+
) -> Self: ...
|
| 162 |
+
@overload
|
| 163 |
+
@classmethod
|
| 164 |
+
def interpolate(
|
| 165 |
+
cls,
|
| 166 |
+
func: Callable[Concatenate[npt.NDArray[np.float64], ...], _CoefSeries],
|
| 167 |
+
deg: _IntLike_co,
|
| 168 |
+
domain: _SeriesLikeCoef_co | None = None,
|
| 169 |
+
*,
|
| 170 |
+
args: Iterable[Any],
|
| 171 |
+
) -> Self: ...
|
| 172 |
+
@overload
|
| 173 |
+
@classmethod
|
| 174 |
+
def interpolate(
|
| 175 |
+
cls,
|
| 176 |
+
func: Callable[Concatenate[npt.NDArray[np.float64], ...], _CoefSeries],
|
| 177 |
+
deg: _IntLike_co,
|
| 178 |
+
domain: _SeriesLikeCoef_co | None,
|
| 179 |
+
args: Iterable[Any],
|
| 180 |
+
) -> Self: ...
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/hermite.py
ADDED
|
@@ -0,0 +1,1738 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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|
+
==============================================================
|
| 3 |
+
Hermite Series, "Physicists" (:mod:`numpy.polynomial.hermite`)
|
| 4 |
+
==============================================================
|
| 5 |
+
|
| 6 |
+
This module provides a number of objects (mostly functions) useful for
|
| 7 |
+
dealing with Hermite series, including a `Hermite` class that
|
| 8 |
+
encapsulates the usual arithmetic operations. (General information
|
| 9 |
+
on how this module represents and works with such polynomials is in the
|
| 10 |
+
docstring for its "parent" sub-package, `numpy.polynomial`).
|
| 11 |
+
|
| 12 |
+
Classes
|
| 13 |
+
-------
|
| 14 |
+
.. autosummary::
|
| 15 |
+
:toctree: generated/
|
| 16 |
+
|
| 17 |
+
Hermite
|
| 18 |
+
|
| 19 |
+
Constants
|
| 20 |
+
---------
|
| 21 |
+
.. autosummary::
|
| 22 |
+
:toctree: generated/
|
| 23 |
+
|
| 24 |
+
hermdomain
|
| 25 |
+
hermzero
|
| 26 |
+
hermone
|
| 27 |
+
hermx
|
| 28 |
+
|
| 29 |
+
Arithmetic
|
| 30 |
+
----------
|
| 31 |
+
.. autosummary::
|
| 32 |
+
:toctree: generated/
|
| 33 |
+
|
| 34 |
+
hermadd
|
| 35 |
+
hermsub
|
| 36 |
+
hermmulx
|
| 37 |
+
hermmul
|
| 38 |
+
hermdiv
|
| 39 |
+
hermpow
|
| 40 |
+
hermval
|
| 41 |
+
hermval2d
|
| 42 |
+
hermval3d
|
| 43 |
+
hermgrid2d
|
| 44 |
+
hermgrid3d
|
| 45 |
+
|
| 46 |
+
Calculus
|
| 47 |
+
--------
|
| 48 |
+
.. autosummary::
|
| 49 |
+
:toctree: generated/
|
| 50 |
+
|
| 51 |
+
hermder
|
| 52 |
+
hermint
|
| 53 |
+
|
| 54 |
+
Misc Functions
|
| 55 |
+
--------------
|
| 56 |
+
.. autosummary::
|
| 57 |
+
:toctree: generated/
|
| 58 |
+
|
| 59 |
+
hermfromroots
|
| 60 |
+
hermroots
|
| 61 |
+
hermvander
|
| 62 |
+
hermvander2d
|
| 63 |
+
hermvander3d
|
| 64 |
+
hermgauss
|
| 65 |
+
hermweight
|
| 66 |
+
hermcompanion
|
| 67 |
+
hermfit
|
| 68 |
+
hermtrim
|
| 69 |
+
hermline
|
| 70 |
+
herm2poly
|
| 71 |
+
poly2herm
|
| 72 |
+
|
| 73 |
+
See also
|
| 74 |
+
--------
|
| 75 |
+
`numpy.polynomial`
|
| 76 |
+
|
| 77 |
+
"""
|
| 78 |
+
import numpy as np
|
| 79 |
+
|
| 80 |
+
from . import polyutils as pu
|
| 81 |
+
from ._polybase import ABCPolyBase
|
| 82 |
+
|
| 83 |
+
__all__ = [
|
| 84 |
+
'hermzero', 'hermone', 'hermx', 'hermdomain', 'hermline', 'hermadd',
|
| 85 |
+
'hermsub', 'hermmulx', 'hermmul', 'hermdiv', 'hermpow', 'hermval',
|
| 86 |
+
'hermder', 'hermint', 'herm2poly', 'poly2herm', 'hermfromroots',
|
| 87 |
+
'hermvander', 'hermfit', 'hermtrim', 'hermroots', 'Hermite',
|
| 88 |
+
'hermval2d', 'hermval3d', 'hermgrid2d', 'hermgrid3d', 'hermvander2d',
|
| 89 |
+
'hermvander3d', 'hermcompanion', 'hermgauss', 'hermweight']
|
| 90 |
+
|
| 91 |
+
hermtrim = pu.trimcoef
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def poly2herm(pol):
|
| 95 |
+
"""
|
| 96 |
+
poly2herm(pol)
|
| 97 |
+
|
| 98 |
+
Convert a polynomial to a Hermite series.
|
| 99 |
+
|
| 100 |
+
Convert an array representing the coefficients of a polynomial (relative
|
| 101 |
+
to the "standard" basis) ordered from lowest degree to highest, to an
|
| 102 |
+
array of the coefficients of the equivalent Hermite series, ordered
|
| 103 |
+
from lowest to highest degree.
|
| 104 |
+
|
| 105 |
+
Parameters
|
| 106 |
+
----------
|
| 107 |
+
pol : array_like
|
| 108 |
+
1-D array containing the polynomial coefficients
|
| 109 |
+
|
| 110 |
+
Returns
|
| 111 |
+
-------
|
| 112 |
+
c : ndarray
|
| 113 |
+
1-D array containing the coefficients of the equivalent Hermite
|
| 114 |
+
series.
|
| 115 |
+
|
| 116 |
+
See Also
|
| 117 |
+
--------
|
| 118 |
+
herm2poly
|
| 119 |
+
|
| 120 |
+
Notes
|
| 121 |
+
-----
|
| 122 |
+
The easy way to do conversions between polynomial basis sets
|
| 123 |
+
is to use the convert method of a class instance.
|
| 124 |
+
|
| 125 |
+
Examples
|
| 126 |
+
--------
|
| 127 |
+
>>> from numpy.polynomial.hermite import poly2herm
|
| 128 |
+
>>> poly2herm(np.arange(4))
|
| 129 |
+
array([1. , 2.75 , 0.5 , 0.375])
|
| 130 |
+
|
| 131 |
+
"""
|
| 132 |
+
[pol] = pu.as_series([pol])
|
| 133 |
+
deg = len(pol) - 1
|
| 134 |
+
res = 0
|
| 135 |
+
for i in range(deg, -1, -1):
|
| 136 |
+
res = hermadd(hermmulx(res), pol[i])
|
| 137 |
+
return res
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
def herm2poly(c):
|
| 141 |
+
"""
|
| 142 |
+
Convert a Hermite series to a polynomial.
|
| 143 |
+
|
| 144 |
+
Convert an array representing the coefficients of a Hermite series,
|
| 145 |
+
ordered from lowest degree to highest, to an array of the coefficients
|
| 146 |
+
of the equivalent polynomial (relative to the "standard" basis) ordered
|
| 147 |
+
from lowest to highest degree.
|
| 148 |
+
|
| 149 |
+
Parameters
|
| 150 |
+
----------
|
| 151 |
+
c : array_like
|
| 152 |
+
1-D array containing the Hermite series coefficients, ordered
|
| 153 |
+
from lowest order term to highest.
|
| 154 |
+
|
| 155 |
+
Returns
|
| 156 |
+
-------
|
| 157 |
+
pol : ndarray
|
| 158 |
+
1-D array containing the coefficients of the equivalent polynomial
|
| 159 |
+
(relative to the "standard" basis) ordered from lowest order term
|
| 160 |
+
to highest.
|
| 161 |
+
|
| 162 |
+
See Also
|
| 163 |
+
--------
|
| 164 |
+
poly2herm
|
| 165 |
+
|
| 166 |
+
Notes
|
| 167 |
+
-----
|
| 168 |
+
The easy way to do conversions between polynomial basis sets
|
| 169 |
+
is to use the convert method of a class instance.
|
| 170 |
+
|
| 171 |
+
Examples
|
| 172 |
+
--------
|
| 173 |
+
>>> from numpy.polynomial.hermite import herm2poly
|
| 174 |
+
>>> herm2poly([ 1. , 2.75 , 0.5 , 0.375])
|
| 175 |
+
array([0., 1., 2., 3.])
|
| 176 |
+
|
| 177 |
+
"""
|
| 178 |
+
from .polynomial import polyadd, polymulx, polysub
|
| 179 |
+
|
| 180 |
+
[c] = pu.as_series([c])
|
| 181 |
+
n = len(c)
|
| 182 |
+
if n == 1:
|
| 183 |
+
return c
|
| 184 |
+
if n == 2:
|
| 185 |
+
c[1] *= 2
|
| 186 |
+
return c
|
| 187 |
+
else:
|
| 188 |
+
c0 = c[-2]
|
| 189 |
+
c1 = c[-1]
|
| 190 |
+
# i is the current degree of c1
|
| 191 |
+
for i in range(n - 1, 1, -1):
|
| 192 |
+
tmp = c0
|
| 193 |
+
c0 = polysub(c[i - 2], c1 * (2 * (i - 1)))
|
| 194 |
+
c1 = polyadd(tmp, polymulx(c1) * 2)
|
| 195 |
+
return polyadd(c0, polymulx(c1) * 2)
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
#
|
| 199 |
+
# These are constant arrays are of integer type so as to be compatible
|
| 200 |
+
# with the widest range of other types, such as Decimal.
|
| 201 |
+
#
|
| 202 |
+
|
| 203 |
+
# Hermite
|
| 204 |
+
hermdomain = np.array([-1., 1.])
|
| 205 |
+
|
| 206 |
+
# Hermite coefficients representing zero.
|
| 207 |
+
hermzero = np.array([0])
|
| 208 |
+
|
| 209 |
+
# Hermite coefficients representing one.
|
| 210 |
+
hermone = np.array([1])
|
| 211 |
+
|
| 212 |
+
# Hermite coefficients representing the identity x.
|
| 213 |
+
hermx = np.array([0, 1 / 2])
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
def hermline(off, scl):
|
| 217 |
+
"""
|
| 218 |
+
Hermite series whose graph is a straight line.
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
Parameters
|
| 223 |
+
----------
|
| 224 |
+
off, scl : scalars
|
| 225 |
+
The specified line is given by ``off + scl*x``.
|
| 226 |
+
|
| 227 |
+
Returns
|
| 228 |
+
-------
|
| 229 |
+
y : ndarray
|
| 230 |
+
This module's representation of the Hermite series for
|
| 231 |
+
``off + scl*x``.
|
| 232 |
+
|
| 233 |
+
See Also
|
| 234 |
+
--------
|
| 235 |
+
numpy.polynomial.polynomial.polyline
|
| 236 |
+
numpy.polynomial.chebyshev.chebline
|
| 237 |
+
numpy.polynomial.legendre.legline
|
| 238 |
+
numpy.polynomial.laguerre.lagline
|
| 239 |
+
numpy.polynomial.hermite_e.hermeline
|
| 240 |
+
|
| 241 |
+
Examples
|
| 242 |
+
--------
|
| 243 |
+
>>> from numpy.polynomial.hermite import hermline, hermval
|
| 244 |
+
>>> hermval(0,hermline(3, 2))
|
| 245 |
+
3.0
|
| 246 |
+
>>> hermval(1,hermline(3, 2))
|
| 247 |
+
5.0
|
| 248 |
+
|
| 249 |
+
"""
|
| 250 |
+
if scl != 0:
|
| 251 |
+
return np.array([off, scl / 2])
|
| 252 |
+
else:
|
| 253 |
+
return np.array([off])
|
| 254 |
+
|
| 255 |
+
|
| 256 |
+
def hermfromroots(roots):
|
| 257 |
+
"""
|
| 258 |
+
Generate a Hermite series with given roots.
|
| 259 |
+
|
| 260 |
+
The function returns the coefficients of the polynomial
|
| 261 |
+
|
| 262 |
+
.. math:: p(x) = (x - r_0) * (x - r_1) * ... * (x - r_n),
|
| 263 |
+
|
| 264 |
+
in Hermite form, where the :math:`r_n` are the roots specified in `roots`.
|
| 265 |
+
If a zero has multiplicity n, then it must appear in `roots` n times.
|
| 266 |
+
For instance, if 2 is a root of multiplicity three and 3 is a root of
|
| 267 |
+
multiplicity 2, then `roots` looks something like [2, 2, 2, 3, 3]. The
|
| 268 |
+
roots can appear in any order.
|
| 269 |
+
|
| 270 |
+
If the returned coefficients are `c`, then
|
| 271 |
+
|
| 272 |
+
.. math:: p(x) = c_0 + c_1 * H_1(x) + ... + c_n * H_n(x)
|
| 273 |
+
|
| 274 |
+
The coefficient of the last term is not generally 1 for monic
|
| 275 |
+
polynomials in Hermite form.
|
| 276 |
+
|
| 277 |
+
Parameters
|
| 278 |
+
----------
|
| 279 |
+
roots : array_like
|
| 280 |
+
Sequence containing the roots.
|
| 281 |
+
|
| 282 |
+
Returns
|
| 283 |
+
-------
|
| 284 |
+
out : ndarray
|
| 285 |
+
1-D array of coefficients. If all roots are real then `out` is a
|
| 286 |
+
real array, if some of the roots are complex, then `out` is complex
|
| 287 |
+
even if all the coefficients in the result are real (see Examples
|
| 288 |
+
below).
|
| 289 |
+
|
| 290 |
+
See Also
|
| 291 |
+
--------
|
| 292 |
+
numpy.polynomial.polynomial.polyfromroots
|
| 293 |
+
numpy.polynomial.legendre.legfromroots
|
| 294 |
+
numpy.polynomial.laguerre.lagfromroots
|
| 295 |
+
numpy.polynomial.chebyshev.chebfromroots
|
| 296 |
+
numpy.polynomial.hermite_e.hermefromroots
|
| 297 |
+
|
| 298 |
+
Examples
|
| 299 |
+
--------
|
| 300 |
+
>>> from numpy.polynomial.hermite import hermfromroots, hermval
|
| 301 |
+
>>> coef = hermfromroots((-1, 0, 1))
|
| 302 |
+
>>> hermval((-1, 0, 1), coef)
|
| 303 |
+
array([0., 0., 0.])
|
| 304 |
+
>>> coef = hermfromroots((-1j, 1j))
|
| 305 |
+
>>> hermval((-1j, 1j), coef)
|
| 306 |
+
array([0.+0.j, 0.+0.j])
|
| 307 |
+
|
| 308 |
+
"""
|
| 309 |
+
return pu._fromroots(hermline, hermmul, roots)
|
| 310 |
+
|
| 311 |
+
|
| 312 |
+
def hermadd(c1, c2):
|
| 313 |
+
"""
|
| 314 |
+
Add one Hermite series to another.
|
| 315 |
+
|
| 316 |
+
Returns the sum of two Hermite series `c1` + `c2`. The arguments
|
| 317 |
+
are sequences of coefficients ordered from lowest order term to
|
| 318 |
+
highest, i.e., [1,2,3] represents the series ``P_0 + 2*P_1 + 3*P_2``.
|
| 319 |
+
|
| 320 |
+
Parameters
|
| 321 |
+
----------
|
| 322 |
+
c1, c2 : array_like
|
| 323 |
+
1-D arrays of Hermite series coefficients ordered from low to
|
| 324 |
+
high.
|
| 325 |
+
|
| 326 |
+
Returns
|
| 327 |
+
-------
|
| 328 |
+
out : ndarray
|
| 329 |
+
Array representing the Hermite series of their sum.
|
| 330 |
+
|
| 331 |
+
See Also
|
| 332 |
+
--------
|
| 333 |
+
hermsub, hermmulx, hermmul, hermdiv, hermpow
|
| 334 |
+
|
| 335 |
+
Notes
|
| 336 |
+
-----
|
| 337 |
+
Unlike multiplication, division, etc., the sum of two Hermite series
|
| 338 |
+
is a Hermite series (without having to "reproject" the result onto
|
| 339 |
+
the basis set) so addition, just like that of "standard" polynomials,
|
| 340 |
+
is simply "component-wise."
|
| 341 |
+
|
| 342 |
+
Examples
|
| 343 |
+
--------
|
| 344 |
+
>>> from numpy.polynomial.hermite import hermadd
|
| 345 |
+
>>> hermadd([1, 2, 3], [1, 2, 3, 4])
|
| 346 |
+
array([2., 4., 6., 4.])
|
| 347 |
+
|
| 348 |
+
"""
|
| 349 |
+
return pu._add(c1, c2)
|
| 350 |
+
|
| 351 |
+
|
| 352 |
+
def hermsub(c1, c2):
|
| 353 |
+
"""
|
| 354 |
+
Subtract one Hermite series from another.
|
| 355 |
+
|
| 356 |
+
Returns the difference of two Hermite series `c1` - `c2`. The
|
| 357 |
+
sequences of coefficients are from lowest order term to highest, i.e.,
|
| 358 |
+
[1,2,3] represents the series ``P_0 + 2*P_1 + 3*P_2``.
|
| 359 |
+
|
| 360 |
+
Parameters
|
| 361 |
+
----------
|
| 362 |
+
c1, c2 : array_like
|
| 363 |
+
1-D arrays of Hermite series coefficients ordered from low to
|
| 364 |
+
high.
|
| 365 |
+
|
| 366 |
+
Returns
|
| 367 |
+
-------
|
| 368 |
+
out : ndarray
|
| 369 |
+
Of Hermite series coefficients representing their difference.
|
| 370 |
+
|
| 371 |
+
See Also
|
| 372 |
+
--------
|
| 373 |
+
hermadd, hermmulx, hermmul, hermdiv, hermpow
|
| 374 |
+
|
| 375 |
+
Notes
|
| 376 |
+
-----
|
| 377 |
+
Unlike multiplication, division, etc., the difference of two Hermite
|
| 378 |
+
series is a Hermite series (without having to "reproject" the result
|
| 379 |
+
onto the basis set) so subtraction, just like that of "standard"
|
| 380 |
+
polynomials, is simply "component-wise."
|
| 381 |
+
|
| 382 |
+
Examples
|
| 383 |
+
--------
|
| 384 |
+
>>> from numpy.polynomial.hermite import hermsub
|
| 385 |
+
>>> hermsub([1, 2, 3, 4], [1, 2, 3])
|
| 386 |
+
array([0., 0., 0., 4.])
|
| 387 |
+
|
| 388 |
+
"""
|
| 389 |
+
return pu._sub(c1, c2)
|
| 390 |
+
|
| 391 |
+
|
| 392 |
+
def hermmulx(c):
|
| 393 |
+
"""Multiply a Hermite series by x.
|
| 394 |
+
|
| 395 |
+
Multiply the Hermite series `c` by x, where x is the independent
|
| 396 |
+
variable.
|
| 397 |
+
|
| 398 |
+
|
| 399 |
+
Parameters
|
| 400 |
+
----------
|
| 401 |
+
c : array_like
|
| 402 |
+
1-D array of Hermite series coefficients ordered from low to
|
| 403 |
+
high.
|
| 404 |
+
|
| 405 |
+
Returns
|
| 406 |
+
-------
|
| 407 |
+
out : ndarray
|
| 408 |
+
Array representing the result of the multiplication.
|
| 409 |
+
|
| 410 |
+
See Also
|
| 411 |
+
--------
|
| 412 |
+
hermadd, hermsub, hermmul, hermdiv, hermpow
|
| 413 |
+
|
| 414 |
+
Notes
|
| 415 |
+
-----
|
| 416 |
+
The multiplication uses the recursion relationship for Hermite
|
| 417 |
+
polynomials in the form
|
| 418 |
+
|
| 419 |
+
.. math::
|
| 420 |
+
|
| 421 |
+
xP_i(x) = (P_{i + 1}(x)/2 + i*P_{i - 1}(x))
|
| 422 |
+
|
| 423 |
+
Examples
|
| 424 |
+
--------
|
| 425 |
+
>>> from numpy.polynomial.hermite import hermmulx
|
| 426 |
+
>>> hermmulx([1, 2, 3])
|
| 427 |
+
array([2. , 6.5, 1. , 1.5])
|
| 428 |
+
|
| 429 |
+
"""
|
| 430 |
+
# c is a trimmed copy
|
| 431 |
+
[c] = pu.as_series([c])
|
| 432 |
+
# The zero series needs special treatment
|
| 433 |
+
if len(c) == 1 and c[0] == 0:
|
| 434 |
+
return c
|
| 435 |
+
|
| 436 |
+
prd = np.empty(len(c) + 1, dtype=c.dtype)
|
| 437 |
+
prd[0] = c[0] * 0
|
| 438 |
+
prd[1] = c[0] / 2
|
| 439 |
+
for i in range(1, len(c)):
|
| 440 |
+
prd[i + 1] = c[i] / 2
|
| 441 |
+
prd[i - 1] += c[i] * i
|
| 442 |
+
return prd
|
| 443 |
+
|
| 444 |
+
|
| 445 |
+
def hermmul(c1, c2):
|
| 446 |
+
"""
|
| 447 |
+
Multiply one Hermite series by another.
|
| 448 |
+
|
| 449 |
+
Returns the product of two Hermite series `c1` * `c2`. The arguments
|
| 450 |
+
are sequences of coefficients, from lowest order "term" to highest,
|
| 451 |
+
e.g., [1,2,3] represents the series ``P_0 + 2*P_1 + 3*P_2``.
|
| 452 |
+
|
| 453 |
+
Parameters
|
| 454 |
+
----------
|
| 455 |
+
c1, c2 : array_like
|
| 456 |
+
1-D arrays of Hermite series coefficients ordered from low to
|
| 457 |
+
high.
|
| 458 |
+
|
| 459 |
+
Returns
|
| 460 |
+
-------
|
| 461 |
+
out : ndarray
|
| 462 |
+
Of Hermite series coefficients representing their product.
|
| 463 |
+
|
| 464 |
+
See Also
|
| 465 |
+
--------
|
| 466 |
+
hermadd, hermsub, hermmulx, hermdiv, hermpow
|
| 467 |
+
|
| 468 |
+
Notes
|
| 469 |
+
-----
|
| 470 |
+
In general, the (polynomial) product of two C-series results in terms
|
| 471 |
+
that are not in the Hermite polynomial basis set. Thus, to express
|
| 472 |
+
the product as a Hermite series, it is necessary to "reproject" the
|
| 473 |
+
product onto said basis set, which may produce "unintuitive" (but
|
| 474 |
+
correct) results; see Examples section below.
|
| 475 |
+
|
| 476 |
+
Examples
|
| 477 |
+
--------
|
| 478 |
+
>>> from numpy.polynomial.hermite import hermmul
|
| 479 |
+
>>> hermmul([1, 2, 3], [0, 1, 2])
|
| 480 |
+
array([52., 29., 52., 7., 6.])
|
| 481 |
+
|
| 482 |
+
"""
|
| 483 |
+
# s1, s2 are trimmed copies
|
| 484 |
+
[c1, c2] = pu.as_series([c1, c2])
|
| 485 |
+
|
| 486 |
+
if len(c1) > len(c2):
|
| 487 |
+
c = c2
|
| 488 |
+
xs = c1
|
| 489 |
+
else:
|
| 490 |
+
c = c1
|
| 491 |
+
xs = c2
|
| 492 |
+
|
| 493 |
+
if len(c) == 1:
|
| 494 |
+
c0 = c[0] * xs
|
| 495 |
+
c1 = 0
|
| 496 |
+
elif len(c) == 2:
|
| 497 |
+
c0 = c[0] * xs
|
| 498 |
+
c1 = c[1] * xs
|
| 499 |
+
else:
|
| 500 |
+
nd = len(c)
|
| 501 |
+
c0 = c[-2] * xs
|
| 502 |
+
c1 = c[-1] * xs
|
| 503 |
+
for i in range(3, len(c) + 1):
|
| 504 |
+
tmp = c0
|
| 505 |
+
nd = nd - 1
|
| 506 |
+
c0 = hermsub(c[-i] * xs, c1 * (2 * (nd - 1)))
|
| 507 |
+
c1 = hermadd(tmp, hermmulx(c1) * 2)
|
| 508 |
+
return hermadd(c0, hermmulx(c1) * 2)
|
| 509 |
+
|
| 510 |
+
|
| 511 |
+
def hermdiv(c1, c2):
|
| 512 |
+
"""
|
| 513 |
+
Divide one Hermite series by another.
|
| 514 |
+
|
| 515 |
+
Returns the quotient-with-remainder of two Hermite series
|
| 516 |
+
`c1` / `c2`. The arguments are sequences of coefficients from lowest
|
| 517 |
+
order "term" to highest, e.g., [1,2,3] represents the series
|
| 518 |
+
``P_0 + 2*P_1 + 3*P_2``.
|
| 519 |
+
|
| 520 |
+
Parameters
|
| 521 |
+
----------
|
| 522 |
+
c1, c2 : array_like
|
| 523 |
+
1-D arrays of Hermite series coefficients ordered from low to
|
| 524 |
+
high.
|
| 525 |
+
|
| 526 |
+
Returns
|
| 527 |
+
-------
|
| 528 |
+
[quo, rem] : ndarrays
|
| 529 |
+
Of Hermite series coefficients representing the quotient and
|
| 530 |
+
remainder.
|
| 531 |
+
|
| 532 |
+
See Also
|
| 533 |
+
--------
|
| 534 |
+
hermadd, hermsub, hermmulx, hermmul, hermpow
|
| 535 |
+
|
| 536 |
+
Notes
|
| 537 |
+
-----
|
| 538 |
+
In general, the (polynomial) division of one Hermite series by another
|
| 539 |
+
results in quotient and remainder terms that are not in the Hermite
|
| 540 |
+
polynomial basis set. Thus, to express these results as a Hermite
|
| 541 |
+
series, it is necessary to "reproject" the results onto the Hermite
|
| 542 |
+
basis set, which may produce "unintuitive" (but correct) results; see
|
| 543 |
+
Examples section below.
|
| 544 |
+
|
| 545 |
+
Examples
|
| 546 |
+
--------
|
| 547 |
+
>>> from numpy.polynomial.hermite import hermdiv
|
| 548 |
+
>>> hermdiv([ 52., 29., 52., 7., 6.], [0, 1, 2])
|
| 549 |
+
(array([1., 2., 3.]), array([0.]))
|
| 550 |
+
>>> hermdiv([ 54., 31., 52., 7., 6.], [0, 1, 2])
|
| 551 |
+
(array([1., 2., 3.]), array([2., 2.]))
|
| 552 |
+
>>> hermdiv([ 53., 30., 52., 7., 6.], [0, 1, 2])
|
| 553 |
+
(array([1., 2., 3.]), array([1., 1.]))
|
| 554 |
+
|
| 555 |
+
"""
|
| 556 |
+
return pu._div(hermmul, c1, c2)
|
| 557 |
+
|
| 558 |
+
|
| 559 |
+
def hermpow(c, pow, maxpower=16):
|
| 560 |
+
"""Raise a Hermite series to a power.
|
| 561 |
+
|
| 562 |
+
Returns the Hermite series `c` raised to the power `pow`. The
|
| 563 |
+
argument `c` is a sequence of coefficients ordered from low to high.
|
| 564 |
+
i.e., [1,2,3] is the series ``P_0 + 2*P_1 + 3*P_2.``
|
| 565 |
+
|
| 566 |
+
Parameters
|
| 567 |
+
----------
|
| 568 |
+
c : array_like
|
| 569 |
+
1-D array of Hermite series coefficients ordered from low to
|
| 570 |
+
high.
|
| 571 |
+
pow : integer
|
| 572 |
+
Power to which the series will be raised
|
| 573 |
+
maxpower : integer, optional
|
| 574 |
+
Maximum power allowed. This is mainly to limit growth of the series
|
| 575 |
+
to unmanageable size. Default is 16
|
| 576 |
+
|
| 577 |
+
Returns
|
| 578 |
+
-------
|
| 579 |
+
coef : ndarray
|
| 580 |
+
Hermite series of power.
|
| 581 |
+
|
| 582 |
+
See Also
|
| 583 |
+
--------
|
| 584 |
+
hermadd, hermsub, hermmulx, hermmul, hermdiv
|
| 585 |
+
|
| 586 |
+
Examples
|
| 587 |
+
--------
|
| 588 |
+
>>> from numpy.polynomial.hermite import hermpow
|
| 589 |
+
>>> hermpow([1, 2, 3], 2)
|
| 590 |
+
array([81., 52., 82., 12., 9.])
|
| 591 |
+
|
| 592 |
+
"""
|
| 593 |
+
return pu._pow(hermmul, c, pow, maxpower)
|
| 594 |
+
|
| 595 |
+
|
| 596 |
+
def hermder(c, m=1, scl=1, axis=0):
|
| 597 |
+
"""
|
| 598 |
+
Differentiate a Hermite series.
|
| 599 |
+
|
| 600 |
+
Returns the Hermite series coefficients `c` differentiated `m` times
|
| 601 |
+
along `axis`. At each iteration the result is multiplied by `scl` (the
|
| 602 |
+
scaling factor is for use in a linear change of variable). The argument
|
| 603 |
+
`c` is an array of coefficients from low to high degree along each
|
| 604 |
+
axis, e.g., [1,2,3] represents the series ``1*H_0 + 2*H_1 + 3*H_2``
|
| 605 |
+
while [[1,2],[1,2]] represents ``1*H_0(x)*H_0(y) + 1*H_1(x)*H_0(y) +
|
| 606 |
+
2*H_0(x)*H_1(y) + 2*H_1(x)*H_1(y)`` if axis=0 is ``x`` and axis=1 is
|
| 607 |
+
``y``.
|
| 608 |
+
|
| 609 |
+
Parameters
|
| 610 |
+
----------
|
| 611 |
+
c : array_like
|
| 612 |
+
Array of Hermite series coefficients. If `c` is multidimensional the
|
| 613 |
+
different axis correspond to different variables with the degree in
|
| 614 |
+
each axis given by the corresponding index.
|
| 615 |
+
m : int, optional
|
| 616 |
+
Number of derivatives taken, must be non-negative. (Default: 1)
|
| 617 |
+
scl : scalar, optional
|
| 618 |
+
Each differentiation is multiplied by `scl`. The end result is
|
| 619 |
+
multiplication by ``scl**m``. This is for use in a linear change of
|
| 620 |
+
variable. (Default: 1)
|
| 621 |
+
axis : int, optional
|
| 622 |
+
Axis over which the derivative is taken. (Default: 0).
|
| 623 |
+
|
| 624 |
+
Returns
|
| 625 |
+
-------
|
| 626 |
+
der : ndarray
|
| 627 |
+
Hermite series of the derivative.
|
| 628 |
+
|
| 629 |
+
See Also
|
| 630 |
+
--------
|
| 631 |
+
hermint
|
| 632 |
+
|
| 633 |
+
Notes
|
| 634 |
+
-----
|
| 635 |
+
In general, the result of differentiating a Hermite series does not
|
| 636 |
+
resemble the same operation on a power series. Thus the result of this
|
| 637 |
+
function may be "unintuitive," albeit correct; see Examples section
|
| 638 |
+
below.
|
| 639 |
+
|
| 640 |
+
Examples
|
| 641 |
+
--------
|
| 642 |
+
>>> from numpy.polynomial.hermite import hermder
|
| 643 |
+
>>> hermder([ 1. , 0.5, 0.5, 0.5])
|
| 644 |
+
array([1., 2., 3.])
|
| 645 |
+
>>> hermder([-0.5, 1./2., 1./8., 1./12., 1./16.], m=2)
|
| 646 |
+
array([1., 2., 3.])
|
| 647 |
+
|
| 648 |
+
"""
|
| 649 |
+
c = np.array(c, ndmin=1, copy=True)
|
| 650 |
+
if c.dtype.char in '?bBhHiIlLqQpP':
|
| 651 |
+
c = c.astype(np.double)
|
| 652 |
+
cnt = pu._as_int(m, "the order of derivation")
|
| 653 |
+
iaxis = pu._as_int(axis, "the axis")
|
| 654 |
+
if cnt < 0:
|
| 655 |
+
raise ValueError("The order of derivation must be non-negative")
|
| 656 |
+
iaxis = np.lib.array_utils.normalize_axis_index(iaxis, c.ndim)
|
| 657 |
+
|
| 658 |
+
if cnt == 0:
|
| 659 |
+
return c
|
| 660 |
+
|
| 661 |
+
c = np.moveaxis(c, iaxis, 0)
|
| 662 |
+
n = len(c)
|
| 663 |
+
if cnt >= n:
|
| 664 |
+
c = c[:1] * 0
|
| 665 |
+
else:
|
| 666 |
+
for i in range(cnt):
|
| 667 |
+
n = n - 1
|
| 668 |
+
c *= scl
|
| 669 |
+
der = np.empty((n,) + c.shape[1:], dtype=c.dtype)
|
| 670 |
+
for j in range(n, 0, -1):
|
| 671 |
+
der[j - 1] = (2 * j) * c[j]
|
| 672 |
+
c = der
|
| 673 |
+
c = np.moveaxis(c, 0, iaxis)
|
| 674 |
+
return c
|
| 675 |
+
|
| 676 |
+
|
| 677 |
+
def hermint(c, m=1, k=[], lbnd=0, scl=1, axis=0):
|
| 678 |
+
"""
|
| 679 |
+
Integrate a Hermite series.
|
| 680 |
+
|
| 681 |
+
Returns the Hermite series coefficients `c` integrated `m` times from
|
| 682 |
+
`lbnd` along `axis`. At each iteration the resulting series is
|
| 683 |
+
**multiplied** by `scl` and an integration constant, `k`, is added.
|
| 684 |
+
The scaling factor is for use in a linear change of variable. ("Buyer
|
| 685 |
+
beware": note that, depending on what one is doing, one may want `scl`
|
| 686 |
+
to be the reciprocal of what one might expect; for more information,
|
| 687 |
+
see the Notes section below.) The argument `c` is an array of
|
| 688 |
+
coefficients from low to high degree along each axis, e.g., [1,2,3]
|
| 689 |
+
represents the series ``H_0 + 2*H_1 + 3*H_2`` while [[1,2],[1,2]]
|
| 690 |
+
represents ``1*H_0(x)*H_0(y) + 1*H_1(x)*H_0(y) + 2*H_0(x)*H_1(y) +
|
| 691 |
+
2*H_1(x)*H_1(y)`` if axis=0 is ``x`` and axis=1 is ``y``.
|
| 692 |
+
|
| 693 |
+
Parameters
|
| 694 |
+
----------
|
| 695 |
+
c : array_like
|
| 696 |
+
Array of Hermite series coefficients. If c is multidimensional the
|
| 697 |
+
different axis correspond to different variables with the degree in
|
| 698 |
+
each axis given by the corresponding index.
|
| 699 |
+
m : int, optional
|
| 700 |
+
Order of integration, must be positive. (Default: 1)
|
| 701 |
+
k : {[], list, scalar}, optional
|
| 702 |
+
Integration constant(s). The value of the first integral at
|
| 703 |
+
``lbnd`` is the first value in the list, the value of the second
|
| 704 |
+
integral at ``lbnd`` is the second value, etc. If ``k == []`` (the
|
| 705 |
+
default), all constants are set to zero. If ``m == 1``, a single
|
| 706 |
+
scalar can be given instead of a list.
|
| 707 |
+
lbnd : scalar, optional
|
| 708 |
+
The lower bound of the integral. (Default: 0)
|
| 709 |
+
scl : scalar, optional
|
| 710 |
+
Following each integration the result is *multiplied* by `scl`
|
| 711 |
+
before the integration constant is added. (Default: 1)
|
| 712 |
+
axis : int, optional
|
| 713 |
+
Axis over which the integral is taken. (Default: 0).
|
| 714 |
+
|
| 715 |
+
Returns
|
| 716 |
+
-------
|
| 717 |
+
S : ndarray
|
| 718 |
+
Hermite series coefficients of the integral.
|
| 719 |
+
|
| 720 |
+
Raises
|
| 721 |
+
------
|
| 722 |
+
ValueError
|
| 723 |
+
If ``m < 0``, ``len(k) > m``, ``np.ndim(lbnd) != 0``, or
|
| 724 |
+
``np.ndim(scl) != 0``.
|
| 725 |
+
|
| 726 |
+
See Also
|
| 727 |
+
--------
|
| 728 |
+
hermder
|
| 729 |
+
|
| 730 |
+
Notes
|
| 731 |
+
-----
|
| 732 |
+
Note that the result of each integration is *multiplied* by `scl`.
|
| 733 |
+
Why is this important to note? Say one is making a linear change of
|
| 734 |
+
variable :math:`u = ax + b` in an integral relative to `x`. Then
|
| 735 |
+
:math:`dx = du/a`, so one will need to set `scl` equal to
|
| 736 |
+
:math:`1/a` - perhaps not what one would have first thought.
|
| 737 |
+
|
| 738 |
+
Also note that, in general, the result of integrating a C-series needs
|
| 739 |
+
to be "reprojected" onto the C-series basis set. Thus, typically,
|
| 740 |
+
the result of this function is "unintuitive," albeit correct; see
|
| 741 |
+
Examples section below.
|
| 742 |
+
|
| 743 |
+
Examples
|
| 744 |
+
--------
|
| 745 |
+
>>> from numpy.polynomial.hermite import hermint
|
| 746 |
+
>>> hermint([1,2,3]) # integrate once, value 0 at 0.
|
| 747 |
+
array([1. , 0.5, 0.5, 0.5])
|
| 748 |
+
>>> hermint([1,2,3], m=2) # integrate twice, value & deriv 0 at 0
|
| 749 |
+
array([-0.5 , 0.5 , 0.125 , 0.08333333, 0.0625 ]) # may vary
|
| 750 |
+
>>> hermint([1,2,3], k=1) # integrate once, value 1 at 0.
|
| 751 |
+
array([2. , 0.5, 0.5, 0.5])
|
| 752 |
+
>>> hermint([1,2,3], lbnd=-1) # integrate once, value 0 at -1
|
| 753 |
+
array([-2. , 0.5, 0.5, 0.5])
|
| 754 |
+
>>> hermint([1,2,3], m=2, k=[1,2], lbnd=-1)
|
| 755 |
+
array([ 1.66666667, -0.5 , 0.125 , 0.08333333, 0.0625 ]) # may vary
|
| 756 |
+
|
| 757 |
+
"""
|
| 758 |
+
c = np.array(c, ndmin=1, copy=True)
|
| 759 |
+
if c.dtype.char in '?bBhHiIlLqQpP':
|
| 760 |
+
c = c.astype(np.double)
|
| 761 |
+
if not np.iterable(k):
|
| 762 |
+
k = [k]
|
| 763 |
+
cnt = pu._as_int(m, "the order of integration")
|
| 764 |
+
iaxis = pu._as_int(axis, "the axis")
|
| 765 |
+
if cnt < 0:
|
| 766 |
+
raise ValueError("The order of integration must be non-negative")
|
| 767 |
+
if len(k) > cnt:
|
| 768 |
+
raise ValueError("Too many integration constants")
|
| 769 |
+
if np.ndim(lbnd) != 0:
|
| 770 |
+
raise ValueError("lbnd must be a scalar.")
|
| 771 |
+
if np.ndim(scl) != 0:
|
| 772 |
+
raise ValueError("scl must be a scalar.")
|
| 773 |
+
iaxis = np.lib.array_utils.normalize_axis_index(iaxis, c.ndim)
|
| 774 |
+
|
| 775 |
+
if cnt == 0:
|
| 776 |
+
return c
|
| 777 |
+
|
| 778 |
+
c = np.moveaxis(c, iaxis, 0)
|
| 779 |
+
k = list(k) + [0] * (cnt - len(k))
|
| 780 |
+
for i in range(cnt):
|
| 781 |
+
n = len(c)
|
| 782 |
+
c *= scl
|
| 783 |
+
if n == 1 and np.all(c[0] == 0):
|
| 784 |
+
c[0] += k[i]
|
| 785 |
+
else:
|
| 786 |
+
tmp = np.empty((n + 1,) + c.shape[1:], dtype=c.dtype)
|
| 787 |
+
tmp[0] = c[0] * 0
|
| 788 |
+
tmp[1] = c[0] / 2
|
| 789 |
+
for j in range(1, n):
|
| 790 |
+
tmp[j + 1] = c[j] / (2 * (j + 1))
|
| 791 |
+
tmp[0] += k[i] - hermval(lbnd, tmp)
|
| 792 |
+
c = tmp
|
| 793 |
+
c = np.moveaxis(c, 0, iaxis)
|
| 794 |
+
return c
|
| 795 |
+
|
| 796 |
+
|
| 797 |
+
def hermval(x, c, tensor=True):
|
| 798 |
+
"""
|
| 799 |
+
Evaluate a Hermite series at points x.
|
| 800 |
+
|
| 801 |
+
If `c` is of length ``n + 1``, this function returns the value:
|
| 802 |
+
|
| 803 |
+
.. math:: p(x) = c_0 * H_0(x) + c_1 * H_1(x) + ... + c_n * H_n(x)
|
| 804 |
+
|
| 805 |
+
The parameter `x` is converted to an array only if it is a tuple or a
|
| 806 |
+
list, otherwise it is treated as a scalar. In either case, either `x`
|
| 807 |
+
or its elements must support multiplication and addition both with
|
| 808 |
+
themselves and with the elements of `c`.
|
| 809 |
+
|
| 810 |
+
If `c` is a 1-D array, then ``p(x)`` will have the same shape as `x`. If
|
| 811 |
+
`c` is multidimensional, then the shape of the result depends on the
|
| 812 |
+
value of `tensor`. If `tensor` is true the shape will be c.shape[1:] +
|
| 813 |
+
x.shape. If `tensor` is false the shape will be c.shape[1:]. Note that
|
| 814 |
+
scalars have shape (,).
|
| 815 |
+
|
| 816 |
+
Trailing zeros in the coefficients will be used in the evaluation, so
|
| 817 |
+
they should be avoided if efficiency is a concern.
|
| 818 |
+
|
| 819 |
+
Parameters
|
| 820 |
+
----------
|
| 821 |
+
x : array_like, compatible object
|
| 822 |
+
If `x` is a list or tuple, it is converted to an ndarray, otherwise
|
| 823 |
+
it is left unchanged and treated as a scalar. In either case, `x`
|
| 824 |
+
or its elements must support addition and multiplication with
|
| 825 |
+
themselves and with the elements of `c`.
|
| 826 |
+
c : array_like
|
| 827 |
+
Array of coefficients ordered so that the coefficients for terms of
|
| 828 |
+
degree n are contained in c[n]. If `c` is multidimensional the
|
| 829 |
+
remaining indices enumerate multiple polynomials. In the two
|
| 830 |
+
dimensional case the coefficients may be thought of as stored in
|
| 831 |
+
the columns of `c`.
|
| 832 |
+
tensor : boolean, optional
|
| 833 |
+
If True, the shape of the coefficient array is extended with ones
|
| 834 |
+
on the right, one for each dimension of `x`. Scalars have dimension 0
|
| 835 |
+
for this action. The result is that every column of coefficients in
|
| 836 |
+
`c` is evaluated for every element of `x`. If False, `x` is broadcast
|
| 837 |
+
over the columns of `c` for the evaluation. This keyword is useful
|
| 838 |
+
when `c` is multidimensional. The default value is True.
|
| 839 |
+
|
| 840 |
+
Returns
|
| 841 |
+
-------
|
| 842 |
+
values : ndarray, algebra_like
|
| 843 |
+
The shape of the return value is described above.
|
| 844 |
+
|
| 845 |
+
See Also
|
| 846 |
+
--------
|
| 847 |
+
hermval2d, hermgrid2d, hermval3d, hermgrid3d
|
| 848 |
+
|
| 849 |
+
Notes
|
| 850 |
+
-----
|
| 851 |
+
The evaluation uses Clenshaw recursion, aka synthetic division.
|
| 852 |
+
|
| 853 |
+
Examples
|
| 854 |
+
--------
|
| 855 |
+
>>> from numpy.polynomial.hermite import hermval
|
| 856 |
+
>>> coef = [1,2,3]
|
| 857 |
+
>>> hermval(1, coef)
|
| 858 |
+
11.0
|
| 859 |
+
>>> hermval([[1,2],[3,4]], coef)
|
| 860 |
+
array([[ 11., 51.],
|
| 861 |
+
[115., 203.]])
|
| 862 |
+
|
| 863 |
+
"""
|
| 864 |
+
c = np.array(c, ndmin=1, copy=None)
|
| 865 |
+
if c.dtype.char in '?bBhHiIlLqQpP':
|
| 866 |
+
c = c.astype(np.double)
|
| 867 |
+
if isinstance(x, (tuple, list)):
|
| 868 |
+
x = np.asarray(x)
|
| 869 |
+
if isinstance(x, np.ndarray) and tensor:
|
| 870 |
+
c = c.reshape(c.shape + (1,) * x.ndim)
|
| 871 |
+
|
| 872 |
+
x2 = x * 2
|
| 873 |
+
if len(c) == 1:
|
| 874 |
+
c0 = c[0]
|
| 875 |
+
c1 = 0
|
| 876 |
+
elif len(c) == 2:
|
| 877 |
+
c0 = c[0]
|
| 878 |
+
c1 = c[1]
|
| 879 |
+
else:
|
| 880 |
+
nd = len(c)
|
| 881 |
+
c0 = c[-2]
|
| 882 |
+
c1 = c[-1]
|
| 883 |
+
for i in range(3, len(c) + 1):
|
| 884 |
+
tmp = c0
|
| 885 |
+
nd = nd - 1
|
| 886 |
+
c0 = c[-i] - c1 * (2 * (nd - 1))
|
| 887 |
+
c1 = tmp + c1 * x2
|
| 888 |
+
return c0 + c1 * x2
|
| 889 |
+
|
| 890 |
+
|
| 891 |
+
def hermval2d(x, y, c):
|
| 892 |
+
"""
|
| 893 |
+
Evaluate a 2-D Hermite series at points (x, y).
|
| 894 |
+
|
| 895 |
+
This function returns the values:
|
| 896 |
+
|
| 897 |
+
.. math:: p(x,y) = \\sum_{i,j} c_{i,j} * H_i(x) * H_j(y)
|
| 898 |
+
|
| 899 |
+
The parameters `x` and `y` are converted to arrays only if they are
|
| 900 |
+
tuples or a lists, otherwise they are treated as a scalars and they
|
| 901 |
+
must have the same shape after conversion. In either case, either `x`
|
| 902 |
+
and `y` or their elements must support multiplication and addition both
|
| 903 |
+
with themselves and with the elements of `c`.
|
| 904 |
+
|
| 905 |
+
If `c` is a 1-D array a one is implicitly appended to its shape to make
|
| 906 |
+
it 2-D. The shape of the result will be c.shape[2:] + x.shape.
|
| 907 |
+
|
| 908 |
+
Parameters
|
| 909 |
+
----------
|
| 910 |
+
x, y : array_like, compatible objects
|
| 911 |
+
The two dimensional series is evaluated at the points ``(x, y)``,
|
| 912 |
+
where `x` and `y` must have the same shape. If `x` or `y` is a list
|
| 913 |
+
or tuple, it is first converted to an ndarray, otherwise it is left
|
| 914 |
+
unchanged and if it isn't an ndarray it is treated as a scalar.
|
| 915 |
+
c : array_like
|
| 916 |
+
Array of coefficients ordered so that the coefficient of the term
|
| 917 |
+
of multi-degree i,j is contained in ``c[i,j]``. If `c` has
|
| 918 |
+
dimension greater than two the remaining indices enumerate multiple
|
| 919 |
+
sets of coefficients.
|
| 920 |
+
|
| 921 |
+
Returns
|
| 922 |
+
-------
|
| 923 |
+
values : ndarray, compatible object
|
| 924 |
+
The values of the two dimensional polynomial at points formed with
|
| 925 |
+
pairs of corresponding values from `x` and `y`.
|
| 926 |
+
|
| 927 |
+
See Also
|
| 928 |
+
--------
|
| 929 |
+
hermval, hermgrid2d, hermval3d, hermgrid3d
|
| 930 |
+
|
| 931 |
+
Examples
|
| 932 |
+
--------
|
| 933 |
+
>>> from numpy.polynomial.hermite import hermval2d
|
| 934 |
+
>>> x = [1, 2]
|
| 935 |
+
>>> y = [4, 5]
|
| 936 |
+
>>> c = [[1, 2, 3], [4, 5, 6]]
|
| 937 |
+
>>> hermval2d(x, y, c)
|
| 938 |
+
array([1035., 2883.])
|
| 939 |
+
|
| 940 |
+
"""
|
| 941 |
+
return pu._valnd(hermval, c, x, y)
|
| 942 |
+
|
| 943 |
+
|
| 944 |
+
def hermgrid2d(x, y, c):
|
| 945 |
+
"""
|
| 946 |
+
Evaluate a 2-D Hermite series on the Cartesian product of x and y.
|
| 947 |
+
|
| 948 |
+
This function returns the values:
|
| 949 |
+
|
| 950 |
+
.. math:: p(a,b) = \\sum_{i,j} c_{i,j} * H_i(a) * H_j(b)
|
| 951 |
+
|
| 952 |
+
where the points ``(a, b)`` consist of all pairs formed by taking
|
| 953 |
+
`a` from `x` and `b` from `y`. The resulting points form a grid with
|
| 954 |
+
`x` in the first dimension and `y` in the second.
|
| 955 |
+
|
| 956 |
+
The parameters `x` and `y` are converted to arrays only if they are
|
| 957 |
+
tuples or a lists, otherwise they are treated as a scalars. In either
|
| 958 |
+
case, either `x` and `y` or their elements must support multiplication
|
| 959 |
+
and addition both with themselves and with the elements of `c`.
|
| 960 |
+
|
| 961 |
+
If `c` has fewer than two dimensions, ones are implicitly appended to
|
| 962 |
+
its shape to make it 2-D. The shape of the result will be c.shape[2:] +
|
| 963 |
+
x.shape.
|
| 964 |
+
|
| 965 |
+
Parameters
|
| 966 |
+
----------
|
| 967 |
+
x, y : array_like, compatible objects
|
| 968 |
+
The two dimensional series is evaluated at the points in the
|
| 969 |
+
Cartesian product of `x` and `y`. If `x` or `y` is a list or
|
| 970 |
+
tuple, it is first converted to an ndarray, otherwise it is left
|
| 971 |
+
unchanged and, if it isn't an ndarray, it is treated as a scalar.
|
| 972 |
+
c : array_like
|
| 973 |
+
Array of coefficients ordered so that the coefficients for terms of
|
| 974 |
+
degree i,j are contained in ``c[i,j]``. If `c` has dimension
|
| 975 |
+
greater than two the remaining indices enumerate multiple sets of
|
| 976 |
+
coefficients.
|
| 977 |
+
|
| 978 |
+
Returns
|
| 979 |
+
-------
|
| 980 |
+
values : ndarray, compatible object
|
| 981 |
+
The values of the two dimensional polynomial at points in the Cartesian
|
| 982 |
+
product of `x` and `y`.
|
| 983 |
+
|
| 984 |
+
See Also
|
| 985 |
+
--------
|
| 986 |
+
hermval, hermval2d, hermval3d, hermgrid3d
|
| 987 |
+
|
| 988 |
+
Examples
|
| 989 |
+
--------
|
| 990 |
+
>>> from numpy.polynomial.hermite import hermgrid2d
|
| 991 |
+
>>> x = [1, 2, 3]
|
| 992 |
+
>>> y = [4, 5]
|
| 993 |
+
>>> c = [[1, 2, 3], [4, 5, 6]]
|
| 994 |
+
>>> hermgrid2d(x, y, c)
|
| 995 |
+
array([[1035., 1599.],
|
| 996 |
+
[1867., 2883.],
|
| 997 |
+
[2699., 4167.]])
|
| 998 |
+
|
| 999 |
+
"""
|
| 1000 |
+
return pu._gridnd(hermval, c, x, y)
|
| 1001 |
+
|
| 1002 |
+
|
| 1003 |
+
def hermval3d(x, y, z, c):
|
| 1004 |
+
"""
|
| 1005 |
+
Evaluate a 3-D Hermite series at points (x, y, z).
|
| 1006 |
+
|
| 1007 |
+
This function returns the values:
|
| 1008 |
+
|
| 1009 |
+
.. math:: p(x,y,z) = \\sum_{i,j,k} c_{i,j,k} * H_i(x) * H_j(y) * H_k(z)
|
| 1010 |
+
|
| 1011 |
+
The parameters `x`, `y`, and `z` are converted to arrays only if
|
| 1012 |
+
they are tuples or a lists, otherwise they are treated as a scalars and
|
| 1013 |
+
they must have the same shape after conversion. In either case, either
|
| 1014 |
+
`x`, `y`, and `z` or their elements must support multiplication and
|
| 1015 |
+
addition both with themselves and with the elements of `c`.
|
| 1016 |
+
|
| 1017 |
+
If `c` has fewer than 3 dimensions, ones are implicitly appended to its
|
| 1018 |
+
shape to make it 3-D. The shape of the result will be c.shape[3:] +
|
| 1019 |
+
x.shape.
|
| 1020 |
+
|
| 1021 |
+
Parameters
|
| 1022 |
+
----------
|
| 1023 |
+
x, y, z : array_like, compatible object
|
| 1024 |
+
The three dimensional series is evaluated at the points
|
| 1025 |
+
``(x, y, z)``, where `x`, `y`, and `z` must have the same shape. If
|
| 1026 |
+
any of `x`, `y`, or `z` is a list or tuple, it is first converted
|
| 1027 |
+
to an ndarray, otherwise it is left unchanged and if it isn't an
|
| 1028 |
+
ndarray it is treated as a scalar.
|
| 1029 |
+
c : array_like
|
| 1030 |
+
Array of coefficients ordered so that the coefficient of the term of
|
| 1031 |
+
multi-degree i,j,k is contained in ``c[i,j,k]``. If `c` has dimension
|
| 1032 |
+
greater than 3 the remaining indices enumerate multiple sets of
|
| 1033 |
+
coefficients.
|
| 1034 |
+
|
| 1035 |
+
Returns
|
| 1036 |
+
-------
|
| 1037 |
+
values : ndarray, compatible object
|
| 1038 |
+
The values of the multidimensional polynomial on points formed with
|
| 1039 |
+
triples of corresponding values from `x`, `y`, and `z`.
|
| 1040 |
+
|
| 1041 |
+
See Also
|
| 1042 |
+
--------
|
| 1043 |
+
hermval, hermval2d, hermgrid2d, hermgrid3d
|
| 1044 |
+
|
| 1045 |
+
Examples
|
| 1046 |
+
--------
|
| 1047 |
+
>>> from numpy.polynomial.hermite import hermval3d
|
| 1048 |
+
>>> x = [1, 2]
|
| 1049 |
+
>>> y = [4, 5]
|
| 1050 |
+
>>> z = [6, 7]
|
| 1051 |
+
>>> c = [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]
|
| 1052 |
+
>>> hermval3d(x, y, z, c)
|
| 1053 |
+
array([ 40077., 120131.])
|
| 1054 |
+
|
| 1055 |
+
"""
|
| 1056 |
+
return pu._valnd(hermval, c, x, y, z)
|
| 1057 |
+
|
| 1058 |
+
|
| 1059 |
+
def hermgrid3d(x, y, z, c):
|
| 1060 |
+
"""
|
| 1061 |
+
Evaluate a 3-D Hermite series on the Cartesian product of x, y, and z.
|
| 1062 |
+
|
| 1063 |
+
This function returns the values:
|
| 1064 |
+
|
| 1065 |
+
.. math:: p(a,b,c) = \\sum_{i,j,k} c_{i,j,k} * H_i(a) * H_j(b) * H_k(c)
|
| 1066 |
+
|
| 1067 |
+
where the points ``(a, b, c)`` consist of all triples formed by taking
|
| 1068 |
+
`a` from `x`, `b` from `y`, and `c` from `z`. The resulting points form
|
| 1069 |
+
a grid with `x` in the first dimension, `y` in the second, and `z` in
|
| 1070 |
+
the third.
|
| 1071 |
+
|
| 1072 |
+
The parameters `x`, `y`, and `z` are converted to arrays only if they
|
| 1073 |
+
are tuples or a lists, otherwise they are treated as a scalars. In
|
| 1074 |
+
either case, either `x`, `y`, and `z` or their elements must support
|
| 1075 |
+
multiplication and addition both with themselves and with the elements
|
| 1076 |
+
of `c`.
|
| 1077 |
+
|
| 1078 |
+
If `c` has fewer than three dimensions, ones are implicitly appended to
|
| 1079 |
+
its shape to make it 3-D. The shape of the result will be c.shape[3:] +
|
| 1080 |
+
x.shape + y.shape + z.shape.
|
| 1081 |
+
|
| 1082 |
+
Parameters
|
| 1083 |
+
----------
|
| 1084 |
+
x, y, z : array_like, compatible objects
|
| 1085 |
+
The three dimensional series is evaluated at the points in the
|
| 1086 |
+
Cartesian product of `x`, `y`, and `z`. If `x`, `y`, or `z` is a
|
| 1087 |
+
list or tuple, it is first converted to an ndarray, otherwise it is
|
| 1088 |
+
left unchanged and, if it isn't an ndarray, it is treated as a
|
| 1089 |
+
scalar.
|
| 1090 |
+
c : array_like
|
| 1091 |
+
Array of coefficients ordered so that the coefficients for terms of
|
| 1092 |
+
degree i,j are contained in ``c[i,j]``. If `c` has dimension
|
| 1093 |
+
greater than two the remaining indices enumerate multiple sets of
|
| 1094 |
+
coefficients.
|
| 1095 |
+
|
| 1096 |
+
Returns
|
| 1097 |
+
-------
|
| 1098 |
+
values : ndarray, compatible object
|
| 1099 |
+
The values of the two dimensional polynomial at points in the Cartesian
|
| 1100 |
+
product of `x` and `y`.
|
| 1101 |
+
|
| 1102 |
+
See Also
|
| 1103 |
+
--------
|
| 1104 |
+
hermval, hermval2d, hermgrid2d, hermval3d
|
| 1105 |
+
|
| 1106 |
+
Examples
|
| 1107 |
+
--------
|
| 1108 |
+
>>> from numpy.polynomial.hermite import hermgrid3d
|
| 1109 |
+
>>> x = [1, 2]
|
| 1110 |
+
>>> y = [4, 5]
|
| 1111 |
+
>>> z = [6, 7]
|
| 1112 |
+
>>> c = [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]
|
| 1113 |
+
>>> hermgrid3d(x, y, z, c)
|
| 1114 |
+
array([[[ 40077., 54117.],
|
| 1115 |
+
[ 49293., 66561.]],
|
| 1116 |
+
[[ 72375., 97719.],
|
| 1117 |
+
[ 88975., 120131.]]])
|
| 1118 |
+
|
| 1119 |
+
"""
|
| 1120 |
+
return pu._gridnd(hermval, c, x, y, z)
|
| 1121 |
+
|
| 1122 |
+
|
| 1123 |
+
def hermvander(x, deg):
|
| 1124 |
+
"""Pseudo-Vandermonde matrix of given degree.
|
| 1125 |
+
|
| 1126 |
+
Returns the pseudo-Vandermonde matrix of degree `deg` and sample points
|
| 1127 |
+
`x`. The pseudo-Vandermonde matrix is defined by
|
| 1128 |
+
|
| 1129 |
+
.. math:: V[..., i] = H_i(x),
|
| 1130 |
+
|
| 1131 |
+
where ``0 <= i <= deg``. The leading indices of `V` index the elements of
|
| 1132 |
+
`x` and the last index is the degree of the Hermite polynomial.
|
| 1133 |
+
|
| 1134 |
+
If `c` is a 1-D array of coefficients of length ``n + 1`` and `V` is the
|
| 1135 |
+
array ``V = hermvander(x, n)``, then ``np.dot(V, c)`` and
|
| 1136 |
+
``hermval(x, c)`` are the same up to roundoff. This equivalence is
|
| 1137 |
+
useful both for least squares fitting and for the evaluation of a large
|
| 1138 |
+
number of Hermite series of the same degree and sample points.
|
| 1139 |
+
|
| 1140 |
+
Parameters
|
| 1141 |
+
----------
|
| 1142 |
+
x : array_like
|
| 1143 |
+
Array of points. The dtype is converted to float64 or complex128
|
| 1144 |
+
depending on whether any of the elements are complex. If `x` is
|
| 1145 |
+
scalar it is converted to a 1-D array.
|
| 1146 |
+
deg : int
|
| 1147 |
+
Degree of the resulting matrix.
|
| 1148 |
+
|
| 1149 |
+
Returns
|
| 1150 |
+
-------
|
| 1151 |
+
vander : ndarray
|
| 1152 |
+
The pseudo-Vandermonde matrix. The shape of the returned matrix is
|
| 1153 |
+
``x.shape + (deg + 1,)``, where The last index is the degree of the
|
| 1154 |
+
corresponding Hermite polynomial. The dtype will be the same as
|
| 1155 |
+
the converted `x`.
|
| 1156 |
+
|
| 1157 |
+
Examples
|
| 1158 |
+
--------
|
| 1159 |
+
>>> import numpy as np
|
| 1160 |
+
>>> from numpy.polynomial.hermite import hermvander
|
| 1161 |
+
>>> x = np.array([-1, 0, 1])
|
| 1162 |
+
>>> hermvander(x, 3)
|
| 1163 |
+
array([[ 1., -2., 2., 4.],
|
| 1164 |
+
[ 1., 0., -2., -0.],
|
| 1165 |
+
[ 1., 2., 2., -4.]])
|
| 1166 |
+
|
| 1167 |
+
"""
|
| 1168 |
+
ideg = pu._as_int(deg, "deg")
|
| 1169 |
+
if ideg < 0:
|
| 1170 |
+
raise ValueError("deg must be non-negative")
|
| 1171 |
+
|
| 1172 |
+
x = np.array(x, copy=None, ndmin=1) + 0.0
|
| 1173 |
+
dims = (ideg + 1,) + x.shape
|
| 1174 |
+
dtyp = x.dtype
|
| 1175 |
+
v = np.empty(dims, dtype=dtyp)
|
| 1176 |
+
v[0] = x * 0 + 1
|
| 1177 |
+
if ideg > 0:
|
| 1178 |
+
x2 = x * 2
|
| 1179 |
+
v[1] = x2
|
| 1180 |
+
for i in range(2, ideg + 1):
|
| 1181 |
+
v[i] = (v[i - 1] * x2 - v[i - 2] * (2 * (i - 1)))
|
| 1182 |
+
return np.moveaxis(v, 0, -1)
|
| 1183 |
+
|
| 1184 |
+
|
| 1185 |
+
def hermvander2d(x, y, deg):
|
| 1186 |
+
"""Pseudo-Vandermonde matrix of given degrees.
|
| 1187 |
+
|
| 1188 |
+
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample
|
| 1189 |
+
points ``(x, y)``. The pseudo-Vandermonde matrix is defined by
|
| 1190 |
+
|
| 1191 |
+
.. math:: V[..., (deg[1] + 1)*i + j] = H_i(x) * H_j(y),
|
| 1192 |
+
|
| 1193 |
+
where ``0 <= i <= deg[0]`` and ``0 <= j <= deg[1]``. The leading indices of
|
| 1194 |
+
`V` index the points ``(x, y)`` and the last index encodes the degrees of
|
| 1195 |
+
the Hermite polynomials.
|
| 1196 |
+
|
| 1197 |
+
If ``V = hermvander2d(x, y, [xdeg, ydeg])``, then the columns of `V`
|
| 1198 |
+
correspond to the elements of a 2-D coefficient array `c` of shape
|
| 1199 |
+
(xdeg + 1, ydeg + 1) in the order
|
| 1200 |
+
|
| 1201 |
+
.. math:: c_{00}, c_{01}, c_{02} ... , c_{10}, c_{11}, c_{12} ...
|
| 1202 |
+
|
| 1203 |
+
and ``np.dot(V, c.flat)`` and ``hermval2d(x, y, c)`` will be the same
|
| 1204 |
+
up to roundoff. This equivalence is useful both for least squares
|
| 1205 |
+
fitting and for the evaluation of a large number of 2-D Hermite
|
| 1206 |
+
series of the same degrees and sample points.
|
| 1207 |
+
|
| 1208 |
+
Parameters
|
| 1209 |
+
----------
|
| 1210 |
+
x, y : array_like
|
| 1211 |
+
Arrays of point coordinates, all of the same shape. The dtypes
|
| 1212 |
+
will be converted to either float64 or complex128 depending on
|
| 1213 |
+
whether any of the elements are complex. Scalars are converted to 1-D
|
| 1214 |
+
arrays.
|
| 1215 |
+
deg : list of ints
|
| 1216 |
+
List of maximum degrees of the form [x_deg, y_deg].
|
| 1217 |
+
|
| 1218 |
+
Returns
|
| 1219 |
+
-------
|
| 1220 |
+
vander2d : ndarray
|
| 1221 |
+
The shape of the returned matrix is ``x.shape + (order,)``, where
|
| 1222 |
+
:math:`order = (deg[0]+1)*(deg[1]+1)`. The dtype will be the same
|
| 1223 |
+
as the converted `x` and `y`.
|
| 1224 |
+
|
| 1225 |
+
See Also
|
| 1226 |
+
--------
|
| 1227 |
+
hermvander, hermvander3d, hermval2d, hermval3d
|
| 1228 |
+
|
| 1229 |
+
Examples
|
| 1230 |
+
--------
|
| 1231 |
+
>>> import numpy as np
|
| 1232 |
+
>>> from numpy.polynomial.hermite import hermvander2d
|
| 1233 |
+
>>> x = np.array([-1, 0, 1])
|
| 1234 |
+
>>> y = np.array([-1, 0, 1])
|
| 1235 |
+
>>> hermvander2d(x, y, [2, 2])
|
| 1236 |
+
array([[ 1., -2., 2., -2., 4., -4., 2., -4., 4.],
|
| 1237 |
+
[ 1., 0., -2., 0., 0., -0., -2., -0., 4.],
|
| 1238 |
+
[ 1., 2., 2., 2., 4., 4., 2., 4., 4.]])
|
| 1239 |
+
|
| 1240 |
+
"""
|
| 1241 |
+
return pu._vander_nd_flat((hermvander, hermvander), (x, y), deg)
|
| 1242 |
+
|
| 1243 |
+
|
| 1244 |
+
def hermvander3d(x, y, z, deg):
|
| 1245 |
+
"""Pseudo-Vandermonde matrix of given degrees.
|
| 1246 |
+
|
| 1247 |
+
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample
|
| 1248 |
+
points ``(x, y, z)``. If `l`, `m`, `n` are the given degrees in `x`, `y`, `z`,
|
| 1249 |
+
then The pseudo-Vandermonde matrix is defined by
|
| 1250 |
+
|
| 1251 |
+
.. math:: V[..., (m+1)(n+1)i + (n+1)j + k] = H_i(x)*H_j(y)*H_k(z),
|
| 1252 |
+
|
| 1253 |
+
where ``0 <= i <= l``, ``0 <= j <= m``, and ``0 <= j <= n``. The leading
|
| 1254 |
+
indices of `V` index the points ``(x, y, z)`` and the last index encodes
|
| 1255 |
+
the degrees of the Hermite polynomials.
|
| 1256 |
+
|
| 1257 |
+
If ``V = hermvander3d(x, y, z, [xdeg, ydeg, zdeg])``, then the columns
|
| 1258 |
+
of `V` correspond to the elements of a 3-D coefficient array `c` of
|
| 1259 |
+
shape (xdeg + 1, ydeg + 1, zdeg + 1) in the order
|
| 1260 |
+
|
| 1261 |
+
.. math:: c_{000}, c_{001}, c_{002},... , c_{010}, c_{011}, c_{012},...
|
| 1262 |
+
|
| 1263 |
+
and ``np.dot(V, c.flat)`` and ``hermval3d(x, y, z, c)`` will be the
|
| 1264 |
+
same up to roundoff. This equivalence is useful both for least squares
|
| 1265 |
+
fitting and for the evaluation of a large number of 3-D Hermite
|
| 1266 |
+
series of the same degrees and sample points.
|
| 1267 |
+
|
| 1268 |
+
Parameters
|
| 1269 |
+
----------
|
| 1270 |
+
x, y, z : array_like
|
| 1271 |
+
Arrays of point coordinates, all of the same shape. The dtypes will
|
| 1272 |
+
be converted to either float64 or complex128 depending on whether
|
| 1273 |
+
any of the elements are complex. Scalars are converted to 1-D
|
| 1274 |
+
arrays.
|
| 1275 |
+
deg : list of ints
|
| 1276 |
+
List of maximum degrees of the form [x_deg, y_deg, z_deg].
|
| 1277 |
+
|
| 1278 |
+
Returns
|
| 1279 |
+
-------
|
| 1280 |
+
vander3d : ndarray
|
| 1281 |
+
The shape of the returned matrix is ``x.shape + (order,)``, where
|
| 1282 |
+
:math:`order = (deg[0]+1)*(deg[1]+1)*(deg[2]+1)`. The dtype will
|
| 1283 |
+
be the same as the converted `x`, `y`, and `z`.
|
| 1284 |
+
|
| 1285 |
+
See Also
|
| 1286 |
+
--------
|
| 1287 |
+
hermvander, hermvander3d, hermval2d, hermval3d
|
| 1288 |
+
|
| 1289 |
+
Examples
|
| 1290 |
+
--------
|
| 1291 |
+
>>> from numpy.polynomial.hermite import hermvander3d
|
| 1292 |
+
>>> x = np.array([-1, 0, 1])
|
| 1293 |
+
>>> y = np.array([-1, 0, 1])
|
| 1294 |
+
>>> z = np.array([-1, 0, 1])
|
| 1295 |
+
>>> hermvander3d(x, y, z, [0, 1, 2])
|
| 1296 |
+
array([[ 1., -2., 2., -2., 4., -4.],
|
| 1297 |
+
[ 1., 0., -2., 0., 0., -0.],
|
| 1298 |
+
[ 1., 2., 2., 2., 4., 4.]])
|
| 1299 |
+
|
| 1300 |
+
"""
|
| 1301 |
+
return pu._vander_nd_flat((hermvander, hermvander, hermvander), (x, y, z), deg)
|
| 1302 |
+
|
| 1303 |
+
|
| 1304 |
+
def hermfit(x, y, deg, rcond=None, full=False, w=None):
|
| 1305 |
+
"""
|
| 1306 |
+
Least squares fit of Hermite series to data.
|
| 1307 |
+
|
| 1308 |
+
Return the coefficients of a Hermite series of degree `deg` that is the
|
| 1309 |
+
least squares fit to the data values `y` given at points `x`. If `y` is
|
| 1310 |
+
1-D the returned coefficients will also be 1-D. If `y` is 2-D multiple
|
| 1311 |
+
fits are done, one for each column of `y`, and the resulting
|
| 1312 |
+
coefficients are stored in the corresponding columns of a 2-D return.
|
| 1313 |
+
The fitted polynomial(s) are in the form
|
| 1314 |
+
|
| 1315 |
+
.. math:: p(x) = c_0 + c_1 * H_1(x) + ... + c_n * H_n(x),
|
| 1316 |
+
|
| 1317 |
+
where `n` is `deg`.
|
| 1318 |
+
|
| 1319 |
+
Parameters
|
| 1320 |
+
----------
|
| 1321 |
+
x : array_like, shape (M,)
|
| 1322 |
+
x-coordinates of the M sample points ``(x[i], y[i])``.
|
| 1323 |
+
y : array_like, shape (M,) or (M, K)
|
| 1324 |
+
y-coordinates of the sample points. Several data sets of sample
|
| 1325 |
+
points sharing the same x-coordinates can be fitted at once by
|
| 1326 |
+
passing in a 2D-array that contains one dataset per column.
|
| 1327 |
+
deg : int or 1-D array_like
|
| 1328 |
+
Degree(s) of the fitting polynomials. If `deg` is a single integer
|
| 1329 |
+
all terms up to and including the `deg`'th term are included in the
|
| 1330 |
+
fit. For NumPy versions >= 1.11.0 a list of integers specifying the
|
| 1331 |
+
degrees of the terms to include may be used instead.
|
| 1332 |
+
rcond : float, optional
|
| 1333 |
+
Relative condition number of the fit. Singular values smaller than
|
| 1334 |
+
this relative to the largest singular value will be ignored. The
|
| 1335 |
+
default value is len(x)*eps, where eps is the relative precision of
|
| 1336 |
+
the float type, about 2e-16 in most cases.
|
| 1337 |
+
full : bool, optional
|
| 1338 |
+
Switch determining nature of return value. When it is False (the
|
| 1339 |
+
default) just the coefficients are returned, when True diagnostic
|
| 1340 |
+
information from the singular value decomposition is also returned.
|
| 1341 |
+
w : array_like, shape (`M`,), optional
|
| 1342 |
+
Weights. If not None, the weight ``w[i]`` applies to the unsquared
|
| 1343 |
+
residual ``y[i] - y_hat[i]`` at ``x[i]``. Ideally the weights are
|
| 1344 |
+
chosen so that the errors of the products ``w[i]*y[i]`` all have the
|
| 1345 |
+
same variance. When using inverse-variance weighting, use
|
| 1346 |
+
``w[i] = 1/sigma(y[i])``. The default value is None.
|
| 1347 |
+
|
| 1348 |
+
Returns
|
| 1349 |
+
-------
|
| 1350 |
+
coef : ndarray, shape (M,) or (M, K)
|
| 1351 |
+
Hermite coefficients ordered from low to high. If `y` was 2-D,
|
| 1352 |
+
the coefficients for the data in column k of `y` are in column
|
| 1353 |
+
`k`.
|
| 1354 |
+
|
| 1355 |
+
[residuals, rank, singular_values, rcond] : list
|
| 1356 |
+
These values are only returned if ``full == True``
|
| 1357 |
+
|
| 1358 |
+
- residuals -- sum of squared residuals of the least squares fit
|
| 1359 |
+
- rank -- the numerical rank of the scaled Vandermonde matrix
|
| 1360 |
+
- singular_values -- singular values of the scaled Vandermonde matrix
|
| 1361 |
+
- rcond -- value of `rcond`.
|
| 1362 |
+
|
| 1363 |
+
For more details, see `numpy.linalg.lstsq`.
|
| 1364 |
+
|
| 1365 |
+
Warns
|
| 1366 |
+
-----
|
| 1367 |
+
RankWarning
|
| 1368 |
+
The rank of the coefficient matrix in the least-squares fit is
|
| 1369 |
+
deficient. The warning is only raised if ``full == False``. The
|
| 1370 |
+
warnings can be turned off by
|
| 1371 |
+
|
| 1372 |
+
>>> import warnings
|
| 1373 |
+
>>> warnings.simplefilter('ignore', np.exceptions.RankWarning)
|
| 1374 |
+
|
| 1375 |
+
See Also
|
| 1376 |
+
--------
|
| 1377 |
+
numpy.polynomial.chebyshev.chebfit
|
| 1378 |
+
numpy.polynomial.legendre.legfit
|
| 1379 |
+
numpy.polynomial.laguerre.lagfit
|
| 1380 |
+
numpy.polynomial.polynomial.polyfit
|
| 1381 |
+
numpy.polynomial.hermite_e.hermefit
|
| 1382 |
+
hermval : Evaluates a Hermite series.
|
| 1383 |
+
hermvander : Vandermonde matrix of Hermite series.
|
| 1384 |
+
hermweight : Hermite weight function
|
| 1385 |
+
numpy.linalg.lstsq : Computes a least-squares fit from the matrix.
|
| 1386 |
+
scipy.interpolate.UnivariateSpline : Computes spline fits.
|
| 1387 |
+
|
| 1388 |
+
Notes
|
| 1389 |
+
-----
|
| 1390 |
+
The solution is the coefficients of the Hermite series `p` that
|
| 1391 |
+
minimizes the sum of the weighted squared errors
|
| 1392 |
+
|
| 1393 |
+
.. math:: E = \\sum_j w_j^2 * |y_j - p(x_j)|^2,
|
| 1394 |
+
|
| 1395 |
+
where the :math:`w_j` are the weights. This problem is solved by
|
| 1396 |
+
setting up the (typically) overdetermined matrix equation
|
| 1397 |
+
|
| 1398 |
+
.. math:: V(x) * c = w * y,
|
| 1399 |
+
|
| 1400 |
+
where `V` is the weighted pseudo Vandermonde matrix of `x`, `c` are the
|
| 1401 |
+
coefficients to be solved for, `w` are the weights, `y` are the
|
| 1402 |
+
observed values. This equation is then solved using the singular value
|
| 1403 |
+
decomposition of `V`.
|
| 1404 |
+
|
| 1405 |
+
If some of the singular values of `V` are so small that they are
|
| 1406 |
+
neglected, then a `~exceptions.RankWarning` will be issued. This means that
|
| 1407 |
+
the coefficient values may be poorly determined. Using a lower order fit
|
| 1408 |
+
will usually get rid of the warning. The `rcond` parameter can also be
|
| 1409 |
+
set to a value smaller than its default, but the resulting fit may be
|
| 1410 |
+
spurious and have large contributions from roundoff error.
|
| 1411 |
+
|
| 1412 |
+
Fits using Hermite series are probably most useful when the data can be
|
| 1413 |
+
approximated by ``sqrt(w(x)) * p(x)``, where ``w(x)`` is the Hermite
|
| 1414 |
+
weight. In that case the weight ``sqrt(w(x[i]))`` should be used
|
| 1415 |
+
together with data values ``y[i]/sqrt(w(x[i]))``. The weight function is
|
| 1416 |
+
available as `hermweight`.
|
| 1417 |
+
|
| 1418 |
+
References
|
| 1419 |
+
----------
|
| 1420 |
+
.. [1] Wikipedia, "Curve fitting",
|
| 1421 |
+
https://en.wikipedia.org/wiki/Curve_fitting
|
| 1422 |
+
|
| 1423 |
+
Examples
|
| 1424 |
+
--------
|
| 1425 |
+
>>> import numpy as np
|
| 1426 |
+
>>> from numpy.polynomial.hermite import hermfit, hermval
|
| 1427 |
+
>>> x = np.linspace(-10, 10)
|
| 1428 |
+
>>> rng = np.random.default_rng()
|
| 1429 |
+
>>> err = rng.normal(scale=1./10, size=len(x))
|
| 1430 |
+
>>> y = hermval(x, [1, 2, 3]) + err
|
| 1431 |
+
>>> hermfit(x, y, 2)
|
| 1432 |
+
array([1.02294967, 2.00016403, 2.99994614]) # may vary
|
| 1433 |
+
|
| 1434 |
+
"""
|
| 1435 |
+
return pu._fit(hermvander, x, y, deg, rcond, full, w)
|
| 1436 |
+
|
| 1437 |
+
|
| 1438 |
+
def hermcompanion(c):
|
| 1439 |
+
"""Return the scaled companion matrix of c.
|
| 1440 |
+
|
| 1441 |
+
The basis polynomials are scaled so that the companion matrix is
|
| 1442 |
+
symmetric when `c` is a Hermite basis polynomial. This provides
|
| 1443 |
+
better eigenvalue estimates than the unscaled case and for basis
|
| 1444 |
+
polynomials the eigenvalues are guaranteed to be real if
|
| 1445 |
+
`numpy.linalg.eigvalsh` is used to obtain them.
|
| 1446 |
+
|
| 1447 |
+
Parameters
|
| 1448 |
+
----------
|
| 1449 |
+
c : array_like
|
| 1450 |
+
1-D array of Hermite series coefficients ordered from low to high
|
| 1451 |
+
degree.
|
| 1452 |
+
|
| 1453 |
+
Returns
|
| 1454 |
+
-------
|
| 1455 |
+
mat : ndarray
|
| 1456 |
+
Scaled companion matrix of dimensions (deg, deg).
|
| 1457 |
+
|
| 1458 |
+
Examples
|
| 1459 |
+
--------
|
| 1460 |
+
>>> from numpy.polynomial.hermite import hermcompanion
|
| 1461 |
+
>>> hermcompanion([1, 0, 1])
|
| 1462 |
+
array([[0. , 0.35355339],
|
| 1463 |
+
[0.70710678, 0. ]])
|
| 1464 |
+
|
| 1465 |
+
"""
|
| 1466 |
+
# c is a trimmed copy
|
| 1467 |
+
[c] = pu.as_series([c])
|
| 1468 |
+
if len(c) < 2:
|
| 1469 |
+
raise ValueError('Series must have maximum degree of at least 1.')
|
| 1470 |
+
if len(c) == 2:
|
| 1471 |
+
return np.array([[-.5 * c[0] / c[1]]])
|
| 1472 |
+
|
| 1473 |
+
n = len(c) - 1
|
| 1474 |
+
mat = np.zeros((n, n), dtype=c.dtype)
|
| 1475 |
+
scl = np.hstack((1., 1. / np.sqrt(2. * np.arange(n - 1, 0, -1))))
|
| 1476 |
+
scl = np.multiply.accumulate(scl)[::-1]
|
| 1477 |
+
top = mat.reshape(-1)[1::n + 1]
|
| 1478 |
+
bot = mat.reshape(-1)[n::n + 1]
|
| 1479 |
+
top[...] = np.sqrt(.5 * np.arange(1, n))
|
| 1480 |
+
bot[...] = top
|
| 1481 |
+
mat[:, -1] -= scl * c[:-1] / (2.0 * c[-1])
|
| 1482 |
+
return mat
|
| 1483 |
+
|
| 1484 |
+
|
| 1485 |
+
def hermroots(c):
|
| 1486 |
+
"""
|
| 1487 |
+
Compute the roots of a Hermite series.
|
| 1488 |
+
|
| 1489 |
+
Return the roots (a.k.a. "zeros") of the polynomial
|
| 1490 |
+
|
| 1491 |
+
.. math:: p(x) = \\sum_i c[i] * H_i(x).
|
| 1492 |
+
|
| 1493 |
+
Parameters
|
| 1494 |
+
----------
|
| 1495 |
+
c : 1-D array_like
|
| 1496 |
+
1-D array of coefficients.
|
| 1497 |
+
|
| 1498 |
+
Returns
|
| 1499 |
+
-------
|
| 1500 |
+
out : ndarray
|
| 1501 |
+
Array of the roots of the series. If all the roots are real,
|
| 1502 |
+
then `out` is also real, otherwise it is complex.
|
| 1503 |
+
|
| 1504 |
+
See Also
|
| 1505 |
+
--------
|
| 1506 |
+
numpy.polynomial.polynomial.polyroots
|
| 1507 |
+
numpy.polynomial.legendre.legroots
|
| 1508 |
+
numpy.polynomial.laguerre.lagroots
|
| 1509 |
+
numpy.polynomial.chebyshev.chebroots
|
| 1510 |
+
numpy.polynomial.hermite_e.hermeroots
|
| 1511 |
+
|
| 1512 |
+
Notes
|
| 1513 |
+
-----
|
| 1514 |
+
The root estimates are obtained as the eigenvalues of the companion
|
| 1515 |
+
matrix, Roots far from the origin of the complex plane may have large
|
| 1516 |
+
errors due to the numerical instability of the series for such
|
| 1517 |
+
values. Roots with multiplicity greater than 1 will also show larger
|
| 1518 |
+
errors as the value of the series near such points is relatively
|
| 1519 |
+
insensitive to errors in the roots. Isolated roots near the origin can
|
| 1520 |
+
be improved by a few iterations of Newton's method.
|
| 1521 |
+
|
| 1522 |
+
The Hermite series basis polynomials aren't powers of `x` so the
|
| 1523 |
+
results of this function may seem unintuitive.
|
| 1524 |
+
|
| 1525 |
+
Examples
|
| 1526 |
+
--------
|
| 1527 |
+
>>> from numpy.polynomial.hermite import hermroots, hermfromroots
|
| 1528 |
+
>>> coef = hermfromroots([-1, 0, 1])
|
| 1529 |
+
>>> coef
|
| 1530 |
+
array([0. , 0.25 , 0. , 0.125])
|
| 1531 |
+
>>> hermroots(coef)
|
| 1532 |
+
array([-1.00000000e+00, -1.38777878e-17, 1.00000000e+00])
|
| 1533 |
+
|
| 1534 |
+
"""
|
| 1535 |
+
# c is a trimmed copy
|
| 1536 |
+
[c] = pu.as_series([c])
|
| 1537 |
+
if len(c) <= 1:
|
| 1538 |
+
return np.array([], dtype=c.dtype)
|
| 1539 |
+
if len(c) == 2:
|
| 1540 |
+
return np.array([-.5 * c[0] / c[1]])
|
| 1541 |
+
|
| 1542 |
+
# rotated companion matrix reduces error
|
| 1543 |
+
m = hermcompanion(c)[::-1, ::-1]
|
| 1544 |
+
r = np.linalg.eigvals(m)
|
| 1545 |
+
r.sort()
|
| 1546 |
+
return r
|
| 1547 |
+
|
| 1548 |
+
|
| 1549 |
+
def _normed_hermite_n(x, n):
|
| 1550 |
+
"""
|
| 1551 |
+
Evaluate a normalized Hermite polynomial.
|
| 1552 |
+
|
| 1553 |
+
Compute the value of the normalized Hermite polynomial of degree ``n``
|
| 1554 |
+
at the points ``x``.
|
| 1555 |
+
|
| 1556 |
+
|
| 1557 |
+
Parameters
|
| 1558 |
+
----------
|
| 1559 |
+
x : ndarray of double.
|
| 1560 |
+
Points at which to evaluate the function
|
| 1561 |
+
n : int
|
| 1562 |
+
Degree of the normalized Hermite function to be evaluated.
|
| 1563 |
+
|
| 1564 |
+
Returns
|
| 1565 |
+
-------
|
| 1566 |
+
values : ndarray
|
| 1567 |
+
The shape of the return value is described above.
|
| 1568 |
+
|
| 1569 |
+
Notes
|
| 1570 |
+
-----
|
| 1571 |
+
This function is needed for finding the Gauss points and integration
|
| 1572 |
+
weights for high degrees. The values of the standard Hermite functions
|
| 1573 |
+
overflow when n >= 207.
|
| 1574 |
+
|
| 1575 |
+
"""
|
| 1576 |
+
if n == 0:
|
| 1577 |
+
return np.full(x.shape, 1 / np.sqrt(np.sqrt(np.pi)))
|
| 1578 |
+
|
| 1579 |
+
c0 = 0.
|
| 1580 |
+
c1 = 1. / np.sqrt(np.sqrt(np.pi))
|
| 1581 |
+
nd = float(n)
|
| 1582 |
+
for i in range(n - 1):
|
| 1583 |
+
tmp = c0
|
| 1584 |
+
c0 = -c1 * np.sqrt((nd - 1.) / nd)
|
| 1585 |
+
c1 = tmp + c1 * x * np.sqrt(2. / nd)
|
| 1586 |
+
nd = nd - 1.0
|
| 1587 |
+
return c0 + c1 * x * np.sqrt(2)
|
| 1588 |
+
|
| 1589 |
+
|
| 1590 |
+
def hermgauss(deg):
|
| 1591 |
+
"""
|
| 1592 |
+
Gauss-Hermite quadrature.
|
| 1593 |
+
|
| 1594 |
+
Computes the sample points and weights for Gauss-Hermite quadrature.
|
| 1595 |
+
These sample points and weights will correctly integrate polynomials of
|
| 1596 |
+
degree :math:`2*deg - 1` or less over the interval :math:`[-\\inf, \\inf]`
|
| 1597 |
+
with the weight function :math:`f(x) = \\exp(-x^2)`.
|
| 1598 |
+
|
| 1599 |
+
Parameters
|
| 1600 |
+
----------
|
| 1601 |
+
deg : int
|
| 1602 |
+
Number of sample points and weights. It must be >= 1.
|
| 1603 |
+
|
| 1604 |
+
Returns
|
| 1605 |
+
-------
|
| 1606 |
+
x : ndarray
|
| 1607 |
+
1-D ndarray containing the sample points.
|
| 1608 |
+
y : ndarray
|
| 1609 |
+
1-D ndarray containing the weights.
|
| 1610 |
+
|
| 1611 |
+
Notes
|
| 1612 |
+
-----
|
| 1613 |
+
The results have only been tested up to degree 100, higher degrees may
|
| 1614 |
+
be problematic. The weights are determined by using the fact that
|
| 1615 |
+
|
| 1616 |
+
.. math:: w_k = c / (H'_n(x_k) * H_{n-1}(x_k))
|
| 1617 |
+
|
| 1618 |
+
where :math:`c` is a constant independent of :math:`k` and :math:`x_k`
|
| 1619 |
+
is the k'th root of :math:`H_n`, and then scaling the results to get
|
| 1620 |
+
the right value when integrating 1.
|
| 1621 |
+
|
| 1622 |
+
Examples
|
| 1623 |
+
--------
|
| 1624 |
+
>>> from numpy.polynomial.hermite import hermgauss
|
| 1625 |
+
>>> hermgauss(2)
|
| 1626 |
+
(array([-0.70710678, 0.70710678]), array([0.88622693, 0.88622693]))
|
| 1627 |
+
|
| 1628 |
+
"""
|
| 1629 |
+
ideg = pu._as_int(deg, "deg")
|
| 1630 |
+
if ideg <= 0:
|
| 1631 |
+
raise ValueError("deg must be a positive integer")
|
| 1632 |
+
|
| 1633 |
+
# first approximation of roots. We use the fact that the companion
|
| 1634 |
+
# matrix is symmetric in this case in order to obtain better zeros.
|
| 1635 |
+
c = np.array([0] * deg + [1], dtype=np.float64)
|
| 1636 |
+
m = hermcompanion(c)
|
| 1637 |
+
x = np.linalg.eigvalsh(m)
|
| 1638 |
+
|
| 1639 |
+
# improve roots by one application of Newton
|
| 1640 |
+
dy = _normed_hermite_n(x, ideg)
|
| 1641 |
+
df = _normed_hermite_n(x, ideg - 1) * np.sqrt(2 * ideg)
|
| 1642 |
+
x -= dy / df
|
| 1643 |
+
|
| 1644 |
+
# compute the weights. We scale the factor to avoid possible numerical
|
| 1645 |
+
# overflow.
|
| 1646 |
+
fm = _normed_hermite_n(x, ideg - 1)
|
| 1647 |
+
fm /= np.abs(fm).max()
|
| 1648 |
+
w = 1 / (fm * fm)
|
| 1649 |
+
|
| 1650 |
+
# for Hermite we can also symmetrize
|
| 1651 |
+
w = (w + w[::-1]) / 2
|
| 1652 |
+
x = (x - x[::-1]) / 2
|
| 1653 |
+
|
| 1654 |
+
# scale w to get the right value
|
| 1655 |
+
w *= np.sqrt(np.pi) / w.sum()
|
| 1656 |
+
|
| 1657 |
+
return x, w
|
| 1658 |
+
|
| 1659 |
+
|
| 1660 |
+
def hermweight(x):
|
| 1661 |
+
"""
|
| 1662 |
+
Weight function of the Hermite polynomials.
|
| 1663 |
+
|
| 1664 |
+
The weight function is :math:`\\exp(-x^2)` and the interval of
|
| 1665 |
+
integration is :math:`[-\\inf, \\inf]`. the Hermite polynomials are
|
| 1666 |
+
orthogonal, but not normalized, with respect to this weight function.
|
| 1667 |
+
|
| 1668 |
+
Parameters
|
| 1669 |
+
----------
|
| 1670 |
+
x : array_like
|
| 1671 |
+
Values at which the weight function will be computed.
|
| 1672 |
+
|
| 1673 |
+
Returns
|
| 1674 |
+
-------
|
| 1675 |
+
w : ndarray
|
| 1676 |
+
The weight function at `x`.
|
| 1677 |
+
|
| 1678 |
+
Examples
|
| 1679 |
+
--------
|
| 1680 |
+
>>> import numpy as np
|
| 1681 |
+
>>> from numpy.polynomial.hermite import hermweight
|
| 1682 |
+
>>> x = np.arange(-2, 2)
|
| 1683 |
+
>>> hermweight(x)
|
| 1684 |
+
array([0.01831564, 0.36787944, 1. , 0.36787944])
|
| 1685 |
+
|
| 1686 |
+
"""
|
| 1687 |
+
w = np.exp(-x**2)
|
| 1688 |
+
return w
|
| 1689 |
+
|
| 1690 |
+
|
| 1691 |
+
#
|
| 1692 |
+
# Hermite series class
|
| 1693 |
+
#
|
| 1694 |
+
|
| 1695 |
+
class Hermite(ABCPolyBase):
|
| 1696 |
+
"""A Hermite series class.
|
| 1697 |
+
|
| 1698 |
+
The Hermite class provides the standard Python numerical methods
|
| 1699 |
+
'+', '-', '*', '//', '%', 'divmod', '**', and '()' as well as the
|
| 1700 |
+
attributes and methods listed below.
|
| 1701 |
+
|
| 1702 |
+
Parameters
|
| 1703 |
+
----------
|
| 1704 |
+
coef : array_like
|
| 1705 |
+
Hermite coefficients in order of increasing degree, i.e,
|
| 1706 |
+
``(1, 2, 3)`` gives ``1*H_0(x) + 2*H_1(x) + 3*H_2(x)``.
|
| 1707 |
+
domain : (2,) array_like, optional
|
| 1708 |
+
Domain to use. The interval ``[domain[0], domain[1]]`` is mapped
|
| 1709 |
+
to the interval ``[window[0], window[1]]`` by shifting and scaling.
|
| 1710 |
+
The default value is [-1., 1.].
|
| 1711 |
+
window : (2,) array_like, optional
|
| 1712 |
+
Window, see `domain` for its use. The default value is [-1., 1.].
|
| 1713 |
+
symbol : str, optional
|
| 1714 |
+
Symbol used to represent the independent variable in string
|
| 1715 |
+
representations of the polynomial expression, e.g. for printing.
|
| 1716 |
+
The symbol must be a valid Python identifier. Default value is 'x'.
|
| 1717 |
+
|
| 1718 |
+
.. versionadded:: 1.24
|
| 1719 |
+
|
| 1720 |
+
"""
|
| 1721 |
+
# Virtual Functions
|
| 1722 |
+
_add = staticmethod(hermadd)
|
| 1723 |
+
_sub = staticmethod(hermsub)
|
| 1724 |
+
_mul = staticmethod(hermmul)
|
| 1725 |
+
_div = staticmethod(hermdiv)
|
| 1726 |
+
_pow = staticmethod(hermpow)
|
| 1727 |
+
_val = staticmethod(hermval)
|
| 1728 |
+
_int = staticmethod(hermint)
|
| 1729 |
+
_der = staticmethod(hermder)
|
| 1730 |
+
_fit = staticmethod(hermfit)
|
| 1731 |
+
_line = staticmethod(hermline)
|
| 1732 |
+
_roots = staticmethod(hermroots)
|
| 1733 |
+
_fromroots = staticmethod(hermfromroots)
|
| 1734 |
+
|
| 1735 |
+
# Virtual properties
|
| 1736 |
+
domain = np.array(hermdomain)
|
| 1737 |
+
window = np.array(hermdomain)
|
| 1738 |
+
basis_name = 'H'
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/hermite.pyi
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, ClassVar, Final, Literal as L, TypeVar
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
from numpy._typing import _Shape
|
| 5 |
+
|
| 6 |
+
from ._polybase import ABCPolyBase
|
| 7 |
+
from ._polytypes import (
|
| 8 |
+
_Array1,
|
| 9 |
+
_Array2,
|
| 10 |
+
_FuncBinOp,
|
| 11 |
+
_FuncCompanion,
|
| 12 |
+
_FuncDer,
|
| 13 |
+
_FuncFit,
|
| 14 |
+
_FuncFromRoots,
|
| 15 |
+
_FuncGauss,
|
| 16 |
+
_FuncInteg,
|
| 17 |
+
_FuncLine,
|
| 18 |
+
_FuncPoly2Ortho,
|
| 19 |
+
_FuncPow,
|
| 20 |
+
_FuncRoots,
|
| 21 |
+
_FuncUnOp,
|
| 22 |
+
_FuncVal,
|
| 23 |
+
_FuncVal2D,
|
| 24 |
+
_FuncVal3D,
|
| 25 |
+
_FuncVander,
|
| 26 |
+
_FuncVander2D,
|
| 27 |
+
_FuncVander3D,
|
| 28 |
+
_FuncWeight,
|
| 29 |
+
)
|
| 30 |
+
from .polyutils import trimcoef as hermtrim
|
| 31 |
+
|
| 32 |
+
__all__ = [
|
| 33 |
+
"hermzero",
|
| 34 |
+
"hermone",
|
| 35 |
+
"hermx",
|
| 36 |
+
"hermdomain",
|
| 37 |
+
"hermline",
|
| 38 |
+
"hermadd",
|
| 39 |
+
"hermsub",
|
| 40 |
+
"hermmulx",
|
| 41 |
+
"hermmul",
|
| 42 |
+
"hermdiv",
|
| 43 |
+
"hermpow",
|
| 44 |
+
"hermval",
|
| 45 |
+
"hermder",
|
| 46 |
+
"hermint",
|
| 47 |
+
"herm2poly",
|
| 48 |
+
"poly2herm",
|
| 49 |
+
"hermfromroots",
|
| 50 |
+
"hermvander",
|
| 51 |
+
"hermfit",
|
| 52 |
+
"hermtrim",
|
| 53 |
+
"hermroots",
|
| 54 |
+
"Hermite",
|
| 55 |
+
"hermval2d",
|
| 56 |
+
"hermval3d",
|
| 57 |
+
"hermgrid2d",
|
| 58 |
+
"hermgrid3d",
|
| 59 |
+
"hermvander2d",
|
| 60 |
+
"hermvander3d",
|
| 61 |
+
"hermcompanion",
|
| 62 |
+
"hermgauss",
|
| 63 |
+
"hermweight",
|
| 64 |
+
]
|
| 65 |
+
|
| 66 |
+
_ShapeT = TypeVar("_ShapeT", bound=_Shape)
|
| 67 |
+
|
| 68 |
+
poly2herm: Final[_FuncPoly2Ortho] = ...
|
| 69 |
+
herm2poly: Final[_FuncUnOp] = ...
|
| 70 |
+
|
| 71 |
+
hermdomain: Final[_Array2[np.float64]] = ...
|
| 72 |
+
hermzero: Final[_Array1[np.int_]] = ...
|
| 73 |
+
hermone: Final[_Array1[np.int_]] = ...
|
| 74 |
+
hermx: Final[_Array2[np.int_]] = ...
|
| 75 |
+
|
| 76 |
+
hermline: Final[_FuncLine] = ...
|
| 77 |
+
hermfromroots: Final[_FuncFromRoots] = ...
|
| 78 |
+
hermadd: Final[_FuncBinOp] = ...
|
| 79 |
+
hermsub: Final[_FuncBinOp] = ...
|
| 80 |
+
hermmulx: Final[_FuncUnOp] = ...
|
| 81 |
+
hermmul: Final[_FuncBinOp] = ...
|
| 82 |
+
hermdiv: Final[_FuncBinOp] = ...
|
| 83 |
+
hermpow: Final[_FuncPow] = ...
|
| 84 |
+
hermder: Final[_FuncDer] = ...
|
| 85 |
+
hermint: Final[_FuncInteg] = ...
|
| 86 |
+
hermval: Final[_FuncVal] = ...
|
| 87 |
+
hermval2d: Final[_FuncVal2D] = ...
|
| 88 |
+
hermval3d: Final[_FuncVal3D] = ...
|
| 89 |
+
hermgrid2d: Final[_FuncVal2D] = ...
|
| 90 |
+
hermgrid3d: Final[_FuncVal3D] = ...
|
| 91 |
+
hermvander: Final[_FuncVander] = ...
|
| 92 |
+
hermvander2d: Final[_FuncVander2D] = ...
|
| 93 |
+
hermvander3d: Final[_FuncVander3D] = ...
|
| 94 |
+
hermfit: Final[_FuncFit] = ...
|
| 95 |
+
hermcompanion: Final[_FuncCompanion] = ...
|
| 96 |
+
hermroots: Final[_FuncRoots] = ...
|
| 97 |
+
|
| 98 |
+
def _normed_hermite_n(x: np.ndarray[_ShapeT, np.dtype[np.float64]], n: int) -> np.ndarray[_ShapeT, np.dtype[np.float64]]: ...
|
| 99 |
+
|
| 100 |
+
hermgauss: Final[_FuncGauss] = ...
|
| 101 |
+
hermweight: Final[_FuncWeight] = ...
|
| 102 |
+
|
| 103 |
+
class Hermite(ABCPolyBase[L["H"]]):
|
| 104 |
+
basis_name: ClassVar[L["H"]] = "H" # pyright: ignore[reportIncompatibleMethodOverride]
|
| 105 |
+
domain: _Array2[np.float64 | Any] = ... # pyright: ignore[reportIncompatibleMethodOverride]
|
| 106 |
+
window: _Array2[np.float64 | Any] = ... # pyright: ignore[reportIncompatibleMethodOverride]
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/hermite_e.py
ADDED
|
@@ -0,0 +1,1640 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
===================================================================
|
| 3 |
+
HermiteE Series, "Probabilists" (:mod:`numpy.polynomial.hermite_e`)
|
| 4 |
+
===================================================================
|
| 5 |
+
|
| 6 |
+
This module provides a number of objects (mostly functions) useful for
|
| 7 |
+
dealing with Hermite_e series, including a `HermiteE` class that
|
| 8 |
+
encapsulates the usual arithmetic operations. (General information
|
| 9 |
+
on how this module represents and works with such polynomials is in the
|
| 10 |
+
docstring for its "parent" sub-package, `numpy.polynomial`).
|
| 11 |
+
|
| 12 |
+
Classes
|
| 13 |
+
-------
|
| 14 |
+
.. autosummary::
|
| 15 |
+
:toctree: generated/
|
| 16 |
+
|
| 17 |
+
HermiteE
|
| 18 |
+
|
| 19 |
+
Constants
|
| 20 |
+
---------
|
| 21 |
+
.. autosummary::
|
| 22 |
+
:toctree: generated/
|
| 23 |
+
|
| 24 |
+
hermedomain
|
| 25 |
+
hermezero
|
| 26 |
+
hermeone
|
| 27 |
+
hermex
|
| 28 |
+
|
| 29 |
+
Arithmetic
|
| 30 |
+
----------
|
| 31 |
+
.. autosummary::
|
| 32 |
+
:toctree: generated/
|
| 33 |
+
|
| 34 |
+
hermeadd
|
| 35 |
+
hermesub
|
| 36 |
+
hermemulx
|
| 37 |
+
hermemul
|
| 38 |
+
hermediv
|
| 39 |
+
hermepow
|
| 40 |
+
hermeval
|
| 41 |
+
hermeval2d
|
| 42 |
+
hermeval3d
|
| 43 |
+
hermegrid2d
|
| 44 |
+
hermegrid3d
|
| 45 |
+
|
| 46 |
+
Calculus
|
| 47 |
+
--------
|
| 48 |
+
.. autosummary::
|
| 49 |
+
:toctree: generated/
|
| 50 |
+
|
| 51 |
+
hermeder
|
| 52 |
+
hermeint
|
| 53 |
+
|
| 54 |
+
Misc Functions
|
| 55 |
+
--------------
|
| 56 |
+
.. autosummary::
|
| 57 |
+
:toctree: generated/
|
| 58 |
+
|
| 59 |
+
hermefromroots
|
| 60 |
+
hermeroots
|
| 61 |
+
hermevander
|
| 62 |
+
hermevander2d
|
| 63 |
+
hermevander3d
|
| 64 |
+
hermegauss
|
| 65 |
+
hermeweight
|
| 66 |
+
hermecompanion
|
| 67 |
+
hermefit
|
| 68 |
+
hermetrim
|
| 69 |
+
hermeline
|
| 70 |
+
herme2poly
|
| 71 |
+
poly2herme
|
| 72 |
+
|
| 73 |
+
See also
|
| 74 |
+
--------
|
| 75 |
+
`numpy.polynomial`
|
| 76 |
+
|
| 77 |
+
"""
|
| 78 |
+
import numpy as np
|
| 79 |
+
|
| 80 |
+
from . import polyutils as pu
|
| 81 |
+
from ._polybase import ABCPolyBase
|
| 82 |
+
|
| 83 |
+
__all__ = [
|
| 84 |
+
'hermezero', 'hermeone', 'hermex', 'hermedomain', 'hermeline',
|
| 85 |
+
'hermeadd', 'hermesub', 'hermemulx', 'hermemul', 'hermediv',
|
| 86 |
+
'hermepow', 'hermeval', 'hermeder', 'hermeint', 'herme2poly',
|
| 87 |
+
'poly2herme', 'hermefromroots', 'hermevander', 'hermefit', 'hermetrim',
|
| 88 |
+
'hermeroots', 'HermiteE', 'hermeval2d', 'hermeval3d', 'hermegrid2d',
|
| 89 |
+
'hermegrid3d', 'hermevander2d', 'hermevander3d', 'hermecompanion',
|
| 90 |
+
'hermegauss', 'hermeweight']
|
| 91 |
+
|
| 92 |
+
hermetrim = pu.trimcoef
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def poly2herme(pol):
|
| 96 |
+
"""
|
| 97 |
+
poly2herme(pol)
|
| 98 |
+
|
| 99 |
+
Convert a polynomial to a Hermite series.
|
| 100 |
+
|
| 101 |
+
Convert an array representing the coefficients of a polynomial (relative
|
| 102 |
+
to the "standard" basis) ordered from lowest degree to highest, to an
|
| 103 |
+
array of the coefficients of the equivalent Hermite series, ordered
|
| 104 |
+
from lowest to highest degree.
|
| 105 |
+
|
| 106 |
+
Parameters
|
| 107 |
+
----------
|
| 108 |
+
pol : array_like
|
| 109 |
+
1-D array containing the polynomial coefficients
|
| 110 |
+
|
| 111 |
+
Returns
|
| 112 |
+
-------
|
| 113 |
+
c : ndarray
|
| 114 |
+
1-D array containing the coefficients of the equivalent Hermite
|
| 115 |
+
series.
|
| 116 |
+
|
| 117 |
+
See Also
|
| 118 |
+
--------
|
| 119 |
+
herme2poly
|
| 120 |
+
|
| 121 |
+
Notes
|
| 122 |
+
-----
|
| 123 |
+
The easy way to do conversions between polynomial basis sets
|
| 124 |
+
is to use the convert method of a class instance.
|
| 125 |
+
|
| 126 |
+
Examples
|
| 127 |
+
--------
|
| 128 |
+
>>> import numpy as np
|
| 129 |
+
>>> from numpy.polynomial.hermite_e import poly2herme
|
| 130 |
+
>>> poly2herme(np.arange(4))
|
| 131 |
+
array([ 2., 10., 2., 3.])
|
| 132 |
+
|
| 133 |
+
"""
|
| 134 |
+
[pol] = pu.as_series([pol])
|
| 135 |
+
deg = len(pol) - 1
|
| 136 |
+
res = 0
|
| 137 |
+
for i in range(deg, -1, -1):
|
| 138 |
+
res = hermeadd(hermemulx(res), pol[i])
|
| 139 |
+
return res
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
def herme2poly(c):
|
| 143 |
+
"""
|
| 144 |
+
Convert a Hermite series to a polynomial.
|
| 145 |
+
|
| 146 |
+
Convert an array representing the coefficients of a Hermite series,
|
| 147 |
+
ordered from lowest degree to highest, to an array of the coefficients
|
| 148 |
+
of the equivalent polynomial (relative to the "standard" basis) ordered
|
| 149 |
+
from lowest to highest degree.
|
| 150 |
+
|
| 151 |
+
Parameters
|
| 152 |
+
----------
|
| 153 |
+
c : array_like
|
| 154 |
+
1-D array containing the Hermite series coefficients, ordered
|
| 155 |
+
from lowest order term to highest.
|
| 156 |
+
|
| 157 |
+
Returns
|
| 158 |
+
-------
|
| 159 |
+
pol : ndarray
|
| 160 |
+
1-D array containing the coefficients of the equivalent polynomial
|
| 161 |
+
(relative to the "standard" basis) ordered from lowest order term
|
| 162 |
+
to highest.
|
| 163 |
+
|
| 164 |
+
See Also
|
| 165 |
+
--------
|
| 166 |
+
poly2herme
|
| 167 |
+
|
| 168 |
+
Notes
|
| 169 |
+
-----
|
| 170 |
+
The easy way to do conversions between polynomial basis sets
|
| 171 |
+
is to use the convert method of a class instance.
|
| 172 |
+
|
| 173 |
+
Examples
|
| 174 |
+
--------
|
| 175 |
+
>>> from numpy.polynomial.hermite_e import herme2poly
|
| 176 |
+
>>> herme2poly([ 2., 10., 2., 3.])
|
| 177 |
+
array([0., 1., 2., 3.])
|
| 178 |
+
|
| 179 |
+
"""
|
| 180 |
+
from .polynomial import polyadd, polymulx, polysub
|
| 181 |
+
|
| 182 |
+
[c] = pu.as_series([c])
|
| 183 |
+
n = len(c)
|
| 184 |
+
if n == 1:
|
| 185 |
+
return c
|
| 186 |
+
if n == 2:
|
| 187 |
+
return c
|
| 188 |
+
else:
|
| 189 |
+
c0 = c[-2]
|
| 190 |
+
c1 = c[-1]
|
| 191 |
+
# i is the current degree of c1
|
| 192 |
+
for i in range(n - 1, 1, -1):
|
| 193 |
+
tmp = c0
|
| 194 |
+
c0 = polysub(c[i - 2], c1 * (i - 1))
|
| 195 |
+
c1 = polyadd(tmp, polymulx(c1))
|
| 196 |
+
return polyadd(c0, polymulx(c1))
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
#
|
| 200 |
+
# These are constant arrays are of integer type so as to be compatible
|
| 201 |
+
# with the widest range of other types, such as Decimal.
|
| 202 |
+
#
|
| 203 |
+
|
| 204 |
+
# Hermite
|
| 205 |
+
hermedomain = np.array([-1., 1.])
|
| 206 |
+
|
| 207 |
+
# Hermite coefficients representing zero.
|
| 208 |
+
hermezero = np.array([0])
|
| 209 |
+
|
| 210 |
+
# Hermite coefficients representing one.
|
| 211 |
+
hermeone = np.array([1])
|
| 212 |
+
|
| 213 |
+
# Hermite coefficients representing the identity x.
|
| 214 |
+
hermex = np.array([0, 1])
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
def hermeline(off, scl):
|
| 218 |
+
"""
|
| 219 |
+
Hermite series whose graph is a straight line.
|
| 220 |
+
|
| 221 |
+
Parameters
|
| 222 |
+
----------
|
| 223 |
+
off, scl : scalars
|
| 224 |
+
The specified line is given by ``off + scl*x``.
|
| 225 |
+
|
| 226 |
+
Returns
|
| 227 |
+
-------
|
| 228 |
+
y : ndarray
|
| 229 |
+
This module's representation of the Hermite series for
|
| 230 |
+
``off + scl*x``.
|
| 231 |
+
|
| 232 |
+
See Also
|
| 233 |
+
--------
|
| 234 |
+
numpy.polynomial.polynomial.polyline
|
| 235 |
+
numpy.polynomial.chebyshev.chebline
|
| 236 |
+
numpy.polynomial.legendre.legline
|
| 237 |
+
numpy.polynomial.laguerre.lagline
|
| 238 |
+
numpy.polynomial.hermite.hermline
|
| 239 |
+
|
| 240 |
+
Examples
|
| 241 |
+
--------
|
| 242 |
+
>>> from numpy.polynomial.hermite_e import hermeline
|
| 243 |
+
>>> from numpy.polynomial.hermite_e import hermeline, hermeval
|
| 244 |
+
>>> hermeval(0,hermeline(3, 2))
|
| 245 |
+
3.0
|
| 246 |
+
>>> hermeval(1,hermeline(3, 2))
|
| 247 |
+
5.0
|
| 248 |
+
|
| 249 |
+
"""
|
| 250 |
+
if scl != 0:
|
| 251 |
+
return np.array([off, scl])
|
| 252 |
+
else:
|
| 253 |
+
return np.array([off])
|
| 254 |
+
|
| 255 |
+
|
| 256 |
+
def hermefromroots(roots):
|
| 257 |
+
"""
|
| 258 |
+
Generate a HermiteE series with given roots.
|
| 259 |
+
|
| 260 |
+
The function returns the coefficients of the polynomial
|
| 261 |
+
|
| 262 |
+
.. math:: p(x) = (x - r_0) * (x - r_1) * ... * (x - r_n),
|
| 263 |
+
|
| 264 |
+
in HermiteE form, where the :math:`r_n` are the roots specified in `roots`.
|
| 265 |
+
If a zero has multiplicity n, then it must appear in `roots` n times.
|
| 266 |
+
For instance, if 2 is a root of multiplicity three and 3 is a root of
|
| 267 |
+
multiplicity 2, then `roots` looks something like [2, 2, 2, 3, 3]. The
|
| 268 |
+
roots can appear in any order.
|
| 269 |
+
|
| 270 |
+
If the returned coefficients are `c`, then
|
| 271 |
+
|
| 272 |
+
.. math:: p(x) = c_0 + c_1 * He_1(x) + ... + c_n * He_n(x)
|
| 273 |
+
|
| 274 |
+
The coefficient of the last term is not generally 1 for monic
|
| 275 |
+
polynomials in HermiteE form.
|
| 276 |
+
|
| 277 |
+
Parameters
|
| 278 |
+
----------
|
| 279 |
+
roots : array_like
|
| 280 |
+
Sequence containing the roots.
|
| 281 |
+
|
| 282 |
+
Returns
|
| 283 |
+
-------
|
| 284 |
+
out : ndarray
|
| 285 |
+
1-D array of coefficients. If all roots are real then `out` is a
|
| 286 |
+
real array, if some of the roots are complex, then `out` is complex
|
| 287 |
+
even if all the coefficients in the result are real (see Examples
|
| 288 |
+
below).
|
| 289 |
+
|
| 290 |
+
See Also
|
| 291 |
+
--------
|
| 292 |
+
numpy.polynomial.polynomial.polyfromroots
|
| 293 |
+
numpy.polynomial.legendre.legfromroots
|
| 294 |
+
numpy.polynomial.laguerre.lagfromroots
|
| 295 |
+
numpy.polynomial.hermite.hermfromroots
|
| 296 |
+
numpy.polynomial.chebyshev.chebfromroots
|
| 297 |
+
|
| 298 |
+
Examples
|
| 299 |
+
--------
|
| 300 |
+
>>> from numpy.polynomial.hermite_e import hermefromroots, hermeval
|
| 301 |
+
>>> coef = hermefromroots((-1, 0, 1))
|
| 302 |
+
>>> hermeval((-1, 0, 1), coef)
|
| 303 |
+
array([0., 0., 0.])
|
| 304 |
+
>>> coef = hermefromroots((-1j, 1j))
|
| 305 |
+
>>> hermeval((-1j, 1j), coef)
|
| 306 |
+
array([0.+0.j, 0.+0.j])
|
| 307 |
+
|
| 308 |
+
"""
|
| 309 |
+
return pu._fromroots(hermeline, hermemul, roots)
|
| 310 |
+
|
| 311 |
+
|
| 312 |
+
def hermeadd(c1, c2):
|
| 313 |
+
"""
|
| 314 |
+
Add one Hermite series to another.
|
| 315 |
+
|
| 316 |
+
Returns the sum of two Hermite series `c1` + `c2`. The arguments
|
| 317 |
+
are sequences of coefficients ordered from lowest order term to
|
| 318 |
+
highest, i.e., [1,2,3] represents the series ``P_0 + 2*P_1 + 3*P_2``.
|
| 319 |
+
|
| 320 |
+
Parameters
|
| 321 |
+
----------
|
| 322 |
+
c1, c2 : array_like
|
| 323 |
+
1-D arrays of Hermite series coefficients ordered from low to
|
| 324 |
+
high.
|
| 325 |
+
|
| 326 |
+
Returns
|
| 327 |
+
-------
|
| 328 |
+
out : ndarray
|
| 329 |
+
Array representing the Hermite series of their sum.
|
| 330 |
+
|
| 331 |
+
See Also
|
| 332 |
+
--------
|
| 333 |
+
hermesub, hermemulx, hermemul, hermediv, hermepow
|
| 334 |
+
|
| 335 |
+
Notes
|
| 336 |
+
-----
|
| 337 |
+
Unlike multiplication, division, etc., the sum of two Hermite series
|
| 338 |
+
is a Hermite series (without having to "reproject" the result onto
|
| 339 |
+
the basis set) so addition, just like that of "standard" polynomials,
|
| 340 |
+
is simply "component-wise."
|
| 341 |
+
|
| 342 |
+
Examples
|
| 343 |
+
--------
|
| 344 |
+
>>> from numpy.polynomial.hermite_e import hermeadd
|
| 345 |
+
>>> hermeadd([1, 2, 3], [1, 2, 3, 4])
|
| 346 |
+
array([2., 4., 6., 4.])
|
| 347 |
+
|
| 348 |
+
"""
|
| 349 |
+
return pu._add(c1, c2)
|
| 350 |
+
|
| 351 |
+
|
| 352 |
+
def hermesub(c1, c2):
|
| 353 |
+
"""
|
| 354 |
+
Subtract one Hermite series from another.
|
| 355 |
+
|
| 356 |
+
Returns the difference of two Hermite series `c1` - `c2`. The
|
| 357 |
+
sequences of coefficients are from lowest order term to highest, i.e.,
|
| 358 |
+
[1,2,3] represents the series ``P_0 + 2*P_1 + 3*P_2``.
|
| 359 |
+
|
| 360 |
+
Parameters
|
| 361 |
+
----------
|
| 362 |
+
c1, c2 : array_like
|
| 363 |
+
1-D arrays of Hermite series coefficients ordered from low to
|
| 364 |
+
high.
|
| 365 |
+
|
| 366 |
+
Returns
|
| 367 |
+
-------
|
| 368 |
+
out : ndarray
|
| 369 |
+
Of Hermite series coefficients representing their difference.
|
| 370 |
+
|
| 371 |
+
See Also
|
| 372 |
+
--------
|
| 373 |
+
hermeadd, hermemulx, hermemul, hermediv, hermepow
|
| 374 |
+
|
| 375 |
+
Notes
|
| 376 |
+
-----
|
| 377 |
+
Unlike multiplication, division, etc., the difference of two Hermite
|
| 378 |
+
series is a Hermite series (without having to "reproject" the result
|
| 379 |
+
onto the basis set) so subtraction, just like that of "standard"
|
| 380 |
+
polynomials, is simply "component-wise."
|
| 381 |
+
|
| 382 |
+
Examples
|
| 383 |
+
--------
|
| 384 |
+
>>> from numpy.polynomial.hermite_e import hermesub
|
| 385 |
+
>>> hermesub([1, 2, 3, 4], [1, 2, 3])
|
| 386 |
+
array([0., 0., 0., 4.])
|
| 387 |
+
|
| 388 |
+
"""
|
| 389 |
+
return pu._sub(c1, c2)
|
| 390 |
+
|
| 391 |
+
|
| 392 |
+
def hermemulx(c):
|
| 393 |
+
"""Multiply a Hermite series by x.
|
| 394 |
+
|
| 395 |
+
Multiply the Hermite series `c` by x, where x is the independent
|
| 396 |
+
variable.
|
| 397 |
+
|
| 398 |
+
|
| 399 |
+
Parameters
|
| 400 |
+
----------
|
| 401 |
+
c : array_like
|
| 402 |
+
1-D array of Hermite series coefficients ordered from low to
|
| 403 |
+
high.
|
| 404 |
+
|
| 405 |
+
Returns
|
| 406 |
+
-------
|
| 407 |
+
out : ndarray
|
| 408 |
+
Array representing the result of the multiplication.
|
| 409 |
+
|
| 410 |
+
See Also
|
| 411 |
+
--------
|
| 412 |
+
hermeadd, hermesub, hermemul, hermediv, hermepow
|
| 413 |
+
|
| 414 |
+
Notes
|
| 415 |
+
-----
|
| 416 |
+
The multiplication uses the recursion relationship for Hermite
|
| 417 |
+
polynomials in the form
|
| 418 |
+
|
| 419 |
+
.. math::
|
| 420 |
+
|
| 421 |
+
xP_i(x) = (P_{i + 1}(x) + iP_{i - 1}(x)))
|
| 422 |
+
|
| 423 |
+
Examples
|
| 424 |
+
--------
|
| 425 |
+
>>> from numpy.polynomial.hermite_e import hermemulx
|
| 426 |
+
>>> hermemulx([1, 2, 3])
|
| 427 |
+
array([2., 7., 2., 3.])
|
| 428 |
+
|
| 429 |
+
"""
|
| 430 |
+
# c is a trimmed copy
|
| 431 |
+
[c] = pu.as_series([c])
|
| 432 |
+
# The zero series needs special treatment
|
| 433 |
+
if len(c) == 1 and c[0] == 0:
|
| 434 |
+
return c
|
| 435 |
+
|
| 436 |
+
prd = np.empty(len(c) + 1, dtype=c.dtype)
|
| 437 |
+
prd[0] = c[0] * 0
|
| 438 |
+
prd[1] = c[0]
|
| 439 |
+
for i in range(1, len(c)):
|
| 440 |
+
prd[i + 1] = c[i]
|
| 441 |
+
prd[i - 1] += c[i] * i
|
| 442 |
+
return prd
|
| 443 |
+
|
| 444 |
+
|
| 445 |
+
def hermemul(c1, c2):
|
| 446 |
+
"""
|
| 447 |
+
Multiply one Hermite series by another.
|
| 448 |
+
|
| 449 |
+
Returns the product of two Hermite series `c1` * `c2`. The arguments
|
| 450 |
+
are sequences of coefficients, from lowest order "term" to highest,
|
| 451 |
+
e.g., [1,2,3] represents the series ``P_0 + 2*P_1 + 3*P_2``.
|
| 452 |
+
|
| 453 |
+
Parameters
|
| 454 |
+
----------
|
| 455 |
+
c1, c2 : array_like
|
| 456 |
+
1-D arrays of Hermite series coefficients ordered from low to
|
| 457 |
+
high.
|
| 458 |
+
|
| 459 |
+
Returns
|
| 460 |
+
-------
|
| 461 |
+
out : ndarray
|
| 462 |
+
Of Hermite series coefficients representing their product.
|
| 463 |
+
|
| 464 |
+
See Also
|
| 465 |
+
--------
|
| 466 |
+
hermeadd, hermesub, hermemulx, hermediv, hermepow
|
| 467 |
+
|
| 468 |
+
Notes
|
| 469 |
+
-----
|
| 470 |
+
In general, the (polynomial) product of two C-series results in terms
|
| 471 |
+
that are not in the Hermite polynomial basis set. Thus, to express
|
| 472 |
+
the product as a Hermite series, it is necessary to "reproject" the
|
| 473 |
+
product onto said basis set, which may produce "unintuitive" (but
|
| 474 |
+
correct) results; see Examples section below.
|
| 475 |
+
|
| 476 |
+
Examples
|
| 477 |
+
--------
|
| 478 |
+
>>> from numpy.polynomial.hermite_e import hermemul
|
| 479 |
+
>>> hermemul([1, 2, 3], [0, 1, 2])
|
| 480 |
+
array([14., 15., 28., 7., 6.])
|
| 481 |
+
|
| 482 |
+
"""
|
| 483 |
+
# s1, s2 are trimmed copies
|
| 484 |
+
[c1, c2] = pu.as_series([c1, c2])
|
| 485 |
+
|
| 486 |
+
if len(c1) > len(c2):
|
| 487 |
+
c = c2
|
| 488 |
+
xs = c1
|
| 489 |
+
else:
|
| 490 |
+
c = c1
|
| 491 |
+
xs = c2
|
| 492 |
+
|
| 493 |
+
if len(c) == 1:
|
| 494 |
+
c0 = c[0] * xs
|
| 495 |
+
c1 = 0
|
| 496 |
+
elif len(c) == 2:
|
| 497 |
+
c0 = c[0] * xs
|
| 498 |
+
c1 = c[1] * xs
|
| 499 |
+
else:
|
| 500 |
+
nd = len(c)
|
| 501 |
+
c0 = c[-2] * xs
|
| 502 |
+
c1 = c[-1] * xs
|
| 503 |
+
for i in range(3, len(c) + 1):
|
| 504 |
+
tmp = c0
|
| 505 |
+
nd = nd - 1
|
| 506 |
+
c0 = hermesub(c[-i] * xs, c1 * (nd - 1))
|
| 507 |
+
c1 = hermeadd(tmp, hermemulx(c1))
|
| 508 |
+
return hermeadd(c0, hermemulx(c1))
|
| 509 |
+
|
| 510 |
+
|
| 511 |
+
def hermediv(c1, c2):
|
| 512 |
+
"""
|
| 513 |
+
Divide one Hermite series by another.
|
| 514 |
+
|
| 515 |
+
Returns the quotient-with-remainder of two Hermite series
|
| 516 |
+
`c1` / `c2`. The arguments are sequences of coefficients from lowest
|
| 517 |
+
order "term" to highest, e.g., [1,2,3] represents the series
|
| 518 |
+
``P_0 + 2*P_1 + 3*P_2``.
|
| 519 |
+
|
| 520 |
+
Parameters
|
| 521 |
+
----------
|
| 522 |
+
c1, c2 : array_like
|
| 523 |
+
1-D arrays of Hermite series coefficients ordered from low to
|
| 524 |
+
high.
|
| 525 |
+
|
| 526 |
+
Returns
|
| 527 |
+
-------
|
| 528 |
+
[quo, rem] : ndarrays
|
| 529 |
+
Of Hermite series coefficients representing the quotient and
|
| 530 |
+
remainder.
|
| 531 |
+
|
| 532 |
+
See Also
|
| 533 |
+
--------
|
| 534 |
+
hermeadd, hermesub, hermemulx, hermemul, hermepow
|
| 535 |
+
|
| 536 |
+
Notes
|
| 537 |
+
-----
|
| 538 |
+
In general, the (polynomial) division of one Hermite series by another
|
| 539 |
+
results in quotient and remainder terms that are not in the Hermite
|
| 540 |
+
polynomial basis set. Thus, to express these results as a Hermite
|
| 541 |
+
series, it is necessary to "reproject" the results onto the Hermite
|
| 542 |
+
basis set, which may produce "unintuitive" (but correct) results; see
|
| 543 |
+
Examples section below.
|
| 544 |
+
|
| 545 |
+
Examples
|
| 546 |
+
--------
|
| 547 |
+
>>> from numpy.polynomial.hermite_e import hermediv
|
| 548 |
+
>>> hermediv([ 14., 15., 28., 7., 6.], [0, 1, 2])
|
| 549 |
+
(array([1., 2., 3.]), array([0.]))
|
| 550 |
+
>>> hermediv([ 15., 17., 28., 7., 6.], [0, 1, 2])
|
| 551 |
+
(array([1., 2., 3.]), array([1., 2.]))
|
| 552 |
+
|
| 553 |
+
"""
|
| 554 |
+
return pu._div(hermemul, c1, c2)
|
| 555 |
+
|
| 556 |
+
|
| 557 |
+
def hermepow(c, pow, maxpower=16):
|
| 558 |
+
"""Raise a Hermite series to a power.
|
| 559 |
+
|
| 560 |
+
Returns the Hermite series `c` raised to the power `pow`. The
|
| 561 |
+
argument `c` is a sequence of coefficients ordered from low to high.
|
| 562 |
+
i.e., [1,2,3] is the series ``P_0 + 2*P_1 + 3*P_2.``
|
| 563 |
+
|
| 564 |
+
Parameters
|
| 565 |
+
----------
|
| 566 |
+
c : array_like
|
| 567 |
+
1-D array of Hermite series coefficients ordered from low to
|
| 568 |
+
high.
|
| 569 |
+
pow : integer
|
| 570 |
+
Power to which the series will be raised
|
| 571 |
+
maxpower : integer, optional
|
| 572 |
+
Maximum power allowed. This is mainly to limit growth of the series
|
| 573 |
+
to unmanageable size. Default is 16
|
| 574 |
+
|
| 575 |
+
Returns
|
| 576 |
+
-------
|
| 577 |
+
coef : ndarray
|
| 578 |
+
Hermite series of power.
|
| 579 |
+
|
| 580 |
+
See Also
|
| 581 |
+
--------
|
| 582 |
+
hermeadd, hermesub, hermemulx, hermemul, hermediv
|
| 583 |
+
|
| 584 |
+
Examples
|
| 585 |
+
--------
|
| 586 |
+
>>> from numpy.polynomial.hermite_e import hermepow
|
| 587 |
+
>>> hermepow([1, 2, 3], 2)
|
| 588 |
+
array([23., 28., 46., 12., 9.])
|
| 589 |
+
|
| 590 |
+
"""
|
| 591 |
+
return pu._pow(hermemul, c, pow, maxpower)
|
| 592 |
+
|
| 593 |
+
|
| 594 |
+
def hermeder(c, m=1, scl=1, axis=0):
|
| 595 |
+
"""
|
| 596 |
+
Differentiate a Hermite_e series.
|
| 597 |
+
|
| 598 |
+
Returns the series coefficients `c` differentiated `m` times along
|
| 599 |
+
`axis`. At each iteration the result is multiplied by `scl` (the
|
| 600 |
+
scaling factor is for use in a linear change of variable). The argument
|
| 601 |
+
`c` is an array of coefficients from low to high degree along each
|
| 602 |
+
axis, e.g., [1,2,3] represents the series ``1*He_0 + 2*He_1 + 3*He_2``
|
| 603 |
+
while [[1,2],[1,2]] represents ``1*He_0(x)*He_0(y) + 1*He_1(x)*He_0(y)
|
| 604 |
+
+ 2*He_0(x)*He_1(y) + 2*He_1(x)*He_1(y)`` if axis=0 is ``x`` and axis=1
|
| 605 |
+
is ``y``.
|
| 606 |
+
|
| 607 |
+
Parameters
|
| 608 |
+
----------
|
| 609 |
+
c : array_like
|
| 610 |
+
Array of Hermite_e series coefficients. If `c` is multidimensional
|
| 611 |
+
the different axis correspond to different variables with the
|
| 612 |
+
degree in each axis given by the corresponding index.
|
| 613 |
+
m : int, optional
|
| 614 |
+
Number of derivatives taken, must be non-negative. (Default: 1)
|
| 615 |
+
scl : scalar, optional
|
| 616 |
+
Each differentiation is multiplied by `scl`. The end result is
|
| 617 |
+
multiplication by ``scl**m``. This is for use in a linear change of
|
| 618 |
+
variable. (Default: 1)
|
| 619 |
+
axis : int, optional
|
| 620 |
+
Axis over which the derivative is taken. (Default: 0).
|
| 621 |
+
|
| 622 |
+
Returns
|
| 623 |
+
-------
|
| 624 |
+
der : ndarray
|
| 625 |
+
Hermite series of the derivative.
|
| 626 |
+
|
| 627 |
+
See Also
|
| 628 |
+
--------
|
| 629 |
+
hermeint
|
| 630 |
+
|
| 631 |
+
Notes
|
| 632 |
+
-----
|
| 633 |
+
In general, the result of differentiating a Hermite series does not
|
| 634 |
+
resemble the same operation on a power series. Thus the result of this
|
| 635 |
+
function may be "unintuitive," albeit correct; see Examples section
|
| 636 |
+
below.
|
| 637 |
+
|
| 638 |
+
Examples
|
| 639 |
+
--------
|
| 640 |
+
>>> from numpy.polynomial.hermite_e import hermeder
|
| 641 |
+
>>> hermeder([ 1., 1., 1., 1.])
|
| 642 |
+
array([1., 2., 3.])
|
| 643 |
+
>>> hermeder([-0.25, 1., 1./2., 1./3., 1./4 ], m=2)
|
| 644 |
+
array([1., 2., 3.])
|
| 645 |
+
|
| 646 |
+
"""
|
| 647 |
+
c = np.array(c, ndmin=1, copy=True)
|
| 648 |
+
if c.dtype.char in '?bBhHiIlLqQpP':
|
| 649 |
+
c = c.astype(np.double)
|
| 650 |
+
cnt = pu._as_int(m, "the order of derivation")
|
| 651 |
+
iaxis = pu._as_int(axis, "the axis")
|
| 652 |
+
if cnt < 0:
|
| 653 |
+
raise ValueError("The order of derivation must be non-negative")
|
| 654 |
+
iaxis = np.lib.array_utils.normalize_axis_index(iaxis, c.ndim)
|
| 655 |
+
|
| 656 |
+
if cnt == 0:
|
| 657 |
+
return c
|
| 658 |
+
|
| 659 |
+
c = np.moveaxis(c, iaxis, 0)
|
| 660 |
+
n = len(c)
|
| 661 |
+
if cnt >= n:
|
| 662 |
+
return c[:1] * 0
|
| 663 |
+
else:
|
| 664 |
+
for i in range(cnt):
|
| 665 |
+
n = n - 1
|
| 666 |
+
c *= scl
|
| 667 |
+
der = np.empty((n,) + c.shape[1:], dtype=c.dtype)
|
| 668 |
+
for j in range(n, 0, -1):
|
| 669 |
+
der[j - 1] = j * c[j]
|
| 670 |
+
c = der
|
| 671 |
+
c = np.moveaxis(c, 0, iaxis)
|
| 672 |
+
return c
|
| 673 |
+
|
| 674 |
+
|
| 675 |
+
def hermeint(c, m=1, k=[], lbnd=0, scl=1, axis=0):
|
| 676 |
+
"""
|
| 677 |
+
Integrate a Hermite_e series.
|
| 678 |
+
|
| 679 |
+
Returns the Hermite_e series coefficients `c` integrated `m` times from
|
| 680 |
+
`lbnd` along `axis`. At each iteration the resulting series is
|
| 681 |
+
**multiplied** by `scl` and an integration constant, `k`, is added.
|
| 682 |
+
The scaling factor is for use in a linear change of variable. ("Buyer
|
| 683 |
+
beware": note that, depending on what one is doing, one may want `scl`
|
| 684 |
+
to be the reciprocal of what one might expect; for more information,
|
| 685 |
+
see the Notes section below.) The argument `c` is an array of
|
| 686 |
+
coefficients from low to high degree along each axis, e.g., [1,2,3]
|
| 687 |
+
represents the series ``H_0 + 2*H_1 + 3*H_2`` while [[1,2],[1,2]]
|
| 688 |
+
represents ``1*H_0(x)*H_0(y) + 1*H_1(x)*H_0(y) + 2*H_0(x)*H_1(y) +
|
| 689 |
+
2*H_1(x)*H_1(y)`` if axis=0 is ``x`` and axis=1 is ``y``.
|
| 690 |
+
|
| 691 |
+
Parameters
|
| 692 |
+
----------
|
| 693 |
+
c : array_like
|
| 694 |
+
Array of Hermite_e series coefficients. If c is multidimensional
|
| 695 |
+
the different axis correspond to different variables with the
|
| 696 |
+
degree in each axis given by the corresponding index.
|
| 697 |
+
m : int, optional
|
| 698 |
+
Order of integration, must be positive. (Default: 1)
|
| 699 |
+
k : {[], list, scalar}, optional
|
| 700 |
+
Integration constant(s). The value of the first integral at
|
| 701 |
+
``lbnd`` is the first value in the list, the value of the second
|
| 702 |
+
integral at ``lbnd`` is the second value, etc. If ``k == []`` (the
|
| 703 |
+
default), all constants are set to zero. If ``m == 1``, a single
|
| 704 |
+
scalar can be given instead of a list.
|
| 705 |
+
lbnd : scalar, optional
|
| 706 |
+
The lower bound of the integral. (Default: 0)
|
| 707 |
+
scl : scalar, optional
|
| 708 |
+
Following each integration the result is *multiplied* by `scl`
|
| 709 |
+
before the integration constant is added. (Default: 1)
|
| 710 |
+
axis : int, optional
|
| 711 |
+
Axis over which the integral is taken. (Default: 0).
|
| 712 |
+
|
| 713 |
+
Returns
|
| 714 |
+
-------
|
| 715 |
+
S : ndarray
|
| 716 |
+
Hermite_e series coefficients of the integral.
|
| 717 |
+
|
| 718 |
+
Raises
|
| 719 |
+
------
|
| 720 |
+
ValueError
|
| 721 |
+
If ``m < 0``, ``len(k) > m``, ``np.ndim(lbnd) != 0``, or
|
| 722 |
+
``np.ndim(scl) != 0``.
|
| 723 |
+
|
| 724 |
+
See Also
|
| 725 |
+
--------
|
| 726 |
+
hermeder
|
| 727 |
+
|
| 728 |
+
Notes
|
| 729 |
+
-----
|
| 730 |
+
Note that the result of each integration is *multiplied* by `scl`.
|
| 731 |
+
Why is this important to note? Say one is making a linear change of
|
| 732 |
+
variable :math:`u = ax + b` in an integral relative to `x`. Then
|
| 733 |
+
:math:`dx = du/a`, so one will need to set `scl` equal to
|
| 734 |
+
:math:`1/a` - perhaps not what one would have first thought.
|
| 735 |
+
|
| 736 |
+
Also note that, in general, the result of integrating a C-series needs
|
| 737 |
+
to be "reprojected" onto the C-series basis set. Thus, typically,
|
| 738 |
+
the result of this function is "unintuitive," albeit correct; see
|
| 739 |
+
Examples section below.
|
| 740 |
+
|
| 741 |
+
Examples
|
| 742 |
+
--------
|
| 743 |
+
>>> from numpy.polynomial.hermite_e import hermeint
|
| 744 |
+
>>> hermeint([1, 2, 3]) # integrate once, value 0 at 0.
|
| 745 |
+
array([1., 1., 1., 1.])
|
| 746 |
+
>>> hermeint([1, 2, 3], m=2) # integrate twice, value & deriv 0 at 0
|
| 747 |
+
array([-0.25 , 1. , 0.5 , 0.33333333, 0.25 ]) # may vary
|
| 748 |
+
>>> hermeint([1, 2, 3], k=1) # integrate once, value 1 at 0.
|
| 749 |
+
array([2., 1., 1., 1.])
|
| 750 |
+
>>> hermeint([1, 2, 3], lbnd=-1) # integrate once, value 0 at -1
|
| 751 |
+
array([-1., 1., 1., 1.])
|
| 752 |
+
>>> hermeint([1, 2, 3], m=2, k=[1, 2], lbnd=-1)
|
| 753 |
+
array([ 1.83333333, 0. , 0.5 , 0.33333333, 0.25 ]) # may vary
|
| 754 |
+
|
| 755 |
+
"""
|
| 756 |
+
c = np.array(c, ndmin=1, copy=True)
|
| 757 |
+
if c.dtype.char in '?bBhHiIlLqQpP':
|
| 758 |
+
c = c.astype(np.double)
|
| 759 |
+
if not np.iterable(k):
|
| 760 |
+
k = [k]
|
| 761 |
+
cnt = pu._as_int(m, "the order of integration")
|
| 762 |
+
iaxis = pu._as_int(axis, "the axis")
|
| 763 |
+
if cnt < 0:
|
| 764 |
+
raise ValueError("The order of integration must be non-negative")
|
| 765 |
+
if len(k) > cnt:
|
| 766 |
+
raise ValueError("Too many integration constants")
|
| 767 |
+
if np.ndim(lbnd) != 0:
|
| 768 |
+
raise ValueError("lbnd must be a scalar.")
|
| 769 |
+
if np.ndim(scl) != 0:
|
| 770 |
+
raise ValueError("scl must be a scalar.")
|
| 771 |
+
iaxis = np.lib.array_utils.normalize_axis_index(iaxis, c.ndim)
|
| 772 |
+
|
| 773 |
+
if cnt == 0:
|
| 774 |
+
return c
|
| 775 |
+
|
| 776 |
+
c = np.moveaxis(c, iaxis, 0)
|
| 777 |
+
k = list(k) + [0] * (cnt - len(k))
|
| 778 |
+
for i in range(cnt):
|
| 779 |
+
n = len(c)
|
| 780 |
+
c *= scl
|
| 781 |
+
if n == 1 and np.all(c[0] == 0):
|
| 782 |
+
c[0] += k[i]
|
| 783 |
+
else:
|
| 784 |
+
tmp = np.empty((n + 1,) + c.shape[1:], dtype=c.dtype)
|
| 785 |
+
tmp[0] = c[0] * 0
|
| 786 |
+
tmp[1] = c[0]
|
| 787 |
+
for j in range(1, n):
|
| 788 |
+
tmp[j + 1] = c[j] / (j + 1)
|
| 789 |
+
tmp[0] += k[i] - hermeval(lbnd, tmp)
|
| 790 |
+
c = tmp
|
| 791 |
+
c = np.moveaxis(c, 0, iaxis)
|
| 792 |
+
return c
|
| 793 |
+
|
| 794 |
+
|
| 795 |
+
def hermeval(x, c, tensor=True):
|
| 796 |
+
"""
|
| 797 |
+
Evaluate a HermiteE series at points x.
|
| 798 |
+
|
| 799 |
+
If `c` is of length ``n + 1``, this function returns the value:
|
| 800 |
+
|
| 801 |
+
.. math:: p(x) = c_0 * He_0(x) + c_1 * He_1(x) + ... + c_n * He_n(x)
|
| 802 |
+
|
| 803 |
+
The parameter `x` is converted to an array only if it is a tuple or a
|
| 804 |
+
list, otherwise it is treated as a scalar. In either case, either `x`
|
| 805 |
+
or its elements must support multiplication and addition both with
|
| 806 |
+
themselves and with the elements of `c`.
|
| 807 |
+
|
| 808 |
+
If `c` is a 1-D array, then ``p(x)`` will have the same shape as `x`. If
|
| 809 |
+
`c` is multidimensional, then the shape of the result depends on the
|
| 810 |
+
value of `tensor`. If `tensor` is true the shape will be c.shape[1:] +
|
| 811 |
+
x.shape. If `tensor` is false the shape will be c.shape[1:]. Note that
|
| 812 |
+
scalars have shape (,).
|
| 813 |
+
|
| 814 |
+
Trailing zeros in the coefficients will be used in the evaluation, so
|
| 815 |
+
they should be avoided if efficiency is a concern.
|
| 816 |
+
|
| 817 |
+
Parameters
|
| 818 |
+
----------
|
| 819 |
+
x : array_like, compatible object
|
| 820 |
+
If `x` is a list or tuple, it is converted to an ndarray, otherwise
|
| 821 |
+
it is left unchanged and treated as a scalar. In either case, `x`
|
| 822 |
+
or its elements must support addition and multiplication with
|
| 823 |
+
with themselves and with the elements of `c`.
|
| 824 |
+
c : array_like
|
| 825 |
+
Array of coefficients ordered so that the coefficients for terms of
|
| 826 |
+
degree n are contained in c[n]. If `c` is multidimensional the
|
| 827 |
+
remaining indices enumerate multiple polynomials. In the two
|
| 828 |
+
dimensional case the coefficients may be thought of as stored in
|
| 829 |
+
the columns of `c`.
|
| 830 |
+
tensor : boolean, optional
|
| 831 |
+
If True, the shape of the coefficient array is extended with ones
|
| 832 |
+
on the right, one for each dimension of `x`. Scalars have dimension 0
|
| 833 |
+
for this action. The result is that every column of coefficients in
|
| 834 |
+
`c` is evaluated for every element of `x`. If False, `x` is broadcast
|
| 835 |
+
over the columns of `c` for the evaluation. This keyword is useful
|
| 836 |
+
when `c` is multidimensional. The default value is True.
|
| 837 |
+
|
| 838 |
+
Returns
|
| 839 |
+
-------
|
| 840 |
+
values : ndarray, algebra_like
|
| 841 |
+
The shape of the return value is described above.
|
| 842 |
+
|
| 843 |
+
See Also
|
| 844 |
+
--------
|
| 845 |
+
hermeval2d, hermegrid2d, hermeval3d, hermegrid3d
|
| 846 |
+
|
| 847 |
+
Notes
|
| 848 |
+
-----
|
| 849 |
+
The evaluation uses Clenshaw recursion, aka synthetic division.
|
| 850 |
+
|
| 851 |
+
Examples
|
| 852 |
+
--------
|
| 853 |
+
>>> from numpy.polynomial.hermite_e import hermeval
|
| 854 |
+
>>> coef = [1,2,3]
|
| 855 |
+
>>> hermeval(1, coef)
|
| 856 |
+
3.0
|
| 857 |
+
>>> hermeval([[1,2],[3,4]], coef)
|
| 858 |
+
array([[ 3., 14.],
|
| 859 |
+
[31., 54.]])
|
| 860 |
+
|
| 861 |
+
"""
|
| 862 |
+
c = np.array(c, ndmin=1, copy=None)
|
| 863 |
+
if c.dtype.char in '?bBhHiIlLqQpP':
|
| 864 |
+
c = c.astype(np.double)
|
| 865 |
+
if isinstance(x, (tuple, list)):
|
| 866 |
+
x = np.asarray(x)
|
| 867 |
+
if isinstance(x, np.ndarray) and tensor:
|
| 868 |
+
c = c.reshape(c.shape + (1,) * x.ndim)
|
| 869 |
+
|
| 870 |
+
if len(c) == 1:
|
| 871 |
+
c0 = c[0]
|
| 872 |
+
c1 = 0
|
| 873 |
+
elif len(c) == 2:
|
| 874 |
+
c0 = c[0]
|
| 875 |
+
c1 = c[1]
|
| 876 |
+
else:
|
| 877 |
+
nd = len(c)
|
| 878 |
+
c0 = c[-2]
|
| 879 |
+
c1 = c[-1]
|
| 880 |
+
for i in range(3, len(c) + 1):
|
| 881 |
+
tmp = c0
|
| 882 |
+
nd = nd - 1
|
| 883 |
+
c0 = c[-i] - c1 * (nd - 1)
|
| 884 |
+
c1 = tmp + c1 * x
|
| 885 |
+
return c0 + c1 * x
|
| 886 |
+
|
| 887 |
+
|
| 888 |
+
def hermeval2d(x, y, c):
|
| 889 |
+
"""
|
| 890 |
+
Evaluate a 2-D HermiteE series at points (x, y).
|
| 891 |
+
|
| 892 |
+
This function returns the values:
|
| 893 |
+
|
| 894 |
+
.. math:: p(x,y) = \\sum_{i,j} c_{i,j} * He_i(x) * He_j(y)
|
| 895 |
+
|
| 896 |
+
The parameters `x` and `y` are converted to arrays only if they are
|
| 897 |
+
tuples or a lists, otherwise they are treated as a scalars and they
|
| 898 |
+
must have the same shape after conversion. In either case, either `x`
|
| 899 |
+
and `y` or their elements must support multiplication and addition both
|
| 900 |
+
with themselves and with the elements of `c`.
|
| 901 |
+
|
| 902 |
+
If `c` is a 1-D array a one is implicitly appended to its shape to make
|
| 903 |
+
it 2-D. The shape of the result will be c.shape[2:] + x.shape.
|
| 904 |
+
|
| 905 |
+
Parameters
|
| 906 |
+
----------
|
| 907 |
+
x, y : array_like, compatible objects
|
| 908 |
+
The two dimensional series is evaluated at the points ``(x, y)``,
|
| 909 |
+
where `x` and `y` must have the same shape. If `x` or `y` is a list
|
| 910 |
+
or tuple, it is first converted to an ndarray, otherwise it is left
|
| 911 |
+
unchanged and if it isn't an ndarray it is treated as a scalar.
|
| 912 |
+
c : array_like
|
| 913 |
+
Array of coefficients ordered so that the coefficient of the term
|
| 914 |
+
of multi-degree i,j is contained in ``c[i,j]``. If `c` has
|
| 915 |
+
dimension greater than two the remaining indices enumerate multiple
|
| 916 |
+
sets of coefficients.
|
| 917 |
+
|
| 918 |
+
Returns
|
| 919 |
+
-------
|
| 920 |
+
values : ndarray, compatible object
|
| 921 |
+
The values of the two dimensional polynomial at points formed with
|
| 922 |
+
pairs of corresponding values from `x` and `y`.
|
| 923 |
+
|
| 924 |
+
See Also
|
| 925 |
+
--------
|
| 926 |
+
hermeval, hermegrid2d, hermeval3d, hermegrid3d
|
| 927 |
+
"""
|
| 928 |
+
return pu._valnd(hermeval, c, x, y)
|
| 929 |
+
|
| 930 |
+
|
| 931 |
+
def hermegrid2d(x, y, c):
|
| 932 |
+
"""
|
| 933 |
+
Evaluate a 2-D HermiteE series on the Cartesian product of x and y.
|
| 934 |
+
|
| 935 |
+
This function returns the values:
|
| 936 |
+
|
| 937 |
+
.. math:: p(a,b) = \\sum_{i,j} c_{i,j} * H_i(a) * H_j(b)
|
| 938 |
+
|
| 939 |
+
where the points ``(a, b)`` consist of all pairs formed by taking
|
| 940 |
+
`a` from `x` and `b` from `y`. The resulting points form a grid with
|
| 941 |
+
`x` in the first dimension and `y` in the second.
|
| 942 |
+
|
| 943 |
+
The parameters `x` and `y` are converted to arrays only if they are
|
| 944 |
+
tuples or a lists, otherwise they are treated as a scalars. In either
|
| 945 |
+
case, either `x` and `y` or their elements must support multiplication
|
| 946 |
+
and addition both with themselves and with the elements of `c`.
|
| 947 |
+
|
| 948 |
+
If `c` has fewer than two dimensions, ones are implicitly appended to
|
| 949 |
+
its shape to make it 2-D. The shape of the result will be c.shape[2:] +
|
| 950 |
+
x.shape.
|
| 951 |
+
|
| 952 |
+
Parameters
|
| 953 |
+
----------
|
| 954 |
+
x, y : array_like, compatible objects
|
| 955 |
+
The two dimensional series is evaluated at the points in the
|
| 956 |
+
Cartesian product of `x` and `y`. If `x` or `y` is a list or
|
| 957 |
+
tuple, it is first converted to an ndarray, otherwise it is left
|
| 958 |
+
unchanged and, if it isn't an ndarray, it is treated as a scalar.
|
| 959 |
+
c : array_like
|
| 960 |
+
Array of coefficients ordered so that the coefficients for terms of
|
| 961 |
+
degree i,j are contained in ``c[i,j]``. If `c` has dimension
|
| 962 |
+
greater than two the remaining indices enumerate multiple sets of
|
| 963 |
+
coefficients.
|
| 964 |
+
|
| 965 |
+
Returns
|
| 966 |
+
-------
|
| 967 |
+
values : ndarray, compatible object
|
| 968 |
+
The values of the two dimensional polynomial at points in the Cartesian
|
| 969 |
+
product of `x` and `y`.
|
| 970 |
+
|
| 971 |
+
See Also
|
| 972 |
+
--------
|
| 973 |
+
hermeval, hermeval2d, hermeval3d, hermegrid3d
|
| 974 |
+
"""
|
| 975 |
+
return pu._gridnd(hermeval, c, x, y)
|
| 976 |
+
|
| 977 |
+
|
| 978 |
+
def hermeval3d(x, y, z, c):
|
| 979 |
+
"""
|
| 980 |
+
Evaluate a 3-D Hermite_e series at points (x, y, z).
|
| 981 |
+
|
| 982 |
+
This function returns the values:
|
| 983 |
+
|
| 984 |
+
.. math:: p(x,y,z) = \\sum_{i,j,k} c_{i,j,k} * He_i(x) * He_j(y) * He_k(z)
|
| 985 |
+
|
| 986 |
+
The parameters `x`, `y`, and `z` are converted to arrays only if
|
| 987 |
+
they are tuples or a lists, otherwise they are treated as a scalars and
|
| 988 |
+
they must have the same shape after conversion. In either case, either
|
| 989 |
+
`x`, `y`, and `z` or their elements must support multiplication and
|
| 990 |
+
addition both with themselves and with the elements of `c`.
|
| 991 |
+
|
| 992 |
+
If `c` has fewer than 3 dimensions, ones are implicitly appended to its
|
| 993 |
+
shape to make it 3-D. The shape of the result will be c.shape[3:] +
|
| 994 |
+
x.shape.
|
| 995 |
+
|
| 996 |
+
Parameters
|
| 997 |
+
----------
|
| 998 |
+
x, y, z : array_like, compatible object
|
| 999 |
+
The three dimensional series is evaluated at the points
|
| 1000 |
+
`(x, y, z)`, where `x`, `y`, and `z` must have the same shape. If
|
| 1001 |
+
any of `x`, `y`, or `z` is a list or tuple, it is first converted
|
| 1002 |
+
to an ndarray, otherwise it is left unchanged and if it isn't an
|
| 1003 |
+
ndarray it is treated as a scalar.
|
| 1004 |
+
c : array_like
|
| 1005 |
+
Array of coefficients ordered so that the coefficient of the term of
|
| 1006 |
+
multi-degree i,j,k is contained in ``c[i,j,k]``. If `c` has dimension
|
| 1007 |
+
greater than 3 the remaining indices enumerate multiple sets of
|
| 1008 |
+
coefficients.
|
| 1009 |
+
|
| 1010 |
+
Returns
|
| 1011 |
+
-------
|
| 1012 |
+
values : ndarray, compatible object
|
| 1013 |
+
The values of the multidimensional polynomial on points formed with
|
| 1014 |
+
triples of corresponding values from `x`, `y`, and `z`.
|
| 1015 |
+
|
| 1016 |
+
See Also
|
| 1017 |
+
--------
|
| 1018 |
+
hermeval, hermeval2d, hermegrid2d, hermegrid3d
|
| 1019 |
+
"""
|
| 1020 |
+
return pu._valnd(hermeval, c, x, y, z)
|
| 1021 |
+
|
| 1022 |
+
|
| 1023 |
+
def hermegrid3d(x, y, z, c):
|
| 1024 |
+
"""
|
| 1025 |
+
Evaluate a 3-D HermiteE series on the Cartesian product of x, y, and z.
|
| 1026 |
+
|
| 1027 |
+
This function returns the values:
|
| 1028 |
+
|
| 1029 |
+
.. math:: p(a,b,c) = \\sum_{i,j,k} c_{i,j,k} * He_i(a) * He_j(b) * He_k(c)
|
| 1030 |
+
|
| 1031 |
+
where the points ``(a, b, c)`` consist of all triples formed by taking
|
| 1032 |
+
`a` from `x`, `b` from `y`, and `c` from `z`. The resulting points form
|
| 1033 |
+
a grid with `x` in the first dimension, `y` in the second, and `z` in
|
| 1034 |
+
the third.
|
| 1035 |
+
|
| 1036 |
+
The parameters `x`, `y`, and `z` are converted to arrays only if they
|
| 1037 |
+
are tuples or a lists, otherwise they are treated as a scalars. In
|
| 1038 |
+
either case, either `x`, `y`, and `z` or their elements must support
|
| 1039 |
+
multiplication and addition both with themselves and with the elements
|
| 1040 |
+
of `c`.
|
| 1041 |
+
|
| 1042 |
+
If `c` has fewer than three dimensions, ones are implicitly appended to
|
| 1043 |
+
its shape to make it 3-D. The shape of the result will be c.shape[3:] +
|
| 1044 |
+
x.shape + y.shape + z.shape.
|
| 1045 |
+
|
| 1046 |
+
Parameters
|
| 1047 |
+
----------
|
| 1048 |
+
x, y, z : array_like, compatible objects
|
| 1049 |
+
The three dimensional series is evaluated at the points in the
|
| 1050 |
+
Cartesian product of `x`, `y`, and `z`. If `x`, `y`, or `z` is a
|
| 1051 |
+
list or tuple, it is first converted to an ndarray, otherwise it is
|
| 1052 |
+
left unchanged and, if it isn't an ndarray, it is treated as a
|
| 1053 |
+
scalar.
|
| 1054 |
+
c : array_like
|
| 1055 |
+
Array of coefficients ordered so that the coefficients for terms of
|
| 1056 |
+
degree i,j are contained in ``c[i,j]``. If `c` has dimension
|
| 1057 |
+
greater than two the remaining indices enumerate multiple sets of
|
| 1058 |
+
coefficients.
|
| 1059 |
+
|
| 1060 |
+
Returns
|
| 1061 |
+
-------
|
| 1062 |
+
values : ndarray, compatible object
|
| 1063 |
+
The values of the two dimensional polynomial at points in the Cartesian
|
| 1064 |
+
product of `x` and `y`.
|
| 1065 |
+
|
| 1066 |
+
See Also
|
| 1067 |
+
--------
|
| 1068 |
+
hermeval, hermeval2d, hermegrid2d, hermeval3d
|
| 1069 |
+
"""
|
| 1070 |
+
return pu._gridnd(hermeval, c, x, y, z)
|
| 1071 |
+
|
| 1072 |
+
|
| 1073 |
+
def hermevander(x, deg):
|
| 1074 |
+
"""Pseudo-Vandermonde matrix of given degree.
|
| 1075 |
+
|
| 1076 |
+
Returns the pseudo-Vandermonde matrix of degree `deg` and sample points
|
| 1077 |
+
`x`. The pseudo-Vandermonde matrix is defined by
|
| 1078 |
+
|
| 1079 |
+
.. math:: V[..., i] = He_i(x),
|
| 1080 |
+
|
| 1081 |
+
where ``0 <= i <= deg``. The leading indices of `V` index the elements of
|
| 1082 |
+
`x` and the last index is the degree of the HermiteE polynomial.
|
| 1083 |
+
|
| 1084 |
+
If `c` is a 1-D array of coefficients of length ``n + 1`` and `V` is the
|
| 1085 |
+
array ``V = hermevander(x, n)``, then ``np.dot(V, c)`` and
|
| 1086 |
+
``hermeval(x, c)`` are the same up to roundoff. This equivalence is
|
| 1087 |
+
useful both for least squares fitting and for the evaluation of a large
|
| 1088 |
+
number of HermiteE series of the same degree and sample points.
|
| 1089 |
+
|
| 1090 |
+
Parameters
|
| 1091 |
+
----------
|
| 1092 |
+
x : array_like
|
| 1093 |
+
Array of points. The dtype is converted to float64 or complex128
|
| 1094 |
+
depending on whether any of the elements are complex. If `x` is
|
| 1095 |
+
scalar it is converted to a 1-D array.
|
| 1096 |
+
deg : int
|
| 1097 |
+
Degree of the resulting matrix.
|
| 1098 |
+
|
| 1099 |
+
Returns
|
| 1100 |
+
-------
|
| 1101 |
+
vander : ndarray
|
| 1102 |
+
The pseudo-Vandermonde matrix. The shape of the returned matrix is
|
| 1103 |
+
``x.shape + (deg + 1,)``, where The last index is the degree of the
|
| 1104 |
+
corresponding HermiteE polynomial. The dtype will be the same as
|
| 1105 |
+
the converted `x`.
|
| 1106 |
+
|
| 1107 |
+
Examples
|
| 1108 |
+
--------
|
| 1109 |
+
>>> import numpy as np
|
| 1110 |
+
>>> from numpy.polynomial.hermite_e import hermevander
|
| 1111 |
+
>>> x = np.array([-1, 0, 1])
|
| 1112 |
+
>>> hermevander(x, 3)
|
| 1113 |
+
array([[ 1., -1., 0., 2.],
|
| 1114 |
+
[ 1., 0., -1., -0.],
|
| 1115 |
+
[ 1., 1., 0., -2.]])
|
| 1116 |
+
|
| 1117 |
+
"""
|
| 1118 |
+
ideg = pu._as_int(deg, "deg")
|
| 1119 |
+
if ideg < 0:
|
| 1120 |
+
raise ValueError("deg must be non-negative")
|
| 1121 |
+
|
| 1122 |
+
x = np.array(x, copy=None, ndmin=1) + 0.0
|
| 1123 |
+
dims = (ideg + 1,) + x.shape
|
| 1124 |
+
dtyp = x.dtype
|
| 1125 |
+
v = np.empty(dims, dtype=dtyp)
|
| 1126 |
+
v[0] = x * 0 + 1
|
| 1127 |
+
if ideg > 0:
|
| 1128 |
+
v[1] = x
|
| 1129 |
+
for i in range(2, ideg + 1):
|
| 1130 |
+
v[i] = (v[i - 1] * x - v[i - 2] * (i - 1))
|
| 1131 |
+
return np.moveaxis(v, 0, -1)
|
| 1132 |
+
|
| 1133 |
+
|
| 1134 |
+
def hermevander2d(x, y, deg):
|
| 1135 |
+
"""Pseudo-Vandermonde matrix of given degrees.
|
| 1136 |
+
|
| 1137 |
+
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample
|
| 1138 |
+
points ``(x, y)``. The pseudo-Vandermonde matrix is defined by
|
| 1139 |
+
|
| 1140 |
+
.. math:: V[..., (deg[1] + 1)*i + j] = He_i(x) * He_j(y),
|
| 1141 |
+
|
| 1142 |
+
where ``0 <= i <= deg[0]`` and ``0 <= j <= deg[1]``. The leading indices of
|
| 1143 |
+
`V` index the points ``(x, y)`` and the last index encodes the degrees of
|
| 1144 |
+
the HermiteE polynomials.
|
| 1145 |
+
|
| 1146 |
+
If ``V = hermevander2d(x, y, [xdeg, ydeg])``, then the columns of `V`
|
| 1147 |
+
correspond to the elements of a 2-D coefficient array `c` of shape
|
| 1148 |
+
(xdeg + 1, ydeg + 1) in the order
|
| 1149 |
+
|
| 1150 |
+
.. math:: c_{00}, c_{01}, c_{02} ... , c_{10}, c_{11}, c_{12} ...
|
| 1151 |
+
|
| 1152 |
+
and ``np.dot(V, c.flat)`` and ``hermeval2d(x, y, c)`` will be the same
|
| 1153 |
+
up to roundoff. This equivalence is useful both for least squares
|
| 1154 |
+
fitting and for the evaluation of a large number of 2-D HermiteE
|
| 1155 |
+
series of the same degrees and sample points.
|
| 1156 |
+
|
| 1157 |
+
Parameters
|
| 1158 |
+
----------
|
| 1159 |
+
x, y : array_like
|
| 1160 |
+
Arrays of point coordinates, all of the same shape. The dtypes
|
| 1161 |
+
will be converted to either float64 or complex128 depending on
|
| 1162 |
+
whether any of the elements are complex. Scalars are converted to
|
| 1163 |
+
1-D arrays.
|
| 1164 |
+
deg : list of ints
|
| 1165 |
+
List of maximum degrees of the form [x_deg, y_deg].
|
| 1166 |
+
|
| 1167 |
+
Returns
|
| 1168 |
+
-------
|
| 1169 |
+
vander2d : ndarray
|
| 1170 |
+
The shape of the returned matrix is ``x.shape + (order,)``, where
|
| 1171 |
+
:math:`order = (deg[0]+1)*(deg[1]+1)`. The dtype will be the same
|
| 1172 |
+
as the converted `x` and `y`.
|
| 1173 |
+
|
| 1174 |
+
See Also
|
| 1175 |
+
--------
|
| 1176 |
+
hermevander, hermevander3d, hermeval2d, hermeval3d
|
| 1177 |
+
"""
|
| 1178 |
+
return pu._vander_nd_flat((hermevander, hermevander), (x, y), deg)
|
| 1179 |
+
|
| 1180 |
+
|
| 1181 |
+
def hermevander3d(x, y, z, deg):
|
| 1182 |
+
"""Pseudo-Vandermonde matrix of given degrees.
|
| 1183 |
+
|
| 1184 |
+
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample
|
| 1185 |
+
points ``(x, y, z)``. If `l`, `m`, `n` are the given degrees in `x`, `y`, `z`,
|
| 1186 |
+
then Hehe pseudo-Vandermonde matrix is defined by
|
| 1187 |
+
|
| 1188 |
+
.. math:: V[..., (m+1)(n+1)i + (n+1)j + k] = He_i(x)*He_j(y)*He_k(z),
|
| 1189 |
+
|
| 1190 |
+
where ``0 <= i <= l``, ``0 <= j <= m``, and ``0 <= j <= n``. The leading
|
| 1191 |
+
indices of `V` index the points ``(x, y, z)`` and the last index encodes
|
| 1192 |
+
the degrees of the HermiteE polynomials.
|
| 1193 |
+
|
| 1194 |
+
If ``V = hermevander3d(x, y, z, [xdeg, ydeg, zdeg])``, then the columns
|
| 1195 |
+
of `V` correspond to the elements of a 3-D coefficient array `c` of
|
| 1196 |
+
shape (xdeg + 1, ydeg + 1, zdeg + 1) in the order
|
| 1197 |
+
|
| 1198 |
+
.. math:: c_{000}, c_{001}, c_{002},... , c_{010}, c_{011}, c_{012},...
|
| 1199 |
+
|
| 1200 |
+
and ``np.dot(V, c.flat)`` and ``hermeval3d(x, y, z, c)`` will be the
|
| 1201 |
+
same up to roundoff. This equivalence is useful both for least squares
|
| 1202 |
+
fitting and for the evaluation of a large number of 3-D HermiteE
|
| 1203 |
+
series of the same degrees and sample points.
|
| 1204 |
+
|
| 1205 |
+
Parameters
|
| 1206 |
+
----------
|
| 1207 |
+
x, y, z : array_like
|
| 1208 |
+
Arrays of point coordinates, all of the same shape. The dtypes will
|
| 1209 |
+
be converted to either float64 or complex128 depending on whether
|
| 1210 |
+
any of the elements are complex. Scalars are converted to 1-D
|
| 1211 |
+
arrays.
|
| 1212 |
+
deg : list of ints
|
| 1213 |
+
List of maximum degrees of the form [x_deg, y_deg, z_deg].
|
| 1214 |
+
|
| 1215 |
+
Returns
|
| 1216 |
+
-------
|
| 1217 |
+
vander3d : ndarray
|
| 1218 |
+
The shape of the returned matrix is ``x.shape + (order,)``, where
|
| 1219 |
+
:math:`order = (deg[0]+1)*(deg[1]+1)*(deg[2]+1)`. The dtype will
|
| 1220 |
+
be the same as the converted `x`, `y`, and `z`.
|
| 1221 |
+
|
| 1222 |
+
See Also
|
| 1223 |
+
--------
|
| 1224 |
+
hermevander, hermevander3d, hermeval2d, hermeval3d
|
| 1225 |
+
"""
|
| 1226 |
+
return pu._vander_nd_flat((hermevander, hermevander, hermevander), (x, y, z), deg)
|
| 1227 |
+
|
| 1228 |
+
|
| 1229 |
+
def hermefit(x, y, deg, rcond=None, full=False, w=None):
|
| 1230 |
+
"""
|
| 1231 |
+
Least squares fit of Hermite series to data.
|
| 1232 |
+
|
| 1233 |
+
Return the coefficients of a HermiteE series of degree `deg` that is
|
| 1234 |
+
the least squares fit to the data values `y` given at points `x`. If
|
| 1235 |
+
`y` is 1-D the returned coefficients will also be 1-D. If `y` is 2-D
|
| 1236 |
+
multiple fits are done, one for each column of `y`, and the resulting
|
| 1237 |
+
coefficients are stored in the corresponding columns of a 2-D return.
|
| 1238 |
+
The fitted polynomial(s) are in the form
|
| 1239 |
+
|
| 1240 |
+
.. math:: p(x) = c_0 + c_1 * He_1(x) + ... + c_n * He_n(x),
|
| 1241 |
+
|
| 1242 |
+
where `n` is `deg`.
|
| 1243 |
+
|
| 1244 |
+
Parameters
|
| 1245 |
+
----------
|
| 1246 |
+
x : array_like, shape (M,)
|
| 1247 |
+
x-coordinates of the M sample points ``(x[i], y[i])``.
|
| 1248 |
+
y : array_like, shape (M,) or (M, K)
|
| 1249 |
+
y-coordinates of the sample points. Several data sets of sample
|
| 1250 |
+
points sharing the same x-coordinates can be fitted at once by
|
| 1251 |
+
passing in a 2D-array that contains one dataset per column.
|
| 1252 |
+
deg : int or 1-D array_like
|
| 1253 |
+
Degree(s) of the fitting polynomials. If `deg` is a single integer
|
| 1254 |
+
all terms up to and including the `deg`'th term are included in the
|
| 1255 |
+
fit. For NumPy versions >= 1.11.0 a list of integers specifying the
|
| 1256 |
+
degrees of the terms to include may be used instead.
|
| 1257 |
+
rcond : float, optional
|
| 1258 |
+
Relative condition number of the fit. Singular values smaller than
|
| 1259 |
+
this relative to the largest singular value will be ignored. The
|
| 1260 |
+
default value is len(x)*eps, where eps is the relative precision of
|
| 1261 |
+
the float type, about 2e-16 in most cases.
|
| 1262 |
+
full : bool, optional
|
| 1263 |
+
Switch determining nature of return value. When it is False (the
|
| 1264 |
+
default) just the coefficients are returned, when True diagnostic
|
| 1265 |
+
information from the singular value decomposition is also returned.
|
| 1266 |
+
w : array_like, shape (`M`,), optional
|
| 1267 |
+
Weights. If not None, the weight ``w[i]`` applies to the unsquared
|
| 1268 |
+
residual ``y[i] - y_hat[i]`` at ``x[i]``. Ideally the weights are
|
| 1269 |
+
chosen so that the errors of the products ``w[i]*y[i]`` all have the
|
| 1270 |
+
same variance. When using inverse-variance weighting, use
|
| 1271 |
+
``w[i] = 1/sigma(y[i])``. The default value is None.
|
| 1272 |
+
|
| 1273 |
+
Returns
|
| 1274 |
+
-------
|
| 1275 |
+
coef : ndarray, shape (M,) or (M, K)
|
| 1276 |
+
Hermite coefficients ordered from low to high. If `y` was 2-D,
|
| 1277 |
+
the coefficients for the data in column k of `y` are in column
|
| 1278 |
+
`k`.
|
| 1279 |
+
|
| 1280 |
+
[residuals, rank, singular_values, rcond] : list
|
| 1281 |
+
These values are only returned if ``full == True``
|
| 1282 |
+
|
| 1283 |
+
- residuals -- sum of squared residuals of the least squares fit
|
| 1284 |
+
- rank -- the numerical rank of the scaled Vandermonde matrix
|
| 1285 |
+
- singular_values -- singular values of the scaled Vandermonde matrix
|
| 1286 |
+
- rcond -- value of `rcond`.
|
| 1287 |
+
|
| 1288 |
+
For more details, see `numpy.linalg.lstsq`.
|
| 1289 |
+
|
| 1290 |
+
Warns
|
| 1291 |
+
-----
|
| 1292 |
+
RankWarning
|
| 1293 |
+
The rank of the coefficient matrix in the least-squares fit is
|
| 1294 |
+
deficient. The warning is only raised if ``full = False``. The
|
| 1295 |
+
warnings can be turned off by
|
| 1296 |
+
|
| 1297 |
+
>>> import warnings
|
| 1298 |
+
>>> warnings.simplefilter('ignore', np.exceptions.RankWarning)
|
| 1299 |
+
|
| 1300 |
+
See Also
|
| 1301 |
+
--------
|
| 1302 |
+
numpy.polynomial.chebyshev.chebfit
|
| 1303 |
+
numpy.polynomial.legendre.legfit
|
| 1304 |
+
numpy.polynomial.polynomial.polyfit
|
| 1305 |
+
numpy.polynomial.hermite.hermfit
|
| 1306 |
+
numpy.polynomial.laguerre.lagfit
|
| 1307 |
+
hermeval : Evaluates a Hermite series.
|
| 1308 |
+
hermevander : pseudo Vandermonde matrix of Hermite series.
|
| 1309 |
+
hermeweight : HermiteE weight function.
|
| 1310 |
+
numpy.linalg.lstsq : Computes a least-squares fit from the matrix.
|
| 1311 |
+
scipy.interpolate.UnivariateSpline : Computes spline fits.
|
| 1312 |
+
|
| 1313 |
+
Notes
|
| 1314 |
+
-----
|
| 1315 |
+
The solution is the coefficients of the HermiteE series `p` that
|
| 1316 |
+
minimizes the sum of the weighted squared errors
|
| 1317 |
+
|
| 1318 |
+
.. math:: E = \\sum_j w_j^2 * |y_j - p(x_j)|^2,
|
| 1319 |
+
|
| 1320 |
+
where the :math:`w_j` are the weights. This problem is solved by
|
| 1321 |
+
setting up the (typically) overdetermined matrix equation
|
| 1322 |
+
|
| 1323 |
+
.. math:: V(x) * c = w * y,
|
| 1324 |
+
|
| 1325 |
+
where `V` is the pseudo Vandermonde matrix of `x`, the elements of `c`
|
| 1326 |
+
are the coefficients to be solved for, and the elements of `y` are the
|
| 1327 |
+
observed values. This equation is then solved using the singular value
|
| 1328 |
+
decomposition of `V`.
|
| 1329 |
+
|
| 1330 |
+
If some of the singular values of `V` are so small that they are
|
| 1331 |
+
neglected, then a `~exceptions.RankWarning` will be issued. This means that
|
| 1332 |
+
the coefficient values may be poorly determined. Using a lower order fit
|
| 1333 |
+
will usually get rid of the warning. The `rcond` parameter can also be
|
| 1334 |
+
set to a value smaller than its default, but the resulting fit may be
|
| 1335 |
+
spurious and have large contributions from roundoff error.
|
| 1336 |
+
|
| 1337 |
+
Fits using HermiteE series are probably most useful when the data can
|
| 1338 |
+
be approximated by ``sqrt(w(x)) * p(x)``, where ``w(x)`` is the HermiteE
|
| 1339 |
+
weight. In that case the weight ``sqrt(w(x[i]))`` should be used
|
| 1340 |
+
together with data values ``y[i]/sqrt(w(x[i]))``. The weight function is
|
| 1341 |
+
available as `hermeweight`.
|
| 1342 |
+
|
| 1343 |
+
References
|
| 1344 |
+
----------
|
| 1345 |
+
.. [1] Wikipedia, "Curve fitting",
|
| 1346 |
+
https://en.wikipedia.org/wiki/Curve_fitting
|
| 1347 |
+
|
| 1348 |
+
Examples
|
| 1349 |
+
--------
|
| 1350 |
+
>>> import numpy as np
|
| 1351 |
+
>>> from numpy.polynomial.hermite_e import hermefit, hermeval
|
| 1352 |
+
>>> x = np.linspace(-10, 10)
|
| 1353 |
+
>>> rng = np.random.default_rng()
|
| 1354 |
+
>>> err = rng.normal(scale=1./10, size=len(x))
|
| 1355 |
+
>>> y = hermeval(x, [1, 2, 3]) + err
|
| 1356 |
+
>>> hermefit(x, y, 2)
|
| 1357 |
+
array([1.02284196, 2.00032805, 2.99978457]) # may vary
|
| 1358 |
+
|
| 1359 |
+
"""
|
| 1360 |
+
return pu._fit(hermevander, x, y, deg, rcond, full, w)
|
| 1361 |
+
|
| 1362 |
+
|
| 1363 |
+
def hermecompanion(c):
|
| 1364 |
+
"""
|
| 1365 |
+
Return the scaled companion matrix of c.
|
| 1366 |
+
|
| 1367 |
+
The basis polynomials are scaled so that the companion matrix is
|
| 1368 |
+
symmetric when `c` is a HermiteE basis polynomial. This provides
|
| 1369 |
+
better eigenvalue estimates than the unscaled case and for basis
|
| 1370 |
+
polynomials the eigenvalues are guaranteed to be real if
|
| 1371 |
+
`numpy.linalg.eigvalsh` is used to obtain them.
|
| 1372 |
+
|
| 1373 |
+
Parameters
|
| 1374 |
+
----------
|
| 1375 |
+
c : array_like
|
| 1376 |
+
1-D array of HermiteE series coefficients ordered from low to high
|
| 1377 |
+
degree.
|
| 1378 |
+
|
| 1379 |
+
Returns
|
| 1380 |
+
-------
|
| 1381 |
+
mat : ndarray
|
| 1382 |
+
Scaled companion matrix of dimensions (deg, deg).
|
| 1383 |
+
"""
|
| 1384 |
+
# c is a trimmed copy
|
| 1385 |
+
[c] = pu.as_series([c])
|
| 1386 |
+
if len(c) < 2:
|
| 1387 |
+
raise ValueError('Series must have maximum degree of at least 1.')
|
| 1388 |
+
if len(c) == 2:
|
| 1389 |
+
return np.array([[-c[0] / c[1]]])
|
| 1390 |
+
|
| 1391 |
+
n = len(c) - 1
|
| 1392 |
+
mat = np.zeros((n, n), dtype=c.dtype)
|
| 1393 |
+
scl = np.hstack((1., 1. / np.sqrt(np.arange(n - 1, 0, -1))))
|
| 1394 |
+
scl = np.multiply.accumulate(scl)[::-1]
|
| 1395 |
+
top = mat.reshape(-1)[1::n + 1]
|
| 1396 |
+
bot = mat.reshape(-1)[n::n + 1]
|
| 1397 |
+
top[...] = np.sqrt(np.arange(1, n))
|
| 1398 |
+
bot[...] = top
|
| 1399 |
+
mat[:, -1] -= scl * c[:-1] / c[-1]
|
| 1400 |
+
return mat
|
| 1401 |
+
|
| 1402 |
+
|
| 1403 |
+
def hermeroots(c):
|
| 1404 |
+
"""
|
| 1405 |
+
Compute the roots of a HermiteE series.
|
| 1406 |
+
|
| 1407 |
+
Return the roots (a.k.a. "zeros") of the polynomial
|
| 1408 |
+
|
| 1409 |
+
.. math:: p(x) = \\sum_i c[i] * He_i(x).
|
| 1410 |
+
|
| 1411 |
+
Parameters
|
| 1412 |
+
----------
|
| 1413 |
+
c : 1-D array_like
|
| 1414 |
+
1-D array of coefficients.
|
| 1415 |
+
|
| 1416 |
+
Returns
|
| 1417 |
+
-------
|
| 1418 |
+
out : ndarray
|
| 1419 |
+
Array of the roots of the series. If all the roots are real,
|
| 1420 |
+
then `out` is also real, otherwise it is complex.
|
| 1421 |
+
|
| 1422 |
+
See Also
|
| 1423 |
+
--------
|
| 1424 |
+
numpy.polynomial.polynomial.polyroots
|
| 1425 |
+
numpy.polynomial.legendre.legroots
|
| 1426 |
+
numpy.polynomial.laguerre.lagroots
|
| 1427 |
+
numpy.polynomial.hermite.hermroots
|
| 1428 |
+
numpy.polynomial.chebyshev.chebroots
|
| 1429 |
+
|
| 1430 |
+
Notes
|
| 1431 |
+
-----
|
| 1432 |
+
The root estimates are obtained as the eigenvalues of the companion
|
| 1433 |
+
matrix, Roots far from the origin of the complex plane may have large
|
| 1434 |
+
errors due to the numerical instability of the series for such
|
| 1435 |
+
values. Roots with multiplicity greater than 1 will also show larger
|
| 1436 |
+
errors as the value of the series near such points is relatively
|
| 1437 |
+
insensitive to errors in the roots. Isolated roots near the origin can
|
| 1438 |
+
be improved by a few iterations of Newton's method.
|
| 1439 |
+
|
| 1440 |
+
The HermiteE series basis polynomials aren't powers of `x` so the
|
| 1441 |
+
results of this function may seem unintuitive.
|
| 1442 |
+
|
| 1443 |
+
Examples
|
| 1444 |
+
--------
|
| 1445 |
+
>>> from numpy.polynomial.hermite_e import hermeroots, hermefromroots
|
| 1446 |
+
>>> coef = hermefromroots([-1, 0, 1])
|
| 1447 |
+
>>> coef
|
| 1448 |
+
array([0., 2., 0., 1.])
|
| 1449 |
+
>>> hermeroots(coef)
|
| 1450 |
+
array([-1., 0., 1.]) # may vary
|
| 1451 |
+
|
| 1452 |
+
"""
|
| 1453 |
+
# c is a trimmed copy
|
| 1454 |
+
[c] = pu.as_series([c])
|
| 1455 |
+
if len(c) <= 1:
|
| 1456 |
+
return np.array([], dtype=c.dtype)
|
| 1457 |
+
if len(c) == 2:
|
| 1458 |
+
return np.array([-c[0] / c[1]])
|
| 1459 |
+
|
| 1460 |
+
# rotated companion matrix reduces error
|
| 1461 |
+
m = hermecompanion(c)[::-1, ::-1]
|
| 1462 |
+
r = np.linalg.eigvals(m)
|
| 1463 |
+
r.sort()
|
| 1464 |
+
return r
|
| 1465 |
+
|
| 1466 |
+
|
| 1467 |
+
def _normed_hermite_e_n(x, n):
|
| 1468 |
+
"""
|
| 1469 |
+
Evaluate a normalized HermiteE polynomial.
|
| 1470 |
+
|
| 1471 |
+
Compute the value of the normalized HermiteE polynomial of degree ``n``
|
| 1472 |
+
at the points ``x``.
|
| 1473 |
+
|
| 1474 |
+
|
| 1475 |
+
Parameters
|
| 1476 |
+
----------
|
| 1477 |
+
x : ndarray of double.
|
| 1478 |
+
Points at which to evaluate the function
|
| 1479 |
+
n : int
|
| 1480 |
+
Degree of the normalized HermiteE function to be evaluated.
|
| 1481 |
+
|
| 1482 |
+
Returns
|
| 1483 |
+
-------
|
| 1484 |
+
values : ndarray
|
| 1485 |
+
The shape of the return value is described above.
|
| 1486 |
+
|
| 1487 |
+
Notes
|
| 1488 |
+
-----
|
| 1489 |
+
This function is needed for finding the Gauss points and integration
|
| 1490 |
+
weights for high degrees. The values of the standard HermiteE functions
|
| 1491 |
+
overflow when n >= 207.
|
| 1492 |
+
|
| 1493 |
+
"""
|
| 1494 |
+
if n == 0:
|
| 1495 |
+
return np.full(x.shape, 1 / np.sqrt(np.sqrt(2 * np.pi)))
|
| 1496 |
+
|
| 1497 |
+
c0 = 0.
|
| 1498 |
+
c1 = 1. / np.sqrt(np.sqrt(2 * np.pi))
|
| 1499 |
+
nd = float(n)
|
| 1500 |
+
for i in range(n - 1):
|
| 1501 |
+
tmp = c0
|
| 1502 |
+
c0 = -c1 * np.sqrt((nd - 1.) / nd)
|
| 1503 |
+
c1 = tmp + c1 * x * np.sqrt(1. / nd)
|
| 1504 |
+
nd = nd - 1.0
|
| 1505 |
+
return c0 + c1 * x
|
| 1506 |
+
|
| 1507 |
+
|
| 1508 |
+
def hermegauss(deg):
|
| 1509 |
+
"""
|
| 1510 |
+
Gauss-HermiteE quadrature.
|
| 1511 |
+
|
| 1512 |
+
Computes the sample points and weights for Gauss-HermiteE quadrature.
|
| 1513 |
+
These sample points and weights will correctly integrate polynomials of
|
| 1514 |
+
degree :math:`2*deg - 1` or less over the interval :math:`[-\\inf, \\inf]`
|
| 1515 |
+
with the weight function :math:`f(x) = \\exp(-x^2/2)`.
|
| 1516 |
+
|
| 1517 |
+
Parameters
|
| 1518 |
+
----------
|
| 1519 |
+
deg : int
|
| 1520 |
+
Number of sample points and weights. It must be >= 1.
|
| 1521 |
+
|
| 1522 |
+
Returns
|
| 1523 |
+
-------
|
| 1524 |
+
x : ndarray
|
| 1525 |
+
1-D ndarray containing the sample points.
|
| 1526 |
+
y : ndarray
|
| 1527 |
+
1-D ndarray containing the weights.
|
| 1528 |
+
|
| 1529 |
+
Notes
|
| 1530 |
+
-----
|
| 1531 |
+
The results have only been tested up to degree 100, higher degrees may
|
| 1532 |
+
be problematic. The weights are determined by using the fact that
|
| 1533 |
+
|
| 1534 |
+
.. math:: w_k = c / (He'_n(x_k) * He_{n-1}(x_k))
|
| 1535 |
+
|
| 1536 |
+
where :math:`c` is a constant independent of :math:`k` and :math:`x_k`
|
| 1537 |
+
is the k'th root of :math:`He_n`, and then scaling the results to get
|
| 1538 |
+
the right value when integrating 1.
|
| 1539 |
+
|
| 1540 |
+
"""
|
| 1541 |
+
ideg = pu._as_int(deg, "deg")
|
| 1542 |
+
if ideg <= 0:
|
| 1543 |
+
raise ValueError("deg must be a positive integer")
|
| 1544 |
+
|
| 1545 |
+
# first approximation of roots. We use the fact that the companion
|
| 1546 |
+
# matrix is symmetric in this case in order to obtain better zeros.
|
| 1547 |
+
c = np.array([0] * deg + [1])
|
| 1548 |
+
m = hermecompanion(c)
|
| 1549 |
+
x = np.linalg.eigvalsh(m)
|
| 1550 |
+
|
| 1551 |
+
# improve roots by one application of Newton
|
| 1552 |
+
dy = _normed_hermite_e_n(x, ideg)
|
| 1553 |
+
df = _normed_hermite_e_n(x, ideg - 1) * np.sqrt(ideg)
|
| 1554 |
+
x -= dy / df
|
| 1555 |
+
|
| 1556 |
+
# compute the weights. We scale the factor to avoid possible numerical
|
| 1557 |
+
# overflow.
|
| 1558 |
+
fm = _normed_hermite_e_n(x, ideg - 1)
|
| 1559 |
+
fm /= np.abs(fm).max()
|
| 1560 |
+
w = 1 / (fm * fm)
|
| 1561 |
+
|
| 1562 |
+
# for Hermite_e we can also symmetrize
|
| 1563 |
+
w = (w + w[::-1]) / 2
|
| 1564 |
+
x = (x - x[::-1]) / 2
|
| 1565 |
+
|
| 1566 |
+
# scale w to get the right value
|
| 1567 |
+
w *= np.sqrt(2 * np.pi) / w.sum()
|
| 1568 |
+
|
| 1569 |
+
return x, w
|
| 1570 |
+
|
| 1571 |
+
|
| 1572 |
+
def hermeweight(x):
|
| 1573 |
+
"""Weight function of the Hermite_e polynomials.
|
| 1574 |
+
|
| 1575 |
+
The weight function is :math:`\\exp(-x^2/2)` and the interval of
|
| 1576 |
+
integration is :math:`[-\\inf, \\inf]`. the HermiteE polynomials are
|
| 1577 |
+
orthogonal, but not normalized, with respect to this weight function.
|
| 1578 |
+
|
| 1579 |
+
Parameters
|
| 1580 |
+
----------
|
| 1581 |
+
x : array_like
|
| 1582 |
+
Values at which the weight function will be computed.
|
| 1583 |
+
|
| 1584 |
+
Returns
|
| 1585 |
+
-------
|
| 1586 |
+
w : ndarray
|
| 1587 |
+
The weight function at `x`.
|
| 1588 |
+
"""
|
| 1589 |
+
w = np.exp(-.5 * x**2)
|
| 1590 |
+
return w
|
| 1591 |
+
|
| 1592 |
+
|
| 1593 |
+
#
|
| 1594 |
+
# HermiteE series class
|
| 1595 |
+
#
|
| 1596 |
+
|
| 1597 |
+
class HermiteE(ABCPolyBase):
|
| 1598 |
+
"""A HermiteE series class.
|
| 1599 |
+
|
| 1600 |
+
The HermiteE class provides the standard Python numerical methods
|
| 1601 |
+
'+', '-', '*', '//', '%', 'divmod', '**', and '()' as well as the
|
| 1602 |
+
attributes and methods listed below.
|
| 1603 |
+
|
| 1604 |
+
Parameters
|
| 1605 |
+
----------
|
| 1606 |
+
coef : array_like
|
| 1607 |
+
HermiteE coefficients in order of increasing degree, i.e,
|
| 1608 |
+
``(1, 2, 3)`` gives ``1*He_0(x) + 2*He_1(X) + 3*He_2(x)``.
|
| 1609 |
+
domain : (2,) array_like, optional
|
| 1610 |
+
Domain to use. The interval ``[domain[0], domain[1]]`` is mapped
|
| 1611 |
+
to the interval ``[window[0], window[1]]`` by shifting and scaling.
|
| 1612 |
+
The default value is [-1., 1.].
|
| 1613 |
+
window : (2,) array_like, optional
|
| 1614 |
+
Window, see `domain` for its use. The default value is [-1., 1.].
|
| 1615 |
+
symbol : str, optional
|
| 1616 |
+
Symbol used to represent the independent variable in string
|
| 1617 |
+
representations of the polynomial expression, e.g. for printing.
|
| 1618 |
+
The symbol must be a valid Python identifier. Default value is 'x'.
|
| 1619 |
+
|
| 1620 |
+
.. versionadded:: 1.24
|
| 1621 |
+
|
| 1622 |
+
"""
|
| 1623 |
+
# Virtual Functions
|
| 1624 |
+
_add = staticmethod(hermeadd)
|
| 1625 |
+
_sub = staticmethod(hermesub)
|
| 1626 |
+
_mul = staticmethod(hermemul)
|
| 1627 |
+
_div = staticmethod(hermediv)
|
| 1628 |
+
_pow = staticmethod(hermepow)
|
| 1629 |
+
_val = staticmethod(hermeval)
|
| 1630 |
+
_int = staticmethod(hermeint)
|
| 1631 |
+
_der = staticmethod(hermeder)
|
| 1632 |
+
_fit = staticmethod(hermefit)
|
| 1633 |
+
_line = staticmethod(hermeline)
|
| 1634 |
+
_roots = staticmethod(hermeroots)
|
| 1635 |
+
_fromroots = staticmethod(hermefromroots)
|
| 1636 |
+
|
| 1637 |
+
# Virtual properties
|
| 1638 |
+
domain = np.array(hermedomain)
|
| 1639 |
+
window = np.array(hermedomain)
|
| 1640 |
+
basis_name = 'He'
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/hermite_e.pyi
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, ClassVar, Final, Literal as L, TypeVar
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
from numpy._typing import _Shape
|
| 5 |
+
|
| 6 |
+
from ._polybase import ABCPolyBase
|
| 7 |
+
from ._polytypes import (
|
| 8 |
+
_Array1,
|
| 9 |
+
_Array2,
|
| 10 |
+
_FuncBinOp,
|
| 11 |
+
_FuncCompanion,
|
| 12 |
+
_FuncDer,
|
| 13 |
+
_FuncFit,
|
| 14 |
+
_FuncFromRoots,
|
| 15 |
+
_FuncGauss,
|
| 16 |
+
_FuncInteg,
|
| 17 |
+
_FuncLine,
|
| 18 |
+
_FuncPoly2Ortho,
|
| 19 |
+
_FuncPow,
|
| 20 |
+
_FuncRoots,
|
| 21 |
+
_FuncUnOp,
|
| 22 |
+
_FuncVal,
|
| 23 |
+
_FuncVal2D,
|
| 24 |
+
_FuncVal3D,
|
| 25 |
+
_FuncVander,
|
| 26 |
+
_FuncVander2D,
|
| 27 |
+
_FuncVander3D,
|
| 28 |
+
_FuncWeight,
|
| 29 |
+
)
|
| 30 |
+
from .polyutils import trimcoef as hermetrim
|
| 31 |
+
|
| 32 |
+
__all__ = [
|
| 33 |
+
"hermezero",
|
| 34 |
+
"hermeone",
|
| 35 |
+
"hermex",
|
| 36 |
+
"hermedomain",
|
| 37 |
+
"hermeline",
|
| 38 |
+
"hermeadd",
|
| 39 |
+
"hermesub",
|
| 40 |
+
"hermemulx",
|
| 41 |
+
"hermemul",
|
| 42 |
+
"hermediv",
|
| 43 |
+
"hermepow",
|
| 44 |
+
"hermeval",
|
| 45 |
+
"hermeder",
|
| 46 |
+
"hermeint",
|
| 47 |
+
"herme2poly",
|
| 48 |
+
"poly2herme",
|
| 49 |
+
"hermefromroots",
|
| 50 |
+
"hermevander",
|
| 51 |
+
"hermefit",
|
| 52 |
+
"hermetrim",
|
| 53 |
+
"hermeroots",
|
| 54 |
+
"HermiteE",
|
| 55 |
+
"hermeval2d",
|
| 56 |
+
"hermeval3d",
|
| 57 |
+
"hermegrid2d",
|
| 58 |
+
"hermegrid3d",
|
| 59 |
+
"hermevander2d",
|
| 60 |
+
"hermevander3d",
|
| 61 |
+
"hermecompanion",
|
| 62 |
+
"hermegauss",
|
| 63 |
+
"hermeweight",
|
| 64 |
+
]
|
| 65 |
+
|
| 66 |
+
_ShapeT = TypeVar("_ShapeT", bound=_Shape)
|
| 67 |
+
|
| 68 |
+
poly2herme: Final[_FuncPoly2Ortho] = ...
|
| 69 |
+
herme2poly: Final[_FuncUnOp] = ...
|
| 70 |
+
|
| 71 |
+
hermedomain: Final[_Array2[np.float64]] = ...
|
| 72 |
+
hermezero: Final[_Array1[np.int_]] = ...
|
| 73 |
+
hermeone: Final[_Array1[np.int_]] = ...
|
| 74 |
+
hermex: Final[_Array2[np.int_]] = ...
|
| 75 |
+
|
| 76 |
+
hermeline: Final[_FuncLine] = ...
|
| 77 |
+
hermefromroots: Final[_FuncFromRoots] = ...
|
| 78 |
+
hermeadd: Final[_FuncBinOp] = ...
|
| 79 |
+
hermesub: Final[_FuncBinOp] = ...
|
| 80 |
+
hermemulx: Final[_FuncUnOp] = ...
|
| 81 |
+
hermemul: Final[_FuncBinOp] = ...
|
| 82 |
+
hermediv: Final[_FuncBinOp] = ...
|
| 83 |
+
hermepow: Final[_FuncPow] = ...
|
| 84 |
+
hermeder: Final[_FuncDer] = ...
|
| 85 |
+
hermeint: Final[_FuncInteg] = ...
|
| 86 |
+
hermeval: Final[_FuncVal] = ...
|
| 87 |
+
hermeval2d: Final[_FuncVal2D] = ...
|
| 88 |
+
hermeval3d: Final[_FuncVal3D] = ...
|
| 89 |
+
hermegrid2d: Final[_FuncVal2D] = ...
|
| 90 |
+
hermegrid3d: Final[_FuncVal3D] = ...
|
| 91 |
+
hermevander: Final[_FuncVander] = ...
|
| 92 |
+
hermevander2d: Final[_FuncVander2D] = ...
|
| 93 |
+
hermevander3d: Final[_FuncVander3D] = ...
|
| 94 |
+
hermefit: Final[_FuncFit] = ...
|
| 95 |
+
hermecompanion: Final[_FuncCompanion] = ...
|
| 96 |
+
hermeroots: Final[_FuncRoots] = ...
|
| 97 |
+
|
| 98 |
+
def _normed_hermite_e_n(x: np.ndarray[_ShapeT, np.dtype[np.float64]], n: int) -> np.ndarray[_ShapeT, np.dtype[np.float64]]: ...
|
| 99 |
+
|
| 100 |
+
hermegauss: Final[_FuncGauss] = ...
|
| 101 |
+
hermeweight: Final[_FuncWeight] = ...
|
| 102 |
+
|
| 103 |
+
class HermiteE(ABCPolyBase[L["He"]]):
|
| 104 |
+
basis_name: ClassVar[L["He"]] = "He" # pyright: ignore[reportIncompatibleMethodOverride]
|
| 105 |
+
domain: _Array2[np.float64 | Any] = ... # pyright: ignore[reportIncompatibleMethodOverride]
|
| 106 |
+
window: _Array2[np.float64 | Any] = ... # pyright: ignore[reportIncompatibleMethodOverride]
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/laguerre.py
ADDED
|
@@ -0,0 +1,1673 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
==================================================
|
| 3 |
+
Laguerre Series (:mod:`numpy.polynomial.laguerre`)
|
| 4 |
+
==================================================
|
| 5 |
+
|
| 6 |
+
This module provides a number of objects (mostly functions) useful for
|
| 7 |
+
dealing with Laguerre series, including a `Laguerre` class that
|
| 8 |
+
encapsulates the usual arithmetic operations. (General information
|
| 9 |
+
on how this module represents and works with such polynomials is in the
|
| 10 |
+
docstring for its "parent" sub-package, `numpy.polynomial`).
|
| 11 |
+
|
| 12 |
+
Classes
|
| 13 |
+
-------
|
| 14 |
+
.. autosummary::
|
| 15 |
+
:toctree: generated/
|
| 16 |
+
|
| 17 |
+
Laguerre
|
| 18 |
+
|
| 19 |
+
Constants
|
| 20 |
+
---------
|
| 21 |
+
.. autosummary::
|
| 22 |
+
:toctree: generated/
|
| 23 |
+
|
| 24 |
+
lagdomain
|
| 25 |
+
lagzero
|
| 26 |
+
lagone
|
| 27 |
+
lagx
|
| 28 |
+
|
| 29 |
+
Arithmetic
|
| 30 |
+
----------
|
| 31 |
+
.. autosummary::
|
| 32 |
+
:toctree: generated/
|
| 33 |
+
|
| 34 |
+
lagadd
|
| 35 |
+
lagsub
|
| 36 |
+
lagmulx
|
| 37 |
+
lagmul
|
| 38 |
+
lagdiv
|
| 39 |
+
lagpow
|
| 40 |
+
lagval
|
| 41 |
+
lagval2d
|
| 42 |
+
lagval3d
|
| 43 |
+
laggrid2d
|
| 44 |
+
laggrid3d
|
| 45 |
+
|
| 46 |
+
Calculus
|
| 47 |
+
--------
|
| 48 |
+
.. autosummary::
|
| 49 |
+
:toctree: generated/
|
| 50 |
+
|
| 51 |
+
lagder
|
| 52 |
+
lagint
|
| 53 |
+
|
| 54 |
+
Misc Functions
|
| 55 |
+
--------------
|
| 56 |
+
.. autosummary::
|
| 57 |
+
:toctree: generated/
|
| 58 |
+
|
| 59 |
+
lagfromroots
|
| 60 |
+
lagroots
|
| 61 |
+
lagvander
|
| 62 |
+
lagvander2d
|
| 63 |
+
lagvander3d
|
| 64 |
+
laggauss
|
| 65 |
+
lagweight
|
| 66 |
+
lagcompanion
|
| 67 |
+
lagfit
|
| 68 |
+
lagtrim
|
| 69 |
+
lagline
|
| 70 |
+
lag2poly
|
| 71 |
+
poly2lag
|
| 72 |
+
|
| 73 |
+
See also
|
| 74 |
+
--------
|
| 75 |
+
`numpy.polynomial`
|
| 76 |
+
|
| 77 |
+
"""
|
| 78 |
+
import numpy as np
|
| 79 |
+
|
| 80 |
+
from . import polyutils as pu
|
| 81 |
+
from ._polybase import ABCPolyBase
|
| 82 |
+
|
| 83 |
+
__all__ = [
|
| 84 |
+
'lagzero', 'lagone', 'lagx', 'lagdomain', 'lagline', 'lagadd',
|
| 85 |
+
'lagsub', 'lagmulx', 'lagmul', 'lagdiv', 'lagpow', 'lagval', 'lagder',
|
| 86 |
+
'lagint', 'lag2poly', 'poly2lag', 'lagfromroots', 'lagvander',
|
| 87 |
+
'lagfit', 'lagtrim', 'lagroots', 'Laguerre', 'lagval2d', 'lagval3d',
|
| 88 |
+
'laggrid2d', 'laggrid3d', 'lagvander2d', 'lagvander3d', 'lagcompanion',
|
| 89 |
+
'laggauss', 'lagweight']
|
| 90 |
+
|
| 91 |
+
lagtrim = pu.trimcoef
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def poly2lag(pol):
|
| 95 |
+
"""
|
| 96 |
+
poly2lag(pol)
|
| 97 |
+
|
| 98 |
+
Convert a polynomial to a Laguerre series.
|
| 99 |
+
|
| 100 |
+
Convert an array representing the coefficients of a polynomial (relative
|
| 101 |
+
to the "standard" basis) ordered from lowest degree to highest, to an
|
| 102 |
+
array of the coefficients of the equivalent Laguerre series, ordered
|
| 103 |
+
from lowest to highest degree.
|
| 104 |
+
|
| 105 |
+
Parameters
|
| 106 |
+
----------
|
| 107 |
+
pol : array_like
|
| 108 |
+
1-D array containing the polynomial coefficients
|
| 109 |
+
|
| 110 |
+
Returns
|
| 111 |
+
-------
|
| 112 |
+
c : ndarray
|
| 113 |
+
1-D array containing the coefficients of the equivalent Laguerre
|
| 114 |
+
series.
|
| 115 |
+
|
| 116 |
+
See Also
|
| 117 |
+
--------
|
| 118 |
+
lag2poly
|
| 119 |
+
|
| 120 |
+
Notes
|
| 121 |
+
-----
|
| 122 |
+
The easy way to do conversions between polynomial basis sets
|
| 123 |
+
is to use the convert method of a class instance.
|
| 124 |
+
|
| 125 |
+
Examples
|
| 126 |
+
--------
|
| 127 |
+
>>> import numpy as np
|
| 128 |
+
>>> from numpy.polynomial.laguerre import poly2lag
|
| 129 |
+
>>> poly2lag(np.arange(4))
|
| 130 |
+
array([ 23., -63., 58., -18.])
|
| 131 |
+
|
| 132 |
+
"""
|
| 133 |
+
[pol] = pu.as_series([pol])
|
| 134 |
+
res = 0
|
| 135 |
+
for p in pol[::-1]:
|
| 136 |
+
res = lagadd(lagmulx(res), p)
|
| 137 |
+
return res
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
def lag2poly(c):
|
| 141 |
+
"""
|
| 142 |
+
Convert a Laguerre series to a polynomial.
|
| 143 |
+
|
| 144 |
+
Convert an array representing the coefficients of a Laguerre series,
|
| 145 |
+
ordered from lowest degree to highest, to an array of the coefficients
|
| 146 |
+
of the equivalent polynomial (relative to the "standard" basis) ordered
|
| 147 |
+
from lowest to highest degree.
|
| 148 |
+
|
| 149 |
+
Parameters
|
| 150 |
+
----------
|
| 151 |
+
c : array_like
|
| 152 |
+
1-D array containing the Laguerre series coefficients, ordered
|
| 153 |
+
from lowest order term to highest.
|
| 154 |
+
|
| 155 |
+
Returns
|
| 156 |
+
-------
|
| 157 |
+
pol : ndarray
|
| 158 |
+
1-D array containing the coefficients of the equivalent polynomial
|
| 159 |
+
(relative to the "standard" basis) ordered from lowest order term
|
| 160 |
+
to highest.
|
| 161 |
+
|
| 162 |
+
See Also
|
| 163 |
+
--------
|
| 164 |
+
poly2lag
|
| 165 |
+
|
| 166 |
+
Notes
|
| 167 |
+
-----
|
| 168 |
+
The easy way to do conversions between polynomial basis sets
|
| 169 |
+
is to use the convert method of a class instance.
|
| 170 |
+
|
| 171 |
+
Examples
|
| 172 |
+
--------
|
| 173 |
+
>>> from numpy.polynomial.laguerre import lag2poly
|
| 174 |
+
>>> lag2poly([ 23., -63., 58., -18.])
|
| 175 |
+
array([0., 1., 2., 3.])
|
| 176 |
+
|
| 177 |
+
"""
|
| 178 |
+
from .polynomial import polyadd, polymulx, polysub
|
| 179 |
+
|
| 180 |
+
[c] = pu.as_series([c])
|
| 181 |
+
n = len(c)
|
| 182 |
+
if n == 1:
|
| 183 |
+
return c
|
| 184 |
+
else:
|
| 185 |
+
c0 = c[-2]
|
| 186 |
+
c1 = c[-1]
|
| 187 |
+
# i is the current degree of c1
|
| 188 |
+
for i in range(n - 1, 1, -1):
|
| 189 |
+
tmp = c0
|
| 190 |
+
c0 = polysub(c[i - 2], (c1 * (i - 1)) / i)
|
| 191 |
+
c1 = polyadd(tmp, polysub((2 * i - 1) * c1, polymulx(c1)) / i)
|
| 192 |
+
return polyadd(c0, polysub(c1, polymulx(c1)))
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
#
|
| 196 |
+
# These are constant arrays are of integer type so as to be compatible
|
| 197 |
+
# with the widest range of other types, such as Decimal.
|
| 198 |
+
#
|
| 199 |
+
|
| 200 |
+
# Laguerre
|
| 201 |
+
lagdomain = np.array([0., 1.])
|
| 202 |
+
|
| 203 |
+
# Laguerre coefficients representing zero.
|
| 204 |
+
lagzero = np.array([0])
|
| 205 |
+
|
| 206 |
+
# Laguerre coefficients representing one.
|
| 207 |
+
lagone = np.array([1])
|
| 208 |
+
|
| 209 |
+
# Laguerre coefficients representing the identity x.
|
| 210 |
+
lagx = np.array([1, -1])
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
def lagline(off, scl):
|
| 214 |
+
"""
|
| 215 |
+
Laguerre series whose graph is a straight line.
|
| 216 |
+
|
| 217 |
+
Parameters
|
| 218 |
+
----------
|
| 219 |
+
off, scl : scalars
|
| 220 |
+
The specified line is given by ``off + scl*x``.
|
| 221 |
+
|
| 222 |
+
Returns
|
| 223 |
+
-------
|
| 224 |
+
y : ndarray
|
| 225 |
+
This module's representation of the Laguerre series for
|
| 226 |
+
``off + scl*x``.
|
| 227 |
+
|
| 228 |
+
See Also
|
| 229 |
+
--------
|
| 230 |
+
numpy.polynomial.polynomial.polyline
|
| 231 |
+
numpy.polynomial.chebyshev.chebline
|
| 232 |
+
numpy.polynomial.legendre.legline
|
| 233 |
+
numpy.polynomial.hermite.hermline
|
| 234 |
+
numpy.polynomial.hermite_e.hermeline
|
| 235 |
+
|
| 236 |
+
Examples
|
| 237 |
+
--------
|
| 238 |
+
>>> from numpy.polynomial.laguerre import lagline, lagval
|
| 239 |
+
>>> lagval(0,lagline(3, 2))
|
| 240 |
+
3.0
|
| 241 |
+
>>> lagval(1,lagline(3, 2))
|
| 242 |
+
5.0
|
| 243 |
+
|
| 244 |
+
"""
|
| 245 |
+
if scl != 0:
|
| 246 |
+
return np.array([off + scl, -scl])
|
| 247 |
+
else:
|
| 248 |
+
return np.array([off])
|
| 249 |
+
|
| 250 |
+
|
| 251 |
+
def lagfromroots(roots):
|
| 252 |
+
"""
|
| 253 |
+
Generate a Laguerre series with given roots.
|
| 254 |
+
|
| 255 |
+
The function returns the coefficients of the polynomial
|
| 256 |
+
|
| 257 |
+
.. math:: p(x) = (x - r_0) * (x - r_1) * ... * (x - r_n),
|
| 258 |
+
|
| 259 |
+
in Laguerre form, where the :math:`r_n` are the roots specified in `roots`.
|
| 260 |
+
If a zero has multiplicity n, then it must appear in `roots` n times.
|
| 261 |
+
For instance, if 2 is a root of multiplicity three and 3 is a root of
|
| 262 |
+
multiplicity 2, then `roots` looks something like [2, 2, 2, 3, 3]. The
|
| 263 |
+
roots can appear in any order.
|
| 264 |
+
|
| 265 |
+
If the returned coefficients are `c`, then
|
| 266 |
+
|
| 267 |
+
.. math:: p(x) = c_0 + c_1 * L_1(x) + ... + c_n * L_n(x)
|
| 268 |
+
|
| 269 |
+
The coefficient of the last term is not generally 1 for monic
|
| 270 |
+
polynomials in Laguerre form.
|
| 271 |
+
|
| 272 |
+
Parameters
|
| 273 |
+
----------
|
| 274 |
+
roots : array_like
|
| 275 |
+
Sequence containing the roots.
|
| 276 |
+
|
| 277 |
+
Returns
|
| 278 |
+
-------
|
| 279 |
+
out : ndarray
|
| 280 |
+
1-D array of coefficients. If all roots are real then `out` is a
|
| 281 |
+
real array, if some of the roots are complex, then `out` is complex
|
| 282 |
+
even if all the coefficients in the result are real (see Examples
|
| 283 |
+
below).
|
| 284 |
+
|
| 285 |
+
See Also
|
| 286 |
+
--------
|
| 287 |
+
numpy.polynomial.polynomial.polyfromroots
|
| 288 |
+
numpy.polynomial.legendre.legfromroots
|
| 289 |
+
numpy.polynomial.chebyshev.chebfromroots
|
| 290 |
+
numpy.polynomial.hermite.hermfromroots
|
| 291 |
+
numpy.polynomial.hermite_e.hermefromroots
|
| 292 |
+
|
| 293 |
+
Examples
|
| 294 |
+
--------
|
| 295 |
+
>>> from numpy.polynomial.laguerre import lagfromroots, lagval
|
| 296 |
+
>>> coef = lagfromroots((-1, 0, 1))
|
| 297 |
+
>>> lagval((-1, 0, 1), coef)
|
| 298 |
+
array([0., 0., 0.])
|
| 299 |
+
>>> coef = lagfromroots((-1j, 1j))
|
| 300 |
+
>>> lagval((-1j, 1j), coef)
|
| 301 |
+
array([0.+0.j, 0.+0.j])
|
| 302 |
+
|
| 303 |
+
"""
|
| 304 |
+
return pu._fromroots(lagline, lagmul, roots)
|
| 305 |
+
|
| 306 |
+
|
| 307 |
+
def lagadd(c1, c2):
|
| 308 |
+
"""
|
| 309 |
+
Add one Laguerre series to another.
|
| 310 |
+
|
| 311 |
+
Returns the sum of two Laguerre series `c1` + `c2`. The arguments
|
| 312 |
+
are sequences of coefficients ordered from lowest order term to
|
| 313 |
+
highest, i.e., [1,2,3] represents the series ``P_0 + 2*P_1 + 3*P_2``.
|
| 314 |
+
|
| 315 |
+
Parameters
|
| 316 |
+
----------
|
| 317 |
+
c1, c2 : array_like
|
| 318 |
+
1-D arrays of Laguerre series coefficients ordered from low to
|
| 319 |
+
high.
|
| 320 |
+
|
| 321 |
+
Returns
|
| 322 |
+
-------
|
| 323 |
+
out : ndarray
|
| 324 |
+
Array representing the Laguerre series of their sum.
|
| 325 |
+
|
| 326 |
+
See Also
|
| 327 |
+
--------
|
| 328 |
+
lagsub, lagmulx, lagmul, lagdiv, lagpow
|
| 329 |
+
|
| 330 |
+
Notes
|
| 331 |
+
-----
|
| 332 |
+
Unlike multiplication, division, etc., the sum of two Laguerre series
|
| 333 |
+
is a Laguerre series (without having to "reproject" the result onto
|
| 334 |
+
the basis set) so addition, just like that of "standard" polynomials,
|
| 335 |
+
is simply "component-wise."
|
| 336 |
+
|
| 337 |
+
Examples
|
| 338 |
+
--------
|
| 339 |
+
>>> from numpy.polynomial.laguerre import lagadd
|
| 340 |
+
>>> lagadd([1, 2, 3], [1, 2, 3, 4])
|
| 341 |
+
array([2., 4., 6., 4.])
|
| 342 |
+
|
| 343 |
+
"""
|
| 344 |
+
return pu._add(c1, c2)
|
| 345 |
+
|
| 346 |
+
|
| 347 |
+
def lagsub(c1, c2):
|
| 348 |
+
"""
|
| 349 |
+
Subtract one Laguerre series from another.
|
| 350 |
+
|
| 351 |
+
Returns the difference of two Laguerre series `c1` - `c2`. The
|
| 352 |
+
sequences of coefficients are from lowest order term to highest, i.e.,
|
| 353 |
+
[1,2,3] represents the series ``P_0 + 2*P_1 + 3*P_2``.
|
| 354 |
+
|
| 355 |
+
Parameters
|
| 356 |
+
----------
|
| 357 |
+
c1, c2 : array_like
|
| 358 |
+
1-D arrays of Laguerre series coefficients ordered from low to
|
| 359 |
+
high.
|
| 360 |
+
|
| 361 |
+
Returns
|
| 362 |
+
-------
|
| 363 |
+
out : ndarray
|
| 364 |
+
Of Laguerre series coefficients representing their difference.
|
| 365 |
+
|
| 366 |
+
See Also
|
| 367 |
+
--------
|
| 368 |
+
lagadd, lagmulx, lagmul, lagdiv, lagpow
|
| 369 |
+
|
| 370 |
+
Notes
|
| 371 |
+
-----
|
| 372 |
+
Unlike multiplication, division, etc., the difference of two Laguerre
|
| 373 |
+
series is a Laguerre series (without having to "reproject" the result
|
| 374 |
+
onto the basis set) so subtraction, just like that of "standard"
|
| 375 |
+
polynomials, is simply "component-wise."
|
| 376 |
+
|
| 377 |
+
Examples
|
| 378 |
+
--------
|
| 379 |
+
>>> from numpy.polynomial.laguerre import lagsub
|
| 380 |
+
>>> lagsub([1, 2, 3, 4], [1, 2, 3])
|
| 381 |
+
array([0., 0., 0., 4.])
|
| 382 |
+
|
| 383 |
+
"""
|
| 384 |
+
return pu._sub(c1, c2)
|
| 385 |
+
|
| 386 |
+
|
| 387 |
+
def lagmulx(c):
|
| 388 |
+
"""Multiply a Laguerre series by x.
|
| 389 |
+
|
| 390 |
+
Multiply the Laguerre series `c` by x, where x is the independent
|
| 391 |
+
variable.
|
| 392 |
+
|
| 393 |
+
|
| 394 |
+
Parameters
|
| 395 |
+
----------
|
| 396 |
+
c : array_like
|
| 397 |
+
1-D array of Laguerre series coefficients ordered from low to
|
| 398 |
+
high.
|
| 399 |
+
|
| 400 |
+
Returns
|
| 401 |
+
-------
|
| 402 |
+
out : ndarray
|
| 403 |
+
Array representing the result of the multiplication.
|
| 404 |
+
|
| 405 |
+
See Also
|
| 406 |
+
--------
|
| 407 |
+
lagadd, lagsub, lagmul, lagdiv, lagpow
|
| 408 |
+
|
| 409 |
+
Notes
|
| 410 |
+
-----
|
| 411 |
+
The multiplication uses the recursion relationship for Laguerre
|
| 412 |
+
polynomials in the form
|
| 413 |
+
|
| 414 |
+
.. math::
|
| 415 |
+
|
| 416 |
+
xP_i(x) = (-(i + 1)*P_{i + 1}(x) + (2i + 1)P_{i}(x) - iP_{i - 1}(x))
|
| 417 |
+
|
| 418 |
+
Examples
|
| 419 |
+
--------
|
| 420 |
+
>>> from numpy.polynomial.laguerre import lagmulx
|
| 421 |
+
>>> lagmulx([1, 2, 3])
|
| 422 |
+
array([-1., -1., 11., -9.])
|
| 423 |
+
|
| 424 |
+
"""
|
| 425 |
+
# c is a trimmed copy
|
| 426 |
+
[c] = pu.as_series([c])
|
| 427 |
+
# The zero series needs special treatment
|
| 428 |
+
if len(c) == 1 and c[0] == 0:
|
| 429 |
+
return c
|
| 430 |
+
|
| 431 |
+
prd = np.empty(len(c) + 1, dtype=c.dtype)
|
| 432 |
+
prd[0] = c[0]
|
| 433 |
+
prd[1] = -c[0]
|
| 434 |
+
for i in range(1, len(c)):
|
| 435 |
+
prd[i + 1] = -c[i] * (i + 1)
|
| 436 |
+
prd[i] += c[i] * (2 * i + 1)
|
| 437 |
+
prd[i - 1] -= c[i] * i
|
| 438 |
+
return prd
|
| 439 |
+
|
| 440 |
+
|
| 441 |
+
def lagmul(c1, c2):
|
| 442 |
+
"""
|
| 443 |
+
Multiply one Laguerre series by another.
|
| 444 |
+
|
| 445 |
+
Returns the product of two Laguerre series `c1` * `c2`. The arguments
|
| 446 |
+
are sequences of coefficients, from lowest order "term" to highest,
|
| 447 |
+
e.g., [1,2,3] represents the series ``P_0 + 2*P_1 + 3*P_2``.
|
| 448 |
+
|
| 449 |
+
Parameters
|
| 450 |
+
----------
|
| 451 |
+
c1, c2 : array_like
|
| 452 |
+
1-D arrays of Laguerre series coefficients ordered from low to
|
| 453 |
+
high.
|
| 454 |
+
|
| 455 |
+
Returns
|
| 456 |
+
-------
|
| 457 |
+
out : ndarray
|
| 458 |
+
Of Laguerre series coefficients representing their product.
|
| 459 |
+
|
| 460 |
+
See Also
|
| 461 |
+
--------
|
| 462 |
+
lagadd, lagsub, lagmulx, lagdiv, lagpow
|
| 463 |
+
|
| 464 |
+
Notes
|
| 465 |
+
-----
|
| 466 |
+
In general, the (polynomial) product of two C-series results in terms
|
| 467 |
+
that are not in the Laguerre polynomial basis set. Thus, to express
|
| 468 |
+
the product as a Laguerre series, it is necessary to "reproject" the
|
| 469 |
+
product onto said basis set, which may produce "unintuitive" (but
|
| 470 |
+
correct) results; see Examples section below.
|
| 471 |
+
|
| 472 |
+
Examples
|
| 473 |
+
--------
|
| 474 |
+
>>> from numpy.polynomial.laguerre import lagmul
|
| 475 |
+
>>> lagmul([1, 2, 3], [0, 1, 2])
|
| 476 |
+
array([ 8., -13., 38., -51., 36.])
|
| 477 |
+
|
| 478 |
+
"""
|
| 479 |
+
# s1, s2 are trimmed copies
|
| 480 |
+
[c1, c2] = pu.as_series([c1, c2])
|
| 481 |
+
|
| 482 |
+
if len(c1) > len(c2):
|
| 483 |
+
c = c2
|
| 484 |
+
xs = c1
|
| 485 |
+
else:
|
| 486 |
+
c = c1
|
| 487 |
+
xs = c2
|
| 488 |
+
|
| 489 |
+
if len(c) == 1:
|
| 490 |
+
c0 = c[0] * xs
|
| 491 |
+
c1 = 0
|
| 492 |
+
elif len(c) == 2:
|
| 493 |
+
c0 = c[0] * xs
|
| 494 |
+
c1 = c[1] * xs
|
| 495 |
+
else:
|
| 496 |
+
nd = len(c)
|
| 497 |
+
c0 = c[-2] * xs
|
| 498 |
+
c1 = c[-1] * xs
|
| 499 |
+
for i in range(3, len(c) + 1):
|
| 500 |
+
tmp = c0
|
| 501 |
+
nd = nd - 1
|
| 502 |
+
c0 = lagsub(c[-i] * xs, (c1 * (nd - 1)) / nd)
|
| 503 |
+
c1 = lagadd(tmp, lagsub((2 * nd - 1) * c1, lagmulx(c1)) / nd)
|
| 504 |
+
return lagadd(c0, lagsub(c1, lagmulx(c1)))
|
| 505 |
+
|
| 506 |
+
|
| 507 |
+
def lagdiv(c1, c2):
|
| 508 |
+
"""
|
| 509 |
+
Divide one Laguerre series by another.
|
| 510 |
+
|
| 511 |
+
Returns the quotient-with-remainder of two Laguerre series
|
| 512 |
+
`c1` / `c2`. The arguments are sequences of coefficients from lowest
|
| 513 |
+
order "term" to highest, e.g., [1,2,3] represents the series
|
| 514 |
+
``P_0 + 2*P_1 + 3*P_2``.
|
| 515 |
+
|
| 516 |
+
Parameters
|
| 517 |
+
----------
|
| 518 |
+
c1, c2 : array_like
|
| 519 |
+
1-D arrays of Laguerre series coefficients ordered from low to
|
| 520 |
+
high.
|
| 521 |
+
|
| 522 |
+
Returns
|
| 523 |
+
-------
|
| 524 |
+
[quo, rem] : ndarrays
|
| 525 |
+
Of Laguerre series coefficients representing the quotient and
|
| 526 |
+
remainder.
|
| 527 |
+
|
| 528 |
+
See Also
|
| 529 |
+
--------
|
| 530 |
+
lagadd, lagsub, lagmulx, lagmul, lagpow
|
| 531 |
+
|
| 532 |
+
Notes
|
| 533 |
+
-----
|
| 534 |
+
In general, the (polynomial) division of one Laguerre series by another
|
| 535 |
+
results in quotient and remainder terms that are not in the Laguerre
|
| 536 |
+
polynomial basis set. Thus, to express these results as a Laguerre
|
| 537 |
+
series, it is necessary to "reproject" the results onto the Laguerre
|
| 538 |
+
basis set, which may produce "unintuitive" (but correct) results; see
|
| 539 |
+
Examples section below.
|
| 540 |
+
|
| 541 |
+
Examples
|
| 542 |
+
--------
|
| 543 |
+
>>> from numpy.polynomial.laguerre import lagdiv
|
| 544 |
+
>>> lagdiv([ 8., -13., 38., -51., 36.], [0, 1, 2])
|
| 545 |
+
(array([1., 2., 3.]), array([0.]))
|
| 546 |
+
>>> lagdiv([ 9., -12., 38., -51., 36.], [0, 1, 2])
|
| 547 |
+
(array([1., 2., 3.]), array([1., 1.]))
|
| 548 |
+
|
| 549 |
+
"""
|
| 550 |
+
return pu._div(lagmul, c1, c2)
|
| 551 |
+
|
| 552 |
+
|
| 553 |
+
def lagpow(c, pow, maxpower=16):
|
| 554 |
+
"""Raise a Laguerre series to a power.
|
| 555 |
+
|
| 556 |
+
Returns the Laguerre series `c` raised to the power `pow`. The
|
| 557 |
+
argument `c` is a sequence of coefficients ordered from low to high.
|
| 558 |
+
i.e., [1,2,3] is the series ``P_0 + 2*P_1 + 3*P_2.``
|
| 559 |
+
|
| 560 |
+
Parameters
|
| 561 |
+
----------
|
| 562 |
+
c : array_like
|
| 563 |
+
1-D array of Laguerre series coefficients ordered from low to
|
| 564 |
+
high.
|
| 565 |
+
pow : integer
|
| 566 |
+
Power to which the series will be raised
|
| 567 |
+
maxpower : integer, optional
|
| 568 |
+
Maximum power allowed. This is mainly to limit growth of the series
|
| 569 |
+
to unmanageable size. Default is 16
|
| 570 |
+
|
| 571 |
+
Returns
|
| 572 |
+
-------
|
| 573 |
+
coef : ndarray
|
| 574 |
+
Laguerre series of power.
|
| 575 |
+
|
| 576 |
+
See Also
|
| 577 |
+
--------
|
| 578 |
+
lagadd, lagsub, lagmulx, lagmul, lagdiv
|
| 579 |
+
|
| 580 |
+
Examples
|
| 581 |
+
--------
|
| 582 |
+
>>> from numpy.polynomial.laguerre import lagpow
|
| 583 |
+
>>> lagpow([1, 2, 3], 2)
|
| 584 |
+
array([ 14., -16., 56., -72., 54.])
|
| 585 |
+
|
| 586 |
+
"""
|
| 587 |
+
return pu._pow(lagmul, c, pow, maxpower)
|
| 588 |
+
|
| 589 |
+
|
| 590 |
+
def lagder(c, m=1, scl=1, axis=0):
|
| 591 |
+
"""
|
| 592 |
+
Differentiate a Laguerre series.
|
| 593 |
+
|
| 594 |
+
Returns the Laguerre series coefficients `c` differentiated `m` times
|
| 595 |
+
along `axis`. At each iteration the result is multiplied by `scl` (the
|
| 596 |
+
scaling factor is for use in a linear change of variable). The argument
|
| 597 |
+
`c` is an array of coefficients from low to high degree along each
|
| 598 |
+
axis, e.g., [1,2,3] represents the series ``1*L_0 + 2*L_1 + 3*L_2``
|
| 599 |
+
while [[1,2],[1,2]] represents ``1*L_0(x)*L_0(y) + 1*L_1(x)*L_0(y) +
|
| 600 |
+
2*L_0(x)*L_1(y) + 2*L_1(x)*L_1(y)`` if axis=0 is ``x`` and axis=1 is
|
| 601 |
+
``y``.
|
| 602 |
+
|
| 603 |
+
Parameters
|
| 604 |
+
----------
|
| 605 |
+
c : array_like
|
| 606 |
+
Array of Laguerre series coefficients. If `c` is multidimensional
|
| 607 |
+
the different axis correspond to different variables with the
|
| 608 |
+
degree in each axis given by the corresponding index.
|
| 609 |
+
m : int, optional
|
| 610 |
+
Number of derivatives taken, must be non-negative. (Default: 1)
|
| 611 |
+
scl : scalar, optional
|
| 612 |
+
Each differentiation is multiplied by `scl`. The end result is
|
| 613 |
+
multiplication by ``scl**m``. This is for use in a linear change of
|
| 614 |
+
variable. (Default: 1)
|
| 615 |
+
axis : int, optional
|
| 616 |
+
Axis over which the derivative is taken. (Default: 0).
|
| 617 |
+
|
| 618 |
+
Returns
|
| 619 |
+
-------
|
| 620 |
+
der : ndarray
|
| 621 |
+
Laguerre series of the derivative.
|
| 622 |
+
|
| 623 |
+
See Also
|
| 624 |
+
--------
|
| 625 |
+
lagint
|
| 626 |
+
|
| 627 |
+
Notes
|
| 628 |
+
-----
|
| 629 |
+
In general, the result of differentiating a Laguerre series does not
|
| 630 |
+
resemble the same operation on a power series. Thus the result of this
|
| 631 |
+
function may be "unintuitive," albeit correct; see Examples section
|
| 632 |
+
below.
|
| 633 |
+
|
| 634 |
+
Examples
|
| 635 |
+
--------
|
| 636 |
+
>>> from numpy.polynomial.laguerre import lagder
|
| 637 |
+
>>> lagder([ 1., 1., 1., -3.])
|
| 638 |
+
array([1., 2., 3.])
|
| 639 |
+
>>> lagder([ 1., 0., 0., -4., 3.], m=2)
|
| 640 |
+
array([1., 2., 3.])
|
| 641 |
+
|
| 642 |
+
"""
|
| 643 |
+
c = np.array(c, ndmin=1, copy=True)
|
| 644 |
+
if c.dtype.char in '?bBhHiIlLqQpP':
|
| 645 |
+
c = c.astype(np.double)
|
| 646 |
+
|
| 647 |
+
cnt = pu._as_int(m, "the order of derivation")
|
| 648 |
+
iaxis = pu._as_int(axis, "the axis")
|
| 649 |
+
if cnt < 0:
|
| 650 |
+
raise ValueError("The order of derivation must be non-negative")
|
| 651 |
+
iaxis = np.lib.array_utils.normalize_axis_index(iaxis, c.ndim)
|
| 652 |
+
|
| 653 |
+
if cnt == 0:
|
| 654 |
+
return c
|
| 655 |
+
|
| 656 |
+
c = np.moveaxis(c, iaxis, 0)
|
| 657 |
+
n = len(c)
|
| 658 |
+
if cnt >= n:
|
| 659 |
+
c = c[:1] * 0
|
| 660 |
+
else:
|
| 661 |
+
for i in range(cnt):
|
| 662 |
+
n = n - 1
|
| 663 |
+
c *= scl
|
| 664 |
+
der = np.empty((n,) + c.shape[1:], dtype=c.dtype)
|
| 665 |
+
for j in range(n, 1, -1):
|
| 666 |
+
der[j - 1] = -c[j]
|
| 667 |
+
c[j - 1] += c[j]
|
| 668 |
+
der[0] = -c[1]
|
| 669 |
+
c = der
|
| 670 |
+
c = np.moveaxis(c, 0, iaxis)
|
| 671 |
+
return c
|
| 672 |
+
|
| 673 |
+
|
| 674 |
+
def lagint(c, m=1, k=[], lbnd=0, scl=1, axis=0):
|
| 675 |
+
"""
|
| 676 |
+
Integrate a Laguerre series.
|
| 677 |
+
|
| 678 |
+
Returns the Laguerre series coefficients `c` integrated `m` times from
|
| 679 |
+
`lbnd` along `axis`. At each iteration the resulting series is
|
| 680 |
+
**multiplied** by `scl` and an integration constant, `k`, is added.
|
| 681 |
+
The scaling factor is for use in a linear change of variable. ("Buyer
|
| 682 |
+
beware": note that, depending on what one is doing, one may want `scl`
|
| 683 |
+
to be the reciprocal of what one might expect; for more information,
|
| 684 |
+
see the Notes section below.) The argument `c` is an array of
|
| 685 |
+
coefficients from low to high degree along each axis, e.g., [1,2,3]
|
| 686 |
+
represents the series ``L_0 + 2*L_1 + 3*L_2`` while [[1,2],[1,2]]
|
| 687 |
+
represents ``1*L_0(x)*L_0(y) + 1*L_1(x)*L_0(y) + 2*L_0(x)*L_1(y) +
|
| 688 |
+
2*L_1(x)*L_1(y)`` if axis=0 is ``x`` and axis=1 is ``y``.
|
| 689 |
+
|
| 690 |
+
|
| 691 |
+
Parameters
|
| 692 |
+
----------
|
| 693 |
+
c : array_like
|
| 694 |
+
Array of Laguerre series coefficients. If `c` is multidimensional
|
| 695 |
+
the different axis correspond to different variables with the
|
| 696 |
+
degree in each axis given by the corresponding index.
|
| 697 |
+
m : int, optional
|
| 698 |
+
Order of integration, must be positive. (Default: 1)
|
| 699 |
+
k : {[], list, scalar}, optional
|
| 700 |
+
Integration constant(s). The value of the first integral at
|
| 701 |
+
``lbnd`` is the first value in the list, the value of the second
|
| 702 |
+
integral at ``lbnd`` is the second value, etc. If ``k == []`` (the
|
| 703 |
+
default), all constants are set to zero. If ``m == 1``, a single
|
| 704 |
+
scalar can be given instead of a list.
|
| 705 |
+
lbnd : scalar, optional
|
| 706 |
+
The lower bound of the integral. (Default: 0)
|
| 707 |
+
scl : scalar, optional
|
| 708 |
+
Following each integration the result is *multiplied* by `scl`
|
| 709 |
+
before the integration constant is added. (Default: 1)
|
| 710 |
+
axis : int, optional
|
| 711 |
+
Axis over which the integral is taken. (Default: 0).
|
| 712 |
+
|
| 713 |
+
Returns
|
| 714 |
+
-------
|
| 715 |
+
S : ndarray
|
| 716 |
+
Laguerre series coefficients of the integral.
|
| 717 |
+
|
| 718 |
+
Raises
|
| 719 |
+
------
|
| 720 |
+
ValueError
|
| 721 |
+
If ``m < 0``, ``len(k) > m``, ``np.ndim(lbnd) != 0``, or
|
| 722 |
+
``np.ndim(scl) != 0``.
|
| 723 |
+
|
| 724 |
+
See Also
|
| 725 |
+
--------
|
| 726 |
+
lagder
|
| 727 |
+
|
| 728 |
+
Notes
|
| 729 |
+
-----
|
| 730 |
+
Note that the result of each integration is *multiplied* by `scl`.
|
| 731 |
+
Why is this important to note? Say one is making a linear change of
|
| 732 |
+
variable :math:`u = ax + b` in an integral relative to `x`. Then
|
| 733 |
+
:math:`dx = du/a`, so one will need to set `scl` equal to
|
| 734 |
+
:math:`1/a` - perhaps not what one would have first thought.
|
| 735 |
+
|
| 736 |
+
Also note that, in general, the result of integrating a C-series needs
|
| 737 |
+
to be "reprojected" onto the C-series basis set. Thus, typically,
|
| 738 |
+
the result of this function is "unintuitive," albeit correct; see
|
| 739 |
+
Examples section below.
|
| 740 |
+
|
| 741 |
+
Examples
|
| 742 |
+
--------
|
| 743 |
+
>>> from numpy.polynomial.laguerre import lagint
|
| 744 |
+
>>> lagint([1,2,3])
|
| 745 |
+
array([ 1., 1., 1., -3.])
|
| 746 |
+
>>> lagint([1,2,3], m=2)
|
| 747 |
+
array([ 1., 0., 0., -4., 3.])
|
| 748 |
+
>>> lagint([1,2,3], k=1)
|
| 749 |
+
array([ 2., 1., 1., -3.])
|
| 750 |
+
>>> lagint([1,2,3], lbnd=-1)
|
| 751 |
+
array([11.5, 1. , 1. , -3. ])
|
| 752 |
+
>>> lagint([1,2], m=2, k=[1,2], lbnd=-1)
|
| 753 |
+
array([ 11.16666667, -5. , -3. , 2. ]) # may vary
|
| 754 |
+
|
| 755 |
+
"""
|
| 756 |
+
c = np.array(c, ndmin=1, copy=True)
|
| 757 |
+
if c.dtype.char in '?bBhHiIlLqQpP':
|
| 758 |
+
c = c.astype(np.double)
|
| 759 |
+
if not np.iterable(k):
|
| 760 |
+
k = [k]
|
| 761 |
+
cnt = pu._as_int(m, "the order of integration")
|
| 762 |
+
iaxis = pu._as_int(axis, "the axis")
|
| 763 |
+
if cnt < 0:
|
| 764 |
+
raise ValueError("The order of integration must be non-negative")
|
| 765 |
+
if len(k) > cnt:
|
| 766 |
+
raise ValueError("Too many integration constants")
|
| 767 |
+
if np.ndim(lbnd) != 0:
|
| 768 |
+
raise ValueError("lbnd must be a scalar.")
|
| 769 |
+
if np.ndim(scl) != 0:
|
| 770 |
+
raise ValueError("scl must be a scalar.")
|
| 771 |
+
iaxis = np.lib.array_utils.normalize_axis_index(iaxis, c.ndim)
|
| 772 |
+
|
| 773 |
+
if cnt == 0:
|
| 774 |
+
return c
|
| 775 |
+
|
| 776 |
+
c = np.moveaxis(c, iaxis, 0)
|
| 777 |
+
k = list(k) + [0] * (cnt - len(k))
|
| 778 |
+
for i in range(cnt):
|
| 779 |
+
n = len(c)
|
| 780 |
+
c *= scl
|
| 781 |
+
if n == 1 and np.all(c[0] == 0):
|
| 782 |
+
c[0] += k[i]
|
| 783 |
+
else:
|
| 784 |
+
tmp = np.empty((n + 1,) + c.shape[1:], dtype=c.dtype)
|
| 785 |
+
tmp[0] = c[0]
|
| 786 |
+
tmp[1] = -c[0]
|
| 787 |
+
for j in range(1, n):
|
| 788 |
+
tmp[j] += c[j]
|
| 789 |
+
tmp[j + 1] = -c[j]
|
| 790 |
+
tmp[0] += k[i] - lagval(lbnd, tmp)
|
| 791 |
+
c = tmp
|
| 792 |
+
c = np.moveaxis(c, 0, iaxis)
|
| 793 |
+
return c
|
| 794 |
+
|
| 795 |
+
|
| 796 |
+
def lagval(x, c, tensor=True):
|
| 797 |
+
"""
|
| 798 |
+
Evaluate a Laguerre series at points x.
|
| 799 |
+
|
| 800 |
+
If `c` is of length ``n + 1``, this function returns the value:
|
| 801 |
+
|
| 802 |
+
.. math:: p(x) = c_0 * L_0(x) + c_1 * L_1(x) + ... + c_n * L_n(x)
|
| 803 |
+
|
| 804 |
+
The parameter `x` is converted to an array only if it is a tuple or a
|
| 805 |
+
list, otherwise it is treated as a scalar. In either case, either `x`
|
| 806 |
+
or its elements must support multiplication and addition both with
|
| 807 |
+
themselves and with the elements of `c`.
|
| 808 |
+
|
| 809 |
+
If `c` is a 1-D array, then ``p(x)`` will have the same shape as `x`. If
|
| 810 |
+
`c` is multidimensional, then the shape of the result depends on the
|
| 811 |
+
value of `tensor`. If `tensor` is true the shape will be c.shape[1:] +
|
| 812 |
+
x.shape. If `tensor` is false the shape will be c.shape[1:]. Note that
|
| 813 |
+
scalars have shape (,).
|
| 814 |
+
|
| 815 |
+
Trailing zeros in the coefficients will be used in the evaluation, so
|
| 816 |
+
they should be avoided if efficiency is a concern.
|
| 817 |
+
|
| 818 |
+
Parameters
|
| 819 |
+
----------
|
| 820 |
+
x : array_like, compatible object
|
| 821 |
+
If `x` is a list or tuple, it is converted to an ndarray, otherwise
|
| 822 |
+
it is left unchanged and treated as a scalar. In either case, `x`
|
| 823 |
+
or its elements must support addition and multiplication with
|
| 824 |
+
themselves and with the elements of `c`.
|
| 825 |
+
c : array_like
|
| 826 |
+
Array of coefficients ordered so that the coefficients for terms of
|
| 827 |
+
degree n are contained in c[n]. If `c` is multidimensional the
|
| 828 |
+
remaining indices enumerate multiple polynomials. In the two
|
| 829 |
+
dimensional case the coefficients may be thought of as stored in
|
| 830 |
+
the columns of `c`.
|
| 831 |
+
tensor : boolean, optional
|
| 832 |
+
If True, the shape of the coefficient array is extended with ones
|
| 833 |
+
on the right, one for each dimension of `x`. Scalars have dimension 0
|
| 834 |
+
for this action. The result is that every column of coefficients in
|
| 835 |
+
`c` is evaluated for every element of `x`. If False, `x` is broadcast
|
| 836 |
+
over the columns of `c` for the evaluation. This keyword is useful
|
| 837 |
+
when `c` is multidimensional. The default value is True.
|
| 838 |
+
|
| 839 |
+
Returns
|
| 840 |
+
-------
|
| 841 |
+
values : ndarray, algebra_like
|
| 842 |
+
The shape of the return value is described above.
|
| 843 |
+
|
| 844 |
+
See Also
|
| 845 |
+
--------
|
| 846 |
+
lagval2d, laggrid2d, lagval3d, laggrid3d
|
| 847 |
+
|
| 848 |
+
Notes
|
| 849 |
+
-----
|
| 850 |
+
The evaluation uses Clenshaw recursion, aka synthetic division.
|
| 851 |
+
|
| 852 |
+
Examples
|
| 853 |
+
--------
|
| 854 |
+
>>> from numpy.polynomial.laguerre import lagval
|
| 855 |
+
>>> coef = [1, 2, 3]
|
| 856 |
+
>>> lagval(1, coef)
|
| 857 |
+
-0.5
|
| 858 |
+
>>> lagval([[1, 2],[3, 4]], coef)
|
| 859 |
+
array([[-0.5, -4. ],
|
| 860 |
+
[-4.5, -2. ]])
|
| 861 |
+
|
| 862 |
+
"""
|
| 863 |
+
c = np.array(c, ndmin=1, copy=None)
|
| 864 |
+
if c.dtype.char in '?bBhHiIlLqQpP':
|
| 865 |
+
c = c.astype(np.double)
|
| 866 |
+
if isinstance(x, (tuple, list)):
|
| 867 |
+
x = np.asarray(x)
|
| 868 |
+
if isinstance(x, np.ndarray) and tensor:
|
| 869 |
+
c = c.reshape(c.shape + (1,) * x.ndim)
|
| 870 |
+
|
| 871 |
+
if len(c) == 1:
|
| 872 |
+
c0 = c[0]
|
| 873 |
+
c1 = 0
|
| 874 |
+
elif len(c) == 2:
|
| 875 |
+
c0 = c[0]
|
| 876 |
+
c1 = c[1]
|
| 877 |
+
else:
|
| 878 |
+
nd = len(c)
|
| 879 |
+
c0 = c[-2]
|
| 880 |
+
c1 = c[-1]
|
| 881 |
+
for i in range(3, len(c) + 1):
|
| 882 |
+
tmp = c0
|
| 883 |
+
nd = nd - 1
|
| 884 |
+
c0 = c[-i] - (c1 * (nd - 1)) / nd
|
| 885 |
+
c1 = tmp + (c1 * ((2 * nd - 1) - x)) / nd
|
| 886 |
+
return c0 + c1 * (1 - x)
|
| 887 |
+
|
| 888 |
+
|
| 889 |
+
def lagval2d(x, y, c):
|
| 890 |
+
"""
|
| 891 |
+
Evaluate a 2-D Laguerre series at points (x, y).
|
| 892 |
+
|
| 893 |
+
This function returns the values:
|
| 894 |
+
|
| 895 |
+
.. math:: p(x,y) = \\sum_{i,j} c_{i,j} * L_i(x) * L_j(y)
|
| 896 |
+
|
| 897 |
+
The parameters `x` and `y` are converted to arrays only if they are
|
| 898 |
+
tuples or a lists, otherwise they are treated as a scalars and they
|
| 899 |
+
must have the same shape after conversion. In either case, either `x`
|
| 900 |
+
and `y` or their elements must support multiplication and addition both
|
| 901 |
+
with themselves and with the elements of `c`.
|
| 902 |
+
|
| 903 |
+
If `c` is a 1-D array a one is implicitly appended to its shape to make
|
| 904 |
+
it 2-D. The shape of the result will be c.shape[2:] + x.shape.
|
| 905 |
+
|
| 906 |
+
Parameters
|
| 907 |
+
----------
|
| 908 |
+
x, y : array_like, compatible objects
|
| 909 |
+
The two dimensional series is evaluated at the points ``(x, y)``,
|
| 910 |
+
where `x` and `y` must have the same shape. If `x` or `y` is a list
|
| 911 |
+
or tuple, it is first converted to an ndarray, otherwise it is left
|
| 912 |
+
unchanged and if it isn't an ndarray it is treated as a scalar.
|
| 913 |
+
c : array_like
|
| 914 |
+
Array of coefficients ordered so that the coefficient of the term
|
| 915 |
+
of multi-degree i,j is contained in ``c[i,j]``. If `c` has
|
| 916 |
+
dimension greater than two the remaining indices enumerate multiple
|
| 917 |
+
sets of coefficients.
|
| 918 |
+
|
| 919 |
+
Returns
|
| 920 |
+
-------
|
| 921 |
+
values : ndarray, compatible object
|
| 922 |
+
The values of the two dimensional polynomial at points formed with
|
| 923 |
+
pairs of corresponding values from `x` and `y`.
|
| 924 |
+
|
| 925 |
+
See Also
|
| 926 |
+
--------
|
| 927 |
+
lagval, laggrid2d, lagval3d, laggrid3d
|
| 928 |
+
|
| 929 |
+
Examples
|
| 930 |
+
--------
|
| 931 |
+
>>> from numpy.polynomial.laguerre import lagval2d
|
| 932 |
+
>>> c = [[1, 2],[3, 4]]
|
| 933 |
+
>>> lagval2d(1, 1, c)
|
| 934 |
+
1.0
|
| 935 |
+
"""
|
| 936 |
+
return pu._valnd(lagval, c, x, y)
|
| 937 |
+
|
| 938 |
+
|
| 939 |
+
def laggrid2d(x, y, c):
|
| 940 |
+
"""
|
| 941 |
+
Evaluate a 2-D Laguerre series on the Cartesian product of x and y.
|
| 942 |
+
|
| 943 |
+
This function returns the values:
|
| 944 |
+
|
| 945 |
+
.. math:: p(a,b) = \\sum_{i,j} c_{i,j} * L_i(a) * L_j(b)
|
| 946 |
+
|
| 947 |
+
where the points ``(a, b)`` consist of all pairs formed by taking
|
| 948 |
+
`a` from `x` and `b` from `y`. The resulting points form a grid with
|
| 949 |
+
`x` in the first dimension and `y` in the second.
|
| 950 |
+
|
| 951 |
+
The parameters `x` and `y` are converted to arrays only if they are
|
| 952 |
+
tuples or a lists, otherwise they are treated as a scalars. In either
|
| 953 |
+
case, either `x` and `y` or their elements must support multiplication
|
| 954 |
+
and addition both with themselves and with the elements of `c`.
|
| 955 |
+
|
| 956 |
+
If `c` has fewer than two dimensions, ones are implicitly appended to
|
| 957 |
+
its shape to make it 2-D. The shape of the result will be c.shape[2:] +
|
| 958 |
+
x.shape + y.shape.
|
| 959 |
+
|
| 960 |
+
Parameters
|
| 961 |
+
----------
|
| 962 |
+
x, y : array_like, compatible objects
|
| 963 |
+
The two dimensional series is evaluated at the points in the
|
| 964 |
+
Cartesian product of `x` and `y`. If `x` or `y` is a list or
|
| 965 |
+
tuple, it is first converted to an ndarray, otherwise it is left
|
| 966 |
+
unchanged and, if it isn't an ndarray, it is treated as a scalar.
|
| 967 |
+
c : array_like
|
| 968 |
+
Array of coefficients ordered so that the coefficient of the term of
|
| 969 |
+
multi-degree i,j is contained in ``c[i,j]``. If `c` has dimension
|
| 970 |
+
greater than two the remaining indices enumerate multiple sets of
|
| 971 |
+
coefficients.
|
| 972 |
+
|
| 973 |
+
Returns
|
| 974 |
+
-------
|
| 975 |
+
values : ndarray, compatible object
|
| 976 |
+
The values of the two dimensional Chebyshev series at points in the
|
| 977 |
+
Cartesian product of `x` and `y`.
|
| 978 |
+
|
| 979 |
+
See Also
|
| 980 |
+
--------
|
| 981 |
+
lagval, lagval2d, lagval3d, laggrid3d
|
| 982 |
+
|
| 983 |
+
Examples
|
| 984 |
+
--------
|
| 985 |
+
>>> from numpy.polynomial.laguerre import laggrid2d
|
| 986 |
+
>>> c = [[1, 2], [3, 4]]
|
| 987 |
+
>>> laggrid2d([0, 1], [0, 1], c)
|
| 988 |
+
array([[10., 4.],
|
| 989 |
+
[ 3., 1.]])
|
| 990 |
+
|
| 991 |
+
"""
|
| 992 |
+
return pu._gridnd(lagval, c, x, y)
|
| 993 |
+
|
| 994 |
+
|
| 995 |
+
def lagval3d(x, y, z, c):
|
| 996 |
+
"""
|
| 997 |
+
Evaluate a 3-D Laguerre series at points (x, y, z).
|
| 998 |
+
|
| 999 |
+
This function returns the values:
|
| 1000 |
+
|
| 1001 |
+
.. math:: p(x,y,z) = \\sum_{i,j,k} c_{i,j,k} * L_i(x) * L_j(y) * L_k(z)
|
| 1002 |
+
|
| 1003 |
+
The parameters `x`, `y`, and `z` are converted to arrays only if
|
| 1004 |
+
they are tuples or a lists, otherwise they are treated as a scalars and
|
| 1005 |
+
they must have the same shape after conversion. In either case, either
|
| 1006 |
+
`x`, `y`, and `z` or their elements must support multiplication and
|
| 1007 |
+
addition both with themselves and with the elements of `c`.
|
| 1008 |
+
|
| 1009 |
+
If `c` has fewer than 3 dimensions, ones are implicitly appended to its
|
| 1010 |
+
shape to make it 3-D. The shape of the result will be c.shape[3:] +
|
| 1011 |
+
x.shape.
|
| 1012 |
+
|
| 1013 |
+
Parameters
|
| 1014 |
+
----------
|
| 1015 |
+
x, y, z : array_like, compatible object
|
| 1016 |
+
The three dimensional series is evaluated at the points
|
| 1017 |
+
``(x, y, z)``, where `x`, `y`, and `z` must have the same shape. If
|
| 1018 |
+
any of `x`, `y`, or `z` is a list or tuple, it is first converted
|
| 1019 |
+
to an ndarray, otherwise it is left unchanged and if it isn't an
|
| 1020 |
+
ndarray it is treated as a scalar.
|
| 1021 |
+
c : array_like
|
| 1022 |
+
Array of coefficients ordered so that the coefficient of the term of
|
| 1023 |
+
multi-degree i,j,k is contained in ``c[i,j,k]``. If `c` has dimension
|
| 1024 |
+
greater than 3 the remaining indices enumerate multiple sets of
|
| 1025 |
+
coefficients.
|
| 1026 |
+
|
| 1027 |
+
Returns
|
| 1028 |
+
-------
|
| 1029 |
+
values : ndarray, compatible object
|
| 1030 |
+
The values of the multidimensional polynomial on points formed with
|
| 1031 |
+
triples of corresponding values from `x`, `y`, and `z`.
|
| 1032 |
+
|
| 1033 |
+
See Also
|
| 1034 |
+
--------
|
| 1035 |
+
lagval, lagval2d, laggrid2d, laggrid3d
|
| 1036 |
+
|
| 1037 |
+
Examples
|
| 1038 |
+
--------
|
| 1039 |
+
>>> from numpy.polynomial.laguerre import lagval3d
|
| 1040 |
+
>>> c = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
|
| 1041 |
+
>>> lagval3d(1, 1, 2, c)
|
| 1042 |
+
-1.0
|
| 1043 |
+
|
| 1044 |
+
"""
|
| 1045 |
+
return pu._valnd(lagval, c, x, y, z)
|
| 1046 |
+
|
| 1047 |
+
|
| 1048 |
+
def laggrid3d(x, y, z, c):
|
| 1049 |
+
"""
|
| 1050 |
+
Evaluate a 3-D Laguerre series on the Cartesian product of x, y, and z.
|
| 1051 |
+
|
| 1052 |
+
This function returns the values:
|
| 1053 |
+
|
| 1054 |
+
.. math:: p(a,b,c) = \\sum_{i,j,k} c_{i,j,k} * L_i(a) * L_j(b) * L_k(c)
|
| 1055 |
+
|
| 1056 |
+
where the points ``(a, b, c)`` consist of all triples formed by taking
|
| 1057 |
+
`a` from `x`, `b` from `y`, and `c` from `z`. The resulting points form
|
| 1058 |
+
a grid with `x` in the first dimension, `y` in the second, and `z` in
|
| 1059 |
+
the third.
|
| 1060 |
+
|
| 1061 |
+
The parameters `x`, `y`, and `z` are converted to arrays only if they
|
| 1062 |
+
are tuples or a lists, otherwise they are treated as a scalars. In
|
| 1063 |
+
either case, either `x`, `y`, and `z` or their elements must support
|
| 1064 |
+
multiplication and addition both with themselves and with the elements
|
| 1065 |
+
of `c`.
|
| 1066 |
+
|
| 1067 |
+
If `c` has fewer than three dimensions, ones are implicitly appended to
|
| 1068 |
+
its shape to make it 3-D. The shape of the result will be c.shape[3:] +
|
| 1069 |
+
x.shape + y.shape + z.shape.
|
| 1070 |
+
|
| 1071 |
+
Parameters
|
| 1072 |
+
----------
|
| 1073 |
+
x, y, z : array_like, compatible objects
|
| 1074 |
+
The three dimensional series is evaluated at the points in the
|
| 1075 |
+
Cartesian product of `x`, `y`, and `z`. If `x`, `y`, or `z` is a
|
| 1076 |
+
list or tuple, it is first converted to an ndarray, otherwise it is
|
| 1077 |
+
left unchanged and, if it isn't an ndarray, it is treated as a
|
| 1078 |
+
scalar.
|
| 1079 |
+
c : array_like
|
| 1080 |
+
Array of coefficients ordered so that the coefficients for terms of
|
| 1081 |
+
degree i,j are contained in ``c[i,j]``. If `c` has dimension
|
| 1082 |
+
greater than two the remaining indices enumerate multiple sets of
|
| 1083 |
+
coefficients.
|
| 1084 |
+
|
| 1085 |
+
Returns
|
| 1086 |
+
-------
|
| 1087 |
+
values : ndarray, compatible object
|
| 1088 |
+
The values of the two dimensional polynomial at points in the Cartesian
|
| 1089 |
+
product of `x` and `y`.
|
| 1090 |
+
|
| 1091 |
+
See Also
|
| 1092 |
+
--------
|
| 1093 |
+
lagval, lagval2d, laggrid2d, lagval3d
|
| 1094 |
+
|
| 1095 |
+
Examples
|
| 1096 |
+
--------
|
| 1097 |
+
>>> from numpy.polynomial.laguerre import laggrid3d
|
| 1098 |
+
>>> c = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
|
| 1099 |
+
>>> laggrid3d([0, 1], [0, 1], [2, 4], c)
|
| 1100 |
+
array([[[ -4., -44.],
|
| 1101 |
+
[ -2., -18.]],
|
| 1102 |
+
[[ -2., -14.],
|
| 1103 |
+
[ -1., -5.]]])
|
| 1104 |
+
|
| 1105 |
+
"""
|
| 1106 |
+
return pu._gridnd(lagval, c, x, y, z)
|
| 1107 |
+
|
| 1108 |
+
|
| 1109 |
+
def lagvander(x, deg):
|
| 1110 |
+
"""Pseudo-Vandermonde matrix of given degree.
|
| 1111 |
+
|
| 1112 |
+
Returns the pseudo-Vandermonde matrix of degree `deg` and sample points
|
| 1113 |
+
`x`. The pseudo-Vandermonde matrix is defined by
|
| 1114 |
+
|
| 1115 |
+
.. math:: V[..., i] = L_i(x)
|
| 1116 |
+
|
| 1117 |
+
where ``0 <= i <= deg``. The leading indices of `V` index the elements of
|
| 1118 |
+
`x` and the last index is the degree of the Laguerre polynomial.
|
| 1119 |
+
|
| 1120 |
+
If `c` is a 1-D array of coefficients of length ``n + 1`` and `V` is the
|
| 1121 |
+
array ``V = lagvander(x, n)``, then ``np.dot(V, c)`` and
|
| 1122 |
+
``lagval(x, c)`` are the same up to roundoff. This equivalence is
|
| 1123 |
+
useful both for least squares fitting and for the evaluation of a large
|
| 1124 |
+
number of Laguerre series of the same degree and sample points.
|
| 1125 |
+
|
| 1126 |
+
Parameters
|
| 1127 |
+
----------
|
| 1128 |
+
x : array_like
|
| 1129 |
+
Array of points. The dtype is converted to float64 or complex128
|
| 1130 |
+
depending on whether any of the elements are complex. If `x` is
|
| 1131 |
+
scalar it is converted to a 1-D array.
|
| 1132 |
+
deg : int
|
| 1133 |
+
Degree of the resulting matrix.
|
| 1134 |
+
|
| 1135 |
+
Returns
|
| 1136 |
+
-------
|
| 1137 |
+
vander : ndarray
|
| 1138 |
+
The pseudo-Vandermonde matrix. The shape of the returned matrix is
|
| 1139 |
+
``x.shape + (deg + 1,)``, where The last index is the degree of the
|
| 1140 |
+
corresponding Laguerre polynomial. The dtype will be the same as
|
| 1141 |
+
the converted `x`.
|
| 1142 |
+
|
| 1143 |
+
Examples
|
| 1144 |
+
--------
|
| 1145 |
+
>>> import numpy as np
|
| 1146 |
+
>>> from numpy.polynomial.laguerre import lagvander
|
| 1147 |
+
>>> x = np.array([0, 1, 2])
|
| 1148 |
+
>>> lagvander(x, 3)
|
| 1149 |
+
array([[ 1. , 1. , 1. , 1. ],
|
| 1150 |
+
[ 1. , 0. , -0.5 , -0.66666667],
|
| 1151 |
+
[ 1. , -1. , -1. , -0.33333333]])
|
| 1152 |
+
|
| 1153 |
+
"""
|
| 1154 |
+
ideg = pu._as_int(deg, "deg")
|
| 1155 |
+
if ideg < 0:
|
| 1156 |
+
raise ValueError("deg must be non-negative")
|
| 1157 |
+
|
| 1158 |
+
x = np.array(x, copy=None, ndmin=1) + 0.0
|
| 1159 |
+
dims = (ideg + 1,) + x.shape
|
| 1160 |
+
dtyp = x.dtype
|
| 1161 |
+
v = np.empty(dims, dtype=dtyp)
|
| 1162 |
+
v[0] = x * 0 + 1
|
| 1163 |
+
if ideg > 0:
|
| 1164 |
+
v[1] = 1 - x
|
| 1165 |
+
for i in range(2, ideg + 1):
|
| 1166 |
+
v[i] = (v[i - 1] * (2 * i - 1 - x) - v[i - 2] * (i - 1)) / i
|
| 1167 |
+
return np.moveaxis(v, 0, -1)
|
| 1168 |
+
|
| 1169 |
+
|
| 1170 |
+
def lagvander2d(x, y, deg):
|
| 1171 |
+
"""Pseudo-Vandermonde matrix of given degrees.
|
| 1172 |
+
|
| 1173 |
+
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample
|
| 1174 |
+
points ``(x, y)``. The pseudo-Vandermonde matrix is defined by
|
| 1175 |
+
|
| 1176 |
+
.. math:: V[..., (deg[1] + 1)*i + j] = L_i(x) * L_j(y),
|
| 1177 |
+
|
| 1178 |
+
where ``0 <= i <= deg[0]`` and ``0 <= j <= deg[1]``. The leading indices of
|
| 1179 |
+
`V` index the points ``(x, y)`` and the last index encodes the degrees of
|
| 1180 |
+
the Laguerre polynomials.
|
| 1181 |
+
|
| 1182 |
+
If ``V = lagvander2d(x, y, [xdeg, ydeg])``, then the columns of `V`
|
| 1183 |
+
correspond to the elements of a 2-D coefficient array `c` of shape
|
| 1184 |
+
(xdeg + 1, ydeg + 1) in the order
|
| 1185 |
+
|
| 1186 |
+
.. math:: c_{00}, c_{01}, c_{02}, ... , c_{10}, c_{11}, c_{12}, ...
|
| 1187 |
+
|
| 1188 |
+
and ``np.dot(V, c.flat)`` and ``lagval2d(x, y, c)`` will be the same
|
| 1189 |
+
up to roundoff. This equivalence is useful both for least squares
|
| 1190 |
+
fitting and for the evaluation of a large number of 2-D Laguerre
|
| 1191 |
+
series of the same degrees and sample points.
|
| 1192 |
+
|
| 1193 |
+
Parameters
|
| 1194 |
+
----------
|
| 1195 |
+
x, y : array_like
|
| 1196 |
+
Arrays of point coordinates, all of the same shape. The dtypes
|
| 1197 |
+
will be converted to either float64 or complex128 depending on
|
| 1198 |
+
whether any of the elements are complex. Scalars are converted to
|
| 1199 |
+
1-D arrays.
|
| 1200 |
+
deg : list of ints
|
| 1201 |
+
List of maximum degrees of the form [x_deg, y_deg].
|
| 1202 |
+
|
| 1203 |
+
Returns
|
| 1204 |
+
-------
|
| 1205 |
+
vander2d : ndarray
|
| 1206 |
+
The shape of the returned matrix is ``x.shape + (order,)``, where
|
| 1207 |
+
:math:`order = (deg[0]+1)*(deg[1]+1)`. The dtype will be the same
|
| 1208 |
+
as the converted `x` and `y`.
|
| 1209 |
+
|
| 1210 |
+
See Also
|
| 1211 |
+
--------
|
| 1212 |
+
lagvander, lagvander3d, lagval2d, lagval3d
|
| 1213 |
+
|
| 1214 |
+
Examples
|
| 1215 |
+
--------
|
| 1216 |
+
>>> import numpy as np
|
| 1217 |
+
>>> from numpy.polynomial.laguerre import lagvander2d
|
| 1218 |
+
>>> x = np.array([0])
|
| 1219 |
+
>>> y = np.array([2])
|
| 1220 |
+
>>> lagvander2d(x, y, [2, 1])
|
| 1221 |
+
array([[ 1., -1., 1., -1., 1., -1.]])
|
| 1222 |
+
|
| 1223 |
+
"""
|
| 1224 |
+
return pu._vander_nd_flat((lagvander, lagvander), (x, y), deg)
|
| 1225 |
+
|
| 1226 |
+
|
| 1227 |
+
def lagvander3d(x, y, z, deg):
|
| 1228 |
+
"""Pseudo-Vandermonde matrix of given degrees.
|
| 1229 |
+
|
| 1230 |
+
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample
|
| 1231 |
+
points ``(x, y, z)``. If `l`, `m`, `n` are the given degrees in `x`, `y`, `z`,
|
| 1232 |
+
then The pseudo-Vandermonde matrix is defined by
|
| 1233 |
+
|
| 1234 |
+
.. math:: V[..., (m+1)(n+1)i + (n+1)j + k] = L_i(x)*L_j(y)*L_k(z),
|
| 1235 |
+
|
| 1236 |
+
where ``0 <= i <= l``, ``0 <= j <= m``, and ``0 <= j <= n``. The leading
|
| 1237 |
+
indices of `V` index the points ``(x, y, z)`` and the last index encodes
|
| 1238 |
+
the degrees of the Laguerre polynomials.
|
| 1239 |
+
|
| 1240 |
+
If ``V = lagvander3d(x, y, z, [xdeg, ydeg, zdeg])``, then the columns
|
| 1241 |
+
of `V` correspond to the elements of a 3-D coefficient array `c` of
|
| 1242 |
+
shape (xdeg + 1, ydeg + 1, zdeg + 1) in the order
|
| 1243 |
+
|
| 1244 |
+
.. math:: c_{000}, c_{001}, c_{002},... , c_{010}, c_{011}, c_{012},...
|
| 1245 |
+
|
| 1246 |
+
and ``np.dot(V, c.flat)`` and ``lagval3d(x, y, z, c)`` will be the
|
| 1247 |
+
same up to roundoff. This equivalence is useful both for least squares
|
| 1248 |
+
fitting and for the evaluation of a large number of 3-D Laguerre
|
| 1249 |
+
series of the same degrees and sample points.
|
| 1250 |
+
|
| 1251 |
+
Parameters
|
| 1252 |
+
----------
|
| 1253 |
+
x, y, z : array_like
|
| 1254 |
+
Arrays of point coordinates, all of the same shape. The dtypes will
|
| 1255 |
+
be converted to either float64 or complex128 depending on whether
|
| 1256 |
+
any of the elements are complex. Scalars are converted to 1-D
|
| 1257 |
+
arrays.
|
| 1258 |
+
deg : list of ints
|
| 1259 |
+
List of maximum degrees of the form [x_deg, y_deg, z_deg].
|
| 1260 |
+
|
| 1261 |
+
Returns
|
| 1262 |
+
-------
|
| 1263 |
+
vander3d : ndarray
|
| 1264 |
+
The shape of the returned matrix is ``x.shape + (order,)``, where
|
| 1265 |
+
:math:`order = (deg[0]+1)*(deg[1]+1)*(deg[2]+1)`. The dtype will
|
| 1266 |
+
be the same as the converted `x`, `y`, and `z`.
|
| 1267 |
+
|
| 1268 |
+
See Also
|
| 1269 |
+
--------
|
| 1270 |
+
lagvander, lagvander3d, lagval2d, lagval3d
|
| 1271 |
+
|
| 1272 |
+
Examples
|
| 1273 |
+
--------
|
| 1274 |
+
>>> import numpy as np
|
| 1275 |
+
>>> from numpy.polynomial.laguerre import lagvander3d
|
| 1276 |
+
>>> x = np.array([0])
|
| 1277 |
+
>>> y = np.array([2])
|
| 1278 |
+
>>> z = np.array([0])
|
| 1279 |
+
>>> lagvander3d(x, y, z, [2, 1, 3])
|
| 1280 |
+
array([[ 1., 1., 1., 1., -1., -1., -1., -1., 1., 1., 1., 1., -1.,
|
| 1281 |
+
-1., -1., -1., 1., 1., 1., 1., -1., -1., -1., -1.]])
|
| 1282 |
+
|
| 1283 |
+
"""
|
| 1284 |
+
return pu._vander_nd_flat((lagvander, lagvander, lagvander), (x, y, z), deg)
|
| 1285 |
+
|
| 1286 |
+
|
| 1287 |
+
def lagfit(x, y, deg, rcond=None, full=False, w=None):
|
| 1288 |
+
"""
|
| 1289 |
+
Least squares fit of Laguerre series to data.
|
| 1290 |
+
|
| 1291 |
+
Return the coefficients of a Laguerre series of degree `deg` that is the
|
| 1292 |
+
least squares fit to the data values `y` given at points `x`. If `y` is
|
| 1293 |
+
1-D the returned coefficients will also be 1-D. If `y` is 2-D multiple
|
| 1294 |
+
fits are done, one for each column of `y`, and the resulting
|
| 1295 |
+
coefficients are stored in the corresponding columns of a 2-D return.
|
| 1296 |
+
The fitted polynomial(s) are in the form
|
| 1297 |
+
|
| 1298 |
+
.. math:: p(x) = c_0 + c_1 * L_1(x) + ... + c_n * L_n(x),
|
| 1299 |
+
|
| 1300 |
+
where ``n`` is `deg`.
|
| 1301 |
+
|
| 1302 |
+
Parameters
|
| 1303 |
+
----------
|
| 1304 |
+
x : array_like, shape (M,)
|
| 1305 |
+
x-coordinates of the M sample points ``(x[i], y[i])``.
|
| 1306 |
+
y : array_like, shape (M,) or (M, K)
|
| 1307 |
+
y-coordinates of the sample points. Several data sets of sample
|
| 1308 |
+
points sharing the same x-coordinates can be fitted at once by
|
| 1309 |
+
passing in a 2D-array that contains one dataset per column.
|
| 1310 |
+
deg : int or 1-D array_like
|
| 1311 |
+
Degree(s) of the fitting polynomials. If `deg` is a single integer
|
| 1312 |
+
all terms up to and including the `deg`'th term are included in the
|
| 1313 |
+
fit. For NumPy versions >= 1.11.0 a list of integers specifying the
|
| 1314 |
+
degrees of the terms to include may be used instead.
|
| 1315 |
+
rcond : float, optional
|
| 1316 |
+
Relative condition number of the fit. Singular values smaller than
|
| 1317 |
+
this relative to the largest singular value will be ignored. The
|
| 1318 |
+
default value is len(x)*eps, where eps is the relative precision of
|
| 1319 |
+
the float type, about 2e-16 in most cases.
|
| 1320 |
+
full : bool, optional
|
| 1321 |
+
Switch determining nature of return value. When it is False (the
|
| 1322 |
+
default) just the coefficients are returned, when True diagnostic
|
| 1323 |
+
information from the singular value decomposition is also returned.
|
| 1324 |
+
w : array_like, shape (`M`,), optional
|
| 1325 |
+
Weights. If not None, the weight ``w[i]`` applies to the unsquared
|
| 1326 |
+
residual ``y[i] - y_hat[i]`` at ``x[i]``. Ideally the weights are
|
| 1327 |
+
chosen so that the errors of the products ``w[i]*y[i]`` all have the
|
| 1328 |
+
same variance. When using inverse-variance weighting, use
|
| 1329 |
+
``w[i] = 1/sigma(y[i])``. The default value is None.
|
| 1330 |
+
|
| 1331 |
+
Returns
|
| 1332 |
+
-------
|
| 1333 |
+
coef : ndarray, shape (M,) or (M, K)
|
| 1334 |
+
Laguerre coefficients ordered from low to high. If `y` was 2-D,
|
| 1335 |
+
the coefficients for the data in column *k* of `y` are in column
|
| 1336 |
+
*k*.
|
| 1337 |
+
|
| 1338 |
+
[residuals, rank, singular_values, rcond] : list
|
| 1339 |
+
These values are only returned if ``full == True``
|
| 1340 |
+
|
| 1341 |
+
- residuals -- sum of squared residuals of the least squares fit
|
| 1342 |
+
- rank -- the numerical rank of the scaled Vandermonde matrix
|
| 1343 |
+
- singular_values -- singular values of the scaled Vandermonde matrix
|
| 1344 |
+
- rcond -- value of `rcond`.
|
| 1345 |
+
|
| 1346 |
+
For more details, see `numpy.linalg.lstsq`.
|
| 1347 |
+
|
| 1348 |
+
Warns
|
| 1349 |
+
-----
|
| 1350 |
+
RankWarning
|
| 1351 |
+
The rank of the coefficient matrix in the least-squares fit is
|
| 1352 |
+
deficient. The warning is only raised if ``full == False``. The
|
| 1353 |
+
warnings can be turned off by
|
| 1354 |
+
|
| 1355 |
+
>>> import warnings
|
| 1356 |
+
>>> warnings.simplefilter('ignore', np.exceptions.RankWarning)
|
| 1357 |
+
|
| 1358 |
+
See Also
|
| 1359 |
+
--------
|
| 1360 |
+
numpy.polynomial.polynomial.polyfit
|
| 1361 |
+
numpy.polynomial.legendre.legfit
|
| 1362 |
+
numpy.polynomial.chebyshev.chebfit
|
| 1363 |
+
numpy.polynomial.hermite.hermfit
|
| 1364 |
+
numpy.polynomial.hermite_e.hermefit
|
| 1365 |
+
lagval : Evaluates a Laguerre series.
|
| 1366 |
+
lagvander : pseudo Vandermonde matrix of Laguerre series.
|
| 1367 |
+
lagweight : Laguerre weight function.
|
| 1368 |
+
numpy.linalg.lstsq : Computes a least-squares fit from the matrix.
|
| 1369 |
+
scipy.interpolate.UnivariateSpline : Computes spline fits.
|
| 1370 |
+
|
| 1371 |
+
Notes
|
| 1372 |
+
-----
|
| 1373 |
+
The solution is the coefficients of the Laguerre series ``p`` that
|
| 1374 |
+
minimizes the sum of the weighted squared errors
|
| 1375 |
+
|
| 1376 |
+
.. math:: E = \\sum_j w_j^2 * |y_j - p(x_j)|^2,
|
| 1377 |
+
|
| 1378 |
+
where the :math:`w_j` are the weights. This problem is solved by
|
| 1379 |
+
setting up as the (typically) overdetermined matrix equation
|
| 1380 |
+
|
| 1381 |
+
.. math:: V(x) * c = w * y,
|
| 1382 |
+
|
| 1383 |
+
where ``V`` is the weighted pseudo Vandermonde matrix of `x`, ``c`` are the
|
| 1384 |
+
coefficients to be solved for, `w` are the weights, and `y` are the
|
| 1385 |
+
observed values. This equation is then solved using the singular value
|
| 1386 |
+
decomposition of ``V``.
|
| 1387 |
+
|
| 1388 |
+
If some of the singular values of `V` are so small that they are
|
| 1389 |
+
neglected, then a `~exceptions.RankWarning` will be issued. This means that
|
| 1390 |
+
the coefficient values may be poorly determined. Using a lower order fit
|
| 1391 |
+
will usually get rid of the warning. The `rcond` parameter can also be
|
| 1392 |
+
set to a value smaller than its default, but the resulting fit may be
|
| 1393 |
+
spurious and have large contributions from roundoff error.
|
| 1394 |
+
|
| 1395 |
+
Fits using Laguerre series are probably most useful when the data can
|
| 1396 |
+
be approximated by ``sqrt(w(x)) * p(x)``, where ``w(x)`` is the Laguerre
|
| 1397 |
+
weight. In that case the weight ``sqrt(w(x[i]))`` should be used
|
| 1398 |
+
together with data values ``y[i]/sqrt(w(x[i]))``. The weight function is
|
| 1399 |
+
available as `lagweight`.
|
| 1400 |
+
|
| 1401 |
+
References
|
| 1402 |
+
----------
|
| 1403 |
+
.. [1] Wikipedia, "Curve fitting",
|
| 1404 |
+
https://en.wikipedia.org/wiki/Curve_fitting
|
| 1405 |
+
|
| 1406 |
+
Examples
|
| 1407 |
+
--------
|
| 1408 |
+
>>> import numpy as np
|
| 1409 |
+
>>> from numpy.polynomial.laguerre import lagfit, lagval
|
| 1410 |
+
>>> x = np.linspace(0, 10)
|
| 1411 |
+
>>> rng = np.random.default_rng()
|
| 1412 |
+
>>> err = rng.normal(scale=1./10, size=len(x))
|
| 1413 |
+
>>> y = lagval(x, [1, 2, 3]) + err
|
| 1414 |
+
>>> lagfit(x, y, 2)
|
| 1415 |
+
array([1.00578369, 1.99417356, 2.99827656]) # may vary
|
| 1416 |
+
|
| 1417 |
+
"""
|
| 1418 |
+
return pu._fit(lagvander, x, y, deg, rcond, full, w)
|
| 1419 |
+
|
| 1420 |
+
|
| 1421 |
+
def lagcompanion(c):
|
| 1422 |
+
"""
|
| 1423 |
+
Return the companion matrix of c.
|
| 1424 |
+
|
| 1425 |
+
The usual companion matrix of the Laguerre polynomials is already
|
| 1426 |
+
symmetric when `c` is a basis Laguerre polynomial, so no scaling is
|
| 1427 |
+
applied.
|
| 1428 |
+
|
| 1429 |
+
Parameters
|
| 1430 |
+
----------
|
| 1431 |
+
c : array_like
|
| 1432 |
+
1-D array of Laguerre series coefficients ordered from low to high
|
| 1433 |
+
degree.
|
| 1434 |
+
|
| 1435 |
+
Returns
|
| 1436 |
+
-------
|
| 1437 |
+
mat : ndarray
|
| 1438 |
+
Companion matrix of dimensions (deg, deg).
|
| 1439 |
+
|
| 1440 |
+
Examples
|
| 1441 |
+
--------
|
| 1442 |
+
>>> from numpy.polynomial.laguerre import lagcompanion
|
| 1443 |
+
>>> lagcompanion([1, 2, 3])
|
| 1444 |
+
array([[ 1. , -0.33333333],
|
| 1445 |
+
[-1. , 4.33333333]])
|
| 1446 |
+
|
| 1447 |
+
"""
|
| 1448 |
+
# c is a trimmed copy
|
| 1449 |
+
[c] = pu.as_series([c])
|
| 1450 |
+
if len(c) < 2:
|
| 1451 |
+
raise ValueError('Series must have maximum degree of at least 1.')
|
| 1452 |
+
if len(c) == 2:
|
| 1453 |
+
return np.array([[1 + c[0] / c[1]]])
|
| 1454 |
+
|
| 1455 |
+
n = len(c) - 1
|
| 1456 |
+
mat = np.zeros((n, n), dtype=c.dtype)
|
| 1457 |
+
top = mat.reshape(-1)[1::n + 1]
|
| 1458 |
+
mid = mat.reshape(-1)[0::n + 1]
|
| 1459 |
+
bot = mat.reshape(-1)[n::n + 1]
|
| 1460 |
+
top[...] = -np.arange(1, n)
|
| 1461 |
+
mid[...] = 2. * np.arange(n) + 1.
|
| 1462 |
+
bot[...] = top
|
| 1463 |
+
mat[:, -1] += (c[:-1] / c[-1]) * n
|
| 1464 |
+
return mat
|
| 1465 |
+
|
| 1466 |
+
|
| 1467 |
+
def lagroots(c):
|
| 1468 |
+
"""
|
| 1469 |
+
Compute the roots of a Laguerre series.
|
| 1470 |
+
|
| 1471 |
+
Return the roots (a.k.a. "zeros") of the polynomial
|
| 1472 |
+
|
| 1473 |
+
.. math:: p(x) = \\sum_i c[i] * L_i(x).
|
| 1474 |
+
|
| 1475 |
+
Parameters
|
| 1476 |
+
----------
|
| 1477 |
+
c : 1-D array_like
|
| 1478 |
+
1-D array of coefficients.
|
| 1479 |
+
|
| 1480 |
+
Returns
|
| 1481 |
+
-------
|
| 1482 |
+
out : ndarray
|
| 1483 |
+
Array of the roots of the series. If all the roots are real,
|
| 1484 |
+
then `out` is also real, otherwise it is complex.
|
| 1485 |
+
|
| 1486 |
+
See Also
|
| 1487 |
+
--------
|
| 1488 |
+
numpy.polynomial.polynomial.polyroots
|
| 1489 |
+
numpy.polynomial.legendre.legroots
|
| 1490 |
+
numpy.polynomial.chebyshev.chebroots
|
| 1491 |
+
numpy.polynomial.hermite.hermroots
|
| 1492 |
+
numpy.polynomial.hermite_e.hermeroots
|
| 1493 |
+
|
| 1494 |
+
Notes
|
| 1495 |
+
-----
|
| 1496 |
+
The root estimates are obtained as the eigenvalues of the companion
|
| 1497 |
+
matrix, Roots far from the origin of the complex plane may have large
|
| 1498 |
+
errors due to the numerical instability of the series for such
|
| 1499 |
+
values. Roots with multiplicity greater than 1 will also show larger
|
| 1500 |
+
errors as the value of the series near such points is relatively
|
| 1501 |
+
insensitive to errors in the roots. Isolated roots near the origin can
|
| 1502 |
+
be improved by a few iterations of Newton's method.
|
| 1503 |
+
|
| 1504 |
+
The Laguerre series basis polynomials aren't powers of `x` so the
|
| 1505 |
+
results of this function may seem unintuitive.
|
| 1506 |
+
|
| 1507 |
+
Examples
|
| 1508 |
+
--------
|
| 1509 |
+
>>> from numpy.polynomial.laguerre import lagroots, lagfromroots
|
| 1510 |
+
>>> coef = lagfromroots([0, 1, 2])
|
| 1511 |
+
>>> coef
|
| 1512 |
+
array([ 2., -8., 12., -6.])
|
| 1513 |
+
>>> lagroots(coef)
|
| 1514 |
+
array([-4.4408921e-16, 1.0000000e+00, 2.0000000e+00])
|
| 1515 |
+
|
| 1516 |
+
"""
|
| 1517 |
+
# c is a trimmed copy
|
| 1518 |
+
[c] = pu.as_series([c])
|
| 1519 |
+
if len(c) <= 1:
|
| 1520 |
+
return np.array([], dtype=c.dtype)
|
| 1521 |
+
if len(c) == 2:
|
| 1522 |
+
return np.array([1 + c[0] / c[1]])
|
| 1523 |
+
|
| 1524 |
+
# rotated companion matrix reduces error
|
| 1525 |
+
m = lagcompanion(c)[::-1, ::-1]
|
| 1526 |
+
r = np.linalg.eigvals(m)
|
| 1527 |
+
r.sort()
|
| 1528 |
+
return r
|
| 1529 |
+
|
| 1530 |
+
|
| 1531 |
+
def laggauss(deg):
|
| 1532 |
+
"""
|
| 1533 |
+
Gauss-Laguerre quadrature.
|
| 1534 |
+
|
| 1535 |
+
Computes the sample points and weights for Gauss-Laguerre quadrature.
|
| 1536 |
+
These sample points and weights will correctly integrate polynomials of
|
| 1537 |
+
degree :math:`2*deg - 1` or less over the interval :math:`[0, \\inf]`
|
| 1538 |
+
with the weight function :math:`f(x) = \\exp(-x)`.
|
| 1539 |
+
|
| 1540 |
+
Parameters
|
| 1541 |
+
----------
|
| 1542 |
+
deg : int
|
| 1543 |
+
Number of sample points and weights. It must be >= 1.
|
| 1544 |
+
|
| 1545 |
+
Returns
|
| 1546 |
+
-------
|
| 1547 |
+
x : ndarray
|
| 1548 |
+
1-D ndarray containing the sample points.
|
| 1549 |
+
y : ndarray
|
| 1550 |
+
1-D ndarray containing the weights.
|
| 1551 |
+
|
| 1552 |
+
Notes
|
| 1553 |
+
-----
|
| 1554 |
+
The results have only been tested up to degree 100 higher degrees may
|
| 1555 |
+
be problematic. The weights are determined by using the fact that
|
| 1556 |
+
|
| 1557 |
+
.. math:: w_k = c / (L'_n(x_k) * L_{n-1}(x_k))
|
| 1558 |
+
|
| 1559 |
+
where :math:`c` is a constant independent of :math:`k` and :math:`x_k`
|
| 1560 |
+
is the k'th root of :math:`L_n`, and then scaling the results to get
|
| 1561 |
+
the right value when integrating 1.
|
| 1562 |
+
|
| 1563 |
+
Examples
|
| 1564 |
+
--------
|
| 1565 |
+
>>> from numpy.polynomial.laguerre import laggauss
|
| 1566 |
+
>>> laggauss(2)
|
| 1567 |
+
(array([0.58578644, 3.41421356]), array([0.85355339, 0.14644661]))
|
| 1568 |
+
|
| 1569 |
+
"""
|
| 1570 |
+
ideg = pu._as_int(deg, "deg")
|
| 1571 |
+
if ideg <= 0:
|
| 1572 |
+
raise ValueError("deg must be a positive integer")
|
| 1573 |
+
|
| 1574 |
+
# first approximation of roots. We use the fact that the companion
|
| 1575 |
+
# matrix is symmetric in this case in order to obtain better zeros.
|
| 1576 |
+
c = np.array([0] * deg + [1])
|
| 1577 |
+
m = lagcompanion(c)
|
| 1578 |
+
x = np.linalg.eigvalsh(m)
|
| 1579 |
+
|
| 1580 |
+
# improve roots by one application of Newton
|
| 1581 |
+
dy = lagval(x, c)
|
| 1582 |
+
df = lagval(x, lagder(c))
|
| 1583 |
+
x -= dy / df
|
| 1584 |
+
|
| 1585 |
+
# compute the weights. We scale the factor to avoid possible numerical
|
| 1586 |
+
# overflow.
|
| 1587 |
+
fm = lagval(x, c[1:])
|
| 1588 |
+
fm /= np.abs(fm).max()
|
| 1589 |
+
df /= np.abs(df).max()
|
| 1590 |
+
w = 1 / (fm * df)
|
| 1591 |
+
|
| 1592 |
+
# scale w to get the right value, 1 in this case
|
| 1593 |
+
w /= w.sum()
|
| 1594 |
+
|
| 1595 |
+
return x, w
|
| 1596 |
+
|
| 1597 |
+
|
| 1598 |
+
def lagweight(x):
|
| 1599 |
+
"""Weight function of the Laguerre polynomials.
|
| 1600 |
+
|
| 1601 |
+
The weight function is :math:`exp(-x)` and the interval of integration
|
| 1602 |
+
is :math:`[0, \\inf]`. The Laguerre polynomials are orthogonal, but not
|
| 1603 |
+
normalized, with respect to this weight function.
|
| 1604 |
+
|
| 1605 |
+
Parameters
|
| 1606 |
+
----------
|
| 1607 |
+
x : array_like
|
| 1608 |
+
Values at which the weight function will be computed.
|
| 1609 |
+
|
| 1610 |
+
Returns
|
| 1611 |
+
-------
|
| 1612 |
+
w : ndarray
|
| 1613 |
+
The weight function at `x`.
|
| 1614 |
+
|
| 1615 |
+
Examples
|
| 1616 |
+
--------
|
| 1617 |
+
>>> from numpy.polynomial.laguerre import lagweight
|
| 1618 |
+
>>> x = np.array([0, 1, 2])
|
| 1619 |
+
>>> lagweight(x)
|
| 1620 |
+
array([1. , 0.36787944, 0.13533528])
|
| 1621 |
+
|
| 1622 |
+
"""
|
| 1623 |
+
w = np.exp(-x)
|
| 1624 |
+
return w
|
| 1625 |
+
|
| 1626 |
+
#
|
| 1627 |
+
# Laguerre series class
|
| 1628 |
+
#
|
| 1629 |
+
|
| 1630 |
+
class Laguerre(ABCPolyBase):
|
| 1631 |
+
"""A Laguerre series class.
|
| 1632 |
+
|
| 1633 |
+
The Laguerre class provides the standard Python numerical methods
|
| 1634 |
+
'+', '-', '*', '//', '%', 'divmod', '**', and '()' as well as the
|
| 1635 |
+
attributes and methods listed below.
|
| 1636 |
+
|
| 1637 |
+
Parameters
|
| 1638 |
+
----------
|
| 1639 |
+
coef : array_like
|
| 1640 |
+
Laguerre coefficients in order of increasing degree, i.e,
|
| 1641 |
+
``(1, 2, 3)`` gives ``1*L_0(x) + 2*L_1(X) + 3*L_2(x)``.
|
| 1642 |
+
domain : (2,) array_like, optional
|
| 1643 |
+
Domain to use. The interval ``[domain[0], domain[1]]`` is mapped
|
| 1644 |
+
to the interval ``[window[0], window[1]]`` by shifting and scaling.
|
| 1645 |
+
The default value is [0., 1.].
|
| 1646 |
+
window : (2,) array_like, optional
|
| 1647 |
+
Window, see `domain` for its use. The default value is [0., 1.].
|
| 1648 |
+
symbol : str, optional
|
| 1649 |
+
Symbol used to represent the independent variable in string
|
| 1650 |
+
representations of the polynomial expression, e.g. for printing.
|
| 1651 |
+
The symbol must be a valid Python identifier. Default value is 'x'.
|
| 1652 |
+
|
| 1653 |
+
.. versionadded:: 1.24
|
| 1654 |
+
|
| 1655 |
+
"""
|
| 1656 |
+
# Virtual Functions
|
| 1657 |
+
_add = staticmethod(lagadd)
|
| 1658 |
+
_sub = staticmethod(lagsub)
|
| 1659 |
+
_mul = staticmethod(lagmul)
|
| 1660 |
+
_div = staticmethod(lagdiv)
|
| 1661 |
+
_pow = staticmethod(lagpow)
|
| 1662 |
+
_val = staticmethod(lagval)
|
| 1663 |
+
_int = staticmethod(lagint)
|
| 1664 |
+
_der = staticmethod(lagder)
|
| 1665 |
+
_fit = staticmethod(lagfit)
|
| 1666 |
+
_line = staticmethod(lagline)
|
| 1667 |
+
_roots = staticmethod(lagroots)
|
| 1668 |
+
_fromroots = staticmethod(lagfromroots)
|
| 1669 |
+
|
| 1670 |
+
# Virtual properties
|
| 1671 |
+
domain = np.array(lagdomain)
|
| 1672 |
+
window = np.array(lagdomain)
|
| 1673 |
+
basis_name = 'L'
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/laguerre.pyi
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, ClassVar, Final, Literal as L
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
from ._polybase import ABCPolyBase
|
| 6 |
+
from ._polytypes import (
|
| 7 |
+
_Array1,
|
| 8 |
+
_Array2,
|
| 9 |
+
_FuncBinOp,
|
| 10 |
+
_FuncCompanion,
|
| 11 |
+
_FuncDer,
|
| 12 |
+
_FuncFit,
|
| 13 |
+
_FuncFromRoots,
|
| 14 |
+
_FuncGauss,
|
| 15 |
+
_FuncInteg,
|
| 16 |
+
_FuncLine,
|
| 17 |
+
_FuncPoly2Ortho,
|
| 18 |
+
_FuncPow,
|
| 19 |
+
_FuncRoots,
|
| 20 |
+
_FuncUnOp,
|
| 21 |
+
_FuncVal,
|
| 22 |
+
_FuncVal2D,
|
| 23 |
+
_FuncVal3D,
|
| 24 |
+
_FuncVander,
|
| 25 |
+
_FuncVander2D,
|
| 26 |
+
_FuncVander3D,
|
| 27 |
+
_FuncWeight,
|
| 28 |
+
)
|
| 29 |
+
from .polyutils import trimcoef as lagtrim
|
| 30 |
+
|
| 31 |
+
__all__ = [
|
| 32 |
+
"lagzero",
|
| 33 |
+
"lagone",
|
| 34 |
+
"lagx",
|
| 35 |
+
"lagdomain",
|
| 36 |
+
"lagline",
|
| 37 |
+
"lagadd",
|
| 38 |
+
"lagsub",
|
| 39 |
+
"lagmulx",
|
| 40 |
+
"lagmul",
|
| 41 |
+
"lagdiv",
|
| 42 |
+
"lagpow",
|
| 43 |
+
"lagval",
|
| 44 |
+
"lagder",
|
| 45 |
+
"lagint",
|
| 46 |
+
"lag2poly",
|
| 47 |
+
"poly2lag",
|
| 48 |
+
"lagfromroots",
|
| 49 |
+
"lagvander",
|
| 50 |
+
"lagfit",
|
| 51 |
+
"lagtrim",
|
| 52 |
+
"lagroots",
|
| 53 |
+
"Laguerre",
|
| 54 |
+
"lagval2d",
|
| 55 |
+
"lagval3d",
|
| 56 |
+
"laggrid2d",
|
| 57 |
+
"laggrid3d",
|
| 58 |
+
"lagvander2d",
|
| 59 |
+
"lagvander3d",
|
| 60 |
+
"lagcompanion",
|
| 61 |
+
"laggauss",
|
| 62 |
+
"lagweight",
|
| 63 |
+
]
|
| 64 |
+
|
| 65 |
+
poly2lag: Final[_FuncPoly2Ortho] = ...
|
| 66 |
+
lag2poly: Final[_FuncUnOp] = ...
|
| 67 |
+
|
| 68 |
+
lagdomain: Final[_Array2[np.float64]] = ...
|
| 69 |
+
lagzero: Final[_Array1[np.int_]] = ...
|
| 70 |
+
lagone: Final[_Array1[np.int_]] = ...
|
| 71 |
+
lagx: Final[_Array2[np.int_]] = ...
|
| 72 |
+
|
| 73 |
+
lagline: Final[_FuncLine] = ...
|
| 74 |
+
lagfromroots: Final[_FuncFromRoots] = ...
|
| 75 |
+
lagadd: Final[_FuncBinOp] = ...
|
| 76 |
+
lagsub: Final[_FuncBinOp] = ...
|
| 77 |
+
lagmulx: Final[_FuncUnOp] = ...
|
| 78 |
+
lagmul: Final[_FuncBinOp] = ...
|
| 79 |
+
lagdiv: Final[_FuncBinOp] = ...
|
| 80 |
+
lagpow: Final[_FuncPow] = ...
|
| 81 |
+
lagder: Final[_FuncDer] = ...
|
| 82 |
+
lagint: Final[_FuncInteg] = ...
|
| 83 |
+
lagval: Final[_FuncVal] = ...
|
| 84 |
+
lagval2d: Final[_FuncVal2D] = ...
|
| 85 |
+
lagval3d: Final[_FuncVal3D] = ...
|
| 86 |
+
laggrid2d: Final[_FuncVal2D] = ...
|
| 87 |
+
laggrid3d: Final[_FuncVal3D] = ...
|
| 88 |
+
lagvander: Final[_FuncVander] = ...
|
| 89 |
+
lagvander2d: Final[_FuncVander2D] = ...
|
| 90 |
+
lagvander3d: Final[_FuncVander3D] = ...
|
| 91 |
+
lagfit: Final[_FuncFit] = ...
|
| 92 |
+
lagcompanion: Final[_FuncCompanion] = ...
|
| 93 |
+
lagroots: Final[_FuncRoots] = ...
|
| 94 |
+
laggauss: Final[_FuncGauss] = ...
|
| 95 |
+
lagweight: Final[_FuncWeight] = ...
|
| 96 |
+
|
| 97 |
+
class Laguerre(ABCPolyBase[L["L"]]):
|
| 98 |
+
basis_name: ClassVar[L["L"]] = "L" # pyright: ignore[reportIncompatibleMethodOverride]
|
| 99 |
+
domain: _Array2[np.float64 | Any] = ... # pyright: ignore[reportIncompatibleMethodOverride]
|
| 100 |
+
window: _Array2[np.float64 | Any] = ... # pyright: ignore[reportIncompatibleMethodOverride]
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/legendre.py
ADDED
|
@@ -0,0 +1,1603 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
==================================================
|
| 3 |
+
Legendre Series (:mod:`numpy.polynomial.legendre`)
|
| 4 |
+
==================================================
|
| 5 |
+
|
| 6 |
+
This module provides a number of objects (mostly functions) useful for
|
| 7 |
+
dealing with Legendre series, including a `Legendre` class that
|
| 8 |
+
encapsulates the usual arithmetic operations. (General information
|
| 9 |
+
on how this module represents and works with such polynomials is in the
|
| 10 |
+
docstring for its "parent" sub-package, `numpy.polynomial`).
|
| 11 |
+
|
| 12 |
+
Classes
|
| 13 |
+
-------
|
| 14 |
+
.. autosummary::
|
| 15 |
+
:toctree: generated/
|
| 16 |
+
|
| 17 |
+
Legendre
|
| 18 |
+
|
| 19 |
+
Constants
|
| 20 |
+
---------
|
| 21 |
+
|
| 22 |
+
.. autosummary::
|
| 23 |
+
:toctree: generated/
|
| 24 |
+
|
| 25 |
+
legdomain
|
| 26 |
+
legzero
|
| 27 |
+
legone
|
| 28 |
+
legx
|
| 29 |
+
|
| 30 |
+
Arithmetic
|
| 31 |
+
----------
|
| 32 |
+
|
| 33 |
+
.. autosummary::
|
| 34 |
+
:toctree: generated/
|
| 35 |
+
|
| 36 |
+
legadd
|
| 37 |
+
legsub
|
| 38 |
+
legmulx
|
| 39 |
+
legmul
|
| 40 |
+
legdiv
|
| 41 |
+
legpow
|
| 42 |
+
legval
|
| 43 |
+
legval2d
|
| 44 |
+
legval3d
|
| 45 |
+
leggrid2d
|
| 46 |
+
leggrid3d
|
| 47 |
+
|
| 48 |
+
Calculus
|
| 49 |
+
--------
|
| 50 |
+
|
| 51 |
+
.. autosummary::
|
| 52 |
+
:toctree: generated/
|
| 53 |
+
|
| 54 |
+
legder
|
| 55 |
+
legint
|
| 56 |
+
|
| 57 |
+
Misc Functions
|
| 58 |
+
--------------
|
| 59 |
+
|
| 60 |
+
.. autosummary::
|
| 61 |
+
:toctree: generated/
|
| 62 |
+
|
| 63 |
+
legfromroots
|
| 64 |
+
legroots
|
| 65 |
+
legvander
|
| 66 |
+
legvander2d
|
| 67 |
+
legvander3d
|
| 68 |
+
leggauss
|
| 69 |
+
legweight
|
| 70 |
+
legcompanion
|
| 71 |
+
legfit
|
| 72 |
+
legtrim
|
| 73 |
+
legline
|
| 74 |
+
leg2poly
|
| 75 |
+
poly2leg
|
| 76 |
+
|
| 77 |
+
See also
|
| 78 |
+
--------
|
| 79 |
+
numpy.polynomial
|
| 80 |
+
|
| 81 |
+
"""
|
| 82 |
+
import numpy as np
|
| 83 |
+
|
| 84 |
+
from . import polyutils as pu
|
| 85 |
+
from ._polybase import ABCPolyBase
|
| 86 |
+
|
| 87 |
+
__all__ = [
|
| 88 |
+
'legzero', 'legone', 'legx', 'legdomain', 'legline', 'legadd',
|
| 89 |
+
'legsub', 'legmulx', 'legmul', 'legdiv', 'legpow', 'legval', 'legder',
|
| 90 |
+
'legint', 'leg2poly', 'poly2leg', 'legfromroots', 'legvander',
|
| 91 |
+
'legfit', 'legtrim', 'legroots', 'Legendre', 'legval2d', 'legval3d',
|
| 92 |
+
'leggrid2d', 'leggrid3d', 'legvander2d', 'legvander3d', 'legcompanion',
|
| 93 |
+
'leggauss', 'legweight']
|
| 94 |
+
|
| 95 |
+
legtrim = pu.trimcoef
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def poly2leg(pol):
|
| 99 |
+
"""
|
| 100 |
+
Convert a polynomial to a Legendre series.
|
| 101 |
+
|
| 102 |
+
Convert an array representing the coefficients of a polynomial (relative
|
| 103 |
+
to the "standard" basis) ordered from lowest degree to highest, to an
|
| 104 |
+
array of the coefficients of the equivalent Legendre series, ordered
|
| 105 |
+
from lowest to highest degree.
|
| 106 |
+
|
| 107 |
+
Parameters
|
| 108 |
+
----------
|
| 109 |
+
pol : array_like
|
| 110 |
+
1-D array containing the polynomial coefficients
|
| 111 |
+
|
| 112 |
+
Returns
|
| 113 |
+
-------
|
| 114 |
+
c : ndarray
|
| 115 |
+
1-D array containing the coefficients of the equivalent Legendre
|
| 116 |
+
series.
|
| 117 |
+
|
| 118 |
+
See Also
|
| 119 |
+
--------
|
| 120 |
+
leg2poly
|
| 121 |
+
|
| 122 |
+
Notes
|
| 123 |
+
-----
|
| 124 |
+
The easy way to do conversions between polynomial basis sets
|
| 125 |
+
is to use the convert method of a class instance.
|
| 126 |
+
|
| 127 |
+
Examples
|
| 128 |
+
--------
|
| 129 |
+
>>> import numpy as np
|
| 130 |
+
>>> from numpy import polynomial as P
|
| 131 |
+
>>> p = P.Polynomial(np.arange(4))
|
| 132 |
+
>>> p
|
| 133 |
+
Polynomial([0., 1., 2., 3.], domain=[-1., 1.], window=[-1., 1.], ...
|
| 134 |
+
>>> c = P.Legendre(P.legendre.poly2leg(p.coef))
|
| 135 |
+
>>> c
|
| 136 |
+
Legendre([ 1. , 3.25, 1. , 0.75], domain=[-1, 1], window=[-1, 1]) # may vary
|
| 137 |
+
|
| 138 |
+
"""
|
| 139 |
+
[pol] = pu.as_series([pol])
|
| 140 |
+
deg = len(pol) - 1
|
| 141 |
+
res = 0
|
| 142 |
+
for i in range(deg, -1, -1):
|
| 143 |
+
res = legadd(legmulx(res), pol[i])
|
| 144 |
+
return res
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
def leg2poly(c):
|
| 148 |
+
"""
|
| 149 |
+
Convert a Legendre series to a polynomial.
|
| 150 |
+
|
| 151 |
+
Convert an array representing the coefficients of a Legendre series,
|
| 152 |
+
ordered from lowest degree to highest, to an array of the coefficients
|
| 153 |
+
of the equivalent polynomial (relative to the "standard" basis) ordered
|
| 154 |
+
from lowest to highest degree.
|
| 155 |
+
|
| 156 |
+
Parameters
|
| 157 |
+
----------
|
| 158 |
+
c : array_like
|
| 159 |
+
1-D array containing the Legendre series coefficients, ordered
|
| 160 |
+
from lowest order term to highest.
|
| 161 |
+
|
| 162 |
+
Returns
|
| 163 |
+
-------
|
| 164 |
+
pol : ndarray
|
| 165 |
+
1-D array containing the coefficients of the equivalent polynomial
|
| 166 |
+
(relative to the "standard" basis) ordered from lowest order term
|
| 167 |
+
to highest.
|
| 168 |
+
|
| 169 |
+
See Also
|
| 170 |
+
--------
|
| 171 |
+
poly2leg
|
| 172 |
+
|
| 173 |
+
Notes
|
| 174 |
+
-----
|
| 175 |
+
The easy way to do conversions between polynomial basis sets
|
| 176 |
+
is to use the convert method of a class instance.
|
| 177 |
+
|
| 178 |
+
Examples
|
| 179 |
+
--------
|
| 180 |
+
>>> from numpy import polynomial as P
|
| 181 |
+
>>> c = P.Legendre(range(4))
|
| 182 |
+
>>> c
|
| 183 |
+
Legendre([0., 1., 2., 3.], domain=[-1., 1.], window=[-1., 1.], symbol='x')
|
| 184 |
+
>>> p = c.convert(kind=P.Polynomial)
|
| 185 |
+
>>> p
|
| 186 |
+
Polynomial([-1. , -3.5, 3. , 7.5], domain=[-1., 1.], window=[-1., ...
|
| 187 |
+
>>> P.legendre.leg2poly(range(4))
|
| 188 |
+
array([-1. , -3.5, 3. , 7.5])
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
"""
|
| 192 |
+
from .polynomial import polyadd, polymulx, polysub
|
| 193 |
+
|
| 194 |
+
[c] = pu.as_series([c])
|
| 195 |
+
n = len(c)
|
| 196 |
+
if n < 3:
|
| 197 |
+
return c
|
| 198 |
+
else:
|
| 199 |
+
c0 = c[-2]
|
| 200 |
+
c1 = c[-1]
|
| 201 |
+
# i is the current degree of c1
|
| 202 |
+
for i in range(n - 1, 1, -1):
|
| 203 |
+
tmp = c0
|
| 204 |
+
c0 = polysub(c[i - 2], (c1 * (i - 1)) / i)
|
| 205 |
+
c1 = polyadd(tmp, (polymulx(c1) * (2 * i - 1)) / i)
|
| 206 |
+
return polyadd(c0, polymulx(c1))
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
#
|
| 210 |
+
# These are constant arrays are of integer type so as to be compatible
|
| 211 |
+
# with the widest range of other types, such as Decimal.
|
| 212 |
+
#
|
| 213 |
+
|
| 214 |
+
# Legendre
|
| 215 |
+
legdomain = np.array([-1., 1.])
|
| 216 |
+
|
| 217 |
+
# Legendre coefficients representing zero.
|
| 218 |
+
legzero = np.array([0])
|
| 219 |
+
|
| 220 |
+
# Legendre coefficients representing one.
|
| 221 |
+
legone = np.array([1])
|
| 222 |
+
|
| 223 |
+
# Legendre coefficients representing the identity x.
|
| 224 |
+
legx = np.array([0, 1])
|
| 225 |
+
|
| 226 |
+
|
| 227 |
+
def legline(off, scl):
|
| 228 |
+
"""
|
| 229 |
+
Legendre series whose graph is a straight line.
|
| 230 |
+
|
| 231 |
+
|
| 232 |
+
|
| 233 |
+
Parameters
|
| 234 |
+
----------
|
| 235 |
+
off, scl : scalars
|
| 236 |
+
The specified line is given by ``off + scl*x``.
|
| 237 |
+
|
| 238 |
+
Returns
|
| 239 |
+
-------
|
| 240 |
+
y : ndarray
|
| 241 |
+
This module's representation of the Legendre series for
|
| 242 |
+
``off + scl*x``.
|
| 243 |
+
|
| 244 |
+
See Also
|
| 245 |
+
--------
|
| 246 |
+
numpy.polynomial.polynomial.polyline
|
| 247 |
+
numpy.polynomial.chebyshev.chebline
|
| 248 |
+
numpy.polynomial.laguerre.lagline
|
| 249 |
+
numpy.polynomial.hermite.hermline
|
| 250 |
+
numpy.polynomial.hermite_e.hermeline
|
| 251 |
+
|
| 252 |
+
Examples
|
| 253 |
+
--------
|
| 254 |
+
>>> import numpy.polynomial.legendre as L
|
| 255 |
+
>>> L.legline(3,2)
|
| 256 |
+
array([3, 2])
|
| 257 |
+
>>> L.legval(-3, L.legline(3,2)) # should be -3
|
| 258 |
+
-3.0
|
| 259 |
+
|
| 260 |
+
"""
|
| 261 |
+
if scl != 0:
|
| 262 |
+
return np.array([off, scl])
|
| 263 |
+
else:
|
| 264 |
+
return np.array([off])
|
| 265 |
+
|
| 266 |
+
|
| 267 |
+
def legfromroots(roots):
|
| 268 |
+
"""
|
| 269 |
+
Generate a Legendre series with given roots.
|
| 270 |
+
|
| 271 |
+
The function returns the coefficients of the polynomial
|
| 272 |
+
|
| 273 |
+
.. math:: p(x) = (x - r_0) * (x - r_1) * ... * (x - r_n),
|
| 274 |
+
|
| 275 |
+
in Legendre form, where the :math:`r_n` are the roots specified in `roots`.
|
| 276 |
+
If a zero has multiplicity n, then it must appear in `roots` n times.
|
| 277 |
+
For instance, if 2 is a root of multiplicity three and 3 is a root of
|
| 278 |
+
multiplicity 2, then `roots` looks something like [2, 2, 2, 3, 3]. The
|
| 279 |
+
roots can appear in any order.
|
| 280 |
+
|
| 281 |
+
If the returned coefficients are `c`, then
|
| 282 |
+
|
| 283 |
+
.. math:: p(x) = c_0 + c_1 * L_1(x) + ... + c_n * L_n(x)
|
| 284 |
+
|
| 285 |
+
The coefficient of the last term is not generally 1 for monic
|
| 286 |
+
polynomials in Legendre form.
|
| 287 |
+
|
| 288 |
+
Parameters
|
| 289 |
+
----------
|
| 290 |
+
roots : array_like
|
| 291 |
+
Sequence containing the roots.
|
| 292 |
+
|
| 293 |
+
Returns
|
| 294 |
+
-------
|
| 295 |
+
out : ndarray
|
| 296 |
+
1-D array of coefficients. If all roots are real then `out` is a
|
| 297 |
+
real array, if some of the roots are complex, then `out` is complex
|
| 298 |
+
even if all the coefficients in the result are real (see Examples
|
| 299 |
+
below).
|
| 300 |
+
|
| 301 |
+
See Also
|
| 302 |
+
--------
|
| 303 |
+
numpy.polynomial.polynomial.polyfromroots
|
| 304 |
+
numpy.polynomial.chebyshev.chebfromroots
|
| 305 |
+
numpy.polynomial.laguerre.lagfromroots
|
| 306 |
+
numpy.polynomial.hermite.hermfromroots
|
| 307 |
+
numpy.polynomial.hermite_e.hermefromroots
|
| 308 |
+
|
| 309 |
+
Examples
|
| 310 |
+
--------
|
| 311 |
+
>>> import numpy.polynomial.legendre as L
|
| 312 |
+
>>> L.legfromroots((-1,0,1)) # x^3 - x relative to the standard basis
|
| 313 |
+
array([ 0. , -0.4, 0. , 0.4])
|
| 314 |
+
>>> j = complex(0,1)
|
| 315 |
+
>>> L.legfromroots((-j,j)) # x^2 + 1 relative to the standard basis
|
| 316 |
+
array([ 1.33333333+0.j, 0.00000000+0.j, 0.66666667+0.j]) # may vary
|
| 317 |
+
|
| 318 |
+
"""
|
| 319 |
+
return pu._fromroots(legline, legmul, roots)
|
| 320 |
+
|
| 321 |
+
|
| 322 |
+
def legadd(c1, c2):
|
| 323 |
+
"""
|
| 324 |
+
Add one Legendre series to another.
|
| 325 |
+
|
| 326 |
+
Returns the sum of two Legendre series `c1` + `c2`. The arguments
|
| 327 |
+
are sequences of coefficients ordered from lowest order term to
|
| 328 |
+
highest, i.e., [1,2,3] represents the series ``P_0 + 2*P_1 + 3*P_2``.
|
| 329 |
+
|
| 330 |
+
Parameters
|
| 331 |
+
----------
|
| 332 |
+
c1, c2 : array_like
|
| 333 |
+
1-D arrays of Legendre series coefficients ordered from low to
|
| 334 |
+
high.
|
| 335 |
+
|
| 336 |
+
Returns
|
| 337 |
+
-------
|
| 338 |
+
out : ndarray
|
| 339 |
+
Array representing the Legendre series of their sum.
|
| 340 |
+
|
| 341 |
+
See Also
|
| 342 |
+
--------
|
| 343 |
+
legsub, legmulx, legmul, legdiv, legpow
|
| 344 |
+
|
| 345 |
+
Notes
|
| 346 |
+
-----
|
| 347 |
+
Unlike multiplication, division, etc., the sum of two Legendre series
|
| 348 |
+
is a Legendre series (without having to "reproject" the result onto
|
| 349 |
+
the basis set) so addition, just like that of "standard" polynomials,
|
| 350 |
+
is simply "component-wise."
|
| 351 |
+
|
| 352 |
+
Examples
|
| 353 |
+
--------
|
| 354 |
+
>>> from numpy.polynomial import legendre as L
|
| 355 |
+
>>> c1 = (1,2,3)
|
| 356 |
+
>>> c2 = (3,2,1)
|
| 357 |
+
>>> L.legadd(c1,c2)
|
| 358 |
+
array([4., 4., 4.])
|
| 359 |
+
|
| 360 |
+
"""
|
| 361 |
+
return pu._add(c1, c2)
|
| 362 |
+
|
| 363 |
+
|
| 364 |
+
def legsub(c1, c2):
|
| 365 |
+
"""
|
| 366 |
+
Subtract one Legendre series from another.
|
| 367 |
+
|
| 368 |
+
Returns the difference of two Legendre series `c1` - `c2`. The
|
| 369 |
+
sequences of coefficients are from lowest order term to highest, i.e.,
|
| 370 |
+
[1,2,3] represents the series ``P_0 + 2*P_1 + 3*P_2``.
|
| 371 |
+
|
| 372 |
+
Parameters
|
| 373 |
+
----------
|
| 374 |
+
c1, c2 : array_like
|
| 375 |
+
1-D arrays of Legendre series coefficients ordered from low to
|
| 376 |
+
high.
|
| 377 |
+
|
| 378 |
+
Returns
|
| 379 |
+
-------
|
| 380 |
+
out : ndarray
|
| 381 |
+
Of Legendre series coefficients representing their difference.
|
| 382 |
+
|
| 383 |
+
See Also
|
| 384 |
+
--------
|
| 385 |
+
legadd, legmulx, legmul, legdiv, legpow
|
| 386 |
+
|
| 387 |
+
Notes
|
| 388 |
+
-----
|
| 389 |
+
Unlike multiplication, division, etc., the difference of two Legendre
|
| 390 |
+
series is a Legendre series (without having to "reproject" the result
|
| 391 |
+
onto the basis set) so subtraction, just like that of "standard"
|
| 392 |
+
polynomials, is simply "component-wise."
|
| 393 |
+
|
| 394 |
+
Examples
|
| 395 |
+
--------
|
| 396 |
+
>>> from numpy.polynomial import legendre as L
|
| 397 |
+
>>> c1 = (1,2,3)
|
| 398 |
+
>>> c2 = (3,2,1)
|
| 399 |
+
>>> L.legsub(c1,c2)
|
| 400 |
+
array([-2., 0., 2.])
|
| 401 |
+
>>> L.legsub(c2,c1) # -C.legsub(c1,c2)
|
| 402 |
+
array([ 2., 0., -2.])
|
| 403 |
+
|
| 404 |
+
"""
|
| 405 |
+
return pu._sub(c1, c2)
|
| 406 |
+
|
| 407 |
+
|
| 408 |
+
def legmulx(c):
|
| 409 |
+
"""Multiply a Legendre series by x.
|
| 410 |
+
|
| 411 |
+
Multiply the Legendre series `c` by x, where x is the independent
|
| 412 |
+
variable.
|
| 413 |
+
|
| 414 |
+
|
| 415 |
+
Parameters
|
| 416 |
+
----------
|
| 417 |
+
c : array_like
|
| 418 |
+
1-D array of Legendre series coefficients ordered from low to
|
| 419 |
+
high.
|
| 420 |
+
|
| 421 |
+
Returns
|
| 422 |
+
-------
|
| 423 |
+
out : ndarray
|
| 424 |
+
Array representing the result of the multiplication.
|
| 425 |
+
|
| 426 |
+
See Also
|
| 427 |
+
--------
|
| 428 |
+
legadd, legsub, legmul, legdiv, legpow
|
| 429 |
+
|
| 430 |
+
Notes
|
| 431 |
+
-----
|
| 432 |
+
The multiplication uses the recursion relationship for Legendre
|
| 433 |
+
polynomials in the form
|
| 434 |
+
|
| 435 |
+
.. math::
|
| 436 |
+
|
| 437 |
+
xP_i(x) = ((i + 1)*P_{i + 1}(x) + i*P_{i - 1}(x))/(2i + 1)
|
| 438 |
+
|
| 439 |
+
Examples
|
| 440 |
+
--------
|
| 441 |
+
>>> from numpy.polynomial import legendre as L
|
| 442 |
+
>>> L.legmulx([1,2,3])
|
| 443 |
+
array([ 0.66666667, 2.2, 1.33333333, 1.8]) # may vary
|
| 444 |
+
|
| 445 |
+
"""
|
| 446 |
+
# c is a trimmed copy
|
| 447 |
+
[c] = pu.as_series([c])
|
| 448 |
+
# The zero series needs special treatment
|
| 449 |
+
if len(c) == 1 and c[0] == 0:
|
| 450 |
+
return c
|
| 451 |
+
|
| 452 |
+
prd = np.empty(len(c) + 1, dtype=c.dtype)
|
| 453 |
+
prd[0] = c[0] * 0
|
| 454 |
+
prd[1] = c[0]
|
| 455 |
+
for i in range(1, len(c)):
|
| 456 |
+
j = i + 1
|
| 457 |
+
k = i - 1
|
| 458 |
+
s = i + j
|
| 459 |
+
prd[j] = (c[i] * j) / s
|
| 460 |
+
prd[k] += (c[i] * i) / s
|
| 461 |
+
return prd
|
| 462 |
+
|
| 463 |
+
|
| 464 |
+
def legmul(c1, c2):
|
| 465 |
+
"""
|
| 466 |
+
Multiply one Legendre series by another.
|
| 467 |
+
|
| 468 |
+
Returns the product of two Legendre series `c1` * `c2`. The arguments
|
| 469 |
+
are sequences of coefficients, from lowest order "term" to highest,
|
| 470 |
+
e.g., [1,2,3] represents the series ``P_0 + 2*P_1 + 3*P_2``.
|
| 471 |
+
|
| 472 |
+
Parameters
|
| 473 |
+
----------
|
| 474 |
+
c1, c2 : array_like
|
| 475 |
+
1-D arrays of Legendre series coefficients ordered from low to
|
| 476 |
+
high.
|
| 477 |
+
|
| 478 |
+
Returns
|
| 479 |
+
-------
|
| 480 |
+
out : ndarray
|
| 481 |
+
Of Legendre series coefficients representing their product.
|
| 482 |
+
|
| 483 |
+
See Also
|
| 484 |
+
--------
|
| 485 |
+
legadd, legsub, legmulx, legdiv, legpow
|
| 486 |
+
|
| 487 |
+
Notes
|
| 488 |
+
-----
|
| 489 |
+
In general, the (polynomial) product of two C-series results in terms
|
| 490 |
+
that are not in the Legendre polynomial basis set. Thus, to express
|
| 491 |
+
the product as a Legendre series, it is necessary to "reproject" the
|
| 492 |
+
product onto said basis set, which may produce "unintuitive" (but
|
| 493 |
+
correct) results; see Examples section below.
|
| 494 |
+
|
| 495 |
+
Examples
|
| 496 |
+
--------
|
| 497 |
+
>>> from numpy.polynomial import legendre as L
|
| 498 |
+
>>> c1 = (1,2,3)
|
| 499 |
+
>>> c2 = (3,2)
|
| 500 |
+
>>> L.legmul(c1,c2) # multiplication requires "reprojection"
|
| 501 |
+
array([ 4.33333333, 10.4 , 11.66666667, 3.6 ]) # may vary
|
| 502 |
+
|
| 503 |
+
"""
|
| 504 |
+
# s1, s2 are trimmed copies
|
| 505 |
+
[c1, c2] = pu.as_series([c1, c2])
|
| 506 |
+
|
| 507 |
+
if len(c1) > len(c2):
|
| 508 |
+
c = c2
|
| 509 |
+
xs = c1
|
| 510 |
+
else:
|
| 511 |
+
c = c1
|
| 512 |
+
xs = c2
|
| 513 |
+
|
| 514 |
+
if len(c) == 1:
|
| 515 |
+
c0 = c[0] * xs
|
| 516 |
+
c1 = 0
|
| 517 |
+
elif len(c) == 2:
|
| 518 |
+
c0 = c[0] * xs
|
| 519 |
+
c1 = c[1] * xs
|
| 520 |
+
else:
|
| 521 |
+
nd = len(c)
|
| 522 |
+
c0 = c[-2] * xs
|
| 523 |
+
c1 = c[-1] * xs
|
| 524 |
+
for i in range(3, len(c) + 1):
|
| 525 |
+
tmp = c0
|
| 526 |
+
nd = nd - 1
|
| 527 |
+
c0 = legsub(c[-i] * xs, (c1 * (nd - 1)) / nd)
|
| 528 |
+
c1 = legadd(tmp, (legmulx(c1) * (2 * nd - 1)) / nd)
|
| 529 |
+
return legadd(c0, legmulx(c1))
|
| 530 |
+
|
| 531 |
+
|
| 532 |
+
def legdiv(c1, c2):
|
| 533 |
+
"""
|
| 534 |
+
Divide one Legendre series by another.
|
| 535 |
+
|
| 536 |
+
Returns the quotient-with-remainder of two Legendre series
|
| 537 |
+
`c1` / `c2`. The arguments are sequences of coefficients from lowest
|
| 538 |
+
order "term" to highest, e.g., [1,2,3] represents the series
|
| 539 |
+
``P_0 + 2*P_1 + 3*P_2``.
|
| 540 |
+
|
| 541 |
+
Parameters
|
| 542 |
+
----------
|
| 543 |
+
c1, c2 : array_like
|
| 544 |
+
1-D arrays of Legendre series coefficients ordered from low to
|
| 545 |
+
high.
|
| 546 |
+
|
| 547 |
+
Returns
|
| 548 |
+
-------
|
| 549 |
+
quo, rem : ndarrays
|
| 550 |
+
Of Legendre series coefficients representing the quotient and
|
| 551 |
+
remainder.
|
| 552 |
+
|
| 553 |
+
See Also
|
| 554 |
+
--------
|
| 555 |
+
legadd, legsub, legmulx, legmul, legpow
|
| 556 |
+
|
| 557 |
+
Notes
|
| 558 |
+
-----
|
| 559 |
+
In general, the (polynomial) division of one Legendre series by another
|
| 560 |
+
results in quotient and remainder terms that are not in the Legendre
|
| 561 |
+
polynomial basis set. Thus, to express these results as a Legendre
|
| 562 |
+
series, it is necessary to "reproject" the results onto the Legendre
|
| 563 |
+
basis set, which may produce "unintuitive" (but correct) results; see
|
| 564 |
+
Examples section below.
|
| 565 |
+
|
| 566 |
+
Examples
|
| 567 |
+
--------
|
| 568 |
+
>>> from numpy.polynomial import legendre as L
|
| 569 |
+
>>> c1 = (1,2,3)
|
| 570 |
+
>>> c2 = (3,2,1)
|
| 571 |
+
>>> L.legdiv(c1,c2) # quotient "intuitive," remainder not
|
| 572 |
+
(array([3.]), array([-8., -4.]))
|
| 573 |
+
>>> c2 = (0,1,2,3)
|
| 574 |
+
>>> L.legdiv(c2,c1) # neither "intuitive"
|
| 575 |
+
(array([-0.07407407, 1.66666667]), array([-1.03703704, -2.51851852])) # may vary
|
| 576 |
+
|
| 577 |
+
"""
|
| 578 |
+
return pu._div(legmul, c1, c2)
|
| 579 |
+
|
| 580 |
+
|
| 581 |
+
def legpow(c, pow, maxpower=16):
|
| 582 |
+
"""Raise a Legendre series to a power.
|
| 583 |
+
|
| 584 |
+
Returns the Legendre series `c` raised to the power `pow`. The
|
| 585 |
+
argument `c` is a sequence of coefficients ordered from low to high.
|
| 586 |
+
i.e., [1,2,3] is the series ``P_0 + 2*P_1 + 3*P_2.``
|
| 587 |
+
|
| 588 |
+
Parameters
|
| 589 |
+
----------
|
| 590 |
+
c : array_like
|
| 591 |
+
1-D array of Legendre series coefficients ordered from low to
|
| 592 |
+
high.
|
| 593 |
+
pow : integer
|
| 594 |
+
Power to which the series will be raised
|
| 595 |
+
maxpower : integer, optional
|
| 596 |
+
Maximum power allowed. This is mainly to limit growth of the series
|
| 597 |
+
to unmanageable size. Default is 16
|
| 598 |
+
|
| 599 |
+
Returns
|
| 600 |
+
-------
|
| 601 |
+
coef : ndarray
|
| 602 |
+
Legendre series of power.
|
| 603 |
+
|
| 604 |
+
See Also
|
| 605 |
+
--------
|
| 606 |
+
legadd, legsub, legmulx, legmul, legdiv
|
| 607 |
+
|
| 608 |
+
"""
|
| 609 |
+
return pu._pow(legmul, c, pow, maxpower)
|
| 610 |
+
|
| 611 |
+
|
| 612 |
+
def legder(c, m=1, scl=1, axis=0):
|
| 613 |
+
"""
|
| 614 |
+
Differentiate a Legendre series.
|
| 615 |
+
|
| 616 |
+
Returns the Legendre series coefficients `c` differentiated `m` times
|
| 617 |
+
along `axis`. At each iteration the result is multiplied by `scl` (the
|
| 618 |
+
scaling factor is for use in a linear change of variable). The argument
|
| 619 |
+
`c` is an array of coefficients from low to high degree along each
|
| 620 |
+
axis, e.g., [1,2,3] represents the series ``1*L_0 + 2*L_1 + 3*L_2``
|
| 621 |
+
while [[1,2],[1,2]] represents ``1*L_0(x)*L_0(y) + 1*L_1(x)*L_0(y) +
|
| 622 |
+
2*L_0(x)*L_1(y) + 2*L_1(x)*L_1(y)`` if axis=0 is ``x`` and axis=1 is
|
| 623 |
+
``y``.
|
| 624 |
+
|
| 625 |
+
Parameters
|
| 626 |
+
----------
|
| 627 |
+
c : array_like
|
| 628 |
+
Array of Legendre series coefficients. If c is multidimensional the
|
| 629 |
+
different axis correspond to different variables with the degree in
|
| 630 |
+
each axis given by the corresponding index.
|
| 631 |
+
m : int, optional
|
| 632 |
+
Number of derivatives taken, must be non-negative. (Default: 1)
|
| 633 |
+
scl : scalar, optional
|
| 634 |
+
Each differentiation is multiplied by `scl`. The end result is
|
| 635 |
+
multiplication by ``scl**m``. This is for use in a linear change of
|
| 636 |
+
variable. (Default: 1)
|
| 637 |
+
axis : int, optional
|
| 638 |
+
Axis over which the derivative is taken. (Default: 0).
|
| 639 |
+
|
| 640 |
+
Returns
|
| 641 |
+
-------
|
| 642 |
+
der : ndarray
|
| 643 |
+
Legendre series of the derivative.
|
| 644 |
+
|
| 645 |
+
See Also
|
| 646 |
+
--------
|
| 647 |
+
legint
|
| 648 |
+
|
| 649 |
+
Notes
|
| 650 |
+
-----
|
| 651 |
+
In general, the result of differentiating a Legendre series does not
|
| 652 |
+
resemble the same operation on a power series. Thus the result of this
|
| 653 |
+
function may be "unintuitive," albeit correct; see Examples section
|
| 654 |
+
below.
|
| 655 |
+
|
| 656 |
+
Examples
|
| 657 |
+
--------
|
| 658 |
+
>>> from numpy.polynomial import legendre as L
|
| 659 |
+
>>> c = (1,2,3,4)
|
| 660 |
+
>>> L.legder(c)
|
| 661 |
+
array([ 6., 9., 20.])
|
| 662 |
+
>>> L.legder(c, 3)
|
| 663 |
+
array([60.])
|
| 664 |
+
>>> L.legder(c, scl=-1)
|
| 665 |
+
array([ -6., -9., -20.])
|
| 666 |
+
>>> L.legder(c, 2,-1)
|
| 667 |
+
array([ 9., 60.])
|
| 668 |
+
|
| 669 |
+
"""
|
| 670 |
+
c = np.array(c, ndmin=1, copy=True)
|
| 671 |
+
if c.dtype.char in '?bBhHiIlLqQpP':
|
| 672 |
+
c = c.astype(np.double)
|
| 673 |
+
cnt = pu._as_int(m, "the order of derivation")
|
| 674 |
+
iaxis = pu._as_int(axis, "the axis")
|
| 675 |
+
if cnt < 0:
|
| 676 |
+
raise ValueError("The order of derivation must be non-negative")
|
| 677 |
+
iaxis = np.lib.array_utils.normalize_axis_index(iaxis, c.ndim)
|
| 678 |
+
|
| 679 |
+
if cnt == 0:
|
| 680 |
+
return c
|
| 681 |
+
|
| 682 |
+
c = np.moveaxis(c, iaxis, 0)
|
| 683 |
+
n = len(c)
|
| 684 |
+
if cnt >= n:
|
| 685 |
+
c = c[:1] * 0
|
| 686 |
+
else:
|
| 687 |
+
for i in range(cnt):
|
| 688 |
+
n = n - 1
|
| 689 |
+
c *= scl
|
| 690 |
+
der = np.empty((n,) + c.shape[1:], dtype=c.dtype)
|
| 691 |
+
for j in range(n, 2, -1):
|
| 692 |
+
der[j - 1] = (2 * j - 1) * c[j]
|
| 693 |
+
c[j - 2] += c[j]
|
| 694 |
+
if n > 1:
|
| 695 |
+
der[1] = 3 * c[2]
|
| 696 |
+
der[0] = c[1]
|
| 697 |
+
c = der
|
| 698 |
+
c = np.moveaxis(c, 0, iaxis)
|
| 699 |
+
return c
|
| 700 |
+
|
| 701 |
+
|
| 702 |
+
def legint(c, m=1, k=[], lbnd=0, scl=1, axis=0):
|
| 703 |
+
"""
|
| 704 |
+
Integrate a Legendre series.
|
| 705 |
+
|
| 706 |
+
Returns the Legendre series coefficients `c` integrated `m` times from
|
| 707 |
+
`lbnd` along `axis`. At each iteration the resulting series is
|
| 708 |
+
**multiplied** by `scl` and an integration constant, `k`, is added.
|
| 709 |
+
The scaling factor is for use in a linear change of variable. ("Buyer
|
| 710 |
+
beware": note that, depending on what one is doing, one may want `scl`
|
| 711 |
+
to be the reciprocal of what one might expect; for more information,
|
| 712 |
+
see the Notes section below.) The argument `c` is an array of
|
| 713 |
+
coefficients from low to high degree along each axis, e.g., [1,2,3]
|
| 714 |
+
represents the series ``L_0 + 2*L_1 + 3*L_2`` while [[1,2],[1,2]]
|
| 715 |
+
represents ``1*L_0(x)*L_0(y) + 1*L_1(x)*L_0(y) + 2*L_0(x)*L_1(y) +
|
| 716 |
+
2*L_1(x)*L_1(y)`` if axis=0 is ``x`` and axis=1 is ``y``.
|
| 717 |
+
|
| 718 |
+
Parameters
|
| 719 |
+
----------
|
| 720 |
+
c : array_like
|
| 721 |
+
Array of Legendre series coefficients. If c is multidimensional the
|
| 722 |
+
different axis correspond to different variables with the degree in
|
| 723 |
+
each axis given by the corresponding index.
|
| 724 |
+
m : int, optional
|
| 725 |
+
Order of integration, must be positive. (Default: 1)
|
| 726 |
+
k : {[], list, scalar}, optional
|
| 727 |
+
Integration constant(s). The value of the first integral at
|
| 728 |
+
``lbnd`` is the first value in the list, the value of the second
|
| 729 |
+
integral at ``lbnd`` is the second value, etc. If ``k == []`` (the
|
| 730 |
+
default), all constants are set to zero. If ``m == 1``, a single
|
| 731 |
+
scalar can be given instead of a list.
|
| 732 |
+
lbnd : scalar, optional
|
| 733 |
+
The lower bound of the integral. (Default: 0)
|
| 734 |
+
scl : scalar, optional
|
| 735 |
+
Following each integration the result is *multiplied* by `scl`
|
| 736 |
+
before the integration constant is added. (Default: 1)
|
| 737 |
+
axis : int, optional
|
| 738 |
+
Axis over which the integral is taken. (Default: 0).
|
| 739 |
+
|
| 740 |
+
Returns
|
| 741 |
+
-------
|
| 742 |
+
S : ndarray
|
| 743 |
+
Legendre series coefficient array of the integral.
|
| 744 |
+
|
| 745 |
+
Raises
|
| 746 |
+
------
|
| 747 |
+
ValueError
|
| 748 |
+
If ``m < 0``, ``len(k) > m``, ``np.ndim(lbnd) != 0``, or
|
| 749 |
+
``np.ndim(scl) != 0``.
|
| 750 |
+
|
| 751 |
+
See Also
|
| 752 |
+
--------
|
| 753 |
+
legder
|
| 754 |
+
|
| 755 |
+
Notes
|
| 756 |
+
-----
|
| 757 |
+
Note that the result of each integration is *multiplied* by `scl`.
|
| 758 |
+
Why is this important to note? Say one is making a linear change of
|
| 759 |
+
variable :math:`u = ax + b` in an integral relative to `x`. Then
|
| 760 |
+
:math:`dx = du/a`, so one will need to set `scl` equal to
|
| 761 |
+
:math:`1/a` - perhaps not what one would have first thought.
|
| 762 |
+
|
| 763 |
+
Also note that, in general, the result of integrating a C-series needs
|
| 764 |
+
to be "reprojected" onto the C-series basis set. Thus, typically,
|
| 765 |
+
the result of this function is "unintuitive," albeit correct; see
|
| 766 |
+
Examples section below.
|
| 767 |
+
|
| 768 |
+
Examples
|
| 769 |
+
--------
|
| 770 |
+
>>> from numpy.polynomial import legendre as L
|
| 771 |
+
>>> c = (1,2,3)
|
| 772 |
+
>>> L.legint(c)
|
| 773 |
+
array([ 0.33333333, 0.4 , 0.66666667, 0.6 ]) # may vary
|
| 774 |
+
>>> L.legint(c, 3)
|
| 775 |
+
array([ 1.66666667e-02, -1.78571429e-02, 4.76190476e-02, # may vary
|
| 776 |
+
-1.73472348e-18, 1.90476190e-02, 9.52380952e-03])
|
| 777 |
+
>>> L.legint(c, k=3)
|
| 778 |
+
array([ 3.33333333, 0.4 , 0.66666667, 0.6 ]) # may vary
|
| 779 |
+
>>> L.legint(c, lbnd=-2)
|
| 780 |
+
array([ 7.33333333, 0.4 , 0.66666667, 0.6 ]) # may vary
|
| 781 |
+
>>> L.legint(c, scl=2)
|
| 782 |
+
array([ 0.66666667, 0.8 , 1.33333333, 1.2 ]) # may vary
|
| 783 |
+
|
| 784 |
+
"""
|
| 785 |
+
c = np.array(c, ndmin=1, copy=True)
|
| 786 |
+
if c.dtype.char in '?bBhHiIlLqQpP':
|
| 787 |
+
c = c.astype(np.double)
|
| 788 |
+
if not np.iterable(k):
|
| 789 |
+
k = [k]
|
| 790 |
+
cnt = pu._as_int(m, "the order of integration")
|
| 791 |
+
iaxis = pu._as_int(axis, "the axis")
|
| 792 |
+
if cnt < 0:
|
| 793 |
+
raise ValueError("The order of integration must be non-negative")
|
| 794 |
+
if len(k) > cnt:
|
| 795 |
+
raise ValueError("Too many integration constants")
|
| 796 |
+
if np.ndim(lbnd) != 0:
|
| 797 |
+
raise ValueError("lbnd must be a scalar.")
|
| 798 |
+
if np.ndim(scl) != 0:
|
| 799 |
+
raise ValueError("scl must be a scalar.")
|
| 800 |
+
iaxis = np.lib.array_utils.normalize_axis_index(iaxis, c.ndim)
|
| 801 |
+
|
| 802 |
+
if cnt == 0:
|
| 803 |
+
return c
|
| 804 |
+
|
| 805 |
+
c = np.moveaxis(c, iaxis, 0)
|
| 806 |
+
k = list(k) + [0] * (cnt - len(k))
|
| 807 |
+
for i in range(cnt):
|
| 808 |
+
n = len(c)
|
| 809 |
+
c *= scl
|
| 810 |
+
if n == 1 and np.all(c[0] == 0):
|
| 811 |
+
c[0] += k[i]
|
| 812 |
+
else:
|
| 813 |
+
tmp = np.empty((n + 1,) + c.shape[1:], dtype=c.dtype)
|
| 814 |
+
tmp[0] = c[0] * 0
|
| 815 |
+
tmp[1] = c[0]
|
| 816 |
+
if n > 1:
|
| 817 |
+
tmp[2] = c[1] / 3
|
| 818 |
+
for j in range(2, n):
|
| 819 |
+
t = c[j] / (2 * j + 1)
|
| 820 |
+
tmp[j + 1] = t
|
| 821 |
+
tmp[j - 1] -= t
|
| 822 |
+
tmp[0] += k[i] - legval(lbnd, tmp)
|
| 823 |
+
c = tmp
|
| 824 |
+
c = np.moveaxis(c, 0, iaxis)
|
| 825 |
+
return c
|
| 826 |
+
|
| 827 |
+
|
| 828 |
+
def legval(x, c, tensor=True):
|
| 829 |
+
"""
|
| 830 |
+
Evaluate a Legendre series at points x.
|
| 831 |
+
|
| 832 |
+
If `c` is of length ``n + 1``, this function returns the value:
|
| 833 |
+
|
| 834 |
+
.. math:: p(x) = c_0 * L_0(x) + c_1 * L_1(x) + ... + c_n * L_n(x)
|
| 835 |
+
|
| 836 |
+
The parameter `x` is converted to an array only if it is a tuple or a
|
| 837 |
+
list, otherwise it is treated as a scalar. In either case, either `x`
|
| 838 |
+
or its elements must support multiplication and addition both with
|
| 839 |
+
themselves and with the elements of `c`.
|
| 840 |
+
|
| 841 |
+
If `c` is a 1-D array, then ``p(x)`` will have the same shape as `x`. If
|
| 842 |
+
`c` is multidimensional, then the shape of the result depends on the
|
| 843 |
+
value of `tensor`. If `tensor` is true the shape will be c.shape[1:] +
|
| 844 |
+
x.shape. If `tensor` is false the shape will be c.shape[1:]. Note that
|
| 845 |
+
scalars have shape (,).
|
| 846 |
+
|
| 847 |
+
Trailing zeros in the coefficients will be used in the evaluation, so
|
| 848 |
+
they should be avoided if efficiency is a concern.
|
| 849 |
+
|
| 850 |
+
Parameters
|
| 851 |
+
----------
|
| 852 |
+
x : array_like, compatible object
|
| 853 |
+
If `x` is a list or tuple, it is converted to an ndarray, otherwise
|
| 854 |
+
it is left unchanged and treated as a scalar. In either case, `x`
|
| 855 |
+
or its elements must support addition and multiplication with
|
| 856 |
+
themselves and with the elements of `c`.
|
| 857 |
+
c : array_like
|
| 858 |
+
Array of coefficients ordered so that the coefficients for terms of
|
| 859 |
+
degree n are contained in c[n]. If `c` is multidimensional the
|
| 860 |
+
remaining indices enumerate multiple polynomials. In the two
|
| 861 |
+
dimensional case the coefficients may be thought of as stored in
|
| 862 |
+
the columns of `c`.
|
| 863 |
+
tensor : boolean, optional
|
| 864 |
+
If True, the shape of the coefficient array is extended with ones
|
| 865 |
+
on the right, one for each dimension of `x`. Scalars have dimension 0
|
| 866 |
+
for this action. The result is that every column of coefficients in
|
| 867 |
+
`c` is evaluated for every element of `x`. If False, `x` is broadcast
|
| 868 |
+
over the columns of `c` for the evaluation. This keyword is useful
|
| 869 |
+
when `c` is multidimensional. The default value is True.
|
| 870 |
+
|
| 871 |
+
Returns
|
| 872 |
+
-------
|
| 873 |
+
values : ndarray, algebra_like
|
| 874 |
+
The shape of the return value is described above.
|
| 875 |
+
|
| 876 |
+
See Also
|
| 877 |
+
--------
|
| 878 |
+
legval2d, leggrid2d, legval3d, leggrid3d
|
| 879 |
+
|
| 880 |
+
Notes
|
| 881 |
+
-----
|
| 882 |
+
The evaluation uses Clenshaw recursion, aka synthetic division.
|
| 883 |
+
|
| 884 |
+
"""
|
| 885 |
+
c = np.array(c, ndmin=1, copy=None)
|
| 886 |
+
if c.dtype.char in '?bBhHiIlLqQpP':
|
| 887 |
+
c = c.astype(np.double)
|
| 888 |
+
if isinstance(x, (tuple, list)):
|
| 889 |
+
x = np.asarray(x)
|
| 890 |
+
if isinstance(x, np.ndarray) and tensor:
|
| 891 |
+
c = c.reshape(c.shape + (1,) * x.ndim)
|
| 892 |
+
|
| 893 |
+
if len(c) == 1:
|
| 894 |
+
c0 = c[0]
|
| 895 |
+
c1 = 0
|
| 896 |
+
elif len(c) == 2:
|
| 897 |
+
c0 = c[0]
|
| 898 |
+
c1 = c[1]
|
| 899 |
+
else:
|
| 900 |
+
nd = len(c)
|
| 901 |
+
c0 = c[-2]
|
| 902 |
+
c1 = c[-1]
|
| 903 |
+
for i in range(3, len(c) + 1):
|
| 904 |
+
tmp = c0
|
| 905 |
+
nd = nd - 1
|
| 906 |
+
c0 = c[-i] - c1 * ((nd - 1) / nd)
|
| 907 |
+
c1 = tmp + c1 * x * ((2 * nd - 1) / nd)
|
| 908 |
+
return c0 + c1 * x
|
| 909 |
+
|
| 910 |
+
|
| 911 |
+
def legval2d(x, y, c):
|
| 912 |
+
"""
|
| 913 |
+
Evaluate a 2-D Legendre series at points (x, y).
|
| 914 |
+
|
| 915 |
+
This function returns the values:
|
| 916 |
+
|
| 917 |
+
.. math:: p(x,y) = \\sum_{i,j} c_{i,j} * L_i(x) * L_j(y)
|
| 918 |
+
|
| 919 |
+
The parameters `x` and `y` are converted to arrays only if they are
|
| 920 |
+
tuples or a lists, otherwise they are treated as a scalars and they
|
| 921 |
+
must have the same shape after conversion. In either case, either `x`
|
| 922 |
+
and `y` or their elements must support multiplication and addition both
|
| 923 |
+
with themselves and with the elements of `c`.
|
| 924 |
+
|
| 925 |
+
If `c` is a 1-D array a one is implicitly appended to its shape to make
|
| 926 |
+
it 2-D. The shape of the result will be c.shape[2:] + x.shape.
|
| 927 |
+
|
| 928 |
+
Parameters
|
| 929 |
+
----------
|
| 930 |
+
x, y : array_like, compatible objects
|
| 931 |
+
The two dimensional series is evaluated at the points ``(x, y)``,
|
| 932 |
+
where `x` and `y` must have the same shape. If `x` or `y` is a list
|
| 933 |
+
or tuple, it is first converted to an ndarray, otherwise it is left
|
| 934 |
+
unchanged and if it isn't an ndarray it is treated as a scalar.
|
| 935 |
+
c : array_like
|
| 936 |
+
Array of coefficients ordered so that the coefficient of the term
|
| 937 |
+
of multi-degree i,j is contained in ``c[i,j]``. If `c` has
|
| 938 |
+
dimension greater than two the remaining indices enumerate multiple
|
| 939 |
+
sets of coefficients.
|
| 940 |
+
|
| 941 |
+
Returns
|
| 942 |
+
-------
|
| 943 |
+
values : ndarray, compatible object
|
| 944 |
+
The values of the two dimensional Legendre series at points formed
|
| 945 |
+
from pairs of corresponding values from `x` and `y`.
|
| 946 |
+
|
| 947 |
+
See Also
|
| 948 |
+
--------
|
| 949 |
+
legval, leggrid2d, legval3d, leggrid3d
|
| 950 |
+
"""
|
| 951 |
+
return pu._valnd(legval, c, x, y)
|
| 952 |
+
|
| 953 |
+
|
| 954 |
+
def leggrid2d(x, y, c):
|
| 955 |
+
"""
|
| 956 |
+
Evaluate a 2-D Legendre series on the Cartesian product of x and y.
|
| 957 |
+
|
| 958 |
+
This function returns the values:
|
| 959 |
+
|
| 960 |
+
.. math:: p(a,b) = \\sum_{i,j} c_{i,j} * L_i(a) * L_j(b)
|
| 961 |
+
|
| 962 |
+
where the points ``(a, b)`` consist of all pairs formed by taking
|
| 963 |
+
`a` from `x` and `b` from `y`. The resulting points form a grid with
|
| 964 |
+
`x` in the first dimension and `y` in the second.
|
| 965 |
+
|
| 966 |
+
The parameters `x` and `y` are converted to arrays only if they are
|
| 967 |
+
tuples or a lists, otherwise they are treated as a scalars. In either
|
| 968 |
+
case, either `x` and `y` or their elements must support multiplication
|
| 969 |
+
and addition both with themselves and with the elements of `c`.
|
| 970 |
+
|
| 971 |
+
If `c` has fewer than two dimensions, ones are implicitly appended to
|
| 972 |
+
its shape to make it 2-D. The shape of the result will be c.shape[2:] +
|
| 973 |
+
x.shape + y.shape.
|
| 974 |
+
|
| 975 |
+
Parameters
|
| 976 |
+
----------
|
| 977 |
+
x, y : array_like, compatible objects
|
| 978 |
+
The two dimensional series is evaluated at the points in the
|
| 979 |
+
Cartesian product of `x` and `y`. If `x` or `y` is a list or
|
| 980 |
+
tuple, it is first converted to an ndarray, otherwise it is left
|
| 981 |
+
unchanged and, if it isn't an ndarray, it is treated as a scalar.
|
| 982 |
+
c : array_like
|
| 983 |
+
Array of coefficients ordered so that the coefficient of the term of
|
| 984 |
+
multi-degree i,j is contained in ``c[i,j]``. If `c` has dimension
|
| 985 |
+
greater than two the remaining indices enumerate multiple sets of
|
| 986 |
+
coefficients.
|
| 987 |
+
|
| 988 |
+
Returns
|
| 989 |
+
-------
|
| 990 |
+
values : ndarray, compatible object
|
| 991 |
+
The values of the two dimensional Chebyshev series at points in the
|
| 992 |
+
Cartesian product of `x` and `y`.
|
| 993 |
+
|
| 994 |
+
See Also
|
| 995 |
+
--------
|
| 996 |
+
legval, legval2d, legval3d, leggrid3d
|
| 997 |
+
"""
|
| 998 |
+
return pu._gridnd(legval, c, x, y)
|
| 999 |
+
|
| 1000 |
+
|
| 1001 |
+
def legval3d(x, y, z, c):
|
| 1002 |
+
"""
|
| 1003 |
+
Evaluate a 3-D Legendre series at points (x, y, z).
|
| 1004 |
+
|
| 1005 |
+
This function returns the values:
|
| 1006 |
+
|
| 1007 |
+
.. math:: p(x,y,z) = \\sum_{i,j,k} c_{i,j,k} * L_i(x) * L_j(y) * L_k(z)
|
| 1008 |
+
|
| 1009 |
+
The parameters `x`, `y`, and `z` are converted to arrays only if
|
| 1010 |
+
they are tuples or a lists, otherwise they are treated as a scalars and
|
| 1011 |
+
they must have the same shape after conversion. In either case, either
|
| 1012 |
+
`x`, `y`, and `z` or their elements must support multiplication and
|
| 1013 |
+
addition both with themselves and with the elements of `c`.
|
| 1014 |
+
|
| 1015 |
+
If `c` has fewer than 3 dimensions, ones are implicitly appended to its
|
| 1016 |
+
shape to make it 3-D. The shape of the result will be c.shape[3:] +
|
| 1017 |
+
x.shape.
|
| 1018 |
+
|
| 1019 |
+
Parameters
|
| 1020 |
+
----------
|
| 1021 |
+
x, y, z : array_like, compatible object
|
| 1022 |
+
The three dimensional series is evaluated at the points
|
| 1023 |
+
``(x, y, z)``, where `x`, `y`, and `z` must have the same shape. If
|
| 1024 |
+
any of `x`, `y`, or `z` is a list or tuple, it is first converted
|
| 1025 |
+
to an ndarray, otherwise it is left unchanged and if it isn't an
|
| 1026 |
+
ndarray it is treated as a scalar.
|
| 1027 |
+
c : array_like
|
| 1028 |
+
Array of coefficients ordered so that the coefficient of the term of
|
| 1029 |
+
multi-degree i,j,k is contained in ``c[i,j,k]``. If `c` has dimension
|
| 1030 |
+
greater than 3 the remaining indices enumerate multiple sets of
|
| 1031 |
+
coefficients.
|
| 1032 |
+
|
| 1033 |
+
Returns
|
| 1034 |
+
-------
|
| 1035 |
+
values : ndarray, compatible object
|
| 1036 |
+
The values of the multidimensional polynomial on points formed with
|
| 1037 |
+
triples of corresponding values from `x`, `y`, and `z`.
|
| 1038 |
+
|
| 1039 |
+
See Also
|
| 1040 |
+
--------
|
| 1041 |
+
legval, legval2d, leggrid2d, leggrid3d
|
| 1042 |
+
"""
|
| 1043 |
+
return pu._valnd(legval, c, x, y, z)
|
| 1044 |
+
|
| 1045 |
+
|
| 1046 |
+
def leggrid3d(x, y, z, c):
|
| 1047 |
+
"""
|
| 1048 |
+
Evaluate a 3-D Legendre series on the Cartesian product of x, y, and z.
|
| 1049 |
+
|
| 1050 |
+
This function returns the values:
|
| 1051 |
+
|
| 1052 |
+
.. math:: p(a,b,c) = \\sum_{i,j,k} c_{i,j,k} * L_i(a) * L_j(b) * L_k(c)
|
| 1053 |
+
|
| 1054 |
+
where the points ``(a, b, c)`` consist of all triples formed by taking
|
| 1055 |
+
`a` from `x`, `b` from `y`, and `c` from `z`. The resulting points form
|
| 1056 |
+
a grid with `x` in the first dimension, `y` in the second, and `z` in
|
| 1057 |
+
the third.
|
| 1058 |
+
|
| 1059 |
+
The parameters `x`, `y`, and `z` are converted to arrays only if they
|
| 1060 |
+
are tuples or a lists, otherwise they are treated as a scalars. In
|
| 1061 |
+
either case, either `x`, `y`, and `z` or their elements must support
|
| 1062 |
+
multiplication and addition both with themselves and with the elements
|
| 1063 |
+
of `c`.
|
| 1064 |
+
|
| 1065 |
+
If `c` has fewer than three dimensions, ones are implicitly appended to
|
| 1066 |
+
its shape to make it 3-D. The shape of the result will be c.shape[3:] +
|
| 1067 |
+
x.shape + y.shape + z.shape.
|
| 1068 |
+
|
| 1069 |
+
Parameters
|
| 1070 |
+
----------
|
| 1071 |
+
x, y, z : array_like, compatible objects
|
| 1072 |
+
The three dimensional series is evaluated at the points in the
|
| 1073 |
+
Cartesian product of `x`, `y`, and `z`. If `x`, `y`, or `z` is a
|
| 1074 |
+
list or tuple, it is first converted to an ndarray, otherwise it is
|
| 1075 |
+
left unchanged and, if it isn't an ndarray, it is treated as a
|
| 1076 |
+
scalar.
|
| 1077 |
+
c : array_like
|
| 1078 |
+
Array of coefficients ordered so that the coefficients for terms of
|
| 1079 |
+
degree i,j are contained in ``c[i,j]``. If `c` has dimension
|
| 1080 |
+
greater than two the remaining indices enumerate multiple sets of
|
| 1081 |
+
coefficients.
|
| 1082 |
+
|
| 1083 |
+
Returns
|
| 1084 |
+
-------
|
| 1085 |
+
values : ndarray, compatible object
|
| 1086 |
+
The values of the two dimensional polynomial at points in the Cartesian
|
| 1087 |
+
product of `x` and `y`.
|
| 1088 |
+
|
| 1089 |
+
See Also
|
| 1090 |
+
--------
|
| 1091 |
+
legval, legval2d, leggrid2d, legval3d
|
| 1092 |
+
"""
|
| 1093 |
+
return pu._gridnd(legval, c, x, y, z)
|
| 1094 |
+
|
| 1095 |
+
|
| 1096 |
+
def legvander(x, deg):
|
| 1097 |
+
"""Pseudo-Vandermonde matrix of given degree.
|
| 1098 |
+
|
| 1099 |
+
Returns the pseudo-Vandermonde matrix of degree `deg` and sample points
|
| 1100 |
+
`x`. The pseudo-Vandermonde matrix is defined by
|
| 1101 |
+
|
| 1102 |
+
.. math:: V[..., i] = L_i(x)
|
| 1103 |
+
|
| 1104 |
+
where ``0 <= i <= deg``. The leading indices of `V` index the elements of
|
| 1105 |
+
`x` and the last index is the degree of the Legendre polynomial.
|
| 1106 |
+
|
| 1107 |
+
If `c` is a 1-D array of coefficients of length ``n + 1`` and `V` is the
|
| 1108 |
+
array ``V = legvander(x, n)``, then ``np.dot(V, c)`` and
|
| 1109 |
+
``legval(x, c)`` are the same up to roundoff. This equivalence is
|
| 1110 |
+
useful both for least squares fitting and for the evaluation of a large
|
| 1111 |
+
number of Legendre series of the same degree and sample points.
|
| 1112 |
+
|
| 1113 |
+
Parameters
|
| 1114 |
+
----------
|
| 1115 |
+
x : array_like
|
| 1116 |
+
Array of points. The dtype is converted to float64 or complex128
|
| 1117 |
+
depending on whether any of the elements are complex. If `x` is
|
| 1118 |
+
scalar it is converted to a 1-D array.
|
| 1119 |
+
deg : int
|
| 1120 |
+
Degree of the resulting matrix.
|
| 1121 |
+
|
| 1122 |
+
Returns
|
| 1123 |
+
-------
|
| 1124 |
+
vander : ndarray
|
| 1125 |
+
The pseudo-Vandermonde matrix. The shape of the returned matrix is
|
| 1126 |
+
``x.shape + (deg + 1,)``, where The last index is the degree of the
|
| 1127 |
+
corresponding Legendre polynomial. The dtype will be the same as
|
| 1128 |
+
the converted `x`.
|
| 1129 |
+
|
| 1130 |
+
"""
|
| 1131 |
+
ideg = pu._as_int(deg, "deg")
|
| 1132 |
+
if ideg < 0:
|
| 1133 |
+
raise ValueError("deg must be non-negative")
|
| 1134 |
+
|
| 1135 |
+
x = np.array(x, copy=None, ndmin=1) + 0.0
|
| 1136 |
+
dims = (ideg + 1,) + x.shape
|
| 1137 |
+
dtyp = x.dtype
|
| 1138 |
+
v = np.empty(dims, dtype=dtyp)
|
| 1139 |
+
# Use forward recursion to generate the entries. This is not as accurate
|
| 1140 |
+
# as reverse recursion in this application but it is more efficient.
|
| 1141 |
+
v[0] = x * 0 + 1
|
| 1142 |
+
if ideg > 0:
|
| 1143 |
+
v[1] = x
|
| 1144 |
+
for i in range(2, ideg + 1):
|
| 1145 |
+
v[i] = (v[i - 1] * x * (2 * i - 1) - v[i - 2] * (i - 1)) / i
|
| 1146 |
+
return np.moveaxis(v, 0, -1)
|
| 1147 |
+
|
| 1148 |
+
|
| 1149 |
+
def legvander2d(x, y, deg):
|
| 1150 |
+
"""Pseudo-Vandermonde matrix of given degrees.
|
| 1151 |
+
|
| 1152 |
+
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample
|
| 1153 |
+
points ``(x, y)``. The pseudo-Vandermonde matrix is defined by
|
| 1154 |
+
|
| 1155 |
+
.. math:: V[..., (deg[1] + 1)*i + j] = L_i(x) * L_j(y),
|
| 1156 |
+
|
| 1157 |
+
where ``0 <= i <= deg[0]`` and ``0 <= j <= deg[1]``. The leading indices of
|
| 1158 |
+
`V` index the points ``(x, y)`` and the last index encodes the degrees of
|
| 1159 |
+
the Legendre polynomials.
|
| 1160 |
+
|
| 1161 |
+
If ``V = legvander2d(x, y, [xdeg, ydeg])``, then the columns of `V`
|
| 1162 |
+
correspond to the elements of a 2-D coefficient array `c` of shape
|
| 1163 |
+
(xdeg + 1, ydeg + 1) in the order
|
| 1164 |
+
|
| 1165 |
+
.. math:: c_{00}, c_{01}, c_{02}, ... , c_{10}, c_{11}, c_{12}, ...
|
| 1166 |
+
|
| 1167 |
+
and ``np.dot(V, c.flat)`` and ``legval2d(x, y, c)`` will be the same
|
| 1168 |
+
up to roundoff. This equivalence is useful both for least squares
|
| 1169 |
+
fitting and for the evaluation of a large number of 2-D Legendre
|
| 1170 |
+
series of the same degrees and sample points.
|
| 1171 |
+
|
| 1172 |
+
Parameters
|
| 1173 |
+
----------
|
| 1174 |
+
x, y : array_like
|
| 1175 |
+
Arrays of point coordinates, all of the same shape. The dtypes
|
| 1176 |
+
will be converted to either float64 or complex128 depending on
|
| 1177 |
+
whether any of the elements are complex. Scalars are converted to
|
| 1178 |
+
1-D arrays.
|
| 1179 |
+
deg : list of ints
|
| 1180 |
+
List of maximum degrees of the form [x_deg, y_deg].
|
| 1181 |
+
|
| 1182 |
+
Returns
|
| 1183 |
+
-------
|
| 1184 |
+
vander2d : ndarray
|
| 1185 |
+
The shape of the returned matrix is ``x.shape + (order,)``, where
|
| 1186 |
+
:math:`order = (deg[0]+1)*(deg[1]+1)`. The dtype will be the same
|
| 1187 |
+
as the converted `x` and `y`.
|
| 1188 |
+
|
| 1189 |
+
See Also
|
| 1190 |
+
--------
|
| 1191 |
+
legvander, legvander3d, legval2d, legval3d
|
| 1192 |
+
"""
|
| 1193 |
+
return pu._vander_nd_flat((legvander, legvander), (x, y), deg)
|
| 1194 |
+
|
| 1195 |
+
|
| 1196 |
+
def legvander3d(x, y, z, deg):
|
| 1197 |
+
"""Pseudo-Vandermonde matrix of given degrees.
|
| 1198 |
+
|
| 1199 |
+
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample
|
| 1200 |
+
points ``(x, y, z)``. If `l`, `m`, `n` are the given degrees in `x`, `y`, `z`,
|
| 1201 |
+
then The pseudo-Vandermonde matrix is defined by
|
| 1202 |
+
|
| 1203 |
+
.. math:: V[..., (m+1)(n+1)i + (n+1)j + k] = L_i(x)*L_j(y)*L_k(z),
|
| 1204 |
+
|
| 1205 |
+
where ``0 <= i <= l``, ``0 <= j <= m``, and ``0 <= j <= n``. The leading
|
| 1206 |
+
indices of `V` index the points ``(x, y, z)`` and the last index encodes
|
| 1207 |
+
the degrees of the Legendre polynomials.
|
| 1208 |
+
|
| 1209 |
+
If ``V = legvander3d(x, y, z, [xdeg, ydeg, zdeg])``, then the columns
|
| 1210 |
+
of `V` correspond to the elements of a 3-D coefficient array `c` of
|
| 1211 |
+
shape (xdeg + 1, ydeg + 1, zdeg + 1) in the order
|
| 1212 |
+
|
| 1213 |
+
.. math:: c_{000}, c_{001}, c_{002},... , c_{010}, c_{011}, c_{012},...
|
| 1214 |
+
|
| 1215 |
+
and ``np.dot(V, c.flat)`` and ``legval3d(x, y, z, c)`` will be the
|
| 1216 |
+
same up to roundoff. This equivalence is useful both for least squares
|
| 1217 |
+
fitting and for the evaluation of a large number of 3-D Legendre
|
| 1218 |
+
series of the same degrees and sample points.
|
| 1219 |
+
|
| 1220 |
+
Parameters
|
| 1221 |
+
----------
|
| 1222 |
+
x, y, z : array_like
|
| 1223 |
+
Arrays of point coordinates, all of the same shape. The dtypes will
|
| 1224 |
+
be converted to either float64 or complex128 depending on whether
|
| 1225 |
+
any of the elements are complex. Scalars are converted to 1-D
|
| 1226 |
+
arrays.
|
| 1227 |
+
deg : list of ints
|
| 1228 |
+
List of maximum degrees of the form [x_deg, y_deg, z_deg].
|
| 1229 |
+
|
| 1230 |
+
Returns
|
| 1231 |
+
-------
|
| 1232 |
+
vander3d : ndarray
|
| 1233 |
+
The shape of the returned matrix is ``x.shape + (order,)``, where
|
| 1234 |
+
:math:`order = (deg[0]+1)*(deg[1]+1)*(deg[2]+1)`. The dtype will
|
| 1235 |
+
be the same as the converted `x`, `y`, and `z`.
|
| 1236 |
+
|
| 1237 |
+
See Also
|
| 1238 |
+
--------
|
| 1239 |
+
legvander, legvander3d, legval2d, legval3d
|
| 1240 |
+
"""
|
| 1241 |
+
return pu._vander_nd_flat((legvander, legvander, legvander), (x, y, z), deg)
|
| 1242 |
+
|
| 1243 |
+
|
| 1244 |
+
def legfit(x, y, deg, rcond=None, full=False, w=None):
|
| 1245 |
+
"""
|
| 1246 |
+
Least squares fit of Legendre series to data.
|
| 1247 |
+
|
| 1248 |
+
Return the coefficients of a Legendre series of degree `deg` that is the
|
| 1249 |
+
least squares fit to the data values `y` given at points `x`. If `y` is
|
| 1250 |
+
1-D the returned coefficients will also be 1-D. If `y` is 2-D multiple
|
| 1251 |
+
fits are done, one for each column of `y`, and the resulting
|
| 1252 |
+
coefficients are stored in the corresponding columns of a 2-D return.
|
| 1253 |
+
The fitted polynomial(s) are in the form
|
| 1254 |
+
|
| 1255 |
+
.. math:: p(x) = c_0 + c_1 * L_1(x) + ... + c_n * L_n(x),
|
| 1256 |
+
|
| 1257 |
+
where `n` is `deg`.
|
| 1258 |
+
|
| 1259 |
+
Parameters
|
| 1260 |
+
----------
|
| 1261 |
+
x : array_like, shape (M,)
|
| 1262 |
+
x-coordinates of the M sample points ``(x[i], y[i])``.
|
| 1263 |
+
y : array_like, shape (M,) or (M, K)
|
| 1264 |
+
y-coordinates of the sample points. Several data sets of sample
|
| 1265 |
+
points sharing the same x-coordinates can be fitted at once by
|
| 1266 |
+
passing in a 2D-array that contains one dataset per column.
|
| 1267 |
+
deg : int or 1-D array_like
|
| 1268 |
+
Degree(s) of the fitting polynomials. If `deg` is a single integer
|
| 1269 |
+
all terms up to and including the `deg`'th term are included in the
|
| 1270 |
+
fit. For NumPy versions >= 1.11.0 a list of integers specifying the
|
| 1271 |
+
degrees of the terms to include may be used instead.
|
| 1272 |
+
rcond : float, optional
|
| 1273 |
+
Relative condition number of the fit. Singular values smaller than
|
| 1274 |
+
this relative to the largest singular value will be ignored. The
|
| 1275 |
+
default value is len(x)*eps, where eps is the relative precision of
|
| 1276 |
+
the float type, about 2e-16 in most cases.
|
| 1277 |
+
full : bool, optional
|
| 1278 |
+
Switch determining nature of return value. When it is False (the
|
| 1279 |
+
default) just the coefficients are returned, when True diagnostic
|
| 1280 |
+
information from the singular value decomposition is also returned.
|
| 1281 |
+
w : array_like, shape (`M`,), optional
|
| 1282 |
+
Weights. If not None, the weight ``w[i]`` applies to the unsquared
|
| 1283 |
+
residual ``y[i] - y_hat[i]`` at ``x[i]``. Ideally the weights are
|
| 1284 |
+
chosen so that the errors of the products ``w[i]*y[i]`` all have the
|
| 1285 |
+
same variance. When using inverse-variance weighting, use
|
| 1286 |
+
``w[i] = 1/sigma(y[i])``. The default value is None.
|
| 1287 |
+
|
| 1288 |
+
Returns
|
| 1289 |
+
-------
|
| 1290 |
+
coef : ndarray, shape (M,) or (M, K)
|
| 1291 |
+
Legendre coefficients ordered from low to high. If `y` was
|
| 1292 |
+
2-D, the coefficients for the data in column k of `y` are in
|
| 1293 |
+
column `k`. If `deg` is specified as a list, coefficients for
|
| 1294 |
+
terms not included in the fit are set equal to zero in the
|
| 1295 |
+
returned `coef`.
|
| 1296 |
+
|
| 1297 |
+
[residuals, rank, singular_values, rcond] : list
|
| 1298 |
+
These values are only returned if ``full == True``
|
| 1299 |
+
|
| 1300 |
+
- residuals -- sum of squared residuals of the least squares fit
|
| 1301 |
+
- rank -- the numerical rank of the scaled Vandermonde matrix
|
| 1302 |
+
- singular_values -- singular values of the scaled Vandermonde matrix
|
| 1303 |
+
- rcond -- value of `rcond`.
|
| 1304 |
+
|
| 1305 |
+
For more details, see `numpy.linalg.lstsq`.
|
| 1306 |
+
|
| 1307 |
+
Warns
|
| 1308 |
+
-----
|
| 1309 |
+
RankWarning
|
| 1310 |
+
The rank of the coefficient matrix in the least-squares fit is
|
| 1311 |
+
deficient. The warning is only raised if ``full == False``. The
|
| 1312 |
+
warnings can be turned off by
|
| 1313 |
+
|
| 1314 |
+
>>> import warnings
|
| 1315 |
+
>>> warnings.simplefilter('ignore', np.exceptions.RankWarning)
|
| 1316 |
+
|
| 1317 |
+
See Also
|
| 1318 |
+
--------
|
| 1319 |
+
numpy.polynomial.polynomial.polyfit
|
| 1320 |
+
numpy.polynomial.chebyshev.chebfit
|
| 1321 |
+
numpy.polynomial.laguerre.lagfit
|
| 1322 |
+
numpy.polynomial.hermite.hermfit
|
| 1323 |
+
numpy.polynomial.hermite_e.hermefit
|
| 1324 |
+
legval : Evaluates a Legendre series.
|
| 1325 |
+
legvander : Vandermonde matrix of Legendre series.
|
| 1326 |
+
legweight : Legendre weight function (= 1).
|
| 1327 |
+
numpy.linalg.lstsq : Computes a least-squares fit from the matrix.
|
| 1328 |
+
scipy.interpolate.UnivariateSpline : Computes spline fits.
|
| 1329 |
+
|
| 1330 |
+
Notes
|
| 1331 |
+
-----
|
| 1332 |
+
The solution is the coefficients of the Legendre series `p` that
|
| 1333 |
+
minimizes the sum of the weighted squared errors
|
| 1334 |
+
|
| 1335 |
+
.. math:: E = \\sum_j w_j^2 * |y_j - p(x_j)|^2,
|
| 1336 |
+
|
| 1337 |
+
where :math:`w_j` are the weights. This problem is solved by setting up
|
| 1338 |
+
as the (typically) overdetermined matrix equation
|
| 1339 |
+
|
| 1340 |
+
.. math:: V(x) * c = w * y,
|
| 1341 |
+
|
| 1342 |
+
where `V` is the weighted pseudo Vandermonde matrix of `x`, `c` are the
|
| 1343 |
+
coefficients to be solved for, `w` are the weights, and `y` are the
|
| 1344 |
+
observed values. This equation is then solved using the singular value
|
| 1345 |
+
decomposition of `V`.
|
| 1346 |
+
|
| 1347 |
+
If some of the singular values of `V` are so small that they are
|
| 1348 |
+
neglected, then a `~exceptions.RankWarning` will be issued. This means that
|
| 1349 |
+
the coefficient values may be poorly determined. Using a lower order fit
|
| 1350 |
+
will usually get rid of the warning. The `rcond` parameter can also be
|
| 1351 |
+
set to a value smaller than its default, but the resulting fit may be
|
| 1352 |
+
spurious and have large contributions from roundoff error.
|
| 1353 |
+
|
| 1354 |
+
Fits using Legendre series are usually better conditioned than fits
|
| 1355 |
+
using power series, but much can depend on the distribution of the
|
| 1356 |
+
sample points and the smoothness of the data. If the quality of the fit
|
| 1357 |
+
is inadequate splines may be a good alternative.
|
| 1358 |
+
|
| 1359 |
+
References
|
| 1360 |
+
----------
|
| 1361 |
+
.. [1] Wikipedia, "Curve fitting",
|
| 1362 |
+
https://en.wikipedia.org/wiki/Curve_fitting
|
| 1363 |
+
|
| 1364 |
+
Examples
|
| 1365 |
+
--------
|
| 1366 |
+
|
| 1367 |
+
"""
|
| 1368 |
+
return pu._fit(legvander, x, y, deg, rcond, full, w)
|
| 1369 |
+
|
| 1370 |
+
|
| 1371 |
+
def legcompanion(c):
|
| 1372 |
+
"""Return the scaled companion matrix of c.
|
| 1373 |
+
|
| 1374 |
+
The basis polynomials are scaled so that the companion matrix is
|
| 1375 |
+
symmetric when `c` is an Legendre basis polynomial. This provides
|
| 1376 |
+
better eigenvalue estimates than the unscaled case and for basis
|
| 1377 |
+
polynomials the eigenvalues are guaranteed to be real if
|
| 1378 |
+
`numpy.linalg.eigvalsh` is used to obtain them.
|
| 1379 |
+
|
| 1380 |
+
Parameters
|
| 1381 |
+
----------
|
| 1382 |
+
c : array_like
|
| 1383 |
+
1-D array of Legendre series coefficients ordered from low to high
|
| 1384 |
+
degree.
|
| 1385 |
+
|
| 1386 |
+
Returns
|
| 1387 |
+
-------
|
| 1388 |
+
mat : ndarray
|
| 1389 |
+
Scaled companion matrix of dimensions (deg, deg).
|
| 1390 |
+
"""
|
| 1391 |
+
# c is a trimmed copy
|
| 1392 |
+
[c] = pu.as_series([c])
|
| 1393 |
+
if len(c) < 2:
|
| 1394 |
+
raise ValueError('Series must have maximum degree of at least 1.')
|
| 1395 |
+
if len(c) == 2:
|
| 1396 |
+
return np.array([[-c[0] / c[1]]])
|
| 1397 |
+
|
| 1398 |
+
n = len(c) - 1
|
| 1399 |
+
mat = np.zeros((n, n), dtype=c.dtype)
|
| 1400 |
+
scl = 1. / np.sqrt(2 * np.arange(n) + 1)
|
| 1401 |
+
top = mat.reshape(-1)[1::n + 1]
|
| 1402 |
+
bot = mat.reshape(-1)[n::n + 1]
|
| 1403 |
+
top[...] = np.arange(1, n) * scl[:n - 1] * scl[1:n]
|
| 1404 |
+
bot[...] = top
|
| 1405 |
+
mat[:, -1] -= (c[:-1] / c[-1]) * (scl / scl[-1]) * (n / (2 * n - 1))
|
| 1406 |
+
return mat
|
| 1407 |
+
|
| 1408 |
+
|
| 1409 |
+
def legroots(c):
|
| 1410 |
+
"""
|
| 1411 |
+
Compute the roots of a Legendre series.
|
| 1412 |
+
|
| 1413 |
+
Return the roots (a.k.a. "zeros") of the polynomial
|
| 1414 |
+
|
| 1415 |
+
.. math:: p(x) = \\sum_i c[i] * L_i(x).
|
| 1416 |
+
|
| 1417 |
+
Parameters
|
| 1418 |
+
----------
|
| 1419 |
+
c : 1-D array_like
|
| 1420 |
+
1-D array of coefficients.
|
| 1421 |
+
|
| 1422 |
+
Returns
|
| 1423 |
+
-------
|
| 1424 |
+
out : ndarray
|
| 1425 |
+
Array of the roots of the series. If all the roots are real,
|
| 1426 |
+
then `out` is also real, otherwise it is complex.
|
| 1427 |
+
|
| 1428 |
+
See Also
|
| 1429 |
+
--------
|
| 1430 |
+
numpy.polynomial.polynomial.polyroots
|
| 1431 |
+
numpy.polynomial.chebyshev.chebroots
|
| 1432 |
+
numpy.polynomial.laguerre.lagroots
|
| 1433 |
+
numpy.polynomial.hermite.hermroots
|
| 1434 |
+
numpy.polynomial.hermite_e.hermeroots
|
| 1435 |
+
|
| 1436 |
+
Notes
|
| 1437 |
+
-----
|
| 1438 |
+
The root estimates are obtained as the eigenvalues of the companion
|
| 1439 |
+
matrix, Roots far from the origin of the complex plane may have large
|
| 1440 |
+
errors due to the numerical instability of the series for such values.
|
| 1441 |
+
Roots with multiplicity greater than 1 will also show larger errors as
|
| 1442 |
+
the value of the series near such points is relatively insensitive to
|
| 1443 |
+
errors in the roots. Isolated roots near the origin can be improved by
|
| 1444 |
+
a few iterations of Newton's method.
|
| 1445 |
+
|
| 1446 |
+
The Legendre series basis polynomials aren't powers of ``x`` so the
|
| 1447 |
+
results of this function may seem unintuitive.
|
| 1448 |
+
|
| 1449 |
+
Examples
|
| 1450 |
+
--------
|
| 1451 |
+
>>> import numpy.polynomial.legendre as leg
|
| 1452 |
+
>>> leg.legroots((1, 2, 3, 4)) # 4L_3 + 3L_2 + 2L_1 + 1L_0, all real roots
|
| 1453 |
+
array([-0.85099543, -0.11407192, 0.51506735]) # may vary
|
| 1454 |
+
|
| 1455 |
+
"""
|
| 1456 |
+
# c is a trimmed copy
|
| 1457 |
+
[c] = pu.as_series([c])
|
| 1458 |
+
if len(c) < 2:
|
| 1459 |
+
return np.array([], dtype=c.dtype)
|
| 1460 |
+
if len(c) == 2:
|
| 1461 |
+
return np.array([-c[0] / c[1]])
|
| 1462 |
+
|
| 1463 |
+
# rotated companion matrix reduces error
|
| 1464 |
+
m = legcompanion(c)[::-1, ::-1]
|
| 1465 |
+
r = np.linalg.eigvals(m)
|
| 1466 |
+
r.sort()
|
| 1467 |
+
return r
|
| 1468 |
+
|
| 1469 |
+
|
| 1470 |
+
def leggauss(deg):
|
| 1471 |
+
"""
|
| 1472 |
+
Gauss-Legendre quadrature.
|
| 1473 |
+
|
| 1474 |
+
Computes the sample points and weights for Gauss-Legendre quadrature.
|
| 1475 |
+
These sample points and weights will correctly integrate polynomials of
|
| 1476 |
+
degree :math:`2*deg - 1` or less over the interval :math:`[-1, 1]` with
|
| 1477 |
+
the weight function :math:`f(x) = 1`.
|
| 1478 |
+
|
| 1479 |
+
Parameters
|
| 1480 |
+
----------
|
| 1481 |
+
deg : int
|
| 1482 |
+
Number of sample points and weights. It must be >= 1.
|
| 1483 |
+
|
| 1484 |
+
Returns
|
| 1485 |
+
-------
|
| 1486 |
+
x : ndarray
|
| 1487 |
+
1-D ndarray containing the sample points.
|
| 1488 |
+
y : ndarray
|
| 1489 |
+
1-D ndarray containing the weights.
|
| 1490 |
+
|
| 1491 |
+
Notes
|
| 1492 |
+
-----
|
| 1493 |
+
The results have only been tested up to degree 100, higher degrees may
|
| 1494 |
+
be problematic. The weights are determined by using the fact that
|
| 1495 |
+
|
| 1496 |
+
.. math:: w_k = c / (L'_n(x_k) * L_{n-1}(x_k))
|
| 1497 |
+
|
| 1498 |
+
where :math:`c` is a constant independent of :math:`k` and :math:`x_k`
|
| 1499 |
+
is the k'th root of :math:`L_n`, and then scaling the results to get
|
| 1500 |
+
the right value when integrating 1.
|
| 1501 |
+
|
| 1502 |
+
"""
|
| 1503 |
+
ideg = pu._as_int(deg, "deg")
|
| 1504 |
+
if ideg <= 0:
|
| 1505 |
+
raise ValueError("deg must be a positive integer")
|
| 1506 |
+
|
| 1507 |
+
# first approximation of roots. We use the fact that the companion
|
| 1508 |
+
# matrix is symmetric in this case in order to obtain better zeros.
|
| 1509 |
+
c = np.array([0] * deg + [1])
|
| 1510 |
+
m = legcompanion(c)
|
| 1511 |
+
x = np.linalg.eigvalsh(m)
|
| 1512 |
+
|
| 1513 |
+
# improve roots by one application of Newton
|
| 1514 |
+
dy = legval(x, c)
|
| 1515 |
+
df = legval(x, legder(c))
|
| 1516 |
+
x -= dy / df
|
| 1517 |
+
|
| 1518 |
+
# compute the weights. We scale the factor to avoid possible numerical
|
| 1519 |
+
# overflow.
|
| 1520 |
+
fm = legval(x, c[1:])
|
| 1521 |
+
fm /= np.abs(fm).max()
|
| 1522 |
+
df /= np.abs(df).max()
|
| 1523 |
+
w = 1 / (fm * df)
|
| 1524 |
+
|
| 1525 |
+
# for Legendre we can also symmetrize
|
| 1526 |
+
w = (w + w[::-1]) / 2
|
| 1527 |
+
x = (x - x[::-1]) / 2
|
| 1528 |
+
|
| 1529 |
+
# scale w to get the right value
|
| 1530 |
+
w *= 2. / w.sum()
|
| 1531 |
+
|
| 1532 |
+
return x, w
|
| 1533 |
+
|
| 1534 |
+
|
| 1535 |
+
def legweight(x):
|
| 1536 |
+
"""
|
| 1537 |
+
Weight function of the Legendre polynomials.
|
| 1538 |
+
|
| 1539 |
+
The weight function is :math:`1` and the interval of integration is
|
| 1540 |
+
:math:`[-1, 1]`. The Legendre polynomials are orthogonal, but not
|
| 1541 |
+
normalized, with respect to this weight function.
|
| 1542 |
+
|
| 1543 |
+
Parameters
|
| 1544 |
+
----------
|
| 1545 |
+
x : array_like
|
| 1546 |
+
Values at which the weight function will be computed.
|
| 1547 |
+
|
| 1548 |
+
Returns
|
| 1549 |
+
-------
|
| 1550 |
+
w : ndarray
|
| 1551 |
+
The weight function at `x`.
|
| 1552 |
+
"""
|
| 1553 |
+
w = x * 0.0 + 1.0
|
| 1554 |
+
return w
|
| 1555 |
+
|
| 1556 |
+
#
|
| 1557 |
+
# Legendre series class
|
| 1558 |
+
#
|
| 1559 |
+
|
| 1560 |
+
class Legendre(ABCPolyBase):
|
| 1561 |
+
"""A Legendre series class.
|
| 1562 |
+
|
| 1563 |
+
The Legendre class provides the standard Python numerical methods
|
| 1564 |
+
'+', '-', '*', '//', '%', 'divmod', '**', and '()' as well as the
|
| 1565 |
+
attributes and methods listed below.
|
| 1566 |
+
|
| 1567 |
+
Parameters
|
| 1568 |
+
----------
|
| 1569 |
+
coef : array_like
|
| 1570 |
+
Legendre coefficients in order of increasing degree, i.e.,
|
| 1571 |
+
``(1, 2, 3)`` gives ``1*P_0(x) + 2*P_1(x) + 3*P_2(x)``.
|
| 1572 |
+
domain : (2,) array_like, optional
|
| 1573 |
+
Domain to use. The interval ``[domain[0], domain[1]]`` is mapped
|
| 1574 |
+
to the interval ``[window[0], window[1]]`` by shifting and scaling.
|
| 1575 |
+
The default value is [-1., 1.].
|
| 1576 |
+
window : (2,) array_like, optional
|
| 1577 |
+
Window, see `domain` for its use. The default value is [-1., 1.].
|
| 1578 |
+
symbol : str, optional
|
| 1579 |
+
Symbol used to represent the independent variable in string
|
| 1580 |
+
representations of the polynomial expression, e.g. for printing.
|
| 1581 |
+
The symbol must be a valid Python identifier. Default value is 'x'.
|
| 1582 |
+
|
| 1583 |
+
.. versionadded:: 1.24
|
| 1584 |
+
|
| 1585 |
+
"""
|
| 1586 |
+
# Virtual Functions
|
| 1587 |
+
_add = staticmethod(legadd)
|
| 1588 |
+
_sub = staticmethod(legsub)
|
| 1589 |
+
_mul = staticmethod(legmul)
|
| 1590 |
+
_div = staticmethod(legdiv)
|
| 1591 |
+
_pow = staticmethod(legpow)
|
| 1592 |
+
_val = staticmethod(legval)
|
| 1593 |
+
_int = staticmethod(legint)
|
| 1594 |
+
_der = staticmethod(legder)
|
| 1595 |
+
_fit = staticmethod(legfit)
|
| 1596 |
+
_line = staticmethod(legline)
|
| 1597 |
+
_roots = staticmethod(legroots)
|
| 1598 |
+
_fromroots = staticmethod(legfromroots)
|
| 1599 |
+
|
| 1600 |
+
# Virtual properties
|
| 1601 |
+
domain = np.array(legdomain)
|
| 1602 |
+
window = np.array(legdomain)
|
| 1603 |
+
basis_name = 'P'
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/legendre.pyi
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, ClassVar, Final, Literal as L
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
from ._polybase import ABCPolyBase
|
| 6 |
+
from ._polytypes import (
|
| 7 |
+
_Array1,
|
| 8 |
+
_Array2,
|
| 9 |
+
_FuncBinOp,
|
| 10 |
+
_FuncCompanion,
|
| 11 |
+
_FuncDer,
|
| 12 |
+
_FuncFit,
|
| 13 |
+
_FuncFromRoots,
|
| 14 |
+
_FuncGauss,
|
| 15 |
+
_FuncInteg,
|
| 16 |
+
_FuncLine,
|
| 17 |
+
_FuncPoly2Ortho,
|
| 18 |
+
_FuncPow,
|
| 19 |
+
_FuncRoots,
|
| 20 |
+
_FuncUnOp,
|
| 21 |
+
_FuncVal,
|
| 22 |
+
_FuncVal2D,
|
| 23 |
+
_FuncVal3D,
|
| 24 |
+
_FuncVander,
|
| 25 |
+
_FuncVander2D,
|
| 26 |
+
_FuncVander3D,
|
| 27 |
+
_FuncWeight,
|
| 28 |
+
)
|
| 29 |
+
from .polyutils import trimcoef as legtrim
|
| 30 |
+
|
| 31 |
+
__all__ = [
|
| 32 |
+
"legzero",
|
| 33 |
+
"legone",
|
| 34 |
+
"legx",
|
| 35 |
+
"legdomain",
|
| 36 |
+
"legline",
|
| 37 |
+
"legadd",
|
| 38 |
+
"legsub",
|
| 39 |
+
"legmulx",
|
| 40 |
+
"legmul",
|
| 41 |
+
"legdiv",
|
| 42 |
+
"legpow",
|
| 43 |
+
"legval",
|
| 44 |
+
"legder",
|
| 45 |
+
"legint",
|
| 46 |
+
"leg2poly",
|
| 47 |
+
"poly2leg",
|
| 48 |
+
"legfromroots",
|
| 49 |
+
"legvander",
|
| 50 |
+
"legfit",
|
| 51 |
+
"legtrim",
|
| 52 |
+
"legroots",
|
| 53 |
+
"Legendre",
|
| 54 |
+
"legval2d",
|
| 55 |
+
"legval3d",
|
| 56 |
+
"leggrid2d",
|
| 57 |
+
"leggrid3d",
|
| 58 |
+
"legvander2d",
|
| 59 |
+
"legvander3d",
|
| 60 |
+
"legcompanion",
|
| 61 |
+
"leggauss",
|
| 62 |
+
"legweight",
|
| 63 |
+
]
|
| 64 |
+
|
| 65 |
+
poly2leg: Final[_FuncPoly2Ortho] = ...
|
| 66 |
+
leg2poly: Final[_FuncUnOp] = ...
|
| 67 |
+
|
| 68 |
+
legdomain: Final[_Array2[np.float64]] = ...
|
| 69 |
+
legzero: Final[_Array1[np.int_]] = ...
|
| 70 |
+
legone: Final[_Array1[np.int_]] = ...
|
| 71 |
+
legx: Final[_Array2[np.int_]] = ...
|
| 72 |
+
|
| 73 |
+
legline: Final[_FuncLine] = ...
|
| 74 |
+
legfromroots: Final[_FuncFromRoots] = ...
|
| 75 |
+
legadd: Final[_FuncBinOp] = ...
|
| 76 |
+
legsub: Final[_FuncBinOp] = ...
|
| 77 |
+
legmulx: Final[_FuncUnOp] = ...
|
| 78 |
+
legmul: Final[_FuncBinOp] = ...
|
| 79 |
+
legdiv: Final[_FuncBinOp] = ...
|
| 80 |
+
legpow: Final[_FuncPow] = ...
|
| 81 |
+
legder: Final[_FuncDer] = ...
|
| 82 |
+
legint: Final[_FuncInteg] = ...
|
| 83 |
+
legval: Final[_FuncVal] = ...
|
| 84 |
+
legval2d: Final[_FuncVal2D] = ...
|
| 85 |
+
legval3d: Final[_FuncVal3D] = ...
|
| 86 |
+
leggrid2d: Final[_FuncVal2D] = ...
|
| 87 |
+
leggrid3d: Final[_FuncVal3D] = ...
|
| 88 |
+
legvander: Final[_FuncVander] = ...
|
| 89 |
+
legvander2d: Final[_FuncVander2D] = ...
|
| 90 |
+
legvander3d: Final[_FuncVander3D] = ...
|
| 91 |
+
legfit: Final[_FuncFit] = ...
|
| 92 |
+
legcompanion: Final[_FuncCompanion] = ...
|
| 93 |
+
legroots: Final[_FuncRoots] = ...
|
| 94 |
+
leggauss: Final[_FuncGauss] = ...
|
| 95 |
+
legweight: Final[_FuncWeight] = ...
|
| 96 |
+
|
| 97 |
+
class Legendre(ABCPolyBase[L["P"]]):
|
| 98 |
+
basis_name: ClassVar[L["P"]] = "P" # pyright: ignore[reportIncompatibleMethodOverride]
|
| 99 |
+
domain: _Array2[np.float64 | Any] = ... # pyright: ignore[reportIncompatibleMethodOverride]
|
| 100 |
+
window: _Array2[np.float64 | Any] = ... # pyright: ignore[reportIncompatibleMethodOverride]
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/polynomial.py
ADDED
|
@@ -0,0 +1,1625 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
=================================================
|
| 3 |
+
Power Series (:mod:`numpy.polynomial.polynomial`)
|
| 4 |
+
=================================================
|
| 5 |
+
|
| 6 |
+
This module provides a number of objects (mostly functions) useful for
|
| 7 |
+
dealing with polynomials, including a `Polynomial` class that
|
| 8 |
+
encapsulates the usual arithmetic operations. (General information
|
| 9 |
+
on how this module represents and works with polynomial objects is in
|
| 10 |
+
the docstring for its "parent" sub-package, `numpy.polynomial`).
|
| 11 |
+
|
| 12 |
+
Classes
|
| 13 |
+
-------
|
| 14 |
+
.. autosummary::
|
| 15 |
+
:toctree: generated/
|
| 16 |
+
|
| 17 |
+
Polynomial
|
| 18 |
+
|
| 19 |
+
Constants
|
| 20 |
+
---------
|
| 21 |
+
.. autosummary::
|
| 22 |
+
:toctree: generated/
|
| 23 |
+
|
| 24 |
+
polydomain
|
| 25 |
+
polyzero
|
| 26 |
+
polyone
|
| 27 |
+
polyx
|
| 28 |
+
|
| 29 |
+
Arithmetic
|
| 30 |
+
----------
|
| 31 |
+
.. autosummary::
|
| 32 |
+
:toctree: generated/
|
| 33 |
+
|
| 34 |
+
polyadd
|
| 35 |
+
polysub
|
| 36 |
+
polymulx
|
| 37 |
+
polymul
|
| 38 |
+
polydiv
|
| 39 |
+
polypow
|
| 40 |
+
polyval
|
| 41 |
+
polyval2d
|
| 42 |
+
polyval3d
|
| 43 |
+
polygrid2d
|
| 44 |
+
polygrid3d
|
| 45 |
+
|
| 46 |
+
Calculus
|
| 47 |
+
--------
|
| 48 |
+
.. autosummary::
|
| 49 |
+
:toctree: generated/
|
| 50 |
+
|
| 51 |
+
polyder
|
| 52 |
+
polyint
|
| 53 |
+
|
| 54 |
+
Misc Functions
|
| 55 |
+
--------------
|
| 56 |
+
.. autosummary::
|
| 57 |
+
:toctree: generated/
|
| 58 |
+
|
| 59 |
+
polyfromroots
|
| 60 |
+
polyroots
|
| 61 |
+
polyvalfromroots
|
| 62 |
+
polyvander
|
| 63 |
+
polyvander2d
|
| 64 |
+
polyvander3d
|
| 65 |
+
polycompanion
|
| 66 |
+
polyfit
|
| 67 |
+
polytrim
|
| 68 |
+
polyline
|
| 69 |
+
|
| 70 |
+
See Also
|
| 71 |
+
--------
|
| 72 |
+
`numpy.polynomial`
|
| 73 |
+
|
| 74 |
+
"""
|
| 75 |
+
__all__ = [
|
| 76 |
+
'polyzero', 'polyone', 'polyx', 'polydomain', 'polyline', 'polyadd',
|
| 77 |
+
'polysub', 'polymulx', 'polymul', 'polydiv', 'polypow', 'polyval',
|
| 78 |
+
'polyvalfromroots', 'polyder', 'polyint', 'polyfromroots', 'polyvander',
|
| 79 |
+
'polyfit', 'polytrim', 'polyroots', 'Polynomial', 'polyval2d', 'polyval3d',
|
| 80 |
+
'polygrid2d', 'polygrid3d', 'polyvander2d', 'polyvander3d',
|
| 81 |
+
'polycompanion']
|
| 82 |
+
|
| 83 |
+
import numpy as np
|
| 84 |
+
from numpy._core.overrides import array_function_dispatch as _array_function_dispatch
|
| 85 |
+
|
| 86 |
+
from . import polyutils as pu
|
| 87 |
+
from ._polybase import ABCPolyBase
|
| 88 |
+
|
| 89 |
+
polytrim = pu.trimcoef
|
| 90 |
+
|
| 91 |
+
#
|
| 92 |
+
# These are constant arrays are of integer type so as to be compatible
|
| 93 |
+
# with the widest range of other types, such as Decimal.
|
| 94 |
+
#
|
| 95 |
+
|
| 96 |
+
# Polynomial default domain.
|
| 97 |
+
polydomain = np.array([-1., 1.])
|
| 98 |
+
|
| 99 |
+
# Polynomial coefficients representing zero.
|
| 100 |
+
polyzero = np.array([0])
|
| 101 |
+
|
| 102 |
+
# Polynomial coefficients representing one.
|
| 103 |
+
polyone = np.array([1])
|
| 104 |
+
|
| 105 |
+
# Polynomial coefficients representing the identity x.
|
| 106 |
+
polyx = np.array([0, 1])
|
| 107 |
+
|
| 108 |
+
#
|
| 109 |
+
# Polynomial series functions
|
| 110 |
+
#
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
def polyline(off, scl):
|
| 114 |
+
"""
|
| 115 |
+
Returns an array representing a linear polynomial.
|
| 116 |
+
|
| 117 |
+
Parameters
|
| 118 |
+
----------
|
| 119 |
+
off, scl : scalars
|
| 120 |
+
The "y-intercept" and "slope" of the line, respectively.
|
| 121 |
+
|
| 122 |
+
Returns
|
| 123 |
+
-------
|
| 124 |
+
y : ndarray
|
| 125 |
+
This module's representation of the linear polynomial ``off +
|
| 126 |
+
scl*x``.
|
| 127 |
+
|
| 128 |
+
See Also
|
| 129 |
+
--------
|
| 130 |
+
numpy.polynomial.chebyshev.chebline
|
| 131 |
+
numpy.polynomial.legendre.legline
|
| 132 |
+
numpy.polynomial.laguerre.lagline
|
| 133 |
+
numpy.polynomial.hermite.hermline
|
| 134 |
+
numpy.polynomial.hermite_e.hermeline
|
| 135 |
+
|
| 136 |
+
Examples
|
| 137 |
+
--------
|
| 138 |
+
>>> from numpy.polynomial import polynomial as P
|
| 139 |
+
>>> P.polyline(1, -1)
|
| 140 |
+
array([ 1, -1])
|
| 141 |
+
>>> P.polyval(1, P.polyline(1, -1)) # should be 0
|
| 142 |
+
0.0
|
| 143 |
+
|
| 144 |
+
"""
|
| 145 |
+
if scl != 0:
|
| 146 |
+
return np.array([off, scl])
|
| 147 |
+
else:
|
| 148 |
+
return np.array([off])
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
def polyfromroots(roots):
|
| 152 |
+
"""
|
| 153 |
+
Generate a monic polynomial with given roots.
|
| 154 |
+
|
| 155 |
+
Return the coefficients of the polynomial
|
| 156 |
+
|
| 157 |
+
.. math:: p(x) = (x - r_0) * (x - r_1) * ... * (x - r_n),
|
| 158 |
+
|
| 159 |
+
where the :math:`r_n` are the roots specified in `roots`. If a zero has
|
| 160 |
+
multiplicity n, then it must appear in `roots` n times. For instance,
|
| 161 |
+
if 2 is a root of multiplicity three and 3 is a root of multiplicity 2,
|
| 162 |
+
then `roots` looks something like [2, 2, 2, 3, 3]. The roots can appear
|
| 163 |
+
in any order.
|
| 164 |
+
|
| 165 |
+
If the returned coefficients are `c`, then
|
| 166 |
+
|
| 167 |
+
.. math:: p(x) = c_0 + c_1 * x + ... + x^n
|
| 168 |
+
|
| 169 |
+
The coefficient of the last term is 1 for monic polynomials in this
|
| 170 |
+
form.
|
| 171 |
+
|
| 172 |
+
Parameters
|
| 173 |
+
----------
|
| 174 |
+
roots : array_like
|
| 175 |
+
Sequence containing the roots.
|
| 176 |
+
|
| 177 |
+
Returns
|
| 178 |
+
-------
|
| 179 |
+
out : ndarray
|
| 180 |
+
1-D array of the polynomial's coefficients If all the roots are
|
| 181 |
+
real, then `out` is also real, otherwise it is complex. (see
|
| 182 |
+
Examples below).
|
| 183 |
+
|
| 184 |
+
See Also
|
| 185 |
+
--------
|
| 186 |
+
numpy.polynomial.chebyshev.chebfromroots
|
| 187 |
+
numpy.polynomial.legendre.legfromroots
|
| 188 |
+
numpy.polynomial.laguerre.lagfromroots
|
| 189 |
+
numpy.polynomial.hermite.hermfromroots
|
| 190 |
+
numpy.polynomial.hermite_e.hermefromroots
|
| 191 |
+
|
| 192 |
+
Notes
|
| 193 |
+
-----
|
| 194 |
+
The coefficients are determined by multiplying together linear factors
|
| 195 |
+
of the form ``(x - r_i)``, i.e.
|
| 196 |
+
|
| 197 |
+
.. math:: p(x) = (x - r_0) (x - r_1) ... (x - r_n)
|
| 198 |
+
|
| 199 |
+
where ``n == len(roots) - 1``; note that this implies that ``1`` is always
|
| 200 |
+
returned for :math:`a_n`.
|
| 201 |
+
|
| 202 |
+
Examples
|
| 203 |
+
--------
|
| 204 |
+
>>> from numpy.polynomial import polynomial as P
|
| 205 |
+
>>> P.polyfromroots((-1,0,1)) # x(x - 1)(x + 1) = x^3 - x
|
| 206 |
+
array([ 0., -1., 0., 1.])
|
| 207 |
+
>>> j = complex(0,1)
|
| 208 |
+
>>> P.polyfromroots((-j,j)) # complex returned, though values are real
|
| 209 |
+
array([1.+0.j, 0.+0.j, 1.+0.j])
|
| 210 |
+
|
| 211 |
+
"""
|
| 212 |
+
return pu._fromroots(polyline, polymul, roots)
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
def polyadd(c1, c2):
|
| 216 |
+
"""
|
| 217 |
+
Add one polynomial to another.
|
| 218 |
+
|
| 219 |
+
Returns the sum of two polynomials `c1` + `c2`. The arguments are
|
| 220 |
+
sequences of coefficients from lowest order term to highest, i.e.,
|
| 221 |
+
[1,2,3] represents the polynomial ``1 + 2*x + 3*x**2``.
|
| 222 |
+
|
| 223 |
+
Parameters
|
| 224 |
+
----------
|
| 225 |
+
c1, c2 : array_like
|
| 226 |
+
1-D arrays of polynomial coefficients ordered from low to high.
|
| 227 |
+
|
| 228 |
+
Returns
|
| 229 |
+
-------
|
| 230 |
+
out : ndarray
|
| 231 |
+
The coefficient array representing their sum.
|
| 232 |
+
|
| 233 |
+
See Also
|
| 234 |
+
--------
|
| 235 |
+
polysub, polymulx, polymul, polydiv, polypow
|
| 236 |
+
|
| 237 |
+
Examples
|
| 238 |
+
--------
|
| 239 |
+
>>> from numpy.polynomial import polynomial as P
|
| 240 |
+
>>> c1 = (1, 2, 3)
|
| 241 |
+
>>> c2 = (3, 2, 1)
|
| 242 |
+
>>> sum = P.polyadd(c1,c2); sum
|
| 243 |
+
array([4., 4., 4.])
|
| 244 |
+
>>> P.polyval(2, sum) # 4 + 4(2) + 4(2**2)
|
| 245 |
+
28.0
|
| 246 |
+
|
| 247 |
+
"""
|
| 248 |
+
return pu._add(c1, c2)
|
| 249 |
+
|
| 250 |
+
|
| 251 |
+
def polysub(c1, c2):
|
| 252 |
+
"""
|
| 253 |
+
Subtract one polynomial from another.
|
| 254 |
+
|
| 255 |
+
Returns the difference of two polynomials `c1` - `c2`. The arguments
|
| 256 |
+
are sequences of coefficients from lowest order term to highest, i.e.,
|
| 257 |
+
[1,2,3] represents the polynomial ``1 + 2*x + 3*x**2``.
|
| 258 |
+
|
| 259 |
+
Parameters
|
| 260 |
+
----------
|
| 261 |
+
c1, c2 : array_like
|
| 262 |
+
1-D arrays of polynomial coefficients ordered from low to
|
| 263 |
+
high.
|
| 264 |
+
|
| 265 |
+
Returns
|
| 266 |
+
-------
|
| 267 |
+
out : ndarray
|
| 268 |
+
Of coefficients representing their difference.
|
| 269 |
+
|
| 270 |
+
See Also
|
| 271 |
+
--------
|
| 272 |
+
polyadd, polymulx, polymul, polydiv, polypow
|
| 273 |
+
|
| 274 |
+
Examples
|
| 275 |
+
--------
|
| 276 |
+
>>> from numpy.polynomial import polynomial as P
|
| 277 |
+
>>> c1 = (1, 2, 3)
|
| 278 |
+
>>> c2 = (3, 2, 1)
|
| 279 |
+
>>> P.polysub(c1,c2)
|
| 280 |
+
array([-2., 0., 2.])
|
| 281 |
+
>>> P.polysub(c2, c1) # -P.polysub(c1,c2)
|
| 282 |
+
array([ 2., 0., -2.])
|
| 283 |
+
|
| 284 |
+
"""
|
| 285 |
+
return pu._sub(c1, c2)
|
| 286 |
+
|
| 287 |
+
|
| 288 |
+
def polymulx(c):
|
| 289 |
+
"""Multiply a polynomial by x.
|
| 290 |
+
|
| 291 |
+
Multiply the polynomial `c` by x, where x is the independent
|
| 292 |
+
variable.
|
| 293 |
+
|
| 294 |
+
|
| 295 |
+
Parameters
|
| 296 |
+
----------
|
| 297 |
+
c : array_like
|
| 298 |
+
1-D array of polynomial coefficients ordered from low to
|
| 299 |
+
high.
|
| 300 |
+
|
| 301 |
+
Returns
|
| 302 |
+
-------
|
| 303 |
+
out : ndarray
|
| 304 |
+
Array representing the result of the multiplication.
|
| 305 |
+
|
| 306 |
+
See Also
|
| 307 |
+
--------
|
| 308 |
+
polyadd, polysub, polymul, polydiv, polypow
|
| 309 |
+
|
| 310 |
+
Examples
|
| 311 |
+
--------
|
| 312 |
+
>>> from numpy.polynomial import polynomial as P
|
| 313 |
+
>>> c = (1, 2, 3)
|
| 314 |
+
>>> P.polymulx(c)
|
| 315 |
+
array([0., 1., 2., 3.])
|
| 316 |
+
|
| 317 |
+
"""
|
| 318 |
+
# c is a trimmed copy
|
| 319 |
+
[c] = pu.as_series([c])
|
| 320 |
+
# The zero series needs special treatment
|
| 321 |
+
if len(c) == 1 and c[0] == 0:
|
| 322 |
+
return c
|
| 323 |
+
|
| 324 |
+
prd = np.empty(len(c) + 1, dtype=c.dtype)
|
| 325 |
+
prd[0] = c[0] * 0
|
| 326 |
+
prd[1:] = c
|
| 327 |
+
return prd
|
| 328 |
+
|
| 329 |
+
|
| 330 |
+
def polymul(c1, c2):
|
| 331 |
+
"""
|
| 332 |
+
Multiply one polynomial by another.
|
| 333 |
+
|
| 334 |
+
Returns the product of two polynomials `c1` * `c2`. The arguments are
|
| 335 |
+
sequences of coefficients, from lowest order term to highest, e.g.,
|
| 336 |
+
[1,2,3] represents the polynomial ``1 + 2*x + 3*x**2.``
|
| 337 |
+
|
| 338 |
+
Parameters
|
| 339 |
+
----------
|
| 340 |
+
c1, c2 : array_like
|
| 341 |
+
1-D arrays of coefficients representing a polynomial, relative to the
|
| 342 |
+
"standard" basis, and ordered from lowest order term to highest.
|
| 343 |
+
|
| 344 |
+
Returns
|
| 345 |
+
-------
|
| 346 |
+
out : ndarray
|
| 347 |
+
Of the coefficients of their product.
|
| 348 |
+
|
| 349 |
+
See Also
|
| 350 |
+
--------
|
| 351 |
+
polyadd, polysub, polymulx, polydiv, polypow
|
| 352 |
+
|
| 353 |
+
Examples
|
| 354 |
+
--------
|
| 355 |
+
>>> from numpy.polynomial import polynomial as P
|
| 356 |
+
>>> c1 = (1, 2, 3)
|
| 357 |
+
>>> c2 = (3, 2, 1)
|
| 358 |
+
>>> P.polymul(c1, c2)
|
| 359 |
+
array([ 3., 8., 14., 8., 3.])
|
| 360 |
+
|
| 361 |
+
"""
|
| 362 |
+
# c1, c2 are trimmed copies
|
| 363 |
+
[c1, c2] = pu.as_series([c1, c2])
|
| 364 |
+
ret = np.convolve(c1, c2)
|
| 365 |
+
return pu.trimseq(ret)
|
| 366 |
+
|
| 367 |
+
|
| 368 |
+
def polydiv(c1, c2):
|
| 369 |
+
"""
|
| 370 |
+
Divide one polynomial by another.
|
| 371 |
+
|
| 372 |
+
Returns the quotient-with-remainder of two polynomials `c1` / `c2`.
|
| 373 |
+
The arguments are sequences of coefficients, from lowest order term
|
| 374 |
+
to highest, e.g., [1,2,3] represents ``1 + 2*x + 3*x**2``.
|
| 375 |
+
|
| 376 |
+
Parameters
|
| 377 |
+
----------
|
| 378 |
+
c1, c2 : array_like
|
| 379 |
+
1-D arrays of polynomial coefficients ordered from low to high.
|
| 380 |
+
|
| 381 |
+
Returns
|
| 382 |
+
-------
|
| 383 |
+
[quo, rem] : ndarrays
|
| 384 |
+
Of coefficient series representing the quotient and remainder.
|
| 385 |
+
|
| 386 |
+
See Also
|
| 387 |
+
--------
|
| 388 |
+
polyadd, polysub, polymulx, polymul, polypow
|
| 389 |
+
|
| 390 |
+
Examples
|
| 391 |
+
--------
|
| 392 |
+
>>> from numpy.polynomial import polynomial as P
|
| 393 |
+
>>> c1 = (1, 2, 3)
|
| 394 |
+
>>> c2 = (3, 2, 1)
|
| 395 |
+
>>> P.polydiv(c1, c2)
|
| 396 |
+
(array([3.]), array([-8., -4.]))
|
| 397 |
+
>>> P.polydiv(c2, c1)
|
| 398 |
+
(array([ 0.33333333]), array([ 2.66666667, 1.33333333])) # may vary
|
| 399 |
+
|
| 400 |
+
"""
|
| 401 |
+
# c1, c2 are trimmed copies
|
| 402 |
+
[c1, c2] = pu.as_series([c1, c2])
|
| 403 |
+
if c2[-1] == 0:
|
| 404 |
+
raise ZeroDivisionError # FIXME: add message with details to exception
|
| 405 |
+
|
| 406 |
+
# note: this is more efficient than `pu._div(polymul, c1, c2)`
|
| 407 |
+
lc1 = len(c1)
|
| 408 |
+
lc2 = len(c2)
|
| 409 |
+
if lc1 < lc2:
|
| 410 |
+
return c1[:1] * 0, c1
|
| 411 |
+
elif lc2 == 1:
|
| 412 |
+
return c1 / c2[-1], c1[:1] * 0
|
| 413 |
+
else:
|
| 414 |
+
dlen = lc1 - lc2
|
| 415 |
+
scl = c2[-1]
|
| 416 |
+
c2 = c2[:-1] / scl
|
| 417 |
+
i = dlen
|
| 418 |
+
j = lc1 - 1
|
| 419 |
+
while i >= 0:
|
| 420 |
+
c1[i:j] -= c2 * c1[j]
|
| 421 |
+
i -= 1
|
| 422 |
+
j -= 1
|
| 423 |
+
return c1[j + 1:] / scl, pu.trimseq(c1[:j + 1])
|
| 424 |
+
|
| 425 |
+
|
| 426 |
+
def polypow(c, pow, maxpower=None):
|
| 427 |
+
"""Raise a polynomial to a power.
|
| 428 |
+
|
| 429 |
+
Returns the polynomial `c` raised to the power `pow`. The argument
|
| 430 |
+
`c` is a sequence of coefficients ordered from low to high. i.e.,
|
| 431 |
+
[1,2,3] is the series ``1 + 2*x + 3*x**2.``
|
| 432 |
+
|
| 433 |
+
Parameters
|
| 434 |
+
----------
|
| 435 |
+
c : array_like
|
| 436 |
+
1-D array of array of series coefficients ordered from low to
|
| 437 |
+
high degree.
|
| 438 |
+
pow : integer
|
| 439 |
+
Power to which the series will be raised
|
| 440 |
+
maxpower : integer, optional
|
| 441 |
+
Maximum power allowed. This is mainly to limit growth of the series
|
| 442 |
+
to unmanageable size. Default is 16
|
| 443 |
+
|
| 444 |
+
Returns
|
| 445 |
+
-------
|
| 446 |
+
coef : ndarray
|
| 447 |
+
Power series of power.
|
| 448 |
+
|
| 449 |
+
See Also
|
| 450 |
+
--------
|
| 451 |
+
polyadd, polysub, polymulx, polymul, polydiv
|
| 452 |
+
|
| 453 |
+
Examples
|
| 454 |
+
--------
|
| 455 |
+
>>> from numpy.polynomial import polynomial as P
|
| 456 |
+
>>> P.polypow([1, 2, 3], 2)
|
| 457 |
+
array([ 1., 4., 10., 12., 9.])
|
| 458 |
+
|
| 459 |
+
"""
|
| 460 |
+
# note: this is more efficient than `pu._pow(polymul, c1, c2)`, as it
|
| 461 |
+
# avoids calling `as_series` repeatedly
|
| 462 |
+
return pu._pow(np.convolve, c, pow, maxpower)
|
| 463 |
+
|
| 464 |
+
|
| 465 |
+
def polyder(c, m=1, scl=1, axis=0):
|
| 466 |
+
"""
|
| 467 |
+
Differentiate a polynomial.
|
| 468 |
+
|
| 469 |
+
Returns the polynomial coefficients `c` differentiated `m` times along
|
| 470 |
+
`axis`. At each iteration the result is multiplied by `scl` (the
|
| 471 |
+
scaling factor is for use in a linear change of variable). The
|
| 472 |
+
argument `c` is an array of coefficients from low to high degree along
|
| 473 |
+
each axis, e.g., [1,2,3] represents the polynomial ``1 + 2*x + 3*x**2``
|
| 474 |
+
while [[1,2],[1,2]] represents ``1 + 1*x + 2*y + 2*x*y`` if axis=0 is
|
| 475 |
+
``x`` and axis=1 is ``y``.
|
| 476 |
+
|
| 477 |
+
Parameters
|
| 478 |
+
----------
|
| 479 |
+
c : array_like
|
| 480 |
+
Array of polynomial coefficients. If c is multidimensional the
|
| 481 |
+
different axis correspond to different variables with the degree
|
| 482 |
+
in each axis given by the corresponding index.
|
| 483 |
+
m : int, optional
|
| 484 |
+
Number of derivatives taken, must be non-negative. (Default: 1)
|
| 485 |
+
scl : scalar, optional
|
| 486 |
+
Each differentiation is multiplied by `scl`. The end result is
|
| 487 |
+
multiplication by ``scl**m``. This is for use in a linear change
|
| 488 |
+
of variable. (Default: 1)
|
| 489 |
+
axis : int, optional
|
| 490 |
+
Axis over which the derivative is taken. (Default: 0).
|
| 491 |
+
|
| 492 |
+
Returns
|
| 493 |
+
-------
|
| 494 |
+
der : ndarray
|
| 495 |
+
Polynomial coefficients of the derivative.
|
| 496 |
+
|
| 497 |
+
See Also
|
| 498 |
+
--------
|
| 499 |
+
polyint
|
| 500 |
+
|
| 501 |
+
Examples
|
| 502 |
+
--------
|
| 503 |
+
>>> from numpy.polynomial import polynomial as P
|
| 504 |
+
>>> c = (1, 2, 3, 4)
|
| 505 |
+
>>> P.polyder(c) # (d/dx)(c)
|
| 506 |
+
array([ 2., 6., 12.])
|
| 507 |
+
>>> P.polyder(c, 3) # (d**3/dx**3)(c)
|
| 508 |
+
array([24.])
|
| 509 |
+
>>> P.polyder(c, scl=-1) # (d/d(-x))(c)
|
| 510 |
+
array([ -2., -6., -12.])
|
| 511 |
+
>>> P.polyder(c, 2, -1) # (d**2/d(-x)**2)(c)
|
| 512 |
+
array([ 6., 24.])
|
| 513 |
+
|
| 514 |
+
"""
|
| 515 |
+
c = np.array(c, ndmin=1, copy=True)
|
| 516 |
+
if c.dtype.char in '?bBhHiIlLqQpP':
|
| 517 |
+
# astype fails with NA
|
| 518 |
+
c = c + 0.0
|
| 519 |
+
cdt = c.dtype
|
| 520 |
+
cnt = pu._as_int(m, "the order of derivation")
|
| 521 |
+
iaxis = pu._as_int(axis, "the axis")
|
| 522 |
+
if cnt < 0:
|
| 523 |
+
raise ValueError("The order of derivation must be non-negative")
|
| 524 |
+
iaxis = np.lib.array_utils.normalize_axis_index(iaxis, c.ndim)
|
| 525 |
+
|
| 526 |
+
if cnt == 0:
|
| 527 |
+
return c
|
| 528 |
+
|
| 529 |
+
c = np.moveaxis(c, iaxis, 0)
|
| 530 |
+
n = len(c)
|
| 531 |
+
if cnt >= n:
|
| 532 |
+
c = c[:1] * 0
|
| 533 |
+
else:
|
| 534 |
+
for i in range(cnt):
|
| 535 |
+
n = n - 1
|
| 536 |
+
c *= scl
|
| 537 |
+
der = np.empty((n,) + c.shape[1:], dtype=cdt)
|
| 538 |
+
for j in range(n, 0, -1):
|
| 539 |
+
der[j - 1] = j * c[j]
|
| 540 |
+
c = der
|
| 541 |
+
c = np.moveaxis(c, 0, iaxis)
|
| 542 |
+
return c
|
| 543 |
+
|
| 544 |
+
|
| 545 |
+
def polyint(c, m=1, k=[], lbnd=0, scl=1, axis=0):
|
| 546 |
+
"""
|
| 547 |
+
Integrate a polynomial.
|
| 548 |
+
|
| 549 |
+
Returns the polynomial coefficients `c` integrated `m` times from
|
| 550 |
+
`lbnd` along `axis`. At each iteration the resulting series is
|
| 551 |
+
**multiplied** by `scl` and an integration constant, `k`, is added.
|
| 552 |
+
The scaling factor is for use in a linear change of variable. ("Buyer
|
| 553 |
+
beware": note that, depending on what one is doing, one may want `scl`
|
| 554 |
+
to be the reciprocal of what one might expect; for more information,
|
| 555 |
+
see the Notes section below.) The argument `c` is an array of
|
| 556 |
+
coefficients, from low to high degree along each axis, e.g., [1,2,3]
|
| 557 |
+
represents the polynomial ``1 + 2*x + 3*x**2`` while [[1,2],[1,2]]
|
| 558 |
+
represents ``1 + 1*x + 2*y + 2*x*y`` if axis=0 is ``x`` and axis=1 is
|
| 559 |
+
``y``.
|
| 560 |
+
|
| 561 |
+
Parameters
|
| 562 |
+
----------
|
| 563 |
+
c : array_like
|
| 564 |
+
1-D array of polynomial coefficients, ordered from low to high.
|
| 565 |
+
m : int, optional
|
| 566 |
+
Order of integration, must be positive. (Default: 1)
|
| 567 |
+
k : {[], list, scalar}, optional
|
| 568 |
+
Integration constant(s). The value of the first integral at zero
|
| 569 |
+
is the first value in the list, the value of the second integral
|
| 570 |
+
at zero is the second value, etc. If ``k == []`` (the default),
|
| 571 |
+
all constants are set to zero. If ``m == 1``, a single scalar can
|
| 572 |
+
be given instead of a list.
|
| 573 |
+
lbnd : scalar, optional
|
| 574 |
+
The lower bound of the integral. (Default: 0)
|
| 575 |
+
scl : scalar, optional
|
| 576 |
+
Following each integration the result is *multiplied* by `scl`
|
| 577 |
+
before the integration constant is added. (Default: 1)
|
| 578 |
+
axis : int, optional
|
| 579 |
+
Axis over which the integral is taken. (Default: 0).
|
| 580 |
+
|
| 581 |
+
Returns
|
| 582 |
+
-------
|
| 583 |
+
S : ndarray
|
| 584 |
+
Coefficient array of the integral.
|
| 585 |
+
|
| 586 |
+
Raises
|
| 587 |
+
------
|
| 588 |
+
ValueError
|
| 589 |
+
If ``m < 1``, ``len(k) > m``, ``np.ndim(lbnd) != 0``, or
|
| 590 |
+
``np.ndim(scl) != 0``.
|
| 591 |
+
|
| 592 |
+
See Also
|
| 593 |
+
--------
|
| 594 |
+
polyder
|
| 595 |
+
|
| 596 |
+
Notes
|
| 597 |
+
-----
|
| 598 |
+
Note that the result of each integration is *multiplied* by `scl`. Why
|
| 599 |
+
is this important to note? Say one is making a linear change of
|
| 600 |
+
variable :math:`u = ax + b` in an integral relative to `x`. Then
|
| 601 |
+
:math:`dx = du/a`, so one will need to set `scl` equal to
|
| 602 |
+
:math:`1/a` - perhaps not what one would have first thought.
|
| 603 |
+
|
| 604 |
+
Examples
|
| 605 |
+
--------
|
| 606 |
+
>>> from numpy.polynomial import polynomial as P
|
| 607 |
+
>>> c = (1, 2, 3)
|
| 608 |
+
>>> P.polyint(c) # should return array([0, 1, 1, 1])
|
| 609 |
+
array([0., 1., 1., 1.])
|
| 610 |
+
>>> P.polyint(c, 3) # should return array([0, 0, 0, 1/6, 1/12, 1/20])
|
| 611 |
+
array([ 0. , 0. , 0. , 0.16666667, 0.08333333, # may vary
|
| 612 |
+
0.05 ])
|
| 613 |
+
>>> P.polyint(c, k=3) # should return array([3, 1, 1, 1])
|
| 614 |
+
array([3., 1., 1., 1.])
|
| 615 |
+
>>> P.polyint(c,lbnd=-2) # should return array([6, 1, 1, 1])
|
| 616 |
+
array([6., 1., 1., 1.])
|
| 617 |
+
>>> P.polyint(c,scl=-2) # should return array([0, -2, -2, -2])
|
| 618 |
+
array([ 0., -2., -2., -2.])
|
| 619 |
+
|
| 620 |
+
"""
|
| 621 |
+
c = np.array(c, ndmin=1, copy=True)
|
| 622 |
+
if c.dtype.char in '?bBhHiIlLqQpP':
|
| 623 |
+
# astype doesn't preserve mask attribute.
|
| 624 |
+
c = c + 0.0
|
| 625 |
+
cdt = c.dtype
|
| 626 |
+
if not np.iterable(k):
|
| 627 |
+
k = [k]
|
| 628 |
+
cnt = pu._as_int(m, "the order of integration")
|
| 629 |
+
iaxis = pu._as_int(axis, "the axis")
|
| 630 |
+
if cnt < 0:
|
| 631 |
+
raise ValueError("The order of integration must be non-negative")
|
| 632 |
+
if len(k) > cnt:
|
| 633 |
+
raise ValueError("Too many integration constants")
|
| 634 |
+
if np.ndim(lbnd) != 0:
|
| 635 |
+
raise ValueError("lbnd must be a scalar.")
|
| 636 |
+
if np.ndim(scl) != 0:
|
| 637 |
+
raise ValueError("scl must be a scalar.")
|
| 638 |
+
iaxis = np.lib.array_utils.normalize_axis_index(iaxis, c.ndim)
|
| 639 |
+
|
| 640 |
+
if cnt == 0:
|
| 641 |
+
return c
|
| 642 |
+
|
| 643 |
+
k = list(k) + [0] * (cnt - len(k))
|
| 644 |
+
c = np.moveaxis(c, iaxis, 0)
|
| 645 |
+
for i in range(cnt):
|
| 646 |
+
n = len(c)
|
| 647 |
+
c *= scl
|
| 648 |
+
if n == 1 and np.all(c[0] == 0):
|
| 649 |
+
c[0] += k[i]
|
| 650 |
+
else:
|
| 651 |
+
tmp = np.empty((n + 1,) + c.shape[1:], dtype=cdt)
|
| 652 |
+
tmp[0] = c[0] * 0
|
| 653 |
+
tmp[1] = c[0]
|
| 654 |
+
for j in range(1, n):
|
| 655 |
+
tmp[j + 1] = c[j] / (j + 1)
|
| 656 |
+
tmp[0] += k[i] - polyval(lbnd, tmp)
|
| 657 |
+
c = tmp
|
| 658 |
+
c = np.moveaxis(c, 0, iaxis)
|
| 659 |
+
return c
|
| 660 |
+
|
| 661 |
+
|
| 662 |
+
def polyval(x, c, tensor=True):
|
| 663 |
+
"""
|
| 664 |
+
Evaluate a polynomial at points x.
|
| 665 |
+
|
| 666 |
+
If `c` is of length ``n + 1``, this function returns the value
|
| 667 |
+
|
| 668 |
+
.. math:: p(x) = c_0 + c_1 * x + ... + c_n * x^n
|
| 669 |
+
|
| 670 |
+
The parameter `x` is converted to an array only if it is a tuple or a
|
| 671 |
+
list, otherwise it is treated as a scalar. In either case, either `x`
|
| 672 |
+
or its elements must support multiplication and addition both with
|
| 673 |
+
themselves and with the elements of `c`.
|
| 674 |
+
|
| 675 |
+
If `c` is a 1-D array, then ``p(x)`` will have the same shape as `x`. If
|
| 676 |
+
`c` is multidimensional, then the shape of the result depends on the
|
| 677 |
+
value of `tensor`. If `tensor` is true the shape will be c.shape[1:] +
|
| 678 |
+
x.shape. If `tensor` is false the shape will be c.shape[1:]. Note that
|
| 679 |
+
scalars have shape (,).
|
| 680 |
+
|
| 681 |
+
Trailing zeros in the coefficients will be used in the evaluation, so
|
| 682 |
+
they should be avoided if efficiency is a concern.
|
| 683 |
+
|
| 684 |
+
Parameters
|
| 685 |
+
----------
|
| 686 |
+
x : array_like, compatible object
|
| 687 |
+
If `x` is a list or tuple, it is converted to an ndarray, otherwise
|
| 688 |
+
it is left unchanged and treated as a scalar. In either case, `x`
|
| 689 |
+
or its elements must support addition and multiplication with
|
| 690 |
+
with themselves and with the elements of `c`.
|
| 691 |
+
c : array_like
|
| 692 |
+
Array of coefficients ordered so that the coefficients for terms of
|
| 693 |
+
degree n are contained in c[n]. If `c` is multidimensional the
|
| 694 |
+
remaining indices enumerate multiple polynomials. In the two
|
| 695 |
+
dimensional case the coefficients may be thought of as stored in
|
| 696 |
+
the columns of `c`.
|
| 697 |
+
tensor : boolean, optional
|
| 698 |
+
If True, the shape of the coefficient array is extended with ones
|
| 699 |
+
on the right, one for each dimension of `x`. Scalars have dimension 0
|
| 700 |
+
for this action. The result is that every column of coefficients in
|
| 701 |
+
`c` is evaluated for every element of `x`. If False, `x` is broadcast
|
| 702 |
+
over the columns of `c` for the evaluation. This keyword is useful
|
| 703 |
+
when `c` is multidimensional. The default value is True.
|
| 704 |
+
|
| 705 |
+
Returns
|
| 706 |
+
-------
|
| 707 |
+
values : ndarray, compatible object
|
| 708 |
+
The shape of the returned array is described above.
|
| 709 |
+
|
| 710 |
+
See Also
|
| 711 |
+
--------
|
| 712 |
+
polyval2d, polygrid2d, polyval3d, polygrid3d
|
| 713 |
+
|
| 714 |
+
Notes
|
| 715 |
+
-----
|
| 716 |
+
The evaluation uses Horner's method.
|
| 717 |
+
|
| 718 |
+
When using coefficients from polynomials created with ``Polynomial.fit()``,
|
| 719 |
+
use ``p(x)`` or ``polyval(x, p.convert().coef)`` to handle domain/window
|
| 720 |
+
scaling correctly, not ``polyval(x, p.coef)``.
|
| 721 |
+
|
| 722 |
+
Examples
|
| 723 |
+
--------
|
| 724 |
+
>>> import numpy as np
|
| 725 |
+
>>> from numpy.polynomial.polynomial import polyval
|
| 726 |
+
>>> polyval(1, [1,2,3])
|
| 727 |
+
6.0
|
| 728 |
+
>>> a = np.arange(4).reshape(2,2)
|
| 729 |
+
>>> a
|
| 730 |
+
array([[0, 1],
|
| 731 |
+
[2, 3]])
|
| 732 |
+
>>> polyval(a, [1, 2, 3])
|
| 733 |
+
array([[ 1., 6.],
|
| 734 |
+
[17., 34.]])
|
| 735 |
+
>>> coef = np.arange(4).reshape(2, 2) # multidimensional coefficients
|
| 736 |
+
>>> coef
|
| 737 |
+
array([[0, 1],
|
| 738 |
+
[2, 3]])
|
| 739 |
+
>>> polyval([1, 2], coef, tensor=True)
|
| 740 |
+
array([[2., 4.],
|
| 741 |
+
[4., 7.]])
|
| 742 |
+
>>> polyval([1, 2], coef, tensor=False)
|
| 743 |
+
array([2., 7.])
|
| 744 |
+
|
| 745 |
+
"""
|
| 746 |
+
c = np.array(c, ndmin=1, copy=None)
|
| 747 |
+
if c.dtype.char in '?bBhHiIlLqQpP':
|
| 748 |
+
# astype fails with NA
|
| 749 |
+
c = c + 0.0
|
| 750 |
+
if isinstance(x, (tuple, list)):
|
| 751 |
+
x = np.asarray(x)
|
| 752 |
+
if isinstance(x, np.ndarray) and tensor:
|
| 753 |
+
c = c.reshape(c.shape + (1,) * x.ndim)
|
| 754 |
+
|
| 755 |
+
c0 = c[-1] + x * 0
|
| 756 |
+
for i in range(2, len(c) + 1):
|
| 757 |
+
c0 = c[-i] + c0 * x
|
| 758 |
+
return c0
|
| 759 |
+
|
| 760 |
+
|
| 761 |
+
def polyvalfromroots(x, r, tensor=True):
|
| 762 |
+
"""
|
| 763 |
+
Evaluate a polynomial specified by its roots at points x.
|
| 764 |
+
|
| 765 |
+
If `r` is of length ``N``, this function returns the value
|
| 766 |
+
|
| 767 |
+
.. math:: p(x) = \\prod_{n=1}^{N} (x - r_n)
|
| 768 |
+
|
| 769 |
+
The parameter `x` is converted to an array only if it is a tuple or a
|
| 770 |
+
list, otherwise it is treated as a scalar. In either case, either `x`
|
| 771 |
+
or its elements must support multiplication and addition both with
|
| 772 |
+
themselves and with the elements of `r`.
|
| 773 |
+
|
| 774 |
+
If `r` is a 1-D array, then ``p(x)`` will have the same shape as `x`. If `r`
|
| 775 |
+
is multidimensional, then the shape of the result depends on the value of
|
| 776 |
+
`tensor`. If `tensor` is ``True`` the shape will be r.shape[1:] + x.shape;
|
| 777 |
+
that is, each polynomial is evaluated at every value of `x`. If `tensor` is
|
| 778 |
+
``False``, the shape will be r.shape[1:]; that is, each polynomial is
|
| 779 |
+
evaluated only for the corresponding broadcast value of `x`. Note that
|
| 780 |
+
scalars have shape (,).
|
| 781 |
+
|
| 782 |
+
Parameters
|
| 783 |
+
----------
|
| 784 |
+
x : array_like, compatible object
|
| 785 |
+
If `x` is a list or tuple, it is converted to an ndarray, otherwise
|
| 786 |
+
it is left unchanged and treated as a scalar. In either case, `x`
|
| 787 |
+
or its elements must support addition and multiplication with
|
| 788 |
+
with themselves and with the elements of `r`.
|
| 789 |
+
r : array_like
|
| 790 |
+
Array of roots. If `r` is multidimensional the first index is the
|
| 791 |
+
root index, while the remaining indices enumerate multiple
|
| 792 |
+
polynomials. For instance, in the two dimensional case the roots
|
| 793 |
+
of each polynomial may be thought of as stored in the columns of `r`.
|
| 794 |
+
tensor : boolean, optional
|
| 795 |
+
If True, the shape of the roots array is extended with ones on the
|
| 796 |
+
right, one for each dimension of `x`. Scalars have dimension 0 for this
|
| 797 |
+
action. The result is that every column of coefficients in `r` is
|
| 798 |
+
evaluated for every element of `x`. If False, `x` is broadcast over the
|
| 799 |
+
columns of `r` for the evaluation. This keyword is useful when `r` is
|
| 800 |
+
multidimensional. The default value is True.
|
| 801 |
+
|
| 802 |
+
Returns
|
| 803 |
+
-------
|
| 804 |
+
values : ndarray, compatible object
|
| 805 |
+
The shape of the returned array is described above.
|
| 806 |
+
|
| 807 |
+
See Also
|
| 808 |
+
--------
|
| 809 |
+
polyroots, polyfromroots, polyval
|
| 810 |
+
|
| 811 |
+
Examples
|
| 812 |
+
--------
|
| 813 |
+
>>> from numpy.polynomial.polynomial import polyvalfromroots
|
| 814 |
+
>>> polyvalfromroots(1, [1, 2, 3])
|
| 815 |
+
0.0
|
| 816 |
+
>>> a = np.arange(4).reshape(2, 2)
|
| 817 |
+
>>> a
|
| 818 |
+
array([[0, 1],
|
| 819 |
+
[2, 3]])
|
| 820 |
+
>>> polyvalfromroots(a, [-1, 0, 1])
|
| 821 |
+
array([[-0., 0.],
|
| 822 |
+
[ 6., 24.]])
|
| 823 |
+
>>> r = np.arange(-2, 2).reshape(2,2) # multidimensional coefficients
|
| 824 |
+
>>> r # each column of r defines one polynomial
|
| 825 |
+
array([[-2, -1],
|
| 826 |
+
[ 0, 1]])
|
| 827 |
+
>>> b = [-2, 1]
|
| 828 |
+
>>> polyvalfromroots(b, r, tensor=True)
|
| 829 |
+
array([[-0., 3.],
|
| 830 |
+
[ 3., 0.]])
|
| 831 |
+
>>> polyvalfromroots(b, r, tensor=False)
|
| 832 |
+
array([-0., 0.])
|
| 833 |
+
|
| 834 |
+
"""
|
| 835 |
+
r = np.array(r, ndmin=1, copy=None)
|
| 836 |
+
if r.dtype.char in '?bBhHiIlLqQpP':
|
| 837 |
+
r = r.astype(np.double)
|
| 838 |
+
if isinstance(x, (tuple, list)):
|
| 839 |
+
x = np.asarray(x)
|
| 840 |
+
if isinstance(x, np.ndarray):
|
| 841 |
+
if tensor:
|
| 842 |
+
r = r.reshape(r.shape + (1,) * x.ndim)
|
| 843 |
+
elif x.ndim >= r.ndim:
|
| 844 |
+
raise ValueError("x.ndim must be < r.ndim when tensor == False")
|
| 845 |
+
return np.prod(x - r, axis=0)
|
| 846 |
+
|
| 847 |
+
def _polyval2d_dispatcher(x, y, c):
|
| 848 |
+
return (x, y, c)
|
| 849 |
+
|
| 850 |
+
def _polygrid2d_dispatcher(x, y, c):
|
| 851 |
+
return (x, y, c)
|
| 852 |
+
|
| 853 |
+
@_array_function_dispatch(_polyval2d_dispatcher)
|
| 854 |
+
def polyval2d(x, y, c):
|
| 855 |
+
"""
|
| 856 |
+
Evaluate a 2-D polynomial at points (x, y).
|
| 857 |
+
|
| 858 |
+
This function returns the value
|
| 859 |
+
|
| 860 |
+
.. math:: p(x,y) = \\sum_{i,j} c_{i,j} * x^i * y^j
|
| 861 |
+
|
| 862 |
+
The parameters `x` and `y` are converted to arrays only if they are
|
| 863 |
+
tuples or a lists, otherwise they are treated as a scalars and they
|
| 864 |
+
must have the same shape after conversion. In either case, either `x`
|
| 865 |
+
and `y` or their elements must support multiplication and addition both
|
| 866 |
+
with themselves and with the elements of `c`.
|
| 867 |
+
|
| 868 |
+
If `c` has fewer than two dimensions, ones are implicitly appended to
|
| 869 |
+
its shape to make it 2-D. The shape of the result will be c.shape[2:] +
|
| 870 |
+
x.shape.
|
| 871 |
+
|
| 872 |
+
Parameters
|
| 873 |
+
----------
|
| 874 |
+
x, y : array_like, compatible objects
|
| 875 |
+
The two dimensional series is evaluated at the points ``(x, y)``,
|
| 876 |
+
where `x` and `y` must have the same shape. If `x` or `y` is a list
|
| 877 |
+
or tuple, it is first converted to an ndarray, otherwise it is left
|
| 878 |
+
unchanged and, if it isn't an ndarray, it is treated as a scalar.
|
| 879 |
+
c : array_like
|
| 880 |
+
Array of coefficients ordered so that the coefficient of the term
|
| 881 |
+
of multi-degree i,j is contained in ``c[i,j]``. If `c` has
|
| 882 |
+
dimension greater than two the remaining indices enumerate multiple
|
| 883 |
+
sets of coefficients.
|
| 884 |
+
|
| 885 |
+
Returns
|
| 886 |
+
-------
|
| 887 |
+
values : ndarray, compatible object
|
| 888 |
+
The values of the two dimensional polynomial at points formed with
|
| 889 |
+
pairs of corresponding values from `x` and `y`.
|
| 890 |
+
|
| 891 |
+
See Also
|
| 892 |
+
--------
|
| 893 |
+
polyval, polygrid2d, polyval3d, polygrid3d
|
| 894 |
+
|
| 895 |
+
Examples
|
| 896 |
+
--------
|
| 897 |
+
>>> from numpy.polynomial import polynomial as P
|
| 898 |
+
>>> c = ((1, 2, 3), (4, 5, 6))
|
| 899 |
+
>>> P.polyval2d(1, 1, c)
|
| 900 |
+
21.0
|
| 901 |
+
|
| 902 |
+
"""
|
| 903 |
+
return pu._valnd(polyval, c, x, y)
|
| 904 |
+
|
| 905 |
+
@_array_function_dispatch(_polygrid2d_dispatcher)
|
| 906 |
+
def polygrid2d(x, y, c):
|
| 907 |
+
"""
|
| 908 |
+
Evaluate a 2-D polynomial on the Cartesian product of x and y.
|
| 909 |
+
|
| 910 |
+
This function returns the values:
|
| 911 |
+
|
| 912 |
+
.. math:: p(a,b) = \\sum_{i,j} c_{i,j} * a^i * b^j
|
| 913 |
+
|
| 914 |
+
where the points ``(a, b)`` consist of all pairs formed by taking
|
| 915 |
+
`a` from `x` and `b` from `y`. The resulting points form a grid with
|
| 916 |
+
`x` in the first dimension and `y` in the second.
|
| 917 |
+
|
| 918 |
+
The parameters `x` and `y` are converted to arrays only if they are
|
| 919 |
+
tuples or a lists, otherwise they are treated as a scalars. In either
|
| 920 |
+
case, either `x` and `y` or their elements must support multiplication
|
| 921 |
+
and addition both with themselves and with the elements of `c`.
|
| 922 |
+
|
| 923 |
+
If `c` has fewer than two dimensions, ones are implicitly appended to
|
| 924 |
+
its shape to make it 2-D. The shape of the result will be c.shape[2:] +
|
| 925 |
+
x.shape + y.shape.
|
| 926 |
+
|
| 927 |
+
Parameters
|
| 928 |
+
----------
|
| 929 |
+
x, y : array_like, compatible objects
|
| 930 |
+
The two dimensional series is evaluated at the points in the
|
| 931 |
+
Cartesian product of `x` and `y`. If `x` or `y` is a list or
|
| 932 |
+
tuple, it is first converted to an ndarray, otherwise it is left
|
| 933 |
+
unchanged and, if it isn't an ndarray, it is treated as a scalar.
|
| 934 |
+
c : array_like
|
| 935 |
+
Array of coefficients ordered so that the coefficients for terms of
|
| 936 |
+
degree i,j are contained in ``c[i,j]``. If `c` has dimension
|
| 937 |
+
greater than two the remaining indices enumerate multiple sets of
|
| 938 |
+
coefficients.
|
| 939 |
+
|
| 940 |
+
Returns
|
| 941 |
+
-------
|
| 942 |
+
values : ndarray, compatible object
|
| 943 |
+
The values of the two dimensional polynomial at points in the Cartesian
|
| 944 |
+
product of `x` and `y`.
|
| 945 |
+
|
| 946 |
+
See Also
|
| 947 |
+
--------
|
| 948 |
+
polyval, polyval2d, polyval3d, polygrid3d
|
| 949 |
+
|
| 950 |
+
Examples
|
| 951 |
+
--------
|
| 952 |
+
>>> from numpy.polynomial import polynomial as P
|
| 953 |
+
>>> c = ((1, 2, 3), (4, 5, 6))
|
| 954 |
+
>>> P.polygrid2d([0, 1], [0, 1], c)
|
| 955 |
+
array([[ 1., 6.],
|
| 956 |
+
[ 5., 21.]])
|
| 957 |
+
|
| 958 |
+
"""
|
| 959 |
+
return pu._gridnd(polyval, c, x, y)
|
| 960 |
+
|
| 961 |
+
|
| 962 |
+
def polyval3d(x, y, z, c):
|
| 963 |
+
"""
|
| 964 |
+
Evaluate a 3-D polynomial at points (x, y, z).
|
| 965 |
+
|
| 966 |
+
This function returns the values:
|
| 967 |
+
|
| 968 |
+
.. math:: p(x,y,z) = \\sum_{i,j,k} c_{i,j,k} * x^i * y^j * z^k
|
| 969 |
+
|
| 970 |
+
The parameters `x`, `y`, and `z` are converted to arrays only if
|
| 971 |
+
they are tuples or a lists, otherwise they are treated as a scalars and
|
| 972 |
+
they must have the same shape after conversion. In either case, either
|
| 973 |
+
`x`, `y`, and `z` or their elements must support multiplication and
|
| 974 |
+
addition both with themselves and with the elements of `c`.
|
| 975 |
+
|
| 976 |
+
If `c` has fewer than 3 dimensions, ones are implicitly appended to its
|
| 977 |
+
shape to make it 3-D. The shape of the result will be c.shape[3:] +
|
| 978 |
+
x.shape.
|
| 979 |
+
|
| 980 |
+
Parameters
|
| 981 |
+
----------
|
| 982 |
+
x, y, z : array_like, compatible object
|
| 983 |
+
The three dimensional series is evaluated at the points
|
| 984 |
+
``(x, y, z)``, where `x`, `y`, and `z` must have the same shape. If
|
| 985 |
+
any of `x`, `y`, or `z` is a list or tuple, it is first converted
|
| 986 |
+
to an ndarray, otherwise it is left unchanged and if it isn't an
|
| 987 |
+
ndarray it is treated as a scalar.
|
| 988 |
+
c : array_like
|
| 989 |
+
Array of coefficients ordered so that the coefficient of the term of
|
| 990 |
+
multi-degree i,j,k is contained in ``c[i,j,k]``. If `c` has dimension
|
| 991 |
+
greater than 3 the remaining indices enumerate multiple sets of
|
| 992 |
+
coefficients.
|
| 993 |
+
|
| 994 |
+
Returns
|
| 995 |
+
-------
|
| 996 |
+
values : ndarray, compatible object
|
| 997 |
+
The values of the multidimensional polynomial on points formed with
|
| 998 |
+
triples of corresponding values from `x`, `y`, and `z`.
|
| 999 |
+
|
| 1000 |
+
See Also
|
| 1001 |
+
--------
|
| 1002 |
+
polyval, polyval2d, polygrid2d, polygrid3d
|
| 1003 |
+
|
| 1004 |
+
Examples
|
| 1005 |
+
--------
|
| 1006 |
+
>>> from numpy.polynomial import polynomial as P
|
| 1007 |
+
>>> c = ((1, 2, 3), (4, 5, 6), (7, 8, 9))
|
| 1008 |
+
>>> P.polyval3d(1, 1, 1, c)
|
| 1009 |
+
45.0
|
| 1010 |
+
|
| 1011 |
+
"""
|
| 1012 |
+
return pu._valnd(polyval, c, x, y, z)
|
| 1013 |
+
|
| 1014 |
+
|
| 1015 |
+
def polygrid3d(x, y, z, c):
|
| 1016 |
+
"""
|
| 1017 |
+
Evaluate a 3-D polynomial on the Cartesian product of x, y and z.
|
| 1018 |
+
|
| 1019 |
+
This function returns the values:
|
| 1020 |
+
|
| 1021 |
+
.. math:: p(a,b,c) = \\sum_{i,j,k} c_{i,j,k} * a^i * b^j * c^k
|
| 1022 |
+
|
| 1023 |
+
where the points ``(a, b, c)`` consist of all triples formed by taking
|
| 1024 |
+
`a` from `x`, `b` from `y`, and `c` from `z`. The resulting points form
|
| 1025 |
+
a grid with `x` in the first dimension, `y` in the second, and `z` in
|
| 1026 |
+
the third.
|
| 1027 |
+
|
| 1028 |
+
The parameters `x`, `y`, and `z` are converted to arrays only if they
|
| 1029 |
+
are tuples or a lists, otherwise they are treated as a scalars. In
|
| 1030 |
+
either case, either `x`, `y`, and `z` or their elements must support
|
| 1031 |
+
multiplication and addition both with themselves and with the elements
|
| 1032 |
+
of `c`.
|
| 1033 |
+
|
| 1034 |
+
If `c` has fewer than three dimensions, ones are implicitly appended to
|
| 1035 |
+
its shape to make it 3-D. The shape of the result will be c.shape[3:] +
|
| 1036 |
+
x.shape + y.shape + z.shape.
|
| 1037 |
+
|
| 1038 |
+
Parameters
|
| 1039 |
+
----------
|
| 1040 |
+
x, y, z : array_like, compatible objects
|
| 1041 |
+
The three dimensional series is evaluated at the points in the
|
| 1042 |
+
Cartesian product of `x`, `y`, and `z`. If `x`, `y`, or `z` is a
|
| 1043 |
+
list or tuple, it is first converted to an ndarray, otherwise it is
|
| 1044 |
+
left unchanged and, if it isn't an ndarray, it is treated as a
|
| 1045 |
+
scalar.
|
| 1046 |
+
c : array_like
|
| 1047 |
+
Array of coefficients ordered so that the coefficients for terms of
|
| 1048 |
+
degree i,j are contained in ``c[i,j]``. If `c` has dimension
|
| 1049 |
+
greater than two the remaining indices enumerate multiple sets of
|
| 1050 |
+
coefficients.
|
| 1051 |
+
|
| 1052 |
+
Returns
|
| 1053 |
+
-------
|
| 1054 |
+
values : ndarray, compatible object
|
| 1055 |
+
The values of the two dimensional polynomial at points in the Cartesian
|
| 1056 |
+
product of `x` and `y`.
|
| 1057 |
+
|
| 1058 |
+
See Also
|
| 1059 |
+
--------
|
| 1060 |
+
polyval, polyval2d, polygrid2d, polyval3d
|
| 1061 |
+
|
| 1062 |
+
Examples
|
| 1063 |
+
--------
|
| 1064 |
+
>>> from numpy.polynomial import polynomial as P
|
| 1065 |
+
>>> c = ((1, 2, 3), (4, 5, 6), (7, 8, 9))
|
| 1066 |
+
>>> P.polygrid3d([0, 1], [0, 1], [0, 1], c)
|
| 1067 |
+
array([[ 1., 13.],
|
| 1068 |
+
[ 6., 51.]])
|
| 1069 |
+
|
| 1070 |
+
"""
|
| 1071 |
+
return pu._gridnd(polyval, c, x, y, z)
|
| 1072 |
+
|
| 1073 |
+
|
| 1074 |
+
def polyvander(x, deg):
|
| 1075 |
+
"""Vandermonde matrix of given degree.
|
| 1076 |
+
|
| 1077 |
+
Returns the Vandermonde matrix of degree `deg` and sample points
|
| 1078 |
+
`x`. The Vandermonde matrix is defined by
|
| 1079 |
+
|
| 1080 |
+
.. math:: V[..., i] = x^i,
|
| 1081 |
+
|
| 1082 |
+
where ``0 <= i <= deg``. The leading indices of `V` index the elements of
|
| 1083 |
+
`x` and the last index is the power of `x`.
|
| 1084 |
+
|
| 1085 |
+
If `c` is a 1-D array of coefficients of length ``n + 1`` and `V` is the
|
| 1086 |
+
matrix ``V = polyvander(x, n)``, then ``np.dot(V, c)`` and
|
| 1087 |
+
``polyval(x, c)`` are the same up to roundoff. This equivalence is
|
| 1088 |
+
useful both for least squares fitting and for the evaluation of a large
|
| 1089 |
+
number of polynomials of the same degree and sample points.
|
| 1090 |
+
|
| 1091 |
+
Parameters
|
| 1092 |
+
----------
|
| 1093 |
+
x : array_like
|
| 1094 |
+
Array of points. The dtype is converted to float64 or complex128
|
| 1095 |
+
depending on whether any of the elements are complex. If `x` is
|
| 1096 |
+
scalar it is converted to a 1-D array.
|
| 1097 |
+
deg : int
|
| 1098 |
+
Degree of the resulting matrix.
|
| 1099 |
+
|
| 1100 |
+
Returns
|
| 1101 |
+
-------
|
| 1102 |
+
vander : ndarray.
|
| 1103 |
+
The Vandermonde matrix. The shape of the returned matrix is
|
| 1104 |
+
``x.shape + (deg + 1,)``, where the last index is the power of `x`.
|
| 1105 |
+
The dtype will be the same as the converted `x`.
|
| 1106 |
+
|
| 1107 |
+
See Also
|
| 1108 |
+
--------
|
| 1109 |
+
polyvander2d, polyvander3d
|
| 1110 |
+
|
| 1111 |
+
Examples
|
| 1112 |
+
--------
|
| 1113 |
+
The Vandermonde matrix of degree ``deg = 5`` and sample points
|
| 1114 |
+
``x = [-1, 2, 3]`` contains the element-wise powers of `x`
|
| 1115 |
+
from 0 to 5 as its columns.
|
| 1116 |
+
|
| 1117 |
+
>>> from numpy.polynomial import polynomial as P
|
| 1118 |
+
>>> x, deg = [-1, 2, 3], 5
|
| 1119 |
+
>>> P.polyvander(x=x, deg=deg)
|
| 1120 |
+
array([[ 1., -1., 1., -1., 1., -1.],
|
| 1121 |
+
[ 1., 2., 4., 8., 16., 32.],
|
| 1122 |
+
[ 1., 3., 9., 27., 81., 243.]])
|
| 1123 |
+
|
| 1124 |
+
"""
|
| 1125 |
+
ideg = pu._as_int(deg, "deg")
|
| 1126 |
+
if ideg < 0:
|
| 1127 |
+
raise ValueError("deg must be non-negative")
|
| 1128 |
+
|
| 1129 |
+
x = np.array(x, copy=None, ndmin=1) + 0.0
|
| 1130 |
+
dims = (ideg + 1,) + x.shape
|
| 1131 |
+
dtyp = x.dtype
|
| 1132 |
+
v = np.empty(dims, dtype=dtyp)
|
| 1133 |
+
v[0] = x * 0 + 1
|
| 1134 |
+
if ideg > 0:
|
| 1135 |
+
v[1] = x
|
| 1136 |
+
for i in range(2, ideg + 1):
|
| 1137 |
+
v[i] = v[i - 1] * x
|
| 1138 |
+
return np.moveaxis(v, 0, -1)
|
| 1139 |
+
|
| 1140 |
+
|
| 1141 |
+
def polyvander2d(x, y, deg):
|
| 1142 |
+
"""Pseudo-Vandermonde matrix of given degrees.
|
| 1143 |
+
|
| 1144 |
+
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample
|
| 1145 |
+
points ``(x, y)``. The pseudo-Vandermonde matrix is defined by
|
| 1146 |
+
|
| 1147 |
+
.. math:: V[..., (deg[1] + 1)*i + j] = x^i * y^j,
|
| 1148 |
+
|
| 1149 |
+
where ``0 <= i <= deg[0]`` and ``0 <= j <= deg[1]``. The leading indices of
|
| 1150 |
+
`V` index the points ``(x, y)`` and the last index encodes the powers of
|
| 1151 |
+
`x` and `y`.
|
| 1152 |
+
|
| 1153 |
+
If ``V = polyvander2d(x, y, [xdeg, ydeg])``, then the columns of `V`
|
| 1154 |
+
correspond to the elements of a 2-D coefficient array `c` of shape
|
| 1155 |
+
(xdeg + 1, ydeg + 1) in the order
|
| 1156 |
+
|
| 1157 |
+
.. math:: c_{00}, c_{01}, c_{02} ... , c_{10}, c_{11}, c_{12} ...
|
| 1158 |
+
|
| 1159 |
+
and ``np.dot(V, c.flat)`` and ``polyval2d(x, y, c)`` will be the same
|
| 1160 |
+
up to roundoff. This equivalence is useful both for least squares
|
| 1161 |
+
fitting and for the evaluation of a large number of 2-D polynomials
|
| 1162 |
+
of the same degrees and sample points.
|
| 1163 |
+
|
| 1164 |
+
Parameters
|
| 1165 |
+
----------
|
| 1166 |
+
x, y : array_like
|
| 1167 |
+
Arrays of point coordinates, all of the same shape. The dtypes
|
| 1168 |
+
will be converted to either float64 or complex128 depending on
|
| 1169 |
+
whether any of the elements are complex. Scalars are converted to
|
| 1170 |
+
1-D arrays.
|
| 1171 |
+
deg : list of ints
|
| 1172 |
+
List of maximum degrees of the form [x_deg, y_deg].
|
| 1173 |
+
|
| 1174 |
+
Returns
|
| 1175 |
+
-------
|
| 1176 |
+
vander2d : ndarray
|
| 1177 |
+
The shape of the returned matrix is ``x.shape + (order,)``, where
|
| 1178 |
+
:math:`order = (deg[0]+1)*(deg([1]+1)`. The dtype will be the same
|
| 1179 |
+
as the converted `x` and `y`.
|
| 1180 |
+
|
| 1181 |
+
See Also
|
| 1182 |
+
--------
|
| 1183 |
+
polyvander, polyvander3d, polyval2d, polyval3d
|
| 1184 |
+
|
| 1185 |
+
Examples
|
| 1186 |
+
--------
|
| 1187 |
+
>>> import numpy as np
|
| 1188 |
+
|
| 1189 |
+
The 2-D pseudo-Vandermonde matrix of degree ``[1, 2]`` and sample
|
| 1190 |
+
points ``x = [-1, 2]`` and ``y = [1, 3]`` is as follows:
|
| 1191 |
+
|
| 1192 |
+
>>> from numpy.polynomial import polynomial as P
|
| 1193 |
+
>>> x = np.array([-1, 2])
|
| 1194 |
+
>>> y = np.array([1, 3])
|
| 1195 |
+
>>> m, n = 1, 2
|
| 1196 |
+
>>> deg = np.array([m, n])
|
| 1197 |
+
>>> V = P.polyvander2d(x=x, y=y, deg=deg)
|
| 1198 |
+
>>> V
|
| 1199 |
+
array([[ 1., 1., 1., -1., -1., -1.],
|
| 1200 |
+
[ 1., 3., 9., 2., 6., 18.]])
|
| 1201 |
+
|
| 1202 |
+
We can verify the columns for any ``0 <= i <= m`` and ``0 <= j <= n``:
|
| 1203 |
+
|
| 1204 |
+
>>> i, j = 0, 1
|
| 1205 |
+
>>> V[:, (deg[1]+1)*i + j] == x**i * y**j
|
| 1206 |
+
array([ True, True])
|
| 1207 |
+
|
| 1208 |
+
The (1D) Vandermonde matrix of sample points ``x`` and degree ``m`` is a
|
| 1209 |
+
special case of the (2D) pseudo-Vandermonde matrix with ``y`` points all
|
| 1210 |
+
zero and degree ``[m, 0]``.
|
| 1211 |
+
|
| 1212 |
+
>>> P.polyvander2d(x=x, y=0*x, deg=(m, 0)) == P.polyvander(x=x, deg=m)
|
| 1213 |
+
array([[ True, True],
|
| 1214 |
+
[ True, True]])
|
| 1215 |
+
|
| 1216 |
+
"""
|
| 1217 |
+
return pu._vander_nd_flat((polyvander, polyvander), (x, y), deg)
|
| 1218 |
+
|
| 1219 |
+
|
| 1220 |
+
def polyvander3d(x, y, z, deg):
|
| 1221 |
+
"""Pseudo-Vandermonde matrix of given degrees.
|
| 1222 |
+
|
| 1223 |
+
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample
|
| 1224 |
+
points ``(x, y, z)``. If `l`, `m`, `n` are the given degrees in `x`, `y`, `z`,
|
| 1225 |
+
then The pseudo-Vandermonde matrix is defined by
|
| 1226 |
+
|
| 1227 |
+
.. math:: V[..., (m+1)(n+1)i + (n+1)j + k] = x^i * y^j * z^k,
|
| 1228 |
+
|
| 1229 |
+
where ``0 <= i <= l``, ``0 <= j <= m``, and ``0 <= j <= n``. The leading
|
| 1230 |
+
indices of `V` index the points ``(x, y, z)`` and the last index encodes
|
| 1231 |
+
the powers of `x`, `y`, and `z`.
|
| 1232 |
+
|
| 1233 |
+
If ``V = polyvander3d(x, y, z, [xdeg, ydeg, zdeg])``, then the columns
|
| 1234 |
+
of `V` correspond to the elements of a 3-D coefficient array `c` of
|
| 1235 |
+
shape (xdeg + 1, ydeg + 1, zdeg + 1) in the order
|
| 1236 |
+
|
| 1237 |
+
.. math:: c_{000}, c_{001}, c_{002},... , c_{010}, c_{011}, c_{012},...
|
| 1238 |
+
|
| 1239 |
+
and ``np.dot(V, c.flat)`` and ``polyval3d(x, y, z, c)`` will be the
|
| 1240 |
+
same up to roundoff. This equivalence is useful both for least squares
|
| 1241 |
+
fitting and for the evaluation of a large number of 3-D polynomials
|
| 1242 |
+
of the same degrees and sample points.
|
| 1243 |
+
|
| 1244 |
+
Parameters
|
| 1245 |
+
----------
|
| 1246 |
+
x, y, z : array_like
|
| 1247 |
+
Arrays of point coordinates, all of the same shape. The dtypes will
|
| 1248 |
+
be converted to either float64 or complex128 depending on whether
|
| 1249 |
+
any of the elements are complex. Scalars are converted to 1-D
|
| 1250 |
+
arrays.
|
| 1251 |
+
deg : list of ints
|
| 1252 |
+
List of maximum degrees of the form [x_deg, y_deg, z_deg].
|
| 1253 |
+
|
| 1254 |
+
Returns
|
| 1255 |
+
-------
|
| 1256 |
+
vander3d : ndarray
|
| 1257 |
+
The shape of the returned matrix is ``x.shape + (order,)``, where
|
| 1258 |
+
:math:`order = (deg[0]+1)*(deg([1]+1)*(deg[2]+1)`. The dtype will
|
| 1259 |
+
be the same as the converted `x`, `y`, and `z`.
|
| 1260 |
+
|
| 1261 |
+
See Also
|
| 1262 |
+
--------
|
| 1263 |
+
polyvander, polyvander3d, polyval2d, polyval3d
|
| 1264 |
+
|
| 1265 |
+
Examples
|
| 1266 |
+
--------
|
| 1267 |
+
>>> import numpy as np
|
| 1268 |
+
>>> from numpy.polynomial import polynomial as P
|
| 1269 |
+
>>> x = np.asarray([-1, 2, 1])
|
| 1270 |
+
>>> y = np.asarray([1, -2, -3])
|
| 1271 |
+
>>> z = np.asarray([2, 2, 5])
|
| 1272 |
+
>>> l, m, n = [2, 2, 1]
|
| 1273 |
+
>>> deg = [l, m, n]
|
| 1274 |
+
>>> V = P.polyvander3d(x=x, y=y, z=z, deg=deg)
|
| 1275 |
+
>>> V
|
| 1276 |
+
array([[ 1., 2., 1., 2., 1., 2., -1., -2., -1.,
|
| 1277 |
+
-2., -1., -2., 1., 2., 1., 2., 1., 2.],
|
| 1278 |
+
[ 1., 2., -2., -4., 4., 8., 2., 4., -4.,
|
| 1279 |
+
-8., 8., 16., 4., 8., -8., -16., 16., 32.],
|
| 1280 |
+
[ 1., 5., -3., -15., 9., 45., 1., 5., -3.,
|
| 1281 |
+
-15., 9., 45., 1., 5., -3., -15., 9., 45.]])
|
| 1282 |
+
|
| 1283 |
+
We can verify the columns for any ``0 <= i <= l``, ``0 <= j <= m``,
|
| 1284 |
+
and ``0 <= k <= n``
|
| 1285 |
+
|
| 1286 |
+
>>> i, j, k = 2, 1, 0
|
| 1287 |
+
>>> V[:, (m+1)*(n+1)*i + (n+1)*j + k] == x**i * y**j * z**k
|
| 1288 |
+
array([ True, True, True])
|
| 1289 |
+
|
| 1290 |
+
"""
|
| 1291 |
+
return pu._vander_nd_flat((polyvander, polyvander, polyvander), (x, y, z), deg)
|
| 1292 |
+
|
| 1293 |
+
|
| 1294 |
+
def polyfit(x, y, deg, rcond=None, full=False, w=None):
|
| 1295 |
+
"""
|
| 1296 |
+
Least-squares fit of a polynomial to data.
|
| 1297 |
+
|
| 1298 |
+
Return the coefficients of a polynomial of degree `deg` that is the
|
| 1299 |
+
least squares fit to the data values `y` given at points `x`. If `y` is
|
| 1300 |
+
1-D the returned coefficients will also be 1-D. If `y` is 2-D multiple
|
| 1301 |
+
fits are done, one for each column of `y`, and the resulting
|
| 1302 |
+
coefficients are stored in the corresponding columns of a 2-D return.
|
| 1303 |
+
The fitted polynomial(s) are in the form
|
| 1304 |
+
|
| 1305 |
+
.. math:: p(x) = c_0 + c_1 * x + ... + c_n * x^n,
|
| 1306 |
+
|
| 1307 |
+
where `n` is `deg`.
|
| 1308 |
+
|
| 1309 |
+
Parameters
|
| 1310 |
+
----------
|
| 1311 |
+
x : array_like, shape (`M`,)
|
| 1312 |
+
x-coordinates of the `M` sample (data) points ``(x[i], y[i])``.
|
| 1313 |
+
y : array_like, shape (`M`,) or (`M`, `K`)
|
| 1314 |
+
y-coordinates of the sample points. Several sets of sample points
|
| 1315 |
+
sharing the same x-coordinates can be (independently) fit with one
|
| 1316 |
+
call to `polyfit` by passing in for `y` a 2-D array that contains
|
| 1317 |
+
one data set per column.
|
| 1318 |
+
deg : int or 1-D array_like
|
| 1319 |
+
Degree(s) of the fitting polynomials. If `deg` is a single integer
|
| 1320 |
+
all terms up to and including the `deg`'th term are included in the
|
| 1321 |
+
fit. For NumPy versions >= 1.11.0 a list of integers specifying the
|
| 1322 |
+
degrees of the terms to include may be used instead.
|
| 1323 |
+
rcond : float, optional
|
| 1324 |
+
Relative condition number of the fit. Singular values smaller
|
| 1325 |
+
than `rcond`, relative to the largest singular value, will be
|
| 1326 |
+
ignored. The default value is ``len(x)*eps``, where `eps` is the
|
| 1327 |
+
relative precision of the platform's float type, about 2e-16 in
|
| 1328 |
+
most cases.
|
| 1329 |
+
full : bool, optional
|
| 1330 |
+
Switch determining the nature of the return value. When ``False``
|
| 1331 |
+
(the default) just the coefficients are returned; when ``True``,
|
| 1332 |
+
diagnostic information from the singular value decomposition (used
|
| 1333 |
+
to solve the fit's matrix equation) is also returned.
|
| 1334 |
+
w : array_like, shape (`M`,), optional
|
| 1335 |
+
Weights. If not None, the weight ``w[i]`` applies to the unsquared
|
| 1336 |
+
residual ``y[i] - y_hat[i]`` at ``x[i]``. Ideally the weights are
|
| 1337 |
+
chosen so that the errors of the products ``w[i]*y[i]`` all have the
|
| 1338 |
+
same variance. When using inverse-variance weighting, use
|
| 1339 |
+
``w[i] = 1/sigma(y[i])``. The default value is None.
|
| 1340 |
+
|
| 1341 |
+
Returns
|
| 1342 |
+
-------
|
| 1343 |
+
coef : ndarray, shape (`deg` + 1,) or (`deg` + 1, `K`)
|
| 1344 |
+
Polynomial coefficients ordered from low to high. If `y` was 2-D,
|
| 1345 |
+
the coefficients in column `k` of `coef` represent the polynomial
|
| 1346 |
+
fit to the data in `y`'s `k`-th column.
|
| 1347 |
+
|
| 1348 |
+
[residuals, rank, singular_values, rcond] : list
|
| 1349 |
+
These values are only returned if ``full == True``
|
| 1350 |
+
|
| 1351 |
+
- residuals -- sum of squared residuals of the least squares fit
|
| 1352 |
+
- rank -- the numerical rank of the scaled Vandermonde matrix
|
| 1353 |
+
- singular_values -- singular values of the scaled Vandermonde matrix
|
| 1354 |
+
- rcond -- value of `rcond`.
|
| 1355 |
+
|
| 1356 |
+
For more details, see `numpy.linalg.lstsq`.
|
| 1357 |
+
|
| 1358 |
+
Raises
|
| 1359 |
+
------
|
| 1360 |
+
RankWarning
|
| 1361 |
+
Raised if the matrix in the least-squares fit is rank deficient.
|
| 1362 |
+
The warning is only raised if ``full == False``. The warnings can
|
| 1363 |
+
be turned off by:
|
| 1364 |
+
|
| 1365 |
+
>>> import warnings
|
| 1366 |
+
>>> warnings.simplefilter('ignore', np.exceptions.RankWarning)
|
| 1367 |
+
|
| 1368 |
+
See Also
|
| 1369 |
+
--------
|
| 1370 |
+
numpy.polynomial.chebyshev.chebfit
|
| 1371 |
+
numpy.polynomial.legendre.legfit
|
| 1372 |
+
numpy.polynomial.laguerre.lagfit
|
| 1373 |
+
numpy.polynomial.hermite.hermfit
|
| 1374 |
+
numpy.polynomial.hermite_e.hermefit
|
| 1375 |
+
polyval : Evaluates a polynomial.
|
| 1376 |
+
polyvander : Vandermonde matrix for powers.
|
| 1377 |
+
numpy.linalg.lstsq : Computes a least-squares fit from the matrix.
|
| 1378 |
+
scipy.interpolate.UnivariateSpline : Computes spline fits.
|
| 1379 |
+
|
| 1380 |
+
Notes
|
| 1381 |
+
-----
|
| 1382 |
+
The solution is the coefficients of the polynomial `p` that minimizes
|
| 1383 |
+
the sum of the weighted squared errors
|
| 1384 |
+
|
| 1385 |
+
.. math:: E = \\sum_j w_j^2 * |y_j - p(x_j)|^2,
|
| 1386 |
+
|
| 1387 |
+
where the :math:`w_j` are the weights. This problem is solved by
|
| 1388 |
+
setting up the (typically) over-determined matrix equation:
|
| 1389 |
+
|
| 1390 |
+
.. math:: V(x) * c = w * y,
|
| 1391 |
+
|
| 1392 |
+
where `V` is the weighted pseudo Vandermonde matrix of `x`, `c` are the
|
| 1393 |
+
coefficients to be solved for, `w` are the weights, and `y` are the
|
| 1394 |
+
observed values. This equation is then solved using the singular value
|
| 1395 |
+
decomposition of `V`.
|
| 1396 |
+
|
| 1397 |
+
If some of the singular values of `V` are so small that they are
|
| 1398 |
+
neglected (and `full` == ``False``), a `~exceptions.RankWarning` will be
|
| 1399 |
+
raised. This means that the coefficient values may be poorly determined.
|
| 1400 |
+
Fitting to a lower order polynomial will usually get rid of the warning
|
| 1401 |
+
(but may not be what you want, of course; if you have independent
|
| 1402 |
+
reason(s) for choosing the degree which isn't working, you may have to:
|
| 1403 |
+
a) reconsider those reasons, and/or b) reconsider the quality of your
|
| 1404 |
+
data). The `rcond` parameter can also be set to a value smaller than
|
| 1405 |
+
its default, but the resulting fit may be spurious and have large
|
| 1406 |
+
contributions from roundoff error.
|
| 1407 |
+
|
| 1408 |
+
Polynomial fits using double precision tend to "fail" at about
|
| 1409 |
+
(polynomial) degree 20. Fits using Chebyshev or Legendre series are
|
| 1410 |
+
generally better conditioned, but much can still depend on the
|
| 1411 |
+
distribution of the sample points and the smoothness of the data. If
|
| 1412 |
+
the quality of the fit is inadequate, splines may be a good
|
| 1413 |
+
alternative.
|
| 1414 |
+
|
| 1415 |
+
Examples
|
| 1416 |
+
--------
|
| 1417 |
+
>>> import numpy as np
|
| 1418 |
+
>>> from numpy.polynomial import polynomial as P
|
| 1419 |
+
>>> x = np.linspace(-1,1,51) # x "data": [-1, -0.96, ..., 0.96, 1]
|
| 1420 |
+
>>> rng = np.random.default_rng()
|
| 1421 |
+
>>> err = rng.normal(size=len(x))
|
| 1422 |
+
>>> y = x**3 - x + err # x^3 - x + Gaussian noise
|
| 1423 |
+
>>> c, stats = P.polyfit(x,y,3,full=True)
|
| 1424 |
+
>>> c # c[0], c[1] approx. -1, c[2] should be approx. 0, c[3] approx. 1
|
| 1425 |
+
array([ 0.23111996, -1.02785049, -0.2241444 , 1.08405657]) # may vary
|
| 1426 |
+
>>> stats # note the large SSR, explaining the rather poor results
|
| 1427 |
+
[array([48.312088]), # may vary
|
| 1428 |
+
4,
|
| 1429 |
+
array([1.38446749, 1.32119158, 0.50443316, 0.28853036]),
|
| 1430 |
+
1.1324274851176597e-14]
|
| 1431 |
+
|
| 1432 |
+
Same thing without the added noise
|
| 1433 |
+
|
| 1434 |
+
>>> y = x**3 - x
|
| 1435 |
+
>>> c, stats = P.polyfit(x,y,3,full=True)
|
| 1436 |
+
>>> c # c[0], c[1] ~= -1, c[2] should be "very close to 0", c[3] ~= 1
|
| 1437 |
+
array([-6.73496154e-17, -1.00000000e+00, 0.00000000e+00, 1.00000000e+00])
|
| 1438 |
+
>>> stats # note the minuscule SSR
|
| 1439 |
+
[array([8.79579319e-31]),
|
| 1440 |
+
np.int32(4),
|
| 1441 |
+
array([1.38446749, 1.32119158, 0.50443316, 0.28853036]),
|
| 1442 |
+
1.1324274851176597e-14]
|
| 1443 |
+
|
| 1444 |
+
"""
|
| 1445 |
+
return pu._fit(polyvander, x, y, deg, rcond, full, w)
|
| 1446 |
+
|
| 1447 |
+
|
| 1448 |
+
def polycompanion(c):
|
| 1449 |
+
"""
|
| 1450 |
+
Return the companion matrix of c.
|
| 1451 |
+
|
| 1452 |
+
The companion matrix for power series cannot be made symmetric by
|
| 1453 |
+
scaling the basis, so this function differs from those for the
|
| 1454 |
+
orthogonal polynomials.
|
| 1455 |
+
|
| 1456 |
+
Parameters
|
| 1457 |
+
----------
|
| 1458 |
+
c : array_like
|
| 1459 |
+
1-D array of polynomial coefficients ordered from low to high
|
| 1460 |
+
degree.
|
| 1461 |
+
|
| 1462 |
+
Returns
|
| 1463 |
+
-------
|
| 1464 |
+
mat : ndarray
|
| 1465 |
+
Companion matrix of dimensions (deg, deg).
|
| 1466 |
+
|
| 1467 |
+
Examples
|
| 1468 |
+
--------
|
| 1469 |
+
>>> from numpy.polynomial import polynomial as P
|
| 1470 |
+
>>> c = (1, 2, 3)
|
| 1471 |
+
>>> P.polycompanion(c)
|
| 1472 |
+
array([[ 0. , -0.33333333],
|
| 1473 |
+
[ 1. , -0.66666667]])
|
| 1474 |
+
|
| 1475 |
+
"""
|
| 1476 |
+
# c is a trimmed copy
|
| 1477 |
+
[c] = pu.as_series([c])
|
| 1478 |
+
if len(c) < 2:
|
| 1479 |
+
raise ValueError('Series must have maximum degree of at least 1.')
|
| 1480 |
+
if len(c) == 2:
|
| 1481 |
+
return np.array([[-c[0] / c[1]]])
|
| 1482 |
+
|
| 1483 |
+
n = len(c) - 1
|
| 1484 |
+
mat = np.zeros((n, n), dtype=c.dtype)
|
| 1485 |
+
bot = mat.reshape(-1)[n::n + 1]
|
| 1486 |
+
bot[...] = 1
|
| 1487 |
+
mat[:, -1] -= c[:-1] / c[-1]
|
| 1488 |
+
return mat
|
| 1489 |
+
|
| 1490 |
+
|
| 1491 |
+
def polyroots(c):
|
| 1492 |
+
"""
|
| 1493 |
+
Compute the roots of a polynomial.
|
| 1494 |
+
|
| 1495 |
+
Return the roots (a.k.a. "zeros") of the polynomial
|
| 1496 |
+
|
| 1497 |
+
.. math:: p(x) = \\sum_i c[i] * x^i.
|
| 1498 |
+
|
| 1499 |
+
Parameters
|
| 1500 |
+
----------
|
| 1501 |
+
c : 1-D array_like
|
| 1502 |
+
1-D array of polynomial coefficients.
|
| 1503 |
+
|
| 1504 |
+
Returns
|
| 1505 |
+
-------
|
| 1506 |
+
out : ndarray
|
| 1507 |
+
Array of the roots of the polynomial. If all the roots are real,
|
| 1508 |
+
then `out` is also real, otherwise it is complex.
|
| 1509 |
+
|
| 1510 |
+
See Also
|
| 1511 |
+
--------
|
| 1512 |
+
numpy.polynomial.chebyshev.chebroots
|
| 1513 |
+
numpy.polynomial.legendre.legroots
|
| 1514 |
+
numpy.polynomial.laguerre.lagroots
|
| 1515 |
+
numpy.polynomial.hermite.hermroots
|
| 1516 |
+
numpy.polynomial.hermite_e.hermeroots
|
| 1517 |
+
|
| 1518 |
+
Notes
|
| 1519 |
+
-----
|
| 1520 |
+
The root estimates are obtained as the eigenvalues of the companion
|
| 1521 |
+
matrix, Roots far from the origin of the complex plane may have large
|
| 1522 |
+
errors due to the numerical instability of the power series for such
|
| 1523 |
+
values. Roots with multiplicity greater than 1 will also show larger
|
| 1524 |
+
errors as the value of the series near such points is relatively
|
| 1525 |
+
insensitive to errors in the roots. Isolated roots near the origin can
|
| 1526 |
+
be improved by a few iterations of Newton's method.
|
| 1527 |
+
|
| 1528 |
+
Examples
|
| 1529 |
+
--------
|
| 1530 |
+
>>> import numpy.polynomial.polynomial as poly
|
| 1531 |
+
>>> poly.polyroots(poly.polyfromroots((-1,0,1)))
|
| 1532 |
+
array([-1., 0., 1.])
|
| 1533 |
+
>>> poly.polyroots(poly.polyfromroots((-1,0,1))).dtype
|
| 1534 |
+
dtype('float64')
|
| 1535 |
+
>>> j = complex(0,1)
|
| 1536 |
+
>>> poly.polyroots(poly.polyfromroots((-j,0,j)))
|
| 1537 |
+
array([ 0.00000000e+00+0.j, 0.00000000e+00+1.j, 2.77555756e-17-1.j]) # may vary
|
| 1538 |
+
|
| 1539 |
+
""" # noqa: E501
|
| 1540 |
+
# c is a trimmed copy
|
| 1541 |
+
[c] = pu.as_series([c])
|
| 1542 |
+
if len(c) < 2:
|
| 1543 |
+
return np.array([], dtype=c.dtype)
|
| 1544 |
+
if len(c) == 2:
|
| 1545 |
+
return np.array([-c[0] / c[1]])
|
| 1546 |
+
|
| 1547 |
+
m = polycompanion(c)
|
| 1548 |
+
r = np.linalg.eigvals(m)
|
| 1549 |
+
r.sort()
|
| 1550 |
+
return r
|
| 1551 |
+
|
| 1552 |
+
|
| 1553 |
+
#
|
| 1554 |
+
# polynomial class
|
| 1555 |
+
#
|
| 1556 |
+
|
| 1557 |
+
class Polynomial(ABCPolyBase):
|
| 1558 |
+
"""A power series class.
|
| 1559 |
+
|
| 1560 |
+
The Polynomial class provides the standard Python numerical methods
|
| 1561 |
+
'+', '-', '*', '//', '%', 'divmod', '**', and '()' as well as the
|
| 1562 |
+
attributes and methods listed below.
|
| 1563 |
+
|
| 1564 |
+
Parameters
|
| 1565 |
+
----------
|
| 1566 |
+
coef : array_like
|
| 1567 |
+
Polynomial coefficients in order of increasing degree, i.e.,
|
| 1568 |
+
``(1, 2, 3)`` give ``1 + 2*x + 3*x**2``.
|
| 1569 |
+
domain : (2,) array_like, optional
|
| 1570 |
+
Domain to use. The interval ``[domain[0], domain[1]]`` is mapped
|
| 1571 |
+
to the interval ``[window[0], window[1]]`` by shifting and scaling.
|
| 1572 |
+
The default value is [-1., 1.].
|
| 1573 |
+
window : (2,) array_like, optional
|
| 1574 |
+
Window, see `domain` for its use. The default value is [-1., 1.].
|
| 1575 |
+
symbol : str, optional
|
| 1576 |
+
Symbol used to represent the independent variable in string
|
| 1577 |
+
representations of the polynomial expression, e.g. for printing.
|
| 1578 |
+
The symbol must be a valid Python identifier. Default value is 'x'.
|
| 1579 |
+
|
| 1580 |
+
.. versionadded:: 1.24
|
| 1581 |
+
|
| 1582 |
+
"""
|
| 1583 |
+
# Virtual Functions
|
| 1584 |
+
_add = staticmethod(polyadd)
|
| 1585 |
+
_sub = staticmethod(polysub)
|
| 1586 |
+
_mul = staticmethod(polymul)
|
| 1587 |
+
_div = staticmethod(polydiv)
|
| 1588 |
+
_pow = staticmethod(polypow)
|
| 1589 |
+
_val = staticmethod(polyval)
|
| 1590 |
+
_int = staticmethod(polyint)
|
| 1591 |
+
_der = staticmethod(polyder)
|
| 1592 |
+
_fit = staticmethod(polyfit)
|
| 1593 |
+
_line = staticmethod(polyline)
|
| 1594 |
+
_roots = staticmethod(polyroots)
|
| 1595 |
+
_fromroots = staticmethod(polyfromroots)
|
| 1596 |
+
|
| 1597 |
+
# Virtual properties
|
| 1598 |
+
domain = np.array(polydomain)
|
| 1599 |
+
window = np.array(polydomain)
|
| 1600 |
+
basis_name = None
|
| 1601 |
+
|
| 1602 |
+
@classmethod
|
| 1603 |
+
def _str_term_unicode(cls, i, arg_str):
|
| 1604 |
+
if i == '1':
|
| 1605 |
+
return f"·{arg_str}"
|
| 1606 |
+
else:
|
| 1607 |
+
return f"·{arg_str}{i.translate(cls._superscript_mapping)}"
|
| 1608 |
+
|
| 1609 |
+
@staticmethod
|
| 1610 |
+
def _str_term_ascii(i, arg_str):
|
| 1611 |
+
if i == '1':
|
| 1612 |
+
return f" {arg_str}"
|
| 1613 |
+
else:
|
| 1614 |
+
return f" {arg_str}**{i}"
|
| 1615 |
+
|
| 1616 |
+
@staticmethod
|
| 1617 |
+
def _repr_latex_term(i, arg_str, needs_parens):
|
| 1618 |
+
if needs_parens:
|
| 1619 |
+
arg_str = rf"\left({arg_str}\right)"
|
| 1620 |
+
if i == 0:
|
| 1621 |
+
return '1'
|
| 1622 |
+
elif i == 1:
|
| 1623 |
+
return arg_str
|
| 1624 |
+
else:
|
| 1625 |
+
return f"{arg_str}^{{{i}}}"
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/polynomial.pyi
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, ClassVar, Final, overload
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
import numpy.typing as npt
|
| 5 |
+
from numpy._typing import (
|
| 6 |
+
_ArrayLikeFloat_co,
|
| 7 |
+
_ArrayLikeNumber_co,
|
| 8 |
+
_FloatLike_co,
|
| 9 |
+
_NumberLike_co,
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
from ._polybase import ABCPolyBase
|
| 13 |
+
from ._polytypes import (
|
| 14 |
+
_Array1,
|
| 15 |
+
_Array2,
|
| 16 |
+
_ArrayLikeCoef_co,
|
| 17 |
+
_FuncBinOp,
|
| 18 |
+
_FuncCompanion,
|
| 19 |
+
_FuncDer,
|
| 20 |
+
_FuncFit,
|
| 21 |
+
_FuncFromRoots,
|
| 22 |
+
_FuncInteg,
|
| 23 |
+
_FuncLine,
|
| 24 |
+
_FuncPow,
|
| 25 |
+
_FuncRoots,
|
| 26 |
+
_FuncUnOp,
|
| 27 |
+
_FuncVal,
|
| 28 |
+
_FuncVal2D,
|
| 29 |
+
_FuncVal3D,
|
| 30 |
+
_FuncVander,
|
| 31 |
+
_FuncVander2D,
|
| 32 |
+
_FuncVander3D,
|
| 33 |
+
)
|
| 34 |
+
from .polyutils import trimcoef as polytrim
|
| 35 |
+
|
| 36 |
+
__all__ = [
|
| 37 |
+
"polyzero",
|
| 38 |
+
"polyone",
|
| 39 |
+
"polyx",
|
| 40 |
+
"polydomain",
|
| 41 |
+
"polyline",
|
| 42 |
+
"polyadd",
|
| 43 |
+
"polysub",
|
| 44 |
+
"polymulx",
|
| 45 |
+
"polymul",
|
| 46 |
+
"polydiv",
|
| 47 |
+
"polypow",
|
| 48 |
+
"polyval",
|
| 49 |
+
"polyvalfromroots",
|
| 50 |
+
"polyder",
|
| 51 |
+
"polyint",
|
| 52 |
+
"polyfromroots",
|
| 53 |
+
"polyvander",
|
| 54 |
+
"polyfit",
|
| 55 |
+
"polytrim",
|
| 56 |
+
"polyroots",
|
| 57 |
+
"Polynomial",
|
| 58 |
+
"polyval2d",
|
| 59 |
+
"polyval3d",
|
| 60 |
+
"polygrid2d",
|
| 61 |
+
"polygrid3d",
|
| 62 |
+
"polyvander2d",
|
| 63 |
+
"polyvander3d",
|
| 64 |
+
"polycompanion",
|
| 65 |
+
]
|
| 66 |
+
|
| 67 |
+
polydomain: Final[_Array2[np.float64]] = ...
|
| 68 |
+
polyzero: Final[_Array1[np.int_]] = ...
|
| 69 |
+
polyone: Final[_Array1[np.int_]] = ...
|
| 70 |
+
polyx: Final[_Array2[np.int_]] = ...
|
| 71 |
+
|
| 72 |
+
polyline: Final[_FuncLine] = ...
|
| 73 |
+
polyfromroots: Final[_FuncFromRoots] = ...
|
| 74 |
+
polyadd: Final[_FuncBinOp] = ...
|
| 75 |
+
polysub: Final[_FuncBinOp] = ...
|
| 76 |
+
polymulx: Final[_FuncUnOp] = ...
|
| 77 |
+
polymul: Final[_FuncBinOp] = ...
|
| 78 |
+
polydiv: Final[_FuncBinOp] = ...
|
| 79 |
+
polypow: Final[_FuncPow] = ...
|
| 80 |
+
polyder: Final[_FuncDer] = ...
|
| 81 |
+
polyint: Final[_FuncInteg] = ...
|
| 82 |
+
polyval: Final[_FuncVal] = ...
|
| 83 |
+
polyval2d: Final[_FuncVal2D] = ...
|
| 84 |
+
polyval3d: Final[_FuncVal3D] = ...
|
| 85 |
+
|
| 86 |
+
@overload
|
| 87 |
+
def polyvalfromroots(x: _FloatLike_co, r: _FloatLike_co, tensor: bool = True) -> np.float64 | Any: ...
|
| 88 |
+
@overload
|
| 89 |
+
def polyvalfromroots(x: _NumberLike_co, r: _NumberLike_co, tensor: bool = True) -> np.complex128 | Any: ...
|
| 90 |
+
@overload
|
| 91 |
+
def polyvalfromroots(x: _ArrayLikeFloat_co, r: _ArrayLikeFloat_co, tensor: bool = True) -> npt.NDArray[np.float64 | Any]: ...
|
| 92 |
+
@overload
|
| 93 |
+
def polyvalfromroots(x: _ArrayLikeNumber_co, r: _ArrayLikeNumber_co, tensor: bool = True) -> npt.NDArray[np.complex128 | Any]: ...
|
| 94 |
+
@overload
|
| 95 |
+
def polyvalfromroots(x: _ArrayLikeCoef_co, r: _ArrayLikeCoef_co, tensor: bool = True) -> npt.NDArray[np.object_ | Any]: ...
|
| 96 |
+
|
| 97 |
+
polygrid2d: Final[_FuncVal2D] = ...
|
| 98 |
+
polygrid3d: Final[_FuncVal3D] = ...
|
| 99 |
+
polyvander: Final[_FuncVander] = ...
|
| 100 |
+
polyvander2d: Final[_FuncVander2D] = ...
|
| 101 |
+
polyvander3d: Final[_FuncVander3D] = ...
|
| 102 |
+
polyfit: Final[_FuncFit] = ...
|
| 103 |
+
polycompanion: Final[_FuncCompanion] = ...
|
| 104 |
+
polyroots: Final[_FuncRoots] = ...
|
| 105 |
+
|
| 106 |
+
class Polynomial(ABCPolyBase[None]):
|
| 107 |
+
basis_name: ClassVar[None] = None # pyright: ignore[reportIncompatibleMethodOverride]
|
| 108 |
+
domain: _Array2[np.float64 | Any] = ... # pyright: ignore[reportIncompatibleMethodOverride]
|
| 109 |
+
window: _Array2[np.float64 | Any] = ... # pyright: ignore[reportIncompatibleMethodOverride]
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/polyutils.py
ADDED
|
@@ -0,0 +1,759 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Utility classes and functions for the polynomial modules.
|
| 3 |
+
|
| 4 |
+
This module provides: error and warning objects; a polynomial base class;
|
| 5 |
+
and some routines used in both the `polynomial` and `chebyshev` modules.
|
| 6 |
+
|
| 7 |
+
Functions
|
| 8 |
+
---------
|
| 9 |
+
|
| 10 |
+
.. autosummary::
|
| 11 |
+
:toctree: generated/
|
| 12 |
+
|
| 13 |
+
as_series convert list of array_likes into 1-D arrays of common type.
|
| 14 |
+
trimseq remove trailing zeros.
|
| 15 |
+
trimcoef remove small trailing coefficients.
|
| 16 |
+
getdomain return the domain appropriate for a given set of abscissae.
|
| 17 |
+
mapdomain maps points between domains.
|
| 18 |
+
mapparms parameters of the linear map between domains.
|
| 19 |
+
|
| 20 |
+
"""
|
| 21 |
+
import functools
|
| 22 |
+
import operator
|
| 23 |
+
import warnings
|
| 24 |
+
|
| 25 |
+
import numpy as np
|
| 26 |
+
|
| 27 |
+
__all__ = [
|
| 28 |
+
'as_series', 'trimseq', 'trimcoef', 'getdomain', 'mapdomain', 'mapparms',
|
| 29 |
+
'format_float']
|
| 30 |
+
|
| 31 |
+
#
|
| 32 |
+
# Helper functions to convert inputs to 1-D arrays
|
| 33 |
+
#
|
| 34 |
+
def trimseq(seq):
|
| 35 |
+
"""Remove small Poly series coefficients.
|
| 36 |
+
|
| 37 |
+
Parameters
|
| 38 |
+
----------
|
| 39 |
+
seq : sequence
|
| 40 |
+
Sequence of Poly series coefficients.
|
| 41 |
+
|
| 42 |
+
Returns
|
| 43 |
+
-------
|
| 44 |
+
series : sequence
|
| 45 |
+
Subsequence with trailing zeros removed. If the resulting sequence
|
| 46 |
+
would be empty, return the first element. The returned sequence may
|
| 47 |
+
or may not be a view.
|
| 48 |
+
|
| 49 |
+
Notes
|
| 50 |
+
-----
|
| 51 |
+
Do not lose the type info if the sequence contains unknown objects.
|
| 52 |
+
|
| 53 |
+
"""
|
| 54 |
+
if len(seq) == 0 or seq[-1] != 0:
|
| 55 |
+
return seq
|
| 56 |
+
else:
|
| 57 |
+
for i in range(len(seq) - 1, -1, -1):
|
| 58 |
+
if seq[i] != 0:
|
| 59 |
+
break
|
| 60 |
+
return seq[:i + 1]
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def as_series(alist, trim=True):
|
| 64 |
+
"""
|
| 65 |
+
Return argument as a list of 1-d arrays.
|
| 66 |
+
|
| 67 |
+
The returned list contains array(s) of dtype double, complex double, or
|
| 68 |
+
object. A 1-d argument of shape ``(N,)`` is parsed into ``N`` arrays of
|
| 69 |
+
size one; a 2-d argument of shape ``(M,N)`` is parsed into ``M`` arrays
|
| 70 |
+
of size ``N`` (i.e., is "parsed by row"); and a higher dimensional array
|
| 71 |
+
raises a Value Error if it is not first reshaped into either a 1-d or 2-d
|
| 72 |
+
array.
|
| 73 |
+
|
| 74 |
+
Parameters
|
| 75 |
+
----------
|
| 76 |
+
alist : array_like
|
| 77 |
+
A 1- or 2-d array_like
|
| 78 |
+
trim : boolean, optional
|
| 79 |
+
When True, trailing zeros are removed from the inputs.
|
| 80 |
+
When False, the inputs are passed through intact.
|
| 81 |
+
|
| 82 |
+
Returns
|
| 83 |
+
-------
|
| 84 |
+
[a1, a2,...] : list of 1-D arrays
|
| 85 |
+
A copy of the input data as a list of 1-d arrays.
|
| 86 |
+
|
| 87 |
+
Raises
|
| 88 |
+
------
|
| 89 |
+
ValueError
|
| 90 |
+
Raised when `as_series` cannot convert its input to 1-d arrays, or at
|
| 91 |
+
least one of the resulting arrays is empty.
|
| 92 |
+
|
| 93 |
+
Examples
|
| 94 |
+
--------
|
| 95 |
+
>>> import numpy as np
|
| 96 |
+
>>> from numpy.polynomial import polyutils as pu
|
| 97 |
+
>>> a = np.arange(4)
|
| 98 |
+
>>> pu.as_series(a)
|
| 99 |
+
[array([0.]), array([1.]), array([2.]), array([3.])]
|
| 100 |
+
>>> b = np.arange(6).reshape((2,3))
|
| 101 |
+
>>> pu.as_series(b)
|
| 102 |
+
[array([0., 1., 2.]), array([3., 4., 5.])]
|
| 103 |
+
|
| 104 |
+
>>> pu.as_series((1, np.arange(3), np.arange(2, dtype=np.float16)))
|
| 105 |
+
[array([1.]), array([0., 1., 2.]), array([0., 1.])]
|
| 106 |
+
|
| 107 |
+
>>> pu.as_series([2, [1.1, 0.]])
|
| 108 |
+
[array([2.]), array([1.1])]
|
| 109 |
+
|
| 110 |
+
>>> pu.as_series([2, [1.1, 0.]], trim=False)
|
| 111 |
+
[array([2.]), array([1.1, 0. ])]
|
| 112 |
+
|
| 113 |
+
"""
|
| 114 |
+
arrays = [np.array(a, ndmin=1, copy=None) for a in alist]
|
| 115 |
+
for a in arrays:
|
| 116 |
+
if a.size == 0:
|
| 117 |
+
raise ValueError("Coefficient array is empty")
|
| 118 |
+
if a.ndim != 1:
|
| 119 |
+
raise ValueError("Coefficient array is not 1-d")
|
| 120 |
+
if trim:
|
| 121 |
+
arrays = [trimseq(a) for a in arrays]
|
| 122 |
+
|
| 123 |
+
try:
|
| 124 |
+
dtype = np.common_type(*arrays)
|
| 125 |
+
except Exception as e:
|
| 126 |
+
object_dtype = np.dtypes.ObjectDType()
|
| 127 |
+
has_one_object_type = False
|
| 128 |
+
ret = []
|
| 129 |
+
for a in arrays:
|
| 130 |
+
if a.dtype != object_dtype:
|
| 131 |
+
tmp = np.empty(len(a), dtype=object_dtype)
|
| 132 |
+
tmp[:] = a[:]
|
| 133 |
+
ret.append(tmp)
|
| 134 |
+
else:
|
| 135 |
+
has_one_object_type = True
|
| 136 |
+
ret.append(a.copy())
|
| 137 |
+
if not has_one_object_type:
|
| 138 |
+
raise ValueError("Coefficient arrays have no common type") from e
|
| 139 |
+
else:
|
| 140 |
+
ret = [np.array(a, copy=True, dtype=dtype) for a in arrays]
|
| 141 |
+
return ret
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
def trimcoef(c, tol=0):
|
| 145 |
+
"""
|
| 146 |
+
Remove "small" "trailing" coefficients from a polynomial.
|
| 147 |
+
|
| 148 |
+
"Small" means "small in absolute value" and is controlled by the
|
| 149 |
+
parameter `tol`; "trailing" means highest order coefficient(s), e.g., in
|
| 150 |
+
``[0, 1, 1, 0, 0]`` (which represents ``0 + x + x**2 + 0*x**3 + 0*x**4``)
|
| 151 |
+
both the 3-rd and 4-th order coefficients would be "trimmed."
|
| 152 |
+
|
| 153 |
+
Parameters
|
| 154 |
+
----------
|
| 155 |
+
c : array_like
|
| 156 |
+
1-d array of coefficients, ordered from lowest order to highest.
|
| 157 |
+
tol : number, optional
|
| 158 |
+
Trailing (i.e., highest order) elements with absolute value less
|
| 159 |
+
than or equal to `tol` (default value is zero) are removed.
|
| 160 |
+
|
| 161 |
+
Returns
|
| 162 |
+
-------
|
| 163 |
+
trimmed : ndarray
|
| 164 |
+
1-d array with trailing zeros removed. If the resulting series
|
| 165 |
+
would be empty, a series containing a single zero is returned.
|
| 166 |
+
|
| 167 |
+
Raises
|
| 168 |
+
------
|
| 169 |
+
ValueError
|
| 170 |
+
If `tol` < 0
|
| 171 |
+
|
| 172 |
+
Examples
|
| 173 |
+
--------
|
| 174 |
+
>>> from numpy.polynomial import polyutils as pu
|
| 175 |
+
>>> pu.trimcoef((0,0,3,0,5,0,0))
|
| 176 |
+
array([0., 0., 3., 0., 5.])
|
| 177 |
+
>>> pu.trimcoef((0,0,1e-3,0,1e-5,0,0),1e-3) # item == tol is trimmed
|
| 178 |
+
array([0.])
|
| 179 |
+
>>> i = complex(0,1) # works for complex
|
| 180 |
+
>>> pu.trimcoef((3e-4,1e-3*(1-i),5e-4,2e-5*(1+i)), 1e-3)
|
| 181 |
+
array([0.0003+0.j , 0.001 -0.001j])
|
| 182 |
+
|
| 183 |
+
"""
|
| 184 |
+
if tol < 0:
|
| 185 |
+
raise ValueError("tol must be non-negative")
|
| 186 |
+
|
| 187 |
+
[c] = as_series([c])
|
| 188 |
+
[ind] = np.nonzero(np.abs(c) > tol)
|
| 189 |
+
if len(ind) == 0:
|
| 190 |
+
return c[:1] * 0
|
| 191 |
+
else:
|
| 192 |
+
return c[:ind[-1] + 1].copy()
|
| 193 |
+
|
| 194 |
+
def getdomain(x):
|
| 195 |
+
"""
|
| 196 |
+
Return a domain suitable for given abscissae.
|
| 197 |
+
|
| 198 |
+
Find a domain suitable for a polynomial or Chebyshev series
|
| 199 |
+
defined at the values supplied.
|
| 200 |
+
|
| 201 |
+
Parameters
|
| 202 |
+
----------
|
| 203 |
+
x : array_like
|
| 204 |
+
1-d array of abscissae whose domain will be determined.
|
| 205 |
+
|
| 206 |
+
Returns
|
| 207 |
+
-------
|
| 208 |
+
domain : ndarray
|
| 209 |
+
1-d array containing two values. If the inputs are complex, then
|
| 210 |
+
the two returned points are the lower left and upper right corners
|
| 211 |
+
of the smallest rectangle (aligned with the axes) in the complex
|
| 212 |
+
plane containing the points `x`. If the inputs are real, then the
|
| 213 |
+
two points are the ends of the smallest interval containing the
|
| 214 |
+
points `x`.
|
| 215 |
+
|
| 216 |
+
See Also
|
| 217 |
+
--------
|
| 218 |
+
mapparms, mapdomain
|
| 219 |
+
|
| 220 |
+
Examples
|
| 221 |
+
--------
|
| 222 |
+
>>> import numpy as np
|
| 223 |
+
>>> from numpy.polynomial import polyutils as pu
|
| 224 |
+
>>> points = np.arange(4)**2 - 5; points
|
| 225 |
+
array([-5, -4, -1, 4])
|
| 226 |
+
>>> pu.getdomain(points)
|
| 227 |
+
array([-5., 4.])
|
| 228 |
+
>>> c = np.exp(complex(0,1)*np.pi*np.arange(12)/6) # unit circle
|
| 229 |
+
>>> pu.getdomain(c)
|
| 230 |
+
array([-1.-1.j, 1.+1.j])
|
| 231 |
+
|
| 232 |
+
"""
|
| 233 |
+
[x] = as_series([x], trim=False)
|
| 234 |
+
if x.dtype.char in np.typecodes['Complex']:
|
| 235 |
+
rmin, rmax = x.real.min(), x.real.max()
|
| 236 |
+
imin, imax = x.imag.min(), x.imag.max()
|
| 237 |
+
return np.array((complex(rmin, imin), complex(rmax, imax)))
|
| 238 |
+
else:
|
| 239 |
+
return np.array((x.min(), x.max()))
|
| 240 |
+
|
| 241 |
+
def mapparms(old, new):
|
| 242 |
+
"""
|
| 243 |
+
Linear map parameters between domains.
|
| 244 |
+
|
| 245 |
+
Return the parameters of the linear map ``offset + scale*x`` that maps
|
| 246 |
+
`old` to `new` such that ``old[i] -> new[i]``, ``i = 0, 1``.
|
| 247 |
+
|
| 248 |
+
Parameters
|
| 249 |
+
----------
|
| 250 |
+
old, new : array_like
|
| 251 |
+
Domains. Each domain must (successfully) convert to a 1-d array
|
| 252 |
+
containing precisely two values.
|
| 253 |
+
|
| 254 |
+
Returns
|
| 255 |
+
-------
|
| 256 |
+
offset, scale : scalars
|
| 257 |
+
The map ``L(x) = offset + scale*x`` maps the first domain to the
|
| 258 |
+
second.
|
| 259 |
+
|
| 260 |
+
See Also
|
| 261 |
+
--------
|
| 262 |
+
getdomain, mapdomain
|
| 263 |
+
|
| 264 |
+
Notes
|
| 265 |
+
-----
|
| 266 |
+
Also works for complex numbers, and thus can be used to calculate the
|
| 267 |
+
parameters required to map any line in the complex plane to any other
|
| 268 |
+
line therein.
|
| 269 |
+
|
| 270 |
+
Examples
|
| 271 |
+
--------
|
| 272 |
+
>>> from numpy.polynomial import polyutils as pu
|
| 273 |
+
>>> pu.mapparms((-1,1),(-1,1))
|
| 274 |
+
(0.0, 1.0)
|
| 275 |
+
>>> pu.mapparms((1,-1),(-1,1))
|
| 276 |
+
(-0.0, -1.0)
|
| 277 |
+
>>> i = complex(0,1)
|
| 278 |
+
>>> pu.mapparms((-i,-1),(1,i))
|
| 279 |
+
((1+1j), (1-0j))
|
| 280 |
+
|
| 281 |
+
"""
|
| 282 |
+
oldlen = old[1] - old[0]
|
| 283 |
+
newlen = new[1] - new[0]
|
| 284 |
+
off = (old[1] * new[0] - old[0] * new[1]) / oldlen
|
| 285 |
+
scl = newlen / oldlen
|
| 286 |
+
return off, scl
|
| 287 |
+
|
| 288 |
+
def mapdomain(x, old, new):
|
| 289 |
+
"""
|
| 290 |
+
Apply linear map to input points.
|
| 291 |
+
|
| 292 |
+
The linear map ``offset + scale*x`` that maps the domain `old` to
|
| 293 |
+
the domain `new` is applied to the points `x`.
|
| 294 |
+
|
| 295 |
+
Parameters
|
| 296 |
+
----------
|
| 297 |
+
x : array_like
|
| 298 |
+
Points to be mapped. If `x` is a subtype of ndarray the subtype
|
| 299 |
+
will be preserved.
|
| 300 |
+
old, new : array_like
|
| 301 |
+
The two domains that determine the map. Each must (successfully)
|
| 302 |
+
convert to 1-d arrays containing precisely two values.
|
| 303 |
+
|
| 304 |
+
Returns
|
| 305 |
+
-------
|
| 306 |
+
x_out : ndarray
|
| 307 |
+
Array of points of the same shape as `x`, after application of the
|
| 308 |
+
linear map between the two domains.
|
| 309 |
+
|
| 310 |
+
See Also
|
| 311 |
+
--------
|
| 312 |
+
getdomain, mapparms
|
| 313 |
+
|
| 314 |
+
Notes
|
| 315 |
+
-----
|
| 316 |
+
Effectively, this implements:
|
| 317 |
+
|
| 318 |
+
.. math::
|
| 319 |
+
x\\_out = new[0] + m(x - old[0])
|
| 320 |
+
|
| 321 |
+
where
|
| 322 |
+
|
| 323 |
+
.. math::
|
| 324 |
+
m = \\frac{new[1]-new[0]}{old[1]-old[0]}
|
| 325 |
+
|
| 326 |
+
Examples
|
| 327 |
+
--------
|
| 328 |
+
>>> import numpy as np
|
| 329 |
+
>>> from numpy.polynomial import polyutils as pu
|
| 330 |
+
>>> old_domain = (-1,1)
|
| 331 |
+
>>> new_domain = (0,2*np.pi)
|
| 332 |
+
>>> x = np.linspace(-1,1,6); x
|
| 333 |
+
array([-1. , -0.6, -0.2, 0.2, 0.6, 1. ])
|
| 334 |
+
>>> x_out = pu.mapdomain(x, old_domain, new_domain); x_out
|
| 335 |
+
array([ 0. , 1.25663706, 2.51327412, 3.76991118, 5.02654825, # may vary
|
| 336 |
+
6.28318531])
|
| 337 |
+
>>> x - pu.mapdomain(x_out, new_domain, old_domain)
|
| 338 |
+
array([0., 0., 0., 0., 0., 0.])
|
| 339 |
+
|
| 340 |
+
Also works for complex numbers (and thus can be used to map any line in
|
| 341 |
+
the complex plane to any other line therein).
|
| 342 |
+
|
| 343 |
+
>>> i = complex(0,1)
|
| 344 |
+
>>> old = (-1 - i, 1 + i)
|
| 345 |
+
>>> new = (-1 + i, 1 - i)
|
| 346 |
+
>>> z = np.linspace(old[0], old[1], 6); z
|
| 347 |
+
array([-1. -1.j , -0.6-0.6j, -0.2-0.2j, 0.2+0.2j, 0.6+0.6j, 1. +1.j ])
|
| 348 |
+
>>> new_z = pu.mapdomain(z, old, new); new_z
|
| 349 |
+
array([-1.0+1.j , -0.6+0.6j, -0.2+0.2j, 0.2-0.2j, 0.6-0.6j, 1.0-1.j ]) # may vary
|
| 350 |
+
|
| 351 |
+
"""
|
| 352 |
+
if type(x) not in (int, float, complex) and not isinstance(x, np.generic):
|
| 353 |
+
x = np.asanyarray(x)
|
| 354 |
+
off, scl = mapparms(old, new)
|
| 355 |
+
return off + scl * x
|
| 356 |
+
|
| 357 |
+
|
| 358 |
+
def _nth_slice(i, ndim):
|
| 359 |
+
sl = [np.newaxis] * ndim
|
| 360 |
+
sl[i] = slice(None)
|
| 361 |
+
return tuple(sl)
|
| 362 |
+
|
| 363 |
+
|
| 364 |
+
def _vander_nd(vander_fs, points, degrees):
|
| 365 |
+
r"""
|
| 366 |
+
A generalization of the Vandermonde matrix for N dimensions
|
| 367 |
+
|
| 368 |
+
The result is built by combining the results of 1d Vandermonde matrices,
|
| 369 |
+
|
| 370 |
+
.. math::
|
| 371 |
+
W[i_0, \ldots, i_M, j_0, \ldots, j_N] = \prod_{k=0}^N{V_k(x_k)[i_0, \ldots, i_M, j_k]}
|
| 372 |
+
|
| 373 |
+
where
|
| 374 |
+
|
| 375 |
+
.. math::
|
| 376 |
+
N &= \texttt{len(points)} = \texttt{len(degrees)} = \texttt{len(vander\_fs)} \\
|
| 377 |
+
M &= \texttt{points[k].ndim} \\
|
| 378 |
+
V_k &= \texttt{vander\_fs[k]} \\
|
| 379 |
+
x_k &= \texttt{points[k]} \\
|
| 380 |
+
0 \le j_k &\le \texttt{degrees[k]}
|
| 381 |
+
|
| 382 |
+
Expanding the one-dimensional :math:`V_k` functions gives:
|
| 383 |
+
|
| 384 |
+
.. math::
|
| 385 |
+
W[i_0, \ldots, i_M, j_0, \ldots, j_N] = \prod_{k=0}^N{B_{k, j_k}(x_k[i_0, \ldots, i_M])}
|
| 386 |
+
|
| 387 |
+
where :math:`B_{k,m}` is the m'th basis of the polynomial construction used along
|
| 388 |
+
dimension :math:`k`. For a regular polynomial, :math:`B_{k, m}(x) = P_m(x) = x^m`.
|
| 389 |
+
|
| 390 |
+
Parameters
|
| 391 |
+
----------
|
| 392 |
+
vander_fs : Sequence[function(array_like, int) -> ndarray]
|
| 393 |
+
The 1d vander function to use for each axis, such as ``polyvander``
|
| 394 |
+
points : Sequence[array_like]
|
| 395 |
+
Arrays of point coordinates, all of the same shape. The dtypes
|
| 396 |
+
will be converted to either float64 or complex128 depending on
|
| 397 |
+
whether any of the elements are complex. Scalars are converted to
|
| 398 |
+
1-D arrays.
|
| 399 |
+
This must be the same length as `vander_fs`.
|
| 400 |
+
degrees : Sequence[int]
|
| 401 |
+
The maximum degree (inclusive) to use for each axis.
|
| 402 |
+
This must be the same length as `vander_fs`.
|
| 403 |
+
|
| 404 |
+
Returns
|
| 405 |
+
-------
|
| 406 |
+
vander_nd : ndarray
|
| 407 |
+
An array of shape ``points[0].shape + tuple(d + 1 for d in degrees)``.
|
| 408 |
+
""" # noqa: E501
|
| 409 |
+
n_dims = len(vander_fs)
|
| 410 |
+
if n_dims != len(points):
|
| 411 |
+
raise ValueError(
|
| 412 |
+
f"Expected {n_dims} dimensions of sample points, got {len(points)}")
|
| 413 |
+
if n_dims != len(degrees):
|
| 414 |
+
raise ValueError(
|
| 415 |
+
f"Expected {n_dims} dimensions of degrees, got {len(degrees)}")
|
| 416 |
+
if n_dims == 0:
|
| 417 |
+
raise ValueError("Unable to guess a dtype or shape when no points are given")
|
| 418 |
+
|
| 419 |
+
# convert to the same shape and type
|
| 420 |
+
points = tuple(np.asarray(tuple(points)) + 0.0)
|
| 421 |
+
|
| 422 |
+
# produce the vandermonde matrix for each dimension, placing the last
|
| 423 |
+
# axis of each in an independent trailing axis of the output
|
| 424 |
+
vander_arrays = (
|
| 425 |
+
vander_fs[i](points[i], degrees[i])[(...,) + _nth_slice(i, n_dims)]
|
| 426 |
+
for i in range(n_dims)
|
| 427 |
+
)
|
| 428 |
+
|
| 429 |
+
# we checked this wasn't empty already, so no `initial` needed
|
| 430 |
+
return functools.reduce(operator.mul, vander_arrays)
|
| 431 |
+
|
| 432 |
+
|
| 433 |
+
def _vander_nd_flat(vander_fs, points, degrees):
|
| 434 |
+
"""
|
| 435 |
+
Like `_vander_nd`, but flattens the last ``len(degrees)`` axes into a single axis
|
| 436 |
+
|
| 437 |
+
Used to implement the public ``<type>vander<n>d`` functions.
|
| 438 |
+
"""
|
| 439 |
+
v = _vander_nd(vander_fs, points, degrees)
|
| 440 |
+
return v.reshape(v.shape[:-len(degrees)] + (-1,))
|
| 441 |
+
|
| 442 |
+
|
| 443 |
+
def _fromroots(line_f, mul_f, roots):
|
| 444 |
+
"""
|
| 445 |
+
Helper function used to implement the ``<type>fromroots`` functions.
|
| 446 |
+
|
| 447 |
+
Parameters
|
| 448 |
+
----------
|
| 449 |
+
line_f : function(float, float) -> ndarray
|
| 450 |
+
The ``<type>line`` function, such as ``polyline``
|
| 451 |
+
mul_f : function(array_like, array_like) -> ndarray
|
| 452 |
+
The ``<type>mul`` function, such as ``polymul``
|
| 453 |
+
roots
|
| 454 |
+
See the ``<type>fromroots`` functions for more detail
|
| 455 |
+
"""
|
| 456 |
+
if len(roots) == 0:
|
| 457 |
+
return np.ones(1)
|
| 458 |
+
else:
|
| 459 |
+
[roots] = as_series([roots], trim=False)
|
| 460 |
+
roots.sort()
|
| 461 |
+
p = [line_f(-r, 1) for r in roots]
|
| 462 |
+
n = len(p)
|
| 463 |
+
while n > 1:
|
| 464 |
+
m, r = divmod(n, 2)
|
| 465 |
+
tmp = [mul_f(p[i], p[i + m]) for i in range(m)]
|
| 466 |
+
if r:
|
| 467 |
+
tmp[0] = mul_f(tmp[0], p[-1])
|
| 468 |
+
p = tmp
|
| 469 |
+
n = m
|
| 470 |
+
return p[0]
|
| 471 |
+
|
| 472 |
+
|
| 473 |
+
def _valnd(val_f, c, *args):
|
| 474 |
+
"""
|
| 475 |
+
Helper function used to implement the ``<type>val<n>d`` functions.
|
| 476 |
+
|
| 477 |
+
Parameters
|
| 478 |
+
----------
|
| 479 |
+
val_f : function(array_like, array_like, tensor: bool) -> array_like
|
| 480 |
+
The ``<type>val`` function, such as ``polyval``
|
| 481 |
+
c, args
|
| 482 |
+
See the ``<type>val<n>d`` functions for more detail
|
| 483 |
+
"""
|
| 484 |
+
args = [np.asanyarray(a) for a in args]
|
| 485 |
+
shape0 = args[0].shape
|
| 486 |
+
if not all(a.shape == shape0 for a in args[1:]):
|
| 487 |
+
if len(args) == 3:
|
| 488 |
+
raise ValueError('x, y, z are incompatible')
|
| 489 |
+
elif len(args) == 2:
|
| 490 |
+
raise ValueError('x, y are incompatible')
|
| 491 |
+
else:
|
| 492 |
+
raise ValueError('ordinates are incompatible')
|
| 493 |
+
it = iter(args)
|
| 494 |
+
x0 = next(it)
|
| 495 |
+
|
| 496 |
+
# use tensor on only the first
|
| 497 |
+
c = val_f(x0, c)
|
| 498 |
+
for xi in it:
|
| 499 |
+
c = val_f(xi, c, tensor=False)
|
| 500 |
+
return c
|
| 501 |
+
|
| 502 |
+
|
| 503 |
+
def _gridnd(val_f, c, *args):
|
| 504 |
+
"""
|
| 505 |
+
Helper function used to implement the ``<type>grid<n>d`` functions.
|
| 506 |
+
|
| 507 |
+
Parameters
|
| 508 |
+
----------
|
| 509 |
+
val_f : function(array_like, array_like, tensor: bool) -> array_like
|
| 510 |
+
The ``<type>val`` function, such as ``polyval``
|
| 511 |
+
c, args
|
| 512 |
+
See the ``<type>grid<n>d`` functions for more detail
|
| 513 |
+
"""
|
| 514 |
+
for xi in args:
|
| 515 |
+
c = val_f(xi, c)
|
| 516 |
+
return c
|
| 517 |
+
|
| 518 |
+
|
| 519 |
+
def _div(mul_f, c1, c2):
|
| 520 |
+
"""
|
| 521 |
+
Helper function used to implement the ``<type>div`` functions.
|
| 522 |
+
|
| 523 |
+
Implementation uses repeated subtraction of c2 multiplied by the nth basis.
|
| 524 |
+
For some polynomial types, a more efficient approach may be possible.
|
| 525 |
+
|
| 526 |
+
Parameters
|
| 527 |
+
----------
|
| 528 |
+
mul_f : function(array_like, array_like) -> array_like
|
| 529 |
+
The ``<type>mul`` function, such as ``polymul``
|
| 530 |
+
c1, c2
|
| 531 |
+
See the ``<type>div`` functions for more detail
|
| 532 |
+
"""
|
| 533 |
+
# c1, c2 are trimmed copies
|
| 534 |
+
[c1, c2] = as_series([c1, c2])
|
| 535 |
+
if c2[-1] == 0:
|
| 536 |
+
raise ZeroDivisionError # FIXME: add message with details to exception
|
| 537 |
+
|
| 538 |
+
lc1 = len(c1)
|
| 539 |
+
lc2 = len(c2)
|
| 540 |
+
if lc1 < lc2:
|
| 541 |
+
return c1[:1] * 0, c1
|
| 542 |
+
elif lc2 == 1:
|
| 543 |
+
return c1 / c2[-1], c1[:1] * 0
|
| 544 |
+
else:
|
| 545 |
+
quo = np.empty(lc1 - lc2 + 1, dtype=c1.dtype)
|
| 546 |
+
rem = c1
|
| 547 |
+
for i in range(lc1 - lc2, - 1, -1):
|
| 548 |
+
p = mul_f([0] * i + [1], c2)
|
| 549 |
+
q = rem[-1] / p[-1]
|
| 550 |
+
rem = rem[:-1] - q * p[:-1]
|
| 551 |
+
quo[i] = q
|
| 552 |
+
return quo, trimseq(rem)
|
| 553 |
+
|
| 554 |
+
|
| 555 |
+
def _add(c1, c2):
|
| 556 |
+
""" Helper function used to implement the ``<type>add`` functions. """
|
| 557 |
+
# c1, c2 are trimmed copies
|
| 558 |
+
[c1, c2] = as_series([c1, c2])
|
| 559 |
+
if len(c1) > len(c2):
|
| 560 |
+
c1[:c2.size] += c2
|
| 561 |
+
ret = c1
|
| 562 |
+
else:
|
| 563 |
+
c2[:c1.size] += c1
|
| 564 |
+
ret = c2
|
| 565 |
+
return trimseq(ret)
|
| 566 |
+
|
| 567 |
+
|
| 568 |
+
def _sub(c1, c2):
|
| 569 |
+
""" Helper function used to implement the ``<type>sub`` functions. """
|
| 570 |
+
# c1, c2 are trimmed copies
|
| 571 |
+
[c1, c2] = as_series([c1, c2])
|
| 572 |
+
if len(c1) > len(c2):
|
| 573 |
+
c1[:c2.size] -= c2
|
| 574 |
+
ret = c1
|
| 575 |
+
else:
|
| 576 |
+
c2 = -c2
|
| 577 |
+
c2[:c1.size] += c1
|
| 578 |
+
ret = c2
|
| 579 |
+
return trimseq(ret)
|
| 580 |
+
|
| 581 |
+
|
| 582 |
+
def _fit(vander_f, x, y, deg, rcond=None, full=False, w=None):
|
| 583 |
+
"""
|
| 584 |
+
Helper function used to implement the ``<type>fit`` functions.
|
| 585 |
+
|
| 586 |
+
Parameters
|
| 587 |
+
----------
|
| 588 |
+
vander_f : function(array_like, int) -> ndarray
|
| 589 |
+
The 1d vander function, such as ``polyvander``
|
| 590 |
+
c1, c2
|
| 591 |
+
See the ``<type>fit`` functions for more detail
|
| 592 |
+
"""
|
| 593 |
+
x = np.asarray(x) + 0.0
|
| 594 |
+
y = np.asarray(y) + 0.0
|
| 595 |
+
deg = np.asarray(deg)
|
| 596 |
+
|
| 597 |
+
# check arguments.
|
| 598 |
+
if deg.ndim > 1 or deg.dtype.kind not in 'iu' or deg.size == 0:
|
| 599 |
+
raise TypeError("deg must be an int or non-empty 1-D array of int")
|
| 600 |
+
if deg.min() < 0:
|
| 601 |
+
raise ValueError("expected deg >= 0")
|
| 602 |
+
if x.ndim != 1:
|
| 603 |
+
raise TypeError("expected 1D vector for x")
|
| 604 |
+
if x.size == 0:
|
| 605 |
+
raise TypeError("expected non-empty vector for x")
|
| 606 |
+
if y.ndim < 1 or y.ndim > 2:
|
| 607 |
+
raise TypeError("expected 1D or 2D array for y")
|
| 608 |
+
if len(x) != len(y):
|
| 609 |
+
raise TypeError("expected x and y to have same length")
|
| 610 |
+
|
| 611 |
+
if deg.ndim == 0:
|
| 612 |
+
lmax = deg
|
| 613 |
+
order = lmax + 1
|
| 614 |
+
van = vander_f(x, lmax)
|
| 615 |
+
else:
|
| 616 |
+
deg = np.sort(deg)
|
| 617 |
+
lmax = deg[-1]
|
| 618 |
+
order = len(deg)
|
| 619 |
+
van = vander_f(x, lmax)[:, deg]
|
| 620 |
+
|
| 621 |
+
# set up the least squares matrices in transposed form
|
| 622 |
+
lhs = van.T
|
| 623 |
+
rhs = y.T
|
| 624 |
+
if w is not None:
|
| 625 |
+
w = np.asarray(w) + 0.0
|
| 626 |
+
if w.ndim != 1:
|
| 627 |
+
raise TypeError("expected 1D vector for w")
|
| 628 |
+
if len(x) != len(w):
|
| 629 |
+
raise TypeError("expected x and w to have same length")
|
| 630 |
+
# apply weights. Don't use inplace operations as they
|
| 631 |
+
# can cause problems with NA.
|
| 632 |
+
lhs = lhs * w
|
| 633 |
+
rhs = rhs * w
|
| 634 |
+
|
| 635 |
+
# set rcond
|
| 636 |
+
if rcond is None:
|
| 637 |
+
rcond = len(x) * np.finfo(x.dtype).eps
|
| 638 |
+
|
| 639 |
+
# Determine the norms of the design matrix columns.
|
| 640 |
+
if issubclass(lhs.dtype.type, np.complexfloating):
|
| 641 |
+
scl = np.sqrt((np.square(lhs.real) + np.square(lhs.imag)).sum(1))
|
| 642 |
+
else:
|
| 643 |
+
scl = np.sqrt(np.square(lhs).sum(1))
|
| 644 |
+
scl[scl == 0] = 1
|
| 645 |
+
|
| 646 |
+
# Solve the least squares problem.
|
| 647 |
+
c, resids, rank, s = np.linalg.lstsq(lhs.T / scl, rhs.T, rcond)
|
| 648 |
+
c = (c.T / scl).T
|
| 649 |
+
|
| 650 |
+
# Expand c to include non-fitted coefficients which are set to zero
|
| 651 |
+
if deg.ndim > 0:
|
| 652 |
+
if c.ndim == 2:
|
| 653 |
+
cc = np.zeros((lmax + 1, c.shape[1]), dtype=c.dtype)
|
| 654 |
+
else:
|
| 655 |
+
cc = np.zeros(lmax + 1, dtype=c.dtype)
|
| 656 |
+
cc[deg] = c
|
| 657 |
+
c = cc
|
| 658 |
+
|
| 659 |
+
# warn on rank reduction
|
| 660 |
+
if rank != order and not full:
|
| 661 |
+
msg = "The fit may be poorly conditioned"
|
| 662 |
+
warnings.warn(msg, np.exceptions.RankWarning, stacklevel=2)
|
| 663 |
+
|
| 664 |
+
if full:
|
| 665 |
+
return c, [resids, rank, s, rcond]
|
| 666 |
+
else:
|
| 667 |
+
return c
|
| 668 |
+
|
| 669 |
+
|
| 670 |
+
def _pow(mul_f, c, pow, maxpower):
|
| 671 |
+
"""
|
| 672 |
+
Helper function used to implement the ``<type>pow`` functions.
|
| 673 |
+
|
| 674 |
+
Parameters
|
| 675 |
+
----------
|
| 676 |
+
mul_f : function(array_like, array_like) -> ndarray
|
| 677 |
+
The ``<type>mul`` function, such as ``polymul``
|
| 678 |
+
c : array_like
|
| 679 |
+
1-D array of array of series coefficients
|
| 680 |
+
pow, maxpower
|
| 681 |
+
See the ``<type>pow`` functions for more detail
|
| 682 |
+
"""
|
| 683 |
+
# c is a trimmed copy
|
| 684 |
+
[c] = as_series([c])
|
| 685 |
+
power = int(pow)
|
| 686 |
+
if power != pow or power < 0:
|
| 687 |
+
raise ValueError("Power must be a non-negative integer.")
|
| 688 |
+
elif maxpower is not None and power > maxpower:
|
| 689 |
+
raise ValueError("Power is too large")
|
| 690 |
+
elif power == 0:
|
| 691 |
+
return np.array([1], dtype=c.dtype)
|
| 692 |
+
elif power == 1:
|
| 693 |
+
return c
|
| 694 |
+
else:
|
| 695 |
+
# This can be made more efficient by using powers of two
|
| 696 |
+
# in the usual way.
|
| 697 |
+
prd = c
|
| 698 |
+
for i in range(2, power + 1):
|
| 699 |
+
prd = mul_f(prd, c)
|
| 700 |
+
return prd
|
| 701 |
+
|
| 702 |
+
|
| 703 |
+
def _as_int(x, desc):
|
| 704 |
+
"""
|
| 705 |
+
Like `operator.index`, but emits a custom exception when passed an
|
| 706 |
+
incorrect type
|
| 707 |
+
|
| 708 |
+
Parameters
|
| 709 |
+
----------
|
| 710 |
+
x : int-like
|
| 711 |
+
Value to interpret as an integer
|
| 712 |
+
desc : str
|
| 713 |
+
description to include in any error message
|
| 714 |
+
|
| 715 |
+
Raises
|
| 716 |
+
------
|
| 717 |
+
TypeError : if x is a float or non-numeric
|
| 718 |
+
"""
|
| 719 |
+
try:
|
| 720 |
+
return operator.index(x)
|
| 721 |
+
except TypeError as e:
|
| 722 |
+
raise TypeError(f"{desc} must be an integer, received {x}") from e
|
| 723 |
+
|
| 724 |
+
|
| 725 |
+
def format_float(x, parens=False):
|
| 726 |
+
from numpy._core.multiarray import dragon4_positional, dragon4_scientific
|
| 727 |
+
|
| 728 |
+
if not np.issubdtype(type(x), np.floating):
|
| 729 |
+
return str(x)
|
| 730 |
+
|
| 731 |
+
opts = np.get_printoptions()
|
| 732 |
+
|
| 733 |
+
if np.isnan(x):
|
| 734 |
+
return opts['nanstr']
|
| 735 |
+
elif np.isinf(x):
|
| 736 |
+
return opts['infstr']
|
| 737 |
+
|
| 738 |
+
exp_format = False
|
| 739 |
+
if x != 0:
|
| 740 |
+
a = np.abs(x)
|
| 741 |
+
if a >= 1.e8 or a < 10**min(0, -(opts['precision'] - 1) // 2):
|
| 742 |
+
exp_format = True
|
| 743 |
+
|
| 744 |
+
trim, unique = '0', True
|
| 745 |
+
if opts['floatmode'] == 'fixed':
|
| 746 |
+
trim, unique = 'k', False
|
| 747 |
+
|
| 748 |
+
if exp_format:
|
| 749 |
+
s = dragon4_scientific(x, precision=opts['precision'],
|
| 750 |
+
unique=unique, trim=trim,
|
| 751 |
+
sign=opts['sign'] == '+')
|
| 752 |
+
if parens:
|
| 753 |
+
s = '(' + s + ')'
|
| 754 |
+
else:
|
| 755 |
+
s = dragon4_positional(x, precision=opts['precision'],
|
| 756 |
+
fractional=True,
|
| 757 |
+
unique=unique, trim=trim,
|
| 758 |
+
sign=opts['sign'] == '+')
|
| 759 |
+
return s
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/polyutils.pyi
ADDED
|
@@ -0,0 +1,307 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from collections.abc import Callable, Iterable, Sequence
|
| 2 |
+
from typing import (
|
| 3 |
+
Final,
|
| 4 |
+
Literal,
|
| 5 |
+
Protocol,
|
| 6 |
+
SupportsIndex,
|
| 7 |
+
TypeAlias,
|
| 8 |
+
TypeVar,
|
| 9 |
+
overload,
|
| 10 |
+
type_check_only,
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
import numpy as np
|
| 14 |
+
import numpy.typing as npt
|
| 15 |
+
from numpy._typing import (
|
| 16 |
+
_ArrayLikeComplex_co,
|
| 17 |
+
_ArrayLikeFloat_co,
|
| 18 |
+
_ArrayLikeObject_co,
|
| 19 |
+
_FloatLike_co,
|
| 20 |
+
_NumberLike_co,
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
from ._polytypes import (
|
| 24 |
+
_AnyInt,
|
| 25 |
+
_Array2,
|
| 26 |
+
_ArrayLikeCoef_co,
|
| 27 |
+
_CoefArray,
|
| 28 |
+
_CoefLike_co,
|
| 29 |
+
_CoefSeries,
|
| 30 |
+
_ComplexArray,
|
| 31 |
+
_ComplexSeries,
|
| 32 |
+
_FloatArray,
|
| 33 |
+
_FloatSeries,
|
| 34 |
+
_FuncBinOp,
|
| 35 |
+
_ObjectArray,
|
| 36 |
+
_ObjectSeries,
|
| 37 |
+
_SeriesLikeCoef_co,
|
| 38 |
+
_SeriesLikeComplex_co,
|
| 39 |
+
_SeriesLikeFloat_co,
|
| 40 |
+
_SeriesLikeInt_co,
|
| 41 |
+
_SeriesLikeObject_co,
|
| 42 |
+
_Tuple2,
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
__all__ = ["as_series", "format_float", "getdomain", "mapdomain", "mapparms", "trimcoef", "trimseq"]
|
| 46 |
+
|
| 47 |
+
_T = TypeVar("_T")
|
| 48 |
+
_SeqT = TypeVar("_SeqT", bound=_CoefArray | Sequence[_CoefLike_co])
|
| 49 |
+
|
| 50 |
+
_AnyLineF: TypeAlias = Callable[[float, float], _CoefArray]
|
| 51 |
+
_AnyMulF: TypeAlias = Callable[[np.ndarray | list[int], np.ndarray], _CoefArray]
|
| 52 |
+
_AnyVanderF: TypeAlias = Callable[[np.ndarray, int], _CoefArray]
|
| 53 |
+
|
| 54 |
+
@type_check_only
|
| 55 |
+
class _ValFunc(Protocol[_T]):
|
| 56 |
+
def __call__(self, x: np.ndarray, c: _T, /, *, tensor: bool = True) -> _T: ...
|
| 57 |
+
|
| 58 |
+
###
|
| 59 |
+
|
| 60 |
+
@overload
|
| 61 |
+
def as_series(alist: npt.NDArray[np.integer] | _FloatArray, trim: bool = True) -> list[_FloatSeries]: ...
|
| 62 |
+
@overload
|
| 63 |
+
def as_series(alist: _ComplexArray, trim: bool = True) -> list[_ComplexSeries]: ...
|
| 64 |
+
@overload
|
| 65 |
+
def as_series(alist: _ObjectArray, trim: bool = True) -> list[_ObjectSeries]: ...
|
| 66 |
+
@overload
|
| 67 |
+
def as_series(alist: Iterable[_FloatArray | npt.NDArray[np.integer]], trim: bool = True) -> list[_FloatSeries]: ...
|
| 68 |
+
@overload
|
| 69 |
+
def as_series(alist: Iterable[_ComplexArray], trim: bool = True) -> list[_ComplexSeries]: ...
|
| 70 |
+
@overload
|
| 71 |
+
def as_series(alist: Iterable[_ObjectArray], trim: bool = True) -> list[_ObjectSeries]: ...
|
| 72 |
+
@overload
|
| 73 |
+
def as_series(alist: Iterable[_SeriesLikeFloat_co | float], trim: bool = True) -> list[_FloatSeries]: ...
|
| 74 |
+
@overload
|
| 75 |
+
def as_series(alist: Iterable[_SeriesLikeComplex_co | complex], trim: bool = True) -> list[_ComplexSeries]: ...
|
| 76 |
+
@overload
|
| 77 |
+
def as_series(alist: Iterable[_SeriesLikeCoef_co | object], trim: bool = True) -> list[_ObjectSeries]: ...
|
| 78 |
+
|
| 79 |
+
#
|
| 80 |
+
def trimseq(seq: _SeqT) -> _SeqT: ...
|
| 81 |
+
|
| 82 |
+
#
|
| 83 |
+
@overload
|
| 84 |
+
def trimcoef(c: npt.NDArray[np.integer] | _FloatArray, tol: _FloatLike_co = 0) -> _FloatSeries: ...
|
| 85 |
+
@overload
|
| 86 |
+
def trimcoef(c: _ComplexArray, tol: _FloatLike_co = 0) -> _ComplexSeries: ...
|
| 87 |
+
@overload
|
| 88 |
+
def trimcoef(c: _ObjectArray, tol: _FloatLike_co = 0) -> _ObjectSeries: ...
|
| 89 |
+
@overload
|
| 90 |
+
def trimcoef(c: _SeriesLikeFloat_co | float, tol: _FloatLike_co = 0) -> _FloatSeries: ...
|
| 91 |
+
@overload
|
| 92 |
+
def trimcoef(c: _SeriesLikeComplex_co | complex, tol: _FloatLike_co = 0) -> _ComplexSeries: ...
|
| 93 |
+
@overload
|
| 94 |
+
def trimcoef(c: _SeriesLikeCoef_co | object, tol: _FloatLike_co = 0) -> _ObjectSeries: ...
|
| 95 |
+
|
| 96 |
+
#
|
| 97 |
+
@overload
|
| 98 |
+
def getdomain(x: _FloatArray | npt.NDArray[np.integer]) -> _Array2[np.float64]: ...
|
| 99 |
+
@overload
|
| 100 |
+
def getdomain(x: _ComplexArray) -> _Array2[np.complex128]: ...
|
| 101 |
+
@overload
|
| 102 |
+
def getdomain(x: _ObjectArray) -> _Array2[np.object_]: ...
|
| 103 |
+
@overload
|
| 104 |
+
def getdomain(x: _SeriesLikeFloat_co | float) -> _Array2[np.float64]: ...
|
| 105 |
+
@overload
|
| 106 |
+
def getdomain(x: _SeriesLikeComplex_co | complex) -> _Array2[np.complex128]: ...
|
| 107 |
+
@overload
|
| 108 |
+
def getdomain(x: _SeriesLikeCoef_co | object) -> _Array2[np.object_]: ...
|
| 109 |
+
|
| 110 |
+
#
|
| 111 |
+
@overload
|
| 112 |
+
def mapparms(old: npt.NDArray[np.floating | np.integer], new: npt.NDArray[np.floating | np.integer]) -> _Tuple2[np.floating]: ...
|
| 113 |
+
@overload
|
| 114 |
+
def mapparms(old: npt.NDArray[np.number], new: npt.NDArray[np.number]) -> _Tuple2[np.complexfloating]: ...
|
| 115 |
+
@overload
|
| 116 |
+
def mapparms(old: npt.NDArray[np.object_ | np.number], new: npt.NDArray[np.object_ | np.number]) -> _Tuple2[object]: ...
|
| 117 |
+
@overload
|
| 118 |
+
def mapparms(old: Sequence[float], new: Sequence[float]) -> _Tuple2[float]: ...
|
| 119 |
+
@overload
|
| 120 |
+
def mapparms(old: Sequence[complex], new: Sequence[complex]) -> _Tuple2[complex]: ...
|
| 121 |
+
@overload
|
| 122 |
+
def mapparms(old: _SeriesLikeFloat_co, new: _SeriesLikeFloat_co) -> _Tuple2[np.floating]: ...
|
| 123 |
+
@overload
|
| 124 |
+
def mapparms(old: _SeriesLikeComplex_co, new: _SeriesLikeComplex_co) -> _Tuple2[np.complexfloating]: ...
|
| 125 |
+
@overload
|
| 126 |
+
def mapparms(old: _SeriesLikeCoef_co, new: _SeriesLikeCoef_co) -> _Tuple2[object]: ...
|
| 127 |
+
|
| 128 |
+
#
|
| 129 |
+
@overload
|
| 130 |
+
def mapdomain(x: _FloatLike_co, old: _SeriesLikeFloat_co, new: _SeriesLikeFloat_co) -> np.floating: ...
|
| 131 |
+
@overload
|
| 132 |
+
def mapdomain(x: _NumberLike_co, old: _SeriesLikeComplex_co, new: _SeriesLikeComplex_co) -> np.complexfloating: ...
|
| 133 |
+
@overload
|
| 134 |
+
def mapdomain(
|
| 135 |
+
x: npt.NDArray[np.floating | np.integer],
|
| 136 |
+
old: npt.NDArray[np.floating | np.integer],
|
| 137 |
+
new: npt.NDArray[np.floating | np.integer],
|
| 138 |
+
) -> _FloatSeries: ...
|
| 139 |
+
@overload
|
| 140 |
+
def mapdomain(x: npt.NDArray[np.number], old: npt.NDArray[np.number], new: npt.NDArray[np.number]) -> _ComplexSeries: ...
|
| 141 |
+
@overload
|
| 142 |
+
def mapdomain(
|
| 143 |
+
x: npt.NDArray[np.object_ | np.number],
|
| 144 |
+
old: npt.NDArray[np.object_ | np.number],
|
| 145 |
+
new: npt.NDArray[np.object_ | np.number],
|
| 146 |
+
) -> _ObjectSeries: ...
|
| 147 |
+
@overload
|
| 148 |
+
def mapdomain(x: _SeriesLikeFloat_co, old: _SeriesLikeFloat_co, new: _SeriesLikeFloat_co) -> _FloatSeries: ...
|
| 149 |
+
@overload
|
| 150 |
+
def mapdomain(x: _SeriesLikeComplex_co, old: _SeriesLikeComplex_co, new: _SeriesLikeComplex_co) -> _ComplexSeries: ...
|
| 151 |
+
@overload
|
| 152 |
+
def mapdomain(x: _SeriesLikeCoef_co, old: _SeriesLikeCoef_co, new: _SeriesLikeCoef_co) -> _ObjectSeries: ...
|
| 153 |
+
@overload
|
| 154 |
+
def mapdomain(x: _CoefLike_co, old: _SeriesLikeCoef_co, new: _SeriesLikeCoef_co) -> object: ...
|
| 155 |
+
|
| 156 |
+
#
|
| 157 |
+
def _nth_slice(i: SupportsIndex, ndim: SupportsIndex) -> tuple[slice | None, ...]: ...
|
| 158 |
+
|
| 159 |
+
# keep in sync with `vander_nd_flat`
|
| 160 |
+
@overload
|
| 161 |
+
def _vander_nd(
|
| 162 |
+
vander_fs: Sequence[_AnyVanderF],
|
| 163 |
+
points: Sequence[_ArrayLikeFloat_co],
|
| 164 |
+
degrees: Sequence[SupportsIndex],
|
| 165 |
+
) -> _FloatArray: ...
|
| 166 |
+
@overload
|
| 167 |
+
def _vander_nd(
|
| 168 |
+
vander_fs: Sequence[_AnyVanderF],
|
| 169 |
+
points: Sequence[_ArrayLikeComplex_co],
|
| 170 |
+
degrees: Sequence[SupportsIndex],
|
| 171 |
+
) -> _ComplexArray: ...
|
| 172 |
+
@overload
|
| 173 |
+
def _vander_nd(
|
| 174 |
+
vander_fs: Sequence[_AnyVanderF],
|
| 175 |
+
points: Sequence[_ArrayLikeObject_co | _ArrayLikeComplex_co],
|
| 176 |
+
degrees: Sequence[SupportsIndex],
|
| 177 |
+
) -> _ObjectArray: ...
|
| 178 |
+
@overload
|
| 179 |
+
def _vander_nd(
|
| 180 |
+
vander_fs: Sequence[_AnyVanderF],
|
| 181 |
+
points: Sequence[npt.ArrayLike],
|
| 182 |
+
degrees: Sequence[SupportsIndex],
|
| 183 |
+
) -> _CoefArray: ...
|
| 184 |
+
|
| 185 |
+
# keep in sync with `vander_nd`
|
| 186 |
+
@overload
|
| 187 |
+
def _vander_nd_flat(
|
| 188 |
+
vander_fs: Sequence[_AnyVanderF],
|
| 189 |
+
points: Sequence[_ArrayLikeFloat_co],
|
| 190 |
+
degrees: Sequence[SupportsIndex],
|
| 191 |
+
) -> _FloatArray: ...
|
| 192 |
+
@overload
|
| 193 |
+
def _vander_nd_flat(
|
| 194 |
+
vander_fs: Sequence[_AnyVanderF],
|
| 195 |
+
points: Sequence[_ArrayLikeComplex_co],
|
| 196 |
+
degrees: Sequence[SupportsIndex],
|
| 197 |
+
) -> _ComplexArray: ...
|
| 198 |
+
@overload
|
| 199 |
+
def _vander_nd_flat(
|
| 200 |
+
vander_fs: Sequence[_AnyVanderF],
|
| 201 |
+
points: Sequence[_ArrayLikeObject_co | _ArrayLikeComplex_co],
|
| 202 |
+
degrees: Sequence[SupportsIndex],
|
| 203 |
+
) -> _ObjectArray: ...
|
| 204 |
+
@overload
|
| 205 |
+
def _vander_nd_flat(
|
| 206 |
+
vander_fs: Sequence[_AnyVanderF],
|
| 207 |
+
points: Sequence[npt.ArrayLike],
|
| 208 |
+
degrees: Sequence[SupportsIndex],
|
| 209 |
+
) -> _CoefArray: ...
|
| 210 |
+
|
| 211 |
+
# keep in sync with `._polytypes._FuncFromRoots`
|
| 212 |
+
@overload
|
| 213 |
+
def _fromroots(line_f: _AnyLineF, mul_f: _AnyMulF, roots: _SeriesLikeFloat_co) -> _FloatSeries: ...
|
| 214 |
+
@overload
|
| 215 |
+
def _fromroots(line_f: _AnyLineF, mul_f: _AnyMulF, roots: _SeriesLikeComplex_co) -> _ComplexSeries: ...
|
| 216 |
+
@overload
|
| 217 |
+
def _fromroots(line_f: _AnyLineF, mul_f: _AnyMulF, roots: _SeriesLikeObject_co) -> _ObjectSeries: ...
|
| 218 |
+
@overload
|
| 219 |
+
def _fromroots(line_f: _AnyLineF, mul_f: _AnyMulF, roots: _SeriesLikeCoef_co) -> _CoefSeries: ...
|
| 220 |
+
|
| 221 |
+
# keep in sync with `_gridnd`
|
| 222 |
+
def _valnd(val_f: _ValFunc[_T], c: _T, *args: npt.ArrayLike) -> _T: ...
|
| 223 |
+
|
| 224 |
+
# keep in sync with `_valnd`
|
| 225 |
+
def _gridnd(val_f: _ValFunc[_T], c: _T, *args: npt.ArrayLike) -> _T: ...
|
| 226 |
+
|
| 227 |
+
# keep in sync with `_polytypes._FuncBinOp`
|
| 228 |
+
@overload
|
| 229 |
+
def _div(mul_f: _AnyMulF, c1: _SeriesLikeFloat_co, c2: _SeriesLikeFloat_co) -> _Tuple2[_FloatSeries]: ...
|
| 230 |
+
@overload
|
| 231 |
+
def _div(mul_f: _AnyMulF, c1: _SeriesLikeComplex_co, c2: _SeriesLikeComplex_co) -> _Tuple2[_ComplexSeries]: ...
|
| 232 |
+
@overload
|
| 233 |
+
def _div(mul_f: _AnyMulF, c1: _SeriesLikeObject_co, c2: _SeriesLikeObject_co) -> _Tuple2[_ObjectSeries]: ...
|
| 234 |
+
@overload
|
| 235 |
+
def _div(mul_f: _AnyMulF, c1: _SeriesLikeCoef_co, c2: _SeriesLikeCoef_co) -> _Tuple2[_CoefSeries]: ...
|
| 236 |
+
|
| 237 |
+
_add: Final[_FuncBinOp] = ...
|
| 238 |
+
_sub: Final[_FuncBinOp] = ...
|
| 239 |
+
|
| 240 |
+
# keep in sync with `_polytypes._FuncPow`
|
| 241 |
+
@overload
|
| 242 |
+
def _pow(mul_f: _AnyMulF, c: _SeriesLikeFloat_co, pow: _AnyInt, maxpower: _AnyInt | None) -> _FloatSeries: ...
|
| 243 |
+
@overload
|
| 244 |
+
def _pow(mul_f: _AnyMulF, c: _SeriesLikeComplex_co, pow: _AnyInt, maxpower: _AnyInt | None) -> _ComplexSeries: ...
|
| 245 |
+
@overload
|
| 246 |
+
def _pow(mul_f: _AnyMulF, c: _SeriesLikeObject_co, pow: _AnyInt, maxpower: _AnyInt | None) -> _ObjectSeries: ...
|
| 247 |
+
@overload
|
| 248 |
+
def _pow(mul_f: _AnyMulF, c: _SeriesLikeCoef_co, pow: _AnyInt, maxpower: _AnyInt | None) -> _CoefSeries: ...
|
| 249 |
+
|
| 250 |
+
# keep in sync with `_polytypes._FuncFit`
|
| 251 |
+
@overload
|
| 252 |
+
def _fit(
|
| 253 |
+
vander_f: _AnyVanderF,
|
| 254 |
+
x: _SeriesLikeFloat_co,
|
| 255 |
+
y: _ArrayLikeFloat_co,
|
| 256 |
+
deg: _SeriesLikeInt_co,
|
| 257 |
+
rcond: _FloatLike_co | None = None,
|
| 258 |
+
full: Literal[False] = False,
|
| 259 |
+
w: _SeriesLikeFloat_co | None = None,
|
| 260 |
+
) -> _FloatArray: ...
|
| 261 |
+
@overload
|
| 262 |
+
def _fit(
|
| 263 |
+
vander_f: _AnyVanderF,
|
| 264 |
+
x: _SeriesLikeComplex_co,
|
| 265 |
+
y: _ArrayLikeComplex_co,
|
| 266 |
+
deg: _SeriesLikeInt_co,
|
| 267 |
+
rcond: _FloatLike_co | None = None,
|
| 268 |
+
full: Literal[False] = False,
|
| 269 |
+
w: _SeriesLikeComplex_co | None = None,
|
| 270 |
+
) -> _ComplexArray: ...
|
| 271 |
+
@overload
|
| 272 |
+
def _fit(
|
| 273 |
+
vander_f: _AnyVanderF,
|
| 274 |
+
x: _SeriesLikeCoef_co,
|
| 275 |
+
y: _ArrayLikeCoef_co,
|
| 276 |
+
deg: _SeriesLikeInt_co,
|
| 277 |
+
rcond: _FloatLike_co | None = None,
|
| 278 |
+
full: Literal[False] = False,
|
| 279 |
+
w: _SeriesLikeCoef_co | None = None,
|
| 280 |
+
) -> _CoefArray: ...
|
| 281 |
+
@overload
|
| 282 |
+
def _fit(
|
| 283 |
+
vander_f: _AnyVanderF,
|
| 284 |
+
x: _SeriesLikeCoef_co,
|
| 285 |
+
y: _SeriesLikeCoef_co,
|
| 286 |
+
deg: _SeriesLikeInt_co,
|
| 287 |
+
rcond: _FloatLike_co | None,
|
| 288 |
+
full: Literal[True],
|
| 289 |
+
w: _SeriesLikeCoef_co | None = None,
|
| 290 |
+
) -> tuple[_CoefSeries, Sequence[np.inexact | np.int32]]: ...
|
| 291 |
+
@overload
|
| 292 |
+
def _fit(
|
| 293 |
+
vander_f: _AnyVanderF,
|
| 294 |
+
x: _SeriesLikeCoef_co,
|
| 295 |
+
y: _SeriesLikeCoef_co,
|
| 296 |
+
deg: _SeriesLikeInt_co,
|
| 297 |
+
rcond: _FloatLike_co | None = None,
|
| 298 |
+
*,
|
| 299 |
+
full: Literal[True],
|
| 300 |
+
w: _SeriesLikeCoef_co | None = None,
|
| 301 |
+
) -> tuple[_CoefSeries, Sequence[np.inexact | np.int32]]: ...
|
| 302 |
+
|
| 303 |
+
#
|
| 304 |
+
def _as_int(x: SupportsIndex, desc: str) -> int: ...
|
| 305 |
+
|
| 306 |
+
#
|
| 307 |
+
def format_float(x: _FloatLike_co, parens: bool = False) -> str: ...
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/tests/__init__.py
ADDED
|
File without changes
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/tests/test_chebyshev.py
ADDED
|
@@ -0,0 +1,618 @@
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for chebyshev module.
|
| 2 |
+
|
| 3 |
+
"""
|
| 4 |
+
from functools import reduce
|
| 5 |
+
|
| 6 |
+
import numpy as np
|
| 7 |
+
import numpy.polynomial.chebyshev as cheb
|
| 8 |
+
from numpy.polynomial.polynomial import polyval
|
| 9 |
+
from numpy.testing import assert_, assert_almost_equal, assert_equal, assert_raises
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def trim(x):
|
| 13 |
+
return cheb.chebtrim(x, tol=1e-6)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
T0 = [1]
|
| 17 |
+
T1 = [0, 1]
|
| 18 |
+
T2 = [-1, 0, 2]
|
| 19 |
+
T3 = [0, -3, 0, 4]
|
| 20 |
+
T4 = [1, 0, -8, 0, 8]
|
| 21 |
+
T5 = [0, 5, 0, -20, 0, 16]
|
| 22 |
+
T6 = [-1, 0, 18, 0, -48, 0, 32]
|
| 23 |
+
T7 = [0, -7, 0, 56, 0, -112, 0, 64]
|
| 24 |
+
T8 = [1, 0, -32, 0, 160, 0, -256, 0, 128]
|
| 25 |
+
T9 = [0, 9, 0, -120, 0, 432, 0, -576, 0, 256]
|
| 26 |
+
|
| 27 |
+
Tlist = [T0, T1, T2, T3, T4, T5, T6, T7, T8, T9]
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
class TestPrivate:
|
| 31 |
+
|
| 32 |
+
def test__cseries_to_zseries(self):
|
| 33 |
+
for i in range(5):
|
| 34 |
+
inp = np.array([2] + [1] * i, np.double)
|
| 35 |
+
tgt = np.array([.5] * i + [2] + [.5] * i, np.double)
|
| 36 |
+
res = cheb._cseries_to_zseries(inp)
|
| 37 |
+
assert_equal(res, tgt)
|
| 38 |
+
|
| 39 |
+
def test__zseries_to_cseries(self):
|
| 40 |
+
for i in range(5):
|
| 41 |
+
inp = np.array([.5] * i + [2] + [.5] * i, np.double)
|
| 42 |
+
tgt = np.array([2] + [1] * i, np.double)
|
| 43 |
+
res = cheb._zseries_to_cseries(inp)
|
| 44 |
+
assert_equal(res, tgt)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
class TestConstants:
|
| 48 |
+
|
| 49 |
+
def test_chebdomain(self):
|
| 50 |
+
assert_equal(cheb.chebdomain, [-1, 1])
|
| 51 |
+
|
| 52 |
+
def test_chebzero(self):
|
| 53 |
+
assert_equal(cheb.chebzero, [0])
|
| 54 |
+
|
| 55 |
+
def test_chebone(self):
|
| 56 |
+
assert_equal(cheb.chebone, [1])
|
| 57 |
+
|
| 58 |
+
def test_chebx(self):
|
| 59 |
+
assert_equal(cheb.chebx, [0, 1])
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
class TestArithmetic:
|
| 63 |
+
|
| 64 |
+
def test_chebadd(self):
|
| 65 |
+
for i in range(5):
|
| 66 |
+
for j in range(5):
|
| 67 |
+
msg = f"At i={i}, j={j}"
|
| 68 |
+
tgt = np.zeros(max(i, j) + 1)
|
| 69 |
+
tgt[i] += 1
|
| 70 |
+
tgt[j] += 1
|
| 71 |
+
res = cheb.chebadd([0] * i + [1], [0] * j + [1])
|
| 72 |
+
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
| 73 |
+
|
| 74 |
+
def test_chebsub(self):
|
| 75 |
+
for i in range(5):
|
| 76 |
+
for j in range(5):
|
| 77 |
+
msg = f"At i={i}, j={j}"
|
| 78 |
+
tgt = np.zeros(max(i, j) + 1)
|
| 79 |
+
tgt[i] += 1
|
| 80 |
+
tgt[j] -= 1
|
| 81 |
+
res = cheb.chebsub([0] * i + [1], [0] * j + [1])
|
| 82 |
+
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
| 83 |
+
|
| 84 |
+
def test_chebmulx(self):
|
| 85 |
+
assert_equal(cheb.chebmulx([0]), [0])
|
| 86 |
+
assert_equal(cheb.chebmulx([1]), [0, 1])
|
| 87 |
+
for i in range(1, 5):
|
| 88 |
+
ser = [0] * i + [1]
|
| 89 |
+
tgt = [0] * (i - 1) + [.5, 0, .5]
|
| 90 |
+
assert_equal(cheb.chebmulx(ser), tgt)
|
| 91 |
+
|
| 92 |
+
def test_chebmul(self):
|
| 93 |
+
for i in range(5):
|
| 94 |
+
for j in range(5):
|
| 95 |
+
msg = f"At i={i}, j={j}"
|
| 96 |
+
tgt = np.zeros(i + j + 1)
|
| 97 |
+
tgt[i + j] += .5
|
| 98 |
+
tgt[abs(i - j)] += .5
|
| 99 |
+
res = cheb.chebmul([0] * i + [1], [0] * j + [1])
|
| 100 |
+
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
| 101 |
+
|
| 102 |
+
def test_chebdiv(self):
|
| 103 |
+
for i in range(5):
|
| 104 |
+
for j in range(5):
|
| 105 |
+
msg = f"At i={i}, j={j}"
|
| 106 |
+
ci = [0] * i + [1]
|
| 107 |
+
cj = [0] * j + [1]
|
| 108 |
+
tgt = cheb.chebadd(ci, cj)
|
| 109 |
+
quo, rem = cheb.chebdiv(tgt, ci)
|
| 110 |
+
res = cheb.chebadd(cheb.chebmul(quo, ci), rem)
|
| 111 |
+
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
| 112 |
+
|
| 113 |
+
def test_chebpow(self):
|
| 114 |
+
for i in range(5):
|
| 115 |
+
for j in range(5):
|
| 116 |
+
msg = f"At i={i}, j={j}"
|
| 117 |
+
c = np.arange(i + 1)
|
| 118 |
+
tgt = reduce(cheb.chebmul, [c] * j, np.array([1]))
|
| 119 |
+
res = cheb.chebpow(c, j)
|
| 120 |
+
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
class TestEvaluation:
|
| 124 |
+
# coefficients of 1 + 2*x + 3*x**2
|
| 125 |
+
c1d = np.array([2.5, 2., 1.5])
|
| 126 |
+
c2d = np.einsum('i,j->ij', c1d, c1d)
|
| 127 |
+
c3d = np.einsum('i,j,k->ijk', c1d, c1d, c1d)
|
| 128 |
+
|
| 129 |
+
# some random values in [-1, 1)
|
| 130 |
+
x = np.random.random((3, 5)) * 2 - 1
|
| 131 |
+
y = polyval(x, [1., 2., 3.])
|
| 132 |
+
|
| 133 |
+
def test_chebval(self):
|
| 134 |
+
# check empty input
|
| 135 |
+
assert_equal(cheb.chebval([], [1]).size, 0)
|
| 136 |
+
|
| 137 |
+
# check normal input)
|
| 138 |
+
x = np.linspace(-1, 1)
|
| 139 |
+
y = [polyval(x, c) for c in Tlist]
|
| 140 |
+
for i in range(10):
|
| 141 |
+
msg = f"At i={i}"
|
| 142 |
+
tgt = y[i]
|
| 143 |
+
res = cheb.chebval(x, [0] * i + [1])
|
| 144 |
+
assert_almost_equal(res, tgt, err_msg=msg)
|
| 145 |
+
|
| 146 |
+
# check that shape is preserved
|
| 147 |
+
for i in range(3):
|
| 148 |
+
dims = [2] * i
|
| 149 |
+
x = np.zeros(dims)
|
| 150 |
+
assert_equal(cheb.chebval(x, [1]).shape, dims)
|
| 151 |
+
assert_equal(cheb.chebval(x, [1, 0]).shape, dims)
|
| 152 |
+
assert_equal(cheb.chebval(x, [1, 0, 0]).shape, dims)
|
| 153 |
+
|
| 154 |
+
def test_chebval2d(self):
|
| 155 |
+
x1, x2, x3 = self.x
|
| 156 |
+
y1, y2, y3 = self.y
|
| 157 |
+
|
| 158 |
+
# test exceptions
|
| 159 |
+
assert_raises(ValueError, cheb.chebval2d, x1, x2[:2], self.c2d)
|
| 160 |
+
|
| 161 |
+
# test values
|
| 162 |
+
tgt = y1 * y2
|
| 163 |
+
res = cheb.chebval2d(x1, x2, self.c2d)
|
| 164 |
+
assert_almost_equal(res, tgt)
|
| 165 |
+
|
| 166 |
+
# test shape
|
| 167 |
+
z = np.ones((2, 3))
|
| 168 |
+
res = cheb.chebval2d(z, z, self.c2d)
|
| 169 |
+
assert_(res.shape == (2, 3))
|
| 170 |
+
|
| 171 |
+
def test_chebval3d(self):
|
| 172 |
+
x1, x2, x3 = self.x
|
| 173 |
+
y1, y2, y3 = self.y
|
| 174 |
+
|
| 175 |
+
# test exceptions
|
| 176 |
+
assert_raises(ValueError, cheb.chebval3d, x1, x2, x3[:2], self.c3d)
|
| 177 |
+
|
| 178 |
+
# test values
|
| 179 |
+
tgt = y1 * y2 * y3
|
| 180 |
+
res = cheb.chebval3d(x1, x2, x3, self.c3d)
|
| 181 |
+
assert_almost_equal(res, tgt)
|
| 182 |
+
|
| 183 |
+
# test shape
|
| 184 |
+
z = np.ones((2, 3))
|
| 185 |
+
res = cheb.chebval3d(z, z, z, self.c3d)
|
| 186 |
+
assert_(res.shape == (2, 3))
|
| 187 |
+
|
| 188 |
+
def test_chebgrid2d(self):
|
| 189 |
+
x1, x2, x3 = self.x
|
| 190 |
+
y1, y2, y3 = self.y
|
| 191 |
+
|
| 192 |
+
# test values
|
| 193 |
+
tgt = np.einsum('i,j->ij', y1, y2)
|
| 194 |
+
res = cheb.chebgrid2d(x1, x2, self.c2d)
|
| 195 |
+
assert_almost_equal(res, tgt)
|
| 196 |
+
|
| 197 |
+
# test shape
|
| 198 |
+
z = np.ones((2, 3))
|
| 199 |
+
res = cheb.chebgrid2d(z, z, self.c2d)
|
| 200 |
+
assert_(res.shape == (2, 3) * 2)
|
| 201 |
+
|
| 202 |
+
def test_chebgrid3d(self):
|
| 203 |
+
x1, x2, x3 = self.x
|
| 204 |
+
y1, y2, y3 = self.y
|
| 205 |
+
|
| 206 |
+
# test values
|
| 207 |
+
tgt = np.einsum('i,j,k->ijk', y1, y2, y3)
|
| 208 |
+
res = cheb.chebgrid3d(x1, x2, x3, self.c3d)
|
| 209 |
+
assert_almost_equal(res, tgt)
|
| 210 |
+
|
| 211 |
+
# test shape
|
| 212 |
+
z = np.ones((2, 3))
|
| 213 |
+
res = cheb.chebgrid3d(z, z, z, self.c3d)
|
| 214 |
+
assert_(res.shape == (2, 3) * 3)
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
class TestIntegral:
|
| 218 |
+
|
| 219 |
+
def test_chebint(self):
|
| 220 |
+
# check exceptions
|
| 221 |
+
assert_raises(TypeError, cheb.chebint, [0], .5)
|
| 222 |
+
assert_raises(ValueError, cheb.chebint, [0], -1)
|
| 223 |
+
assert_raises(ValueError, cheb.chebint, [0], 1, [0, 0])
|
| 224 |
+
assert_raises(ValueError, cheb.chebint, [0], lbnd=[0])
|
| 225 |
+
assert_raises(ValueError, cheb.chebint, [0], scl=[0])
|
| 226 |
+
assert_raises(TypeError, cheb.chebint, [0], axis=.5)
|
| 227 |
+
|
| 228 |
+
# test integration of zero polynomial
|
| 229 |
+
for i in range(2, 5):
|
| 230 |
+
k = [0] * (i - 2) + [1]
|
| 231 |
+
res = cheb.chebint([0], m=i, k=k)
|
| 232 |
+
assert_almost_equal(res, [0, 1])
|
| 233 |
+
|
| 234 |
+
# check single integration with integration constant
|
| 235 |
+
for i in range(5):
|
| 236 |
+
scl = i + 1
|
| 237 |
+
pol = [0] * i + [1]
|
| 238 |
+
tgt = [i] + [0] * i + [1 / scl]
|
| 239 |
+
chebpol = cheb.poly2cheb(pol)
|
| 240 |
+
chebint = cheb.chebint(chebpol, m=1, k=[i])
|
| 241 |
+
res = cheb.cheb2poly(chebint)
|
| 242 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 243 |
+
|
| 244 |
+
# check single integration with integration constant and lbnd
|
| 245 |
+
for i in range(5):
|
| 246 |
+
scl = i + 1
|
| 247 |
+
pol = [0] * i + [1]
|
| 248 |
+
chebpol = cheb.poly2cheb(pol)
|
| 249 |
+
chebint = cheb.chebint(chebpol, m=1, k=[i], lbnd=-1)
|
| 250 |
+
assert_almost_equal(cheb.chebval(-1, chebint), i)
|
| 251 |
+
|
| 252 |
+
# check single integration with integration constant and scaling
|
| 253 |
+
for i in range(5):
|
| 254 |
+
scl = i + 1
|
| 255 |
+
pol = [0] * i + [1]
|
| 256 |
+
tgt = [i] + [0] * i + [2 / scl]
|
| 257 |
+
chebpol = cheb.poly2cheb(pol)
|
| 258 |
+
chebint = cheb.chebint(chebpol, m=1, k=[i], scl=2)
|
| 259 |
+
res = cheb.cheb2poly(chebint)
|
| 260 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 261 |
+
|
| 262 |
+
# check multiple integrations with default k
|
| 263 |
+
for i in range(5):
|
| 264 |
+
for j in range(2, 5):
|
| 265 |
+
pol = [0] * i + [1]
|
| 266 |
+
tgt = pol[:]
|
| 267 |
+
for k in range(j):
|
| 268 |
+
tgt = cheb.chebint(tgt, m=1)
|
| 269 |
+
res = cheb.chebint(pol, m=j)
|
| 270 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 271 |
+
|
| 272 |
+
# check multiple integrations with defined k
|
| 273 |
+
for i in range(5):
|
| 274 |
+
for j in range(2, 5):
|
| 275 |
+
pol = [0] * i + [1]
|
| 276 |
+
tgt = pol[:]
|
| 277 |
+
for k in range(j):
|
| 278 |
+
tgt = cheb.chebint(tgt, m=1, k=[k])
|
| 279 |
+
res = cheb.chebint(pol, m=j, k=list(range(j)))
|
| 280 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 281 |
+
|
| 282 |
+
# check multiple integrations with lbnd
|
| 283 |
+
for i in range(5):
|
| 284 |
+
for j in range(2, 5):
|
| 285 |
+
pol = [0] * i + [1]
|
| 286 |
+
tgt = pol[:]
|
| 287 |
+
for k in range(j):
|
| 288 |
+
tgt = cheb.chebint(tgt, m=1, k=[k], lbnd=-1)
|
| 289 |
+
res = cheb.chebint(pol, m=j, k=list(range(j)), lbnd=-1)
|
| 290 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 291 |
+
|
| 292 |
+
# check multiple integrations with scaling
|
| 293 |
+
for i in range(5):
|
| 294 |
+
for j in range(2, 5):
|
| 295 |
+
pol = [0] * i + [1]
|
| 296 |
+
tgt = pol[:]
|
| 297 |
+
for k in range(j):
|
| 298 |
+
tgt = cheb.chebint(tgt, m=1, k=[k], scl=2)
|
| 299 |
+
res = cheb.chebint(pol, m=j, k=list(range(j)), scl=2)
|
| 300 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 301 |
+
|
| 302 |
+
def test_chebint_axis(self):
|
| 303 |
+
# check that axis keyword works
|
| 304 |
+
c2d = np.random.random((3, 4))
|
| 305 |
+
|
| 306 |
+
tgt = np.vstack([cheb.chebint(c) for c in c2d.T]).T
|
| 307 |
+
res = cheb.chebint(c2d, axis=0)
|
| 308 |
+
assert_almost_equal(res, tgt)
|
| 309 |
+
|
| 310 |
+
tgt = np.vstack([cheb.chebint(c) for c in c2d])
|
| 311 |
+
res = cheb.chebint(c2d, axis=1)
|
| 312 |
+
assert_almost_equal(res, tgt)
|
| 313 |
+
|
| 314 |
+
tgt = np.vstack([cheb.chebint(c, k=3) for c in c2d])
|
| 315 |
+
res = cheb.chebint(c2d, k=3, axis=1)
|
| 316 |
+
assert_almost_equal(res, tgt)
|
| 317 |
+
|
| 318 |
+
|
| 319 |
+
class TestDerivative:
|
| 320 |
+
|
| 321 |
+
def test_chebder(self):
|
| 322 |
+
# check exceptions
|
| 323 |
+
assert_raises(TypeError, cheb.chebder, [0], .5)
|
| 324 |
+
assert_raises(ValueError, cheb.chebder, [0], -1)
|
| 325 |
+
|
| 326 |
+
# check that zeroth derivative does nothing
|
| 327 |
+
for i in range(5):
|
| 328 |
+
tgt = [0] * i + [1]
|
| 329 |
+
res = cheb.chebder(tgt, m=0)
|
| 330 |
+
assert_equal(trim(res), trim(tgt))
|
| 331 |
+
|
| 332 |
+
# check that derivation is the inverse of integration
|
| 333 |
+
for i in range(5):
|
| 334 |
+
for j in range(2, 5):
|
| 335 |
+
tgt = [0] * i + [1]
|
| 336 |
+
res = cheb.chebder(cheb.chebint(tgt, m=j), m=j)
|
| 337 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 338 |
+
|
| 339 |
+
# check derivation with scaling
|
| 340 |
+
for i in range(5):
|
| 341 |
+
for j in range(2, 5):
|
| 342 |
+
tgt = [0] * i + [1]
|
| 343 |
+
res = cheb.chebder(cheb.chebint(tgt, m=j, scl=2), m=j, scl=.5)
|
| 344 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 345 |
+
|
| 346 |
+
def test_chebder_axis(self):
|
| 347 |
+
# check that axis keyword works
|
| 348 |
+
c2d = np.random.random((3, 4))
|
| 349 |
+
|
| 350 |
+
tgt = np.vstack([cheb.chebder(c) for c in c2d.T]).T
|
| 351 |
+
res = cheb.chebder(c2d, axis=0)
|
| 352 |
+
assert_almost_equal(res, tgt)
|
| 353 |
+
|
| 354 |
+
tgt = np.vstack([cheb.chebder(c) for c in c2d])
|
| 355 |
+
res = cheb.chebder(c2d, axis=1)
|
| 356 |
+
assert_almost_equal(res, tgt)
|
| 357 |
+
|
| 358 |
+
|
| 359 |
+
class TestVander:
|
| 360 |
+
# some random values in [-1, 1)
|
| 361 |
+
x = np.random.random((3, 5)) * 2 - 1
|
| 362 |
+
|
| 363 |
+
def test_chebvander(self):
|
| 364 |
+
# check for 1d x
|
| 365 |
+
x = np.arange(3)
|
| 366 |
+
v = cheb.chebvander(x, 3)
|
| 367 |
+
assert_(v.shape == (3, 4))
|
| 368 |
+
for i in range(4):
|
| 369 |
+
coef = [0] * i + [1]
|
| 370 |
+
assert_almost_equal(v[..., i], cheb.chebval(x, coef))
|
| 371 |
+
|
| 372 |
+
# check for 2d x
|
| 373 |
+
x = np.array([[1, 2], [3, 4], [5, 6]])
|
| 374 |
+
v = cheb.chebvander(x, 3)
|
| 375 |
+
assert_(v.shape == (3, 2, 4))
|
| 376 |
+
for i in range(4):
|
| 377 |
+
coef = [0] * i + [1]
|
| 378 |
+
assert_almost_equal(v[..., i], cheb.chebval(x, coef))
|
| 379 |
+
|
| 380 |
+
def test_chebvander2d(self):
|
| 381 |
+
# also tests chebval2d for non-square coefficient array
|
| 382 |
+
x1, x2, x3 = self.x
|
| 383 |
+
c = np.random.random((2, 3))
|
| 384 |
+
van = cheb.chebvander2d(x1, x2, [1, 2])
|
| 385 |
+
tgt = cheb.chebval2d(x1, x2, c)
|
| 386 |
+
res = np.dot(van, c.flat)
|
| 387 |
+
assert_almost_equal(res, tgt)
|
| 388 |
+
|
| 389 |
+
# check shape
|
| 390 |
+
van = cheb.chebvander2d([x1], [x2], [1, 2])
|
| 391 |
+
assert_(van.shape == (1, 5, 6))
|
| 392 |
+
|
| 393 |
+
def test_chebvander3d(self):
|
| 394 |
+
# also tests chebval3d for non-square coefficient array
|
| 395 |
+
x1, x2, x3 = self.x
|
| 396 |
+
c = np.random.random((2, 3, 4))
|
| 397 |
+
van = cheb.chebvander3d(x1, x2, x3, [1, 2, 3])
|
| 398 |
+
tgt = cheb.chebval3d(x1, x2, x3, c)
|
| 399 |
+
res = np.dot(van, c.flat)
|
| 400 |
+
assert_almost_equal(res, tgt)
|
| 401 |
+
|
| 402 |
+
# check shape
|
| 403 |
+
van = cheb.chebvander3d([x1], [x2], [x3], [1, 2, 3])
|
| 404 |
+
assert_(van.shape == (1, 5, 24))
|
| 405 |
+
|
| 406 |
+
|
| 407 |
+
class TestFitting:
|
| 408 |
+
|
| 409 |
+
def test_chebfit(self):
|
| 410 |
+
def f(x):
|
| 411 |
+
return x * (x - 1) * (x - 2)
|
| 412 |
+
|
| 413 |
+
def f2(x):
|
| 414 |
+
return x**4 + x**2 + 1
|
| 415 |
+
|
| 416 |
+
# Test exceptions
|
| 417 |
+
assert_raises(ValueError, cheb.chebfit, [1], [1], -1)
|
| 418 |
+
assert_raises(TypeError, cheb.chebfit, [[1]], [1], 0)
|
| 419 |
+
assert_raises(TypeError, cheb.chebfit, [], [1], 0)
|
| 420 |
+
assert_raises(TypeError, cheb.chebfit, [1], [[[1]]], 0)
|
| 421 |
+
assert_raises(TypeError, cheb.chebfit, [1, 2], [1], 0)
|
| 422 |
+
assert_raises(TypeError, cheb.chebfit, [1], [1, 2], 0)
|
| 423 |
+
assert_raises(TypeError, cheb.chebfit, [1], [1], 0, w=[[1]])
|
| 424 |
+
assert_raises(TypeError, cheb.chebfit, [1], [1], 0, w=[1, 1])
|
| 425 |
+
assert_raises(ValueError, cheb.chebfit, [1], [1], [-1,])
|
| 426 |
+
assert_raises(ValueError, cheb.chebfit, [1], [1], [2, -1, 6])
|
| 427 |
+
assert_raises(TypeError, cheb.chebfit, [1], [1], [])
|
| 428 |
+
|
| 429 |
+
# Test fit
|
| 430 |
+
x = np.linspace(0, 2)
|
| 431 |
+
y = f(x)
|
| 432 |
+
#
|
| 433 |
+
coef3 = cheb.chebfit(x, y, 3)
|
| 434 |
+
assert_equal(len(coef3), 4)
|
| 435 |
+
assert_almost_equal(cheb.chebval(x, coef3), y)
|
| 436 |
+
coef3 = cheb.chebfit(x, y, [0, 1, 2, 3])
|
| 437 |
+
assert_equal(len(coef3), 4)
|
| 438 |
+
assert_almost_equal(cheb.chebval(x, coef3), y)
|
| 439 |
+
#
|
| 440 |
+
coef4 = cheb.chebfit(x, y, 4)
|
| 441 |
+
assert_equal(len(coef4), 5)
|
| 442 |
+
assert_almost_equal(cheb.chebval(x, coef4), y)
|
| 443 |
+
coef4 = cheb.chebfit(x, y, [0, 1, 2, 3, 4])
|
| 444 |
+
assert_equal(len(coef4), 5)
|
| 445 |
+
assert_almost_equal(cheb.chebval(x, coef4), y)
|
| 446 |
+
# check things still work if deg is not in strict increasing
|
| 447 |
+
coef4 = cheb.chebfit(x, y, [2, 3, 4, 1, 0])
|
| 448 |
+
assert_equal(len(coef4), 5)
|
| 449 |
+
assert_almost_equal(cheb.chebval(x, coef4), y)
|
| 450 |
+
#
|
| 451 |
+
coef2d = cheb.chebfit(x, np.array([y, y]).T, 3)
|
| 452 |
+
assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
|
| 453 |
+
coef2d = cheb.chebfit(x, np.array([y, y]).T, [0, 1, 2, 3])
|
| 454 |
+
assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
|
| 455 |
+
# test weighting
|
| 456 |
+
w = np.zeros_like(x)
|
| 457 |
+
yw = y.copy()
|
| 458 |
+
w[1::2] = 1
|
| 459 |
+
y[0::2] = 0
|
| 460 |
+
wcoef3 = cheb.chebfit(x, yw, 3, w=w)
|
| 461 |
+
assert_almost_equal(wcoef3, coef3)
|
| 462 |
+
wcoef3 = cheb.chebfit(x, yw, [0, 1, 2, 3], w=w)
|
| 463 |
+
assert_almost_equal(wcoef3, coef3)
|
| 464 |
+
#
|
| 465 |
+
wcoef2d = cheb.chebfit(x, np.array([yw, yw]).T, 3, w=w)
|
| 466 |
+
assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
|
| 467 |
+
wcoef2d = cheb.chebfit(x, np.array([yw, yw]).T, [0, 1, 2, 3], w=w)
|
| 468 |
+
assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
|
| 469 |
+
# test scaling with complex values x points whose square
|
| 470 |
+
# is zero when summed.
|
| 471 |
+
x = [1, 1j, -1, -1j]
|
| 472 |
+
assert_almost_equal(cheb.chebfit(x, x, 1), [0, 1])
|
| 473 |
+
assert_almost_equal(cheb.chebfit(x, x, [0, 1]), [0, 1])
|
| 474 |
+
# test fitting only even polynomials
|
| 475 |
+
x = np.linspace(-1, 1)
|
| 476 |
+
y = f2(x)
|
| 477 |
+
coef1 = cheb.chebfit(x, y, 4)
|
| 478 |
+
assert_almost_equal(cheb.chebval(x, coef1), y)
|
| 479 |
+
coef2 = cheb.chebfit(x, y, [0, 2, 4])
|
| 480 |
+
assert_almost_equal(cheb.chebval(x, coef2), y)
|
| 481 |
+
assert_almost_equal(coef1, coef2)
|
| 482 |
+
|
| 483 |
+
|
| 484 |
+
class TestInterpolate:
|
| 485 |
+
|
| 486 |
+
def f(self, x):
|
| 487 |
+
return x * (x - 1) * (x - 2)
|
| 488 |
+
|
| 489 |
+
def test_raises(self):
|
| 490 |
+
assert_raises(ValueError, cheb.chebinterpolate, self.f, -1)
|
| 491 |
+
assert_raises(TypeError, cheb.chebinterpolate, self.f, 10.)
|
| 492 |
+
|
| 493 |
+
def test_dimensions(self):
|
| 494 |
+
for deg in range(1, 5):
|
| 495 |
+
assert_(cheb.chebinterpolate(self.f, deg).shape == (deg + 1,))
|
| 496 |
+
|
| 497 |
+
def test_approximation(self):
|
| 498 |
+
|
| 499 |
+
def powx(x, p):
|
| 500 |
+
return x**p
|
| 501 |
+
|
| 502 |
+
x = np.linspace(-1, 1, 10)
|
| 503 |
+
for deg in range(10):
|
| 504 |
+
for p in range(deg + 1):
|
| 505 |
+
c = cheb.chebinterpolate(powx, deg, (p,))
|
| 506 |
+
assert_almost_equal(cheb.chebval(x, c), powx(x, p), decimal=12)
|
| 507 |
+
|
| 508 |
+
|
| 509 |
+
class TestCompanion:
|
| 510 |
+
|
| 511 |
+
def test_raises(self):
|
| 512 |
+
assert_raises(ValueError, cheb.chebcompanion, [])
|
| 513 |
+
assert_raises(ValueError, cheb.chebcompanion, [1])
|
| 514 |
+
|
| 515 |
+
def test_dimensions(self):
|
| 516 |
+
for i in range(1, 5):
|
| 517 |
+
coef = [0] * i + [1]
|
| 518 |
+
assert_(cheb.chebcompanion(coef).shape == (i, i))
|
| 519 |
+
|
| 520 |
+
def test_linear_root(self):
|
| 521 |
+
assert_(cheb.chebcompanion([1, 2])[0, 0] == -.5)
|
| 522 |
+
|
| 523 |
+
|
| 524 |
+
class TestGauss:
|
| 525 |
+
|
| 526 |
+
def test_100(self):
|
| 527 |
+
x, w = cheb.chebgauss(100)
|
| 528 |
+
|
| 529 |
+
# test orthogonality. Note that the results need to be normalized,
|
| 530 |
+
# otherwise the huge values that can arise from fast growing
|
| 531 |
+
# functions like Laguerre can be very confusing.
|
| 532 |
+
v = cheb.chebvander(x, 99)
|
| 533 |
+
vv = np.dot(v.T * w, v)
|
| 534 |
+
vd = 1 / np.sqrt(vv.diagonal())
|
| 535 |
+
vv = vd[:, None] * vv * vd
|
| 536 |
+
assert_almost_equal(vv, np.eye(100))
|
| 537 |
+
|
| 538 |
+
# check that the integral of 1 is correct
|
| 539 |
+
tgt = np.pi
|
| 540 |
+
assert_almost_equal(w.sum(), tgt)
|
| 541 |
+
|
| 542 |
+
|
| 543 |
+
class TestMisc:
|
| 544 |
+
|
| 545 |
+
def test_chebfromroots(self):
|
| 546 |
+
res = cheb.chebfromroots([])
|
| 547 |
+
assert_almost_equal(trim(res), [1])
|
| 548 |
+
for i in range(1, 5):
|
| 549 |
+
roots = np.cos(np.linspace(-np.pi, 0, 2 * i + 1)[1::2])
|
| 550 |
+
tgt = [0] * i + [1]
|
| 551 |
+
res = cheb.chebfromroots(roots) * 2**(i - 1)
|
| 552 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 553 |
+
|
| 554 |
+
def test_chebroots(self):
|
| 555 |
+
assert_almost_equal(cheb.chebroots([1]), [])
|
| 556 |
+
assert_almost_equal(cheb.chebroots([1, 2]), [-.5])
|
| 557 |
+
for i in range(2, 5):
|
| 558 |
+
tgt = np.linspace(-1, 1, i)
|
| 559 |
+
res = cheb.chebroots(cheb.chebfromroots(tgt))
|
| 560 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 561 |
+
|
| 562 |
+
def test_chebtrim(self):
|
| 563 |
+
coef = [2, -1, 1, 0]
|
| 564 |
+
|
| 565 |
+
# Test exceptions
|
| 566 |
+
assert_raises(ValueError, cheb.chebtrim, coef, -1)
|
| 567 |
+
|
| 568 |
+
# Test results
|
| 569 |
+
assert_equal(cheb.chebtrim(coef), coef[:-1])
|
| 570 |
+
assert_equal(cheb.chebtrim(coef, 1), coef[:-3])
|
| 571 |
+
assert_equal(cheb.chebtrim(coef, 2), [0])
|
| 572 |
+
|
| 573 |
+
def test_chebline(self):
|
| 574 |
+
assert_equal(cheb.chebline(3, 4), [3, 4])
|
| 575 |
+
|
| 576 |
+
def test_cheb2poly(self):
|
| 577 |
+
for i in range(10):
|
| 578 |
+
assert_almost_equal(cheb.cheb2poly([0] * i + [1]), Tlist[i])
|
| 579 |
+
|
| 580 |
+
def test_poly2cheb(self):
|
| 581 |
+
for i in range(10):
|
| 582 |
+
assert_almost_equal(cheb.poly2cheb(Tlist[i]), [0] * i + [1])
|
| 583 |
+
|
| 584 |
+
def test_weight(self):
|
| 585 |
+
x = np.linspace(-1, 1, 11)[1:-1]
|
| 586 |
+
tgt = 1. / (np.sqrt(1 + x) * np.sqrt(1 - x))
|
| 587 |
+
res = cheb.chebweight(x)
|
| 588 |
+
assert_almost_equal(res, tgt)
|
| 589 |
+
|
| 590 |
+
def test_chebpts1(self):
|
| 591 |
+
# test exceptions
|
| 592 |
+
assert_raises(ValueError, cheb.chebpts1, 1.5)
|
| 593 |
+
assert_raises(ValueError, cheb.chebpts1, 0)
|
| 594 |
+
|
| 595 |
+
# test points
|
| 596 |
+
tgt = [0]
|
| 597 |
+
assert_almost_equal(cheb.chebpts1(1), tgt)
|
| 598 |
+
tgt = [-0.70710678118654746, 0.70710678118654746]
|
| 599 |
+
assert_almost_equal(cheb.chebpts1(2), tgt)
|
| 600 |
+
tgt = [-0.86602540378443871, 0, 0.86602540378443871]
|
| 601 |
+
assert_almost_equal(cheb.chebpts1(3), tgt)
|
| 602 |
+
tgt = [-0.9238795325, -0.3826834323, 0.3826834323, 0.9238795325]
|
| 603 |
+
assert_almost_equal(cheb.chebpts1(4), tgt)
|
| 604 |
+
|
| 605 |
+
def test_chebpts2(self):
|
| 606 |
+
# test exceptions
|
| 607 |
+
assert_raises(ValueError, cheb.chebpts2, 1.5)
|
| 608 |
+
assert_raises(ValueError, cheb.chebpts2, 1)
|
| 609 |
+
|
| 610 |
+
# test points
|
| 611 |
+
tgt = [-1, 1]
|
| 612 |
+
assert_almost_equal(cheb.chebpts2(2), tgt)
|
| 613 |
+
tgt = [-1, 0, 1]
|
| 614 |
+
assert_almost_equal(cheb.chebpts2(3), tgt)
|
| 615 |
+
tgt = [-1, -0.5, .5, 1]
|
| 616 |
+
assert_almost_equal(cheb.chebpts2(4), tgt)
|
| 617 |
+
tgt = [-1.0, -0.707106781187, 0, 0.707106781187, 1.0]
|
| 618 |
+
assert_almost_equal(cheb.chebpts2(5), tgt)
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/tests/test_classes.py
ADDED
|
@@ -0,0 +1,613 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Test inter-conversion of different polynomial classes.
|
| 2 |
+
|
| 3 |
+
This tests the convert and cast methods of all the polynomial classes.
|
| 4 |
+
|
| 5 |
+
"""
|
| 6 |
+
import operator as op
|
| 7 |
+
from numbers import Number
|
| 8 |
+
|
| 9 |
+
import pytest
|
| 10 |
+
|
| 11 |
+
import numpy as np
|
| 12 |
+
from numpy.exceptions import RankWarning
|
| 13 |
+
from numpy.polynomial import (
|
| 14 |
+
Chebyshev,
|
| 15 |
+
Hermite,
|
| 16 |
+
HermiteE,
|
| 17 |
+
Laguerre,
|
| 18 |
+
Legendre,
|
| 19 |
+
Polynomial,
|
| 20 |
+
)
|
| 21 |
+
from numpy.testing import assert_, assert_almost_equal, assert_equal, assert_raises
|
| 22 |
+
|
| 23 |
+
#
|
| 24 |
+
# fixtures
|
| 25 |
+
#
|
| 26 |
+
|
| 27 |
+
classes = (
|
| 28 |
+
Polynomial, Legendre, Chebyshev, Laguerre,
|
| 29 |
+
Hermite, HermiteE
|
| 30 |
+
)
|
| 31 |
+
classids = tuple(cls.__name__ for cls in classes)
|
| 32 |
+
|
| 33 |
+
@pytest.fixture(params=classes, ids=classids)
|
| 34 |
+
def Poly(request):
|
| 35 |
+
return request.param
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
#
|
| 39 |
+
# helper functions
|
| 40 |
+
#
|
| 41 |
+
random = np.random.random
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def assert_poly_almost_equal(p1, p2, msg=""):
|
| 45 |
+
try:
|
| 46 |
+
assert_(np.all(p1.domain == p2.domain))
|
| 47 |
+
assert_(np.all(p1.window == p2.window))
|
| 48 |
+
assert_almost_equal(p1.coef, p2.coef)
|
| 49 |
+
except AssertionError:
|
| 50 |
+
msg = f"Result: {p1}\nTarget: {p2}"
|
| 51 |
+
raise AssertionError(msg)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
#
|
| 55 |
+
# Test conversion methods that depend on combinations of two classes.
|
| 56 |
+
#
|
| 57 |
+
|
| 58 |
+
Poly1 = Poly
|
| 59 |
+
Poly2 = Poly
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def test_conversion(Poly1, Poly2):
|
| 63 |
+
x = np.linspace(0, 1, 10)
|
| 64 |
+
coef = random((3,))
|
| 65 |
+
|
| 66 |
+
d1 = Poly1.domain + random((2,)) * .25
|
| 67 |
+
w1 = Poly1.window + random((2,)) * .25
|
| 68 |
+
p1 = Poly1(coef, domain=d1, window=w1)
|
| 69 |
+
|
| 70 |
+
d2 = Poly2.domain + random((2,)) * .25
|
| 71 |
+
w2 = Poly2.window + random((2,)) * .25
|
| 72 |
+
p2 = p1.convert(kind=Poly2, domain=d2, window=w2)
|
| 73 |
+
|
| 74 |
+
assert_almost_equal(p2.domain, d2)
|
| 75 |
+
assert_almost_equal(p2.window, w2)
|
| 76 |
+
assert_almost_equal(p2(x), p1(x))
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def test_cast(Poly1, Poly2):
|
| 80 |
+
x = np.linspace(0, 1, 10)
|
| 81 |
+
coef = random((3,))
|
| 82 |
+
|
| 83 |
+
d1 = Poly1.domain + random((2,)) * .25
|
| 84 |
+
w1 = Poly1.window + random((2,)) * .25
|
| 85 |
+
p1 = Poly1(coef, domain=d1, window=w1)
|
| 86 |
+
|
| 87 |
+
d2 = Poly2.domain + random((2,)) * .25
|
| 88 |
+
w2 = Poly2.window + random((2,)) * .25
|
| 89 |
+
p2 = Poly2.cast(p1, domain=d2, window=w2)
|
| 90 |
+
|
| 91 |
+
assert_almost_equal(p2.domain, d2)
|
| 92 |
+
assert_almost_equal(p2.window, w2)
|
| 93 |
+
assert_almost_equal(p2(x), p1(x))
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
#
|
| 97 |
+
# test methods that depend on one class
|
| 98 |
+
#
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def test_identity(Poly):
|
| 102 |
+
d = Poly.domain + random((2,)) * .25
|
| 103 |
+
w = Poly.window + random((2,)) * .25
|
| 104 |
+
x = np.linspace(d[0], d[1], 11)
|
| 105 |
+
p = Poly.identity(domain=d, window=w)
|
| 106 |
+
assert_equal(p.domain, d)
|
| 107 |
+
assert_equal(p.window, w)
|
| 108 |
+
assert_almost_equal(p(x), x)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def test_basis(Poly):
|
| 112 |
+
d = Poly.domain + random((2,)) * .25
|
| 113 |
+
w = Poly.window + random((2,)) * .25
|
| 114 |
+
p = Poly.basis(5, domain=d, window=w)
|
| 115 |
+
assert_equal(p.domain, d)
|
| 116 |
+
assert_equal(p.window, w)
|
| 117 |
+
assert_equal(p.coef, [0] * 5 + [1])
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
def test_fromroots(Poly):
|
| 121 |
+
# check that requested roots are zeros of a polynomial
|
| 122 |
+
# of correct degree, domain, and window.
|
| 123 |
+
d = Poly.domain + random((2,)) * .25
|
| 124 |
+
w = Poly.window + random((2,)) * .25
|
| 125 |
+
r = random((5,))
|
| 126 |
+
p1 = Poly.fromroots(r, domain=d, window=w)
|
| 127 |
+
assert_equal(p1.degree(), len(r))
|
| 128 |
+
assert_equal(p1.domain, d)
|
| 129 |
+
assert_equal(p1.window, w)
|
| 130 |
+
assert_almost_equal(p1(r), 0)
|
| 131 |
+
|
| 132 |
+
# check that polynomial is monic
|
| 133 |
+
pdom = Polynomial.domain
|
| 134 |
+
pwin = Polynomial.window
|
| 135 |
+
p2 = Polynomial.cast(p1, domain=pdom, window=pwin)
|
| 136 |
+
assert_almost_equal(p2.coef[-1], 1)
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
def test_bad_conditioned_fit(Poly):
|
| 140 |
+
|
| 141 |
+
x = [0., 0., 1.]
|
| 142 |
+
y = [1., 2., 3.]
|
| 143 |
+
|
| 144 |
+
# check RankWarning is raised
|
| 145 |
+
with pytest.warns(RankWarning) as record:
|
| 146 |
+
Poly.fit(x, y, 2)
|
| 147 |
+
assert record[0].message.args[0] == "The fit may be poorly conditioned"
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
def test_fit(Poly):
|
| 151 |
+
|
| 152 |
+
def f(x):
|
| 153 |
+
return x * (x - 1) * (x - 2)
|
| 154 |
+
x = np.linspace(0, 3)
|
| 155 |
+
y = f(x)
|
| 156 |
+
|
| 157 |
+
# check default value of domain and window
|
| 158 |
+
p = Poly.fit(x, y, 3)
|
| 159 |
+
assert_almost_equal(p.domain, [0, 3])
|
| 160 |
+
assert_almost_equal(p(x), y)
|
| 161 |
+
assert_equal(p.degree(), 3)
|
| 162 |
+
|
| 163 |
+
# check with given domains and window
|
| 164 |
+
d = Poly.domain + random((2,)) * .25
|
| 165 |
+
w = Poly.window + random((2,)) * .25
|
| 166 |
+
p = Poly.fit(x, y, 3, domain=d, window=w)
|
| 167 |
+
assert_almost_equal(p(x), y)
|
| 168 |
+
assert_almost_equal(p.domain, d)
|
| 169 |
+
assert_almost_equal(p.window, w)
|
| 170 |
+
p = Poly.fit(x, y, [0, 1, 2, 3], domain=d, window=w)
|
| 171 |
+
assert_almost_equal(p(x), y)
|
| 172 |
+
assert_almost_equal(p.domain, d)
|
| 173 |
+
assert_almost_equal(p.window, w)
|
| 174 |
+
|
| 175 |
+
# check with class domain default
|
| 176 |
+
p = Poly.fit(x, y, 3, [])
|
| 177 |
+
assert_equal(p.domain, Poly.domain)
|
| 178 |
+
assert_equal(p.window, Poly.window)
|
| 179 |
+
p = Poly.fit(x, y, [0, 1, 2, 3], [])
|
| 180 |
+
assert_equal(p.domain, Poly.domain)
|
| 181 |
+
assert_equal(p.window, Poly.window)
|
| 182 |
+
|
| 183 |
+
# check that fit accepts weights.
|
| 184 |
+
w = np.zeros_like(x)
|
| 185 |
+
z = y + random(y.shape) * .25
|
| 186 |
+
w[::2] = 1
|
| 187 |
+
p1 = Poly.fit(x[::2], z[::2], 3)
|
| 188 |
+
p2 = Poly.fit(x, z, 3, w=w)
|
| 189 |
+
p3 = Poly.fit(x, z, [0, 1, 2, 3], w=w)
|
| 190 |
+
assert_almost_equal(p1(x), p2(x))
|
| 191 |
+
assert_almost_equal(p2(x), p3(x))
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
def test_equal(Poly):
|
| 195 |
+
p1 = Poly([1, 2, 3], domain=[0, 1], window=[2, 3])
|
| 196 |
+
p2 = Poly([1, 1, 1], domain=[0, 1], window=[2, 3])
|
| 197 |
+
p3 = Poly([1, 2, 3], domain=[1, 2], window=[2, 3])
|
| 198 |
+
p4 = Poly([1, 2, 3], domain=[0, 1], window=[1, 2])
|
| 199 |
+
assert_(p1 == p1)
|
| 200 |
+
assert_(not p1 == p2)
|
| 201 |
+
assert_(not p1 == p3)
|
| 202 |
+
assert_(not p1 == p4)
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
def test_not_equal(Poly):
|
| 206 |
+
p1 = Poly([1, 2, 3], domain=[0, 1], window=[2, 3])
|
| 207 |
+
p2 = Poly([1, 1, 1], domain=[0, 1], window=[2, 3])
|
| 208 |
+
p3 = Poly([1, 2, 3], domain=[1, 2], window=[2, 3])
|
| 209 |
+
p4 = Poly([1, 2, 3], domain=[0, 1], window=[1, 2])
|
| 210 |
+
assert_(not p1 != p1)
|
| 211 |
+
assert_(p1 != p2)
|
| 212 |
+
assert_(p1 != p3)
|
| 213 |
+
assert_(p1 != p4)
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
def test_add(Poly):
|
| 217 |
+
# This checks commutation, not numerical correctness
|
| 218 |
+
c1 = list(random((4,)) + .5)
|
| 219 |
+
c2 = list(random((3,)) + .5)
|
| 220 |
+
p1 = Poly(c1)
|
| 221 |
+
p2 = Poly(c2)
|
| 222 |
+
p3 = p1 + p2
|
| 223 |
+
assert_poly_almost_equal(p2 + p1, p3)
|
| 224 |
+
assert_poly_almost_equal(p1 + c2, p3)
|
| 225 |
+
assert_poly_almost_equal(c2 + p1, p3)
|
| 226 |
+
assert_poly_almost_equal(p1 + tuple(c2), p3)
|
| 227 |
+
assert_poly_almost_equal(tuple(c2) + p1, p3)
|
| 228 |
+
assert_poly_almost_equal(p1 + np.array(c2), p3)
|
| 229 |
+
assert_poly_almost_equal(np.array(c2) + p1, p3)
|
| 230 |
+
assert_raises(TypeError, op.add, p1, Poly([0], domain=Poly.domain + 1))
|
| 231 |
+
assert_raises(TypeError, op.add, p1, Poly([0], window=Poly.window + 1))
|
| 232 |
+
if Poly is Polynomial:
|
| 233 |
+
assert_raises(TypeError, op.add, p1, Chebyshev([0]))
|
| 234 |
+
else:
|
| 235 |
+
assert_raises(TypeError, op.add, p1, Polynomial([0]))
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
def test_sub(Poly):
|
| 239 |
+
# This checks commutation, not numerical correctness
|
| 240 |
+
c1 = list(random((4,)) + .5)
|
| 241 |
+
c2 = list(random((3,)) + .5)
|
| 242 |
+
p1 = Poly(c1)
|
| 243 |
+
p2 = Poly(c2)
|
| 244 |
+
p3 = p1 - p2
|
| 245 |
+
assert_poly_almost_equal(p2 - p1, -p3)
|
| 246 |
+
assert_poly_almost_equal(p1 - c2, p3)
|
| 247 |
+
assert_poly_almost_equal(c2 - p1, -p3)
|
| 248 |
+
assert_poly_almost_equal(p1 - tuple(c2), p3)
|
| 249 |
+
assert_poly_almost_equal(tuple(c2) - p1, -p3)
|
| 250 |
+
assert_poly_almost_equal(p1 - np.array(c2), p3)
|
| 251 |
+
assert_poly_almost_equal(np.array(c2) - p1, -p3)
|
| 252 |
+
assert_raises(TypeError, op.sub, p1, Poly([0], domain=Poly.domain + 1))
|
| 253 |
+
assert_raises(TypeError, op.sub, p1, Poly([0], window=Poly.window + 1))
|
| 254 |
+
if Poly is Polynomial:
|
| 255 |
+
assert_raises(TypeError, op.sub, p1, Chebyshev([0]))
|
| 256 |
+
else:
|
| 257 |
+
assert_raises(TypeError, op.sub, p1, Polynomial([0]))
|
| 258 |
+
|
| 259 |
+
|
| 260 |
+
def test_mul(Poly):
|
| 261 |
+
c1 = list(random((4,)) + .5)
|
| 262 |
+
c2 = list(random((3,)) + .5)
|
| 263 |
+
p1 = Poly(c1)
|
| 264 |
+
p2 = Poly(c2)
|
| 265 |
+
p3 = p1 * p2
|
| 266 |
+
assert_poly_almost_equal(p2 * p1, p3)
|
| 267 |
+
assert_poly_almost_equal(p1 * c2, p3)
|
| 268 |
+
assert_poly_almost_equal(c2 * p1, p3)
|
| 269 |
+
assert_poly_almost_equal(p1 * tuple(c2), p3)
|
| 270 |
+
assert_poly_almost_equal(tuple(c2) * p1, p3)
|
| 271 |
+
assert_poly_almost_equal(p1 * np.array(c2), p3)
|
| 272 |
+
assert_poly_almost_equal(np.array(c2) * p1, p3)
|
| 273 |
+
assert_poly_almost_equal(p1 * 2, p1 * Poly([2]))
|
| 274 |
+
assert_poly_almost_equal(2 * p1, p1 * Poly([2]))
|
| 275 |
+
assert_raises(TypeError, op.mul, p1, Poly([0], domain=Poly.domain + 1))
|
| 276 |
+
assert_raises(TypeError, op.mul, p1, Poly([0], window=Poly.window + 1))
|
| 277 |
+
if Poly is Polynomial:
|
| 278 |
+
assert_raises(TypeError, op.mul, p1, Chebyshev([0]))
|
| 279 |
+
else:
|
| 280 |
+
assert_raises(TypeError, op.mul, p1, Polynomial([0]))
|
| 281 |
+
|
| 282 |
+
|
| 283 |
+
def test_floordiv(Poly):
|
| 284 |
+
c1 = list(random((4,)) + .5)
|
| 285 |
+
c2 = list(random((3,)) + .5)
|
| 286 |
+
c3 = list(random((2,)) + .5)
|
| 287 |
+
p1 = Poly(c1)
|
| 288 |
+
p2 = Poly(c2)
|
| 289 |
+
p3 = Poly(c3)
|
| 290 |
+
p4 = p1 * p2 + p3
|
| 291 |
+
c4 = list(p4.coef)
|
| 292 |
+
assert_poly_almost_equal(p4 // p2, p1)
|
| 293 |
+
assert_poly_almost_equal(p4 // c2, p1)
|
| 294 |
+
assert_poly_almost_equal(c4 // p2, p1)
|
| 295 |
+
assert_poly_almost_equal(p4 // tuple(c2), p1)
|
| 296 |
+
assert_poly_almost_equal(tuple(c4) // p2, p1)
|
| 297 |
+
assert_poly_almost_equal(p4 // np.array(c2), p1)
|
| 298 |
+
assert_poly_almost_equal(np.array(c4) // p2, p1)
|
| 299 |
+
assert_poly_almost_equal(2 // p2, Poly([0]))
|
| 300 |
+
assert_poly_almost_equal(p2 // 2, 0.5 * p2)
|
| 301 |
+
assert_raises(
|
| 302 |
+
TypeError, op.floordiv, p1, Poly([0], domain=Poly.domain + 1))
|
| 303 |
+
assert_raises(
|
| 304 |
+
TypeError, op.floordiv, p1, Poly([0], window=Poly.window + 1))
|
| 305 |
+
if Poly is Polynomial:
|
| 306 |
+
assert_raises(TypeError, op.floordiv, p1, Chebyshev([0]))
|
| 307 |
+
else:
|
| 308 |
+
assert_raises(TypeError, op.floordiv, p1, Polynomial([0]))
|
| 309 |
+
|
| 310 |
+
|
| 311 |
+
def test_truediv(Poly):
|
| 312 |
+
# true division is valid only if the denominator is a Number and
|
| 313 |
+
# not a python bool.
|
| 314 |
+
p1 = Poly([1, 2, 3])
|
| 315 |
+
p2 = p1 * 5
|
| 316 |
+
|
| 317 |
+
for stype in np.ScalarType:
|
| 318 |
+
if not issubclass(stype, Number) or issubclass(stype, bool):
|
| 319 |
+
continue
|
| 320 |
+
s = stype(5)
|
| 321 |
+
assert_poly_almost_equal(op.truediv(p2, s), p1)
|
| 322 |
+
assert_raises(TypeError, op.truediv, s, p2)
|
| 323 |
+
for stype in (int, float):
|
| 324 |
+
s = stype(5)
|
| 325 |
+
assert_poly_almost_equal(op.truediv(p2, s), p1)
|
| 326 |
+
assert_raises(TypeError, op.truediv, s, p2)
|
| 327 |
+
for stype in [complex]:
|
| 328 |
+
s = stype(5, 0)
|
| 329 |
+
assert_poly_almost_equal(op.truediv(p2, s), p1)
|
| 330 |
+
assert_raises(TypeError, op.truediv, s, p2)
|
| 331 |
+
for s in [(), [], {}, False, np.array([1])]:
|
| 332 |
+
assert_raises(TypeError, op.truediv, p2, s)
|
| 333 |
+
assert_raises(TypeError, op.truediv, s, p2)
|
| 334 |
+
for ptype in classes:
|
| 335 |
+
assert_raises(TypeError, op.truediv, p2, ptype(1))
|
| 336 |
+
|
| 337 |
+
|
| 338 |
+
def test_mod(Poly):
|
| 339 |
+
# This checks commutation, not numerical correctness
|
| 340 |
+
c1 = list(random((4,)) + .5)
|
| 341 |
+
c2 = list(random((3,)) + .5)
|
| 342 |
+
c3 = list(random((2,)) + .5)
|
| 343 |
+
p1 = Poly(c1)
|
| 344 |
+
p2 = Poly(c2)
|
| 345 |
+
p3 = Poly(c3)
|
| 346 |
+
p4 = p1 * p2 + p3
|
| 347 |
+
c4 = list(p4.coef)
|
| 348 |
+
assert_poly_almost_equal(p4 % p2, p3)
|
| 349 |
+
assert_poly_almost_equal(p4 % c2, p3)
|
| 350 |
+
assert_poly_almost_equal(c4 % p2, p3)
|
| 351 |
+
assert_poly_almost_equal(p4 % tuple(c2), p3)
|
| 352 |
+
assert_poly_almost_equal(tuple(c4) % p2, p3)
|
| 353 |
+
assert_poly_almost_equal(p4 % np.array(c2), p3)
|
| 354 |
+
assert_poly_almost_equal(np.array(c4) % p2, p3)
|
| 355 |
+
assert_poly_almost_equal(2 % p2, Poly([2]))
|
| 356 |
+
assert_poly_almost_equal(p2 % 2, Poly([0]))
|
| 357 |
+
assert_raises(TypeError, op.mod, p1, Poly([0], domain=Poly.domain + 1))
|
| 358 |
+
assert_raises(TypeError, op.mod, p1, Poly([0], window=Poly.window + 1))
|
| 359 |
+
if Poly is Polynomial:
|
| 360 |
+
assert_raises(TypeError, op.mod, p1, Chebyshev([0]))
|
| 361 |
+
else:
|
| 362 |
+
assert_raises(TypeError, op.mod, p1, Polynomial([0]))
|
| 363 |
+
|
| 364 |
+
|
| 365 |
+
def test_divmod(Poly):
|
| 366 |
+
# This checks commutation, not numerical correctness
|
| 367 |
+
c1 = list(random((4,)) + .5)
|
| 368 |
+
c2 = list(random((3,)) + .5)
|
| 369 |
+
c3 = list(random((2,)) + .5)
|
| 370 |
+
p1 = Poly(c1)
|
| 371 |
+
p2 = Poly(c2)
|
| 372 |
+
p3 = Poly(c3)
|
| 373 |
+
p4 = p1 * p2 + p3
|
| 374 |
+
c4 = list(p4.coef)
|
| 375 |
+
quo, rem = divmod(p4, p2)
|
| 376 |
+
assert_poly_almost_equal(quo, p1)
|
| 377 |
+
assert_poly_almost_equal(rem, p3)
|
| 378 |
+
quo, rem = divmod(p4, c2)
|
| 379 |
+
assert_poly_almost_equal(quo, p1)
|
| 380 |
+
assert_poly_almost_equal(rem, p3)
|
| 381 |
+
quo, rem = divmod(c4, p2)
|
| 382 |
+
assert_poly_almost_equal(quo, p1)
|
| 383 |
+
assert_poly_almost_equal(rem, p3)
|
| 384 |
+
quo, rem = divmod(p4, tuple(c2))
|
| 385 |
+
assert_poly_almost_equal(quo, p1)
|
| 386 |
+
assert_poly_almost_equal(rem, p3)
|
| 387 |
+
quo, rem = divmod(tuple(c4), p2)
|
| 388 |
+
assert_poly_almost_equal(quo, p1)
|
| 389 |
+
assert_poly_almost_equal(rem, p3)
|
| 390 |
+
quo, rem = divmod(p4, np.array(c2))
|
| 391 |
+
assert_poly_almost_equal(quo, p1)
|
| 392 |
+
assert_poly_almost_equal(rem, p3)
|
| 393 |
+
quo, rem = divmod(np.array(c4), p2)
|
| 394 |
+
assert_poly_almost_equal(quo, p1)
|
| 395 |
+
assert_poly_almost_equal(rem, p3)
|
| 396 |
+
quo, rem = divmod(p2, 2)
|
| 397 |
+
assert_poly_almost_equal(quo, 0.5 * p2)
|
| 398 |
+
assert_poly_almost_equal(rem, Poly([0]))
|
| 399 |
+
quo, rem = divmod(2, p2)
|
| 400 |
+
assert_poly_almost_equal(quo, Poly([0]))
|
| 401 |
+
assert_poly_almost_equal(rem, Poly([2]))
|
| 402 |
+
assert_raises(TypeError, divmod, p1, Poly([0], domain=Poly.domain + 1))
|
| 403 |
+
assert_raises(TypeError, divmod, p1, Poly([0], window=Poly.window + 1))
|
| 404 |
+
if Poly is Polynomial:
|
| 405 |
+
assert_raises(TypeError, divmod, p1, Chebyshev([0]))
|
| 406 |
+
else:
|
| 407 |
+
assert_raises(TypeError, divmod, p1, Polynomial([0]))
|
| 408 |
+
|
| 409 |
+
|
| 410 |
+
def test_roots(Poly):
|
| 411 |
+
d = Poly.domain * 1.25 + .25
|
| 412 |
+
w = Poly.window
|
| 413 |
+
tgt = np.linspace(d[0], d[1], 5)
|
| 414 |
+
res = np.sort(Poly.fromroots(tgt, domain=d, window=w).roots())
|
| 415 |
+
assert_almost_equal(res, tgt)
|
| 416 |
+
# default domain and window
|
| 417 |
+
res = np.sort(Poly.fromroots(tgt).roots())
|
| 418 |
+
assert_almost_equal(res, tgt)
|
| 419 |
+
|
| 420 |
+
|
| 421 |
+
def test_degree(Poly):
|
| 422 |
+
p = Poly.basis(5)
|
| 423 |
+
assert_equal(p.degree(), 5)
|
| 424 |
+
|
| 425 |
+
|
| 426 |
+
def test_copy(Poly):
|
| 427 |
+
p1 = Poly.basis(5)
|
| 428 |
+
p2 = p1.copy()
|
| 429 |
+
assert_(p1 == p2)
|
| 430 |
+
assert_(p1 is not p2)
|
| 431 |
+
assert_(p1.coef is not p2.coef)
|
| 432 |
+
assert_(p1.domain is not p2.domain)
|
| 433 |
+
assert_(p1.window is not p2.window)
|
| 434 |
+
|
| 435 |
+
|
| 436 |
+
def test_integ(Poly):
|
| 437 |
+
P = Polynomial
|
| 438 |
+
# Check defaults
|
| 439 |
+
p0 = Poly.cast(P([1 * 2, 2 * 3, 3 * 4]))
|
| 440 |
+
p1 = P.cast(p0.integ())
|
| 441 |
+
p2 = P.cast(p0.integ(2))
|
| 442 |
+
assert_poly_almost_equal(p1, P([0, 2, 3, 4]))
|
| 443 |
+
assert_poly_almost_equal(p2, P([0, 0, 1, 1, 1]))
|
| 444 |
+
# Check with k
|
| 445 |
+
p0 = Poly.cast(P([1 * 2, 2 * 3, 3 * 4]))
|
| 446 |
+
p1 = P.cast(p0.integ(k=1))
|
| 447 |
+
p2 = P.cast(p0.integ(2, k=[1, 1]))
|
| 448 |
+
assert_poly_almost_equal(p1, P([1, 2, 3, 4]))
|
| 449 |
+
assert_poly_almost_equal(p2, P([1, 1, 1, 1, 1]))
|
| 450 |
+
# Check with lbnd
|
| 451 |
+
p0 = Poly.cast(P([1 * 2, 2 * 3, 3 * 4]))
|
| 452 |
+
p1 = P.cast(p0.integ(lbnd=1))
|
| 453 |
+
p2 = P.cast(p0.integ(2, lbnd=1))
|
| 454 |
+
assert_poly_almost_equal(p1, P([-9, 2, 3, 4]))
|
| 455 |
+
assert_poly_almost_equal(p2, P([6, -9, 1, 1, 1]))
|
| 456 |
+
# Check scaling
|
| 457 |
+
d = 2 * Poly.domain
|
| 458 |
+
p0 = Poly.cast(P([1 * 2, 2 * 3, 3 * 4]), domain=d)
|
| 459 |
+
p1 = P.cast(p0.integ())
|
| 460 |
+
p2 = P.cast(p0.integ(2))
|
| 461 |
+
assert_poly_almost_equal(p1, P([0, 2, 3, 4]))
|
| 462 |
+
assert_poly_almost_equal(p2, P([0, 0, 1, 1, 1]))
|
| 463 |
+
|
| 464 |
+
|
| 465 |
+
def test_deriv(Poly):
|
| 466 |
+
# Check that the derivative is the inverse of integration. It is
|
| 467 |
+
# assumes that the integration has been checked elsewhere.
|
| 468 |
+
d = Poly.domain + random((2,)) * .25
|
| 469 |
+
w = Poly.window + random((2,)) * .25
|
| 470 |
+
p1 = Poly([1, 2, 3], domain=d, window=w)
|
| 471 |
+
p2 = p1.integ(2, k=[1, 2])
|
| 472 |
+
p3 = p1.integ(1, k=[1])
|
| 473 |
+
assert_almost_equal(p2.deriv(1).coef, p3.coef)
|
| 474 |
+
assert_almost_equal(p2.deriv(2).coef, p1.coef)
|
| 475 |
+
# default domain and window
|
| 476 |
+
p1 = Poly([1, 2, 3])
|
| 477 |
+
p2 = p1.integ(2, k=[1, 2])
|
| 478 |
+
p3 = p1.integ(1, k=[1])
|
| 479 |
+
assert_almost_equal(p2.deriv(1).coef, p3.coef)
|
| 480 |
+
assert_almost_equal(p2.deriv(2).coef, p1.coef)
|
| 481 |
+
|
| 482 |
+
|
| 483 |
+
def test_linspace(Poly):
|
| 484 |
+
d = Poly.domain + random((2,)) * .25
|
| 485 |
+
w = Poly.window + random((2,)) * .25
|
| 486 |
+
p = Poly([1, 2, 3], domain=d, window=w)
|
| 487 |
+
# check default domain
|
| 488 |
+
xtgt = np.linspace(d[0], d[1], 20)
|
| 489 |
+
ytgt = p(xtgt)
|
| 490 |
+
xres, yres = p.linspace(20)
|
| 491 |
+
assert_almost_equal(xres, xtgt)
|
| 492 |
+
assert_almost_equal(yres, ytgt)
|
| 493 |
+
# check specified domain
|
| 494 |
+
xtgt = np.linspace(0, 2, 20)
|
| 495 |
+
ytgt = p(xtgt)
|
| 496 |
+
xres, yres = p.linspace(20, domain=[0, 2])
|
| 497 |
+
assert_almost_equal(xres, xtgt)
|
| 498 |
+
assert_almost_equal(yres, ytgt)
|
| 499 |
+
|
| 500 |
+
|
| 501 |
+
def test_pow(Poly):
|
| 502 |
+
d = Poly.domain + random((2,)) * .25
|
| 503 |
+
w = Poly.window + random((2,)) * .25
|
| 504 |
+
tgt = Poly([1], domain=d, window=w)
|
| 505 |
+
tst = Poly([1, 2, 3], domain=d, window=w)
|
| 506 |
+
for i in range(5):
|
| 507 |
+
assert_poly_almost_equal(tst**i, tgt)
|
| 508 |
+
tgt = tgt * tst
|
| 509 |
+
# default domain and window
|
| 510 |
+
tgt = Poly([1])
|
| 511 |
+
tst = Poly([1, 2, 3])
|
| 512 |
+
for i in range(5):
|
| 513 |
+
assert_poly_almost_equal(tst**i, tgt)
|
| 514 |
+
tgt = tgt * tst
|
| 515 |
+
# check error for invalid powers
|
| 516 |
+
assert_raises(ValueError, op.pow, tgt, 1.5)
|
| 517 |
+
assert_raises(ValueError, op.pow, tgt, -1)
|
| 518 |
+
|
| 519 |
+
|
| 520 |
+
def test_call(Poly):
|
| 521 |
+
P = Polynomial
|
| 522 |
+
d = Poly.domain
|
| 523 |
+
x = np.linspace(d[0], d[1], 11)
|
| 524 |
+
|
| 525 |
+
# Check defaults
|
| 526 |
+
p = Poly.cast(P([1, 2, 3]))
|
| 527 |
+
tgt = 1 + x * (2 + 3 * x)
|
| 528 |
+
res = p(x)
|
| 529 |
+
assert_almost_equal(res, tgt)
|
| 530 |
+
|
| 531 |
+
|
| 532 |
+
def test_call_with_list(Poly):
|
| 533 |
+
p = Poly([1, 2, 3])
|
| 534 |
+
x = [-1, 0, 2]
|
| 535 |
+
res = p(x)
|
| 536 |
+
assert_equal(res, p(np.array(x)))
|
| 537 |
+
|
| 538 |
+
|
| 539 |
+
def test_cutdeg(Poly):
|
| 540 |
+
p = Poly([1, 2, 3])
|
| 541 |
+
assert_raises(ValueError, p.cutdeg, .5)
|
| 542 |
+
assert_raises(ValueError, p.cutdeg, -1)
|
| 543 |
+
assert_equal(len(p.cutdeg(3)), 3)
|
| 544 |
+
assert_equal(len(p.cutdeg(2)), 3)
|
| 545 |
+
assert_equal(len(p.cutdeg(1)), 2)
|
| 546 |
+
assert_equal(len(p.cutdeg(0)), 1)
|
| 547 |
+
|
| 548 |
+
|
| 549 |
+
def test_truncate(Poly):
|
| 550 |
+
p = Poly([1, 2, 3])
|
| 551 |
+
assert_raises(ValueError, p.truncate, .5)
|
| 552 |
+
assert_raises(ValueError, p.truncate, 0)
|
| 553 |
+
assert_equal(len(p.truncate(4)), 3)
|
| 554 |
+
assert_equal(len(p.truncate(3)), 3)
|
| 555 |
+
assert_equal(len(p.truncate(2)), 2)
|
| 556 |
+
assert_equal(len(p.truncate(1)), 1)
|
| 557 |
+
|
| 558 |
+
|
| 559 |
+
def test_trim(Poly):
|
| 560 |
+
c = [1, 1e-6, 1e-12, 0]
|
| 561 |
+
p = Poly(c)
|
| 562 |
+
assert_equal(p.trim().coef, c[:3])
|
| 563 |
+
assert_equal(p.trim(1e-10).coef, c[:2])
|
| 564 |
+
assert_equal(p.trim(1e-5).coef, c[:1])
|
| 565 |
+
|
| 566 |
+
|
| 567 |
+
def test_mapparms(Poly):
|
| 568 |
+
# check with defaults. Should be identity.
|
| 569 |
+
d = Poly.domain
|
| 570 |
+
w = Poly.window
|
| 571 |
+
p = Poly([1], domain=d, window=w)
|
| 572 |
+
assert_almost_equal([0, 1], p.mapparms())
|
| 573 |
+
#
|
| 574 |
+
w = 2 * d + 1
|
| 575 |
+
p = Poly([1], domain=d, window=w)
|
| 576 |
+
assert_almost_equal([1, 2], p.mapparms())
|
| 577 |
+
|
| 578 |
+
|
| 579 |
+
def test_ufunc_override(Poly):
|
| 580 |
+
p = Poly([1, 2, 3])
|
| 581 |
+
x = np.ones(3)
|
| 582 |
+
assert_raises(TypeError, np.add, p, x)
|
| 583 |
+
assert_raises(TypeError, np.add, x, p)
|
| 584 |
+
|
| 585 |
+
|
| 586 |
+
#
|
| 587 |
+
# Test class method that only exists for some classes
|
| 588 |
+
#
|
| 589 |
+
|
| 590 |
+
|
| 591 |
+
class TestInterpolate:
|
| 592 |
+
|
| 593 |
+
def f(self, x):
|
| 594 |
+
return x * (x - 1) * (x - 2)
|
| 595 |
+
|
| 596 |
+
def test_raises(self):
|
| 597 |
+
assert_raises(ValueError, Chebyshev.interpolate, self.f, -1)
|
| 598 |
+
assert_raises(TypeError, Chebyshev.interpolate, self.f, 10.)
|
| 599 |
+
|
| 600 |
+
def test_dimensions(self):
|
| 601 |
+
for deg in range(1, 5):
|
| 602 |
+
assert_(Chebyshev.interpolate(self.f, deg).degree() == deg)
|
| 603 |
+
|
| 604 |
+
def test_approximation(self):
|
| 605 |
+
|
| 606 |
+
def powx(x, p):
|
| 607 |
+
return x**p
|
| 608 |
+
|
| 609 |
+
x = np.linspace(0, 2, 10)
|
| 610 |
+
for deg in range(10):
|
| 611 |
+
for t in range(deg + 1):
|
| 612 |
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for hermite module.
|
| 2 |
+
|
| 3 |
+
"""
|
| 4 |
+
from functools import reduce
|
| 5 |
+
|
| 6 |
+
import numpy as np
|
| 7 |
+
import numpy.polynomial.hermite as herm
|
| 8 |
+
from numpy.polynomial.polynomial import polyval
|
| 9 |
+
from numpy.testing import assert_, assert_almost_equal, assert_equal, assert_raises
|
| 10 |
+
|
| 11 |
+
H0 = np.array([1])
|
| 12 |
+
H1 = np.array([0, 2])
|
| 13 |
+
H2 = np.array([-2, 0, 4])
|
| 14 |
+
H3 = np.array([0, -12, 0, 8])
|
| 15 |
+
H4 = np.array([12, 0, -48, 0, 16])
|
| 16 |
+
H5 = np.array([0, 120, 0, -160, 0, 32])
|
| 17 |
+
H6 = np.array([-120, 0, 720, 0, -480, 0, 64])
|
| 18 |
+
H7 = np.array([0, -1680, 0, 3360, 0, -1344, 0, 128])
|
| 19 |
+
H8 = np.array([1680, 0, -13440, 0, 13440, 0, -3584, 0, 256])
|
| 20 |
+
H9 = np.array([0, 30240, 0, -80640, 0, 48384, 0, -9216, 0, 512])
|
| 21 |
+
|
| 22 |
+
Hlist = [H0, H1, H2, H3, H4, H5, H6, H7, H8, H9]
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def trim(x):
|
| 26 |
+
return herm.hermtrim(x, tol=1e-6)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
class TestConstants:
|
| 30 |
+
|
| 31 |
+
def test_hermdomain(self):
|
| 32 |
+
assert_equal(herm.hermdomain, [-1, 1])
|
| 33 |
+
|
| 34 |
+
def test_hermzero(self):
|
| 35 |
+
assert_equal(herm.hermzero, [0])
|
| 36 |
+
|
| 37 |
+
def test_hermone(self):
|
| 38 |
+
assert_equal(herm.hermone, [1])
|
| 39 |
+
|
| 40 |
+
def test_hermx(self):
|
| 41 |
+
assert_equal(herm.hermx, [0, .5])
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
class TestArithmetic:
|
| 45 |
+
x = np.linspace(-3, 3, 100)
|
| 46 |
+
|
| 47 |
+
def test_hermadd(self):
|
| 48 |
+
for i in range(5):
|
| 49 |
+
for j in range(5):
|
| 50 |
+
msg = f"At i={i}, j={j}"
|
| 51 |
+
tgt = np.zeros(max(i, j) + 1)
|
| 52 |
+
tgt[i] += 1
|
| 53 |
+
tgt[j] += 1
|
| 54 |
+
res = herm.hermadd([0] * i + [1], [0] * j + [1])
|
| 55 |
+
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
| 56 |
+
|
| 57 |
+
def test_hermsub(self):
|
| 58 |
+
for i in range(5):
|
| 59 |
+
for j in range(5):
|
| 60 |
+
msg = f"At i={i}, j={j}"
|
| 61 |
+
tgt = np.zeros(max(i, j) + 1)
|
| 62 |
+
tgt[i] += 1
|
| 63 |
+
tgt[j] -= 1
|
| 64 |
+
res = herm.hermsub([0] * i + [1], [0] * j + [1])
|
| 65 |
+
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
| 66 |
+
|
| 67 |
+
def test_hermmulx(self):
|
| 68 |
+
assert_equal(herm.hermmulx([0]), [0])
|
| 69 |
+
assert_equal(herm.hermmulx([1]), [0, .5])
|
| 70 |
+
for i in range(1, 5):
|
| 71 |
+
ser = [0] * i + [1]
|
| 72 |
+
tgt = [0] * (i - 1) + [i, 0, .5]
|
| 73 |
+
assert_equal(herm.hermmulx(ser), tgt)
|
| 74 |
+
|
| 75 |
+
def test_hermmul(self):
|
| 76 |
+
# check values of result
|
| 77 |
+
for i in range(5):
|
| 78 |
+
pol1 = [0] * i + [1]
|
| 79 |
+
val1 = herm.hermval(self.x, pol1)
|
| 80 |
+
for j in range(5):
|
| 81 |
+
msg = f"At i={i}, j={j}"
|
| 82 |
+
pol2 = [0] * j + [1]
|
| 83 |
+
val2 = herm.hermval(self.x, pol2)
|
| 84 |
+
pol3 = herm.hermmul(pol1, pol2)
|
| 85 |
+
val3 = herm.hermval(self.x, pol3)
|
| 86 |
+
assert_(len(pol3) == i + j + 1, msg)
|
| 87 |
+
assert_almost_equal(val3, val1 * val2, err_msg=msg)
|
| 88 |
+
|
| 89 |
+
def test_hermdiv(self):
|
| 90 |
+
for i in range(5):
|
| 91 |
+
for j in range(5):
|
| 92 |
+
msg = f"At i={i}, j={j}"
|
| 93 |
+
ci = [0] * i + [1]
|
| 94 |
+
cj = [0] * j + [1]
|
| 95 |
+
tgt = herm.hermadd(ci, cj)
|
| 96 |
+
quo, rem = herm.hermdiv(tgt, ci)
|
| 97 |
+
res = herm.hermadd(herm.hermmul(quo, ci), rem)
|
| 98 |
+
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
| 99 |
+
|
| 100 |
+
def test_hermpow(self):
|
| 101 |
+
for i in range(5):
|
| 102 |
+
for j in range(5):
|
| 103 |
+
msg = f"At i={i}, j={j}"
|
| 104 |
+
c = np.arange(i + 1)
|
| 105 |
+
tgt = reduce(herm.hermmul, [c] * j, np.array([1]))
|
| 106 |
+
res = herm.hermpow(c, j)
|
| 107 |
+
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
class TestEvaluation:
|
| 111 |
+
# coefficients of 1 + 2*x + 3*x**2
|
| 112 |
+
c1d = np.array([2.5, 1., .75])
|
| 113 |
+
c2d = np.einsum('i,j->ij', c1d, c1d)
|
| 114 |
+
c3d = np.einsum('i,j,k->ijk', c1d, c1d, c1d)
|
| 115 |
+
|
| 116 |
+
# some random values in [-1, 1)
|
| 117 |
+
x = np.random.random((3, 5)) * 2 - 1
|
| 118 |
+
y = polyval(x, [1., 2., 3.])
|
| 119 |
+
|
| 120 |
+
def test_hermval(self):
|
| 121 |
+
# check empty input
|
| 122 |
+
assert_equal(herm.hermval([], [1]).size, 0)
|
| 123 |
+
|
| 124 |
+
# check normal input)
|
| 125 |
+
x = np.linspace(-1, 1)
|
| 126 |
+
y = [polyval(x, c) for c in Hlist]
|
| 127 |
+
for i in range(10):
|
| 128 |
+
msg = f"At i={i}"
|
| 129 |
+
tgt = y[i]
|
| 130 |
+
res = herm.hermval(x, [0] * i + [1])
|
| 131 |
+
assert_almost_equal(res, tgt, err_msg=msg)
|
| 132 |
+
|
| 133 |
+
# check that shape is preserved
|
| 134 |
+
for i in range(3):
|
| 135 |
+
dims = [2] * i
|
| 136 |
+
x = np.zeros(dims)
|
| 137 |
+
assert_equal(herm.hermval(x, [1]).shape, dims)
|
| 138 |
+
assert_equal(herm.hermval(x, [1, 0]).shape, dims)
|
| 139 |
+
assert_equal(herm.hermval(x, [1, 0, 0]).shape, dims)
|
| 140 |
+
|
| 141 |
+
def test_hermval2d(self):
|
| 142 |
+
x1, x2, x3 = self.x
|
| 143 |
+
y1, y2, y3 = self.y
|
| 144 |
+
|
| 145 |
+
# test exceptions
|
| 146 |
+
assert_raises(ValueError, herm.hermval2d, x1, x2[:2], self.c2d)
|
| 147 |
+
|
| 148 |
+
# test values
|
| 149 |
+
tgt = y1 * y2
|
| 150 |
+
res = herm.hermval2d(x1, x2, self.c2d)
|
| 151 |
+
assert_almost_equal(res, tgt)
|
| 152 |
+
|
| 153 |
+
# test shape
|
| 154 |
+
z = np.ones((2, 3))
|
| 155 |
+
res = herm.hermval2d(z, z, self.c2d)
|
| 156 |
+
assert_(res.shape == (2, 3))
|
| 157 |
+
|
| 158 |
+
def test_hermval3d(self):
|
| 159 |
+
x1, x2, x3 = self.x
|
| 160 |
+
y1, y2, y3 = self.y
|
| 161 |
+
|
| 162 |
+
# test exceptions
|
| 163 |
+
assert_raises(ValueError, herm.hermval3d, x1, x2, x3[:2], self.c3d)
|
| 164 |
+
|
| 165 |
+
# test values
|
| 166 |
+
tgt = y1 * y2 * y3
|
| 167 |
+
res = herm.hermval3d(x1, x2, x3, self.c3d)
|
| 168 |
+
assert_almost_equal(res, tgt)
|
| 169 |
+
|
| 170 |
+
# test shape
|
| 171 |
+
z = np.ones((2, 3))
|
| 172 |
+
res = herm.hermval3d(z, z, z, self.c3d)
|
| 173 |
+
assert_(res.shape == (2, 3))
|
| 174 |
+
|
| 175 |
+
def test_hermgrid2d(self):
|
| 176 |
+
x1, x2, x3 = self.x
|
| 177 |
+
y1, y2, y3 = self.y
|
| 178 |
+
|
| 179 |
+
# test values
|
| 180 |
+
tgt = np.einsum('i,j->ij', y1, y2)
|
| 181 |
+
res = herm.hermgrid2d(x1, x2, self.c2d)
|
| 182 |
+
assert_almost_equal(res, tgt)
|
| 183 |
+
|
| 184 |
+
# test shape
|
| 185 |
+
z = np.ones((2, 3))
|
| 186 |
+
res = herm.hermgrid2d(z, z, self.c2d)
|
| 187 |
+
assert_(res.shape == (2, 3) * 2)
|
| 188 |
+
|
| 189 |
+
def test_hermgrid3d(self):
|
| 190 |
+
x1, x2, x3 = self.x
|
| 191 |
+
y1, y2, y3 = self.y
|
| 192 |
+
|
| 193 |
+
# test values
|
| 194 |
+
tgt = np.einsum('i,j,k->ijk', y1, y2, y3)
|
| 195 |
+
res = herm.hermgrid3d(x1, x2, x3, self.c3d)
|
| 196 |
+
assert_almost_equal(res, tgt)
|
| 197 |
+
|
| 198 |
+
# test shape
|
| 199 |
+
z = np.ones((2, 3))
|
| 200 |
+
res = herm.hermgrid3d(z, z, z, self.c3d)
|
| 201 |
+
assert_(res.shape == (2, 3) * 3)
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
class TestIntegral:
|
| 205 |
+
|
| 206 |
+
def test_hermint(self):
|
| 207 |
+
# check exceptions
|
| 208 |
+
assert_raises(TypeError, herm.hermint, [0], .5)
|
| 209 |
+
assert_raises(ValueError, herm.hermint, [0], -1)
|
| 210 |
+
assert_raises(ValueError, herm.hermint, [0], 1, [0, 0])
|
| 211 |
+
assert_raises(ValueError, herm.hermint, [0], lbnd=[0])
|
| 212 |
+
assert_raises(ValueError, herm.hermint, [0], scl=[0])
|
| 213 |
+
assert_raises(TypeError, herm.hermint, [0], axis=.5)
|
| 214 |
+
|
| 215 |
+
# test integration of zero polynomial
|
| 216 |
+
for i in range(2, 5):
|
| 217 |
+
k = [0] * (i - 2) + [1]
|
| 218 |
+
res = herm.hermint([0], m=i, k=k)
|
| 219 |
+
assert_almost_equal(res, [0, .5])
|
| 220 |
+
|
| 221 |
+
# check single integration with integration constant
|
| 222 |
+
for i in range(5):
|
| 223 |
+
scl = i + 1
|
| 224 |
+
pol = [0] * i + [1]
|
| 225 |
+
tgt = [i] + [0] * i + [1 / scl]
|
| 226 |
+
hermpol = herm.poly2herm(pol)
|
| 227 |
+
hermint = herm.hermint(hermpol, m=1, k=[i])
|
| 228 |
+
res = herm.herm2poly(hermint)
|
| 229 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 230 |
+
|
| 231 |
+
# check single integration with integration constant and lbnd
|
| 232 |
+
for i in range(5):
|
| 233 |
+
scl = i + 1
|
| 234 |
+
pol = [0] * i + [1]
|
| 235 |
+
hermpol = herm.poly2herm(pol)
|
| 236 |
+
hermint = herm.hermint(hermpol, m=1, k=[i], lbnd=-1)
|
| 237 |
+
assert_almost_equal(herm.hermval(-1, hermint), i)
|
| 238 |
+
|
| 239 |
+
# check single integration with integration constant and scaling
|
| 240 |
+
for i in range(5):
|
| 241 |
+
scl = i + 1
|
| 242 |
+
pol = [0] * i + [1]
|
| 243 |
+
tgt = [i] + [0] * i + [2 / scl]
|
| 244 |
+
hermpol = herm.poly2herm(pol)
|
| 245 |
+
hermint = herm.hermint(hermpol, m=1, k=[i], scl=2)
|
| 246 |
+
res = herm.herm2poly(hermint)
|
| 247 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 248 |
+
|
| 249 |
+
# check multiple integrations with default k
|
| 250 |
+
for i in range(5):
|
| 251 |
+
for j in range(2, 5):
|
| 252 |
+
pol = [0] * i + [1]
|
| 253 |
+
tgt = pol[:]
|
| 254 |
+
for k in range(j):
|
| 255 |
+
tgt = herm.hermint(tgt, m=1)
|
| 256 |
+
res = herm.hermint(pol, m=j)
|
| 257 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 258 |
+
|
| 259 |
+
# check multiple integrations with defined k
|
| 260 |
+
for i in range(5):
|
| 261 |
+
for j in range(2, 5):
|
| 262 |
+
pol = [0] * i + [1]
|
| 263 |
+
tgt = pol[:]
|
| 264 |
+
for k in range(j):
|
| 265 |
+
tgt = herm.hermint(tgt, m=1, k=[k])
|
| 266 |
+
res = herm.hermint(pol, m=j, k=list(range(j)))
|
| 267 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 268 |
+
|
| 269 |
+
# check multiple integrations with lbnd
|
| 270 |
+
for i in range(5):
|
| 271 |
+
for j in range(2, 5):
|
| 272 |
+
pol = [0] * i + [1]
|
| 273 |
+
tgt = pol[:]
|
| 274 |
+
for k in range(j):
|
| 275 |
+
tgt = herm.hermint(tgt, m=1, k=[k], lbnd=-1)
|
| 276 |
+
res = herm.hermint(pol, m=j, k=list(range(j)), lbnd=-1)
|
| 277 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 278 |
+
|
| 279 |
+
# check multiple integrations with scaling
|
| 280 |
+
for i in range(5):
|
| 281 |
+
for j in range(2, 5):
|
| 282 |
+
pol = [0] * i + [1]
|
| 283 |
+
tgt = pol[:]
|
| 284 |
+
for k in range(j):
|
| 285 |
+
tgt = herm.hermint(tgt, m=1, k=[k], scl=2)
|
| 286 |
+
res = herm.hermint(pol, m=j, k=list(range(j)), scl=2)
|
| 287 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 288 |
+
|
| 289 |
+
def test_hermint_axis(self):
|
| 290 |
+
# check that axis keyword works
|
| 291 |
+
c2d = np.random.random((3, 4))
|
| 292 |
+
|
| 293 |
+
tgt = np.vstack([herm.hermint(c) for c in c2d.T]).T
|
| 294 |
+
res = herm.hermint(c2d, axis=0)
|
| 295 |
+
assert_almost_equal(res, tgt)
|
| 296 |
+
|
| 297 |
+
tgt = np.vstack([herm.hermint(c) for c in c2d])
|
| 298 |
+
res = herm.hermint(c2d, axis=1)
|
| 299 |
+
assert_almost_equal(res, tgt)
|
| 300 |
+
|
| 301 |
+
tgt = np.vstack([herm.hermint(c, k=3) for c in c2d])
|
| 302 |
+
res = herm.hermint(c2d, k=3, axis=1)
|
| 303 |
+
assert_almost_equal(res, tgt)
|
| 304 |
+
|
| 305 |
+
|
| 306 |
+
class TestDerivative:
|
| 307 |
+
|
| 308 |
+
def test_hermder(self):
|
| 309 |
+
# check exceptions
|
| 310 |
+
assert_raises(TypeError, herm.hermder, [0], .5)
|
| 311 |
+
assert_raises(ValueError, herm.hermder, [0], -1)
|
| 312 |
+
|
| 313 |
+
# check that zeroth derivative does nothing
|
| 314 |
+
for i in range(5):
|
| 315 |
+
tgt = [0] * i + [1]
|
| 316 |
+
res = herm.hermder(tgt, m=0)
|
| 317 |
+
assert_equal(trim(res), trim(tgt))
|
| 318 |
+
|
| 319 |
+
# check that derivation is the inverse of integration
|
| 320 |
+
for i in range(5):
|
| 321 |
+
for j in range(2, 5):
|
| 322 |
+
tgt = [0] * i + [1]
|
| 323 |
+
res = herm.hermder(herm.hermint(tgt, m=j), m=j)
|
| 324 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 325 |
+
|
| 326 |
+
# check derivation with scaling
|
| 327 |
+
for i in range(5):
|
| 328 |
+
for j in range(2, 5):
|
| 329 |
+
tgt = [0] * i + [1]
|
| 330 |
+
res = herm.hermder(herm.hermint(tgt, m=j, scl=2), m=j, scl=.5)
|
| 331 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 332 |
+
|
| 333 |
+
def test_hermder_axis(self):
|
| 334 |
+
# check that axis keyword works
|
| 335 |
+
c2d = np.random.random((3, 4))
|
| 336 |
+
|
| 337 |
+
tgt = np.vstack([herm.hermder(c) for c in c2d.T]).T
|
| 338 |
+
res = herm.hermder(c2d, axis=0)
|
| 339 |
+
assert_almost_equal(res, tgt)
|
| 340 |
+
|
| 341 |
+
tgt = np.vstack([herm.hermder(c) for c in c2d])
|
| 342 |
+
res = herm.hermder(c2d, axis=1)
|
| 343 |
+
assert_almost_equal(res, tgt)
|
| 344 |
+
|
| 345 |
+
|
| 346 |
+
class TestVander:
|
| 347 |
+
# some random values in [-1, 1)
|
| 348 |
+
x = np.random.random((3, 5)) * 2 - 1
|
| 349 |
+
|
| 350 |
+
def test_hermvander(self):
|
| 351 |
+
# check for 1d x
|
| 352 |
+
x = np.arange(3)
|
| 353 |
+
v = herm.hermvander(x, 3)
|
| 354 |
+
assert_(v.shape == (3, 4))
|
| 355 |
+
for i in range(4):
|
| 356 |
+
coef = [0] * i + [1]
|
| 357 |
+
assert_almost_equal(v[..., i], herm.hermval(x, coef))
|
| 358 |
+
|
| 359 |
+
# check for 2d x
|
| 360 |
+
x = np.array([[1, 2], [3, 4], [5, 6]])
|
| 361 |
+
v = herm.hermvander(x, 3)
|
| 362 |
+
assert_(v.shape == (3, 2, 4))
|
| 363 |
+
for i in range(4):
|
| 364 |
+
coef = [0] * i + [1]
|
| 365 |
+
assert_almost_equal(v[..., i], herm.hermval(x, coef))
|
| 366 |
+
|
| 367 |
+
def test_hermvander2d(self):
|
| 368 |
+
# also tests hermval2d for non-square coefficient array
|
| 369 |
+
x1, x2, x3 = self.x
|
| 370 |
+
c = np.random.random((2, 3))
|
| 371 |
+
van = herm.hermvander2d(x1, x2, [1, 2])
|
| 372 |
+
tgt = herm.hermval2d(x1, x2, c)
|
| 373 |
+
res = np.dot(van, c.flat)
|
| 374 |
+
assert_almost_equal(res, tgt)
|
| 375 |
+
|
| 376 |
+
# check shape
|
| 377 |
+
van = herm.hermvander2d([x1], [x2], [1, 2])
|
| 378 |
+
assert_(van.shape == (1, 5, 6))
|
| 379 |
+
|
| 380 |
+
def test_hermvander3d(self):
|
| 381 |
+
# also tests hermval3d for non-square coefficient array
|
| 382 |
+
x1, x2, x3 = self.x
|
| 383 |
+
c = np.random.random((2, 3, 4))
|
| 384 |
+
van = herm.hermvander3d(x1, x2, x3, [1, 2, 3])
|
| 385 |
+
tgt = herm.hermval3d(x1, x2, x3, c)
|
| 386 |
+
res = np.dot(van, c.flat)
|
| 387 |
+
assert_almost_equal(res, tgt)
|
| 388 |
+
|
| 389 |
+
# check shape
|
| 390 |
+
van = herm.hermvander3d([x1], [x2], [x3], [1, 2, 3])
|
| 391 |
+
assert_(van.shape == (1, 5, 24))
|
| 392 |
+
|
| 393 |
+
|
| 394 |
+
class TestFitting:
|
| 395 |
+
|
| 396 |
+
def test_hermfit(self):
|
| 397 |
+
def f(x):
|
| 398 |
+
return x * (x - 1) * (x - 2)
|
| 399 |
+
|
| 400 |
+
def f2(x):
|
| 401 |
+
return x**4 + x**2 + 1
|
| 402 |
+
|
| 403 |
+
# Test exceptions
|
| 404 |
+
assert_raises(ValueError, herm.hermfit, [1], [1], -1)
|
| 405 |
+
assert_raises(TypeError, herm.hermfit, [[1]], [1], 0)
|
| 406 |
+
assert_raises(TypeError, herm.hermfit, [], [1], 0)
|
| 407 |
+
assert_raises(TypeError, herm.hermfit, [1], [[[1]]], 0)
|
| 408 |
+
assert_raises(TypeError, herm.hermfit, [1, 2], [1], 0)
|
| 409 |
+
assert_raises(TypeError, herm.hermfit, [1], [1, 2], 0)
|
| 410 |
+
assert_raises(TypeError, herm.hermfit, [1], [1], 0, w=[[1]])
|
| 411 |
+
assert_raises(TypeError, herm.hermfit, [1], [1], 0, w=[1, 1])
|
| 412 |
+
assert_raises(ValueError, herm.hermfit, [1], [1], [-1,])
|
| 413 |
+
assert_raises(ValueError, herm.hermfit, [1], [1], [2, -1, 6])
|
| 414 |
+
assert_raises(TypeError, herm.hermfit, [1], [1], [])
|
| 415 |
+
|
| 416 |
+
# Test fit
|
| 417 |
+
x = np.linspace(0, 2)
|
| 418 |
+
y = f(x)
|
| 419 |
+
#
|
| 420 |
+
coef3 = herm.hermfit(x, y, 3)
|
| 421 |
+
assert_equal(len(coef3), 4)
|
| 422 |
+
assert_almost_equal(herm.hermval(x, coef3), y)
|
| 423 |
+
coef3 = herm.hermfit(x, y, [0, 1, 2, 3])
|
| 424 |
+
assert_equal(len(coef3), 4)
|
| 425 |
+
assert_almost_equal(herm.hermval(x, coef3), y)
|
| 426 |
+
#
|
| 427 |
+
coef4 = herm.hermfit(x, y, 4)
|
| 428 |
+
assert_equal(len(coef4), 5)
|
| 429 |
+
assert_almost_equal(herm.hermval(x, coef4), y)
|
| 430 |
+
coef4 = herm.hermfit(x, y, [0, 1, 2, 3, 4])
|
| 431 |
+
assert_equal(len(coef4), 5)
|
| 432 |
+
assert_almost_equal(herm.hermval(x, coef4), y)
|
| 433 |
+
# check things still work if deg is not in strict increasing
|
| 434 |
+
coef4 = herm.hermfit(x, y, [2, 3, 4, 1, 0])
|
| 435 |
+
assert_equal(len(coef4), 5)
|
| 436 |
+
assert_almost_equal(herm.hermval(x, coef4), y)
|
| 437 |
+
#
|
| 438 |
+
coef2d = herm.hermfit(x, np.array([y, y]).T, 3)
|
| 439 |
+
assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
|
| 440 |
+
coef2d = herm.hermfit(x, np.array([y, y]).T, [0, 1, 2, 3])
|
| 441 |
+
assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
|
| 442 |
+
# test weighting
|
| 443 |
+
w = np.zeros_like(x)
|
| 444 |
+
yw = y.copy()
|
| 445 |
+
w[1::2] = 1
|
| 446 |
+
y[0::2] = 0
|
| 447 |
+
wcoef3 = herm.hermfit(x, yw, 3, w=w)
|
| 448 |
+
assert_almost_equal(wcoef3, coef3)
|
| 449 |
+
wcoef3 = herm.hermfit(x, yw, [0, 1, 2, 3], w=w)
|
| 450 |
+
assert_almost_equal(wcoef3, coef3)
|
| 451 |
+
#
|
| 452 |
+
wcoef2d = herm.hermfit(x, np.array([yw, yw]).T, 3, w=w)
|
| 453 |
+
assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
|
| 454 |
+
wcoef2d = herm.hermfit(x, np.array([yw, yw]).T, [0, 1, 2, 3], w=w)
|
| 455 |
+
assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
|
| 456 |
+
# test scaling with complex values x points whose square
|
| 457 |
+
# is zero when summed.
|
| 458 |
+
x = [1, 1j, -1, -1j]
|
| 459 |
+
assert_almost_equal(herm.hermfit(x, x, 1), [0, .5])
|
| 460 |
+
assert_almost_equal(herm.hermfit(x, x, [0, 1]), [0, .5])
|
| 461 |
+
# test fitting only even Legendre polynomials
|
| 462 |
+
x = np.linspace(-1, 1)
|
| 463 |
+
y = f2(x)
|
| 464 |
+
coef1 = herm.hermfit(x, y, 4)
|
| 465 |
+
assert_almost_equal(herm.hermval(x, coef1), y)
|
| 466 |
+
coef2 = herm.hermfit(x, y, [0, 2, 4])
|
| 467 |
+
assert_almost_equal(herm.hermval(x, coef2), y)
|
| 468 |
+
assert_almost_equal(coef1, coef2)
|
| 469 |
+
|
| 470 |
+
|
| 471 |
+
class TestCompanion:
|
| 472 |
+
|
| 473 |
+
def test_raises(self):
|
| 474 |
+
assert_raises(ValueError, herm.hermcompanion, [])
|
| 475 |
+
assert_raises(ValueError, herm.hermcompanion, [1])
|
| 476 |
+
|
| 477 |
+
def test_dimensions(self):
|
| 478 |
+
for i in range(1, 5):
|
| 479 |
+
coef = [0] * i + [1]
|
| 480 |
+
assert_(herm.hermcompanion(coef).shape == (i, i))
|
| 481 |
+
|
| 482 |
+
def test_linear_root(self):
|
| 483 |
+
assert_(herm.hermcompanion([1, 2])[0, 0] == -.25)
|
| 484 |
+
|
| 485 |
+
|
| 486 |
+
class TestGauss:
|
| 487 |
+
|
| 488 |
+
def test_100(self):
|
| 489 |
+
x, w = herm.hermgauss(100)
|
| 490 |
+
|
| 491 |
+
# test orthogonality. Note that the results need to be normalized,
|
| 492 |
+
# otherwise the huge values that can arise from fast growing
|
| 493 |
+
# functions like Laguerre can be very confusing.
|
| 494 |
+
v = herm.hermvander(x, 99)
|
| 495 |
+
vv = np.dot(v.T * w, v)
|
| 496 |
+
vd = 1 / np.sqrt(vv.diagonal())
|
| 497 |
+
vv = vd[:, None] * vv * vd
|
| 498 |
+
assert_almost_equal(vv, np.eye(100))
|
| 499 |
+
|
| 500 |
+
# check that the integral of 1 is correct
|
| 501 |
+
tgt = np.sqrt(np.pi)
|
| 502 |
+
assert_almost_equal(w.sum(), tgt)
|
| 503 |
+
|
| 504 |
+
|
| 505 |
+
class TestMisc:
|
| 506 |
+
|
| 507 |
+
def test_hermfromroots(self):
|
| 508 |
+
res = herm.hermfromroots([])
|
| 509 |
+
assert_almost_equal(trim(res), [1])
|
| 510 |
+
for i in range(1, 5):
|
| 511 |
+
roots = np.cos(np.linspace(-np.pi, 0, 2 * i + 1)[1::2])
|
| 512 |
+
pol = herm.hermfromroots(roots)
|
| 513 |
+
res = herm.hermval(roots, pol)
|
| 514 |
+
tgt = 0
|
| 515 |
+
assert_(len(pol) == i + 1)
|
| 516 |
+
assert_almost_equal(herm.herm2poly(pol)[-1], 1)
|
| 517 |
+
assert_almost_equal(res, tgt)
|
| 518 |
+
|
| 519 |
+
def test_hermroots(self):
|
| 520 |
+
assert_almost_equal(herm.hermroots([1]), [])
|
| 521 |
+
assert_almost_equal(herm.hermroots([1, 1]), [-.5])
|
| 522 |
+
for i in range(2, 5):
|
| 523 |
+
tgt = np.linspace(-1, 1, i)
|
| 524 |
+
res = herm.hermroots(herm.hermfromroots(tgt))
|
| 525 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 526 |
+
|
| 527 |
+
def test_hermtrim(self):
|
| 528 |
+
coef = [2, -1, 1, 0]
|
| 529 |
+
|
| 530 |
+
# Test exceptions
|
| 531 |
+
assert_raises(ValueError, herm.hermtrim, coef, -1)
|
| 532 |
+
|
| 533 |
+
# Test results
|
| 534 |
+
assert_equal(herm.hermtrim(coef), coef[:-1])
|
| 535 |
+
assert_equal(herm.hermtrim(coef, 1), coef[:-3])
|
| 536 |
+
assert_equal(herm.hermtrim(coef, 2), [0])
|
| 537 |
+
|
| 538 |
+
def test_hermline(self):
|
| 539 |
+
assert_equal(herm.hermline(3, 4), [3, 2])
|
| 540 |
+
|
| 541 |
+
def test_herm2poly(self):
|
| 542 |
+
for i in range(10):
|
| 543 |
+
assert_almost_equal(herm.herm2poly([0] * i + [1]), Hlist[i])
|
| 544 |
+
|
| 545 |
+
def test_poly2herm(self):
|
| 546 |
+
for i in range(10):
|
| 547 |
+
assert_almost_equal(herm.poly2herm(Hlist[i]), [0] * i + [1])
|
| 548 |
+
|
| 549 |
+
def test_weight(self):
|
| 550 |
+
x = np.linspace(-5, 5, 11)
|
| 551 |
+
tgt = np.exp(-x**2)
|
| 552 |
+
res = herm.hermweight(x)
|
| 553 |
+
assert_almost_equal(res, tgt)
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/tests/test_hermite_e.py
ADDED
|
@@ -0,0 +1,554 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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numpy as np
|
| 7 |
+
import numpy.polynomial.hermite_e as herme
|
| 8 |
+
from numpy.polynomial.polynomial import polyval
|
| 9 |
+
from numpy.testing import assert_, assert_almost_equal, assert_equal, assert_raises
|
| 10 |
+
|
| 11 |
+
He0 = np.array([1])
|
| 12 |
+
He1 = np.array([0, 1])
|
| 13 |
+
He2 = np.array([-1, 0, 1])
|
| 14 |
+
He3 = np.array([0, -3, 0, 1])
|
| 15 |
+
He4 = np.array([3, 0, -6, 0, 1])
|
| 16 |
+
He5 = np.array([0, 15, 0, -10, 0, 1])
|
| 17 |
+
He6 = np.array([-15, 0, 45, 0, -15, 0, 1])
|
| 18 |
+
He7 = np.array([0, -105, 0, 105, 0, -21, 0, 1])
|
| 19 |
+
He8 = np.array([105, 0, -420, 0, 210, 0, -28, 0, 1])
|
| 20 |
+
He9 = np.array([0, 945, 0, -1260, 0, 378, 0, -36, 0, 1])
|
| 21 |
+
|
| 22 |
+
Helist = [He0, He1, He2, He3, He4, He5, He6, He7, He8, He9]
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def trim(x):
|
| 26 |
+
return herme.hermetrim(x, tol=1e-6)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
class TestConstants:
|
| 30 |
+
|
| 31 |
+
def test_hermedomain(self):
|
| 32 |
+
assert_equal(herme.hermedomain, [-1, 1])
|
| 33 |
+
|
| 34 |
+
def test_hermezero(self):
|
| 35 |
+
assert_equal(herme.hermezero, [0])
|
| 36 |
+
|
| 37 |
+
def test_hermeone(self):
|
| 38 |
+
assert_equal(herme.hermeone, [1])
|
| 39 |
+
|
| 40 |
+
def test_hermex(self):
|
| 41 |
+
assert_equal(herme.hermex, [0, 1])
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
class TestArithmetic:
|
| 45 |
+
x = np.linspace(-3, 3, 100)
|
| 46 |
+
|
| 47 |
+
def test_hermeadd(self):
|
| 48 |
+
for i in range(5):
|
| 49 |
+
for j in range(5):
|
| 50 |
+
msg = f"At i={i}, j={j}"
|
| 51 |
+
tgt = np.zeros(max(i, j) + 1)
|
| 52 |
+
tgt[i] += 1
|
| 53 |
+
tgt[j] += 1
|
| 54 |
+
res = herme.hermeadd([0] * i + [1], [0] * j + [1])
|
| 55 |
+
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
| 56 |
+
|
| 57 |
+
def test_hermesub(self):
|
| 58 |
+
for i in range(5):
|
| 59 |
+
for j in range(5):
|
| 60 |
+
msg = f"At i={i}, j={j}"
|
| 61 |
+
tgt = np.zeros(max(i, j) + 1)
|
| 62 |
+
tgt[i] += 1
|
| 63 |
+
tgt[j] -= 1
|
| 64 |
+
res = herme.hermesub([0] * i + [1], [0] * j + [1])
|
| 65 |
+
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
| 66 |
+
|
| 67 |
+
def test_hermemulx(self):
|
| 68 |
+
assert_equal(herme.hermemulx([0]), [0])
|
| 69 |
+
assert_equal(herme.hermemulx([1]), [0, 1])
|
| 70 |
+
for i in range(1, 5):
|
| 71 |
+
ser = [0] * i + [1]
|
| 72 |
+
tgt = [0] * (i - 1) + [i, 0, 1]
|
| 73 |
+
assert_equal(herme.hermemulx(ser), tgt)
|
| 74 |
+
|
| 75 |
+
def test_hermemul(self):
|
| 76 |
+
# check values of result
|
| 77 |
+
for i in range(5):
|
| 78 |
+
pol1 = [0] * i + [1]
|
| 79 |
+
val1 = herme.hermeval(self.x, pol1)
|
| 80 |
+
for j in range(5):
|
| 81 |
+
msg = f"At i={i}, j={j}"
|
| 82 |
+
pol2 = [0] * j + [1]
|
| 83 |
+
val2 = herme.hermeval(self.x, pol2)
|
| 84 |
+
pol3 = herme.hermemul(pol1, pol2)
|
| 85 |
+
val3 = herme.hermeval(self.x, pol3)
|
| 86 |
+
assert_(len(pol3) == i + j + 1, msg)
|
| 87 |
+
assert_almost_equal(val3, val1 * val2, err_msg=msg)
|
| 88 |
+
|
| 89 |
+
def test_hermediv(self):
|
| 90 |
+
for i in range(5):
|
| 91 |
+
for j in range(5):
|
| 92 |
+
msg = f"At i={i}, j={j}"
|
| 93 |
+
ci = [0] * i + [1]
|
| 94 |
+
cj = [0] * j + [1]
|
| 95 |
+
tgt = herme.hermeadd(ci, cj)
|
| 96 |
+
quo, rem = herme.hermediv(tgt, ci)
|
| 97 |
+
res = herme.hermeadd(herme.hermemul(quo, ci), rem)
|
| 98 |
+
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
| 99 |
+
|
| 100 |
+
def test_hermepow(self):
|
| 101 |
+
for i in range(5):
|
| 102 |
+
for j in range(5):
|
| 103 |
+
msg = f"At i={i}, j={j}"
|
| 104 |
+
c = np.arange(i + 1)
|
| 105 |
+
tgt = reduce(herme.hermemul, [c] * j, np.array([1]))
|
| 106 |
+
res = herme.hermepow(c, j)
|
| 107 |
+
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
class TestEvaluation:
|
| 111 |
+
# coefficients of 1 + 2*x + 3*x**2
|
| 112 |
+
c1d = np.array([4., 2., 3.])
|
| 113 |
+
c2d = np.einsum('i,j->ij', c1d, c1d)
|
| 114 |
+
c3d = np.einsum('i,j,k->ijk', c1d, c1d, c1d)
|
| 115 |
+
|
| 116 |
+
# some random values in [-1, 1)
|
| 117 |
+
x = np.random.random((3, 5)) * 2 - 1
|
| 118 |
+
y = polyval(x, [1., 2., 3.])
|
| 119 |
+
|
| 120 |
+
def test_hermeval(self):
|
| 121 |
+
# check empty input
|
| 122 |
+
assert_equal(herme.hermeval([], [1]).size, 0)
|
| 123 |
+
|
| 124 |
+
# check normal input)
|
| 125 |
+
x = np.linspace(-1, 1)
|
| 126 |
+
y = [polyval(x, c) for c in Helist]
|
| 127 |
+
for i in range(10):
|
| 128 |
+
msg = f"At i={i}"
|
| 129 |
+
tgt = y[i]
|
| 130 |
+
res = herme.hermeval(x, [0] * i + [1])
|
| 131 |
+
assert_almost_equal(res, tgt, err_msg=msg)
|
| 132 |
+
|
| 133 |
+
# check that shape is preserved
|
| 134 |
+
for i in range(3):
|
| 135 |
+
dims = [2] * i
|
| 136 |
+
x = np.zeros(dims)
|
| 137 |
+
assert_equal(herme.hermeval(x, [1]).shape, dims)
|
| 138 |
+
assert_equal(herme.hermeval(x, [1, 0]).shape, dims)
|
| 139 |
+
assert_equal(herme.hermeval(x, [1, 0, 0]).shape, dims)
|
| 140 |
+
|
| 141 |
+
def test_hermeval2d(self):
|
| 142 |
+
x1, x2, x3 = self.x
|
| 143 |
+
y1, y2, y3 = self.y
|
| 144 |
+
|
| 145 |
+
# test exceptions
|
| 146 |
+
assert_raises(ValueError, herme.hermeval2d, x1, x2[:2], self.c2d)
|
| 147 |
+
|
| 148 |
+
# test values
|
| 149 |
+
tgt = y1 * y2
|
| 150 |
+
res = herme.hermeval2d(x1, x2, self.c2d)
|
| 151 |
+
assert_almost_equal(res, tgt)
|
| 152 |
+
|
| 153 |
+
# test shape
|
| 154 |
+
z = np.ones((2, 3))
|
| 155 |
+
res = herme.hermeval2d(z, z, self.c2d)
|
| 156 |
+
assert_(res.shape == (2, 3))
|
| 157 |
+
|
| 158 |
+
def test_hermeval3d(self):
|
| 159 |
+
x1, x2, x3 = self.x
|
| 160 |
+
y1, y2, y3 = self.y
|
| 161 |
+
|
| 162 |
+
# test exceptions
|
| 163 |
+
assert_raises(ValueError, herme.hermeval3d, x1, x2, x3[:2], self.c3d)
|
| 164 |
+
|
| 165 |
+
# test values
|
| 166 |
+
tgt = y1 * y2 * y3
|
| 167 |
+
res = herme.hermeval3d(x1, x2, x3, self.c3d)
|
| 168 |
+
assert_almost_equal(res, tgt)
|
| 169 |
+
|
| 170 |
+
# test shape
|
| 171 |
+
z = np.ones((2, 3))
|
| 172 |
+
res = herme.hermeval3d(z, z, z, self.c3d)
|
| 173 |
+
assert_(res.shape == (2, 3))
|
| 174 |
+
|
| 175 |
+
def test_hermegrid2d(self):
|
| 176 |
+
x1, x2, x3 = self.x
|
| 177 |
+
y1, y2, y3 = self.y
|
| 178 |
+
|
| 179 |
+
# test values
|
| 180 |
+
tgt = np.einsum('i,j->ij', y1, y2)
|
| 181 |
+
res = herme.hermegrid2d(x1, x2, self.c2d)
|
| 182 |
+
assert_almost_equal(res, tgt)
|
| 183 |
+
|
| 184 |
+
# test shape
|
| 185 |
+
z = np.ones((2, 3))
|
| 186 |
+
res = herme.hermegrid2d(z, z, self.c2d)
|
| 187 |
+
assert_(res.shape == (2, 3) * 2)
|
| 188 |
+
|
| 189 |
+
def test_hermegrid3d(self):
|
| 190 |
+
x1, x2, x3 = self.x
|
| 191 |
+
y1, y2, y3 = self.y
|
| 192 |
+
|
| 193 |
+
# test values
|
| 194 |
+
tgt = np.einsum('i,j,k->ijk', y1, y2, y3)
|
| 195 |
+
res = herme.hermegrid3d(x1, x2, x3, self.c3d)
|
| 196 |
+
assert_almost_equal(res, tgt)
|
| 197 |
+
|
| 198 |
+
# test shape
|
| 199 |
+
z = np.ones((2, 3))
|
| 200 |
+
res = herme.hermegrid3d(z, z, z, self.c3d)
|
| 201 |
+
assert_(res.shape == (2, 3) * 3)
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
class TestIntegral:
|
| 205 |
+
|
| 206 |
+
def test_hermeint(self):
|
| 207 |
+
# check exceptions
|
| 208 |
+
assert_raises(TypeError, herme.hermeint, [0], .5)
|
| 209 |
+
assert_raises(ValueError, herme.hermeint, [0], -1)
|
| 210 |
+
assert_raises(ValueError, herme.hermeint, [0], 1, [0, 0])
|
| 211 |
+
assert_raises(ValueError, herme.hermeint, [0], lbnd=[0])
|
| 212 |
+
assert_raises(ValueError, herme.hermeint, [0], scl=[0])
|
| 213 |
+
assert_raises(TypeError, herme.hermeint, [0], axis=.5)
|
| 214 |
+
|
| 215 |
+
# test integration of zero polynomial
|
| 216 |
+
for i in range(2, 5):
|
| 217 |
+
k = [0] * (i - 2) + [1]
|
| 218 |
+
res = herme.hermeint([0], m=i, k=k)
|
| 219 |
+
assert_almost_equal(res, [0, 1])
|
| 220 |
+
|
| 221 |
+
# check single integration with integration constant
|
| 222 |
+
for i in range(5):
|
| 223 |
+
scl = i + 1
|
| 224 |
+
pol = [0] * i + [1]
|
| 225 |
+
tgt = [i] + [0] * i + [1 / scl]
|
| 226 |
+
hermepol = herme.poly2herme(pol)
|
| 227 |
+
hermeint = herme.hermeint(hermepol, m=1, k=[i])
|
| 228 |
+
res = herme.herme2poly(hermeint)
|
| 229 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 230 |
+
|
| 231 |
+
# check single integration with integration constant and lbnd
|
| 232 |
+
for i in range(5):
|
| 233 |
+
scl = i + 1
|
| 234 |
+
pol = [0] * i + [1]
|
| 235 |
+
hermepol = herme.poly2herme(pol)
|
| 236 |
+
hermeint = herme.hermeint(hermepol, m=1, k=[i], lbnd=-1)
|
| 237 |
+
assert_almost_equal(herme.hermeval(-1, hermeint), i)
|
| 238 |
+
|
| 239 |
+
# check single integration with integration constant and scaling
|
| 240 |
+
for i in range(5):
|
| 241 |
+
scl = i + 1
|
| 242 |
+
pol = [0] * i + [1]
|
| 243 |
+
tgt = [i] + [0] * i + [2 / scl]
|
| 244 |
+
hermepol = herme.poly2herme(pol)
|
| 245 |
+
hermeint = herme.hermeint(hermepol, m=1, k=[i], scl=2)
|
| 246 |
+
res = herme.herme2poly(hermeint)
|
| 247 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 248 |
+
|
| 249 |
+
# check multiple integrations with default k
|
| 250 |
+
for i in range(5):
|
| 251 |
+
for j in range(2, 5):
|
| 252 |
+
pol = [0] * i + [1]
|
| 253 |
+
tgt = pol[:]
|
| 254 |
+
for k in range(j):
|
| 255 |
+
tgt = herme.hermeint(tgt, m=1)
|
| 256 |
+
res = herme.hermeint(pol, m=j)
|
| 257 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 258 |
+
|
| 259 |
+
# check multiple integrations with defined k
|
| 260 |
+
for i in range(5):
|
| 261 |
+
for j in range(2, 5):
|
| 262 |
+
pol = [0] * i + [1]
|
| 263 |
+
tgt = pol[:]
|
| 264 |
+
for k in range(j):
|
| 265 |
+
tgt = herme.hermeint(tgt, m=1, k=[k])
|
| 266 |
+
res = herme.hermeint(pol, m=j, k=list(range(j)))
|
| 267 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 268 |
+
|
| 269 |
+
# check multiple integrations with lbnd
|
| 270 |
+
for i in range(5):
|
| 271 |
+
for j in range(2, 5):
|
| 272 |
+
pol = [0] * i + [1]
|
| 273 |
+
tgt = pol[:]
|
| 274 |
+
for k in range(j):
|
| 275 |
+
tgt = herme.hermeint(tgt, m=1, k=[k], lbnd=-1)
|
| 276 |
+
res = herme.hermeint(pol, m=j, k=list(range(j)), lbnd=-1)
|
| 277 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 278 |
+
|
| 279 |
+
# check multiple integrations with scaling
|
| 280 |
+
for i in range(5):
|
| 281 |
+
for j in range(2, 5):
|
| 282 |
+
pol = [0] * i + [1]
|
| 283 |
+
tgt = pol[:]
|
| 284 |
+
for k in range(j):
|
| 285 |
+
tgt = herme.hermeint(tgt, m=1, k=[k], scl=2)
|
| 286 |
+
res = herme.hermeint(pol, m=j, k=list(range(j)), scl=2)
|
| 287 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 288 |
+
|
| 289 |
+
def test_hermeint_axis(self):
|
| 290 |
+
# check that axis keyword works
|
| 291 |
+
c2d = np.random.random((3, 4))
|
| 292 |
+
|
| 293 |
+
tgt = np.vstack([herme.hermeint(c) for c in c2d.T]).T
|
| 294 |
+
res = herme.hermeint(c2d, axis=0)
|
| 295 |
+
assert_almost_equal(res, tgt)
|
| 296 |
+
|
| 297 |
+
tgt = np.vstack([herme.hermeint(c) for c in c2d])
|
| 298 |
+
res = herme.hermeint(c2d, axis=1)
|
| 299 |
+
assert_almost_equal(res, tgt)
|
| 300 |
+
|
| 301 |
+
tgt = np.vstack([herme.hermeint(c, k=3) for c in c2d])
|
| 302 |
+
res = herme.hermeint(c2d, k=3, axis=1)
|
| 303 |
+
assert_almost_equal(res, tgt)
|
| 304 |
+
|
| 305 |
+
|
| 306 |
+
class TestDerivative:
|
| 307 |
+
|
| 308 |
+
def test_hermeder(self):
|
| 309 |
+
# check exceptions
|
| 310 |
+
assert_raises(TypeError, herme.hermeder, [0], .5)
|
| 311 |
+
assert_raises(ValueError, herme.hermeder, [0], -1)
|
| 312 |
+
|
| 313 |
+
# check that zeroth derivative does nothing
|
| 314 |
+
for i in range(5):
|
| 315 |
+
tgt = [0] * i + [1]
|
| 316 |
+
res = herme.hermeder(tgt, m=0)
|
| 317 |
+
assert_equal(trim(res), trim(tgt))
|
| 318 |
+
|
| 319 |
+
# check that derivation is the inverse of integration
|
| 320 |
+
for i in range(5):
|
| 321 |
+
for j in range(2, 5):
|
| 322 |
+
tgt = [0] * i + [1]
|
| 323 |
+
res = herme.hermeder(herme.hermeint(tgt, m=j), m=j)
|
| 324 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 325 |
+
|
| 326 |
+
# check derivation with scaling
|
| 327 |
+
for i in range(5):
|
| 328 |
+
for j in range(2, 5):
|
| 329 |
+
tgt = [0] * i + [1]
|
| 330 |
+
res = herme.hermeder(
|
| 331 |
+
herme.hermeint(tgt, m=j, scl=2), m=j, scl=.5)
|
| 332 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 333 |
+
|
| 334 |
+
def test_hermeder_axis(self):
|
| 335 |
+
# check that axis keyword works
|
| 336 |
+
c2d = np.random.random((3, 4))
|
| 337 |
+
|
| 338 |
+
tgt = np.vstack([herme.hermeder(c) for c in c2d.T]).T
|
| 339 |
+
res = herme.hermeder(c2d, axis=0)
|
| 340 |
+
assert_almost_equal(res, tgt)
|
| 341 |
+
|
| 342 |
+
tgt = np.vstack([herme.hermeder(c) for c in c2d])
|
| 343 |
+
res = herme.hermeder(c2d, axis=1)
|
| 344 |
+
assert_almost_equal(res, tgt)
|
| 345 |
+
|
| 346 |
+
|
| 347 |
+
class TestVander:
|
| 348 |
+
# some random values in [-1, 1)
|
| 349 |
+
x = np.random.random((3, 5)) * 2 - 1
|
| 350 |
+
|
| 351 |
+
def test_hermevander(self):
|
| 352 |
+
# check for 1d x
|
| 353 |
+
x = np.arange(3)
|
| 354 |
+
v = herme.hermevander(x, 3)
|
| 355 |
+
assert_(v.shape == (3, 4))
|
| 356 |
+
for i in range(4):
|
| 357 |
+
coef = [0] * i + [1]
|
| 358 |
+
assert_almost_equal(v[..., i], herme.hermeval(x, coef))
|
| 359 |
+
|
| 360 |
+
# check for 2d x
|
| 361 |
+
x = np.array([[1, 2], [3, 4], [5, 6]])
|
| 362 |
+
v = herme.hermevander(x, 3)
|
| 363 |
+
assert_(v.shape == (3, 2, 4))
|
| 364 |
+
for i in range(4):
|
| 365 |
+
coef = [0] * i + [1]
|
| 366 |
+
assert_almost_equal(v[..., i], herme.hermeval(x, coef))
|
| 367 |
+
|
| 368 |
+
def test_hermevander2d(self):
|
| 369 |
+
# also tests hermeval2d for non-square coefficient array
|
| 370 |
+
x1, x2, x3 = self.x
|
| 371 |
+
c = np.random.random((2, 3))
|
| 372 |
+
van = herme.hermevander2d(x1, x2, [1, 2])
|
| 373 |
+
tgt = herme.hermeval2d(x1, x2, c)
|
| 374 |
+
res = np.dot(van, c.flat)
|
| 375 |
+
assert_almost_equal(res, tgt)
|
| 376 |
+
|
| 377 |
+
# check shape
|
| 378 |
+
van = herme.hermevander2d([x1], [x2], [1, 2])
|
| 379 |
+
assert_(van.shape == (1, 5, 6))
|
| 380 |
+
|
| 381 |
+
def test_hermevander3d(self):
|
| 382 |
+
# also tests hermeval3d for non-square coefficient array
|
| 383 |
+
x1, x2, x3 = self.x
|
| 384 |
+
c = np.random.random((2, 3, 4))
|
| 385 |
+
van = herme.hermevander3d(x1, x2, x3, [1, 2, 3])
|
| 386 |
+
tgt = herme.hermeval3d(x1, x2, x3, c)
|
| 387 |
+
res = np.dot(van, c.flat)
|
| 388 |
+
assert_almost_equal(res, tgt)
|
| 389 |
+
|
| 390 |
+
# check shape
|
| 391 |
+
van = herme.hermevander3d([x1], [x2], [x3], [1, 2, 3])
|
| 392 |
+
assert_(van.shape == (1, 5, 24))
|
| 393 |
+
|
| 394 |
+
|
| 395 |
+
class TestFitting:
|
| 396 |
+
|
| 397 |
+
def test_hermefit(self):
|
| 398 |
+
def f(x):
|
| 399 |
+
return x * (x - 1) * (x - 2)
|
| 400 |
+
|
| 401 |
+
def f2(x):
|
| 402 |
+
return x**4 + x**2 + 1
|
| 403 |
+
|
| 404 |
+
# Test exceptions
|
| 405 |
+
assert_raises(ValueError, herme.hermefit, [1], [1], -1)
|
| 406 |
+
assert_raises(TypeError, herme.hermefit, [[1]], [1], 0)
|
| 407 |
+
assert_raises(TypeError, herme.hermefit, [], [1], 0)
|
| 408 |
+
assert_raises(TypeError, herme.hermefit, [1], [[[1]]], 0)
|
| 409 |
+
assert_raises(TypeError, herme.hermefit, [1, 2], [1], 0)
|
| 410 |
+
assert_raises(TypeError, herme.hermefit, [1], [1, 2], 0)
|
| 411 |
+
assert_raises(TypeError, herme.hermefit, [1], [1], 0, w=[[1]])
|
| 412 |
+
assert_raises(TypeError, herme.hermefit, [1], [1], 0, w=[1, 1])
|
| 413 |
+
assert_raises(ValueError, herme.hermefit, [1], [1], [-1,])
|
| 414 |
+
assert_raises(ValueError, herme.hermefit, [1], [1], [2, -1, 6])
|
| 415 |
+
assert_raises(TypeError, herme.hermefit, [1], [1], [])
|
| 416 |
+
|
| 417 |
+
# Test fit
|
| 418 |
+
x = np.linspace(0, 2)
|
| 419 |
+
y = f(x)
|
| 420 |
+
#
|
| 421 |
+
coef3 = herme.hermefit(x, y, 3)
|
| 422 |
+
assert_equal(len(coef3), 4)
|
| 423 |
+
assert_almost_equal(herme.hermeval(x, coef3), y)
|
| 424 |
+
coef3 = herme.hermefit(x, y, [0, 1, 2, 3])
|
| 425 |
+
assert_equal(len(coef3), 4)
|
| 426 |
+
assert_almost_equal(herme.hermeval(x, coef3), y)
|
| 427 |
+
#
|
| 428 |
+
coef4 = herme.hermefit(x, y, 4)
|
| 429 |
+
assert_equal(len(coef4), 5)
|
| 430 |
+
assert_almost_equal(herme.hermeval(x, coef4), y)
|
| 431 |
+
coef4 = herme.hermefit(x, y, [0, 1, 2, 3, 4])
|
| 432 |
+
assert_equal(len(coef4), 5)
|
| 433 |
+
assert_almost_equal(herme.hermeval(x, coef4), y)
|
| 434 |
+
# check things still work if deg is not in strict increasing
|
| 435 |
+
coef4 = herme.hermefit(x, y, [2, 3, 4, 1, 0])
|
| 436 |
+
assert_equal(len(coef4), 5)
|
| 437 |
+
assert_almost_equal(herme.hermeval(x, coef4), y)
|
| 438 |
+
#
|
| 439 |
+
coef2d = herme.hermefit(x, np.array([y, y]).T, 3)
|
| 440 |
+
assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
|
| 441 |
+
coef2d = herme.hermefit(x, np.array([y, y]).T, [0, 1, 2, 3])
|
| 442 |
+
assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
|
| 443 |
+
# test weighting
|
| 444 |
+
w = np.zeros_like(x)
|
| 445 |
+
yw = y.copy()
|
| 446 |
+
w[1::2] = 1
|
| 447 |
+
y[0::2] = 0
|
| 448 |
+
wcoef3 = herme.hermefit(x, yw, 3, w=w)
|
| 449 |
+
assert_almost_equal(wcoef3, coef3)
|
| 450 |
+
wcoef3 = herme.hermefit(x, yw, [0, 1, 2, 3], w=w)
|
| 451 |
+
assert_almost_equal(wcoef3, coef3)
|
| 452 |
+
#
|
| 453 |
+
wcoef2d = herme.hermefit(x, np.array([yw, yw]).T, 3, w=w)
|
| 454 |
+
assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
|
| 455 |
+
wcoef2d = herme.hermefit(x, np.array([yw, yw]).T, [0, 1, 2, 3], w=w)
|
| 456 |
+
assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
|
| 457 |
+
# test scaling with complex values x points whose square
|
| 458 |
+
# is zero when summed.
|
| 459 |
+
x = [1, 1j, -1, -1j]
|
| 460 |
+
assert_almost_equal(herme.hermefit(x, x, 1), [0, 1])
|
| 461 |
+
assert_almost_equal(herme.hermefit(x, x, [0, 1]), [0, 1])
|
| 462 |
+
# test fitting only even Legendre polynomials
|
| 463 |
+
x = np.linspace(-1, 1)
|
| 464 |
+
y = f2(x)
|
| 465 |
+
coef1 = herme.hermefit(x, y, 4)
|
| 466 |
+
assert_almost_equal(herme.hermeval(x, coef1), y)
|
| 467 |
+
coef2 = herme.hermefit(x, y, [0, 2, 4])
|
| 468 |
+
assert_almost_equal(herme.hermeval(x, coef2), y)
|
| 469 |
+
assert_almost_equal(coef1, coef2)
|
| 470 |
+
|
| 471 |
+
|
| 472 |
+
class TestCompanion:
|
| 473 |
+
|
| 474 |
+
def test_raises(self):
|
| 475 |
+
assert_raises(ValueError, herme.hermecompanion, [])
|
| 476 |
+
assert_raises(ValueError, herme.hermecompanion, [1])
|
| 477 |
+
|
| 478 |
+
def test_dimensions(self):
|
| 479 |
+
for i in range(1, 5):
|
| 480 |
+
coef = [0] * i + [1]
|
| 481 |
+
assert_(herme.hermecompanion(coef).shape == (i, i))
|
| 482 |
+
|
| 483 |
+
def test_linear_root(self):
|
| 484 |
+
assert_(herme.hermecompanion([1, 2])[0, 0] == -.5)
|
| 485 |
+
|
| 486 |
+
|
| 487 |
+
class TestGauss:
|
| 488 |
+
|
| 489 |
+
def test_100(self):
|
| 490 |
+
x, w = herme.hermegauss(100)
|
| 491 |
+
|
| 492 |
+
# test orthogonality. Note that the results need to be normalized,
|
| 493 |
+
# otherwise the huge values that can arise from fast growing
|
| 494 |
+
# functions like Laguerre can be very confusing.
|
| 495 |
+
v = herme.hermevander(x, 99)
|
| 496 |
+
vv = np.dot(v.T * w, v)
|
| 497 |
+
vd = 1 / np.sqrt(vv.diagonal())
|
| 498 |
+
vv = vd[:, None] * vv * vd
|
| 499 |
+
assert_almost_equal(vv, np.eye(100))
|
| 500 |
+
|
| 501 |
+
# check that the integral of 1 is correct
|
| 502 |
+
tgt = np.sqrt(2 * np.pi)
|
| 503 |
+
assert_almost_equal(w.sum(), tgt)
|
| 504 |
+
|
| 505 |
+
|
| 506 |
+
class TestMisc:
|
| 507 |
+
|
| 508 |
+
def test_hermefromroots(self):
|
| 509 |
+
res = herme.hermefromroots([])
|
| 510 |
+
assert_almost_equal(trim(res), [1])
|
| 511 |
+
for i in range(1, 5):
|
| 512 |
+
roots = np.cos(np.linspace(-np.pi, 0, 2 * i + 1)[1::2])
|
| 513 |
+
pol = herme.hermefromroots(roots)
|
| 514 |
+
res = herme.hermeval(roots, pol)
|
| 515 |
+
tgt = 0
|
| 516 |
+
assert_(len(pol) == i + 1)
|
| 517 |
+
assert_almost_equal(herme.herme2poly(pol)[-1], 1)
|
| 518 |
+
assert_almost_equal(res, tgt)
|
| 519 |
+
|
| 520 |
+
def test_hermeroots(self):
|
| 521 |
+
assert_almost_equal(herme.hermeroots([1]), [])
|
| 522 |
+
assert_almost_equal(herme.hermeroots([1, 1]), [-1])
|
| 523 |
+
for i in range(2, 5):
|
| 524 |
+
tgt = np.linspace(-1, 1, i)
|
| 525 |
+
res = herme.hermeroots(herme.hermefromroots(tgt))
|
| 526 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 527 |
+
|
| 528 |
+
def test_hermetrim(self):
|
| 529 |
+
coef = [2, -1, 1, 0]
|
| 530 |
+
|
| 531 |
+
# Test exceptions
|
| 532 |
+
assert_raises(ValueError, herme.hermetrim, coef, -1)
|
| 533 |
+
|
| 534 |
+
# Test results
|
| 535 |
+
assert_equal(herme.hermetrim(coef), coef[:-1])
|
| 536 |
+
assert_equal(herme.hermetrim(coef, 1), coef[:-3])
|
| 537 |
+
assert_equal(herme.hermetrim(coef, 2), [0])
|
| 538 |
+
|
| 539 |
+
def test_hermeline(self):
|
| 540 |
+
assert_equal(herme.hermeline(3, 4), [3, 4])
|
| 541 |
+
|
| 542 |
+
def test_herme2poly(self):
|
| 543 |
+
for i in range(10):
|
| 544 |
+
assert_almost_equal(herme.herme2poly([0] * i + [1]), Helist[i])
|
| 545 |
+
|
| 546 |
+
def test_poly2herme(self):
|
| 547 |
+
for i in range(10):
|
| 548 |
+
assert_almost_equal(herme.poly2herme(Helist[i]), [0] * i + [1])
|
| 549 |
+
|
| 550 |
+
def test_weight(self):
|
| 551 |
+
x = np.linspace(-5, 5, 11)
|
| 552 |
+
tgt = np.exp(-.5 * x**2)
|
| 553 |
+
res = herme.hermeweight(x)
|
| 554 |
+
assert_almost_equal(res, tgt)
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/tests/test_laguerre.py
ADDED
|
@@ -0,0 +1,535 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for laguerre module.
|
| 2 |
+
|
| 3 |
+
"""
|
| 4 |
+
from functools import reduce
|
| 5 |
+
|
| 6 |
+
import numpy as np
|
| 7 |
+
import numpy.polynomial.laguerre as lag
|
| 8 |
+
from numpy.polynomial.polynomial import polyval
|
| 9 |
+
from numpy.testing import assert_, assert_almost_equal, assert_equal, assert_raises
|
| 10 |
+
|
| 11 |
+
L0 = np.array([1]) / 1
|
| 12 |
+
L1 = np.array([1, -1]) / 1
|
| 13 |
+
L2 = np.array([2, -4, 1]) / 2
|
| 14 |
+
L3 = np.array([6, -18, 9, -1]) / 6
|
| 15 |
+
L4 = np.array([24, -96, 72, -16, 1]) / 24
|
| 16 |
+
L5 = np.array([120, -600, 600, -200, 25, -1]) / 120
|
| 17 |
+
L6 = np.array([720, -4320, 5400, -2400, 450, -36, 1]) / 720
|
| 18 |
+
|
| 19 |
+
Llist = [L0, L1, L2, L3, L4, L5, L6]
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def trim(x):
|
| 23 |
+
return lag.lagtrim(x, tol=1e-6)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class TestConstants:
|
| 27 |
+
|
| 28 |
+
def test_lagdomain(self):
|
| 29 |
+
assert_equal(lag.lagdomain, [0, 1])
|
| 30 |
+
|
| 31 |
+
def test_lagzero(self):
|
| 32 |
+
assert_equal(lag.lagzero, [0])
|
| 33 |
+
|
| 34 |
+
def test_lagone(self):
|
| 35 |
+
assert_equal(lag.lagone, [1])
|
| 36 |
+
|
| 37 |
+
def test_lagx(self):
|
| 38 |
+
assert_equal(lag.lagx, [1, -1])
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
class TestArithmetic:
|
| 42 |
+
x = np.linspace(-3, 3, 100)
|
| 43 |
+
|
| 44 |
+
def test_lagadd(self):
|
| 45 |
+
for i in range(5):
|
| 46 |
+
for j in range(5):
|
| 47 |
+
msg = f"At i={i}, j={j}"
|
| 48 |
+
tgt = np.zeros(max(i, j) + 1)
|
| 49 |
+
tgt[i] += 1
|
| 50 |
+
tgt[j] += 1
|
| 51 |
+
res = lag.lagadd([0] * i + [1], [0] * j + [1])
|
| 52 |
+
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
| 53 |
+
|
| 54 |
+
def test_lagsub(self):
|
| 55 |
+
for i in range(5):
|
| 56 |
+
for j in range(5):
|
| 57 |
+
msg = f"At i={i}, j={j}"
|
| 58 |
+
tgt = np.zeros(max(i, j) + 1)
|
| 59 |
+
tgt[i] += 1
|
| 60 |
+
tgt[j] -= 1
|
| 61 |
+
res = lag.lagsub([0] * i + [1], [0] * j + [1])
|
| 62 |
+
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
| 63 |
+
|
| 64 |
+
def test_lagmulx(self):
|
| 65 |
+
assert_equal(lag.lagmulx([0]), [0])
|
| 66 |
+
assert_equal(lag.lagmulx([1]), [1, -1])
|
| 67 |
+
for i in range(1, 5):
|
| 68 |
+
ser = [0] * i + [1]
|
| 69 |
+
tgt = [0] * (i - 1) + [-i, 2 * i + 1, -(i + 1)]
|
| 70 |
+
assert_almost_equal(lag.lagmulx(ser), tgt)
|
| 71 |
+
|
| 72 |
+
def test_lagmul(self):
|
| 73 |
+
# check values of result
|
| 74 |
+
for i in range(5):
|
| 75 |
+
pol1 = [0] * i + [1]
|
| 76 |
+
val1 = lag.lagval(self.x, pol1)
|
| 77 |
+
for j in range(5):
|
| 78 |
+
msg = f"At i={i}, j={j}"
|
| 79 |
+
pol2 = [0] * j + [1]
|
| 80 |
+
val2 = lag.lagval(self.x, pol2)
|
| 81 |
+
pol3 = lag.lagmul(pol1, pol2)
|
| 82 |
+
val3 = lag.lagval(self.x, pol3)
|
| 83 |
+
assert_(len(pol3) == i + j + 1, msg)
|
| 84 |
+
assert_almost_equal(val3, val1 * val2, err_msg=msg)
|
| 85 |
+
|
| 86 |
+
def test_lagdiv(self):
|
| 87 |
+
for i in range(5):
|
| 88 |
+
for j in range(5):
|
| 89 |
+
msg = f"At i={i}, j={j}"
|
| 90 |
+
ci = [0] * i + [1]
|
| 91 |
+
cj = [0] * j + [1]
|
| 92 |
+
tgt = lag.lagadd(ci, cj)
|
| 93 |
+
quo, rem = lag.lagdiv(tgt, ci)
|
| 94 |
+
res = lag.lagadd(lag.lagmul(quo, ci), rem)
|
| 95 |
+
assert_almost_equal(trim(res), trim(tgt), err_msg=msg)
|
| 96 |
+
|
| 97 |
+
def test_lagpow(self):
|
| 98 |
+
for i in range(5):
|
| 99 |
+
for j in range(5):
|
| 100 |
+
msg = f"At i={i}, j={j}"
|
| 101 |
+
c = np.arange(i + 1)
|
| 102 |
+
tgt = reduce(lag.lagmul, [c] * j, np.array([1]))
|
| 103 |
+
res = lag.lagpow(c, j)
|
| 104 |
+
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
class TestEvaluation:
|
| 108 |
+
# coefficients of 1 + 2*x + 3*x**2
|
| 109 |
+
c1d = np.array([9., -14., 6.])
|
| 110 |
+
c2d = np.einsum('i,j->ij', c1d, c1d)
|
| 111 |
+
c3d = np.einsum('i,j,k->ijk', c1d, c1d, c1d)
|
| 112 |
+
|
| 113 |
+
# some random values in [-1, 1)
|
| 114 |
+
x = np.random.random((3, 5)) * 2 - 1
|
| 115 |
+
y = polyval(x, [1., 2., 3.])
|
| 116 |
+
|
| 117 |
+
def test_lagval(self):
|
| 118 |
+
# check empty input
|
| 119 |
+
assert_equal(lag.lagval([], [1]).size, 0)
|
| 120 |
+
|
| 121 |
+
# check normal input)
|
| 122 |
+
x = np.linspace(-1, 1)
|
| 123 |
+
y = [polyval(x, c) for c in Llist]
|
| 124 |
+
for i in range(7):
|
| 125 |
+
msg = f"At i={i}"
|
| 126 |
+
tgt = y[i]
|
| 127 |
+
res = lag.lagval(x, [0] * i + [1])
|
| 128 |
+
assert_almost_equal(res, tgt, err_msg=msg)
|
| 129 |
+
|
| 130 |
+
# check that shape is preserved
|
| 131 |
+
for i in range(3):
|
| 132 |
+
dims = [2] * i
|
| 133 |
+
x = np.zeros(dims)
|
| 134 |
+
assert_equal(lag.lagval(x, [1]).shape, dims)
|
| 135 |
+
assert_equal(lag.lagval(x, [1, 0]).shape, dims)
|
| 136 |
+
assert_equal(lag.lagval(x, [1, 0, 0]).shape, dims)
|
| 137 |
+
|
| 138 |
+
def test_lagval2d(self):
|
| 139 |
+
x1, x2, x3 = self.x
|
| 140 |
+
y1, y2, y3 = self.y
|
| 141 |
+
|
| 142 |
+
# test exceptions
|
| 143 |
+
assert_raises(ValueError, lag.lagval2d, x1, x2[:2], self.c2d)
|
| 144 |
+
|
| 145 |
+
# test values
|
| 146 |
+
tgt = y1 * y2
|
| 147 |
+
res = lag.lagval2d(x1, x2, self.c2d)
|
| 148 |
+
assert_almost_equal(res, tgt)
|
| 149 |
+
|
| 150 |
+
# test shape
|
| 151 |
+
z = np.ones((2, 3))
|
| 152 |
+
res = lag.lagval2d(z, z, self.c2d)
|
| 153 |
+
assert_(res.shape == (2, 3))
|
| 154 |
+
|
| 155 |
+
def test_lagval3d(self):
|
| 156 |
+
x1, x2, x3 = self.x
|
| 157 |
+
y1, y2, y3 = self.y
|
| 158 |
+
|
| 159 |
+
# test exceptions
|
| 160 |
+
assert_raises(ValueError, lag.lagval3d, x1, x2, x3[:2], self.c3d)
|
| 161 |
+
|
| 162 |
+
# test values
|
| 163 |
+
tgt = y1 * y2 * y3
|
| 164 |
+
res = lag.lagval3d(x1, x2, x3, self.c3d)
|
| 165 |
+
assert_almost_equal(res, tgt)
|
| 166 |
+
|
| 167 |
+
# test shape
|
| 168 |
+
z = np.ones((2, 3))
|
| 169 |
+
res = lag.lagval3d(z, z, z, self.c3d)
|
| 170 |
+
assert_(res.shape == (2, 3))
|
| 171 |
+
|
| 172 |
+
def test_laggrid2d(self):
|
| 173 |
+
x1, x2, x3 = self.x
|
| 174 |
+
y1, y2, y3 = self.y
|
| 175 |
+
|
| 176 |
+
# test values
|
| 177 |
+
tgt = np.einsum('i,j->ij', y1, y2)
|
| 178 |
+
res = lag.laggrid2d(x1, x2, self.c2d)
|
| 179 |
+
assert_almost_equal(res, tgt)
|
| 180 |
+
|
| 181 |
+
# test shape
|
| 182 |
+
z = np.ones((2, 3))
|
| 183 |
+
res = lag.laggrid2d(z, z, self.c2d)
|
| 184 |
+
assert_(res.shape == (2, 3) * 2)
|
| 185 |
+
|
| 186 |
+
def test_laggrid3d(self):
|
| 187 |
+
x1, x2, x3 = self.x
|
| 188 |
+
y1, y2, y3 = self.y
|
| 189 |
+
|
| 190 |
+
# test values
|
| 191 |
+
tgt = np.einsum('i,j,k->ijk', y1, y2, y3)
|
| 192 |
+
res = lag.laggrid3d(x1, x2, x3, self.c3d)
|
| 193 |
+
assert_almost_equal(res, tgt)
|
| 194 |
+
|
| 195 |
+
# test shape
|
| 196 |
+
z = np.ones((2, 3))
|
| 197 |
+
res = lag.laggrid3d(z, z, z, self.c3d)
|
| 198 |
+
assert_(res.shape == (2, 3) * 3)
|
| 199 |
+
|
| 200 |
+
|
| 201 |
+
class TestIntegral:
|
| 202 |
+
|
| 203 |
+
def test_lagint(self):
|
| 204 |
+
# check exceptions
|
| 205 |
+
assert_raises(TypeError, lag.lagint, [0], .5)
|
| 206 |
+
assert_raises(ValueError, lag.lagint, [0], -1)
|
| 207 |
+
assert_raises(ValueError, lag.lagint, [0], 1, [0, 0])
|
| 208 |
+
assert_raises(ValueError, lag.lagint, [0], lbnd=[0])
|
| 209 |
+
assert_raises(ValueError, lag.lagint, [0], scl=[0])
|
| 210 |
+
assert_raises(TypeError, lag.lagint, [0], axis=.5)
|
| 211 |
+
|
| 212 |
+
# test integration of zero polynomial
|
| 213 |
+
for i in range(2, 5):
|
| 214 |
+
k = [0] * (i - 2) + [1]
|
| 215 |
+
res = lag.lagint([0], m=i, k=k)
|
| 216 |
+
assert_almost_equal(res, [1, -1])
|
| 217 |
+
|
| 218 |
+
# check single integration with integration constant
|
| 219 |
+
for i in range(5):
|
| 220 |
+
scl = i + 1
|
| 221 |
+
pol = [0] * i + [1]
|
| 222 |
+
tgt = [i] + [0] * i + [1 / scl]
|
| 223 |
+
lagpol = lag.poly2lag(pol)
|
| 224 |
+
lagint = lag.lagint(lagpol, m=1, k=[i])
|
| 225 |
+
res = lag.lag2poly(lagint)
|
| 226 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 227 |
+
|
| 228 |
+
# check single integration with integration constant and lbnd
|
| 229 |
+
for i in range(5):
|
| 230 |
+
scl = i + 1
|
| 231 |
+
pol = [0] * i + [1]
|
| 232 |
+
lagpol = lag.poly2lag(pol)
|
| 233 |
+
lagint = lag.lagint(lagpol, m=1, k=[i], lbnd=-1)
|
| 234 |
+
assert_almost_equal(lag.lagval(-1, lagint), i)
|
| 235 |
+
|
| 236 |
+
# check single integration with integration constant and scaling
|
| 237 |
+
for i in range(5):
|
| 238 |
+
scl = i + 1
|
| 239 |
+
pol = [0] * i + [1]
|
| 240 |
+
tgt = [i] + [0] * i + [2 / scl]
|
| 241 |
+
lagpol = lag.poly2lag(pol)
|
| 242 |
+
lagint = lag.lagint(lagpol, m=1, k=[i], scl=2)
|
| 243 |
+
res = lag.lag2poly(lagint)
|
| 244 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 245 |
+
|
| 246 |
+
# check multiple integrations with default k
|
| 247 |
+
for i in range(5):
|
| 248 |
+
for j in range(2, 5):
|
| 249 |
+
pol = [0] * i + [1]
|
| 250 |
+
tgt = pol[:]
|
| 251 |
+
for k in range(j):
|
| 252 |
+
tgt = lag.lagint(tgt, m=1)
|
| 253 |
+
res = lag.lagint(pol, m=j)
|
| 254 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 255 |
+
|
| 256 |
+
# check multiple integrations with defined k
|
| 257 |
+
for i in range(5):
|
| 258 |
+
for j in range(2, 5):
|
| 259 |
+
pol = [0] * i + [1]
|
| 260 |
+
tgt = pol[:]
|
| 261 |
+
for k in range(j):
|
| 262 |
+
tgt = lag.lagint(tgt, m=1, k=[k])
|
| 263 |
+
res = lag.lagint(pol, m=j, k=list(range(j)))
|
| 264 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 265 |
+
|
| 266 |
+
# check multiple integrations with lbnd
|
| 267 |
+
for i in range(5):
|
| 268 |
+
for j in range(2, 5):
|
| 269 |
+
pol = [0] * i + [1]
|
| 270 |
+
tgt = pol[:]
|
| 271 |
+
for k in range(j):
|
| 272 |
+
tgt = lag.lagint(tgt, m=1, k=[k], lbnd=-1)
|
| 273 |
+
res = lag.lagint(pol, m=j, k=list(range(j)), lbnd=-1)
|
| 274 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 275 |
+
|
| 276 |
+
# check multiple integrations with scaling
|
| 277 |
+
for i in range(5):
|
| 278 |
+
for j in range(2, 5):
|
| 279 |
+
pol = [0] * i + [1]
|
| 280 |
+
tgt = pol[:]
|
| 281 |
+
for k in range(j):
|
| 282 |
+
tgt = lag.lagint(tgt, m=1, k=[k], scl=2)
|
| 283 |
+
res = lag.lagint(pol, m=j, k=list(range(j)), scl=2)
|
| 284 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 285 |
+
|
| 286 |
+
def test_lagint_axis(self):
|
| 287 |
+
# check that axis keyword works
|
| 288 |
+
c2d = np.random.random((3, 4))
|
| 289 |
+
|
| 290 |
+
tgt = np.vstack([lag.lagint(c) for c in c2d.T]).T
|
| 291 |
+
res = lag.lagint(c2d, axis=0)
|
| 292 |
+
assert_almost_equal(res, tgt)
|
| 293 |
+
|
| 294 |
+
tgt = np.vstack([lag.lagint(c) for c in c2d])
|
| 295 |
+
res = lag.lagint(c2d, axis=1)
|
| 296 |
+
assert_almost_equal(res, tgt)
|
| 297 |
+
|
| 298 |
+
tgt = np.vstack([lag.lagint(c, k=3) for c in c2d])
|
| 299 |
+
res = lag.lagint(c2d, k=3, axis=1)
|
| 300 |
+
assert_almost_equal(res, tgt)
|
| 301 |
+
|
| 302 |
+
|
| 303 |
+
class TestDerivative:
|
| 304 |
+
|
| 305 |
+
def test_lagder(self):
|
| 306 |
+
# check exceptions
|
| 307 |
+
assert_raises(TypeError, lag.lagder, [0], .5)
|
| 308 |
+
assert_raises(ValueError, lag.lagder, [0], -1)
|
| 309 |
+
|
| 310 |
+
# check that zeroth derivative does nothing
|
| 311 |
+
for i in range(5):
|
| 312 |
+
tgt = [0] * i + [1]
|
| 313 |
+
res = lag.lagder(tgt, m=0)
|
| 314 |
+
assert_equal(trim(res), trim(tgt))
|
| 315 |
+
|
| 316 |
+
# check that derivation is the inverse of integration
|
| 317 |
+
for i in range(5):
|
| 318 |
+
for j in range(2, 5):
|
| 319 |
+
tgt = [0] * i + [1]
|
| 320 |
+
res = lag.lagder(lag.lagint(tgt, m=j), m=j)
|
| 321 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 322 |
+
|
| 323 |
+
# check derivation with scaling
|
| 324 |
+
for i in range(5):
|
| 325 |
+
for j in range(2, 5):
|
| 326 |
+
tgt = [0] * i + [1]
|
| 327 |
+
res = lag.lagder(lag.lagint(tgt, m=j, scl=2), m=j, scl=.5)
|
| 328 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 329 |
+
|
| 330 |
+
def test_lagder_axis(self):
|
| 331 |
+
# check that axis keyword works
|
| 332 |
+
c2d = np.random.random((3, 4))
|
| 333 |
+
|
| 334 |
+
tgt = np.vstack([lag.lagder(c) for c in c2d.T]).T
|
| 335 |
+
res = lag.lagder(c2d, axis=0)
|
| 336 |
+
assert_almost_equal(res, tgt)
|
| 337 |
+
|
| 338 |
+
tgt = np.vstack([lag.lagder(c) for c in c2d])
|
| 339 |
+
res = lag.lagder(c2d, axis=1)
|
| 340 |
+
assert_almost_equal(res, tgt)
|
| 341 |
+
|
| 342 |
+
|
| 343 |
+
class TestVander:
|
| 344 |
+
# some random values in [-1, 1)
|
| 345 |
+
x = np.random.random((3, 5)) * 2 - 1
|
| 346 |
+
|
| 347 |
+
def test_lagvander(self):
|
| 348 |
+
# check for 1d x
|
| 349 |
+
x = np.arange(3)
|
| 350 |
+
v = lag.lagvander(x, 3)
|
| 351 |
+
assert_(v.shape == (3, 4))
|
| 352 |
+
for i in range(4):
|
| 353 |
+
coef = [0] * i + [1]
|
| 354 |
+
assert_almost_equal(v[..., i], lag.lagval(x, coef))
|
| 355 |
+
|
| 356 |
+
# check for 2d x
|
| 357 |
+
x = np.array([[1, 2], [3, 4], [5, 6]])
|
| 358 |
+
v = lag.lagvander(x, 3)
|
| 359 |
+
assert_(v.shape == (3, 2, 4))
|
| 360 |
+
for i in range(4):
|
| 361 |
+
coef = [0] * i + [1]
|
| 362 |
+
assert_almost_equal(v[..., i], lag.lagval(x, coef))
|
| 363 |
+
|
| 364 |
+
def test_lagvander2d(self):
|
| 365 |
+
# also tests lagval2d for non-square coefficient array
|
| 366 |
+
x1, x2, x3 = self.x
|
| 367 |
+
c = np.random.random((2, 3))
|
| 368 |
+
van = lag.lagvander2d(x1, x2, [1, 2])
|
| 369 |
+
tgt = lag.lagval2d(x1, x2, c)
|
| 370 |
+
res = np.dot(van, c.flat)
|
| 371 |
+
assert_almost_equal(res, tgt)
|
| 372 |
+
|
| 373 |
+
# check shape
|
| 374 |
+
van = lag.lagvander2d([x1], [x2], [1, 2])
|
| 375 |
+
assert_(van.shape == (1, 5, 6))
|
| 376 |
+
|
| 377 |
+
def test_lagvander3d(self):
|
| 378 |
+
# also tests lagval3d for non-square coefficient array
|
| 379 |
+
x1, x2, x3 = self.x
|
| 380 |
+
c = np.random.random((2, 3, 4))
|
| 381 |
+
van = lag.lagvander3d(x1, x2, x3, [1, 2, 3])
|
| 382 |
+
tgt = lag.lagval3d(x1, x2, x3, c)
|
| 383 |
+
res = np.dot(van, c.flat)
|
| 384 |
+
assert_almost_equal(res, tgt)
|
| 385 |
+
|
| 386 |
+
# check shape
|
| 387 |
+
van = lag.lagvander3d([x1], [x2], [x3], [1, 2, 3])
|
| 388 |
+
assert_(van.shape == (1, 5, 24))
|
| 389 |
+
|
| 390 |
+
|
| 391 |
+
class TestFitting:
|
| 392 |
+
|
| 393 |
+
def test_lagfit(self):
|
| 394 |
+
def f(x):
|
| 395 |
+
return x * (x - 1) * (x - 2)
|
| 396 |
+
|
| 397 |
+
# Test exceptions
|
| 398 |
+
assert_raises(ValueError, lag.lagfit, [1], [1], -1)
|
| 399 |
+
assert_raises(TypeError, lag.lagfit, [[1]], [1], 0)
|
| 400 |
+
assert_raises(TypeError, lag.lagfit, [], [1], 0)
|
| 401 |
+
assert_raises(TypeError, lag.lagfit, [1], [[[1]]], 0)
|
| 402 |
+
assert_raises(TypeError, lag.lagfit, [1, 2], [1], 0)
|
| 403 |
+
assert_raises(TypeError, lag.lagfit, [1], [1, 2], 0)
|
| 404 |
+
assert_raises(TypeError, lag.lagfit, [1], [1], 0, w=[[1]])
|
| 405 |
+
assert_raises(TypeError, lag.lagfit, [1], [1], 0, w=[1, 1])
|
| 406 |
+
assert_raises(ValueError, lag.lagfit, [1], [1], [-1,])
|
| 407 |
+
assert_raises(ValueError, lag.lagfit, [1], [1], [2, -1, 6])
|
| 408 |
+
assert_raises(TypeError, lag.lagfit, [1], [1], [])
|
| 409 |
+
|
| 410 |
+
# Test fit
|
| 411 |
+
x = np.linspace(0, 2)
|
| 412 |
+
y = f(x)
|
| 413 |
+
#
|
| 414 |
+
coef3 = lag.lagfit(x, y, 3)
|
| 415 |
+
assert_equal(len(coef3), 4)
|
| 416 |
+
assert_almost_equal(lag.lagval(x, coef3), y)
|
| 417 |
+
coef3 = lag.lagfit(x, y, [0, 1, 2, 3])
|
| 418 |
+
assert_equal(len(coef3), 4)
|
| 419 |
+
assert_almost_equal(lag.lagval(x, coef3), y)
|
| 420 |
+
#
|
| 421 |
+
coef4 = lag.lagfit(x, y, 4)
|
| 422 |
+
assert_equal(len(coef4), 5)
|
| 423 |
+
assert_almost_equal(lag.lagval(x, coef4), y)
|
| 424 |
+
coef4 = lag.lagfit(x, y, [0, 1, 2, 3, 4])
|
| 425 |
+
assert_equal(len(coef4), 5)
|
| 426 |
+
assert_almost_equal(lag.lagval(x, coef4), y)
|
| 427 |
+
#
|
| 428 |
+
coef2d = lag.lagfit(x, np.array([y, y]).T, 3)
|
| 429 |
+
assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
|
| 430 |
+
coef2d = lag.lagfit(x, np.array([y, y]).T, [0, 1, 2, 3])
|
| 431 |
+
assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
|
| 432 |
+
# test weighting
|
| 433 |
+
w = np.zeros_like(x)
|
| 434 |
+
yw = y.copy()
|
| 435 |
+
w[1::2] = 1
|
| 436 |
+
y[0::2] = 0
|
| 437 |
+
wcoef3 = lag.lagfit(x, yw, 3, w=w)
|
| 438 |
+
assert_almost_equal(wcoef3, coef3)
|
| 439 |
+
wcoef3 = lag.lagfit(x, yw, [0, 1, 2, 3], w=w)
|
| 440 |
+
assert_almost_equal(wcoef3, coef3)
|
| 441 |
+
#
|
| 442 |
+
wcoef2d = lag.lagfit(x, np.array([yw, yw]).T, 3, w=w)
|
| 443 |
+
assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
|
| 444 |
+
wcoef2d = lag.lagfit(x, np.array([yw, yw]).T, [0, 1, 2, 3], w=w)
|
| 445 |
+
assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
|
| 446 |
+
# test scaling with complex values x points whose square
|
| 447 |
+
# is zero when summed.
|
| 448 |
+
x = [1, 1j, -1, -1j]
|
| 449 |
+
assert_almost_equal(lag.lagfit(x, x, 1), [1, -1])
|
| 450 |
+
assert_almost_equal(lag.lagfit(x, x, [0, 1]), [1, -1])
|
| 451 |
+
|
| 452 |
+
|
| 453 |
+
class TestCompanion:
|
| 454 |
+
|
| 455 |
+
def test_raises(self):
|
| 456 |
+
assert_raises(ValueError, lag.lagcompanion, [])
|
| 457 |
+
assert_raises(ValueError, lag.lagcompanion, [1])
|
| 458 |
+
|
| 459 |
+
def test_dimensions(self):
|
| 460 |
+
for i in range(1, 5):
|
| 461 |
+
coef = [0] * i + [1]
|
| 462 |
+
assert_(lag.lagcompanion(coef).shape == (i, i))
|
| 463 |
+
|
| 464 |
+
def test_linear_root(self):
|
| 465 |
+
assert_(lag.lagcompanion([1, 2])[0, 0] == 1.5)
|
| 466 |
+
|
| 467 |
+
|
| 468 |
+
class TestGauss:
|
| 469 |
+
|
| 470 |
+
def test_100(self):
|
| 471 |
+
x, w = lag.laggauss(100)
|
| 472 |
+
|
| 473 |
+
# test orthogonality. Note that the results need to be normalized,
|
| 474 |
+
# otherwise the huge values that can arise from fast growing
|
| 475 |
+
# functions like Laguerre can be very confusing.
|
| 476 |
+
v = lag.lagvander(x, 99)
|
| 477 |
+
vv = np.dot(v.T * w, v)
|
| 478 |
+
vd = 1 / np.sqrt(vv.diagonal())
|
| 479 |
+
vv = vd[:, None] * vv * vd
|
| 480 |
+
assert_almost_equal(vv, np.eye(100))
|
| 481 |
+
|
| 482 |
+
# check that the integral of 1 is correct
|
| 483 |
+
tgt = 1.0
|
| 484 |
+
assert_almost_equal(w.sum(), tgt)
|
| 485 |
+
|
| 486 |
+
|
| 487 |
+
class TestMisc:
|
| 488 |
+
|
| 489 |
+
def test_lagfromroots(self):
|
| 490 |
+
res = lag.lagfromroots([])
|
| 491 |
+
assert_almost_equal(trim(res), [1])
|
| 492 |
+
for i in range(1, 5):
|
| 493 |
+
roots = np.cos(np.linspace(-np.pi, 0, 2 * i + 1)[1::2])
|
| 494 |
+
pol = lag.lagfromroots(roots)
|
| 495 |
+
res = lag.lagval(roots, pol)
|
| 496 |
+
tgt = 0
|
| 497 |
+
assert_(len(pol) == i + 1)
|
| 498 |
+
assert_almost_equal(lag.lag2poly(pol)[-1], 1)
|
| 499 |
+
assert_almost_equal(res, tgt)
|
| 500 |
+
|
| 501 |
+
def test_lagroots(self):
|
| 502 |
+
assert_almost_equal(lag.lagroots([1]), [])
|
| 503 |
+
assert_almost_equal(lag.lagroots([0, 1]), [1])
|
| 504 |
+
for i in range(2, 5):
|
| 505 |
+
tgt = np.linspace(0, 3, i)
|
| 506 |
+
res = lag.lagroots(lag.lagfromroots(tgt))
|
| 507 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 508 |
+
|
| 509 |
+
def test_lagtrim(self):
|
| 510 |
+
coef = [2, -1, 1, 0]
|
| 511 |
+
|
| 512 |
+
# Test exceptions
|
| 513 |
+
assert_raises(ValueError, lag.lagtrim, coef, -1)
|
| 514 |
+
|
| 515 |
+
# Test results
|
| 516 |
+
assert_equal(lag.lagtrim(coef), coef[:-1])
|
| 517 |
+
assert_equal(lag.lagtrim(coef, 1), coef[:-3])
|
| 518 |
+
assert_equal(lag.lagtrim(coef, 2), [0])
|
| 519 |
+
|
| 520 |
+
def test_lagline(self):
|
| 521 |
+
assert_equal(lag.lagline(3, 4), [7, -4])
|
| 522 |
+
|
| 523 |
+
def test_lag2poly(self):
|
| 524 |
+
for i in range(7):
|
| 525 |
+
assert_almost_equal(lag.lag2poly([0] * i + [1]), Llist[i])
|
| 526 |
+
|
| 527 |
+
def test_poly2lag(self):
|
| 528 |
+
for i in range(7):
|
| 529 |
+
assert_almost_equal(lag.poly2lag(Llist[i]), [0] * i + [1])
|
| 530 |
+
|
| 531 |
+
def test_weight(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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for legendre module.
|
| 2 |
+
|
| 3 |
+
"""
|
| 4 |
+
from functools import reduce
|
| 5 |
+
|
| 6 |
+
import numpy as np
|
| 7 |
+
import numpy.polynomial.legendre as leg
|
| 8 |
+
from numpy.polynomial.polynomial import polyval
|
| 9 |
+
from numpy.testing import assert_, assert_almost_equal, assert_equal, assert_raises
|
| 10 |
+
|
| 11 |
+
L0 = np.array([1])
|
| 12 |
+
L1 = np.array([0, 1])
|
| 13 |
+
L2 = np.array([-1, 0, 3]) / 2
|
| 14 |
+
L3 = np.array([0, -3, 0, 5]) / 2
|
| 15 |
+
L4 = np.array([3, 0, -30, 0, 35]) / 8
|
| 16 |
+
L5 = np.array([0, 15, 0, -70, 0, 63]) / 8
|
| 17 |
+
L6 = np.array([-5, 0, 105, 0, -315, 0, 231]) / 16
|
| 18 |
+
L7 = np.array([0, -35, 0, 315, 0, -693, 0, 429]) / 16
|
| 19 |
+
L8 = np.array([35, 0, -1260, 0, 6930, 0, -12012, 0, 6435]) / 128
|
| 20 |
+
L9 = np.array([0, 315, 0, -4620, 0, 18018, 0, -25740, 0, 12155]) / 128
|
| 21 |
+
|
| 22 |
+
Llist = [L0, L1, L2, L3, L4, L5, L6, L7, L8, L9]
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def trim(x):
|
| 26 |
+
return leg.legtrim(x, tol=1e-6)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
class TestConstants:
|
| 30 |
+
|
| 31 |
+
def test_legdomain(self):
|
| 32 |
+
assert_equal(leg.legdomain, [-1, 1])
|
| 33 |
+
|
| 34 |
+
def test_legzero(self):
|
| 35 |
+
assert_equal(leg.legzero, [0])
|
| 36 |
+
|
| 37 |
+
def test_legone(self):
|
| 38 |
+
assert_equal(leg.legone, [1])
|
| 39 |
+
|
| 40 |
+
def test_legx(self):
|
| 41 |
+
assert_equal(leg.legx, [0, 1])
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
class TestArithmetic:
|
| 45 |
+
x = np.linspace(-1, 1, 100)
|
| 46 |
+
|
| 47 |
+
def test_legadd(self):
|
| 48 |
+
for i in range(5):
|
| 49 |
+
for j in range(5):
|
| 50 |
+
msg = f"At i={i}, j={j}"
|
| 51 |
+
tgt = np.zeros(max(i, j) + 1)
|
| 52 |
+
tgt[i] += 1
|
| 53 |
+
tgt[j] += 1
|
| 54 |
+
res = leg.legadd([0] * i + [1], [0] * j + [1])
|
| 55 |
+
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
| 56 |
+
|
| 57 |
+
def test_legsub(self):
|
| 58 |
+
for i in range(5):
|
| 59 |
+
for j in range(5):
|
| 60 |
+
msg = f"At i={i}, j={j}"
|
| 61 |
+
tgt = np.zeros(max(i, j) + 1)
|
| 62 |
+
tgt[i] += 1
|
| 63 |
+
tgt[j] -= 1
|
| 64 |
+
res = leg.legsub([0] * i + [1], [0] * j + [1])
|
| 65 |
+
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
| 66 |
+
|
| 67 |
+
def test_legmulx(self):
|
| 68 |
+
assert_equal(leg.legmulx([0]), [0])
|
| 69 |
+
assert_equal(leg.legmulx([1]), [0, 1])
|
| 70 |
+
for i in range(1, 5):
|
| 71 |
+
tmp = 2 * i + 1
|
| 72 |
+
ser = [0] * i + [1]
|
| 73 |
+
tgt = [0] * (i - 1) + [i / tmp, 0, (i + 1) / tmp]
|
| 74 |
+
assert_equal(leg.legmulx(ser), tgt)
|
| 75 |
+
|
| 76 |
+
def test_legmul(self):
|
| 77 |
+
# check values of result
|
| 78 |
+
for i in range(5):
|
| 79 |
+
pol1 = [0] * i + [1]
|
| 80 |
+
val1 = leg.legval(self.x, pol1)
|
| 81 |
+
for j in range(5):
|
| 82 |
+
msg = f"At i={i}, j={j}"
|
| 83 |
+
pol2 = [0] * j + [1]
|
| 84 |
+
val2 = leg.legval(self.x, pol2)
|
| 85 |
+
pol3 = leg.legmul(pol1, pol2)
|
| 86 |
+
val3 = leg.legval(self.x, pol3)
|
| 87 |
+
assert_(len(pol3) == i + j + 1, msg)
|
| 88 |
+
assert_almost_equal(val3, val1 * val2, err_msg=msg)
|
| 89 |
+
|
| 90 |
+
def test_legdiv(self):
|
| 91 |
+
for i in range(5):
|
| 92 |
+
for j in range(5):
|
| 93 |
+
msg = f"At i={i}, j={j}"
|
| 94 |
+
ci = [0] * i + [1]
|
| 95 |
+
cj = [0] * j + [1]
|
| 96 |
+
tgt = leg.legadd(ci, cj)
|
| 97 |
+
quo, rem = leg.legdiv(tgt, ci)
|
| 98 |
+
res = leg.legadd(leg.legmul(quo, ci), rem)
|
| 99 |
+
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
| 100 |
+
|
| 101 |
+
def test_legpow(self):
|
| 102 |
+
for i in range(5):
|
| 103 |
+
for j in range(5):
|
| 104 |
+
msg = f"At i={i}, j={j}"
|
| 105 |
+
c = np.arange(i + 1)
|
| 106 |
+
tgt = reduce(leg.legmul, [c] * j, np.array([1]))
|
| 107 |
+
res = leg.legpow(c, j)
|
| 108 |
+
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
class TestEvaluation:
|
| 112 |
+
# coefficients of 1 + 2*x + 3*x**2
|
| 113 |
+
c1d = np.array([2., 2., 2.])
|
| 114 |
+
c2d = np.einsum('i,j->ij', c1d, c1d)
|
| 115 |
+
c3d = np.einsum('i,j,k->ijk', c1d, c1d, c1d)
|
| 116 |
+
|
| 117 |
+
# some random values in [-1, 1)
|
| 118 |
+
x = np.random.random((3, 5)) * 2 - 1
|
| 119 |
+
y = polyval(x, [1., 2., 3.])
|
| 120 |
+
|
| 121 |
+
def test_legval(self):
|
| 122 |
+
# check empty input
|
| 123 |
+
assert_equal(leg.legval([], [1]).size, 0)
|
| 124 |
+
|
| 125 |
+
# check normal input)
|
| 126 |
+
x = np.linspace(-1, 1)
|
| 127 |
+
y = [polyval(x, c) for c in Llist]
|
| 128 |
+
for i in range(10):
|
| 129 |
+
msg = f"At i={i}"
|
| 130 |
+
tgt = y[i]
|
| 131 |
+
res = leg.legval(x, [0] * i + [1])
|
| 132 |
+
assert_almost_equal(res, tgt, err_msg=msg)
|
| 133 |
+
|
| 134 |
+
# check that shape is preserved
|
| 135 |
+
for i in range(3):
|
| 136 |
+
dims = [2] * i
|
| 137 |
+
x = np.zeros(dims)
|
| 138 |
+
assert_equal(leg.legval(x, [1]).shape, dims)
|
| 139 |
+
assert_equal(leg.legval(x, [1, 0]).shape, dims)
|
| 140 |
+
assert_equal(leg.legval(x, [1, 0, 0]).shape, dims)
|
| 141 |
+
|
| 142 |
+
def test_legval2d(self):
|
| 143 |
+
x1, x2, x3 = self.x
|
| 144 |
+
y1, y2, y3 = self.y
|
| 145 |
+
|
| 146 |
+
# test exceptions
|
| 147 |
+
assert_raises(ValueError, leg.legval2d, x1, x2[:2], self.c2d)
|
| 148 |
+
|
| 149 |
+
# test values
|
| 150 |
+
tgt = y1 * y2
|
| 151 |
+
res = leg.legval2d(x1, x2, self.c2d)
|
| 152 |
+
assert_almost_equal(res, tgt)
|
| 153 |
+
|
| 154 |
+
# test shape
|
| 155 |
+
z = np.ones((2, 3))
|
| 156 |
+
res = leg.legval2d(z, z, self.c2d)
|
| 157 |
+
assert_(res.shape == (2, 3))
|
| 158 |
+
|
| 159 |
+
def test_legval3d(self):
|
| 160 |
+
x1, x2, x3 = self.x
|
| 161 |
+
y1, y2, y3 = self.y
|
| 162 |
+
|
| 163 |
+
# test exceptions
|
| 164 |
+
assert_raises(ValueError, leg.legval3d, x1, x2, x3[:2], self.c3d)
|
| 165 |
+
|
| 166 |
+
# test values
|
| 167 |
+
tgt = y1 * y2 * y3
|
| 168 |
+
res = leg.legval3d(x1, x2, x3, self.c3d)
|
| 169 |
+
assert_almost_equal(res, tgt)
|
| 170 |
+
|
| 171 |
+
# test shape
|
| 172 |
+
z = np.ones((2, 3))
|
| 173 |
+
res = leg.legval3d(z, z, z, self.c3d)
|
| 174 |
+
assert_(res.shape == (2, 3))
|
| 175 |
+
|
| 176 |
+
def test_leggrid2d(self):
|
| 177 |
+
x1, x2, x3 = self.x
|
| 178 |
+
y1, y2, y3 = self.y
|
| 179 |
+
|
| 180 |
+
# test values
|
| 181 |
+
tgt = np.einsum('i,j->ij', y1, y2)
|
| 182 |
+
res = leg.leggrid2d(x1, x2, self.c2d)
|
| 183 |
+
assert_almost_equal(res, tgt)
|
| 184 |
+
|
| 185 |
+
# test shape
|
| 186 |
+
z = np.ones((2, 3))
|
| 187 |
+
res = leg.leggrid2d(z, z, self.c2d)
|
| 188 |
+
assert_(res.shape == (2, 3) * 2)
|
| 189 |
+
|
| 190 |
+
def test_leggrid3d(self):
|
| 191 |
+
x1, x2, x3 = self.x
|
| 192 |
+
y1, y2, y3 = self.y
|
| 193 |
+
|
| 194 |
+
# test values
|
| 195 |
+
tgt = np.einsum('i,j,k->ijk', y1, y2, y3)
|
| 196 |
+
res = leg.leggrid3d(x1, x2, x3, self.c3d)
|
| 197 |
+
assert_almost_equal(res, tgt)
|
| 198 |
+
|
| 199 |
+
# test shape
|
| 200 |
+
z = np.ones((2, 3))
|
| 201 |
+
res = leg.leggrid3d(z, z, z, self.c3d)
|
| 202 |
+
assert_(res.shape == (2, 3) * 3)
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
class TestIntegral:
|
| 206 |
+
|
| 207 |
+
def test_legint(self):
|
| 208 |
+
# check exceptions
|
| 209 |
+
assert_raises(TypeError, leg.legint, [0], .5)
|
| 210 |
+
assert_raises(ValueError, leg.legint, [0], -1)
|
| 211 |
+
assert_raises(ValueError, leg.legint, [0], 1, [0, 0])
|
| 212 |
+
assert_raises(ValueError, leg.legint, [0], lbnd=[0])
|
| 213 |
+
assert_raises(ValueError, leg.legint, [0], scl=[0])
|
| 214 |
+
assert_raises(TypeError, leg.legint, [0], axis=.5)
|
| 215 |
+
|
| 216 |
+
# test integration of zero polynomial
|
| 217 |
+
for i in range(2, 5):
|
| 218 |
+
k = [0] * (i - 2) + [1]
|
| 219 |
+
res = leg.legint([0], m=i, k=k)
|
| 220 |
+
assert_almost_equal(res, [0, 1])
|
| 221 |
+
|
| 222 |
+
# check single integration with integration constant
|
| 223 |
+
for i in range(5):
|
| 224 |
+
scl = i + 1
|
| 225 |
+
pol = [0] * i + [1]
|
| 226 |
+
tgt = [i] + [0] * i + [1 / scl]
|
| 227 |
+
legpol = leg.poly2leg(pol)
|
| 228 |
+
legint = leg.legint(legpol, m=1, k=[i])
|
| 229 |
+
res = leg.leg2poly(legint)
|
| 230 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 231 |
+
|
| 232 |
+
# check single integration with integration constant and lbnd
|
| 233 |
+
for i in range(5):
|
| 234 |
+
scl = i + 1
|
| 235 |
+
pol = [0] * i + [1]
|
| 236 |
+
legpol = leg.poly2leg(pol)
|
| 237 |
+
legint = leg.legint(legpol, m=1, k=[i], lbnd=-1)
|
| 238 |
+
assert_almost_equal(leg.legval(-1, legint), i)
|
| 239 |
+
|
| 240 |
+
# check single integration with integration constant and scaling
|
| 241 |
+
for i in range(5):
|
| 242 |
+
scl = i + 1
|
| 243 |
+
pol = [0] * i + [1]
|
| 244 |
+
tgt = [i] + [0] * i + [2 / scl]
|
| 245 |
+
legpol = leg.poly2leg(pol)
|
| 246 |
+
legint = leg.legint(legpol, m=1, k=[i], scl=2)
|
| 247 |
+
res = leg.leg2poly(legint)
|
| 248 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 249 |
+
|
| 250 |
+
# check multiple integrations with default k
|
| 251 |
+
for i in range(5):
|
| 252 |
+
for j in range(2, 5):
|
| 253 |
+
pol = [0] * i + [1]
|
| 254 |
+
tgt = pol[:]
|
| 255 |
+
for k in range(j):
|
| 256 |
+
tgt = leg.legint(tgt, m=1)
|
| 257 |
+
res = leg.legint(pol, m=j)
|
| 258 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 259 |
+
|
| 260 |
+
# check multiple integrations with defined k
|
| 261 |
+
for i in range(5):
|
| 262 |
+
for j in range(2, 5):
|
| 263 |
+
pol = [0] * i + [1]
|
| 264 |
+
tgt = pol[:]
|
| 265 |
+
for k in range(j):
|
| 266 |
+
tgt = leg.legint(tgt, m=1, k=[k])
|
| 267 |
+
res = leg.legint(pol, m=j, k=list(range(j)))
|
| 268 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 269 |
+
|
| 270 |
+
# check multiple integrations with lbnd
|
| 271 |
+
for i in range(5):
|
| 272 |
+
for j in range(2, 5):
|
| 273 |
+
pol = [0] * i + [1]
|
| 274 |
+
tgt = pol[:]
|
| 275 |
+
for k in range(j):
|
| 276 |
+
tgt = leg.legint(tgt, m=1, k=[k], lbnd=-1)
|
| 277 |
+
res = leg.legint(pol, m=j, k=list(range(j)), lbnd=-1)
|
| 278 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 279 |
+
|
| 280 |
+
# check multiple integrations with scaling
|
| 281 |
+
for i in range(5):
|
| 282 |
+
for j in range(2, 5):
|
| 283 |
+
pol = [0] * i + [1]
|
| 284 |
+
tgt = pol[:]
|
| 285 |
+
for k in range(j):
|
| 286 |
+
tgt = leg.legint(tgt, m=1, k=[k], scl=2)
|
| 287 |
+
res = leg.legint(pol, m=j, k=list(range(j)), scl=2)
|
| 288 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 289 |
+
|
| 290 |
+
def test_legint_axis(self):
|
| 291 |
+
# check that axis keyword works
|
| 292 |
+
c2d = np.random.random((3, 4))
|
| 293 |
+
|
| 294 |
+
tgt = np.vstack([leg.legint(c) for c in c2d.T]).T
|
| 295 |
+
res = leg.legint(c2d, axis=0)
|
| 296 |
+
assert_almost_equal(res, tgt)
|
| 297 |
+
|
| 298 |
+
tgt = np.vstack([leg.legint(c) for c in c2d])
|
| 299 |
+
res = leg.legint(c2d, axis=1)
|
| 300 |
+
assert_almost_equal(res, tgt)
|
| 301 |
+
|
| 302 |
+
tgt = np.vstack([leg.legint(c, k=3) for c in c2d])
|
| 303 |
+
res = leg.legint(c2d, k=3, axis=1)
|
| 304 |
+
assert_almost_equal(res, tgt)
|
| 305 |
+
|
| 306 |
+
def test_legint_zerointord(self):
|
| 307 |
+
assert_equal(leg.legint((1, 2, 3), 0), (1, 2, 3))
|
| 308 |
+
|
| 309 |
+
|
| 310 |
+
class TestDerivative:
|
| 311 |
+
|
| 312 |
+
def test_legder(self):
|
| 313 |
+
# check exceptions
|
| 314 |
+
assert_raises(TypeError, leg.legder, [0], .5)
|
| 315 |
+
assert_raises(ValueError, leg.legder, [0], -1)
|
| 316 |
+
|
| 317 |
+
# check that zeroth derivative does nothing
|
| 318 |
+
for i in range(5):
|
| 319 |
+
tgt = [0] * i + [1]
|
| 320 |
+
res = leg.legder(tgt, m=0)
|
| 321 |
+
assert_equal(trim(res), trim(tgt))
|
| 322 |
+
|
| 323 |
+
# check that derivation is the inverse of integration
|
| 324 |
+
for i in range(5):
|
| 325 |
+
for j in range(2, 5):
|
| 326 |
+
tgt = [0] * i + [1]
|
| 327 |
+
res = leg.legder(leg.legint(tgt, m=j), m=j)
|
| 328 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 329 |
+
|
| 330 |
+
# check derivation with scaling
|
| 331 |
+
for i in range(5):
|
| 332 |
+
for j in range(2, 5):
|
| 333 |
+
tgt = [0] * i + [1]
|
| 334 |
+
res = leg.legder(leg.legint(tgt, m=j, scl=2), m=j, scl=.5)
|
| 335 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 336 |
+
|
| 337 |
+
def test_legder_axis(self):
|
| 338 |
+
# check that axis keyword works
|
| 339 |
+
c2d = np.random.random((3, 4))
|
| 340 |
+
|
| 341 |
+
tgt = np.vstack([leg.legder(c) for c in c2d.T]).T
|
| 342 |
+
res = leg.legder(c2d, axis=0)
|
| 343 |
+
assert_almost_equal(res, tgt)
|
| 344 |
+
|
| 345 |
+
tgt = np.vstack([leg.legder(c) for c in c2d])
|
| 346 |
+
res = leg.legder(c2d, axis=1)
|
| 347 |
+
assert_almost_equal(res, tgt)
|
| 348 |
+
|
| 349 |
+
def test_legder_orderhigherthancoeff(self):
|
| 350 |
+
c = (1, 2, 3, 4)
|
| 351 |
+
assert_equal(leg.legder(c, 4), [0])
|
| 352 |
+
|
| 353 |
+
class TestVander:
|
| 354 |
+
# some random values in [-1, 1)
|
| 355 |
+
x = np.random.random((3, 5)) * 2 - 1
|
| 356 |
+
|
| 357 |
+
def test_legvander(self):
|
| 358 |
+
# check for 1d x
|
| 359 |
+
x = np.arange(3)
|
| 360 |
+
v = leg.legvander(x, 3)
|
| 361 |
+
assert_(v.shape == (3, 4))
|
| 362 |
+
for i in range(4):
|
| 363 |
+
coef = [0] * i + [1]
|
| 364 |
+
assert_almost_equal(v[..., i], leg.legval(x, coef))
|
| 365 |
+
|
| 366 |
+
# check for 2d x
|
| 367 |
+
x = np.array([[1, 2], [3, 4], [5, 6]])
|
| 368 |
+
v = leg.legvander(x, 3)
|
| 369 |
+
assert_(v.shape == (3, 2, 4))
|
| 370 |
+
for i in range(4):
|
| 371 |
+
coef = [0] * i + [1]
|
| 372 |
+
assert_almost_equal(v[..., i], leg.legval(x, coef))
|
| 373 |
+
|
| 374 |
+
def test_legvander2d(self):
|
| 375 |
+
# also tests polyval2d for non-square coefficient array
|
| 376 |
+
x1, x2, x3 = self.x
|
| 377 |
+
c = np.random.random((2, 3))
|
| 378 |
+
van = leg.legvander2d(x1, x2, [1, 2])
|
| 379 |
+
tgt = leg.legval2d(x1, x2, c)
|
| 380 |
+
res = np.dot(van, c.flat)
|
| 381 |
+
assert_almost_equal(res, tgt)
|
| 382 |
+
|
| 383 |
+
# check shape
|
| 384 |
+
van = leg.legvander2d([x1], [x2], [1, 2])
|
| 385 |
+
assert_(van.shape == (1, 5, 6))
|
| 386 |
+
|
| 387 |
+
def test_legvander3d(self):
|
| 388 |
+
# also tests polyval3d for non-square coefficient array
|
| 389 |
+
x1, x2, x3 = self.x
|
| 390 |
+
c = np.random.random((2, 3, 4))
|
| 391 |
+
van = leg.legvander3d(x1, x2, x3, [1, 2, 3])
|
| 392 |
+
tgt = leg.legval3d(x1, x2, x3, c)
|
| 393 |
+
res = np.dot(van, c.flat)
|
| 394 |
+
assert_almost_equal(res, tgt)
|
| 395 |
+
|
| 396 |
+
# check shape
|
| 397 |
+
van = leg.legvander3d([x1], [x2], [x3], [1, 2, 3])
|
| 398 |
+
assert_(van.shape == (1, 5, 24))
|
| 399 |
+
|
| 400 |
+
def test_legvander_negdeg(self):
|
| 401 |
+
assert_raises(ValueError, leg.legvander, (1, 2, 3), -1)
|
| 402 |
+
|
| 403 |
+
|
| 404 |
+
class TestFitting:
|
| 405 |
+
|
| 406 |
+
def test_legfit(self):
|
| 407 |
+
def f(x):
|
| 408 |
+
return x * (x - 1) * (x - 2)
|
| 409 |
+
|
| 410 |
+
def f2(x):
|
| 411 |
+
return x**4 + x**2 + 1
|
| 412 |
+
|
| 413 |
+
# Test exceptions
|
| 414 |
+
assert_raises(ValueError, leg.legfit, [1], [1], -1)
|
| 415 |
+
assert_raises(TypeError, leg.legfit, [[1]], [1], 0)
|
| 416 |
+
assert_raises(TypeError, leg.legfit, [], [1], 0)
|
| 417 |
+
assert_raises(TypeError, leg.legfit, [1], [[[1]]], 0)
|
| 418 |
+
assert_raises(TypeError, leg.legfit, [1, 2], [1], 0)
|
| 419 |
+
assert_raises(TypeError, leg.legfit, [1], [1, 2], 0)
|
| 420 |
+
assert_raises(TypeError, leg.legfit, [1], [1], 0, w=[[1]])
|
| 421 |
+
assert_raises(TypeError, leg.legfit, [1], [1], 0, w=[1, 1])
|
| 422 |
+
assert_raises(ValueError, leg.legfit, [1], [1], [-1,])
|
| 423 |
+
assert_raises(ValueError, leg.legfit, [1], [1], [2, -1, 6])
|
| 424 |
+
assert_raises(TypeError, leg.legfit, [1], [1], [])
|
| 425 |
+
|
| 426 |
+
# Test fit
|
| 427 |
+
x = np.linspace(0, 2)
|
| 428 |
+
y = f(x)
|
| 429 |
+
#
|
| 430 |
+
coef3 = leg.legfit(x, y, 3)
|
| 431 |
+
assert_equal(len(coef3), 4)
|
| 432 |
+
assert_almost_equal(leg.legval(x, coef3), y)
|
| 433 |
+
coef3 = leg.legfit(x, y, [0, 1, 2, 3])
|
| 434 |
+
assert_equal(len(coef3), 4)
|
| 435 |
+
assert_almost_equal(leg.legval(x, coef3), y)
|
| 436 |
+
#
|
| 437 |
+
coef4 = leg.legfit(x, y, 4)
|
| 438 |
+
assert_equal(len(coef4), 5)
|
| 439 |
+
assert_almost_equal(leg.legval(x, coef4), y)
|
| 440 |
+
coef4 = leg.legfit(x, y, [0, 1, 2, 3, 4])
|
| 441 |
+
assert_equal(len(coef4), 5)
|
| 442 |
+
assert_almost_equal(leg.legval(x, coef4), y)
|
| 443 |
+
# check things still work if deg is not in strict increasing
|
| 444 |
+
coef4 = leg.legfit(x, y, [2, 3, 4, 1, 0])
|
| 445 |
+
assert_equal(len(coef4), 5)
|
| 446 |
+
assert_almost_equal(leg.legval(x, coef4), y)
|
| 447 |
+
#
|
| 448 |
+
coef2d = leg.legfit(x, np.array([y, y]).T, 3)
|
| 449 |
+
assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
|
| 450 |
+
coef2d = leg.legfit(x, np.array([y, y]).T, [0, 1, 2, 3])
|
| 451 |
+
assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
|
| 452 |
+
# test weighting
|
| 453 |
+
w = np.zeros_like(x)
|
| 454 |
+
yw = y.copy()
|
| 455 |
+
w[1::2] = 1
|
| 456 |
+
y[0::2] = 0
|
| 457 |
+
wcoef3 = leg.legfit(x, yw, 3, w=w)
|
| 458 |
+
assert_almost_equal(wcoef3, coef3)
|
| 459 |
+
wcoef3 = leg.legfit(x, yw, [0, 1, 2, 3], w=w)
|
| 460 |
+
assert_almost_equal(wcoef3, coef3)
|
| 461 |
+
#
|
| 462 |
+
wcoef2d = leg.legfit(x, np.array([yw, yw]).T, 3, w=w)
|
| 463 |
+
assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
|
| 464 |
+
wcoef2d = leg.legfit(x, np.array([yw, yw]).T, [0, 1, 2, 3], w=w)
|
| 465 |
+
assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
|
| 466 |
+
# test scaling with complex values x points whose square
|
| 467 |
+
# is zero when summed.
|
| 468 |
+
x = [1, 1j, -1, -1j]
|
| 469 |
+
assert_almost_equal(leg.legfit(x, x, 1), [0, 1])
|
| 470 |
+
assert_almost_equal(leg.legfit(x, x, [0, 1]), [0, 1])
|
| 471 |
+
# test fitting only even Legendre polynomials
|
| 472 |
+
x = np.linspace(-1, 1)
|
| 473 |
+
y = f2(x)
|
| 474 |
+
coef1 = leg.legfit(x, y, 4)
|
| 475 |
+
assert_almost_equal(leg.legval(x, coef1), y)
|
| 476 |
+
coef2 = leg.legfit(x, y, [0, 2, 4])
|
| 477 |
+
assert_almost_equal(leg.legval(x, coef2), y)
|
| 478 |
+
assert_almost_equal(coef1, coef2)
|
| 479 |
+
|
| 480 |
+
|
| 481 |
+
class TestCompanion:
|
| 482 |
+
|
| 483 |
+
def test_raises(self):
|
| 484 |
+
assert_raises(ValueError, leg.legcompanion, [])
|
| 485 |
+
assert_raises(ValueError, leg.legcompanion, [1])
|
| 486 |
+
|
| 487 |
+
def test_dimensions(self):
|
| 488 |
+
for i in range(1, 5):
|
| 489 |
+
coef = [0] * i + [1]
|
| 490 |
+
assert_(leg.legcompanion(coef).shape == (i, i))
|
| 491 |
+
|
| 492 |
+
def test_linear_root(self):
|
| 493 |
+
assert_(leg.legcompanion([1, 2])[0, 0] == -.5)
|
| 494 |
+
|
| 495 |
+
|
| 496 |
+
class TestGauss:
|
| 497 |
+
|
| 498 |
+
def test_100(self):
|
| 499 |
+
x, w = leg.leggauss(100)
|
| 500 |
+
|
| 501 |
+
# test orthogonality. Note that the results need to be normalized,
|
| 502 |
+
# otherwise the huge values that can arise from fast growing
|
| 503 |
+
# functions like Laguerre can be very confusing.
|
| 504 |
+
v = leg.legvander(x, 99)
|
| 505 |
+
vv = np.dot(v.T * w, v)
|
| 506 |
+
vd = 1 / np.sqrt(vv.diagonal())
|
| 507 |
+
vv = vd[:, None] * vv * vd
|
| 508 |
+
assert_almost_equal(vv, np.eye(100))
|
| 509 |
+
|
| 510 |
+
# check that the integral of 1 is correct
|
| 511 |
+
tgt = 2.0
|
| 512 |
+
assert_almost_equal(w.sum(), tgt)
|
| 513 |
+
|
| 514 |
+
|
| 515 |
+
class TestMisc:
|
| 516 |
+
|
| 517 |
+
def test_legfromroots(self):
|
| 518 |
+
res = leg.legfromroots([])
|
| 519 |
+
assert_almost_equal(trim(res), [1])
|
| 520 |
+
for i in range(1, 5):
|
| 521 |
+
roots = np.cos(np.linspace(-np.pi, 0, 2 * i + 1)[1::2])
|
| 522 |
+
pol = leg.legfromroots(roots)
|
| 523 |
+
res = leg.legval(roots, pol)
|
| 524 |
+
tgt = 0
|
| 525 |
+
assert_(len(pol) == i + 1)
|
| 526 |
+
assert_almost_equal(leg.leg2poly(pol)[-1], 1)
|
| 527 |
+
assert_almost_equal(res, tgt)
|
| 528 |
+
|
| 529 |
+
def test_legroots(self):
|
| 530 |
+
assert_almost_equal(leg.legroots([1]), [])
|
| 531 |
+
assert_almost_equal(leg.legroots([1, 2]), [-.5])
|
| 532 |
+
for i in range(2, 5):
|
| 533 |
+
tgt = np.linspace(-1, 1, i)
|
| 534 |
+
res = leg.legroots(leg.legfromroots(tgt))
|
| 535 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 536 |
+
|
| 537 |
+
def test_legtrim(self):
|
| 538 |
+
coef = [2, -1, 1, 0]
|
| 539 |
+
|
| 540 |
+
# Test exceptions
|
| 541 |
+
assert_raises(ValueError, leg.legtrim, coef, -1)
|
| 542 |
+
|
| 543 |
+
# Test results
|
| 544 |
+
assert_equal(leg.legtrim(coef), coef[:-1])
|
| 545 |
+
assert_equal(leg.legtrim(coef, 1), coef[:-3])
|
| 546 |
+
assert_equal(leg.legtrim(coef, 2), [0])
|
| 547 |
+
|
| 548 |
+
def test_legline(self):
|
| 549 |
+
assert_equal(leg.legline(3, 4), [3, 4])
|
| 550 |
+
|
| 551 |
+
def test_legline_zeroscl(self):
|
| 552 |
+
assert_equal(leg.legline(3, 0), [3])
|
| 553 |
+
|
| 554 |
+
def test_leg2poly(self):
|
| 555 |
+
for i in range(10):
|
| 556 |
+
assert_almost_equal(leg.leg2poly([0] * i + [1]), Llist[i])
|
| 557 |
+
|
| 558 |
+
def test_poly2leg(self):
|
| 559 |
+
for i in range(10):
|
| 560 |
+
assert_almost_equal(leg.poly2leg(Llist[i]), [0] * i + [1])
|
| 561 |
+
|
| 562 |
+
def test_weight(self):
|
| 563 |
+
x = np.linspace(-1, 1, 11)
|
| 564 |
+
tgt = 1.
|
| 565 |
+
res = leg.legweight(x)
|
| 566 |
+
assert_almost_equal(res, tgt)
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/tests/test_polynomial.py
ADDED
|
@@ -0,0 +1,691 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
+
"""Tests for polynomial module.
|
| 2 |
+
|
| 3 |
+
"""
|
| 4 |
+
import pickle
|
| 5 |
+
from copy import deepcopy
|
| 6 |
+
from fractions import Fraction
|
| 7 |
+
from functools import reduce
|
| 8 |
+
|
| 9 |
+
import pytest
|
| 10 |
+
|
| 11 |
+
import numpy as np
|
| 12 |
+
import numpy.polynomial.polynomial as poly
|
| 13 |
+
from numpy.testing import (
|
| 14 |
+
assert_,
|
| 15 |
+
assert_almost_equal,
|
| 16 |
+
assert_array_equal,
|
| 17 |
+
assert_equal,
|
| 18 |
+
assert_raises,
|
| 19 |
+
assert_raises_regex,
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def trim(x):
|
| 24 |
+
return poly.polytrim(x, tol=1e-6)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
T0 = [1]
|
| 28 |
+
T1 = [0, 1]
|
| 29 |
+
T2 = [-1, 0, 2]
|
| 30 |
+
T3 = [0, -3, 0, 4]
|
| 31 |
+
T4 = [1, 0, -8, 0, 8]
|
| 32 |
+
T5 = [0, 5, 0, -20, 0, 16]
|
| 33 |
+
T6 = [-1, 0, 18, 0, -48, 0, 32]
|
| 34 |
+
T7 = [0, -7, 0, 56, 0, -112, 0, 64]
|
| 35 |
+
T8 = [1, 0, -32, 0, 160, 0, -256, 0, 128]
|
| 36 |
+
T9 = [0, 9, 0, -120, 0, 432, 0, -576, 0, 256]
|
| 37 |
+
|
| 38 |
+
Tlist = [T0, T1, T2, T3, T4, T5, T6, T7, T8, T9]
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
class TestConstants:
|
| 42 |
+
|
| 43 |
+
def test_polydomain(self):
|
| 44 |
+
assert_equal(poly.polydomain, [-1, 1])
|
| 45 |
+
|
| 46 |
+
def test_polyzero(self):
|
| 47 |
+
assert_equal(poly.polyzero, [0])
|
| 48 |
+
|
| 49 |
+
def test_polyone(self):
|
| 50 |
+
assert_equal(poly.polyone, [1])
|
| 51 |
+
|
| 52 |
+
def test_polyx(self):
|
| 53 |
+
assert_equal(poly.polyx, [0, 1])
|
| 54 |
+
|
| 55 |
+
def test_copy(self):
|
| 56 |
+
x = poly.Polynomial([1, 2, 3])
|
| 57 |
+
y = deepcopy(x)
|
| 58 |
+
assert_equal(x, y)
|
| 59 |
+
|
| 60 |
+
def test_pickle(self):
|
| 61 |
+
x = poly.Polynomial([1, 2, 3])
|
| 62 |
+
y = pickle.loads(pickle.dumps(x))
|
| 63 |
+
assert_equal(x, y)
|
| 64 |
+
|
| 65 |
+
class TestArithmetic:
|
| 66 |
+
|
| 67 |
+
def test_polyadd(self):
|
| 68 |
+
for i in range(5):
|
| 69 |
+
for j in range(5):
|
| 70 |
+
msg = f"At i={i}, j={j}"
|
| 71 |
+
tgt = np.zeros(max(i, j) + 1)
|
| 72 |
+
tgt[i] += 1
|
| 73 |
+
tgt[j] += 1
|
| 74 |
+
res = poly.polyadd([0] * i + [1], [0] * j + [1])
|
| 75 |
+
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
| 76 |
+
|
| 77 |
+
def test_polysub(self):
|
| 78 |
+
for i in range(5):
|
| 79 |
+
for j in range(5):
|
| 80 |
+
msg = f"At i={i}, j={j}"
|
| 81 |
+
tgt = np.zeros(max(i, j) + 1)
|
| 82 |
+
tgt[i] += 1
|
| 83 |
+
tgt[j] -= 1
|
| 84 |
+
res = poly.polysub([0] * i + [1], [0] * j + [1])
|
| 85 |
+
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
| 86 |
+
|
| 87 |
+
def test_polymulx(self):
|
| 88 |
+
assert_equal(poly.polymulx([0]), [0])
|
| 89 |
+
assert_equal(poly.polymulx([1]), [0, 1])
|
| 90 |
+
for i in range(1, 5):
|
| 91 |
+
ser = [0] * i + [1]
|
| 92 |
+
tgt = [0] * (i + 1) + [1]
|
| 93 |
+
assert_equal(poly.polymulx(ser), tgt)
|
| 94 |
+
|
| 95 |
+
def test_polymul(self):
|
| 96 |
+
for i in range(5):
|
| 97 |
+
for j in range(5):
|
| 98 |
+
msg = f"At i={i}, j={j}"
|
| 99 |
+
tgt = np.zeros(i + j + 1)
|
| 100 |
+
tgt[i + j] += 1
|
| 101 |
+
res = poly.polymul([0] * i + [1], [0] * j + [1])
|
| 102 |
+
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
| 103 |
+
|
| 104 |
+
def test_polydiv(self):
|
| 105 |
+
# check zero division
|
| 106 |
+
assert_raises(ZeroDivisionError, poly.polydiv, [1], [0])
|
| 107 |
+
|
| 108 |
+
# check scalar division
|
| 109 |
+
quo, rem = poly.polydiv([2], [2])
|
| 110 |
+
assert_equal((quo, rem), (1, 0))
|
| 111 |
+
quo, rem = poly.polydiv([2, 2], [2])
|
| 112 |
+
assert_equal((quo, rem), ((1, 1), 0))
|
| 113 |
+
|
| 114 |
+
# check rest.
|
| 115 |
+
for i in range(5):
|
| 116 |
+
for j in range(5):
|
| 117 |
+
msg = f"At i={i}, j={j}"
|
| 118 |
+
ci = [0] * i + [1, 2]
|
| 119 |
+
cj = [0] * j + [1, 2]
|
| 120 |
+
tgt = poly.polyadd(ci, cj)
|
| 121 |
+
quo, rem = poly.polydiv(tgt, ci)
|
| 122 |
+
res = poly.polyadd(poly.polymul(quo, ci), rem)
|
| 123 |
+
assert_equal(res, tgt, err_msg=msg)
|
| 124 |
+
|
| 125 |
+
def test_polypow(self):
|
| 126 |
+
for i in range(5):
|
| 127 |
+
for j in range(5):
|
| 128 |
+
msg = f"At i={i}, j={j}"
|
| 129 |
+
c = np.arange(i + 1)
|
| 130 |
+
tgt = reduce(poly.polymul, [c] * j, np.array([1]))
|
| 131 |
+
res = poly.polypow(c, j)
|
| 132 |
+
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
| 133 |
+
|
| 134 |
+
class TestFraction:
|
| 135 |
+
|
| 136 |
+
def test_Fraction(self):
|
| 137 |
+
# assert we can use Polynomials with coefficients of object dtype
|
| 138 |
+
f = Fraction(2, 3)
|
| 139 |
+
one = Fraction(1, 1)
|
| 140 |
+
zero = Fraction(0, 1)
|
| 141 |
+
p = poly.Polynomial([f, f], domain=[zero, one], window=[zero, one])
|
| 142 |
+
|
| 143 |
+
x = 2 * p + p ** 2
|
| 144 |
+
assert_equal(x.coef, np.array([Fraction(16, 9), Fraction(20, 9),
|
| 145 |
+
Fraction(4, 9)], dtype=object))
|
| 146 |
+
assert_equal(p.domain, [zero, one])
|
| 147 |
+
assert_equal(p.coef.dtype, np.dtypes.ObjectDType())
|
| 148 |
+
assert_(isinstance(p(f), Fraction))
|
| 149 |
+
assert_equal(p(f), Fraction(10, 9))
|
| 150 |
+
p_deriv = poly.Polynomial([Fraction(2, 3)], domain=[zero, one],
|
| 151 |
+
window=[zero, one])
|
| 152 |
+
assert_equal(p.deriv(), p_deriv)
|
| 153 |
+
|
| 154 |
+
class TestEvaluation:
|
| 155 |
+
# coefficients of 1 + 2*x + 3*x**2
|
| 156 |
+
c1d = np.array([1., 2., 3.])
|
| 157 |
+
c2d = np.einsum('i,j->ij', c1d, c1d)
|
| 158 |
+
c3d = np.einsum('i,j,k->ijk', c1d, c1d, c1d)
|
| 159 |
+
|
| 160 |
+
# some random values in [-1, 1)
|
| 161 |
+
x = np.random.random((3, 5)) * 2 - 1
|
| 162 |
+
y = poly.polyval(x, [1., 2., 3.])
|
| 163 |
+
|
| 164 |
+
def test_polyval(self):
|
| 165 |
+
# check empty input
|
| 166 |
+
assert_equal(poly.polyval([], [1]).size, 0)
|
| 167 |
+
|
| 168 |
+
# check normal input)
|
| 169 |
+
x = np.linspace(-1, 1)
|
| 170 |
+
y = [x**i for i in range(5)]
|
| 171 |
+
for i in range(5):
|
| 172 |
+
tgt = y[i]
|
| 173 |
+
res = poly.polyval(x, [0] * i + [1])
|
| 174 |
+
assert_almost_equal(res, tgt)
|
| 175 |
+
tgt = x * (x**2 - 1)
|
| 176 |
+
res = poly.polyval(x, [0, -1, 0, 1])
|
| 177 |
+
assert_almost_equal(res, tgt)
|
| 178 |
+
|
| 179 |
+
# check that shape is preserved
|
| 180 |
+
for i in range(3):
|
| 181 |
+
dims = [2] * i
|
| 182 |
+
x = np.zeros(dims)
|
| 183 |
+
assert_equal(poly.polyval(x, [1]).shape, dims)
|
| 184 |
+
assert_equal(poly.polyval(x, [1, 0]).shape, dims)
|
| 185 |
+
assert_equal(poly.polyval(x, [1, 0, 0]).shape, dims)
|
| 186 |
+
|
| 187 |
+
# check masked arrays are processed correctly
|
| 188 |
+
mask = [False, True, False]
|
| 189 |
+
mx = np.ma.array([1, 2, 3], mask=mask)
|
| 190 |
+
res = np.polyval([7, 5, 3], mx)
|
| 191 |
+
assert_array_equal(res.mask, mask)
|
| 192 |
+
|
| 193 |
+
# check subtypes of ndarray are preserved
|
| 194 |
+
class C(np.ndarray):
|
| 195 |
+
pass
|
| 196 |
+
|
| 197 |
+
cx = np.array([1, 2, 3]).view(C)
|
| 198 |
+
assert_equal(type(np.polyval([2, 3, 4], cx)), C)
|
| 199 |
+
|
| 200 |
+
def test_polyvalfromroots(self):
|
| 201 |
+
# check exception for broadcasting x values over root array with
|
| 202 |
+
# too few dimensions
|
| 203 |
+
assert_raises(ValueError, poly.polyvalfromroots,
|
| 204 |
+
[1], [1], tensor=False)
|
| 205 |
+
|
| 206 |
+
# check empty input
|
| 207 |
+
assert_equal(poly.polyvalfromroots([], [1]).size, 0)
|
| 208 |
+
assert_(poly.polyvalfromroots([], [1]).shape == (0,))
|
| 209 |
+
|
| 210 |
+
# check empty input + multidimensional roots
|
| 211 |
+
assert_equal(poly.polyvalfromroots([], [[1] * 5]).size, 0)
|
| 212 |
+
assert_(poly.polyvalfromroots([], [[1] * 5]).shape == (5, 0))
|
| 213 |
+
|
| 214 |
+
# check scalar input
|
| 215 |
+
assert_equal(poly.polyvalfromroots(1, 1), 0)
|
| 216 |
+
assert_(poly.polyvalfromroots(1, np.ones((3, 3))).shape == (3,))
|
| 217 |
+
|
| 218 |
+
# check normal input)
|
| 219 |
+
x = np.linspace(-1, 1)
|
| 220 |
+
y = [x**i for i in range(5)]
|
| 221 |
+
for i in range(1, 5):
|
| 222 |
+
tgt = y[i]
|
| 223 |
+
res = poly.polyvalfromroots(x, [0] * i)
|
| 224 |
+
assert_almost_equal(res, tgt)
|
| 225 |
+
tgt = x * (x - 1) * (x + 1)
|
| 226 |
+
res = poly.polyvalfromroots(x, [-1, 0, 1])
|
| 227 |
+
assert_almost_equal(res, tgt)
|
| 228 |
+
|
| 229 |
+
# check that shape is preserved
|
| 230 |
+
for i in range(3):
|
| 231 |
+
dims = [2] * i
|
| 232 |
+
x = np.zeros(dims)
|
| 233 |
+
assert_equal(poly.polyvalfromroots(x, [1]).shape, dims)
|
| 234 |
+
assert_equal(poly.polyvalfromroots(x, [1, 0]).shape, dims)
|
| 235 |
+
assert_equal(poly.polyvalfromroots(x, [1, 0, 0]).shape, dims)
|
| 236 |
+
|
| 237 |
+
# check compatibility with factorization
|
| 238 |
+
ptest = [15, 2, -16, -2, 1]
|
| 239 |
+
r = poly.polyroots(ptest)
|
| 240 |
+
x = np.linspace(-1, 1)
|
| 241 |
+
assert_almost_equal(poly.polyval(x, ptest),
|
| 242 |
+
poly.polyvalfromroots(x, r))
|
| 243 |
+
|
| 244 |
+
# check multidimensional arrays of roots and values
|
| 245 |
+
# check tensor=False
|
| 246 |
+
rshape = (3, 5)
|
| 247 |
+
x = np.arange(-3, 2)
|
| 248 |
+
r = np.random.randint(-5, 5, size=rshape)
|
| 249 |
+
res = poly.polyvalfromroots(x, r, tensor=False)
|
| 250 |
+
tgt = np.empty(r.shape[1:])
|
| 251 |
+
for ii in range(tgt.size):
|
| 252 |
+
tgt[ii] = poly.polyvalfromroots(x[ii], r[:, ii])
|
| 253 |
+
assert_equal(res, tgt)
|
| 254 |
+
|
| 255 |
+
# check tensor=True
|
| 256 |
+
x = np.vstack([x, 2 * x])
|
| 257 |
+
res = poly.polyvalfromroots(x, r, tensor=True)
|
| 258 |
+
tgt = np.empty(r.shape[1:] + x.shape)
|
| 259 |
+
for ii in range(r.shape[1]):
|
| 260 |
+
for jj in range(x.shape[0]):
|
| 261 |
+
tgt[ii, jj, :] = poly.polyvalfromroots(x[jj], r[:, ii])
|
| 262 |
+
assert_equal(res, tgt)
|
| 263 |
+
|
| 264 |
+
def test_polyval2d(self):
|
| 265 |
+
x1, x2, x3 = self.x
|
| 266 |
+
y1, y2, y3 = self.y
|
| 267 |
+
|
| 268 |
+
# test exceptions
|
| 269 |
+
assert_raises_regex(ValueError, 'incompatible',
|
| 270 |
+
poly.polyval2d, x1, x2[:2], self.c2d)
|
| 271 |
+
|
| 272 |
+
# test values
|
| 273 |
+
tgt = y1 * y2
|
| 274 |
+
res = poly.polyval2d(x1, x2, self.c2d)
|
| 275 |
+
assert_almost_equal(res, tgt)
|
| 276 |
+
|
| 277 |
+
# test shape
|
| 278 |
+
z = np.ones((2, 3))
|
| 279 |
+
res = poly.polyval2d(z, z, self.c2d)
|
| 280 |
+
assert_(res.shape == (2, 3))
|
| 281 |
+
|
| 282 |
+
def test_polyval3d(self):
|
| 283 |
+
x1, x2, x3 = self.x
|
| 284 |
+
y1, y2, y3 = self.y
|
| 285 |
+
|
| 286 |
+
# test exceptions
|
| 287 |
+
assert_raises_regex(ValueError, 'incompatible',
|
| 288 |
+
poly.polyval3d, x1, x2, x3[:2], self.c3d)
|
| 289 |
+
|
| 290 |
+
# test values
|
| 291 |
+
tgt = y1 * y2 * y3
|
| 292 |
+
res = poly.polyval3d(x1, x2, x3, self.c3d)
|
| 293 |
+
assert_almost_equal(res, tgt)
|
| 294 |
+
|
| 295 |
+
# test shape
|
| 296 |
+
z = np.ones((2, 3))
|
| 297 |
+
res = poly.polyval3d(z, z, z, self.c3d)
|
| 298 |
+
assert_(res.shape == (2, 3))
|
| 299 |
+
|
| 300 |
+
def test_polygrid2d(self):
|
| 301 |
+
x1, x2, x3 = self.x
|
| 302 |
+
y1, y2, y3 = self.y
|
| 303 |
+
|
| 304 |
+
# test values
|
| 305 |
+
tgt = np.einsum('i,j->ij', y1, y2)
|
| 306 |
+
res = poly.polygrid2d(x1, x2, self.c2d)
|
| 307 |
+
assert_almost_equal(res, tgt)
|
| 308 |
+
|
| 309 |
+
# test shape
|
| 310 |
+
z = np.ones((2, 3))
|
| 311 |
+
res = poly.polygrid2d(z, z, self.c2d)
|
| 312 |
+
assert_(res.shape == (2, 3) * 2)
|
| 313 |
+
|
| 314 |
+
def test_polygrid3d(self):
|
| 315 |
+
x1, x2, x3 = self.x
|
| 316 |
+
y1, y2, y3 = self.y
|
| 317 |
+
|
| 318 |
+
# test values
|
| 319 |
+
tgt = np.einsum('i,j,k->ijk', y1, y2, y3)
|
| 320 |
+
res = poly.polygrid3d(x1, x2, x3, self.c3d)
|
| 321 |
+
assert_almost_equal(res, tgt)
|
| 322 |
+
|
| 323 |
+
# test shape
|
| 324 |
+
z = np.ones((2, 3))
|
| 325 |
+
res = poly.polygrid3d(z, z, z, self.c3d)
|
| 326 |
+
assert_(res.shape == (2, 3) * 3)
|
| 327 |
+
|
| 328 |
+
|
| 329 |
+
class TestIntegral:
|
| 330 |
+
|
| 331 |
+
def test_polyint(self):
|
| 332 |
+
# check exceptions
|
| 333 |
+
assert_raises(TypeError, poly.polyint, [0], .5)
|
| 334 |
+
assert_raises(ValueError, poly.polyint, [0], -1)
|
| 335 |
+
assert_raises(ValueError, poly.polyint, [0], 1, [0, 0])
|
| 336 |
+
assert_raises(ValueError, poly.polyint, [0], lbnd=[0])
|
| 337 |
+
assert_raises(ValueError, poly.polyint, [0], scl=[0])
|
| 338 |
+
assert_raises(TypeError, poly.polyint, [0], axis=.5)
|
| 339 |
+
assert_raises(TypeError, poly.polyint, [1, 1], 1.)
|
| 340 |
+
|
| 341 |
+
# test integration of zero polynomial
|
| 342 |
+
for i in range(2, 5):
|
| 343 |
+
k = [0] * (i - 2) + [1]
|
| 344 |
+
res = poly.polyint([0], m=i, k=k)
|
| 345 |
+
assert_almost_equal(res, [0, 1])
|
| 346 |
+
|
| 347 |
+
# check single integration with integration constant
|
| 348 |
+
for i in range(5):
|
| 349 |
+
scl = i + 1
|
| 350 |
+
pol = [0] * i + [1]
|
| 351 |
+
tgt = [i] + [0] * i + [1 / scl]
|
| 352 |
+
res = poly.polyint(pol, m=1, k=[i])
|
| 353 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 354 |
+
|
| 355 |
+
# check single integration with integration constant and lbnd
|
| 356 |
+
for i in range(5):
|
| 357 |
+
scl = i + 1
|
| 358 |
+
pol = [0] * i + [1]
|
| 359 |
+
res = poly.polyint(pol, m=1, k=[i], lbnd=-1)
|
| 360 |
+
assert_almost_equal(poly.polyval(-1, res), i)
|
| 361 |
+
|
| 362 |
+
# check single integration with integration constant and scaling
|
| 363 |
+
for i in range(5):
|
| 364 |
+
scl = i + 1
|
| 365 |
+
pol = [0] * i + [1]
|
| 366 |
+
tgt = [i] + [0] * i + [2 / scl]
|
| 367 |
+
res = poly.polyint(pol, m=1, k=[i], scl=2)
|
| 368 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 369 |
+
|
| 370 |
+
# check multiple integrations with default k
|
| 371 |
+
for i in range(5):
|
| 372 |
+
for j in range(2, 5):
|
| 373 |
+
pol = [0] * i + [1]
|
| 374 |
+
tgt = pol[:]
|
| 375 |
+
for k in range(j):
|
| 376 |
+
tgt = poly.polyint(tgt, m=1)
|
| 377 |
+
res = poly.polyint(pol, m=j)
|
| 378 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 379 |
+
|
| 380 |
+
# check multiple integrations with defined k
|
| 381 |
+
for i in range(5):
|
| 382 |
+
for j in range(2, 5):
|
| 383 |
+
pol = [0] * i + [1]
|
| 384 |
+
tgt = pol[:]
|
| 385 |
+
for k in range(j):
|
| 386 |
+
tgt = poly.polyint(tgt, m=1, k=[k])
|
| 387 |
+
res = poly.polyint(pol, m=j, k=list(range(j)))
|
| 388 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 389 |
+
|
| 390 |
+
# check multiple integrations with lbnd
|
| 391 |
+
for i in range(5):
|
| 392 |
+
for j in range(2, 5):
|
| 393 |
+
pol = [0] * i + [1]
|
| 394 |
+
tgt = pol[:]
|
| 395 |
+
for k in range(j):
|
| 396 |
+
tgt = poly.polyint(tgt, m=1, k=[k], lbnd=-1)
|
| 397 |
+
res = poly.polyint(pol, m=j, k=list(range(j)), lbnd=-1)
|
| 398 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 399 |
+
|
| 400 |
+
# check multiple integrations with scaling
|
| 401 |
+
for i in range(5):
|
| 402 |
+
for j in range(2, 5):
|
| 403 |
+
pol = [0] * i + [1]
|
| 404 |
+
tgt = pol[:]
|
| 405 |
+
for k in range(j):
|
| 406 |
+
tgt = poly.polyint(tgt, m=1, k=[k], scl=2)
|
| 407 |
+
res = poly.polyint(pol, m=j, k=list(range(j)), scl=2)
|
| 408 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 409 |
+
|
| 410 |
+
def test_polyint_axis(self):
|
| 411 |
+
# check that axis keyword works
|
| 412 |
+
c2d = np.random.random((3, 4))
|
| 413 |
+
|
| 414 |
+
tgt = np.vstack([poly.polyint(c) for c in c2d.T]).T
|
| 415 |
+
res = poly.polyint(c2d, axis=0)
|
| 416 |
+
assert_almost_equal(res, tgt)
|
| 417 |
+
|
| 418 |
+
tgt = np.vstack([poly.polyint(c) for c in c2d])
|
| 419 |
+
res = poly.polyint(c2d, axis=1)
|
| 420 |
+
assert_almost_equal(res, tgt)
|
| 421 |
+
|
| 422 |
+
tgt = np.vstack([poly.polyint(c, k=3) for c in c2d])
|
| 423 |
+
res = poly.polyint(c2d, k=3, axis=1)
|
| 424 |
+
assert_almost_equal(res, tgt)
|
| 425 |
+
|
| 426 |
+
|
| 427 |
+
class TestDerivative:
|
| 428 |
+
|
| 429 |
+
def test_polyder(self):
|
| 430 |
+
# check exceptions
|
| 431 |
+
assert_raises(TypeError, poly.polyder, [0], .5)
|
| 432 |
+
assert_raises(ValueError, poly.polyder, [0], -1)
|
| 433 |
+
|
| 434 |
+
# check that zeroth derivative does nothing
|
| 435 |
+
for i in range(5):
|
| 436 |
+
tgt = [0] * i + [1]
|
| 437 |
+
res = poly.polyder(tgt, m=0)
|
| 438 |
+
assert_equal(trim(res), trim(tgt))
|
| 439 |
+
|
| 440 |
+
# check that derivation is the inverse of integration
|
| 441 |
+
for i in range(5):
|
| 442 |
+
for j in range(2, 5):
|
| 443 |
+
tgt = [0] * i + [1]
|
| 444 |
+
res = poly.polyder(poly.polyint(tgt, m=j), m=j)
|
| 445 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 446 |
+
|
| 447 |
+
# check derivation with scaling
|
| 448 |
+
for i in range(5):
|
| 449 |
+
for j in range(2, 5):
|
| 450 |
+
tgt = [0] * i + [1]
|
| 451 |
+
res = poly.polyder(poly.polyint(tgt, m=j, scl=2), m=j, scl=.5)
|
| 452 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 453 |
+
|
| 454 |
+
def test_polyder_axis(self):
|
| 455 |
+
# check that axis keyword works
|
| 456 |
+
c2d = np.random.random((3, 4))
|
| 457 |
+
|
| 458 |
+
tgt = np.vstack([poly.polyder(c) for c in c2d.T]).T
|
| 459 |
+
res = poly.polyder(c2d, axis=0)
|
| 460 |
+
assert_almost_equal(res, tgt)
|
| 461 |
+
|
| 462 |
+
tgt = np.vstack([poly.polyder(c) for c in c2d])
|
| 463 |
+
res = poly.polyder(c2d, axis=1)
|
| 464 |
+
assert_almost_equal(res, tgt)
|
| 465 |
+
|
| 466 |
+
|
| 467 |
+
class TestVander:
|
| 468 |
+
# some random values in [-1, 1)
|
| 469 |
+
x = np.random.random((3, 5)) * 2 - 1
|
| 470 |
+
|
| 471 |
+
def test_polyvander(self):
|
| 472 |
+
# check for 1d x
|
| 473 |
+
x = np.arange(3)
|
| 474 |
+
v = poly.polyvander(x, 3)
|
| 475 |
+
assert_(v.shape == (3, 4))
|
| 476 |
+
for i in range(4):
|
| 477 |
+
coef = [0] * i + [1]
|
| 478 |
+
assert_almost_equal(v[..., i], poly.polyval(x, coef))
|
| 479 |
+
|
| 480 |
+
# check for 2d x
|
| 481 |
+
x = np.array([[1, 2], [3, 4], [5, 6]])
|
| 482 |
+
v = poly.polyvander(x, 3)
|
| 483 |
+
assert_(v.shape == (3, 2, 4))
|
| 484 |
+
for i in range(4):
|
| 485 |
+
coef = [0] * i + [1]
|
| 486 |
+
assert_almost_equal(v[..., i], poly.polyval(x, coef))
|
| 487 |
+
|
| 488 |
+
def test_polyvander2d(self):
|
| 489 |
+
# also tests polyval2d for non-square coefficient array
|
| 490 |
+
x1, x2, x3 = self.x
|
| 491 |
+
c = np.random.random((2, 3))
|
| 492 |
+
van = poly.polyvander2d(x1, x2, [1, 2])
|
| 493 |
+
tgt = poly.polyval2d(x1, x2, c)
|
| 494 |
+
res = np.dot(van, c.flat)
|
| 495 |
+
assert_almost_equal(res, tgt)
|
| 496 |
+
|
| 497 |
+
# check shape
|
| 498 |
+
van = poly.polyvander2d([x1], [x2], [1, 2])
|
| 499 |
+
assert_(van.shape == (1, 5, 6))
|
| 500 |
+
|
| 501 |
+
def test_polyvander3d(self):
|
| 502 |
+
# also tests polyval3d for non-square coefficient array
|
| 503 |
+
x1, x2, x3 = self.x
|
| 504 |
+
c = np.random.random((2, 3, 4))
|
| 505 |
+
van = poly.polyvander3d(x1, x2, x3, [1, 2, 3])
|
| 506 |
+
tgt = poly.polyval3d(x1, x2, x3, c)
|
| 507 |
+
res = np.dot(van, c.flat)
|
| 508 |
+
assert_almost_equal(res, tgt)
|
| 509 |
+
|
| 510 |
+
# check shape
|
| 511 |
+
van = poly.polyvander3d([x1], [x2], [x3], [1, 2, 3])
|
| 512 |
+
assert_(van.shape == (1, 5, 24))
|
| 513 |
+
|
| 514 |
+
def test_polyvandernegdeg(self):
|
| 515 |
+
x = np.arange(3)
|
| 516 |
+
assert_raises(ValueError, poly.polyvander, x, -1)
|
| 517 |
+
|
| 518 |
+
|
| 519 |
+
class TestCompanion:
|
| 520 |
+
|
| 521 |
+
def test_raises(self):
|
| 522 |
+
assert_raises(ValueError, poly.polycompanion, [])
|
| 523 |
+
assert_raises(ValueError, poly.polycompanion, [1])
|
| 524 |
+
|
| 525 |
+
def test_dimensions(self):
|
| 526 |
+
for i in range(1, 5):
|
| 527 |
+
coef = [0] * i + [1]
|
| 528 |
+
assert_(poly.polycompanion(coef).shape == (i, i))
|
| 529 |
+
|
| 530 |
+
def test_linear_root(self):
|
| 531 |
+
assert_(poly.polycompanion([1, 2])[0, 0] == -.5)
|
| 532 |
+
|
| 533 |
+
|
| 534 |
+
class TestMisc:
|
| 535 |
+
|
| 536 |
+
def test_polyfromroots(self):
|
| 537 |
+
res = poly.polyfromroots([])
|
| 538 |
+
assert_almost_equal(trim(res), [1])
|
| 539 |
+
for i in range(1, 5):
|
| 540 |
+
roots = np.cos(np.linspace(-np.pi, 0, 2 * i + 1)[1::2])
|
| 541 |
+
tgt = Tlist[i]
|
| 542 |
+
res = poly.polyfromroots(roots) * 2**(i - 1)
|
| 543 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 544 |
+
|
| 545 |
+
def test_polyroots(self):
|
| 546 |
+
assert_almost_equal(poly.polyroots([1]), [])
|
| 547 |
+
assert_almost_equal(poly.polyroots([1, 2]), [-.5])
|
| 548 |
+
for i in range(2, 5):
|
| 549 |
+
tgt = np.linspace(-1, 1, i)
|
| 550 |
+
res = poly.polyroots(poly.polyfromroots(tgt))
|
| 551 |
+
assert_almost_equal(trim(res), trim(tgt))
|
| 552 |
+
|
| 553 |
+
# Testing for larger root values
|
| 554 |
+
for i in np.logspace(10, 25, num=1000, base=10):
|
| 555 |
+
tgt = np.array([-1, 1, i])
|
| 556 |
+
res = poly.polyroots(poly.polyfromroots(tgt))
|
| 557 |
+
# Adapting the expected precision according to the root value,
|
| 558 |
+
# to take into account numerical calculation error.
|
| 559 |
+
assert_almost_equal(res, tgt, 15 - int(np.log10(i)))
|
| 560 |
+
for i in np.logspace(10, 25, num=1000, base=10):
|
| 561 |
+
tgt = np.array([-1, 1.01, i])
|
| 562 |
+
res = poly.polyroots(poly.polyfromroots(tgt))
|
| 563 |
+
# Adapting the expected precision according to the root value,
|
| 564 |
+
# to take into account numerical calculation error.
|
| 565 |
+
assert_almost_equal(res, tgt, 14 - int(np.log10(i)))
|
| 566 |
+
|
| 567 |
+
def test_polyfit(self):
|
| 568 |
+
def f(x):
|
| 569 |
+
return x * (x - 1) * (x - 2)
|
| 570 |
+
|
| 571 |
+
def f2(x):
|
| 572 |
+
return x**4 + x**2 + 1
|
| 573 |
+
|
| 574 |
+
# Test exceptions
|
| 575 |
+
assert_raises(ValueError, poly.polyfit, [1], [1], -1)
|
| 576 |
+
assert_raises(TypeError, poly.polyfit, [[1]], [1], 0)
|
| 577 |
+
assert_raises(TypeError, poly.polyfit, [], [1], 0)
|
| 578 |
+
assert_raises(TypeError, poly.polyfit, [1], [[[1]]], 0)
|
| 579 |
+
assert_raises(TypeError, poly.polyfit, [1, 2], [1], 0)
|
| 580 |
+
assert_raises(TypeError, poly.polyfit, [1], [1, 2], 0)
|
| 581 |
+
assert_raises(TypeError, poly.polyfit, [1], [1], 0, w=[[1]])
|
| 582 |
+
assert_raises(TypeError, poly.polyfit, [1], [1], 0, w=[1, 1])
|
| 583 |
+
assert_raises(ValueError, poly.polyfit, [1], [1], [-1,])
|
| 584 |
+
assert_raises(ValueError, poly.polyfit, [1], [1], [2, -1, 6])
|
| 585 |
+
assert_raises(TypeError, poly.polyfit, [1], [1], [])
|
| 586 |
+
|
| 587 |
+
# Test fit
|
| 588 |
+
x = np.linspace(0, 2)
|
| 589 |
+
y = f(x)
|
| 590 |
+
#
|
| 591 |
+
coef3 = poly.polyfit(x, y, 3)
|
| 592 |
+
assert_equal(len(coef3), 4)
|
| 593 |
+
assert_almost_equal(poly.polyval(x, coef3), y)
|
| 594 |
+
coef3 = poly.polyfit(x, y, [0, 1, 2, 3])
|
| 595 |
+
assert_equal(len(coef3), 4)
|
| 596 |
+
assert_almost_equal(poly.polyval(x, coef3), y)
|
| 597 |
+
#
|
| 598 |
+
coef4 = poly.polyfit(x, y, 4)
|
| 599 |
+
assert_equal(len(coef4), 5)
|
| 600 |
+
assert_almost_equal(poly.polyval(x, coef4), y)
|
| 601 |
+
coef4 = poly.polyfit(x, y, [0, 1, 2, 3, 4])
|
| 602 |
+
assert_equal(len(coef4), 5)
|
| 603 |
+
assert_almost_equal(poly.polyval(x, coef4), y)
|
| 604 |
+
#
|
| 605 |
+
coef2d = poly.polyfit(x, np.array([y, y]).T, 3)
|
| 606 |
+
assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
|
| 607 |
+
coef2d = poly.polyfit(x, np.array([y, y]).T, [0, 1, 2, 3])
|
| 608 |
+
assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
|
| 609 |
+
# test weighting
|
| 610 |
+
w = np.zeros_like(x)
|
| 611 |
+
yw = y.copy()
|
| 612 |
+
w[1::2] = 1
|
| 613 |
+
yw[0::2] = 0
|
| 614 |
+
wcoef3 = poly.polyfit(x, yw, 3, w=w)
|
| 615 |
+
assert_almost_equal(wcoef3, coef3)
|
| 616 |
+
wcoef3 = poly.polyfit(x, yw, [0, 1, 2, 3], w=w)
|
| 617 |
+
assert_almost_equal(wcoef3, coef3)
|
| 618 |
+
#
|
| 619 |
+
wcoef2d = poly.polyfit(x, np.array([yw, yw]).T, 3, w=w)
|
| 620 |
+
assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
|
| 621 |
+
wcoef2d = poly.polyfit(x, np.array([yw, yw]).T, [0, 1, 2, 3], w=w)
|
| 622 |
+
assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
|
| 623 |
+
# test scaling with complex values x points whose square
|
| 624 |
+
# is zero when summed.
|
| 625 |
+
x = [1, 1j, -1, -1j]
|
| 626 |
+
assert_almost_equal(poly.polyfit(x, x, 1), [0, 1])
|
| 627 |
+
assert_almost_equal(poly.polyfit(x, x, [0, 1]), [0, 1])
|
| 628 |
+
# test fitting only even Polyendre polynomials
|
| 629 |
+
x = np.linspace(-1, 1)
|
| 630 |
+
y = f2(x)
|
| 631 |
+
coef1 = poly.polyfit(x, y, 4)
|
| 632 |
+
assert_almost_equal(poly.polyval(x, coef1), y)
|
| 633 |
+
coef2 = poly.polyfit(x, y, [0, 2, 4])
|
| 634 |
+
assert_almost_equal(poly.polyval(x, coef2), y)
|
| 635 |
+
assert_almost_equal(coef1, coef2)
|
| 636 |
+
|
| 637 |
+
def test_polytrim(self):
|
| 638 |
+
coef = [2, -1, 1, 0]
|
| 639 |
+
|
| 640 |
+
# Test exceptions
|
| 641 |
+
assert_raises(ValueError, poly.polytrim, coef, -1)
|
| 642 |
+
|
| 643 |
+
# Test results
|
| 644 |
+
assert_equal(poly.polytrim(coef), coef[:-1])
|
| 645 |
+
assert_equal(poly.polytrim(coef, 1), coef[:-3])
|
| 646 |
+
assert_equal(poly.polytrim(coef, 2), [0])
|
| 647 |
+
|
| 648 |
+
def test_polyline(self):
|
| 649 |
+
assert_equal(poly.polyline(3, 4), [3, 4])
|
| 650 |
+
|
| 651 |
+
def test_polyline_zero(self):
|
| 652 |
+
assert_equal(poly.polyline(3, 0), [3])
|
| 653 |
+
|
| 654 |
+
def test_fit_degenerate_domain(self):
|
| 655 |
+
p = poly.Polynomial.fit([1], [2], deg=0)
|
| 656 |
+
assert_equal(p.coef, [2.])
|
| 657 |
+
p = poly.Polynomial.fit([1, 1], [2, 2.1], deg=0)
|
| 658 |
+
assert_almost_equal(p.coef, [2.05])
|
| 659 |
+
with pytest.warns(np.exceptions.RankWarning):
|
| 660 |
+
p = poly.Polynomial.fit([1, 1], [2, 2.1], deg=1)
|
| 661 |
+
|
| 662 |
+
def test_result_type(self):
|
| 663 |
+
w = np.array([-1, 1], dtype=np.float32)
|
| 664 |
+
p = np.polynomial.Polynomial(w, domain=w, window=w)
|
| 665 |
+
v = p(2)
|
| 666 |
+
assert_equal(v.dtype, np.float32)
|
| 667 |
+
|
| 668 |
+
arr = np.polydiv(1, np.float32(1))
|
| 669 |
+
assert_equal(arr[0].dtype, np.float64)
|
| 670 |
+
|
| 671 |
+
class ArrayFunctionInterceptor:
|
| 672 |
+
def __init__(self):
|
| 673 |
+
self.called = False
|
| 674 |
+
|
| 675 |
+
def __array_function__(self, func, types, args, kwargs):
|
| 676 |
+
self.called = True
|
| 677 |
+
return "intercepted"
|
| 678 |
+
|
| 679 |
+
def test_polyval2d_array_function_hook():
|
| 680 |
+
x = ArrayFunctionInterceptor()
|
| 681 |
+
y = ArrayFunctionInterceptor()
|
| 682 |
+
c = ArrayFunctionInterceptor()
|
| 683 |
+
result = np.polynomial.polynomial.polyval2d(x, y, c)
|
| 684 |
+
assert result == "intercepted"
|
| 685 |
+
|
| 686 |
+
def test_polygrid2d_array_function_hook():
|
| 687 |
+
x = ArrayFunctionInterceptor()
|
| 688 |
+
y = ArrayFunctionInterceptor()
|
| 689 |
+
c = ArrayFunctionInterceptor()
|
| 690 |
+
result = np.polynomial.polynomial.polygrid2d(x, y, c)
|
| 691 |
+
assert result == "intercepted"
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/tests/test_polyutils.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for polyutils module.
|
| 2 |
+
|
| 3 |
+
"""
|
| 4 |
+
import numpy as np
|
| 5 |
+
import numpy.polynomial.polyutils as pu
|
| 6 |
+
from numpy.testing import assert_, assert_almost_equal, assert_equal, assert_raises
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class TestMisc:
|
| 10 |
+
|
| 11 |
+
def test_trimseq(self):
|
| 12 |
+
tgt = [1]
|
| 13 |
+
for num_trailing_zeros in range(5):
|
| 14 |
+
res = pu.trimseq([1] + [0] * num_trailing_zeros)
|
| 15 |
+
assert_equal(res, tgt)
|
| 16 |
+
|
| 17 |
+
def test_trimseq_empty_input(self):
|
| 18 |
+
for empty_seq in [[], np.array([], dtype=np.int32)]:
|
| 19 |
+
assert_equal(pu.trimseq(empty_seq), empty_seq)
|
| 20 |
+
|
| 21 |
+
def test_as_series(self):
|
| 22 |
+
# check exceptions
|
| 23 |
+
assert_raises(ValueError, pu.as_series, [[]])
|
| 24 |
+
assert_raises(ValueError, pu.as_series, [[[1, 2]]])
|
| 25 |
+
assert_raises(ValueError, pu.as_series, [[1], ['a']])
|
| 26 |
+
# check common types
|
| 27 |
+
types = ['i', 'd', 'O']
|
| 28 |
+
for i in range(len(types)):
|
| 29 |
+
for j in range(i):
|
| 30 |
+
ci = np.ones(1, types[i])
|
| 31 |
+
cj = np.ones(1, types[j])
|
| 32 |
+
[resi, resj] = pu.as_series([ci, cj])
|
| 33 |
+
assert_(resi.dtype.char == resj.dtype.char)
|
| 34 |
+
assert_(resj.dtype.char == types[i])
|
| 35 |
+
|
| 36 |
+
def test_trimcoef(self):
|
| 37 |
+
coef = [2, -1, 1, 0]
|
| 38 |
+
# Test exceptions
|
| 39 |
+
assert_raises(ValueError, pu.trimcoef, coef, -1)
|
| 40 |
+
# Test results
|
| 41 |
+
assert_equal(pu.trimcoef(coef), coef[:-1])
|
| 42 |
+
assert_equal(pu.trimcoef(coef, 1), coef[:-3])
|
| 43 |
+
assert_equal(pu.trimcoef(coef, 2), [0])
|
| 44 |
+
|
| 45 |
+
def test_vander_nd_exception(self):
|
| 46 |
+
# n_dims != len(points)
|
| 47 |
+
assert_raises(ValueError, pu._vander_nd, (), (1, 2, 3), [90])
|
| 48 |
+
# n_dims != len(degrees)
|
| 49 |
+
assert_raises(ValueError, pu._vander_nd, (), (), [90.65])
|
| 50 |
+
# n_dims == 0
|
| 51 |
+
assert_raises(ValueError, pu._vander_nd, (), (), [])
|
| 52 |
+
|
| 53 |
+
def test_div_zerodiv(self):
|
| 54 |
+
# c2[-1] == 0
|
| 55 |
+
assert_raises(ZeroDivisionError, pu._div, pu._div, (1, 2, 3), [0])
|
| 56 |
+
|
| 57 |
+
def test_pow_too_large(self):
|
| 58 |
+
# power > maxpower
|
| 59 |
+
assert_raises(ValueError, pu._pow, (), [1, 2, 3], 5, 4)
|
| 60 |
+
|
| 61 |
+
class TestDomain:
|
| 62 |
+
|
| 63 |
+
def test_getdomain(self):
|
| 64 |
+
# test for real values
|
| 65 |
+
x = [1, 10, 3, -1]
|
| 66 |
+
tgt = [-1, 10]
|
| 67 |
+
res = pu.getdomain(x)
|
| 68 |
+
assert_almost_equal(res, tgt)
|
| 69 |
+
|
| 70 |
+
# test for complex values
|
| 71 |
+
x = [1 + 1j, 1 - 1j, 0, 2]
|
| 72 |
+
tgt = [-1j, 2 + 1j]
|
| 73 |
+
res = pu.getdomain(x)
|
| 74 |
+
assert_almost_equal(res, tgt)
|
| 75 |
+
|
| 76 |
+
def test_mapdomain(self):
|
| 77 |
+
# test for real values
|
| 78 |
+
dom1 = [0, 4]
|
| 79 |
+
dom2 = [1, 3]
|
| 80 |
+
tgt = dom2
|
| 81 |
+
res = pu.mapdomain(dom1, dom1, dom2)
|
| 82 |
+
assert_almost_equal(res, tgt)
|
| 83 |
+
|
| 84 |
+
# test for complex values
|
| 85 |
+
dom1 = [0 - 1j, 2 + 1j]
|
| 86 |
+
dom2 = [-2, 2]
|
| 87 |
+
tgt = dom2
|
| 88 |
+
x = dom1
|
| 89 |
+
res = pu.mapdomain(x, dom1, dom2)
|
| 90 |
+
assert_almost_equal(res, tgt)
|
| 91 |
+
|
| 92 |
+
# test for multidimensional arrays
|
| 93 |
+
dom1 = [0, 4]
|
| 94 |
+
dom2 = [1, 3]
|
| 95 |
+
tgt = np.array([dom2, dom2])
|
| 96 |
+
x = np.array([dom1, dom1])
|
| 97 |
+
res = pu.mapdomain(x, dom1, dom2)
|
| 98 |
+
assert_almost_equal(res, tgt)
|
| 99 |
+
|
| 100 |
+
# test that subtypes are preserved.
|
| 101 |
+
class MyNDArray(np.ndarray):
|
| 102 |
+
pass
|
| 103 |
+
|
| 104 |
+
dom1 = [0, 4]
|
| 105 |
+
dom2 = [1, 3]
|
| 106 |
+
x = np.array([dom1, dom1]).view(MyNDArray)
|
| 107 |
+
res = pu.mapdomain(x, dom1, dom2)
|
| 108 |
+
assert_(isinstance(res, MyNDArray))
|
| 109 |
+
|
| 110 |
+
def test_mapparms(self):
|
| 111 |
+
# test for real values
|
| 112 |
+
dom1 = [0, 4]
|
| 113 |
+
dom2 = [1, 3]
|
| 114 |
+
tgt = [1, .5]
|
| 115 |
+
res = pu. mapparms(dom1, dom2)
|
| 116 |
+
assert_almost_equal(res, tgt)
|
| 117 |
+
|
| 118 |
+
# test for complex values
|
| 119 |
+
dom1 = [0 - 1j, 2 + 1j]
|
| 120 |
+
dom2 = [-2, 2]
|
| 121 |
+
tgt =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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from decimal import Decimal
|
| 2 |
+
|
| 3 |
+
# For testing polynomial printing with object arrays
|
| 4 |
+
from fractions import Fraction
|
| 5 |
+
from math import inf, nan
|
| 6 |
+
|
| 7 |
+
import pytest
|
| 8 |
+
|
| 9 |
+
import numpy.polynomial as poly
|
| 10 |
+
from numpy._core import arange, array, printoptions
|
| 11 |
+
from numpy.testing import assert_, assert_equal
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class TestStrUnicodeSuperSubscripts:
|
| 15 |
+
|
| 16 |
+
@pytest.fixture(scope='class', autouse=True)
|
| 17 |
+
def use_unicode(self):
|
| 18 |
+
poly.set_default_printstyle('unicode')
|
| 19 |
+
|
| 20 |
+
@pytest.mark.parametrize(('inp', 'tgt'), (
|
| 21 |
+
([1, 2, 3], "1.0 + 2.0·x + 3.0·x²"),
|
| 22 |
+
([-1, 0, 3, -1], "-1.0 + 0.0·x + 3.0·x² - 1.0·x³"),
|
| 23 |
+
(arange(12), ("0.0 + 1.0·x + 2.0·x² + 3.0·x³ + 4.0·x⁴ + 5.0·x⁵ + "
|
| 24 |
+
"6.0·x⁶ + 7.0·x⁷ +\n8.0·x⁸ + 9.0·x⁹ + 10.0·x¹⁰ + "
|
| 25 |
+
"11.0·x¹¹")),
|
| 26 |
+
))
|
| 27 |
+
def test_polynomial_str(self, inp, tgt):
|
| 28 |
+
p = poly.Polynomial(inp)
|
| 29 |
+
res = str(p)
|
| 30 |
+
assert_equal(res, tgt)
|
| 31 |
+
|
| 32 |
+
@pytest.mark.parametrize(('inp', 'tgt'), (
|
| 33 |
+
([1, 2, 3], "1.0 + 2.0·T₁(x) + 3.0·T₂(x)"),
|
| 34 |
+
([-1, 0, 3, -1], "-1.0 + 0.0·T₁(x) + 3.0·T₂(x) - 1.0·T₃(x)"),
|
| 35 |
+
(arange(12), ("0.0 + 1.0·T₁(x) + 2.0·T₂(x) + 3.0·T₃(x) + 4.0·T₄(x) + "
|
| 36 |
+
"5.0·T₅(x) +\n6.0·T₆(x) + 7.0·T₇(x) + 8.0·T₈(x) + "
|
| 37 |
+
"9.0·T₉(x) + 10.0·T₁₀(x) + 11.0·T₁₁(x)")),
|
| 38 |
+
))
|
| 39 |
+
def test_chebyshev_str(self, inp, tgt):
|
| 40 |
+
res = str(poly.Chebyshev(inp))
|
| 41 |
+
assert_equal(res, tgt)
|
| 42 |
+
|
| 43 |
+
@pytest.mark.parametrize(('inp', 'tgt'), (
|
| 44 |
+
([1, 2, 3], "1.0 + 2.0·P₁(x) + 3.0·P₂(x)"),
|
| 45 |
+
([-1, 0, 3, -1], "-1.0 + 0.0·P₁(x) + 3.0·P₂(x) - 1.0·P₃(x)"),
|
| 46 |
+
(arange(12), ("0.0 + 1.0·P₁(x) + 2.0·P₂(x) + 3.0·P₃(x) + 4.0·P₄(x) + "
|
| 47 |
+
"5.0·P₅(x) +\n6.0·P₆(x) + 7.0·P₇(x) + 8.0·P₈(x) + "
|
| 48 |
+
"9.0·P₉(x) + 10.0·P₁₀(x) + 11.0·P₁₁(x)")),
|
| 49 |
+
))
|
| 50 |
+
def test_legendre_str(self, inp, tgt):
|
| 51 |
+
res = str(poly.Legendre(inp))
|
| 52 |
+
assert_equal(res, tgt)
|
| 53 |
+
|
| 54 |
+
@pytest.mark.parametrize(('inp', 'tgt'), (
|
| 55 |
+
([1, 2, 3], "1.0 + 2.0·H₁(x) + 3.0·H₂(x)"),
|
| 56 |
+
([-1, 0, 3, -1], "-1.0 + 0.0·H₁(x) + 3.0·H₂(x) - 1.0·H₃(x)"),
|
| 57 |
+
(arange(12), ("0.0 + 1.0·H₁(x) + 2.0·H₂(x) + 3.0·H₃(x) + 4.0·H₄(x) + "
|
| 58 |
+
"5.0·H₅(x) +\n6.0·H₆(x) + 7.0·H₇(x) + 8.0·H₈(x) + "
|
| 59 |
+
"9.0·H₉(x) + 10.0·H₁₀(x) + 11.0·H₁₁(x)")),
|
| 60 |
+
))
|
| 61 |
+
def test_hermite_str(self, inp, tgt):
|
| 62 |
+
res = str(poly.Hermite(inp))
|
| 63 |
+
assert_equal(res, tgt)
|
| 64 |
+
|
| 65 |
+
@pytest.mark.parametrize(('inp', 'tgt'), (
|
| 66 |
+
([1, 2, 3], "1.0 + 2.0·He₁(x) + 3.0·He₂(x)"),
|
| 67 |
+
([-1, 0, 3, -1], "-1.0 + 0.0·He₁(x) + 3.0·He₂(x) - 1.0·He₃(x)"),
|
| 68 |
+
(arange(12), ("0.0 + 1.0·He₁(x) + 2.0·He₂(x) + 3.0·He₃(x) + "
|
| 69 |
+
"4.0·He₄(x) + 5.0·He₅(x) +\n6.0·He₆(x) + 7.0·He₇(x) + "
|
| 70 |
+
"8.0·He₈(x) + 9.0·He₉(x) + 10.0·He₁₀(x) +\n"
|
| 71 |
+
"11.0·He₁₁(x)")),
|
| 72 |
+
))
|
| 73 |
+
def test_hermiteE_str(self, inp, tgt):
|
| 74 |
+
res = str(poly.HermiteE(inp))
|
| 75 |
+
assert_equal(res, tgt)
|
| 76 |
+
|
| 77 |
+
@pytest.mark.parametrize(('inp', 'tgt'), (
|
| 78 |
+
([1, 2, 3], "1.0 + 2.0·L₁(x) + 3.0·L₂(x)"),
|
| 79 |
+
([-1, 0, 3, -1], "-1.0 + 0.0·L₁(x) + 3.0·L₂(x) - 1.0·L₃(x)"),
|
| 80 |
+
(arange(12), ("0.0 + 1.0·L₁(x) + 2.0·L₂(x) + 3.0·L₃(x) + 4.0·L₄(x) + "
|
| 81 |
+
"5.0·L₅(x) +\n6.0·L₆(x) + 7.0·L₇(x) + 8.0·L₈(x) + "
|
| 82 |
+
"9.0·L₉(x) + 10.0·L₁₀(x) + 11.0·L₁₁(x)")),
|
| 83 |
+
))
|
| 84 |
+
def test_laguerre_str(self, inp, tgt):
|
| 85 |
+
res = str(poly.Laguerre(inp))
|
| 86 |
+
assert_equal(res, tgt)
|
| 87 |
+
|
| 88 |
+
def test_polynomial_str_domains(self):
|
| 89 |
+
res = str(poly.Polynomial([0, 1]))
|
| 90 |
+
tgt = '0.0 + 1.0·x'
|
| 91 |
+
assert_equal(res, tgt)
|
| 92 |
+
|
| 93 |
+
res = str(poly.Polynomial([0, 1], domain=[1, 2]))
|
| 94 |
+
tgt = '0.0 + 1.0·(-3.0 + 2.0x)'
|
| 95 |
+
assert_equal(res, tgt)
|
| 96 |
+
|
| 97 |
+
class TestStrAscii:
|
| 98 |
+
|
| 99 |
+
@pytest.fixture(scope='class', autouse=True)
|
| 100 |
+
def use_ascii(self):
|
| 101 |
+
poly.set_default_printstyle('ascii')
|
| 102 |
+
|
| 103 |
+
@pytest.mark.parametrize(('inp', 'tgt'), (
|
| 104 |
+
([1, 2, 3], "1.0 + 2.0 x + 3.0 x**2"),
|
| 105 |
+
([-1, 0, 3, -1], "-1.0 + 0.0 x + 3.0 x**2 - 1.0 x**3"),
|
| 106 |
+
(arange(12), ("0.0 + 1.0 x + 2.0 x**2 + 3.0 x**3 + 4.0 x**4 + "
|
| 107 |
+
"5.0 x**5 + 6.0 x**6 +\n7.0 x**7 + 8.0 x**8 + "
|
| 108 |
+
"9.0 x**9 + 10.0 x**10 + 11.0 x**11")),
|
| 109 |
+
))
|
| 110 |
+
def test_polynomial_str(self, inp, tgt):
|
| 111 |
+
res = str(poly.Polynomial(inp))
|
| 112 |
+
assert_equal(res, tgt)
|
| 113 |
+
|
| 114 |
+
@pytest.mark.parametrize(('inp', 'tgt'), (
|
| 115 |
+
([1, 2, 3], "1.0 + 2.0 T_1(x) + 3.0 T_2(x)"),
|
| 116 |
+
([-1, 0, 3, -1], "-1.0 + 0.0 T_1(x) + 3.0 T_2(x) - 1.0 T_3(x)"),
|
| 117 |
+
(arange(12), ("0.0 + 1.0 T_1(x) + 2.0 T_2(x) + 3.0 T_3(x) + "
|
| 118 |
+
"4.0 T_4(x) + 5.0 T_5(x) +\n6.0 T_6(x) + 7.0 T_7(x) + "
|
| 119 |
+
"8.0 T_8(x) + 9.0 T_9(x) + 10.0 T_10(x) +\n"
|
| 120 |
+
"11.0 T_11(x)")),
|
| 121 |
+
))
|
| 122 |
+
def test_chebyshev_str(self, inp, tgt):
|
| 123 |
+
res = str(poly.Chebyshev(inp))
|
| 124 |
+
assert_equal(res, tgt)
|
| 125 |
+
|
| 126 |
+
@pytest.mark.parametrize(('inp', 'tgt'), (
|
| 127 |
+
([1, 2, 3], "1.0 + 2.0 P_1(x) + 3.0 P_2(x)"),
|
| 128 |
+
([-1, 0, 3, -1], "-1.0 + 0.0 P_1(x) + 3.0 P_2(x) - 1.0 P_3(x)"),
|
| 129 |
+
(arange(12), ("0.0 + 1.0 P_1(x) + 2.0 P_2(x) + 3.0 P_3(x) + "
|
| 130 |
+
"4.0 P_4(x) + 5.0 P_5(x) +\n6.0 P_6(x) + 7.0 P_7(x) + "
|
| 131 |
+
"8.0 P_8(x) + 9.0 P_9(x) + 10.0 P_10(x) +\n"
|
| 132 |
+
"11.0 P_11(x)")),
|
| 133 |
+
))
|
| 134 |
+
def test_legendre_str(self, inp, tgt):
|
| 135 |
+
res = str(poly.Legendre(inp))
|
| 136 |
+
assert_equal(res, tgt)
|
| 137 |
+
|
| 138 |
+
@pytest.mark.parametrize(('inp', 'tgt'), (
|
| 139 |
+
([1, 2, 3], "1.0 + 2.0 H_1(x) + 3.0 H_2(x)"),
|
| 140 |
+
([-1, 0, 3, -1], "-1.0 + 0.0 H_1(x) + 3.0 H_2(x) - 1.0 H_3(x)"),
|
| 141 |
+
(arange(12), ("0.0 + 1.0 H_1(x) + 2.0 H_2(x) + 3.0 H_3(x) + "
|
| 142 |
+
"4.0 H_4(x) + 5.0 H_5(x) +\n6.0 H_6(x) + 7.0 H_7(x) + "
|
| 143 |
+
"8.0 H_8(x) + 9.0 H_9(x) + 10.0 H_10(x) +\n"
|
| 144 |
+
"11.0 H_11(x)")),
|
| 145 |
+
))
|
| 146 |
+
def test_hermite_str(self, inp, tgt):
|
| 147 |
+
res = str(poly.Hermite(inp))
|
| 148 |
+
assert_equal(res, tgt)
|
| 149 |
+
|
| 150 |
+
@pytest.mark.parametrize(('inp', 'tgt'), (
|
| 151 |
+
([1, 2, 3], "1.0 + 2.0 He_1(x) + 3.0 He_2(x)"),
|
| 152 |
+
([-1, 0, 3, -1], "-1.0 + 0.0 He_1(x) + 3.0 He_2(x) - 1.0 He_3(x)"),
|
| 153 |
+
(arange(12), ("0.0 + 1.0 He_1(x) + 2.0 He_2(x) + 3.0 He_3(x) + "
|
| 154 |
+
"4.0 He_4(x) +\n5.0 He_5(x) + 6.0 He_6(x) + "
|
| 155 |
+
"7.0 He_7(x) + 8.0 He_8(x) + 9.0 He_9(x) +\n"
|
| 156 |
+
"10.0 He_10(x) + 11.0 He_11(x)")),
|
| 157 |
+
))
|
| 158 |
+
def test_hermiteE_str(self, inp, tgt):
|
| 159 |
+
res = str(poly.HermiteE(inp))
|
| 160 |
+
assert_equal(res, tgt)
|
| 161 |
+
|
| 162 |
+
@pytest.mark.parametrize(('inp', 'tgt'), (
|
| 163 |
+
([1, 2, 3], "1.0 + 2.0 L_1(x) + 3.0 L_2(x)"),
|
| 164 |
+
([-1, 0, 3, -1], "-1.0 + 0.0 L_1(x) + 3.0 L_2(x) - 1.0 L_3(x)"),
|
| 165 |
+
(arange(12), ("0.0 + 1.0 L_1(x) + 2.0 L_2(x) + 3.0 L_3(x) + "
|
| 166 |
+
"4.0 L_4(x) + 5.0 L_5(x) +\n6.0 L_6(x) + 7.0 L_7(x) + "
|
| 167 |
+
"8.0 L_8(x) + 9.0 L_9(x) + 10.0 L_10(x) +\n"
|
| 168 |
+
"11.0 L_11(x)")),
|
| 169 |
+
))
|
| 170 |
+
def test_laguerre_str(self, inp, tgt):
|
| 171 |
+
res = str(poly.Laguerre(inp))
|
| 172 |
+
assert_equal(res, tgt)
|
| 173 |
+
|
| 174 |
+
def test_polynomial_str_domains(self):
|
| 175 |
+
res = str(poly.Polynomial([0, 1]))
|
| 176 |
+
tgt = '0.0 + 1.0 x'
|
| 177 |
+
assert_equal(res, tgt)
|
| 178 |
+
|
| 179 |
+
res = str(poly.Polynomial([0, 1], domain=[1, 2]))
|
| 180 |
+
tgt = '0.0 + 1.0 (-3.0 + 2.0x)'
|
| 181 |
+
assert_equal(res, tgt)
|
| 182 |
+
|
| 183 |
+
class TestLinebreaking:
|
| 184 |
+
|
| 185 |
+
@pytest.fixture(scope='class', autouse=True)
|
| 186 |
+
def use_ascii(self):
|
| 187 |
+
poly.set_default_printstyle('ascii')
|
| 188 |
+
|
| 189 |
+
def test_single_line_one_less(self):
|
| 190 |
+
# With 'ascii' style, len(str(p)) is default linewidth - 1 (i.e. 74)
|
| 191 |
+
p = poly.Polynomial([12345678, 12345678, 12345678, 12345678, 123])
|
| 192 |
+
assert_equal(len(str(p)), 74)
|
| 193 |
+
assert_equal(str(p), (
|
| 194 |
+
'12345678.0 + 12345678.0 x + 12345678.0 x**2 + '
|
| 195 |
+
'12345678.0 x**3 + 123.0 x**4'
|
| 196 |
+
))
|
| 197 |
+
|
| 198 |
+
def test_num_chars_is_linewidth(self):
|
| 199 |
+
# len(str(p)) == default linewidth == 75
|
| 200 |
+
p = poly.Polynomial([12345678, 12345678, 12345678, 12345678, 1234])
|
| 201 |
+
assert_equal(len(str(p)), 75)
|
| 202 |
+
assert_equal(str(p), (
|
| 203 |
+
'12345678.0 + 12345678.0 x + 12345678.0 x**2 + '
|
| 204 |
+
'12345678.0 x**3 +\n1234.0 x**4'
|
| 205 |
+
))
|
| 206 |
+
|
| 207 |
+
def test_first_linebreak_multiline_one_less_than_linewidth(self):
|
| 208 |
+
# Multiline str where len(first_line) + len(next_term) == lw - 1 == 74
|
| 209 |
+
p = poly.Polynomial(
|
| 210 |
+
[12345678, 12345678, 12345678, 12345678, 1, 12345678]
|
| 211 |
+
)
|
| 212 |
+
assert_equal(len(str(p).split('\n')[0]), 74)
|
| 213 |
+
assert_equal(str(p), (
|
| 214 |
+
'12345678.0 + 12345678.0 x + 12345678.0 x**2 + '
|
| 215 |
+
'12345678.0 x**3 + 1.0 x**4 +\n12345678.0 x**5'
|
| 216 |
+
))
|
| 217 |
+
|
| 218 |
+
def test_first_linebreak_multiline_on_linewidth(self):
|
| 219 |
+
# First line is one character longer than previous test
|
| 220 |
+
p = poly.Polynomial(
|
| 221 |
+
[12345678, 12345678, 12345678, 12345678.12, 1, 12345678]
|
| 222 |
+
)
|
| 223 |
+
assert_equal(str(p), (
|
| 224 |
+
'12345678.0 + 12345678.0 x + 12345678.0 x**2 + '
|
| 225 |
+
'12345678.12 x**3 +\n1.0 x**4 + 12345678.0 x**5'
|
| 226 |
+
))
|
| 227 |
+
|
| 228 |
+
@pytest.mark.parametrize(('lw', 'tgt'), (
|
| 229 |
+
(75, ('0.0 + 10.0 x + 200.0 x**2 + 3000.0 x**3 + 40000.0 x**4 + '
|
| 230 |
+
'500000.0 x**5 +\n600000.0 x**6 + 70000.0 x**7 + 8000.0 x**8 + '
|
| 231 |
+
'900.0 x**9')),
|
| 232 |
+
(45, ('0.0 + 10.0 x + 200.0 x**2 + 3000.0 x**3 +\n40000.0 x**4 + '
|
| 233 |
+
'500000.0 x**5 +\n600000.0 x**6 + 70000.0 x**7 + 8000.0 x**8 +\n'
|
| 234 |
+
'900.0 x**9')),
|
| 235 |
+
(132, ('0.0 + 10.0 x + 200.0 x**2 + 3000.0 x**3 + 40000.0 x**4 + '
|
| 236 |
+
'500000.0 x**5 + 600000.0 x**6 + 70000.0 x**7 + 8000.0 x**8 + '
|
| 237 |
+
'900.0 x**9')),
|
| 238 |
+
))
|
| 239 |
+
def test_linewidth_printoption(self, lw, tgt):
|
| 240 |
+
p = poly.Polynomial(
|
| 241 |
+
[0, 10, 200, 3000, 40000, 500000, 600000, 70000, 8000, 900]
|
| 242 |
+
)
|
| 243 |
+
with printoptions(linewidth=lw):
|
| 244 |
+
assert_equal(str(p), tgt)
|
| 245 |
+
for line in str(p).split('\n'):
|
| 246 |
+
assert_(len(line) < lw)
|
| 247 |
+
|
| 248 |
+
|
| 249 |
+
@pytest.mark.thread_unsafe(reason="set_default_printstyle() is global state")
|
| 250 |
+
def test_set_default_printoptions():
|
| 251 |
+
p = poly.Polynomial([1, 2, 3])
|
| 252 |
+
c = poly.Chebyshev([1, 2, 3])
|
| 253 |
+
poly.set_default_printstyle('ascii')
|
| 254 |
+
assert_equal(str(p), "1.0 + 2.0 x + 3.0 x**2")
|
| 255 |
+
assert_equal(str(c), "1.0 + 2.0 T_1(x) + 3.0 T_2(x)")
|
| 256 |
+
poly.set_default_printstyle('unicode')
|
| 257 |
+
assert_equal(str(p), "1.0 + 2.0·x + 3.0·x²")
|
| 258 |
+
assert_equal(str(c), "1.0 + 2.0·T₁(x) + 3.0·T₂(x)")
|
| 259 |
+
with pytest.raises(ValueError):
|
| 260 |
+
poly.set_default_printstyle('invalid_input')
|
| 261 |
+
|
| 262 |
+
|
| 263 |
+
@pytest.mark.thread_unsafe(reason="set_default_printstyle() is global state")
|
| 264 |
+
def test_complex_coefficients():
|
| 265 |
+
"""Test both numpy and built-in complex."""
|
| 266 |
+
coefs = [0 + 1j, 1 + 1j, -2 + 2j, 3 + 0j]
|
| 267 |
+
# numpy complex
|
| 268 |
+
p1 = poly.Polynomial(coefs)
|
| 269 |
+
# Python complex
|
| 270 |
+
p2 = poly.Polynomial(array(coefs, dtype=object))
|
| 271 |
+
poly.set_default_printstyle('unicode')
|
| 272 |
+
assert_equal(str(p1), "1j + (1+1j)·x - (2-2j)·x² + (3+0j)·x³")
|
| 273 |
+
assert_equal(str(p2), "1j + (1+1j)·x + (-2+2j)·x² + (3+0j)·x³")
|
| 274 |
+
poly.set_default_printstyle('ascii')
|
| 275 |
+
assert_equal(str(p1), "1j + (1+1j) x - (2-2j) x**2 + (3+0j) x**3")
|
| 276 |
+
assert_equal(str(p2), "1j + (1+1j) x + (-2+2j) x**2 + (3+0j) x**3")
|
| 277 |
+
|
| 278 |
+
|
| 279 |
+
@pytest.mark.parametrize(('coefs', 'tgt'), (
|
| 280 |
+
(array([Fraction(1, 2), Fraction(3, 4)], dtype=object), (
|
| 281 |
+
"1/2 + 3/4·x"
|
| 282 |
+
)),
|
| 283 |
+
(array([1, 2, Fraction(5, 7)], dtype=object), (
|
| 284 |
+
"1 + 2·x + 5/7·x²"
|
| 285 |
+
)),
|
| 286 |
+
(array([Decimal('1.00'), Decimal('2.2'), 3], dtype=object), (
|
| 287 |
+
"1.00 + 2.2·x + 3·x²"
|
| 288 |
+
)),
|
| 289 |
+
))
|
| 290 |
+
def test_numeric_object_coefficients(coefs, tgt):
|
| 291 |
+
p = poly.Polynomial(coefs)
|
| 292 |
+
poly.set_default_printstyle('unicode')
|
| 293 |
+
assert_equal(str(p), tgt)
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
@pytest.mark.parametrize(('coefs', 'tgt'), (
|
| 297 |
+
(array([1, 2, 'f'], dtype=object), '1 + 2·x + f·x²'),
|
| 298 |
+
(array([1, 2, [3, 4]], dtype=object), '1 + 2·x + [3, 4]·x²'),
|
| 299 |
+
))
|
| 300 |
+
def test_nonnumeric_object_coefficients(coefs, tgt):
|
| 301 |
+
"""
|
| 302 |
+
Test coef fallback for object arrays of non-numeric coefficients.
|
| 303 |
+
"""
|
| 304 |
+
p = poly.Polynomial(coefs)
|
| 305 |
+
poly.set_default_printstyle('unicode')
|
| 306 |
+
assert_equal(str(p), tgt)
|
| 307 |
+
|
| 308 |
+
|
| 309 |
+
class TestFormat:
|
| 310 |
+
def test_format_unicode(self):
|
| 311 |
+
poly.set_default_printstyle('ascii')
|
| 312 |
+
p = poly.Polynomial([1, 2, 0, -1])
|
| 313 |
+
assert_equal(format(p, 'unicode'), "1.0 + 2.0·x + 0.0·x² - 1.0·x³")
|
| 314 |
+
|
| 315 |
+
def test_format_ascii(self):
|
| 316 |
+
poly.set_default_printstyle('unicode')
|
| 317 |
+
p = poly.Polynomial([1, 2, 0, -1])
|
| 318 |
+
assert_equal(
|
| 319 |
+
format(p, 'ascii'), "1.0 + 2.0 x + 0.0 x**2 - 1.0 x**3"
|
| 320 |
+
)
|
| 321 |
+
|
| 322 |
+
def test_empty_formatstr(self):
|
| 323 |
+
poly.set_default_printstyle('ascii')
|
| 324 |
+
p = poly.Polynomial([1, 2, 3])
|
| 325 |
+
assert_equal(format(p), "1.0 + 2.0 x + 3.0 x**2")
|
| 326 |
+
assert_equal(f"{p}", "1.0 + 2.0 x + 3.0 x**2")
|
| 327 |
+
|
| 328 |
+
def test_bad_formatstr(self):
|
| 329 |
+
p = poly.Polynomial([1, 2, 0, -1])
|
| 330 |
+
with pytest.raises(ValueError):
|
| 331 |
+
format(p, '.2f')
|
| 332 |
+
|
| 333 |
+
|
| 334 |
+
@pytest.mark.parametrize(('poly', 'tgt'), (
|
| 335 |
+
(poly.Polynomial, '1.0 + 2.0·z + 3.0·z²'),
|
| 336 |
+
(poly.Chebyshev, '1.0 + 2.0·T₁(z) + 3.0·T₂(z)'),
|
| 337 |
+
(poly.Hermite, '1.0 + 2.0·H₁(z) + 3.0·H₂(z)'),
|
| 338 |
+
(poly.HermiteE, '1.0 + 2.0·He₁(z) + 3.0·He₂(z)'),
|
| 339 |
+
(poly.Laguerre, '1.0 + 2.0·L₁(z) + 3.0·L₂(z)'),
|
| 340 |
+
(poly.Legendre, '1.0 + 2.0·P₁(z) + 3.0·P₂(z)'),
|
| 341 |
+
))
|
| 342 |
+
def test_symbol(poly, tgt):
|
| 343 |
+
p = poly([1, 2, 3], symbol='z')
|
| 344 |
+
assert_equal(f"{p:unicode}", tgt)
|
| 345 |
+
|
| 346 |
+
|
| 347 |
+
class TestRepr:
|
| 348 |
+
def test_polynomial_repr(self):
|
| 349 |
+
res = repr(poly.Polynomial([0, 1]))
|
| 350 |
+
tgt = (
|
| 351 |
+
"Polynomial([0., 1.], domain=[-1., 1.], window=[-1., 1.], "
|
| 352 |
+
"symbol='x')"
|
| 353 |
+
)
|
| 354 |
+
assert_equal(res, tgt)
|
| 355 |
+
|
| 356 |
+
def test_chebyshev_repr(self):
|
| 357 |
+
res = repr(poly.Chebyshev([0, 1]))
|
| 358 |
+
tgt = (
|
| 359 |
+
"Chebyshev([0., 1.], domain=[-1., 1.], window=[-1., 1.], "
|
| 360 |
+
"symbol='x')"
|
| 361 |
+
)
|
| 362 |
+
assert_equal(res, tgt)
|
| 363 |
+
|
| 364 |
+
def test_legendre_repr(self):
|
| 365 |
+
res = repr(poly.Legendre([0, 1]))
|
| 366 |
+
tgt = (
|
| 367 |
+
"Legendre([0., 1.], domain=[-1., 1.], window=[-1., 1.], "
|
| 368 |
+
"symbol='x')"
|
| 369 |
+
)
|
| 370 |
+
assert_equal(res, tgt)
|
| 371 |
+
|
| 372 |
+
def test_hermite_repr(self):
|
| 373 |
+
res = repr(poly.Hermite([0, 1]))
|
| 374 |
+
tgt = (
|
| 375 |
+
"Hermite([0., 1.], domain=[-1., 1.], window=[-1., 1.], "
|
| 376 |
+
"symbol='x')"
|
| 377 |
+
)
|
| 378 |
+
assert_equal(res, tgt)
|
| 379 |
+
|
| 380 |
+
def test_hermiteE_repr(self):
|
| 381 |
+
res = repr(poly.HermiteE([0, 1]))
|
| 382 |
+
tgt = (
|
| 383 |
+
"HermiteE([0., 1.], domain=[-1., 1.], window=[-1., 1.], "
|
| 384 |
+
"symbol='x')"
|
| 385 |
+
)
|
| 386 |
+
assert_equal(res, tgt)
|
| 387 |
+
|
| 388 |
+
def test_laguerre_repr(self):
|
| 389 |
+
res = repr(poly.Laguerre([0, 1]))
|
| 390 |
+
tgt = (
|
| 391 |
+
"Laguerre([0., 1.], domain=[0., 1.], window=[0., 1.], "
|
| 392 |
+
"symbol='x')"
|
| 393 |
+
)
|
| 394 |
+
assert_equal(res, tgt)
|
| 395 |
+
|
| 396 |
+
|
| 397 |
+
class TestLatexRepr:
|
| 398 |
+
"""Test the latex repr used by Jupyter"""
|
| 399 |
+
|
| 400 |
+
@staticmethod
|
| 401 |
+
def as_latex(obj):
|
| 402 |
+
# right now we ignore the formatting of scalars in our tests, since
|
| 403 |
+
# it makes them too verbose. Ideally, the formatting of scalars will
|
| 404 |
+
# be fixed such that tests below continue to pass
|
| 405 |
+
obj._repr_latex_scalar = lambda x, parens=False: str(x)
|
| 406 |
+
try:
|
| 407 |
+
return obj._repr_latex_()
|
| 408 |
+
finally:
|
| 409 |
+
del obj._repr_latex_scalar
|
| 410 |
+
|
| 411 |
+
def test_simple_polynomial(self):
|
| 412 |
+
# default input
|
| 413 |
+
p = poly.Polynomial([1, 2, 3])
|
| 414 |
+
assert_equal(self.as_latex(p),
|
| 415 |
+
r'$x \mapsto 1.0 + 2.0\,x + 3.0\,x^{2}$')
|
| 416 |
+
|
| 417 |
+
# translated input
|
| 418 |
+
p = poly.Polynomial([1, 2, 3], domain=[-2, 0])
|
| 419 |
+
assert_equal(self.as_latex(p),
|
| 420 |
+
r'$x \mapsto 1.0 + 2.0\,\left(1.0 + x\right) + 3.0\,\left(1.0 + x\right)^{2}$') # noqa: E501
|
| 421 |
+
|
| 422 |
+
# scaled input
|
| 423 |
+
p = poly.Polynomial([1, 2, 3], domain=[-0.5, 0.5])
|
| 424 |
+
assert_equal(self.as_latex(p),
|
| 425 |
+
r'$x \mapsto 1.0 + 2.0\,\left(2.0x\right) + 3.0\,\left(2.0x\right)^{2}$')
|
| 426 |
+
|
| 427 |
+
# affine input
|
| 428 |
+
p = poly.Polynomial([1, 2, 3], domain=[-1, 0])
|
| 429 |
+
assert_equal(self.as_latex(p),
|
| 430 |
+
r'$x \mapsto 1.0 + 2.0\,\left(1.0 + 2.0x\right) + 3.0\,\left(1.0 + 2.0x\right)^{2}$') # noqa: E501
|
| 431 |
+
|
| 432 |
+
def test_basis_func(self):
|
| 433 |
+
p = poly.Chebyshev([1, 2, 3])
|
| 434 |
+
assert_equal(self.as_latex(p),
|
| 435 |
+
r'$x \mapsto 1.0\,{T}_{0}(x) + 2.0\,{T}_{1}(x) + 3.0\,{T}_{2}(x)$')
|
| 436 |
+
# affine input - check no surplus parens are added
|
| 437 |
+
p = poly.Chebyshev([1, 2, 3], domain=[-1, 0])
|
| 438 |
+
assert_equal(self.as_latex(p),
|
| 439 |
+
r'$x \mapsto 1.0\,{T}_{0}(1.0 + 2.0x) + 2.0\,{T}_{1}(1.0 + 2.0x) + 3.0\,{T}_{2}(1.0 + 2.0x)$') # noqa: E501
|
| 440 |
+
|
| 441 |
+
def test_multichar_basis_func(self):
|
| 442 |
+
p = poly.HermiteE([1, 2, 3])
|
| 443 |
+
assert_equal(self.as_latex(p),
|
| 444 |
+
r'$x \mapsto 1.0\,{He}_{0}(x) + 2.0\,{He}_{1}(x) + 3.0\,{He}_{2}(x)$')
|
| 445 |
+
|
| 446 |
+
def test_symbol_basic(self):
|
| 447 |
+
# default input
|
| 448 |
+
p = poly.Polynomial([1, 2, 3], symbol='z')
|
| 449 |
+
assert_equal(self.as_latex(p),
|
| 450 |
+
r'$z \mapsto 1.0 + 2.0\,z + 3.0\,z^{2}$')
|
| 451 |
+
|
| 452 |
+
# translated input
|
| 453 |
+
p = poly.Polynomial([1, 2, 3], domain=[-2, 0], symbol='z')
|
| 454 |
+
assert_equal(
|
| 455 |
+
self.as_latex(p),
|
| 456 |
+
(
|
| 457 |
+
r'$z \mapsto 1.0 + 2.0\,\left(1.0 + z\right) + 3.0\,'
|
| 458 |
+
r'\left(1.0 + z\right)^{2}$'
|
| 459 |
+
),
|
| 460 |
+
)
|
| 461 |
+
|
| 462 |
+
# scaled input
|
| 463 |
+
p = poly.Polynomial([1, 2, 3], domain=[-0.5, 0.5], symbol='z')
|
| 464 |
+
assert_equal(
|
| 465 |
+
self.as_latex(p),
|
| 466 |
+
(
|
| 467 |
+
r'$z \mapsto 1.0 + 2.0\,\left(2.0z\right) + 3.0\,'
|
| 468 |
+
r'\left(2.0z\right)^{2}$'
|
| 469 |
+
),
|
| 470 |
+
)
|
| 471 |
+
|
| 472 |
+
# affine input
|
| 473 |
+
p = poly.Polynomial([1, 2, 3], domain=[-1, 0], symbol='z')
|
| 474 |
+
assert_equal(
|
| 475 |
+
self.as_latex(p),
|
| 476 |
+
(
|
| 477 |
+
r'$z \mapsto 1.0 + 2.0\,\left(1.0 + 2.0z\right) + 3.0\,'
|
| 478 |
+
r'\left(1.0 + 2.0z\right)^{2}$'
|
| 479 |
+
),
|
| 480 |
+
)
|
| 481 |
+
|
| 482 |
+
def test_numeric_object_coefficients(self):
|
| 483 |
+
coefs = array([Fraction(1, 2), Fraction(1)])
|
| 484 |
+
p = poly.Polynomial(coefs)
|
| 485 |
+
assert_equal(self.as_latex(p), '$x \\mapsto 1/2 + 1\\,x$')
|
| 486 |
+
|
| 487 |
+
|
| 488 |
+
SWITCH_TO_EXP = (
|
| 489 |
+
'1.0 + (1.0e-01) x + (1.0e-02) x**2',
|
| 490 |
+
'1.2 + (1.2e-01) x + (1.2e-02) x**2',
|
| 491 |
+
'1.23 + 0.12 x + (1.23e-02) x**2 + (1.23e-03) x**3',
|
| 492 |
+
'1.235 + 0.123 x + (1.235e-02) x**2 + (1.235e-03) x**3',
|
| 493 |
+
'1.2346 + 0.1235 x + 0.0123 x**2 + (1.2346e-03) x**3 + (1.2346e-04) x**4',
|
| 494 |
+
'1.23457 + 0.12346 x + 0.01235 x**2 + (1.23457e-03) x**3 + '
|
| 495 |
+
'(1.23457e-04) x**4',
|
| 496 |
+
'1.234568 + 0.123457 x + 0.012346 x**2 + 0.001235 x**3 + '
|
| 497 |
+
'(1.234568e-04) x**4 + (1.234568e-05) x**5',
|
| 498 |
+
'1.2345679 + 0.1234568 x + 0.0123457 x**2 + 0.0012346 x**3 + '
|
| 499 |
+
'(1.2345679e-04) x**4 + (1.2345679e-05) x**5')
|
| 500 |
+
|
| 501 |
+
class TestPrintOptions:
|
| 502 |
+
"""
|
| 503 |
+
Test the output is properly configured via printoptions.
|
| 504 |
+
The exponential notation is enabled automatically when the values
|
| 505 |
+
are too small or too large.
|
| 506 |
+
"""
|
| 507 |
+
|
| 508 |
+
@pytest.fixture(scope='class', autouse=True)
|
| 509 |
+
def use_ascii(self):
|
| 510 |
+
poly.set_default_printstyle('ascii')
|
| 511 |
+
|
| 512 |
+
def test_str(self):
|
| 513 |
+
p = poly.Polynomial([1 / 2, 1 / 7, 1 / 7 * 10**8, 1 / 7 * 10**9])
|
| 514 |
+
assert_equal(str(p), '0.5 + 0.14285714 x + 14285714.28571429 x**2 '
|
| 515 |
+
'+ (1.42857143e+08) x**3')
|
| 516 |
+
|
| 517 |
+
with printoptions(precision=3):
|
| 518 |
+
assert_equal(str(p), '0.5 + 0.143 x + 14285714.286 x**2 '
|
| 519 |
+
'+ (1.429e+08) x**3')
|
| 520 |
+
|
| 521 |
+
def test_latex(self):
|
| 522 |
+
p = poly.Polynomial([1 / 2, 1 / 7, 1 / 7 * 10**8, 1 / 7 * 10**9])
|
| 523 |
+
assert_equal(p._repr_latex_(),
|
| 524 |
+
r'$x \mapsto \text{0.5} + \text{0.14285714}\,x + '
|
| 525 |
+
r'\text{14285714.28571429}\,x^{2} + '
|
| 526 |
+
r'\text{(1.42857143e+08)}\,x^{3}$')
|
| 527 |
+
|
| 528 |
+
with printoptions(precision=3):
|
| 529 |
+
assert_equal(p._repr_latex_(),
|
| 530 |
+
r'$x \mapsto \text{0.5} + \text{0.143}\,x + '
|
| 531 |
+
r'\text{14285714.286}\,x^{2} + \text{(1.429e+08)}\,x^{3}$')
|
| 532 |
+
|
| 533 |
+
def test_fixed(self):
|
| 534 |
+
p = poly.Polynomial([1 / 2])
|
| 535 |
+
assert_equal(str(p), '0.5')
|
| 536 |
+
|
| 537 |
+
with printoptions(floatmode='fixed'):
|
| 538 |
+
assert_equal(str(p), '0.50000000')
|
| 539 |
+
|
| 540 |
+
with printoptions(floatmode='fixed', precision=4):
|
| 541 |
+
assert_equal(str(p), '0.5000')
|
| 542 |
+
|
| 543 |
+
def test_switch_to_exp(self):
|
| 544 |
+
for i, s in enumerate(SWITCH_TO_EXP):
|
| 545 |
+
with printoptions(precision=i):
|
| 546 |
+
p = poly.Polynomial([1.23456789 * 10**-i
|
| 547 |
+
for i in range(i // 2 + 3)])
|
| 548 |
+
assert str(p).replace('\n', ' ') == s
|
| 549 |
+
|
| 550 |
+
def test_non_finite(self):
|
| 551 |
+
p = poly.Polynomial([nan, inf])
|
| 552 |
+
assert str(p) == 'nan + inf x'
|
| 553 |
+
assert p._repr_latex_() == r'$x \mapsto \text{nan} + \text{inf}\,x$' # noqa: RUF027
|
| 554 |
+
with printoptions(nanstr='NAN', infstr='INF'):
|
| 555 |
+
assert str(p) == 'NAN + INF x'
|
| 556 |
+
assert p._repr_latex_() == \
|
| 557 |
+
r'$x \mapsto \text{NAN} + \text{INF}\,x$'
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/polynomial/tests/test_symbol.py
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
Tests related to the ``symbol`` attribute of the ABCPolyBase class.
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import pytest
|
| 6 |
+
|
| 7 |
+
import numpy.polynomial as poly
|
| 8 |
+
from numpy._core import array
|
| 9 |
+
from numpy.testing import assert_, assert_equal, assert_raises
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class TestInit:
|
| 13 |
+
"""
|
| 14 |
+
Test polynomial creation with symbol kwarg.
|
| 15 |
+
"""
|
| 16 |
+
c = [1, 2, 3]
|
| 17 |
+
|
| 18 |
+
def test_default_symbol(self):
|
| 19 |
+
p = poly.Polynomial(self.c)
|
| 20 |
+
assert_equal(p.symbol, 'x')
|
| 21 |
+
|
| 22 |
+
@pytest.mark.parametrize(('bad_input', 'exception'), (
|
| 23 |
+
('', ValueError),
|
| 24 |
+
('3', ValueError),
|
| 25 |
+
(None, TypeError),
|
| 26 |
+
(1, TypeError),
|
| 27 |
+
))
|
| 28 |
+
def test_symbol_bad_input(self, bad_input, exception):
|
| 29 |
+
with pytest.raises(exception):
|
| 30 |
+
p = poly.Polynomial(self.c, symbol=bad_input)
|
| 31 |
+
|
| 32 |
+
@pytest.mark.parametrize('symbol', (
|
| 33 |
+
'x',
|
| 34 |
+
'x_1',
|
| 35 |
+
'A',
|
| 36 |
+
'xyz',
|
| 37 |
+
'β',
|
| 38 |
+
))
|
| 39 |
+
def test_valid_symbols(self, symbol):
|
| 40 |
+
"""
|
| 41 |
+
Values for symbol that should pass input validation.
|
| 42 |
+
"""
|
| 43 |
+
p = poly.Polynomial(self.c, symbol=symbol)
|
| 44 |
+
assert_equal(p.symbol, symbol)
|
| 45 |
+
|
| 46 |
+
def test_property(self):
|
| 47 |
+
"""
|
| 48 |
+
'symbol' attribute is read only.
|
| 49 |
+
"""
|
| 50 |
+
p = poly.Polynomial(self.c, symbol='x')
|
| 51 |
+
with pytest.raises(AttributeError):
|
| 52 |
+
p.symbol = 'z'
|
| 53 |
+
|
| 54 |
+
def test_change_symbol(self):
|
| 55 |
+
p = poly.Polynomial(self.c, symbol='y')
|
| 56 |
+
# Create new polynomial from p with different symbol
|
| 57 |
+
pt = poly.Polynomial(p.coef, symbol='t')
|
| 58 |
+
assert_equal(pt.symbol, 't')
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
class TestUnaryOperators:
|
| 62 |
+
p = poly.Polynomial([1, 2, 3], symbol='z')
|
| 63 |
+
|
| 64 |
+
def test_neg(self):
|
| 65 |
+
n = -self.p
|
| 66 |
+
assert_equal(n.symbol, 'z')
|
| 67 |
+
|
| 68 |
+
def test_scalarmul(self):
|
| 69 |
+
out = self.p * 10
|
| 70 |
+
assert_equal(out.symbol, 'z')
|
| 71 |
+
|
| 72 |
+
def test_rscalarmul(self):
|
| 73 |
+
out = 10 * self.p
|
| 74 |
+
assert_equal(out.symbol, 'z')
|
| 75 |
+
|
| 76 |
+
def test_pow(self):
|
| 77 |
+
out = self.p ** 3
|
| 78 |
+
assert_equal(out.symbol, 'z')
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
@pytest.mark.parametrize(
|
| 82 |
+
'rhs',
|
| 83 |
+
(
|
| 84 |
+
poly.Polynomial([4, 5, 6], symbol='z'),
|
| 85 |
+
array([4, 5, 6]),
|
| 86 |
+
),
|
| 87 |
+
)
|
| 88 |
+
class TestBinaryOperatorsSameSymbol:
|
| 89 |
+
"""
|
| 90 |
+
Ensure symbol is preserved for numeric operations on polynomials with
|
| 91 |
+
the same symbol
|
| 92 |
+
"""
|
| 93 |
+
p = poly.Polynomial([1, 2, 3], symbol='z')
|
| 94 |
+
|
| 95 |
+
def test_add(self, rhs):
|
| 96 |
+
out = self.p + rhs
|
| 97 |
+
assert_equal(out.symbol, 'z')
|
| 98 |
+
|
| 99 |
+
def test_sub(self, rhs):
|
| 100 |
+
out = self.p - rhs
|
| 101 |
+
assert_equal(out.symbol, 'z')
|
| 102 |
+
|
| 103 |
+
def test_polymul(self, rhs):
|
| 104 |
+
out = self.p * rhs
|
| 105 |
+
assert_equal(out.symbol, 'z')
|
| 106 |
+
|
| 107 |
+
def test_divmod(self, rhs):
|
| 108 |
+
for out in divmod(self.p, rhs):
|
| 109 |
+
assert_equal(out.symbol, 'z')
|
| 110 |
+
|
| 111 |
+
def test_radd(self, rhs):
|
| 112 |
+
out = rhs + self.p
|
| 113 |
+
assert_equal(out.symbol, 'z')
|
| 114 |
+
|
| 115 |
+
def test_rsub(self, rhs):
|
| 116 |
+
out = rhs - self.p
|
| 117 |
+
assert_equal(out.symbol, 'z')
|
| 118 |
+
|
| 119 |
+
def test_rmul(self, rhs):
|
| 120 |
+
out = rhs * self.p
|
| 121 |
+
assert_equal(out.symbol, 'z')
|
| 122 |
+
|
| 123 |
+
def test_rdivmod(self, rhs):
|
| 124 |
+
for out in divmod(rhs, self.p):
|
| 125 |
+
assert_equal(out.symbol, 'z')
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
class TestBinaryOperatorsDifferentSymbol:
|
| 129 |
+
p = poly.Polynomial([1, 2, 3], symbol='x')
|
| 130 |
+
other = poly.Polynomial([4, 5, 6], symbol='y')
|
| 131 |
+
ops = (p.__add__, p.__sub__, p.__mul__, p.__floordiv__, p.__mod__)
|
| 132 |
+
|
| 133 |
+
@pytest.mark.parametrize('f', ops)
|
| 134 |
+
def test_binops_fails(self, f):
|
| 135 |
+
assert_raises(ValueError, f, self.other)
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
class TestEquality:
|
| 139 |
+
p = poly.Polynomial([1, 2, 3], symbol='x')
|
| 140 |
+
|
| 141 |
+
def test_eq(self):
|
| 142 |
+
other = poly.Polynomial([1, 2, 3], symbol='x')
|
| 143 |
+
assert_(self.p == other)
|
| 144 |
+
|
| 145 |
+
def test_neq(self):
|
| 146 |
+
other = poly.Polynomial([1, 2, 3], symbol='y')
|
| 147 |
+
assert_(not self.p == other)
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
class TestExtraMethods:
|
| 151 |
+
"""
|
| 152 |
+
Test other methods for manipulating/creating polynomial objects.
|
| 153 |
+
"""
|
| 154 |
+
p = poly.Polynomial([1, 2, 3, 0], symbol='z')
|
| 155 |
+
|
| 156 |
+
def test_copy(self):
|
| 157 |
+
other = self.p.copy()
|
| 158 |
+
assert_equal(other.symbol, 'z')
|
| 159 |
+
|
| 160 |
+
def test_trim(self):
|
| 161 |
+
other = self.p.trim()
|
| 162 |
+
assert_equal(other.symbol, 'z')
|
| 163 |
+
|
| 164 |
+
def test_truncate(self):
|
| 165 |
+
other = self.p.truncate(2)
|
| 166 |
+
assert_equal(other.symbol, 'z')
|
| 167 |
+
|
| 168 |
+
@pytest.mark.parametrize('kwarg', (
|
| 169 |
+
{'domain': [-10, 10]},
|
| 170 |
+
{'window': [-10, 10]},
|
| 171 |
+
{'kind': poly.Chebyshev},
|
| 172 |
+
))
|
| 173 |
+
def test_convert(self, kwarg):
|
| 174 |
+
other = self.p.convert(**kwarg)
|
| 175 |
+
assert_equal(other.symbol, 'z')
|
| 176 |
+
|
| 177 |
+
def test_integ(self):
|
| 178 |
+
other = self.p.integ()
|
| 179 |
+
assert_equal(other.symbol, 'z')
|
| 180 |
+
|
| 181 |
+
def test_deriv(self):
|
| 182 |
+
other = self.p.deriv()
|
| 183 |
+
assert_equal(other.symbol, 'z')
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
def test_composition():
|
| 187 |
+
p = poly.Polynomial([3, 2, 1], symbol="t")
|
| 188 |
+
q = poly.Polynomial([5, 1, 0, -1], symbol="λ_1")
|
| 189 |
+
r = p(q)
|
| 190 |
+
assert r.symbol == "λ_1"
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
#
|
| 194 |
+
# Class methods that result in new polynomial class instances
|
| 195 |
+
#
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
def test_fit():
|
| 199 |
+
x, y = (range(10),) * 2
|
| 200 |
+
p = poly.Polynomial.fit(x, y, deg=1, symbol='z')
|
| 201 |
+
assert_equal(p.symbol, 'z')
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
def test_froomroots():
|
| 205 |
+
roots = [-2, 2]
|
| 206 |
+
p = poly.Polynomial.fromroots(roots, symbol='z')
|
| 207 |
+
assert_equal(p.symbol, 'z')
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
def test_identity():
|
| 211 |
+
p = poly.Polynomial.identity(domain=[-1, 1], window=[5, 20], symbol='z')
|
| 212 |
+
assert_equal(p.symbol, 'z')
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
def test_basis():
|
| 216 |
+
p = poly.Polynomial.basis(3, symbol='z')
|
| 217 |
+
assert_equal(p.symbol, 'z')
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/py.typed
ADDED
|
File without changes
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/random/LICENSE.md
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
**This software is dual-licensed under the The University of Illinois/NCSA
|
| 2 |
+
Open Source License (NCSA) and The 3-Clause BSD License**
|
| 3 |
+
|
| 4 |
+
# NCSA Open Source License
|
| 5 |
+
**Copyright (c) 2019 Kevin Sheppard. All rights reserved.**
|
| 6 |
+
|
| 7 |
+
Developed by: Kevin Sheppard (<kevin.sheppard@economics.ox.ac.uk>,
|
| 8 |
+
<kevin.k.sheppard@gmail.com>)
|
| 9 |
+
[http://www.kevinsheppard.com](http://www.kevinsheppard.com)
|
| 10 |
+
|
| 11 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
| 12 |
+
this software and associated documentation files (the "Software"), to deal with
|
| 13 |
+
the Software without restriction, including without limitation the rights to
|
| 14 |
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
| 15 |
+
of the Software, and to permit persons to whom the Software is furnished to do
|
| 16 |
+
so, subject to the following conditions:
|
| 17 |
+
|
| 18 |
+
Redistributions of source code must retain the above copyright notice, this
|
| 19 |
+
list of conditions and the following disclaimers.
|
| 20 |
+
|
| 21 |
+
Redistributions in binary form must reproduce the above copyright notice, this
|
| 22 |
+
list of conditions and the following disclaimers in the documentation and/or
|
| 23 |
+
other materials provided with the distribution.
|
| 24 |
+
|
| 25 |
+
Neither the names of Kevin Sheppard, nor the names of any contributors may be
|
| 26 |
+
used to endorse or promote products derived from this Software without specific
|
| 27 |
+
prior written permission.
|
| 28 |
+
|
| 29 |
+
**THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 30 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 31 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 32 |
+
CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 33 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 34 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
|
| 35 |
+
THE SOFTWARE.**
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# 3-Clause BSD License
|
| 39 |
+
**Copyright (c) 2019 Kevin Sheppard. All rights reserved.**
|
| 40 |
+
|
| 41 |
+
Redistribution and use in source and binary forms, with or without
|
| 42 |
+
modification, are permitted provided that the following conditions are met:
|
| 43 |
+
|
| 44 |
+
1. Redistributions of source code must retain the above copyright notice,
|
| 45 |
+
this list of conditions and the following disclaimer.
|
| 46 |
+
|
| 47 |
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
| 48 |
+
this list of conditions and the following disclaimer in the documentation
|
| 49 |
+
and/or other materials provided with the distribution.
|
| 50 |
+
|
| 51 |
+
3. Neither the name of the copyright holder nor the names of its contributors
|
| 52 |
+
may be used to endorse or promote products derived from this software
|
| 53 |
+
without specific prior written permission.
|
| 54 |
+
|
| 55 |
+
**THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
| 56 |
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
| 57 |
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
| 58 |
+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
| 59 |
+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
| 60 |
+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
| 61 |
+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
| 62 |
+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
| 63 |
+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
| 64 |
+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
| 65 |
+
THE POSSIBILITY OF SUCH DAMAGE.**
|
| 66 |
+
|
| 67 |
+
# Components
|
| 68 |
+
|
| 69 |
+
Many parts of this module have been derived from original sources,
|
| 70 |
+
often the algorithm's designer. Component licenses are located with
|
| 71 |
+
the component code.
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/random/__init__.pxd
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
cimport numpy as np
|
| 2 |
+
from libc.stdint cimport uint32_t, uint64_t
|
| 3 |
+
|
| 4 |
+
cdef extern from "numpy/random/bitgen.h":
|
| 5 |
+
struct bitgen:
|
| 6 |
+
void *state
|
| 7 |
+
uint64_t (*next_uint64)(void *st) nogil
|
| 8 |
+
uint32_t (*next_uint32)(void *st) nogil
|
| 9 |
+
double (*next_double)(void *st) nogil
|
| 10 |
+
uint64_t (*next_raw)(void *st) nogil
|
| 11 |
+
|
| 12 |
+
ctypedef bitgen bitgen_t
|
| 13 |
+
|
| 14 |
+
from numpy.random.bit_generator cimport BitGenerator, SeedSequence
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/random/__init__.py
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
========================
|
| 3 |
+
Random Number Generation
|
| 4 |
+
========================
|
| 5 |
+
|
| 6 |
+
Use ``default_rng()`` to create a `Generator` and call its methods.
|
| 7 |
+
|
| 8 |
+
=============== =========================================================
|
| 9 |
+
Generator
|
| 10 |
+
--------------- ---------------------------------------------------------
|
| 11 |
+
Generator Class implementing all of the random number distributions
|
| 12 |
+
default_rng Default constructor for ``Generator``
|
| 13 |
+
=============== =========================================================
|
| 14 |
+
|
| 15 |
+
============================================= ===
|
| 16 |
+
BitGenerator Streams that work with Generator
|
| 17 |
+
--------------------------------------------- ---
|
| 18 |
+
MT19937
|
| 19 |
+
PCG64
|
| 20 |
+
PCG64DXSM
|
| 21 |
+
Philox
|
| 22 |
+
SFC64
|
| 23 |
+
============================================= ===
|
| 24 |
+
|
| 25 |
+
============================================= ===
|
| 26 |
+
Getting entropy to initialize a BitGenerator
|
| 27 |
+
--------------------------------------------- ---
|
| 28 |
+
SeedSequence
|
| 29 |
+
============================================= ===
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
Legacy
|
| 33 |
+
------
|
| 34 |
+
|
| 35 |
+
For backwards compatibility with previous versions of numpy before 1.17, the
|
| 36 |
+
various aliases to the global `RandomState` methods are left alone and do not
|
| 37 |
+
use the new `Generator` API.
|
| 38 |
+
|
| 39 |
+
==================== =========================================================
|
| 40 |
+
Utility functions
|
| 41 |
+
-------------------- ---------------------------------------------------------
|
| 42 |
+
random Uniformly distributed floats over ``[0, 1)``
|
| 43 |
+
bytes Uniformly distributed random bytes.
|
| 44 |
+
permutation Randomly permute a sequence / generate a random sequence.
|
| 45 |
+
shuffle Randomly permute a sequence in place.
|
| 46 |
+
choice Random sample from 1-D array.
|
| 47 |
+
==================== =========================================================
|
| 48 |
+
|
| 49 |
+
==================== =========================================================
|
| 50 |
+
Compatibility
|
| 51 |
+
functions - removed
|
| 52 |
+
in the new API
|
| 53 |
+
-------------------- ---------------------------------------------------------
|
| 54 |
+
rand Uniformly distributed values.
|
| 55 |
+
randn Normally distributed values.
|
| 56 |
+
ranf Uniformly distributed floating point numbers.
|
| 57 |
+
random_integers Uniformly distributed integers in a given range.
|
| 58 |
+
(deprecated, use ``integers(..., closed=True)`` instead)
|
| 59 |
+
random_sample Alias for `random_sample`
|
| 60 |
+
randint Uniformly distributed integers in a given range
|
| 61 |
+
seed Seed the legacy random number generator.
|
| 62 |
+
==================== =========================================================
|
| 63 |
+
|
| 64 |
+
==================== =========================================================
|
| 65 |
+
Univariate
|
| 66 |
+
distributions
|
| 67 |
+
-------------------- ---------------------------------------------------------
|
| 68 |
+
beta Beta distribution over ``[0, 1]``.
|
| 69 |
+
binomial Binomial distribution.
|
| 70 |
+
chisquare :math:`\\chi^2` distribution.
|
| 71 |
+
exponential Exponential distribution.
|
| 72 |
+
f F (Fisher-Snedecor) distribution.
|
| 73 |
+
gamma Gamma distribution.
|
| 74 |
+
geometric Geometric distribution.
|
| 75 |
+
gumbel Gumbel distribution.
|
| 76 |
+
hypergeometric Hypergeometric distribution.
|
| 77 |
+
laplace Laplace distribution.
|
| 78 |
+
logistic Logistic distribution.
|
| 79 |
+
lognormal Log-normal distribution.
|
| 80 |
+
logseries Logarithmic series distribution.
|
| 81 |
+
negative_binomial Negative binomial distribution.
|
| 82 |
+
noncentral_chisquare Non-central chi-square distribution.
|
| 83 |
+
noncentral_f Non-central F distribution.
|
| 84 |
+
normal Normal / Gaussian distribution.
|
| 85 |
+
pareto Pareto distribution.
|
| 86 |
+
poisson Poisson distribution.
|
| 87 |
+
power Power distribution.
|
| 88 |
+
rayleigh Rayleigh distribution.
|
| 89 |
+
triangular Triangular distribution.
|
| 90 |
+
uniform Uniform distribution.
|
| 91 |
+
vonmises Von Mises circular distribution.
|
| 92 |
+
wald Wald (inverse Gaussian) distribution.
|
| 93 |
+
weibull Weibull distribution.
|
| 94 |
+
zipf Zipf's distribution over ranked data.
|
| 95 |
+
==================== =========================================================
|
| 96 |
+
|
| 97 |
+
==================== ==========================================================
|
| 98 |
+
Multivariate
|
| 99 |
+
distributions
|
| 100 |
+
-------------------- ----------------------------------------------------------
|
| 101 |
+
dirichlet Multivariate generalization of Beta distribution.
|
| 102 |
+
multinomial Multivariate generalization of the binomial distribution.
|
| 103 |
+
multivariate_normal Multivariate generalization of the normal distribution.
|
| 104 |
+
==================== ==========================================================
|
| 105 |
+
|
| 106 |
+
==================== =========================================================
|
| 107 |
+
Standard
|
| 108 |
+
distributions
|
| 109 |
+
-------------------- ---------------------------------------------------------
|
| 110 |
+
standard_cauchy Standard Cauchy-Lorentz distribution.
|
| 111 |
+
standard_exponential Standard exponential distribution.
|
| 112 |
+
standard_gamma Standard Gamma distribution.
|
| 113 |
+
standard_normal Standard normal distribution.
|
| 114 |
+
standard_t Standard Student's t-distribution.
|
| 115 |
+
==================== =========================================================
|
| 116 |
+
|
| 117 |
+
==================== =========================================================
|
| 118 |
+
Internal functions
|
| 119 |
+
-------------------- ---------------------------------------------------------
|
| 120 |
+
get_state Get tuple representing internal state of generator.
|
| 121 |
+
set_state Set state of generator.
|
| 122 |
+
==================== =========================================================
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
"""
|
| 126 |
+
__all__ = [
|
| 127 |
+
'beta',
|
| 128 |
+
'binomial',
|
| 129 |
+
'bytes',
|
| 130 |
+
'chisquare',
|
| 131 |
+
'choice',
|
| 132 |
+
'dirichlet',
|
| 133 |
+
'exponential',
|
| 134 |
+
'f',
|
| 135 |
+
'gamma',
|
| 136 |
+
'geometric',
|
| 137 |
+
'get_state',
|
| 138 |
+
'gumbel',
|
| 139 |
+
'hypergeometric',
|
| 140 |
+
'laplace',
|
| 141 |
+
'logistic',
|
| 142 |
+
'lognormal',
|
| 143 |
+
'logseries',
|
| 144 |
+
'multinomial',
|
| 145 |
+
'multivariate_normal',
|
| 146 |
+
'negative_binomial',
|
| 147 |
+
'noncentral_chisquare',
|
| 148 |
+
'noncentral_f',
|
| 149 |
+
'normal',
|
| 150 |
+
'pareto',
|
| 151 |
+
'permutation',
|
| 152 |
+
'poisson',
|
| 153 |
+
'power',
|
| 154 |
+
'rand',
|
| 155 |
+
'randint',
|
| 156 |
+
'randn',
|
| 157 |
+
'random',
|
| 158 |
+
'random_integers',
|
| 159 |
+
'random_sample',
|
| 160 |
+
'ranf',
|
| 161 |
+
'rayleigh',
|
| 162 |
+
'sample',
|
| 163 |
+
'seed',
|
| 164 |
+
'set_state',
|
| 165 |
+
'shuffle',
|
| 166 |
+
'standard_cauchy',
|
| 167 |
+
'standard_exponential',
|
| 168 |
+
'standard_gamma',
|
| 169 |
+
'standard_normal',
|
| 170 |
+
'standard_t',
|
| 171 |
+
'triangular',
|
| 172 |
+
'uniform',
|
| 173 |
+
'vonmises',
|
| 174 |
+
'wald',
|
| 175 |
+
'weibull',
|
| 176 |
+
'zipf',
|
| 177 |
+
]
|
| 178 |
+
|
| 179 |
+
# add these for module-freeze analysis (like PyInstaller)
|
| 180 |
+
from . import _bounded_integers, _common, _pickle
|
| 181 |
+
from ._generator import Generator, default_rng
|
| 182 |
+
from ._mt19937 import MT19937
|
| 183 |
+
from ._pcg64 import PCG64, PCG64DXSM
|
| 184 |
+
from ._philox import Philox
|
| 185 |
+
from ._sfc64 import SFC64
|
| 186 |
+
from .bit_generator import BitGenerator, SeedSequence
|
| 187 |
+
from .mtrand import *
|
| 188 |
+
|
| 189 |
+
__all__ += ['Generator', 'RandomState', 'SeedSequence', 'MT19937',
|
| 190 |
+
'Philox', 'PCG64', 'PCG64DXSM', 'SFC64', 'default_rng',
|
| 191 |
+
'BitGenerator']
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
def __RandomState_ctor():
|
| 195 |
+
"""Return a RandomState instance.
|
| 196 |
+
|
| 197 |
+
This function exists solely to assist (un)pickling.
|
| 198 |
+
|
| 199 |
+
Note that the state of the RandomState returned here is irrelevant, as this
|
| 200 |
+
function's entire purpose is to return a newly allocated RandomState whose
|
| 201 |
+
state pickle can set. Consequently the RandomState returned by this function
|
| 202 |
+
is a freshly allocated copy with a seed=0.
|
| 203 |
+
|
| 204 |
+
See https://github.com/numpy/numpy/issues/4763 for a detailed discussion
|
| 205 |
+
|
| 206 |
+
"""
|
| 207 |
+
return RandomState(seed=0)
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
from numpy._pytesttester import PytestTester
|
| 211 |
+
|
| 212 |
+
test = PytestTester(__name__)
|
| 213 |
+
del PytestTester
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/random/__init__.pyi
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ._generator import Generator, default_rng
|
| 2 |
+
from ._mt19937 import MT19937
|
| 3 |
+
from ._pcg64 import PCG64, PCG64DXSM
|
| 4 |
+
from ._philox import Philox
|
| 5 |
+
from ._sfc64 import SFC64
|
| 6 |
+
from .bit_generator import BitGenerator, SeedSequence
|
| 7 |
+
from .mtrand import (
|
| 8 |
+
RandomState,
|
| 9 |
+
beta,
|
| 10 |
+
binomial,
|
| 11 |
+
bytes,
|
| 12 |
+
chisquare,
|
| 13 |
+
choice,
|
| 14 |
+
dirichlet,
|
| 15 |
+
exponential,
|
| 16 |
+
f,
|
| 17 |
+
gamma,
|
| 18 |
+
geometric,
|
| 19 |
+
get_bit_generator, # noqa: F401
|
| 20 |
+
get_state,
|
| 21 |
+
gumbel,
|
| 22 |
+
hypergeometric,
|
| 23 |
+
laplace,
|
| 24 |
+
logistic,
|
| 25 |
+
lognormal,
|
| 26 |
+
logseries,
|
| 27 |
+
multinomial,
|
| 28 |
+
multivariate_normal,
|
| 29 |
+
negative_binomial,
|
| 30 |
+
noncentral_chisquare,
|
| 31 |
+
noncentral_f,
|
| 32 |
+
normal,
|
| 33 |
+
pareto,
|
| 34 |
+
permutation,
|
| 35 |
+
poisson,
|
| 36 |
+
power,
|
| 37 |
+
rand,
|
| 38 |
+
randint,
|
| 39 |
+
randn,
|
| 40 |
+
random,
|
| 41 |
+
random_integers,
|
| 42 |
+
random_sample,
|
| 43 |
+
ranf,
|
| 44 |
+
rayleigh,
|
| 45 |
+
sample,
|
| 46 |
+
seed,
|
| 47 |
+
set_bit_generator, # noqa: F401
|
| 48 |
+
set_state,
|
| 49 |
+
shuffle,
|
| 50 |
+
standard_cauchy,
|
| 51 |
+
standard_exponential,
|
| 52 |
+
standard_gamma,
|
| 53 |
+
standard_normal,
|
| 54 |
+
standard_t,
|
| 55 |
+
triangular,
|
| 56 |
+
uniform,
|
| 57 |
+
vonmises,
|
| 58 |
+
wald,
|
| 59 |
+
weibull,
|
| 60 |
+
zipf,
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
__all__ = [
|
| 64 |
+
"beta",
|
| 65 |
+
"binomial",
|
| 66 |
+
"bytes",
|
| 67 |
+
"chisquare",
|
| 68 |
+
"choice",
|
| 69 |
+
"dirichlet",
|
| 70 |
+
"exponential",
|
| 71 |
+
"f",
|
| 72 |
+
"gamma",
|
| 73 |
+
"geometric",
|
| 74 |
+
"get_state",
|
| 75 |
+
"gumbel",
|
| 76 |
+
"hypergeometric",
|
| 77 |
+
"laplace",
|
| 78 |
+
"logistic",
|
| 79 |
+
"lognormal",
|
| 80 |
+
"logseries",
|
| 81 |
+
"multinomial",
|
| 82 |
+
"multivariate_normal",
|
| 83 |
+
"negative_binomial",
|
| 84 |
+
"noncentral_chisquare",
|
| 85 |
+
"noncentral_f",
|
| 86 |
+
"normal",
|
| 87 |
+
"pareto",
|
| 88 |
+
"permutation",
|
| 89 |
+
"poisson",
|
| 90 |
+
"power",
|
| 91 |
+
"rand",
|
| 92 |
+
"randint",
|
| 93 |
+
"randn",
|
| 94 |
+
"random",
|
| 95 |
+
"random_integers",
|
| 96 |
+
"random_sample",
|
| 97 |
+
"ranf",
|
| 98 |
+
"rayleigh",
|
| 99 |
+
"sample",
|
| 100 |
+
"seed",
|
| 101 |
+
"set_state",
|
| 102 |
+
"shuffle",
|
| 103 |
+
"standard_cauchy",
|
| 104 |
+
"standard_exponential",
|
| 105 |
+
"standard_gamma",
|
| 106 |
+
"standard_normal",
|
| 107 |
+
"standard_t",
|
| 108 |
+
"triangular",
|
| 109 |
+
"uniform",
|
| 110 |
+
"vonmises",
|
| 111 |
+
"wald",
|
| 112 |
+
"weibull",
|
| 113 |
+
"zipf",
|
| 114 |
+
"Generator",
|
| 115 |
+
"RandomState",
|
| 116 |
+
"SeedSequence",
|
| 117 |
+
"MT19937",
|
| 118 |
+
"Philox",
|
| 119 |
+
"PCG64",
|
| 120 |
+
"PCG64DXSM",
|
| 121 |
+
"SFC64",
|
| 122 |
+
"default_rng",
|
| 123 |
+
"BitGenerator",
|
| 124 |
+
]
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/random/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (7.51 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/random/__pycache__/_pickle.cpython-312.pyc
ADDED
|
Binary file (2.92 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_bounded_integers.cp312-win_amd64.lib
ADDED
|
Binary file (18 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_bounded_integers.cp312-win_amd64.pyd
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:dd98c61f9c78c6dfbf16b7ef5060647aba412a6ea74ab6cd80d93c34dd485d57
|
| 3 |
+
size 207360
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_bounded_integers.pxd
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from libc.stdint cimport (uint8_t, uint16_t, uint32_t, uint64_t,
|
| 2 |
+
int8_t, int16_t, int32_t, int64_t, intptr_t)
|
| 3 |
+
import numpy as np
|
| 4 |
+
cimport numpy as np
|
| 5 |
+
ctypedef np.npy_bool bool_t
|
| 6 |
+
|
| 7 |
+
from numpy.random cimport bitgen_t
|
| 8 |
+
|
| 9 |
+
cdef inline uint64_t _gen_mask(uint64_t max_val) noexcept nogil:
|
| 10 |
+
"""Mask generator for use in bounded random numbers"""
|
| 11 |
+
# Smallest bit mask >= max
|
| 12 |
+
cdef uint64_t mask = max_val
|
| 13 |
+
mask |= mask >> 1
|
| 14 |
+
mask |= mask >> 2
|
| 15 |
+
mask |= mask >> 4
|
| 16 |
+
mask |= mask >> 8
|
| 17 |
+
mask |= mask >> 16
|
| 18 |
+
mask |= mask >> 32
|
| 19 |
+
return mask
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
cdef object _rand_uint64(object low, object high, object size, bint use_masked, bint closed, bitgen_t *state, object lock)
|
| 23 |
+
|
| 24 |
+
cdef object _rand_uint32(object low, object high, object size, bint use_masked, bint closed, bitgen_t *state, object lock)
|
| 25 |
+
|
| 26 |
+
cdef object _rand_uint16(object low, object high, object size, bint use_masked, bint closed, bitgen_t *state, object lock)
|
| 27 |
+
|
| 28 |
+
cdef object _rand_uint8(object low, object high, object size, bint use_masked, bint closed, bitgen_t *state, object lock)
|
| 29 |
+
|
| 30 |
+
cdef object _rand_bool(object low, object high, object size, bint use_masked, bint closed, bitgen_t *state, object lock)
|
| 31 |
+
|
| 32 |
+
cdef object _rand_int64(object low, object high, object size, bint use_masked, bint closed, bitgen_t *state, object lock)
|
| 33 |
+
|
| 34 |
+
cdef object _rand_int32(object low, object high, object size, bint use_masked, bint closed, bitgen_t *state, object lock)
|
| 35 |
+
|
| 36 |
+
cdef object _rand_int16(object low, object high, object size, bint use_masked, bint closed, bitgen_t *state, object lock)
|
| 37 |
+
|
| 38 |
+
cdef object _rand_int8(object low, object high, object size, bint use_masked, bint closed, bitgen_t *state, object lock)
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_bounded_integers.pyi
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
__all__: list[str] = []
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_common.cp312-win_amd64.lib
ADDED
|
Binary file (2.01 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_common.cp312-win_amd64.pyd
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:95b8170a18ec959a6e7d5eb3c5ec62945e25ef000e7d3e079634f6a6f1025599
|
| 3 |
+
size 167424
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_common.pxd
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#cython: language_level=3
|
| 2 |
+
|
| 3 |
+
from libc.stdint cimport uint32_t, uint64_t, int32_t, int64_t
|
| 4 |
+
|
| 5 |
+
import numpy as np
|
| 6 |
+
cimport numpy as np
|
| 7 |
+
|
| 8 |
+
from numpy.random cimport bitgen_t
|
| 9 |
+
|
| 10 |
+
cdef double POISSON_LAM_MAX
|
| 11 |
+
cdef double LEGACY_POISSON_LAM_MAX
|
| 12 |
+
cdef uint64_t MAXSIZE
|
| 13 |
+
|
| 14 |
+
cdef enum ConstraintType:
|
| 15 |
+
CONS_NONE
|
| 16 |
+
CONS_NON_NEGATIVE
|
| 17 |
+
CONS_POSITIVE
|
| 18 |
+
CONS_POSITIVE_NOT_NAN
|
| 19 |
+
CONS_BOUNDED_0_1
|
| 20 |
+
CONS_BOUNDED_GT_0_1
|
| 21 |
+
CONS_BOUNDED_LT_0_1
|
| 22 |
+
CONS_GT_1
|
| 23 |
+
CONS_GTE_1
|
| 24 |
+
CONS_POISSON
|
| 25 |
+
LEGACY_CONS_POISSON
|
| 26 |
+
LEGACY_CONS_NON_NEGATIVE_INBOUNDS_LONG
|
| 27 |
+
|
| 28 |
+
ctypedef ConstraintType constraint_type
|
| 29 |
+
ctypedef fused double_or_int64:
|
| 30 |
+
double
|
| 31 |
+
int64_t
|
| 32 |
+
|
| 33 |
+
cdef object benchmark(bitgen_t *bitgen, object lock, Py_ssize_t cnt, object method)
|
| 34 |
+
cdef object random_raw(bitgen_t *bitgen, object lock, object size, object output)
|
| 35 |
+
cdef object prepare_cffi(bitgen_t *bitgen)
|
| 36 |
+
cdef object prepare_ctypes(bitgen_t *bitgen)
|
| 37 |
+
cdef int check_constraint(double_or_int64 val, object name, constraint_type cons) except -1
|
| 38 |
+
cdef int check_array_constraint(np.ndarray val, object name, constraint_type cons) except -1
|
| 39 |
+
|
| 40 |
+
cdef extern from "include/aligned_malloc.h":
|
| 41 |
+
cdef void *PyArray_realloc_aligned(void *p, size_t n)
|
| 42 |
+
cdef void *PyArray_malloc_aligned(size_t n)
|
| 43 |
+
cdef void *PyArray_calloc_aligned(size_t n, size_t s)
|
| 44 |
+
cdef void PyArray_free_aligned(void *p)
|
| 45 |
+
|
| 46 |
+
ctypedef void (*random_double_fill)(bitgen_t *state, np.npy_intp count, double* out) noexcept nogil
|
| 47 |
+
ctypedef double (*random_double_0)(void *state) noexcept nogil
|
| 48 |
+
ctypedef double (*random_double_1)(void *state, double a) noexcept nogil
|
| 49 |
+
ctypedef double (*random_double_2)(void *state, double a, double b) noexcept nogil
|
| 50 |
+
ctypedef double (*random_double_3)(void *state, double a, double b, double c) noexcept nogil
|
| 51 |
+
|
| 52 |
+
ctypedef void (*random_float_fill)(bitgen_t *state, np.npy_intp count, float* out) noexcept nogil
|
| 53 |
+
ctypedef float (*random_float_0)(bitgen_t *state) noexcept nogil
|
| 54 |
+
ctypedef float (*random_float_1)(bitgen_t *state, float a) noexcept nogil
|
| 55 |
+
|
| 56 |
+
ctypedef int64_t (*random_uint_0)(void *state) noexcept nogil
|
| 57 |
+
ctypedef int64_t (*random_uint_d)(void *state, double a) noexcept nogil
|
| 58 |
+
ctypedef int64_t (*random_uint_dd)(void *state, double a, double b) noexcept nogil
|
| 59 |
+
ctypedef int64_t (*random_uint_di)(void *state, double a, uint64_t b) noexcept nogil
|
| 60 |
+
ctypedef int64_t (*random_uint_i)(void *state, int64_t a) noexcept nogil
|
| 61 |
+
ctypedef int64_t (*random_uint_iii)(void *state, int64_t a, int64_t b, int64_t c) noexcept nogil
|
| 62 |
+
|
| 63 |
+
ctypedef uint32_t (*random_uint_0_32)(bitgen_t *state) noexcept nogil
|
| 64 |
+
ctypedef uint32_t (*random_uint_1_i_32)(bitgen_t *state, uint32_t a) noexcept nogil
|
| 65 |
+
|
| 66 |
+
ctypedef int32_t (*random_int_2_i_32)(bitgen_t *state, int32_t a, int32_t b) noexcept nogil
|
| 67 |
+
ctypedef int64_t (*random_int_2_i)(bitgen_t *state, int64_t a, int64_t b) noexcept nogil
|
| 68 |
+
|
| 69 |
+
cdef double kahan_sum(double *darr, np.npy_intp n) noexcept
|
| 70 |
+
|
| 71 |
+
cdef inline double uint64_to_double(uint64_t rnd) noexcept nogil:
|
| 72 |
+
return (rnd >> 11) * (1.0 / 9007199254740992.0)
|
| 73 |
+
|
| 74 |
+
cdef object double_fill(void *func, bitgen_t *state, object size, object lock, object out)
|
| 75 |
+
|
| 76 |
+
cdef object float_fill(void *func, bitgen_t *state, object size, object lock, object out)
|
| 77 |
+
|
| 78 |
+
cdef object float_fill_from_double(void *func, bitgen_t *state, object size, object lock, object out)
|
| 79 |
+
|
| 80 |
+
cdef object wrap_int(object val, object bits)
|
| 81 |
+
|
| 82 |
+
cdef np.ndarray int_to_array(object value, object name, object bits, object uint_size)
|
| 83 |
+
|
| 84 |
+
cdef validate_output_shape(iter_shape, np.ndarray output)
|
| 85 |
+
|
| 86 |
+
cdef object cont(void *func, void *state, object size, object lock, int narg,
|
| 87 |
+
object a, object a_name, constraint_type a_constraint,
|
| 88 |
+
object b, object b_name, constraint_type b_constraint,
|
| 89 |
+
object c, object c_name, constraint_type c_constraint,
|
| 90 |
+
object out)
|
| 91 |
+
|
| 92 |
+
cdef object disc(void *func, void *state, object size, object lock,
|
| 93 |
+
int narg_double, int narg_int64,
|
| 94 |
+
object a, object a_name, constraint_type a_constraint,
|
| 95 |
+
object b, object b_name, constraint_type b_constraint,
|
| 96 |
+
object c, object c_name, constraint_type c_constraint)
|
| 97 |
+
|
| 98 |
+
cdef object cont_f(void *func, bitgen_t *state, object size, object lock,
|
| 99 |
+
object a, object a_name, constraint_type a_constraint,
|
| 100 |
+
object out)
|
| 101 |
+
|
| 102 |
+
cdef object cont_broadcast_3(void *func, void *state, object size, object lock,
|
| 103 |
+
np.ndarray a_arr, object a_name, constraint_type a_constraint,
|
| 104 |
+
np.ndarray b_arr, object b_name, constraint_type b_constraint,
|
| 105 |
+
np.ndarray c_arr, object c_name, constraint_type c_constraint)
|
| 106 |
+
|
| 107 |
+
cdef object discrete_broadcast_iii(void *func, void *state, object size, object lock,
|
| 108 |
+
np.ndarray a_arr, object a_name, constraint_type a_constraint,
|
| 109 |
+
np.ndarray b_arr, object b_name, constraint_type b_constraint,
|
| 110 |
+
np.ndarray c_arr, object c_name, constraint_type c_constraint)
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_common.pyi
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from collections.abc import Callable
|
| 2 |
+
from typing import Any, NamedTuple, TypeAlias
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
__all__: list[str] = ["interface"]
|
| 7 |
+
|
| 8 |
+
_CDataVoidPointer: TypeAlias = Any
|
| 9 |
+
|
| 10 |
+
class interface(NamedTuple):
|
| 11 |
+
state_address: int
|
| 12 |
+
state: _CDataVoidPointer
|
| 13 |
+
next_uint64: Callable[..., np.uint64]
|
| 14 |
+
next_uint32: Callable[..., np.uint32]
|
| 15 |
+
next_double: Callable[..., np.float64]
|
| 16 |
+
bit_generator: _CDataVoidPointer
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_examples/cffi/extending.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Use cffi to access any of the underlying C functions from distributions.h
|
| 3 |
+
"""
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
import cffi
|
| 7 |
+
|
| 8 |
+
import numpy as np
|
| 9 |
+
|
| 10 |
+
from .parse import parse_distributions_h
|
| 11 |
+
|
| 12 |
+
ffi = cffi.FFI()
|
| 13 |
+
|
| 14 |
+
inc_dir = os.path.join(np.get_include(), 'numpy')
|
| 15 |
+
|
| 16 |
+
# Basic numpy types
|
| 17 |
+
ffi.cdef('''
|
| 18 |
+
typedef intptr_t npy_intp;
|
| 19 |
+
typedef unsigned char npy_bool;
|
| 20 |
+
|
| 21 |
+
''')
|
| 22 |
+
|
| 23 |
+
parse_distributions_h(ffi, inc_dir)
|
| 24 |
+
|
| 25 |
+
lib = ffi.dlopen(np.random._generator.__file__)
|
| 26 |
+
|
| 27 |
+
# Compare the distributions.h random_standard_normal_fill to
|
| 28 |
+
# Generator.standard_random
|
| 29 |
+
bit_gen = np.random.PCG64()
|
| 30 |
+
rng = np.random.Generator(bit_gen)
|
| 31 |
+
state = bit_gen.state
|
| 32 |
+
|
| 33 |
+
interface = rng.bit_generator.cffi
|
| 34 |
+
n = 100
|
| 35 |
+
vals_cffi = ffi.new('double[%d]' % n)
|
| 36 |
+
lib.random_standard_normal_fill(interface.bit_generator, n, vals_cffi)
|
| 37 |
+
|
| 38 |
+
# reset the state
|
| 39 |
+
bit_gen.state = state
|
| 40 |
+
|
| 41 |
+
vals = rng.standard_normal(n)
|
| 42 |
+
|
| 43 |
+
for i in range(n):
|
| 44 |
+
assert vals[i] == vals_cffi[i]
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_examples/cffi/parse.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def parse_distributions_h(ffi, inc_dir):
|
| 5 |
+
"""
|
| 6 |
+
Parse distributions.h located in inc_dir for CFFI, filling in the ffi.cdef
|
| 7 |
+
|
| 8 |
+
Read the function declarations without the "#define ..." macros that will
|
| 9 |
+
be filled in when loading the library.
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
with open(os.path.join(inc_dir, 'random', 'bitgen.h')) as fid:
|
| 13 |
+
s = []
|
| 14 |
+
for line in fid:
|
| 15 |
+
# massage the include file
|
| 16 |
+
if line.strip().startswith('#'):
|
| 17 |
+
continue
|
| 18 |
+
s.append(line)
|
| 19 |
+
ffi.cdef('\n'.join(s))
|
| 20 |
+
|
| 21 |
+
with open(os.path.join(inc_dir, 'random', 'distributions.h')) as fid:
|
| 22 |
+
s = []
|
| 23 |
+
in_skip = 0
|
| 24 |
+
ignoring = False
|
| 25 |
+
for line in fid:
|
| 26 |
+
# check for and remove extern "C" guards
|
| 27 |
+
if ignoring:
|
| 28 |
+
if line.strip().startswith('#endif'):
|
| 29 |
+
ignoring = False
|
| 30 |
+
continue
|
| 31 |
+
if line.strip().startswith('#ifdef __cplusplus'):
|
| 32 |
+
ignoring = True
|
| 33 |
+
|
| 34 |
+
# massage the include file
|
| 35 |
+
if line.strip().startswith('#'):
|
| 36 |
+
continue
|
| 37 |
+
|
| 38 |
+
# skip any inlined function definition
|
| 39 |
+
# which starts with 'static inline xxx(...) {'
|
| 40 |
+
# and ends with a closing '}'
|
| 41 |
+
if line.strip().startswith('static inline'):
|
| 42 |
+
in_skip += line.count('{')
|
| 43 |
+
continue
|
| 44 |
+
elif in_skip > 0:
|
| 45 |
+
in_skip += line.count('{')
|
| 46 |
+
in_skip -= line.count('}')
|
| 47 |
+
continue
|
| 48 |
+
|
| 49 |
+
# replace defines with their value or remove them
|
| 50 |
+
line = line.replace('DECLDIR', '')
|
| 51 |
+
line = line.replace('RAND_INT_TYPE', 'int64_t')
|
| 52 |
+
s.append(line)
|
| 53 |
+
ffi.cdef('\n'.join(s))
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_examples/cython/extending.pyx
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#cython: language_level=3
|
| 2 |
+
|
| 3 |
+
from libc.stdint cimport uint32_t
|
| 4 |
+
from cpython.pycapsule cimport PyCapsule_IsValid, PyCapsule_GetPointer
|
| 5 |
+
|
| 6 |
+
import numpy as np
|
| 7 |
+
cimport numpy as np
|
| 8 |
+
cimport cython
|
| 9 |
+
|
| 10 |
+
from numpy.random cimport bitgen_t
|
| 11 |
+
from numpy.random import PCG64
|
| 12 |
+
|
| 13 |
+
np.import_array()
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
@cython.boundscheck(False)
|
| 17 |
+
@cython.wraparound(False)
|
| 18 |
+
def uniform_mean(Py_ssize_t n):
|
| 19 |
+
cdef Py_ssize_t i
|
| 20 |
+
cdef bitgen_t *rng
|
| 21 |
+
cdef const char *capsule_name = "BitGenerator"
|
| 22 |
+
cdef double[::1] random_values
|
| 23 |
+
cdef np.ndarray randoms
|
| 24 |
+
|
| 25 |
+
x = PCG64()
|
| 26 |
+
capsule = x.capsule
|
| 27 |
+
if not PyCapsule_IsValid(capsule, capsule_name):
|
| 28 |
+
raise ValueError("Invalid pointer to anon_func_state")
|
| 29 |
+
rng = <bitgen_t *> PyCapsule_GetPointer(capsule, capsule_name)
|
| 30 |
+
random_values = np.empty(n)
|
| 31 |
+
# Best practice is to acquire the lock whenever generating random values.
|
| 32 |
+
# This prevents other threads from modifying the state. Acquiring the lock
|
| 33 |
+
# is only necessary if the GIL is also released, as in this example.
|
| 34 |
+
with x.lock, nogil:
|
| 35 |
+
for i in range(n):
|
| 36 |
+
random_values[i] = rng.next_double(rng.state)
|
| 37 |
+
randoms = np.asarray(random_values)
|
| 38 |
+
return randoms.mean()
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# This function is declared nogil so it can be used without the GIL below
|
| 42 |
+
cdef uint32_t bounded_uint(uint32_t lb, uint32_t ub, bitgen_t *rng) nogil:
|
| 43 |
+
cdef uint32_t mask, delta, val
|
| 44 |
+
mask = delta = ub - lb
|
| 45 |
+
mask |= mask >> 1
|
| 46 |
+
mask |= mask >> 2
|
| 47 |
+
mask |= mask >> 4
|
| 48 |
+
mask |= mask >> 8
|
| 49 |
+
mask |= mask >> 16
|
| 50 |
+
|
| 51 |
+
val = rng.next_uint32(rng.state) & mask
|
| 52 |
+
while val > delta:
|
| 53 |
+
val = rng.next_uint32(rng.state) & mask
|
| 54 |
+
|
| 55 |
+
return lb + val
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
@cython.boundscheck(False)
|
| 59 |
+
@cython.wraparound(False)
|
| 60 |
+
def bounded_uints(uint32_t lb, uint32_t ub, Py_ssize_t n):
|
| 61 |
+
cdef Py_ssize_t i
|
| 62 |
+
cdef bitgen_t *rng
|
| 63 |
+
cdef uint32_t[::1] out
|
| 64 |
+
cdef const char *capsule_name = "BitGenerator"
|
| 65 |
+
|
| 66 |
+
x = PCG64()
|
| 67 |
+
out = np.empty(n, dtype=np.uint32)
|
| 68 |
+
capsule = x.capsule
|
| 69 |
+
|
| 70 |
+
if not PyCapsule_IsValid(capsule, capsule_name):
|
| 71 |
+
raise ValueError("Invalid pointer to anon_func_state")
|
| 72 |
+
rng = <bitgen_t *>PyCapsule_GetPointer(capsule, capsule_name)
|
| 73 |
+
|
| 74 |
+
with x.lock, nogil:
|
| 75 |
+
for i in range(n):
|
| 76 |
+
out[i] = bounded_uint(lb, ub, rng)
|
| 77 |
+
return np.asarray(out)
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_examples/cython/extending_distributions.pyx
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#cython: language_level=3
|
| 2 |
+
"""
|
| 3 |
+
This file shows how the to use a BitGenerator to create a distribution.
|
| 4 |
+
"""
|
| 5 |
+
import numpy as np
|
| 6 |
+
cimport numpy as np
|
| 7 |
+
cimport cython
|
| 8 |
+
from cpython.pycapsule cimport PyCapsule_IsValid, PyCapsule_GetPointer
|
| 9 |
+
from libc.stdint cimport uint16_t, uint64_t
|
| 10 |
+
from numpy.random cimport bitgen_t
|
| 11 |
+
from numpy.random import PCG64
|
| 12 |
+
from numpy.random.c_distributions cimport (
|
| 13 |
+
random_standard_uniform_fill, random_standard_uniform_fill_f)
|
| 14 |
+
|
| 15 |
+
np.import_array()
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
@cython.boundscheck(False)
|
| 19 |
+
@cython.wraparound(False)
|
| 20 |
+
def uniforms(Py_ssize_t n):
|
| 21 |
+
"""
|
| 22 |
+
Create an array of `n` uniformly distributed doubles.
|
| 23 |
+
A 'real' distribution would want to process the values into
|
| 24 |
+
some non-uniform distribution
|
| 25 |
+
"""
|
| 26 |
+
cdef Py_ssize_t i
|
| 27 |
+
cdef bitgen_t *rng
|
| 28 |
+
cdef const char *capsule_name = "BitGenerator"
|
| 29 |
+
cdef double[::1] random_values
|
| 30 |
+
|
| 31 |
+
x = PCG64()
|
| 32 |
+
capsule = x.capsule
|
| 33 |
+
# Optional check that the capsule if from a BitGenerator
|
| 34 |
+
if not PyCapsule_IsValid(capsule, capsule_name):
|
| 35 |
+
raise ValueError("Invalid pointer to anon_func_state")
|
| 36 |
+
# Cast the pointer
|
| 37 |
+
rng = <bitgen_t *> PyCapsule_GetPointer(capsule, capsule_name)
|
| 38 |
+
random_values = np.empty(n, dtype='float64')
|
| 39 |
+
with x.lock, nogil:
|
| 40 |
+
for i in range(n):
|
| 41 |
+
# Call the function
|
| 42 |
+
random_values[i] = rng.next_double(rng.state)
|
| 43 |
+
randoms = np.asarray(random_values)
|
| 44 |
+
|
| 45 |
+
return randoms
|
| 46 |
+
|
| 47 |
+
# cython example 2
|
| 48 |
+
@cython.boundscheck(False)
|
| 49 |
+
@cython.wraparound(False)
|
| 50 |
+
def uint10_uniforms(Py_ssize_t n):
|
| 51 |
+
"""Uniform 10 bit integers stored as 16-bit unsigned integers"""
|
| 52 |
+
cdef Py_ssize_t i
|
| 53 |
+
cdef bitgen_t *rng
|
| 54 |
+
cdef const char *capsule_name = "BitGenerator"
|
| 55 |
+
cdef uint16_t[::1] random_values
|
| 56 |
+
cdef int bits_remaining
|
| 57 |
+
cdef int width = 10
|
| 58 |
+
cdef uint64_t buff, mask = 0x3FF
|
| 59 |
+
|
| 60 |
+
x = PCG64()
|
| 61 |
+
capsule = x.capsule
|
| 62 |
+
if not PyCapsule_IsValid(capsule, capsule_name):
|
| 63 |
+
raise ValueError("Invalid pointer to anon_func_state")
|
| 64 |
+
rng = <bitgen_t *> PyCapsule_GetPointer(capsule, capsule_name)
|
| 65 |
+
random_values = np.empty(n, dtype='uint16')
|
| 66 |
+
# Best practice is to release GIL and acquire the lock
|
| 67 |
+
bits_remaining = 0
|
| 68 |
+
with x.lock, nogil:
|
| 69 |
+
for i in range(n):
|
| 70 |
+
if bits_remaining < width:
|
| 71 |
+
buff = rng.next_uint64(rng.state)
|
| 72 |
+
random_values[i] = buff & mask
|
| 73 |
+
buff >>= width
|
| 74 |
+
|
| 75 |
+
randoms = np.asarray(random_values)
|
| 76 |
+
return randoms
|
| 77 |
+
|
| 78 |
+
# cython example 3
|
| 79 |
+
def uniforms_ex(bit_generator, Py_ssize_t n, dtype=np.float64):
|
| 80 |
+
"""
|
| 81 |
+
Create an array of `n` uniformly distributed doubles via a "fill" function.
|
| 82 |
+
|
| 83 |
+
A 'real' distribution would want to process the values into
|
| 84 |
+
some non-uniform distribution
|
| 85 |
+
|
| 86 |
+
Parameters
|
| 87 |
+
----------
|
| 88 |
+
bit_generator: BitGenerator instance
|
| 89 |
+
n: int
|
| 90 |
+
Output vector length
|
| 91 |
+
dtype: {str, dtype}, optional
|
| 92 |
+
Desired dtype, either 'd' (or 'float64') or 'f' (or 'float32'). The
|
| 93 |
+
default dtype value is 'd'
|
| 94 |
+
"""
|
| 95 |
+
cdef bitgen_t *rng
|
| 96 |
+
cdef const char *capsule_name = "BitGenerator"
|
| 97 |
+
cdef np.ndarray randoms
|
| 98 |
+
|
| 99 |
+
capsule = bit_generator.capsule
|
| 100 |
+
# Optional check that the capsule if from a BitGenerator
|
| 101 |
+
if not PyCapsule_IsValid(capsule, capsule_name):
|
| 102 |
+
raise ValueError("Invalid pointer to anon_func_state")
|
| 103 |
+
# Cast the pointer
|
| 104 |
+
rng = <bitgen_t *> PyCapsule_GetPointer(capsule, capsule_name)
|
| 105 |
+
|
| 106 |
+
_dtype = np.dtype(dtype)
|
| 107 |
+
randoms = np.empty(n, dtype=_dtype)
|
| 108 |
+
if _dtype == np.float32:
|
| 109 |
+
with bit_generator.lock:
|
| 110 |
+
random_standard_uniform_fill_f(rng, n, <float*>np.PyArray_DATA(randoms))
|
| 111 |
+
elif _dtype == np.float64:
|
| 112 |
+
with bit_generator.lock:
|
| 113 |
+
random_standard_uniform_fill(rng, n, <double*>np.PyArray_DATA(randoms))
|
| 114 |
+
else:
|
| 115 |
+
raise TypeError('Unsupported dtype %r for random' % _dtype)
|
| 116 |
+
return randoms
|
| 117 |
+
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_examples/cython/meson.build
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
project('random-build-examples', 'c', 'cpp', 'cython')
|
| 2 |
+
|
| 3 |
+
py_mod = import('python')
|
| 4 |
+
py3 = py_mod.find_installation(pure: false)
|
| 5 |
+
|
| 6 |
+
cc = meson.get_compiler('c')
|
| 7 |
+
cy = meson.get_compiler('cython')
|
| 8 |
+
|
| 9 |
+
# Keep synced with pyproject.toml
|
| 10 |
+
if not cy.version().version_compare('>=3.0.6')
|
| 11 |
+
error('tests requires Cython >= 3.0.6')
|
| 12 |
+
endif
|
| 13 |
+
|
| 14 |
+
base_cython_args = []
|
| 15 |
+
if cy.version().version_compare('>=3.1.0')
|
| 16 |
+
base_cython_args += ['-Xfreethreading_compatible=True']
|
| 17 |
+
endif
|
| 18 |
+
|
| 19 |
+
_numpy_abs = run_command(py3, ['-c',
|
| 20 |
+
'import os; os.chdir(".."); import numpy; print(os.path.abspath(numpy.get_include() + "../../.."))'],
|
| 21 |
+
check: true).stdout().strip()
|
| 22 |
+
|
| 23 |
+
npymath_path = _numpy_abs / '_core' / 'lib'
|
| 24 |
+
npy_include_path = _numpy_abs / '_core' / 'include'
|
| 25 |
+
npyrandom_path = _numpy_abs / 'random' / 'lib'
|
| 26 |
+
npymath_lib = cc.find_library('npymath', dirs: npymath_path)
|
| 27 |
+
npyrandom_lib = cc.find_library('npyrandom', dirs: npyrandom_path)
|
| 28 |
+
|
| 29 |
+
py3.extension_module(
|
| 30 |
+
'extending_distributions',
|
| 31 |
+
'extending_distributions.pyx',
|
| 32 |
+
install: false,
|
| 33 |
+
include_directories: [npy_include_path],
|
| 34 |
+
dependencies: [npyrandom_lib, npymath_lib],
|
| 35 |
+
cython_args: base_cython_args,
|
| 36 |
+
)
|
| 37 |
+
py3.extension_module(
|
| 38 |
+
'extending',
|
| 39 |
+
'extending.pyx',
|
| 40 |
+
install: false,
|
| 41 |
+
include_directories: [npy_include_path],
|
| 42 |
+
dependencies: [npyrandom_lib, npymath_lib],
|
| 43 |
+
cython_args: base_cython_args,
|
| 44 |
+
)
|
| 45 |
+
py3.extension_module(
|
| 46 |
+
'extending_cpp',
|
| 47 |
+
'extending_distributions.pyx',
|
| 48 |
+
install: false,
|
| 49 |
+
override_options : ['cython_language=cpp'],
|
| 50 |
+
cython_args: base_cython_args + ['--module-name', 'extending_cpp'],
|
| 51 |
+
include_directories: [npy_include_path],
|
| 52 |
+
dependencies: [npyrandom_lib, npymath_lib],
|
| 53 |
+
)
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_examples/numba/extending.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from timeit import timeit
|
| 2 |
+
|
| 3 |
+
import numba as nb
|
| 4 |
+
|
| 5 |
+
import numpy as np
|
| 6 |
+
from numpy.random import PCG64
|
| 7 |
+
|
| 8 |
+
bit_gen = PCG64()
|
| 9 |
+
next_d = bit_gen.cffi.next_double
|
| 10 |
+
state_addr = bit_gen.cffi.state_address
|
| 11 |
+
|
| 12 |
+
def normals(n, state):
|
| 13 |
+
out = np.empty(n)
|
| 14 |
+
for i in range((n + 1) // 2):
|
| 15 |
+
x1 = 2.0 * next_d(state) - 1.0
|
| 16 |
+
x2 = 2.0 * next_d(state) - 1.0
|
| 17 |
+
r2 = x1 * x1 + x2 * x2
|
| 18 |
+
while r2 >= 1.0 or r2 == 0.0:
|
| 19 |
+
x1 = 2.0 * next_d(state) - 1.0
|
| 20 |
+
x2 = 2.0 * next_d(state) - 1.0
|
| 21 |
+
r2 = x1 * x1 + x2 * x2
|
| 22 |
+
f = np.sqrt(-2.0 * np.log(r2) / r2)
|
| 23 |
+
out[2 * i] = f * x1
|
| 24 |
+
if 2 * i + 1 < n:
|
| 25 |
+
out[2 * i + 1] = f * x2
|
| 26 |
+
return out
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
# Compile using Numba
|
| 30 |
+
normalsj = nb.jit(normals, nopython=True)
|
| 31 |
+
# Must use state address not state with numba
|
| 32 |
+
n = 10000
|
| 33 |
+
|
| 34 |
+
def numbacall():
|
| 35 |
+
return normalsj(n, state_addr)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
rg = np.random.Generator(PCG64())
|
| 39 |
+
|
| 40 |
+
def numpycall():
|
| 41 |
+
return rg.normal(size=n)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# Check that the functions work
|
| 45 |
+
r1 = numbacall()
|
| 46 |
+
r2 = numpycall()
|
| 47 |
+
assert r1.shape == (n,)
|
| 48 |
+
assert r1.shape == r2.shape
|
| 49 |
+
|
| 50 |
+
t1 = timeit(numbacall, number=1000)
|
| 51 |
+
print(f'{t1:.2f} secs for {n} PCG64 (Numba/PCG64) gaussian randoms')
|
| 52 |
+
t2 = timeit(numpycall, number=1000)
|
| 53 |
+
print(f'{t2:.2f} secs for {n} PCG64 (NumPy/PCG64) gaussian randoms')
|
| 54 |
+
|
| 55 |
+
# example 2
|
| 56 |
+
|
| 57 |
+
next_u32 = bit_gen.ctypes.next_uint32
|
| 58 |
+
ctypes_state = bit_gen.ctypes.state
|
| 59 |
+
|
| 60 |
+
@nb.jit(nopython=True)
|
| 61 |
+
def bounded_uint(lb, ub, state):
|
| 62 |
+
mask = delta = ub - lb
|
| 63 |
+
mask |= mask >> 1
|
| 64 |
+
mask |= mask >> 2
|
| 65 |
+
mask |= mask >> 4
|
| 66 |
+
mask |= mask >> 8
|
| 67 |
+
mask |= mask >> 16
|
| 68 |
+
|
| 69 |
+
val = next_u32(state) & mask
|
| 70 |
+
while val > delta:
|
| 71 |
+
val = next_u32(state) & mask
|
| 72 |
+
|
| 73 |
+
return lb + val
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
print(bounded_uint(323, 2394691, ctypes_state.value))
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
@nb.jit(nopython=True)
|
| 80 |
+
def bounded_uints(lb, ub, n, state):
|
| 81 |
+
out = np.empty(n, dtype=np.uint32)
|
| 82 |
+
for i in range(n):
|
| 83 |
+
out[i] = bounded_uint(lb, ub, state)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
bounded_uints(323, 2394691, 10000000, ctypes_state.value)
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_examples/numba/extending_distributions.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
r"""
|
| 2 |
+
Building the required library in this example requires a source distribution
|
| 3 |
+
of NumPy or clone of the NumPy git repository since distributions.c is not
|
| 4 |
+
included in binary distributions.
|
| 5 |
+
|
| 6 |
+
On *nix, execute in numpy/random/src/distributions
|
| 7 |
+
|
| 8 |
+
export ${PYTHON_VERSION}=3.8 # Python version
|
| 9 |
+
export PYTHON_INCLUDE=#path to Python's include folder, usually \
|
| 10 |
+
${PYTHON_HOME}/include/python${PYTHON_VERSION}m
|
| 11 |
+
export NUMPY_INCLUDE=#path to numpy's include folder, usually \
|
| 12 |
+
${PYTHON_HOME}/lib/python${PYTHON_VERSION}/site-packages/numpy/_core/include
|
| 13 |
+
gcc -shared -o libdistributions.so -fPIC distributions.c \
|
| 14 |
+
-I${NUMPY_INCLUDE} -I${PYTHON_INCLUDE}
|
| 15 |
+
mv libdistributions.so ../../_examples/numba/
|
| 16 |
+
|
| 17 |
+
On Windows
|
| 18 |
+
|
| 19 |
+
rem PYTHON_HOME and PYTHON_VERSION are setup dependent, this is an example
|
| 20 |
+
set PYTHON_HOME=c:\Anaconda
|
| 21 |
+
set PYTHON_VERSION=38
|
| 22 |
+
cl.exe /LD .\distributions.c -DDLL_EXPORT \
|
| 23 |
+
-I%PYTHON_HOME%\lib\site-packages\numpy\_core\include \
|
| 24 |
+
-I%PYTHON_HOME%\include %PYTHON_HOME%\libs\python%PYTHON_VERSION%.lib
|
| 25 |
+
move distributions.dll ../../_examples/numba/
|
| 26 |
+
"""
|
| 27 |
+
import os
|
| 28 |
+
|
| 29 |
+
import numba as nb
|
| 30 |
+
from cffi import FFI
|
| 31 |
+
|
| 32 |
+
import numpy as np
|
| 33 |
+
from numpy.random import PCG64
|
| 34 |
+
|
| 35 |
+
ffi = FFI()
|
| 36 |
+
if os.path.exists('./distributions.dll'):
|
| 37 |
+
lib = ffi.dlopen('./distributions.dll')
|
| 38 |
+
elif os.path.exists('./libdistributions.so'):
|
| 39 |
+
lib = ffi.dlopen('./libdistributions.so')
|
| 40 |
+
else:
|
| 41 |
+
raise RuntimeError('Required DLL/so file was not found.')
|
| 42 |
+
|
| 43 |
+
ffi.cdef("""
|
| 44 |
+
double random_standard_normal(void *bitgen_state);
|
| 45 |
+
""")
|
| 46 |
+
x = PCG64()
|
| 47 |
+
xffi = x.cffi
|
| 48 |
+
bit_generator = xffi.bit_generator
|
| 49 |
+
|
| 50 |
+
random_standard_normal = lib.random_standard_normal
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def normals(n, bit_generator):
|
| 54 |
+
out = np.empty(n)
|
| 55 |
+
for i in range(n):
|
| 56 |
+
out[i] = random_standard_normal(bit_generator)
|
| 57 |
+
return out
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
normalsj = nb.jit(normals, nopython=True)
|
| 61 |
+
|
| 62 |
+
# Numba requires a memory address for void *
|
| 63 |
+
# Can also get address from x.ctypes.bit_generator.value
|
| 64 |
+
bit_generator_address = int(ffi.cast('uintptr_t', bit_generator))
|
| 65 |
+
|
| 66 |
+
norm = normalsj(1000, bit_generator_address)
|
| 67 |
+
print(norm[:12])
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_generator.cp312-win_amd64.lib
ADDED
|
Binary file (18.4 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/numpy/random/_generator.cp312-win_amd64.pyd
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:dd21763b7cbd0934d11e18a205b43a2f94281749cdc3ae29a8b0551d89bec069
|
| 3 |
+
size 585216
|