Add loan-advisor-agent (part 2)
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .gitattributes +16 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/_pocketfft/LICENSE.md +25 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/_pocketfft/__pycache__/helper.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/_pocketfft/__pycache__/realtransforms.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/_pocketfft/basic.py +251 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/_pocketfft/helper.py +252 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/_pocketfft/pypocketfft.cp312-win_amd64.dll.a +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/_pocketfft/pypocketfft.cp312-win_amd64.pyd +3 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/_pocketfft/realtransforms.py +109 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/_pocketfft/tests/__init__.py +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/_pocketfft/tests/test_basic.py +1011 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/_pocketfft/tests/test_real_transforms.py +506 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/_realtransforms.py +717 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/_realtransforms_backend.py +63 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/tests/__init__.py +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/tests/mock_backend.py +96 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/tests/test_backend.py +98 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/tests/test_basic.py +549 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/tests/test_fftlog.py +214 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/tests/test_helper.py +555 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/tests/test_multithreading.py +84 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/tests/test_real_transforms.py +249 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/__init__.py +103 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/_basic.py +428 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/_helper.py +115 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/_pseudo_diffs.py +554 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/_realtransforms.py +598 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/basic.py +20 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/convolve.cp312-win_amd64.dll.a +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/convolve.cp312-win_amd64.pyd +3 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/helper.py +19 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/pseudo_diffs.py +22 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/realtransforms.py +19 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/tests/__init__.py +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/tests/fftw_double_ref.npz +3 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/tests/fftw_longdouble_ref.npz +3 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/tests/fftw_single_ref.npz +3 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/tests/test.npz +3 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/tests/test_basic.py +877 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/tests/test_helper.py +54 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/tests/test_import.py +33 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/tests/test_pseudo_diffs.py +388 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/tests/test_real_transforms.py +836 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/integrate/LICENSE_DOP +76 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/integrate/__init__.py +122 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/integrate/__pycache__/__init__.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/integrate/__pycache__/_bvp.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/integrate/__pycache__/_cubature.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/integrate/__pycache__/_lebedev.cpython-312.pyc +3 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/integrate/__pycache__/_ode.cpython-312.pyc +0 -0
.gitattributes
CHANGED
|
@@ -562,3 +562,19 @@ loan-advisor-agent/.venv/Lib/site-packages/scipy/cluster/_hierarchy.cp312-win_am
|
|
| 562 |
loan-advisor-agent/.venv/Lib/site-packages/scipy/cluster/_optimal_leaf_ordering.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 563 |
loan-advisor-agent/.venv/Lib/site-packages/scipy/cluster/_vq.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 564 |
loan-advisor-agent/.venv/Lib/site-packages/scipy/constants/__pycache__/_codata.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 562 |
loan-advisor-agent/.venv/Lib/site-packages/scipy/cluster/_optimal_leaf_ordering.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 563 |
loan-advisor-agent/.venv/Lib/site-packages/scipy/cluster/_vq.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 564 |
loan-advisor-agent/.venv/Lib/site-packages/scipy/constants/__pycache__/_codata.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 565 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/_pocketfft/pypocketfft.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 566 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/convolve.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 567 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/integrate/__pycache__/_lebedev.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 568 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/integrate/_odepack.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 569 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/integrate/_quadpack.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 570 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/integrate/_vode.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 571 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/interpolate/__pycache__/_bsplines.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 572 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/interpolate/__pycache__/_fitpack2.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 573 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/interpolate/_dfitpack.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 574 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/interpolate/_dierckx.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 575 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/interpolate/_fitpack.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 576 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/interpolate/_interpnd.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 577 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/interpolate/_ppoly.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 578 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/interpolate/_rbfinterp_pythran.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 579 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/interpolate/_rgi_cython.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 580 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/io/_fast_matrix_market/_fmm_core.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/_pocketfft/LICENSE.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Copyright (C) 2010-2019 Max-Planck-Society
|
| 2 |
+
All rights reserved.
|
| 3 |
+
|
| 4 |
+
Redistribution and use in source and binary forms, with or without modification,
|
| 5 |
+
are permitted provided that the following conditions are met:
|
| 6 |
+
|
| 7 |
+
* Redistributions of source code must retain the above copyright notice, this
|
| 8 |
+
list of conditions and the following disclaimer.
|
| 9 |
+
* Redistributions in binary form must reproduce the above copyright notice, this
|
| 10 |
+
list of conditions and the following disclaimer in the documentation and/or
|
| 11 |
+
other materials provided with the distribution.
|
| 12 |
+
* Neither the name of the copyright holder nor the names of its contributors may
|
| 13 |
+
be used to endorse or promote products derived from this software without
|
| 14 |
+
specific prior written permission.
|
| 15 |
+
|
| 16 |
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
| 17 |
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
| 18 |
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
| 19 |
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
| 20 |
+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
| 21 |
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
| 22 |
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
| 23 |
+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
| 24 |
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
| 25 |
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/_pocketfft/__pycache__/helper.cpython-312.pyc
ADDED
|
Binary file (9.98 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/_pocketfft/__pycache__/realtransforms.cpython-312.pyc
ADDED
|
Binary file (4.21 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/_pocketfft/basic.py
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Discrete Fourier Transforms - basic.py
|
| 3 |
+
"""
|
| 4 |
+
import numpy as np
|
| 5 |
+
import functools
|
| 6 |
+
from . import pypocketfft as pfft
|
| 7 |
+
from .helper import (_asfarray, _init_nd_shape_and_axes, _datacopied,
|
| 8 |
+
_fix_shape, _fix_shape_1d, _normalization,
|
| 9 |
+
_workers)
|
| 10 |
+
|
| 11 |
+
def c2c(forward, x, n=None, axis=-1, norm=None, overwrite_x=False,
|
| 12 |
+
workers=None, *, plan=None):
|
| 13 |
+
""" Return discrete Fourier transform of real or complex sequence. """
|
| 14 |
+
if plan is not None:
|
| 15 |
+
raise NotImplementedError('Passing a precomputed plan is not yet '
|
| 16 |
+
'supported by scipy.fft functions')
|
| 17 |
+
tmp = _asfarray(x)
|
| 18 |
+
overwrite_x = overwrite_x or _datacopied(tmp, x)
|
| 19 |
+
norm = _normalization(norm, forward)
|
| 20 |
+
workers = _workers(workers)
|
| 21 |
+
|
| 22 |
+
if n is not None:
|
| 23 |
+
tmp, copied = _fix_shape_1d(tmp, n, axis)
|
| 24 |
+
overwrite_x = overwrite_x or copied
|
| 25 |
+
elif tmp.shape[axis] < 1:
|
| 26 |
+
message = f"invalid number of data points ({tmp.shape[axis]}) specified"
|
| 27 |
+
raise ValueError(message)
|
| 28 |
+
|
| 29 |
+
out = (tmp if overwrite_x and tmp.dtype.kind == 'c' else None)
|
| 30 |
+
|
| 31 |
+
return pfft.c2c(tmp, (axis,), forward, norm, out, workers)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
fft = functools.partial(c2c, True)
|
| 35 |
+
fft.__name__ = 'fft'
|
| 36 |
+
ifft = functools.partial(c2c, False)
|
| 37 |
+
ifft.__name__ = 'ifft'
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def r2c(forward, x, n=None, axis=-1, norm=None, overwrite_x=False,
|
| 41 |
+
workers=None, *, plan=None):
|
| 42 |
+
"""
|
| 43 |
+
Discrete Fourier transform of a real sequence.
|
| 44 |
+
"""
|
| 45 |
+
if plan is not None:
|
| 46 |
+
raise NotImplementedError('Passing a precomputed plan is not yet '
|
| 47 |
+
'supported by scipy.fft functions')
|
| 48 |
+
tmp = _asfarray(x)
|
| 49 |
+
norm = _normalization(norm, forward)
|
| 50 |
+
workers = _workers(workers)
|
| 51 |
+
|
| 52 |
+
if not np.isrealobj(tmp):
|
| 53 |
+
raise TypeError("x must be a real sequence")
|
| 54 |
+
|
| 55 |
+
if n is not None:
|
| 56 |
+
tmp, _ = _fix_shape_1d(tmp, n, axis)
|
| 57 |
+
elif tmp.shape[axis] < 1:
|
| 58 |
+
raise ValueError(f"invalid number of data points ({tmp.shape[axis]}) specified")
|
| 59 |
+
|
| 60 |
+
# Note: overwrite_x is not utilised
|
| 61 |
+
return pfft.r2c(tmp, (axis,), forward, norm, None, workers)
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
rfft = functools.partial(r2c, True)
|
| 65 |
+
rfft.__name__ = 'rfft'
|
| 66 |
+
ihfft = functools.partial(r2c, False)
|
| 67 |
+
ihfft.__name__ = 'ihfft'
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def c2r(forward, x, n=None, axis=-1, norm=None, overwrite_x=False,
|
| 71 |
+
workers=None, *, plan=None):
|
| 72 |
+
"""
|
| 73 |
+
Return inverse discrete Fourier transform of real sequence x.
|
| 74 |
+
"""
|
| 75 |
+
if plan is not None:
|
| 76 |
+
raise NotImplementedError('Passing a precomputed plan is not yet '
|
| 77 |
+
'supported by scipy.fft functions')
|
| 78 |
+
tmp = _asfarray(x)
|
| 79 |
+
norm = _normalization(norm, forward)
|
| 80 |
+
workers = _workers(workers)
|
| 81 |
+
|
| 82 |
+
# TODO: Optimize for hermitian and real?
|
| 83 |
+
if np.isrealobj(tmp):
|
| 84 |
+
tmp = tmp + 0.j
|
| 85 |
+
|
| 86 |
+
# Last axis utilizes hermitian symmetry
|
| 87 |
+
if n is None:
|
| 88 |
+
n = (tmp.shape[axis] - 1) * 2
|
| 89 |
+
if n < 1:
|
| 90 |
+
raise ValueError(f"Invalid number of data points ({n}) specified")
|
| 91 |
+
else:
|
| 92 |
+
tmp, _ = _fix_shape_1d(tmp, (n//2) + 1, axis)
|
| 93 |
+
|
| 94 |
+
# Note: overwrite_x is not utilized
|
| 95 |
+
return pfft.c2r(tmp, (axis,), n, forward, norm, None, workers)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
hfft = functools.partial(c2r, True)
|
| 99 |
+
hfft.__name__ = 'hfft'
|
| 100 |
+
irfft = functools.partial(c2r, False)
|
| 101 |
+
irfft.__name__ = 'irfft'
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
def hfft2(x, s=None, axes=(-2,-1), norm=None, overwrite_x=False, workers=None,
|
| 105 |
+
*, plan=None):
|
| 106 |
+
"""
|
| 107 |
+
2-D discrete Fourier transform of a Hermitian sequence
|
| 108 |
+
"""
|
| 109 |
+
if plan is not None:
|
| 110 |
+
raise NotImplementedError('Passing a precomputed plan is not yet '
|
| 111 |
+
'supported by scipy.fft functions')
|
| 112 |
+
return hfftn(x, s, axes, norm, overwrite_x, workers)
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def ihfft2(x, s=None, axes=(-2,-1), norm=None, overwrite_x=False, workers=None,
|
| 116 |
+
*, plan=None):
|
| 117 |
+
"""
|
| 118 |
+
2-D discrete inverse Fourier transform of a Hermitian sequence
|
| 119 |
+
"""
|
| 120 |
+
if plan is not None:
|
| 121 |
+
raise NotImplementedError('Passing a precomputed plan is not yet '
|
| 122 |
+
'supported by scipy.fft functions')
|
| 123 |
+
return ihfftn(x, s, axes, norm, overwrite_x, workers)
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
def c2cn(forward, x, s=None, axes=None, norm=None, overwrite_x=False,
|
| 127 |
+
workers=None, *, plan=None):
|
| 128 |
+
"""
|
| 129 |
+
Return multidimensional discrete Fourier transform.
|
| 130 |
+
"""
|
| 131 |
+
if plan is not None:
|
| 132 |
+
raise NotImplementedError('Passing a precomputed plan is not yet '
|
| 133 |
+
'supported by scipy.fft functions')
|
| 134 |
+
tmp = _asfarray(x)
|
| 135 |
+
|
| 136 |
+
shape, axes = _init_nd_shape_and_axes(tmp, s, axes)
|
| 137 |
+
overwrite_x = overwrite_x or _datacopied(tmp, x)
|
| 138 |
+
workers = _workers(workers)
|
| 139 |
+
|
| 140 |
+
if len(axes) == 0:
|
| 141 |
+
return x
|
| 142 |
+
|
| 143 |
+
tmp, copied = _fix_shape(tmp, shape, axes)
|
| 144 |
+
overwrite_x = overwrite_x or copied
|
| 145 |
+
|
| 146 |
+
norm = _normalization(norm, forward)
|
| 147 |
+
out = (tmp if overwrite_x and tmp.dtype.kind == 'c' else None)
|
| 148 |
+
|
| 149 |
+
return pfft.c2c(tmp, axes, forward, norm, out, workers)
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
fftn = functools.partial(c2cn, True)
|
| 153 |
+
fftn.__name__ = 'fftn'
|
| 154 |
+
ifftn = functools.partial(c2cn, False)
|
| 155 |
+
ifftn.__name__ = 'ifftn'
|
| 156 |
+
|
| 157 |
+
def r2cn(forward, x, s=None, axes=None, norm=None, overwrite_x=False,
|
| 158 |
+
workers=None, *, plan=None):
|
| 159 |
+
"""Return multidimensional discrete Fourier transform of real input"""
|
| 160 |
+
if plan is not None:
|
| 161 |
+
raise NotImplementedError('Passing a precomputed plan is not yet '
|
| 162 |
+
'supported by scipy.fft functions')
|
| 163 |
+
tmp = _asfarray(x)
|
| 164 |
+
|
| 165 |
+
if not np.isrealobj(tmp):
|
| 166 |
+
raise TypeError("x must be a real sequence")
|
| 167 |
+
|
| 168 |
+
shape, axes = _init_nd_shape_and_axes(tmp, s, axes)
|
| 169 |
+
tmp, _ = _fix_shape(tmp, shape, axes)
|
| 170 |
+
norm = _normalization(norm, forward)
|
| 171 |
+
workers = _workers(workers)
|
| 172 |
+
|
| 173 |
+
if len(axes) == 0:
|
| 174 |
+
raise ValueError("at least 1 axis must be transformed")
|
| 175 |
+
|
| 176 |
+
# Note: overwrite_x is not utilized
|
| 177 |
+
return pfft.r2c(tmp, axes, forward, norm, None, workers)
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
rfftn = functools.partial(r2cn, True)
|
| 181 |
+
rfftn.__name__ = 'rfftn'
|
| 182 |
+
ihfftn = functools.partial(r2cn, False)
|
| 183 |
+
ihfftn.__name__ = 'ihfftn'
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
def c2rn(forward, x, s=None, axes=None, norm=None, overwrite_x=False,
|
| 187 |
+
workers=None, *, plan=None):
|
| 188 |
+
"""Multidimensional inverse discrete fourier transform with real output"""
|
| 189 |
+
if plan is not None:
|
| 190 |
+
raise NotImplementedError('Passing a precomputed plan is not yet '
|
| 191 |
+
'supported by scipy.fft functions')
|
| 192 |
+
tmp = _asfarray(x)
|
| 193 |
+
|
| 194 |
+
# TODO: Optimize for hermitian and real?
|
| 195 |
+
if np.isrealobj(tmp):
|
| 196 |
+
tmp = tmp + 0.j
|
| 197 |
+
|
| 198 |
+
noshape = s is None
|
| 199 |
+
shape, axes = _init_nd_shape_and_axes(tmp, s, axes)
|
| 200 |
+
|
| 201 |
+
if len(axes) == 0:
|
| 202 |
+
raise ValueError("at least 1 axis must be transformed")
|
| 203 |
+
|
| 204 |
+
shape = list(shape)
|
| 205 |
+
if noshape:
|
| 206 |
+
shape[-1] = (x.shape[axes[-1]] - 1) * 2
|
| 207 |
+
|
| 208 |
+
norm = _normalization(norm, forward)
|
| 209 |
+
workers = _workers(workers)
|
| 210 |
+
|
| 211 |
+
# Last axis utilizes hermitian symmetry
|
| 212 |
+
lastsize = shape[-1]
|
| 213 |
+
shape[-1] = (shape[-1] // 2) + 1
|
| 214 |
+
|
| 215 |
+
tmp, _ = tuple(_fix_shape(tmp, shape, axes))
|
| 216 |
+
|
| 217 |
+
# Note: overwrite_x is not utilized
|
| 218 |
+
return pfft.c2r(tmp, axes, lastsize, forward, norm, None, workers)
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
hfftn = functools.partial(c2rn, True)
|
| 222 |
+
hfftn.__name__ = 'hfftn'
|
| 223 |
+
irfftn = functools.partial(c2rn, False)
|
| 224 |
+
irfftn.__name__ = 'irfftn'
|
| 225 |
+
|
| 226 |
+
|
| 227 |
+
def r2r_fftpack(forward, x, n=None, axis=-1, norm=None, overwrite_x=False):
|
| 228 |
+
"""FFT of a real sequence, returning fftpack half complex format"""
|
| 229 |
+
tmp = _asfarray(x)
|
| 230 |
+
overwrite_x = overwrite_x or _datacopied(tmp, x)
|
| 231 |
+
norm = _normalization(norm, forward)
|
| 232 |
+
workers = _workers(None)
|
| 233 |
+
|
| 234 |
+
if tmp.dtype.kind == 'c':
|
| 235 |
+
raise TypeError('x must be a real sequence')
|
| 236 |
+
|
| 237 |
+
if n is not None:
|
| 238 |
+
tmp, copied = _fix_shape_1d(tmp, n, axis)
|
| 239 |
+
overwrite_x = overwrite_x or copied
|
| 240 |
+
elif tmp.shape[axis] < 1:
|
| 241 |
+
raise ValueError(f"invalid number of data points ({tmp.shape[axis]}) specified")
|
| 242 |
+
|
| 243 |
+
out = (tmp if overwrite_x else None)
|
| 244 |
+
|
| 245 |
+
return pfft.r2r_fftpack(tmp, (axis,), forward, forward, norm, out, workers)
|
| 246 |
+
|
| 247 |
+
|
| 248 |
+
rfft_fftpack = functools.partial(r2r_fftpack, True)
|
| 249 |
+
rfft_fftpack.__name__ = 'rfft_fftpack'
|
| 250 |
+
irfft_fftpack = functools.partial(r2r_fftpack, False)
|
| 251 |
+
irfft_fftpack.__name__ = 'irfft_fftpack'
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/_pocketfft/helper.py
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from numbers import Number
|
| 2 |
+
import operator
|
| 3 |
+
import os
|
| 4 |
+
import threading
|
| 5 |
+
import contextlib
|
| 6 |
+
|
| 7 |
+
import numpy as np
|
| 8 |
+
|
| 9 |
+
from scipy._lib._util import copy_if_needed
|
| 10 |
+
from scipy._lib._array_api import xp_capabilities
|
| 11 |
+
|
| 12 |
+
# good_size is exposed (and used) from this import
|
| 13 |
+
from .pypocketfft import good_size, prev_good_size
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
__all__ = ['good_size', 'prev_good_size', 'set_workers', 'get_workers']
|
| 17 |
+
|
| 18 |
+
_config = threading.local()
|
| 19 |
+
_cpu_count = os.cpu_count()
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def _iterable_of_int(x, name=None):
|
| 23 |
+
"""Convert ``x`` to an iterable sequence of int
|
| 24 |
+
|
| 25 |
+
Parameters
|
| 26 |
+
----------
|
| 27 |
+
x : value, or sequence of values, convertible to int
|
| 28 |
+
name : str, optional
|
| 29 |
+
Name of the argument being converted, only used in the error message
|
| 30 |
+
|
| 31 |
+
Returns
|
| 32 |
+
-------
|
| 33 |
+
y : ``List[int]``
|
| 34 |
+
"""
|
| 35 |
+
if isinstance(x, Number):
|
| 36 |
+
x = (x,)
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
x = [operator.index(a) for a in x]
|
| 40 |
+
except TypeError as e:
|
| 41 |
+
name = name or "value"
|
| 42 |
+
raise ValueError(f"{name} must be a scalar or iterable of integers") from e
|
| 43 |
+
|
| 44 |
+
return x
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def _init_nd_shape_and_axes(x, shape, axes):
|
| 48 |
+
"""
|
| 49 |
+
Handle shape and axes arguments for N-D transforms.
|
| 50 |
+
|
| 51 |
+
Returns the shape and axes in a standard form, taking into account negative
|
| 52 |
+
values and checking for various potential errors.
|
| 53 |
+
|
| 54 |
+
Parameters
|
| 55 |
+
----------
|
| 56 |
+
x : ndarray
|
| 57 |
+
The input array.
|
| 58 |
+
shape : int or array_like of ints or None
|
| 59 |
+
The shape of the result. If both `shape` and `axes` (see below) are
|
| 60 |
+
None, `shape` is ``x.shape``; if `shape` is None but `axes` is
|
| 61 |
+
not None, then `shape` is ``numpy.take(x.shape, axes, axis=0)``.
|
| 62 |
+
If `shape` is -1, the size of the corresponding dimension of `x` is
|
| 63 |
+
used.
|
| 64 |
+
axes : int or array_like of ints or None
|
| 65 |
+
Axes along which the calculation is computed.
|
| 66 |
+
The default is over all axes.
|
| 67 |
+
Negative indices are automatically converted to their positive
|
| 68 |
+
counterparts.
|
| 69 |
+
|
| 70 |
+
Returns
|
| 71 |
+
-------
|
| 72 |
+
shape : tuple
|
| 73 |
+
The shape of the result as a tuple of integers.
|
| 74 |
+
axes : list
|
| 75 |
+
Axes along which the calculation is computed, as a list of integers.
|
| 76 |
+
"""
|
| 77 |
+
noshape = shape is None
|
| 78 |
+
noaxes = axes is None
|
| 79 |
+
|
| 80 |
+
if not noaxes:
|
| 81 |
+
axes = _iterable_of_int(axes, 'axes')
|
| 82 |
+
axes = [a + x.ndim if a < 0 else a for a in axes]
|
| 83 |
+
|
| 84 |
+
if any(a >= x.ndim or a < 0 for a in axes):
|
| 85 |
+
raise ValueError("axes exceeds dimensionality of input")
|
| 86 |
+
if len(set(axes)) != len(axes):
|
| 87 |
+
raise ValueError("all axes must be unique")
|
| 88 |
+
|
| 89 |
+
if not noshape:
|
| 90 |
+
shape = _iterable_of_int(shape, 'shape')
|
| 91 |
+
|
| 92 |
+
if axes and len(axes) != len(shape):
|
| 93 |
+
raise ValueError("when given, axes and shape arguments"
|
| 94 |
+
" have to be of the same length")
|
| 95 |
+
if noaxes:
|
| 96 |
+
if len(shape) > x.ndim:
|
| 97 |
+
raise ValueError("shape requires more axes than are present")
|
| 98 |
+
axes = range(x.ndim - len(shape), x.ndim)
|
| 99 |
+
|
| 100 |
+
shape = [x.shape[a] if s == -1 else s for s, a in zip(shape, axes)]
|
| 101 |
+
elif noaxes:
|
| 102 |
+
shape = list(x.shape)
|
| 103 |
+
axes = range(x.ndim)
|
| 104 |
+
else:
|
| 105 |
+
shape = [x.shape[a] for a in axes]
|
| 106 |
+
|
| 107 |
+
if any(s < 1 for s in shape):
|
| 108 |
+
raise ValueError(
|
| 109 |
+
f"invalid number of data points ({shape}) specified")
|
| 110 |
+
|
| 111 |
+
return tuple(shape), list(axes)
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
def _asfarray(x):
|
| 115 |
+
"""
|
| 116 |
+
Convert to array with floating or complex dtype.
|
| 117 |
+
|
| 118 |
+
float16 values are also promoted to float32.
|
| 119 |
+
"""
|
| 120 |
+
if not hasattr(x, "dtype"):
|
| 121 |
+
x = np.asarray(x)
|
| 122 |
+
|
| 123 |
+
if x.dtype == np.float16:
|
| 124 |
+
return np.asarray(x, np.float32)
|
| 125 |
+
elif x.dtype.kind not in 'fc':
|
| 126 |
+
return np.asarray(x, np.float64)
|
| 127 |
+
|
| 128 |
+
# Require native byte order
|
| 129 |
+
dtype = x.dtype.newbyteorder('=')
|
| 130 |
+
# Always align input
|
| 131 |
+
copy = True if not x.flags['ALIGNED'] else copy_if_needed
|
| 132 |
+
return np.array(x, dtype=dtype, copy=copy)
|
| 133 |
+
|
| 134 |
+
def _datacopied(arr, original):
|
| 135 |
+
"""
|
| 136 |
+
Strict check for `arr` not sharing any data with `original`,
|
| 137 |
+
under the assumption that arr = asarray(original)
|
| 138 |
+
"""
|
| 139 |
+
if arr is original:
|
| 140 |
+
return False
|
| 141 |
+
if not isinstance(original, np.ndarray) and hasattr(original, '__array__'):
|
| 142 |
+
return False
|
| 143 |
+
return arr.base is None
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
def _fix_shape(x, shape, axes):
|
| 147 |
+
"""Internal auxiliary function for _raw_fft, _raw_fftnd."""
|
| 148 |
+
must_copy = False
|
| 149 |
+
|
| 150 |
+
# Build an nd slice with the dimensions to be read from x
|
| 151 |
+
index = [slice(None)]*x.ndim
|
| 152 |
+
for n, ax in zip(shape, axes):
|
| 153 |
+
if x.shape[ax] >= n:
|
| 154 |
+
index[ax] = slice(0, n)
|
| 155 |
+
else:
|
| 156 |
+
index[ax] = slice(0, x.shape[ax])
|
| 157 |
+
must_copy = True
|
| 158 |
+
|
| 159 |
+
index = tuple(index)
|
| 160 |
+
|
| 161 |
+
if not must_copy:
|
| 162 |
+
return x[index], False
|
| 163 |
+
|
| 164 |
+
s = list(x.shape)
|
| 165 |
+
for n, axis in zip(shape, axes):
|
| 166 |
+
s[axis] = n
|
| 167 |
+
|
| 168 |
+
z = np.zeros(s, x.dtype)
|
| 169 |
+
z[index] = x[index]
|
| 170 |
+
return z, True
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
def _fix_shape_1d(x, n, axis):
|
| 174 |
+
if n < 1:
|
| 175 |
+
raise ValueError(
|
| 176 |
+
f"invalid number of data points ({n}) specified")
|
| 177 |
+
|
| 178 |
+
return _fix_shape(x, (n,), (axis,))
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
_NORM_MAP = {None: 0, 'backward': 0, 'ortho': 1, 'forward': 2}
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
def _normalization(norm, forward):
|
| 185 |
+
"""Returns the pypocketfft normalization mode from the norm argument"""
|
| 186 |
+
try:
|
| 187 |
+
inorm = _NORM_MAP[norm]
|
| 188 |
+
return inorm if forward else (2 - inorm)
|
| 189 |
+
except KeyError:
|
| 190 |
+
raise ValueError(
|
| 191 |
+
f'Invalid norm value {norm!r}, should '
|
| 192 |
+
'be "backward", "ortho" or "forward"') from None
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
def _workers(workers):
|
| 196 |
+
if workers is None:
|
| 197 |
+
return getattr(_config, 'default_workers', 1)
|
| 198 |
+
|
| 199 |
+
if workers < 0:
|
| 200 |
+
if workers >= -_cpu_count:
|
| 201 |
+
workers += 1 + _cpu_count
|
| 202 |
+
else:
|
| 203 |
+
raise ValueError(f"workers value out of range; got {workers}, must not be"
|
| 204 |
+
f" less than {-_cpu_count}")
|
| 205 |
+
elif workers == 0:
|
| 206 |
+
raise ValueError("workers must not be zero")
|
| 207 |
+
|
| 208 |
+
return workers
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
@xp_capabilities(out_of_scope=True)
|
| 212 |
+
@contextlib.contextmanager
|
| 213 |
+
def set_workers(workers):
|
| 214 |
+
"""Context manager for the default number of workers used in `scipy.fft`
|
| 215 |
+
|
| 216 |
+
Parameters
|
| 217 |
+
----------
|
| 218 |
+
workers : int
|
| 219 |
+
The default number of workers to use
|
| 220 |
+
|
| 221 |
+
Examples
|
| 222 |
+
--------
|
| 223 |
+
>>> import numpy as np
|
| 224 |
+
>>> from scipy import fft, signal
|
| 225 |
+
>>> rng = np.random.default_rng()
|
| 226 |
+
>>> x = rng.standard_normal((128, 64))
|
| 227 |
+
>>> with fft.set_workers(4):
|
| 228 |
+
... y = signal.fftconvolve(x, x)
|
| 229 |
+
|
| 230 |
+
"""
|
| 231 |
+
old_workers = get_workers()
|
| 232 |
+
_config.default_workers = _workers(operator.index(workers))
|
| 233 |
+
try:
|
| 234 |
+
yield
|
| 235 |
+
finally:
|
| 236 |
+
_config.default_workers = old_workers
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
@xp_capabilities(out_of_scope=True)
|
| 240 |
+
def get_workers():
|
| 241 |
+
"""Returns the default number of workers within the current context
|
| 242 |
+
|
| 243 |
+
Examples
|
| 244 |
+
--------
|
| 245 |
+
>>> from scipy import fft
|
| 246 |
+
>>> fft.get_workers()
|
| 247 |
+
1
|
| 248 |
+
>>> with fft.set_workers(4):
|
| 249 |
+
... fft.get_workers()
|
| 250 |
+
4
|
| 251 |
+
"""
|
| 252 |
+
return getattr(_config, 'default_workers', 1)
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/_pocketfft/pypocketfft.cp312-win_amd64.dll.a
ADDED
|
Binary file (1.59 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/_pocketfft/pypocketfft.cp312-win_amd64.pyd
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:dd089b853dd1382b4ce73a138076efa8dda9391efa6090ee31fe607e2bdee313
|
| 3 |
+
size 1097216
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/_pocketfft/realtransforms.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from . import pypocketfft as pfft
|
| 3 |
+
from .helper import (_asfarray, _init_nd_shape_and_axes, _datacopied,
|
| 4 |
+
_fix_shape, _fix_shape_1d, _normalization, _workers)
|
| 5 |
+
import functools
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def _r2r(forward, transform, x, type=2, n=None, axis=-1, norm=None,
|
| 9 |
+
overwrite_x=False, workers=None, orthogonalize=None):
|
| 10 |
+
"""Forward or backward 1-D DCT/DST
|
| 11 |
+
|
| 12 |
+
Parameters
|
| 13 |
+
----------
|
| 14 |
+
forward : bool
|
| 15 |
+
Transform direction (determines type and normalisation)
|
| 16 |
+
transform : {pypocketfft.dct, pypocketfft.dst}
|
| 17 |
+
The transform to perform
|
| 18 |
+
"""
|
| 19 |
+
tmp = _asfarray(x)
|
| 20 |
+
overwrite_x = overwrite_x or _datacopied(tmp, x)
|
| 21 |
+
norm = _normalization(norm, forward)
|
| 22 |
+
workers = _workers(workers)
|
| 23 |
+
|
| 24 |
+
if not forward:
|
| 25 |
+
if type == 2:
|
| 26 |
+
type = 3
|
| 27 |
+
elif type == 3:
|
| 28 |
+
type = 2
|
| 29 |
+
|
| 30 |
+
if n is not None:
|
| 31 |
+
tmp, copied = _fix_shape_1d(tmp, n, axis)
|
| 32 |
+
overwrite_x = overwrite_x or copied
|
| 33 |
+
elif tmp.shape[axis] < 1:
|
| 34 |
+
raise ValueError(f"invalid number of data points ({tmp.shape[axis]}) specified")
|
| 35 |
+
|
| 36 |
+
out = (tmp if overwrite_x else None)
|
| 37 |
+
|
| 38 |
+
# For complex input, transform real and imaginary components separably
|
| 39 |
+
if np.iscomplexobj(x):
|
| 40 |
+
out = np.empty_like(tmp) if out is None else out
|
| 41 |
+
transform(tmp.real, type, (axis,), norm, out.real, workers)
|
| 42 |
+
transform(tmp.imag, type, (axis,), norm, out.imag, workers)
|
| 43 |
+
return out
|
| 44 |
+
|
| 45 |
+
return transform(tmp, type, (axis,), norm, out, workers, orthogonalize)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
dct = functools.partial(_r2r, True, pfft.dct)
|
| 49 |
+
dct.__name__ = 'dct'
|
| 50 |
+
idct = functools.partial(_r2r, False, pfft.dct)
|
| 51 |
+
idct.__name__ = 'idct'
|
| 52 |
+
|
| 53 |
+
dst = functools.partial(_r2r, True, pfft.dst)
|
| 54 |
+
dst.__name__ = 'dst'
|
| 55 |
+
idst = functools.partial(_r2r, False, pfft.dst)
|
| 56 |
+
idst.__name__ = 'idst'
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def _r2rn(forward, transform, x, type=2, s=None, axes=None, norm=None,
|
| 60 |
+
overwrite_x=False, workers=None, orthogonalize=None):
|
| 61 |
+
"""Forward or backward nd DCT/DST
|
| 62 |
+
|
| 63 |
+
Parameters
|
| 64 |
+
----------
|
| 65 |
+
forward : bool
|
| 66 |
+
Transform direction (determines type and normalisation)
|
| 67 |
+
transform : {pypocketfft.dct, pypocketfft.dst}
|
| 68 |
+
The transform to perform
|
| 69 |
+
"""
|
| 70 |
+
tmp = _asfarray(x)
|
| 71 |
+
|
| 72 |
+
shape, axes = _init_nd_shape_and_axes(tmp, s, axes)
|
| 73 |
+
overwrite_x = overwrite_x or _datacopied(tmp, x)
|
| 74 |
+
|
| 75 |
+
if len(axes) == 0:
|
| 76 |
+
return x
|
| 77 |
+
|
| 78 |
+
tmp, copied = _fix_shape(tmp, shape, axes)
|
| 79 |
+
overwrite_x = overwrite_x or copied
|
| 80 |
+
|
| 81 |
+
if not forward:
|
| 82 |
+
if type == 2:
|
| 83 |
+
type = 3
|
| 84 |
+
elif type == 3:
|
| 85 |
+
type = 2
|
| 86 |
+
|
| 87 |
+
norm = _normalization(norm, forward)
|
| 88 |
+
workers = _workers(workers)
|
| 89 |
+
out = (tmp if overwrite_x else None)
|
| 90 |
+
|
| 91 |
+
# For complex input, transform real and imaginary components separably
|
| 92 |
+
if np.iscomplexobj(x):
|
| 93 |
+
out = np.empty_like(tmp) if out is None else out
|
| 94 |
+
transform(tmp.real, type, axes, norm, out.real, workers)
|
| 95 |
+
transform(tmp.imag, type, axes, norm, out.imag, workers)
|
| 96 |
+
return out
|
| 97 |
+
|
| 98 |
+
return transform(tmp, type, axes, norm, out, workers, orthogonalize)
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
dctn = functools.partial(_r2rn, True, pfft.dct)
|
| 102 |
+
dctn.__name__ = 'dctn'
|
| 103 |
+
idctn = functools.partial(_r2rn, False, pfft.dct)
|
| 104 |
+
idctn.__name__ = 'idctn'
|
| 105 |
+
|
| 106 |
+
dstn = functools.partial(_r2rn, True, pfft.dst)
|
| 107 |
+
dstn.__name__ = 'dstn'
|
| 108 |
+
idstn = functools.partial(_r2rn, False, pfft.dst)
|
| 109 |
+
idstn.__name__ = 'idstn'
|
loan-advisor-agent/.venv/Lib/site-packages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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Created by Pearu Peterson, September 2002
|
| 2 |
+
|
| 3 |
+
from numpy.testing import (assert_, assert_equal, assert_array_almost_equal,
|
| 4 |
+
assert_array_almost_equal_nulp, assert_array_less,
|
| 5 |
+
assert_allclose)
|
| 6 |
+
import pytest
|
| 7 |
+
from pytest import raises as assert_raises
|
| 8 |
+
from scipy.fft._pocketfft import (ifft, fft, fftn, ifftn,
|
| 9 |
+
rfft, irfft, rfftn, irfftn,
|
| 10 |
+
hfft, ihfft, hfftn, ihfftn)
|
| 11 |
+
|
| 12 |
+
from numpy import (arange, array, asarray, zeros, dot, exp, pi,
|
| 13 |
+
swapaxes, cdouble)
|
| 14 |
+
import numpy as np
|
| 15 |
+
import numpy.fft
|
| 16 |
+
from numpy.random import rand
|
| 17 |
+
|
| 18 |
+
# "large" composite numbers supported by FFT._PYPOCKETFFT
|
| 19 |
+
LARGE_COMPOSITE_SIZES = [
|
| 20 |
+
2**13,
|
| 21 |
+
2**5 * 3**5,
|
| 22 |
+
2**3 * 3**3 * 5**2,
|
| 23 |
+
]
|
| 24 |
+
SMALL_COMPOSITE_SIZES = [
|
| 25 |
+
2,
|
| 26 |
+
2*3*5,
|
| 27 |
+
2*2*3*3,
|
| 28 |
+
]
|
| 29 |
+
# prime
|
| 30 |
+
LARGE_PRIME_SIZES = [
|
| 31 |
+
2011
|
| 32 |
+
]
|
| 33 |
+
SMALL_PRIME_SIZES = [
|
| 34 |
+
29
|
| 35 |
+
]
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def _assert_close_in_norm(x, y, rtol, size, rdt):
|
| 39 |
+
# helper function for testing
|
| 40 |
+
err_msg = f"size: {size} rdt: {rdt}"
|
| 41 |
+
assert_array_less(np.linalg.norm(x - y), rtol*np.linalg.norm(x), err_msg)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def random(size):
|
| 45 |
+
return rand(*size)
|
| 46 |
+
|
| 47 |
+
def swap_byteorder(arr):
|
| 48 |
+
"""Returns the same array with swapped byteorder"""
|
| 49 |
+
dtype = arr.dtype.newbyteorder('S')
|
| 50 |
+
return arr.astype(dtype)
|
| 51 |
+
|
| 52 |
+
def direct_dft(x):
|
| 53 |
+
x = asarray(x)
|
| 54 |
+
n = len(x)
|
| 55 |
+
y = zeros(n, dtype=cdouble)
|
| 56 |
+
w = -arange(n)*(2j*pi/n)
|
| 57 |
+
for i in range(n):
|
| 58 |
+
y[i] = dot(exp(i*w), x)
|
| 59 |
+
return y
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def direct_idft(x):
|
| 63 |
+
x = asarray(x)
|
| 64 |
+
n = len(x)
|
| 65 |
+
y = zeros(n, dtype=cdouble)
|
| 66 |
+
w = arange(n)*(2j*pi/n)
|
| 67 |
+
for i in range(n):
|
| 68 |
+
y[i] = dot(exp(i*w), x)/n
|
| 69 |
+
return y
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def direct_dftn(x):
|
| 73 |
+
x = asarray(x)
|
| 74 |
+
for axis in range(x.ndim):
|
| 75 |
+
x = fft(x, axis=axis)
|
| 76 |
+
return x
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def direct_idftn(x):
|
| 80 |
+
x = asarray(x)
|
| 81 |
+
for axis in range(x.ndim):
|
| 82 |
+
x = ifft(x, axis=axis)
|
| 83 |
+
return x
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def direct_rdft(x):
|
| 87 |
+
x = asarray(x)
|
| 88 |
+
n = len(x)
|
| 89 |
+
w = -arange(n)*(2j*pi/n)
|
| 90 |
+
y = zeros(n//2+1, dtype=cdouble)
|
| 91 |
+
for i in range(n//2+1):
|
| 92 |
+
y[i] = dot(exp(i*w), x)
|
| 93 |
+
return y
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
def direct_irdft(x, n):
|
| 97 |
+
x = asarray(x)
|
| 98 |
+
x1 = zeros(n, dtype=cdouble)
|
| 99 |
+
for i in range(n//2+1):
|
| 100 |
+
x1[i] = x[i]
|
| 101 |
+
if i > 0 and 2*i < n:
|
| 102 |
+
x1[n-i] = np.conj(x[i])
|
| 103 |
+
return direct_idft(x1).real
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def direct_rdftn(x):
|
| 107 |
+
return fftn(rfft(x), axes=range(x.ndim - 1))
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
class _TestFFTBase:
|
| 111 |
+
def setup_method(self):
|
| 112 |
+
self.cdt = None
|
| 113 |
+
self.rdt = None
|
| 114 |
+
np.random.seed(1234)
|
| 115 |
+
|
| 116 |
+
def test_definition(self):
|
| 117 |
+
x = np.array([1,2,3,4+1j,1,2,3,4+2j], dtype=self.cdt)
|
| 118 |
+
y = fft(x)
|
| 119 |
+
assert_equal(y.dtype, self.cdt)
|
| 120 |
+
y1 = direct_dft(x)
|
| 121 |
+
assert_array_almost_equal(y,y1)
|
| 122 |
+
x = np.array([1,2,3,4+0j,5], dtype=self.cdt)
|
| 123 |
+
assert_array_almost_equal(fft(x),direct_dft(x))
|
| 124 |
+
|
| 125 |
+
def test_n_argument_real(self):
|
| 126 |
+
x1 = np.array([1,2,3,4], dtype=self.rdt)
|
| 127 |
+
x2 = np.array([1,2,3,4], dtype=self.rdt)
|
| 128 |
+
y = fft([x1,x2],n=4)
|
| 129 |
+
assert_equal(y.dtype, self.cdt)
|
| 130 |
+
assert_equal(y.shape,(2,4))
|
| 131 |
+
assert_array_almost_equal(y[0],direct_dft(x1))
|
| 132 |
+
assert_array_almost_equal(y[1],direct_dft(x2))
|
| 133 |
+
|
| 134 |
+
def _test_n_argument_complex(self):
|
| 135 |
+
x1 = np.array([1,2,3,4+1j], dtype=self.cdt)
|
| 136 |
+
x2 = np.array([1,2,3,4+1j], dtype=self.cdt)
|
| 137 |
+
y = fft([x1,x2],n=4)
|
| 138 |
+
assert_equal(y.dtype, self.cdt)
|
| 139 |
+
assert_equal(y.shape,(2,4))
|
| 140 |
+
assert_array_almost_equal(y[0],direct_dft(x1))
|
| 141 |
+
assert_array_almost_equal(y[1],direct_dft(x2))
|
| 142 |
+
|
| 143 |
+
def test_djbfft(self):
|
| 144 |
+
for i in range(2,14):
|
| 145 |
+
n = 2**i
|
| 146 |
+
x = np.arange(n)
|
| 147 |
+
y = fft(x.astype(complex))
|
| 148 |
+
y2 = numpy.fft.fft(x)
|
| 149 |
+
assert_array_almost_equal(y,y2)
|
| 150 |
+
y = fft(x)
|
| 151 |
+
assert_array_almost_equal(y,y2)
|
| 152 |
+
|
| 153 |
+
def test_invalid_sizes(self):
|
| 154 |
+
assert_raises(ValueError, fft, [])
|
| 155 |
+
assert_raises(ValueError, fft, [[1,1],[2,2]], -5)
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
class TestLongDoubleFFT(_TestFFTBase):
|
| 159 |
+
def setup_method(self):
|
| 160 |
+
self.cdt = np.clongdouble
|
| 161 |
+
self.rdt = np.longdouble
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
class TestDoubleFFT(_TestFFTBase):
|
| 165 |
+
def setup_method(self):
|
| 166 |
+
self.cdt = np.cdouble
|
| 167 |
+
self.rdt = np.float64
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
class TestSingleFFT(_TestFFTBase):
|
| 171 |
+
def setup_method(self):
|
| 172 |
+
self.cdt = np.complex64
|
| 173 |
+
self.rdt = np.float32
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
class TestFloat16FFT:
|
| 177 |
+
|
| 178 |
+
def test_1_argument_real(self):
|
| 179 |
+
x1 = np.array([1, 2, 3, 4], dtype=np.float16)
|
| 180 |
+
y = fft(x1, n=4)
|
| 181 |
+
assert_equal(y.dtype, np.complex64)
|
| 182 |
+
assert_equal(y.shape, (4, ))
|
| 183 |
+
assert_array_almost_equal(y, direct_dft(x1.astype(np.float32)))
|
| 184 |
+
|
| 185 |
+
def test_n_argument_real(self):
|
| 186 |
+
x1 = np.array([1, 2, 3, 4], dtype=np.float16)
|
| 187 |
+
x2 = np.array([1, 2, 3, 4], dtype=np.float16)
|
| 188 |
+
y = fft([x1, x2], n=4)
|
| 189 |
+
assert_equal(y.dtype, np.complex64)
|
| 190 |
+
assert_equal(y.shape, (2, 4))
|
| 191 |
+
assert_array_almost_equal(y[0], direct_dft(x1.astype(np.float32)))
|
| 192 |
+
assert_array_almost_equal(y[1], direct_dft(x2.astype(np.float32)))
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
class _TestIFFTBase:
|
| 196 |
+
def setup_method(self):
|
| 197 |
+
np.random.seed(1234)
|
| 198 |
+
|
| 199 |
+
def test_definition(self):
|
| 200 |
+
x = np.array([1,2,3,4+1j,1,2,3,4+2j], self.cdt)
|
| 201 |
+
y = ifft(x)
|
| 202 |
+
y1 = direct_idft(x)
|
| 203 |
+
assert_equal(y.dtype, self.cdt)
|
| 204 |
+
assert_array_almost_equal(y,y1)
|
| 205 |
+
|
| 206 |
+
x = np.array([1,2,3,4+0j,5], self.cdt)
|
| 207 |
+
assert_array_almost_equal(ifft(x),direct_idft(x))
|
| 208 |
+
|
| 209 |
+
def test_definition_real(self):
|
| 210 |
+
x = np.array([1,2,3,4,1,2,3,4], self.rdt)
|
| 211 |
+
y = ifft(x)
|
| 212 |
+
assert_equal(y.dtype, self.cdt)
|
| 213 |
+
y1 = direct_idft(x)
|
| 214 |
+
assert_array_almost_equal(y,y1)
|
| 215 |
+
|
| 216 |
+
x = np.array([1,2,3,4,5], dtype=self.rdt)
|
| 217 |
+
assert_equal(y.dtype, self.cdt)
|
| 218 |
+
assert_array_almost_equal(ifft(x),direct_idft(x))
|
| 219 |
+
|
| 220 |
+
def test_djbfft(self):
|
| 221 |
+
for i in range(2,14):
|
| 222 |
+
n = 2**i
|
| 223 |
+
x = np.arange(n)
|
| 224 |
+
y = ifft(x.astype(self.cdt))
|
| 225 |
+
y2 = numpy.fft.ifft(x.astype(self.cdt))
|
| 226 |
+
assert_allclose(y,y2, rtol=self.rtol, atol=self.atol)
|
| 227 |
+
y = ifft(x)
|
| 228 |
+
assert_allclose(y,y2, rtol=self.rtol, atol=self.atol)
|
| 229 |
+
|
| 230 |
+
def test_random_complex(self):
|
| 231 |
+
for size in [1,51,111,100,200,64,128,256,1024]:
|
| 232 |
+
x = random([size]).astype(self.cdt)
|
| 233 |
+
x = random([size]).astype(self.cdt) + 1j*x
|
| 234 |
+
y1 = ifft(fft(x))
|
| 235 |
+
y2 = fft(ifft(x))
|
| 236 |
+
assert_equal(y1.dtype, self.cdt)
|
| 237 |
+
assert_equal(y2.dtype, self.cdt)
|
| 238 |
+
assert_array_almost_equal(y1, x)
|
| 239 |
+
assert_array_almost_equal(y2, x)
|
| 240 |
+
|
| 241 |
+
def test_random_real(self):
|
| 242 |
+
for size in [1,51,111,100,200,64,128,256,1024]:
|
| 243 |
+
x = random([size]).astype(self.rdt)
|
| 244 |
+
y1 = ifft(fft(x))
|
| 245 |
+
y2 = fft(ifft(x))
|
| 246 |
+
assert_equal(y1.dtype, self.cdt)
|
| 247 |
+
assert_equal(y2.dtype, self.cdt)
|
| 248 |
+
assert_array_almost_equal(y1, x)
|
| 249 |
+
assert_array_almost_equal(y2, x)
|
| 250 |
+
|
| 251 |
+
def test_size_accuracy(self):
|
| 252 |
+
# Sanity check for the accuracy for prime and non-prime sized inputs
|
| 253 |
+
for size in LARGE_COMPOSITE_SIZES + LARGE_PRIME_SIZES:
|
| 254 |
+
np.random.seed(1234)
|
| 255 |
+
x = np.random.rand(size).astype(self.rdt)
|
| 256 |
+
y = ifft(fft(x))
|
| 257 |
+
_assert_close_in_norm(x, y, self.rtol, size, self.rdt)
|
| 258 |
+
y = fft(ifft(x))
|
| 259 |
+
_assert_close_in_norm(x, y, self.rtol, size, self.rdt)
|
| 260 |
+
|
| 261 |
+
x = (x + 1j*np.random.rand(size)).astype(self.cdt)
|
| 262 |
+
y = ifft(fft(x))
|
| 263 |
+
_assert_close_in_norm(x, y, self.rtol, size, self.rdt)
|
| 264 |
+
y = fft(ifft(x))
|
| 265 |
+
_assert_close_in_norm(x, y, self.rtol, size, self.rdt)
|
| 266 |
+
|
| 267 |
+
def test_invalid_sizes(self):
|
| 268 |
+
assert_raises(ValueError, ifft, [])
|
| 269 |
+
assert_raises(ValueError, ifft, [[1,1],[2,2]], -5)
|
| 270 |
+
|
| 271 |
+
|
| 272 |
+
@pytest.mark.skipif(np.longdouble is np.float64,
|
| 273 |
+
reason="Long double is aliased to double")
|
| 274 |
+
class TestLongDoubleIFFT(_TestIFFTBase):
|
| 275 |
+
def setup_method(self):
|
| 276 |
+
self.cdt = np.clongdouble
|
| 277 |
+
self.rdt = np.longdouble
|
| 278 |
+
self.rtol = 1e-10
|
| 279 |
+
self.atol = 1e-10
|
| 280 |
+
|
| 281 |
+
|
| 282 |
+
class TestDoubleIFFT(_TestIFFTBase):
|
| 283 |
+
def setup_method(self):
|
| 284 |
+
self.cdt = np.complex128
|
| 285 |
+
self.rdt = np.float64
|
| 286 |
+
self.rtol = 1e-10
|
| 287 |
+
self.atol = 1e-10
|
| 288 |
+
|
| 289 |
+
|
| 290 |
+
class TestSingleIFFT(_TestIFFTBase):
|
| 291 |
+
def setup_method(self):
|
| 292 |
+
self.cdt = np.complex64
|
| 293 |
+
self.rdt = np.float32
|
| 294 |
+
self.rtol = 1e-5
|
| 295 |
+
self.atol = 1e-4
|
| 296 |
+
|
| 297 |
+
|
| 298 |
+
class _TestRFFTBase:
|
| 299 |
+
def setup_method(self):
|
| 300 |
+
np.random.seed(1234)
|
| 301 |
+
|
| 302 |
+
def test_definition(self):
|
| 303 |
+
for t in [[1, 2, 3, 4, 1, 2, 3, 4], [1, 2, 3, 4, 1, 2, 3, 4, 5]]:
|
| 304 |
+
x = np.array(t, dtype=self.rdt)
|
| 305 |
+
y = rfft(x)
|
| 306 |
+
y1 = direct_rdft(x)
|
| 307 |
+
assert_array_almost_equal(y,y1)
|
| 308 |
+
assert_equal(y.dtype, self.cdt)
|
| 309 |
+
|
| 310 |
+
def test_djbfft(self):
|
| 311 |
+
for i in range(2,14):
|
| 312 |
+
n = 2**i
|
| 313 |
+
x = np.arange(n)
|
| 314 |
+
y1 = np.fft.rfft(x)
|
| 315 |
+
y = rfft(x)
|
| 316 |
+
assert_array_almost_equal(y,y1)
|
| 317 |
+
|
| 318 |
+
def test_invalid_sizes(self):
|
| 319 |
+
assert_raises(ValueError, rfft, [])
|
| 320 |
+
assert_raises(ValueError, rfft, [[1,1],[2,2]], -5)
|
| 321 |
+
|
| 322 |
+
def test_complex_input(self):
|
| 323 |
+
x = np.zeros(10, dtype=self.cdt)
|
| 324 |
+
with assert_raises(TypeError, match="x must be a real sequence"):
|
| 325 |
+
rfft(x)
|
| 326 |
+
|
| 327 |
+
# See gh-5790
|
| 328 |
+
class MockSeries:
|
| 329 |
+
def __init__(self, data):
|
| 330 |
+
self.data = np.asarray(data)
|
| 331 |
+
|
| 332 |
+
def __getattr__(self, item):
|
| 333 |
+
try:
|
| 334 |
+
return getattr(self.data, item)
|
| 335 |
+
except AttributeError as e:
|
| 336 |
+
raise AttributeError("'MockSeries' object "
|
| 337 |
+
f"has no attribute '{item}'") from e
|
| 338 |
+
|
| 339 |
+
def test_non_ndarray_with_dtype(self):
|
| 340 |
+
x = np.array([1., 2., 3., 4., 5.])
|
| 341 |
+
xs = _TestRFFTBase.MockSeries(x)
|
| 342 |
+
|
| 343 |
+
expected = [1, 2, 3, 4, 5]
|
| 344 |
+
rfft(xs)
|
| 345 |
+
|
| 346 |
+
# Data should not have been overwritten
|
| 347 |
+
assert_equal(x, expected)
|
| 348 |
+
assert_equal(xs.data, expected)
|
| 349 |
+
|
| 350 |
+
@pytest.mark.skipif(np.longdouble is np.float64,
|
| 351 |
+
reason="Long double is aliased to double")
|
| 352 |
+
class TestRFFTLongDouble(_TestRFFTBase):
|
| 353 |
+
def setup_method(self):
|
| 354 |
+
self.cdt = np.clongdouble
|
| 355 |
+
self.rdt = np.longdouble
|
| 356 |
+
|
| 357 |
+
|
| 358 |
+
class TestRFFTDouble(_TestRFFTBase):
|
| 359 |
+
def setup_method(self):
|
| 360 |
+
self.cdt = np.complex128
|
| 361 |
+
self.rdt = np.float64
|
| 362 |
+
|
| 363 |
+
|
| 364 |
+
class TestRFFTSingle(_TestRFFTBase):
|
| 365 |
+
def setup_method(self):
|
| 366 |
+
self.cdt = np.complex64
|
| 367 |
+
self.rdt = np.float32
|
| 368 |
+
|
| 369 |
+
|
| 370 |
+
class _TestIRFFTBase:
|
| 371 |
+
def setup_method(self):
|
| 372 |
+
np.random.seed(1234)
|
| 373 |
+
|
| 374 |
+
def test_definition(self):
|
| 375 |
+
x1 = [1,2+3j,4+1j,1+2j,3+4j]
|
| 376 |
+
x1_1 = [1,2+3j,4+1j,2+3j,4,2-3j,4-1j,2-3j]
|
| 377 |
+
x1 = x1_1[:5]
|
| 378 |
+
x2_1 = [1,2+3j,4+1j,2+3j,4+5j,4-5j,2-3j,4-1j,2-3j]
|
| 379 |
+
x2 = x2_1[:5]
|
| 380 |
+
|
| 381 |
+
def _test(x, xr):
|
| 382 |
+
y = irfft(np.array(x, dtype=self.cdt), n=len(xr))
|
| 383 |
+
y1 = direct_irdft(x, len(xr))
|
| 384 |
+
assert_equal(y.dtype, self.rdt)
|
| 385 |
+
assert_array_almost_equal(y,y1, decimal=self.ndec)
|
| 386 |
+
assert_array_almost_equal(y,ifft(xr), decimal=self.ndec)
|
| 387 |
+
|
| 388 |
+
_test(x1, x1_1)
|
| 389 |
+
_test(x2, x2_1)
|
| 390 |
+
|
| 391 |
+
def test_djbfft(self):
|
| 392 |
+
for i in range(2,14):
|
| 393 |
+
n = 2**i
|
| 394 |
+
x = np.arange(-1, n, 2) + 1j * np.arange(0, n+1, 2)
|
| 395 |
+
x[0] = 0
|
| 396 |
+
if n % 2 == 0:
|
| 397 |
+
x[-1] = np.real(x[-1])
|
| 398 |
+
y1 = np.fft.irfft(x)
|
| 399 |
+
y = irfft(x)
|
| 400 |
+
assert_array_almost_equal(y,y1)
|
| 401 |
+
|
| 402 |
+
def test_random_real(self):
|
| 403 |
+
for size in [1,51,111,100,200,64,128,256,1024]:
|
| 404 |
+
x = random([size]).astype(self.rdt)
|
| 405 |
+
y1 = irfft(rfft(x), n=size)
|
| 406 |
+
y2 = rfft(irfft(x, n=(size*2-1)))
|
| 407 |
+
assert_equal(y1.dtype, self.rdt)
|
| 408 |
+
assert_equal(y2.dtype, self.cdt)
|
| 409 |
+
assert_array_almost_equal(y1, x, decimal=self.ndec, err_msg=f"size={size}")
|
| 410 |
+
assert_array_almost_equal(y2, x, decimal=self.ndec, err_msg=f"size={size}")
|
| 411 |
+
|
| 412 |
+
def test_size_accuracy(self):
|
| 413 |
+
# Sanity check for the accuracy for prime and non-prime sized inputs
|
| 414 |
+
if self.rdt == np.float32:
|
| 415 |
+
rtol = 1e-5
|
| 416 |
+
elif self.rdt == np.float64:
|
| 417 |
+
rtol = 1e-10
|
| 418 |
+
|
| 419 |
+
for size in LARGE_COMPOSITE_SIZES + LARGE_PRIME_SIZES:
|
| 420 |
+
np.random.seed(1234)
|
| 421 |
+
x = np.random.rand(size).astype(self.rdt)
|
| 422 |
+
y = irfft(rfft(x), len(x))
|
| 423 |
+
_assert_close_in_norm(x, y, rtol, size, self.rdt)
|
| 424 |
+
y = rfft(irfft(x, 2 * len(x) - 1))
|
| 425 |
+
_assert_close_in_norm(x, y, rtol, size, self.rdt)
|
| 426 |
+
|
| 427 |
+
def test_invalid_sizes(self):
|
| 428 |
+
assert_raises(ValueError, irfft, [])
|
| 429 |
+
assert_raises(ValueError, irfft, [[1,1],[2,2]], -5)
|
| 430 |
+
|
| 431 |
+
|
| 432 |
+
# self.ndec is bogus; we should have a assert_array_approx_equal for number of
|
| 433 |
+
# significant digits
|
| 434 |
+
|
| 435 |
+
@pytest.mark.skipif(np.longdouble is np.float64,
|
| 436 |
+
reason="Long double is aliased to double")
|
| 437 |
+
class TestIRFFTLongDouble(_TestIRFFTBase):
|
| 438 |
+
def setup_method(self):
|
| 439 |
+
self.cdt = np.complex128
|
| 440 |
+
self.rdt = np.float64
|
| 441 |
+
self.ndec = 14
|
| 442 |
+
|
| 443 |
+
|
| 444 |
+
class TestIRFFTDouble(_TestIRFFTBase):
|
| 445 |
+
def setup_method(self):
|
| 446 |
+
self.cdt = np.complex128
|
| 447 |
+
self.rdt = np.float64
|
| 448 |
+
self.ndec = 14
|
| 449 |
+
|
| 450 |
+
|
| 451 |
+
class TestIRFFTSingle(_TestIRFFTBase):
|
| 452 |
+
def setup_method(self):
|
| 453 |
+
self.cdt = np.complex64
|
| 454 |
+
self.rdt = np.float32
|
| 455 |
+
self.ndec = 5
|
| 456 |
+
|
| 457 |
+
|
| 458 |
+
class TestFftnSingle:
|
| 459 |
+
def setup_method(self):
|
| 460 |
+
np.random.seed(1234)
|
| 461 |
+
|
| 462 |
+
def test_definition(self):
|
| 463 |
+
x = [[1, 2, 3],
|
| 464 |
+
[4, 5, 6],
|
| 465 |
+
[7, 8, 9]]
|
| 466 |
+
y = fftn(np.array(x, np.float32))
|
| 467 |
+
assert_(y.dtype == np.complex64,
|
| 468 |
+
msg="double precision output with single precision")
|
| 469 |
+
|
| 470 |
+
y_r = np.array(fftn(x), np.complex64)
|
| 471 |
+
assert_array_almost_equal_nulp(y, y_r)
|
| 472 |
+
|
| 473 |
+
@pytest.mark.parametrize('size', SMALL_COMPOSITE_SIZES + SMALL_PRIME_SIZES)
|
| 474 |
+
def test_size_accuracy_small(self, size):
|
| 475 |
+
rng = np.random.default_rng(1234)
|
| 476 |
+
x = rng.random((size, size)) + 1j * rng.random((size, size))
|
| 477 |
+
y1 = fftn(x.real.astype(np.float32))
|
| 478 |
+
y2 = fftn(x.real.astype(np.float64)).astype(np.complex64)
|
| 479 |
+
|
| 480 |
+
assert_equal(y1.dtype, np.complex64)
|
| 481 |
+
assert_array_almost_equal_nulp(y1, y2, 2000)
|
| 482 |
+
|
| 483 |
+
@pytest.mark.parametrize('size', LARGE_COMPOSITE_SIZES + LARGE_PRIME_SIZES)
|
| 484 |
+
def test_size_accuracy_large(self, size):
|
| 485 |
+
rng = np.random.default_rng(1234)
|
| 486 |
+
x = rng.random((size, 3)) + 1j * rng.random((size, 3))
|
| 487 |
+
y1 = fftn(x.real.astype(np.float32))
|
| 488 |
+
y2 = fftn(x.real.astype(np.float64)).astype(np.complex64)
|
| 489 |
+
|
| 490 |
+
assert_equal(y1.dtype, np.complex64)
|
| 491 |
+
assert_array_almost_equal_nulp(y1, y2, 2000)
|
| 492 |
+
|
| 493 |
+
def test_definition_float16(self):
|
| 494 |
+
x = [[1, 2, 3],
|
| 495 |
+
[4, 5, 6],
|
| 496 |
+
[7, 8, 9]]
|
| 497 |
+
y = fftn(np.array(x, np.float16))
|
| 498 |
+
assert_equal(y.dtype, np.complex64)
|
| 499 |
+
y_r = np.array(fftn(x), np.complex64)
|
| 500 |
+
assert_array_almost_equal_nulp(y, y_r)
|
| 501 |
+
|
| 502 |
+
@pytest.mark.parametrize('size', SMALL_COMPOSITE_SIZES + SMALL_PRIME_SIZES)
|
| 503 |
+
def test_float16_input_small(self, size):
|
| 504 |
+
rng = np.random.default_rng(1234)
|
| 505 |
+
x = rng.random((size, size)) + 1j*rng.random((size, size))
|
| 506 |
+
y1 = fftn(x.real.astype(np.float16))
|
| 507 |
+
y2 = fftn(x.real.astype(np.float64)).astype(np.complex64)
|
| 508 |
+
|
| 509 |
+
assert_equal(y1.dtype, np.complex64)
|
| 510 |
+
assert_array_almost_equal_nulp(y1, y2, 5e5)
|
| 511 |
+
|
| 512 |
+
@pytest.mark.parametrize('size', LARGE_COMPOSITE_SIZES + LARGE_PRIME_SIZES)
|
| 513 |
+
def test_float16_input_large(self, size):
|
| 514 |
+
rng = np.random.default_rng(1234)
|
| 515 |
+
x = rng.random((size, 3)) + 1j*rng.random((size, 3))
|
| 516 |
+
y1 = fftn(x.real.astype(np.float16))
|
| 517 |
+
y2 = fftn(x.real.astype(np.float64)).astype(np.complex64)
|
| 518 |
+
|
| 519 |
+
assert_equal(y1.dtype, np.complex64)
|
| 520 |
+
assert_array_almost_equal_nulp(y1, y2, 2e6)
|
| 521 |
+
|
| 522 |
+
|
| 523 |
+
class TestFftn:
|
| 524 |
+
def setup_method(self):
|
| 525 |
+
np.random.seed(1234)
|
| 526 |
+
|
| 527 |
+
def test_definition(self):
|
| 528 |
+
x = [[1, 2, 3],
|
| 529 |
+
[4, 5, 6],
|
| 530 |
+
[7, 8, 9]]
|
| 531 |
+
y = fftn(x)
|
| 532 |
+
assert_array_almost_equal(y, direct_dftn(x))
|
| 533 |
+
|
| 534 |
+
x = random((20, 26))
|
| 535 |
+
assert_array_almost_equal(fftn(x), direct_dftn(x))
|
| 536 |
+
|
| 537 |
+
x = random((5, 4, 3, 20))
|
| 538 |
+
assert_array_almost_equal(fftn(x), direct_dftn(x))
|
| 539 |
+
|
| 540 |
+
def test_axes_argument(self):
|
| 541 |
+
# plane == ji_plane, x== kji_space
|
| 542 |
+
plane1 = [[1, 2, 3],
|
| 543 |
+
[4, 5, 6],
|
| 544 |
+
[7, 8, 9]]
|
| 545 |
+
plane2 = [[10, 11, 12],
|
| 546 |
+
[13, 14, 15],
|
| 547 |
+
[16, 17, 18]]
|
| 548 |
+
plane3 = [[19, 20, 21],
|
| 549 |
+
[22, 23, 24],
|
| 550 |
+
[25, 26, 27]]
|
| 551 |
+
ki_plane1 = [[1, 2, 3],
|
| 552 |
+
[10, 11, 12],
|
| 553 |
+
[19, 20, 21]]
|
| 554 |
+
ki_plane2 = [[4, 5, 6],
|
| 555 |
+
[13, 14, 15],
|
| 556 |
+
[22, 23, 24]]
|
| 557 |
+
ki_plane3 = [[7, 8, 9],
|
| 558 |
+
[16, 17, 18],
|
| 559 |
+
[25, 26, 27]]
|
| 560 |
+
jk_plane1 = [[1, 10, 19],
|
| 561 |
+
[4, 13, 22],
|
| 562 |
+
[7, 16, 25]]
|
| 563 |
+
jk_plane2 = [[2, 11, 20],
|
| 564 |
+
[5, 14, 23],
|
| 565 |
+
[8, 17, 26]]
|
| 566 |
+
jk_plane3 = [[3, 12, 21],
|
| 567 |
+
[6, 15, 24],
|
| 568 |
+
[9, 18, 27]]
|
| 569 |
+
kj_plane1 = [[1, 4, 7],
|
| 570 |
+
[10, 13, 16], [19, 22, 25]]
|
| 571 |
+
kj_plane2 = [[2, 5, 8],
|
| 572 |
+
[11, 14, 17], [20, 23, 26]]
|
| 573 |
+
kj_plane3 = [[3, 6, 9],
|
| 574 |
+
[12, 15, 18], [21, 24, 27]]
|
| 575 |
+
ij_plane1 = [[1, 4, 7],
|
| 576 |
+
[2, 5, 8],
|
| 577 |
+
[3, 6, 9]]
|
| 578 |
+
ij_plane2 = [[10, 13, 16],
|
| 579 |
+
[11, 14, 17],
|
| 580 |
+
[12, 15, 18]]
|
| 581 |
+
ij_plane3 = [[19, 22, 25],
|
| 582 |
+
[20, 23, 26],
|
| 583 |
+
[21, 24, 27]]
|
| 584 |
+
ik_plane1 = [[1, 10, 19],
|
| 585 |
+
[2, 11, 20],
|
| 586 |
+
[3, 12, 21]]
|
| 587 |
+
ik_plane2 = [[4, 13, 22],
|
| 588 |
+
[5, 14, 23],
|
| 589 |
+
[6, 15, 24]]
|
| 590 |
+
ik_plane3 = [[7, 16, 25],
|
| 591 |
+
[8, 17, 26],
|
| 592 |
+
[9, 18, 27]]
|
| 593 |
+
ijk_space = [jk_plane1, jk_plane2, jk_plane3]
|
| 594 |
+
ikj_space = [kj_plane1, kj_plane2, kj_plane3]
|
| 595 |
+
jik_space = [ik_plane1, ik_plane2, ik_plane3]
|
| 596 |
+
jki_space = [ki_plane1, ki_plane2, ki_plane3]
|
| 597 |
+
kij_space = [ij_plane1, ij_plane2, ij_plane3]
|
| 598 |
+
x = array([plane1, plane2, plane3])
|
| 599 |
+
|
| 600 |
+
assert_array_almost_equal(fftn(x),
|
| 601 |
+
fftn(x, axes=(-3, -2, -1))) # kji_space
|
| 602 |
+
assert_array_almost_equal(fftn(x), fftn(x, axes=(0, 1, 2)))
|
| 603 |
+
assert_array_almost_equal(fftn(x, axes=(0, 2)), fftn(x, axes=(0, -1)))
|
| 604 |
+
y = fftn(x, axes=(2, 1, 0)) # ijk_space
|
| 605 |
+
assert_array_almost_equal(swapaxes(y, -1, -3), fftn(ijk_space))
|
| 606 |
+
y = fftn(x, axes=(2, 0, 1)) # ikj_space
|
| 607 |
+
assert_array_almost_equal(swapaxes(swapaxes(y, -1, -3), -1, -2),
|
| 608 |
+
fftn(ikj_space))
|
| 609 |
+
y = fftn(x, axes=(1, 2, 0)) # jik_space
|
| 610 |
+
assert_array_almost_equal(swapaxes(swapaxes(y, -1, -3), -3, -2),
|
| 611 |
+
fftn(jik_space))
|
| 612 |
+
y = fftn(x, axes=(1, 0, 2)) # jki_space
|
| 613 |
+
assert_array_almost_equal(swapaxes(y, -2, -3), fftn(jki_space))
|
| 614 |
+
y = fftn(x, axes=(0, 2, 1)) # kij_space
|
| 615 |
+
assert_array_almost_equal(swapaxes(y, -2, -1), fftn(kij_space))
|
| 616 |
+
|
| 617 |
+
y = fftn(x, axes=(-2, -1)) # ji_plane
|
| 618 |
+
assert_array_almost_equal(fftn(plane1), y[0])
|
| 619 |
+
assert_array_almost_equal(fftn(plane2), y[1])
|
| 620 |
+
assert_array_almost_equal(fftn(plane3), y[2])
|
| 621 |
+
|
| 622 |
+
y = fftn(x, axes=(1, 2)) # ji_plane
|
| 623 |
+
assert_array_almost_equal(fftn(plane1), y[0])
|
| 624 |
+
assert_array_almost_equal(fftn(plane2), y[1])
|
| 625 |
+
assert_array_almost_equal(fftn(plane3), y[2])
|
| 626 |
+
|
| 627 |
+
y = fftn(x, axes=(-3, -2)) # kj_plane
|
| 628 |
+
assert_array_almost_equal(fftn(x[:, :, 0]), y[:, :, 0])
|
| 629 |
+
assert_array_almost_equal(fftn(x[:, :, 1]), y[:, :, 1])
|
| 630 |
+
assert_array_almost_equal(fftn(x[:, :, 2]), y[:, :, 2])
|
| 631 |
+
|
| 632 |
+
y = fftn(x, axes=(-3, -1)) # ki_plane
|
| 633 |
+
assert_array_almost_equal(fftn(x[:, 0, :]), y[:, 0, :])
|
| 634 |
+
assert_array_almost_equal(fftn(x[:, 1, :]), y[:, 1, :])
|
| 635 |
+
assert_array_almost_equal(fftn(x[:, 2, :]), y[:, 2, :])
|
| 636 |
+
|
| 637 |
+
y = fftn(x, axes=(-1, -2)) # ij_plane
|
| 638 |
+
assert_array_almost_equal(fftn(ij_plane1), swapaxes(y[0], -2, -1))
|
| 639 |
+
assert_array_almost_equal(fftn(ij_plane2), swapaxes(y[1], -2, -1))
|
| 640 |
+
assert_array_almost_equal(fftn(ij_plane3), swapaxes(y[2], -2, -1))
|
| 641 |
+
|
| 642 |
+
y = fftn(x, axes=(-1, -3)) # ik_plane
|
| 643 |
+
assert_array_almost_equal(fftn(ik_plane1),
|
| 644 |
+
swapaxes(y[:, 0, :], -1, -2))
|
| 645 |
+
assert_array_almost_equal(fftn(ik_plane2),
|
| 646 |
+
swapaxes(y[:, 1, :], -1, -2))
|
| 647 |
+
assert_array_almost_equal(fftn(ik_plane3),
|
| 648 |
+
swapaxes(y[:, 2, :], -1, -2))
|
| 649 |
+
|
| 650 |
+
y = fftn(x, axes=(-2, -3)) # jk_plane
|
| 651 |
+
assert_array_almost_equal(fftn(jk_plane1),
|
| 652 |
+
swapaxes(y[:, :, 0], -1, -2))
|
| 653 |
+
assert_array_almost_equal(fftn(jk_plane2),
|
| 654 |
+
swapaxes(y[:, :, 1], -1, -2))
|
| 655 |
+
assert_array_almost_equal(fftn(jk_plane3),
|
| 656 |
+
swapaxes(y[:, :, 2], -1, -2))
|
| 657 |
+
|
| 658 |
+
y = fftn(x, axes=(-1,)) # i_line
|
| 659 |
+
for i in range(3):
|
| 660 |
+
for j in range(3):
|
| 661 |
+
assert_array_almost_equal(fft(x[i, j, :]), y[i, j, :])
|
| 662 |
+
y = fftn(x, axes=(-2,)) # j_line
|
| 663 |
+
for i in range(3):
|
| 664 |
+
for j in range(3):
|
| 665 |
+
assert_array_almost_equal(fft(x[i, :, j]), y[i, :, j])
|
| 666 |
+
y = fftn(x, axes=(0,)) # k_line
|
| 667 |
+
for i in range(3):
|
| 668 |
+
for j in range(3):
|
| 669 |
+
assert_array_almost_equal(fft(x[:, i, j]), y[:, i, j])
|
| 670 |
+
|
| 671 |
+
y = fftn(x, axes=()) # point
|
| 672 |
+
assert_array_almost_equal(y, x)
|
| 673 |
+
|
| 674 |
+
def test_shape_argument(self):
|
| 675 |
+
small_x = [[1, 2, 3],
|
| 676 |
+
[4, 5, 6]]
|
| 677 |
+
large_x1 = [[1, 2, 3, 0],
|
| 678 |
+
[4, 5, 6, 0],
|
| 679 |
+
[0, 0, 0, 0],
|
| 680 |
+
[0, 0, 0, 0]]
|
| 681 |
+
|
| 682 |
+
y = fftn(small_x, s=(4, 4))
|
| 683 |
+
assert_array_almost_equal(y, fftn(large_x1))
|
| 684 |
+
|
| 685 |
+
y = fftn(small_x, s=(3, 4))
|
| 686 |
+
assert_array_almost_equal(y, fftn(large_x1[:-1]))
|
| 687 |
+
|
| 688 |
+
def test_shape_axes_argument(self):
|
| 689 |
+
small_x = [[1, 2, 3],
|
| 690 |
+
[4, 5, 6],
|
| 691 |
+
[7, 8, 9]]
|
| 692 |
+
large_x1 = array([[1, 2, 3, 0],
|
| 693 |
+
[4, 5, 6, 0],
|
| 694 |
+
[7, 8, 9, 0],
|
| 695 |
+
[0, 0, 0, 0]])
|
| 696 |
+
y = fftn(small_x, s=(4, 4), axes=(-2, -1))
|
| 697 |
+
assert_array_almost_equal(y, fftn(large_x1))
|
| 698 |
+
y = fftn(small_x, s=(4, 4), axes=(-1, -2))
|
| 699 |
+
|
| 700 |
+
assert_array_almost_equal(y, swapaxes(
|
| 701 |
+
fftn(swapaxes(large_x1, -1, -2)), -1, -2))
|
| 702 |
+
|
| 703 |
+
def test_shape_axes_argument2(self):
|
| 704 |
+
# Change shape of the last axis
|
| 705 |
+
x = numpy.random.random((10, 5, 3, 7))
|
| 706 |
+
y = fftn(x, axes=(-1,), s=(8,))
|
| 707 |
+
assert_array_almost_equal(y, fft(x, axis=-1, n=8))
|
| 708 |
+
|
| 709 |
+
# Change shape of an arbitrary axis which is not the last one
|
| 710 |
+
x = numpy.random.random((10, 5, 3, 7))
|
| 711 |
+
y = fftn(x, axes=(-2,), s=(8,))
|
| 712 |
+
assert_array_almost_equal(y, fft(x, axis=-2, n=8))
|
| 713 |
+
|
| 714 |
+
# Change shape of axes: cf #244, where shape and axes were mixed up
|
| 715 |
+
x = numpy.random.random((4, 4, 2))
|
| 716 |
+
y = fftn(x, axes=(-3, -2), s=(8, 8))
|
| 717 |
+
assert_array_almost_equal(y,
|
| 718 |
+
numpy.fft.fftn(x, axes=(-3, -2), s=(8, 8)))
|
| 719 |
+
|
| 720 |
+
def test_shape_argument_more(self):
|
| 721 |
+
x = zeros((4, 4, 2))
|
| 722 |
+
with assert_raises(ValueError,
|
| 723 |
+
match="shape requires more axes than are present"):
|
| 724 |
+
fftn(x, s=(8, 8, 2, 1))
|
| 725 |
+
|
| 726 |
+
def test_invalid_sizes(self):
|
| 727 |
+
with assert_raises(ValueError,
|
| 728 |
+
match="invalid number of data points"
|
| 729 |
+
r" \(\[1, 0\]\) specified"):
|
| 730 |
+
fftn([[]])
|
| 731 |
+
|
| 732 |
+
with assert_raises(ValueError,
|
| 733 |
+
match="invalid number of data points"
|
| 734 |
+
r" \(\[4, -3\]\) specified"):
|
| 735 |
+
fftn([[1, 1], [2, 2]], (4, -3))
|
| 736 |
+
|
| 737 |
+
def test_no_axes(self):
|
| 738 |
+
x = numpy.random.random((2,2,2))
|
| 739 |
+
assert_allclose(fftn(x, axes=[]), x, atol=1e-7)
|
| 740 |
+
|
| 741 |
+
def test_regression_244(self):
|
| 742 |
+
"""FFT returns wrong result with axes parameter."""
|
| 743 |
+
# fftn (and hence fft2) used to break when both axes and shape were used
|
| 744 |
+
x = numpy.ones((4, 4, 2))
|
| 745 |
+
y = fftn(x, s=(8, 8), axes=(-3, -2))
|
| 746 |
+
y_r = numpy.fft.fftn(x, s=(8, 8), axes=(-3, -2))
|
| 747 |
+
assert_allclose(y, y_r)
|
| 748 |
+
|
| 749 |
+
|
| 750 |
+
class TestIfftn:
|
| 751 |
+
dtype = None
|
| 752 |
+
cdtype = None
|
| 753 |
+
|
| 754 |
+
def setup_method(self):
|
| 755 |
+
np.random.seed(1234)
|
| 756 |
+
|
| 757 |
+
@pytest.mark.parametrize('dtype,cdtype,maxnlp',
|
| 758 |
+
[(np.float64, np.complex128, 2000),
|
| 759 |
+
(np.float32, np.complex64, 3500)])
|
| 760 |
+
def test_definition(self, dtype, cdtype, maxnlp):
|
| 761 |
+
rng = np.random.default_rng(1234)
|
| 762 |
+
x = np.array([[1, 2, 3],
|
| 763 |
+
[4, 5, 6],
|
| 764 |
+
[7, 8, 9]], dtype=dtype)
|
| 765 |
+
y = ifftn(x)
|
| 766 |
+
assert_equal(y.dtype, cdtype)
|
| 767 |
+
assert_array_almost_equal_nulp(y, direct_idftn(x), maxnlp)
|
| 768 |
+
|
| 769 |
+
x = rng.random((20, 26))
|
| 770 |
+
assert_array_almost_equal_nulp(ifftn(x), direct_idftn(x), maxnlp)
|
| 771 |
+
|
| 772 |
+
x = rng.random((5, 4, 3, 20))
|
| 773 |
+
assert_array_almost_equal_nulp(ifftn(x), direct_idftn(x), maxnlp)
|
| 774 |
+
|
| 775 |
+
@pytest.mark.parametrize('maxnlp', [2000, 3500])
|
| 776 |
+
@pytest.mark.parametrize('size', [1, 2, 51, 32, 64, 92])
|
| 777 |
+
def test_random_complex(self, maxnlp, size):
|
| 778 |
+
rng = np.random.default_rng(1234)
|
| 779 |
+
x = rng.random([size, size]) + 1j * rng.random([size, size])
|
| 780 |
+
assert_array_almost_equal_nulp(ifftn(fftn(x)), x, maxnlp)
|
| 781 |
+
assert_array_almost_equal_nulp(fftn(ifftn(x)), x, maxnlp)
|
| 782 |
+
|
| 783 |
+
def test_invalid_sizes(self):
|
| 784 |
+
with assert_raises(ValueError,
|
| 785 |
+
match="invalid number of data points"
|
| 786 |
+
r" \(\[1, 0\]\) specified"):
|
| 787 |
+
ifftn([[]])
|
| 788 |
+
|
| 789 |
+
with assert_raises(ValueError,
|
| 790 |
+
match="invalid number of data points"
|
| 791 |
+
r" \(\[4, -3\]\) specified"):
|
| 792 |
+
ifftn([[1, 1], [2, 2]], (4, -3))
|
| 793 |
+
|
| 794 |
+
def test_no_axes(self):
|
| 795 |
+
x = numpy.random.random((2,2,2))
|
| 796 |
+
assert_allclose(ifftn(x, axes=[]), x, atol=1e-7)
|
| 797 |
+
|
| 798 |
+
class TestRfftn:
|
| 799 |
+
dtype = None
|
| 800 |
+
cdtype = None
|
| 801 |
+
|
| 802 |
+
def setup_method(self):
|
| 803 |
+
np.random.seed(1234)
|
| 804 |
+
|
| 805 |
+
@pytest.mark.parametrize('dtype,cdtype,maxnlp',
|
| 806 |
+
[(np.float64, np.complex128, 2000),
|
| 807 |
+
(np.float32, np.complex64, 3500)])
|
| 808 |
+
def test_definition(self, dtype, cdtype, maxnlp):
|
| 809 |
+
rng = np.random.default_rng(1234)
|
| 810 |
+
x = np.array([[1, 2, 3],
|
| 811 |
+
[4, 5, 6],
|
| 812 |
+
[7, 8, 9]], dtype=dtype)
|
| 813 |
+
y = rfftn(x)
|
| 814 |
+
assert_equal(y.dtype, cdtype)
|
| 815 |
+
assert_array_almost_equal_nulp(y, direct_rdftn(x), maxnlp)
|
| 816 |
+
|
| 817 |
+
x = rng.random((20, 26))
|
| 818 |
+
assert_array_almost_equal_nulp(rfftn(x), direct_rdftn(x), maxnlp)
|
| 819 |
+
|
| 820 |
+
x = rng.random((5, 4, 3, 20))
|
| 821 |
+
assert_array_almost_equal_nulp(rfftn(x), direct_rdftn(x), maxnlp)
|
| 822 |
+
|
| 823 |
+
@pytest.mark.parametrize('size', [1, 2, 51, 32, 64, 92])
|
| 824 |
+
def test_random(self, size):
|
| 825 |
+
rng = np.random.default_rng(1234)
|
| 826 |
+
x = rng.random([size, size])
|
| 827 |
+
assert_allclose(irfftn(rfftn(x), x.shape), x, atol=1e-10)
|
| 828 |
+
|
| 829 |
+
@pytest.mark.parametrize('func', [rfftn, irfftn])
|
| 830 |
+
def test_invalid_sizes(self, func):
|
| 831 |
+
with assert_raises(ValueError,
|
| 832 |
+
match="invalid number of data points"
|
| 833 |
+
r" \(\[1, 0\]\) specified"):
|
| 834 |
+
func([[]])
|
| 835 |
+
|
| 836 |
+
with assert_raises(ValueError,
|
| 837 |
+
match="invalid number of data points"
|
| 838 |
+
r" \(\[4, -3\]\) specified"):
|
| 839 |
+
func([[1, 1], [2, 2]], (4, -3))
|
| 840 |
+
|
| 841 |
+
@pytest.mark.parametrize('func', [rfftn, irfftn])
|
| 842 |
+
def test_no_axes(self, func):
|
| 843 |
+
with assert_raises(ValueError,
|
| 844 |
+
match="at least 1 axis must be transformed"):
|
| 845 |
+
func([], axes=[])
|
| 846 |
+
|
| 847 |
+
def test_complex_input(self):
|
| 848 |
+
with assert_raises(TypeError, match="x must be a real sequence"):
|
| 849 |
+
rfftn(np.zeros(10, dtype=np.complex64))
|
| 850 |
+
|
| 851 |
+
|
| 852 |
+
class FakeArray:
|
| 853 |
+
def __init__(self, data):
|
| 854 |
+
self._data = data
|
| 855 |
+
self.__array_interface__ = data.__array_interface__
|
| 856 |
+
|
| 857 |
+
|
| 858 |
+
class FakeArray2:
|
| 859 |
+
def __init__(self, data):
|
| 860 |
+
self._data = data
|
| 861 |
+
|
| 862 |
+
def __array__(self, dtype=None, copy=None):
|
| 863 |
+
return self._data
|
| 864 |
+
|
| 865 |
+
# TODO: Is this test actually valuable? The behavior it's testing shouldn't be
|
| 866 |
+
# relied upon by users except for overwrite_x = False
|
| 867 |
+
class TestOverwrite:
|
| 868 |
+
"""Check input overwrite behavior of the FFT functions."""
|
| 869 |
+
|
| 870 |
+
real_dtypes = [np.float32, np.float64, np.longdouble]
|
| 871 |
+
dtypes = real_dtypes + [np.complex64, np.complex128, np.clongdouble]
|
| 872 |
+
fftsizes = [8, 16, 32]
|
| 873 |
+
|
| 874 |
+
def _check(self, x, routine, fftsize, axis, overwrite_x, should_overwrite):
|
| 875 |
+
x2 = x.copy()
|
| 876 |
+
for fake in [lambda x: x, FakeArray, FakeArray2]:
|
| 877 |
+
routine(fake(x2), fftsize, axis, overwrite_x=overwrite_x)
|
| 878 |
+
|
| 879 |
+
sig = (f"{routine.__name__}({x.dtype}{x.shape!r}, {fftsize!r}, "
|
| 880 |
+
f"axis={axis!r}, overwrite_x={overwrite_x!r})")
|
| 881 |
+
if not should_overwrite:
|
| 882 |
+
assert_equal(x2, x, err_msg=f"spurious overwrite in {sig}")
|
| 883 |
+
|
| 884 |
+
def _check_1d(self, routine, dtype, shape, axis, overwritable_dtypes,
|
| 885 |
+
fftsize, overwrite_x):
|
| 886 |
+
np.random.seed(1234)
|
| 887 |
+
if np.issubdtype(dtype, np.complexfloating):
|
| 888 |
+
data = np.random.randn(*shape) + 1j*np.random.randn(*shape)
|
| 889 |
+
else:
|
| 890 |
+
data = np.random.randn(*shape)
|
| 891 |
+
data = data.astype(dtype)
|
| 892 |
+
|
| 893 |
+
should_overwrite = (overwrite_x
|
| 894 |
+
and dtype in overwritable_dtypes
|
| 895 |
+
and fftsize <= shape[axis])
|
| 896 |
+
self._check(data, routine, fftsize, axis,
|
| 897 |
+
overwrite_x=overwrite_x,
|
| 898 |
+
should_overwrite=should_overwrite)
|
| 899 |
+
|
| 900 |
+
@pytest.mark.parametrize('dtype', dtypes)
|
| 901 |
+
@pytest.mark.parametrize('fftsize', fftsizes)
|
| 902 |
+
@pytest.mark.parametrize('overwrite_x', [True, False])
|
| 903 |
+
@pytest.mark.parametrize('shape,axes', [((16,), -1),
|
| 904 |
+
((16, 2), 0),
|
| 905 |
+
((2, 16), 1)])
|
| 906 |
+
def test_fft_ifft(self, dtype, fftsize, overwrite_x, shape, axes):
|
| 907 |
+
overwritable = (np.clongdouble, np.complex128, np.complex64)
|
| 908 |
+
self._check_1d(fft, dtype, shape, axes, overwritable,
|
| 909 |
+
fftsize, overwrite_x)
|
| 910 |
+
self._check_1d(ifft, dtype, shape, axes, overwritable,
|
| 911 |
+
fftsize, overwrite_x)
|
| 912 |
+
|
| 913 |
+
@pytest.mark.parametrize('dtype', real_dtypes)
|
| 914 |
+
@pytest.mark.parametrize('fftsize', fftsizes)
|
| 915 |
+
@pytest.mark.parametrize('overwrite_x', [True, False])
|
| 916 |
+
@pytest.mark.parametrize('shape,axes', [((16,), -1),
|
| 917 |
+
((16, 2), 0),
|
| 918 |
+
((2, 16), 1)])
|
| 919 |
+
def test_rfft_irfft(self, dtype, fftsize, overwrite_x, shape, axes):
|
| 920 |
+
overwritable = self.real_dtypes
|
| 921 |
+
self._check_1d(irfft, dtype, shape, axes, overwritable,
|
| 922 |
+
fftsize, overwrite_x)
|
| 923 |
+
self._check_1d(rfft, dtype, shape, axes, overwritable,
|
| 924 |
+
fftsize, overwrite_x)
|
| 925 |
+
|
| 926 |
+
def _check_nd_one(self, routine, dtype, shape, axes, overwritable_dtypes,
|
| 927 |
+
overwrite_x):
|
| 928 |
+
np.random.seed(1234)
|
| 929 |
+
if np.issubdtype(dtype, np.complexfloating):
|
| 930 |
+
data = np.random.randn(*shape) + 1j*np.random.randn(*shape)
|
| 931 |
+
else:
|
| 932 |
+
data = np.random.randn(*shape)
|
| 933 |
+
data = data.astype(dtype)
|
| 934 |
+
|
| 935 |
+
def fftshape_iter(shp):
|
| 936 |
+
if len(shp) <= 0:
|
| 937 |
+
yield ()
|
| 938 |
+
else:
|
| 939 |
+
for j in (shp[0]//2, shp[0], shp[0]*2):
|
| 940 |
+
for rest in fftshape_iter(shp[1:]):
|
| 941 |
+
yield (j,) + rest
|
| 942 |
+
|
| 943 |
+
def part_shape(shape, axes):
|
| 944 |
+
if axes is None:
|
| 945 |
+
return shape
|
| 946 |
+
else:
|
| 947 |
+
return tuple(np.take(shape, axes))
|
| 948 |
+
|
| 949 |
+
def should_overwrite(data, shape, axes):
|
| 950 |
+
s = part_shape(data.shape, axes)
|
| 951 |
+
return (overwrite_x and
|
| 952 |
+
np.prod(shape) <= np.prod(s)
|
| 953 |
+
and dtype in overwritable_dtypes)
|
| 954 |
+
|
| 955 |
+
for fftshape in fftshape_iter(part_shape(shape, axes)):
|
| 956 |
+
self._check(data, routine, fftshape, axes,
|
| 957 |
+
overwrite_x=overwrite_x,
|
| 958 |
+
should_overwrite=should_overwrite(data, fftshape, axes))
|
| 959 |
+
if data.ndim > 1:
|
| 960 |
+
# check fortran order
|
| 961 |
+
self._check(data.T, routine, fftshape, axes,
|
| 962 |
+
overwrite_x=overwrite_x,
|
| 963 |
+
should_overwrite=should_overwrite(
|
| 964 |
+
data.T, fftshape, axes))
|
| 965 |
+
|
| 966 |
+
@pytest.mark.parametrize('dtype', dtypes)
|
| 967 |
+
@pytest.mark.parametrize('overwrite_x', [True, False])
|
| 968 |
+
@pytest.mark.parametrize('shape,axes', [((16,), None),
|
| 969 |
+
((16,), (0,)),
|
| 970 |
+
((16, 2), (0,)),
|
| 971 |
+
((2, 16), (1,)),
|
| 972 |
+
((8, 16), None),
|
| 973 |
+
((8, 16), (0, 1)),
|
| 974 |
+
((8, 16, 2), (0, 1)),
|
| 975 |
+
((8, 16, 2), (1, 2)),
|
| 976 |
+
((8, 16, 2), (0,)),
|
| 977 |
+
((8, 16, 2), (1,)),
|
| 978 |
+
((8, 16, 2), (2,)),
|
| 979 |
+
((8, 16, 2), None),
|
| 980 |
+
((8, 16, 2), (0, 1, 2))])
|
| 981 |
+
def test_fftn_ifftn(self, dtype, overwrite_x, shape, axes):
|
| 982 |
+
overwritable = (np.clongdouble, np.complex128, np.complex64)
|
| 983 |
+
self._check_nd_one(fftn, dtype, shape, axes, overwritable,
|
| 984 |
+
overwrite_x)
|
| 985 |
+
self._check_nd_one(ifftn, dtype, shape, axes, overwritable,
|
| 986 |
+
overwrite_x)
|
| 987 |
+
|
| 988 |
+
|
| 989 |
+
@pytest.mark.parametrize('func', [fft, ifft, fftn, ifftn,
|
| 990 |
+
rfft, irfft, rfftn, irfftn])
|
| 991 |
+
def test_invalid_norm(func):
|
| 992 |
+
x = np.arange(10, dtype=float)
|
| 993 |
+
with assert_raises(ValueError,
|
| 994 |
+
match='Invalid norm value \'o\', should be'
|
| 995 |
+
' "backward", "ortho" or "forward"'):
|
| 996 |
+
func(x, norm='o')
|
| 997 |
+
|
| 998 |
+
|
| 999 |
+
@pytest.mark.parametrize('func', [fft, ifft, fftn, ifftn,
|
| 1000 |
+
irfft, irfftn, hfft, hfftn])
|
| 1001 |
+
def test_swapped_byte_order_complex(func):
|
| 1002 |
+
rng = np.random.RandomState(1234)
|
| 1003 |
+
x = rng.rand(10) + 1j * rng.rand(10)
|
| 1004 |
+
assert_allclose(func(swap_byteorder(x)), func(x))
|
| 1005 |
+
|
| 1006 |
+
|
| 1007 |
+
@pytest.mark.parametrize('func', [ihfft, ihfftn, rfft, rfftn])
|
| 1008 |
+
def test_swapped_byte_order_real(func):
|
| 1009 |
+
rng = np.random.RandomState(1234)
|
| 1010 |
+
x = rng.rand(10)
|
| 1011 |
+
assert_allclose(func(swap_byteorder(x)), func(x))
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/_pocketfft/tests/test_real_transforms.py
ADDED
|
@@ -0,0 +1,506 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from os.path import join, dirname
|
| 2 |
+
from collections.abc import Callable
|
| 3 |
+
from threading import Lock
|
| 4 |
+
|
| 5 |
+
import numpy as np
|
| 6 |
+
from numpy.testing import (
|
| 7 |
+
assert_array_almost_equal, assert_equal, assert_allclose)
|
| 8 |
+
import pytest
|
| 9 |
+
from pytest import raises as assert_raises
|
| 10 |
+
|
| 11 |
+
from scipy.fft._pocketfft.realtransforms import (
|
| 12 |
+
dct, idct, dst, idst, dctn, idctn, dstn, idstn)
|
| 13 |
+
|
| 14 |
+
fftpack_test_dir = join(dirname(__file__), '..', '..', '..', 'fftpack', 'tests')
|
| 15 |
+
|
| 16 |
+
MDATA_COUNT = 8
|
| 17 |
+
FFTWDATA_COUNT = 14
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def is_longdouble_binary_compatible():
|
| 21 |
+
try:
|
| 22 |
+
one = np.frombuffer(
|
| 23 |
+
b'\x00\x00\x00\x00\x00\x00\x00\x80\xff\x3f\x00\x00\x00\x00\x00\x00',
|
| 24 |
+
dtype='<f16')
|
| 25 |
+
return one == np.longdouble(1.)
|
| 26 |
+
except TypeError:
|
| 27 |
+
return False
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
@pytest.fixture(scope="module")
|
| 31 |
+
def reference_data():
|
| 32 |
+
# Matlab reference data
|
| 33 |
+
MDATA = np.load(join(fftpack_test_dir, 'test.npz'))
|
| 34 |
+
X = [MDATA[f'x{i}'] for i in range(MDATA_COUNT)]
|
| 35 |
+
Y = [MDATA[f'y{i}'] for i in range(MDATA_COUNT)]
|
| 36 |
+
|
| 37 |
+
# FFTW reference data: the data are organized as follows:
|
| 38 |
+
# * SIZES is an array containing all available sizes
|
| 39 |
+
# * for every type (1, 2, 3, 4) and every size, the array dct_type_size
|
| 40 |
+
# contains the output of the DCT applied to the input np.linspace(0, size-1,
|
| 41 |
+
# size)
|
| 42 |
+
FFTWDATA_DOUBLE = np.load(join(fftpack_test_dir, 'fftw_double_ref.npz'))
|
| 43 |
+
FFTWDATA_SINGLE = np.load(join(fftpack_test_dir, 'fftw_single_ref.npz'))
|
| 44 |
+
FFTWDATA_SIZES = FFTWDATA_DOUBLE['sizes']
|
| 45 |
+
assert len(FFTWDATA_SIZES) == FFTWDATA_COUNT
|
| 46 |
+
|
| 47 |
+
if is_longdouble_binary_compatible():
|
| 48 |
+
FFTWDATA_LONGDOUBLE = np.load(
|
| 49 |
+
join(fftpack_test_dir, 'fftw_longdouble_ref.npz'))
|
| 50 |
+
else:
|
| 51 |
+
FFTWDATA_LONGDOUBLE = {k: v.astype(np.longdouble)
|
| 52 |
+
for k,v in FFTWDATA_DOUBLE.items()}
|
| 53 |
+
|
| 54 |
+
ref = {
|
| 55 |
+
'FFTWDATA_LONGDOUBLE': FFTWDATA_LONGDOUBLE,
|
| 56 |
+
'FFTWDATA_DOUBLE': FFTWDATA_DOUBLE,
|
| 57 |
+
'FFTWDATA_SINGLE': FFTWDATA_SINGLE,
|
| 58 |
+
'FFTWDATA_SIZES': FFTWDATA_SIZES,
|
| 59 |
+
'X': X,
|
| 60 |
+
'Y': Y
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
yield ref
|
| 64 |
+
|
| 65 |
+
if is_longdouble_binary_compatible():
|
| 66 |
+
FFTWDATA_LONGDOUBLE.close()
|
| 67 |
+
FFTWDATA_SINGLE.close()
|
| 68 |
+
FFTWDATA_DOUBLE.close()
|
| 69 |
+
MDATA.close()
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
@pytest.fixture(params=range(FFTWDATA_COUNT))
|
| 73 |
+
def fftwdata_size(request, reference_data):
|
| 74 |
+
return reference_data['FFTWDATA_SIZES'][request.param]
|
| 75 |
+
|
| 76 |
+
@pytest.fixture(params=range(MDATA_COUNT))
|
| 77 |
+
def mdata_x(request, reference_data):
|
| 78 |
+
return reference_data['X'][request.param]
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
@pytest.fixture(params=range(MDATA_COUNT))
|
| 82 |
+
def mdata_xy(request, reference_data):
|
| 83 |
+
y = reference_data['Y'][request.param]
|
| 84 |
+
x = reference_data['X'][request.param]
|
| 85 |
+
return x, y
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
@pytest.fixture
|
| 89 |
+
def ref_lock():
|
| 90 |
+
return Lock()
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
def fftw_dct_ref(type, size, dt, reference_data):
|
| 94 |
+
x = np.linspace(0, size-1, size).astype(dt)
|
| 95 |
+
dt = np.result_type(np.float32, dt)
|
| 96 |
+
if dt == np.float64:
|
| 97 |
+
data = reference_data['FFTWDATA_DOUBLE']
|
| 98 |
+
elif dt == np.float32:
|
| 99 |
+
data = reference_data['FFTWDATA_SINGLE']
|
| 100 |
+
elif dt == np.longdouble:
|
| 101 |
+
data = reference_data['FFTWDATA_LONGDOUBLE']
|
| 102 |
+
else:
|
| 103 |
+
raise ValueError()
|
| 104 |
+
y = (data[f'dct_{type}_{size}']).astype(dt)
|
| 105 |
+
return x, y, dt
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
def fftw_dst_ref(type, size, dt, reference_data):
|
| 109 |
+
x = np.linspace(0, size-1, size).astype(dt)
|
| 110 |
+
dt = np.result_type(np.float32, dt)
|
| 111 |
+
if dt == np.float64:
|
| 112 |
+
data = reference_data['FFTWDATA_DOUBLE']
|
| 113 |
+
elif dt == np.float32:
|
| 114 |
+
data = reference_data['FFTWDATA_SINGLE']
|
| 115 |
+
elif dt == np.longdouble:
|
| 116 |
+
data = reference_data['FFTWDATA_LONGDOUBLE']
|
| 117 |
+
else:
|
| 118 |
+
raise ValueError()
|
| 119 |
+
y = (data[f'dst_{type}_{size}']).astype(dt)
|
| 120 |
+
return x, y, dt
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
def ref_2d(func, x, **kwargs):
|
| 124 |
+
"""Calculate 2-D reference data from a 1d transform"""
|
| 125 |
+
x = np.array(x, copy=True)
|
| 126 |
+
for row in range(x.shape[0]):
|
| 127 |
+
x[row, :] = func(x[row, :], **kwargs)
|
| 128 |
+
for col in range(x.shape[1]):
|
| 129 |
+
x[:, col] = func(x[:, col], **kwargs)
|
| 130 |
+
return x
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
def naive_dct1(x, norm=None):
|
| 134 |
+
"""Calculate textbook definition version of DCT-I."""
|
| 135 |
+
x = np.array(x, copy=True)
|
| 136 |
+
N = len(x)
|
| 137 |
+
M = N-1
|
| 138 |
+
y = np.zeros(N)
|
| 139 |
+
m0, m = 1, 2
|
| 140 |
+
if norm == 'ortho':
|
| 141 |
+
m0 = np.sqrt(1.0/M)
|
| 142 |
+
m = np.sqrt(2.0/M)
|
| 143 |
+
for k in range(N):
|
| 144 |
+
for n in range(1, N-1):
|
| 145 |
+
y[k] += m*x[n]*np.cos(np.pi*n*k/M)
|
| 146 |
+
y[k] += m0 * x[0]
|
| 147 |
+
y[k] += m0 * x[N-1] * (1 if k % 2 == 0 else -1)
|
| 148 |
+
if norm == 'ortho':
|
| 149 |
+
y[0] *= 1/np.sqrt(2)
|
| 150 |
+
y[N-1] *= 1/np.sqrt(2)
|
| 151 |
+
return y
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
def naive_dst1(x, norm=None):
|
| 155 |
+
"""Calculate textbook definition version of DST-I."""
|
| 156 |
+
x = np.array(x, copy=True)
|
| 157 |
+
N = len(x)
|
| 158 |
+
M = N+1
|
| 159 |
+
y = np.zeros(N)
|
| 160 |
+
for k in range(N):
|
| 161 |
+
for n in range(N):
|
| 162 |
+
y[k] += 2*x[n]*np.sin(np.pi*(n+1.0)*(k+1.0)/M)
|
| 163 |
+
if norm == 'ortho':
|
| 164 |
+
y *= np.sqrt(0.5/M)
|
| 165 |
+
return y
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
def naive_dct4(x, norm=None):
|
| 169 |
+
"""Calculate textbook definition version of DCT-IV."""
|
| 170 |
+
x = np.array(x, copy=True)
|
| 171 |
+
N = len(x)
|
| 172 |
+
y = np.zeros(N)
|
| 173 |
+
for k in range(N):
|
| 174 |
+
for n in range(N):
|
| 175 |
+
y[k] += x[n]*np.cos(np.pi*(n+0.5)*(k+0.5)/(N))
|
| 176 |
+
if norm == 'ortho':
|
| 177 |
+
y *= np.sqrt(2.0/N)
|
| 178 |
+
else:
|
| 179 |
+
y *= 2
|
| 180 |
+
return y
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
def naive_dst4(x, norm=None):
|
| 184 |
+
"""Calculate textbook definition version of DST-IV."""
|
| 185 |
+
x = np.array(x, copy=True)
|
| 186 |
+
N = len(x)
|
| 187 |
+
y = np.zeros(N)
|
| 188 |
+
for k in range(N):
|
| 189 |
+
for n in range(N):
|
| 190 |
+
y[k] += x[n]*np.sin(np.pi*(n+0.5)*(k+0.5)/(N))
|
| 191 |
+
if norm == 'ortho':
|
| 192 |
+
y *= np.sqrt(2.0/N)
|
| 193 |
+
else:
|
| 194 |
+
y *= 2
|
| 195 |
+
return y
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
@pytest.mark.parametrize('dtype', [np.complex64, np.complex128, np.clongdouble])
|
| 199 |
+
@pytest.mark.parametrize('transform', [dct, dst, idct, idst])
|
| 200 |
+
def test_complex(transform, dtype):
|
| 201 |
+
y = transform(1j*np.arange(5, dtype=dtype))
|
| 202 |
+
x = 1j*transform(np.arange(5))
|
| 203 |
+
assert_array_almost_equal(x, y)
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
DecMapType = dict[
|
| 207 |
+
tuple[Callable[..., np.ndarray], type[np.floating] | type[int], int],
|
| 208 |
+
int,
|
| 209 |
+
]
|
| 210 |
+
|
| 211 |
+
# map (transform, dtype, type) -> decimal
|
| 212 |
+
dec_map: DecMapType = {
|
| 213 |
+
# DCT
|
| 214 |
+
(dct, np.float64, 1): 13,
|
| 215 |
+
(dct, np.float32, 1): 6,
|
| 216 |
+
|
| 217 |
+
(dct, np.float64, 2): 14,
|
| 218 |
+
(dct, np.float32, 2): 5,
|
| 219 |
+
|
| 220 |
+
(dct, np.float64, 3): 14,
|
| 221 |
+
(dct, np.float32, 3): 5,
|
| 222 |
+
|
| 223 |
+
(dct, np.float64, 4): 13,
|
| 224 |
+
(dct, np.float32, 4): 6,
|
| 225 |
+
|
| 226 |
+
# IDCT
|
| 227 |
+
(idct, np.float64, 1): 14,
|
| 228 |
+
(idct, np.float32, 1): 6,
|
| 229 |
+
|
| 230 |
+
(idct, np.float64, 2): 14,
|
| 231 |
+
(idct, np.float32, 2): 5,
|
| 232 |
+
|
| 233 |
+
(idct, np.float64, 3): 14,
|
| 234 |
+
(idct, np.float32, 3): 5,
|
| 235 |
+
|
| 236 |
+
(idct, np.float64, 4): 14,
|
| 237 |
+
(idct, np.float32, 4): 6,
|
| 238 |
+
|
| 239 |
+
# DST
|
| 240 |
+
(dst, np.float64, 1): 13,
|
| 241 |
+
(dst, np.float32, 1): 6,
|
| 242 |
+
|
| 243 |
+
(dst, np.float64, 2): 14,
|
| 244 |
+
(dst, np.float32, 2): 6,
|
| 245 |
+
|
| 246 |
+
(dst, np.float64, 3): 14,
|
| 247 |
+
(dst, np.float32, 3): 7,
|
| 248 |
+
|
| 249 |
+
(dst, np.float64, 4): 13,
|
| 250 |
+
(dst, np.float32, 4): 5,
|
| 251 |
+
|
| 252 |
+
# IDST
|
| 253 |
+
(idst, np.float64, 1): 14,
|
| 254 |
+
(idst, np.float32, 1): 6,
|
| 255 |
+
|
| 256 |
+
(idst, np.float64, 2): 14,
|
| 257 |
+
(idst, np.float32, 2): 6,
|
| 258 |
+
|
| 259 |
+
(idst, np.float64, 3): 14,
|
| 260 |
+
(idst, np.float32, 3): 6,
|
| 261 |
+
|
| 262 |
+
(idst, np.float64, 4): 14,
|
| 263 |
+
(idst, np.float32, 4): 6,
|
| 264 |
+
}
|
| 265 |
+
|
| 266 |
+
for k,v in dec_map.copy().items():
|
| 267 |
+
if k[1] == np.float64:
|
| 268 |
+
dec_map[(k[0], np.longdouble, k[2])] = v
|
| 269 |
+
elif k[1] == np.float32:
|
| 270 |
+
dec_map[(k[0], int, k[2])] = v
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
@pytest.mark.parametrize('rdt', [np.longdouble, np.float64, np.float32, int])
|
| 274 |
+
@pytest.mark.parametrize('type', [1, 2, 3, 4])
|
| 275 |
+
class TestDCT:
|
| 276 |
+
def test_definition(self, rdt, type, fftwdata_size,
|
| 277 |
+
reference_data, ref_lock):
|
| 278 |
+
with ref_lock:
|
| 279 |
+
x, yr, dt = fftw_dct_ref(type, fftwdata_size, rdt, reference_data)
|
| 280 |
+
y = dct(x, type=type)
|
| 281 |
+
assert_equal(y.dtype, dt)
|
| 282 |
+
dec = dec_map[(dct, rdt, type)]
|
| 283 |
+
assert_allclose(y, yr, rtol=0., atol=np.max(yr)*10**(-dec))
|
| 284 |
+
|
| 285 |
+
@pytest.mark.parametrize('size', [7, 8, 9, 16, 32, 64])
|
| 286 |
+
def test_axis(self, rdt, type, size):
|
| 287 |
+
nt = 2
|
| 288 |
+
dec = dec_map[(dct, rdt, type)]
|
| 289 |
+
x = np.random.randn(nt, size)
|
| 290 |
+
y = dct(x, type=type)
|
| 291 |
+
for j in range(nt):
|
| 292 |
+
assert_array_almost_equal(y[j], dct(x[j], type=type),
|
| 293 |
+
decimal=dec)
|
| 294 |
+
|
| 295 |
+
x = x.T
|
| 296 |
+
y = dct(x, axis=0, type=type)
|
| 297 |
+
for j in range(nt):
|
| 298 |
+
assert_array_almost_equal(y[:,j], dct(x[:,j], type=type),
|
| 299 |
+
decimal=dec)
|
| 300 |
+
|
| 301 |
+
|
| 302 |
+
@pytest.mark.parametrize('rdt', [np.longdouble, np.float64, np.float32, int])
|
| 303 |
+
def test_dct1_definition_ortho(rdt, mdata_x):
|
| 304 |
+
# Test orthornomal mode.
|
| 305 |
+
dec = dec_map[(dct, rdt, 1)]
|
| 306 |
+
x = np.array(mdata_x, dtype=rdt)
|
| 307 |
+
dt = np.result_type(np.float32, rdt)
|
| 308 |
+
y = dct(x, norm='ortho', type=1)
|
| 309 |
+
y2 = naive_dct1(x, norm='ortho')
|
| 310 |
+
assert_equal(y.dtype, dt)
|
| 311 |
+
assert_allclose(y, y2, rtol=0., atol=np.max(y2)*10**(-dec))
|
| 312 |
+
|
| 313 |
+
|
| 314 |
+
@pytest.mark.parametrize('rdt', [np.longdouble, np.float64, np.float32, int])
|
| 315 |
+
def test_dct2_definition_matlab(mdata_xy, rdt):
|
| 316 |
+
# Test correspondence with matlab (orthornomal mode).
|
| 317 |
+
dt = np.result_type(np.float32, rdt)
|
| 318 |
+
x = np.array(mdata_xy[0], dtype=dt)
|
| 319 |
+
|
| 320 |
+
yr = mdata_xy[1]
|
| 321 |
+
y = dct(x, norm="ortho", type=2)
|
| 322 |
+
dec = dec_map[(dct, rdt, 2)]
|
| 323 |
+
assert_equal(y.dtype, dt)
|
| 324 |
+
assert_array_almost_equal(y, yr, decimal=dec)
|
| 325 |
+
|
| 326 |
+
|
| 327 |
+
@pytest.mark.parametrize('rdt', [np.longdouble, np.float64, np.float32, int])
|
| 328 |
+
def test_dct3_definition_ortho(mdata_x, rdt):
|
| 329 |
+
# Test orthornomal mode.
|
| 330 |
+
x = np.array(mdata_x, dtype=rdt)
|
| 331 |
+
dt = np.result_type(np.float32, rdt)
|
| 332 |
+
y = dct(x, norm='ortho', type=2)
|
| 333 |
+
xi = dct(y, norm="ortho", type=3)
|
| 334 |
+
dec = dec_map[(dct, rdt, 3)]
|
| 335 |
+
assert_equal(xi.dtype, dt)
|
| 336 |
+
assert_array_almost_equal(xi, x, decimal=dec)
|
| 337 |
+
|
| 338 |
+
|
| 339 |
+
@pytest.mark.parametrize('rdt', [np.longdouble, np.float64, np.float32, int])
|
| 340 |
+
def test_dct4_definition_ortho(mdata_x, rdt):
|
| 341 |
+
# Test orthornomal mode.
|
| 342 |
+
x = np.array(mdata_x, dtype=rdt)
|
| 343 |
+
dt = np.result_type(np.float32, rdt)
|
| 344 |
+
y = dct(x, norm='ortho', type=4)
|
| 345 |
+
y2 = naive_dct4(x, norm='ortho')
|
| 346 |
+
dec = dec_map[(dct, rdt, 4)]
|
| 347 |
+
assert_equal(y.dtype, dt)
|
| 348 |
+
assert_allclose(y, y2, rtol=0., atol=np.max(y2)*10**(-dec))
|
| 349 |
+
|
| 350 |
+
|
| 351 |
+
@pytest.mark.parametrize('rdt', [np.longdouble, np.float64, np.float32, int])
|
| 352 |
+
@pytest.mark.parametrize('type', [1, 2, 3, 4])
|
| 353 |
+
def test_idct_definition(fftwdata_size, rdt, type, reference_data, ref_lock):
|
| 354 |
+
with ref_lock:
|
| 355 |
+
xr, yr, dt = fftw_dct_ref(type, fftwdata_size, rdt, reference_data)
|
| 356 |
+
x = idct(yr, type=type)
|
| 357 |
+
dec = dec_map[(idct, rdt, type)]
|
| 358 |
+
assert_equal(x.dtype, dt)
|
| 359 |
+
assert_allclose(x, xr, rtol=0., atol=np.max(xr)*10**(-dec))
|
| 360 |
+
|
| 361 |
+
|
| 362 |
+
@pytest.mark.parametrize('rdt', [np.longdouble, np.float64, np.float32, int])
|
| 363 |
+
@pytest.mark.parametrize('type', [1, 2, 3, 4])
|
| 364 |
+
def test_definition(fftwdata_size, rdt, type, reference_data, ref_lock):
|
| 365 |
+
with ref_lock:
|
| 366 |
+
xr, yr, dt = fftw_dst_ref(type, fftwdata_size, rdt, reference_data)
|
| 367 |
+
y = dst(xr, type=type)
|
| 368 |
+
dec = dec_map[(dst, rdt, type)]
|
| 369 |
+
assert_equal(y.dtype, dt)
|
| 370 |
+
assert_allclose(y, yr, rtol=0., atol=np.max(yr)*10**(-dec))
|
| 371 |
+
|
| 372 |
+
|
| 373 |
+
@pytest.mark.parametrize('rdt', [np.longdouble, np.float64, np.float32, int])
|
| 374 |
+
def test_dst1_definition_ortho(rdt, mdata_x):
|
| 375 |
+
# Test orthornomal mode.
|
| 376 |
+
dec = dec_map[(dst, rdt, 1)]
|
| 377 |
+
x = np.array(mdata_x, dtype=rdt)
|
| 378 |
+
dt = np.result_type(np.float32, rdt)
|
| 379 |
+
y = dst(x, norm='ortho', type=1)
|
| 380 |
+
y2 = naive_dst1(x, norm='ortho')
|
| 381 |
+
assert_equal(y.dtype, dt)
|
| 382 |
+
assert_allclose(y, y2, rtol=0., atol=np.max(y2)*10**(-dec))
|
| 383 |
+
|
| 384 |
+
|
| 385 |
+
@pytest.mark.parametrize('rdt', [np.longdouble, np.float64, np.float32, int])
|
| 386 |
+
def test_dst4_definition_ortho(rdt, mdata_x):
|
| 387 |
+
# Test orthornomal mode.
|
| 388 |
+
dec = dec_map[(dst, rdt, 4)]
|
| 389 |
+
x = np.array(mdata_x, dtype=rdt)
|
| 390 |
+
dt = np.result_type(np.float32, rdt)
|
| 391 |
+
y = dst(x, norm='ortho', type=4)
|
| 392 |
+
y2 = naive_dst4(x, norm='ortho')
|
| 393 |
+
assert_equal(y.dtype, dt)
|
| 394 |
+
assert_array_almost_equal(y, y2, decimal=dec)
|
| 395 |
+
|
| 396 |
+
|
| 397 |
+
@pytest.mark.parametrize('rdt', [np.longdouble, np.float64, np.float32, int])
|
| 398 |
+
@pytest.mark.parametrize('type', [1, 2, 3, 4])
|
| 399 |
+
def test_idst_definition(fftwdata_size, rdt, type, reference_data, ref_lock):
|
| 400 |
+
with ref_lock:
|
| 401 |
+
xr, yr, dt = fftw_dst_ref(type, fftwdata_size, rdt, reference_data)
|
| 402 |
+
x = idst(yr, type=type)
|
| 403 |
+
dec = dec_map[(idst, rdt, type)]
|
| 404 |
+
assert_equal(x.dtype, dt)
|
| 405 |
+
assert_allclose(x, xr, rtol=0., atol=np.max(xr)*10**(-dec))
|
| 406 |
+
|
| 407 |
+
|
| 408 |
+
@pytest.mark.parametrize('routine', [dct, dst, idct, idst])
|
| 409 |
+
@pytest.mark.parametrize('dtype', [np.float32, np.float64, np.longdouble])
|
| 410 |
+
@pytest.mark.parametrize('shape, axis', [
|
| 411 |
+
((16,), -1), ((16, 2), 0), ((2, 16), 1)
|
| 412 |
+
])
|
| 413 |
+
@pytest.mark.parametrize('type', [1, 2, 3, 4])
|
| 414 |
+
@pytest.mark.parametrize('overwrite_x', [True, False])
|
| 415 |
+
@pytest.mark.parametrize('norm', [None, 'ortho'])
|
| 416 |
+
def test_overwrite(routine, dtype, shape, axis, type, norm, overwrite_x):
|
| 417 |
+
# Check input overwrite behavior
|
| 418 |
+
np.random.seed(1234)
|
| 419 |
+
if np.issubdtype(dtype, np.complexfloating):
|
| 420 |
+
x = np.random.randn(*shape) + 1j*np.random.randn(*shape)
|
| 421 |
+
else:
|
| 422 |
+
x = np.random.randn(*shape)
|
| 423 |
+
x = x.astype(dtype)
|
| 424 |
+
x2 = x.copy()
|
| 425 |
+
routine(x2, type, None, axis, norm, overwrite_x=overwrite_x)
|
| 426 |
+
|
| 427 |
+
sig = (f"{routine.__name__}({x.dtype}{x.shape!r}, {None!r}, axis={axis!r}, "
|
| 428 |
+
f"overwrite_x={overwrite_x!r})")
|
| 429 |
+
if not overwrite_x:
|
| 430 |
+
assert_equal(x2, x, err_msg=f"spurious overwrite in {sig}")
|
| 431 |
+
|
| 432 |
+
|
| 433 |
+
class Test_DCTN_IDCTN:
|
| 434 |
+
dec = 14
|
| 435 |
+
dct_type = [1, 2, 3, 4]
|
| 436 |
+
norms = [None, 'backward', 'ortho', 'forward']
|
| 437 |
+
rstate = np.random.RandomState(1234)
|
| 438 |
+
shape = (32, 16)
|
| 439 |
+
data = rstate.randn(*shape)
|
| 440 |
+
|
| 441 |
+
@pytest.mark.parametrize('fforward,finverse', [(dctn, idctn),
|
| 442 |
+
(dstn, idstn)])
|
| 443 |
+
@pytest.mark.parametrize('axes', [None,
|
| 444 |
+
1, (1,), [1],
|
| 445 |
+
0, (0,), [0],
|
| 446 |
+
(0, 1), [0, 1],
|
| 447 |
+
(-2, -1), [-2, -1]])
|
| 448 |
+
@pytest.mark.parametrize('dct_type', dct_type)
|
| 449 |
+
@pytest.mark.parametrize('norm', ['ortho'])
|
| 450 |
+
def test_axes_round_trip(self, fforward, finverse, axes, dct_type, norm):
|
| 451 |
+
tmp = fforward(self.data, type=dct_type, axes=axes, norm=norm)
|
| 452 |
+
tmp = finverse(tmp, type=dct_type, axes=axes, norm=norm)
|
| 453 |
+
assert_array_almost_equal(self.data, tmp, decimal=12)
|
| 454 |
+
|
| 455 |
+
@pytest.mark.parametrize('funcn,func', [(dctn, dct), (dstn, dst)])
|
| 456 |
+
@pytest.mark.parametrize('dct_type', dct_type)
|
| 457 |
+
@pytest.mark.parametrize('norm', norms)
|
| 458 |
+
def test_dctn_vs_2d_reference(self, funcn, func, dct_type, norm):
|
| 459 |
+
y1 = funcn(self.data, type=dct_type, axes=None, norm=norm)
|
| 460 |
+
y2 = ref_2d(func, self.data, type=dct_type, norm=norm)
|
| 461 |
+
assert_array_almost_equal(y1, y2, decimal=11)
|
| 462 |
+
|
| 463 |
+
@pytest.mark.parametrize('funcn,func', [(idctn, idct), (idstn, idst)])
|
| 464 |
+
@pytest.mark.parametrize('dct_type', dct_type)
|
| 465 |
+
@pytest.mark.parametrize('norm', norms)
|
| 466 |
+
def test_idctn_vs_2d_reference(self, funcn, func, dct_type, norm):
|
| 467 |
+
fdata = dctn(self.data, type=dct_type, norm=norm)
|
| 468 |
+
y1 = funcn(fdata, type=dct_type, norm=norm)
|
| 469 |
+
y2 = ref_2d(func, fdata, type=dct_type, norm=norm)
|
| 470 |
+
assert_array_almost_equal(y1, y2, decimal=11)
|
| 471 |
+
|
| 472 |
+
@pytest.mark.parametrize('fforward,finverse', [(dctn, idctn),
|
| 473 |
+
(dstn, idstn)])
|
| 474 |
+
def test_axes_and_shape(self, fforward, finverse):
|
| 475 |
+
with assert_raises(ValueError,
|
| 476 |
+
match="when given, axes and shape arguments"
|
| 477 |
+
" have to be of the same length"):
|
| 478 |
+
fforward(self.data, s=self.data.shape[0], axes=(0, 1))
|
| 479 |
+
|
| 480 |
+
with assert_raises(ValueError,
|
| 481 |
+
match="when given, axes and shape arguments"
|
| 482 |
+
" have to be of the same length"):
|
| 483 |
+
fforward(self.data, s=self.data.shape, axes=0)
|
| 484 |
+
|
| 485 |
+
@pytest.mark.parametrize('fforward', [dctn, dstn])
|
| 486 |
+
def test_shape(self, fforward):
|
| 487 |
+
tmp = fforward(self.data, s=(128, 128), axes=None)
|
| 488 |
+
assert_equal(tmp.shape, (128, 128))
|
| 489 |
+
|
| 490 |
+
@pytest.mark.parametrize('fforward,finverse', [(dctn, idctn),
|
| 491 |
+
(dstn, idstn)])
|
| 492 |
+
@pytest.mark.parametrize('axes', [1, (1,), [1],
|
| 493 |
+
0, (0,), [0]])
|
| 494 |
+
def test_shape_is_none_with_axes(self, fforward, finverse, axes):
|
| 495 |
+
tmp = fforward(self.data, s=None, axes=axes, norm='ortho')
|
| 496 |
+
tmp = finverse(tmp, s=None, axes=axes, norm='ortho')
|
| 497 |
+
assert_array_almost_equal(self.data, tmp, decimal=self.dec)
|
| 498 |
+
|
| 499 |
+
|
| 500 |
+
@pytest.mark.parametrize('func', [dct, dctn, idct, idctn,
|
| 501 |
+
dst, dstn, idst, idstn])
|
| 502 |
+
def test_swapped_byte_order(func):
|
| 503 |
+
rng = np.random.RandomState(1234)
|
| 504 |
+
x = rng.rand(10)
|
| 505 |
+
swapped_dt = x.dtype.newbyteorder('S')
|
| 506 |
+
assert_allclose(func(x.astype(swapped_dt)), func(x))
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/_realtransforms.py
ADDED
|
@@ -0,0 +1,717 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ._basic import _dispatch
|
| 2 |
+
from scipy._lib.uarray import Dispatchable
|
| 3 |
+
from scipy._lib._array_api import xp_capabilities
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
__all__ = ['dct', 'idct', 'dst', 'idst', 'dctn', 'idctn', 'dstn', 'idstn']
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
@xp_capabilities(cpu_only=True, allow_dask_compute=True)
|
| 10 |
+
@_dispatch
|
| 11 |
+
def dctn(x, type=2, s=None, axes=None, norm=None, overwrite_x=False,
|
| 12 |
+
workers=None, *, orthogonalize=None):
|
| 13 |
+
"""
|
| 14 |
+
Return multidimensional Discrete Cosine Transform along the specified axes.
|
| 15 |
+
|
| 16 |
+
Parameters
|
| 17 |
+
----------
|
| 18 |
+
x : array_like
|
| 19 |
+
The input array.
|
| 20 |
+
type : {1, 2, 3, 4}, optional
|
| 21 |
+
Type of the DCT (see Notes). Default type is 2.
|
| 22 |
+
s : int or array_like of ints or None, optional
|
| 23 |
+
The shape of the result. If both `s` and `axes` (see below) are None,
|
| 24 |
+
`s` is ``x.shape``; if `s` is None but `axes` is not None, then `s` is
|
| 25 |
+
``numpy.take(x.shape, axes, axis=0)``.
|
| 26 |
+
If ``s[i] > x.shape[i]``, the ith dimension of the input is padded with zeros.
|
| 27 |
+
If ``s[i] < x.shape[i]``, the ith dimension of the input is truncated to length
|
| 28 |
+
``s[i]``.
|
| 29 |
+
If any element of `s` is -1, the size of the corresponding dimension of
|
| 30 |
+
`x` is used.
|
| 31 |
+
axes : int or array_like of ints or None, optional
|
| 32 |
+
Axes over which the DCT is computed. If not given, the last ``len(s)``
|
| 33 |
+
axes are used, or all axes if `s` is also not specified.
|
| 34 |
+
norm : {"backward", "ortho", "forward"}, optional
|
| 35 |
+
Normalization mode (see Notes). Default is "backward".
|
| 36 |
+
overwrite_x : bool, optional
|
| 37 |
+
If True, the contents of `x` can be destroyed; the default is False.
|
| 38 |
+
workers : int, optional
|
| 39 |
+
Maximum number of workers to use for parallel computation. If negative,
|
| 40 |
+
the value wraps around from ``os.cpu_count()``.
|
| 41 |
+
See :func:`~scipy.fft.fft` for more details.
|
| 42 |
+
orthogonalize : bool, optional
|
| 43 |
+
Whether to use the orthogonalized DCT variant (see Notes).
|
| 44 |
+
Defaults to ``True`` when ``norm="ortho"`` and ``False`` otherwise.
|
| 45 |
+
|
| 46 |
+
.. versionadded:: 1.8.0
|
| 47 |
+
|
| 48 |
+
Returns
|
| 49 |
+
-------
|
| 50 |
+
y : ndarray of real
|
| 51 |
+
The transformed input array.
|
| 52 |
+
|
| 53 |
+
See Also
|
| 54 |
+
--------
|
| 55 |
+
idctn : Inverse multidimensional DCT
|
| 56 |
+
|
| 57 |
+
Notes
|
| 58 |
+
-----
|
| 59 |
+
For full details of the DCT types and normalization modes, as well as
|
| 60 |
+
references, see `dct`.
|
| 61 |
+
|
| 62 |
+
Examples
|
| 63 |
+
--------
|
| 64 |
+
>>> import numpy as np
|
| 65 |
+
>>> from scipy.fft import dctn, idctn
|
| 66 |
+
>>> rng = np.random.default_rng()
|
| 67 |
+
>>> y = rng.standard_normal((16, 16))
|
| 68 |
+
>>> np.allclose(y, idctn(dctn(y)))
|
| 69 |
+
True
|
| 70 |
+
|
| 71 |
+
"""
|
| 72 |
+
return (Dispatchable(x, np.ndarray),)
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
@xp_capabilities(cpu_only=True, allow_dask_compute=True)
|
| 76 |
+
@_dispatch
|
| 77 |
+
def idctn(x, type=2, s=None, axes=None, norm=None, overwrite_x=False,
|
| 78 |
+
workers=None, orthogonalize=None):
|
| 79 |
+
"""
|
| 80 |
+
Return multidimensional Inverse Discrete Cosine Transform along the specified axes.
|
| 81 |
+
|
| 82 |
+
Parameters
|
| 83 |
+
----------
|
| 84 |
+
x : array_like
|
| 85 |
+
The input array.
|
| 86 |
+
type : {1, 2, 3, 4}, optional
|
| 87 |
+
Type of the DCT (see Notes). Default type is 2.
|
| 88 |
+
s : int or array_like of ints or None, optional
|
| 89 |
+
The shape of the result. If both `s` and `axes` (see below) are
|
| 90 |
+
None, `s` is ``x.shape``; if `s` is None but `axes` is
|
| 91 |
+
not None, then `s` is ``numpy.take(x.shape, axes, axis=0)``.
|
| 92 |
+
If ``s[i] > x.shape[i]``, the ith dimension of the input is padded with zeros.
|
| 93 |
+
If ``s[i] < x.shape[i]``, the ith dimension of the input is truncated to length
|
| 94 |
+
``s[i]``.
|
| 95 |
+
If any element of `s` is -1, the size of the corresponding dimension of
|
| 96 |
+
`x` is used.
|
| 97 |
+
axes : int or array_like of ints or None, optional
|
| 98 |
+
Axes over which the IDCT is computed. If not given, the last ``len(s)``
|
| 99 |
+
axes are used, or all axes if `s` is also not specified.
|
| 100 |
+
norm : {"backward", "ortho", "forward"}, optional
|
| 101 |
+
Normalization mode (see Notes). Default is "backward".
|
| 102 |
+
overwrite_x : bool, optional
|
| 103 |
+
If True, the contents of `x` can be destroyed; the default is False.
|
| 104 |
+
workers : int, optional
|
| 105 |
+
Maximum number of workers to use for parallel computation. If negative,
|
| 106 |
+
the value wraps around from ``os.cpu_count()``.
|
| 107 |
+
See :func:`~scipy.fft.fft` for more details.
|
| 108 |
+
orthogonalize : bool, optional
|
| 109 |
+
Whether to use the orthogonalized IDCT variant (see Notes).
|
| 110 |
+
Defaults to ``True`` when ``norm="ortho"`` and ``False`` otherwise.
|
| 111 |
+
|
| 112 |
+
.. versionadded:: 1.8.0
|
| 113 |
+
|
| 114 |
+
Returns
|
| 115 |
+
-------
|
| 116 |
+
y : ndarray of real
|
| 117 |
+
The transformed input array.
|
| 118 |
+
|
| 119 |
+
See Also
|
| 120 |
+
--------
|
| 121 |
+
dctn : multidimensional DCT
|
| 122 |
+
|
| 123 |
+
Notes
|
| 124 |
+
-----
|
| 125 |
+
For full details of the IDCT types and normalization modes, as well as
|
| 126 |
+
references, see `idct`.
|
| 127 |
+
|
| 128 |
+
Examples
|
| 129 |
+
--------
|
| 130 |
+
>>> import numpy as np
|
| 131 |
+
>>> from scipy.fft import dctn, idctn
|
| 132 |
+
>>> rng = np.random.default_rng()
|
| 133 |
+
>>> y = rng.standard_normal((16, 16))
|
| 134 |
+
>>> np.allclose(y, idctn(dctn(y)))
|
| 135 |
+
True
|
| 136 |
+
|
| 137 |
+
"""
|
| 138 |
+
return (Dispatchable(x, np.ndarray),)
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
@xp_capabilities(cpu_only=True, allow_dask_compute=True)
|
| 142 |
+
@_dispatch
|
| 143 |
+
def dstn(x, type=2, s=None, axes=None, norm=None, overwrite_x=False,
|
| 144 |
+
workers=None, orthogonalize=None):
|
| 145 |
+
"""
|
| 146 |
+
Return multidimensional Discrete Sine Transform along the specified axes.
|
| 147 |
+
|
| 148 |
+
Parameters
|
| 149 |
+
----------
|
| 150 |
+
x : array_like
|
| 151 |
+
The input array.
|
| 152 |
+
type : {1, 2, 3, 4}, optional
|
| 153 |
+
Type of the DST (see Notes). Default type is 2.
|
| 154 |
+
s : int or array_like of ints or None, optional
|
| 155 |
+
The shape of the result. If both `s` and `axes` (see below) are None,
|
| 156 |
+
`s` is ``x.shape``; if `s` is None but `axes` is not None, then `s` is
|
| 157 |
+
``numpy.take(x.shape, axes, axis=0)``.
|
| 158 |
+
If ``s[i] > x.shape[i]``, the ith dimension of the input is padded with zeros.
|
| 159 |
+
If ``s[i] < x.shape[i]``, the ith dimension of the input is truncated to length
|
| 160 |
+
``s[i]``.
|
| 161 |
+
If any element of `shape` is -1, the size of the corresponding dimension
|
| 162 |
+
of `x` is used.
|
| 163 |
+
axes : int or array_like of ints or None, optional
|
| 164 |
+
Axes over which the DST is computed. If not given, the last ``len(s)``
|
| 165 |
+
axes are used, or all axes if `s` is also not specified.
|
| 166 |
+
norm : {"backward", "ortho", "forward"}, optional
|
| 167 |
+
Normalization mode (see Notes). Default is "backward".
|
| 168 |
+
overwrite_x : bool, optional
|
| 169 |
+
If True, the contents of `x` can be destroyed; the default is False.
|
| 170 |
+
workers : int, optional
|
| 171 |
+
Maximum number of workers to use for parallel computation. If negative,
|
| 172 |
+
the value wraps around from ``os.cpu_count()``.
|
| 173 |
+
See :func:`~scipy.fft.fft` for more details.
|
| 174 |
+
orthogonalize : bool, optional
|
| 175 |
+
Whether to use the orthogonalized DST variant (see Notes).
|
| 176 |
+
Defaults to ``True`` when ``norm="ortho"`` and ``False`` otherwise.
|
| 177 |
+
|
| 178 |
+
.. versionadded:: 1.8.0
|
| 179 |
+
|
| 180 |
+
Returns
|
| 181 |
+
-------
|
| 182 |
+
y : ndarray of real
|
| 183 |
+
The transformed input array.
|
| 184 |
+
|
| 185 |
+
See Also
|
| 186 |
+
--------
|
| 187 |
+
idstn : Inverse multidimensional DST
|
| 188 |
+
|
| 189 |
+
Notes
|
| 190 |
+
-----
|
| 191 |
+
For full details of the DST types and normalization modes, as well as
|
| 192 |
+
references, see `dst`.
|
| 193 |
+
|
| 194 |
+
Examples
|
| 195 |
+
--------
|
| 196 |
+
>>> import numpy as np
|
| 197 |
+
>>> from scipy.fft import dstn, idstn
|
| 198 |
+
>>> rng = np.random.default_rng()
|
| 199 |
+
>>> y = rng.standard_normal((16, 16))
|
| 200 |
+
>>> np.allclose(y, idstn(dstn(y)))
|
| 201 |
+
True
|
| 202 |
+
|
| 203 |
+
"""
|
| 204 |
+
return (Dispatchable(x, np.ndarray),)
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
@xp_capabilities(cpu_only=True, allow_dask_compute=True)
|
| 208 |
+
@_dispatch
|
| 209 |
+
def idstn(x, type=2, s=None, axes=None, norm=None, overwrite_x=False,
|
| 210 |
+
workers=None, orthogonalize=None):
|
| 211 |
+
"""
|
| 212 |
+
Return multidimensional Inverse Discrete Sine Transform along the specified axes.
|
| 213 |
+
|
| 214 |
+
Parameters
|
| 215 |
+
----------
|
| 216 |
+
x : array_like
|
| 217 |
+
The input array.
|
| 218 |
+
type : {1, 2, 3, 4}, optional
|
| 219 |
+
Type of the DST (see Notes). Default type is 2.
|
| 220 |
+
s : int or array_like of ints or None, optional
|
| 221 |
+
The shape of the result. If both `s` and `axes` (see below) are None,
|
| 222 |
+
`s` is ``x.shape``; if `s` is None but `axes` is not None, then `s` is
|
| 223 |
+
``numpy.take(x.shape, axes, axis=0)``.
|
| 224 |
+
If ``s[i] > x.shape[i]``, the ith dimension of the input is padded with zeros.
|
| 225 |
+
If ``s[i] < x.shape[i]``, the ith dimension of the input is truncated to length
|
| 226 |
+
``s[i]``.
|
| 227 |
+
If any element of `s` is -1, the size of the corresponding dimension of
|
| 228 |
+
`x` is used.
|
| 229 |
+
axes : int or array_like of ints or None, optional
|
| 230 |
+
Axes over which the IDST is computed. If not given, the last ``len(s)``
|
| 231 |
+
axes are used, or all axes if `s` is also not specified.
|
| 232 |
+
norm : {"backward", "ortho", "forward"}, optional
|
| 233 |
+
Normalization mode (see Notes). Default is "backward".
|
| 234 |
+
overwrite_x : bool, optional
|
| 235 |
+
If True, the contents of `x` can be destroyed; the default is False.
|
| 236 |
+
workers : int, optional
|
| 237 |
+
Maximum number of workers to use for parallel computation. If negative,
|
| 238 |
+
the value wraps around from ``os.cpu_count()``.
|
| 239 |
+
See :func:`~scipy.fft.fft` for more details.
|
| 240 |
+
orthogonalize : bool, optional
|
| 241 |
+
Whether to use the orthogonalized IDST variant (see Notes).
|
| 242 |
+
Defaults to ``True`` when ``norm="ortho"`` and ``False`` otherwise.
|
| 243 |
+
|
| 244 |
+
.. versionadded:: 1.8.0
|
| 245 |
+
|
| 246 |
+
Returns
|
| 247 |
+
-------
|
| 248 |
+
y : ndarray of real
|
| 249 |
+
The transformed input array.
|
| 250 |
+
|
| 251 |
+
See Also
|
| 252 |
+
--------
|
| 253 |
+
dstn : multidimensional DST
|
| 254 |
+
|
| 255 |
+
Notes
|
| 256 |
+
-----
|
| 257 |
+
For full details of the IDST types and normalization modes, as well as
|
| 258 |
+
references, see `idst`.
|
| 259 |
+
|
| 260 |
+
Examples
|
| 261 |
+
--------
|
| 262 |
+
>>> import numpy as np
|
| 263 |
+
>>> from scipy.fft import dstn, idstn
|
| 264 |
+
>>> rng = np.random.default_rng()
|
| 265 |
+
>>> y = rng.standard_normal((16, 16))
|
| 266 |
+
>>> np.allclose(y, idstn(dstn(y)))
|
| 267 |
+
True
|
| 268 |
+
|
| 269 |
+
"""
|
| 270 |
+
return (Dispatchable(x, np.ndarray),)
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
@xp_capabilities(cpu_only=True, allow_dask_compute=True,
|
| 274 |
+
skip_backends=[('jax.numpy', 'XXX large tolerance violations')])
|
| 275 |
+
@_dispatch
|
| 276 |
+
def dct(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False, workers=None,
|
| 277 |
+
orthogonalize=None):
|
| 278 |
+
r"""Return the Discrete Cosine Transform of arbitrary type sequence x.
|
| 279 |
+
|
| 280 |
+
Parameters
|
| 281 |
+
----------
|
| 282 |
+
x : array_like
|
| 283 |
+
The input array.
|
| 284 |
+
type : {1, 2, 3, 4}, optional
|
| 285 |
+
Type of the DCT (see Notes). Default type is 2.
|
| 286 |
+
n : int, optional
|
| 287 |
+
Length of the transform. If ``n < x.shape[axis]``, `x` is
|
| 288 |
+
truncated. If ``n > x.shape[axis]``, `x` is zero-padded. The
|
| 289 |
+
default results in ``n = x.shape[axis]``.
|
| 290 |
+
axis : int, optional
|
| 291 |
+
Axis along which the dct is computed; the default is over the
|
| 292 |
+
last axis (i.e., ``axis=-1``).
|
| 293 |
+
norm : {"backward", "ortho", "forward"}, optional
|
| 294 |
+
Normalization mode (see Notes). Default is "backward".
|
| 295 |
+
overwrite_x : bool, optional
|
| 296 |
+
If True, the contents of `x` can be destroyed; the default is False.
|
| 297 |
+
workers : int, optional
|
| 298 |
+
Maximum number of workers to use for parallel computation. If negative,
|
| 299 |
+
the value wraps around from ``os.cpu_count()``.
|
| 300 |
+
See :func:`~scipy.fft.fft` for more details.
|
| 301 |
+
orthogonalize : bool, optional
|
| 302 |
+
Whether to use the orthogonalized DCT variant (see Notes).
|
| 303 |
+
Defaults to ``True`` when ``norm="ortho"`` and ``False`` otherwise.
|
| 304 |
+
|
| 305 |
+
.. versionadded:: 1.8.0
|
| 306 |
+
|
| 307 |
+
Returns
|
| 308 |
+
-------
|
| 309 |
+
y : ndarray of real
|
| 310 |
+
The transformed input array.
|
| 311 |
+
|
| 312 |
+
See Also
|
| 313 |
+
--------
|
| 314 |
+
idct : Inverse DCT
|
| 315 |
+
|
| 316 |
+
Notes
|
| 317 |
+
-----
|
| 318 |
+
For a single dimension array ``x``, ``dct(x, norm='ortho')`` is equal to
|
| 319 |
+
MATLAB ``dct(x)``.
|
| 320 |
+
|
| 321 |
+
.. warning:: For ``type in {1, 2, 3}``, ``norm="ortho"`` breaks the direct
|
| 322 |
+
correspondence with the direct Fourier transform. To recover
|
| 323 |
+
it you must specify ``orthogonalize=False``.
|
| 324 |
+
|
| 325 |
+
For ``norm="ortho"`` both the `dct` and `idct` are scaled by the same
|
| 326 |
+
overall factor in both directions. By default, the transform is also
|
| 327 |
+
orthogonalized which for types 1, 2 and 3 means the transform definition is
|
| 328 |
+
modified to give orthogonality of the DCT matrix (see below).
|
| 329 |
+
|
| 330 |
+
For ``norm="backward"``, there is no scaling on `dct` and the `idct` is
|
| 331 |
+
scaled by ``1/N`` where ``N`` is the "logical" size of the DCT. For
|
| 332 |
+
``norm="forward"`` the ``1/N`` normalization is applied to the forward
|
| 333 |
+
`dct` instead and the `idct` is unnormalized.
|
| 334 |
+
|
| 335 |
+
There are, theoretically, 8 types of the DCT, only the first 4 types are
|
| 336 |
+
implemented in SciPy.'The' DCT generally refers to DCT type 2, and 'the'
|
| 337 |
+
Inverse DCT generally refers to DCT type 3.
|
| 338 |
+
|
| 339 |
+
**Type I**
|
| 340 |
+
|
| 341 |
+
There are several definitions of the DCT-I; we use the following
|
| 342 |
+
(for ``norm="backward"``)
|
| 343 |
+
|
| 344 |
+
.. math::
|
| 345 |
+
|
| 346 |
+
y_k = x_0 + (-1)^k x_{N-1} + 2 \sum_{n=1}^{N-2} x_n \cos\left(
|
| 347 |
+
\frac{\pi k n}{N-1} \right)
|
| 348 |
+
|
| 349 |
+
If ``orthogonalize=True``, ``x[0]`` and ``x[N-1]`` are multiplied by a
|
| 350 |
+
scaling factor of :math:`\sqrt{2}`, and ``y[0]`` and ``y[N-1]`` are divided
|
| 351 |
+
by :math:`\sqrt{2}`. When combined with ``norm="ortho"``, this makes the
|
| 352 |
+
corresponding matrix of coefficients orthonormal (``O @ O.T = np.eye(N)``).
|
| 353 |
+
|
| 354 |
+
.. note::
|
| 355 |
+
The DCT-I is only supported for input size > 1.
|
| 356 |
+
|
| 357 |
+
**Type II**
|
| 358 |
+
|
| 359 |
+
There are several definitions of the DCT-II; we use the following
|
| 360 |
+
(for ``norm="backward"``)
|
| 361 |
+
|
| 362 |
+
.. math::
|
| 363 |
+
|
| 364 |
+
y_k = 2 \sum_{n=0}^{N-1} x_n \cos\left(\frac{\pi k(2n+1)}{2N} \right)
|
| 365 |
+
|
| 366 |
+
If ``orthogonalize=True``, ``y[0]`` is divided by :math:`\sqrt{2}` which,
|
| 367 |
+
when combined with ``norm="ortho"``, makes the corresponding matrix of
|
| 368 |
+
coefficients orthonormal (``O @ O.T = np.eye(N)``).
|
| 369 |
+
|
| 370 |
+
**Type III**
|
| 371 |
+
|
| 372 |
+
There are several definitions, we use the following (for
|
| 373 |
+
``norm="backward"``)
|
| 374 |
+
|
| 375 |
+
.. math::
|
| 376 |
+
|
| 377 |
+
y_k = x_0 + 2 \sum_{n=1}^{N-1} x_n \cos\left(\frac{\pi(2k+1)n}{2N}\right)
|
| 378 |
+
|
| 379 |
+
If ``orthogonalize=True``, ``x[0]`` terms are multiplied by
|
| 380 |
+
:math:`\sqrt{2}` which, when combined with ``norm="ortho"``, makes the
|
| 381 |
+
corresponding matrix of coefficients orthonormal (``O @ O.T = np.eye(N)``).
|
| 382 |
+
|
| 383 |
+
The (unnormalized) DCT-III is the inverse of the (unnormalized) DCT-II, up
|
| 384 |
+
to a factor `2N`. The orthonormalized DCT-III is exactly the inverse of
|
| 385 |
+
the orthonormalized DCT-II.
|
| 386 |
+
|
| 387 |
+
**Type IV**
|
| 388 |
+
|
| 389 |
+
There are several definitions of the DCT-IV; we use the following
|
| 390 |
+
(for ``norm="backward"``)
|
| 391 |
+
|
| 392 |
+
.. math::
|
| 393 |
+
|
| 394 |
+
y_k = 2 \sum_{n=0}^{N-1} x_n \cos\left(\frac{\pi(2k+1)(2n+1)}{4N} \right)
|
| 395 |
+
|
| 396 |
+
``orthogonalize`` has no effect here, as the DCT-IV matrix is already
|
| 397 |
+
orthogonal up to a scale factor of ``2N``.
|
| 398 |
+
|
| 399 |
+
References
|
| 400 |
+
----------
|
| 401 |
+
.. [1] 'A Fast Cosine Transform in One and Two Dimensions', by J.
|
| 402 |
+
Makhoul, `IEEE Transactions on acoustics, speech and signal
|
| 403 |
+
processing` vol. 28(1), pp. 27-34,
|
| 404 |
+
:doi:`10.1109/TASSP.1980.1163351` (1980).
|
| 405 |
+
.. [2] Wikipedia, "Discrete cosine transform",
|
| 406 |
+
https://en.wikipedia.org/wiki/Discrete_cosine_transform
|
| 407 |
+
|
| 408 |
+
Examples
|
| 409 |
+
--------
|
| 410 |
+
The Type 1 DCT is equivalent to the FFT (though faster) for real,
|
| 411 |
+
even-symmetrical inputs. The output is also real and even-symmetrical.
|
| 412 |
+
Half of the FFT input is used to generate half of the FFT output:
|
| 413 |
+
|
| 414 |
+
>>> from scipy.fft import fft, dct
|
| 415 |
+
>>> import numpy as np
|
| 416 |
+
>>> fft(np.array([4., 3., 5., 10., 5., 3.])).real
|
| 417 |
+
array([ 30., -8., 6., -2., 6., -8.])
|
| 418 |
+
>>> dct(np.array([4., 3., 5., 10.]), 1)
|
| 419 |
+
array([ 30., -8., 6., -2.])
|
| 420 |
+
|
| 421 |
+
"""
|
| 422 |
+
return (Dispatchable(x, np.ndarray),)
|
| 423 |
+
|
| 424 |
+
|
| 425 |
+
@xp_capabilities(cpu_only=True, allow_dask_compute=True)
|
| 426 |
+
@_dispatch
|
| 427 |
+
def idct(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False,
|
| 428 |
+
workers=None, orthogonalize=None):
|
| 429 |
+
"""
|
| 430 |
+
Return the Inverse Discrete Cosine Transform of an arbitrary type sequence.
|
| 431 |
+
|
| 432 |
+
Parameters
|
| 433 |
+
----------
|
| 434 |
+
x : array_like
|
| 435 |
+
The input array.
|
| 436 |
+
type : {1, 2, 3, 4}, optional
|
| 437 |
+
Type of the DCT (see Notes). Default type is 2.
|
| 438 |
+
n : int, optional
|
| 439 |
+
Length of the transform. If ``n < x.shape[axis]``, `x` is
|
| 440 |
+
truncated. If ``n > x.shape[axis]``, `x` is zero-padded. The
|
| 441 |
+
default results in ``n = x.shape[axis]``.
|
| 442 |
+
axis : int, optional
|
| 443 |
+
Axis along which the idct is computed; the default is over the
|
| 444 |
+
last axis (i.e., ``axis=-1``).
|
| 445 |
+
norm : {"backward", "ortho", "forward"}, optional
|
| 446 |
+
Normalization mode (see Notes). Default is "backward".
|
| 447 |
+
overwrite_x : bool, optional
|
| 448 |
+
If True, the contents of `x` can be destroyed; the default is False.
|
| 449 |
+
workers : int, optional
|
| 450 |
+
Maximum number of workers to use for parallel computation. If negative,
|
| 451 |
+
the value wraps around from ``os.cpu_count()``.
|
| 452 |
+
See :func:`~scipy.fft.fft` for more details.
|
| 453 |
+
orthogonalize : bool, optional
|
| 454 |
+
Whether to use the orthogonalized IDCT variant (see Notes).
|
| 455 |
+
Defaults to ``True`` when ``norm="ortho"`` and ``False`` otherwise.
|
| 456 |
+
|
| 457 |
+
.. versionadded:: 1.8.0
|
| 458 |
+
|
| 459 |
+
Returns
|
| 460 |
+
-------
|
| 461 |
+
idct : ndarray of real
|
| 462 |
+
The transformed input array.
|
| 463 |
+
|
| 464 |
+
See Also
|
| 465 |
+
--------
|
| 466 |
+
dct : Forward DCT
|
| 467 |
+
|
| 468 |
+
Notes
|
| 469 |
+
-----
|
| 470 |
+
For a single dimension array `x`, ``idct(x, norm='ortho')`` is equal to
|
| 471 |
+
MATLAB ``idct(x)``.
|
| 472 |
+
|
| 473 |
+
.. warning:: For ``type in {1, 2, 3}``, ``norm="ortho"`` breaks the direct
|
| 474 |
+
correspondence with the inverse direct Fourier transform. To
|
| 475 |
+
recover it you must specify ``orthogonalize=False``.
|
| 476 |
+
|
| 477 |
+
For ``norm="ortho"`` both the `dct` and `idct` are scaled by the same
|
| 478 |
+
overall factor in both directions. By default, the transform is also
|
| 479 |
+
orthogonalized which for types 1, 2 and 3 means the transform definition is
|
| 480 |
+
modified to give orthogonality of the IDCT matrix (see `dct` for the full
|
| 481 |
+
definitions).
|
| 482 |
+
|
| 483 |
+
'The' IDCT is the IDCT-II, which is the same as the normalized DCT-III.
|
| 484 |
+
|
| 485 |
+
The IDCT is equivalent to a normal DCT except for the normalization and
|
| 486 |
+
type. DCT type 1 and 4 are their own inverse and DCTs 2 and 3 are each
|
| 487 |
+
other's inverses.
|
| 488 |
+
|
| 489 |
+
Examples
|
| 490 |
+
--------
|
| 491 |
+
The Type 1 DCT is equivalent to the DFT for real, even-symmetrical
|
| 492 |
+
inputs. The output is also real and even-symmetrical. Half of the IFFT
|
| 493 |
+
input is used to generate half of the IFFT output:
|
| 494 |
+
|
| 495 |
+
>>> from scipy.fft import ifft, idct
|
| 496 |
+
>>> import numpy as np
|
| 497 |
+
>>> ifft(np.array([ 30., -8., 6., -2., 6., -8.])).real
|
| 498 |
+
array([ 4., 3., 5., 10., 5., 3.])
|
| 499 |
+
>>> idct(np.array([ 30., -8., 6., -2.]), 1)
|
| 500 |
+
array([ 4., 3., 5., 10.])
|
| 501 |
+
|
| 502 |
+
"""
|
| 503 |
+
return (Dispatchable(x, np.ndarray),)
|
| 504 |
+
|
| 505 |
+
|
| 506 |
+
@xp_capabilities(cpu_only=True, allow_dask_compute=True,
|
| 507 |
+
skip_backends=[('jax.numpy', 'XXX large tolerance violations')])
|
| 508 |
+
@_dispatch
|
| 509 |
+
def dst(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False, workers=None,
|
| 510 |
+
orthogonalize=None):
|
| 511 |
+
r"""
|
| 512 |
+
Return the Discrete Sine Transform of arbitrary type sequence x.
|
| 513 |
+
|
| 514 |
+
Parameters
|
| 515 |
+
----------
|
| 516 |
+
x : array_like
|
| 517 |
+
The input array.
|
| 518 |
+
type : {1, 2, 3, 4}, optional
|
| 519 |
+
Type of the DST (see Notes). Default type is 2.
|
| 520 |
+
n : int, optional
|
| 521 |
+
Length of the transform. If ``n < x.shape[axis]``, `x` is
|
| 522 |
+
truncated. If ``n > x.shape[axis]``, `x` is zero-padded. The
|
| 523 |
+
default results in ``n = x.shape[axis]``.
|
| 524 |
+
axis : int, optional
|
| 525 |
+
Axis along which the dst is computed; the default is over the
|
| 526 |
+
last axis (i.e., ``axis=-1``).
|
| 527 |
+
norm : {"backward", "ortho", "forward"}, optional
|
| 528 |
+
Normalization mode (see Notes). Default is "backward".
|
| 529 |
+
overwrite_x : bool, optional
|
| 530 |
+
If True, the contents of `x` can be destroyed; the default is False.
|
| 531 |
+
workers : int, optional
|
| 532 |
+
Maximum number of workers to use for parallel computation. If negative,
|
| 533 |
+
the value wraps around from ``os.cpu_count()``.
|
| 534 |
+
See :func:`~scipy.fft.fft` for more details.
|
| 535 |
+
orthogonalize : bool, optional
|
| 536 |
+
Whether to use the orthogonalized DST variant (see Notes).
|
| 537 |
+
Defaults to ``True`` when ``norm="ortho"`` and ``False`` otherwise.
|
| 538 |
+
|
| 539 |
+
.. versionadded:: 1.8.0
|
| 540 |
+
|
| 541 |
+
Returns
|
| 542 |
+
-------
|
| 543 |
+
dst : ndarray of reals
|
| 544 |
+
The transformed input array.
|
| 545 |
+
|
| 546 |
+
See Also
|
| 547 |
+
--------
|
| 548 |
+
idst : Inverse DST
|
| 549 |
+
|
| 550 |
+
Notes
|
| 551 |
+
-----
|
| 552 |
+
.. warning:: For ``type in {2, 3}``, ``norm="ortho"`` breaks the direct
|
| 553 |
+
correspondence with the direct Fourier transform. To recover
|
| 554 |
+
it you must specify ``orthogonalize=False``.
|
| 555 |
+
|
| 556 |
+
For ``norm="ortho"`` both the `dst` and `idst` are scaled by the same
|
| 557 |
+
overall factor in both directions. By default, the transform is also
|
| 558 |
+
orthogonalized which for types 2 and 3 means the transform definition is
|
| 559 |
+
modified to give orthogonality of the DST matrix (see below).
|
| 560 |
+
|
| 561 |
+
For ``norm="backward"``, there is no scaling on the `dst` and the `idst` is
|
| 562 |
+
scaled by ``1/N`` where ``N`` is the "logical" size of the DST.
|
| 563 |
+
|
| 564 |
+
There are, theoretically, 8 types of the DST for different combinations of
|
| 565 |
+
even/odd boundary conditions and boundary off sets [1]_, only the first
|
| 566 |
+
4 types are implemented in SciPy.
|
| 567 |
+
|
| 568 |
+
**Type I**
|
| 569 |
+
|
| 570 |
+
There are several definitions of the DST-I; we use the following for
|
| 571 |
+
``norm="backward"``. DST-I assumes the input is odd around :math:`n=-1` and
|
| 572 |
+
:math:`n=N`.
|
| 573 |
+
|
| 574 |
+
.. math::
|
| 575 |
+
|
| 576 |
+
y_k = 2 \sum_{n=0}^{N-1} x_n \sin\left(\frac{\pi(k+1)(n+1)}{N+1}\right)
|
| 577 |
+
|
| 578 |
+
Note that the DST-I is only supported for input size > 1.
|
| 579 |
+
The (unnormalized) DST-I is its own inverse, up to a factor :math:`2(N+1)`.
|
| 580 |
+
The orthonormalized DST-I is exactly its own inverse.
|
| 581 |
+
|
| 582 |
+
``orthogonalize`` has no effect here, as the DST-I matrix is already
|
| 583 |
+
orthogonal up to a scale factor of ``2N``.
|
| 584 |
+
|
| 585 |
+
**Type II**
|
| 586 |
+
|
| 587 |
+
There are several definitions of the DST-II; we use the following for
|
| 588 |
+
``norm="backward"``. DST-II assumes the input is odd around :math:`n=-1/2` and
|
| 589 |
+
:math:`n=N-1/2`; the output is odd around :math:`k=-1` and even around :math:`k=N-1`
|
| 590 |
+
|
| 591 |
+
.. math::
|
| 592 |
+
|
| 593 |
+
y_k = 2 \sum_{n=0}^{N-1} x_n \sin\left(\frac{\pi(k+1)(2n+1)}{2N}\right)
|
| 594 |
+
|
| 595 |
+
If ``orthogonalize=True``, ``y[-1]`` is divided :math:`\sqrt{2}` which, when
|
| 596 |
+
combined with ``norm="ortho"``, makes the corresponding matrix of
|
| 597 |
+
coefficients orthonormal (``O @ O.T = np.eye(N)``).
|
| 598 |
+
|
| 599 |
+
**Type III**
|
| 600 |
+
|
| 601 |
+
There are several definitions of the DST-III, we use the following (for
|
| 602 |
+
``norm="backward"``). DST-III assumes the input is odd around :math:`n=-1` and
|
| 603 |
+
even around :math:`n=N-1`
|
| 604 |
+
|
| 605 |
+
.. math::
|
| 606 |
+
|
| 607 |
+
y_k = (-1)^k x_{N-1} + 2 \sum_{n=0}^{N-2} x_n \sin\left(
|
| 608 |
+
\frac{\pi(2k+1)(n+1)}{2N}\right)
|
| 609 |
+
|
| 610 |
+
If ``orthogonalize=True``, ``x[-1]`` is multiplied by :math:`\sqrt{2}`
|
| 611 |
+
which, when combined with ``norm="ortho"``, makes the corresponding matrix
|
| 612 |
+
of coefficients orthonormal (``O @ O.T = np.eye(N)``).
|
| 613 |
+
|
| 614 |
+
The (unnormalized) DST-III is the inverse of the (unnormalized) DST-II, up
|
| 615 |
+
to a factor :math:`2N`. The orthonormalized DST-III is exactly the inverse of the
|
| 616 |
+
orthonormalized DST-II.
|
| 617 |
+
|
| 618 |
+
**Type IV**
|
| 619 |
+
|
| 620 |
+
There are several definitions of the DST-IV, we use the following (for
|
| 621 |
+
``norm="backward"``). DST-IV assumes the input is odd around :math:`n=-0.5` and
|
| 622 |
+
even around :math:`n=N-0.5`
|
| 623 |
+
|
| 624 |
+
.. math::
|
| 625 |
+
|
| 626 |
+
y_k = 2 \sum_{n=0}^{N-1} x_n \sin\left(\frac{\pi(2k+1)(2n+1)}{4N}\right)
|
| 627 |
+
|
| 628 |
+
``orthogonalize`` has no effect here, as the DST-IV matrix is already
|
| 629 |
+
orthogonal up to a scale factor of ``2N``.
|
| 630 |
+
|
| 631 |
+
The (unnormalized) DST-IV is its own inverse, up to a factor :math:`2N`. The
|
| 632 |
+
orthonormalized DST-IV is exactly its own inverse.
|
| 633 |
+
|
| 634 |
+
Examples
|
| 635 |
+
--------
|
| 636 |
+
Compute the DST of a simple 1D array:
|
| 637 |
+
|
| 638 |
+
>>> import numpy as np
|
| 639 |
+
>>> from scipy.fft import dst
|
| 640 |
+
>>> x = np.array([1, -1, 1, -1])
|
| 641 |
+
>>> dst(x, type=2)
|
| 642 |
+
array([0., 0., 0., 8.])
|
| 643 |
+
|
| 644 |
+
This computes the Discrete Sine Transform (DST) of type-II for the input array.
|
| 645 |
+
The output contains the transformed values corresponding to the given input sequence
|
| 646 |
+
|
| 647 |
+
References
|
| 648 |
+
----------
|
| 649 |
+
.. [1] Wikipedia, "Discrete sine transform",
|
| 650 |
+
https://en.wikipedia.org/wiki/Discrete_sine_transform
|
| 651 |
+
|
| 652 |
+
"""
|
| 653 |
+
return (Dispatchable(x, np.ndarray),)
|
| 654 |
+
|
| 655 |
+
|
| 656 |
+
@xp_capabilities(cpu_only=True, allow_dask_compute=True)
|
| 657 |
+
@_dispatch
|
| 658 |
+
def idst(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False,
|
| 659 |
+
workers=None, orthogonalize=None):
|
| 660 |
+
"""
|
| 661 |
+
Return the Inverse Discrete Sine Transform of an arbitrary type sequence.
|
| 662 |
+
|
| 663 |
+
Parameters
|
| 664 |
+
----------
|
| 665 |
+
x : array_like
|
| 666 |
+
The input array.
|
| 667 |
+
type : {1, 2, 3, 4}, optional
|
| 668 |
+
Type of the DST (see Notes). Default type is 2.
|
| 669 |
+
n : int, optional
|
| 670 |
+
Length of the transform. If ``n < x.shape[axis]``, `x` is
|
| 671 |
+
truncated. If ``n > x.shape[axis]``, `x` is zero-padded. The
|
| 672 |
+
default results in ``n = x.shape[axis]``.
|
| 673 |
+
axis : int, optional
|
| 674 |
+
Axis along which the idst is computed; the default is over the
|
| 675 |
+
last axis (i.e., ``axis=-1``).
|
| 676 |
+
norm : {"backward", "ortho", "forward"}, optional
|
| 677 |
+
Normalization mode (see Notes). Default is "backward".
|
| 678 |
+
overwrite_x : bool, optional
|
| 679 |
+
If True, the contents of `x` can be destroyed; the default is False.
|
| 680 |
+
workers : int, optional
|
| 681 |
+
Maximum number of workers to use for parallel computation. If negative,
|
| 682 |
+
the value wraps around from ``os.cpu_count()``.
|
| 683 |
+
See :func:`~scipy.fft.fft` for more details.
|
| 684 |
+
orthogonalize : bool, optional
|
| 685 |
+
Whether to use the orthogonalized IDST variant (see Notes).
|
| 686 |
+
Defaults to ``True`` when ``norm="ortho"`` and ``False`` otherwise.
|
| 687 |
+
|
| 688 |
+
.. versionadded:: 1.8.0
|
| 689 |
+
|
| 690 |
+
Returns
|
| 691 |
+
-------
|
| 692 |
+
idst : ndarray of real
|
| 693 |
+
The transformed input array.
|
| 694 |
+
|
| 695 |
+
See Also
|
| 696 |
+
--------
|
| 697 |
+
dst : Forward DST
|
| 698 |
+
|
| 699 |
+
Notes
|
| 700 |
+
-----
|
| 701 |
+
.. warning:: For ``type in {2, 3}``, ``norm="ortho"`` breaks the direct
|
| 702 |
+
correspondence with the inverse direct Fourier transform.
|
| 703 |
+
|
| 704 |
+
For ``norm="ortho"`` both the `dst` and `idst` are scaled by the same
|
| 705 |
+
overall factor in both directions. By default, the transform is also
|
| 706 |
+
orthogonalized which for types 2 and 3 means the transform definition is
|
| 707 |
+
modified to give orthogonality of the DST matrix (see `dst` for the full
|
| 708 |
+
definitions).
|
| 709 |
+
|
| 710 |
+
'The' IDST is the IDST-II, which is the same as the normalized DST-III.
|
| 711 |
+
|
| 712 |
+
The IDST is equivalent to a normal DST except for the normalization and
|
| 713 |
+
type. DST type 1 and 4 are their own inverse and DSTs 2 and 3 are each
|
| 714 |
+
other's inverses.
|
| 715 |
+
|
| 716 |
+
"""
|
| 717 |
+
return (Dispatchable(x, np.ndarray),)
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/_realtransforms_backend.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from scipy._lib._array_api import array_namespace
|
| 2 |
+
import numpy as np
|
| 3 |
+
from . import _pocketfft
|
| 4 |
+
|
| 5 |
+
__all__ = ['dct', 'idct', 'dst', 'idst', 'dctn', 'idctn', 'dstn', 'idstn']
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def _execute(pocketfft_func, x, type, s, axes, norm,
|
| 9 |
+
overwrite_x, workers, orthogonalize):
|
| 10 |
+
xp = array_namespace(x)
|
| 11 |
+
x = np.asarray(x)
|
| 12 |
+
y = pocketfft_func(x, type, s, axes, norm,
|
| 13 |
+
overwrite_x=overwrite_x, workers=workers,
|
| 14 |
+
orthogonalize=orthogonalize)
|
| 15 |
+
return xp.asarray(y)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def dctn(x, type=2, s=None, axes=None, norm=None,
|
| 19 |
+
overwrite_x=False, workers=None, *, orthogonalize=None):
|
| 20 |
+
return _execute(_pocketfft.dctn, x, type, s, axes, norm,
|
| 21 |
+
overwrite_x, workers, orthogonalize)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def idctn(x, type=2, s=None, axes=None, norm=None,
|
| 25 |
+
overwrite_x=False, workers=None, *, orthogonalize=None):
|
| 26 |
+
return _execute(_pocketfft.idctn, x, type, s, axes, norm,
|
| 27 |
+
overwrite_x, workers, orthogonalize)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def dstn(x, type=2, s=None, axes=None, norm=None,
|
| 31 |
+
overwrite_x=False, workers=None, orthogonalize=None):
|
| 32 |
+
return _execute(_pocketfft.dstn, x, type, s, axes, norm,
|
| 33 |
+
overwrite_x, workers, orthogonalize)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def idstn(x, type=2, s=None, axes=None, norm=None,
|
| 37 |
+
overwrite_x=False, workers=None, *, orthogonalize=None):
|
| 38 |
+
return _execute(_pocketfft.idstn, x, type, s, axes, norm,
|
| 39 |
+
overwrite_x, workers, orthogonalize)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def dct(x, type=2, n=None, axis=-1, norm=None,
|
| 43 |
+
overwrite_x=False, workers=None, orthogonalize=None):
|
| 44 |
+
return _execute(_pocketfft.dct, x, type, n, axis, norm,
|
| 45 |
+
overwrite_x, workers, orthogonalize)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def idct(x, type=2, n=None, axis=-1, norm=None,
|
| 49 |
+
overwrite_x=False, workers=None, orthogonalize=None):
|
| 50 |
+
return _execute(_pocketfft.idct, x, type, n, axis, norm,
|
| 51 |
+
overwrite_x, workers, orthogonalize)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def dst(x, type=2, n=None, axis=-1, norm=None,
|
| 55 |
+
overwrite_x=False, workers=None, orthogonalize=None):
|
| 56 |
+
return _execute(_pocketfft.dst, x, type, n, axis, norm,
|
| 57 |
+
overwrite_x, workers, orthogonalize)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def idst(x, type=2, n=None, axis=-1, norm=None,
|
| 61 |
+
overwrite_x=False, workers=None, orthogonalize=None):
|
| 62 |
+
return _execute(_pocketfft.idst, x, type, n, axis, norm,
|
| 63 |
+
overwrite_x, workers, orthogonalize)
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/tests/__init__.py
ADDED
|
File without changes
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/tests/mock_backend.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import scipy.fft
|
| 3 |
+
import threading
|
| 4 |
+
|
| 5 |
+
class _MockFunction:
|
| 6 |
+
def __init__(self, return_value = None):
|
| 7 |
+
self.number_calls = threading.local()
|
| 8 |
+
self.return_value = return_value
|
| 9 |
+
self.last_args = threading.local()
|
| 10 |
+
|
| 11 |
+
def __call__(self, *args, **kwargs):
|
| 12 |
+
if not hasattr(self.number_calls, 'c'):
|
| 13 |
+
self.number_calls.c = 0
|
| 14 |
+
|
| 15 |
+
self.number_calls.c += 1
|
| 16 |
+
self.last_args.l = (args, kwargs)
|
| 17 |
+
return self.return_value
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
fft = _MockFunction(np.random.random(10))
|
| 21 |
+
fft2 = _MockFunction(np.random.random(10))
|
| 22 |
+
fftn = _MockFunction(np.random.random(10))
|
| 23 |
+
|
| 24 |
+
ifft = _MockFunction(np.random.random(10))
|
| 25 |
+
ifft2 = _MockFunction(np.random.random(10))
|
| 26 |
+
ifftn = _MockFunction(np.random.random(10))
|
| 27 |
+
|
| 28 |
+
rfft = _MockFunction(np.random.random(10))
|
| 29 |
+
rfft2 = _MockFunction(np.random.random(10))
|
| 30 |
+
rfftn = _MockFunction(np.random.random(10))
|
| 31 |
+
|
| 32 |
+
irfft = _MockFunction(np.random.random(10))
|
| 33 |
+
irfft2 = _MockFunction(np.random.random(10))
|
| 34 |
+
irfftn = _MockFunction(np.random.random(10))
|
| 35 |
+
|
| 36 |
+
hfft = _MockFunction(np.random.random(10))
|
| 37 |
+
hfft2 = _MockFunction(np.random.random(10))
|
| 38 |
+
hfftn = _MockFunction(np.random.random(10))
|
| 39 |
+
|
| 40 |
+
ihfft = _MockFunction(np.random.random(10))
|
| 41 |
+
ihfft2 = _MockFunction(np.random.random(10))
|
| 42 |
+
ihfftn = _MockFunction(np.random.random(10))
|
| 43 |
+
|
| 44 |
+
dct = _MockFunction(np.random.random(10))
|
| 45 |
+
idct = _MockFunction(np.random.random(10))
|
| 46 |
+
dctn = _MockFunction(np.random.random(10))
|
| 47 |
+
idctn = _MockFunction(np.random.random(10))
|
| 48 |
+
|
| 49 |
+
dst = _MockFunction(np.random.random(10))
|
| 50 |
+
idst = _MockFunction(np.random.random(10))
|
| 51 |
+
dstn = _MockFunction(np.random.random(10))
|
| 52 |
+
idstn = _MockFunction(np.random.random(10))
|
| 53 |
+
|
| 54 |
+
fht = _MockFunction(np.random.random(10))
|
| 55 |
+
ifht = _MockFunction(np.random.random(10))
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
__ua_domain__ = "numpy.scipy.fft"
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
_implements = {
|
| 62 |
+
scipy.fft.fft: fft,
|
| 63 |
+
scipy.fft.fft2: fft2,
|
| 64 |
+
scipy.fft.fftn: fftn,
|
| 65 |
+
scipy.fft.ifft: ifft,
|
| 66 |
+
scipy.fft.ifft2: ifft2,
|
| 67 |
+
scipy.fft.ifftn: ifftn,
|
| 68 |
+
scipy.fft.rfft: rfft,
|
| 69 |
+
scipy.fft.rfft2: rfft2,
|
| 70 |
+
scipy.fft.rfftn: rfftn,
|
| 71 |
+
scipy.fft.irfft: irfft,
|
| 72 |
+
scipy.fft.irfft2: irfft2,
|
| 73 |
+
scipy.fft.irfftn: irfftn,
|
| 74 |
+
scipy.fft.hfft: hfft,
|
| 75 |
+
scipy.fft.hfft2: hfft2,
|
| 76 |
+
scipy.fft.hfftn: hfftn,
|
| 77 |
+
scipy.fft.ihfft: ihfft,
|
| 78 |
+
scipy.fft.ihfft2: ihfft2,
|
| 79 |
+
scipy.fft.ihfftn: ihfftn,
|
| 80 |
+
scipy.fft.dct: dct,
|
| 81 |
+
scipy.fft.idct: idct,
|
| 82 |
+
scipy.fft.dctn: dctn,
|
| 83 |
+
scipy.fft.idctn: idctn,
|
| 84 |
+
scipy.fft.dst: dst,
|
| 85 |
+
scipy.fft.idst: idst,
|
| 86 |
+
scipy.fft.dstn: dstn,
|
| 87 |
+
scipy.fft.idstn: idstn,
|
| 88 |
+
scipy.fft.fht: fht,
|
| 89 |
+
scipy.fft.ifht: ifht
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
def __ua_function__(method, args, kwargs):
|
| 94 |
+
fn = _implements.get(method)
|
| 95 |
+
return (fn(*args, **kwargs) if fn is not None
|
| 96 |
+
else NotImplemented)
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/tests/test_backend.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from functools import partial
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
import scipy.fft
|
| 5 |
+
from scipy.fft import _fftlog, _pocketfft, set_backend
|
| 6 |
+
from scipy.fft.tests import mock_backend
|
| 7 |
+
|
| 8 |
+
from numpy.testing import assert_allclose, assert_equal
|
| 9 |
+
import pytest
|
| 10 |
+
|
| 11 |
+
fnames = ('fft', 'fft2', 'fftn',
|
| 12 |
+
'ifft', 'ifft2', 'ifftn',
|
| 13 |
+
'rfft', 'rfft2', 'rfftn',
|
| 14 |
+
'irfft', 'irfft2', 'irfftn',
|
| 15 |
+
'dct', 'idct', 'dctn', 'idctn',
|
| 16 |
+
'dst', 'idst', 'dstn', 'idstn',
|
| 17 |
+
'fht', 'ifht')
|
| 18 |
+
|
| 19 |
+
np_funcs = (np.fft.fft, np.fft.fft2, np.fft.fftn,
|
| 20 |
+
np.fft.ifft, np.fft.ifft2, np.fft.ifftn,
|
| 21 |
+
np.fft.rfft, np.fft.rfft2, np.fft.rfftn,
|
| 22 |
+
np.fft.irfft, np.fft.irfft2, np.fft.irfftn,
|
| 23 |
+
np.fft.hfft, _pocketfft.hfft2, _pocketfft.hfftn, # np has no hfftn
|
| 24 |
+
np.fft.ihfft, _pocketfft.ihfft2, _pocketfft.ihfftn,
|
| 25 |
+
_pocketfft.dct, _pocketfft.idct, _pocketfft.dctn, _pocketfft.idctn,
|
| 26 |
+
_pocketfft.dst, _pocketfft.idst, _pocketfft.dstn, _pocketfft.idstn,
|
| 27 |
+
# must provide required kwargs for fht, ifht
|
| 28 |
+
partial(_fftlog.fht, dln=2, mu=0.5),
|
| 29 |
+
partial(_fftlog.ifht, dln=2, mu=0.5))
|
| 30 |
+
|
| 31 |
+
funcs = (scipy.fft.fft, scipy.fft.fft2, scipy.fft.fftn,
|
| 32 |
+
scipy.fft.ifft, scipy.fft.ifft2, scipy.fft.ifftn,
|
| 33 |
+
scipy.fft.rfft, scipy.fft.rfft2, scipy.fft.rfftn,
|
| 34 |
+
scipy.fft.irfft, scipy.fft.irfft2, scipy.fft.irfftn,
|
| 35 |
+
scipy.fft.hfft, scipy.fft.hfft2, scipy.fft.hfftn,
|
| 36 |
+
scipy.fft.ihfft, scipy.fft.ihfft2, scipy.fft.ihfftn,
|
| 37 |
+
scipy.fft.dct, scipy.fft.idct, scipy.fft.dctn, scipy.fft.idctn,
|
| 38 |
+
scipy.fft.dst, scipy.fft.idst, scipy.fft.dstn, scipy.fft.idstn,
|
| 39 |
+
# must provide required kwargs for fht, ifht
|
| 40 |
+
partial(scipy.fft.fht, dln=2, mu=0.5),
|
| 41 |
+
partial(scipy.fft.ifht, dln=2, mu=0.5))
|
| 42 |
+
|
| 43 |
+
mocks = (mock_backend.fft, mock_backend.fft2, mock_backend.fftn,
|
| 44 |
+
mock_backend.ifft, mock_backend.ifft2, mock_backend.ifftn,
|
| 45 |
+
mock_backend.rfft, mock_backend.rfft2, mock_backend.rfftn,
|
| 46 |
+
mock_backend.irfft, mock_backend.irfft2, mock_backend.irfftn,
|
| 47 |
+
mock_backend.hfft, mock_backend.hfft2, mock_backend.hfftn,
|
| 48 |
+
mock_backend.ihfft, mock_backend.ihfft2, mock_backend.ihfftn,
|
| 49 |
+
mock_backend.dct, mock_backend.idct,
|
| 50 |
+
mock_backend.dctn, mock_backend.idctn,
|
| 51 |
+
mock_backend.dst, mock_backend.idst,
|
| 52 |
+
mock_backend.dstn, mock_backend.idstn,
|
| 53 |
+
mock_backend.fht, mock_backend.ifht)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
@pytest.mark.parametrize("func, np_func, mock", zip(funcs, np_funcs, mocks))
|
| 57 |
+
def test_backend_call(func, np_func, mock):
|
| 58 |
+
x = np.arange(20).reshape((10,2))
|
| 59 |
+
answer = np_func(x.astype(np.float64))
|
| 60 |
+
assert_allclose(func(x), answer, atol=1e-10)
|
| 61 |
+
|
| 62 |
+
with set_backend(mock_backend, only=True):
|
| 63 |
+
mock.number_calls.c = 0
|
| 64 |
+
y = func(x)
|
| 65 |
+
assert_equal(y, mock.return_value)
|
| 66 |
+
assert_equal(mock.number_calls.c, 1)
|
| 67 |
+
|
| 68 |
+
assert_allclose(func(x), answer, atol=1e-10)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
plan_funcs = (scipy.fft.fft, scipy.fft.fft2, scipy.fft.fftn,
|
| 72 |
+
scipy.fft.ifft, scipy.fft.ifft2, scipy.fft.ifftn,
|
| 73 |
+
scipy.fft.rfft, scipy.fft.rfft2, scipy.fft.rfftn,
|
| 74 |
+
scipy.fft.irfft, scipy.fft.irfft2, scipy.fft.irfftn,
|
| 75 |
+
scipy.fft.hfft, scipy.fft.hfft2, scipy.fft.hfftn,
|
| 76 |
+
scipy.fft.ihfft, scipy.fft.ihfft2, scipy.fft.ihfftn)
|
| 77 |
+
|
| 78 |
+
plan_mocks = (mock_backend.fft, mock_backend.fft2, mock_backend.fftn,
|
| 79 |
+
mock_backend.ifft, mock_backend.ifft2, mock_backend.ifftn,
|
| 80 |
+
mock_backend.rfft, mock_backend.rfft2, mock_backend.rfftn,
|
| 81 |
+
mock_backend.irfft, mock_backend.irfft2, mock_backend.irfftn,
|
| 82 |
+
mock_backend.hfft, mock_backend.hfft2, mock_backend.hfftn,
|
| 83 |
+
mock_backend.ihfft, mock_backend.ihfft2, mock_backend.ihfftn)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
@pytest.mark.parametrize("func, mock", zip(plan_funcs, plan_mocks))
|
| 87 |
+
def test_backend_plan(func, mock):
|
| 88 |
+
x = np.arange(20).reshape((10, 2))
|
| 89 |
+
|
| 90 |
+
with pytest.raises(NotImplementedError, match='precomputed plan'):
|
| 91 |
+
func(x, plan='foo')
|
| 92 |
+
|
| 93 |
+
with set_backend(mock_backend, only=True):
|
| 94 |
+
mock.number_calls.c = 0
|
| 95 |
+
y = func(x, plan='foo')
|
| 96 |
+
assert_equal(y, mock.return_value)
|
| 97 |
+
assert_equal(mock.number_calls.c, 1)
|
| 98 |
+
assert_equal(mock.last_args.l[1]['plan'], 'fo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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import queue
|
| 2 |
+
import threading
|
| 3 |
+
import multiprocessing
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pytest
|
| 6 |
+
from numpy.random import random
|
| 7 |
+
from numpy.testing import assert_array_almost_equal, assert_allclose
|
| 8 |
+
from pytest import raises as assert_raises
|
| 9 |
+
import scipy.fft as fft
|
| 10 |
+
from scipy._lib._array_api import (
|
| 11 |
+
is_numpy, xp_size, xp_assert_close, xp_assert_equal, make_xp_test_case,
|
| 12 |
+
make_xp_pytest_param
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
lazy_xp_modules = [fft]
|
| 16 |
+
skip_xp_backends = pytest.mark.skip_xp_backends
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Expected input dtypes. Note that `scipy.fft` is more flexible for numpy,
|
| 20 |
+
# but for C2C transforms like `fft.fft`, the array API standard only mandates
|
| 21 |
+
# that complex dtypes should work, float32/float64 aren't guaranteed to.
|
| 22 |
+
def get_expected_input_dtype(func, xp):
|
| 23 |
+
# use __name__ so that `lazy_xp_function` doesn't break things
|
| 24 |
+
if func.__name__ in ["fft", "fftn", "fft2", "ifft", "ifftn", "ifft2", "hfft",
|
| 25 |
+
"hfftn", "hfft2", "irfft", "irfftn", "irfft2"]:
|
| 26 |
+
dtype = xp.complex128
|
| 27 |
+
elif func.__name__ in ["rfft", "rfftn", "rfft2", "ihfft", "ihfftn", "ihfft2"]:
|
| 28 |
+
dtype = xp.float64
|
| 29 |
+
else:
|
| 30 |
+
raise ValueError(f'Unknown FFT function: {func}')
|
| 31 |
+
|
| 32 |
+
return dtype
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def fft1(x):
|
| 36 |
+
L = len(x)
|
| 37 |
+
phase = -2j*np.pi*(np.arange(L)/float(L))
|
| 38 |
+
phase = np.arange(L).reshape(-1, 1) * phase
|
| 39 |
+
return np.sum(x*np.exp(phase), axis=1)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
class TestFFT:
|
| 43 |
+
@make_xp_test_case(fft.ifft, fft.fft, fft.rfft, fft.irfft)
|
| 44 |
+
def test_identity(self, xp):
|
| 45 |
+
maxlen = 512
|
| 46 |
+
x = xp.asarray(random(maxlen) + 1j*random(maxlen))
|
| 47 |
+
xr = xp.asarray(random(maxlen))
|
| 48 |
+
# Check some powers of 2 and some primes
|
| 49 |
+
for i in [1, 2, 16, 128, 512, 53, 149, 281, 397]:
|
| 50 |
+
xp_assert_close(fft.ifft(fft.fft(x[0:i])), x[0:i])
|
| 51 |
+
xp_assert_close(fft.irfft(fft.rfft(xr[0:i]), i), xr[0:i])
|
| 52 |
+
|
| 53 |
+
@skip_xp_backends(np_only=True, reason='significant overhead for some backends')
|
| 54 |
+
def test_identity_extensive(self, xp):
|
| 55 |
+
maxlen = 512
|
| 56 |
+
x = xp.asarray(random(maxlen) + 1j*random(maxlen))
|
| 57 |
+
xr = xp.asarray(random(maxlen))
|
| 58 |
+
for i in range(1, maxlen):
|
| 59 |
+
xp_assert_close(fft.ifft(fft.fft(x[0:i])), x[0:i])
|
| 60 |
+
xp_assert_close(fft.irfft(fft.rfft(xr[0:i]), i), xr[0:i])
|
| 61 |
+
|
| 62 |
+
@make_xp_test_case(fft.fft)
|
| 63 |
+
def test_fft(self, xp):
|
| 64 |
+
x = random(30) + 1j*random(30)
|
| 65 |
+
expect = xp.asarray(fft1(x))
|
| 66 |
+
x = xp.asarray(x)
|
| 67 |
+
xp_assert_close(fft.fft(x), expect)
|
| 68 |
+
xp_assert_close(fft.fft(x, norm="backward"), expect)
|
| 69 |
+
xp_assert_close(fft.fft(x, norm="ortho"),
|
| 70 |
+
expect / xp.sqrt(xp.asarray(30, dtype=xp.float64)),)
|
| 71 |
+
xp_assert_close(fft.fft(x, norm="forward"), expect / 30)
|
| 72 |
+
|
| 73 |
+
@skip_xp_backends(np_only=True, reason='some backends allow `n=0`')
|
| 74 |
+
def test_fft_n(self, xp):
|
| 75 |
+
x = xp.asarray([1, 2, 3], dtype=xp.complex128)
|
| 76 |
+
assert_raises(ValueError, fft.fft, x, 0)
|
| 77 |
+
|
| 78 |
+
@make_xp_test_case(fft.fft, fft.ifft)
|
| 79 |
+
def test_ifft(self, xp):
|
| 80 |
+
x = xp.asarray(random(30) + 1j*random(30))
|
| 81 |
+
xp_assert_close(fft.ifft(fft.fft(x)), x)
|
| 82 |
+
for norm in ["backward", "ortho", "forward"]:
|
| 83 |
+
xp_assert_close(fft.ifft(fft.fft(x, norm=norm), norm=norm), x)
|
| 84 |
+
|
| 85 |
+
@make_xp_test_case(fft.fft, fft.fft2)
|
| 86 |
+
def test_fft2(self, xp):
|
| 87 |
+
x = xp.asarray(random((30, 20)) + 1j*random((30, 20)))
|
| 88 |
+
expect = fft.fft(fft.fft(x, axis=1), axis=0)
|
| 89 |
+
xp_assert_close(fft.fft2(x), expect)
|
| 90 |
+
xp_assert_close(fft.fft2(x, norm="backward"), expect)
|
| 91 |
+
xp_assert_close(fft.fft2(x, norm="ortho"),
|
| 92 |
+
expect / xp.sqrt(xp.asarray(30 * 20, dtype=xp.float64)))
|
| 93 |
+
xp_assert_close(fft.fft2(x, norm="forward"), expect / (30 * 20))
|
| 94 |
+
|
| 95 |
+
@make_xp_test_case(fft.ifft, fft.ifft2)
|
| 96 |
+
def test_ifft2(self, xp):
|
| 97 |
+
x = xp.asarray(random((30, 20)) + 1j*random((30, 20)))
|
| 98 |
+
expect = fft.ifft(fft.ifft(x, axis=1), axis=0)
|
| 99 |
+
xp_assert_close(fft.ifft2(x), expect)
|
| 100 |
+
xp_assert_close(fft.ifft2(x, norm="backward"), expect)
|
| 101 |
+
xp_assert_close(fft.ifft2(x, norm="ortho"),
|
| 102 |
+
expect * xp.sqrt(xp.asarray(30 * 20, dtype=xp.float64)))
|
| 103 |
+
xp_assert_close(fft.ifft2(x, norm="forward"), expect * (30 * 20))
|
| 104 |
+
|
| 105 |
+
@make_xp_test_case(fft.fft, fft.fftn)
|
| 106 |
+
def test_fftn(self, xp):
|
| 107 |
+
x = xp.asarray(random((30, 20, 10)) + 1j*random((30, 20, 10)))
|
| 108 |
+
expect = fft.fft(fft.fft(fft.fft(x, axis=2), axis=1), axis=0)
|
| 109 |
+
xp_assert_close(fft.fftn(x), expect)
|
| 110 |
+
xp_assert_close(fft.fftn(x, norm="backward"), expect)
|
| 111 |
+
xp_assert_close(fft.fftn(x, norm="ortho"),
|
| 112 |
+
expect / xp.sqrt(xp.asarray(30 * 20 * 10, dtype=xp.float64)))
|
| 113 |
+
xp_assert_close(fft.fftn(x, norm="forward"), expect / (30 * 20 * 10))
|
| 114 |
+
|
| 115 |
+
@make_xp_test_case(fft.ifft, fft.ifftn)
|
| 116 |
+
def test_ifftn(self, xp):
|
| 117 |
+
x = xp.asarray(random((30, 20, 10)) + 1j*random((30, 20, 10)))
|
| 118 |
+
expect = fft.ifft(fft.ifft(fft.ifft(x, axis=2), axis=1), axis=0)
|
| 119 |
+
xp_assert_close(fft.ifftn(x), expect, rtol=1e-7)
|
| 120 |
+
xp_assert_close(fft.ifftn(x, norm="backward"), expect, rtol=1e-7)
|
| 121 |
+
xp_assert_close(
|
| 122 |
+
fft.ifftn(x, norm="ortho"),
|
| 123 |
+
fft.ifftn(x) * xp.sqrt(xp.asarray(30 * 20 * 10, dtype=xp.float64))
|
| 124 |
+
)
|
| 125 |
+
xp_assert_close(fft.ifftn(x, norm="forward"),
|
| 126 |
+
expect * (30 * 20 * 10),
|
| 127 |
+
rtol=1e-7)
|
| 128 |
+
|
| 129 |
+
@make_xp_test_case(fft.fft, fft.rfft)
|
| 130 |
+
def test_rfft(self, xp):
|
| 131 |
+
x = xp.asarray(random(29), dtype=xp.float64)
|
| 132 |
+
for n in [xp_size(x), 2*xp_size(x)]:
|
| 133 |
+
for norm in [None, "backward", "ortho", "forward"]:
|
| 134 |
+
xp_assert_close(fft.rfft(x, n=n, norm=norm),
|
| 135 |
+
fft.fft(xp.asarray(x, dtype=xp.complex128),
|
| 136 |
+
n=n, norm=norm)[:(n//2 + 1)])
|
| 137 |
+
xp_assert_close(
|
| 138 |
+
fft.rfft(x, n=n, norm="ortho"),
|
| 139 |
+
fft.rfft(x, n=n) / xp.sqrt(xp.asarray(n, dtype=xp.float64))
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
@make_xp_test_case(fft.irfft, fft.rfft)
|
| 143 |
+
def test_irfft(self, xp):
|
| 144 |
+
x = xp.asarray(random(30))
|
| 145 |
+
xp_assert_close(fft.irfft(fft.rfft(x)), x)
|
| 146 |
+
for norm in ["backward", "ortho", "forward"]:
|
| 147 |
+
xp_assert_close(fft.irfft(fft.rfft(x, norm=norm), norm=norm), x)
|
| 148 |
+
|
| 149 |
+
@make_xp_test_case(fft.rfft2)
|
| 150 |
+
def test_rfft2(self, xp):
|
| 151 |
+
x = xp.asarray(random((30, 20)), dtype=xp.float64)
|
| 152 |
+
expect = fft.fft2(xp.asarray(x, dtype=xp.complex128))[:, :11]
|
| 153 |
+
xp_assert_close(fft.rfft2(x), expect)
|
| 154 |
+
xp_assert_close(fft.rfft2(x, norm="backward"), expect)
|
| 155 |
+
xp_assert_close(fft.rfft2(x, norm="ortho"),
|
| 156 |
+
expect / xp.sqrt(xp.asarray(30 * 20, dtype=xp.float64)))
|
| 157 |
+
xp_assert_close(fft.rfft2(x, norm="forward"), expect / (30 * 20))
|
| 158 |
+
|
| 159 |
+
@make_xp_test_case(fft.rfft2, fft.irfft2)
|
| 160 |
+
def test_irfft2(self, xp):
|
| 161 |
+
x = xp.asarray(random((30, 20)))
|
| 162 |
+
xp_assert_close(fft.irfft2(fft.rfft2(x)), x)
|
| 163 |
+
for norm in ["backward", "ortho", "forward"]:
|
| 164 |
+
xp_assert_close(fft.irfft2(fft.rfft2(x, norm=norm), norm=norm), x)
|
| 165 |
+
|
| 166 |
+
@make_xp_test_case(fft.fftn, fft.rfftn)
|
| 167 |
+
def test_rfftn(self, xp):
|
| 168 |
+
x = xp.asarray(random((30, 20, 10)), dtype=xp.float64)
|
| 169 |
+
expect = fft.fftn(xp.asarray(x, dtype=xp.complex128))[:, :, :6]
|
| 170 |
+
xp_assert_close(fft.rfftn(x), expect)
|
| 171 |
+
xp_assert_close(fft.rfftn(x, norm="backward"), expect)
|
| 172 |
+
xp_assert_close(fft.rfftn(x, norm="ortho"),
|
| 173 |
+
expect / xp.sqrt(xp.asarray(30 * 20 * 10, dtype=xp.float64)))
|
| 174 |
+
xp_assert_close(fft.rfftn(x, norm="forward"), expect / (30 * 20 * 10))
|
| 175 |
+
|
| 176 |
+
@make_xp_test_case(fft.irfftn, fft.rfftn)
|
| 177 |
+
def test_irfftn(self, xp):
|
| 178 |
+
x = xp.asarray(random((30, 20, 10)))
|
| 179 |
+
xp_assert_close(fft.irfftn(fft.rfftn(x)), x)
|
| 180 |
+
for norm in ["backward", "ortho", "forward"]:
|
| 181 |
+
xp_assert_close(fft.irfftn(fft.rfftn(x, norm=norm), norm=norm), x)
|
| 182 |
+
|
| 183 |
+
@make_xp_test_case(fft.hfft, fft.fft)
|
| 184 |
+
def test_hfft(self, xp):
|
| 185 |
+
x = random(14) + 1j*random(14)
|
| 186 |
+
x_herm = np.concatenate((random(1), x, random(1)))
|
| 187 |
+
x = np.concatenate((x_herm, x[::-1].conj()))
|
| 188 |
+
x = xp.asarray(x)
|
| 189 |
+
x_herm = xp.asarray(x_herm)
|
| 190 |
+
expect = xp.real(fft.fft(x))
|
| 191 |
+
xp_assert_close(fft.hfft(x_herm), expect)
|
| 192 |
+
xp_assert_close(fft.hfft(x_herm, norm="backward"), expect)
|
| 193 |
+
xp_assert_close(fft.hfft(x_herm, norm="ortho"),
|
| 194 |
+
expect / xp.sqrt(xp.asarray(30, dtype=xp.float64)))
|
| 195 |
+
xp_assert_close(fft.hfft(x_herm, norm="forward"), expect / 30)
|
| 196 |
+
|
| 197 |
+
@make_xp_test_case(fft.hfft, fft.ihfft)
|
| 198 |
+
def test_ihfft(self, xp):
|
| 199 |
+
x = random(14) + 1j*random(14)
|
| 200 |
+
x_herm = np.concatenate((random(1), x, random(1)))
|
| 201 |
+
x = np.concatenate((x_herm, x[::-1].conj()))
|
| 202 |
+
x = xp.asarray(x)
|
| 203 |
+
x_herm = xp.asarray(x_herm)
|
| 204 |
+
xp_assert_close(fft.ihfft(fft.hfft(x_herm)), x_herm)
|
| 205 |
+
for norm in ["backward", "ortho", "forward"]:
|
| 206 |
+
xp_assert_close(fft.ihfft(fft.hfft(x_herm, norm=norm), norm=norm), x_herm)
|
| 207 |
+
|
| 208 |
+
@make_xp_test_case(fft.hfft2, fft.ihfft2)
|
| 209 |
+
def test_hfft2(self, xp):
|
| 210 |
+
x = xp.asarray(random((30, 20)))
|
| 211 |
+
xp_assert_close(fft.hfft2(fft.ihfft2(x)), x)
|
| 212 |
+
for norm in ["backward", "ortho", "forward"]:
|
| 213 |
+
xp_assert_close(fft.hfft2(fft.ihfft2(x, norm=norm), norm=norm), x)
|
| 214 |
+
|
| 215 |
+
@make_xp_test_case(fft.ifft2)
|
| 216 |
+
def test_ihfft2(self, xp):
|
| 217 |
+
x = xp.asarray(random((30, 20)), dtype=xp.float64)
|
| 218 |
+
expect = fft.ifft2(xp.asarray(x, dtype=xp.complex128))[:, :11]
|
| 219 |
+
xp_assert_close(fft.ihfft2(x), expect)
|
| 220 |
+
xp_assert_close(fft.ihfft2(x, norm="backward"), expect)
|
| 221 |
+
xp_assert_close(
|
| 222 |
+
fft.ihfft2(x, norm="ortho"),
|
| 223 |
+
expect * xp.sqrt(xp.asarray(30 * 20, dtype=xp.float64))
|
| 224 |
+
)
|
| 225 |
+
xp_assert_close(fft.ihfft2(x, norm="forward"), expect * (30 * 20))
|
| 226 |
+
|
| 227 |
+
@make_xp_test_case(fft.hfftn, fft.ihfftn)
|
| 228 |
+
def test_hfftn(self, xp):
|
| 229 |
+
x = xp.asarray(random((30, 20, 10)))
|
| 230 |
+
xp_assert_close(fft.hfftn(fft.ihfftn(x)), x)
|
| 231 |
+
for norm in ["backward", "ortho", "forward"]:
|
| 232 |
+
xp_assert_close(fft.hfftn(fft.ihfftn(x, norm=norm), norm=norm), x)
|
| 233 |
+
|
| 234 |
+
@make_xp_test_case(fft.ifftn, fft.ihfftn)
|
| 235 |
+
def test_ihfftn(self, xp):
|
| 236 |
+
x = xp.asarray(random((30, 20, 10)), dtype=xp.float64)
|
| 237 |
+
expect = fft.ifftn(xp.asarray(x, dtype=xp.complex128))[:, :, :6]
|
| 238 |
+
xp_assert_close(expect, fft.ihfftn(x))
|
| 239 |
+
xp_assert_close(expect, fft.ihfftn(x, norm="backward"))
|
| 240 |
+
xp_assert_close(
|
| 241 |
+
fft.ihfftn(x, norm="ortho"),
|
| 242 |
+
expect * xp.sqrt(xp.asarray(30 * 20 * 10, dtype=xp.float64))
|
| 243 |
+
)
|
| 244 |
+
xp_assert_close(fft.ihfftn(x, norm="forward"), expect * (30 * 20 * 10))
|
| 245 |
+
|
| 246 |
+
def _check_axes(self, op, xp):
|
| 247 |
+
dtype = get_expected_input_dtype(op, xp)
|
| 248 |
+
x = xp.asarray(random((30, 20, 10)), dtype=dtype)
|
| 249 |
+
axes = [(0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0)]
|
| 250 |
+
|
| 251 |
+
for a in axes:
|
| 252 |
+
op_tr = op(xp.permute_dims(x, axes=a))
|
| 253 |
+
tr_op = xp.permute_dims(op(x, axes=a), axes=a)
|
| 254 |
+
xp_assert_close(op_tr, tr_op)
|
| 255 |
+
|
| 256 |
+
@pytest.mark.parametrize("op", [make_xp_pytest_param(fft.fftn),
|
| 257 |
+
make_xp_pytest_param(fft.ifftn),
|
| 258 |
+
make_xp_pytest_param(fft.rfftn),
|
| 259 |
+
make_xp_pytest_param(fft.irfftn)])
|
| 260 |
+
def test_axes_standard(self, op, xp):
|
| 261 |
+
self._check_axes(op, xp)
|
| 262 |
+
|
| 263 |
+
@pytest.mark.parametrize("op", [make_xp_pytest_param(fft.hfftn),
|
| 264 |
+
make_xp_pytest_param(fft.ihfftn)])
|
| 265 |
+
def test_axes_non_standard(self, op, xp):
|
| 266 |
+
self._check_axes(op, xp)
|
| 267 |
+
|
| 268 |
+
@pytest.mark.parametrize("op", [make_xp_pytest_param(fft.fftn),
|
| 269 |
+
make_xp_pytest_param(fft.ifftn),
|
| 270 |
+
make_xp_pytest_param(fft.rfftn),
|
| 271 |
+
make_xp_pytest_param(fft.irfftn)])
|
| 272 |
+
def test_axes_subset_with_shape_standard(self, op, xp):
|
| 273 |
+
dtype = get_expected_input_dtype(op, xp)
|
| 274 |
+
x = xp.asarray(random((16, 8, 4)), dtype=dtype)
|
| 275 |
+
axes = [(0, 1, 2), (0, 2, 1), (1, 2, 0)]
|
| 276 |
+
|
| 277 |
+
for a in axes:
|
| 278 |
+
# different shape on the first two axes
|
| 279 |
+
shape = tuple([2*x.shape[ax] if ax in a[:2] else x.shape[ax]
|
| 280 |
+
for ax in range(x.ndim)])
|
| 281 |
+
# transform only the first two axes
|
| 282 |
+
op_tr = op(xp.permute_dims(x, axes=a),
|
| 283 |
+
s=shape[:2], axes=(0, 1))
|
| 284 |
+
tr_op = xp.permute_dims(op(x, s=shape[:2], axes=a[:2]),
|
| 285 |
+
axes=a)
|
| 286 |
+
xp_assert_close(op_tr, tr_op)
|
| 287 |
+
|
| 288 |
+
@pytest.mark.parametrize("op", [make_xp_pytest_param(fft.fft2),
|
| 289 |
+
make_xp_pytest_param(fft.ifft2),
|
| 290 |
+
make_xp_pytest_param(fft.rfft2),
|
| 291 |
+
make_xp_pytest_param(fft.irfft2),
|
| 292 |
+
make_xp_pytest_param(fft.hfft2),
|
| 293 |
+
make_xp_pytest_param(fft.ihfft2),
|
| 294 |
+
make_xp_pytest_param(fft.hfftn),
|
| 295 |
+
make_xp_pytest_param(fft.ihfftn)])
|
| 296 |
+
def test_axes_subset_with_shape_non_standard(self, op, xp):
|
| 297 |
+
dtype = get_expected_input_dtype(op, xp)
|
| 298 |
+
x = xp.asarray(random((16, 8, 4)), dtype=dtype)
|
| 299 |
+
axes = [(0, 1, 2), (0, 2, 1), (1, 2, 0)]
|
| 300 |
+
|
| 301 |
+
for a in axes:
|
| 302 |
+
# different shape on the first two axes
|
| 303 |
+
shape = tuple([2*x.shape[ax] if ax in a[:2] else x.shape[ax]
|
| 304 |
+
for ax in range(x.ndim)])
|
| 305 |
+
# transform only the first two axes
|
| 306 |
+
op_tr = op(xp.permute_dims(x, axes=a), s=shape[:2], axes=(0, 1))
|
| 307 |
+
tr_op = xp.permute_dims(op(x, s=shape[:2], axes=a[:2]), axes=a)
|
| 308 |
+
xp_assert_close(op_tr, tr_op)
|
| 309 |
+
|
| 310 |
+
@make_xp_test_case(fft.rfft, fft.irfft, fft.ihfft, fft.hfft, fft.fft, fft.ifft)
|
| 311 |
+
def test_all_1d_norm_preserving(self, xp):
|
| 312 |
+
# verify that round-trip transforms are norm-preserving
|
| 313 |
+
x = xp.asarray(random(30), dtype=xp.float64)
|
| 314 |
+
|
| 315 |
+
x_norm = xp.linalg.vector_norm(x)
|
| 316 |
+
n = xp_size(x) * 2
|
| 317 |
+
func_pairs = [(fft.rfft, fft.irfft),
|
| 318 |
+
# hfft: order so the first function takes x.size samples
|
| 319 |
+
# (necessary for comparison to x_norm above)
|
| 320 |
+
(fft.ihfft, fft.hfft),
|
| 321 |
+
# functions that expect complex dtypes at the end
|
| 322 |
+
(fft.fft, fft.ifft),
|
| 323 |
+
]
|
| 324 |
+
for forw, back in func_pairs:
|
| 325 |
+
if forw == fft.fft:
|
| 326 |
+
x = xp.asarray(x, dtype=xp.complex128)
|
| 327 |
+
x_norm = xp.linalg.vector_norm(x)
|
| 328 |
+
for n in [xp_size(x), 2*xp_size(x)]:
|
| 329 |
+
for norm in ['backward', 'ortho', 'forward']:
|
| 330 |
+
tmp = forw(x, n=n, norm=norm)
|
| 331 |
+
tmp = back(tmp, n=n, norm=norm)
|
| 332 |
+
xp_assert_close(xp.linalg.vector_norm(tmp), x_norm)
|
| 333 |
+
|
| 334 |
+
@pytest.mark.parametrize("dtype", [np.float16, np.longdouble])
|
| 335 |
+
def test_dtypes_nonstandard(self, dtype):
|
| 336 |
+
x = random(30).astype(dtype)
|
| 337 |
+
out_dtypes = {np.float16: np.complex64, np.longdouble: np.clongdouble}
|
| 338 |
+
x_complex = x.astype(out_dtypes[dtype])
|
| 339 |
+
|
| 340 |
+
res_fft = fft.ifft(fft.fft(x))
|
| 341 |
+
res_rfft = fft.irfft(fft.rfft(x))
|
| 342 |
+
res_hfft = fft.hfft(fft.ihfft(x), x.shape[0])
|
| 343 |
+
# Check both numerical results and exact dtype matches
|
| 344 |
+
assert_array_almost_equal(res_fft, x_complex)
|
| 345 |
+
assert_array_almost_equal(res_rfft, x)
|
| 346 |
+
assert_array_almost_equal(res_hfft, x)
|
| 347 |
+
assert res_fft.dtype == x_complex.dtype
|
| 348 |
+
assert res_rfft.dtype == np.result_type(np.float32, x.dtype)
|
| 349 |
+
assert res_hfft.dtype == np.result_type(np.float32, x.dtype)
|
| 350 |
+
|
| 351 |
+
@make_xp_test_case(fft.irfft, fft.rfft)
|
| 352 |
+
@pytest.mark.parametrize("dtype", ["float32", "float64"])
|
| 353 |
+
def test_dtypes_real(self, dtype, xp):
|
| 354 |
+
x = xp.asarray(random(30), dtype=getattr(xp, dtype))
|
| 355 |
+
|
| 356 |
+
res_rfft = fft.irfft(fft.rfft(x))
|
| 357 |
+
res_hfft = fft.hfft(fft.ihfft(x), x.shape[0])
|
| 358 |
+
# Check both numerical results and exact dtype matches
|
| 359 |
+
xp_assert_close(res_rfft, x)
|
| 360 |
+
xp_assert_close(res_hfft, x)
|
| 361 |
+
|
| 362 |
+
@make_xp_test_case(fft.fft, fft.ifft)
|
| 363 |
+
@pytest.mark.parametrize("dtype", ["complex64", "complex128"])
|
| 364 |
+
def test_dtypes_complex(self, dtype, xp):
|
| 365 |
+
rng = np.random.default_rng(1234)
|
| 366 |
+
x = xp.asarray(rng.random(30), dtype=getattr(xp, dtype))
|
| 367 |
+
|
| 368 |
+
res_fft = fft.ifft(fft.fft(x))
|
| 369 |
+
# Check both numerical results and exact dtype matches
|
| 370 |
+
xp_assert_close(res_fft, x)
|
| 371 |
+
|
| 372 |
+
@pytest.mark.parametrize("op", [fft.fft, fft.ifft,
|
| 373 |
+
fft.fft2, fft.ifft2,
|
| 374 |
+
fft.fftn, fft.ifftn,
|
| 375 |
+
fft.rfft, fft.irfft,
|
| 376 |
+
fft.rfft2, fft.irfft2,
|
| 377 |
+
fft.rfftn, fft.irfftn,
|
| 378 |
+
fft.hfft, fft.ihfft,
|
| 379 |
+
fft.hfft2, fft.ihfft2,
|
| 380 |
+
fft.hfftn, fft.ihfftn,])
|
| 381 |
+
def test_array_like(self, op):
|
| 382 |
+
x = [[[1.0, 1.0], [1.0, 1.0]],
|
| 383 |
+
[[1.0, 1.0], [1.0, 1.0]],
|
| 384 |
+
[[1.0, 1.0], [1.0, 1.0]]]
|
| 385 |
+
xp_assert_close(op(x), op(np.asarray(x)))
|
| 386 |
+
|
| 387 |
+
|
| 388 |
+
@pytest.mark.parametrize(
|
| 389 |
+
"dtype",
|
| 390 |
+
[np.float32, np.float64, np.longdouble,
|
| 391 |
+
np.complex64, np.complex128, np.clongdouble])
|
| 392 |
+
@pytest.mark.parametrize("order", ["F", 'non-contiguous'])
|
| 393 |
+
@pytest.mark.parametrize(
|
| 394 |
+
"fft",
|
| 395 |
+
[fft.fft, fft.fft2, fft.fftn, fft.ifft, fft.ifft2, fft.ifftn])
|
| 396 |
+
def test_fft_with_order(dtype, order, fft):
|
| 397 |
+
# Check that FFT/IFFT produces identical results for C, Fortran and
|
| 398 |
+
# non contiguous arrays
|
| 399 |
+
rng = np.random.RandomState(42)
|
| 400 |
+
X = rng.rand(8, 7, 13).astype(dtype, copy=False)
|
| 401 |
+
if order == 'F':
|
| 402 |
+
Y = np.asfortranarray(X)
|
| 403 |
+
else:
|
| 404 |
+
# Make a non contiguous array
|
| 405 |
+
Y = X[::-1]
|
| 406 |
+
X = np.ascontiguousarray(X[::-1])
|
| 407 |
+
|
| 408 |
+
if fft.__name__.endswith('fft'):
|
| 409 |
+
for axis in range(3):
|
| 410 |
+
X_res = fft(X, axis=axis)
|
| 411 |
+
Y_res = fft(Y, axis=axis)
|
| 412 |
+
assert_array_almost_equal(X_res, Y_res)
|
| 413 |
+
elif fft.__name__.endswith(('fft2', 'fftn')):
|
| 414 |
+
axes = [(0, 1), (1, 2), (0, 2)]
|
| 415 |
+
if fft.__name__.endswith('fftn'):
|
| 416 |
+
axes.extend([(0,), (1,), (2,), None])
|
| 417 |
+
for ax in axes:
|
| 418 |
+
X_res = fft(X, axes=ax)
|
| 419 |
+
Y_res = fft(Y, axes=ax)
|
| 420 |
+
assert_array_almost_equal(X_res, Y_res)
|
| 421 |
+
else:
|
| 422 |
+
raise ValueError
|
| 423 |
+
|
| 424 |
+
|
| 425 |
+
@skip_xp_backends(cpu_only=True)
|
| 426 |
+
class TestFFTThreadSafe:
|
| 427 |
+
threads = 16
|
| 428 |
+
input_shape = (800, 200)
|
| 429 |
+
|
| 430 |
+
def _test_mtsame(self, func, *args, xp=None):
|
| 431 |
+
def worker(args, q):
|
| 432 |
+
q.put(func(*args))
|
| 433 |
+
|
| 434 |
+
q = queue.Queue()
|
| 435 |
+
expected = func(*args)
|
| 436 |
+
|
| 437 |
+
# Spin off a bunch of threads to call the same function simultaneously
|
| 438 |
+
t = [threading.Thread(target=worker, args=(args, q))
|
| 439 |
+
for i in range(self.threads)]
|
| 440 |
+
[x.start() for x in t]
|
| 441 |
+
|
| 442 |
+
[x.join() for x in t]
|
| 443 |
+
|
| 444 |
+
# Make sure all threads returned the correct value
|
| 445 |
+
for i in range(self.threads):
|
| 446 |
+
xp_assert_equal(
|
| 447 |
+
q.get(timeout=5), expected,
|
| 448 |
+
err_msg='Function returned wrong value in multithreaded context'
|
| 449 |
+
)
|
| 450 |
+
|
| 451 |
+
@make_xp_test_case(fft.fft)
|
| 452 |
+
def test_fft(self, xp):
|
| 453 |
+
a = xp.ones(self.input_shape, dtype=xp.complex128)
|
| 454 |
+
self._test_mtsame(fft.fft, a, xp=xp)
|
| 455 |
+
|
| 456 |
+
@make_xp_test_case(fft.ifft)
|
| 457 |
+
def test_ifft(self, xp):
|
| 458 |
+
a = xp.full(self.input_shape, 1+0j)
|
| 459 |
+
self._test_mtsame(fft.ifft, a, xp=xp)
|
| 460 |
+
|
| 461 |
+
@make_xp_test_case(fft.rfft)
|
| 462 |
+
def test_rfft(self, xp):
|
| 463 |
+
a = xp.ones(self.input_shape)
|
| 464 |
+
self._test_mtsame(fft.rfft, a, xp=xp)
|
| 465 |
+
|
| 466 |
+
@make_xp_test_case(fft.irfft)
|
| 467 |
+
def test_irfft(self, xp):
|
| 468 |
+
a = xp.full(self.input_shape, 1+0j)
|
| 469 |
+
self._test_mtsame(fft.irfft, a, xp=xp)
|
| 470 |
+
|
| 471 |
+
@make_xp_test_case(fft.hfft)
|
| 472 |
+
def test_hfft(self, xp):
|
| 473 |
+
a = xp.ones(self.input_shape, dtype=xp.complex64)
|
| 474 |
+
self._test_mtsame(fft.hfft, a, xp=xp)
|
| 475 |
+
|
| 476 |
+
@make_xp_test_case(fft.ihfft)
|
| 477 |
+
def test_ihfft(self, xp):
|
| 478 |
+
a = xp.ones(self.input_shape)
|
| 479 |
+
self._test_mtsame(fft.ihfft, a, xp=xp)
|
| 480 |
+
|
| 481 |
+
|
| 482 |
+
@pytest.mark.parametrize("func", [fft.fft, fft.ifft, fft.rfft, fft.irfft])
|
| 483 |
+
def test_multiprocess(func):
|
| 484 |
+
# Test that fft still works after fork (gh-10422)
|
| 485 |
+
|
| 486 |
+
with multiprocessing.Pool(2) as p:
|
| 487 |
+
res = p.map(func, [np.ones(100) for _ in range(4)])
|
| 488 |
+
|
| 489 |
+
expect = func(np.ones(100))
|
| 490 |
+
for x in res:
|
| 491 |
+
assert_allclose(x, expect)
|
| 492 |
+
|
| 493 |
+
|
| 494 |
+
@make_xp_test_case(fft.irfftn)
|
| 495 |
+
class TestIRFFTN:
|
| 496 |
+
|
| 497 |
+
def test_not_last_axis_success(self, xp):
|
| 498 |
+
ar, ai = np.random.random((2, 16, 8, 32))
|
| 499 |
+
a = ar + 1j*ai
|
| 500 |
+
a = xp.asarray(a)
|
| 501 |
+
|
| 502 |
+
axes = (-2,)
|
| 503 |
+
|
| 504 |
+
# Should not raise error
|
| 505 |
+
fft.irfftn(a, axes=axes)
|
| 506 |
+
|
| 507 |
+
|
| 508 |
+
@pytest.mark.parametrize("func", [make_xp_pytest_param(fft.fft),
|
| 509 |
+
make_xp_pytest_param(fft.ifft),
|
| 510 |
+
make_xp_pytest_param(fft.rfft),
|
| 511 |
+
make_xp_pytest_param(fft.irfft),
|
| 512 |
+
make_xp_pytest_param(fft.fftn),
|
| 513 |
+
make_xp_pytest_param(fft.ifftn),
|
| 514 |
+
make_xp_pytest_param(fft.rfftn),
|
| 515 |
+
make_xp_pytest_param(fft.irfftn),
|
| 516 |
+
make_xp_pytest_param(fft.hfft),
|
| 517 |
+
make_xp_pytest_param(fft.ihfft)])
|
| 518 |
+
def test_non_standard_params(func, xp):
|
| 519 |
+
# use __name__ so that `lazy_xp_function` doesn't break things
|
| 520 |
+
if func.__name__ in ["rfft", "rfftn", "ihfft"]:
|
| 521 |
+
dtype = xp.float64
|
| 522 |
+
else:
|
| 523 |
+
dtype = xp.complex128
|
| 524 |
+
|
| 525 |
+
x = xp.asarray([1, 2, 3], dtype=dtype)
|
| 526 |
+
# func(x) should not raise an exception
|
| 527 |
+
func(x)
|
| 528 |
+
|
| 529 |
+
if is_numpy(xp):
|
| 530 |
+
func(x, workers=2)
|
| 531 |
+
else:
|
| 532 |
+
assert_raises(ValueError, func, x, workers=2)
|
| 533 |
+
|
| 534 |
+
# `plan` param is not tested since SciPy does not use it currently
|
| 535 |
+
# but should be tested if it comes into use
|
| 536 |
+
|
| 537 |
+
|
| 538 |
+
@pytest.mark.parametrize("dtype", ['float32', 'float64'])
|
| 539 |
+
@pytest.mark.parametrize("func", [make_xp_pytest_param(fft.fft),
|
| 540 |
+
make_xp_pytest_param(fft.ifft),
|
| 541 |
+
make_xp_pytest_param(fft.irfft),
|
| 542 |
+
make_xp_pytest_param(fft.fftn),
|
| 543 |
+
make_xp_pytest_param(fft.ifftn),
|
| 544 |
+
make_xp_pytest_param(fft.irfftn),
|
| 545 |
+
make_xp_pytest_param(fft.hfft)])
|
| 546 |
+
def test_real_input(func, dtype, xp):
|
| 547 |
+
x = xp.asarray([1, 2, 3], dtype=getattr(xp, dtype))
|
| 548 |
+
# func(x) should not raise an exception
|
| 549 |
+
func(x)
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/tests/test_fftlog.py
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
| 9 |
+
|
| 10 |
+
from scipy._lib._array_api import xp_assert_close, xp_assert_less
|
| 11 |
+
|
| 12 |
+
skip_xp_backends = pytest.mark.skip_xp_backends
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def test_fht_agrees_with_fftlog(xp):
|
| 16 |
+
# check that fht numerically agrees with the output from Fortran FFTLog,
|
| 17 |
+
# the results were generated with the provided `fftlogtest` program,
|
| 18 |
+
# after fixing how the k array is generated (divide range by n-1, not n)
|
| 19 |
+
|
| 20 |
+
# test function, analytical Hankel transform is of the same form
|
| 21 |
+
def f(r, mu):
|
| 22 |
+
return r**(mu+1)*np.exp(-r**2/2)
|
| 23 |
+
|
| 24 |
+
r = np.logspace(-4, 4, 16)
|
| 25 |
+
|
| 26 |
+
dln = math.log(r[1]/r[0])
|
| 27 |
+
mu = 0.3
|
| 28 |
+
offset = 0.0
|
| 29 |
+
bias = 0.0
|
| 30 |
+
|
| 31 |
+
a = xp.asarray(f(r, mu))
|
| 32 |
+
|
| 33 |
+
# test 1: compute as given
|
| 34 |
+
ours = fht(a, dln, mu, offset=offset, bias=bias)
|
| 35 |
+
theirs = [-0.1159922613593045E-02, +0.1625822618458832E-02,
|
| 36 |
+
-0.1949518286432330E-02, +0.3789220182554077E-02,
|
| 37 |
+
+0.5093959119952945E-03, +0.2785387803618774E-01,
|
| 38 |
+
+0.9944952700848897E-01, +0.4599202164586588E+00,
|
| 39 |
+
+0.3157462160881342E+00, -0.8201236844404755E-03,
|
| 40 |
+
-0.7834031308271878E-03, +0.3931444945110708E-03,
|
| 41 |
+
-0.2697710625194777E-03, +0.3568398050238820E-03,
|
| 42 |
+
-0.5554454827797206E-03, +0.8286331026468585E-03]
|
| 43 |
+
theirs = xp.asarray(theirs, dtype=xp.float64)
|
| 44 |
+
xp_assert_close(ours, theirs)
|
| 45 |
+
|
| 46 |
+
# test 2: change to optimal offset
|
| 47 |
+
offset = fhtoffset(dln, mu, bias=bias)
|
| 48 |
+
ours = fht(a, dln, mu, offset=offset, bias=bias)
|
| 49 |
+
theirs = [+0.4353768523152057E-04, -0.9197045663594285E-05,
|
| 50 |
+
+0.3150140927838524E-03, +0.9149121960963704E-03,
|
| 51 |
+
+0.5808089753959363E-02, +0.2548065256377240E-01,
|
| 52 |
+
+0.1339477692089897E+00, +0.4821530509479356E+00,
|
| 53 |
+
+0.2659899781579785E+00, -0.1116475278448113E-01,
|
| 54 |
+
+0.1791441617592385E-02, -0.4181810476548056E-03,
|
| 55 |
+
+0.1314963536765343E-03, -0.5422057743066297E-04,
|
| 56 |
+
+0.3208681804170443E-04, -0.2696849476008234E-04]
|
| 57 |
+
theirs = xp.asarray(theirs, dtype=xp.float64)
|
| 58 |
+
xp_assert_close(ours, theirs)
|
| 59 |
+
|
| 60 |
+
# test 3: positive bias
|
| 61 |
+
bias = 0.8
|
| 62 |
+
offset = fhtoffset(dln, mu, bias=bias)
|
| 63 |
+
# offset is a np.float64, which array-api-strict disallows
|
| 64 |
+
# even if it's technically a subclass of float
|
| 65 |
+
offset = float(offset)
|
| 66 |
+
|
| 67 |
+
ours = fht(a, dln, mu, offset=offset, bias=bias)
|
| 68 |
+
theirs = [-7.3436673558316850E+00, +0.1710271207817100E+00,
|
| 69 |
+
+0.1065374386206564E+00, -0.5121739602708132E-01,
|
| 70 |
+
+0.2636649319269470E-01, +0.1697209218849693E-01,
|
| 71 |
+
+0.1250215614723183E+00, +0.4739583261486729E+00,
|
| 72 |
+
+0.2841149874912028E+00, -0.8312764741645729E-02,
|
| 73 |
+
+0.1024233505508988E-02, -0.1644902767389120E-03,
|
| 74 |
+
+0.3305775476926270E-04, -0.7786993194882709E-05,
|
| 75 |
+
+0.1962258449520547E-05, -0.8977895734909250E-06]
|
| 76 |
+
theirs = xp.asarray(theirs, dtype=xp.float64)
|
| 77 |
+
xp_assert_close(ours, theirs)
|
| 78 |
+
|
| 79 |
+
# test 4: negative bias
|
| 80 |
+
bias = -0.8
|
| 81 |
+
offset = fhtoffset(dln, mu, bias=bias)
|
| 82 |
+
offset = float(offset)
|
| 83 |
+
|
| 84 |
+
ours = fht(a, dln, mu, offset=offset, bias=bias)
|
| 85 |
+
theirs = [+0.8985777068568745E-05, +0.4074898209936099E-04,
|
| 86 |
+
+0.2123969254700955E-03, +0.1009558244834628E-02,
|
| 87 |
+
+0.5131386375222176E-02, +0.2461678673516286E-01,
|
| 88 |
+
+0.1235812845384476E+00, +0.4719570096404403E+00,
|
| 89 |
+
+0.2893487490631317E+00, -0.1686570611318716E-01,
|
| 90 |
+
+0.2231398155172505E-01, -0.1480742256379873E-01,
|
| 91 |
+
+0.1692387813500801E+00, +0.3097490354365797E+00,
|
| 92 |
+
+2.7593607182401860E+00, 10.5251075070045800E+00]
|
| 93 |
+
theirs = xp.asarray(theirs, dtype=xp.float64)
|
| 94 |
+
xp_assert_close(ours, theirs)
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
@pytest.mark.parametrize('optimal', [True, False])
|
| 98 |
+
@pytest.mark.parametrize('offset', [0.0, 1.0, -1.0])
|
| 99 |
+
@pytest.mark.parametrize('bias', [0, 0.1, -0.1])
|
| 100 |
+
@pytest.mark.parametrize('n', [64, 63])
|
| 101 |
+
def test_fht_identity(n, bias, offset, optimal, xp):
|
| 102 |
+
rng = np.random.RandomState(3491349965)
|
| 103 |
+
|
| 104 |
+
a = xp.asarray(rng.standard_normal(n))
|
| 105 |
+
dln = rng.uniform(-1, 1)
|
| 106 |
+
mu = rng.uniform(-2, 2)
|
| 107 |
+
|
| 108 |
+
if optimal:
|
| 109 |
+
offset = fhtoffset(dln, mu, initial=offset, bias=bias)
|
| 110 |
+
# offset is a np.float64, which array-api-strict disallows
|
| 111 |
+
# even if it's technically a subclass of float
|
| 112 |
+
offset = float(offset)
|
| 113 |
+
|
| 114 |
+
A = fht(a, dln, mu, offset=offset, bias=bias)
|
| 115 |
+
a_ = ifht(A, dln, mu, offset=offset, bias=bias)
|
| 116 |
+
|
| 117 |
+
xp_assert_close(a_, a, rtol=1.5e-7)
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
def test_fht_special_cases(xp):
|
| 123 |
+
rng = np.random.RandomState(3491349965)
|
| 124 |
+
|
| 125 |
+
a = xp.asarray(rng.standard_normal(64))
|
| 126 |
+
dln = rng.uniform(-1, 1)
|
| 127 |
+
|
| 128 |
+
# let x = (mu+1+q)/2, y = (mu+1-q)/2, M = {0, -1, -2, ...}
|
| 129 |
+
|
| 130 |
+
# case 1: x in M, y in M => well-defined transform
|
| 131 |
+
mu, bias = -4.0, 1.0
|
| 132 |
+
with warnings.catch_warnings(record=True) as record:
|
| 133 |
+
fht(a, dln, mu, bias=bias)
|
| 134 |
+
assert not record, 'fht warned about a well-defined transform'
|
| 135 |
+
|
| 136 |
+
# case 2: x not in M, y in M => well-defined transform
|
| 137 |
+
mu, bias = -2.5, 0.5
|
| 138 |
+
with warnings.catch_warnings(record=True) as record:
|
| 139 |
+
fht(a, dln, mu, bias=bias)
|
| 140 |
+
assert not record, 'fht warned about a well-defined transform'
|
| 141 |
+
|
| 142 |
+
# with fht_lock:
|
| 143 |
+
# case 3: x in M, y not in M => singular transform
|
| 144 |
+
mu, bias = -3.5, 0.5
|
| 145 |
+
with pytest.warns(Warning) as record:
|
| 146 |
+
fht(a, dln, mu, bias=bias)
|
| 147 |
+
assert record, 'fht did not warn about a singular transform'
|
| 148 |
+
|
| 149 |
+
# with fht_lock:
|
| 150 |
+
# case 4: x not in M, y in M => singular inverse transform
|
| 151 |
+
mu, bias = -2.5, 0.5
|
| 152 |
+
with pytest.warns(Warning) as record:
|
| 153 |
+
ifht(a, dln, mu, bias=bias)
|
| 154 |
+
assert record, 'ifht did not warn about a singular transform'
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
@pytest.mark.parametrize('n', [64, 63])
|
| 158 |
+
def test_fht_exact(n, xp):
|
| 159 |
+
rng = np.random.RandomState(3491349965)
|
| 160 |
+
|
| 161 |
+
# for a(r) a power law r^\gamma, the fast Hankel transform produces the
|
| 162 |
+
# exact continuous Hankel transform if biased with q = \gamma
|
| 163 |
+
|
| 164 |
+
mu = rng.uniform(0, 3)
|
| 165 |
+
|
| 166 |
+
# convergence of HT: -1-mu < gamma < 1/2
|
| 167 |
+
gamma = rng.uniform(-1-mu, 1/2)
|
| 168 |
+
|
| 169 |
+
r = np.logspace(-2, 2, n)
|
| 170 |
+
a = xp.asarray(r**gamma)
|
| 171 |
+
|
| 172 |
+
dln = math.log(r[1]/r[0])
|
| 173 |
+
|
| 174 |
+
offset = fhtoffset(dln, mu, initial=0.0, bias=gamma)
|
| 175 |
+
# offset is a np.float64, which array-api-strict disallows
|
| 176 |
+
# even if it's technically a subclass of float
|
| 177 |
+
offset = float(offset)
|
| 178 |
+
|
| 179 |
+
A = fht(a, dln, mu, offset=offset, bias=gamma)
|
| 180 |
+
|
| 181 |
+
k = np.exp(offset)/r[::-1]
|
| 182 |
+
|
| 183 |
+
# analytical result
|
| 184 |
+
At = xp.asarray((2/k)**gamma * poch((mu+1-gamma)/2, gamma))
|
| 185 |
+
|
| 186 |
+
xp_assert_close(A, At)
|
| 187 |
+
|
| 188 |
+
@skip_xp_backends(np_only=True,
|
| 189 |
+
reason='array-likes only supported for NumPy backend')
|
| 190 |
+
@pytest.mark.parametrize("op", [fht, ifht])
|
| 191 |
+
def test_array_like(xp, op):
|
| 192 |
+
x = [[[1.0, 1.0], [1.0, 1.0]],
|
| 193 |
+
[[1.0, 1.0], [1.0, 1.0]],
|
| 194 |
+
[[1.0, 1.0], [1.0, 1.0]]]
|
| 195 |
+
xp_assert_close(op(x, 1.0, 2.0), op(xp.asarray(x), 1.0, 2.0))
|
| 196 |
+
|
| 197 |
+
@pytest.mark.parametrize('n', [128, 129])
|
| 198 |
+
def test_gh_21661(xp, n):
|
| 199 |
+
one = xp.asarray(1.0)
|
| 200 |
+
mu = 0.0
|
| 201 |
+
r = np.logspace(-7, 1, n)
|
| 202 |
+
dln = math.log(r[1] / r[0])
|
| 203 |
+
offset = fhtoffset(dln, initial=-6 * np.log(10), mu=mu)
|
| 204 |
+
r = xp.asarray(r, dtype=one.dtype)
|
| 205 |
+
k = math.exp(offset) / xp.flip(r, axis=-1)
|
| 206 |
+
|
| 207 |
+
def f(x, mu):
|
| 208 |
+
return x**(mu + 1)*xp.exp(-x**2/2)
|
| 209 |
+
|
| 210 |
+
a_r = f(r, mu)
|
| 211 |
+
fht_val = fht(a_r, dln, mu=mu, offset=offset)
|
| 212 |
+
a_k = f(k, mu)
|
| 213 |
+
rel_err = xp.max(xp.abs((fht_val - a_k) / a_k))
|
| 214 |
+
xp_assert_less(rel_err, xp.asarray(7.28e+16)[()])
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/tests/test_helper.py
ADDED
|
@@ -0,0 +1,555 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Includes test functions for fftpack.helper module
|
| 2 |
+
|
| 3 |
+
Copied from fftpack.helper by Pearu Peterson, October 2005
|
| 4 |
+
Modified for Array API, 2023
|
| 5 |
+
|
| 6 |
+
"""
|
| 7 |
+
from scipy.fft._helper import next_fast_len, prev_fast_len, _init_nd_shape_and_axes
|
| 8 |
+
from numpy.testing import assert_equal
|
| 9 |
+
from pytest import raises as assert_raises
|
| 10 |
+
import pytest
|
| 11 |
+
import numpy as np
|
| 12 |
+
import sys
|
| 13 |
+
from scipy._lib._array_api import xp_assert_close, xp_device
|
| 14 |
+
from scipy import fft
|
| 15 |
+
|
| 16 |
+
skip_xp_backends = pytest.mark.skip_xp_backends
|
| 17 |
+
|
| 18 |
+
_5_smooth_numbers = [
|
| 19 |
+
2, 3, 4, 5, 6, 8, 9, 10,
|
| 20 |
+
2 * 3 * 5,
|
| 21 |
+
2**3 * 3**5,
|
| 22 |
+
2**3 * 3**3 * 5**2,
|
| 23 |
+
]
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def test_next_fast_len():
|
| 27 |
+
for n in _5_smooth_numbers:
|
| 28 |
+
assert_equal(next_fast_len(n), n)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def _assert_n_smooth(x, n):
|
| 32 |
+
x_orig = x
|
| 33 |
+
if n < 2:
|
| 34 |
+
assert False
|
| 35 |
+
|
| 36 |
+
while True:
|
| 37 |
+
q, r = divmod(x, 2)
|
| 38 |
+
if r != 0:
|
| 39 |
+
break
|
| 40 |
+
x = q
|
| 41 |
+
|
| 42 |
+
for d in range(3, n+1, 2):
|
| 43 |
+
while True:
|
| 44 |
+
q, r = divmod(x, d)
|
| 45 |
+
if r != 0:
|
| 46 |
+
break
|
| 47 |
+
x = q
|
| 48 |
+
|
| 49 |
+
assert x == 1, \
|
| 50 |
+
f'x={x_orig} is not {n}-smooth, remainder={x}'
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
class TestNextFastLen:
|
| 54 |
+
|
| 55 |
+
def test_next_fast_len(self):
|
| 56 |
+
np.random.seed(1234)
|
| 57 |
+
|
| 58 |
+
def nums():
|
| 59 |
+
yield from range(1, 1000)
|
| 60 |
+
yield 2**5 * 3**5 * 4**5 + 1
|
| 61 |
+
|
| 62 |
+
for n in nums():
|
| 63 |
+
m = next_fast_len(n)
|
| 64 |
+
_assert_n_smooth(m, 11)
|
| 65 |
+
assert m == next_fast_len(n, False)
|
| 66 |
+
|
| 67 |
+
m = next_fast_len(n, True)
|
| 68 |
+
_assert_n_smooth(m, 5)
|
| 69 |
+
|
| 70 |
+
def test_np_integers(self):
|
| 71 |
+
ITYPES = [np.int16, np.int32, np.int64, np.uint16, np.uint32, np.uint64]
|
| 72 |
+
for ityp in ITYPES:
|
| 73 |
+
x = ityp(12345)
|
| 74 |
+
testN = next_fast_len(x)
|
| 75 |
+
assert_equal(testN, next_fast_len(int(x)))
|
| 76 |
+
|
| 77 |
+
def testnext_fast_len_small(self):
|
| 78 |
+
hams = {
|
| 79 |
+
1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 8, 8: 8, 14: 15, 15: 15,
|
| 80 |
+
16: 16, 17: 18, 1021: 1024, 1536: 1536, 51200000: 51200000
|
| 81 |
+
}
|
| 82 |
+
for x, y in hams.items():
|
| 83 |
+
assert_equal(next_fast_len(x, True), y)
|
| 84 |
+
|
| 85 |
+
@pytest.mark.xfail(sys.maxsize < 2**32,
|
| 86 |
+
reason="Hamming Numbers too large for 32-bit",
|
| 87 |
+
raises=ValueError, strict=True)
|
| 88 |
+
def testnext_fast_len_big(self):
|
| 89 |
+
hams = {
|
| 90 |
+
510183360: 510183360, 510183360 + 1: 512000000,
|
| 91 |
+
511000000: 512000000,
|
| 92 |
+
854296875: 854296875, 854296875 + 1: 859963392,
|
| 93 |
+
196608000000: 196608000000, 196608000000 + 1: 196830000000,
|
| 94 |
+
8789062500000: 8789062500000, 8789062500000 + 1: 8796093022208,
|
| 95 |
+
206391214080000: 206391214080000,
|
| 96 |
+
206391214080000 + 1: 206624260800000,
|
| 97 |
+
470184984576000: 470184984576000,
|
| 98 |
+
470184984576000 + 1: 470715894135000,
|
| 99 |
+
7222041363087360: 7222041363087360,
|
| 100 |
+
7222041363087360 + 1: 7230196133913600,
|
| 101 |
+
# power of 5 5**23
|
| 102 |
+
11920928955078125: 11920928955078125,
|
| 103 |
+
11920928955078125 - 1: 11920928955078125,
|
| 104 |
+
# power of 3 3**34
|
| 105 |
+
16677181699666569: 16677181699666569,
|
| 106 |
+
16677181699666569 - 1: 16677181699666569,
|
| 107 |
+
# power of 2 2**54
|
| 108 |
+
18014398509481984: 18014398509481984,
|
| 109 |
+
18014398509481984 - 1: 18014398509481984,
|
| 110 |
+
# above this, int(ceil(n)) == int(ceil(n+1))
|
| 111 |
+
19200000000000000: 19200000000000000,
|
| 112 |
+
19200000000000000 + 1: 19221679687500000,
|
| 113 |
+
288230376151711744: 288230376151711744,
|
| 114 |
+
288230376151711744 + 1: 288325195312500000,
|
| 115 |
+
288325195312500000 - 1: 288325195312500000,
|
| 116 |
+
288325195312500000: 288325195312500000,
|
| 117 |
+
288325195312500000 + 1: 288555831593533440,
|
| 118 |
+
}
|
| 119 |
+
for x, y in hams.items():
|
| 120 |
+
assert_equal(next_fast_len(x, True), y)
|
| 121 |
+
|
| 122 |
+
def test_keyword_args(self, xp):
|
| 123 |
+
assert next_fast_len(11, real=True) == 12
|
| 124 |
+
assert next_fast_len(target=7, real=False) == 7
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
class TestPrevFastLen:
|
| 128 |
+
|
| 129 |
+
def test_prev_fast_len(self):
|
| 130 |
+
np.random.seed(1234)
|
| 131 |
+
|
| 132 |
+
def nums():
|
| 133 |
+
yield from range(1, 1000)
|
| 134 |
+
yield 2**5 * 3**5 * 4**5 + 1
|
| 135 |
+
|
| 136 |
+
for n in nums():
|
| 137 |
+
m = prev_fast_len(n)
|
| 138 |
+
_assert_n_smooth(m, 11)
|
| 139 |
+
assert m == prev_fast_len(n, False)
|
| 140 |
+
|
| 141 |
+
m = prev_fast_len(n, True)
|
| 142 |
+
_assert_n_smooth(m, 5)
|
| 143 |
+
|
| 144 |
+
def test_np_integers(self):
|
| 145 |
+
ITYPES = [np.int16, np.int32, np.int64, np.uint16, np.uint32,
|
| 146 |
+
np.uint64]
|
| 147 |
+
for ityp in ITYPES:
|
| 148 |
+
x = ityp(12345)
|
| 149 |
+
testN = prev_fast_len(x)
|
| 150 |
+
assert_equal(testN, prev_fast_len(int(x)))
|
| 151 |
+
|
| 152 |
+
testN = prev_fast_len(x, real=True)
|
| 153 |
+
assert_equal(testN, prev_fast_len(int(x), real=True))
|
| 154 |
+
|
| 155 |
+
def testprev_fast_len_small(self):
|
| 156 |
+
hams = {
|
| 157 |
+
1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 6, 8: 8, 14: 12, 15: 15,
|
| 158 |
+
16: 16, 17: 16, 1021: 1000, 1536: 1536, 51200000: 51200000
|
| 159 |
+
}
|
| 160 |
+
for x, y in hams.items():
|
| 161 |
+
assert_equal(prev_fast_len(x, True), y)
|
| 162 |
+
|
| 163 |
+
hams = {
|
| 164 |
+
1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10,
|
| 165 |
+
11: 11, 12: 12, 13: 12, 14: 14, 15: 15, 16: 16, 17: 16, 18: 18,
|
| 166 |
+
19: 18, 20: 20, 21: 21, 22: 22, 120: 120, 121: 121, 122: 121,
|
| 167 |
+
1021: 1008, 1536: 1536, 51200000: 51200000
|
| 168 |
+
}
|
| 169 |
+
for x, y in hams.items():
|
| 170 |
+
assert_equal(prev_fast_len(x, False), y)
|
| 171 |
+
|
| 172 |
+
@pytest.mark.xfail(sys.maxsize < 2**32,
|
| 173 |
+
reason="Hamming Numbers too large for 32-bit",
|
| 174 |
+
raises=ValueError, strict=True)
|
| 175 |
+
def testprev_fast_len_big(self):
|
| 176 |
+
hams = {
|
| 177 |
+
# 2**6 * 3**13 * 5**1
|
| 178 |
+
510183360: 510183360,
|
| 179 |
+
510183360 + 1: 510183360,
|
| 180 |
+
510183360 - 1: 509607936, # 2**21 * 3**5
|
| 181 |
+
# 2**6 * 5**6 * 7**1 * 73**1
|
| 182 |
+
511000000: 510183360,
|
| 183 |
+
511000000 + 1: 510183360,
|
| 184 |
+
511000000 - 1: 510183360, # 2**6 * 3**13 * 5**1
|
| 185 |
+
# 3**7 * 5**8
|
| 186 |
+
854296875: 854296875,
|
| 187 |
+
854296875 + 1: 854296875,
|
| 188 |
+
854296875 - 1: 850305600, # 2**6 * 3**12 * 5**2
|
| 189 |
+
# 2**22 * 3**1 * 5**6
|
| 190 |
+
196608000000: 196608000000,
|
| 191 |
+
196608000000 + 1: 196608000000,
|
| 192 |
+
196608000000 - 1: 195910410240, # 2**13 * 3**14 * 5**1
|
| 193 |
+
# 2**5 * 3**2 * 5**15
|
| 194 |
+
8789062500000: 8789062500000,
|
| 195 |
+
8789062500000 + 1: 8789062500000,
|
| 196 |
+
8789062500000 - 1: 8748000000000, # 2**11 * 3**7 * 5**9
|
| 197 |
+
# 2**24 * 3**9 * 5**4
|
| 198 |
+
206391214080000: 206391214080000,
|
| 199 |
+
206391214080000 + 1: 206391214080000,
|
| 200 |
+
206391214080000 - 1: 206158430208000, # 2**39 * 3**1 * 5**3
|
| 201 |
+
# 2**18 * 3**15 * 5**3
|
| 202 |
+
470184984576000: 470184984576000,
|
| 203 |
+
470184984576000 + 1: 470184984576000,
|
| 204 |
+
470184984576000 - 1: 469654673817600, # 2**33 * 3**7 **5**2
|
| 205 |
+
# 2**25 * 3**16 * 5**1
|
| 206 |
+
7222041363087360: 7222041363087360,
|
| 207 |
+
7222041363087360 + 1: 7222041363087360,
|
| 208 |
+
7222041363087360 - 1: 7213895789838336, # 2**40 * 3**8
|
| 209 |
+
# power of 5 5**23
|
| 210 |
+
11920928955078125: 11920928955078125,
|
| 211 |
+
11920928955078125 + 1: 11920928955078125,
|
| 212 |
+
11920928955078125 - 1: 11901557422080000, # 2**14 * 3**19 * 5**4
|
| 213 |
+
# power of 3 3**34
|
| 214 |
+
16677181699666569: 16677181699666569,
|
| 215 |
+
16677181699666569 + 1: 16677181699666569,
|
| 216 |
+
16677181699666569 - 1: 16607531250000000, # 2**7 * 3**12 * 5**12
|
| 217 |
+
# power of 2 2**54
|
| 218 |
+
18014398509481984: 18014398509481984,
|
| 219 |
+
18014398509481984 + 1: 18014398509481984,
|
| 220 |
+
18014398509481984 - 1: 18000000000000000, # 2**16 * 3**2 * 5**15
|
| 221 |
+
# 2**20 * 3**1 * 5**14
|
| 222 |
+
19200000000000000: 19200000000000000,
|
| 223 |
+
19200000000000000 + 1: 19200000000000000,
|
| 224 |
+
19200000000000000 - 1: 19131876000000000, # 2**11 * 3**14 * 5**9
|
| 225 |
+
# 2**58
|
| 226 |
+
288230376151711744: 288230376151711744,
|
| 227 |
+
288230376151711744 + 1: 288230376151711744,
|
| 228 |
+
288230376151711744 - 1: 288000000000000000, # 2**20 * 3**2 * 5**15
|
| 229 |
+
# 2**5 * 3**10 * 5**16
|
| 230 |
+
288325195312500000: 288325195312500000,
|
| 231 |
+
288325195312500000 + 1: 288325195312500000,
|
| 232 |
+
288325195312500000 - 1: 288230376151711744, # 2**58
|
| 233 |
+
}
|
| 234 |
+
for x, y in hams.items():
|
| 235 |
+
assert_equal(prev_fast_len(x, True), y)
|
| 236 |
+
|
| 237 |
+
def test_keyword_args(self):
|
| 238 |
+
assert prev_fast_len(11, real=True) == 10
|
| 239 |
+
assert prev_fast_len(target=7, real=False) == 7
|
| 240 |
+
|
| 241 |
+
|
| 242 |
+
@skip_xp_backends(cpu_only=True)
|
| 243 |
+
class Test_init_nd_shape_and_axes:
|
| 244 |
+
|
| 245 |
+
def test_py_0d_defaults(self, xp):
|
| 246 |
+
x = xp.asarray(4)
|
| 247 |
+
shape = None
|
| 248 |
+
axes = None
|
| 249 |
+
|
| 250 |
+
shape_expected = ()
|
| 251 |
+
axes_expected = []
|
| 252 |
+
|
| 253 |
+
shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes)
|
| 254 |
+
|
| 255 |
+
assert shape_res == shape_expected
|
| 256 |
+
assert axes_res == axes_expected
|
| 257 |
+
|
| 258 |
+
def test_xp_0d_defaults(self, xp):
|
| 259 |
+
x = xp.asarray(7.)
|
| 260 |
+
shape = None
|
| 261 |
+
axes = None
|
| 262 |
+
|
| 263 |
+
shape_expected = ()
|
| 264 |
+
axes_expected = []
|
| 265 |
+
|
| 266 |
+
shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes)
|
| 267 |
+
|
| 268 |
+
assert shape_res == shape_expected
|
| 269 |
+
assert axes_res == axes_expected
|
| 270 |
+
|
| 271 |
+
def test_py_1d_defaults(self, xp):
|
| 272 |
+
x = xp.asarray([1, 2, 3])
|
| 273 |
+
shape = None
|
| 274 |
+
axes = None
|
| 275 |
+
|
| 276 |
+
shape_expected = (3,)
|
| 277 |
+
axes_expected = [0]
|
| 278 |
+
|
| 279 |
+
shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes)
|
| 280 |
+
|
| 281 |
+
assert shape_res == shape_expected
|
| 282 |
+
assert axes_res == axes_expected
|
| 283 |
+
|
| 284 |
+
def test_xp_1d_defaults(self, xp):
|
| 285 |
+
x = xp.arange(0, 1, .1)
|
| 286 |
+
shape = None
|
| 287 |
+
axes = None
|
| 288 |
+
|
| 289 |
+
shape_expected = (10,)
|
| 290 |
+
axes_expected = [0]
|
| 291 |
+
|
| 292 |
+
shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes)
|
| 293 |
+
|
| 294 |
+
assert shape_res == shape_expected
|
| 295 |
+
assert axes_res == axes_expected
|
| 296 |
+
|
| 297 |
+
def test_py_2d_defaults(self, xp):
|
| 298 |
+
x = xp.asarray([[1, 2, 3, 4],
|
| 299 |
+
[5, 6, 7, 8]])
|
| 300 |
+
shape = None
|
| 301 |
+
axes = None
|
| 302 |
+
|
| 303 |
+
shape_expected = (2, 4)
|
| 304 |
+
axes_expected = [0, 1]
|
| 305 |
+
|
| 306 |
+
shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes)
|
| 307 |
+
|
| 308 |
+
assert shape_res == shape_expected
|
| 309 |
+
assert axes_res == axes_expected
|
| 310 |
+
|
| 311 |
+
def test_xp_2d_defaults(self, xp):
|
| 312 |
+
x = xp.arange(0, 1, .1)
|
| 313 |
+
x = xp.reshape(x, (5, 2))
|
| 314 |
+
shape = None
|
| 315 |
+
axes = None
|
| 316 |
+
|
| 317 |
+
shape_expected = (5, 2)
|
| 318 |
+
axes_expected = [0, 1]
|
| 319 |
+
|
| 320 |
+
shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes)
|
| 321 |
+
|
| 322 |
+
assert shape_res == shape_expected
|
| 323 |
+
assert axes_res == axes_expected
|
| 324 |
+
|
| 325 |
+
def test_xp_5d_defaults(self, xp):
|
| 326 |
+
x = xp.zeros([6, 2, 5, 3, 4])
|
| 327 |
+
shape = None
|
| 328 |
+
axes = None
|
| 329 |
+
|
| 330 |
+
shape_expected = (6, 2, 5, 3, 4)
|
| 331 |
+
axes_expected = [0, 1, 2, 3, 4]
|
| 332 |
+
|
| 333 |
+
shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes)
|
| 334 |
+
|
| 335 |
+
assert shape_res == shape_expected
|
| 336 |
+
assert axes_res == axes_expected
|
| 337 |
+
|
| 338 |
+
def test_xp_5d_set_shape(self, xp):
|
| 339 |
+
x = xp.zeros([6, 2, 5, 3, 4])
|
| 340 |
+
shape = [10, -1, -1, 1, 4]
|
| 341 |
+
axes = None
|
| 342 |
+
|
| 343 |
+
shape_expected = (10, 2, 5, 1, 4)
|
| 344 |
+
axes_expected = [0, 1, 2, 3, 4]
|
| 345 |
+
|
| 346 |
+
shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes)
|
| 347 |
+
|
| 348 |
+
assert shape_res == shape_expected
|
| 349 |
+
assert axes_res == axes_expected
|
| 350 |
+
|
| 351 |
+
def test_xp_5d_set_axes(self, xp):
|
| 352 |
+
x = xp.zeros([6, 2, 5, 3, 4])
|
| 353 |
+
shape = None
|
| 354 |
+
axes = [4, 1, 2]
|
| 355 |
+
|
| 356 |
+
shape_expected = (4, 2, 5)
|
| 357 |
+
axes_expected = [4, 1, 2]
|
| 358 |
+
|
| 359 |
+
shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes)
|
| 360 |
+
|
| 361 |
+
assert shape_res == shape_expected
|
| 362 |
+
assert axes_res == axes_expected
|
| 363 |
+
|
| 364 |
+
def test_xp_5d_set_shape_axes(self, xp):
|
| 365 |
+
x = xp.zeros([6, 2, 5, 3, 4])
|
| 366 |
+
shape = [10, -1, 2]
|
| 367 |
+
axes = [1, 0, 3]
|
| 368 |
+
|
| 369 |
+
shape_expected = (10, 6, 2)
|
| 370 |
+
axes_expected = [1, 0, 3]
|
| 371 |
+
|
| 372 |
+
shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes)
|
| 373 |
+
|
| 374 |
+
assert shape_res == shape_expected
|
| 375 |
+
assert axes_res == axes_expected
|
| 376 |
+
|
| 377 |
+
def test_shape_axes_subset(self, xp):
|
| 378 |
+
x = xp.zeros((2, 3, 4, 5))
|
| 379 |
+
shape, axes = _init_nd_shape_and_axes(x, shape=(5, 5, 5), axes=None)
|
| 380 |
+
|
| 381 |
+
assert shape == (5, 5, 5)
|
| 382 |
+
assert axes == [1, 2, 3]
|
| 383 |
+
|
| 384 |
+
def test_errors(self, xp):
|
| 385 |
+
x = xp.zeros(1)
|
| 386 |
+
with assert_raises(ValueError, match="axes must be a scalar or "
|
| 387 |
+
"iterable of integers"):
|
| 388 |
+
_init_nd_shape_and_axes(x, shape=None, axes=[[1, 2], [3, 4]])
|
| 389 |
+
|
| 390 |
+
with assert_raises(ValueError, match="axes must be a scalar or "
|
| 391 |
+
"iterable of integers"):
|
| 392 |
+
_init_nd_shape_and_axes(x, shape=None, axes=[1., 2., 3., 4.])
|
| 393 |
+
|
| 394 |
+
with assert_raises(ValueError,
|
| 395 |
+
match="axes exceeds dimensionality of input"):
|
| 396 |
+
_init_nd_shape_and_axes(x, shape=None, axes=[1])
|
| 397 |
+
|
| 398 |
+
with assert_raises(ValueError,
|
| 399 |
+
match="axes exceeds dimensionality of input"):
|
| 400 |
+
_init_nd_shape_and_axes(x, shape=None, axes=[-2])
|
| 401 |
+
|
| 402 |
+
with assert_raises(ValueError,
|
| 403 |
+
match="all axes must be unique"):
|
| 404 |
+
_init_nd_shape_and_axes(x, shape=None, axes=[0, 0])
|
| 405 |
+
|
| 406 |
+
with assert_raises(ValueError, match="shape must be a scalar or "
|
| 407 |
+
"iterable of integers"):
|
| 408 |
+
_init_nd_shape_and_axes(x, shape=[[1, 2], [3, 4]], axes=None)
|
| 409 |
+
|
| 410 |
+
with assert_raises(ValueError, match="shape must be a scalar or "
|
| 411 |
+
"iterable of integers"):
|
| 412 |
+
_init_nd_shape_and_axes(x, shape=[1., 2., 3., 4.], axes=None)
|
| 413 |
+
|
| 414 |
+
with assert_raises(ValueError,
|
| 415 |
+
match="when given, axes and shape arguments"
|
| 416 |
+
" have to be of the same length"):
|
| 417 |
+
_init_nd_shape_and_axes(xp.zeros([1, 1, 1, 1]),
|
| 418 |
+
shape=[1, 2, 3], axes=[1])
|
| 419 |
+
|
| 420 |
+
with assert_raises(ValueError,
|
| 421 |
+
match="invalid number of data points"
|
| 422 |
+
r" \(\[0\]\) specified"):
|
| 423 |
+
_init_nd_shape_and_axes(x, shape=[0], axes=None)
|
| 424 |
+
|
| 425 |
+
with assert_raises(ValueError,
|
| 426 |
+
match="invalid number of data points"
|
| 427 |
+
r" \(\[-2\]\) specified"):
|
| 428 |
+
_init_nd_shape_and_axes(x, shape=-2, axes=None)
|
| 429 |
+
|
| 430 |
+
|
| 431 |
+
class TestFFTShift:
|
| 432 |
+
|
| 433 |
+
def test_definition(self, xp):
|
| 434 |
+
x = xp.asarray([0., 1, 2, 3, 4, -4, -3, -2, -1])
|
| 435 |
+
y = xp.asarray([-4., -3, -2, -1, 0, 1, 2, 3, 4])
|
| 436 |
+
xp_assert_close(fft.fftshift(x), y)
|
| 437 |
+
xp_assert_close(fft.ifftshift(y), x)
|
| 438 |
+
x = xp.asarray([0., 1, 2, 3, 4, -5, -4, -3, -2, -1])
|
| 439 |
+
y = xp.asarray([-5., -4, -3, -2, -1, 0, 1, 2, 3, 4])
|
| 440 |
+
xp_assert_close(fft.fftshift(x), y)
|
| 441 |
+
xp_assert_close(fft.ifftshift(y), x)
|
| 442 |
+
|
| 443 |
+
def test_inverse(self, xp):
|
| 444 |
+
for n in [1, 4, 9, 100, 211]:
|
| 445 |
+
x = xp.asarray(np.random.random((n,)))
|
| 446 |
+
xp_assert_close(fft.ifftshift(fft.fftshift(x)), x)
|
| 447 |
+
|
| 448 |
+
def test_axes_keyword(self, xp):
|
| 449 |
+
freqs = xp.asarray([[0., 1, 2], [3, 4, -4], [-3, -2, -1]])
|
| 450 |
+
shifted = xp.asarray([[-1., -3, -2], [2, 0, 1], [-4, 3, 4]])
|
| 451 |
+
xp_assert_close(fft.fftshift(freqs, axes=(0, 1)), shifted)
|
| 452 |
+
xp_assert_close(fft.fftshift(freqs, axes=0), fft.fftshift(freqs, axes=(0,)))
|
| 453 |
+
xp_assert_close(fft.ifftshift(shifted, axes=(0, 1)), freqs)
|
| 454 |
+
xp_assert_close(fft.ifftshift(shifted, axes=0),
|
| 455 |
+
fft.ifftshift(shifted, axes=(0,)))
|
| 456 |
+
xp_assert_close(fft.fftshift(freqs), shifted)
|
| 457 |
+
xp_assert_close(fft.ifftshift(shifted), freqs)
|
| 458 |
+
|
| 459 |
+
def test_uneven_dims(self, xp):
|
| 460 |
+
""" Test 2D input, which has uneven dimension sizes """
|
| 461 |
+
freqs = xp.asarray([
|
| 462 |
+
[0, 1],
|
| 463 |
+
[2, 3],
|
| 464 |
+
[4, 5]
|
| 465 |
+
], dtype=xp.float64)
|
| 466 |
+
|
| 467 |
+
# shift in dimension 0
|
| 468 |
+
shift_dim0 = xp.asarray([
|
| 469 |
+
[4, 5],
|
| 470 |
+
[0, 1],
|
| 471 |
+
[2, 3]
|
| 472 |
+
], dtype=xp.float64)
|
| 473 |
+
xp_assert_close(fft.fftshift(freqs, axes=0), shift_dim0)
|
| 474 |
+
xp_assert_close(fft.ifftshift(shift_dim0, axes=0), freqs)
|
| 475 |
+
xp_assert_close(fft.fftshift(freqs, axes=(0,)), shift_dim0)
|
| 476 |
+
xp_assert_close(fft.ifftshift(shift_dim0, axes=[0]), freqs)
|
| 477 |
+
|
| 478 |
+
# shift in dimension 1
|
| 479 |
+
shift_dim1 = xp.asarray([
|
| 480 |
+
[1, 0],
|
| 481 |
+
[3, 2],
|
| 482 |
+
[5, 4]
|
| 483 |
+
], dtype=xp.float64)
|
| 484 |
+
xp_assert_close(fft.fftshift(freqs, axes=1), shift_dim1)
|
| 485 |
+
xp_assert_close(fft.ifftshift(shift_dim1, axes=1), freqs)
|
| 486 |
+
|
| 487 |
+
# shift in both dimensions
|
| 488 |
+
shift_dim_both = xp.asarray([
|
| 489 |
+
[5, 4],
|
| 490 |
+
[1, 0],
|
| 491 |
+
[3, 2]
|
| 492 |
+
], dtype=xp.float64)
|
| 493 |
+
xp_assert_close(fft.fftshift(freqs, axes=(0, 1)), shift_dim_both)
|
| 494 |
+
xp_assert_close(fft.ifftshift(shift_dim_both, axes=(0, 1)), freqs)
|
| 495 |
+
xp_assert_close(fft.fftshift(freqs, axes=[0, 1]), shift_dim_both)
|
| 496 |
+
xp_assert_close(fft.ifftshift(shift_dim_both, axes=[0, 1]), freqs)
|
| 497 |
+
|
| 498 |
+
# axes=None (default) shift in all dimensions
|
| 499 |
+
xp_assert_close(fft.fftshift(freqs, axes=None), shift_dim_both)
|
| 500 |
+
xp_assert_close(fft.ifftshift(shift_dim_both, axes=None), freqs)
|
| 501 |
+
xp_assert_close(fft.fftshift(freqs), shift_dim_both)
|
| 502 |
+
xp_assert_close(fft.ifftshift(shift_dim_both), freqs)
|
| 503 |
+
|
| 504 |
+
|
| 505 |
+
class TestFFTFreq:
|
| 506 |
+
def test_definition(self, xp):
|
| 507 |
+
x = xp.asarray([0, 1, 2, 3, 4, -4, -3, -2, -1], dtype=xp.float64)
|
| 508 |
+
x2 = xp.asarray([0, 1, 2, 3, 4, -5, -4, -3, -2, -1], dtype=xp.float64)
|
| 509 |
+
|
| 510 |
+
# default dtype varies across backends
|
| 511 |
+
|
| 512 |
+
y = 9 * fft.fftfreq(9, xp=xp)
|
| 513 |
+
xp_assert_close(y, x, check_dtype=False, check_namespace=True)
|
| 514 |
+
|
| 515 |
+
y = 9 * xp.pi * fft.fftfreq(9, xp.pi, xp=xp)
|
| 516 |
+
xp_assert_close(y, x, check_dtype=False)
|
| 517 |
+
|
| 518 |
+
y = 10 * fft.fftfreq(10, xp=xp)
|
| 519 |
+
xp_assert_close(y, x2, check_dtype=False)
|
| 520 |
+
|
| 521 |
+
y = 10 * xp.pi * fft.fftfreq(10, xp.pi, xp=xp)
|
| 522 |
+
xp_assert_close(y, x2, check_dtype=False)
|
| 523 |
+
|
| 524 |
+
def test_device(self, xp, devices):
|
| 525 |
+
for d in devices:
|
| 526 |
+
y = fft.fftfreq(9, xp=xp, device=d)
|
| 527 |
+
x = xp.empty(0, device=d)
|
| 528 |
+
assert xp_device(y) == xp_device(x)
|
| 529 |
+
|
| 530 |
+
|
| 531 |
+
class TestRFFTFreq:
|
| 532 |
+
|
| 533 |
+
def test_definition(self, xp):
|
| 534 |
+
x = xp.asarray([0, 1, 2, 3, 4], dtype=xp.float64)
|
| 535 |
+
x2 = xp.asarray([0, 1, 2, 3, 4, 5], dtype=xp.float64)
|
| 536 |
+
|
| 537 |
+
# default dtype varies across backends
|
| 538 |
+
|
| 539 |
+
y = 9 * fft.rfftfreq(9, xp=xp)
|
| 540 |
+
xp_assert_close(y, x, check_dtype=False, check_namespace=True)
|
| 541 |
+
|
| 542 |
+
y = 9 * xp.pi * fft.rfftfreq(9, xp.pi, xp=xp)
|
| 543 |
+
xp_assert_close(y, x, check_dtype=False)
|
| 544 |
+
|
| 545 |
+
y = 10 * fft.rfftfreq(10, xp=xp)
|
| 546 |
+
xp_assert_close(y, x2, check_dtype=False)
|
| 547 |
+
|
| 548 |
+
y = 10 * xp.pi * fft.rfftfreq(10, xp.pi, xp=xp)
|
| 549 |
+
xp_assert_close(y, x2, check_dtype=False)
|
| 550 |
+
|
| 551 |
+
def test_device(self, xp, devices):
|
| 552 |
+
for d in devices:
|
| 553 |
+
y = fft.rfftfreq(9, xp=xp, device=d)
|
| 554 |
+
x = xp.empty(0, device=d)
|
| 555 |
+
assert xp_device(y) == xp_device(x)
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/tests/test_multithreading.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from scipy import fft
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pytest
|
| 4 |
+
from numpy.testing import assert_allclose
|
| 5 |
+
import multiprocessing
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
@pytest.fixture(scope='module')
|
| 10 |
+
def x():
|
| 11 |
+
return np.random.randn(512, 128) # Must be large enough to qualify for mt
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
@pytest.mark.parametrize("func", [
|
| 15 |
+
fft.fft, fft.ifft, fft.fft2, fft.ifft2, fft.fftn, fft.ifftn,
|
| 16 |
+
fft.rfft, fft.irfft, fft.rfft2, fft.irfft2, fft.rfftn, fft.irfftn,
|
| 17 |
+
fft.hfft, fft.ihfft, fft.hfft2, fft.ihfft2, fft.hfftn, fft.ihfftn,
|
| 18 |
+
fft.dct, fft.idct, fft.dctn, fft.idctn,
|
| 19 |
+
fft.dst, fft.idst, fft.dstn, fft.idstn,
|
| 20 |
+
])
|
| 21 |
+
@pytest.mark.parametrize("workers", [2, -1])
|
| 22 |
+
def test_threaded_same(x, func, workers):
|
| 23 |
+
expected = func(x, workers=1)
|
| 24 |
+
actual = func(x, workers=workers)
|
| 25 |
+
assert_allclose(actual, expected)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def _mt_fft(x):
|
| 29 |
+
return fft.fft(x, workers=2)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
@pytest.mark.slow
|
| 33 |
+
def test_mixed_threads_processes(x):
|
| 34 |
+
# Test that the fft threadpool is safe to use before & after fork
|
| 35 |
+
|
| 36 |
+
expect = fft.fft(x, workers=2)
|
| 37 |
+
|
| 38 |
+
with multiprocessing.Pool(2) as p:
|
| 39 |
+
res = p.map(_mt_fft, [x for _ in range(4)])
|
| 40 |
+
|
| 41 |
+
for r in res:
|
| 42 |
+
assert_allclose(r, expect)
|
| 43 |
+
|
| 44 |
+
fft.fft(x, workers=2)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def test_invalid_workers(x):
|
| 48 |
+
cpus = os.cpu_count()
|
| 49 |
+
|
| 50 |
+
fft.ifft([1], workers=-cpus)
|
| 51 |
+
|
| 52 |
+
with pytest.raises(ValueError, match='workers must not be zero'):
|
| 53 |
+
fft.fft(x, workers=0)
|
| 54 |
+
|
| 55 |
+
with pytest.raises(ValueError, match='workers value out of range'):
|
| 56 |
+
fft.ifft(x, workers=-cpus-1)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def test_set_get_workers():
|
| 60 |
+
cpus = os.cpu_count()
|
| 61 |
+
assert fft.get_workers() == 1
|
| 62 |
+
with fft.set_workers(4):
|
| 63 |
+
assert fft.get_workers() == 4
|
| 64 |
+
|
| 65 |
+
with fft.set_workers(-1):
|
| 66 |
+
assert fft.get_workers() == cpus
|
| 67 |
+
|
| 68 |
+
assert fft.get_workers() == 4
|
| 69 |
+
|
| 70 |
+
assert fft.get_workers() == 1
|
| 71 |
+
|
| 72 |
+
with fft.set_workers(-cpus):
|
| 73 |
+
assert fft.get_workers() == 1
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def test_set_workers_invalid():
|
| 77 |
+
|
| 78 |
+
with pytest.raises(ValueError, match='workers must not be zero'):
|
| 79 |
+
with fft.set_workers(0):
|
| 80 |
+
pass
|
| 81 |
+
|
| 82 |
+
with pytest.raises(ValueError, match='workers value out of range'):
|
| 83 |
+
with fft.set_workers(-os.cpu_count()-1):
|
| 84 |
+
pass
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fft/tests/test_real_transforms.py
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from numpy.testing import assert_allclose, assert_array_equal
|
| 3 |
+
import pytest
|
| 4 |
+
import math
|
| 5 |
+
|
| 6 |
+
from scipy.fft import dct, idct, dctn, idctn, dst, idst, dstn, idstn
|
| 7 |
+
import scipy.fft as fft
|
| 8 |
+
from scipy import fftpack
|
| 9 |
+
from scipy._lib._array_api import (xp_copy, xp_assert_close, make_xp_test_case,
|
| 10 |
+
make_xp_pytest_param)
|
| 11 |
+
import scipy._lib.array_api_extra as xpx
|
| 12 |
+
|
| 13 |
+
skip_xp_backends = pytest.mark.skip_xp_backends
|
| 14 |
+
|
| 15 |
+
SQRT_2 = math.sqrt(2)
|
| 16 |
+
|
| 17 |
+
# scipy.fft wraps the fftpack versions but with normalized inverse transforms.
|
| 18 |
+
# So, the forward transforms and definitions are already thoroughly tested in
|
| 19 |
+
# fftpack/test_real_transforms.py
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
@pytest.mark.parametrize("forward, backward", [make_xp_pytest_param(dct, idct),
|
| 23 |
+
make_xp_pytest_param(dst, idst)])
|
| 24 |
+
@pytest.mark.parametrize("type", [1, 2, 3, 4])
|
| 25 |
+
@pytest.mark.parametrize("n", [2, 3, 4, 5, 10, 16])
|
| 26 |
+
@pytest.mark.parametrize("axis", [0, 1])
|
| 27 |
+
@pytest.mark.parametrize("norm", [None, 'backward', 'ortho', 'forward'])
|
| 28 |
+
@pytest.mark.parametrize("orthogonalize", [False, True])
|
| 29 |
+
def test_identity_1d(forward, backward, type, n, axis, norm, orthogonalize, xp):
|
| 30 |
+
# Test the identity f^-1(f(x)) == x
|
| 31 |
+
x = xp.asarray(np.random.rand(n, n))
|
| 32 |
+
|
| 33 |
+
y = forward(x, type, axis=axis, norm=norm, orthogonalize=orthogonalize)
|
| 34 |
+
z = backward(y, type, axis=axis, norm=norm, orthogonalize=orthogonalize)
|
| 35 |
+
xp_assert_close(z, x)
|
| 36 |
+
|
| 37 |
+
pad = [(0, 0)] * 2
|
| 38 |
+
pad[axis] = (0, 4)
|
| 39 |
+
|
| 40 |
+
y2 = xp.asarray(np.pad(np.asarray(y), pad, mode='edge'))
|
| 41 |
+
z2 = backward(y2, type, n, axis, norm, orthogonalize=orthogonalize)
|
| 42 |
+
xp_assert_close(z2, x)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
@skip_xp_backends(np_only=True,
|
| 46 |
+
reason='`overwrite_x` only supported for NumPy backend.')
|
| 47 |
+
@pytest.mark.parametrize("forward, backward", [(dct, idct), (dst, idst)])
|
| 48 |
+
@pytest.mark.parametrize("type", [1, 2, 3, 4])
|
| 49 |
+
@pytest.mark.parametrize("dtype", [np.float16, np.float32, np.float64,
|
| 50 |
+
np.complex64, np.complex128])
|
| 51 |
+
@pytest.mark.parametrize("axis", [0, 1])
|
| 52 |
+
@pytest.mark.parametrize("norm", [None, 'backward', 'ortho', 'forward'])
|
| 53 |
+
@pytest.mark.parametrize("overwrite_x", [True, False])
|
| 54 |
+
def test_identity_1d_overwrite(forward, backward, type, dtype, axis, norm,
|
| 55 |
+
overwrite_x, xp):
|
| 56 |
+
# Test the identity f^-1(f(x)) == x
|
| 57 |
+
x = np.random.rand(7, 8).astype(dtype)
|
| 58 |
+
x_orig = x.copy()
|
| 59 |
+
|
| 60 |
+
y = forward(x, type, axis=axis, norm=norm, overwrite_x=overwrite_x)
|
| 61 |
+
y_orig = y.copy()
|
| 62 |
+
z = backward(y, type, axis=axis, norm=norm, overwrite_x=overwrite_x)
|
| 63 |
+
if not overwrite_x:
|
| 64 |
+
assert_allclose(z, x, rtol=1e-6, atol=1e-6)
|
| 65 |
+
assert_array_equal(x, x_orig)
|
| 66 |
+
assert_array_equal(y, y_orig)
|
| 67 |
+
else:
|
| 68 |
+
assert_allclose(z, x_orig, rtol=1e-6, atol=1e-6)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
@pytest.mark.parametrize("forward, backward", [make_xp_pytest_param(dctn, idctn),
|
| 72 |
+
make_xp_pytest_param(dstn, idstn)])
|
| 73 |
+
@pytest.mark.parametrize("type", [1, 2, 3, 4])
|
| 74 |
+
@pytest.mark.parametrize("shape, axes",
|
| 75 |
+
[
|
| 76 |
+
((4, 4), 0),
|
| 77 |
+
((4, 4), 1),
|
| 78 |
+
((4, 4), None),
|
| 79 |
+
((4, 4), (0, 1)),
|
| 80 |
+
((10, 12), None),
|
| 81 |
+
((10, 12), (0, 1)),
|
| 82 |
+
((4, 5, 6), None),
|
| 83 |
+
((4, 5, 6), 1),
|
| 84 |
+
((4, 5, 6), (0, 2)),
|
| 85 |
+
])
|
| 86 |
+
@pytest.mark.parametrize("norm", [None, 'backward', 'ortho', 'forward'])
|
| 87 |
+
@pytest.mark.parametrize("orthogonalize", [False, True])
|
| 88 |
+
def test_identity_nd(forward, backward, type, shape, axes, norm,
|
| 89 |
+
orthogonalize, xp):
|
| 90 |
+
# Test the identity f^-1(f(x)) == x
|
| 91 |
+
|
| 92 |
+
x = xp.asarray(np.random.random(shape))
|
| 93 |
+
|
| 94 |
+
if axes is not None:
|
| 95 |
+
shape = np.take(shape, axes)
|
| 96 |
+
|
| 97 |
+
y = forward(x, type, axes=axes, norm=norm, orthogonalize=orthogonalize)
|
| 98 |
+
z = backward(y, type, axes=axes, norm=norm, orthogonalize=orthogonalize)
|
| 99 |
+
xp_assert_close(z, x)
|
| 100 |
+
|
| 101 |
+
if axes is None:
|
| 102 |
+
pad = [(0, 4)] * x.ndim
|
| 103 |
+
elif isinstance(axes, int):
|
| 104 |
+
pad = [(0, 0)] * x.ndim
|
| 105 |
+
pad[axes] = (0, 4)
|
| 106 |
+
else:
|
| 107 |
+
pad = [(0, 0)] * x.ndim
|
| 108 |
+
|
| 109 |
+
for a in axes:
|
| 110 |
+
pad[a] = (0, 4)
|
| 111 |
+
|
| 112 |
+
# TODO write an array-agnostic pad
|
| 113 |
+
y2 = xp.asarray(np.pad(np.asarray(y), pad, mode='edge'))
|
| 114 |
+
z2 = backward(y2, type, shape, axes, norm, orthogonalize=orthogonalize)
|
| 115 |
+
xp_assert_close(z2, x)
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
@skip_xp_backends(np_only=True,
|
| 119 |
+
reason='`overwrite_x` only supported for NumPy backend.')
|
| 120 |
+
@pytest.mark.parametrize("forward, backward", [(dctn, idctn), (dstn, idstn)])
|
| 121 |
+
@pytest.mark.parametrize("type", [1, 2, 3, 4])
|
| 122 |
+
@pytest.mark.parametrize("shape, axes",
|
| 123 |
+
[
|
| 124 |
+
((4, 5), 0),
|
| 125 |
+
((4, 5), 1),
|
| 126 |
+
((4, 5), None),
|
| 127 |
+
])
|
| 128 |
+
@pytest.mark.parametrize("dtype", [np.float16, np.float32, np.float64,
|
| 129 |
+
np.complex64, np.complex128])
|
| 130 |
+
@pytest.mark.parametrize("norm", [None, 'backward', 'ortho', 'forward'])
|
| 131 |
+
@pytest.mark.parametrize("overwrite_x", [False, True])
|
| 132 |
+
def test_identity_nd_overwrite(forward, backward, type, shape, axes, dtype,
|
| 133 |
+
norm, overwrite_x, xp):
|
| 134 |
+
# Test the identity f^-1(f(x)) == x
|
| 135 |
+
|
| 136 |
+
x = np.random.random(shape).astype(dtype)
|
| 137 |
+
x_orig = x.copy()
|
| 138 |
+
|
| 139 |
+
if axes is not None:
|
| 140 |
+
shape = np.take(shape, axes)
|
| 141 |
+
|
| 142 |
+
y = forward(x, type, axes=axes, norm=norm)
|
| 143 |
+
y_orig = y.copy()
|
| 144 |
+
z = backward(y, type, axes=axes, norm=norm)
|
| 145 |
+
if overwrite_x:
|
| 146 |
+
assert_allclose(z, x_orig, rtol=1e-6, atol=1e-6)
|
| 147 |
+
else:
|
| 148 |
+
assert_allclose(z, x, rtol=1e-6, atol=1e-6)
|
| 149 |
+
assert_array_equal(x, x_orig)
|
| 150 |
+
assert_array_equal(y, y_orig)
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
@pytest.mark.parametrize("func", [make_xp_pytest_param(dct),
|
| 154 |
+
make_xp_pytest_param(dst),
|
| 155 |
+
make_xp_pytest_param(dctn),
|
| 156 |
+
make_xp_pytest_param(dstn)])
|
| 157 |
+
@pytest.mark.parametrize("type", [1, 2, 3, 4])
|
| 158 |
+
@pytest.mark.parametrize("norm", [None, 'backward', 'ortho', 'forward'])
|
| 159 |
+
def test_fftpack_equivalience(func, type, norm, xp):
|
| 160 |
+
x = np.random.rand(8, 16)
|
| 161 |
+
fftpack_res = xp.asarray(getattr(fftpack, func.__name__)(x, type, norm=norm))
|
| 162 |
+
x = xp.asarray(x)
|
| 163 |
+
fft_res = getattr(fft, func.__name__)(x, type, norm=norm)
|
| 164 |
+
|
| 165 |
+
xp_assert_close(fft_res, fftpack_res)
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
@pytest.mark.parametrize("func", [make_xp_pytest_param(dct),
|
| 169 |
+
make_xp_pytest_param(dst),
|
| 170 |
+
make_xp_pytest_param(dctn),
|
| 171 |
+
make_xp_pytest_param(dstn)])
|
| 172 |
+
@pytest.mark.parametrize("type", [1, 2, 3, 4])
|
| 173 |
+
def test_orthogonalize_default(func, type, xp):
|
| 174 |
+
# Test orthogonalize is the default when norm="ortho", but not otherwise
|
| 175 |
+
x = xp.asarray(np.random.rand(100))
|
| 176 |
+
|
| 177 |
+
for norm, ortho in [
|
| 178 |
+
("forward", False),
|
| 179 |
+
("backward", False),
|
| 180 |
+
("ortho", True),
|
| 181 |
+
]:
|
| 182 |
+
a = func(x, type=type, norm=norm, orthogonalize=ortho)
|
| 183 |
+
b = func(x, type=type, norm=norm)
|
| 184 |
+
xp_assert_close(a, b)
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
@pytest.mark.parametrize("norm", ["backward", "ortho", "forward"])
|
| 188 |
+
@pytest.mark.parametrize("func, type", [make_xp_pytest_param(dct, 4),
|
| 189 |
+
make_xp_pytest_param(dst, 1),
|
| 190 |
+
make_xp_pytest_param(dst, 4)])
|
| 191 |
+
def test_orthogonalize_noop(func, type, norm, xp):
|
| 192 |
+
# Transforms where orthogonalize is a no-op
|
| 193 |
+
x = xp.asarray(np.random.rand(100))
|
| 194 |
+
y1 = func(x, type=type, norm=norm, orthogonalize=True)
|
| 195 |
+
y2 = func(x, type=type, norm=norm, orthogonalize=False)
|
| 196 |
+
xp_assert_close(y1, y2)
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
@make_xp_test_case(dct)
|
| 200 |
+
@pytest.mark.parametrize("norm", ["backward", "ortho", "forward"])
|
| 201 |
+
def test_orthogonalize_dct1(norm, xp):
|
| 202 |
+
x = xp.asarray(np.random.rand(100))
|
| 203 |
+
|
| 204 |
+
x2 = xp_copy(x, xp=xp)
|
| 205 |
+
xpx.at(x2, 0).multiply(SQRT_2)
|
| 206 |
+
xpx.at(x2, -1).multiply(SQRT_2)
|
| 207 |
+
|
| 208 |
+
y1 = dct(x, type=1, norm=norm, orthogonalize=True)
|
| 209 |
+
y2 = dct(x2, type=1, norm=norm, orthogonalize=False)
|
| 210 |
+
|
| 211 |
+
xpx.at(y2, 0).divide(SQRT_2)
|
| 212 |
+
xpx.at(y2, -1).divide(SQRT_2)
|
| 213 |
+
xp_assert_close(y1, y2)
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
@pytest.mark.parametrize("norm", ["backward", "ortho", "forward"])
|
| 218 |
+
@pytest.mark.parametrize("func", [make_xp_pytest_param(dct),
|
| 219 |
+
make_xp_pytest_param(dst)])
|
| 220 |
+
def test_orthogonalize_dcst2(func, norm, xp):
|
| 221 |
+
x = xp.asarray(np.random.rand(100))
|
| 222 |
+
y1 = func(x, type=2, norm=norm, orthogonalize=True)
|
| 223 |
+
y2 = func(x, type=2, norm=norm, orthogonalize=False)
|
| 224 |
+
|
| 225 |
+
xpx.at(y2, 0 if func.__name__ == "dct" else -1).divide(SQRT_2)
|
| 226 |
+
xp_assert_close(y1, y2)
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
@pytest.mark.parametrize("norm", ["backward", "ortho", "forward"])
|
| 230 |
+
@pytest.mark.parametrize("func", [make_xp_pytest_param(dct),
|
| 231 |
+
make_xp_pytest_param(dst)])
|
| 232 |
+
def test_orthogonalize_dcst3(func, norm, xp):
|
| 233 |
+
x = xp.asarray(np.random.rand(100))
|
| 234 |
+
x2 = xp_copy(x, xp=xp)
|
| 235 |
+
xpx.at(x2, 0 if func.__name__ == "dct" else -1).multiply(SQRT_2)
|
| 236 |
+
|
| 237 |
+
y1 = func(x, type=3, norm=norm, orthogonalize=True)
|
| 238 |
+
y2 = func(x2, type=3, norm=norm, orthogonalize=False)
|
| 239 |
+
xp_assert_close(y1, y2)
|
| 240 |
+
|
| 241 |
+
|
| 242 |
+
@skip_xp_backends(np_only=True,
|
| 243 |
+
reason='array-likes only supported for NumPy backend')
|
| 244 |
+
@pytest.mark.parametrize("func", [dct, idct, dctn, idctn, dst, idst, dstn, idstn])
|
| 245 |
+
def test_array_like(xp, func):
|
| 246 |
+
x = [[[1.0, 1.0], [1.0, 1.0]],
|
| 247 |
+
[[1.0, 1.0], [1.0, 1.0]],
|
| 248 |
+
[[1.0, 1.0], [1.0, 1.0]]]
|
| 249 |
+
xp_assert_close(func(x), func(xp.asarray(x)))
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/__init__.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
=========================================================
|
| 3 |
+
Legacy discrete Fourier transforms (:mod:`scipy.fftpack`)
|
| 4 |
+
=========================================================
|
| 5 |
+
|
| 6 |
+
.. legacy::
|
| 7 |
+
|
| 8 |
+
New code should use :mod:`scipy.fft`.
|
| 9 |
+
|
| 10 |
+
Fast Fourier Transforms (FFTs)
|
| 11 |
+
==============================
|
| 12 |
+
|
| 13 |
+
.. autosummary::
|
| 14 |
+
:toctree: generated/
|
| 15 |
+
|
| 16 |
+
fft - Fast (discrete) Fourier Transform (FFT)
|
| 17 |
+
ifft - Inverse FFT
|
| 18 |
+
fft2 - 2-D FFT
|
| 19 |
+
ifft2 - 2-D inverse FFT
|
| 20 |
+
fftn - N-D FFT
|
| 21 |
+
ifftn - N-D inverse FFT
|
| 22 |
+
rfft - FFT of strictly real-valued sequence
|
| 23 |
+
irfft - Inverse of rfft
|
| 24 |
+
dct - Discrete cosine transform
|
| 25 |
+
idct - Inverse discrete cosine transform
|
| 26 |
+
dctn - N-D Discrete cosine transform
|
| 27 |
+
idctn - N-D Inverse discrete cosine transform
|
| 28 |
+
dst - Discrete sine transform
|
| 29 |
+
idst - Inverse discrete sine transform
|
| 30 |
+
dstn - N-D Discrete sine transform
|
| 31 |
+
idstn - N-D Inverse discrete sine transform
|
| 32 |
+
|
| 33 |
+
Differential and pseudo-differential operators
|
| 34 |
+
==============================================
|
| 35 |
+
|
| 36 |
+
.. autosummary::
|
| 37 |
+
:toctree: generated/
|
| 38 |
+
|
| 39 |
+
diff - Differentiation and integration of periodic sequences
|
| 40 |
+
tilbert - Tilbert transform: cs_diff(x,h,h)
|
| 41 |
+
itilbert - Inverse Tilbert transform: sc_diff(x,h,h)
|
| 42 |
+
hilbert - Hilbert transform: cs_diff(x,inf,inf)
|
| 43 |
+
ihilbert - Inverse Hilbert transform: sc_diff(x,inf,inf)
|
| 44 |
+
cs_diff - cosh/sinh pseudo-derivative of periodic sequences
|
| 45 |
+
sc_diff - sinh/cosh pseudo-derivative of periodic sequences
|
| 46 |
+
ss_diff - sinh/sinh pseudo-derivative of periodic sequences
|
| 47 |
+
cc_diff - cosh/cosh pseudo-derivative of periodic sequences
|
| 48 |
+
shift - Shift periodic sequences
|
| 49 |
+
|
| 50 |
+
Helper functions
|
| 51 |
+
================
|
| 52 |
+
|
| 53 |
+
.. autosummary::
|
| 54 |
+
:toctree: generated/
|
| 55 |
+
|
| 56 |
+
fftshift - Shift the zero-frequency component to the center of the spectrum
|
| 57 |
+
ifftshift - The inverse of `fftshift`
|
| 58 |
+
fftfreq - Return the Discrete Fourier Transform sample frequencies
|
| 59 |
+
rfftfreq - DFT sample frequencies (for usage with rfft, irfft)
|
| 60 |
+
next_fast_len - Find the optimal length to zero-pad an FFT for speed
|
| 61 |
+
|
| 62 |
+
Note that ``fftshift``, ``ifftshift`` and ``fftfreq`` are numpy functions
|
| 63 |
+
exposed by ``fftpack``; importing them from ``numpy`` should be preferred.
|
| 64 |
+
|
| 65 |
+
Convolutions (:mod:`scipy.fftpack.convolve`)
|
| 66 |
+
============================================
|
| 67 |
+
|
| 68 |
+
.. module:: scipy.fftpack.convolve
|
| 69 |
+
|
| 70 |
+
.. autosummary::
|
| 71 |
+
:toctree: generated/
|
| 72 |
+
|
| 73 |
+
convolve
|
| 74 |
+
convolve_z
|
| 75 |
+
init_convolution_kernel
|
| 76 |
+
destroy_convolve_cache
|
| 77 |
+
|
| 78 |
+
"""
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
__all__ = ['fft','ifft','fftn','ifftn','rfft','irfft',
|
| 82 |
+
'fft2','ifft2',
|
| 83 |
+
'diff',
|
| 84 |
+
'tilbert','itilbert','hilbert','ihilbert',
|
| 85 |
+
'sc_diff','cs_diff','cc_diff','ss_diff',
|
| 86 |
+
'shift',
|
| 87 |
+
'fftfreq', 'rfftfreq',
|
| 88 |
+
'fftshift', 'ifftshift',
|
| 89 |
+
'next_fast_len',
|
| 90 |
+
'dct', 'idct', 'dst', 'idst', 'dctn', 'idctn', 'dstn', 'idstn'
|
| 91 |
+
]
|
| 92 |
+
|
| 93 |
+
from ._basic import *
|
| 94 |
+
from ._pseudo_diffs import *
|
| 95 |
+
from ._helper import *
|
| 96 |
+
from ._realtransforms import *
|
| 97 |
+
|
| 98 |
+
# Deprecated namespaces, to be removed in v2.0.0
|
| 99 |
+
from . import basic, helper, pseudo_diffs, realtransforms
|
| 100 |
+
|
| 101 |
+
from scipy._lib._testutils import PytestTester
|
| 102 |
+
test = PytestTester(__name__)
|
| 103 |
+
del PytestTester
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/_basic.py
ADDED
|
@@ -0,0 +1,428 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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','fftn','ifftn','rfft','irfft',
|
| 6 |
+
'fft2','ifft2']
|
| 7 |
+
|
| 8 |
+
from scipy.fft import _pocketfft
|
| 9 |
+
from ._helper import _good_shape
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def fft(x, n=None, axis=-1, overwrite_x=False):
|
| 13 |
+
"""
|
| 14 |
+
Return discrete Fourier transform of real or complex sequence.
|
| 15 |
+
|
| 16 |
+
The returned complex array contains ``y(0), y(1),..., y(n-1)``, where
|
| 17 |
+
|
| 18 |
+
``y(j) = (x * exp(-2*pi*sqrt(-1)*j*np.arange(n)/n)).sum()``.
|
| 19 |
+
|
| 20 |
+
Parameters
|
| 21 |
+
----------
|
| 22 |
+
x : array_like
|
| 23 |
+
Array to Fourier transform.
|
| 24 |
+
n : int, optional
|
| 25 |
+
Length of the Fourier transform. If ``n < x.shape[axis]``, `x` is
|
| 26 |
+
truncated. If ``n > x.shape[axis]``, `x` is zero-padded. The
|
| 27 |
+
default results in ``n = x.shape[axis]``.
|
| 28 |
+
axis : int, optional
|
| 29 |
+
Axis along which the fft's are computed; the default is over the
|
| 30 |
+
last axis (i.e., ``axis=-1``).
|
| 31 |
+
overwrite_x : bool, optional
|
| 32 |
+
If True, the contents of `x` can be destroyed; the default is False.
|
| 33 |
+
|
| 34 |
+
Returns
|
| 35 |
+
-------
|
| 36 |
+
z : complex ndarray
|
| 37 |
+
with the elements::
|
| 38 |
+
|
| 39 |
+
[y(0),y(1),..,y(n/2),y(1-n/2),...,y(-1)] if n is even
|
| 40 |
+
[y(0),y(1),..,y((n-1)/2),y(-(n-1)/2),...,y(-1)] if n is odd
|
| 41 |
+
|
| 42 |
+
where::
|
| 43 |
+
|
| 44 |
+
y(j) = sum[k=0..n-1] x[k] * exp(-sqrt(-1)*j*k* 2*pi/n), j = 0..n-1
|
| 45 |
+
|
| 46 |
+
See Also
|
| 47 |
+
--------
|
| 48 |
+
ifft : Inverse FFT
|
| 49 |
+
rfft : FFT of a real sequence
|
| 50 |
+
|
| 51 |
+
Notes
|
| 52 |
+
-----
|
| 53 |
+
The packing of the result is "standard": If ``A = fft(a, n)``, then
|
| 54 |
+
``A[0]`` contains the zero-frequency term, ``A[1:n/2]`` contains the
|
| 55 |
+
positive-frequency terms, and ``A[n/2:]`` contains the negative-frequency
|
| 56 |
+
terms, in order of decreasingly negative frequency. So ,for an 8-point
|
| 57 |
+
transform, the frequencies of the result are [0, 1, 2, 3, -4, -3, -2, -1].
|
| 58 |
+
To rearrange the fft output so that the zero-frequency component is
|
| 59 |
+
centered, like [-4, -3, -2, -1, 0, 1, 2, 3], use `fftshift`.
|
| 60 |
+
|
| 61 |
+
Both single and double precision routines are implemented. Half precision
|
| 62 |
+
inputs will be converted to single precision. Non-floating-point inputs
|
| 63 |
+
will be converted to double precision. Long-double precision inputs are
|
| 64 |
+
not supported.
|
| 65 |
+
|
| 66 |
+
This function is most efficient when `n` is a power of two, and least
|
| 67 |
+
efficient when `n` is prime.
|
| 68 |
+
|
| 69 |
+
Note that if ``x`` is real-valued, then ``A[j] == A[n-j].conjugate()``.
|
| 70 |
+
If ``x`` is real-valued and ``n`` is even, then ``A[n/2]`` is real.
|
| 71 |
+
|
| 72 |
+
If the data type of `x` is real, a "real FFT" algorithm is automatically
|
| 73 |
+
used, which roughly halves the computation time. To increase efficiency
|
| 74 |
+
a little further, use `rfft`, which does the same calculation, but only
|
| 75 |
+
outputs half of the symmetrical spectrum. If the data is both real and
|
| 76 |
+
symmetrical, the `dct` can again double the efficiency by generating
|
| 77 |
+
half of the spectrum from half of the signal.
|
| 78 |
+
|
| 79 |
+
Examples
|
| 80 |
+
--------
|
| 81 |
+
>>> import numpy as np
|
| 82 |
+
>>> from scipy.fftpack import fft, ifft
|
| 83 |
+
>>> x = np.arange(5)
|
| 84 |
+
>>> np.allclose(fft(ifft(x)), x, atol=1e-15) # within numerical accuracy.
|
| 85 |
+
True
|
| 86 |
+
|
| 87 |
+
"""
|
| 88 |
+
return _pocketfft.fft(x, n, axis, None, overwrite_x)
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def ifft(x, n=None, axis=-1, overwrite_x=False):
|
| 92 |
+
"""
|
| 93 |
+
Return discrete inverse Fourier transform of real or complex sequence.
|
| 94 |
+
|
| 95 |
+
The returned complex array contains ``y(0), y(1),..., y(n-1)``, where
|
| 96 |
+
|
| 97 |
+
``y(j) = (x * exp(2*pi*sqrt(-1)*j*np.arange(n)/n)).mean()``.
|
| 98 |
+
|
| 99 |
+
Parameters
|
| 100 |
+
----------
|
| 101 |
+
x : array_like
|
| 102 |
+
Transformed data to invert.
|
| 103 |
+
n : int, optional
|
| 104 |
+
Length of the inverse Fourier transform. If ``n < x.shape[axis]``,
|
| 105 |
+
`x` is truncated. If ``n > x.shape[axis]``, `x` is zero-padded.
|
| 106 |
+
The default results in ``n = x.shape[axis]``.
|
| 107 |
+
axis : int, optional
|
| 108 |
+
Axis along which the ifft's are computed; the default is over the
|
| 109 |
+
last axis (i.e., ``axis=-1``).
|
| 110 |
+
overwrite_x : bool, optional
|
| 111 |
+
If True, the contents of `x` can be destroyed; the default is False.
|
| 112 |
+
|
| 113 |
+
Returns
|
| 114 |
+
-------
|
| 115 |
+
ifft : ndarray of floats
|
| 116 |
+
The inverse discrete Fourier transform.
|
| 117 |
+
|
| 118 |
+
See Also
|
| 119 |
+
--------
|
| 120 |
+
fft : Forward FFT
|
| 121 |
+
|
| 122 |
+
Notes
|
| 123 |
+
-----
|
| 124 |
+
Both single and double precision routines are implemented. Half precision
|
| 125 |
+
inputs will be converted to single precision. Non-floating-point inputs
|
| 126 |
+
will be converted to double precision. Long-double precision inputs are
|
| 127 |
+
not supported.
|
| 128 |
+
|
| 129 |
+
This function is most efficient when `n` is a power of two, and least
|
| 130 |
+
efficient when `n` is prime.
|
| 131 |
+
|
| 132 |
+
If the data type of `x` is real, a "real IFFT" algorithm is automatically
|
| 133 |
+
used, which roughly halves the computation time.
|
| 134 |
+
|
| 135 |
+
Examples
|
| 136 |
+
--------
|
| 137 |
+
>>> from scipy.fftpack import fft, ifft
|
| 138 |
+
>>> import numpy as np
|
| 139 |
+
>>> x = np.arange(5)
|
| 140 |
+
>>> np.allclose(ifft(fft(x)), x, atol=1e-15) # within numerical accuracy.
|
| 141 |
+
True
|
| 142 |
+
|
| 143 |
+
"""
|
| 144 |
+
return _pocketfft.ifft(x, n, axis, None, overwrite_x)
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
def rfft(x, n=None, axis=-1, overwrite_x=False):
|
| 148 |
+
"""
|
| 149 |
+
Discrete Fourier transform of a real sequence.
|
| 150 |
+
|
| 151 |
+
Parameters
|
| 152 |
+
----------
|
| 153 |
+
x : array_like, real-valued
|
| 154 |
+
The data to transform.
|
| 155 |
+
n : int, optional
|
| 156 |
+
Defines the length of the Fourier transform. If `n` is not specified
|
| 157 |
+
(the default) then ``n = x.shape[axis]``. If ``n < x.shape[axis]``,
|
| 158 |
+
`x` is truncated, if ``n > x.shape[axis]``, `x` is zero-padded.
|
| 159 |
+
axis : int, optional
|
| 160 |
+
The axis along which the transform is applied. The default is the
|
| 161 |
+
last axis.
|
| 162 |
+
overwrite_x : bool, optional
|
| 163 |
+
If set to true, the contents of `x` can be overwritten. Default is
|
| 164 |
+
False.
|
| 165 |
+
|
| 166 |
+
Returns
|
| 167 |
+
-------
|
| 168 |
+
z : real ndarray
|
| 169 |
+
The returned real array contains::
|
| 170 |
+
|
| 171 |
+
[y(0),Re(y(1)),Im(y(1)),...,Re(y(n/2))] if n is even
|
| 172 |
+
[y(0),Re(y(1)),Im(y(1)),...,Re(y(n/2)),Im(y(n/2))] if n is odd
|
| 173 |
+
|
| 174 |
+
where::
|
| 175 |
+
|
| 176 |
+
y(j) = sum[k=0..n-1] x[k] * exp(-sqrt(-1)*j*k*2*pi/n)
|
| 177 |
+
j = 0..n-1
|
| 178 |
+
|
| 179 |
+
See Also
|
| 180 |
+
--------
|
| 181 |
+
fft, irfft, scipy.fft.rfft
|
| 182 |
+
|
| 183 |
+
Notes
|
| 184 |
+
-----
|
| 185 |
+
Within numerical accuracy, ``y == rfft(irfft(y))``.
|
| 186 |
+
|
| 187 |
+
Both single and double precision routines are implemented. Half precision
|
| 188 |
+
inputs will be converted to single precision. Non-floating-point inputs
|
| 189 |
+
will be converted to double precision. Long-double precision inputs are
|
| 190 |
+
not supported.
|
| 191 |
+
|
| 192 |
+
To get an output with a complex datatype, consider using the newer
|
| 193 |
+
function `scipy.fft.rfft`.
|
| 194 |
+
|
| 195 |
+
Examples
|
| 196 |
+
--------
|
| 197 |
+
>>> from scipy.fftpack import fft, rfft
|
| 198 |
+
>>> a = [9, -9, 1, 3]
|
| 199 |
+
>>> fft(a)
|
| 200 |
+
array([ 4. +0.j, 8.+12.j, 16. +0.j, 8.-12.j])
|
| 201 |
+
>>> rfft(a)
|
| 202 |
+
array([ 4., 8., 12., 16.])
|
| 203 |
+
|
| 204 |
+
"""
|
| 205 |
+
return _pocketfft.rfft_fftpack(x, n, axis, None, overwrite_x)
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
def irfft(x, n=None, axis=-1, overwrite_x=False):
|
| 209 |
+
"""
|
| 210 |
+
Return inverse discrete Fourier transform of real sequence x.
|
| 211 |
+
|
| 212 |
+
The contents of `x` are interpreted as the output of the `rfft`
|
| 213 |
+
function.
|
| 214 |
+
|
| 215 |
+
Parameters
|
| 216 |
+
----------
|
| 217 |
+
x : array_like
|
| 218 |
+
Transformed data to invert.
|
| 219 |
+
n : int, optional
|
| 220 |
+
Length of the inverse Fourier transform.
|
| 221 |
+
If n < x.shape[axis], x is truncated.
|
| 222 |
+
If n > x.shape[axis], x is zero-padded.
|
| 223 |
+
The default results in n = x.shape[axis].
|
| 224 |
+
axis : int, optional
|
| 225 |
+
Axis along which the ifft's are computed; the default is over
|
| 226 |
+
the last axis (i.e., axis=-1).
|
| 227 |
+
overwrite_x : bool, optional
|
| 228 |
+
If True, the contents of `x` can be destroyed; the default is False.
|
| 229 |
+
|
| 230 |
+
Returns
|
| 231 |
+
-------
|
| 232 |
+
irfft : ndarray of floats
|
| 233 |
+
The inverse discrete Fourier transform.
|
| 234 |
+
|
| 235 |
+
See Also
|
| 236 |
+
--------
|
| 237 |
+
rfft, ifft, scipy.fft.irfft
|
| 238 |
+
|
| 239 |
+
Notes
|
| 240 |
+
-----
|
| 241 |
+
The returned real array contains::
|
| 242 |
+
|
| 243 |
+
[y(0),y(1),...,y(n-1)]
|
| 244 |
+
|
| 245 |
+
where for n is even::
|
| 246 |
+
|
| 247 |
+
y(j) = 1/n (sum[k=1..n/2-1] (x[2*k-1]+sqrt(-1)*x[2*k])
|
| 248 |
+
* exp(sqrt(-1)*j*k* 2*pi/n)
|
| 249 |
+
+ c.c. + x[0] + (-1)**(j) x[n-1])
|
| 250 |
+
|
| 251 |
+
and for n is odd::
|
| 252 |
+
|
| 253 |
+
y(j) = 1/n (sum[k=1..(n-1)/2] (x[2*k-1]+sqrt(-1)*x[2*k])
|
| 254 |
+
* exp(sqrt(-1)*j*k* 2*pi/n)
|
| 255 |
+
+ c.c. + x[0])
|
| 256 |
+
|
| 257 |
+
c.c. denotes complex conjugate of preceding expression.
|
| 258 |
+
|
| 259 |
+
For details on input parameters, see `rfft`.
|
| 260 |
+
|
| 261 |
+
To process (conjugate-symmetric) frequency-domain data with a complex
|
| 262 |
+
datatype, consider using the newer function `scipy.fft.irfft`.
|
| 263 |
+
|
| 264 |
+
Examples
|
| 265 |
+
--------
|
| 266 |
+
>>> from scipy.fftpack import rfft, irfft
|
| 267 |
+
>>> a = [1.0, 2.0, 3.0, 4.0, 5.0]
|
| 268 |
+
>>> irfft(a)
|
| 269 |
+
array([ 2.6 , -3.16405192, 1.24398433, -1.14955713, 1.46962473])
|
| 270 |
+
>>> irfft(rfft(a))
|
| 271 |
+
array([1., 2., 3., 4., 5.])
|
| 272 |
+
|
| 273 |
+
"""
|
| 274 |
+
return _pocketfft.irfft_fftpack(x, n, axis, None, overwrite_x)
|
| 275 |
+
|
| 276 |
+
|
| 277 |
+
def fftn(x, shape=None, axes=None, overwrite_x=False):
|
| 278 |
+
"""
|
| 279 |
+
Return multidimensional discrete Fourier transform.
|
| 280 |
+
|
| 281 |
+
The returned array contains::
|
| 282 |
+
|
| 283 |
+
y[j_1,..,j_d] = sum[k_1=0..n_1-1, ..., k_d=0..n_d-1]
|
| 284 |
+
x[k_1,..,k_d] * prod[i=1..d] exp(-sqrt(-1)*2*pi/n_i * j_i * k_i)
|
| 285 |
+
|
| 286 |
+
where d = len(x.shape) and n = x.shape.
|
| 287 |
+
|
| 288 |
+
Parameters
|
| 289 |
+
----------
|
| 290 |
+
x : array_like
|
| 291 |
+
The (N-D) array to transform.
|
| 292 |
+
shape : int or array_like of ints or None, optional
|
| 293 |
+
The shape of the result. If both `shape` and `axes` (see below) are
|
| 294 |
+
None, `shape` is ``x.shape``; if `shape` is None but `axes` is
|
| 295 |
+
not None, then `shape` is ``numpy.take(x.shape, axes, axis=0)``.
|
| 296 |
+
If ``shape[i] > x.shape[i]``, the ith dimension is padded with zeros.
|
| 297 |
+
If ``shape[i] < x.shape[i]``, the ith dimension is truncated to
|
| 298 |
+
length ``shape[i]``.
|
| 299 |
+
If any element of `shape` is -1, the size of the corresponding
|
| 300 |
+
dimension of `x` is used.
|
| 301 |
+
axes : int or array_like of ints or None, optional
|
| 302 |
+
The axes of `x` (`y` if `shape` is not None) along which the
|
| 303 |
+
transform is applied.
|
| 304 |
+
The default is over all axes.
|
| 305 |
+
overwrite_x : bool, optional
|
| 306 |
+
If True, the contents of `x` can be destroyed. Default is False.
|
| 307 |
+
|
| 308 |
+
Returns
|
| 309 |
+
-------
|
| 310 |
+
y : complex-valued N-D NumPy array
|
| 311 |
+
The (N-D) DFT of the input array.
|
| 312 |
+
|
| 313 |
+
See Also
|
| 314 |
+
--------
|
| 315 |
+
ifftn
|
| 316 |
+
|
| 317 |
+
Notes
|
| 318 |
+
-----
|
| 319 |
+
If ``x`` is real-valued, then
|
| 320 |
+
``y[..., j_i, ...] == y[..., n_i-j_i, ...].conjugate()``.
|
| 321 |
+
|
| 322 |
+
Both single and double precision routines are implemented. Half precision
|
| 323 |
+
inputs will be converted to single precision. Non-floating-point inputs
|
| 324 |
+
will be converted to double precision. Long-double precision inputs are
|
| 325 |
+
not supported.
|
| 326 |
+
|
| 327 |
+
Examples
|
| 328 |
+
--------
|
| 329 |
+
>>> import numpy as np
|
| 330 |
+
>>> from scipy.fftpack import fftn, ifftn
|
| 331 |
+
>>> y = (-np.arange(16), 8 - np.arange(16), np.arange(16))
|
| 332 |
+
>>> np.allclose(y, fftn(ifftn(y)))
|
| 333 |
+
True
|
| 334 |
+
|
| 335 |
+
"""
|
| 336 |
+
shape = _good_shape(x, shape, axes)
|
| 337 |
+
return _pocketfft.fftn(x, shape, axes, None, overwrite_x)
|
| 338 |
+
|
| 339 |
+
|
| 340 |
+
def ifftn(x, shape=None, axes=None, overwrite_x=False):
|
| 341 |
+
"""
|
| 342 |
+
Return inverse multidimensional discrete Fourier transform.
|
| 343 |
+
|
| 344 |
+
The sequence can be of an arbitrary type.
|
| 345 |
+
|
| 346 |
+
The returned array contains::
|
| 347 |
+
|
| 348 |
+
y[j_1,..,j_d] = 1/p * sum[k_1=0..n_1-1, ..., k_d=0..n_d-1]
|
| 349 |
+
x[k_1,..,k_d] * prod[i=1..d] exp(sqrt(-1)*2*pi/n_i * j_i * k_i)
|
| 350 |
+
|
| 351 |
+
where ``d = len(x.shape)``, ``n = x.shape``, and ``p = prod[i=1..d] n_i``.
|
| 352 |
+
|
| 353 |
+
For description of parameters see `fftn`.
|
| 354 |
+
|
| 355 |
+
See Also
|
| 356 |
+
--------
|
| 357 |
+
fftn : for detailed information.
|
| 358 |
+
|
| 359 |
+
Examples
|
| 360 |
+
--------
|
| 361 |
+
>>> from scipy.fftpack import fftn, ifftn
|
| 362 |
+
>>> import numpy as np
|
| 363 |
+
>>> y = (-np.arange(16), 8 - np.arange(16), np.arange(16))
|
| 364 |
+
>>> np.allclose(y, ifftn(fftn(y)))
|
| 365 |
+
True
|
| 366 |
+
|
| 367 |
+
"""
|
| 368 |
+
shape = _good_shape(x, shape, axes)
|
| 369 |
+
return _pocketfft.ifftn(x, shape, axes, None, overwrite_x)
|
| 370 |
+
|
| 371 |
+
|
| 372 |
+
def fft2(x, shape=None, axes=(-2,-1), overwrite_x=False):
|
| 373 |
+
"""
|
| 374 |
+
2-D discrete Fourier transform.
|
| 375 |
+
|
| 376 |
+
Return the 2-D discrete Fourier transform of the 2-D argument
|
| 377 |
+
`x`.
|
| 378 |
+
|
| 379 |
+
See Also
|
| 380 |
+
--------
|
| 381 |
+
fftn : for detailed information.
|
| 382 |
+
|
| 383 |
+
Examples
|
| 384 |
+
--------
|
| 385 |
+
>>> import numpy as np
|
| 386 |
+
>>> from scipy.fftpack import fft2, ifft2
|
| 387 |
+
>>> y = np.mgrid[:5, :5][0]
|
| 388 |
+
>>> y
|
| 389 |
+
array([[0, 0, 0, 0, 0],
|
| 390 |
+
[1, 1, 1, 1, 1],
|
| 391 |
+
[2, 2, 2, 2, 2],
|
| 392 |
+
[3, 3, 3, 3, 3],
|
| 393 |
+
[4, 4, 4, 4, 4]])
|
| 394 |
+
>>> np.allclose(y, ifft2(fft2(y)))
|
| 395 |
+
True
|
| 396 |
+
"""
|
| 397 |
+
return fftn(x,shape,axes,overwrite_x)
|
| 398 |
+
|
| 399 |
+
|
| 400 |
+
def ifft2(x, shape=None, axes=(-2,-1), overwrite_x=False):
|
| 401 |
+
"""
|
| 402 |
+
2-D discrete inverse Fourier transform of real or complex sequence.
|
| 403 |
+
|
| 404 |
+
Return inverse 2-D discrete Fourier transform of
|
| 405 |
+
arbitrary type sequence x.
|
| 406 |
+
|
| 407 |
+
See `ifft` for more information.
|
| 408 |
+
|
| 409 |
+
See Also
|
| 410 |
+
--------
|
| 411 |
+
fft2, ifft
|
| 412 |
+
|
| 413 |
+
Examples
|
| 414 |
+
--------
|
| 415 |
+
>>> import numpy as np
|
| 416 |
+
>>> from scipy.fftpack import fft2, ifft2
|
| 417 |
+
>>> y = np.mgrid[:5, :5][0]
|
| 418 |
+
>>> y
|
| 419 |
+
array([[0, 0, 0, 0, 0],
|
| 420 |
+
[1, 1, 1, 1, 1],
|
| 421 |
+
[2, 2, 2, 2, 2],
|
| 422 |
+
[3, 3, 3, 3, 3],
|
| 423 |
+
[4, 4, 4, 4, 4]])
|
| 424 |
+
>>> np.allclose(y, fft2(ifft2(y)))
|
| 425 |
+
True
|
| 426 |
+
|
| 427 |
+
"""
|
| 428 |
+
return ifftn(x,shape,axes,overwrite_x)
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/_helper.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import operator
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
from numpy.fft import fftshift, ifftshift, fftfreq
|
| 5 |
+
|
| 6 |
+
import scipy.fft._pocketfft.helper as _helper
|
| 7 |
+
|
| 8 |
+
__all__ = ['fftshift', 'ifftshift', 'fftfreq', 'rfftfreq', 'next_fast_len']
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def rfftfreq(n, d=1.0):
|
| 12 |
+
"""DFT sample frequencies (for usage with rfft, irfft).
|
| 13 |
+
|
| 14 |
+
The returned float array contains the frequency bins in
|
| 15 |
+
cycles/unit (with zero at the start) given a window length `n` and a
|
| 16 |
+
sample spacing `d`::
|
| 17 |
+
|
| 18 |
+
f = [0,1,1,2,2,...,n/2-1,n/2-1,n/2]/(d*n) if n is even
|
| 19 |
+
f = [0,1,1,2,2,...,n/2-1,n/2-1,n/2,n/2]/(d*n) if n is odd
|
| 20 |
+
|
| 21 |
+
Parameters
|
| 22 |
+
----------
|
| 23 |
+
n : int
|
| 24 |
+
Window length.
|
| 25 |
+
d : scalar, optional
|
| 26 |
+
Sample spacing. Default is 1.
|
| 27 |
+
|
| 28 |
+
Returns
|
| 29 |
+
-------
|
| 30 |
+
out : ndarray
|
| 31 |
+
The array of length `n`, containing the sample frequencies.
|
| 32 |
+
|
| 33 |
+
Examples
|
| 34 |
+
--------
|
| 35 |
+
>>> import numpy as np
|
| 36 |
+
>>> from scipy import fftpack
|
| 37 |
+
>>> sig = np.array([-2, 8, 6, 4, 1, 0, 3, 5], dtype=float)
|
| 38 |
+
>>> sig_fft = fftpack.rfft(sig)
|
| 39 |
+
>>> n = sig_fft.size
|
| 40 |
+
>>> timestep = 0.1
|
| 41 |
+
>>> freq = fftpack.rfftfreq(n, d=timestep)
|
| 42 |
+
>>> freq
|
| 43 |
+
array([ 0. , 1.25, 1.25, 2.5 , 2.5 , 3.75, 3.75, 5. ])
|
| 44 |
+
|
| 45 |
+
"""
|
| 46 |
+
n = operator.index(n)
|
| 47 |
+
if n < 0:
|
| 48 |
+
raise ValueError(f"n = {n} is not valid. "
|
| 49 |
+
"n must be a nonnegative integer.")
|
| 50 |
+
|
| 51 |
+
return (np.arange(1, n + 1, dtype=int) // 2) / float(n * d)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def next_fast_len(target):
|
| 55 |
+
"""
|
| 56 |
+
Find the next fast size of input data to `fft`, for zero-padding, etc.
|
| 57 |
+
|
| 58 |
+
SciPy's FFTPACK has efficient functions for radix {2, 3, 4, 5}, so this
|
| 59 |
+
returns the next composite of the prime factors 2, 3, and 5 which is
|
| 60 |
+
greater than or equal to `target`. (These are also known as 5-smooth
|
| 61 |
+
numbers, regular numbers, or Hamming numbers.)
|
| 62 |
+
|
| 63 |
+
Parameters
|
| 64 |
+
----------
|
| 65 |
+
target : int
|
| 66 |
+
Length to start searching from. Must be a positive integer.
|
| 67 |
+
|
| 68 |
+
Returns
|
| 69 |
+
-------
|
| 70 |
+
out : int
|
| 71 |
+
The first 5-smooth number greater than or equal to `target`.
|
| 72 |
+
|
| 73 |
+
Notes
|
| 74 |
+
-----
|
| 75 |
+
.. versionadded:: 0.18.0
|
| 76 |
+
|
| 77 |
+
Examples
|
| 78 |
+
--------
|
| 79 |
+
On a particular machine, an FFT of prime length takes 133 ms:
|
| 80 |
+
|
| 81 |
+
>>> from scipy import fftpack
|
| 82 |
+
>>> import numpy as np
|
| 83 |
+
>>> rng = np.random.default_rng()
|
| 84 |
+
>>> min_len = 10007 # prime length is worst case for speed
|
| 85 |
+
>>> a = rng.standard_normal(min_len)
|
| 86 |
+
>>> b = fftpack.fft(a)
|
| 87 |
+
|
| 88 |
+
Zero-padding to the next 5-smooth length reduces computation time to
|
| 89 |
+
211 us, a speedup of 630 times:
|
| 90 |
+
|
| 91 |
+
>>> fftpack.next_fast_len(min_len)
|
| 92 |
+
10125
|
| 93 |
+
>>> b = fftpack.fft(a, 10125)
|
| 94 |
+
|
| 95 |
+
Rounding up to the next power of 2 is not optimal, taking 367 us to
|
| 96 |
+
compute, 1.7 times as long as the 5-smooth size:
|
| 97 |
+
|
| 98 |
+
>>> b = fftpack.fft(a, 16384)
|
| 99 |
+
|
| 100 |
+
"""
|
| 101 |
+
# Real transforms use regular sizes so this is backwards compatible
|
| 102 |
+
return _helper.good_size(target, True)
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def _good_shape(x, shape, axes):
|
| 106 |
+
"""Ensure that shape argument is valid for scipy.fftpack
|
| 107 |
+
|
| 108 |
+
scipy.fftpack does not support len(shape) < x.ndim when axes is not given.
|
| 109 |
+
"""
|
| 110 |
+
if shape is not None and axes is None:
|
| 111 |
+
shape = _helper._iterable_of_int(shape, 'shape')
|
| 112 |
+
if len(shape) != np.ndim(x):
|
| 113 |
+
raise ValueError("when given, axes and shape arguments"
|
| 114 |
+
" have to be of the same length")
|
| 115 |
+
return shape
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/_pseudo_diffs.py
ADDED
|
@@ -0,0 +1,554 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Differential and pseudo-differential operators.
|
| 3 |
+
"""
|
| 4 |
+
# Created by Pearu Peterson, September 2002
|
| 5 |
+
|
| 6 |
+
__all__ = ['diff',
|
| 7 |
+
'tilbert','itilbert','hilbert','ihilbert',
|
| 8 |
+
'cs_diff','cc_diff','sc_diff','ss_diff',
|
| 9 |
+
'shift']
|
| 10 |
+
|
| 11 |
+
import threading
|
| 12 |
+
|
| 13 |
+
from numpy import pi, asarray, sin, cos, sinh, cosh, tanh, iscomplexobj
|
| 14 |
+
from . import convolve
|
| 15 |
+
|
| 16 |
+
from scipy.fft._pocketfft.helper import _datacopied
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
_cache = threading.local()
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def diff(x,order=1,period=None, _cache=_cache):
|
| 23 |
+
"""
|
| 24 |
+
Return kth derivative (or integral) of a periodic sequence x.
|
| 25 |
+
|
| 26 |
+
If x_j and y_j are Fourier coefficients of periodic functions x
|
| 27 |
+
and y, respectively, then::
|
| 28 |
+
|
| 29 |
+
y_j = pow(sqrt(-1)*j*2*pi/period, order) * x_j
|
| 30 |
+
y_0 = 0 if order is not 0.
|
| 31 |
+
|
| 32 |
+
Parameters
|
| 33 |
+
----------
|
| 34 |
+
x : array_like
|
| 35 |
+
Input array.
|
| 36 |
+
order : int, optional
|
| 37 |
+
The order of differentiation. Default order is 1. If order is
|
| 38 |
+
negative, then integration is carried out under the assumption
|
| 39 |
+
that ``x_0 == 0``.
|
| 40 |
+
period : float, optional
|
| 41 |
+
The assumed period of the sequence. Default is ``2*pi``.
|
| 42 |
+
|
| 43 |
+
Notes
|
| 44 |
+
-----
|
| 45 |
+
If ``sum(x, axis=0) = 0`` then ``diff(diff(x, k), -k) == x`` (within
|
| 46 |
+
numerical accuracy).
|
| 47 |
+
|
| 48 |
+
For odd order and even ``len(x)``, the Nyquist mode is taken zero.
|
| 49 |
+
|
| 50 |
+
"""
|
| 51 |
+
if isinstance(_cache, threading.local):
|
| 52 |
+
if not hasattr(_cache, 'diff_cache'):
|
| 53 |
+
_cache.diff_cache = {}
|
| 54 |
+
_cache = _cache.diff_cache
|
| 55 |
+
|
| 56 |
+
tmp = asarray(x)
|
| 57 |
+
if order == 0:
|
| 58 |
+
return tmp
|
| 59 |
+
if iscomplexobj(tmp):
|
| 60 |
+
return diff(tmp.real, order, period, _cache)+1j*diff(
|
| 61 |
+
tmp.imag, order, period, _cache)
|
| 62 |
+
if period is not None:
|
| 63 |
+
c = 2*pi/period
|
| 64 |
+
else:
|
| 65 |
+
c = 1.0
|
| 66 |
+
n = len(x)
|
| 67 |
+
omega = _cache.get((n,order,c))
|
| 68 |
+
if omega is None:
|
| 69 |
+
if len(_cache) > 20:
|
| 70 |
+
while _cache:
|
| 71 |
+
_cache.popitem()
|
| 72 |
+
|
| 73 |
+
def kernel(k,order=order,c=c):
|
| 74 |
+
if k:
|
| 75 |
+
return pow(c*k,order)
|
| 76 |
+
return 0
|
| 77 |
+
omega = convolve.init_convolution_kernel(n,kernel,d=order,
|
| 78 |
+
zero_nyquist=1)
|
| 79 |
+
_cache[(n,order,c)] = omega
|
| 80 |
+
overwrite_x = _datacopied(tmp, x)
|
| 81 |
+
return convolve.convolve(tmp,omega,swap_real_imag=order % 2,
|
| 82 |
+
overwrite_x=overwrite_x)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def tilbert(x, h, period=None, _cache=_cache):
|
| 86 |
+
"""
|
| 87 |
+
Return h-Tilbert transform of a periodic sequence x.
|
| 88 |
+
|
| 89 |
+
If x_j and y_j are Fourier coefficients of periodic functions x
|
| 90 |
+
and y, respectively, then::
|
| 91 |
+
|
| 92 |
+
y_j = sqrt(-1)*coth(j*h*2*pi/period) * x_j
|
| 93 |
+
y_0 = 0
|
| 94 |
+
|
| 95 |
+
Parameters
|
| 96 |
+
----------
|
| 97 |
+
x : array_like
|
| 98 |
+
The input array to transform.
|
| 99 |
+
h : float
|
| 100 |
+
Defines the parameter of the Tilbert transform.
|
| 101 |
+
period : float, optional
|
| 102 |
+
The assumed period of the sequence. Default period is ``2*pi``.
|
| 103 |
+
|
| 104 |
+
Returns
|
| 105 |
+
-------
|
| 106 |
+
tilbert : ndarray
|
| 107 |
+
The result of the transform.
|
| 108 |
+
|
| 109 |
+
Notes
|
| 110 |
+
-----
|
| 111 |
+
If ``sum(x, axis=0) == 0`` and ``n = len(x)`` is odd, then
|
| 112 |
+
``tilbert(itilbert(x)) == x``.
|
| 113 |
+
|
| 114 |
+
If ``2 * pi * h / period`` is approximately 10 or larger, then
|
| 115 |
+
numerically ``tilbert == hilbert``
|
| 116 |
+
(theoretically oo-Tilbert == Hilbert).
|
| 117 |
+
|
| 118 |
+
For even ``len(x)``, the Nyquist mode of ``x`` is taken zero.
|
| 119 |
+
|
| 120 |
+
"""
|
| 121 |
+
if isinstance(_cache, threading.local):
|
| 122 |
+
if not hasattr(_cache, 'tilbert_cache'):
|
| 123 |
+
_cache.tilbert_cache = {}
|
| 124 |
+
_cache = _cache.tilbert_cache
|
| 125 |
+
|
| 126 |
+
tmp = asarray(x)
|
| 127 |
+
if iscomplexobj(tmp):
|
| 128 |
+
return tilbert(tmp.real, h, period, _cache) + \
|
| 129 |
+
1j * tilbert(tmp.imag, h, period, _cache)
|
| 130 |
+
|
| 131 |
+
if period is not None:
|
| 132 |
+
h = h * 2 * pi / period
|
| 133 |
+
|
| 134 |
+
n = len(x)
|
| 135 |
+
omega = _cache.get((n, h))
|
| 136 |
+
if omega is None:
|
| 137 |
+
if len(_cache) > 20:
|
| 138 |
+
while _cache:
|
| 139 |
+
_cache.popitem()
|
| 140 |
+
|
| 141 |
+
def kernel(k, h=h):
|
| 142 |
+
if k:
|
| 143 |
+
return 1.0/tanh(h*k)
|
| 144 |
+
|
| 145 |
+
return 0
|
| 146 |
+
|
| 147 |
+
omega = convolve.init_convolution_kernel(n, kernel, d=1)
|
| 148 |
+
_cache[(n,h)] = omega
|
| 149 |
+
|
| 150 |
+
overwrite_x = _datacopied(tmp, x)
|
| 151 |
+
return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x)
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
def itilbert(x,h,period=None, _cache=_cache):
|
| 155 |
+
"""
|
| 156 |
+
Return inverse h-Tilbert transform of a periodic sequence x.
|
| 157 |
+
|
| 158 |
+
If ``x_j`` and ``y_j`` are Fourier coefficients of periodic functions x
|
| 159 |
+
and y, respectively, then::
|
| 160 |
+
|
| 161 |
+
y_j = -sqrt(-1)*tanh(j*h*2*pi/period) * x_j
|
| 162 |
+
y_0 = 0
|
| 163 |
+
|
| 164 |
+
For more details, see `tilbert`.
|
| 165 |
+
|
| 166 |
+
"""
|
| 167 |
+
if isinstance(_cache, threading.local):
|
| 168 |
+
if not hasattr(_cache, 'itilbert_cache'):
|
| 169 |
+
_cache.itilbert_cache = {}
|
| 170 |
+
_cache = _cache.itilbert_cache
|
| 171 |
+
|
| 172 |
+
tmp = asarray(x)
|
| 173 |
+
if iscomplexobj(tmp):
|
| 174 |
+
return itilbert(tmp.real, h, period, _cache) + \
|
| 175 |
+
1j*itilbert(tmp.imag, h, period, _cache)
|
| 176 |
+
if period is not None:
|
| 177 |
+
h = h*2*pi/period
|
| 178 |
+
n = len(x)
|
| 179 |
+
omega = _cache.get((n,h))
|
| 180 |
+
if omega is None:
|
| 181 |
+
if len(_cache) > 20:
|
| 182 |
+
while _cache:
|
| 183 |
+
_cache.popitem()
|
| 184 |
+
|
| 185 |
+
def kernel(k,h=h):
|
| 186 |
+
if k:
|
| 187 |
+
return -tanh(h*k)
|
| 188 |
+
return 0
|
| 189 |
+
omega = convolve.init_convolution_kernel(n,kernel,d=1)
|
| 190 |
+
_cache[(n,h)] = omega
|
| 191 |
+
overwrite_x = _datacopied(tmp, x)
|
| 192 |
+
return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x)
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
def hilbert(x, _cache=_cache):
|
| 196 |
+
"""
|
| 197 |
+
Return Hilbert transform of a periodic sequence x.
|
| 198 |
+
|
| 199 |
+
If x_j and y_j are Fourier coefficients of periodic functions x
|
| 200 |
+
and y, respectively, then::
|
| 201 |
+
|
| 202 |
+
y_j = sqrt(-1)*sign(j) * x_j
|
| 203 |
+
y_0 = 0
|
| 204 |
+
|
| 205 |
+
Parameters
|
| 206 |
+
----------
|
| 207 |
+
x : array_like
|
| 208 |
+
The input array, should be periodic.
|
| 209 |
+
_cache : dict, optional
|
| 210 |
+
Dictionary that contains the kernel used to do a convolution with.
|
| 211 |
+
|
| 212 |
+
Returns
|
| 213 |
+
-------
|
| 214 |
+
y : ndarray
|
| 215 |
+
The transformed input.
|
| 216 |
+
|
| 217 |
+
See Also
|
| 218 |
+
--------
|
| 219 |
+
scipy.signal.hilbert : Compute the analytic signal, using the Hilbert
|
| 220 |
+
transform.
|
| 221 |
+
|
| 222 |
+
Notes
|
| 223 |
+
-----
|
| 224 |
+
If ``sum(x, axis=0) == 0`` then ``hilbert(ihilbert(x)) == x``.
|
| 225 |
+
|
| 226 |
+
For even len(x), the Nyquist mode of x is taken zero.
|
| 227 |
+
|
| 228 |
+
The sign of the returned transform does not have a factor -1 that is more
|
| 229 |
+
often than not found in the definition of the Hilbert transform. Note also
|
| 230 |
+
that `scipy.signal.hilbert` does have an extra -1 factor compared to this
|
| 231 |
+
function.
|
| 232 |
+
|
| 233 |
+
"""
|
| 234 |
+
if isinstance(_cache, threading.local):
|
| 235 |
+
if not hasattr(_cache, 'hilbert_cache'):
|
| 236 |
+
_cache.hilbert_cache = {}
|
| 237 |
+
_cache = _cache.hilbert_cache
|
| 238 |
+
|
| 239 |
+
tmp = asarray(x)
|
| 240 |
+
if iscomplexobj(tmp):
|
| 241 |
+
return hilbert(tmp.real, _cache) + 1j * hilbert(tmp.imag, _cache)
|
| 242 |
+
n = len(x)
|
| 243 |
+
omega = _cache.get(n)
|
| 244 |
+
if omega is None:
|
| 245 |
+
if len(_cache) > 20:
|
| 246 |
+
while _cache:
|
| 247 |
+
_cache.popitem()
|
| 248 |
+
|
| 249 |
+
def kernel(k):
|
| 250 |
+
if k > 0:
|
| 251 |
+
return 1.0
|
| 252 |
+
elif k < 0:
|
| 253 |
+
return -1.0
|
| 254 |
+
return 0.0
|
| 255 |
+
omega = convolve.init_convolution_kernel(n,kernel,d=1)
|
| 256 |
+
_cache[n] = omega
|
| 257 |
+
overwrite_x = _datacopied(tmp, x)
|
| 258 |
+
return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x)
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
def ihilbert(x, _cache=_cache):
|
| 262 |
+
"""
|
| 263 |
+
Return inverse Hilbert transform of a periodic sequence x.
|
| 264 |
+
|
| 265 |
+
If ``x_j`` and ``y_j`` are Fourier coefficients of periodic functions x
|
| 266 |
+
and y, respectively, then::
|
| 267 |
+
|
| 268 |
+
y_j = -sqrt(-1)*sign(j) * x_j
|
| 269 |
+
y_0 = 0
|
| 270 |
+
|
| 271 |
+
"""
|
| 272 |
+
if isinstance(_cache, threading.local):
|
| 273 |
+
if not hasattr(_cache, 'ihilbert_cache'):
|
| 274 |
+
_cache.ihilbert_cache = {}
|
| 275 |
+
_cache = _cache.ihilbert_cache
|
| 276 |
+
return -hilbert(x, _cache)
|
| 277 |
+
|
| 278 |
+
|
| 279 |
+
def cs_diff(x, a, b, period=None, _cache=_cache):
|
| 280 |
+
"""
|
| 281 |
+
Return (a,b)-cosh/sinh pseudo-derivative of a periodic sequence.
|
| 282 |
+
|
| 283 |
+
If ``x_j`` and ``y_j`` are Fourier coefficients of periodic functions x
|
| 284 |
+
and y, respectively, then::
|
| 285 |
+
|
| 286 |
+
y_j = -sqrt(-1)*cosh(j*a*2*pi/period)/sinh(j*b*2*pi/period) * x_j
|
| 287 |
+
y_0 = 0
|
| 288 |
+
|
| 289 |
+
Parameters
|
| 290 |
+
----------
|
| 291 |
+
x : array_like
|
| 292 |
+
The array to take the pseudo-derivative from.
|
| 293 |
+
a, b : float
|
| 294 |
+
Defines the parameters of the cosh/sinh pseudo-differential
|
| 295 |
+
operator.
|
| 296 |
+
period : float, optional
|
| 297 |
+
The period of the sequence. Default period is ``2*pi``.
|
| 298 |
+
|
| 299 |
+
Returns
|
| 300 |
+
-------
|
| 301 |
+
cs_diff : ndarray
|
| 302 |
+
Pseudo-derivative of periodic sequence `x`.
|
| 303 |
+
|
| 304 |
+
Notes
|
| 305 |
+
-----
|
| 306 |
+
For even len(`x`), the Nyquist mode of `x` is taken as zero.
|
| 307 |
+
|
| 308 |
+
"""
|
| 309 |
+
if isinstance(_cache, threading.local):
|
| 310 |
+
if not hasattr(_cache, 'cs_diff_cache'):
|
| 311 |
+
_cache.cs_diff_cache = {}
|
| 312 |
+
_cache = _cache.cs_diff_cache
|
| 313 |
+
|
| 314 |
+
tmp = asarray(x)
|
| 315 |
+
if iscomplexobj(tmp):
|
| 316 |
+
return cs_diff(tmp.real, a, b, period, _cache) + \
|
| 317 |
+
1j*cs_diff(tmp.imag, a, b, period, _cache)
|
| 318 |
+
if period is not None:
|
| 319 |
+
a = a*2*pi/period
|
| 320 |
+
b = b*2*pi/period
|
| 321 |
+
n = len(x)
|
| 322 |
+
omega = _cache.get((n,a,b))
|
| 323 |
+
if omega is None:
|
| 324 |
+
if len(_cache) > 20:
|
| 325 |
+
while _cache:
|
| 326 |
+
_cache.popitem()
|
| 327 |
+
|
| 328 |
+
def kernel(k,a=a,b=b):
|
| 329 |
+
if k:
|
| 330 |
+
return -cosh(a*k)/sinh(b*k)
|
| 331 |
+
return 0
|
| 332 |
+
omega = convolve.init_convolution_kernel(n,kernel,d=1)
|
| 333 |
+
_cache[(n,a,b)] = omega
|
| 334 |
+
overwrite_x = _datacopied(tmp, x)
|
| 335 |
+
return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x)
|
| 336 |
+
|
| 337 |
+
|
| 338 |
+
def sc_diff(x, a, b, period=None, _cache=_cache):
|
| 339 |
+
"""
|
| 340 |
+
Return (a,b)-sinh/cosh pseudo-derivative of a periodic sequence x.
|
| 341 |
+
|
| 342 |
+
If x_j and y_j are Fourier coefficients of periodic functions x
|
| 343 |
+
and y, respectively, then::
|
| 344 |
+
|
| 345 |
+
y_j = sqrt(-1)*sinh(j*a*2*pi/period)/cosh(j*b*2*pi/period) * x_j
|
| 346 |
+
y_0 = 0
|
| 347 |
+
|
| 348 |
+
Parameters
|
| 349 |
+
----------
|
| 350 |
+
x : array_like
|
| 351 |
+
Input array.
|
| 352 |
+
a,b : float
|
| 353 |
+
Defines the parameters of the sinh/cosh pseudo-differential
|
| 354 |
+
operator.
|
| 355 |
+
period : float, optional
|
| 356 |
+
The period of the sequence x. Default is 2*pi.
|
| 357 |
+
|
| 358 |
+
Notes
|
| 359 |
+
-----
|
| 360 |
+
``sc_diff(cs_diff(x,a,b),b,a) == x``
|
| 361 |
+
For even ``len(x)``, the Nyquist mode of x is taken as zero.
|
| 362 |
+
|
| 363 |
+
"""
|
| 364 |
+
if isinstance(_cache, threading.local):
|
| 365 |
+
if not hasattr(_cache, 'sc_diff_cache'):
|
| 366 |
+
_cache.sc_diff_cache = {}
|
| 367 |
+
_cache = _cache.sc_diff_cache
|
| 368 |
+
|
| 369 |
+
tmp = asarray(x)
|
| 370 |
+
if iscomplexobj(tmp):
|
| 371 |
+
return sc_diff(tmp.real, a, b, period, _cache) + \
|
| 372 |
+
1j * sc_diff(tmp.imag, a, b, period, _cache)
|
| 373 |
+
if period is not None:
|
| 374 |
+
a = a*2*pi/period
|
| 375 |
+
b = b*2*pi/period
|
| 376 |
+
n = len(x)
|
| 377 |
+
omega = _cache.get((n,a,b))
|
| 378 |
+
if omega is None:
|
| 379 |
+
if len(_cache) > 20:
|
| 380 |
+
while _cache:
|
| 381 |
+
_cache.popitem()
|
| 382 |
+
|
| 383 |
+
def kernel(k,a=a,b=b):
|
| 384 |
+
if k:
|
| 385 |
+
return sinh(a*k)/cosh(b*k)
|
| 386 |
+
return 0
|
| 387 |
+
omega = convolve.init_convolution_kernel(n,kernel,d=1)
|
| 388 |
+
_cache[(n,a,b)] = omega
|
| 389 |
+
overwrite_x = _datacopied(tmp, x)
|
| 390 |
+
return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x)
|
| 391 |
+
|
| 392 |
+
|
| 393 |
+
def ss_diff(x, a, b, period=None, _cache=_cache):
|
| 394 |
+
"""
|
| 395 |
+
Return (a,b)-sinh/sinh pseudo-derivative of a periodic sequence x.
|
| 396 |
+
|
| 397 |
+
If x_j and y_j are Fourier coefficients of periodic functions x
|
| 398 |
+
and y, respectively, then::
|
| 399 |
+
|
| 400 |
+
y_j = sinh(j*a*2*pi/period)/sinh(j*b*2*pi/period) * x_j
|
| 401 |
+
y_0 = a/b * x_0
|
| 402 |
+
|
| 403 |
+
Parameters
|
| 404 |
+
----------
|
| 405 |
+
x : array_like
|
| 406 |
+
The array to take the pseudo-derivative from.
|
| 407 |
+
a,b
|
| 408 |
+
Defines the parameters of the sinh/sinh pseudo-differential
|
| 409 |
+
operator.
|
| 410 |
+
period : float, optional
|
| 411 |
+
The period of the sequence x. Default is ``2*pi``.
|
| 412 |
+
|
| 413 |
+
Notes
|
| 414 |
+
-----
|
| 415 |
+
``ss_diff(ss_diff(x,a,b),b,a) == x``
|
| 416 |
+
|
| 417 |
+
"""
|
| 418 |
+
if isinstance(_cache, threading.local):
|
| 419 |
+
if not hasattr(_cache, 'ss_diff_cache'):
|
| 420 |
+
_cache.ss_diff_cache = {}
|
| 421 |
+
_cache = _cache.ss_diff_cache
|
| 422 |
+
|
| 423 |
+
tmp = asarray(x)
|
| 424 |
+
if iscomplexobj(tmp):
|
| 425 |
+
return ss_diff(tmp.real, a, b, period, _cache) + \
|
| 426 |
+
1j*ss_diff(tmp.imag, a, b, period, _cache)
|
| 427 |
+
if period is not None:
|
| 428 |
+
a = a*2*pi/period
|
| 429 |
+
b = b*2*pi/period
|
| 430 |
+
n = len(x)
|
| 431 |
+
omega = _cache.get((n,a,b))
|
| 432 |
+
if omega is None:
|
| 433 |
+
if len(_cache) > 20:
|
| 434 |
+
while _cache:
|
| 435 |
+
_cache.popitem()
|
| 436 |
+
|
| 437 |
+
def kernel(k,a=a,b=b):
|
| 438 |
+
if k:
|
| 439 |
+
return sinh(a*k)/sinh(b*k)
|
| 440 |
+
return float(a)/b
|
| 441 |
+
omega = convolve.init_convolution_kernel(n,kernel)
|
| 442 |
+
_cache[(n,a,b)] = omega
|
| 443 |
+
overwrite_x = _datacopied(tmp, x)
|
| 444 |
+
return convolve.convolve(tmp,omega,overwrite_x=overwrite_x)
|
| 445 |
+
|
| 446 |
+
|
| 447 |
+
def cc_diff(x, a, b, period=None, _cache=_cache):
|
| 448 |
+
"""
|
| 449 |
+
Return (a,b)-cosh/cosh pseudo-derivative of a periodic sequence.
|
| 450 |
+
|
| 451 |
+
If x_j and y_j are Fourier coefficients of periodic functions x
|
| 452 |
+
and y, respectively, then::
|
| 453 |
+
|
| 454 |
+
y_j = cosh(j*a*2*pi/period)/cosh(j*b*2*pi/period) * x_j
|
| 455 |
+
|
| 456 |
+
Parameters
|
| 457 |
+
----------
|
| 458 |
+
x : array_like
|
| 459 |
+
The array to take the pseudo-derivative from.
|
| 460 |
+
a,b : float
|
| 461 |
+
Defines the parameters of the sinh/sinh pseudo-differential
|
| 462 |
+
operator.
|
| 463 |
+
period : float, optional
|
| 464 |
+
The period of the sequence x. Default is ``2*pi``.
|
| 465 |
+
|
| 466 |
+
Returns
|
| 467 |
+
-------
|
| 468 |
+
cc_diff : ndarray
|
| 469 |
+
Pseudo-derivative of periodic sequence `x`.
|
| 470 |
+
|
| 471 |
+
Notes
|
| 472 |
+
-----
|
| 473 |
+
``cc_diff(cc_diff(x,a,b),b,a) == x``
|
| 474 |
+
|
| 475 |
+
"""
|
| 476 |
+
if isinstance(_cache, threading.local):
|
| 477 |
+
if not hasattr(_cache, 'cc_diff_cache'):
|
| 478 |
+
_cache.cc_diff_cache = {}
|
| 479 |
+
_cache = _cache.cc_diff_cache
|
| 480 |
+
|
| 481 |
+
tmp = asarray(x)
|
| 482 |
+
if iscomplexobj(tmp):
|
| 483 |
+
return cc_diff(tmp.real, a, b, period, _cache) + \
|
| 484 |
+
1j * cc_diff(tmp.imag, a, b, period, _cache)
|
| 485 |
+
if period is not None:
|
| 486 |
+
a = a*2*pi/period
|
| 487 |
+
b = b*2*pi/period
|
| 488 |
+
n = len(x)
|
| 489 |
+
omega = _cache.get((n,a,b))
|
| 490 |
+
if omega is None:
|
| 491 |
+
if len(_cache) > 20:
|
| 492 |
+
while _cache:
|
| 493 |
+
_cache.popitem()
|
| 494 |
+
|
| 495 |
+
def kernel(k,a=a,b=b):
|
| 496 |
+
return cosh(a*k)/cosh(b*k)
|
| 497 |
+
omega = convolve.init_convolution_kernel(n,kernel)
|
| 498 |
+
_cache[(n,a,b)] = omega
|
| 499 |
+
overwrite_x = _datacopied(tmp, x)
|
| 500 |
+
return convolve.convolve(tmp,omega,overwrite_x=overwrite_x)
|
| 501 |
+
|
| 502 |
+
|
| 503 |
+
def shift(x, a, period=None, _cache=_cache):
|
| 504 |
+
"""
|
| 505 |
+
Shift periodic sequence x by a: y(u) = x(u+a).
|
| 506 |
+
|
| 507 |
+
If x_j and y_j are Fourier coefficients of periodic functions x
|
| 508 |
+
and y, respectively, then::
|
| 509 |
+
|
| 510 |
+
y_j = exp(j*a*2*pi/period*sqrt(-1)) * x_f
|
| 511 |
+
|
| 512 |
+
Parameters
|
| 513 |
+
----------
|
| 514 |
+
x : array_like
|
| 515 |
+
The array to take the pseudo-derivative from.
|
| 516 |
+
a : float
|
| 517 |
+
Defines the parameters of the sinh/sinh pseudo-differential
|
| 518 |
+
period : float, optional
|
| 519 |
+
The period of the sequences x and y. Default period is ``2*pi``.
|
| 520 |
+
"""
|
| 521 |
+
if isinstance(_cache, threading.local):
|
| 522 |
+
if not hasattr(_cache, 'shift_cache'):
|
| 523 |
+
_cache.shift_cache = {}
|
| 524 |
+
_cache = _cache.shift_cache
|
| 525 |
+
|
| 526 |
+
tmp = asarray(x)
|
| 527 |
+
if iscomplexobj(tmp):
|
| 528 |
+
return shift(tmp.real, a, period, _cache) + 1j * shift(
|
| 529 |
+
tmp.imag, a, period, _cache)
|
| 530 |
+
if period is not None:
|
| 531 |
+
a = a*2*pi/period
|
| 532 |
+
n = len(x)
|
| 533 |
+
omega = _cache.get((n,a))
|
| 534 |
+
if omega is None:
|
| 535 |
+
if len(_cache) > 20:
|
| 536 |
+
while _cache:
|
| 537 |
+
_cache.popitem()
|
| 538 |
+
|
| 539 |
+
def kernel_real(k,a=a):
|
| 540 |
+
return cos(a*k)
|
| 541 |
+
|
| 542 |
+
def kernel_imag(k,a=a):
|
| 543 |
+
return sin(a*k)
|
| 544 |
+
omega_real = convolve.init_convolution_kernel(n,kernel_real,d=0,
|
| 545 |
+
zero_nyquist=0)
|
| 546 |
+
omega_imag = convolve.init_convolution_kernel(n,kernel_imag,d=1,
|
| 547 |
+
zero_nyquist=0)
|
| 548 |
+
_cache[(n,a)] = omega_real,omega_imag
|
| 549 |
+
else:
|
| 550 |
+
omega_real,omega_imag = omega
|
| 551 |
+
overwrite_x = _datacopied(tmp, x)
|
| 552 |
+
return convolve.convolve_z(tmp,omega_real,omega_imag,
|
| 553 |
+
overwrite_x=overwrite_x)
|
| 554 |
+
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/_realtransforms.py
ADDED
|
@@ -0,0 +1,598 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Real spectrum transforms (DCT, DST, MDCT)
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
__all__ = ['dct', 'idct', 'dst', 'idst', 'dctn', 'idctn', 'dstn', 'idstn']
|
| 6 |
+
|
| 7 |
+
from scipy.fft import _pocketfft
|
| 8 |
+
from ._helper import _good_shape
|
| 9 |
+
|
| 10 |
+
_inverse_typemap = {1: 1, 2: 3, 3: 2, 4: 4}
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def dctn(x, type=2, shape=None, axes=None, norm=None, overwrite_x=False):
|
| 14 |
+
"""
|
| 15 |
+
Return multidimensional Discrete Cosine Transform along the specified axes.
|
| 16 |
+
|
| 17 |
+
Parameters
|
| 18 |
+
----------
|
| 19 |
+
x : array_like
|
| 20 |
+
The input array.
|
| 21 |
+
type : {1, 2, 3, 4}, optional
|
| 22 |
+
Type of the DCT (see Notes). Default type is 2.
|
| 23 |
+
shape : int or array_like of ints or None, optional
|
| 24 |
+
The shape of the result. If both `shape` and `axes` (see below) are
|
| 25 |
+
None, `shape` is ``x.shape``; if `shape` is None but `axes` is
|
| 26 |
+
not None, then `shape` is ``numpy.take(x.shape, axes, axis=0)``.
|
| 27 |
+
If ``shape[i] > x.shape[i]``, the ith dimension is padded with zeros.
|
| 28 |
+
If ``shape[i] < x.shape[i]``, the ith dimension is truncated to
|
| 29 |
+
length ``shape[i]``.
|
| 30 |
+
If any element of `shape` is -1, the size of the corresponding
|
| 31 |
+
dimension of `x` is used.
|
| 32 |
+
axes : int or array_like of ints or None, optional
|
| 33 |
+
Axes along which the DCT is computed.
|
| 34 |
+
The default is over all axes.
|
| 35 |
+
norm : {None, 'ortho'}, optional
|
| 36 |
+
Normalization mode (see Notes). Default is None.
|
| 37 |
+
overwrite_x : bool, optional
|
| 38 |
+
If True, the contents of `x` can be destroyed; the default is False.
|
| 39 |
+
|
| 40 |
+
Returns
|
| 41 |
+
-------
|
| 42 |
+
y : ndarray of real
|
| 43 |
+
The transformed input array.
|
| 44 |
+
|
| 45 |
+
See Also
|
| 46 |
+
--------
|
| 47 |
+
idctn : Inverse multidimensional DCT
|
| 48 |
+
|
| 49 |
+
Notes
|
| 50 |
+
-----
|
| 51 |
+
For full details of the DCT types and normalization modes, as well as
|
| 52 |
+
references, see `dct`.
|
| 53 |
+
|
| 54 |
+
Examples
|
| 55 |
+
--------
|
| 56 |
+
>>> import numpy as np
|
| 57 |
+
>>> from scipy.fftpack import dctn, idctn
|
| 58 |
+
>>> rng = np.random.default_rng()
|
| 59 |
+
>>> y = rng.standard_normal((16, 16))
|
| 60 |
+
>>> np.allclose(y, idctn(dctn(y, norm='ortho'), norm='ortho'))
|
| 61 |
+
True
|
| 62 |
+
|
| 63 |
+
"""
|
| 64 |
+
shape = _good_shape(x, shape, axes)
|
| 65 |
+
return _pocketfft.dctn(x, type, shape, axes, norm, overwrite_x)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def idctn(x, type=2, shape=None, axes=None, norm=None, overwrite_x=False):
|
| 69 |
+
"""
|
| 70 |
+
Return multidimensional Discrete Cosine Transform along the specified axes.
|
| 71 |
+
|
| 72 |
+
Parameters
|
| 73 |
+
----------
|
| 74 |
+
x : array_like
|
| 75 |
+
The input array.
|
| 76 |
+
type : {1, 2, 3, 4}, optional
|
| 77 |
+
Type of the DCT (see Notes). Default type is 2.
|
| 78 |
+
shape : int or array_like of ints or None, optional
|
| 79 |
+
The shape of the result. If both `shape` and `axes` (see below) are
|
| 80 |
+
None, `shape` is ``x.shape``; if `shape` is None but `axes` is
|
| 81 |
+
not None, then `shape` is ``numpy.take(x.shape, axes, axis=0)``.
|
| 82 |
+
If ``shape[i] > x.shape[i]``, the ith dimension is padded with zeros.
|
| 83 |
+
If ``shape[i] < x.shape[i]``, the ith dimension is truncated to
|
| 84 |
+
length ``shape[i]``.
|
| 85 |
+
If any element of `shape` is -1, the size of the corresponding
|
| 86 |
+
dimension of `x` is used.
|
| 87 |
+
axes : int or array_like of ints or None, optional
|
| 88 |
+
Axes along which the IDCT is computed.
|
| 89 |
+
The default is over all axes.
|
| 90 |
+
norm : {None, 'ortho'}, optional
|
| 91 |
+
Normalization mode (see Notes). Default is None.
|
| 92 |
+
overwrite_x : bool, optional
|
| 93 |
+
If True, the contents of `x` can be destroyed; the default is False.
|
| 94 |
+
|
| 95 |
+
Returns
|
| 96 |
+
-------
|
| 97 |
+
y : ndarray of real
|
| 98 |
+
The transformed input array.
|
| 99 |
+
|
| 100 |
+
See Also
|
| 101 |
+
--------
|
| 102 |
+
dctn : multidimensional DCT
|
| 103 |
+
|
| 104 |
+
Notes
|
| 105 |
+
-----
|
| 106 |
+
For full details of the IDCT types and normalization modes, as well as
|
| 107 |
+
references, see `idct`.
|
| 108 |
+
|
| 109 |
+
Examples
|
| 110 |
+
--------
|
| 111 |
+
>>> import numpy as np
|
| 112 |
+
>>> from scipy.fftpack import dctn, idctn
|
| 113 |
+
>>> rng = np.random.default_rng()
|
| 114 |
+
>>> y = rng.standard_normal((16, 16))
|
| 115 |
+
>>> np.allclose(y, idctn(dctn(y, norm='ortho'), norm='ortho'))
|
| 116 |
+
True
|
| 117 |
+
|
| 118 |
+
"""
|
| 119 |
+
type = _inverse_typemap[type]
|
| 120 |
+
shape = _good_shape(x, shape, axes)
|
| 121 |
+
return _pocketfft.dctn(x, type, shape, axes, norm, overwrite_x)
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
def dstn(x, type=2, shape=None, axes=None, norm=None, overwrite_x=False):
|
| 125 |
+
"""
|
| 126 |
+
Return multidimensional Discrete Sine Transform along the specified axes.
|
| 127 |
+
|
| 128 |
+
Parameters
|
| 129 |
+
----------
|
| 130 |
+
x : array_like
|
| 131 |
+
The input array.
|
| 132 |
+
type : {1, 2, 3, 4}, optional
|
| 133 |
+
Type of the DST (see Notes). Default type is 2.
|
| 134 |
+
shape : int or array_like of ints or None, optional
|
| 135 |
+
The shape of the result. If both `shape` and `axes` (see below) are
|
| 136 |
+
None, `shape` is ``x.shape``; if `shape` is None but `axes` is
|
| 137 |
+
not None, then `shape` is ``numpy.take(x.shape, axes, axis=0)``.
|
| 138 |
+
If ``shape[i] > x.shape[i]``, the ith dimension is padded with zeros.
|
| 139 |
+
If ``shape[i] < x.shape[i]``, the ith dimension is truncated to
|
| 140 |
+
length ``shape[i]``.
|
| 141 |
+
If any element of `shape` is -1, the size of the corresponding
|
| 142 |
+
dimension of `x` is used.
|
| 143 |
+
axes : int or array_like of ints or None, optional
|
| 144 |
+
Axes along which the DCT is computed.
|
| 145 |
+
The default is over all axes.
|
| 146 |
+
norm : {None, 'ortho'}, optional
|
| 147 |
+
Normalization mode (see Notes). Default is None.
|
| 148 |
+
overwrite_x : bool, optional
|
| 149 |
+
If True, the contents of `x` can be destroyed; the default is False.
|
| 150 |
+
|
| 151 |
+
Returns
|
| 152 |
+
-------
|
| 153 |
+
y : ndarray of real
|
| 154 |
+
The transformed input array.
|
| 155 |
+
|
| 156 |
+
See Also
|
| 157 |
+
--------
|
| 158 |
+
idstn : Inverse multidimensional DST
|
| 159 |
+
|
| 160 |
+
Notes
|
| 161 |
+
-----
|
| 162 |
+
For full details of the DST types and normalization modes, as well as
|
| 163 |
+
references, see `dst`.
|
| 164 |
+
|
| 165 |
+
Examples
|
| 166 |
+
--------
|
| 167 |
+
>>> import numpy as np
|
| 168 |
+
>>> from scipy.fftpack import dstn, idstn
|
| 169 |
+
>>> rng = np.random.default_rng()
|
| 170 |
+
>>> y = rng.standard_normal((16, 16))
|
| 171 |
+
>>> np.allclose(y, idstn(dstn(y, norm='ortho'), norm='ortho'))
|
| 172 |
+
True
|
| 173 |
+
|
| 174 |
+
"""
|
| 175 |
+
shape = _good_shape(x, shape, axes)
|
| 176 |
+
return _pocketfft.dstn(x, type, shape, axes, norm, overwrite_x)
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
def idstn(x, type=2, shape=None, axes=None, norm=None, overwrite_x=False):
|
| 180 |
+
"""
|
| 181 |
+
Return multidimensional Discrete Sine Transform along the specified axes.
|
| 182 |
+
|
| 183 |
+
Parameters
|
| 184 |
+
----------
|
| 185 |
+
x : array_like
|
| 186 |
+
The input array.
|
| 187 |
+
type : {1, 2, 3, 4}, optional
|
| 188 |
+
Type of the DST (see Notes). Default type is 2.
|
| 189 |
+
shape : int or array_like of ints or None, optional
|
| 190 |
+
The shape of the result. If both `shape` and `axes` (see below) are
|
| 191 |
+
None, `shape` is ``x.shape``; if `shape` is None but `axes` is
|
| 192 |
+
not None, then `shape` is ``numpy.take(x.shape, axes, axis=0)``.
|
| 193 |
+
If ``shape[i] > x.shape[i]``, the ith dimension is padded with zeros.
|
| 194 |
+
If ``shape[i] < x.shape[i]``, the ith dimension is truncated to
|
| 195 |
+
length ``shape[i]``.
|
| 196 |
+
If any element of `shape` is -1, the size of the corresponding
|
| 197 |
+
dimension of `x` is used.
|
| 198 |
+
axes : int or array_like of ints or None, optional
|
| 199 |
+
Axes along which the IDST is computed.
|
| 200 |
+
The default is over all axes.
|
| 201 |
+
norm : {None, 'ortho'}, optional
|
| 202 |
+
Normalization mode (see Notes). Default is None.
|
| 203 |
+
overwrite_x : bool, optional
|
| 204 |
+
If True, the contents of `x` can be destroyed; the default is False.
|
| 205 |
+
|
| 206 |
+
Returns
|
| 207 |
+
-------
|
| 208 |
+
y : ndarray of real
|
| 209 |
+
The transformed input array.
|
| 210 |
+
|
| 211 |
+
See Also
|
| 212 |
+
--------
|
| 213 |
+
dstn : multidimensional DST
|
| 214 |
+
|
| 215 |
+
Notes
|
| 216 |
+
-----
|
| 217 |
+
For full details of the IDST types and normalization modes, as well as
|
| 218 |
+
references, see `idst`.
|
| 219 |
+
|
| 220 |
+
Examples
|
| 221 |
+
--------
|
| 222 |
+
>>> import numpy as np
|
| 223 |
+
>>> from scipy.fftpack import dstn, idstn
|
| 224 |
+
>>> rng = np.random.default_rng()
|
| 225 |
+
>>> y = rng.standard_normal((16, 16))
|
| 226 |
+
>>> np.allclose(y, idstn(dstn(y, norm='ortho'), norm='ortho'))
|
| 227 |
+
True
|
| 228 |
+
|
| 229 |
+
"""
|
| 230 |
+
type = _inverse_typemap[type]
|
| 231 |
+
shape = _good_shape(x, shape, axes)
|
| 232 |
+
return _pocketfft.dstn(x, type, shape, axes, norm, overwrite_x)
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
def dct(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False):
|
| 236 |
+
r"""
|
| 237 |
+
Return the Discrete Cosine Transform of arbitrary type sequence x.
|
| 238 |
+
|
| 239 |
+
Parameters
|
| 240 |
+
----------
|
| 241 |
+
x : array_like
|
| 242 |
+
The input array.
|
| 243 |
+
type : {1, 2, 3, 4}, optional
|
| 244 |
+
Type of the DCT (see Notes). Default type is 2.
|
| 245 |
+
n : int, optional
|
| 246 |
+
Length of the transform. If ``n < x.shape[axis]``, `x` is
|
| 247 |
+
truncated. If ``n > x.shape[axis]``, `x` is zero-padded. The
|
| 248 |
+
default results in ``n = x.shape[axis]``.
|
| 249 |
+
axis : int, optional
|
| 250 |
+
Axis along which the dct is computed; the default is over the
|
| 251 |
+
last axis (i.e., ``axis=-1``).
|
| 252 |
+
norm : {None, 'ortho'}, optional
|
| 253 |
+
Normalization mode (see Notes). Default is None.
|
| 254 |
+
overwrite_x : bool, optional
|
| 255 |
+
If True, the contents of `x` can be destroyed; the default is False.
|
| 256 |
+
|
| 257 |
+
Returns
|
| 258 |
+
-------
|
| 259 |
+
y : ndarray of real
|
| 260 |
+
The transformed input array.
|
| 261 |
+
|
| 262 |
+
See Also
|
| 263 |
+
--------
|
| 264 |
+
idct : Inverse DCT
|
| 265 |
+
|
| 266 |
+
Notes
|
| 267 |
+
-----
|
| 268 |
+
For a single dimension array ``x``, ``dct(x, norm='ortho')`` is equal to
|
| 269 |
+
MATLAB ``dct(x)``.
|
| 270 |
+
|
| 271 |
+
There are, theoretically, 8 types of the DCT, only the first 4 types are
|
| 272 |
+
implemented in scipy. 'The' DCT generally refers to DCT type 2, and 'the'
|
| 273 |
+
Inverse DCT generally refers to DCT type 3.
|
| 274 |
+
|
| 275 |
+
**Type I**
|
| 276 |
+
|
| 277 |
+
There are several definitions of the DCT-I; we use the following
|
| 278 |
+
(for ``norm=None``)
|
| 279 |
+
|
| 280 |
+
.. math::
|
| 281 |
+
|
| 282 |
+
y_k = x_0 + (-1)^k x_{N-1} + 2 \sum_{n=1}^{N-2} x_n \cos\left(
|
| 283 |
+
\frac{\pi k n}{N-1} \right)
|
| 284 |
+
|
| 285 |
+
If ``norm='ortho'``, ``x[0]`` and ``x[N-1]`` are multiplied by a scaling
|
| 286 |
+
factor of :math:`\sqrt{2}`, and ``y[k]`` is multiplied by a scaling factor
|
| 287 |
+
``f``
|
| 288 |
+
|
| 289 |
+
.. math::
|
| 290 |
+
|
| 291 |
+
f = \begin{cases}
|
| 292 |
+
\frac{1}{2}\sqrt{\frac{1}{N-1}} & \text{if }k=0\text{ or }N-1, \\
|
| 293 |
+
\frac{1}{2}\sqrt{\frac{2}{N-1}} & \text{otherwise} \end{cases}
|
| 294 |
+
|
| 295 |
+
.. versionadded:: 1.2.0
|
| 296 |
+
Orthonormalization in DCT-I.
|
| 297 |
+
|
| 298 |
+
.. note::
|
| 299 |
+
The DCT-I is only supported for input size > 1.
|
| 300 |
+
|
| 301 |
+
**Type II**
|
| 302 |
+
|
| 303 |
+
There are several definitions of the DCT-II; we use the following
|
| 304 |
+
(for ``norm=None``)
|
| 305 |
+
|
| 306 |
+
.. math::
|
| 307 |
+
|
| 308 |
+
y_k = 2 \sum_{n=0}^{N-1} x_n \cos\left(\frac{\pi k(2n+1)}{2N} \right)
|
| 309 |
+
|
| 310 |
+
If ``norm='ortho'``, ``y[k]`` is multiplied by a scaling factor ``f``
|
| 311 |
+
|
| 312 |
+
.. math::
|
| 313 |
+
f = \begin{cases}
|
| 314 |
+
\sqrt{\frac{1}{4N}} & \text{if }k=0, \\
|
| 315 |
+
\sqrt{\frac{1}{2N}} & \text{otherwise} \end{cases}
|
| 316 |
+
|
| 317 |
+
which makes the corresponding matrix of coefficients orthonormal
|
| 318 |
+
(``O @ O.T = np.eye(N)``).
|
| 319 |
+
|
| 320 |
+
**Type III**
|
| 321 |
+
|
| 322 |
+
There are several definitions, we use the following (for ``norm=None``)
|
| 323 |
+
|
| 324 |
+
.. math::
|
| 325 |
+
|
| 326 |
+
y_k = x_0 + 2 \sum_{n=1}^{N-1} x_n \cos\left(\frac{\pi(2k+1)n}{2N}\right)
|
| 327 |
+
|
| 328 |
+
or, for ``norm='ortho'``
|
| 329 |
+
|
| 330 |
+
.. math::
|
| 331 |
+
|
| 332 |
+
y_k = \frac{x_0}{\sqrt{N}} + \sqrt{\frac{2}{N}} \sum_{n=1}^{N-1} x_n
|
| 333 |
+
\cos\left(\frac{\pi(2k+1)n}{2N}\right)
|
| 334 |
+
|
| 335 |
+
The (unnormalized) DCT-III is the inverse of the (unnormalized) DCT-II, up
|
| 336 |
+
to a factor ``2N``. The orthonormalized DCT-III is exactly the inverse of
|
| 337 |
+
the orthonormalized DCT-II.
|
| 338 |
+
|
| 339 |
+
**Type IV**
|
| 340 |
+
|
| 341 |
+
There are several definitions of the DCT-IV; we use the following
|
| 342 |
+
(for ``norm=None``)
|
| 343 |
+
|
| 344 |
+
.. math::
|
| 345 |
+
|
| 346 |
+
y_k = 2 \sum_{n=0}^{N-1} x_n \cos\left(\frac{\pi(2k+1)(2n+1)}{4N} \right)
|
| 347 |
+
|
| 348 |
+
If ``norm='ortho'``, ``y[k]`` is multiplied by a scaling factor ``f``
|
| 349 |
+
|
| 350 |
+
.. math::
|
| 351 |
+
|
| 352 |
+
f = \frac{1}{\sqrt{2N}}
|
| 353 |
+
|
| 354 |
+
.. versionadded:: 1.2.0
|
| 355 |
+
Support for DCT-IV.
|
| 356 |
+
|
| 357 |
+
References
|
| 358 |
+
----------
|
| 359 |
+
.. [1] 'A Fast Cosine Transform in One and Two Dimensions', by J.
|
| 360 |
+
Makhoul, `IEEE Transactions on acoustics, speech and signal
|
| 361 |
+
processing` vol. 28(1), pp. 27-34,
|
| 362 |
+
:doi:`10.1109/TASSP.1980.1163351` (1980).
|
| 363 |
+
.. [2] Wikipedia, "Discrete cosine transform",
|
| 364 |
+
https://en.wikipedia.org/wiki/Discrete_cosine_transform
|
| 365 |
+
|
| 366 |
+
Examples
|
| 367 |
+
--------
|
| 368 |
+
The Type 1 DCT is equivalent to the FFT (though faster) for real,
|
| 369 |
+
even-symmetrical inputs. The output is also real and even-symmetrical.
|
| 370 |
+
Half of the FFT input is used to generate half of the FFT output:
|
| 371 |
+
|
| 372 |
+
>>> from scipy.fftpack import fft, dct
|
| 373 |
+
>>> import numpy as np
|
| 374 |
+
>>> fft(np.array([4., 3., 5., 10., 5., 3.])).real
|
| 375 |
+
array([ 30., -8., 6., -2., 6., -8.])
|
| 376 |
+
>>> dct(np.array([4., 3., 5., 10.]), 1)
|
| 377 |
+
array([ 30., -8., 6., -2.])
|
| 378 |
+
|
| 379 |
+
"""
|
| 380 |
+
return _pocketfft.dct(x, type, n, axis, norm, overwrite_x)
|
| 381 |
+
|
| 382 |
+
|
| 383 |
+
def idct(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False):
|
| 384 |
+
"""
|
| 385 |
+
Return the Inverse Discrete Cosine Transform of an arbitrary type sequence.
|
| 386 |
+
|
| 387 |
+
Parameters
|
| 388 |
+
----------
|
| 389 |
+
x : array_like
|
| 390 |
+
The input array.
|
| 391 |
+
type : {1, 2, 3, 4}, optional
|
| 392 |
+
Type of the DCT (see Notes). Default type is 2.
|
| 393 |
+
n : int, optional
|
| 394 |
+
Length of the transform. If ``n < x.shape[axis]``, `x` is
|
| 395 |
+
truncated. If ``n > x.shape[axis]``, `x` is zero-padded. The
|
| 396 |
+
default results in ``n = x.shape[axis]``.
|
| 397 |
+
axis : int, optional
|
| 398 |
+
Axis along which the idct is computed; the default is over the
|
| 399 |
+
last axis (i.e., ``axis=-1``).
|
| 400 |
+
norm : {None, 'ortho'}, optional
|
| 401 |
+
Normalization mode (see Notes). Default is None.
|
| 402 |
+
overwrite_x : bool, optional
|
| 403 |
+
If True, the contents of `x` can be destroyed; the default is False.
|
| 404 |
+
|
| 405 |
+
Returns
|
| 406 |
+
-------
|
| 407 |
+
idct : ndarray of real
|
| 408 |
+
The transformed input array.
|
| 409 |
+
|
| 410 |
+
See Also
|
| 411 |
+
--------
|
| 412 |
+
dct : Forward DCT
|
| 413 |
+
|
| 414 |
+
Notes
|
| 415 |
+
-----
|
| 416 |
+
For a single dimension array `x`, ``idct(x, norm='ortho')`` is equal to
|
| 417 |
+
MATLAB ``idct(x)``.
|
| 418 |
+
|
| 419 |
+
'The' IDCT is the IDCT of type 2, which is the same as DCT of type 3.
|
| 420 |
+
|
| 421 |
+
IDCT of type 1 is the DCT of type 1, IDCT of type 2 is the DCT of type
|
| 422 |
+
3, and IDCT of type 3 is the DCT of type 2. IDCT of type 4 is the DCT
|
| 423 |
+
of type 4. For the definition of these types, see `dct`.
|
| 424 |
+
|
| 425 |
+
Examples
|
| 426 |
+
--------
|
| 427 |
+
The Type 1 DCT is equivalent to the DFT for real, even-symmetrical
|
| 428 |
+
inputs. The output is also real and even-symmetrical. Half of the IFFT
|
| 429 |
+
input is used to generate half of the IFFT output:
|
| 430 |
+
|
| 431 |
+
>>> from scipy.fftpack import ifft, idct
|
| 432 |
+
>>> import numpy as np
|
| 433 |
+
>>> ifft(np.array([ 30., -8., 6., -2., 6., -8.])).real
|
| 434 |
+
array([ 4., 3., 5., 10., 5., 3.])
|
| 435 |
+
>>> idct(np.array([ 30., -8., 6., -2.]), 1) / 6
|
| 436 |
+
array([ 4., 3., 5., 10.])
|
| 437 |
+
|
| 438 |
+
"""
|
| 439 |
+
type = _inverse_typemap[type]
|
| 440 |
+
return _pocketfft.dct(x, type, n, axis, norm, overwrite_x)
|
| 441 |
+
|
| 442 |
+
|
| 443 |
+
def dst(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False):
|
| 444 |
+
r"""
|
| 445 |
+
Return the Discrete Sine Transform of arbitrary type sequence x.
|
| 446 |
+
|
| 447 |
+
Parameters
|
| 448 |
+
----------
|
| 449 |
+
x : array_like
|
| 450 |
+
The input array.
|
| 451 |
+
type : {1, 2, 3, 4}, optional
|
| 452 |
+
Type of the DST (see Notes). Default type is 2.
|
| 453 |
+
n : int, optional
|
| 454 |
+
Length of the transform. If ``n < x.shape[axis]``, `x` is
|
| 455 |
+
truncated. If ``n > x.shape[axis]``, `x` is zero-padded. The
|
| 456 |
+
default results in ``n = x.shape[axis]``.
|
| 457 |
+
axis : int, optional
|
| 458 |
+
Axis along which the dst is computed; the default is over the
|
| 459 |
+
last axis (i.e., ``axis=-1``).
|
| 460 |
+
norm : {None, 'ortho'}, optional
|
| 461 |
+
Normalization mode (see Notes). Default is None.
|
| 462 |
+
overwrite_x : bool, optional
|
| 463 |
+
If True, the contents of `x` can be destroyed; the default is False.
|
| 464 |
+
|
| 465 |
+
Returns
|
| 466 |
+
-------
|
| 467 |
+
dst : ndarray of reals
|
| 468 |
+
The transformed input array.
|
| 469 |
+
|
| 470 |
+
See Also
|
| 471 |
+
--------
|
| 472 |
+
idst : Inverse DST
|
| 473 |
+
|
| 474 |
+
Notes
|
| 475 |
+
-----
|
| 476 |
+
For a single dimension array ``x``.
|
| 477 |
+
|
| 478 |
+
There are, theoretically, 8 types of the DST for different combinations of
|
| 479 |
+
even/odd boundary conditions and boundary off sets [1]_, only the first
|
| 480 |
+
4 types are implemented in scipy.
|
| 481 |
+
|
| 482 |
+
**Type I**
|
| 483 |
+
|
| 484 |
+
There are several definitions of the DST-I; we use the following
|
| 485 |
+
for ``norm=None``. DST-I assumes the input is odd around `n=-1` and `n=N`.
|
| 486 |
+
|
| 487 |
+
.. math::
|
| 488 |
+
|
| 489 |
+
y_k = 2 \sum_{n=0}^{N-1} x_n \sin\left(\frac{\pi(k+1)(n+1)}{N+1}\right)
|
| 490 |
+
|
| 491 |
+
Note that the DST-I is only supported for input size > 1.
|
| 492 |
+
The (unnormalized) DST-I is its own inverse, up to a factor ``2(N+1)``.
|
| 493 |
+
The orthonormalized DST-I is exactly its own inverse.
|
| 494 |
+
|
| 495 |
+
**Type II**
|
| 496 |
+
|
| 497 |
+
There are several definitions of the DST-II; we use the following for
|
| 498 |
+
``norm=None``. DST-II assumes the input is odd around `n=-1/2` and
|
| 499 |
+
`n=N-1/2`; the output is odd around :math:`k=-1` and even around `k=N-1`
|
| 500 |
+
|
| 501 |
+
.. math::
|
| 502 |
+
|
| 503 |
+
y_k = 2 \sum_{n=0}^{N-1} x_n \sin\left(\frac{\pi(k+1)(2n+1)}{2N}\right)
|
| 504 |
+
|
| 505 |
+
if ``norm='ortho'``, ``y[k]`` is multiplied by a scaling factor ``f``
|
| 506 |
+
|
| 507 |
+
.. math::
|
| 508 |
+
|
| 509 |
+
f = \begin{cases}
|
| 510 |
+
\sqrt{\frac{1}{4N}} & \text{if }k = 0, \\
|
| 511 |
+
\sqrt{\frac{1}{2N}} & \text{otherwise} \end{cases}
|
| 512 |
+
|
| 513 |
+
**Type III**
|
| 514 |
+
|
| 515 |
+
There are several definitions of the DST-III, we use the following (for
|
| 516 |
+
``norm=None``). DST-III assumes the input is odd around `n=-1` and even
|
| 517 |
+
around `n=N-1`
|
| 518 |
+
|
| 519 |
+
.. math::
|
| 520 |
+
|
| 521 |
+
y_k = (-1)^k x_{N-1} + 2 \sum_{n=0}^{N-2} x_n \sin\left(
|
| 522 |
+
\frac{\pi(2k+1)(n+1)}{2N}\right)
|
| 523 |
+
|
| 524 |
+
The (unnormalized) DST-III is the inverse of the (unnormalized) DST-II, up
|
| 525 |
+
to a factor ``2N``. The orthonormalized DST-III is exactly the inverse of the
|
| 526 |
+
orthonormalized DST-II.
|
| 527 |
+
|
| 528 |
+
.. versionadded:: 0.11.0
|
| 529 |
+
|
| 530 |
+
**Type IV**
|
| 531 |
+
|
| 532 |
+
There are several definitions of the DST-IV, we use the following (for
|
| 533 |
+
``norm=None``). DST-IV assumes the input is odd around `n=-0.5` and even
|
| 534 |
+
around `n=N-0.5`
|
| 535 |
+
|
| 536 |
+
.. math::
|
| 537 |
+
|
| 538 |
+
y_k = 2 \sum_{n=0}^{N-1} x_n \sin\left(\frac{\pi(2k+1)(2n+1)}{4N}\right)
|
| 539 |
+
|
| 540 |
+
The (unnormalized) DST-IV is its own inverse, up to a factor ``2N``. The
|
| 541 |
+
orthonormalized DST-IV is exactly its own inverse.
|
| 542 |
+
|
| 543 |
+
.. versionadded:: 1.2.0
|
| 544 |
+
Support for DST-IV.
|
| 545 |
+
|
| 546 |
+
References
|
| 547 |
+
----------
|
| 548 |
+
.. [1] Wikipedia, "Discrete sine transform",
|
| 549 |
+
https://en.wikipedia.org/wiki/Discrete_sine_transform
|
| 550 |
+
|
| 551 |
+
"""
|
| 552 |
+
return _pocketfft.dst(x, type, n, axis, norm, overwrite_x)
|
| 553 |
+
|
| 554 |
+
|
| 555 |
+
def idst(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False):
|
| 556 |
+
"""
|
| 557 |
+
Return the Inverse Discrete Sine Transform of an arbitrary type sequence.
|
| 558 |
+
|
| 559 |
+
Parameters
|
| 560 |
+
----------
|
| 561 |
+
x : array_like
|
| 562 |
+
The input array.
|
| 563 |
+
type : {1, 2, 3, 4}, optional
|
| 564 |
+
Type of the DST (see Notes). Default type is 2.
|
| 565 |
+
n : int, optional
|
| 566 |
+
Length of the transform. If ``n < x.shape[axis]``, `x` is
|
| 567 |
+
truncated. If ``n > x.shape[axis]``, `x` is zero-padded. The
|
| 568 |
+
default results in ``n = x.shape[axis]``.
|
| 569 |
+
axis : int, optional
|
| 570 |
+
Axis along which the idst is computed; the default is over the
|
| 571 |
+
last axis (i.e., ``axis=-1``).
|
| 572 |
+
norm : {None, 'ortho'}, optional
|
| 573 |
+
Normalization mode (see Notes). Default is None.
|
| 574 |
+
overwrite_x : bool, optional
|
| 575 |
+
If True, the contents of `x` can be destroyed; the default is False.
|
| 576 |
+
|
| 577 |
+
Returns
|
| 578 |
+
-------
|
| 579 |
+
idst : ndarray of real
|
| 580 |
+
The transformed input array.
|
| 581 |
+
|
| 582 |
+
See Also
|
| 583 |
+
--------
|
| 584 |
+
dst : Forward DST
|
| 585 |
+
|
| 586 |
+
Notes
|
| 587 |
+
-----
|
| 588 |
+
'The' IDST is the IDST of type 2, which is the same as DST of type 3.
|
| 589 |
+
|
| 590 |
+
IDST of type 1 is the DST of type 1, IDST of type 2 is the DST of type
|
| 591 |
+
3, and IDST of type 3 is the DST of type 2. For the definition of these
|
| 592 |
+
types, see `dst`.
|
| 593 |
+
|
| 594 |
+
.. versionadded:: 0.11.0
|
| 595 |
+
|
| 596 |
+
"""
|
| 597 |
+
type = _inverse_typemap[type]
|
| 598 |
+
return _pocketfft.dst(x, type, n, axis, norm, overwrite_x)
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/basic.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This file is not meant for public use and will be removed in SciPy v2.0.0.
|
| 2 |
+
# Use the `scipy.fftpack` namespace for importing the functions
|
| 3 |
+
# included below.
|
| 4 |
+
|
| 5 |
+
from scipy._lib.deprecation import _sub_module_deprecation
|
| 6 |
+
|
| 7 |
+
__all__ = [ # noqa: F822
|
| 8 |
+
'fft','ifft','fftn','ifftn','rfft','irfft',
|
| 9 |
+
'fft2','ifft2'
|
| 10 |
+
]
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def __dir__():
|
| 14 |
+
return __all__
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def __getattr__(name):
|
| 18 |
+
return _sub_module_deprecation(sub_package="fftpack", module="basic",
|
| 19 |
+
private_modules=["_basic"], all=__all__,
|
| 20 |
+
attribute=name)
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/convolve.cp312-win_amd64.dll.a
ADDED
|
Binary file (1.56 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/convolve.cp312-win_amd64.pyd
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b3c2881a63d1676579f4f398ec68d08f30cc108fcb595a1b91feb41a75d2994c
|
| 3 |
+
size 124928
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/helper.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This file is not meant for public use and will be removed in SciPy v2.0.0.
|
| 2 |
+
# Use the `scipy.fftpack` namespace for importing the functions
|
| 3 |
+
# included below.
|
| 4 |
+
|
| 5 |
+
from scipy._lib.deprecation import _sub_module_deprecation
|
| 6 |
+
|
| 7 |
+
__all__ = [ # noqa: F822
|
| 8 |
+
'fftshift', 'ifftshift', 'fftfreq', 'rfftfreq', 'next_fast_len'
|
| 9 |
+
]
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def __dir__():
|
| 13 |
+
return __all__
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def __getattr__(name):
|
| 17 |
+
return _sub_module_deprecation(sub_package="fftpack", module="helper",
|
| 18 |
+
private_modules=["_helper"], all=__all__,
|
| 19 |
+
attribute=name)
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/pseudo_diffs.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This file is not meant for public use and will be removed in SciPy v2.0.0.
|
| 2 |
+
# Use the `scipy.fftpack` namespace for importing the functions
|
| 3 |
+
# included below.
|
| 4 |
+
|
| 5 |
+
from scipy._lib.deprecation import _sub_module_deprecation
|
| 6 |
+
|
| 7 |
+
__all__ = [ # noqa: F822
|
| 8 |
+
'diff',
|
| 9 |
+
'tilbert', 'itilbert', 'hilbert', 'ihilbert',
|
| 10 |
+
'cs_diff', 'cc_diff', 'sc_diff', 'ss_diff',
|
| 11 |
+
'shift', 'convolve'
|
| 12 |
+
]
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def __dir__():
|
| 16 |
+
return __all__
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def __getattr__(name):
|
| 20 |
+
return _sub_module_deprecation(sub_package="fftpack", module="pseudo_diffs",
|
| 21 |
+
private_modules=["_pseudo_diffs"], all=__all__,
|
| 22 |
+
attribute=name)
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/realtransforms.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This file is not meant for public use and will be removed in SciPy v2.0.0.
|
| 2 |
+
# Use the `scipy.fftpack` namespace for importing the functions
|
| 3 |
+
# included below.
|
| 4 |
+
|
| 5 |
+
from scipy._lib.deprecation import _sub_module_deprecation
|
| 6 |
+
|
| 7 |
+
__all__ = [ # noqa: F822
|
| 8 |
+
'dct', 'idct', 'dst', 'idst', 'dctn', 'idctn', 'dstn', 'idstn'
|
| 9 |
+
]
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def __dir__():
|
| 13 |
+
return __all__
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def __getattr__(name):
|
| 17 |
+
return _sub_module_deprecation(sub_package="fftpack", module="realtransforms",
|
| 18 |
+
private_modules=["_realtransforms"], all=__all__,
|
| 19 |
+
attribute=name)
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/tests/__init__.py
ADDED
|
File without changes
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/tests/fftw_double_ref.npz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a60c649415b645223924d8342ccc5c097801c86901287a369e53fc9259f5ec4e
|
| 3 |
+
size 162120
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/tests/fftw_longdouble_ref.npz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a406cbd4dad04d0c59dd38f54416fb49424c82229c1a074b6a44ec0cde2000e3
|
| 3 |
+
size 296072
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/tests/fftw_single_ref.npz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:276a9141318e6fc36e4ab6ff54a61b64054ef8849b660f17359e5f541b43c526
|
| 3 |
+
size 95144
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/tests/test.npz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:36de804a22d8fdea054590ce49ddf3c859838b7d89193c56b3bcb660cbf43797
|
| 3 |
+
size 11968
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/tests/test_basic.py
ADDED
|
@@ -0,0 +1,877 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Created by Pearu Peterson, September 2002
|
| 2 |
+
|
| 3 |
+
from numpy.testing import (assert_, assert_equal, assert_array_almost_equal,
|
| 4 |
+
assert_array_almost_equal_nulp, assert_array_less)
|
| 5 |
+
import pytest
|
| 6 |
+
from pytest import raises as assert_raises
|
| 7 |
+
from scipy.fftpack import ifft, fft, fftn, ifftn, rfft, irfft, fft2
|
| 8 |
+
|
| 9 |
+
from numpy import (arange, array, asarray, zeros, dot, exp, pi,
|
| 10 |
+
swapaxes, double, cdouble)
|
| 11 |
+
import numpy as np
|
| 12 |
+
import numpy.fft
|
| 13 |
+
from numpy.random import rand
|
| 14 |
+
|
| 15 |
+
# "large" composite numbers supported by FFTPACK
|
| 16 |
+
LARGE_COMPOSITE_SIZES = [
|
| 17 |
+
2**13,
|
| 18 |
+
2**5 * 3**5,
|
| 19 |
+
2**3 * 3**3 * 5**2,
|
| 20 |
+
]
|
| 21 |
+
SMALL_COMPOSITE_SIZES = [
|
| 22 |
+
2,
|
| 23 |
+
2*3*5,
|
| 24 |
+
2*2*3*3,
|
| 25 |
+
]
|
| 26 |
+
# prime
|
| 27 |
+
LARGE_PRIME_SIZES = [
|
| 28 |
+
2011
|
| 29 |
+
]
|
| 30 |
+
SMALL_PRIME_SIZES = [
|
| 31 |
+
29
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def _assert_close_in_norm(x, y, rtol, size, rdt):
|
| 36 |
+
# helper function for testing
|
| 37 |
+
err_msg = f"size: {size} rdt: {rdt}"
|
| 38 |
+
assert_array_less(np.linalg.norm(x - y), rtol*np.linalg.norm(x), err_msg)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def random(size):
|
| 42 |
+
return rand(*size)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def direct_dft(x):
|
| 46 |
+
x = asarray(x)
|
| 47 |
+
n = len(x)
|
| 48 |
+
y = zeros(n, dtype=cdouble)
|
| 49 |
+
w = -arange(n)*(2j*pi/n)
|
| 50 |
+
for i in range(n):
|
| 51 |
+
y[i] = dot(exp(i*w), x)
|
| 52 |
+
return y
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def direct_idft(x):
|
| 56 |
+
x = asarray(x)
|
| 57 |
+
n = len(x)
|
| 58 |
+
y = zeros(n, dtype=cdouble)
|
| 59 |
+
w = arange(n)*(2j*pi/n)
|
| 60 |
+
for i in range(n):
|
| 61 |
+
y[i] = dot(exp(i*w), x)/n
|
| 62 |
+
return y
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def direct_dftn(x):
|
| 66 |
+
x = asarray(x)
|
| 67 |
+
for axis in range(len(x.shape)):
|
| 68 |
+
x = fft(x, axis=axis)
|
| 69 |
+
return x
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def direct_idftn(x):
|
| 73 |
+
x = asarray(x)
|
| 74 |
+
for axis in range(len(x.shape)):
|
| 75 |
+
x = ifft(x, axis=axis)
|
| 76 |
+
return x
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def direct_rdft(x):
|
| 80 |
+
x = asarray(x)
|
| 81 |
+
n = len(x)
|
| 82 |
+
w = -arange(n)*(2j*pi/n)
|
| 83 |
+
r = zeros(n, dtype=double)
|
| 84 |
+
for i in range(n//2+1):
|
| 85 |
+
y = dot(exp(i*w), x)
|
| 86 |
+
if i:
|
| 87 |
+
r[2*i-1] = y.real
|
| 88 |
+
if 2*i < n:
|
| 89 |
+
r[2*i] = y.imag
|
| 90 |
+
else:
|
| 91 |
+
r[0] = y.real
|
| 92 |
+
return r
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def direct_irdft(x):
|
| 96 |
+
x = asarray(x)
|
| 97 |
+
n = len(x)
|
| 98 |
+
x1 = zeros(n, dtype=cdouble)
|
| 99 |
+
for i in range(n//2+1):
|
| 100 |
+
if i:
|
| 101 |
+
if 2*i < n:
|
| 102 |
+
x1[i] = x[2*i-1] + 1j*x[2*i]
|
| 103 |
+
x1[n-i] = x[2*i-1] - 1j*x[2*i]
|
| 104 |
+
else:
|
| 105 |
+
x1[i] = x[2*i-1]
|
| 106 |
+
else:
|
| 107 |
+
x1[0] = x[0]
|
| 108 |
+
return direct_idft(x1).real
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
class _TestFFTBase:
|
| 112 |
+
def setup_method(self):
|
| 113 |
+
self.cdt = None
|
| 114 |
+
self.rdt = None
|
| 115 |
+
np.random.seed(1234)
|
| 116 |
+
|
| 117 |
+
def test_definition(self):
|
| 118 |
+
x = np.array([1,2,3,4+1j,1,2,3,4+2j], dtype=self.cdt)
|
| 119 |
+
y = fft(x)
|
| 120 |
+
assert_equal(y.dtype, self.cdt)
|
| 121 |
+
y1 = direct_dft(x)
|
| 122 |
+
assert_array_almost_equal(y,y1)
|
| 123 |
+
x = np.array([1,2,3,4+0j,5], dtype=self.cdt)
|
| 124 |
+
assert_array_almost_equal(fft(x),direct_dft(x))
|
| 125 |
+
|
| 126 |
+
def test_n_argument_real(self):
|
| 127 |
+
x1 = np.array([1,2,3,4], dtype=self.rdt)
|
| 128 |
+
x2 = np.array([1,2,3,4], dtype=self.rdt)
|
| 129 |
+
y = fft([x1,x2],n=4)
|
| 130 |
+
assert_equal(y.dtype, self.cdt)
|
| 131 |
+
assert_equal(y.shape,(2,4))
|
| 132 |
+
assert_array_almost_equal(y[0],direct_dft(x1))
|
| 133 |
+
assert_array_almost_equal(y[1],direct_dft(x2))
|
| 134 |
+
|
| 135 |
+
def _test_n_argument_complex(self):
|
| 136 |
+
x1 = np.array([1,2,3,4+1j], dtype=self.cdt)
|
| 137 |
+
x2 = np.array([1,2,3,4+1j], dtype=self.cdt)
|
| 138 |
+
y = fft([x1,x2],n=4)
|
| 139 |
+
assert_equal(y.dtype, self.cdt)
|
| 140 |
+
assert_equal(y.shape,(2,4))
|
| 141 |
+
assert_array_almost_equal(y[0],direct_dft(x1))
|
| 142 |
+
assert_array_almost_equal(y[1],direct_dft(x2))
|
| 143 |
+
|
| 144 |
+
def test_invalid_sizes(self):
|
| 145 |
+
assert_raises(ValueError, fft, [])
|
| 146 |
+
assert_raises(ValueError, fft, [[1,1],[2,2]], -5)
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
class TestDoubleFFT(_TestFFTBase):
|
| 150 |
+
def setup_method(self):
|
| 151 |
+
self.cdt = np.complex128
|
| 152 |
+
self.rdt = np.float64
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
class TestSingleFFT(_TestFFTBase):
|
| 156 |
+
def setup_method(self):
|
| 157 |
+
self.cdt = np.complex64
|
| 158 |
+
self.rdt = np.float32
|
| 159 |
+
|
| 160 |
+
reason = ("single-precision FFT implementation is partially disabled, "
|
| 161 |
+
"until accuracy issues with large prime powers are resolved")
|
| 162 |
+
|
| 163 |
+
@pytest.mark.xfail(run=False, reason=reason)
|
| 164 |
+
def test_notice(self):
|
| 165 |
+
pass
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
class TestFloat16FFT:
|
| 169 |
+
|
| 170 |
+
def test_1_argument_real(self):
|
| 171 |
+
x1 = np.array([1, 2, 3, 4], dtype=np.float16)
|
| 172 |
+
y = fft(x1, n=4)
|
| 173 |
+
assert_equal(y.dtype, np.complex64)
|
| 174 |
+
assert_equal(y.shape, (4, ))
|
| 175 |
+
assert_array_almost_equal(y, direct_dft(x1.astype(np.float32)))
|
| 176 |
+
|
| 177 |
+
def test_n_argument_real(self):
|
| 178 |
+
x1 = np.array([1, 2, 3, 4], dtype=np.float16)
|
| 179 |
+
x2 = np.array([1, 2, 3, 4], dtype=np.float16)
|
| 180 |
+
y = fft([x1, x2], n=4)
|
| 181 |
+
assert_equal(y.dtype, np.complex64)
|
| 182 |
+
assert_equal(y.shape, (2, 4))
|
| 183 |
+
assert_array_almost_equal(y[0], direct_dft(x1.astype(np.float32)))
|
| 184 |
+
assert_array_almost_equal(y[1], direct_dft(x2.astype(np.float32)))
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
class _TestIFFTBase:
|
| 188 |
+
def setup_method(self):
|
| 189 |
+
np.random.seed(1234)
|
| 190 |
+
|
| 191 |
+
def test_definition(self):
|
| 192 |
+
x = np.array([1,2,3,4+1j,1,2,3,4+2j], self.cdt)
|
| 193 |
+
y = ifft(x)
|
| 194 |
+
y1 = direct_idft(x)
|
| 195 |
+
assert_equal(y.dtype, self.cdt)
|
| 196 |
+
assert_array_almost_equal(y,y1)
|
| 197 |
+
|
| 198 |
+
x = np.array([1,2,3,4+0j,5], self.cdt)
|
| 199 |
+
assert_array_almost_equal(ifft(x),direct_idft(x))
|
| 200 |
+
|
| 201 |
+
def test_definition_real(self):
|
| 202 |
+
x = np.array([1,2,3,4,1,2,3,4], self.rdt)
|
| 203 |
+
y = ifft(x)
|
| 204 |
+
assert_equal(y.dtype, self.cdt)
|
| 205 |
+
y1 = direct_idft(x)
|
| 206 |
+
assert_array_almost_equal(y,y1)
|
| 207 |
+
|
| 208 |
+
x = np.array([1,2,3,4,5], dtype=self.rdt)
|
| 209 |
+
assert_equal(y.dtype, self.cdt)
|
| 210 |
+
assert_array_almost_equal(ifft(x),direct_idft(x))
|
| 211 |
+
|
| 212 |
+
def test_random_complex(self):
|
| 213 |
+
for size in [1,51,111,100,200,64,128,256,1024]:
|
| 214 |
+
x = random([size]).astype(self.cdt)
|
| 215 |
+
x = random([size]).astype(self.cdt) + 1j*x
|
| 216 |
+
y1 = ifft(fft(x))
|
| 217 |
+
y2 = fft(ifft(x))
|
| 218 |
+
assert_equal(y1.dtype, self.cdt)
|
| 219 |
+
assert_equal(y2.dtype, self.cdt)
|
| 220 |
+
assert_array_almost_equal(y1, x)
|
| 221 |
+
assert_array_almost_equal(y2, x)
|
| 222 |
+
|
| 223 |
+
def test_random_real(self):
|
| 224 |
+
for size in [1,51,111,100,200,64,128,256,1024]:
|
| 225 |
+
x = random([size]).astype(self.rdt)
|
| 226 |
+
y1 = ifft(fft(x))
|
| 227 |
+
y2 = fft(ifft(x))
|
| 228 |
+
assert_equal(y1.dtype, self.cdt)
|
| 229 |
+
assert_equal(y2.dtype, self.cdt)
|
| 230 |
+
assert_array_almost_equal(y1, x)
|
| 231 |
+
assert_array_almost_equal(y2, x)
|
| 232 |
+
|
| 233 |
+
def test_size_accuracy(self):
|
| 234 |
+
# Sanity check for the accuracy for prime and non-prime sized inputs
|
| 235 |
+
if self.rdt == np.float32:
|
| 236 |
+
rtol = 1e-5
|
| 237 |
+
elif self.rdt == np.float64:
|
| 238 |
+
rtol = 1e-10
|
| 239 |
+
|
| 240 |
+
for size in LARGE_COMPOSITE_SIZES + LARGE_PRIME_SIZES:
|
| 241 |
+
np.random.seed(1234)
|
| 242 |
+
x = np.random.rand(size).astype(self.rdt)
|
| 243 |
+
y = ifft(fft(x))
|
| 244 |
+
_assert_close_in_norm(x, y, rtol, size, self.rdt)
|
| 245 |
+
y = fft(ifft(x))
|
| 246 |
+
_assert_close_in_norm(x, y, rtol, size, self.rdt)
|
| 247 |
+
|
| 248 |
+
x = (x + 1j*np.random.rand(size)).astype(self.cdt)
|
| 249 |
+
y = ifft(fft(x))
|
| 250 |
+
_assert_close_in_norm(x, y, rtol, size, self.rdt)
|
| 251 |
+
y = fft(ifft(x))
|
| 252 |
+
_assert_close_in_norm(x, y, rtol, size, self.rdt)
|
| 253 |
+
|
| 254 |
+
def test_invalid_sizes(self):
|
| 255 |
+
assert_raises(ValueError, ifft, [])
|
| 256 |
+
assert_raises(ValueError, ifft, [[1,1],[2,2]], -5)
|
| 257 |
+
|
| 258 |
+
|
| 259 |
+
class TestDoubleIFFT(_TestIFFTBase):
|
| 260 |
+
def setup_method(self):
|
| 261 |
+
self.cdt = np.complex128
|
| 262 |
+
self.rdt = np.float64
|
| 263 |
+
|
| 264 |
+
|
| 265 |
+
class TestSingleIFFT(_TestIFFTBase):
|
| 266 |
+
def setup_method(self):
|
| 267 |
+
self.cdt = np.complex64
|
| 268 |
+
self.rdt = np.float32
|
| 269 |
+
|
| 270 |
+
|
| 271 |
+
class _TestRFFTBase:
|
| 272 |
+
def setup_method(self):
|
| 273 |
+
np.random.seed(1234)
|
| 274 |
+
|
| 275 |
+
def test_definition(self):
|
| 276 |
+
for t in [[1, 2, 3, 4, 1, 2, 3, 4], [1, 2, 3, 4, 1, 2, 3, 4, 5]]:
|
| 277 |
+
x = np.array(t, dtype=self.rdt)
|
| 278 |
+
y = rfft(x)
|
| 279 |
+
y1 = direct_rdft(x)
|
| 280 |
+
assert_array_almost_equal(y,y1)
|
| 281 |
+
assert_equal(y.dtype, self.rdt)
|
| 282 |
+
|
| 283 |
+
def test_invalid_sizes(self):
|
| 284 |
+
assert_raises(ValueError, rfft, [])
|
| 285 |
+
assert_raises(ValueError, rfft, [[1,1],[2,2]], -5)
|
| 286 |
+
|
| 287 |
+
# See gh-5790
|
| 288 |
+
class MockSeries:
|
| 289 |
+
def __init__(self, data):
|
| 290 |
+
self.data = np.asarray(data)
|
| 291 |
+
|
| 292 |
+
def __getattr__(self, item):
|
| 293 |
+
try:
|
| 294 |
+
return getattr(self.data, item)
|
| 295 |
+
except AttributeError as e:
|
| 296 |
+
raise AttributeError("'MockSeries' object "
|
| 297 |
+
f"has no attribute '{item}'") from e
|
| 298 |
+
|
| 299 |
+
def test_non_ndarray_with_dtype(self):
|
| 300 |
+
x = np.array([1., 2., 3., 4., 5.])
|
| 301 |
+
xs = _TestRFFTBase.MockSeries(x)
|
| 302 |
+
|
| 303 |
+
expected = [1, 2, 3, 4, 5]
|
| 304 |
+
rfft(xs)
|
| 305 |
+
|
| 306 |
+
# Data should not have been overwritten
|
| 307 |
+
assert_equal(x, expected)
|
| 308 |
+
assert_equal(xs.data, expected)
|
| 309 |
+
|
| 310 |
+
def test_complex_input(self):
|
| 311 |
+
assert_raises(TypeError, rfft, np.arange(4, dtype=np.complex64))
|
| 312 |
+
|
| 313 |
+
|
| 314 |
+
class TestRFFTDouble(_TestRFFTBase):
|
| 315 |
+
def setup_method(self):
|
| 316 |
+
self.cdt = np.complex128
|
| 317 |
+
self.rdt = np.float64
|
| 318 |
+
|
| 319 |
+
|
| 320 |
+
class TestRFFTSingle(_TestRFFTBase):
|
| 321 |
+
def setup_method(self):
|
| 322 |
+
self.cdt = np.complex64
|
| 323 |
+
self.rdt = np.float32
|
| 324 |
+
|
| 325 |
+
|
| 326 |
+
class _TestIRFFTBase:
|
| 327 |
+
def setup_method(self):
|
| 328 |
+
np.random.seed(1234)
|
| 329 |
+
|
| 330 |
+
def test_definition(self):
|
| 331 |
+
x1 = [1,2,3,4,1,2,3,4]
|
| 332 |
+
x1_1 = [1,2+3j,4+1j,2+3j,4,2-3j,4-1j,2-3j]
|
| 333 |
+
x2 = [1,2,3,4,1,2,3,4,5]
|
| 334 |
+
x2_1 = [1,2+3j,4+1j,2+3j,4+5j,4-5j,2-3j,4-1j,2-3j]
|
| 335 |
+
|
| 336 |
+
def _test(x, xr):
|
| 337 |
+
y = irfft(np.array(x, dtype=self.rdt))
|
| 338 |
+
y1 = direct_irdft(x)
|
| 339 |
+
assert_equal(y.dtype, self.rdt)
|
| 340 |
+
assert_array_almost_equal(y,y1, decimal=self.ndec)
|
| 341 |
+
assert_array_almost_equal(y,ifft(xr), decimal=self.ndec)
|
| 342 |
+
|
| 343 |
+
_test(x1, x1_1)
|
| 344 |
+
_test(x2, x2_1)
|
| 345 |
+
|
| 346 |
+
def test_random_real(self):
|
| 347 |
+
for size in [1,51,111,100,200,64,128,256,1024]:
|
| 348 |
+
x = random([size]).astype(self.rdt)
|
| 349 |
+
y1 = irfft(rfft(x))
|
| 350 |
+
y2 = rfft(irfft(x))
|
| 351 |
+
assert_equal(y1.dtype, self.rdt)
|
| 352 |
+
assert_equal(y2.dtype, self.rdt)
|
| 353 |
+
assert_array_almost_equal(y1, x, decimal=self.ndec, err_msg=f"size={size}")
|
| 354 |
+
assert_array_almost_equal(y2, x, decimal=self.ndec, err_msg=f"size={size}")
|
| 355 |
+
|
| 356 |
+
def test_size_accuracy(self):
|
| 357 |
+
# Sanity check for the accuracy for prime and non-prime sized inputs
|
| 358 |
+
if self.rdt == np.float32:
|
| 359 |
+
rtol = 1e-5
|
| 360 |
+
elif self.rdt == np.float64:
|
| 361 |
+
rtol = 1e-10
|
| 362 |
+
|
| 363 |
+
for size in LARGE_COMPOSITE_SIZES + LARGE_PRIME_SIZES:
|
| 364 |
+
np.random.seed(1234)
|
| 365 |
+
x = np.random.rand(size).astype(self.rdt)
|
| 366 |
+
y = irfft(rfft(x))
|
| 367 |
+
_assert_close_in_norm(x, y, rtol, size, self.rdt)
|
| 368 |
+
y = rfft(irfft(x))
|
| 369 |
+
_assert_close_in_norm(x, y, rtol, size, self.rdt)
|
| 370 |
+
|
| 371 |
+
def test_invalid_sizes(self):
|
| 372 |
+
assert_raises(ValueError, irfft, [])
|
| 373 |
+
assert_raises(ValueError, irfft, [[1,1],[2,2]], -5)
|
| 374 |
+
|
| 375 |
+
def test_complex_input(self):
|
| 376 |
+
assert_raises(TypeError, irfft, np.arange(4, dtype=np.complex64))
|
| 377 |
+
|
| 378 |
+
|
| 379 |
+
# self.ndec is bogus; we should have a assert_array_approx_equal for number of
|
| 380 |
+
# significant digits
|
| 381 |
+
|
| 382 |
+
class TestIRFFTDouble(_TestIRFFTBase):
|
| 383 |
+
def setup_method(self):
|
| 384 |
+
self.cdt = np.complex128
|
| 385 |
+
self.rdt = np.float64
|
| 386 |
+
self.ndec = 14
|
| 387 |
+
|
| 388 |
+
|
| 389 |
+
class TestIRFFTSingle(_TestIRFFTBase):
|
| 390 |
+
def setup_method(self):
|
| 391 |
+
self.cdt = np.complex64
|
| 392 |
+
self.rdt = np.float32
|
| 393 |
+
self.ndec = 5
|
| 394 |
+
|
| 395 |
+
|
| 396 |
+
class Testfft2:
|
| 397 |
+
def setup_method(self):
|
| 398 |
+
np.random.seed(1234)
|
| 399 |
+
|
| 400 |
+
def test_regression_244(self):
|
| 401 |
+
"""FFT returns wrong result with axes parameter."""
|
| 402 |
+
# fftn (and hence fft2) used to break when both axes and shape were
|
| 403 |
+
# used
|
| 404 |
+
x = numpy.ones((4, 4, 2))
|
| 405 |
+
y = fft2(x, shape=(8, 8), axes=(-3, -2))
|
| 406 |
+
y_r = numpy.fft.fftn(x, s=(8, 8), axes=(-3, -2))
|
| 407 |
+
assert_array_almost_equal(y, y_r)
|
| 408 |
+
|
| 409 |
+
def test_invalid_sizes(self):
|
| 410 |
+
assert_raises(ValueError, fft2, [[]])
|
| 411 |
+
assert_raises(ValueError, fft2, [[1, 1], [2, 2]], (4, -3))
|
| 412 |
+
|
| 413 |
+
|
| 414 |
+
class TestFftnSingle:
|
| 415 |
+
def setup_method(self):
|
| 416 |
+
np.random.seed(1234)
|
| 417 |
+
|
| 418 |
+
def test_definition(self):
|
| 419 |
+
x = [[1, 2, 3],
|
| 420 |
+
[4, 5, 6],
|
| 421 |
+
[7, 8, 9]]
|
| 422 |
+
y = fftn(np.array(x, np.float32))
|
| 423 |
+
assert_(y.dtype == np.complex64,
|
| 424 |
+
msg="double precision output with single precision")
|
| 425 |
+
|
| 426 |
+
y_r = np.array(fftn(x), np.complex64)
|
| 427 |
+
assert_array_almost_equal_nulp(y, y_r)
|
| 428 |
+
|
| 429 |
+
@pytest.mark.parametrize('size', SMALL_COMPOSITE_SIZES + SMALL_PRIME_SIZES)
|
| 430 |
+
def test_size_accuracy_small(self, size):
|
| 431 |
+
rng = np.random.default_rng(1234)
|
| 432 |
+
x = rng.random((size, size)) + 1j*rng.random((size, size))
|
| 433 |
+
y1 = fftn(x.real.astype(np.float32))
|
| 434 |
+
y2 = fftn(x.real.astype(np.float64)).astype(np.complex64)
|
| 435 |
+
|
| 436 |
+
assert_equal(y1.dtype, np.complex64)
|
| 437 |
+
assert_array_almost_equal_nulp(y1, y2, 2000)
|
| 438 |
+
|
| 439 |
+
@pytest.mark.parametrize('size', LARGE_COMPOSITE_SIZES + LARGE_PRIME_SIZES)
|
| 440 |
+
def test_size_accuracy_large(self, size):
|
| 441 |
+
rand = np.random.default_rng(1234)
|
| 442 |
+
x = rand.random((size, 3)) + 1j*rand.random((size, 3))
|
| 443 |
+
y1 = fftn(x.real.astype(np.float32))
|
| 444 |
+
y2 = fftn(x.real.astype(np.float64)).astype(np.complex64)
|
| 445 |
+
|
| 446 |
+
assert_equal(y1.dtype, np.complex64)
|
| 447 |
+
assert_array_almost_equal_nulp(y1, y2, 2000)
|
| 448 |
+
|
| 449 |
+
def test_definition_float16(self):
|
| 450 |
+
x = [[1, 2, 3],
|
| 451 |
+
[4, 5, 6],
|
| 452 |
+
[7, 8, 9]]
|
| 453 |
+
y = fftn(np.array(x, np.float16))
|
| 454 |
+
assert_equal(y.dtype, np.complex64)
|
| 455 |
+
y_r = np.array(fftn(x), np.complex64)
|
| 456 |
+
assert_array_almost_equal_nulp(y, y_r)
|
| 457 |
+
|
| 458 |
+
@pytest.mark.parametrize('size', SMALL_COMPOSITE_SIZES + SMALL_PRIME_SIZES)
|
| 459 |
+
def test_float16_input_small(self, size):
|
| 460 |
+
rng = np.random.default_rng(1234)
|
| 461 |
+
x = rng.random((size, size)) + 1j * rng.random((size, size))
|
| 462 |
+
y1 = fftn(x.real.astype(np.float16))
|
| 463 |
+
y2 = fftn(x.real.astype(np.float64)).astype(np.complex64)
|
| 464 |
+
|
| 465 |
+
assert_equal(y1.dtype, np.complex64)
|
| 466 |
+
assert_array_almost_equal_nulp(y1, y2, 5e5)
|
| 467 |
+
|
| 468 |
+
@pytest.mark.parametrize('size', LARGE_COMPOSITE_SIZES + LARGE_PRIME_SIZES)
|
| 469 |
+
def test_float16_input_large(self, size):
|
| 470 |
+
rng = np.random.default_rng(1234)
|
| 471 |
+
x = rng.random((size, 3)) + 1j*rng.random((size, 3))
|
| 472 |
+
y1 = fftn(x.real.astype(np.float16))
|
| 473 |
+
y2 = fftn(x.real.astype(np.float64)).astype(np.complex64)
|
| 474 |
+
|
| 475 |
+
assert_equal(y1.dtype, np.complex64)
|
| 476 |
+
assert_array_almost_equal_nulp(y1, y2, 2e6)
|
| 477 |
+
|
| 478 |
+
|
| 479 |
+
class TestFftn:
|
| 480 |
+
def setup_method(self):
|
| 481 |
+
np.random.seed(1234)
|
| 482 |
+
|
| 483 |
+
def test_definition(self):
|
| 484 |
+
x = [[1, 2, 3],
|
| 485 |
+
[4, 5, 6],
|
| 486 |
+
[7, 8, 9]]
|
| 487 |
+
y = fftn(x)
|
| 488 |
+
assert_array_almost_equal(y, direct_dftn(x))
|
| 489 |
+
|
| 490 |
+
x = random((20, 26))
|
| 491 |
+
assert_array_almost_equal(fftn(x), direct_dftn(x))
|
| 492 |
+
|
| 493 |
+
x = random((5, 4, 3, 20))
|
| 494 |
+
assert_array_almost_equal(fftn(x), direct_dftn(x))
|
| 495 |
+
|
| 496 |
+
def test_axes_argument(self):
|
| 497 |
+
# plane == ji_plane, x== kji_space
|
| 498 |
+
plane1 = [[1, 2, 3],
|
| 499 |
+
[4, 5, 6],
|
| 500 |
+
[7, 8, 9]]
|
| 501 |
+
plane2 = [[10, 11, 12],
|
| 502 |
+
[13, 14, 15],
|
| 503 |
+
[16, 17, 18]]
|
| 504 |
+
plane3 = [[19, 20, 21],
|
| 505 |
+
[22, 23, 24],
|
| 506 |
+
[25, 26, 27]]
|
| 507 |
+
ki_plane1 = [[1, 2, 3],
|
| 508 |
+
[10, 11, 12],
|
| 509 |
+
[19, 20, 21]]
|
| 510 |
+
ki_plane2 = [[4, 5, 6],
|
| 511 |
+
[13, 14, 15],
|
| 512 |
+
[22, 23, 24]]
|
| 513 |
+
ki_plane3 = [[7, 8, 9],
|
| 514 |
+
[16, 17, 18],
|
| 515 |
+
[25, 26, 27]]
|
| 516 |
+
jk_plane1 = [[1, 10, 19],
|
| 517 |
+
[4, 13, 22],
|
| 518 |
+
[7, 16, 25]]
|
| 519 |
+
jk_plane2 = [[2, 11, 20],
|
| 520 |
+
[5, 14, 23],
|
| 521 |
+
[8, 17, 26]]
|
| 522 |
+
jk_plane3 = [[3, 12, 21],
|
| 523 |
+
[6, 15, 24],
|
| 524 |
+
[9, 18, 27]]
|
| 525 |
+
kj_plane1 = [[1, 4, 7],
|
| 526 |
+
[10, 13, 16], [19, 22, 25]]
|
| 527 |
+
kj_plane2 = [[2, 5, 8],
|
| 528 |
+
[11, 14, 17], [20, 23, 26]]
|
| 529 |
+
kj_plane3 = [[3, 6, 9],
|
| 530 |
+
[12, 15, 18], [21, 24, 27]]
|
| 531 |
+
ij_plane1 = [[1, 4, 7],
|
| 532 |
+
[2, 5, 8],
|
| 533 |
+
[3, 6, 9]]
|
| 534 |
+
ij_plane2 = [[10, 13, 16],
|
| 535 |
+
[11, 14, 17],
|
| 536 |
+
[12, 15, 18]]
|
| 537 |
+
ij_plane3 = [[19, 22, 25],
|
| 538 |
+
[20, 23, 26],
|
| 539 |
+
[21, 24, 27]]
|
| 540 |
+
ik_plane1 = [[1, 10, 19],
|
| 541 |
+
[2, 11, 20],
|
| 542 |
+
[3, 12, 21]]
|
| 543 |
+
ik_plane2 = [[4, 13, 22],
|
| 544 |
+
[5, 14, 23],
|
| 545 |
+
[6, 15, 24]]
|
| 546 |
+
ik_plane3 = [[7, 16, 25],
|
| 547 |
+
[8, 17, 26],
|
| 548 |
+
[9, 18, 27]]
|
| 549 |
+
ijk_space = [jk_plane1, jk_plane2, jk_plane3]
|
| 550 |
+
ikj_space = [kj_plane1, kj_plane2, kj_plane3]
|
| 551 |
+
jik_space = [ik_plane1, ik_plane2, ik_plane3]
|
| 552 |
+
jki_space = [ki_plane1, ki_plane2, ki_plane3]
|
| 553 |
+
kij_space = [ij_plane1, ij_plane2, ij_plane3]
|
| 554 |
+
x = array([plane1, plane2, plane3])
|
| 555 |
+
|
| 556 |
+
assert_array_almost_equal(fftn(x),
|
| 557 |
+
fftn(x, axes=(-3, -2, -1))) # kji_space
|
| 558 |
+
assert_array_almost_equal(fftn(x), fftn(x, axes=(0, 1, 2)))
|
| 559 |
+
assert_array_almost_equal(fftn(x, axes=(0, 2)), fftn(x, axes=(0, -1)))
|
| 560 |
+
y = fftn(x, axes=(2, 1, 0)) # ijk_space
|
| 561 |
+
assert_array_almost_equal(swapaxes(y, -1, -3), fftn(ijk_space))
|
| 562 |
+
y = fftn(x, axes=(2, 0, 1)) # ikj_space
|
| 563 |
+
assert_array_almost_equal(swapaxes(swapaxes(y, -1, -3), -1, -2),
|
| 564 |
+
fftn(ikj_space))
|
| 565 |
+
y = fftn(x, axes=(1, 2, 0)) # jik_space
|
| 566 |
+
assert_array_almost_equal(swapaxes(swapaxes(y, -1, -3), -3, -2),
|
| 567 |
+
fftn(jik_space))
|
| 568 |
+
y = fftn(x, axes=(1, 0, 2)) # jki_space
|
| 569 |
+
assert_array_almost_equal(swapaxes(y, -2, -3), fftn(jki_space))
|
| 570 |
+
y = fftn(x, axes=(0, 2, 1)) # kij_space
|
| 571 |
+
assert_array_almost_equal(swapaxes(y, -2, -1), fftn(kij_space))
|
| 572 |
+
|
| 573 |
+
y = fftn(x, axes=(-2, -1)) # ji_plane
|
| 574 |
+
assert_array_almost_equal(fftn(plane1), y[0])
|
| 575 |
+
assert_array_almost_equal(fftn(plane2), y[1])
|
| 576 |
+
assert_array_almost_equal(fftn(plane3), y[2])
|
| 577 |
+
|
| 578 |
+
y = fftn(x, axes=(1, 2)) # ji_plane
|
| 579 |
+
assert_array_almost_equal(fftn(plane1), y[0])
|
| 580 |
+
assert_array_almost_equal(fftn(plane2), y[1])
|
| 581 |
+
assert_array_almost_equal(fftn(plane3), y[2])
|
| 582 |
+
|
| 583 |
+
y = fftn(x, axes=(-3, -2)) # kj_plane
|
| 584 |
+
assert_array_almost_equal(fftn(x[:, :, 0]), y[:, :, 0])
|
| 585 |
+
assert_array_almost_equal(fftn(x[:, :, 1]), y[:, :, 1])
|
| 586 |
+
assert_array_almost_equal(fftn(x[:, :, 2]), y[:, :, 2])
|
| 587 |
+
|
| 588 |
+
y = fftn(x, axes=(-3, -1)) # ki_plane
|
| 589 |
+
assert_array_almost_equal(fftn(x[:, 0, :]), y[:, 0, :])
|
| 590 |
+
assert_array_almost_equal(fftn(x[:, 1, :]), y[:, 1, :])
|
| 591 |
+
assert_array_almost_equal(fftn(x[:, 2, :]), y[:, 2, :])
|
| 592 |
+
|
| 593 |
+
y = fftn(x, axes=(-1, -2)) # ij_plane
|
| 594 |
+
assert_array_almost_equal(fftn(ij_plane1), swapaxes(y[0], -2, -1))
|
| 595 |
+
assert_array_almost_equal(fftn(ij_plane2), swapaxes(y[1], -2, -1))
|
| 596 |
+
assert_array_almost_equal(fftn(ij_plane3), swapaxes(y[2], -2, -1))
|
| 597 |
+
|
| 598 |
+
y = fftn(x, axes=(-1, -3)) # ik_plane
|
| 599 |
+
assert_array_almost_equal(fftn(ik_plane1),
|
| 600 |
+
swapaxes(y[:, 0, :], -1, -2))
|
| 601 |
+
assert_array_almost_equal(fftn(ik_plane2),
|
| 602 |
+
swapaxes(y[:, 1, :], -1, -2))
|
| 603 |
+
assert_array_almost_equal(fftn(ik_plane3),
|
| 604 |
+
swapaxes(y[:, 2, :], -1, -2))
|
| 605 |
+
|
| 606 |
+
y = fftn(x, axes=(-2, -3)) # jk_plane
|
| 607 |
+
assert_array_almost_equal(fftn(jk_plane1),
|
| 608 |
+
swapaxes(y[:, :, 0], -1, -2))
|
| 609 |
+
assert_array_almost_equal(fftn(jk_plane2),
|
| 610 |
+
swapaxes(y[:, :, 1], -1, -2))
|
| 611 |
+
assert_array_almost_equal(fftn(jk_plane3),
|
| 612 |
+
swapaxes(y[:, :, 2], -1, -2))
|
| 613 |
+
|
| 614 |
+
y = fftn(x, axes=(-1,)) # i_line
|
| 615 |
+
for i in range(3):
|
| 616 |
+
for j in range(3):
|
| 617 |
+
assert_array_almost_equal(fft(x[i, j, :]), y[i, j, :])
|
| 618 |
+
y = fftn(x, axes=(-2,)) # j_line
|
| 619 |
+
for i in range(3):
|
| 620 |
+
for j in range(3):
|
| 621 |
+
assert_array_almost_equal(fft(x[i, :, j]), y[i, :, j])
|
| 622 |
+
y = fftn(x, axes=(0,)) # k_line
|
| 623 |
+
for i in range(3):
|
| 624 |
+
for j in range(3):
|
| 625 |
+
assert_array_almost_equal(fft(x[:, i, j]), y[:, i, j])
|
| 626 |
+
|
| 627 |
+
y = fftn(x, axes=()) # point
|
| 628 |
+
assert_array_almost_equal(y, x)
|
| 629 |
+
|
| 630 |
+
def test_shape_argument(self):
|
| 631 |
+
small_x = [[1, 2, 3],
|
| 632 |
+
[4, 5, 6]]
|
| 633 |
+
large_x1 = [[1, 2, 3, 0],
|
| 634 |
+
[4, 5, 6, 0],
|
| 635 |
+
[0, 0, 0, 0],
|
| 636 |
+
[0, 0, 0, 0]]
|
| 637 |
+
|
| 638 |
+
y = fftn(small_x, shape=(4, 4))
|
| 639 |
+
assert_array_almost_equal(y, fftn(large_x1))
|
| 640 |
+
|
| 641 |
+
y = fftn(small_x, shape=(3, 4))
|
| 642 |
+
assert_array_almost_equal(y, fftn(large_x1[:-1]))
|
| 643 |
+
|
| 644 |
+
def test_shape_axes_argument(self):
|
| 645 |
+
small_x = [[1, 2, 3],
|
| 646 |
+
[4, 5, 6],
|
| 647 |
+
[7, 8, 9]]
|
| 648 |
+
large_x1 = array([[1, 2, 3, 0],
|
| 649 |
+
[4, 5, 6, 0],
|
| 650 |
+
[7, 8, 9, 0],
|
| 651 |
+
[0, 0, 0, 0]])
|
| 652 |
+
y = fftn(small_x, shape=(4, 4), axes=(-2, -1))
|
| 653 |
+
assert_array_almost_equal(y, fftn(large_x1))
|
| 654 |
+
y = fftn(small_x, shape=(4, 4), axes=(-1, -2))
|
| 655 |
+
|
| 656 |
+
assert_array_almost_equal(y, swapaxes(
|
| 657 |
+
fftn(swapaxes(large_x1, -1, -2)), -1, -2))
|
| 658 |
+
|
| 659 |
+
def test_shape_axes_argument2(self):
|
| 660 |
+
# Change shape of the last axis
|
| 661 |
+
x = numpy.random.random((10, 5, 3, 7))
|
| 662 |
+
y = fftn(x, axes=(-1,), shape=(8,))
|
| 663 |
+
assert_array_almost_equal(y, fft(x, axis=-1, n=8))
|
| 664 |
+
|
| 665 |
+
# Change shape of an arbitrary axis which is not the last one
|
| 666 |
+
x = numpy.random.random((10, 5, 3, 7))
|
| 667 |
+
y = fftn(x, axes=(-2,), shape=(8,))
|
| 668 |
+
assert_array_almost_equal(y, fft(x, axis=-2, n=8))
|
| 669 |
+
|
| 670 |
+
# Change shape of axes: cf #244, where shape and axes were mixed up
|
| 671 |
+
x = numpy.random.random((4, 4, 2))
|
| 672 |
+
y = fftn(x, axes=(-3, -2), shape=(8, 8))
|
| 673 |
+
assert_array_almost_equal(y,
|
| 674 |
+
numpy.fft.fftn(x, axes=(-3, -2), s=(8, 8)))
|
| 675 |
+
|
| 676 |
+
def test_shape_argument_more(self):
|
| 677 |
+
x = zeros((4, 4, 2))
|
| 678 |
+
with assert_raises(ValueError,
|
| 679 |
+
match="when given, axes and shape arguments"
|
| 680 |
+
" have to be of the same length"):
|
| 681 |
+
fftn(x, shape=(8, 8, 2, 1))
|
| 682 |
+
|
| 683 |
+
def test_invalid_sizes(self):
|
| 684 |
+
with assert_raises(ValueError,
|
| 685 |
+
match="invalid number of data points"
|
| 686 |
+
r" \(\[1, 0\]\) specified"):
|
| 687 |
+
fftn([[]])
|
| 688 |
+
|
| 689 |
+
with assert_raises(ValueError,
|
| 690 |
+
match="invalid number of data points"
|
| 691 |
+
r" \(\[4, -3\]\) specified"):
|
| 692 |
+
fftn([[1, 1], [2, 2]], (4, -3))
|
| 693 |
+
|
| 694 |
+
|
| 695 |
+
class TestIfftn:
|
| 696 |
+
dtype = None
|
| 697 |
+
cdtype = None
|
| 698 |
+
|
| 699 |
+
def setup_method(self):
|
| 700 |
+
np.random.seed(1234)
|
| 701 |
+
|
| 702 |
+
@pytest.mark.parametrize('dtype,cdtype,maxnlp',
|
| 703 |
+
[(np.float64, np.complex128, 2000),
|
| 704 |
+
(np.float32, np.complex64, 3500)])
|
| 705 |
+
def test_definition(self, dtype, cdtype, maxnlp):
|
| 706 |
+
rng = np.random.default_rng(1234)
|
| 707 |
+
x = np.array([[1, 2, 3],
|
| 708 |
+
[4, 5, 6],
|
| 709 |
+
[7, 8, 9]], dtype=dtype)
|
| 710 |
+
y = ifftn(x)
|
| 711 |
+
assert_equal(y.dtype, cdtype)
|
| 712 |
+
assert_array_almost_equal_nulp(y, direct_idftn(x), maxnlp)
|
| 713 |
+
|
| 714 |
+
x = rng.random((20, 26))
|
| 715 |
+
assert_array_almost_equal_nulp(ifftn(x), direct_idftn(x), maxnlp)
|
| 716 |
+
|
| 717 |
+
x = rng.random((5, 4, 3, 20))
|
| 718 |
+
assert_array_almost_equal_nulp(ifftn(x), direct_idftn(x), maxnlp)
|
| 719 |
+
|
| 720 |
+
@pytest.mark.parametrize('maxnlp', [2000, 3500])
|
| 721 |
+
@pytest.mark.parametrize('size', [1, 2, 51, 32, 64, 92])
|
| 722 |
+
def test_random_complex(self, maxnlp, size):
|
| 723 |
+
rng = np.random.default_rng(1234)
|
| 724 |
+
x = rng.random([size, size]) + 1j * rng.random([size, size])
|
| 725 |
+
assert_array_almost_equal_nulp(ifftn(fftn(x)), x, maxnlp)
|
| 726 |
+
assert_array_almost_equal_nulp(fftn(ifftn(x)), x, maxnlp)
|
| 727 |
+
|
| 728 |
+
def test_invalid_sizes(self):
|
| 729 |
+
with assert_raises(ValueError,
|
| 730 |
+
match="invalid number of data points"
|
| 731 |
+
r" \(\[1, 0\]\) specified"):
|
| 732 |
+
ifftn([[]])
|
| 733 |
+
|
| 734 |
+
with assert_raises(ValueError,
|
| 735 |
+
match="invalid number of data points"
|
| 736 |
+
r" \(\[4, -3\]\) specified"):
|
| 737 |
+
ifftn([[1, 1], [2, 2]], (4, -3))
|
| 738 |
+
|
| 739 |
+
|
| 740 |
+
class FakeArray:
|
| 741 |
+
def __init__(self, data):
|
| 742 |
+
self._data = data
|
| 743 |
+
self.__array_interface__ = data.__array_interface__
|
| 744 |
+
|
| 745 |
+
|
| 746 |
+
class FakeArray2:
|
| 747 |
+
def __init__(self, data):
|
| 748 |
+
self._data = data
|
| 749 |
+
|
| 750 |
+
def __array__(self, dtype=None, copy=None):
|
| 751 |
+
return self._data
|
| 752 |
+
|
| 753 |
+
|
| 754 |
+
class TestOverwrite:
|
| 755 |
+
"""Check input overwrite behavior of the FFT functions."""
|
| 756 |
+
|
| 757 |
+
real_dtypes = (np.float32, np.float64)
|
| 758 |
+
dtypes = real_dtypes + (np.complex64, np.complex128)
|
| 759 |
+
fftsizes = [8, 16, 32]
|
| 760 |
+
|
| 761 |
+
def _check(self, x, routine, fftsize, axis, overwrite_x):
|
| 762 |
+
x2 = x.copy()
|
| 763 |
+
for fake in [lambda x: x, FakeArray, FakeArray2]:
|
| 764 |
+
routine(fake(x2), fftsize, axis, overwrite_x=overwrite_x)
|
| 765 |
+
|
| 766 |
+
sig = (f"{routine.__name__}({x.dtype}{x.shape!r}, {fftsize!r}, "
|
| 767 |
+
f"axis={axis!r}, overwrite_x={overwrite_x!r})")
|
| 768 |
+
if not overwrite_x:
|
| 769 |
+
assert_equal(x2, x, err_msg=f"spurious overwrite in {sig}")
|
| 770 |
+
|
| 771 |
+
def _check_1d(self, routine, dtype, shape, axis, overwritable_dtypes,
|
| 772 |
+
fftsize, overwrite_x):
|
| 773 |
+
np.random.seed(1234)
|
| 774 |
+
if np.issubdtype(dtype, np.complexfloating):
|
| 775 |
+
data = np.random.randn(*shape) + 1j*np.random.randn(*shape)
|
| 776 |
+
else:
|
| 777 |
+
data = np.random.randn(*shape)
|
| 778 |
+
data = data.astype(dtype)
|
| 779 |
+
|
| 780 |
+
self._check(data, routine, fftsize, axis,
|
| 781 |
+
overwrite_x=overwrite_x)
|
| 782 |
+
|
| 783 |
+
@pytest.mark.parametrize('dtype', dtypes)
|
| 784 |
+
@pytest.mark.parametrize('fftsize', fftsizes)
|
| 785 |
+
@pytest.mark.parametrize('overwrite_x', [True, False])
|
| 786 |
+
@pytest.mark.parametrize('shape,axes', [((16,), -1),
|
| 787 |
+
((16, 2), 0),
|
| 788 |
+
((2, 16), 1)])
|
| 789 |
+
def test_fft_ifft(self, dtype, fftsize, overwrite_x, shape, axes):
|
| 790 |
+
overwritable = (np.complex128, np.complex64)
|
| 791 |
+
self._check_1d(fft, dtype, shape, axes, overwritable,
|
| 792 |
+
fftsize, overwrite_x)
|
| 793 |
+
self._check_1d(ifft, dtype, shape, axes, overwritable,
|
| 794 |
+
fftsize, overwrite_x)
|
| 795 |
+
|
| 796 |
+
@pytest.mark.parametrize('dtype', real_dtypes)
|
| 797 |
+
@pytest.mark.parametrize('fftsize', fftsizes)
|
| 798 |
+
@pytest.mark.parametrize('overwrite_x', [True, False])
|
| 799 |
+
@pytest.mark.parametrize('shape,axes', [((16,), -1),
|
| 800 |
+
((16, 2), 0),
|
| 801 |
+
((2, 16), 1)])
|
| 802 |
+
def test_rfft_irfft(self, dtype, fftsize, overwrite_x, shape, axes):
|
| 803 |
+
overwritable = self.real_dtypes
|
| 804 |
+
self._check_1d(irfft, dtype, shape, axes, overwritable,
|
| 805 |
+
fftsize, overwrite_x)
|
| 806 |
+
self._check_1d(rfft, dtype, shape, axes, overwritable,
|
| 807 |
+
fftsize, overwrite_x)
|
| 808 |
+
|
| 809 |
+
def _check_nd_one(self, routine, dtype, shape, axes, overwritable_dtypes,
|
| 810 |
+
overwrite_x):
|
| 811 |
+
np.random.seed(1234)
|
| 812 |
+
if np.issubdtype(dtype, np.complexfloating):
|
| 813 |
+
data = np.random.randn(*shape) + 1j*np.random.randn(*shape)
|
| 814 |
+
else:
|
| 815 |
+
data = np.random.randn(*shape)
|
| 816 |
+
data = data.astype(dtype)
|
| 817 |
+
|
| 818 |
+
def fftshape_iter(shp):
|
| 819 |
+
if len(shp) <= 0:
|
| 820 |
+
yield ()
|
| 821 |
+
else:
|
| 822 |
+
for j in (shp[0]//2, shp[0], shp[0]*2):
|
| 823 |
+
for rest in fftshape_iter(shp[1:]):
|
| 824 |
+
yield (j,) + rest
|
| 825 |
+
|
| 826 |
+
if axes is None:
|
| 827 |
+
part_shape = shape
|
| 828 |
+
else:
|
| 829 |
+
part_shape = tuple(np.take(shape, axes))
|
| 830 |
+
|
| 831 |
+
for fftshape in fftshape_iter(part_shape):
|
| 832 |
+
self._check(data, routine, fftshape, axes,
|
| 833 |
+
overwrite_x=overwrite_x)
|
| 834 |
+
if data.ndim > 1:
|
| 835 |
+
self._check(data.T, routine, fftshape, axes,
|
| 836 |
+
overwrite_x=overwrite_x)
|
| 837 |
+
|
| 838 |
+
@pytest.mark.parametrize('dtype', dtypes)
|
| 839 |
+
@pytest.mark.parametrize('overwrite_x', [True, False])
|
| 840 |
+
@pytest.mark.parametrize('shape,axes', [((16,), None),
|
| 841 |
+
((16,), (0,)),
|
| 842 |
+
((16, 2), (0,)),
|
| 843 |
+
((2, 16), (1,)),
|
| 844 |
+
((8, 16), None),
|
| 845 |
+
((8, 16), (0, 1)),
|
| 846 |
+
((8, 16, 2), (0, 1)),
|
| 847 |
+
((8, 16, 2), (1, 2)),
|
| 848 |
+
((8, 16, 2), (0,)),
|
| 849 |
+
((8, 16, 2), (1,)),
|
| 850 |
+
((8, 16, 2), (2,)),
|
| 851 |
+
((8, 16, 2), None),
|
| 852 |
+
((8, 16, 2), (0, 1, 2))])
|
| 853 |
+
def test_fftn_ifftn(self, dtype, overwrite_x, shape, axes):
|
| 854 |
+
overwritable = (np.complex128, np.complex64)
|
| 855 |
+
self._check_nd_one(fftn, dtype, shape, axes, overwritable,
|
| 856 |
+
overwrite_x)
|
| 857 |
+
self._check_nd_one(ifftn, dtype, shape, axes, overwritable,
|
| 858 |
+
overwrite_x)
|
| 859 |
+
|
| 860 |
+
|
| 861 |
+
@pytest.mark.parametrize('func', [fftn, ifftn, fft2])
|
| 862 |
+
def test_shape_axes_ndarray(func):
|
| 863 |
+
# Test fftn and ifftn work with NumPy arrays for shape and axes arguments
|
| 864 |
+
# Regression test for gh-13342
|
| 865 |
+
a = np.random.rand(10, 10)
|
| 866 |
+
|
| 867 |
+
expect = func(a, shape=(5, 5))
|
| 868 |
+
actual = func(a, shape=np.array([5, 5]))
|
| 869 |
+
assert_equal(expect, actual)
|
| 870 |
+
|
| 871 |
+
expect = func(a, axes=(-1,))
|
| 872 |
+
actual = func(a, axes=np.array([-1,]))
|
| 873 |
+
assert_equal(expect, actual)
|
| 874 |
+
|
| 875 |
+
expect = func(a, shape=(4, 7), axes=(1, 0))
|
| 876 |
+
actual = func(a, shape=np.array([4, 7]), axes=np.array([1, 0]))
|
| 877 |
+
assert_equal(expect, actual)
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/tests/test_helper.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Created by Pearu Peterson, September 2002
|
| 2 |
+
|
| 3 |
+
__usage__ = """
|
| 4 |
+
Build fftpack:
|
| 5 |
+
python setup_fftpack.py build
|
| 6 |
+
Run tests if scipy is installed:
|
| 7 |
+
python -c 'import scipy;scipy.fftpack.test(<level>)'
|
| 8 |
+
Run tests if fftpack is not installed:
|
| 9 |
+
python tests/test_helper.py [<level>]
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
from numpy.testing import assert_array_almost_equal
|
| 13 |
+
from scipy.fftpack import fftshift, ifftshift, fftfreq, rfftfreq
|
| 14 |
+
|
| 15 |
+
from numpy import pi, random
|
| 16 |
+
|
| 17 |
+
class TestFFTShift:
|
| 18 |
+
|
| 19 |
+
def test_definition(self):
|
| 20 |
+
x = [0,1,2,3,4,-4,-3,-2,-1]
|
| 21 |
+
y = [-4,-3,-2,-1,0,1,2,3,4]
|
| 22 |
+
assert_array_almost_equal(fftshift(x),y)
|
| 23 |
+
assert_array_almost_equal(ifftshift(y),x)
|
| 24 |
+
x = [0,1,2,3,4,-5,-4,-3,-2,-1]
|
| 25 |
+
y = [-5,-4,-3,-2,-1,0,1,2,3,4]
|
| 26 |
+
assert_array_almost_equal(fftshift(x),y)
|
| 27 |
+
assert_array_almost_equal(ifftshift(y),x)
|
| 28 |
+
|
| 29 |
+
def test_inverse(self):
|
| 30 |
+
for n in [1,4,9,100,211]:
|
| 31 |
+
x = random.random((n,))
|
| 32 |
+
assert_array_almost_equal(ifftshift(fftshift(x)),x)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
class TestFFTFreq:
|
| 36 |
+
|
| 37 |
+
def test_definition(self):
|
| 38 |
+
x = [0,1,2,3,4,-4,-3,-2,-1]
|
| 39 |
+
assert_array_almost_equal(9*fftfreq(9),x)
|
| 40 |
+
assert_array_almost_equal(9*pi*fftfreq(9,pi),x)
|
| 41 |
+
x = [0,1,2,3,4,-5,-4,-3,-2,-1]
|
| 42 |
+
assert_array_almost_equal(10*fftfreq(10),x)
|
| 43 |
+
assert_array_almost_equal(10*pi*fftfreq(10,pi),x)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
class TestRFFTFreq:
|
| 47 |
+
|
| 48 |
+
def test_definition(self):
|
| 49 |
+
x = [0,1,1,2,2,3,3,4,4]
|
| 50 |
+
assert_array_almost_equal(9*rfftfreq(9),x)
|
| 51 |
+
assert_array_almost_equal(9*pi*rfftfreq(9,pi),x)
|
| 52 |
+
x = [0,1,1,2,2,3,3,4,4,5]
|
| 53 |
+
assert_array_almost_equal(10*rfftfreq(10),x)
|
| 54 |
+
assert_array_almost_equal(10*pi*rfftfreq(10,pi),x)
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/tests/test_import.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Test possibility of patching fftpack with pyfftw.
|
| 2 |
+
|
| 3 |
+
No module source outside of scipy.fftpack should contain an import of
|
| 4 |
+
the form `from scipy.fftpack import ...`, so that a simple replacement
|
| 5 |
+
of scipy.fftpack by the corresponding fftw interface completely swaps
|
| 6 |
+
the two FFT implementations.
|
| 7 |
+
|
| 8 |
+
Because this simply inspects source files, we only need to run the test
|
| 9 |
+
on one version of Python.
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
from pathlib import Path
|
| 14 |
+
import re
|
| 15 |
+
import tokenize
|
| 16 |
+
import pytest
|
| 17 |
+
from numpy.testing import assert_
|
| 18 |
+
import scipy
|
| 19 |
+
|
| 20 |
+
class TestFFTPackImport:
|
| 21 |
+
@pytest.mark.slow
|
| 22 |
+
def test_fftpack_import(self):
|
| 23 |
+
base = Path(scipy.__file__).parent
|
| 24 |
+
regexp = r"\s*from.+\.fftpack import .*\n"
|
| 25 |
+
for path in base.rglob("*.py"):
|
| 26 |
+
if base / "fftpack" in path.parents:
|
| 27 |
+
continue
|
| 28 |
+
# use tokenize to auto-detect encoding on systems where no
|
| 29 |
+
# default encoding is defined (e.g., LANG='C')
|
| 30 |
+
with tokenize.open(str(path)) as file:
|
| 31 |
+
assert_(all(not re.fullmatch(regexp, line)
|
| 32 |
+
for line in file),
|
| 33 |
+
f"{path} contains an import from fftpack")
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/tests/test_pseudo_diffs.py
ADDED
|
@@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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Created by Pearu Peterson, September 2002
|
| 2 |
+
|
| 3 |
+
__usage__ = """
|
| 4 |
+
Build fftpack:
|
| 5 |
+
python setup_fftpack.py build
|
| 6 |
+
Run tests if scipy is installed:
|
| 7 |
+
python -c 'import scipy;scipy.fftpack.test(<level>)'
|
| 8 |
+
Run tests if fftpack is not installed:
|
| 9 |
+
python tests/test_pseudo_diffs.py [<level>]
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
from numpy.testing import (assert_equal, assert_almost_equal,
|
| 13 |
+
assert_array_almost_equal)
|
| 14 |
+
from scipy.fftpack import (diff, fft, ifft, tilbert, itilbert, hilbert,
|
| 15 |
+
ihilbert, shift, fftfreq, cs_diff, sc_diff,
|
| 16 |
+
ss_diff, cc_diff)
|
| 17 |
+
|
| 18 |
+
import numpy as np
|
| 19 |
+
from numpy import arange, sin, cos, pi, exp, tanh, sum, sign
|
| 20 |
+
from numpy.random import random
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def direct_diff(x,k=1,period=None):
|
| 24 |
+
fx = fft(x)
|
| 25 |
+
n = len(fx)
|
| 26 |
+
if period is None:
|
| 27 |
+
period = 2*pi
|
| 28 |
+
w = fftfreq(n)*2j*pi/period*n
|
| 29 |
+
if k < 0:
|
| 30 |
+
w = 1 / w**k
|
| 31 |
+
w[0] = 0.0
|
| 32 |
+
else:
|
| 33 |
+
w = w**k
|
| 34 |
+
if n > 2000:
|
| 35 |
+
w[250:n-250] = 0.0
|
| 36 |
+
return ifft(w*fx).real
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def direct_tilbert(x,h=1,period=None):
|
| 40 |
+
fx = fft(x)
|
| 41 |
+
n = len(fx)
|
| 42 |
+
if period is None:
|
| 43 |
+
period = 2*pi
|
| 44 |
+
w = fftfreq(n)*h*2*pi/period*n
|
| 45 |
+
w[0] = 1
|
| 46 |
+
w = 1j/tanh(w)
|
| 47 |
+
w[0] = 0j
|
| 48 |
+
return ifft(w*fx)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def direct_itilbert(x,h=1,period=None):
|
| 52 |
+
fx = fft(x)
|
| 53 |
+
n = len(fx)
|
| 54 |
+
if period is None:
|
| 55 |
+
period = 2*pi
|
| 56 |
+
w = fftfreq(n)*h*2*pi/period*n
|
| 57 |
+
w = -1j*tanh(w)
|
| 58 |
+
return ifft(w*fx)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def direct_hilbert(x):
|
| 62 |
+
fx = fft(x)
|
| 63 |
+
n = len(fx)
|
| 64 |
+
w = fftfreq(n)*n
|
| 65 |
+
w = 1j*sign(w)
|
| 66 |
+
return ifft(w*fx)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def direct_ihilbert(x):
|
| 70 |
+
return -direct_hilbert(x)
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def direct_shift(x,a,period=None):
|
| 74 |
+
n = len(x)
|
| 75 |
+
if period is None:
|
| 76 |
+
k = fftfreq(n)*1j*n
|
| 77 |
+
else:
|
| 78 |
+
k = fftfreq(n)*2j*pi/period*n
|
| 79 |
+
return ifft(fft(x)*exp(k*a)).real
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
class TestDiff:
|
| 83 |
+
|
| 84 |
+
def test_definition(self):
|
| 85 |
+
for n in [16,17,64,127,32]:
|
| 86 |
+
x = arange(n)*2*pi/n
|
| 87 |
+
assert_array_almost_equal(diff(sin(x)),direct_diff(sin(x)))
|
| 88 |
+
assert_array_almost_equal(diff(sin(x),2),direct_diff(sin(x),2))
|
| 89 |
+
assert_array_almost_equal(diff(sin(x),3),direct_diff(sin(x),3))
|
| 90 |
+
assert_array_almost_equal(diff(sin(x),4),direct_diff(sin(x),4))
|
| 91 |
+
assert_array_almost_equal(diff(sin(x),5),direct_diff(sin(x),5))
|
| 92 |
+
assert_array_almost_equal(diff(sin(2*x),3),direct_diff(sin(2*x),3))
|
| 93 |
+
assert_array_almost_equal(diff(sin(2*x),4),direct_diff(sin(2*x),4))
|
| 94 |
+
assert_array_almost_equal(diff(cos(x)),direct_diff(cos(x)))
|
| 95 |
+
assert_array_almost_equal(diff(cos(x),2),direct_diff(cos(x),2))
|
| 96 |
+
assert_array_almost_equal(diff(cos(x),3),direct_diff(cos(x),3))
|
| 97 |
+
assert_array_almost_equal(diff(cos(x),4),direct_diff(cos(x),4))
|
| 98 |
+
assert_array_almost_equal(diff(cos(2*x)),direct_diff(cos(2*x)))
|
| 99 |
+
assert_array_almost_equal(diff(sin(x*n/8)),direct_diff(sin(x*n/8)))
|
| 100 |
+
assert_array_almost_equal(diff(cos(x*n/8)),direct_diff(cos(x*n/8)))
|
| 101 |
+
for k in range(5):
|
| 102 |
+
assert_array_almost_equal(diff(sin(4*x),k),direct_diff(sin(4*x),k))
|
| 103 |
+
assert_array_almost_equal(diff(cos(4*x),k),direct_diff(cos(4*x),k))
|
| 104 |
+
|
| 105 |
+
def test_period(self):
|
| 106 |
+
for n in [17,64]:
|
| 107 |
+
x = arange(n)/float(n)
|
| 108 |
+
assert_array_almost_equal(diff(sin(2*pi*x),period=1),
|
| 109 |
+
2*pi*cos(2*pi*x))
|
| 110 |
+
assert_array_almost_equal(diff(sin(2*pi*x),3,period=1),
|
| 111 |
+
-(2*pi)**3*cos(2*pi*x))
|
| 112 |
+
|
| 113 |
+
def test_sin(self):
|
| 114 |
+
for n in [32,64,77]:
|
| 115 |
+
x = arange(n)*2*pi/n
|
| 116 |
+
assert_array_almost_equal(diff(sin(x)),cos(x))
|
| 117 |
+
assert_array_almost_equal(diff(cos(x)),-sin(x))
|
| 118 |
+
assert_array_almost_equal(diff(sin(x),2),-sin(x))
|
| 119 |
+
assert_array_almost_equal(diff(sin(x),4),sin(x))
|
| 120 |
+
assert_array_almost_equal(diff(sin(4*x)),4*cos(4*x))
|
| 121 |
+
assert_array_almost_equal(diff(sin(sin(x))),cos(x)*cos(sin(x)))
|
| 122 |
+
|
| 123 |
+
def test_expr(self):
|
| 124 |
+
for n in [64,77,100,128,256,512,1024,2048,4096,8192][:5]:
|
| 125 |
+
x = arange(n)*2*pi/n
|
| 126 |
+
f = sin(x)*cos(4*x)+exp(sin(3*x))
|
| 127 |
+
df = cos(x)*cos(4*x)-4*sin(x)*sin(4*x)+3*cos(3*x)*exp(sin(3*x))
|
| 128 |
+
ddf = -17*sin(x)*cos(4*x)-8*cos(x)*sin(4*x)\
|
| 129 |
+
- 9*sin(3*x)*exp(sin(3*x))+9*cos(3*x)**2*exp(sin(3*x))
|
| 130 |
+
d1 = diff(f)
|
| 131 |
+
assert_array_almost_equal(d1,df)
|
| 132 |
+
assert_array_almost_equal(diff(df),ddf)
|
| 133 |
+
assert_array_almost_equal(diff(f,2),ddf)
|
| 134 |
+
assert_array_almost_equal(diff(ddf,-1),df)
|
| 135 |
+
|
| 136 |
+
def test_expr_large(self):
|
| 137 |
+
for n in [2048,4096]:
|
| 138 |
+
x = arange(n)*2*pi/n
|
| 139 |
+
f = sin(x)*cos(4*x)+exp(sin(3*x))
|
| 140 |
+
df = cos(x)*cos(4*x)-4*sin(x)*sin(4*x)+3*cos(3*x)*exp(sin(3*x))
|
| 141 |
+
ddf = -17*sin(x)*cos(4*x)-8*cos(x)*sin(4*x)\
|
| 142 |
+
- 9*sin(3*x)*exp(sin(3*x))+9*cos(3*x)**2*exp(sin(3*x))
|
| 143 |
+
assert_array_almost_equal(diff(f),df)
|
| 144 |
+
assert_array_almost_equal(diff(df),ddf)
|
| 145 |
+
assert_array_almost_equal(diff(ddf,-1),df)
|
| 146 |
+
assert_array_almost_equal(diff(f,2),ddf)
|
| 147 |
+
|
| 148 |
+
def test_int(self):
|
| 149 |
+
n = 64
|
| 150 |
+
x = arange(n)*2*pi/n
|
| 151 |
+
assert_array_almost_equal(diff(sin(x),-1),-cos(x))
|
| 152 |
+
assert_array_almost_equal(diff(sin(x),-2),-sin(x))
|
| 153 |
+
assert_array_almost_equal(diff(sin(x),-4),sin(x))
|
| 154 |
+
assert_array_almost_equal(diff(2*cos(2*x),-1),sin(2*x))
|
| 155 |
+
|
| 156 |
+
def test_random_even(self):
|
| 157 |
+
rng = np.random.default_rng(1234)
|
| 158 |
+
for k in [0,2,4,6]:
|
| 159 |
+
for n in [60,32,64,56,55]:
|
| 160 |
+
f = rng.random((n,))
|
| 161 |
+
af = sum(f,axis=0)/n
|
| 162 |
+
f = f-af
|
| 163 |
+
# zeroing Nyquist mode:
|
| 164 |
+
f = diff(diff(f,1),-1)
|
| 165 |
+
assert_almost_equal(sum(f,axis=0),0.0)
|
| 166 |
+
assert_array_almost_equal(diff(diff(f,k),-k),f)
|
| 167 |
+
assert_array_almost_equal(diff(diff(f,-k),k),f)
|
| 168 |
+
|
| 169 |
+
def test_random_odd(self):
|
| 170 |
+
rng = np.random.default_rng(1234)
|
| 171 |
+
for k in [0,1,2,3,4,5,6]:
|
| 172 |
+
for n in [33,65,55]:
|
| 173 |
+
f = rng.random((n,))
|
| 174 |
+
af = sum(f,axis=0)/n
|
| 175 |
+
f = f-af
|
| 176 |
+
assert_almost_equal(sum(f,axis=0),0.0)
|
| 177 |
+
assert_array_almost_equal(diff(diff(f,k),-k),f)
|
| 178 |
+
assert_array_almost_equal(diff(diff(f,-k),k),f)
|
| 179 |
+
|
| 180 |
+
def test_zero_nyquist(self):
|
| 181 |
+
rng = np.random.default_rng(1234)
|
| 182 |
+
for k in [0,1,2,3,4,5,6]:
|
| 183 |
+
for n in [32,33,64,56,55]:
|
| 184 |
+
f = rng.random((n,))
|
| 185 |
+
af = sum(f,axis=0)/n
|
| 186 |
+
f = f-af
|
| 187 |
+
# zeroing Nyquist mode:
|
| 188 |
+
f = diff(diff(f,1),-1)
|
| 189 |
+
assert_almost_equal(sum(f,axis=0),0.0)
|
| 190 |
+
assert_array_almost_equal(diff(diff(f,k),-k),f)
|
| 191 |
+
assert_array_almost_equal(diff(diff(f,-k),k),f)
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
class TestTilbert:
|
| 195 |
+
|
| 196 |
+
def test_definition(self):
|
| 197 |
+
for h in [0.1,0.5,1,5.5,10]:
|
| 198 |
+
for n in [16,17,64,127]:
|
| 199 |
+
x = arange(n)*2*pi/n
|
| 200 |
+
y = tilbert(sin(x),h)
|
| 201 |
+
y1 = direct_tilbert(sin(x),h)
|
| 202 |
+
assert_array_almost_equal(y,y1)
|
| 203 |
+
assert_array_almost_equal(tilbert(sin(x),h),
|
| 204 |
+
direct_tilbert(sin(x),h))
|
| 205 |
+
assert_array_almost_equal(tilbert(sin(2*x),h),
|
| 206 |
+
direct_tilbert(sin(2*x),h))
|
| 207 |
+
|
| 208 |
+
def test_random_even(self):
|
| 209 |
+
for h in [0.1,0.5,1,5.5,10]:
|
| 210 |
+
for n in [32,64,56]:
|
| 211 |
+
f = random((n,))
|
| 212 |
+
af = sum(f,axis=0)/n
|
| 213 |
+
f = f-af
|
| 214 |
+
assert_almost_equal(sum(f,axis=0),0.0)
|
| 215 |
+
assert_array_almost_equal(direct_tilbert(direct_itilbert(f,h),h),f)
|
| 216 |
+
|
| 217 |
+
def test_random_odd(self):
|
| 218 |
+
rng = np.random.default_rng(1234)
|
| 219 |
+
for h in [0.1,0.5,1,5.5,10]:
|
| 220 |
+
for n in [33,65,55]:
|
| 221 |
+
f = rng.random((n,))
|
| 222 |
+
af = sum(f,axis=0)/n
|
| 223 |
+
f = f-af
|
| 224 |
+
assert_almost_equal(sum(f,axis=0),0.0)
|
| 225 |
+
assert_array_almost_equal(itilbert(tilbert(f,h),h),f)
|
| 226 |
+
assert_array_almost_equal(tilbert(itilbert(f,h),h),f)
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
class TestITilbert:
|
| 230 |
+
|
| 231 |
+
def test_definition(self):
|
| 232 |
+
for h in [0.1,0.5,1,5.5,10]:
|
| 233 |
+
for n in [16,17,64,127]:
|
| 234 |
+
x = arange(n)*2*pi/n
|
| 235 |
+
y = itilbert(sin(x),h)
|
| 236 |
+
y1 = direct_itilbert(sin(x),h)
|
| 237 |
+
assert_array_almost_equal(y,y1)
|
| 238 |
+
assert_array_almost_equal(itilbert(sin(x),h),
|
| 239 |
+
direct_itilbert(sin(x),h))
|
| 240 |
+
assert_array_almost_equal(itilbert(sin(2*x),h),
|
| 241 |
+
direct_itilbert(sin(2*x),h))
|
| 242 |
+
|
| 243 |
+
|
| 244 |
+
class TestHilbert:
|
| 245 |
+
|
| 246 |
+
def test_definition(self):
|
| 247 |
+
for n in [16,17,64,127]:
|
| 248 |
+
x = arange(n)*2*pi/n
|
| 249 |
+
y = hilbert(sin(x))
|
| 250 |
+
y1 = direct_hilbert(sin(x))
|
| 251 |
+
assert_array_almost_equal(y,y1)
|
| 252 |
+
assert_array_almost_equal(hilbert(sin(2*x)),
|
| 253 |
+
direct_hilbert(sin(2*x)))
|
| 254 |
+
|
| 255 |
+
def test_tilbert_relation(self):
|
| 256 |
+
for n in [16,17,64,127]:
|
| 257 |
+
x = arange(n)*2*pi/n
|
| 258 |
+
f = sin(x)+cos(2*x)*sin(x)
|
| 259 |
+
y = hilbert(f)
|
| 260 |
+
y1 = direct_hilbert(f)
|
| 261 |
+
assert_array_almost_equal(y,y1)
|
| 262 |
+
y2 = tilbert(f,h=10)
|
| 263 |
+
assert_array_almost_equal(y,y2)
|
| 264 |
+
|
| 265 |
+
def test_random_odd(self):
|
| 266 |
+
rng = np.random.default_rng(1234)
|
| 267 |
+
for n in [33,65,55]:
|
| 268 |
+
f = rng.random((n,))
|
| 269 |
+
af = sum(f,axis=0)/n
|
| 270 |
+
f = f-af
|
| 271 |
+
assert_almost_equal(sum(f,axis=0),0.0)
|
| 272 |
+
assert_array_almost_equal(ihilbert(hilbert(f)),f)
|
| 273 |
+
assert_array_almost_equal(hilbert(ihilbert(f)),f)
|
| 274 |
+
|
| 275 |
+
def test_random_even(self):
|
| 276 |
+
rng = np.random.default_rng(1234)
|
| 277 |
+
for n in [32,64,56]:
|
| 278 |
+
f = rng.random((n,))
|
| 279 |
+
af = sum(f,axis=0)/n
|
| 280 |
+
f = f-af
|
| 281 |
+
# zeroing Nyquist mode:
|
| 282 |
+
f = diff(diff(f,1),-1)
|
| 283 |
+
assert_almost_equal(sum(f,axis=0),0.0)
|
| 284 |
+
assert_array_almost_equal(direct_hilbert(direct_ihilbert(f)),f)
|
| 285 |
+
assert_array_almost_equal(hilbert(ihilbert(f)),f)
|
| 286 |
+
|
| 287 |
+
|
| 288 |
+
class TestIHilbert:
|
| 289 |
+
|
| 290 |
+
def test_definition(self):
|
| 291 |
+
for n in [16,17,64,127]:
|
| 292 |
+
x = arange(n)*2*pi/n
|
| 293 |
+
y = ihilbert(sin(x))
|
| 294 |
+
y1 = direct_ihilbert(sin(x))
|
| 295 |
+
assert_array_almost_equal(y,y1)
|
| 296 |
+
assert_array_almost_equal(ihilbert(sin(2*x)),
|
| 297 |
+
direct_ihilbert(sin(2*x)))
|
| 298 |
+
|
| 299 |
+
def test_itilbert_relation(self):
|
| 300 |
+
for n in [16,17,64,127]:
|
| 301 |
+
x = arange(n)*2*pi/n
|
| 302 |
+
f = sin(x)+cos(2*x)*sin(x)
|
| 303 |
+
y = ihilbert(f)
|
| 304 |
+
y1 = direct_ihilbert(f)
|
| 305 |
+
assert_array_almost_equal(y,y1)
|
| 306 |
+
y2 = itilbert(f,h=10)
|
| 307 |
+
assert_array_almost_equal(y,y2)
|
| 308 |
+
|
| 309 |
+
|
| 310 |
+
class TestShift:
|
| 311 |
+
|
| 312 |
+
def test_definition(self):
|
| 313 |
+
for n in [18,17,64,127,32,2048,256]:
|
| 314 |
+
x = arange(n)*2*pi/n
|
| 315 |
+
for a in [0.1,3]:
|
| 316 |
+
assert_array_almost_equal(shift(sin(x),a),direct_shift(sin(x),a))
|
| 317 |
+
assert_array_almost_equal(shift(sin(x),a),sin(x+a))
|
| 318 |
+
assert_array_almost_equal(shift(cos(x),a),cos(x+a))
|
| 319 |
+
assert_array_almost_equal(shift(cos(2*x)+sin(x),a),
|
| 320 |
+
cos(2*(x+a))+sin(x+a))
|
| 321 |
+
assert_array_almost_equal(shift(exp(sin(x)),a),exp(sin(x+a)))
|
| 322 |
+
assert_array_almost_equal(shift(sin(x),2*pi),sin(x))
|
| 323 |
+
assert_array_almost_equal(shift(sin(x),pi),-sin(x))
|
| 324 |
+
assert_array_almost_equal(shift(sin(x),pi/2),cos(x))
|
| 325 |
+
|
| 326 |
+
|
| 327 |
+
class TestOverwrite:
|
| 328 |
+
"""Check input overwrite behavior """
|
| 329 |
+
|
| 330 |
+
real_dtypes = (np.float32, np.float64)
|
| 331 |
+
dtypes = real_dtypes + (np.complex64, np.complex128)
|
| 332 |
+
|
| 333 |
+
def _check(self, x, routine, *args, **kwargs):
|
| 334 |
+
x2 = x.copy()
|
| 335 |
+
routine(x2, *args, **kwargs)
|
| 336 |
+
sig = routine.__name__
|
| 337 |
+
if args:
|
| 338 |
+
sig += repr(args)
|
| 339 |
+
if kwargs:
|
| 340 |
+
sig += repr(kwargs)
|
| 341 |
+
assert_equal(x2, x, err_msg=f"spurious overwrite in {sig}")
|
| 342 |
+
|
| 343 |
+
def _check_1d(self, routine, dtype, shape, *args, **kwargs):
|
| 344 |
+
# rng = np.random.default_rng(1234)
|
| 345 |
+
rng = np.random.RandomState(1234)
|
| 346 |
+
# np.random.seed(1234)
|
| 347 |
+
if np.issubdtype(dtype, np.complexfloating):
|
| 348 |
+
data = rng.randn(*shape) + 1j*rng.randn(*shape)
|
| 349 |
+
else:
|
| 350 |
+
data = rng.randn(*shape)
|
| 351 |
+
data = data.astype(dtype)
|
| 352 |
+
self._check(data, routine, *args, **kwargs)
|
| 353 |
+
|
| 354 |
+
def test_diff(self):
|
| 355 |
+
for dtype in self.dtypes:
|
| 356 |
+
self._check_1d(diff, dtype, (16,))
|
| 357 |
+
|
| 358 |
+
def test_tilbert(self):
|
| 359 |
+
for dtype in self.dtypes:
|
| 360 |
+
self._check_1d(tilbert, dtype, (16,), 1.6)
|
| 361 |
+
|
| 362 |
+
def test_itilbert(self):
|
| 363 |
+
for dtype in self.dtypes:
|
| 364 |
+
self._check_1d(itilbert, dtype, (16,), 1.6)
|
| 365 |
+
|
| 366 |
+
def test_hilbert(self):
|
| 367 |
+
for dtype in self.dtypes:
|
| 368 |
+
self._check_1d(hilbert, dtype, (16,))
|
| 369 |
+
|
| 370 |
+
def test_cs_diff(self):
|
| 371 |
+
for dtype in self.dtypes:
|
| 372 |
+
self._check_1d(cs_diff, dtype, (16,), 1.0, 4.0)
|
| 373 |
+
|
| 374 |
+
def test_sc_diff(self):
|
| 375 |
+
for dtype in self.dtypes:
|
| 376 |
+
self._check_1d(sc_diff, dtype, (16,), 1.0, 4.0)
|
| 377 |
+
|
| 378 |
+
def test_ss_diff(self):
|
| 379 |
+
for dtype in self.dtypes:
|
| 380 |
+
self._check_1d(ss_diff, dtype, (16,), 1.0, 4.0)
|
| 381 |
+
|
| 382 |
+
def test_cc_diff(self):
|
| 383 |
+
for dtype in self.dtypes:
|
| 384 |
+
self._check_1d(cc_diff, dtype, (16,), 1.0, 4.0)
|
| 385 |
+
|
| 386 |
+
def test_shift(self):
|
| 387 |
+
for dtype in self.dtypes:
|
| 388 |
+
self._check_1d(shift, dtype, (16,), 1.0)
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/fftpack/tests/test_real_transforms.py
ADDED
|
@@ -0,0 +1,836 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from os.path import join, dirname
|
| 2 |
+
import threading
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
from numpy.testing import assert_array_almost_equal, assert_equal
|
| 6 |
+
import pytest
|
| 7 |
+
from pytest import raises as assert_raises
|
| 8 |
+
|
| 9 |
+
from scipy.fftpack._realtransforms import (
|
| 10 |
+
dct, idct, dst, idst, dctn, idctn, dstn, idstn)
|
| 11 |
+
|
| 12 |
+
# Matlab reference data
|
| 13 |
+
MDATA = np.load(join(dirname(__file__), 'test.npz'))
|
| 14 |
+
X = [MDATA[f'x{i}'] for i in range(8)]
|
| 15 |
+
Y = [MDATA[f'y{i}'] for i in range(8)]
|
| 16 |
+
|
| 17 |
+
# FFTW reference data: the data are organized as follows:
|
| 18 |
+
# * SIZES is an array containing all available sizes
|
| 19 |
+
# * for every type (1, 2, 3, 4) and every size, the array dct_type_size
|
| 20 |
+
# contains the output of the DCT applied to the input np.linspace(0, size-1,
|
| 21 |
+
# size)
|
| 22 |
+
FFTWDATA_DOUBLE = np.load(join(dirname(__file__), 'fftw_double_ref.npz'))
|
| 23 |
+
FFTWDATA_SINGLE = np.load(join(dirname(__file__), 'fftw_single_ref.npz'))
|
| 24 |
+
FFTWDATA_SIZES = FFTWDATA_DOUBLE['sizes']
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def fftw_dct_ref(type, size, dt):
|
| 28 |
+
x = np.linspace(0, size-1, size).astype(dt)
|
| 29 |
+
dt = np.result_type(np.float32, dt)
|
| 30 |
+
if dt == np.float64:
|
| 31 |
+
data = FFTWDATA_DOUBLE
|
| 32 |
+
elif dt == np.float32:
|
| 33 |
+
data = FFTWDATA_SINGLE
|
| 34 |
+
else:
|
| 35 |
+
raise ValueError()
|
| 36 |
+
y = (data[f'dct_{type}_{size}']).astype(dt)
|
| 37 |
+
return x, y, dt
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def fftw_dst_ref(type, size, dt):
|
| 41 |
+
x = np.linspace(0, size-1, size).astype(dt)
|
| 42 |
+
dt = np.result_type(np.float32, dt)
|
| 43 |
+
if dt == np.float64:
|
| 44 |
+
data = FFTWDATA_DOUBLE
|
| 45 |
+
elif dt == np.float32:
|
| 46 |
+
data = FFTWDATA_SINGLE
|
| 47 |
+
else:
|
| 48 |
+
raise ValueError()
|
| 49 |
+
y = (data[f'dst_{type}_{size}']).astype(dt)
|
| 50 |
+
return x, y, dt
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def dct_2d_ref(x, **kwargs):
|
| 54 |
+
"""Calculate reference values for testing dct2."""
|
| 55 |
+
x = np.array(x, copy=True)
|
| 56 |
+
for row in range(x.shape[0]):
|
| 57 |
+
x[row, :] = dct(x[row, :], **kwargs)
|
| 58 |
+
for col in range(x.shape[1]):
|
| 59 |
+
x[:, col] = dct(x[:, col], **kwargs)
|
| 60 |
+
return x
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def idct_2d_ref(x, **kwargs):
|
| 64 |
+
"""Calculate reference values for testing idct2."""
|
| 65 |
+
x = np.array(x, copy=True)
|
| 66 |
+
for row in range(x.shape[0]):
|
| 67 |
+
x[row, :] = idct(x[row, :], **kwargs)
|
| 68 |
+
for col in range(x.shape[1]):
|
| 69 |
+
x[:, col] = idct(x[:, col], **kwargs)
|
| 70 |
+
return x
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def dst_2d_ref(x, **kwargs):
|
| 74 |
+
"""Calculate reference values for testing dst2."""
|
| 75 |
+
x = np.array(x, copy=True)
|
| 76 |
+
for row in range(x.shape[0]):
|
| 77 |
+
x[row, :] = dst(x[row, :], **kwargs)
|
| 78 |
+
for col in range(x.shape[1]):
|
| 79 |
+
x[:, col] = dst(x[:, col], **kwargs)
|
| 80 |
+
return x
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def idst_2d_ref(x, **kwargs):
|
| 84 |
+
"""Calculate reference values for testing idst2."""
|
| 85 |
+
x = np.array(x, copy=True)
|
| 86 |
+
for row in range(x.shape[0]):
|
| 87 |
+
x[row, :] = idst(x[row, :], **kwargs)
|
| 88 |
+
for col in range(x.shape[1]):
|
| 89 |
+
x[:, col] = idst(x[:, col], **kwargs)
|
| 90 |
+
return x
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
def naive_dct1(x, norm=None):
|
| 94 |
+
"""Calculate textbook definition version of DCT-I."""
|
| 95 |
+
x = np.array(x, copy=True)
|
| 96 |
+
N = len(x)
|
| 97 |
+
M = N-1
|
| 98 |
+
y = np.zeros(N)
|
| 99 |
+
m0, m = 1, 2
|
| 100 |
+
if norm == 'ortho':
|
| 101 |
+
m0 = np.sqrt(1.0/M)
|
| 102 |
+
m = np.sqrt(2.0/M)
|
| 103 |
+
for k in range(N):
|
| 104 |
+
for n in range(1, N-1):
|
| 105 |
+
y[k] += m*x[n]*np.cos(np.pi*n*k/M)
|
| 106 |
+
y[k] += m0 * x[0]
|
| 107 |
+
y[k] += m0 * x[N-1] * (1 if k % 2 == 0 else -1)
|
| 108 |
+
if norm == 'ortho':
|
| 109 |
+
y[0] *= 1/np.sqrt(2)
|
| 110 |
+
y[N-1] *= 1/np.sqrt(2)
|
| 111 |
+
return y
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
def naive_dst1(x, norm=None):
|
| 115 |
+
"""Calculate textbook definition version of DST-I."""
|
| 116 |
+
x = np.array(x, copy=True)
|
| 117 |
+
N = len(x)
|
| 118 |
+
M = N+1
|
| 119 |
+
y = np.zeros(N)
|
| 120 |
+
for k in range(N):
|
| 121 |
+
for n in range(N):
|
| 122 |
+
y[k] += 2*x[n]*np.sin(np.pi*(n+1.0)*(k+1.0)/M)
|
| 123 |
+
if norm == 'ortho':
|
| 124 |
+
y *= np.sqrt(0.5/M)
|
| 125 |
+
return y
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
def naive_dct4(x, norm=None):
|
| 129 |
+
"""Calculate textbook definition version of DCT-IV."""
|
| 130 |
+
x = np.array(x, copy=True)
|
| 131 |
+
N = len(x)
|
| 132 |
+
y = np.zeros(N)
|
| 133 |
+
for k in range(N):
|
| 134 |
+
for n in range(N):
|
| 135 |
+
y[k] += x[n]*np.cos(np.pi*(n+0.5)*(k+0.5)/(N))
|
| 136 |
+
if norm == 'ortho':
|
| 137 |
+
y *= np.sqrt(2.0/N)
|
| 138 |
+
else:
|
| 139 |
+
y *= 2
|
| 140 |
+
return y
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
def naive_dst4(x, norm=None):
|
| 144 |
+
"""Calculate textbook definition version of DST-IV."""
|
| 145 |
+
x = np.array(x, copy=True)
|
| 146 |
+
N = len(x)
|
| 147 |
+
y = np.zeros(N)
|
| 148 |
+
for k in range(N):
|
| 149 |
+
for n in range(N):
|
| 150 |
+
y[k] += x[n]*np.sin(np.pi*(n+0.5)*(k+0.5)/(N))
|
| 151 |
+
if norm == 'ortho':
|
| 152 |
+
y *= np.sqrt(2.0/N)
|
| 153 |
+
else:
|
| 154 |
+
y *= 2
|
| 155 |
+
return y
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
class TestComplex:
|
| 159 |
+
def test_dct_complex64(self):
|
| 160 |
+
y = dct(1j*np.arange(5, dtype=np.complex64))
|
| 161 |
+
x = 1j*dct(np.arange(5))
|
| 162 |
+
assert_array_almost_equal(x, y)
|
| 163 |
+
|
| 164 |
+
def test_dct_complex(self):
|
| 165 |
+
y = dct(np.arange(5)*1j)
|
| 166 |
+
x = 1j*dct(np.arange(5))
|
| 167 |
+
assert_array_almost_equal(x, y)
|
| 168 |
+
|
| 169 |
+
def test_idct_complex(self):
|
| 170 |
+
y = idct(np.arange(5)*1j)
|
| 171 |
+
x = 1j*idct(np.arange(5))
|
| 172 |
+
assert_array_almost_equal(x, y)
|
| 173 |
+
|
| 174 |
+
def test_dst_complex64(self):
|
| 175 |
+
y = dst(np.arange(5, dtype=np.complex64)*1j)
|
| 176 |
+
x = 1j*dst(np.arange(5))
|
| 177 |
+
assert_array_almost_equal(x, y)
|
| 178 |
+
|
| 179 |
+
def test_dst_complex(self):
|
| 180 |
+
y = dst(np.arange(5)*1j)
|
| 181 |
+
x = 1j*dst(np.arange(5))
|
| 182 |
+
assert_array_almost_equal(x, y)
|
| 183 |
+
|
| 184 |
+
def test_idst_complex(self):
|
| 185 |
+
y = idst(np.arange(5)*1j)
|
| 186 |
+
x = 1j*idst(np.arange(5))
|
| 187 |
+
assert_array_almost_equal(x, y)
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
class _TestDCTBase:
|
| 191 |
+
def setup_method(self):
|
| 192 |
+
self.rdt = None
|
| 193 |
+
self.dec = 14
|
| 194 |
+
self.type = None
|
| 195 |
+
|
| 196 |
+
@pytest.fixture
|
| 197 |
+
def dct_lock(self):
|
| 198 |
+
return threading.Lock()
|
| 199 |
+
|
| 200 |
+
def test_definition(self, dct_lock):
|
| 201 |
+
for i in FFTWDATA_SIZES:
|
| 202 |
+
with dct_lock:
|
| 203 |
+
x, yr, dt = fftw_dct_ref(self.type, i, self.rdt)
|
| 204 |
+
y = dct(x, type=self.type)
|
| 205 |
+
assert_equal(y.dtype, dt)
|
| 206 |
+
# XXX: we divide by np.max(y) because the tests fail otherwise. We
|
| 207 |
+
# should really use something like assert_array_approx_equal. The
|
| 208 |
+
# difference is due to fftw using a better algorithm w.r.t error
|
| 209 |
+
# propagation compared to the ones from fftpack.
|
| 210 |
+
assert_array_almost_equal(y / np.max(y), yr / np.max(y), decimal=self.dec,
|
| 211 |
+
err_msg=f"Size {i} failed")
|
| 212 |
+
|
| 213 |
+
def test_axis(self):
|
| 214 |
+
nt = 2
|
| 215 |
+
rng = np.random.RandomState(1234)
|
| 216 |
+
for i in [7, 8, 9, 16, 32, 64]:
|
| 217 |
+
x = rng.randn(nt, i)
|
| 218 |
+
y = dct(x, type=self.type)
|
| 219 |
+
for j in range(nt):
|
| 220 |
+
assert_array_almost_equal(y[j], dct(x[j], type=self.type),
|
| 221 |
+
decimal=self.dec)
|
| 222 |
+
|
| 223 |
+
x = x.T
|
| 224 |
+
y = dct(x, axis=0, type=self.type)
|
| 225 |
+
for j in range(nt):
|
| 226 |
+
assert_array_almost_equal(y[:,j], dct(x[:,j], type=self.type),
|
| 227 |
+
decimal=self.dec)
|
| 228 |
+
|
| 229 |
+
|
| 230 |
+
class _TestDCTIBase(_TestDCTBase):
|
| 231 |
+
def test_definition_ortho(self):
|
| 232 |
+
# Test orthornomal mode.
|
| 233 |
+
dt = np.result_type(np.float32, self.rdt)
|
| 234 |
+
for xr in X:
|
| 235 |
+
x = np.array(xr, dtype=self.rdt)
|
| 236 |
+
y = dct(x, norm='ortho', type=1)
|
| 237 |
+
y2 = naive_dct1(x, norm='ortho')
|
| 238 |
+
assert_equal(y.dtype, dt)
|
| 239 |
+
assert_array_almost_equal(y / np.max(y), y2 / np.max(y), decimal=self.dec)
|
| 240 |
+
|
| 241 |
+
class _TestDCTIIBase(_TestDCTBase):
|
| 242 |
+
def test_definition_matlab(self):
|
| 243 |
+
# Test correspondence with MATLAB (orthornomal mode).
|
| 244 |
+
dt = np.result_type(np.float32, self.rdt)
|
| 245 |
+
for xr, yr in zip(X, Y):
|
| 246 |
+
x = np.array(xr, dtype=dt)
|
| 247 |
+
y = dct(x, norm="ortho", type=2)
|
| 248 |
+
assert_equal(y.dtype, dt)
|
| 249 |
+
assert_array_almost_equal(y, yr, decimal=self.dec)
|
| 250 |
+
|
| 251 |
+
|
| 252 |
+
class _TestDCTIIIBase(_TestDCTBase):
|
| 253 |
+
def test_definition_ortho(self):
|
| 254 |
+
# Test orthornomal mode.
|
| 255 |
+
dt = np.result_type(np.float32, self.rdt)
|
| 256 |
+
for xr in X:
|
| 257 |
+
x = np.array(xr, dtype=self.rdt)
|
| 258 |
+
y = dct(x, norm='ortho', type=2)
|
| 259 |
+
xi = dct(y, norm="ortho", type=3)
|
| 260 |
+
assert_equal(xi.dtype, dt)
|
| 261 |
+
assert_array_almost_equal(xi, x, decimal=self.dec)
|
| 262 |
+
|
| 263 |
+
class _TestDCTIVBase(_TestDCTBase):
|
| 264 |
+
def test_definition_ortho(self):
|
| 265 |
+
# Test orthornomal mode.
|
| 266 |
+
dt = np.result_type(np.float32, self.rdt)
|
| 267 |
+
for xr in X:
|
| 268 |
+
x = np.array(xr, dtype=self.rdt)
|
| 269 |
+
y = dct(x, norm='ortho', type=4)
|
| 270 |
+
y2 = naive_dct4(x, norm='ortho')
|
| 271 |
+
assert_equal(y.dtype, dt)
|
| 272 |
+
assert_array_almost_equal(y / np.max(y), y2 / np.max(y), decimal=self.dec)
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
class TestDCTIDouble(_TestDCTIBase):
|
| 276 |
+
def setup_method(self):
|
| 277 |
+
self.rdt = np.float64
|
| 278 |
+
self.dec = 10
|
| 279 |
+
self.type = 1
|
| 280 |
+
|
| 281 |
+
|
| 282 |
+
class TestDCTIFloat(_TestDCTIBase):
|
| 283 |
+
def setup_method(self):
|
| 284 |
+
self.rdt = np.float32
|
| 285 |
+
self.dec = 4
|
| 286 |
+
self.type = 1
|
| 287 |
+
|
| 288 |
+
|
| 289 |
+
class TestDCTIInt(_TestDCTIBase):
|
| 290 |
+
def setup_method(self):
|
| 291 |
+
self.rdt = int
|
| 292 |
+
self.dec = 5
|
| 293 |
+
self.type = 1
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
class TestDCTIIDouble(_TestDCTIIBase):
|
| 297 |
+
def setup_method(self):
|
| 298 |
+
self.rdt = np.float64
|
| 299 |
+
self.dec = 10
|
| 300 |
+
self.type = 2
|
| 301 |
+
|
| 302 |
+
|
| 303 |
+
class TestDCTIIFloat(_TestDCTIIBase):
|
| 304 |
+
def setup_method(self):
|
| 305 |
+
self.rdt = np.float32
|
| 306 |
+
self.dec = 5
|
| 307 |
+
self.type = 2
|
| 308 |
+
|
| 309 |
+
|
| 310 |
+
class TestDCTIIInt(_TestDCTIIBase):
|
| 311 |
+
def setup_method(self):
|
| 312 |
+
self.rdt = int
|
| 313 |
+
self.dec = 5
|
| 314 |
+
self.type = 2
|
| 315 |
+
|
| 316 |
+
|
| 317 |
+
class TestDCTIIIDouble(_TestDCTIIIBase):
|
| 318 |
+
def setup_method(self):
|
| 319 |
+
self.rdt = np.float64
|
| 320 |
+
self.dec = 14
|
| 321 |
+
self.type = 3
|
| 322 |
+
|
| 323 |
+
|
| 324 |
+
class TestDCTIIIFloat(_TestDCTIIIBase):
|
| 325 |
+
def setup_method(self):
|
| 326 |
+
self.rdt = np.float32
|
| 327 |
+
self.dec = 5
|
| 328 |
+
self.type = 3
|
| 329 |
+
|
| 330 |
+
|
| 331 |
+
class TestDCTIIIInt(_TestDCTIIIBase):
|
| 332 |
+
def setup_method(self):
|
| 333 |
+
self.rdt = int
|
| 334 |
+
self.dec = 5
|
| 335 |
+
self.type = 3
|
| 336 |
+
|
| 337 |
+
|
| 338 |
+
class TestDCTIVDouble(_TestDCTIVBase):
|
| 339 |
+
def setup_method(self):
|
| 340 |
+
self.rdt = np.float64
|
| 341 |
+
self.dec = 12
|
| 342 |
+
self.type = 3
|
| 343 |
+
|
| 344 |
+
|
| 345 |
+
class TestDCTIVFloat(_TestDCTIVBase):
|
| 346 |
+
def setup_method(self):
|
| 347 |
+
self.rdt = np.float32
|
| 348 |
+
self.dec = 5
|
| 349 |
+
self.type = 3
|
| 350 |
+
|
| 351 |
+
|
| 352 |
+
class TestDCTIVInt(_TestDCTIVBase):
|
| 353 |
+
def setup_method(self):
|
| 354 |
+
self.rdt = int
|
| 355 |
+
self.dec = 5
|
| 356 |
+
self.type = 3
|
| 357 |
+
|
| 358 |
+
|
| 359 |
+
class _TestIDCTBase:
|
| 360 |
+
def setup_method(self):
|
| 361 |
+
self.rdt = None
|
| 362 |
+
self.dec = 14
|
| 363 |
+
self.type = None
|
| 364 |
+
|
| 365 |
+
@pytest.fixture
|
| 366 |
+
def idct_lock(self):
|
| 367 |
+
return threading.Lock()
|
| 368 |
+
|
| 369 |
+
def test_definition(self, idct_lock):
|
| 370 |
+
for i in FFTWDATA_SIZES:
|
| 371 |
+
with idct_lock:
|
| 372 |
+
xr, yr, dt = fftw_dct_ref(self.type, i, self.rdt)
|
| 373 |
+
x = idct(yr, type=self.type)
|
| 374 |
+
if self.type == 1:
|
| 375 |
+
x /= 2 * (i-1)
|
| 376 |
+
else:
|
| 377 |
+
x /= 2 * i
|
| 378 |
+
assert_equal(x.dtype, dt)
|
| 379 |
+
# XXX: we divide by np.max(y) because the tests fail otherwise. We
|
| 380 |
+
# should really use something like assert_array_approx_equal. The
|
| 381 |
+
# difference is due to fftw using a better algorithm w.r.t error
|
| 382 |
+
# propagation compared to the ones from fftpack.
|
| 383 |
+
assert_array_almost_equal(x / np.max(x), xr / np.max(x), decimal=self.dec,
|
| 384 |
+
err_msg=f"Size {i} failed")
|
| 385 |
+
|
| 386 |
+
class TestIDCTIDouble(_TestIDCTBase):
|
| 387 |
+
def setup_method(self):
|
| 388 |
+
self.rdt = np.float64
|
| 389 |
+
self.dec = 10
|
| 390 |
+
self.type = 1
|
| 391 |
+
|
| 392 |
+
|
| 393 |
+
class TestIDCTIFloat(_TestIDCTBase):
|
| 394 |
+
def setup_method(self):
|
| 395 |
+
self.rdt = np.float32
|
| 396 |
+
self.dec = 4
|
| 397 |
+
self.type = 1
|
| 398 |
+
|
| 399 |
+
|
| 400 |
+
class TestIDCTIInt(_TestIDCTBase):
|
| 401 |
+
def setup_method(self):
|
| 402 |
+
self.rdt = int
|
| 403 |
+
self.dec = 4
|
| 404 |
+
self.type = 1
|
| 405 |
+
|
| 406 |
+
|
| 407 |
+
class TestIDCTIIDouble(_TestIDCTBase):
|
| 408 |
+
def setup_method(self):
|
| 409 |
+
self.rdt = np.float64
|
| 410 |
+
self.dec = 10
|
| 411 |
+
self.type = 2
|
| 412 |
+
|
| 413 |
+
|
| 414 |
+
class TestIDCTIIFloat(_TestIDCTBase):
|
| 415 |
+
def setup_method(self):
|
| 416 |
+
self.rdt = np.float32
|
| 417 |
+
self.dec = 5
|
| 418 |
+
self.type = 2
|
| 419 |
+
|
| 420 |
+
|
| 421 |
+
class TestIDCTIIInt(_TestIDCTBase):
|
| 422 |
+
def setup_method(self):
|
| 423 |
+
self.rdt = int
|
| 424 |
+
self.dec = 5
|
| 425 |
+
self.type = 2
|
| 426 |
+
|
| 427 |
+
|
| 428 |
+
class TestIDCTIIIDouble(_TestIDCTBase):
|
| 429 |
+
def setup_method(self):
|
| 430 |
+
self.rdt = np.float64
|
| 431 |
+
self.dec = 14
|
| 432 |
+
self.type = 3
|
| 433 |
+
|
| 434 |
+
|
| 435 |
+
class TestIDCTIIIFloat(_TestIDCTBase):
|
| 436 |
+
def setup_method(self):
|
| 437 |
+
self.rdt = np.float32
|
| 438 |
+
self.dec = 5
|
| 439 |
+
self.type = 3
|
| 440 |
+
|
| 441 |
+
|
| 442 |
+
class TestIDCTIIIInt(_TestIDCTBase):
|
| 443 |
+
def setup_method(self):
|
| 444 |
+
self.rdt = int
|
| 445 |
+
self.dec = 5
|
| 446 |
+
self.type = 3
|
| 447 |
+
|
| 448 |
+
class TestIDCTIVDouble(_TestIDCTBase):
|
| 449 |
+
def setup_method(self):
|
| 450 |
+
self.rdt = np.float64
|
| 451 |
+
self.dec = 12
|
| 452 |
+
self.type = 4
|
| 453 |
+
|
| 454 |
+
|
| 455 |
+
class TestIDCTIVFloat(_TestIDCTBase):
|
| 456 |
+
def setup_method(self):
|
| 457 |
+
self.rdt = np.float32
|
| 458 |
+
self.dec = 5
|
| 459 |
+
self.type = 4
|
| 460 |
+
|
| 461 |
+
|
| 462 |
+
class TestIDCTIVInt(_TestIDCTBase):
|
| 463 |
+
def setup_method(self):
|
| 464 |
+
self.rdt = int
|
| 465 |
+
self.dec = 5
|
| 466 |
+
self.type = 4
|
| 467 |
+
|
| 468 |
+
class _TestDSTBase:
|
| 469 |
+
def setup_method(self):
|
| 470 |
+
self.rdt = None # dtype
|
| 471 |
+
self.dec = None # number of decimals to match
|
| 472 |
+
self.type = None # dst type
|
| 473 |
+
|
| 474 |
+
@pytest.fixture
|
| 475 |
+
def dst_lock(self):
|
| 476 |
+
return threading.Lock()
|
| 477 |
+
|
| 478 |
+
def test_definition(self, dst_lock):
|
| 479 |
+
for i in FFTWDATA_SIZES:
|
| 480 |
+
with dst_lock:
|
| 481 |
+
xr, yr, dt = fftw_dst_ref(self.type, i, self.rdt)
|
| 482 |
+
y = dst(xr, type=self.type)
|
| 483 |
+
assert_equal(y.dtype, dt)
|
| 484 |
+
# XXX: we divide by np.max(y) because the tests fail otherwise. We
|
| 485 |
+
# should really use something like assert_array_approx_equal. The
|
| 486 |
+
# difference is due to fftw using a better algorithm w.r.t error
|
| 487 |
+
# propagation compared to the ones from fftpack.
|
| 488 |
+
assert_array_almost_equal(y / np.max(y), yr / np.max(y), decimal=self.dec,
|
| 489 |
+
err_msg=f"Size {i} failed")
|
| 490 |
+
|
| 491 |
+
|
| 492 |
+
class _TestDSTIBase(_TestDSTBase):
|
| 493 |
+
def test_definition_ortho(self):
|
| 494 |
+
# Test orthornomal mode.
|
| 495 |
+
dt = np.result_type(np.float32, self.rdt)
|
| 496 |
+
for xr in X:
|
| 497 |
+
x = np.array(xr, dtype=self.rdt)
|
| 498 |
+
y = dst(x, norm='ortho', type=1)
|
| 499 |
+
y2 = naive_dst1(x, norm='ortho')
|
| 500 |
+
assert_equal(y.dtype, dt)
|
| 501 |
+
assert_array_almost_equal(y / np.max(y), y2 / np.max(y), decimal=self.dec)
|
| 502 |
+
|
| 503 |
+
class _TestDSTIVBase(_TestDSTBase):
|
| 504 |
+
def test_definition_ortho(self):
|
| 505 |
+
# Test orthornomal mode.
|
| 506 |
+
dt = np.result_type(np.float32, self.rdt)
|
| 507 |
+
for xr in X:
|
| 508 |
+
x = np.array(xr, dtype=self.rdt)
|
| 509 |
+
y = dst(x, norm='ortho', type=4)
|
| 510 |
+
y2 = naive_dst4(x, norm='ortho')
|
| 511 |
+
assert_equal(y.dtype, dt)
|
| 512 |
+
assert_array_almost_equal(y, y2, decimal=self.dec)
|
| 513 |
+
|
| 514 |
+
class TestDSTIDouble(_TestDSTIBase):
|
| 515 |
+
def setup_method(self):
|
| 516 |
+
self.rdt = np.float64
|
| 517 |
+
self.dec = 12
|
| 518 |
+
self.type = 1
|
| 519 |
+
|
| 520 |
+
|
| 521 |
+
class TestDSTIFloat(_TestDSTIBase):
|
| 522 |
+
def setup_method(self):
|
| 523 |
+
self.rdt = np.float32
|
| 524 |
+
self.dec = 4
|
| 525 |
+
self.type = 1
|
| 526 |
+
|
| 527 |
+
|
| 528 |
+
class TestDSTIInt(_TestDSTIBase):
|
| 529 |
+
def setup_method(self):
|
| 530 |
+
self.rdt = int
|
| 531 |
+
self.dec = 5
|
| 532 |
+
self.type = 1
|
| 533 |
+
|
| 534 |
+
|
| 535 |
+
class TestDSTIIDouble(_TestDSTBase):
|
| 536 |
+
def setup_method(self):
|
| 537 |
+
self.rdt = np.float64
|
| 538 |
+
self.dec = 14
|
| 539 |
+
self.type = 2
|
| 540 |
+
|
| 541 |
+
|
| 542 |
+
class TestDSTIIFloat(_TestDSTBase):
|
| 543 |
+
def setup_method(self):
|
| 544 |
+
self.rdt = np.float32
|
| 545 |
+
self.dec = 6
|
| 546 |
+
self.type = 2
|
| 547 |
+
|
| 548 |
+
|
| 549 |
+
class TestDSTIIInt(_TestDSTBase):
|
| 550 |
+
def setup_method(self):
|
| 551 |
+
self.rdt = int
|
| 552 |
+
self.dec = 6
|
| 553 |
+
self.type = 2
|
| 554 |
+
|
| 555 |
+
|
| 556 |
+
class TestDSTIIIDouble(_TestDSTBase):
|
| 557 |
+
def setup_method(self):
|
| 558 |
+
self.rdt = np.float64
|
| 559 |
+
self.dec = 14
|
| 560 |
+
self.type = 3
|
| 561 |
+
|
| 562 |
+
|
| 563 |
+
class TestDSTIIIFloat(_TestDSTBase):
|
| 564 |
+
def setup_method(self):
|
| 565 |
+
self.rdt = np.float32
|
| 566 |
+
self.dec = 7
|
| 567 |
+
self.type = 3
|
| 568 |
+
|
| 569 |
+
|
| 570 |
+
class TestDSTIIIInt(_TestDSTBase):
|
| 571 |
+
def setup_method(self):
|
| 572 |
+
self.rdt = int
|
| 573 |
+
self.dec = 7
|
| 574 |
+
self.type = 3
|
| 575 |
+
|
| 576 |
+
|
| 577 |
+
class TestDSTIVDouble(_TestDSTIVBase):
|
| 578 |
+
def setup_method(self):
|
| 579 |
+
self.rdt = np.float64
|
| 580 |
+
self.dec = 12
|
| 581 |
+
self.type = 4
|
| 582 |
+
|
| 583 |
+
|
| 584 |
+
class TestDSTIVFloat(_TestDSTIVBase):
|
| 585 |
+
def setup_method(self):
|
| 586 |
+
self.rdt = np.float32
|
| 587 |
+
self.dec = 4
|
| 588 |
+
self.type = 4
|
| 589 |
+
|
| 590 |
+
|
| 591 |
+
class TestDSTIVInt(_TestDSTIVBase):
|
| 592 |
+
def setup_method(self):
|
| 593 |
+
self.rdt = int
|
| 594 |
+
self.dec = 5
|
| 595 |
+
self.type = 4
|
| 596 |
+
|
| 597 |
+
|
| 598 |
+
class _TestIDSTBase:
|
| 599 |
+
def setup_method(self):
|
| 600 |
+
self.rdt = None
|
| 601 |
+
self.dec = None
|
| 602 |
+
self.type = None
|
| 603 |
+
|
| 604 |
+
@pytest.fixture
|
| 605 |
+
def idst_lock(self):
|
| 606 |
+
return threading.Lock()
|
| 607 |
+
|
| 608 |
+
def test_definition(self, idst_lock):
|
| 609 |
+
for i in FFTWDATA_SIZES:
|
| 610 |
+
with idst_lock:
|
| 611 |
+
xr, yr, dt = fftw_dst_ref(self.type, i, self.rdt)
|
| 612 |
+
x = idst(yr, type=self.type)
|
| 613 |
+
if self.type == 1:
|
| 614 |
+
x /= 2 * (i+1)
|
| 615 |
+
else:
|
| 616 |
+
x /= 2 * i
|
| 617 |
+
assert_equal(x.dtype, dt)
|
| 618 |
+
# XXX: we divide by np.max(x) because the tests fail otherwise. We
|
| 619 |
+
# should really use something like assert_array_approx_equal. The
|
| 620 |
+
# difference is due to fftw using a better algorithm w.r.t error
|
| 621 |
+
# propagation compared to the ones from fftpack.
|
| 622 |
+
assert_array_almost_equal(x / np.max(x), xr / np.max(x), decimal=self.dec,
|
| 623 |
+
err_msg=f"Size {i} failed")
|
| 624 |
+
|
| 625 |
+
|
| 626 |
+
class TestIDSTIDouble(_TestIDSTBase):
|
| 627 |
+
def setup_method(self):
|
| 628 |
+
self.rdt = np.float64
|
| 629 |
+
self.dec = 12
|
| 630 |
+
self.type = 1
|
| 631 |
+
|
| 632 |
+
|
| 633 |
+
class TestIDSTIFloat(_TestIDSTBase):
|
| 634 |
+
def setup_method(self):
|
| 635 |
+
self.rdt = np.float32
|
| 636 |
+
self.dec = 4
|
| 637 |
+
self.type = 1
|
| 638 |
+
|
| 639 |
+
|
| 640 |
+
class TestIDSTIInt(_TestIDSTBase):
|
| 641 |
+
def setup_method(self):
|
| 642 |
+
self.rdt = int
|
| 643 |
+
self.dec = 4
|
| 644 |
+
self.type = 1
|
| 645 |
+
|
| 646 |
+
|
| 647 |
+
class TestIDSTIIDouble(_TestIDSTBase):
|
| 648 |
+
def setup_method(self):
|
| 649 |
+
self.rdt = np.float64
|
| 650 |
+
self.dec = 14
|
| 651 |
+
self.type = 2
|
| 652 |
+
|
| 653 |
+
|
| 654 |
+
class TestIDSTIIFloat(_TestIDSTBase):
|
| 655 |
+
def setup_method(self):
|
| 656 |
+
self.rdt = np.float32
|
| 657 |
+
self.dec = 6
|
| 658 |
+
self.type = 2
|
| 659 |
+
|
| 660 |
+
|
| 661 |
+
class TestIDSTIIInt(_TestIDSTBase):
|
| 662 |
+
def setup_method(self):
|
| 663 |
+
self.rdt = int
|
| 664 |
+
self.dec = 6
|
| 665 |
+
self.type = 2
|
| 666 |
+
|
| 667 |
+
|
| 668 |
+
class TestIDSTIIIDouble(_TestIDSTBase):
|
| 669 |
+
def setup_method(self):
|
| 670 |
+
self.rdt = np.float64
|
| 671 |
+
self.dec = 14
|
| 672 |
+
self.type = 3
|
| 673 |
+
|
| 674 |
+
|
| 675 |
+
class TestIDSTIIIFloat(_TestIDSTBase):
|
| 676 |
+
def setup_method(self):
|
| 677 |
+
self.rdt = np.float32
|
| 678 |
+
self.dec = 6
|
| 679 |
+
self.type = 3
|
| 680 |
+
|
| 681 |
+
|
| 682 |
+
class TestIDSTIIIInt(_TestIDSTBase):
|
| 683 |
+
def setup_method(self):
|
| 684 |
+
self.rdt = int
|
| 685 |
+
self.dec = 6
|
| 686 |
+
self.type = 3
|
| 687 |
+
|
| 688 |
+
|
| 689 |
+
class TestIDSTIVDouble(_TestIDSTBase):
|
| 690 |
+
def setup_method(self):
|
| 691 |
+
self.rdt = np.float64
|
| 692 |
+
self.dec = 12
|
| 693 |
+
self.type = 4
|
| 694 |
+
|
| 695 |
+
|
| 696 |
+
class TestIDSTIVFloat(_TestIDSTBase):
|
| 697 |
+
def setup_method(self):
|
| 698 |
+
self.rdt = np.float32
|
| 699 |
+
self.dec = 6
|
| 700 |
+
self.type = 4
|
| 701 |
+
|
| 702 |
+
|
| 703 |
+
class TestIDSTIVnt(_TestIDSTBase):
|
| 704 |
+
def setup_method(self):
|
| 705 |
+
self.rdt = int
|
| 706 |
+
self.dec = 6
|
| 707 |
+
self.type = 4
|
| 708 |
+
|
| 709 |
+
|
| 710 |
+
class TestOverwrite:
|
| 711 |
+
"""Check input overwrite behavior."""
|
| 712 |
+
|
| 713 |
+
real_dtypes = [np.float32, np.float64]
|
| 714 |
+
|
| 715 |
+
def _check(self, x, routine, type, fftsize, axis, norm, overwrite_x, **kw):
|
| 716 |
+
x2 = x.copy()
|
| 717 |
+
routine(x2, type, fftsize, axis, norm, overwrite_x=overwrite_x)
|
| 718 |
+
|
| 719 |
+
sig = (f"{routine.__name__}({x.dtype}{x.shape!r}, {fftsize!r}, "
|
| 720 |
+
f"axis={axis!r}, overwrite_x={overwrite_x!r})")
|
| 721 |
+
if not overwrite_x:
|
| 722 |
+
assert_equal(x2, x, err_msg=f"spurious overwrite in {sig}")
|
| 723 |
+
|
| 724 |
+
def _check_1d(self, routine, dtype, shape, axis):
|
| 725 |
+
rng = np.random.RandomState(1234)
|
| 726 |
+
if np.issubdtype(dtype, np.complexfloating):
|
| 727 |
+
data = rng.randn(*shape) + 1j*rng.randn(*shape)
|
| 728 |
+
else:
|
| 729 |
+
data = rng.randn(*shape)
|
| 730 |
+
data = data.astype(dtype)
|
| 731 |
+
|
| 732 |
+
for type in [1, 2, 3, 4]:
|
| 733 |
+
for overwrite_x in [True, False]:
|
| 734 |
+
for norm in [None, 'ortho']:
|
| 735 |
+
self._check(data, routine, type, None, axis, norm,
|
| 736 |
+
overwrite_x)
|
| 737 |
+
|
| 738 |
+
def test_dct(self):
|
| 739 |
+
for dtype in self.real_dtypes:
|
| 740 |
+
self._check_1d(dct, dtype, (16,), -1)
|
| 741 |
+
self._check_1d(dct, dtype, (16, 2), 0)
|
| 742 |
+
self._check_1d(dct, dtype, (2, 16), 1)
|
| 743 |
+
|
| 744 |
+
def test_idct(self):
|
| 745 |
+
for dtype in self.real_dtypes:
|
| 746 |
+
self._check_1d(idct, dtype, (16,), -1)
|
| 747 |
+
self._check_1d(idct, dtype, (16, 2), 0)
|
| 748 |
+
self._check_1d(idct, dtype, (2, 16), 1)
|
| 749 |
+
|
| 750 |
+
def test_dst(self):
|
| 751 |
+
for dtype in self.real_dtypes:
|
| 752 |
+
self._check_1d(dst, dtype, (16,), -1)
|
| 753 |
+
self._check_1d(dst, dtype, (16, 2), 0)
|
| 754 |
+
self._check_1d(dst, dtype, (2, 16), 1)
|
| 755 |
+
|
| 756 |
+
def test_idst(self):
|
| 757 |
+
for dtype in self.real_dtypes:
|
| 758 |
+
self._check_1d(idst, dtype, (16,), -1)
|
| 759 |
+
self._check_1d(idst, dtype, (16, 2), 0)
|
| 760 |
+
self._check_1d(idst, dtype, (2, 16), 1)
|
| 761 |
+
|
| 762 |
+
|
| 763 |
+
class Test_DCTN_IDCTN:
|
| 764 |
+
dec = 14
|
| 765 |
+
dct_type = [1, 2, 3, 4]
|
| 766 |
+
norms = [None, 'ortho']
|
| 767 |
+
rstate = np.random.RandomState(1234)
|
| 768 |
+
shape = (32, 16)
|
| 769 |
+
data = rstate.randn(*shape)
|
| 770 |
+
|
| 771 |
+
@pytest.mark.parametrize('fforward,finverse', [(dctn, idctn),
|
| 772 |
+
(dstn, idstn)])
|
| 773 |
+
@pytest.mark.parametrize('axes', [None,
|
| 774 |
+
1, (1,), [1],
|
| 775 |
+
0, (0,), [0],
|
| 776 |
+
(0, 1), [0, 1],
|
| 777 |
+
(-2, -1), [-2, -1]])
|
| 778 |
+
@pytest.mark.parametrize('dct_type', dct_type)
|
| 779 |
+
@pytest.mark.parametrize('norm', ['ortho'])
|
| 780 |
+
def test_axes_round_trip(self, fforward, finverse, axes, dct_type, norm):
|
| 781 |
+
tmp = fforward(self.data, type=dct_type, axes=axes, norm=norm)
|
| 782 |
+
tmp = finverse(tmp, type=dct_type, axes=axes, norm=norm)
|
| 783 |
+
assert_array_almost_equal(self.data, tmp, decimal=12)
|
| 784 |
+
|
| 785 |
+
@pytest.mark.parametrize('fforward,fforward_ref', [(dctn, dct_2d_ref),
|
| 786 |
+
(dstn, dst_2d_ref)])
|
| 787 |
+
@pytest.mark.parametrize('dct_type', dct_type)
|
| 788 |
+
@pytest.mark.parametrize('norm', norms)
|
| 789 |
+
def test_dctn_vs_2d_reference(self, fforward, fforward_ref,
|
| 790 |
+
dct_type, norm):
|
| 791 |
+
y1 = fforward(self.data, type=dct_type, axes=None, norm=norm)
|
| 792 |
+
y2 = fforward_ref(self.data, type=dct_type, norm=norm)
|
| 793 |
+
assert_array_almost_equal(y1, y2, decimal=11)
|
| 794 |
+
|
| 795 |
+
@pytest.mark.parametrize('finverse,finverse_ref', [(idctn, idct_2d_ref),
|
| 796 |
+
(idstn, idst_2d_ref)])
|
| 797 |
+
@pytest.mark.parametrize('dct_type', dct_type)
|
| 798 |
+
@pytest.mark.parametrize('norm', [None, 'ortho'])
|
| 799 |
+
def test_idctn_vs_2d_reference(self, finverse, finverse_ref,
|
| 800 |
+
dct_type, norm):
|
| 801 |
+
fdata = dctn(self.data, type=dct_type, norm=norm)
|
| 802 |
+
y1 = finverse(fdata, type=dct_type, norm=norm)
|
| 803 |
+
y2 = finverse_ref(fdata, type=dct_type, norm=norm)
|
| 804 |
+
assert_array_almost_equal(y1, y2, decimal=11)
|
| 805 |
+
|
| 806 |
+
@pytest.mark.parametrize('fforward,finverse', [(dctn, idctn),
|
| 807 |
+
(dstn, idstn)])
|
| 808 |
+
def test_axes_and_shape(self, fforward, finverse):
|
| 809 |
+
with assert_raises(ValueError,
|
| 810 |
+
match="when given, axes and shape arguments"
|
| 811 |
+
" have to be of the same length"):
|
| 812 |
+
fforward(self.data, shape=self.data.shape[0], axes=(0, 1))
|
| 813 |
+
|
| 814 |
+
with assert_raises(ValueError,
|
| 815 |
+
match="when given, axes and shape arguments"
|
| 816 |
+
" have to be of the same length"):
|
| 817 |
+
fforward(self.data, shape=self.data.shape[0], axes=None)
|
| 818 |
+
|
| 819 |
+
with assert_raises(ValueError,
|
| 820 |
+
match="when given, axes and shape arguments"
|
| 821 |
+
" have to be of the same length"):
|
| 822 |
+
fforward(self.data, shape=self.data.shape, axes=0)
|
| 823 |
+
|
| 824 |
+
@pytest.mark.parametrize('fforward', [dctn, dstn])
|
| 825 |
+
def test_shape(self, fforward):
|
| 826 |
+
tmp = fforward(self.data, shape=(128, 128), axes=None)
|
| 827 |
+
assert_equal(tmp.shape, (128, 128))
|
| 828 |
+
|
| 829 |
+
@pytest.mark.parametrize('fforward,finverse', [(dctn, idctn),
|
| 830 |
+
(dstn, idstn)])
|
| 831 |
+
@pytest.mark.parametrize('axes', [1, (1,), [1],
|
| 832 |
+
0, (0,), [0]])
|
| 833 |
+
def test_shape_is_none_with_axes(self, fforward, finverse, axes):
|
| 834 |
+
tmp = fforward(self.data, shape=None, axes=axes, norm='ortho')
|
| 835 |
+
tmp = finverse(tmp, shape=None, axes=axes, norm='ortho')
|
| 836 |
+
assert_array_almost_equal(self.data, tmp, decimal=self.dec)
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/integrate/LICENSE_DOP
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Copyright (C) 2025 SciPy developers
|
| 2 |
+
|
| 3 |
+
Redistribution and use in source and binary forms, with or without
|
| 4 |
+
modification, are permitted provided that the following conditions are met:
|
| 5 |
+
|
| 6 |
+
a. Redistributions of source code must retain the above copyright notice,
|
| 7 |
+
this list of conditions and the following disclaimer.
|
| 8 |
+
b. Redistributions in binary form must reproduce the above copyright
|
| 9 |
+
notice, this list of conditions and the following disclaimer in the
|
| 10 |
+
documentation and/or other materials provided with the distribution.
|
| 11 |
+
c. Names of the SciPy Developers may not be used to endorse or promote
|
| 12 |
+
products derived from this software without specific prior written
|
| 13 |
+
permission.
|
| 14 |
+
|
| 15 |
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
| 16 |
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
| 17 |
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
| 18 |
+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
|
| 19 |
+
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
| 20 |
+
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
| 21 |
+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
| 22 |
+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
| 23 |
+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
| 24 |
+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
| 25 |
+
THE POSSIBILITY OF SUCH DAMAGE.
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
DOP library consisting Dormand-Prince (4)5 and 8(5,3) integrators, is a
|
| 29 |
+
C translation of the Fortran code written by Ernst Hairer, and Gerhard
|
| 30 |
+
Wanner with the original descriptions below.
|
| 31 |
+
|
| 32 |
+
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
| 33 |
+
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
| 34 |
+
NUMERICAL SOLUTION OF A SYSTEM OF FIRST 0RDER
|
| 35 |
+
ORDINARY DIFFERENTIAL EQUATIONS Y'=F(X,Y).
|
| 36 |
+
THIS IS AN EXPLICIT RUNGE-KUTTA METHOD OF ORDER 8(5,3)
|
| 37 |
+
DUE TO DORMAND & PRINCE (WITH STEPSIZE CONTROL AND
|
| 38 |
+
DENSE OUTPUT)
|
| 39 |
+
|
| 40 |
+
AUTHORS: E. HAIRER AND G. WANNER
|
| 41 |
+
UNIVERSITE DE GENEVE, DEPT. DE MATHEMATIQUES
|
| 42 |
+
CH-1211 GENEVE 24, SWITZERLAND
|
| 43 |
+
E-MAIL: Ernst.Hairer@math.unige.ch
|
| 44 |
+
Gerhard.Wanner@math.unige.ch
|
| 45 |
+
|
| 46 |
+
THIS CODE IS DESCRIBED IN:
|
| 47 |
+
E. HAIRER, S.P. NORSETT AND G. WANNER, SOLVING ORDINARY
|
| 48 |
+
DIFFERENTIAL EQUATIONS I. NONSTIFF PROBLEMS. 2ND EDITION.
|
| 49 |
+
SPRINGER SERIES IN COMPUTATIONAL MATHEMATICS,
|
| 50 |
+
SPRINGER-VERLAG (1993)
|
| 51 |
+
|
| 52 |
+
VERSION OF APRIL 25, 1996
|
| 53 |
+
(latest correction of a small bug: August 8, 2005)
|
| 54 |
+
|
| 55 |
+
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
| 56 |
+
NUMERICAL SOLUTION OF A SYSTEM OF FIRST 0RDER
|
| 57 |
+
ORDINARY DIFFERENTIAL EQUATIONS Y'=F(X,Y).
|
| 58 |
+
THIS IS AN EXPLICIT RUNGE-KUTTA METHOD OF ORDER (4)5
|
| 59 |
+
DUE TO DORMAND & PRINCE (WITH STEPSIZE CONTROL AND
|
| 60 |
+
DENSE OUTPUT).
|
| 61 |
+
|
| 62 |
+
AUTHORS: E. HAIRER AND G. WANNER
|
| 63 |
+
UNIVERSITE DE GENEVE, DEPT. DE MATHEMATIQUES
|
| 64 |
+
CH-1211 GENEVE 24, SWITZERLAND
|
| 65 |
+
E-MAIL: Ernst.Hairer@math.unige.ch
|
| 66 |
+
Gerhard.Wanner@math.unige.ch
|
| 67 |
+
|
| 68 |
+
THIS CODE IS DESCRIBED IN:
|
| 69 |
+
E. HAIRER, S.P. NORSETT AND G. WANNER, SOLVING ORDINARY
|
| 70 |
+
DIFFERENTIAL EQUATIONS I. NONSTIFF PROBLEMS. 2ND EDITION.
|
| 71 |
+
SPRINGER SERIES IN COMPUTATIONAL MATHEMATICS,
|
| 72 |
+
SPRINGER-VERLAG (1993)
|
| 73 |
+
|
| 74 |
+
VERSION OF APRIL 25, 1996
|
| 75 |
+
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
| 76 |
+
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/integrate/__init__.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
=============================================
|
| 3 |
+
Integration and ODEs (:mod:`scipy.integrate`)
|
| 4 |
+
=============================================
|
| 5 |
+
|
| 6 |
+
.. currentmodule:: scipy.integrate
|
| 7 |
+
|
| 8 |
+
Integrating functions, given function object
|
| 9 |
+
============================================
|
| 10 |
+
|
| 11 |
+
.. autosummary::
|
| 12 |
+
:toctree: generated/
|
| 13 |
+
|
| 14 |
+
quad -- General purpose integration
|
| 15 |
+
quad_vec -- General purpose integration of vector-valued functions
|
| 16 |
+
cubature -- General purpose multi-dimensional integration of array-valued functions
|
| 17 |
+
dblquad -- General purpose double integration
|
| 18 |
+
tplquad -- General purpose triple integration
|
| 19 |
+
nquad -- General purpose N-D integration
|
| 20 |
+
tanhsinh -- General purpose elementwise integration
|
| 21 |
+
fixed_quad -- Integrate func(x) using Gaussian quadrature of order n
|
| 22 |
+
newton_cotes -- Weights and error coefficient for Newton-Cotes integration
|
| 23 |
+
lebedev_rule
|
| 24 |
+
qmc_quad -- N-D integration using Quasi-Monte Carlo quadrature
|
| 25 |
+
IntegrationWarning -- Warning on issues during integration
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
Integrating functions, given fixed samples
|
| 29 |
+
==========================================
|
| 30 |
+
|
| 31 |
+
.. autosummary::
|
| 32 |
+
:toctree: generated/
|
| 33 |
+
|
| 34 |
+
trapezoid -- Use trapezoidal rule to compute integral.
|
| 35 |
+
cumulative_trapezoid -- Use trapezoidal rule to cumulatively compute integral.
|
| 36 |
+
simpson -- Use Simpson's rule to compute integral from samples.
|
| 37 |
+
cumulative_simpson -- Use Simpson's rule to cumulatively compute integral from samples.
|
| 38 |
+
romb -- Use Romberg Integration to compute integral from
|
| 39 |
+
-- (2**k + 1) evenly-spaced samples.
|
| 40 |
+
|
| 41 |
+
.. seealso::
|
| 42 |
+
|
| 43 |
+
:mod:`scipy.special` for orthogonal polynomials (special) for Gaussian
|
| 44 |
+
quadrature roots and weights for other weighting factors and regions.
|
| 45 |
+
|
| 46 |
+
Summation
|
| 47 |
+
=========
|
| 48 |
+
|
| 49 |
+
.. autosummary::
|
| 50 |
+
:toctree: generated/
|
| 51 |
+
|
| 52 |
+
nsum
|
| 53 |
+
|
| 54 |
+
Solving initial value problems for ODE systems
|
| 55 |
+
==============================================
|
| 56 |
+
|
| 57 |
+
The solvers are implemented as individual classes, which can be used directly
|
| 58 |
+
(low-level usage) or through a convenience function.
|
| 59 |
+
|
| 60 |
+
.. autosummary::
|
| 61 |
+
:toctree: generated/
|
| 62 |
+
|
| 63 |
+
solve_ivp -- Convenient function for ODE integration.
|
| 64 |
+
RK23 -- Explicit Runge-Kutta solver of order 3(2).
|
| 65 |
+
RK45 -- Explicit Runge-Kutta solver of order 5(4).
|
| 66 |
+
DOP853 -- Explicit Runge-Kutta solver of order 8.
|
| 67 |
+
Radau -- Implicit Runge-Kutta solver of order 5.
|
| 68 |
+
BDF -- Implicit multi-step variable order (1 to 5) solver.
|
| 69 |
+
LSODA -- LSODA solver from ODEPACK Fortran package.
|
| 70 |
+
OdeSolver -- Base class for ODE solvers.
|
| 71 |
+
DenseOutput -- Local interpolant for computing a dense output.
|
| 72 |
+
OdeSolution -- Class which represents a continuous ODE solution.
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
Old API
|
| 76 |
+
-------
|
| 77 |
+
|
| 78 |
+
These are the routines developed earlier for SciPy. They wrap older solvers
|
| 79 |
+
implemented in Fortran (mostly ODEPACK). While the interface to them is not
|
| 80 |
+
particularly convenient and certain features are missing compared to the new
|
| 81 |
+
API, the solvers themselves are of good quality and work fast as compiled
|
| 82 |
+
Fortran code. In some cases, it might be worth using this old API.
|
| 83 |
+
|
| 84 |
+
.. autosummary::
|
| 85 |
+
:toctree: generated/
|
| 86 |
+
|
| 87 |
+
odeint -- General integration of ordinary differential equations.
|
| 88 |
+
ode -- Integrate ODE using VODE and ZVODE routines.
|
| 89 |
+
complex_ode -- Convert a complex-valued ODE to real-valued and integrate.
|
| 90 |
+
ODEintWarning -- Warning raised during the execution of `odeint`.
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
Solving boundary value problems for ODE systems
|
| 94 |
+
===============================================
|
| 95 |
+
|
| 96 |
+
.. autosummary::
|
| 97 |
+
:toctree: generated/
|
| 98 |
+
|
| 99 |
+
solve_bvp -- Solve a boundary value problem for a system of ODEs.
|
| 100 |
+
""" # noqa: E501
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
from ._quadrature import *
|
| 104 |
+
from ._odepack_py import *
|
| 105 |
+
from ._quadpack_py import *
|
| 106 |
+
from ._ode import *
|
| 107 |
+
from ._bvp import solve_bvp
|
| 108 |
+
from ._ivp import (solve_ivp, OdeSolution, DenseOutput,
|
| 109 |
+
OdeSolver, RK23, RK45, DOP853, Radau, BDF, LSODA)
|
| 110 |
+
from ._quad_vec import quad_vec
|
| 111 |
+
from ._tanhsinh import nsum, tanhsinh
|
| 112 |
+
from ._cubature import cubature
|
| 113 |
+
from ._lebedev import lebedev_rule
|
| 114 |
+
|
| 115 |
+
# Deprecated namespaces, to be removed in v2.0.0
|
| 116 |
+
from . import dop, lsoda, vode, odepack, quadpack
|
| 117 |
+
|
| 118 |
+
__all__ = [s for s in dir() if not s.startswith('_')]
|
| 119 |
+
|
| 120 |
+
from scipy._lib._testutils import PytestTester
|
| 121 |
+
test = PytestTester(__name__)
|
| 122 |
+
del PytestTester
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/integrate/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (4.86 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/integrate/__pycache__/_bvp.cpython-312.pyc
ADDED
|
Binary file (48.9 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/integrate/__pycache__/_cubature.cpython-312.pyc
ADDED
|
Binary file (26.9 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/integrate/__pycache__/_lebedev.cpython-312.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7443c521081b6b9709c5d620b92a6c4baab8ff9d2ffac19f1cf63cf13b862d86
|
| 3 |
+
size 180838
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/integrate/__pycache__/_ode.cpython-312.pyc
ADDED
|
Binary file (52.4 kB). View file
|
|
|