Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/__init__.py +103 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/__pycache__/__init__.cpython-310.pyc +0 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/__pycache__/_basic.cpython-310.pyc +0 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/__pycache__/_helper.cpython-310.pyc +0 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/__pycache__/_pseudo_diffs.cpython-310.pyc +0 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/__pycache__/_realtransforms.cpython-310.pyc +0 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/__pycache__/basic.cpython-310.pyc +0 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/__pycache__/helper.cpython-310.pyc +0 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/__pycache__/pseudo_diffs.cpython-310.pyc +0 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/__pycache__/realtransforms.cpython-310.pyc +0 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/_basic.py +428 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/_helper.py +115 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/_pseudo_diffs.py +551 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/_realtransforms.py +598 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/basic.py +20 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/helper.py +19 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/pseudo_diffs.py +22 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/realtransforms.py +19 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/tests/__init__.py +0 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/tests/__pycache__/__init__.cpython-310.pyc +0 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/tests/__pycache__/test_basic.cpython-310.pyc +0 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/tests/__pycache__/test_helper.cpython-310.pyc +0 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/tests/__pycache__/test_import.cpython-310.pyc +0 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/tests/__pycache__/test_pseudo_diffs.cpython-310.pyc +0 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/tests/__pycache__/test_real_transforms.cpython-310.pyc +0 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/tests/fftw_single_ref.npz +0 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/tests/test.npz +0 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/tests/test_basic.py +873 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/tests/test_helper.py +54 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/tests/test_import.py +33 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/tests/test_pseudo_diffs.py +380 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/tests/test_real_transforms.py +815 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/optimize/_lsq/__pycache__/bvls.cpython-310.pyc +0 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/optimize/_lsq/__pycache__/common.cpython-310.pyc +0 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/optimize/_lsq/__pycache__/dogbox.cpython-310.pyc +0 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/optimize/_lsq/__pycache__/least_squares.cpython-310.pyc +0 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/optimize/_lsq/__pycache__/lsq_linear.cpython-310.pyc +0 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/optimize/_lsq/__pycache__/trf_linear.cpython-310.pyc +0 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/optimize/_trlib/__init__.py +12 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/optimize/_trlib/__pycache__/__init__.cpython-310.pyc +0 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/optimize/cython_optimize/__init__.py +133 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/optimize/cython_optimize/__pycache__/__init__.cpython-310.pyc +0 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/optimize/cython_optimize/_zeros.pxd +33 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/optimize/cython_optimize/c_zeros.pxd +26 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/sparse/__init__.py +323 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/sparse/__pycache__/__init__.cpython-310.pyc +0 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/sparse/__pycache__/_base.cpython-310.pyc +0 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/sparse/__pycache__/_bsr.cpython-310.pyc +0 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/sparse/__pycache__/_compressed.cpython-310.pyc +0 -0
- emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/sparse/__pycache__/_construct.cpython-310.pyc +0 -0
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/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
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (3.2 kB). View file
|
|
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/__pycache__/_basic.cpython-310.pyc
ADDED
|
Binary file (13.2 kB). View file
|
|
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/__pycache__/_helper.cpython-310.pyc
ADDED
|
Binary file (3.62 kB). View file
|
|
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/__pycache__/_pseudo_diffs.cpython-310.pyc
ADDED
|
Binary file (12.4 kB). View file
|
|
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/__pycache__/_realtransforms.cpython-310.pyc
ADDED
|
Binary file (19.1 kB). View file
|
|
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/__pycache__/basic.cpython-310.pyc
ADDED
|
Binary file (671 Bytes). View file
|
|
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/__pycache__/helper.cpython-310.pyc
ADDED
|
Binary file (679 Bytes). View file
|
|
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/__pycache__/pseudo_diffs.cpython-310.pyc
ADDED
|
Binary file (739 Bytes). View file
|
|
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/__pycache__/realtransforms.cpython-310.pyc
ADDED
|
Binary file (696 Bytes). View file
|
|
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/_basic.py
ADDED
|
@@ -0,0 +1,428 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Discrete Fourier Transforms - _basic.py
|
| 3 |
+
"""
|
| 4 |
+
# Created by Pearu Peterson, August,September 2002
|
| 5 |
+
__all__ = ['fft','ifft','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)
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/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("n = %s is not valid. "
|
| 49 |
+
"n must be a nonnegative integer." % n)
|
| 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
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/_pseudo_diffs.py
ADDED
|
@@ -0,0 +1,551 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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 2002
|
| 5 |
+
|
| 6 |
+
__all__ = ['diff',
|
| 7 |
+
'tilbert','itilbert','hilbert','ihilbert',
|
| 8 |
+
'cs_diff','cc_diff','sc_diff','ss_diff',
|
| 9 |
+
'shift']
|
| 10 |
+
|
| 11 |
+
from numpy import pi, asarray, sin, cos, sinh, cosh, tanh, iscomplexobj
|
| 12 |
+
from . import convolve
|
| 13 |
+
|
| 14 |
+
from scipy.fft._pocketfft.helper import _datacopied
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
_cache = {}
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def diff(x,order=1,period=None, _cache=_cache):
|
| 21 |
+
"""
|
| 22 |
+
Return kth derivative (or integral) of a periodic sequence x.
|
| 23 |
+
|
| 24 |
+
If x_j and y_j are Fourier coefficients of periodic functions x
|
| 25 |
+
and y, respectively, then::
|
| 26 |
+
|
| 27 |
+
y_j = pow(sqrt(-1)*j*2*pi/period, order) * x_j
|
| 28 |
+
y_0 = 0 if order is not 0.
|
| 29 |
+
|
| 30 |
+
Parameters
|
| 31 |
+
----------
|
| 32 |
+
x : array_like
|
| 33 |
+
Input array.
|
| 34 |
+
order : int, optional
|
| 35 |
+
The order of differentiation. Default order is 1. If order is
|
| 36 |
+
negative, then integration is carried out under the assumption
|
| 37 |
+
that ``x_0 == 0``.
|
| 38 |
+
period : float, optional
|
| 39 |
+
The assumed period of the sequence. Default is ``2*pi``.
|
| 40 |
+
|
| 41 |
+
Notes
|
| 42 |
+
-----
|
| 43 |
+
If ``sum(x, axis=0) = 0`` then ``diff(diff(x, k), -k) == x`` (within
|
| 44 |
+
numerical accuracy).
|
| 45 |
+
|
| 46 |
+
For odd order and even ``len(x)``, the Nyquist mode is taken zero.
|
| 47 |
+
|
| 48 |
+
"""
|
| 49 |
+
tmp = asarray(x)
|
| 50 |
+
if order == 0:
|
| 51 |
+
return tmp
|
| 52 |
+
if iscomplexobj(tmp):
|
| 53 |
+
return diff(tmp.real,order,period)+1j*diff(tmp.imag,order,period)
|
| 54 |
+
if period is not None:
|
| 55 |
+
c = 2*pi/period
|
| 56 |
+
else:
|
| 57 |
+
c = 1.0
|
| 58 |
+
n = len(x)
|
| 59 |
+
omega = _cache.get((n,order,c))
|
| 60 |
+
if omega is None:
|
| 61 |
+
if len(_cache) > 20:
|
| 62 |
+
while _cache:
|
| 63 |
+
_cache.popitem()
|
| 64 |
+
|
| 65 |
+
def kernel(k,order=order,c=c):
|
| 66 |
+
if k:
|
| 67 |
+
return pow(c*k,order)
|
| 68 |
+
return 0
|
| 69 |
+
omega = convolve.init_convolution_kernel(n,kernel,d=order,
|
| 70 |
+
zero_nyquist=1)
|
| 71 |
+
_cache[(n,order,c)] = omega
|
| 72 |
+
overwrite_x = _datacopied(tmp, x)
|
| 73 |
+
return convolve.convolve(tmp,omega,swap_real_imag=order % 2,
|
| 74 |
+
overwrite_x=overwrite_x)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
del _cache
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
_cache = {}
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def tilbert(x, h, period=None, _cache=_cache):
|
| 84 |
+
"""
|
| 85 |
+
Return h-Tilbert transform of a periodic sequence x.
|
| 86 |
+
|
| 87 |
+
If x_j and y_j are Fourier coefficients of periodic functions x
|
| 88 |
+
and y, respectively, then::
|
| 89 |
+
|
| 90 |
+
y_j = sqrt(-1)*coth(j*h*2*pi/period) * x_j
|
| 91 |
+
y_0 = 0
|
| 92 |
+
|
| 93 |
+
Parameters
|
| 94 |
+
----------
|
| 95 |
+
x : array_like
|
| 96 |
+
The input array to transform.
|
| 97 |
+
h : float
|
| 98 |
+
Defines the parameter of the Tilbert transform.
|
| 99 |
+
period : float, optional
|
| 100 |
+
The assumed period of the sequence. Default period is ``2*pi``.
|
| 101 |
+
|
| 102 |
+
Returns
|
| 103 |
+
-------
|
| 104 |
+
tilbert : ndarray
|
| 105 |
+
The result of the transform.
|
| 106 |
+
|
| 107 |
+
Notes
|
| 108 |
+
-----
|
| 109 |
+
If ``sum(x, axis=0) == 0`` and ``n = len(x)`` is odd, then
|
| 110 |
+
``tilbert(itilbert(x)) == x``.
|
| 111 |
+
|
| 112 |
+
If ``2 * pi * h / period`` is approximately 10 or larger, then
|
| 113 |
+
numerically ``tilbert == hilbert``
|
| 114 |
+
(theoretically oo-Tilbert == Hilbert).
|
| 115 |
+
|
| 116 |
+
For even ``len(x)``, the Nyquist mode of ``x`` is taken zero.
|
| 117 |
+
|
| 118 |
+
"""
|
| 119 |
+
tmp = asarray(x)
|
| 120 |
+
if iscomplexobj(tmp):
|
| 121 |
+
return tilbert(tmp.real, h, period) + \
|
| 122 |
+
1j * tilbert(tmp.imag, h, period)
|
| 123 |
+
|
| 124 |
+
if period is not None:
|
| 125 |
+
h = h * 2 * pi / period
|
| 126 |
+
|
| 127 |
+
n = len(x)
|
| 128 |
+
omega = _cache.get((n, h))
|
| 129 |
+
if omega is None:
|
| 130 |
+
if len(_cache) > 20:
|
| 131 |
+
while _cache:
|
| 132 |
+
_cache.popitem()
|
| 133 |
+
|
| 134 |
+
def kernel(k, h=h):
|
| 135 |
+
if k:
|
| 136 |
+
return 1.0/tanh(h*k)
|
| 137 |
+
|
| 138 |
+
return 0
|
| 139 |
+
|
| 140 |
+
omega = convolve.init_convolution_kernel(n, kernel, d=1)
|
| 141 |
+
_cache[(n,h)] = omega
|
| 142 |
+
|
| 143 |
+
overwrite_x = _datacopied(tmp, x)
|
| 144 |
+
return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x)
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
del _cache
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
_cache = {}
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
def itilbert(x,h,period=None, _cache=_cache):
|
| 154 |
+
"""
|
| 155 |
+
Return inverse h-Tilbert transform of a periodic sequence x.
|
| 156 |
+
|
| 157 |
+
If ``x_j`` and ``y_j`` are Fourier coefficients of periodic functions x
|
| 158 |
+
and y, respectively, then::
|
| 159 |
+
|
| 160 |
+
y_j = -sqrt(-1)*tanh(j*h*2*pi/period) * x_j
|
| 161 |
+
y_0 = 0
|
| 162 |
+
|
| 163 |
+
For more details, see `tilbert`.
|
| 164 |
+
|
| 165 |
+
"""
|
| 166 |
+
tmp = asarray(x)
|
| 167 |
+
if iscomplexobj(tmp):
|
| 168 |
+
return itilbert(tmp.real,h,period) + \
|
| 169 |
+
1j*itilbert(tmp.imag,h,period)
|
| 170 |
+
if period is not None:
|
| 171 |
+
h = h*2*pi/period
|
| 172 |
+
n = len(x)
|
| 173 |
+
omega = _cache.get((n,h))
|
| 174 |
+
if omega is None:
|
| 175 |
+
if len(_cache) > 20:
|
| 176 |
+
while _cache:
|
| 177 |
+
_cache.popitem()
|
| 178 |
+
|
| 179 |
+
def kernel(k,h=h):
|
| 180 |
+
if k:
|
| 181 |
+
return -tanh(h*k)
|
| 182 |
+
return 0
|
| 183 |
+
omega = convolve.init_convolution_kernel(n,kernel,d=1)
|
| 184 |
+
_cache[(n,h)] = omega
|
| 185 |
+
overwrite_x = _datacopied(tmp, x)
|
| 186 |
+
return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x)
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
del _cache
|
| 190 |
+
|
| 191 |
+
|
| 192 |
+
_cache = {}
|
| 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 |
+
tmp = asarray(x)
|
| 235 |
+
if iscomplexobj(tmp):
|
| 236 |
+
return hilbert(tmp.real)+1j*hilbert(tmp.imag)
|
| 237 |
+
n = len(x)
|
| 238 |
+
omega = _cache.get(n)
|
| 239 |
+
if omega is None:
|
| 240 |
+
if len(_cache) > 20:
|
| 241 |
+
while _cache:
|
| 242 |
+
_cache.popitem()
|
| 243 |
+
|
| 244 |
+
def kernel(k):
|
| 245 |
+
if k > 0:
|
| 246 |
+
return 1.0
|
| 247 |
+
elif k < 0:
|
| 248 |
+
return -1.0
|
| 249 |
+
return 0.0
|
| 250 |
+
omega = convolve.init_convolution_kernel(n,kernel,d=1)
|
| 251 |
+
_cache[n] = omega
|
| 252 |
+
overwrite_x = _datacopied(tmp, x)
|
| 253 |
+
return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x)
|
| 254 |
+
|
| 255 |
+
|
| 256 |
+
del _cache
|
| 257 |
+
|
| 258 |
+
|
| 259 |
+
def ihilbert(x):
|
| 260 |
+
"""
|
| 261 |
+
Return inverse Hilbert transform of a periodic sequence x.
|
| 262 |
+
|
| 263 |
+
If ``x_j`` and ``y_j`` are Fourier coefficients of periodic functions x
|
| 264 |
+
and y, respectively, then::
|
| 265 |
+
|
| 266 |
+
y_j = -sqrt(-1)*sign(j) * x_j
|
| 267 |
+
y_0 = 0
|
| 268 |
+
|
| 269 |
+
"""
|
| 270 |
+
return -hilbert(x)
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
_cache = {}
|
| 274 |
+
|
| 275 |
+
|
| 276 |
+
def cs_diff(x, a, b, period=None, _cache=_cache):
|
| 277 |
+
"""
|
| 278 |
+
Return (a,b)-cosh/sinh pseudo-derivative of a periodic sequence.
|
| 279 |
+
|
| 280 |
+
If ``x_j`` and ``y_j`` are Fourier coefficients of periodic functions x
|
| 281 |
+
and y, respectively, then::
|
| 282 |
+
|
| 283 |
+
y_j = -sqrt(-1)*cosh(j*a*2*pi/period)/sinh(j*b*2*pi/period) * x_j
|
| 284 |
+
y_0 = 0
|
| 285 |
+
|
| 286 |
+
Parameters
|
| 287 |
+
----------
|
| 288 |
+
x : array_like
|
| 289 |
+
The array to take the pseudo-derivative from.
|
| 290 |
+
a, b : float
|
| 291 |
+
Defines the parameters of the cosh/sinh pseudo-differential
|
| 292 |
+
operator.
|
| 293 |
+
period : float, optional
|
| 294 |
+
The period of the sequence. Default period is ``2*pi``.
|
| 295 |
+
|
| 296 |
+
Returns
|
| 297 |
+
-------
|
| 298 |
+
cs_diff : ndarray
|
| 299 |
+
Pseudo-derivative of periodic sequence `x`.
|
| 300 |
+
|
| 301 |
+
Notes
|
| 302 |
+
-----
|
| 303 |
+
For even len(`x`), the Nyquist mode of `x` is taken as zero.
|
| 304 |
+
|
| 305 |
+
"""
|
| 306 |
+
tmp = asarray(x)
|
| 307 |
+
if iscomplexobj(tmp):
|
| 308 |
+
return cs_diff(tmp.real,a,b,period) + \
|
| 309 |
+
1j*cs_diff(tmp.imag,a,b,period)
|
| 310 |
+
if period is not None:
|
| 311 |
+
a = a*2*pi/period
|
| 312 |
+
b = b*2*pi/period
|
| 313 |
+
n = len(x)
|
| 314 |
+
omega = _cache.get((n,a,b))
|
| 315 |
+
if omega is None:
|
| 316 |
+
if len(_cache) > 20:
|
| 317 |
+
while _cache:
|
| 318 |
+
_cache.popitem()
|
| 319 |
+
|
| 320 |
+
def kernel(k,a=a,b=b):
|
| 321 |
+
if k:
|
| 322 |
+
return -cosh(a*k)/sinh(b*k)
|
| 323 |
+
return 0
|
| 324 |
+
omega = convolve.init_convolution_kernel(n,kernel,d=1)
|
| 325 |
+
_cache[(n,a,b)] = omega
|
| 326 |
+
overwrite_x = _datacopied(tmp, x)
|
| 327 |
+
return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x)
|
| 328 |
+
|
| 329 |
+
|
| 330 |
+
del _cache
|
| 331 |
+
|
| 332 |
+
|
| 333 |
+
_cache = {}
|
| 334 |
+
|
| 335 |
+
|
| 336 |
+
def sc_diff(x, a, b, period=None, _cache=_cache):
|
| 337 |
+
"""
|
| 338 |
+
Return (a,b)-sinh/cosh pseudo-derivative of a periodic sequence x.
|
| 339 |
+
|
| 340 |
+
If x_j and y_j are Fourier coefficients of periodic functions x
|
| 341 |
+
and y, respectively, then::
|
| 342 |
+
|
| 343 |
+
y_j = sqrt(-1)*sinh(j*a*2*pi/period)/cosh(j*b*2*pi/period) * x_j
|
| 344 |
+
y_0 = 0
|
| 345 |
+
|
| 346 |
+
Parameters
|
| 347 |
+
----------
|
| 348 |
+
x : array_like
|
| 349 |
+
Input array.
|
| 350 |
+
a,b : float
|
| 351 |
+
Defines the parameters of the sinh/cosh pseudo-differential
|
| 352 |
+
operator.
|
| 353 |
+
period : float, optional
|
| 354 |
+
The period of the sequence x. Default is 2*pi.
|
| 355 |
+
|
| 356 |
+
Notes
|
| 357 |
+
-----
|
| 358 |
+
``sc_diff(cs_diff(x,a,b),b,a) == x``
|
| 359 |
+
For even ``len(x)``, the Nyquist mode of x is taken as zero.
|
| 360 |
+
|
| 361 |
+
"""
|
| 362 |
+
tmp = asarray(x)
|
| 363 |
+
if iscomplexobj(tmp):
|
| 364 |
+
return sc_diff(tmp.real,a,b,period) + \
|
| 365 |
+
1j*sc_diff(tmp.imag,a,b,period)
|
| 366 |
+
if period is not None:
|
| 367 |
+
a = a*2*pi/period
|
| 368 |
+
b = b*2*pi/period
|
| 369 |
+
n = len(x)
|
| 370 |
+
omega = _cache.get((n,a,b))
|
| 371 |
+
if omega is None:
|
| 372 |
+
if len(_cache) > 20:
|
| 373 |
+
while _cache:
|
| 374 |
+
_cache.popitem()
|
| 375 |
+
|
| 376 |
+
def kernel(k,a=a,b=b):
|
| 377 |
+
if k:
|
| 378 |
+
return sinh(a*k)/cosh(b*k)
|
| 379 |
+
return 0
|
| 380 |
+
omega = convolve.init_convolution_kernel(n,kernel,d=1)
|
| 381 |
+
_cache[(n,a,b)] = omega
|
| 382 |
+
overwrite_x = _datacopied(tmp, x)
|
| 383 |
+
return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x)
|
| 384 |
+
|
| 385 |
+
|
| 386 |
+
del _cache
|
| 387 |
+
|
| 388 |
+
|
| 389 |
+
_cache = {}
|
| 390 |
+
|
| 391 |
+
|
| 392 |
+
def ss_diff(x, a, b, period=None, _cache=_cache):
|
| 393 |
+
"""
|
| 394 |
+
Return (a,b)-sinh/sinh pseudo-derivative of a periodic sequence x.
|
| 395 |
+
|
| 396 |
+
If x_j and y_j are Fourier coefficients of periodic functions x
|
| 397 |
+
and y, respectively, then::
|
| 398 |
+
|
| 399 |
+
y_j = sinh(j*a*2*pi/period)/sinh(j*b*2*pi/period) * x_j
|
| 400 |
+
y_0 = a/b * x_0
|
| 401 |
+
|
| 402 |
+
Parameters
|
| 403 |
+
----------
|
| 404 |
+
x : array_like
|
| 405 |
+
The array to take the pseudo-derivative from.
|
| 406 |
+
a,b
|
| 407 |
+
Defines the parameters of the sinh/sinh pseudo-differential
|
| 408 |
+
operator.
|
| 409 |
+
period : float, optional
|
| 410 |
+
The period of the sequence x. Default is ``2*pi``.
|
| 411 |
+
|
| 412 |
+
Notes
|
| 413 |
+
-----
|
| 414 |
+
``ss_diff(ss_diff(x,a,b),b,a) == x``
|
| 415 |
+
|
| 416 |
+
"""
|
| 417 |
+
tmp = asarray(x)
|
| 418 |
+
if iscomplexobj(tmp):
|
| 419 |
+
return ss_diff(tmp.real,a,b,period) + \
|
| 420 |
+
1j*ss_diff(tmp.imag,a,b,period)
|
| 421 |
+
if period is not None:
|
| 422 |
+
a = a*2*pi/period
|
| 423 |
+
b = b*2*pi/period
|
| 424 |
+
n = len(x)
|
| 425 |
+
omega = _cache.get((n,a,b))
|
| 426 |
+
if omega is None:
|
| 427 |
+
if len(_cache) > 20:
|
| 428 |
+
while _cache:
|
| 429 |
+
_cache.popitem()
|
| 430 |
+
|
| 431 |
+
def kernel(k,a=a,b=b):
|
| 432 |
+
if k:
|
| 433 |
+
return sinh(a*k)/sinh(b*k)
|
| 434 |
+
return float(a)/b
|
| 435 |
+
omega = convolve.init_convolution_kernel(n,kernel)
|
| 436 |
+
_cache[(n,a,b)] = omega
|
| 437 |
+
overwrite_x = _datacopied(tmp, x)
|
| 438 |
+
return convolve.convolve(tmp,omega,overwrite_x=overwrite_x)
|
| 439 |
+
|
| 440 |
+
|
| 441 |
+
del _cache
|
| 442 |
+
|
| 443 |
+
|
| 444 |
+
_cache = {}
|
| 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 |
+
tmp = asarray(x)
|
| 477 |
+
if iscomplexobj(tmp):
|
| 478 |
+
return cc_diff(tmp.real,a,b,period) + \
|
| 479 |
+
1j*cc_diff(tmp.imag,a,b,period)
|
| 480 |
+
if period is not None:
|
| 481 |
+
a = a*2*pi/period
|
| 482 |
+
b = b*2*pi/period
|
| 483 |
+
n = len(x)
|
| 484 |
+
omega = _cache.get((n,a,b))
|
| 485 |
+
if omega is None:
|
| 486 |
+
if len(_cache) > 20:
|
| 487 |
+
while _cache:
|
| 488 |
+
_cache.popitem()
|
| 489 |
+
|
| 490 |
+
def kernel(k,a=a,b=b):
|
| 491 |
+
return cosh(a*k)/cosh(b*k)
|
| 492 |
+
omega = convolve.init_convolution_kernel(n,kernel)
|
| 493 |
+
_cache[(n,a,b)] = omega
|
| 494 |
+
overwrite_x = _datacopied(tmp, x)
|
| 495 |
+
return convolve.convolve(tmp,omega,overwrite_x=overwrite_x)
|
| 496 |
+
|
| 497 |
+
|
| 498 |
+
del _cache
|
| 499 |
+
|
| 500 |
+
|
| 501 |
+
_cache = {}
|
| 502 |
+
|
| 503 |
+
|
| 504 |
+
def shift(x, a, period=None, _cache=_cache):
|
| 505 |
+
"""
|
| 506 |
+
Shift periodic sequence x by a: y(u) = x(u+a).
|
| 507 |
+
|
| 508 |
+
If x_j and y_j are Fourier coefficients of periodic functions x
|
| 509 |
+
and y, respectively, then::
|
| 510 |
+
|
| 511 |
+
y_j = exp(j*a*2*pi/period*sqrt(-1)) * x_f
|
| 512 |
+
|
| 513 |
+
Parameters
|
| 514 |
+
----------
|
| 515 |
+
x : array_like
|
| 516 |
+
The array to take the pseudo-derivative from.
|
| 517 |
+
a : float
|
| 518 |
+
Defines the parameters of the sinh/sinh pseudo-differential
|
| 519 |
+
period : float, optional
|
| 520 |
+
The period of the sequences x and y. Default period is ``2*pi``.
|
| 521 |
+
"""
|
| 522 |
+
tmp = asarray(x)
|
| 523 |
+
if iscomplexobj(tmp):
|
| 524 |
+
return shift(tmp.real,a,period)+1j*shift(tmp.imag,a,period)
|
| 525 |
+
if period is not None:
|
| 526 |
+
a = a*2*pi/period
|
| 527 |
+
n = len(x)
|
| 528 |
+
omega = _cache.get((n,a))
|
| 529 |
+
if omega is None:
|
| 530 |
+
if len(_cache) > 20:
|
| 531 |
+
while _cache:
|
| 532 |
+
_cache.popitem()
|
| 533 |
+
|
| 534 |
+
def kernel_real(k,a=a):
|
| 535 |
+
return cos(a*k)
|
| 536 |
+
|
| 537 |
+
def kernel_imag(k,a=a):
|
| 538 |
+
return sin(a*k)
|
| 539 |
+
omega_real = convolve.init_convolution_kernel(n,kernel_real,d=0,
|
| 540 |
+
zero_nyquist=0)
|
| 541 |
+
omega_imag = convolve.init_convolution_kernel(n,kernel_imag,d=1,
|
| 542 |
+
zero_nyquist=0)
|
| 543 |
+
_cache[(n,a)] = omega_real,omega_imag
|
| 544 |
+
else:
|
| 545 |
+
omega_real,omega_imag = omega
|
| 546 |
+
overwrite_x = _datacopied(tmp, x)
|
| 547 |
+
return convolve.convolve_z(tmp,omega_real,omega_imag,
|
| 548 |
+
overwrite_x=overwrite_x)
|
| 549 |
+
|
| 550 |
+
|
| 551 |
+
del _cache
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/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)
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/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)
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/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)
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/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)
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/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)
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/tests/__init__.py
ADDED
|
File without changes
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/tests/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (217 Bytes). View file
|
|
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/tests/__pycache__/test_basic.cpython-310.pyc
ADDED
|
Binary file (28.1 kB). View file
|
|
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/tests/__pycache__/test_helper.cpython-310.pyc
ADDED
|
Binary file (2.26 kB). View file
|
|
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/tests/__pycache__/test_import.cpython-310.pyc
ADDED
|
Binary file (1.64 kB). View file
|
|
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/tests/__pycache__/test_pseudo_diffs.cpython-310.pyc
ADDED
|
Binary file (13.1 kB). View file
|
|
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/tests/__pycache__/test_real_transforms.cpython-310.pyc
ADDED
|
Binary file (25.9 kB). View file
|
|
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/tests/fftw_single_ref.npz
ADDED
|
Binary file (95.1 kB). View file
|
|
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/tests/test.npz
ADDED
|
Binary file (12 kB). View file
|
|
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/tests/test_basic.py
ADDED
|
@@ -0,0 +1,873 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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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,
|
| 354 |
+
err_msg="size=%d" % size)
|
| 355 |
+
assert_array_almost_equal(y2, x, decimal=self.ndec,
|
| 356 |
+
err_msg="size=%d" % size)
|
| 357 |
+
|
| 358 |
+
def test_size_accuracy(self):
|
| 359 |
+
# Sanity check for the accuracy for prime and non-prime sized inputs
|
| 360 |
+
if self.rdt == np.float32:
|
| 361 |
+
rtol = 1e-5
|
| 362 |
+
elif self.rdt == np.float64:
|
| 363 |
+
rtol = 1e-10
|
| 364 |
+
|
| 365 |
+
for size in LARGE_COMPOSITE_SIZES + LARGE_PRIME_SIZES:
|
| 366 |
+
np.random.seed(1234)
|
| 367 |
+
x = np.random.rand(size).astype(self.rdt)
|
| 368 |
+
y = irfft(rfft(x))
|
| 369 |
+
_assert_close_in_norm(x, y, rtol, size, self.rdt)
|
| 370 |
+
y = rfft(irfft(x))
|
| 371 |
+
_assert_close_in_norm(x, y, rtol, size, self.rdt)
|
| 372 |
+
|
| 373 |
+
def test_invalid_sizes(self):
|
| 374 |
+
assert_raises(ValueError, irfft, [])
|
| 375 |
+
assert_raises(ValueError, irfft, [[1,1],[2,2]], -5)
|
| 376 |
+
|
| 377 |
+
def test_complex_input(self):
|
| 378 |
+
assert_raises(TypeError, irfft, np.arange(4, dtype=np.complex64))
|
| 379 |
+
|
| 380 |
+
|
| 381 |
+
# self.ndec is bogus; we should have a assert_array_approx_equal for number of
|
| 382 |
+
# significant digits
|
| 383 |
+
|
| 384 |
+
class TestIRFFTDouble(_TestIRFFTBase):
|
| 385 |
+
def setup_method(self):
|
| 386 |
+
self.cdt = np.complex128
|
| 387 |
+
self.rdt = np.float64
|
| 388 |
+
self.ndec = 14
|
| 389 |
+
|
| 390 |
+
|
| 391 |
+
class TestIRFFTSingle(_TestIRFFTBase):
|
| 392 |
+
def setup_method(self):
|
| 393 |
+
self.cdt = np.complex64
|
| 394 |
+
self.rdt = np.float32
|
| 395 |
+
self.ndec = 5
|
| 396 |
+
|
| 397 |
+
|
| 398 |
+
class Testfft2:
|
| 399 |
+
def setup_method(self):
|
| 400 |
+
np.random.seed(1234)
|
| 401 |
+
|
| 402 |
+
def test_regression_244(self):
|
| 403 |
+
"""FFT returns wrong result with axes parameter."""
|
| 404 |
+
# fftn (and hence fft2) used to break when both axes and shape were
|
| 405 |
+
# used
|
| 406 |
+
x = numpy.ones((4, 4, 2))
|
| 407 |
+
y = fft2(x, shape=(8, 8), axes=(-3, -2))
|
| 408 |
+
y_r = numpy.fft.fftn(x, s=(8, 8), axes=(-3, -2))
|
| 409 |
+
assert_array_almost_equal(y, y_r)
|
| 410 |
+
|
| 411 |
+
def test_invalid_sizes(self):
|
| 412 |
+
assert_raises(ValueError, fft2, [[]])
|
| 413 |
+
assert_raises(ValueError, fft2, [[1, 1], [2, 2]], (4, -3))
|
| 414 |
+
|
| 415 |
+
|
| 416 |
+
class TestFftnSingle:
|
| 417 |
+
def setup_method(self):
|
| 418 |
+
np.random.seed(1234)
|
| 419 |
+
|
| 420 |
+
def test_definition(self):
|
| 421 |
+
x = [[1, 2, 3],
|
| 422 |
+
[4, 5, 6],
|
| 423 |
+
[7, 8, 9]]
|
| 424 |
+
y = fftn(np.array(x, np.float32))
|
| 425 |
+
assert_(y.dtype == np.complex64,
|
| 426 |
+
msg="double precision output with single precision")
|
| 427 |
+
|
| 428 |
+
y_r = np.array(fftn(x), np.complex64)
|
| 429 |
+
assert_array_almost_equal_nulp(y, y_r)
|
| 430 |
+
|
| 431 |
+
@pytest.mark.parametrize('size', SMALL_COMPOSITE_SIZES + SMALL_PRIME_SIZES)
|
| 432 |
+
def test_size_accuracy_small(self, size):
|
| 433 |
+
x = np.random.rand(size, size) + 1j*np.random.rand(size, size)
|
| 434 |
+
y1 = fftn(x.real.astype(np.float32))
|
| 435 |
+
y2 = fftn(x.real.astype(np.float64)).astype(np.complex64)
|
| 436 |
+
|
| 437 |
+
assert_equal(y1.dtype, np.complex64)
|
| 438 |
+
assert_array_almost_equal_nulp(y1, y2, 2000)
|
| 439 |
+
|
| 440 |
+
@pytest.mark.parametrize('size', LARGE_COMPOSITE_SIZES + LARGE_PRIME_SIZES)
|
| 441 |
+
def test_size_accuracy_large(self, size):
|
| 442 |
+
x = np.random.rand(size, 3) + 1j*np.random.rand(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 |
+
x = np.random.rand(size, size) + 1j*np.random.rand(size, size)
|
| 461 |
+
y1 = fftn(x.real.astype(np.float16))
|
| 462 |
+
y2 = fftn(x.real.astype(np.float64)).astype(np.complex64)
|
| 463 |
+
|
| 464 |
+
assert_equal(y1.dtype, np.complex64)
|
| 465 |
+
assert_array_almost_equal_nulp(y1, y2, 5e5)
|
| 466 |
+
|
| 467 |
+
@pytest.mark.parametrize('size', LARGE_COMPOSITE_SIZES + LARGE_PRIME_SIZES)
|
| 468 |
+
def test_float16_input_large(self, size):
|
| 469 |
+
x = np.random.rand(size, 3) + 1j*np.random.rand(size, 3)
|
| 470 |
+
y1 = fftn(x.real.astype(np.float16))
|
| 471 |
+
y2 = fftn(x.real.astype(np.float64)).astype(np.complex64)
|
| 472 |
+
|
| 473 |
+
assert_equal(y1.dtype, np.complex64)
|
| 474 |
+
assert_array_almost_equal_nulp(y1, y2, 2e6)
|
| 475 |
+
|
| 476 |
+
|
| 477 |
+
class TestFftn:
|
| 478 |
+
def setup_method(self):
|
| 479 |
+
np.random.seed(1234)
|
| 480 |
+
|
| 481 |
+
def test_definition(self):
|
| 482 |
+
x = [[1, 2, 3],
|
| 483 |
+
[4, 5, 6],
|
| 484 |
+
[7, 8, 9]]
|
| 485 |
+
y = fftn(x)
|
| 486 |
+
assert_array_almost_equal(y, direct_dftn(x))
|
| 487 |
+
|
| 488 |
+
x = random((20, 26))
|
| 489 |
+
assert_array_almost_equal(fftn(x), direct_dftn(x))
|
| 490 |
+
|
| 491 |
+
x = random((5, 4, 3, 20))
|
| 492 |
+
assert_array_almost_equal(fftn(x), direct_dftn(x))
|
| 493 |
+
|
| 494 |
+
def test_axes_argument(self):
|
| 495 |
+
# plane == ji_plane, x== kji_space
|
| 496 |
+
plane1 = [[1, 2, 3],
|
| 497 |
+
[4, 5, 6],
|
| 498 |
+
[7, 8, 9]]
|
| 499 |
+
plane2 = [[10, 11, 12],
|
| 500 |
+
[13, 14, 15],
|
| 501 |
+
[16, 17, 18]]
|
| 502 |
+
plane3 = [[19, 20, 21],
|
| 503 |
+
[22, 23, 24],
|
| 504 |
+
[25, 26, 27]]
|
| 505 |
+
ki_plane1 = [[1, 2, 3],
|
| 506 |
+
[10, 11, 12],
|
| 507 |
+
[19, 20, 21]]
|
| 508 |
+
ki_plane2 = [[4, 5, 6],
|
| 509 |
+
[13, 14, 15],
|
| 510 |
+
[22, 23, 24]]
|
| 511 |
+
ki_plane3 = [[7, 8, 9],
|
| 512 |
+
[16, 17, 18],
|
| 513 |
+
[25, 26, 27]]
|
| 514 |
+
jk_plane1 = [[1, 10, 19],
|
| 515 |
+
[4, 13, 22],
|
| 516 |
+
[7, 16, 25]]
|
| 517 |
+
jk_plane2 = [[2, 11, 20],
|
| 518 |
+
[5, 14, 23],
|
| 519 |
+
[8, 17, 26]]
|
| 520 |
+
jk_plane3 = [[3, 12, 21],
|
| 521 |
+
[6, 15, 24],
|
| 522 |
+
[9, 18, 27]]
|
| 523 |
+
kj_plane1 = [[1, 4, 7],
|
| 524 |
+
[10, 13, 16], [19, 22, 25]]
|
| 525 |
+
kj_plane2 = [[2, 5, 8],
|
| 526 |
+
[11, 14, 17], [20, 23, 26]]
|
| 527 |
+
kj_plane3 = [[3, 6, 9],
|
| 528 |
+
[12, 15, 18], [21, 24, 27]]
|
| 529 |
+
ij_plane1 = [[1, 4, 7],
|
| 530 |
+
[2, 5, 8],
|
| 531 |
+
[3, 6, 9]]
|
| 532 |
+
ij_plane2 = [[10, 13, 16],
|
| 533 |
+
[11, 14, 17],
|
| 534 |
+
[12, 15, 18]]
|
| 535 |
+
ij_plane3 = [[19, 22, 25],
|
| 536 |
+
[20, 23, 26],
|
| 537 |
+
[21, 24, 27]]
|
| 538 |
+
ik_plane1 = [[1, 10, 19],
|
| 539 |
+
[2, 11, 20],
|
| 540 |
+
[3, 12, 21]]
|
| 541 |
+
ik_plane2 = [[4, 13, 22],
|
| 542 |
+
[5, 14, 23],
|
| 543 |
+
[6, 15, 24]]
|
| 544 |
+
ik_plane3 = [[7, 16, 25],
|
| 545 |
+
[8, 17, 26],
|
| 546 |
+
[9, 18, 27]]
|
| 547 |
+
ijk_space = [jk_plane1, jk_plane2, jk_plane3]
|
| 548 |
+
ikj_space = [kj_plane1, kj_plane2, kj_plane3]
|
| 549 |
+
jik_space = [ik_plane1, ik_plane2, ik_plane3]
|
| 550 |
+
jki_space = [ki_plane1, ki_plane2, ki_plane3]
|
| 551 |
+
kij_space = [ij_plane1, ij_plane2, ij_plane3]
|
| 552 |
+
x = array([plane1, plane2, plane3])
|
| 553 |
+
|
| 554 |
+
assert_array_almost_equal(fftn(x),
|
| 555 |
+
fftn(x, axes=(-3, -2, -1))) # kji_space
|
| 556 |
+
assert_array_almost_equal(fftn(x), fftn(x, axes=(0, 1, 2)))
|
| 557 |
+
assert_array_almost_equal(fftn(x, axes=(0, 2)), fftn(x, axes=(0, -1)))
|
| 558 |
+
y = fftn(x, axes=(2, 1, 0)) # ijk_space
|
| 559 |
+
assert_array_almost_equal(swapaxes(y, -1, -3), fftn(ijk_space))
|
| 560 |
+
y = fftn(x, axes=(2, 0, 1)) # ikj_space
|
| 561 |
+
assert_array_almost_equal(swapaxes(swapaxes(y, -1, -3), -1, -2),
|
| 562 |
+
fftn(ikj_space))
|
| 563 |
+
y = fftn(x, axes=(1, 2, 0)) # jik_space
|
| 564 |
+
assert_array_almost_equal(swapaxes(swapaxes(y, -1, -3), -3, -2),
|
| 565 |
+
fftn(jik_space))
|
| 566 |
+
y = fftn(x, axes=(1, 0, 2)) # jki_space
|
| 567 |
+
assert_array_almost_equal(swapaxes(y, -2, -3), fftn(jki_space))
|
| 568 |
+
y = fftn(x, axes=(0, 2, 1)) # kij_space
|
| 569 |
+
assert_array_almost_equal(swapaxes(y, -2, -1), fftn(kij_space))
|
| 570 |
+
|
| 571 |
+
y = fftn(x, axes=(-2, -1)) # ji_plane
|
| 572 |
+
assert_array_almost_equal(fftn(plane1), y[0])
|
| 573 |
+
assert_array_almost_equal(fftn(plane2), y[1])
|
| 574 |
+
assert_array_almost_equal(fftn(plane3), y[2])
|
| 575 |
+
|
| 576 |
+
y = fftn(x, axes=(1, 2)) # ji_plane
|
| 577 |
+
assert_array_almost_equal(fftn(plane1), y[0])
|
| 578 |
+
assert_array_almost_equal(fftn(plane2), y[1])
|
| 579 |
+
assert_array_almost_equal(fftn(plane3), y[2])
|
| 580 |
+
|
| 581 |
+
y = fftn(x, axes=(-3, -2)) # kj_plane
|
| 582 |
+
assert_array_almost_equal(fftn(x[:, :, 0]), y[:, :, 0])
|
| 583 |
+
assert_array_almost_equal(fftn(x[:, :, 1]), y[:, :, 1])
|
| 584 |
+
assert_array_almost_equal(fftn(x[:, :, 2]), y[:, :, 2])
|
| 585 |
+
|
| 586 |
+
y = fftn(x, axes=(-3, -1)) # ki_plane
|
| 587 |
+
assert_array_almost_equal(fftn(x[:, 0, :]), y[:, 0, :])
|
| 588 |
+
assert_array_almost_equal(fftn(x[:, 1, :]), y[:, 1, :])
|
| 589 |
+
assert_array_almost_equal(fftn(x[:, 2, :]), y[:, 2, :])
|
| 590 |
+
|
| 591 |
+
y = fftn(x, axes=(-1, -2)) # ij_plane
|
| 592 |
+
assert_array_almost_equal(fftn(ij_plane1), swapaxes(y[0], -2, -1))
|
| 593 |
+
assert_array_almost_equal(fftn(ij_plane2), swapaxes(y[1], -2, -1))
|
| 594 |
+
assert_array_almost_equal(fftn(ij_plane3), swapaxes(y[2], -2, -1))
|
| 595 |
+
|
| 596 |
+
y = fftn(x, axes=(-1, -3)) # ik_plane
|
| 597 |
+
assert_array_almost_equal(fftn(ik_plane1),
|
| 598 |
+
swapaxes(y[:, 0, :], -1, -2))
|
| 599 |
+
assert_array_almost_equal(fftn(ik_plane2),
|
| 600 |
+
swapaxes(y[:, 1, :], -1, -2))
|
| 601 |
+
assert_array_almost_equal(fftn(ik_plane3),
|
| 602 |
+
swapaxes(y[:, 2, :], -1, -2))
|
| 603 |
+
|
| 604 |
+
y = fftn(x, axes=(-2, -3)) # jk_plane
|
| 605 |
+
assert_array_almost_equal(fftn(jk_plane1),
|
| 606 |
+
swapaxes(y[:, :, 0], -1, -2))
|
| 607 |
+
assert_array_almost_equal(fftn(jk_plane2),
|
| 608 |
+
swapaxes(y[:, :, 1], -1, -2))
|
| 609 |
+
assert_array_almost_equal(fftn(jk_plane3),
|
| 610 |
+
swapaxes(y[:, :, 2], -1, -2))
|
| 611 |
+
|
| 612 |
+
y = fftn(x, axes=(-1,)) # i_line
|
| 613 |
+
for i in range(3):
|
| 614 |
+
for j in range(3):
|
| 615 |
+
assert_array_almost_equal(fft(x[i, j, :]), y[i, j, :])
|
| 616 |
+
y = fftn(x, axes=(-2,)) # j_line
|
| 617 |
+
for i in range(3):
|
| 618 |
+
for j in range(3):
|
| 619 |
+
assert_array_almost_equal(fft(x[i, :, j]), y[i, :, j])
|
| 620 |
+
y = fftn(x, axes=(0,)) # k_line
|
| 621 |
+
for i in range(3):
|
| 622 |
+
for j in range(3):
|
| 623 |
+
assert_array_almost_equal(fft(x[:, i, j]), y[:, i, j])
|
| 624 |
+
|
| 625 |
+
y = fftn(x, axes=()) # point
|
| 626 |
+
assert_array_almost_equal(y, x)
|
| 627 |
+
|
| 628 |
+
def test_shape_argument(self):
|
| 629 |
+
small_x = [[1, 2, 3],
|
| 630 |
+
[4, 5, 6]]
|
| 631 |
+
large_x1 = [[1, 2, 3, 0],
|
| 632 |
+
[4, 5, 6, 0],
|
| 633 |
+
[0, 0, 0, 0],
|
| 634 |
+
[0, 0, 0, 0]]
|
| 635 |
+
|
| 636 |
+
y = fftn(small_x, shape=(4, 4))
|
| 637 |
+
assert_array_almost_equal(y, fftn(large_x1))
|
| 638 |
+
|
| 639 |
+
y = fftn(small_x, shape=(3, 4))
|
| 640 |
+
assert_array_almost_equal(y, fftn(large_x1[:-1]))
|
| 641 |
+
|
| 642 |
+
def test_shape_axes_argument(self):
|
| 643 |
+
small_x = [[1, 2, 3],
|
| 644 |
+
[4, 5, 6],
|
| 645 |
+
[7, 8, 9]]
|
| 646 |
+
large_x1 = array([[1, 2, 3, 0],
|
| 647 |
+
[4, 5, 6, 0],
|
| 648 |
+
[7, 8, 9, 0],
|
| 649 |
+
[0, 0, 0, 0]])
|
| 650 |
+
y = fftn(small_x, shape=(4, 4), axes=(-2, -1))
|
| 651 |
+
assert_array_almost_equal(y, fftn(large_x1))
|
| 652 |
+
y = fftn(small_x, shape=(4, 4), axes=(-1, -2))
|
| 653 |
+
|
| 654 |
+
assert_array_almost_equal(y, swapaxes(
|
| 655 |
+
fftn(swapaxes(large_x1, -1, -2)), -1, -2))
|
| 656 |
+
|
| 657 |
+
def test_shape_axes_argument2(self):
|
| 658 |
+
# Change shape of the last axis
|
| 659 |
+
x = numpy.random.random((10, 5, 3, 7))
|
| 660 |
+
y = fftn(x, axes=(-1,), shape=(8,))
|
| 661 |
+
assert_array_almost_equal(y, fft(x, axis=-1, n=8))
|
| 662 |
+
|
| 663 |
+
# Change shape of an arbitrary axis which is not the last one
|
| 664 |
+
x = numpy.random.random((10, 5, 3, 7))
|
| 665 |
+
y = fftn(x, axes=(-2,), shape=(8,))
|
| 666 |
+
assert_array_almost_equal(y, fft(x, axis=-2, n=8))
|
| 667 |
+
|
| 668 |
+
# Change shape of axes: cf #244, where shape and axes were mixed up
|
| 669 |
+
x = numpy.random.random((4, 4, 2))
|
| 670 |
+
y = fftn(x, axes=(-3, -2), shape=(8, 8))
|
| 671 |
+
assert_array_almost_equal(y,
|
| 672 |
+
numpy.fft.fftn(x, axes=(-3, -2), s=(8, 8)))
|
| 673 |
+
|
| 674 |
+
def test_shape_argument_more(self):
|
| 675 |
+
x = zeros((4, 4, 2))
|
| 676 |
+
with assert_raises(ValueError,
|
| 677 |
+
match="when given, axes and shape arguments"
|
| 678 |
+
" have to be of the same length"):
|
| 679 |
+
fftn(x, shape=(8, 8, 2, 1))
|
| 680 |
+
|
| 681 |
+
def test_invalid_sizes(self):
|
| 682 |
+
with assert_raises(ValueError,
|
| 683 |
+
match="invalid number of data points"
|
| 684 |
+
r" \(\[1, 0\]\) specified"):
|
| 685 |
+
fftn([[]])
|
| 686 |
+
|
| 687 |
+
with assert_raises(ValueError,
|
| 688 |
+
match="invalid number of data points"
|
| 689 |
+
r" \(\[4, -3\]\) specified"):
|
| 690 |
+
fftn([[1, 1], [2, 2]], (4, -3))
|
| 691 |
+
|
| 692 |
+
|
| 693 |
+
class TestIfftn:
|
| 694 |
+
dtype = None
|
| 695 |
+
cdtype = None
|
| 696 |
+
|
| 697 |
+
def setup_method(self):
|
| 698 |
+
np.random.seed(1234)
|
| 699 |
+
|
| 700 |
+
@pytest.mark.parametrize('dtype,cdtype,maxnlp',
|
| 701 |
+
[(np.float64, np.complex128, 2000),
|
| 702 |
+
(np.float32, np.complex64, 3500)])
|
| 703 |
+
def test_definition(self, dtype, cdtype, maxnlp):
|
| 704 |
+
x = np.array([[1, 2, 3],
|
| 705 |
+
[4, 5, 6],
|
| 706 |
+
[7, 8, 9]], dtype=dtype)
|
| 707 |
+
y = ifftn(x)
|
| 708 |
+
assert_equal(y.dtype, cdtype)
|
| 709 |
+
assert_array_almost_equal_nulp(y, direct_idftn(x), maxnlp)
|
| 710 |
+
|
| 711 |
+
x = random((20, 26))
|
| 712 |
+
assert_array_almost_equal_nulp(ifftn(x), direct_idftn(x), maxnlp)
|
| 713 |
+
|
| 714 |
+
x = random((5, 4, 3, 20))
|
| 715 |
+
assert_array_almost_equal_nulp(ifftn(x), direct_idftn(x), maxnlp)
|
| 716 |
+
|
| 717 |
+
@pytest.mark.parametrize('maxnlp', [2000, 3500])
|
| 718 |
+
@pytest.mark.parametrize('size', [1, 2, 51, 32, 64, 92])
|
| 719 |
+
def test_random_complex(self, maxnlp, size):
|
| 720 |
+
x = random([size, size]) + 1j*random([size, size])
|
| 721 |
+
assert_array_almost_equal_nulp(ifftn(fftn(x)), x, maxnlp)
|
| 722 |
+
assert_array_almost_equal_nulp(fftn(ifftn(x)), x, maxnlp)
|
| 723 |
+
|
| 724 |
+
def test_invalid_sizes(self):
|
| 725 |
+
with assert_raises(ValueError,
|
| 726 |
+
match="invalid number of data points"
|
| 727 |
+
r" \(\[1, 0\]\) specified"):
|
| 728 |
+
ifftn([[]])
|
| 729 |
+
|
| 730 |
+
with assert_raises(ValueError,
|
| 731 |
+
match="invalid number of data points"
|
| 732 |
+
r" \(\[4, -3\]\) specified"):
|
| 733 |
+
ifftn([[1, 1], [2, 2]], (4, -3))
|
| 734 |
+
|
| 735 |
+
|
| 736 |
+
class FakeArray:
|
| 737 |
+
def __init__(self, data):
|
| 738 |
+
self._data = data
|
| 739 |
+
self.__array_interface__ = data.__array_interface__
|
| 740 |
+
|
| 741 |
+
|
| 742 |
+
class FakeArray2:
|
| 743 |
+
def __init__(self, data):
|
| 744 |
+
self._data = data
|
| 745 |
+
|
| 746 |
+
def __array__(self, dtype=None, copy=None):
|
| 747 |
+
return self._data
|
| 748 |
+
|
| 749 |
+
|
| 750 |
+
class TestOverwrite:
|
| 751 |
+
"""Check input overwrite behavior of the FFT functions."""
|
| 752 |
+
|
| 753 |
+
real_dtypes = (np.float32, np.float64)
|
| 754 |
+
dtypes = real_dtypes + (np.complex64, np.complex128)
|
| 755 |
+
fftsizes = [8, 16, 32]
|
| 756 |
+
|
| 757 |
+
def _check(self, x, routine, fftsize, axis, overwrite_x):
|
| 758 |
+
x2 = x.copy()
|
| 759 |
+
for fake in [lambda x: x, FakeArray, FakeArray2]:
|
| 760 |
+
routine(fake(x2), fftsize, axis, overwrite_x=overwrite_x)
|
| 761 |
+
|
| 762 |
+
sig = "{}({}{!r}, {!r}, axis={!r}, overwrite_x={!r})".format(
|
| 763 |
+
routine.__name__, x.dtype, x.shape, fftsize, axis, overwrite_x)
|
| 764 |
+
if not overwrite_x:
|
| 765 |
+
assert_equal(x2, x, err_msg="spurious overwrite in %s" % sig)
|
| 766 |
+
|
| 767 |
+
def _check_1d(self, routine, dtype, shape, axis, overwritable_dtypes,
|
| 768 |
+
fftsize, overwrite_x):
|
| 769 |
+
np.random.seed(1234)
|
| 770 |
+
if np.issubdtype(dtype, np.complexfloating):
|
| 771 |
+
data = np.random.randn(*shape) + 1j*np.random.randn(*shape)
|
| 772 |
+
else:
|
| 773 |
+
data = np.random.randn(*shape)
|
| 774 |
+
data = data.astype(dtype)
|
| 775 |
+
|
| 776 |
+
self._check(data, routine, fftsize, axis,
|
| 777 |
+
overwrite_x=overwrite_x)
|
| 778 |
+
|
| 779 |
+
@pytest.mark.parametrize('dtype', dtypes)
|
| 780 |
+
@pytest.mark.parametrize('fftsize', fftsizes)
|
| 781 |
+
@pytest.mark.parametrize('overwrite_x', [True, False])
|
| 782 |
+
@pytest.mark.parametrize('shape,axes', [((16,), -1),
|
| 783 |
+
((16, 2), 0),
|
| 784 |
+
((2, 16), 1)])
|
| 785 |
+
def test_fft_ifft(self, dtype, fftsize, overwrite_x, shape, axes):
|
| 786 |
+
overwritable = (np.complex128, np.complex64)
|
| 787 |
+
self._check_1d(fft, dtype, shape, axes, overwritable,
|
| 788 |
+
fftsize, overwrite_x)
|
| 789 |
+
self._check_1d(ifft, dtype, shape, axes, overwritable,
|
| 790 |
+
fftsize, overwrite_x)
|
| 791 |
+
|
| 792 |
+
@pytest.mark.parametrize('dtype', real_dtypes)
|
| 793 |
+
@pytest.mark.parametrize('fftsize', fftsizes)
|
| 794 |
+
@pytest.mark.parametrize('overwrite_x', [True, False])
|
| 795 |
+
@pytest.mark.parametrize('shape,axes', [((16,), -1),
|
| 796 |
+
((16, 2), 0),
|
| 797 |
+
((2, 16), 1)])
|
| 798 |
+
def test_rfft_irfft(self, dtype, fftsize, overwrite_x, shape, axes):
|
| 799 |
+
overwritable = self.real_dtypes
|
| 800 |
+
self._check_1d(irfft, dtype, shape, axes, overwritable,
|
| 801 |
+
fftsize, overwrite_x)
|
| 802 |
+
self._check_1d(rfft, dtype, shape, axes, overwritable,
|
| 803 |
+
fftsize, overwrite_x)
|
| 804 |
+
|
| 805 |
+
def _check_nd_one(self, routine, dtype, shape, axes, overwritable_dtypes,
|
| 806 |
+
overwrite_x):
|
| 807 |
+
np.random.seed(1234)
|
| 808 |
+
if np.issubdtype(dtype, np.complexfloating):
|
| 809 |
+
data = np.random.randn(*shape) + 1j*np.random.randn(*shape)
|
| 810 |
+
else:
|
| 811 |
+
data = np.random.randn(*shape)
|
| 812 |
+
data = data.astype(dtype)
|
| 813 |
+
|
| 814 |
+
def fftshape_iter(shp):
|
| 815 |
+
if len(shp) <= 0:
|
| 816 |
+
yield ()
|
| 817 |
+
else:
|
| 818 |
+
for j in (shp[0]//2, shp[0], shp[0]*2):
|
| 819 |
+
for rest in fftshape_iter(shp[1:]):
|
| 820 |
+
yield (j,) + rest
|
| 821 |
+
|
| 822 |
+
if axes is None:
|
| 823 |
+
part_shape = shape
|
| 824 |
+
else:
|
| 825 |
+
part_shape = tuple(np.take(shape, axes))
|
| 826 |
+
|
| 827 |
+
for fftshape in fftshape_iter(part_shape):
|
| 828 |
+
self._check(data, routine, fftshape, axes,
|
| 829 |
+
overwrite_x=overwrite_x)
|
| 830 |
+
if data.ndim > 1:
|
| 831 |
+
self._check(data.T, routine, fftshape, axes,
|
| 832 |
+
overwrite_x=overwrite_x)
|
| 833 |
+
|
| 834 |
+
@pytest.mark.parametrize('dtype', dtypes)
|
| 835 |
+
@pytest.mark.parametrize('overwrite_x', [True, False])
|
| 836 |
+
@pytest.mark.parametrize('shape,axes', [((16,), None),
|
| 837 |
+
((16,), (0,)),
|
| 838 |
+
((16, 2), (0,)),
|
| 839 |
+
((2, 16), (1,)),
|
| 840 |
+
((8, 16), None),
|
| 841 |
+
((8, 16), (0, 1)),
|
| 842 |
+
((8, 16, 2), (0, 1)),
|
| 843 |
+
((8, 16, 2), (1, 2)),
|
| 844 |
+
((8, 16, 2), (0,)),
|
| 845 |
+
((8, 16, 2), (1,)),
|
| 846 |
+
((8, 16, 2), (2,)),
|
| 847 |
+
((8, 16, 2), None),
|
| 848 |
+
((8, 16, 2), (0, 1, 2))])
|
| 849 |
+
def test_fftn_ifftn(self, dtype, overwrite_x, shape, axes):
|
| 850 |
+
overwritable = (np.complex128, np.complex64)
|
| 851 |
+
self._check_nd_one(fftn, dtype, shape, axes, overwritable,
|
| 852 |
+
overwrite_x)
|
| 853 |
+
self._check_nd_one(ifftn, dtype, shape, axes, overwritable,
|
| 854 |
+
overwrite_x)
|
| 855 |
+
|
| 856 |
+
|
| 857 |
+
@pytest.mark.parametrize('func', [fftn, ifftn, fft2])
|
| 858 |
+
def test_shape_axes_ndarray(func):
|
| 859 |
+
# Test fftn and ifftn work with NumPy arrays for shape and axes arguments
|
| 860 |
+
# Regression test for gh-13342
|
| 861 |
+
a = np.random.rand(10, 10)
|
| 862 |
+
|
| 863 |
+
expect = func(a, shape=(5, 5))
|
| 864 |
+
actual = func(a, shape=np.array([5, 5]))
|
| 865 |
+
assert_equal(expect, actual)
|
| 866 |
+
|
| 867 |
+
expect = func(a, axes=(-1,))
|
| 868 |
+
actual = func(a, axes=np.array([-1,]))
|
| 869 |
+
assert_equal(expect, actual)
|
| 870 |
+
|
| 871 |
+
expect = func(a, shape=(4, 7), axes=(1, 0))
|
| 872 |
+
actual = func(a, shape=np.array([4, 7]), axes=np.array([1, 0]))
|
| 873 |
+
assert_equal(expect, actual)
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/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)
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/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")
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/tests/test_pseudo_diffs.py
ADDED
|
@@ -0,0 +1,380 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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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 |
+
for k in [0,2,4,6]:
|
| 158 |
+
for n in [60,32,64,56,55]:
|
| 159 |
+
f = random((n,))
|
| 160 |
+
af = sum(f,axis=0)/n
|
| 161 |
+
f = f-af
|
| 162 |
+
# zeroing Nyquist mode:
|
| 163 |
+
f = diff(diff(f,1),-1)
|
| 164 |
+
assert_almost_equal(sum(f,axis=0),0.0)
|
| 165 |
+
assert_array_almost_equal(diff(diff(f,k),-k),f)
|
| 166 |
+
assert_array_almost_equal(diff(diff(f,-k),k),f)
|
| 167 |
+
|
| 168 |
+
def test_random_odd(self):
|
| 169 |
+
for k in [0,1,2,3,4,5,6]:
|
| 170 |
+
for n in [33,65,55]:
|
| 171 |
+
f = random((n,))
|
| 172 |
+
af = sum(f,axis=0)/n
|
| 173 |
+
f = f-af
|
| 174 |
+
assert_almost_equal(sum(f,axis=0),0.0)
|
| 175 |
+
assert_array_almost_equal(diff(diff(f,k),-k),f)
|
| 176 |
+
assert_array_almost_equal(diff(diff(f,-k),k),f)
|
| 177 |
+
|
| 178 |
+
def test_zero_nyquist(self):
|
| 179 |
+
for k in [0,1,2,3,4,5,6]:
|
| 180 |
+
for n in [32,33,64,56,55]:
|
| 181 |
+
f = random((n,))
|
| 182 |
+
af = sum(f,axis=0)/n
|
| 183 |
+
f = f-af
|
| 184 |
+
# zeroing Nyquist mode:
|
| 185 |
+
f = diff(diff(f,1),-1)
|
| 186 |
+
assert_almost_equal(sum(f,axis=0),0.0)
|
| 187 |
+
assert_array_almost_equal(diff(diff(f,k),-k),f)
|
| 188 |
+
assert_array_almost_equal(diff(diff(f,-k),k),f)
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
class TestTilbert:
|
| 192 |
+
|
| 193 |
+
def test_definition(self):
|
| 194 |
+
for h in [0.1,0.5,1,5.5,10]:
|
| 195 |
+
for n in [16,17,64,127]:
|
| 196 |
+
x = arange(n)*2*pi/n
|
| 197 |
+
y = tilbert(sin(x),h)
|
| 198 |
+
y1 = direct_tilbert(sin(x),h)
|
| 199 |
+
assert_array_almost_equal(y,y1)
|
| 200 |
+
assert_array_almost_equal(tilbert(sin(x),h),
|
| 201 |
+
direct_tilbert(sin(x),h))
|
| 202 |
+
assert_array_almost_equal(tilbert(sin(2*x),h),
|
| 203 |
+
direct_tilbert(sin(2*x),h))
|
| 204 |
+
|
| 205 |
+
def test_random_even(self):
|
| 206 |
+
for h in [0.1,0.5,1,5.5,10]:
|
| 207 |
+
for n in [32,64,56]:
|
| 208 |
+
f = random((n,))
|
| 209 |
+
af = sum(f,axis=0)/n
|
| 210 |
+
f = f-af
|
| 211 |
+
assert_almost_equal(sum(f,axis=0),0.0)
|
| 212 |
+
assert_array_almost_equal(direct_tilbert(direct_itilbert(f,h),h),f)
|
| 213 |
+
|
| 214 |
+
def test_random_odd(self):
|
| 215 |
+
for h in [0.1,0.5,1,5.5,10]:
|
| 216 |
+
for n in [33,65,55]:
|
| 217 |
+
f = random((n,))
|
| 218 |
+
af = sum(f,axis=0)/n
|
| 219 |
+
f = f-af
|
| 220 |
+
assert_almost_equal(sum(f,axis=0),0.0)
|
| 221 |
+
assert_array_almost_equal(itilbert(tilbert(f,h),h),f)
|
| 222 |
+
assert_array_almost_equal(tilbert(itilbert(f,h),h),f)
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
class TestITilbert:
|
| 226 |
+
|
| 227 |
+
def test_definition(self):
|
| 228 |
+
for h in [0.1,0.5,1,5.5,10]:
|
| 229 |
+
for n in [16,17,64,127]:
|
| 230 |
+
x = arange(n)*2*pi/n
|
| 231 |
+
y = itilbert(sin(x),h)
|
| 232 |
+
y1 = direct_itilbert(sin(x),h)
|
| 233 |
+
assert_array_almost_equal(y,y1)
|
| 234 |
+
assert_array_almost_equal(itilbert(sin(x),h),
|
| 235 |
+
direct_itilbert(sin(x),h))
|
| 236 |
+
assert_array_almost_equal(itilbert(sin(2*x),h),
|
| 237 |
+
direct_itilbert(sin(2*x),h))
|
| 238 |
+
|
| 239 |
+
|
| 240 |
+
class TestHilbert:
|
| 241 |
+
|
| 242 |
+
def test_definition(self):
|
| 243 |
+
for n in [16,17,64,127]:
|
| 244 |
+
x = arange(n)*2*pi/n
|
| 245 |
+
y = hilbert(sin(x))
|
| 246 |
+
y1 = direct_hilbert(sin(x))
|
| 247 |
+
assert_array_almost_equal(y,y1)
|
| 248 |
+
assert_array_almost_equal(hilbert(sin(2*x)),
|
| 249 |
+
direct_hilbert(sin(2*x)))
|
| 250 |
+
|
| 251 |
+
def test_tilbert_relation(self):
|
| 252 |
+
for n in [16,17,64,127]:
|
| 253 |
+
x = arange(n)*2*pi/n
|
| 254 |
+
f = sin(x)+cos(2*x)*sin(x)
|
| 255 |
+
y = hilbert(f)
|
| 256 |
+
y1 = direct_hilbert(f)
|
| 257 |
+
assert_array_almost_equal(y,y1)
|
| 258 |
+
y2 = tilbert(f,h=10)
|
| 259 |
+
assert_array_almost_equal(y,y2)
|
| 260 |
+
|
| 261 |
+
def test_random_odd(self):
|
| 262 |
+
for n in [33,65,55]:
|
| 263 |
+
f = random((n,))
|
| 264 |
+
af = sum(f,axis=0)/n
|
| 265 |
+
f = f-af
|
| 266 |
+
assert_almost_equal(sum(f,axis=0),0.0)
|
| 267 |
+
assert_array_almost_equal(ihilbert(hilbert(f)),f)
|
| 268 |
+
assert_array_almost_equal(hilbert(ihilbert(f)),f)
|
| 269 |
+
|
| 270 |
+
def test_random_even(self):
|
| 271 |
+
for n in [32,64,56]:
|
| 272 |
+
f = random((n,))
|
| 273 |
+
af = sum(f,axis=0)/n
|
| 274 |
+
f = f-af
|
| 275 |
+
# zeroing Nyquist mode:
|
| 276 |
+
f = diff(diff(f,1),-1)
|
| 277 |
+
assert_almost_equal(sum(f,axis=0),0.0)
|
| 278 |
+
assert_array_almost_equal(direct_hilbert(direct_ihilbert(f)),f)
|
| 279 |
+
assert_array_almost_equal(hilbert(ihilbert(f)),f)
|
| 280 |
+
|
| 281 |
+
|
| 282 |
+
class TestIHilbert:
|
| 283 |
+
|
| 284 |
+
def test_definition(self):
|
| 285 |
+
for n in [16,17,64,127]:
|
| 286 |
+
x = arange(n)*2*pi/n
|
| 287 |
+
y = ihilbert(sin(x))
|
| 288 |
+
y1 = direct_ihilbert(sin(x))
|
| 289 |
+
assert_array_almost_equal(y,y1)
|
| 290 |
+
assert_array_almost_equal(ihilbert(sin(2*x)),
|
| 291 |
+
direct_ihilbert(sin(2*x)))
|
| 292 |
+
|
| 293 |
+
def test_itilbert_relation(self):
|
| 294 |
+
for n in [16,17,64,127]:
|
| 295 |
+
x = arange(n)*2*pi/n
|
| 296 |
+
f = sin(x)+cos(2*x)*sin(x)
|
| 297 |
+
y = ihilbert(f)
|
| 298 |
+
y1 = direct_ihilbert(f)
|
| 299 |
+
assert_array_almost_equal(y,y1)
|
| 300 |
+
y2 = itilbert(f,h=10)
|
| 301 |
+
assert_array_almost_equal(y,y2)
|
| 302 |
+
|
| 303 |
+
|
| 304 |
+
class TestShift:
|
| 305 |
+
|
| 306 |
+
def test_definition(self):
|
| 307 |
+
for n in [18,17,64,127,32,2048,256]:
|
| 308 |
+
x = arange(n)*2*pi/n
|
| 309 |
+
for a in [0.1,3]:
|
| 310 |
+
assert_array_almost_equal(shift(sin(x),a),direct_shift(sin(x),a))
|
| 311 |
+
assert_array_almost_equal(shift(sin(x),a),sin(x+a))
|
| 312 |
+
assert_array_almost_equal(shift(cos(x),a),cos(x+a))
|
| 313 |
+
assert_array_almost_equal(shift(cos(2*x)+sin(x),a),
|
| 314 |
+
cos(2*(x+a))+sin(x+a))
|
| 315 |
+
assert_array_almost_equal(shift(exp(sin(x)),a),exp(sin(x+a)))
|
| 316 |
+
assert_array_almost_equal(shift(sin(x),2*pi),sin(x))
|
| 317 |
+
assert_array_almost_equal(shift(sin(x),pi),-sin(x))
|
| 318 |
+
assert_array_almost_equal(shift(sin(x),pi/2),cos(x))
|
| 319 |
+
|
| 320 |
+
|
| 321 |
+
class TestOverwrite:
|
| 322 |
+
"""Check input overwrite behavior """
|
| 323 |
+
|
| 324 |
+
real_dtypes = (np.float32, np.float64)
|
| 325 |
+
dtypes = real_dtypes + (np.complex64, np.complex128)
|
| 326 |
+
|
| 327 |
+
def _check(self, x, routine, *args, **kwargs):
|
| 328 |
+
x2 = x.copy()
|
| 329 |
+
routine(x2, *args, **kwargs)
|
| 330 |
+
sig = routine.__name__
|
| 331 |
+
if args:
|
| 332 |
+
sig += repr(args)
|
| 333 |
+
if kwargs:
|
| 334 |
+
sig += repr(kwargs)
|
| 335 |
+
assert_equal(x2, x, err_msg="spurious overwrite in %s" % sig)
|
| 336 |
+
|
| 337 |
+
def _check_1d(self, routine, dtype, shape, *args, **kwargs):
|
| 338 |
+
np.random.seed(1234)
|
| 339 |
+
if np.issubdtype(dtype, np.complexfloating):
|
| 340 |
+
data = np.random.randn(*shape) + 1j*np.random.randn(*shape)
|
| 341 |
+
else:
|
| 342 |
+
data = np.random.randn(*shape)
|
| 343 |
+
data = data.astype(dtype)
|
| 344 |
+
self._check(data, routine, *args, **kwargs)
|
| 345 |
+
|
| 346 |
+
def test_diff(self):
|
| 347 |
+
for dtype in self.dtypes:
|
| 348 |
+
self._check_1d(diff, dtype, (16,))
|
| 349 |
+
|
| 350 |
+
def test_tilbert(self):
|
| 351 |
+
for dtype in self.dtypes:
|
| 352 |
+
self._check_1d(tilbert, dtype, (16,), 1.6)
|
| 353 |
+
|
| 354 |
+
def test_itilbert(self):
|
| 355 |
+
for dtype in self.dtypes:
|
| 356 |
+
self._check_1d(itilbert, dtype, (16,), 1.6)
|
| 357 |
+
|
| 358 |
+
def test_hilbert(self):
|
| 359 |
+
for dtype in self.dtypes:
|
| 360 |
+
self._check_1d(hilbert, dtype, (16,))
|
| 361 |
+
|
| 362 |
+
def test_cs_diff(self):
|
| 363 |
+
for dtype in self.dtypes:
|
| 364 |
+
self._check_1d(cs_diff, dtype, (16,), 1.0, 4.0)
|
| 365 |
+
|
| 366 |
+
def test_sc_diff(self):
|
| 367 |
+
for dtype in self.dtypes:
|
| 368 |
+
self._check_1d(sc_diff, dtype, (16,), 1.0, 4.0)
|
| 369 |
+
|
| 370 |
+
def test_ss_diff(self):
|
| 371 |
+
for dtype in self.dtypes:
|
| 372 |
+
self._check_1d(ss_diff, dtype, (16,), 1.0, 4.0)
|
| 373 |
+
|
| 374 |
+
def test_cc_diff(self):
|
| 375 |
+
for dtype in self.dtypes:
|
| 376 |
+
self._check_1d(cc_diff, dtype, (16,), 1.0, 4.0)
|
| 377 |
+
|
| 378 |
+
def test_shift(self):
|
| 379 |
+
for dtype in self.dtypes:
|
| 380 |
+
self._check_1d(shift, dtype, (16,), 1.0)
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/fftpack/tests/test_real_transforms.py
ADDED
|
@@ -0,0 +1,815 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from os.path import join, dirname
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
from numpy.testing import assert_array_almost_equal, assert_equal
|
| 5 |
+
import pytest
|
| 6 |
+
from pytest import raises as assert_raises
|
| 7 |
+
|
| 8 |
+
from scipy.fftpack._realtransforms import (
|
| 9 |
+
dct, idct, dst, idst, dctn, idctn, dstn, idstn)
|
| 10 |
+
|
| 11 |
+
# Matlab reference data
|
| 12 |
+
MDATA = np.load(join(dirname(__file__), 'test.npz'))
|
| 13 |
+
X = [MDATA['x%d' % i] for i in range(8)]
|
| 14 |
+
Y = [MDATA['y%d' % i] for i in range(8)]
|
| 15 |
+
|
| 16 |
+
# FFTW reference data: the data are organized as follows:
|
| 17 |
+
# * SIZES is an array containing all available sizes
|
| 18 |
+
# * for every type (1, 2, 3, 4) and every size, the array dct_type_size
|
| 19 |
+
# contains the output of the DCT applied to the input np.linspace(0, size-1,
|
| 20 |
+
# size)
|
| 21 |
+
FFTWDATA_DOUBLE = np.load(join(dirname(__file__), 'fftw_double_ref.npz'))
|
| 22 |
+
FFTWDATA_SINGLE = np.load(join(dirname(__file__), 'fftw_single_ref.npz'))
|
| 23 |
+
FFTWDATA_SIZES = FFTWDATA_DOUBLE['sizes']
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def fftw_dct_ref(type, size, dt):
|
| 27 |
+
x = np.linspace(0, size-1, size).astype(dt)
|
| 28 |
+
dt = np.result_type(np.float32, dt)
|
| 29 |
+
if dt == np.float64:
|
| 30 |
+
data = FFTWDATA_DOUBLE
|
| 31 |
+
elif dt == np.float32:
|
| 32 |
+
data = FFTWDATA_SINGLE
|
| 33 |
+
else:
|
| 34 |
+
raise ValueError()
|
| 35 |
+
y = (data['dct_%d_%d' % (type, size)]).astype(dt)
|
| 36 |
+
return x, y, dt
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def fftw_dst_ref(type, size, dt):
|
| 40 |
+
x = np.linspace(0, size-1, size).astype(dt)
|
| 41 |
+
dt = np.result_type(np.float32, dt)
|
| 42 |
+
if dt == np.float64:
|
| 43 |
+
data = FFTWDATA_DOUBLE
|
| 44 |
+
elif dt == np.float32:
|
| 45 |
+
data = FFTWDATA_SINGLE
|
| 46 |
+
else:
|
| 47 |
+
raise ValueError()
|
| 48 |
+
y = (data['dst_%d_%d' % (type, size)]).astype(dt)
|
| 49 |
+
return x, y, dt
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def dct_2d_ref(x, **kwargs):
|
| 53 |
+
"""Calculate reference values for testing dct2."""
|
| 54 |
+
x = np.array(x, copy=True)
|
| 55 |
+
for row in range(x.shape[0]):
|
| 56 |
+
x[row, :] = dct(x[row, :], **kwargs)
|
| 57 |
+
for col in range(x.shape[1]):
|
| 58 |
+
x[:, col] = dct(x[:, col], **kwargs)
|
| 59 |
+
return x
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def idct_2d_ref(x, **kwargs):
|
| 63 |
+
"""Calculate reference values for testing idct2."""
|
| 64 |
+
x = np.array(x, copy=True)
|
| 65 |
+
for row in range(x.shape[0]):
|
| 66 |
+
x[row, :] = idct(x[row, :], **kwargs)
|
| 67 |
+
for col in range(x.shape[1]):
|
| 68 |
+
x[:, col] = idct(x[:, col], **kwargs)
|
| 69 |
+
return x
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def dst_2d_ref(x, **kwargs):
|
| 73 |
+
"""Calculate reference values for testing dst2."""
|
| 74 |
+
x = np.array(x, copy=True)
|
| 75 |
+
for row in range(x.shape[0]):
|
| 76 |
+
x[row, :] = dst(x[row, :], **kwargs)
|
| 77 |
+
for col in range(x.shape[1]):
|
| 78 |
+
x[:, col] = dst(x[:, col], **kwargs)
|
| 79 |
+
return x
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def idst_2d_ref(x, **kwargs):
|
| 83 |
+
"""Calculate reference values for testing idst2."""
|
| 84 |
+
x = np.array(x, copy=True)
|
| 85 |
+
for row in range(x.shape[0]):
|
| 86 |
+
x[row, :] = idst(x[row, :], **kwargs)
|
| 87 |
+
for col in range(x.shape[1]):
|
| 88 |
+
x[:, col] = idst(x[:, col], **kwargs)
|
| 89 |
+
return x
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def naive_dct1(x, norm=None):
|
| 93 |
+
"""Calculate textbook definition version of DCT-I."""
|
| 94 |
+
x = np.array(x, copy=True)
|
| 95 |
+
N = len(x)
|
| 96 |
+
M = N-1
|
| 97 |
+
y = np.zeros(N)
|
| 98 |
+
m0, m = 1, 2
|
| 99 |
+
if norm == 'ortho':
|
| 100 |
+
m0 = np.sqrt(1.0/M)
|
| 101 |
+
m = np.sqrt(2.0/M)
|
| 102 |
+
for k in range(N):
|
| 103 |
+
for n in range(1, N-1):
|
| 104 |
+
y[k] += m*x[n]*np.cos(np.pi*n*k/M)
|
| 105 |
+
y[k] += m0 * x[0]
|
| 106 |
+
y[k] += m0 * x[N-1] * (1 if k % 2 == 0 else -1)
|
| 107 |
+
if norm == 'ortho':
|
| 108 |
+
y[0] *= 1/np.sqrt(2)
|
| 109 |
+
y[N-1] *= 1/np.sqrt(2)
|
| 110 |
+
return y
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
def naive_dst1(x, norm=None):
|
| 114 |
+
"""Calculate textbook definition version of DST-I."""
|
| 115 |
+
x = np.array(x, copy=True)
|
| 116 |
+
N = len(x)
|
| 117 |
+
M = N+1
|
| 118 |
+
y = np.zeros(N)
|
| 119 |
+
for k in range(N):
|
| 120 |
+
for n in range(N):
|
| 121 |
+
y[k] += 2*x[n]*np.sin(np.pi*(n+1.0)*(k+1.0)/M)
|
| 122 |
+
if norm == 'ortho':
|
| 123 |
+
y *= np.sqrt(0.5/M)
|
| 124 |
+
return y
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
def naive_dct4(x, norm=None):
|
| 128 |
+
"""Calculate textbook definition version of DCT-IV."""
|
| 129 |
+
x = np.array(x, copy=True)
|
| 130 |
+
N = len(x)
|
| 131 |
+
y = np.zeros(N)
|
| 132 |
+
for k in range(N):
|
| 133 |
+
for n in range(N):
|
| 134 |
+
y[k] += x[n]*np.cos(np.pi*(n+0.5)*(k+0.5)/(N))
|
| 135 |
+
if norm == 'ortho':
|
| 136 |
+
y *= np.sqrt(2.0/N)
|
| 137 |
+
else:
|
| 138 |
+
y *= 2
|
| 139 |
+
return y
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
def naive_dst4(x, norm=None):
|
| 143 |
+
"""Calculate textbook definition version of DST-IV."""
|
| 144 |
+
x = np.array(x, copy=True)
|
| 145 |
+
N = len(x)
|
| 146 |
+
y = np.zeros(N)
|
| 147 |
+
for k in range(N):
|
| 148 |
+
for n in range(N):
|
| 149 |
+
y[k] += x[n]*np.sin(np.pi*(n+0.5)*(k+0.5)/(N))
|
| 150 |
+
if norm == 'ortho':
|
| 151 |
+
y *= np.sqrt(2.0/N)
|
| 152 |
+
else:
|
| 153 |
+
y *= 2
|
| 154 |
+
return y
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
class TestComplex:
|
| 158 |
+
def test_dct_complex64(self):
|
| 159 |
+
y = dct(1j*np.arange(5, dtype=np.complex64))
|
| 160 |
+
x = 1j*dct(np.arange(5))
|
| 161 |
+
assert_array_almost_equal(x, y)
|
| 162 |
+
|
| 163 |
+
def test_dct_complex(self):
|
| 164 |
+
y = dct(np.arange(5)*1j)
|
| 165 |
+
x = 1j*dct(np.arange(5))
|
| 166 |
+
assert_array_almost_equal(x, y)
|
| 167 |
+
|
| 168 |
+
def test_idct_complex(self):
|
| 169 |
+
y = idct(np.arange(5)*1j)
|
| 170 |
+
x = 1j*idct(np.arange(5))
|
| 171 |
+
assert_array_almost_equal(x, y)
|
| 172 |
+
|
| 173 |
+
def test_dst_complex64(self):
|
| 174 |
+
y = dst(np.arange(5, dtype=np.complex64)*1j)
|
| 175 |
+
x = 1j*dst(np.arange(5))
|
| 176 |
+
assert_array_almost_equal(x, y)
|
| 177 |
+
|
| 178 |
+
def test_dst_complex(self):
|
| 179 |
+
y = dst(np.arange(5)*1j)
|
| 180 |
+
x = 1j*dst(np.arange(5))
|
| 181 |
+
assert_array_almost_equal(x, y)
|
| 182 |
+
|
| 183 |
+
def test_idst_complex(self):
|
| 184 |
+
y = idst(np.arange(5)*1j)
|
| 185 |
+
x = 1j*idst(np.arange(5))
|
| 186 |
+
assert_array_almost_equal(x, y)
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
class _TestDCTBase:
|
| 190 |
+
def setup_method(self):
|
| 191 |
+
self.rdt = None
|
| 192 |
+
self.dec = 14
|
| 193 |
+
self.type = None
|
| 194 |
+
|
| 195 |
+
def test_definition(self):
|
| 196 |
+
for i in FFTWDATA_SIZES:
|
| 197 |
+
x, yr, dt = fftw_dct_ref(self.type, i, self.rdt)
|
| 198 |
+
y = dct(x, type=self.type)
|
| 199 |
+
assert_equal(y.dtype, dt)
|
| 200 |
+
# XXX: we divide by np.max(y) because the tests fail otherwise. We
|
| 201 |
+
# should really use something like assert_array_approx_equal. The
|
| 202 |
+
# difference is due to fftw using a better algorithm w.r.t error
|
| 203 |
+
# propagation compared to the ones from fftpack.
|
| 204 |
+
assert_array_almost_equal(y / np.max(y), yr / np.max(y), decimal=self.dec,
|
| 205 |
+
err_msg="Size %d failed" % i)
|
| 206 |
+
|
| 207 |
+
def test_axis(self):
|
| 208 |
+
nt = 2
|
| 209 |
+
for i in [7, 8, 9, 16, 32, 64]:
|
| 210 |
+
x = np.random.randn(nt, i)
|
| 211 |
+
y = dct(x, type=self.type)
|
| 212 |
+
for j in range(nt):
|
| 213 |
+
assert_array_almost_equal(y[j], dct(x[j], type=self.type),
|
| 214 |
+
decimal=self.dec)
|
| 215 |
+
|
| 216 |
+
x = x.T
|
| 217 |
+
y = dct(x, axis=0, type=self.type)
|
| 218 |
+
for j in range(nt):
|
| 219 |
+
assert_array_almost_equal(y[:,j], dct(x[:,j], type=self.type),
|
| 220 |
+
decimal=self.dec)
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
class _TestDCTIBase(_TestDCTBase):
|
| 224 |
+
def test_definition_ortho(self):
|
| 225 |
+
# Test orthornomal mode.
|
| 226 |
+
dt = np.result_type(np.float32, self.rdt)
|
| 227 |
+
for xr in X:
|
| 228 |
+
x = np.array(xr, dtype=self.rdt)
|
| 229 |
+
y = dct(x, norm='ortho', type=1)
|
| 230 |
+
y2 = naive_dct1(x, norm='ortho')
|
| 231 |
+
assert_equal(y.dtype, dt)
|
| 232 |
+
assert_array_almost_equal(y / np.max(y), y2 / np.max(y), decimal=self.dec)
|
| 233 |
+
|
| 234 |
+
class _TestDCTIIBase(_TestDCTBase):
|
| 235 |
+
def test_definition_matlab(self):
|
| 236 |
+
# Test correspondence with MATLAB (orthornomal mode).
|
| 237 |
+
dt = np.result_type(np.float32, self.rdt)
|
| 238 |
+
for xr, yr in zip(X, Y):
|
| 239 |
+
x = np.array(xr, dtype=dt)
|
| 240 |
+
y = dct(x, norm="ortho", type=2)
|
| 241 |
+
assert_equal(y.dtype, dt)
|
| 242 |
+
assert_array_almost_equal(y, yr, decimal=self.dec)
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
class _TestDCTIIIBase(_TestDCTBase):
|
| 246 |
+
def test_definition_ortho(self):
|
| 247 |
+
# Test orthornomal mode.
|
| 248 |
+
dt = np.result_type(np.float32, self.rdt)
|
| 249 |
+
for xr in X:
|
| 250 |
+
x = np.array(xr, dtype=self.rdt)
|
| 251 |
+
y = dct(x, norm='ortho', type=2)
|
| 252 |
+
xi = dct(y, norm="ortho", type=3)
|
| 253 |
+
assert_equal(xi.dtype, dt)
|
| 254 |
+
assert_array_almost_equal(xi, x, decimal=self.dec)
|
| 255 |
+
|
| 256 |
+
class _TestDCTIVBase(_TestDCTBase):
|
| 257 |
+
def test_definition_ortho(self):
|
| 258 |
+
# Test orthornomal mode.
|
| 259 |
+
dt = np.result_type(np.float32, self.rdt)
|
| 260 |
+
for xr in X:
|
| 261 |
+
x = np.array(xr, dtype=self.rdt)
|
| 262 |
+
y = dct(x, norm='ortho', type=4)
|
| 263 |
+
y2 = naive_dct4(x, norm='ortho')
|
| 264 |
+
assert_equal(y.dtype, dt)
|
| 265 |
+
assert_array_almost_equal(y / np.max(y), y2 / np.max(y), decimal=self.dec)
|
| 266 |
+
|
| 267 |
+
|
| 268 |
+
class TestDCTIDouble(_TestDCTIBase):
|
| 269 |
+
def setup_method(self):
|
| 270 |
+
self.rdt = np.float64
|
| 271 |
+
self.dec = 10
|
| 272 |
+
self.type = 1
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
class TestDCTIFloat(_TestDCTIBase):
|
| 276 |
+
def setup_method(self):
|
| 277 |
+
self.rdt = np.float32
|
| 278 |
+
self.dec = 4
|
| 279 |
+
self.type = 1
|
| 280 |
+
|
| 281 |
+
|
| 282 |
+
class TestDCTIInt(_TestDCTIBase):
|
| 283 |
+
def setup_method(self):
|
| 284 |
+
self.rdt = int
|
| 285 |
+
self.dec = 5
|
| 286 |
+
self.type = 1
|
| 287 |
+
|
| 288 |
+
|
| 289 |
+
class TestDCTIIDouble(_TestDCTIIBase):
|
| 290 |
+
def setup_method(self):
|
| 291 |
+
self.rdt = np.float64
|
| 292 |
+
self.dec = 10
|
| 293 |
+
self.type = 2
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
class TestDCTIIFloat(_TestDCTIIBase):
|
| 297 |
+
def setup_method(self):
|
| 298 |
+
self.rdt = np.float32
|
| 299 |
+
self.dec = 5
|
| 300 |
+
self.type = 2
|
| 301 |
+
|
| 302 |
+
|
| 303 |
+
class TestDCTIIInt(_TestDCTIIBase):
|
| 304 |
+
def setup_method(self):
|
| 305 |
+
self.rdt = int
|
| 306 |
+
self.dec = 5
|
| 307 |
+
self.type = 2
|
| 308 |
+
|
| 309 |
+
|
| 310 |
+
class TestDCTIIIDouble(_TestDCTIIIBase):
|
| 311 |
+
def setup_method(self):
|
| 312 |
+
self.rdt = np.float64
|
| 313 |
+
self.dec = 14
|
| 314 |
+
self.type = 3
|
| 315 |
+
|
| 316 |
+
|
| 317 |
+
class TestDCTIIIFloat(_TestDCTIIIBase):
|
| 318 |
+
def setup_method(self):
|
| 319 |
+
self.rdt = np.float32
|
| 320 |
+
self.dec = 5
|
| 321 |
+
self.type = 3
|
| 322 |
+
|
| 323 |
+
|
| 324 |
+
class TestDCTIIIInt(_TestDCTIIIBase):
|
| 325 |
+
def setup_method(self):
|
| 326 |
+
self.rdt = int
|
| 327 |
+
self.dec = 5
|
| 328 |
+
self.type = 3
|
| 329 |
+
|
| 330 |
+
|
| 331 |
+
class TestDCTIVDouble(_TestDCTIVBase):
|
| 332 |
+
def setup_method(self):
|
| 333 |
+
self.rdt = np.float64
|
| 334 |
+
self.dec = 12
|
| 335 |
+
self.type = 3
|
| 336 |
+
|
| 337 |
+
|
| 338 |
+
class TestDCTIVFloat(_TestDCTIVBase):
|
| 339 |
+
def setup_method(self):
|
| 340 |
+
self.rdt = np.float32
|
| 341 |
+
self.dec = 5
|
| 342 |
+
self.type = 3
|
| 343 |
+
|
| 344 |
+
|
| 345 |
+
class TestDCTIVInt(_TestDCTIVBase):
|
| 346 |
+
def setup_method(self):
|
| 347 |
+
self.rdt = int
|
| 348 |
+
self.dec = 5
|
| 349 |
+
self.type = 3
|
| 350 |
+
|
| 351 |
+
|
| 352 |
+
class _TestIDCTBase:
|
| 353 |
+
def setup_method(self):
|
| 354 |
+
self.rdt = None
|
| 355 |
+
self.dec = 14
|
| 356 |
+
self.type = None
|
| 357 |
+
|
| 358 |
+
def test_definition(self):
|
| 359 |
+
for i in FFTWDATA_SIZES:
|
| 360 |
+
xr, yr, dt = fftw_dct_ref(self.type, i, self.rdt)
|
| 361 |
+
x = idct(yr, type=self.type)
|
| 362 |
+
if self.type == 1:
|
| 363 |
+
x /= 2 * (i-1)
|
| 364 |
+
else:
|
| 365 |
+
x /= 2 * i
|
| 366 |
+
assert_equal(x.dtype, dt)
|
| 367 |
+
# XXX: we divide by np.max(y) because the tests fail otherwise. We
|
| 368 |
+
# should really use something like assert_array_approx_equal. The
|
| 369 |
+
# difference is due to fftw using a better algorithm w.r.t error
|
| 370 |
+
# propagation compared to the ones from fftpack.
|
| 371 |
+
assert_array_almost_equal(x / np.max(x), xr / np.max(x), decimal=self.dec,
|
| 372 |
+
err_msg="Size %d failed" % i)
|
| 373 |
+
|
| 374 |
+
|
| 375 |
+
class TestIDCTIDouble(_TestIDCTBase):
|
| 376 |
+
def setup_method(self):
|
| 377 |
+
self.rdt = np.float64
|
| 378 |
+
self.dec = 10
|
| 379 |
+
self.type = 1
|
| 380 |
+
|
| 381 |
+
|
| 382 |
+
class TestIDCTIFloat(_TestIDCTBase):
|
| 383 |
+
def setup_method(self):
|
| 384 |
+
self.rdt = np.float32
|
| 385 |
+
self.dec = 4
|
| 386 |
+
self.type = 1
|
| 387 |
+
|
| 388 |
+
|
| 389 |
+
class TestIDCTIInt(_TestIDCTBase):
|
| 390 |
+
def setup_method(self):
|
| 391 |
+
self.rdt = int
|
| 392 |
+
self.dec = 4
|
| 393 |
+
self.type = 1
|
| 394 |
+
|
| 395 |
+
|
| 396 |
+
class TestIDCTIIDouble(_TestIDCTBase):
|
| 397 |
+
def setup_method(self):
|
| 398 |
+
self.rdt = np.float64
|
| 399 |
+
self.dec = 10
|
| 400 |
+
self.type = 2
|
| 401 |
+
|
| 402 |
+
|
| 403 |
+
class TestIDCTIIFloat(_TestIDCTBase):
|
| 404 |
+
def setup_method(self):
|
| 405 |
+
self.rdt = np.float32
|
| 406 |
+
self.dec = 5
|
| 407 |
+
self.type = 2
|
| 408 |
+
|
| 409 |
+
|
| 410 |
+
class TestIDCTIIInt(_TestIDCTBase):
|
| 411 |
+
def setup_method(self):
|
| 412 |
+
self.rdt = int
|
| 413 |
+
self.dec = 5
|
| 414 |
+
self.type = 2
|
| 415 |
+
|
| 416 |
+
|
| 417 |
+
class TestIDCTIIIDouble(_TestIDCTBase):
|
| 418 |
+
def setup_method(self):
|
| 419 |
+
self.rdt = np.float64
|
| 420 |
+
self.dec = 14
|
| 421 |
+
self.type = 3
|
| 422 |
+
|
| 423 |
+
|
| 424 |
+
class TestIDCTIIIFloat(_TestIDCTBase):
|
| 425 |
+
def setup_method(self):
|
| 426 |
+
self.rdt = np.float32
|
| 427 |
+
self.dec = 5
|
| 428 |
+
self.type = 3
|
| 429 |
+
|
| 430 |
+
|
| 431 |
+
class TestIDCTIIIInt(_TestIDCTBase):
|
| 432 |
+
def setup_method(self):
|
| 433 |
+
self.rdt = int
|
| 434 |
+
self.dec = 5
|
| 435 |
+
self.type = 3
|
| 436 |
+
|
| 437 |
+
class TestIDCTIVDouble(_TestIDCTBase):
|
| 438 |
+
def setup_method(self):
|
| 439 |
+
self.rdt = np.float64
|
| 440 |
+
self.dec = 12
|
| 441 |
+
self.type = 4
|
| 442 |
+
|
| 443 |
+
|
| 444 |
+
class TestIDCTIVFloat(_TestIDCTBase):
|
| 445 |
+
def setup_method(self):
|
| 446 |
+
self.rdt = np.float32
|
| 447 |
+
self.dec = 5
|
| 448 |
+
self.type = 4
|
| 449 |
+
|
| 450 |
+
|
| 451 |
+
class TestIDCTIVInt(_TestIDCTBase):
|
| 452 |
+
def setup_method(self):
|
| 453 |
+
self.rdt = int
|
| 454 |
+
self.dec = 5
|
| 455 |
+
self.type = 4
|
| 456 |
+
|
| 457 |
+
class _TestDSTBase:
|
| 458 |
+
def setup_method(self):
|
| 459 |
+
self.rdt = None # dtype
|
| 460 |
+
self.dec = None # number of decimals to match
|
| 461 |
+
self.type = None # dst type
|
| 462 |
+
|
| 463 |
+
def test_definition(self):
|
| 464 |
+
for i in FFTWDATA_SIZES:
|
| 465 |
+
xr, yr, dt = fftw_dst_ref(self.type, i, self.rdt)
|
| 466 |
+
y = dst(xr, type=self.type)
|
| 467 |
+
assert_equal(y.dtype, dt)
|
| 468 |
+
# XXX: we divide by np.max(y) because the tests fail otherwise. We
|
| 469 |
+
# should really use something like assert_array_approx_equal. The
|
| 470 |
+
# difference is due to fftw using a better algorithm w.r.t error
|
| 471 |
+
# propagation compared to the ones from fftpack.
|
| 472 |
+
assert_array_almost_equal(y / np.max(y), yr / np.max(y), decimal=self.dec,
|
| 473 |
+
err_msg="Size %d failed" % i)
|
| 474 |
+
|
| 475 |
+
|
| 476 |
+
class _TestDSTIBase(_TestDSTBase):
|
| 477 |
+
def test_definition_ortho(self):
|
| 478 |
+
# Test orthornomal mode.
|
| 479 |
+
dt = np.result_type(np.float32, self.rdt)
|
| 480 |
+
for xr in X:
|
| 481 |
+
x = np.array(xr, dtype=self.rdt)
|
| 482 |
+
y = dst(x, norm='ortho', type=1)
|
| 483 |
+
y2 = naive_dst1(x, norm='ortho')
|
| 484 |
+
assert_equal(y.dtype, dt)
|
| 485 |
+
assert_array_almost_equal(y / np.max(y), y2 / np.max(y), decimal=self.dec)
|
| 486 |
+
|
| 487 |
+
class _TestDSTIVBase(_TestDSTBase):
|
| 488 |
+
def test_definition_ortho(self):
|
| 489 |
+
# Test orthornomal mode.
|
| 490 |
+
dt = np.result_type(np.float32, self.rdt)
|
| 491 |
+
for xr in X:
|
| 492 |
+
x = np.array(xr, dtype=self.rdt)
|
| 493 |
+
y = dst(x, norm='ortho', type=4)
|
| 494 |
+
y2 = naive_dst4(x, norm='ortho')
|
| 495 |
+
assert_equal(y.dtype, dt)
|
| 496 |
+
assert_array_almost_equal(y, y2, decimal=self.dec)
|
| 497 |
+
|
| 498 |
+
class TestDSTIDouble(_TestDSTIBase):
|
| 499 |
+
def setup_method(self):
|
| 500 |
+
self.rdt = np.float64
|
| 501 |
+
self.dec = 12
|
| 502 |
+
self.type = 1
|
| 503 |
+
|
| 504 |
+
|
| 505 |
+
class TestDSTIFloat(_TestDSTIBase):
|
| 506 |
+
def setup_method(self):
|
| 507 |
+
self.rdt = np.float32
|
| 508 |
+
self.dec = 4
|
| 509 |
+
self.type = 1
|
| 510 |
+
|
| 511 |
+
|
| 512 |
+
class TestDSTIInt(_TestDSTIBase):
|
| 513 |
+
def setup_method(self):
|
| 514 |
+
self.rdt = int
|
| 515 |
+
self.dec = 5
|
| 516 |
+
self.type = 1
|
| 517 |
+
|
| 518 |
+
|
| 519 |
+
class TestDSTIIDouble(_TestDSTBase):
|
| 520 |
+
def setup_method(self):
|
| 521 |
+
self.rdt = np.float64
|
| 522 |
+
self.dec = 14
|
| 523 |
+
self.type = 2
|
| 524 |
+
|
| 525 |
+
|
| 526 |
+
class TestDSTIIFloat(_TestDSTBase):
|
| 527 |
+
def setup_method(self):
|
| 528 |
+
self.rdt = np.float32
|
| 529 |
+
self.dec = 6
|
| 530 |
+
self.type = 2
|
| 531 |
+
|
| 532 |
+
|
| 533 |
+
class TestDSTIIInt(_TestDSTBase):
|
| 534 |
+
def setup_method(self):
|
| 535 |
+
self.rdt = int
|
| 536 |
+
self.dec = 6
|
| 537 |
+
self.type = 2
|
| 538 |
+
|
| 539 |
+
|
| 540 |
+
class TestDSTIIIDouble(_TestDSTBase):
|
| 541 |
+
def setup_method(self):
|
| 542 |
+
self.rdt = np.float64
|
| 543 |
+
self.dec = 14
|
| 544 |
+
self.type = 3
|
| 545 |
+
|
| 546 |
+
|
| 547 |
+
class TestDSTIIIFloat(_TestDSTBase):
|
| 548 |
+
def setup_method(self):
|
| 549 |
+
self.rdt = np.float32
|
| 550 |
+
self.dec = 7
|
| 551 |
+
self.type = 3
|
| 552 |
+
|
| 553 |
+
|
| 554 |
+
class TestDSTIIIInt(_TestDSTBase):
|
| 555 |
+
def setup_method(self):
|
| 556 |
+
self.rdt = int
|
| 557 |
+
self.dec = 7
|
| 558 |
+
self.type = 3
|
| 559 |
+
|
| 560 |
+
|
| 561 |
+
class TestDSTIVDouble(_TestDSTIVBase):
|
| 562 |
+
def setup_method(self):
|
| 563 |
+
self.rdt = np.float64
|
| 564 |
+
self.dec = 12
|
| 565 |
+
self.type = 4
|
| 566 |
+
|
| 567 |
+
|
| 568 |
+
class TestDSTIVFloat(_TestDSTIVBase):
|
| 569 |
+
def setup_method(self):
|
| 570 |
+
self.rdt = np.float32
|
| 571 |
+
self.dec = 4
|
| 572 |
+
self.type = 4
|
| 573 |
+
|
| 574 |
+
|
| 575 |
+
class TestDSTIVInt(_TestDSTIVBase):
|
| 576 |
+
def setup_method(self):
|
| 577 |
+
self.rdt = int
|
| 578 |
+
self.dec = 5
|
| 579 |
+
self.type = 4
|
| 580 |
+
|
| 581 |
+
|
| 582 |
+
class _TestIDSTBase:
|
| 583 |
+
def setup_method(self):
|
| 584 |
+
self.rdt = None
|
| 585 |
+
self.dec = None
|
| 586 |
+
self.type = None
|
| 587 |
+
|
| 588 |
+
def test_definition(self):
|
| 589 |
+
for i in FFTWDATA_SIZES:
|
| 590 |
+
xr, yr, dt = fftw_dst_ref(self.type, i, self.rdt)
|
| 591 |
+
x = idst(yr, type=self.type)
|
| 592 |
+
if self.type == 1:
|
| 593 |
+
x /= 2 * (i+1)
|
| 594 |
+
else:
|
| 595 |
+
x /= 2 * i
|
| 596 |
+
assert_equal(x.dtype, dt)
|
| 597 |
+
# XXX: we divide by np.max(x) because the tests fail otherwise. We
|
| 598 |
+
# should really use something like assert_array_approx_equal. The
|
| 599 |
+
# difference is due to fftw using a better algorithm w.r.t error
|
| 600 |
+
# propagation compared to the ones from fftpack.
|
| 601 |
+
assert_array_almost_equal(x / np.max(x), xr / np.max(x), decimal=self.dec,
|
| 602 |
+
err_msg="Size %d failed" % i)
|
| 603 |
+
|
| 604 |
+
|
| 605 |
+
class TestIDSTIDouble(_TestIDSTBase):
|
| 606 |
+
def setup_method(self):
|
| 607 |
+
self.rdt = np.float64
|
| 608 |
+
self.dec = 12
|
| 609 |
+
self.type = 1
|
| 610 |
+
|
| 611 |
+
|
| 612 |
+
class TestIDSTIFloat(_TestIDSTBase):
|
| 613 |
+
def setup_method(self):
|
| 614 |
+
self.rdt = np.float32
|
| 615 |
+
self.dec = 4
|
| 616 |
+
self.type = 1
|
| 617 |
+
|
| 618 |
+
|
| 619 |
+
class TestIDSTIInt(_TestIDSTBase):
|
| 620 |
+
def setup_method(self):
|
| 621 |
+
self.rdt = int
|
| 622 |
+
self.dec = 4
|
| 623 |
+
self.type = 1
|
| 624 |
+
|
| 625 |
+
|
| 626 |
+
class TestIDSTIIDouble(_TestIDSTBase):
|
| 627 |
+
def setup_method(self):
|
| 628 |
+
self.rdt = np.float64
|
| 629 |
+
self.dec = 14
|
| 630 |
+
self.type = 2
|
| 631 |
+
|
| 632 |
+
|
| 633 |
+
class TestIDSTIIFloat(_TestIDSTBase):
|
| 634 |
+
def setup_method(self):
|
| 635 |
+
self.rdt = np.float32
|
| 636 |
+
self.dec = 6
|
| 637 |
+
self.type = 2
|
| 638 |
+
|
| 639 |
+
|
| 640 |
+
class TestIDSTIIInt(_TestIDSTBase):
|
| 641 |
+
def setup_method(self):
|
| 642 |
+
self.rdt = int
|
| 643 |
+
self.dec = 6
|
| 644 |
+
self.type = 2
|
| 645 |
+
|
| 646 |
+
|
| 647 |
+
class TestIDSTIIIDouble(_TestIDSTBase):
|
| 648 |
+
def setup_method(self):
|
| 649 |
+
self.rdt = np.float64
|
| 650 |
+
self.dec = 14
|
| 651 |
+
self.type = 3
|
| 652 |
+
|
| 653 |
+
|
| 654 |
+
class TestIDSTIIIFloat(_TestIDSTBase):
|
| 655 |
+
def setup_method(self):
|
| 656 |
+
self.rdt = np.float32
|
| 657 |
+
self.dec = 6
|
| 658 |
+
self.type = 3
|
| 659 |
+
|
| 660 |
+
|
| 661 |
+
class TestIDSTIIIInt(_TestIDSTBase):
|
| 662 |
+
def setup_method(self):
|
| 663 |
+
self.rdt = int
|
| 664 |
+
self.dec = 6
|
| 665 |
+
self.type = 3
|
| 666 |
+
|
| 667 |
+
|
| 668 |
+
class TestIDSTIVDouble(_TestIDSTBase):
|
| 669 |
+
def setup_method(self):
|
| 670 |
+
self.rdt = np.float64
|
| 671 |
+
self.dec = 12
|
| 672 |
+
self.type = 4
|
| 673 |
+
|
| 674 |
+
|
| 675 |
+
class TestIDSTIVFloat(_TestIDSTBase):
|
| 676 |
+
def setup_method(self):
|
| 677 |
+
self.rdt = np.float32
|
| 678 |
+
self.dec = 6
|
| 679 |
+
self.type = 4
|
| 680 |
+
|
| 681 |
+
|
| 682 |
+
class TestIDSTIVnt(_TestIDSTBase):
|
| 683 |
+
def setup_method(self):
|
| 684 |
+
self.rdt = int
|
| 685 |
+
self.dec = 6
|
| 686 |
+
self.type = 4
|
| 687 |
+
|
| 688 |
+
|
| 689 |
+
class TestOverwrite:
|
| 690 |
+
"""Check input overwrite behavior."""
|
| 691 |
+
|
| 692 |
+
real_dtypes = [np.float32, np.float64]
|
| 693 |
+
|
| 694 |
+
def _check(self, x, routine, type, fftsize, axis, norm, overwrite_x, **kw):
|
| 695 |
+
x2 = x.copy()
|
| 696 |
+
routine(x2, type, fftsize, axis, norm, overwrite_x=overwrite_x)
|
| 697 |
+
|
| 698 |
+
sig = "{}({}{!r}, {!r}, axis={!r}, overwrite_x={!r})".format(
|
| 699 |
+
routine.__name__, x.dtype, x.shape, fftsize, axis, overwrite_x)
|
| 700 |
+
if not overwrite_x:
|
| 701 |
+
assert_equal(x2, x, err_msg="spurious overwrite in %s" % sig)
|
| 702 |
+
|
| 703 |
+
def _check_1d(self, routine, dtype, shape, axis):
|
| 704 |
+
np.random.seed(1234)
|
| 705 |
+
if np.issubdtype(dtype, np.complexfloating):
|
| 706 |
+
data = np.random.randn(*shape) + 1j*np.random.randn(*shape)
|
| 707 |
+
else:
|
| 708 |
+
data = np.random.randn(*shape)
|
| 709 |
+
data = data.astype(dtype)
|
| 710 |
+
|
| 711 |
+
for type in [1, 2, 3, 4]:
|
| 712 |
+
for overwrite_x in [True, False]:
|
| 713 |
+
for norm in [None, 'ortho']:
|
| 714 |
+
self._check(data, routine, type, None, axis, norm,
|
| 715 |
+
overwrite_x)
|
| 716 |
+
|
| 717 |
+
def test_dct(self):
|
| 718 |
+
for dtype in self.real_dtypes:
|
| 719 |
+
self._check_1d(dct, dtype, (16,), -1)
|
| 720 |
+
self._check_1d(dct, dtype, (16, 2), 0)
|
| 721 |
+
self._check_1d(dct, dtype, (2, 16), 1)
|
| 722 |
+
|
| 723 |
+
def test_idct(self):
|
| 724 |
+
for dtype in self.real_dtypes:
|
| 725 |
+
self._check_1d(idct, dtype, (16,), -1)
|
| 726 |
+
self._check_1d(idct, dtype, (16, 2), 0)
|
| 727 |
+
self._check_1d(idct, dtype, (2, 16), 1)
|
| 728 |
+
|
| 729 |
+
def test_dst(self):
|
| 730 |
+
for dtype in self.real_dtypes:
|
| 731 |
+
self._check_1d(dst, dtype, (16,), -1)
|
| 732 |
+
self._check_1d(dst, dtype, (16, 2), 0)
|
| 733 |
+
self._check_1d(dst, dtype, (2, 16), 1)
|
| 734 |
+
|
| 735 |
+
def test_idst(self):
|
| 736 |
+
for dtype in self.real_dtypes:
|
| 737 |
+
self._check_1d(idst, dtype, (16,), -1)
|
| 738 |
+
self._check_1d(idst, dtype, (16, 2), 0)
|
| 739 |
+
self._check_1d(idst, dtype, (2, 16), 1)
|
| 740 |
+
|
| 741 |
+
|
| 742 |
+
class Test_DCTN_IDCTN:
|
| 743 |
+
dec = 14
|
| 744 |
+
dct_type = [1, 2, 3, 4]
|
| 745 |
+
norms = [None, 'ortho']
|
| 746 |
+
rstate = np.random.RandomState(1234)
|
| 747 |
+
shape = (32, 16)
|
| 748 |
+
data = rstate.randn(*shape)
|
| 749 |
+
|
| 750 |
+
@pytest.mark.parametrize('fforward,finverse', [(dctn, idctn),
|
| 751 |
+
(dstn, idstn)])
|
| 752 |
+
@pytest.mark.parametrize('axes', [None,
|
| 753 |
+
1, (1,), [1],
|
| 754 |
+
0, (0,), [0],
|
| 755 |
+
(0, 1), [0, 1],
|
| 756 |
+
(-2, -1), [-2, -1]])
|
| 757 |
+
@pytest.mark.parametrize('dct_type', dct_type)
|
| 758 |
+
@pytest.mark.parametrize('norm', ['ortho'])
|
| 759 |
+
def test_axes_round_trip(self, fforward, finverse, axes, dct_type, norm):
|
| 760 |
+
tmp = fforward(self.data, type=dct_type, axes=axes, norm=norm)
|
| 761 |
+
tmp = finverse(tmp, type=dct_type, axes=axes, norm=norm)
|
| 762 |
+
assert_array_almost_equal(self.data, tmp, decimal=12)
|
| 763 |
+
|
| 764 |
+
@pytest.mark.parametrize('fforward,fforward_ref', [(dctn, dct_2d_ref),
|
| 765 |
+
(dstn, dst_2d_ref)])
|
| 766 |
+
@pytest.mark.parametrize('dct_type', dct_type)
|
| 767 |
+
@pytest.mark.parametrize('norm', norms)
|
| 768 |
+
def test_dctn_vs_2d_reference(self, fforward, fforward_ref,
|
| 769 |
+
dct_type, norm):
|
| 770 |
+
y1 = fforward(self.data, type=dct_type, axes=None, norm=norm)
|
| 771 |
+
y2 = fforward_ref(self.data, type=dct_type, norm=norm)
|
| 772 |
+
assert_array_almost_equal(y1, y2, decimal=11)
|
| 773 |
+
|
| 774 |
+
@pytest.mark.parametrize('finverse,finverse_ref', [(idctn, idct_2d_ref),
|
| 775 |
+
(idstn, idst_2d_ref)])
|
| 776 |
+
@pytest.mark.parametrize('dct_type', dct_type)
|
| 777 |
+
@pytest.mark.parametrize('norm', [None, 'ortho'])
|
| 778 |
+
def test_idctn_vs_2d_reference(self, finverse, finverse_ref,
|
| 779 |
+
dct_type, norm):
|
| 780 |
+
fdata = dctn(self.data, type=dct_type, norm=norm)
|
| 781 |
+
y1 = finverse(fdata, type=dct_type, norm=norm)
|
| 782 |
+
y2 = finverse_ref(fdata, type=dct_type, norm=norm)
|
| 783 |
+
assert_array_almost_equal(y1, y2, decimal=11)
|
| 784 |
+
|
| 785 |
+
@pytest.mark.parametrize('fforward,finverse', [(dctn, idctn),
|
| 786 |
+
(dstn, idstn)])
|
| 787 |
+
def test_axes_and_shape(self, fforward, finverse):
|
| 788 |
+
with assert_raises(ValueError,
|
| 789 |
+
match="when given, axes and shape arguments"
|
| 790 |
+
" have to be of the same length"):
|
| 791 |
+
fforward(self.data, shape=self.data.shape[0], axes=(0, 1))
|
| 792 |
+
|
| 793 |
+
with assert_raises(ValueError,
|
| 794 |
+
match="when given, axes and shape arguments"
|
| 795 |
+
" have to be of the same length"):
|
| 796 |
+
fforward(self.data, shape=self.data.shape[0], axes=None)
|
| 797 |
+
|
| 798 |
+
with assert_raises(ValueError,
|
| 799 |
+
match="when given, axes and shape arguments"
|
| 800 |
+
" have to be of the same length"):
|
| 801 |
+
fforward(self.data, shape=self.data.shape, axes=0)
|
| 802 |
+
|
| 803 |
+
@pytest.mark.parametrize('fforward', [dctn, dstn])
|
| 804 |
+
def test_shape(self, fforward):
|
| 805 |
+
tmp = fforward(self.data, shape=(128, 128), axes=None)
|
| 806 |
+
assert_equal(tmp.shape, (128, 128))
|
| 807 |
+
|
| 808 |
+
@pytest.mark.parametrize('fforward,finverse', [(dctn, idctn),
|
| 809 |
+
(dstn, idstn)])
|
| 810 |
+
@pytest.mark.parametrize('axes', [1, (1,), [1],
|
| 811 |
+
0, (0,), [0]])
|
| 812 |
+
def test_shape_is_none_with_axes(self, fforward, finverse, axes):
|
| 813 |
+
tmp = fforward(self.data, shape=None, axes=axes, norm='ortho')
|
| 814 |
+
tmp = finverse(tmp, shape=None, axes=axes, norm='ortho')
|
| 815 |
+
assert_array_almost_equal(self.data, tmp, decimal=self.dec)
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/optimize/_lsq/__pycache__/bvls.cpython-310.pyc
ADDED
|
Binary file (2.76 kB). View file
|
|
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/optimize/_lsq/__pycache__/common.cpython-310.pyc
ADDED
|
Binary file (19.5 kB). View file
|
|
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/optimize/_lsq/__pycache__/dogbox.cpython-310.pyc
ADDED
|
Binary file (8.23 kB). View file
|
|
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/optimize/_lsq/__pycache__/least_squares.cpython-310.pyc
ADDED
|
Binary file (36.6 kB). View file
|
|
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/optimize/_lsq/__pycache__/lsq_linear.cpython-310.pyc
ADDED
|
Binary file (14.1 kB). View file
|
|
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/optimize/_lsq/__pycache__/trf_linear.cpython-310.pyc
ADDED
|
Binary file (5.69 kB). View file
|
|
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/optimize/_trlib/__init__.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ._trlib import TRLIBQuadraticSubproblem
|
| 2 |
+
|
| 3 |
+
__all__ = ['TRLIBQuadraticSubproblem', 'get_trlib_quadratic_subproblem']
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def get_trlib_quadratic_subproblem(tol_rel_i=-2.0, tol_rel_b=-3.0, disp=False):
|
| 7 |
+
def subproblem_factory(x, fun, jac, hess, hessp):
|
| 8 |
+
return TRLIBQuadraticSubproblem(x, fun, jac, hess, hessp,
|
| 9 |
+
tol_rel_i=tol_rel_i,
|
| 10 |
+
tol_rel_b=tol_rel_b,
|
| 11 |
+
disp=disp)
|
| 12 |
+
return subproblem_factory
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/optimize/_trlib/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (738 Bytes). View file
|
|
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/optimize/cython_optimize/__init__.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Cython optimize root finding API
|
| 3 |
+
================================
|
| 4 |
+
The underlying C functions for the following root finders can be accessed
|
| 5 |
+
directly using Cython:
|
| 6 |
+
|
| 7 |
+
- `~scipy.optimize.bisect`
|
| 8 |
+
- `~scipy.optimize.ridder`
|
| 9 |
+
- `~scipy.optimize.brenth`
|
| 10 |
+
- `~scipy.optimize.brentq`
|
| 11 |
+
|
| 12 |
+
The Cython API for the root finding functions is similar except there is no
|
| 13 |
+
``disp`` argument. Import the root finding functions using ``cimport`` from
|
| 14 |
+
`scipy.optimize.cython_optimize`. ::
|
| 15 |
+
|
| 16 |
+
from scipy.optimize.cython_optimize cimport bisect, ridder, brentq, brenth
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
Callback signature
|
| 20 |
+
------------------
|
| 21 |
+
The zeros functions in `~scipy.optimize.cython_optimize` expect a callback that
|
| 22 |
+
takes a double for the scalar independent variable as the 1st argument and a
|
| 23 |
+
user defined ``struct`` with any extra parameters as the 2nd argument. ::
|
| 24 |
+
|
| 25 |
+
double (*callback_type)(double, void*) noexcept
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
Examples
|
| 29 |
+
--------
|
| 30 |
+
Usage of `~scipy.optimize.cython_optimize` requires Cython to write callbacks
|
| 31 |
+
that are compiled into C. For more information on compiling Cython, see the
|
| 32 |
+
`Cython Documentation <http://docs.cython.org/en/latest/index.html>`_.
|
| 33 |
+
|
| 34 |
+
These are the basic steps:
|
| 35 |
+
|
| 36 |
+
1. Create a Cython ``.pyx`` file, for example: ``myexample.pyx``.
|
| 37 |
+
2. Import the desired root finder from `~scipy.optimize.cython_optimize`.
|
| 38 |
+
3. Write the callback function, and call the selected root finding function
|
| 39 |
+
passing the callback, any extra arguments, and the other solver
|
| 40 |
+
parameters. ::
|
| 41 |
+
|
| 42 |
+
from scipy.optimize.cython_optimize cimport brentq
|
| 43 |
+
|
| 44 |
+
# import math from Cython
|
| 45 |
+
from libc cimport math
|
| 46 |
+
|
| 47 |
+
myargs = {'C0': 1.0, 'C1': 0.7} # a dictionary of extra arguments
|
| 48 |
+
XLO, XHI = 0.5, 1.0 # lower and upper search boundaries
|
| 49 |
+
XTOL, RTOL, MITR = 1e-3, 1e-3, 10 # other solver parameters
|
| 50 |
+
|
| 51 |
+
# user-defined struct for extra parameters
|
| 52 |
+
ctypedef struct test_params:
|
| 53 |
+
double C0
|
| 54 |
+
double C1
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
# user-defined callback
|
| 58 |
+
cdef double f(double x, void *args) noexcept:
|
| 59 |
+
cdef test_params *myargs = <test_params *> args
|
| 60 |
+
return myargs.C0 - math.exp(-(x - myargs.C1))
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
# Cython wrapper function
|
| 64 |
+
cdef double brentq_wrapper_example(dict args, double xa, double xb,
|
| 65 |
+
double xtol, double rtol, int mitr):
|
| 66 |
+
# Cython automatically casts dictionary to struct
|
| 67 |
+
cdef test_params myargs = args
|
| 68 |
+
return brentq(
|
| 69 |
+
f, xa, xb, <test_params *> &myargs, xtol, rtol, mitr, NULL)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
# Python function
|
| 73 |
+
def brentq_example(args=myargs, xa=XLO, xb=XHI, xtol=XTOL, rtol=RTOL,
|
| 74 |
+
mitr=MITR):
|
| 75 |
+
'''Calls Cython wrapper from Python.'''
|
| 76 |
+
return brentq_wrapper_example(args, xa, xb, xtol, rtol, mitr)
|
| 77 |
+
|
| 78 |
+
4. If you want to call your function from Python, create a Cython wrapper, and
|
| 79 |
+
a Python function that calls the wrapper, or use ``cpdef``. Then, in Python,
|
| 80 |
+
you can import and run the example. ::
|
| 81 |
+
|
| 82 |
+
from myexample import brentq_example
|
| 83 |
+
|
| 84 |
+
x = brentq_example()
|
| 85 |
+
# 0.6999942848231314
|
| 86 |
+
|
| 87 |
+
5. Create a Cython ``.pxd`` file if you need to export any Cython functions.
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
Full output
|
| 91 |
+
-----------
|
| 92 |
+
The functions in `~scipy.optimize.cython_optimize` can also copy the full
|
| 93 |
+
output from the solver to a C ``struct`` that is passed as its last argument.
|
| 94 |
+
If you don't want the full output, just pass ``NULL``. The full output
|
| 95 |
+
``struct`` must be type ``zeros_full_output``, which is defined in
|
| 96 |
+
`scipy.optimize.cython_optimize` with the following fields:
|
| 97 |
+
|
| 98 |
+
- ``int funcalls``: number of function calls
|
| 99 |
+
- ``int iterations``: number of iterations
|
| 100 |
+
- ``int error_num``: error number
|
| 101 |
+
- ``double root``: root of function
|
| 102 |
+
|
| 103 |
+
The root is copied by `~scipy.optimize.cython_optimize` to the full output
|
| 104 |
+
``struct``. An error number of -1 means a sign error, -2 means a convergence
|
| 105 |
+
error, and 0 means the solver converged. Continuing from the previous example::
|
| 106 |
+
|
| 107 |
+
from scipy.optimize.cython_optimize cimport zeros_full_output
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
# cython brentq solver with full output
|
| 111 |
+
cdef zeros_full_output brentq_full_output_wrapper_example(
|
| 112 |
+
dict args, double xa, double xb, double xtol, double rtol,
|
| 113 |
+
int mitr):
|
| 114 |
+
cdef test_params myargs = args
|
| 115 |
+
cdef zeros_full_output my_full_output
|
| 116 |
+
# use my_full_output instead of NULL
|
| 117 |
+
brentq(f, xa, xb, &myargs, xtol, rtol, mitr, &my_full_output)
|
| 118 |
+
return my_full_output
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
# Python function
|
| 122 |
+
def brent_full_output_example(args=myargs, xa=XLO, xb=XHI, xtol=XTOL,
|
| 123 |
+
rtol=RTOL, mitr=MITR):
|
| 124 |
+
'''Returns full output'''
|
| 125 |
+
return brentq_full_output_wrapper_example(args, xa, xb, xtol, rtol,
|
| 126 |
+
mitr)
|
| 127 |
+
|
| 128 |
+
result = brent_full_output_example()
|
| 129 |
+
# {'error_num': 0,
|
| 130 |
+
# 'funcalls': 6,
|
| 131 |
+
# 'iterations': 5,
|
| 132 |
+
# 'root': 0.6999942848231314}
|
| 133 |
+
"""
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/optimize/cython_optimize/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (5.12 kB). View file
|
|
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/optimize/cython_optimize/_zeros.pxd
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Legacy public Cython API declarations
|
| 2 |
+
#
|
| 3 |
+
# NOTE: due to the way Cython ABI compatibility works, **no changes
|
| 4 |
+
# should be made to this file** --- any API additions/changes should be
|
| 5 |
+
# done in `cython_optimize.pxd` (see gh-11793).
|
| 6 |
+
|
| 7 |
+
ctypedef double (*callback_type)(double, void*) noexcept
|
| 8 |
+
|
| 9 |
+
ctypedef struct zeros_parameters:
|
| 10 |
+
callback_type function
|
| 11 |
+
void* args
|
| 12 |
+
|
| 13 |
+
ctypedef struct zeros_full_output:
|
| 14 |
+
int funcalls
|
| 15 |
+
int iterations
|
| 16 |
+
int error_num
|
| 17 |
+
double root
|
| 18 |
+
|
| 19 |
+
cdef double bisect(callback_type f, double xa, double xb, void* args,
|
| 20 |
+
double xtol, double rtol, int iter,
|
| 21 |
+
zeros_full_output *full_output) noexcept nogil
|
| 22 |
+
|
| 23 |
+
cdef double ridder(callback_type f, double xa, double xb, void* args,
|
| 24 |
+
double xtol, double rtol, int iter,
|
| 25 |
+
zeros_full_output *full_output) noexcept nogil
|
| 26 |
+
|
| 27 |
+
cdef double brenth(callback_type f, double xa, double xb, void* args,
|
| 28 |
+
double xtol, double rtol, int iter,
|
| 29 |
+
zeros_full_output *full_output) noexcept nogil
|
| 30 |
+
|
| 31 |
+
cdef double brentq(callback_type f, double xa, double xb, void* args,
|
| 32 |
+
double xtol, double rtol, int iter,
|
| 33 |
+
zeros_full_output *full_output) noexcept nogil
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/optimize/cython_optimize/c_zeros.pxd
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
cdef extern from "../Zeros/zeros.h":
|
| 2 |
+
ctypedef double (*callback_type)(double, void*) noexcept
|
| 3 |
+
ctypedef struct scipy_zeros_info:
|
| 4 |
+
int funcalls
|
| 5 |
+
int iterations
|
| 6 |
+
int error_num
|
| 7 |
+
|
| 8 |
+
cdef extern from "../Zeros/bisect.c" nogil:
|
| 9 |
+
double bisect(callback_type f, double xa, double xb, double xtol,
|
| 10 |
+
double rtol, int iter, void *func_data_param,
|
| 11 |
+
scipy_zeros_info *solver_stats)
|
| 12 |
+
|
| 13 |
+
cdef extern from "../Zeros/ridder.c" nogil:
|
| 14 |
+
double ridder(callback_type f, double xa, double xb, double xtol,
|
| 15 |
+
double rtol, int iter, void *func_data_param,
|
| 16 |
+
scipy_zeros_info *solver_stats)
|
| 17 |
+
|
| 18 |
+
cdef extern from "../Zeros/brenth.c" nogil:
|
| 19 |
+
double brenth(callback_type f, double xa, double xb, double xtol,
|
| 20 |
+
double rtol, int iter, void *func_data_param,
|
| 21 |
+
scipy_zeros_info *solver_stats)
|
| 22 |
+
|
| 23 |
+
cdef extern from "../Zeros/brentq.c" nogil:
|
| 24 |
+
double brentq(callback_type f, double xa, double xb, double xtol,
|
| 25 |
+
double rtol, int iter, void *func_data_param,
|
| 26 |
+
scipy_zeros_info *solver_stats)
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/sparse/__init__.py
ADDED
|
@@ -0,0 +1,323 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
=====================================
|
| 3 |
+
Sparse matrices (:mod:`scipy.sparse`)
|
| 4 |
+
=====================================
|
| 5 |
+
|
| 6 |
+
.. currentmodule:: scipy.sparse
|
| 7 |
+
|
| 8 |
+
.. toctree::
|
| 9 |
+
:hidden:
|
| 10 |
+
|
| 11 |
+
sparse.csgraph
|
| 12 |
+
sparse.linalg
|
| 13 |
+
|
| 14 |
+
SciPy 2-D sparse array package for numeric data.
|
| 15 |
+
|
| 16 |
+
.. note::
|
| 17 |
+
|
| 18 |
+
This package is switching to an array interface, compatible with
|
| 19 |
+
NumPy arrays, from the older matrix interface. We recommend that
|
| 20 |
+
you use the array objects (`bsr_array`, `coo_array`, etc.) for
|
| 21 |
+
all new work.
|
| 22 |
+
|
| 23 |
+
When using the array interface, please note that:
|
| 24 |
+
|
| 25 |
+
- ``x * y`` no longer performs matrix multiplication, but
|
| 26 |
+
element-wise multiplication (just like with NumPy arrays). To
|
| 27 |
+
make code work with both arrays and matrices, use ``x @ y`` for
|
| 28 |
+
matrix multiplication.
|
| 29 |
+
- Operations such as `sum`, that used to produce dense matrices, now
|
| 30 |
+
produce arrays, whose multiplication behavior differs similarly.
|
| 31 |
+
- Sparse arrays currently must be two-dimensional. This also means
|
| 32 |
+
that all *slicing* operations on these objects must produce
|
| 33 |
+
two-dimensional results, or they will result in an error. This
|
| 34 |
+
will be addressed in a future version.
|
| 35 |
+
|
| 36 |
+
The construction utilities (`eye`, `kron`, `random`, `diags`, etc.)
|
| 37 |
+
have not yet been ported, but their results can be wrapped into arrays::
|
| 38 |
+
|
| 39 |
+
A = csr_array(eye(3))
|
| 40 |
+
|
| 41 |
+
Contents
|
| 42 |
+
========
|
| 43 |
+
|
| 44 |
+
Sparse array classes
|
| 45 |
+
--------------------
|
| 46 |
+
|
| 47 |
+
.. autosummary::
|
| 48 |
+
:toctree: generated/
|
| 49 |
+
|
| 50 |
+
bsr_array - Block Sparse Row array
|
| 51 |
+
coo_array - A sparse array in COOrdinate format
|
| 52 |
+
csc_array - Compressed Sparse Column array
|
| 53 |
+
csr_array - Compressed Sparse Row array
|
| 54 |
+
dia_array - Sparse array with DIAgonal storage
|
| 55 |
+
dok_array - Dictionary Of Keys based sparse array
|
| 56 |
+
lil_array - Row-based list of lists sparse array
|
| 57 |
+
sparray - Sparse array base class
|
| 58 |
+
|
| 59 |
+
Sparse matrix classes
|
| 60 |
+
---------------------
|
| 61 |
+
|
| 62 |
+
.. autosummary::
|
| 63 |
+
:toctree: generated/
|
| 64 |
+
|
| 65 |
+
bsr_matrix - Block Sparse Row matrix
|
| 66 |
+
coo_matrix - A sparse matrix in COOrdinate format
|
| 67 |
+
csc_matrix - Compressed Sparse Column matrix
|
| 68 |
+
csr_matrix - Compressed Sparse Row matrix
|
| 69 |
+
dia_matrix - Sparse matrix with DIAgonal storage
|
| 70 |
+
dok_matrix - Dictionary Of Keys based sparse matrix
|
| 71 |
+
lil_matrix - Row-based list of lists sparse matrix
|
| 72 |
+
spmatrix - Sparse matrix base class
|
| 73 |
+
|
| 74 |
+
Functions
|
| 75 |
+
---------
|
| 76 |
+
|
| 77 |
+
Building sparse arrays:
|
| 78 |
+
|
| 79 |
+
.. autosummary::
|
| 80 |
+
:toctree: generated/
|
| 81 |
+
|
| 82 |
+
diags_array - Return a sparse array from diagonals
|
| 83 |
+
eye_array - Sparse MxN array whose k-th diagonal is all ones
|
| 84 |
+
random_array - Random values in a given shape array
|
| 85 |
+
block_array - Build a sparse array from sub-blocks
|
| 86 |
+
|
| 87 |
+
Building sparse matrices:
|
| 88 |
+
|
| 89 |
+
.. autosummary::
|
| 90 |
+
:toctree: generated/
|
| 91 |
+
|
| 92 |
+
eye - Sparse MxN matrix whose k-th diagonal is all ones
|
| 93 |
+
identity - Identity matrix in sparse matrix format
|
| 94 |
+
diags - Return a sparse matrix from diagonals
|
| 95 |
+
spdiags - Return a sparse matrix from diagonals
|
| 96 |
+
bmat - Build a sparse matrix from sparse sub-blocks
|
| 97 |
+
random - Random values in a given shape matrix
|
| 98 |
+
rand - Random values in a given shape matrix (old interface)
|
| 99 |
+
|
| 100 |
+
Building larger structures from smaller (array or matrix)
|
| 101 |
+
|
| 102 |
+
.. autosummary::
|
| 103 |
+
:toctree: generated/
|
| 104 |
+
|
| 105 |
+
kron - kronecker product of two sparse matrices
|
| 106 |
+
kronsum - kronecker sum of sparse matrices
|
| 107 |
+
block_diag - Build a block diagonal sparse matrix
|
| 108 |
+
tril - Lower triangular portion of a matrix in sparse format
|
| 109 |
+
triu - Upper triangular portion of a matrix in sparse format
|
| 110 |
+
hstack - Stack sparse matrices horizontally (column wise)
|
| 111 |
+
vstack - Stack sparse matrices vertically (row wise)
|
| 112 |
+
|
| 113 |
+
Save and load sparse matrices:
|
| 114 |
+
|
| 115 |
+
.. autosummary::
|
| 116 |
+
:toctree: generated/
|
| 117 |
+
|
| 118 |
+
save_npz - Save a sparse matrix/array to a file using ``.npz`` format.
|
| 119 |
+
load_npz - Load a sparse matrix/array from a file using ``.npz`` format.
|
| 120 |
+
|
| 121 |
+
Sparse tools:
|
| 122 |
+
|
| 123 |
+
.. autosummary::
|
| 124 |
+
:toctree: generated/
|
| 125 |
+
|
| 126 |
+
find
|
| 127 |
+
|
| 128 |
+
Identifying sparse arrays:
|
| 129 |
+
|
| 130 |
+
- use `isinstance(A, sp.sparse.sparray)` to check whether an array or matrix.
|
| 131 |
+
- use `A.format == 'csr'` to check the sparse format
|
| 132 |
+
|
| 133 |
+
Identifying sparse matrices:
|
| 134 |
+
|
| 135 |
+
.. autosummary::
|
| 136 |
+
:toctree: generated/
|
| 137 |
+
|
| 138 |
+
issparse
|
| 139 |
+
isspmatrix
|
| 140 |
+
isspmatrix_csc
|
| 141 |
+
isspmatrix_csr
|
| 142 |
+
isspmatrix_bsr
|
| 143 |
+
isspmatrix_lil
|
| 144 |
+
isspmatrix_dok
|
| 145 |
+
isspmatrix_coo
|
| 146 |
+
isspmatrix_dia
|
| 147 |
+
|
| 148 |
+
Submodules
|
| 149 |
+
----------
|
| 150 |
+
|
| 151 |
+
.. autosummary::
|
| 152 |
+
|
| 153 |
+
csgraph - Compressed sparse graph routines
|
| 154 |
+
linalg - sparse linear algebra routines
|
| 155 |
+
|
| 156 |
+
Exceptions
|
| 157 |
+
----------
|
| 158 |
+
|
| 159 |
+
.. autosummary::
|
| 160 |
+
:toctree: generated/
|
| 161 |
+
|
| 162 |
+
SparseEfficiencyWarning
|
| 163 |
+
SparseWarning
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
Usage information
|
| 167 |
+
=================
|
| 168 |
+
|
| 169 |
+
There are seven available sparse array types:
|
| 170 |
+
|
| 171 |
+
1. `csc_array`: Compressed Sparse Column format
|
| 172 |
+
2. `csr_array`: Compressed Sparse Row format
|
| 173 |
+
3. `bsr_array`: Block Sparse Row format
|
| 174 |
+
4. `lil_array`: List of Lists format
|
| 175 |
+
5. `dok_array`: Dictionary of Keys format
|
| 176 |
+
6. `coo_array`: COOrdinate format (aka IJV, triplet format)
|
| 177 |
+
7. `dia_array`: DIAgonal format
|
| 178 |
+
|
| 179 |
+
To construct an array efficiently, use either `dok_array` or `lil_array`.
|
| 180 |
+
The `lil_array` class supports basic slicing and fancy indexing with a
|
| 181 |
+
similar syntax to NumPy arrays. As illustrated below, the COO format
|
| 182 |
+
may also be used to efficiently construct arrays. Despite their
|
| 183 |
+
similarity to NumPy arrays, it is **strongly discouraged** to use NumPy
|
| 184 |
+
functions directly on these arrays because NumPy may not properly convert
|
| 185 |
+
them for computations, leading to unexpected (and incorrect) results. If you
|
| 186 |
+
do want to apply a NumPy function to these arrays, first check if SciPy has
|
| 187 |
+
its own implementation for the given sparse array class, or **convert the
|
| 188 |
+
sparse array to a NumPy array** (e.g., using the ``toarray`` method of the
|
| 189 |
+
class) first before applying the method.
|
| 190 |
+
|
| 191 |
+
To perform manipulations such as multiplication or inversion, first
|
| 192 |
+
convert the array to either CSC or CSR format. The `lil_array` format is
|
| 193 |
+
row-based, so conversion to CSR is efficient, whereas conversion to CSC
|
| 194 |
+
is less so.
|
| 195 |
+
|
| 196 |
+
All conversions among the CSR, CSC, and COO formats are efficient,
|
| 197 |
+
linear-time operations.
|
| 198 |
+
|
| 199 |
+
Matrix vector product
|
| 200 |
+
---------------------
|
| 201 |
+
To do a vector product between a sparse array and a vector simply use
|
| 202 |
+
the array ``dot`` method, as described in its docstring:
|
| 203 |
+
|
| 204 |
+
>>> import numpy as np
|
| 205 |
+
>>> from scipy.sparse import csr_array
|
| 206 |
+
>>> A = csr_array([[1, 2, 0], [0, 0, 3], [4, 0, 5]])
|
| 207 |
+
>>> v = np.array([1, 0, -1])
|
| 208 |
+
>>> A.dot(v)
|
| 209 |
+
array([ 1, -3, -1], dtype=int64)
|
| 210 |
+
|
| 211 |
+
.. warning:: As of NumPy 1.7, ``np.dot`` is not aware of sparse arrays,
|
| 212 |
+
therefore using it will result on unexpected results or errors.
|
| 213 |
+
The corresponding dense array should be obtained first instead:
|
| 214 |
+
|
| 215 |
+
>>> np.dot(A.toarray(), v)
|
| 216 |
+
array([ 1, -3, -1], dtype=int64)
|
| 217 |
+
|
| 218 |
+
but then all the performance advantages would be lost.
|
| 219 |
+
|
| 220 |
+
The CSR format is especially suitable for fast matrix vector products.
|
| 221 |
+
|
| 222 |
+
Example 1
|
| 223 |
+
---------
|
| 224 |
+
Construct a 1000x1000 `lil_array` and add some values to it:
|
| 225 |
+
|
| 226 |
+
>>> from scipy.sparse import lil_array
|
| 227 |
+
>>> from scipy.sparse.linalg import spsolve
|
| 228 |
+
>>> from numpy.linalg import solve, norm
|
| 229 |
+
>>> from numpy.random import rand
|
| 230 |
+
|
| 231 |
+
>>> A = lil_array((1000, 1000))
|
| 232 |
+
>>> A[0, :100] = rand(100)
|
| 233 |
+
>>> A.setdiag(rand(1000))
|
| 234 |
+
|
| 235 |
+
Now convert it to CSR format and solve A x = b for x:
|
| 236 |
+
|
| 237 |
+
>>> A = A.tocsr()
|
| 238 |
+
>>> b = rand(1000)
|
| 239 |
+
>>> x = spsolve(A, b)
|
| 240 |
+
|
| 241 |
+
Convert it to a dense array and solve, and check that the result
|
| 242 |
+
is the same:
|
| 243 |
+
|
| 244 |
+
>>> x_ = solve(A.toarray(), b)
|
| 245 |
+
|
| 246 |
+
Now we can compute norm of the error with:
|
| 247 |
+
|
| 248 |
+
>>> err = norm(x-x_)
|
| 249 |
+
>>> err < 1e-10
|
| 250 |
+
True
|
| 251 |
+
|
| 252 |
+
It should be small :)
|
| 253 |
+
|
| 254 |
+
|
| 255 |
+
Example 2
|
| 256 |
+
---------
|
| 257 |
+
|
| 258 |
+
Construct an array in COO format:
|
| 259 |
+
|
| 260 |
+
>>> from scipy import sparse
|
| 261 |
+
>>> from numpy import array
|
| 262 |
+
>>> I = array([0,3,1,0])
|
| 263 |
+
>>> J = array([0,3,1,2])
|
| 264 |
+
>>> V = array([4,5,7,9])
|
| 265 |
+
>>> A = sparse.coo_array((V,(I,J)),shape=(4,4))
|
| 266 |
+
|
| 267 |
+
Notice that the indices do not need to be sorted.
|
| 268 |
+
|
| 269 |
+
Duplicate (i,j) entries are summed when converting to CSR or CSC.
|
| 270 |
+
|
| 271 |
+
>>> I = array([0,0,1,3,1,0,0])
|
| 272 |
+
>>> J = array([0,2,1,3,1,0,0])
|
| 273 |
+
>>> V = array([1,1,1,1,1,1,1])
|
| 274 |
+
>>> B = sparse.coo_array((V,(I,J)),shape=(4,4)).tocsr()
|
| 275 |
+
|
| 276 |
+
This is useful for constructing finite-element stiffness and mass matrices.
|
| 277 |
+
|
| 278 |
+
Further details
|
| 279 |
+
---------------
|
| 280 |
+
|
| 281 |
+
CSR column indices are not necessarily sorted. Likewise for CSC row
|
| 282 |
+
indices. Use the ``.sorted_indices()`` and ``.sort_indices()`` methods when
|
| 283 |
+
sorted indices are required (e.g., when passing data to other libraries).
|
| 284 |
+
|
| 285 |
+
"""
|
| 286 |
+
|
| 287 |
+
# Original code by Travis Oliphant.
|
| 288 |
+
# Modified and extended by Ed Schofield, Robert Cimrman,
|
| 289 |
+
# Nathan Bell, and Jake Vanderplas.
|
| 290 |
+
|
| 291 |
+
import warnings as _warnings
|
| 292 |
+
|
| 293 |
+
from ._base import *
|
| 294 |
+
from ._csr import *
|
| 295 |
+
from ._csc import *
|
| 296 |
+
from ._lil import *
|
| 297 |
+
from ._dok import *
|
| 298 |
+
from ._coo import *
|
| 299 |
+
from ._dia import *
|
| 300 |
+
from ._bsr import *
|
| 301 |
+
from ._construct import *
|
| 302 |
+
from ._extract import *
|
| 303 |
+
from ._matrix import spmatrix
|
| 304 |
+
from ._matrix_io import *
|
| 305 |
+
|
| 306 |
+
# For backward compatibility with v0.19.
|
| 307 |
+
from . import csgraph
|
| 308 |
+
|
| 309 |
+
# Deprecated namespaces, to be removed in v2.0.0
|
| 310 |
+
from . import (
|
| 311 |
+
base, bsr, compressed, construct, coo, csc, csr, data, dia, dok, extract,
|
| 312 |
+
lil, sparsetools, sputils
|
| 313 |
+
)
|
| 314 |
+
|
| 315 |
+
__all__ = [s for s in dir() if not s.startswith('_')]
|
| 316 |
+
|
| 317 |
+
# Filter PendingDeprecationWarning for np.matrix introduced with numpy 1.15
|
| 318 |
+
msg = 'the matrix subclass is not the recommended way'
|
| 319 |
+
_warnings.filterwarnings('ignore', message=msg)
|
| 320 |
+
|
| 321 |
+
from scipy._lib._testutils import PytestTester
|
| 322 |
+
test = PytestTester(__name__)
|
| 323 |
+
del PytestTester
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/sparse/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (9.37 kB). View file
|
|
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/sparse/__pycache__/_base.cpython-310.pyc
ADDED
|
Binary file (41.8 kB). View file
|
|
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/sparse/__pycache__/_bsr.cpython-310.pyc
ADDED
|
Binary file (22.9 kB). View file
|
|
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/sparse/__pycache__/_compressed.cpython-310.pyc
ADDED
|
Binary file (35.6 kB). View file
|
|
|
emissary-ml/llm-scripts/fine-tuning/llama3/venv/lib/python3.10/site-packages/scipy/sparse/__pycache__/_construct.cpython-310.pyc
ADDED
|
Binary file (42.9 kB). View file
|
|
|