diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/numpy/_info.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/numpy/_info.py new file mode 100644 index 0000000000000000000000000000000000000000..62f7ae62ca242cc17def5703b4412823aa31abcd --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/numpy/_info.py @@ -0,0 +1,346 @@ +""" +Array API Inspection namespace + +This is the namespace for inspection functions as defined by the array API +standard. See +https://data-apis.org/array-api/latest/API_specification/inspection.html for +more details. + +""" +from numpy import ( + dtype, + bool_ as bool, + intp, + int8, + int16, + int32, + int64, + uint8, + uint16, + uint32, + uint64, + float32, + float64, + complex64, + complex128, +) + + +class __array_namespace_info__: + """ + Get the array API inspection namespace for NumPy. + + The array API inspection namespace defines the following functions: + + - capabilities() + - default_device() + - default_dtypes() + - dtypes() + - devices() + + See + https://data-apis.org/array-api/latest/API_specification/inspection.html + for more details. + + Returns + ------- + info : ModuleType + The array API inspection namespace for NumPy. + + Examples + -------- + >>> info = np.__array_namespace_info__() + >>> info.default_dtypes() + {'real floating': numpy.float64, + 'complex floating': numpy.complex128, + 'integral': numpy.int64, + 'indexing': numpy.int64} + + """ + + __module__ = 'numpy' + + def capabilities(self): + """ + Return a dictionary of array API library capabilities. + + The resulting dictionary has the following keys: + + - **"boolean indexing"**: boolean indicating whether an array library + supports boolean indexing. Always ``True`` for NumPy. + + - **"data-dependent shapes"**: boolean indicating whether an array + library supports data-dependent output shapes. Always ``True`` for + NumPy. + + See + https://data-apis.org/array-api/latest/API_specification/generated/array_api.info.capabilities.html + for more details. + + See Also + -------- + __array_namespace_info__.default_device, + __array_namespace_info__.default_dtypes, + __array_namespace_info__.dtypes, + __array_namespace_info__.devices + + Returns + ------- + capabilities : dict + A dictionary of array API library capabilities. + + Examples + -------- + >>> info = np.__array_namespace_info__() + >>> info.capabilities() + {'boolean indexing': True, + 'data-dependent shapes': True} + + """ + return { + "boolean indexing": True, + "data-dependent shapes": True, + # 'max rank' will be part of the 2024.12 standard + # "max rank": 64, + } + + def default_device(self): + """ + The default device used for new NumPy arrays. + + For NumPy, this always returns ``'cpu'``. + + See Also + -------- + __array_namespace_info__.capabilities, + __array_namespace_info__.default_dtypes, + __array_namespace_info__.dtypes, + __array_namespace_info__.devices + + Returns + ------- + device : str + The default device used for new NumPy arrays. + + Examples + -------- + >>> info = np.__array_namespace_info__() + >>> info.default_device() + 'cpu' + + """ + return "cpu" + + def default_dtypes(self, *, device=None): + """ + The default data types used for new NumPy arrays. + + For NumPy, this always returns the following dictionary: + + - **"real floating"**: ``numpy.float64`` + - **"complex floating"**: ``numpy.complex128`` + - **"integral"**: ``numpy.intp`` + - **"indexing"**: ``numpy.intp`` + + Parameters + ---------- + device : str, optional + The device to get the default data types for. For NumPy, only + ``'cpu'`` is allowed. + + Returns + ------- + dtypes : dict + A dictionary describing the default data types used for new NumPy + arrays. + + See Also + -------- + __array_namespace_info__.capabilities, + __array_namespace_info__.default_device, + __array_namespace_info__.dtypes, + __array_namespace_info__.devices + + Examples + -------- + >>> info = np.__array_namespace_info__() + >>> info.default_dtypes() + {'real floating': numpy.float64, + 'complex floating': numpy.complex128, + 'integral': numpy.int64, + 'indexing': numpy.int64} + + """ + if device not in ["cpu", None]: + raise ValueError( + 'Device not understood. Only "cpu" is allowed, but received:' + f' {device}' + ) + return { + "real floating": dtype(float64), + "complex floating": dtype(complex128), + "integral": dtype(intp), + "indexing": dtype(intp), + } + + def dtypes(self, *, device=None, kind=None): + """ + The array API data types supported by NumPy. + + Note that this function only returns data types that are defined by + the array API. + + Parameters + ---------- + device : str, optional + The device to get the data types for. For NumPy, only ``'cpu'`` is + allowed. + kind : str or tuple of str, optional + The kind of data types to return. If ``None``, all data types are + returned. If a string, only data types of that kind are returned. + If a tuple, a dictionary containing the union of the given kinds + is returned. The following kinds are supported: + + - ``'bool'``: boolean data types (i.e., ``bool``). + - ``'signed integer'``: signed integer data types (i.e., ``int8``, + ``int16``, ``int32``, ``int64``). + - ``'unsigned integer'``: unsigned integer data types (i.e., + ``uint8``, ``uint16``, ``uint32``, ``uint64``). + - ``'integral'``: integer data types. Shorthand for ``('signed + integer', 'unsigned integer')``. + - ``'real floating'``: real-valued floating-point data types + (i.e., ``float32``, ``float64``). + - ``'complex floating'``: complex floating-point data types (i.e., + ``complex64``, ``complex128``). + - ``'numeric'``: numeric data types. Shorthand for ``('integral', + 'real floating', 'complex floating')``. + + Returns + ------- + dtypes : dict + A dictionary mapping the names of data types to the corresponding + NumPy data types. + + See Also + -------- + __array_namespace_info__.capabilities, + __array_namespace_info__.default_device, + __array_namespace_info__.default_dtypes, + __array_namespace_info__.devices + + Examples + -------- + >>> info = np.__array_namespace_info__() + >>> info.dtypes(kind='signed integer') + {'int8': numpy.int8, + 'int16': numpy.int16, + 'int32': numpy.int32, + 'int64': numpy.int64} + + """ + if device not in ["cpu", None]: + raise ValueError( + 'Device not understood. Only "cpu" is allowed, but received:' + f' {device}' + ) + if kind is None: + return { + "bool": dtype(bool), + "int8": dtype(int8), + "int16": dtype(int16), + "int32": dtype(int32), + "int64": dtype(int64), + "uint8": dtype(uint8), + "uint16": dtype(uint16), + "uint32": dtype(uint32), + "uint64": dtype(uint64), + "float32": dtype(float32), + "float64": dtype(float64), + "complex64": dtype(complex64), + "complex128": dtype(complex128), + } + if kind == "bool": + return {"bool": bool} + if kind == "signed integer": + return { + "int8": dtype(int8), + "int16": dtype(int16), + "int32": dtype(int32), + "int64": dtype(int64), + } + if kind == "unsigned integer": + return { + "uint8": dtype(uint8), + "uint16": dtype(uint16), + "uint32": dtype(uint32), + "uint64": dtype(uint64), + } + if kind == "integral": + return { + "int8": dtype(int8), + "int16": dtype(int16), + "int32": dtype(int32), + "int64": dtype(int64), + "uint8": dtype(uint8), + "uint16": dtype(uint16), + "uint32": dtype(uint32), + "uint64": dtype(uint64), + } + if kind == "real floating": + return { + "float32": dtype(float32), + "float64": dtype(float64), + } + if kind == "complex floating": + return { + "complex64": dtype(complex64), + "complex128": dtype(complex128), + } + if kind == "numeric": + return { + "int8": dtype(int8), + "int16": dtype(int16), + "int32": dtype(int32), + "int64": dtype(int64), + "uint8": dtype(uint8), + "uint16": dtype(uint16), + "uint32": dtype(uint32), + "uint64": dtype(uint64), + "float32": dtype(float32), + "float64": dtype(float64), + "complex64": dtype(complex64), + "complex128": dtype(complex128), + } + if isinstance(kind, tuple): + res = {} + for k in kind: + res.update(self.dtypes(kind=k)) + return res + raise ValueError(f"unsupported kind: {kind!r}") + + def devices(self): + """ + The devices supported by NumPy. + + For NumPy, this always returns ``['cpu']``. + + Returns + ------- + devices : list of str + The devices supported by NumPy. + + See Also + -------- + __array_namespace_info__.capabilities, + __array_namespace_info__.default_device, + __array_namespace_info__.default_dtypes, + __array_namespace_info__.dtypes + + Examples + -------- + >>> info = np.__array_namespace_info__() + >>> info.devices() + ['cpu'] + + """ + return ["cpu"] diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/numpy/_typing.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/numpy/_typing.py new file mode 100644 index 0000000000000000000000000000000000000000..c5ebb5abb987572be625ee864a37e61126d36d8b --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/numpy/_typing.py @@ -0,0 +1,46 @@ +from __future__ import annotations + +__all__ = [ + "ndarray", + "Device", + "Dtype", +] + +import sys +from typing import ( + Literal, + Union, + TYPE_CHECKING, +) + +from numpy import ( + ndarray, + dtype, + int8, + int16, + int32, + int64, + uint8, + uint16, + uint32, + uint64, + float32, + float64, +) + +Device = Literal["cpu"] +if TYPE_CHECKING or sys.version_info >= (3, 9): + Dtype = dtype[Union[ + int8, + int16, + int32, + int64, + uint8, + uint16, + uint32, + uint64, + float32, + float64, + ]] +else: + Dtype = dtype diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/numpy/fft.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/numpy/fft.py new file mode 100644 index 0000000000000000000000000000000000000000..286675946e0fbb0aa18105d25db08ebbbd2e4d0c --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/numpy/fft.py @@ -0,0 +1,29 @@ +from numpy.fft import * # noqa: F403 +from numpy.fft import __all__ as fft_all + +from ..common import _fft +from .._internal import get_xp + +import numpy as np + +fft = get_xp(np)(_fft.fft) +ifft = get_xp(np)(_fft.ifft) +fftn = get_xp(np)(_fft.fftn) +ifftn = get_xp(np)(_fft.ifftn) +rfft = get_xp(np)(_fft.rfft) +irfft = get_xp(np)(_fft.irfft) +rfftn = get_xp(np)(_fft.rfftn) +irfftn = get_xp(np)(_fft.irfftn) +hfft = get_xp(np)(_fft.hfft) +ihfft = get_xp(np)(_fft.ihfft) +fftfreq = get_xp(np)(_fft.fftfreq) +rfftfreq = get_xp(np)(_fft.rfftfreq) +fftshift = get_xp(np)(_fft.fftshift) +ifftshift = get_xp(np)(_fft.ifftshift) + +__all__ = fft_all + _fft.__all__ + +del get_xp +del np +del fft_all +del _fft diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/numpy/linalg.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/numpy/linalg.py new file mode 100644 index 0000000000000000000000000000000000000000..8f01593bd0ae619b3bea471980b4eeabfc29f319 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/numpy/linalg.py @@ -0,0 +1,90 @@ +from numpy.linalg import * # noqa: F403 +from numpy.linalg import __all__ as linalg_all +import numpy as _np + +from ..common import _linalg +from .._internal import get_xp + +# These functions are in both the main and linalg namespaces +from ._aliases import matmul, matrix_transpose, tensordot, vecdot # noqa: F401 + +import numpy as np + +cross = get_xp(np)(_linalg.cross) +outer = get_xp(np)(_linalg.outer) +EighResult = _linalg.EighResult +QRResult = _linalg.QRResult +SlogdetResult = _linalg.SlogdetResult +SVDResult = _linalg.SVDResult +eigh = get_xp(np)(_linalg.eigh) +qr = get_xp(np)(_linalg.qr) +slogdet = get_xp(np)(_linalg.slogdet) +svd = get_xp(np)(_linalg.svd) +cholesky = get_xp(np)(_linalg.cholesky) +matrix_rank = get_xp(np)(_linalg.matrix_rank) +pinv = get_xp(np)(_linalg.pinv) +matrix_norm = get_xp(np)(_linalg.matrix_norm) +svdvals = get_xp(np)(_linalg.svdvals) +diagonal = get_xp(np)(_linalg.diagonal) +trace = get_xp(np)(_linalg.trace) + +# Note: unlike np.linalg.solve, the array API solve() only accepts x2 as a +# vector when it is exactly 1-dimensional. All other cases treat x2 as a stack +# of matrices. The np.linalg.solve behavior of allowing stacks of both +# matrices and vectors is ambiguous c.f. +# https://github.com/numpy/numpy/issues/15349 and +# https://github.com/data-apis/array-api/issues/285. + +# To workaround this, the below is the code from np.linalg.solve except +# only calling solve1 in the exactly 1D case. + +# This code is here instead of in common because it is numpy specific. Also +# note that CuPy's solve() does not currently support broadcasting (see +# https://github.com/cupy/cupy/blob/main/cupy/cublas.py#L43). +def solve(x1: _np.ndarray, x2: _np.ndarray, /) -> _np.ndarray: + try: + from numpy.linalg._linalg import ( + _makearray, _assert_stacked_2d, _assert_stacked_square, + _commonType, isComplexType, _raise_linalgerror_singular + ) + except ImportError: + from numpy.linalg.linalg import ( + _makearray, _assert_stacked_2d, _assert_stacked_square, + _commonType, isComplexType, _raise_linalgerror_singular + ) + from numpy.linalg import _umath_linalg + + x1, _ = _makearray(x1) + _assert_stacked_2d(x1) + _assert_stacked_square(x1) + x2, wrap = _makearray(x2) + t, result_t = _commonType(x1, x2) + + # This part is different from np.linalg.solve + if x2.ndim == 1: + gufunc = _umath_linalg.solve1 + else: + gufunc = _umath_linalg.solve + + # This does nothing currently but is left in because it will be relevant + # when complex dtype support is added to the spec in 2022. + signature = 'DD->D' if isComplexType(t) else 'dd->d' + with _np.errstate(call=_raise_linalgerror_singular, invalid='call', + over='ignore', divide='ignore', under='ignore'): + r = gufunc(x1, x2, signature=signature) + + return wrap(r.astype(result_t, copy=False)) + +# These functions are completely new here. If the library already has them +# (i.e., numpy 2.0), use the library version instead of our wrapper. +if hasattr(np.linalg, 'vector_norm'): + vector_norm = np.linalg.vector_norm +else: + vector_norm = get_xp(np)(_linalg.vector_norm) + +__all__ = linalg_all + _linalg.__all__ + ['solve'] + +del get_xp +del np +del linalg_all +del _linalg diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/torch/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/torch/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..cfa3acf8945a84c8e3fcdc892edc19d4f674cd30 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/torch/__init__.py @@ -0,0 +1,24 @@ +from torch import * # noqa: F403 + +# Several names are not included in the above import * +import torch +for n in dir(torch): + if (n.startswith('_') + or n.endswith('_') + or 'cuda' in n + or 'cpu' in n + or 'backward' in n): + continue + exec(n + ' = torch.' + n) + +# These imports may overwrite names from the import * above. +from ._aliases import * # noqa: F403 + +# See the comment in the numpy __init__.py +__import__(__package__ + '.linalg') + +__import__(__package__ + '.fft') + +from ..common._helpers import * # noqa: F403 + +__array_api_version__ = '2023.12' diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/torch/_aliases.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/torch/_aliases.py new file mode 100644 index 0000000000000000000000000000000000000000..5ac66bcb17e6f13a51bd6c7fd345bee23c16140f --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/torch/_aliases.py @@ -0,0 +1,752 @@ +from __future__ import annotations + +from functools import wraps as _wraps +from builtins import all as _builtin_all, any as _builtin_any + +from ..common._aliases import (matrix_transpose as _aliases_matrix_transpose, + vecdot as _aliases_vecdot, + clip as _aliases_clip, + unstack as _aliases_unstack, + cumulative_sum as _aliases_cumulative_sum, + ) +from .._internal import get_xp + +from ._info import __array_namespace_info__ + +import torch + +from typing import TYPE_CHECKING +if TYPE_CHECKING: + from typing import List, Optional, Sequence, Tuple, Union + from ..common._typing import Device + from torch import dtype as Dtype + + array = torch.Tensor + +_int_dtypes = { + torch.uint8, + torch.int8, + torch.int16, + torch.int32, + torch.int64, +} + +_array_api_dtypes = { + torch.bool, + *_int_dtypes, + torch.float32, + torch.float64, + torch.complex64, + torch.complex128, +} + +_promotion_table = { + # bool + (torch.bool, torch.bool): torch.bool, + # ints + (torch.int8, torch.int8): torch.int8, + (torch.int8, torch.int16): torch.int16, + (torch.int8, torch.int32): torch.int32, + (torch.int8, torch.int64): torch.int64, + (torch.int16, torch.int8): torch.int16, + (torch.int16, torch.int16): torch.int16, + (torch.int16, torch.int32): torch.int32, + (torch.int16, torch.int64): torch.int64, + (torch.int32, torch.int8): torch.int32, + (torch.int32, torch.int16): torch.int32, + (torch.int32, torch.int32): torch.int32, + (torch.int32, torch.int64): torch.int64, + (torch.int64, torch.int8): torch.int64, + (torch.int64, torch.int16): torch.int64, + (torch.int64, torch.int32): torch.int64, + (torch.int64, torch.int64): torch.int64, + # uints + (torch.uint8, torch.uint8): torch.uint8, + # ints and uints (mixed sign) + (torch.int8, torch.uint8): torch.int16, + (torch.int16, torch.uint8): torch.int16, + (torch.int32, torch.uint8): torch.int32, + (torch.int64, torch.uint8): torch.int64, + (torch.uint8, torch.int8): torch.int16, + (torch.uint8, torch.int16): torch.int16, + (torch.uint8, torch.int32): torch.int32, + (torch.uint8, torch.int64): torch.int64, + # floats + (torch.float32, torch.float32): torch.float32, + (torch.float32, torch.float64): torch.float64, + (torch.float64, torch.float32): torch.float64, + (torch.float64, torch.float64): torch.float64, + # complexes + (torch.complex64, torch.complex64): torch.complex64, + (torch.complex64, torch.complex128): torch.complex128, + (torch.complex128, torch.complex64): torch.complex128, + (torch.complex128, torch.complex128): torch.complex128, + # Mixed float and complex + (torch.float32, torch.complex64): torch.complex64, + (torch.float32, torch.complex128): torch.complex128, + (torch.float64, torch.complex64): torch.complex128, + (torch.float64, torch.complex128): torch.complex128, +} + + +def _two_arg(f): + @_wraps(f) + def _f(x1, x2, /, **kwargs): + x1, x2 = _fix_promotion(x1, x2) + return f(x1, x2, **kwargs) + if _f.__doc__ is None: + _f.__doc__ = f"""\ +Array API compatibility wrapper for torch.{f.__name__}. + +See the corresponding PyTorch documentation and/or the array API specification +for more details. + +""" + return _f + +def _fix_promotion(x1, x2, only_scalar=True): + if not isinstance(x1, torch.Tensor) or not isinstance(x2, torch.Tensor): + return x1, x2 + if x1.dtype not in _array_api_dtypes or x2.dtype not in _array_api_dtypes: + return x1, x2 + # If an argument is 0-D pytorch downcasts the other argument + if not only_scalar or x1.shape == (): + dtype = result_type(x1, x2) + x2 = x2.to(dtype) + if not only_scalar or x2.shape == (): + dtype = result_type(x1, x2) + x1 = x1.to(dtype) + return x1, x2 + +def result_type(*arrays_and_dtypes: Union[array, Dtype]) -> Dtype: + if len(arrays_and_dtypes) == 0: + raise TypeError("At least one array or dtype must be provided") + if len(arrays_and_dtypes) == 1: + x = arrays_and_dtypes[0] + if isinstance(x, torch.dtype): + return x + return x.dtype + if len(arrays_and_dtypes) > 2: + return result_type(arrays_and_dtypes[0], result_type(*arrays_and_dtypes[1:])) + + x, y = arrays_and_dtypes + xdt = x.dtype if not isinstance(x, torch.dtype) else x + ydt = y.dtype if not isinstance(y, torch.dtype) else y + + if (xdt, ydt) in _promotion_table: + return _promotion_table[xdt, ydt] + + # This doesn't result_type(dtype, dtype) for non-array API dtypes + # because torch.result_type only accepts tensors. This does however, allow + # cross-kind promotion. + x = torch.tensor([], dtype=x) if isinstance(x, torch.dtype) else x + y = torch.tensor([], dtype=y) if isinstance(y, torch.dtype) else y + return torch.result_type(x, y) + +def can_cast(from_: Union[Dtype, array], to: Dtype, /) -> bool: + if not isinstance(from_, torch.dtype): + from_ = from_.dtype + return torch.can_cast(from_, to) + +# Basic renames +bitwise_invert = torch.bitwise_not +newaxis = None +# torch.conj sets the conjugation bit, which breaks conversion to other +# libraries. See https://github.com/data-apis/array-api-compat/issues/173 +conj = torch.conj_physical + +# Two-arg elementwise functions +# These require a wrapper to do the correct type promotion on 0-D tensors +add = _two_arg(torch.add) +atan2 = _two_arg(torch.atan2) +bitwise_and = _two_arg(torch.bitwise_and) +bitwise_left_shift = _two_arg(torch.bitwise_left_shift) +bitwise_or = _two_arg(torch.bitwise_or) +bitwise_right_shift = _two_arg(torch.bitwise_right_shift) +bitwise_xor = _two_arg(torch.bitwise_xor) +copysign = _two_arg(torch.copysign) +divide = _two_arg(torch.divide) +# Also a rename. torch.equal does not broadcast +equal = _two_arg(torch.eq) +floor_divide = _two_arg(torch.floor_divide) +greater = _two_arg(torch.greater) +greater_equal = _two_arg(torch.greater_equal) +hypot = _two_arg(torch.hypot) +less = _two_arg(torch.less) +less_equal = _two_arg(torch.less_equal) +logaddexp = _two_arg(torch.logaddexp) +# logical functions are not included here because they only accept bool in the +# spec, so type promotion is irrelevant. +maximum = _two_arg(torch.maximum) +minimum = _two_arg(torch.minimum) +multiply = _two_arg(torch.multiply) +not_equal = _two_arg(torch.not_equal) +pow = _two_arg(torch.pow) +remainder = _two_arg(torch.remainder) +subtract = _two_arg(torch.subtract) + +# These wrappers are mostly based on the fact that pytorch uses 'dim' instead +# of 'axis'. + +# torch.min and torch.max return a tuple and don't support multiple axes https://github.com/pytorch/pytorch/issues/58745 +def max(x: array, /, *, axis: Optional[Union[int, Tuple[int, ...]]] = None, keepdims: bool = False) -> array: + # https://github.com/pytorch/pytorch/issues/29137 + if axis == (): + return torch.clone(x) + return torch.amax(x, axis, keepdims=keepdims) + +def min(x: array, /, *, axis: Optional[Union[int, Tuple[int, ...]]] = None, keepdims: bool = False) -> array: + # https://github.com/pytorch/pytorch/issues/29137 + if axis == (): + return torch.clone(x) + return torch.amin(x, axis, keepdims=keepdims) + +clip = get_xp(torch)(_aliases_clip) +unstack = get_xp(torch)(_aliases_unstack) +cumulative_sum = get_xp(torch)(_aliases_cumulative_sum) + +# torch.sort also returns a tuple +# https://github.com/pytorch/pytorch/issues/70921 +def sort(x: array, /, *, axis: int = -1, descending: bool = False, stable: bool = True, **kwargs) -> array: + return torch.sort(x, dim=axis, descending=descending, stable=stable, **kwargs).values + +def _normalize_axes(axis, ndim): + axes = [] + if ndim == 0 and axis: + # Better error message in this case + raise IndexError(f"Dimension out of range: {axis[0]}") + lower, upper = -ndim, ndim - 1 + for a in axis: + if a < lower or a > upper: + # Match torch error message (e.g., from sum()) + raise IndexError(f"Dimension out of range (expected to be in range of [{lower}, {upper}], but got {a}") + if a < 0: + a = a + ndim + if a in axes: + # Use IndexError instead of RuntimeError, and "axis" instead of "dim" + raise IndexError(f"Axis {a} appears multiple times in the list of axes") + axes.append(a) + return sorted(axes) + +def _axis_none_keepdims(x, ndim, keepdims): + # Apply keepdims when axis=None + # (https://github.com/pytorch/pytorch/issues/71209) + # Note that this is only valid for the axis=None case. + if keepdims: + for i in range(ndim): + x = torch.unsqueeze(x, 0) + return x + +def _reduce_multiple_axes(f, x, axis, keepdims=False, **kwargs): + # Some reductions don't support multiple axes + # (https://github.com/pytorch/pytorch/issues/56586). + axes = _normalize_axes(axis, x.ndim) + for a in reversed(axes): + x = torch.movedim(x, a, -1) + x = torch.flatten(x, -len(axes)) + + out = f(x, -1, **kwargs) + + if keepdims: + for a in axes: + out = torch.unsqueeze(out, a) + return out + +def prod(x: array, + /, + *, + axis: Optional[Union[int, Tuple[int, ...]]] = None, + dtype: Optional[Dtype] = None, + keepdims: bool = False, + **kwargs) -> array: + x = torch.asarray(x) + ndim = x.ndim + + # https://github.com/pytorch/pytorch/issues/29137. Separate from the logic + # below because it still needs to upcast. + if axis == (): + if dtype is None: + # We can't upcast uint8 according to the spec because there is no + # torch.uint64, so at least upcast to int64 which is what sum does + # when axis=None. + if x.dtype in [torch.int8, torch.int16, torch.int32, torch.uint8]: + return x.to(torch.int64) + return x.clone() + return x.to(dtype) + + # torch.prod doesn't support multiple axes + # (https://github.com/pytorch/pytorch/issues/56586). + if isinstance(axis, tuple): + return _reduce_multiple_axes(torch.prod, x, axis, keepdims=keepdims, dtype=dtype, **kwargs) + if axis is None: + # torch doesn't support keepdims with axis=None + # (https://github.com/pytorch/pytorch/issues/71209) + res = torch.prod(x, dtype=dtype, **kwargs) + res = _axis_none_keepdims(res, ndim, keepdims) + return res + + return torch.prod(x, axis, dtype=dtype, keepdims=keepdims, **kwargs) + + +def sum(x: array, + /, + *, + axis: Optional[Union[int, Tuple[int, ...]]] = None, + dtype: Optional[Dtype] = None, + keepdims: bool = False, + **kwargs) -> array: + x = torch.asarray(x) + ndim = x.ndim + + # https://github.com/pytorch/pytorch/issues/29137. + # Make sure it upcasts. + if axis == (): + if dtype is None: + # We can't upcast uint8 according to the spec because there is no + # torch.uint64, so at least upcast to int64 which is what sum does + # when axis=None. + if x.dtype in [torch.int8, torch.int16, torch.int32, torch.uint8]: + return x.to(torch.int64) + return x.clone() + return x.to(dtype) + + if axis is None: + # torch doesn't support keepdims with axis=None + # (https://github.com/pytorch/pytorch/issues/71209) + res = torch.sum(x, dtype=dtype, **kwargs) + res = _axis_none_keepdims(res, ndim, keepdims) + return res + + return torch.sum(x, axis, dtype=dtype, keepdims=keepdims, **kwargs) + +def any(x: array, + /, + *, + axis: Optional[Union[int, Tuple[int, ...]]] = None, + keepdims: bool = False, + **kwargs) -> array: + x = torch.asarray(x) + ndim = x.ndim + if axis == (): + return x.to(torch.bool) + # torch.any doesn't support multiple axes + # (https://github.com/pytorch/pytorch/issues/56586). + if isinstance(axis, tuple): + res = _reduce_multiple_axes(torch.any, x, axis, keepdims=keepdims, **kwargs) + return res.to(torch.bool) + if axis is None: + # torch doesn't support keepdims with axis=None + # (https://github.com/pytorch/pytorch/issues/71209) + res = torch.any(x, **kwargs) + res = _axis_none_keepdims(res, ndim, keepdims) + return res.to(torch.bool) + + # torch.any doesn't return bool for uint8 + return torch.any(x, axis, keepdims=keepdims).to(torch.bool) + +def all(x: array, + /, + *, + axis: Optional[Union[int, Tuple[int, ...]]] = None, + keepdims: bool = False, + **kwargs) -> array: + x = torch.asarray(x) + ndim = x.ndim + if axis == (): + return x.to(torch.bool) + # torch.all doesn't support multiple axes + # (https://github.com/pytorch/pytorch/issues/56586). + if isinstance(axis, tuple): + res = _reduce_multiple_axes(torch.all, x, axis, keepdims=keepdims, **kwargs) + return res.to(torch.bool) + if axis is None: + # torch doesn't support keepdims with axis=None + # (https://github.com/pytorch/pytorch/issues/71209) + res = torch.all(x, **kwargs) + res = _axis_none_keepdims(res, ndim, keepdims) + return res.to(torch.bool) + + # torch.all doesn't return bool for uint8 + return torch.all(x, axis, keepdims=keepdims).to(torch.bool) + +def mean(x: array, + /, + *, + axis: Optional[Union[int, Tuple[int, ...]]] = None, + keepdims: bool = False, + **kwargs) -> array: + # https://github.com/pytorch/pytorch/issues/29137 + if axis == (): + return torch.clone(x) + if axis is None: + # torch doesn't support keepdims with axis=None + # (https://github.com/pytorch/pytorch/issues/71209) + res = torch.mean(x, **kwargs) + res = _axis_none_keepdims(res, x.ndim, keepdims) + return res + return torch.mean(x, axis, keepdims=keepdims, **kwargs) + +def std(x: array, + /, + *, + axis: Optional[Union[int, Tuple[int, ...]]] = None, + correction: Union[int, float] = 0.0, + keepdims: bool = False, + **kwargs) -> array: + # Note, float correction is not supported + # https://github.com/pytorch/pytorch/issues/61492. We don't try to + # implement it here for now. + + if isinstance(correction, float): + _correction = int(correction) + if correction != _correction: + raise NotImplementedError("float correction in torch std() is not yet supported") + else: + _correction = correction + + # https://github.com/pytorch/pytorch/issues/29137 + if axis == (): + return torch.zeros_like(x) + if isinstance(axis, int): + axis = (axis,) + if axis is None: + # torch doesn't support keepdims with axis=None + # (https://github.com/pytorch/pytorch/issues/71209) + res = torch.std(x, tuple(range(x.ndim)), correction=_correction, **kwargs) + res = _axis_none_keepdims(res, x.ndim, keepdims) + return res + return torch.std(x, axis, correction=_correction, keepdims=keepdims, **kwargs) + +def var(x: array, + /, + *, + axis: Optional[Union[int, Tuple[int, ...]]] = None, + correction: Union[int, float] = 0.0, + keepdims: bool = False, + **kwargs) -> array: + # Note, float correction is not supported + # https://github.com/pytorch/pytorch/issues/61492. We don't try to + # implement it here for now. + + # if isinstance(correction, float): + # correction = int(correction) + + # https://github.com/pytorch/pytorch/issues/29137 + if axis == (): + return torch.zeros_like(x) + if isinstance(axis, int): + axis = (axis,) + if axis is None: + # torch doesn't support keepdims with axis=None + # (https://github.com/pytorch/pytorch/issues/71209) + res = torch.var(x, tuple(range(x.ndim)), correction=correction, **kwargs) + res = _axis_none_keepdims(res, x.ndim, keepdims) + return res + return torch.var(x, axis, correction=correction, keepdims=keepdims, **kwargs) + +# torch.concat doesn't support dim=None +# https://github.com/pytorch/pytorch/issues/70925 +def concat(arrays: Union[Tuple[array, ...], List[array]], + /, + *, + axis: Optional[int] = 0, + **kwargs) -> array: + if axis is None: + arrays = tuple(ar.flatten() for ar in arrays) + axis = 0 + return torch.concat(arrays, axis, **kwargs) + +# torch.squeeze only accepts int dim and doesn't require it +# https://github.com/pytorch/pytorch/issues/70924. Support for tuple dim was +# added at https://github.com/pytorch/pytorch/pull/89017. +def squeeze(x: array, /, axis: Union[int, Tuple[int, ...]]) -> array: + if isinstance(axis, int): + axis = (axis,) + for a in axis: + if x.shape[a] != 1: + raise ValueError("squeezed dimensions must be equal to 1") + axes = _normalize_axes(axis, x.ndim) + # Remove this once pytorch 1.14 is released with the above PR #89017. + sequence = [a - i for i, a in enumerate(axes)] + for a in sequence: + x = torch.squeeze(x, a) + return x + +# torch.broadcast_to uses size instead of shape +def broadcast_to(x: array, /, shape: Tuple[int, ...], **kwargs) -> array: + return torch.broadcast_to(x, shape, **kwargs) + +# torch.permute uses dims instead of axes +def permute_dims(x: array, /, axes: Tuple[int, ...]) -> array: + return torch.permute(x, axes) + +# The axis parameter doesn't work for flip() and roll() +# https://github.com/pytorch/pytorch/issues/71210. Also torch.flip() doesn't +# accept axis=None +def flip(x: array, /, *, axis: Optional[Union[int, Tuple[int, ...]]] = None, **kwargs) -> array: + if axis is None: + axis = tuple(range(x.ndim)) + # torch.flip doesn't accept dim as an int but the method does + # https://github.com/pytorch/pytorch/issues/18095 + return x.flip(axis, **kwargs) + +def roll(x: array, /, shift: Union[int, Tuple[int, ...]], *, axis: Optional[Union[int, Tuple[int, ...]]] = None, **kwargs) -> array: + return torch.roll(x, shift, axis, **kwargs) + +def nonzero(x: array, /, **kwargs) -> Tuple[array, ...]: + if x.ndim == 0: + raise ValueError("nonzero() does not support zero-dimensional arrays") + return torch.nonzero(x, as_tuple=True, **kwargs) + +def where(condition: array, x1: array, x2: array, /) -> array: + x1, x2 = _fix_promotion(x1, x2) + return torch.where(condition, x1, x2) + +# torch.reshape doesn't have the copy keyword +def reshape(x: array, + /, + shape: Tuple[int, ...], + copy: Optional[bool] = None, + **kwargs) -> array: + if copy is not None: + raise NotImplementedError("torch.reshape doesn't yet support the copy keyword") + return torch.reshape(x, shape, **kwargs) + +# torch.arange doesn't support returning empty arrays +# (https://github.com/pytorch/pytorch/issues/70915), and doesn't support some +# keyword argument combinations +# (https://github.com/pytorch/pytorch/issues/70914) +def arange(start: Union[int, float], + /, + stop: Optional[Union[int, float]] = None, + step: Union[int, float] = 1, + *, + dtype: Optional[Dtype] = None, + device: Optional[Device] = None, + **kwargs) -> array: + if stop is None: + start, stop = 0, start + if step > 0 and stop <= start or step < 0 and stop >= start: + if dtype is None: + if _builtin_all(isinstance(i, int) for i in [start, stop, step]): + dtype = torch.int64 + else: + dtype = torch.float32 + return torch.empty(0, dtype=dtype, device=device, **kwargs) + return torch.arange(start, stop, step, dtype=dtype, device=device, **kwargs) + +# torch.eye does not accept None as a default for the second argument and +# doesn't support off-diagonals (https://github.com/pytorch/pytorch/issues/70910) +def eye(n_rows: int, + n_cols: Optional[int] = None, + /, + *, + k: int = 0, + dtype: Optional[Dtype] = None, + device: Optional[Device] = None, + **kwargs) -> array: + if n_cols is None: + n_cols = n_rows + z = torch.zeros(n_rows, n_cols, dtype=dtype, device=device, **kwargs) + if abs(k) <= n_rows + n_cols: + z.diagonal(k).fill_(1) + return z + +# torch.linspace doesn't have the endpoint parameter +def linspace(start: Union[int, float], + stop: Union[int, float], + /, + num: int, + *, + dtype: Optional[Dtype] = None, + device: Optional[Device] = None, + endpoint: bool = True, + **kwargs) -> array: + if not endpoint: + return torch.linspace(start, stop, num+1, dtype=dtype, device=device, **kwargs)[:-1] + return torch.linspace(start, stop, num, dtype=dtype, device=device, **kwargs) + +# torch.full does not accept an int size +# https://github.com/pytorch/pytorch/issues/70906 +def full(shape: Union[int, Tuple[int, ...]], + fill_value: Union[bool, int, float, complex], + *, + dtype: Optional[Dtype] = None, + device: Optional[Device] = None, + **kwargs) -> array: + if isinstance(shape, int): + shape = (shape,) + + return torch.full(shape, fill_value, dtype=dtype, device=device, **kwargs) + +# ones, zeros, and empty do not accept shape as a keyword argument +def ones(shape: Union[int, Tuple[int, ...]], + *, + dtype: Optional[Dtype] = None, + device: Optional[Device] = None, + **kwargs) -> array: + return torch.ones(shape, dtype=dtype, device=device, **kwargs) + +def zeros(shape: Union[int, Tuple[int, ...]], + *, + dtype: Optional[Dtype] = None, + device: Optional[Device] = None, + **kwargs) -> array: + return torch.zeros(shape, dtype=dtype, device=device, **kwargs) + +def empty(shape: Union[int, Tuple[int, ...]], + *, + dtype: Optional[Dtype] = None, + device: Optional[Device] = None, + **kwargs) -> array: + return torch.empty(shape, dtype=dtype, device=device, **kwargs) + +# tril and triu do not call the keyword argument k + +def tril(x: array, /, *, k: int = 0) -> array: + return torch.tril(x, k) + +def triu(x: array, /, *, k: int = 0) -> array: + return torch.triu(x, k) + +# Functions that aren't in torch https://github.com/pytorch/pytorch/issues/58742 +def expand_dims(x: array, /, *, axis: int = 0) -> array: + return torch.unsqueeze(x, axis) + +def astype(x: array, dtype: Dtype, /, *, copy: bool = True) -> array: + return x.to(dtype, copy=copy) + +def broadcast_arrays(*arrays: array) -> List[array]: + shape = torch.broadcast_shapes(*[a.shape for a in arrays]) + return [torch.broadcast_to(a, shape) for a in arrays] + +# Note that these named tuples aren't actually part of the standard namespace, +# but I don't see any issue with exporting the names here regardless. +from ..common._aliases import (UniqueAllResult, UniqueCountsResult, + UniqueInverseResult) + +# https://github.com/pytorch/pytorch/issues/70920 +def unique_all(x: array) -> UniqueAllResult: + # torch.unique doesn't support returning indices. + # https://github.com/pytorch/pytorch/issues/36748. The workaround + # suggested in that issue doesn't actually function correctly (it relies + # on non-deterministic behavior of scatter()). + raise NotImplementedError("unique_all() not yet implemented for pytorch (see https://github.com/pytorch/pytorch/issues/36748)") + + # values, inverse_indices, counts = torch.unique(x, return_counts=True, return_inverse=True) + # # torch.unique incorrectly gives a 0 count for nan values. + # # https://github.com/pytorch/pytorch/issues/94106 + # counts[torch.isnan(values)] = 1 + # return UniqueAllResult(values, indices, inverse_indices, counts) + +def unique_counts(x: array) -> UniqueCountsResult: + values, counts = torch.unique(x, return_counts=True) + + # torch.unique incorrectly gives a 0 count for nan values. + # https://github.com/pytorch/pytorch/issues/94106 + counts[torch.isnan(values)] = 1 + return UniqueCountsResult(values, counts) + +def unique_inverse(x: array) -> UniqueInverseResult: + values, inverse = torch.unique(x, return_inverse=True) + return UniqueInverseResult(values, inverse) + +def unique_values(x: array) -> array: + return torch.unique(x) + +def matmul(x1: array, x2: array, /, **kwargs) -> array: + # torch.matmul doesn't type promote (but differently from _fix_promotion) + x1, x2 = _fix_promotion(x1, x2, only_scalar=False) + return torch.matmul(x1, x2, **kwargs) + +matrix_transpose = get_xp(torch)(_aliases_matrix_transpose) +_vecdot = get_xp(torch)(_aliases_vecdot) + +def vecdot(x1: array, x2: array, /, *, axis: int = -1) -> array: + x1, x2 = _fix_promotion(x1, x2, only_scalar=False) + return _vecdot(x1, x2, axis=axis) + +# torch.tensordot uses dims instead of axes +def tensordot(x1: array, x2: array, /, *, axes: Union[int, Tuple[Sequence[int], Sequence[int]]] = 2, **kwargs) -> array: + # Note: torch.tensordot fails with integer dtypes when there is only 1 + # element in the axis (https://github.com/pytorch/pytorch/issues/84530). + x1, x2 = _fix_promotion(x1, x2, only_scalar=False) + return torch.tensordot(x1, x2, dims=axes, **kwargs) + + +def isdtype( + dtype: Dtype, kind: Union[Dtype, str, Tuple[Union[Dtype, str], ...]], + *, _tuple=True, # Disallow nested tuples +) -> bool: + """ + Returns a boolean indicating whether a provided dtype is of a specified data type ``kind``. + + Note that outside of this function, this compat library does not yet fully + support complex numbers. + + See + https://data-apis.org/array-api/latest/API_specification/generated/array_api.isdtype.html + for more details + """ + if isinstance(kind, tuple) and _tuple: + return _builtin_any(isdtype(dtype, k, _tuple=False) for k in kind) + elif isinstance(kind, str): + if kind == 'bool': + return dtype == torch.bool + elif kind == 'signed integer': + return dtype in _int_dtypes and dtype.is_signed + elif kind == 'unsigned integer': + return dtype in _int_dtypes and not dtype.is_signed + elif kind == 'integral': + return dtype in _int_dtypes + elif kind == 'real floating': + return dtype.is_floating_point + elif kind == 'complex floating': + return dtype.is_complex + elif kind == 'numeric': + return isdtype(dtype, ('integral', 'real floating', 'complex floating')) + else: + raise ValueError(f"Unrecognized data type kind: {kind!r}") + else: + return dtype == kind + +def take(x: array, indices: array, /, *, axis: Optional[int] = None, **kwargs) -> array: + if axis is None: + if x.ndim != 1: + raise ValueError("axis must be specified when ndim > 1") + axis = 0 + return torch.index_select(x, axis, indices, **kwargs) + +def sign(x: array, /) -> array: + # torch sign() does not support complex numbers and does not propagate + # nans. See https://github.com/data-apis/array-api-compat/issues/136 + if x.dtype.is_complex: + out = x/torch.abs(x) + # sign(0) = 0 but the above formula would give nan + out[x == 0+0j] = 0+0j + return out + else: + out = torch.sign(x) + if x.dtype.is_floating_point: + out[torch.isnan(x)] = torch.nan + return out + + +__all__ = ['__array_namespace_info__', 'result_type', 'can_cast', + 'permute_dims', 'bitwise_invert', 'newaxis', 'conj', 'add', + 'atan2', 'bitwise_and', 'bitwise_left_shift', 'bitwise_or', + 'bitwise_right_shift', 'bitwise_xor', 'copysign', 'divide', + 'equal', 'floor_divide', 'greater', 'greater_equal', 'hypot', + 'less', 'less_equal', 'logaddexp', 'maximum', 'minimum', + 'multiply', 'not_equal', 'pow', 'remainder', 'subtract', 'max', + 'min', 'clip', 'unstack', 'cumulative_sum', 'sort', 'prod', 'sum', + 'any', 'all', 'mean', 'std', 'var', 'concat', 'squeeze', + 'broadcast_to', 'flip', 'roll', 'nonzero', 'where', 'reshape', + 'arange', 'eye', 'linspace', 'full', 'ones', 'zeros', 'empty', + 'tril', 'triu', 'expand_dims', 'astype', 'broadcast_arrays', + 'UniqueAllResult', 'UniqueCountsResult', 'UniqueInverseResult', + 'unique_all', 'unique_counts', 'unique_inverse', 'unique_values', + 'matmul', 'matrix_transpose', 'vecdot', 'tensordot', 'isdtype', + 'take', 'sign'] + +_all_ignore = ['torch', 'get_xp'] diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/torch/_info.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/torch/_info.py new file mode 100644 index 0000000000000000000000000000000000000000..264caa9e5fbbe9da3d9b9594b8d11f313d8536ef --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/torch/_info.py @@ -0,0 +1,358 @@ +""" +Array API Inspection namespace + +This is the namespace for inspection functions as defined by the array API +standard. See +https://data-apis.org/array-api/latest/API_specification/inspection.html for +more details. + +""" +import torch + +from functools import cache + +class __array_namespace_info__: + """ + Get the array API inspection namespace for PyTorch. + + The array API inspection namespace defines the following functions: + + - capabilities() + - default_device() + - default_dtypes() + - dtypes() + - devices() + + See + https://data-apis.org/array-api/latest/API_specification/inspection.html + for more details. + + Returns + ------- + info : ModuleType + The array API inspection namespace for PyTorch. + + Examples + -------- + >>> info = np.__array_namespace_info__() + >>> info.default_dtypes() + {'real floating': numpy.float64, + 'complex floating': numpy.complex128, + 'integral': numpy.int64, + 'indexing': numpy.int64} + + """ + + __module__ = 'torch' + + def capabilities(self): + """ + Return a dictionary of array API library capabilities. + + The resulting dictionary has the following keys: + + - **"boolean indexing"**: boolean indicating whether an array library + supports boolean indexing. Always ``True`` for PyTorch. + + - **"data-dependent shapes"**: boolean indicating whether an array + library supports data-dependent output shapes. Always ``True`` for + PyTorch. + + See + https://data-apis.org/array-api/latest/API_specification/generated/array_api.info.capabilities.html + for more details. + + See Also + -------- + __array_namespace_info__.default_device, + __array_namespace_info__.default_dtypes, + __array_namespace_info__.dtypes, + __array_namespace_info__.devices + + Returns + ------- + capabilities : dict + A dictionary of array API library capabilities. + + Examples + -------- + >>> info = np.__array_namespace_info__() + >>> info.capabilities() + {'boolean indexing': True, + 'data-dependent shapes': True} + + """ + return { + "boolean indexing": True, + "data-dependent shapes": True, + # 'max rank' will be part of the 2024.12 standard + # "max rank": 64, + } + + def default_device(self): + """ + The default device used for new PyTorch arrays. + + See Also + -------- + __array_namespace_info__.capabilities, + __array_namespace_info__.default_dtypes, + __array_namespace_info__.dtypes, + __array_namespace_info__.devices + + Returns + ------- + device : str + The default device used for new PyTorch arrays. + + Examples + -------- + >>> info = np.__array_namespace_info__() + >>> info.default_device() + 'cpu' + + """ + return torch.device("cpu") + + def default_dtypes(self, *, device=None): + """ + The default data types used for new PyTorch arrays. + + Parameters + ---------- + device : str, optional + The device to get the default data types for. For PyTorch, only + ``'cpu'`` is allowed. + + Returns + ------- + dtypes : dict + A dictionary describing the default data types used for new PyTorch + arrays. + + See Also + -------- + __array_namespace_info__.capabilities, + __array_namespace_info__.default_device, + __array_namespace_info__.dtypes, + __array_namespace_info__.devices + + Examples + -------- + >>> info = np.__array_namespace_info__() + >>> info.default_dtypes() + {'real floating': torch.float32, + 'complex floating': torch.complex64, + 'integral': torch.int64, + 'indexing': torch.int64} + + """ + # Note: if the default is set to float64, the devices like MPS that + # don't support float64 will error. We still return the default_dtype + # value here because this error doesn't represent a different default + # per-device. + default_floating = torch.get_default_dtype() + default_complex = torch.complex64 if default_floating == torch.float32 else torch.complex128 + default_integral = torch.int64 + return { + "real floating": default_floating, + "complex floating": default_complex, + "integral": default_integral, + "indexing": default_integral, + } + + + def _dtypes(self, kind): + bool = torch.bool + int8 = torch.int8 + int16 = torch.int16 + int32 = torch.int32 + int64 = torch.int64 + uint8 = torch.uint8 + # uint16, uint32, and uint64 are present in newer versions of pytorch, + # but they aren't generally supported by the array API functions, so + # we omit them from this function. + float32 = torch.float32 + float64 = torch.float64 + complex64 = torch.complex64 + complex128 = torch.complex128 + + if kind is None: + return { + "bool": bool, + "int8": int8, + "int16": int16, + "int32": int32, + "int64": int64, + "uint8": uint8, + "float32": float32, + "float64": float64, + "complex64": complex64, + "complex128": complex128, + } + if kind == "bool": + return {"bool": bool} + if kind == "signed integer": + return { + "int8": int8, + "int16": int16, + "int32": int32, + "int64": int64, + } + if kind == "unsigned integer": + return { + "uint8": uint8, + } + if kind == "integral": + return { + "int8": int8, + "int16": int16, + "int32": int32, + "int64": int64, + "uint8": uint8, + } + if kind == "real floating": + return { + "float32": float32, + "float64": float64, + } + if kind == "complex floating": + return { + "complex64": complex64, + "complex128": complex128, + } + if kind == "numeric": + return { + "int8": int8, + "int16": int16, + "int32": int32, + "int64": int64, + "uint8": uint8, + "float32": float32, + "float64": float64, + "complex64": complex64, + "complex128": complex128, + } + if isinstance(kind, tuple): + res = {} + for k in kind: + res.update(self.dtypes(kind=k)) + return res + raise ValueError(f"unsupported kind: {kind!r}") + + @cache + def dtypes(self, *, device=None, kind=None): + """ + The array API data types supported by PyTorch. + + Note that this function only returns data types that are defined by + the array API. + + Parameters + ---------- + device : str, optional + The device to get the data types for. + kind : str or tuple of str, optional + The kind of data types to return. If ``None``, all data types are + returned. If a string, only data types of that kind are returned. + If a tuple, a dictionary containing the union of the given kinds + is returned. The following kinds are supported: + + - ``'bool'``: boolean data types (i.e., ``bool``). + - ``'signed integer'``: signed integer data types (i.e., ``int8``, + ``int16``, ``int32``, ``int64``). + - ``'unsigned integer'``: unsigned integer data types (i.e., + ``uint8``, ``uint16``, ``uint32``, ``uint64``). + - ``'integral'``: integer data types. Shorthand for ``('signed + integer', 'unsigned integer')``. + - ``'real floating'``: real-valued floating-point data types + (i.e., ``float32``, ``float64``). + - ``'complex floating'``: complex floating-point data types (i.e., + ``complex64``, ``complex128``). + - ``'numeric'``: numeric data types. Shorthand for ``('integral', + 'real floating', 'complex floating')``. + + Returns + ------- + dtypes : dict + A dictionary mapping the names of data types to the corresponding + PyTorch data types. + + See Also + -------- + __array_namespace_info__.capabilities, + __array_namespace_info__.default_device, + __array_namespace_info__.default_dtypes, + __array_namespace_info__.devices + + Examples + -------- + >>> info = np.__array_namespace_info__() + >>> info.dtypes(kind='signed integer') + {'int8': numpy.int8, + 'int16': numpy.int16, + 'int32': numpy.int32, + 'int64': numpy.int64} + + """ + res = self._dtypes(kind) + for k, v in res.copy().items(): + try: + torch.empty((0,), dtype=v, device=device) + except: + del res[k] + return res + + @cache + def devices(self): + """ + The devices supported by PyTorch. + + Returns + ------- + devices : list of str + The devices supported by PyTorch. + + See Also + -------- + __array_namespace_info__.capabilities, + __array_namespace_info__.default_device, + __array_namespace_info__.default_dtypes, + __array_namespace_info__.dtypes + + Examples + -------- + >>> info = np.__array_namespace_info__() + >>> info.devices() + [device(type='cpu'), device(type='mps', index=0), device(type='meta')] + + """ + # Torch doesn't have a straightforward way to get the list of all + # currently supported devices. To do this, we first parse the error + # message of torch.device to get the list of all possible types of + # device: + try: + torch.device('notadevice') + except RuntimeError as e: + # The error message is something like: + # "Expected one of cpu, cuda, ipu, xpu, mkldnn, opengl, opencl, ideep, hip, ve, fpga, ort, xla, lazy, vulkan, mps, meta, hpu, mtia, privateuseone device type at start of device string: notadevice" + devices_names = e.args[0].split('Expected one of ')[1].split(' device type')[0].split(', ') + + # Next we need to check for different indices for different devices. + # device(device_name, index=index) doesn't actually check if the + # device name or index is valid. We have to try to create a tensor + # with it (which is why this function is cached). + devices = [] + for device_name in devices_names: + i = 0 + while True: + try: + a = torch.empty((0,), device=torch.device(device_name, index=i)) + if a.device in devices: + break + devices.append(a.device) + except: + break + i += 1 + + return devices diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/torch/fft.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/torch/fft.py new file mode 100644 index 0000000000000000000000000000000000000000..3c9117ee57d3534e3e72329d740632c02e936200 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/torch/fft.py @@ -0,0 +1,86 @@ +from __future__ import annotations + +from typing import TYPE_CHECKING +if TYPE_CHECKING: + import torch + array = torch.Tensor + from typing import Union, Sequence, Literal + +from torch.fft import * # noqa: F403 +import torch.fft + +# Several torch fft functions do not map axes to dim + +def fftn( + x: array, + /, + *, + s: Sequence[int] = None, + axes: Sequence[int] = None, + norm: Literal["backward", "ortho", "forward"] = "backward", + **kwargs, +) -> array: + return torch.fft.fftn(x, s=s, dim=axes, norm=norm, **kwargs) + +def ifftn( + x: array, + /, + *, + s: Sequence[int] = None, + axes: Sequence[int] = None, + norm: Literal["backward", "ortho", "forward"] = "backward", + **kwargs, +) -> array: + return torch.fft.ifftn(x, s=s, dim=axes, norm=norm, **kwargs) + +def rfftn( + x: array, + /, + *, + s: Sequence[int] = None, + axes: Sequence[int] = None, + norm: Literal["backward", "ortho", "forward"] = "backward", + **kwargs, +) -> array: + return torch.fft.rfftn(x, s=s, dim=axes, norm=norm, **kwargs) + +def irfftn( + x: array, + /, + *, + s: Sequence[int] = None, + axes: Sequence[int] = None, + norm: Literal["backward", "ortho", "forward"] = "backward", + **kwargs, +) -> array: + return torch.fft.irfftn(x, s=s, dim=axes, norm=norm, **kwargs) + +def fftshift( + x: array, + /, + *, + axes: Union[int, Sequence[int]] = None, + **kwargs, +) -> array: + return torch.fft.fftshift(x, dim=axes, **kwargs) + +def ifftshift( + x: array, + /, + *, + axes: Union[int, Sequence[int]] = None, + **kwargs, +) -> array: + return torch.fft.ifftshift(x, dim=axes, **kwargs) + + +__all__ = torch.fft.__all__ + [ + "fftn", + "ifftn", + "rfftn", + "irfftn", + "fftshift", + "ifftshift", +] + +_all_ignore = ['torch'] diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/torch/linalg.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/torch/linalg.py new file mode 100644 index 0000000000000000000000000000000000000000..e26198b9b562ed307206dd08dd9de7c8aa2a918b --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/torch/linalg.py @@ -0,0 +1,121 @@ +from __future__ import annotations + +from typing import TYPE_CHECKING +if TYPE_CHECKING: + import torch + array = torch.Tensor + from torch import dtype as Dtype + from typing import Optional, Union, Tuple, Literal + inf = float('inf') + +from ._aliases import _fix_promotion, sum + +from torch.linalg import * # noqa: F403 + +# torch.linalg doesn't define __all__ +# from torch.linalg import __all__ as linalg_all +from torch import linalg as torch_linalg +linalg_all = [i for i in dir(torch_linalg) if not i.startswith('_')] + +# outer is implemented in torch but aren't in the linalg namespace +from torch import outer +# These functions are in both the main and linalg namespaces +from ._aliases import matmul, matrix_transpose, tensordot + +# Note: torch.linalg.cross does not default to axis=-1 (it defaults to the +# first axis with size 3), see https://github.com/pytorch/pytorch/issues/58743 + +# torch.cross also does not support broadcasting when it would add new +# dimensions https://github.com/pytorch/pytorch/issues/39656 +def cross(x1: array, x2: array, /, *, axis: int = -1) -> array: + x1, x2 = _fix_promotion(x1, x2, only_scalar=False) + if not (-min(x1.ndim, x2.ndim) <= axis < max(x1.ndim, x2.ndim)): + raise ValueError(f"axis {axis} out of bounds for cross product of arrays with shapes {x1.shape} and {x2.shape}") + if not (x1.shape[axis] == x2.shape[axis] == 3): + raise ValueError(f"cross product axis must have size 3, got {x1.shape[axis]} and {x2.shape[axis]}") + x1, x2 = torch.broadcast_tensors(x1, x2) + return torch_linalg.cross(x1, x2, dim=axis) + +def vecdot(x1: array, x2: array, /, *, axis: int = -1, **kwargs) -> array: + from ._aliases import isdtype + + x1, x2 = _fix_promotion(x1, x2, only_scalar=False) + + # torch.linalg.vecdot incorrectly allows broadcasting along the contracted dimension + if x1.shape[axis] != x2.shape[axis]: + raise ValueError("x1 and x2 must have the same size along the given axis") + + # torch.linalg.vecdot doesn't support integer dtypes + if isdtype(x1.dtype, 'integral') or isdtype(x2.dtype, 'integral'): + if kwargs: + raise RuntimeError("vecdot kwargs not supported for integral dtypes") + + x1_ = torch.moveaxis(x1, axis, -1) + x2_ = torch.moveaxis(x2, axis, -1) + x1_, x2_ = torch.broadcast_tensors(x1_, x2_) + + res = x1_[..., None, :] @ x2_[..., None] + return res[..., 0, 0] + return torch.linalg.vecdot(x1, x2, dim=axis, **kwargs) + +def solve(x1: array, x2: array, /, **kwargs) -> array: + x1, x2 = _fix_promotion(x1, x2, only_scalar=False) + # Torch tries to emulate NumPy 1 solve behavior by using batched 1-D solve + # whenever + # 1. x1.ndim - 1 == x2.ndim + # 2. x1.shape[:-1] == x2.shape + # + # See linalg_solve_is_vector_rhs in + # aten/src/ATen/native/LinearAlgebraUtils.h and + # TORCH_META_FUNC(_linalg_solve_ex) in + # aten/src/ATen/native/BatchLinearAlgebra.cpp in the PyTorch source code. + # + # The easiest way to work around this is to prepend a size 1 dimension to + # x2, since x2 is already one dimension less than x1. + # + # See https://github.com/pytorch/pytorch/issues/52915 + if x2.ndim != 1 and x1.ndim - 1 == x2.ndim and x1.shape[:-1] == x2.shape: + x2 = x2[None] + return torch.linalg.solve(x1, x2, **kwargs) + +# torch.trace doesn't support the offset argument and doesn't support stacking +def trace(x: array, /, *, offset: int = 0, dtype: Optional[Dtype] = None) -> array: + # Use our wrapped sum to make sure it does upcasting correctly + return sum(torch.diagonal(x, offset=offset, dim1=-2, dim2=-1), axis=-1, dtype=dtype) + +def vector_norm( + x: array, + /, + *, + axis: Optional[Union[int, Tuple[int, ...]]] = None, + keepdims: bool = False, + ord: Union[int, float, Literal[inf, -inf]] = 2, + **kwargs, +) -> array: + # torch.vector_norm incorrectly treats axis=() the same as axis=None + if axis == (): + out = kwargs.get('out') + if out is None: + dtype = None + if x.dtype == torch.complex64: + dtype = torch.float32 + elif x.dtype == torch.complex128: + dtype = torch.float64 + + out = torch.zeros_like(x, dtype=dtype) + + # The norm of a single scalar works out to abs(x) in every case except + # for ord=0, which is x != 0. + if ord == 0: + out[:] = (x != 0) + else: + out[:] = torch.abs(x) + return out + return torch.linalg.vector_norm(x, ord=ord, axis=axis, keepdim=keepdims, **kwargs) + +__all__ = linalg_all + ['outer', 'matmul', 'matrix_transpose', 'tensordot', + 'cross', 'vecdot', 'solve', 'trace', 'vector_norm'] + +_all_ignore = ['torch_linalg', 'sum'] + +del linalg_all diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_extra/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_extra/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..2062f7d5d6a4d9f5a3556164720a6abc4da456bc --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_extra/__init__.py @@ -0,0 +1,15 @@ +from __future__ import annotations + +from ._funcs import atleast_nd, cov, create_diagonal, expand_dims, kron, sinc + +__version__ = "0.2.0" + +__all__ = [ + "__version__", + "atleast_nd", + "cov", + "create_diagonal", + "expand_dims", + "kron", + "sinc", +] diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_extra/_funcs.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_extra/_funcs.py new file mode 100644 index 0000000000000000000000000000000000000000..ce800189b46d25316c3123a22ce4ff2e7e1e81ce --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_extra/_funcs.py @@ -0,0 +1,484 @@ +from __future__ import annotations + +import warnings +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from ._typing import Array, ModuleType + +__all__ = ["atleast_nd", "cov", "create_diagonal", "expand_dims", "kron", "sinc"] + + +def atleast_nd(x: Array, /, *, ndim: int, xp: ModuleType) -> Array: + """ + Recursively expand the dimension of an array to at least `ndim`. + + Parameters + ---------- + x : array + ndim : int + The minimum number of dimensions for the result. + xp : array_namespace + The standard-compatible namespace for `x`. + + Returns + ------- + res : array + An array with ``res.ndim`` >= `ndim`. + If ``x.ndim`` >= `ndim`, `x` is returned. + If ``x.ndim`` < `ndim`, `x` is expanded by prepending new axes + until ``res.ndim`` equals `ndim`. + + Examples + -------- + >>> import array_api_strict as xp + >>> import array_api_extra as xpx + >>> x = xp.asarray([1]) + >>> xpx.atleast_nd(x, ndim=3, xp=xp) + Array([[[1]]], dtype=array_api_strict.int64) + + >>> x = xp.asarray([[[1, 2], + ... [3, 4]]]) + >>> xpx.atleast_nd(x, ndim=1, xp=xp) is x + True + + """ + if x.ndim < ndim: + x = xp.expand_dims(x, axis=0) + x = atleast_nd(x, ndim=ndim, xp=xp) + return x + + +def cov(m: Array, /, *, xp: ModuleType) -> Array: + """ + Estimate a covariance matrix. + + Covariance indicates the level to which two variables vary together. + If we examine N-dimensional samples, :math:`X = [x_1, x_2, ... x_N]^T`, + then the covariance matrix element :math:`C_{ij}` is the covariance of + :math:`x_i` and :math:`x_j`. The element :math:`C_{ii}` is the variance + of :math:`x_i`. + + This provides a subset of the functionality of ``numpy.cov``. + + Parameters + ---------- + m : array + A 1-D or 2-D array containing multiple variables and observations. + Each row of `m` represents a variable, and each column a single + observation of all those variables. + xp : array_namespace + The standard-compatible namespace for `m`. + + Returns + ------- + res : array + The covariance matrix of the variables. + + Examples + -------- + >>> import array_api_strict as xp + >>> import array_api_extra as xpx + + Consider two variables, :math:`x_0` and :math:`x_1`, which + correlate perfectly, but in opposite directions: + + >>> x = xp.asarray([[0, 2], [1, 1], [2, 0]]).T + >>> x + Array([[0, 1, 2], + [2, 1, 0]], dtype=array_api_strict.int64) + + Note how :math:`x_0` increases while :math:`x_1` decreases. The covariance + matrix shows this clearly: + + >>> xpx.cov(x, xp=xp) + Array([[ 1., -1.], + [-1., 1.]], dtype=array_api_strict.float64) + + + Note that element :math:`C_{0,1}`, which shows the correlation between + :math:`x_0` and :math:`x_1`, is negative. + + Further, note how `x` and `y` are combined: + + >>> x = xp.asarray([-2.1, -1, 4.3]) + >>> y = xp.asarray([3, 1.1, 0.12]) + >>> X = xp.stack((x, y), axis=0) + >>> xpx.cov(X, xp=xp) + Array([[11.71 , -4.286 ], + [-4.286 , 2.14413333]], dtype=array_api_strict.float64) + + >>> xpx.cov(x, xp=xp) + Array(11.71, dtype=array_api_strict.float64) + + >>> xpx.cov(y, xp=xp) + Array(2.14413333, dtype=array_api_strict.float64) + + """ + m = xp.asarray(m, copy=True) + dtype = ( + xp.float64 if xp.isdtype(m.dtype, "integral") else xp.result_type(m, xp.float64) + ) + + m = atleast_nd(m, ndim=2, xp=xp) + m = xp.astype(m, dtype) + + avg = _mean(m, axis=1, xp=xp) + fact = m.shape[1] - 1 + + if fact <= 0: + warnings.warn("Degrees of freedom <= 0 for slice", RuntimeWarning, stacklevel=2) + fact = 0.0 + + m -= avg[:, None] + m_transpose = m.T + if xp.isdtype(m_transpose.dtype, "complex floating"): + m_transpose = xp.conj(m_transpose) + c = m @ m_transpose + c /= fact + axes = tuple(axis for axis, length in enumerate(c.shape) if length == 1) + return xp.squeeze(c, axis=axes) + + +def create_diagonal(x: Array, /, *, offset: int = 0, xp: ModuleType) -> Array: + """ + Construct a diagonal array. + + Parameters + ---------- + x : array + A 1-D array + offset : int, optional + Offset from the leading diagonal (default is ``0``). + Use positive ints for diagonals above the leading diagonal, + and negative ints for diagonals below the leading diagonal. + xp : array_namespace + The standard-compatible namespace for `x`. + + Returns + ------- + res : array + A 2-D array with `x` on the diagonal (offset by `offset`). + + Examples + -------- + >>> import array_api_strict as xp + >>> import array_api_extra as xpx + >>> x = xp.asarray([2, 4, 8]) + + >>> xpx.create_diagonal(x, xp=xp) + Array([[2, 0, 0], + [0, 4, 0], + [0, 0, 8]], dtype=array_api_strict.int64) + + >>> xpx.create_diagonal(x, offset=-2, xp=xp) + Array([[0, 0, 0, 0, 0], + [0, 0, 0, 0, 0], + [2, 0, 0, 0, 0], + [0, 4, 0, 0, 0], + [0, 0, 8, 0, 0]], dtype=array_api_strict.int64) + + """ + if x.ndim != 1: + err_msg = "`x` must be 1-dimensional." + raise ValueError(err_msg) + n = x.shape[0] + abs(offset) + diag = xp.zeros(n**2, dtype=x.dtype) + i = offset if offset >= 0 else abs(offset) * n + diag[i : min(n * (n - offset), diag.shape[0]) : n + 1] = x + return xp.reshape(diag, (n, n)) + + +def _mean( + x: Array, + /, + *, + axis: int | tuple[int, ...] | None = None, + keepdims: bool = False, + xp: ModuleType, +) -> Array: + """ + Complex mean, https://github.com/data-apis/array-api/issues/846. + """ + if xp.isdtype(x.dtype, "complex floating"): + x_real = xp.real(x) + x_imag = xp.imag(x) + mean_real = xp.mean(x_real, axis=axis, keepdims=keepdims) + mean_imag = xp.mean(x_imag, axis=axis, keepdims=keepdims) + return mean_real + (mean_imag * xp.asarray(1j)) + return xp.mean(x, axis=axis, keepdims=keepdims) + + +def expand_dims( + a: Array, /, *, axis: int | tuple[int, ...] = (0,), xp: ModuleType +) -> Array: + """ + Expand the shape of an array. + + Insert (a) new axis/axes that will appear at the position(s) specified by + `axis` in the expanded array shape. + + This is ``xp.expand_dims`` for `axis` an int *or a tuple of ints*. + Roughly equivalent to ``numpy.expand_dims`` for NumPy arrays. + + Parameters + ---------- + a : array + axis : int or tuple of ints, optional + Position(s) in the expanded axes where the new axis (or axes) is/are placed. + If multiple positions are provided, they should be unique (note that a position + given by a positive index could also be referred to by a negative index - + that will also result in an error). + Default: ``(0,)``. + xp : array_namespace + The standard-compatible namespace for `a`. + + Returns + ------- + res : array + `a` with an expanded shape. + + Examples + -------- + >>> import array_api_strict as xp + >>> import array_api_extra as xpx + >>> x = xp.asarray([1, 2]) + >>> x.shape + (2,) + + The following is equivalent to ``x[xp.newaxis, :]`` or ``x[xp.newaxis]``: + + >>> y = xpx.expand_dims(x, axis=0, xp=xp) + >>> y + Array([[1, 2]], dtype=array_api_strict.int64) + >>> y.shape + (1, 2) + + The following is equivalent to ``x[:, xp.newaxis]``: + + >>> y = xpx.expand_dims(x, axis=1, xp=xp) + >>> y + Array([[1], + [2]], dtype=array_api_strict.int64) + >>> y.shape + (2, 1) + + ``axis`` may also be a tuple: + + >>> y = xpx.expand_dims(x, axis=(0, 1), xp=xp) + >>> y + Array([[[1, 2]]], dtype=array_api_strict.int64) + + >>> y = xpx.expand_dims(x, axis=(2, 0), xp=xp) + >>> y + Array([[[1], + [2]]], dtype=array_api_strict.int64) + + """ + if not isinstance(axis, tuple): + axis = (axis,) + ndim = a.ndim + len(axis) + if axis != () and (min(axis) < -ndim or max(axis) >= ndim): + err_msg = ( + f"a provided axis position is out of bounds for array of dimension {a.ndim}" + ) + raise IndexError(err_msg) + axis = tuple(dim % ndim for dim in axis) + if len(set(axis)) != len(axis): + err_msg = "Duplicate dimensions specified in `axis`." + raise ValueError(err_msg) + for i in sorted(axis): + a = xp.expand_dims(a, axis=i) + return a + + +def kron(a: Array, b: Array, /, *, xp: ModuleType) -> Array: + """ + Kronecker product of two arrays. + + Computes the Kronecker product, a composite array made of blocks of the + second array scaled by the first. + + Equivalent to ``numpy.kron`` for NumPy arrays. + + Parameters + ---------- + a, b : array + xp : array_namespace + The standard-compatible namespace for `a` and `b`. + + Returns + ------- + res : array + The Kronecker product of `a` and `b`. + + Notes + ----- + The function assumes that the number of dimensions of `a` and `b` + are the same, if necessary prepending the smallest with ones. + If ``a.shape = (r0,r1,..,rN)`` and ``b.shape = (s0,s1,...,sN)``, + the Kronecker product has shape ``(r0*s0, r1*s1, ..., rN*SN)``. + The elements are products of elements from `a` and `b`, organized + explicitly by:: + + kron(a,b)[k0,k1,...,kN] = a[i0,i1,...,iN] * b[j0,j1,...,jN] + + where:: + + kt = it * st + jt, t = 0,...,N + + In the common 2-D case (N=1), the block structure can be visualized:: + + [[ a[0,0]*b, a[0,1]*b, ... , a[0,-1]*b ], + [ ... ... ], + [ a[-1,0]*b, a[-1,1]*b, ... , a[-1,-1]*b ]] + + + Examples + -------- + >>> import array_api_strict as xp + >>> import array_api_extra as xpx + >>> xpx.kron(xp.asarray([1, 10, 100]), xp.asarray([5, 6, 7]), xp=xp) + Array([ 5, 6, 7, 50, 60, 70, 500, + 600, 700], dtype=array_api_strict.int64) + + >>> xpx.kron(xp.asarray([5, 6, 7]), xp.asarray([1, 10, 100]), xp=xp) + Array([ 5, 50, 500, 6, 60, 600, 7, + 70, 700], dtype=array_api_strict.int64) + + >>> xpx.kron(xp.eye(2), xp.ones((2, 2)), xp=xp) + Array([[1., 1., 0., 0.], + [1., 1., 0., 0.], + [0., 0., 1., 1.], + [0., 0., 1., 1.]], dtype=array_api_strict.float64) + + + >>> a = xp.reshape(xp.arange(100), (2, 5, 2, 5)) + >>> b = xp.reshape(xp.arange(24), (2, 3, 4)) + >>> c = xpx.kron(a, b, xp=xp) + >>> c.shape + (2, 10, 6, 20) + >>> I = (1, 3, 0, 2) + >>> J = (0, 2, 1) + >>> J1 = (0,) + J # extend to ndim=4 + >>> S1 = (1,) + b.shape + >>> K = tuple(xp.asarray(I) * xp.asarray(S1) + xp.asarray(J1)) + >>> c[K] == a[I]*b[J] + Array(True, dtype=array_api_strict.bool) + + """ + + b = xp.asarray(b) + singletons = (1,) * (b.ndim - a.ndim) + a = xp.broadcast_to(xp.asarray(a), singletons + a.shape) + + nd_b, nd_a = b.ndim, a.ndim + nd_max = max(nd_b, nd_a) + if nd_a == 0 or nd_b == 0: + return xp.multiply(a, b) + + a_shape = a.shape + b_shape = b.shape + + # Equalise the shapes by prepending smaller one with 1s + a_shape = (1,) * max(0, nd_b - nd_a) + a_shape + b_shape = (1,) * max(0, nd_a - nd_b) + b_shape + + # Insert empty dimensions + a_arr = expand_dims(a, axis=tuple(range(nd_b - nd_a)), xp=xp) + b_arr = expand_dims(b, axis=tuple(range(nd_a - nd_b)), xp=xp) + + # Compute the product + a_arr = expand_dims(a_arr, axis=tuple(range(1, nd_max * 2, 2)), xp=xp) + b_arr = expand_dims(b_arr, axis=tuple(range(0, nd_max * 2, 2)), xp=xp) + result = xp.multiply(a_arr, b_arr) + + # Reshape back and return + a_shape = xp.asarray(a_shape) + b_shape = xp.asarray(b_shape) + return xp.reshape(result, tuple(xp.multiply(a_shape, b_shape))) + + +def sinc(x: Array, /, *, xp: ModuleType) -> Array: + r""" + Return the normalized sinc function. + + The sinc function is equal to :math:`\sin(\pi x)/(\pi x)` for any argument + :math:`x\ne 0`. ``sinc(0)`` takes the limit value 1, making ``sinc`` not + only everywhere continuous but also infinitely differentiable. + + .. note:: + + Note the normalization factor of ``pi`` used in the definition. + This is the most commonly used definition in signal processing. + Use ``sinc(x / xp.pi)`` to obtain the unnormalized sinc function + :math:`\sin(x)/x` that is more common in mathematics. + + Parameters + ---------- + x : array + Array (possibly multi-dimensional) of values for which to calculate + ``sinc(x)``. Must have a real floating point dtype. + xp : array_namespace + The standard-compatible namespace for `x`. + + Returns + ------- + res : array + ``sinc(x)`` calculated elementwise, which has the same shape as the input. + + Notes + ----- + The name sinc is short for "sine cardinal" or "sinus cardinalis". + + The sinc function is used in various signal processing applications, + including in anti-aliasing, in the construction of a Lanczos resampling + filter, and in interpolation. + + For bandlimited interpolation of discrete-time signals, the ideal + interpolation kernel is proportional to the sinc function. + + References + ---------- + .. [1] Weisstein, Eric W. "Sinc Function." From MathWorld--A Wolfram Web + Resource. https://mathworld.wolfram.com/SincFunction.html + .. [2] Wikipedia, "Sinc function", + https://en.wikipedia.org/wiki/Sinc_function + + Examples + -------- + >>> import array_api_strict as xp + >>> import array_api_extra as xpx + >>> x = xp.linspace(-4, 4, 41) + >>> xpx.sinc(x, xp=xp) + Array([-3.89817183e-17, -4.92362781e-02, + -8.40918587e-02, -8.90384387e-02, + -5.84680802e-02, 3.89817183e-17, + 6.68206631e-02, 1.16434881e-01, + 1.26137788e-01, 8.50444803e-02, + -3.89817183e-17, -1.03943254e-01, + -1.89206682e-01, -2.16236208e-01, + -1.55914881e-01, 3.89817183e-17, + 2.33872321e-01, 5.04551152e-01, + 7.56826729e-01, 9.35489284e-01, + 1.00000000e+00, 9.35489284e-01, + 7.56826729e-01, 5.04551152e-01, + 2.33872321e-01, 3.89817183e-17, + -1.55914881e-01, -2.16236208e-01, + -1.89206682e-01, -1.03943254e-01, + -3.89817183e-17, 8.50444803e-02, + 1.26137788e-01, 1.16434881e-01, + 6.68206631e-02, 3.89817183e-17, + -5.84680802e-02, -8.90384387e-02, + -8.40918587e-02, -4.92362781e-02, + -3.89817183e-17], dtype=array_api_strict.float64) + + """ + if not xp.isdtype(x.dtype, "real floating"): + err_msg = "`x` must have a real floating data type." + raise ValueError(err_msg) + # no scalars in `where` - array-api#807 + y = xp.pi * xp.where( + x, x, xp.asarray(xp.finfo(x.dtype).smallest_normal, dtype=x.dtype) + ) + return xp.sin(y) / y diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_extra/_typing.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_extra/_typing.py new file mode 100644 index 0000000000000000000000000000000000000000..9ffa13f23fc8c52abf5c65206ec1ff5a8481832c --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_extra/_typing.py @@ -0,0 +1,8 @@ +from __future__ import annotations + +from types import ModuleType +from typing import Any + +Array = Any # To be changed to a Protocol later (see array-api#589) + +__all__ = ["Array", "ModuleType"] diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e2418395425d7ecce9e1a4da68985c8fde93bc1c --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/__init__.py @@ -0,0 +1,20 @@ +from .main import minimize +from .utils import show_versions + +# PEP0440 compatible formatted version, see: +# https://www.python.org/dev/peps/pep-0440/ +# +# Final release markers: +# X.Y.0 # For first release after an increment in Y +# X.Y.Z # For bugfix releases +# +# Admissible pre-release markers: +# X.YaN # Alpha release +# X.YbN # Beta release +# X.YrcN # Release Candidate +# +# Dev branch marker is: 'X.Y.dev' or 'X.Y.devN' where N is an integer. +# 'X.Y.dev0' is the canonical version of 'X.Y.dev'. +__version__ = "1.1.2" + +__all__ = ["minimize", "show_versions"] diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/framework.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/framework.py new file mode 100644 index 0000000000000000000000000000000000000000..9afea66281067e27a486ff317a4bffa03ec3e68b --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/framework.py @@ -0,0 +1,1240 @@ +import warnings + +import numpy as np +from scipy.optimize import lsq_linear + +from .models import Models, Quadratic +from .settings import Options, Constants +from .subsolvers import ( + cauchy_geometry, + spider_geometry, + normal_byrd_omojokun, + tangential_byrd_omojokun, + constrained_tangential_byrd_omojokun, +) +from .subsolvers.optim import qr_tangential_byrd_omojokun +from .utils import get_arrays_tol + + +TINY = np.finfo(float).tiny +EPS = np.finfo(float).eps + + +class TrustRegion: + """ + Trust-region framework. + """ + + def __init__(self, pb, options, constants): + """ + Initialize the trust-region framework. + + Parameters + ---------- + pb : `cobyqa.problem.Problem` + Problem to solve. + options : dict + Options of the solver. + constants : dict + Constants of the solver. + + Raises + ------ + `cobyqa.utils.MaxEvalError` + If the maximum number of evaluations is reached. + `cobyqa.utils.TargetSuccess` + If a nearly feasible point has been found with an objective + function value below the target. + `cobyqa.utils.FeasibleSuccess` + If a feasible point has been found for a feasibility problem. + `numpy.linalg.LinAlgError` + If the initial interpolation system is ill-defined. + """ + # Set the initial penalty parameter. + self._penalty = 0.0 + + # Initialize the models. + self._pb = pb + self._models = Models(self._pb, options, self.penalty) + self._constants = constants + + # Set the index of the best interpolation point. + self._best_index = 0 + self.set_best_index() + + # Set the initial Lagrange multipliers. + self._lm_linear_ub = np.zeros(self.m_linear_ub) + self._lm_linear_eq = np.zeros(self.m_linear_eq) + self._lm_nonlinear_ub = np.zeros(self.m_nonlinear_ub) + self._lm_nonlinear_eq = np.zeros(self.m_nonlinear_eq) + self.set_multipliers(self.x_best) + + # Set the initial trust-region radius and the resolution. + self._resolution = options[Options.RHOBEG] + self._radius = self.resolution + + @property + def n(self): + """ + Number of variables. + + Returns + ------- + int + Number of variables. + """ + return self._pb.n + + @property + def m_linear_ub(self): + """ + Number of linear inequality constraints. + + Returns + ------- + int + Number of linear inequality constraints. + """ + return self._pb.m_linear_ub + + @property + def m_linear_eq(self): + """ + Number of linear equality constraints. + + Returns + ------- + int + Number of linear equality constraints. + """ + return self._pb.m_linear_eq + + @property + def m_nonlinear_ub(self): + """ + Number of nonlinear inequality constraints. + + Returns + ------- + int + Number of nonlinear inequality constraints. + """ + return self._pb.m_nonlinear_ub + + @property + def m_nonlinear_eq(self): + """ + Number of nonlinear equality constraints. + + Returns + ------- + int + Number of nonlinear equality constraints. + """ + return self._pb.m_nonlinear_eq + + @property + def radius(self): + """ + Trust-region radius. + + Returns + ------- + float + Trust-region radius. + """ + return self._radius + + @radius.setter + def radius(self, radius): + """ + Set the trust-region radius. + + Parameters + ---------- + radius : float + New trust-region radius. + """ + self._radius = radius + if ( + self.radius + <= self._constants[Constants.DECREASE_RADIUS_THRESHOLD] + * self.resolution + ): + self._radius = self.resolution + + @property + def resolution(self): + """ + Resolution of the trust-region framework. + + The resolution is a lower bound on the trust-region radius. + + Returns + ------- + float + Resolution of the trust-region framework. + """ + return self._resolution + + @resolution.setter + def resolution(self, resolution): + """ + Set the resolution of the trust-region framework. + + Parameters + ---------- + resolution : float + New resolution of the trust-region framework. + """ + self._resolution = resolution + + @property + def penalty(self): + """ + Penalty parameter. + + Returns + ------- + float + Penalty parameter. + """ + return self._penalty + + @property + def models(self): + """ + Models of the objective function and constraints. + + Returns + ------- + `cobyqa.models.Models` + Models of the objective function and constraints. + """ + return self._models + + @property + def best_index(self): + """ + Index of the best interpolation point. + + Returns + ------- + int + Index of the best interpolation point. + """ + return self._best_index + + @property + def x_best(self): + """ + Best interpolation point. + + Its value is interpreted as relative to the origin, not the base point. + + Returns + ------- + `numpy.ndarray` + Best interpolation point. + """ + return self.models.interpolation.point(self.best_index) + + @property + def fun_best(self): + """ + Value of the objective function at `x_best`. + + Returns + ------- + float + Value of the objective function at `x_best`. + """ + return self.models.fun_val[self.best_index] + + @property + def cub_best(self): + """ + Values of the nonlinear inequality constraints at `x_best`. + + Returns + ------- + `numpy.ndarray`, shape (m_nonlinear_ub,) + Values of the nonlinear inequality constraints at `x_best`. + """ + return self.models.cub_val[self.best_index, :] + + @property + def ceq_best(self): + """ + Values of the nonlinear equality constraints at `x_best`. + + Returns + ------- + `numpy.ndarray`, shape (m_nonlinear_eq,) + Values of the nonlinear equality constraints at `x_best`. + """ + return self.models.ceq_val[self.best_index, :] + + def lag_model(self, x): + """ + Evaluate the Lagrangian model at a given point. + + Parameters + ---------- + x : `numpy.ndarray`, shape (n,) + Point at which the Lagrangian model is evaluated. + + Returns + ------- + float + Value of the Lagrangian model at `x`. + """ + return ( + self.models.fun(x) + + self._lm_linear_ub + @ (self._pb.linear.a_ub @ x - self._pb.linear.b_ub) + + self._lm_linear_eq + @ (self._pb.linear.a_eq @ x - self._pb.linear.b_eq) + + self._lm_nonlinear_ub @ self.models.cub(x) + + self._lm_nonlinear_eq @ self.models.ceq(x) + ) + + def lag_model_grad(self, x): + """ + Evaluate the gradient of the Lagrangian model at a given point. + + Parameters + ---------- + x : `numpy.ndarray`, shape (n,) + Point at which the gradient of the Lagrangian model is evaluated. + + Returns + ------- + `numpy.ndarray`, shape (n,) + Gradient of the Lagrangian model at `x`. + """ + return ( + self.models.fun_grad(x) + + self._lm_linear_ub @ self._pb.linear.a_ub + + self._lm_linear_eq @ self._pb.linear.a_eq + + self._lm_nonlinear_ub @ self.models.cub_grad(x) + + self._lm_nonlinear_eq @ self.models.ceq_grad(x) + ) + + def lag_model_hess(self): + """ + Evaluate the Hessian matrix of the Lagrangian model at a given point. + + Returns + ------- + `numpy.ndarray`, shape (n, n) + Hessian matrix of the Lagrangian model at `x`. + """ + hess = self.models.fun_hess() + if self.m_nonlinear_ub > 0: + hess += self._lm_nonlinear_ub @ self.models.cub_hess() + if self.m_nonlinear_eq > 0: + hess += self._lm_nonlinear_eq @ self.models.ceq_hess() + return hess + + def lag_model_hess_prod(self, v): + """ + Evaluate the right product of the Hessian matrix of the Lagrangian + model with a given vector. + + Parameters + ---------- + v : `numpy.ndarray`, shape (n,) + Vector with which the Hessian matrix of the Lagrangian model is + multiplied from the right. + + Returns + ------- + `numpy.ndarray`, shape (n,) + Right product of the Hessian matrix of the Lagrangian model with + `v`. + """ + return ( + self.models.fun_hess_prod(v) + + self._lm_nonlinear_ub @ self.models.cub_hess_prod(v) + + self._lm_nonlinear_eq @ self.models.ceq_hess_prod(v) + ) + + def lag_model_curv(self, v): + """ + Evaluate the curvature of the Lagrangian model along a given direction. + + Parameters + ---------- + v : `numpy.ndarray`, shape (n,) + Direction along which the curvature of the Lagrangian model is + evaluated. + + Returns + ------- + float + Curvature of the Lagrangian model along `v`. + """ + return ( + self.models.fun_curv(v) + + self._lm_nonlinear_ub @ self.models.cub_curv(v) + + self._lm_nonlinear_eq @ self.models.ceq_curv(v) + ) + + def sqp_fun(self, step): + """ + Evaluate the objective function of the SQP subproblem. + + Parameters + ---------- + step : `numpy.ndarray`, shape (n,) + Step along which the objective function of the SQP subproblem is + evaluated. + + Returns + ------- + float + Value of the objective function of the SQP subproblem along `step`. + """ + return step @ ( + self.models.fun_grad(self.x_best) + + 0.5 * self.lag_model_hess_prod(step) + ) + + def sqp_cub(self, step): + """ + Evaluate the linearization of the nonlinear inequality constraints. + + Parameters + ---------- + step : `numpy.ndarray`, shape (n,) + Step along which the linearization of the nonlinear inequality + constraints is evaluated. + + Returns + ------- + `numpy.ndarray`, shape (m_nonlinear_ub,) + Value of the linearization of the nonlinear inequality constraints + along `step`. + """ + return ( + self.models.cub(self.x_best) + + self.models.cub_grad(self.x_best) @ step + ) + + def sqp_ceq(self, step): + """ + Evaluate the linearization of the nonlinear equality constraints. + + Parameters + ---------- + step : `numpy.ndarray`, shape (n,) + Step along which the linearization of the nonlinear equality + constraints is evaluated. + + Returns + ------- + `numpy.ndarray`, shape (m_nonlinear_ub,) + Value of the linearization of the nonlinear equality constraints + along `step`. + """ + return ( + self.models.ceq(self.x_best) + + self.models.ceq_grad(self.x_best) @ step + ) + + def merit(self, x, fun_val=None, cub_val=None, ceq_val=None): + """ + Evaluate the merit function at a given point. + + Parameters + ---------- + x : `numpy.ndarray`, shape (n,) + Point at which the merit function is evaluated. + fun_val : float, optional + Value of the objective function at `x`. If not provided, the + objective function is evaluated at `x`. + cub_val : `numpy.ndarray`, shape (m_nonlinear_ub,), optional + Values of the nonlinear inequality constraints. If not provided, + the nonlinear inequality constraints are evaluated at `x`. + ceq_val : `numpy.ndarray`, shape (m_nonlinear_eq,), optional + Values of the nonlinear equality constraints. If not provided, + the nonlinear equality constraints are evaluated at `x`. + + Returns + ------- + float + Value of the merit function at `x`. + """ + if fun_val is None or cub_val is None or ceq_val is None: + fun_val, cub_val, ceq_val = self._pb(x, self.penalty) + m_val = fun_val + if self._penalty > 0.0: + c_val = self._pb.violation(x, cub_val=cub_val, ceq_val=ceq_val) + if np.count_nonzero(c_val): + m_val += self._penalty * np.linalg.norm(c_val) + return m_val + + def get_constraint_linearizations(self, x): + """ + Get the linearizations of the constraints at a given point. + + Parameters + ---------- + x : `numpy.ndarray`, shape (n,) + Point at which the linearizations of the constraints are evaluated. + + Returns + ------- + `numpy.ndarray`, shape (m_linear_ub + m_nonlinear_ub, n) + Left-hand side matrix of the linearized inequality constraints. + `numpy.ndarray`, shape (m_linear_ub + m_nonlinear_ub,) + Right-hand side vector of the linearized inequality constraints. + `numpy.ndarray`, shape (m_linear_eq + m_nonlinear_eq, n) + Left-hand side matrix of the linearized equality constraints. + `numpy.ndarray`, shape (m_linear_eq + m_nonlinear_eq,) + Right-hand side vector of the linearized equality constraints. + """ + aub = np.block( + [ + [self._pb.linear.a_ub], + [self.models.cub_grad(x)], + ] + ) + bub = np.block( + [ + self._pb.linear.b_ub - self._pb.linear.a_ub @ x, + -self.models.cub(x), + ] + ) + aeq = np.block( + [ + [self._pb.linear.a_eq], + [self.models.ceq_grad(x)], + ] + ) + beq = np.block( + [ + self._pb.linear.b_eq - self._pb.linear.a_eq @ x, + -self.models.ceq(x), + ] + ) + return aub, bub, aeq, beq + + def get_trust_region_step(self, options): + """ + Get the trust-region step. + + The trust-region step is computed by solving the derivative-free + trust-region SQP subproblem using a Byrd-Omojokun composite-step + approach. For more details, see Section 5.2.3 of [1]_. + + Parameters + ---------- + options : dict + Options of the solver. + + Returns + ------- + `numpy.ndarray`, shape (n,) + Normal step. + `numpy.ndarray`, shape (n,) + Tangential step. + + References + ---------- + .. [1] T. M. Ragonneau. *Model-Based Derivative-Free Optimization + Methods and Software*. PhD thesis, Department of Applied + Mathematics, The Hong Kong Polytechnic University, Hong Kong, China, + 2022. URL: https://theses.lib.polyu.edu.hk/handle/200/12294. + """ + # Evaluate the linearizations of the constraints. + aub, bub, aeq, beq = self.get_constraint_linearizations(self.x_best) + xl = self._pb.bounds.xl - self.x_best + xu = self._pb.bounds.xu - self.x_best + + # Evaluate the normal step. + radius = self._constants[Constants.BYRD_OMOJOKUN_FACTOR] * self.radius + normal_step = normal_byrd_omojokun( + aub, + bub, + aeq, + beq, + xl, + xu, + radius, + options[Options.DEBUG], + **self._constants, + ) + if options[Options.DEBUG]: + tol = get_arrays_tol(xl, xu) + if (np.any(normal_step + tol < xl) + or np.any(xu < normal_step - tol)): + warnings.warn( + "the normal step does not respect the bound constraint.", + RuntimeWarning, + 2, + ) + if np.linalg.norm(normal_step) > 1.1 * radius: + warnings.warn( + "the normal step does not respect the trust-region " + "constraint.", + RuntimeWarning, + 2, + ) + + # Evaluate the tangential step. + radius = np.sqrt(self.radius**2.0 - normal_step @ normal_step) + xl -= normal_step + xu -= normal_step + bub = np.maximum(bub - aub @ normal_step, 0.0) + g_best = self.models.fun_grad(self.x_best) + self.lag_model_hess_prod( + normal_step + ) + if self._pb.type in ["unconstrained", "bound-constrained"]: + tangential_step = tangential_byrd_omojokun( + g_best, + self.lag_model_hess_prod, + xl, + xu, + radius, + options[Options.DEBUG], + **self._constants, + ) + else: + tangential_step = constrained_tangential_byrd_omojokun( + g_best, + self.lag_model_hess_prod, + xl, + xu, + aub, + bub, + aeq, + radius, + options["debug"], + **self._constants, + ) + if options[Options.DEBUG]: + tol = get_arrays_tol(xl, xu) + if np.any(tangential_step + tol < xl) or np.any( + xu < tangential_step - tol + ): + warnings.warn( + "The tangential step does not respect the bound " + "constraints.", + RuntimeWarning, + 2, + ) + if ( + np.linalg.norm(normal_step + tangential_step) + > 1.1 * np.sqrt(2.0) * self.radius + ): + warnings.warn( + "The trial step does not respect the trust-region " + "constraint.", + RuntimeWarning, + 2, + ) + return normal_step, tangential_step + + def get_geometry_step(self, k_new, options): + """ + Get the geometry-improving step. + + Three different geometry-improving steps are computed and the best one + is returned. For more details, see Section 5.2.7 of [1]_. + + Parameters + ---------- + k_new : int + Index of the interpolation point to be modified. + options : dict + Options of the solver. + + Returns + ------- + `numpy.ndarray`, shape (n,) + Geometry-improving step. + + Raises + ------ + `numpy.linalg.LinAlgError` + If the computation of a determinant fails. + + References + ---------- + .. [1] T. M. Ragonneau. *Model-Based Derivative-Free Optimization + Methods and Software*. PhD thesis, Department of Applied + Mathematics, The Hong Kong Polytechnic University, Hong Kong, China, + 2022. URL: https://theses.lib.polyu.edu.hk/handle/200/12294. + """ + if options[Options.DEBUG]: + assert ( + k_new != self.best_index + ), "The index `k_new` must be different from the best index." + + # Build the k_new-th Lagrange polynomial. + coord_vec = np.squeeze(np.eye(1, self.models.npt, k_new)) + lag = Quadratic( + self.models.interpolation, + coord_vec, + options[Options.DEBUG], + ) + g_lag = lag.grad(self.x_best, self.models.interpolation) + + # Compute a simple constrained Cauchy step. + xl = self._pb.bounds.xl - self.x_best + xu = self._pb.bounds.xu - self.x_best + step = cauchy_geometry( + 0.0, + g_lag, + lambda v: lag.curv(v, self.models.interpolation), + xl, + xu, + self.radius, + options[Options.DEBUG], + ) + sigma = self.models.determinants(self.x_best + step, k_new) + + # Compute the solution on the straight lines joining the interpolation + # points to the k-th one, and choose it if it provides a larger value + # of the determinant of the interpolation system in absolute value. + xpt = ( + self.models.interpolation.xpt + - self.models.interpolation.xpt[:, self.best_index, np.newaxis] + ) + xpt[:, [0, self.best_index]] = xpt[:, [self.best_index, 0]] + step_alt = spider_geometry( + 0.0, + g_lag, + lambda v: lag.curv(v, self.models.interpolation), + xpt[:, 1:], + xl, + xu, + self.radius, + options[Options.DEBUG], + ) + sigma_alt = self.models.determinants(self.x_best + step_alt, k_new) + if abs(sigma_alt) > abs(sigma): + step = step_alt + sigma = sigma_alt + + # Compute a Cauchy step on the tangent space of the active constraints. + if self._pb.type in [ + "linearly constrained", + "nonlinearly constrained", + ]: + aub, bub, aeq, beq = ( + self.get_constraint_linearizations(self.x_best)) + tol_bd = get_arrays_tol(xl, xu) + tol_ub = get_arrays_tol(bub) + free_xl = xl <= -tol_bd + free_xu = xu >= tol_bd + free_ub = bub >= tol_ub + + # Compute the Cauchy step. + n_act, q = qr_tangential_byrd_omojokun( + aub, + aeq, + free_xl, + free_xu, + free_ub, + ) + g_lag_proj = q[:, n_act:] @ (q[:, n_act:].T @ g_lag) + norm_g_lag_proj = np.linalg.norm(g_lag_proj) + if 0 < n_act < self._pb.n and norm_g_lag_proj > TINY * self.radius: + step_alt = (self.radius / norm_g_lag_proj) * g_lag_proj + if lag.curv(step_alt, self.models.interpolation) < 0.0: + step_alt = -step_alt + + # Evaluate the constraint violation at the Cauchy step. + cbd = np.block([xl - step_alt, step_alt - xu]) + cub = aub @ step_alt - bub + ceq = aeq @ step_alt - beq + maxcv_val = max( + np.max(array, initial=0.0) + for array in [cbd, cub, np.abs(ceq)] + ) + + # Accept the new step if it is nearly feasible and do not + # drastically worsen the determinant of the interpolation + # system in absolute value. + tol = np.max(np.abs(step_alt[~free_xl]), initial=0.0) + tol = np.max(np.abs(step_alt[~free_xu]), initial=tol) + tol = np.max(np.abs(aub[~free_ub, :] @ step_alt), initial=tol) + tol = min(10.0 * tol, 1e-2 * np.linalg.norm(step_alt)) + if maxcv_val <= tol: + sigma_alt = self.models.determinants( + self.x_best + step_alt, k_new + ) + if abs(sigma_alt) >= 0.1 * abs(sigma): + step = np.clip(step_alt, xl, xu) + + if options[Options.DEBUG]: + tol = get_arrays_tol(xl, xu) + if np.any(step + tol < xl) or np.any(xu < step - tol): + warnings.warn( + "The geometry step does not respect the bound " + "constraints.", + RuntimeWarning, + 2, + ) + if np.linalg.norm(step) > 1.1 * self.radius: + warnings.warn( + "The geometry step does not respect the " + "trust-region constraint.", + RuntimeWarning, + 2, + ) + return step + + def get_second_order_correction_step(self, step, options): + """ + Get the second-order correction step. + + Parameters + ---------- + step : `numpy.ndarray`, shape (n,) + Trust-region step. + options : dict + Options of the solver. + + Returns + ------- + `numpy.ndarray`, shape (n,) + Second-order correction step. + """ + # Evaluate the linearizations of the constraints. + aub, bub, aeq, beq = self.get_constraint_linearizations(self.x_best) + xl = self._pb.bounds.xl - self.x_best + xu = self._pb.bounds.xu - self.x_best + radius = np.linalg.norm(step) + soc_step = normal_byrd_omojokun( + aub, + bub, + aeq, + beq, + xl, + xu, + radius, + options[Options.DEBUG], + **self._constants, + ) + if options[Options.DEBUG]: + tol = get_arrays_tol(xl, xu) + if np.any(soc_step + tol < xl) or np.any(xu < soc_step - tol): + warnings.warn( + "The second-order correction step does not " + "respect the bound constraints.", + RuntimeWarning, + 2, + ) + if np.linalg.norm(soc_step) > 1.1 * radius: + warnings.warn( + "The second-order correction step does not " + "respect the trust-region constraint.", + RuntimeWarning, + 2, + ) + return soc_step + + def get_reduction_ratio(self, step, fun_val, cub_val, ceq_val): + """ + Get the reduction ratio. + + Parameters + ---------- + step : `numpy.ndarray`, shape (n,) + Trust-region step. + fun_val : float + Objective function value at the trial point. + cub_val : `numpy.ndarray`, shape (m_nonlinear_ub,) + Nonlinear inequality constraint values at the trial point. + ceq_val : `numpy.ndarray`, shape (m_nonlinear_eq,) + Nonlinear equality constraint values at the trial point. + + Returns + ------- + float + Reduction ratio. + """ + merit_old = self.merit( + self.x_best, + self.fun_best, + self.cub_best, + self.ceq_best, + ) + merit_new = self.merit(self.x_best + step, fun_val, cub_val, ceq_val) + merit_model_old = self.merit( + self.x_best, + 0.0, + self.models.cub(self.x_best), + self.models.ceq(self.x_best), + ) + merit_model_new = self.merit( + self.x_best + step, + self.sqp_fun(step), + self.sqp_cub(step), + self.sqp_ceq(step), + ) + if abs(merit_model_old - merit_model_new) > TINY * abs( + merit_old - merit_new + ): + return (merit_old - merit_new) / abs( + merit_model_old - merit_model_new + ) + else: + return -1.0 + + def increase_penalty(self, step): + """ + Increase the penalty parameter. + + Parameters + ---------- + step : `numpy.ndarray`, shape (n,) + Trust-region step. + """ + aub, bub, aeq, beq = self.get_constraint_linearizations(self.x_best) + viol_diff = max( + np.linalg.norm( + np.block( + [ + np.maximum(0.0, -bub), + beq, + ] + ) + ) + - np.linalg.norm( + np.block( + [ + np.maximum(0.0, aub @ step - bub), + aeq @ step - beq, + ] + ) + ), + 0.0, + ) + sqp_val = self.sqp_fun(step) + + threshold = np.linalg.norm( + np.block( + [ + self._lm_linear_ub, + self._lm_linear_eq, + self._lm_nonlinear_ub, + self._lm_nonlinear_eq, + ] + ) + ) + if abs(viol_diff) > TINY * abs(sqp_val): + threshold = max(threshold, sqp_val / viol_diff) + best_index_save = self.best_index + if ( + self._penalty + <= self._constants[Constants.PENALTY_INCREASE_THRESHOLD] + * threshold + ): + self._penalty = max( + self._constants[Constants.PENALTY_INCREASE_FACTOR] * threshold, + 1.0, + ) + self.set_best_index() + return best_index_save == self.best_index + + def decrease_penalty(self): + """ + Decrease the penalty parameter. + """ + self._penalty = min(self._penalty, self._get_low_penalty()) + self.set_best_index() + + def set_best_index(self): + """ + Set the index of the best point. + """ + best_index = self.best_index + m_best = self.merit( + self.x_best, + self.models.fun_val[best_index], + self.models.cub_val[best_index, :], + self.models.ceq_val[best_index, :], + ) + r_best = self._pb.maxcv( + self.x_best, + self.models.cub_val[best_index, :], + self.models.ceq_val[best_index, :], + ) + tol = ( + 10.0 + * EPS + * max(self.models.n, self.models.npt) + * max(abs(m_best), 1.0) + ) + for k in range(self.models.npt): + if k != self.best_index: + x_val = self.models.interpolation.point(k) + m_val = self.merit( + x_val, + self.models.fun_val[k], + self.models.cub_val[k, :], + self.models.ceq_val[k, :], + ) + r_val = self._pb.maxcv( + x_val, + self.models.cub_val[k, :], + self.models.ceq_val[k, :], + ) + if m_val < m_best or (m_val < m_best + tol and r_val < r_best): + best_index = k + m_best = m_val + r_best = r_val + self._best_index = best_index + + def get_index_to_remove(self, x_new=None): + """ + Get the index of the interpolation point to remove. + + If `x_new` is not provided, the index returned should be used during + the geometry-improvement phase. Otherwise, the index returned is the + best index for included `x_new` in the interpolation set. + + Parameters + ---------- + x_new : `numpy.ndarray`, shape (n,), optional + New point to be included in the interpolation set. + + Returns + ------- + int + Index of the interpolation point to remove. + float + Distance between `x_best` and the removed point. + + Raises + ------ + `numpy.linalg.LinAlgError` + If the computation of a determinant fails. + """ + dist_sq = np.sum( + ( + self.models.interpolation.xpt + - self.models.interpolation.xpt[:, self.best_index, np.newaxis] + ) + ** 2.0, + axis=0, + ) + if x_new is None: + sigma = 1.0 + weights = dist_sq + else: + sigma = self.models.determinants(x_new) + weights = ( + np.maximum( + 1.0, + dist_sq + / max( + self._constants[Constants.LOW_RADIUS_FACTOR] + * self.radius, + self.resolution, + ) + ** 2.0, + ) + ** 3.0 + ) + weights[self.best_index] = -1.0 # do not remove the best point + k_max = np.argmax(weights * np.abs(sigma)) + return k_max, np.sqrt(dist_sq[k_max]) + + def update_radius(self, step, ratio): + """ + Update the trust-region radius. + + Parameters + ---------- + step : `numpy.ndarray`, shape (n,) + Trust-region step. + ratio : float + Reduction ratio. + """ + s_norm = np.linalg.norm(step) + if ratio <= self._constants[Constants.LOW_RATIO]: + self.radius *= self._constants[Constants.DECREASE_RADIUS_FACTOR] + elif ratio <= self._constants[Constants.HIGH_RATIO]: + self.radius = max( + self._constants[Constants.DECREASE_RADIUS_FACTOR] + * self.radius, + s_norm, + ) + else: + self.radius = min( + self._constants[Constants.INCREASE_RADIUS_FACTOR] + * self.radius, + max( + self._constants[Constants.DECREASE_RADIUS_FACTOR] + * self.radius, + self._constants[Constants.INCREASE_RADIUS_THRESHOLD] + * s_norm, + ), + ) + + def enhance_resolution(self, options): + """ + Enhance the resolution of the trust-region framework. + + Parameters + ---------- + options : dict + Options of the solver. + """ + if ( + self._constants[Constants.LARGE_RESOLUTION_THRESHOLD] + * options[Options.RHOEND] + < self.resolution + ): + self.resolution *= self._constants[ + Constants.DECREASE_RESOLUTION_FACTOR + ] + elif ( + self._constants[Constants.MODERATE_RESOLUTION_THRESHOLD] + * options[Options.RHOEND] + < self.resolution + ): + self.resolution = np.sqrt(self.resolution + * options[Options.RHOEND]) + else: + self.resolution = options[Options.RHOEND] + + # Reduce the trust-region radius. + self._radius = max( + self._constants[Constants.DECREASE_RADIUS_FACTOR] * self._radius, + self.resolution, + ) + + def shift_x_base(self, options): + """ + Shift the base point to `x_best`. + + Parameters + ---------- + options : dict + Options of the solver. + """ + self.models.shift_x_base(np.copy(self.x_best), options) + + def set_multipliers(self, x): + """ + Set the Lagrange multipliers. + + This method computes and set the Lagrange multipliers of the linear and + nonlinear constraints to be the QP multipliers. + + Parameters + ---------- + x : `numpy.ndarray`, shape (n,) + Point at which the Lagrange multipliers are computed. + """ + # Build the constraints of the least-squares problem. + incl_linear_ub = self._pb.linear.a_ub @ x >= self._pb.linear.b_ub + incl_nonlinear_ub = self.cub_best >= 0.0 + incl_xl = self._pb.bounds.xl >= x + incl_xu = self._pb.bounds.xu <= x + m_linear_ub = np.count_nonzero(incl_linear_ub) + m_nonlinear_ub = np.count_nonzero(incl_nonlinear_ub) + m_xl = np.count_nonzero(incl_xl) + m_xu = np.count_nonzero(incl_xu) + + if ( + m_linear_ub + m_nonlinear_ub + self.m_linear_eq + + self.m_nonlinear_eq > 0 + ): + identity = np.eye(self._pb.n) + c_jac = np.r_[ + -identity[incl_xl, :], + identity[incl_xu, :], + self._pb.linear.a_ub[incl_linear_ub, :], + self.models.cub_grad(x, incl_nonlinear_ub), + self._pb.linear.a_eq, + self.models.ceq_grad(x), + ] + + # Solve the least-squares problem. + g_best = self.models.fun_grad(x) + xl_lm = np.full(c_jac.shape[0], -np.inf) + xl_lm[: m_xl + m_xu + m_linear_ub + m_nonlinear_ub] = 0.0 + res = lsq_linear( + c_jac.T, + -g_best, + bounds=(xl_lm, np.inf), + method="bvls", + ) + + # Extract the Lagrange multipliers. + self._lm_linear_ub[incl_linear_ub] = res.x[ + m_xl + m_xu:m_xl + m_xu + m_linear_ub + ] + self._lm_linear_ub[~incl_linear_ub] = 0.0 + self._lm_nonlinear_ub[incl_nonlinear_ub] = res.x[ + m_xl + + m_xu + + m_linear_ub:m_xl + + m_xu + + m_linear_ub + + m_nonlinear_ub + ] + self._lm_nonlinear_ub[~incl_nonlinear_ub] = 0.0 + self._lm_linear_eq[:] = res.x[ + m_xl + + m_xu + + m_linear_ub + + m_nonlinear_ub:m_xl + + m_xu + + m_linear_ub + + m_nonlinear_ub + + self.m_linear_eq + ] + self._lm_nonlinear_eq[:] = res.x[ + m_xl + m_xu + m_linear_ub + m_nonlinear_ub + self.m_linear_eq: + ] + + def _get_low_penalty(self): + r_val_ub = np.c_[ + ( + self.models.interpolation.x_base[np.newaxis, :] + + self.models.interpolation.xpt.T + ) + @ self._pb.linear.a_ub.T + - self._pb.linear.b_ub[np.newaxis, :], + self.models.cub_val, + ] + r_val_eq = ( + self.models.interpolation.x_base[np.newaxis, :] + + self.models.interpolation.xpt.T + ) @ self._pb.linear.a_eq.T - self._pb.linear.b_eq[np.newaxis, :] + r_val_eq = np.block( + [ + r_val_eq, + -r_val_eq, + self.models.ceq_val, + -self.models.ceq_val, + ] + ) + r_val = np.block([r_val_ub, r_val_eq]) + c_min = np.nanmin(r_val, axis=0) + c_max = np.nanmax(r_val, axis=0) + indices = ( + c_min + < self._constants[Constants.THRESHOLD_RATIO_CONSTRAINTS] * c_max + ) + if np.any(indices): + f_min = np.nanmin(self.models.fun_val) + f_max = np.nanmax(self.models.fun_val) + c_min_neg = np.minimum(0.0, c_min[indices]) + c_diff = np.min(c_max[indices] - c_min_neg) + if c_diff > TINY * (f_max - f_min): + penalty = (f_max - f_min) / c_diff + else: + penalty = np.inf + else: + penalty = 0.0 + return penalty diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/main.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/main.py new file mode 100644 index 0000000000000000000000000000000000000000..01e5159e0dfebed9a78c6948cb99bfb1d744b6c7 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/main.py @@ -0,0 +1,1506 @@ +import warnings + +import numpy as np +from scipy.optimize import ( + Bounds, + LinearConstraint, + NonlinearConstraint, + OptimizeResult, +) + +from .framework import TrustRegion +from .problem import ( + ObjectiveFunction, + BoundConstraints, + LinearConstraints, + NonlinearConstraints, + Problem, +) +from .utils import ( + MaxEvalError, + TargetSuccess, + CallbackSuccess, + FeasibleSuccess, + exact_1d_array, +) +from .settings import ( + ExitStatus, + Options, + Constants, + DEFAULT_OPTIONS, + DEFAULT_CONSTANTS, + PRINT_OPTIONS, +) + + +def minimize( + fun, + x0, + args=(), + bounds=None, + constraints=(), + callback=None, + options=None, + **kwargs, +): + r""" + Minimize a scalar function using the COBYQA method. + + The Constrained Optimization BY Quadratic Approximations (COBYQA) method is + a derivative-free optimization method designed to solve general nonlinear + optimization problems. A complete description of COBYQA is given in [3]_. + + Parameters + ---------- + fun : {callable, None} + Objective function to be minimized. + + ``fun(x, *args) -> float`` + + where ``x`` is an array with shape (n,) and `args` is a tuple. If `fun` + is ``None``, the objective function is assumed to be the zero function, + resulting in a feasibility problem. + x0 : array_like, shape (n,) + Initial guess. + args : tuple, optional + Extra arguments passed to the objective function. + bounds : {`scipy.optimize.Bounds`, array_like, shape (n, 2)}, optional + Bound constraints of the problem. It can be one of the cases below. + + #. An instance of `scipy.optimize.Bounds`. For the time being, the + argument ``keep_feasible`` is disregarded, and all the constraints + are considered unrelaxable and will be enforced. + #. An array with shape (n, 2). The bound constraints for ``x[i]`` are + ``bounds[i][0] <= x[i] <= bounds[i][1]``. Set ``bounds[i][0]`` to + :math:`-\infty` if there is no lower bound, and set ``bounds[i][1]`` + to :math:`\infty` if there is no upper bound. + + The COBYQA method always respect the bound constraints. + constraints : {Constraint, list}, optional + General constraints of the problem. It can be one of the cases below. + + #. An instance of `scipy.optimize.LinearConstraint`. The argument + ``keep_feasible`` is disregarded. + #. An instance of `scipy.optimize.NonlinearConstraint`. The arguments + ``jac``, ``hess``, ``keep_feasible``, ``finite_diff_rel_step``, and + ``finite_diff_jac_sparsity`` are disregarded. + + #. A list, each of whose elements are described in the cases above. + + callback : callable, optional + A callback executed at each objective function evaluation. The method + terminates if a ``StopIteration`` exception is raised by the callback + function. Its signature can be one of the following: + + ``callback(intermediate_result)`` + + where ``intermediate_result`` is a keyword parameter that contains an + instance of `scipy.optimize.OptimizeResult`, with attributes ``x`` + and ``fun``, being the point at which the objective function is + evaluated and the value of the objective function, respectively. The + name of the parameter must be ``intermediate_result`` for the callback + to be passed an instance of `scipy.optimize.OptimizeResult`. + + Alternatively, the callback function can have the signature: + + ``callback(xk)`` + + where ``xk`` is the point at which the objective function is evaluated. + Introspection is used to determine which of the signatures to invoke. + options : dict, optional + Options passed to the solver. Accepted keys are: + + disp : bool, optional + Whether to print information about the optimization procedure. + Default is ``False``. + maxfev : int, optional + Maximum number of function evaluations. Default is ``500 * n``. + maxiter : int, optional + Maximum number of iterations. Default is ``1000 * n``. + target : float, optional + Target on the objective function value. The optimization + procedure is terminated when the objective function value of a + feasible point is less than or equal to this target. Default is + ``-numpy.inf``. + feasibility_tol : float, optional + Tolerance on the constraint violation. If the maximum + constraint violation at a point is less than or equal to this + tolerance, the point is considered feasible. Default is + ``numpy.sqrt(numpy.finfo(float).eps)``. + radius_init : float, optional + Initial trust-region radius. Typically, this value should be in + the order of one tenth of the greatest expected change to `x0`. + Default is ``1.0``. + radius_final : float, optional + Final trust-region radius. It should indicate the accuracy + required in the final values of the variables. Default is + ``1e-6``. + nb_points : int, optional + Number of interpolation points used to build the quadratic + models of the objective and constraint functions. Default is + ``2 * n + 1``. + scale : bool, optional + Whether to scale the variables according to the bounds. Default + is ``False``. + filter_size : int, optional + Maximum number of points in the filter. The filter is used to + select the best point returned by the optimization procedure. + Default is ``sys.maxsize``. + store_history : bool, optional + Whether to store the history of the function evaluations. + Default is ``False``. + history_size : int, optional + Maximum number of function evaluations to store in the history. + Default is ``sys.maxsize``. + debug : bool, optional + Whether to perform additional checks during the optimization + procedure. This option should be used only for debugging + purposes and is highly discouraged to general users. Default is + ``False``. + + Other constants (from the keyword arguments) are described below. They + are not intended to be changed by general users. They should only be + changed by users with a deep understanding of the algorithm, who want + to experiment with different settings. + + Returns + ------- + `scipy.optimize.OptimizeResult` + Result of the optimization procedure, with the following fields: + + message : str + Description of the cause of the termination. + success : bool + Whether the optimization procedure terminated successfully. + status : int + Termination status of the optimization procedure. + x : `numpy.ndarray`, shape (n,) + Solution point. + fun : float + Objective function value at the solution point. + maxcv : float + Maximum constraint violation at the solution point. + nfev : int + Number of function evaluations. + nit : int + Number of iterations. + + If ``store_history`` is True, the result also has the following fields: + + fun_history : `numpy.ndarray`, shape (nfev,) + History of the objective function values. + maxcv_history : `numpy.ndarray`, shape (nfev,) + History of the maximum constraint violations. + + A description of the termination statuses is given below. + + .. list-table:: + :widths: 25 75 + :header-rows: 1 + + * - Exit status + - Description + * - 0 + - The lower bound for the trust-region radius has been reached. + * - 1 + - The target objective function value has been reached. + * - 2 + - All variables are fixed by the bound constraints. + * - 3 + - The callback requested to stop the optimization procedure. + * - 4 + - The feasibility problem received has been solved successfully. + * - 5 + - The maximum number of function evaluations has been exceeded. + * - 6 + - The maximum number of iterations has been exceeded. + * - -1 + - The bound constraints are infeasible. + * - -2 + - A linear algebra error occurred. + + Other Parameters + ---------------- + decrease_radius_factor : float, optional + Factor by which the trust-region radius is reduced when the reduction + ratio is low or negative. Default is ``0.5``. + increase_radius_factor : float, optional + Factor by which the trust-region radius is increased when the reduction + ratio is large. Default is ``numpy.sqrt(2.0)``. + increase_radius_threshold : float, optional + Threshold that controls the increase of the trust-region radius when + the reduction ratio is large. Default is ``2.0``. + decrease_radius_threshold : float, optional + Threshold used to determine whether the trust-region radius should be + reduced to the resolution. Default is ``1.4``. + decrease_resolution_factor : float, optional + Factor by which the resolution is reduced when the current value is far + from its final value. Default is ``0.1``. + large_resolution_threshold : float, optional + Threshold used to determine whether the resolution is far from its + final value. Default is ``250.0``. + moderate_resolution_threshold : float, optional + Threshold used to determine whether the resolution is close to its + final value. Default is ``16.0``. + low_ratio : float, optional + Threshold used to determine whether the reduction ratio is low. Default + is ``0.1``. + high_ratio : float, optional + Threshold used to determine whether the reduction ratio is high. + Default is ``0.7``. + very_low_ratio : float, optional + Threshold used to determine whether the reduction ratio is very low. + This is used to determine whether the models should be reset. Default + is ``0.01``. + penalty_increase_threshold : float, optional + Threshold used to determine whether the penalty parameter should be + increased. Default is ``1.5``. + penalty_increase_factor : float, optional + Factor by which the penalty parameter is increased. Default is ``2.0``. + short_step_threshold : float, optional + Factor used to determine whether the trial step is too short. Default + is ``0.5``. + low_radius_factor : float, optional + Factor used to determine which interpolation point should be removed + from the interpolation set at each iteration. Default is ``0.1``. + byrd_omojokun_factor : float, optional + Factor by which the trust-region radius is reduced for the computations + of the normal step in the Byrd-Omojokun composite-step approach. + Default is ``0.8``. + threshold_ratio_constraints : float, optional + Threshold used to determine which constraints should be taken into + account when decreasing the penalty parameter. Default is ``2.0``. + large_shift_factor : float, optional + Factor used to determine whether the point around which the quadratic + models are built should be updated. Default is ``10.0``. + large_gradient_factor : float, optional + Factor used to determine whether the models should be reset. Default is + ``10.0``. + resolution_factor : float, optional + Factor by which the resolution is decreased. Default is ``2.0``. + improve_tcg : bool, optional + Whether to improve the steps computed by the truncated conjugate + gradient method when the trust-region boundary is reached. Default is + ``True``. + + References + ---------- + .. [1] J. Nocedal and S. J. Wright. *Numerical Optimization*. Springer Ser. + Oper. Res. Financ. Eng. Springer, New York, NY, USA, second edition, + 2006. `doi:10.1007/978-0-387-40065-5 + `_. + .. [2] M. J. D. Powell. A direct search optimization method that models the + objective and constraint functions by linear interpolation. In S. Gomez + and J.-P. Hennart, editors, *Advances in Optimization and Numerical + Analysis*, volume 275 of Math. Appl., pages 51--67. Springer, Dordrecht, + Netherlands, 1994. `doi:10.1007/978-94-015-8330-5_4 + `_. + .. [3] T. M. Ragonneau. *Model-Based Derivative-Free Optimization Methods + and Software*. PhD thesis, Department of Applied Mathematics, The Hong + Kong Polytechnic University, Hong Kong, China, 2022. URL: + https://theses.lib.polyu.edu.hk/handle/200/12294. + + Examples + -------- + To demonstrate how to use `minimize`, we first minimize the Rosenbrock + function implemented in `scipy.optimize` in an unconstrained setting. + + .. testsetup:: + + import numpy as np + np.set_printoptions(precision=3, suppress=True) + + >>> from cobyqa import minimize + >>> from scipy.optimize import rosen + + To solve the problem using COBYQA, run: + + >>> x0 = [1.3, 0.7, 0.8, 1.9, 1.2] + >>> res = minimize(rosen, x0) + >>> res.x + array([1., 1., 1., 1., 1.]) + + To see how bound and constraints are handled using `minimize`, we solve + Example 16.4 of [1]_, defined as + + .. math:: + + \begin{aligned} + \min_{x \in \mathbb{R}^2} & \quad (x_1 - 1)^2 + (x_2 - 2.5)^2\\ + \text{s.t.} & \quad -x_1 + 2x_2 \le 2,\\ + & \quad x_1 + 2x_2 \le 6,\\ + & \quad x_1 - 2x_2 \le 2,\\ + & \quad x_1 \ge 0,\\ + & \quad x_2 \ge 0. + \end{aligned} + + >>> import numpy as np + >>> from scipy.optimize import Bounds, LinearConstraint + + Its objective function can be implemented as: + + >>> def fun(x): + ... return (x[0] - 1.0)**2 + (x[1] - 2.5)**2 + + This problem can be solved using `minimize` as: + + >>> x0 = [2.0, 0.0] + >>> bounds = Bounds([0.0, 0.0], np.inf) + >>> constraints = LinearConstraint([ + ... [-1.0, 2.0], + ... [1.0, 2.0], + ... [1.0, -2.0], + ... ], -np.inf, [2.0, 6.0, 2.0]) + >>> res = minimize(fun, x0, bounds=bounds, constraints=constraints) + >>> res.x + array([1.4, 1.7]) + + To see how nonlinear constraints are handled, we solve Problem (F) of [2]_, + defined as + + .. math:: + + \begin{aligned} + \min_{x \in \mathbb{R}^2} & \quad -x_1 - x_2\\ + \text{s.t.} & \quad x_1^2 - x_2 \le 0,\\ + & \quad x_1^2 + x_2^2 \le 1. + \end{aligned} + + >>> from scipy.optimize import NonlinearConstraint + + Its objective and constraint functions can be implemented as: + + >>> def fun(x): + ... return -x[0] - x[1] + >>> + >>> def cub(x): + ... return [x[0]**2 - x[1], x[0]**2 + x[1]**2] + + This problem can be solved using `minimize` as: + + >>> x0 = [1.0, 1.0] + >>> constraints = NonlinearConstraint(cub, -np.inf, [0.0, 1.0]) + >>> res = minimize(fun, x0, constraints=constraints) + >>> res.x + array([0.707, 0.707]) + + Finally, to see how to supply linear and nonlinear constraints + simultaneously, we solve Problem (G) of [2]_, defined as + + .. math:: + + \begin{aligned} + \min_{x \in \mathbb{R}^3} & \quad x_3\\ + \text{s.t.} & \quad 5x_1 - x_2 + x_3 \ge 0,\\ + & \quad -5x_1 - x_2 + x_3 \ge 0,\\ + & \quad x_1^2 + x_2^2 + 4x_2 \le x_3. + \end{aligned} + + Its objective and nonlinear constraint functions can be implemented as: + + >>> def fun(x): + ... return x[2] + >>> + >>> def cub(x): + ... return x[0]**2 + x[1]**2 + 4.0*x[1] - x[2] + + This problem can be solved using `minimize` as: + + >>> x0 = [1.0, 1.0, 1.0] + >>> constraints = [ + ... LinearConstraint( + ... [[5.0, -1.0, 1.0], [-5.0, -1.0, 1.0]], + ... [0.0, 0.0], + ... np.inf, + ... ), + ... NonlinearConstraint(cub, -np.inf, 0.0), + ... ] + >>> res = minimize(fun, x0, constraints=constraints) + >>> res.x + array([ 0., -3., -3.]) + """ + # Get basic options that are needed for the initialization. + if options is None: + options = {} + else: + options = dict(options) + verbose = options.get(Options.VERBOSE, DEFAULT_OPTIONS[Options.VERBOSE]) + verbose = bool(verbose) + feasibility_tol = options.get( + Options.FEASIBILITY_TOL, + DEFAULT_OPTIONS[Options.FEASIBILITY_TOL], + ) + feasibility_tol = float(feasibility_tol) + scale = options.get(Options.SCALE, DEFAULT_OPTIONS[Options.SCALE]) + scale = bool(scale) + store_history = options.get( + Options.STORE_HISTORY, + DEFAULT_OPTIONS[Options.STORE_HISTORY], + ) + store_history = bool(store_history) + if Options.HISTORY_SIZE in options and options[Options.HISTORY_SIZE] <= 0: + raise ValueError("The size of the history must be positive.") + history_size = options.get( + Options.HISTORY_SIZE, + DEFAULT_OPTIONS[Options.HISTORY_SIZE], + ) + history_size = int(history_size) + if Options.FILTER_SIZE in options and options[Options.FILTER_SIZE] <= 0: + raise ValueError("The size of the filter must be positive.") + filter_size = options.get( + Options.FILTER_SIZE, + DEFAULT_OPTIONS[Options.FILTER_SIZE], + ) + filter_size = int(filter_size) + debug = options.get(Options.DEBUG, DEFAULT_OPTIONS[Options.DEBUG]) + debug = bool(debug) + + # Initialize the objective function. + if not isinstance(args, tuple): + args = (args,) + obj = ObjectiveFunction(fun, verbose, debug, *args) + + # Initialize the bound constraints. + if not hasattr(x0, "__len__"): + x0 = [x0] + n_orig = len(x0) + bounds = BoundConstraints(_get_bounds(bounds, n_orig)) + + # Initialize the constraints. + linear_constraints, nonlinear_constraints = _get_constraints(constraints) + linear = LinearConstraints(linear_constraints, n_orig, debug) + nonlinear = NonlinearConstraints(nonlinear_constraints, verbose, debug) + + # Initialize the problem (and remove the fixed variables). + pb = Problem( + obj, + x0, + bounds, + linear, + nonlinear, + callback, + feasibility_tol, + scale, + store_history, + history_size, + filter_size, + debug, + ) + + # Set the default options. + _set_default_options(options, pb.n) + constants = _set_default_constants(**kwargs) + + # Initialize the models and skip the computations whenever possible. + if not pb.bounds.is_feasible: + # The bound constraints are infeasible. + return _build_result( + pb, + 0.0, + False, + ExitStatus.INFEASIBLE_ERROR, + 0, + options, + ) + elif pb.n == 0: + # All variables are fixed by the bound constraints. + return _build_result( + pb, + 0.0, + True, + ExitStatus.FIXED_SUCCESS, + 0, + options, + ) + if verbose: + print("Starting the optimization procedure.") + print(f"Initial trust-region radius: {options[Options.RHOBEG]}.") + print(f"Final trust-region radius: {options[Options.RHOEND]}.") + print( + f"Maximum number of function evaluations: " + f"{options[Options.MAX_EVAL]}." + ) + print(f"Maximum number of iterations: {options[Options.MAX_ITER]}.") + print() + try: + framework = TrustRegion(pb, options, constants) + except TargetSuccess: + # The target on the objective function value has been reached + return _build_result( + pb, + 0.0, + True, + ExitStatus.TARGET_SUCCESS, + 0, + options, + ) + except CallbackSuccess: + # The callback raised a StopIteration exception. + return _build_result( + pb, + 0.0, + True, + ExitStatus.CALLBACK_SUCCESS, + 0, + options, + ) + except FeasibleSuccess: + # The feasibility problem has been solved successfully. + return _build_result( + pb, + 0.0, + True, + ExitStatus.FEASIBLE_SUCCESS, + 0, + options, + ) + except MaxEvalError: + # The maximum number of function evaluations has been exceeded. + return _build_result( + pb, + 0.0, + False, + ExitStatus.MAX_ITER_WARNING, + 0, + options, + ) + except np.linalg.LinAlgError: + # The construction of the initial interpolation set failed. + return _build_result( + pb, + 0.0, + False, + ExitStatus.LINALG_ERROR, + 0, + options, + ) + + # Start the optimization procedure. + success = False + n_iter = 0 + k_new = None + n_short_steps = 0 + n_very_short_steps = 0 + n_alt_models = 0 + while True: + # Stop the optimization procedure if the maximum number of iterations + # has been exceeded. We do not write the main loop as a for loop + # because we want to access the number of iterations outside the loop. + if n_iter >= options[Options.MAX_ITER]: + status = ExitStatus.MAX_ITER_WARNING + break + n_iter += 1 + + # Update the point around which the quadratic models are built. + if ( + np.linalg.norm( + framework.x_best - framework.models.interpolation.x_base + ) + >= constants[Constants.LARGE_SHIFT_FACTOR] * framework.radius + ): + framework.shift_x_base(options) + + # Evaluate the trial step. + radius_save = framework.radius + normal_step, tangential_step = framework.get_trust_region_step(options) + step = normal_step + tangential_step + s_norm = np.linalg.norm(step) + + # If the trial step is too short, we do not attempt to evaluate the + # objective and constraint functions. Instead, we reduce the + # trust-region radius and check whether the resolution should be + # enhanced and whether the geometry of the interpolation set should be + # improved. Otherwise, we entertain a classical iteration. The + # criterion for performing an exceptional jump is taken from NEWUOA. + if ( + s_norm + <= constants[Constants.SHORT_STEP_THRESHOLD] * framework.resolution + ): + framework.radius *= constants[Constants.DECREASE_RESOLUTION_FACTOR] + if radius_save > framework.resolution: + n_short_steps = 0 + n_very_short_steps = 0 + else: + n_short_steps += 1 + n_very_short_steps += 1 + if s_norm > 0.1 * framework.resolution: + n_very_short_steps = 0 + enhance_resolution = n_short_steps >= 5 or n_very_short_steps >= 3 + if enhance_resolution: + n_short_steps = 0 + n_very_short_steps = 0 + improve_geometry = False + else: + try: + k_new, dist_new = framework.get_index_to_remove() + except np.linalg.LinAlgError: + status = ExitStatus.LINALG_ERROR + break + improve_geometry = dist_new > max( + framework.radius, + constants[Constants.RESOLUTION_FACTOR] + * framework.resolution, + ) + else: + # Increase the penalty parameter if necessary. + same_best_point = framework.increase_penalty(step) + if same_best_point: + # Evaluate the objective and constraint functions. + try: + fun_val, cub_val, ceq_val = _eval( + pb, + framework, + step, + options, + ) + except TargetSuccess: + status = ExitStatus.TARGET_SUCCESS + success = True + break + except FeasibleSuccess: + status = ExitStatus.FEASIBLE_SUCCESS + success = True + break + except CallbackSuccess: + status = ExitStatus.CALLBACK_SUCCESS + success = True + break + except MaxEvalError: + status = ExitStatus.MAX_EVAL_WARNING + break + + # Perform a second-order correction step if necessary. + merit_old = framework.merit( + framework.x_best, + framework.fun_best, + framework.cub_best, + framework.ceq_best, + ) + merit_new = framework.merit( + framework.x_best + step, fun_val, cub_val, ceq_val + ) + if ( + pb.type == "nonlinearly constrained" + and merit_new > merit_old + and np.linalg.norm(normal_step) + > constants[Constants.BYRD_OMOJOKUN_FACTOR] ** 2.0 + * framework.radius + ): + soc_step = framework.get_second_order_correction_step( + step, options + ) + if np.linalg.norm(soc_step) > 0.0: + step += soc_step + + # Evaluate the objective and constraint functions. + try: + fun_val, cub_val, ceq_val = _eval( + pb, + framework, + step, + options, + ) + except TargetSuccess: + status = ExitStatus.TARGET_SUCCESS + success = True + break + except FeasibleSuccess: + status = ExitStatus.FEASIBLE_SUCCESS + success = True + break + except CallbackSuccess: + status = ExitStatus.CALLBACK_SUCCESS + success = True + break + except MaxEvalError: + status = ExitStatus.MAX_EVAL_WARNING + break + + # Calculate the reduction ratio. + ratio = framework.get_reduction_ratio( + step, + fun_val, + cub_val, + ceq_val, + ) + + # Choose an interpolation point to remove. + try: + k_new = framework.get_index_to_remove( + framework.x_best + step + )[0] + except np.linalg.LinAlgError: + status = ExitStatus.LINALG_ERROR + break + + # Update the interpolation set. + try: + ill_conditioned = framework.models.update_interpolation( + k_new, framework.x_best + step, fun_val, cub_val, + ceq_val + ) + except np.linalg.LinAlgError: + status = ExitStatus.LINALG_ERROR + break + framework.set_best_index() + + # Update the trust-region radius. + framework.update_radius(step, ratio) + + # Attempt to replace the models by the alternative ones. + if framework.radius <= framework.resolution: + if ratio >= constants[Constants.VERY_LOW_RATIO]: + n_alt_models = 0 + else: + n_alt_models += 1 + grad = framework.models.fun_grad(framework.x_best) + try: + grad_alt = framework.models.fun_alt_grad( + framework.x_best + ) + except np.linalg.LinAlgError: + status = ExitStatus.LINALG_ERROR + break + if np.linalg.norm(grad) < constants[ + Constants.LARGE_GRADIENT_FACTOR + ] * np.linalg.norm(grad_alt): + n_alt_models = 0 + if n_alt_models >= 3: + try: + framework.models.reset_models() + except np.linalg.LinAlgError: + status = ExitStatus.LINALG_ERROR + break + n_alt_models = 0 + + # Update the Lagrange multipliers. + framework.set_multipliers(framework.x_best + step) + + # Check whether the resolution should be enhanced. + try: + k_new, dist_new = framework.get_index_to_remove() + except np.linalg.LinAlgError: + status = ExitStatus.LINALG_ERROR + break + improve_geometry = ( + ill_conditioned + or ratio <= constants[Constants.LOW_RATIO] + and dist_new + > max( + framework.radius, + constants[Constants.RESOLUTION_FACTOR] + * framework.resolution, + ) + ) + enhance_resolution = ( + radius_save <= framework.resolution + and ratio <= constants[Constants.LOW_RATIO] + and not improve_geometry + ) + else: + # When increasing the penalty parameter, the best point so far + # may change. In this case, we restart the iteration. + enhance_resolution = False + improve_geometry = False + + # Reduce the resolution if necessary. + if enhance_resolution: + if framework.resolution <= options[Options.RHOEND]: + success = True + status = ExitStatus.RADIUS_SUCCESS + break + framework.enhance_resolution(options) + framework.decrease_penalty() + + if verbose: + maxcv_val = pb.maxcv( + framework.x_best, framework.cub_best, framework.ceq_best + ) + _print_step( + f"New trust-region radius: {framework.resolution}", + pb, + pb.build_x(framework.x_best), + framework.fun_best, + maxcv_val, + pb.n_eval, + n_iter, + ) + print() + + # Improve the geometry of the interpolation set if necessary. + if improve_geometry: + try: + step = framework.get_geometry_step(k_new, options) + except np.linalg.LinAlgError: + status = ExitStatus.LINALG_ERROR + break + + # Evaluate the objective and constraint functions. + try: + fun_val, cub_val, ceq_val = _eval(pb, framework, step, options) + except TargetSuccess: + status = ExitStatus.TARGET_SUCCESS + success = True + break + except FeasibleSuccess: + status = ExitStatus.FEASIBLE_SUCCESS + success = True + break + except CallbackSuccess: + status = ExitStatus.CALLBACK_SUCCESS + success = True + break + except MaxEvalError: + status = ExitStatus.MAX_EVAL_WARNING + break + + # Update the interpolation set. + try: + framework.models.update_interpolation( + k_new, + framework.x_best + step, + fun_val, + cub_val, + ceq_val, + ) + except np.linalg.LinAlgError: + status = ExitStatus.LINALG_ERROR + break + framework.set_best_index() + + return _build_result( + pb, + framework.penalty, + success, + status, + n_iter, + options, + ) + + +def _get_bounds(bounds, n): + """ + Uniformize the bounds. + """ + if bounds is None: + return Bounds(np.full(n, -np.inf), np.full(n, np.inf)) + elif isinstance(bounds, Bounds): + if bounds.lb.shape != (n,) or bounds.ub.shape != (n,): + raise ValueError(f"The bounds must have {n} elements.") + return Bounds(bounds.lb, bounds.ub) + elif hasattr(bounds, "__len__"): + bounds = np.asarray(bounds) + if bounds.shape != (n, 2): + raise ValueError( + "The shape of the bounds is not compatible with " + "the number of variables." + ) + return Bounds(bounds[:, 0], bounds[:, 1]) + else: + raise TypeError( + "The bounds must be an instance of " + "scipy.optimize.Bounds or an array-like object." + ) + + +def _get_constraints(constraints): + """ + Extract the linear and nonlinear constraints. + """ + if isinstance(constraints, dict) or not hasattr(constraints, "__len__"): + constraints = (constraints,) + + # Extract the linear and nonlinear constraints. + linear_constraints = [] + nonlinear_constraints = [] + for constraint in constraints: + if isinstance(constraint, LinearConstraint): + lb = exact_1d_array( + constraint.lb, + "The lower bound of the linear constraints must be a vector.", + ) + ub = exact_1d_array( + constraint.ub, + "The upper bound of the linear constraints must be a vector.", + ) + linear_constraints.append( + LinearConstraint( + constraint.A, + *np.broadcast_arrays(lb, ub), + ) + ) + elif isinstance(constraint, NonlinearConstraint): + lb = exact_1d_array( + constraint.lb, + "The lower bound of the " + "nonlinear constraints must be a " + "vector.", + ) + ub = exact_1d_array( + constraint.ub, + "The upper bound of the " + "nonlinear constraints must be a " + "vector.", + ) + nonlinear_constraints.append( + NonlinearConstraint( + constraint.fun, + *np.broadcast_arrays(lb, ub), + ) + ) + elif isinstance(constraint, dict): + if "type" not in constraint or constraint["type"] not in ( + "eq", + "ineq", + ): + raise ValueError('The constraint type must be "eq" or "ineq".') + if "fun" not in constraint or not callable(constraint["fun"]): + raise ValueError("The constraint function must be callable.") + nonlinear_constraints.append( + { + "fun": constraint["fun"], + "type": constraint["type"], + "args": constraint.get("args", ()), + } + ) + else: + raise TypeError( + "The constraints must be instances of " + "scipy.optimize.LinearConstraint, " + "scipy.optimize.NonlinearConstraint, or dict." + ) + return linear_constraints, nonlinear_constraints + + +def _set_default_options(options, n): + """ + Set the default options. + """ + if Options.RHOBEG in options and options[Options.RHOBEG] <= 0.0: + raise ValueError("The initial trust-region radius must be positive.") + if Options.RHOEND in options and options[Options.RHOEND] < 0.0: + raise ValueError("The final trust-region radius must be nonnegative.") + if Options.RHOBEG in options and Options.RHOEND in options: + if options[Options.RHOBEG] < options[Options.RHOEND]: + raise ValueError( + "The initial trust-region radius must be greater " + "than or equal to the final trust-region radius." + ) + elif Options.RHOBEG in options: + options[Options.RHOEND.value] = np.min( + [ + DEFAULT_OPTIONS[Options.RHOEND], + options[Options.RHOBEG], + ] + ) + elif Options.RHOEND in options: + options[Options.RHOBEG.value] = np.max( + [ + DEFAULT_OPTIONS[Options.RHOBEG], + options[Options.RHOEND], + ] + ) + else: + options[Options.RHOBEG.value] = DEFAULT_OPTIONS[Options.RHOBEG] + options[Options.RHOEND.value] = DEFAULT_OPTIONS[Options.RHOEND] + options[Options.RHOBEG.value] = float(options[Options.RHOBEG]) + options[Options.RHOEND.value] = float(options[Options.RHOEND]) + if Options.NPT in options and options[Options.NPT] <= 0: + raise ValueError("The number of interpolation points must be " + "positive.") + if ( + Options.NPT in options + and options[Options.NPT] > ((n + 1) * (n + 2)) // 2 + ): + raise ValueError( + f"The number of interpolation points must be at most " + f"{((n + 1) * (n + 2)) // 2}." + ) + options.setdefault(Options.NPT.value, DEFAULT_OPTIONS[Options.NPT](n)) + options[Options.NPT.value] = int(options[Options.NPT]) + if Options.MAX_EVAL in options and options[Options.MAX_EVAL] <= 0: + raise ValueError( + "The maximum number of function evaluations must be positive." + ) + options.setdefault( + Options.MAX_EVAL.value, + np.max( + [ + DEFAULT_OPTIONS[Options.MAX_EVAL](n), + options[Options.NPT] + 1, + ] + ), + ) + options[Options.MAX_EVAL.value] = int(options[Options.MAX_EVAL]) + if Options.MAX_ITER in options and options[Options.MAX_ITER] <= 0: + raise ValueError("The maximum number of iterations must be positive.") + options.setdefault( + Options.MAX_ITER.value, + DEFAULT_OPTIONS[Options.MAX_ITER](n), + ) + options[Options.MAX_ITER.value] = int(options[Options.MAX_ITER]) + options.setdefault(Options.TARGET.value, DEFAULT_OPTIONS[Options.TARGET]) + options[Options.TARGET.value] = float(options[Options.TARGET]) + options.setdefault( + Options.FEASIBILITY_TOL.value, + DEFAULT_OPTIONS[Options.FEASIBILITY_TOL], + ) + options[Options.FEASIBILITY_TOL.value] = float( + options[Options.FEASIBILITY_TOL] + ) + options.setdefault(Options.VERBOSE.value, DEFAULT_OPTIONS[Options.VERBOSE]) + options[Options.VERBOSE.value] = bool(options[Options.VERBOSE]) + options.setdefault(Options.SCALE.value, DEFAULT_OPTIONS[Options.SCALE]) + options[Options.SCALE.value] = bool(options[Options.SCALE]) + options.setdefault( + Options.FILTER_SIZE.value, + DEFAULT_OPTIONS[Options.FILTER_SIZE], + ) + options[Options.FILTER_SIZE.value] = int(options[Options.FILTER_SIZE]) + options.setdefault( + Options.STORE_HISTORY.value, + DEFAULT_OPTIONS[Options.STORE_HISTORY], + ) + options[Options.STORE_HISTORY.value] = bool(options[Options.STORE_HISTORY]) + options.setdefault( + Options.HISTORY_SIZE.value, + DEFAULT_OPTIONS[Options.HISTORY_SIZE], + ) + options[Options.HISTORY_SIZE.value] = int(options[Options.HISTORY_SIZE]) + options.setdefault(Options.DEBUG.value, DEFAULT_OPTIONS[Options.DEBUG]) + options[Options.DEBUG.value] = bool(options[Options.DEBUG]) + + # Check whether they are any unknown options. + for key in options: + if key not in Options.__members__.values(): + warnings.warn(f"Unknown option: {key}.", RuntimeWarning, 3) + + +def _set_default_constants(**kwargs): + """ + Set the default constants. + """ + constants = dict(kwargs) + constants.setdefault( + Constants.DECREASE_RADIUS_FACTOR.value, + DEFAULT_CONSTANTS[Constants.DECREASE_RADIUS_FACTOR], + ) + constants[Constants.DECREASE_RADIUS_FACTOR.value] = float( + constants[Constants.DECREASE_RADIUS_FACTOR] + ) + if ( + constants[Constants.DECREASE_RADIUS_FACTOR] <= 0.0 + or constants[Constants.DECREASE_RADIUS_FACTOR] >= 1.0 + ): + raise ValueError( + "The constant decrease_radius_factor must be in the interval " + "(0, 1)." + ) + constants.setdefault( + Constants.INCREASE_RADIUS_THRESHOLD.value, + DEFAULT_CONSTANTS[Constants.INCREASE_RADIUS_THRESHOLD], + ) + constants[Constants.INCREASE_RADIUS_THRESHOLD.value] = float( + constants[Constants.INCREASE_RADIUS_THRESHOLD] + ) + if constants[Constants.INCREASE_RADIUS_THRESHOLD] <= 1.0: + raise ValueError( + "The constant increase_radius_threshold must be greater than 1." + ) + if ( + Constants.INCREASE_RADIUS_FACTOR in constants + and constants[Constants.INCREASE_RADIUS_FACTOR] <= 1.0 + ): + raise ValueError( + "The constant increase_radius_factor must be greater than 1." + ) + if ( + Constants.DECREASE_RADIUS_THRESHOLD in constants + and constants[Constants.DECREASE_RADIUS_THRESHOLD] <= 1.0 + ): + raise ValueError( + "The constant decrease_radius_threshold must be greater than 1." + ) + if ( + Constants.INCREASE_RADIUS_FACTOR in constants + and Constants.DECREASE_RADIUS_THRESHOLD in constants + ): + if ( + constants[Constants.DECREASE_RADIUS_THRESHOLD] + >= constants[Constants.INCREASE_RADIUS_FACTOR] + ): + raise ValueError( + "The constant decrease_radius_threshold must be " + "less than increase_radius_factor." + ) + elif Constants.INCREASE_RADIUS_FACTOR in constants: + constants[Constants.DECREASE_RADIUS_THRESHOLD.value] = np.min( + [ + DEFAULT_CONSTANTS[Constants.DECREASE_RADIUS_THRESHOLD], + 0.5 * (1.0 + constants[Constants.INCREASE_RADIUS_FACTOR]), + ] + ) + elif Constants.DECREASE_RADIUS_THRESHOLD in constants: + constants[Constants.INCREASE_RADIUS_FACTOR.value] = np.max( + [ + DEFAULT_CONSTANTS[Constants.INCREASE_RADIUS_FACTOR], + 2.0 * constants[Constants.DECREASE_RADIUS_THRESHOLD], + ] + ) + else: + constants[Constants.INCREASE_RADIUS_FACTOR.value] = DEFAULT_CONSTANTS[ + Constants.INCREASE_RADIUS_FACTOR + ] + constants[Constants.DECREASE_RADIUS_THRESHOLD.value] = ( + DEFAULT_CONSTANTS[Constants.DECREASE_RADIUS_THRESHOLD]) + constants.setdefault( + Constants.DECREASE_RESOLUTION_FACTOR.value, + DEFAULT_CONSTANTS[Constants.DECREASE_RESOLUTION_FACTOR], + ) + constants[Constants.DECREASE_RESOLUTION_FACTOR.value] = float( + constants[Constants.DECREASE_RESOLUTION_FACTOR] + ) + if ( + constants[Constants.DECREASE_RESOLUTION_FACTOR] <= 0.0 + or constants[Constants.DECREASE_RESOLUTION_FACTOR] >= 1.0 + ): + raise ValueError( + "The constant decrease_resolution_factor must be in the interval " + "(0, 1)." + ) + if ( + Constants.LARGE_RESOLUTION_THRESHOLD in constants + and constants[Constants.LARGE_RESOLUTION_THRESHOLD] <= 1.0 + ): + raise ValueError( + "The constant large_resolution_threshold must be greater than 1." + ) + if ( + Constants.MODERATE_RESOLUTION_THRESHOLD in constants + and constants[Constants.MODERATE_RESOLUTION_THRESHOLD] <= 1.0 + ): + raise ValueError( + "The constant moderate_resolution_threshold must be greater than " + "1." + ) + if ( + Constants.LARGE_RESOLUTION_THRESHOLD in constants + and Constants.MODERATE_RESOLUTION_THRESHOLD in constants + ): + if ( + constants[Constants.MODERATE_RESOLUTION_THRESHOLD] + > constants[Constants.LARGE_RESOLUTION_THRESHOLD] + ): + raise ValueError( + "The constant moderate_resolution_threshold " + "must be at most large_resolution_threshold." + ) + elif Constants.LARGE_RESOLUTION_THRESHOLD in constants: + constants[Constants.MODERATE_RESOLUTION_THRESHOLD.value] = np.min( + [ + DEFAULT_CONSTANTS[Constants.MODERATE_RESOLUTION_THRESHOLD], + constants[Constants.LARGE_RESOLUTION_THRESHOLD], + ] + ) + elif Constants.MODERATE_RESOLUTION_THRESHOLD in constants: + constants[Constants.LARGE_RESOLUTION_THRESHOLD.value] = np.max( + [ + DEFAULT_CONSTANTS[Constants.LARGE_RESOLUTION_THRESHOLD], + constants[Constants.MODERATE_RESOLUTION_THRESHOLD], + ] + ) + else: + constants[Constants.LARGE_RESOLUTION_THRESHOLD.value] = ( + DEFAULT_CONSTANTS[Constants.LARGE_RESOLUTION_THRESHOLD] + ) + constants[Constants.MODERATE_RESOLUTION_THRESHOLD.value] = ( + DEFAULT_CONSTANTS[Constants.MODERATE_RESOLUTION_THRESHOLD] + ) + if Constants.LOW_RATIO in constants and ( + constants[Constants.LOW_RATIO] <= 0.0 + or constants[Constants.LOW_RATIO] >= 1.0 + ): + raise ValueError( + "The constant low_ratio must be in the interval (0, 1)." + ) + if Constants.HIGH_RATIO in constants and ( + constants[Constants.HIGH_RATIO] <= 0.0 + or constants[Constants.HIGH_RATIO] >= 1.0 + ): + raise ValueError( + "The constant high_ratio must be in the interval (0, 1)." + ) + if Constants.LOW_RATIO in constants and Constants.HIGH_RATIO in constants: + if constants[Constants.LOW_RATIO] > constants[Constants.HIGH_RATIO]: + raise ValueError( + "The constant low_ratio must be at most high_ratio." + ) + elif Constants.LOW_RATIO in constants: + constants[Constants.HIGH_RATIO.value] = np.max( + [ + DEFAULT_CONSTANTS[Constants.HIGH_RATIO], + constants[Constants.LOW_RATIO], + ] + ) + elif Constants.HIGH_RATIO in constants: + constants[Constants.LOW_RATIO.value] = np.min( + [ + DEFAULT_CONSTANTS[Constants.LOW_RATIO], + constants[Constants.HIGH_RATIO], + ] + ) + else: + constants[Constants.LOW_RATIO.value] = DEFAULT_CONSTANTS[ + Constants.LOW_RATIO + ] + constants[Constants.HIGH_RATIO.value] = DEFAULT_CONSTANTS[ + Constants.HIGH_RATIO + ] + constants.setdefault( + Constants.VERY_LOW_RATIO.value, + DEFAULT_CONSTANTS[Constants.VERY_LOW_RATIO], + ) + constants[Constants.VERY_LOW_RATIO.value] = float( + constants[Constants.VERY_LOW_RATIO] + ) + if ( + constants[Constants.VERY_LOW_RATIO] <= 0.0 + or constants[Constants.VERY_LOW_RATIO] >= 1.0 + ): + raise ValueError( + "The constant very_low_ratio must be in the interval (0, 1)." + ) + if ( + Constants.PENALTY_INCREASE_THRESHOLD in constants + and constants[Constants.PENALTY_INCREASE_THRESHOLD] < 1.0 + ): + raise ValueError( + "The constant penalty_increase_threshold must be " + "greater than or equal to 1." + ) + if ( + Constants.PENALTY_INCREASE_FACTOR in constants + and constants[Constants.PENALTY_INCREASE_FACTOR] <= 1.0 + ): + raise ValueError( + "The constant penalty_increase_factor must be greater than 1." + ) + if ( + Constants.PENALTY_INCREASE_THRESHOLD in constants + and Constants.PENALTY_INCREASE_FACTOR in constants + ): + if ( + constants[Constants.PENALTY_INCREASE_FACTOR] + < constants[Constants.PENALTY_INCREASE_THRESHOLD] + ): + raise ValueError( + "The constant penalty_increase_factor must be " + "greater than or equal to " + "penalty_increase_threshold." + ) + elif Constants.PENALTY_INCREASE_THRESHOLD in constants: + constants[Constants.PENALTY_INCREASE_FACTOR.value] = np.max( + [ + DEFAULT_CONSTANTS[Constants.PENALTY_INCREASE_FACTOR], + constants[Constants.PENALTY_INCREASE_THRESHOLD], + ] + ) + elif Constants.PENALTY_INCREASE_FACTOR in constants: + constants[Constants.PENALTY_INCREASE_THRESHOLD.value] = np.min( + [ + DEFAULT_CONSTANTS[Constants.PENALTY_INCREASE_THRESHOLD], + constants[Constants.PENALTY_INCREASE_FACTOR], + ] + ) + else: + constants[Constants.PENALTY_INCREASE_THRESHOLD.value] = ( + DEFAULT_CONSTANTS[Constants.PENALTY_INCREASE_THRESHOLD] + ) + constants[Constants.PENALTY_INCREASE_FACTOR.value] = DEFAULT_CONSTANTS[ + Constants.PENALTY_INCREASE_FACTOR + ] + constants.setdefault( + Constants.SHORT_STEP_THRESHOLD.value, + DEFAULT_CONSTANTS[Constants.SHORT_STEP_THRESHOLD], + ) + constants[Constants.SHORT_STEP_THRESHOLD.value] = float( + constants[Constants.SHORT_STEP_THRESHOLD] + ) + if ( + constants[Constants.SHORT_STEP_THRESHOLD] <= 0.0 + or constants[Constants.SHORT_STEP_THRESHOLD] >= 1.0 + ): + raise ValueError( + "The constant short_step_threshold must be in the interval (0, 1)." + ) + constants.setdefault( + Constants.LOW_RADIUS_FACTOR.value, + DEFAULT_CONSTANTS[Constants.LOW_RADIUS_FACTOR], + ) + constants[Constants.LOW_RADIUS_FACTOR.value] = float( + constants[Constants.LOW_RADIUS_FACTOR] + ) + if ( + constants[Constants.LOW_RADIUS_FACTOR] <= 0.0 + or constants[Constants.LOW_RADIUS_FACTOR] >= 1.0 + ): + raise ValueError( + "The constant low_radius_factor must be in the interval (0, 1)." + ) + constants.setdefault( + Constants.BYRD_OMOJOKUN_FACTOR.value, + DEFAULT_CONSTANTS[Constants.BYRD_OMOJOKUN_FACTOR], + ) + constants[Constants.BYRD_OMOJOKUN_FACTOR.value] = float( + constants[Constants.BYRD_OMOJOKUN_FACTOR] + ) + if ( + constants[Constants.BYRD_OMOJOKUN_FACTOR] <= 0.0 + or constants[Constants.BYRD_OMOJOKUN_FACTOR] >= 1.0 + ): + raise ValueError( + "The constant byrd_omojokun_factor must be in the interval (0, 1)." + ) + constants.setdefault( + Constants.THRESHOLD_RATIO_CONSTRAINTS.value, + DEFAULT_CONSTANTS[Constants.THRESHOLD_RATIO_CONSTRAINTS], + ) + constants[Constants.THRESHOLD_RATIO_CONSTRAINTS.value] = float( + constants[Constants.THRESHOLD_RATIO_CONSTRAINTS] + ) + if constants[Constants.THRESHOLD_RATIO_CONSTRAINTS] <= 1.0: + raise ValueError( + "The constant threshold_ratio_constraints must be greater than 1." + ) + constants.setdefault( + Constants.LARGE_SHIFT_FACTOR.value, + DEFAULT_CONSTANTS[Constants.LARGE_SHIFT_FACTOR], + ) + constants[Constants.LARGE_SHIFT_FACTOR.value] = float( + constants[Constants.LARGE_SHIFT_FACTOR] + ) + if constants[Constants.LARGE_SHIFT_FACTOR] < 0.0: + raise ValueError("The constant large_shift_factor must be " + "nonnegative.") + constants.setdefault( + Constants.LARGE_GRADIENT_FACTOR.value, + DEFAULT_CONSTANTS[Constants.LARGE_GRADIENT_FACTOR], + ) + constants[Constants.LARGE_GRADIENT_FACTOR.value] = float( + constants[Constants.LARGE_GRADIENT_FACTOR] + ) + if constants[Constants.LARGE_GRADIENT_FACTOR] <= 1.0: + raise ValueError( + "The constant large_gradient_factor must be greater than 1." + ) + constants.setdefault( + Constants.RESOLUTION_FACTOR.value, + DEFAULT_CONSTANTS[Constants.RESOLUTION_FACTOR], + ) + constants[Constants.RESOLUTION_FACTOR.value] = float( + constants[Constants.RESOLUTION_FACTOR] + ) + if constants[Constants.RESOLUTION_FACTOR] <= 1.0: + raise ValueError( + "The constant resolution_factor must be greater than 1." + ) + constants.setdefault( + Constants.IMPROVE_TCG.value, + DEFAULT_CONSTANTS[Constants.IMPROVE_TCG], + ) + constants[Constants.IMPROVE_TCG.value] = bool( + constants[Constants.IMPROVE_TCG] + ) + + # Check whether they are any unknown options. + for key in kwargs: + if key not in Constants.__members__.values(): + warnings.warn(f"Unknown constant: {key}.", RuntimeWarning, 3) + return constants + + +def _eval(pb, framework, step, options): + """ + Evaluate the objective and constraint functions. + """ + if pb.n_eval >= options[Options.MAX_EVAL]: + raise MaxEvalError + x_eval = framework.x_best + step + fun_val, cub_val, ceq_val = pb(x_eval, framework.penalty) + r_val = pb.maxcv(x_eval, cub_val, ceq_val) + if ( + fun_val <= options[Options.TARGET] + and r_val <= options[Options.FEASIBILITY_TOL] + ): + raise TargetSuccess + if pb.is_feasibility and r_val <= options[Options.FEASIBILITY_TOL]: + raise FeasibleSuccess + return fun_val, cub_val, ceq_val + + +def _build_result(pb, penalty, success, status, n_iter, options): + """ + Build the result of the optimization process. + """ + # Build the result. + x, fun, maxcv = pb.best_eval(penalty) + success = success and np.isfinite(fun) and np.isfinite(maxcv) + if status not in [ExitStatus.TARGET_SUCCESS, ExitStatus.FEASIBLE_SUCCESS]: + success = success and maxcv <= options[Options.FEASIBILITY_TOL] + result = OptimizeResult() + result.message = { + ExitStatus.RADIUS_SUCCESS: "The lower bound for the trust-region " + "radius has been reached", + ExitStatus.TARGET_SUCCESS: "The target objective function value has " + "been reached", + ExitStatus.FIXED_SUCCESS: "All variables are fixed by the bound " + "constraints", + ExitStatus.CALLBACK_SUCCESS: "The callback requested to stop the " + "optimization procedure", + ExitStatus.FEASIBLE_SUCCESS: "The feasibility problem received has " + "been solved successfully", + ExitStatus.MAX_EVAL_WARNING: "The maximum number of function " + "evaluations has been exceeded", + ExitStatus.MAX_ITER_WARNING: "The maximum number of iterations has " + "been exceeded", + ExitStatus.INFEASIBLE_ERROR: "The bound constraints are infeasible", + ExitStatus.LINALG_ERROR: "A linear algebra error occurred", + }.get(status, "Unknown exit status") + result.success = success + result.status = status.value + result.x = pb.build_x(x) + result.fun = fun + result.maxcv = maxcv + result.nfev = pb.n_eval + result.nit = n_iter + if options[Options.STORE_HISTORY]: + result.fun_history = pb.fun_history + result.maxcv_history = pb.maxcv_history + + # Print the result if requested. + if options[Options.VERBOSE]: + _print_step( + result.message, + pb, + result.x, + result.fun, + result.maxcv, + result.nfev, + result.nit, + ) + return result + + +def _print_step(message, pb, x, fun_val, r_val, n_eval, n_iter): + """ + Print information about the current state of the optimization process. + """ + print() + print(f"{message}.") + print(f"Number of function evaluations: {n_eval}.") + print(f"Number of iterations: {n_iter}.") + if not pb.is_feasibility: + print(f"Least value of {pb.fun_name}: {fun_val}.") + print(f"Maximum constraint violation: {r_val}.") + with np.printoptions(**PRINT_OPTIONS): + print(f"Corresponding point: {x}.") diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/models.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/models.py new file mode 100644 index 0000000000000000000000000000000000000000..4891b074bfd6dd3f7d43fa95b0b845a764cac114 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/models.py @@ -0,0 +1,1529 @@ +import warnings + +import numpy as np +from scipy.linalg import eigh + +from .settings import Options +from .utils import MaxEvalError, TargetSuccess, FeasibleSuccess + + +EPS = np.finfo(float).eps + + +class Interpolation: + """ + Interpolation set. + + This class stores a base point around which the models are expanded and the + interpolation points. The coordinates of the interpolation points are + relative to the base point. + """ + + def __init__(self, pb, options): + """ + Initialize the interpolation set. + + Parameters + ---------- + pb : `cobyqa.problem.Problem` + Problem to be solved. + options : dict + Options of the solver. + """ + # Reduce the initial trust-region radius if necessary. + self._debug = options[Options.DEBUG] + max_radius = 0.5 * np.min(pb.bounds.xu - pb.bounds.xl) + if options[Options.RHOBEG] > max_radius: + options[Options.RHOBEG.value] = max_radius + options[Options.RHOEND.value] = np.min( + [ + options[Options.RHOEND], + max_radius, + ] + ) + + # Set the initial point around which the models are expanded. + self._x_base = np.copy(pb.x0) + very_close_xl_idx = ( + self.x_base <= pb.bounds.xl + 0.5 * options[Options.RHOBEG] + ) + self.x_base[very_close_xl_idx] = pb.bounds.xl[very_close_xl_idx] + close_xl_idx = ( + pb.bounds.xl + 0.5 * options[Options.RHOBEG] < self.x_base + ) & (self.x_base <= pb.bounds.xl + options[Options.RHOBEG]) + self.x_base[close_xl_idx] = np.minimum( + pb.bounds.xl[close_xl_idx] + options[Options.RHOBEG], + pb.bounds.xu[close_xl_idx], + ) + very_close_xu_idx = ( + self.x_base >= pb.bounds.xu - 0.5 * options[Options.RHOBEG] + ) + self.x_base[very_close_xu_idx] = pb.bounds.xu[very_close_xu_idx] + close_xu_idx = ( + self.x_base < pb.bounds.xu - 0.5 * options[Options.RHOBEG] + ) & (pb.bounds.xu - options[Options.RHOBEG] <= self.x_base) + self.x_base[close_xu_idx] = np.maximum( + pb.bounds.xu[close_xu_idx] - options[Options.RHOBEG], + pb.bounds.xl[close_xu_idx], + ) + + # Set the initial interpolation set. + self._xpt = np.zeros((pb.n, options[Options.NPT])) + for k in range(1, options[Options.NPT]): + if k <= pb.n: + if very_close_xu_idx[k - 1]: + self.xpt[k - 1, k] = -options[Options.RHOBEG] + else: + self.xpt[k - 1, k] = options[Options.RHOBEG] + elif k <= 2 * pb.n: + if very_close_xl_idx[k - pb.n - 1]: + self.xpt[k - pb.n - 1, k] = 2.0 * options[Options.RHOBEG] + elif very_close_xu_idx[k - pb.n - 1]: + self.xpt[k - pb.n - 1, k] = -2.0 * options[Options.RHOBEG] + else: + self.xpt[k - pb.n - 1, k] = -options[Options.RHOBEG] + else: + spread = (k - pb.n - 1) // pb.n + k1 = k - (1 + spread) * pb.n - 1 + k2 = (k1 + spread) % pb.n + self.xpt[k1, k] = self.xpt[k1, k1 + 1] + self.xpt[k2, k] = self.xpt[k2, k2 + 1] + + @property + def n(self): + """ + Number of variables. + + Returns + ------- + int + Number of variables. + """ + return self.xpt.shape[0] + + @property + def npt(self): + """ + Number of interpolation points. + + Returns + ------- + int + Number of interpolation points. + """ + return self.xpt.shape[1] + + @property + def xpt(self): + """ + Interpolation points. + + Returns + ------- + `numpy.ndarray`, shape (n, npt) + Interpolation points. + """ + return self._xpt + + @xpt.setter + def xpt(self, xpt): + """ + Set the interpolation points. + + Parameters + ---------- + xpt : `numpy.ndarray`, shape (n, npt) + New interpolation points. + """ + if self._debug: + assert xpt.shape == ( + self.n, + self.npt, + ), "The shape of `xpt` is not valid." + self._xpt = xpt + + @property + def x_base(self): + """ + Base point around which the models are expanded. + + Returns + ------- + `numpy.ndarray`, shape (n,) + Base point around which the models are expanded. + """ + return self._x_base + + @x_base.setter + def x_base(self, x_base): + """ + Set the base point around which the models are expanded. + + Parameters + ---------- + x_base : `numpy.ndarray`, shape (n,) + New base point around which the models are expanded. + """ + if self._debug: + assert x_base.shape == ( + self.n, + ), "The shape of `x_base` is not valid." + self._x_base = x_base + + def point(self, k): + """ + Get the `k`-th interpolation point. + + The return point is relative to the origin. + + Parameters + ---------- + k : int + Index of the interpolation point. + + Returns + ------- + `numpy.ndarray`, shape (n,) + `k`-th interpolation point. + """ + if self._debug: + assert 0 <= k < self.npt, "The index `k` is not valid." + return self.x_base + self.xpt[:, k] + + +_cache = {"xpt": None, "a": None, "right_scaling": None, "eigh": None} + + +def build_system(interpolation): + """ + Build the left-hand side matrix of the interpolation system. The + matrix below stores W * diag(right_scaling), + where W is the theoretical matrix of the interpolation system. The + right scaling matrices is chosen to keep the elements in + the matrix well-balanced. + + Parameters + ---------- + interpolation : `cobyqa.models.Interpolation` + Interpolation set. + """ + + # Compute the scaled directions from the base point to the + # interpolation points. We scale the directions to avoid numerical + # difficulties. + if _cache["xpt"] is not None and np.array_equal( + interpolation.xpt, _cache["xpt"] + ): + return _cache["a"], _cache["right_scaling"], _cache["eigh"] + + scale = np.max(np.linalg.norm(interpolation.xpt, axis=0), initial=EPS) + xpt_scale = interpolation.xpt / scale + + n, npt = xpt_scale.shape + a = np.zeros((npt + n + 1, npt + n + 1)) + a[:npt, :npt] = 0.5 * (xpt_scale.T @ xpt_scale) ** 2.0 + a[:npt, npt] = 1.0 + a[:npt, npt + 1:] = xpt_scale.T + a[npt, :npt] = 1.0 + a[npt + 1:, :npt] = xpt_scale + + # Build the left and right scaling diagonal matrices. + right_scaling = np.empty(npt + n + 1) + right_scaling[:npt] = 1.0 / scale**2.0 + right_scaling[npt] = scale**2.0 + right_scaling[npt + 1:] = scale + + eig_values, eig_vectors = eigh(a, check_finite=False) + + _cache["xpt"] = np.copy(interpolation.xpt) + _cache["a"] = np.copy(a) + _cache["right_scaling"] = np.copy(right_scaling) + _cache["eigh"] = (eig_values, eig_vectors) + + return a, right_scaling, (eig_values, eig_vectors) + + +class Quadratic: + """ + Quadratic model. + + This class stores the Hessian matrix of the quadratic model using the + implicit/explicit representation designed by Powell for NEWUOA [1]_. + + References + ---------- + .. [1] M. J. D. Powell. The NEWUOA software for unconstrained optimization + without derivatives. In G. Di Pillo and M. Roma, editors, *Large-Scale + Nonlinear Optimization*, volume 83 of Nonconvex Optim. Appl., pages + 255--297. Springer, Boston, MA, USA, 2006. `doi:10.1007/0-387-30065-1_16 + `_. + """ + + def __init__(self, interpolation, values, debug): + """ + Initialize the quadratic model. + + Parameters + ---------- + interpolation : `cobyqa.models.Interpolation` + Interpolation set. + values : `numpy.ndarray`, shape (npt,) + Values of the interpolated function at the interpolation points. + debug : bool + Whether to make debugging tests during the execution. + + Raises + ------ + `numpy.linalg.LinAlgError` + If the interpolation system is ill-defined. + """ + self._debug = debug + if self._debug: + assert values.shape == ( + interpolation.npt, + ), "The shape of `values` is not valid." + if interpolation.npt < interpolation.n + 1: + raise ValueError( + f"The number of interpolation points must be at least " + f"{interpolation.n + 1}." + ) + self._const, self._grad, self._i_hess, _ = self._get_model( + interpolation, + values, + ) + self._e_hess = np.zeros((self.n, self.n)) + + def __call__(self, x, interpolation): + """ + Evaluate the quadratic model at a given point. + + Parameters + ---------- + x : `numpy.ndarray`, shape (n,) + Point at which the quadratic model is evaluated. + interpolation : `cobyqa.models.Interpolation` + Interpolation set. + + Returns + ------- + float + Value of the quadratic model at `x`. + """ + if self._debug: + assert x.shape == (self.n,), "The shape of `x` is not valid." + x_diff = x - interpolation.x_base + return ( + self._const + + self._grad @ x_diff + + 0.5 + * ( + self._i_hess @ (interpolation.xpt.T @ x_diff) ** 2.0 + + x_diff @ self._e_hess @ x_diff + ) + ) + + @property + def n(self): + """ + Number of variables. + + Returns + ------- + int + Number of variables. + """ + return self._grad.size + + @property + def npt(self): + """ + Number of interpolation points used to define the quadratic model. + + Returns + ------- + int + Number of interpolation points used to define the quadratic model. + """ + return self._i_hess.size + + def grad(self, x, interpolation): + """ + Evaluate the gradient of the quadratic model at a given point. + + Parameters + ---------- + x : `numpy.ndarray`, shape (n,) + Point at which the gradient of the quadratic model is evaluated. + interpolation : `cobyqa.models.Interpolation` + Interpolation set. + + Returns + ------- + `numpy.ndarray`, shape (n,) + Gradient of the quadratic model at `x`. + """ + if self._debug: + assert x.shape == (self.n,), "The shape of `x` is not valid." + x_diff = x - interpolation.x_base + return self._grad + self.hess_prod(x_diff, interpolation) + + def hess(self, interpolation): + """ + Evaluate the Hessian matrix of the quadratic model. + + Parameters + ---------- + interpolation : `cobyqa.models.Interpolation` + Interpolation set. + + Returns + ------- + `numpy.ndarray`, shape (n, n) + Hessian matrix of the quadratic model. + """ + return self._e_hess + interpolation.xpt @ ( + self._i_hess[:, np.newaxis] * interpolation.xpt.T + ) + + def hess_prod(self, v, interpolation): + """ + Evaluate the right product of the Hessian matrix of the quadratic model + with a given vector. + + Parameters + ---------- + v : `numpy.ndarray`, shape (n,) + Vector with which the Hessian matrix of the quadratic model is + multiplied from the right. + interpolation : `cobyqa.models.Interpolation` + Interpolation set. + + Returns + ------- + `numpy.ndarray`, shape (n,) + Right product of the Hessian matrix of the quadratic model with + `v`. + """ + if self._debug: + assert v.shape == (self.n,), "The shape of `v` is not valid." + return self._e_hess @ v + interpolation.xpt @ ( + self._i_hess * (interpolation.xpt.T @ v) + ) + + def curv(self, v, interpolation): + """ + Evaluate the curvature of the quadratic model along a given direction. + + Parameters + ---------- + v : `numpy.ndarray`, shape (n,) + Direction along which the curvature of the quadratic model is + evaluated. + interpolation : `cobyqa.models.Interpolation` + Interpolation set. + + Returns + ------- + float + Curvature of the quadratic model along `v`. + """ + if self._debug: + assert v.shape == (self.n,), "The shape of `v` is not valid." + return ( + v @ self._e_hess @ v + + self._i_hess @ (interpolation.xpt.T @ v) ** 2.0 + ) + + def update(self, interpolation, k_new, dir_old, values_diff): + """ + Update the quadratic model. + + This method applies the derivative-free symmetric Broyden update to the + quadratic model. The `knew`-th interpolation point must be updated + before calling this method. + + Parameters + ---------- + interpolation : `cobyqa.models.Interpolation` + Updated interpolation set. + k_new : int + Index of the updated interpolation point. + dir_old : `numpy.ndarray`, shape (n,) + Value of ``interpolation.xpt[:, k_new]`` before the update. + values_diff : `numpy.ndarray`, shape (npt,) + Differences between the values of the interpolated nonlinear + function and the previous quadratic model at the updated + interpolation points. + + Raises + ------ + `numpy.linalg.LinAlgError` + If the interpolation system is ill-defined. + """ + if self._debug: + assert 0 <= k_new < self.npt, "The index `k_new` is not valid." + assert dir_old.shape == ( + self.n, + ), "The shape of `dir_old` is not valid." + assert values_diff.shape == ( + self.npt, + ), "The shape of `values_diff` is not valid." + + # Forward the k_new-th element of the implicit Hessian matrix to the + # explicit Hessian matrix. This must be done because the implicit + # Hessian matrix is related to the interpolation points, and the + # k_new-th interpolation point is modified. + self._e_hess += self._i_hess[k_new] * np.outer(dir_old, dir_old) + self._i_hess[k_new] = 0.0 + + # Update the quadratic model. + const, grad, i_hess, ill_conditioned = self._get_model( + interpolation, + values_diff, + ) + self._const += const + self._grad += grad + self._i_hess += i_hess + return ill_conditioned + + def shift_x_base(self, interpolation, new_x_base): + """ + Shift the point around which the quadratic model is defined. + + Parameters + ---------- + interpolation : `cobyqa.models.Interpolation` + Previous interpolation set. + new_x_base : `numpy.ndarray`, shape (n,) + Point that will replace ``interpolation.x_base``. + """ + if self._debug: + assert new_x_base.shape == ( + self.n, + ), "The shape of `new_x_base` is not valid." + self._const = self(new_x_base, interpolation) + self._grad = self.grad(new_x_base, interpolation) + shift = new_x_base - interpolation.x_base + update = np.outer( + shift, + (interpolation.xpt - 0.5 * shift[:, np.newaxis]) @ self._i_hess, + ) + self._e_hess += update + update.T + + @staticmethod + def solve_systems(interpolation, rhs): + """ + Solve the interpolation systems. + + Parameters + ---------- + interpolation : `cobyqa.models.Interpolation` + Interpolation set. + rhs : `numpy.ndarray`, shape (npt + n + 1, m) + Right-hand side vectors of the ``m`` interpolation systems. + + Returns + ------- + `numpy.ndarray`, shape (npt + n + 1, m) + Solutions of the interpolation systems. + `numpy.ndarray`, shape (m, ) + Whether the interpolation systems are ill-conditioned. + + Raises + ------ + `numpy.linalg.LinAlgError` + If the interpolation systems are ill-defined. + """ + n, npt = interpolation.xpt.shape + assert ( + rhs.ndim == 2 and rhs.shape[0] == npt + n + 1 + ), "The shape of `rhs` is not valid." + + # Build the left-hand side matrix of the interpolation system. The + # matrix below stores diag(left_scaling) * W * diag(right_scaling), + # where W is the theoretical matrix of the interpolation system. The + # left and right scaling matrices are chosen to keep the elements in + # the matrix well-balanced. + a, right_scaling, eig = build_system(interpolation) + + # Build the solution. After a discussion with Mike Saunders and Alexis + # Montoison during their visit to the Hong Kong Polytechnic University + # in 2024, we decided to use the eigendecomposition of the symmetric + # matrix a. This is more stable than the previously employed LBL + # decomposition, and allows us to directly detect ill-conditioning of + # the system and to build the least-squares solution if necessary. + # Numerical experiments have shown that this strategy improves the + # performance of the solver. + rhs_scaled = rhs * right_scaling[:, np.newaxis] + if not (np.all(np.isfinite(a)) and np.all(np.isfinite(rhs_scaled))): + raise np.linalg.LinAlgError( + "The interpolation system is ill-defined." + ) + + # calculated in build_system + eig_values, eig_vectors = eig + + large_eig_values = np.abs(eig_values) > EPS + eig_vectors = eig_vectors[:, large_eig_values] + inv_eig_values = 1.0 / eig_values[large_eig_values] + ill_conditioned = ~np.all(large_eig_values, 0) + left_scaled_solutions = eig_vectors @ ( + (eig_vectors.T @ rhs_scaled) * inv_eig_values[:, np.newaxis] + ) + return ( + left_scaled_solutions * right_scaling[:, np.newaxis], + ill_conditioned, + ) + + @staticmethod + def _get_model(interpolation, values): + """ + Solve the interpolation system. + + Parameters + ---------- + interpolation : `cobyqa.models.Interpolation` + Interpolation set. + values : `numpy.ndarray`, shape (npt,) + Values of the interpolated function at the interpolation points. + + Returns + ------- + float + Constant term of the quadratic model. + `numpy.ndarray`, shape (n,) + Gradient of the quadratic model at ``interpolation.x_base``. + `numpy.ndarray`, shape (npt,) + Implicit Hessian matrix of the quadratic model. + + Raises + ------ + `numpy.linalg.LinAlgError` + If the interpolation system is ill-defined. + """ + assert values.shape == ( + interpolation.npt, + ), "The shape of `values` is not valid." + n, npt = interpolation.xpt.shape + x, ill_conditioned = Quadratic.solve_systems( + interpolation, + np.block( + [ + [ + values, + np.zeros(n + 1), + ] + ] + ).T, + ) + return x[npt, 0], x[npt + 1:, 0], x[:npt, 0], ill_conditioned + + +class Models: + """ + Models for a nonlinear optimization problem. + """ + + def __init__(self, pb, options, penalty): + """ + Initialize the models. + + Parameters + ---------- + pb : `cobyqa.problem.Problem` + Problem to be solved. + options : dict + Options of the solver. + penalty : float + Penalty parameter used to select the point in the filter to forward + to the callback function. + + Raises + ------ + `cobyqa.utils.MaxEvalError` + If the maximum number of evaluations is reached. + `cobyqa.utils.TargetSuccess` + If a nearly feasible point has been found with an objective + function value below the target. + `cobyqa.utils.FeasibleSuccess` + If a feasible point has been found for a feasibility problem. + `numpy.linalg.LinAlgError` + If the interpolation system is ill-defined. + """ + # Set the initial interpolation set. + self._debug = options[Options.DEBUG] + self._interpolation = Interpolation(pb, options) + + # Evaluate the nonlinear functions at the initial interpolation points. + x_eval = self.interpolation.point(0) + fun_init, cub_init, ceq_init = pb(x_eval, penalty) + self._fun_val = np.full(options[Options.NPT], np.nan) + self._cub_val = np.full((options[Options.NPT], cub_init.size), np.nan) + self._ceq_val = np.full((options[Options.NPT], ceq_init.size), np.nan) + for k in range(options[Options.NPT]): + if k >= options[Options.MAX_EVAL]: + raise MaxEvalError + if k == 0: + self.fun_val[k] = fun_init + self.cub_val[k, :] = cub_init + self.ceq_val[k, :] = ceq_init + else: + x_eval = self.interpolation.point(k) + self.fun_val[k], self.cub_val[k, :], self.ceq_val[k, :] = pb( + x_eval, + penalty, + ) + + # Stop the iterations if the problem is a feasibility problem and + # the current interpolation point is feasible. + if ( + pb.is_feasibility + and pb.maxcv( + self.interpolation.point(k), + self.cub_val[k, :], + self.ceq_val[k, :], + ) + <= options[Options.FEASIBILITY_TOL] + ): + raise FeasibleSuccess + + # Stop the iterations if the current interpolation point is nearly + # feasible and has an objective function value below the target. + if ( + self._fun_val[k] <= options[Options.TARGET] + and pb.maxcv( + self.interpolation.point(k), + self.cub_val[k, :], + self.ceq_val[k, :], + ) + <= options[Options.FEASIBILITY_TOL] + ): + raise TargetSuccess + + # Build the initial quadratic models. + self._fun = Quadratic( + self.interpolation, + self._fun_val, + options[Options.DEBUG], + ) + self._cub = np.empty(self.m_nonlinear_ub, dtype=Quadratic) + self._ceq = np.empty(self.m_nonlinear_eq, dtype=Quadratic) + for i in range(self.m_nonlinear_ub): + self._cub[i] = Quadratic( + self.interpolation, + self.cub_val[:, i], + options[Options.DEBUG], + ) + for i in range(self.m_nonlinear_eq): + self._ceq[i] = Quadratic( + self.interpolation, + self.ceq_val[:, i], + options[Options.DEBUG], + ) + if self._debug: + self._check_interpolation_conditions() + + @property + def n(self): + """ + Dimension of the problem. + + Returns + ------- + int + Dimension of the problem. + """ + return self.interpolation.n + + @property + def npt(self): + """ + Number of interpolation points. + + Returns + ------- + int + Number of interpolation points. + """ + return self.interpolation.npt + + @property + def m_nonlinear_ub(self): + """ + Number of nonlinear inequality constraints. + + Returns + ------- + int + Number of nonlinear inequality constraints. + """ + return self.cub_val.shape[1] + + @property + def m_nonlinear_eq(self): + """ + Number of nonlinear equality constraints. + + Returns + ------- + int + Number of nonlinear equality constraints. + """ + return self.ceq_val.shape[1] + + @property + def interpolation(self): + """ + Interpolation set. + + Returns + ------- + `cobyqa.models.Interpolation` + Interpolation set. + """ + return self._interpolation + + @property + def fun_val(self): + """ + Values of the objective function at the interpolation points. + + Returns + ------- + `numpy.ndarray`, shape (npt,) + Values of the objective function at the interpolation points. + """ + return self._fun_val + + @property + def cub_val(self): + """ + Values of the nonlinear inequality constraint functions at the + interpolation points. + + Returns + ------- + `numpy.ndarray`, shape (npt, m_nonlinear_ub) + Values of the nonlinear inequality constraint functions at the + interpolation points. + """ + return self._cub_val + + @property + def ceq_val(self): + """ + Values of the nonlinear equality constraint functions at the + interpolation points. + + Returns + ------- + `numpy.ndarray`, shape (npt, m_nonlinear_eq) + Values of the nonlinear equality constraint functions at the + interpolation points. + """ + return self._ceq_val + + def fun(self, x): + """ + Evaluate the quadratic model of the objective function at a given + point. + + Parameters + ---------- + x : `numpy.ndarray`, shape (n,) + Point at which to evaluate the quadratic model of the objective + function. + + Returns + ------- + float + Value of the quadratic model of the objective function at `x`. + """ + if self._debug: + assert x.shape == (self.n,), "The shape of `x` is not valid." + return self._fun(x, self.interpolation) + + def fun_grad(self, x): + """ + Evaluate the gradient of the quadratic model of the objective function + at a given point. + + Parameters + ---------- + x : `numpy.ndarray`, shape (n,) + Point at which to evaluate the gradient of the quadratic model of + the objective function. + + Returns + ------- + `numpy.ndarray`, shape (n,) + Gradient of the quadratic model of the objective function at `x`. + """ + if self._debug: + assert x.shape == (self.n,), "The shape of `x` is not valid." + return self._fun.grad(x, self.interpolation) + + def fun_hess(self): + """ + Evaluate the Hessian matrix of the quadratic model of the objective + function. + + Returns + ------- + `numpy.ndarray`, shape (n, n) + Hessian matrix of the quadratic model of the objective function. + """ + return self._fun.hess(self.interpolation) + + def fun_hess_prod(self, v): + """ + Evaluate the right product of the Hessian matrix of the quadratic model + of the objective function with a given vector. + + Parameters + ---------- + v : `numpy.ndarray`, shape (n,) + Vector with which the Hessian matrix of the quadratic model of the + objective function is multiplied from the right. + + Returns + ------- + `numpy.ndarray`, shape (n,) + Right product of the Hessian matrix of the quadratic model of the + objective function with `v`. + """ + if self._debug: + assert v.shape == (self.n,), "The shape of `v` is not valid." + return self._fun.hess_prod(v, self.interpolation) + + def fun_curv(self, v): + """ + Evaluate the curvature of the quadratic model of the objective function + along a given direction. + + Parameters + ---------- + v : `numpy.ndarray`, shape (n,) + Direction along which the curvature of the quadratic model of the + objective function is evaluated. + + Returns + ------- + float + Curvature of the quadratic model of the objective function along + `v`. + """ + if self._debug: + assert v.shape == (self.n,), "The shape of `v` is not valid." + return self._fun.curv(v, self.interpolation) + + def fun_alt_grad(self, x): + """ + Evaluate the gradient of the alternative quadratic model of the + objective function at a given point. + + Parameters + ---------- + x : `numpy.ndarray`, shape (n,) + Point at which to evaluate the gradient of the alternative + quadratic model of the objective function. + + Returns + ------- + `numpy.ndarray`, shape (n,) + Gradient of the alternative quadratic model of the objective + function at `x`. + + Raises + ------ + `numpy.linalg.LinAlgError` + If the interpolation system is ill-defined. + """ + if self._debug: + assert x.shape == (self.n,), "The shape of `x` is not valid." + model = Quadratic(self.interpolation, self.fun_val, self._debug) + return model.grad(x, self.interpolation) + + def cub(self, x, mask=None): + """ + Evaluate the quadratic models of the nonlinear inequality functions at + a given point. + + Parameters + ---------- + x : `numpy.ndarray`, shape (n,) + Point at which to evaluate the quadratic models of the nonlinear + inequality functions. + mask : `numpy.ndarray`, shape (m_nonlinear_ub,), optional + Mask of the quadratic models to consider. + + Returns + ------- + `numpy.ndarray` + Values of the quadratic model of the nonlinear inequality + functions. + """ + if self._debug: + assert x.shape == (self.n,), "The shape of `x` is not valid." + assert mask is None or mask.shape == ( + self.m_nonlinear_ub, + ), "The shape of `mask` is not valid." + return np.array( + [model(x, self.interpolation) for model in self._get_cub(mask)] + ) + + def cub_grad(self, x, mask=None): + """ + Evaluate the gradients of the quadratic models of the nonlinear + inequality functions at a given point. + + Parameters + ---------- + x : `numpy.ndarray`, shape (n,) + Point at which to evaluate the gradients of the quadratic models of + the nonlinear inequality functions. + mask : `numpy.ndarray`, shape (m_nonlinear_eq,), optional + Mask of the quadratic models to consider. + + Returns + ------- + `numpy.ndarray` + Gradients of the quadratic model of the nonlinear inequality + functions. + """ + if self._debug: + assert x.shape == (self.n,), "The shape of `x` is not valid." + assert mask is None or mask.shape == ( + self.m_nonlinear_ub, + ), "The shape of `mask` is not valid." + return np.reshape( + [model.grad(x, self.interpolation) + for model in self._get_cub(mask)], + (-1, self.n), + ) + + def cub_hess(self, mask=None): + """ + Evaluate the Hessian matrices of the quadratic models of the nonlinear + inequality functions. + + Parameters + ---------- + mask : `numpy.ndarray`, shape (m_nonlinear_ub,), optional + Mask of the quadratic models to consider. + + Returns + ------- + `numpy.ndarray` + Hessian matrices of the quadratic models of the nonlinear + inequality functions. + """ + if self._debug: + assert mask is None or mask.shape == ( + self.m_nonlinear_ub, + ), "The shape of `mask` is not valid." + return np.reshape( + [model.hess(self.interpolation) for model in self._get_cub(mask)], + (-1, self.n, self.n), + ) + + def cub_hess_prod(self, v, mask=None): + """ + Evaluate the right product of the Hessian matrices of the quadratic + models of the nonlinear inequality functions with a given vector. + + Parameters + ---------- + v : `numpy.ndarray`, shape (n,) + Vector with which the Hessian matrices of the quadratic models of + the nonlinear inequality functions are multiplied from the right. + mask : `numpy.ndarray`, shape (m_nonlinear_ub,), optional + Mask of the quadratic models to consider. + + Returns + ------- + `numpy.ndarray` + Right products of the Hessian matrices of the quadratic models of + the nonlinear inequality functions with `v`. + """ + if self._debug: + assert v.shape == (self.n,), "The shape of `v` is not valid." + assert mask is None or mask.shape == ( + self.m_nonlinear_ub, + ), "The shape of `mask` is not valid." + return np.reshape( + [ + model.hess_prod(v, self.interpolation) + for model in self._get_cub(mask) + ], + (-1, self.n), + ) + + def cub_curv(self, v, mask=None): + """ + Evaluate the curvature of the quadratic models of the nonlinear + inequality functions along a given direction. + + Parameters + ---------- + v : `numpy.ndarray`, shape (n,) + Direction along which the curvature of the quadratic models of the + nonlinear inequality functions is evaluated. + mask : `numpy.ndarray`, shape (m_nonlinear_ub,), optional + Mask of the quadratic models to consider. + + Returns + ------- + `numpy.ndarray` + Curvature of the quadratic models of the nonlinear inequality + functions along `v`. + """ + if self._debug: + assert v.shape == (self.n,), "The shape of `v` is not valid." + assert mask is None or mask.shape == ( + self.m_nonlinear_ub, + ), "The shape of `mask` is not valid." + return np.array( + [model.curv(v, self.interpolation) + for model in self._get_cub(mask)] + ) + + def ceq(self, x, mask=None): + """ + Evaluate the quadratic models of the nonlinear equality functions at a + given point. + + Parameters + ---------- + x : `numpy.ndarray`, shape (n,) + Point at which to evaluate the quadratic models of the nonlinear + equality functions. + mask : `numpy.ndarray`, shape (m_nonlinear_eq,), optional + Mask of the quadratic models to consider. + + Returns + ------- + `numpy.ndarray` + Values of the quadratic model of the nonlinear equality functions. + """ + if self._debug: + assert x.shape == (self.n,), "The shape of `x` is not valid." + assert mask is None or mask.shape == ( + self.m_nonlinear_eq, + ), "The shape of `mask` is not valid." + return np.array( + [model(x, self.interpolation) for model in self._get_ceq(mask)] + ) + + def ceq_grad(self, x, mask=None): + """ + Evaluate the gradients of the quadratic models of the nonlinear + equality functions at a given point. + + Parameters + ---------- + x : `numpy.ndarray`, shape (n,) + Point at which to evaluate the gradients of the quadratic models of + the nonlinear equality functions. + mask : `numpy.ndarray`, shape (m_nonlinear_eq,), optional + Mask of the quadratic models to consider. + + Returns + ------- + `numpy.ndarray` + Gradients of the quadratic model of the nonlinear equality + functions. + """ + if self._debug: + assert x.shape == (self.n,), "The shape of `x` is not valid." + assert mask is None or mask.shape == ( + self.m_nonlinear_eq, + ), "The shape of `mask` is not valid." + return np.reshape( + [model.grad(x, self.interpolation) + for model in self._get_ceq(mask)], + (-1, self.n), + ) + + def ceq_hess(self, mask=None): + """ + Evaluate the Hessian matrices of the quadratic models of the nonlinear + equality functions. + + Parameters + ---------- + mask : `numpy.ndarray`, shape (m_nonlinear_eq,), optional + Mask of the quadratic models to consider. + + Returns + ------- + `numpy.ndarray` + Hessian matrices of the quadratic models of the nonlinear equality + functions. + """ + if self._debug: + assert mask is None or mask.shape == ( + self.m_nonlinear_eq, + ), "The shape of `mask` is not valid." + return np.reshape( + [model.hess(self.interpolation) for model in self._get_ceq(mask)], + (-1, self.n, self.n), + ) + + def ceq_hess_prod(self, v, mask=None): + """ + Evaluate the right product of the Hessian matrices of the quadratic + models of the nonlinear equality functions with a given vector. + + Parameters + ---------- + v : `numpy.ndarray`, shape (n,) + Vector with which the Hessian matrices of the quadratic models of + the nonlinear equality functions are multiplied from the right. + mask : `numpy.ndarray`, shape (m_nonlinear_eq,), optional + Mask of the quadratic models to consider. + + Returns + ------- + `numpy.ndarray` + Right products of the Hessian matrices of the quadratic models of + the nonlinear equality functions with `v`. + """ + if self._debug: + assert v.shape == (self.n,), "The shape of `v` is not valid." + assert mask is None or mask.shape == ( + self.m_nonlinear_eq, + ), "The shape of `mask` is not valid." + return np.reshape( + [ + model.hess_prod(v, self.interpolation) + for model in self._get_ceq(mask) + ], + (-1, self.n), + ) + + def ceq_curv(self, v, mask=None): + """ + Evaluate the curvature of the quadratic models of the nonlinear + equality functions along a given direction. + + Parameters + ---------- + v : `numpy.ndarray`, shape (n,) + Direction along which the curvature of the quadratic models of the + nonlinear equality functions is evaluated. + mask : `numpy.ndarray`, shape (m_nonlinear_eq,), optional + Mask of the quadratic models to consider. + + Returns + ------- + `numpy.ndarray` + Curvature of the quadratic models of the nonlinear equality + functions along `v`. + """ + if self._debug: + assert v.shape == (self.n,), "The shape of `v` is not valid." + assert mask is None or mask.shape == ( + self.m_nonlinear_eq, + ), "The shape of `mask` is not valid." + return np.array( + [model.curv(v, self.interpolation) + for model in self._get_ceq(mask)] + ) + + def reset_models(self): + """ + Set the quadratic models of the objective function, nonlinear + inequality constraints, and nonlinear equality constraints to the + alternative quadratic models. + + Raises + ------ + `numpy.linalg.LinAlgError` + If the interpolation system is ill-defined. + """ + self._fun = Quadratic(self.interpolation, self.fun_val, self._debug) + for i in range(self.m_nonlinear_ub): + self._cub[i] = Quadratic( + self.interpolation, + self.cub_val[:, i], + self._debug, + ) + for i in range(self.m_nonlinear_eq): + self._ceq[i] = Quadratic( + self.interpolation, + self.ceq_val[:, i], + self._debug, + ) + if self._debug: + self._check_interpolation_conditions() + + def update_interpolation(self, k_new, x_new, fun_val, cub_val, ceq_val): + """ + Update the interpolation set. + + This method updates the interpolation set by replacing the `knew`-th + interpolation point with `xnew`. It also updates the function values + and the quadratic models. + + Parameters + ---------- + k_new : int + Index of the updated interpolation point. + x_new : `numpy.ndarray`, shape (n,) + New interpolation point. Its value is interpreted as relative to + the origin, not the base point. + fun_val : float + Value of the objective function at `x_new`. + Objective function value at `x_new`. + cub_val : `numpy.ndarray`, shape (m_nonlinear_ub,) + Values of the nonlinear inequality constraints at `x_new`. + ceq_val : `numpy.ndarray`, shape (m_nonlinear_eq,) + Values of the nonlinear equality constraints at `x_new`. + + Raises + ------ + `numpy.linalg.LinAlgError` + If the interpolation system is ill-defined. + """ + if self._debug: + assert 0 <= k_new < self.npt, "The index `k_new` is not valid." + assert x_new.shape == (self.n,), \ + "The shape of `x_new` is not valid." + assert isinstance(fun_val, float), \ + "The function value is not valid." + assert cub_val.shape == ( + self.m_nonlinear_ub, + ), "The shape of `cub_val` is not valid." + assert ceq_val.shape == ( + self.m_nonlinear_eq, + ), "The shape of `ceq_val` is not valid." + + # Compute the updates in the interpolation conditions. + fun_diff = np.zeros(self.npt) + cub_diff = np.zeros(self.cub_val.shape) + ceq_diff = np.zeros(self.ceq_val.shape) + fun_diff[k_new] = fun_val - self.fun(x_new) + cub_diff[k_new, :] = cub_val - self.cub(x_new) + ceq_diff[k_new, :] = ceq_val - self.ceq(x_new) + + # Update the function values. + self.fun_val[k_new] = fun_val + self.cub_val[k_new, :] = cub_val + self.ceq_val[k_new, :] = ceq_val + + # Update the interpolation set. + dir_old = np.copy(self.interpolation.xpt[:, k_new]) + self.interpolation.xpt[:, k_new] = x_new - self.interpolation.x_base + + # Update the quadratic models. + ill_conditioned = self._fun.update( + self.interpolation, + k_new, + dir_old, + fun_diff, + ) + for i in range(self.m_nonlinear_ub): + ill_conditioned = ill_conditioned or self._cub[i].update( + self.interpolation, + k_new, + dir_old, + cub_diff[:, i], + ) + for i in range(self.m_nonlinear_eq): + ill_conditioned = ill_conditioned or self._ceq[i].update( + self.interpolation, + k_new, + dir_old, + ceq_diff[:, i], + ) + if self._debug: + self._check_interpolation_conditions() + return ill_conditioned + + def determinants(self, x_new, k_new=None): + """ + Compute the normalized determinants of the new interpolation systems. + + Parameters + ---------- + x_new : `numpy.ndarray`, shape (n,) + New interpolation point. Its value is interpreted as relative to + the origin, not the base point. + k_new : int, optional + Index of the updated interpolation point. If `k_new` is not + specified, all the possible determinants are computed. + + Returns + ------- + {float, `numpy.ndarray`, shape (npt,)} + Determinant(s) of the new interpolation system. + + Raises + ------ + `numpy.linalg.LinAlgError` + If the interpolation system is ill-defined. + + Notes + ----- + The determinants are normalized by the determinant of the current + interpolation system. For stability reasons, the calculations are done + using the formula (2.12) in [1]_. + + References + ---------- + .. [1] M. J. D. Powell. On updating the inverse of a KKT matrix. + Technical Report DAMTP 2004/NA01, Department of Applied Mathematics + and Theoretical Physics, University of Cambridge, Cambridge, UK, + 2004. + """ + if self._debug: + assert x_new.shape == (self.n,), \ + "The shape of `x_new` is not valid." + assert ( + k_new is None or 0 <= k_new < self.npt + ), "The index `k_new` is not valid." + + # Compute the values independent of k_new. + shift = x_new - self.interpolation.x_base + new_col = np.empty((self.npt + self.n + 1, 1)) + new_col[: self.npt, 0] = ( + 0.5 * (self.interpolation.xpt.T @ shift) ** 2.0) + new_col[self.npt, 0] = 1.0 + new_col[self.npt + 1:, 0] = shift + inv_new_col = Quadratic.solve_systems(self.interpolation, new_col)[0] + beta = 0.5 * (shift @ shift) ** 2.0 - new_col[:, 0] @ inv_new_col[:, 0] + + # Compute the values that depend on k. + if k_new is None: + coord_vec = np.eye(self.npt + self.n + 1, self.npt) + alpha = np.diag( + Quadratic.solve_systems( + self.interpolation, + coord_vec, + )[0] + ) + tau = inv_new_col[: self.npt, 0] + else: + coord_vec = np.eye(self.npt + self.n + 1, 1, -k_new) + alpha = Quadratic.solve_systems( + self.interpolation, + coord_vec, + )[ + 0 + ][k_new, 0] + tau = inv_new_col[k_new, 0] + return alpha * beta + tau**2.0 + + def shift_x_base(self, new_x_base, options): + """ + Shift the base point without changing the interpolation set. + + Parameters + ---------- + new_x_base : `numpy.ndarray`, shape (n,) + New base point. + options : dict + Options of the solver. + """ + if self._debug: + assert new_x_base.shape == ( + self.n, + ), "The shape of `new_x_base` is not valid." + + # Update the models. + self._fun.shift_x_base(self.interpolation, new_x_base) + for model in self._cub: + model.shift_x_base(self.interpolation, new_x_base) + for model in self._ceq: + model.shift_x_base(self.interpolation, new_x_base) + + # Update the base point and the interpolation points. + shift = new_x_base - self.interpolation.x_base + self.interpolation.x_base += shift + self.interpolation.xpt -= shift[:, np.newaxis] + if options[Options.DEBUG]: + self._check_interpolation_conditions() + + def _get_cub(self, mask=None): + """ + Get the quadratic models of the nonlinear inequality constraints. + + Parameters + ---------- + mask : `numpy.ndarray`, shape (m_nonlinear_ub,), optional + Mask of the quadratic models to return. + + Returns + ------- + `numpy.ndarray` + Quadratic models of the nonlinear inequality constraints. + """ + return self._cub if mask is None else self._cub[mask] + + def _get_ceq(self, mask=None): + """ + Get the quadratic models of the nonlinear equality constraints. + + Parameters + ---------- + mask : `numpy.ndarray`, shape (m_nonlinear_eq,), optional + Mask of the quadratic models to return. + + Returns + ------- + `numpy.ndarray` + Quadratic models of the nonlinear equality constraints. + """ + return self._ceq if mask is None else self._ceq[mask] + + def _check_interpolation_conditions(self): + """ + Check the interpolation conditions of all quadratic models. + """ + error_fun = 0.0 + error_cub = 0.0 + error_ceq = 0.0 + for k in range(self.npt): + error_fun = np.max( + [ + error_fun, + np.abs( + self.fun(self.interpolation.point(k)) - self.fun_val[k] + ), + ] + ) + error_cub = np.max( + np.abs( + self.cub(self.interpolation.point(k)) - self.cub_val[k, :] + ), + initial=error_cub, + ) + error_ceq = np.max( + np.abs( + self.ceq(self.interpolation.point(k)) - self.ceq_val[k, :] + ), + initial=error_ceq, + ) + tol = 10.0 * np.sqrt(EPS) * max(self.n, self.npt) + if error_fun > tol * np.max(np.abs(self.fun_val), initial=1.0): + warnings.warn( + "The interpolation conditions for the objective function are " + "not satisfied.", + RuntimeWarning, + 2, + ) + if error_cub > tol * np.max(np.abs(self.cub_val), initial=1.0): + warnings.warn( + "The interpolation conditions for the inequality constraint " + "function are not satisfied.", + RuntimeWarning, + 2, + ) + if error_ceq > tol * np.max(np.abs(self.ceq_val), initial=1.0): + warnings.warn( + "The interpolation conditions for the equality constraint " + "function are not satisfied.", + RuntimeWarning, + 2, + ) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/problem.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/problem.py new file mode 100644 index 0000000000000000000000000000000000000000..2dbebce3a48067e97da2b75bd2cdd609e01029b2 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/problem.py @@ -0,0 +1,1296 @@ +from contextlib import suppress +from inspect import signature +import copy + +import numpy as np +from scipy.optimize import ( + Bounds, + LinearConstraint, + NonlinearConstraint, + OptimizeResult, +) +from scipy.optimize._constraints import PreparedConstraint + + +from .settings import PRINT_OPTIONS, BARRIER +from .utils import CallbackSuccess, get_arrays_tol +from .utils import exact_1d_array + + +class ObjectiveFunction: + """ + Real-valued objective function. + """ + + def __init__(self, fun, verbose, debug, *args): + """ + Initialize the objective function. + + Parameters + ---------- + fun : {callable, None} + Function to evaluate, or None. + + ``fun(x, *args) -> float`` + + where ``x`` is an array with shape (n,) and `args` is a tuple. + verbose : bool + Whether to print the function evaluations. + debug : bool + Whether to make debugging tests during the execution. + *args : tuple + Additional arguments to be passed to the function. + """ + if debug: + assert fun is None or callable(fun) + assert isinstance(verbose, bool) + assert isinstance(debug, bool) + + self._fun = fun + self._verbose = verbose + self._args = args + self._n_eval = 0 + + def __call__(self, x): + """ + Evaluate the objective function. + + Parameters + ---------- + x : array_like, shape (n,) + Point at which the objective function is evaluated. + + Returns + ------- + float + Function value at `x`. + """ + x = np.array(x, dtype=float) + if self._fun is None: + f = 0.0 + else: + f = float(np.squeeze(self._fun(x, *self._args))) + self._n_eval += 1 + if self._verbose: + with np.printoptions(**PRINT_OPTIONS): + print(f"{self.name}({x}) = {f}") + return f + + @property + def n_eval(self): + """ + Number of function evaluations. + + Returns + ------- + int + Number of function evaluations. + """ + return self._n_eval + + @property + def name(self): + """ + Name of the objective function. + + Returns + ------- + str + Name of the objective function. + """ + name = "" + if self._fun is not None: + try: + name = self._fun.__name__ + except AttributeError: + name = "fun" + return name + + +class BoundConstraints: + """ + Bound constraints ``xl <= x <= xu``. + """ + + def __init__(self, bounds): + """ + Initialize the bound constraints. + + Parameters + ---------- + bounds : scipy.optimize.Bounds + Bound constraints. + """ + self._xl = np.array(bounds.lb, float) + self._xu = np.array(bounds.ub, float) + + # Remove the ill-defined bounds. + self.xl[np.isnan(self.xl)] = -np.inf + self.xu[np.isnan(self.xu)] = np.inf + + self.is_feasible = ( + np.all(self.xl <= self.xu) + and np.all(self.xl < np.inf) + and np.all(self.xu > -np.inf) + ) + self.m = np.count_nonzero(self.xl > -np.inf) + np.count_nonzero( + self.xu < np.inf + ) + self.pcs = PreparedConstraint(bounds, np.ones(bounds.lb.size)) + + @property + def xl(self): + """ + Lower bound. + + Returns + ------- + `numpy.ndarray`, shape (n,) + Lower bound. + """ + return self._xl + + @property + def xu(self): + """ + Upper bound. + + Returns + ------- + `numpy.ndarray`, shape (n,) + Upper bound. + """ + return self._xu + + def maxcv(self, x): + """ + Evaluate the maximum constraint violation. + + Parameters + ---------- + x : array_like, shape (n,) + Point at which the maximum constraint violation is evaluated. + + Returns + ------- + float + Maximum constraint violation at `x`. + """ + x = np.asarray(x, dtype=float) + return self.violation(x) + + def violation(self, x): + # shortcut for no bounds + if self.is_feasible: + return np.array([0]) + else: + return self.pcs.violation(x) + + def project(self, x): + """ + Project a point onto the feasible set. + + Parameters + ---------- + x : array_like, shape (n,) + Point to be projected. + + Returns + ------- + `numpy.ndarray`, shape (n,) + Projection of `x` onto the feasible set. + """ + return np.clip(x, self.xl, self.xu) if self.is_feasible else x + + +class LinearConstraints: + """ + Linear constraints ``a_ub @ x <= b_ub`` and ``a_eq @ x == b_eq``. + """ + + def __init__(self, constraints, n, debug): + """ + Initialize the linear constraints. + + Parameters + ---------- + constraints : list of LinearConstraint + Linear constraints. + n : int + Number of variables. + debug : bool + Whether to make debugging tests during the execution. + """ + if debug: + assert isinstance(constraints, list) + for constraint in constraints: + assert isinstance(constraint, LinearConstraint) + assert isinstance(debug, bool) + + self._a_ub = np.empty((0, n)) + self._b_ub = np.empty(0) + self._a_eq = np.empty((0, n)) + self._b_eq = np.empty(0) + for constraint in constraints: + is_equality = np.abs( + constraint.ub - constraint.lb + ) <= get_arrays_tol(constraint.lb, constraint.ub) + if np.any(is_equality): + self._a_eq = np.vstack((self.a_eq, constraint.A[is_equality])) + self._b_eq = np.concatenate( + ( + self.b_eq, + 0.5 + * ( + constraint.lb[is_equality] + + constraint.ub[is_equality] + ), + ) + ) + if not np.all(is_equality): + self._a_ub = np.vstack( + ( + self.a_ub, + constraint.A[~is_equality], + -constraint.A[~is_equality], + ) + ) + self._b_ub = np.concatenate( + ( + self.b_ub, + constraint.ub[~is_equality], + -constraint.lb[~is_equality], + ) + ) + + # Remove the ill-defined constraints. + self.a_ub[np.isnan(self.a_ub)] = 0.0 + self.a_eq[np.isnan(self.a_eq)] = 0.0 + undef_ub = np.isnan(self.b_ub) | np.isinf(self.b_ub) + undef_eq = np.isnan(self.b_eq) + self._a_ub = self.a_ub[~undef_ub, :] + self._b_ub = self.b_ub[~undef_ub] + self._a_eq = self.a_eq[~undef_eq, :] + self._b_eq = self.b_eq[~undef_eq] + self.pcs = [ + PreparedConstraint(c, np.ones(n)) for c in constraints if c.A.size + ] + + @property + def a_ub(self): + """ + Left-hand side matrix of the linear inequality constraints. + + Returns + ------- + `numpy.ndarray`, shape (m, n) + Left-hand side matrix of the linear inequality constraints. + """ + return self._a_ub + + @property + def b_ub(self): + """ + Right-hand side vector of the linear inequality constraints. + + Returns + ------- + `numpy.ndarray`, shape (m, n) + Right-hand side vector of the linear inequality constraints. + """ + return self._b_ub + + @property + def a_eq(self): + """ + Left-hand side matrix of the linear equality constraints. + + Returns + ------- + `numpy.ndarray`, shape (m, n) + Left-hand side matrix of the linear equality constraints. + """ + return self._a_eq + + @property + def b_eq(self): + """ + Right-hand side vector of the linear equality constraints. + + Returns + ------- + `numpy.ndarray`, shape (m, n) + Right-hand side vector of the linear equality constraints. + """ + return self._b_eq + + @property + def m_ub(self): + """ + Number of linear inequality constraints. + + Returns + ------- + int + Number of linear inequality constraints. + """ + return self.b_ub.size + + @property + def m_eq(self): + """ + Number of linear equality constraints. + + Returns + ------- + int + Number of linear equality constraints. + """ + return self.b_eq.size + + def maxcv(self, x): + """ + Evaluate the maximum constraint violation. + + Parameters + ---------- + x : array_like, shape (n,) + Point at which the maximum constraint violation is evaluated. + + Returns + ------- + float + Maximum constraint violation at `x`. + """ + return np.max(self.violation(x), initial=0.0) + + def violation(self, x): + if len(self.pcs): + return np.concatenate([pc.violation(x) for pc in self.pcs]) + return np.array([]) + + +class NonlinearConstraints: + """ + Nonlinear constraints ``c_ub(x) <= 0`` and ``c_eq(x) == b_eq``. + """ + + def __init__(self, constraints, verbose, debug): + """ + Initialize the nonlinear constraints. + + Parameters + ---------- + constraints : list + Nonlinear constraints. + verbose : bool + Whether to print the function evaluations. + debug : bool + Whether to make debugging tests during the execution. + """ + if debug: + assert isinstance(constraints, list) + for constraint in constraints: + assert isinstance(constraint, NonlinearConstraint) + assert isinstance(verbose, bool) + assert isinstance(debug, bool) + + self._constraints = constraints + self.pcs = [] + self._verbose = verbose + + # map of indexes for equality and inequality constraints + self._map_ub = None + self._map_eq = None + self._m_ub = self._m_eq = None + + def __call__(self, x): + """ + Calculates the residual (slack) for the constraints. + + Parameters + ---------- + x : array_like, shape (n,) + Point at which the constraints are evaluated. + + Returns + ------- + `numpy.ndarray`, shape (m_nonlinear_ub,) + Nonlinear inequality constraint slack values. + `numpy.ndarray`, shape (m_nonlinear_eq,) + Nonlinear equality constraint slack values. + """ + if not len(self._constraints): + self._m_eq = self._m_ub = 0 + return np.array([]), np.array([]) + + x = np.array(x, dtype=float) + # first time around the constraints haven't been prepared + if not len(self.pcs): + self._map_ub = [] + self._map_eq = [] + self._m_eq = 0 + self._m_ub = 0 + + for constraint in self._constraints: + if not callable(constraint.jac): + # having a callable constraint function prevents + # constraint.fun from being evaluated when preparing + # constraint + c = copy.copy(constraint) + c.jac = lambda x0: x0 + c.hess = lambda x0, v: 0.0 + pc = PreparedConstraint(c, x) + else: + pc = PreparedConstraint(constraint, x) + # we're going to be using the same x value again immediately + # after this initialisation + pc.fun.f_updated = True + + self.pcs.append(pc) + idx = np.arange(pc.fun.m) + + # figure out equality and inequality maps + lb, ub = pc.bounds[0], pc.bounds[1] + arr_tol = get_arrays_tol(lb, ub) + is_equality = np.abs(ub - lb) <= arr_tol + self._map_eq.append(idx[is_equality]) + self._map_ub.append(idx[~is_equality]) + + # these values will be corrected to their proper values later + self._m_eq += np.count_nonzero(is_equality) + self._m_ub += np.count_nonzero(~is_equality) + + c_ub = [] + c_eq = [] + for i, pc in enumerate(self.pcs): + val = pc.fun.fun(x) + if self._verbose: + with np.printoptions(**PRINT_OPTIONS): + with suppress(AttributeError): + fun_name = self._constraints[i].fun.__name__ + print(f"{fun_name}({x}) = {val}") + + # separate violations into c_eq and c_ub + eq_idx = self._map_eq[i] + ub_idx = self._map_ub[i] + + ub_val = val[ub_idx] + if len(ub_idx): + xl = pc.bounds[0][ub_idx] + xu = pc.bounds[1][ub_idx] + + # calculate slack within lower bound + finite_xl = xl > -np.inf + _v = xl[finite_xl] - ub_val[finite_xl] + c_ub.append(_v) + + # calculate slack within lower bound + finite_xu = xu < np.inf + _v = ub_val[finite_xu] - xu[finite_xu] + c_ub.append(_v) + + # equality constraints taken from midpoint between lb and ub + eq_val = val[eq_idx] + if len(eq_idx): + midpoint = 0.5 * (pc.bounds[1][eq_idx] + pc.bounds[0][eq_idx]) + eq_val -= midpoint + c_eq.append(eq_val) + + if self._m_eq: + c_eq = np.concatenate(c_eq) + else: + c_eq = np.array([]) + + if self._m_ub: + c_ub = np.concatenate(c_ub) + else: + c_ub = np.array([]) + + self._m_ub = c_ub.size + self._m_eq = c_eq.size + + return c_ub, c_eq + + @property + def m_ub(self): + """ + Number of nonlinear inequality constraints. + + Returns + ------- + int + Number of nonlinear inequality constraints. + + Raises + ------ + ValueError + If the number of nonlinear inequality constraints is unknown. + """ + if self._m_ub is None: + raise ValueError( + "The number of nonlinear inequality constraints is unknown." + ) + else: + return self._m_ub + + @property + def m_eq(self): + """ + Number of nonlinear equality constraints. + + Returns + ------- + int + Number of nonlinear equality constraints. + + Raises + ------ + ValueError + If the number of nonlinear equality constraints is unknown. + """ + if self._m_eq is None: + raise ValueError( + "The number of nonlinear equality constraints is unknown." + ) + else: + return self._m_eq + + @property + def n_eval(self): + """ + Number of function evaluations. + + Returns + ------- + int + Number of function evaluations. + """ + if len(self.pcs): + return self.pcs[0].fun.nfev + else: + return 0 + + def maxcv(self, x, cub_val=None, ceq_val=None): + """ + Evaluate the maximum constraint violation. + + Parameters + ---------- + x : array_like, shape (n,) + Point at which the maximum constraint violation is evaluated. + cub_val : array_like, shape (m_nonlinear_ub,), optional + Values of the nonlinear inequality constraints. If not provided, + the nonlinear inequality constraints are evaluated at `x`. + ceq_val : array_like, shape (m_nonlinear_eq,), optional + Values of the nonlinear equality constraints. If not provided, + the nonlinear equality constraints are evaluated at `x`. + + Returns + ------- + float + Maximum constraint violation at `x`. + """ + return np.max( + self.violation(x, cub_val=cub_val, ceq_val=ceq_val), initial=0.0 + ) + + def violation(self, x, cub_val=None, ceq_val=None): + return np.concatenate([pc.violation(x) for pc in self.pcs]) + + +class Problem: + """ + Optimization problem. + """ + + def __init__( + self, + obj, + x0, + bounds, + linear, + nonlinear, + callback, + feasibility_tol, + scale, + store_history, + history_size, + filter_size, + debug, + ): + """ + Initialize the nonlinear problem. + + The problem is preprocessed to remove all the variables that are fixed + by the bound constraints. + + Parameters + ---------- + obj : ObjectiveFunction + Objective function. + x0 : array_like, shape (n,) + Initial guess. + bounds : BoundConstraints + Bound constraints. + linear : LinearConstraints + Linear constraints. + nonlinear : NonlinearConstraints + Nonlinear constraints. + callback : {callable, None} + Callback function. + feasibility_tol : float + Tolerance on the constraint violation. + scale : bool + Whether to scale the problem according to the bounds. + store_history : bool + Whether to store the function evaluations. + history_size : int + Maximum number of function evaluations to store. + filter_size : int + Maximum number of points in the filter. + debug : bool + Whether to make debugging tests during the execution. + """ + if debug: + assert isinstance(obj, ObjectiveFunction) + assert isinstance(bounds, BoundConstraints) + assert isinstance(linear, LinearConstraints) + assert isinstance(nonlinear, NonlinearConstraints) + assert isinstance(feasibility_tol, float) + assert isinstance(scale, bool) + assert isinstance(store_history, bool) + assert isinstance(history_size, int) + if store_history: + assert history_size > 0 + assert isinstance(filter_size, int) + assert filter_size > 0 + assert isinstance(debug, bool) + + self._obj = obj + self._linear = linear + self._nonlinear = nonlinear + if callback is not None: + if not callable(callback): + raise TypeError("The callback must be a callable function.") + self._callback = callback + + # Check the consistency of the problem. + x0 = exact_1d_array(x0, "The initial guess must be a vector.") + n = x0.size + if bounds.xl.size != n: + raise ValueError(f"The bounds must have {n} elements.") + if linear.a_ub.shape[1] != n: + raise ValueError( + f"The left-hand side matrices of the linear constraints must " + f"have {n} columns." + ) + + # Check which variables are fixed. + tol = get_arrays_tol(bounds.xl, bounds.xu) + self._fixed_idx = (bounds.xl <= bounds.xu) & ( + np.abs(bounds.xl - bounds.xu) < tol + ) + self._fixed_val = 0.5 * ( + bounds.xl[self._fixed_idx] + bounds.xu[self._fixed_idx] + ) + self._fixed_val = np.clip( + self._fixed_val, + bounds.xl[self._fixed_idx], + bounds.xu[self._fixed_idx], + ) + + # Set the bound constraints. + self._orig_bounds = bounds + self._bounds = BoundConstraints( + Bounds(bounds.xl[~self._fixed_idx], bounds.xu[~self._fixed_idx]) + ) + + # Set the initial guess. + self._x0 = self._bounds.project(x0[~self._fixed_idx]) + + # Set the linear constraints. + b_eq = linear.b_eq - linear.a_eq[:, self._fixed_idx] @ self._fixed_val + self._linear = LinearConstraints( + [ + LinearConstraint( + linear.a_ub[:, ~self._fixed_idx], + -np.inf, + linear.b_ub + - linear.a_ub[:, self._fixed_idx] @ self._fixed_val, + ), + LinearConstraint(linear.a_eq[:, ~self._fixed_idx], b_eq, b_eq), + ], + self.n, + debug, + ) + + # Scale the problem if necessary. + scale = ( + scale + and self._bounds.is_feasible + and np.all(np.isfinite(self._bounds.xl)) + and np.all(np.isfinite(self._bounds.xu)) + ) + if scale: + self._scaling_factor = 0.5 * (self._bounds.xu - self._bounds.xl) + self._scaling_shift = 0.5 * (self._bounds.xu + self._bounds.xl) + self._bounds = BoundConstraints( + Bounds(-np.ones(self.n), np.ones(self.n)) + ) + b_eq = self._linear.b_eq - self._linear.a_eq @ self._scaling_shift + self._linear = LinearConstraints( + [ + LinearConstraint( + self._linear.a_ub @ np.diag(self._scaling_factor), + -np.inf, + self._linear.b_ub + - self._linear.a_ub @ self._scaling_shift, + ), + LinearConstraint( + self._linear.a_eq @ np.diag(self._scaling_factor), + b_eq, + b_eq, + ), + ], + self.n, + debug, + ) + self._x0 = (self._x0 - self._scaling_shift) / self._scaling_factor + else: + self._scaling_factor = np.ones(self.n) + self._scaling_shift = np.zeros(self.n) + + # Set the initial filter. + self._feasibility_tol = feasibility_tol + self._filter_size = filter_size + self._fun_filter = [] + self._maxcv_filter = [] + self._x_filter = [] + + # Set the initial history. + self._store_history = store_history + self._history_size = history_size + self._fun_history = [] + self._maxcv_history = [] + self._x_history = [] + + def __call__(self, x, penalty=0.0): + """ + Evaluate the objective and nonlinear constraint functions. + + Parameters + ---------- + x : array_like, shape (n,) + Point at which the functions are evaluated. + penalty : float, optional + Penalty parameter used to select the point in the filter to forward + to the callback function. + + Returns + ------- + float + Objective function value. + `numpy.ndarray`, shape (m_nonlinear_ub,) + Nonlinear inequality constraint function values. + `numpy.ndarray`, shape (m_nonlinear_eq,) + Nonlinear equality constraint function values. + + Raises + ------ + `cobyqa.utils.CallbackSuccess` + If the callback function raises a ``StopIteration``. + """ + # Evaluate the objective and nonlinear constraint functions. + x = np.asarray(x, dtype=float) + x_full = self.build_x(x) + fun_val = self._obj(x_full) + cub_val, ceq_val = self._nonlinear(x_full) + maxcv_val = self.maxcv(x, cub_val, ceq_val) + if self._store_history: + self._fun_history.append(fun_val) + self._maxcv_history.append(maxcv_val) + self._x_history.append(x) + if len(self._fun_history) > self._history_size: + self._fun_history.pop(0) + self._maxcv_history.pop(0) + self._x_history.pop(0) + + # Add the point to the filter if it is not dominated by any point. + if np.isnan(fun_val) and np.isnan(maxcv_val): + include_point = len(self._fun_filter) == 0 + elif np.isnan(fun_val): + include_point = all( + np.isnan(fun_filter) + and maxcv_val < maxcv_filter + or np.isnan(maxcv_filter) + for fun_filter, maxcv_filter in zip( + self._fun_filter, + self._maxcv_filter, + ) + ) + elif np.isnan(maxcv_val): + include_point = all( + np.isnan(maxcv_filter) + and fun_val < fun_filter + or np.isnan(fun_filter) + for fun_filter, maxcv_filter in zip( + self._fun_filter, + self._maxcv_filter, + ) + ) + else: + include_point = all( + fun_val < fun_filter or maxcv_val < maxcv_filter + for fun_filter, maxcv_filter in zip( + self._fun_filter, + self._maxcv_filter, + ) + ) + if include_point: + self._fun_filter.append(fun_val) + self._maxcv_filter.append(maxcv_val) + self._x_filter.append(x) + + # Remove the points in the filter that are dominated by the new + # point. We must iterate in reverse order to avoid problems when + # removing elements from the list. + for k in range(len(self._fun_filter) - 2, -1, -1): + if np.isnan(fun_val): + remove_point = np.isnan(self._fun_filter[k]) + elif np.isnan(maxcv_val): + remove_point = np.isnan(self._maxcv_filter[k]) + else: + remove_point = ( + np.isnan(self._fun_filter[k]) + or np.isnan(self._maxcv_filter[k]) + or fun_val <= self._fun_filter[k] + and maxcv_val <= self._maxcv_filter[k] + ) + if remove_point: + self._fun_filter.pop(k) + self._maxcv_filter.pop(k) + self._x_filter.pop(k) + + # Keep only the most recent points in the filter. + if len(self._fun_filter) > self._filter_size: + self._fun_filter.pop(0) + self._maxcv_filter.pop(0) + self._x_filter.pop(0) + + # Evaluate the callback function after updating the filter to ensure + # that the current point can be returned by the method. + if self._callback is not None: + sig = signature(self._callback) + try: + x_best, fun_best, _ = self.best_eval(penalty) + x_best = self.build_x(x_best) + if set(sig.parameters) == {"intermediate_result"}: + intermediate_result = OptimizeResult( + x=x_best, + fun=fun_best, + # maxcv=maxcv_best, + ) + self._callback(intermediate_result=intermediate_result) + else: + self._callback(x_best) + except StopIteration as exc: + raise CallbackSuccess from exc + + # Apply the extreme barriers and return. + if np.isnan(fun_val): + fun_val = BARRIER + cub_val[np.isnan(cub_val)] = BARRIER + ceq_val[np.isnan(ceq_val)] = BARRIER + fun_val = max(min(fun_val, BARRIER), -BARRIER) + cub_val = np.maximum(np.minimum(cub_val, BARRIER), -BARRIER) + ceq_val = np.maximum(np.minimum(ceq_val, BARRIER), -BARRIER) + return fun_val, cub_val, ceq_val + + @property + def n(self): + """ + Number of variables. + + Returns + ------- + int + Number of variables. + """ + return self.x0.size + + @property + def n_orig(self): + """ + Number of variables in the original problem (with fixed variables). + + Returns + ------- + int + Number of variables in the original problem (with fixed variables). + """ + return self._fixed_idx.size + + @property + def x0(self): + """ + Initial guess. + + Returns + ------- + `numpy.ndarray`, shape (n,) + Initial guess. + """ + return self._x0 + + @property + def n_eval(self): + """ + Number of function evaluations. + + Returns + ------- + int + Number of function evaluations. + """ + return self._obj.n_eval + + @property + def fun_name(self): + """ + Name of the objective function. + + Returns + ------- + str + Name of the objective function. + """ + return self._obj.name + + @property + def bounds(self): + """ + Bound constraints. + + Returns + ------- + BoundConstraints + Bound constraints. + """ + return self._bounds + + @property + def linear(self): + """ + Linear constraints. + + Returns + ------- + LinearConstraints + Linear constraints. + """ + return self._linear + + @property + def m_bounds(self): + """ + Number of bound constraints. + + Returns + ------- + int + Number of bound constraints. + """ + return self.bounds.m + + @property + def m_linear_ub(self): + """ + Number of linear inequality constraints. + + Returns + ------- + int + Number of linear inequality constraints. + """ + return self.linear.m_ub + + @property + def m_linear_eq(self): + """ + Number of linear equality constraints. + + Returns + ------- + int + Number of linear equality constraints. + """ + return self.linear.m_eq + + @property + def m_nonlinear_ub(self): + """ + Number of nonlinear inequality constraints. + + Returns + ------- + int + Number of nonlinear inequality constraints. + + Raises + ------ + ValueError + If the number of nonlinear inequality constraints is not known. + """ + return self._nonlinear.m_ub + + @property + def m_nonlinear_eq(self): + """ + Number of nonlinear equality constraints. + + Returns + ------- + int + Number of nonlinear equality constraints. + + Raises + ------ + ValueError + If the number of nonlinear equality constraints is not known. + """ + return self._nonlinear.m_eq + + @property + def fun_history(self): + """ + History of objective function evaluations. + + Returns + ------- + `numpy.ndarray`, shape (n_eval,) + History of objective function evaluations. + """ + return np.array(self._fun_history, dtype=float) + + @property + def maxcv_history(self): + """ + History of maximum constraint violations. + + Returns + ------- + `numpy.ndarray`, shape (n_eval,) + History of maximum constraint violations. + """ + return np.array(self._maxcv_history, dtype=float) + + @property + def type(self): + """ + Type of the problem. + + The problem can be either 'unconstrained', 'bound-constrained', + 'linearly constrained', or 'nonlinearly constrained'. + + Returns + ------- + str + Type of the problem. + """ + try: + if self.m_nonlinear_ub > 0 or self.m_nonlinear_eq > 0: + return "nonlinearly constrained" + elif self.m_linear_ub > 0 or self.m_linear_eq > 0: + return "linearly constrained" + elif self.m_bounds > 0: + return "bound-constrained" + else: + return "unconstrained" + except ValueError: + # The number of nonlinear constraints is not known. It may be zero + # if the user provided a nonlinear inequality and/or equality + # constraint function that returns an empty array. However, as this + # is not known before the first call to the function, we assume + # that the problem is nonlinearly constrained. + return "nonlinearly constrained" + + @property + def is_feasibility(self): + """ + Whether the problem is a feasibility problem. + + Returns + ------- + bool + Whether the problem is a feasibility problem. + """ + return self.fun_name == "" + + def build_x(self, x): + """ + Build the full vector of variables from the reduced vector. + + Parameters + ---------- + x : array_like, shape (n,) + Reduced vector of variables. + + Returns + ------- + `numpy.ndarray`, shape (n_orig,) + Full vector of variables. + """ + x_full = np.empty(self.n_orig) + x_full[self._fixed_idx] = self._fixed_val + x_full[~self._fixed_idx] = (x * self._scaling_factor + + self._scaling_shift) + return self._orig_bounds.project(x_full) + + def maxcv(self, x, cub_val=None, ceq_val=None): + """ + Evaluate the maximum constraint violation. + + Parameters + ---------- + x : array_like, shape (n,) + Point at which the maximum constraint violation is evaluated. + cub_val : array_like, shape (m_nonlinear_ub,), optional + Values of the nonlinear inequality constraints. If not provided, + the nonlinear inequality constraints are evaluated at `x`. + ceq_val : array_like, shape (m_nonlinear_eq,), optional + Values of the nonlinear equality constraints. If not provided, + the nonlinear equality constraints are evaluated at `x`. + + Returns + ------- + float + Maximum constraint violation at `x`. + """ + violation = self.violation(x, cub_val=cub_val, ceq_val=ceq_val) + if np.count_nonzero(violation): + return np.max(violation, initial=0.0) + else: + return 0.0 + + def violation(self, x, cub_val=None, ceq_val=None): + violation = [] + if not self.bounds.is_feasible: + b = self.bounds.violation(x) + violation.append(b) + + if len(self.linear.pcs): + lc = self.linear.violation(x) + violation.append(lc) + if len(self._nonlinear.pcs): + nlc = self._nonlinear.violation(x, cub_val, ceq_val) + violation.append(nlc) + + if len(violation): + return np.concatenate(violation) + + def best_eval(self, penalty): + """ + Return the best point in the filter and the corresponding objective and + nonlinear constraint function evaluations. + + Parameters + ---------- + penalty : float + Penalty parameter + + Returns + ------- + `numpy.ndarray`, shape (n,) + Best point. + float + Corresponding objective function value. + float + Corresponding maximum constraint violation. + """ + # If the filter is empty, i.e., if no function evaluation has been + # performed, we evaluate the objective and nonlinear constraint + # functions at the initial guess. + if len(self._fun_filter) == 0: + self(self.x0) + + # Find the best point in the filter. + fun_filter = np.array(self._fun_filter) + maxcv_filter = np.array(self._maxcv_filter) + x_filter = np.array(self._x_filter) + finite_idx = np.isfinite(maxcv_filter) + if np.any(finite_idx): + # At least one point has a finite maximum constraint violation. + feasible_idx = maxcv_filter <= self._feasibility_tol + if np.any(feasible_idx) and not np.all( + np.isnan(fun_filter[feasible_idx]) + ): + # At least one point is feasible and has a well-defined + # objective function value. We select the point with the least + # objective function value. If there is a tie, we select the + # point with the least maximum constraint violation. If there + # is still a tie, we select the most recent point. + fun_min_idx = feasible_idx & ( + fun_filter <= np.nanmin(fun_filter[feasible_idx]) + ) + if np.count_nonzero(fun_min_idx) > 1: + fun_min_idx &= maxcv_filter <= np.min( + maxcv_filter[fun_min_idx] + ) + i = np.flatnonzero(fun_min_idx)[-1] + elif np.any(feasible_idx): + # At least one point is feasible but no feasible point has a + # well-defined objective function value. We select the most + # recent feasible point. + i = np.flatnonzero(feasible_idx)[-1] + else: + # No point is feasible. We first compute the merit function + # value for each point. + merit_filter = np.full_like(fun_filter, np.nan) + merit_filter[finite_idx] = ( + fun_filter[finite_idx] + penalty * maxcv_filter[finite_idx] + ) + if np.all(np.isnan(merit_filter)): + # No point has a well-defined merit function value. In + # other words, among the points with a well-defined maximum + # constraint violation, none has a well-defined objective + # function value. We select the point with the least + # maximum constraint violation. If there is a tie, we + # select the most recent point. + min_maxcv_idx = maxcv_filter <= np.nanmin(maxcv_filter) + i = np.flatnonzero(min_maxcv_idx)[-1] + else: + # At least one point has a well-defined merit function + # value. We select the point with the least merit function + # value. If there is a tie, we select the point with the + # least maximum constraint violation. If there is still a + # tie, we select the point with the least objective + # function value. If there is still a tie, we select the + # most recent point. + merit_min_idx = merit_filter <= np.nanmin(merit_filter) + if np.count_nonzero(merit_min_idx) > 1: + merit_min_idx &= maxcv_filter <= np.min( + maxcv_filter[merit_min_idx] + ) + + if np.count_nonzero(merit_min_idx) > 1: + merit_min_idx &= fun_filter <= np.min( + fun_filter[merit_min_idx] + ) + i = np.flatnonzero(merit_min_idx)[-1] + elif not np.all(np.isnan(fun_filter)): + # No maximum constraint violation is well-defined but at least one + # point has a well-defined objective function value. We select the + # point with the least objective function value. If there is a tie, + # we select the most recent point. + fun_min_idx = fun_filter <= np.nanmin(fun_filter) + i = np.flatnonzero(fun_min_idx)[-1] + else: + # No point has a well-defined maximum constraint violation or + # objective function value. We select the most recent point. + i = len(fun_filter) - 1 + return ( + self.bounds.project(x_filter[i, :]), + fun_filter[i], + maxcv_filter[i], + ) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/settings.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/settings.py new file mode 100644 index 0000000000000000000000000000000000000000..6394822826e094a803a485556a298e342bf260ac --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/settings.py @@ -0,0 +1,132 @@ +import sys +from enum import Enum + +import numpy as np + + +# Exit status. +class ExitStatus(Enum): + """ + Exit statuses. + """ + + RADIUS_SUCCESS = 0 + TARGET_SUCCESS = 1 + FIXED_SUCCESS = 2 + CALLBACK_SUCCESS = 3 + FEASIBLE_SUCCESS = 4 + MAX_EVAL_WARNING = 5 + MAX_ITER_WARNING = 6 + INFEASIBLE_ERROR = -1 + LINALG_ERROR = -2 + + +class Options(str, Enum): + """ + Options. + """ + + DEBUG = "debug" + FEASIBILITY_TOL = "feasibility_tol" + FILTER_SIZE = "filter_size" + HISTORY_SIZE = "history_size" + MAX_EVAL = "maxfev" + MAX_ITER = "maxiter" + NPT = "nb_points" + RHOBEG = "radius_init" + RHOEND = "radius_final" + SCALE = "scale" + STORE_HISTORY = "store_history" + TARGET = "target" + VERBOSE = "disp" + + +class Constants(str, Enum): + """ + Constants. + """ + + DECREASE_RADIUS_FACTOR = "decrease_radius_factor" + INCREASE_RADIUS_FACTOR = "increase_radius_factor" + INCREASE_RADIUS_THRESHOLD = "increase_radius_threshold" + DECREASE_RADIUS_THRESHOLD = "decrease_radius_threshold" + DECREASE_RESOLUTION_FACTOR = "decrease_resolution_factor" + LARGE_RESOLUTION_THRESHOLD = "large_resolution_threshold" + MODERATE_RESOLUTION_THRESHOLD = "moderate_resolution_threshold" + LOW_RATIO = "low_ratio" + HIGH_RATIO = "high_ratio" + VERY_LOW_RATIO = "very_low_ratio" + PENALTY_INCREASE_THRESHOLD = "penalty_increase_threshold" + PENALTY_INCREASE_FACTOR = "penalty_increase_factor" + SHORT_STEP_THRESHOLD = "short_step_threshold" + LOW_RADIUS_FACTOR = "low_radius_factor" + BYRD_OMOJOKUN_FACTOR = "byrd_omojokun_factor" + THRESHOLD_RATIO_CONSTRAINTS = "threshold_ratio_constraints" + LARGE_SHIFT_FACTOR = "large_shift_factor" + LARGE_GRADIENT_FACTOR = "large_gradient_factor" + RESOLUTION_FACTOR = "resolution_factor" + IMPROVE_TCG = "improve_tcg" + + +# Default options. +DEFAULT_OPTIONS = { + Options.DEBUG.value: False, + Options.FEASIBILITY_TOL.value: np.sqrt(np.finfo(float).eps), + Options.FILTER_SIZE.value: sys.maxsize, + Options.HISTORY_SIZE.value: sys.maxsize, + Options.MAX_EVAL.value: lambda n: 500 * n, + Options.MAX_ITER.value: lambda n: 1000 * n, + Options.NPT.value: lambda n: 2 * n + 1, + Options.RHOBEG.value: 1.0, + Options.RHOEND.value: 1e-6, + Options.SCALE.value: False, + Options.STORE_HISTORY.value: False, + Options.TARGET.value: -np.inf, + Options.VERBOSE.value: False, +} + +# Default constants. +DEFAULT_CONSTANTS = { + Constants.DECREASE_RADIUS_FACTOR.value: 0.5, + Constants.INCREASE_RADIUS_FACTOR.value: np.sqrt(2.0), + Constants.INCREASE_RADIUS_THRESHOLD.value: 2.0, + Constants.DECREASE_RADIUS_THRESHOLD.value: 1.4, + Constants.DECREASE_RESOLUTION_FACTOR.value: 0.1, + Constants.LARGE_RESOLUTION_THRESHOLD.value: 250.0, + Constants.MODERATE_RESOLUTION_THRESHOLD.value: 16.0, + Constants.LOW_RATIO.value: 0.1, + Constants.HIGH_RATIO.value: 0.7, + Constants.VERY_LOW_RATIO.value: 0.01, + Constants.PENALTY_INCREASE_THRESHOLD.value: 1.5, + Constants.PENALTY_INCREASE_FACTOR.value: 2.0, + Constants.SHORT_STEP_THRESHOLD.value: 0.5, + Constants.LOW_RADIUS_FACTOR.value: 0.1, + Constants.BYRD_OMOJOKUN_FACTOR.value: 0.8, + Constants.THRESHOLD_RATIO_CONSTRAINTS.value: 2.0, + Constants.LARGE_SHIFT_FACTOR.value: 10.0, + Constants.LARGE_GRADIENT_FACTOR.value: 10.0, + Constants.RESOLUTION_FACTOR.value: 2.0, + Constants.IMPROVE_TCG.value: True, +} + +# Printing options. +PRINT_OPTIONS = { + "threshold": 6, + "edgeitems": 2, + "linewidth": sys.maxsize, + "formatter": { + "float_kind": lambda x: np.format_float_scientific( + x, + precision=3, + unique=False, + pad_left=2, + ) + }, +} + +# Constants. +BARRIER = 2.0 ** min( + 100, + np.finfo(float).maxexp // 2, + -np.finfo(float).minexp // 2, +) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/subsolvers/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/subsolvers/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..01a1ad3c6f4cb5c0c9b99d1ce35fea92e7618ff5 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/subsolvers/__init__.py @@ -0,0 +1,14 @@ +from .geometry import cauchy_geometry, spider_geometry +from .optim import ( + tangential_byrd_omojokun, + constrained_tangential_byrd_omojokun, + normal_byrd_omojokun, +) + +__all__ = [ + "cauchy_geometry", + "spider_geometry", + "tangential_byrd_omojokun", + "constrained_tangential_byrd_omojokun", + "normal_byrd_omojokun", +] diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/subsolvers/geometry.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/subsolvers/geometry.py new file mode 100644 index 0000000000000000000000000000000000000000..7b67fd7c813ee493b18720d1daf71324d72330b6 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/subsolvers/geometry.py @@ -0,0 +1,387 @@ +import inspect + +import numpy as np + +from ..utils import get_arrays_tol + + +TINY = np.finfo(float).tiny + + +def cauchy_geometry(const, grad, curv, xl, xu, delta, debug): + r""" + Maximize approximately the absolute value of a quadratic function subject + to bound constraints in a trust region. + + This function solves approximately + + .. math:: + + \max_{s \in \mathbb{R}^n} \quad \bigg\lvert c + g^{\mathsf{T}} s + + \frac{1}{2} s^{\mathsf{T}} H s \bigg\rvert \quad \text{s.t.} \quad + \left\{ \begin{array}{l} + l \le s \le u,\\ + \lVert s \rVert \le \Delta, + \end{array} \right. + + by maximizing the objective function along the constrained Cauchy + direction. + + Parameters + ---------- + const : float + Constant :math:`c` as shown above. + grad : `numpy.ndarray`, shape (n,) + Gradient :math:`g` as shown above. + curv : callable + Curvature of :math:`H` along any vector. + + ``curv(s) -> float`` + + returns :math:`s^{\mathsf{T}} H s`. + xl : `numpy.ndarray`, shape (n,) + Lower bounds :math:`l` as shown above. + xu : `numpy.ndarray`, shape (n,) + Upper bounds :math:`u` as shown above. + delta : float + Trust-region radius :math:`\Delta` as shown above. + debug : bool + Whether to make debugging tests during the execution. + + Returns + ------- + `numpy.ndarray`, shape (n,) + Approximate solution :math:`s`. + + Notes + ----- + This function is described as the first alternative in Section 6.5 of [1]_. + It is assumed that the origin is feasible with respect to the bound + constraints and that `delta` is finite and positive. + + References + ---------- + .. [1] T. M. Ragonneau. *Model-Based Derivative-Free Optimization Methods + and Software*. PhD thesis, Department of Applied Mathematics, The Hong + Kong Polytechnic University, Hong Kong, China, 2022. URL: + https://theses.lib.polyu.edu.hk/handle/200/12294. + """ + if debug: + assert isinstance(const, float) + assert isinstance(grad, np.ndarray) and grad.ndim == 1 + assert inspect.signature(curv).bind(grad) + assert isinstance(xl, np.ndarray) and xl.shape == grad.shape + assert isinstance(xu, np.ndarray) and xu.shape == grad.shape + assert isinstance(delta, float) + assert isinstance(debug, bool) + tol = get_arrays_tol(xl, xu) + assert np.all(xl <= tol) + assert np.all(xu >= -tol) + assert np.isfinite(delta) and delta > 0.0 + xl = np.minimum(xl, 0.0) + xu = np.maximum(xu, 0.0) + + # To maximize the absolute value of a quadratic function, we maximize the + # function itself or its negative, and we choose the solution that provides + # the largest function value. + step1, q_val1 = _cauchy_geom(const, grad, curv, xl, xu, delta, debug) + step2, q_val2 = _cauchy_geom( + -const, + -grad, + lambda x: -curv(x), + xl, + xu, + delta, + debug, + ) + step = step1 if abs(q_val1) >= abs(q_val2) else step2 + + if debug: + assert np.all(xl <= step) + assert np.all(step <= xu) + assert np.linalg.norm(step) < 1.1 * delta + return step + + +def spider_geometry(const, grad, curv, xpt, xl, xu, delta, debug): + r""" + Maximize approximately the absolute value of a quadratic function subject + to bound constraints in a trust region. + + This function solves approximately + + .. math:: + + \max_{s \in \mathbb{R}^n} \quad \bigg\lvert c + g^{\mathsf{T}} s + + \frac{1}{2} s^{\mathsf{T}} H s \bigg\rvert \quad \text{s.t.} \quad + \left\{ \begin{array}{l} + l \le s \le u,\\ + \lVert s \rVert \le \Delta, + \end{array} \right. + + by maximizing the objective function along given straight lines. + + Parameters + ---------- + const : float + Constant :math:`c` as shown above. + grad : `numpy.ndarray`, shape (n,) + Gradient :math:`g` as shown above. + curv : callable + Curvature of :math:`H` along any vector. + + ``curv(s) -> float`` + + returns :math:`s^{\mathsf{T}} H s`. + xpt : `numpy.ndarray`, shape (n, npt) + Points defining the straight lines. The straight lines considered are + the ones passing through the origin and the points in `xpt`. + xl : `numpy.ndarray`, shape (n,) + Lower bounds :math:`l` as shown above. + xu : `numpy.ndarray`, shape (n,) + Upper bounds :math:`u` as shown above. + delta : float + Trust-region radius :math:`\Delta` as shown above. + debug : bool + Whether to make debugging tests during the execution. + + Returns + ------- + `numpy.ndarray`, shape (n,) + Approximate solution :math:`s`. + + Notes + ----- + This function is described as the second alternative in Section 6.5 of + [1]_. It is assumed that the origin is feasible with respect to the bound + constraints and that `delta` is finite and positive. + + References + ---------- + .. [1] T. M. Ragonneau. *Model-Based Derivative-Free Optimization Methods + and Software*. PhD thesis, Department of Applied Mathematics, The Hong + Kong Polytechnic University, Hong Kong, China, 2022. URL: + https://theses.lib.polyu.edu.hk/handle/200/12294. + """ + if debug: + assert isinstance(const, float) + assert isinstance(grad, np.ndarray) and grad.ndim == 1 + assert inspect.signature(curv).bind(grad) + assert ( + isinstance(xpt, np.ndarray) + and xpt.ndim == 2 + and xpt.shape[0] == grad.size + ) + assert isinstance(xl, np.ndarray) and xl.shape == grad.shape + assert isinstance(xu, np.ndarray) and xu.shape == grad.shape + assert isinstance(delta, float) + assert isinstance(debug, bool) + tol = get_arrays_tol(xl, xu) + assert np.all(xl <= tol) + assert np.all(xu >= -tol) + assert np.isfinite(delta) and delta > 0.0 + xl = np.minimum(xl, 0.0) + xu = np.maximum(xu, 0.0) + + # Iterate through the straight lines. + step = np.zeros_like(grad) + q_val = const + s_norm = np.linalg.norm(xpt, axis=0) + + # Set alpha_xl to the step size for the lower-bound constraint and + # alpha_xu to the step size for the upper-bound constraint. + + # xl.shape = (N,) + # xpt.shape = (N, M) + # i_xl_pos.shape = (M, N) + i_xl_pos = (xl > -np.inf) & (xpt.T > -TINY * xl) + i_xl_neg = (xl > -np.inf) & (xpt.T < TINY * xl) + i_xu_pos = (xu < np.inf) & (xpt.T > TINY * xu) + i_xu_neg = (xu < np.inf) & (xpt.T < -TINY * xu) + + # (M, N) + alpha_xl_pos = np.atleast_2d( + np.broadcast_to(xl, i_xl_pos.shape)[i_xl_pos] / xpt.T[i_xl_pos] + ) + # (M,) + alpha_xl_pos = np.max(alpha_xl_pos, axis=1, initial=-np.inf) + # make sure it's (M,) + alpha_xl_pos = np.broadcast_to(np.atleast_1d(alpha_xl_pos), xpt.shape[1]) + + alpha_xl_neg = np.atleast_2d( + np.broadcast_to(xl, i_xl_neg.shape)[i_xl_neg] / xpt.T[i_xl_neg] + ) + alpha_xl_neg = np.max(alpha_xl_neg, axis=1, initial=np.inf) + alpha_xl_neg = np.broadcast_to(np.atleast_1d(alpha_xl_neg), xpt.shape[1]) + + alpha_xu_neg = np.atleast_2d( + np.broadcast_to(xu, i_xu_neg.shape)[i_xu_neg] / xpt.T[i_xu_neg] + ) + alpha_xu_neg = np.max(alpha_xu_neg, axis=1, initial=-np.inf) + alpha_xu_neg = np.broadcast_to(np.atleast_1d(alpha_xu_neg), xpt.shape[1]) + + alpha_xu_pos = np.atleast_2d( + np.broadcast_to(xu, i_xu_pos.shape)[i_xu_pos] / xpt.T[i_xu_pos] + ) + alpha_xu_pos = np.max(alpha_xu_pos, axis=1, initial=np.inf) + alpha_xu_pos = np.broadcast_to(np.atleast_1d(alpha_xu_pos), xpt.shape[1]) + + for k in range(xpt.shape[1]): + # Set alpha_tr to the step size for the trust-region constraint. + if s_norm[k] > TINY * delta: + alpha_tr = max(delta / s_norm[k], 0.0) + else: + # The current straight line is basically zero. + continue + + alpha_bd_pos = max(min(alpha_xu_pos[k], alpha_xl_neg[k]), 0.0) + alpha_bd_neg = min(max(alpha_xl_pos[k], alpha_xu_neg[k]), 0.0) + + # Set alpha_quad_pos and alpha_quad_neg to the step size to the extrema + # of the quadratic function along the positive and negative directions. + grad_step = grad @ xpt[:, k] + curv_step = curv(xpt[:, k]) + if ( + grad_step >= 0.0 + and curv_step < -TINY * grad_step + or grad_step <= 0.0 + and curv_step > -TINY * grad_step + ): + alpha_quad_pos = max(-grad_step / curv_step, 0.0) + else: + alpha_quad_pos = np.inf + if ( + grad_step >= 0.0 + and curv_step > TINY * grad_step + or grad_step <= 0.0 + and curv_step < TINY * grad_step + ): + alpha_quad_neg = min(-grad_step / curv_step, 0.0) + else: + alpha_quad_neg = -np.inf + + # Select the step that provides the largest value of the objective + # function if it improves the current best. The best positive step is + # either the one that reaches the constraints or the one that reaches + # the extremum of the objective function along the current direction + # (only possible if the resulting step is feasible). We test both, and + # we perform similar calculations along the negative step. + # N.B.: we select the largest possible step among all the ones that + # maximize the objective function. This is to avoid returning the zero + # step in some extreme cases. + alpha_pos = min(alpha_tr, alpha_bd_pos) + alpha_neg = max(-alpha_tr, alpha_bd_neg) + q_val_pos = ( + const + alpha_pos * grad_step + 0.5 * alpha_pos**2.0 * curv_step + ) + q_val_neg = ( + const + alpha_neg * grad_step + 0.5 * alpha_neg**2.0 * curv_step + ) + if alpha_quad_pos < alpha_pos: + q_val_quad_pos = ( + const + + alpha_quad_pos * grad_step + + 0.5 * alpha_quad_pos**2.0 * curv_step + ) + if abs(q_val_quad_pos) > abs(q_val_pos): + alpha_pos = alpha_quad_pos + q_val_pos = q_val_quad_pos + if alpha_quad_neg > alpha_neg: + q_val_quad_neg = ( + const + + alpha_quad_neg * grad_step + + 0.5 * alpha_quad_neg**2.0 * curv_step + ) + if abs(q_val_quad_neg) > abs(q_val_neg): + alpha_neg = alpha_quad_neg + q_val_neg = q_val_quad_neg + if abs(q_val_pos) >= abs(q_val_neg) and abs(q_val_pos) > abs(q_val): + step = np.clip(alpha_pos * xpt[:, k], xl, xu) + q_val = q_val_pos + elif abs(q_val_neg) > abs(q_val_pos) and abs(q_val_neg) > abs(q_val): + step = np.clip(alpha_neg * xpt[:, k], xl, xu) + q_val = q_val_neg + + if debug: + assert np.all(xl <= step) + assert np.all(step <= xu) + assert np.linalg.norm(step) < 1.1 * delta + return step + + +def _cauchy_geom(const, grad, curv, xl, xu, delta, debug): + """ + Same as `bound_constrained_cauchy_step` without the absolute value. + """ + # Calculate the initial active set. + fixed_xl = (xl < 0.0) & (grad > 0.0) + fixed_xu = (xu > 0.0) & (grad < 0.0) + + # Calculate the Cauchy step. + cauchy_step = np.zeros_like(grad) + cauchy_step[fixed_xl] = xl[fixed_xl] + cauchy_step[fixed_xu] = xu[fixed_xu] + if np.linalg.norm(cauchy_step) > delta: + working = fixed_xl | fixed_xu + while True: + # Calculate the Cauchy step for the directions in the working set. + g_norm = np.linalg.norm(grad[working]) + delta_reduced = np.sqrt( + delta**2.0 - cauchy_step[~working] @ cauchy_step[~working] + ) + if g_norm > TINY * abs(delta_reduced): + mu = max(delta_reduced / g_norm, 0.0) + else: + break + cauchy_step[working] = mu * grad[working] + + # Update the working set. + fixed_xl = working & (cauchy_step < xl) + fixed_xu = working & (cauchy_step > xu) + if not np.any(fixed_xl) and not np.any(fixed_xu): + # Stop the calculations as the Cauchy step is now feasible. + break + cauchy_step[fixed_xl] = xl[fixed_xl] + cauchy_step[fixed_xu] = xu[fixed_xu] + working = working & ~(fixed_xl | fixed_xu) + + # Calculate the step that maximizes the quadratic along the Cauchy step. + grad_step = grad @ cauchy_step + if grad_step >= 0.0: + # Set alpha_tr to the step size for the trust-region constraint. + s_norm = np.linalg.norm(cauchy_step) + if s_norm > TINY * delta: + alpha_tr = max(delta / s_norm, 0.0) + else: + # The Cauchy step is basically zero. + alpha_tr = 0.0 + + # Set alpha_quad to the step size for the maximization problem. + curv_step = curv(cauchy_step) + if curv_step < -TINY * grad_step: + alpha_quad = max(-grad_step / curv_step, 0.0) + else: + alpha_quad = np.inf + + # Set alpha_bd to the step size for the bound constraints. + i_xl = (xl > -np.inf) & (cauchy_step < TINY * xl) + i_xu = (xu < np.inf) & (cauchy_step > TINY * xu) + alpha_xl = np.min(xl[i_xl] / cauchy_step[i_xl], initial=np.inf) + alpha_xu = np.min(xu[i_xu] / cauchy_step[i_xu], initial=np.inf) + alpha_bd = min(alpha_xl, alpha_xu) + + # Calculate the solution and the corresponding function value. + alpha = min(alpha_tr, alpha_quad, alpha_bd) + step = np.clip(alpha * cauchy_step, xl, xu) + q_val = const + alpha * grad_step + 0.5 * alpha**2.0 * curv_step + else: + # This case is never reached in exact arithmetic. It prevents this + # function to return a step that decreases the objective function. + step = np.zeros_like(grad) + q_val = const + + if debug: + assert np.all(xl <= step) + assert np.all(step <= xu) + assert np.linalg.norm(step) < 1.1 * delta + return step, q_val diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/subsolvers/optim.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/subsolvers/optim.py new file mode 100644 index 0000000000000000000000000000000000000000..c4a960396fb2e992cf76bac0baf171b5af9b7717 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/subsolvers/optim.py @@ -0,0 +1,1203 @@ +import inspect + +import numpy as np +from scipy.linalg import qr + +from ..utils import get_arrays_tol + + +TINY = np.finfo(float).tiny +EPS = np.finfo(float).eps + + +def tangential_byrd_omojokun(grad, hess_prod, xl, xu, delta, debug, **kwargs): + r""" + Minimize approximately a quadratic function subject to bound constraints in + a trust region. + + This function solves approximately + + .. math:: + + \min_{s \in \mathbb{R}^n} \quad g^{\mathsf{T}} s + \frac{1}{2} + s^{\mathsf{T}} H s \quad \text{s.t.} \quad + \left\{ \begin{array}{l} + l \le s \le u\\ + \lVert s \rVert \le \Delta, + \end{array} \right. + + using an active-set variation of the truncated conjugate gradient method. + + Parameters + ---------- + grad : `numpy.ndarray`, shape (n,) + Gradient :math:`g` as shown above. + hess_prod : callable + Product of the Hessian matrix :math:`H` with any vector. + + ``hess_prod(s) -> `numpy.ndarray`, shape (n,)`` + + returns the product :math:`H s`. + xl : `numpy.ndarray`, shape (n,) + Lower bounds :math:`l` as shown above. + xu : `numpy.ndarray`, shape (n,) + Upper bounds :math:`u` as shown above. + delta : float + Trust-region radius :math:`\Delta` as shown above. + debug : bool + Whether to make debugging tests during the execution. + + Returns + ------- + `numpy.ndarray`, shape (n,) + Approximate solution :math:`s`. + + Other Parameters + ---------------- + improve_tcg : bool, optional + If True, a solution generated by the truncated conjugate gradient + method that is on the boundary of the trust region is improved by + moving around the trust-region boundary on the two-dimensional space + spanned by the solution and the gradient of the quadratic function at + the solution (default is True). + + Notes + ----- + This function implements Algorithm 6.2 of [1]_. It is assumed that the + origin is feasible with respect to the bound constraints and that `delta` + is finite and positive. + + References + ---------- + .. [1] T. M. Ragonneau. *Model-Based Derivative-Free Optimization Methods + and Software*. PhD thesis, Department of Applied Mathematics, The Hong + Kong Polytechnic University, Hong Kong, China, 2022. URL: + https://theses.lib.polyu.edu.hk/handle/200/12294. + """ + if debug: + assert isinstance(grad, np.ndarray) and grad.ndim == 1 + assert inspect.signature(hess_prod).bind(grad) + assert isinstance(xl, np.ndarray) and xl.shape == grad.shape + assert isinstance(xu, np.ndarray) and xu.shape == grad.shape + assert isinstance(delta, float) + assert isinstance(debug, bool) + tol = get_arrays_tol(xl, xu) + assert np.all(xl <= tol) + assert np.all(xu >= -tol) + assert np.isfinite(delta) and delta > 0.0 + xl = np.minimum(xl, 0.0) + xu = np.maximum(xu, 0.0) + + # Copy the arrays that may be modified by the code below. + n = grad.size + grad = np.copy(grad) + grad_orig = np.copy(grad) + + # Calculate the initial active set. + free_bd = ((xl < 0.0) | (grad < 0.0)) & ((xu > 0.0) | (grad > 0.0)) + + # Set the initial iterate and the initial search direction. + step = np.zeros_like(grad) + sd = np.zeros_like(step) + sd[free_bd] = -grad[free_bd] + + k = 0 + reduct = 0.0 + boundary_reached = False + while k < np.count_nonzero(free_bd): + # Stop the computations if sd is not a descent direction. + grad_sd = grad @ sd + if grad_sd >= -10.0 * EPS * n * max(1.0, np.linalg.norm(grad)): + break + + # Set alpha_tr to the step size for the trust-region constraint. + try: + alpha_tr = _alpha_tr(step, sd, delta) + except ZeroDivisionError: + break + + # Stop the computations if a step along sd is expected to give a + # relatively small reduction in the objective function. + if -alpha_tr * grad_sd <= 1e-8 * reduct: + break + + # Set alpha_quad to the step size for the minimization problem. + hess_sd = hess_prod(sd) + curv_sd = sd @ hess_sd + if curv_sd > TINY * abs(grad_sd): + alpha_quad = max(-grad_sd / curv_sd, 0.0) + else: + alpha_quad = np.inf + + # Stop the computations if the reduction in the objective function + # provided by an unconstrained step is small. + alpha = min(alpha_tr, alpha_quad) + if -alpha * (grad_sd + 0.5 * alpha * curv_sd) <= 1e-8 * reduct: + break + + # Set alpha_bd to the step size for the bound constraints. + i_xl = (xl > -np.inf) & (sd < -TINY * np.abs(xl - step)) + i_xu = (xu < np.inf) & (sd > TINY * np.abs(xu - step)) + all_alpha_xl = np.full_like(step, np.inf) + all_alpha_xu = np.full_like(step, np.inf) + all_alpha_xl[i_xl] = np.maximum( + (xl[i_xl] - step[i_xl]) / sd[i_xl], + 0.0, + ) + all_alpha_xu[i_xu] = np.maximum( + (xu[i_xu] - step[i_xu]) / sd[i_xu], + 0.0, + ) + alpha_xl = np.min(all_alpha_xl) + alpha_xu = np.min(all_alpha_xu) + alpha_bd = min(alpha_xl, alpha_xu) + + # Update the iterate. + alpha = min(alpha, alpha_bd) + if alpha > 0.0: + step[free_bd] = np.clip( + step[free_bd] + alpha * sd[free_bd], + xl[free_bd], + xu[free_bd], + ) + grad += alpha * hess_sd + reduct -= alpha * (grad_sd + 0.5 * alpha * curv_sd) + + if alpha < min(alpha_tr, alpha_bd): + # The current iteration is a conjugate gradient iteration. Update + # the search direction so that it is conjugate (with respect to H) + # to all the previous search directions. + beta = (grad[free_bd] @ hess_sd[free_bd]) / curv_sd + sd[free_bd] = beta * sd[free_bd] - grad[free_bd] + sd[~free_bd] = 0.0 + k += 1 + elif alpha < alpha_tr: + # The iterate is restricted by a bound constraint. Add this bound + # constraint to the active set, and restart the calculations. + if alpha_xl <= alpha: + i_new = np.argmin(all_alpha_xl) + step[i_new] = xl[i_new] + else: + i_new = np.argmin(all_alpha_xu) + step[i_new] = xu[i_new] + free_bd[i_new] = False + sd[free_bd] = -grad[free_bd] + sd[~free_bd] = 0.0 + k = 0 + else: + # The current iterate is on the trust-region boundary. Add all the + # active bounds to the working set to prepare for the improvement + # of the solution, and stop the iterations. + if alpha_xl <= alpha: + i_new = _argmin(all_alpha_xl) + step[i_new] = xl[i_new] + free_bd[i_new] = False + if alpha_xu <= alpha: + i_new = _argmin(all_alpha_xu) + step[i_new] = xu[i_new] + free_bd[i_new] = False + boundary_reached = True + break + + # Attempt to improve the solution on the trust-region boundary. + if kwargs.get("improve_tcg", True) and boundary_reached: + step_base = np.copy(step) + step_comparator = grad_orig @ step_base + 0.5 * step_base @ hess_prod( + step_base + ) + + while np.count_nonzero(free_bd) > 0: + # Check whether a substantial reduction in the objective function + # is possible, and set the search direction. + step_sq = step[free_bd] @ step[free_bd] + grad_sq = grad[free_bd] @ grad[free_bd] + grad_step = grad[free_bd] @ step[free_bd] + grad_sd = -np.sqrt(max(step_sq * grad_sq - grad_step**2.0, 0.0)) + sd[free_bd] = grad_step * step[free_bd] - step_sq * grad[free_bd] + sd[~free_bd] = 0.0 + if grad_sd >= -1e-8 * reduct or np.any( + grad_sd >= -TINY * np.abs(sd[free_bd]) + ): + break + sd[free_bd] /= -grad_sd + + # Calculate an upper bound for the tangent of half the angle theta + # of this alternative iteration. The step will be updated as: + # step = cos(theta) * step + sin(theta) * sd. + temp_xl = np.zeros(n) + temp_xu = np.zeros(n) + temp_xl[free_bd] = ( + step[free_bd] ** 2.0 + sd[free_bd] ** 2.0 - xl[free_bd] ** 2.0 + ) + temp_xu[free_bd] = ( + step[free_bd] ** 2.0 + sd[free_bd] ** 2.0 - xu[free_bd] ** 2.0 + ) + temp_xl[temp_xl > 0.0] = ( + np.sqrt(temp_xl[temp_xl > 0.0]) - sd[temp_xl > 0.0] + ) + temp_xu[temp_xu > 0.0] = ( + np.sqrt(temp_xu[temp_xu > 0.0]) + sd[temp_xu > 0.0] + ) + dist_xl = np.maximum(step - xl, 0.0) + dist_xu = np.maximum(xu - step, 0.0) + i_xl = temp_xl > TINY * dist_xl + i_xu = temp_xu > TINY * dist_xu + all_t_xl = np.ones(n) + all_t_xu = np.ones(n) + all_t_xl[i_xl] = np.minimum( + all_t_xl[i_xl], + dist_xl[i_xl] / temp_xl[i_xl], + ) + all_t_xu[i_xu] = np.minimum( + all_t_xu[i_xu], + dist_xu[i_xu] / temp_xu[i_xu], + ) + t_xl = np.min(all_t_xl) + t_xu = np.min(all_t_xu) + t_bd = min(t_xl, t_xu) + + # Calculate some curvature information. + hess_step = hess_prod(step) + hess_sd = hess_prod(sd) + curv_step = step @ hess_step + curv_sd = sd @ hess_sd + curv_step_sd = step @ hess_sd + + # For a range of equally spaced values of tan(0.5 * theta), + # calculate the reduction in the objective function that would be + # obtained by accepting the corresponding angle. + n_samples = 20 + n_samples = int((n_samples - 3) * t_bd + 3) + t_samples = np.linspace(t_bd / n_samples, t_bd, n_samples) + sin_values = 2.0 * t_samples / (1.0 + t_samples**2.0) + all_reduct = sin_values * ( + grad_step * t_samples + - grad_sd + - t_samples * curv_step + + sin_values + * (t_samples * curv_step_sd - 0.5 * (curv_sd - curv_step)) + ) + if np.all(all_reduct <= 0.0): + # No reduction in the objective function is obtained. + break + + # Accept the angle that provides the largest reduction in the + # objective function, and update the iterate. + i_max = np.argmax(all_reduct) + cos_value = (1.0 - t_samples[i_max] ** 2.0) / ( + 1.0 + t_samples[i_max] ** 2.0 + ) + step[free_bd] = ( + cos_value * step[free_bd] + sin_values[i_max] * sd[free_bd] + ) + grad += (cos_value - 1.0) * hess_step + sin_values[i_max] * hess_sd + reduct += all_reduct[i_max] + + # If the above angle is restricted by bound constraints, add them + # to the working set, and restart the alternative iteration. + # Otherwise, the calculations are terminated. + if t_bd < 1.0 and i_max == n_samples - 1: + if t_xl <= t_bd: + i_new = _argmin(all_t_xl) + step[i_new] = xl[i_new] + free_bd[i_new] = False + if t_xu <= t_bd: + i_new = _argmin(all_t_xu) + step[i_new] = xu[i_new] + free_bd[i_new] = False + else: + break + + # Ensure that the alternative iteration improves the objective + # function. + if grad_orig @ step + 0.5 * step @ hess_prod(step) > step_comparator: + step = step_base + + if debug: + assert np.all(xl <= step) + assert np.all(step <= xu) + assert np.linalg.norm(step) < 1.1 * delta + return step + + +def constrained_tangential_byrd_omojokun( + grad, + hess_prod, + xl, + xu, + aub, + bub, + aeq, + delta, + debug, + **kwargs, +): + r""" + Minimize approximately a quadratic function subject to bound and linear + constraints in a trust region. + + This function solves approximately + + .. math:: + + \min_{s \in \mathbb{R}^n} \quad g^{\mathsf{T}} s + \frac{1}{2} + s^{\mathsf{T}} H s \quad \text{s.t.} \quad + \left\{ \begin{array}{l} + l \le s \le u,\\ + A_{\scriptscriptstyle I} s \le b_{\scriptscriptstyle I},\\ + A_{\scriptscriptstyle E} s = 0,\\ + \lVert s \rVert \le \Delta, + \end{array} \right. + + using an active-set variation of the truncated conjugate gradient method. + + Parameters + ---------- + grad : `numpy.ndarray`, shape (n,) + Gradient :math:`g` as shown above. + hess_prod : callable + Product of the Hessian matrix :math:`H` with any vector. + + ``hess_prod(s) -> `numpy.ndarray`, shape (n,)`` + + returns the product :math:`H s`. + xl : `numpy.ndarray`, shape (n,) + Lower bounds :math:`l` as shown above. + xu : `numpy.ndarray`, shape (n,) + Upper bounds :math:`u` as shown above. + aub : `numpy.ndarray`, shape (m_linear_ub, n) + Coefficient matrix :math:`A_{\scriptscriptstyle I}` as shown above. + bub : `numpy.ndarray`, shape (m_linear_ub,) + Right-hand side :math:`b_{\scriptscriptstyle I}` as shown above. + aeq : `numpy.ndarray`, shape (m_linear_eq, n) + Coefficient matrix :math:`A_{\scriptscriptstyle E}` as shown above. + delta : float + Trust-region radius :math:`\Delta` as shown above. + debug : bool + Whether to make debugging tests during the execution. + + Returns + ------- + `numpy.ndarray`, shape (n,) + Approximate solution :math:`s`. + + Other Parameters + ---------------- + improve_tcg : bool, optional + If True, a solution generated by the truncated conjugate gradient + method that is on the boundary of the trust region is improved by + moving around the trust-region boundary on the two-dimensional space + spanned by the solution and the gradient of the quadratic function at + the solution (default is True). + + Notes + ----- + This function implements Algorithm 6.3 of [1]_. It is assumed that the + origin is feasible with respect to the bound and linear constraints, and + that `delta` is finite and positive. + + References + ---------- + .. [1] T. M. Ragonneau. *Model-Based Derivative-Free Optimization Methods + and Software*. PhD thesis, Department of Applied Mathematics, The Hong + Kong Polytechnic University, Hong Kong, China, 2022. URL: + https://theses.lib.polyu.edu.hk/handle/200/12294. + """ + if debug: + assert isinstance(grad, np.ndarray) and grad.ndim == 1 + assert inspect.signature(hess_prod).bind(grad) + assert isinstance(xl, np.ndarray) and xl.shape == grad.shape + assert isinstance(xu, np.ndarray) and xu.shape == grad.shape + assert ( + isinstance(aub, np.ndarray) + and aub.ndim == 2 + and aub.shape[1] == grad.size + ) + assert ( + isinstance(bub, np.ndarray) + and bub.ndim == 1 + and bub.size == aub.shape[0] + ) + assert ( + isinstance(aeq, np.ndarray) + and aeq.ndim == 2 + and aeq.shape[1] == grad.size + ) + assert isinstance(delta, float) + assert isinstance(debug, bool) + tol = get_arrays_tol(xl, xu) + assert np.all(xl <= tol) + assert np.all(xu >= -tol) + assert np.all(bub >= -tol) + assert np.isfinite(delta) and delta > 0.0 + xl = np.minimum(xl, 0.0) + xu = np.maximum(xu, 0.0) + bub = np.maximum(bub, 0.0) + + # Copy the arrays that may be modified by the code below. + n = grad.size + grad = np.copy(grad) + grad_orig = np.copy(grad) + + # Calculate the initial active set. + free_xl = (xl < 0.0) | (grad < 0.0) + free_xu = (xu > 0.0) | (grad > 0.0) + free_ub = (bub > 0.0) | (aub @ grad > 0.0) + n_act, q = qr_tangential_byrd_omojokun(aub, aeq, free_xl, free_xu, free_ub) + + # Set the initial iterate and the initial search direction. + step = np.zeros_like(grad) + sd = -q[:, n_act:] @ (q[:, n_act:].T @ grad) + resid = np.copy(bub) + + k = 0 + reduct = 0.0 + boundary_reached = False + while k < n - n_act: + # Stop the computations if sd is not a descent direction. + grad_sd = grad @ sd + if grad_sd >= -10.0 * EPS * n * max(1.0, np.linalg.norm(grad)): + break + + # Set alpha_tr to the step size for the trust-region constraint. + try: + alpha_tr = _alpha_tr(step, sd, delta) + except ZeroDivisionError: + break + + # Stop the computations if a step along sd is expected to give a + # relatively small reduction in the objective function. + if -alpha_tr * grad_sd <= 1e-8 * reduct: + break + + # Set alpha_quad to the step size for the minimization problem. + hess_sd = hess_prod(sd) + curv_sd = sd @ hess_sd + if curv_sd > TINY * abs(grad_sd): + alpha_quad = max(-grad_sd / curv_sd, 0.0) + else: + alpha_quad = np.inf + + # Stop the computations if the reduction in the objective function + # provided by an unconstrained step is small. + alpha = min(alpha_tr, alpha_quad) + if -alpha * (grad_sd + 0.5 * alpha * curv_sd) <= 1e-8 * reduct: + break + + # Set alpha_bd to the step size for the bound constraints. + i_xl = free_xl & (xl > -np.inf) & (sd < -TINY * np.abs(xl - step)) + i_xu = free_xu & (xu < np.inf) & (sd > TINY * np.abs(xu - step)) + all_alpha_xl = np.full_like(step, np.inf) + all_alpha_xu = np.full_like(step, np.inf) + all_alpha_xl[i_xl] = np.maximum( + (xl[i_xl] - step[i_xl]) / sd[i_xl], + 0.0, + ) + all_alpha_xu[i_xu] = np.maximum( + (xu[i_xu] - step[i_xu]) / sd[i_xu], + 0.0, + ) + alpha_xl = np.min(all_alpha_xl) + alpha_xu = np.min(all_alpha_xu) + alpha_bd = min(alpha_xl, alpha_xu) + + # Set alpha_ub to the step size for the linear constraints. + aub_sd = aub @ sd + i_ub = free_ub & (aub_sd > TINY * np.abs(resid)) + all_alpha_ub = np.full_like(bub, np.inf) + all_alpha_ub[i_ub] = resid[i_ub] / aub_sd[i_ub] + alpha_ub = np.min(all_alpha_ub, initial=np.inf) + + # Update the iterate. + alpha = min(alpha, alpha_bd, alpha_ub) + if alpha > 0.0: + step = np.clip(step + alpha * sd, xl, xu) + grad += alpha * hess_sd + resid = np.maximum(0.0, resid - alpha * aub_sd) + reduct -= alpha * (grad_sd + 0.5 * alpha * curv_sd) + + if alpha < min(alpha_tr, alpha_bd, alpha_ub): + # The current iteration is a conjugate gradient iteration. Update + # the search direction so that it is conjugate (with respect to H) + # to all the previous search directions. + grad_proj = q[:, n_act:] @ (q[:, n_act:].T @ grad) + beta = (grad_proj @ hess_sd) / curv_sd + sd = beta * sd - grad_proj + k += 1 + elif alpha < alpha_tr: + # The iterate is restricted by a bound/linear constraint. Add this + # constraint to the active set, and restart the calculations. + if alpha_xl <= alpha: + i_new = np.argmin(all_alpha_xl) + step[i_new] = xl[i_new] + free_xl[i_new] = False + elif alpha_xu <= alpha: + i_new = np.argmin(all_alpha_xu) + step[i_new] = xu[i_new] + free_xu[i_new] = False + else: + i_new = np.argmin(all_alpha_ub) + free_ub[i_new] = False + n_act, q = qr_tangential_byrd_omojokun( + aub, + aeq, + free_xl, + free_xu, + free_ub, + ) + sd = -q[:, n_act:] @ (q[:, n_act:].T @ grad) + k = 0 + else: + # The current iterate is on the trust-region boundary. Add all the + # active bound/linear constraints to the working set to prepare for + # the improvement of the solution, and stop the iterations. + if alpha_xl <= alpha: + i_new = _argmin(all_alpha_xl) + step[i_new] = xl[i_new] + free_xl[i_new] = False + if alpha_xu <= alpha: + i_new = _argmin(all_alpha_xu) + step[i_new] = xu[i_new] + free_xu[i_new] = False + if alpha_ub <= alpha: + i_new = _argmin(all_alpha_ub) + free_ub[i_new] = False + n_act, q = qr_tangential_byrd_omojokun( + aub, + aeq, + free_xl, + free_xu, + free_ub, + ) + boundary_reached = True + break + + # Attempt to improve the solution on the trust-region boundary. + if kwargs.get("improve_tcg", True) and boundary_reached and n_act < n: + step_base = np.copy(step) + while n_act < n: + # Check whether a substantial reduction in the objective function + # is possible, and set the search direction. + step_proj = q[:, n_act:] @ (q[:, n_act:].T @ step) + grad_proj = q[:, n_act:] @ (q[:, n_act:].T @ grad) + step_sq = step_proj @ step_proj + grad_sq = grad_proj @ grad_proj + grad_step = grad_proj @ step_proj + grad_sd = -np.sqrt(max(step_sq * grad_sq - grad_step**2.0, 0.0)) + sd = q[:, n_act:] @ ( + q[:, n_act:].T @ (grad_step * step - step_sq * grad) + ) + if grad_sd >= -1e-8 * reduct or np.any( + grad_sd >= -TINY * np.abs(sd) + ): + break + sd /= -grad_sd + + # Calculate an upper bound for the tangent of half the angle theta + # of this alternative iteration for the bound constraints. The step + # will be updated as: + # step += (cos(theta) - 1) * step_proj + sin(theta) * sd. + temp_xl = np.zeros(n) + temp_xu = np.zeros(n) + dist_xl = np.maximum(step - xl, 0.0) + dist_xu = np.maximum(xu - step, 0.0) + temp_xl[free_xl] = sd[free_xl] ** 2.0 - dist_xl[free_xl] * ( + dist_xl[free_xl] - 2.0 * step_proj[free_xl] + ) + temp_xu[free_xu] = sd[free_xu] ** 2.0 - dist_xu[free_xu] * ( + dist_xu[free_xu] + 2.0 * step_proj[free_xu] + ) + temp_xl[temp_xl > 0.0] = ( + np.sqrt(temp_xl[temp_xl > 0.0]) - sd[temp_xl > 0.0] + ) + temp_xu[temp_xu > 0.0] = ( + np.sqrt(temp_xu[temp_xu > 0.0]) + sd[temp_xu > 0.0] + ) + i_xl = temp_xl > TINY * dist_xl + i_xu = temp_xu > TINY * dist_xu + all_t_xl = np.ones(n) + all_t_xu = np.ones(n) + all_t_xl[i_xl] = np.minimum( + all_t_xl[i_xl], + dist_xl[i_xl] / temp_xl[i_xl], + ) + all_t_xu[i_xu] = np.minimum( + all_t_xu[i_xu], + dist_xu[i_xu] / temp_xu[i_xu], + ) + t_xl = np.min(all_t_xl) + t_xu = np.min(all_t_xu) + t_bd = min(t_xl, t_xu) + + # Calculate an upper bound for the tangent of half the angle theta + # of this alternative iteration for the linear constraints. + temp_ub = np.zeros_like(resid) + aub_step = aub @ step_proj + aub_sd = aub @ sd + temp_ub[free_ub] = aub_sd[free_ub] ** 2.0 - resid[free_ub] * ( + resid[free_ub] + 2.0 * aub_step[free_ub] + ) + temp_ub[temp_ub > 0.0] = ( + np.sqrt(temp_ub[temp_ub > 0.0]) + aub_sd[temp_ub > 0.0] + ) + i_ub = temp_ub > TINY * resid + all_t_ub = np.ones_like(resid) + all_t_ub[i_ub] = np.minimum( + all_t_ub[i_ub], + resid[i_ub] / temp_ub[i_ub], + ) + t_ub = np.min(all_t_ub, initial=1.0) + t_min = min(t_bd, t_ub) + + # Calculate some curvature information. + hess_step = hess_prod(step_proj) + hess_sd = hess_prod(sd) + curv_step = step_proj @ hess_step + curv_sd = sd @ hess_sd + curv_step_sd = step_proj @ hess_sd + + # For a range of equally spaced values of tan(0.5 * theta), + # calculate the reduction in the objective function that would be + # obtained by accepting the corresponding angle. + n_samples = 20 + n_samples = int((n_samples - 3) * t_min + 3) + t_samples = np.linspace(t_min / n_samples, t_min, n_samples) + sin_values = 2.0 * t_samples / (1.0 + t_samples**2.0) + all_reduct = sin_values * ( + grad_step * t_samples + - grad_sd + - sin_values + * ( + 0.5 * t_samples**2.0 * curv_step + - 2.0 * t_samples * curv_step_sd + + 0.5 * curv_sd + ) + ) + if np.all(all_reduct <= 0.0): + # No reduction in the objective function is obtained. + break + + # Accept the angle that provides the largest reduction in the + # objective function, and update the iterate. + i_max = np.argmax(all_reduct) + cos_value = (1.0 - t_samples[i_max] ** 2.0) / ( + 1.0 + t_samples[i_max] ** 2.0 + ) + step = np.clip( + step + (cos_value - 1.0) * step_proj + sin_values[i_max] * sd, + xl, + xu, + ) + grad += (cos_value - 1.0) * hess_step + sin_values[i_max] * hess_sd + resid = np.maximum( + 0.0, + resid + - (cos_value - 1.0) * aub_step + - sin_values[i_max] * aub_sd, + ) + reduct += all_reduct[i_max] + + # If the above angle is restricted by bound constraints, add them + # to the working set, and restart the alternative iteration. + # Otherwise, the calculations are terminated. + if t_min < 1.0 and i_max == n_samples - 1: + if t_xl <= t_min: + i_new = _argmin(all_t_xl) + step[i_new] = xl[i_new] + free_xl[i_new] = False + if t_xu <= t_min: + i_new = _argmin(all_t_xu) + step[i_new] = xu[i_new] + free_xl[i_new] = False + if t_ub <= t_min: + i_new = _argmin(all_t_ub) + free_ub[i_new] = False + n_act, q = qr_tangential_byrd_omojokun( + aub, + aeq, + free_xl, + free_xu, + free_ub, + ) + else: + break + + # Ensure that the alternative iteration improves the objective + # function. + if grad_orig @ step + 0.5 * step @ hess_prod( + step + ) > grad_orig @ step_base + 0.5 * step_base @ hess_prod(step_base): + step = step_base + + if debug: + tol = get_arrays_tol(xl, xu) + assert np.all(xl <= step) + assert np.all(step <= xu) + assert np.all(aub @ step <= bub + tol) + assert np.all(np.abs(aeq @ step) <= tol) + assert np.linalg.norm(step) < 1.1 * delta + return step + + +def normal_byrd_omojokun(aub, bub, aeq, beq, xl, xu, delta, debug, **kwargs): + r""" + Minimize approximately a linear constraint violation subject to bound + constraints in a trust region. + + This function solves approximately + + .. math:: + + \min_{s \in \mathbb{R}^n} \quad \frac{1}{2} \big( \lVert \max \{ + A_{\scriptscriptstyle I} s - b_{\scriptscriptstyle I}, 0 \} \rVert^2 + + \lVert A_{\scriptscriptstyle E} s - b_{\scriptscriptstyle E} \rVert^2 + \big) \quad \text{s.t.} + \quad + \left\{ \begin{array}{l} + l \le s \le u,\\ + \lVert s \rVert \le \Delta, + \end{array} \right. + + using a variation of the truncated conjugate gradient method. + + Parameters + ---------- + aub : `numpy.ndarray`, shape (m_linear_ub, n) + Matrix :math:`A_{\scriptscriptstyle I}` as shown above. + bub : `numpy.ndarray`, shape (m_linear_ub,) + Vector :math:`b_{\scriptscriptstyle I}` as shown above. + aeq : `numpy.ndarray`, shape (m_linear_eq, n) + Matrix :math:`A_{\scriptscriptstyle E}` as shown above. + beq : `numpy.ndarray`, shape (m_linear_eq,) + Vector :math:`b_{\scriptscriptstyle E}` as shown above. + xl : `numpy.ndarray`, shape (n,) + Lower bounds :math:`l` as shown above. + xu : `numpy.ndarray`, shape (n,) + Upper bounds :math:`u` as shown above. + delta : float + Trust-region radius :math:`\Delta` as shown above. + debug : bool + Whether to make debugging tests during the execution. + + Returns + ------- + `numpy.ndarray`, shape (n,) + Approximate solution :math:`s`. + + Other Parameters + ---------------- + improve_tcg : bool, optional + If True, a solution generated by the truncated conjugate gradient + method that is on the boundary of the trust region is improved by + moving around the trust-region boundary on the two-dimensional space + spanned by the solution and the gradient of the quadratic function at + the solution (default is True). + + Notes + ----- + This function implements Algorithm 6.4 of [1]_. It is assumed that the + origin is feasible with respect to the bound constraints and that `delta` + is finite and positive. + + References + ---------- + .. [1] T. M. Ragonneau. *Model-Based Derivative-Free Optimization Methods + and Software*. PhD thesis, Department of Applied Mathematics, The Hong + Kong Polytechnic University, Hong Kong, China, 2022. URL: + https://theses.lib.polyu.edu.hk/handle/200/12294. + """ + if debug: + assert isinstance(aub, np.ndarray) and aub.ndim == 2 + assert ( + isinstance(bub, np.ndarray) + and bub.ndim == 1 + and bub.size == aub.shape[0] + ) + assert ( + isinstance(aeq, np.ndarray) + and aeq.ndim == 2 + and aeq.shape[1] == aub.shape[1] + ) + assert ( + isinstance(beq, np.ndarray) + and beq.ndim == 1 + and beq.size == aeq.shape[0] + ) + assert isinstance(xl, np.ndarray) and xl.shape == (aub.shape[1],) + assert isinstance(xu, np.ndarray) and xu.shape == (aub.shape[1],) + assert isinstance(delta, float) + assert isinstance(debug, bool) + tol = get_arrays_tol(xl, xu) + assert np.all(xl <= tol) + assert np.all(xu >= -tol) + assert np.isfinite(delta) and delta > 0.0 + xl = np.minimum(xl, 0.0) + xu = np.maximum(xu, 0.0) + + # Calculate the initial active set. + m_linear_ub, n = aub.shape + grad = np.r_[aeq.T @ -beq, np.maximum(0.0, -bub)] + free_xl = (xl < 0.0) | (grad[:n] < 0.0) + free_xu = (xu > 0.0) | (grad[:n] > 0.0) + free_slack = bub < 0.0 + free_ub = (bub > 0.0) | (aub @ grad[:n] - grad[n:] > 0.0) + n_act, q = qr_normal_byrd_omojokun( + aub, + free_xl, + free_xu, + free_slack, + free_ub, + ) + + # Calculate an upper bound on the norm of the slack variables. It is not + # used in the original algorithm, but it may prevent undesired behaviors + # engendered by computer rounding errors. + delta_slack = np.sqrt(beq @ beq + grad[n:] @ grad[n:]) + + # Set the initial iterate and the initial search direction. + step = np.zeros(n) + sd = -q[:, n_act:] @ (q[:, n_act:].T @ grad) + resid = bub + grad[n:] + + k = 0 + reduct = 0.0 + boundary_reached = False + while k < n + m_linear_ub - n_act: + # Stop the computations if sd is not a descent direction. + grad_sd = grad @ sd + if grad_sd >= -10.0 * EPS * n * max(1.0, np.linalg.norm(grad)): + break + + # Set alpha_tr to the step size for the trust-region constraint. + try: + alpha_tr = _alpha_tr(step, sd[:n], delta) + except ZeroDivisionError: + alpha_tr = np.inf + + # Prevent undesired behaviors engendered by computer rounding errors by + # considering the trust-region constraint on the slack variables. + try: + alpha_tr = min(alpha_tr, _alpha_tr(grad[n:], sd[n:], delta_slack)) + except ZeroDivisionError: + pass + + # Stop the computations if a step along sd is expected to give a + # relatively small reduction in the objective function. + if -alpha_tr * grad_sd <= 1e-8 * reduct: + break + + # Set alpha_quad to the step size for the minimization problem. + hess_sd = np.r_[aeq.T @ (aeq @ sd[:n]), sd[n:]] + curv_sd = sd @ hess_sd + if curv_sd > TINY * abs(grad_sd): + alpha_quad = max(-grad_sd / curv_sd, 0.0) + else: + alpha_quad = np.inf + + # Stop the computations if the reduction in the objective function + # provided by an unconstrained step is small. + alpha = min(alpha_tr, alpha_quad) + if -alpha * (grad_sd + 0.5 * alpha * curv_sd) <= 1e-8 * reduct: + break + + # Set alpha_bd to the step size for the bound constraints. + i_xl = free_xl & (xl > -np.inf) & (sd[:n] < -TINY * np.abs(xl - step)) + i_xu = free_xu & (xu < np.inf) & (sd[:n] > TINY * np.abs(xu - step)) + i_slack = free_slack & (sd[n:] < -TINY * np.abs(grad[n:])) + all_alpha_xl = np.full_like(step, np.inf) + all_alpha_xu = np.full_like(step, np.inf) + all_alpha_slack = np.full_like(bub, np.inf) + all_alpha_xl[i_xl] = np.maximum( + (xl[i_xl] - step[i_xl]) / sd[:n][i_xl], + 0.0, + ) + all_alpha_xu[i_xu] = np.maximum( + (xu[i_xu] - step[i_xu]) / sd[:n][i_xu], + 0.0, + ) + all_alpha_slack[i_slack] = np.maximum( + -grad[n:][i_slack] / sd[n:][i_slack], + 0.0, + ) + alpha_xl = np.min(all_alpha_xl) + alpha_xu = np.min(all_alpha_xu) + alpha_slack = np.min(all_alpha_slack, initial=np.inf) + alpha_bd = min(alpha_xl, alpha_xu, alpha_slack) + + # Set alpha_ub to the step size for the linear constraints. + aub_sd = aub @ sd[:n] - sd[n:] + i_ub = free_ub & (aub_sd > TINY * np.abs(resid)) + all_alpha_ub = np.full_like(bub, np.inf) + all_alpha_ub[i_ub] = resid[i_ub] / aub_sd[i_ub] + alpha_ub = np.min(all_alpha_ub, initial=np.inf) + + # Update the iterate. + alpha = min(alpha, alpha_bd, alpha_ub) + if alpha > 0.0: + step = np.clip(step + alpha * sd[:n], xl, xu) + grad += alpha * hess_sd + resid = np.maximum(0.0, resid - alpha * aub_sd) + reduct -= alpha * (grad_sd + 0.5 * alpha * curv_sd) + + if alpha < min(alpha_tr, alpha_bd, alpha_ub): + # The current iteration is a conjugate gradient iteration. Update + # the search direction so that it is conjugate (with respect to H) + # to all the previous search directions. + grad_proj = q[:, n_act:] @ (q[:, n_act:].T @ grad) + beta = (grad_proj @ hess_sd) / curv_sd + sd = beta * sd - grad_proj + k += 1 + elif alpha < alpha_tr: + # The iterate is restricted by a bound/linear constraint. Add this + # constraint to the active set, and restart the calculations. + if alpha_xl <= alpha: + i_new = np.argmin(all_alpha_xl) + step[i_new] = xl[i_new] + free_xl[i_new] = False + elif alpha_xu <= alpha: + i_new = np.argmin(all_alpha_xu) + step[i_new] = xu[i_new] + free_xu[i_new] = False + elif alpha_slack <= alpha: + i_new = np.argmin(all_alpha_slack) + free_slack[i_new] = False + else: + i_new = np.argmin(all_alpha_ub) + free_ub[i_new] = False + n_act, q = qr_normal_byrd_omojokun( + aub, free_xl, free_xu, free_slack, free_ub + ) + sd = -q[:, n_act:] @ (q[:, n_act:].T @ grad) + k = 0 + else: + # The current iterate is on the trust-region boundary. Add all the + # active bound constraints to the working set to prepare for the + # improvement of the solution, and stop the iterations. + if alpha_xl <= alpha: + i_new = _argmin(all_alpha_xl) + step[i_new] = xl[i_new] + free_xl[i_new] = False + if alpha_xu <= alpha: + i_new = _argmin(all_alpha_xu) + step[i_new] = xu[i_new] + free_xu[i_new] = False + boundary_reached = True + break + + # Attempt to improve the solution on the trust-region boundary. + if kwargs.get("improve_tcg", True) and boundary_reached: + step_base = np.copy(step) + free_bd = free_xl & free_xu + grad = aub.T @ np.maximum(aub @ step - bub, 0.0) + aeq.T @ ( + aeq @ step - beq + ) + sd = np.zeros(n) + while np.count_nonzero(free_bd) > 0: + # Check whether a substantial reduction in the objective function + # is possible, and set the search direction. + step_sq = step[free_bd] @ step[free_bd] + grad_sq = grad[free_bd] @ grad[free_bd] + grad_step = grad[free_bd] @ step[free_bd] + grad_sd = -np.sqrt(max(step_sq * grad_sq - grad_step**2.0, 0.0)) + sd[free_bd] = grad_step * step[free_bd] - step_sq * grad[free_bd] + sd[~free_bd] = 0.0 + if grad_sd >= -1e-8 * reduct or np.any( + grad_sd >= -TINY * np.abs(sd[free_bd]) + ): + break + sd[free_bd] /= -grad_sd + + # Calculate an upper bound for the tangent of half the angle theta + # of this alternative iteration. The step will be updated as: + # step = cos(theta) * step + sin(theta) * sd. + temp_xl = np.zeros(n) + temp_xu = np.zeros(n) + temp_xl[free_bd] = ( + step[free_bd] ** 2.0 + sd[free_bd] ** 2.0 - xl[free_bd] ** 2.0 + ) + temp_xu[free_bd] = ( + step[free_bd] ** 2.0 + sd[free_bd] ** 2.0 - xu[free_bd] ** 2.0 + ) + temp_xl[temp_xl > 0.0] = ( + np.sqrt(temp_xl[temp_xl > 0.0]) - sd[temp_xl > 0.0] + ) + temp_xu[temp_xu > 0.0] = ( + np.sqrt(temp_xu[temp_xu > 0.0]) + sd[temp_xu > 0.0] + ) + dist_xl = np.maximum(step - xl, 0.0) + dist_xu = np.maximum(xu - step, 0.0) + i_xl = temp_xl > TINY * dist_xl + i_xu = temp_xu > TINY * dist_xu + all_t_xl = np.ones(n) + all_t_xu = np.ones(n) + all_t_xl[i_xl] = np.minimum( + all_t_xl[i_xl], + dist_xl[i_xl] / temp_xl[i_xl], + ) + all_t_xu[i_xu] = np.minimum( + all_t_xu[i_xu], + dist_xu[i_xu] / temp_xu[i_xu], + ) + t_xl = np.min(all_t_xl) + t_xu = np.min(all_t_xu) + t_bd = min(t_xl, t_xu) + + # For a range of equally spaced values of tan(0.5 * theta), + # calculate the reduction in the objective function that would be + # obtained by accepting the corresponding angle. + n_samples = 20 + n_samples = int((n_samples - 3) * t_bd + 3) + t_samples = np.linspace(t_bd / n_samples, t_bd, n_samples) + resid_ub = np.maximum(aub @ step - bub, 0.0) + resid_eq = aeq @ step - beq + step_proj = np.copy(step) + step_proj[~free_bd] = 0.0 + all_reduct = np.empty(n_samples) + for i in range(n_samples): + sin_value = 2.0 * t_samples[i] / (1.0 + t_samples[i] ** 2.0) + step_alt = np.clip( + step + sin_value * (sd - t_samples[i] * step_proj), + xl, + xu, + ) + resid_ub_alt = np.maximum(aub @ step_alt - bub, 0.0) + resid_eq_alt = aeq @ step_alt - beq + all_reduct[i] = 0.5 * ( + resid_ub @ resid_ub + + resid_eq @ resid_eq + - resid_ub_alt @ resid_ub_alt + - resid_eq_alt @ resid_eq_alt + ) + if np.all(all_reduct <= 0.0): + # No reduction in the objective function is obtained. + break + + # Accept the angle that provides the largest reduction in the + # objective function, and update the iterate. + i_max = np.argmax(all_reduct) + cos_value = (1.0 - t_samples[i_max] ** 2.0) / ( + 1.0 + t_samples[i_max] ** 2.0 + ) + sin_value = (2.0 * t_samples[i_max] + / (1.0 + t_samples[i_max] ** 2.0)) + step[free_bd] = cos_value * step[free_bd] + sin_value * sd[free_bd] + grad = aub.T @ np.maximum(aub @ step - bub, 0.0) + aeq.T @ ( + aeq @ step - beq + ) + reduct += all_reduct[i_max] + + # If the above angle is restricted by bound constraints, add them + # to the working set, and restart the alternative iteration. + # Otherwise, the calculations are terminated. + if t_bd < 1.0 and i_max == n_samples - 1: + if t_xl <= t_bd: + i_new = _argmin(all_t_xl) + step[i_new] = xl[i_new] + free_bd[i_new] = False + if t_xu <= t_bd: + i_new = _argmin(all_t_xu) + step[i_new] = xu[i_new] + free_bd[i_new] = False + else: + break + + # Ensure that the alternative iteration improves the objective + # function. + resid_ub = np.maximum(aub @ step - bub, 0.0) + resid_ub_base = np.maximum(aub @ step_base - bub, 0.0) + resid_eq = aeq @ step - beq + resid_eq_base = aeq @ step_base - beq + if ( + resid_ub @ resid_ub + resid_eq @ resid_eq + > resid_ub_base @ resid_ub_base + resid_eq_base @ resid_eq_base + ): + step = step_base + + if debug: + assert np.all(xl <= step) + assert np.all(step <= xu) + assert np.linalg.norm(step) < 1.1 * delta + return step + + +def qr_tangential_byrd_omojokun(aub, aeq, free_xl, free_xu, free_ub): + n = free_xl.size + identity = np.eye(n) + q, r, _ = qr( + np.block( + [ + [aeq], + [aub[~free_ub, :]], + [-identity[~free_xl, :]], + [identity[~free_xu, :]], + ] + ).T, + pivoting=True, + ) + n_act = np.count_nonzero( + np.abs(np.diag(r)) + >= 10.0 + * EPS + * n + * np.linalg.norm(r[: np.min(r.shape), : np.min(r.shape)], axis=0) + ) + return n_act, q + + +def qr_normal_byrd_omojokun(aub, free_xl, free_xu, free_slack, free_ub): + m_linear_ub, n = aub.shape + identity_n = np.eye(n) + identity_m = np.eye(m_linear_ub) + q, r, _ = qr( + np.block( + [ + [ + aub[~free_ub, :], + -identity_m[~free_ub, :], + ], + [ + np.zeros((m_linear_ub - np.count_nonzero(free_slack), n)), + -identity_m[~free_slack, :], + ], + [ + -identity_n[~free_xl, :], + np.zeros((n - np.count_nonzero(free_xl), m_linear_ub)), + ], + [ + identity_n[~free_xu, :], + np.zeros((n - np.count_nonzero(free_xu), m_linear_ub)), + ], + ] + ).T, + pivoting=True, + ) + n_act = np.count_nonzero( + np.abs(np.diag(r)) + >= 10.0 + * EPS + * (n + m_linear_ub) + * np.linalg.norm(r[: np.min(r.shape), : np.min(r.shape)], axis=0) + ) + return n_act, q + + +def _alpha_tr(step, sd, delta): + step_sd = step @ sd + sd_sq = sd @ sd + dist_tr_sq = delta**2.0 - step @ step + temp = np.sqrt(max(step_sd**2.0 + sd_sq * dist_tr_sq, 0.0)) + if step_sd <= 0.0 and sd_sq > TINY * abs(temp - step_sd): + alpha_tr = max((temp - step_sd) / sd_sq, 0.0) + elif abs(temp + step_sd) > TINY * dist_tr_sq: + alpha_tr = max(dist_tr_sq / (temp + step_sd), 0.0) + else: + raise ZeroDivisionError + return alpha_tr + + +def _argmax(x): + return np.flatnonzero(x >= np.max(x)) + + +def _argmin(x): + return np.flatnonzero(x <= np.min(x)) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/utils/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/utils/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..fe6b4841ddff3a04bda5cbff744e30681b6963b9 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/utils/__init__.py @@ -0,0 +1,18 @@ +from .exceptions import ( + MaxEvalError, + TargetSuccess, + CallbackSuccess, + FeasibleSuccess, +) +from .math import get_arrays_tol, exact_1d_array +from .versions import show_versions + +__all__ = [ + "MaxEvalError", + "TargetSuccess", + "CallbackSuccess", + "FeasibleSuccess", + "get_arrays_tol", + "exact_1d_array", + "show_versions", +] diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/utils/exceptions.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/utils/exceptions.py new file mode 100644 index 0000000000000000000000000000000000000000..c85094894f378a8e3934ad109ea6166e33e4366b --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/utils/exceptions.py @@ -0,0 +1,22 @@ +class MaxEvalError(Exception): + """ + Exception raised when the maximum number of evaluations is reached. + """ + + +class TargetSuccess(Exception): + """ + Exception raised when the target value is reached. + """ + + +class CallbackSuccess(StopIteration): + """ + Exception raised when the callback function raises a ``StopIteration``. + """ + + +class FeasibleSuccess(Exception): + """ + Exception raised when a feasible point of a feasible problem is found. + """ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/utils/math.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/utils/math.py new file mode 100644 index 0000000000000000000000000000000000000000..1b16ae98a0df38752815f5a69d56da20f856f9f9 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/utils/math.py @@ -0,0 +1,77 @@ +import numpy as np + + +EPS = np.finfo(float).eps + + +def get_arrays_tol(*arrays): + """ + Get a relative tolerance for a set of arrays. + + Parameters + ---------- + *arrays: tuple + Set of `numpy.ndarray` to get the tolerance for. + + Returns + ------- + float + Relative tolerance for the set of arrays. + + Raises + ------ + ValueError + If no array is provided. + """ + if len(arrays) == 0: + raise ValueError("At least one array must be provided.") + size = max(array.size for array in arrays) + weight = max( + np.max(np.abs(array[np.isfinite(array)]), initial=1.0) + for array in arrays + ) + return 10.0 * EPS * max(size, 1.0) * weight + + +def exact_1d_array(x, message): + """ + Preprocess a 1-dimensional array. + + Parameters + ---------- + x : array_like + Array to be preprocessed. + message : str + Error message if `x` cannot be interpreter as a 1-dimensional array. + + Returns + ------- + `numpy.ndarray` + Preprocessed array. + """ + x = np.atleast_1d(np.squeeze(x)).astype(float) + if x.ndim != 1: + raise ValueError(message) + return x + + +def exact_2d_array(x, message): + """ + Preprocess a 2-dimensional array. + + Parameters + ---------- + x : array_like + Array to be preprocessed. + message : str + Error message if `x` cannot be interpreter as a 2-dimensional array. + + Returns + ------- + `numpy.ndarray` + Preprocessed array. + """ + x = np.atleast_2d(x).astype(float) + if x.ndim != 2: + raise ValueError(message) + return x diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/utils/versions.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/utils/versions.py new file mode 100644 index 0000000000000000000000000000000000000000..94a0f8f5cef626354f40901cbe06a84287291c1c --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/utils/versions.py @@ -0,0 +1,67 @@ +import os +import platform +import sys +from importlib.metadata import PackageNotFoundError, version + + +def _get_sys_info(): + """ + Get useful system information. + + Returns + ------- + dict + Useful system information. + """ + return { + "python": sys.version.replace(os.linesep, " "), + "executable": sys.executable, + "machine": platform.platform(), + } + + +def _get_deps_info(): + """ + Get the versions of the dependencies. + + Returns + ------- + dict + Versions of the dependencies. + """ + deps = ["cobyqa", "numpy", "scipy", "setuptools", "pip"] + deps_info = {} + for module in deps: + try: + deps_info[module] = version(module) + except PackageNotFoundError: + deps_info[module] = None + return deps_info + + +def show_versions(): + """ + Display useful system and dependencies information. + + When reporting issues, please include this information. + """ + print("System settings") + print("---------------") + sys_info = _get_sys_info() + print( + "\n".join( + f"{k:>{max(map(len, sys_info.keys())) + 1}}: {v}" + for k, v in sys_info.items() + ) + ) + + print() + print("Python dependencies") + print("-------------------") + deps_info = _get_deps_info() + print( + "\n".join( + f"{k:>{max(map(len, deps_info.keys())) + 1}}: {v}" + for k, v in deps_info.items() + ) + ) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test__gcutils.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test__gcutils.py new file mode 100644 index 0000000000000000000000000000000000000000..0e397af4fb7e9bc69f31d1e39aa80716469d5470 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test__gcutils.py @@ -0,0 +1,110 @@ +""" Test for assert_deallocated context manager and gc utilities +""" +import gc +from threading import Lock + +from scipy._lib._gcutils import (set_gc_state, gc_state, assert_deallocated, + ReferenceError, IS_PYPY) + +from numpy.testing import assert_equal + +import pytest + + +@pytest.fixture +def gc_lock(): + return Lock() + + +def test_set_gc_state(gc_lock): + with gc_lock: + gc_status = gc.isenabled() + try: + for state in (True, False): + gc.enable() + set_gc_state(state) + assert_equal(gc.isenabled(), state) + gc.disable() + set_gc_state(state) + assert_equal(gc.isenabled(), state) + finally: + if gc_status: + gc.enable() + + +def test_gc_state(gc_lock): + # Test gc_state context manager + with gc_lock: + gc_status = gc.isenabled() + try: + for pre_state in (True, False): + set_gc_state(pre_state) + for with_state in (True, False): + # Check the gc state is with_state in with block + with gc_state(with_state): + assert_equal(gc.isenabled(), with_state) + # And returns to previous state outside block + assert_equal(gc.isenabled(), pre_state) + # Even if the gc state is set explicitly within the block + with gc_state(with_state): + assert_equal(gc.isenabled(), with_state) + set_gc_state(not with_state) + assert_equal(gc.isenabled(), pre_state) + finally: + if gc_status: + gc.enable() + + +@pytest.mark.skipif(IS_PYPY, reason="Test not meaningful on PyPy") +def test_assert_deallocated(gc_lock): + # Ordinary use + class C: + def __init__(self, arg0, arg1, name='myname'): + self.name = name + with gc_lock: + for gc_current in (True, False): + with gc_state(gc_current): + # We are deleting from with-block context, so that's OK + with assert_deallocated(C, 0, 2, 'another name') as c: + assert_equal(c.name, 'another name') + del c + # Or not using the thing in with-block context, also OK + with assert_deallocated(C, 0, 2, name='third name'): + pass + assert_equal(gc.isenabled(), gc_current) + + +@pytest.mark.skipif(IS_PYPY, reason="Test not meaningful on PyPy") +def test_assert_deallocated_nodel(): + class C: + pass + with pytest.raises(ReferenceError): + # Need to delete after using if in with-block context + # Note: assert_deallocated(C) needs to be assigned for the test + # to function correctly. It is assigned to _, but _ itself is + # not referenced in the body of the with, it is only there for + # the refcount. + with assert_deallocated(C) as _: + pass + + +@pytest.mark.skipif(IS_PYPY, reason="Test not meaningful on PyPy") +def test_assert_deallocated_circular(): + class C: + def __init__(self): + self._circular = self + with pytest.raises(ReferenceError): + # Circular reference, no automatic garbage collection + with assert_deallocated(C) as c: + del c + + +@pytest.mark.skipif(IS_PYPY, reason="Test not meaningful on PyPy") +def test_assert_deallocated_circular2(): + class C: + def __init__(self): + self._circular = self + with pytest.raises(ReferenceError): + # Still circular reference, no automatic garbage collection + with assert_deallocated(C): + pass diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test__pep440.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test__pep440.py new file mode 100644 index 0000000000000000000000000000000000000000..7f5b71c8f1e13b42de2e8e612a005dec409fc025 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test__pep440.py @@ -0,0 +1,67 @@ +from pytest import raises as assert_raises +from scipy._lib._pep440 import Version, parse + + +def test_main_versions(): + assert Version('1.8.0') == Version('1.8.0') + for ver in ['1.9.0', '2.0.0', '1.8.1']: + assert Version('1.8.0') < Version(ver) + + for ver in ['1.7.0', '1.7.1', '0.9.9']: + assert Version('1.8.0') > Version(ver) + + +def test_version_1_point_10(): + # regression test for gh-2998. + assert Version('1.9.0') < Version('1.10.0') + assert Version('1.11.0') < Version('1.11.1') + assert Version('1.11.0') == Version('1.11.0') + assert Version('1.99.11') < Version('1.99.12') + + +def test_alpha_beta_rc(): + assert Version('1.8.0rc1') == Version('1.8.0rc1') + for ver in ['1.8.0', '1.8.0rc2']: + assert Version('1.8.0rc1') < Version(ver) + + for ver in ['1.8.0a2', '1.8.0b3', '1.7.2rc4']: + assert Version('1.8.0rc1') > Version(ver) + + assert Version('1.8.0b1') > Version('1.8.0a2') + + +def test_dev_version(): + assert Version('1.9.0.dev+Unknown') < Version('1.9.0') + for ver in ['1.9.0', '1.9.0a1', '1.9.0b2', '1.9.0b2.dev+ffffffff', '1.9.0.dev1']: + assert Version('1.9.0.dev+f16acvda') < Version(ver) + + assert Version('1.9.0.dev+f16acvda') == Version('1.9.0.dev+f16acvda') + + +def test_dev_a_b_rc_mixed(): + assert Version('1.9.0a2.dev+f16acvda') == Version('1.9.0a2.dev+f16acvda') + assert Version('1.9.0a2.dev+6acvda54') < Version('1.9.0a2') + + +def test_dev0_version(): + assert Version('1.9.0.dev0+Unknown') < Version('1.9.0') + for ver in ['1.9.0', '1.9.0a1', '1.9.0b2', '1.9.0b2.dev0+ffffffff']: + assert Version('1.9.0.dev0+f16acvda') < Version(ver) + + assert Version('1.9.0.dev0+f16acvda') == Version('1.9.0.dev0+f16acvda') + + +def test_dev0_a_b_rc_mixed(): + assert Version('1.9.0a2.dev0+f16acvda') == Version('1.9.0a2.dev0+f16acvda') + assert Version('1.9.0a2.dev0+6acvda54') < Version('1.9.0a2') + + +def test_raises(): + for ver in ['1,9.0', '1.7.x']: + assert_raises(ValueError, Version, ver) + +def test_legacy_version(): + # Non-PEP-440 version identifiers always compare less. For NumPy this only + # occurs on dev builds prior to 1.10.0 which are unsupported anyway. + assert parse('invalid') < Version('0.0.0') + assert parse('1.9.0-f16acvda') < Version('1.0.0') diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test__testutils.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test__testutils.py new file mode 100644 index 0000000000000000000000000000000000000000..88db113d6d5a35c96ecc0a6a36ab42d74be49153 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test__testutils.py @@ -0,0 +1,32 @@ +import sys +from scipy._lib._testutils import _parse_size, _get_mem_available +import pytest + + +def test__parse_size(): + expected = { + '12': 12e6, + '12 b': 12, + '12k': 12e3, + ' 12 M ': 12e6, + ' 12 G ': 12e9, + ' 12Tb ': 12e12, + '12 Mib ': 12 * 1024.0**2, + '12Tib': 12 * 1024.0**4, + } + + for inp, outp in sorted(expected.items()): + if outp is None: + with pytest.raises(ValueError): + _parse_size(inp) + else: + assert _parse_size(inp) == outp + + +def test__mem_available(): + # May return None on non-Linux platforms + available = _get_mem_available() + if sys.platform.startswith('linux'): + assert available >= 0 + else: + assert available is None or available >= 0 diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test__threadsafety.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test__threadsafety.py new file mode 100644 index 0000000000000000000000000000000000000000..87ae85ef318da2b8bb104c4a87faa4e4021c01d5 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test__threadsafety.py @@ -0,0 +1,51 @@ +import threading +import time +import traceback + +from numpy.testing import assert_ +from pytest import raises as assert_raises + +from scipy._lib._threadsafety import ReentrancyLock, non_reentrant, ReentrancyError + + +def test_parallel_threads(): + # Check that ReentrancyLock serializes work in parallel threads. + # + # The test is not fully deterministic, and may succeed falsely if + # the timings go wrong. + + lock = ReentrancyLock("failure") + + failflag = [False] + exceptions_raised = [] + + def worker(k): + try: + with lock: + assert_(not failflag[0]) + failflag[0] = True + time.sleep(0.1 * k) + assert_(failflag[0]) + failflag[0] = False + except Exception: + exceptions_raised.append(traceback.format_exc(2)) + + threads = [threading.Thread(target=lambda k=k: worker(k)) + for k in range(3)] + for t in threads: + t.start() + for t in threads: + t.join() + + exceptions_raised = "\n".join(exceptions_raised) + assert_(not exceptions_raised, exceptions_raised) + + +def test_reentering(): + # Check that ReentrancyLock prevents re-entering from the same thread. + + @non_reentrant() + def func(x): + return func(x) + + assert_raises(ReentrancyError, func, 0) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test__util.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test__util.py new file mode 100644 index 0000000000000000000000000000000000000000..2a4d22ce468ec951355961cb77dda15b56899818 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test__util.py @@ -0,0 +1,657 @@ +from multiprocessing import Pool +from multiprocessing.pool import Pool as PWL +import re +import math +from fractions import Fraction + +import numpy as np +from numpy.testing import assert_equal, assert_ +import pytest +from pytest import raises as assert_raises +import hypothesis.extra.numpy as npst +from hypothesis import given, strategies, reproduce_failure # noqa: F401 +from scipy.conftest import array_api_compatible, skip_xp_invalid_arg + +from scipy._lib._array_api import (xp_assert_equal, xp_assert_close, is_numpy, + xp_copy, is_array_api_strict) +from scipy._lib._util import (_aligned_zeros, check_random_state, MapWrapper, + getfullargspec_no_self, FullArgSpec, + rng_integers, _validate_int, _rename_parameter, + _contains_nan, _rng_html_rewrite, _lazywhere) +from scipy import cluster, interpolate, linalg, optimize, sparse, spatial, stats + +skip_xp_backends = pytest.mark.skip_xp_backends + + +@pytest.mark.slow +def test__aligned_zeros(): + niter = 10 + + def check(shape, dtype, order, align): + err_msg = repr((shape, dtype, order, align)) + x = _aligned_zeros(shape, dtype, order, align=align) + if align is None: + align = np.dtype(dtype).alignment + assert_equal(x.__array_interface__['data'][0] % align, 0) + if hasattr(shape, '__len__'): + assert_equal(x.shape, shape, err_msg) + else: + assert_equal(x.shape, (shape,), err_msg) + assert_equal(x.dtype, dtype) + if order == "C": + assert_(x.flags.c_contiguous, err_msg) + elif order == "F": + if x.size > 0: + # Size-0 arrays get invalid flags on NumPy 1.5 + assert_(x.flags.f_contiguous, err_msg) + elif order is None: + assert_(x.flags.c_contiguous, err_msg) + else: + raise ValueError() + + # try various alignments + for align in [1, 2, 3, 4, 8, 16, 32, 64, None]: + for n in [0, 1, 3, 11]: + for order in ["C", "F", None]: + for dtype in [np.uint8, np.float64]: + for shape in [n, (1, 2, 3, n)]: + for j in range(niter): + check(shape, dtype, order, align) + + +def test_check_random_state(): + # If seed is None, return the RandomState singleton used by np.random. + # If seed is an int, return a new RandomState instance seeded with seed. + # If seed is already a RandomState instance, return it. + # Otherwise raise ValueError. + rsi = check_random_state(1) + assert_equal(type(rsi), np.random.RandomState) + rsi = check_random_state(rsi) + assert_equal(type(rsi), np.random.RandomState) + rsi = check_random_state(None) + assert_equal(type(rsi), np.random.RandomState) + assert_raises(ValueError, check_random_state, 'a') + rg = np.random.Generator(np.random.PCG64()) + rsi = check_random_state(rg) + assert_equal(type(rsi), np.random.Generator) + + +def test_getfullargspec_no_self(): + p = MapWrapper(1) + argspec = getfullargspec_no_self(p.__init__) + assert_equal(argspec, FullArgSpec(['pool'], None, None, (1,), [], + None, {})) + argspec = getfullargspec_no_self(p.__call__) + assert_equal(argspec, FullArgSpec(['func', 'iterable'], None, None, None, + [], None, {})) + + class _rv_generic: + def _rvs(self, a, b=2, c=3, *args, size=None, **kwargs): + return None + + rv_obj = _rv_generic() + argspec = getfullargspec_no_self(rv_obj._rvs) + assert_equal(argspec, FullArgSpec(['a', 'b', 'c'], 'args', 'kwargs', + (2, 3), ['size'], {'size': None}, {})) + + +def test_mapwrapper_serial(): + in_arg = np.arange(10.) + out_arg = np.sin(in_arg) + + p = MapWrapper(1) + assert_(p._mapfunc is map) + assert_(p.pool is None) + assert_(p._own_pool is False) + out = list(p(np.sin, in_arg)) + assert_equal(out, out_arg) + + with assert_raises(RuntimeError): + p = MapWrapper(0) + + +def test_pool(): + with Pool(2) as p: + p.map(math.sin, [1, 2, 3, 4]) + + +def test_mapwrapper_parallel(): + in_arg = np.arange(10.) + out_arg = np.sin(in_arg) + + with MapWrapper(2) as p: + out = p(np.sin, in_arg) + assert_equal(list(out), out_arg) + + assert_(p._own_pool is True) + assert_(isinstance(p.pool, PWL)) + assert_(p._mapfunc is not None) + + # the context manager should've closed the internal pool + # check that it has by asking it to calculate again. + with assert_raises(Exception) as excinfo: + p(np.sin, in_arg) + + assert_(excinfo.type is ValueError) + + # can also set a PoolWrapper up with a map-like callable instance + with Pool(2) as p: + q = MapWrapper(p.map) + + assert_(q._own_pool is False) + q.close() + + # closing the PoolWrapper shouldn't close the internal pool + # because it didn't create it + out = p.map(np.sin, in_arg) + assert_equal(list(out), out_arg) + + +def test_rng_integers(): + rng = np.random.RandomState() + + # test that numbers are inclusive of high point + arr = rng_integers(rng, low=2, high=5, size=100, endpoint=True) + assert np.max(arr) == 5 + assert np.min(arr) == 2 + assert arr.shape == (100, ) + + # test that numbers are inclusive of high point + arr = rng_integers(rng, low=5, size=100, endpoint=True) + assert np.max(arr) == 5 + assert np.min(arr) == 0 + assert arr.shape == (100, ) + + # test that numbers are exclusive of high point + arr = rng_integers(rng, low=2, high=5, size=100, endpoint=False) + assert np.max(arr) == 4 + assert np.min(arr) == 2 + assert arr.shape == (100, ) + + # test that numbers are exclusive of high point + arr = rng_integers(rng, low=5, size=100, endpoint=False) + assert np.max(arr) == 4 + assert np.min(arr) == 0 + assert arr.shape == (100, ) + + # now try with np.random.Generator + try: + rng = np.random.default_rng() + except AttributeError: + return + + # test that numbers are inclusive of high point + arr = rng_integers(rng, low=2, high=5, size=100, endpoint=True) + assert np.max(arr) == 5 + assert np.min(arr) == 2 + assert arr.shape == (100, ) + + # test that numbers are inclusive of high point + arr = rng_integers(rng, low=5, size=100, endpoint=True) + assert np.max(arr) == 5 + assert np.min(arr) == 0 + assert arr.shape == (100, ) + + # test that numbers are exclusive of high point + arr = rng_integers(rng, low=2, high=5, size=100, endpoint=False) + assert np.max(arr) == 4 + assert np.min(arr) == 2 + assert arr.shape == (100, ) + + # test that numbers are exclusive of high point + arr = rng_integers(rng, low=5, size=100, endpoint=False) + assert np.max(arr) == 4 + assert np.min(arr) == 0 + assert arr.shape == (100, ) + + +class TestValidateInt: + + @pytest.mark.parametrize('n', [4, np.uint8(4), np.int16(4), np.array(4)]) + def test_validate_int(self, n): + n = _validate_int(n, 'n') + assert n == 4 + + @pytest.mark.parametrize('n', [4.0, np.array([4]), Fraction(4, 1)]) + def test_validate_int_bad(self, n): + with pytest.raises(TypeError, match='n must be an integer'): + _validate_int(n, 'n') + + def test_validate_int_below_min(self): + with pytest.raises(ValueError, match='n must be an integer not ' + 'less than 0'): + _validate_int(-1, 'n', 0) + + +class TestRenameParameter: + # check that wrapper `_rename_parameter` for backward-compatible + # keyword renaming works correctly + + # Example method/function that still accepts keyword `old` + @_rename_parameter("old", "new") + def old_keyword_still_accepted(self, new): + return new + + # Example method/function for which keyword `old` is deprecated + @_rename_parameter("old", "new", dep_version="1.9.0") + def old_keyword_deprecated(self, new): + return new + + def test_old_keyword_still_accepted(self): + # positional argument and both keyword work identically + res1 = self.old_keyword_still_accepted(10) + res2 = self.old_keyword_still_accepted(new=10) + res3 = self.old_keyword_still_accepted(old=10) + assert res1 == res2 == res3 == 10 + + # unexpected keyword raises an error + message = re.escape("old_keyword_still_accepted() got an unexpected") + with pytest.raises(TypeError, match=message): + self.old_keyword_still_accepted(unexpected=10) + + # multiple values for the same parameter raises an error + message = re.escape("old_keyword_still_accepted() got multiple") + with pytest.raises(TypeError, match=message): + self.old_keyword_still_accepted(10, new=10) + with pytest.raises(TypeError, match=message): + self.old_keyword_still_accepted(10, old=10) + with pytest.raises(TypeError, match=message): + self.old_keyword_still_accepted(new=10, old=10) + + @pytest.fixture + def kwarg_lock(self): + from threading import Lock + return Lock() + + def test_old_keyword_deprecated(self, kwarg_lock): + # positional argument and both keyword work identically, + # but use of old keyword results in DeprecationWarning + dep_msg = "Use of keyword argument `old` is deprecated" + res1 = self.old_keyword_deprecated(10) + res2 = self.old_keyword_deprecated(new=10) + # pytest warning filter is not thread-safe, enforce serialization + with kwarg_lock: + with pytest.warns(DeprecationWarning, match=dep_msg): + res3 = self.old_keyword_deprecated(old=10) + assert res1 == res2 == res3 == 10 + + # unexpected keyword raises an error + message = re.escape("old_keyword_deprecated() got an unexpected") + with pytest.raises(TypeError, match=message): + self.old_keyword_deprecated(unexpected=10) + + # multiple values for the same parameter raises an error and, + # if old keyword is used, results in DeprecationWarning + message = re.escape("old_keyword_deprecated() got multiple") + with pytest.raises(TypeError, match=message): + self.old_keyword_deprecated(10, new=10) + with kwarg_lock: + with pytest.raises(TypeError, match=message), \ + pytest.warns(DeprecationWarning, match=dep_msg): + # breakpoint() + self.old_keyword_deprecated(10, old=10) + with kwarg_lock: + with pytest.raises(TypeError, match=message), \ + pytest.warns(DeprecationWarning, match=dep_msg): + self.old_keyword_deprecated(new=10, old=10) + + +class TestContainsNaNTest: + + def test_policy(self): + data = np.array([1, 2, 3, np.nan]) + + contains_nan, nan_policy = _contains_nan(data, nan_policy="propagate") + assert contains_nan + assert nan_policy == "propagate" + + contains_nan, nan_policy = _contains_nan(data, nan_policy="omit") + assert contains_nan + assert nan_policy == "omit" + + msg = "The input contains nan values" + with pytest.raises(ValueError, match=msg): + _contains_nan(data, nan_policy="raise") + + msg = "nan_policy must be one of" + with pytest.raises(ValueError, match=msg): + _contains_nan(data, nan_policy="nan") + + def test_contains_nan(self): + data1 = np.array([1, 2, 3]) + assert not _contains_nan(data1)[0] + + data2 = np.array([1, 2, 3, np.nan]) + assert _contains_nan(data2)[0] + + data3 = np.array([np.nan, 2, 3, np.nan]) + assert _contains_nan(data3)[0] + + data4 = np.array([[1, 2], [3, 4]]) + assert not _contains_nan(data4)[0] + + data5 = np.array([[1, 2], [3, np.nan]]) + assert _contains_nan(data5)[0] + + @skip_xp_invalid_arg + def test_contains_nan_with_strings(self): + data1 = np.array([1, 2, "3", np.nan]) # converted to string "nan" + assert not _contains_nan(data1)[0] + + data2 = np.array([1, 2, "3", np.nan], dtype='object') + assert _contains_nan(data2)[0] + + data3 = np.array([["1", 2], [3, np.nan]]) # converted to string "nan" + assert not _contains_nan(data3)[0] + + data4 = np.array([["1", 2], [3, np.nan]], dtype='object') + assert _contains_nan(data4)[0] + + @skip_xp_backends('jax.numpy', + reason="JAX arrays do not support item assignment") + @pytest.mark.usefixtures("skip_xp_backends") + @array_api_compatible + @pytest.mark.parametrize("nan_policy", ['propagate', 'omit', 'raise']) + def test_array_api(self, xp, nan_policy): + rng = np.random.default_rng(932347235892482) + x0 = rng.random(size=(2, 3, 4)) + x = xp.asarray(x0) + x_nan = xp_copy(x, xp=xp) + x_nan[1, 2, 1] = np.nan + + contains_nan, nan_policy_out = _contains_nan(x, nan_policy=nan_policy) + assert not contains_nan + assert nan_policy_out == nan_policy + + if nan_policy == 'raise': + message = 'The input contains...' + with pytest.raises(ValueError, match=message): + _contains_nan(x_nan, nan_policy=nan_policy) + elif nan_policy == 'omit' and not is_numpy(xp): + message = "`nan_policy='omit' is incompatible..." + with pytest.raises(ValueError, match=message): + _contains_nan(x_nan, nan_policy=nan_policy) + elif nan_policy == 'propagate': + contains_nan, nan_policy_out = _contains_nan( + x_nan, nan_policy=nan_policy) + assert contains_nan + assert nan_policy_out == nan_policy + + +def test__rng_html_rewrite(): + def mock_str(): + lines = [ + 'np.random.default_rng(8989843)', + 'np.random.default_rng(seed)', + 'np.random.default_rng(0x9a71b21474694f919882289dc1559ca)', + ' bob ', + ] + return lines + + res = _rng_html_rewrite(mock_str)() + ref = [ + 'np.random.default_rng()', + 'np.random.default_rng(seed)', + 'np.random.default_rng()', + ' bob ', + ] + + assert res == ref + + +class TestTransitionToRNG: + def kmeans(self, **kwargs): + rng = np.random.default_rng(3458934594269824562) + return cluster.vq.kmeans2(rng.random(size=(20, 3)), 3, **kwargs) + + def kmeans2(self, **kwargs): + rng = np.random.default_rng(3458934594269824562) + return cluster.vq.kmeans2(rng.random(size=(20, 3)), 3, **kwargs) + + def barycentric(self, **kwargs): + rng = np.random.default_rng(3458934594269824562) + x1, x2, y1 = rng.random((3, 10)) + f = interpolate.BarycentricInterpolator(x1, y1, **kwargs) + return f(x2) + + def clarkson_woodruff_transform(self, **kwargs): + rng = np.random.default_rng(3458934594269824562) + return linalg.clarkson_woodruff_transform(rng.random((10, 10)), 3, **kwargs) + + def basinhopping(self, **kwargs): + rng = np.random.default_rng(3458934594269824562) + return optimize.basinhopping(optimize.rosen, rng.random(3), **kwargs).x + + def opt(self, fun, **kwargs): + rng = np.random.default_rng(3458934594269824562) + bounds = optimize.Bounds(-rng.random(3) * 10, rng.random(3) * 10) + return fun(optimize.rosen, bounds, **kwargs).x + + def differential_evolution(self, **kwargs): + return self.opt(optimize.differential_evolution, **kwargs) + + def dual_annealing(self, **kwargs): + return self.opt(optimize.dual_annealing, **kwargs) + + def check_grad(self, **kwargs): + rng = np.random.default_rng(3458934594269824562) + x = rng.random(3) + return optimize.check_grad(optimize.rosen, optimize.rosen_der, x, + direction='random', **kwargs) + + def random_array(self, **kwargs): + return sparse.random_array((10, 10), density=1.0, **kwargs).toarray() + + def random(self, **kwargs): + return sparse.random(10, 10, density=1.0, **kwargs).toarray() + + def rand(self, **kwargs): + return sparse.rand(10, 10, density=1.0, **kwargs).toarray() + + def svds(self, **kwargs): + rng = np.random.default_rng(3458934594269824562) + A = rng.random((10, 10)) + return sparse.linalg.svds(A, **kwargs) + + def random_rotation(self, **kwargs): + return spatial.transform.Rotation.random(3, **kwargs).as_matrix() + + def goodness_of_fit(self, **kwargs): + rng = np.random.default_rng(3458934594269824562) + data = rng.random(100) + return stats.goodness_of_fit(stats.laplace, data, **kwargs).pvalue + + def permutation_test(self, **kwargs): + rng = np.random.default_rng(3458934594269824562) + data = tuple(rng.random((2, 100))) + def statistic(x, y, axis): return np.mean(x, axis=axis) - np.mean(y, axis=axis) + return stats.permutation_test(data, statistic, **kwargs).pvalue + + def bootstrap(self, **kwargs): + rng = np.random.default_rng(3458934594269824562) + data = (rng.random(100),) + return stats.bootstrap(data, np.mean, **kwargs).confidence_interval + + def dunnett(self, **kwargs): + rng = np.random.default_rng(3458934594269824562) + x, y, control = rng.random((3, 100)) + return stats.dunnett(x, y, control=control, **kwargs).pvalue + + def sobol_indices(self, **kwargs): + def f_ishigami(x): return (np.sin(x[0]) + 7 * np.sin(x[1]) ** 2 + + 0.1 * (x[2] ** 4) * np.sin(x[0])) + dists = [stats.uniform(loc=-np.pi, scale=2 * np.pi), + stats.uniform(loc=-np.pi, scale=2 * np.pi), + stats.uniform(loc=-np.pi, scale=2 * np.pi)] + res = stats.sobol_indices(func=f_ishigami, n=1024, dists=dists, **kwargs) + return res.first_order + + def qmc_engine(self, engine, **kwargs): + qrng = engine(d=1, **kwargs) + return qrng.random(4) + + def halton(self, **kwargs): + return self.qmc_engine(stats.qmc.Halton, **kwargs) + + def sobol(self, **kwargs): + return self.qmc_engine(stats.qmc.Sobol, **kwargs) + + def latin_hypercube(self, **kwargs): + return self.qmc_engine(stats.qmc.LatinHypercube, **kwargs) + + def poisson_disk(self, **kwargs): + return self.qmc_engine(stats.qmc.PoissonDisk, **kwargs) + + def multivariate_normal_qmc(self, **kwargs): + X = stats.qmc.MultivariateNormalQMC([0], **kwargs) + return X.random(4) + + def multinomial_qmc(self, **kwargs): + X = stats.qmc.MultinomialQMC([0.5, 0.5], 4, **kwargs) + return X.random(4) + + def permutation_method(self, **kwargs): + rng = np.random.default_rng(3458934594269824562) + data = tuple(rng.random((2, 100))) + method = stats.PermutationMethod(**kwargs) + return stats.pearsonr(*data, method=method).pvalue + + def bootstrap_method(self, **kwargs): + rng = np.random.default_rng(3458934594269824562) + data = tuple(rng.random((2, 100))) + res = stats.pearsonr(*data) + method = stats.BootstrapMethod(**kwargs) + return res.confidence_interval(method=method) + + @pytest.mark.fail_slow(10) + @pytest.mark.slow + @pytest.mark.parametrize("method, arg_name", [ + (kmeans, "seed"), + (kmeans2, "seed"), + (barycentric, "random_state"), + (clarkson_woodruff_transform, "seed"), + (basinhopping, "seed"), + (differential_evolution, "seed"), + (dual_annealing, "seed"), + (check_grad, "seed"), + (random_array, 'random_state'), + (random, 'random_state'), + (rand, 'random_state'), + (svds, "random_state"), + (random_rotation, "random_state"), + (goodness_of_fit, "random_state"), + (permutation_test, "random_state"), + (bootstrap, "random_state"), + (permutation_method, "random_state"), + (bootstrap_method, "random_state"), + (dunnett, "random_state"), + (sobol_indices, "random_state"), + (halton, "seed"), + (sobol, "seed"), + (latin_hypercube, "seed"), + (poisson_disk, "seed"), + (multivariate_normal_qmc, "seed"), + (multinomial_qmc, "seed"), + ]) + def test_rng_deterministic(self, method, arg_name): + np.random.seed(None) + seed = 2949672964 + + rng = np.random.default_rng(seed) + message = "got multiple values for argument now known as `rng`" + with pytest.raises(TypeError, match=message): + method(self, **{'rng': rng, arg_name: seed}) + + rng = np.random.default_rng(seed) + res1 = method(self, rng=rng) + res2 = method(self, rng=seed) + assert_equal(res2, res1) + + if method.__name__ in {"dunnett", "sobol_indices"}: + # the two kwargs have essentially the same behavior for these functions + res3 = method(self, **{arg_name: seed}) + assert_equal(res3, res1) + return + + rng = np.random.RandomState(seed) + res1 = method(self, **{arg_name: rng}) + res2 = method(self, **{arg_name: seed}) + + if method.__name__ in {"halton", "sobol", "latin_hypercube", "poisson_disk", + "multivariate_normal_qmc", "multinomial_qmc"}: + # For these, passing `random_state=RandomState(seed)` is not the same as + # passing integer `seed`. + res1b = method(self, **{arg_name: np.random.RandomState(seed)}) + assert_equal(res1b, res1) + res2b = method(self, **{arg_name: seed}) + assert_equal(res2b, res2) + return + + np.random.seed(seed) + res3 = method(self, **{arg_name: None}) + assert_equal(res2, res1) + assert_equal(res3, res1) + + +class TestLazywhere: + n_arrays = strategies.integers(min_value=1, max_value=3) + rng_seed = strategies.integers(min_value=1000000000, max_value=9999999999) + dtype = strategies.sampled_from((np.float32, np.float64)) + p = strategies.floats(min_value=0, max_value=1) + data = strategies.data() + + @pytest.mark.fail_slow(10) + @pytest.mark.filterwarnings('ignore::RuntimeWarning') # overflows, etc. + @skip_xp_backends('jax.numpy', + reason="JAX arrays do not support item assignment") + @pytest.mark.usefixtures("skip_xp_backends") + @array_api_compatible + @given(n_arrays=n_arrays, rng_seed=rng_seed, dtype=dtype, p=p, data=data) + @pytest.mark.thread_unsafe + def test_basic(self, n_arrays, rng_seed, dtype, p, data, xp): + mbs = npst.mutually_broadcastable_shapes(num_shapes=n_arrays+1, + min_side=0) + input_shapes, result_shape = data.draw(mbs) + cond_shape, *shapes = input_shapes + elements = {'allow_subnormal': False} # cupy/cupy#8382 + fillvalue = xp.asarray(data.draw(npst.arrays(dtype=dtype, shape=tuple(), + elements=elements))) + float_fillvalue = float(fillvalue) + arrays = [xp.asarray(data.draw(npst.arrays(dtype=dtype, shape=shape))) + for shape in shapes] + + def f(*args): + return sum(arg for arg in args) + + def f2(*args): + return sum(arg for arg in args) / 2 + + rng = np.random.default_rng(rng_seed) + cond = xp.asarray(rng.random(size=cond_shape) > p) + + res1 = _lazywhere(cond, arrays, f, fillvalue) + res2 = _lazywhere(cond, arrays, f, f2=f2) + if not is_array_api_strict(xp): + res3 = _lazywhere(cond, arrays, f, float_fillvalue) + + # Ensure arrays are at least 1d to follow sane type promotion rules. + # This can be removed when minimum supported NumPy is 2.0 + if xp == np: + cond, fillvalue, *arrays = np.atleast_1d(cond, fillvalue, *arrays) + + ref1 = xp.where(cond, f(*arrays), fillvalue) + ref2 = xp.where(cond, f(*arrays), f2(*arrays)) + if not is_array_api_strict(xp): + # Array API standard doesn't currently define behavior when fillvalue is a + # Python scalar. When it does, test can be run with array_api_strict, too. + ref3 = xp.where(cond, f(*arrays), float_fillvalue) + + if xp == np: # because we ensured arrays are at least 1d + ref1 = ref1.reshape(result_shape) + ref2 = ref2.reshape(result_shape) + ref3 = ref3.reshape(result_shape) + + xp_assert_close(res1, ref1, rtol=2e-16) + xp_assert_equal(res2, ref2) + if not is_array_api_strict(xp): + xp_assert_equal(res3, ref3) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_array_api.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_array_api.py new file mode 100644 index 0000000000000000000000000000000000000000..f425eb4327fe042a3cf8eb79cf3402f23b526ae7 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_array_api.py @@ -0,0 +1,191 @@ +import numpy as np +import pytest + +from scipy.conftest import array_api_compatible +from scipy._lib._array_api import ( + _GLOBAL_CONFIG, array_namespace, _asarray, xp_copy, xp_assert_equal, is_numpy, + np_compat, xp_default_dtype +) +from scipy._lib._array_api_no_0d import xp_assert_equal as xp_assert_equal_no_0d + +skip_xp_backends = pytest.mark.skip_xp_backends + + +@pytest.mark.skipif(not _GLOBAL_CONFIG["SCIPY_ARRAY_API"], + reason="Array API test; set environment variable SCIPY_ARRAY_API=1 to run it") +class TestArrayAPI: + + def test_array_namespace(self): + x, y = np.array([0, 1, 2]), np.array([0, 1, 2]) + xp = array_namespace(x, y) + assert 'array_api_compat.numpy' in xp.__name__ + + _GLOBAL_CONFIG["SCIPY_ARRAY_API"] = False + xp = array_namespace(x, y) + assert 'array_api_compat.numpy' in xp.__name__ + _GLOBAL_CONFIG["SCIPY_ARRAY_API"] = True + + @array_api_compatible + def test_asarray(self, xp): + x, y = _asarray([0, 1, 2], xp=xp), _asarray(np.arange(3), xp=xp) + ref = xp.asarray([0, 1, 2]) + xp_assert_equal(x, ref) + xp_assert_equal(y, ref) + + @pytest.mark.filterwarnings("ignore: the matrix subclass") + def test_raises(self): + msg = "of type `numpy.ma.MaskedArray` are not supported" + with pytest.raises(TypeError, match=msg): + array_namespace(np.ma.array(1), np.array(1)) + + msg = "of type `numpy.matrix` are not supported" + with pytest.raises(TypeError, match=msg): + array_namespace(np.array(1), np.matrix(1)) + + msg = "only boolean and numerical dtypes are supported" + with pytest.raises(TypeError, match=msg): + array_namespace([object()]) + with pytest.raises(TypeError, match=msg): + array_namespace('abc') + + def test_array_likes(self): + # should be no exceptions + array_namespace([0, 1, 2]) + array_namespace(1, 2, 3) + array_namespace(1) + + @skip_xp_backends('jax.numpy', + reason="JAX arrays do not support item assignment") + @pytest.mark.usefixtures("skip_xp_backends") + @array_api_compatible + def test_copy(self, xp): + for _xp in [xp, None]: + x = xp.asarray([1, 2, 3]) + y = xp_copy(x, xp=_xp) + # with numpy we'd want to use np.shared_memory, but that's not specified + # in the array-api + x[0] = 10 + x[1] = 11 + x[2] = 12 + + assert x[0] != y[0] + assert x[1] != y[1] + assert x[2] != y[2] + assert id(x) != id(y) + + @array_api_compatible + @pytest.mark.parametrize('dtype', ['int32', 'int64', 'float32', 'float64']) + @pytest.mark.parametrize('shape', [(), (3,)]) + def test_strict_checks(self, xp, dtype, shape): + # Check that `_strict_check` behaves as expected + dtype = getattr(xp, dtype) + x = xp.broadcast_to(xp.asarray(1, dtype=dtype), shape) + x = x if shape else x[()] + y = np_compat.asarray(1)[()] + + kwarg_names = ["check_namespace", "check_dtype", "check_shape", "check_0d"] + options = dict(zip(kwarg_names, [True, False, False, False])) + if xp == np: + xp_assert_equal(x, y, **options) + else: + with pytest.raises(AssertionError, match="Namespaces do not match."): + xp_assert_equal(x, y, **options) + + options = dict(zip(kwarg_names, [False, True, False, False])) + if y.dtype.name in str(x.dtype): + xp_assert_equal(x, y, **options) + else: + with pytest.raises(AssertionError, match="dtypes do not match."): + xp_assert_equal(x, y, **options) + + options = dict(zip(kwarg_names, [False, False, True, False])) + if x.shape == y.shape: + xp_assert_equal(x, y, **options) + else: + with pytest.raises(AssertionError, match="Shapes do not match."): + xp_assert_equal(x, xp.asarray(y), **options) + + options = dict(zip(kwarg_names, [False, False, False, True])) + if is_numpy(xp) and x.shape == y.shape: + xp_assert_equal(x, y, **options) + elif is_numpy(xp): + with pytest.raises(AssertionError, match="Array-ness does not match."): + xp_assert_equal(x, y, **options) + + + @array_api_compatible + def test_check_scalar(self, xp): + if not is_numpy(xp): + pytest.skip("Scalars only exist in NumPy") + + # identity always passes + xp_assert_equal(xp.float64(0), xp.float64(0)) + xp_assert_equal(xp.asarray(0.), xp.asarray(0.)) + xp_assert_equal(xp.float64(0), xp.float64(0), check_0d=False) + xp_assert_equal(xp.asarray(0.), xp.asarray(0.), check_0d=False) + + # Check default convention: 0d-arrays are distinguished from scalars + message = "Array-ness does not match:.*" + with pytest.raises(AssertionError, match=message): + xp_assert_equal(xp.asarray(0.), xp.float64(0)) + with pytest.raises(AssertionError, match=message): + xp_assert_equal(xp.float64(0), xp.asarray(0.)) + with pytest.raises(AssertionError, match=message): + xp_assert_equal(xp.asarray(42), xp.int64(42)) + with pytest.raises(AssertionError, match=message): + xp_assert_equal(xp.int64(42), xp.asarray(42)) + + # with `check_0d=False`, scalars-vs-0d passes (if values match) + xp_assert_equal(xp.asarray(0.), xp.float64(0), check_0d=False) + xp_assert_equal(xp.float64(0), xp.asarray(0.), check_0d=False) + # also with regular python objects + xp_assert_equal(xp.asarray(0.), 0., check_0d=False) + xp_assert_equal(0., xp.asarray(0.), check_0d=False) + xp_assert_equal(xp.asarray(42), 42, check_0d=False) + xp_assert_equal(42, xp.asarray(42), check_0d=False) + + # as an alternative to `check_0d=False`, explicitly expect scalar + xp_assert_equal(xp.float64(0), xp.asarray(0.)[()]) + + + @array_api_compatible + def test_check_scalar_no_0d(self, xp): + if not is_numpy(xp): + pytest.skip("Scalars only exist in NumPy") + + # identity passes, if first argument is not 0d (or check_0d=True) + xp_assert_equal_no_0d(xp.float64(0), xp.float64(0)) + xp_assert_equal_no_0d(xp.float64(0), xp.float64(0), check_0d=True) + xp_assert_equal_no_0d(xp.asarray(0.), xp.asarray(0.), check_0d=True) + + # by default, 0d values are forbidden as the first argument + message = "Result is a NumPy 0d-array.*" + with pytest.raises(AssertionError, match=message): + xp_assert_equal_no_0d(xp.asarray(0.), xp.asarray(0.)) + with pytest.raises(AssertionError, match=message): + xp_assert_equal_no_0d(xp.asarray(0.), xp.float64(0)) + with pytest.raises(AssertionError, match=message): + xp_assert_equal_no_0d(xp.asarray(42), xp.int64(42)) + + # Check default convention: 0d-arrays are NOT distinguished from scalars + xp_assert_equal_no_0d(xp.float64(0), xp.asarray(0.)) + xp_assert_equal_no_0d(xp.int64(42), xp.asarray(42)) + + # opt in to 0d-check remains possible + message = "Array-ness does not match:.*" + with pytest.raises(AssertionError, match=message): + xp_assert_equal_no_0d(xp.asarray(0.), xp.float64(0), check_0d=True) + with pytest.raises(AssertionError, match=message): + xp_assert_equal_no_0d(xp.float64(0), xp.asarray(0.), check_0d=True) + with pytest.raises(AssertionError, match=message): + xp_assert_equal_no_0d(xp.asarray(42), xp.int64(0), check_0d=True) + with pytest.raises(AssertionError, match=message): + xp_assert_equal_no_0d(xp.int64(0), xp.asarray(42), check_0d=True) + + # scalars-vs-0d passes (if values match) also with regular python objects + xp_assert_equal_no_0d(0., xp.asarray(0.)) + xp_assert_equal_no_0d(42, xp.asarray(42)) + + @array_api_compatible + def test_default_dtype(self, xp): + assert xp_default_dtype(xp) == xp.asarray(1.).dtype diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_bunch.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_bunch.py new file mode 100644 index 0000000000000000000000000000000000000000..f19ca377129b925cad732dd25bf3089c646f923f --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_bunch.py @@ -0,0 +1,162 @@ +import pytest +import pickle +from numpy.testing import assert_equal +from scipy._lib._bunch import _make_tuple_bunch + + +# `Result` is defined at the top level of the module so it can be +# used to test pickling. +Result = _make_tuple_bunch('Result', ['x', 'y', 'z'], ['w', 'beta']) + + +class TestMakeTupleBunch: + + # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + # Tests with Result + # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + def setup_method(self): + # Set up an instance of Result. + self.result = Result(x=1, y=2, z=3, w=99, beta=0.5) + + def test_attribute_access(self): + assert_equal(self.result.x, 1) + assert_equal(self.result.y, 2) + assert_equal(self.result.z, 3) + assert_equal(self.result.w, 99) + assert_equal(self.result.beta, 0.5) + + def test_indexing(self): + assert_equal(self.result[0], 1) + assert_equal(self.result[1], 2) + assert_equal(self.result[2], 3) + assert_equal(self.result[-1], 3) + with pytest.raises(IndexError, match='index out of range'): + self.result[3] + + def test_unpacking(self): + x0, y0, z0 = self.result + assert_equal((x0, y0, z0), (1, 2, 3)) + assert_equal(self.result, (1, 2, 3)) + + def test_slice(self): + assert_equal(self.result[1:], (2, 3)) + assert_equal(self.result[::2], (1, 3)) + assert_equal(self.result[::-1], (3, 2, 1)) + + def test_len(self): + assert_equal(len(self.result), 3) + + def test_repr(self): + s = repr(self.result) + assert_equal(s, 'Result(x=1, y=2, z=3, w=99, beta=0.5)') + + def test_hash(self): + assert_equal(hash(self.result), hash((1, 2, 3))) + + def test_pickle(self): + s = pickle.dumps(self.result) + obj = pickle.loads(s) + assert isinstance(obj, Result) + assert_equal(obj.x, self.result.x) + assert_equal(obj.y, self.result.y) + assert_equal(obj.z, self.result.z) + assert_equal(obj.w, self.result.w) + assert_equal(obj.beta, self.result.beta) + + def test_read_only_existing(self): + with pytest.raises(AttributeError, match="can't set attribute"): + self.result.x = -1 + + def test_read_only_new(self): + self.result.plate_of_shrimp = "lattice of coincidence" + assert self.result.plate_of_shrimp == "lattice of coincidence" + + def test_constructor_missing_parameter(self): + with pytest.raises(TypeError, match='missing'): + # `w` is missing. + Result(x=1, y=2, z=3, beta=0.75) + + def test_constructor_incorrect_parameter(self): + with pytest.raises(TypeError, match='unexpected'): + # `foo` is not an existing field. + Result(x=1, y=2, z=3, w=123, beta=0.75, foo=999) + + def test_module(self): + m = 'scipy._lib.tests.test_bunch' + assert_equal(Result.__module__, m) + assert_equal(self.result.__module__, m) + + def test_extra_fields_per_instance(self): + # This test exists to ensure that instances of the same class + # store their own values for the extra fields. That is, the values + # are stored per instance and not in the class. + result1 = Result(x=1, y=2, z=3, w=-1, beta=0.0) + result2 = Result(x=4, y=5, z=6, w=99, beta=1.0) + assert_equal(result1.w, -1) + assert_equal(result1.beta, 0.0) + # The rest of these checks aren't essential, but let's check + # them anyway. + assert_equal(result1[:], (1, 2, 3)) + assert_equal(result2.w, 99) + assert_equal(result2.beta, 1.0) + assert_equal(result2[:], (4, 5, 6)) + + # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + # Other tests + # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + def test_extra_field_names_is_optional(self): + Square = _make_tuple_bunch('Square', ['width', 'height']) + sq = Square(width=1, height=2) + assert_equal(sq.width, 1) + assert_equal(sq.height, 2) + s = repr(sq) + assert_equal(s, 'Square(width=1, height=2)') + + def test_tuple_like(self): + Tup = _make_tuple_bunch('Tup', ['a', 'b']) + tu = Tup(a=1, b=2) + assert isinstance(tu, tuple) + assert isinstance(tu + (1,), tuple) + + def test_explicit_module(self): + m = 'some.module.name' + Foo = _make_tuple_bunch('Foo', ['x'], ['a', 'b'], module=m) + foo = Foo(x=1, a=355, b=113) + assert_equal(Foo.__module__, m) + assert_equal(foo.__module__, m) + + # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + # Argument validation + # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + @pytest.mark.parametrize('args', [('123', ['a'], ['b']), + ('Foo', ['-3'], ['x']), + ('Foo', ['a'], ['+-*/'])]) + def test_identifiers_not_allowed(self, args): + with pytest.raises(ValueError, match='identifiers'): + _make_tuple_bunch(*args) + + @pytest.mark.parametrize('args', [('Foo', ['a', 'b', 'a'], ['x']), + ('Foo', ['a', 'b'], ['b', 'x'])]) + def test_repeated_field_names(self, args): + with pytest.raises(ValueError, match='Duplicate'): + _make_tuple_bunch(*args) + + @pytest.mark.parametrize('args', [('Foo', ['_a'], ['x']), + ('Foo', ['a'], ['_x'])]) + def test_leading_underscore_not_allowed(self, args): + with pytest.raises(ValueError, match='underscore'): + _make_tuple_bunch(*args) + + @pytest.mark.parametrize('args', [('Foo', ['def'], ['x']), + ('Foo', ['a'], ['or']), + ('and', ['a'], ['x'])]) + def test_keyword_not_allowed_in_fields(self, args): + with pytest.raises(ValueError, match='keyword'): + _make_tuple_bunch(*args) + + def test_at_least_one_field_name_required(self): + with pytest.raises(ValueError, match='at least one name'): + _make_tuple_bunch('Qwerty', [], ['a', 'b']) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_ccallback.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_ccallback.py new file mode 100644 index 0000000000000000000000000000000000000000..82021775c294c7b881b9458b57d16deaac483cc7 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_ccallback.py @@ -0,0 +1,204 @@ +from numpy.testing import assert_equal, assert_ +from pytest import raises as assert_raises + +import time +import pytest +import ctypes +import threading +from scipy._lib import _ccallback_c as _test_ccallback_cython +from scipy._lib import _test_ccallback +from scipy._lib._ccallback import LowLevelCallable + +try: + import cffi + HAVE_CFFI = True +except ImportError: + HAVE_CFFI = False + + +ERROR_VALUE = 2.0 + + +def callback_python(a, user_data=None): + if a == ERROR_VALUE: + raise ValueError("bad value") + + if user_data is None: + return a + 1 + else: + return a + user_data + +def _get_cffi_func(base, signature): + if not HAVE_CFFI: + pytest.skip("cffi not installed") + + # Get function address + voidp = ctypes.cast(base, ctypes.c_void_p) + address = voidp.value + + # Create corresponding cffi handle + ffi = cffi.FFI() + func = ffi.cast(signature, address) + return func + + +def _get_ctypes_data(): + value = ctypes.c_double(2.0) + return ctypes.cast(ctypes.pointer(value), ctypes.c_voidp) + + +def _get_cffi_data(): + if not HAVE_CFFI: + pytest.skip("cffi not installed") + ffi = cffi.FFI() + return ffi.new('double *', 2.0) + + +CALLERS = { + 'simple': _test_ccallback.test_call_simple, + 'nodata': _test_ccallback.test_call_nodata, + 'nonlocal': _test_ccallback.test_call_nonlocal, + 'cython': _test_ccallback_cython.test_call_cython, +} + +# These functions have signatures known to the callers +FUNCS = { + 'python': lambda: callback_python, + 'capsule': lambda: _test_ccallback.test_get_plus1_capsule(), + 'cython': lambda: LowLevelCallable.from_cython(_test_ccallback_cython, + "plus1_cython"), + 'ctypes': lambda: _test_ccallback_cython.plus1_ctypes, + 'cffi': lambda: _get_cffi_func(_test_ccallback_cython.plus1_ctypes, + 'double (*)(double, int *, void *)'), + 'capsule_b': lambda: _test_ccallback.test_get_plus1b_capsule(), + 'cython_b': lambda: LowLevelCallable.from_cython(_test_ccallback_cython, + "plus1b_cython"), + 'ctypes_b': lambda: _test_ccallback_cython.plus1b_ctypes, + 'cffi_b': lambda: _get_cffi_func(_test_ccallback_cython.plus1b_ctypes, + 'double (*)(double, double, int *, void *)'), +} + +# These functions have signatures the callers don't know +BAD_FUNCS = { + 'capsule_bc': lambda: _test_ccallback.test_get_plus1bc_capsule(), + 'cython_bc': lambda: LowLevelCallable.from_cython(_test_ccallback_cython, + "plus1bc_cython"), + 'ctypes_bc': lambda: _test_ccallback_cython.plus1bc_ctypes, + 'cffi_bc': lambda: _get_cffi_func( + _test_ccallback_cython.plus1bc_ctypes, + 'double (*)(double, double, double, int *, void *)' + ), +} + +USER_DATAS = { + 'ctypes': _get_ctypes_data, + 'cffi': _get_cffi_data, + 'capsule': _test_ccallback.test_get_data_capsule, +} + + +def test_callbacks(): + def check(caller, func, user_data): + caller = CALLERS[caller] + func = FUNCS[func]() + user_data = USER_DATAS[user_data]() + + if func is callback_python: + def func2(x): + return func(x, 2.0) + else: + func2 = LowLevelCallable(func, user_data) + func = LowLevelCallable(func) + + # Test basic call + assert_equal(caller(func, 1.0), 2.0) + + # Test 'bad' value resulting to an error + assert_raises(ValueError, caller, func, ERROR_VALUE) + + # Test passing in user_data + assert_equal(caller(func2, 1.0), 3.0) + + for caller in sorted(CALLERS.keys()): + for func in sorted(FUNCS.keys()): + for user_data in sorted(USER_DATAS.keys()): + check(caller, func, user_data) + + +def test_bad_callbacks(): + def check(caller, func, user_data): + caller = CALLERS[caller] + user_data = USER_DATAS[user_data]() + func = BAD_FUNCS[func]() + + if func is callback_python: + def func2(x): + return func(x, 2.0) + else: + func2 = LowLevelCallable(func, user_data) + func = LowLevelCallable(func) + + # Test that basic call fails + assert_raises(ValueError, caller, LowLevelCallable(func), 1.0) + + # Test that passing in user_data also fails + assert_raises(ValueError, caller, func2, 1.0) + + # Test error message + llfunc = LowLevelCallable(func) + try: + caller(llfunc, 1.0) + except ValueError as err: + msg = str(err) + assert_(llfunc.signature in msg, msg) + assert_('double (double, double, int *, void *)' in msg, msg) + + for caller in sorted(CALLERS.keys()): + for func in sorted(BAD_FUNCS.keys()): + for user_data in sorted(USER_DATAS.keys()): + check(caller, func, user_data) + + +def test_signature_override(): + caller = _test_ccallback.test_call_simple + func = _test_ccallback.test_get_plus1_capsule() + + llcallable = LowLevelCallable(func, signature="bad signature") + assert_equal(llcallable.signature, "bad signature") + assert_raises(ValueError, caller, llcallable, 3) + + llcallable = LowLevelCallable(func, signature="double (double, int *, void *)") + assert_equal(llcallable.signature, "double (double, int *, void *)") + assert_equal(caller(llcallable, 3), 4) + + +def test_threadsafety(): + def callback(a, caller): + if a <= 0: + return 1 + else: + res = caller(lambda x: callback(x, caller), a - 1) + return 2*res + + def check(caller): + caller = CALLERS[caller] + + results = [] + + count = 10 + + def run(): + time.sleep(0.01) + r = caller(lambda x: callback(x, caller), count) + results.append(r) + + threads = [threading.Thread(target=run) for j in range(20)] + for thread in threads: + thread.start() + for thread in threads: + thread.join() + + assert_equal(results, [2.0**count]*len(threads)) + + for caller in CALLERS.keys(): + check(caller) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_config.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_config.py new file mode 100644 index 0000000000000000000000000000000000000000..794e365c0d8a5ce337765fc669d688e80240d540 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_config.py @@ -0,0 +1,45 @@ +""" +Check the SciPy config is valid. +""" +import scipy +import pytest +from unittest.mock import patch + +pytestmark = pytest.mark.skipif( + not hasattr(scipy.__config__, "_built_with_meson"), + reason="Requires Meson builds", +) + + +class TestSciPyConfigs: + REQUIRED_CONFIG_KEYS = [ + "Compilers", + "Machine Information", + "Python Information", + ] + + @pytest.mark.thread_unsafe + @patch("scipy.__config__._check_pyyaml") + def test_pyyaml_not_found(self, mock_yaml_importer): + mock_yaml_importer.side_effect = ModuleNotFoundError() + with pytest.warns(UserWarning): + scipy.show_config() + + def test_dict_mode(self): + config = scipy.show_config(mode="dicts") + + assert isinstance(config, dict) + assert all([key in config for key in self.REQUIRED_CONFIG_KEYS]), ( + "Required key missing," + " see index of `False` with `REQUIRED_CONFIG_KEYS`" + ) + + def test_invalid_mode(self): + with pytest.raises(AttributeError): + scipy.show_config(mode="foo") + + def test_warn_to_add_tests(self): + assert len(scipy.__config__.DisplayModes) == 2, ( + "New mode detected," + " please add UT if applicable and increment this count" + ) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_deprecation.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_deprecation.py new file mode 100644 index 0000000000000000000000000000000000000000..667e6ab94346fc8b22c0ea4d4624acf33124c8c0 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_deprecation.py @@ -0,0 +1,10 @@ +import pytest + +@pytest.mark.thread_unsafe +def test_cython_api_deprecation(): + match = ("`scipy._lib._test_deprecation_def.foo_deprecated` " + "is deprecated, use `foo` instead!\n" + "Deprecated in Scipy 42.0.0") + with pytest.warns(DeprecationWarning, match=match): + from .. import _test_deprecation_call + assert _test_deprecation_call.call() == (1, 1) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_doccer.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_doccer.py new file mode 100644 index 0000000000000000000000000000000000000000..176a69698b10bd1d0d23fc57f8e8a99ce7209f0f --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_doccer.py @@ -0,0 +1,143 @@ +''' Some tests for the documenting decorator and support functions ''' + +import sys +import pytest +from numpy.testing import assert_equal, suppress_warnings + +from scipy._lib import doccer + +# python -OO strips docstrings +DOCSTRINGS_STRIPPED = sys.flags.optimize > 1 + +docstring = \ +"""Docstring + %(strtest1)s + %(strtest2)s + %(strtest3)s +""" +param_doc1 = \ +"""Another test + with some indent""" + +param_doc2 = \ +"""Another test, one line""" + +param_doc3 = \ +""" Another test + with some indent""" + +doc_dict = {'strtest1':param_doc1, + 'strtest2':param_doc2, + 'strtest3':param_doc3} + +filled_docstring = \ +"""Docstring + Another test + with some indent + Another test, one line + Another test + with some indent +""" + + +def test_unindent(): + with suppress_warnings() as sup: + sup.filter(category=DeprecationWarning) + assert_equal(doccer.unindent_string(param_doc1), param_doc1) + assert_equal(doccer.unindent_string(param_doc2), param_doc2) + assert_equal(doccer.unindent_string(param_doc3), param_doc1) + + +def test_unindent_dict(): + with suppress_warnings() as sup: + sup.filter(category=DeprecationWarning) + d2 = doccer.unindent_dict(doc_dict) + assert_equal(d2['strtest1'], doc_dict['strtest1']) + assert_equal(d2['strtest2'], doc_dict['strtest2']) + assert_equal(d2['strtest3'], doc_dict['strtest1']) + + +def test_docformat(): + with suppress_warnings() as sup: + sup.filter(category=DeprecationWarning) + udd = doccer.unindent_dict(doc_dict) + formatted = doccer.docformat(docstring, udd) + assert_equal(formatted, filled_docstring) + single_doc = 'Single line doc %(strtest1)s' + formatted = doccer.docformat(single_doc, doc_dict) + # Note - initial indent of format string does not + # affect subsequent indent of inserted parameter + assert_equal(formatted, """Single line doc Another test + with some indent""") + + +@pytest.mark.skipif(DOCSTRINGS_STRIPPED, reason="docstrings stripped") +def test_decorator(): + with suppress_warnings() as sup: + sup.filter(category=DeprecationWarning) + # with unindentation of parameters + decorator = doccer.filldoc(doc_dict, True) + + @decorator + def func(): + """ Docstring + %(strtest3)s + """ + + def expected(): + """ Docstring + Another test + with some indent + """ + assert_equal(func.__doc__, expected.__doc__) + + # without unindentation of parameters + + # The docstring should be unindented for Python 3.13+ + # because of https://github.com/python/cpython/issues/81283 + decorator = doccer.filldoc(doc_dict, False if \ + sys.version_info < (3, 13) else True) + + @decorator + def func(): + """ Docstring + %(strtest3)s + """ + def expected(): + """ Docstring + Another test + with some indent + """ + assert_equal(func.__doc__, expected.__doc__) + + +@pytest.mark.skipif(DOCSTRINGS_STRIPPED, reason="docstrings stripped") +def test_inherit_docstring_from(): + + with suppress_warnings() as sup: + sup.filter(category=DeprecationWarning) + + class Foo: + def func(self): + '''Do something useful.''' + return + + def func2(self): + '''Something else.''' + + class Bar(Foo): + @doccer.inherit_docstring_from(Foo) + def func(self): + '''%(super)sABC''' + return + + @doccer.inherit_docstring_from(Foo) + def func2(self): + # No docstring. + return + + assert_equal(Bar.func.__doc__, Foo.func.__doc__ + 'ABC') + assert_equal(Bar.func2.__doc__, Foo.func2.__doc__) + bar = Bar() + assert_equal(bar.func.__doc__, Foo.func.__doc__ + 'ABC') + assert_equal(bar.func2.__doc__, Foo.func2.__doc__) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_import_cycles.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_import_cycles.py new file mode 100644 index 0000000000000000000000000000000000000000..3a35800a8198af8215e0b5624738f9ac45b0bb96 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_import_cycles.py @@ -0,0 +1,18 @@ +import pytest +import sys +import subprocess + +from .test_public_api import PUBLIC_MODULES + +# Regression tests for gh-6793. +# Check that all modules are importable in a new Python process. +# This is not necessarily true if there are import cycles present. + +@pytest.mark.fail_slow(40) +@pytest.mark.slow +@pytest.mark.thread_unsafe +def test_public_modules_importable(): + pids = [subprocess.Popen([sys.executable, '-c', f'import {module}']) + for module in PUBLIC_MODULES] + for i, pid in enumerate(pids): + assert pid.wait() == 0, f'Failed to import {PUBLIC_MODULES[i]}' diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_public_api.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_public_api.py new file mode 100644 index 0000000000000000000000000000000000000000..5332107cd21cdd2b6e40cc545c87138cee04ff97 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_public_api.py @@ -0,0 +1,469 @@ +""" +This test script is adopted from: + https://github.com/numpy/numpy/blob/main/numpy/tests/test_public_api.py +""" + +import pkgutil +import types +import importlib +import warnings +from importlib import import_module + +import pytest + +import numpy as np +import scipy + +from scipy.conftest import xp_available_backends + + +def test_dir_testing(): + """Assert that output of dir has only one "testing/tester" + attribute without duplicate""" + assert len(dir(scipy)) == len(set(dir(scipy))) + + +# Historically SciPy has not used leading underscores for private submodules +# much. This has resulted in lots of things that look like public modules +# (i.e. things that can be imported as `import scipy.somesubmodule.somefile`), +# but were never intended to be public. The PUBLIC_MODULES list contains +# modules that are either public because they were meant to be, or because they +# contain public functions/objects that aren't present in any other namespace +# for whatever reason and therefore should be treated as public. +PUBLIC_MODULES = ["scipy." + s for s in [ + "cluster", + "cluster.vq", + "cluster.hierarchy", + "constants", + "datasets", + "differentiate", + "fft", + "fftpack", + "integrate", + "interpolate", + "io", + "io.arff", + "io.matlab", + "io.wavfile", + "linalg", + "linalg.blas", + "linalg.cython_blas", + "linalg.lapack", + "linalg.cython_lapack", + "linalg.interpolative", + "ndimage", + "odr", + "optimize", + "optimize.elementwise", + "signal", + "signal.windows", + "sparse", + "sparse.linalg", + "sparse.csgraph", + "spatial", + "spatial.distance", + "spatial.transform", + "special", + "stats", + "stats.contingency", + "stats.distributions", + "stats.mstats", + "stats.qmc", + "stats.sampling" +]] + +# The PRIVATE_BUT_PRESENT_MODULES list contains modules that lacked underscores +# in their name and hence looked public, but weren't meant to be. All these +# namespace were deprecated in the 1.8.0 release - see "clear split between +# public and private API" in the 1.8.0 release notes. +# These private modules support will be removed in SciPy v2.0.0, as the +# deprecation messages emitted by each of these modules say. +PRIVATE_BUT_PRESENT_MODULES = [ + 'scipy.constants.codata', + 'scipy.constants.constants', + 'scipy.fftpack.basic', + 'scipy.fftpack.convolve', + 'scipy.fftpack.helper', + 'scipy.fftpack.pseudo_diffs', + 'scipy.fftpack.realtransforms', + 'scipy.integrate.dop', + 'scipy.integrate.lsoda', + 'scipy.integrate.odepack', + 'scipy.integrate.quadpack', + 'scipy.integrate.vode', + 'scipy.interpolate.dfitpack', + 'scipy.interpolate.fitpack', + 'scipy.interpolate.fitpack2', + 'scipy.interpolate.interpnd', + 'scipy.interpolate.interpolate', + 'scipy.interpolate.ndgriddata', + 'scipy.interpolate.polyint', + 'scipy.interpolate.rbf', + 'scipy.io.arff.arffread', + 'scipy.io.harwell_boeing', + 'scipy.io.idl', + 'scipy.io.matlab.byteordercodes', + 'scipy.io.matlab.mio', + 'scipy.io.matlab.mio4', + 'scipy.io.matlab.mio5', + 'scipy.io.matlab.mio5_params', + 'scipy.io.matlab.mio5_utils', + 'scipy.io.matlab.mio_utils', + 'scipy.io.matlab.miobase', + 'scipy.io.matlab.streams', + 'scipy.io.mmio', + 'scipy.io.netcdf', + 'scipy.linalg.basic', + 'scipy.linalg.decomp', + 'scipy.linalg.decomp_cholesky', + 'scipy.linalg.decomp_lu', + 'scipy.linalg.decomp_qr', + 'scipy.linalg.decomp_schur', + 'scipy.linalg.decomp_svd', + 'scipy.linalg.matfuncs', + 'scipy.linalg.misc', + 'scipy.linalg.special_matrices', + 'scipy.misc', + 'scipy.misc.common', + 'scipy.misc.doccer', + 'scipy.ndimage.filters', + 'scipy.ndimage.fourier', + 'scipy.ndimage.interpolation', + 'scipy.ndimage.measurements', + 'scipy.ndimage.morphology', + 'scipy.odr.models', + 'scipy.odr.odrpack', + 'scipy.optimize.cobyla', + 'scipy.optimize.cython_optimize', + 'scipy.optimize.lbfgsb', + 'scipy.optimize.linesearch', + 'scipy.optimize.minpack', + 'scipy.optimize.minpack2', + 'scipy.optimize.moduleTNC', + 'scipy.optimize.nonlin', + 'scipy.optimize.optimize', + 'scipy.optimize.slsqp', + 'scipy.optimize.tnc', + 'scipy.optimize.zeros', + 'scipy.signal.bsplines', + 'scipy.signal.filter_design', + 'scipy.signal.fir_filter_design', + 'scipy.signal.lti_conversion', + 'scipy.signal.ltisys', + 'scipy.signal.signaltools', + 'scipy.signal.spectral', + 'scipy.signal.spline', + 'scipy.signal.waveforms', + 'scipy.signal.wavelets', + 'scipy.signal.windows.windows', + 'scipy.sparse.base', + 'scipy.sparse.bsr', + 'scipy.sparse.compressed', + 'scipy.sparse.construct', + 'scipy.sparse.coo', + 'scipy.sparse.csc', + 'scipy.sparse.csr', + 'scipy.sparse.data', + 'scipy.sparse.dia', + 'scipy.sparse.dok', + 'scipy.sparse.extract', + 'scipy.sparse.lil', + 'scipy.sparse.linalg.dsolve', + 'scipy.sparse.linalg.eigen', + 'scipy.sparse.linalg.interface', + 'scipy.sparse.linalg.isolve', + 'scipy.sparse.linalg.matfuncs', + 'scipy.sparse.sparsetools', + 'scipy.sparse.spfuncs', + 'scipy.sparse.sputils', + 'scipy.spatial.ckdtree', + 'scipy.spatial.kdtree', + 'scipy.spatial.qhull', + 'scipy.spatial.transform.rotation', + 'scipy.special.add_newdocs', + 'scipy.special.basic', + 'scipy.special.cython_special', + 'scipy.special.orthogonal', + 'scipy.special.sf_error', + 'scipy.special.specfun', + 'scipy.special.spfun_stats', + 'scipy.stats.biasedurn', + 'scipy.stats.kde', + 'scipy.stats.morestats', + 'scipy.stats.mstats_basic', + 'scipy.stats.mstats_extras', + 'scipy.stats.mvn', + 'scipy.stats.stats', +] + + +def is_unexpected(name): + """Check if this needs to be considered.""" + if '._' in name or '.tests' in name or '.setup' in name: + return False + + if name in PUBLIC_MODULES: + return False + + if name in PRIVATE_BUT_PRESENT_MODULES: + return False + + return True + + +SKIP_LIST = [ + 'scipy.conftest', + 'scipy.version', + 'scipy.special.libsf_error_state' +] + + +# XXX: this test does more than it says on the tin - in using `pkgutil.walk_packages`, +# it will raise if it encounters any exceptions which are not handled by `ignore_errors` +# while attempting to import each discovered package. +# For now, `ignore_errors` only ignores what is necessary, but this could be expanded - +# for example, to all errors from private modules or git subpackages - if desired. +@pytest.mark.thread_unsafe +def test_all_modules_are_expected(): + """ + Test that we don't add anything that looks like a new public module by + accident. Check is based on filenames. + """ + + def ignore_errors(name): + # if versions of other array libraries are installed which are incompatible + # with the installed NumPy version, there can be errors on importing + # `array_api_compat`. This should only raise if SciPy is configured with + # that library as an available backend. + backends = {'cupy', 'torch', 'dask.array'} + for backend in backends: + path = f'array_api_compat.{backend}' + if path in name and backend not in xp_available_backends: + return + raise + + modnames = [] + + with np.testing.suppress_warnings() as sup: + sup.filter(DeprecationWarning,"scipy.misc") + for _, modname, _ in pkgutil.walk_packages(path=scipy.__path__, + prefix=scipy.__name__ + '.', + onerror=ignore_errors): + if is_unexpected(modname) and modname not in SKIP_LIST: + # We have a name that is new. If that's on purpose, add it to + # PUBLIC_MODULES. We don't expect to have to add anything to + # PRIVATE_BUT_PRESENT_MODULES. Use an underscore in the name! + modnames.append(modname) + + if modnames: + raise AssertionError(f'Found unexpected modules: {modnames}') + + +# Stuff that clearly shouldn't be in the API and is detected by the next test +# below +SKIP_LIST_2 = [ + 'scipy.char', + 'scipy.rec', + 'scipy.emath', + 'scipy.math', + 'scipy.random', + 'scipy.ctypeslib', + 'scipy.ma' +] + + +def test_all_modules_are_expected_2(): + """ + Method checking all objects. The pkgutil-based method in + `test_all_modules_are_expected` does not catch imports into a namespace, + only filenames. + """ + + def find_unexpected_members(mod_name): + members = [] + module = importlib.import_module(mod_name) + if hasattr(module, '__all__'): + objnames = module.__all__ + else: + objnames = dir(module) + + for objname in objnames: + if not objname.startswith('_'): + fullobjname = mod_name + '.' + objname + if isinstance(getattr(module, objname), types.ModuleType): + if is_unexpected(fullobjname) and fullobjname not in SKIP_LIST_2: + members.append(fullobjname) + + return members + with np.testing.suppress_warnings() as sup: + sup.filter(DeprecationWarning, "scipy.misc") + unexpected_members = find_unexpected_members("scipy") + + for modname in PUBLIC_MODULES: + unexpected_members.extend(find_unexpected_members(modname)) + + if unexpected_members: + raise AssertionError("Found unexpected object(s) that look like " + f"modules: {unexpected_members}") + + +def test_api_importable(): + """ + Check that all submodules listed higher up in this file can be imported + Note that if a PRIVATE_BUT_PRESENT_MODULES entry goes missing, it may + simply need to be removed from the list (deprecation may or may not be + needed - apply common sense). + """ + def check_importable(module_name): + try: + importlib.import_module(module_name) + except (ImportError, AttributeError): + return False + + return True + + module_names = [] + for module_name in PUBLIC_MODULES: + if not check_importable(module_name): + module_names.append(module_name) + + if module_names: + raise AssertionError("Modules in the public API that cannot be " + f"imported: {module_names}") + + with warnings.catch_warnings(record=True): + warnings.filterwarnings('always', category=DeprecationWarning) + warnings.filterwarnings('always', category=ImportWarning) + for module_name in PRIVATE_BUT_PRESENT_MODULES: + if not check_importable(module_name): + module_names.append(module_name) + + if module_names: + raise AssertionError("Modules that are not really public but looked " + "public and can not be imported: " + f"{module_names}") + + +@pytest.mark.thread_unsafe +@pytest.mark.parametrize(("module_name", "correct_module"), + [('scipy.constants.codata', None), + ('scipy.constants.constants', None), + ('scipy.fftpack.basic', None), + ('scipy.fftpack.helper', None), + ('scipy.fftpack.pseudo_diffs', None), + ('scipy.fftpack.realtransforms', None), + ('scipy.integrate.dop', None), + ('scipy.integrate.lsoda', None), + ('scipy.integrate.odepack', None), + ('scipy.integrate.quadpack', None), + ('scipy.integrate.vode', None), + ('scipy.interpolate.fitpack', None), + ('scipy.interpolate.fitpack2', None), + ('scipy.interpolate.interpolate', None), + ('scipy.interpolate.ndgriddata', None), + ('scipy.interpolate.polyint', None), + ('scipy.interpolate.rbf', None), + ('scipy.io.harwell_boeing', None), + ('scipy.io.idl', None), + ('scipy.io.mmio', None), + ('scipy.io.netcdf', None), + ('scipy.io.arff.arffread', 'arff'), + ('scipy.io.matlab.byteordercodes', 'matlab'), + ('scipy.io.matlab.mio_utils', 'matlab'), + ('scipy.io.matlab.mio', 'matlab'), + ('scipy.io.matlab.mio4', 'matlab'), + ('scipy.io.matlab.mio5_params', 'matlab'), + ('scipy.io.matlab.mio5_utils', 'matlab'), + ('scipy.io.matlab.mio5', 'matlab'), + ('scipy.io.matlab.miobase', 'matlab'), + ('scipy.io.matlab.streams', 'matlab'), + ('scipy.linalg.basic', None), + ('scipy.linalg.decomp', None), + ('scipy.linalg.decomp_cholesky', None), + ('scipy.linalg.decomp_lu', None), + ('scipy.linalg.decomp_qr', None), + ('scipy.linalg.decomp_schur', None), + ('scipy.linalg.decomp_svd', None), + ('scipy.linalg.matfuncs', None), + ('scipy.linalg.misc', None), + ('scipy.linalg.special_matrices', None), + ('scipy.ndimage.filters', None), + ('scipy.ndimage.fourier', None), + ('scipy.ndimage.interpolation', None), + ('scipy.ndimage.measurements', None), + ('scipy.ndimage.morphology', None), + ('scipy.odr.models', None), + ('scipy.odr.odrpack', None), + ('scipy.optimize.cobyla', None), + ('scipy.optimize.lbfgsb', None), + ('scipy.optimize.linesearch', None), + ('scipy.optimize.minpack', None), + ('scipy.optimize.minpack2', None), + ('scipy.optimize.moduleTNC', None), + ('scipy.optimize.nonlin', None), + ('scipy.optimize.optimize', None), + ('scipy.optimize.slsqp', None), + ('scipy.optimize.tnc', None), + ('scipy.optimize.zeros', None), + ('scipy.signal.bsplines', None), + ('scipy.signal.filter_design', None), + ('scipy.signal.fir_filter_design', None), + ('scipy.signal.lti_conversion', None), + ('scipy.signal.ltisys', None), + ('scipy.signal.signaltools', None), + ('scipy.signal.spectral', None), + ('scipy.signal.waveforms', None), + ('scipy.signal.wavelets', None), + ('scipy.signal.windows.windows', 'windows'), + ('scipy.sparse.lil', None), + ('scipy.sparse.linalg.dsolve', 'linalg'), + ('scipy.sparse.linalg.eigen', 'linalg'), + ('scipy.sparse.linalg.interface', 'linalg'), + ('scipy.sparse.linalg.isolve', 'linalg'), + ('scipy.sparse.linalg.matfuncs', 'linalg'), + ('scipy.sparse.sparsetools', None), + ('scipy.sparse.spfuncs', None), + ('scipy.sparse.sputils', None), + ('scipy.spatial.ckdtree', None), + ('scipy.spatial.kdtree', None), + ('scipy.spatial.qhull', None), + ('scipy.spatial.transform.rotation', 'transform'), + ('scipy.special.add_newdocs', None), + ('scipy.special.basic', None), + ('scipy.special.orthogonal', None), + ('scipy.special.sf_error', None), + ('scipy.special.specfun', None), + ('scipy.special.spfun_stats', None), + ('scipy.stats.biasedurn', None), + ('scipy.stats.kde', None), + ('scipy.stats.morestats', None), + ('scipy.stats.mstats_basic', 'mstats'), + ('scipy.stats.mstats_extras', 'mstats'), + ('scipy.stats.mvn', None), + ('scipy.stats.stats', None)]) +def test_private_but_present_deprecation(module_name, correct_module): + # gh-18279, gh-17572, gh-17771 noted that deprecation warnings + # for imports from private modules + # were misleading. Check that this is resolved. + module = import_module(module_name) + if correct_module is None: + import_name = f'scipy.{module_name.split(".")[1]}' + else: + import_name = f'scipy.{module_name.split(".")[1]}.{correct_module}' + + correct_import = import_module(import_name) + + # Attributes that were formerly in `module_name` can still be imported from + # `module_name`, albeit with a deprecation warning. + for attr_name in module.__all__: + # ensure attribute is present where the warning is pointing + assert getattr(correct_import, attr_name, None) is not None + message = f"Please import `{attr_name}` from the `{import_name}`..." + with pytest.deprecated_call(match=message): + getattr(module, attr_name) + + # Attributes that were not in `module_name` get an error notifying the user + # that the attribute is not in `module_name` and that `module_name` is deprecated. + message = f"`{module_name}` is deprecated..." + with pytest.raises(AttributeError, match=message): + getattr(module, "ekki") diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_scipy_version.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_scipy_version.py new file mode 100644 index 0000000000000000000000000000000000000000..68e1a43c3fb329b6a4274ba76b53a215738da6ad --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_scipy_version.py @@ -0,0 +1,28 @@ +import re + +import scipy +import scipy.version + + +def test_valid_scipy_version(): + # Verify that the SciPy version is a valid one (no .post suffix or other + # nonsense). See NumPy issue gh-6431 for an issue caused by an invalid + # version. + version_pattern = r"^[0-9]+\.[0-9]+\.[0-9]+(|a[0-9]|b[0-9]|rc[0-9])" + dev_suffix = r"((.dev0)|(\.dev0+\+git[0-9]{8}.[0-9a-f]{7}))" + if scipy.version.release: + res = re.match(version_pattern, scipy.__version__) + else: + res = re.match(version_pattern + dev_suffix, scipy.__version__) + + assert res is not None + assert scipy.__version__ + + +def test_version_submodule_members(): + """`scipy.version` may not be quite public, but we install it. + + So check that we don't silently change its contents. + """ + for attr in ('version', 'full_version', 'short_version', 'git_revision', 'release'): + assert hasattr(scipy.version, attr) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_tmpdirs.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_tmpdirs.py new file mode 100644 index 0000000000000000000000000000000000000000..292e7ab1739e663979f9f0b9647fb2c7c95d625c --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_tmpdirs.py @@ -0,0 +1,48 @@ +""" Test tmpdirs module """ +from os import getcwd +from os.path import realpath, abspath, dirname, isfile, join as pjoin, exists + +from scipy._lib._tmpdirs import tempdir, in_tempdir, in_dir + +from numpy.testing import assert_, assert_equal + +import pytest + + +MY_PATH = abspath(__file__) +MY_DIR = dirname(MY_PATH) + + +@pytest.mark.thread_unsafe +def test_tempdir(): + with tempdir() as tmpdir: + fname = pjoin(tmpdir, 'example_file.txt') + with open(fname, "w") as fobj: + fobj.write('a string\\n') + assert_(not exists(tmpdir)) + + +@pytest.mark.thread_unsafe +def test_in_tempdir(): + my_cwd = getcwd() + with in_tempdir() as tmpdir: + with open('test.txt', "w") as f: + f.write('some text') + assert_(isfile('test.txt')) + assert_(isfile(pjoin(tmpdir, 'test.txt'))) + assert_(not exists(tmpdir)) + assert_equal(getcwd(), my_cwd) + + +@pytest.mark.thread_unsafe +def test_given_directory(): + # Test InGivenDirectory + cwd = getcwd() + with in_dir() as tmpdir: + assert_equal(tmpdir, abspath(cwd)) + assert_equal(tmpdir, abspath(getcwd())) + with in_dir(MY_DIR) as tmpdir: + assert_equal(tmpdir, MY_DIR) + assert_equal(realpath(MY_DIR), realpath(abspath(getcwd()))) + # We were deleting the given directory! Check not so now. + assert_(isfile(MY_PATH)) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_warnings.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_warnings.py new file mode 100644 index 0000000000000000000000000000000000000000..f200b1a6e9756b17c96e5b8368271bbf61878d72 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_warnings.py @@ -0,0 +1,137 @@ +""" +Tests which scan for certain occurrences in the code, they may not find +all of these occurrences but should catch almost all. This file was adapted +from NumPy. +""" + + +import os +from pathlib import Path +import ast +import tokenize + +import scipy + +import pytest + + +class ParseCall(ast.NodeVisitor): + def __init__(self): + self.ls = [] + + def visit_Attribute(self, node): + ast.NodeVisitor.generic_visit(self, node) + self.ls.append(node.attr) + + def visit_Name(self, node): + self.ls.append(node.id) + + +class FindFuncs(ast.NodeVisitor): + def __init__(self, filename): + super().__init__() + self.__filename = filename + self.bad_filters = [] + self.bad_stacklevels = [] + + def visit_Call(self, node): + p = ParseCall() + p.visit(node.func) + ast.NodeVisitor.generic_visit(self, node) + + if p.ls[-1] == 'simplefilter' or p.ls[-1] == 'filterwarnings': + # get first argument of the `args` node of the filter call + match node.args[0]: + case ast.Constant() as c: + argtext = c.value + case ast.JoinedStr() as js: + # if we get an f-string, discard the templated pieces, which + # are likely the type or specific message; we're interested + # in the action, which is less likely to use a template + argtext = "".join( + x.value for x in js.values if isinstance(x, ast.Constant) + ) + case _: + raise ValueError("unknown ast node type") + # check if filter is set to ignore + if argtext == "ignore": + self.bad_filters.append( + f"{self.__filename}:{node.lineno}") + + if p.ls[-1] == 'warn' and ( + len(p.ls) == 1 or p.ls[-2] == 'warnings'): + + if self.__filename == "_lib/tests/test_warnings.py": + # This file + return + + # See if stacklevel exists: + if len(node.args) == 3: + return + args = {kw.arg for kw in node.keywords} + if "stacklevel" not in args: + self.bad_stacklevels.append( + f"{self.__filename}:{node.lineno}") + + +@pytest.fixture(scope="session") +def warning_calls(): + # combined "ignore" and stacklevel error + base = Path(scipy.__file__).parent + + bad_filters = [] + bad_stacklevels = [] + + for path in base.rglob("*.py"): + # use tokenize to auto-detect encoding on systems where no + # default encoding is defined (e.g., LANG='C') + with tokenize.open(str(path)) as file: + tree = ast.parse(file.read(), filename=str(path)) + finder = FindFuncs(path.relative_to(base)) + finder.visit(tree) + bad_filters.extend(finder.bad_filters) + bad_stacklevels.extend(finder.bad_stacklevels) + + return bad_filters, bad_stacklevels + + +@pytest.mark.fail_slow(40) +@pytest.mark.slow +def test_warning_calls_filters(warning_calls): + bad_filters, bad_stacklevels = warning_calls + + # We try not to add filters in the code base, because those filters aren't + # thread-safe. We aim to only filter in tests with + # np.testing.suppress_warnings. However, in some cases it may prove + # necessary to filter out warnings, because we can't (easily) fix the root + # cause for them and we don't want users to see some warnings when they use + # SciPy correctly. So we list exceptions here. Add new entries only if + # there's a good reason. + allowed_filters = ( + os.path.join('datasets', '_fetchers.py'), + os.path.join('datasets', '__init__.py'), + os.path.join('optimize', '_optimize.py'), + os.path.join('optimize', '_constraints.py'), + os.path.join('optimize', '_nnls.py'), + os.path.join('signal', '_ltisys.py'), + os.path.join('sparse', '__init__.py'), # np.matrix pending-deprecation + os.path.join('special', '_basic.py'), # gh-21801 + os.path.join('stats', '_discrete_distns.py'), # gh-14901 + os.path.join('stats', '_continuous_distns.py'), + os.path.join('stats', '_binned_statistic.py'), # gh-19345 + os.path.join('stats', '_stats_py.py'), # gh-20743 + os.path.join('stats', 'tests', 'test_axis_nan_policy.py'), # gh-20694 + os.path.join('_lib', '_util.py'), # gh-19341 + os.path.join('sparse', 'linalg', '_dsolve', 'linsolve.py'), # gh-17924 + "conftest.py", + ) + bad_filters = [item for item in bad_filters if item.split(':')[0] not in + allowed_filters] + + if bad_filters: + raise AssertionError( + "warning ignore filter should not be used, instead, use\n" + "numpy.testing.suppress_warnings (in tests only);\n" + "found in:\n {}".format( + "\n ".join(bad_filters))) + diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..7b7784969a573d05cc6b98ee9066c42720156d1d --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/__init__.py @@ -0,0 +1,31 @@ +""" +========================================= +Clustering package (:mod:`scipy.cluster`) +========================================= + +.. currentmodule:: scipy.cluster + +Clustering algorithms are useful in information theory, target detection, +communications, compression, and other areas. The `vq` module only +supports vector quantization and the k-means algorithms. + +The `hierarchy` module provides functions for hierarchical and +agglomerative clustering. Its features include generating hierarchical +clusters from distance matrices, +calculating statistics on clusters, cutting linkages +to generate flat clusters, and visualizing clusters with dendrograms. + +.. toctree:: + :maxdepth: 1 + + cluster.vq + cluster.hierarchy + +""" +__all__ = ['vq', 'hierarchy'] + +from . import vq, hierarchy + +from scipy._lib._testutils import PytestTester +test = PytestTester(__name__) +del PytestTester diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/hierarchy.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/hierarchy.py new file mode 100644 index 0000000000000000000000000000000000000000..522e63d8e6f2904c73c704897f715721509c819c --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/hierarchy.py @@ -0,0 +1,4178 @@ +""" +Hierarchical clustering (:mod:`scipy.cluster.hierarchy`) +======================================================== + +.. currentmodule:: scipy.cluster.hierarchy + +These functions cut hierarchical clusterings into flat clusterings +or find the roots of the forest formed by a cut by providing the flat +cluster ids of each observation. + +.. autosummary:: + :toctree: generated/ + + fcluster + fclusterdata + leaders + +These are routines for agglomerative clustering. + +.. autosummary:: + :toctree: generated/ + + linkage + single + complete + average + weighted + centroid + median + ward + +These routines compute statistics on hierarchies. + +.. autosummary:: + :toctree: generated/ + + cophenet + from_mlab_linkage + inconsistent + maxinconsts + maxdists + maxRstat + to_mlab_linkage + +Routines for visualizing flat clusters. + +.. autosummary:: + :toctree: generated/ + + dendrogram + +These are data structures and routines for representing hierarchies as +tree objects. + +.. autosummary:: + :toctree: generated/ + + ClusterNode + leaves_list + to_tree + cut_tree + optimal_leaf_ordering + +These are predicates for checking the validity of linkage and +inconsistency matrices as well as for checking isomorphism of two +flat cluster assignments. + +.. autosummary:: + :toctree: generated/ + + is_valid_im + is_valid_linkage + is_isomorphic + is_monotonic + correspond + num_obs_linkage + +Utility routines for plotting: + +.. autosummary:: + :toctree: generated/ + + set_link_color_palette + +Utility classes: + +.. autosummary:: + :toctree: generated/ + + DisjointSet -- data structure for incremental connectivity queries + +""" +# Copyright (C) Damian Eads, 2007-2008. New BSD License. + +# hierarchy.py (derived from cluster.py, http://scipy-cluster.googlecode.com) +# +# Author: Damian Eads +# Date: September 22, 2007 +# +# Copyright (c) 2007, 2008, Damian Eads +# +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# - Redistributions of source code must retain the above +# copyright notice, this list of conditions and the +# following disclaimer. +# - Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer +# in the documentation and/or other materials provided with the +# distribution. +# - Neither the name of the author nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +import warnings +import bisect +from collections import deque + +import numpy as np +from . import _hierarchy, _optimal_leaf_ordering +import scipy.spatial.distance as distance +from scipy._lib._array_api import array_namespace, _asarray, xp_copy, is_jax +from scipy._lib._disjoint_set import DisjointSet + + +_LINKAGE_METHODS = {'single': 0, 'complete': 1, 'average': 2, 'centroid': 3, + 'median': 4, 'ward': 5, 'weighted': 6} +_EUCLIDEAN_METHODS = ('centroid', 'median', 'ward') + +__all__ = ['ClusterNode', 'DisjointSet', 'average', 'centroid', 'complete', + 'cophenet', 'correspond', 'cut_tree', 'dendrogram', 'fcluster', + 'fclusterdata', 'from_mlab_linkage', 'inconsistent', + 'is_isomorphic', 'is_monotonic', 'is_valid_im', 'is_valid_linkage', + 'leaders', 'leaves_list', 'linkage', 'maxRstat', 'maxdists', + 'maxinconsts', 'median', 'num_obs_linkage', 'optimal_leaf_ordering', + 'set_link_color_palette', 'single', 'to_mlab_linkage', 'to_tree', + 'ward', 'weighted'] + + +class ClusterWarning(UserWarning): + pass + + +def _warning(s): + warnings.warn(f'scipy.cluster: {s}', ClusterWarning, stacklevel=3) + + +def int_floor(arr, xp): + # array_api_strict is strict about not allowing `int()` on a float array. + # That's typically not needed, here it is - so explicitly convert + return int(xp.astype(xp.asarray(arr), xp.int64)) + + +def single(y): + """ + Perform single/min/nearest linkage on the condensed distance matrix ``y``. + + Parameters + ---------- + y : ndarray + The upper triangular of the distance matrix. The result of + ``pdist`` is returned in this form. + + Returns + ------- + Z : ndarray + The linkage matrix. + + See Also + -------- + linkage : for advanced creation of hierarchical clusterings. + scipy.spatial.distance.pdist : pairwise distance metrics + + Examples + -------- + >>> from scipy.cluster.hierarchy import single, fcluster + >>> from scipy.spatial.distance import pdist + + First, we need a toy dataset to play with:: + + x x x x + x x + + x x + x x x x + + >>> X = [[0, 0], [0, 1], [1, 0], + ... [0, 4], [0, 3], [1, 4], + ... [4, 0], [3, 0], [4, 1], + ... [4, 4], [3, 4], [4, 3]] + + Then, we get a condensed distance matrix from this dataset: + + >>> y = pdist(X) + + Finally, we can perform the clustering: + + >>> Z = single(y) + >>> Z + array([[ 0., 1., 1., 2.], + [ 2., 12., 1., 3.], + [ 3., 4., 1., 2.], + [ 5., 14., 1., 3.], + [ 6., 7., 1., 2.], + [ 8., 16., 1., 3.], + [ 9., 10., 1., 2.], + [11., 18., 1., 3.], + [13., 15., 2., 6.], + [17., 20., 2., 9.], + [19., 21., 2., 12.]]) + + The linkage matrix ``Z`` represents a dendrogram - see + `scipy.cluster.hierarchy.linkage` for a detailed explanation of its + contents. + + We can use `scipy.cluster.hierarchy.fcluster` to see to which cluster + each initial point would belong given a distance threshold: + + >>> fcluster(Z, 0.9, criterion='distance') + array([ 7, 8, 9, 10, 11, 12, 4, 5, 6, 1, 2, 3], dtype=int32) + >>> fcluster(Z, 1, criterion='distance') + array([3, 3, 3, 4, 4, 4, 2, 2, 2, 1, 1, 1], dtype=int32) + >>> fcluster(Z, 2, criterion='distance') + array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=int32) + + Also, `scipy.cluster.hierarchy.dendrogram` can be used to generate a + plot of the dendrogram. + """ + return linkage(y, method='single', metric='euclidean') + + +def complete(y): + """ + Perform complete/max/farthest point linkage on a condensed distance matrix. + + Parameters + ---------- + y : ndarray + The upper triangular of the distance matrix. The result of + ``pdist`` is returned in this form. + + Returns + ------- + Z : ndarray + A linkage matrix containing the hierarchical clustering. See + the `linkage` function documentation for more information + on its structure. + + See Also + -------- + linkage : for advanced creation of hierarchical clusterings. + scipy.spatial.distance.pdist : pairwise distance metrics + + Examples + -------- + >>> from scipy.cluster.hierarchy import complete, fcluster + >>> from scipy.spatial.distance import pdist + + First, we need a toy dataset to play with:: + + x x x x + x x + + x x + x x x x + + >>> X = [[0, 0], [0, 1], [1, 0], + ... [0, 4], [0, 3], [1, 4], + ... [4, 0], [3, 0], [4, 1], + ... [4, 4], [3, 4], [4, 3]] + + Then, we get a condensed distance matrix from this dataset: + + >>> y = pdist(X) + + Finally, we can perform the clustering: + + >>> Z = complete(y) + >>> Z + array([[ 0. , 1. , 1. , 2. ], + [ 3. , 4. , 1. , 2. ], + [ 6. , 7. , 1. , 2. ], + [ 9. , 10. , 1. , 2. ], + [ 2. , 12. , 1.41421356, 3. ], + [ 5. , 13. , 1.41421356, 3. ], + [ 8. , 14. , 1.41421356, 3. ], + [11. , 15. , 1.41421356, 3. ], + [16. , 17. , 4.12310563, 6. ], + [18. , 19. , 4.12310563, 6. ], + [20. , 21. , 5.65685425, 12. ]]) + + The linkage matrix ``Z`` represents a dendrogram - see + `scipy.cluster.hierarchy.linkage` for a detailed explanation of its + contents. + + We can use `scipy.cluster.hierarchy.fcluster` to see to which cluster + each initial point would belong given a distance threshold: + + >>> fcluster(Z, 0.9, criterion='distance') + array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], dtype=int32) + >>> fcluster(Z, 1.5, criterion='distance') + array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4], dtype=int32) + >>> fcluster(Z, 4.5, criterion='distance') + array([1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2], dtype=int32) + >>> fcluster(Z, 6, criterion='distance') + array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=int32) + + Also, `scipy.cluster.hierarchy.dendrogram` can be used to generate a + plot of the dendrogram. + """ + return linkage(y, method='complete', metric='euclidean') + + +def average(y): + """ + Perform average/UPGMA linkage on a condensed distance matrix. + + Parameters + ---------- + y : ndarray + The upper triangular of the distance matrix. The result of + ``pdist`` is returned in this form. + + Returns + ------- + Z : ndarray + A linkage matrix containing the hierarchical clustering. See + `linkage` for more information on its structure. + + See Also + -------- + linkage : for advanced creation of hierarchical clusterings. + scipy.spatial.distance.pdist : pairwise distance metrics + + Examples + -------- + >>> from scipy.cluster.hierarchy import average, fcluster + >>> from scipy.spatial.distance import pdist + + First, we need a toy dataset to play with:: + + x x x x + x x + + x x + x x x x + + >>> X = [[0, 0], [0, 1], [1, 0], + ... [0, 4], [0, 3], [1, 4], + ... [4, 0], [3, 0], [4, 1], + ... [4, 4], [3, 4], [4, 3]] + + Then, we get a condensed distance matrix from this dataset: + + >>> y = pdist(X) + + Finally, we can perform the clustering: + + >>> Z = average(y) + >>> Z + array([[ 0. , 1. , 1. , 2. ], + [ 3. , 4. , 1. , 2. ], + [ 6. , 7. , 1. , 2. ], + [ 9. , 10. , 1. , 2. ], + [ 2. , 12. , 1.20710678, 3. ], + [ 5. , 13. , 1.20710678, 3. ], + [ 8. , 14. , 1.20710678, 3. ], + [11. , 15. , 1.20710678, 3. ], + [16. , 17. , 3.39675184, 6. ], + [18. , 19. , 3.39675184, 6. ], + [20. , 21. , 4.09206523, 12. ]]) + + The linkage matrix ``Z`` represents a dendrogram - see + `scipy.cluster.hierarchy.linkage` for a detailed explanation of its + contents. + + We can use `scipy.cluster.hierarchy.fcluster` to see to which cluster + each initial point would belong given a distance threshold: + + >>> fcluster(Z, 0.9, criterion='distance') + array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], dtype=int32) + >>> fcluster(Z, 1.5, criterion='distance') + array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4], dtype=int32) + >>> fcluster(Z, 4, criterion='distance') + array([1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2], dtype=int32) + >>> fcluster(Z, 6, criterion='distance') + array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=int32) + + Also, `scipy.cluster.hierarchy.dendrogram` can be used to generate a + plot of the dendrogram. + + """ + return linkage(y, method='average', metric='euclidean') + + +def weighted(y): + """ + Perform weighted/WPGMA linkage on the condensed distance matrix. + + See `linkage` for more information on the return + structure and algorithm. + + Parameters + ---------- + y : ndarray + The upper triangular of the distance matrix. The result of + ``pdist`` is returned in this form. + + Returns + ------- + Z : ndarray + A linkage matrix containing the hierarchical clustering. See + `linkage` for more information on its structure. + + See Also + -------- + linkage : for advanced creation of hierarchical clusterings. + scipy.spatial.distance.pdist : pairwise distance metrics + + Examples + -------- + >>> from scipy.cluster.hierarchy import weighted, fcluster + >>> from scipy.spatial.distance import pdist + + First, we need a toy dataset to play with:: + + x x x x + x x + + x x + x x x x + + >>> X = [[0, 0], [0, 1], [1, 0], + ... [0, 4], [0, 3], [1, 4], + ... [4, 0], [3, 0], [4, 1], + ... [4, 4], [3, 4], [4, 3]] + + Then, we get a condensed distance matrix from this dataset: + + >>> y = pdist(X) + + Finally, we can perform the clustering: + + >>> Z = weighted(y) + >>> Z + array([[ 0. , 1. , 1. , 2. ], + [ 6. , 7. , 1. , 2. ], + [ 3. , 4. , 1. , 2. ], + [ 9. , 11. , 1. , 2. ], + [ 2. , 12. , 1.20710678, 3. ], + [ 8. , 13. , 1.20710678, 3. ], + [ 5. , 14. , 1.20710678, 3. ], + [10. , 15. , 1.20710678, 3. ], + [18. , 19. , 3.05595762, 6. ], + [16. , 17. , 3.32379407, 6. ], + [20. , 21. , 4.06357713, 12. ]]) + + The linkage matrix ``Z`` represents a dendrogram - see + `scipy.cluster.hierarchy.linkage` for a detailed explanation of its + contents. + + We can use `scipy.cluster.hierarchy.fcluster` to see to which cluster + each initial point would belong given a distance threshold: + + >>> fcluster(Z, 0.9, criterion='distance') + array([ 7, 8, 9, 1, 2, 3, 10, 11, 12, 4, 6, 5], dtype=int32) + >>> fcluster(Z, 1.5, criterion='distance') + array([3, 3, 3, 1, 1, 1, 4, 4, 4, 2, 2, 2], dtype=int32) + >>> fcluster(Z, 4, criterion='distance') + array([2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1], dtype=int32) + >>> fcluster(Z, 6, criterion='distance') + array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=int32) + + Also, `scipy.cluster.hierarchy.dendrogram` can be used to generate a + plot of the dendrogram. + + """ + return linkage(y, method='weighted', metric='euclidean') + + +def centroid(y): + """ + Perform centroid/UPGMC linkage. + + See `linkage` for more information on the input matrix, + return structure, and algorithm. + + The following are common calling conventions: + + 1. ``Z = centroid(y)`` + + Performs centroid/UPGMC linkage on the condensed distance + matrix ``y``. + + 2. ``Z = centroid(X)`` + + Performs centroid/UPGMC linkage on the observation matrix ``X`` + using Euclidean distance as the distance metric. + + Parameters + ---------- + y : ndarray + A condensed distance matrix. A condensed + distance matrix is a flat array containing the upper + triangular of the distance matrix. This is the form that + ``pdist`` returns. Alternatively, a collection of + m observation vectors in n dimensions may be passed as + an m by n array. + + Returns + ------- + Z : ndarray + A linkage matrix containing the hierarchical clustering. See + the `linkage` function documentation for more information + on its structure. + + See Also + -------- + linkage : for advanced creation of hierarchical clusterings. + scipy.spatial.distance.pdist : pairwise distance metrics + + Examples + -------- + >>> from scipy.cluster.hierarchy import centroid, fcluster + >>> from scipy.spatial.distance import pdist + + First, we need a toy dataset to play with:: + + x x x x + x x + + x x + x x x x + + >>> X = [[0, 0], [0, 1], [1, 0], + ... [0, 4], [0, 3], [1, 4], + ... [4, 0], [3, 0], [4, 1], + ... [4, 4], [3, 4], [4, 3]] + + Then, we get a condensed distance matrix from this dataset: + + >>> y = pdist(X) + + Finally, we can perform the clustering: + + >>> Z = centroid(y) + >>> Z + array([[ 0. , 1. , 1. , 2. ], + [ 3. , 4. , 1. , 2. ], + [ 9. , 10. , 1. , 2. ], + [ 6. , 7. , 1. , 2. ], + [ 2. , 12. , 1.11803399, 3. ], + [ 5. , 13. , 1.11803399, 3. ], + [ 8. , 15. , 1.11803399, 3. ], + [11. , 14. , 1.11803399, 3. ], + [18. , 19. , 3.33333333, 6. ], + [16. , 17. , 3.33333333, 6. ], + [20. , 21. , 3.33333333, 12. ]]) # may vary + + The linkage matrix ``Z`` represents a dendrogram - see + `scipy.cluster.hierarchy.linkage` for a detailed explanation of its + contents. + + We can use `scipy.cluster.hierarchy.fcluster` to see to which cluster + each initial point would belong given a distance threshold: + + >>> fcluster(Z, 0.9, criterion='distance') + array([ 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6], dtype=int32) # may vary + >>> fcluster(Z, 1.1, criterion='distance') + array([5, 5, 6, 7, 7, 8, 1, 1, 2, 3, 3, 4], dtype=int32) # may vary + >>> fcluster(Z, 2, criterion='distance') + array([3, 3, 3, 4, 4, 4, 1, 1, 1, 2, 2, 2], dtype=int32) # may vary + >>> fcluster(Z, 4, criterion='distance') + array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=int32) + + Also, `scipy.cluster.hierarchy.dendrogram` can be used to generate a + plot of the dendrogram. + + """ + return linkage(y, method='centroid', metric='euclidean') + + +def median(y): + """ + Perform median/WPGMC linkage. + + See `linkage` for more information on the return structure + and algorithm. + + The following are common calling conventions: + + 1. ``Z = median(y)`` + + Performs median/WPGMC linkage on the condensed distance matrix + ``y``. See ``linkage`` for more information on the return + structure and algorithm. + + 2. ``Z = median(X)`` + + Performs median/WPGMC linkage on the observation matrix ``X`` + using Euclidean distance as the distance metric. See `linkage` + for more information on the return structure and algorithm. + + Parameters + ---------- + y : ndarray + A condensed distance matrix. A condensed + distance matrix is a flat array containing the upper + triangular of the distance matrix. This is the form that + ``pdist`` returns. Alternatively, a collection of + m observation vectors in n dimensions may be passed as + an m by n array. + + Returns + ------- + Z : ndarray + The hierarchical clustering encoded as a linkage matrix. + + See Also + -------- + linkage : for advanced creation of hierarchical clusterings. + scipy.spatial.distance.pdist : pairwise distance metrics + + Examples + -------- + >>> from scipy.cluster.hierarchy import median, fcluster + >>> from scipy.spatial.distance import pdist + + First, we need a toy dataset to play with:: + + x x x x + x x + + x x + x x x x + + >>> X = [[0, 0], [0, 1], [1, 0], + ... [0, 4], [0, 3], [1, 4], + ... [4, 0], [3, 0], [4, 1], + ... [4, 4], [3, 4], [4, 3]] + + Then, we get a condensed distance matrix from this dataset: + + >>> y = pdist(X) + + Finally, we can perform the clustering: + + >>> Z = median(y) + >>> Z + array([[ 0. , 1. , 1. , 2. ], + [ 3. , 4. , 1. , 2. ], + [ 9. , 10. , 1. , 2. ], + [ 6. , 7. , 1. , 2. ], + [ 2. , 12. , 1.11803399, 3. ], + [ 5. , 13. , 1.11803399, 3. ], + [ 8. , 15. , 1.11803399, 3. ], + [11. , 14. , 1.11803399, 3. ], + [18. , 19. , 3. , 6. ], + [16. , 17. , 3.5 , 6. ], + [20. , 21. , 3.25 , 12. ]]) + + The linkage matrix ``Z`` represents a dendrogram - see + `scipy.cluster.hierarchy.linkage` for a detailed explanation of its + contents. + + We can use `scipy.cluster.hierarchy.fcluster` to see to which cluster + each initial point would belong given a distance threshold: + + >>> fcluster(Z, 0.9, criterion='distance') + array([ 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6], dtype=int32) + >>> fcluster(Z, 1.1, criterion='distance') + array([5, 5, 6, 7, 7, 8, 1, 1, 2, 3, 3, 4], dtype=int32) + >>> fcluster(Z, 2, criterion='distance') + array([3, 3, 3, 4, 4, 4, 1, 1, 1, 2, 2, 2], dtype=int32) + >>> fcluster(Z, 4, criterion='distance') + array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=int32) + + Also, `scipy.cluster.hierarchy.dendrogram` can be used to generate a + plot of the dendrogram. + + """ + return linkage(y, method='median', metric='euclidean') + + +def ward(y): + """ + Perform Ward's linkage on a condensed distance matrix. + + See `linkage` for more information on the return structure + and algorithm. + + The following are common calling conventions: + + 1. ``Z = ward(y)`` + Performs Ward's linkage on the condensed distance matrix ``y``. + + 2. ``Z = ward(X)`` + Performs Ward's linkage on the observation matrix ``X`` using + Euclidean distance as the distance metric. + + Parameters + ---------- + y : ndarray + A condensed distance matrix. A condensed + distance matrix is a flat array containing the upper + triangular of the distance matrix. This is the form that + ``pdist`` returns. Alternatively, a collection of + m observation vectors in n dimensions may be passed as + an m by n array. + + Returns + ------- + Z : ndarray + The hierarchical clustering encoded as a linkage matrix. See + `linkage` for more information on the return structure and + algorithm. + + See Also + -------- + linkage : for advanced creation of hierarchical clusterings. + scipy.spatial.distance.pdist : pairwise distance metrics + + Examples + -------- + >>> from scipy.cluster.hierarchy import ward, fcluster + >>> from scipy.spatial.distance import pdist + + First, we need a toy dataset to play with:: + + x x x x + x x + + x x + x x x x + + >>> X = [[0, 0], [0, 1], [1, 0], + ... [0, 4], [0, 3], [1, 4], + ... [4, 0], [3, 0], [4, 1], + ... [4, 4], [3, 4], [4, 3]] + + Then, we get a condensed distance matrix from this dataset: + + >>> y = pdist(X) + + Finally, we can perform the clustering: + + >>> Z = ward(y) + >>> Z + array([[ 0. , 1. , 1. , 2. ], + [ 3. , 4. , 1. , 2. ], + [ 6. , 7. , 1. , 2. ], + [ 9. , 10. , 1. , 2. ], + [ 2. , 12. , 1.29099445, 3. ], + [ 5. , 13. , 1.29099445, 3. ], + [ 8. , 14. , 1.29099445, 3. ], + [11. , 15. , 1.29099445, 3. ], + [16. , 17. , 5.77350269, 6. ], + [18. , 19. , 5.77350269, 6. ], + [20. , 21. , 8.16496581, 12. ]]) + + The linkage matrix ``Z`` represents a dendrogram - see + `scipy.cluster.hierarchy.linkage` for a detailed explanation of its + contents. + + We can use `scipy.cluster.hierarchy.fcluster` to see to which cluster + each initial point would belong given a distance threshold: + + >>> fcluster(Z, 0.9, criterion='distance') + array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], dtype=int32) + >>> fcluster(Z, 1.1, criterion='distance') + array([1, 1, 2, 3, 3, 4, 5, 5, 6, 7, 7, 8], dtype=int32) + >>> fcluster(Z, 3, criterion='distance') + array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4], dtype=int32) + >>> fcluster(Z, 9, criterion='distance') + array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=int32) + + Also, `scipy.cluster.hierarchy.dendrogram` can be used to generate a + plot of the dendrogram. + + """ + return linkage(y, method='ward', metric='euclidean') + + +def linkage(y, method='single', metric='euclidean', optimal_ordering=False): + """ + Perform hierarchical/agglomerative clustering. + + The input y may be either a 1-D condensed distance matrix + or a 2-D array of observation vectors. + + If y is a 1-D condensed distance matrix, + then y must be a :math:`\\binom{n}{2}` sized + vector, where n is the number of original observations paired + in the distance matrix. The behavior of this function is very + similar to the MATLAB linkage function. + + A :math:`(n-1)` by 4 matrix ``Z`` is returned. At the + :math:`i`-th iteration, clusters with indices ``Z[i, 0]`` and + ``Z[i, 1]`` are combined to form cluster :math:`n + i`. A + cluster with an index less than :math:`n` corresponds to one of + the :math:`n` original observations. The distance between + clusters ``Z[i, 0]`` and ``Z[i, 1]`` is given by ``Z[i, 2]``. The + fourth value ``Z[i, 3]`` represents the number of original + observations in the newly formed cluster. + + The following linkage methods are used to compute the distance + :math:`d(s, t)` between two clusters :math:`s` and + :math:`t`. The algorithm begins with a forest of clusters that + have yet to be used in the hierarchy being formed. When two + clusters :math:`s` and :math:`t` from this forest are combined + into a single cluster :math:`u`, :math:`s` and :math:`t` are + removed from the forest, and :math:`u` is added to the + forest. When only one cluster remains in the forest, the algorithm + stops, and this cluster becomes the root. + + A distance matrix is maintained at each iteration. The ``d[i,j]`` + entry corresponds to the distance between cluster :math:`i` and + :math:`j` in the original forest. + + At each iteration, the algorithm must update the distance matrix + to reflect the distance of the newly formed cluster u with the + remaining clusters in the forest. + + Suppose there are :math:`|u|` original observations + :math:`u[0], \\ldots, u[|u|-1]` in cluster :math:`u` and + :math:`|v|` original objects :math:`v[0], \\ldots, v[|v|-1]` in + cluster :math:`v`. Recall, :math:`s` and :math:`t` are + combined to form cluster :math:`u`. Let :math:`v` be any + remaining cluster in the forest that is not :math:`u`. + + The following are methods for calculating the distance between the + newly formed cluster :math:`u` and each :math:`v`. + + * method='single' assigns + + .. math:: + d(u,v) = \\min(dist(u[i],v[j])) + + for all points :math:`i` in cluster :math:`u` and + :math:`j` in cluster :math:`v`. This is also known as the + Nearest Point Algorithm. + + * method='complete' assigns + + .. math:: + d(u, v) = \\max(dist(u[i],v[j])) + + for all points :math:`i` in cluster u and :math:`j` in + cluster :math:`v`. This is also known by the Farthest Point + Algorithm or Voor Hees Algorithm. + + * method='average' assigns + + .. math:: + d(u,v) = \\sum_{ij} \\frac{d(u[i], v[j])} + {(|u|*|v|)} + + for all points :math:`i` and :math:`j` where :math:`|u|` + and :math:`|v|` are the cardinalities of clusters :math:`u` + and :math:`v`, respectively. This is also called the UPGMA + algorithm. + + * method='weighted' assigns + + .. math:: + d(u,v) = (dist(s,v) + dist(t,v))/2 + + where cluster u was formed with cluster s and t and v + is a remaining cluster in the forest (also called WPGMA). + + * method='centroid' assigns + + .. math:: + dist(s,t) = ||c_s-c_t||_2 + + where :math:`c_s` and :math:`c_t` are the centroids of + clusters :math:`s` and :math:`t`, respectively. When two + clusters :math:`s` and :math:`t` are combined into a new + cluster :math:`u`, the new centroid is computed over all the + original objects in clusters :math:`s` and :math:`t`. The + distance then becomes the Euclidean distance between the + centroid of :math:`u` and the centroid of a remaining cluster + :math:`v` in the forest. This is also known as the UPGMC + algorithm. + + * method='median' assigns :math:`d(s,t)` like the ``centroid`` + method. When two clusters :math:`s` and :math:`t` are combined + into a new cluster :math:`u`, the average of centroids s and t + give the new centroid :math:`u`. This is also known as the + WPGMC algorithm. + + * method='ward' uses the Ward variance minimization algorithm. + The new entry :math:`d(u,v)` is computed as follows, + + .. math:: + + d(u,v) = \\sqrt{\\frac{|v|+|s|} + {T}d(v,s)^2 + + \\frac{|v|+|t|} + {T}d(v,t)^2 + - \\frac{|v|} + {T}d(s,t)^2} + + where :math:`u` is the newly joined cluster consisting of + clusters :math:`s` and :math:`t`, :math:`v` is an unused + cluster in the forest, :math:`T=|v|+|s|+|t|`, and + :math:`|*|` is the cardinality of its argument. This is also + known as the incremental algorithm. + + Warning: When the minimum distance pair in the forest is chosen, there + may be two or more pairs with the same minimum distance. This + implementation may choose a different minimum than the MATLAB + version. + + Parameters + ---------- + y : ndarray + A condensed distance matrix. A condensed distance matrix + is a flat array containing the upper triangular of the distance matrix. + This is the form that ``pdist`` returns. Alternatively, a collection of + :math:`m` observation vectors in :math:`n` dimensions may be passed as + an :math:`m` by :math:`n` array. All elements of the condensed distance + matrix must be finite, i.e., no NaNs or infs. + method : str, optional + The linkage algorithm to use. See the ``Linkage Methods`` section below + for full descriptions. + metric : str or function, optional + The distance metric to use in the case that y is a collection of + observation vectors; ignored otherwise. See the ``pdist`` + function for a list of valid distance metrics. A custom distance + function can also be used. + optimal_ordering : bool, optional + If True, the linkage matrix will be reordered so that the distance + between successive leaves is minimal. This results in a more intuitive + tree structure when the data are visualized. defaults to False, because + this algorithm can be slow, particularly on large datasets [2]_. See + also the `optimal_leaf_ordering` function. + + .. versionadded:: 1.0.0 + + Returns + ------- + Z : ndarray + The hierarchical clustering encoded as a linkage matrix. + + Notes + ----- + 1. For method 'single', an optimized algorithm based on minimum spanning + tree is implemented. It has time complexity :math:`O(n^2)`. + For methods 'complete', 'average', 'weighted' and 'ward', an algorithm + called nearest-neighbors chain is implemented. It also has time + complexity :math:`O(n^2)`. + For other methods, a naive algorithm is implemented with :math:`O(n^3)` + time complexity. + All algorithms use :math:`O(n^2)` memory. + Refer to [1]_ for details about the algorithms. + 2. Methods 'centroid', 'median', and 'ward' are correctly defined only if + Euclidean pairwise metric is used. If `y` is passed as precomputed + pairwise distances, then it is the user's responsibility to assure that + these distances are in fact Euclidean, otherwise the produced result + will be incorrect. + + See Also + -------- + scipy.spatial.distance.pdist : pairwise distance metrics + + References + ---------- + .. [1] Daniel Mullner, "Modern hierarchical, agglomerative clustering + algorithms", :arXiv:`1109.2378v1`. + .. [2] Ziv Bar-Joseph, David K. Gifford, Tommi S. Jaakkola, "Fast optimal + leaf ordering for hierarchical clustering", 2001. Bioinformatics + :doi:`10.1093/bioinformatics/17.suppl_1.S22` + + Examples + -------- + >>> from scipy.cluster.hierarchy import dendrogram, linkage + >>> from matplotlib import pyplot as plt + >>> X = [[i] for i in [2, 8, 0, 4, 1, 9, 9, 0]] + + >>> Z = linkage(X, 'ward') + >>> fig = plt.figure(figsize=(25, 10)) + >>> dn = dendrogram(Z) + + >>> Z = linkage(X, 'single') + >>> fig = plt.figure(figsize=(25, 10)) + >>> dn = dendrogram(Z) + >>> plt.show() + """ + xp = array_namespace(y) + y = _asarray(y, order='C', dtype=xp.float64, xp=xp) + + if method not in _LINKAGE_METHODS: + raise ValueError(f"Invalid method: {method}") + + if method in _EUCLIDEAN_METHODS and metric != 'euclidean' and y.ndim == 2: + msg = f"`method={method}` requires the distance metric to be Euclidean" + raise ValueError(msg) + + if y.ndim == 1: + distance.is_valid_y(y, throw=True, name='y') + elif y.ndim == 2: + if (y.shape[0] == y.shape[1] and np.allclose(np.diag(y), 0) and + xp.all(y >= 0) and np.allclose(y, y.T)): + warnings.warn('The symmetric non-negative hollow observation ' + 'matrix looks suspiciously like an uncondensed ' + 'distance matrix', + ClusterWarning, stacklevel=2) + y = distance.pdist(y, metric) + y = xp.asarray(y) + else: + raise ValueError("`y` must be 1 or 2 dimensional.") + + if not xp.all(xp.isfinite(y)): + raise ValueError("The condensed distance matrix must contain only " + "finite values.") + + n = int(distance.num_obs_y(y)) + method_code = _LINKAGE_METHODS[method] + + y = np.asarray(y) + if method == 'single': + result = _hierarchy.mst_single_linkage(y, n) + elif method in ['complete', 'average', 'weighted', 'ward']: + result = _hierarchy.nn_chain(y, n, method_code) + else: + result = _hierarchy.fast_linkage(y, n, method_code) + result = xp.asarray(result) + + if optimal_ordering: + y = xp.asarray(y) + return optimal_leaf_ordering(result, y) + else: + return result + + +class ClusterNode: + """ + A tree node class for representing a cluster. + + Leaf nodes correspond to original observations, while non-leaf nodes + correspond to non-singleton clusters. + + The `to_tree` function converts a matrix returned by the linkage + function into an easy-to-use tree representation. + + All parameter names are also attributes. + + Parameters + ---------- + id : int + The node id. + left : ClusterNode instance, optional + The left child tree node. + right : ClusterNode instance, optional + The right child tree node. + dist : float, optional + Distance for this cluster in the linkage matrix. + count : int, optional + The number of samples in this cluster. + + See Also + -------- + to_tree : for converting a linkage matrix ``Z`` into a tree object. + + """ + + def __init__(self, id, left=None, right=None, dist=0.0, count=1): + if id < 0: + raise ValueError('The id must be non-negative.') + if dist < 0: + raise ValueError('The distance must be non-negative.') + if (left is None and right is not None) or \ + (left is not None and right is None): + raise ValueError('Only full or proper binary trees are permitted.' + ' This node has one child.') + if count < 1: + raise ValueError('A cluster must contain at least one original ' + 'observation.') + self.id = id + self.left = left + self.right = right + self.dist = dist + if self.left is None: + self.count = count + else: + self.count = left.count + right.count + + def __lt__(self, node): + if not isinstance(node, ClusterNode): + raise ValueError("Can't compare ClusterNode " + f"to type {type(node)}") + return self.dist < node.dist + + def __gt__(self, node): + if not isinstance(node, ClusterNode): + raise ValueError("Can't compare ClusterNode " + f"to type {type(node)}") + return self.dist > node.dist + + def __eq__(self, node): + if not isinstance(node, ClusterNode): + raise ValueError("Can't compare ClusterNode " + f"to type {type(node)}") + return self.dist == node.dist + + def get_id(self): + """ + The identifier of the target node. + + For ``0 <= i < n``, `i` corresponds to original observation i. + For ``n <= i < 2n-1``, `i` corresponds to non-singleton cluster formed + at iteration ``i-n``. + + Returns + ------- + id : int + The identifier of the target node. + + """ + return self.id + + def get_count(self): + """ + The number of leaf nodes (original observations) belonging to + the cluster node nd. If the target node is a leaf, 1 is + returned. + + Returns + ------- + get_count : int + The number of leaf nodes below the target node. + + """ + return self.count + + def get_left(self): + """ + Return a reference to the left child tree object. + + Returns + ------- + left : ClusterNode + The left child of the target node. If the node is a leaf, + None is returned. + + """ + return self.left + + def get_right(self): + """ + Return a reference to the right child tree object. + + Returns + ------- + right : ClusterNode + The left child of the target node. If the node is a leaf, + None is returned. + + """ + return self.right + + def is_leaf(self): + """ + Return True if the target node is a leaf. + + Returns + ------- + leafness : bool + True if the target node is a leaf node. + + """ + return self.left is None + + def pre_order(self, func=(lambda x: x.id)): + """ + Perform pre-order traversal without recursive function calls. + + When a leaf node is first encountered, ``func`` is called with + the leaf node as its argument, and its result is appended to + the list. + + For example, the statement:: + + ids = root.pre_order(lambda x: x.id) + + returns a list of the node ids corresponding to the leaf nodes + of the tree as they appear from left to right. + + Parameters + ---------- + func : function + Applied to each leaf ClusterNode object in the pre-order traversal. + Given the ``i``-th leaf node in the pre-order traversal ``n[i]``, + the result of ``func(n[i])`` is stored in ``L[i]``. If not + provided, the index of the original observation to which the node + corresponds is used. + + Returns + ------- + L : list + The pre-order traversal. + + """ + # Do a preorder traversal, caching the result. To avoid having to do + # recursion, we'll store the previous index we've visited in a vector. + n = self.count + + curNode = [None] * (2 * n) + lvisited = set() + rvisited = set() + curNode[0] = self + k = 0 + preorder = [] + while k >= 0: + nd = curNode[k] + ndid = nd.id + if nd.is_leaf(): + preorder.append(func(nd)) + k = k - 1 + else: + if ndid not in lvisited: + curNode[k + 1] = nd.left + lvisited.add(ndid) + k = k + 1 + elif ndid not in rvisited: + curNode[k + 1] = nd.right + rvisited.add(ndid) + k = k + 1 + # If we've visited the left and right of this non-leaf + # node already, go up in the tree. + else: + k = k - 1 + + return preorder + + +_cnode_bare = ClusterNode(0) +_cnode_type = type(ClusterNode) + + +def _order_cluster_tree(Z): + """ + Return clustering nodes in bottom-up order by distance. + + Parameters + ---------- + Z : scipy.cluster.linkage array + The linkage matrix. + + Returns + ------- + nodes : list + A list of ClusterNode objects. + """ + q = deque() + tree = to_tree(Z) + q.append(tree) + nodes = [] + + while q: + node = q.popleft() + if not node.is_leaf(): + bisect.insort_left(nodes, node) + q.append(node.get_right()) + q.append(node.get_left()) + return nodes + + +def cut_tree(Z, n_clusters=None, height=None): + """ + Given a linkage matrix Z, return the cut tree. + + Parameters + ---------- + Z : scipy.cluster.linkage array + The linkage matrix. + n_clusters : array_like, optional + Number of clusters in the tree at the cut point. + height : array_like, optional + The height at which to cut the tree. Only possible for ultrametric + trees. + + Returns + ------- + cutree : array + An array indicating group membership at each agglomeration step. I.e., + for a full cut tree, in the first column each data point is in its own + cluster. At the next step, two nodes are merged. Finally, all + singleton and non-singleton clusters are in one group. If `n_clusters` + or `height` are given, the columns correspond to the columns of + `n_clusters` or `height`. + + Examples + -------- + >>> from scipy import cluster + >>> import numpy as np + >>> from numpy.random import default_rng + >>> rng = default_rng() + >>> X = rng.random((50, 4)) + >>> Z = cluster.hierarchy.ward(X) + >>> cutree = cluster.hierarchy.cut_tree(Z, n_clusters=[5, 10]) + >>> cutree[:10] + array([[0, 0], + [1, 1], + [2, 2], + [3, 3], + [3, 4], + [2, 2], + [0, 0], + [1, 5], + [3, 6], + [4, 7]]) # random + + """ + xp = array_namespace(Z) + nobs = num_obs_linkage(Z) + nodes = _order_cluster_tree(Z) + + if height is not None and n_clusters is not None: + raise ValueError("At least one of either height or n_clusters " + "must be None") + elif height is None and n_clusters is None: # return the full cut tree + cols_idx = xp.arange(nobs) + elif height is not None: + height = xp.asarray(height) + heights = xp.asarray([x.dist for x in nodes]) + cols_idx = xp.searchsorted(heights, height) + else: + n_clusters = xp.asarray(n_clusters) + cols_idx = nobs - xp.searchsorted(xp.arange(nobs), n_clusters) + + try: + n_cols = len(cols_idx) + except TypeError: # scalar + n_cols = 1 + cols_idx = xp.asarray([cols_idx]) + + groups = xp.zeros((n_cols, nobs), dtype=xp.int64) + last_group = xp.arange(nobs) + if 0 in cols_idx: + groups[0] = last_group + + for i, node in enumerate(nodes): + idx = node.pre_order() + this_group = xp_copy(last_group, xp=xp) + # TODO ARRAY_API complex indexing not supported + this_group[idx] = xp.min(last_group[idx]) + this_group[this_group > xp.max(last_group[idx])] -= 1 + if i + 1 in cols_idx: + groups[np.nonzero(i + 1 == cols_idx)[0]] = this_group + last_group = this_group + + return groups.T + + +def to_tree(Z, rd=False): + """ + Convert a linkage matrix into an easy-to-use tree object. + + The reference to the root `ClusterNode` object is returned (by default). + + Each `ClusterNode` object has a ``left``, ``right``, ``dist``, ``id``, + and ``count`` attribute. The left and right attributes point to + ClusterNode objects that were combined to generate the cluster. + If both are None then the `ClusterNode` object is a leaf node, its count + must be 1, and its distance is meaningless but set to 0. + + *Note: This function is provided for the convenience of the library + user. ClusterNodes are not used as input to any of the functions in this + library.* + + Parameters + ---------- + Z : ndarray + The linkage matrix in proper form (see the `linkage` + function documentation). + rd : bool, optional + When False (default), a reference to the root `ClusterNode` object is + returned. Otherwise, a tuple ``(r, d)`` is returned. ``r`` is a + reference to the root node while ``d`` is a list of `ClusterNode` + objects - one per original entry in the linkage matrix plus entries + for all clustering steps. If a cluster id is + less than the number of samples ``n`` in the data that the linkage + matrix describes, then it corresponds to a singleton cluster (leaf + node). + See `linkage` for more information on the assignment of cluster ids + to clusters. + + Returns + ------- + tree : ClusterNode or tuple (ClusterNode, list of ClusterNode) + If ``rd`` is False, a `ClusterNode`. + If ``rd`` is True, a list of length ``2*n - 1``, with ``n`` the number + of samples. See the description of `rd` above for more details. + + See Also + -------- + linkage, is_valid_linkage, ClusterNode + + Examples + -------- + >>> import numpy as np + >>> from scipy.cluster import hierarchy + >>> rng = np.random.default_rng() + >>> x = rng.random((5, 2)) + >>> Z = hierarchy.linkage(x) + >>> hierarchy.to_tree(Z) + >> rootnode, nodelist = hierarchy.to_tree(Z, rd=True) + >>> rootnode + >> len(nodelist) + 9 + + """ + xp = array_namespace(Z) + Z = _asarray(Z, order='c', xp=xp) + is_valid_linkage(Z, throw=True, name='Z') + + # Number of original objects is equal to the number of rows plus 1. + n = Z.shape[0] + 1 + + # Create a list full of None's to store the node objects + d = [None] * (n * 2 - 1) + + # Create the nodes corresponding to the n original objects. + for i in range(0, n): + d[i] = ClusterNode(i) + + nd = None + + for i in range(Z.shape[0]): + row = Z[i, :] + + fi = int_floor(row[0], xp) + fj = int_floor(row[1], xp) + if fi > i + n: + raise ValueError(('Corrupt matrix Z. Index to derivative cluster ' + 'is used before it is formed. See row %d, ' + 'column 0') % fi) + if fj > i + n: + raise ValueError(('Corrupt matrix Z. Index to derivative cluster ' + 'is used before it is formed. See row %d, ' + 'column 1') % fj) + + nd = ClusterNode(i + n, d[fi], d[fj], row[2]) + # ^ id ^ left ^ right ^ dist + if row[3] != nd.count: + raise ValueError(('Corrupt matrix Z. The count Z[%d,3] is ' + 'incorrect.') % i) + d[n + i] = nd + + if rd: + return (nd, d) + else: + return nd + + +def optimal_leaf_ordering(Z, y, metric='euclidean'): + """ + Given a linkage matrix Z and distance, reorder the cut tree. + + Parameters + ---------- + Z : ndarray + The hierarchical clustering encoded as a linkage matrix. See + `linkage` for more information on the return structure and + algorithm. + y : ndarray + The condensed distance matrix from which Z was generated. + Alternatively, a collection of m observation vectors in n + dimensions may be passed as an m by n array. + metric : str or function, optional + The distance metric to use in the case that y is a collection of + observation vectors; ignored otherwise. See the ``pdist`` + function for a list of valid distance metrics. A custom distance + function can also be used. + + Returns + ------- + Z_ordered : ndarray + A copy of the linkage matrix Z, reordered to minimize the distance + between adjacent leaves. + + Examples + -------- + >>> import numpy as np + >>> from scipy.cluster import hierarchy + >>> rng = np.random.default_rng() + >>> X = rng.standard_normal((10, 10)) + >>> Z = hierarchy.ward(X) + >>> hierarchy.leaves_list(Z) + array([0, 3, 1, 9, 2, 5, 7, 4, 6, 8], dtype=int32) + >>> hierarchy.leaves_list(hierarchy.optimal_leaf_ordering(Z, X)) + array([3, 0, 2, 5, 7, 4, 8, 6, 9, 1], dtype=int32) + + """ + xp = array_namespace(Z, y) + Z = _asarray(Z, order='C', xp=xp) + is_valid_linkage(Z, throw=True, name='Z') + + y = _asarray(y, order='C', dtype=xp.float64, xp=xp) + + if y.ndim == 1: + distance.is_valid_y(y, throw=True, name='y') + elif y.ndim == 2: + if (y.shape[0] == y.shape[1] and np.allclose(np.diag(y), 0) and + np.all(y >= 0) and np.allclose(y, y.T)): + warnings.warn('The symmetric non-negative hollow observation ' + 'matrix looks suspiciously like an uncondensed ' + 'distance matrix', + ClusterWarning, stacklevel=2) + y = distance.pdist(y, metric) + y = xp.asarray(y) + else: + raise ValueError("`y` must be 1 or 2 dimensional.") + + if not xp.all(xp.isfinite(y)): + raise ValueError("The condensed distance matrix must contain only " + "finite values.") + + Z = np.asarray(Z) + y = np.asarray(y) + return xp.asarray(_optimal_leaf_ordering.optimal_leaf_ordering(Z, y)) + + +def cophenet(Z, Y=None): + """ + Calculate the cophenetic distances between each observation in + the hierarchical clustering defined by the linkage ``Z``. + + Suppose ``p`` and ``q`` are original observations in + disjoint clusters ``s`` and ``t``, respectively and + ``s`` and ``t`` are joined by a direct parent cluster + ``u``. The cophenetic distance between observations + ``i`` and ``j`` is simply the distance between + clusters ``s`` and ``t``. + + Parameters + ---------- + Z : ndarray + The hierarchical clustering encoded as an array + (see `linkage` function). + Y : ndarray (optional) + Calculates the cophenetic correlation coefficient ``c`` of a + hierarchical clustering defined by the linkage matrix `Z` + of a set of :math:`n` observations in :math:`m` + dimensions. `Y` is the condensed distance matrix from which + `Z` was generated. + + Returns + ------- + c : ndarray + The cophentic correlation distance (if ``Y`` is passed). + d : ndarray + The cophenetic distance matrix in condensed form. The + :math:`ij` th entry is the cophenetic distance between + original observations :math:`i` and :math:`j`. + + See Also + -------- + linkage : + for a description of what a linkage matrix is. + scipy.spatial.distance.squareform : + transforming condensed matrices into square ones. + + Examples + -------- + >>> from scipy.cluster.hierarchy import single, cophenet + >>> from scipy.spatial.distance import pdist, squareform + + Given a dataset ``X`` and a linkage matrix ``Z``, the cophenetic distance + between two points of ``X`` is the distance between the largest two + distinct clusters that each of the points: + + >>> X = [[0, 0], [0, 1], [1, 0], + ... [0, 4], [0, 3], [1, 4], + ... [4, 0], [3, 0], [4, 1], + ... [4, 4], [3, 4], [4, 3]] + + ``X`` corresponds to this dataset :: + + x x x x + x x + + x x + x x x x + + >>> Z = single(pdist(X)) + >>> Z + array([[ 0., 1., 1., 2.], + [ 2., 12., 1., 3.], + [ 3., 4., 1., 2.], + [ 5., 14., 1., 3.], + [ 6., 7., 1., 2.], + [ 8., 16., 1., 3.], + [ 9., 10., 1., 2.], + [11., 18., 1., 3.], + [13., 15., 2., 6.], + [17., 20., 2., 9.], + [19., 21., 2., 12.]]) + >>> cophenet(Z) + array([1., 1., 2., 2., 2., 2., 2., 2., 2., 2., 2., 1., 2., 2., 2., 2., 2., + 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 1., 1., 2., 2., + 2., 2., 2., 2., 1., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., + 1., 1., 2., 2., 2., 1., 2., 2., 2., 2., 2., 2., 1., 1., 1.]) + + The output of the `scipy.cluster.hierarchy.cophenet` method is + represented in condensed form. We can use + `scipy.spatial.distance.squareform` to see the output as a + regular matrix (where each element ``ij`` denotes the cophenetic distance + between each ``i``, ``j`` pair of points in ``X``): + + >>> squareform(cophenet(Z)) + array([[0., 1., 1., 2., 2., 2., 2., 2., 2., 2., 2., 2.], + [1., 0., 1., 2., 2., 2., 2., 2., 2., 2., 2., 2.], + [1., 1., 0., 2., 2., 2., 2., 2., 2., 2., 2., 2.], + [2., 2., 2., 0., 1., 1., 2., 2., 2., 2., 2., 2.], + [2., 2., 2., 1., 0., 1., 2., 2., 2., 2., 2., 2.], + [2., 2., 2., 1., 1., 0., 2., 2., 2., 2., 2., 2.], + [2., 2., 2., 2., 2., 2., 0., 1., 1., 2., 2., 2.], + [2., 2., 2., 2., 2., 2., 1., 0., 1., 2., 2., 2.], + [2., 2., 2., 2., 2., 2., 1., 1., 0., 2., 2., 2.], + [2., 2., 2., 2., 2., 2., 2., 2., 2., 0., 1., 1.], + [2., 2., 2., 2., 2., 2., 2., 2., 2., 1., 0., 1.], + [2., 2., 2., 2., 2., 2., 2., 2., 2., 1., 1., 0.]]) + + In this example, the cophenetic distance between points on ``X`` that are + very close (i.e., in the same corner) is 1. For other pairs of points is 2, + because the points will be located in clusters at different + corners - thus, the distance between these clusters will be larger. + + """ + xp = array_namespace(Z, Y) + # Ensure float64 C-contiguous array. Cython code doesn't deal with striding. + Z = _asarray(Z, order='C', dtype=xp.float64, xp=xp) + is_valid_linkage(Z, throw=True, name='Z') + n = Z.shape[0] + 1 + zz = np.zeros((n * (n-1)) // 2, dtype=np.float64) + + Z = np.asarray(Z) + _hierarchy.cophenetic_distances(Z, zz, int(n)) + zz = xp.asarray(zz) + if Y is None: + return zz + + Y = _asarray(Y, order='C', xp=xp) + distance.is_valid_y(Y, throw=True, name='Y') + + z = xp.mean(zz) + y = xp.mean(Y) + Yy = Y - y + Zz = zz - z + numerator = (Yy * Zz) + denomA = Yy**2 + denomB = Zz**2 + c = xp.sum(numerator) / xp.sqrt(xp.sum(denomA) * xp.sum(denomB)) + return (c, zz) + + +def inconsistent(Z, d=2): + r""" + Calculate inconsistency statistics on a linkage matrix. + + Parameters + ---------- + Z : ndarray + The :math:`(n-1)` by 4 matrix encoding the linkage (hierarchical + clustering). See `linkage` documentation for more information on its + form. + d : int, optional + The number of links up to `d` levels below each non-singleton cluster. + + Returns + ------- + R : ndarray + A :math:`(n-1)` by 4 matrix where the ``i``'th row contains the link + statistics for the non-singleton cluster ``i``. The link statistics are + computed over the link heights for links :math:`d` levels below the + cluster ``i``. ``R[i,0]`` and ``R[i,1]`` are the mean and standard + deviation of the link heights, respectively; ``R[i,2]`` is the number + of links included in the calculation; and ``R[i,3]`` is the + inconsistency coefficient, + + .. math:: \frac{\mathtt{Z[i,2]} - \mathtt{R[i,0]}} {R[i,1]} + + Notes + ----- + This function behaves similarly to the MATLAB(TM) ``inconsistent`` + function. + + Examples + -------- + >>> from scipy.cluster.hierarchy import inconsistent, linkage + >>> from matplotlib import pyplot as plt + >>> X = [[i] for i in [2, 8, 0, 4, 1, 9, 9, 0]] + >>> Z = linkage(X, 'ward') + >>> print(Z) + [[ 5. 6. 0. 2. ] + [ 2. 7. 0. 2. ] + [ 0. 4. 1. 2. ] + [ 1. 8. 1.15470054 3. ] + [ 9. 10. 2.12132034 4. ] + [ 3. 12. 4.11096096 5. ] + [11. 13. 14.07183949 8. ]] + >>> inconsistent(Z) + array([[ 0. , 0. , 1. , 0. ], + [ 0. , 0. , 1. , 0. ], + [ 1. , 0. , 1. , 0. ], + [ 0.57735027, 0.81649658, 2. , 0.70710678], + [ 1.04044011, 1.06123822, 3. , 1.01850858], + [ 3.11614065, 1.40688837, 2. , 0.70710678], + [ 6.44583366, 6.76770586, 3. , 1.12682288]]) + + """ + xp = array_namespace(Z) + Z = _asarray(Z, order='C', dtype=xp.float64, xp=xp) + is_valid_linkage(Z, throw=True, name='Z') + + if (not d == np.floor(d)) or d < 0: + raise ValueError('The second argument d must be a nonnegative ' + 'integer value.') + + n = Z.shape[0] + 1 + R = np.zeros((n - 1, 4), dtype=np.float64) + + Z = np.asarray(Z) + _hierarchy.inconsistent(Z, R, int(n), int(d)) + R = xp.asarray(R) + return R + + +def from_mlab_linkage(Z): + """ + Convert a linkage matrix generated by MATLAB(TM) to a new + linkage matrix compatible with this module. + + The conversion does two things: + + * the indices are converted from ``1..N`` to ``0..(N-1)`` form, + and + + * a fourth column ``Z[:,3]`` is added where ``Z[i,3]`` represents the + number of original observations (leaves) in the non-singleton + cluster ``i``. + + This function is useful when loading in linkages from legacy data + files generated by MATLAB. + + Parameters + ---------- + Z : ndarray + A linkage matrix generated by MATLAB(TM). + + Returns + ------- + ZS : ndarray + A linkage matrix compatible with ``scipy.cluster.hierarchy``. + + See Also + -------- + linkage : for a description of what a linkage matrix is. + to_mlab_linkage : transform from SciPy to MATLAB format. + + Examples + -------- + >>> import numpy as np + >>> from scipy.cluster.hierarchy import ward, from_mlab_linkage + + Given a linkage matrix in MATLAB format ``mZ``, we can use + `scipy.cluster.hierarchy.from_mlab_linkage` to import + it into SciPy format: + + >>> mZ = np.array([[1, 2, 1], [4, 5, 1], [7, 8, 1], + ... [10, 11, 1], [3, 13, 1.29099445], + ... [6, 14, 1.29099445], + ... [9, 15, 1.29099445], + ... [12, 16, 1.29099445], + ... [17, 18, 5.77350269], + ... [19, 20, 5.77350269], + ... [21, 22, 8.16496581]]) + + >>> Z = from_mlab_linkage(mZ) + >>> Z + array([[ 0. , 1. , 1. , 2. ], + [ 3. , 4. , 1. , 2. ], + [ 6. , 7. , 1. , 2. ], + [ 9. , 10. , 1. , 2. ], + [ 2. , 12. , 1.29099445, 3. ], + [ 5. , 13. , 1.29099445, 3. ], + [ 8. , 14. , 1.29099445, 3. ], + [ 11. , 15. , 1.29099445, 3. ], + [ 16. , 17. , 5.77350269, 6. ], + [ 18. , 19. , 5.77350269, 6. ], + [ 20. , 21. , 8.16496581, 12. ]]) + + As expected, the linkage matrix ``Z`` returned includes an + additional column counting the number of original samples in + each cluster. Also, all cluster indices are reduced by 1 + (MATLAB format uses 1-indexing, whereas SciPy uses 0-indexing). + + """ + xp = array_namespace(Z) + Z = _asarray(Z, dtype=xp.float64, order='C', xp=xp) + Zs = Z.shape + + # If it's empty, return it. + if len(Zs) == 0 or (len(Zs) == 1 and Zs[0] == 0): + return xp_copy(Z, xp=xp) + + if len(Zs) != 2: + raise ValueError("The linkage array must be rectangular.") + + # If it contains no rows, return it. + if Zs[0] == 0: + return xp_copy(Z, xp=xp) + + if xp.min(Z[:, 0:2]) != 1.0 and xp.max(Z[:, 0:2]) != 2 * Zs[0]: + raise ValueError('The format of the indices is not 1..N') + + Zpart = xp.concat((Z[:, 0:2] - 1.0, Z[:, 2:]), axis=1) + CS = np.zeros((Zs[0],), dtype=np.float64) + if is_jax(xp): + # calculate_cluster_sizes doesn't accept read-only arrays + Zpart = np.array(Zpart, copy=True) + else: + Zpart = np.asarray(Zpart) + _hierarchy.calculate_cluster_sizes(Zpart, CS, int(Zs[0]) + 1) + res = np.hstack([Zpart, CS.reshape(Zs[0], 1)]) + return xp.asarray(res) + + +def to_mlab_linkage(Z): + """ + Convert a linkage matrix to a MATLAB(TM) compatible one. + + Converts a linkage matrix ``Z`` generated by the linkage function + of this module to a MATLAB(TM) compatible one. The return linkage + matrix has the last column removed and the cluster indices are + converted to ``1..N`` indexing. + + Parameters + ---------- + Z : ndarray + A linkage matrix generated by ``scipy.cluster.hierarchy``. + + Returns + ------- + to_mlab_linkage : ndarray + A linkage matrix compatible with MATLAB(TM)'s hierarchical + clustering functions. + + The return linkage matrix has the last column removed + and the cluster indices are converted to ``1..N`` indexing. + + See Also + -------- + linkage : for a description of what a linkage matrix is. + from_mlab_linkage : transform from Matlab to SciPy format. + + Examples + -------- + >>> from scipy.cluster.hierarchy import ward, to_mlab_linkage + >>> from scipy.spatial.distance import pdist + + >>> X = [[0, 0], [0, 1], [1, 0], + ... [0, 4], [0, 3], [1, 4], + ... [4, 0], [3, 0], [4, 1], + ... [4, 4], [3, 4], [4, 3]] + + >>> Z = ward(pdist(X)) + >>> Z + array([[ 0. , 1. , 1. , 2. ], + [ 3. , 4. , 1. , 2. ], + [ 6. , 7. , 1. , 2. ], + [ 9. , 10. , 1. , 2. ], + [ 2. , 12. , 1.29099445, 3. ], + [ 5. , 13. , 1.29099445, 3. ], + [ 8. , 14. , 1.29099445, 3. ], + [11. , 15. , 1.29099445, 3. ], + [16. , 17. , 5.77350269, 6. ], + [18. , 19. , 5.77350269, 6. ], + [20. , 21. , 8.16496581, 12. ]]) + + After a linkage matrix ``Z`` has been created, we can use + `scipy.cluster.hierarchy.to_mlab_linkage` to convert it + into MATLAB format: + + >>> mZ = to_mlab_linkage(Z) + >>> mZ + array([[ 1. , 2. , 1. ], + [ 4. , 5. , 1. ], + [ 7. , 8. , 1. ], + [ 10. , 11. , 1. ], + [ 3. , 13. , 1.29099445], + [ 6. , 14. , 1.29099445], + [ 9. , 15. , 1.29099445], + [ 12. , 16. , 1.29099445], + [ 17. , 18. , 5.77350269], + [ 19. , 20. , 5.77350269], + [ 21. , 22. , 8.16496581]]) + + The new linkage matrix ``mZ`` uses 1-indexing for all the + clusters (instead of 0-indexing). Also, the last column of + the original linkage matrix has been dropped. + + """ + xp = array_namespace(Z) + Z = _asarray(Z, order='C', dtype=xp.float64, xp=xp) + Zs = Z.shape + if len(Zs) == 0 or (len(Zs) == 1 and Zs[0] == 0): + return xp_copy(Z, xp=xp) + is_valid_linkage(Z, throw=True, name='Z') + + return xp.concat((Z[:, :2] + 1.0, Z[:, 2:3]), axis=1) + + +def is_monotonic(Z): + """ + Return True if the linkage passed is monotonic. + + The linkage is monotonic if for every cluster :math:`s` and :math:`t` + joined, the distance between them is no less than the distance + between any previously joined clusters. + + Parameters + ---------- + Z : ndarray + The linkage matrix to check for monotonicity. + + Returns + ------- + b : bool + A boolean indicating whether the linkage is monotonic. + + See Also + -------- + linkage : for a description of what a linkage matrix is. + + Examples + -------- + >>> from scipy.cluster.hierarchy import median, ward, is_monotonic + >>> from scipy.spatial.distance import pdist + + By definition, some hierarchical clustering algorithms - such as + `scipy.cluster.hierarchy.ward` - produce monotonic assignments of + samples to clusters; however, this is not always true for other + hierarchical methods - e.g. `scipy.cluster.hierarchy.median`. + + Given a linkage matrix ``Z`` (as the result of a hierarchical clustering + method) we can test programmatically whether it has the monotonicity + property or not, using `scipy.cluster.hierarchy.is_monotonic`: + + >>> X = [[0, 0], [0, 1], [1, 0], + ... [0, 4], [0, 3], [1, 4], + ... [4, 0], [3, 0], [4, 1], + ... [4, 4], [3, 4], [4, 3]] + + >>> Z = ward(pdist(X)) + >>> Z + array([[ 0. , 1. , 1. , 2. ], + [ 3. , 4. , 1. , 2. ], + [ 6. , 7. , 1. , 2. ], + [ 9. , 10. , 1. , 2. ], + [ 2. , 12. , 1.29099445, 3. ], + [ 5. , 13. , 1.29099445, 3. ], + [ 8. , 14. , 1.29099445, 3. ], + [11. , 15. , 1.29099445, 3. ], + [16. , 17. , 5.77350269, 6. ], + [18. , 19. , 5.77350269, 6. ], + [20. , 21. , 8.16496581, 12. ]]) + >>> is_monotonic(Z) + True + + >>> Z = median(pdist(X)) + >>> Z + array([[ 0. , 1. , 1. , 2. ], + [ 3. , 4. , 1. , 2. ], + [ 9. , 10. , 1. , 2. ], + [ 6. , 7. , 1. , 2. ], + [ 2. , 12. , 1.11803399, 3. ], + [ 5. , 13. , 1.11803399, 3. ], + [ 8. , 15. , 1.11803399, 3. ], + [11. , 14. , 1.11803399, 3. ], + [18. , 19. , 3. , 6. ], + [16. , 17. , 3.5 , 6. ], + [20. , 21. , 3.25 , 12. ]]) + >>> is_monotonic(Z) + False + + Note that this method is equivalent to just verifying that the distances + in the third column of the linkage matrix appear in a monotonically + increasing order. + + """ + xp = array_namespace(Z) + Z = _asarray(Z, order='c', xp=xp) + is_valid_linkage(Z, throw=True, name='Z') + + # We expect the i'th value to be greater than its successor. + return xp.all(Z[1:, 2] >= Z[:-1, 2]) + + +def is_valid_im(R, warning=False, throw=False, name=None): + """Return True if the inconsistency matrix passed is valid. + + It must be a :math:`n` by 4 array of doubles. The standard + deviations ``R[:,1]`` must be nonnegative. The link counts + ``R[:,2]`` must be positive and no greater than :math:`n-1`. + + Parameters + ---------- + R : ndarray + The inconsistency matrix to check for validity. + warning : bool, optional + When True, issues a Python warning if the linkage + matrix passed is invalid. + throw : bool, optional + When True, throws a Python exception if the linkage + matrix passed is invalid. + name : str, optional + This string refers to the variable name of the invalid + linkage matrix. + + Returns + ------- + b : bool + True if the inconsistency matrix is valid. + + See Also + -------- + linkage : for a description of what a linkage matrix is. + inconsistent : for the creation of a inconsistency matrix. + + Examples + -------- + >>> from scipy.cluster.hierarchy import ward, inconsistent, is_valid_im + >>> from scipy.spatial.distance import pdist + + Given a data set ``X``, we can apply a clustering method to obtain a + linkage matrix ``Z``. `scipy.cluster.hierarchy.inconsistent` can + be also used to obtain the inconsistency matrix ``R`` associated to + this clustering process: + + >>> X = [[0, 0], [0, 1], [1, 0], + ... [0, 4], [0, 3], [1, 4], + ... [4, 0], [3, 0], [4, 1], + ... [4, 4], [3, 4], [4, 3]] + + >>> Z = ward(pdist(X)) + >>> R = inconsistent(Z) + >>> Z + array([[ 0. , 1. , 1. , 2. ], + [ 3. , 4. , 1. , 2. ], + [ 6. , 7. , 1. , 2. ], + [ 9. , 10. , 1. , 2. ], + [ 2. , 12. , 1.29099445, 3. ], + [ 5. , 13. , 1.29099445, 3. ], + [ 8. , 14. , 1.29099445, 3. ], + [11. , 15. , 1.29099445, 3. ], + [16. , 17. , 5.77350269, 6. ], + [18. , 19. , 5.77350269, 6. ], + [20. , 21. , 8.16496581, 12. ]]) + >>> R + array([[1. , 0. , 1. , 0. ], + [1. , 0. , 1. , 0. ], + [1. , 0. , 1. , 0. ], + [1. , 0. , 1. , 0. ], + [1.14549722, 0.20576415, 2. , 0.70710678], + [1.14549722, 0.20576415, 2. , 0.70710678], + [1.14549722, 0.20576415, 2. , 0.70710678], + [1.14549722, 0.20576415, 2. , 0.70710678], + [2.78516386, 2.58797734, 3. , 1.15470054], + [2.78516386, 2.58797734, 3. , 1.15470054], + [6.57065706, 1.38071187, 3. , 1.15470054]]) + + Now we can use `scipy.cluster.hierarchy.is_valid_im` to verify that + ``R`` is correct: + + >>> is_valid_im(R) + True + + However, if ``R`` is wrongly constructed (e.g., one of the standard + deviations is set to a negative value), then the check will fail: + + >>> R[-1,1] = R[-1,1] * -1 + >>> is_valid_im(R) + False + + """ + xp = array_namespace(R) + R = _asarray(R, order='c', xp=xp) + valid = True + name_str = f"{name!r} " if name else '' + try: + if R.dtype != xp.float64: + raise TypeError(f'Inconsistency matrix {name_str}must contain doubles ' + '(double).') + if len(R.shape) != 2: + raise ValueError(f'Inconsistency matrix {name_str}must have shape=2 (i.e. ' + 'be two-dimensional).') + if R.shape[1] != 4: + raise ValueError(f'Inconsistency matrix {name_str}' + 'must have 4 columns.') + if R.shape[0] < 1: + raise ValueError(f'Inconsistency matrix {name_str}' + 'must have at least one row.') + if xp.any(R[:, 0] < 0): + raise ValueError(f'Inconsistency matrix {name_str}' + 'contains negative link height means.') + if xp.any(R[:, 1] < 0): + raise ValueError(f'Inconsistency matrix {name_str}' + 'contains negative link height standard deviations.') + if xp.any(R[:, 2] < 0): + raise ValueError(f'Inconsistency matrix {name_str}' + 'contains negative link counts.') + except Exception as e: + if throw: + raise + if warning: + _warning(str(e)) + valid = False + + return valid + + +def is_valid_linkage(Z, warning=False, throw=False, name=None): + """ + Check the validity of a linkage matrix. + + A linkage matrix is valid if it is a 2-D array (type double) + with :math:`n` rows and 4 columns. The first two columns must contain + indices between 0 and :math:`2n-1`. For a given row ``i``, the following + two expressions have to hold: + + .. math:: + + 0 \\leq \\mathtt{Z[i,0]} \\leq i+n-1 + 0 \\leq Z[i,1] \\leq i+n-1 + + I.e., a cluster cannot join another cluster unless the cluster being joined + has been generated. + + The fourth column of `Z` represents the number of original observations + in a cluster, so a valid ``Z[i, 3]`` value may not exceed the number of + original observations. + + Parameters + ---------- + Z : array_like + Linkage matrix. + warning : bool, optional + When True, issues a Python warning if the linkage + matrix passed is invalid. + throw : bool, optional + When True, throws a Python exception if the linkage + matrix passed is invalid. + name : str, optional + This string refers to the variable name of the invalid + linkage matrix. + + Returns + ------- + b : bool + True if the inconsistency matrix is valid. + + See Also + -------- + linkage: for a description of what a linkage matrix is. + + Examples + -------- + >>> from scipy.cluster.hierarchy import ward, is_valid_linkage + >>> from scipy.spatial.distance import pdist + + All linkage matrices generated by the clustering methods in this module + will be valid (i.e., they will have the appropriate dimensions and the two + required expressions will hold for all the rows). + + We can check this using `scipy.cluster.hierarchy.is_valid_linkage`: + + >>> X = [[0, 0], [0, 1], [1, 0], + ... [0, 4], [0, 3], [1, 4], + ... [4, 0], [3, 0], [4, 1], + ... [4, 4], [3, 4], [4, 3]] + + >>> Z = ward(pdist(X)) + >>> Z + array([[ 0. , 1. , 1. , 2. ], + [ 3. , 4. , 1. , 2. ], + [ 6. , 7. , 1. , 2. ], + [ 9. , 10. , 1. , 2. ], + [ 2. , 12. , 1.29099445, 3. ], + [ 5. , 13. , 1.29099445, 3. ], + [ 8. , 14. , 1.29099445, 3. ], + [11. , 15. , 1.29099445, 3. ], + [16. , 17. , 5.77350269, 6. ], + [18. , 19. , 5.77350269, 6. ], + [20. , 21. , 8.16496581, 12. ]]) + >>> is_valid_linkage(Z) + True + + However, if we create a linkage matrix in a wrong way - or if we modify + a valid one in a way that any of the required expressions don't hold + anymore, then the check will fail: + + >>> Z[3][1] = 20 # the cluster number 20 is not defined at this point + >>> is_valid_linkage(Z) + False + + """ + xp = array_namespace(Z) + Z = _asarray(Z, order='c', xp=xp) + valid = True + name_str = f"{name!r} " if name else '' + try: + if Z.dtype != xp.float64: + raise TypeError(f'Linkage matrix {name_str}must contain doubles.') + if len(Z.shape) != 2: + raise ValueError(f'Linkage matrix {name_str}must have shape=2 (i.e. be' + ' two-dimensional).') + if Z.shape[1] != 4: + raise ValueError(f'Linkage matrix {name_str}must have 4 columns.') + if Z.shape[0] == 0: + raise ValueError('Linkage must be computed on at least two ' + 'observations.') + n = Z.shape[0] + if n > 1: + if (xp.any(Z[:, 0] < 0) or xp.any(Z[:, 1] < 0)): + raise ValueError(f'Linkage {name_str}contains negative indices.') + if xp.any(Z[:, 2] < 0): + raise ValueError(f'Linkage {name_str}contains negative distances.') + if xp.any(Z[:, 3] < 0): + raise ValueError(f'Linkage {name_str}contains negative counts.') + if xp.any(Z[:, 3] > (Z.shape[0] + 1)): + raise ValueError('Linkage matrix contains excessive observations' + 'in a cluster') + if _check_hierarchy_uses_cluster_before_formed(Z): + raise ValueError(f'Linkage {name_str}uses non-singleton cluster before' + ' it is formed.') + if _check_hierarchy_uses_cluster_more_than_once(Z): + raise ValueError(f'Linkage {name_str}uses the same cluster more than once.') + except Exception as e: + if throw: + raise + if warning: + _warning(str(e)) + valid = False + + return valid + + +def _check_hierarchy_uses_cluster_before_formed(Z): + n = Z.shape[0] + 1 + for i in range(0, n - 1): + if Z[i, 0] >= n + i or Z[i, 1] >= n + i: + return True + return False + + +def _check_hierarchy_uses_cluster_more_than_once(Z): + n = Z.shape[0] + 1 + chosen = set() + for i in range(0, n - 1): + used_more_than_once = ( + (float(Z[i, 0]) in chosen) + or (float(Z[i, 1]) in chosen) + or Z[i, 0] == Z[i, 1] + ) + if used_more_than_once: + return True + chosen.add(float(Z[i, 0])) + chosen.add(float(Z[i, 1])) + return False + + +def _check_hierarchy_not_all_clusters_used(Z): + n = Z.shape[0] + 1 + chosen = set() + for i in range(0, n - 1): + chosen.add(int(Z[i, 0])) + chosen.add(int(Z[i, 1])) + must_chosen = set(range(0, 2 * n - 2)) + return len(must_chosen.difference(chosen)) > 0 + + +def num_obs_linkage(Z): + """ + Return the number of original observations of the linkage matrix passed. + + Parameters + ---------- + Z : ndarray + The linkage matrix on which to perform the operation. + + Returns + ------- + n : int + The number of original observations in the linkage. + + Examples + -------- + >>> from scipy.cluster.hierarchy import ward, num_obs_linkage + >>> from scipy.spatial.distance import pdist + + >>> X = [[0, 0], [0, 1], [1, 0], + ... [0, 4], [0, 3], [1, 4], + ... [4, 0], [3, 0], [4, 1], + ... [4, 4], [3, 4], [4, 3]] + + >>> Z = ward(pdist(X)) + + ``Z`` is a linkage matrix obtained after using the Ward clustering method + with ``X``, a dataset with 12 data points. + + >>> num_obs_linkage(Z) + 12 + + """ + xp = array_namespace(Z) + Z = _asarray(Z, order='c', xp=xp) + is_valid_linkage(Z, throw=True, name='Z') + return (Z.shape[0] + 1) + + +def correspond(Z, Y): + """ + Check for correspondence between linkage and condensed distance matrices. + + They must have the same number of original observations for + the check to succeed. + + This function is useful as a sanity check in algorithms that make + extensive use of linkage and distance matrices that must + correspond to the same set of original observations. + + Parameters + ---------- + Z : array_like + The linkage matrix to check for correspondence. + Y : array_like + The condensed distance matrix to check for correspondence. + + Returns + ------- + b : bool + A boolean indicating whether the linkage matrix and distance + matrix could possibly correspond to one another. + + See Also + -------- + linkage : for a description of what a linkage matrix is. + + Examples + -------- + >>> from scipy.cluster.hierarchy import ward, correspond + >>> from scipy.spatial.distance import pdist + + This method can be used to check if a given linkage matrix ``Z`` has been + obtained from the application of a cluster method over a dataset ``X``: + + >>> X = [[0, 0], [0, 1], [1, 0], + ... [0, 4], [0, 3], [1, 4], + ... [4, 0], [3, 0], [4, 1], + ... [4, 4], [3, 4], [4, 3]] + >>> X_condensed = pdist(X) + >>> Z = ward(X_condensed) + + Here, we can compare ``Z`` and ``X`` (in condensed form): + + >>> correspond(Z, X_condensed) + True + + """ + is_valid_linkage(Z, throw=True) + distance.is_valid_y(Y, throw=True) + xp = array_namespace(Z, Y) + Z = _asarray(Z, order='c', xp=xp) + Y = _asarray(Y, order='c', xp=xp) + return distance.num_obs_y(Y) == num_obs_linkage(Z) + + +def fcluster(Z, t, criterion='inconsistent', depth=2, R=None, monocrit=None): + """ + Form flat clusters from the hierarchical clustering defined by + the given linkage matrix. + + Parameters + ---------- + Z : ndarray + The hierarchical clustering encoded with the matrix returned + by the `linkage` function. + t : scalar + For criteria 'inconsistent', 'distance' or 'monocrit', + this is the threshold to apply when forming flat clusters. + For 'maxclust' or 'maxclust_monocrit' criteria, + this would be max number of clusters requested. + criterion : str, optional + The criterion to use in forming flat clusters. This can + be any of the following values: + + ``inconsistent`` : + If a cluster node and all its + descendants have an inconsistent value less than or equal + to `t`, then all its leaf descendants belong to the + same flat cluster. When no non-singleton cluster meets + this criterion, every node is assigned to its own + cluster. (Default) + + ``distance`` : + Forms flat clusters so that the original + observations in each flat cluster have no greater a + cophenetic distance than `t`. + + ``maxclust`` : + Finds a minimum threshold ``r`` so that + the cophenetic distance between any two original + observations in the same flat cluster is no more than + ``r`` and no more than `t` flat clusters are formed. + + ``monocrit`` : + Forms a flat cluster from a cluster node c + with index i when ``monocrit[j] <= t``. + + For example, to threshold on the maximum mean distance + as computed in the inconsistency matrix R with a + threshold of 0.8 do:: + + MR = maxRstat(Z, R, 3) + fcluster(Z, t=0.8, criterion='monocrit', monocrit=MR) + + ``maxclust_monocrit`` : + Forms a flat cluster from a + non-singleton cluster node ``c`` when ``monocrit[i] <= + r`` for all cluster indices ``i`` below and including + ``c``. ``r`` is minimized such that no more than ``t`` + flat clusters are formed. monocrit must be + monotonic. For example, to minimize the threshold t on + maximum inconsistency values so that no more than 3 flat + clusters are formed, do:: + + MI = maxinconsts(Z, R) + fcluster(Z, t=3, criterion='maxclust_monocrit', monocrit=MI) + depth : int, optional + The maximum depth to perform the inconsistency calculation. + It has no meaning for the other criteria. Default is 2. + R : ndarray, optional + The inconsistency matrix to use for the ``'inconsistent'`` + criterion. This matrix is computed if not provided. + monocrit : ndarray, optional + An array of length n-1. `monocrit[i]` is the + statistics upon which non-singleton i is thresholded. The + monocrit vector must be monotonic, i.e., given a node c with + index i, for all node indices j corresponding to nodes + below c, ``monocrit[i] >= monocrit[j]``. + + Returns + ------- + fcluster : ndarray + An array of length ``n``. ``T[i]`` is the flat cluster number to + which original observation ``i`` belongs. + + See Also + -------- + linkage : for information about hierarchical clustering methods work. + + Examples + -------- + >>> from scipy.cluster.hierarchy import ward, fcluster + >>> from scipy.spatial.distance import pdist + + All cluster linkage methods - e.g., `scipy.cluster.hierarchy.ward` + generate a linkage matrix ``Z`` as their output: + + >>> X = [[0, 0], [0, 1], [1, 0], + ... [0, 4], [0, 3], [1, 4], + ... [4, 0], [3, 0], [4, 1], + ... [4, 4], [3, 4], [4, 3]] + + >>> Z = ward(pdist(X)) + + >>> Z + array([[ 0. , 1. , 1. , 2. ], + [ 3. , 4. , 1. , 2. ], + [ 6. , 7. , 1. , 2. ], + [ 9. , 10. , 1. , 2. ], + [ 2. , 12. , 1.29099445, 3. ], + [ 5. , 13. , 1.29099445, 3. ], + [ 8. , 14. , 1.29099445, 3. ], + [11. , 15. , 1.29099445, 3. ], + [16. , 17. , 5.77350269, 6. ], + [18. , 19. , 5.77350269, 6. ], + [20. , 21. , 8.16496581, 12. ]]) + + This matrix represents a dendrogram, where the first and second elements + are the two clusters merged at each step, the third element is the + distance between these clusters, and the fourth element is the size of + the new cluster - the number of original data points included. + + `scipy.cluster.hierarchy.fcluster` can be used to flatten the + dendrogram, obtaining as a result an assignation of the original data + points to single clusters. + + This assignation mostly depends on a distance threshold ``t`` - the maximum + inter-cluster distance allowed: + + >>> fcluster(Z, t=0.9, criterion='distance') + array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], dtype=int32) + + >>> fcluster(Z, t=1.1, criterion='distance') + array([1, 1, 2, 3, 3, 4, 5, 5, 6, 7, 7, 8], dtype=int32) + + >>> fcluster(Z, t=3, criterion='distance') + array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4], dtype=int32) + + >>> fcluster(Z, t=9, criterion='distance') + array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=int32) + + In the first case, the threshold ``t`` is too small to allow any two + samples in the data to form a cluster, so 12 different clusters are + returned. + + In the second case, the threshold is large enough to allow the first + 4 points to be merged with their nearest neighbors. So, here, only 8 + clusters are returned. + + The third case, with a much higher threshold, allows for up to 8 data + points to be connected - so 4 clusters are returned here. + + Lastly, the threshold of the fourth case is large enough to allow for + all data points to be merged together - so a single cluster is returned. + + """ + xp = array_namespace(Z) + Z = _asarray(Z, order='C', dtype=xp.float64, xp=xp) + is_valid_linkage(Z, throw=True, name='Z') + + n = Z.shape[0] + 1 + T = np.zeros((n,), dtype='i') + + if monocrit is not None: + monocrit = np.asarray(monocrit, order='C', dtype=np.float64) + + Z = np.asarray(Z) + monocrit = np.asarray(monocrit) + if criterion == 'inconsistent': + if R is None: + R = inconsistent(Z, depth) + else: + R = _asarray(R, order='C', dtype=xp.float64, xp=xp) + is_valid_im(R, throw=True, name='R') + # Since the C code does not support striding using strides. + # The dimensions are used instead. + R = np.asarray(R) + _hierarchy.cluster_in(Z, R, T, float(t), int(n)) + elif criterion == 'distance': + _hierarchy.cluster_dist(Z, T, float(t), int(n)) + elif criterion == 'maxclust': + _hierarchy.cluster_maxclust_dist(Z, T, int(n), t) + elif criterion == 'monocrit': + _hierarchy.cluster_monocrit(Z, monocrit, T, float(t), int(n)) + elif criterion == 'maxclust_monocrit': + _hierarchy.cluster_maxclust_monocrit(Z, monocrit, T, int(n), int(t)) + else: + raise ValueError(f'Invalid cluster formation criterion: {str(criterion)}') + return xp.asarray(T) + + +def fclusterdata(X, t, criterion='inconsistent', + metric='euclidean', depth=2, method='single', R=None): + """ + Cluster observation data using a given metric. + + Clusters the original observations in the n-by-m data + matrix X (n observations in m dimensions), using the euclidean + distance metric to calculate distances between original observations, + performs hierarchical clustering using the single linkage algorithm, + and forms flat clusters using the inconsistency method with `t` as the + cut-off threshold. + + A 1-D array ``T`` of length ``n`` is returned. ``T[i]`` is + the index of the flat cluster to which the original observation ``i`` + belongs. + + Parameters + ---------- + X : (N, M) ndarray + N by M data matrix with N observations in M dimensions. + t : scalar + For criteria 'inconsistent', 'distance' or 'monocrit', + this is the threshold to apply when forming flat clusters. + For 'maxclust' or 'maxclust_monocrit' criteria, + this would be max number of clusters requested. + criterion : str, optional + Specifies the criterion for forming flat clusters. Valid + values are 'inconsistent' (default), 'distance', or 'maxclust' + cluster formation algorithms. See `fcluster` for descriptions. + metric : str or function, optional + The distance metric for calculating pairwise distances. See + ``distance.pdist`` for descriptions and linkage to verify + compatibility with the linkage method. + depth : int, optional + The maximum depth for the inconsistency calculation. See + `inconsistent` for more information. + method : str, optional + The linkage method to use (single, complete, average, + weighted, median centroid, ward). See `linkage` for more + information. Default is "single". + R : ndarray, optional + The inconsistency matrix. It will be computed if necessary + if it is not passed. + + Returns + ------- + fclusterdata : ndarray + A vector of length n. T[i] is the flat cluster number to + which original observation i belongs. + + See Also + -------- + scipy.spatial.distance.pdist : pairwise distance metrics + + Notes + ----- + This function is similar to the MATLAB function ``clusterdata``. + + Examples + -------- + >>> from scipy.cluster.hierarchy import fclusterdata + + This is a convenience method that abstracts all the steps to perform in a + typical SciPy's hierarchical clustering workflow. + + * Transform the input data into a condensed matrix with + `scipy.spatial.distance.pdist`. + + * Apply a clustering method. + + * Obtain flat clusters at a user defined distance threshold ``t`` using + `scipy.cluster.hierarchy.fcluster`. + + >>> X = [[0, 0], [0, 1], [1, 0], + ... [0, 4], [0, 3], [1, 4], + ... [4, 0], [3, 0], [4, 1], + ... [4, 4], [3, 4], [4, 3]] + + >>> fclusterdata(X, t=1) + array([3, 3, 3, 4, 4, 4, 2, 2, 2, 1, 1, 1], dtype=int32) + + The output here (for the dataset ``X``, distance threshold ``t``, and the + default settings) is four clusters with three data points each. + + """ + xp = array_namespace(X) + X = _asarray(X, order='C', dtype=xp.float64, xp=xp) + + if X.ndim != 2: + raise TypeError('The observation matrix X must be an n by m ' + 'array.') + + Y = distance.pdist(X, metric=metric) + Y = xp.asarray(Y) + Z = linkage(Y, method=method) + if R is None: + R = inconsistent(Z, d=depth) + else: + R = _asarray(R, order='c', xp=xp) + T = fcluster(Z, criterion=criterion, depth=depth, R=R, t=t) + return T + + +def leaves_list(Z): + """ + Return a list of leaf node ids. + + The return corresponds to the observation vector index as it appears + in the tree from left to right. Z is a linkage matrix. + + Parameters + ---------- + Z : ndarray + The hierarchical clustering encoded as a matrix. `Z` is + a linkage matrix. See `linkage` for more information. + + Returns + ------- + leaves_list : ndarray + The list of leaf node ids. + + See Also + -------- + dendrogram : for information about dendrogram structure. + + Examples + -------- + >>> from scipy.cluster.hierarchy import ward, dendrogram, leaves_list + >>> from scipy.spatial.distance import pdist + >>> from matplotlib import pyplot as plt + + >>> X = [[0, 0], [0, 1], [1, 0], + ... [0, 4], [0, 3], [1, 4], + ... [4, 0], [3, 0], [4, 1], + ... [4, 4], [3, 4], [4, 3]] + + >>> Z = ward(pdist(X)) + + The linkage matrix ``Z`` represents a dendrogram, that is, a tree that + encodes the structure of the clustering performed. + `scipy.cluster.hierarchy.leaves_list` shows the mapping between + indices in the ``X`` dataset and leaves in the dendrogram: + + >>> leaves_list(Z) + array([ 2, 0, 1, 5, 3, 4, 8, 6, 7, 11, 9, 10], dtype=int32) + + >>> fig = plt.figure(figsize=(25, 10)) + >>> dn = dendrogram(Z) + >>> plt.show() + + """ + xp = array_namespace(Z) + Z = _asarray(Z, order='C', xp=xp) + is_valid_linkage(Z, throw=True, name='Z') + n = Z.shape[0] + 1 + ML = np.zeros((n,), dtype='i') + Z = np.asarray(Z) + _hierarchy.prelist(Z, ML, n) + return xp.asarray(ML) + + +# Maps number of leaves to text size. +# +# p <= 20, size="12" +# 20 < p <= 30, size="10" +# 30 < p <= 50, size="8" +# 50 < p <= np.inf, size="6" + +_dtextsizes = {20: 12, 30: 10, 50: 8, 85: 6, np.inf: 5} +_drotation = {20: 0, 40: 45, np.inf: 90} +_dtextsortedkeys = list(_dtextsizes.keys()) +_dtextsortedkeys.sort() +_drotationsortedkeys = list(_drotation.keys()) +_drotationsortedkeys.sort() + + +def _remove_dups(L): + """ + Remove duplicates AND preserve the original order of the elements. + + The set class is not guaranteed to do this. + """ + seen_before = set() + L2 = [] + for i in L: + if i not in seen_before: + seen_before.add(i) + L2.append(i) + return L2 + + +def _get_tick_text_size(p): + for k in _dtextsortedkeys: + if p <= k: + return _dtextsizes[k] + + +def _get_tick_rotation(p): + for k in _drotationsortedkeys: + if p <= k: + return _drotation[k] + + +def _plot_dendrogram(icoords, dcoords, ivl, p, n, mh, orientation, + no_labels, color_list, leaf_font_size=None, + leaf_rotation=None, contraction_marks=None, + ax=None, above_threshold_color='C0'): + # Import matplotlib here so that it's not imported unless dendrograms + # are plotted. Raise an informative error if importing fails. + try: + # if an axis is provided, don't use pylab at all + if ax is None: + import matplotlib.pylab + import matplotlib.patches + import matplotlib.collections + except ImportError as e: + raise ImportError("You must install the matplotlib library to plot " + "the dendrogram. Use no_plot=True to calculate the " + "dendrogram without plotting.") from e + + if ax is None: + ax = matplotlib.pylab.gca() + # if we're using pylab, we want to trigger a draw at the end + trigger_redraw = True + else: + trigger_redraw = False + + # Independent variable plot width + ivw = len(ivl) * 10 + # Dependent variable plot height + dvw = mh + mh * 0.05 + + iv_ticks = np.arange(5, len(ivl) * 10 + 5, 10) + if orientation in ('top', 'bottom'): + if orientation == 'top': + ax.set_ylim([0, dvw]) + ax.set_xlim([0, ivw]) + else: + ax.set_ylim([dvw, 0]) + ax.set_xlim([0, ivw]) + + xlines = icoords + ylines = dcoords + if no_labels: + ax.set_xticks([]) + ax.set_xticklabels([]) + else: + ax.set_xticks(iv_ticks) + + if orientation == 'top': + ax.xaxis.set_ticks_position('bottom') + else: + ax.xaxis.set_ticks_position('top') + + # Make the tick marks invisible because they cover up the links + for line in ax.get_xticklines(): + line.set_visible(False) + + leaf_rot = (float(_get_tick_rotation(len(ivl))) + if (leaf_rotation is None) else leaf_rotation) + leaf_font = (float(_get_tick_text_size(len(ivl))) + if (leaf_font_size is None) else leaf_font_size) + ax.set_xticklabels(ivl, rotation=leaf_rot, size=leaf_font) + + elif orientation in ('left', 'right'): + if orientation == 'left': + ax.set_xlim([dvw, 0]) + ax.set_ylim([0, ivw]) + else: + ax.set_xlim([0, dvw]) + ax.set_ylim([0, ivw]) + + xlines = dcoords + ylines = icoords + if no_labels: + ax.set_yticks([]) + ax.set_yticklabels([]) + else: + ax.set_yticks(iv_ticks) + + if orientation == 'left': + ax.yaxis.set_ticks_position('right') + else: + ax.yaxis.set_ticks_position('left') + + # Make the tick marks invisible because they cover up the links + for line in ax.get_yticklines(): + line.set_visible(False) + + leaf_font = (float(_get_tick_text_size(len(ivl))) + if (leaf_font_size is None) else leaf_font_size) + + if leaf_rotation is not None: + ax.set_yticklabels(ivl, rotation=leaf_rotation, size=leaf_font) + else: + ax.set_yticklabels(ivl, size=leaf_font) + + # Let's use collections instead. This way there is a separate legend item + # for each tree grouping, rather than stupidly one for each line segment. + colors_used = _remove_dups(color_list) + color_to_lines = {} + for color in colors_used: + color_to_lines[color] = [] + for (xline, yline, color) in zip(xlines, ylines, color_list): + color_to_lines[color].append(list(zip(xline, yline))) + + colors_to_collections = {} + # Construct the collections. + for color in colors_used: + coll = matplotlib.collections.LineCollection(color_to_lines[color], + colors=(color,)) + colors_to_collections[color] = coll + + # Add all the groupings below the color threshold. + for color in colors_used: + if color != above_threshold_color: + ax.add_collection(colors_to_collections[color]) + # If there's a grouping of links above the color threshold, it goes last. + if above_threshold_color in colors_to_collections: + ax.add_collection(colors_to_collections[above_threshold_color]) + + if contraction_marks is not None: + Ellipse = matplotlib.patches.Ellipse + for (x, y) in contraction_marks: + if orientation in ('left', 'right'): + e = Ellipse((y, x), width=dvw / 100, height=1.0) + else: + e = Ellipse((x, y), width=1.0, height=dvw / 100) + ax.add_artist(e) + e.set_clip_box(ax.bbox) + e.set_alpha(0.5) + e.set_facecolor('k') + + if trigger_redraw: + matplotlib.pylab.draw_if_interactive() + + +# C0 is used for above threshold color +_link_line_colors_default = ('C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9') +_link_line_colors = list(_link_line_colors_default) + + +def set_link_color_palette(palette): + """ + Set list of matplotlib color codes for use by dendrogram. + + Note that this palette is global (i.e., setting it once changes the colors + for all subsequent calls to `dendrogram`) and that it affects only the + the colors below ``color_threshold``. + + Note that `dendrogram` also accepts a custom coloring function through its + ``link_color_func`` keyword, which is more flexible and non-global. + + Parameters + ---------- + palette : list of str or None + A list of matplotlib color codes. The order of the color codes is the + order in which the colors are cycled through when color thresholding in + the dendrogram. + + If ``None``, resets the palette to its default (which are matplotlib + default colors C1 to C9). + + Returns + ------- + None + + See Also + -------- + dendrogram + + Notes + ----- + Ability to reset the palette with ``None`` added in SciPy 0.17.0. + + Thread safety: using this function in a multi-threaded fashion may + result in `dendrogram` producing plots with unexpected colors. + + Examples + -------- + >>> import numpy as np + >>> from scipy.cluster import hierarchy + >>> ytdist = np.array([662., 877., 255., 412., 996., 295., 468., 268., + ... 400., 754., 564., 138., 219., 869., 669.]) + >>> Z = hierarchy.linkage(ytdist, 'single') + >>> dn = hierarchy.dendrogram(Z, no_plot=True) + >>> dn['color_list'] + ['C1', 'C0', 'C0', 'C0', 'C0'] + >>> hierarchy.set_link_color_palette(['c', 'm', 'y', 'k']) + >>> dn = hierarchy.dendrogram(Z, no_plot=True, above_threshold_color='b') + >>> dn['color_list'] + ['c', 'b', 'b', 'b', 'b'] + >>> dn = hierarchy.dendrogram(Z, no_plot=True, color_threshold=267, + ... above_threshold_color='k') + >>> dn['color_list'] + ['c', 'm', 'm', 'k', 'k'] + + Now, reset the color palette to its default: + + >>> hierarchy.set_link_color_palette(None) + + """ + if palette is None: + # reset to its default + palette = _link_line_colors_default + elif not isinstance(palette, (list, tuple)): + raise TypeError("palette must be a list or tuple") + _ptypes = [isinstance(p, str) for p in palette] + + if False in _ptypes: + raise TypeError("all palette list elements must be color strings") + + global _link_line_colors + _link_line_colors = palette + + +def dendrogram(Z, p=30, truncate_mode=None, color_threshold=None, + get_leaves=True, orientation='top', labels=None, + count_sort=False, distance_sort=False, show_leaf_counts=True, + no_plot=False, no_labels=False, leaf_font_size=None, + leaf_rotation=None, leaf_label_func=None, + show_contracted=False, link_color_func=None, ax=None, + above_threshold_color='C0'): + """ + Plot the hierarchical clustering as a dendrogram. + + The dendrogram illustrates how each cluster is + composed by drawing a U-shaped link between a non-singleton + cluster and its children. The top of the U-link indicates a + cluster merge. The two legs of the U-link indicate which clusters + were merged. The length of the two legs of the U-link represents + the distance between the child clusters. It is also the + cophenetic distance between original observations in the two + children clusters. + + Parameters + ---------- + Z : ndarray + The linkage matrix encoding the hierarchical clustering to + render as a dendrogram. See the ``linkage`` function for more + information on the format of ``Z``. + p : int, optional + The ``p`` parameter for ``truncate_mode``. + truncate_mode : str, optional + The dendrogram can be hard to read when the original + observation matrix from which the linkage is derived is + large. Truncation is used to condense the dendrogram. There + are several modes: + + ``None`` + No truncation is performed (default). + Note: ``'none'`` is an alias for ``None`` that's kept for + backward compatibility. + + ``'lastp'`` + The last ``p`` non-singleton clusters formed in the linkage are the + only non-leaf nodes in the linkage; they correspond to rows + ``Z[n-p-2:end]`` in ``Z``. All other non-singleton clusters are + contracted into leaf nodes. + + ``'level'`` + No more than ``p`` levels of the dendrogram tree are displayed. + A "level" includes all nodes with ``p`` merges from the final merge. + + Note: ``'mtica'`` is an alias for ``'level'`` that's kept for + backward compatibility. + + color_threshold : double, optional + For brevity, let :math:`t` be the ``color_threshold``. + Colors all the descendent links below a cluster node + :math:`k` the same color if :math:`k` is the first node below + the cut threshold :math:`t`. All links connecting nodes with + distances greater than or equal to the threshold are colored + with de default matplotlib color ``'C0'``. If :math:`t` is less + than or equal to zero, all nodes are colored ``'C0'``. + If ``color_threshold`` is None or 'default', + corresponding with MATLAB(TM) behavior, the threshold is set to + ``0.7*max(Z[:,2])``. + + get_leaves : bool, optional + Includes a list ``R['leaves']=H`` in the result + dictionary. For each :math:`i`, ``H[i] == j``, cluster node + ``j`` appears in position ``i`` in the left-to-right traversal + of the leaves, where :math:`j < 2n-1` and :math:`i < n`. + orientation : str, optional + The direction to plot the dendrogram, which can be any + of the following strings: + + ``'top'`` + Plots the root at the top, and plot descendent links going downwards. + (default). + + ``'bottom'`` + Plots the root at the bottom, and plot descendent links going + upwards. + + ``'left'`` + Plots the root at the left, and plot descendent links going right. + + ``'right'`` + Plots the root at the right, and plot descendent links going left. + + labels : ndarray, optional + By default, ``labels`` is None so the index of the original observation + is used to label the leaf nodes. Otherwise, this is an :math:`n`-sized + sequence, with ``n == Z.shape[0] + 1``. The ``labels[i]`` value is the + text to put under the :math:`i` th leaf node only if it corresponds to + an original observation and not a non-singleton cluster. + count_sort : str or bool, optional + For each node n, the order (visually, from left-to-right) n's + two descendent links are plotted is determined by this + parameter, which can be any of the following values: + + ``False`` + Nothing is done. + + ``'ascending'`` or ``True`` + The child with the minimum number of original objects in its cluster + is plotted first. + + ``'descending'`` + The child with the maximum number of original objects in its cluster + is plotted first. + + Note, ``distance_sort`` and ``count_sort`` cannot both be True. + distance_sort : str or bool, optional + For each node n, the order (visually, from left-to-right) n's + two descendent links are plotted is determined by this + parameter, which can be any of the following values: + + ``False`` + Nothing is done. + + ``'ascending'`` or ``True`` + The child with the minimum distance between its direct descendents is + plotted first. + + ``'descending'`` + The child with the maximum distance between its direct descendents is + plotted first. + + Note ``distance_sort`` and ``count_sort`` cannot both be True. + show_leaf_counts : bool, optional + When True, leaf nodes representing :math:`k>1` original + observation are labeled with the number of observations they + contain in parentheses. + no_plot : bool, optional + When True, the final rendering is not performed. This is + useful if only the data structures computed for the rendering + are needed or if matplotlib is not available. + no_labels : bool, optional + When True, no labels appear next to the leaf nodes in the + rendering of the dendrogram. + leaf_rotation : double, optional + Specifies the angle (in degrees) to rotate the leaf + labels. When unspecified, the rotation is based on the number of + nodes in the dendrogram (default is 0). + leaf_font_size : int, optional + Specifies the font size (in points) of the leaf labels. When + unspecified, the size based on the number of nodes in the + dendrogram. + leaf_label_func : lambda or function, optional + When ``leaf_label_func`` is a callable function, for each + leaf with cluster index :math:`k < 2n-1`. The function + is expected to return a string with the label for the + leaf. + + Indices :math:`k < n` correspond to original observations + while indices :math:`k \\geq n` correspond to non-singleton + clusters. + + For example, to label singletons with their node id and + non-singletons with their id, count, and inconsistency + coefficient, simply do:: + + # First define the leaf label function. + def llf(id): + if id < n: + return str(id) + else: + return '[%d %d %1.2f]' % (id, count, R[n-id,3]) + + # The text for the leaf nodes is going to be big so force + # a rotation of 90 degrees. + dendrogram(Z, leaf_label_func=llf, leaf_rotation=90) + + # leaf_label_func can also be used together with ``truncate_mode``, + # in which case you will get your leaves labeled after truncation: + dendrogram(Z, leaf_label_func=llf, leaf_rotation=90, + truncate_mode='level', p=2) + + show_contracted : bool, optional + When True the heights of non-singleton nodes contracted + into a leaf node are plotted as crosses along the link + connecting that leaf node. This really is only useful when + truncation is used (see ``truncate_mode`` parameter). + link_color_func : callable, optional + If given, `link_color_function` is called with each non-singleton id + corresponding to each U-shaped link it will paint. The function is + expected to return the color to paint the link, encoded as a matplotlib + color string code. For example:: + + dendrogram(Z, link_color_func=lambda k: colors[k]) + + colors the direct links below each untruncated non-singleton node + ``k`` using ``colors[k]``. + ax : matplotlib Axes instance, optional + If None and `no_plot` is not True, the dendrogram will be plotted + on the current axes. Otherwise if `no_plot` is not True the + dendrogram will be plotted on the given ``Axes`` instance. This can be + useful if the dendrogram is part of a more complex figure. + above_threshold_color : str, optional + This matplotlib color string sets the color of the links above the + color_threshold. The default is ``'C0'``. + + Returns + ------- + R : dict + A dictionary of data structures computed to render the + dendrogram. Its has the following keys: + + ``'color_list'`` + A list of color names. The k'th element represents the color of the + k'th link. + + ``'icoord'`` and ``'dcoord'`` + Each of them is a list of lists. Let ``icoord = [I1, I2, ..., Ip]`` + where ``Ik = [xk1, xk2, xk3, xk4]`` and ``dcoord = [D1, D2, ..., Dp]`` + where ``Dk = [yk1, yk2, yk3, yk4]``, then the k'th link painted is + ``(xk1, yk1)`` - ``(xk2, yk2)`` - ``(xk3, yk3)`` - ``(xk4, yk4)``. + + ``'ivl'`` + A list of labels corresponding to the leaf nodes. + + ``'leaves'`` + For each i, ``H[i] == j``, cluster node ``j`` appears in position + ``i`` in the left-to-right traversal of the leaves, where + :math:`j < 2n-1` and :math:`i < n`. If ``j`` is less than ``n``, the + ``i``-th leaf node corresponds to an original observation. + Otherwise, it corresponds to a non-singleton cluster. + + ``'leaves_color_list'`` + A list of color names. The k'th element represents the color of the + k'th leaf. + + See Also + -------- + linkage, set_link_color_palette + + Notes + ----- + It is expected that the distances in ``Z[:,2]`` be monotonic, otherwise + crossings appear in the dendrogram. + + Examples + -------- + >>> import numpy as np + >>> from scipy.cluster import hierarchy + >>> import matplotlib.pyplot as plt + + A very basic example: + + >>> ytdist = np.array([662., 877., 255., 412., 996., 295., 468., 268., + ... 400., 754., 564., 138., 219., 869., 669.]) + >>> Z = hierarchy.linkage(ytdist, 'single') + >>> plt.figure() + >>> dn = hierarchy.dendrogram(Z) + + Now, plot in given axes, improve the color scheme and use both vertical and + horizontal orientations: + + >>> hierarchy.set_link_color_palette(['m', 'c', 'y', 'k']) + >>> fig, axes = plt.subplots(1, 2, figsize=(8, 3)) + >>> dn1 = hierarchy.dendrogram(Z, ax=axes[0], above_threshold_color='y', + ... orientation='top') + >>> dn2 = hierarchy.dendrogram(Z, ax=axes[1], + ... above_threshold_color='#bcbddc', + ... orientation='right') + >>> hierarchy.set_link_color_palette(None) # reset to default after use + >>> plt.show() + + """ + # This feature was thought about but never implemented (still useful?): + # + # ... = dendrogram(..., leaves_order=None) + # + # Plots the leaves in the order specified by a vector of + # original observation indices. If the vector contains duplicates + # or results in a crossing, an exception will be thrown. Passing + # None orders leaf nodes based on the order they appear in the + # pre-order traversal. + xp = array_namespace(Z) + Z = _asarray(Z, order='c', xp=xp) + + if orientation not in ["top", "left", "bottom", "right"]: + raise ValueError("orientation must be one of 'top', 'left', " + "'bottom', or 'right'") + + if labels is not None: + try: + len_labels = len(labels) + except (TypeError, AttributeError): + len_labels = labels.shape[0] + if Z.shape[0] + 1 != len_labels: + raise ValueError("Dimensions of Z and labels must be consistent.") + + is_valid_linkage(Z, throw=True, name='Z') + Zs = Z.shape + n = Zs[0] + 1 + if isinstance(p, (int, float)): + p = int(p) + else: + raise TypeError('The second argument must be a number') + + if truncate_mode not in ('lastp', 'mtica', 'level', 'none', None): + # 'mtica' is kept working for backwards compat. + raise ValueError('Invalid truncation mode.') + + if truncate_mode == 'lastp': + if p > n or p == 0: + p = n + + if truncate_mode == 'mtica': + # 'mtica' is an alias + truncate_mode = 'level' + + if truncate_mode == 'level': + if p <= 0: + p = np.inf + + if get_leaves: + lvs = [] + else: + lvs = None + + icoord_list = [] + dcoord_list = [] + color_list = [] + current_color = [0] + currently_below_threshold = [False] + ivl = [] # list of leaves + + if color_threshold is None or (isinstance(color_threshold, str) and + color_threshold == 'default'): + color_threshold = xp.max(Z[:, 2]) * 0.7 + + R = {'icoord': icoord_list, 'dcoord': dcoord_list, 'ivl': ivl, + 'leaves': lvs, 'color_list': color_list} + + # Empty list will be filled in _dendrogram_calculate_info + contraction_marks = [] if show_contracted else None + + _dendrogram_calculate_info( + Z=Z, p=p, + truncate_mode=truncate_mode, + color_threshold=color_threshold, + get_leaves=get_leaves, + orientation=orientation, + labels=labels, + count_sort=count_sort, + distance_sort=distance_sort, + show_leaf_counts=show_leaf_counts, + i=2*n - 2, + iv=0.0, + ivl=ivl, + n=n, + icoord_list=icoord_list, + dcoord_list=dcoord_list, + lvs=lvs, + current_color=current_color, + color_list=color_list, + currently_below_threshold=currently_below_threshold, + leaf_label_func=leaf_label_func, + contraction_marks=contraction_marks, + link_color_func=link_color_func, + above_threshold_color=above_threshold_color) + + if not no_plot: + mh = xp.max(Z[:, 2]) + _plot_dendrogram(icoord_list, dcoord_list, ivl, p, n, mh, orientation, + no_labels, color_list, + leaf_font_size=leaf_font_size, + leaf_rotation=leaf_rotation, + contraction_marks=contraction_marks, + ax=ax, + above_threshold_color=above_threshold_color) + + R["leaves_color_list"] = _get_leaves_color_list(R) + + return R + + +def _get_leaves_color_list(R): + leaves_color_list = [None] * len(R['leaves']) + for link_x, link_y, link_color in zip(R['icoord'], + R['dcoord'], + R['color_list']): + for (xi, yi) in zip(link_x, link_y): + if yi == 0.0 and (xi % 5 == 0 and xi % 2 == 1): + # if yi is 0.0 and xi is divisible by 5 and odd, + # the point is a leaf + # xi of leaves are 5, 15, 25, 35, ... (see `iv_ticks`) + # index of leaves are 0, 1, 2, 3, ... as below + leaf_index = (int(xi) - 5) // 10 + # each leaf has a same color of its link. + leaves_color_list[leaf_index] = link_color + return leaves_color_list + + +def _append_singleton_leaf_node(Z, p, n, level, lvs, ivl, leaf_label_func, + i, labels): + # If the leaf id structure is not None and is a list then the caller + # to dendrogram has indicated that cluster id's corresponding to the + # leaf nodes should be recorded. + + if lvs is not None: + lvs.append(int(i)) + + # If leaf node labels are to be displayed... + if ivl is not None: + # If a leaf_label_func has been provided, the label comes from the + # string returned from the leaf_label_func, which is a function + # passed to dendrogram. + if leaf_label_func: + ivl.append(leaf_label_func(int(i))) + else: + # Otherwise, if the dendrogram caller has passed a labels list + # for the leaf nodes, use it. + if labels is not None: + ivl.append(labels[int(i - n)]) + else: + # Otherwise, use the id as the label for the leaf.x + ivl.append(str(int(i))) + + +def _append_nonsingleton_leaf_node(Z, p, n, level, lvs, ivl, leaf_label_func, + i, labels, show_leaf_counts): + # If the leaf id structure is not None and is a list then the caller + # to dendrogram has indicated that cluster id's corresponding to the + # leaf nodes should be recorded. + + if lvs is not None: + lvs.append(int(i)) + if ivl is not None: + if leaf_label_func: + ivl.append(leaf_label_func(int(i))) + else: + if show_leaf_counts: + ivl.append("(" + str(np.asarray(Z[i - n, 3], dtype=np.int64)) + ")") + else: + ivl.append("") + + +def _append_contraction_marks(Z, iv, i, n, contraction_marks, xp): + _append_contraction_marks_sub(Z, iv, int_floor(Z[i - n, 0], xp), + n, contraction_marks, xp) + _append_contraction_marks_sub(Z, iv, int_floor(Z[i - n, 1], xp), + n, contraction_marks, xp) + + +def _append_contraction_marks_sub(Z, iv, i, n, contraction_marks, xp): + if i >= n: + contraction_marks.append((iv, Z[i - n, 2])) + _append_contraction_marks_sub(Z, iv, int_floor(Z[i - n, 0], xp), + n, contraction_marks, xp) + _append_contraction_marks_sub(Z, iv, int_floor(Z[i - n, 1], xp), + n, contraction_marks, xp) + + +def _dendrogram_calculate_info(Z, p, truncate_mode, + color_threshold=np.inf, get_leaves=True, + orientation='top', labels=None, + count_sort=False, distance_sort=False, + show_leaf_counts=False, i=-1, iv=0.0, + ivl=None, n=0, icoord_list=None, dcoord_list=None, + lvs=None, mhr=False, + current_color=None, color_list=None, + currently_below_threshold=None, + leaf_label_func=None, level=0, + contraction_marks=None, + link_color_func=None, + above_threshold_color='C0'): + """ + Calculate the endpoints of the links as well as the labels for the + the dendrogram rooted at the node with index i. iv is the independent + variable value to plot the left-most leaf node below the root node i + (if orientation='top', this would be the left-most x value where the + plotting of this root node i and its descendents should begin). + + ivl is a list to store the labels of the leaf nodes. The leaf_label_func + is called whenever ivl != None, labels == None, and + leaf_label_func != None. When ivl != None and labels != None, the + labels list is used only for labeling the leaf nodes. When + ivl == None, no labels are generated for leaf nodes. + + When get_leaves==True, a list of leaves is built as they are visited + in the dendrogram. + + Returns a tuple with l being the independent variable coordinate that + corresponds to the midpoint of cluster to the left of cluster i if + i is non-singleton, otherwise the independent coordinate of the leaf + node if i is a leaf node. + + Returns + ------- + A tuple (left, w, h, md), where: + * left is the independent variable coordinate of the center of the + the U of the subtree + + * w is the amount of space used for the subtree (in independent + variable units) + + * h is the height of the subtree in dependent variable units + + * md is the ``max(Z[*,2]``) for all nodes ``*`` below and including + the target node. + + """ + xp = array_namespace(Z) + if n == 0: + raise ValueError("Invalid singleton cluster count n.") + + if i == -1: + raise ValueError("Invalid root cluster index i.") + + if truncate_mode == 'lastp': + # If the node is a leaf node but corresponds to a non-singleton + # cluster, its label is either the empty string or the number of + # original observations belonging to cluster i. + if 2*n - p > i >= n: + d = Z[i - n, 2] + _append_nonsingleton_leaf_node(Z, p, n, level, lvs, ivl, + leaf_label_func, i, labels, + show_leaf_counts) + if contraction_marks is not None: + _append_contraction_marks(Z, iv + 5.0, i, n, contraction_marks, xp) + return (iv + 5.0, 10.0, 0.0, d) + elif i < n: + _append_singleton_leaf_node(Z, p, n, level, lvs, ivl, + leaf_label_func, i, labels) + return (iv + 5.0, 10.0, 0.0, 0.0) + elif truncate_mode == 'level': + if i > n and level > p: + d = Z[i - n, 2] + _append_nonsingleton_leaf_node(Z, p, n, level, lvs, ivl, + leaf_label_func, i, labels, + show_leaf_counts) + if contraction_marks is not None: + _append_contraction_marks(Z, iv + 5.0, i, n, contraction_marks, xp) + return (iv + 5.0, 10.0, 0.0, d) + elif i < n: + _append_singleton_leaf_node(Z, p, n, level, lvs, ivl, + leaf_label_func, i, labels) + return (iv + 5.0, 10.0, 0.0, 0.0) + + # Otherwise, only truncate if we have a leaf node. + # + # Only place leaves if they correspond to original observations. + if i < n: + _append_singleton_leaf_node(Z, p, n, level, lvs, ivl, + leaf_label_func, i, labels) + return (iv + 5.0, 10.0, 0.0, 0.0) + + # !!! Otherwise, we don't have a leaf node, so work on plotting a + # non-leaf node. + # Actual indices of a and b + aa = int_floor(Z[i - n, 0], xp) + ab = int_floor(Z[i - n, 1], xp) + if aa >= n: + # The number of singletons below cluster a + na = Z[aa - n, 3] + # The distance between a's two direct children. + da = Z[aa - n, 2] + else: + na = 1 + da = 0.0 + if ab >= n: + nb = Z[ab - n, 3] + db = Z[ab - n, 2] + else: + nb = 1 + db = 0.0 + + if count_sort == 'ascending' or count_sort is True: + # If a has a count greater than b, it and its descendents should + # be drawn to the right. Otherwise, to the left. + if na > nb: + # The cluster index to draw to the left (ua) will be ab + # and the one to draw to the right (ub) will be aa + ua = ab + ub = aa + else: + ua = aa + ub = ab + elif count_sort == 'descending': + # If a has a count less than or equal to b, it and its + # descendents should be drawn to the left. Otherwise, to + # the right. + if na > nb: + ua = aa + ub = ab + else: + ua = ab + ub = aa + elif distance_sort == 'ascending' or distance_sort is True: + # If a has a distance greater than b, it and its descendents should + # be drawn to the right. Otherwise, to the left. + if da > db: + ua = ab + ub = aa + else: + ua = aa + ub = ab + elif distance_sort == 'descending': + # If a has a distance less than or equal to b, it and its + # descendents should be drawn to the left. Otherwise, to + # the right. + if da > db: + ua = aa + ub = ab + else: + ua = ab + ub = aa + else: + ua = aa + ub = ab + + # Updated iv variable and the amount of space used. + (uiva, uwa, uah, uamd) = \ + _dendrogram_calculate_info( + Z=Z, p=p, + truncate_mode=truncate_mode, + color_threshold=color_threshold, + get_leaves=get_leaves, + orientation=orientation, + labels=labels, + count_sort=count_sort, + distance_sort=distance_sort, + show_leaf_counts=show_leaf_counts, + i=ua, iv=iv, ivl=ivl, n=n, + icoord_list=icoord_list, + dcoord_list=dcoord_list, lvs=lvs, + current_color=current_color, + color_list=color_list, + currently_below_threshold=currently_below_threshold, + leaf_label_func=leaf_label_func, + level=level + 1, contraction_marks=contraction_marks, + link_color_func=link_color_func, + above_threshold_color=above_threshold_color) + + h = Z[i - n, 2] + if h >= color_threshold or color_threshold <= 0: + c = above_threshold_color + + if currently_below_threshold[0]: + current_color[0] = (current_color[0] + 1) % len(_link_line_colors) + currently_below_threshold[0] = False + else: + currently_below_threshold[0] = True + c = _link_line_colors[current_color[0]] + + (uivb, uwb, ubh, ubmd) = \ + _dendrogram_calculate_info( + Z=Z, p=p, + truncate_mode=truncate_mode, + color_threshold=color_threshold, + get_leaves=get_leaves, + orientation=orientation, + labels=labels, + count_sort=count_sort, + distance_sort=distance_sort, + show_leaf_counts=show_leaf_counts, + i=ub, iv=iv + uwa, ivl=ivl, n=n, + icoord_list=icoord_list, + dcoord_list=dcoord_list, lvs=lvs, + current_color=current_color, + color_list=color_list, + currently_below_threshold=currently_below_threshold, + leaf_label_func=leaf_label_func, + level=level + 1, contraction_marks=contraction_marks, + link_color_func=link_color_func, + above_threshold_color=above_threshold_color) + + max_dist = max(uamd, ubmd, h) + + icoord_list.append([uiva, uiva, uivb, uivb]) + dcoord_list.append([uah, h, h, ubh]) + if link_color_func is not None: + v = link_color_func(int(i)) + if not isinstance(v, str): + raise TypeError("link_color_func must return a matplotlib " + "color string!") + color_list.append(v) + else: + color_list.append(c) + + return (((uiva + uivb) / 2), uwa + uwb, h, max_dist) + + +def is_isomorphic(T1, T2): + """ + Determine if two different cluster assignments are equivalent. + + Parameters + ---------- + T1 : array_like + An assignment of singleton cluster ids to flat cluster ids. + T2 : array_like + An assignment of singleton cluster ids to flat cluster ids. + + Returns + ------- + b : bool + Whether the flat cluster assignments `T1` and `T2` are + equivalent. + + See Also + -------- + linkage : for a description of what a linkage matrix is. + fcluster : for the creation of flat cluster assignments. + + Examples + -------- + >>> from scipy.cluster.hierarchy import fcluster, is_isomorphic + >>> from scipy.cluster.hierarchy import single, complete + >>> from scipy.spatial.distance import pdist + + Two flat cluster assignments can be isomorphic if they represent the same + cluster assignment, with different labels. + + For example, we can use the `scipy.cluster.hierarchy.single`: method + and flatten the output to four clusters: + + >>> X = [[0, 0], [0, 1], [1, 0], + ... [0, 4], [0, 3], [1, 4], + ... [4, 0], [3, 0], [4, 1], + ... [4, 4], [3, 4], [4, 3]] + + >>> Z = single(pdist(X)) + >>> T = fcluster(Z, 1, criterion='distance') + >>> T + array([3, 3, 3, 4, 4, 4, 2, 2, 2, 1, 1, 1], dtype=int32) + + We can then do the same using the + `scipy.cluster.hierarchy.complete`: method: + + >>> Z = complete(pdist(X)) + >>> T_ = fcluster(Z, 1.5, criterion='distance') + >>> T_ + array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4], dtype=int32) + + As we can see, in both cases we obtain four clusters and all the data + points are distributed in the same way - the only thing that changes + are the flat cluster labels (3 => 1, 4 =>2, 2 =>3 and 4 =>1), so both + cluster assignments are isomorphic: + + >>> is_isomorphic(T, T_) + True + + """ + T1 = np.asarray(T1, order='c') + T2 = np.asarray(T2, order='c') + + T1S = T1.shape + T2S = T2.shape + + if len(T1S) != 1: + raise ValueError('T1 must be one-dimensional.') + if len(T2S) != 1: + raise ValueError('T2 must be one-dimensional.') + if T1S[0] != T2S[0]: + raise ValueError('T1 and T2 must have the same number of elements.') + n = T1S[0] + d1 = {} + d2 = {} + for i in range(0, n): + if T1[i] in d1: + if T2[i] not in d2: + return False + if d1[T1[i]] != T2[i] or d2[T2[i]] != T1[i]: + return False + elif T2[i] in d2: + return False + else: + d1[T1[i]] = T2[i] + d2[T2[i]] = T1[i] + return True + + +def maxdists(Z): + """ + Return the maximum distance between any non-singleton cluster. + + Parameters + ---------- + Z : ndarray + The hierarchical clustering encoded as a matrix. See + ``linkage`` for more information. + + Returns + ------- + maxdists : ndarray + A ``(n-1)`` sized numpy array of doubles; ``MD[i]`` represents + the maximum distance between any cluster (including + singletons) below and including the node with index i. More + specifically, ``MD[i] = Z[Q(i)-n, 2].max()`` where ``Q(i)`` is the + set of all node indices below and including node i. + + See Also + -------- + linkage : for a description of what a linkage matrix is. + is_monotonic : for testing for monotonicity of a linkage matrix. + + Examples + -------- + >>> from scipy.cluster.hierarchy import median, maxdists + >>> from scipy.spatial.distance import pdist + + Given a linkage matrix ``Z``, `scipy.cluster.hierarchy.maxdists` + computes for each new cluster generated (i.e., for each row of the linkage + matrix) what is the maximum distance between any two child clusters. + + Due to the nature of hierarchical clustering, in many cases this is going + to be just the distance between the two child clusters that were merged + to form the current one - that is, Z[:,2]. + + However, for non-monotonic cluster assignments such as + `scipy.cluster.hierarchy.median` clustering this is not always the + case: There may be cluster formations were the distance between the two + clusters merged is smaller than the distance between their children. + + We can see this in an example: + + >>> X = [[0, 0], [0, 1], [1, 0], + ... [0, 4], [0, 3], [1, 4], + ... [4, 0], [3, 0], [4, 1], + ... [4, 4], [3, 4], [4, 3]] + + >>> Z = median(pdist(X)) + >>> Z + array([[ 0. , 1. , 1. , 2. ], + [ 3. , 4. , 1. , 2. ], + [ 9. , 10. , 1. , 2. ], + [ 6. , 7. , 1. , 2. ], + [ 2. , 12. , 1.11803399, 3. ], + [ 5. , 13. , 1.11803399, 3. ], + [ 8. , 15. , 1.11803399, 3. ], + [11. , 14. , 1.11803399, 3. ], + [18. , 19. , 3. , 6. ], + [16. , 17. , 3.5 , 6. ], + [20. , 21. , 3.25 , 12. ]]) + >>> maxdists(Z) + array([1. , 1. , 1. , 1. , 1.11803399, + 1.11803399, 1.11803399, 1.11803399, 3. , 3.5 , + 3.5 ]) + + Note that while the distance between the two clusters merged when creating the + last cluster is 3.25, there are two children (clusters 16 and 17) whose distance + is larger (3.5). Thus, `scipy.cluster.hierarchy.maxdists` returns 3.5 in + this case. + + """ + xp = array_namespace(Z) + Z = _asarray(Z, order='C', dtype=xp.float64, xp=xp) + is_valid_linkage(Z, throw=True, name='Z') + + n = Z.shape[0] + 1 + MD = np.zeros((n - 1,)) + Z = np.asarray(Z) + _hierarchy.get_max_dist_for_each_cluster(Z, MD, int(n)) + MD = xp.asarray(MD) + return MD + + +def maxinconsts(Z, R): + """ + Return the maximum inconsistency coefficient for each + non-singleton cluster and its children. + + Parameters + ---------- + Z : ndarray + The hierarchical clustering encoded as a matrix. See + `linkage` for more information. + R : ndarray + The inconsistency matrix. + + Returns + ------- + MI : ndarray + A monotonic ``(n-1)``-sized numpy array of doubles. + + See Also + -------- + linkage : for a description of what a linkage matrix is. + inconsistent : for the creation of a inconsistency matrix. + + Examples + -------- + >>> from scipy.cluster.hierarchy import median, inconsistent, maxinconsts + >>> from scipy.spatial.distance import pdist + + Given a data set ``X``, we can apply a clustering method to obtain a + linkage matrix ``Z``. `scipy.cluster.hierarchy.inconsistent` can + be also used to obtain the inconsistency matrix ``R`` associated to + this clustering process: + + >>> X = [[0, 0], [0, 1], [1, 0], + ... [0, 4], [0, 3], [1, 4], + ... [4, 0], [3, 0], [4, 1], + ... [4, 4], [3, 4], [4, 3]] + + >>> Z = median(pdist(X)) + >>> R = inconsistent(Z) + >>> Z + array([[ 0. , 1. , 1. , 2. ], + [ 3. , 4. , 1. , 2. ], + [ 9. , 10. , 1. , 2. ], + [ 6. , 7. , 1. , 2. ], + [ 2. , 12. , 1.11803399, 3. ], + [ 5. , 13. , 1.11803399, 3. ], + [ 8. , 15. , 1.11803399, 3. ], + [11. , 14. , 1.11803399, 3. ], + [18. , 19. , 3. , 6. ], + [16. , 17. , 3.5 , 6. ], + [20. , 21. , 3.25 , 12. ]]) + >>> R + array([[1. , 0. , 1. , 0. ], + [1. , 0. , 1. , 0. ], + [1. , 0. , 1. , 0. ], + [1. , 0. , 1. , 0. ], + [1.05901699, 0.08346263, 2. , 0.70710678], + [1.05901699, 0.08346263, 2. , 0.70710678], + [1.05901699, 0.08346263, 2. , 0.70710678], + [1.05901699, 0.08346263, 2. , 0.70710678], + [1.74535599, 1.08655358, 3. , 1.15470054], + [1.91202266, 1.37522872, 3. , 1.15470054], + [3.25 , 0.25 , 3. , 0. ]]) + + Here, `scipy.cluster.hierarchy.maxinconsts` can be used to compute + the maximum value of the inconsistency statistic (the last column of + ``R``) for each non-singleton cluster and its children: + + >>> maxinconsts(Z, R) + array([0. , 0. , 0. , 0. , 0.70710678, + 0.70710678, 0.70710678, 0.70710678, 1.15470054, 1.15470054, + 1.15470054]) + + """ + xp = array_namespace(Z, R) + Z = _asarray(Z, order='C', dtype=xp.float64, xp=xp) + R = _asarray(R, order='C', dtype=xp.float64, xp=xp) + is_valid_linkage(Z, throw=True, name='Z') + is_valid_im(R, throw=True, name='R') + + n = Z.shape[0] + 1 + if Z.shape[0] != R.shape[0]: + raise ValueError("The inconsistency matrix and linkage matrix each " + "have a different number of rows.") + MI = np.zeros((n - 1,)) + Z = np.asarray(Z) + R = np.asarray(R) + _hierarchy.get_max_Rfield_for_each_cluster(Z, R, MI, int(n), 3) + MI = xp.asarray(MI) + return MI + + +def maxRstat(Z, R, i): + """ + Return the maximum statistic for each non-singleton cluster and its + children. + + Parameters + ---------- + Z : array_like + The hierarchical clustering encoded as a matrix. See `linkage` for more + information. + R : array_like + The inconsistency matrix. + i : int + The column of `R` to use as the statistic. + + Returns + ------- + MR : ndarray + Calculates the maximum statistic for the i'th column of the + inconsistency matrix `R` for each non-singleton cluster + node. ``MR[j]`` is the maximum over ``R[Q(j)-n, i]``, where + ``Q(j)`` the set of all node ids corresponding to nodes below + and including ``j``. + + See Also + -------- + linkage : for a description of what a linkage matrix is. + inconsistent : for the creation of a inconsistency matrix. + + Examples + -------- + >>> from scipy.cluster.hierarchy import median, inconsistent, maxRstat + >>> from scipy.spatial.distance import pdist + + Given a data set ``X``, we can apply a clustering method to obtain a + linkage matrix ``Z``. `scipy.cluster.hierarchy.inconsistent` can + be also used to obtain the inconsistency matrix ``R`` associated to + this clustering process: + + >>> X = [[0, 0], [0, 1], [1, 0], + ... [0, 4], [0, 3], [1, 4], + ... [4, 0], [3, 0], [4, 1], + ... [4, 4], [3, 4], [4, 3]] + + >>> Z = median(pdist(X)) + >>> R = inconsistent(Z) + >>> R + array([[1. , 0. , 1. , 0. ], + [1. , 0. , 1. , 0. ], + [1. , 0. , 1. , 0. ], + [1. , 0. , 1. , 0. ], + [1.05901699, 0.08346263, 2. , 0.70710678], + [1.05901699, 0.08346263, 2. , 0.70710678], + [1.05901699, 0.08346263, 2. , 0.70710678], + [1.05901699, 0.08346263, 2. , 0.70710678], + [1.74535599, 1.08655358, 3. , 1.15470054], + [1.91202266, 1.37522872, 3. , 1.15470054], + [3.25 , 0.25 , 3. , 0. ]]) + + `scipy.cluster.hierarchy.maxRstat` can be used to compute + the maximum value of each column of ``R``, for each non-singleton + cluster and its children: + + >>> maxRstat(Z, R, 0) + array([1. , 1. , 1. , 1. , 1.05901699, + 1.05901699, 1.05901699, 1.05901699, 1.74535599, 1.91202266, + 3.25 ]) + >>> maxRstat(Z, R, 1) + array([0. , 0. , 0. , 0. , 0.08346263, + 0.08346263, 0.08346263, 0.08346263, 1.08655358, 1.37522872, + 1.37522872]) + >>> maxRstat(Z, R, 3) + array([0. , 0. , 0. , 0. , 0.70710678, + 0.70710678, 0.70710678, 0.70710678, 1.15470054, 1.15470054, + 1.15470054]) + + """ + xp = array_namespace(Z, R) + Z = _asarray(Z, order='C', dtype=xp.float64, xp=xp) + R = _asarray(R, order='C', dtype=xp.float64, xp=xp) + is_valid_linkage(Z, throw=True, name='Z') + is_valid_im(R, throw=True, name='R') + + if not isinstance(i, int): + raise TypeError('The third argument must be an integer.') + + if i < 0 or i > 3: + raise ValueError('i must be an integer between 0 and 3 inclusive.') + + if Z.shape[0] != R.shape[0]: + raise ValueError("The inconsistency matrix and linkage matrix each " + "have a different number of rows.") + + n = Z.shape[0] + 1 + MR = np.zeros((n - 1,)) + Z = np.asarray(Z) + R = np.asarray(R) + _hierarchy.get_max_Rfield_for_each_cluster(Z, R, MR, int(n), i) + MR = xp.asarray(MR) + return MR + + +def leaders(Z, T): + """ + Return the root nodes in a hierarchical clustering. + + Returns the root nodes in a hierarchical clustering corresponding + to a cut defined by a flat cluster assignment vector ``T``. See + the ``fcluster`` function for more information on the format of ``T``. + + For each flat cluster :math:`j` of the :math:`k` flat clusters + represented in the n-sized flat cluster assignment vector ``T``, + this function finds the lowest cluster node :math:`i` in the linkage + tree Z, such that: + + * leaf descendants belong only to flat cluster j + (i.e., ``T[p]==j`` for all :math:`p` in :math:`S(i)`, where + :math:`S(i)` is the set of leaf ids of descendant leaf nodes + with cluster node :math:`i`) + + * there does not exist a leaf that is not a descendant with + :math:`i` that also belongs to cluster :math:`j` + (i.e., ``T[q]!=j`` for all :math:`q` not in :math:`S(i)`). If + this condition is violated, ``T`` is not a valid cluster + assignment vector, and an exception will be thrown. + + Parameters + ---------- + Z : ndarray + The hierarchical clustering encoded as a matrix. See + `linkage` for more information. + T : ndarray + The flat cluster assignment vector. + + Returns + ------- + L : ndarray + The leader linkage node id's stored as a k-element 1-D array, + where ``k`` is the number of flat clusters found in ``T``. + + ``L[j]=i`` is the linkage cluster node id that is the + leader of flat cluster with id M[j]. If ``i < n``, ``i`` + corresponds to an original observation, otherwise it + corresponds to a non-singleton cluster. + M : ndarray + The leader linkage node id's stored as a k-element 1-D array, where + ``k`` is the number of flat clusters found in ``T``. This allows the + set of flat cluster ids to be any arbitrary set of ``k`` integers. + + For example: if ``L[3]=2`` and ``M[3]=8``, the flat cluster with + id 8's leader is linkage node 2. + + See Also + -------- + fcluster : for the creation of flat cluster assignments. + + Examples + -------- + >>> from scipy.cluster.hierarchy import ward, fcluster, leaders + >>> from scipy.spatial.distance import pdist + + Given a linkage matrix ``Z`` - obtained after apply a clustering method + to a dataset ``X`` - and a flat cluster assignment array ``T``: + + >>> X = [[0, 0], [0, 1], [1, 0], + ... [0, 4], [0, 3], [1, 4], + ... [4, 0], [3, 0], [4, 1], + ... [4, 4], [3, 4], [4, 3]] + + >>> Z = ward(pdist(X)) + >>> Z + array([[ 0. , 1. , 1. , 2. ], + [ 3. , 4. , 1. , 2. ], + [ 6. , 7. , 1. , 2. ], + [ 9. , 10. , 1. , 2. ], + [ 2. , 12. , 1.29099445, 3. ], + [ 5. , 13. , 1.29099445, 3. ], + [ 8. , 14. , 1.29099445, 3. ], + [11. , 15. , 1.29099445, 3. ], + [16. , 17. , 5.77350269, 6. ], + [18. , 19. , 5.77350269, 6. ], + [20. , 21. , 8.16496581, 12. ]]) + + >>> T = fcluster(Z, 3, criterion='distance') + >>> T + array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4], dtype=int32) + + `scipy.cluster.hierarchy.leaders` returns the indices of the nodes + in the dendrogram that are the leaders of each flat cluster: + + >>> L, M = leaders(Z, T) + >>> L + array([16, 17, 18, 19], dtype=int32) + + (remember that indices 0-11 point to the 12 data points in ``X``, + whereas indices 12-22 point to the 11 rows of ``Z``) + + `scipy.cluster.hierarchy.leaders` also returns the indices of + the flat clusters in ``T``: + + >>> M + array([1, 2, 3, 4], dtype=int32) + + """ + xp = array_namespace(Z, T) + Z = _asarray(Z, order='C', dtype=xp.float64, xp=xp) + T = _asarray(T, order='C', xp=xp) + is_valid_linkage(Z, throw=True, name='Z') + + if T.dtype != xp.int32: + raise TypeError('T must be a 1-D array of dtype int32.') + + if T.shape[0] != Z.shape[0] + 1: + raise ValueError('Mismatch: len(T)!=Z.shape[0] + 1.') + + n_clusters = int(xp.unique_values(T).shape[0]) + n_obs = int(Z.shape[0] + 1) + L = np.zeros(n_clusters, dtype=np.int32) + M = np.zeros(n_clusters, dtype=np.int32) + Z = np.asarray(Z) + T = np.asarray(T, dtype=np.int32) + s = _hierarchy.leaders(Z, T, L, M, n_clusters, n_obs) + if s >= 0: + raise ValueError(('T is not a valid assignment vector. Error found ' + 'when examining linkage node %d (< 2n-1).') % s) + L, M = xp.asarray(L), xp.asarray(M) + return (L, M) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/tests/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/tests/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/tests/hierarchy_test_data.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/tests/hierarchy_test_data.py new file mode 100644 index 0000000000000000000000000000000000000000..7d874ca5eb7141a44559307d1c28dd412171396f --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/tests/hierarchy_test_data.py @@ -0,0 +1,145 @@ +from numpy import array + + +Q_X = array([[5.26563660e-01, 3.14160190e-01, 8.00656370e-02], + [7.50205180e-01, 4.60299830e-01, 8.98696460e-01], + [6.65461230e-01, 6.94011420e-01, 9.10465700e-01], + [9.64047590e-01, 1.43082200e-03, 7.39874220e-01], + [1.08159060e-01, 5.53028790e-01, 6.63804780e-02], + [9.31359130e-01, 8.25424910e-01, 9.52315440e-01], + [6.78086960e-01, 3.41903970e-01, 5.61481950e-01], + [9.82730940e-01, 7.04605210e-01, 8.70978630e-02], + [6.14691610e-01, 4.69989230e-02, 6.02406450e-01], + [5.80161260e-01, 9.17354970e-01, 5.88163850e-01], + [1.38246310e+00, 1.96358160e+00, 1.94437880e+00], + [2.10675860e+00, 1.67148730e+00, 1.34854480e+00], + [1.39880070e+00, 1.66142050e+00, 1.32224550e+00], + [1.71410460e+00, 1.49176380e+00, 1.45432170e+00], + [1.54102340e+00, 1.84374950e+00, 1.64658950e+00], + [2.08512480e+00, 1.84524350e+00, 2.17340850e+00], + [1.30748740e+00, 1.53801650e+00, 2.16007740e+00], + [1.41447700e+00, 1.99329070e+00, 1.99107420e+00], + [1.61943490e+00, 1.47703280e+00, 1.89788160e+00], + [1.59880600e+00, 1.54988980e+00, 1.57563350e+00], + [3.37247380e+00, 2.69635310e+00, 3.39981700e+00], + [3.13705120e+00, 3.36528090e+00, 3.06089070e+00], + [3.29413250e+00, 3.19619500e+00, 2.90700170e+00], + [2.65510510e+00, 3.06785900e+00, 2.97198540e+00], + [3.30941040e+00, 2.59283970e+00, 2.57714110e+00], + [2.59557220e+00, 3.33477370e+00, 3.08793190e+00], + [2.58206180e+00, 3.41615670e+00, 3.26441990e+00], + [2.71127000e+00, 2.77032450e+00, 2.63466500e+00], + [2.79617850e+00, 3.25473720e+00, 3.41801560e+00], + [2.64741750e+00, 2.54538040e+00, 3.25354110e+00]]) + +ytdist = array([662., 877., 255., 412., 996., 295., 468., 268., 400., 754., + 564., 138., 219., 869., 669.]) + +linkage_ytdist_single = array([[2., 5., 138., 2.], + [3., 4., 219., 2.], + [0., 7., 255., 3.], + [1., 8., 268., 4.], + [6., 9., 295., 6.]]) + +linkage_ytdist_complete = array([[2., 5., 138., 2.], + [3., 4., 219., 2.], + [1., 6., 400., 3.], + [0., 7., 412., 3.], + [8., 9., 996., 6.]]) + +linkage_ytdist_average = array([[2., 5., 138., 2.], + [3., 4., 219., 2.], + [0., 7., 333.5, 3.], + [1., 6., 347.5, 3.], + [8., 9., 680.77777778, 6.]]) + +linkage_ytdist_weighted = array([[2., 5., 138., 2.], + [3., 4., 219., 2.], + [0., 7., 333.5, 3.], + [1., 6., 347.5, 3.], + [8., 9., 670.125, 6.]]) + +# the optimal leaf ordering of linkage_ytdist_single +linkage_ytdist_single_olo = array([[5., 2., 138., 2.], + [4., 3., 219., 2.], + [7., 0., 255., 3.], + [1., 8., 268., 4.], + [6., 9., 295., 6.]]) + +X = array([[1.43054825, -7.5693489], + [6.95887839, 6.82293382], + [2.87137846, -9.68248579], + [7.87974764, -6.05485803], + [8.24018364, -6.09495602], + [7.39020262, 8.54004355]]) + +linkage_X_centroid = array([[3., 4., 0.36265956, 2.], + [1., 5., 1.77045373, 2.], + [0., 2., 2.55760419, 2.], + [6., 8., 6.43614494, 4.], + [7., 9., 15.17363237, 6.]]) + +linkage_X_median = array([[3., 4., 0.36265956, 2.], + [1., 5., 1.77045373, 2.], + [0., 2., 2.55760419, 2.], + [6., 8., 6.43614494, 4.], + [7., 9., 15.17363237, 6.]]) + +linkage_X_ward = array([[3., 4., 0.36265956, 2.], + [1., 5., 1.77045373, 2.], + [0., 2., 2.55760419, 2.], + [6., 8., 9.10208346, 4.], + [7., 9., 24.7784379, 6.]]) + +# the optimal leaf ordering of linkage_X_ward +linkage_X_ward_olo = array([[4., 3., 0.36265956, 2.], + [5., 1., 1.77045373, 2.], + [2., 0., 2.55760419, 2.], + [6., 8., 9.10208346, 4.], + [7., 9., 24.7784379, 6.]]) + +inconsistent_ytdist = { + 1: array([[138., 0., 1., 0.], + [219., 0., 1., 0.], + [255., 0., 1., 0.], + [268., 0., 1., 0.], + [295., 0., 1., 0.]]), + 2: array([[138., 0., 1., 0.], + [219., 0., 1., 0.], + [237., 25.45584412, 2., 0.70710678], + [261.5, 9.19238816, 2., 0.70710678], + [233.66666667, 83.9424406, 3., 0.7306594]]), + 3: array([[138., 0., 1., 0.], + [219., 0., 1., 0.], + [237., 25.45584412, 2., 0.70710678], + [247.33333333, 25.38372182, 3., 0.81417007], + [239., 69.36377537, 4., 0.80733783]]), + 4: array([[138., 0., 1., 0.], + [219., 0., 1., 0.], + [237., 25.45584412, 2., 0.70710678], + [247.33333333, 25.38372182, 3., 0.81417007], + [235., 60.73302232, 5., 0.98793042]])} + +fcluster_inconsistent = { + 0.8: array([6, 2, 2, 4, 6, 2, 3, 7, 3, 5, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1]), + 1.0: array([6, 2, 2, 4, 6, 2, 3, 7, 3, 5, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1]), + 2.0: array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1])} + +fcluster_distance = { + 0.6: array([4, 4, 4, 4, 4, 4, 4, 5, 4, 4, 6, 6, 6, 6, 6, 7, 6, 6, 6, 6, 3, + 1, 1, 1, 2, 1, 1, 1, 1, 1]), + 1.0: array([2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1]), + 2.0: array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1])} + +fcluster_maxclust = { + 8.0: array([5, 5, 5, 5, 5, 5, 5, 6, 5, 5, 7, 7, 7, 7, 7, 8, 7, 7, 7, 7, 4, + 1, 1, 1, 3, 1, 1, 1, 1, 2]), + 4.0: array([3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, + 1, 1, 1, 1, 1, 1, 1, 1, 1]), + 1.0: array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1])} diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/tests/test_disjoint_set.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/tests/test_disjoint_set.py new file mode 100644 index 0000000000000000000000000000000000000000..a73512d35eef168f625a1942a87d248e73a71aa2 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/tests/test_disjoint_set.py @@ -0,0 +1,202 @@ +import pytest +from pytest import raises as assert_raises +import numpy as np +from scipy.cluster.hierarchy import DisjointSet +import string + + +def generate_random_token(): + k = len(string.ascii_letters) + tokens = list(np.arange(k, dtype=int)) + tokens += list(np.arange(k, dtype=float)) + tokens += list(string.ascii_letters) + tokens += [None for i in range(k)] + tokens = np.array(tokens, dtype=object) + rng = np.random.RandomState(seed=0) + + while 1: + size = rng.randint(1, 3) + element = rng.choice(tokens, size) + if size == 1: + yield element[0] + else: + yield tuple(element) + + +def get_elements(n): + # dict is deterministic without difficulty of comparing numpy ints + elements = {} + for element in generate_random_token(): + if element not in elements: + elements[element] = len(elements) + if len(elements) >= n: + break + return list(elements.keys()) + + +def test_init(): + n = 10 + elements = get_elements(n) + dis = DisjointSet(elements) + assert dis.n_subsets == n + assert list(dis) == elements + + +def test_len(): + n = 10 + elements = get_elements(n) + dis = DisjointSet(elements) + assert len(dis) == n + + dis.add("dummy") + assert len(dis) == n + 1 + + +@pytest.mark.parametrize("n", [10, 100]) +def test_contains(n): + elements = get_elements(n) + dis = DisjointSet(elements) + for x in elements: + assert x in dis + + assert "dummy" not in dis + + +@pytest.mark.parametrize("n", [10, 100]) +def test_add(n): + elements = get_elements(n) + dis1 = DisjointSet(elements) + + dis2 = DisjointSet() + for i, x in enumerate(elements): + dis2.add(x) + assert len(dis2) == i + 1 + + # test idempotency by adding element again + dis2.add(x) + assert len(dis2) == i + 1 + + assert list(dis1) == list(dis2) + + +def test_element_not_present(): + elements = get_elements(n=10) + dis = DisjointSet(elements) + + with assert_raises(KeyError): + dis["dummy"] + + with assert_raises(KeyError): + dis.merge(elements[0], "dummy") + + with assert_raises(KeyError): + dis.connected(elements[0], "dummy") + + +@pytest.mark.parametrize("direction", ["forwards", "backwards"]) +@pytest.mark.parametrize("n", [10, 100]) +def test_linear_union_sequence(n, direction): + elements = get_elements(n) + dis = DisjointSet(elements) + assert elements == list(dis) + + indices = list(range(n - 1)) + if direction == "backwards": + indices = indices[::-1] + + for it, i in enumerate(indices): + assert not dis.connected(elements[i], elements[i + 1]) + assert dis.merge(elements[i], elements[i + 1]) + assert dis.connected(elements[i], elements[i + 1]) + assert dis.n_subsets == n - 1 - it + + roots = [dis[i] for i in elements] + if direction == "forwards": + assert all(elements[0] == r for r in roots) + else: + assert all(elements[-2] == r for r in roots) + assert not dis.merge(elements[0], elements[-1]) + + +@pytest.mark.parametrize("n", [10, 100]) +def test_self_unions(n): + elements = get_elements(n) + dis = DisjointSet(elements) + + for x in elements: + assert dis.connected(x, x) + assert not dis.merge(x, x) + assert dis.connected(x, x) + assert dis.n_subsets == len(elements) + + assert elements == list(dis) + roots = [dis[x] for x in elements] + assert elements == roots + + +@pytest.mark.parametrize("order", ["ab", "ba"]) +@pytest.mark.parametrize("n", [10, 100]) +def test_equal_size_ordering(n, order): + elements = get_elements(n) + dis = DisjointSet(elements) + + rng = np.random.RandomState(seed=0) + indices = np.arange(n) + rng.shuffle(indices) + + for i in range(0, len(indices), 2): + a, b = elements[indices[i]], elements[indices[i + 1]] + if order == "ab": + assert dis.merge(a, b) + else: + assert dis.merge(b, a) + + expected = elements[min(indices[i], indices[i + 1])] + assert dis[a] == expected + assert dis[b] == expected + + +@pytest.mark.parametrize("kmax", [5, 10]) +def test_binary_tree(kmax): + n = 2**kmax + elements = get_elements(n) + dis = DisjointSet(elements) + rng = np.random.RandomState(seed=0) + + for k in 2**np.arange(kmax): + for i in range(0, n, 2 * k): + r1, r2 = rng.randint(0, k, size=2) + a, b = elements[i + r1], elements[i + k + r2] + assert not dis.connected(a, b) + assert dis.merge(a, b) + assert dis.connected(a, b) + + assert elements == list(dis) + roots = [dis[i] for i in elements] + expected_indices = np.arange(n) - np.arange(n) % (2 * k) + expected = [elements[i] for i in expected_indices] + assert roots == expected + + +@pytest.mark.parametrize("n", [10, 100]) +def test_subsets(n): + elements = get_elements(n) + dis = DisjointSet(elements) + + rng = np.random.RandomState(seed=0) + for i, j in rng.randint(0, n, (n, 2)): + x = elements[i] + y = elements[j] + + expected = {element for element in dis if {dis[element]} == {dis[x]}} + assert dis.subset_size(x) == len(dis.subset(x)) + assert expected == dis.subset(x) + + expected = {dis[element]: set() for element in dis} + for element in dis: + expected[dis[element]].add(element) + expected = list(expected.values()) + assert expected == dis.subsets() + + dis.merge(x, y) + assert dis.subset(x) == dis.subset(y) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/tests/test_hierarchy.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/tests/test_hierarchy.py new file mode 100644 index 0000000000000000000000000000000000000000..2dd0e37c59f3b004b09ce1662a98b53505398453 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/tests/test_hierarchy.py @@ -0,0 +1,1300 @@ +# +# Author: Damian Eads +# Date: April 17, 2008 +# +# Copyright (C) 2008 Damian Eads +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials provided +# with the distribution. +# +# 3. The name of the author may not be used to endorse or promote +# products derived from this software without specific prior +# written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS +# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE +# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +import numpy as np +from numpy.testing import (assert_allclose, assert_equal, assert_array_equal, assert_, + assert_warns) +import pytest +from pytest import raises as assert_raises + +import scipy.cluster.hierarchy +from scipy.cluster.hierarchy import ( + ClusterWarning, linkage, from_mlab_linkage, to_mlab_linkage, + num_obs_linkage, inconsistent, cophenet, fclusterdata, fcluster, + is_isomorphic, single, leaders, + correspond, is_monotonic, maxdists, maxinconsts, maxRstat, + is_valid_linkage, is_valid_im, to_tree, leaves_list, dendrogram, + set_link_color_palette, cut_tree, optimal_leaf_ordering, + _order_cluster_tree, _hierarchy, _LINKAGE_METHODS) +from scipy.spatial.distance import pdist +from scipy.cluster._hierarchy import Heap +from scipy.conftest import array_api_compatible +from scipy._lib._array_api import xp_assert_close, xp_assert_equal + +from threading import Lock + +from . import hierarchy_test_data + + +# Matplotlib is not a scipy dependency but is optionally used in dendrogram, so +# check if it's available +try: + import matplotlib + # and set the backend to be Agg (no gui) + matplotlib.use('Agg') + # before importing pyplot + import matplotlib.pyplot as plt + have_matplotlib = True +except Exception: + have_matplotlib = False + + +pytestmark = [array_api_compatible, pytest.mark.usefixtures("skip_xp_backends")] +skip_xp_backends = pytest.mark.skip_xp_backends + + +class TestLinkage: + + @skip_xp_backends(cpu_only=True) + def test_linkage_non_finite_elements_in_distance_matrix(self, xp): + # Tests linkage(Y) where Y contains a non-finite element (e.g. NaN or Inf). + # Exception expected. + y = xp.asarray([xp.nan] + [0.0]*5) + assert_raises(ValueError, linkage, y) + + @skip_xp_backends(cpu_only=True) + def test_linkage_empty_distance_matrix(self, xp): + # Tests linkage(Y) where Y is a 0x4 linkage matrix. Exception expected. + y = xp.zeros((0,)) + assert_raises(ValueError, linkage, y) + + @skip_xp_backends(cpu_only=True) + def test_linkage_tdist(self, xp): + for method in ['single', 'complete', 'average', 'weighted']: + self.check_linkage_tdist(method, xp) + + def check_linkage_tdist(self, method, xp): + # Tests linkage(Y, method) on the tdist data set. + Z = linkage(xp.asarray(hierarchy_test_data.ytdist), method) + expectedZ = getattr(hierarchy_test_data, 'linkage_ytdist_' + method) + xp_assert_close(Z, xp.asarray(expectedZ), atol=1e-10) + + @skip_xp_backends(cpu_only=True) + def test_linkage_X(self, xp): + for method in ['centroid', 'median', 'ward']: + self.check_linkage_q(method, xp) + + def check_linkage_q(self, method, xp): + # Tests linkage(Y, method) on the Q data set. + Z = linkage(xp.asarray(hierarchy_test_data.X), method) + expectedZ = getattr(hierarchy_test_data, 'linkage_X_' + method) + xp_assert_close(Z, xp.asarray(expectedZ), atol=1e-06) + + y = scipy.spatial.distance.pdist(hierarchy_test_data.X, + metric="euclidean") + Z = linkage(xp.asarray(y), method) + xp_assert_close(Z, xp.asarray(expectedZ), atol=1e-06) + + @skip_xp_backends(cpu_only=True) + def test_compare_with_trivial(self, xp): + rng = np.random.RandomState(0) + n = 20 + X = rng.rand(n, 2) + d = pdist(X) + + for method, code in _LINKAGE_METHODS.items(): + Z_trivial = _hierarchy.linkage(d, n, code) + Z = linkage(xp.asarray(d), method) + xp_assert_close(Z, xp.asarray(Z_trivial), rtol=1e-14, atol=1e-15) + + @skip_xp_backends(cpu_only=True) + def test_optimal_leaf_ordering(self, xp): + Z = linkage(xp.asarray(hierarchy_test_data.ytdist), optimal_ordering=True) + expectedZ = getattr(hierarchy_test_data, 'linkage_ytdist_single_olo') + xp_assert_close(Z, xp.asarray(expectedZ), atol=1e-10) + + +@skip_xp_backends(cpu_only=True) +class TestLinkageTies: + + _expectations = { + 'single': np.array([[0, 1, 1.41421356, 2], + [2, 3, 1.41421356, 3]]), + 'complete': np.array([[0, 1, 1.41421356, 2], + [2, 3, 2.82842712, 3]]), + 'average': np.array([[0, 1, 1.41421356, 2], + [2, 3, 2.12132034, 3]]), + 'weighted': np.array([[0, 1, 1.41421356, 2], + [2, 3, 2.12132034, 3]]), + 'centroid': np.array([[0, 1, 1.41421356, 2], + [2, 3, 2.12132034, 3]]), + 'median': np.array([[0, 1, 1.41421356, 2], + [2, 3, 2.12132034, 3]]), + 'ward': np.array([[0, 1, 1.41421356, 2], + [2, 3, 2.44948974, 3]]), + } + + def test_linkage_ties(self, xp): + for method in ['single', 'complete', 'average', 'weighted', + 'centroid', 'median', 'ward']: + self.check_linkage_ties(method, xp) + + def check_linkage_ties(self, method, xp): + X = xp.asarray([[-1, -1], [0, 0], [1, 1]]) + Z = linkage(X, method=method) + expectedZ = self._expectations[method] + xp_assert_close(Z, xp.asarray(expectedZ), atol=1e-06) + + +@skip_xp_backends(cpu_only=True) +class TestInconsistent: + + def test_inconsistent_tdist(self, xp): + for depth in hierarchy_test_data.inconsistent_ytdist: + self.check_inconsistent_tdist(depth, xp) + + def check_inconsistent_tdist(self, depth, xp): + Z = xp.asarray(hierarchy_test_data.linkage_ytdist_single) + xp_assert_close(inconsistent(Z, depth), + xp.asarray(hierarchy_test_data.inconsistent_ytdist[depth])) + + +@skip_xp_backends(cpu_only=True) +class TestCopheneticDistance: + + def test_linkage_cophenet_tdist_Z(self, xp): + # Tests cophenet(Z) on tdist data set. + expectedM = xp.asarray([268, 295, 255, 255, 295, 295, 268, 268, 295, 295, + 295, 138, 219, 295, 295]) + Z = xp.asarray(hierarchy_test_data.linkage_ytdist_single) + M = cophenet(Z) + xp_assert_close(M, xp.asarray(expectedM, dtype=xp.float64), atol=1e-10) + + def test_linkage_cophenet_tdist_Z_Y(self, xp): + # Tests cophenet(Z, Y) on tdist data set. + Z = xp.asarray(hierarchy_test_data.linkage_ytdist_single) + (c, M) = cophenet(Z, xp.asarray(hierarchy_test_data.ytdist)) + expectedM = xp.asarray([268, 295, 255, 255, 295, 295, 268, 268, 295, 295, + 295, 138, 219, 295, 295], dtype=xp.float64) + expectedc = xp.asarray(0.639931296433393415057366837573, dtype=xp.float64)[()] + xp_assert_close(c, expectedc, atol=1e-10) + xp_assert_close(M, expectedM, atol=1e-10) + + def test_gh_22183(self, xp): + # check for lack of segfault + # (out of bounds memory access) + # and correct interception of + # invalid linkage matrix + arr=[[0.0, 1.0, 1.0, 2.0], + [2.0, 12.0, 1.0, 3.0], + [3.0, 4.0, 1.0, 2.0], + [5.0, 14.0, 1.0, 3.0], + [6.0, 7.0, 1.0, 2.0], + [8.0, 16.0, 1.0, 3.0], + [9.0, 10.0, 1.0, 2.0], + [11.0, 18.0, 1.0, 3.0], + [13.0, 15.0, 2.0, 6.0], + [17.0, 20.0, 2.0, 32.0], + [19.0, 21.0, 2.0, 12.0]] + with pytest.raises(ValueError, match="excessive observations"): + cophenet(xp.asarray(arr)) + + +class TestMLabLinkageConversion: + + def test_mlab_linkage_conversion_empty(self, xp): + # Tests from/to_mlab_linkage on empty linkage array. + X = xp.asarray([], dtype=xp.float64) + xp_assert_equal(from_mlab_linkage(X), X) + xp_assert_equal(to_mlab_linkage(X), X) + + @skip_xp_backends(cpu_only=True) + def test_mlab_linkage_conversion_single_row(self, xp): + # Tests from/to_mlab_linkage on linkage array with single row. + Z = xp.asarray([[0., 1., 3., 2.]]) + Zm = xp.asarray([[1, 2, 3]]) + xp_assert_close(from_mlab_linkage(Zm), xp.asarray(Z, dtype=xp.float64), + rtol=1e-15) + xp_assert_close(to_mlab_linkage(Z), xp.asarray(Zm, dtype=xp.float64), + rtol=1e-15) + + @skip_xp_backends(cpu_only=True) + def test_mlab_linkage_conversion_multiple_rows(self, xp): + # Tests from/to_mlab_linkage on linkage array with multiple rows. + Zm = xp.asarray([[3, 6, 138], [4, 5, 219], + [1, 8, 255], [2, 9, 268], [7, 10, 295]]) + Z = xp.asarray([[2., 5., 138., 2.], + [3., 4., 219., 2.], + [0., 7., 255., 3.], + [1., 8., 268., 4.], + [6., 9., 295., 6.]], + dtype=xp.float64) + xp_assert_close(from_mlab_linkage(Zm), Z, rtol=1e-15) + xp_assert_close(to_mlab_linkage(Z), xp.asarray(Zm, dtype=xp.float64), + rtol=1e-15) + + +@skip_xp_backends(cpu_only=True) +class TestFcluster: + + def test_fclusterdata(self, xp): + for t in hierarchy_test_data.fcluster_inconsistent: + self.check_fclusterdata(t, 'inconsistent', xp) + for t in hierarchy_test_data.fcluster_distance: + self.check_fclusterdata(t, 'distance', xp) + for t in hierarchy_test_data.fcluster_maxclust: + self.check_fclusterdata(t, 'maxclust', xp) + + def check_fclusterdata(self, t, criterion, xp): + # Tests fclusterdata(X, criterion=criterion, t=t) on a random 3-cluster data set + expectedT = xp.asarray(getattr(hierarchy_test_data, 'fcluster_' + criterion)[t]) + X = xp.asarray(hierarchy_test_data.Q_X) + T = fclusterdata(X, criterion=criterion, t=t) + assert_(is_isomorphic(T, expectedT)) + + def test_fcluster(self, xp): + for t in hierarchy_test_data.fcluster_inconsistent: + self.check_fcluster(t, 'inconsistent', xp) + for t in hierarchy_test_data.fcluster_distance: + self.check_fcluster(t, 'distance', xp) + for t in hierarchy_test_data.fcluster_maxclust: + self.check_fcluster(t, 'maxclust', xp) + + def check_fcluster(self, t, criterion, xp): + # Tests fcluster(Z, criterion=criterion, t=t) on a random 3-cluster data set. + expectedT = xp.asarray(getattr(hierarchy_test_data, 'fcluster_' + criterion)[t]) + Z = single(xp.asarray(hierarchy_test_data.Q_X)) + T = fcluster(Z, criterion=criterion, t=t) + assert_(is_isomorphic(T, expectedT)) + + def test_fcluster_monocrit(self, xp): + for t in hierarchy_test_data.fcluster_distance: + self.check_fcluster_monocrit(t, xp) + for t in hierarchy_test_data.fcluster_maxclust: + self.check_fcluster_maxclust_monocrit(t, xp) + + def check_fcluster_monocrit(self, t, xp): + expectedT = xp.asarray(hierarchy_test_data.fcluster_distance[t]) + Z = single(xp.asarray(hierarchy_test_data.Q_X)) + T = fcluster(Z, t, criterion='monocrit', monocrit=maxdists(Z)) + assert_(is_isomorphic(T, expectedT)) + + def check_fcluster_maxclust_monocrit(self, t, xp): + expectedT = xp.asarray(hierarchy_test_data.fcluster_maxclust[t]) + Z = single(xp.asarray(hierarchy_test_data.Q_X)) + T = fcluster(Z, t, criterion='maxclust_monocrit', monocrit=maxdists(Z)) + assert_(is_isomorphic(T, expectedT)) + + def test_fcluster_maxclust_gh_12651(self, xp): + y = xp.asarray([[1], [4], [5]]) + Z = single(y) + assert_array_equal(fcluster(Z, t=1, criterion="maxclust"), + xp.asarray([1, 1, 1])) + assert_array_equal(fcluster(Z, t=2, criterion="maxclust"), + xp.asarray([2, 1, 1])) + assert_array_equal(fcluster(Z, t=3, criterion="maxclust"), + xp.asarray([1, 2, 3])) + assert_array_equal(fcluster(Z, t=5, criterion="maxclust"), + xp.asarray([1, 2, 3])) + + +@skip_xp_backends(cpu_only=True) +class TestLeaders: + + def test_leaders_single(self, xp): + # Tests leaders using a flat clustering generated by single linkage. + X = hierarchy_test_data.Q_X + Y = pdist(X) + Y = xp.asarray(Y) + Z = linkage(Y) + T = fcluster(Z, criterion='maxclust', t=3) + Lright = (xp.asarray([53, 55, 56]), xp.asarray([2, 3, 1])) + T = xp.asarray(T, dtype=xp.int32) + L = leaders(Z, T) + assert_allclose(np.concatenate(L), np.concatenate(Lright), rtol=1e-15) + + +@skip_xp_backends(np_only=True, + reason='`is_isomorphic` only supports NumPy backend') +class TestIsIsomorphic: + + @skip_xp_backends(np_only=True, + reason='array-likes only supported for NumPy backend') + def test_array_like(self, xp): + assert is_isomorphic([1, 1, 1], [2, 2, 2]) + assert is_isomorphic([], []) + + def test_is_isomorphic_1(self, xp): + # Tests is_isomorphic on test case #1 (one flat cluster, different labellings) + a = xp.asarray([1, 1, 1]) + b = xp.asarray([2, 2, 2]) + assert is_isomorphic(a, b) + assert is_isomorphic(b, a) + + def test_is_isomorphic_2(self, xp): + # Tests is_isomorphic on test case #2 (two flat clusters, different labelings) + a = xp.asarray([1, 7, 1]) + b = xp.asarray([2, 3, 2]) + assert is_isomorphic(a, b) + assert is_isomorphic(b, a) + + def test_is_isomorphic_3(self, xp): + # Tests is_isomorphic on test case #3 (no flat clusters) + a = xp.asarray([]) + b = xp.asarray([]) + assert is_isomorphic(a, b) + + def test_is_isomorphic_4A(self, xp): + # Tests is_isomorphic on test case #4A + # (3 flat clusters, different labelings, isomorphic) + a = xp.asarray([1, 2, 3]) + b = xp.asarray([1, 3, 2]) + assert is_isomorphic(a, b) + assert is_isomorphic(b, a) + + def test_is_isomorphic_4B(self, xp): + # Tests is_isomorphic on test case #4B + # (3 flat clusters, different labelings, nonisomorphic) + a = xp.asarray([1, 2, 3, 3]) + b = xp.asarray([1, 3, 2, 3]) + assert is_isomorphic(a, b) is False + assert is_isomorphic(b, a) is False + + def test_is_isomorphic_4C(self, xp): + # Tests is_isomorphic on test case #4C + # (3 flat clusters, different labelings, isomorphic) + a = xp.asarray([7, 2, 3]) + b = xp.asarray([6, 3, 2]) + assert is_isomorphic(a, b) + assert is_isomorphic(b, a) + + def test_is_isomorphic_5(self, xp): + # Tests is_isomorphic on test case #5 (1000 observations, 2/3/5 random + # clusters, random permutation of the labeling). + for nc in [2, 3, 5]: + self.help_is_isomorphic_randperm(1000, nc, xp=xp) + + def test_is_isomorphic_6(self, xp): + # Tests is_isomorphic on test case #5A (1000 observations, 2/3/5 random + # clusters, random permutation of the labeling, slightly + # nonisomorphic.) + for nc in [2, 3, 5]: + self.help_is_isomorphic_randperm(1000, nc, True, 5, xp=xp) + + def test_is_isomorphic_7(self, xp): + # Regression test for gh-6271 + a = xp.asarray([1, 2, 3]) + b = xp.asarray([1, 1, 1]) + assert not is_isomorphic(a, b) + + def help_is_isomorphic_randperm(self, nobs, nclusters, noniso=False, nerrors=0, + *, xp): + for k in range(3): + a = (np.random.rand(nobs) * nclusters).astype(int) + b = np.zeros(a.size, dtype=int) + P = np.random.permutation(nclusters) + for i in range(0, a.shape[0]): + b[i] = P[a[i]] + if noniso: + Q = np.random.permutation(nobs) + b[Q[0:nerrors]] += 1 + b[Q[0:nerrors]] %= nclusters + a = xp.asarray(a) + b = xp.asarray(b) + assert is_isomorphic(a, b) == (not noniso) + assert is_isomorphic(b, a) == (not noniso) + + +@skip_xp_backends(cpu_only=True) +class TestIsValidLinkage: + + def test_is_valid_linkage_various_size(self, xp): + for nrow, ncol, valid in [(2, 5, False), (2, 3, False), + (1, 4, True), (2, 4, True)]: + self.check_is_valid_linkage_various_size(nrow, ncol, valid, xp) + + def check_is_valid_linkage_various_size(self, nrow, ncol, valid, xp): + # Tests is_valid_linkage(Z) with linkage matrices of various sizes + Z = xp.asarray([[0, 1, 3.0, 2, 5], + [3, 2, 4.0, 3, 3]], dtype=xp.float64) + Z = Z[:nrow, :ncol] + assert_(is_valid_linkage(Z) == valid) + if not valid: + assert_raises(ValueError, is_valid_linkage, Z, throw=True) + + def test_is_valid_linkage_int_type(self, xp): + # Tests is_valid_linkage(Z) with integer type. + Z = xp.asarray([[0, 1, 3.0, 2], + [3, 2, 4.0, 3]], dtype=xp.int64) + assert_(is_valid_linkage(Z) is False) + assert_raises(TypeError, is_valid_linkage, Z, throw=True) + + def test_is_valid_linkage_empty(self, xp): + # Tests is_valid_linkage(Z) with empty linkage. + Z = xp.zeros((0, 4), dtype=xp.float64) + assert_(is_valid_linkage(Z) is False) + assert_raises(ValueError, is_valid_linkage, Z, throw=True) + + def test_is_valid_linkage_4_and_up(self, xp): + # Tests is_valid_linkage(Z) on linkage on observation sets between + # sizes 4 and 15 (step size 3). + for i in range(4, 15, 3): + y = np.random.rand(i*(i-1)//2) + y = xp.asarray(y) + Z = linkage(y) + assert_(is_valid_linkage(Z) is True) + + @skip_xp_backends('jax.numpy', + reason='jax arrays do not support item assignment') + def test_is_valid_linkage_4_and_up_neg_index_left(self, xp): + # Tests is_valid_linkage(Z) on linkage on observation sets between + # sizes 4 and 15 (step size 3) with negative indices (left). + for i in range(4, 15, 3): + y = np.random.rand(i*(i-1)//2) + y = xp.asarray(y) + Z = linkage(y) + Z[i//2,0] = -2 + assert_(is_valid_linkage(Z) is False) + assert_raises(ValueError, is_valid_linkage, Z, throw=True) + + @skip_xp_backends('jax.numpy', + reason='jax arrays do not support item assignment') + def test_is_valid_linkage_4_and_up_neg_index_right(self, xp): + # Tests is_valid_linkage(Z) on linkage on observation sets between + # sizes 4 and 15 (step size 3) with negative indices (right). + for i in range(4, 15, 3): + y = np.random.rand(i*(i-1)//2) + y = xp.asarray(y) + Z = linkage(y) + Z[i//2,1] = -2 + assert_(is_valid_linkage(Z) is False) + assert_raises(ValueError, is_valid_linkage, Z, throw=True) + + @skip_xp_backends('jax.numpy', + reason='jax arrays do not support item assignment') + def test_is_valid_linkage_4_and_up_neg_dist(self, xp): + # Tests is_valid_linkage(Z) on linkage on observation sets between + # sizes 4 and 15 (step size 3) with negative distances. + for i in range(4, 15, 3): + y = np.random.rand(i*(i-1)//2) + y = xp.asarray(y) + Z = linkage(y) + Z[i//2,2] = -0.5 + assert_(is_valid_linkage(Z) is False) + assert_raises(ValueError, is_valid_linkage, Z, throw=True) + + @skip_xp_backends('jax.numpy', + reason='jax arrays do not support item assignment') + def test_is_valid_linkage_4_and_up_neg_counts(self, xp): + # Tests is_valid_linkage(Z) on linkage on observation sets between + # sizes 4 and 15 (step size 3) with negative counts. + for i in range(4, 15, 3): + y = np.random.rand(i*(i-1)//2) + y = xp.asarray(y) + Z = linkage(y) + Z[i//2,3] = -2 + assert_(is_valid_linkage(Z) is False) + assert_raises(ValueError, is_valid_linkage, Z, throw=True) + + +@skip_xp_backends(cpu_only=True) +class TestIsValidInconsistent: + + def test_is_valid_im_int_type(self, xp): + # Tests is_valid_im(R) with integer type. + R = xp.asarray([[0, 1, 3.0, 2], + [3, 2, 4.0, 3]], dtype=xp.int64) + assert_(is_valid_im(R) is False) + assert_raises(TypeError, is_valid_im, R, throw=True) + + def test_is_valid_im_various_size(self, xp): + for nrow, ncol, valid in [(2, 5, False), (2, 3, False), + (1, 4, True), (2, 4, True)]: + self.check_is_valid_im_various_size(nrow, ncol, valid, xp) + + def check_is_valid_im_various_size(self, nrow, ncol, valid, xp): + # Tests is_valid_im(R) with linkage matrices of various sizes + R = xp.asarray([[0, 1, 3.0, 2, 5], + [3, 2, 4.0, 3, 3]], dtype=xp.float64) + R = R[:nrow, :ncol] + assert_(is_valid_im(R) == valid) + if not valid: + assert_raises(ValueError, is_valid_im, R, throw=True) + + def test_is_valid_im_empty(self, xp): + # Tests is_valid_im(R) with empty inconsistency matrix. + R = xp.zeros((0, 4), dtype=xp.float64) + assert_(is_valid_im(R) is False) + assert_raises(ValueError, is_valid_im, R, throw=True) + + def test_is_valid_im_4_and_up(self, xp): + # Tests is_valid_im(R) on im on observation sets between sizes 4 and 15 + # (step size 3). + for i in range(4, 15, 3): + y = np.random.rand(i*(i-1)//2) + y = xp.asarray(y) + Z = linkage(y) + R = inconsistent(Z) + assert_(is_valid_im(R) is True) + + @skip_xp_backends('jax.numpy', reason='jax arrays do not support item assignment') + def test_is_valid_im_4_and_up_neg_index_left(self, xp): + # Tests is_valid_im(R) on im on observation sets between sizes 4 and 15 + # (step size 3) with negative link height means. + for i in range(4, 15, 3): + y = np.random.rand(i*(i-1)//2) + y = xp.asarray(y) + Z = linkage(y) + R = inconsistent(Z) + R[i//2,0] = -2.0 + assert_(is_valid_im(R) is False) + assert_raises(ValueError, is_valid_im, R, throw=True) + + @skip_xp_backends('jax.numpy', reason='jax arrays do not support item assignment') + def test_is_valid_im_4_and_up_neg_index_right(self, xp): + # Tests is_valid_im(R) on im on observation sets between sizes 4 and 15 + # (step size 3) with negative link height standard deviations. + for i in range(4, 15, 3): + y = np.random.rand(i*(i-1)//2) + y = xp.asarray(y) + Z = linkage(y) + R = inconsistent(Z) + R[i//2,1] = -2.0 + assert_(is_valid_im(R) is False) + assert_raises(ValueError, is_valid_im, R, throw=True) + + @skip_xp_backends('jax.numpy', reason='jax arrays do not support item assignment') + def test_is_valid_im_4_and_up_neg_dist(self, xp): + # Tests is_valid_im(R) on im on observation sets between sizes 4 and 15 + # (step size 3) with negative link counts. + for i in range(4, 15, 3): + y = np.random.rand(i*(i-1)//2) + y = xp.asarray(y) + Z = linkage(y) + R = inconsistent(Z) + R[i//2,2] = -0.5 + assert_(is_valid_im(R) is False) + assert_raises(ValueError, is_valid_im, R, throw=True) + + +class TestNumObsLinkage: + + @skip_xp_backends(cpu_only=True) + def test_num_obs_linkage_empty(self, xp): + # Tests num_obs_linkage(Z) with empty linkage. + Z = xp.zeros((0, 4), dtype=xp.float64) + assert_raises(ValueError, num_obs_linkage, Z) + + def test_num_obs_linkage_1x4(self, xp): + # Tests num_obs_linkage(Z) on linkage over 2 observations. + Z = xp.asarray([[0, 1, 3.0, 2]], dtype=xp.float64) + assert_equal(num_obs_linkage(Z), 2) + + def test_num_obs_linkage_2x4(self, xp): + # Tests num_obs_linkage(Z) on linkage over 3 observations. + Z = xp.asarray([[0, 1, 3.0, 2], + [3, 2, 4.0, 3]], dtype=xp.float64) + assert_equal(num_obs_linkage(Z), 3) + + @skip_xp_backends(cpu_only=True) + def test_num_obs_linkage_4_and_up(self, xp): + # Tests num_obs_linkage(Z) on linkage on observation sets between sizes + # 4 and 15 (step size 3). + for i in range(4, 15, 3): + y = np.random.rand(i*(i-1)//2) + y = xp.asarray(y) + Z = linkage(y) + assert_equal(num_obs_linkage(Z), i) + + +@skip_xp_backends(cpu_only=True) +class TestLeavesList: + + def test_leaves_list_1x4(self, xp): + # Tests leaves_list(Z) on a 1x4 linkage. + Z = xp.asarray([[0, 1, 3.0, 2]], dtype=xp.float64) + to_tree(Z) + assert_allclose(leaves_list(Z), [0, 1], rtol=1e-15) + + def test_leaves_list_2x4(self, xp): + # Tests leaves_list(Z) on a 2x4 linkage. + Z = xp.asarray([[0, 1, 3.0, 2], + [3, 2, 4.0, 3]], dtype=xp.float64) + to_tree(Z) + assert_allclose(leaves_list(Z), [0, 1, 2], rtol=1e-15) + + def test_leaves_list_Q(self, xp): + for method in ['single', 'complete', 'average', 'weighted', 'centroid', + 'median', 'ward']: + self.check_leaves_list_Q(method, xp) + + def check_leaves_list_Q(self, method, xp): + # Tests leaves_list(Z) on the Q data set + X = xp.asarray(hierarchy_test_data.Q_X) + Z = linkage(X, method) + node = to_tree(Z) + assert_allclose(node.pre_order(), leaves_list(Z), rtol=1e-15) + + def test_Q_subtree_pre_order(self, xp): + # Tests that pre_order() works when called on sub-trees. + X = xp.asarray(hierarchy_test_data.Q_X) + Z = linkage(X, 'single') + node = to_tree(Z) + assert_allclose(node.pre_order(), (node.get_left().pre_order() + + node.get_right().pre_order()), + rtol=1e-15) + + +@skip_xp_backends(cpu_only=True) +class TestCorrespond: + + def test_correspond_empty(self, xp): + # Tests correspond(Z, y) with empty linkage and condensed distance matrix. + y = xp.zeros((0,), dtype=xp.float64) + Z = xp.zeros((0,4), dtype=xp.float64) + assert_raises(ValueError, correspond, Z, y) + + def test_correspond_2_and_up(self, xp): + # Tests correspond(Z, y) on linkage and CDMs over observation sets of + # different sizes. + for i in range(2, 4): + y = np.random.rand(i*(i-1)//2) + y = xp.asarray(y) + Z = linkage(y) + assert_(correspond(Z, y)) + for i in range(4, 15, 3): + y = np.random.rand(i*(i-1)//2) + y = xp.asarray(y) + Z = linkage(y) + assert_(correspond(Z, y)) + + def test_correspond_4_and_up(self, xp): + # Tests correspond(Z, y) on linkage and CDMs over observation sets of + # different sizes. Correspondence should be false. + for (i, j) in (list(zip(list(range(2, 4)), list(range(3, 5)))) + + list(zip(list(range(3, 5)), list(range(2, 4))))): + y = np.random.rand(i*(i-1)//2) + y2 = np.random.rand(j*(j-1)//2) + y = xp.asarray(y) + y2 = xp.asarray(y2) + Z = linkage(y) + Z2 = linkage(y2) + assert not correspond(Z, y2) + assert not correspond(Z2, y) + + def test_correspond_4_and_up_2(self, xp): + # Tests correspond(Z, y) on linkage and CDMs over observation sets of + # different sizes. Correspondence should be false. + for (i, j) in (list(zip(list(range(2, 7)), list(range(16, 21)))) + + list(zip(list(range(2, 7)), list(range(16, 21))))): + y = np.random.rand(i*(i-1)//2) + y2 = np.random.rand(j*(j-1)//2) + y = xp.asarray(y) + y2 = xp.asarray(y2) + Z = linkage(y) + Z2 = linkage(y2) + assert not correspond(Z, y2) + assert not correspond(Z2, y) + + def test_num_obs_linkage_multi_matrix(self, xp): + # Tests num_obs_linkage with observation matrices of multiple sizes. + for n in range(2, 10): + X = np.random.rand(n, 4) + Y = pdist(X) + Y = xp.asarray(Y) + Z = linkage(Y) + assert_equal(num_obs_linkage(Z), n) + + +@skip_xp_backends(cpu_only=True) +class TestIsMonotonic: + + def test_is_monotonic_empty(self, xp): + # Tests is_monotonic(Z) on an empty linkage. + Z = xp.zeros((0, 4), dtype=xp.float64) + assert_raises(ValueError, is_monotonic, Z) + + def test_is_monotonic_1x4(self, xp): + # Tests is_monotonic(Z) on 1x4 linkage. Expecting True. + Z = xp.asarray([[0, 1, 0.3, 2]], dtype=xp.float64) + assert is_monotonic(Z) + + def test_is_monotonic_2x4_T(self, xp): + # Tests is_monotonic(Z) on 2x4 linkage. Expecting True. + Z = xp.asarray([[0, 1, 0.3, 2], + [2, 3, 0.4, 3]], dtype=xp.float64) + assert is_monotonic(Z) + + def test_is_monotonic_2x4_F(self, xp): + # Tests is_monotonic(Z) on 2x4 linkage. Expecting False. + Z = xp.asarray([[0, 1, 0.4, 2], + [2, 3, 0.3, 3]], dtype=xp.float64) + assert not is_monotonic(Z) + + def test_is_monotonic_3x4_T(self, xp): + # Tests is_monotonic(Z) on 3x4 linkage. Expecting True. + Z = xp.asarray([[0, 1, 0.3, 2], + [2, 3, 0.4, 2], + [4, 5, 0.6, 4]], dtype=xp.float64) + assert is_monotonic(Z) + + def test_is_monotonic_3x4_F1(self, xp): + # Tests is_monotonic(Z) on 3x4 linkage (case 1). Expecting False. + Z = xp.asarray([[0, 1, 0.3, 2], + [2, 3, 0.2, 2], + [4, 5, 0.6, 4]], dtype=xp.float64) + assert not is_monotonic(Z) + + def test_is_monotonic_3x4_F2(self, xp): + # Tests is_monotonic(Z) on 3x4 linkage (case 2). Expecting False. + Z = xp.asarray([[0, 1, 0.8, 2], + [2, 3, 0.4, 2], + [4, 5, 0.6, 4]], dtype=xp.float64) + assert not is_monotonic(Z) + + def test_is_monotonic_3x4_F3(self, xp): + # Tests is_monotonic(Z) on 3x4 linkage (case 3). Expecting False + Z = xp.asarray([[0, 1, 0.3, 2], + [2, 3, 0.4, 2], + [4, 5, 0.2, 4]], dtype=xp.float64) + assert not is_monotonic(Z) + + def test_is_monotonic_tdist_linkage1(self, xp): + # Tests is_monotonic(Z) on clustering generated by single linkage on + # tdist data set. Expecting True. + Z = linkage(xp.asarray(hierarchy_test_data.ytdist), 'single') + assert is_monotonic(Z) + + @skip_xp_backends('jax.numpy', reason='jax arrays do not support item assignment') + def test_is_monotonic_tdist_linkage2(self, xp): + # Tests is_monotonic(Z) on clustering generated by single linkage on + # tdist data set. Perturbing. Expecting False. + Z = linkage(xp.asarray(hierarchy_test_data.ytdist), 'single') + Z[2,2] = 0.0 + assert not is_monotonic(Z) + + def test_is_monotonic_Q_linkage(self, xp): + # Tests is_monotonic(Z) on clustering generated by single linkage on + # Q data set. Expecting True. + X = xp.asarray(hierarchy_test_data.Q_X) + Z = linkage(X, 'single') + assert is_monotonic(Z) + + +@skip_xp_backends(cpu_only=True) +class TestMaxDists: + + def test_maxdists_empty_linkage(self, xp): + # Tests maxdists(Z) on empty linkage. Expecting exception. + Z = xp.zeros((0, 4), dtype=xp.float64) + assert_raises(ValueError, maxdists, Z) + + @skip_xp_backends('jax.numpy', reason='jax arrays do not support item assignment') + def test_maxdists_one_cluster_linkage(self, xp): + # Tests maxdists(Z) on linkage with one cluster. + Z = xp.asarray([[0, 1, 0.3, 4]], dtype=xp.float64) + MD = maxdists(Z) + expectedMD = calculate_maximum_distances(Z, xp) + xp_assert_close(MD, expectedMD, atol=1e-15) + + @skip_xp_backends('jax.numpy', reason='jax arrays do not support item assignment') + def test_maxdists_Q_linkage(self, xp): + for method in ['single', 'complete', 'ward', 'centroid', 'median']: + self.check_maxdists_Q_linkage(method, xp) + + def check_maxdists_Q_linkage(self, method, xp): + # Tests maxdists(Z) on the Q data set + X = xp.asarray(hierarchy_test_data.Q_X) + Z = linkage(X, method) + MD = maxdists(Z) + expectedMD = calculate_maximum_distances(Z, xp) + xp_assert_close(MD, expectedMD, atol=1e-15) + + +class TestMaxInconsts: + + @skip_xp_backends(cpu_only=True) + def test_maxinconsts_empty_linkage(self, xp): + # Tests maxinconsts(Z, R) on empty linkage. Expecting exception. + Z = xp.zeros((0, 4), dtype=xp.float64) + R = xp.zeros((0, 4), dtype=xp.float64) + assert_raises(ValueError, maxinconsts, Z, R) + + def test_maxinconsts_difrow_linkage(self, xp): + # Tests maxinconsts(Z, R) on linkage and inconsistency matrices with + # different numbers of clusters. Expecting exception. + Z = xp.asarray([[0, 1, 0.3, 4]], dtype=xp.float64) + R = np.random.rand(2, 4) + R = xp.asarray(R) + assert_raises(ValueError, maxinconsts, Z, R) + + @skip_xp_backends('jax.numpy', reason='jax arrays do not support item assignment', + cpu_only=True) + def test_maxinconsts_one_cluster_linkage(self, xp): + # Tests maxinconsts(Z, R) on linkage with one cluster. + Z = xp.asarray([[0, 1, 0.3, 4]], dtype=xp.float64) + R = xp.asarray([[0, 0, 0, 0.3]], dtype=xp.float64) + MD = maxinconsts(Z, R) + expectedMD = calculate_maximum_inconsistencies(Z, R, xp=xp) + xp_assert_close(MD, expectedMD, atol=1e-15) + + @skip_xp_backends('jax.numpy', reason='jax arrays do not support item assignment', + cpu_only=True) + def test_maxinconsts_Q_linkage(self, xp): + for method in ['single', 'complete', 'ward', 'centroid', 'median']: + self.check_maxinconsts_Q_linkage(method, xp) + + def check_maxinconsts_Q_linkage(self, method, xp): + # Tests maxinconsts(Z, R) on the Q data set + X = xp.asarray(hierarchy_test_data.Q_X) + Z = linkage(X, method) + R = inconsistent(Z) + MD = maxinconsts(Z, R) + expectedMD = calculate_maximum_inconsistencies(Z, R, xp=xp) + xp_assert_close(MD, expectedMD, atol=1e-15) + + +class TestMaxRStat: + + def test_maxRstat_invalid_index(self, xp): + for i in [3.3, -1, 4]: + self.check_maxRstat_invalid_index(i, xp) + + def check_maxRstat_invalid_index(self, i, xp): + # Tests maxRstat(Z, R, i). Expecting exception. + Z = xp.asarray([[0, 1, 0.3, 4]], dtype=xp.float64) + R = xp.asarray([[0, 0, 0, 0.3]], dtype=xp.float64) + if isinstance(i, int): + assert_raises(ValueError, maxRstat, Z, R, i) + else: + assert_raises(TypeError, maxRstat, Z, R, i) + + @skip_xp_backends(cpu_only=True) + def test_maxRstat_empty_linkage(self, xp): + for i in range(4): + self.check_maxRstat_empty_linkage(i, xp) + + def check_maxRstat_empty_linkage(self, i, xp): + # Tests maxRstat(Z, R, i) on empty linkage. Expecting exception. + Z = xp.zeros((0, 4), dtype=xp.float64) + R = xp.zeros((0, 4), dtype=xp.float64) + assert_raises(ValueError, maxRstat, Z, R, i) + + def test_maxRstat_difrow_linkage(self, xp): + for i in range(4): + self.check_maxRstat_difrow_linkage(i, xp) + + def check_maxRstat_difrow_linkage(self, i, xp): + # Tests maxRstat(Z, R, i) on linkage and inconsistency matrices with + # different numbers of clusters. Expecting exception. + Z = xp.asarray([[0, 1, 0.3, 4]], dtype=xp.float64) + R = np.random.rand(2, 4) + R = xp.asarray(R) + assert_raises(ValueError, maxRstat, Z, R, i) + + @skip_xp_backends('jax.numpy', reason='jax arrays do not support item assignment', + cpu_only=True) + def test_maxRstat_one_cluster_linkage(self, xp): + for i in range(4): + self.check_maxRstat_one_cluster_linkage(i, xp) + + def check_maxRstat_one_cluster_linkage(self, i, xp): + # Tests maxRstat(Z, R, i) on linkage with one cluster. + Z = xp.asarray([[0, 1, 0.3, 4]], dtype=xp.float64) + R = xp.asarray([[0, 0, 0, 0.3]], dtype=xp.float64) + MD = maxRstat(Z, R, 1) + expectedMD = calculate_maximum_inconsistencies(Z, R, 1, xp) + xp_assert_close(MD, expectedMD, atol=1e-15) + + @skip_xp_backends('jax.numpy', reason='jax arrays do not support item assignment', + cpu_only=True) + def test_maxRstat_Q_linkage(self, xp): + for method in ['single', 'complete', 'ward', 'centroid', 'median']: + for i in range(4): + self.check_maxRstat_Q_linkage(method, i, xp) + + def check_maxRstat_Q_linkage(self, method, i, xp): + # Tests maxRstat(Z, R, i) on the Q data set + X = xp.asarray(hierarchy_test_data.Q_X) + Z = linkage(X, method) + R = inconsistent(Z) + MD = maxRstat(Z, R, 1) + expectedMD = calculate_maximum_inconsistencies(Z, R, 1, xp) + xp_assert_close(MD, expectedMD, atol=1e-15) + + +@skip_xp_backends(cpu_only=True) +class TestDendrogram: + + def test_dendrogram_single_linkage_tdist(self, xp): + # Tests dendrogram calculation on single linkage of the tdist data set. + Z = linkage(xp.asarray(hierarchy_test_data.ytdist), 'single') + R = dendrogram(Z, no_plot=True) + leaves = R["leaves"] + assert_equal(leaves, [2, 5, 1, 0, 3, 4]) + + def test_valid_orientation(self, xp): + Z = linkage(xp.asarray(hierarchy_test_data.ytdist), 'single') + assert_raises(ValueError, dendrogram, Z, orientation="foo") + + def test_labels_as_array_or_list(self, xp): + # test for gh-12418 + Z = linkage(xp.asarray(hierarchy_test_data.ytdist), 'single') + labels = [1, 3, 2, 6, 4, 5] + result1 = dendrogram(Z, labels=xp.asarray(labels), no_plot=True) + result2 = dendrogram(Z, labels=labels, no_plot=True) + assert result1 == result2 + + @pytest.mark.skipif(not have_matplotlib, reason="no matplotlib") + def test_valid_label_size(self, xp): + link = xp.asarray([ + [0, 1, 1.0, 4], + [2, 3, 1.0, 5], + [4, 5, 2.0, 6], + ]) + plt.figure() + with pytest.raises(ValueError) as exc_info: + dendrogram(link, labels=list(range(100))) + assert "Dimensions of Z and labels must be consistent."\ + in str(exc_info.value) + + with pytest.raises( + ValueError, + match="Dimensions of Z and labels must be consistent."): + dendrogram(link, labels=[]) + + plt.close() + + @skip_xp_backends('torch', + reason='MPL 3.9.2 & torch DeprecationWarning from __array_wrap__' + ' and NumPy 2.0' + ) + @pytest.mark.skipif(not have_matplotlib, reason="no matplotlib") + def test_dendrogram_plot(self, xp): + for orientation in ['top', 'bottom', 'left', 'right']: + self.check_dendrogram_plot(orientation, xp) + + def check_dendrogram_plot(self, orientation, xp): + # Tests dendrogram plotting. + Z = linkage(xp.asarray(hierarchy_test_data.ytdist), 'single') + expected = {'color_list': ['C1', 'C0', 'C0', 'C0', 'C0'], + 'dcoord': [[0.0, 138.0, 138.0, 0.0], + [0.0, 219.0, 219.0, 0.0], + [0.0, 255.0, 255.0, 219.0], + [0.0, 268.0, 268.0, 255.0], + [138.0, 295.0, 295.0, 268.0]], + 'icoord': [[5.0, 5.0, 15.0, 15.0], + [45.0, 45.0, 55.0, 55.0], + [35.0, 35.0, 50.0, 50.0], + [25.0, 25.0, 42.5, 42.5], + [10.0, 10.0, 33.75, 33.75]], + 'ivl': ['2', '5', '1', '0', '3', '4'], + 'leaves': [2, 5, 1, 0, 3, 4], + 'leaves_color_list': ['C1', 'C1', 'C0', 'C0', 'C0', 'C0'], + } + + fig = plt.figure() + ax = fig.add_subplot(221) + + # test that dendrogram accepts ax keyword + R1 = dendrogram(Z, ax=ax, orientation=orientation) + R1['dcoord'] = np.asarray(R1['dcoord']) + assert_equal(R1, expected) + + # test that dendrogram accepts and handle the leaf_font_size and + # leaf_rotation keywords + dendrogram(Z, ax=ax, orientation=orientation, + leaf_font_size=20, leaf_rotation=90) + testlabel = ( + ax.get_xticklabels()[0] + if orientation in ['top', 'bottom'] + else ax.get_yticklabels()[0] + ) + assert_equal(testlabel.get_rotation(), 90) + assert_equal(testlabel.get_size(), 20) + dendrogram(Z, ax=ax, orientation=orientation, + leaf_rotation=90) + testlabel = ( + ax.get_xticklabels()[0] + if orientation in ['top', 'bottom'] + else ax.get_yticklabels()[0] + ) + assert_equal(testlabel.get_rotation(), 90) + dendrogram(Z, ax=ax, orientation=orientation, + leaf_font_size=20) + testlabel = ( + ax.get_xticklabels()[0] + if orientation in ['top', 'bottom'] + else ax.get_yticklabels()[0] + ) + assert_equal(testlabel.get_size(), 20) + plt.close() + + # test plotting to gca (will import pylab) + R2 = dendrogram(Z, orientation=orientation) + plt.close() + R2['dcoord'] = np.asarray(R2['dcoord']) + assert_equal(R2, expected) + + @skip_xp_backends('torch', + reason='MPL 3.9.2 & torch DeprecationWarning from __array_wrap__' + ' and NumPy 2.0' + ) + @pytest.mark.skipif(not have_matplotlib, reason="no matplotlib") + def test_dendrogram_truncate_mode(self, xp): + Z = linkage(xp.asarray(hierarchy_test_data.ytdist), 'single') + + R = dendrogram(Z, 2, 'lastp', show_contracted=True) + plt.close() + R['dcoord'] = np.asarray(R['dcoord']) + assert_equal(R, {'color_list': ['C0'], + 'dcoord': [[0.0, 295.0, 295.0, 0.0]], + 'icoord': [[5.0, 5.0, 15.0, 15.0]], + 'ivl': ['(2)', '(4)'], + 'leaves': [6, 9], + 'leaves_color_list': ['C0', 'C0'], + }) + + R = dendrogram(Z, 2, 'mtica', show_contracted=True) + plt.close() + R['dcoord'] = np.asarray(R['dcoord']) + assert_equal(R, {'color_list': ['C1', 'C0', 'C0', 'C0'], + 'dcoord': [[0.0, 138.0, 138.0, 0.0], + [0.0, 255.0, 255.0, 0.0], + [0.0, 268.0, 268.0, 255.0], + [138.0, 295.0, 295.0, 268.0]], + 'icoord': [[5.0, 5.0, 15.0, 15.0], + [35.0, 35.0, 45.0, 45.0], + [25.0, 25.0, 40.0, 40.0], + [10.0, 10.0, 32.5, 32.5]], + 'ivl': ['2', '5', '1', '0', '(2)'], + 'leaves': [2, 5, 1, 0, 7], + 'leaves_color_list': ['C1', 'C1', 'C0', 'C0', 'C0'], + }) + + @pytest.fixture + def dendrogram_lock(self): + return Lock() + + def test_dendrogram_colors(self, xp, dendrogram_lock): + # Tests dendrogram plots with alternate colors + Z = linkage(xp.asarray(hierarchy_test_data.ytdist), 'single') + + with dendrogram_lock: + # Global color palette might be changed concurrently + set_link_color_palette(['c', 'm', 'y', 'k']) + R = dendrogram(Z, no_plot=True, + above_threshold_color='g', color_threshold=250) + set_link_color_palette(['g', 'r', 'c', 'm', 'y', 'k']) + + color_list = R['color_list'] + assert_equal(color_list, ['c', 'm', 'g', 'g', 'g']) + + # reset color palette (global list) + set_link_color_palette(None) + + def test_dendrogram_leaf_colors_zero_dist(self, xp): + # tests that the colors of leafs are correct for tree + # with two identical points + x = xp.asarray([[1, 0, 0], + [0, 0, 1], + [0, 2, 0], + [0, 0, 1], + [0, 1, 0], + [0, 1, 0]]) + z = linkage(x, "single") + d = dendrogram(z, no_plot=True) + exp_colors = ['C0', 'C1', 'C1', 'C0', 'C2', 'C2'] + colors = d["leaves_color_list"] + assert_equal(colors, exp_colors) + + def test_dendrogram_leaf_colors(self, xp): + # tests that the colors are correct for a tree + # with two near points ((0, 0, 1.1) and (0, 0, 1)) + x = xp.asarray([[1, 0, 0], + [0, 0, 1.1], + [0, 2, 0], + [0, 0, 1], + [0, 1, 0], + [0, 1, 0]]) + z = linkage(x, "single") + d = dendrogram(z, no_plot=True) + exp_colors = ['C0', 'C1', 'C1', 'C0', 'C2', 'C2'] + colors = d["leaves_color_list"] + assert_equal(colors, exp_colors) + + +def calculate_maximum_distances(Z, xp): + # Used for testing correctness of maxdists. + n = Z.shape[0] + 1 + B = xp.zeros((n-1,), dtype=Z.dtype) + q = xp.zeros((3,)) + for i in range(0, n - 1): + q[:] = 0.0 + left = Z[i, 0] + right = Z[i, 1] + if left >= n: + q[0] = B[xp.asarray(left, dtype=xp.int64) - n] + if right >= n: + q[1] = B[xp.asarray(right, dtype=xp.int64) - n] + q[2] = Z[i, 2] + B[i] = xp.max(q) + return B + + +def calculate_maximum_inconsistencies(Z, R, k=3, xp=np): + # Used for testing correctness of maxinconsts. + n = Z.shape[0] + 1 + dtype = xp.result_type(Z, R) + B = xp.zeros((n-1,), dtype=dtype) + q = xp.zeros((3,)) + for i in range(0, n - 1): + q[:] = 0.0 + left = Z[i, 0] + right = Z[i, 1] + if left >= n: + q[0] = B[xp.asarray(left, dtype=xp.int64) - n] + if right >= n: + q[1] = B[xp.asarray(right, dtype=xp.int64) - n] + q[2] = R[i, k] + B[i] = xp.max(q) + return B + + +@pytest.mark.thread_unsafe +@skip_xp_backends(cpu_only=True) +def test_unsupported_uncondensed_distance_matrix_linkage_warning(xp): + assert_warns(ClusterWarning, linkage, xp.asarray([[0, 1], [1, 0]])) + + +def test_euclidean_linkage_value_error(xp): + for method in scipy.cluster.hierarchy._EUCLIDEAN_METHODS: + assert_raises(ValueError, linkage, xp.asarray([[1, 1], [1, 1]]), + method=method, metric='cityblock') + + +@skip_xp_backends(cpu_only=True) +def test_2x2_linkage(xp): + Z1 = linkage(xp.asarray([1]), method='single', metric='euclidean') + Z2 = linkage(xp.asarray([[0, 1], [0, 0]]), method='single', metric='euclidean') + xp_assert_close(Z1, Z2, rtol=1e-15) + + +@skip_xp_backends(cpu_only=True) +def test_node_compare(xp): + np.random.seed(23) + nobs = 50 + X = np.random.randn(nobs, 4) + X = xp.asarray(X) + Z = scipy.cluster.hierarchy.ward(X) + tree = to_tree(Z) + assert_(tree > tree.get_left()) + assert_(tree.get_right() > tree.get_left()) + assert_(tree.get_right() == tree.get_right()) + assert_(tree.get_right() != tree.get_left()) + + +@skip_xp_backends(np_only=True, reason='`cut_tree` uses non-standard indexing') +def test_cut_tree(xp): + np.random.seed(23) + nobs = 50 + X = np.random.randn(nobs, 4) + X = xp.asarray(X) + Z = scipy.cluster.hierarchy.ward(X) + cutree = cut_tree(Z) + + # cutree.dtype varies between int32 and int64 over platforms + xp_assert_close(cutree[:, 0], xp.arange(nobs), rtol=1e-15, check_dtype=False) + xp_assert_close(cutree[:, -1], xp.zeros(nobs), rtol=1e-15, check_dtype=False) + assert_equal(np.asarray(cutree).max(0), np.arange(nobs - 1, -1, -1)) + + xp_assert_close(cutree[:, [-5]], cut_tree(Z, n_clusters=5), rtol=1e-15) + xp_assert_close(cutree[:, [-5, -10]], cut_tree(Z, n_clusters=[5, 10]), rtol=1e-15) + xp_assert_close(cutree[:, [-10, -5]], cut_tree(Z, n_clusters=[10, 5]), rtol=1e-15) + + nodes = _order_cluster_tree(Z) + heights = xp.asarray([node.dist for node in nodes]) + + xp_assert_close(cutree[:, np.searchsorted(heights, [5])], + cut_tree(Z, height=5), rtol=1e-15) + xp_assert_close(cutree[:, np.searchsorted(heights, [5, 10])], + cut_tree(Z, height=[5, 10]), rtol=1e-15) + xp_assert_close(cutree[:, np.searchsorted(heights, [10, 5])], + cut_tree(Z, height=[10, 5]), rtol=1e-15) + + +@skip_xp_backends(cpu_only=True) +def test_optimal_leaf_ordering(xp): + # test with the distance vector y + Z = optimal_leaf_ordering(linkage(xp.asarray(hierarchy_test_data.ytdist)), + xp.asarray(hierarchy_test_data.ytdist)) + expectedZ = hierarchy_test_data.linkage_ytdist_single_olo + xp_assert_close(Z, xp.asarray(expectedZ), atol=1e-10) + + # test with the observation matrix X + Z = optimal_leaf_ordering(linkage(xp.asarray(hierarchy_test_data.X), 'ward'), + xp.asarray(hierarchy_test_data.X)) + expectedZ = hierarchy_test_data.linkage_X_ward_olo + xp_assert_close(Z, xp.asarray(expectedZ), atol=1e-06) + + +@skip_xp_backends(np_only=True, reason='`Heap` only supports NumPy backend') +def test_Heap(xp): + values = xp.asarray([2, -1, 0, -1.5, 3]) + heap = Heap(values) + + pair = heap.get_min() + assert_equal(pair['key'], 3) + assert_equal(pair['value'], -1.5) + + heap.remove_min() + pair = heap.get_min() + assert_equal(pair['key'], 1) + assert_equal(pair['value'], -1) + + heap.change_value(1, 2.5) + pair = heap.get_min() + assert_equal(pair['key'], 2) + assert_equal(pair['value'], 0) + + heap.remove_min() + heap.remove_min() + + heap.change_value(1, 10) + pair = heap.get_min() + assert_equal(pair['key'], 4) + assert_equal(pair['value'], 3) + + heap.remove_min() + pair = heap.get_min() + assert_equal(pair['key'], 1) + assert_equal(pair['value'], 10) + + +@skip_xp_backends(cpu_only=True) +def test_centroid_neg_distance(xp): + # gh-21011 + values = xp.asarray([0, 0, -1]) + with pytest.raises(ValueError): + # This is just checking that this doesn't crash + linkage(values, method='centroid') diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/tests/test_vq.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/tests/test_vq.py new file mode 100644 index 0000000000000000000000000000000000000000..d0321e7d81d79472ffc773baeee806aad14212fc --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/tests/test_vq.py @@ -0,0 +1,450 @@ +import warnings +import sys +from copy import deepcopy +from threading import Lock + +import numpy as np +from numpy.testing import ( + assert_array_equal, assert_equal, assert_, suppress_warnings +) +import pytest +from pytest import raises as assert_raises + +from scipy.cluster.vq import (kmeans, kmeans2, py_vq, vq, whiten, + ClusterError, _krandinit) +from scipy.cluster import _vq +from scipy.conftest import array_api_compatible +from scipy.sparse._sputils import matrix + +from scipy._lib import array_api_extra as xpx +from scipy._lib._array_api import ( + SCIPY_ARRAY_API, array_namespace, xp_copy, xp_assert_close, xp_assert_equal +) + +pytestmark = [array_api_compatible, pytest.mark.usefixtures("skip_xp_backends")] +skip_xp_backends = pytest.mark.skip_xp_backends + +TESTDATA_2D = np.array([ + -2.2, 1.17, -1.63, 1.69, -2.04, 4.38, -3.09, 0.95, -1.7, 4.79, -1.68, 0.68, + -2.26, 3.34, -2.29, 2.55, -1.72, -0.72, -1.99, 2.34, -2.75, 3.43, -2.45, + 2.41, -4.26, 3.65, -1.57, 1.87, -1.96, 4.03, -3.01, 3.86, -2.53, 1.28, + -4.0, 3.95, -1.62, 1.25, -3.42, 3.17, -1.17, 0.12, -3.03, -0.27, -2.07, + -0.55, -1.17, 1.34, -2.82, 3.08, -2.44, 0.24, -1.71, 2.48, -5.23, 4.29, + -2.08, 3.69, -1.89, 3.62, -2.09, 0.26, -0.92, 1.07, -2.25, 0.88, -2.25, + 2.02, -4.31, 3.86, -2.03, 3.42, -2.76, 0.3, -2.48, -0.29, -3.42, 3.21, + -2.3, 1.73, -2.84, 0.69, -1.81, 2.48, -5.24, 4.52, -2.8, 1.31, -1.67, + -2.34, -1.18, 2.17, -2.17, 2.82, -1.85, 2.25, -2.45, 1.86, -6.79, 3.94, + -2.33, 1.89, -1.55, 2.08, -1.36, 0.93, -2.51, 2.74, -2.39, 3.92, -3.33, + 2.99, -2.06, -0.9, -2.83, 3.35, -2.59, 3.05, -2.36, 1.85, -1.69, 1.8, + -1.39, 0.66, -2.06, 0.38, -1.47, 0.44, -4.68, 3.77, -5.58, 3.44, -2.29, + 2.24, -1.04, -0.38, -1.85, 4.23, -2.88, 0.73, -2.59, 1.39, -1.34, 1.75, + -1.95, 1.3, -2.45, 3.09, -1.99, 3.41, -5.55, 5.21, -1.73, 2.52, -2.17, + 0.85, -2.06, 0.49, -2.54, 2.07, -2.03, 1.3, -3.23, 3.09, -1.55, 1.44, + -0.81, 1.1, -2.99, 2.92, -1.59, 2.18, -2.45, -0.73, -3.12, -1.3, -2.83, + 0.2, -2.77, 3.24, -1.98, 1.6, -4.59, 3.39, -4.85, 3.75, -2.25, 1.71, -3.28, + 3.38, -1.74, 0.88, -2.41, 1.92, -2.24, 1.19, -2.48, 1.06, -1.68, -0.62, + -1.3, 0.39, -1.78, 2.35, -3.54, 2.44, -1.32, 0.66, -2.38, 2.76, -2.35, + 3.95, -1.86, 4.32, -2.01, -1.23, -1.79, 2.76, -2.13, -0.13, -5.25, 3.84, + -2.24, 1.59, -4.85, 2.96, -2.41, 0.01, -0.43, 0.13, -3.92, 2.91, -1.75, + -0.53, -1.69, 1.69, -1.09, 0.15, -2.11, 2.17, -1.53, 1.22, -2.1, -0.86, + -2.56, 2.28, -3.02, 3.33, -1.12, 3.86, -2.18, -1.19, -3.03, 0.79, -0.83, + 0.97, -3.19, 1.45, -1.34, 1.28, -2.52, 4.22, -4.53, 3.22, -1.97, 1.75, + -2.36, 3.19, -0.83, 1.53, -1.59, 1.86, -2.17, 2.3, -1.63, 2.71, -2.03, + 3.75, -2.57, -0.6, -1.47, 1.33, -1.95, 0.7, -1.65, 1.27, -1.42, 1.09, -3.0, + 3.87, -2.51, 3.06, -2.6, 0.74, -1.08, -0.03, -2.44, 1.31, -2.65, 2.99, + -1.84, 1.65, -4.76, 3.75, -2.07, 3.98, -2.4, 2.67, -2.21, 1.49, -1.21, + 1.22, -5.29, 2.38, -2.85, 2.28, -5.6, 3.78, -2.7, 0.8, -1.81, 3.5, -3.75, + 4.17, -1.29, 2.99, -5.92, 3.43, -1.83, 1.23, -1.24, -1.04, -2.56, 2.37, + -3.26, 0.39, -4.63, 2.51, -4.52, 3.04, -1.7, 0.36, -1.41, 0.04, -2.1, 1.0, + -1.87, 3.78, -4.32, 3.59, -2.24, 1.38, -1.99, -0.22, -1.87, 1.95, -0.84, + 2.17, -5.38, 3.56, -1.27, 2.9, -1.79, 3.31, -5.47, 3.85, -1.44, 3.69, + -2.02, 0.37, -1.29, 0.33, -2.34, 2.56, -1.74, -1.27, -1.97, 1.22, -2.51, + -0.16, -1.64, -0.96, -2.99, 1.4, -1.53, 3.31, -2.24, 0.45, -2.46, 1.71, + -2.88, 1.56, -1.63, 1.46, -1.41, 0.68, -1.96, 2.76, -1.61, + 2.11]).reshape((200, 2)) + + +# Global data +X = np.array([[3.0, 3], [4, 3], [4, 2], + [9, 2], [5, 1], [6, 2], [9, 4], + [5, 2], [5, 4], [7, 4], [6, 5]]) + +CODET1 = np.array([[3.0000, 3.0000], + [6.2000, 4.0000], + [5.8000, 1.8000]]) + +CODET2 = np.array([[11.0/3, 8.0/3], + [6.7500, 4.2500], + [6.2500, 1.7500]]) + +LABEL1 = np.array([0, 1, 2, 2, 2, 2, 1, 2, 1, 1, 1]) + + +class TestWhiten: + + def test_whiten(self, xp): + desired = xp.asarray([[5.08738849, 2.97091878], + [3.19909255, 0.69660580], + [4.51041982, 0.02640918], + [4.38567074, 0.95120889], + [2.32191480, 1.63195503]]) + + obs = xp.asarray([[0.98744510, 0.82766775], + [0.62093317, 0.19406729], + [0.87545741, 0.00735733], + [0.85124403, 0.26499712], + [0.45067590, 0.45464607]]) + xp_assert_close(whiten(obs), desired, rtol=1e-5) + + @pytest.fixture + def whiten_lock(self): + return Lock() + + @skip_xp_backends('jax.numpy', + reason='jax arrays do not support item assignment') + def test_whiten_zero_std(self, xp, whiten_lock): + desired = xp.asarray([[0., 1.0, 2.86666544], + [0., 1.0, 1.32460034], + [0., 1.0, 3.74382172]]) + + obs = xp.asarray([[0., 1., 0.74109533], + [0., 1., 0.34243798], + [0., 1., 0.96785929]]) + + with whiten_lock: + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter('always') + + xp_assert_close(whiten(obs), desired, rtol=1e-5) + + assert_equal(len(w), 1) + assert_(issubclass(w[-1].category, RuntimeWarning)) + + def test_whiten_not_finite(self, xp): + for bad_value in xp.nan, xp.inf, -xp.inf: + obs = xp.asarray([[0.98744510, bad_value], + [0.62093317, 0.19406729], + [0.87545741, 0.00735733], + [0.85124403, 0.26499712], + [0.45067590, 0.45464607]]) + assert_raises(ValueError, whiten, obs) + + @pytest.mark.skipif(SCIPY_ARRAY_API, + reason='`np.matrix` unsupported in array API mode') + def test_whiten_not_finite_matrix(self, xp): + for bad_value in np.nan, np.inf, -np.inf: + obs = matrix([[0.98744510, bad_value], + [0.62093317, 0.19406729], + [0.87545741, 0.00735733], + [0.85124403, 0.26499712], + [0.45067590, 0.45464607]]) + assert_raises(ValueError, whiten, obs) + + +class TestVq: + + @skip_xp_backends(cpu_only=True) + def test_py_vq(self, xp): + initc = np.concatenate([[X[0]], [X[1]], [X[2]]]) + # label1.dtype varies between int32 and int64 over platforms + label1 = py_vq(xp.asarray(X), xp.asarray(initc))[0] + xp_assert_equal(label1, xp.asarray(LABEL1, dtype=xp.int64), + check_dtype=False) + + @pytest.mark.skipif(SCIPY_ARRAY_API, + reason='`np.matrix` unsupported in array API mode') + def test_py_vq_matrix(self, xp): + initc = np.concatenate([[X[0]], [X[1]], [X[2]]]) + # label1.dtype varies between int32 and int64 over platforms + label1 = py_vq(matrix(X), matrix(initc))[0] + assert_array_equal(label1, LABEL1) + + @skip_xp_backends(np_only=True, reason='`_vq` only supports NumPy backend') + def test_vq(self, xp): + initc = np.concatenate([[X[0]], [X[1]], [X[2]]]) + label1, _ = _vq.vq(xp.asarray(X), xp.asarray(initc)) + assert_array_equal(label1, LABEL1) + _, _ = vq(xp.asarray(X), xp.asarray(initc)) + + @pytest.mark.skipif(SCIPY_ARRAY_API, + reason='`np.matrix` unsupported in array API mode') + def test_vq_matrix(self, xp): + initc = np.concatenate([[X[0]], [X[1]], [X[2]]]) + label1, _ = _vq.vq(matrix(X), matrix(initc)) + assert_array_equal(label1, LABEL1) + _, _ = vq(matrix(X), matrix(initc)) + + @skip_xp_backends(cpu_only=True) + def test_vq_1d(self, xp): + # Test special rank 1 vq algo, python implementation. + data = X[:, 0] + initc = data[:3] + a, b = _vq.vq(data, initc) + data = xp.asarray(data) + initc = xp.asarray(initc) + ta, tb = py_vq(data[:, np.newaxis], initc[:, np.newaxis]) + # ta.dtype varies between int32 and int64 over platforms + xp_assert_equal(ta, xp.asarray(a, dtype=xp.int64), check_dtype=False) + xp_assert_equal(tb, xp.asarray(b)) + + @skip_xp_backends(np_only=True, reason='`_vq` only supports NumPy backend') + def test__vq_sametype(self, xp): + a = xp.asarray([1.0, 2.0], dtype=xp.float64) + b = a.astype(xp.float32) + assert_raises(TypeError, _vq.vq, a, b) + + @skip_xp_backends(np_only=True, reason='`_vq` only supports NumPy backend') + def test__vq_invalid_type(self, xp): + a = xp.asarray([1, 2], dtype=int) + assert_raises(TypeError, _vq.vq, a, a) + + @skip_xp_backends(cpu_only=True) + def test_vq_large_nfeat(self, xp): + X = np.random.rand(20, 20) + code_book = np.random.rand(3, 20) + + codes0, dis0 = _vq.vq(X, code_book) + codes1, dis1 = py_vq( + xp.asarray(X), xp.asarray(code_book) + ) + xp_assert_close(dis1, xp.asarray(dis0), rtol=1e-5) + # codes1.dtype varies between int32 and int64 over platforms + xp_assert_equal(codes1, xp.asarray(codes0, dtype=xp.int64), check_dtype=False) + + X = X.astype(np.float32) + code_book = code_book.astype(np.float32) + + codes0, dis0 = _vq.vq(X, code_book) + codes1, dis1 = py_vq( + xp.asarray(X), xp.asarray(code_book) + ) + xp_assert_close(dis1, xp.asarray(dis0, dtype=xp.float64), rtol=1e-5) + # codes1.dtype varies between int32 and int64 over platforms + xp_assert_equal(codes1, xp.asarray(codes0, dtype=xp.int64), check_dtype=False) + + @skip_xp_backends(cpu_only=True) + def test_vq_large_features(self, xp): + X = np.random.rand(10, 5) * 1000000 + code_book = np.random.rand(2, 5) * 1000000 + + codes0, dis0 = _vq.vq(X, code_book) + codes1, dis1 = py_vq( + xp.asarray(X), xp.asarray(code_book) + ) + xp_assert_close(dis1, xp.asarray(dis0), rtol=1e-5) + # codes1.dtype varies between int32 and int64 over platforms + xp_assert_equal(codes1, xp.asarray(codes0, dtype=xp.int64), check_dtype=False) + + +# Whole class skipped on GPU for now; +# once pdist/cdist are hooked up for CuPy, more tests will work +@skip_xp_backends(cpu_only=True) +class TestKMean: + + def test_large_features(self, xp): + # Generate a data set with large values, and run kmeans on it to + # (regression for 1077). + d = 300 + n = 100 + + m1 = np.random.randn(d) + m2 = np.random.randn(d) + x = 10000 * np.random.randn(n, d) - 20000 * m1 + y = 10000 * np.random.randn(n, d) + 20000 * m2 + + data = np.empty((x.shape[0] + y.shape[0], d), np.float64) + data[:x.shape[0]] = x + data[x.shape[0]:] = y + + # use `seed` to ensure backwards compatibility after SPEC7 + kmeans(xp.asarray(data), 2, seed=1) + + def test_kmeans_simple(self, xp): + rng = np.random.default_rng(54321) + initc = np.concatenate([[X[0]], [X[1]], [X[2]]]) + code1 = kmeans(xp.asarray(X), xp.asarray(initc), iter=1, rng=rng)[0] + xp_assert_close(code1, xp.asarray(CODET2)) + + @pytest.mark.skipif(SCIPY_ARRAY_API, + reason='`np.matrix` unsupported in array API mode') + def test_kmeans_simple_matrix(self, xp): + rng = np.random.default_rng(54321) + initc = np.concatenate([[X[0]], [X[1]], [X[2]]]) + code1 = kmeans(matrix(X), matrix(initc), iter=1, rng=rng)[0] + xp_assert_close(code1, CODET2) + + def test_kmeans_lost_cluster(self, xp): + # This will cause kmeans to have a cluster with no points. + data = xp.asarray(TESTDATA_2D) + initk = xp.asarray([[-1.8127404, -0.67128041], + [2.04621601, 0.07401111], + [-2.31149087, -0.05160469]]) + + kmeans(data, initk) + with suppress_warnings() as sup: + sup.filter(UserWarning, + "One of the clusters is empty. Re-run kmeans with a " + "different initialization") + kmeans2(data, initk, missing='warn') + + assert_raises(ClusterError, kmeans2, data, initk, missing='raise') + + def test_kmeans2_simple(self, xp): + rng = np.random.default_rng(12345678) + initc = xp.asarray(np.concatenate([[X[0]], [X[1]], [X[2]]])) + arrays = [xp.asarray] if SCIPY_ARRAY_API else [np.asarray, matrix] + for tp in arrays: + code1 = kmeans2(tp(X), tp(initc), iter=1, rng=rng)[0] + code2 = kmeans2(tp(X), tp(initc), iter=2, rng=rng)[0] + + xp_assert_close(code1, xp.asarray(CODET1)) + xp_assert_close(code2, xp.asarray(CODET2)) + + @pytest.mark.skipif(SCIPY_ARRAY_API, + reason='`np.matrix` unsupported in array API mode') + def test_kmeans2_simple_matrix(self, xp): + rng = np.random.default_rng(12345678) + initc = xp.asarray(np.concatenate([[X[0]], [X[1]], [X[2]]])) + code1 = kmeans2(matrix(X), matrix(initc), iter=1, rng=rng)[0] + code2 = kmeans2(matrix(X), matrix(initc), iter=2, rng=rng)[0] + + xp_assert_close(code1, CODET1) + xp_assert_close(code2, CODET2) + + def test_kmeans2_rank1(self, xp): + data = xp.asarray(TESTDATA_2D) + data1 = data[:, 0] + + initc = data1[:3] + code = xp_copy(initc, xp=xp) + + # use `seed` to ensure backwards compatibility after SPEC7 + kmeans2(data1, code, iter=1, seed=1)[0] + kmeans2(data1, code, iter=2)[0] + + def test_kmeans2_rank1_2(self, xp): + data = xp.asarray(TESTDATA_2D) + data1 = data[:, 0] + kmeans2(data1, 2, iter=1) + + def test_kmeans2_high_dim(self, xp): + # test kmeans2 when the number of dimensions exceeds the number + # of input points + data = xp.asarray(TESTDATA_2D) + data = xp.reshape(data, (20, 20))[:10, :] + kmeans2(data, 2) + + @skip_xp_backends('jax.numpy', + reason='jax arrays do not support item assignment') + def test_kmeans2_init(self, xp): + rng = np.random.default_rng(12345678) + data = xp.asarray(TESTDATA_2D) + k = 3 + + kmeans2(data, k, minit='points', rng=rng) + kmeans2(data[:, 1], k, minit='points', rng=rng) # special case (1-D) + + kmeans2(data, k, minit='++', rng=rng) + kmeans2(data[:, 1], k, minit='++', rng=rng) # special case (1-D) + + # minit='random' can give warnings, filter those + with suppress_warnings() as sup: + sup.filter(message="One of the clusters is empty. Re-run.") + kmeans2(data, k, minit='random', rng=rng) + kmeans2(data[:, 1], k, minit='random', rng=rng) # special case (1-D) + + @pytest.fixture + def krand_lock(self): + return Lock() + + @pytest.mark.skipif(sys.platform == 'win32', + reason='Fails with MemoryError in Wine.') + def test_krandinit(self, xp, krand_lock): + data = xp.asarray(TESTDATA_2D) + datas = [xp.reshape(data, (200, 2)), + xp.reshape(data, (20, 20))[:10, :]] + k = int(1e6) + xp_test = array_namespace(data) + with krand_lock: + for data in datas: + rng = np.random.default_rng(1234) + init = _krandinit(data, k, rng, xp_test) + orig_cov = xpx.cov(data.T, xp=xp_test) + init_cov = xpx.cov(init.T, xp=xp_test) + xp_assert_close(orig_cov, init_cov, atol=1.1e-2) + + def test_kmeans2_empty(self, xp): + # Regression test for gh-1032. + assert_raises(ValueError, kmeans2, xp.asarray([]), 2) + + def test_kmeans_0k(self, xp): + # Regression test for gh-1073: fail when k arg is 0. + assert_raises(ValueError, kmeans, xp.asarray(X), 0) + assert_raises(ValueError, kmeans2, xp.asarray(X), 0) + assert_raises(ValueError, kmeans2, xp.asarray(X), xp.asarray([])) + + def test_kmeans_large_thres(self, xp): + # Regression test for gh-1774 + x = xp.asarray([1, 2, 3, 4, 10], dtype=xp.float64) + res = kmeans(x, 1, thresh=1e16) + xp_assert_close(res[0], xp.asarray([4.], dtype=xp.float64)) + xp_assert_close(res[1], xp.asarray(2.3999999999999999, dtype=xp.float64)[()]) + + @skip_xp_backends('jax.numpy', + reason='jax arrays do not support item assignment') + def test_kmeans2_kpp_low_dim(self, xp): + # Regression test for gh-11462 + rng = np.random.default_rng(2358792345678234568) + prev_res = xp.asarray([[-1.95266667, 0.898], + [-3.153375, 3.3945]], dtype=xp.float64) + res, _ = kmeans2(xp.asarray(TESTDATA_2D), 2, minit='++', rng=rng) + xp_assert_close(res, prev_res) + + @pytest.mark.thread_unsafe + @skip_xp_backends('jax.numpy', + reason='jax arrays do not support item assignment') + def test_kmeans2_kpp_high_dim(self, xp): + # Regression test for gh-11462 + rng = np.random.default_rng(23587923456834568) + n_dim = 100 + size = 10 + centers = np.vstack([5 * np.ones(n_dim), + -5 * np.ones(n_dim)]) + + data = np.vstack([ + rng.multivariate_normal(centers[0], np.eye(n_dim), size=size), + rng.multivariate_normal(centers[1], np.eye(n_dim), size=size) + ]) + + data = xp.asarray(data) + res, _ = kmeans2(data, 2, minit='++', rng=rng) + xp_assert_equal(xp.sign(res), xp.sign(xp.asarray(centers))) + + def test_kmeans_diff_convergence(self, xp): + # Regression test for gh-8727 + obs = xp.asarray([-3, -1, 0, 1, 1, 8], dtype=xp.float64) + res = kmeans(obs, xp.asarray([-3., 0.99])) + xp_assert_close(res[0], xp.asarray([-0.4, 8.], dtype=xp.float64)) + xp_assert_close(res[1], xp.asarray(1.0666666666666667, dtype=xp.float64)[()]) + + @skip_xp_backends('jax.numpy', + reason='jax arrays do not support item assignment') + def test_kmeans_and_kmeans2_random_seed(self, xp): + + seed_list = [ + 1234, np.random.RandomState(1234), np.random.default_rng(1234) + ] + + for seed in seed_list: + seed1 = deepcopy(seed) + seed2 = deepcopy(seed) + data = xp.asarray(TESTDATA_2D) + # test for kmeans + res1, _ = kmeans(data, 2, seed=seed1) + res2, _ = kmeans(data, 2, seed=seed2) + xp_assert_close(res1, res2) # should be same results + # test for kmeans2 + for minit in ["random", "points", "++"]: + res1, _ = kmeans2(data, 2, minit=minit, seed=seed1) + res2, _ = kmeans2(data, 2, minit=minit, seed=seed2) + xp_assert_close(res1, res2) # should be same results diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/vq.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/vq.py new file mode 100644 index 0000000000000000000000000000000000000000..a791e2956070d0785556dd9c4f877004ae50cd9f --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/vq.py @@ -0,0 +1,828 @@ +""" +K-means clustering and vector quantization (:mod:`scipy.cluster.vq`) +==================================================================== + +Provides routines for k-means clustering, generating code books +from k-means models and quantizing vectors by comparing them with +centroids in a code book. + +.. autosummary:: + :toctree: generated/ + + whiten -- Normalize a group of observations so each feature has unit variance + vq -- Calculate code book membership of a set of observation vectors + kmeans -- Perform k-means on a set of observation vectors forming k clusters + kmeans2 -- A different implementation of k-means with more methods + -- for initializing centroids + +Background information +---------------------- +The k-means algorithm takes as input the number of clusters to +generate, k, and a set of observation vectors to cluster. It +returns a set of centroids, one for each of the k clusters. An +observation vector is classified with the cluster number or +centroid index of the centroid closest to it. + +A vector v belongs to cluster i if it is closer to centroid i than +any other centroid. If v belongs to i, we say centroid i is the +dominating centroid of v. The k-means algorithm tries to +minimize distortion, which is defined as the sum of the squared distances +between each observation vector and its dominating centroid. +The minimization is achieved by iteratively reclassifying +the observations into clusters and recalculating the centroids until +a configuration is reached in which the centroids are stable. One can +also define a maximum number of iterations. + +Since vector quantization is a natural application for k-means, +information theory terminology is often used. The centroid index +or cluster index is also referred to as a "code" and the table +mapping codes to centroids and, vice versa, is often referred to as a +"code book". The result of k-means, a set of centroids, can be +used to quantize vectors. Quantization aims to find an encoding of +vectors that reduces the expected distortion. + +All routines expect obs to be an M by N array, where the rows are +the observation vectors. The codebook is a k by N array, where the +ith row is the centroid of code word i. The observation vectors +and centroids have the same feature dimension. + +As an example, suppose we wish to compress a 24-bit color image +(each pixel is represented by one byte for red, one for blue, and +one for green) before sending it over the web. By using a smaller +8-bit encoding, we can reduce the amount of data by two +thirds. Ideally, the colors for each of the 256 possible 8-bit +encoding values should be chosen to minimize distortion of the +color. Running k-means with k=256 generates a code book of 256 +codes, which fills up all possible 8-bit sequences. Instead of +sending a 3-byte value for each pixel, the 8-bit centroid index +(or code word) of the dominating centroid is transmitted. The code +book is also sent over the wire so each 8-bit code can be +translated back to a 24-bit pixel value representation. If the +image of interest was of an ocean, we would expect many 24-bit +blues to be represented by 8-bit codes. If it was an image of a +human face, more flesh-tone colors would be represented in the +code book. + +""" +import warnings +import numpy as np +from collections import deque +from scipy._lib._array_api import ( + _asarray, array_namespace, xp_size, xp_copy +) +from scipy._lib._util import (check_random_state, rng_integers, + _transition_to_rng) +from scipy._lib import array_api_extra as xpx +from scipy.spatial.distance import cdist + +from . import _vq + +__docformat__ = 'restructuredtext' + +__all__ = ['whiten', 'vq', 'kmeans', 'kmeans2'] + + +class ClusterError(Exception): + pass + + +def whiten(obs, check_finite=True): + """ + Normalize a group of observations on a per feature basis. + + Before running k-means, it is beneficial to rescale each feature + dimension of the observation set by its standard deviation (i.e. "whiten" + it - as in "white noise" where each frequency has equal power). + Each feature is divided by its standard deviation across all observations + to give it unit variance. + + Parameters + ---------- + obs : ndarray + Each row of the array is an observation. The + columns are the features seen during each observation. + + >>> # f0 f1 f2 + >>> obs = [[ 1., 1., 1.], #o0 + ... [ 2., 2., 2.], #o1 + ... [ 3., 3., 3.], #o2 + ... [ 4., 4., 4.]] #o3 + + check_finite : bool, optional + Whether to check that the input matrices contain only finite numbers. + Disabling may give a performance gain, but may result in problems + (crashes, non-termination) if the inputs do contain infinities or NaNs. + Default: True + + Returns + ------- + result : ndarray + Contains the values in `obs` scaled by the standard deviation + of each column. + + Examples + -------- + >>> import numpy as np + >>> from scipy.cluster.vq import whiten + >>> features = np.array([[1.9, 2.3, 1.7], + ... [1.5, 2.5, 2.2], + ... [0.8, 0.6, 1.7,]]) + >>> whiten(features) + array([[ 4.17944278, 2.69811351, 7.21248917], + [ 3.29956009, 2.93273208, 9.33380951], + [ 1.75976538, 0.7038557 , 7.21248917]]) + + """ + xp = array_namespace(obs) + obs = _asarray(obs, check_finite=check_finite, xp=xp) + std_dev = xp.std(obs, axis=0) + zero_std_mask = std_dev == 0 + if xp.any(zero_std_mask): + std_dev[zero_std_mask] = 1.0 + warnings.warn("Some columns have standard deviation zero. " + "The values of these columns will not change.", + RuntimeWarning, stacklevel=2) + return obs / std_dev + + +def vq(obs, code_book, check_finite=True): + """ + Assign codes from a code book to observations. + + Assigns a code from a code book to each observation. Each + observation vector in the 'M' by 'N' `obs` array is compared with the + centroids in the code book and assigned the code of the closest + centroid. + + The features in `obs` should have unit variance, which can be + achieved by passing them through the whiten function. The code + book can be created with the k-means algorithm or a different + encoding algorithm. + + Parameters + ---------- + obs : ndarray + Each row of the 'M' x 'N' array is an observation. The columns are + the "features" seen during each observation. The features must be + whitened first using the whiten function or something equivalent. + code_book : ndarray + The code book is usually generated using the k-means algorithm. + Each row of the array holds a different code, and the columns are + the features of the code. + + >>> # f0 f1 f2 f3 + >>> code_book = [ + ... [ 1., 2., 3., 4.], #c0 + ... [ 1., 2., 3., 4.], #c1 + ... [ 1., 2., 3., 4.]] #c2 + + check_finite : bool, optional + Whether to check that the input matrices contain only finite numbers. + Disabling may give a performance gain, but may result in problems + (crashes, non-termination) if the inputs do contain infinities or NaNs. + Default: True + + Returns + ------- + code : ndarray + A length M array holding the code book index for each observation. + dist : ndarray + The distortion (distance) between the observation and its nearest + code. + + Examples + -------- + >>> import numpy as np + >>> from scipy.cluster.vq import vq + >>> code_book = np.array([[1., 1., 1.], + ... [2., 2., 2.]]) + >>> features = np.array([[1.9, 2.3, 1.7], + ... [1.5, 2.5, 2.2], + ... [0.8, 0.6, 1.7]]) + >>> vq(features, code_book) + (array([1, 1, 0], dtype=int32), array([0.43588989, 0.73484692, 0.83066239])) + + """ + xp = array_namespace(obs, code_book) + obs = _asarray(obs, xp=xp, check_finite=check_finite) + code_book = _asarray(code_book, xp=xp, check_finite=check_finite) + ct = xp.result_type(obs, code_book) + + c_obs = xp.astype(obs, ct, copy=False) + c_code_book = xp.astype(code_book, ct, copy=False) + + if xp.isdtype(ct, kind='real floating'): + c_obs = np.asarray(c_obs) + c_code_book = np.asarray(c_code_book) + result = _vq.vq(c_obs, c_code_book) + return xp.asarray(result[0]), xp.asarray(result[1]) + return py_vq(obs, code_book, check_finite=False) + + +def py_vq(obs, code_book, check_finite=True): + """ Python version of vq algorithm. + + The algorithm computes the Euclidean distance between each + observation and every frame in the code_book. + + Parameters + ---------- + obs : ndarray + Expects a rank 2 array. Each row is one observation. + code_book : ndarray + Code book to use. Same format than obs. Should have same number of + features (e.g., columns) than obs. + check_finite : bool, optional + Whether to check that the input matrices contain only finite numbers. + Disabling may give a performance gain, but may result in problems + (crashes, non-termination) if the inputs do contain infinities or NaNs. + Default: True + + Returns + ------- + code : ndarray + code[i] gives the label of the ith obversation; its code is + code_book[code[i]]. + mind_dist : ndarray + min_dist[i] gives the distance between the ith observation and its + corresponding code. + + Notes + ----- + This function is slower than the C version but works for + all input types. If the inputs have the wrong types for the + C versions of the function, this one is called as a last resort. + + It is about 20 times slower than the C version. + + """ + xp = array_namespace(obs, code_book) + obs = _asarray(obs, xp=xp, check_finite=check_finite) + code_book = _asarray(code_book, xp=xp, check_finite=check_finite) + + if obs.ndim != code_book.ndim: + raise ValueError("Observation and code_book should have the same rank") + + if obs.ndim == 1: + obs = obs[:, xp.newaxis] + code_book = code_book[:, xp.newaxis] + + # Once `cdist` has array API support, this `xp.asarray` call can be removed + dist = xp.asarray(cdist(obs, code_book)) + code = xp.argmin(dist, axis=1) + min_dist = xp.min(dist, axis=1) + return code, min_dist + + +def _kmeans(obs, guess, thresh=1e-5, xp=None): + """ "raw" version of k-means. + + Returns + ------- + code_book + The lowest distortion codebook found. + avg_dist + The average distance a observation is from a code in the book. + Lower means the code_book matches the data better. + + See Also + -------- + kmeans : wrapper around k-means + + Examples + -------- + Note: not whitened in this example. + + >>> import numpy as np + >>> from scipy.cluster.vq import _kmeans + >>> features = np.array([[ 1.9,2.3], + ... [ 1.5,2.5], + ... [ 0.8,0.6], + ... [ 0.4,1.8], + ... [ 1.0,1.0]]) + >>> book = np.array((features[0],features[2])) + >>> _kmeans(features,book) + (array([[ 1.7 , 2.4 ], + [ 0.73333333, 1.13333333]]), 0.40563916697728591) + + """ + xp = np if xp is None else xp + code_book = guess + diff = xp.inf + prev_avg_dists = deque([diff], maxlen=2) + while diff > thresh: + # compute membership and distances between obs and code_book + obs_code, distort = vq(obs, code_book, check_finite=False) + prev_avg_dists.append(xp.mean(distort, axis=-1)) + # recalc code_book as centroids of associated obs + obs = np.asarray(obs) + obs_code = np.asarray(obs_code) + code_book, has_members = _vq.update_cluster_means(obs, obs_code, + code_book.shape[0]) + obs = xp.asarray(obs) + obs_code = xp.asarray(obs_code) + code_book = xp.asarray(code_book) + has_members = xp.asarray(has_members) + code_book = code_book[has_members] + diff = xp.abs(prev_avg_dists[0] - prev_avg_dists[1]) + + return code_book, prev_avg_dists[1] + + +@_transition_to_rng("seed") +def kmeans(obs, k_or_guess, iter=20, thresh=1e-5, check_finite=True, + *, rng=None): + """ + Performs k-means on a set of observation vectors forming k clusters. + + The k-means algorithm adjusts the classification of the observations + into clusters and updates the cluster centroids until the position of + the centroids is stable over successive iterations. In this + implementation of the algorithm, the stability of the centroids is + determined by comparing the absolute value of the change in the average + Euclidean distance between the observations and their corresponding + centroids against a threshold. This yields + a code book mapping centroids to codes and vice versa. + + Parameters + ---------- + obs : ndarray + Each row of the M by N array is an observation vector. The + columns are the features seen during each observation. + The features must be whitened first with the `whiten` function. + + k_or_guess : int or ndarray + The number of centroids to generate. A code is assigned to + each centroid, which is also the row index of the centroid + in the code_book matrix generated. + + The initial k centroids are chosen by randomly selecting + observations from the observation matrix. Alternatively, + passing a k by N array specifies the initial k centroids. + + iter : int, optional + The number of times to run k-means, returning the codebook + with the lowest distortion. This argument is ignored if + initial centroids are specified with an array for the + ``k_or_guess`` parameter. This parameter does not represent the + number of iterations of the k-means algorithm. + + thresh : float, optional + Terminates the k-means algorithm if the change in + distortion since the last k-means iteration is less than + or equal to threshold. + + check_finite : bool, optional + Whether to check that the input matrices contain only finite numbers. + Disabling may give a performance gain, but may result in problems + (crashes, non-termination) if the inputs do contain infinities or NaNs. + Default: True + rng : `numpy.random.Generator`, optional + Pseudorandom number generator state. When `rng` is None, a new + `numpy.random.Generator` is created using entropy from the + operating system. Types other than `numpy.random.Generator` are + passed to `numpy.random.default_rng` to instantiate a ``Generator``. + + Returns + ------- + codebook : ndarray + A k by N array of k centroids. The ith centroid + codebook[i] is represented with the code i. The centroids + and codes generated represent the lowest distortion seen, + not necessarily the globally minimal distortion. + Note that the number of centroids is not necessarily the same as the + ``k_or_guess`` parameter, because centroids assigned to no observations + are removed during iterations. + + distortion : float + The mean (non-squared) Euclidean distance between the observations + passed and the centroids generated. Note the difference to the standard + definition of distortion in the context of the k-means algorithm, which + is the sum of the squared distances. + + See Also + -------- + kmeans2 : a different implementation of k-means clustering + with more methods for generating initial centroids but without + using a distortion change threshold as a stopping criterion. + + whiten : must be called prior to passing an observation matrix + to kmeans. + + Notes + ----- + For more functionalities or optimal performance, you can use + `sklearn.cluster.KMeans `_. + `This `_ + is a benchmark result of several implementations. + + Examples + -------- + >>> import numpy as np + >>> from scipy.cluster.vq import vq, kmeans, whiten + >>> import matplotlib.pyplot as plt + >>> features = np.array([[ 1.9,2.3], + ... [ 1.5,2.5], + ... [ 0.8,0.6], + ... [ 0.4,1.8], + ... [ 0.1,0.1], + ... [ 0.2,1.8], + ... [ 2.0,0.5], + ... [ 0.3,1.5], + ... [ 1.0,1.0]]) + >>> whitened = whiten(features) + >>> book = np.array((whitened[0],whitened[2])) + >>> kmeans(whitened,book) + (array([[ 2.3110306 , 2.86287398], # random + [ 0.93218041, 1.24398691]]), 0.85684700941625547) + + >>> codes = 3 + >>> kmeans(whitened,codes) + (array([[ 2.3110306 , 2.86287398], # random + [ 1.32544402, 0.65607529], + [ 0.40782893, 2.02786907]]), 0.5196582527686241) + + >>> # Create 50 datapoints in two clusters a and b + >>> pts = 50 + >>> rng = np.random.default_rng() + >>> a = rng.multivariate_normal([0, 0], [[4, 1], [1, 4]], size=pts) + >>> b = rng.multivariate_normal([30, 10], + ... [[10, 2], [2, 1]], + ... size=pts) + >>> features = np.concatenate((a, b)) + >>> # Whiten data + >>> whitened = whiten(features) + >>> # Find 2 clusters in the data + >>> codebook, distortion = kmeans(whitened, 2) + >>> # Plot whitened data and cluster centers in red + >>> plt.scatter(whitened[:, 0], whitened[:, 1]) + >>> plt.scatter(codebook[:, 0], codebook[:, 1], c='r') + >>> plt.show() + + """ + if isinstance(k_or_guess, int): + xp = array_namespace(obs) + else: + xp = array_namespace(obs, k_or_guess) + obs = _asarray(obs, xp=xp, check_finite=check_finite) + guess = _asarray(k_or_guess, xp=xp, check_finite=check_finite) + if iter < 1: + raise ValueError(f"iter must be at least 1, got {iter}") + + # Determine whether a count (scalar) or an initial guess (array) was passed. + if xp_size(guess) != 1: + if xp_size(guess) < 1: + raise ValueError(f"Asked for 0 clusters. Initial book was {guess}") + return _kmeans(obs, guess, thresh=thresh, xp=xp) + + # k_or_guess is a scalar, now verify that it's an integer + k = int(guess) + if k != guess: + raise ValueError("If k_or_guess is a scalar, it must be an integer.") + if k < 1: + raise ValueError("Asked for %d clusters." % k) + + rng = check_random_state(rng) + + # initialize best distance value to a large value + best_dist = xp.inf + for i in range(iter): + # the initial code book is randomly selected from observations + guess = _kpoints(obs, k, rng, xp) + book, dist = _kmeans(obs, guess, thresh=thresh, xp=xp) + if dist < best_dist: + best_book = book + best_dist = dist + return best_book, best_dist + + +def _kpoints(data, k, rng, xp): + """Pick k points at random in data (one row = one observation). + + Parameters + ---------- + data : ndarray + Expect a rank 1 or 2 array. Rank 1 are assumed to describe one + dimensional data, rank 2 multidimensional data, in which case one + row is one observation. + k : int + Number of samples to generate. + rng : `numpy.random.Generator` or `numpy.random.RandomState` + Random number generator. + + Returns + ------- + x : ndarray + A 'k' by 'N' containing the initial centroids + + """ + idx = rng.choice(data.shape[0], size=int(k), replace=False) + # convert to array with default integer dtype (avoids numpy#25607) + idx = xp.asarray(idx, dtype=xp.asarray([1]).dtype) + return xp.take(data, idx, axis=0) + + +def _krandinit(data, k, rng, xp): + """Returns k samples of a random variable whose parameters depend on data. + + More precisely, it returns k observations sampled from a Gaussian random + variable whose mean and covariances are the ones estimated from the data. + + Parameters + ---------- + data : ndarray + Expect a rank 1 or 2 array. Rank 1 is assumed to describe 1-D + data, rank 2 multidimensional data, in which case one + row is one observation. + k : int + Number of samples to generate. + rng : `numpy.random.Generator` or `numpy.random.RandomState` + Random number generator. + + Returns + ------- + x : ndarray + A 'k' by 'N' containing the initial centroids + + """ + mu = xp.mean(data, axis=0) + k = np.asarray(k) + + if data.ndim == 1: + _cov = xpx.cov(data, xp=xp) + x = rng.standard_normal(size=k) + x = xp.asarray(x) + x *= xp.sqrt(_cov) + elif data.shape[1] > data.shape[0]: + # initialize when the covariance matrix is rank deficient + _, s, vh = xp.linalg.svd(data - mu, full_matrices=False) + x = rng.standard_normal(size=(k, xp_size(s))) + x = xp.asarray(x) + sVh = s[:, None] * vh / xp.sqrt(data.shape[0] - xp.asarray(1.)) + x = x @ sVh + else: + _cov = xpx.atleast_nd(xpx.cov(data.T, xp=xp), ndim=2, xp=xp) + + # k rows, d cols (one row = one obs) + # Generate k sample of a random variable ~ Gaussian(mu, cov) + x = rng.standard_normal(size=(k, xp_size(mu))) + x = xp.asarray(x) + x = x @ xp.linalg.cholesky(_cov).T + + x += mu + return x + + +def _kpp(data, k, rng, xp): + """ Picks k points in the data based on the kmeans++ method. + + Parameters + ---------- + data : ndarray + Expect a rank 1 or 2 array. Rank 1 is assumed to describe 1-D + data, rank 2 multidimensional data, in which case one + row is one observation. + k : int + Number of samples to generate. + rng : `numpy.random.Generator` or `numpy.random.RandomState` + Random number generator. + + Returns + ------- + init : ndarray + A 'k' by 'N' containing the initial centroids. + + References + ---------- + .. [1] D. Arthur and S. Vassilvitskii, "k-means++: the advantages of + careful seeding", Proceedings of the Eighteenth Annual ACM-SIAM Symposium + on Discrete Algorithms, 2007. + """ + + ndim = len(data.shape) + if ndim == 1: + data = data[:, None] + + dims = data.shape[1] + + init = xp.empty((int(k), dims)) + + for i in range(k): + if i == 0: + init[i, :] = data[rng_integers(rng, data.shape[0]), :] + + else: + D2 = cdist(init[:i,:], data, metric='sqeuclidean').min(axis=0) + probs = D2/D2.sum() + cumprobs = probs.cumsum() + r = rng.uniform() + cumprobs = np.asarray(cumprobs) + init[i, :] = data[np.searchsorted(cumprobs, r), :] + + if ndim == 1: + init = init[:, 0] + return init + + +_valid_init_meth = {'random': _krandinit, 'points': _kpoints, '++': _kpp} + + +def _missing_warn(): + """Print a warning when called.""" + warnings.warn("One of the clusters is empty. " + "Re-run kmeans with a different initialization.", + stacklevel=3) + + +def _missing_raise(): + """Raise a ClusterError when called.""" + raise ClusterError("One of the clusters is empty. " + "Re-run kmeans with a different initialization.") + + +_valid_miss_meth = {'warn': _missing_warn, 'raise': _missing_raise} + + +@_transition_to_rng("seed") +def kmeans2(data, k, iter=10, thresh=1e-5, minit='random', + missing='warn', check_finite=True, *, rng=None): + """ + Classify a set of observations into k clusters using the k-means algorithm. + + The algorithm attempts to minimize the Euclidean distance between + observations and centroids. Several initialization methods are + included. + + Parameters + ---------- + data : ndarray + A 'M' by 'N' array of 'M' observations in 'N' dimensions or a length + 'M' array of 'M' 1-D observations. + k : int or ndarray + The number of clusters to form as well as the number of + centroids to generate. If `minit` initialization string is + 'matrix', or if a ndarray is given instead, it is + interpreted as initial cluster to use instead. + iter : int, optional + Number of iterations of the k-means algorithm to run. Note + that this differs in meaning from the iters parameter to + the kmeans function. + thresh : float, optional + (not used yet) + minit : str, optional + Method for initialization. Available methods are 'random', + 'points', '++' and 'matrix': + + 'random': generate k centroids from a Gaussian with mean and + variance estimated from the data. + + 'points': choose k observations (rows) at random from data for + the initial centroids. + + '++': choose k observations accordingly to the kmeans++ method + (careful seeding) + + 'matrix': interpret the k parameter as a k by M (or length k + array for 1-D data) array of initial centroids. + missing : str, optional + Method to deal with empty clusters. Available methods are + 'warn' and 'raise': + + 'warn': give a warning and continue. + + 'raise': raise an ClusterError and terminate the algorithm. + check_finite : bool, optional + Whether to check that the input matrices contain only finite numbers. + Disabling may give a performance gain, but may result in problems + (crashes, non-termination) if the inputs do contain infinities or NaNs. + Default: True + rng : `numpy.random.Generator`, optional + Pseudorandom number generator state. When `rng` is None, a new + `numpy.random.Generator` is created using entropy from the + operating system. Types other than `numpy.random.Generator` are + passed to `numpy.random.default_rng` to instantiate a ``Generator``. + + Returns + ------- + centroid : ndarray + A 'k' by 'N' array of centroids found at the last iteration of + k-means. + label : ndarray + label[i] is the code or index of the centroid the + ith observation is closest to. + + See Also + -------- + kmeans + + References + ---------- + .. [1] D. Arthur and S. Vassilvitskii, "k-means++: the advantages of + careful seeding", Proceedings of the Eighteenth Annual ACM-SIAM Symposium + on Discrete Algorithms, 2007. + + Examples + -------- + >>> from scipy.cluster.vq import kmeans2 + >>> import matplotlib.pyplot as plt + >>> import numpy as np + + Create z, an array with shape (100, 2) containing a mixture of samples + from three multivariate normal distributions. + + >>> rng = np.random.default_rng() + >>> a = rng.multivariate_normal([0, 6], [[2, 1], [1, 1.5]], size=45) + >>> b = rng.multivariate_normal([2, 0], [[1, -1], [-1, 3]], size=30) + >>> c = rng.multivariate_normal([6, 4], [[5, 0], [0, 1.2]], size=25) + >>> z = np.concatenate((a, b, c)) + >>> rng.shuffle(z) + + Compute three clusters. + + >>> centroid, label = kmeans2(z, 3, minit='points') + >>> centroid + array([[ 2.22274463, -0.61666946], # may vary + [ 0.54069047, 5.86541444], + [ 6.73846769, 4.01991898]]) + + How many points are in each cluster? + + >>> counts = np.bincount(label) + >>> counts + array([29, 51, 20]) # may vary + + Plot the clusters. + + >>> w0 = z[label == 0] + >>> w1 = z[label == 1] + >>> w2 = z[label == 2] + >>> plt.plot(w0[:, 0], w0[:, 1], 'o', alpha=0.5, label='cluster 0') + >>> plt.plot(w1[:, 0], w1[:, 1], 'd', alpha=0.5, label='cluster 1') + >>> plt.plot(w2[:, 0], w2[:, 1], 's', alpha=0.5, label='cluster 2') + >>> plt.plot(centroid[:, 0], centroid[:, 1], 'k*', label='centroids') + >>> plt.axis('equal') + >>> plt.legend(shadow=True) + >>> plt.show() + + """ + if int(iter) < 1: + raise ValueError(f"Invalid iter ({iter}), must be a positive integer.") + try: + miss_meth = _valid_miss_meth[missing] + except KeyError as e: + raise ValueError(f"Unknown missing method {missing!r}") from e + + if isinstance(k, int): + xp = array_namespace(data) + else: + xp = array_namespace(data, k) + data = _asarray(data, xp=xp, check_finite=check_finite) + code_book = xp_copy(k, xp=xp) + if data.ndim == 1: + d = 1 + elif data.ndim == 2: + d = data.shape[1] + else: + raise ValueError("Input of rank > 2 is not supported.") + + if xp_size(data) < 1 or xp_size(code_book) < 1: + raise ValueError("Empty input is not supported.") + + # If k is not a single value, it should be compatible with data's shape + if minit == 'matrix' or xp_size(code_book) > 1: + if data.ndim != code_book.ndim: + raise ValueError("k array doesn't match data rank") + nc = code_book.shape[0] + if data.ndim > 1 and code_book.shape[1] != d: + raise ValueError("k array doesn't match data dimension") + else: + nc = int(code_book) + + if nc < 1: + raise ValueError("Cannot ask kmeans2 for %d clusters" + " (k was %s)" % (nc, code_book)) + elif nc != code_book: + warnings.warn("k was not an integer, was converted.", stacklevel=2) + + try: + init_meth = _valid_init_meth[minit] + except KeyError as e: + raise ValueError(f"Unknown init method {minit!r}") from e + else: + rng = check_random_state(rng) + code_book = init_meth(data, code_book, rng, xp) + + data = np.asarray(data) + code_book = np.asarray(code_book) + for i in range(iter): + # Compute the nearest neighbor for each obs using the current code book + label = vq(data, code_book, check_finite=check_finite)[0] + # Update the code book by computing centroids + new_code_book, has_members = _vq.update_cluster_means(data, label, nc) + if not has_members.all(): + miss_meth() + # Set the empty clusters to their previous positions + new_code_book[~has_members] = code_book[~has_members] + code_book = new_code_book + + return xp.asarray(code_book), xp.asarray(label) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/constants/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/constants/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..fdf939b249c17256b622c2f2756a5f34c4a128cc --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/constants/__init__.py @@ -0,0 +1,358 @@ +r""" +================================== +Constants (:mod:`scipy.constants`) +================================== + +.. currentmodule:: scipy.constants + +Physical and mathematical constants and units. + + +Mathematical constants +====================== + +================ ================================================================= +``pi`` Pi +``golden`` Golden ratio +``golden_ratio`` Golden ratio +================ ================================================================= + + +Physical constants +================== +The following physical constants are available as attributes of `scipy.constants`. +All units are `SI `_. + +=========================== ================================================================ =============== +Attribute Quantity Units +=========================== ================================================================ =============== +``c`` speed of light in vacuum m s^-1 +``speed_of_light`` speed of light in vacuum m s^-1 +``mu_0`` the magnetic constant :math:`\mu_0` N A^-2 +``epsilon_0`` the electric constant (vacuum permittivity), :math:`\epsilon_0` F m^-1 +``h`` the Planck constant :math:`h` J Hz^-1 +``Planck`` the Planck constant :math:`h` J Hz^-1 +``hbar`` the reduced Planck constant, :math:`\hbar = h/(2\pi)` J s +``G`` Newtonian constant of gravitation m^3 kg^-1 s^-2 +``gravitational_constant`` Newtonian constant of gravitation m^3 kg^-1 s^-2 +``g`` standard acceleration of gravity m s^-2 +``e`` elementary charge C +``elementary_charge`` elementary charge C +``R`` molar gas constant J mol^-1 K^-1 +``gas_constant`` molar gas constant J mol^-1 K^-1 +``alpha`` fine-structure constant (unitless) +``fine_structure`` fine-structure constant (unitless) +``N_A`` Avogadro constant mol^-1 +``Avogadro`` Avogadro constant mol^-1 +``k`` Boltzmann constant J K^-1 +``Boltzmann`` Boltzmann constant J K^-1 +``sigma`` Stefan-Boltzmann constant :math:`\sigma` W m^-2 K^-4 +``Stefan_Boltzmann`` Stefan-Boltzmann constant :math:`\sigma` W m^-2 K^-4 +``Wien`` Wien wavelength displacement law constant m K +``Rydberg`` Rydberg constant m^-1 +``m_e`` electron mass kg +``electron_mass`` electron mass kg +``m_p`` proton mass kg +``proton_mass`` proton mass kg +``m_n`` neutron mass kg +``neutron_mass`` neutron mass kg +=========================== ================================================================ =============== + + +Constants database +------------------ + +In addition to the above variables, :mod:`scipy.constants` also contains the +2022 CODATA recommended values [CODATA2022]_ database containing more physical +constants. + +.. autosummary:: + :toctree: generated/ + + value -- Value in physical_constants indexed by key + unit -- Unit in physical_constants indexed by key + precision -- Relative precision in physical_constants indexed by key + find -- Return list of physical_constant keys with a given string + ConstantWarning -- Constant sought not in newest CODATA data set + +.. data:: physical_constants + + Dictionary of physical constants, of the format + ``physical_constants[name] = (value, unit, uncertainty)``. + The CODATA database uses ellipses to indicate that a value is defined + (exactly) in terms of others but cannot be represented exactly with the + allocated number of digits. In these cases, SciPy calculates the derived + value and reports it to the full precision of a Python ``float``. Although + ``physical_constants`` lists the uncertainty as ``0.0`` to indicate that + the CODATA value is exact, the value in ``physical_constants`` is still + subject to the truncation error inherent in double-precision representation. + +Available constants: + +====================================================================== ==== +%(constant_names)s +====================================================================== ==== + + +Units +===== + +SI prefixes +----------- + +============ ================================================================= +``quetta`` :math:`10^{30}` +``ronna`` :math:`10^{27}` +``yotta`` :math:`10^{24}` +``zetta`` :math:`10^{21}` +``exa`` :math:`10^{18}` +``peta`` :math:`10^{15}` +``tera`` :math:`10^{12}` +``giga`` :math:`10^{9}` +``mega`` :math:`10^{6}` +``kilo`` :math:`10^{3}` +``hecto`` :math:`10^{2}` +``deka`` :math:`10^{1}` +``deci`` :math:`10^{-1}` +``centi`` :math:`10^{-2}` +``milli`` :math:`10^{-3}` +``micro`` :math:`10^{-6}` +``nano`` :math:`10^{-9}` +``pico`` :math:`10^{-12}` +``femto`` :math:`10^{-15}` +``atto`` :math:`10^{-18}` +``zepto`` :math:`10^{-21}` +``yocto`` :math:`10^{-24}` +``ronto`` :math:`10^{-27}` +``quecto`` :math:`10^{-30}` +============ ================================================================= + +Binary prefixes +--------------- + +============ ================================================================= +``kibi`` :math:`2^{10}` +``mebi`` :math:`2^{20}` +``gibi`` :math:`2^{30}` +``tebi`` :math:`2^{40}` +``pebi`` :math:`2^{50}` +``exbi`` :math:`2^{60}` +``zebi`` :math:`2^{70}` +``yobi`` :math:`2^{80}` +============ ================================================================= + +Mass +---- + +================= ============================================================ +``gram`` :math:`10^{-3}` kg +``metric_ton`` :math:`10^{3}` kg +``grain`` one grain in kg +``lb`` one pound (avoirdupous) in kg +``pound`` one pound (avoirdupous) in kg +``blob`` one inch version of a slug in kg (added in 1.0.0) +``slinch`` one inch version of a slug in kg (added in 1.0.0) +``slug`` one slug in kg (added in 1.0.0) +``oz`` one ounce in kg +``ounce`` one ounce in kg +``stone`` one stone in kg +``grain`` one grain in kg +``long_ton`` one long ton in kg +``short_ton`` one short ton in kg +``troy_ounce`` one Troy ounce in kg +``troy_pound`` one Troy pound in kg +``carat`` one carat in kg +``m_u`` atomic mass constant (in kg) +``u`` atomic mass constant (in kg) +``atomic_mass`` atomic mass constant (in kg) +================= ============================================================ + +Angle +----- + +================= ============================================================ +``degree`` degree in radians +``arcmin`` arc minute in radians +``arcminute`` arc minute in radians +``arcsec`` arc second in radians +``arcsecond`` arc second in radians +================= ============================================================ + + +Time +---- + +================= ============================================================ +``minute`` one minute in seconds +``hour`` one hour in seconds +``day`` one day in seconds +``week`` one week in seconds +``year`` one year (365 days) in seconds +``Julian_year`` one Julian year (365.25 days) in seconds +================= ============================================================ + + +Length +------ + +===================== ============================================================ +``inch`` one inch in meters +``foot`` one foot in meters +``yard`` one yard in meters +``mile`` one mile in meters +``mil`` one mil in meters +``pt`` one point in meters +``point`` one point in meters +``survey_foot`` one survey foot in meters +``survey_mile`` one survey mile in meters +``nautical_mile`` one nautical mile in meters +``fermi`` one Fermi in meters +``angstrom`` one Angstrom in meters +``micron`` one micron in meters +``au`` one astronomical unit in meters +``astronomical_unit`` one astronomical unit in meters +``light_year`` one light year in meters +``parsec`` one parsec in meters +===================== ============================================================ + +Pressure +-------- + +================= ============================================================ +``atm`` standard atmosphere in pascals +``atmosphere`` standard atmosphere in pascals +``bar`` one bar in pascals +``torr`` one torr (mmHg) in pascals +``mmHg`` one torr (mmHg) in pascals +``psi`` one psi in pascals +================= ============================================================ + +Area +---- + +================= ============================================================ +``hectare`` one hectare in square meters +``acre`` one acre in square meters +================= ============================================================ + + +Volume +------ + +=================== ======================================================== +``liter`` one liter in cubic meters +``litre`` one liter in cubic meters +``gallon`` one gallon (US) in cubic meters +``gallon_US`` one gallon (US) in cubic meters +``gallon_imp`` one gallon (UK) in cubic meters +``fluid_ounce`` one fluid ounce (US) in cubic meters +``fluid_ounce_US`` one fluid ounce (US) in cubic meters +``fluid_ounce_imp`` one fluid ounce (UK) in cubic meters +``bbl`` one barrel in cubic meters +``barrel`` one barrel in cubic meters +=================== ======================================================== + +Speed +----- + +================== ========================================================== +``kmh`` kilometers per hour in meters per second +``mph`` miles per hour in meters per second +``mach`` one Mach (approx., at 15 C, 1 atm) in meters per second +``speed_of_sound`` one Mach (approx., at 15 C, 1 atm) in meters per second +``knot`` one knot in meters per second +================== ========================================================== + + +Temperature +----------- + +===================== ======================================================= +``zero_Celsius`` zero of Celsius scale in Kelvin +``degree_Fahrenheit`` one Fahrenheit (only differences) in Kelvins +===================== ======================================================= + +.. autosummary:: + :toctree: generated/ + + convert_temperature + +Energy +------ + +==================== ======================================================= +``eV`` one electron volt in Joules +``electron_volt`` one electron volt in Joules +``calorie`` one calorie (thermochemical) in Joules +``calorie_th`` one calorie (thermochemical) in Joules +``calorie_IT`` one calorie (International Steam Table calorie, 1956) in Joules +``erg`` one erg in Joules +``Btu`` one British thermal unit (International Steam Table) in Joules +``Btu_IT`` one British thermal unit (International Steam Table) in Joules +``Btu_th`` one British thermal unit (thermochemical) in Joules +``ton_TNT`` one ton of TNT in Joules +==================== ======================================================= + +Power +----- + +==================== ======================================================= +``hp`` one horsepower in watts +``horsepower`` one horsepower in watts +==================== ======================================================= + +Force +----- + +==================== ======================================================= +``dyn`` one dyne in newtons +``dyne`` one dyne in newtons +``lbf`` one pound force in newtons +``pound_force`` one pound force in newtons +``kgf`` one kilogram force in newtons +``kilogram_force`` one kilogram force in newtons +==================== ======================================================= + +Optics +------ + +.. autosummary:: + :toctree: generated/ + + lambda2nu + nu2lambda + +References +========== + +.. [CODATA2022] CODATA Recommended Values of the Fundamental + Physical Constants 2022. + + https://physics.nist.gov/cuu/Constants/ + +""" # noqa: E501 +# Modules contributed by BasSw (wegwerp@gmail.com) +from ._codata import * +from ._constants import * +from ._codata import _obsolete_constants, physical_constants + +# Deprecated namespaces, to be removed in v2.0.0 +from . import codata, constants + +_constant_names_list = [(_k.lower(), _k, _v) + for _k, _v in physical_constants.items() + if _k not in _obsolete_constants] +_constant_names = "\n".join(["``{}``{} {} {}".format(_x[1], " "*(66-len(_x[1])), + _x[2][0], _x[2][1]) + for _x in sorted(_constant_names_list)]) +if __doc__: + __doc__ = __doc__ % dict(constant_names=_constant_names) + +del _constant_names +del _constant_names_list + +__all__ = [s for s in dir() if not s.startswith('_')] + +from scipy._lib._testutils import PytestTester +test = PytestTester(__name__) +del PytestTester diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/constants/_codata.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/constants/_codata.py new file mode 100644 index 0000000000000000000000000000000000000000..4457a8089d9c4b7ba159519e6ec1606fb7b74070 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/constants/_codata.py @@ -0,0 +1,2266 @@ +""" +Fundamental Physical Constants +------------------------------ + +These constants are taken from CODATA Recommended Values of the Fundamental +Physical Constants 2022. + +Object +------ +physical_constants : dict + A dictionary containing physical constants. Keys are the names of physical + constants, values are tuples (value, units, precision). + +Functions +--------- +value(key): + Returns the value of the physical constant(key). +unit(key): + Returns the units of the physical constant(key). +precision(key): + Returns the relative precision of the physical constant(key). +find(sub): + Prints or returns list of keys containing the string sub, default is all. + +Source +------ +The values of the constants provided at this site are recommended for +international use by CODATA and are the latest available. Termed the "2018 +CODATA recommended values," they are generally recognized worldwide for use in +all fields of science and technology. The values became available on 20 May +2019 and replaced the 2014 CODATA set. Also available is an introduction to the +constants for non-experts at + +https://physics.nist.gov/cuu/Constants/introduction.html + +References +---------- +Theoretical and experimental publications relevant to the fundamental constants +and closely related precision measurements published since the mid 1980s, but +also including many older papers of particular interest, some of which date +back to the 1800s. To search the bibliography, visit + +https://physics.nist.gov/cuu/Constants/ + +""" + +# Compiled by Charles Harris, dated October 3, 2002 +# updated to 2002 values by BasSw, 2006 +# Updated to 2006 values by Vincent Davis June 2010 +# Updated to 2014 values by Joseph Booker, 2015 +# Updated to 2018 values by Jakob Jakobson, 2019 +# Updated to 2022 values by Jakob Jakobson, 2024 + +import warnings +import math + +from typing import Any +from collections.abc import Callable + +__all__ = ['physical_constants', 'value', 'unit', 'precision', 'find', + 'ConstantWarning'] + +""" +Source: https://physics.nist.gov/cuu/Constants/ + +The values of the constants provided at this site are recommended for +international use by CODATA and are the latest available. Termed the "2018 +CODATA recommended values," they are generally recognized worldwide for use in +all fields of science and technology. The values became available on 20 May +2019 and replaced the 2014 CODATA set. +""" + +# +# Source: https://physics.nist.gov/cuu/Constants/ +# + +# Quantity Value Uncertainty Unit +# ---------------------------------------------------- --------------------- -------------------- ------------- +txt2002 = """\ +Wien displacement law constant 2.897 7685e-3 0.000 0051e-3 m K +atomic unit of 1st hyperpolarizablity 3.206 361 51e-53 0.000 000 28e-53 C^3 m^3 J^-2 +atomic unit of 2nd hyperpolarizablity 6.235 3808e-65 0.000 0011e-65 C^4 m^4 J^-3 +atomic unit of electric dipole moment 8.478 353 09e-30 0.000 000 73e-30 C m +atomic unit of electric polarizablity 1.648 777 274e-41 0.000 000 016e-41 C^2 m^2 J^-1 +atomic unit of electric quadrupole moment 4.486 551 24e-40 0.000 000 39e-40 C m^2 +atomic unit of magn. dipole moment 1.854 801 90e-23 0.000 000 16e-23 J T^-1 +atomic unit of magn. flux density 2.350 517 42e5 0.000 000 20e5 T +deuteron magn. moment 0.433 073 482e-26 0.000 000 038e-26 J T^-1 +deuteron magn. moment to Bohr magneton ratio 0.466 975 4567e-3 0.000 000 0050e-3 +deuteron magn. moment to nuclear magneton ratio 0.857 438 2329 0.000 000 0092 +deuteron-electron magn. moment ratio -4.664 345 548e-4 0.000 000 050e-4 +deuteron-proton magn. moment ratio 0.307 012 2084 0.000 000 0045 +deuteron-neutron magn. moment ratio -0.448 206 52 0.000 000 11 +electron gyromagn. ratio 1.760 859 74e11 0.000 000 15e11 s^-1 T^-1 +electron gyromagn. ratio over 2 pi 28 024.9532 0.0024 MHz T^-1 +electron magn. moment -928.476 412e-26 0.000 080e-26 J T^-1 +electron magn. moment to Bohr magneton ratio -1.001 159 652 1859 0.000 000 000 0038 +electron magn. moment to nuclear magneton ratio -1838.281 971 07 0.000 000 85 +electron magn. moment anomaly 1.159 652 1859e-3 0.000 000 0038e-3 +electron to shielded proton magn. moment ratio -658.227 5956 0.000 0071 +electron to shielded helion magn. moment ratio 864.058 255 0.000 010 +electron-deuteron magn. moment ratio -2143.923 493 0.000 023 +electron-muon magn. moment ratio 206.766 9894 0.000 0054 +electron-neutron magn. moment ratio 960.920 50 0.000 23 +electron-proton magn. moment ratio -658.210 6862 0.000 0066 +magn. constant 12.566 370 614...e-7 (exact) N A^-2 +magn. flux quantum 2.067 833 72e-15 0.000 000 18e-15 Wb +muon magn. moment -4.490 447 99e-26 0.000 000 40e-26 J T^-1 +muon magn. moment to Bohr magneton ratio -4.841 970 45e-3 0.000 000 13e-3 +muon magn. moment to nuclear magneton ratio -8.890 596 98 0.000 000 23 +muon-proton magn. moment ratio -3.183 345 118 0.000 000 089 +neutron gyromagn. ratio 1.832 471 83e8 0.000 000 46e8 s^-1 T^-1 +neutron gyromagn. ratio over 2 pi 29.164 6950 0.000 0073 MHz T^-1 +neutron magn. moment -0.966 236 45e-26 0.000 000 24e-26 J T^-1 +neutron magn. moment to Bohr magneton ratio -1.041 875 63e-3 0.000 000 25e-3 +neutron magn. moment to nuclear magneton ratio -1.913 042 73 0.000 000 45 +neutron to shielded proton magn. moment ratio -0.684 996 94 0.000 000 16 +neutron-electron magn. moment ratio 1.040 668 82e-3 0.000 000 25e-3 +neutron-proton magn. moment ratio -0.684 979 34 0.000 000 16 +proton gyromagn. ratio 2.675 222 05e8 0.000 000 23e8 s^-1 T^-1 +proton gyromagn. ratio over 2 pi 42.577 4813 0.000 0037 MHz T^-1 +proton magn. moment 1.410 606 71e-26 0.000 000 12e-26 J T^-1 +proton magn. moment to Bohr magneton ratio 1.521 032 206e-3 0.000 000 015e-3 +proton magn. moment to nuclear magneton ratio 2.792 847 351 0.000 000 028 +proton magn. shielding correction 25.689e-6 0.015e-6 +proton-neutron magn. moment ratio -1.459 898 05 0.000 000 34 +shielded helion gyromagn. ratio 2.037 894 70e8 0.000 000 18e8 s^-1 T^-1 +shielded helion gyromagn. ratio over 2 pi 32.434 1015 0.000 0028 MHz T^-1 +shielded helion magn. moment -1.074 553 024e-26 0.000 000 093e-26 J T^-1 +shielded helion magn. moment to Bohr magneton ratio -1.158 671 474e-3 0.000 000 014e-3 +shielded helion magn. moment to nuclear magneton ratio -2.127 497 723 0.000 000 025 +shielded helion to proton magn. moment ratio -0.761 766 562 0.000 000 012 +shielded helion to shielded proton magn. moment ratio -0.761 786 1313 0.000 000 0033 +shielded helion gyromagn. ratio 2.037 894 70e8 0.000 000 18e8 s^-1 T^-1 +shielded helion gyromagn. ratio over 2 pi 32.434 1015 0.000 0028 MHz T^-1 +shielded proton magn. moment 1.410 570 47e-26 0.000 000 12e-26 J T^-1 +shielded proton magn. moment to Bohr magneton ratio 1.520 993 132e-3 0.000 000 016e-3 +shielded proton magn. moment to nuclear magneton ratio 2.792 775 604 0.000 000 030 +{220} lattice spacing of silicon 192.015 5965e-12 0.000 0070e-12 m""" + + +def exact2002(exact): + replace = { + 'magn. constant': 4e-7 * math.pi, + } + return replace + + +txt2006 = """\ +lattice spacing of silicon 192.015 5762 e-12 0.000 0050 e-12 m +alpha particle-electron mass ratio 7294.299 5365 0.000 0031 +alpha particle mass 6.644 656 20 e-27 0.000 000 33 e-27 kg +alpha particle mass energy equivalent 5.971 919 17 e-10 0.000 000 30 e-10 J +alpha particle mass energy equivalent in MeV 3727.379 109 0.000 093 MeV +alpha particle mass in u 4.001 506 179 127 0.000 000 000 062 u +alpha particle molar mass 4.001 506 179 127 e-3 0.000 000 000 062 e-3 kg mol^-1 +alpha particle-proton mass ratio 3.972 599 689 51 0.000 000 000 41 +Angstrom star 1.000 014 98 e-10 0.000 000 90 e-10 m +atomic mass constant 1.660 538 782 e-27 0.000 000 083 e-27 kg +atomic mass constant energy equivalent 1.492 417 830 e-10 0.000 000 074 e-10 J +atomic mass constant energy equivalent in MeV 931.494 028 0.000 023 MeV +atomic mass unit-electron volt relationship 931.494 028 e6 0.000 023 e6 eV +atomic mass unit-hartree relationship 3.423 177 7149 e7 0.000 000 0049 e7 E_h +atomic mass unit-hertz relationship 2.252 342 7369 e23 0.000 000 0032 e23 Hz +atomic mass unit-inverse meter relationship 7.513 006 671 e14 0.000 000 011 e14 m^-1 +atomic mass unit-joule relationship 1.492 417 830 e-10 0.000 000 074 e-10 J +atomic mass unit-kelvin relationship 1.080 9527 e13 0.000 0019 e13 K +atomic mass unit-kilogram relationship 1.660 538 782 e-27 0.000 000 083 e-27 kg +atomic unit of 1st hyperpolarizability 3.206 361 533 e-53 0.000 000 081 e-53 C^3 m^3 J^-2 +atomic unit of 2nd hyperpolarizability 6.235 380 95 e-65 0.000 000 31 e-65 C^4 m^4 J^-3 +atomic unit of action 1.054 571 628 e-34 0.000 000 053 e-34 J s +atomic unit of charge 1.602 176 487 e-19 0.000 000 040 e-19 C +atomic unit of charge density 1.081 202 300 e12 0.000 000 027 e12 C m^-3 +atomic unit of current 6.623 617 63 e-3 0.000 000 17 e-3 A +atomic unit of electric dipole mom. 8.478 352 81 e-30 0.000 000 21 e-30 C m +atomic unit of electric field 5.142 206 32 e11 0.000 000 13 e11 V m^-1 +atomic unit of electric field gradient 9.717 361 66 e21 0.000 000 24 e21 V m^-2 +atomic unit of electric polarizability 1.648 777 2536 e-41 0.000 000 0034 e-41 C^2 m^2 J^-1 +atomic unit of electric potential 27.211 383 86 0.000 000 68 V +atomic unit of electric quadrupole mom. 4.486 551 07 e-40 0.000 000 11 e-40 C m^2 +atomic unit of energy 4.359 743 94 e-18 0.000 000 22 e-18 J +atomic unit of force 8.238 722 06 e-8 0.000 000 41 e-8 N +atomic unit of length 0.529 177 208 59 e-10 0.000 000 000 36 e-10 m +atomic unit of mag. dipole mom. 1.854 801 830 e-23 0.000 000 046 e-23 J T^-1 +atomic unit of mag. flux density 2.350 517 382 e5 0.000 000 059 e5 T +atomic unit of magnetizability 7.891 036 433 e-29 0.000 000 027 e-29 J T^-2 +atomic unit of mass 9.109 382 15 e-31 0.000 000 45 e-31 kg +atomic unit of momentum 1.992 851 565 e-24 0.000 000 099 e-24 kg m s^-1 +atomic unit of permittivity 1.112 650 056... e-10 (exact) F m^-1 +atomic unit of time 2.418 884 326 505 e-17 0.000 000 000 016 e-17 s +atomic unit of velocity 2.187 691 2541 e6 0.000 000 0015 e6 m s^-1 +Avogadro constant 6.022 141 79 e23 0.000 000 30 e23 mol^-1 +Bohr magneton 927.400 915 e-26 0.000 023 e-26 J T^-1 +Bohr magneton in eV/T 5.788 381 7555 e-5 0.000 000 0079 e-5 eV T^-1 +Bohr magneton in Hz/T 13.996 246 04 e9 0.000 000 35 e9 Hz T^-1 +Bohr magneton in inverse meters per tesla 46.686 4515 0.000 0012 m^-1 T^-1 +Bohr magneton in K/T 0.671 7131 0.000 0012 K T^-1 +Bohr radius 0.529 177 208 59 e-10 0.000 000 000 36 e-10 m +Boltzmann constant 1.380 6504 e-23 0.000 0024 e-23 J K^-1 +Boltzmann constant in eV/K 8.617 343 e-5 0.000 015 e-5 eV K^-1 +Boltzmann constant in Hz/K 2.083 6644 e10 0.000 0036 e10 Hz K^-1 +Boltzmann constant in inverse meters per kelvin 69.503 56 0.000 12 m^-1 K^-1 +characteristic impedance of vacuum 376.730 313 461... (exact) ohm +classical electron radius 2.817 940 2894 e-15 0.000 000 0058 e-15 m +Compton wavelength 2.426 310 2175 e-12 0.000 000 0033 e-12 m +Compton wavelength over 2 pi 386.159 264 59 e-15 0.000 000 53 e-15 m +conductance quantum 7.748 091 7004 e-5 0.000 000 0053 e-5 S +conventional value of Josephson constant 483 597.9 e9 (exact) Hz V^-1 +conventional value of von Klitzing constant 25 812.807 (exact) ohm +Cu x unit 1.002 076 99 e-13 0.000 000 28 e-13 m +deuteron-electron mag. mom. ratio -4.664 345 537 e-4 0.000 000 039 e-4 +deuteron-electron mass ratio 3670.482 9654 0.000 0016 +deuteron g factor 0.857 438 2308 0.000 000 0072 +deuteron mag. mom. 0.433 073 465 e-26 0.000 000 011 e-26 J T^-1 +deuteron mag. mom. to Bohr magneton ratio 0.466 975 4556 e-3 0.000 000 0039 e-3 +deuteron mag. mom. to nuclear magneton ratio 0.857 438 2308 0.000 000 0072 +deuteron mass 3.343 583 20 e-27 0.000 000 17 e-27 kg +deuteron mass energy equivalent 3.005 062 72 e-10 0.000 000 15 e-10 J +deuteron mass energy equivalent in MeV 1875.612 793 0.000 047 MeV +deuteron mass in u 2.013 553 212 724 0.000 000 000 078 u +deuteron molar mass 2.013 553 212 724 e-3 0.000 000 000 078 e-3 kg mol^-1 +deuteron-neutron mag. mom. ratio -0.448 206 52 0.000 000 11 +deuteron-proton mag. mom. ratio 0.307 012 2070 0.000 000 0024 +deuteron-proton mass ratio 1.999 007 501 08 0.000 000 000 22 +deuteron rms charge radius 2.1402 e-15 0.0028 e-15 m +electric constant 8.854 187 817... e-12 (exact) F m^-1 +electron charge to mass quotient -1.758 820 150 e11 0.000 000 044 e11 C kg^-1 +electron-deuteron mag. mom. ratio -2143.923 498 0.000 018 +electron-deuteron mass ratio 2.724 437 1093 e-4 0.000 000 0012 e-4 +electron g factor -2.002 319 304 3622 0.000 000 000 0015 +electron gyromag. ratio 1.760 859 770 e11 0.000 000 044 e11 s^-1 T^-1 +electron gyromag. ratio over 2 pi 28 024.953 64 0.000 70 MHz T^-1 +electron mag. mom. -928.476 377 e-26 0.000 023 e-26 J T^-1 +electron mag. mom. anomaly 1.159 652 181 11 e-3 0.000 000 000 74 e-3 +electron mag. mom. to Bohr magneton ratio -1.001 159 652 181 11 0.000 000 000 000 74 +electron mag. mom. to nuclear magneton ratio -1838.281 970 92 0.000 000 80 +electron mass 9.109 382 15 e-31 0.000 000 45 e-31 kg +electron mass energy equivalent 8.187 104 38 e-14 0.000 000 41 e-14 J +electron mass energy equivalent in MeV 0.510 998 910 0.000 000 013 MeV +electron mass in u 5.485 799 0943 e-4 0.000 000 0023 e-4 u +electron molar mass 5.485 799 0943 e-7 0.000 000 0023 e-7 kg mol^-1 +electron-muon mag. mom. ratio 206.766 9877 0.000 0052 +electron-muon mass ratio 4.836 331 71 e-3 0.000 000 12 e-3 +electron-neutron mag. mom. ratio 960.920 50 0.000 23 +electron-neutron mass ratio 5.438 673 4459 e-4 0.000 000 0033 e-4 +electron-proton mag. mom. ratio -658.210 6848 0.000 0054 +electron-proton mass ratio 5.446 170 2177 e-4 0.000 000 0024 e-4 +electron-tau mass ratio 2.875 64 e-4 0.000 47 e-4 +electron to alpha particle mass ratio 1.370 933 555 70 e-4 0.000 000 000 58 e-4 +electron to shielded helion mag. mom. ratio 864.058 257 0.000 010 +electron to shielded proton mag. mom. ratio -658.227 5971 0.000 0072 +electron volt 1.602 176 487 e-19 0.000 000 040 e-19 J +electron volt-atomic mass unit relationship 1.073 544 188 e-9 0.000 000 027 e-9 u +electron volt-hartree relationship 3.674 932 540 e-2 0.000 000 092 e-2 E_h +electron volt-hertz relationship 2.417 989 454 e14 0.000 000 060 e14 Hz +electron volt-inverse meter relationship 8.065 544 65 e5 0.000 000 20 e5 m^-1 +electron volt-joule relationship 1.602 176 487 e-19 0.000 000 040 e-19 J +electron volt-kelvin relationship 1.160 4505 e4 0.000 0020 e4 K +electron volt-kilogram relationship 1.782 661 758 e-36 0.000 000 044 e-36 kg +elementary charge 1.602 176 487 e-19 0.000 000 040 e-19 C +elementary charge over h 2.417 989 454 e14 0.000 000 060 e14 A J^-1 +Faraday constant 96 485.3399 0.0024 C mol^-1 +Faraday constant for conventional electric current 96 485.3401 0.0048 C_90 mol^-1 +Fermi coupling constant 1.166 37 e-5 0.000 01 e-5 GeV^-2 +fine-structure constant 7.297 352 5376 e-3 0.000 000 0050 e-3 +first radiation constant 3.741 771 18 e-16 0.000 000 19 e-16 W m^2 +first radiation constant for spectral radiance 1.191 042 759 e-16 0.000 000 059 e-16 W m^2 sr^-1 +hartree-atomic mass unit relationship 2.921 262 2986 e-8 0.000 000 0042 e-8 u +hartree-electron volt relationship 27.211 383 86 0.000 000 68 eV +Hartree energy 4.359 743 94 e-18 0.000 000 22 e-18 J +Hartree energy in eV 27.211 383 86 0.000 000 68 eV +hartree-hertz relationship 6.579 683 920 722 e15 0.000 000 000 044 e15 Hz +hartree-inverse meter relationship 2.194 746 313 705 e7 0.000 000 000 015 e7 m^-1 +hartree-joule relationship 4.359 743 94 e-18 0.000 000 22 e-18 J +hartree-kelvin relationship 3.157 7465 e5 0.000 0055 e5 K +hartree-kilogram relationship 4.850 869 34 e-35 0.000 000 24 e-35 kg +helion-electron mass ratio 5495.885 2765 0.000 0052 +helion mass 5.006 411 92 e-27 0.000 000 25 e-27 kg +helion mass energy equivalent 4.499 538 64 e-10 0.000 000 22 e-10 J +helion mass energy equivalent in MeV 2808.391 383 0.000 070 MeV +helion mass in u 3.014 932 2473 0.000 000 0026 u +helion molar mass 3.014 932 2473 e-3 0.000 000 0026 e-3 kg mol^-1 +helion-proton mass ratio 2.993 152 6713 0.000 000 0026 +hertz-atomic mass unit relationship 4.439 821 6294 e-24 0.000 000 0064 e-24 u +hertz-electron volt relationship 4.135 667 33 e-15 0.000 000 10 e-15 eV +hertz-hartree relationship 1.519 829 846 006 e-16 0.000 000 000010e-16 E_h +hertz-inverse meter relationship 3.335 640 951... e-9 (exact) m^-1 +hertz-joule relationship 6.626 068 96 e-34 0.000 000 33 e-34 J +hertz-kelvin relationship 4.799 2374 e-11 0.000 0084 e-11 K +hertz-kilogram relationship 7.372 496 00 e-51 0.000 000 37 e-51 kg +inverse fine-structure constant 137.035 999 679 0.000 000 094 +inverse meter-atomic mass unit relationship 1.331 025 0394 e-15 0.000 000 0019 e-15 u +inverse meter-electron volt relationship 1.239 841 875 e-6 0.000 000 031 e-6 eV +inverse meter-hartree relationship 4.556 335 252 760 e-8 0.000 000 000 030 e-8 E_h +inverse meter-hertz relationship 299 792 458 (exact) Hz +inverse meter-joule relationship 1.986 445 501 e-25 0.000 000 099 e-25 J +inverse meter-kelvin relationship 1.438 7752 e-2 0.000 0025 e-2 K +inverse meter-kilogram relationship 2.210 218 70 e-42 0.000 000 11 e-42 kg +inverse of conductance quantum 12 906.403 7787 0.000 0088 ohm +Josephson constant 483 597.891 e9 0.012 e9 Hz V^-1 +joule-atomic mass unit relationship 6.700 536 41 e9 0.000 000 33 e9 u +joule-electron volt relationship 6.241 509 65 e18 0.000 000 16 e18 eV +joule-hartree relationship 2.293 712 69 e17 0.000 000 11 e17 E_h +joule-hertz relationship 1.509 190 450 e33 0.000 000 075 e33 Hz +joule-inverse meter relationship 5.034 117 47 e24 0.000 000 25 e24 m^-1 +joule-kelvin relationship 7.242 963 e22 0.000 013 e22 K +joule-kilogram relationship 1.112 650 056... e-17 (exact) kg +kelvin-atomic mass unit relationship 9.251 098 e-14 0.000 016 e-14 u +kelvin-electron volt relationship 8.617 343 e-5 0.000 015 e-5 eV +kelvin-hartree relationship 3.166 8153 e-6 0.000 0055 e-6 E_h +kelvin-hertz relationship 2.083 6644 e10 0.000 0036 e10 Hz +kelvin-inverse meter relationship 69.503 56 0.000 12 m^-1 +kelvin-joule relationship 1.380 6504 e-23 0.000 0024 e-23 J +kelvin-kilogram relationship 1.536 1807 e-40 0.000 0027 e-40 kg +kilogram-atomic mass unit relationship 6.022 141 79 e26 0.000 000 30 e26 u +kilogram-electron volt relationship 5.609 589 12 e35 0.000 000 14 e35 eV +kilogram-hartree relationship 2.061 486 16 e34 0.000 000 10 e34 E_h +kilogram-hertz relationship 1.356 392 733 e50 0.000 000 068 e50 Hz +kilogram-inverse meter relationship 4.524 439 15 e41 0.000 000 23 e41 m^-1 +kilogram-joule relationship 8.987 551 787... e16 (exact) J +kilogram-kelvin relationship 6.509 651 e39 0.000 011 e39 K +lattice parameter of silicon 543.102 064 e-12 0.000 014 e-12 m +Loschmidt constant (273.15 K, 101.325 kPa) 2.686 7774 e25 0.000 0047 e25 m^-3 +mag. constant 12.566 370 614... e-7 (exact) N A^-2 +mag. flux quantum 2.067 833 667 e-15 0.000 000 052 e-15 Wb +molar gas constant 8.314 472 0.000 015 J mol^-1 K^-1 +molar mass constant 1 e-3 (exact) kg mol^-1 +molar mass of carbon-12 12 e-3 (exact) kg mol^-1 +molar Planck constant 3.990 312 6821 e-10 0.000 000 0057 e-10 J s mol^-1 +molar Planck constant times c 0.119 626 564 72 0.000 000 000 17 J m mol^-1 +molar volume of ideal gas (273.15 K, 100 kPa) 22.710 981 e-3 0.000 040 e-3 m^3 mol^-1 +molar volume of ideal gas (273.15 K, 101.325 kPa) 22.413 996 e-3 0.000 039 e-3 m^3 mol^-1 +molar volume of silicon 12.058 8349 e-6 0.000 0011 e-6 m^3 mol^-1 +Mo x unit 1.002 099 55 e-13 0.000 000 53 e-13 m +muon Compton wavelength 11.734 441 04 e-15 0.000 000 30 e-15 m +muon Compton wavelength over 2 pi 1.867 594 295 e-15 0.000 000 047 e-15 m +muon-electron mass ratio 206.768 2823 0.000 0052 +muon g factor -2.002 331 8414 0.000 000 0012 +muon mag. mom. -4.490 447 86 e-26 0.000 000 16 e-26 J T^-1 +muon mag. mom. anomaly 1.165 920 69 e-3 0.000 000 60 e-3 +muon mag. mom. to Bohr magneton ratio -4.841 970 49 e-3 0.000 000 12 e-3 +muon mag. mom. to nuclear magneton ratio -8.890 597 05 0.000 000 23 +muon mass 1.883 531 30 e-28 0.000 000 11 e-28 kg +muon mass energy equivalent 1.692 833 510 e-11 0.000 000 095 e-11 J +muon mass energy equivalent in MeV 105.658 3668 0.000 0038 MeV +muon mass in u 0.113 428 9256 0.000 000 0029 u +muon molar mass 0.113 428 9256 e-3 0.000 000 0029 e-3 kg mol^-1 +muon-neutron mass ratio 0.112 454 5167 0.000 000 0029 +muon-proton mag. mom. ratio -3.183 345 137 0.000 000 085 +muon-proton mass ratio 0.112 609 5261 0.000 000 0029 +muon-tau mass ratio 5.945 92 e-2 0.000 97 e-2 +natural unit of action 1.054 571 628 e-34 0.000 000 053 e-34 J s +natural unit of action in eV s 6.582 118 99 e-16 0.000 000 16 e-16 eV s +natural unit of energy 8.187 104 38 e-14 0.000 000 41 e-14 J +natural unit of energy in MeV 0.510 998 910 0.000 000 013 MeV +natural unit of length 386.159 264 59 e-15 0.000 000 53 e-15 m +natural unit of mass 9.109 382 15 e-31 0.000 000 45 e-31 kg +natural unit of momentum 2.730 924 06 e-22 0.000 000 14 e-22 kg m s^-1 +natural unit of momentum in MeV/c 0.510 998 910 0.000 000 013 MeV/c +natural unit of time 1.288 088 6570 e-21 0.000 000 0018 e-21 s +natural unit of velocity 299 792 458 (exact) m s^-1 +neutron Compton wavelength 1.319 590 8951 e-15 0.000 000 0020 e-15 m +neutron Compton wavelength over 2 pi 0.210 019 413 82 e-15 0.000 000 000 31 e-15 m +neutron-electron mag. mom. ratio 1.040 668 82 e-3 0.000 000 25 e-3 +neutron-electron mass ratio 1838.683 6605 0.000 0011 +neutron g factor -3.826 085 45 0.000 000 90 +neutron gyromag. ratio 1.832 471 85 e8 0.000 000 43 e8 s^-1 T^-1 +neutron gyromag. ratio over 2 pi 29.164 6954 0.000 0069 MHz T^-1 +neutron mag. mom. -0.966 236 41 e-26 0.000 000 23 e-26 J T^-1 +neutron mag. mom. to Bohr magneton ratio -1.041 875 63 e-3 0.000 000 25 e-3 +neutron mag. mom. to nuclear magneton ratio -1.913 042 73 0.000 000 45 +neutron mass 1.674 927 211 e-27 0.000 000 084 e-27 kg +neutron mass energy equivalent 1.505 349 505 e-10 0.000 000 075 e-10 J +neutron mass energy equivalent in MeV 939.565 346 0.000 023 MeV +neutron mass in u 1.008 664 915 97 0.000 000 000 43 u +neutron molar mass 1.008 664 915 97 e-3 0.000 000 000 43 e-3 kg mol^-1 +neutron-muon mass ratio 8.892 484 09 0.000 000 23 +neutron-proton mag. mom. ratio -0.684 979 34 0.000 000 16 +neutron-proton mass ratio 1.001 378 419 18 0.000 000 000 46 +neutron-tau mass ratio 0.528 740 0.000 086 +neutron to shielded proton mag. mom. ratio -0.684 996 94 0.000 000 16 +Newtonian constant of gravitation 6.674 28 e-11 0.000 67 e-11 m^3 kg^-1 s^-2 +Newtonian constant of gravitation over h-bar c 6.708 81 e-39 0.000 67 e-39 (GeV/c^2)^-2 +nuclear magneton 5.050 783 24 e-27 0.000 000 13 e-27 J T^-1 +nuclear magneton in eV/T 3.152 451 2326 e-8 0.000 000 0045 e-8 eV T^-1 +nuclear magneton in inverse meters per tesla 2.542 623 616 e-2 0.000 000 064 e-2 m^-1 T^-1 +nuclear magneton in K/T 3.658 2637 e-4 0.000 0064 e-4 K T^-1 +nuclear magneton in MHz/T 7.622 593 84 0.000 000 19 MHz T^-1 +Planck constant 6.626 068 96 e-34 0.000 000 33 e-34 J s +Planck constant in eV s 4.135 667 33 e-15 0.000 000 10 e-15 eV s +Planck constant over 2 pi 1.054 571 628 e-34 0.000 000 053 e-34 J s +Planck constant over 2 pi in eV s 6.582 118 99 e-16 0.000 000 16 e-16 eV s +Planck constant over 2 pi times c in MeV fm 197.326 9631 0.000 0049 MeV fm +Planck length 1.616 252 e-35 0.000 081 e-35 m +Planck mass 2.176 44 e-8 0.000 11 e-8 kg +Planck mass energy equivalent in GeV 1.220 892 e19 0.000 061 e19 GeV +Planck temperature 1.416 785 e32 0.000 071 e32 K +Planck time 5.391 24 e-44 0.000 27 e-44 s +proton charge to mass quotient 9.578 833 92 e7 0.000 000 24 e7 C kg^-1 +proton Compton wavelength 1.321 409 8446 e-15 0.000 000 0019 e-15 m +proton Compton wavelength over 2 pi 0.210 308 908 61 e-15 0.000 000 000 30 e-15 m +proton-electron mass ratio 1836.152 672 47 0.000 000 80 +proton g factor 5.585 694 713 0.000 000 046 +proton gyromag. ratio 2.675 222 099 e8 0.000 000 070 e8 s^-1 T^-1 +proton gyromag. ratio over 2 pi 42.577 4821 0.000 0011 MHz T^-1 +proton mag. mom. 1.410 606 662 e-26 0.000 000 037 e-26 J T^-1 +proton mag. mom. to Bohr magneton ratio 1.521 032 209 e-3 0.000 000 012 e-3 +proton mag. mom. to nuclear magneton ratio 2.792 847 356 0.000 000 023 +proton mag. shielding correction 25.694 e-6 0.014 e-6 +proton mass 1.672 621 637 e-27 0.000 000 083 e-27 kg +proton mass energy equivalent 1.503 277 359 e-10 0.000 000 075 e-10 J +proton mass energy equivalent in MeV 938.272 013 0.000 023 MeV +proton mass in u 1.007 276 466 77 0.000 000 000 10 u +proton molar mass 1.007 276 466 77 e-3 0.000 000 000 10 e-3 kg mol^-1 +proton-muon mass ratio 8.880 243 39 0.000 000 23 +proton-neutron mag. mom. ratio -1.459 898 06 0.000 000 34 +proton-neutron mass ratio 0.998 623 478 24 0.000 000 000 46 +proton rms charge radius 0.8768 e-15 0.0069 e-15 m +proton-tau mass ratio 0.528 012 0.000 086 +quantum of circulation 3.636 947 5199 e-4 0.000 000 0050 e-4 m^2 s^-1 +quantum of circulation times 2 7.273 895 040 e-4 0.000 000 010 e-4 m^2 s^-1 +Rydberg constant 10 973 731.568 527 0.000 073 m^-1 +Rydberg constant times c in Hz 3.289 841 960 361 e15 0.000 000 000 022 e15 Hz +Rydberg constant times hc in eV 13.605 691 93 0.000 000 34 eV +Rydberg constant times hc in J 2.179 871 97 e-18 0.000 000 11 e-18 J +Sackur-Tetrode constant (1 K, 100 kPa) -1.151 7047 0.000 0044 +Sackur-Tetrode constant (1 K, 101.325 kPa) -1.164 8677 0.000 0044 +second radiation constant 1.438 7752 e-2 0.000 0025 e-2 m K +shielded helion gyromag. ratio 2.037 894 730 e8 0.000 000 056 e8 s^-1 T^-1 +shielded helion gyromag. ratio over 2 pi 32.434 101 98 0.000 000 90 MHz T^-1 +shielded helion mag. mom. -1.074 552 982 e-26 0.000 000 030 e-26 J T^-1 +shielded helion mag. mom. to Bohr magneton ratio -1.158 671 471 e-3 0.000 000 014 e-3 +shielded helion mag. mom. to nuclear magneton ratio -2.127 497 718 0.000 000 025 +shielded helion to proton mag. mom. ratio -0.761 766 558 0.000 000 011 +shielded helion to shielded proton mag. mom. ratio -0.761 786 1313 0.000 000 0033 +shielded proton gyromag. ratio 2.675 153 362 e8 0.000 000 073 e8 s^-1 T^-1 +shielded proton gyromag. ratio over 2 pi 42.576 3881 0.000 0012 MHz T^-1 +shielded proton mag. mom. 1.410 570 419 e-26 0.000 000 038 e-26 J T^-1 +shielded proton mag. mom. to Bohr magneton ratio 1.520 993 128 e-3 0.000 000 017 e-3 +shielded proton mag. mom. to nuclear magneton ratio 2.792 775 598 0.000 000 030 +speed of light in vacuum 299 792 458 (exact) m s^-1 +standard acceleration of gravity 9.806 65 (exact) m s^-2 +standard atmosphere 101 325 (exact) Pa +Stefan-Boltzmann constant 5.670 400 e-8 0.000 040 e-8 W m^-2 K^-4 +tau Compton wavelength 0.697 72 e-15 0.000 11 e-15 m +tau Compton wavelength over 2 pi 0.111 046 e-15 0.000 018 e-15 m +tau-electron mass ratio 3477.48 0.57 +tau mass 3.167 77 e-27 0.000 52 e-27 kg +tau mass energy equivalent 2.847 05 e-10 0.000 46 e-10 J +tau mass energy equivalent in MeV 1776.99 0.29 MeV +tau mass in u 1.907 68 0.000 31 u +tau molar mass 1.907 68 e-3 0.000 31 e-3 kg mol^-1 +tau-muon mass ratio 16.8183 0.0027 +tau-neutron mass ratio 1.891 29 0.000 31 +tau-proton mass ratio 1.893 90 0.000 31 +Thomson cross section 0.665 245 8558 e-28 0.000 000 0027 e-28 m^2 +triton-electron mag. mom. ratio -1.620 514 423 e-3 0.000 000 021 e-3 +triton-electron mass ratio 5496.921 5269 0.000 0051 +triton g factor 5.957 924 896 0.000 000 076 +triton mag. mom. 1.504 609 361 e-26 0.000 000 042 e-26 J T^-1 +triton mag. mom. to Bohr magneton ratio 1.622 393 657 e-3 0.000 000 021 e-3 +triton mag. mom. to nuclear magneton ratio 2.978 962 448 0.000 000 038 +triton mass 5.007 355 88 e-27 0.000 000 25 e-27 kg +triton mass energy equivalent 4.500 387 03 e-10 0.000 000 22 e-10 J +triton mass energy equivalent in MeV 2808.920 906 0.000 070 MeV +triton mass in u 3.015 500 7134 0.000 000 0025 u +triton molar mass 3.015 500 7134 e-3 0.000 000 0025 e-3 kg mol^-1 +triton-neutron mag. mom. ratio -1.557 185 53 0.000 000 37 +triton-proton mag. mom. ratio 1.066 639 908 0.000 000 010 +triton-proton mass ratio 2.993 717 0309 0.000 000 0025 +unified atomic mass unit 1.660 538 782 e-27 0.000 000 083 e-27 kg +von Klitzing constant 25 812.807 557 0.000 018 ohm +weak mixing angle 0.222 55 0.000 56 +Wien frequency displacement law constant 5.878 933 e10 0.000 010 e10 Hz K^-1 +Wien wavelength displacement law constant 2.897 7685 e-3 0.000 0051 e-3 m K""" + + +def exact2006(exact): + mu0 = 4e-7 * math.pi + c = exact['speed of light in vacuum'] + epsilon0 = 1 / (mu0 * c**2) + replace = { + 'mag. constant': mu0, + 'electric constant': epsilon0, + 'atomic unit of permittivity': 4*math.pi*epsilon0, + 'characteristic impedance of vacuum': math.sqrt(mu0 / epsilon0), + 'hertz-inverse meter relationship': 1/c, + 'joule-kilogram relationship': 1/c**2, + 'kilogram-joule relationship': c**2, + } + return replace + + +txt2010 = """\ +{220} lattice spacing of silicon 192.015 5714 e-12 0.000 0032 e-12 m +alpha particle-electron mass ratio 7294.299 5361 0.000 0029 +alpha particle mass 6.644 656 75 e-27 0.000 000 29 e-27 kg +alpha particle mass energy equivalent 5.971 919 67 e-10 0.000 000 26 e-10 J +alpha particle mass energy equivalent in MeV 3727.379 240 0.000 082 MeV +alpha particle mass in u 4.001 506 179 125 0.000 000 000 062 u +alpha particle molar mass 4.001 506 179 125 e-3 0.000 000 000 062 e-3 kg mol^-1 +alpha particle-proton mass ratio 3.972 599 689 33 0.000 000 000 36 +Angstrom star 1.000 014 95 e-10 0.000 000 90 e-10 m +atomic mass constant 1.660 538 921 e-27 0.000 000 073 e-27 kg +atomic mass constant energy equivalent 1.492 417 954 e-10 0.000 000 066 e-10 J +atomic mass constant energy equivalent in MeV 931.494 061 0.000 021 MeV +atomic mass unit-electron volt relationship 931.494 061 e6 0.000 021 e6 eV +atomic mass unit-hartree relationship 3.423 177 6845 e7 0.000 000 0024 e7 E_h +atomic mass unit-hertz relationship 2.252 342 7168 e23 0.000 000 0016 e23 Hz +atomic mass unit-inverse meter relationship 7.513 006 6042 e14 0.000 000 0053 e14 m^-1 +atomic mass unit-joule relationship 1.492 417 954 e-10 0.000 000 066 e-10 J +atomic mass unit-kelvin relationship 1.080 954 08 e13 0.000 000 98 e13 K +atomic mass unit-kilogram relationship 1.660 538 921 e-27 0.000 000 073 e-27 kg +atomic unit of 1st hyperpolarizability 3.206 361 449 e-53 0.000 000 071 e-53 C^3 m^3 J^-2 +atomic unit of 2nd hyperpolarizability 6.235 380 54 e-65 0.000 000 28 e-65 C^4 m^4 J^-3 +atomic unit of action 1.054 571 726 e-34 0.000 000 047 e-34 J s +atomic unit of charge 1.602 176 565 e-19 0.000 000 035 e-19 C +atomic unit of charge density 1.081 202 338 e12 0.000 000 024 e12 C m^-3 +atomic unit of current 6.623 617 95 e-3 0.000 000 15 e-3 A +atomic unit of electric dipole mom. 8.478 353 26 e-30 0.000 000 19 e-30 C m +atomic unit of electric field 5.142 206 52 e11 0.000 000 11 e11 V m^-1 +atomic unit of electric field gradient 9.717 362 00 e21 0.000 000 21 e21 V m^-2 +atomic unit of electric polarizability 1.648 777 2754 e-41 0.000 000 0016 e-41 C^2 m^2 J^-1 +atomic unit of electric potential 27.211 385 05 0.000 000 60 V +atomic unit of electric quadrupole mom. 4.486 551 331 e-40 0.000 000 099 e-40 C m^2 +atomic unit of energy 4.359 744 34 e-18 0.000 000 19 e-18 J +atomic unit of force 8.238 722 78 e-8 0.000 000 36 e-8 N +atomic unit of length 0.529 177 210 92 e-10 0.000 000 000 17 e-10 m +atomic unit of mag. dipole mom. 1.854 801 936 e-23 0.000 000 041 e-23 J T^-1 +atomic unit of mag. flux density 2.350 517 464 e5 0.000 000 052 e5 T +atomic unit of magnetizability 7.891 036 607 e-29 0.000 000 013 e-29 J T^-2 +atomic unit of mass 9.109 382 91 e-31 0.000 000 40 e-31 kg +atomic unit of mom.um 1.992 851 740 e-24 0.000 000 088 e-24 kg m s^-1 +atomic unit of permittivity 1.112 650 056... e-10 (exact) F m^-1 +atomic unit of time 2.418 884 326 502e-17 0.000 000 000 012e-17 s +atomic unit of velocity 2.187 691 263 79 e6 0.000 000 000 71 e6 m s^-1 +Avogadro constant 6.022 141 29 e23 0.000 000 27 e23 mol^-1 +Bohr magneton 927.400 968 e-26 0.000 020 e-26 J T^-1 +Bohr magneton in eV/T 5.788 381 8066 e-5 0.000 000 0038 e-5 eV T^-1 +Bohr magneton in Hz/T 13.996 245 55 e9 0.000 000 31 e9 Hz T^-1 +Bohr magneton in inverse meters per tesla 46.686 4498 0.000 0010 m^-1 T^-1 +Bohr magneton in K/T 0.671 713 88 0.000 000 61 K T^-1 +Bohr radius 0.529 177 210 92 e-10 0.000 000 000 17 e-10 m +Boltzmann constant 1.380 6488 e-23 0.000 0013 e-23 J K^-1 +Boltzmann constant in eV/K 8.617 3324 e-5 0.000 0078 e-5 eV K^-1 +Boltzmann constant in Hz/K 2.083 6618 e10 0.000 0019 e10 Hz K^-1 +Boltzmann constant in inverse meters per kelvin 69.503 476 0.000 063 m^-1 K^-1 +characteristic impedance of vacuum 376.730 313 461... (exact) ohm +classical electron radius 2.817 940 3267 e-15 0.000 000 0027 e-15 m +Compton wavelength 2.426 310 2389 e-12 0.000 000 0016 e-12 m +Compton wavelength over 2 pi 386.159 268 00 e-15 0.000 000 25 e-15 m +conductance quantum 7.748 091 7346 e-5 0.000 000 0025 e-5 S +conventional value of Josephson constant 483 597.9 e9 (exact) Hz V^-1 +conventional value of von Klitzing constant 25 812.807 (exact) ohm +Cu x unit 1.002 076 97 e-13 0.000 000 28 e-13 m +deuteron-electron mag. mom. ratio -4.664 345 537 e-4 0.000 000 039 e-4 +deuteron-electron mass ratio 3670.482 9652 0.000 0015 +deuteron g factor 0.857 438 2308 0.000 000 0072 +deuteron mag. mom. 0.433 073 489 e-26 0.000 000 010 e-26 J T^-1 +deuteron mag. mom. to Bohr magneton ratio 0.466 975 4556 e-3 0.000 000 0039 e-3 +deuteron mag. mom. to nuclear magneton ratio 0.857 438 2308 0.000 000 0072 +deuteron mass 3.343 583 48 e-27 0.000 000 15 e-27 kg +deuteron mass energy equivalent 3.005 062 97 e-10 0.000 000 13 e-10 J +deuteron mass energy equivalent in MeV 1875.612 859 0.000 041 MeV +deuteron mass in u 2.013 553 212 712 0.000 000 000 077 u +deuteron molar mass 2.013 553 212 712 e-3 0.000 000 000 077 e-3 kg mol^-1 +deuteron-neutron mag. mom. ratio -0.448 206 52 0.000 000 11 +deuteron-proton mag. mom. ratio 0.307 012 2070 0.000 000 0024 +deuteron-proton mass ratio 1.999 007 500 97 0.000 000 000 18 +deuteron rms charge radius 2.1424 e-15 0.0021 e-15 m +electric constant 8.854 187 817... e-12 (exact) F m^-1 +electron charge to mass quotient -1.758 820 088 e11 0.000 000 039 e11 C kg^-1 +electron-deuteron mag. mom. ratio -2143.923 498 0.000 018 +electron-deuteron mass ratio 2.724 437 1095 e-4 0.000 000 0011 e-4 +electron g factor -2.002 319 304 361 53 0.000 000 000 000 53 +electron gyromag. ratio 1.760 859 708 e11 0.000 000 039 e11 s^-1 T^-1 +electron gyromag. ratio over 2 pi 28 024.952 66 0.000 62 MHz T^-1 +electron-helion mass ratio 1.819 543 0761 e-4 0.000 000 0017 e-4 +electron mag. mom. -928.476 430 e-26 0.000 021 e-26 J T^-1 +electron mag. mom. anomaly 1.159 652 180 76 e-3 0.000 000 000 27 e-3 +electron mag. mom. to Bohr magneton ratio -1.001 159 652 180 76 0.000 000 000 000 27 +electron mag. mom. to nuclear magneton ratio -1838.281 970 90 0.000 000 75 +electron mass 9.109 382 91 e-31 0.000 000 40 e-31 kg +electron mass energy equivalent 8.187 105 06 e-14 0.000 000 36 e-14 J +electron mass energy equivalent in MeV 0.510 998 928 0.000 000 011 MeV +electron mass in u 5.485 799 0946 e-4 0.000 000 0022 e-4 u +electron molar mass 5.485 799 0946 e-7 0.000 000 0022 e-7 kg mol^-1 +electron-muon mag. mom. ratio 206.766 9896 0.000 0052 +electron-muon mass ratio 4.836 331 66 e-3 0.000 000 12 e-3 +electron-neutron mag. mom. ratio 960.920 50 0.000 23 +electron-neutron mass ratio 5.438 673 4461 e-4 0.000 000 0032 e-4 +electron-proton mag. mom. ratio -658.210 6848 0.000 0054 +electron-proton mass ratio 5.446 170 2178 e-4 0.000 000 0022 e-4 +electron-tau mass ratio 2.875 92 e-4 0.000 26 e-4 +electron to alpha particle mass ratio 1.370 933 555 78 e-4 0.000 000 000 55 e-4 +electron to shielded helion mag. mom. ratio 864.058 257 0.000 010 +electron to shielded proton mag. mom. ratio -658.227 5971 0.000 0072 +electron-triton mass ratio 1.819 200 0653 e-4 0.000 000 0017 e-4 +electron volt 1.602 176 565 e-19 0.000 000 035 e-19 J +electron volt-atomic mass unit relationship 1.073 544 150 e-9 0.000 000 024 e-9 u +electron volt-hartree relationship 3.674 932 379 e-2 0.000 000 081 e-2 E_h +electron volt-hertz relationship 2.417 989 348 e14 0.000 000 053 e14 Hz +electron volt-inverse meter relationship 8.065 544 29 e5 0.000 000 18 e5 m^-1 +electron volt-joule relationship 1.602 176 565 e-19 0.000 000 035 e-19 J +electron volt-kelvin relationship 1.160 4519 e4 0.000 0011 e4 K +electron volt-kilogram relationship 1.782 661 845 e-36 0.000 000 039 e-36 kg +elementary charge 1.602 176 565 e-19 0.000 000 035 e-19 C +elementary charge over h 2.417 989 348 e14 0.000 000 053 e14 A J^-1 +Faraday constant 96 485.3365 0.0021 C mol^-1 +Faraday constant for conventional electric current 96 485.3321 0.0043 C_90 mol^-1 +Fermi coupling constant 1.166 364 e-5 0.000 005 e-5 GeV^-2 +fine-structure constant 7.297 352 5698 e-3 0.000 000 0024 e-3 +first radiation constant 3.741 771 53 e-16 0.000 000 17 e-16 W m^2 +first radiation constant for spectral radiance 1.191 042 869 e-16 0.000 000 053 e-16 W m^2 sr^-1 +hartree-atomic mass unit relationship 2.921 262 3246 e-8 0.000 000 0021 e-8 u +hartree-electron volt relationship 27.211 385 05 0.000 000 60 eV +Hartree energy 4.359 744 34 e-18 0.000 000 19 e-18 J +Hartree energy in eV 27.211 385 05 0.000 000 60 eV +hartree-hertz relationship 6.579 683 920 729 e15 0.000 000 000 033 e15 Hz +hartree-inverse meter relationship 2.194 746 313 708 e7 0.000 000 000 011 e7 m^-1 +hartree-joule relationship 4.359 744 34 e-18 0.000 000 19 e-18 J +hartree-kelvin relationship 3.157 7504 e5 0.000 0029 e5 K +hartree-kilogram relationship 4.850 869 79 e-35 0.000 000 21 e-35 kg +helion-electron mass ratio 5495.885 2754 0.000 0050 +helion g factor -4.255 250 613 0.000 000 050 +helion mag. mom. -1.074 617 486 e-26 0.000 000 027 e-26 J T^-1 +helion mag. mom. to Bohr magneton ratio -1.158 740 958 e-3 0.000 000 014 e-3 +helion mag. mom. to nuclear magneton ratio -2.127 625 306 0.000 000 025 +helion mass 5.006 412 34 e-27 0.000 000 22 e-27 kg +helion mass energy equivalent 4.499 539 02 e-10 0.000 000 20 e-10 J +helion mass energy equivalent in MeV 2808.391 482 0.000 062 MeV +helion mass in u 3.014 932 2468 0.000 000 0025 u +helion molar mass 3.014 932 2468 e-3 0.000 000 0025 e-3 kg mol^-1 +helion-proton mass ratio 2.993 152 6707 0.000 000 0025 +hertz-atomic mass unit relationship 4.439 821 6689 e-24 0.000 000 0031 e-24 u +hertz-electron volt relationship 4.135 667 516 e-15 0.000 000 091 e-15 eV +hertz-hartree relationship 1.519 829 8460045e-16 0.000 000 0000076e-16 E_h +hertz-inverse meter relationship 3.335 640 951... e-9 (exact) m^-1 +hertz-joule relationship 6.626 069 57 e-34 0.000 000 29 e-34 J +hertz-kelvin relationship 4.799 2434 e-11 0.000 0044 e-11 K +hertz-kilogram relationship 7.372 496 68 e-51 0.000 000 33 e-51 kg +inverse fine-structure constant 137.035 999 074 0.000 000 044 +inverse meter-atomic mass unit relationship 1.331 025 051 20 e-15 0.000 000 000 94 e-15 u +inverse meter-electron volt relationship 1.239 841 930 e-6 0.000 000 027 e-6 eV +inverse meter-hartree relationship 4.556 335 252 755 e-8 0.000 000 000 023 e-8 E_h +inverse meter-hertz relationship 299 792 458 (exact) Hz +inverse meter-joule relationship 1.986 445 684 e-25 0.000 000 088 e-25 J +inverse meter-kelvin relationship 1.438 7770 e-2 0.000 0013 e-2 K +inverse meter-kilogram relationship 2.210 218 902 e-42 0.000 000 098 e-42 kg +inverse of conductance quantum 12 906.403 7217 0.000 0042 ohm +Josephson constant 483 597.870 e9 0.011 e9 Hz V^-1 +joule-atomic mass unit relationship 6.700 535 85 e9 0.000 000 30 e9 u +joule-electron volt relationship 6.241 509 34 e18 0.000 000 14 e18 eV +joule-hartree relationship 2.293 712 48 e17 0.000 000 10 e17 E_h +joule-hertz relationship 1.509 190 311 e33 0.000 000 067 e33 Hz +joule-inverse meter relationship 5.034 117 01 e24 0.000 000 22 e24 m^-1 +joule-kelvin relationship 7.242 9716 e22 0.000 0066 e22 K +joule-kilogram relationship 1.112 650 056... e-17 (exact) kg +kelvin-atomic mass unit relationship 9.251 0868 e-14 0.000 0084 e-14 u +kelvin-electron volt relationship 8.617 3324 e-5 0.000 0078 e-5 eV +kelvin-hartree relationship 3.166 8114 e-6 0.000 0029 e-6 E_h +kelvin-hertz relationship 2.083 6618 e10 0.000 0019 e10 Hz +kelvin-inverse meter relationship 69.503 476 0.000 063 m^-1 +kelvin-joule relationship 1.380 6488 e-23 0.000 0013 e-23 J +kelvin-kilogram relationship 1.536 1790 e-40 0.000 0014 e-40 kg +kilogram-atomic mass unit relationship 6.022 141 29 e26 0.000 000 27 e26 u +kilogram-electron volt relationship 5.609 588 85 e35 0.000 000 12 e35 eV +kilogram-hartree relationship 2.061 485 968 e34 0.000 000 091 e34 E_h +kilogram-hertz relationship 1.356 392 608 e50 0.000 000 060 e50 Hz +kilogram-inverse meter relationship 4.524 438 73 e41 0.000 000 20 e41 m^-1 +kilogram-joule relationship 8.987 551 787... e16 (exact) J +kilogram-kelvin relationship 6.509 6582 e39 0.000 0059 e39 K +lattice parameter of silicon 543.102 0504 e-12 0.000 0089 e-12 m +Loschmidt constant (273.15 K, 100 kPa) 2.651 6462 e25 0.000 0024 e25 m^-3 +Loschmidt constant (273.15 K, 101.325 kPa) 2.686 7805 e25 0.000 0024 e25 m^-3 +mag. constant 12.566 370 614... e-7 (exact) N A^-2 +mag. flux quantum 2.067 833 758 e-15 0.000 000 046 e-15 Wb +molar gas constant 8.314 4621 0.000 0075 J mol^-1 K^-1 +molar mass constant 1 e-3 (exact) kg mol^-1 +molar mass of carbon-12 12 e-3 (exact) kg mol^-1 +molar Planck constant 3.990 312 7176 e-10 0.000 000 0028 e-10 J s mol^-1 +molar Planck constant times c 0.119 626 565 779 0.000 000 000 084 J m mol^-1 +molar volume of ideal gas (273.15 K, 100 kPa) 22.710 953 e-3 0.000 021 e-3 m^3 mol^-1 +molar volume of ideal gas (273.15 K, 101.325 kPa) 22.413 968 e-3 0.000 020 e-3 m^3 mol^-1 +molar volume of silicon 12.058 833 01 e-6 0.000 000 80 e-6 m^3 mol^-1 +Mo x unit 1.002 099 52 e-13 0.000 000 53 e-13 m +muon Compton wavelength 11.734 441 03 e-15 0.000 000 30 e-15 m +muon Compton wavelength over 2 pi 1.867 594 294 e-15 0.000 000 047 e-15 m +muon-electron mass ratio 206.768 2843 0.000 0052 +muon g factor -2.002 331 8418 0.000 000 0013 +muon mag. mom. -4.490 448 07 e-26 0.000 000 15 e-26 J T^-1 +muon mag. mom. anomaly 1.165 920 91 e-3 0.000 000 63 e-3 +muon mag. mom. to Bohr magneton ratio -4.841 970 44 e-3 0.000 000 12 e-3 +muon mag. mom. to nuclear magneton ratio -8.890 596 97 0.000 000 22 +muon mass 1.883 531 475 e-28 0.000 000 096 e-28 kg +muon mass energy equivalent 1.692 833 667 e-11 0.000 000 086 e-11 J +muon mass energy equivalent in MeV 105.658 3715 0.000 0035 MeV +muon mass in u 0.113 428 9267 0.000 000 0029 u +muon molar mass 0.113 428 9267 e-3 0.000 000 0029 e-3 kg mol^-1 +muon-neutron mass ratio 0.112 454 5177 0.000 000 0028 +muon-proton mag. mom. ratio -3.183 345 107 0.000 000 084 +muon-proton mass ratio 0.112 609 5272 0.000 000 0028 +muon-tau mass ratio 5.946 49 e-2 0.000 54 e-2 +natural unit of action 1.054 571 726 e-34 0.000 000 047 e-34 J s +natural unit of action in eV s 6.582 119 28 e-16 0.000 000 15 e-16 eV s +natural unit of energy 8.187 105 06 e-14 0.000 000 36 e-14 J +natural unit of energy in MeV 0.510 998 928 0.000 000 011 MeV +natural unit of length 386.159 268 00 e-15 0.000 000 25 e-15 m +natural unit of mass 9.109 382 91 e-31 0.000 000 40 e-31 kg +natural unit of mom.um 2.730 924 29 e-22 0.000 000 12 e-22 kg m s^-1 +natural unit of mom.um in MeV/c 0.510 998 928 0.000 000 011 MeV/c +natural unit of time 1.288 088 668 33 e-21 0.000 000 000 83 e-21 s +natural unit of velocity 299 792 458 (exact) m s^-1 +neutron Compton wavelength 1.319 590 9068 e-15 0.000 000 0011 e-15 m +neutron Compton wavelength over 2 pi 0.210 019 415 68 e-15 0.000 000 000 17 e-15 m +neutron-electron mag. mom. ratio 1.040 668 82 e-3 0.000 000 25 e-3 +neutron-electron mass ratio 1838.683 6605 0.000 0011 +neutron g factor -3.826 085 45 0.000 000 90 +neutron gyromag. ratio 1.832 471 79 e8 0.000 000 43 e8 s^-1 T^-1 +neutron gyromag. ratio over 2 pi 29.164 6943 0.000 0069 MHz T^-1 +neutron mag. mom. -0.966 236 47 e-26 0.000 000 23 e-26 J T^-1 +neutron mag. mom. to Bohr magneton ratio -1.041 875 63 e-3 0.000 000 25 e-3 +neutron mag. mom. to nuclear magneton ratio -1.913 042 72 0.000 000 45 +neutron mass 1.674 927 351 e-27 0.000 000 074 e-27 kg +neutron mass energy equivalent 1.505 349 631 e-10 0.000 000 066 e-10 J +neutron mass energy equivalent in MeV 939.565 379 0.000 021 MeV +neutron mass in u 1.008 664 916 00 0.000 000 000 43 u +neutron molar mass 1.008 664 916 00 e-3 0.000 000 000 43 e-3 kg mol^-1 +neutron-muon mass ratio 8.892 484 00 0.000 000 22 +neutron-proton mag. mom. ratio -0.684 979 34 0.000 000 16 +neutron-proton mass difference 2.305 573 92 e-30 0.000 000 76 e-30 +neutron-proton mass difference energy equivalent 2.072 146 50 e-13 0.000 000 68 e-13 +neutron-proton mass difference energy equivalent in MeV 1.293 332 17 0.000 000 42 +neutron-proton mass difference in u 0.001 388 449 19 0.000 000 000 45 +neutron-proton mass ratio 1.001 378 419 17 0.000 000 000 45 +neutron-tau mass ratio 0.528 790 0.000 048 +neutron to shielded proton mag. mom. ratio -0.684 996 94 0.000 000 16 +Newtonian constant of gravitation 6.673 84 e-11 0.000 80 e-11 m^3 kg^-1 s^-2 +Newtonian constant of gravitation over h-bar c 6.708 37 e-39 0.000 80 e-39 (GeV/c^2)^-2 +nuclear magneton 5.050 783 53 e-27 0.000 000 11 e-27 J T^-1 +nuclear magneton in eV/T 3.152 451 2605 e-8 0.000 000 0022 e-8 eV T^-1 +nuclear magneton in inverse meters per tesla 2.542 623 527 e-2 0.000 000 056 e-2 m^-1 T^-1 +nuclear magneton in K/T 3.658 2682 e-4 0.000 0033 e-4 K T^-1 +nuclear magneton in MHz/T 7.622 593 57 0.000 000 17 MHz T^-1 +Planck constant 6.626 069 57 e-34 0.000 000 29 e-34 J s +Planck constant in eV s 4.135 667 516 e-15 0.000 000 091 e-15 eV s +Planck constant over 2 pi 1.054 571 726 e-34 0.000 000 047 e-34 J s +Planck constant over 2 pi in eV s 6.582 119 28 e-16 0.000 000 15 e-16 eV s +Planck constant over 2 pi times c in MeV fm 197.326 9718 0.000 0044 MeV fm +Planck length 1.616 199 e-35 0.000 097 e-35 m +Planck mass 2.176 51 e-8 0.000 13 e-8 kg +Planck mass energy equivalent in GeV 1.220 932 e19 0.000 073 e19 GeV +Planck temperature 1.416 833 e32 0.000 085 e32 K +Planck time 5.391 06 e-44 0.000 32 e-44 s +proton charge to mass quotient 9.578 833 58 e7 0.000 000 21 e7 C kg^-1 +proton Compton wavelength 1.321 409 856 23 e-15 0.000 000 000 94 e-15 m +proton Compton wavelength over 2 pi 0.210 308 910 47 e-15 0.000 000 000 15 e-15 m +proton-electron mass ratio 1836.152 672 45 0.000 000 75 +proton g factor 5.585 694 713 0.000 000 046 +proton gyromag. ratio 2.675 222 005 e8 0.000 000 063 e8 s^-1 T^-1 +proton gyromag. ratio over 2 pi 42.577 4806 0.000 0010 MHz T^-1 +proton mag. mom. 1.410 606 743 e-26 0.000 000 033 e-26 J T^-1 +proton mag. mom. to Bohr magneton ratio 1.521 032 210 e-3 0.000 000 012 e-3 +proton mag. mom. to nuclear magneton ratio 2.792 847 356 0.000 000 023 +proton mag. shielding correction 25.694 e-6 0.014 e-6 +proton mass 1.672 621 777 e-27 0.000 000 074 e-27 kg +proton mass energy equivalent 1.503 277 484 e-10 0.000 000 066 e-10 J +proton mass energy equivalent in MeV 938.272 046 0.000 021 MeV +proton mass in u 1.007 276 466 812 0.000 000 000 090 u +proton molar mass 1.007 276 466 812 e-3 0.000 000 000 090 e-3 kg mol^-1 +proton-muon mass ratio 8.880 243 31 0.000 000 22 +proton-neutron mag. mom. ratio -1.459 898 06 0.000 000 34 +proton-neutron mass ratio 0.998 623 478 26 0.000 000 000 45 +proton rms charge radius 0.8775 e-15 0.0051 e-15 m +proton-tau mass ratio 0.528 063 0.000 048 +quantum of circulation 3.636 947 5520 e-4 0.000 000 0024 e-4 m^2 s^-1 +quantum of circulation times 2 7.273 895 1040 e-4 0.000 000 0047 e-4 m^2 s^-1 +Rydberg constant 10 973 731.568 539 0.000 055 m^-1 +Rydberg constant times c in Hz 3.289 841 960 364 e15 0.000 000 000 017 e15 Hz +Rydberg constant times hc in eV 13.605 692 53 0.000 000 30 eV +Rydberg constant times hc in J 2.179 872 171 e-18 0.000 000 096 e-18 J +Sackur-Tetrode constant (1 K, 100 kPa) -1.151 7078 0.000 0023 +Sackur-Tetrode constant (1 K, 101.325 kPa) -1.164 8708 0.000 0023 +second radiation constant 1.438 7770 e-2 0.000 0013 e-2 m K +shielded helion gyromag. ratio 2.037 894 659 e8 0.000 000 051 e8 s^-1 T^-1 +shielded helion gyromag. ratio over 2 pi 32.434 100 84 0.000 000 81 MHz T^-1 +shielded helion mag. mom. -1.074 553 044 e-26 0.000 000 027 e-26 J T^-1 +shielded helion mag. mom. to Bohr magneton ratio -1.158 671 471 e-3 0.000 000 014 e-3 +shielded helion mag. mom. to nuclear magneton ratio -2.127 497 718 0.000 000 025 +shielded helion to proton mag. mom. ratio -0.761 766 558 0.000 000 011 +shielded helion to shielded proton mag. mom. ratio -0.761 786 1313 0.000 000 0033 +shielded proton gyromag. ratio 2.675 153 268 e8 0.000 000 066 e8 s^-1 T^-1 +shielded proton gyromag. ratio over 2 pi 42.576 3866 0.000 0010 MHz T^-1 +shielded proton mag. mom. 1.410 570 499 e-26 0.000 000 035 e-26 J T^-1 +shielded proton mag. mom. to Bohr magneton ratio 1.520 993 128 e-3 0.000 000 017 e-3 +shielded proton mag. mom. to nuclear magneton ratio 2.792 775 598 0.000 000 030 +speed of light in vacuum 299 792 458 (exact) m s^-1 +standard acceleration of gravity 9.806 65 (exact) m s^-2 +standard atmosphere 101 325 (exact) Pa +standard-state pressure 100 000 (exact) Pa +Stefan-Boltzmann constant 5.670 373 e-8 0.000 021 e-8 W m^-2 K^-4 +tau Compton wavelength 0.697 787 e-15 0.000 063 e-15 m +tau Compton wavelength over 2 pi 0.111 056 e-15 0.000 010 e-15 m +tau-electron mass ratio 3477.15 0.31 +tau mass 3.167 47 e-27 0.000 29 e-27 kg +tau mass energy equivalent 2.846 78 e-10 0.000 26 e-10 J +tau mass energy equivalent in MeV 1776.82 0.16 MeV +tau mass in u 1.907 49 0.000 17 u +tau molar mass 1.907 49 e-3 0.000 17 e-3 kg mol^-1 +tau-muon mass ratio 16.8167 0.0015 +tau-neutron mass ratio 1.891 11 0.000 17 +tau-proton mass ratio 1.893 72 0.000 17 +Thomson cross section 0.665 245 8734 e-28 0.000 000 0013 e-28 m^2 +triton-electron mass ratio 5496.921 5267 0.000 0050 +triton g factor 5.957 924 896 0.000 000 076 +triton mag. mom. 1.504 609 447 e-26 0.000 000 038 e-26 J T^-1 +triton mag. mom. to Bohr magneton ratio 1.622 393 657 e-3 0.000 000 021 e-3 +triton mag. mom. to nuclear magneton ratio 2.978 962 448 0.000 000 038 +triton mass 5.007 356 30 e-27 0.000 000 22 e-27 kg +triton mass energy equivalent 4.500 387 41 e-10 0.000 000 20 e-10 J +triton mass energy equivalent in MeV 2808.921 005 0.000 062 MeV +triton mass in u 3.015 500 7134 0.000 000 0025 u +triton molar mass 3.015 500 7134 e-3 0.000 000 0025 e-3 kg mol^-1 +triton-proton mass ratio 2.993 717 0308 0.000 000 0025 +unified atomic mass unit 1.660 538 921 e-27 0.000 000 073 e-27 kg +von Klitzing constant 25 812.807 4434 0.000 0084 ohm +weak mixing angle 0.2223 0.0021 +Wien frequency displacement law constant 5.878 9254 e10 0.000 0053 e10 Hz K^-1 +Wien wavelength displacement law constant 2.897 7721 e-3 0.000 0026 e-3 m K""" + + +exact2010 = exact2006 + + +txt2014 = """\ +{220} lattice spacing of silicon 192.015 5714 e-12 0.000 0032 e-12 m +alpha particle-electron mass ratio 7294.299 541 36 0.000 000 24 +alpha particle mass 6.644 657 230 e-27 0.000 000 082 e-27 kg +alpha particle mass energy equivalent 5.971 920 097 e-10 0.000 000 073 e-10 J +alpha particle mass energy equivalent in MeV 3727.379 378 0.000 023 MeV +alpha particle mass in u 4.001 506 179 127 0.000 000 000 063 u +alpha particle molar mass 4.001 506 179 127 e-3 0.000 000 000 063 e-3 kg mol^-1 +alpha particle-proton mass ratio 3.972 599 689 07 0.000 000 000 36 +Angstrom star 1.000 014 95 e-10 0.000 000 90 e-10 m +atomic mass constant 1.660 539 040 e-27 0.000 000 020 e-27 kg +atomic mass constant energy equivalent 1.492 418 062 e-10 0.000 000 018 e-10 J +atomic mass constant energy equivalent in MeV 931.494 0954 0.000 0057 MeV +atomic mass unit-electron volt relationship 931.494 0954 e6 0.000 0057 e6 eV +atomic mass unit-hartree relationship 3.423 177 6902 e7 0.000 000 0016 e7 E_h +atomic mass unit-hertz relationship 2.252 342 7206 e23 0.000 000 0010 e23 Hz +atomic mass unit-inverse meter relationship 7.513 006 6166 e14 0.000 000 0034 e14 m^-1 +atomic mass unit-joule relationship 1.492 418 062 e-10 0.000 000 018 e-10 J +atomic mass unit-kelvin relationship 1.080 954 38 e13 0.000 000 62 e13 K +atomic mass unit-kilogram relationship 1.660 539 040 e-27 0.000 000 020 e-27 kg +atomic unit of 1st hyperpolarizability 3.206 361 329 e-53 0.000 000 020 e-53 C^3 m^3 J^-2 +atomic unit of 2nd hyperpolarizability 6.235 380 085 e-65 0.000 000 077 e-65 C^4 m^4 J^-3 +atomic unit of action 1.054 571 800 e-34 0.000 000 013 e-34 J s +atomic unit of charge 1.602 176 6208 e-19 0.000 000 0098 e-19 C +atomic unit of charge density 1.081 202 3770 e12 0.000 000 0067 e12 C m^-3 +atomic unit of current 6.623 618 183 e-3 0.000 000 041 e-3 A +atomic unit of electric dipole mom. 8.478 353 552 e-30 0.000 000 052 e-30 C m +atomic unit of electric field 5.142 206 707 e11 0.000 000 032 e11 V m^-1 +atomic unit of electric field gradient 9.717 362 356 e21 0.000 000 060 e21 V m^-2 +atomic unit of electric polarizability 1.648 777 2731 e-41 0.000 000 0011 e-41 C^2 m^2 J^-1 +atomic unit of electric potential 27.211 386 02 0.000 000 17 V +atomic unit of electric quadrupole mom. 4.486 551 484 e-40 0.000 000 028 e-40 C m^2 +atomic unit of energy 4.359 744 650 e-18 0.000 000 054 e-18 J +atomic unit of force 8.238 723 36 e-8 0.000 000 10 e-8 N +atomic unit of length 0.529 177 210 67 e-10 0.000 000 000 12 e-10 m +atomic unit of mag. dipole mom. 1.854 801 999 e-23 0.000 000 011 e-23 J T^-1 +atomic unit of mag. flux density 2.350 517 550 e5 0.000 000 014 e5 T +atomic unit of magnetizability 7.891 036 5886 e-29 0.000 000 0090 e-29 J T^-2 +atomic unit of mass 9.109 383 56 e-31 0.000 000 11 e-31 kg +atomic unit of mom.um 1.992 851 882 e-24 0.000 000 024 e-24 kg m s^-1 +atomic unit of permittivity 1.112 650 056... e-10 (exact) F m^-1 +atomic unit of time 2.418 884 326509e-17 0.000 000 000014e-17 s +atomic unit of velocity 2.187 691 262 77 e6 0.000 000 000 50 e6 m s^-1 +Avogadro constant 6.022 140 857 e23 0.000 000 074 e23 mol^-1 +Bohr magneton 927.400 9994 e-26 0.000 0057 e-26 J T^-1 +Bohr magneton in eV/T 5.788 381 8012 e-5 0.000 000 0026 e-5 eV T^-1 +Bohr magneton in Hz/T 13.996 245 042 e9 0.000 000 086 e9 Hz T^-1 +Bohr magneton in inverse meters per tesla 46.686 448 14 0.000 000 29 m^-1 T^-1 +Bohr magneton in K/T 0.671 714 05 0.000 000 39 K T^-1 +Bohr radius 0.529 177 210 67 e-10 0.000 000 000 12 e-10 m +Boltzmann constant 1.380 648 52 e-23 0.000 000 79 e-23 J K^-1 +Boltzmann constant in eV/K 8.617 3303 e-5 0.000 0050 e-5 eV K^-1 +Boltzmann constant in Hz/K 2.083 6612 e10 0.000 0012 e10 Hz K^-1 +Boltzmann constant in inverse meters per kelvin 69.503 457 0.000 040 m^-1 K^-1 +characteristic impedance of vacuum 376.730 313 461... (exact) ohm +classical electron radius 2.817 940 3227 e-15 0.000 000 0019 e-15 m +Compton wavelength 2.426 310 2367 e-12 0.000 000 0011 e-12 m +Compton wavelength over 2 pi 386.159 267 64 e-15 0.000 000 18 e-15 m +conductance quantum 7.748 091 7310 e-5 0.000 000 0018 e-5 S +conventional value of Josephson constant 483 597.9 e9 (exact) Hz V^-1 +conventional value of von Klitzing constant 25 812.807 (exact) ohm +Cu x unit 1.002 076 97 e-13 0.000 000 28 e-13 m +deuteron-electron mag. mom. ratio -4.664 345 535 e-4 0.000 000 026 e-4 +deuteron-electron mass ratio 3670.482 967 85 0.000 000 13 +deuteron g factor 0.857 438 2311 0.000 000 0048 +deuteron mag. mom. 0.433 073 5040 e-26 0.000 000 0036 e-26 J T^-1 +deuteron mag. mom. to Bohr magneton ratio 0.466 975 4554 e-3 0.000 000 0026 e-3 +deuteron mag. mom. to nuclear magneton ratio 0.857 438 2311 0.000 000 0048 +deuteron mass 3.343 583 719 e-27 0.000 000 041 e-27 kg +deuteron mass energy equivalent 3.005 063 183 e-10 0.000 000 037 e-10 J +deuteron mass energy equivalent in MeV 1875.612 928 0.000 012 MeV +deuteron mass in u 2.013 553 212 745 0.000 000 000 040 u +deuteron molar mass 2.013 553 212 745 e-3 0.000 000 000 040 e-3 kg mol^-1 +deuteron-neutron mag. mom. ratio -0.448 206 52 0.000 000 11 +deuteron-proton mag. mom. ratio 0.307 012 2077 0.000 000 0015 +deuteron-proton mass ratio 1.999 007 500 87 0.000 000 000 19 +deuteron rms charge radius 2.1413 e-15 0.0025 e-15 m +electric constant 8.854 187 817... e-12 (exact) F m^-1 +electron charge to mass quotient -1.758 820 024 e11 0.000 000 011 e11 C kg^-1 +electron-deuteron mag. mom. ratio -2143.923 499 0.000 012 +electron-deuteron mass ratio 2.724 437 107 484 e-4 0.000 000 000 096 e-4 +electron g factor -2.002 319 304 361 82 0.000 000 000 000 52 +electron gyromag. ratio 1.760 859 644 e11 0.000 000 011 e11 s^-1 T^-1 +electron gyromag. ratio over 2 pi 28 024.951 64 0.000 17 MHz T^-1 +electron-helion mass ratio 1.819 543 074 854 e-4 0.000 000 000 088 e-4 +electron mag. mom. -928.476 4620 e-26 0.000 0057 e-26 J T^-1 +electron mag. mom. anomaly 1.159 652 180 91 e-3 0.000 000 000 26 e-3 +electron mag. mom. to Bohr magneton ratio -1.001 159 652 180 91 0.000 000 000 000 26 +electron mag. mom. to nuclear magneton ratio -1838.281 972 34 0.000 000 17 +electron mass 9.109 383 56 e-31 0.000 000 11 e-31 kg +electron mass energy equivalent 8.187 105 65 e-14 0.000 000 10 e-14 J +electron mass energy equivalent in MeV 0.510 998 9461 0.000 000 0031 MeV +electron mass in u 5.485 799 090 70 e-4 0.000 000 000 16 e-4 u +electron molar mass 5.485 799 090 70 e-7 0.000 000 000 16 e-7 kg mol^-1 +electron-muon mag. mom. ratio 206.766 9880 0.000 0046 +electron-muon mass ratio 4.836 331 70 e-3 0.000 000 11 e-3 +electron-neutron mag. mom. ratio 960.920 50 0.000 23 +electron-neutron mass ratio 5.438 673 4428 e-4 0.000 000 0027 e-4 +electron-proton mag. mom. ratio -658.210 6866 0.000 0020 +electron-proton mass ratio 5.446 170 213 52 e-4 0.000 000 000 52 e-4 +electron-tau mass ratio 2.875 92 e-4 0.000 26 e-4 +electron to alpha particle mass ratio 1.370 933 554 798 e-4 0.000 000 000 045 e-4 +electron to shielded helion mag. mom. ratio 864.058 257 0.000 010 +electron to shielded proton mag. mom. ratio -658.227 5971 0.000 0072 +electron-triton mass ratio 1.819 200 062 203 e-4 0.000 000 000 084 e-4 +electron volt 1.602 176 6208 e-19 0.000 000 0098 e-19 J +electron volt-atomic mass unit relationship 1.073 544 1105 e-9 0.000 000 0066 e-9 u +electron volt-hartree relationship 3.674 932 248 e-2 0.000 000 023 e-2 E_h +electron volt-hertz relationship 2.417 989 262 e14 0.000 000 015 e14 Hz +electron volt-inverse meter relationship 8.065 544 005 e5 0.000 000 050 e5 m^-1 +electron volt-joule relationship 1.602 176 6208 e-19 0.000 000 0098 e-19 J +electron volt-kelvin relationship 1.160 452 21 e4 0.000 000 67 e4 K +electron volt-kilogram relationship 1.782 661 907 e-36 0.000 000 011 e-36 kg +elementary charge 1.602 176 6208 e-19 0.000 000 0098 e-19 C +elementary charge over h 2.417 989 262 e14 0.000 000 015 e14 A J^-1 +Faraday constant 96 485.332 89 0.000 59 C mol^-1 +Faraday constant for conventional electric current 96 485.3251 0.0012 C_90 mol^-1 +Fermi coupling constant 1.166 3787 e-5 0.000 0006 e-5 GeV^-2 +fine-structure constant 7.297 352 5664 e-3 0.000 000 0017 e-3 +first radiation constant 3.741 771 790 e-16 0.000 000 046 e-16 W m^2 +first radiation constant for spectral radiance 1.191 042 953 e-16 0.000 000 015 e-16 W m^2 sr^-1 +hartree-atomic mass unit relationship 2.921 262 3197 e-8 0.000 000 0013 e-8 u +hartree-electron volt relationship 27.211 386 02 0.000 000 17 eV +Hartree energy 4.359 744 650 e-18 0.000 000 054 e-18 J +Hartree energy in eV 27.211 386 02 0.000 000 17 eV +hartree-hertz relationship 6.579 683 920 711 e15 0.000 000 000 039 e15 Hz +hartree-inverse meter relationship 2.194 746 313 702 e7 0.000 000 000 013 e7 m^-1 +hartree-joule relationship 4.359 744 650 e-18 0.000 000 054 e-18 J +hartree-kelvin relationship 3.157 7513 e5 0.000 0018 e5 K +hartree-kilogram relationship 4.850 870 129 e-35 0.000 000 060 e-35 kg +helion-electron mass ratio 5495.885 279 22 0.000 000 27 +helion g factor -4.255 250 616 0.000 000 050 +helion mag. mom. -1.074 617 522 e-26 0.000 000 014 e-26 J T^-1 +helion mag. mom. to Bohr magneton ratio -1.158 740 958 e-3 0.000 000 014 e-3 +helion mag. mom. to nuclear magneton ratio -2.127 625 308 0.000 000 025 +helion mass 5.006 412 700 e-27 0.000 000 062 e-27 kg +helion mass energy equivalent 4.499 539 341 e-10 0.000 000 055 e-10 J +helion mass energy equivalent in MeV 2808.391 586 0.000 017 MeV +helion mass in u 3.014 932 246 73 0.000 000 000 12 u +helion molar mass 3.014 932 246 73 e-3 0.000 000 000 12 e-3 kg mol^-1 +helion-proton mass ratio 2.993 152 670 46 0.000 000 000 29 +hertz-atomic mass unit relationship 4.439 821 6616 e-24 0.000 000 0020 e-24 u +hertz-electron volt relationship 4.135 667 662 e-15 0.000 000 025 e-15 eV +hertz-hartree relationship 1.5198298460088 e-16 0.0000000000090e-16 E_h +hertz-inverse meter relationship 3.335 640 951... e-9 (exact) m^-1 +hertz-joule relationship 6.626 070 040 e-34 0.000 000 081 e-34 J +hertz-kelvin relationship 4.799 2447 e-11 0.000 0028 e-11 K +hertz-kilogram relationship 7.372 497 201 e-51 0.000 000 091 e-51 kg +inverse fine-structure constant 137.035 999 139 0.000 000 031 +inverse meter-atomic mass unit relationship 1.331 025 049 00 e-15 0.000 000 000 61 e-15 u +inverse meter-electron volt relationship 1.239 841 9739 e-6 0.000 000 0076 e-6 eV +inverse meter-hartree relationship 4.556 335 252 767 e-8 0.000 000 000 027 e-8 E_h +inverse meter-hertz relationship 299 792 458 (exact) Hz +inverse meter-joule relationship 1.986 445 824 e-25 0.000 000 024 e-25 J +inverse meter-kelvin relationship 1.438 777 36 e-2 0.000 000 83 e-2 K +inverse meter-kilogram relationship 2.210 219 057 e-42 0.000 000 027 e-42 kg +inverse of conductance quantum 12 906.403 7278 0.000 0029 ohm +Josephson constant 483 597.8525 e9 0.0030 e9 Hz V^-1 +joule-atomic mass unit relationship 6.700 535 363 e9 0.000 000 082 e9 u +joule-electron volt relationship 6.241 509 126 e18 0.000 000 038 e18 eV +joule-hartree relationship 2.293 712 317 e17 0.000 000 028 e17 E_h +joule-hertz relationship 1.509 190 205 e33 0.000 000 019 e33 Hz +joule-inverse meter relationship 5.034 116 651 e24 0.000 000 062 e24 m^-1 +joule-kelvin relationship 7.242 9731 e22 0.000 0042 e22 K +joule-kilogram relationship 1.112 650 056... e-17 (exact) kg +kelvin-atomic mass unit relationship 9.251 0842 e-14 0.000 0053 e-14 u +kelvin-electron volt relationship 8.617 3303 e-5 0.000 0050 e-5 eV +kelvin-hartree relationship 3.166 8105 e-6 0.000 0018 e-6 E_h +kelvin-hertz relationship 2.083 6612 e10 0.000 0012 e10 Hz +kelvin-inverse meter relationship 69.503 457 0.000 040 m^-1 +kelvin-joule relationship 1.380 648 52 e-23 0.000 000 79 e-23 J +kelvin-kilogram relationship 1.536 178 65 e-40 0.000 000 88 e-40 kg +kilogram-atomic mass unit relationship 6.022 140 857 e26 0.000 000 074 e26 u +kilogram-electron volt relationship 5.609 588 650 e35 0.000 000 034 e35 eV +kilogram-hartree relationship 2.061 485 823 e34 0.000 000 025 e34 E_h +kilogram-hertz relationship 1.356 392 512 e50 0.000 000 017 e50 Hz +kilogram-inverse meter relationship 4.524 438 411 e41 0.000 000 056 e41 m^-1 +kilogram-joule relationship 8.987 551 787... e16 (exact) J +kilogram-kelvin relationship 6.509 6595 e39 0.000 0037 e39 K +lattice parameter of silicon 543.102 0504 e-12 0.000 0089 e-12 m +Loschmidt constant (273.15 K, 100 kPa) 2.651 6467 e25 0.000 0015 e25 m^-3 +Loschmidt constant (273.15 K, 101.325 kPa) 2.686 7811 e25 0.000 0015 e25 m^-3 +mag. constant 12.566 370 614... e-7 (exact) N A^-2 +mag. flux quantum 2.067 833 831 e-15 0.000 000 013 e-15 Wb +molar gas constant 8.314 4598 0.000 0048 J mol^-1 K^-1 +molar mass constant 1 e-3 (exact) kg mol^-1 +molar mass of carbon-12 12 e-3 (exact) kg mol^-1 +molar Planck constant 3.990 312 7110 e-10 0.000 000 0018 e-10 J s mol^-1 +molar Planck constant times c 0.119 626 565 582 0.000 000 000 054 J m mol^-1 +molar volume of ideal gas (273.15 K, 100 kPa) 22.710 947 e-3 0.000 013 e-3 m^3 mol^-1 +molar volume of ideal gas (273.15 K, 101.325 kPa) 22.413 962 e-3 0.000 013 e-3 m^3 mol^-1 +molar volume of silicon 12.058 832 14 e-6 0.000 000 61 e-6 m^3 mol^-1 +Mo x unit 1.002 099 52 e-13 0.000 000 53 e-13 m +muon Compton wavelength 11.734 441 11 e-15 0.000 000 26 e-15 m +muon Compton wavelength over 2 pi 1.867 594 308 e-15 0.000 000 042 e-15 m +muon-electron mass ratio 206.768 2826 0.000 0046 +muon g factor -2.002 331 8418 0.000 000 0013 +muon mag. mom. -4.490 448 26 e-26 0.000 000 10 e-26 J T^-1 +muon mag. mom. anomaly 1.165 920 89 e-3 0.000 000 63 e-3 +muon mag. mom. to Bohr magneton ratio -4.841 970 48 e-3 0.000 000 11 e-3 +muon mag. mom. to nuclear magneton ratio -8.890 597 05 0.000 000 20 +muon mass 1.883 531 594 e-28 0.000 000 048 e-28 kg +muon mass energy equivalent 1.692 833 774 e-11 0.000 000 043 e-11 J +muon mass energy equivalent in MeV 105.658 3745 0.000 0024 MeV +muon mass in u 0.113 428 9257 0.000 000 0025 u +muon molar mass 0.113 428 9257 e-3 0.000 000 0025 e-3 kg mol^-1 +muon-neutron mass ratio 0.112 454 5167 0.000 000 0025 +muon-proton mag. mom. ratio -3.183 345 142 0.000 000 071 +muon-proton mass ratio 0.112 609 5262 0.000 000 0025 +muon-tau mass ratio 5.946 49 e-2 0.000 54 e-2 +natural unit of action 1.054 571 800 e-34 0.000 000 013 e-34 J s +natural unit of action in eV s 6.582 119 514 e-16 0.000 000 040 e-16 eV s +natural unit of energy 8.187 105 65 e-14 0.000 000 10 e-14 J +natural unit of energy in MeV 0.510 998 9461 0.000 000 0031 MeV +natural unit of length 386.159 267 64 e-15 0.000 000 18 e-15 m +natural unit of mass 9.109 383 56 e-31 0.000 000 11 e-31 kg +natural unit of mom.um 2.730 924 488 e-22 0.000 000 034 e-22 kg m s^-1 +natural unit of mom.um in MeV/c 0.510 998 9461 0.000 000 0031 MeV/c +natural unit of time 1.288 088 667 12 e-21 0.000 000 000 58 e-21 s +natural unit of velocity 299 792 458 (exact) m s^-1 +neutron Compton wavelength 1.319 590 904 81 e-15 0.000 000 000 88 e-15 m +neutron Compton wavelength over 2 pi 0.210 019 415 36 e-15 0.000 000 000 14 e-15 m +neutron-electron mag. mom. ratio 1.040 668 82 e-3 0.000 000 25 e-3 +neutron-electron mass ratio 1838.683 661 58 0.000 000 90 +neutron g factor -3.826 085 45 0.000 000 90 +neutron gyromag. ratio 1.832 471 72 e8 0.000 000 43 e8 s^-1 T^-1 +neutron gyromag. ratio over 2 pi 29.164 6933 0.000 0069 MHz T^-1 +neutron mag. mom. -0.966 236 50 e-26 0.000 000 23 e-26 J T^-1 +neutron mag. mom. to Bohr magneton ratio -1.041 875 63 e-3 0.000 000 25 e-3 +neutron mag. mom. to nuclear magneton ratio -1.913 042 73 0.000 000 45 +neutron mass 1.674 927 471 e-27 0.000 000 021 e-27 kg +neutron mass energy equivalent 1.505 349 739 e-10 0.000 000 019 e-10 J +neutron mass energy equivalent in MeV 939.565 4133 0.000 0058 MeV +neutron mass in u 1.008 664 915 88 0.000 000 000 49 u +neutron molar mass 1.008 664 915 88 e-3 0.000 000 000 49 e-3 kg mol^-1 +neutron-muon mass ratio 8.892 484 08 0.000 000 20 +neutron-proton mag. mom. ratio -0.684 979 34 0.000 000 16 +neutron-proton mass difference 2.305 573 77 e-30 0.000 000 85 e-30 +neutron-proton mass difference energy equivalent 2.072 146 37 e-13 0.000 000 76 e-13 +neutron-proton mass difference energy equivalent in MeV 1.293 332 05 0.000 000 48 +neutron-proton mass difference in u 0.001 388 449 00 0.000 000 000 51 +neutron-proton mass ratio 1.001 378 418 98 0.000 000 000 51 +neutron-tau mass ratio 0.528 790 0.000 048 +neutron to shielded proton mag. mom. ratio -0.684 996 94 0.000 000 16 +Newtonian constant of gravitation 6.674 08 e-11 0.000 31 e-11 m^3 kg^-1 s^-2 +Newtonian constant of gravitation over h-bar c 6.708 61 e-39 0.000 31 e-39 (GeV/c^2)^-2 +nuclear magneton 5.050 783 699 e-27 0.000 000 031 e-27 J T^-1 +nuclear magneton in eV/T 3.152 451 2550 e-8 0.000 000 0015 e-8 eV T^-1 +nuclear magneton in inverse meters per tesla 2.542 623 432 e-2 0.000 000 016 e-2 m^-1 T^-1 +nuclear magneton in K/T 3.658 2690 e-4 0.000 0021 e-4 K T^-1 +nuclear magneton in MHz/T 7.622 593 285 0.000 000 047 MHz T^-1 +Planck constant 6.626 070 040 e-34 0.000 000 081 e-34 J s +Planck constant in eV s 4.135 667 662 e-15 0.000 000 025 e-15 eV s +Planck constant over 2 pi 1.054 571 800 e-34 0.000 000 013 e-34 J s +Planck constant over 2 pi in eV s 6.582 119 514 e-16 0.000 000 040 e-16 eV s +Planck constant over 2 pi times c in MeV fm 197.326 9788 0.000 0012 MeV fm +Planck length 1.616 229 e-35 0.000 038 e-35 m +Planck mass 2.176 470 e-8 0.000 051 e-8 kg +Planck mass energy equivalent in GeV 1.220 910 e19 0.000 029 e19 GeV +Planck temperature 1.416 808 e32 0.000 033 e32 K +Planck time 5.391 16 e-44 0.000 13 e-44 s +proton charge to mass quotient 9.578 833 226 e7 0.000 000 059 e7 C kg^-1 +proton Compton wavelength 1.321 409 853 96 e-15 0.000 000 000 61 e-15 m +proton Compton wavelength over 2 pi 0.210 308910109e-15 0.000 000 000097e-15 m +proton-electron mass ratio 1836.152 673 89 0.000 000 17 +proton g factor 5.585 694 702 0.000 000 017 +proton gyromag. ratio 2.675 221 900 e8 0.000 000 018 e8 s^-1 T^-1 +proton gyromag. ratio over 2 pi 42.577 478 92 0.000 000 29 MHz T^-1 +proton mag. mom. 1.410 606 7873 e-26 0.000 000 0097 e-26 J T^-1 +proton mag. mom. to Bohr magneton ratio 1.521 032 2053 e-3 0.000 000 0046 e-3 +proton mag. mom. to nuclear magneton ratio 2.792 847 3508 0.000 000 0085 +proton mag. shielding correction 25.691 e-6 0.011 e-6 +proton mass 1.672 621 898 e-27 0.000 000 021 e-27 kg +proton mass energy equivalent 1.503 277 593 e-10 0.000 000 018 e-10 J +proton mass energy equivalent in MeV 938.272 0813 0.000 0058 MeV +proton mass in u 1.007 276 466 879 0.000 000 000 091 u +proton molar mass 1.007 276 466 879 e-3 0.000 000 000 091 e-3 kg mol^-1 +proton-muon mass ratio 8.880 243 38 0.000 000 20 +proton-neutron mag. mom. ratio -1.459 898 05 0.000 000 34 +proton-neutron mass ratio 0.998 623 478 44 0.000 000 000 51 +proton rms charge radius 0.8751 e-15 0.0061 e-15 m +proton-tau mass ratio 0.528 063 0.000 048 +quantum of circulation 3.636 947 5486 e-4 0.000 000 0017 e-4 m^2 s^-1 +quantum of circulation times 2 7.273 895 0972 e-4 0.000 000 0033 e-4 m^2 s^-1 +Rydberg constant 10 973 731.568 508 0.000 065 m^-1 +Rydberg constant times c in Hz 3.289 841 960 355 e15 0.000 000 000 019 e15 Hz +Rydberg constant times hc in eV 13.605 693 009 0.000 000 084 eV +Rydberg constant times hc in J 2.179 872 325 e-18 0.000 000 027 e-18 J +Sackur-Tetrode constant (1 K, 100 kPa) -1.151 7084 0.000 0014 +Sackur-Tetrode constant (1 K, 101.325 kPa) -1.164 8714 0.000 0014 +second radiation constant 1.438 777 36 e-2 0.000 000 83 e-2 m K +shielded helion gyromag. ratio 2.037 894 585 e8 0.000 000 027 e8 s^-1 T^-1 +shielded helion gyromag. ratio over 2 pi 32.434 099 66 0.000 000 43 MHz T^-1 +shielded helion mag. mom. -1.074 553 080 e-26 0.000 000 014 e-26 J T^-1 +shielded helion mag. mom. to Bohr magneton ratio -1.158 671 471 e-3 0.000 000 014 e-3 +shielded helion mag. mom. to nuclear magneton ratio -2.127 497 720 0.000 000 025 +shielded helion to proton mag. mom. ratio -0.761 766 5603 0.000 000 0092 +shielded helion to shielded proton mag. mom. ratio -0.761 786 1313 0.000 000 0033 +shielded proton gyromag. ratio 2.675 153 171 e8 0.000 000 033 e8 s^-1 T^-1 +shielded proton gyromag. ratio over 2 pi 42.576 385 07 0.000 000 53 MHz T^-1 +shielded proton mag. mom. 1.410 570 547 e-26 0.000 000 018 e-26 J T^-1 +shielded proton mag. mom. to Bohr magneton ratio 1.520 993 128 e-3 0.000 000 017 e-3 +shielded proton mag. mom. to nuclear magneton ratio 2.792 775 600 0.000 000 030 +speed of light in vacuum 299 792 458 (exact) m s^-1 +standard acceleration of gravity 9.806 65 (exact) m s^-2 +standard atmosphere 101 325 (exact) Pa +standard-state pressure 100 000 (exact) Pa +Stefan-Boltzmann constant 5.670 367 e-8 0.000 013 e-8 W m^-2 K^-4 +tau Compton wavelength 0.697 787 e-15 0.000 063 e-15 m +tau Compton wavelength over 2 pi 0.111 056 e-15 0.000 010 e-15 m +tau-electron mass ratio 3477.15 0.31 +tau mass 3.167 47 e-27 0.000 29 e-27 kg +tau mass energy equivalent 2.846 78 e-10 0.000 26 e-10 J +tau mass energy equivalent in MeV 1776.82 0.16 MeV +tau mass in u 1.907 49 0.000 17 u +tau molar mass 1.907 49 e-3 0.000 17 e-3 kg mol^-1 +tau-muon mass ratio 16.8167 0.0015 +tau-neutron mass ratio 1.891 11 0.000 17 +tau-proton mass ratio 1.893 72 0.000 17 +Thomson cross section 0.665 245 871 58 e-28 0.000 000 000 91 e-28 m^2 +triton-electron mass ratio 5496.921 535 88 0.000 000 26 +triton g factor 5.957 924 920 0.000 000 028 +triton mag. mom. 1.504 609 503 e-26 0.000 000 012 e-26 J T^-1 +triton mag. mom. to Bohr magneton ratio 1.622 393 6616 e-3 0.000 000 0076 e-3 +triton mag. mom. to nuclear magneton ratio 2.978 962 460 0.000 000 014 +triton mass 5.007 356 665 e-27 0.000 000 062 e-27 kg +triton mass energy equivalent 4.500 387 735 e-10 0.000 000 055 e-10 J +triton mass energy equivalent in MeV 2808.921 112 0.000 017 MeV +triton mass in u 3.015 500 716 32 0.000 000 000 11 u +triton molar mass 3.015 500 716 32 e-3 0.000 000 000 11 e-3 kg mol^-1 +triton-proton mass ratio 2.993 717 033 48 0.000 000 000 22 +unified atomic mass unit 1.660 539 040 e-27 0.000 000 020 e-27 kg +von Klitzing constant 25 812.807 4555 0.000 0059 ohm +weak mixing angle 0.2223 0.0021 +Wien frequency displacement law constant 5.878 9238 e10 0.000 0034 e10 Hz K^-1 +Wien wavelength displacement law constant 2.897 7729 e-3 0.000 0017 e-3 m K""" + + +exact2014 = exact2010 + + +txt2018 = """\ +alpha particle-electron mass ratio 7294.299 541 42 0.000 000 24 +alpha particle mass 6.644 657 3357 e-27 0.000 000 0020 e-27 kg +alpha particle mass energy equivalent 5.971 920 1914 e-10 0.000 000 0018 e-10 J +alpha particle mass energy equivalent in MeV 3727.379 4066 0.000 0011 MeV +alpha particle mass in u 4.001 506 179 127 0.000 000 000 063 u +alpha particle molar mass 4.001 506 1777 e-3 0.000 000 0012 e-3 kg mol^-1 +alpha particle-proton mass ratio 3.972 599 690 09 0.000 000 000 22 +alpha particle relative atomic mass 4.001 506 179 127 0.000 000 000 063 +Angstrom star 1.000 014 95 e-10 0.000 000 90 e-10 m +atomic mass constant 1.660 539 066 60 e-27 0.000 000 000 50 e-27 kg +atomic mass constant energy equivalent 1.492 418 085 60 e-10 0.000 000 000 45 e-10 J +atomic mass constant energy equivalent in MeV 931.494 102 42 0.000 000 28 MeV +atomic mass unit-electron volt relationship 9.314 941 0242 e8 0.000 000 0028 e8 eV +atomic mass unit-hartree relationship 3.423 177 6874 e7 0.000 000 0010 e7 E_h +atomic mass unit-hertz relationship 2.252 342 718 71 e23 0.000 000 000 68 e23 Hz +atomic mass unit-inverse meter relationship 7.513 006 6104 e14 0.000 000 0023 e14 m^-1 +atomic mass unit-joule relationship 1.492 418 085 60 e-10 0.000 000 000 45 e-10 J +atomic mass unit-kelvin relationship 1.080 954 019 16 e13 0.000 000 000 33 e13 K +atomic mass unit-kilogram relationship 1.660 539 066 60 e-27 0.000 000 000 50 e-27 kg +atomic unit of 1st hyperpolarizability 3.206 361 3061 e-53 0.000 000 0015 e-53 C^3 m^3 J^-2 +atomic unit of 2nd hyperpolarizability 6.235 379 9905 e-65 0.000 000 0038 e-65 C^4 m^4 J^-3 +atomic unit of action 1.054 571 817... e-34 (exact) J s +atomic unit of charge 1.602 176 634 e-19 (exact) C +atomic unit of charge density 1.081 202 384 57 e12 0.000 000 000 49 e12 C m^-3 +atomic unit of current 6.623 618 237 510 e-3 0.000 000 000 013 e-3 A +atomic unit of electric dipole mom. 8.478 353 6255 e-30 0.000 000 0013 e-30 C m +atomic unit of electric field 5.142 206 747 63 e11 0.000 000 000 78 e11 V m^-1 +atomic unit of electric field gradient 9.717 362 4292 e21 0.000 000 0029 e21 V m^-2 +atomic unit of electric polarizability 1.648 777 274 36 e-41 0.000 000 000 50 e-41 C^2 m^2 J^-1 +atomic unit of electric potential 27.211 386 245 988 0.000 000 000 053 V +atomic unit of electric quadrupole mom. 4.486 551 5246 e-40 0.000 000 0014 e-40 C m^2 +atomic unit of energy 4.359 744 722 2071 e-18 0.000 000 000 0085 e-18 J +atomic unit of force 8.238 723 4983 e-8 0.000 000 0012 e-8 N +atomic unit of length 5.291 772 109 03 e-11 0.000 000 000 80 e-11 m +atomic unit of mag. dipole mom. 1.854 802 015 66 e-23 0.000 000 000 56 e-23 J T^-1 +atomic unit of mag. flux density 2.350 517 567 58 e5 0.000 000 000 71 e5 T +atomic unit of magnetizability 7.891 036 6008 e-29 0.000 000 0048 e-29 J T^-2 +atomic unit of mass 9.109 383 7015 e-31 0.000 000 0028 e-31 kg +atomic unit of momentum 1.992 851 914 10 e-24 0.000 000 000 30 e-24 kg m s^-1 +atomic unit of permittivity 1.112 650 055 45 e-10 0.000 000 000 17 e-10 F m^-1 +atomic unit of time 2.418 884 326 5857 e-17 0.000 000 000 0047 e-17 s +atomic unit of velocity 2.187 691 263 64 e6 0.000 000 000 33 e6 m s^-1 +Avogadro constant 6.022 140 76 e23 (exact) mol^-1 +Bohr magneton 9.274 010 0783 e-24 0.000 000 0028 e-24 J T^-1 +Bohr magneton in eV/T 5.788 381 8060 e-5 0.000 000 0017 e-5 eV T^-1 +Bohr magneton in Hz/T 1.399 624 493 61 e10 0.000 000 000 42 e10 Hz T^-1 +Bohr magneton in inverse meter per tesla 46.686 447 783 0.000 000 014 m^-1 T^-1 +Bohr magneton in K/T 0.671 713 815 63 0.000 000 000 20 K T^-1 +Bohr radius 5.291 772 109 03 e-11 0.000 000 000 80 e-11 m +Boltzmann constant 1.380 649 e-23 (exact) J K^-1 +Boltzmann constant in eV/K 8.617 333 262... e-5 (exact) eV K^-1 +Boltzmann constant in Hz/K 2.083 661 912... e10 (exact) Hz K^-1 +Boltzmann constant in inverse meter per kelvin 69.503 480 04... (exact) m^-1 K^-1 +characteristic impedance of vacuum 376.730 313 668 0.000 000 057 ohm +classical electron radius 2.817 940 3262 e-15 0.000 000 0013 e-15 m +Compton wavelength 2.426 310 238 67 e-12 0.000 000 000 73 e-12 m +conductance quantum 7.748 091 729... e-5 (exact) S +conventional value of ampere-90 1.000 000 088 87... (exact) A +conventional value of coulomb-90 1.000 000 088 87... (exact) C +conventional value of farad-90 0.999 999 982 20... (exact) F +conventional value of henry-90 1.000 000 017 79... (exact) H +conventional value of Josephson constant 483 597.9 e9 (exact) Hz V^-1 +conventional value of ohm-90 1.000 000 017 79... (exact) ohm +conventional value of volt-90 1.000 000 106 66... (exact) V +conventional value of von Klitzing constant 25 812.807 (exact) ohm +conventional value of watt-90 1.000 000 195 53... (exact) W +Cu x unit 1.002 076 97 e-13 0.000 000 28 e-13 m +deuteron-electron mag. mom. ratio -4.664 345 551 e-4 0.000 000 012 e-4 +deuteron-electron mass ratio 3670.482 967 88 0.000 000 13 +deuteron g factor 0.857 438 2338 0.000 000 0022 +deuteron mag. mom. 4.330 735 094 e-27 0.000 000 011 e-27 J T^-1 +deuteron mag. mom. to Bohr magneton ratio 4.669 754 570 e-4 0.000 000 012 e-4 +deuteron mag. mom. to nuclear magneton ratio 0.857 438 2338 0.000 000 0022 +deuteron mass 3.343 583 7724 e-27 0.000 000 0010 e-27 kg +deuteron mass energy equivalent 3.005 063 231 02 e-10 0.000 000 000 91 e-10 J +deuteron mass energy equivalent in MeV 1875.612 942 57 0.000 000 57 MeV +deuteron mass in u 2.013 553 212 745 0.000 000 000 040 u +deuteron molar mass 2.013 553 212 05 e-3 0.000 000 000 61 e-3 kg mol^-1 +deuteron-neutron mag. mom. ratio -0.448 206 53 0.000 000 11 +deuteron-proton mag. mom. ratio 0.307 012 209 39 0.000 000 000 79 +deuteron-proton mass ratio 1.999 007 501 39 0.000 000 000 11 +deuteron relative atomic mass 2.013 553 212 745 0.000 000 000 040 +deuteron rms charge radius 2.127 99 e-15 0.000 74 e-15 m +electron charge to mass quotient -1.758 820 010 76 e11 0.000 000 000 53 e11 C kg^-1 +electron-deuteron mag. mom. ratio -2143.923 4915 0.000 0056 +electron-deuteron mass ratio 2.724 437 107 462 e-4 0.000 000 000 096 e-4 +electron g factor -2.002 319 304 362 56 0.000 000 000 000 35 +electron gyromag. ratio 1.760 859 630 23 e11 0.000 000 000 53 e11 s^-1 T^-1 +electron gyromag. ratio in MHz/T 28 024.951 4242 0.000 0085 MHz T^-1 +electron-helion mass ratio 1.819 543 074 573 e-4 0.000 000 000 079 e-4 +electron mag. mom. -9.284 764 7043 e-24 0.000 000 0028 e-24 J T^-1 +electron mag. mom. anomaly 1.159 652 181 28 e-3 0.000 000 000 18 e-3 +electron mag. mom. to Bohr magneton ratio -1.001 159 652 181 28 0.000 000 000 000 18 +electron mag. mom. to nuclear magneton ratio -1838.281 971 88 0.000 000 11 +electron mass 9.109 383 7015 e-31 0.000 000 0028 e-31 kg +electron mass energy equivalent 8.187 105 7769 e-14 0.000 000 0025 e-14 J +electron mass energy equivalent in MeV 0.510 998 950 00 0.000 000 000 15 MeV +electron mass in u 5.485 799 090 65 e-4 0.000 000 000 16 e-4 u +electron molar mass 5.485 799 0888 e-7 0.000 000 0017 e-7 kg mol^-1 +electron-muon mag. mom. ratio 206.766 9883 0.000 0046 +electron-muon mass ratio 4.836 331 69 e-3 0.000 000 11 e-3 +electron-neutron mag. mom. ratio 960.920 50 0.000 23 +electron-neutron mass ratio 5.438 673 4424 e-4 0.000 000 0026 e-4 +electron-proton mag. mom. ratio -658.210 687 89 0.000 000 20 +electron-proton mass ratio 5.446 170 214 87 e-4 0.000 000 000 33 e-4 +electron relative atomic mass 5.485 799 090 65 e-4 0.000 000 000 16 e-4 +electron-tau mass ratio 2.875 85 e-4 0.000 19 e-4 +electron to alpha particle mass ratio 1.370 933 554 787 e-4 0.000 000 000 045 e-4 +electron to shielded helion mag. mom. ratio 864.058 257 0.000 010 +electron to shielded proton mag. mom. ratio -658.227 5971 0.000 0072 +electron-triton mass ratio 1.819 200 062 251 e-4 0.000 000 000 090 e-4 +electron volt 1.602 176 634 e-19 (exact) J +electron volt-atomic mass unit relationship 1.073 544 102 33 e-9 0.000 000 000 32 e-9 u +electron volt-hartree relationship 3.674 932 217 5655 e-2 0.000 000 000 0071 e-2 E_h +electron volt-hertz relationship 2.417 989 242... e14 (exact) Hz +electron volt-inverse meter relationship 8.065 543 937... e5 (exact) m^-1 +electron volt-joule relationship 1.602 176 634 e-19 (exact) J +electron volt-kelvin relationship 1.160 451 812... e4 (exact) K +electron volt-kilogram relationship 1.782 661 921... e-36 (exact) kg +elementary charge 1.602 176 634 e-19 (exact) C +elementary charge over h-bar 1.519 267 447... e15 (exact) A J^-1 +Faraday constant 96 485.332 12... (exact) C mol^-1 +Fermi coupling constant 1.166 3787 e-5 0.000 0006 e-5 GeV^-2 +fine-structure constant 7.297 352 5693 e-3 0.000 000 0011 e-3 +first radiation constant 3.741 771 852... e-16 (exact) W m^2 +first radiation constant for spectral radiance 1.191 042 972... e-16 (exact) W m^2 sr^-1 +hartree-atomic mass unit relationship 2.921 262 322 05 e-8 0.000 000 000 88 e-8 u +hartree-electron volt relationship 27.211 386 245 988 0.000 000 000 053 eV +Hartree energy 4.359 744 722 2071 e-18 0.000 000 000 0085 e-18 J +Hartree energy in eV 27.211 386 245 988 0.000 000 000 053 eV +hartree-hertz relationship 6.579 683 920 502 e15 0.000 000 000 013 e15 Hz +hartree-inverse meter relationship 2.194 746 313 6320 e7 0.000 000 000 0043 e7 m^-1 +hartree-joule relationship 4.359 744 722 2071 e-18 0.000 000 000 0085 e-18 J +hartree-kelvin relationship 3.157 750 248 0407 e5 0.000 000 000 0061 e5 K +hartree-kilogram relationship 4.850 870 209 5432 e-35 0.000 000 000 0094 e-35 kg +helion-electron mass ratio 5495.885 280 07 0.000 000 24 +helion g factor -4.255 250 615 0.000 000 050 +helion mag. mom. -1.074 617 532 e-26 0.000 000 013 e-26 J T^-1 +helion mag. mom. to Bohr magneton ratio -1.158 740 958 e-3 0.000 000 014 e-3 +helion mag. mom. to nuclear magneton ratio -2.127 625 307 0.000 000 025 +helion mass 5.006 412 7796 e-27 0.000 000 0015 e-27 kg +helion mass energy equivalent 4.499 539 4125 e-10 0.000 000 0014 e-10 J +helion mass energy equivalent in MeV 2808.391 607 43 0.000 000 85 MeV +helion mass in u 3.014 932 247 175 0.000 000 000 097 u +helion molar mass 3.014 932 246 13 e-3 0.000 000 000 91 e-3 kg mol^-1 +helion-proton mass ratio 2.993 152 671 67 0.000 000 000 13 +helion relative atomic mass 3.014 932 247 175 0.000 000 000 097 +helion shielding shift 5.996 743 e-5 0.000 010 e-5 +hertz-atomic mass unit relationship 4.439 821 6652 e-24 0.000 000 0013 e-24 u +hertz-electron volt relationship 4.135 667 696... e-15 (exact) eV +hertz-hartree relationship 1.519 829 846 0570 e-16 0.000 000 000 0029 e-16 E_h +hertz-inverse meter relationship 3.335 640 951... e-9 (exact) m^-1 +hertz-joule relationship 6.626 070 15 e-34 (exact) J +hertz-kelvin relationship 4.799 243 073... e-11 (exact) K +hertz-kilogram relationship 7.372 497 323... e-51 (exact) kg +hyperfine transition frequency of Cs-133 9 192 631 770 (exact) Hz +inverse fine-structure constant 137.035 999 084 0.000 000 021 +inverse meter-atomic mass unit relationship 1.331 025 050 10 e-15 0.000 000 000 40 e-15 u +inverse meter-electron volt relationship 1.239 841 984... e-6 (exact) eV +inverse meter-hartree relationship 4.556 335 252 9120 e-8 0.000 000 000 0088 e-8 E_h +inverse meter-hertz relationship 299 792 458 (exact) Hz +inverse meter-joule relationship 1.986 445 857... e-25 (exact) J +inverse meter-kelvin relationship 1.438 776 877... e-2 (exact) K +inverse meter-kilogram relationship 2.210 219 094... e-42 (exact) kg +inverse of conductance quantum 12 906.403 72... (exact) ohm +Josephson constant 483 597.848 4... e9 (exact) Hz V^-1 +joule-atomic mass unit relationship 6.700 535 2565 e9 0.000 000 0020 e9 u +joule-electron volt relationship 6.241 509 074... e18 (exact) eV +joule-hartree relationship 2.293 712 278 3963 e17 0.000 000 000 0045 e17 E_h +joule-hertz relationship 1.509 190 179... e33 (exact) Hz +joule-inverse meter relationship 5.034 116 567... e24 (exact) m^-1 +joule-kelvin relationship 7.242 970 516... e22 (exact) K +joule-kilogram relationship 1.112 650 056... e-17 (exact) kg +kelvin-atomic mass unit relationship 9.251 087 3014 e-14 0.000 000 0028 e-14 u +kelvin-electron volt relationship 8.617 333 262... e-5 (exact) eV +kelvin-hartree relationship 3.166 811 563 4556 e-6 0.000 000 000 0061 e-6 E_h +kelvin-hertz relationship 2.083 661 912... e10 (exact) Hz +kelvin-inverse meter relationship 69.503 480 04... (exact) m^-1 +kelvin-joule relationship 1.380 649 e-23 (exact) J +kelvin-kilogram relationship 1.536 179 187... e-40 (exact) kg +kilogram-atomic mass unit relationship 6.022 140 7621 e26 0.000 000 0018 e26 u +kilogram-electron volt relationship 5.609 588 603... e35 (exact) eV +kilogram-hartree relationship 2.061 485 788 7409 e34 0.000 000 000 0040 e34 E_h +kilogram-hertz relationship 1.356 392 489... e50 (exact) Hz +kilogram-inverse meter relationship 4.524 438 335... e41 (exact) m^-1 +kilogram-joule relationship 8.987 551 787... e16 (exact) J +kilogram-kelvin relationship 6.509 657 260... e39 (exact) K +lattice parameter of silicon 5.431 020 511 e-10 0.000 000 089 e-10 m +lattice spacing of ideal Si (220) 1.920 155 716 e-10 0.000 000 032 e-10 m +Loschmidt constant (273.15 K, 100 kPa) 2.651 645 804... e25 (exact) m^-3 +Loschmidt constant (273.15 K, 101.325 kPa) 2.686 780 111... e25 (exact) m^-3 +luminous efficacy 683 (exact) lm W^-1 +mag. flux quantum 2.067 833 848... e-15 (exact) Wb +molar gas constant 8.314 462 618... (exact) J mol^-1 K^-1 +molar mass constant 0.999 999 999 65 e-3 0.000 000 000 30 e-3 kg mol^-1 +molar mass of carbon-12 11.999 999 9958 e-3 0.000 000 0036 e-3 kg mol^-1 +molar Planck constant 3.990 312 712... e-10 (exact) J Hz^-1 mol^-1 +molar volume of ideal gas (273.15 K, 100 kPa) 22.710 954 64... e-3 (exact) m^3 mol^-1 +molar volume of ideal gas (273.15 K, 101.325 kPa) 22.413 969 54... e-3 (exact) m^3 mol^-1 +molar volume of silicon 1.205 883 199 e-5 0.000 000 060 e-5 m^3 mol^-1 +Mo x unit 1.002 099 52 e-13 0.000 000 53 e-13 m +muon Compton wavelength 1.173 444 110 e-14 0.000 000 026 e-14 m +muon-electron mass ratio 206.768 2830 0.000 0046 +muon g factor -2.002 331 8418 0.000 000 0013 +muon mag. mom. -4.490 448 30 e-26 0.000 000 10 e-26 J T^-1 +muon mag. mom. anomaly 1.165 920 89 e-3 0.000 000 63 e-3 +muon mag. mom. to Bohr magneton ratio -4.841 970 47 e-3 0.000 000 11 e-3 +muon mag. mom. to nuclear magneton ratio -8.890 597 03 0.000 000 20 +muon mass 1.883 531 627 e-28 0.000 000 042 e-28 kg +muon mass energy equivalent 1.692 833 804 e-11 0.000 000 038 e-11 J +muon mass energy equivalent in MeV 105.658 3755 0.000 0023 MeV +muon mass in u 0.113 428 9259 0.000 000 0025 u +muon molar mass 1.134 289 259 e-4 0.000 000 025 e-4 kg mol^-1 +muon-neutron mass ratio 0.112 454 5170 0.000 000 0025 +muon-proton mag. mom. ratio -3.183 345 142 0.000 000 071 +muon-proton mass ratio 0.112 609 5264 0.000 000 0025 +muon-tau mass ratio 5.946 35 e-2 0.000 40 e-2 +natural unit of action 1.054 571 817... e-34 (exact) J s +natural unit of action in eV s 6.582 119 569... e-16 (exact) eV s +natural unit of energy 8.187 105 7769 e-14 0.000 000 0025 e-14 J +natural unit of energy in MeV 0.510 998 950 00 0.000 000 000 15 MeV +natural unit of length 3.861 592 6796 e-13 0.000 000 0012 e-13 m +natural unit of mass 9.109 383 7015 e-31 0.000 000 0028 e-31 kg +natural unit of momentum 2.730 924 530 75 e-22 0.000 000 000 82 e-22 kg m s^-1 +natural unit of momentum in MeV/c 0.510 998 950 00 0.000 000 000 15 MeV/c +natural unit of time 1.288 088 668 19 e-21 0.000 000 000 39 e-21 s +natural unit of velocity 299 792 458 (exact) m s^-1 +neutron Compton wavelength 1.319 590 905 81 e-15 0.000 000 000 75 e-15 m +neutron-electron mag. mom. ratio 1.040 668 82 e-3 0.000 000 25 e-3 +neutron-electron mass ratio 1838.683 661 73 0.000 000 89 +neutron g factor -3.826 085 45 0.000 000 90 +neutron gyromag. ratio 1.832 471 71 e8 0.000 000 43 e8 s^-1 T^-1 +neutron gyromag. ratio in MHz/T 29.164 6931 0.000 0069 MHz T^-1 +neutron mag. mom. -9.662 3651 e-27 0.000 0023 e-27 J T^-1 +neutron mag. mom. to Bohr magneton ratio -1.041 875 63 e-3 0.000 000 25 e-3 +neutron mag. mom. to nuclear magneton ratio -1.913 042 73 0.000 000 45 +neutron mass 1.674 927 498 04 e-27 0.000 000 000 95 e-27 kg +neutron mass energy equivalent 1.505 349 762 87 e-10 0.000 000 000 86 e-10 J +neutron mass energy equivalent in MeV 939.565 420 52 0.000 000 54 MeV +neutron mass in u 1.008 664 915 95 0.000 000 000 49 u +neutron molar mass 1.008 664 915 60 e-3 0.000 000 000 57 e-3 kg mol^-1 +neutron-muon mass ratio 8.892 484 06 0.000 000 20 +neutron-proton mag. mom. ratio -0.684 979 34 0.000 000 16 +neutron-proton mass difference 2.305 574 35 e-30 0.000 000 82 e-30 kg +neutron-proton mass difference energy equivalent 2.072 146 89 e-13 0.000 000 74 e-13 J +neutron-proton mass difference energy equivalent in MeV 1.293 332 36 0.000 000 46 MeV +neutron-proton mass difference in u 1.388 449 33 e-3 0.000 000 49 e-3 u +neutron-proton mass ratio 1.001 378 419 31 0.000 000 000 49 +neutron relative atomic mass 1.008 664 915 95 0.000 000 000 49 +neutron-tau mass ratio 0.528 779 0.000 036 +neutron to shielded proton mag. mom. ratio -0.684 996 94 0.000 000 16 +Newtonian constant of gravitation 6.674 30 e-11 0.000 15 e-11 m^3 kg^-1 s^-2 +Newtonian constant of gravitation over h-bar c 6.708 83 e-39 0.000 15 e-39 (GeV/c^2)^-2 +nuclear magneton 5.050 783 7461 e-27 0.000 000 0015 e-27 J T^-1 +nuclear magneton in eV/T 3.152 451 258 44 e-8 0.000 000 000 96 e-8 eV T^-1 +nuclear magneton in inverse meter per tesla 2.542 623 413 53 e-2 0.000 000 000 78 e-2 m^-1 T^-1 +nuclear magneton in K/T 3.658 267 7756 e-4 0.000 000 0011 e-4 K T^-1 +nuclear magneton in MHz/T 7.622 593 2291 0.000 000 0023 MHz T^-1 +Planck constant 6.626 070 15 e-34 (exact) J Hz^-1 +Planck constant in eV/Hz 4.135 667 696... e-15 (exact) eV Hz^-1 +Planck length 1.616 255 e-35 0.000 018 e-35 m +Planck mass 2.176 434 e-8 0.000 024 e-8 kg +Planck mass energy equivalent in GeV 1.220 890 e19 0.000 014 e19 GeV +Planck temperature 1.416 784 e32 0.000 016 e32 K +Planck time 5.391 247 e-44 0.000 060 e-44 s +proton charge to mass quotient 9.578 833 1560 e7 0.000 000 0029 e7 C kg^-1 +proton Compton wavelength 1.321 409 855 39 e-15 0.000 000 000 40 e-15 m +proton-electron mass ratio 1836.152 673 43 0.000 000 11 +proton g factor 5.585 694 6893 0.000 000 0016 +proton gyromag. ratio 2.675 221 8744 e8 0.000 000 0011 e8 s^-1 T^-1 +proton gyromag. ratio in MHz/T 42.577 478 518 0.000 000 018 MHz T^-1 +proton mag. mom. 1.410 606 797 36 e-26 0.000 000 000 60 e-26 J T^-1 +proton mag. mom. to Bohr magneton ratio 1.521 032 202 30 e-3 0.000 000 000 46 e-3 +proton mag. mom. to nuclear magneton ratio 2.792 847 344 63 0.000 000 000 82 +proton mag. shielding correction 2.5689 e-5 0.0011 e-5 +proton mass 1.672 621 923 69 e-27 0.000 000 000 51 e-27 kg +proton mass energy equivalent 1.503 277 615 98 e-10 0.000 000 000 46 e-10 J +proton mass energy equivalent in MeV 938.272 088 16 0.000 000 29 MeV +proton mass in u 1.007 276 466 621 0.000 000 000 053 u +proton molar mass 1.007 276 466 27 e-3 0.000 000 000 31 e-3 kg mol^-1 +proton-muon mass ratio 8.880 243 37 0.000 000 20 +proton-neutron mag. mom. ratio -1.459 898 05 0.000 000 34 +proton-neutron mass ratio 0.998 623 478 12 0.000 000 000 49 +proton relative atomic mass 1.007 276 466 621 0.000 000 000 053 +proton rms charge radius 8.414 e-16 0.019 e-16 m +proton-tau mass ratio 0.528 051 0.000 036 +quantum of circulation 3.636 947 5516 e-4 0.000 000 0011 e-4 m^2 s^-1 +quantum of circulation times 2 7.273 895 1032 e-4 0.000 000 0022 e-4 m^2 s^-1 +reduced Compton wavelength 3.861 592 6796 e-13 0.000 000 0012 e-13 m +reduced muon Compton wavelength 1.867 594 306 e-15 0.000 000 042 e-15 m +reduced neutron Compton wavelength 2.100 194 1552 e-16 0.000 000 0012 e-16 m +reduced Planck constant 1.054 571 817... e-34 (exact) J s +reduced Planck constant in eV s 6.582 119 569... e-16 (exact) eV s +reduced Planck constant times c in MeV fm 197.326 980 4... (exact) MeV fm +reduced proton Compton wavelength 2.103 089 103 36 e-16 0.000 000 000 64 e-16 m +reduced tau Compton wavelength 1.110 538 e-16 0.000 075 e-16 m +Rydberg constant 10 973 731.568 160 0.000 021 m^-1 +Rydberg constant times c in Hz 3.289 841 960 2508 e15 0.000 000 000 0064 e15 Hz +Rydberg constant times hc in eV 13.605 693 122 994 0.000 000 000 026 eV +Rydberg constant times hc in J 2.179 872 361 1035 e-18 0.000 000 000 0042 e-18 J +Sackur-Tetrode constant (1 K, 100 kPa) -1.151 707 537 06 0.000 000 000 45 +Sackur-Tetrode constant (1 K, 101.325 kPa) -1.164 870 523 58 0.000 000 000 45 +second radiation constant 1.438 776 877... e-2 (exact) m K +shielded helion gyromag. ratio 2.037 894 569 e8 0.000 000 024 e8 s^-1 T^-1 +shielded helion gyromag. ratio in MHz/T 32.434 099 42 0.000 000 38 MHz T^-1 +shielded helion mag. mom. -1.074 553 090 e-26 0.000 000 013 e-26 J T^-1 +shielded helion mag. mom. to Bohr magneton ratio -1.158 671 471 e-3 0.000 000 014 e-3 +shielded helion mag. mom. to nuclear magneton ratio -2.127 497 719 0.000 000 025 +shielded helion to proton mag. mom. ratio -0.761 766 5618 0.000 000 0089 +shielded helion to shielded proton mag. mom. ratio -0.761 786 1313 0.000 000 0033 +shielded proton gyromag. ratio 2.675 153 151 e8 0.000 000 029 e8 s^-1 T^-1 +shielded proton gyromag. ratio in MHz/T 42.576 384 74 0.000 000 46 MHz T^-1 +shielded proton mag. mom. 1.410 570 560 e-26 0.000 000 015 e-26 J T^-1 +shielded proton mag. mom. to Bohr magneton ratio 1.520 993 128 e-3 0.000 000 017 e-3 +shielded proton mag. mom. to nuclear magneton ratio 2.792 775 599 0.000 000 030 +shielding difference of d and p in HD 2.0200 e-8 0.0020 e-8 +shielding difference of t and p in HT 2.4140 e-8 0.0020 e-8 +speed of light in vacuum 299 792 458 (exact) m s^-1 +standard acceleration of gravity 9.806 65 (exact) m s^-2 +standard atmosphere 101 325 (exact) Pa +standard-state pressure 100 000 (exact) Pa +Stefan-Boltzmann constant 5.670 374 419... e-8 (exact) W m^-2 K^-4 +tau Compton wavelength 6.977 71 e-16 0.000 47 e-16 m +tau-electron mass ratio 3477.23 0.23 +tau energy equivalent 1776.86 0.12 MeV +tau mass 3.167 54 e-27 0.000 21 e-27 kg +tau mass energy equivalent 2.846 84 e-10 0.000 19 e-10 J +tau mass in u 1.907 54 0.000 13 u +tau molar mass 1.907 54 e-3 0.000 13 e-3 kg mol^-1 +tau-muon mass ratio 16.8170 0.0011 +tau-neutron mass ratio 1.891 15 0.000 13 +tau-proton mass ratio 1.893 76 0.000 13 +Thomson cross section 6.652 458 7321 e-29 0.000 000 0060 e-29 m^2 +triton-electron mass ratio 5496.921 535 73 0.000 000 27 +triton g factor 5.957 924 931 0.000 000 012 +triton mag. mom. 1.504 609 5202 e-26 0.000 000 0030 e-26 J T^-1 +triton mag. mom. to Bohr magneton ratio 1.622 393 6651 e-3 0.000 000 0032 e-3 +triton mag. mom. to nuclear magneton ratio 2.978 962 4656 0.000 000 0059 +triton mass 5.007 356 7446 e-27 0.000 000 0015 e-27 kg +triton mass energy equivalent 4.500 387 8060 e-10 0.000 000 0014 e-10 J +triton mass energy equivalent in MeV 2808.921 132 98 0.000 000 85 MeV +triton mass in u 3.015 500 716 21 0.000 000 000 12 u +triton molar mass 3.015 500 715 17 e-3 0.000 000 000 92 e-3 kg mol^-1 +triton-proton mass ratio 2.993 717 034 14 0.000 000 000 15 +triton relative atomic mass 3.015 500 716 21 0.000 000 000 12 +triton to proton mag. mom. ratio 1.066 639 9191 0.000 000 0021 +unified atomic mass unit 1.660 539 066 60 e-27 0.000 000 000 50 e-27 kg +vacuum electric permittivity 8.854 187 8128 e-12 0.000 000 0013 e-12 F m^-1 +vacuum mag. permeability 1.256 637 062 12 e-6 0.000 000 000 19 e-6 N A^-2 +von Klitzing constant 25 812.807 45... (exact) ohm +weak mixing angle 0.222 90 0.000 30 +Wien frequency displacement law constant 5.878 925 757... e10 (exact) Hz K^-1 +Wien wavelength displacement law constant 2.897 771 955... e-3 (exact) m K +W to Z mass ratio 0.881 53 0.000 17 """ + + +def exact2018(exact): + # SI base constants + c = exact['speed of light in vacuum'] + h = exact['Planck constant'] + e = exact['elementary charge'] + k = exact['Boltzmann constant'] + N_A = exact['Avogadro constant'] + + # Other useful constants + R = N_A * k + hbar = h / (2*math.pi) + G_0 = 2 * e**2 / h + + # Wien law numerical constants: https://en.wikipedia.org/wiki/Wien%27s_displacement_law + # (alpha - 3)*exp(alpha) + 3 = 0 + # (x - 5)*exp(x) + 5 = 0 + alpha_W = 2.821439372122078893403 # 3 + lambertw(-3 * exp(-3)) + x_W = 4.965114231744276303699 # 5 + lambertw(-5 * exp(-5)) + + # Conventional electrical unit + # See https://en.wikipedia.org/wiki/Conventional_electrical_unit + K_J90 = exact['conventional value of Josephson constant'] + K_J = 2 * e / h + R_K90 = exact['conventional value of von Klitzing constant'] + R_K = h / e**2 + V_90 = K_J90 / K_J + ohm_90 = R_K / R_K90 + A_90 = V_90 / ohm_90 + + replace = { + 'atomic unit of action': hbar, + 'Boltzmann constant in eV/K': k / e, + 'Boltzmann constant in Hz/K': k / h, + 'Boltzmann constant in inverse meter per kelvin': k / (h * c), + 'conductance quantum': G_0, + 'conventional value of ampere-90': A_90, + 'conventional value of coulomb-90': A_90, + 'conventional value of farad-90': 1 / ohm_90, + 'conventional value of henry-90': ohm_90, + 'conventional value of ohm-90': ohm_90, + 'conventional value of volt-90': V_90, + 'conventional value of watt-90': V_90**2 / ohm_90, + 'electron volt-hertz relationship': e / h, + 'electron volt-inverse meter relationship': e / (h * c), + 'electron volt-kelvin relationship': e / k, + 'electron volt-kilogram relationship': e / c**2, + 'elementary charge over h-bar': e / hbar, + 'Faraday constant': e * N_A, + 'first radiation constant': 2 * math.pi * h * c**2, + 'first radiation constant for spectral radiance': 2 * h * c**2, + 'hertz-electron volt relationship': h / e, + 'hertz-inverse meter relationship': 1 / c, + 'hertz-kelvin relationship': h / k, + 'hertz-kilogram relationship': h / c**2, + 'inverse meter-electron volt relationship': (h * c) / e, + 'inverse meter-joule relationship': h * c, + 'inverse meter-kelvin relationship': h * c / k, + 'inverse meter-kilogram relationship': h / c, + 'inverse of conductance quantum': 1 / G_0, + 'Josephson constant': K_J, + 'joule-electron volt relationship': 1 / e, + 'joule-hertz relationship': 1 / h, + 'joule-inverse meter relationship': 1 / (h * c), + 'joule-kelvin relationship': 1 / k, + 'joule-kilogram relationship': 1 / c**2, + 'kelvin-electron volt relationship': k / e, + 'kelvin-hertz relationship': k / h, + 'kelvin-inverse meter relationship': k / (h * c), + 'kelvin-kilogram relationship': k / c**2, + 'kilogram-electron volt relationship': c**2 / e, + 'kilogram-hertz relationship': c**2 / h, + 'kilogram-inverse meter relationship': c / h, + 'kilogram-joule relationship': c**2, + 'kilogram-kelvin relationship': c**2 / k, + 'Loschmidt constant (273.15 K, 100 kPa)': 100e3 / 273.15 / k, + 'Loschmidt constant (273.15 K, 101.325 kPa)': 101.325e3 / 273.15 / k, + 'mag. flux quantum': h / (2 * e), + 'molar gas constant': R, + 'molar Planck constant': h * N_A, + 'molar volume of ideal gas (273.15 K, 100 kPa)': R * 273.15 / 100e3, + 'molar volume of ideal gas (273.15 K, 101.325 kPa)': R * 273.15 / 101.325e3, + 'natural unit of action': hbar, + 'natural unit of action in eV s': hbar / e, + 'Planck constant in eV/Hz': h / e, + 'reduced Planck constant': hbar, + 'reduced Planck constant in eV s': hbar / e, + 'reduced Planck constant times c in MeV fm': hbar * c / (e * 1e6 * 1e-15), + 'second radiation constant': h * c / k, + 'Stefan-Boltzmann constant': 2 * math.pi**5 * k**4 / (15 * h**3 * c**2), + 'von Klitzing constant': R_K, + 'Wien frequency displacement law constant': alpha_W * k / h, + 'Wien wavelength displacement law constant': h * c / (x_W * k), + } + return replace + + +txt2022 = """\ +alpha particle-electron mass ratio 7294.299 541 71 0.000 000 17 +alpha particle mass 6.644 657 3450 e-27 0.000 000 0021 e-27 kg +alpha particle mass energy equivalent 5.971 920 1997 e-10 0.000 000 0019 e-10 J +alpha particle mass energy equivalent in MeV 3727.379 4118 0.000 0012 MeV +alpha particle mass in u 4.001 506 179 129 0.000 000 000 062 u +alpha particle molar mass 4.001 506 1833 e-3 0.000 000 0012 e-3 kg mol^-1 +alpha particle-proton mass ratio 3.972 599 690 252 0.000 000 000 070 +alpha particle relative atomic mass 4.001 506 179 129 0.000 000 000 062 +alpha particle rms charge radius 1.6785 e-15 0.0021 e-15 m +Angstrom star 1.000 014 95 e-10 0.000 000 90 e-10 m +atomic mass constant 1.660 539 068 92 e-27 0.000 000 000 52 e-27 kg +atomic mass constant energy equivalent 1.492 418 087 68 e-10 0.000 000 000 46 e-10 J +atomic mass constant energy equivalent in MeV 931.494 103 72 0.000 000 29 MeV +atomic mass unit-electron volt relationship 9.314 941 0372 e8 0.000 000 0029 e8 eV +atomic mass unit-hartree relationship 3.423 177 6922 e7 0.000 000 0011 e7 E_h +atomic mass unit-hertz relationship 2.252 342 721 85 e23 0.000 000 000 70 e23 Hz +atomic mass unit-inverse meter relationship 7.513 006 6209 e14 0.000 000 0023 e14 m^-1 +atomic mass unit-joule relationship 1.492 418 087 68 e-10 0.000 000 000 46 e-10 J +atomic mass unit-kelvin relationship 1.080 954 020 67 e13 0.000 000 000 34 e13 K +atomic mass unit-kilogram relationship 1.660 539 068 92 e-27 0.000 000 000 52 e-27 kg +atomic unit of 1st hyperpolarizability 3.206 361 2996 e-53 0.000 000 0015 e-53 C^3 m^3 J^-2 +atomic unit of 2nd hyperpolarizability 6.235 379 9735 e-65 0.000 000 0039 e-65 C^4 m^4 J^-3 +atomic unit of action 1.054 571 817... e-34 (exact) J s +atomic unit of charge 1.602 176 634 e-19 (exact) C +atomic unit of charge density 1.081 202 386 77 e12 0.000 000 000 51 e12 C m^-3 +atomic unit of current 6.623 618 237 5082 e-3 0.000 000 000 0072 e-3 A +atomic unit of electric dipole mom. 8.478 353 6198 e-30 0.000 000 0013 e-30 C m +atomic unit of electric field 5.142 206 751 12 e11 0.000 000 000 80 e11 V m^-1 +atomic unit of electric field gradient 9.717 362 4424 e21 0.000 000 0030 e21 V m^-2 +atomic unit of electric polarizability 1.648 777 272 12 e-41 0.000 000 000 51 e-41 C^2 m^2 J^-1 +atomic unit of electric potential 27.211 386 245 981 0.000 000 000 030 V +atomic unit of electric quadrupole mom. 4.486 551 5185 e-40 0.000 000 0014 e-40 C m^2 +atomic unit of energy 4.359 744 722 2060 e-18 0.000 000 000 0048 e-18 J +atomic unit of force 8.238 723 5038 e-8 0.000 000 0013 e-8 N +atomic unit of length 5.291 772 105 44 e-11 0.000 000 000 82 e-11 m +atomic unit of mag. dipole mom. 1.854 802 013 15 e-23 0.000 000 000 58 e-23 J T^-1 +atomic unit of mag. flux density 2.350 517 570 77 e5 0.000 000 000 73 e5 T +atomic unit of magnetizability 7.891 036 5794 e-29 0.000 000 0049 e-29 J T^-2 +atomic unit of mass 9.109 383 7139 e-31 0.000 000 0028 e-31 kg +atomic unit of momentum 1.992 851 915 45 e-24 0.000 000 000 31 e-24 kg m s^-1 +atomic unit of permittivity 1.112 650 056 20 e-10 0.000 000 000 17 e-10 F m^-1 +atomic unit of time 2.418 884 326 5864 e-17 0.000 000 000 0026 e-17 s +atomic unit of velocity 2.187 691 262 16 e6 0.000 000 000 34 e6 m s^-1 +Avogadro constant 6.022 140 76 e23 (exact) mol^-1 +Bohr magneton 9.274 010 0657 e-24 0.000 000 0029 e-24 J T^-1 +Bohr magneton in eV/T 5.788 381 7982 e-5 0.000 000 0018 e-5 eV T^-1 +Bohr magneton in Hz/T 1.399 624 491 71 e10 0.000 000 000 44 e10 Hz T^-1 +Bohr magneton in inverse meter per tesla 46.686 447 719 0.000 000 015 m^-1 T^-1 +Bohr magneton in K/T 0.671 713 814 72 0.000 000 000 21 K T^-1 +Bohr radius 5.291 772 105 44 e-11 0.000 000 000 82 e-11 m +Boltzmann constant 1.380 649 e-23 (exact) J K^-1 +Boltzmann constant in eV/K 8.617 333 262... e-5 (exact) eV K^-1 +Boltzmann constant in Hz/K 2.083 661 912... e10 (exact) Hz K^-1 +Boltzmann constant in inverse meter per kelvin 69.503 480 04... (exact) m^-1 K^-1 +characteristic impedance of vacuum 376.730 313 412 0.000 000 059 ohm +classical electron radius 2.817 940 3205 e-15 0.000 000 0013 e-15 m +Compton wavelength 2.426 310 235 38 e-12 0.000 000 000 76 e-12 m +conductance quantum 7.748 091 729... e-5 (exact) S +conventional value of ampere-90 1.000 000 088 87... (exact) A +conventional value of coulomb-90 1.000 000 088 87... (exact) C +conventional value of farad-90 0.999 999 982 20... (exact) F +conventional value of henry-90 1.000 000 017 79... (exact) H +conventional value of Josephson constant 483 597.9 e9 (exact) Hz V^-1 +conventional value of ohm-90 1.000 000 017 79... (exact) ohm +conventional value of volt-90 1.000 000 106 66... (exact) V +conventional value of von Klitzing constant 25 812.807 (exact) ohm +conventional value of watt-90 1.000 000 195 53... (exact) W +Copper x unit 1.002 076 97 e-13 0.000 000 28 e-13 m +deuteron-electron mag. mom. ratio -4.664 345 550 e-4 0.000 000 012 e-4 +deuteron-electron mass ratio 3670.482 967 655 0.000 000 063 +deuteron g factor 0.857 438 2335 0.000 000 0022 +deuteron mag. mom. 4.330 735 087 e-27 0.000 000 011 e-27 J T^-1 +deuteron mag. mom. to Bohr magneton ratio 4.669 754 568 e-4 0.000 000 012 e-4 +deuteron mag. mom. to nuclear magneton ratio 0.857 438 2335 0.000 000 0022 +deuteron mass 3.343 583 7768 e-27 0.000 000 0010 e-27 kg +deuteron mass energy equivalent 3.005 063 234 91 e-10 0.000 000 000 94 e-10 J +deuteron mass energy equivalent in MeV 1875.612 945 00 0.000 000 58 MeV +deuteron mass in u 2.013 553 212 544 0.000 000 000 015 u +deuteron molar mass 2.013 553 214 66 e-3 0.000 000 000 63 e-3 kg mol^-1 +deuteron-neutron mag. mom. ratio -0.448 206 52 0.000 000 11 +deuteron-proton mag. mom. ratio 0.307 012 209 30 0.000 000 000 79 +deuteron-proton mass ratio 1.999 007 501 2699 0.000 000 000 0084 +deuteron relative atomic mass 2.013 553 212 544 0.000 000 000 015 +deuteron rms charge radius 2.127 78 e-15 0.000 27 e-15 m +electron charge to mass quotient -1.758 820 008 38 e11 0.000 000 000 55 e11 C kg^-1 +electron-deuteron mag. mom. ratio -2143.923 4921 0.000 0056 +electron-deuteron mass ratio 2.724 437 107 629 e-4 0.000 000 000 047 e-4 +electron g factor -2.002 319 304 360 92 0.000 000 000 000 36 +electron gyromag. ratio 1.760 859 627 84 e11 0.000 000 000 55 e11 s^-1 T^-1 +electron gyromag. ratio in MHz/T 28 024.951 3861 0.000 0087 MHz T^-1 +electron-helion mass ratio 1.819 543 074 649 e-4 0.000 000 000 053 e-4 +electron mag. mom. -9.284 764 6917 e-24 0.000 000 0029 e-24 J T^-1 +electron mag. mom. anomaly 1.159 652 180 46 e-3 0.000 000 000 18 e-3 +electron mag. mom. to Bohr magneton ratio -1.001 159 652 180 46 0.000 000 000 000 18 +electron mag. mom. to nuclear magneton ratio -1838.281 971 877 0.000 000 032 +electron mass 9.109 383 7139 e-31 0.000 000 0028 e-31 kg +electron mass energy equivalent 8.187 105 7880 e-14 0.000 000 0026 e-14 J +electron mass energy equivalent in MeV 0.510 998 950 69 0.000 000 000 16 MeV +electron mass in u 5.485 799 090 441 e-4 0.000 000 000 097 e-4 u +electron molar mass 5.485 799 0962 e-7 0.000 000 0017 e-7 kg mol^-1 +electron-muon mag. mom. ratio 206.766 9881 0.000 0046 +electron-muon mass ratio 4.836 331 70 e-3 0.000 000 11 e-3 +electron-neutron mag. mom. ratio 960.920 48 0.000 23 +electron-neutron mass ratio 5.438 673 4416 e-4 0.000 000 0022 e-4 +electron-proton mag. mom. ratio -658.210 687 89 0.000 000 19 +electron-proton mass ratio 5.446 170 214 889 e-4 0.000 000 000 094 e-4 +electron relative atomic mass 5.485 799 090 441 e-4 0.000 000 000 097 e-4 +electron-tau mass ratio 2.875 85 e-4 0.000 19 e-4 +electron to alpha particle mass ratio 1.370 933 554 733 e-4 0.000 000 000 032 e-4 +electron to shielded helion mag. mom. ratio 864.058 239 86 0.000 000 70 +electron to shielded proton mag. mom. ratio -658.227 5856 0.000 0027 +electron-triton mass ratio 1.819 200 062 327 e-4 0.000 000 000 068 e-4 +electron volt 1.602 176 634 e-19 (exact) J +electron volt-atomic mass unit relationship 1.073 544 100 83 e-9 0.000 000 000 33 e-9 u +electron volt-hartree relationship 3.674 932 217 5665 e-2 0.000 000 000 0040 e-2 E_h +electron volt-hertz relationship 2.417 989 242... e14 (exact) Hz +electron volt-inverse meter relationship 8.065 543 937... e5 (exact) m^-1 +electron volt-joule relationship 1.602 176 634 e-19 (exact) J +electron volt-kelvin relationship 1.160 451 812... e4 (exact) K +electron volt-kilogram relationship 1.782 661 921... e-36 (exact) kg +elementary charge 1.602 176 634 e-19 (exact) C +elementary charge over h-bar 1.519 267 447... e15 (exact) A J^-1 +Faraday constant 96 485.332 12... (exact) C mol^-1 +Fermi coupling constant 1.166 3787 e-5 0.000 0006 e-5 GeV^-2 +fine-structure constant 7.297 352 5643 e-3 0.000 000 0011 e-3 +first radiation constant 3.741 771 852... e-16 (exact) W m^2 +first radiation constant for spectral radiance 1.191 042 972... e-16 (exact) W m^2 sr^-1 +hartree-atomic mass unit relationship 2.921 262 317 97 e-8 0.000 000 000 91 e-8 u +hartree-electron volt relationship 27.211 386 245 981 0.000 000 000 030 eV +Hartree energy 4.359 744 722 2060 e-18 0.000 000 000 0048 e-18 J +Hartree energy in eV 27.211 386 245 981 0.000 000 000 030 eV +hartree-hertz relationship 6.579 683 920 4999 e15 0.000 000 000 0072 e15 Hz +hartree-inverse meter relationship 2.194 746 313 6314 e7 0.000 000 000 0024 e7 m^-1 +hartree-joule relationship 4.359 744 722 2060 e-18 0.000 000 000 0048 e-18 J +hartree-kelvin relationship 3.157 750 248 0398 e5 0.000 000 000 0034 e5 K +hartree-kilogram relationship 4.850 870 209 5419 e-35 0.000 000 000 0053 e-35 kg +helion-electron mass ratio 5495.885 279 84 0.000 000 16 +helion g factor -4.255 250 6995 0.000 000 0034 +helion mag. mom. -1.074 617 551 98 e-26 0.000 000 000 93 e-26 J T^-1 +helion mag. mom. to Bohr magneton ratio -1.158 740 980 83 e-3 0.000 000 000 94 e-3 +helion mag. mom. to nuclear magneton ratio -2.127 625 3498 0.000 000 0017 +helion mass 5.006 412 7862 e-27 0.000 000 0016 e-27 kg +helion mass energy equivalent 4.499 539 4185 e-10 0.000 000 0014 e-10 J +helion mass energy equivalent in MeV 2808.391 611 12 0.000 000 88 MeV +helion mass in u 3.014 932 246 932 0.000 000 000 074 u +helion molar mass 3.014 932 250 10 e-3 0.000 000 000 94 e-3 kg mol^-1 +helion-proton mass ratio 2.993 152 671 552 0.000 000 000 070 +helion relative atomic mass 3.014 932 246 932 0.000 000 000 074 +helion shielding shift 5.996 7029 e-5 0.000 0023 e-5 +hertz-atomic mass unit relationship 4.439 821 6590 e-24 0.000 000 0014 e-24 u +hertz-electron volt relationship 4.135 667 696... e-15 (exact) eV +hertz-hartree relationship 1.519 829 846 0574 e-16 0.000 000 000 0017 e-16 E_h +hertz-inverse meter relationship 3.335 640 951... e-9 (exact) m^-1 +hertz-joule relationship 6.626 070 15 e-34 (exact) J +hertz-kelvin relationship 4.799 243 073... e-11 (exact) K +hertz-kilogram relationship 7.372 497 323... e-51 (exact) kg +hyperfine transition frequency of Cs-133 9 192 631 770 (exact) Hz +inverse fine-structure constant 137.035 999 177 0.000 000 021 +inverse meter-atomic mass unit relationship 1.331 025 048 24 e-15 0.000 000 000 41 e-15 u +inverse meter-electron volt relationship 1.239 841 984... e-6 (exact) eV +inverse meter-hartree relationship 4.556 335 252 9132 e-8 0.000 000 000 0050 e-8 E_h +inverse meter-hertz relationship 299 792 458 (exact) Hz +inverse meter-joule relationship 1.986 445 857... e-25 (exact) J +inverse meter-kelvin relationship 1.438 776 877... e-2 (exact) K +inverse meter-kilogram relationship 2.210 219 094... e-42 (exact) kg +inverse of conductance quantum 12 906.403 72... (exact) ohm +Josephson constant 483 597.848 4... e9 (exact) Hz V^-1 +joule-atomic mass unit relationship 6.700 535 2471 e9 0.000 000 0021 e9 u +joule-electron volt relationship 6.241 509 074... e18 (exact) eV +joule-hartree relationship 2.293 712 278 3969 e17 0.000 000 000 0025 e17 E_h +joule-hertz relationship 1.509 190 179... e33 (exact) Hz +joule-inverse meter relationship 5.034 116 567... e24 (exact) m^-1 +joule-kelvin relationship 7.242 970 516... e22 (exact) K +joule-kilogram relationship 1.112 650 056... e-17 (exact) kg +kelvin-atomic mass unit relationship 9.251 087 2884 e-14 0.000 000 0029 e-14 u +kelvin-electron volt relationship 8.617 333 262... e-5 (exact) eV +kelvin-hartree relationship 3.166 811 563 4564 e-6 0.000 000 000 0035 e-6 E_h +kelvin-hertz relationship 2.083 661 912... e10 (exact) Hz +kelvin-inverse meter relationship 69.503 480 04... (exact) m^-1 +kelvin-joule relationship 1.380 649 e-23 (exact) J +kelvin-kilogram relationship 1.536 179 187... e-40 (exact) kg +kilogram-atomic mass unit relationship 6.022 140 7537 e26 0.000 000 0019 e26 u +kilogram-electron volt relationship 5.609 588 603... e35 (exact) eV +kilogram-hartree relationship 2.061 485 788 7415 e34 0.000 000 000 0022 e34 E_h +kilogram-hertz relationship 1.356 392 489... e50 (exact) Hz +kilogram-inverse meter relationship 4.524 438 335... e41 (exact) m^-1 +kilogram-joule relationship 8.987 551 787... e16 (exact) J +kilogram-kelvin relationship 6.509 657 260... e39 (exact) K +lattice parameter of silicon 5.431 020 511 e-10 0.000 000 089 e-10 m +lattice spacing of ideal Si (220) 1.920 155 716 e-10 0.000 000 032 e-10 m +Loschmidt constant (273.15 K, 100 kPa) 2.651 645 804... e25 (exact) m^-3 +Loschmidt constant (273.15 K, 101.325 kPa) 2.686 780 111... e25 (exact) m^-3 +luminous efficacy 683 (exact) lm W^-1 +mag. flux quantum 2.067 833 848... e-15 (exact) Wb +molar gas constant 8.314 462 618... (exact) J mol^-1 K^-1 +molar mass constant 1.000 000 001 05 e-3 0.000 000 000 31 e-3 kg mol^-1 +molar mass of carbon-12 12.000 000 0126 e-3 0.000 000 0037 e-3 kg mol^-1 +molar Planck constant 3.990 312 712... e-10 (exact) J Hz^-1 mol^-1 +molar volume of ideal gas (273.15 K, 100 kPa) 22.710 954 64... e-3 (exact) m^3 mol^-1 +molar volume of ideal gas (273.15 K, 101.325 kPa) 22.413 969 54... e-3 (exact) m^3 mol^-1 +molar volume of silicon 1.205 883 199 e-5 0.000 000 060 e-5 m^3 mol^-1 +Molybdenum x unit 1.002 099 52 e-13 0.000 000 53 e-13 m +muon Compton wavelength 1.173 444 110 e-14 0.000 000 026 e-14 m +muon-electron mass ratio 206.768 2827 0.000 0046 +muon g factor -2.002 331 841 23 0.000 000 000 82 +muon mag. mom. -4.490 448 30 e-26 0.000 000 10 e-26 J T^-1 +muon mag. mom. anomaly 1.165 920 62 e-3 0.000 000 41 e-3 +muon mag. mom. to Bohr magneton ratio -4.841 970 48 e-3 0.000 000 11 e-3 +muon mag. mom. to nuclear magneton ratio -8.890 597 04 0.000 000 20 +muon mass 1.883 531 627 e-28 0.000 000 042 e-28 kg +muon mass energy equivalent 1.692 833 804 e-11 0.000 000 038 e-11 J +muon mass energy equivalent in MeV 105.658 3755 0.000 0023 MeV +muon mass in u 0.113 428 9257 0.000 000 0025 u +muon molar mass 1.134 289 258 e-4 0.000 000 025 e-4 kg mol^-1 +muon-neutron mass ratio 0.112 454 5168 0.000 000 0025 +muon-proton mag. mom. ratio -3.183 345 146 0.000 000 071 +muon-proton mass ratio 0.112 609 5262 0.000 000 0025 +muon-tau mass ratio 5.946 35 e-2 0.000 40 e-2 +natural unit of action 1.054 571 817... e-34 (exact) J s +natural unit of action in eV s 6.582 119 569... e-16 (exact) eV s +natural unit of energy 8.187 105 7880 e-14 0.000 000 0026 e-14 J +natural unit of energy in MeV 0.510 998 950 69 0.000 000 000 16 MeV +natural unit of length 3.861 592 6744 e-13 0.000 000 0012 e-13 m +natural unit of mass 9.109 383 7139 e-31 0.000 000 0028 e-31 kg +natural unit of momentum 2.730 924 534 46 e-22 0.000 000 000 85 e-22 kg m s^-1 +natural unit of momentum in MeV/c 0.510 998 950 69 0.000 000 000 16 MeV/c +natural unit of time 1.288 088 666 44 e-21 0.000 000 000 40 e-21 s +natural unit of velocity 299 792 458 (exact) m s^-1 +neutron Compton wavelength 1.319 590 903 82 e-15 0.000 000 000 67 e-15 m +neutron-electron mag. mom. ratio 1.040 668 84 e-3 0.000 000 24 e-3 +neutron-electron mass ratio 1838.683 662 00 0.000 000 74 +neutron g factor -3.826 085 52 0.000 000 90 +neutron gyromag. ratio 1.832 471 74 e8 0.000 000 43 e8 s^-1 T^-1 +neutron gyromag. ratio in MHz/T 29.164 6935 0.000 0069 MHz T^-1 +neutron mag. mom. -9.662 3653 e-27 0.000 0023 e-27 J T^-1 +neutron mag. mom. to Bohr magneton ratio -1.041 875 65 e-3 0.000 000 25 e-3 +neutron mag. mom. to nuclear magneton ratio -1.913 042 76 0.000 000 45 +neutron mass 1.674 927 500 56 e-27 0.000 000 000 85 e-27 kg +neutron mass energy equivalent 1.505 349 765 14 e-10 0.000 000 000 76 e-10 J +neutron mass energy equivalent in MeV 939.565 421 94 0.000 000 48 MeV +neutron mass in u 1.008 664 916 06 0.000 000 000 40 u +neutron molar mass 1.008 664 917 12 e-3 0.000 000 000 51 e-3 kg mol^-1 +neutron-muon mass ratio 8.892 484 08 0.000 000 20 +neutron-proton mag. mom. ratio -0.684 979 35 0.000 000 16 +neutron-proton mass difference 2.305 574 61 e-30 0.000 000 67 e-30 kg +neutron-proton mass difference energy equivalent 2.072 147 12 e-13 0.000 000 60 e-13 J +neutron-proton mass difference energy equivalent in MeV 1.293 332 51 0.000 000 38 MeV +neutron-proton mass difference in u 1.388 449 48 e-3 0.000 000 40 e-3 u +neutron-proton mass ratio 1.001 378 419 46 0.000 000 000 40 +neutron relative atomic mass 1.008 664 916 06 0.000 000 000 40 +neutron-tau mass ratio 0.528 779 0.000 036 +neutron to shielded proton mag. mom. ratio -0.684 996 94 0.000 000 16 +Newtonian constant of gravitation 6.674 30 e-11 0.000 15 e-11 m^3 kg^-1 s^-2 +Newtonian constant of gravitation over h-bar c 6.708 83 e-39 0.000 15 e-39 (GeV/c^2)^-2 +nuclear magneton 5.050 783 7393 e-27 0.000 000 0016 e-27 J T^-1 +nuclear magneton in eV/T 3.152 451 254 17 e-8 0.000 000 000 98 e-8 eV T^-1 +nuclear magneton in inverse meter per tesla 2.542 623 410 09 e-2 0.000 000 000 79 e-2 m^-1 T^-1 +nuclear magneton in K/T 3.658 267 7706 e-4 0.000 000 0011 e-4 K T^-1 +nuclear magneton in MHz/T 7.622 593 2188 0.000 000 0024 MHz T^-1 +Planck constant 6.626 070 15 e-34 (exact) J Hz^-1 +Planck constant in eV/Hz 4.135 667 696... e-15 (exact) eV Hz^-1 +Planck length 1.616 255 e-35 0.000 018 e-35 m +Planck mass 2.176 434 e-8 0.000 024 e-8 kg +Planck mass energy equivalent in GeV 1.220 890 e19 0.000 014 e19 GeV +Planck temperature 1.416 784 e32 0.000 016 e32 K +Planck time 5.391 247 e-44 0.000 060 e-44 s +proton charge to mass quotient 9.578 833 1430 e7 0.000 000 0030 e7 C kg^-1 +proton Compton wavelength 1.321 409 853 60 e-15 0.000 000 000 41 e-15 m +proton-electron mass ratio 1836.152 673 426 0.000 000 032 +proton g factor 5.585 694 6893 0.000 000 0016 +proton gyromag. ratio 2.675 221 8708 e8 0.000 000 0011 e8 s^-1 T^-1 +proton gyromag. ratio in MHz/T 42.577 478 461 0.000 000 018 MHz T^-1 +proton mag. mom. 1.410 606 795 45 e-26 0.000 000 000 60 e-26 J T^-1 +proton mag. mom. to Bohr magneton ratio 1.521 032 202 30 e-3 0.000 000 000 45 e-3 +proton mag. mom. to nuclear magneton ratio 2.792 847 344 63 0.000 000 000 82 +proton mag. shielding correction 2.567 15 e-5 0.000 41 e-5 +proton mass 1.672 621 925 95 e-27 0.000 000 000 52 e-27 kg +proton mass energy equivalent 1.503 277 618 02 e-10 0.000 000 000 47 e-10 J +proton mass energy equivalent in MeV 938.272 089 43 0.000 000 29 MeV +proton mass in u 1.007 276 466 5789 0.000 000 000 0083 u +proton molar mass 1.007 276 467 64 e-3 0.000 000 000 31 e-3 kg mol^-1 +proton-muon mass ratio 8.880 243 38 0.000 000 20 +proton-neutron mag. mom. ratio -1.459 898 02 0.000 000 34 +proton-neutron mass ratio 0.998 623 477 97 0.000 000 000 40 +proton relative atomic mass 1.007 276 466 5789 0.000 000 000 0083 +proton rms charge radius 8.4075 e-16 0.0064 e-16 m +proton-tau mass ratio 0.528 051 0.000 036 +quantum of circulation 3.636 947 5467 e-4 0.000 000 0011 e-4 m^2 s^-1 +quantum of circulation times 2 7.273 895 0934 e-4 0.000 000 0023 e-4 m^2 s^-1 +reduced Compton wavelength 3.861 592 6744 e-13 0.000 000 0012 e-13 m +reduced muon Compton wavelength 1.867 594 306 e-15 0.000 000 042 e-15 m +reduced neutron Compton wavelength 2.100 194 1520 e-16 0.000 000 0011 e-16 m +reduced Planck constant 1.054 571 817... e-34 (exact) J s +reduced Planck constant in eV s 6.582 119 569... e-16 (exact) eV s +reduced Planck constant times c in MeV fm 197.326 980 4... (exact) MeV fm +reduced proton Compton wavelength 2.103 089 100 51 e-16 0.000 000 000 66 e-16 m +reduced tau Compton wavelength 1.110 538 e-16 0.000 075 e-16 m +Rydberg constant 10 973 731.568 157 0.000 012 m^-1 +Rydberg constant times c in Hz 3.289 841 960 2500 e15 0.000 000 000 0036 e15 Hz +Rydberg constant times hc in eV 13.605 693 122 990 0.000 000 000 015 eV +Rydberg constant times hc in J 2.179 872 361 1030 e-18 0.000 000 000 0024 e-18 J +Sackur-Tetrode constant (1 K, 100 kPa) -1.151 707 534 96 0.000 000 000 47 +Sackur-Tetrode constant (1 K, 101.325 kPa) -1.164 870 521 49 0.000 000 000 47 +second radiation constant 1.438 776 877... e-2 (exact) m K +shielded helion gyromag. ratio 2.037 894 6078 e8 0.000 000 0018 e8 s^-1 T^-1 +shielded helion gyromag. ratio in MHz/T 32.434 100 033 0.000 000 028 MHz T^-1 +shielded helion mag. mom. -1.074 553 110 35 e-26 0.000 000 000 93 e-26 J T^-1 +shielded helion mag. mom. to Bohr magneton ratio -1.158 671 494 57 e-3 0.000 000 000 94 e-3 +shielded helion mag. mom. to nuclear magneton ratio -2.127 497 7624 0.000 000 0017 +shielded helion to proton mag. mom. ratio -0.761 766 577 21 0.000 000 000 66 +shielded helion to shielded proton mag. mom. ratio -0.761 786 1334 0.000 000 0031 +shielded proton gyromag. ratio 2.675 153 194 e8 0.000 000 011 e8 s^-1 T^-1 +shielded proton gyromag. ratio in MHz/T 42.576 385 43 0.000 000 17 MHz T^-1 +shielded proton mag. mom. 1.410 570 5830 e-26 0.000 000 0058 e-26 J T^-1 +shielded proton mag. mom. to Bohr magneton ratio 1.520 993 1551 e-3 0.000 000 0062 e-3 +shielded proton mag. mom. to nuclear magneton ratio 2.792 775 648 0.000 000 011 +shielding difference of d and p in HD 1.987 70 e-8 0.000 10 e-8 +shielding difference of t and p in HT 2.394 50 e-8 0.000 20 e-8 +speed of light in vacuum 299 792 458 (exact) m s^-1 +standard acceleration of gravity 9.806 65 (exact) m s^-2 +standard atmosphere 101 325 (exact) Pa +standard-state pressure 100 000 (exact) Pa +Stefan-Boltzmann constant 5.670 374 419... e-8 (exact) W m^-2 K^-4 +tau Compton wavelength 6.977 71 e-16 0.000 47 e-16 m +tau-electron mass ratio 3477.23 0.23 +tau energy equivalent 1776.86 0.12 MeV +tau mass 3.167 54 e-27 0.000 21 e-27 kg +tau mass energy equivalent 2.846 84 e-10 0.000 19 e-10 J +tau mass in u 1.907 54 0.000 13 u +tau molar mass 1.907 54 e-3 0.000 13 e-3 kg mol^-1 +tau-muon mass ratio 16.8170 0.0011 +tau-neutron mass ratio 1.891 15 0.000 13 +tau-proton mass ratio 1.893 76 0.000 13 +Thomson cross section 6.652 458 7051 e-29 0.000 000 0062 e-29 m^2 +triton-electron mass ratio 5496.921 535 51 0.000 000 21 +triton g factor 5.957 924 930 0.000 000 012 +triton mag. mom. 1.504 609 5178 e-26 0.000 000 0030 e-26 J T^-1 +triton mag. mom. to Bohr magneton ratio 1.622 393 6648 e-3 0.000 000 0032 e-3 +triton mag. mom. to nuclear magneton ratio 2.978 962 4650 0.000 000 0059 +triton mass 5.007 356 7512 e-27 0.000 000 0016 e-27 kg +triton mass energy equivalent 4.500 387 8119 e-10 0.000 000 0014 e-10 J +triton mass energy equivalent in MeV 2808.921 136 68 0.000 000 88 MeV +triton mass in u 3.015 500 715 97 0.000 000 000 10 u +triton molar mass 3.015 500 719 13 e-3 0.000 000 000 94 e-3 kg mol^-1 +triton-proton mass ratio 2.993 717 034 03 0.000 000 000 10 +triton relative atomic mass 3.015 500 715 97 0.000 000 000 10 +triton to proton mag. mom. ratio 1.066 639 9189 0.000 000 0021 +unified atomic mass unit 1.660 539 068 92 e-27 0.000 000 000 52 e-27 kg +vacuum electric permittivity 8.854 187 8188 e-12 0.000 000 0014 e-12 F m^-1 +vacuum mag. permeability 1.256 637 061 27 e-6 0.000 000 000 20 e-6 N A^-2 +von Klitzing constant 25 812.807 45... (exact) ohm +weak mixing angle 0.223 05 0.000 23 +Wien frequency displacement law constant 5.878 925 757... e10 (exact) Hz K^-1 +Wien wavelength displacement law constant 2.897 771 955... e-3 (exact) m K +W to Z mass ratio 0.881 45 0.000 13 """ + + +exact2022 = exact2018 + + +# ----------------------------------------------------------------------------- + + +def parse_constants_2002to2014( + d: str, exact_func: Callable[[Any], Any] +) -> dict[str, tuple[float, str, float]]: + constants: dict[str, tuple[float, str, float]] = {} + exact: dict[str, float] = {} + need_replace = set() + for line in d.split('\n'): + name = line[:55].rstrip() + val = float(line[55:77].replace(' ', '').replace('...', '')) + is_truncated = '...' in line[55:77] + is_exact = '(exact)' in line[77:99] + if is_truncated and is_exact: + # missing decimals, use computed exact value + need_replace.add(name) + elif is_exact: + exact[name] = val + else: + assert not is_truncated + uncert = float(line[77:99].replace(' ', '').replace('(exact)', '0')) + units = line[99:].rstrip() + constants[name] = (val, units, uncert) + replace = exact_func(exact) + replace_exact(constants, need_replace, replace) + return constants + + +def parse_constants_2018toXXXX( + d: str, exact_func: Callable[[Any], Any] +) -> dict[str, tuple[float, str, float]]: + constants: dict[str, tuple[float, str, float]] = {} + exact: dict[str, float] = {} + need_replace = set() + for line in d.split('\n'): + name = line[:60].rstrip() + val = float(line[60:85].replace(' ', '').replace('...', '')) + is_truncated = '...' in line[60:85] + is_exact = '(exact)' in line[85:110] + if is_truncated and is_exact: + # missing decimals, use computed exact value + need_replace.add(name) + elif is_exact: + exact[name] = val + else: + assert not is_truncated + uncert = float(line[85:110].replace(' ', '').replace('(exact)', '0')) + units = line[110:].rstrip() + constants[name] = (val, units, uncert) + replace = exact_func(exact) + replace_exact(constants, need_replace, replace) + return constants + + +def replace_exact(d, to_replace, exact): + for name in to_replace: + assert name in exact, f'Missing exact value: {name}' + assert abs(exact[name]/d[name][0] - 1) <= 1e-9, \ + f'Bad exact value: {name}: { exact[name]}, {d[name][0]}' + d[name] = (exact[name],) + d[name][1:] + assert set(exact.keys()) == set(to_replace) + + +_physical_constants_2002 = parse_constants_2002to2014(txt2002, exact2002) +_physical_constants_2006 = parse_constants_2002to2014(txt2006, exact2006) +_physical_constants_2010 = parse_constants_2002to2014(txt2010, exact2010) +_physical_constants_2014 = parse_constants_2002to2014(txt2014, exact2014) +_physical_constants_2018 = parse_constants_2018toXXXX(txt2018, exact2018) +_physical_constants_2022 = parse_constants_2018toXXXX(txt2022, exact2022) + +physical_constants: dict[str, tuple[float, str, float]] = {} +physical_constants.update(_physical_constants_2002) +physical_constants.update(_physical_constants_2006) +physical_constants.update(_physical_constants_2010) +physical_constants.update(_physical_constants_2014) +physical_constants.update(_physical_constants_2018) +physical_constants.update(_physical_constants_2022) +_current_constants = _physical_constants_2022 +_current_codata = "CODATA 2022" + +# check obsolete values +_obsolete_constants = {} +for k in physical_constants: + if k not in _current_constants: + _obsolete_constants[k] = True + +# generate some additional aliases +_aliases = {} +for k in _physical_constants_2002: + if 'magn.' in k: + _aliases[k] = k.replace('magn.', 'mag.') +for k in _physical_constants_2006: + if 'momentum' in k: + _aliases[k] = k.replace('momentum', 'mom.um') +for k in _physical_constants_2018: + if 'momentum' in k: + _aliases[k] = k.replace('momentum', 'mom.um') +for k in _physical_constants_2022: + if 'momentum' in k: + _aliases[k] = k.replace('momentum', 'mom.um') + +# CODATA 2018 and 2022: renamed and no longer exact; use as aliases +_aliases['mag. constant'] = 'vacuum mag. permeability' +_aliases['electric constant'] = 'vacuum electric permittivity' + + +_extra_alias_keys = ['natural unit of velocity', + 'natural unit of action', + 'natural unit of action in eV s', + 'natural unit of mass', + 'natural unit of energy', + 'natural unit of energy in MeV', + 'natural unit of mom.um', + 'natural unit of mom.um in MeV/c', + 'natural unit of length', + 'natural unit of time'] + +# finally, insert aliases for values +for k, v in list(_aliases.items()): + if v in _current_constants or v in _extra_alias_keys: + physical_constants[k] = physical_constants[v] + else: + del _aliases[k] + + +class ConstantWarning(DeprecationWarning): + """Accessing a constant no longer in current CODATA data set""" + pass + + +def _check_obsolete(key: str) -> None: + if key in _obsolete_constants and key not in _aliases: + warnings.warn(f"Constant '{key}' is not in current {_current_codata} data set", + ConstantWarning, stacklevel=3) + + +def value(key: str) -> float: + """ + Value in physical_constants indexed by key + + Parameters + ---------- + key : Python string + Key in dictionary `physical_constants` + + Returns + ------- + value : float + Value in `physical_constants` corresponding to `key` + + Examples + -------- + >>> from scipy import constants + >>> constants.value('elementary charge') + 1.602176634e-19 + + """ + _check_obsolete(key) + return physical_constants[key][0] + + +def unit(key: str) -> str: + """ + Unit in physical_constants indexed by key + + Parameters + ---------- + key : Python string + Key in dictionary `physical_constants` + + Returns + ------- + unit : Python string + Unit in `physical_constants` corresponding to `key` + + Examples + -------- + >>> from scipy import constants + >>> constants.unit('proton mass') + 'kg' + + """ + _check_obsolete(key) + return physical_constants[key][1] + + +def precision(key: str) -> float: + """ + Relative precision in physical_constants indexed by key + + Parameters + ---------- + key : Python string + Key in dictionary `physical_constants` + + Returns + ------- + prec : float + Relative precision in `physical_constants` corresponding to `key` + + Examples + -------- + >>> from scipy import constants + >>> constants.precision('proton mass') + 5.1e-37 + + """ + _check_obsolete(key) + return physical_constants[key][2] / physical_constants[key][0] + + +def find(sub: str | None = None, disp: bool = False) -> Any: + """ + Return list of physical_constant keys containing a given string. + + Parameters + ---------- + sub : str + Sub-string to search keys for. By default, return all keys. + disp : bool + If True, print the keys that are found and return None. + Otherwise, return the list of keys without printing anything. + + Returns + ------- + keys : list or None + If `disp` is False, the list of keys is returned. + Otherwise, None is returned. + + Examples + -------- + >>> from scipy.constants import find, physical_constants + + Which keys in the ``physical_constants`` dictionary contain 'boltzmann'? + + >>> find('boltzmann') + ['Boltzmann constant', + 'Boltzmann constant in Hz/K', + 'Boltzmann constant in eV/K', + 'Boltzmann constant in inverse meter per kelvin', + 'Stefan-Boltzmann constant'] + + Get the constant called 'Boltzmann constant in Hz/K': + + >>> physical_constants['Boltzmann constant in Hz/K'] + (20836619120.0, 'Hz K^-1', 0.0) + + Find constants with 'radius' in the key: + + >>> find('radius') + ['Bohr radius', + 'alpha particle rms charge radius', + 'classical electron radius', + 'deuteron rms charge radius', + 'proton rms charge radius'] + >>> physical_constants['classical electron radius'] + (2.8179403262e-15, 'm', 1.3e-24) + + """ + if sub is None: + result = list(_current_constants.keys()) + else: + result = [key for key in _current_constants + if sub.lower() in key.lower()] + + result.sort() + if disp: + for key in result: + print(key) + return + else: + return result + +# This is not used here, but it must be defined to pass +# scipy/_lib/tests/test_public_api.py::test_private_but_present_deprecation +c = value('speed of light in vacuum') diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/constants/_constants.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/constants/_constants.py new file mode 100644 index 0000000000000000000000000000000000000000..a3a098d5469bb7195202a638022b1120ec0969bd --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/constants/_constants.py @@ -0,0 +1,366 @@ +""" +Collection of physical constants and conversion factors. + +Most constants are in SI units, so you can do +print '10 mile per minute is', 10*mile/minute, 'm/s or', 10*mile/(minute*knot), 'knots' + +The list is not meant to be comprehensive, but just convenient for everyday use. +""" + +import math as _math +from typing import TYPE_CHECKING, Any + +from ._codata import value as _cd + +if TYPE_CHECKING: + import numpy.typing as npt + +from scipy._lib._array_api import array_namespace, _asarray + + +""" +BasSw 2006 +physical constants: imported from CODATA +unit conversion: see e.g., NIST special publication 811 +Use at own risk: double-check values before calculating your Mars orbit-insertion burn. +Some constants exist in a few variants, which are marked with suffixes. +The ones without any suffix should be the most common ones. +""" + +__all__ = [ + 'Avogadro', 'Boltzmann', 'Btu', 'Btu_IT', 'Btu_th', 'G', + 'Julian_year', 'N_A', 'Planck', 'R', 'Rydberg', + 'Stefan_Boltzmann', 'Wien', 'acre', 'alpha', + 'angstrom', 'arcmin', 'arcminute', 'arcsec', + 'arcsecond', 'astronomical_unit', 'atm', + 'atmosphere', 'atomic_mass', 'atto', 'au', 'bar', + 'barrel', 'bbl', 'blob', 'c', 'calorie', + 'calorie_IT', 'calorie_th', 'carat', 'centi', + 'convert_temperature', 'day', 'deci', 'degree', + 'degree_Fahrenheit', 'deka', 'dyn', 'dyne', 'e', + 'eV', 'electron_mass', 'electron_volt', + 'elementary_charge', 'epsilon_0', 'erg', + 'exa', 'exbi', 'femto', 'fermi', 'fine_structure', + 'fluid_ounce', 'fluid_ounce_US', 'fluid_ounce_imp', + 'foot', 'g', 'gallon', 'gallon_US', 'gallon_imp', + 'gas_constant', 'gibi', 'giga', 'golden', 'golden_ratio', + 'grain', 'gram', 'gravitational_constant', 'h', 'hbar', + 'hectare', 'hecto', 'horsepower', 'hour', 'hp', + 'inch', 'k', 'kgf', 'kibi', 'kilo', 'kilogram_force', + 'kmh', 'knot', 'lambda2nu', 'lb', 'lbf', + 'light_year', 'liter', 'litre', 'long_ton', 'm_e', + 'm_n', 'm_p', 'm_u', 'mach', 'mebi', 'mega', + 'metric_ton', 'micro', 'micron', 'mil', 'mile', + 'milli', 'minute', 'mmHg', 'mph', 'mu_0', 'nano', + 'nautical_mile', 'neutron_mass', 'nu2lambda', + 'ounce', 'oz', 'parsec', 'pebi', 'peta', + 'pi', 'pico', 'point', 'pound', 'pound_force', + 'proton_mass', 'psi', 'pt', 'quecto', 'quetta', 'ronna', 'ronto', + 'short_ton', 'sigma', 'slinch', 'slug', 'speed_of_light', + 'speed_of_sound', 'stone', 'survey_foot', + 'survey_mile', 'tebi', 'tera', 'ton_TNT', + 'torr', 'troy_ounce', 'troy_pound', 'u', + 'week', 'yard', 'year', 'yobi', 'yocto', + 'yotta', 'zebi', 'zepto', 'zero_Celsius', 'zetta' +] + + +# mathematical constants +pi = _math.pi +golden = golden_ratio = (1 + _math.sqrt(5)) / 2 + +# SI prefixes +quetta = 1e30 +ronna = 1e27 +yotta = 1e24 +zetta = 1e21 +exa = 1e18 +peta = 1e15 +tera = 1e12 +giga = 1e9 +mega = 1e6 +kilo = 1e3 +hecto = 1e2 +deka = 1e1 +deci = 1e-1 +centi = 1e-2 +milli = 1e-3 +micro = 1e-6 +nano = 1e-9 +pico = 1e-12 +femto = 1e-15 +atto = 1e-18 +zepto = 1e-21 +yocto = 1e-24 +ronto = 1e-27 +quecto = 1e-30 + +# binary prefixes +kibi = 2**10 +mebi = 2**20 +gibi = 2**30 +tebi = 2**40 +pebi = 2**50 +exbi = 2**60 +zebi = 2**70 +yobi = 2**80 + +# physical constants +c = speed_of_light = _cd('speed of light in vacuum') +mu_0 = _cd('vacuum mag. permeability') +epsilon_0 = _cd('vacuum electric permittivity') +h = Planck = _cd('Planck constant') +hbar = _cd('reduced Planck constant') +G = gravitational_constant = _cd('Newtonian constant of gravitation') +g = _cd('standard acceleration of gravity') +e = elementary_charge = _cd('elementary charge') +R = gas_constant = _cd('molar gas constant') +alpha = fine_structure = _cd('fine-structure constant') +N_A = Avogadro = _cd('Avogadro constant') +k = Boltzmann = _cd('Boltzmann constant') +sigma = Stefan_Boltzmann = _cd('Stefan-Boltzmann constant') +Wien = _cd('Wien wavelength displacement law constant') +Rydberg = _cd('Rydberg constant') + +# mass in kg +gram = 1e-3 +metric_ton = 1e3 +grain = 64.79891e-6 +lb = pound = 7000 * grain # avoirdupois +blob = slinch = pound * g / 0.0254 # lbf*s**2/in (added in 1.0.0) +slug = blob / 12 # lbf*s**2/foot (added in 1.0.0) +oz = ounce = pound / 16 +stone = 14 * pound +long_ton = 2240 * pound +short_ton = 2000 * pound + +troy_ounce = 480 * grain # only for metals / gems +troy_pound = 12 * troy_ounce +carat = 200e-6 + +m_e = electron_mass = _cd('electron mass') +m_p = proton_mass = _cd('proton mass') +m_n = neutron_mass = _cd('neutron mass') +m_u = u = atomic_mass = _cd('atomic mass constant') + +# angle in rad +degree = pi / 180 +arcmin = arcminute = degree / 60 +arcsec = arcsecond = arcmin / 60 + +# time in second +minute = 60.0 +hour = 60 * minute +day = 24 * hour +week = 7 * day +year = 365 * day +Julian_year = 365.25 * day + +# length in meter +inch = 0.0254 +foot = 12 * inch +yard = 3 * foot +mile = 1760 * yard +mil = inch / 1000 +pt = point = inch / 72 # typography +survey_foot = 1200.0 / 3937 +survey_mile = 5280 * survey_foot +nautical_mile = 1852.0 +fermi = 1e-15 +angstrom = 1e-10 +micron = 1e-6 +au = astronomical_unit = 149597870700.0 +light_year = Julian_year * c +parsec = au / arcsec + +# pressure in pascal +atm = atmosphere = _cd('standard atmosphere') +bar = 1e5 +torr = mmHg = atm / 760 +psi = pound * g / (inch * inch) + +# area in meter**2 +hectare = 1e4 +acre = 43560 * foot**2 + +# volume in meter**3 +litre = liter = 1e-3 +gallon = gallon_US = 231 * inch**3 # US +# pint = gallon_US / 8 +fluid_ounce = fluid_ounce_US = gallon_US / 128 +bbl = barrel = 42 * gallon_US # for oil + +gallon_imp = 4.54609e-3 # UK +fluid_ounce_imp = gallon_imp / 160 + +# speed in meter per second +kmh = 1e3 / hour +mph = mile / hour +# approx value of mach at 15 degrees in 1 atm. Is this a common value? +mach = speed_of_sound = 340.5 +knot = nautical_mile / hour + +# temperature in kelvin +zero_Celsius = 273.15 +degree_Fahrenheit = 1/1.8 # only for differences + +# energy in joule +eV = electron_volt = elementary_charge # * 1 Volt +calorie = calorie_th = 4.184 +calorie_IT = 4.1868 +erg = 1e-7 +Btu_th = pound * degree_Fahrenheit * calorie_th / gram +Btu = Btu_IT = pound * degree_Fahrenheit * calorie_IT / gram +ton_TNT = 1e9 * calorie_th +# Wh = watt_hour + +# power in watt +hp = horsepower = 550 * foot * pound * g + +# force in newton +dyn = dyne = 1e-5 +lbf = pound_force = pound * g +kgf = kilogram_force = g # * 1 kg + +# functions for conversions that are not linear + + +def convert_temperature( + val: "npt.ArrayLike", + old_scale: str, + new_scale: str, +) -> Any: + """ + Convert from a temperature scale to another one among Celsius, Kelvin, + Fahrenheit, and Rankine scales. + + Parameters + ---------- + val : array_like + Value(s) of the temperature(s) to be converted expressed in the + original scale. + old_scale : str + Specifies as a string the original scale from which the temperature + value(s) will be converted. Supported scales are Celsius ('Celsius', + 'celsius', 'C' or 'c'), Kelvin ('Kelvin', 'kelvin', 'K', 'k'), + Fahrenheit ('Fahrenheit', 'fahrenheit', 'F' or 'f'), and Rankine + ('Rankine', 'rankine', 'R', 'r'). + new_scale : str + Specifies as a string the new scale to which the temperature + value(s) will be converted. Supported scales are Celsius ('Celsius', + 'celsius', 'C' or 'c'), Kelvin ('Kelvin', 'kelvin', 'K', 'k'), + Fahrenheit ('Fahrenheit', 'fahrenheit', 'F' or 'f'), and Rankine + ('Rankine', 'rankine', 'R', 'r'). + + Returns + ------- + res : float or array of floats + Value(s) of the converted temperature(s) expressed in the new scale. + + Notes + ----- + .. versionadded:: 0.18.0 + + Examples + -------- + >>> from scipy.constants import convert_temperature + >>> import numpy as np + >>> convert_temperature(np.array([-40, 40]), 'Celsius', 'Kelvin') + array([ 233.15, 313.15]) + + """ + xp = array_namespace(val) + _val = _asarray(val, xp=xp, subok=True) + # Convert from `old_scale` to Kelvin + if old_scale.lower() in ['celsius', 'c']: + tempo = _val + zero_Celsius + elif old_scale.lower() in ['kelvin', 'k']: + tempo = _val + elif old_scale.lower() in ['fahrenheit', 'f']: + tempo = (_val - 32) * 5 / 9 + zero_Celsius + elif old_scale.lower() in ['rankine', 'r']: + tempo = _val * 5 / 9 + else: + raise NotImplementedError(f"{old_scale=} is unsupported: supported scales " + "are Celsius, Kelvin, Fahrenheit, and " + "Rankine") + # and from Kelvin to `new_scale`. + if new_scale.lower() in ['celsius', 'c']: + res = tempo - zero_Celsius + elif new_scale.lower() in ['kelvin', 'k']: + res = tempo + elif new_scale.lower() in ['fahrenheit', 'f']: + res = (tempo - zero_Celsius) * 9 / 5 + 32 + elif new_scale.lower() in ['rankine', 'r']: + res = tempo * 9 / 5 + else: + raise NotImplementedError(f"{new_scale=} is unsupported: supported " + "scales are 'Celsius', 'Kelvin', " + "'Fahrenheit', and 'Rankine'") + + return res + + +# optics + + +def lambda2nu(lambda_: "npt.ArrayLike") -> Any: + """ + Convert wavelength to optical frequency + + Parameters + ---------- + lambda_ : array_like + Wavelength(s) to be converted. + + Returns + ------- + nu : float or array of floats + Equivalent optical frequency. + + Notes + ----- + Computes ``nu = c / lambda`` where c = 299792458.0, i.e., the + (vacuum) speed of light in meters/second. + + Examples + -------- + >>> from scipy.constants import lambda2nu, speed_of_light + >>> import numpy as np + >>> lambda2nu(np.array((1, speed_of_light))) + array([ 2.99792458e+08, 1.00000000e+00]) + + """ + xp = array_namespace(lambda_) + return c / _asarray(lambda_, xp=xp, subok=True) + + +def nu2lambda(nu: "npt.ArrayLike") -> Any: + """ + Convert optical frequency to wavelength. + + Parameters + ---------- + nu : array_like + Optical frequency to be converted. + + Returns + ------- + lambda : float or array of floats + Equivalent wavelength(s). + + Notes + ----- + Computes ``lambda = c / nu`` where c = 299792458.0, i.e., the + (vacuum) speed of light in meters/second. + + Examples + -------- + >>> from scipy.constants import nu2lambda, speed_of_light + >>> import numpy as np + >>> nu2lambda(np.array((1, speed_of_light))) + array([ 2.99792458e+08, 1.00000000e+00]) + + """ + xp = array_namespace(nu) + return c / _asarray(nu, xp=xp, subok=True) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/constants/codata.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/constants/codata.py new file mode 100644 index 0000000000000000000000000000000000000000..912e0bbf7c4f14d23ced4546b6704f7789996d97 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/constants/codata.py @@ -0,0 +1,21 @@ +# This file is not meant for public use and will be removed in SciPy v2.0.0. +# Use the `scipy.constants` namespace for importing the functions +# included below. + +from scipy._lib.deprecation import _sub_module_deprecation + +__all__ = [ # noqa: F822 + 'physical_constants', 'value', 'unit', 'precision', 'find', + 'ConstantWarning', 'k', 'c', + +] + + +def __dir__(): + return __all__ + + +def __getattr__(name): + return _sub_module_deprecation(sub_package="constants", module="codata", + private_modules=["_codata"], all=__all__, + attribute=name) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/constants/constants.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/constants/constants.py new file mode 100644 index 0000000000000000000000000000000000000000..855901ba802881090b99b7e8972de741331c7ab9 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/constants/constants.py @@ -0,0 +1,53 @@ +# This file is not meant for public use and will be removed in SciPy v2.0.0. +# Use the `scipy.constants` namespace for importing the functions +# included below. + +from scipy._lib.deprecation import _sub_module_deprecation + + +__all__ = [ # noqa: F822 + 'Avogadro', 'Boltzmann', 'Btu', 'Btu_IT', 'Btu_th', 'G', + 'Julian_year', 'N_A', 'Planck', 'R', 'Rydberg', + 'Stefan_Boltzmann', 'Wien', 'acre', 'alpha', + 'angstrom', 'arcmin', 'arcminute', 'arcsec', + 'arcsecond', 'astronomical_unit', 'atm', + 'atmosphere', 'atomic_mass', 'atto', 'au', 'bar', + 'barrel', 'bbl', 'blob', 'c', 'calorie', + 'calorie_IT', 'calorie_th', 'carat', 'centi', + 'convert_temperature', 'day', 'deci', 'degree', + 'degree_Fahrenheit', 'deka', 'dyn', 'dyne', 'e', + 'eV', 'electron_mass', 'electron_volt', + 'elementary_charge', 'epsilon_0', 'erg', + 'exa', 'exbi', 'femto', 'fermi', 'fine_structure', + 'fluid_ounce', 'fluid_ounce_US', 'fluid_ounce_imp', + 'foot', 'g', 'gallon', 'gallon_US', 'gallon_imp', + 'gas_constant', 'gibi', 'giga', 'golden', 'golden_ratio', + 'grain', 'gram', 'gravitational_constant', 'h', 'hbar', + 'hectare', 'hecto', 'horsepower', 'hour', 'hp', + 'inch', 'k', 'kgf', 'kibi', 'kilo', 'kilogram_force', + 'kmh', 'knot', 'lambda2nu', 'lb', 'lbf', + 'light_year', 'liter', 'litre', 'long_ton', 'm_e', + 'm_n', 'm_p', 'm_u', 'mach', 'mebi', 'mega', + 'metric_ton', 'micro', 'micron', 'mil', 'mile', + 'milli', 'minute', 'mmHg', 'mph', 'mu_0', 'nano', + 'nautical_mile', 'neutron_mass', 'nu2lambda', + 'ounce', 'oz', 'parsec', 'pebi', 'peta', + 'pi', 'pico', 'point', 'pound', 'pound_force', + 'proton_mass', 'psi', 'pt', 'short_ton', + 'sigma', 'slinch', 'slug', 'speed_of_light', + 'speed_of_sound', 'stone', 'survey_foot', + 'survey_mile', 'tebi', 'tera', 'ton_TNT', + 'torr', 'troy_ounce', 'troy_pound', 'u', + 'week', 'yard', 'year', 'yobi', 'yocto', + 'yotta', 'zebi', 'zepto', 'zero_Celsius', 'zetta' +] + + +def __dir__(): + return __all__ + + +def __getattr__(name): + return _sub_module_deprecation(sub_package="constants", module="constants", + private_modules=["_constants"], all=__all__, + attribute=name) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/constants/tests/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/constants/tests/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/constants/tests/test_codata.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/constants/tests/test_codata.py new file mode 100644 index 0000000000000000000000000000000000000000..51b77c491344963c648fef90cdeaa5adac5d9a6f --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/constants/tests/test_codata.py @@ -0,0 +1,78 @@ +from scipy.constants import find, value, c, speed_of_light, precision +from numpy.testing import assert_equal, assert_, assert_almost_equal +import scipy.constants._codata as _cd +from scipy import constants + + +def test_find(): + keys = find('weak mixing', disp=False) + assert_equal(keys, ['weak mixing angle']) + + keys = find('qwertyuiop', disp=False) + assert_equal(keys, []) + + keys = find('natural unit', disp=False) + assert_equal(keys, sorted(['natural unit of velocity', + 'natural unit of action', + 'natural unit of action in eV s', + 'natural unit of mass', + 'natural unit of energy', + 'natural unit of energy in MeV', + 'natural unit of momentum', + 'natural unit of momentum in MeV/c', + 'natural unit of length', + 'natural unit of time'])) + + +def test_basic_table_parse(): + c_s = 'speed of light in vacuum' + assert_equal(value(c_s), c) + assert_equal(value(c_s), speed_of_light) + + +def test_basic_lookup(): + assert_equal('%d %s' % (_cd.value('speed of light in vacuum'), + _cd.unit('speed of light in vacuum')), + '299792458 m s^-1') + + +def test_find_all(): + assert_(len(find(disp=False)) > 300) + + +def test_find_single(): + assert_equal(find('Wien freq', disp=False)[0], + 'Wien frequency displacement law constant') + + +def test_2002_vs_2006(): + assert_almost_equal(value('magn. flux quantum'), + value('mag. flux quantum')) + + +def test_exact_values(): + # Check that updating stored values with exact ones worked. + exact = dict((k, v[0]) for k, v in _cd._physical_constants_2018.items()) + replace = _cd.exact2018(exact) + for key, val in replace.items(): + assert_equal(val, value(key)) + assert precision(key) == 0 + + +def test_gh11341(): + # gh-11341 noted that these three constants should exist (for backward + # compatibility) and should always have the same value: + a = constants.epsilon_0 + b = constants.physical_constants['electric constant'][0] + c = constants.physical_constants['vacuum electric permittivity'][0] + assert a == b == c + + +def test_gh14467(): + # gh-14467 noted that some physical constants in CODATA are rounded + # to only ten significant figures even though they are supposed to be + # exact. Check that (at least) the case mentioned in the issue is resolved. + res = constants.physical_constants['Boltzmann constant in eV/K'][0] + ref = (constants.physical_constants['Boltzmann constant'][0] + / constants.physical_constants['elementary charge'][0]) + assert res == ref diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/constants/tests/test_constants.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/constants/tests/test_constants.py new file mode 100644 index 0000000000000000000000000000000000000000..6b9dcd3b5355063cffb3b7a144937a95aab77955 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/constants/tests/test_constants.py @@ -0,0 +1,90 @@ +import pytest + +import scipy.constants as sc +from scipy.conftest import array_api_compatible +from scipy._lib._array_api_no_0d import xp_assert_equal, xp_assert_close +from numpy.testing import assert_allclose + + +pytestmark = [array_api_compatible, pytest.mark.usefixtures("skip_xp_backends")] +skip_xp_backends = pytest.mark.skip_xp_backends + + +class TestConvertTemperature: + def test_convert_temperature(self, xp): + xp_assert_equal(sc.convert_temperature(xp.asarray(32.), 'f', 'Celsius'), + xp.asarray(0.0)) + xp_assert_equal(sc.convert_temperature(xp.asarray([0., 0.]), + 'celsius', 'Kelvin'), + xp.asarray([273.15, 273.15])) + xp_assert_equal(sc.convert_temperature(xp.asarray([0., 0.]), 'kelvin', 'c'), + xp.asarray([-273.15, -273.15])) + xp_assert_equal(sc.convert_temperature(xp.asarray([32., 32.]), 'f', 'k'), + xp.asarray([273.15, 273.15])) + xp_assert_equal(sc.convert_temperature(xp.asarray([273.15, 273.15]), + 'kelvin', 'F'), + xp.asarray([32., 32.])) + xp_assert_equal(sc.convert_temperature(xp.asarray([0., 0.]), 'C', 'fahrenheit'), + xp.asarray([32., 32.])) + xp_assert_close(sc.convert_temperature(xp.asarray([0., 0.], dtype=xp.float64), + 'c', 'r'), + xp.asarray([491.67, 491.67], dtype=xp.float64), + rtol=0., atol=1e-13) + xp_assert_close(sc.convert_temperature(xp.asarray([491.67, 491.67], + dtype=xp.float64), + 'Rankine', 'C'), + xp.asarray([0., 0.], dtype=xp.float64), rtol=0., atol=1e-13) + xp_assert_close(sc.convert_temperature(xp.asarray([491.67, 491.67], + dtype=xp.float64), + 'r', 'F'), + xp.asarray([32., 32.], dtype=xp.float64), rtol=0., atol=1e-13) + xp_assert_close(sc.convert_temperature(xp.asarray([32., 32.], dtype=xp.float64), + 'fahrenheit', 'R'), + xp.asarray([491.67, 491.67], dtype=xp.float64), + rtol=0., atol=1e-13) + xp_assert_close(sc.convert_temperature(xp.asarray([273.15, 273.15], + dtype=xp.float64), + 'K', 'R'), + xp.asarray([491.67, 491.67], dtype=xp.float64), + rtol=0., atol=1e-13) + xp_assert_close(sc.convert_temperature(xp.asarray([491.67, 0.], + dtype=xp.float64), + 'rankine', 'kelvin'), + xp.asarray([273.15, 0.], dtype=xp.float64), rtol=0., atol=1e-13) + + @skip_xp_backends(np_only=True, reason='Python list input uses NumPy backend') + def test_convert_temperature_array_like(self): + assert_allclose(sc.convert_temperature([491.67, 0.], 'rankine', 'kelvin'), + [273.15, 0.], rtol=0., atol=1e-13) + + + @skip_xp_backends(np_only=True, reason='Python int input uses NumPy backend') + def test_convert_temperature_errors(self, xp): + with pytest.raises(NotImplementedError, match="old_scale="): + sc.convert_temperature(1, old_scale="cheddar", new_scale="kelvin") + with pytest.raises(NotImplementedError, match="new_scale="): + sc.convert_temperature(1, old_scale="kelvin", new_scale="brie") + + +class TestLambdaToNu: + def test_lambda_to_nu(self, xp): + xp_assert_equal(sc.lambda2nu(xp.asarray([sc.speed_of_light, 1])), + xp.asarray([1, sc.speed_of_light])) + + + @skip_xp_backends(np_only=True, reason='Python list input uses NumPy backend') + def test_lambda_to_nu_array_like(self, xp): + assert_allclose(sc.lambda2nu([sc.speed_of_light, 1]), + [1, sc.speed_of_light]) + + +class TestNuToLambda: + def test_nu_to_lambda(self, xp): + xp_assert_equal(sc.nu2lambda(xp.asarray([sc.speed_of_light, 1])), + xp.asarray([1, sc.speed_of_light])) + + @skip_xp_backends(np_only=True, reason='Python list input uses NumPy backend') + def test_nu_to_lambda_array_like(self, xp): + assert_allclose(sc.nu2lambda([sc.speed_of_light, 1]), + [1, sc.speed_of_light]) + diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/datasets/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/datasets/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..fdd4ffebec4c57f6d399a0f76df2b66056f0b225 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/datasets/__init__.py @@ -0,0 +1,90 @@ +""" +================================ +Datasets (:mod:`scipy.datasets`) +================================ + +.. currentmodule:: scipy.datasets + +Dataset Methods +=============== + +.. autosummary:: + :toctree: generated/ + + ascent + face + electrocardiogram + +Utility Methods +=============== + +.. autosummary:: + :toctree: generated/ + + download_all -- Download all the dataset files to specified path. + clear_cache -- Clear cached dataset directory. + + +Usage of Datasets +================= + +SciPy dataset methods can be simply called as follows: ``'()'`` +This downloads the dataset files over the network once, and saves the cache, +before returning a `numpy.ndarray` object representing the dataset. + +Note that the return data structure and data type might be different for +different dataset methods. For a more detailed example on usage, please look +into the particular dataset method documentation above. + + +How dataset retrieval and storage works +======================================= + +SciPy dataset files are stored within individual GitHub repositories under the +SciPy GitHub organization, following a naming convention as +``'dataset-'``, for example `scipy.datasets.face` files live at +https://github.com/scipy/dataset-face. The `scipy.datasets` submodule utilizes +and depends on `Pooch `_, a Python +package built to simplify fetching data files. Pooch uses these repos to +retrieve the respective dataset files when calling the dataset function. + +A registry of all the datasets, essentially a mapping of filenames with their +SHA256 hash and repo urls are maintained, which Pooch uses to handle and verify +the downloads on function call. After downloading the dataset once, the files +are saved in the system cache directory under ``'scipy-data'``. + +Dataset cache locations may vary on different platforms. + +For macOS:: + + '~/Library/Caches/scipy-data' + +For Linux and other Unix-like platforms:: + + '~/.cache/scipy-data' # or the value of the XDG_CACHE_HOME env var, if defined + +For Windows:: + + 'C:\\Users\\\\AppData\\Local\\\\scipy-data\\Cache' + + +In environments with constrained network connectivity for various security +reasons or on systems without continuous internet connections, one may manually +load the cache of the datasets by placing the contents of the dataset repo in +the above mentioned cache directory to avoid fetching dataset errors without +the internet connectivity. + +""" + + +from ._fetchers import face, ascent, electrocardiogram +from ._download_all import download_all +from ._utils import clear_cache + +__all__ = ['ascent', 'electrocardiogram', 'face', + 'download_all', 'clear_cache'] + + +from scipy._lib._testutils import PytestTester +test = PytestTester(__name__) +del PytestTester diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/datasets/_download_all.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/datasets/_download_all.py new file mode 100644 index 0000000000000000000000000000000000000000..255fdcaf22950848f458a7ed9ada183e0a2e630e --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/datasets/_download_all.py @@ -0,0 +1,57 @@ +""" +Platform independent script to download all the +`scipy.datasets` module data files. +This doesn't require a full scipy build. + +Run: python _download_all.py +""" + +import argparse +try: + import pooch +except ImportError: + pooch = None + + +if __package__ is None or __package__ == '': + # Running as python script, use absolute import + import _registry # type: ignore +else: + # Running as python module, use relative import + from . import _registry + + +def download_all(path=None): + """ + Utility method to download all the dataset files + for `scipy.datasets` module. + + Parameters + ---------- + path : str, optional + Directory path to download all the dataset files. + If None, default to the system cache_dir detected by pooch. + """ + if pooch is None: + raise ImportError("Missing optional dependency 'pooch' required " + "for scipy.datasets module. Please use pip or " + "conda to install 'pooch'.") + if path is None: + path = pooch.os_cache('scipy-data') + for dataset_name, dataset_hash in _registry.registry.items(): + pooch.retrieve(url=_registry.registry_urls[dataset_name], + known_hash=dataset_hash, + fname=dataset_name, path=path) + + +def main(): + parser = argparse.ArgumentParser(description='Download SciPy data files.') + parser.add_argument("path", nargs='?', type=str, + default=pooch.os_cache('scipy-data'), + help="Directory path to download all the data files.") + args = parser.parse_args() + download_all(args.path) + + +if __name__ == "__main__": + main() diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/datasets/_fetchers.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/datasets/_fetchers.py new file mode 100644 index 0000000000000000000000000000000000000000..57bb2fa6a12e753eb07a1f359ac04a29bd5c77e5 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/datasets/_fetchers.py @@ -0,0 +1,219 @@ +from numpy import array, frombuffer, load +from ._registry import registry, registry_urls + +try: + import pooch +except ImportError: + pooch = None + data_fetcher = None +else: + data_fetcher = pooch.create( + # Use the default cache folder for the operating system + # Pooch uses appdirs (https://github.com/ActiveState/appdirs) to + # select an appropriate directory for the cache on each platform. + path=pooch.os_cache("scipy-data"), + + # The remote data is on Github + # base_url is a required param, even though we override this + # using individual urls in the registry. + base_url="https://github.com/scipy/", + registry=registry, + urls=registry_urls + ) + + +def fetch_data(dataset_name, data_fetcher=data_fetcher): + if data_fetcher is None: + raise ImportError("Missing optional dependency 'pooch' required " + "for scipy.datasets module. Please use pip or " + "conda to install 'pooch'.") + # The "fetch" method returns the full path to the downloaded data file. + return data_fetcher.fetch(dataset_name) + + +def ascent(): + """ + Get an 8-bit grayscale bit-depth, 512 x 512 derived image for easy + use in demos. + + The image is derived from + https://pixnio.com/people/accent-to-the-top + + Parameters + ---------- + None + + Returns + ------- + ascent : ndarray + convenient image to use for testing and demonstration + + Examples + -------- + >>> import scipy.datasets + >>> ascent = scipy.datasets.ascent() + >>> ascent.shape + (512, 512) + >>> ascent.max() + np.uint8(255) + + >>> import matplotlib.pyplot as plt + >>> plt.gray() + >>> plt.imshow(ascent) + >>> plt.show() + + """ + import pickle + + # The file will be downloaded automatically the first time this is run, + # returning the path to the downloaded file. Afterwards, Pooch finds + # it in the local cache and doesn't repeat the download. + fname = fetch_data("ascent.dat") + # Now we just need to load it with our standard Python tools. + with open(fname, 'rb') as f: + ascent = array(pickle.load(f)) + return ascent + + +def electrocardiogram(): + """ + Load an electrocardiogram as an example for a 1-D signal. + + The returned signal is a 5 minute long electrocardiogram (ECG), a medical + recording of the heart's electrical activity, sampled at 360 Hz. + + Returns + ------- + ecg : ndarray + The electrocardiogram in millivolt (mV) sampled at 360 Hz. + + Notes + ----- + The provided signal is an excerpt (19:35 to 24:35) from the `record 208`_ + (lead MLII) provided by the MIT-BIH Arrhythmia Database [1]_ on + PhysioNet [2]_. The excerpt includes noise induced artifacts, typical + heartbeats as well as pathological changes. + + .. _record 208: https://physionet.org/physiobank/database/html/mitdbdir/records.htm#208 + + .. versionadded:: 1.1.0 + + References + ---------- + .. [1] Moody GB, Mark RG. The impact of the MIT-BIH Arrhythmia Database. + IEEE Eng in Med and Biol 20(3):45-50 (May-June 2001). + (PMID: 11446209); :doi:`10.13026/C2F305` + .. [2] Goldberger AL, Amaral LAN, Glass L, Hausdorff JM, Ivanov PCh, + Mark RG, Mietus JE, Moody GB, Peng C-K, Stanley HE. PhysioBank, + PhysioToolkit, and PhysioNet: Components of a New Research Resource + for Complex Physiologic Signals. Circulation 101(23):e215-e220; + :doi:`10.1161/01.CIR.101.23.e215` + + Examples + -------- + >>> from scipy.datasets import electrocardiogram + >>> ecg = electrocardiogram() + >>> ecg + array([-0.245, -0.215, -0.185, ..., -0.405, -0.395, -0.385], shape=(108000,)) + >>> ecg.shape, ecg.mean(), ecg.std() + ((108000,), -0.16510875, 0.5992473991177294) + + As stated the signal features several areas with a different morphology. + E.g., the first few seconds show the electrical activity of a heart in + normal sinus rhythm as seen below. + + >>> import numpy as np + >>> import matplotlib.pyplot as plt + >>> fs = 360 + >>> time = np.arange(ecg.size) / fs + >>> plt.plot(time, ecg) + >>> plt.xlabel("time in s") + >>> plt.ylabel("ECG in mV") + >>> plt.xlim(9, 10.2) + >>> plt.ylim(-1, 1.5) + >>> plt.show() + + After second 16, however, the first premature ventricular contractions, + also called extrasystoles, appear. These have a different morphology + compared to typical heartbeats. The difference can easily be observed + in the following plot. + + >>> plt.plot(time, ecg) + >>> plt.xlabel("time in s") + >>> plt.ylabel("ECG in mV") + >>> plt.xlim(46.5, 50) + >>> plt.ylim(-2, 1.5) + >>> plt.show() + + At several points large artifacts disturb the recording, e.g.: + + >>> plt.plot(time, ecg) + >>> plt.xlabel("time in s") + >>> plt.ylabel("ECG in mV") + >>> plt.xlim(207, 215) + >>> plt.ylim(-2, 3.5) + >>> plt.show() + + Finally, examining the power spectrum reveals that most of the biosignal is + made up of lower frequencies. At 60 Hz the noise induced by the mains + electricity can be clearly observed. + + >>> from scipy.signal import welch + >>> f, Pxx = welch(ecg, fs=fs, nperseg=2048, scaling="spectrum") + >>> plt.semilogy(f, Pxx) + >>> plt.xlabel("Frequency in Hz") + >>> plt.ylabel("Power spectrum of the ECG in mV**2") + >>> plt.xlim(f[[0, -1]]) + >>> plt.show() + """ + fname = fetch_data("ecg.dat") + with load(fname) as file: + ecg = file["ecg"].astype(int) # np.uint16 -> int + # Convert raw output of ADC to mV: (ecg - adc_zero) / adc_gain + ecg = (ecg - 1024) / 200.0 + return ecg + + +def face(gray=False): + """ + Get a 1024 x 768, color image of a raccoon face. + + The image is derived from + https://pixnio.com/fauna-animals/raccoons/raccoon-procyon-lotor + + Parameters + ---------- + gray : bool, optional + If True return 8-bit grey-scale image, otherwise return a color image + + Returns + ------- + face : ndarray + image of a raccoon face + + Examples + -------- + >>> import scipy.datasets + >>> face = scipy.datasets.face() + >>> face.shape + (768, 1024, 3) + >>> face.max() + np.uint8(255) + + >>> import matplotlib.pyplot as plt + >>> plt.gray() + >>> plt.imshow(face) + >>> plt.show() + + """ + import bz2 + fname = fetch_data("face.dat") + with open(fname, 'rb') as f: + rawdata = f.read() + face_data = bz2.decompress(rawdata) + face = frombuffer(face_data, dtype='uint8') + face.shape = (768, 1024, 3) + if gray is True: + face = (0.21 * face[:, :, 0] + 0.71 * face[:, :, 1] + + 0.07 * face[:, :, 2]).astype('uint8') + return face diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/datasets/_registry.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/datasets/_registry.py new file mode 100644 index 0000000000000000000000000000000000000000..969384ad9843159e766100bfa9755aed8102dd09 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/datasets/_registry.py @@ -0,0 +1,26 @@ +########################################################################## +# This file serves as the dataset registry for SciPy Datasets SubModule. +########################################################################## + + +# To generate the SHA256 hash, use the command +# openssl sha256 +registry = { + "ascent.dat": "03ce124c1afc880f87b55f6b061110e2e1e939679184f5614e38dacc6c1957e2", + "ecg.dat": "f20ad3365fb9b7f845d0e5c48b6fe67081377ee466c3a220b7f69f35c8958baf", + "face.dat": "9d8b0b4d081313e2b485748c770472e5a95ed1738146883d84c7030493e82886" +} + +registry_urls = { + "ascent.dat": "https://raw.githubusercontent.com/scipy/dataset-ascent/main/ascent.dat", + "ecg.dat": "https://raw.githubusercontent.com/scipy/dataset-ecg/main/ecg.dat", + "face.dat": "https://raw.githubusercontent.com/scipy/dataset-face/main/face.dat" +} + +# dataset method mapping with their associated filenames +# : ["filename1", "filename2", ...] +method_files_map = { + "ascent": ["ascent.dat"], + "electrocardiogram": ["ecg.dat"], + "face": ["face.dat"] +} diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/datasets/_utils.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/datasets/_utils.py new file mode 100644 index 0000000000000000000000000000000000000000..8f644f8797d6e3256a16ec2c509eec725c726300 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/datasets/_utils.py @@ -0,0 +1,81 @@ +import os +import shutil +from ._registry import method_files_map + +try: + import platformdirs +except ImportError: + platformdirs = None # type: ignore[assignment] + + +def _clear_cache(datasets, cache_dir=None, method_map=None): + if method_map is None: + # Use SciPy Datasets method map + method_map = method_files_map + if cache_dir is None: + # Use default cache_dir path + if platformdirs is None: + # platformdirs is pooch dependency + raise ImportError("Missing optional dependency 'pooch' required " + "for scipy.datasets module. Please use pip or " + "conda to install 'pooch'.") + cache_dir = platformdirs.user_cache_dir("scipy-data") + + if not os.path.exists(cache_dir): + print(f"Cache Directory {cache_dir} doesn't exist. Nothing to clear.") + return + + if datasets is None: + print(f"Cleaning the cache directory {cache_dir}!") + shutil.rmtree(cache_dir) + else: + if not isinstance(datasets, (list, tuple)): + # single dataset method passed should be converted to list + datasets = [datasets, ] + for dataset in datasets: + assert callable(dataset) + dataset_name = dataset.__name__ # Name of the dataset method + if dataset_name not in method_map: + raise ValueError(f"Dataset method {dataset_name} doesn't " + "exist. Please check if the passed dataset " + "is a subset of the following dataset " + f"methods: {list(method_map.keys())}") + + data_files = method_map[dataset_name] + data_filepaths = [os.path.join(cache_dir, file) + for file in data_files] + for data_filepath in data_filepaths: + if os.path.exists(data_filepath): + print("Cleaning the file " + f"{os.path.split(data_filepath)[1]} " + f"for dataset {dataset_name}") + os.remove(data_filepath) + else: + print(f"Path {data_filepath} doesn't exist. " + "Nothing to clear.") + + +def clear_cache(datasets=None): + """ + Cleans the scipy datasets cache directory. + + If a scipy.datasets method or a list/tuple of the same is + provided, then clear_cache removes all the data files + associated to the passed dataset method callable(s). + + By default, it removes all the cached data files. + + Parameters + ---------- + datasets : callable or list/tuple of callable or None + + Examples + -------- + >>> from scipy import datasets + >>> ascent_array = datasets.ascent() + >>> ascent_array.shape + (512, 512) + >>> datasets.clear_cache([datasets.ascent]) + Cleaning the file ascent.dat for dataset ascent + """ + _clear_cache(datasets) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/datasets/tests/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/datasets/tests/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/datasets/tests/test_data.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/datasets/tests/test_data.py new file mode 100644 index 0000000000000000000000000000000000000000..243176bd89b7b6f16406d66293d1872ac2712252 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/datasets/tests/test_data.py @@ -0,0 +1,128 @@ +from scipy.datasets._registry import registry +from scipy.datasets._fetchers import data_fetcher +from scipy.datasets._utils import _clear_cache +from scipy.datasets import ascent, face, electrocardiogram, download_all +from numpy.testing import assert_equal, assert_almost_equal +import os +from threading import get_ident +import pytest + +try: + import pooch +except ImportError: + raise ImportError("Missing optional dependency 'pooch' required " + "for scipy.datasets module. Please use pip or " + "conda to install 'pooch'.") + + +data_dir = data_fetcher.path # type: ignore + + +def _has_hash(path, expected_hash): + """Check if the provided path has the expected hash.""" + if not os.path.exists(path): + return False + return pooch.file_hash(path) == expected_hash + + +class TestDatasets: + + @pytest.fixture(scope='module', autouse=True) + def test_download_all(self): + # This fixture requires INTERNET CONNECTION + + # test_setup phase + download_all() + + yield + + @pytest.mark.fail_slow(10) + def test_existence_all(self): + assert len(os.listdir(data_dir)) >= len(registry) + + def test_ascent(self): + assert_equal(ascent().shape, (512, 512)) + + # hash check + assert _has_hash(os.path.join(data_dir, "ascent.dat"), + registry["ascent.dat"]) + + def test_face(self): + assert_equal(face().shape, (768, 1024, 3)) + + # hash check + assert _has_hash(os.path.join(data_dir, "face.dat"), + registry["face.dat"]) + + def test_electrocardiogram(self): + # Test shape, dtype and stats of signal + ecg = electrocardiogram() + assert_equal(ecg.dtype, float) + assert_equal(ecg.shape, (108000,)) + assert_almost_equal(ecg.mean(), -0.16510875) + assert_almost_equal(ecg.std(), 0.5992473991177294) + + # hash check + assert _has_hash(os.path.join(data_dir, "ecg.dat"), + registry["ecg.dat"]) + + +def test_clear_cache(tmp_path): + # Note: `tmp_path` is a pytest fixture, it handles cleanup + thread_basepath = tmp_path / str(get_ident()) + thread_basepath.mkdir() + + dummy_basepath = thread_basepath / "dummy_cache_dir" + dummy_basepath.mkdir() + + # Create three dummy dataset files for dummy dataset methods + dummy_method_map = {} + for i in range(4): + dummy_method_map[f"data{i}"] = [f"data{i}.dat"] + data_filepath = dummy_basepath / f"data{i}.dat" + data_filepath.write_text("") + + # clear files associated to single dataset method data0 + # also test callable argument instead of list of callables + def data0(): + pass + _clear_cache(datasets=data0, cache_dir=dummy_basepath, + method_map=dummy_method_map) + assert not os.path.exists(dummy_basepath/"data0.dat") + + # clear files associated to multiple dataset methods "data3" and "data4" + def data1(): + pass + + def data2(): + pass + _clear_cache(datasets=[data1, data2], cache_dir=dummy_basepath, + method_map=dummy_method_map) + assert not os.path.exists(dummy_basepath/"data1.dat") + assert not os.path.exists(dummy_basepath/"data2.dat") + + # clear multiple dataset files "data3_0.dat" and "data3_1.dat" + # associated with dataset method "data3" + def data4(): + pass + # create files + (dummy_basepath / "data4_0.dat").write_text("") + (dummy_basepath / "data4_1.dat").write_text("") + + dummy_method_map["data4"] = ["data4_0.dat", "data4_1.dat"] + _clear_cache(datasets=[data4], cache_dir=dummy_basepath, + method_map=dummy_method_map) + assert not os.path.exists(dummy_basepath/"data4_0.dat") + assert not os.path.exists(dummy_basepath/"data4_1.dat") + + # wrong dataset method should raise ValueError since it + # doesn't exist in the dummy_method_map + def data5(): + pass + with pytest.raises(ValueError): + _clear_cache(datasets=[data5], cache_dir=dummy_basepath, + method_map=dummy_method_map) + + # remove all dataset cache + _clear_cache(datasets=None, cache_dir=dummy_basepath) + assert not os.path.exists(dummy_basepath) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/differentiate/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/differentiate/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..c3a7ccc4b33f27dbae7958641a89106cf9580326 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/differentiate/__init__.py @@ -0,0 +1,27 @@ +""" +============================================================== +Finite Difference Differentiation (:mod:`scipy.differentiate`) +============================================================== + +.. currentmodule:: scipy.differentiate + +SciPy ``differentiate`` provides functions for performing finite difference +numerical differentiation of black-box functions. + +.. autosummary:: + :toctree: generated/ + + derivative + jacobian + hessian + +""" + + +from ._differentiate import * + +__all__ = ['derivative', 'jacobian', 'hessian'] + +from scipy._lib._testutils import PytestTester +test = PytestTester(__name__) +del PytestTester diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/differentiate/_differentiate.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/differentiate/_differentiate.py new file mode 100644 index 0000000000000000000000000000000000000000..0e104a071055161b69f62cec317e8a07b4466653 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/differentiate/_differentiate.py @@ -0,0 +1,1129 @@ +# mypy: disable-error-code="attr-defined" +import warnings +import numpy as np +import scipy._lib._elementwise_iterative_method as eim +from scipy._lib._util import _RichResult +from scipy._lib._array_api import array_namespace, xp_sign, xp_copy, xp_take_along_axis + +_EERRORINCREASE = -1 # used in derivative + +def _derivative_iv(f, x, args, tolerances, maxiter, order, initial_step, + step_factor, step_direction, preserve_shape, callback): + # Input validation for `derivative` + xp = array_namespace(x) + + if not callable(f): + raise ValueError('`f` must be callable.') + + if not np.iterable(args): + args = (args,) + + tolerances = {} if tolerances is None else tolerances + atol = tolerances.get('atol', None) + rtol = tolerances.get('rtol', None) + + # tolerances are floats, not arrays; OK to use NumPy + message = 'Tolerances and step parameters must be non-negative scalars.' + tols = np.asarray([atol if atol is not None else 1, + rtol if rtol is not None else 1, + step_factor]) + if (not np.issubdtype(tols.dtype, np.number) or np.any(tols < 0) + or np.any(np.isnan(tols)) or tols.shape != (3,)): + raise ValueError(message) + step_factor = float(tols[2]) + + maxiter_int = int(maxiter) + if maxiter != maxiter_int or maxiter <= 0: + raise ValueError('`maxiter` must be a positive integer.') + + order_int = int(order) + if order_int != order or order <= 0: + raise ValueError('`order` must be a positive integer.') + + step_direction = xp.asarray(step_direction) + initial_step = xp.asarray(initial_step) + temp = xp.broadcast_arrays(x, step_direction, initial_step) + x, step_direction, initial_step = temp + + message = '`preserve_shape` must be True or False.' + if preserve_shape not in {True, False}: + raise ValueError(message) + + if callback is not None and not callable(callback): + raise ValueError('`callback` must be callable.') + + return (f, x, args, atol, rtol, maxiter_int, order_int, initial_step, + step_factor, step_direction, preserve_shape, callback) + + +def derivative(f, x, *, args=(), tolerances=None, maxiter=10, + order=8, initial_step=0.5, step_factor=2.0, + step_direction=0, preserve_shape=False, callback=None): + """Evaluate the derivative of a elementwise, real scalar function numerically. + + For each element of the output of `f`, `derivative` approximates the first + derivative of `f` at the corresponding element of `x` using finite difference + differentiation. + + This function works elementwise when `x`, `step_direction`, and `args` contain + (broadcastable) arrays. + + Parameters + ---------- + f : callable + The function whose derivative is desired. The signature must be:: + + f(xi: ndarray, *argsi) -> ndarray + + where each element of ``xi`` is a finite real number and ``argsi`` is a tuple, + which may contain an arbitrary number of arrays that are broadcastable with + ``xi``. `f` must be an elementwise function: each scalar element ``f(xi)[j]`` + must equal ``f(xi[j])`` for valid indices ``j``. It must not mutate the array + ``xi`` or the arrays in ``argsi``. + x : float array_like + Abscissae at which to evaluate the derivative. Must be broadcastable with + `args` and `step_direction`. + args : tuple of array_like, optional + Additional positional array arguments to be passed to `f`. Arrays + must be broadcastable with one another and the arrays of `init`. + If the callable for which the root is desired requires arguments that are + not broadcastable with `x`, wrap that callable with `f` such that `f` + accepts only `x` and broadcastable ``*args``. + tolerances : dictionary of floats, optional + Absolute and relative tolerances. Valid keys of the dictionary are: + + - ``atol`` - absolute tolerance on the derivative + - ``rtol`` - relative tolerance on the derivative + + Iteration will stop when ``res.error < atol + rtol * abs(res.df)``. The default + `atol` is the smallest normal number of the appropriate dtype, and + the default `rtol` is the square root of the precision of the + appropriate dtype. + order : int, default: 8 + The (positive integer) order of the finite difference formula to be + used. Odd integers will be rounded up to the next even integer. + initial_step : float array_like, default: 0.5 + The (absolute) initial step size for the finite difference derivative + approximation. + step_factor : float, default: 2.0 + The factor by which the step size is *reduced* in each iteration; i.e. + the step size in iteration 1 is ``initial_step/step_factor``. If + ``step_factor < 1``, subsequent steps will be greater than the initial + step; this may be useful if steps smaller than some threshold are + undesirable (e.g. due to subtractive cancellation error). + maxiter : int, default: 10 + The maximum number of iterations of the algorithm to perform. See + Notes. + step_direction : integer array_like + An array representing the direction of the finite difference steps (for + use when `x` lies near to the boundary of the domain of the function.) + Must be broadcastable with `x` and all `args`. + Where 0 (default), central differences are used; where negative (e.g. + -1), steps are non-positive; and where positive (e.g. 1), all steps are + non-negative. + preserve_shape : bool, default: False + In the following, "arguments of `f`" refers to the array ``xi`` and + any arrays within ``argsi``. Let ``shape`` be the broadcasted shape + of `x` and all elements of `args` (which is conceptually + distinct from ``xi` and ``argsi`` passed into `f`). + + - When ``preserve_shape=False`` (default), `f` must accept arguments + of *any* broadcastable shapes. + + - When ``preserve_shape=True``, `f` must accept arguments of shape + ``shape`` *or* ``shape + (n,)``, where ``(n,)`` is the number of + abscissae at which the function is being evaluated. + + In either case, for each scalar element ``xi[j]`` within ``xi``, the array + returned by `f` must include the scalar ``f(xi[j])`` at the same index. + Consequently, the shape of the output is always the shape of the input + ``xi``. + + See Examples. + callback : callable, optional + An optional user-supplied function to be called before the first + iteration and after each iteration. + Called as ``callback(res)``, where ``res`` is a ``_RichResult`` + similar to that returned by `derivative` (but containing the current + iterate's values of all variables). If `callback` raises a + ``StopIteration``, the algorithm will terminate immediately and + `derivative` will return a result. `callback` must not mutate + `res` or its attributes. + + Returns + ------- + res : _RichResult + An object similar to an instance of `scipy.optimize.OptimizeResult` with the + following attributes. The descriptions are written as though the values will + be scalars; however, if `f` returns an array, the outputs will be + arrays of the same shape. + + success : bool array + ``True`` where the algorithm terminated successfully (status ``0``); + ``False`` otherwise. + status : int array + An integer representing the exit status of the algorithm. + + - ``0`` : The algorithm converged to the specified tolerances. + - ``-1`` : The error estimate increased, so iteration was terminated. + - ``-2`` : The maximum number of iterations was reached. + - ``-3`` : A non-finite value was encountered. + - ``-4`` : Iteration was terminated by `callback`. + - ``1`` : The algorithm is proceeding normally (in `callback` only). + + df : float array + The derivative of `f` at `x`, if the algorithm terminated + successfully. + error : float array + An estimate of the error: the magnitude of the difference between + the current estimate of the derivative and the estimate in the + previous iteration. + nit : int array + The number of iterations of the algorithm that were performed. + nfev : int array + The number of points at which `f` was evaluated. + x : float array + The value at which the derivative of `f` was evaluated + (after broadcasting with `args` and `step_direction`). + + See Also + -------- + jacobian, hessian + + Notes + ----- + The implementation was inspired by jacobi [1]_, numdifftools [2]_, and + DERIVEST [3]_, but the implementation follows the theory of Taylor series + more straightforwardly (and arguably naively so). + In the first iteration, the derivative is estimated using a finite + difference formula of order `order` with maximum step size `initial_step`. + Each subsequent iteration, the maximum step size is reduced by + `step_factor`, and the derivative is estimated again until a termination + condition is reached. The error estimate is the magnitude of the difference + between the current derivative approximation and that of the previous + iteration. + + The stencils of the finite difference formulae are designed such that + abscissae are "nested": after `f` is evaluated at ``order + 1`` + points in the first iteration, `f` is evaluated at only two new points + in each subsequent iteration; ``order - 1`` previously evaluated function + values required by the finite difference formula are reused, and two + function values (evaluations at the points furthest from `x`) are unused. + + Step sizes are absolute. When the step size is small relative to the + magnitude of `x`, precision is lost; for example, if `x` is ``1e20``, the + default initial step size of ``0.5`` cannot be resolved. Accordingly, + consider using larger initial step sizes for large magnitudes of `x`. + + The default tolerances are challenging to satisfy at points where the + true derivative is exactly zero. If the derivative may be exactly zero, + consider specifying an absolute tolerance (e.g. ``atol=1e-12``) to + improve convergence. + + References + ---------- + .. [1] Hans Dembinski (@HDembinski). jacobi. + https://github.com/HDembinski/jacobi + .. [2] Per A. Brodtkorb and John D'Errico. numdifftools. + https://numdifftools.readthedocs.io/en/latest/ + .. [3] John D'Errico. DERIVEST: Adaptive Robust Numerical Differentiation. + https://www.mathworks.com/matlabcentral/fileexchange/13490-adaptive-robust-numerical-differentiation + .. [4] Numerical Differentition. Wikipedia. + https://en.wikipedia.org/wiki/Numerical_differentiation + + Examples + -------- + Evaluate the derivative of ``np.exp`` at several points ``x``. + + >>> import numpy as np + >>> from scipy.differentiate import derivative + >>> f = np.exp + >>> df = np.exp # true derivative + >>> x = np.linspace(1, 2, 5) + >>> res = derivative(f, x) + >>> res.df # approximation of the derivative + array([2.71828183, 3.49034296, 4.48168907, 5.75460268, 7.3890561 ]) + >>> res.error # estimate of the error + array([7.13740178e-12, 9.16600129e-12, 1.17594823e-11, 1.51061386e-11, + 1.94262384e-11]) + >>> abs(res.df - df(x)) # true error + array([2.53130850e-14, 3.55271368e-14, 5.77315973e-14, 5.59552404e-14, + 6.92779167e-14]) + + Show the convergence of the approximation as the step size is reduced. + Each iteration, the step size is reduced by `step_factor`, so for + sufficiently small initial step, each iteration reduces the error by a + factor of ``1/step_factor**order`` until finite precision arithmetic + inhibits further improvement. + + >>> import matplotlib.pyplot as plt + >>> iter = list(range(1, 12)) # maximum iterations + >>> hfac = 2 # step size reduction per iteration + >>> hdir = [-1, 0, 1] # compare left-, central-, and right- steps + >>> order = 4 # order of differentiation formula + >>> x = 1 + >>> ref = df(x) + >>> errors = [] # true error + >>> for i in iter: + ... res = derivative(f, x, maxiter=i, step_factor=hfac, + ... step_direction=hdir, order=order, + ... # prevent early termination + ... tolerances=dict(atol=0, rtol=0)) + ... errors.append(abs(res.df - ref)) + >>> errors = np.array(errors) + >>> plt.semilogy(iter, errors[:, 0], label='left differences') + >>> plt.semilogy(iter, errors[:, 1], label='central differences') + >>> plt.semilogy(iter, errors[:, 2], label='right differences') + >>> plt.xlabel('iteration') + >>> plt.ylabel('error') + >>> plt.legend() + >>> plt.show() + >>> (errors[1, 1] / errors[0, 1], 1 / hfac**order) + (0.06215223140159822, 0.0625) + + The implementation is vectorized over `x`, `step_direction`, and `args`. + The function is evaluated once before the first iteration to perform input + validation and standardization, and once per iteration thereafter. + + >>> def f(x, p): + ... f.nit += 1 + ... return x**p + >>> f.nit = 0 + >>> def df(x, p): + ... return p*x**(p-1) + >>> x = np.arange(1, 5) + >>> p = np.arange(1, 6).reshape((-1, 1)) + >>> hdir = np.arange(-1, 2).reshape((-1, 1, 1)) + >>> res = derivative(f, x, args=(p,), step_direction=hdir, maxiter=1) + >>> np.allclose(res.df, df(x, p)) + True + >>> res.df.shape + (3, 5, 4) + >>> f.nit + 2 + + By default, `preserve_shape` is False, and therefore the callable + `f` may be called with arrays of any broadcastable shapes. + For example: + + >>> shapes = [] + >>> def f(x, c): + ... shape = np.broadcast_shapes(x.shape, c.shape) + ... shapes.append(shape) + ... return np.sin(c*x) + >>> + >>> c = [1, 5, 10, 20] + >>> res = derivative(f, 0, args=(c,)) + >>> shapes + [(4,), (4, 8), (4, 2), (3, 2), (2, 2), (1, 2)] + + To understand where these shapes are coming from - and to better + understand how `derivative` computes accurate results - note that + higher values of ``c`` correspond with higher frequency sinusoids. + The higher frequency sinusoids make the function's derivative change + faster, so more function evaluations are required to achieve the target + accuracy: + + >>> res.nfev + array([11, 13, 15, 17], dtype=int32) + + The initial ``shape``, ``(4,)``, corresponds with evaluating the + function at a single abscissa and all four frequencies; this is used + for input validation and to determine the size and dtype of the arrays + that store results. The next shape corresponds with evaluating the + function at an initial grid of abscissae and all four frequencies. + Successive calls to the function evaluate the function at two more + abscissae, increasing the effective order of the approximation by two. + However, in later function evaluations, the function is evaluated at + fewer frequencies because the corresponding derivative has already + converged to the required tolerance. This saves function evaluations to + improve performance, but it requires the function to accept arguments of + any shape. + + "Vector-valued" functions are unlikely to satisfy this requirement. + For example, consider + + >>> def f(x): + ... return [x, np.sin(3*x), x+np.sin(10*x), np.sin(20*x)*(x-1)**2] + + This integrand is not compatible with `derivative` as written; for instance, + the shape of the output will not be the same as the shape of ``x``. Such a + function *could* be converted to a compatible form with the introduction of + additional parameters, but this would be inconvenient. In such cases, + a simpler solution would be to use `preserve_shape`. + + >>> shapes = [] + >>> def f(x): + ... shapes.append(x.shape) + ... x0, x1, x2, x3 = x + ... return [x0, np.sin(3*x1), x2+np.sin(10*x2), np.sin(20*x3)*(x3-1)**2] + >>> + >>> x = np.zeros(4) + >>> res = derivative(f, x, preserve_shape=True) + >>> shapes + [(4,), (4, 8), (4, 2), (4, 2), (4, 2), (4, 2)] + + Here, the shape of ``x`` is ``(4,)``. With ``preserve_shape=True``, the + function may be called with argument ``x`` of shape ``(4,)`` or ``(4, n)``, + and this is what we observe. + + """ + # TODO (followup): + # - investigate behavior at saddle points + # - multivariate functions? + # - relative steps? + # - show example of `np.vectorize` + + res = _derivative_iv(f, x, args, tolerances, maxiter, order, initial_step, + step_factor, step_direction, preserve_shape, callback) + (func, x, args, atol, rtol, maxiter, order, + h0, fac, hdir, preserve_shape, callback) = res + + # Initialization + # Since f(x) (no step) is not needed for central differences, it may be + # possible to eliminate this function evaluation. However, it's useful for + # input validation and standardization, and everything else is designed to + # reduce function calls, so let's keep it simple. + temp = eim._initialize(func, (x,), args, preserve_shape=preserve_shape) + func, xs, fs, args, shape, dtype, xp = temp + + finfo = xp.finfo(dtype) + atol = finfo.smallest_normal if atol is None else atol + rtol = finfo.eps**0.5 if rtol is None else rtol # keep same as `hessian` + + x, f = xs[0], fs[0] + df = xp.full_like(f, xp.nan) + + # Ideally we'd broadcast the shape of `hdir` in `_elementwise_algo_init`, but + # it's simpler to do it here than to generalize `_elementwise_algo_init` further. + # `hdir` and `x` are already broadcasted in `_derivative_iv`, so we know + # that `hdir` can be broadcasted to the final shape. Same with `h0`. + hdir = xp.broadcast_to(hdir, shape) + hdir = xp.reshape(hdir, (-1,)) + hdir = xp.astype(xp_sign(hdir), dtype) + h0 = xp.broadcast_to(h0, shape) + h0 = xp.reshape(h0, (-1,)) + h0 = xp.astype(h0, dtype) + h0[h0 <= 0] = xp.asarray(xp.nan, dtype=dtype) + + status = xp.full_like(x, eim._EINPROGRESS, dtype=xp.int32) # in progress + nit, nfev = 0, 1 # one function evaluations performed above + # Boolean indices of left, central, right, and (all) one-sided steps + il = hdir < 0 + ic = hdir == 0 + ir = hdir > 0 + io = il | ir + + # Most of these attributes are reasonably obvious, but: + # - `fs` holds all the function values of all active `x`. The zeroth + # axis corresponds with active points `x`, the first axis corresponds + # with the different steps (in the order described in + # `_derivative_weights`). + # - `terms` (which could probably use a better name) is half the `order`, + # which is always even. + work = _RichResult(x=x, df=df, fs=f[:, xp.newaxis], error=xp.nan, h=h0, + df_last=xp.nan, error_last=xp.nan, fac=fac, + atol=atol, rtol=rtol, nit=nit, nfev=nfev, + status=status, dtype=dtype, terms=(order+1)//2, + hdir=hdir, il=il, ic=ic, ir=ir, io=io, + # Store the weights in an object so they can't get compressed + # Using RichResult to allow dot notation, but a dict would work + diff_state=_RichResult(central=[], right=[], fac=None)) + + # This is the correspondence between terms in the `work` object and the + # final result. In this case, the mapping is trivial. Note that `success` + # is prepended automatically. + res_work_pairs = [('status', 'status'), ('df', 'df'), ('error', 'error'), + ('nit', 'nit'), ('nfev', 'nfev'), ('x', 'x')] + + def pre_func_eval(work): + """Determine the abscissae at which the function needs to be evaluated. + + See `_derivative_weights` for a description of the stencil (pattern + of the abscissae). + + In the first iteration, there is only one stored function value in + `work.fs`, `f(x)`, so we need to evaluate at `order` new points. In + subsequent iterations, we evaluate at two new points. Note that + `work.x` is always flattened into a 1D array after broadcasting with + all `args`, so we add a new axis at the end and evaluate all point + in one call to the function. + + For improvement: + - Consider measuring the step size actually taken, since ``(x + h) - x`` + is not identically equal to `h` with floating point arithmetic. + - Adjust the step size automatically if `x` is too big to resolve the + step. + - We could probably save some work if there are no central difference + steps or no one-sided steps. + """ + n = work.terms # half the order + h = work.h[:, xp.newaxis] # step size + c = work.fac # step reduction factor + d = c**0.5 # square root of step reduction factor (one-sided stencil) + # Note - no need to be careful about dtypes until we allocate `x_eval` + + if work.nit == 0: + hc = h / c**xp.arange(n, dtype=work.dtype) + hc = xp.concat((-xp.flip(hc, axis=-1), hc), axis=-1) + else: + hc = xp.concat((-h, h), axis=-1) / c**(n-1) + + if work.nit == 0: + hr = h / d**xp.arange(2*n, dtype=work.dtype) + else: + hr = xp.concat((h, h/d), axis=-1) / c**(n-1) + + n_new = 2*n if work.nit == 0 else 2 # number of new abscissae + x_eval = xp.zeros((work.hdir.shape[0], n_new), dtype=work.dtype) + il, ic, ir = work.il, work.ic, work.ir + x_eval[ir] = work.x[ir][:, xp.newaxis] + hr[ir] + x_eval[ic] = work.x[ic][:, xp.newaxis] + hc[ic] + x_eval[il] = work.x[il][:, xp.newaxis] - hr[il] + return x_eval + + def post_func_eval(x, f, work): + """ Estimate the derivative and error from the function evaluations + + As in `pre_func_eval`: in the first iteration, there is only one stored + function value in `work.fs`, `f(x)`, so we need to add the `order` new + points. In subsequent iterations, we add two new points. The tricky + part is getting the order to match that of the weights, which is + described in `_derivative_weights`. + + For improvement: + - Change the order of the weights (and steps in `pre_func_eval`) to + simplify `work_fc` concatenation and eliminate `fc` concatenation. + - It would be simple to do one-step Richardson extrapolation with `df` + and `df_last` to increase the order of the estimate and/or improve + the error estimate. + - Process the function evaluations in a more numerically favorable + way. For instance, combining the pairs of central difference evals + into a second-order approximation and using Richardson extrapolation + to produce a higher order approximation seemed to retain accuracy up + to very high order. + - Alternatively, we could use `polyfit` like Jacobi. An advantage of + fitting polynomial to more points than necessary is improved noise + tolerance. + """ + n = work.terms + n_new = n if work.nit == 0 else 1 + il, ic, io = work.il, work.ic, work.io + + # Central difference + # `work_fc` is *all* the points at which the function has been evaluated + # `fc` is the points we're using *this iteration* to produce the estimate + work_fc = (f[ic][:, :n_new], work.fs[ic], f[ic][:, -n_new:]) + work_fc = xp.concat(work_fc, axis=-1) + if work.nit == 0: + fc = work_fc + else: + fc = (work_fc[:, :n], work_fc[:, n:n+1], work_fc[:, -n:]) + fc = xp.concat(fc, axis=-1) + + # One-sided difference + work_fo = xp.concat((work.fs[io], f[io]), axis=-1) + if work.nit == 0: + fo = work_fo + else: + fo = xp.concat((work_fo[:, 0:1], work_fo[:, -2*n:]), axis=-1) + + work.fs = xp.zeros((ic.shape[0], work.fs.shape[-1] + 2*n_new), dtype=work.dtype) + work.fs[ic] = work_fc + work.fs[io] = work_fo + + wc, wo = _derivative_weights(work, n, xp) + work.df_last = xp.asarray(work.df, copy=True) + work.df[ic] = fc @ wc / work.h[ic] + work.df[io] = fo @ wo / work.h[io] + work.df[il] *= -1 + + work.h /= work.fac + work.error_last = work.error + # Simple error estimate - the difference in derivative estimates between + # this iteration and the last. This is typically conservative because if + # convergence has begin, the true error is much closer to the difference + # between the current estimate and the *next* error estimate. However, + # we could use Richarson extrapolation to produce an error estimate that + # is one order higher, and take the difference between that and + # `work.df` (which would just be constant factor that depends on `fac`.) + work.error = xp.abs(work.df - work.df_last) + + def check_termination(work): + """Terminate due to convergence, non-finite values, or error increase""" + stop = xp.astype(xp.zeros_like(work.df), xp.bool) + + i = work.error < work.atol + work.rtol*abs(work.df) + work.status[i] = eim._ECONVERGED + stop[i] = True + + if work.nit > 0: + i = ~((xp.isfinite(work.x) & xp.isfinite(work.df)) | stop) + work.df[i], work.status[i] = xp.nan, eim._EVALUEERR + stop[i] = True + + # With infinite precision, there is a step size below which + # all smaller step sizes will reduce the error. But in floating point + # arithmetic, catastrophic cancellation will begin to cause the error + # to increase again. This heuristic tries to avoid step sizes that are + # too small. There may be more theoretically sound approaches for + # detecting a step size that minimizes the total error, but this + # heuristic seems simple and effective. + i = (work.error > work.error_last*10) & ~stop + work.status[i] = _EERRORINCREASE + stop[i] = True + + return stop + + def post_termination_check(work): + return + + def customize_result(res, shape): + return shape + + return eim._loop(work, callback, shape, maxiter, func, args, dtype, + pre_func_eval, post_func_eval, check_termination, + post_termination_check, customize_result, res_work_pairs, + xp, preserve_shape) + + +def _derivative_weights(work, n, xp): + # This produces the weights of the finite difference formula for a given + # stencil. In experiments, use of a second-order central difference formula + # with Richardson extrapolation was more accurate numerically, but it was + # more complicated, and it would have become even more complicated when + # adding support for one-sided differences. However, now that all the + # function evaluation values are stored, they can be processed in whatever + # way is desired to produce the derivative estimate. We leave alternative + # approaches to future work. To be more self-contained, here is the theory + # for deriving the weights below. + # + # Recall that the Taylor expansion of a univariate, scalar-values function + # about a point `x` may be expressed as: + # f(x + h) = f(x) + f'(x)*h + f''(x)/2!*h**2 + O(h**3) + # Suppose we evaluate f(x), f(x+h), and f(x-h). We have: + # f(x) = f(x) + # f(x + h) = f(x) + f'(x)*h + f''(x)/2!*h**2 + O(h**3) + # f(x - h) = f(x) - f'(x)*h + f''(x)/2!*h**2 + O(h**3) + # We can solve for weights `wi` such that: + # w1*f(x) = w1*(f(x)) + # + w2*f(x + h) = w2*(f(x) + f'(x)*h + f''(x)/2!*h**2) + O(h**3) + # + w3*f(x - h) = w3*(f(x) - f'(x)*h + f''(x)/2!*h**2) + O(h**3) + # = 0 + f'(x)*h + 0 + O(h**3) + # Then + # f'(x) ~ (w1*f(x) + w2*f(x+h) + w3*f(x-h))/h + # is a finite difference derivative approximation with error O(h**2), + # and so it is said to be a "second-order" approximation. Under certain + # conditions (e.g. well-behaved function, `h` sufficiently small), the + # error in the approximation will decrease with h**2; that is, if `h` is + # reduced by a factor of 2, the error is reduced by a factor of 4. + # + # By default, we use eighth-order formulae. Our central-difference formula + # uses abscissae: + # x-h/c**3, x-h/c**2, x-h/c, x-h, x, x+h, x+h/c, x+h/c**2, x+h/c**3 + # where `c` is the step factor. (Typically, the step factor is greater than + # one, so the outermost points - as written above - are actually closest to + # `x`.) This "stencil" is chosen so that each iteration, the step can be + # reduced by the factor `c`, and most of the function evaluations can be + # reused with the new step size. For example, in the next iteration, we + # will have: + # x-h/c**4, x-h/c**3, x-h/c**2, x-h/c, x, x+h/c, x+h/c**2, x+h/c**3, x+h/c**4 + # We do not reuse `x-h` and `x+h` for the new derivative estimate. + # While this would increase the order of the formula and thus the + # theoretical convergence rate, it is also less stable numerically. + # (As noted above, there are other ways of processing the values that are + # more stable. Thus, even now we store `f(x-h)` and `f(x+h)` in `work.fs` + # to simplify future development of this sort of improvement.) + # + # The (right) one-sided formula is produced similarly using abscissae + # x, x+h, x+h/d, x+h/d**2, ..., x+h/d**6, x+h/d**7, x+h/d**7 + # where `d` is the square root of `c`. (The left one-sided formula simply + # uses -h.) When the step size is reduced by factor `c = d**2`, we have + # abscissae: + # x, x+h/d**2, x+h/d**3..., x+h/d**8, x+h/d**9, x+h/d**9 + # `d` is chosen as the square root of `c` so that the rate of the step-size + # reduction is the same per iteration as in the central difference case. + # Note that because the central difference formulas are inherently of even + # order, for simplicity, we use only even-order formulas for one-sided + # differences, too. + + # It's possible for the user to specify `fac` in, say, double precision but + # `x` and `args` in single precision. `fac` gets converted to single + # precision, but we should always use double precision for the intermediate + # calculations here to avoid additional error in the weights. + fac = float(work.fac) + + # Note that if the user switches back to floating point precision with + # `x` and `args`, then `fac` will not necessarily equal the (lower + # precision) cached `_derivative_weights.fac`, and the weights will + # need to be recalculated. This could be fixed, but it's late, and of + # low consequence. + + diff_state = work.diff_state + if fac != diff_state.fac: + diff_state.central = [] + diff_state.right = [] + diff_state.fac = fac + + if len(diff_state.central) != 2*n + 1: + # Central difference weights. Consider refactoring this; it could + # probably be more compact. + # Note: Using NumPy here is OK; we convert to xp-type at the end + i = np.arange(-n, n + 1) + p = np.abs(i) - 1. # center point has power `p` -1, but sign `s` is 0 + s = np.sign(i) + + h = s / fac ** p + A = np.vander(h, increasing=True).T + b = np.zeros(2*n + 1) + b[1] = 1 + weights = np.linalg.solve(A, b) + + # Enforce identities to improve accuracy + weights[n] = 0 + for i in range(n): + weights[-i-1] = -weights[i] + + # Cache the weights. We only need to calculate them once unless + # the step factor changes. + diff_state.central = weights + + # One-sided difference weights. The left one-sided weights (with + # negative steps) are simply the negative of the right one-sided + # weights, so no need to compute them separately. + i = np.arange(2*n + 1) + p = i - 1. + s = np.sign(i) + + h = s / np.sqrt(fac) ** p + A = np.vander(h, increasing=True).T + b = np.zeros(2 * n + 1) + b[1] = 1 + weights = np.linalg.solve(A, b) + + diff_state.right = weights + + return (xp.asarray(diff_state.central, dtype=work.dtype), + xp.asarray(diff_state.right, dtype=work.dtype)) + + +def jacobian(f, x, *, tolerances=None, maxiter=10, order=8, initial_step=0.5, + step_factor=2.0, step_direction=0): + r"""Evaluate the Jacobian of a function numerically. + + Parameters + ---------- + f : callable + The function whose Jacobian is desired. The signature must be:: + + f(xi: ndarray) -> ndarray + + where each element of ``xi`` is a finite real. If the function to be + differentiated accepts additional arguments, wrap it (e.g. using + `functools.partial` or ``lambda``) and pass the wrapped callable + into `jacobian`. `f` must not mutate the array ``xi``. See Notes + regarding vectorization and the dimensionality of the input and output. + x : float array_like + Points at which to evaluate the Jacobian. Must have at least one dimension. + See Notes regarding the dimensionality and vectorization. + tolerances : dictionary of floats, optional + Absolute and relative tolerances. Valid keys of the dictionary are: + + - ``atol`` - absolute tolerance on the derivative + - ``rtol`` - relative tolerance on the derivative + + Iteration will stop when ``res.error < atol + rtol * abs(res.df)``. The default + `atol` is the smallest normal number of the appropriate dtype, and + the default `rtol` is the square root of the precision of the + appropriate dtype. + maxiter : int, default: 10 + The maximum number of iterations of the algorithm to perform. See + Notes. + order : int, default: 8 + The (positive integer) order of the finite difference formula to be + used. Odd integers will be rounded up to the next even integer. + initial_step : float array_like, default: 0.5 + The (absolute) initial step size for the finite difference derivative + approximation. Must be broadcastable with `x` and `step_direction`. + step_factor : float, default: 2.0 + The factor by which the step size is *reduced* in each iteration; i.e. + the step size in iteration 1 is ``initial_step/step_factor``. If + ``step_factor < 1``, subsequent steps will be greater than the initial + step; this may be useful if steps smaller than some threshold are + undesirable (e.g. due to subtractive cancellation error). + step_direction : integer array_like + An array representing the direction of the finite difference steps (e.g. + for use when `x` lies near to the boundary of the domain of the function.) + Must be broadcastable with `x` and `initial_step`. + Where 0 (default), central differences are used; where negative (e.g. + -1), steps are non-positive; and where positive (e.g. 1), all steps are + non-negative. + + Returns + ------- + res : _RichResult + An object similar to an instance of `scipy.optimize.OptimizeResult` with the + following attributes. The descriptions are written as though the values will + be scalars; however, if `f` returns an array, the outputs will be + arrays of the same shape. + + success : bool array + ``True`` where the algorithm terminated successfully (status ``0``); + ``False`` otherwise. + status : int array + An integer representing the exit status of the algorithm. + + - ``0`` : The algorithm converged to the specified tolerances. + - ``-1`` : The error estimate increased, so iteration was terminated. + - ``-2`` : The maximum number of iterations was reached. + - ``-3`` : A non-finite value was encountered. + + df : float array + The Jacobian of `f` at `x`, if the algorithm terminated + successfully. + error : float array + An estimate of the error: the magnitude of the difference between + the current estimate of the Jacobian and the estimate in the + previous iteration. + nit : int array + The number of iterations of the algorithm that were performed. + nfev : int array + The number of points at which `f` was evaluated. + + Each element of an attribute is associated with the corresponding + element of `df`. For instance, element ``i`` of `nfev` is the + number of points at which `f` was evaluated for the sake of + computing element ``i`` of `df`. + + See Also + -------- + derivative, hessian + + Notes + ----- + Suppose we wish to evaluate the Jacobian of a function + :math:`f: \mathbf{R}^m \rightarrow \mathbf{R}^n`. Assign to variables + ``m`` and ``n`` the positive integer values of :math:`m` and :math:`n`, + respectively, and let ``...`` represent an arbitrary tuple of integers. + If we wish to evaluate the Jacobian at a single point, then: + + - argument `x` must be an array of shape ``(m,)`` + - argument `f` must be vectorized to accept an array of shape ``(m, ...)``. + The first axis represents the :math:`m` inputs of :math:`f`; the remainder + are for evaluating the function at multiple points in a single call. + - argument `f` must return an array of shape ``(n, ...)``. The first + axis represents the :math:`n` outputs of :math:`f`; the remainder + are for the result of evaluating the function at multiple points. + - attribute ``df`` of the result object will be an array of shape ``(n, m)``, + the Jacobian. + + This function is also vectorized in the sense that the Jacobian can be + evaluated at ``k`` points in a single call. In this case, `x` would be an + array of shape ``(m, k)``, `f` would accept an array of shape + ``(m, k, ...)`` and return an array of shape ``(n, k, ...)``, and the ``df`` + attribute of the result would have shape ``(n, m, k)``. + + Suppose the desired callable ``f_not_vectorized`` is not vectorized; it can + only accept an array of shape ``(m,)``. A simple solution to satisfy the required + interface is to wrap ``f_not_vectorized`` as follows:: + + def f(x): + return np.apply_along_axis(f_not_vectorized, axis=0, arr=x) + + Alternatively, suppose the desired callable ``f_vec_q`` is vectorized, but + only for 2-D arrays of shape ``(m, q)``. To satisfy the required interface, + consider:: + + def f(x): + m, batch = x.shape[0], x.shape[1:] # x.shape is (m, ...) + x = np.reshape(x, (m, -1)) # `-1` is short for q = prod(batch) + res = f_vec_q(x) # pass shape (m, q) to function + n = res.shape[0] + return np.reshape(res, (n,) + batch) # return shape (n, ...) + + Then pass the wrapped callable ``f`` as the first argument of `jacobian`. + + References + ---------- + .. [1] Jacobian matrix and determinant, *Wikipedia*, + https://en.wikipedia.org/wiki/Jacobian_matrix_and_determinant + + Examples + -------- + The Rosenbrock function maps from :math:`\mathbf{R}^m \rightarrow \mathbf{R}`; + the SciPy implementation `scipy.optimize.rosen` is vectorized to accept an + array of shape ``(m, p)`` and return an array of shape ``p``. Suppose we wish + to evaluate the Jacobian (AKA the gradient because the function returns a scalar) + at ``[0.5, 0.5, 0.5]``. + + >>> import numpy as np + >>> from scipy.differentiate import jacobian + >>> from scipy.optimize import rosen, rosen_der + >>> m = 3 + >>> x = np.full(m, 0.5) + >>> res = jacobian(rosen, x) + >>> ref = rosen_der(x) # reference value of the gradient + >>> res.df, ref + (array([-51., -1., 50.]), array([-51., -1., 50.])) + + As an example of a function with multiple outputs, consider Example 4 + from [1]_. + + >>> def f(x): + ... x1, x2, x3 = x + ... return [x1, 5*x3, 4*x2**2 - 2*x3, x3*np.sin(x1)] + + The true Jacobian is given by: + + >>> def df(x): + ... x1, x2, x3 = x + ... one = np.ones_like(x1) + ... return [[one, 0*one, 0*one], + ... [0*one, 0*one, 5*one], + ... [0*one, 8*x2, -2*one], + ... [x3*np.cos(x1), 0*one, np.sin(x1)]] + + Evaluate the Jacobian at an arbitrary point. + + >>> rng = np.random.default_rng(389252938452) + >>> x = rng.random(size=3) + >>> res = jacobian(f, x) + >>> ref = df(x) + >>> res.df.shape == (4, 3) + True + >>> np.allclose(res.df, ref) + True + + Evaluate the Jacobian at 10 arbitrary points in a single call. + + >>> x = rng.random(size=(3, 10)) + >>> res = jacobian(f, x) + >>> ref = df(x) + >>> res.df.shape == (4, 3, 10) + True + >>> np.allclose(res.df, ref) + True + + """ + xp = array_namespace(x) + x = xp.asarray(x) + int_dtype = xp.isdtype(x.dtype, 'integral') + x0 = xp.asarray(x, dtype=xp.asarray(1.0).dtype) if int_dtype else x + + if x0.ndim < 1: + message = "Argument `x` must be at least 1-D." + raise ValueError(message) + + m = x0.shape[0] + i = xp.arange(m) + + def wrapped(x): + p = () if x.ndim == x0.ndim else (x.shape[-1],) # number of abscissae + + new_shape = (m, m) + x0.shape[1:] + p + xph = xp.expand_dims(x0, axis=1) + if x.ndim != x0.ndim: + xph = xp.expand_dims(xph, axis=-1) + xph = xp_copy(xp.broadcast_to(xph, new_shape), xp=xp) + xph[i, i] = x + return f(xph) + + res = derivative(wrapped, x, tolerances=tolerances, + maxiter=maxiter, order=order, initial_step=initial_step, + step_factor=step_factor, preserve_shape=True, + step_direction=step_direction) + + del res.x # the user knows `x`, and the way it gets broadcasted is meaningless here + return res + + +def hessian(f, x, *, tolerances=None, maxiter=10, + order=8, initial_step=0.5, step_factor=2.0): + r"""Evaluate the Hessian of a function numerically. + + Parameters + ---------- + f : callable + The function whose Hessian is desired. The signature must be:: + + f(xi: ndarray) -> ndarray + + where each element of ``xi`` is a finite real. If the function to be + differentiated accepts additional arguments, wrap it (e.g. using + `functools.partial` or ``lambda``) and pass the wrapped callable + into `hessian`. `f` must not mutate the array ``xi``. See Notes + regarding vectorization and the dimensionality of the input and output. + x : float array_like + Points at which to evaluate the Hessian. Must have at least one dimension. + See Notes regarding the dimensionality and vectorization. + tolerances : dictionary of floats, optional + Absolute and relative tolerances. Valid keys of the dictionary are: + + - ``atol`` - absolute tolerance on the derivative + - ``rtol`` - relative tolerance on the derivative + + Iteration will stop when ``res.error < atol + rtol * abs(res.df)``. The default + `atol` is the smallest normal number of the appropriate dtype, and + the default `rtol` is the square root of the precision of the + appropriate dtype. + order : int, default: 8 + The (positive integer) order of the finite difference formula to be + used. Odd integers will be rounded up to the next even integer. + initial_step : float, default: 0.5 + The (absolute) initial step size for the finite difference derivative + approximation. + step_factor : float, default: 2.0 + The factor by which the step size is *reduced* in each iteration; i.e. + the step size in iteration 1 is ``initial_step/step_factor``. If + ``step_factor < 1``, subsequent steps will be greater than the initial + step; this may be useful if steps smaller than some threshold are + undesirable (e.g. due to subtractive cancellation error). + maxiter : int, default: 10 + The maximum number of iterations of the algorithm to perform. See + Notes. + + Returns + ------- + res : _RichResult + An object similar to an instance of `scipy.optimize.OptimizeResult` with the + following attributes. The descriptions are written as though the values will + be scalars; however, if `f` returns an array, the outputs will be + arrays of the same shape. + + success : bool array + ``True`` where the algorithm terminated successfully (status ``0``); + ``False`` otherwise. + status : int array + An integer representing the exit status of the algorithm. + + - ``0`` : The algorithm converged to the specified tolerances. + - ``-1`` : The error estimate increased, so iteration was terminated. + - ``-2`` : The maximum number of iterations was reached. + - ``-3`` : A non-finite value was encountered. + + ddf : float array + The Hessian of `f` at `x`, if the algorithm terminated + successfully. + error : float array + An estimate of the error: the magnitude of the difference between + the current estimate of the Hessian and the estimate in the + previous iteration. + nfev : int array + The number of points at which `f` was evaluated. + + Each element of an attribute is associated with the corresponding + element of `ddf`. For instance, element ``[i, j]`` of `nfev` is the + number of points at which `f` was evaluated for the sake of + computing element ``[i, j]`` of `ddf`. + + See Also + -------- + derivative, jacobian + + Notes + ----- + Suppose we wish to evaluate the Hessian of a function + :math:`f: \mathbf{R}^m \rightarrow \mathbf{R}`, and we assign to variable + ``m`` the positive integer value of :math:`m`. If we wish to evaluate + the Hessian at a single point, then: + + - argument `x` must be an array of shape ``(m,)`` + - argument `f` must be vectorized to accept an array of shape + ``(m, ...)``. The first axis represents the :math:`m` inputs of + :math:`f`; the remaining axes indicated by ellipses are for evaluating + the function at several abscissae in a single call. + - argument `f` must return an array of shape ``(...)``. + - attribute ``dff`` of the result object will be an array of shape ``(m, m)``, + the Hessian. + + This function is also vectorized in the sense that the Hessian can be + evaluated at ``k`` points in a single call. In this case, `x` would be an + array of shape ``(m, k)``, `f` would accept an array of shape + ``(m, ...)`` and return an array of shape ``(...)``, and the ``ddf`` + attribute of the result would have shape ``(m, m, k)``. Note that the + axis associated with the ``k`` points is included within the axes + denoted by ``(...)``. + + Currently, `hessian` is implemented by nesting calls to `jacobian`. + All options passed to `hessian` are used for both the inner and outer + calls with one exception: the `rtol` used in the inner `jacobian` call + is tightened by a factor of 100 with the expectation that the inner + error can be ignored. A consequence is that `rtol` should not be set + less than 100 times the precision of the dtype of `x`; a warning is + emitted otherwise. + + References + ---------- + .. [1] Hessian matrix, *Wikipedia*, + https://en.wikipedia.org/wiki/Hessian_matrix + + Examples + -------- + The Rosenbrock function maps from :math:`\mathbf{R}^m \rightarrow \mathbf{R}`; + the SciPy implementation `scipy.optimize.rosen` is vectorized to accept an + array of shape ``(m, ...)`` and return an array of shape ``...``. Suppose we + wish to evaluate the Hessian at ``[0.5, 0.5, 0.5]``. + + >>> import numpy as np + >>> from scipy.differentiate import hessian + >>> from scipy.optimize import rosen, rosen_hess + >>> m = 3 + >>> x = np.full(m, 0.5) + >>> res = hessian(rosen, x) + >>> ref = rosen_hess(x) # reference value of the Hessian + >>> np.allclose(res.ddf, ref) + True + + `hessian` is vectorized to evaluate the Hessian at multiple points + in a single call. + + >>> rng = np.random.default_rng(4589245925010) + >>> x = rng.random((m, 10)) + >>> res = hessian(rosen, x) + >>> ref = [rosen_hess(xi) for xi in x.T] + >>> ref = np.moveaxis(ref, 0, -1) + >>> np.allclose(res.ddf, ref) + True + + """ + # todo: + # - add ability to vectorize over additional parameters (*args?) + # - error estimate stack with inner jacobian (or use legit 2D stencil) + + kwargs = dict(maxiter=maxiter, order=order, initial_step=initial_step, + step_factor=step_factor) + tolerances = {} if tolerances is None else tolerances + atol = tolerances.get('atol', None) + rtol = tolerances.get('rtol', None) + + xp = array_namespace(x) + x = xp.asarray(x) + dtype = x.dtype if not xp.isdtype(x.dtype, 'integral') else xp.asarray(1.).dtype + finfo = xp.finfo(dtype) + rtol = finfo.eps**0.5 if rtol is None else rtol # keep same as `derivative` + + # tighten the inner tolerance to make the inner error negligible + rtol_min = finfo.eps * 100 + message = (f"The specified `{rtol=}`, but error estimates are likely to be " + f"unreliable when `rtol < {rtol_min}`.") + if 0 < rtol < rtol_min: # rtol <= 0 is an error + warnings.warn(message, RuntimeWarning, stacklevel=2) + rtol = rtol_min + + def df(x): + tolerances = dict(rtol=rtol/100, atol=atol) + temp = jacobian(f, x, tolerances=tolerances, **kwargs) + nfev.append(temp.nfev if len(nfev) == 0 else temp.nfev.sum(axis=-1)) + return temp.df + + nfev = [] # track inner function evaluations + res = jacobian(df, x, tolerances=tolerances, **kwargs) # jacobian of jacobian + + nfev = xp.cumulative_sum(xp.stack(nfev), axis=0) + res_nit = xp.astype(res.nit[xp.newaxis, ...], xp.int64) # appease torch + res.nfev = xp_take_along_axis(nfev, res_nit, axis=0)[0] + res.ddf = res.df + del res.df # this is renamed to ddf + del res.nit # this is only the outer-jacobian nit + + return res diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/differentiate/tests/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/differentiate/tests/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/differentiate/tests/test_differentiate.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/differentiate/tests/test_differentiate.py new file mode 100644 index 0000000000000000000000000000000000000000..64bc8193cc237465e9427300bedfac8712963e4c --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/differentiate/tests/test_differentiate.py @@ -0,0 +1,695 @@ +import math +import pytest + +import numpy as np + +from scipy.conftest import array_api_compatible +import scipy._lib._elementwise_iterative_method as eim +from scipy._lib._array_api_no_0d import xp_assert_close, xp_assert_equal, xp_assert_less +from scipy._lib._array_api import is_numpy, is_torch, array_namespace + +from scipy import stats, optimize, special +from scipy.differentiate import derivative, jacobian, hessian +from scipy.differentiate._differentiate import _EERRORINCREASE + + +pytestmark = [array_api_compatible, pytest.mark.usefixtures("skip_xp_backends")] + +array_api_strict_skip_reason = 'Array API does not support fancy indexing assignment.' +jax_skip_reason = 'JAX arrays do not support item assignment.' + + +@pytest.mark.skip_xp_backends('array_api_strict', reason=array_api_strict_skip_reason) +@pytest.mark.skip_xp_backends('jax.numpy',reason=jax_skip_reason) +class TestDerivative: + + def f(self, x): + return special.ndtr(x) + + @pytest.mark.parametrize('x', [0.6, np.linspace(-0.05, 1.05, 10)]) + def test_basic(self, x, xp): + # Invert distribution CDF and compare against distribution `ppf` + default_dtype = xp.asarray(1.).dtype + res = derivative(self.f, xp.asarray(x, dtype=default_dtype)) + ref = xp.asarray(stats.norm().pdf(x), dtype=default_dtype) + xp_assert_close(res.df, ref) + # This would be nice, but doesn't always work out. `error` is an + # estimate, not a bound. + if not is_torch(xp): + xp_assert_less(xp.abs(res.df - ref), res.error) + + @pytest.mark.skip_xp_backends(np_only=True) + @pytest.mark.parametrize('case', stats._distr_params.distcont) + def test_accuracy(self, case): + distname, params = case + dist = getattr(stats, distname)(*params) + x = dist.median() + 0.1 + res = derivative(dist.cdf, x) + ref = dist.pdf(x) + xp_assert_close(res.df, ref, atol=1e-10) + + @pytest.mark.parametrize('order', [1, 6]) + @pytest.mark.parametrize('shape', [tuple(), (12,), (3, 4), (3, 2, 2)]) + def test_vectorization(self, order, shape, xp): + # Test for correct functionality, output shapes, and dtypes for various + # input shapes. + x = np.linspace(-0.05, 1.05, 12).reshape(shape) if shape else 0.6 + n = np.size(x) + state = {} + + @np.vectorize + def _derivative_single(x): + return derivative(self.f, x, order=order) + + def f(x, *args, **kwargs): + state['nit'] += 1 + state['feval'] += 1 if (x.size == n or x.ndim <=1) else x.shape[-1] + return self.f(x, *args, **kwargs) + + state['nit'] = -1 + state['feval'] = 0 + + res = derivative(f, xp.asarray(x, dtype=xp.float64), order=order) + refs = _derivative_single(x).ravel() + + ref_x = [ref.x for ref in refs] + xp_assert_close(xp.reshape(res.x, (-1,)), xp.asarray(ref_x)) + + ref_df = [ref.df for ref in refs] + xp_assert_close(xp.reshape(res.df, (-1,)), xp.asarray(ref_df)) + + ref_error = [ref.error for ref in refs] + xp_assert_close(xp.reshape(res.error, (-1,)), xp.asarray(ref_error), + atol=1e-12) + + ref_success = [bool(ref.success) for ref in refs] + xp_assert_equal(xp.reshape(res.success, (-1,)), xp.asarray(ref_success)) + + ref_flag = [np.int32(ref.status) for ref in refs] + xp_assert_equal(xp.reshape(res.status, (-1,)), xp.asarray(ref_flag)) + + ref_nfev = [np.int32(ref.nfev) for ref in refs] + xp_assert_equal(xp.reshape(res.nfev, (-1,)), xp.asarray(ref_nfev)) + if is_numpy(xp): # can't expect other backends to be exactly the same + assert xp.max(res.nfev) == state['feval'] + + ref_nit = [np.int32(ref.nit) for ref in refs] + xp_assert_equal(xp.reshape(res.nit, (-1,)), xp.asarray(ref_nit)) + if is_numpy(xp): # can't expect other backends to be exactly the same + assert xp.max(res.nit) == state['nit'] + + def test_flags(self, xp): + # Test cases that should produce different status flags; show that all + # can be produced simultaneously. + rng = np.random.default_rng(5651219684984213) + def f(xs, js): + f.nit += 1 + funcs = [lambda x: x - 2.5, # converges + lambda x: xp.exp(x)*rng.random(), # error increases + lambda x: xp.exp(x), # reaches maxiter due to order=2 + lambda x: xp.full_like(x, xp.nan)] # stops due to NaN + res = [funcs[int(j)](x) for x, j in zip(xs, xp.reshape(js, (-1,)))] + return xp.stack(res) + f.nit = 0 + + args = (xp.arange(4, dtype=xp.int64),) + res = derivative(f, xp.ones(4, dtype=xp.float64), + tolerances=dict(rtol=1e-14), + order=2, args=args) + + ref_flags = xp.asarray([eim._ECONVERGED, + _EERRORINCREASE, + eim._ECONVERR, + eim._EVALUEERR], dtype=xp.int32) + xp_assert_equal(res.status, ref_flags) + + def test_flags_preserve_shape(self, xp): + # Same test as above but using `preserve_shape` option to simplify. + rng = np.random.default_rng(5651219684984213) + def f(x): + out = [x - 2.5, # converges + xp.exp(x)*rng.random(), # error increases + xp.exp(x), # reaches maxiter due to order=2 + xp.full_like(x, xp.nan)] # stops due to NaN + return xp.stack(out) + + res = derivative(f, xp.asarray(1, dtype=xp.float64), + tolerances=dict(rtol=1e-14), + order=2, preserve_shape=True) + + ref_flags = xp.asarray([eim._ECONVERGED, + _EERRORINCREASE, + eim._ECONVERR, + eim._EVALUEERR], dtype=xp.int32) + xp_assert_equal(res.status, ref_flags) + + def test_preserve_shape(self, xp): + # Test `preserve_shape` option + def f(x): + out = [x, xp.sin(3*x), x+xp.sin(10*x), xp.sin(20*x)*(x-1)**2] + return xp.stack(out) + + x = xp.asarray(0.) + ref = xp.asarray([xp.asarray(1), 3*xp.cos(3*x), 1+10*xp.cos(10*x), + 20*xp.cos(20*x)*(x-1)**2 + 2*xp.sin(20*x)*(x-1)]) + res = derivative(f, x, preserve_shape=True) + xp_assert_close(res.df, ref) + + def test_convergence(self, xp): + # Test that the convergence tolerances behave as expected + x = xp.asarray(1., dtype=xp.float64) + f = special.ndtr + ref = float(stats.norm.pdf(1.)) + tolerances0 = dict(atol=0, rtol=0) + + tolerances = tolerances0.copy() + tolerances['atol'] = 1e-3 + res1 = derivative(f, x, tolerances=tolerances, order=4) + assert abs(res1.df - ref) < 1e-3 + tolerances['atol'] = 1e-6 + res2 = derivative(f, x, tolerances=tolerances, order=4) + assert abs(res2.df - ref) < 1e-6 + assert abs(res2.df - ref) < abs(res1.df - ref) + + tolerances = tolerances0.copy() + tolerances['rtol'] = 1e-3 + res1 = derivative(f, x, tolerances=tolerances, order=4) + assert abs(res1.df - ref) < 1e-3 * ref + tolerances['rtol'] = 1e-6 + res2 = derivative(f, x, tolerances=tolerances, order=4) + assert abs(res2.df - ref) < 1e-6 * ref + assert abs(res2.df - ref) < abs(res1.df - ref) + + def test_step_parameters(self, xp): + # Test that step factors have the expected effect on accuracy + x = xp.asarray(1., dtype=xp.float64) + f = special.ndtr + ref = float(stats.norm.pdf(1.)) + + res1 = derivative(f, x, initial_step=0.5, maxiter=1) + res2 = derivative(f, x, initial_step=0.05, maxiter=1) + assert abs(res2.df - ref) < abs(res1.df - ref) + + res1 = derivative(f, x, step_factor=2, maxiter=1) + res2 = derivative(f, x, step_factor=20, maxiter=1) + assert abs(res2.df - ref) < abs(res1.df - ref) + + # `step_factor` can be less than 1: `initial_step` is the minimum step + kwargs = dict(order=4, maxiter=1, step_direction=0) + res = derivative(f, x, initial_step=0.5, step_factor=0.5, **kwargs) + ref = derivative(f, x, initial_step=1, step_factor=2, **kwargs) + xp_assert_close(res.df, ref.df, rtol=5e-15) + + # This is a similar test for one-sided difference + kwargs = dict(order=2, maxiter=1, step_direction=1) + res = derivative(f, x, initial_step=1, step_factor=2, **kwargs) + ref = derivative(f, x, initial_step=1/np.sqrt(2), step_factor=0.5, **kwargs) + xp_assert_close(res.df, ref.df, rtol=5e-15) + + kwargs['step_direction'] = -1 + res = derivative(f, x, initial_step=1, step_factor=2, **kwargs) + ref = derivative(f, x, initial_step=1/np.sqrt(2), step_factor=0.5, **kwargs) + xp_assert_close(res.df, ref.df, rtol=5e-15) + + def test_step_direction(self, xp): + # test that `step_direction` works as expected + def f(x): + y = xp.exp(x) + y[(x < 0) + (x > 2)] = xp.nan + return y + + x = xp.linspace(0, 2, 10) + step_direction = xp.zeros_like(x) + step_direction[x < 0.6], step_direction[x > 1.4] = 1, -1 + res = derivative(f, x, step_direction=step_direction) + xp_assert_close(res.df, xp.exp(x)) + assert xp.all(res.success) + + def test_vectorized_step_direction_args(self, xp): + # test that `step_direction` and `args` are vectorized properly + def f(x, p): + return x ** p + + def df(x, p): + return p * x ** (p - 1) + + x = xp.reshape(xp.asarray([1, 2, 3, 4]), (-1, 1, 1)) + hdir = xp.reshape(xp.asarray([-1, 0, 1]), (1, -1, 1)) + p = xp.reshape(xp.asarray([2, 3]), (1, 1, -1)) + res = derivative(f, x, step_direction=hdir, args=(p,)) + ref = xp.broadcast_to(df(x, p), res.df.shape) + ref = xp.asarray(ref, dtype=xp.asarray(1.).dtype) + xp_assert_close(res.df, ref) + + def test_initial_step(self, xp): + # Test that `initial_step` works as expected and is vectorized + def f(x): + return xp.exp(x) + + x = xp.asarray(0., dtype=xp.float64) + step_direction = xp.asarray([-1, 0, 1]) + h0 = xp.reshape(xp.logspace(-3, 0, 10), (-1, 1)) + res = derivative(f, x, initial_step=h0, order=2, maxiter=1, + step_direction=step_direction) + err = xp.abs(res.df - f(x)) + + # error should be smaller for smaller step sizes + assert xp.all(err[:-1, ...] < err[1:, ...]) + + # results of vectorized call should match results with + # initial_step taken one at a time + for i in range(h0.shape[0]): + ref = derivative(f, x, initial_step=h0[i, 0], order=2, maxiter=1, + step_direction=step_direction) + xp_assert_close(res.df[i, :], ref.df, rtol=1e-14) + + def test_maxiter_callback(self, xp): + # Test behavior of `maxiter` parameter and `callback` interface + x = xp.asarray(0.612814, dtype=xp.float64) + maxiter = 3 + + def f(x): + res = special.ndtr(x) + return res + + default_order = 8 + res = derivative(f, x, maxiter=maxiter, tolerances=dict(rtol=1e-15)) + assert not xp.any(res.success) + assert xp.all(res.nfev == default_order + 1 + (maxiter - 1)*2) + assert xp.all(res.nit == maxiter) + + def callback(res): + callback.iter += 1 + callback.res = res + assert hasattr(res, 'x') + assert float(res.df) not in callback.dfs + callback.dfs.add(float(res.df)) + assert res.status == eim._EINPROGRESS + if callback.iter == maxiter: + raise StopIteration + callback.iter = -1 # callback called once before first iteration + callback.res = None + callback.dfs = set() + + res2 = derivative(f, x, callback=callback, tolerances=dict(rtol=1e-15)) + # terminating with callback is identical to terminating due to maxiter + # (except for `status`) + for key in res.keys(): + if key == 'status': + assert res[key] == eim._ECONVERR + assert res2[key] == eim._ECALLBACK + else: + assert res2[key] == callback.res[key] == res[key] + + @pytest.mark.parametrize("hdir", (-1, 0, 1)) + @pytest.mark.parametrize("x", (0.65, [0.65, 0.7])) + @pytest.mark.parametrize("dtype", ('float16', 'float32', 'float64')) + def test_dtype(self, hdir, x, dtype, xp): + if dtype == 'float16' and not is_numpy(xp): + pytest.skip('float16 not tested for alternative backends') + + # Test that dtypes are preserved + dtype = getattr(xp, dtype) + x = xp.asarray(x, dtype=dtype) + + def f(x): + assert x.dtype == dtype + return xp.exp(x) + + def callback(res): + assert res.x.dtype == dtype + assert res.df.dtype == dtype + assert res.error.dtype == dtype + + res = derivative(f, x, order=4, step_direction=hdir, callback=callback) + assert res.x.dtype == dtype + assert res.df.dtype == dtype + assert res.error.dtype == dtype + eps = xp.finfo(dtype).eps + # not sure why torch is less accurate here; might be worth investigating + rtol = eps**0.5 * 50 if is_torch(xp) else eps**0.5 + xp_assert_close(res.df, xp.exp(res.x), rtol=rtol) + + def test_input_validation(self, xp): + # Test input validation for appropriate error messages + one = xp.asarray(1) + + message = '`f` must be callable.' + with pytest.raises(ValueError, match=message): + derivative(None, one) + + message = 'Abscissae and function output must be real numbers.' + with pytest.raises(ValueError, match=message): + derivative(lambda x: x, xp.asarray(-4+1j)) + + message = "When `preserve_shape=False`, the shape of the array..." + with pytest.raises(ValueError, match=message): + derivative(lambda x: [1, 2, 3], xp.asarray([-2, -3])) + + message = 'Tolerances and step parameters must be non-negative...' + with pytest.raises(ValueError, match=message): + derivative(lambda x: x, one, tolerances=dict(atol=-1)) + with pytest.raises(ValueError, match=message): + derivative(lambda x: x, one, tolerances=dict(rtol='ekki')) + with pytest.raises(ValueError, match=message): + derivative(lambda x: x, one, step_factor=object()) + + message = '`maxiter` must be a positive integer.' + with pytest.raises(ValueError, match=message): + derivative(lambda x: x, one, maxiter=1.5) + with pytest.raises(ValueError, match=message): + derivative(lambda x: x, one, maxiter=0) + + message = '`order` must be a positive integer' + with pytest.raises(ValueError, match=message): + derivative(lambda x: x, one, order=1.5) + with pytest.raises(ValueError, match=message): + derivative(lambda x: x, one, order=0) + + message = '`preserve_shape` must be True or False.' + with pytest.raises(ValueError, match=message): + derivative(lambda x: x, one, preserve_shape='herring') + + message = '`callback` must be callable.' + with pytest.raises(ValueError, match=message): + derivative(lambda x: x, one, callback='shrubbery') + + def test_special_cases(self, xp): + # Test edge cases and other special cases + + # Test that integers are not passed to `f` + # (otherwise this would overflow) + def f(x): + xp_test = array_namespace(x) # needs `isdtype` + assert xp_test.isdtype(x.dtype, 'real floating') + return x ** 99 - 1 + + if not is_torch(xp): # torch defaults to float32 + res = derivative(f, xp.asarray(7), tolerances=dict(rtol=1e-10)) + assert res.success + xp_assert_close(res.df, xp.asarray(99*7.**98)) + + # Test invalid step size and direction + res = derivative(xp.exp, xp.asarray(1), step_direction=xp.nan) + xp_assert_equal(res.df, xp.asarray(xp.nan)) + xp_assert_equal(res.status, xp.asarray(-3, dtype=xp.int32)) + + res = derivative(xp.exp, xp.asarray(1), initial_step=0) + xp_assert_equal(res.df, xp.asarray(xp.nan)) + xp_assert_equal(res.status, xp.asarray(-3, dtype=xp.int32)) + + # Test that if success is achieved in the correct number + # of iterations if function is a polynomial. Ideally, all polynomials + # of order 0-2 would get exact result with 0 refinement iterations, + # all polynomials of order 3-4 would be differentiated exactly after + # 1 iteration, etc. However, it seems that `derivative` needs an + # extra iteration to detect convergence based on the error estimate. + + for n in range(6): + x = xp.asarray(1.5, dtype=xp.float64) + def f(x): + return 2*x**n + + ref = 2*n*x**(n-1) + + res = derivative(f, x, maxiter=1, order=max(1, n)) + xp_assert_close(res.df, ref, rtol=1e-15) + xp_assert_equal(res.error, xp.asarray(xp.nan, dtype=xp.float64)) + + res = derivative(f, x, order=max(1, n)) + assert res.success + assert res.nit == 2 + xp_assert_close(res.df, ref, rtol=1e-15) + + # Test scalar `args` (not in tuple) + def f(x, c): + return c*x - 1 + + res = derivative(f, xp.asarray(2), args=xp.asarray(3)) + xp_assert_close(res.df, xp.asarray(3.)) + + # no need to run a test on multiple backends if it's xfailed + @pytest.mark.skip_xp_backends(np_only=True) + @pytest.mark.xfail + @pytest.mark.parametrize("case", ( # function, evaluation point + (lambda x: (x - 1) ** 3, 1), + (lambda x: np.where(x > 1, (x - 1) ** 5, (x - 1) ** 3), 1) + )) + def test_saddle_gh18811(self, case): + # With default settings, `derivative` will not always converge when + # the true derivative is exactly zero. This tests that specifying a + # (tight) `atol` alleviates the problem. See discussion in gh-18811. + atol = 1e-16 + res = derivative(*case, step_direction=[-1, 0, 1], atol=atol) + assert np.all(res.success) + xp_assert_close(res.df, 0, atol=atol) + + +class JacobianHessianTest: + def test_iv(self, xp): + jh_func = self.jh_func.__func__ + + # Test input validation + message = "Argument `x` must be at least 1-D." + with pytest.raises(ValueError, match=message): + jh_func(xp.sin, 1, tolerances=dict(atol=-1)) + + # Confirm that other parameters are being passed to `derivative`, + # which raises an appropriate error message. + x = xp.ones(3) + func = optimize.rosen + message = 'Tolerances and step parameters must be non-negative scalars.' + with pytest.raises(ValueError, match=message): + jh_func(func, x, tolerances=dict(atol=-1)) + with pytest.raises(ValueError, match=message): + jh_func(func, x, tolerances=dict(rtol=-1)) + with pytest.raises(ValueError, match=message): + jh_func(func, x, step_factor=-1) + + message = '`order` must be a positive integer.' + with pytest.raises(ValueError, match=message): + jh_func(func, x, order=-1) + + message = '`maxiter` must be a positive integer.' + with pytest.raises(ValueError, match=message): + jh_func(func, x, maxiter=-1) + + +@pytest.mark.skip_xp_backends('array_api_strict', reason=array_api_strict_skip_reason) +@pytest.mark.skip_xp_backends('jax.numpy',reason=jax_skip_reason) +class TestJacobian(JacobianHessianTest): + jh_func = jacobian + + # Example functions and Jacobians from Wikipedia: + # https://en.wikipedia.org/wiki/Jacobian_matrix_and_determinant#Examples + + def f1(z, xp): + x, y = z + return xp.stack([x ** 2 * y, 5 * x + xp.sin(y)]) + + def df1(z): + x, y = z + return [[2 * x * y, x ** 2], [np.full_like(x, 5), np.cos(y)]] + + f1.mn = 2, 2 # type: ignore[attr-defined] + f1.ref = df1 # type: ignore[attr-defined] + + def f2(z, xp): + r, phi = z + return xp.stack([r * xp.cos(phi), r * xp.sin(phi)]) + + def df2(z): + r, phi = z + return [[np.cos(phi), -r * np.sin(phi)], + [np.sin(phi), r * np.cos(phi)]] + + f2.mn = 2, 2 # type: ignore[attr-defined] + f2.ref = df2 # type: ignore[attr-defined] + + def f3(z, xp): + r, phi, th = z + return xp.stack([r * xp.sin(phi) * xp.cos(th), r * xp.sin(phi) * xp.sin(th), + r * xp.cos(phi)]) + + def df3(z): + r, phi, th = z + return [[np.sin(phi) * np.cos(th), r * np.cos(phi) * np.cos(th), + -r * np.sin(phi) * np.sin(th)], + [np.sin(phi) * np.sin(th), r * np.cos(phi) * np.sin(th), + r * np.sin(phi) * np.cos(th)], + [np.cos(phi), -r * np.sin(phi), np.zeros_like(r)]] + + f3.mn = 3, 3 # type: ignore[attr-defined] + f3.ref = df3 # type: ignore[attr-defined] + + def f4(x, xp): + x1, x2, x3 = x + return xp.stack([x1, 5 * x3, 4 * x2 ** 2 - 2 * x3, x3 * xp.sin(x1)]) + + def df4(x): + x1, x2, x3 = x + one = np.ones_like(x1) + return [[one, 0 * one, 0 * one], + [0 * one, 0 * one, 5 * one], + [0 * one, 8 * x2, -2 * one], + [x3 * np.cos(x1), 0 * one, np.sin(x1)]] + + f4.mn = 3, 4 # type: ignore[attr-defined] + f4.ref = df4 # type: ignore[attr-defined] + + def f5(x, xp): + x1, x2, x3 = x + return xp.stack([5 * x2, 4 * x1 ** 2 - 2 * xp.sin(x2 * x3), x2 * x3]) + + def df5(x): + x1, x2, x3 = x + one = np.ones_like(x1) + return [[0 * one, 5 * one, 0 * one], + [8 * x1, -2 * x3 * np.cos(x2 * x3), -2 * x2 * np.cos(x2 * x3)], + [0 * one, x3, x2]] + + f5.mn = 3, 3 # type: ignore[attr-defined] + f5.ref = df5 # type: ignore[attr-defined] + + def rosen(x, _): return optimize.rosen(x) + rosen.mn = 5, 1 # type: ignore[attr-defined] + rosen.ref = optimize.rosen_der # type: ignore[attr-defined] + + @pytest.mark.parametrize('dtype', ('float32', 'float64')) + @pytest.mark.parametrize('size', [(), (6,), (2, 3)]) + @pytest.mark.parametrize('func', [f1, f2, f3, f4, f5, rosen]) + def test_examples(self, dtype, size, func, xp): + atol = 1e-10 if dtype == 'float64' else 1.99e-3 + dtype = getattr(xp, dtype) + rng = np.random.default_rng(458912319542) + m, n = func.mn + x = rng.random(size=(m,) + size) + res = jacobian(lambda x: func(x , xp), xp.asarray(x, dtype=dtype)) + # convert list of arrays to single array before converting to xp array + ref = xp.asarray(np.asarray(func.ref(x)), dtype=dtype) + xp_assert_close(res.df, ref, atol=atol) + + def test_attrs(self, xp): + # Test attributes of result object + z = xp.asarray([0.5, 0.25]) + + # case in which some elements of the Jacobian are harder + # to calculate than others + def df1(z): + x, y = z + return xp.stack([xp.cos(0.5*x) * xp.cos(y), xp.sin(2*x) * y**2]) + + def df1_0xy(x, y): + return xp.cos(0.5*x) * xp.cos(y) + + def df1_1xy(x, y): + return xp.sin(2*x) * y**2 + + res = jacobian(df1, z, initial_step=10) + if is_numpy(xp): + assert len(np.unique(res.nit)) == 4 + assert len(np.unique(res.nfev)) == 4 + + res00 = jacobian(lambda x: df1_0xy(x, z[1]), z[0:1], initial_step=10) + res01 = jacobian(lambda y: df1_0xy(z[0], y), z[1:2], initial_step=10) + res10 = jacobian(lambda x: df1_1xy(x, z[1]), z[0:1], initial_step=10) + res11 = jacobian(lambda y: df1_1xy(z[0], y), z[1:2], initial_step=10) + ref = optimize.OptimizeResult() + for attr in ['success', 'status', 'df', 'nit', 'nfev']: + ref_attr = xp.asarray([[getattr(res00, attr), getattr(res01, attr)], + [getattr(res10, attr), getattr(res11, attr)]]) + ref[attr] = xp.squeeze(ref_attr) + rtol = 1.5e-5 if res[attr].dtype == xp.float32 else 1.5e-14 + xp_assert_close(res[attr], ref[attr], rtol=rtol) + + def test_step_direction_size(self, xp): + # Check that `step_direction` and `initial_step` can be used to ensure that + # the usable domain of a function is respected. + rng = np.random.default_rng(23892589425245) + b = rng.random(3) + eps = 1e-7 # torch needs wiggle room? + + def f(x): + x[0, x[0] < b[0]] = xp.nan + x[0, x[0] > b[0] + 0.25] = xp.nan + x[1, x[1] > b[1]] = xp.nan + x[1, x[1] < b[1] - 0.1-eps] = xp.nan + return TestJacobian.f5(x, xp) + + dir = [1, -1, 0] + h0 = [0.25, 0.1, 0.5] + atol = {'atol': 1e-8} + res = jacobian(f, xp.asarray(b, dtype=xp.float64), initial_step=h0, + step_direction=dir, tolerances=atol) + ref = xp.asarray(TestJacobian.df5(b), dtype=xp.float64) + xp_assert_close(res.df, ref, atol=1e-8) + assert xp.all(xp.isfinite(ref)) + + +@pytest.mark.skip_xp_backends('array_api_strict', reason=array_api_strict_skip_reason) +@pytest.mark.skip_xp_backends('jax.numpy',reason=jax_skip_reason) +class TestHessian(JacobianHessianTest): + jh_func = hessian + + @pytest.mark.parametrize('shape', [(), (4,), (2, 4)]) + def test_example(self, shape, xp): + rng = np.random.default_rng(458912319542) + m = 3 + x = xp.asarray(rng.random((m,) + shape), dtype=xp.float64) + res = hessian(optimize.rosen, x) + if shape: + x = xp.reshape(x, (m, -1)) + ref = xp.stack([optimize.rosen_hess(xi) for xi in x.T]) + ref = xp.moveaxis(ref, 0, -1) + ref = xp.reshape(ref, (m, m,) + shape) + else: + ref = optimize.rosen_hess(x) + xp_assert_close(res.ddf, ref, atol=1e-8) + + # # Removed symmetry enforcement; consider adding back in as a feature + # # check symmetry + # for key in ['ddf', 'error', 'nfev', 'success', 'status']: + # assert_equal(res[key], np.swapaxes(res[key], 0, 1)) + + def test_float32(self, xp): + rng = np.random.default_rng(458912319542) + x = xp.asarray(rng.random(3), dtype=xp.float32) + res = hessian(optimize.rosen, x) + ref = optimize.rosen_hess(x) + mask = (ref != 0) + xp_assert_close(res.ddf[mask], ref[mask]) + atol = 1e-2 * xp.abs(xp.min(ref[mask])) + xp_assert_close(res.ddf[~mask], ref[~mask], atol=atol) + + def test_nfev(self, xp): + z = xp.asarray([0.5, 0.25]) + xp_test = array_namespace(z) + + def f1(z): + x, y = xp_test.broadcast_arrays(*z) + f1.nfev = f1.nfev + (math.prod(x.shape[2:]) if x.ndim > 2 else 1) + return xp.sin(x) * y ** 3 + f1.nfev = 0 + + + res = hessian(f1, z, initial_step=10) + f1.nfev = 0 + res00 = hessian(lambda x: f1([x[0], z[1]]), z[0:1], initial_step=10) + assert res.nfev[0, 0] == f1.nfev == res00.nfev[0, 0] + + f1.nfev = 0 + res11 = hessian(lambda y: f1([z[0], y[0]]), z[1:2], initial_step=10) + assert res.nfev[1, 1] == f1.nfev == res11.nfev[0, 0] + + # Removed symmetry enforcement; consider adding back in as a feature + # assert_equal(res.nfev, res.nfev.T) # check symmetry + # assert np.unique(res.nfev).size == 3 + + + @pytest.mark.thread_unsafe + @pytest.mark.skip_xp_backends(np_only=True, + reason='Python list input uses NumPy backend') + def test_small_rtol_warning(self, xp): + message = 'The specified `rtol=1e-15`, but...' + with pytest.warns(RuntimeWarning, match=message): + hessian(xp.sin, [1.], tolerances=dict(rtol=1e-15)) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..c545a00b9fd63427088ac873fa3fa65678b77f71 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/__init__.py @@ -0,0 +1,114 @@ +""" +============================================== +Discrete Fourier transforms (:mod:`scipy.fft`) +============================================== + +.. currentmodule:: scipy.fft + +Fast Fourier Transforms (FFTs) +============================== + +.. autosummary:: + :toctree: generated/ + + fft - Fast (discrete) Fourier Transform (FFT) + ifft - Inverse FFT + fft2 - 2-D FFT + ifft2 - 2-D inverse FFT + fftn - N-D FFT + ifftn - N-D inverse FFT + rfft - FFT of strictly real-valued sequence + irfft - Inverse of rfft + rfft2 - 2-D FFT of real sequence + irfft2 - Inverse of rfft2 + rfftn - N-D FFT of real sequence + irfftn - Inverse of rfftn + hfft - FFT of a Hermitian sequence (real spectrum) + ihfft - Inverse of hfft + hfft2 - 2-D FFT of a Hermitian sequence + ihfft2 - Inverse of hfft2 + hfftn - N-D FFT of a Hermitian sequence + ihfftn - Inverse of hfftn + +Discrete Sin and Cosine Transforms (DST and DCT) +================================================ + +.. autosummary:: + :toctree: generated/ + + dct - Discrete cosine transform + idct - Inverse discrete cosine transform + dctn - N-D Discrete cosine transform + idctn - N-D Inverse discrete cosine transform + dst - Discrete sine transform + idst - Inverse discrete sine transform + dstn - N-D Discrete sine transform + idstn - N-D Inverse discrete sine transform + +Fast Hankel Transforms +====================== + +.. autosummary:: + :toctree: generated/ + + fht - Fast Hankel transform + ifht - Inverse of fht + +Helper functions +================ + +.. autosummary:: + :toctree: generated/ + + fftshift - Shift the zero-frequency component to the center of the spectrum + ifftshift - The inverse of `fftshift` + fftfreq - Return the Discrete Fourier Transform sample frequencies + rfftfreq - DFT sample frequencies (for usage with rfft, irfft) + fhtoffset - Compute an optimal offset for the Fast Hankel Transform + next_fast_len - Find the optimal length to zero-pad an FFT for speed + prev_fast_len - Find the maximum slice length that results in a fast FFT + set_workers - Context manager to set default number of workers + get_workers - Get the current default number of workers + +Backend control +=============== + +.. autosummary:: + :toctree: generated/ + + set_backend - Context manager to set the backend within a fixed scope + skip_backend - Context manager to skip a backend within a fixed scope + set_global_backend - Sets the global fft backend + register_backend - Register a backend for permanent use + +""" + +from ._basic import ( + fft, ifft, fft2, ifft2, fftn, ifftn, + rfft, irfft, rfft2, irfft2, rfftn, irfftn, + hfft, ihfft, hfft2, ihfft2, hfftn, ihfftn) +from ._realtransforms import dct, idct, dst, idst, dctn, idctn, dstn, idstn +from ._fftlog import fht, ifht, fhtoffset +from ._helper import ( + next_fast_len, prev_fast_len, fftfreq, + rfftfreq, fftshift, ifftshift) +from ._backend import (set_backend, skip_backend, set_global_backend, + register_backend) +from ._pocketfft.helper import set_workers, get_workers + +__all__ = [ + 'fft', 'ifft', 'fft2', 'ifft2', 'fftn', 'ifftn', + 'rfft', 'irfft', 'rfft2', 'irfft2', 'rfftn', 'irfftn', + 'hfft', 'ihfft', 'hfft2', 'ihfft2', 'hfftn', 'ihfftn', + 'fftfreq', 'rfftfreq', 'fftshift', 'ifftshift', + 'next_fast_len', 'prev_fast_len', + 'dct', 'idct', 'dst', 'idst', 'dctn', 'idctn', 'dstn', 'idstn', + 'fht', 'ifht', + 'fhtoffset', + 'set_backend', 'skip_backend', 'set_global_backend', 'register_backend', + 'get_workers', 'set_workers'] + + +from scipy._lib._testutils import PytestTester +test = PytestTester(__name__) +del PytestTester diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_backend.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_backend.py new file mode 100644 index 0000000000000000000000000000000000000000..c1e5cfcad5c4cbc43276e151d2da33039368630d --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_backend.py @@ -0,0 +1,196 @@ +import scipy._lib.uarray as ua +from . import _basic_backend +from . import _realtransforms_backend +from . import _fftlog_backend + + +class _ScipyBackend: + """The default backend for fft calculations + + Notes + ----- + We use the domain ``numpy.scipy`` rather than ``scipy`` because ``uarray`` + treats the domain as a hierarchy. This means the user can install a single + backend for ``numpy`` and have it implement ``numpy.scipy.fft`` as well. + """ + __ua_domain__ = "numpy.scipy.fft" + + @staticmethod + def __ua_function__(method, args, kwargs): + + fn = getattr(_basic_backend, method.__name__, None) + if fn is None: + fn = getattr(_realtransforms_backend, method.__name__, None) + if fn is None: + fn = getattr(_fftlog_backend, method.__name__, None) + if fn is None: + return NotImplemented + return fn(*args, **kwargs) + + +_named_backends = { + 'scipy': _ScipyBackend, +} + + +def _backend_from_arg(backend): + """Maps strings to known backends and validates the backend""" + + if isinstance(backend, str): + try: + backend = _named_backends[backend] + except KeyError as e: + raise ValueError(f'Unknown backend {backend}') from e + + if backend.__ua_domain__ != 'numpy.scipy.fft': + raise ValueError('Backend does not implement "numpy.scipy.fft"') + + return backend + + +def set_global_backend(backend, coerce=False, only=False, try_last=False): + """Sets the global fft backend + + This utility method replaces the default backend for permanent use. It + will be tried in the list of backends automatically, unless the + ``only`` flag is set on a backend. This will be the first tried + backend outside the :obj:`set_backend` context manager. + + Parameters + ---------- + backend : {object, 'scipy'} + The backend to use. + Can either be a ``str`` containing the name of a known backend + {'scipy'} or an object that implements the uarray protocol. + coerce : bool + Whether to coerce input types when trying this backend. + only : bool + If ``True``, no more backends will be tried if this fails. + Implied by ``coerce=True``. + try_last : bool + If ``True``, the global backend is tried after registered backends. + + Raises + ------ + ValueError: If the backend does not implement ``numpy.scipy.fft``. + + Notes + ----- + This will overwrite the previously set global backend, which, by default, is + the SciPy implementation. + + Examples + -------- + We can set the global fft backend: + + >>> from scipy.fft import fft, set_global_backend + >>> set_global_backend("scipy") # Sets global backend (default is "scipy"). + >>> fft([1]) # Calls the global backend + array([1.+0.j]) + """ + backend = _backend_from_arg(backend) + ua.set_global_backend(backend, coerce=coerce, only=only, try_last=try_last) + + +def register_backend(backend): + """ + Register a backend for permanent use. + + Registered backends have the lowest priority and will be tried after the + global backend. + + Parameters + ---------- + backend : {object, 'scipy'} + The backend to use. + Can either be a ``str`` containing the name of a known backend + {'scipy'} or an object that implements the uarray protocol. + + Raises + ------ + ValueError: If the backend does not implement ``numpy.scipy.fft``. + + Examples + -------- + We can register a new fft backend: + + >>> from scipy.fft import fft, register_backend, set_global_backend + >>> class NoopBackend: # Define an invalid Backend + ... __ua_domain__ = "numpy.scipy.fft" + ... def __ua_function__(self, func, args, kwargs): + ... return NotImplemented + >>> set_global_backend(NoopBackend()) # Set the invalid backend as global + >>> register_backend("scipy") # Register a new backend + # The registered backend is called because + # the global backend returns `NotImplemented` + >>> fft([1]) + array([1.+0.j]) + >>> set_global_backend("scipy") # Restore global backend to default + + """ + backend = _backend_from_arg(backend) + ua.register_backend(backend) + + +def set_backend(backend, coerce=False, only=False): + """Context manager to set the backend within a fixed scope. + + Upon entering the ``with`` statement, the given backend will be added to + the list of available backends with the highest priority. Upon exit, the + backend is reset to the state before entering the scope. + + Parameters + ---------- + backend : {object, 'scipy'} + The backend to use. + Can either be a ``str`` containing the name of a known backend + {'scipy'} or an object that implements the uarray protocol. + coerce : bool, optional + Whether to allow expensive conversions for the ``x`` parameter. e.g., + copying a NumPy array to the GPU for a CuPy backend. Implies ``only``. + only : bool, optional + If only is ``True`` and this backend returns ``NotImplemented``, then a + BackendNotImplemented error will be raised immediately. Ignoring any + lower priority backends. + + Examples + -------- + >>> import scipy.fft as fft + >>> with fft.set_backend('scipy', only=True): + ... fft.fft([1]) # Always calls the scipy implementation + array([1.+0.j]) + """ + backend = _backend_from_arg(backend) + return ua.set_backend(backend, coerce=coerce, only=only) + + +def skip_backend(backend): + """Context manager to skip a backend within a fixed scope. + + Within the context of a ``with`` statement, the given backend will not be + called. This covers backends registered both locally and globally. Upon + exit, the backend will again be considered. + + Parameters + ---------- + backend : {object, 'scipy'} + The backend to skip. + Can either be a ``str`` containing the name of a known backend + {'scipy'} or an object that implements the uarray protocol. + + Examples + -------- + >>> import scipy.fft as fft + >>> fft.fft([1]) # Calls default SciPy backend + array([1.+0.j]) + >>> with fft.skip_backend('scipy'): # We explicitly skip the SciPy backend + ... fft.fft([1]) # leaving no implementation available + Traceback (most recent call last): + ... + BackendNotImplementedError: No selected backends had an implementation ... + """ + backend = _backend_from_arg(backend) + return ua.skip_backend(backend) + + +set_global_backend('scipy', try_last=True) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_basic.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_basic.py new file mode 100644 index 0000000000000000000000000000000000000000..a3fc021c9ef9b7c2a40bf7b5138158df8e276ae6 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_basic.py @@ -0,0 +1,1630 @@ +from scipy._lib.uarray import generate_multimethod, Dispatchable +import numpy as np + + +def _x_replacer(args, kwargs, dispatchables): + """ + uarray argument replacer to replace the transform input array (``x``) + """ + if len(args) > 0: + return (dispatchables[0],) + args[1:], kwargs + kw = kwargs.copy() + kw['x'] = dispatchables[0] + return args, kw + + +def _dispatch(func): + """ + Function annotation that creates a uarray multimethod from the function + """ + return generate_multimethod(func, _x_replacer, domain="numpy.scipy.fft") + + +@_dispatch +def fft(x, n=None, axis=-1, norm=None, overwrite_x=False, workers=None, *, + plan=None): + """ + Compute the 1-D discrete Fourier Transform. + + This function computes the 1-D *n*-point discrete Fourier + Transform (DFT) with the efficient Fast Fourier Transform (FFT) + algorithm [1]_. + + Parameters + ---------- + x : array_like + Input array, can be complex. + n : int, optional + Length of the transformed axis of the output. + If `n` is smaller than the length of the input, the input is cropped. + If it is larger, the input is padded with zeros. If `n` is not given, + the length of the input along the axis specified by `axis` is used. + axis : int, optional + Axis over which to compute the FFT. If not given, the last axis is + used. + norm : {"backward", "ortho", "forward"}, optional + Normalization mode. Default is "backward", meaning no normalization on + the forward transforms and scaling by ``1/n`` on the `ifft`. + "forward" instead applies the ``1/n`` factor on the forward transform. + For ``norm="ortho"``, both directions are scaled by ``1/sqrt(n)``. + + .. versionadded:: 1.6.0 + ``norm={"forward", "backward"}`` options were added + + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + See the notes below for more details. + workers : int, optional + Maximum number of workers to use for parallel computation. If negative, + the value wraps around from ``os.cpu_count()``. See below for more + details. + plan : object, optional + This argument is reserved for passing in a precomputed plan provided + by downstream FFT vendors. It is currently not used in SciPy. + + .. versionadded:: 1.5.0 + + Returns + ------- + out : complex ndarray + The truncated or zero-padded input, transformed along the axis + indicated by `axis`, or the last one if `axis` is not specified. + + Raises + ------ + IndexError + if `axes` is larger than the last axis of `x`. + + See Also + -------- + ifft : The inverse of `fft`. + fft2 : The 2-D FFT. + fftn : The N-D FFT. + rfftn : The N-D FFT of real input. + fftfreq : Frequency bins for given FFT parameters. + next_fast_len : Size to pad input to for most efficient transforms + + Notes + ----- + FFT (Fast Fourier Transform) refers to a way the discrete Fourier Transform + (DFT) can be calculated efficiently, by using symmetries in the calculated + terms. The symmetry is highest when `n` is a power of 2, and the transform + is therefore most efficient for these sizes. For poorly factorizable sizes, + `scipy.fft` uses Bluestein's algorithm [2]_ and so is never worse than + O(`n` log `n`). Further performance improvements may be seen by zero-padding + the input using `next_fast_len`. + + If ``x`` is a 1d array, then the `fft` is equivalent to :: + + y[k] = np.sum(x * np.exp(-2j * np.pi * k * np.arange(n)/n)) + + The frequency term ``f=k/n`` is found at ``y[k]``. At ``y[n/2]`` we reach + the Nyquist frequency and wrap around to the negative-frequency terms. So, + for an 8-point transform, the frequencies of the result are + [0, 1, 2, 3, -4, -3, -2, -1]. To rearrange the fft output so that the + zero-frequency component is centered, like [-4, -3, -2, -1, 0, 1, 2, 3], + use `fftshift`. + + Transforms can be done in single, double, or extended precision (long + double) floating point. Half precision inputs will be converted to single + precision and non-floating-point inputs will be converted to double + precision. + + If the data type of ``x`` is real, a "real FFT" algorithm is automatically + used, which roughly halves the computation time. To increase efficiency + a little further, use `rfft`, which does the same calculation, but only + outputs half of the symmetrical spectrum. If the data are both real and + symmetrical, the `dct` can again double the efficiency, by generating + half of the spectrum from half of the signal. + + When ``overwrite_x=True`` is specified, the memory referenced by ``x`` may + be used by the implementation in any way. This may include reusing the + memory for the result, but this is in no way guaranteed. You should not + rely on the contents of ``x`` after the transform as this may change in + future without warning. + + The ``workers`` argument specifies the maximum number of parallel jobs to + split the FFT computation into. This will execute independent 1-D + FFTs within ``x``. So, ``x`` must be at least 2-D and the + non-transformed axes must be large enough to split into chunks. If ``x`` is + too small, fewer jobs may be used than requested. + + References + ---------- + .. [1] Cooley, James W., and John W. Tukey, 1965, "An algorithm for the + machine calculation of complex Fourier series," *Math. Comput.* + 19: 297-301. + .. [2] Bluestein, L., 1970, "A linear filtering approach to the + computation of discrete Fourier transform". *IEEE Transactions on + Audio and Electroacoustics.* 18 (4): 451-455. + + Examples + -------- + >>> import scipy.fft + >>> import numpy as np + >>> scipy.fft.fft(np.exp(2j * np.pi * np.arange(8) / 8)) + array([-2.33486982e-16+1.14423775e-17j, 8.00000000e+00-1.25557246e-15j, + 2.33486982e-16+2.33486982e-16j, 0.00000000e+00+1.22464680e-16j, + -1.14423775e-17+2.33486982e-16j, 0.00000000e+00+5.20784380e-16j, + 1.14423775e-17+1.14423775e-17j, 0.00000000e+00+1.22464680e-16j]) + + In this example, real input has an FFT which is Hermitian, i.e., symmetric + in the real part and anti-symmetric in the imaginary part: + + >>> from scipy.fft import fft, fftfreq, fftshift + >>> import matplotlib.pyplot as plt + >>> t = np.arange(256) + >>> sp = fftshift(fft(np.sin(t))) + >>> freq = fftshift(fftfreq(t.shape[-1])) + >>> plt.plot(freq, sp.real, freq, sp.imag) + [, + ] + >>> plt.show() + + """ + return (Dispatchable(x, np.ndarray),) + + +@_dispatch +def ifft(x, n=None, axis=-1, norm=None, overwrite_x=False, workers=None, *, + plan=None): + """ + Compute the 1-D inverse discrete Fourier Transform. + + This function computes the inverse of the 1-D *n*-point + discrete Fourier transform computed by `fft`. In other words, + ``ifft(fft(x)) == x`` to within numerical accuracy. + + The input should be ordered in the same way as is returned by `fft`, + i.e., + + * ``x[0]`` should contain the zero frequency term, + * ``x[1:n//2]`` should contain the positive-frequency terms, + * ``x[n//2 + 1:]`` should contain the negative-frequency terms, in + increasing order starting from the most negative frequency. + + For an even number of input points, ``x[n//2]`` represents the sum of + the values at the positive and negative Nyquist frequencies, as the two + are aliased together. See `fft` for details. + + Parameters + ---------- + x : array_like + Input array, can be complex. + n : int, optional + Length of the transformed axis of the output. + If `n` is smaller than the length of the input, the input is cropped. + If it is larger, the input is padded with zeros. If `n` is not given, + the length of the input along the axis specified by `axis` is used. + See notes about padding issues. + axis : int, optional + Axis over which to compute the inverse DFT. If not given, the last + axis is used. + norm : {"backward", "ortho", "forward"}, optional + Normalization mode (see `fft`). Default is "backward". + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + See :func:`fft` for more details. + workers : int, optional + Maximum number of workers to use for parallel computation. If negative, + the value wraps around from ``os.cpu_count()``. + See :func:`~scipy.fft.fft` for more details. + plan : object, optional + This argument is reserved for passing in a precomputed plan provided + by downstream FFT vendors. It is currently not used in SciPy. + + .. versionadded:: 1.5.0 + + Returns + ------- + out : complex ndarray + The truncated or zero-padded input, transformed along the axis + indicated by `axis`, or the last one if `axis` is not specified. + + Raises + ------ + IndexError + If `axes` is larger than the last axis of `x`. + + See Also + -------- + fft : The 1-D (forward) FFT, of which `ifft` is the inverse. + ifft2 : The 2-D inverse FFT. + ifftn : The N-D inverse FFT. + + Notes + ----- + If the input parameter `n` is larger than the size of the input, the input + is padded by appending zeros at the end. Even though this is the common + approach, it might lead to surprising results. If a different padding is + desired, it must be performed before calling `ifft`. + + If ``x`` is a 1-D array, then the `ifft` is equivalent to :: + + y[k] = np.sum(x * np.exp(2j * np.pi * k * np.arange(n)/n)) / len(x) + + As with `fft`, `ifft` has support for all floating point types and is + optimized for real input. + + Examples + -------- + >>> import scipy.fft + >>> import numpy as np + >>> scipy.fft.ifft([0, 4, 0, 0]) + array([ 1.+0.j, 0.+1.j, -1.+0.j, 0.-1.j]) # may vary + + Create and plot a band-limited signal with random phases: + + >>> import matplotlib.pyplot as plt + >>> rng = np.random.default_rng() + >>> t = np.arange(400) + >>> n = np.zeros((400,), dtype=complex) + >>> n[40:60] = np.exp(1j*rng.uniform(0, 2*np.pi, (20,))) + >>> s = scipy.fft.ifft(n) + >>> plt.plot(t, s.real, 'b-', t, s.imag, 'r--') + [, ] + >>> plt.legend(('real', 'imaginary')) + + >>> plt.show() + + """ + return (Dispatchable(x, np.ndarray),) + + +@_dispatch +def rfft(x, n=None, axis=-1, norm=None, overwrite_x=False, workers=None, *, + plan=None): + """ + Compute the 1-D discrete Fourier Transform for real input. + + This function computes the 1-D *n*-point discrete Fourier + Transform (DFT) of a real-valued array by means of an efficient algorithm + called the Fast Fourier Transform (FFT). + + Parameters + ---------- + x : array_like + Input array + n : int, optional + Number of points along transformation axis in the input to use. + If `n` is smaller than the length of the input, the input is cropped. + If it is larger, the input is padded with zeros. If `n` is not given, + the length of the input along the axis specified by `axis` is used. + axis : int, optional + Axis over which to compute the FFT. If not given, the last axis is + used. + norm : {"backward", "ortho", "forward"}, optional + Normalization mode (see `fft`). Default is "backward". + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + See :func:`fft` for more details. + workers : int, optional + Maximum number of workers to use for parallel computation. If negative, + the value wraps around from ``os.cpu_count()``. + See :func:`~scipy.fft.fft` for more details. + plan : object, optional + This argument is reserved for passing in a precomputed plan provided + by downstream FFT vendors. It is currently not used in SciPy. + + .. versionadded:: 1.5.0 + + Returns + ------- + out : complex ndarray + The truncated or zero-padded input, transformed along the axis + indicated by `axis`, or the last one if `axis` is not specified. + If `n` is even, the length of the transformed axis is ``(n/2)+1``. + If `n` is odd, the length is ``(n+1)/2``. + + Raises + ------ + IndexError + If `axis` is larger than the last axis of `a`. + + See Also + -------- + irfft : The inverse of `rfft`. + fft : The 1-D FFT of general (complex) input. + fftn : The N-D FFT. + rfft2 : The 2-D FFT of real input. + rfftn : The N-D FFT of real input. + + Notes + ----- + When the DFT is computed for purely real input, the output is + Hermitian-symmetric, i.e., the negative frequency terms are just the complex + conjugates of the corresponding positive-frequency terms, and the + negative-frequency terms are therefore redundant. This function does not + compute the negative frequency terms, and the length of the transformed + axis of the output is therefore ``n//2 + 1``. + + When ``X = rfft(x)`` and fs is the sampling frequency, ``X[0]`` contains + the zero-frequency term 0*fs, which is real due to Hermitian symmetry. + + If `n` is even, ``A[-1]`` contains the term representing both positive + and negative Nyquist frequency (+fs/2 and -fs/2), and must also be purely + real. If `n` is odd, there is no term at fs/2; ``A[-1]`` contains + the largest positive frequency (fs/2*(n-1)/n), and is complex in the + general case. + + If the input `a` contains an imaginary part, it is silently discarded. + + Examples + -------- + >>> import scipy.fft + >>> scipy.fft.fft([0, 1, 0, 0]) + array([ 1.+0.j, 0.-1.j, -1.+0.j, 0.+1.j]) # may vary + >>> scipy.fft.rfft([0, 1, 0, 0]) + array([ 1.+0.j, 0.-1.j, -1.+0.j]) # may vary + + Notice how the final element of the `fft` output is the complex conjugate + of the second element, for real input. For `rfft`, this symmetry is + exploited to compute only the non-negative frequency terms. + + """ + return (Dispatchable(x, np.ndarray),) + + +@_dispatch +def irfft(x, n=None, axis=-1, norm=None, overwrite_x=False, workers=None, *, + plan=None): + """ + Computes the inverse of `rfft`. + + This function computes the inverse of the 1-D *n*-point + discrete Fourier Transform of real input computed by `rfft`. + In other words, ``irfft(rfft(x), len(x)) == x`` to within numerical + accuracy. (See Notes below for why ``len(a)`` is necessary here.) + + The input is expected to be in the form returned by `rfft`, i.e., the + real zero-frequency term followed by the complex positive frequency terms + in order of increasing frequency. Since the discrete Fourier Transform of + real input is Hermitian-symmetric, the negative frequency terms are taken + to be the complex conjugates of the corresponding positive frequency terms. + + Parameters + ---------- + x : array_like + The input array. + n : int, optional + Length of the transformed axis of the output. + For `n` output points, ``n//2+1`` input points are necessary. If the + input is longer than this, it is cropped. If it is shorter than this, + it is padded with zeros. If `n` is not given, it is taken to be + ``2*(m-1)``, where ``m`` is the length of the input along the axis + specified by `axis`. + axis : int, optional + Axis over which to compute the inverse FFT. If not given, the last + axis is used. + norm : {"backward", "ortho", "forward"}, optional + Normalization mode (see `fft`). Default is "backward". + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + See :func:`fft` for more details. + workers : int, optional + Maximum number of workers to use for parallel computation. If negative, + the value wraps around from ``os.cpu_count()``. + See :func:`~scipy.fft.fft` for more details. + plan : object, optional + This argument is reserved for passing in a precomputed plan provided + by downstream FFT vendors. It is currently not used in SciPy. + + .. versionadded:: 1.5.0 + + Returns + ------- + out : ndarray + The truncated or zero-padded input, transformed along the axis + indicated by `axis`, or the last one if `axis` is not specified. + The length of the transformed axis is `n`, or, if `n` is not given, + ``2*(m-1)`` where ``m`` is the length of the transformed axis of the + input. To get an odd number of output points, `n` must be specified. + + Raises + ------ + IndexError + If `axis` is larger than the last axis of `x`. + + See Also + -------- + rfft : The 1-D FFT of real input, of which `irfft` is inverse. + fft : The 1-D FFT. + irfft2 : The inverse of the 2-D FFT of real input. + irfftn : The inverse of the N-D FFT of real input. + + Notes + ----- + Returns the real valued `n`-point inverse discrete Fourier transform + of `x`, where `x` contains the non-negative frequency terms of a + Hermitian-symmetric sequence. `n` is the length of the result, not the + input. + + If you specify an `n` such that `a` must be zero-padded or truncated, the + extra/removed values will be added/removed at high frequencies. One can + thus resample a series to `m` points via Fourier interpolation by: + ``a_resamp = irfft(rfft(a), m)``. + + The default value of `n` assumes an even output length. By the Hermitian + symmetry, the last imaginary component must be 0 and so is ignored. To + avoid losing information, the correct length of the real input *must* be + given. + + Examples + -------- + >>> import scipy.fft + >>> scipy.fft.ifft([1, -1j, -1, 1j]) + array([0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j]) # may vary + >>> scipy.fft.irfft([1, -1j, -1]) + array([0., 1., 0., 0.]) + + Notice how the last term in the input to the ordinary `ifft` is the + complex conjugate of the second term, and the output has zero imaginary + part everywhere. When calling `irfft`, the negative frequencies are not + specified, and the output array is purely real. + + """ + return (Dispatchable(x, np.ndarray),) + + +@_dispatch +def hfft(x, n=None, axis=-1, norm=None, overwrite_x=False, workers=None, *, + plan=None): + """ + Compute the FFT of a signal that has Hermitian symmetry, i.e., a real + spectrum. + + Parameters + ---------- + x : array_like + The input array. + n : int, optional + Length of the transformed axis of the output. For `n` output + points, ``n//2 + 1`` input points are necessary. If the input is + longer than this, it is cropped. If it is shorter than this, it is + padded with zeros. If `n` is not given, it is taken to be ``2*(m-1)``, + where ``m`` is the length of the input along the axis specified by + `axis`. + axis : int, optional + Axis over which to compute the FFT. If not given, the last + axis is used. + norm : {"backward", "ortho", "forward"}, optional + Normalization mode (see `fft`). Default is "backward". + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + See `fft` for more details. + workers : int, optional + Maximum number of workers to use for parallel computation. If negative, + the value wraps around from ``os.cpu_count()``. + See :func:`~scipy.fft.fft` for more details. + plan : object, optional + This argument is reserved for passing in a precomputed plan provided + by downstream FFT vendors. It is currently not used in SciPy. + + .. versionadded:: 1.5.0 + + Returns + ------- + out : ndarray + The truncated or zero-padded input, transformed along the axis + indicated by `axis`, or the last one if `axis` is not specified. + The length of the transformed axis is `n`, or, if `n` is not given, + ``2*m - 2``, where ``m`` is the length of the transformed axis of + the input. To get an odd number of output points, `n` must be + specified, for instance, as ``2*m - 1`` in the typical case, + + Raises + ------ + IndexError + If `axis` is larger than the last axis of `a`. + + See Also + -------- + rfft : Compute the 1-D FFT for real input. + ihfft : The inverse of `hfft`. + hfftn : Compute the N-D FFT of a Hermitian signal. + + Notes + ----- + `hfft`/`ihfft` are a pair analogous to `rfft`/`irfft`, but for the + opposite case: here the signal has Hermitian symmetry in the time + domain and is real in the frequency domain. So, here, it's `hfft`, for + which you must supply the length of the result if it is to be odd. + * even: ``ihfft(hfft(a, 2*len(a) - 2) == a``, within roundoff error, + * odd: ``ihfft(hfft(a, 2*len(a) - 1) == a``, within roundoff error. + + Examples + -------- + >>> from scipy.fft import fft, hfft + >>> import numpy as np + >>> a = 2 * np.pi * np.arange(10) / 10 + >>> signal = np.cos(a) + 3j * np.sin(3 * a) + >>> fft(signal).round(10) + array([ -0.+0.j, 5.+0.j, -0.+0.j, 15.-0.j, 0.+0.j, 0.+0.j, + -0.+0.j, -15.-0.j, 0.+0.j, 5.+0.j]) + >>> hfft(signal[:6]).round(10) # Input first half of signal + array([ 0., 5., 0., 15., -0., 0., 0., -15., -0., 5.]) + >>> hfft(signal, 10) # Input entire signal and truncate + array([ 0., 5., 0., 15., -0., 0., 0., -15., -0., 5.]) + """ + return (Dispatchable(x, np.ndarray),) + + +@_dispatch +def ihfft(x, n=None, axis=-1, norm=None, overwrite_x=False, workers=None, *, + plan=None): + """ + Compute the inverse FFT of a signal that has Hermitian symmetry. + + Parameters + ---------- + x : array_like + Input array. + n : int, optional + Length of the inverse FFT, the number of points along + transformation axis in the input to use. If `n` is smaller than + the length of the input, the input is cropped. If it is larger, + the input is padded with zeros. If `n` is not given, the length of + the input along the axis specified by `axis` is used. + axis : int, optional + Axis over which to compute the inverse FFT. If not given, the last + axis is used. + norm : {"backward", "ortho", "forward"}, optional + Normalization mode (see `fft`). Default is "backward". + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + See `fft` for more details. + workers : int, optional + Maximum number of workers to use for parallel computation. If negative, + the value wraps around from ``os.cpu_count()``. + See :func:`~scipy.fft.fft` for more details. + plan : object, optional + This argument is reserved for passing in a precomputed plan provided + by downstream FFT vendors. It is currently not used in SciPy. + + .. versionadded:: 1.5.0 + + Returns + ------- + out : complex ndarray + The truncated or zero-padded input, transformed along the axis + indicated by `axis`, or the last one if `axis` is not specified. + The length of the transformed axis is ``n//2 + 1``. + + See Also + -------- + hfft, irfft + + Notes + ----- + `hfft`/`ihfft` are a pair analogous to `rfft`/`irfft`, but for the + opposite case: here, the signal has Hermitian symmetry in the time + domain and is real in the frequency domain. So, here, it's `hfft`, for + which you must supply the length of the result if it is to be odd: + * even: ``ihfft(hfft(a, 2*len(a) - 2) == a``, within roundoff error, + * odd: ``ihfft(hfft(a, 2*len(a) - 1) == a``, within roundoff error. + + Examples + -------- + >>> from scipy.fft import ifft, ihfft + >>> import numpy as np + >>> spectrum = np.array([ 15, -4, 0, -1, 0, -4]) + >>> ifft(spectrum) + array([1.+0.j, 2.+0.j, 3.+0.j, 4.+0.j, 3.+0.j, 2.+0.j]) # may vary + >>> ihfft(spectrum) + array([ 1.-0.j, 2.-0.j, 3.-0.j, 4.-0.j]) # may vary + """ + return (Dispatchable(x, np.ndarray),) + + +@_dispatch +def fftn(x, s=None, axes=None, norm=None, overwrite_x=False, workers=None, *, + plan=None): + """ + Compute the N-D discrete Fourier Transform. + + This function computes the N-D discrete Fourier Transform over + any number of axes in an M-D array by means of the Fast Fourier + Transform (FFT). + + Parameters + ---------- + x : array_like + Input array, can be complex. + s : sequence of ints, optional + Shape (length of each transformed axis) of the output + (``s[0]`` refers to axis 0, ``s[1]`` to axis 1, etc.). + This corresponds to ``n`` for ``fft(x, n)``. + Along any axis, if the given shape is smaller than that of the input, + the input is cropped. If it is larger, the input is padded with zeros. + if `s` is not given, the shape of the input along the axes specified + by `axes` is used. + axes : sequence of ints, optional + Axes over which to compute the FFT. If not given, the last ``len(s)`` + axes are used, or all axes if `s` is also not specified. + norm : {"backward", "ortho", "forward"}, optional + Normalization mode (see `fft`). Default is "backward". + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + See :func:`fft` for more details. + workers : int, optional + Maximum number of workers to use for parallel computation. If negative, + the value wraps around from ``os.cpu_count()``. + See :func:`~scipy.fft.fft` for more details. + plan : object, optional + This argument is reserved for passing in a precomputed plan provided + by downstream FFT vendors. It is currently not used in SciPy. + + .. versionadded:: 1.5.0 + + Returns + ------- + out : complex ndarray + The truncated or zero-padded input, transformed along the axes + indicated by `axes`, or by a combination of `s` and `x`, + as explained in the parameters section above. + + Raises + ------ + ValueError + If `s` and `axes` have different length. + IndexError + If an element of `axes` is larger than the number of axes of `x`. + + See Also + -------- + ifftn : The inverse of `fftn`, the inverse N-D FFT. + fft : The 1-D FFT, with definitions and conventions used. + rfftn : The N-D FFT of real input. + fft2 : The 2-D FFT. + fftshift : Shifts zero-frequency terms to centre of array. + + Notes + ----- + The output, analogously to `fft`, contains the term for zero frequency in + the low-order corner of all axes, the positive frequency terms in the + first half of all axes, the term for the Nyquist frequency in the middle + of all axes and the negative frequency terms in the second half of all + axes, in order of decreasingly negative frequency. + + Examples + -------- + >>> import scipy.fft + >>> import numpy as np + >>> x = np.mgrid[:3, :3, :3][0] + >>> scipy.fft.fftn(x, axes=(1, 2)) + array([[[ 0.+0.j, 0.+0.j, 0.+0.j], # may vary + [ 0.+0.j, 0.+0.j, 0.+0.j], + [ 0.+0.j, 0.+0.j, 0.+0.j]], + [[ 9.+0.j, 0.+0.j, 0.+0.j], + [ 0.+0.j, 0.+0.j, 0.+0.j], + [ 0.+0.j, 0.+0.j, 0.+0.j]], + [[18.+0.j, 0.+0.j, 0.+0.j], + [ 0.+0.j, 0.+0.j, 0.+0.j], + [ 0.+0.j, 0.+0.j, 0.+0.j]]]) + >>> scipy.fft.fftn(x, (2, 2), axes=(0, 1)) + array([[[ 2.+0.j, 2.+0.j, 2.+0.j], # may vary + [ 0.+0.j, 0.+0.j, 0.+0.j]], + [[-2.+0.j, -2.+0.j, -2.+0.j], + [ 0.+0.j, 0.+0.j, 0.+0.j]]]) + + >>> import matplotlib.pyplot as plt + >>> rng = np.random.default_rng() + >>> [X, Y] = np.meshgrid(2 * np.pi * np.arange(200) / 12, + ... 2 * np.pi * np.arange(200) / 34) + >>> S = np.sin(X) + np.cos(Y) + rng.uniform(0, 1, X.shape) + >>> FS = scipy.fft.fftn(S) + >>> plt.imshow(np.log(np.abs(scipy.fft.fftshift(FS))**2)) + + >>> plt.show() + + """ + return (Dispatchable(x, np.ndarray),) + + +@_dispatch +def ifftn(x, s=None, axes=None, norm=None, overwrite_x=False, workers=None, *, + plan=None): + """ + Compute the N-D inverse discrete Fourier Transform. + + This function computes the inverse of the N-D discrete + Fourier Transform over any number of axes in an M-D array by + means of the Fast Fourier Transform (FFT). In other words, + ``ifftn(fftn(x)) == x`` to within numerical accuracy. + + The input, analogously to `ifft`, should be ordered in the same way as is + returned by `fftn`, i.e., it should have the term for zero frequency + in all axes in the low-order corner, the positive frequency terms in the + first half of all axes, the term for the Nyquist frequency in the middle + of all axes and the negative frequency terms in the second half of all + axes, in order of decreasingly negative frequency. + + Parameters + ---------- + x : array_like + Input array, can be complex. + s : sequence of ints, optional + Shape (length of each transformed axis) of the output + (``s[0]`` refers to axis 0, ``s[1]`` to axis 1, etc.). + This corresponds to ``n`` for ``ifft(x, n)``. + Along any axis, if the given shape is smaller than that of the input, + the input is cropped. If it is larger, the input is padded with zeros. + if `s` is not given, the shape of the input along the axes specified + by `axes` is used. See notes for issue on `ifft` zero padding. + axes : sequence of ints, optional + Axes over which to compute the IFFT. If not given, the last ``len(s)`` + axes are used, or all axes if `s` is also not specified. + norm : {"backward", "ortho", "forward"}, optional + Normalization mode (see `fft`). Default is "backward". + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + See :func:`fft` for more details. + workers : int, optional + Maximum number of workers to use for parallel computation. If negative, + the value wraps around from ``os.cpu_count()``. + See :func:`~scipy.fft.fft` for more details. + plan : object, optional + This argument is reserved for passing in a precomputed plan provided + by downstream FFT vendors. It is currently not used in SciPy. + + .. versionadded:: 1.5.0 + + Returns + ------- + out : complex ndarray + The truncated or zero-padded input, transformed along the axes + indicated by `axes`, or by a combination of `s` or `x`, + as explained in the parameters section above. + + Raises + ------ + ValueError + If `s` and `axes` have different length. + IndexError + If an element of `axes` is larger than the number of axes of `x`. + + See Also + -------- + fftn : The forward N-D FFT, of which `ifftn` is the inverse. + ifft : The 1-D inverse FFT. + ifft2 : The 2-D inverse FFT. + ifftshift : Undoes `fftshift`, shifts zero-frequency terms to beginning + of array. + + Notes + ----- + Zero-padding, analogously with `ifft`, is performed by appending zeros to + the input along the specified dimension. Although this is the common + approach, it might lead to surprising results. If another form of zero + padding is desired, it must be performed before `ifftn` is called. + + Examples + -------- + >>> import scipy.fft + >>> import numpy as np + >>> x = np.eye(4) + >>> scipy.fft.ifftn(scipy.fft.fftn(x, axes=(0,)), axes=(1,)) + array([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j], # may vary + [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j], + [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j], + [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j]]) + + + Create and plot an image with band-limited frequency content: + + >>> import matplotlib.pyplot as plt + >>> rng = np.random.default_rng() + >>> n = np.zeros((200,200), dtype=complex) + >>> n[60:80, 20:40] = np.exp(1j*rng.uniform(0, 2*np.pi, (20, 20))) + >>> im = scipy.fft.ifftn(n).real + >>> plt.imshow(im) + + >>> plt.show() + + """ + return (Dispatchable(x, np.ndarray),) + + +@_dispatch +def fft2(x, s=None, axes=(-2, -1), norm=None, overwrite_x=False, workers=None, *, + plan=None): + """ + Compute the 2-D discrete Fourier Transform + + This function computes the N-D discrete Fourier Transform + over any axes in an M-D array by means of the + Fast Fourier Transform (FFT). By default, the transform is computed over + the last two axes of the input array, i.e., a 2-dimensional FFT. + + Parameters + ---------- + x : array_like + Input array, can be complex + s : sequence of ints, optional + Shape (length of each transformed axis) of the output + (``s[0]`` refers to axis 0, ``s[1]`` to axis 1, etc.). + This corresponds to ``n`` for ``fft(x, n)``. + Along each axis, if the given shape is smaller than that of the input, + the input is cropped. If it is larger, the input is padded with zeros. + if `s` is not given, the shape of the input along the axes specified + by `axes` is used. + axes : sequence of ints, optional + Axes over which to compute the FFT. If not given, the last two axes are + used. + norm : {"backward", "ortho", "forward"}, optional + Normalization mode (see `fft`). Default is "backward". + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + See :func:`fft` for more details. + workers : int, optional + Maximum number of workers to use for parallel computation. If negative, + the value wraps around from ``os.cpu_count()``. + See :func:`~scipy.fft.fft` for more details. + plan : object, optional + This argument is reserved for passing in a precomputed plan provided + by downstream FFT vendors. It is currently not used in SciPy. + + .. versionadded:: 1.5.0 + + Returns + ------- + out : complex ndarray + The truncated or zero-padded input, transformed along the axes + indicated by `axes`, or the last two axes if `axes` is not given. + + Raises + ------ + ValueError + If `s` and `axes` have different length, or `axes` not given and + ``len(s) != 2``. + IndexError + If an element of `axes` is larger than the number of axes of `x`. + + See Also + -------- + ifft2 : The inverse 2-D FFT. + fft : The 1-D FFT. + fftn : The N-D FFT. + fftshift : Shifts zero-frequency terms to the center of the array. + For 2-D input, swaps first and third quadrants, and second + and fourth quadrants. + + Notes + ----- + `fft2` is just `fftn` with a different default for `axes`. + + The output, analogously to `fft`, contains the term for zero frequency in + the low-order corner of the transformed axes, the positive frequency terms + in the first half of these axes, the term for the Nyquist frequency in the + middle of the axes and the negative frequency terms in the second half of + the axes, in order of decreasingly negative frequency. + + See `fftn` for details and a plotting example, and `fft` for + definitions and conventions used. + + + Examples + -------- + >>> import scipy.fft + >>> import numpy as np + >>> x = np.mgrid[:5, :5][0] + >>> scipy.fft.fft2(x) + array([[ 50. +0.j , 0. +0.j , 0. +0.j , # may vary + 0. +0.j , 0. +0.j ], + [-12.5+17.20477401j, 0. +0.j , 0. +0.j , + 0. +0.j , 0. +0.j ], + [-12.5 +4.0614962j , 0. +0.j , 0. +0.j , + 0. +0.j , 0. +0.j ], + [-12.5 -4.0614962j , 0. +0.j , 0. +0.j , + 0. +0.j , 0. +0.j ], + [-12.5-17.20477401j, 0. +0.j , 0. +0.j , + 0. +0.j , 0. +0.j ]]) + + """ + return (Dispatchable(x, np.ndarray),) + + +@_dispatch +def ifft2(x, s=None, axes=(-2, -1), norm=None, overwrite_x=False, workers=None, *, + plan=None): + """ + Compute the 2-D inverse discrete Fourier Transform. + + This function computes the inverse of the 2-D discrete Fourier + Transform over any number of axes in an M-D array by means of + the Fast Fourier Transform (FFT). In other words, ``ifft2(fft2(x)) == x`` + to within numerical accuracy. By default, the inverse transform is + computed over the last two axes of the input array. + + The input, analogously to `ifft`, should be ordered in the same way as is + returned by `fft2`, i.e., it should have the term for zero frequency + in the low-order corner of the two axes, the positive frequency terms in + the first half of these axes, the term for the Nyquist frequency in the + middle of the axes and the negative frequency terms in the second half of + both axes, in order of decreasingly negative frequency. + + Parameters + ---------- + x : array_like + Input array, can be complex. + s : sequence of ints, optional + Shape (length of each axis) of the output (``s[0]`` refers to axis 0, + ``s[1]`` to axis 1, etc.). This corresponds to `n` for ``ifft(x, n)``. + Along each axis, if the given shape is smaller than that of the input, + the input is cropped. If it is larger, the input is padded with zeros. + if `s` is not given, the shape of the input along the axes specified + by `axes` is used. See notes for issue on `ifft` zero padding. + axes : sequence of ints, optional + Axes over which to compute the FFT. If not given, the last two + axes are used. + norm : {"backward", "ortho", "forward"}, optional + Normalization mode (see `fft`). Default is "backward". + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + See :func:`fft` for more details. + workers : int, optional + Maximum number of workers to use for parallel computation. If negative, + the value wraps around from ``os.cpu_count()``. + See :func:`~scipy.fft.fft` for more details. + plan : object, optional + This argument is reserved for passing in a precomputed plan provided + by downstream FFT vendors. It is currently not used in SciPy. + + .. versionadded:: 1.5.0 + + Returns + ------- + out : complex ndarray + The truncated or zero-padded input, transformed along the axes + indicated by `axes`, or the last two axes if `axes` is not given. + + Raises + ------ + ValueError + If `s` and `axes` have different length, or `axes` not given and + ``len(s) != 2``. + IndexError + If an element of `axes` is larger than the number of axes of `x`. + + See Also + -------- + fft2 : The forward 2-D FFT, of which `ifft2` is the inverse. + ifftn : The inverse of the N-D FFT. + fft : The 1-D FFT. + ifft : The 1-D inverse FFT. + + Notes + ----- + `ifft2` is just `ifftn` with a different default for `axes`. + + See `ifftn` for details and a plotting example, and `fft` for + definition and conventions used. + + Zero-padding, analogously with `ifft`, is performed by appending zeros to + the input along the specified dimension. Although this is the common + approach, it might lead to surprising results. If another form of zero + padding is desired, it must be performed before `ifft2` is called. + + Examples + -------- + >>> import scipy.fft + >>> import numpy as np + >>> x = 4 * np.eye(4) + >>> scipy.fft.ifft2(x) + array([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j], # may vary + [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j], + [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j], + [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j]]) + + """ + return (Dispatchable(x, np.ndarray),) + + +@_dispatch +def rfftn(x, s=None, axes=None, norm=None, overwrite_x=False, workers=None, *, + plan=None): + """ + Compute the N-D discrete Fourier Transform for real input. + + This function computes the N-D discrete Fourier Transform over + any number of axes in an M-D real array by means of the Fast + Fourier Transform (FFT). By default, all axes are transformed, with the + real transform performed over the last axis, while the remaining + transforms are complex. + + Parameters + ---------- + x : array_like + Input array, taken to be real. + s : sequence of ints, optional + Shape (length along each transformed axis) to use from the input. + (``s[0]`` refers to axis 0, ``s[1]`` to axis 1, etc.). + The final element of `s` corresponds to `n` for ``rfft(x, n)``, while + for the remaining axes, it corresponds to `n` for ``fft(x, n)``. + Along any axis, if the given shape is smaller than that of the input, + the input is cropped. If it is larger, the input is padded with zeros. + if `s` is not given, the shape of the input along the axes specified + by `axes` is used. + axes : sequence of ints, optional + Axes over which to compute the FFT. If not given, the last ``len(s)`` + axes are used, or all axes if `s` is also not specified. + norm : {"backward", "ortho", "forward"}, optional + Normalization mode (see `fft`). Default is "backward". + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + See :func:`fft` for more details. + workers : int, optional + Maximum number of workers to use for parallel computation. If negative, + the value wraps around from ``os.cpu_count()``. + See :func:`~scipy.fft.fft` for more details. + plan : object, optional + This argument is reserved for passing in a precomputed plan provided + by downstream FFT vendors. It is currently not used in SciPy. + + .. versionadded:: 1.5.0 + + Returns + ------- + out : complex ndarray + The truncated or zero-padded input, transformed along the axes + indicated by `axes`, or by a combination of `s` and `x`, + as explained in the parameters section above. + The length of the last axis transformed will be ``s[-1]//2+1``, + while the remaining transformed axes will have lengths according to + `s`, or unchanged from the input. + + Raises + ------ + ValueError + If `s` and `axes` have different length. + IndexError + If an element of `axes` is larger than the number of axes of `x`. + + See Also + -------- + irfftn : The inverse of `rfftn`, i.e., the inverse of the N-D FFT + of real input. + fft : The 1-D FFT, with definitions and conventions used. + rfft : The 1-D FFT of real input. + fftn : The N-D FFT. + rfft2 : The 2-D FFT of real input. + + Notes + ----- + The transform for real input is performed over the last transformation + axis, as by `rfft`, then the transform over the remaining axes is + performed as by `fftn`. The order of the output is as for `rfft` for the + final transformation axis, and as for `fftn` for the remaining + transformation axes. + + See `fft` for details, definitions and conventions used. + + Examples + -------- + >>> import scipy.fft + >>> import numpy as np + >>> x = np.ones((2, 2, 2)) + >>> scipy.fft.rfftn(x) + array([[[8.+0.j, 0.+0.j], # may vary + [0.+0.j, 0.+0.j]], + [[0.+0.j, 0.+0.j], + [0.+0.j, 0.+0.j]]]) + + >>> scipy.fft.rfftn(x, axes=(2, 0)) + array([[[4.+0.j, 0.+0.j], # may vary + [4.+0.j, 0.+0.j]], + [[0.+0.j, 0.+0.j], + [0.+0.j, 0.+0.j]]]) + + """ + return (Dispatchable(x, np.ndarray),) + + +@_dispatch +def rfft2(x, s=None, axes=(-2, -1), norm=None, overwrite_x=False, workers=None, *, + plan=None): + """ + Compute the 2-D FFT of a real array. + + Parameters + ---------- + x : array + Input array, taken to be real. + s : sequence of ints, optional + Shape of the FFT. + axes : sequence of ints, optional + Axes over which to compute the FFT. + norm : {"backward", "ortho", "forward"}, optional + Normalization mode (see `fft`). Default is "backward". + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + See :func:`fft` for more details. + workers : int, optional + Maximum number of workers to use for parallel computation. If negative, + the value wraps around from ``os.cpu_count()``. + See :func:`~scipy.fft.fft` for more details. + plan : object, optional + This argument is reserved for passing in a precomputed plan provided + by downstream FFT vendors. It is currently not used in SciPy. + + .. versionadded:: 1.5.0 + + Returns + ------- + out : ndarray + The result of the real 2-D FFT. + + See Also + -------- + irfft2 : The inverse of the 2-D FFT of real input. + rfft : The 1-D FFT of real input. + rfftn : Compute the N-D discrete Fourier Transform for real + input. + + Notes + ----- + This is really just `rfftn` with different default behavior. + For more details see `rfftn`. + + """ + return (Dispatchable(x, np.ndarray),) + + +@_dispatch +def irfftn(x, s=None, axes=None, norm=None, overwrite_x=False, workers=None, *, + plan=None): + """ + Computes the inverse of `rfftn` + + This function computes the inverse of the N-D discrete + Fourier Transform for real input over any number of axes in an + M-D array by means of the Fast Fourier Transform (FFT). In + other words, ``irfftn(rfftn(x), x.shape) == x`` to within numerical + accuracy. (The ``a.shape`` is necessary like ``len(a)`` is for `irfft`, + and for the same reason.) + + The input should be ordered in the same way as is returned by `rfftn`, + i.e., as for `irfft` for the final transformation axis, and as for `ifftn` + along all the other axes. + + Parameters + ---------- + x : array_like + Input array. + s : sequence of ints, optional + Shape (length of each transformed axis) of the output + (``s[0]`` refers to axis 0, ``s[1]`` to axis 1, etc.). `s` is also the + number of input points used along this axis, except for the last axis, + where ``s[-1]//2+1`` points of the input are used. + Along any axis, if the shape indicated by `s` is smaller than that of + the input, the input is cropped. If it is larger, the input is padded + with zeros. If `s` is not given, the shape of the input along the axes + specified by axes is used. Except for the last axis which is taken to be + ``2*(m-1)``, where ``m`` is the length of the input along that axis. + axes : sequence of ints, optional + Axes over which to compute the inverse FFT. If not given, the last + `len(s)` axes are used, or all axes if `s` is also not specified. + norm : {"backward", "ortho", "forward"}, optional + Normalization mode (see `fft`). Default is "backward". + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + See :func:`fft` for more details. + workers : int, optional + Maximum number of workers to use for parallel computation. If negative, + the value wraps around from ``os.cpu_count()``. + See :func:`~scipy.fft.fft` for more details. + plan : object, optional + This argument is reserved for passing in a precomputed plan provided + by downstream FFT vendors. It is currently not used in SciPy. + + .. versionadded:: 1.5.0 + + Returns + ------- + out : ndarray + The truncated or zero-padded input, transformed along the axes + indicated by `axes`, or by a combination of `s` or `x`, + as explained in the parameters section above. + The length of each transformed axis is as given by the corresponding + element of `s`, or the length of the input in every axis except for the + last one if `s` is not given. In the final transformed axis the length + of the output when `s` is not given is ``2*(m-1)``, where ``m`` is the + length of the final transformed axis of the input. To get an odd + number of output points in the final axis, `s` must be specified. + + Raises + ------ + ValueError + If `s` and `axes` have different length. + IndexError + If an element of `axes` is larger than the number of axes of `x`. + + See Also + -------- + rfftn : The forward N-D FFT of real input, + of which `ifftn` is the inverse. + fft : The 1-D FFT, with definitions and conventions used. + irfft : The inverse of the 1-D FFT of real input. + irfft2 : The inverse of the 2-D FFT of real input. + + Notes + ----- + See `fft` for definitions and conventions used. + + See `rfft` for definitions and conventions used for real input. + + The default value of `s` assumes an even output length in the final + transformation axis. When performing the final complex to real + transformation, the Hermitian symmetry requires that the last imaginary + component along that axis must be 0 and so it is ignored. To avoid losing + information, the correct length of the real input *must* be given. + + Examples + -------- + >>> import scipy.fft + >>> import numpy as np + >>> x = np.zeros((3, 2, 2)) + >>> x[0, 0, 0] = 3 * 2 * 2 + >>> scipy.fft.irfftn(x) + array([[[1., 1.], + [1., 1.]], + [[1., 1.], + [1., 1.]], + [[1., 1.], + [1., 1.]]]) + + """ + return (Dispatchable(x, np.ndarray),) + + +@_dispatch +def irfft2(x, s=None, axes=(-2, -1), norm=None, overwrite_x=False, workers=None, *, + plan=None): + """ + Computes the inverse of `rfft2` + + Parameters + ---------- + x : array_like + The input array + s : sequence of ints, optional + Shape of the real output to the inverse FFT. + axes : sequence of ints, optional + The axes over which to compute the inverse fft. + Default is the last two axes. + norm : {"backward", "ortho", "forward"}, optional + Normalization mode (see `fft`). Default is "backward". + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + See :func:`fft` for more details. + workers : int, optional + Maximum number of workers to use for parallel computation. If negative, + the value wraps around from ``os.cpu_count()``. + See :func:`~scipy.fft.fft` for more details. + plan : object, optional + This argument is reserved for passing in a precomputed plan provided + by downstream FFT vendors. It is currently not used in SciPy. + + .. versionadded:: 1.5.0 + + Returns + ------- + out : ndarray + The result of the inverse real 2-D FFT. + + See Also + -------- + rfft2 : The 2-D FFT of real input. + irfft : The inverse of the 1-D FFT of real input. + irfftn : The inverse of the N-D FFT of real input. + + Notes + ----- + This is really `irfftn` with different defaults. + For more details see `irfftn`. + + """ + return (Dispatchable(x, np.ndarray),) + + +@_dispatch +def hfftn(x, s=None, axes=None, norm=None, overwrite_x=False, workers=None, *, + plan=None): + """ + Compute the N-D FFT of Hermitian symmetric complex input, i.e., a + signal with a real spectrum. + + This function computes the N-D discrete Fourier Transform for a + Hermitian symmetric complex input over any number of axes in an + M-D array by means of the Fast Fourier Transform (FFT). In other + words, ``ihfftn(hfftn(x, s)) == x`` to within numerical accuracy. (``s`` + here is ``x.shape`` with ``s[-1] = x.shape[-1] * 2 - 1``, this is necessary + for the same reason ``x.shape`` would be necessary for `irfft`.) + + Parameters + ---------- + x : array_like + Input array. + s : sequence of ints, optional + Shape (length of each transformed axis) of the output + (``s[0]`` refers to axis 0, ``s[1]`` to axis 1, etc.). `s` is also the + number of input points used along this axis, except for the last axis, + where ``s[-1]//2+1`` points of the input are used. + Along any axis, if the shape indicated by `s` is smaller than that of + the input, the input is cropped. If it is larger, the input is padded + with zeros. If `s` is not given, the shape of the input along the axes + specified by axes is used. Except for the last axis which is taken to be + ``2*(m-1)`` where ``m`` is the length of the input along that axis. + axes : sequence of ints, optional + Axes over which to compute the inverse FFT. If not given, the last + `len(s)` axes are used, or all axes if `s` is also not specified. + norm : {"backward", "ortho", "forward"}, optional + Normalization mode (see `fft`). Default is "backward". + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + See :func:`fft` for more details. + workers : int, optional + Maximum number of workers to use for parallel computation. If negative, + the value wraps around from ``os.cpu_count()``. + See :func:`~scipy.fft.fft` for more details. + plan : object, optional + This argument is reserved for passing in a precomputed plan provided + by downstream FFT vendors. It is currently not used in SciPy. + + .. versionadded:: 1.5.0 + + Returns + ------- + out : ndarray + The truncated or zero-padded input, transformed along the axes + indicated by `axes`, or by a combination of `s` or `x`, + as explained in the parameters section above. + The length of each transformed axis is as given by the corresponding + element of `s`, or the length of the input in every axis except for the + last one if `s` is not given. In the final transformed axis the length + of the output when `s` is not given is ``2*(m-1)`` where ``m`` is the + length of the final transformed axis of the input. To get an odd + number of output points in the final axis, `s` must be specified. + + Raises + ------ + ValueError + If `s` and `axes` have different length. + IndexError + If an element of `axes` is larger than the number of axes of `x`. + + See Also + -------- + ihfftn : The inverse N-D FFT with real spectrum. Inverse of `hfftn`. + fft : The 1-D FFT, with definitions and conventions used. + rfft : Forward FFT of real input. + + Notes + ----- + For a 1-D signal ``x`` to have a real spectrum, it must satisfy + the Hermitian property:: + + x[i] == np.conj(x[-i]) for all i + + This generalizes into higher dimensions by reflecting over each axis in + turn:: + + x[i, j, k, ...] == np.conj(x[-i, -j, -k, ...]) for all i, j, k, ... + + This should not be confused with a Hermitian matrix, for which the + transpose is its own conjugate:: + + x[i, j] == np.conj(x[j, i]) for all i, j + + + The default value of `s` assumes an even output length in the final + transformation axis. When performing the final complex to real + transformation, the Hermitian symmetry requires that the last imaginary + component along that axis must be 0 and so it is ignored. To avoid losing + information, the correct length of the real input *must* be given. + + Examples + -------- + >>> import scipy.fft + >>> import numpy as np + >>> x = np.ones((3, 2, 2)) + >>> scipy.fft.hfftn(x) + array([[[12., 0.], + [ 0., 0.]], + [[ 0., 0.], + [ 0., 0.]], + [[ 0., 0.], + [ 0., 0.]]]) + + """ + return (Dispatchable(x, np.ndarray),) + + +@_dispatch +def hfft2(x, s=None, axes=(-2, -1), norm=None, overwrite_x=False, workers=None, *, + plan=None): + """ + Compute the 2-D FFT of a Hermitian complex array. + + Parameters + ---------- + x : array + Input array, taken to be Hermitian complex. + s : sequence of ints, optional + Shape of the real output. + axes : sequence of ints, optional + Axes over which to compute the FFT. + norm : {"backward", "ortho", "forward"}, optional + Normalization mode (see `fft`). Default is "backward". + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + See `fft` for more details. + workers : int, optional + Maximum number of workers to use for parallel computation. If negative, + the value wraps around from ``os.cpu_count()``. + See :func:`~scipy.fft.fft` for more details. + plan : object, optional + This argument is reserved for passing in a precomputed plan provided + by downstream FFT vendors. It is currently not used in SciPy. + + .. versionadded:: 1.5.0 + + Returns + ------- + out : ndarray + The real result of the 2-D Hermitian complex real FFT. + + See Also + -------- + hfftn : Compute the N-D discrete Fourier Transform for Hermitian + complex input. + + Notes + ----- + This is really just `hfftn` with different default behavior. + For more details see `hfftn`. + + """ + return (Dispatchable(x, np.ndarray),) + + +@_dispatch +def ihfftn(x, s=None, axes=None, norm=None, overwrite_x=False, workers=None, *, + plan=None): + """ + Compute the N-D inverse discrete Fourier Transform for a real + spectrum. + + This function computes the N-D inverse discrete Fourier Transform + over any number of axes in an M-D real array by means of the Fast + Fourier Transform (FFT). By default, all axes are transformed, with the + real transform performed over the last axis, while the remaining transforms + are complex. + + Parameters + ---------- + x : array_like + Input array, taken to be real. + s : sequence of ints, optional + Shape (length along each transformed axis) to use from the input. + (``s[0]`` refers to axis 0, ``s[1]`` to axis 1, etc.). + Along any axis, if the given shape is smaller than that of the input, + the input is cropped. If it is larger, the input is padded with zeros. + if `s` is not given, the shape of the input along the axes specified + by `axes` is used. + axes : sequence of ints, optional + Axes over which to compute the FFT. If not given, the last ``len(s)`` + axes are used, or all axes if `s` is also not specified. + norm : {"backward", "ortho", "forward"}, optional + Normalization mode (see `fft`). Default is "backward". + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + See :func:`fft` for more details. + workers : int, optional + Maximum number of workers to use for parallel computation. If negative, + the value wraps around from ``os.cpu_count()``. + See :func:`~scipy.fft.fft` for more details. + plan : object, optional + This argument is reserved for passing in a precomputed plan provided + by downstream FFT vendors. It is currently not used in SciPy. + + .. versionadded:: 1.5.0 + + Returns + ------- + out : complex ndarray + The truncated or zero-padded input, transformed along the axes + indicated by `axes`, or by a combination of `s` and `x`, + as explained in the parameters section above. + The length of the last axis transformed will be ``s[-1]//2+1``, + while the remaining transformed axes will have lengths according to + `s`, or unchanged from the input. + + Raises + ------ + ValueError + If `s` and `axes` have different length. + IndexError + If an element of `axes` is larger than the number of axes of `x`. + + See Also + -------- + hfftn : The forward N-D FFT of Hermitian input. + hfft : The 1-D FFT of Hermitian input. + fft : The 1-D FFT, with definitions and conventions used. + fftn : The N-D FFT. + hfft2 : The 2-D FFT of Hermitian input. + + Notes + ----- + The transform for real input is performed over the last transformation + axis, as by `ihfft`, then the transform over the remaining axes is + performed as by `ifftn`. The order of the output is the positive part of + the Hermitian output signal, in the same format as `rfft`. + + Examples + -------- + >>> import scipy.fft + >>> import numpy as np + >>> x = np.ones((2, 2, 2)) + >>> scipy.fft.ihfftn(x) + array([[[1.+0.j, 0.+0.j], # may vary + [0.+0.j, 0.+0.j]], + [[0.+0.j, 0.+0.j], + [0.+0.j, 0.+0.j]]]) + >>> scipy.fft.ihfftn(x, axes=(2, 0)) + array([[[1.+0.j, 0.+0.j], # may vary + [1.+0.j, 0.+0.j]], + [[0.+0.j, 0.+0.j], + [0.+0.j, 0.+0.j]]]) + + """ + return (Dispatchable(x, np.ndarray),) + + +@_dispatch +def ihfft2(x, s=None, axes=(-2, -1), norm=None, overwrite_x=False, workers=None, *, + plan=None): + """ + Compute the 2-D inverse FFT of a real spectrum. + + Parameters + ---------- + x : array_like + The input array + s : sequence of ints, optional + Shape of the real input to the inverse FFT. + axes : sequence of ints, optional + The axes over which to compute the inverse fft. + Default is the last two axes. + norm : {"backward", "ortho", "forward"}, optional + Normalization mode (see `fft`). Default is "backward". + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + See :func:`fft` for more details. + workers : int, optional + Maximum number of workers to use for parallel computation. If negative, + the value wraps around from ``os.cpu_count()``. + See :func:`~scipy.fft.fft` for more details. + plan : object, optional + This argument is reserved for passing in a precomputed plan provided + by downstream FFT vendors. It is currently not used in SciPy. + + .. versionadded:: 1.5.0 + + Returns + ------- + out : ndarray + The result of the inverse real 2-D FFT. + + See Also + -------- + ihfftn : Compute the inverse of the N-D FFT of Hermitian input. + + Notes + ----- + This is really `ihfftn` with different defaults. + For more details see `ihfftn`. + + """ + return (Dispatchable(x, np.ndarray),) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_basic_backend.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_basic_backend.py new file mode 100644 index 0000000000000000000000000000000000000000..775b26a8bf5922f7fa3634ad6c8c708a96820931 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_basic_backend.py @@ -0,0 +1,197 @@ +from scipy._lib._array_api import ( + array_namespace, is_numpy, xp_unsupported_param_msg, is_complex, xp_float_to_complex +) +from . import _pocketfft +import numpy as np + + +def _validate_fft_args(workers, plan, norm): + if workers is not None: + raise ValueError(xp_unsupported_param_msg("workers")) + if plan is not None: + raise ValueError(xp_unsupported_param_msg("plan")) + if norm is None: + norm = 'backward' + return norm + + +# these functions expect complex input in the fft standard extension +complex_funcs = {'fft', 'ifft', 'fftn', 'ifftn', 'hfft', 'irfft', 'irfftn'} + +# pocketfft is used whenever SCIPY_ARRAY_API is not set, +# or x is a NumPy array or array-like. +# When SCIPY_ARRAY_API is set, we try to use xp.fft for CuPy arrays, +# PyTorch arrays and other array API standard supporting objects. +# If xp.fft does not exist, we attempt to convert to np and back to use pocketfft. + +def _execute_1D(func_str, pocketfft_func, x, n, axis, norm, overwrite_x, workers, plan): + xp = array_namespace(x) + + if is_numpy(xp): + x = np.asarray(x) + return pocketfft_func(x, n=n, axis=axis, norm=norm, + overwrite_x=overwrite_x, workers=workers, plan=plan) + + norm = _validate_fft_args(workers, plan, norm) + if hasattr(xp, 'fft'): + xp_func = getattr(xp.fft, func_str) + if func_str in complex_funcs: + try: + res = xp_func(x, n=n, axis=axis, norm=norm) + except: # backends may require complex input # noqa: E722 + x = xp_float_to_complex(x, xp) + res = xp_func(x, n=n, axis=axis, norm=norm) + return res + return xp_func(x, n=n, axis=axis, norm=norm) + + x = np.asarray(x) + y = pocketfft_func(x, n=n, axis=axis, norm=norm) + return xp.asarray(y) + + +def _execute_nD(func_str, pocketfft_func, x, s, axes, norm, overwrite_x, workers, plan): + xp = array_namespace(x) + + if is_numpy(xp): + x = np.asarray(x) + return pocketfft_func(x, s=s, axes=axes, norm=norm, + overwrite_x=overwrite_x, workers=workers, plan=plan) + + norm = _validate_fft_args(workers, plan, norm) + if hasattr(xp, 'fft'): + xp_func = getattr(xp.fft, func_str) + if func_str in complex_funcs: + try: + res = xp_func(x, s=s, axes=axes, norm=norm) + except: # backends may require complex input # noqa: E722 + x = xp_float_to_complex(x, xp) + res = xp_func(x, s=s, axes=axes, norm=norm) + return res + return xp_func(x, s=s, axes=axes, norm=norm) + + x = np.asarray(x) + y = pocketfft_func(x, s=s, axes=axes, norm=norm) + return xp.asarray(y) + + +def fft(x, n=None, axis=-1, norm=None, + overwrite_x=False, workers=None, *, plan=None): + return _execute_1D('fft', _pocketfft.fft, x, n=n, axis=axis, norm=norm, + overwrite_x=overwrite_x, workers=workers, plan=plan) + + +def ifft(x, n=None, axis=-1, norm=None, overwrite_x=False, workers=None, *, + plan=None): + return _execute_1D('ifft', _pocketfft.ifft, x, n=n, axis=axis, norm=norm, + overwrite_x=overwrite_x, workers=workers, plan=plan) + + +def rfft(x, n=None, axis=-1, norm=None, + overwrite_x=False, workers=None, *, plan=None): + return _execute_1D('rfft', _pocketfft.rfft, x, n=n, axis=axis, norm=norm, + overwrite_x=overwrite_x, workers=workers, plan=plan) + + +def irfft(x, n=None, axis=-1, norm=None, + overwrite_x=False, workers=None, *, plan=None): + return _execute_1D('irfft', _pocketfft.irfft, x, n=n, axis=axis, norm=norm, + overwrite_x=overwrite_x, workers=workers, plan=plan) + + +def hfft(x, n=None, axis=-1, norm=None, + overwrite_x=False, workers=None, *, plan=None): + return _execute_1D('hfft', _pocketfft.hfft, x, n=n, axis=axis, norm=norm, + overwrite_x=overwrite_x, workers=workers, plan=plan) + + +def ihfft(x, n=None, axis=-1, norm=None, + overwrite_x=False, workers=None, *, plan=None): + return _execute_1D('ihfft', _pocketfft.ihfft, x, n=n, axis=axis, norm=norm, + overwrite_x=overwrite_x, workers=workers, plan=plan) + + +def fftn(x, s=None, axes=None, norm=None, + overwrite_x=False, workers=None, *, plan=None): + return _execute_nD('fftn', _pocketfft.fftn, x, s=s, axes=axes, norm=norm, + overwrite_x=overwrite_x, workers=workers, plan=plan) + + + +def ifftn(x, s=None, axes=None, norm=None, + overwrite_x=False, workers=None, *, plan=None): + return _execute_nD('ifftn', _pocketfft.ifftn, x, s=s, axes=axes, norm=norm, + overwrite_x=overwrite_x, workers=workers, plan=plan) + + +def fft2(x, s=None, axes=(-2, -1), norm=None, + overwrite_x=False, workers=None, *, plan=None): + return fftn(x, s, axes, norm, overwrite_x, workers, plan=plan) + + +def ifft2(x, s=None, axes=(-2, -1), norm=None, + overwrite_x=False, workers=None, *, plan=None): + return ifftn(x, s, axes, norm, overwrite_x, workers, plan=plan) + + +def rfftn(x, s=None, axes=None, norm=None, + overwrite_x=False, workers=None, *, plan=None): + return _execute_nD('rfftn', _pocketfft.rfftn, x, s=s, axes=axes, norm=norm, + overwrite_x=overwrite_x, workers=workers, plan=plan) + + +def rfft2(x, s=None, axes=(-2, -1), norm=None, + overwrite_x=False, workers=None, *, plan=None): + return rfftn(x, s, axes, norm, overwrite_x, workers, plan=plan) + + +def irfftn(x, s=None, axes=None, norm=None, + overwrite_x=False, workers=None, *, plan=None): + return _execute_nD('irfftn', _pocketfft.irfftn, x, s=s, axes=axes, norm=norm, + overwrite_x=overwrite_x, workers=workers, plan=plan) + + +def irfft2(x, s=None, axes=(-2, -1), norm=None, + overwrite_x=False, workers=None, *, plan=None): + return irfftn(x, s, axes, norm, overwrite_x, workers, plan=plan) + + +def _swap_direction(norm): + if norm in (None, 'backward'): + norm = 'forward' + elif norm == 'forward': + norm = 'backward' + elif norm != 'ortho': + raise ValueError(f'Invalid norm value {norm}; should be "backward", ' + '"ortho", or "forward".') + return norm + + +def hfftn(x, s=None, axes=None, norm=None, + overwrite_x=False, workers=None, *, plan=None): + xp = array_namespace(x) + if is_numpy(xp): + x = np.asarray(x) + return _pocketfft.hfftn(x, s, axes, norm, overwrite_x, workers, plan=plan) + if is_complex(x, xp): + x = xp.conj(x) + return irfftn(x, s, axes, _swap_direction(norm), + overwrite_x, workers, plan=plan) + + +def hfft2(x, s=None, axes=(-2, -1), norm=None, + overwrite_x=False, workers=None, *, plan=None): + return hfftn(x, s, axes, norm, overwrite_x, workers, plan=plan) + + +def ihfftn(x, s=None, axes=None, norm=None, + overwrite_x=False, workers=None, *, plan=None): + xp = array_namespace(x) + if is_numpy(xp): + x = np.asarray(x) + return _pocketfft.ihfftn(x, s, axes, norm, overwrite_x, workers, plan=plan) + return xp.conj(rfftn(x, s, axes, _swap_direction(norm), + overwrite_x, workers, plan=plan)) + +def ihfft2(x, s=None, axes=(-2, -1), norm=None, + overwrite_x=False, workers=None, *, plan=None): + return ihfftn(x, s, axes, norm, overwrite_x, workers, plan=plan) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_debug_backends.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_debug_backends.py new file mode 100644 index 0000000000000000000000000000000000000000..c9647c5d6ceddc73b97d95f562662ada02c1ae74 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_debug_backends.py @@ -0,0 +1,22 @@ +import numpy as np + +class NumPyBackend: + """Backend that uses numpy.fft""" + __ua_domain__ = "numpy.scipy.fft" + + @staticmethod + def __ua_function__(method, args, kwargs): + kwargs.pop("overwrite_x", None) + + fn = getattr(np.fft, method.__name__, None) + return (NotImplemented if fn is None + else fn(*args, **kwargs)) + + +class EchoBackend: + """Backend that just prints the __ua_function__ arguments""" + __ua_domain__ = "numpy.scipy.fft" + + @staticmethod + def __ua_function__(method, args, kwargs): + print(method, args, kwargs, sep='\n') diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_fftlog.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_fftlog.py new file mode 100644 index 0000000000000000000000000000000000000000..61bbe802bbb4a40df8ca3b653c693105b29c6578 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_fftlog.py @@ -0,0 +1,223 @@ +"""Fast Hankel transforms using the FFTLog algorithm. + +The implementation closely follows the Fortran code of Hamilton (2000). + +added: 14/11/2020 Nicolas Tessore +""" + +from ._basic import _dispatch +from scipy._lib.uarray import Dispatchable +from ._fftlog_backend import fhtoffset +import numpy as np + +__all__ = ['fht', 'ifht', 'fhtoffset'] + + +@_dispatch +def fht(a, dln, mu, offset=0.0, bias=0.0): + r'''Compute the fast Hankel transform. + + Computes the discrete Hankel transform of a logarithmically spaced periodic + sequence using the FFTLog algorithm [1]_, [2]_. + + Parameters + ---------- + a : array_like (..., n) + Real periodic input array, uniformly logarithmically spaced. For + multidimensional input, the transform is performed over the last axis. + dln : float + Uniform logarithmic spacing of the input array. + mu : float + Order of the Hankel transform, any positive or negative real number. + offset : float, optional + Offset of the uniform logarithmic spacing of the output array. + bias : float, optional + Exponent of power law bias, any positive or negative real number. + + Returns + ------- + A : array_like (..., n) + The transformed output array, which is real, periodic, uniformly + logarithmically spaced, and of the same shape as the input array. + + See Also + -------- + ifht : The inverse of `fht`. + fhtoffset : Return an optimal offset for `fht`. + + Notes + ----- + This function computes a discrete version of the Hankel transform + + .. math:: + + A(k) = \int_{0}^{\infty} \! a(r) \, J_\mu(kr) \, k \, dr \;, + + where :math:`J_\mu` is the Bessel function of order :math:`\mu`. The index + :math:`\mu` may be any real number, positive or negative. Note that the + numerical Hankel transform uses an integrand of :math:`k \, dr`, while the + mathematical Hankel transform is commonly defined using :math:`r \, dr`. + + The input array `a` is a periodic sequence of length :math:`n`, uniformly + logarithmically spaced with spacing `dln`, + + .. math:: + + a_j = a(r_j) \;, \quad + r_j = r_c \exp[(j-j_c) \, \mathtt{dln}] + + centred about the point :math:`r_c`. Note that the central index + :math:`j_c = (n-1)/2` is half-integral if :math:`n` is even, so that + :math:`r_c` falls between two input elements. Similarly, the output + array `A` is a periodic sequence of length :math:`n`, also uniformly + logarithmically spaced with spacing `dln` + + .. math:: + + A_j = A(k_j) \;, \quad + k_j = k_c \exp[(j-j_c) \, \mathtt{dln}] + + centred about the point :math:`k_c`. + + The centre points :math:`r_c` and :math:`k_c` of the periodic intervals may + be chosen arbitrarily, but it would be usual to choose the product + :math:`k_c r_c = k_j r_{n-1-j} = k_{n-1-j} r_j` to be unity. This can be + changed using the `offset` parameter, which controls the logarithmic offset + :math:`\log(k_c) = \mathtt{offset} - \log(r_c)` of the output array. + Choosing an optimal value for `offset` may reduce ringing of the discrete + Hankel transform. + + If the `bias` parameter is nonzero, this function computes a discrete + version of the biased Hankel transform + + .. math:: + + A(k) = \int_{0}^{\infty} \! a_q(r) \, (kr)^q \, J_\mu(kr) \, k \, dr + + where :math:`q` is the value of `bias`, and a power law bias + :math:`a_q(r) = a(r) \, (kr)^{-q}` is applied to the input sequence. + Biasing the transform can help approximate the continuous transform of + :math:`a(r)` if there is a value :math:`q` such that :math:`a_q(r)` is + close to a periodic sequence, in which case the resulting :math:`A(k)` will + be close to the continuous transform. + + References + ---------- + .. [1] Talman J. D., 1978, J. Comp. Phys., 29, 35 + .. [2] Hamilton A. J. S., 2000, MNRAS, 312, 257 (astro-ph/9905191) + + Examples + -------- + + This example is the adapted version of ``fftlogtest.f`` which is provided + in [2]_. It evaluates the integral + + .. math:: + + \int^\infty_0 r^{\mu+1} \exp(-r^2/2) J_\mu(kr) k dr + = k^{\mu+1} \exp(-k^2/2) . + + >>> import numpy as np + >>> from scipy import fft + >>> import matplotlib.pyplot as plt + + Parameters for the transform. + + >>> mu = 0.0 # Order mu of Bessel function + >>> r = np.logspace(-7, 1, 128) # Input evaluation points + >>> dln = np.log(r[1]/r[0]) # Step size + >>> offset = fft.fhtoffset(dln, initial=-6*np.log(10), mu=mu) + >>> k = np.exp(offset)/r[::-1] # Output evaluation points + + Define the analytical function. + + >>> def f(x, mu): + ... """Analytical function: x^(mu+1) exp(-x^2/2).""" + ... return x**(mu + 1)*np.exp(-x**2/2) + + Evaluate the function at ``r`` and compute the corresponding values at + ``k`` using FFTLog. + + >>> a_r = f(r, mu) + >>> fht = fft.fht(a_r, dln, mu=mu, offset=offset) + + For this example we can actually compute the analytical response (which in + this case is the same as the input function) for comparison and compute the + relative error. + + >>> a_k = f(k, mu) + >>> rel_err = abs((fht-a_k)/a_k) + + Plot the result. + + >>> figargs = {'sharex': True, 'sharey': True, 'constrained_layout': True} + >>> fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4), **figargs) + >>> ax1.set_title(r'$r^{\mu+1}\ \exp(-r^2/2)$') + >>> ax1.loglog(r, a_r, 'k', lw=2) + >>> ax1.set_xlabel('r') + >>> ax2.set_title(r'$k^{\mu+1} \exp(-k^2/2)$') + >>> ax2.loglog(k, a_k, 'k', lw=2, label='Analytical') + >>> ax2.loglog(k, fht, 'C3--', lw=2, label='FFTLog') + >>> ax2.set_xlabel('k') + >>> ax2.legend(loc=3, framealpha=1) + >>> ax2.set_ylim([1e-10, 1e1]) + >>> ax2b = ax2.twinx() + >>> ax2b.loglog(k, rel_err, 'C0', label='Rel. Error (-)') + >>> ax2b.set_ylabel('Rel. Error (-)', color='C0') + >>> ax2b.tick_params(axis='y', labelcolor='C0') + >>> ax2b.legend(loc=4, framealpha=1) + >>> ax2b.set_ylim([1e-9, 1e-3]) + >>> plt.show() + + ''' + return (Dispatchable(a, np.ndarray),) + + +@_dispatch +def ifht(A, dln, mu, offset=0.0, bias=0.0): + r"""Compute the inverse fast Hankel transform. + + Computes the discrete inverse Hankel transform of a logarithmically spaced + periodic sequence. This is the inverse operation to `fht`. + + Parameters + ---------- + A : array_like (..., n) + Real periodic input array, uniformly logarithmically spaced. For + multidimensional input, the transform is performed over the last axis. + dln : float + Uniform logarithmic spacing of the input array. + mu : float + Order of the Hankel transform, any positive or negative real number. + offset : float, optional + Offset of the uniform logarithmic spacing of the output array. + bias : float, optional + Exponent of power law bias, any positive or negative real number. + + Returns + ------- + a : array_like (..., n) + The transformed output array, which is real, periodic, uniformly + logarithmically spaced, and of the same shape as the input array. + + See Also + -------- + fht : Definition of the fast Hankel transform. + fhtoffset : Return an optimal offset for `ifht`. + + Notes + ----- + This function computes a discrete version of the Hankel transform + + .. math:: + + a(r) = \int_{0}^{\infty} \! A(k) \, J_\mu(kr) \, r \, dk \;, + + where :math:`J_\mu` is the Bessel function of order :math:`\mu`. The index + :math:`\mu` may be any real number, positive or negative. Note that the + numerical inverse Hankel transform uses an integrand of :math:`r \, dk`, while the + mathematical inverse Hankel transform is commonly defined using :math:`k \, dk`. + + See `fht` for further details. + """ + return (Dispatchable(A, np.ndarray),) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_fftlog_backend.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_fftlog_backend.py new file mode 100644 index 0000000000000000000000000000000000000000..0b38733aaa349c301ba7d5b83691c55204dc8991 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_fftlog_backend.py @@ -0,0 +1,200 @@ +import numpy as np +from warnings import warn +from ._basic import rfft, irfft +from ..special import loggamma, poch + +from scipy._lib._array_api import array_namespace + +__all__ = ['fht', 'ifht', 'fhtoffset'] + +# constants +LN_2 = np.log(2) + + +def fht(a, dln, mu, offset=0.0, bias=0.0): + xp = array_namespace(a) + a = xp.asarray(a) + + # size of transform + n = a.shape[-1] + + # bias input array + if bias != 0: + # a_q(r) = a(r) (r/r_c)^{-q} + j_c = (n-1)/2 + j = xp.arange(n, dtype=xp.float64) + a = a * xp.exp(-bias*(j - j_c)*dln) + + # compute FHT coefficients + u = xp.asarray(fhtcoeff(n, dln, mu, offset=offset, bias=bias)) + + # transform + A = _fhtq(a, u, xp=xp) + + # bias output array + if bias != 0: + # A(k) = A_q(k) (k/k_c)^{-q} (k_c r_c)^{-q} + A *= xp.exp(-bias*((j - j_c)*dln + offset)) + + return A + + +def ifht(A, dln, mu, offset=0.0, bias=0.0): + xp = array_namespace(A) + A = xp.asarray(A) + + # size of transform + n = A.shape[-1] + + # bias input array + if bias != 0: + # A_q(k) = A(k) (k/k_c)^{q} (k_c r_c)^{q} + j_c = (n-1)/2 + j = xp.arange(n, dtype=xp.float64) + A = A * xp.exp(bias*((j - j_c)*dln + offset)) + + # compute FHT coefficients + u = xp.asarray(fhtcoeff(n, dln, mu, offset=offset, bias=bias, inverse=True)) + + # transform + a = _fhtq(A, u, inverse=True, xp=xp) + + # bias output array + if bias != 0: + # a(r) = a_q(r) (r/r_c)^{q} + a /= xp.exp(-bias*(j - j_c)*dln) + + return a + + +def fhtcoeff(n, dln, mu, offset=0.0, bias=0.0, inverse=False): + """Compute the coefficient array for a fast Hankel transform.""" + lnkr, q = offset, bias + + # Hankel transform coefficients + # u_m = (kr)^{-i 2m pi/(n dlnr)} U_mu(q + i 2m pi/(n dlnr)) + # with U_mu(x) = 2^x Gamma((mu+1+x)/2)/Gamma((mu+1-x)/2) + xp = (mu+1+q)/2 + xm = (mu+1-q)/2 + y = np.linspace(0, np.pi*(n//2)/(n*dln), n//2+1) + u = np.empty(n//2+1, dtype=complex) + v = np.empty(n//2+1, dtype=complex) + u.imag[:] = y + u.real[:] = xm + loggamma(u, out=v) + u.real[:] = xp + loggamma(u, out=u) + y *= 2*(LN_2 - lnkr) + u.real -= v.real + u.real += LN_2*q + u.imag += v.imag + u.imag += y + np.exp(u, out=u) + + # fix last coefficient to be real + if n % 2 == 0: + u.imag[-1] = 0 + + # deal with special cases + if not np.isfinite(u[0]): + # write u_0 = 2^q Gamma(xp)/Gamma(xm) = 2^q poch(xm, xp-xm) + # poch() handles special cases for negative integers correctly + u[0] = 2**q * poch(xm, xp-xm) + # the coefficient may be inf or 0, meaning the transform or the + # inverse transform, respectively, is singular + + # check for singular transform or singular inverse transform + if np.isinf(u[0]) and not inverse: + warn('singular transform; consider changing the bias', stacklevel=3) + # fix coefficient to obtain (potentially correct) transform anyway + u = np.copy(u) + u[0] = 0 + elif u[0] == 0 and inverse: + warn('singular inverse transform; consider changing the bias', stacklevel=3) + # fix coefficient to obtain (potentially correct) inverse anyway + u = np.copy(u) + u[0] = np.inf + + return u + + +def fhtoffset(dln, mu, initial=0.0, bias=0.0): + """Return optimal offset for a fast Hankel transform. + + Returns an offset close to `initial` that fulfils the low-ringing + condition of [1]_ for the fast Hankel transform `fht` with logarithmic + spacing `dln`, order `mu` and bias `bias`. + + Parameters + ---------- + dln : float + Uniform logarithmic spacing of the transform. + mu : float + Order of the Hankel transform, any positive or negative real number. + initial : float, optional + Initial value for the offset. Returns the closest value that fulfils + the low-ringing condition. + bias : float, optional + Exponent of power law bias, any positive or negative real number. + + Returns + ------- + offset : float + Optimal offset of the uniform logarithmic spacing of the transform that + fulfils a low-ringing condition. + + Examples + -------- + >>> from scipy.fft import fhtoffset + >>> dln = 0.1 + >>> mu = 2.0 + >>> initial = 0.5 + >>> bias = 0.0 + >>> offset = fhtoffset(dln, mu, initial, bias) + >>> offset + 0.5454581477676637 + + See Also + -------- + fht : Definition of the fast Hankel transform. + + References + ---------- + .. [1] Hamilton A. J. S., 2000, MNRAS, 312, 257 (astro-ph/9905191) + + """ + + lnkr, q = initial, bias + + xp = (mu+1+q)/2 + xm = (mu+1-q)/2 + y = np.pi/(2*dln) + zp = loggamma(xp + 1j*y) + zm = loggamma(xm + 1j*y) + arg = (LN_2 - lnkr)/dln + (zp.imag + zm.imag)/np.pi + return lnkr + (arg - np.round(arg))*dln + + +def _fhtq(a, u, inverse=False, *, xp=None): + """Compute the biased fast Hankel transform. + + This is the basic FFTLog routine. + """ + if xp is None: + xp = np + + # size of transform + n = a.shape[-1] + + # biased fast Hankel transform via real FFT + A = rfft(a, axis=-1) + if not inverse: + # forward transform + A *= u + else: + # backward transform + A /= xp.conj(u) + A = irfft(A, n, axis=-1) + A = xp.flip(A, axis=-1) + + return A diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_helper.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_helper.py new file mode 100644 index 0000000000000000000000000000000000000000..76e08c4f61c854f9bfb9407a1951f2cf5a2af123 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_helper.py @@ -0,0 +1,379 @@ +from functools import update_wrapper, lru_cache +import inspect + +from ._pocketfft import helper as _helper + +import numpy as np +from scipy._lib._array_api import array_namespace + + +def next_fast_len(target, real=False): + """Find the next fast size of input data to ``fft``, for zero-padding, etc. + + SciPy's FFT algorithms gain their speed by a recursive divide and conquer + strategy. This relies on efficient functions for small prime factors of the + input length. Thus, the transforms are fastest when using composites of the + prime factors handled by the fft implementation. If there are efficient + functions for all radices <= `n`, then the result will be a number `x` + >= ``target`` with only prime factors < `n`. (Also known as `n`-smooth + numbers) + + Parameters + ---------- + target : int + Length to start searching from. Must be a positive integer. + real : bool, optional + True if the FFT involves real input or output (e.g., `rfft` or `hfft` + but not `fft`). Defaults to False. + + Returns + ------- + out : int + The smallest fast length greater than or equal to ``target``. + + Notes + ----- + The result of this function may change in future as performance + considerations change, for example, if new prime factors are added. + + Calling `fft` or `ifft` with real input data performs an ``'R2C'`` + transform internally. + + Examples + -------- + On a particular machine, an FFT of prime length takes 11.4 ms: + + >>> from scipy import fft + >>> import numpy as np + >>> rng = np.random.default_rng() + >>> min_len = 93059 # prime length is worst case for speed + >>> a = rng.standard_normal(min_len) + >>> b = fft.fft(a) + + Zero-padding to the next regular length reduces computation time to + 1.6 ms, a speedup of 7.3 times: + + >>> fft.next_fast_len(min_len, real=True) + 93312 + >>> b = fft.fft(a, 93312) + + Rounding up to the next power of 2 is not optimal, taking 3.0 ms to + compute; 1.9 times longer than the size given by ``next_fast_len``: + + >>> b = fft.fft(a, 131072) + + """ + pass + + +# Directly wrap the c-function good_size but take the docstring etc., from the +# next_fast_len function above +_sig = inspect.signature(next_fast_len) +next_fast_len = update_wrapper(lru_cache(_helper.good_size), next_fast_len) +next_fast_len.__wrapped__ = _helper.good_size +next_fast_len.__signature__ = _sig + + +def prev_fast_len(target, real=False): + """Find the previous fast size of input data to ``fft``. + Useful for discarding a minimal number of samples before FFT. + + SciPy's FFT algorithms gain their speed by a recursive divide and conquer + strategy. This relies on efficient functions for small prime factors of the + input length. Thus, the transforms are fastest when using composites of the + prime factors handled by the fft implementation. If there are efficient + functions for all radices <= `n`, then the result will be a number `x` + <= ``target`` with only prime factors <= `n`. (Also known as `n`-smooth + numbers) + + Parameters + ---------- + target : int + Maximum length to search until. Must be a positive integer. + real : bool, optional + True if the FFT involves real input or output (e.g., `rfft` or `hfft` + but not `fft`). Defaults to False. + + Returns + ------- + out : int + The largest fast length less than or equal to ``target``. + + Notes + ----- + The result of this function may change in future as performance + considerations change, for example, if new prime factors are added. + + Calling `fft` or `ifft` with real input data performs an ``'R2C'`` + transform internally. + + In the current implementation, prev_fast_len assumes radices of + 2,3,5,7,11 for complex FFT and 2,3,5 for real FFT. + + Examples + -------- + On a particular machine, an FFT of prime length takes 16.2 ms: + + >>> from scipy import fft + >>> import numpy as np + >>> rng = np.random.default_rng() + >>> max_len = 93059 # prime length is worst case for speed + >>> a = rng.standard_normal(max_len) + >>> b = fft.fft(a) + + Performing FFT on the maximum fast length less than max_len + reduces the computation time to 1.5 ms, a speedup of 10.5 times: + + >>> fft.prev_fast_len(max_len, real=True) + 92160 + >>> c = fft.fft(a[:92160]) # discard last 899 samples + + """ + pass + + +# Directly wrap the c-function prev_good_size but take the docstring etc., +# from the prev_fast_len function above +_sig_prev_fast_len = inspect.signature(prev_fast_len) +prev_fast_len = update_wrapper(lru_cache()(_helper.prev_good_size), prev_fast_len) +prev_fast_len.__wrapped__ = _helper.prev_good_size +prev_fast_len.__signature__ = _sig_prev_fast_len + + +def _init_nd_shape_and_axes(x, shape, axes): + """Handle shape and axes arguments for N-D transforms. + + Returns the shape and axes in a standard form, taking into account negative + values and checking for various potential errors. + + Parameters + ---------- + x : array_like + The input array. + shape : int or array_like of ints or None + The shape of the result. If both `shape` and `axes` (see below) are + None, `shape` is ``x.shape``; if `shape` is None but `axes` is + not None, then `shape` is ``numpy.take(x.shape, axes, axis=0)``. + If `shape` is -1, the size of the corresponding dimension of `x` is + used. + axes : int or array_like of ints or None + Axes along which the calculation is computed. + The default is over all axes. + Negative indices are automatically converted to their positive + counterparts. + + Returns + ------- + shape : tuple + The shape of the result as a tuple of integers. + axes : list + Axes along which the calculation is computed, as a list of integers. + + """ + x = np.asarray(x) + return _helper._init_nd_shape_and_axes(x, shape, axes) + + +def fftfreq(n, d=1.0, *, xp=None, device=None): + """Return the Discrete Fourier Transform sample frequencies. + + The returned float array `f` contains the frequency bin centers in cycles + per unit of the sample spacing (with zero at the start). For instance, if + the sample spacing is in seconds, then the frequency unit is cycles/second. + + Given a window length `n` and a sample spacing `d`:: + + f = [0, 1, ..., n/2-1, -n/2, ..., -1] / (d*n) if n is even + f = [0, 1, ..., (n-1)/2, -(n-1)/2, ..., -1] / (d*n) if n is odd + + Parameters + ---------- + n : int + Window length. + d : scalar, optional + Sample spacing (inverse of the sampling rate). Defaults to 1. + xp : array_namespace, optional + The namespace for the return array. Default is None, where NumPy is used. + device : device, optional + The device for the return array. + Only valid when `xp.fft.fftfreq` implements the device parameter. + + Returns + ------- + f : ndarray + Array of length `n` containing the sample frequencies. + + Examples + -------- + >>> import numpy as np + >>> import scipy.fft + >>> signal = np.array([-2, 8, 6, 4, 1, 0, 3, 5], dtype=float) + >>> fourier = scipy.fft.fft(signal) + >>> n = signal.size + >>> timestep = 0.1 + >>> freq = scipy.fft.fftfreq(n, d=timestep) + >>> freq + array([ 0. , 1.25, 2.5 , ..., -3.75, -2.5 , -1.25]) + + """ + xp = np if xp is None else xp + # numpy does not yet support the `device` keyword + # `xp.__name__ != 'numpy'` should be removed when numpy is compatible + if hasattr(xp, 'fft') and xp.__name__ != 'numpy': + return xp.fft.fftfreq(n, d=d, device=device) + if device is not None: + raise ValueError('device parameter is not supported for input array type') + return np.fft.fftfreq(n, d=d) + + +def rfftfreq(n, d=1.0, *, xp=None, device=None): + """Return the Discrete Fourier Transform sample frequencies + (for usage with rfft, irfft). + + The returned float array `f` contains the frequency bin centers in cycles + per unit of the sample spacing (with zero at the start). For instance, if + the sample spacing is in seconds, then the frequency unit is cycles/second. + + Given a window length `n` and a sample spacing `d`:: + + f = [0, 1, ..., n/2-1, n/2] / (d*n) if n is even + f = [0, 1, ..., (n-1)/2-1, (n-1)/2] / (d*n) if n is odd + + Unlike `fftfreq` (but like `scipy.fftpack.rfftfreq`) + the Nyquist frequency component is considered to be positive. + + Parameters + ---------- + n : int + Window length. + d : scalar, optional + Sample spacing (inverse of the sampling rate). Defaults to 1. + xp : array_namespace, optional + The namespace for the return array. Default is None, where NumPy is used. + device : device, optional + The device for the return array. + Only valid when `xp.fft.rfftfreq` implements the device parameter. + + Returns + ------- + f : ndarray + Array of length ``n//2 + 1`` containing the sample frequencies. + + Examples + -------- + >>> import numpy as np + >>> import scipy.fft + >>> signal = np.array([-2, 8, 6, 4, 1, 0, 3, 5, -3, 4], dtype=float) + >>> fourier = scipy.fft.rfft(signal) + >>> n = signal.size + >>> sample_rate = 100 + >>> freq = scipy.fft.fftfreq(n, d=1./sample_rate) + >>> freq + array([ 0., 10., 20., ..., -30., -20., -10.]) + >>> freq = scipy.fft.rfftfreq(n, d=1./sample_rate) + >>> freq + array([ 0., 10., 20., 30., 40., 50.]) + + """ + xp = np if xp is None else xp + # numpy does not yet support the `device` keyword + # `xp.__name__ != 'numpy'` should be removed when numpy is compatible + if hasattr(xp, 'fft') and xp.__name__ != 'numpy': + return xp.fft.rfftfreq(n, d=d, device=device) + if device is not None: + raise ValueError('device parameter is not supported for input array type') + return np.fft.rfftfreq(n, d=d) + + +def fftshift(x, axes=None): + """Shift the zero-frequency component to the center of the spectrum. + + This function swaps half-spaces for all axes listed (defaults to all). + Note that ``y[0]`` is the Nyquist component only if ``len(x)`` is even. + + Parameters + ---------- + x : array_like + Input array. + axes : int or shape tuple, optional + Axes over which to shift. Default is None, which shifts all axes. + + Returns + ------- + y : ndarray + The shifted array. + + See Also + -------- + ifftshift : The inverse of `fftshift`. + + Examples + -------- + >>> import numpy as np + >>> freqs = np.fft.fftfreq(10, 0.1) + >>> freqs + array([ 0., 1., 2., ..., -3., -2., -1.]) + >>> np.fft.fftshift(freqs) + array([-5., -4., -3., -2., -1., 0., 1., 2., 3., 4.]) + + Shift the zero-frequency component only along the second axis: + + >>> freqs = np.fft.fftfreq(9, d=1./9).reshape(3, 3) + >>> freqs + array([[ 0., 1., 2.], + [ 3., 4., -4.], + [-3., -2., -1.]]) + >>> np.fft.fftshift(freqs, axes=(1,)) + array([[ 2., 0., 1.], + [-4., 3., 4.], + [-1., -3., -2.]]) + + """ + xp = array_namespace(x) + if hasattr(xp, 'fft'): + return xp.fft.fftshift(x, axes=axes) + x = np.asarray(x) + y = np.fft.fftshift(x, axes=axes) + return xp.asarray(y) + + +def ifftshift(x, axes=None): + """The inverse of `fftshift`. Although identical for even-length `x`, the + functions differ by one sample for odd-length `x`. + + Parameters + ---------- + x : array_like + Input array. + axes : int or shape tuple, optional + Axes over which to calculate. Defaults to None, which shifts all axes. + + Returns + ------- + y : ndarray + The shifted array. + + See Also + -------- + fftshift : Shift zero-frequency component to the center of the spectrum. + + Examples + -------- + >>> import numpy as np + >>> freqs = np.fft.fftfreq(9, d=1./9).reshape(3, 3) + >>> freqs + array([[ 0., 1., 2.], + [ 3., 4., -4.], + [-3., -2., -1.]]) + >>> np.fft.ifftshift(np.fft.fftshift(freqs)) + array([[ 0., 1., 2.], + [ 3., 4., -4.], + [-3., -2., -1.]]) + + """ + xp = array_namespace(x) + if hasattr(xp, 'fft'): + return xp.fft.ifftshift(x, axes=axes) + x = np.asarray(x) + y = np.fft.ifftshift(x, axes=axes) + return xp.asarray(y) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_pocketfft/LICENSE.md b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_pocketfft/LICENSE.md new file mode 100644 index 0000000000000000000000000000000000000000..1b5163d8435976c24988afbd39ded304947178cb --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_pocketfft/LICENSE.md @@ -0,0 +1,25 @@ +Copyright (C) 2010-2019 Max-Planck-Society +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. +* Redistributions in binary form must reproduce the above copyright notice, this + list of conditions and the following disclaimer in the documentation and/or + other materials provided with the distribution. +* Neither the name of the copyright holder nor the names of its contributors may + be used to endorse or promote products derived from this software without + specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_pocketfft/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_pocketfft/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..0671484c9a0780df353b9b783813b6fa7492d38d --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_pocketfft/__init__.py @@ -0,0 +1,9 @@ +""" FFT backend using pypocketfft """ + +from .basic import * +from .realtransforms import * +from .helper import * + +from scipy._lib._testutils import PytestTester +test = PytestTester(__name__) +del PytestTester diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_pocketfft/basic.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_pocketfft/basic.py new file mode 100644 index 0000000000000000000000000000000000000000..bd2d0d33958021c431171b72f72c37363ac98e03 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_pocketfft/basic.py @@ -0,0 +1,251 @@ +""" +Discrete Fourier Transforms - basic.py +""" +import numpy as np +import functools +from . import pypocketfft as pfft +from .helper import (_asfarray, _init_nd_shape_and_axes, _datacopied, + _fix_shape, _fix_shape_1d, _normalization, + _workers) + +def c2c(forward, x, n=None, axis=-1, norm=None, overwrite_x=False, + workers=None, *, plan=None): + """ Return discrete Fourier transform of real or complex sequence. """ + if plan is not None: + raise NotImplementedError('Passing a precomputed plan is not yet ' + 'supported by scipy.fft functions') + tmp = _asfarray(x) + overwrite_x = overwrite_x or _datacopied(tmp, x) + norm = _normalization(norm, forward) + workers = _workers(workers) + + if n is not None: + tmp, copied = _fix_shape_1d(tmp, n, axis) + overwrite_x = overwrite_x or copied + elif tmp.shape[axis] < 1: + message = f"invalid number of data points ({tmp.shape[axis]}) specified" + raise ValueError(message) + + out = (tmp if overwrite_x and tmp.dtype.kind == 'c' else None) + + return pfft.c2c(tmp, (axis,), forward, norm, out, workers) + + +fft = functools.partial(c2c, True) +fft.__name__ = 'fft' +ifft = functools.partial(c2c, False) +ifft.__name__ = 'ifft' + + +def r2c(forward, x, n=None, axis=-1, norm=None, overwrite_x=False, + workers=None, *, plan=None): + """ + Discrete Fourier transform of a real sequence. + """ + if plan is not None: + raise NotImplementedError('Passing a precomputed plan is not yet ' + 'supported by scipy.fft functions') + tmp = _asfarray(x) + norm = _normalization(norm, forward) + workers = _workers(workers) + + if not np.isrealobj(tmp): + raise TypeError("x must be a real sequence") + + if n is not None: + tmp, _ = _fix_shape_1d(tmp, n, axis) + elif tmp.shape[axis] < 1: + raise ValueError(f"invalid number of data points ({tmp.shape[axis]}) specified") + + # Note: overwrite_x is not utilised + return pfft.r2c(tmp, (axis,), forward, norm, None, workers) + + +rfft = functools.partial(r2c, True) +rfft.__name__ = 'rfft' +ihfft = functools.partial(r2c, False) +ihfft.__name__ = 'ihfft' + + +def c2r(forward, x, n=None, axis=-1, norm=None, overwrite_x=False, + workers=None, *, plan=None): + """ + Return inverse discrete Fourier transform of real sequence x. + """ + if plan is not None: + raise NotImplementedError('Passing a precomputed plan is not yet ' + 'supported by scipy.fft functions') + tmp = _asfarray(x) + norm = _normalization(norm, forward) + workers = _workers(workers) + + # TODO: Optimize for hermitian and real? + if np.isrealobj(tmp): + tmp = tmp + 0.j + + # Last axis utilizes hermitian symmetry + if n is None: + n = (tmp.shape[axis] - 1) * 2 + if n < 1: + raise ValueError(f"Invalid number of data points ({n}) specified") + else: + tmp, _ = _fix_shape_1d(tmp, (n//2) + 1, axis) + + # Note: overwrite_x is not utilized + return pfft.c2r(tmp, (axis,), n, forward, norm, None, workers) + + +hfft = functools.partial(c2r, True) +hfft.__name__ = 'hfft' +irfft = functools.partial(c2r, False) +irfft.__name__ = 'irfft' + + +def hfft2(x, s=None, axes=(-2,-1), norm=None, overwrite_x=False, workers=None, + *, plan=None): + """ + 2-D discrete Fourier transform of a Hermitian sequence + """ + if plan is not None: + raise NotImplementedError('Passing a precomputed plan is not yet ' + 'supported by scipy.fft functions') + return hfftn(x, s, axes, norm, overwrite_x, workers) + + +def ihfft2(x, s=None, axes=(-2,-1), norm=None, overwrite_x=False, workers=None, + *, plan=None): + """ + 2-D discrete inverse Fourier transform of a Hermitian sequence + """ + if plan is not None: + raise NotImplementedError('Passing a precomputed plan is not yet ' + 'supported by scipy.fft functions') + return ihfftn(x, s, axes, norm, overwrite_x, workers) + + +def c2cn(forward, x, s=None, axes=None, norm=None, overwrite_x=False, + workers=None, *, plan=None): + """ + Return multidimensional discrete Fourier transform. + """ + if plan is not None: + raise NotImplementedError('Passing a precomputed plan is not yet ' + 'supported by scipy.fft functions') + tmp = _asfarray(x) + + shape, axes = _init_nd_shape_and_axes(tmp, s, axes) + overwrite_x = overwrite_x or _datacopied(tmp, x) + workers = _workers(workers) + + if len(axes) == 0: + return x + + tmp, copied = _fix_shape(tmp, shape, axes) + overwrite_x = overwrite_x or copied + + norm = _normalization(norm, forward) + out = (tmp if overwrite_x and tmp.dtype.kind == 'c' else None) + + return pfft.c2c(tmp, axes, forward, norm, out, workers) + + +fftn = functools.partial(c2cn, True) +fftn.__name__ = 'fftn' +ifftn = functools.partial(c2cn, False) +ifftn.__name__ = 'ifftn' + +def r2cn(forward, x, s=None, axes=None, norm=None, overwrite_x=False, + workers=None, *, plan=None): + """Return multidimensional discrete Fourier transform of real input""" + if plan is not None: + raise NotImplementedError('Passing a precomputed plan is not yet ' + 'supported by scipy.fft functions') + tmp = _asfarray(x) + + if not np.isrealobj(tmp): + raise TypeError("x must be a real sequence") + + shape, axes = _init_nd_shape_and_axes(tmp, s, axes) + tmp, _ = _fix_shape(tmp, shape, axes) + norm = _normalization(norm, forward) + workers = _workers(workers) + + if len(axes) == 0: + raise ValueError("at least 1 axis must be transformed") + + # Note: overwrite_x is not utilized + return pfft.r2c(tmp, axes, forward, norm, None, workers) + + +rfftn = functools.partial(r2cn, True) +rfftn.__name__ = 'rfftn' +ihfftn = functools.partial(r2cn, False) +ihfftn.__name__ = 'ihfftn' + + +def c2rn(forward, x, s=None, axes=None, norm=None, overwrite_x=False, + workers=None, *, plan=None): + """Multidimensional inverse discrete fourier transform with real output""" + if plan is not None: + raise NotImplementedError('Passing a precomputed plan is not yet ' + 'supported by scipy.fft functions') + tmp = _asfarray(x) + + # TODO: Optimize for hermitian and real? + if np.isrealobj(tmp): + tmp = tmp + 0.j + + noshape = s is None + shape, axes = _init_nd_shape_and_axes(tmp, s, axes) + + if len(axes) == 0: + raise ValueError("at least 1 axis must be transformed") + + shape = list(shape) + if noshape: + shape[-1] = (x.shape[axes[-1]] - 1) * 2 + + norm = _normalization(norm, forward) + workers = _workers(workers) + + # Last axis utilizes hermitian symmetry + lastsize = shape[-1] + shape[-1] = (shape[-1] // 2) + 1 + + tmp, _ = tuple(_fix_shape(tmp, shape, axes)) + + # Note: overwrite_x is not utilized + return pfft.c2r(tmp, axes, lastsize, forward, norm, None, workers) + + +hfftn = functools.partial(c2rn, True) +hfftn.__name__ = 'hfftn' +irfftn = functools.partial(c2rn, False) +irfftn.__name__ = 'irfftn' + + +def r2r_fftpack(forward, x, n=None, axis=-1, norm=None, overwrite_x=False): + """FFT of a real sequence, returning fftpack half complex format""" + tmp = _asfarray(x) + overwrite_x = overwrite_x or _datacopied(tmp, x) + norm = _normalization(norm, forward) + workers = _workers(None) + + if tmp.dtype.kind == 'c': + raise TypeError('x must be a real sequence') + + if n is not None: + tmp, copied = _fix_shape_1d(tmp, n, axis) + overwrite_x = overwrite_x or copied + elif tmp.shape[axis] < 1: + raise ValueError(f"invalid number of data points ({tmp.shape[axis]}) specified") + + out = (tmp if overwrite_x else None) + + return pfft.r2r_fftpack(tmp, (axis,), forward, forward, norm, out, workers) + + +rfft_fftpack = functools.partial(r2r_fftpack, True) +rfft_fftpack.__name__ = 'rfft_fftpack' +irfft_fftpack = functools.partial(r2r_fftpack, False) +irfft_fftpack.__name__ = 'irfft_fftpack' diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_pocketfft/helper.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_pocketfft/helper.py new file mode 100644 index 0000000000000000000000000000000000000000..ab2fbc553ccc46a4b337060a62702ec28cb8b254 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_pocketfft/helper.py @@ -0,0 +1,221 @@ +from numbers import Number +import operator +import os +import threading +import contextlib + +import numpy as np + +from scipy._lib._util import copy_if_needed + +# good_size is exposed (and used) from this import +from .pypocketfft import good_size, prev_good_size + + +__all__ = ['good_size', 'prev_good_size', 'set_workers', 'get_workers'] + +_config = threading.local() +_cpu_count = os.cpu_count() + + +def _iterable_of_int(x, name=None): + """Convert ``x`` to an iterable sequence of int + + Parameters + ---------- + x : value, or sequence of values, convertible to int + name : str, optional + Name of the argument being converted, only used in the error message + + Returns + ------- + y : ``List[int]`` + """ + if isinstance(x, Number): + x = (x,) + + try: + x = [operator.index(a) for a in x] + except TypeError as e: + name = name or "value" + raise ValueError(f"{name} must be a scalar or iterable of integers") from e + + return x + + +def _init_nd_shape_and_axes(x, shape, axes): + """Handles shape and axes arguments for nd transforms""" + noshape = shape is None + noaxes = axes is None + + if not noaxes: + axes = _iterable_of_int(axes, 'axes') + axes = [a + x.ndim if a < 0 else a for a in axes] + + if any(a >= x.ndim or a < 0 for a in axes): + raise ValueError("axes exceeds dimensionality of input") + if len(set(axes)) != len(axes): + raise ValueError("all axes must be unique") + + if not noshape: + shape = _iterable_of_int(shape, 'shape') + + if axes and len(axes) != len(shape): + raise ValueError("when given, axes and shape arguments" + " have to be of the same length") + if noaxes: + if len(shape) > x.ndim: + raise ValueError("shape requires more axes than are present") + axes = range(x.ndim - len(shape), x.ndim) + + shape = [x.shape[a] if s == -1 else s for s, a in zip(shape, axes)] + elif noaxes: + shape = list(x.shape) + axes = range(x.ndim) + else: + shape = [x.shape[a] for a in axes] + + if any(s < 1 for s in shape): + raise ValueError( + f"invalid number of data points ({shape}) specified") + + return tuple(shape), list(axes) + + +def _asfarray(x): + """ + Convert to array with floating or complex dtype. + + float16 values are also promoted to float32. + """ + if not hasattr(x, "dtype"): + x = np.asarray(x) + + if x.dtype == np.float16: + return np.asarray(x, np.float32) + elif x.dtype.kind not in 'fc': + return np.asarray(x, np.float64) + + # Require native byte order + dtype = x.dtype.newbyteorder('=') + # Always align input + copy = True if not x.flags['ALIGNED'] else copy_if_needed + return np.array(x, dtype=dtype, copy=copy) + +def _datacopied(arr, original): + """ + Strict check for `arr` not sharing any data with `original`, + under the assumption that arr = asarray(original) + """ + if arr is original: + return False + if not isinstance(original, np.ndarray) and hasattr(original, '__array__'): + return False + return arr.base is None + + +def _fix_shape(x, shape, axes): + """Internal auxiliary function for _raw_fft, _raw_fftnd.""" + must_copy = False + + # Build an nd slice with the dimensions to be read from x + index = [slice(None)]*x.ndim + for n, ax in zip(shape, axes): + if x.shape[ax] >= n: + index[ax] = slice(0, n) + else: + index[ax] = slice(0, x.shape[ax]) + must_copy = True + + index = tuple(index) + + if not must_copy: + return x[index], False + + s = list(x.shape) + for n, axis in zip(shape, axes): + s[axis] = n + + z = np.zeros(s, x.dtype) + z[index] = x[index] + return z, True + + +def _fix_shape_1d(x, n, axis): + if n < 1: + raise ValueError( + f"invalid number of data points ({n}) specified") + + return _fix_shape(x, (n,), (axis,)) + + +_NORM_MAP = {None: 0, 'backward': 0, 'ortho': 1, 'forward': 2} + + +def _normalization(norm, forward): + """Returns the pypocketfft normalization mode from the norm argument""" + try: + inorm = _NORM_MAP[norm] + return inorm if forward else (2 - inorm) + except KeyError: + raise ValueError( + f'Invalid norm value {norm!r}, should ' + 'be "backward", "ortho" or "forward"') from None + + +def _workers(workers): + if workers is None: + return getattr(_config, 'default_workers', 1) + + if workers < 0: + if workers >= -_cpu_count: + workers += 1 + _cpu_count + else: + raise ValueError(f"workers value out of range; got {workers}, must not be" + f" less than {-_cpu_count}") + elif workers == 0: + raise ValueError("workers must not be zero") + + return workers + + +@contextlib.contextmanager +def set_workers(workers): + """Context manager for the default number of workers used in `scipy.fft` + + Parameters + ---------- + workers : int + The default number of workers to use + + Examples + -------- + >>> import numpy as np + >>> from scipy import fft, signal + >>> rng = np.random.default_rng() + >>> x = rng.standard_normal((128, 64)) + >>> with fft.set_workers(4): + ... y = signal.fftconvolve(x, x) + + """ + old_workers = get_workers() + _config.default_workers = _workers(operator.index(workers)) + try: + yield + finally: + _config.default_workers = old_workers + + +def get_workers(): + """Returns the default number of workers within the current context + + Examples + -------- + >>> from scipy import fft + >>> fft.get_workers() + 1 + >>> with fft.set_workers(4): + ... fft.get_workers() + 4 + """ + return getattr(_config, 'default_workers', 1) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_pocketfft/realtransforms.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_pocketfft/realtransforms.py new file mode 100644 index 0000000000000000000000000000000000000000..5a0c616742305444d51258e650344c060129dfab --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_pocketfft/realtransforms.py @@ -0,0 +1,109 @@ +import numpy as np +from . import pypocketfft as pfft +from .helper import (_asfarray, _init_nd_shape_and_axes, _datacopied, + _fix_shape, _fix_shape_1d, _normalization, _workers) +import functools + + +def _r2r(forward, transform, x, type=2, n=None, axis=-1, norm=None, + overwrite_x=False, workers=None, orthogonalize=None): + """Forward or backward 1-D DCT/DST + + Parameters + ---------- + forward : bool + Transform direction (determines type and normalisation) + transform : {pypocketfft.dct, pypocketfft.dst} + The transform to perform + """ + tmp = _asfarray(x) + overwrite_x = overwrite_x or _datacopied(tmp, x) + norm = _normalization(norm, forward) + workers = _workers(workers) + + if not forward: + if type == 2: + type = 3 + elif type == 3: + type = 2 + + if n is not None: + tmp, copied = _fix_shape_1d(tmp, n, axis) + overwrite_x = overwrite_x or copied + elif tmp.shape[axis] < 1: + raise ValueError(f"invalid number of data points ({tmp.shape[axis]}) specified") + + out = (tmp if overwrite_x else None) + + # For complex input, transform real and imaginary components separably + if np.iscomplexobj(x): + out = np.empty_like(tmp) if out is None else out + transform(tmp.real, type, (axis,), norm, out.real, workers) + transform(tmp.imag, type, (axis,), norm, out.imag, workers) + return out + + return transform(tmp, type, (axis,), norm, out, workers, orthogonalize) + + +dct = functools.partial(_r2r, True, pfft.dct) +dct.__name__ = 'dct' +idct = functools.partial(_r2r, False, pfft.dct) +idct.__name__ = 'idct' + +dst = functools.partial(_r2r, True, pfft.dst) +dst.__name__ = 'dst' +idst = functools.partial(_r2r, False, pfft.dst) +idst.__name__ = 'idst' + + +def _r2rn(forward, transform, x, type=2, s=None, axes=None, norm=None, + overwrite_x=False, workers=None, orthogonalize=None): + """Forward or backward nd DCT/DST + + Parameters + ---------- + forward : bool + Transform direction (determines type and normalisation) + transform : {pypocketfft.dct, pypocketfft.dst} + The transform to perform + """ + tmp = _asfarray(x) + + shape, axes = _init_nd_shape_and_axes(tmp, s, axes) + overwrite_x = overwrite_x or _datacopied(tmp, x) + + if len(axes) == 0: + return x + + tmp, copied = _fix_shape(tmp, shape, axes) + overwrite_x = overwrite_x or copied + + if not forward: + if type == 2: + type = 3 + elif type == 3: + type = 2 + + norm = _normalization(norm, forward) + workers = _workers(workers) + out = (tmp if overwrite_x else None) + + # For complex input, transform real and imaginary components separably + if np.iscomplexobj(x): + out = np.empty_like(tmp) if out is None else out + transform(tmp.real, type, axes, norm, out.real, workers) + transform(tmp.imag, type, axes, norm, out.imag, workers) + return out + + return transform(tmp, type, axes, norm, out, workers, orthogonalize) + + +dctn = functools.partial(_r2rn, True, pfft.dct) +dctn.__name__ = 'dctn' +idctn = functools.partial(_r2rn, False, pfft.dct) +idctn.__name__ = 'idctn' + +dstn = functools.partial(_r2rn, True, pfft.dst) +dstn.__name__ = 'dstn' +idstn = functools.partial(_r2rn, False, pfft.dst) +idstn.__name__ = 'idstn' diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_pocketfft/tests/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_pocketfft/tests/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_pocketfft/tests/test_basic.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_pocketfft/tests/test_basic.py new file mode 100644 index 0000000000000000000000000000000000000000..feffc37944c24f81bba3352329cbf75743e4e280 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_pocketfft/tests/test_basic.py @@ -0,0 +1,1013 @@ +# Created by Pearu Peterson, September 2002 + +from numpy.testing import (assert_, assert_equal, assert_array_almost_equal, + assert_array_almost_equal_nulp, assert_array_less, + assert_allclose) +import pytest +from pytest import raises as assert_raises +from scipy.fft._pocketfft import (ifft, fft, fftn, ifftn, + rfft, irfft, rfftn, irfftn, + hfft, ihfft, hfftn, ihfftn) + +from numpy import (arange, array, asarray, zeros, dot, exp, pi, + swapaxes, cdouble) +import numpy as np +import numpy.fft +from numpy.random import rand + +# "large" composite numbers supported by FFT._PYPOCKETFFT +LARGE_COMPOSITE_SIZES = [ + 2**13, + 2**5 * 3**5, + 2**3 * 3**3 * 5**2, +] +SMALL_COMPOSITE_SIZES = [ + 2, + 2*3*5, + 2*2*3*3, +] +# prime +LARGE_PRIME_SIZES = [ + 2011 +] +SMALL_PRIME_SIZES = [ + 29 +] + + +def _assert_close_in_norm(x, y, rtol, size, rdt): + # helper function for testing + err_msg = f"size: {size} rdt: {rdt}" + assert_array_less(np.linalg.norm(x - y), rtol*np.linalg.norm(x), err_msg) + + +def random(size): + return rand(*size) + +def swap_byteorder(arr): + """Returns the same array with swapped byteorder""" + dtype = arr.dtype.newbyteorder('S') + return arr.astype(dtype) + +def direct_dft(x): + x = asarray(x) + n = len(x) + y = zeros(n, dtype=cdouble) + w = -arange(n)*(2j*pi/n) + for i in range(n): + y[i] = dot(exp(i*w), x) + return y + + +def direct_idft(x): + x = asarray(x) + n = len(x) + y = zeros(n, dtype=cdouble) + w = arange(n)*(2j*pi/n) + for i in range(n): + y[i] = dot(exp(i*w), x)/n + return y + + +def direct_dftn(x): + x = asarray(x) + for axis in range(x.ndim): + x = fft(x, axis=axis) + return x + + +def direct_idftn(x): + x = asarray(x) + for axis in range(x.ndim): + x = ifft(x, axis=axis) + return x + + +def direct_rdft(x): + x = asarray(x) + n = len(x) + w = -arange(n)*(2j*pi/n) + y = zeros(n//2+1, dtype=cdouble) + for i in range(n//2+1): + y[i] = dot(exp(i*w), x) + return y + + +def direct_irdft(x, n): + x = asarray(x) + x1 = zeros(n, dtype=cdouble) + for i in range(n//2+1): + x1[i] = x[i] + if i > 0 and 2*i < n: + x1[n-i] = np.conj(x[i]) + return direct_idft(x1).real + + +def direct_rdftn(x): + return fftn(rfft(x), axes=range(x.ndim - 1)) + + +class _TestFFTBase: + def setup_method(self): + self.cdt = None + self.rdt = None + np.random.seed(1234) + + def test_definition(self): + x = np.array([1,2,3,4+1j,1,2,3,4+2j], dtype=self.cdt) + y = fft(x) + assert_equal(y.dtype, self.cdt) + y1 = direct_dft(x) + assert_array_almost_equal(y,y1) + x = np.array([1,2,3,4+0j,5], dtype=self.cdt) + assert_array_almost_equal(fft(x),direct_dft(x)) + + def test_n_argument_real(self): + x1 = np.array([1,2,3,4], dtype=self.rdt) + x2 = np.array([1,2,3,4], dtype=self.rdt) + y = fft([x1,x2],n=4) + assert_equal(y.dtype, self.cdt) + assert_equal(y.shape,(2,4)) + assert_array_almost_equal(y[0],direct_dft(x1)) + assert_array_almost_equal(y[1],direct_dft(x2)) + + def _test_n_argument_complex(self): + x1 = np.array([1,2,3,4+1j], dtype=self.cdt) + x2 = np.array([1,2,3,4+1j], dtype=self.cdt) + y = fft([x1,x2],n=4) + assert_equal(y.dtype, self.cdt) + assert_equal(y.shape,(2,4)) + assert_array_almost_equal(y[0],direct_dft(x1)) + assert_array_almost_equal(y[1],direct_dft(x2)) + + def test_djbfft(self): + for i in range(2,14): + n = 2**i + x = np.arange(n) + y = fft(x.astype(complex)) + y2 = numpy.fft.fft(x) + assert_array_almost_equal(y,y2) + y = fft(x) + assert_array_almost_equal(y,y2) + + def test_invalid_sizes(self): + assert_raises(ValueError, fft, []) + assert_raises(ValueError, fft, [[1,1],[2,2]], -5) + + +class TestLongDoubleFFT(_TestFFTBase): + def setup_method(self): + self.cdt = np.clongdouble + self.rdt = np.longdouble + + +class TestDoubleFFT(_TestFFTBase): + def setup_method(self): + self.cdt = np.cdouble + self.rdt = np.float64 + + +class TestSingleFFT(_TestFFTBase): + def setup_method(self): + self.cdt = np.complex64 + self.rdt = np.float32 + + +class TestFloat16FFT: + + def test_1_argument_real(self): + x1 = np.array([1, 2, 3, 4], dtype=np.float16) + y = fft(x1, n=4) + assert_equal(y.dtype, np.complex64) + assert_equal(y.shape, (4, )) + assert_array_almost_equal(y, direct_dft(x1.astype(np.float32))) + + def test_n_argument_real(self): + x1 = np.array([1, 2, 3, 4], dtype=np.float16) + x2 = np.array([1, 2, 3, 4], dtype=np.float16) + y = fft([x1, x2], n=4) + assert_equal(y.dtype, np.complex64) + assert_equal(y.shape, (2, 4)) + assert_array_almost_equal(y[0], direct_dft(x1.astype(np.float32))) + assert_array_almost_equal(y[1], direct_dft(x2.astype(np.float32))) + + +class _TestIFFTBase: + def setup_method(self): + np.random.seed(1234) + + def test_definition(self): + x = np.array([1,2,3,4+1j,1,2,3,4+2j], self.cdt) + y = ifft(x) + y1 = direct_idft(x) + assert_equal(y.dtype, self.cdt) + assert_array_almost_equal(y,y1) + + x = np.array([1,2,3,4+0j,5], self.cdt) + assert_array_almost_equal(ifft(x),direct_idft(x)) + + def test_definition_real(self): + x = np.array([1,2,3,4,1,2,3,4], self.rdt) + y = ifft(x) + assert_equal(y.dtype, self.cdt) + y1 = direct_idft(x) + assert_array_almost_equal(y,y1) + + x = np.array([1,2,3,4,5], dtype=self.rdt) + assert_equal(y.dtype, self.cdt) + assert_array_almost_equal(ifft(x),direct_idft(x)) + + def test_djbfft(self): + for i in range(2,14): + n = 2**i + x = np.arange(n) + y = ifft(x.astype(self.cdt)) + y2 = numpy.fft.ifft(x.astype(self.cdt)) + assert_allclose(y,y2, rtol=self.rtol, atol=self.atol) + y = ifft(x) + assert_allclose(y,y2, rtol=self.rtol, atol=self.atol) + + def test_random_complex(self): + for size in [1,51,111,100,200,64,128,256,1024]: + x = random([size]).astype(self.cdt) + x = random([size]).astype(self.cdt) + 1j*x + y1 = ifft(fft(x)) + y2 = fft(ifft(x)) + assert_equal(y1.dtype, self.cdt) + assert_equal(y2.dtype, self.cdt) + assert_array_almost_equal(y1, x) + assert_array_almost_equal(y2, x) + + def test_random_real(self): + for size in [1,51,111,100,200,64,128,256,1024]: + x = random([size]).astype(self.rdt) + y1 = ifft(fft(x)) + y2 = fft(ifft(x)) + assert_equal(y1.dtype, self.cdt) + assert_equal(y2.dtype, self.cdt) + assert_array_almost_equal(y1, x) + assert_array_almost_equal(y2, x) + + def test_size_accuracy(self): + # Sanity check for the accuracy for prime and non-prime sized inputs + for size in LARGE_COMPOSITE_SIZES + LARGE_PRIME_SIZES: + np.random.seed(1234) + x = np.random.rand(size).astype(self.rdt) + y = ifft(fft(x)) + _assert_close_in_norm(x, y, self.rtol, size, self.rdt) + y = fft(ifft(x)) + _assert_close_in_norm(x, y, self.rtol, size, self.rdt) + + x = (x + 1j*np.random.rand(size)).astype(self.cdt) + y = ifft(fft(x)) + _assert_close_in_norm(x, y, self.rtol, size, self.rdt) + y = fft(ifft(x)) + _assert_close_in_norm(x, y, self.rtol, size, self.rdt) + + def test_invalid_sizes(self): + assert_raises(ValueError, ifft, []) + assert_raises(ValueError, ifft, [[1,1],[2,2]], -5) + + +@pytest.mark.skipif(np.longdouble is np.float64, + reason="Long double is aliased to double") +class TestLongDoubleIFFT(_TestIFFTBase): + def setup_method(self): + self.cdt = np.clongdouble + self.rdt = np.longdouble + self.rtol = 1e-10 + self.atol = 1e-10 + + +class TestDoubleIFFT(_TestIFFTBase): + def setup_method(self): + self.cdt = np.complex128 + self.rdt = np.float64 + self.rtol = 1e-10 + self.atol = 1e-10 + + +class TestSingleIFFT(_TestIFFTBase): + def setup_method(self): + self.cdt = np.complex64 + self.rdt = np.float32 + self.rtol = 1e-5 + self.atol = 1e-4 + + +class _TestRFFTBase: + def setup_method(self): + np.random.seed(1234) + + def test_definition(self): + for t in [[1, 2, 3, 4, 1, 2, 3, 4], [1, 2, 3, 4, 1, 2, 3, 4, 5]]: + x = np.array(t, dtype=self.rdt) + y = rfft(x) + y1 = direct_rdft(x) + assert_array_almost_equal(y,y1) + assert_equal(y.dtype, self.cdt) + + def test_djbfft(self): + for i in range(2,14): + n = 2**i + x = np.arange(n) + y1 = np.fft.rfft(x) + y = rfft(x) + assert_array_almost_equal(y,y1) + + def test_invalid_sizes(self): + assert_raises(ValueError, rfft, []) + assert_raises(ValueError, rfft, [[1,1],[2,2]], -5) + + def test_complex_input(self): + x = np.zeros(10, dtype=self.cdt) + with assert_raises(TypeError, match="x must be a real sequence"): + rfft(x) + + # See gh-5790 + class MockSeries: + def __init__(self, data): + self.data = np.asarray(data) + + def __getattr__(self, item): + try: + return getattr(self.data, item) + except AttributeError as e: + raise AttributeError("'MockSeries' object " + f"has no attribute '{item}'") from e + + def test_non_ndarray_with_dtype(self): + x = np.array([1., 2., 3., 4., 5.]) + xs = _TestRFFTBase.MockSeries(x) + + expected = [1, 2, 3, 4, 5] + rfft(xs) + + # Data should not have been overwritten + assert_equal(x, expected) + assert_equal(xs.data, expected) + +@pytest.mark.skipif(np.longdouble is np.float64, + reason="Long double is aliased to double") +class TestRFFTLongDouble(_TestRFFTBase): + def setup_method(self): + self.cdt = np.clongdouble + self.rdt = np.longdouble + + +class TestRFFTDouble(_TestRFFTBase): + def setup_method(self): + self.cdt = np.complex128 + self.rdt = np.float64 + + +class TestRFFTSingle(_TestRFFTBase): + def setup_method(self): + self.cdt = np.complex64 + self.rdt = np.float32 + + +class _TestIRFFTBase: + def setup_method(self): + np.random.seed(1234) + + def test_definition(self): + x1 = [1,2+3j,4+1j,1+2j,3+4j] + x1_1 = [1,2+3j,4+1j,2+3j,4,2-3j,4-1j,2-3j] + x1 = x1_1[:5] + x2_1 = [1,2+3j,4+1j,2+3j,4+5j,4-5j,2-3j,4-1j,2-3j] + x2 = x2_1[:5] + + def _test(x, xr): + y = irfft(np.array(x, dtype=self.cdt), n=len(xr)) + y1 = direct_irdft(x, len(xr)) + assert_equal(y.dtype, self.rdt) + assert_array_almost_equal(y,y1, decimal=self.ndec) + assert_array_almost_equal(y,ifft(xr), decimal=self.ndec) + + _test(x1, x1_1) + _test(x2, x2_1) + + def test_djbfft(self): + for i in range(2,14): + n = 2**i + x = np.arange(-1, n, 2) + 1j * np.arange(0, n+1, 2) + x[0] = 0 + if n % 2 == 0: + x[-1] = np.real(x[-1]) + y1 = np.fft.irfft(x) + y = irfft(x) + assert_array_almost_equal(y,y1) + + def test_random_real(self): + for size in [1,51,111,100,200,64,128,256,1024]: + x = random([size]).astype(self.rdt) + y1 = irfft(rfft(x), n=size) + y2 = rfft(irfft(x, n=(size*2-1))) + assert_equal(y1.dtype, self.rdt) + assert_equal(y2.dtype, self.cdt) + assert_array_almost_equal(y1, x, decimal=self.ndec, + err_msg="size=%d" % size) + assert_array_almost_equal(y2, x, decimal=self.ndec, + err_msg="size=%d" % size) + + def test_size_accuracy(self): + # Sanity check for the accuracy for prime and non-prime sized inputs + if self.rdt == np.float32: + rtol = 1e-5 + elif self.rdt == np.float64: + rtol = 1e-10 + + for size in LARGE_COMPOSITE_SIZES + LARGE_PRIME_SIZES: + np.random.seed(1234) + x = np.random.rand(size).astype(self.rdt) + y = irfft(rfft(x), len(x)) + _assert_close_in_norm(x, y, rtol, size, self.rdt) + y = rfft(irfft(x, 2 * len(x) - 1)) + _assert_close_in_norm(x, y, rtol, size, self.rdt) + + def test_invalid_sizes(self): + assert_raises(ValueError, irfft, []) + assert_raises(ValueError, irfft, [[1,1],[2,2]], -5) + + +# self.ndec is bogus; we should have a assert_array_approx_equal for number of +# significant digits + +@pytest.mark.skipif(np.longdouble is np.float64, + reason="Long double is aliased to double") +class TestIRFFTLongDouble(_TestIRFFTBase): + def setup_method(self): + self.cdt = np.complex128 + self.rdt = np.float64 + self.ndec = 14 + + +class TestIRFFTDouble(_TestIRFFTBase): + def setup_method(self): + self.cdt = np.complex128 + self.rdt = np.float64 + self.ndec = 14 + + +class TestIRFFTSingle(_TestIRFFTBase): + def setup_method(self): + self.cdt = np.complex64 + self.rdt = np.float32 + self.ndec = 5 + + +class TestFftnSingle: + def setup_method(self): + np.random.seed(1234) + + def test_definition(self): + x = [[1, 2, 3], + [4, 5, 6], + [7, 8, 9]] + y = fftn(np.array(x, np.float32)) + assert_(y.dtype == np.complex64, + msg="double precision output with single precision") + + y_r = np.array(fftn(x), np.complex64) + assert_array_almost_equal_nulp(y, y_r) + + @pytest.mark.parametrize('size', SMALL_COMPOSITE_SIZES + SMALL_PRIME_SIZES) + def test_size_accuracy_small(self, size): + rng = np.random.default_rng(1234) + x = rng.random((size, size)) + 1j * rng.random((size, size)) + y1 = fftn(x.real.astype(np.float32)) + y2 = fftn(x.real.astype(np.float64)).astype(np.complex64) + + assert_equal(y1.dtype, np.complex64) + assert_array_almost_equal_nulp(y1, y2, 2000) + + @pytest.mark.parametrize('size', LARGE_COMPOSITE_SIZES + LARGE_PRIME_SIZES) + def test_size_accuracy_large(self, size): + rng = np.random.default_rng(1234) + x = rng.random((size, 3)) + 1j * rng.random((size, 3)) + y1 = fftn(x.real.astype(np.float32)) + y2 = fftn(x.real.astype(np.float64)).astype(np.complex64) + + assert_equal(y1.dtype, np.complex64) + assert_array_almost_equal_nulp(y1, y2, 2000) + + def test_definition_float16(self): + x = [[1, 2, 3], + [4, 5, 6], + [7, 8, 9]] + y = fftn(np.array(x, np.float16)) + assert_equal(y.dtype, np.complex64) + y_r = np.array(fftn(x), np.complex64) + assert_array_almost_equal_nulp(y, y_r) + + @pytest.mark.parametrize('size', SMALL_COMPOSITE_SIZES + SMALL_PRIME_SIZES) + def test_float16_input_small(self, size): + rng = np.random.default_rng(1234) + x = rng.random((size, size)) + 1j*rng.random((size, size)) + y1 = fftn(x.real.astype(np.float16)) + y2 = fftn(x.real.astype(np.float64)).astype(np.complex64) + + assert_equal(y1.dtype, np.complex64) + assert_array_almost_equal_nulp(y1, y2, 5e5) + + @pytest.mark.parametrize('size', LARGE_COMPOSITE_SIZES + LARGE_PRIME_SIZES) + def test_float16_input_large(self, size): + rng = np.random.default_rng(1234) + x = rng.random((size, 3)) + 1j*rng.random((size, 3)) + y1 = fftn(x.real.astype(np.float16)) + y2 = fftn(x.real.astype(np.float64)).astype(np.complex64) + + assert_equal(y1.dtype, np.complex64) + assert_array_almost_equal_nulp(y1, y2, 2e6) + + +class TestFftn: + def setup_method(self): + np.random.seed(1234) + + def test_definition(self): + x = [[1, 2, 3], + [4, 5, 6], + [7, 8, 9]] + y = fftn(x) + assert_array_almost_equal(y, direct_dftn(x)) + + x = random((20, 26)) + assert_array_almost_equal(fftn(x), direct_dftn(x)) + + x = random((5, 4, 3, 20)) + assert_array_almost_equal(fftn(x), direct_dftn(x)) + + def test_axes_argument(self): + # plane == ji_plane, x== kji_space + plane1 = [[1, 2, 3], + [4, 5, 6], + [7, 8, 9]] + plane2 = [[10, 11, 12], + [13, 14, 15], + [16, 17, 18]] + plane3 = [[19, 20, 21], + [22, 23, 24], + [25, 26, 27]] + ki_plane1 = [[1, 2, 3], + [10, 11, 12], + [19, 20, 21]] + ki_plane2 = [[4, 5, 6], + [13, 14, 15], + [22, 23, 24]] + ki_plane3 = [[7, 8, 9], + [16, 17, 18], + [25, 26, 27]] + jk_plane1 = [[1, 10, 19], + [4, 13, 22], + [7, 16, 25]] + jk_plane2 = [[2, 11, 20], + [5, 14, 23], + [8, 17, 26]] + jk_plane3 = [[3, 12, 21], + [6, 15, 24], + [9, 18, 27]] + kj_plane1 = [[1, 4, 7], + [10, 13, 16], [19, 22, 25]] + kj_plane2 = [[2, 5, 8], + [11, 14, 17], [20, 23, 26]] + kj_plane3 = [[3, 6, 9], + [12, 15, 18], [21, 24, 27]] + ij_plane1 = [[1, 4, 7], + [2, 5, 8], + [3, 6, 9]] + ij_plane2 = [[10, 13, 16], + [11, 14, 17], + [12, 15, 18]] + ij_plane3 = [[19, 22, 25], + [20, 23, 26], + [21, 24, 27]] + ik_plane1 = [[1, 10, 19], + [2, 11, 20], + [3, 12, 21]] + ik_plane2 = [[4, 13, 22], + [5, 14, 23], + [6, 15, 24]] + ik_plane3 = [[7, 16, 25], + [8, 17, 26], + [9, 18, 27]] + ijk_space = [jk_plane1, jk_plane2, jk_plane3] + ikj_space = [kj_plane1, kj_plane2, kj_plane3] + jik_space = [ik_plane1, ik_plane2, ik_plane3] + jki_space = [ki_plane1, ki_plane2, ki_plane3] + kij_space = [ij_plane1, ij_plane2, ij_plane3] + x = array([plane1, plane2, plane3]) + + assert_array_almost_equal(fftn(x), + fftn(x, axes=(-3, -2, -1))) # kji_space + assert_array_almost_equal(fftn(x), fftn(x, axes=(0, 1, 2))) + assert_array_almost_equal(fftn(x, axes=(0, 2)), fftn(x, axes=(0, -1))) + y = fftn(x, axes=(2, 1, 0)) # ijk_space + assert_array_almost_equal(swapaxes(y, -1, -3), fftn(ijk_space)) + y = fftn(x, axes=(2, 0, 1)) # ikj_space + assert_array_almost_equal(swapaxes(swapaxes(y, -1, -3), -1, -2), + fftn(ikj_space)) + y = fftn(x, axes=(1, 2, 0)) # jik_space + assert_array_almost_equal(swapaxes(swapaxes(y, -1, -3), -3, -2), + fftn(jik_space)) + y = fftn(x, axes=(1, 0, 2)) # jki_space + assert_array_almost_equal(swapaxes(y, -2, -3), fftn(jki_space)) + y = fftn(x, axes=(0, 2, 1)) # kij_space + assert_array_almost_equal(swapaxes(y, -2, -1), fftn(kij_space)) + + y = fftn(x, axes=(-2, -1)) # ji_plane + assert_array_almost_equal(fftn(plane1), y[0]) + assert_array_almost_equal(fftn(plane2), y[1]) + assert_array_almost_equal(fftn(plane3), y[2]) + + y = fftn(x, axes=(1, 2)) # ji_plane + assert_array_almost_equal(fftn(plane1), y[0]) + assert_array_almost_equal(fftn(plane2), y[1]) + assert_array_almost_equal(fftn(plane3), y[2]) + + y = fftn(x, axes=(-3, -2)) # kj_plane + assert_array_almost_equal(fftn(x[:, :, 0]), y[:, :, 0]) + assert_array_almost_equal(fftn(x[:, :, 1]), y[:, :, 1]) + assert_array_almost_equal(fftn(x[:, :, 2]), y[:, :, 2]) + + y = fftn(x, axes=(-3, -1)) # ki_plane + assert_array_almost_equal(fftn(x[:, 0, :]), y[:, 0, :]) + assert_array_almost_equal(fftn(x[:, 1, :]), y[:, 1, :]) + assert_array_almost_equal(fftn(x[:, 2, :]), y[:, 2, :]) + + y = fftn(x, axes=(-1, -2)) # ij_plane + assert_array_almost_equal(fftn(ij_plane1), swapaxes(y[0], -2, -1)) + assert_array_almost_equal(fftn(ij_plane2), swapaxes(y[1], -2, -1)) + assert_array_almost_equal(fftn(ij_plane3), swapaxes(y[2], -2, -1)) + + y = fftn(x, axes=(-1, -3)) # ik_plane + assert_array_almost_equal(fftn(ik_plane1), + swapaxes(y[:, 0, :], -1, -2)) + assert_array_almost_equal(fftn(ik_plane2), + swapaxes(y[:, 1, :], -1, -2)) + assert_array_almost_equal(fftn(ik_plane3), + swapaxes(y[:, 2, :], -1, -2)) + + y = fftn(x, axes=(-2, -3)) # jk_plane + assert_array_almost_equal(fftn(jk_plane1), + swapaxes(y[:, :, 0], -1, -2)) + assert_array_almost_equal(fftn(jk_plane2), + swapaxes(y[:, :, 1], -1, -2)) + assert_array_almost_equal(fftn(jk_plane3), + swapaxes(y[:, :, 2], -1, -2)) + + y = fftn(x, axes=(-1,)) # i_line + for i in range(3): + for j in range(3): + assert_array_almost_equal(fft(x[i, j, :]), y[i, j, :]) + y = fftn(x, axes=(-2,)) # j_line + for i in range(3): + for j in range(3): + assert_array_almost_equal(fft(x[i, :, j]), y[i, :, j]) + y = fftn(x, axes=(0,)) # k_line + for i in range(3): + for j in range(3): + assert_array_almost_equal(fft(x[:, i, j]), y[:, i, j]) + + y = fftn(x, axes=()) # point + assert_array_almost_equal(y, x) + + def test_shape_argument(self): + small_x = [[1, 2, 3], + [4, 5, 6]] + large_x1 = [[1, 2, 3, 0], + [4, 5, 6, 0], + [0, 0, 0, 0], + [0, 0, 0, 0]] + + y = fftn(small_x, s=(4, 4)) + assert_array_almost_equal(y, fftn(large_x1)) + + y = fftn(small_x, s=(3, 4)) + assert_array_almost_equal(y, fftn(large_x1[:-1])) + + def test_shape_axes_argument(self): + small_x = [[1, 2, 3], + [4, 5, 6], + [7, 8, 9]] + large_x1 = array([[1, 2, 3, 0], + [4, 5, 6, 0], + [7, 8, 9, 0], + [0, 0, 0, 0]]) + y = fftn(small_x, s=(4, 4), axes=(-2, -1)) + assert_array_almost_equal(y, fftn(large_x1)) + y = fftn(small_x, s=(4, 4), axes=(-1, -2)) + + assert_array_almost_equal(y, swapaxes( + fftn(swapaxes(large_x1, -1, -2)), -1, -2)) + + def test_shape_axes_argument2(self): + # Change shape of the last axis + x = numpy.random.random((10, 5, 3, 7)) + y = fftn(x, axes=(-1,), s=(8,)) + assert_array_almost_equal(y, fft(x, axis=-1, n=8)) + + # Change shape of an arbitrary axis which is not the last one + x = numpy.random.random((10, 5, 3, 7)) + y = fftn(x, axes=(-2,), s=(8,)) + assert_array_almost_equal(y, fft(x, axis=-2, n=8)) + + # Change shape of axes: cf #244, where shape and axes were mixed up + x = numpy.random.random((4, 4, 2)) + y = fftn(x, axes=(-3, -2), s=(8, 8)) + assert_array_almost_equal(y, + numpy.fft.fftn(x, axes=(-3, -2), s=(8, 8))) + + def test_shape_argument_more(self): + x = zeros((4, 4, 2)) + with assert_raises(ValueError, + match="shape requires more axes than are present"): + fftn(x, s=(8, 8, 2, 1)) + + def test_invalid_sizes(self): + with assert_raises(ValueError, + match="invalid number of data points" + r" \(\[1, 0\]\) specified"): + fftn([[]]) + + with assert_raises(ValueError, + match="invalid number of data points" + r" \(\[4, -3\]\) specified"): + fftn([[1, 1], [2, 2]], (4, -3)) + + def test_no_axes(self): + x = numpy.random.random((2,2,2)) + assert_allclose(fftn(x, axes=[]), x, atol=1e-7) + + def test_regression_244(self): + """FFT returns wrong result with axes parameter.""" + # fftn (and hence fft2) used to break when both axes and shape were used + x = numpy.ones((4, 4, 2)) + y = fftn(x, s=(8, 8), axes=(-3, -2)) + y_r = numpy.fft.fftn(x, s=(8, 8), axes=(-3, -2)) + assert_allclose(y, y_r) + + +class TestIfftn: + dtype = None + cdtype = None + + def setup_method(self): + np.random.seed(1234) + + @pytest.mark.parametrize('dtype,cdtype,maxnlp', + [(np.float64, np.complex128, 2000), + (np.float32, np.complex64, 3500)]) + def test_definition(self, dtype, cdtype, maxnlp): + rng = np.random.default_rng(1234) + x = np.array([[1, 2, 3], + [4, 5, 6], + [7, 8, 9]], dtype=dtype) + y = ifftn(x) + assert_equal(y.dtype, cdtype) + assert_array_almost_equal_nulp(y, direct_idftn(x), maxnlp) + + x = rng.random((20, 26)) + assert_array_almost_equal_nulp(ifftn(x), direct_idftn(x), maxnlp) + + x = rng.random((5, 4, 3, 20)) + assert_array_almost_equal_nulp(ifftn(x), direct_idftn(x), maxnlp) + + @pytest.mark.parametrize('maxnlp', [2000, 3500]) + @pytest.mark.parametrize('size', [1, 2, 51, 32, 64, 92]) + def test_random_complex(self, maxnlp, size): + rng = np.random.default_rng(1234) + x = rng.random([size, size]) + 1j * rng.random([size, size]) + assert_array_almost_equal_nulp(ifftn(fftn(x)), x, maxnlp) + assert_array_almost_equal_nulp(fftn(ifftn(x)), x, maxnlp) + + def test_invalid_sizes(self): + with assert_raises(ValueError, + match="invalid number of data points" + r" \(\[1, 0\]\) specified"): + ifftn([[]]) + + with assert_raises(ValueError, + match="invalid number of data points" + r" \(\[4, -3\]\) specified"): + ifftn([[1, 1], [2, 2]], (4, -3)) + + def test_no_axes(self): + x = numpy.random.random((2,2,2)) + assert_allclose(ifftn(x, axes=[]), x, atol=1e-7) + +class TestRfftn: + dtype = None + cdtype = None + + def setup_method(self): + np.random.seed(1234) + + @pytest.mark.parametrize('dtype,cdtype,maxnlp', + [(np.float64, np.complex128, 2000), + (np.float32, np.complex64, 3500)]) + def test_definition(self, dtype, cdtype, maxnlp): + rng = np.random.default_rng(1234) + x = np.array([[1, 2, 3], + [4, 5, 6], + [7, 8, 9]], dtype=dtype) + y = rfftn(x) + assert_equal(y.dtype, cdtype) + assert_array_almost_equal_nulp(y, direct_rdftn(x), maxnlp) + + x = rng.random((20, 26)) + assert_array_almost_equal_nulp(rfftn(x), direct_rdftn(x), maxnlp) + + x = rng.random((5, 4, 3, 20)) + assert_array_almost_equal_nulp(rfftn(x), direct_rdftn(x), maxnlp) + + @pytest.mark.parametrize('size', [1, 2, 51, 32, 64, 92]) + def test_random(self, size): + rng = np.random.default_rng(1234) + x = rng.random([size, size]) + assert_allclose(irfftn(rfftn(x), x.shape), x, atol=1e-10) + + @pytest.mark.parametrize('func', [rfftn, irfftn]) + def test_invalid_sizes(self, func): + with assert_raises(ValueError, + match="invalid number of data points" + r" \(\[1, 0\]\) specified"): + func([[]]) + + with assert_raises(ValueError, + match="invalid number of data points" + r" \(\[4, -3\]\) specified"): + func([[1, 1], [2, 2]], (4, -3)) + + @pytest.mark.parametrize('func', [rfftn, irfftn]) + def test_no_axes(self, func): + with assert_raises(ValueError, + match="at least 1 axis must be transformed"): + func([], axes=[]) + + def test_complex_input(self): + with assert_raises(TypeError, match="x must be a real sequence"): + rfftn(np.zeros(10, dtype=np.complex64)) + + +class FakeArray: + def __init__(self, data): + self._data = data + self.__array_interface__ = data.__array_interface__ + + +class FakeArray2: + def __init__(self, data): + self._data = data + + def __array__(self, dtype=None, copy=None): + return self._data + +# TODO: Is this test actually valuable? The behavior it's testing shouldn't be +# relied upon by users except for overwrite_x = False +class TestOverwrite: + """Check input overwrite behavior of the FFT functions.""" + + real_dtypes = [np.float32, np.float64, np.longdouble] + dtypes = real_dtypes + [np.complex64, np.complex128, np.clongdouble] + fftsizes = [8, 16, 32] + + def _check(self, x, routine, fftsize, axis, overwrite_x, should_overwrite): + x2 = x.copy() + for fake in [lambda x: x, FakeArray, FakeArray2]: + routine(fake(x2), fftsize, axis, overwrite_x=overwrite_x) + + sig = (f"{routine.__name__}({x.dtype}{x.shape!r}, {fftsize!r}, " + f"axis={axis!r}, overwrite_x={overwrite_x!r})") + if not should_overwrite: + assert_equal(x2, x, err_msg=f"spurious overwrite in {sig}") + + def _check_1d(self, routine, dtype, shape, axis, overwritable_dtypes, + fftsize, overwrite_x): + np.random.seed(1234) + if np.issubdtype(dtype, np.complexfloating): + data = np.random.randn(*shape) + 1j*np.random.randn(*shape) + else: + data = np.random.randn(*shape) + data = data.astype(dtype) + + should_overwrite = (overwrite_x + and dtype in overwritable_dtypes + and fftsize <= shape[axis]) + self._check(data, routine, fftsize, axis, + overwrite_x=overwrite_x, + should_overwrite=should_overwrite) + + @pytest.mark.parametrize('dtype', dtypes) + @pytest.mark.parametrize('fftsize', fftsizes) + @pytest.mark.parametrize('overwrite_x', [True, False]) + @pytest.mark.parametrize('shape,axes', [((16,), -1), + ((16, 2), 0), + ((2, 16), 1)]) + def test_fft_ifft(self, dtype, fftsize, overwrite_x, shape, axes): + overwritable = (np.clongdouble, np.complex128, np.complex64) + self._check_1d(fft, dtype, shape, axes, overwritable, + fftsize, overwrite_x) + self._check_1d(ifft, dtype, shape, axes, overwritable, + fftsize, overwrite_x) + + @pytest.mark.parametrize('dtype', real_dtypes) + @pytest.mark.parametrize('fftsize', fftsizes) + @pytest.mark.parametrize('overwrite_x', [True, False]) + @pytest.mark.parametrize('shape,axes', [((16,), -1), + ((16, 2), 0), + ((2, 16), 1)]) + def test_rfft_irfft(self, dtype, fftsize, overwrite_x, shape, axes): + overwritable = self.real_dtypes + self._check_1d(irfft, dtype, shape, axes, overwritable, + fftsize, overwrite_x) + self._check_1d(rfft, dtype, shape, axes, overwritable, + fftsize, overwrite_x) + + def _check_nd_one(self, routine, dtype, shape, axes, overwritable_dtypes, + overwrite_x): + np.random.seed(1234) + if np.issubdtype(dtype, np.complexfloating): + data = np.random.randn(*shape) + 1j*np.random.randn(*shape) + else: + data = np.random.randn(*shape) + data = data.astype(dtype) + + def fftshape_iter(shp): + if len(shp) <= 0: + yield () + else: + for j in (shp[0]//2, shp[0], shp[0]*2): + for rest in fftshape_iter(shp[1:]): + yield (j,) + rest + + def part_shape(shape, axes): + if axes is None: + return shape + else: + return tuple(np.take(shape, axes)) + + def should_overwrite(data, shape, axes): + s = part_shape(data.shape, axes) + return (overwrite_x and + np.prod(shape) <= np.prod(s) + and dtype in overwritable_dtypes) + + for fftshape in fftshape_iter(part_shape(shape, axes)): + self._check(data, routine, fftshape, axes, + overwrite_x=overwrite_x, + should_overwrite=should_overwrite(data, fftshape, axes)) + if data.ndim > 1: + # check fortran order + self._check(data.T, routine, fftshape, axes, + overwrite_x=overwrite_x, + should_overwrite=should_overwrite( + data.T, fftshape, axes)) + + @pytest.mark.parametrize('dtype', dtypes) + @pytest.mark.parametrize('overwrite_x', [True, False]) + @pytest.mark.parametrize('shape,axes', [((16,), None), + ((16,), (0,)), + ((16, 2), (0,)), + ((2, 16), (1,)), + ((8, 16), None), + ((8, 16), (0, 1)), + ((8, 16, 2), (0, 1)), + ((8, 16, 2), (1, 2)), + ((8, 16, 2), (0,)), + ((8, 16, 2), (1,)), + ((8, 16, 2), (2,)), + ((8, 16, 2), None), + ((8, 16, 2), (0, 1, 2))]) + def test_fftn_ifftn(self, dtype, overwrite_x, shape, axes): + overwritable = (np.clongdouble, np.complex128, np.complex64) + self._check_nd_one(fftn, dtype, shape, axes, overwritable, + overwrite_x) + self._check_nd_one(ifftn, dtype, shape, axes, overwritable, + overwrite_x) + + +@pytest.mark.parametrize('func', [fft, ifft, fftn, ifftn, + rfft, irfft, rfftn, irfftn]) +def test_invalid_norm(func): + x = np.arange(10, dtype=float) + with assert_raises(ValueError, + match='Invalid norm value \'o\', should be' + ' "backward", "ortho" or "forward"'): + func(x, norm='o') + + +@pytest.mark.parametrize('func', [fft, ifft, fftn, ifftn, + irfft, irfftn, hfft, hfftn]) +def test_swapped_byte_order_complex(func): + rng = np.random.RandomState(1234) + x = rng.rand(10) + 1j * rng.rand(10) + assert_allclose(func(swap_byteorder(x)), func(x)) + + +@pytest.mark.parametrize('func', [ihfft, ihfftn, rfft, rfftn]) +def test_swapped_byte_order_real(func): + rng = np.random.RandomState(1234) + x = rng.rand(10) + assert_allclose(func(swap_byteorder(x)), func(x)) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_pocketfft/tests/test_real_transforms.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_pocketfft/tests/test_real_transforms.py new file mode 100644 index 0000000000000000000000000000000000000000..38b3f0a7367a9e97a97133f62ddb5dfb223581ed --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_pocketfft/tests/test_real_transforms.py @@ -0,0 +1,505 @@ +from os.path import join, dirname +from collections.abc import Callable +from threading import Lock + +import numpy as np +from numpy.testing import ( + assert_array_almost_equal, assert_equal, assert_allclose) +import pytest +from pytest import raises as assert_raises + +from scipy.fft._pocketfft.realtransforms import ( + dct, idct, dst, idst, dctn, idctn, dstn, idstn) + +fftpack_test_dir = join(dirname(__file__), '..', '..', '..', 'fftpack', 'tests') + +MDATA_COUNT = 8 +FFTWDATA_COUNT = 14 + +def is_longdouble_binary_compatible(): + try: + one = np.frombuffer( + b'\x00\x00\x00\x00\x00\x00\x00\x80\xff\x3f\x00\x00\x00\x00\x00\x00', + dtype=' decimal +dec_map: DecMapType = { + # DCT + (dct, np.float64, 1): 13, + (dct, np.float32, 1): 6, + + (dct, np.float64, 2): 14, + (dct, np.float32, 2): 5, + + (dct, np.float64, 3): 14, + (dct, np.float32, 3): 5, + + (dct, np.float64, 4): 13, + (dct, np.float32, 4): 6, + + # IDCT + (idct, np.float64, 1): 14, + (idct, np.float32, 1): 6, + + (idct, np.float64, 2): 14, + (idct, np.float32, 2): 5, + + (idct, np.float64, 3): 14, + (idct, np.float32, 3): 5, + + (idct, np.float64, 4): 14, + (idct, np.float32, 4): 6, + + # DST + (dst, np.float64, 1): 13, + (dst, np.float32, 1): 6, + + (dst, np.float64, 2): 14, + (dst, np.float32, 2): 6, + + (dst, np.float64, 3): 14, + (dst, np.float32, 3): 7, + + (dst, np.float64, 4): 13, + (dst, np.float32, 4): 5, + + # IDST + (idst, np.float64, 1): 14, + (idst, np.float32, 1): 6, + + (idst, np.float64, 2): 14, + (idst, np.float32, 2): 6, + + (idst, np.float64, 3): 14, + (idst, np.float32, 3): 6, + + (idst, np.float64, 4): 14, + (idst, np.float32, 4): 6, +} + +for k,v in dec_map.copy().items(): + if k[1] == np.float64: + dec_map[(k[0], np.longdouble, k[2])] = v + elif k[1] == np.float32: + dec_map[(k[0], int, k[2])] = v + + +@pytest.mark.parametrize('rdt', [np.longdouble, np.float64, np.float32, int]) +@pytest.mark.parametrize('type', [1, 2, 3, 4]) +class TestDCT: + def test_definition(self, rdt, type, fftwdata_size, + reference_data, ref_lock): + with ref_lock: + x, yr, dt = fftw_dct_ref(type, fftwdata_size, rdt, reference_data) + y = dct(x, type=type) + assert_equal(y.dtype, dt) + dec = dec_map[(dct, rdt, type)] + assert_allclose(y, yr, rtol=0., atol=np.max(yr)*10**(-dec)) + + @pytest.mark.parametrize('size', [7, 8, 9, 16, 32, 64]) + def test_axis(self, rdt, type, size): + nt = 2 + dec = dec_map[(dct, rdt, type)] + x = np.random.randn(nt, size) + y = dct(x, type=type) + for j in range(nt): + assert_array_almost_equal(y[j], dct(x[j], type=type), + decimal=dec) + + x = x.T + y = dct(x, axis=0, type=type) + for j in range(nt): + assert_array_almost_equal(y[:,j], dct(x[:,j], type=type), + decimal=dec) + + +@pytest.mark.parametrize('rdt', [np.longdouble, np.float64, np.float32, int]) +def test_dct1_definition_ortho(rdt, mdata_x): + # Test orthornomal mode. + dec = dec_map[(dct, rdt, 1)] + x = np.array(mdata_x, dtype=rdt) + dt = np.result_type(np.float32, rdt) + y = dct(x, norm='ortho', type=1) + y2 = naive_dct1(x, norm='ortho') + assert_equal(y.dtype, dt) + assert_allclose(y, y2, rtol=0., atol=np.max(y2)*10**(-dec)) + + +@pytest.mark.parametrize('rdt', [np.longdouble, np.float64, np.float32, int]) +def test_dct2_definition_matlab(mdata_xy, rdt): + # Test correspondence with matlab (orthornomal mode). + dt = np.result_type(np.float32, rdt) + x = np.array(mdata_xy[0], dtype=dt) + + yr = mdata_xy[1] + y = dct(x, norm="ortho", type=2) + dec = dec_map[(dct, rdt, 2)] + assert_equal(y.dtype, dt) + assert_array_almost_equal(y, yr, decimal=dec) + + +@pytest.mark.parametrize('rdt', [np.longdouble, np.float64, np.float32, int]) +def test_dct3_definition_ortho(mdata_x, rdt): + # Test orthornomal mode. + x = np.array(mdata_x, dtype=rdt) + dt = np.result_type(np.float32, rdt) + y = dct(x, norm='ortho', type=2) + xi = dct(y, norm="ortho", type=3) + dec = dec_map[(dct, rdt, 3)] + assert_equal(xi.dtype, dt) + assert_array_almost_equal(xi, x, decimal=dec) + + +@pytest.mark.parametrize('rdt', [np.longdouble, np.float64, np.float32, int]) +def test_dct4_definition_ortho(mdata_x, rdt): + # Test orthornomal mode. + x = np.array(mdata_x, dtype=rdt) + dt = np.result_type(np.float32, rdt) + y = dct(x, norm='ortho', type=4) + y2 = naive_dct4(x, norm='ortho') + dec = dec_map[(dct, rdt, 4)] + assert_equal(y.dtype, dt) + assert_allclose(y, y2, rtol=0., atol=np.max(y2)*10**(-dec)) + + +@pytest.mark.parametrize('rdt', [np.longdouble, np.float64, np.float32, int]) +@pytest.mark.parametrize('type', [1, 2, 3, 4]) +def test_idct_definition(fftwdata_size, rdt, type, reference_data, ref_lock): + with ref_lock: + xr, yr, dt = fftw_dct_ref(type, fftwdata_size, rdt, reference_data) + x = idct(yr, type=type) + dec = dec_map[(idct, rdt, type)] + assert_equal(x.dtype, dt) + assert_allclose(x, xr, rtol=0., atol=np.max(xr)*10**(-dec)) + + +@pytest.mark.parametrize('rdt', [np.longdouble, np.float64, np.float32, int]) +@pytest.mark.parametrize('type', [1, 2, 3, 4]) +def test_definition(fftwdata_size, rdt, type, reference_data, ref_lock): + with ref_lock: + xr, yr, dt = fftw_dst_ref(type, fftwdata_size, rdt, reference_data) + y = dst(xr, type=type) + dec = dec_map[(dst, rdt, type)] + assert_equal(y.dtype, dt) + assert_allclose(y, yr, rtol=0., atol=np.max(yr)*10**(-dec)) + + +@pytest.mark.parametrize('rdt', [np.longdouble, np.float64, np.float32, int]) +def test_dst1_definition_ortho(rdt, mdata_x): + # Test orthornomal mode. + dec = dec_map[(dst, rdt, 1)] + x = np.array(mdata_x, dtype=rdt) + dt = np.result_type(np.float32, rdt) + y = dst(x, norm='ortho', type=1) + y2 = naive_dst1(x, norm='ortho') + assert_equal(y.dtype, dt) + assert_allclose(y, y2, rtol=0., atol=np.max(y2)*10**(-dec)) + + +@pytest.mark.parametrize('rdt', [np.longdouble, np.float64, np.float32, int]) +def test_dst4_definition_ortho(rdt, mdata_x): + # Test orthornomal mode. + dec = dec_map[(dst, rdt, 4)] + x = np.array(mdata_x, dtype=rdt) + dt = np.result_type(np.float32, rdt) + y = dst(x, norm='ortho', type=4) + y2 = naive_dst4(x, norm='ortho') + assert_equal(y.dtype, dt) + assert_array_almost_equal(y, y2, decimal=dec) + + +@pytest.mark.parametrize('rdt', [np.longdouble, np.float64, np.float32, int]) +@pytest.mark.parametrize('type', [1, 2, 3, 4]) +def test_idst_definition(fftwdata_size, rdt, type, reference_data, ref_lock): + with ref_lock: + xr, yr, dt = fftw_dst_ref(type, fftwdata_size, rdt, reference_data) + x = idst(yr, type=type) + dec = dec_map[(idst, rdt, type)] + assert_equal(x.dtype, dt) + assert_allclose(x, xr, rtol=0., atol=np.max(xr)*10**(-dec)) + + +@pytest.mark.parametrize('routine', [dct, dst, idct, idst]) +@pytest.mark.parametrize('dtype', [np.float32, np.float64, np.longdouble]) +@pytest.mark.parametrize('shape, axis', [ + ((16,), -1), ((16, 2), 0), ((2, 16), 1) +]) +@pytest.mark.parametrize('type', [1, 2, 3, 4]) +@pytest.mark.parametrize('overwrite_x', [True, False]) +@pytest.mark.parametrize('norm', [None, 'ortho']) +def test_overwrite(routine, dtype, shape, axis, type, norm, overwrite_x): + # Check input overwrite behavior + np.random.seed(1234) + if np.issubdtype(dtype, np.complexfloating): + x = np.random.randn(*shape) + 1j*np.random.randn(*shape) + else: + x = np.random.randn(*shape) + x = x.astype(dtype) + x2 = x.copy() + routine(x2, type, None, axis, norm, overwrite_x=overwrite_x) + + sig = (f"{routine.__name__}({x.dtype}{x.shape!r}, {None!r}, axis={axis!r}, " + f"overwrite_x={overwrite_x!r})") + if not overwrite_x: + assert_equal(x2, x, err_msg=f"spurious overwrite in {sig}") + + +class Test_DCTN_IDCTN: + dec = 14 + dct_type = [1, 2, 3, 4] + norms = [None, 'backward', 'ortho', 'forward'] + rstate = np.random.RandomState(1234) + shape = (32, 16) + data = rstate.randn(*shape) + + @pytest.mark.parametrize('fforward,finverse', [(dctn, idctn), + (dstn, idstn)]) + @pytest.mark.parametrize('axes', [None, + 1, (1,), [1], + 0, (0,), [0], + (0, 1), [0, 1], + (-2, -1), [-2, -1]]) + @pytest.mark.parametrize('dct_type', dct_type) + @pytest.mark.parametrize('norm', ['ortho']) + def test_axes_round_trip(self, fforward, finverse, axes, dct_type, norm): + tmp = fforward(self.data, type=dct_type, axes=axes, norm=norm) + tmp = finverse(tmp, type=dct_type, axes=axes, norm=norm) + assert_array_almost_equal(self.data, tmp, decimal=12) + + @pytest.mark.parametrize('funcn,func', [(dctn, dct), (dstn, dst)]) + @pytest.mark.parametrize('dct_type', dct_type) + @pytest.mark.parametrize('norm', norms) + def test_dctn_vs_2d_reference(self, funcn, func, dct_type, norm): + y1 = funcn(self.data, type=dct_type, axes=None, norm=norm) + y2 = ref_2d(func, self.data, type=dct_type, norm=norm) + assert_array_almost_equal(y1, y2, decimal=11) + + @pytest.mark.parametrize('funcn,func', [(idctn, idct), (idstn, idst)]) + @pytest.mark.parametrize('dct_type', dct_type) + @pytest.mark.parametrize('norm', norms) + def test_idctn_vs_2d_reference(self, funcn, func, dct_type, norm): + fdata = dctn(self.data, type=dct_type, norm=norm) + y1 = funcn(fdata, type=dct_type, norm=norm) + y2 = ref_2d(func, fdata, type=dct_type, norm=norm) + assert_array_almost_equal(y1, y2, decimal=11) + + @pytest.mark.parametrize('fforward,finverse', [(dctn, idctn), + (dstn, idstn)]) + def test_axes_and_shape(self, fforward, finverse): + with assert_raises(ValueError, + match="when given, axes and shape arguments" + " have to be of the same length"): + fforward(self.data, s=self.data.shape[0], axes=(0, 1)) + + with assert_raises(ValueError, + match="when given, axes and shape arguments" + " have to be of the same length"): + fforward(self.data, s=self.data.shape, axes=0) + + @pytest.mark.parametrize('fforward', [dctn, dstn]) + def test_shape(self, fforward): + tmp = fforward(self.data, s=(128, 128), axes=None) + assert_equal(tmp.shape, (128, 128)) + + @pytest.mark.parametrize('fforward,finverse', [(dctn, idctn), + (dstn, idstn)]) + @pytest.mark.parametrize('axes', [1, (1,), [1], + 0, (0,), [0]]) + def test_shape_is_none_with_axes(self, fforward, finverse, axes): + tmp = fforward(self.data, s=None, axes=axes, norm='ortho') + tmp = finverse(tmp, s=None, axes=axes, norm='ortho') + assert_array_almost_equal(self.data, tmp, decimal=self.dec) + + +@pytest.mark.parametrize('func', [dct, dctn, idct, idctn, + dst, dstn, idst, idstn]) +def test_swapped_byte_order(func): + rng = np.random.RandomState(1234) + x = rng.rand(10) + swapped_dt = x.dtype.newbyteorder('S') + assert_allclose(func(x.astype(swapped_dt)), func(x)) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_realtransforms.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_realtransforms.py new file mode 100644 index 0000000000000000000000000000000000000000..1c7a3d683dd78d3227a7de88f5c47569d2f4e17f --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_realtransforms.py @@ -0,0 +1,693 @@ +from ._basic import _dispatch +from scipy._lib.uarray import Dispatchable +import numpy as np + +__all__ = ['dct', 'idct', 'dst', 'idst', 'dctn', 'idctn', 'dstn', 'idstn'] + + +@_dispatch +def dctn(x, type=2, s=None, axes=None, norm=None, overwrite_x=False, + workers=None, *, orthogonalize=None): + """ + Return multidimensional Discrete Cosine Transform along the specified axes. + + Parameters + ---------- + x : array_like + The input array. + type : {1, 2, 3, 4}, optional + Type of the DCT (see Notes). Default type is 2. + s : int or array_like of ints or None, optional + The shape of the result. If both `s` and `axes` (see below) are None, + `s` is ``x.shape``; if `s` is None but `axes` is not None, then `s` is + ``numpy.take(x.shape, axes, axis=0)``. + If ``s[i] > x.shape[i]``, the ith dimension of the input is padded with zeros. + If ``s[i] < x.shape[i]``, the ith dimension of the input is truncated to length + ``s[i]``. + If any element of `s` is -1, the size of the corresponding dimension of + `x` is used. + axes : int or array_like of ints or None, optional + Axes over which the DCT is computed. If not given, the last ``len(s)`` + axes are used, or all axes if `s` is also not specified. + norm : {"backward", "ortho", "forward"}, optional + Normalization mode (see Notes). Default is "backward". + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + workers : int, optional + Maximum number of workers to use for parallel computation. If negative, + the value wraps around from ``os.cpu_count()``. + See :func:`~scipy.fft.fft` for more details. + orthogonalize : bool, optional + Whether to use the orthogonalized DCT variant (see Notes). + Defaults to ``True`` when ``norm="ortho"`` and ``False`` otherwise. + + .. versionadded:: 1.8.0 + + Returns + ------- + y : ndarray of real + The transformed input array. + + See Also + -------- + idctn : Inverse multidimensional DCT + + Notes + ----- + For full details of the DCT types and normalization modes, as well as + references, see `dct`. + + Examples + -------- + >>> import numpy as np + >>> from scipy.fft import dctn, idctn + >>> rng = np.random.default_rng() + >>> y = rng.standard_normal((16, 16)) + >>> np.allclose(y, idctn(dctn(y))) + True + + """ + return (Dispatchable(x, np.ndarray),) + + +@_dispatch +def idctn(x, type=2, s=None, axes=None, norm=None, overwrite_x=False, + workers=None, orthogonalize=None): + """ + Return multidimensional Inverse Discrete Cosine Transform along the specified axes. + + Parameters + ---------- + x : array_like + The input array. + type : {1, 2, 3, 4}, optional + Type of the DCT (see Notes). Default type is 2. + s : int or array_like of ints or None, optional + The shape of the result. If both `s` and `axes` (see below) are + None, `s` is ``x.shape``; if `s` is None but `axes` is + not None, then `s` is ``numpy.take(x.shape, axes, axis=0)``. + If ``s[i] > x.shape[i]``, the ith dimension of the input is padded with zeros. + If ``s[i] < x.shape[i]``, the ith dimension of the input is truncated to length + ``s[i]``. + If any element of `s` is -1, the size of the corresponding dimension of + `x` is used. + axes : int or array_like of ints or None, optional + Axes over which the IDCT is computed. If not given, the last ``len(s)`` + axes are used, or all axes if `s` is also not specified. + norm : {"backward", "ortho", "forward"}, optional + Normalization mode (see Notes). Default is "backward". + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + workers : int, optional + Maximum number of workers to use for parallel computation. If negative, + the value wraps around from ``os.cpu_count()``. + See :func:`~scipy.fft.fft` for more details. + orthogonalize : bool, optional + Whether to use the orthogonalized IDCT variant (see Notes). + Defaults to ``True`` when ``norm="ortho"`` and ``False`` otherwise. + + .. versionadded:: 1.8.0 + + Returns + ------- + y : ndarray of real + The transformed input array. + + See Also + -------- + dctn : multidimensional DCT + + Notes + ----- + For full details of the IDCT types and normalization modes, as well as + references, see `idct`. + + Examples + -------- + >>> import numpy as np + >>> from scipy.fft import dctn, idctn + >>> rng = np.random.default_rng() + >>> y = rng.standard_normal((16, 16)) + >>> np.allclose(y, idctn(dctn(y))) + True + + """ + return (Dispatchable(x, np.ndarray),) + + +@_dispatch +def dstn(x, type=2, s=None, axes=None, norm=None, overwrite_x=False, + workers=None, orthogonalize=None): + """ + Return multidimensional Discrete Sine Transform along the specified axes. + + Parameters + ---------- + x : array_like + The input array. + type : {1, 2, 3, 4}, optional + Type of the DST (see Notes). Default type is 2. + s : int or array_like of ints or None, optional + The shape of the result. If both `s` and `axes` (see below) are None, + `s` is ``x.shape``; if `s` is None but `axes` is not None, then `s` is + ``numpy.take(x.shape, axes, axis=0)``. + If ``s[i] > x.shape[i]``, the ith dimension of the input is padded with zeros. + If ``s[i] < x.shape[i]``, the ith dimension of the input is truncated to length + ``s[i]``. + If any element of `shape` is -1, the size of the corresponding dimension + of `x` is used. + axes : int or array_like of ints or None, optional + Axes over which the DST is computed. If not given, the last ``len(s)`` + axes are used, or all axes if `s` is also not specified. + norm : {"backward", "ortho", "forward"}, optional + Normalization mode (see Notes). Default is "backward". + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + workers : int, optional + Maximum number of workers to use for parallel computation. If negative, + the value wraps around from ``os.cpu_count()``. + See :func:`~scipy.fft.fft` for more details. + orthogonalize : bool, optional + Whether to use the orthogonalized DST variant (see Notes). + Defaults to ``True`` when ``norm="ortho"`` and ``False`` otherwise. + + .. versionadded:: 1.8.0 + + Returns + ------- + y : ndarray of real + The transformed input array. + + See Also + -------- + idstn : Inverse multidimensional DST + + Notes + ----- + For full details of the DST types and normalization modes, as well as + references, see `dst`. + + Examples + -------- + >>> import numpy as np + >>> from scipy.fft import dstn, idstn + >>> rng = np.random.default_rng() + >>> y = rng.standard_normal((16, 16)) + >>> np.allclose(y, idstn(dstn(y))) + True + + """ + return (Dispatchable(x, np.ndarray),) + + +@_dispatch +def idstn(x, type=2, s=None, axes=None, norm=None, overwrite_x=False, + workers=None, orthogonalize=None): + """ + Return multidimensional Inverse Discrete Sine Transform along the specified axes. + + Parameters + ---------- + x : array_like + The input array. + type : {1, 2, 3, 4}, optional + Type of the DST (see Notes). Default type is 2. + s : int or array_like of ints or None, optional + The shape of the result. If both `s` and `axes` (see below) are None, + `s` is ``x.shape``; if `s` is None but `axes` is not None, then `s` is + ``numpy.take(x.shape, axes, axis=0)``. + If ``s[i] > x.shape[i]``, the ith dimension of the input is padded with zeros. + If ``s[i] < x.shape[i]``, the ith dimension of the input is truncated to length + ``s[i]``. + If any element of `s` is -1, the size of the corresponding dimension of + `x` is used. + axes : int or array_like of ints or None, optional + Axes over which the IDST is computed. If not given, the last ``len(s)`` + axes are used, or all axes if `s` is also not specified. + norm : {"backward", "ortho", "forward"}, optional + Normalization mode (see Notes). Default is "backward". + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + workers : int, optional + Maximum number of workers to use for parallel computation. If negative, + the value wraps around from ``os.cpu_count()``. + See :func:`~scipy.fft.fft` for more details. + orthogonalize : bool, optional + Whether to use the orthogonalized IDST variant (see Notes). + Defaults to ``True`` when ``norm="ortho"`` and ``False`` otherwise. + + .. versionadded:: 1.8.0 + + Returns + ------- + y : ndarray of real + The transformed input array. + + See Also + -------- + dstn : multidimensional DST + + Notes + ----- + For full details of the IDST types and normalization modes, as well as + references, see `idst`. + + Examples + -------- + >>> import numpy as np + >>> from scipy.fft import dstn, idstn + >>> rng = np.random.default_rng() + >>> y = rng.standard_normal((16, 16)) + >>> np.allclose(y, idstn(dstn(y))) + True + + """ + return (Dispatchable(x, np.ndarray),) + + +@_dispatch +def dct(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False, workers=None, + orthogonalize=None): + r"""Return the Discrete Cosine Transform of arbitrary type sequence x. + + Parameters + ---------- + x : array_like + The input array. + type : {1, 2, 3, 4}, optional + Type of the DCT (see Notes). Default type is 2. + n : int, optional + Length of the transform. If ``n < x.shape[axis]``, `x` is + truncated. If ``n > x.shape[axis]``, `x` is zero-padded. The + default results in ``n = x.shape[axis]``. + axis : int, optional + Axis along which the dct is computed; the default is over the + last axis (i.e., ``axis=-1``). + norm : {"backward", "ortho", "forward"}, optional + Normalization mode (see Notes). Default is "backward". + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + workers : int, optional + Maximum number of workers to use for parallel computation. If negative, + the value wraps around from ``os.cpu_count()``. + See :func:`~scipy.fft.fft` for more details. + orthogonalize : bool, optional + Whether to use the orthogonalized DCT variant (see Notes). + Defaults to ``True`` when ``norm="ortho"`` and ``False`` otherwise. + + .. versionadded:: 1.8.0 + + Returns + ------- + y : ndarray of real + The transformed input array. + + See Also + -------- + idct : Inverse DCT + + Notes + ----- + For a single dimension array ``x``, ``dct(x, norm='ortho')`` is equal to + MATLAB ``dct(x)``. + + .. warning:: For ``type in {1, 2, 3}``, ``norm="ortho"`` breaks the direct + correspondence with the direct Fourier transform. To recover + it you must specify ``orthogonalize=False``. + + For ``norm="ortho"`` both the `dct` and `idct` are scaled by the same + overall factor in both directions. By default, the transform is also + orthogonalized which for types 1, 2 and 3 means the transform definition is + modified to give orthogonality of the DCT matrix (see below). + + For ``norm="backward"``, there is no scaling on `dct` and the `idct` is + scaled by ``1/N`` where ``N`` is the "logical" size of the DCT. For + ``norm="forward"`` the ``1/N`` normalization is applied to the forward + `dct` instead and the `idct` is unnormalized. + + There are, theoretically, 8 types of the DCT, only the first 4 types are + implemented in SciPy.'The' DCT generally refers to DCT type 2, and 'the' + Inverse DCT generally refers to DCT type 3. + + **Type I** + + There are several definitions of the DCT-I; we use the following + (for ``norm="backward"``) + + .. math:: + + y_k = x_0 + (-1)^k x_{N-1} + 2 \sum_{n=1}^{N-2} x_n \cos\left( + \frac{\pi k n}{N-1} \right) + + If ``orthogonalize=True``, ``x[0]`` and ``x[N-1]`` are multiplied by a + scaling factor of :math:`\sqrt{2}`, and ``y[0]`` and ``y[N-1]`` are divided + by :math:`\sqrt{2}`. When combined with ``norm="ortho"``, this makes the + corresponding matrix of coefficients orthonormal (``O @ O.T = np.eye(N)``). + + .. note:: + The DCT-I is only supported for input size > 1. + + **Type II** + + There are several definitions of the DCT-II; we use the following + (for ``norm="backward"``) + + .. math:: + + y_k = 2 \sum_{n=0}^{N-1} x_n \cos\left(\frac{\pi k(2n+1)}{2N} \right) + + If ``orthogonalize=True``, ``y[0]`` is divided by :math:`\sqrt{2}` which, + when combined with ``norm="ortho"``, makes the corresponding matrix of + coefficients orthonormal (``O @ O.T = np.eye(N)``). + + **Type III** + + There are several definitions, we use the following (for + ``norm="backward"``) + + .. math:: + + y_k = x_0 + 2 \sum_{n=1}^{N-1} x_n \cos\left(\frac{\pi(2k+1)n}{2N}\right) + + If ``orthogonalize=True``, ``x[0]`` terms are multiplied by + :math:`\sqrt{2}` which, when combined with ``norm="ortho"``, makes the + corresponding matrix of coefficients orthonormal (``O @ O.T = np.eye(N)``). + + The (unnormalized) DCT-III is the inverse of the (unnormalized) DCT-II, up + to a factor `2N`. The orthonormalized DCT-III is exactly the inverse of + the orthonormalized DCT-II. + + **Type IV** + + There are several definitions of the DCT-IV; we use the following + (for ``norm="backward"``) + + .. math:: + + y_k = 2 \sum_{n=0}^{N-1} x_n \cos\left(\frac{\pi(2k+1)(2n+1)}{4N} \right) + + ``orthogonalize`` has no effect here, as the DCT-IV matrix is already + orthogonal up to a scale factor of ``2N``. + + References + ---------- + .. [1] 'A Fast Cosine Transform in One and Two Dimensions', by J. + Makhoul, `IEEE Transactions on acoustics, speech and signal + processing` vol. 28(1), pp. 27-34, + :doi:`10.1109/TASSP.1980.1163351` (1980). + .. [2] Wikipedia, "Discrete cosine transform", + https://en.wikipedia.org/wiki/Discrete_cosine_transform + + Examples + -------- + The Type 1 DCT is equivalent to the FFT (though faster) for real, + even-symmetrical inputs. The output is also real and even-symmetrical. + Half of the FFT input is used to generate half of the FFT output: + + >>> from scipy.fft import fft, dct + >>> import numpy as np + >>> fft(np.array([4., 3., 5., 10., 5., 3.])).real + array([ 30., -8., 6., -2., 6., -8.]) + >>> dct(np.array([4., 3., 5., 10.]), 1) + array([ 30., -8., 6., -2.]) + + """ + return (Dispatchable(x, np.ndarray),) + + +@_dispatch +def idct(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False, + workers=None, orthogonalize=None): + """ + Return the Inverse Discrete Cosine Transform of an arbitrary type sequence. + + Parameters + ---------- + x : array_like + The input array. + type : {1, 2, 3, 4}, optional + Type of the DCT (see Notes). Default type is 2. + n : int, optional + Length of the transform. If ``n < x.shape[axis]``, `x` is + truncated. If ``n > x.shape[axis]``, `x` is zero-padded. The + default results in ``n = x.shape[axis]``. + axis : int, optional + Axis along which the idct is computed; the default is over the + last axis (i.e., ``axis=-1``). + norm : {"backward", "ortho", "forward"}, optional + Normalization mode (see Notes). Default is "backward". + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + workers : int, optional + Maximum number of workers to use for parallel computation. If negative, + the value wraps around from ``os.cpu_count()``. + See :func:`~scipy.fft.fft` for more details. + orthogonalize : bool, optional + Whether to use the orthogonalized IDCT variant (see Notes). + Defaults to ``True`` when ``norm="ortho"`` and ``False`` otherwise. + + .. versionadded:: 1.8.0 + + Returns + ------- + idct : ndarray of real + The transformed input array. + + See Also + -------- + dct : Forward DCT + + Notes + ----- + For a single dimension array `x`, ``idct(x, norm='ortho')`` is equal to + MATLAB ``idct(x)``. + + .. warning:: For ``type in {1, 2, 3}``, ``norm="ortho"`` breaks the direct + correspondence with the inverse direct Fourier transform. To + recover it you must specify ``orthogonalize=False``. + + For ``norm="ortho"`` both the `dct` and `idct` are scaled by the same + overall factor in both directions. By default, the transform is also + orthogonalized which for types 1, 2 and 3 means the transform definition is + modified to give orthogonality of the IDCT matrix (see `dct` for the full + definitions). + + 'The' IDCT is the IDCT-II, which is the same as the normalized DCT-III. + + The IDCT is equivalent to a normal DCT except for the normalization and + type. DCT type 1 and 4 are their own inverse and DCTs 2 and 3 are each + other's inverses. + + Examples + -------- + The Type 1 DCT is equivalent to the DFT for real, even-symmetrical + inputs. The output is also real and even-symmetrical. Half of the IFFT + input is used to generate half of the IFFT output: + + >>> from scipy.fft import ifft, idct + >>> import numpy as np + >>> ifft(np.array([ 30., -8., 6., -2., 6., -8.])).real + array([ 4., 3., 5., 10., 5., 3.]) + >>> idct(np.array([ 30., -8., 6., -2.]), 1) + array([ 4., 3., 5., 10.]) + + """ + return (Dispatchable(x, np.ndarray),) + + +@_dispatch +def dst(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False, workers=None, + orthogonalize=None): + r""" + Return the Discrete Sine Transform of arbitrary type sequence x. + + Parameters + ---------- + x : array_like + The input array. + type : {1, 2, 3, 4}, optional + Type of the DST (see Notes). Default type is 2. + n : int, optional + Length of the transform. If ``n < x.shape[axis]``, `x` is + truncated. If ``n > x.shape[axis]``, `x` is zero-padded. The + default results in ``n = x.shape[axis]``. + axis : int, optional + Axis along which the dst is computed; the default is over the + last axis (i.e., ``axis=-1``). + norm : {"backward", "ortho", "forward"}, optional + Normalization mode (see Notes). Default is "backward". + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + workers : int, optional + Maximum number of workers to use for parallel computation. If negative, + the value wraps around from ``os.cpu_count()``. + See :func:`~scipy.fft.fft` for more details. + orthogonalize : bool, optional + Whether to use the orthogonalized DST variant (see Notes). + Defaults to ``True`` when ``norm="ortho"`` and ``False`` otherwise. + + .. versionadded:: 1.8.0 + + Returns + ------- + dst : ndarray of reals + The transformed input array. + + See Also + -------- + idst : Inverse DST + + Notes + ----- + .. warning:: For ``type in {2, 3}``, ``norm="ortho"`` breaks the direct + correspondence with the direct Fourier transform. To recover + it you must specify ``orthogonalize=False``. + + For ``norm="ortho"`` both the `dst` and `idst` are scaled by the same + overall factor in both directions. By default, the transform is also + orthogonalized which for types 2 and 3 means the transform definition is + modified to give orthogonality of the DST matrix (see below). + + For ``norm="backward"``, there is no scaling on the `dst` and the `idst` is + scaled by ``1/N`` where ``N`` is the "logical" size of the DST. + + There are, theoretically, 8 types of the DST for different combinations of + even/odd boundary conditions and boundary off sets [1]_, only the first + 4 types are implemented in SciPy. + + **Type I** + + There are several definitions of the DST-I; we use the following for + ``norm="backward"``. DST-I assumes the input is odd around :math:`n=-1` and + :math:`n=N`. + + .. math:: + + y_k = 2 \sum_{n=0}^{N-1} x_n \sin\left(\frac{\pi(k+1)(n+1)}{N+1}\right) + + Note that the DST-I is only supported for input size > 1. + The (unnormalized) DST-I is its own inverse, up to a factor :math:`2(N+1)`. + The orthonormalized DST-I is exactly its own inverse. + + ``orthogonalize`` has no effect here, as the DST-I matrix is already + orthogonal up to a scale factor of ``2N``. + + **Type II** + + There are several definitions of the DST-II; we use the following for + ``norm="backward"``. DST-II assumes the input is odd around :math:`n=-1/2` and + :math:`n=N-1/2`; the output is odd around :math:`k=-1` and even around :math:`k=N-1` + + .. math:: + + y_k = 2 \sum_{n=0}^{N-1} x_n \sin\left(\frac{\pi(k+1)(2n+1)}{2N}\right) + + If ``orthogonalize=True``, ``y[-1]`` is divided :math:`\sqrt{2}` which, when + combined with ``norm="ortho"``, makes the corresponding matrix of + coefficients orthonormal (``O @ O.T = np.eye(N)``). + + **Type III** + + There are several definitions of the DST-III, we use the following (for + ``norm="backward"``). DST-III assumes the input is odd around :math:`n=-1` and + even around :math:`n=N-1` + + .. math:: + + y_k = (-1)^k x_{N-1} + 2 \sum_{n=0}^{N-2} x_n \sin\left( + \frac{\pi(2k+1)(n+1)}{2N}\right) + + If ``orthogonalize=True``, ``x[-1]`` is multiplied by :math:`\sqrt{2}` + which, when combined with ``norm="ortho"``, makes the corresponding matrix + of coefficients orthonormal (``O @ O.T = np.eye(N)``). + + The (unnormalized) DST-III is the inverse of the (unnormalized) DST-II, up + to a factor :math:`2N`. The orthonormalized DST-III is exactly the inverse of the + orthonormalized DST-II. + + **Type IV** + + There are several definitions of the DST-IV, we use the following (for + ``norm="backward"``). DST-IV assumes the input is odd around :math:`n=-0.5` and + even around :math:`n=N-0.5` + + .. math:: + + y_k = 2 \sum_{n=0}^{N-1} x_n \sin\left(\frac{\pi(2k+1)(2n+1)}{4N}\right) + + ``orthogonalize`` has no effect here, as the DST-IV matrix is already + orthogonal up to a scale factor of ``2N``. + + The (unnormalized) DST-IV is its own inverse, up to a factor :math:`2N`. The + orthonormalized DST-IV is exactly its own inverse. + + References + ---------- + .. [1] Wikipedia, "Discrete sine transform", + https://en.wikipedia.org/wiki/Discrete_sine_transform + + """ + return (Dispatchable(x, np.ndarray),) + + +@_dispatch +def idst(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False, + workers=None, orthogonalize=None): + """ + Return the Inverse Discrete Sine Transform of an arbitrary type sequence. + + Parameters + ---------- + x : array_like + The input array. + type : {1, 2, 3, 4}, optional + Type of the DST (see Notes). Default type is 2. + n : int, optional + Length of the transform. If ``n < x.shape[axis]``, `x` is + truncated. If ``n > x.shape[axis]``, `x` is zero-padded. The + default results in ``n = x.shape[axis]``. + axis : int, optional + Axis along which the idst is computed; the default is over the + last axis (i.e., ``axis=-1``). + norm : {"backward", "ortho", "forward"}, optional + Normalization mode (see Notes). Default is "backward". + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + workers : int, optional + Maximum number of workers to use for parallel computation. If negative, + the value wraps around from ``os.cpu_count()``. + See :func:`~scipy.fft.fft` for more details. + orthogonalize : bool, optional + Whether to use the orthogonalized IDST variant (see Notes). + Defaults to ``True`` when ``norm="ortho"`` and ``False`` otherwise. + + .. versionadded:: 1.8.0 + + Returns + ------- + idst : ndarray of real + The transformed input array. + + See Also + -------- + dst : Forward DST + + Notes + ----- + .. warning:: For ``type in {2, 3}``, ``norm="ortho"`` breaks the direct + correspondence with the inverse direct Fourier transform. + + For ``norm="ortho"`` both the `dst` and `idst` are scaled by the same + overall factor in both directions. By default, the transform is also + orthogonalized which for types 2 and 3 means the transform definition is + modified to give orthogonality of the DST matrix (see `dst` for the full + definitions). + + 'The' IDST is the IDST-II, which is the same as the normalized DST-III. + + The IDST is equivalent to a normal DST except for the normalization and + type. DST type 1 and 4 are their own inverse and DSTs 2 and 3 are each + other's inverses. + + """ + return (Dispatchable(x, np.ndarray),) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_realtransforms_backend.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_realtransforms_backend.py new file mode 100644 index 0000000000000000000000000000000000000000..2042453733bec54860974cc1e20ba908e8c9b94d --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/_realtransforms_backend.py @@ -0,0 +1,63 @@ +from scipy._lib._array_api import array_namespace +import numpy as np +from . import _pocketfft + +__all__ = ['dct', 'idct', 'dst', 'idst', 'dctn', 'idctn', 'dstn', 'idstn'] + + +def _execute(pocketfft_func, x, type, s, axes, norm, + overwrite_x, workers, orthogonalize): + xp = array_namespace(x) + x = np.asarray(x) + y = pocketfft_func(x, type, s, axes, norm, + overwrite_x=overwrite_x, workers=workers, + orthogonalize=orthogonalize) + return xp.asarray(y) + + +def dctn(x, type=2, s=None, axes=None, norm=None, + overwrite_x=False, workers=None, *, orthogonalize=None): + return _execute(_pocketfft.dctn, x, type, s, axes, norm, + overwrite_x, workers, orthogonalize) + + +def idctn(x, type=2, s=None, axes=None, norm=None, + overwrite_x=False, workers=None, *, orthogonalize=None): + return _execute(_pocketfft.idctn, x, type, s, axes, norm, + overwrite_x, workers, orthogonalize) + + +def dstn(x, type=2, s=None, axes=None, norm=None, + overwrite_x=False, workers=None, orthogonalize=None): + return _execute(_pocketfft.dstn, x, type, s, axes, norm, + overwrite_x, workers, orthogonalize) + + +def idstn(x, type=2, s=None, axes=None, norm=None, + overwrite_x=False, workers=None, *, orthogonalize=None): + return _execute(_pocketfft.idstn, x, type, s, axes, norm, + overwrite_x, workers, orthogonalize) + + +def dct(x, type=2, n=None, axis=-1, norm=None, + overwrite_x=False, workers=None, orthogonalize=None): + return _execute(_pocketfft.dct, x, type, n, axis, norm, + overwrite_x, workers, orthogonalize) + + +def idct(x, type=2, n=None, axis=-1, norm=None, + overwrite_x=False, workers=None, orthogonalize=None): + return _execute(_pocketfft.idct, x, type, n, axis, norm, + overwrite_x, workers, orthogonalize) + + +def dst(x, type=2, n=None, axis=-1, norm=None, + overwrite_x=False, workers=None, orthogonalize=None): + return _execute(_pocketfft.dst, x, type, n, axis, norm, + overwrite_x, workers, orthogonalize) + + +def idst(x, type=2, n=None, axis=-1, norm=None, + overwrite_x=False, workers=None, orthogonalize=None): + return _execute(_pocketfft.idst, x, type, n, axis, norm, + overwrite_x, workers, orthogonalize) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/tests/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/tests/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/tests/mock_backend.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/tests/mock_backend.py new file mode 100644 index 0000000000000000000000000000000000000000..48a7d2b3b50501b84f7c18e366ad2e66782b4ab6 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/tests/mock_backend.py @@ -0,0 +1,96 @@ +import numpy as np +import scipy.fft +import threading + +class _MockFunction: + def __init__(self, return_value = None): + self.number_calls = threading.local() + self.return_value = return_value + self.last_args = threading.local() + + def __call__(self, *args, **kwargs): + if not hasattr(self.number_calls, 'c'): + self.number_calls.c = 0 + + self.number_calls.c += 1 + self.last_args.l = (args, kwargs) + return self.return_value + + +fft = _MockFunction(np.random.random(10)) +fft2 = _MockFunction(np.random.random(10)) +fftn = _MockFunction(np.random.random(10)) + +ifft = _MockFunction(np.random.random(10)) +ifft2 = _MockFunction(np.random.random(10)) +ifftn = _MockFunction(np.random.random(10)) + +rfft = _MockFunction(np.random.random(10)) +rfft2 = _MockFunction(np.random.random(10)) +rfftn = _MockFunction(np.random.random(10)) + +irfft = _MockFunction(np.random.random(10)) +irfft2 = _MockFunction(np.random.random(10)) +irfftn = _MockFunction(np.random.random(10)) + +hfft = _MockFunction(np.random.random(10)) +hfft2 = _MockFunction(np.random.random(10)) +hfftn = _MockFunction(np.random.random(10)) + +ihfft = _MockFunction(np.random.random(10)) +ihfft2 = _MockFunction(np.random.random(10)) +ihfftn = _MockFunction(np.random.random(10)) + +dct = _MockFunction(np.random.random(10)) +idct = _MockFunction(np.random.random(10)) +dctn = _MockFunction(np.random.random(10)) +idctn = _MockFunction(np.random.random(10)) + +dst = _MockFunction(np.random.random(10)) +idst = _MockFunction(np.random.random(10)) +dstn = _MockFunction(np.random.random(10)) +idstn = _MockFunction(np.random.random(10)) + +fht = _MockFunction(np.random.random(10)) +ifht = _MockFunction(np.random.random(10)) + + +__ua_domain__ = "numpy.scipy.fft" + + +_implements = { + scipy.fft.fft: fft, + scipy.fft.fft2: fft2, + scipy.fft.fftn: fftn, + scipy.fft.ifft: ifft, + scipy.fft.ifft2: ifft2, + scipy.fft.ifftn: ifftn, + scipy.fft.rfft: rfft, + scipy.fft.rfft2: rfft2, + scipy.fft.rfftn: rfftn, + scipy.fft.irfft: irfft, + scipy.fft.irfft2: irfft2, + scipy.fft.irfftn: irfftn, + scipy.fft.hfft: hfft, + scipy.fft.hfft2: hfft2, + scipy.fft.hfftn: hfftn, + scipy.fft.ihfft: ihfft, + scipy.fft.ihfft2: ihfft2, + scipy.fft.ihfftn: ihfftn, + scipy.fft.dct: dct, + scipy.fft.idct: idct, + scipy.fft.dctn: dctn, + scipy.fft.idctn: idctn, + scipy.fft.dst: dst, + scipy.fft.idst: idst, + scipy.fft.dstn: dstn, + scipy.fft.idstn: idstn, + scipy.fft.fht: fht, + scipy.fft.ifht: ifht +} + + +def __ua_function__(method, args, kwargs): + fn = _implements.get(method) + return (fn(*args, **kwargs) if fn is not None + else NotImplemented) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/tests/test_backend.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/tests/test_backend.py new file mode 100644 index 0000000000000000000000000000000000000000..933e9c0302d46faf33b3dc015d58996e3e46c058 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/tests/test_backend.py @@ -0,0 +1,98 @@ +from functools import partial + +import numpy as np +import scipy.fft +from scipy.fft import _fftlog, _pocketfft, set_backend +from scipy.fft.tests import mock_backend + +from numpy.testing import assert_allclose, assert_equal +import pytest + +fnames = ('fft', 'fft2', 'fftn', + 'ifft', 'ifft2', 'ifftn', + 'rfft', 'rfft2', 'rfftn', + 'irfft', 'irfft2', 'irfftn', + 'dct', 'idct', 'dctn', 'idctn', + 'dst', 'idst', 'dstn', 'idstn', + 'fht', 'ifht') + +np_funcs = (np.fft.fft, np.fft.fft2, np.fft.fftn, + np.fft.ifft, np.fft.ifft2, np.fft.ifftn, + np.fft.rfft, np.fft.rfft2, np.fft.rfftn, + np.fft.irfft, np.fft.irfft2, np.fft.irfftn, + np.fft.hfft, _pocketfft.hfft2, _pocketfft.hfftn, # np has no hfftn + np.fft.ihfft, _pocketfft.ihfft2, _pocketfft.ihfftn, + _pocketfft.dct, _pocketfft.idct, _pocketfft.dctn, _pocketfft.idctn, + _pocketfft.dst, _pocketfft.idst, _pocketfft.dstn, _pocketfft.idstn, + # must provide required kwargs for fht, ifht + partial(_fftlog.fht, dln=2, mu=0.5), + partial(_fftlog.ifht, dln=2, mu=0.5)) + +funcs = (scipy.fft.fft, scipy.fft.fft2, scipy.fft.fftn, + scipy.fft.ifft, scipy.fft.ifft2, scipy.fft.ifftn, + scipy.fft.rfft, scipy.fft.rfft2, scipy.fft.rfftn, + scipy.fft.irfft, scipy.fft.irfft2, scipy.fft.irfftn, + scipy.fft.hfft, scipy.fft.hfft2, scipy.fft.hfftn, + scipy.fft.ihfft, scipy.fft.ihfft2, scipy.fft.ihfftn, + scipy.fft.dct, scipy.fft.idct, scipy.fft.dctn, scipy.fft.idctn, + scipy.fft.dst, scipy.fft.idst, scipy.fft.dstn, scipy.fft.idstn, + # must provide required kwargs for fht, ifht + partial(scipy.fft.fht, dln=2, mu=0.5), + partial(scipy.fft.ifht, dln=2, mu=0.5)) + +mocks = (mock_backend.fft, mock_backend.fft2, mock_backend.fftn, + mock_backend.ifft, mock_backend.ifft2, mock_backend.ifftn, + mock_backend.rfft, mock_backend.rfft2, mock_backend.rfftn, + mock_backend.irfft, mock_backend.irfft2, mock_backend.irfftn, + mock_backend.hfft, mock_backend.hfft2, mock_backend.hfftn, + mock_backend.ihfft, mock_backend.ihfft2, mock_backend.ihfftn, + mock_backend.dct, mock_backend.idct, + mock_backend.dctn, mock_backend.idctn, + mock_backend.dst, mock_backend.idst, + mock_backend.dstn, mock_backend.idstn, + mock_backend.fht, mock_backend.ifht) + + +@pytest.mark.parametrize("func, np_func, mock", zip(funcs, np_funcs, mocks)) +def test_backend_call(func, np_func, mock): + x = np.arange(20).reshape((10,2)) + answer = np_func(x.astype(np.float64)) + assert_allclose(func(x), answer, atol=1e-10) + + with set_backend(mock_backend, only=True): + mock.number_calls.c = 0 + y = func(x) + assert_equal(y, mock.return_value) + assert_equal(mock.number_calls.c, 1) + + assert_allclose(func(x), answer, atol=1e-10) + + +plan_funcs = (scipy.fft.fft, scipy.fft.fft2, scipy.fft.fftn, + scipy.fft.ifft, scipy.fft.ifft2, scipy.fft.ifftn, + scipy.fft.rfft, scipy.fft.rfft2, scipy.fft.rfftn, + scipy.fft.irfft, scipy.fft.irfft2, scipy.fft.irfftn, + scipy.fft.hfft, scipy.fft.hfft2, scipy.fft.hfftn, + scipy.fft.ihfft, scipy.fft.ihfft2, scipy.fft.ihfftn) + +plan_mocks = (mock_backend.fft, mock_backend.fft2, mock_backend.fftn, + mock_backend.ifft, mock_backend.ifft2, mock_backend.ifftn, + mock_backend.rfft, mock_backend.rfft2, mock_backend.rfftn, + mock_backend.irfft, mock_backend.irfft2, mock_backend.irfftn, + mock_backend.hfft, mock_backend.hfft2, mock_backend.hfftn, + mock_backend.ihfft, mock_backend.ihfft2, mock_backend.ihfftn) + + +@pytest.mark.parametrize("func, mock", zip(plan_funcs, plan_mocks)) +def test_backend_plan(func, mock): + x = np.arange(20).reshape((10, 2)) + + with pytest.raises(NotImplementedError, match='precomputed plan'): + func(x, plan='foo') + + with set_backend(mock_backend, only=True): + mock.number_calls.c = 0 + y = func(x, plan='foo') + assert_equal(y, mock.return_value) + assert_equal(mock.number_calls.c, 1) + assert_equal(mock.last_args.l[1]['plan'], 'foo') diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/tests/test_basic.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/tests/test_basic.py new file mode 100644 index 0000000000000000000000000000000000000000..4ed32d54c8936b8b36ff52ef7b639d05338a783c --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/tests/test_basic.py @@ -0,0 +1,502 @@ +import queue +import threading +import multiprocessing +import numpy as np +import pytest +from numpy.random import random +from numpy.testing import assert_array_almost_equal, assert_allclose +from pytest import raises as assert_raises +import scipy.fft as fft +from scipy.conftest import array_api_compatible +from scipy._lib._array_api import ( + array_namespace, xp_size, xp_assert_close, xp_assert_equal +) + +pytestmark = [array_api_compatible, pytest.mark.usefixtures("skip_xp_backends")] +skip_xp_backends = pytest.mark.skip_xp_backends + + +# Expected input dtypes. Note that `scipy.fft` is more flexible for numpy, +# but for C2C transforms like `fft.fft`, the array API standard only mandates +# that complex dtypes should work, float32/float64 aren't guaranteed to. +def get_expected_input_dtype(func, xp): + if func in [fft.fft, fft.fftn, fft.fft2, + fft.ifft, fft.ifftn, fft.ifft2, + fft.hfft, fft.hfftn, fft.hfft2, + fft.irfft, fft.irfftn, fft.irfft2]: + dtype = xp.complex128 + elif func in [fft.rfft, fft.rfftn, fft.rfft2, + fft.ihfft, fft.ihfftn, fft.ihfft2]: + dtype = xp.float64 + else: + raise ValueError(f'Unknown FFT function: {func}') + + return dtype + + +def fft1(x): + L = len(x) + phase = -2j*np.pi*(np.arange(L)/float(L)) + phase = np.arange(L).reshape(-1, 1) * phase + return np.sum(x*np.exp(phase), axis=1) + +class TestFFT: + + def test_identity(self, xp): + maxlen = 512 + x = xp.asarray(random(maxlen) + 1j*random(maxlen)) + xr = xp.asarray(random(maxlen)) + # Check some powers of 2 and some primes + for i in [1, 2, 16, 128, 512, 53, 149, 281, 397]: + xp_assert_close(fft.ifft(fft.fft(x[0:i])), x[0:i]) + xp_assert_close(fft.irfft(fft.rfft(xr[0:i]), i), xr[0:i]) + + @skip_xp_backends(np_only=True, reason='significant overhead for some backends') + def test_identity_extensive(self, xp): + maxlen = 512 + x = xp.asarray(random(maxlen) + 1j*random(maxlen)) + xr = xp.asarray(random(maxlen)) + for i in range(1, maxlen): + xp_assert_close(fft.ifft(fft.fft(x[0:i])), x[0:i]) + xp_assert_close(fft.irfft(fft.rfft(xr[0:i]), i), xr[0:i]) + + def test_fft(self, xp): + x = random(30) + 1j*random(30) + expect = xp.asarray(fft1(x)) + x = xp.asarray(x) + xp_assert_close(fft.fft(x), expect) + xp_assert_close(fft.fft(x, norm="backward"), expect) + xp_assert_close(fft.fft(x, norm="ortho"), + expect / xp.sqrt(xp.asarray(30, dtype=xp.float64)),) + xp_assert_close(fft.fft(x, norm="forward"), expect / 30) + + @skip_xp_backends(np_only=True, reason='some backends allow `n=0`') + def test_fft_n(self, xp): + x = xp.asarray([1, 2, 3], dtype=xp.complex128) + assert_raises(ValueError, fft.fft, x, 0) + + def test_ifft(self, xp): + x = xp.asarray(random(30) + 1j*random(30)) + xp_assert_close(fft.ifft(fft.fft(x)), x) + for norm in ["backward", "ortho", "forward"]: + xp_assert_close(fft.ifft(fft.fft(x, norm=norm), norm=norm), x) + + def test_fft2(self, xp): + x = xp.asarray(random((30, 20)) + 1j*random((30, 20))) + expect = fft.fft(fft.fft(x, axis=1), axis=0) + xp_assert_close(fft.fft2(x), expect) + xp_assert_close(fft.fft2(x, norm="backward"), expect) + xp_assert_close(fft.fft2(x, norm="ortho"), + expect / xp.sqrt(xp.asarray(30 * 20, dtype=xp.float64))) + xp_assert_close(fft.fft2(x, norm="forward"), expect / (30 * 20)) + + def test_ifft2(self, xp): + x = xp.asarray(random((30, 20)) + 1j*random((30, 20))) + expect = fft.ifft(fft.ifft(x, axis=1), axis=0) + xp_assert_close(fft.ifft2(x), expect) + xp_assert_close(fft.ifft2(x, norm="backward"), expect) + xp_assert_close(fft.ifft2(x, norm="ortho"), + expect * xp.sqrt(xp.asarray(30 * 20, dtype=xp.float64))) + xp_assert_close(fft.ifft2(x, norm="forward"), expect * (30 * 20)) + + def test_fftn(self, xp): + x = xp.asarray(random((30, 20, 10)) + 1j*random((30, 20, 10))) + expect = fft.fft(fft.fft(fft.fft(x, axis=2), axis=1), axis=0) + xp_assert_close(fft.fftn(x), expect) + xp_assert_close(fft.fftn(x, norm="backward"), expect) + xp_assert_close(fft.fftn(x, norm="ortho"), + expect / xp.sqrt(xp.asarray(30 * 20 * 10, dtype=xp.float64))) + xp_assert_close(fft.fftn(x, norm="forward"), expect / (30 * 20 * 10)) + + def test_ifftn(self, xp): + x = xp.asarray(random((30, 20, 10)) + 1j*random((30, 20, 10))) + expect = fft.ifft(fft.ifft(fft.ifft(x, axis=2), axis=1), axis=0) + xp_assert_close(fft.ifftn(x), expect, rtol=1e-7) + xp_assert_close(fft.ifftn(x, norm="backward"), expect, rtol=1e-7) + xp_assert_close( + fft.ifftn(x, norm="ortho"), + fft.ifftn(x) * xp.sqrt(xp.asarray(30 * 20 * 10, dtype=xp.float64)) + ) + xp_assert_close(fft.ifftn(x, norm="forward"), + expect * (30 * 20 * 10), + rtol=1e-7) + + def test_rfft(self, xp): + x = xp.asarray(random(29), dtype=xp.float64) + for n in [xp_size(x), 2*xp_size(x)]: + for norm in [None, "backward", "ortho", "forward"]: + xp_assert_close(fft.rfft(x, n=n, norm=norm), + fft.fft(xp.asarray(x, dtype=xp.complex128), + n=n, norm=norm)[:(n//2 + 1)]) + xp_assert_close( + fft.rfft(x, n=n, norm="ortho"), + fft.rfft(x, n=n) / xp.sqrt(xp.asarray(n, dtype=xp.float64)) + ) + + def test_irfft(self, xp): + x = xp.asarray(random(30)) + xp_assert_close(fft.irfft(fft.rfft(x)), x) + for norm in ["backward", "ortho", "forward"]: + xp_assert_close(fft.irfft(fft.rfft(x, norm=norm), norm=norm), x) + + def test_rfft2(self, xp): + x = xp.asarray(random((30, 20)), dtype=xp.float64) + expect = fft.fft2(xp.asarray(x, dtype=xp.complex128))[:, :11] + xp_assert_close(fft.rfft2(x), expect) + xp_assert_close(fft.rfft2(x, norm="backward"), expect) + xp_assert_close(fft.rfft2(x, norm="ortho"), + expect / xp.sqrt(xp.asarray(30 * 20, dtype=xp.float64))) + xp_assert_close(fft.rfft2(x, norm="forward"), expect / (30 * 20)) + + def test_irfft2(self, xp): + x = xp.asarray(random((30, 20))) + xp_assert_close(fft.irfft2(fft.rfft2(x)), x) + for norm in ["backward", "ortho", "forward"]: + xp_assert_close(fft.irfft2(fft.rfft2(x, norm=norm), norm=norm), x) + + def test_rfftn(self, xp): + x = xp.asarray(random((30, 20, 10)), dtype=xp.float64) + expect = fft.fftn(xp.asarray(x, dtype=xp.complex128))[:, :, :6] + xp_assert_close(fft.rfftn(x), expect) + xp_assert_close(fft.rfftn(x, norm="backward"), expect) + xp_assert_close(fft.rfftn(x, norm="ortho"), + expect / xp.sqrt(xp.asarray(30 * 20 * 10, dtype=xp.float64))) + xp_assert_close(fft.rfftn(x, norm="forward"), expect / (30 * 20 * 10)) + + def test_irfftn(self, xp): + x = xp.asarray(random((30, 20, 10))) + xp_assert_close(fft.irfftn(fft.rfftn(x)), x) + for norm in ["backward", "ortho", "forward"]: + xp_assert_close(fft.irfftn(fft.rfftn(x, norm=norm), norm=norm), x) + + def test_hfft(self, xp): + x = random(14) + 1j*random(14) + x_herm = np.concatenate((random(1), x, random(1))) + x = np.concatenate((x_herm, x[::-1].conj())) + x = xp.asarray(x) + x_herm = xp.asarray(x_herm) + expect = xp.real(fft.fft(x)) + xp_assert_close(fft.hfft(x_herm), expect) + xp_assert_close(fft.hfft(x_herm, norm="backward"), expect) + xp_assert_close(fft.hfft(x_herm, norm="ortho"), + expect / xp.sqrt(xp.asarray(30, dtype=xp.float64))) + xp_assert_close(fft.hfft(x_herm, norm="forward"), expect / 30) + + def test_ihfft(self, xp): + x = random(14) + 1j*random(14) + x_herm = np.concatenate((random(1), x, random(1))) + x = np.concatenate((x_herm, x[::-1].conj())) + x = xp.asarray(x) + x_herm = xp.asarray(x_herm) + xp_assert_close(fft.ihfft(fft.hfft(x_herm)), x_herm) + for norm in ["backward", "ortho", "forward"]: + xp_assert_close(fft.ihfft(fft.hfft(x_herm, norm=norm), norm=norm), x_herm) + + def test_hfft2(self, xp): + x = xp.asarray(random((30, 20))) + xp_assert_close(fft.hfft2(fft.ihfft2(x)), x) + for norm in ["backward", "ortho", "forward"]: + xp_assert_close(fft.hfft2(fft.ihfft2(x, norm=norm), norm=norm), x) + + def test_ihfft2(self, xp): + x = xp.asarray(random((30, 20)), dtype=xp.float64) + expect = fft.ifft2(xp.asarray(x, dtype=xp.complex128))[:, :11] + xp_assert_close(fft.ihfft2(x), expect) + xp_assert_close(fft.ihfft2(x, norm="backward"), expect) + xp_assert_close( + fft.ihfft2(x, norm="ortho"), + expect * xp.sqrt(xp.asarray(30 * 20, dtype=xp.float64)) + ) + xp_assert_close(fft.ihfft2(x, norm="forward"), expect * (30 * 20)) + + def test_hfftn(self, xp): + x = xp.asarray(random((30, 20, 10))) + xp_assert_close(fft.hfftn(fft.ihfftn(x)), x) + for norm in ["backward", "ortho", "forward"]: + xp_assert_close(fft.hfftn(fft.ihfftn(x, norm=norm), norm=norm), x) + + def test_ihfftn(self, xp): + x = xp.asarray(random((30, 20, 10)), dtype=xp.float64) + expect = fft.ifftn(xp.asarray(x, dtype=xp.complex128))[:, :, :6] + xp_assert_close(expect, fft.ihfftn(x)) + xp_assert_close(expect, fft.ihfftn(x, norm="backward")) + xp_assert_close( + fft.ihfftn(x, norm="ortho"), + expect * xp.sqrt(xp.asarray(30 * 20 * 10, dtype=xp.float64)) + ) + xp_assert_close(fft.ihfftn(x, norm="forward"), expect * (30 * 20 * 10)) + + def _check_axes(self, op, xp): + dtype = get_expected_input_dtype(op, xp) + x = xp.asarray(random((30, 20, 10)), dtype=dtype) + axes = [(0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0)] + xp_test = array_namespace(x) + for a in axes: + op_tr = op(xp_test.permute_dims(x, axes=a)) + tr_op = xp_test.permute_dims(op(x, axes=a), axes=a) + xp_assert_close(op_tr, tr_op) + + @pytest.mark.parametrize("op", [fft.fftn, fft.ifftn, fft.rfftn, fft.irfftn]) + def test_axes_standard(self, op, xp): + self._check_axes(op, xp) + + @pytest.mark.parametrize("op", [fft.hfftn, fft.ihfftn]) + def test_axes_non_standard(self, op, xp): + self._check_axes(op, xp) + + @pytest.mark.parametrize("op", [fft.fftn, fft.ifftn, + fft.rfftn, fft.irfftn]) + def test_axes_subset_with_shape_standard(self, op, xp): + dtype = get_expected_input_dtype(op, xp) + x = xp.asarray(random((16, 8, 4)), dtype=dtype) + axes = [(0, 1, 2), (0, 2, 1), (1, 2, 0)] + xp_test = array_namespace(x) + for a in axes: + # different shape on the first two axes + shape = tuple([2*x.shape[ax] if ax in a[:2] else x.shape[ax] + for ax in range(x.ndim)]) + # transform only the first two axes + op_tr = op(xp_test.permute_dims(x, axes=a), + s=shape[:2], axes=(0, 1)) + tr_op = xp_test.permute_dims(op(x, s=shape[:2], axes=a[:2]), + axes=a) + xp_assert_close(op_tr, tr_op) + + @pytest.mark.parametrize("op", [fft.fft2, fft.ifft2, + fft.rfft2, fft.irfft2, + fft.hfft2, fft.ihfft2, + fft.hfftn, fft.ihfftn]) + def test_axes_subset_with_shape_non_standard(self, op, xp): + dtype = get_expected_input_dtype(op, xp) + x = xp.asarray(random((16, 8, 4)), dtype=dtype) + axes = [(0, 1, 2), (0, 2, 1), (1, 2, 0)] + xp_test = array_namespace(x) + for a in axes: + # different shape on the first two axes + shape = tuple([2*x.shape[ax] if ax in a[:2] else x.shape[ax] + for ax in range(x.ndim)]) + # transform only the first two axes + op_tr = op(xp_test.permute_dims(x, axes=a), s=shape[:2], axes=(0, 1)) + tr_op = xp_test.permute_dims(op(x, s=shape[:2], axes=a[:2]), axes=a) + xp_assert_close(op_tr, tr_op) + + def test_all_1d_norm_preserving(self, xp): + # verify that round-trip transforms are norm-preserving + x = xp.asarray(random(30), dtype=xp.float64) + xp_test = array_namespace(x) + x_norm = xp_test.linalg.vector_norm(x) + n = xp_size(x) * 2 + func_pairs = [(fft.rfft, fft.irfft), + # hfft: order so the first function takes x.size samples + # (necessary for comparison to x_norm above) + (fft.ihfft, fft.hfft), + # functions that expect complex dtypes at the end + (fft.fft, fft.ifft), + ] + for forw, back in func_pairs: + if forw == fft.fft: + x = xp.asarray(x, dtype=xp.complex128) + x_norm = xp_test.linalg.vector_norm(x) + for n in [xp_size(x), 2*xp_size(x)]: + for norm in ['backward', 'ortho', 'forward']: + tmp = forw(x, n=n, norm=norm) + tmp = back(tmp, n=n, norm=norm) + xp_assert_close(xp_test.linalg.vector_norm(tmp), x_norm) + + @skip_xp_backends(np_only=True) + @pytest.mark.parametrize("dtype", [np.float16, np.longdouble]) + def test_dtypes_nonstandard(self, dtype): + x = random(30).astype(dtype) + out_dtypes = {np.float16: np.complex64, np.longdouble: np.clongdouble} + x_complex = x.astype(out_dtypes[dtype]) + + res_fft = fft.ifft(fft.fft(x)) + res_rfft = fft.irfft(fft.rfft(x)) + res_hfft = fft.hfft(fft.ihfft(x), x.shape[0]) + # Check both numerical results and exact dtype matches + assert_array_almost_equal(res_fft, x_complex) + assert_array_almost_equal(res_rfft, x) + assert_array_almost_equal(res_hfft, x) + assert res_fft.dtype == x_complex.dtype + assert res_rfft.dtype == np.result_type(np.float32, x.dtype) + assert res_hfft.dtype == np.result_type(np.float32, x.dtype) + + @pytest.mark.parametrize("dtype", ["float32", "float64"]) + def test_dtypes_real(self, dtype, xp): + x = xp.asarray(random(30), dtype=getattr(xp, dtype)) + + res_rfft = fft.irfft(fft.rfft(x)) + res_hfft = fft.hfft(fft.ihfft(x), x.shape[0]) + # Check both numerical results and exact dtype matches + xp_assert_close(res_rfft, x) + xp_assert_close(res_hfft, x) + + @pytest.mark.parametrize("dtype", ["complex64", "complex128"]) + def test_dtypes_complex(self, dtype, xp): + rng = np.random.default_rng(1234) + x = xp.asarray(rng.random(30), dtype=getattr(xp, dtype)) + + res_fft = fft.ifft(fft.fft(x)) + # Check both numerical results and exact dtype matches + xp_assert_close(res_fft, x) + + @skip_xp_backends(np_only=True, + reason='array-likes only supported for NumPy backend') + @pytest.mark.parametrize("op", [fft.fft, fft.ifft, + fft.fft2, fft.ifft2, + fft.fftn, fft.ifftn, + fft.rfft, fft.irfft, + fft.rfft2, fft.irfft2, + fft.rfftn, fft.irfftn, + fft.hfft, fft.ihfft, + fft.hfft2, fft.ihfft2, + fft.hfftn, fft.ihfftn,]) + def test_array_like(self, xp, op): + x = [[[1.0, 1.0], [1.0, 1.0]], + [[1.0, 1.0], [1.0, 1.0]], + [[1.0, 1.0], [1.0, 1.0]]] + xp_assert_close(op(x), op(xp.asarray(x))) + + +@skip_xp_backends(np_only=True) +@pytest.mark.parametrize( + "dtype", + [np.float32, np.float64, np.longdouble, + np.complex64, np.complex128, np.clongdouble]) +@pytest.mark.parametrize("order", ["F", 'non-contiguous']) +@pytest.mark.parametrize( + "fft", + [fft.fft, fft.fft2, fft.fftn, + fft.ifft, fft.ifft2, fft.ifftn]) +def test_fft_with_order(dtype, order, fft): + # Check that FFT/IFFT produces identical results for C, Fortran and + # non contiguous arrays + rng = np.random.RandomState(42) + X = rng.rand(8, 7, 13).astype(dtype, copy=False) + if order == 'F': + Y = np.asfortranarray(X) + else: + # Make a non contiguous array + Y = X[::-1] + X = np.ascontiguousarray(X[::-1]) + + if fft.__name__.endswith('fft'): + for axis in range(3): + X_res = fft(X, axis=axis) + Y_res = fft(Y, axis=axis) + assert_array_almost_equal(X_res, Y_res) + elif fft.__name__.endswith(('fft2', 'fftn')): + axes = [(0, 1), (1, 2), (0, 2)] + if fft.__name__.endswith('fftn'): + axes.extend([(0,), (1,), (2,), None]) + for ax in axes: + X_res = fft(X, axes=ax) + Y_res = fft(Y, axes=ax) + assert_array_almost_equal(X_res, Y_res) + else: + raise ValueError + + +@skip_xp_backends(cpu_only=True) +class TestFFTThreadSafe: + threads = 16 + input_shape = (800, 200) + + def _test_mtsame(self, func, *args, xp=None): + def worker(args, q): + q.put(func(*args)) + + q = queue.Queue() + expected = func(*args) + + # Spin off a bunch of threads to call the same function simultaneously + t = [threading.Thread(target=worker, args=(args, q)) + for i in range(self.threads)] + [x.start() for x in t] + + [x.join() for x in t] + + # Make sure all threads returned the correct value + for i in range(self.threads): + xp_assert_equal( + q.get(timeout=5), expected, + err_msg='Function returned wrong value in multithreaded context' + ) + + def test_fft(self, xp): + a = xp.ones(self.input_shape, dtype=xp.complex128) + self._test_mtsame(fft.fft, a, xp=xp) + + def test_ifft(self, xp): + a = xp.full(self.input_shape, 1+0j) + self._test_mtsame(fft.ifft, a, xp=xp) + + def test_rfft(self, xp): + a = xp.ones(self.input_shape) + self._test_mtsame(fft.rfft, a, xp=xp) + + def test_irfft(self, xp): + a = xp.full(self.input_shape, 1+0j) + self._test_mtsame(fft.irfft, a, xp=xp) + + def test_hfft(self, xp): + a = xp.ones(self.input_shape, dtype=xp.complex64) + self._test_mtsame(fft.hfft, a, xp=xp) + + def test_ihfft(self, xp): + a = xp.ones(self.input_shape) + self._test_mtsame(fft.ihfft, a, xp=xp) + + +@skip_xp_backends(np_only=True) +@pytest.mark.parametrize("func", [fft.fft, fft.ifft, fft.rfft, fft.irfft]) +def test_multiprocess(func): + # Test that fft still works after fork (gh-10422) + + with multiprocessing.Pool(2) as p: + res = p.map(func, [np.ones(100) for _ in range(4)]) + + expect = func(np.ones(100)) + for x in res: + assert_allclose(x, expect) + + +class TestIRFFTN: + + def test_not_last_axis_success(self, xp): + ar, ai = np.random.random((2, 16, 8, 32)) + a = ar + 1j*ai + a = xp.asarray(a) + + axes = (-2,) + + # Should not raise error + fft.irfftn(a, axes=axes) + + +@pytest.mark.parametrize("func", [fft.fft, fft.ifft, fft.rfft, fft.irfft, + fft.fftn, fft.ifftn, + fft.rfftn, fft.irfftn, fft.hfft, fft.ihfft]) +def test_non_standard_params(func, xp): + if func in [fft.rfft, fft.rfftn, fft.ihfft]: + dtype = xp.float64 + else: + dtype = xp.complex128 + + if xp.__name__ != 'numpy': + x = xp.asarray([1, 2, 3], dtype=dtype) + # func(x) should not raise an exception + func(x) + assert_raises(ValueError, func, x, workers=2) + # `plan` param is not tested since SciPy does not use it currently + # but should be tested if it comes into use + + +@pytest.mark.parametrize("dtype", ['float32', 'float64']) +@pytest.mark.parametrize("func", [fft.fft, fft.ifft, fft.irfft, + fft.fftn, fft.ifftn, + fft.irfftn, fft.hfft,]) +def test_real_input(func, dtype, xp): + x = xp.asarray([1, 2, 3], dtype=getattr(xp, dtype)) + # func(x) should not raise an exception + func(x) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/tests/test_fftlog.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/tests/test_fftlog.py new file mode 100644 index 0000000000000000000000000000000000000000..3480e165180baf3866ca7c99a313996a2dbbca49 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/tests/test_fftlog.py @@ -0,0 +1,206 @@ +import warnings +import math + +import numpy as np +import pytest + +from scipy.fft._fftlog import fht, ifht, fhtoffset +from scipy.special import poch + +from scipy.conftest import array_api_compatible +from scipy._lib._array_api import xp_assert_close, xp_assert_less, array_namespace + +pytestmark = [array_api_compatible, pytest.mark.usefixtures("skip_xp_backends"),] +skip_xp_backends = pytest.mark.skip_xp_backends + + +def test_fht_agrees_with_fftlog(xp): + # check that fht numerically agrees with the output from Fortran FFTLog, + # the results were generated with the provided `fftlogtest` program, + # after fixing how the k array is generated (divide range by n-1, not n) + + # test function, analytical Hankel transform is of the same form + def f(r, mu): + return r**(mu+1)*np.exp(-r**2/2) + + r = np.logspace(-4, 4, 16) + + dln = np.log(r[1]/r[0]) + mu = 0.3 + offset = 0.0 + bias = 0.0 + + a = xp.asarray(f(r, mu)) + + # test 1: compute as given + ours = fht(a, dln, mu, offset=offset, bias=bias) + theirs = [-0.1159922613593045E-02, +0.1625822618458832E-02, + -0.1949518286432330E-02, +0.3789220182554077E-02, + +0.5093959119952945E-03, +0.2785387803618774E-01, + +0.9944952700848897E-01, +0.4599202164586588E+00, + +0.3157462160881342E+00, -0.8201236844404755E-03, + -0.7834031308271878E-03, +0.3931444945110708E-03, + -0.2697710625194777E-03, +0.3568398050238820E-03, + -0.5554454827797206E-03, +0.8286331026468585E-03] + theirs = xp.asarray(theirs, dtype=xp.float64) + xp_assert_close(ours, theirs) + + # test 2: change to optimal offset + offset = fhtoffset(dln, mu, bias=bias) + ours = fht(a, dln, mu, offset=offset, bias=bias) + theirs = [+0.4353768523152057E-04, -0.9197045663594285E-05, + +0.3150140927838524E-03, +0.9149121960963704E-03, + +0.5808089753959363E-02, +0.2548065256377240E-01, + +0.1339477692089897E+00, +0.4821530509479356E+00, + +0.2659899781579785E+00, -0.1116475278448113E-01, + +0.1791441617592385E-02, -0.4181810476548056E-03, + +0.1314963536765343E-03, -0.5422057743066297E-04, + +0.3208681804170443E-04, -0.2696849476008234E-04] + theirs = xp.asarray(theirs, dtype=xp.float64) + xp_assert_close(ours, theirs) + + # test 3: positive bias + bias = 0.8 + offset = fhtoffset(dln, mu, bias=bias) + ours = fht(a, dln, mu, offset=offset, bias=bias) + theirs = [-7.3436673558316850E+00, +0.1710271207817100E+00, + +0.1065374386206564E+00, -0.5121739602708132E-01, + +0.2636649319269470E-01, +0.1697209218849693E-01, + +0.1250215614723183E+00, +0.4739583261486729E+00, + +0.2841149874912028E+00, -0.8312764741645729E-02, + +0.1024233505508988E-02, -0.1644902767389120E-03, + +0.3305775476926270E-04, -0.7786993194882709E-05, + +0.1962258449520547E-05, -0.8977895734909250E-06] + theirs = xp.asarray(theirs, dtype=xp.float64) + xp_assert_close(ours, theirs) + + # test 4: negative bias + bias = -0.8 + offset = fhtoffset(dln, mu, bias=bias) + ours = fht(a, dln, mu, offset=offset, bias=bias) + theirs = [+0.8985777068568745E-05, +0.4074898209936099E-04, + +0.2123969254700955E-03, +0.1009558244834628E-02, + +0.5131386375222176E-02, +0.2461678673516286E-01, + +0.1235812845384476E+00, +0.4719570096404403E+00, + +0.2893487490631317E+00, -0.1686570611318716E-01, + +0.2231398155172505E-01, -0.1480742256379873E-01, + +0.1692387813500801E+00, +0.3097490354365797E+00, + +2.7593607182401860E+00, 10.5251075070045800E+00] + theirs = xp.asarray(theirs, dtype=xp.float64) + xp_assert_close(ours, theirs) + + +@pytest.mark.parametrize('optimal', [True, False]) +@pytest.mark.parametrize('offset', [0.0, 1.0, -1.0]) +@pytest.mark.parametrize('bias', [0, 0.1, -0.1]) +@pytest.mark.parametrize('n', [64, 63]) +def test_fht_identity(n, bias, offset, optimal, xp): + rng = np.random.RandomState(3491349965) + + a = xp.asarray(rng.standard_normal(n)) + dln = rng.uniform(-1, 1) + mu = rng.uniform(-2, 2) + + if optimal: + offset = fhtoffset(dln, mu, initial=offset, bias=bias) + + A = fht(a, dln, mu, offset=offset, bias=bias) + a_ = ifht(A, dln, mu, offset=offset, bias=bias) + + xp_assert_close(a_, a, rtol=1.5e-7) + + + + +@pytest.mark.thread_unsafe +def test_fht_special_cases(xp): + rng = np.random.RandomState(3491349965) + + a = xp.asarray(rng.standard_normal(64)) + dln = rng.uniform(-1, 1) + + # let x = (mu+1+q)/2, y = (mu+1-q)/2, M = {0, -1, -2, ...} + + # case 1: x in M, y in M => well-defined transform + mu, bias = -4.0, 1.0 + with warnings.catch_warnings(record=True) as record: + fht(a, dln, mu, bias=bias) + assert not record, 'fht warned about a well-defined transform' + + # case 2: x not in M, y in M => well-defined transform + mu, bias = -2.5, 0.5 + with warnings.catch_warnings(record=True) as record: + fht(a, dln, mu, bias=bias) + assert not record, 'fht warned about a well-defined transform' + + # with fht_lock: + # case 3: x in M, y not in M => singular transform + mu, bias = -3.5, 0.5 + with pytest.warns(Warning) as record: + fht(a, dln, mu, bias=bias) + assert record, 'fht did not warn about a singular transform' + + # with fht_lock: + # case 4: x not in M, y in M => singular inverse transform + mu, bias = -2.5, 0.5 + with pytest.warns(Warning) as record: + ifht(a, dln, mu, bias=bias) + assert record, 'ifht did not warn about a singular transform' + + +@pytest.mark.parametrize('n', [64, 63]) +def test_fht_exact(n, xp): + rng = np.random.RandomState(3491349965) + + # for a(r) a power law r^\gamma, the fast Hankel transform produces the + # exact continuous Hankel transform if biased with q = \gamma + + mu = rng.uniform(0, 3) + + # convergence of HT: -1-mu < gamma < 1/2 + gamma = rng.uniform(-1-mu, 1/2) + + r = np.logspace(-2, 2, n) + a = xp.asarray(r**gamma) + + dln = np.log(r[1]/r[0]) + + offset = fhtoffset(dln, mu, initial=0.0, bias=gamma) + + A = fht(a, dln, mu, offset=offset, bias=gamma) + + k = np.exp(offset)/r[::-1] + + # analytical result + At = xp.asarray((2/k)**gamma * poch((mu+1-gamma)/2, gamma)) + + xp_assert_close(A, At) + +@skip_xp_backends(np_only=True, + reason='array-likes only supported for NumPy backend') +@pytest.mark.parametrize("op", [fht, ifht]) +def test_array_like(xp, op): + x = [[[1.0, 1.0], [1.0, 1.0]], + [[1.0, 1.0], [1.0, 1.0]], + [[1.0, 1.0], [1.0, 1.0]]] + xp_assert_close(op(x, 1.0, 2.0), op(xp.asarray(x), 1.0, 2.0)) + +@pytest.mark.parametrize('n', [128, 129]) +def test_gh_21661(xp, n): + one = xp.asarray(1.0) + xp_test = array_namespace(one) + mu = 0.0 + r = np.logspace(-7, 1, n) + dln = math.log(r[1] / r[0]) + offset = fhtoffset(dln, initial=-6 * np.log(10), mu=mu) + r = xp.asarray(r, dtype=one.dtype) + k = math.exp(offset) / xp_test.flip(r, axis=-1) + + def f(x, mu): + return x**(mu + 1)*xp.exp(-x**2/2) + + a_r = f(r, mu) + fht_val = fht(a_r, dln, mu=mu, offset=offset) + a_k = f(k, mu) + rel_err = xp.max(xp.abs((fht_val - a_k) / a_k)) + xp_assert_less(rel_err, xp.asarray(7.28e+16)[()]) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/tests/test_helper.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/tests/test_helper.py new file mode 100644 index 0000000000000000000000000000000000000000..4333886555ffbbb0831f830456a3290f32e317fa --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/tests/test_helper.py @@ -0,0 +1,574 @@ +"""Includes test functions for fftpack.helper module + +Copied from fftpack.helper by Pearu Peterson, October 2005 +Modified for Array API, 2023 + +""" +from scipy.fft._helper import next_fast_len, prev_fast_len, _init_nd_shape_and_axes +from numpy.testing import assert_equal +from pytest import raises as assert_raises +import pytest +import numpy as np +import sys +from scipy.conftest import array_api_compatible +from scipy._lib._array_api import ( + xp_assert_close, get_xp_devices, xp_device, array_namespace +) +from scipy import fft + +pytestmark = [array_api_compatible, pytest.mark.usefixtures("skip_xp_backends")] +skip_xp_backends = pytest.mark.skip_xp_backends + +_5_smooth_numbers = [ + 2, 3, 4, 5, 6, 8, 9, 10, + 2 * 3 * 5, + 2**3 * 3**5, + 2**3 * 3**3 * 5**2, +] + +def test_next_fast_len(): + for n in _5_smooth_numbers: + assert_equal(next_fast_len(n), n) + + +def _assert_n_smooth(x, n): + x_orig = x + if n < 2: + assert False + + while True: + q, r = divmod(x, 2) + if r != 0: + break + x = q + + for d in range(3, n+1, 2): + while True: + q, r = divmod(x, d) + if r != 0: + break + x = q + + assert x == 1, \ + f'x={x_orig} is not {n}-smooth, remainder={x}' + + +@skip_xp_backends(np_only=True) +class TestNextFastLen: + + def test_next_fast_len(self): + np.random.seed(1234) + + def nums(): + yield from range(1, 1000) + yield 2**5 * 3**5 * 4**5 + 1 + + for n in nums(): + m = next_fast_len(n) + _assert_n_smooth(m, 11) + assert m == next_fast_len(n, False) + + m = next_fast_len(n, True) + _assert_n_smooth(m, 5) + + def test_np_integers(self): + ITYPES = [np.int16, np.int32, np.int64, np.uint16, np.uint32, np.uint64] + for ityp in ITYPES: + x = ityp(12345) + testN = next_fast_len(x) + assert_equal(testN, next_fast_len(int(x))) + + def testnext_fast_len_small(self): + hams = { + 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 8, 8: 8, 14: 15, 15: 15, + 16: 16, 17: 18, 1021: 1024, 1536: 1536, 51200000: 51200000 + } + for x, y in hams.items(): + assert_equal(next_fast_len(x, True), y) + + @pytest.mark.xfail(sys.maxsize < 2**32, + reason="Hamming Numbers too large for 32-bit", + raises=ValueError, strict=True) + def testnext_fast_len_big(self): + hams = { + 510183360: 510183360, 510183360 + 1: 512000000, + 511000000: 512000000, + 854296875: 854296875, 854296875 + 1: 859963392, + 196608000000: 196608000000, 196608000000 + 1: 196830000000, + 8789062500000: 8789062500000, 8789062500000 + 1: 8796093022208, + 206391214080000: 206391214080000, + 206391214080000 + 1: 206624260800000, + 470184984576000: 470184984576000, + 470184984576000 + 1: 470715894135000, + 7222041363087360: 7222041363087360, + 7222041363087360 + 1: 7230196133913600, + # power of 5 5**23 + 11920928955078125: 11920928955078125, + 11920928955078125 - 1: 11920928955078125, + # power of 3 3**34 + 16677181699666569: 16677181699666569, + 16677181699666569 - 1: 16677181699666569, + # power of 2 2**54 + 18014398509481984: 18014398509481984, + 18014398509481984 - 1: 18014398509481984, + # above this, int(ceil(n)) == int(ceil(n+1)) + 19200000000000000: 19200000000000000, + 19200000000000000 + 1: 19221679687500000, + 288230376151711744: 288230376151711744, + 288230376151711744 + 1: 288325195312500000, + 288325195312500000 - 1: 288325195312500000, + 288325195312500000: 288325195312500000, + 288325195312500000 + 1: 288555831593533440, + } + for x, y in hams.items(): + assert_equal(next_fast_len(x, True), y) + + def test_keyword_args(self): + assert next_fast_len(11, real=True) == 12 + assert next_fast_len(target=7, real=False) == 7 + +@skip_xp_backends(np_only=True) +class TestPrevFastLen: + + def test_prev_fast_len(self): + np.random.seed(1234) + + def nums(): + yield from range(1, 1000) + yield 2**5 * 3**5 * 4**5 + 1 + + for n in nums(): + m = prev_fast_len(n) + _assert_n_smooth(m, 11) + assert m == prev_fast_len(n, False) + + m = prev_fast_len(n, True) + _assert_n_smooth(m, 5) + + def test_np_integers(self): + ITYPES = [np.int16, np.int32, np.int64, np.uint16, np.uint32, + np.uint64] + for ityp in ITYPES: + x = ityp(12345) + testN = prev_fast_len(x) + assert_equal(testN, prev_fast_len(int(x))) + + testN = prev_fast_len(x, real=True) + assert_equal(testN, prev_fast_len(int(x), real=True)) + + def testprev_fast_len_small(self): + hams = { + 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 6, 8: 8, 14: 12, 15: 15, + 16: 16, 17: 16, 1021: 1000, 1536: 1536, 51200000: 51200000 + } + for x, y in hams.items(): + assert_equal(prev_fast_len(x, True), y) + + hams = { + 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10, + 11: 11, 12: 12, 13: 12, 14: 14, 15: 15, 16: 16, 17: 16, 18: 18, + 19: 18, 20: 20, 21: 21, 22: 22, 120: 120, 121: 121, 122: 121, + 1021: 1008, 1536: 1536, 51200000: 51200000 + } + for x, y in hams.items(): + assert_equal(prev_fast_len(x, False), y) + + @pytest.mark.xfail(sys.maxsize < 2**32, + reason="Hamming Numbers too large for 32-bit", + raises=ValueError, strict=True) + def testprev_fast_len_big(self): + hams = { + # 2**6 * 3**13 * 5**1 + 510183360: 510183360, + 510183360 + 1: 510183360, + 510183360 - 1: 509607936, # 2**21 * 3**5 + # 2**6 * 5**6 * 7**1 * 73**1 + 511000000: 510183360, + 511000000 + 1: 510183360, + 511000000 - 1: 510183360, # 2**6 * 3**13 * 5**1 + # 3**7 * 5**8 + 854296875: 854296875, + 854296875 + 1: 854296875, + 854296875 - 1: 850305600, # 2**6 * 3**12 * 5**2 + # 2**22 * 3**1 * 5**6 + 196608000000: 196608000000, + 196608000000 + 1: 196608000000, + 196608000000 - 1: 195910410240, # 2**13 * 3**14 * 5**1 + # 2**5 * 3**2 * 5**15 + 8789062500000: 8789062500000, + 8789062500000 + 1: 8789062500000, + 8789062500000 - 1: 8748000000000, # 2**11 * 3**7 * 5**9 + # 2**24 * 3**9 * 5**4 + 206391214080000: 206391214080000, + 206391214080000 + 1: 206391214080000, + 206391214080000 - 1: 206158430208000, # 2**39 * 3**1 * 5**3 + # 2**18 * 3**15 * 5**3 + 470184984576000: 470184984576000, + 470184984576000 + 1: 470184984576000, + 470184984576000 - 1: 469654673817600, # 2**33 * 3**7 **5**2 + # 2**25 * 3**16 * 5**1 + 7222041363087360: 7222041363087360, + 7222041363087360 + 1: 7222041363087360, + 7222041363087360 - 1: 7213895789838336, # 2**40 * 3**8 + # power of 5 5**23 + 11920928955078125: 11920928955078125, + 11920928955078125 + 1: 11920928955078125, + 11920928955078125 - 1: 11901557422080000, # 2**14 * 3**19 * 5**4 + # power of 3 3**34 + 16677181699666569: 16677181699666569, + 16677181699666569 + 1: 16677181699666569, + 16677181699666569 - 1: 16607531250000000, # 2**7 * 3**12 * 5**12 + # power of 2 2**54 + 18014398509481984: 18014398509481984, + 18014398509481984 + 1: 18014398509481984, + 18014398509481984 - 1: 18000000000000000, # 2**16 * 3**2 * 5**15 + # 2**20 * 3**1 * 5**14 + 19200000000000000: 19200000000000000, + 19200000000000000 + 1: 19200000000000000, + 19200000000000000 - 1: 19131876000000000, # 2**11 * 3**14 * 5**9 + # 2**58 + 288230376151711744: 288230376151711744, + 288230376151711744 + 1: 288230376151711744, + 288230376151711744 - 1: 288000000000000000, # 2**20 * 3**2 * 5**15 + # 2**5 * 3**10 * 5**16 + 288325195312500000: 288325195312500000, + 288325195312500000 + 1: 288325195312500000, + 288325195312500000 - 1: 288230376151711744, # 2**58 + } + for x, y in hams.items(): + assert_equal(prev_fast_len(x, True), y) + + def test_keyword_args(self): + assert prev_fast_len(11, real=True) == 10 + assert prev_fast_len(target=7, real=False) == 7 + + +@skip_xp_backends(cpu_only=True) +class Test_init_nd_shape_and_axes: + + def test_py_0d_defaults(self, xp): + x = xp.asarray(4) + shape = None + axes = None + + shape_expected = () + axes_expected = [] + + shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes) + + assert shape_res == shape_expected + assert axes_res == axes_expected + + def test_xp_0d_defaults(self, xp): + x = xp.asarray(7.) + shape = None + axes = None + + shape_expected = () + axes_expected = [] + + shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes) + + assert shape_res == shape_expected + assert axes_res == axes_expected + + def test_py_1d_defaults(self, xp): + x = xp.asarray([1, 2, 3]) + shape = None + axes = None + + shape_expected = (3,) + axes_expected = [0] + + shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes) + + assert shape_res == shape_expected + assert axes_res == axes_expected + + def test_xp_1d_defaults(self, xp): + x = xp.arange(0, 1, .1) + shape = None + axes = None + + shape_expected = (10,) + axes_expected = [0] + + shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes) + + assert shape_res == shape_expected + assert axes_res == axes_expected + + def test_py_2d_defaults(self, xp): + x = xp.asarray([[1, 2, 3, 4], + [5, 6, 7, 8]]) + shape = None + axes = None + + shape_expected = (2, 4) + axes_expected = [0, 1] + + shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes) + + assert shape_res == shape_expected + assert axes_res == axes_expected + + def test_xp_2d_defaults(self, xp): + x = xp.arange(0, 1, .1) + x = xp.reshape(x, (5, 2)) + shape = None + axes = None + + shape_expected = (5, 2) + axes_expected = [0, 1] + + shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes) + + assert shape_res == shape_expected + assert axes_res == axes_expected + + def test_xp_5d_defaults(self, xp): + x = xp.zeros([6, 2, 5, 3, 4]) + shape = None + axes = None + + shape_expected = (6, 2, 5, 3, 4) + axes_expected = [0, 1, 2, 3, 4] + + shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes) + + assert shape_res == shape_expected + assert axes_res == axes_expected + + def test_xp_5d_set_shape(self, xp): + x = xp.zeros([6, 2, 5, 3, 4]) + shape = [10, -1, -1, 1, 4] + axes = None + + shape_expected = (10, 2, 5, 1, 4) + axes_expected = [0, 1, 2, 3, 4] + + shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes) + + assert shape_res == shape_expected + assert axes_res == axes_expected + + def test_xp_5d_set_axes(self, xp): + x = xp.zeros([6, 2, 5, 3, 4]) + shape = None + axes = [4, 1, 2] + + shape_expected = (4, 2, 5) + axes_expected = [4, 1, 2] + + shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes) + + assert shape_res == shape_expected + assert axes_res == axes_expected + + def test_xp_5d_set_shape_axes(self, xp): + x = xp.zeros([6, 2, 5, 3, 4]) + shape = [10, -1, 2] + axes = [1, 0, 3] + + shape_expected = (10, 6, 2) + axes_expected = [1, 0, 3] + + shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes) + + assert shape_res == shape_expected + assert axes_res == axes_expected + + def test_shape_axes_subset(self, xp): + x = xp.zeros((2, 3, 4, 5)) + shape, axes = _init_nd_shape_and_axes(x, shape=(5, 5, 5), axes=None) + + assert shape == (5, 5, 5) + assert axes == [1, 2, 3] + + def test_errors(self, xp): + x = xp.zeros(1) + with assert_raises(ValueError, match="axes must be a scalar or " + "iterable of integers"): + _init_nd_shape_and_axes(x, shape=None, axes=[[1, 2], [3, 4]]) + + with assert_raises(ValueError, match="axes must be a scalar or " + "iterable of integers"): + _init_nd_shape_and_axes(x, shape=None, axes=[1., 2., 3., 4.]) + + with assert_raises(ValueError, + match="axes exceeds dimensionality of input"): + _init_nd_shape_and_axes(x, shape=None, axes=[1]) + + with assert_raises(ValueError, + match="axes exceeds dimensionality of input"): + _init_nd_shape_and_axes(x, shape=None, axes=[-2]) + + with assert_raises(ValueError, + match="all axes must be unique"): + _init_nd_shape_and_axes(x, shape=None, axes=[0, 0]) + + with assert_raises(ValueError, match="shape must be a scalar or " + "iterable of integers"): + _init_nd_shape_and_axes(x, shape=[[1, 2], [3, 4]], axes=None) + + with assert_raises(ValueError, match="shape must be a scalar or " + "iterable of integers"): + _init_nd_shape_and_axes(x, shape=[1., 2., 3., 4.], axes=None) + + with assert_raises(ValueError, + match="when given, axes and shape arguments" + " have to be of the same length"): + _init_nd_shape_and_axes(xp.zeros([1, 1, 1, 1]), + shape=[1, 2, 3], axes=[1]) + + with assert_raises(ValueError, + match="invalid number of data points" + r" \(\[0\]\) specified"): + _init_nd_shape_and_axes(x, shape=[0], axes=None) + + with assert_raises(ValueError, + match="invalid number of data points" + r" \(\[-2\]\) specified"): + _init_nd_shape_and_axes(x, shape=-2, axes=None) + + +class TestFFTShift: + + def test_definition(self, xp): + x = xp.asarray([0., 1, 2, 3, 4, -4, -3, -2, -1]) + y = xp.asarray([-4., -3, -2, -1, 0, 1, 2, 3, 4]) + xp_assert_close(fft.fftshift(x), y) + xp_assert_close(fft.ifftshift(y), x) + x = xp.asarray([0., 1, 2, 3, 4, -5, -4, -3, -2, -1]) + y = xp.asarray([-5., -4, -3, -2, -1, 0, 1, 2, 3, 4]) + xp_assert_close(fft.fftshift(x), y) + xp_assert_close(fft.ifftshift(y), x) + + def test_inverse(self, xp): + for n in [1, 4, 9, 100, 211]: + x = xp.asarray(np.random.random((n,))) + xp_assert_close(fft.ifftshift(fft.fftshift(x)), x) + + @skip_xp_backends('cupy', reason='cupy/cupy#8393') + def test_axes_keyword(self, xp): + freqs = xp.asarray([[0., 1, 2], [3, 4, -4], [-3, -2, -1]]) + shifted = xp.asarray([[-1., -3, -2], [2, 0, 1], [-4, 3, 4]]) + xp_assert_close(fft.fftshift(freqs, axes=(0, 1)), shifted) + xp_assert_close(fft.fftshift(freqs, axes=0), fft.fftshift(freqs, axes=(0,))) + xp_assert_close(fft.ifftshift(shifted, axes=(0, 1)), freqs) + xp_assert_close(fft.ifftshift(shifted, axes=0), + fft.ifftshift(shifted, axes=(0,))) + xp_assert_close(fft.fftshift(freqs), shifted) + xp_assert_close(fft.ifftshift(shifted), freqs) + + @skip_xp_backends('cupy', reason='cupy/cupy#8393') + def test_uneven_dims(self, xp): + """ Test 2D input, which has uneven dimension sizes """ + freqs = xp.asarray([ + [0, 1], + [2, 3], + [4, 5] + ], dtype=xp.float64) + + # shift in dimension 0 + shift_dim0 = xp.asarray([ + [4, 5], + [0, 1], + [2, 3] + ], dtype=xp.float64) + xp_assert_close(fft.fftshift(freqs, axes=0), shift_dim0) + xp_assert_close(fft.ifftshift(shift_dim0, axes=0), freqs) + xp_assert_close(fft.fftshift(freqs, axes=(0,)), shift_dim0) + xp_assert_close(fft.ifftshift(shift_dim0, axes=[0]), freqs) + + # shift in dimension 1 + shift_dim1 = xp.asarray([ + [1, 0], + [3, 2], + [5, 4] + ], dtype=xp.float64) + xp_assert_close(fft.fftshift(freqs, axes=1), shift_dim1) + xp_assert_close(fft.ifftshift(shift_dim1, axes=1), freqs) + + # shift in both dimensions + shift_dim_both = xp.asarray([ + [5, 4], + [1, 0], + [3, 2] + ], dtype=xp.float64) + xp_assert_close(fft.fftshift(freqs, axes=(0, 1)), shift_dim_both) + xp_assert_close(fft.ifftshift(shift_dim_both, axes=(0, 1)), freqs) + xp_assert_close(fft.fftshift(freqs, axes=[0, 1]), shift_dim_both) + xp_assert_close(fft.ifftshift(shift_dim_both, axes=[0, 1]), freqs) + + # axes=None (default) shift in all dimensions + xp_assert_close(fft.fftshift(freqs, axes=None), shift_dim_both) + xp_assert_close(fft.ifftshift(shift_dim_both, axes=None), freqs) + xp_assert_close(fft.fftshift(freqs), shift_dim_both) + xp_assert_close(fft.ifftshift(shift_dim_both), freqs) + + +@skip_xp_backends("cupy", + reason="CuPy has not implemented the `device` param") +@skip_xp_backends("jax.numpy", + reason="JAX has not implemented the `device` param") +class TestFFTFreq: + + def test_definition(self, xp): + x = xp.asarray([0, 1, 2, 3, 4, -4, -3, -2, -1], dtype=xp.float64) + x2 = xp.asarray([0, 1, 2, 3, 4, -5, -4, -3, -2, -1], dtype=xp.float64) + + # default dtype varies across backends + + y = 9 * fft.fftfreq(9, xp=xp) + xp_assert_close(y, x, check_dtype=False, check_namespace=True) + + y = 9 * xp.pi * fft.fftfreq(9, xp.pi, xp=xp) + xp_assert_close(y, x, check_dtype=False) + + y = 10 * fft.fftfreq(10, xp=xp) + xp_assert_close(y, x2, check_dtype=False) + + y = 10 * xp.pi * fft.fftfreq(10, xp.pi, xp=xp) + xp_assert_close(y, x2, check_dtype=False) + + def test_device(self, xp): + xp_test = array_namespace(xp.empty(0)) + devices = get_xp_devices(xp) + for d in devices: + y = fft.fftfreq(9, xp=xp, device=d) + x = xp_test.empty(0, device=d) + assert xp_device(y) == xp_device(x) + + +@skip_xp_backends("cupy", + reason="CuPy has not implemented the `device` param") +@skip_xp_backends("jax.numpy", + reason="JAX has not implemented the `device` param") +class TestRFFTFreq: + + def test_definition(self, xp): + x = xp.asarray([0, 1, 2, 3, 4], dtype=xp.float64) + x2 = xp.asarray([0, 1, 2, 3, 4, 5], dtype=xp.float64) + + # default dtype varies across backends + + y = 9 * fft.rfftfreq(9, xp=xp) + xp_assert_close(y, x, check_dtype=False, check_namespace=True) + + y = 9 * xp.pi * fft.rfftfreq(9, xp.pi, xp=xp) + xp_assert_close(y, x, check_dtype=False) + + y = 10 * fft.rfftfreq(10, xp=xp) + xp_assert_close(y, x2, check_dtype=False) + + y = 10 * xp.pi * fft.rfftfreq(10, xp.pi, xp=xp) + xp_assert_close(y, x2, check_dtype=False) + + def test_device(self, xp): + xp_test = array_namespace(xp.empty(0)) + devices = get_xp_devices(xp) + for d in devices: + y = fft.rfftfreq(9, xp=xp, device=d) + x = xp_test.empty(0, device=d) + assert xp_device(y) == xp_device(x) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/tests/test_multithreading.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/tests/test_multithreading.py new file mode 100644 index 0000000000000000000000000000000000000000..1a6b71b830211f8bcbe56e97ff71098be75021c8 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/tests/test_multithreading.py @@ -0,0 +1,84 @@ +from scipy import fft +import numpy as np +import pytest +from numpy.testing import assert_allclose +import multiprocessing +import os + + +@pytest.fixture(scope='module') +def x(): + return np.random.randn(512, 128) # Must be large enough to qualify for mt + + +@pytest.mark.parametrize("func", [ + fft.fft, fft.ifft, fft.fft2, fft.ifft2, fft.fftn, fft.ifftn, + fft.rfft, fft.irfft, fft.rfft2, fft.irfft2, fft.rfftn, fft.irfftn, + fft.hfft, fft.ihfft, fft.hfft2, fft.ihfft2, fft.hfftn, fft.ihfftn, + fft.dct, fft.idct, fft.dctn, fft.idctn, + fft.dst, fft.idst, fft.dstn, fft.idstn, +]) +@pytest.mark.parametrize("workers", [2, -1]) +def test_threaded_same(x, func, workers): + expected = func(x, workers=1) + actual = func(x, workers=workers) + assert_allclose(actual, expected) + + +def _mt_fft(x): + return fft.fft(x, workers=2) + + +@pytest.mark.slow +def test_mixed_threads_processes(x): + # Test that the fft threadpool is safe to use before & after fork + + expect = fft.fft(x, workers=2) + + with multiprocessing.Pool(2) as p: + res = p.map(_mt_fft, [x for _ in range(4)]) + + for r in res: + assert_allclose(r, expect) + + fft.fft(x, workers=2) + + +def test_invalid_workers(x): + cpus = os.cpu_count() + + fft.ifft([1], workers=-cpus) + + with pytest.raises(ValueError, match='workers must not be zero'): + fft.fft(x, workers=0) + + with pytest.raises(ValueError, match='workers value out of range'): + fft.ifft(x, workers=-cpus-1) + + +def test_set_get_workers(): + cpus = os.cpu_count() + assert fft.get_workers() == 1 + with fft.set_workers(4): + assert fft.get_workers() == 4 + + with fft.set_workers(-1): + assert fft.get_workers() == cpus + + assert fft.get_workers() == 4 + + assert fft.get_workers() == 1 + + with fft.set_workers(-cpus): + assert fft.get_workers() == 1 + + +def test_set_workers_invalid(): + + with pytest.raises(ValueError, match='workers must not be zero'): + with fft.set_workers(0): + pass + + with pytest.raises(ValueError, match='workers value out of range'): + with fft.set_workers(-os.cpu_count()-1): + pass diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/tests/test_real_transforms.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/tests/test_real_transforms.py new file mode 100644 index 0000000000000000000000000000000000000000..890dc79640aff571262f5a12f721f62f5c907069 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fft/tests/test_real_transforms.py @@ -0,0 +1,249 @@ +import numpy as np +from numpy.testing import assert_allclose, assert_array_equal +import pytest +import math + +from scipy.fft import dct, idct, dctn, idctn, dst, idst, dstn, idstn +import scipy.fft as fft +from scipy import fftpack +from scipy.conftest import array_api_compatible +from scipy._lib._array_api import xp_copy, xp_assert_close + +pytestmark = [array_api_compatible, pytest.mark.usefixtures("skip_xp_backends")] +skip_xp_backends = pytest.mark.skip_xp_backends + +SQRT_2 = math.sqrt(2) + +# scipy.fft wraps the fftpack versions but with normalized inverse transforms. +# So, the forward transforms and definitions are already thoroughly tested in +# fftpack/test_real_transforms.py + + +@skip_xp_backends(cpu_only=True) +@pytest.mark.parametrize("forward, backward", [(dct, idct), (dst, idst)]) +@pytest.mark.parametrize("type", [1, 2, 3, 4]) +@pytest.mark.parametrize("n", [2, 3, 4, 5, 10, 16]) +@pytest.mark.parametrize("axis", [0, 1]) +@pytest.mark.parametrize("norm", [None, 'backward', 'ortho', 'forward']) +@pytest.mark.parametrize("orthogonalize", [False, True]) +def test_identity_1d(forward, backward, type, n, axis, norm, orthogonalize, xp): + # Test the identity f^-1(f(x)) == x + x = xp.asarray(np.random.rand(n, n)) + + y = forward(x, type, axis=axis, norm=norm, orthogonalize=orthogonalize) + z = backward(y, type, axis=axis, norm=norm, orthogonalize=orthogonalize) + xp_assert_close(z, x) + + pad = [(0, 0)] * 2 + pad[axis] = (0, 4) + + y2 = xp.asarray(np.pad(np.asarray(y), pad, mode='edge')) + z2 = backward(y2, type, n, axis, norm, orthogonalize=orthogonalize) + xp_assert_close(z2, x) + + +@skip_xp_backends(np_only=True, + reason='`overwrite_x` only supported for NumPy backend.') +@pytest.mark.parametrize("forward, backward", [(dct, idct), (dst, idst)]) +@pytest.mark.parametrize("type", [1, 2, 3, 4]) +@pytest.mark.parametrize("dtype", [np.float16, np.float32, np.float64, + np.complex64, np.complex128]) +@pytest.mark.parametrize("axis", [0, 1]) +@pytest.mark.parametrize("norm", [None, 'backward', 'ortho', 'forward']) +@pytest.mark.parametrize("overwrite_x", [True, False]) +def test_identity_1d_overwrite(forward, backward, type, dtype, axis, norm, + overwrite_x): + # Test the identity f^-1(f(x)) == x + x = np.random.rand(7, 8).astype(dtype) + x_orig = x.copy() + + y = forward(x, type, axis=axis, norm=norm, overwrite_x=overwrite_x) + y_orig = y.copy() + z = backward(y, type, axis=axis, norm=norm, overwrite_x=overwrite_x) + if not overwrite_x: + assert_allclose(z, x, rtol=1e-6, atol=1e-6) + assert_array_equal(x, x_orig) + assert_array_equal(y, y_orig) + else: + assert_allclose(z, x_orig, rtol=1e-6, atol=1e-6) + + +@skip_xp_backends(cpu_only=True) +@pytest.mark.parametrize("forward, backward", [(dctn, idctn), (dstn, idstn)]) +@pytest.mark.parametrize("type", [1, 2, 3, 4]) +@pytest.mark.parametrize("shape, axes", + [ + ((4, 4), 0), + ((4, 4), 1), + ((4, 4), None), + ((4, 4), (0, 1)), + ((10, 12), None), + ((10, 12), (0, 1)), + ((4, 5, 6), None), + ((4, 5, 6), 1), + ((4, 5, 6), (0, 2)), + ]) +@pytest.mark.parametrize("norm", [None, 'backward', 'ortho', 'forward']) +@pytest.mark.parametrize("orthogonalize", [False, True]) +def test_identity_nd(forward, backward, type, shape, axes, norm, + orthogonalize, xp): + # Test the identity f^-1(f(x)) == x + + x = xp.asarray(np.random.random(shape)) + + if axes is not None: + shape = np.take(shape, axes) + + y = forward(x, type, axes=axes, norm=norm, orthogonalize=orthogonalize) + z = backward(y, type, axes=axes, norm=norm, orthogonalize=orthogonalize) + xp_assert_close(z, x) + + if axes is None: + pad = [(0, 4)] * x.ndim + elif isinstance(axes, int): + pad = [(0, 0)] * x.ndim + pad[axes] = (0, 4) + else: + pad = [(0, 0)] * x.ndim + + for a in axes: + pad[a] = (0, 4) + + # TODO write an array-agnostic pad + y2 = xp.asarray(np.pad(np.asarray(y), pad, mode='edge')) + z2 = backward(y2, type, shape, axes, norm, orthogonalize=orthogonalize) + xp_assert_close(z2, x) + + +@skip_xp_backends(np_only=True, + reason='`overwrite_x` only supported for NumPy backend.') +@pytest.mark.parametrize("forward, backward", [(dctn, idctn), (dstn, idstn)]) +@pytest.mark.parametrize("type", [1, 2, 3, 4]) +@pytest.mark.parametrize("shape, axes", + [ + ((4, 5), 0), + ((4, 5), 1), + ((4, 5), None), + ]) +@pytest.mark.parametrize("dtype", [np.float16, np.float32, np.float64, + np.complex64, np.complex128]) +@pytest.mark.parametrize("norm", [None, 'backward', 'ortho', 'forward']) +@pytest.mark.parametrize("overwrite_x", [False, True]) +def test_identity_nd_overwrite(forward, backward, type, shape, axes, dtype, + norm, overwrite_x): + # Test the identity f^-1(f(x)) == x + + x = np.random.random(shape).astype(dtype) + x_orig = x.copy() + + if axes is not None: + shape = np.take(shape, axes) + + y = forward(x, type, axes=axes, norm=norm) + y_orig = y.copy() + z = backward(y, type, axes=axes, norm=norm) + if overwrite_x: + assert_allclose(z, x_orig, rtol=1e-6, atol=1e-6) + else: + assert_allclose(z, x, rtol=1e-6, atol=1e-6) + assert_array_equal(x, x_orig) + assert_array_equal(y, y_orig) + + +@skip_xp_backends(cpu_only=True) +@pytest.mark.parametrize("func", ['dct', 'dst', 'dctn', 'dstn']) +@pytest.mark.parametrize("type", [1, 2, 3, 4]) +@pytest.mark.parametrize("norm", [None, 'backward', 'ortho', 'forward']) +def test_fftpack_equivalience(func, type, norm, xp): + x = np.random.rand(8, 16) + fftpack_res = xp.asarray(getattr(fftpack, func)(x, type, norm=norm)) + x = xp.asarray(x) + fft_res = getattr(fft, func)(x, type, norm=norm) + + xp_assert_close(fft_res, fftpack_res) + + +@skip_xp_backends(cpu_only=True) +@pytest.mark.parametrize("func", [dct, dst, dctn, dstn]) +@pytest.mark.parametrize("type", [1, 2, 3, 4]) +def test_orthogonalize_default(func, type, xp): + # Test orthogonalize is the default when norm="ortho", but not otherwise + x = xp.asarray(np.random.rand(100)) + + for norm, ortho in [ + ("forward", False), + ("backward", False), + ("ortho", True), + ]: + a = func(x, type=type, norm=norm, orthogonalize=ortho) + b = func(x, type=type, norm=norm) + xp_assert_close(a, b) + + +@skip_xp_backends(cpu_only=True) +@pytest.mark.parametrize("norm", ["backward", "ortho", "forward"]) +@pytest.mark.parametrize("func, type", [ + (dct, 4), (dst, 1), (dst, 4)]) +def test_orthogonalize_noop(func, type, norm, xp): + # Transforms where orthogonalize is a no-op + x = xp.asarray(np.random.rand(100)) + y1 = func(x, type=type, norm=norm, orthogonalize=True) + y2 = func(x, type=type, norm=norm, orthogonalize=False) + xp_assert_close(y1, y2) + + +@skip_xp_backends('jax.numpy', + reason='jax arrays do not support item assignment', + cpu_only=True) +@pytest.mark.parametrize("norm", ["backward", "ortho", "forward"]) +def test_orthogonalize_dct1(norm, xp): + x = xp.asarray(np.random.rand(100)) + + x2 = xp_copy(x, xp=xp) + x2[0] *= SQRT_2 + x2[-1] *= SQRT_2 + + y1 = dct(x, type=1, norm=norm, orthogonalize=True) + y2 = dct(x2, type=1, norm=norm, orthogonalize=False) + + y2[0] /= SQRT_2 + y2[-1] /= SQRT_2 + xp_assert_close(y1, y2) + + +@skip_xp_backends('jax.numpy', + reason='jax arrays do not support item assignment', + cpu_only=True) +@pytest.mark.parametrize("norm", ["backward", "ortho", "forward"]) +@pytest.mark.parametrize("func", [dct, dst]) +def test_orthogonalize_dcst2(func, norm, xp): + x = xp.asarray(np.random.rand(100)) + y1 = func(x, type=2, norm=norm, orthogonalize=True) + y2 = func(x, type=2, norm=norm, orthogonalize=False) + + y2[0 if func == dct else -1] /= SQRT_2 + xp_assert_close(y1, y2) + + +@skip_xp_backends('jax.numpy', + reason='jax arrays do not support item assignment', + cpu_only=True) +@pytest.mark.parametrize("norm", ["backward", "ortho", "forward"]) +@pytest.mark.parametrize("func", [dct, dst]) +def test_orthogonalize_dcst3(func, norm, xp): + x = xp.asarray(np.random.rand(100)) + x2 = xp_copy(x, xp=xp) + x2[0 if func == dct else -1] *= SQRT_2 + + y1 = func(x, type=3, norm=norm, orthogonalize=True) + y2 = func(x2, type=3, norm=norm, orthogonalize=False) + xp_assert_close(y1, y2) + +@skip_xp_backends(np_only=True, + reason='array-likes only supported for NumPy backend') +@pytest.mark.parametrize("func", [dct, idct, dctn, idctn, dst, idst, dstn, idstn]) +def test_array_like(xp, func): + x = [[[1.0, 1.0], [1.0, 1.0]], + [[1.0, 1.0], [1.0, 1.0]], + [[1.0, 1.0], [1.0, 1.0]]] + xp_assert_close(func(x), func(xp.asarray(x))) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..10f4b39e48e2d6c0b042582ca65f572bde6ba575 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/__init__.py @@ -0,0 +1,103 @@ +""" +========================================================= +Legacy discrete Fourier transforms (:mod:`scipy.fftpack`) +========================================================= + +.. legacy:: + + New code should use :mod:`scipy.fft`. + +Fast Fourier Transforms (FFTs) +============================== + +.. autosummary:: + :toctree: generated/ + + fft - Fast (discrete) Fourier Transform (FFT) + ifft - Inverse FFT + fft2 - 2-D FFT + ifft2 - 2-D inverse FFT + fftn - N-D FFT + ifftn - N-D inverse FFT + rfft - FFT of strictly real-valued sequence + irfft - Inverse of rfft + dct - Discrete cosine transform + idct - Inverse discrete cosine transform + dctn - N-D Discrete cosine transform + idctn - N-D Inverse discrete cosine transform + dst - Discrete sine transform + idst - Inverse discrete sine transform + dstn - N-D Discrete sine transform + idstn - N-D Inverse discrete sine transform + +Differential and pseudo-differential operators +============================================== + +.. autosummary:: + :toctree: generated/ + + diff - Differentiation and integration of periodic sequences + tilbert - Tilbert transform: cs_diff(x,h,h) + itilbert - Inverse Tilbert transform: sc_diff(x,h,h) + hilbert - Hilbert transform: cs_diff(x,inf,inf) + ihilbert - Inverse Hilbert transform: sc_diff(x,inf,inf) + cs_diff - cosh/sinh pseudo-derivative of periodic sequences + sc_diff - sinh/cosh pseudo-derivative of periodic sequences + ss_diff - sinh/sinh pseudo-derivative of periodic sequences + cc_diff - cosh/cosh pseudo-derivative of periodic sequences + shift - Shift periodic sequences + +Helper functions +================ + +.. autosummary:: + :toctree: generated/ + + fftshift - Shift the zero-frequency component to the center of the spectrum + ifftshift - The inverse of `fftshift` + fftfreq - Return the Discrete Fourier Transform sample frequencies + rfftfreq - DFT sample frequencies (for usage with rfft, irfft) + next_fast_len - Find the optimal length to zero-pad an FFT for speed + +Note that ``fftshift``, ``ifftshift`` and ``fftfreq`` are numpy functions +exposed by ``fftpack``; importing them from ``numpy`` should be preferred. + +Convolutions (:mod:`scipy.fftpack.convolve`) +============================================ + +.. module:: scipy.fftpack.convolve + +.. autosummary:: + :toctree: generated/ + + convolve + convolve_z + init_convolution_kernel + destroy_convolve_cache + +""" + + +__all__ = ['fft','ifft','fftn','ifftn','rfft','irfft', + 'fft2','ifft2', + 'diff', + 'tilbert','itilbert','hilbert','ihilbert', + 'sc_diff','cs_diff','cc_diff','ss_diff', + 'shift', + 'fftfreq', 'rfftfreq', + 'fftshift', 'ifftshift', + 'next_fast_len', + 'dct', 'idct', 'dst', 'idst', 'dctn', 'idctn', 'dstn', 'idstn' + ] + +from ._basic import * +from ._pseudo_diffs import * +from ._helper import * +from ._realtransforms import * + +# Deprecated namespaces, to be removed in v2.0.0 +from . import basic, helper, pseudo_diffs, realtransforms + +from scipy._lib._testutils import PytestTester +test = PytestTester(__name__) +del PytestTester diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/_basic.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/_basic.py new file mode 100644 index 0000000000000000000000000000000000000000..59c85ae4b364464a66489ef221f7f7ac45624694 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/_basic.py @@ -0,0 +1,428 @@ +""" +Discrete Fourier Transforms - _basic.py +""" +# Created by Pearu Peterson, August,September 2002 +__all__ = ['fft','ifft','fftn','ifftn','rfft','irfft', + 'fft2','ifft2'] + +from scipy.fft import _pocketfft +from ._helper import _good_shape + + +def fft(x, n=None, axis=-1, overwrite_x=False): + """ + Return discrete Fourier transform of real or complex sequence. + + The returned complex array contains ``y(0), y(1),..., y(n-1)``, where + + ``y(j) = (x * exp(-2*pi*sqrt(-1)*j*np.arange(n)/n)).sum()``. + + Parameters + ---------- + x : array_like + Array to Fourier transform. + n : int, optional + Length of the Fourier transform. If ``n < x.shape[axis]``, `x` is + truncated. If ``n > x.shape[axis]``, `x` is zero-padded. The + default results in ``n = x.shape[axis]``. + axis : int, optional + Axis along which the fft's are computed; the default is over the + last axis (i.e., ``axis=-1``). + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + + Returns + ------- + z : complex ndarray + with the elements:: + + [y(0),y(1),..,y(n/2),y(1-n/2),...,y(-1)] if n is even + [y(0),y(1),..,y((n-1)/2),y(-(n-1)/2),...,y(-1)] if n is odd + + where:: + + y(j) = sum[k=0..n-1] x[k] * exp(-sqrt(-1)*j*k* 2*pi/n), j = 0..n-1 + + See Also + -------- + ifft : Inverse FFT + rfft : FFT of a real sequence + + Notes + ----- + The packing of the result is "standard": If ``A = fft(a, n)``, then + ``A[0]`` contains the zero-frequency term, ``A[1:n/2]`` contains the + positive-frequency terms, and ``A[n/2:]`` contains the negative-frequency + terms, in order of decreasingly negative frequency. So ,for an 8-point + transform, the frequencies of the result are [0, 1, 2, 3, -4, -3, -2, -1]. + To rearrange the fft output so that the zero-frequency component is + centered, like [-4, -3, -2, -1, 0, 1, 2, 3], use `fftshift`. + + Both single and double precision routines are implemented. Half precision + inputs will be converted to single precision. Non-floating-point inputs + will be converted to double precision. Long-double precision inputs are + not supported. + + This function is most efficient when `n` is a power of two, and least + efficient when `n` is prime. + + Note that if ``x`` is real-valued, then ``A[j] == A[n-j].conjugate()``. + If ``x`` is real-valued and ``n`` is even, then ``A[n/2]`` is real. + + If the data type of `x` is real, a "real FFT" algorithm is automatically + used, which roughly halves the computation time. To increase efficiency + a little further, use `rfft`, which does the same calculation, but only + outputs half of the symmetrical spectrum. If the data is both real and + symmetrical, the `dct` can again double the efficiency by generating + half of the spectrum from half of the signal. + + Examples + -------- + >>> import numpy as np + >>> from scipy.fftpack import fft, ifft + >>> x = np.arange(5) + >>> np.allclose(fft(ifft(x)), x, atol=1e-15) # within numerical accuracy. + True + + """ + return _pocketfft.fft(x, n, axis, None, overwrite_x) + + +def ifft(x, n=None, axis=-1, overwrite_x=False): + """ + Return discrete inverse Fourier transform of real or complex sequence. + + The returned complex array contains ``y(0), y(1),..., y(n-1)``, where + + ``y(j) = (x * exp(2*pi*sqrt(-1)*j*np.arange(n)/n)).mean()``. + + Parameters + ---------- + x : array_like + Transformed data to invert. + n : int, optional + Length of the inverse Fourier transform. If ``n < x.shape[axis]``, + `x` is truncated. If ``n > x.shape[axis]``, `x` is zero-padded. + The default results in ``n = x.shape[axis]``. + axis : int, optional + Axis along which the ifft's are computed; the default is over the + last axis (i.e., ``axis=-1``). + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + + Returns + ------- + ifft : ndarray of floats + The inverse discrete Fourier transform. + + See Also + -------- + fft : Forward FFT + + Notes + ----- + Both single and double precision routines are implemented. Half precision + inputs will be converted to single precision. Non-floating-point inputs + will be converted to double precision. Long-double precision inputs are + not supported. + + This function is most efficient when `n` is a power of two, and least + efficient when `n` is prime. + + If the data type of `x` is real, a "real IFFT" algorithm is automatically + used, which roughly halves the computation time. + + Examples + -------- + >>> from scipy.fftpack import fft, ifft + >>> import numpy as np + >>> x = np.arange(5) + >>> np.allclose(ifft(fft(x)), x, atol=1e-15) # within numerical accuracy. + True + + """ + return _pocketfft.ifft(x, n, axis, None, overwrite_x) + + +def rfft(x, n=None, axis=-1, overwrite_x=False): + """ + Discrete Fourier transform of a real sequence. + + Parameters + ---------- + x : array_like, real-valued + The data to transform. + n : int, optional + Defines the length of the Fourier transform. If `n` is not specified + (the default) then ``n = x.shape[axis]``. If ``n < x.shape[axis]``, + `x` is truncated, if ``n > x.shape[axis]``, `x` is zero-padded. + axis : int, optional + The axis along which the transform is applied. The default is the + last axis. + overwrite_x : bool, optional + If set to true, the contents of `x` can be overwritten. Default is + False. + + Returns + ------- + z : real ndarray + The returned real array contains:: + + [y(0),Re(y(1)),Im(y(1)),...,Re(y(n/2))] if n is even + [y(0),Re(y(1)),Im(y(1)),...,Re(y(n/2)),Im(y(n/2))] if n is odd + + where:: + + y(j) = sum[k=0..n-1] x[k] * exp(-sqrt(-1)*j*k*2*pi/n) + j = 0..n-1 + + See Also + -------- + fft, irfft, scipy.fft.rfft + + Notes + ----- + Within numerical accuracy, ``y == rfft(irfft(y))``. + + Both single and double precision routines are implemented. Half precision + inputs will be converted to single precision. Non-floating-point inputs + will be converted to double precision. Long-double precision inputs are + not supported. + + To get an output with a complex datatype, consider using the newer + function `scipy.fft.rfft`. + + Examples + -------- + >>> from scipy.fftpack import fft, rfft + >>> a = [9, -9, 1, 3] + >>> fft(a) + array([ 4. +0.j, 8.+12.j, 16. +0.j, 8.-12.j]) + >>> rfft(a) + array([ 4., 8., 12., 16.]) + + """ + return _pocketfft.rfft_fftpack(x, n, axis, None, overwrite_x) + + +def irfft(x, n=None, axis=-1, overwrite_x=False): + """ + Return inverse discrete Fourier transform of real sequence x. + + The contents of `x` are interpreted as the output of the `rfft` + function. + + Parameters + ---------- + x : array_like + Transformed data to invert. + n : int, optional + Length of the inverse Fourier transform. + If n < x.shape[axis], x is truncated. + If n > x.shape[axis], x is zero-padded. + The default results in n = x.shape[axis]. + axis : int, optional + Axis along which the ifft's are computed; the default is over + the last axis (i.e., axis=-1). + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + + Returns + ------- + irfft : ndarray of floats + The inverse discrete Fourier transform. + + See Also + -------- + rfft, ifft, scipy.fft.irfft + + Notes + ----- + The returned real array contains:: + + [y(0),y(1),...,y(n-1)] + + where for n is even:: + + y(j) = 1/n (sum[k=1..n/2-1] (x[2*k-1]+sqrt(-1)*x[2*k]) + * exp(sqrt(-1)*j*k* 2*pi/n) + + c.c. + x[0] + (-1)**(j) x[n-1]) + + and for n is odd:: + + y(j) = 1/n (sum[k=1..(n-1)/2] (x[2*k-1]+sqrt(-1)*x[2*k]) + * exp(sqrt(-1)*j*k* 2*pi/n) + + c.c. + x[0]) + + c.c. denotes complex conjugate of preceding expression. + + For details on input parameters, see `rfft`. + + To process (conjugate-symmetric) frequency-domain data with a complex + datatype, consider using the newer function `scipy.fft.irfft`. + + Examples + -------- + >>> from scipy.fftpack import rfft, irfft + >>> a = [1.0, 2.0, 3.0, 4.0, 5.0] + >>> irfft(a) + array([ 2.6 , -3.16405192, 1.24398433, -1.14955713, 1.46962473]) + >>> irfft(rfft(a)) + array([1., 2., 3., 4., 5.]) + + """ + return _pocketfft.irfft_fftpack(x, n, axis, None, overwrite_x) + + +def fftn(x, shape=None, axes=None, overwrite_x=False): + """ + Return multidimensional discrete Fourier transform. + + The returned array contains:: + + y[j_1,..,j_d] = sum[k_1=0..n_1-1, ..., k_d=0..n_d-1] + x[k_1,..,k_d] * prod[i=1..d] exp(-sqrt(-1)*2*pi/n_i * j_i * k_i) + + where d = len(x.shape) and n = x.shape. + + Parameters + ---------- + x : array_like + The (N-D) array to transform. + shape : int or array_like of ints or None, optional + The shape of the result. If both `shape` and `axes` (see below) are + None, `shape` is ``x.shape``; if `shape` is None but `axes` is + not None, then `shape` is ``numpy.take(x.shape, axes, axis=0)``. + If ``shape[i] > x.shape[i]``, the ith dimension is padded with zeros. + If ``shape[i] < x.shape[i]``, the ith dimension is truncated to + length ``shape[i]``. + If any element of `shape` is -1, the size of the corresponding + dimension of `x` is used. + axes : int or array_like of ints or None, optional + The axes of `x` (`y` if `shape` is not None) along which the + transform is applied. + The default is over all axes. + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed. Default is False. + + Returns + ------- + y : complex-valued N-D NumPy array + The (N-D) DFT of the input array. + + See Also + -------- + ifftn + + Notes + ----- + If ``x`` is real-valued, then + ``y[..., j_i, ...] == y[..., n_i-j_i, ...].conjugate()``. + + Both single and double precision routines are implemented. Half precision + inputs will be converted to single precision. Non-floating-point inputs + will be converted to double precision. Long-double precision inputs are + not supported. + + Examples + -------- + >>> import numpy as np + >>> from scipy.fftpack import fftn, ifftn + >>> y = (-np.arange(16), 8 - np.arange(16), np.arange(16)) + >>> np.allclose(y, fftn(ifftn(y))) + True + + """ + shape = _good_shape(x, shape, axes) + return _pocketfft.fftn(x, shape, axes, None, overwrite_x) + + +def ifftn(x, shape=None, axes=None, overwrite_x=False): + """ + Return inverse multidimensional discrete Fourier transform. + + The sequence can be of an arbitrary type. + + The returned array contains:: + + y[j_1,..,j_d] = 1/p * sum[k_1=0..n_1-1, ..., k_d=0..n_d-1] + x[k_1,..,k_d] * prod[i=1..d] exp(sqrt(-1)*2*pi/n_i * j_i * k_i) + + where ``d = len(x.shape)``, ``n = x.shape``, and ``p = prod[i=1..d] n_i``. + + For description of parameters see `fftn`. + + See Also + -------- + fftn : for detailed information. + + Examples + -------- + >>> from scipy.fftpack import fftn, ifftn + >>> import numpy as np + >>> y = (-np.arange(16), 8 - np.arange(16), np.arange(16)) + >>> np.allclose(y, ifftn(fftn(y))) + True + + """ + shape = _good_shape(x, shape, axes) + return _pocketfft.ifftn(x, shape, axes, None, overwrite_x) + + +def fft2(x, shape=None, axes=(-2,-1), overwrite_x=False): + """ + 2-D discrete Fourier transform. + + Return the 2-D discrete Fourier transform of the 2-D argument + `x`. + + See Also + -------- + fftn : for detailed information. + + Examples + -------- + >>> import numpy as np + >>> from scipy.fftpack import fft2, ifft2 + >>> y = np.mgrid[:5, :5][0] + >>> y + array([[0, 0, 0, 0, 0], + [1, 1, 1, 1, 1], + [2, 2, 2, 2, 2], + [3, 3, 3, 3, 3], + [4, 4, 4, 4, 4]]) + >>> np.allclose(y, ifft2(fft2(y))) + True + """ + return fftn(x,shape,axes,overwrite_x) + + +def ifft2(x, shape=None, axes=(-2,-1), overwrite_x=False): + """ + 2-D discrete inverse Fourier transform of real or complex sequence. + + Return inverse 2-D discrete Fourier transform of + arbitrary type sequence x. + + See `ifft` for more information. + + See Also + -------- + fft2, ifft + + Examples + -------- + >>> import numpy as np + >>> from scipy.fftpack import fft2, ifft2 + >>> y = np.mgrid[:5, :5][0] + >>> y + array([[0, 0, 0, 0, 0], + [1, 1, 1, 1, 1], + [2, 2, 2, 2, 2], + [3, 3, 3, 3, 3], + [4, 4, 4, 4, 4]]) + >>> np.allclose(y, fft2(ifft2(y))) + True + + """ + return ifftn(x,shape,axes,overwrite_x) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/_helper.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/_helper.py new file mode 100644 index 0000000000000000000000000000000000000000..7892543732906dcd86b4c1aa9c1f249af701c137 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/_helper.py @@ -0,0 +1,115 @@ +import operator + +import numpy as np +from numpy.fft import fftshift, ifftshift, fftfreq + +import scipy.fft._pocketfft.helper as _helper + +__all__ = ['fftshift', 'ifftshift', 'fftfreq', 'rfftfreq', 'next_fast_len'] + + +def rfftfreq(n, d=1.0): + """DFT sample frequencies (for usage with rfft, irfft). + + The returned float array contains the frequency bins in + cycles/unit (with zero at the start) given a window length `n` and a + sample spacing `d`:: + + f = [0,1,1,2,2,...,n/2-1,n/2-1,n/2]/(d*n) if n is even + f = [0,1,1,2,2,...,n/2-1,n/2-1,n/2,n/2]/(d*n) if n is odd + + Parameters + ---------- + n : int + Window length. + d : scalar, optional + Sample spacing. Default is 1. + + Returns + ------- + out : ndarray + The array of length `n`, containing the sample frequencies. + + Examples + -------- + >>> import numpy as np + >>> from scipy import fftpack + >>> sig = np.array([-2, 8, 6, 4, 1, 0, 3, 5], dtype=float) + >>> sig_fft = fftpack.rfft(sig) + >>> n = sig_fft.size + >>> timestep = 0.1 + >>> freq = fftpack.rfftfreq(n, d=timestep) + >>> freq + array([ 0. , 1.25, 1.25, 2.5 , 2.5 , 3.75, 3.75, 5. ]) + + """ + n = operator.index(n) + if n < 0: + raise ValueError(f"n = {n} is not valid. " + "n must be a nonnegative integer.") + + return (np.arange(1, n + 1, dtype=int) // 2) / float(n * d) + + +def next_fast_len(target): + """ + Find the next fast size of input data to `fft`, for zero-padding, etc. + + SciPy's FFTPACK has efficient functions for radix {2, 3, 4, 5}, so this + returns the next composite of the prime factors 2, 3, and 5 which is + greater than or equal to `target`. (These are also known as 5-smooth + numbers, regular numbers, or Hamming numbers.) + + Parameters + ---------- + target : int + Length to start searching from. Must be a positive integer. + + Returns + ------- + out : int + The first 5-smooth number greater than or equal to `target`. + + Notes + ----- + .. versionadded:: 0.18.0 + + Examples + -------- + On a particular machine, an FFT of prime length takes 133 ms: + + >>> from scipy import fftpack + >>> import numpy as np + >>> rng = np.random.default_rng() + >>> min_len = 10007 # prime length is worst case for speed + >>> a = rng.standard_normal(min_len) + >>> b = fftpack.fft(a) + + Zero-padding to the next 5-smooth length reduces computation time to + 211 us, a speedup of 630 times: + + >>> fftpack.next_fast_len(min_len) + 10125 + >>> b = fftpack.fft(a, 10125) + + Rounding up to the next power of 2 is not optimal, taking 367 us to + compute, 1.7 times as long as the 5-smooth size: + + >>> b = fftpack.fft(a, 16384) + + """ + # Real transforms use regular sizes so this is backwards compatible + return _helper.good_size(target, True) + + +def _good_shape(x, shape, axes): + """Ensure that shape argument is valid for scipy.fftpack + + scipy.fftpack does not support len(shape) < x.ndim when axes is not given. + """ + if shape is not None and axes is None: + shape = _helper._iterable_of_int(shape, 'shape') + if len(shape) != np.ndim(x): + raise ValueError("when given, axes and shape arguments" + " have to be of the same length") + return shape diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/_pseudo_diffs.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/_pseudo_diffs.py new file mode 100644 index 0000000000000000000000000000000000000000..6dbcc8d3979b35c1497266fea34cf565cd3d11d7 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/_pseudo_diffs.py @@ -0,0 +1,554 @@ +""" +Differential and pseudo-differential operators. +""" +# Created by Pearu Peterson, September 2002 + +__all__ = ['diff', + 'tilbert','itilbert','hilbert','ihilbert', + 'cs_diff','cc_diff','sc_diff','ss_diff', + 'shift'] + +import threading + +from numpy import pi, asarray, sin, cos, sinh, cosh, tanh, iscomplexobj +from . import convolve + +from scipy.fft._pocketfft.helper import _datacopied + + +_cache = threading.local() + + +def diff(x,order=1,period=None, _cache=_cache): + """ + Return kth derivative (or integral) of a periodic sequence x. + + If x_j and y_j are Fourier coefficients of periodic functions x + and y, respectively, then:: + + y_j = pow(sqrt(-1)*j*2*pi/period, order) * x_j + y_0 = 0 if order is not 0. + + Parameters + ---------- + x : array_like + Input array. + order : int, optional + The order of differentiation. Default order is 1. If order is + negative, then integration is carried out under the assumption + that ``x_0 == 0``. + period : float, optional + The assumed period of the sequence. Default is ``2*pi``. + + Notes + ----- + If ``sum(x, axis=0) = 0`` then ``diff(diff(x, k), -k) == x`` (within + numerical accuracy). + + For odd order and even ``len(x)``, the Nyquist mode is taken zero. + + """ + if isinstance(_cache, threading.local): + if not hasattr(_cache, 'diff_cache'): + _cache.diff_cache = {} + _cache = _cache.diff_cache + + tmp = asarray(x) + if order == 0: + return tmp + if iscomplexobj(tmp): + return diff(tmp.real, order, period, _cache)+1j*diff( + tmp.imag, order, period, _cache) + if period is not None: + c = 2*pi/period + else: + c = 1.0 + n = len(x) + omega = _cache.get((n,order,c)) + if omega is None: + if len(_cache) > 20: + while _cache: + _cache.popitem() + + def kernel(k,order=order,c=c): + if k: + return pow(c*k,order) + return 0 + omega = convolve.init_convolution_kernel(n,kernel,d=order, + zero_nyquist=1) + _cache[(n,order,c)] = omega + overwrite_x = _datacopied(tmp, x) + return convolve.convolve(tmp,omega,swap_real_imag=order % 2, + overwrite_x=overwrite_x) + + +def tilbert(x, h, period=None, _cache=_cache): + """ + Return h-Tilbert transform of a periodic sequence x. + + If x_j and y_j are Fourier coefficients of periodic functions x + and y, respectively, then:: + + y_j = sqrt(-1)*coth(j*h*2*pi/period) * x_j + y_0 = 0 + + Parameters + ---------- + x : array_like + The input array to transform. + h : float + Defines the parameter of the Tilbert transform. + period : float, optional + The assumed period of the sequence. Default period is ``2*pi``. + + Returns + ------- + tilbert : ndarray + The result of the transform. + + Notes + ----- + If ``sum(x, axis=0) == 0`` and ``n = len(x)`` is odd, then + ``tilbert(itilbert(x)) == x``. + + If ``2 * pi * h / period`` is approximately 10 or larger, then + numerically ``tilbert == hilbert`` + (theoretically oo-Tilbert == Hilbert). + + For even ``len(x)``, the Nyquist mode of ``x`` is taken zero. + + """ + if isinstance(_cache, threading.local): + if not hasattr(_cache, 'tilbert_cache'): + _cache.tilbert_cache = {} + _cache = _cache.tilbert_cache + + tmp = asarray(x) + if iscomplexobj(tmp): + return tilbert(tmp.real, h, period, _cache) + \ + 1j * tilbert(tmp.imag, h, period, _cache) + + if period is not None: + h = h * 2 * pi / period + + n = len(x) + omega = _cache.get((n, h)) + if omega is None: + if len(_cache) > 20: + while _cache: + _cache.popitem() + + def kernel(k, h=h): + if k: + return 1.0/tanh(h*k) + + return 0 + + omega = convolve.init_convolution_kernel(n, kernel, d=1) + _cache[(n,h)] = omega + + overwrite_x = _datacopied(tmp, x) + return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x) + + +def itilbert(x,h,period=None, _cache=_cache): + """ + Return inverse h-Tilbert transform of a periodic sequence x. + + If ``x_j`` and ``y_j`` are Fourier coefficients of periodic functions x + and y, respectively, then:: + + y_j = -sqrt(-1)*tanh(j*h*2*pi/period) * x_j + y_0 = 0 + + For more details, see `tilbert`. + + """ + if isinstance(_cache, threading.local): + if not hasattr(_cache, 'itilbert_cache'): + _cache.itilbert_cache = {} + _cache = _cache.itilbert_cache + + tmp = asarray(x) + if iscomplexobj(tmp): + return itilbert(tmp.real, h, period, _cache) + \ + 1j*itilbert(tmp.imag, h, period, _cache) + if period is not None: + h = h*2*pi/period + n = len(x) + omega = _cache.get((n,h)) + if omega is None: + if len(_cache) > 20: + while _cache: + _cache.popitem() + + def kernel(k,h=h): + if k: + return -tanh(h*k) + return 0 + omega = convolve.init_convolution_kernel(n,kernel,d=1) + _cache[(n,h)] = omega + overwrite_x = _datacopied(tmp, x) + return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x) + + +def hilbert(x, _cache=_cache): + """ + Return Hilbert transform of a periodic sequence x. + + If x_j and y_j are Fourier coefficients of periodic functions x + and y, respectively, then:: + + y_j = sqrt(-1)*sign(j) * x_j + y_0 = 0 + + Parameters + ---------- + x : array_like + The input array, should be periodic. + _cache : dict, optional + Dictionary that contains the kernel used to do a convolution with. + + Returns + ------- + y : ndarray + The transformed input. + + See Also + -------- + scipy.signal.hilbert : Compute the analytic signal, using the Hilbert + transform. + + Notes + ----- + If ``sum(x, axis=0) == 0`` then ``hilbert(ihilbert(x)) == x``. + + For even len(x), the Nyquist mode of x is taken zero. + + The sign of the returned transform does not have a factor -1 that is more + often than not found in the definition of the Hilbert transform. Note also + that `scipy.signal.hilbert` does have an extra -1 factor compared to this + function. + + """ + if isinstance(_cache, threading.local): + if not hasattr(_cache, 'hilbert_cache'): + _cache.hilbert_cache = {} + _cache = _cache.hilbert_cache + + tmp = asarray(x) + if iscomplexobj(tmp): + return hilbert(tmp.real, _cache) + 1j * hilbert(tmp.imag, _cache) + n = len(x) + omega = _cache.get(n) + if omega is None: + if len(_cache) > 20: + while _cache: + _cache.popitem() + + def kernel(k): + if k > 0: + return 1.0 + elif k < 0: + return -1.0 + return 0.0 + omega = convolve.init_convolution_kernel(n,kernel,d=1) + _cache[n] = omega + overwrite_x = _datacopied(tmp, x) + return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x) + + +def ihilbert(x, _cache=_cache): + """ + Return inverse Hilbert transform of a periodic sequence x. + + If ``x_j`` and ``y_j`` are Fourier coefficients of periodic functions x + and y, respectively, then:: + + y_j = -sqrt(-1)*sign(j) * x_j + y_0 = 0 + + """ + if isinstance(_cache, threading.local): + if not hasattr(_cache, 'ihilbert_cache'): + _cache.ihilbert_cache = {} + _cache = _cache.ihilbert_cache + return -hilbert(x, _cache) + + +def cs_diff(x, a, b, period=None, _cache=_cache): + """ + Return (a,b)-cosh/sinh pseudo-derivative of a periodic sequence. + + If ``x_j`` and ``y_j`` are Fourier coefficients of periodic functions x + and y, respectively, then:: + + y_j = -sqrt(-1)*cosh(j*a*2*pi/period)/sinh(j*b*2*pi/period) * x_j + y_0 = 0 + + Parameters + ---------- + x : array_like + The array to take the pseudo-derivative from. + a, b : float + Defines the parameters of the cosh/sinh pseudo-differential + operator. + period : float, optional + The period of the sequence. Default period is ``2*pi``. + + Returns + ------- + cs_diff : ndarray + Pseudo-derivative of periodic sequence `x`. + + Notes + ----- + For even len(`x`), the Nyquist mode of `x` is taken as zero. + + """ + if isinstance(_cache, threading.local): + if not hasattr(_cache, 'cs_diff_cache'): + _cache.cs_diff_cache = {} + _cache = _cache.cs_diff_cache + + tmp = asarray(x) + if iscomplexobj(tmp): + return cs_diff(tmp.real, a, b, period, _cache) + \ + 1j*cs_diff(tmp.imag, a, b, period, _cache) + if period is not None: + a = a*2*pi/period + b = b*2*pi/period + n = len(x) + omega = _cache.get((n,a,b)) + if omega is None: + if len(_cache) > 20: + while _cache: + _cache.popitem() + + def kernel(k,a=a,b=b): + if k: + return -cosh(a*k)/sinh(b*k) + return 0 + omega = convolve.init_convolution_kernel(n,kernel,d=1) + _cache[(n,a,b)] = omega + overwrite_x = _datacopied(tmp, x) + return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x) + + +def sc_diff(x, a, b, period=None, _cache=_cache): + """ + Return (a,b)-sinh/cosh pseudo-derivative of a periodic sequence x. + + If x_j and y_j are Fourier coefficients of periodic functions x + and y, respectively, then:: + + y_j = sqrt(-1)*sinh(j*a*2*pi/period)/cosh(j*b*2*pi/period) * x_j + y_0 = 0 + + Parameters + ---------- + x : array_like + Input array. + a,b : float + Defines the parameters of the sinh/cosh pseudo-differential + operator. + period : float, optional + The period of the sequence x. Default is 2*pi. + + Notes + ----- + ``sc_diff(cs_diff(x,a,b),b,a) == x`` + For even ``len(x)``, the Nyquist mode of x is taken as zero. + + """ + if isinstance(_cache, threading.local): + if not hasattr(_cache, 'sc_diff_cache'): + _cache.sc_diff_cache = {} + _cache = _cache.sc_diff_cache + + tmp = asarray(x) + if iscomplexobj(tmp): + return sc_diff(tmp.real, a, b, period, _cache) + \ + 1j * sc_diff(tmp.imag, a, b, period, _cache) + if period is not None: + a = a*2*pi/period + b = b*2*pi/period + n = len(x) + omega = _cache.get((n,a,b)) + if omega is None: + if len(_cache) > 20: + while _cache: + _cache.popitem() + + def kernel(k,a=a,b=b): + if k: + return sinh(a*k)/cosh(b*k) + return 0 + omega = convolve.init_convolution_kernel(n,kernel,d=1) + _cache[(n,a,b)] = omega + overwrite_x = _datacopied(tmp, x) + return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x) + + +def ss_diff(x, a, b, period=None, _cache=_cache): + """ + Return (a,b)-sinh/sinh pseudo-derivative of a periodic sequence x. + + If x_j and y_j are Fourier coefficients of periodic functions x + and y, respectively, then:: + + y_j = sinh(j*a*2*pi/period)/sinh(j*b*2*pi/period) * x_j + y_0 = a/b * x_0 + + Parameters + ---------- + x : array_like + The array to take the pseudo-derivative from. + a,b + Defines the parameters of the sinh/sinh pseudo-differential + operator. + period : float, optional + The period of the sequence x. Default is ``2*pi``. + + Notes + ----- + ``ss_diff(ss_diff(x,a,b),b,a) == x`` + + """ + if isinstance(_cache, threading.local): + if not hasattr(_cache, 'ss_diff_cache'): + _cache.ss_diff_cache = {} + _cache = _cache.ss_diff_cache + + tmp = asarray(x) + if iscomplexobj(tmp): + return ss_diff(tmp.real, a, b, period, _cache) + \ + 1j*ss_diff(tmp.imag, a, b, period, _cache) + if period is not None: + a = a*2*pi/period + b = b*2*pi/period + n = len(x) + omega = _cache.get((n,a,b)) + if omega is None: + if len(_cache) > 20: + while _cache: + _cache.popitem() + + def kernel(k,a=a,b=b): + if k: + return sinh(a*k)/sinh(b*k) + return float(a)/b + omega = convolve.init_convolution_kernel(n,kernel) + _cache[(n,a,b)] = omega + overwrite_x = _datacopied(tmp, x) + return convolve.convolve(tmp,omega,overwrite_x=overwrite_x) + + +def cc_diff(x, a, b, period=None, _cache=_cache): + """ + Return (a,b)-cosh/cosh pseudo-derivative of a periodic sequence. + + If x_j and y_j are Fourier coefficients of periodic functions x + and y, respectively, then:: + + y_j = cosh(j*a*2*pi/period)/cosh(j*b*2*pi/period) * x_j + + Parameters + ---------- + x : array_like + The array to take the pseudo-derivative from. + a,b : float + Defines the parameters of the sinh/sinh pseudo-differential + operator. + period : float, optional + The period of the sequence x. Default is ``2*pi``. + + Returns + ------- + cc_diff : ndarray + Pseudo-derivative of periodic sequence `x`. + + Notes + ----- + ``cc_diff(cc_diff(x,a,b),b,a) == x`` + + """ + if isinstance(_cache, threading.local): + if not hasattr(_cache, 'cc_diff_cache'): + _cache.cc_diff_cache = {} + _cache = _cache.cc_diff_cache + + tmp = asarray(x) + if iscomplexobj(tmp): + return cc_diff(tmp.real, a, b, period, _cache) + \ + 1j * cc_diff(tmp.imag, a, b, period, _cache) + if period is not None: + a = a*2*pi/period + b = b*2*pi/period + n = len(x) + omega = _cache.get((n,a,b)) + if omega is None: + if len(_cache) > 20: + while _cache: + _cache.popitem() + + def kernel(k,a=a,b=b): + return cosh(a*k)/cosh(b*k) + omega = convolve.init_convolution_kernel(n,kernel) + _cache[(n,a,b)] = omega + overwrite_x = _datacopied(tmp, x) + return convolve.convolve(tmp,omega,overwrite_x=overwrite_x) + + +def shift(x, a, period=None, _cache=_cache): + """ + Shift periodic sequence x by a: y(u) = x(u+a). + + If x_j and y_j are Fourier coefficients of periodic functions x + and y, respectively, then:: + + y_j = exp(j*a*2*pi/period*sqrt(-1)) * x_f + + Parameters + ---------- + x : array_like + The array to take the pseudo-derivative from. + a : float + Defines the parameters of the sinh/sinh pseudo-differential + period : float, optional + The period of the sequences x and y. Default period is ``2*pi``. + """ + if isinstance(_cache, threading.local): + if not hasattr(_cache, 'shift_cache'): + _cache.shift_cache = {} + _cache = _cache.shift_cache + + tmp = asarray(x) + if iscomplexobj(tmp): + return shift(tmp.real, a, period, _cache) + 1j * shift( + tmp.imag, a, period, _cache) + if period is not None: + a = a*2*pi/period + n = len(x) + omega = _cache.get((n,a)) + if omega is None: + if len(_cache) > 20: + while _cache: + _cache.popitem() + + def kernel_real(k,a=a): + return cos(a*k) + + def kernel_imag(k,a=a): + return sin(a*k) + omega_real = convolve.init_convolution_kernel(n,kernel_real,d=0, + zero_nyquist=0) + omega_imag = convolve.init_convolution_kernel(n,kernel_imag,d=1, + zero_nyquist=0) + _cache[(n,a)] = omega_real,omega_imag + else: + omega_real,omega_imag = omega + overwrite_x = _datacopied(tmp, x) + return convolve.convolve_z(tmp,omega_real,omega_imag, + overwrite_x=overwrite_x) + diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/_realtransforms.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/_realtransforms.py new file mode 100644 index 0000000000000000000000000000000000000000..ad71d517b0ac829ab71850bf67f7dc38636161f2 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/_realtransforms.py @@ -0,0 +1,598 @@ +""" +Real spectrum transforms (DCT, DST, MDCT) +""" + +__all__ = ['dct', 'idct', 'dst', 'idst', 'dctn', 'idctn', 'dstn', 'idstn'] + +from scipy.fft import _pocketfft +from ._helper import _good_shape + +_inverse_typemap = {1: 1, 2: 3, 3: 2, 4: 4} + + +def dctn(x, type=2, shape=None, axes=None, norm=None, overwrite_x=False): + """ + Return multidimensional Discrete Cosine Transform along the specified axes. + + Parameters + ---------- + x : array_like + The input array. + type : {1, 2, 3, 4}, optional + Type of the DCT (see Notes). Default type is 2. + shape : int or array_like of ints or None, optional + The shape of the result. If both `shape` and `axes` (see below) are + None, `shape` is ``x.shape``; if `shape` is None but `axes` is + not None, then `shape` is ``numpy.take(x.shape, axes, axis=0)``. + If ``shape[i] > x.shape[i]``, the ith dimension is padded with zeros. + If ``shape[i] < x.shape[i]``, the ith dimension is truncated to + length ``shape[i]``. + If any element of `shape` is -1, the size of the corresponding + dimension of `x` is used. + axes : int or array_like of ints or None, optional + Axes along which the DCT is computed. + The default is over all axes. + norm : {None, 'ortho'}, optional + Normalization mode (see Notes). Default is None. + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + + Returns + ------- + y : ndarray of real + The transformed input array. + + See Also + -------- + idctn : Inverse multidimensional DCT + + Notes + ----- + For full details of the DCT types and normalization modes, as well as + references, see `dct`. + + Examples + -------- + >>> import numpy as np + >>> from scipy.fftpack import dctn, idctn + >>> rng = np.random.default_rng() + >>> y = rng.standard_normal((16, 16)) + >>> np.allclose(y, idctn(dctn(y, norm='ortho'), norm='ortho')) + True + + """ + shape = _good_shape(x, shape, axes) + return _pocketfft.dctn(x, type, shape, axes, norm, overwrite_x) + + +def idctn(x, type=2, shape=None, axes=None, norm=None, overwrite_x=False): + """ + Return multidimensional Discrete Cosine Transform along the specified axes. + + Parameters + ---------- + x : array_like + The input array. + type : {1, 2, 3, 4}, optional + Type of the DCT (see Notes). Default type is 2. + shape : int or array_like of ints or None, optional + The shape of the result. If both `shape` and `axes` (see below) are + None, `shape` is ``x.shape``; if `shape` is None but `axes` is + not None, then `shape` is ``numpy.take(x.shape, axes, axis=0)``. + If ``shape[i] > x.shape[i]``, the ith dimension is padded with zeros. + If ``shape[i] < x.shape[i]``, the ith dimension is truncated to + length ``shape[i]``. + If any element of `shape` is -1, the size of the corresponding + dimension of `x` is used. + axes : int or array_like of ints or None, optional + Axes along which the IDCT is computed. + The default is over all axes. + norm : {None, 'ortho'}, optional + Normalization mode (see Notes). Default is None. + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + + Returns + ------- + y : ndarray of real + The transformed input array. + + See Also + -------- + dctn : multidimensional DCT + + Notes + ----- + For full details of the IDCT types and normalization modes, as well as + references, see `idct`. + + Examples + -------- + >>> import numpy as np + >>> from scipy.fftpack import dctn, idctn + >>> rng = np.random.default_rng() + >>> y = rng.standard_normal((16, 16)) + >>> np.allclose(y, idctn(dctn(y, norm='ortho'), norm='ortho')) + True + + """ + type = _inverse_typemap[type] + shape = _good_shape(x, shape, axes) + return _pocketfft.dctn(x, type, shape, axes, norm, overwrite_x) + + +def dstn(x, type=2, shape=None, axes=None, norm=None, overwrite_x=False): + """ + Return multidimensional Discrete Sine Transform along the specified axes. + + Parameters + ---------- + x : array_like + The input array. + type : {1, 2, 3, 4}, optional + Type of the DST (see Notes). Default type is 2. + shape : int or array_like of ints or None, optional + The shape of the result. If both `shape` and `axes` (see below) are + None, `shape` is ``x.shape``; if `shape` is None but `axes` is + not None, then `shape` is ``numpy.take(x.shape, axes, axis=0)``. + If ``shape[i] > x.shape[i]``, the ith dimension is padded with zeros. + If ``shape[i] < x.shape[i]``, the ith dimension is truncated to + length ``shape[i]``. + If any element of `shape` is -1, the size of the corresponding + dimension of `x` is used. + axes : int or array_like of ints or None, optional + Axes along which the DCT is computed. + The default is over all axes. + norm : {None, 'ortho'}, optional + Normalization mode (see Notes). Default is None. + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + + Returns + ------- + y : ndarray of real + The transformed input array. + + See Also + -------- + idstn : Inverse multidimensional DST + + Notes + ----- + For full details of the DST types and normalization modes, as well as + references, see `dst`. + + Examples + -------- + >>> import numpy as np + >>> from scipy.fftpack import dstn, idstn + >>> rng = np.random.default_rng() + >>> y = rng.standard_normal((16, 16)) + >>> np.allclose(y, idstn(dstn(y, norm='ortho'), norm='ortho')) + True + + """ + shape = _good_shape(x, shape, axes) + return _pocketfft.dstn(x, type, shape, axes, norm, overwrite_x) + + +def idstn(x, type=2, shape=None, axes=None, norm=None, overwrite_x=False): + """ + Return multidimensional Discrete Sine Transform along the specified axes. + + Parameters + ---------- + x : array_like + The input array. + type : {1, 2, 3, 4}, optional + Type of the DST (see Notes). Default type is 2. + shape : int or array_like of ints or None, optional + The shape of the result. If both `shape` and `axes` (see below) are + None, `shape` is ``x.shape``; if `shape` is None but `axes` is + not None, then `shape` is ``numpy.take(x.shape, axes, axis=0)``. + If ``shape[i] > x.shape[i]``, the ith dimension is padded with zeros. + If ``shape[i] < x.shape[i]``, the ith dimension is truncated to + length ``shape[i]``. + If any element of `shape` is -1, the size of the corresponding + dimension of `x` is used. + axes : int or array_like of ints or None, optional + Axes along which the IDST is computed. + The default is over all axes. + norm : {None, 'ortho'}, optional + Normalization mode (see Notes). Default is None. + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + + Returns + ------- + y : ndarray of real + The transformed input array. + + See Also + -------- + dstn : multidimensional DST + + Notes + ----- + For full details of the IDST types and normalization modes, as well as + references, see `idst`. + + Examples + -------- + >>> import numpy as np + >>> from scipy.fftpack import dstn, idstn + >>> rng = np.random.default_rng() + >>> y = rng.standard_normal((16, 16)) + >>> np.allclose(y, idstn(dstn(y, norm='ortho'), norm='ortho')) + True + + """ + type = _inverse_typemap[type] + shape = _good_shape(x, shape, axes) + return _pocketfft.dstn(x, type, shape, axes, norm, overwrite_x) + + +def dct(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False): + r""" + Return the Discrete Cosine Transform of arbitrary type sequence x. + + Parameters + ---------- + x : array_like + The input array. + type : {1, 2, 3, 4}, optional + Type of the DCT (see Notes). Default type is 2. + n : int, optional + Length of the transform. If ``n < x.shape[axis]``, `x` is + truncated. If ``n > x.shape[axis]``, `x` is zero-padded. The + default results in ``n = x.shape[axis]``. + axis : int, optional + Axis along which the dct is computed; the default is over the + last axis (i.e., ``axis=-1``). + norm : {None, 'ortho'}, optional + Normalization mode (see Notes). Default is None. + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + + Returns + ------- + y : ndarray of real + The transformed input array. + + See Also + -------- + idct : Inverse DCT + + Notes + ----- + For a single dimension array ``x``, ``dct(x, norm='ortho')`` is equal to + MATLAB ``dct(x)``. + + There are, theoretically, 8 types of the DCT, only the first 4 types are + implemented in scipy. 'The' DCT generally refers to DCT type 2, and 'the' + Inverse DCT generally refers to DCT type 3. + + **Type I** + + There are several definitions of the DCT-I; we use the following + (for ``norm=None``) + + .. math:: + + y_k = x_0 + (-1)^k x_{N-1} + 2 \sum_{n=1}^{N-2} x_n \cos\left( + \frac{\pi k n}{N-1} \right) + + If ``norm='ortho'``, ``x[0]`` and ``x[N-1]`` are multiplied by a scaling + factor of :math:`\sqrt{2}`, and ``y[k]`` is multiplied by a scaling factor + ``f`` + + .. math:: + + f = \begin{cases} + \frac{1}{2}\sqrt{\frac{1}{N-1}} & \text{if }k=0\text{ or }N-1, \\ + \frac{1}{2}\sqrt{\frac{2}{N-1}} & \text{otherwise} \end{cases} + + .. versionadded:: 1.2.0 + Orthonormalization in DCT-I. + + .. note:: + The DCT-I is only supported for input size > 1. + + **Type II** + + There are several definitions of the DCT-II; we use the following + (for ``norm=None``) + + .. math:: + + y_k = 2 \sum_{n=0}^{N-1} x_n \cos\left(\frac{\pi k(2n+1)}{2N} \right) + + If ``norm='ortho'``, ``y[k]`` is multiplied by a scaling factor ``f`` + + .. math:: + f = \begin{cases} + \sqrt{\frac{1}{4N}} & \text{if }k=0, \\ + \sqrt{\frac{1}{2N}} & \text{otherwise} \end{cases} + + which makes the corresponding matrix of coefficients orthonormal + (``O @ O.T = np.eye(N)``). + + **Type III** + + There are several definitions, we use the following (for ``norm=None``) + + .. math:: + + y_k = x_0 + 2 \sum_{n=1}^{N-1} x_n \cos\left(\frac{\pi(2k+1)n}{2N}\right) + + or, for ``norm='ortho'`` + + .. math:: + + y_k = \frac{x_0}{\sqrt{N}} + \sqrt{\frac{2}{N}} \sum_{n=1}^{N-1} x_n + \cos\left(\frac{\pi(2k+1)n}{2N}\right) + + The (unnormalized) DCT-III is the inverse of the (unnormalized) DCT-II, up + to a factor ``2N``. The orthonormalized DCT-III is exactly the inverse of + the orthonormalized DCT-II. + + **Type IV** + + There are several definitions of the DCT-IV; we use the following + (for ``norm=None``) + + .. math:: + + y_k = 2 \sum_{n=0}^{N-1} x_n \cos\left(\frac{\pi(2k+1)(2n+1)}{4N} \right) + + If ``norm='ortho'``, ``y[k]`` is multiplied by a scaling factor ``f`` + + .. math:: + + f = \frac{1}{\sqrt{2N}} + + .. versionadded:: 1.2.0 + Support for DCT-IV. + + References + ---------- + .. [1] 'A Fast Cosine Transform in One and Two Dimensions', by J. + Makhoul, `IEEE Transactions on acoustics, speech and signal + processing` vol. 28(1), pp. 27-34, + :doi:`10.1109/TASSP.1980.1163351` (1980). + .. [2] Wikipedia, "Discrete cosine transform", + https://en.wikipedia.org/wiki/Discrete_cosine_transform + + Examples + -------- + The Type 1 DCT is equivalent to the FFT (though faster) for real, + even-symmetrical inputs. The output is also real and even-symmetrical. + Half of the FFT input is used to generate half of the FFT output: + + >>> from scipy.fftpack import fft, dct + >>> import numpy as np + >>> fft(np.array([4., 3., 5., 10., 5., 3.])).real + array([ 30., -8., 6., -2., 6., -8.]) + >>> dct(np.array([4., 3., 5., 10.]), 1) + array([ 30., -8., 6., -2.]) + + """ + return _pocketfft.dct(x, type, n, axis, norm, overwrite_x) + + +def idct(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False): + """ + Return the Inverse Discrete Cosine Transform of an arbitrary type sequence. + + Parameters + ---------- + x : array_like + The input array. + type : {1, 2, 3, 4}, optional + Type of the DCT (see Notes). Default type is 2. + n : int, optional + Length of the transform. If ``n < x.shape[axis]``, `x` is + truncated. If ``n > x.shape[axis]``, `x` is zero-padded. The + default results in ``n = x.shape[axis]``. + axis : int, optional + Axis along which the idct is computed; the default is over the + last axis (i.e., ``axis=-1``). + norm : {None, 'ortho'}, optional + Normalization mode (see Notes). Default is None. + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + + Returns + ------- + idct : ndarray of real + The transformed input array. + + See Also + -------- + dct : Forward DCT + + Notes + ----- + For a single dimension array `x`, ``idct(x, norm='ortho')`` is equal to + MATLAB ``idct(x)``. + + 'The' IDCT is the IDCT of type 2, which is the same as DCT of type 3. + + IDCT of type 1 is the DCT of type 1, IDCT of type 2 is the DCT of type + 3, and IDCT of type 3 is the DCT of type 2. IDCT of type 4 is the DCT + of type 4. For the definition of these types, see `dct`. + + Examples + -------- + The Type 1 DCT is equivalent to the DFT for real, even-symmetrical + inputs. The output is also real and even-symmetrical. Half of the IFFT + input is used to generate half of the IFFT output: + + >>> from scipy.fftpack import ifft, idct + >>> import numpy as np + >>> ifft(np.array([ 30., -8., 6., -2., 6., -8.])).real + array([ 4., 3., 5., 10., 5., 3.]) + >>> idct(np.array([ 30., -8., 6., -2.]), 1) / 6 + array([ 4., 3., 5., 10.]) + + """ + type = _inverse_typemap[type] + return _pocketfft.dct(x, type, n, axis, norm, overwrite_x) + + +def dst(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False): + r""" + Return the Discrete Sine Transform of arbitrary type sequence x. + + Parameters + ---------- + x : array_like + The input array. + type : {1, 2, 3, 4}, optional + Type of the DST (see Notes). Default type is 2. + n : int, optional + Length of the transform. If ``n < x.shape[axis]``, `x` is + truncated. If ``n > x.shape[axis]``, `x` is zero-padded. The + default results in ``n = x.shape[axis]``. + axis : int, optional + Axis along which the dst is computed; the default is over the + last axis (i.e., ``axis=-1``). + norm : {None, 'ortho'}, optional + Normalization mode (see Notes). Default is None. + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + + Returns + ------- + dst : ndarray of reals + The transformed input array. + + See Also + -------- + idst : Inverse DST + + Notes + ----- + For a single dimension array ``x``. + + There are, theoretically, 8 types of the DST for different combinations of + even/odd boundary conditions and boundary off sets [1]_, only the first + 4 types are implemented in scipy. + + **Type I** + + There are several definitions of the DST-I; we use the following + for ``norm=None``. DST-I assumes the input is odd around `n=-1` and `n=N`. + + .. math:: + + y_k = 2 \sum_{n=0}^{N-1} x_n \sin\left(\frac{\pi(k+1)(n+1)}{N+1}\right) + + Note that the DST-I is only supported for input size > 1. + The (unnormalized) DST-I is its own inverse, up to a factor ``2(N+1)``. + The orthonormalized DST-I is exactly its own inverse. + + **Type II** + + There are several definitions of the DST-II; we use the following for + ``norm=None``. DST-II assumes the input is odd around `n=-1/2` and + `n=N-1/2`; the output is odd around :math:`k=-1` and even around `k=N-1` + + .. math:: + + y_k = 2 \sum_{n=0}^{N-1} x_n \sin\left(\frac{\pi(k+1)(2n+1)}{2N}\right) + + if ``norm='ortho'``, ``y[k]`` is multiplied by a scaling factor ``f`` + + .. math:: + + f = \begin{cases} + \sqrt{\frac{1}{4N}} & \text{if }k = 0, \\ + \sqrt{\frac{1}{2N}} & \text{otherwise} \end{cases} + + **Type III** + + There are several definitions of the DST-III, we use the following (for + ``norm=None``). DST-III assumes the input is odd around `n=-1` and even + around `n=N-1` + + .. math:: + + y_k = (-1)^k x_{N-1} + 2 \sum_{n=0}^{N-2} x_n \sin\left( + \frac{\pi(2k+1)(n+1)}{2N}\right) + + The (unnormalized) DST-III is the inverse of the (unnormalized) DST-II, up + to a factor ``2N``. The orthonormalized DST-III is exactly the inverse of the + orthonormalized DST-II. + + .. versionadded:: 0.11.0 + + **Type IV** + + There are several definitions of the DST-IV, we use the following (for + ``norm=None``). DST-IV assumes the input is odd around `n=-0.5` and even + around `n=N-0.5` + + .. math:: + + y_k = 2 \sum_{n=0}^{N-1} x_n \sin\left(\frac{\pi(2k+1)(2n+1)}{4N}\right) + + The (unnormalized) DST-IV is its own inverse, up to a factor ``2N``. The + orthonormalized DST-IV is exactly its own inverse. + + .. versionadded:: 1.2.0 + Support for DST-IV. + + References + ---------- + .. [1] Wikipedia, "Discrete sine transform", + https://en.wikipedia.org/wiki/Discrete_sine_transform + + """ + return _pocketfft.dst(x, type, n, axis, norm, overwrite_x) + + +def idst(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False): + """ + Return the Inverse Discrete Sine Transform of an arbitrary type sequence. + + Parameters + ---------- + x : array_like + The input array. + type : {1, 2, 3, 4}, optional + Type of the DST (see Notes). Default type is 2. + n : int, optional + Length of the transform. If ``n < x.shape[axis]``, `x` is + truncated. If ``n > x.shape[axis]``, `x` is zero-padded. The + default results in ``n = x.shape[axis]``. + axis : int, optional + Axis along which the idst is computed; the default is over the + last axis (i.e., ``axis=-1``). + norm : {None, 'ortho'}, optional + Normalization mode (see Notes). Default is None. + overwrite_x : bool, optional + If True, the contents of `x` can be destroyed; the default is False. + + Returns + ------- + idst : ndarray of real + The transformed input array. + + See Also + -------- + dst : Forward DST + + Notes + ----- + 'The' IDST is the IDST of type 2, which is the same as DST of type 3. + + IDST of type 1 is the DST of type 1, IDST of type 2 is the DST of type + 3, and IDST of type 3 is the DST of type 2. For the definition of these + types, see `dst`. + + .. versionadded:: 0.11.0 + + """ + type = _inverse_typemap[type] + return _pocketfft.dst(x, type, n, axis, norm, overwrite_x) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/basic.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/basic.py new file mode 100644 index 0000000000000000000000000000000000000000..553f456fe1561c28928ecc4ebe2238459cc60443 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/basic.py @@ -0,0 +1,20 @@ +# This file is not meant for public use and will be removed in SciPy v2.0.0. +# Use the `scipy.fftpack` namespace for importing the functions +# included below. + +from scipy._lib.deprecation import _sub_module_deprecation + +__all__ = [ # noqa: F822 + 'fft','ifft','fftn','ifftn','rfft','irfft', + 'fft2','ifft2' +] + + +def __dir__(): + return __all__ + + +def __getattr__(name): + return _sub_module_deprecation(sub_package="fftpack", module="basic", + private_modules=["_basic"], all=__all__, + attribute=name) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/helper.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/helper.py new file mode 100644 index 0000000000000000000000000000000000000000..fcc7000c215f8a7605a2a59b5767b27b2fcd969d --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/helper.py @@ -0,0 +1,19 @@ +# This file is not meant for public use and will be removed in SciPy v2.0.0. +# Use the `scipy.fftpack` namespace for importing the functions +# included below. + +from scipy._lib.deprecation import _sub_module_deprecation + +__all__ = [ # noqa: F822 + 'fftshift', 'ifftshift', 'fftfreq', 'rfftfreq', 'next_fast_len' +] + + +def __dir__(): + return __all__ + + +def __getattr__(name): + return _sub_module_deprecation(sub_package="fftpack", module="helper", + private_modules=["_helper"], all=__all__, + attribute=name) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/pseudo_diffs.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/pseudo_diffs.py new file mode 100644 index 0000000000000000000000000000000000000000..ecf71ad3256d48d2131c8058072da724cb001af9 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/pseudo_diffs.py @@ -0,0 +1,22 @@ +# This file is not meant for public use and will be removed in SciPy v2.0.0. +# Use the `scipy.fftpack` namespace for importing the functions +# included below. + +from scipy._lib.deprecation import _sub_module_deprecation + +__all__ = [ # noqa: F822 + 'diff', + 'tilbert', 'itilbert', 'hilbert', 'ihilbert', + 'cs_diff', 'cc_diff', 'sc_diff', 'ss_diff', + 'shift', 'convolve' +] + + +def __dir__(): + return __all__ + + +def __getattr__(name): + return _sub_module_deprecation(sub_package="fftpack", module="pseudo_diffs", + private_modules=["_pseudo_diffs"], all=__all__, + attribute=name) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/realtransforms.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/realtransforms.py new file mode 100644 index 0000000000000000000000000000000000000000..9a392198fccf213bc988a79058bd69515e39f510 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/realtransforms.py @@ -0,0 +1,19 @@ +# This file is not meant for public use and will be removed in SciPy v2.0.0. +# Use the `scipy.fftpack` namespace for importing the functions +# included below. + +from scipy._lib.deprecation import _sub_module_deprecation + +__all__ = [ # noqa: F822 + 'dct', 'idct', 'dst', 'idst', 'dctn', 'idctn', 'dstn', 'idstn' +] + + +def __dir__(): + return __all__ + + +def __getattr__(name): + return _sub_module_deprecation(sub_package="fftpack", module="realtransforms", + private_modules=["_realtransforms"], all=__all__, + attribute=name) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/tests/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/tests/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/tests/test_basic.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/tests/test_basic.py new file mode 100644 index 0000000000000000000000000000000000000000..2951471d2abb5c4a88ae0b44c172d3ecc0862d4d --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/tests/test_basic.py @@ -0,0 +1,879 @@ +# Created by Pearu Peterson, September 2002 + +from numpy.testing import (assert_, assert_equal, assert_array_almost_equal, + assert_array_almost_equal_nulp, assert_array_less) +import pytest +from pytest import raises as assert_raises +from scipy.fftpack import ifft, fft, fftn, ifftn, rfft, irfft, fft2 + +from numpy import (arange, array, asarray, zeros, dot, exp, pi, + swapaxes, double, cdouble) +import numpy as np +import numpy.fft +from numpy.random import rand + +# "large" composite numbers supported by FFTPACK +LARGE_COMPOSITE_SIZES = [ + 2**13, + 2**5 * 3**5, + 2**3 * 3**3 * 5**2, +] +SMALL_COMPOSITE_SIZES = [ + 2, + 2*3*5, + 2*2*3*3, +] +# prime +LARGE_PRIME_SIZES = [ + 2011 +] +SMALL_PRIME_SIZES = [ + 29 +] + + +def _assert_close_in_norm(x, y, rtol, size, rdt): + # helper function for testing + err_msg = f"size: {size} rdt: {rdt}" + assert_array_less(np.linalg.norm(x - y), rtol*np.linalg.norm(x), err_msg) + + +def random(size): + return rand(*size) + + +def direct_dft(x): + x = asarray(x) + n = len(x) + y = zeros(n, dtype=cdouble) + w = -arange(n)*(2j*pi/n) + for i in range(n): + y[i] = dot(exp(i*w), x) + return y + + +def direct_idft(x): + x = asarray(x) + n = len(x) + y = zeros(n, dtype=cdouble) + w = arange(n)*(2j*pi/n) + for i in range(n): + y[i] = dot(exp(i*w), x)/n + return y + + +def direct_dftn(x): + x = asarray(x) + for axis in range(len(x.shape)): + x = fft(x, axis=axis) + return x + + +def direct_idftn(x): + x = asarray(x) + for axis in range(len(x.shape)): + x = ifft(x, axis=axis) + return x + + +def direct_rdft(x): + x = asarray(x) + n = len(x) + w = -arange(n)*(2j*pi/n) + r = zeros(n, dtype=double) + for i in range(n//2+1): + y = dot(exp(i*w), x) + if i: + r[2*i-1] = y.real + if 2*i < n: + r[2*i] = y.imag + else: + r[0] = y.real + return r + + +def direct_irdft(x): + x = asarray(x) + n = len(x) + x1 = zeros(n, dtype=cdouble) + for i in range(n//2+1): + if i: + if 2*i < n: + x1[i] = x[2*i-1] + 1j*x[2*i] + x1[n-i] = x[2*i-1] - 1j*x[2*i] + else: + x1[i] = x[2*i-1] + else: + x1[0] = x[0] + return direct_idft(x1).real + + +class _TestFFTBase: + def setup_method(self): + self.cdt = None + self.rdt = None + np.random.seed(1234) + + def test_definition(self): + x = np.array([1,2,3,4+1j,1,2,3,4+2j], dtype=self.cdt) + y = fft(x) + assert_equal(y.dtype, self.cdt) + y1 = direct_dft(x) + assert_array_almost_equal(y,y1) + x = np.array([1,2,3,4+0j,5], dtype=self.cdt) + assert_array_almost_equal(fft(x),direct_dft(x)) + + def test_n_argument_real(self): + x1 = np.array([1,2,3,4], dtype=self.rdt) + x2 = np.array([1,2,3,4], dtype=self.rdt) + y = fft([x1,x2],n=4) + assert_equal(y.dtype, self.cdt) + assert_equal(y.shape,(2,4)) + assert_array_almost_equal(y[0],direct_dft(x1)) + assert_array_almost_equal(y[1],direct_dft(x2)) + + def _test_n_argument_complex(self): + x1 = np.array([1,2,3,4+1j], dtype=self.cdt) + x2 = np.array([1,2,3,4+1j], dtype=self.cdt) + y = fft([x1,x2],n=4) + assert_equal(y.dtype, self.cdt) + assert_equal(y.shape,(2,4)) + assert_array_almost_equal(y[0],direct_dft(x1)) + assert_array_almost_equal(y[1],direct_dft(x2)) + + def test_invalid_sizes(self): + assert_raises(ValueError, fft, []) + assert_raises(ValueError, fft, [[1,1],[2,2]], -5) + + +class TestDoubleFFT(_TestFFTBase): + def setup_method(self): + self.cdt = np.complex128 + self.rdt = np.float64 + + +class TestSingleFFT(_TestFFTBase): + def setup_method(self): + self.cdt = np.complex64 + self.rdt = np.float32 + + reason = ("single-precision FFT implementation is partially disabled, " + "until accuracy issues with large prime powers are resolved") + + @pytest.mark.xfail(run=False, reason=reason) + def test_notice(self): + pass + + +class TestFloat16FFT: + + def test_1_argument_real(self): + x1 = np.array([1, 2, 3, 4], dtype=np.float16) + y = fft(x1, n=4) + assert_equal(y.dtype, np.complex64) + assert_equal(y.shape, (4, )) + assert_array_almost_equal(y, direct_dft(x1.astype(np.float32))) + + def test_n_argument_real(self): + x1 = np.array([1, 2, 3, 4], dtype=np.float16) + x2 = np.array([1, 2, 3, 4], dtype=np.float16) + y = fft([x1, x2], n=4) + assert_equal(y.dtype, np.complex64) + assert_equal(y.shape, (2, 4)) + assert_array_almost_equal(y[0], direct_dft(x1.astype(np.float32))) + assert_array_almost_equal(y[1], direct_dft(x2.astype(np.float32))) + + +class _TestIFFTBase: + def setup_method(self): + np.random.seed(1234) + + def test_definition(self): + x = np.array([1,2,3,4+1j,1,2,3,4+2j], self.cdt) + y = ifft(x) + y1 = direct_idft(x) + assert_equal(y.dtype, self.cdt) + assert_array_almost_equal(y,y1) + + x = np.array([1,2,3,4+0j,5], self.cdt) + assert_array_almost_equal(ifft(x),direct_idft(x)) + + def test_definition_real(self): + x = np.array([1,2,3,4,1,2,3,4], self.rdt) + y = ifft(x) + assert_equal(y.dtype, self.cdt) + y1 = direct_idft(x) + assert_array_almost_equal(y,y1) + + x = np.array([1,2,3,4,5], dtype=self.rdt) + assert_equal(y.dtype, self.cdt) + assert_array_almost_equal(ifft(x),direct_idft(x)) + + def test_random_complex(self): + for size in [1,51,111,100,200,64,128,256,1024]: + x = random([size]).astype(self.cdt) + x = random([size]).astype(self.cdt) + 1j*x + y1 = ifft(fft(x)) + y2 = fft(ifft(x)) + assert_equal(y1.dtype, self.cdt) + assert_equal(y2.dtype, self.cdt) + assert_array_almost_equal(y1, x) + assert_array_almost_equal(y2, x) + + def test_random_real(self): + for size in [1,51,111,100,200,64,128,256,1024]: + x = random([size]).astype(self.rdt) + y1 = ifft(fft(x)) + y2 = fft(ifft(x)) + assert_equal(y1.dtype, self.cdt) + assert_equal(y2.dtype, self.cdt) + assert_array_almost_equal(y1, x) + assert_array_almost_equal(y2, x) + + def test_size_accuracy(self): + # Sanity check for the accuracy for prime and non-prime sized inputs + if self.rdt == np.float32: + rtol = 1e-5 + elif self.rdt == np.float64: + rtol = 1e-10 + + for size in LARGE_COMPOSITE_SIZES + LARGE_PRIME_SIZES: + np.random.seed(1234) + x = np.random.rand(size).astype(self.rdt) + y = ifft(fft(x)) + _assert_close_in_norm(x, y, rtol, size, self.rdt) + y = fft(ifft(x)) + _assert_close_in_norm(x, y, rtol, size, self.rdt) + + x = (x + 1j*np.random.rand(size)).astype(self.cdt) + y = ifft(fft(x)) + _assert_close_in_norm(x, y, rtol, size, self.rdt) + y = fft(ifft(x)) + _assert_close_in_norm(x, y, rtol, size, self.rdt) + + def test_invalid_sizes(self): + assert_raises(ValueError, ifft, []) + assert_raises(ValueError, ifft, [[1,1],[2,2]], -5) + + +class TestDoubleIFFT(_TestIFFTBase): + def setup_method(self): + self.cdt = np.complex128 + self.rdt = np.float64 + + +class TestSingleIFFT(_TestIFFTBase): + def setup_method(self): + self.cdt = np.complex64 + self.rdt = np.float32 + + +class _TestRFFTBase: + def setup_method(self): + np.random.seed(1234) + + def test_definition(self): + for t in [[1, 2, 3, 4, 1, 2, 3, 4], [1, 2, 3, 4, 1, 2, 3, 4, 5]]: + x = np.array(t, dtype=self.rdt) + y = rfft(x) + y1 = direct_rdft(x) + assert_array_almost_equal(y,y1) + assert_equal(y.dtype, self.rdt) + + def test_invalid_sizes(self): + assert_raises(ValueError, rfft, []) + assert_raises(ValueError, rfft, [[1,1],[2,2]], -5) + + # See gh-5790 + class MockSeries: + def __init__(self, data): + self.data = np.asarray(data) + + def __getattr__(self, item): + try: + return getattr(self.data, item) + except AttributeError as e: + raise AttributeError("'MockSeries' object " + f"has no attribute '{item}'") from e + + def test_non_ndarray_with_dtype(self): + x = np.array([1., 2., 3., 4., 5.]) + xs = _TestRFFTBase.MockSeries(x) + + expected = [1, 2, 3, 4, 5] + rfft(xs) + + # Data should not have been overwritten + assert_equal(x, expected) + assert_equal(xs.data, expected) + + def test_complex_input(self): + assert_raises(TypeError, rfft, np.arange(4, dtype=np.complex64)) + + +class TestRFFTDouble(_TestRFFTBase): + def setup_method(self): + self.cdt = np.complex128 + self.rdt = np.float64 + + +class TestRFFTSingle(_TestRFFTBase): + def setup_method(self): + self.cdt = np.complex64 + self.rdt = np.float32 + + +class _TestIRFFTBase: + def setup_method(self): + np.random.seed(1234) + + def test_definition(self): + x1 = [1,2,3,4,1,2,3,4] + x1_1 = [1,2+3j,4+1j,2+3j,4,2-3j,4-1j,2-3j] + x2 = [1,2,3,4,1,2,3,4,5] + x2_1 = [1,2+3j,4+1j,2+3j,4+5j,4-5j,2-3j,4-1j,2-3j] + + def _test(x, xr): + y = irfft(np.array(x, dtype=self.rdt)) + y1 = direct_irdft(x) + assert_equal(y.dtype, self.rdt) + assert_array_almost_equal(y,y1, decimal=self.ndec) + assert_array_almost_equal(y,ifft(xr), decimal=self.ndec) + + _test(x1, x1_1) + _test(x2, x2_1) + + def test_random_real(self): + for size in [1,51,111,100,200,64,128,256,1024]: + x = random([size]).astype(self.rdt) + y1 = irfft(rfft(x)) + y2 = rfft(irfft(x)) + assert_equal(y1.dtype, self.rdt) + assert_equal(y2.dtype, self.rdt) + assert_array_almost_equal(y1, x, decimal=self.ndec, + err_msg="size=%d" % size) + assert_array_almost_equal(y2, x, decimal=self.ndec, + err_msg="size=%d" % size) + + def test_size_accuracy(self): + # Sanity check for the accuracy for prime and non-prime sized inputs + if self.rdt == np.float32: + rtol = 1e-5 + elif self.rdt == np.float64: + rtol = 1e-10 + + for size in LARGE_COMPOSITE_SIZES + LARGE_PRIME_SIZES: + np.random.seed(1234) + x = np.random.rand(size).astype(self.rdt) + y = irfft(rfft(x)) + _assert_close_in_norm(x, y, rtol, size, self.rdt) + y = rfft(irfft(x)) + _assert_close_in_norm(x, y, rtol, size, self.rdt) + + def test_invalid_sizes(self): + assert_raises(ValueError, irfft, []) + assert_raises(ValueError, irfft, [[1,1],[2,2]], -5) + + def test_complex_input(self): + assert_raises(TypeError, irfft, np.arange(4, dtype=np.complex64)) + + +# self.ndec is bogus; we should have a assert_array_approx_equal for number of +# significant digits + +class TestIRFFTDouble(_TestIRFFTBase): + def setup_method(self): + self.cdt = np.complex128 + self.rdt = np.float64 + self.ndec = 14 + + +class TestIRFFTSingle(_TestIRFFTBase): + def setup_method(self): + self.cdt = np.complex64 + self.rdt = np.float32 + self.ndec = 5 + + +class Testfft2: + def setup_method(self): + np.random.seed(1234) + + def test_regression_244(self): + """FFT returns wrong result with axes parameter.""" + # fftn (and hence fft2) used to break when both axes and shape were + # used + x = numpy.ones((4, 4, 2)) + y = fft2(x, shape=(8, 8), axes=(-3, -2)) + y_r = numpy.fft.fftn(x, s=(8, 8), axes=(-3, -2)) + assert_array_almost_equal(y, y_r) + + def test_invalid_sizes(self): + assert_raises(ValueError, fft2, [[]]) + assert_raises(ValueError, fft2, [[1, 1], [2, 2]], (4, -3)) + + +class TestFftnSingle: + def setup_method(self): + np.random.seed(1234) + + def test_definition(self): + x = [[1, 2, 3], + [4, 5, 6], + [7, 8, 9]] + y = fftn(np.array(x, np.float32)) + assert_(y.dtype == np.complex64, + msg="double precision output with single precision") + + y_r = np.array(fftn(x), np.complex64) + assert_array_almost_equal_nulp(y, y_r) + + @pytest.mark.parametrize('size', SMALL_COMPOSITE_SIZES + SMALL_PRIME_SIZES) + def test_size_accuracy_small(self, size): + rng = np.random.default_rng(1234) + x = rng.random((size, size)) + 1j*rng.random((size, size)) + y1 = fftn(x.real.astype(np.float32)) + y2 = fftn(x.real.astype(np.float64)).astype(np.complex64) + + assert_equal(y1.dtype, np.complex64) + assert_array_almost_equal_nulp(y1, y2, 2000) + + @pytest.mark.parametrize('size', LARGE_COMPOSITE_SIZES + LARGE_PRIME_SIZES) + def test_size_accuracy_large(self, size): + rand = np.random.default_rng(1234) + x = rand.random((size, 3)) + 1j*rand.random((size, 3)) + y1 = fftn(x.real.astype(np.float32)) + y2 = fftn(x.real.astype(np.float64)).astype(np.complex64) + + assert_equal(y1.dtype, np.complex64) + assert_array_almost_equal_nulp(y1, y2, 2000) + + def test_definition_float16(self): + x = [[1, 2, 3], + [4, 5, 6], + [7, 8, 9]] + y = fftn(np.array(x, np.float16)) + assert_equal(y.dtype, np.complex64) + y_r = np.array(fftn(x), np.complex64) + assert_array_almost_equal_nulp(y, y_r) + + @pytest.mark.parametrize('size', SMALL_COMPOSITE_SIZES + SMALL_PRIME_SIZES) + def test_float16_input_small(self, size): + rng = np.random.default_rng(1234) + x = rng.random((size, size)) + 1j * rng.random((size, size)) + y1 = fftn(x.real.astype(np.float16)) + y2 = fftn(x.real.astype(np.float64)).astype(np.complex64) + + assert_equal(y1.dtype, np.complex64) + assert_array_almost_equal_nulp(y1, y2, 5e5) + + @pytest.mark.parametrize('size', LARGE_COMPOSITE_SIZES + LARGE_PRIME_SIZES) + def test_float16_input_large(self, size): + rng = np.random.default_rng(1234) + x = rng.random((size, 3)) + 1j*rng.random((size, 3)) + y1 = fftn(x.real.astype(np.float16)) + y2 = fftn(x.real.astype(np.float64)).astype(np.complex64) + + assert_equal(y1.dtype, np.complex64) + assert_array_almost_equal_nulp(y1, y2, 2e6) + + +class TestFftn: + def setup_method(self): + np.random.seed(1234) + + def test_definition(self): + x = [[1, 2, 3], + [4, 5, 6], + [7, 8, 9]] + y = fftn(x) + assert_array_almost_equal(y, direct_dftn(x)) + + x = random((20, 26)) + assert_array_almost_equal(fftn(x), direct_dftn(x)) + + x = random((5, 4, 3, 20)) + assert_array_almost_equal(fftn(x), direct_dftn(x)) + + def test_axes_argument(self): + # plane == ji_plane, x== kji_space + plane1 = [[1, 2, 3], + [4, 5, 6], + [7, 8, 9]] + plane2 = [[10, 11, 12], + [13, 14, 15], + [16, 17, 18]] + plane3 = [[19, 20, 21], + [22, 23, 24], + [25, 26, 27]] + ki_plane1 = [[1, 2, 3], + [10, 11, 12], + [19, 20, 21]] + ki_plane2 = [[4, 5, 6], + [13, 14, 15], + [22, 23, 24]] + ki_plane3 = [[7, 8, 9], + [16, 17, 18], + [25, 26, 27]] + jk_plane1 = [[1, 10, 19], + [4, 13, 22], + [7, 16, 25]] + jk_plane2 = [[2, 11, 20], + [5, 14, 23], + [8, 17, 26]] + jk_plane3 = [[3, 12, 21], + [6, 15, 24], + [9, 18, 27]] + kj_plane1 = [[1, 4, 7], + [10, 13, 16], [19, 22, 25]] + kj_plane2 = [[2, 5, 8], + [11, 14, 17], [20, 23, 26]] + kj_plane3 = [[3, 6, 9], + [12, 15, 18], [21, 24, 27]] + ij_plane1 = [[1, 4, 7], + [2, 5, 8], + [3, 6, 9]] + ij_plane2 = [[10, 13, 16], + [11, 14, 17], + [12, 15, 18]] + ij_plane3 = [[19, 22, 25], + [20, 23, 26], + [21, 24, 27]] + ik_plane1 = [[1, 10, 19], + [2, 11, 20], + [3, 12, 21]] + ik_plane2 = [[4, 13, 22], + [5, 14, 23], + [6, 15, 24]] + ik_plane3 = [[7, 16, 25], + [8, 17, 26], + [9, 18, 27]] + ijk_space = [jk_plane1, jk_plane2, jk_plane3] + ikj_space = [kj_plane1, kj_plane2, kj_plane3] + jik_space = [ik_plane1, ik_plane2, ik_plane3] + jki_space = [ki_plane1, ki_plane2, ki_plane3] + kij_space = [ij_plane1, ij_plane2, ij_plane3] + x = array([plane1, plane2, plane3]) + + assert_array_almost_equal(fftn(x), + fftn(x, axes=(-3, -2, -1))) # kji_space + assert_array_almost_equal(fftn(x), fftn(x, axes=(0, 1, 2))) + assert_array_almost_equal(fftn(x, axes=(0, 2)), fftn(x, axes=(0, -1))) + y = fftn(x, axes=(2, 1, 0)) # ijk_space + assert_array_almost_equal(swapaxes(y, -1, -3), fftn(ijk_space)) + y = fftn(x, axes=(2, 0, 1)) # ikj_space + assert_array_almost_equal(swapaxes(swapaxes(y, -1, -3), -1, -2), + fftn(ikj_space)) + y = fftn(x, axes=(1, 2, 0)) # jik_space + assert_array_almost_equal(swapaxes(swapaxes(y, -1, -3), -3, -2), + fftn(jik_space)) + y = fftn(x, axes=(1, 0, 2)) # jki_space + assert_array_almost_equal(swapaxes(y, -2, -3), fftn(jki_space)) + y = fftn(x, axes=(0, 2, 1)) # kij_space + assert_array_almost_equal(swapaxes(y, -2, -1), fftn(kij_space)) + + y = fftn(x, axes=(-2, -1)) # ji_plane + assert_array_almost_equal(fftn(plane1), y[0]) + assert_array_almost_equal(fftn(plane2), y[1]) + assert_array_almost_equal(fftn(plane3), y[2]) + + y = fftn(x, axes=(1, 2)) # ji_plane + assert_array_almost_equal(fftn(plane1), y[0]) + assert_array_almost_equal(fftn(plane2), y[1]) + assert_array_almost_equal(fftn(plane3), y[2]) + + y = fftn(x, axes=(-3, -2)) # kj_plane + assert_array_almost_equal(fftn(x[:, :, 0]), y[:, :, 0]) + assert_array_almost_equal(fftn(x[:, :, 1]), y[:, :, 1]) + assert_array_almost_equal(fftn(x[:, :, 2]), y[:, :, 2]) + + y = fftn(x, axes=(-3, -1)) # ki_plane + assert_array_almost_equal(fftn(x[:, 0, :]), y[:, 0, :]) + assert_array_almost_equal(fftn(x[:, 1, :]), y[:, 1, :]) + assert_array_almost_equal(fftn(x[:, 2, :]), y[:, 2, :]) + + y = fftn(x, axes=(-1, -2)) # ij_plane + assert_array_almost_equal(fftn(ij_plane1), swapaxes(y[0], -2, -1)) + assert_array_almost_equal(fftn(ij_plane2), swapaxes(y[1], -2, -1)) + assert_array_almost_equal(fftn(ij_plane3), swapaxes(y[2], -2, -1)) + + y = fftn(x, axes=(-1, -3)) # ik_plane + assert_array_almost_equal(fftn(ik_plane1), + swapaxes(y[:, 0, :], -1, -2)) + assert_array_almost_equal(fftn(ik_plane2), + swapaxes(y[:, 1, :], -1, -2)) + assert_array_almost_equal(fftn(ik_plane3), + swapaxes(y[:, 2, :], -1, -2)) + + y = fftn(x, axes=(-2, -3)) # jk_plane + assert_array_almost_equal(fftn(jk_plane1), + swapaxes(y[:, :, 0], -1, -2)) + assert_array_almost_equal(fftn(jk_plane2), + swapaxes(y[:, :, 1], -1, -2)) + assert_array_almost_equal(fftn(jk_plane3), + swapaxes(y[:, :, 2], -1, -2)) + + y = fftn(x, axes=(-1,)) # i_line + for i in range(3): + for j in range(3): + assert_array_almost_equal(fft(x[i, j, :]), y[i, j, :]) + y = fftn(x, axes=(-2,)) # j_line + for i in range(3): + for j in range(3): + assert_array_almost_equal(fft(x[i, :, j]), y[i, :, j]) + y = fftn(x, axes=(0,)) # k_line + for i in range(3): + for j in range(3): + assert_array_almost_equal(fft(x[:, i, j]), y[:, i, j]) + + y = fftn(x, axes=()) # point + assert_array_almost_equal(y, x) + + def test_shape_argument(self): + small_x = [[1, 2, 3], + [4, 5, 6]] + large_x1 = [[1, 2, 3, 0], + [4, 5, 6, 0], + [0, 0, 0, 0], + [0, 0, 0, 0]] + + y = fftn(small_x, shape=(4, 4)) + assert_array_almost_equal(y, fftn(large_x1)) + + y = fftn(small_x, shape=(3, 4)) + assert_array_almost_equal(y, fftn(large_x1[:-1])) + + def test_shape_axes_argument(self): + small_x = [[1, 2, 3], + [4, 5, 6], + [7, 8, 9]] + large_x1 = array([[1, 2, 3, 0], + [4, 5, 6, 0], + [7, 8, 9, 0], + [0, 0, 0, 0]]) + y = fftn(small_x, shape=(4, 4), axes=(-2, -1)) + assert_array_almost_equal(y, fftn(large_x1)) + y = fftn(small_x, shape=(4, 4), axes=(-1, -2)) + + assert_array_almost_equal(y, swapaxes( + fftn(swapaxes(large_x1, -1, -2)), -1, -2)) + + def test_shape_axes_argument2(self): + # Change shape of the last axis + x = numpy.random.random((10, 5, 3, 7)) + y = fftn(x, axes=(-1,), shape=(8,)) + assert_array_almost_equal(y, fft(x, axis=-1, n=8)) + + # Change shape of an arbitrary axis which is not the last one + x = numpy.random.random((10, 5, 3, 7)) + y = fftn(x, axes=(-2,), shape=(8,)) + assert_array_almost_equal(y, fft(x, axis=-2, n=8)) + + # Change shape of axes: cf #244, where shape and axes were mixed up + x = numpy.random.random((4, 4, 2)) + y = fftn(x, axes=(-3, -2), shape=(8, 8)) + assert_array_almost_equal(y, + numpy.fft.fftn(x, axes=(-3, -2), s=(8, 8))) + + def test_shape_argument_more(self): + x = zeros((4, 4, 2)) + with assert_raises(ValueError, + match="when given, axes and shape arguments" + " have to be of the same length"): + fftn(x, shape=(8, 8, 2, 1)) + + def test_invalid_sizes(self): + with assert_raises(ValueError, + match="invalid number of data points" + r" \(\[1, 0\]\) specified"): + fftn([[]]) + + with assert_raises(ValueError, + match="invalid number of data points" + r" \(\[4, -3\]\) specified"): + fftn([[1, 1], [2, 2]], (4, -3)) + + +class TestIfftn: + dtype = None + cdtype = None + + def setup_method(self): + np.random.seed(1234) + + @pytest.mark.parametrize('dtype,cdtype,maxnlp', + [(np.float64, np.complex128, 2000), + (np.float32, np.complex64, 3500)]) + def test_definition(self, dtype, cdtype, maxnlp): + rng = np.random.default_rng(1234) + x = np.array([[1, 2, 3], + [4, 5, 6], + [7, 8, 9]], dtype=dtype) + y = ifftn(x) + assert_equal(y.dtype, cdtype) + assert_array_almost_equal_nulp(y, direct_idftn(x), maxnlp) + + x = rng.random((20, 26)) + assert_array_almost_equal_nulp(ifftn(x), direct_idftn(x), maxnlp) + + x = rng.random((5, 4, 3, 20)) + assert_array_almost_equal_nulp(ifftn(x), direct_idftn(x), maxnlp) + + @pytest.mark.parametrize('maxnlp', [2000, 3500]) + @pytest.mark.parametrize('size', [1, 2, 51, 32, 64, 92]) + def test_random_complex(self, maxnlp, size): + rng = np.random.default_rng(1234) + x = rng.random([size, size]) + 1j * rng.random([size, size]) + assert_array_almost_equal_nulp(ifftn(fftn(x)), x, maxnlp) + assert_array_almost_equal_nulp(fftn(ifftn(x)), x, maxnlp) + + def test_invalid_sizes(self): + with assert_raises(ValueError, + match="invalid number of data points" + r" \(\[1, 0\]\) specified"): + ifftn([[]]) + + with assert_raises(ValueError, + match="invalid number of data points" + r" \(\[4, -3\]\) specified"): + ifftn([[1, 1], [2, 2]], (4, -3)) + + +class FakeArray: + def __init__(self, data): + self._data = data + self.__array_interface__ = data.__array_interface__ + + +class FakeArray2: + def __init__(self, data): + self._data = data + + def __array__(self, dtype=None, copy=None): + return self._data + + +class TestOverwrite: + """Check input overwrite behavior of the FFT functions.""" + + real_dtypes = (np.float32, np.float64) + dtypes = real_dtypes + (np.complex64, np.complex128) + fftsizes = [8, 16, 32] + + def _check(self, x, routine, fftsize, axis, overwrite_x): + x2 = x.copy() + for fake in [lambda x: x, FakeArray, FakeArray2]: + routine(fake(x2), fftsize, axis, overwrite_x=overwrite_x) + + sig = (f"{routine.__name__}({x.dtype}{x.shape!r}, {fftsize!r}, " + f"axis={axis!r}, overwrite_x={overwrite_x!r})") + if not overwrite_x: + assert_equal(x2, x, err_msg=f"spurious overwrite in {sig}") + + def _check_1d(self, routine, dtype, shape, axis, overwritable_dtypes, + fftsize, overwrite_x): + np.random.seed(1234) + if np.issubdtype(dtype, np.complexfloating): + data = np.random.randn(*shape) + 1j*np.random.randn(*shape) + else: + data = np.random.randn(*shape) + data = data.astype(dtype) + + self._check(data, routine, fftsize, axis, + overwrite_x=overwrite_x) + + @pytest.mark.parametrize('dtype', dtypes) + @pytest.mark.parametrize('fftsize', fftsizes) + @pytest.mark.parametrize('overwrite_x', [True, False]) + @pytest.mark.parametrize('shape,axes', [((16,), -1), + ((16, 2), 0), + ((2, 16), 1)]) + def test_fft_ifft(self, dtype, fftsize, overwrite_x, shape, axes): + overwritable = (np.complex128, np.complex64) + self._check_1d(fft, dtype, shape, axes, overwritable, + fftsize, overwrite_x) + self._check_1d(ifft, dtype, shape, axes, overwritable, + fftsize, overwrite_x) + + @pytest.mark.parametrize('dtype', real_dtypes) + @pytest.mark.parametrize('fftsize', fftsizes) + @pytest.mark.parametrize('overwrite_x', [True, False]) + @pytest.mark.parametrize('shape,axes', [((16,), -1), + ((16, 2), 0), + ((2, 16), 1)]) + def test_rfft_irfft(self, dtype, fftsize, overwrite_x, shape, axes): + overwritable = self.real_dtypes + self._check_1d(irfft, dtype, shape, axes, overwritable, + fftsize, overwrite_x) + self._check_1d(rfft, dtype, shape, axes, overwritable, + fftsize, overwrite_x) + + def _check_nd_one(self, routine, dtype, shape, axes, overwritable_dtypes, + overwrite_x): + np.random.seed(1234) + if np.issubdtype(dtype, np.complexfloating): + data = np.random.randn(*shape) + 1j*np.random.randn(*shape) + else: + data = np.random.randn(*shape) + data = data.astype(dtype) + + def fftshape_iter(shp): + if len(shp) <= 0: + yield () + else: + for j in (shp[0]//2, shp[0], shp[0]*2): + for rest in fftshape_iter(shp[1:]): + yield (j,) + rest + + if axes is None: + part_shape = shape + else: + part_shape = tuple(np.take(shape, axes)) + + for fftshape in fftshape_iter(part_shape): + self._check(data, routine, fftshape, axes, + overwrite_x=overwrite_x) + if data.ndim > 1: + self._check(data.T, routine, fftshape, axes, + overwrite_x=overwrite_x) + + @pytest.mark.parametrize('dtype', dtypes) + @pytest.mark.parametrize('overwrite_x', [True, False]) + @pytest.mark.parametrize('shape,axes', [((16,), None), + ((16,), (0,)), + ((16, 2), (0,)), + ((2, 16), (1,)), + ((8, 16), None), + ((8, 16), (0, 1)), + ((8, 16, 2), (0, 1)), + ((8, 16, 2), (1, 2)), + ((8, 16, 2), (0,)), + ((8, 16, 2), (1,)), + ((8, 16, 2), (2,)), + ((8, 16, 2), None), + ((8, 16, 2), (0, 1, 2))]) + def test_fftn_ifftn(self, dtype, overwrite_x, shape, axes): + overwritable = (np.complex128, np.complex64) + self._check_nd_one(fftn, dtype, shape, axes, overwritable, + overwrite_x) + self._check_nd_one(ifftn, dtype, shape, axes, overwritable, + overwrite_x) + + +@pytest.mark.parametrize('func', [fftn, ifftn, fft2]) +def test_shape_axes_ndarray(func): + # Test fftn and ifftn work with NumPy arrays for shape and axes arguments + # Regression test for gh-13342 + a = np.random.rand(10, 10) + + expect = func(a, shape=(5, 5)) + actual = func(a, shape=np.array([5, 5])) + assert_equal(expect, actual) + + expect = func(a, axes=(-1,)) + actual = func(a, axes=np.array([-1,])) + assert_equal(expect, actual) + + expect = func(a, shape=(4, 7), axes=(1, 0)) + actual = func(a, shape=np.array([4, 7]), axes=np.array([1, 0])) + assert_equal(expect, actual) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/tests/test_helper.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/tests/test_helper.py new file mode 100644 index 0000000000000000000000000000000000000000..5e7be04f3c0291502b50b101db82d299aadc7772 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/tests/test_helper.py @@ -0,0 +1,54 @@ +# Created by Pearu Peterson, September 2002 + +__usage__ = """ +Build fftpack: + python setup_fftpack.py build +Run tests if scipy is installed: + python -c 'import scipy;scipy.fftpack.test()' +Run tests if fftpack is not installed: + python tests/test_helper.py [] +""" + +from numpy.testing import assert_array_almost_equal +from scipy.fftpack import fftshift, ifftshift, fftfreq, rfftfreq + +from numpy import pi, random + +class TestFFTShift: + + def test_definition(self): + x = [0,1,2,3,4,-4,-3,-2,-1] + y = [-4,-3,-2,-1,0,1,2,3,4] + assert_array_almost_equal(fftshift(x),y) + assert_array_almost_equal(ifftshift(y),x) + x = [0,1,2,3,4,-5,-4,-3,-2,-1] + y = [-5,-4,-3,-2,-1,0,1,2,3,4] + assert_array_almost_equal(fftshift(x),y) + assert_array_almost_equal(ifftshift(y),x) + + def test_inverse(self): + for n in [1,4,9,100,211]: + x = random.random((n,)) + assert_array_almost_equal(ifftshift(fftshift(x)),x) + + +class TestFFTFreq: + + def test_definition(self): + x = [0,1,2,3,4,-4,-3,-2,-1] + assert_array_almost_equal(9*fftfreq(9),x) + assert_array_almost_equal(9*pi*fftfreq(9,pi),x) + x = [0,1,2,3,4,-5,-4,-3,-2,-1] + assert_array_almost_equal(10*fftfreq(10),x) + assert_array_almost_equal(10*pi*fftfreq(10,pi),x) + + +class TestRFFTFreq: + + def test_definition(self): + x = [0,1,1,2,2,3,3,4,4] + assert_array_almost_equal(9*rfftfreq(9),x) + assert_array_almost_equal(9*pi*rfftfreq(9,pi),x) + x = [0,1,1,2,2,3,3,4,4,5] + assert_array_almost_equal(10*rfftfreq(10),x) + assert_array_almost_equal(10*pi*rfftfreq(10,pi),x) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/tests/test_import.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/tests/test_import.py new file mode 100644 index 0000000000000000000000000000000000000000..e71aec9bd07cd4ef486b7e74b9589b6f1634d629 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/tests/test_import.py @@ -0,0 +1,33 @@ +"""Test possibility of patching fftpack with pyfftw. + +No module source outside of scipy.fftpack should contain an import of +the form `from scipy.fftpack import ...`, so that a simple replacement +of scipy.fftpack by the corresponding fftw interface completely swaps +the two FFT implementations. + +Because this simply inspects source files, we only need to run the test +on one version of Python. +""" + + +from pathlib import Path +import re +import tokenize +import pytest +from numpy.testing import assert_ +import scipy + +class TestFFTPackImport: + @pytest.mark.slow + def test_fftpack_import(self): + base = Path(scipy.__file__).parent + regexp = r"\s*from.+\.fftpack import .*\n" + for path in base.rglob("*.py"): + if base / "fftpack" in path.parents: + continue + # use tokenize to auto-detect encoding on systems where no + # default encoding is defined (e.g., LANG='C') + with tokenize.open(str(path)) as file: + assert_(all(not re.fullmatch(regexp, line) + for line in file), + f"{path} contains an import from fftpack") diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/tests/test_pseudo_diffs.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/tests/test_pseudo_diffs.py new file mode 100644 index 0000000000000000000000000000000000000000..0a92729626a280c12aa3197e99b0d58ce00812d9 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/tests/test_pseudo_diffs.py @@ -0,0 +1,388 @@ +# Created by Pearu Peterson, September 2002 + +__usage__ = """ +Build fftpack: + python setup_fftpack.py build +Run tests if scipy is installed: + python -c 'import scipy;scipy.fftpack.test()' +Run tests if fftpack is not installed: + python tests/test_pseudo_diffs.py [] +""" + +from numpy.testing import (assert_equal, assert_almost_equal, + assert_array_almost_equal) +from scipy.fftpack import (diff, fft, ifft, tilbert, itilbert, hilbert, + ihilbert, shift, fftfreq, cs_diff, sc_diff, + ss_diff, cc_diff) + +import numpy as np +from numpy import arange, sin, cos, pi, exp, tanh, sum, sign +from numpy.random import random + + +def direct_diff(x,k=1,period=None): + fx = fft(x) + n = len(fx) + if period is None: + period = 2*pi + w = fftfreq(n)*2j*pi/period*n + if k < 0: + w = 1 / w**k + w[0] = 0.0 + else: + w = w**k + if n > 2000: + w[250:n-250] = 0.0 + return ifft(w*fx).real + + +def direct_tilbert(x,h=1,period=None): + fx = fft(x) + n = len(fx) + if period is None: + period = 2*pi + w = fftfreq(n)*h*2*pi/period*n + w[0] = 1 + w = 1j/tanh(w) + w[0] = 0j + return ifft(w*fx) + + +def direct_itilbert(x,h=1,period=None): + fx = fft(x) + n = len(fx) + if period is None: + period = 2*pi + w = fftfreq(n)*h*2*pi/period*n + w = -1j*tanh(w) + return ifft(w*fx) + + +def direct_hilbert(x): + fx = fft(x) + n = len(fx) + w = fftfreq(n)*n + w = 1j*sign(w) + return ifft(w*fx) + + +def direct_ihilbert(x): + return -direct_hilbert(x) + + +def direct_shift(x,a,period=None): + n = len(x) + if period is None: + k = fftfreq(n)*1j*n + else: + k = fftfreq(n)*2j*pi/period*n + return ifft(fft(x)*exp(k*a)).real + + +class TestDiff: + + def test_definition(self): + for n in [16,17,64,127,32]: + x = arange(n)*2*pi/n + assert_array_almost_equal(diff(sin(x)),direct_diff(sin(x))) + assert_array_almost_equal(diff(sin(x),2),direct_diff(sin(x),2)) + assert_array_almost_equal(diff(sin(x),3),direct_diff(sin(x),3)) + assert_array_almost_equal(diff(sin(x),4),direct_diff(sin(x),4)) + assert_array_almost_equal(diff(sin(x),5),direct_diff(sin(x),5)) + assert_array_almost_equal(diff(sin(2*x),3),direct_diff(sin(2*x),3)) + assert_array_almost_equal(diff(sin(2*x),4),direct_diff(sin(2*x),4)) + assert_array_almost_equal(diff(cos(x)),direct_diff(cos(x))) + assert_array_almost_equal(diff(cos(x),2),direct_diff(cos(x),2)) + assert_array_almost_equal(diff(cos(x),3),direct_diff(cos(x),3)) + assert_array_almost_equal(diff(cos(x),4),direct_diff(cos(x),4)) + assert_array_almost_equal(diff(cos(2*x)),direct_diff(cos(2*x))) + assert_array_almost_equal(diff(sin(x*n/8)),direct_diff(sin(x*n/8))) + assert_array_almost_equal(diff(cos(x*n/8)),direct_diff(cos(x*n/8))) + for k in range(5): + assert_array_almost_equal(diff(sin(4*x),k),direct_diff(sin(4*x),k)) + assert_array_almost_equal(diff(cos(4*x),k),direct_diff(cos(4*x),k)) + + def test_period(self): + for n in [17,64]: + x = arange(n)/float(n) + assert_array_almost_equal(diff(sin(2*pi*x),period=1), + 2*pi*cos(2*pi*x)) + assert_array_almost_equal(diff(sin(2*pi*x),3,period=1), + -(2*pi)**3*cos(2*pi*x)) + + def test_sin(self): + for n in [32,64,77]: + x = arange(n)*2*pi/n + assert_array_almost_equal(diff(sin(x)),cos(x)) + assert_array_almost_equal(diff(cos(x)),-sin(x)) + assert_array_almost_equal(diff(sin(x),2),-sin(x)) + assert_array_almost_equal(diff(sin(x),4),sin(x)) + assert_array_almost_equal(diff(sin(4*x)),4*cos(4*x)) + assert_array_almost_equal(diff(sin(sin(x))),cos(x)*cos(sin(x))) + + def test_expr(self): + for n in [64,77,100,128,256,512,1024,2048,4096,8192][:5]: + x = arange(n)*2*pi/n + f = sin(x)*cos(4*x)+exp(sin(3*x)) + df = cos(x)*cos(4*x)-4*sin(x)*sin(4*x)+3*cos(3*x)*exp(sin(3*x)) + ddf = -17*sin(x)*cos(4*x)-8*cos(x)*sin(4*x)\ + - 9*sin(3*x)*exp(sin(3*x))+9*cos(3*x)**2*exp(sin(3*x)) + d1 = diff(f) + assert_array_almost_equal(d1,df) + assert_array_almost_equal(diff(df),ddf) + assert_array_almost_equal(diff(f,2),ddf) + assert_array_almost_equal(diff(ddf,-1),df) + + def test_expr_large(self): + for n in [2048,4096]: + x = arange(n)*2*pi/n + f = sin(x)*cos(4*x)+exp(sin(3*x)) + df = cos(x)*cos(4*x)-4*sin(x)*sin(4*x)+3*cos(3*x)*exp(sin(3*x)) + ddf = -17*sin(x)*cos(4*x)-8*cos(x)*sin(4*x)\ + - 9*sin(3*x)*exp(sin(3*x))+9*cos(3*x)**2*exp(sin(3*x)) + assert_array_almost_equal(diff(f),df) + assert_array_almost_equal(diff(df),ddf) + assert_array_almost_equal(diff(ddf,-1),df) + assert_array_almost_equal(diff(f,2),ddf) + + def test_int(self): + n = 64 + x = arange(n)*2*pi/n + assert_array_almost_equal(diff(sin(x),-1),-cos(x)) + assert_array_almost_equal(diff(sin(x),-2),-sin(x)) + assert_array_almost_equal(diff(sin(x),-4),sin(x)) + assert_array_almost_equal(diff(2*cos(2*x),-1),sin(2*x)) + + def test_random_even(self): + rng = np.random.default_rng(1234) + for k in [0,2,4,6]: + for n in [60,32,64,56,55]: + f = rng.random((n,)) + af = sum(f,axis=0)/n + f = f-af + # zeroing Nyquist mode: + f = diff(diff(f,1),-1) + assert_almost_equal(sum(f,axis=0),0.0) + assert_array_almost_equal(diff(diff(f,k),-k),f) + assert_array_almost_equal(diff(diff(f,-k),k),f) + + def test_random_odd(self): + rng = np.random.default_rng(1234) + for k in [0,1,2,3,4,5,6]: + for n in [33,65,55]: + f = rng.random((n,)) + af = sum(f,axis=0)/n + f = f-af + assert_almost_equal(sum(f,axis=0),0.0) + assert_array_almost_equal(diff(diff(f,k),-k),f) + assert_array_almost_equal(diff(diff(f,-k),k),f) + + def test_zero_nyquist(self): + rng = np.random.default_rng(1234) + for k in [0,1,2,3,4,5,6]: + for n in [32,33,64,56,55]: + f = rng.random((n,)) + af = sum(f,axis=0)/n + f = f-af + # zeroing Nyquist mode: + f = diff(diff(f,1),-1) + assert_almost_equal(sum(f,axis=0),0.0) + assert_array_almost_equal(diff(diff(f,k),-k),f) + assert_array_almost_equal(diff(diff(f,-k),k),f) + + +class TestTilbert: + + def test_definition(self): + for h in [0.1,0.5,1,5.5,10]: + for n in [16,17,64,127]: + x = arange(n)*2*pi/n + y = tilbert(sin(x),h) + y1 = direct_tilbert(sin(x),h) + assert_array_almost_equal(y,y1) + assert_array_almost_equal(tilbert(sin(x),h), + direct_tilbert(sin(x),h)) + assert_array_almost_equal(tilbert(sin(2*x),h), + direct_tilbert(sin(2*x),h)) + + def test_random_even(self): + for h in [0.1,0.5,1,5.5,10]: + for n in [32,64,56]: + f = random((n,)) + af = sum(f,axis=0)/n + f = f-af + assert_almost_equal(sum(f,axis=0),0.0) + assert_array_almost_equal(direct_tilbert(direct_itilbert(f,h),h),f) + + def test_random_odd(self): + rng = np.random.default_rng(1234) + for h in [0.1,0.5,1,5.5,10]: + for n in [33,65,55]: + f = rng.random((n,)) + af = sum(f,axis=0)/n + f = f-af + assert_almost_equal(sum(f,axis=0),0.0) + assert_array_almost_equal(itilbert(tilbert(f,h),h),f) + assert_array_almost_equal(tilbert(itilbert(f,h),h),f) + + +class TestITilbert: + + def test_definition(self): + for h in [0.1,0.5,1,5.5,10]: + for n in [16,17,64,127]: + x = arange(n)*2*pi/n + y = itilbert(sin(x),h) + y1 = direct_itilbert(sin(x),h) + assert_array_almost_equal(y,y1) + assert_array_almost_equal(itilbert(sin(x),h), + direct_itilbert(sin(x),h)) + assert_array_almost_equal(itilbert(sin(2*x),h), + direct_itilbert(sin(2*x),h)) + + +class TestHilbert: + + def test_definition(self): + for n in [16,17,64,127]: + x = arange(n)*2*pi/n + y = hilbert(sin(x)) + y1 = direct_hilbert(sin(x)) + assert_array_almost_equal(y,y1) + assert_array_almost_equal(hilbert(sin(2*x)), + direct_hilbert(sin(2*x))) + + def test_tilbert_relation(self): + for n in [16,17,64,127]: + x = arange(n)*2*pi/n + f = sin(x)+cos(2*x)*sin(x) + y = hilbert(f) + y1 = direct_hilbert(f) + assert_array_almost_equal(y,y1) + y2 = tilbert(f,h=10) + assert_array_almost_equal(y,y2) + + def test_random_odd(self): + rng = np.random.default_rng(1234) + for n in [33,65,55]: + f = rng.random((n,)) + af = sum(f,axis=0)/n + f = f-af + assert_almost_equal(sum(f,axis=0),0.0) + assert_array_almost_equal(ihilbert(hilbert(f)),f) + assert_array_almost_equal(hilbert(ihilbert(f)),f) + + def test_random_even(self): + rng = np.random.default_rng(1234) + for n in [32,64,56]: + f = rng.random((n,)) + af = sum(f,axis=0)/n + f = f-af + # zeroing Nyquist mode: + f = diff(diff(f,1),-1) + assert_almost_equal(sum(f,axis=0),0.0) + assert_array_almost_equal(direct_hilbert(direct_ihilbert(f)),f) + assert_array_almost_equal(hilbert(ihilbert(f)),f) + + +class TestIHilbert: + + def test_definition(self): + for n in [16,17,64,127]: + x = arange(n)*2*pi/n + y = ihilbert(sin(x)) + y1 = direct_ihilbert(sin(x)) + assert_array_almost_equal(y,y1) + assert_array_almost_equal(ihilbert(sin(2*x)), + direct_ihilbert(sin(2*x))) + + def test_itilbert_relation(self): + for n in [16,17,64,127]: + x = arange(n)*2*pi/n + f = sin(x)+cos(2*x)*sin(x) + y = ihilbert(f) + y1 = direct_ihilbert(f) + assert_array_almost_equal(y,y1) + y2 = itilbert(f,h=10) + assert_array_almost_equal(y,y2) + + +class TestShift: + + def test_definition(self): + for n in [18,17,64,127,32,2048,256]: + x = arange(n)*2*pi/n + for a in [0.1,3]: + assert_array_almost_equal(shift(sin(x),a),direct_shift(sin(x),a)) + assert_array_almost_equal(shift(sin(x),a),sin(x+a)) + assert_array_almost_equal(shift(cos(x),a),cos(x+a)) + assert_array_almost_equal(shift(cos(2*x)+sin(x),a), + cos(2*(x+a))+sin(x+a)) + assert_array_almost_equal(shift(exp(sin(x)),a),exp(sin(x+a))) + assert_array_almost_equal(shift(sin(x),2*pi),sin(x)) + assert_array_almost_equal(shift(sin(x),pi),-sin(x)) + assert_array_almost_equal(shift(sin(x),pi/2),cos(x)) + + +class TestOverwrite: + """Check input overwrite behavior """ + + real_dtypes = (np.float32, np.float64) + dtypes = real_dtypes + (np.complex64, np.complex128) + + def _check(self, x, routine, *args, **kwargs): + x2 = x.copy() + routine(x2, *args, **kwargs) + sig = routine.__name__ + if args: + sig += repr(args) + if kwargs: + sig += repr(kwargs) + assert_equal(x2, x, err_msg=f"spurious overwrite in {sig}") + + def _check_1d(self, routine, dtype, shape, *args, **kwargs): + # rng = np.random.default_rng(1234) + rng = np.random.RandomState(1234) + # np.random.seed(1234) + if np.issubdtype(dtype, np.complexfloating): + data = rng.randn(*shape) + 1j*rng.randn(*shape) + else: + data = rng.randn(*shape) + data = data.astype(dtype) + self._check(data, routine, *args, **kwargs) + + def test_diff(self): + for dtype in self.dtypes: + self._check_1d(diff, dtype, (16,)) + + def test_tilbert(self): + for dtype in self.dtypes: + self._check_1d(tilbert, dtype, (16,), 1.6) + + def test_itilbert(self): + for dtype in self.dtypes: + self._check_1d(itilbert, dtype, (16,), 1.6) + + def test_hilbert(self): + for dtype in self.dtypes: + self._check_1d(hilbert, dtype, (16,)) + + def test_cs_diff(self): + for dtype in self.dtypes: + self._check_1d(cs_diff, dtype, (16,), 1.0, 4.0) + + def test_sc_diff(self): + for dtype in self.dtypes: + self._check_1d(sc_diff, dtype, (16,), 1.0, 4.0) + + def test_ss_diff(self): + for dtype in self.dtypes: + self._check_1d(ss_diff, dtype, (16,), 1.0, 4.0) + + def test_cc_diff(self): + for dtype in self.dtypes: + self._check_1d(cc_diff, dtype, (16,), 1.0, 4.0) + + def test_shift(self): + for dtype in self.dtypes: + self._check_1d(shift, dtype, (16,), 1.0) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/tests/test_real_transforms.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/tests/test_real_transforms.py new file mode 100644 index 0000000000000000000000000000000000000000..876af8f18a312f88c508c6b2ae96f1e5c1dad96d --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/fftpack/tests/test_real_transforms.py @@ -0,0 +1,837 @@ +from os.path import join, dirname +import threading + +import numpy as np +from numpy.testing import assert_array_almost_equal, assert_equal +import pytest +from pytest import raises as assert_raises + +from scipy.fftpack._realtransforms import ( + dct, idct, dst, idst, dctn, idctn, dstn, idstn) + +# Matlab reference data +MDATA = np.load(join(dirname(__file__), 'test.npz')) +X = [MDATA['x%d' % i] for i in range(8)] +Y = [MDATA['y%d' % i] for i in range(8)] + +# FFTW reference data: the data are organized as follows: +# * SIZES is an array containing all available sizes +# * for every type (1, 2, 3, 4) and every size, the array dct_type_size +# contains the output of the DCT applied to the input np.linspace(0, size-1, +# size) +FFTWDATA_DOUBLE = np.load(join(dirname(__file__), 'fftw_double_ref.npz')) +FFTWDATA_SINGLE = np.load(join(dirname(__file__), 'fftw_single_ref.npz')) +FFTWDATA_SIZES = FFTWDATA_DOUBLE['sizes'] + + +def fftw_dct_ref(type, size, dt): + x = np.linspace(0, size-1, size).astype(dt) + dt = np.result_type(np.float32, dt) + if dt == np.float64: + data = FFTWDATA_DOUBLE + elif dt == np.float32: + data = FFTWDATA_SINGLE + else: + raise ValueError() + y = (data['dct_%d_%d' % (type, size)]).astype(dt) + return x, y, dt + + +def fftw_dst_ref(type, size, dt): + x = np.linspace(0, size-1, size).astype(dt) + dt = np.result_type(np.float32, dt) + if dt == np.float64: + data = FFTWDATA_DOUBLE + elif dt == np.float32: + data = FFTWDATA_SINGLE + else: + raise ValueError() + y = (data['dst_%d_%d' % (type, size)]).astype(dt) + return x, y, dt + + +def dct_2d_ref(x, **kwargs): + """Calculate reference values for testing dct2.""" + x = np.array(x, copy=True) + for row in range(x.shape[0]): + x[row, :] = dct(x[row, :], **kwargs) + for col in range(x.shape[1]): + x[:, col] = dct(x[:, col], **kwargs) + return x + + +def idct_2d_ref(x, **kwargs): + """Calculate reference values for testing idct2.""" + x = np.array(x, copy=True) + for row in range(x.shape[0]): + x[row, :] = idct(x[row, :], **kwargs) + for col in range(x.shape[1]): + x[:, col] = idct(x[:, col], **kwargs) + return x + + +def dst_2d_ref(x, **kwargs): + """Calculate reference values for testing dst2.""" + x = np.array(x, copy=True) + for row in range(x.shape[0]): + x[row, :] = dst(x[row, :], **kwargs) + for col in range(x.shape[1]): + x[:, col] = dst(x[:, col], **kwargs) + return x + + +def idst_2d_ref(x, **kwargs): + """Calculate reference values for testing idst2.""" + x = np.array(x, copy=True) + for row in range(x.shape[0]): + x[row, :] = idst(x[row, :], **kwargs) + for col in range(x.shape[1]): + x[:, col] = idst(x[:, col], **kwargs) + return x + + +def naive_dct1(x, norm=None): + """Calculate textbook definition version of DCT-I.""" + x = np.array(x, copy=True) + N = len(x) + M = N-1 + y = np.zeros(N) + m0, m = 1, 2 + if norm == 'ortho': + m0 = np.sqrt(1.0/M) + m = np.sqrt(2.0/M) + for k in range(N): + for n in range(1, N-1): + y[k] += m*x[n]*np.cos(np.pi*n*k/M) + y[k] += m0 * x[0] + y[k] += m0 * x[N-1] * (1 if k % 2 == 0 else -1) + if norm == 'ortho': + y[0] *= 1/np.sqrt(2) + y[N-1] *= 1/np.sqrt(2) + return y + + +def naive_dst1(x, norm=None): + """Calculate textbook definition version of DST-I.""" + x = np.array(x, copy=True) + N = len(x) + M = N+1 + y = np.zeros(N) + for k in range(N): + for n in range(N): + y[k] += 2*x[n]*np.sin(np.pi*(n+1.0)*(k+1.0)/M) + if norm == 'ortho': + y *= np.sqrt(0.5/M) + return y + + +def naive_dct4(x, norm=None): + """Calculate textbook definition version of DCT-IV.""" + x = np.array(x, copy=True) + N = len(x) + y = np.zeros(N) + for k in range(N): + for n in range(N): + y[k] += x[n]*np.cos(np.pi*(n+0.5)*(k+0.5)/(N)) + if norm == 'ortho': + y *= np.sqrt(2.0/N) + else: + y *= 2 + return y + + +def naive_dst4(x, norm=None): + """Calculate textbook definition version of DST-IV.""" + x = np.array(x, copy=True) + N = len(x) + y = np.zeros(N) + for k in range(N): + for n in range(N): + y[k] += x[n]*np.sin(np.pi*(n+0.5)*(k+0.5)/(N)) + if norm == 'ortho': + y *= np.sqrt(2.0/N) + else: + y *= 2 + return y + + +class TestComplex: + def test_dct_complex64(self): + y = dct(1j*np.arange(5, dtype=np.complex64)) + x = 1j*dct(np.arange(5)) + assert_array_almost_equal(x, y) + + def test_dct_complex(self): + y = dct(np.arange(5)*1j) + x = 1j*dct(np.arange(5)) + assert_array_almost_equal(x, y) + + def test_idct_complex(self): + y = idct(np.arange(5)*1j) + x = 1j*idct(np.arange(5)) + assert_array_almost_equal(x, y) + + def test_dst_complex64(self): + y = dst(np.arange(5, dtype=np.complex64)*1j) + x = 1j*dst(np.arange(5)) + assert_array_almost_equal(x, y) + + def test_dst_complex(self): + y = dst(np.arange(5)*1j) + x = 1j*dst(np.arange(5)) + assert_array_almost_equal(x, y) + + def test_idst_complex(self): + y = idst(np.arange(5)*1j) + x = 1j*idst(np.arange(5)) + assert_array_almost_equal(x, y) + + +class _TestDCTBase: + def setup_method(self): + self.rdt = None + self.dec = 14 + self.type = None + + @pytest.fixture + def dct_lock(self): + return threading.Lock() + + def test_definition(self, dct_lock): + for i in FFTWDATA_SIZES: + with dct_lock: + x, yr, dt = fftw_dct_ref(self.type, i, self.rdt) + y = dct(x, type=self.type) + assert_equal(y.dtype, dt) + # XXX: we divide by np.max(y) because the tests fail otherwise. We + # should really use something like assert_array_approx_equal. The + # difference is due to fftw using a better algorithm w.r.t error + # propagation compared to the ones from fftpack. + assert_array_almost_equal(y / np.max(y), yr / np.max(y), decimal=self.dec, + err_msg="Size %d failed" % i) + + def test_axis(self): + nt = 2 + rng = np.random.RandomState(1234) + for i in [7, 8, 9, 16, 32, 64]: + x = rng.randn(nt, i) + y = dct(x, type=self.type) + for j in range(nt): + assert_array_almost_equal(y[j], dct(x[j], type=self.type), + decimal=self.dec) + + x = x.T + y = dct(x, axis=0, type=self.type) + for j in range(nt): + assert_array_almost_equal(y[:,j], dct(x[:,j], type=self.type), + decimal=self.dec) + + +class _TestDCTIBase(_TestDCTBase): + def test_definition_ortho(self): + # Test orthornomal mode. + dt = np.result_type(np.float32, self.rdt) + for xr in X: + x = np.array(xr, dtype=self.rdt) + y = dct(x, norm='ortho', type=1) + y2 = naive_dct1(x, norm='ortho') + assert_equal(y.dtype, dt) + assert_array_almost_equal(y / np.max(y), y2 / np.max(y), decimal=self.dec) + +class _TestDCTIIBase(_TestDCTBase): + def test_definition_matlab(self): + # Test correspondence with MATLAB (orthornomal mode). + dt = np.result_type(np.float32, self.rdt) + for xr, yr in zip(X, Y): + x = np.array(xr, dtype=dt) + y = dct(x, norm="ortho", type=2) + assert_equal(y.dtype, dt) + assert_array_almost_equal(y, yr, decimal=self.dec) + + +class _TestDCTIIIBase(_TestDCTBase): + def test_definition_ortho(self): + # Test orthornomal mode. + dt = np.result_type(np.float32, self.rdt) + for xr in X: + x = np.array(xr, dtype=self.rdt) + y = dct(x, norm='ortho', type=2) + xi = dct(y, norm="ortho", type=3) + assert_equal(xi.dtype, dt) + assert_array_almost_equal(xi, x, decimal=self.dec) + +class _TestDCTIVBase(_TestDCTBase): + def test_definition_ortho(self): + # Test orthornomal mode. + dt = np.result_type(np.float32, self.rdt) + for xr in X: + x = np.array(xr, dtype=self.rdt) + y = dct(x, norm='ortho', type=4) + y2 = naive_dct4(x, norm='ortho') + assert_equal(y.dtype, dt) + assert_array_almost_equal(y / np.max(y), y2 / np.max(y), decimal=self.dec) + + +class TestDCTIDouble(_TestDCTIBase): + def setup_method(self): + self.rdt = np.float64 + self.dec = 10 + self.type = 1 + + +class TestDCTIFloat(_TestDCTIBase): + def setup_method(self): + self.rdt = np.float32 + self.dec = 4 + self.type = 1 + + +class TestDCTIInt(_TestDCTIBase): + def setup_method(self): + self.rdt = int + self.dec = 5 + self.type = 1 + + +class TestDCTIIDouble(_TestDCTIIBase): + def setup_method(self): + self.rdt = np.float64 + self.dec = 10 + self.type = 2 + + +class TestDCTIIFloat(_TestDCTIIBase): + def setup_method(self): + self.rdt = np.float32 + self.dec = 5 + self.type = 2 + + +class TestDCTIIInt(_TestDCTIIBase): + def setup_method(self): + self.rdt = int + self.dec = 5 + self.type = 2 + + +class TestDCTIIIDouble(_TestDCTIIIBase): + def setup_method(self): + self.rdt = np.float64 + self.dec = 14 + self.type = 3 + + +class TestDCTIIIFloat(_TestDCTIIIBase): + def setup_method(self): + self.rdt = np.float32 + self.dec = 5 + self.type = 3 + + +class TestDCTIIIInt(_TestDCTIIIBase): + def setup_method(self): + self.rdt = int + self.dec = 5 + self.type = 3 + + +class TestDCTIVDouble(_TestDCTIVBase): + def setup_method(self): + self.rdt = np.float64 + self.dec = 12 + self.type = 3 + + +class TestDCTIVFloat(_TestDCTIVBase): + def setup_method(self): + self.rdt = np.float32 + self.dec = 5 + self.type = 3 + + +class TestDCTIVInt(_TestDCTIVBase): + def setup_method(self): + self.rdt = int + self.dec = 5 + self.type = 3 + + +class _TestIDCTBase: + def setup_method(self): + self.rdt = None + self.dec = 14 + self.type = None + + @pytest.fixture + def idct_lock(self): + return threading.Lock() + + def test_definition(self, idct_lock): + for i in FFTWDATA_SIZES: + with idct_lock: + xr, yr, dt = fftw_dct_ref(self.type, i, self.rdt) + x = idct(yr, type=self.type) + if self.type == 1: + x /= 2 * (i-1) + else: + x /= 2 * i + assert_equal(x.dtype, dt) + # XXX: we divide by np.max(y) because the tests fail otherwise. We + # should really use something like assert_array_approx_equal. The + # difference is due to fftw using a better algorithm w.r.t error + # propagation compared to the ones from fftpack. + assert_array_almost_equal(x / np.max(x), xr / np.max(x), decimal=self.dec, + err_msg="Size %d failed" % i) + + +class TestIDCTIDouble(_TestIDCTBase): + def setup_method(self): + self.rdt = np.float64 + self.dec = 10 + self.type = 1 + + +class TestIDCTIFloat(_TestIDCTBase): + def setup_method(self): + self.rdt = np.float32 + self.dec = 4 + self.type = 1 + + +class TestIDCTIInt(_TestIDCTBase): + def setup_method(self): + self.rdt = int + self.dec = 4 + self.type = 1 + + +class TestIDCTIIDouble(_TestIDCTBase): + def setup_method(self): + self.rdt = np.float64 + self.dec = 10 + self.type = 2 + + +class TestIDCTIIFloat(_TestIDCTBase): + def setup_method(self): + self.rdt = np.float32 + self.dec = 5 + self.type = 2 + + +class TestIDCTIIInt(_TestIDCTBase): + def setup_method(self): + self.rdt = int + self.dec = 5 + self.type = 2 + + +class TestIDCTIIIDouble(_TestIDCTBase): + def setup_method(self): + self.rdt = np.float64 + self.dec = 14 + self.type = 3 + + +class TestIDCTIIIFloat(_TestIDCTBase): + def setup_method(self): + self.rdt = np.float32 + self.dec = 5 + self.type = 3 + + +class TestIDCTIIIInt(_TestIDCTBase): + def setup_method(self): + self.rdt = int + self.dec = 5 + self.type = 3 + +class TestIDCTIVDouble(_TestIDCTBase): + def setup_method(self): + self.rdt = np.float64 + self.dec = 12 + self.type = 4 + + +class TestIDCTIVFloat(_TestIDCTBase): + def setup_method(self): + self.rdt = np.float32 + self.dec = 5 + self.type = 4 + + +class TestIDCTIVInt(_TestIDCTBase): + def setup_method(self): + self.rdt = int + self.dec = 5 + self.type = 4 + +class _TestDSTBase: + def setup_method(self): + self.rdt = None # dtype + self.dec = None # number of decimals to match + self.type = None # dst type + + @pytest.fixture + def dst_lock(self): + return threading.Lock() + + def test_definition(self, dst_lock): + for i in FFTWDATA_SIZES: + with dst_lock: + xr, yr, dt = fftw_dst_ref(self.type, i, self.rdt) + y = dst(xr, type=self.type) + assert_equal(y.dtype, dt) + # XXX: we divide by np.max(y) because the tests fail otherwise. We + # should really use something like assert_array_approx_equal. The + # difference is due to fftw using a better algorithm w.r.t error + # propagation compared to the ones from fftpack. + assert_array_almost_equal(y / np.max(y), yr / np.max(y), decimal=self.dec, + err_msg="Size %d failed" % i) + + +class _TestDSTIBase(_TestDSTBase): + def test_definition_ortho(self): + # Test orthornomal mode. + dt = np.result_type(np.float32, self.rdt) + for xr in X: + x = np.array(xr, dtype=self.rdt) + y = dst(x, norm='ortho', type=1) + y2 = naive_dst1(x, norm='ortho') + assert_equal(y.dtype, dt) + assert_array_almost_equal(y / np.max(y), y2 / np.max(y), decimal=self.dec) + +class _TestDSTIVBase(_TestDSTBase): + def test_definition_ortho(self): + # Test orthornomal mode. + dt = np.result_type(np.float32, self.rdt) + for xr in X: + x = np.array(xr, dtype=self.rdt) + y = dst(x, norm='ortho', type=4) + y2 = naive_dst4(x, norm='ortho') + assert_equal(y.dtype, dt) + assert_array_almost_equal(y, y2, decimal=self.dec) + +class TestDSTIDouble(_TestDSTIBase): + def setup_method(self): + self.rdt = np.float64 + self.dec = 12 + self.type = 1 + + +class TestDSTIFloat(_TestDSTIBase): + def setup_method(self): + self.rdt = np.float32 + self.dec = 4 + self.type = 1 + + +class TestDSTIInt(_TestDSTIBase): + def setup_method(self): + self.rdt = int + self.dec = 5 + self.type = 1 + + +class TestDSTIIDouble(_TestDSTBase): + def setup_method(self): + self.rdt = np.float64 + self.dec = 14 + self.type = 2 + + +class TestDSTIIFloat(_TestDSTBase): + def setup_method(self): + self.rdt = np.float32 + self.dec = 6 + self.type = 2 + + +class TestDSTIIInt(_TestDSTBase): + def setup_method(self): + self.rdt = int + self.dec = 6 + self.type = 2 + + +class TestDSTIIIDouble(_TestDSTBase): + def setup_method(self): + self.rdt = np.float64 + self.dec = 14 + self.type = 3 + + +class TestDSTIIIFloat(_TestDSTBase): + def setup_method(self): + self.rdt = np.float32 + self.dec = 7 + self.type = 3 + + +class TestDSTIIIInt(_TestDSTBase): + def setup_method(self): + self.rdt = int + self.dec = 7 + self.type = 3 + + +class TestDSTIVDouble(_TestDSTIVBase): + def setup_method(self): + self.rdt = np.float64 + self.dec = 12 + self.type = 4 + + +class TestDSTIVFloat(_TestDSTIVBase): + def setup_method(self): + self.rdt = np.float32 + self.dec = 4 + self.type = 4 + + +class TestDSTIVInt(_TestDSTIVBase): + def setup_method(self): + self.rdt = int + self.dec = 5 + self.type = 4 + + +class _TestIDSTBase: + def setup_method(self): + self.rdt = None + self.dec = None + self.type = None + + @pytest.fixture + def idst_lock(self): + return threading.Lock() + + def test_definition(self, idst_lock): + for i in FFTWDATA_SIZES: + with idst_lock: + xr, yr, dt = fftw_dst_ref(self.type, i, self.rdt) + x = idst(yr, type=self.type) + if self.type == 1: + x /= 2 * (i+1) + else: + x /= 2 * i + assert_equal(x.dtype, dt) + # XXX: we divide by np.max(x) because the tests fail otherwise. We + # should really use something like assert_array_approx_equal. The + # difference is due to fftw using a better algorithm w.r.t error + # propagation compared to the ones from fftpack. + assert_array_almost_equal(x / np.max(x), xr / np.max(x), decimal=self.dec, + err_msg="Size %d failed" % i) + + +class TestIDSTIDouble(_TestIDSTBase): + def setup_method(self): + self.rdt = np.float64 + self.dec = 12 + self.type = 1 + + +class TestIDSTIFloat(_TestIDSTBase): + def setup_method(self): + self.rdt = np.float32 + self.dec = 4 + self.type = 1 + + +class TestIDSTIInt(_TestIDSTBase): + def setup_method(self): + self.rdt = int + self.dec = 4 + self.type = 1 + + +class TestIDSTIIDouble(_TestIDSTBase): + def setup_method(self): + self.rdt = np.float64 + self.dec = 14 + self.type = 2 + + +class TestIDSTIIFloat(_TestIDSTBase): + def setup_method(self): + self.rdt = np.float32 + self.dec = 6 + self.type = 2 + + +class TestIDSTIIInt(_TestIDSTBase): + def setup_method(self): + self.rdt = int + self.dec = 6 + self.type = 2 + + +class TestIDSTIIIDouble(_TestIDSTBase): + def setup_method(self): + self.rdt = np.float64 + self.dec = 14 + self.type = 3 + + +class TestIDSTIIIFloat(_TestIDSTBase): + def setup_method(self): + self.rdt = np.float32 + self.dec = 6 + self.type = 3 + + +class TestIDSTIIIInt(_TestIDSTBase): + def setup_method(self): + self.rdt = int + self.dec = 6 + self.type = 3 + + +class TestIDSTIVDouble(_TestIDSTBase): + def setup_method(self): + self.rdt = np.float64 + self.dec = 12 + self.type = 4 + + +class TestIDSTIVFloat(_TestIDSTBase): + def setup_method(self): + self.rdt = np.float32 + self.dec = 6 + self.type = 4 + + +class TestIDSTIVnt(_TestIDSTBase): + def setup_method(self): + self.rdt = int + self.dec = 6 + self.type = 4 + + +class TestOverwrite: + """Check input overwrite behavior.""" + + real_dtypes = [np.float32, np.float64] + + def _check(self, x, routine, type, fftsize, axis, norm, overwrite_x, **kw): + x2 = x.copy() + routine(x2, type, fftsize, axis, norm, overwrite_x=overwrite_x) + + sig = (f"{routine.__name__}({x.dtype}{x.shape!r}, {fftsize!r}, " + f"axis={axis!r}, overwrite_x={overwrite_x!r})") + if not overwrite_x: + assert_equal(x2, x, err_msg=f"spurious overwrite in {sig}") + + def _check_1d(self, routine, dtype, shape, axis): + rng = np.random.RandomState(1234) + if np.issubdtype(dtype, np.complexfloating): + data = rng.randn(*shape) + 1j*rng.randn(*shape) + else: + data = rng.randn(*shape) + data = data.astype(dtype) + + for type in [1, 2, 3, 4]: + for overwrite_x in [True, False]: + for norm in [None, 'ortho']: + self._check(data, routine, type, None, axis, norm, + overwrite_x) + + def test_dct(self): + for dtype in self.real_dtypes: + self._check_1d(dct, dtype, (16,), -1) + self._check_1d(dct, dtype, (16, 2), 0) + self._check_1d(dct, dtype, (2, 16), 1) + + def test_idct(self): + for dtype in self.real_dtypes: + self._check_1d(idct, dtype, (16,), -1) + self._check_1d(idct, dtype, (16, 2), 0) + self._check_1d(idct, dtype, (2, 16), 1) + + def test_dst(self): + for dtype in self.real_dtypes: + self._check_1d(dst, dtype, (16,), -1) + self._check_1d(dst, dtype, (16, 2), 0) + self._check_1d(dst, dtype, (2, 16), 1) + + def test_idst(self): + for dtype in self.real_dtypes: + self._check_1d(idst, dtype, (16,), -1) + self._check_1d(idst, dtype, (16, 2), 0) + self._check_1d(idst, dtype, (2, 16), 1) + + +class Test_DCTN_IDCTN: + dec = 14 + dct_type = [1, 2, 3, 4] + norms = [None, 'ortho'] + rstate = np.random.RandomState(1234) + shape = (32, 16) + data = rstate.randn(*shape) + + @pytest.mark.parametrize('fforward,finverse', [(dctn, idctn), + (dstn, idstn)]) + @pytest.mark.parametrize('axes', [None, + 1, (1,), [1], + 0, (0,), [0], + (0, 1), [0, 1], + (-2, -1), [-2, -1]]) + @pytest.mark.parametrize('dct_type', dct_type) + @pytest.mark.parametrize('norm', ['ortho']) + def test_axes_round_trip(self, fforward, finverse, axes, dct_type, norm): + tmp = fforward(self.data, type=dct_type, axes=axes, norm=norm) + tmp = finverse(tmp, type=dct_type, axes=axes, norm=norm) + assert_array_almost_equal(self.data, tmp, decimal=12) + + @pytest.mark.parametrize('fforward,fforward_ref', [(dctn, dct_2d_ref), + (dstn, dst_2d_ref)]) + @pytest.mark.parametrize('dct_type', dct_type) + @pytest.mark.parametrize('norm', norms) + def test_dctn_vs_2d_reference(self, fforward, fforward_ref, + dct_type, norm): + y1 = fforward(self.data, type=dct_type, axes=None, norm=norm) + y2 = fforward_ref(self.data, type=dct_type, norm=norm) + assert_array_almost_equal(y1, y2, decimal=11) + + @pytest.mark.parametrize('finverse,finverse_ref', [(idctn, idct_2d_ref), + (idstn, idst_2d_ref)]) + @pytest.mark.parametrize('dct_type', dct_type) + @pytest.mark.parametrize('norm', [None, 'ortho']) + def test_idctn_vs_2d_reference(self, finverse, finverse_ref, + dct_type, norm): + fdata = dctn(self.data, type=dct_type, norm=norm) + y1 = finverse(fdata, type=dct_type, norm=norm) + y2 = finverse_ref(fdata, type=dct_type, norm=norm) + assert_array_almost_equal(y1, y2, decimal=11) + + @pytest.mark.parametrize('fforward,finverse', [(dctn, idctn), + (dstn, idstn)]) + def test_axes_and_shape(self, fforward, finverse): + with assert_raises(ValueError, + match="when given, axes and shape arguments" + " have to be of the same length"): + fforward(self.data, shape=self.data.shape[0], axes=(0, 1)) + + with assert_raises(ValueError, + match="when given, axes and shape arguments" + " have to be of the same length"): + fforward(self.data, shape=self.data.shape[0], axes=None) + + with assert_raises(ValueError, + match="when given, axes and shape arguments" + " have to be of the same length"): + fforward(self.data, shape=self.data.shape, axes=0) + + @pytest.mark.parametrize('fforward', [dctn, dstn]) + def test_shape(self, fforward): + tmp = fforward(self.data, shape=(128, 128), axes=None) + assert_equal(tmp.shape, (128, 128)) + + @pytest.mark.parametrize('fforward,finverse', [(dctn, idctn), + (dstn, idstn)]) + @pytest.mark.parametrize('axes', [1, (1,), [1], + 0, (0,), [0]]) + def test_shape_is_none_with_axes(self, fforward, finverse, axes): + tmp = fforward(self.data, shape=None, axes=axes, norm='ortho') + tmp = finverse(tmp, shape=None, axes=axes, norm='ortho') + assert_array_almost_equal(self.data, tmp, decimal=self.dec) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..1533b5c60b695fce0abf08e2163dfba3bdd4fb17 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/__init__.py @@ -0,0 +1,122 @@ +""" +============================================= +Integration and ODEs (:mod:`scipy.integrate`) +============================================= + +.. currentmodule:: scipy.integrate + +Integrating functions, given function object +============================================ + +.. autosummary:: + :toctree: generated/ + + quad -- General purpose integration + quad_vec -- General purpose integration of vector-valued functions + cubature -- General purpose multi-dimensional integration of array-valued functions + dblquad -- General purpose double integration + tplquad -- General purpose triple integration + nquad -- General purpose N-D integration + tanhsinh -- General purpose elementwise integration + fixed_quad -- Integrate func(x) using Gaussian quadrature of order n + newton_cotes -- Weights and error coefficient for Newton-Cotes integration + lebedev_rule + qmc_quad -- N-D integration using Quasi-Monte Carlo quadrature + IntegrationWarning -- Warning on issues during integration + + +Integrating functions, given fixed samples +========================================== + +.. autosummary:: + :toctree: generated/ + + trapezoid -- Use trapezoidal rule to compute integral. + cumulative_trapezoid -- Use trapezoidal rule to cumulatively compute integral. + simpson -- Use Simpson's rule to compute integral from samples. + cumulative_simpson -- Use Simpson's rule to cumulatively compute integral from samples. + romb -- Use Romberg Integration to compute integral from + -- (2**k + 1) evenly-spaced samples. + +.. seealso:: + + :mod:`scipy.special` for orthogonal polynomials (special) for Gaussian + quadrature roots and weights for other weighting factors and regions. + +Summation +========= + +.. autosummary:: + :toctree: generated/ + + nsum + +Solving initial value problems for ODE systems +============================================== + +The solvers are implemented as individual classes, which can be used directly +(low-level usage) or through a convenience function. + +.. autosummary:: + :toctree: generated/ + + solve_ivp -- Convenient function for ODE integration. + RK23 -- Explicit Runge-Kutta solver of order 3(2). + RK45 -- Explicit Runge-Kutta solver of order 5(4). + DOP853 -- Explicit Runge-Kutta solver of order 8. + Radau -- Implicit Runge-Kutta solver of order 5. + BDF -- Implicit multi-step variable order (1 to 5) solver. + LSODA -- LSODA solver from ODEPACK Fortran package. + OdeSolver -- Base class for ODE solvers. + DenseOutput -- Local interpolant for computing a dense output. + OdeSolution -- Class which represents a continuous ODE solution. + + +Old API +------- + +These are the routines developed earlier for SciPy. They wrap older solvers +implemented in Fortran (mostly ODEPACK). While the interface to them is not +particularly convenient and certain features are missing compared to the new +API, the solvers themselves are of good quality and work fast as compiled +Fortran code. In some cases, it might be worth using this old API. + +.. autosummary:: + :toctree: generated/ + + odeint -- General integration of ordinary differential equations. + ode -- Integrate ODE using VODE and ZVODE routines. + complex_ode -- Convert a complex-valued ODE to real-valued and integrate. + ODEintWarning -- Warning raised during the execution of `odeint`. + + +Solving boundary value problems for ODE systems +=============================================== + +.. autosummary:: + :toctree: generated/ + + solve_bvp -- Solve a boundary value problem for a system of ODEs. +""" # noqa: E501 + + +from ._quadrature import * +from ._odepack_py import * +from ._quadpack_py import * +from ._ode import * +from ._bvp import solve_bvp +from ._ivp import (solve_ivp, OdeSolution, DenseOutput, + OdeSolver, RK23, RK45, DOP853, Radau, BDF, LSODA) +from ._quad_vec import quad_vec +from ._tanhsinh import nsum, tanhsinh +from ._cubature import cubature +from ._lebedev import lebedev_rule + +# Deprecated namespaces, to be removed in v2.0.0 +from . import dop, lsoda, vode, odepack, quadpack + +__all__ = [s for s in dir() if not s.startswith('_')] + +from scipy._lib._testutils import PytestTester +test = PytestTester(__name__) +del PytestTester diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_bvp.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_bvp.py new file mode 100644 index 0000000000000000000000000000000000000000..74406c89a689edc3de21fcb7274c90d41b8d2dcc --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_bvp.py @@ -0,0 +1,1154 @@ +"""Boundary value problem solver.""" +from warnings import warn + +import numpy as np +from numpy.linalg import pinv + +from scipy.sparse import coo_matrix, csc_matrix +from scipy.sparse.linalg import splu +from scipy.optimize import OptimizeResult + + +EPS = np.finfo(float).eps + + +def estimate_fun_jac(fun, x, y, p, f0=None): + """Estimate derivatives of an ODE system rhs with forward differences. + + Returns + ------- + df_dy : ndarray, shape (n, n, m) + Derivatives with respect to y. An element (i, j, q) corresponds to + d f_i(x_q, y_q) / d (y_q)_j. + df_dp : ndarray with shape (n, k, m) or None + Derivatives with respect to p. An element (i, j, q) corresponds to + d f_i(x_q, y_q, p) / d p_j. If `p` is empty, None is returned. + """ + n, m = y.shape + if f0 is None: + f0 = fun(x, y, p) + + dtype = y.dtype + + df_dy = np.empty((n, n, m), dtype=dtype) + h = EPS**0.5 * (1 + np.abs(y)) + for i in range(n): + y_new = y.copy() + y_new[i] += h[i] + hi = y_new[i] - y[i] + f_new = fun(x, y_new, p) + df_dy[:, i, :] = (f_new - f0) / hi + + k = p.shape[0] + if k == 0: + df_dp = None + else: + df_dp = np.empty((n, k, m), dtype=dtype) + h = EPS**0.5 * (1 + np.abs(p)) + for i in range(k): + p_new = p.copy() + p_new[i] += h[i] + hi = p_new[i] - p[i] + f_new = fun(x, y, p_new) + df_dp[:, i, :] = (f_new - f0) / hi + + return df_dy, df_dp + + +def estimate_bc_jac(bc, ya, yb, p, bc0=None): + """Estimate derivatives of boundary conditions with forward differences. + + Returns + ------- + dbc_dya : ndarray, shape (n + k, n) + Derivatives with respect to ya. An element (i, j) corresponds to + d bc_i / d ya_j. + dbc_dyb : ndarray, shape (n + k, n) + Derivatives with respect to yb. An element (i, j) corresponds to + d bc_i / d ya_j. + dbc_dp : ndarray with shape (n + k, k) or None + Derivatives with respect to p. An element (i, j) corresponds to + d bc_i / d p_j. If `p` is empty, None is returned. + """ + n = ya.shape[0] + k = p.shape[0] + + if bc0 is None: + bc0 = bc(ya, yb, p) + + dtype = ya.dtype + + dbc_dya = np.empty((n, n + k), dtype=dtype) + h = EPS**0.5 * (1 + np.abs(ya)) + for i in range(n): + ya_new = ya.copy() + ya_new[i] += h[i] + hi = ya_new[i] - ya[i] + bc_new = bc(ya_new, yb, p) + dbc_dya[i] = (bc_new - bc0) / hi + dbc_dya = dbc_dya.T + + h = EPS**0.5 * (1 + np.abs(yb)) + dbc_dyb = np.empty((n, n + k), dtype=dtype) + for i in range(n): + yb_new = yb.copy() + yb_new[i] += h[i] + hi = yb_new[i] - yb[i] + bc_new = bc(ya, yb_new, p) + dbc_dyb[i] = (bc_new - bc0) / hi + dbc_dyb = dbc_dyb.T + + if k == 0: + dbc_dp = None + else: + h = EPS**0.5 * (1 + np.abs(p)) + dbc_dp = np.empty((k, n + k), dtype=dtype) + for i in range(k): + p_new = p.copy() + p_new[i] += h[i] + hi = p_new[i] - p[i] + bc_new = bc(ya, yb, p_new) + dbc_dp[i] = (bc_new - bc0) / hi + dbc_dp = dbc_dp.T + + return dbc_dya, dbc_dyb, dbc_dp + + +def compute_jac_indices(n, m, k): + """Compute indices for the collocation system Jacobian construction. + + See `construct_global_jac` for the explanation. + """ + i_col = np.repeat(np.arange((m - 1) * n), n) + j_col = (np.tile(np.arange(n), n * (m - 1)) + + np.repeat(np.arange(m - 1) * n, n**2)) + + i_bc = np.repeat(np.arange((m - 1) * n, m * n + k), n) + j_bc = np.tile(np.arange(n), n + k) + + i_p_col = np.repeat(np.arange((m - 1) * n), k) + j_p_col = np.tile(np.arange(m * n, m * n + k), (m - 1) * n) + + i_p_bc = np.repeat(np.arange((m - 1) * n, m * n + k), k) + j_p_bc = np.tile(np.arange(m * n, m * n + k), n + k) + + i = np.hstack((i_col, i_col, i_bc, i_bc, i_p_col, i_p_bc)) + j = np.hstack((j_col, j_col + n, + j_bc, j_bc + (m - 1) * n, + j_p_col, j_p_bc)) + + return i, j + + +def stacked_matmul(a, b): + """Stacked matrix multiply: out[i,:,:] = np.dot(a[i,:,:], b[i,:,:]). + + Empirical optimization. Use outer Python loop and BLAS for large + matrices, otherwise use a single einsum call. + """ + if a.shape[1] > 50: + out = np.empty((a.shape[0], a.shape[1], b.shape[2])) + for i in range(a.shape[0]): + out[i] = np.dot(a[i], b[i]) + return out + else: + return np.einsum('...ij,...jk->...ik', a, b) + + +def construct_global_jac(n, m, k, i_jac, j_jac, h, df_dy, df_dy_middle, df_dp, + df_dp_middle, dbc_dya, dbc_dyb, dbc_dp): + """Construct the Jacobian of the collocation system. + + There are n * m + k functions: m - 1 collocations residuals, each + containing n components, followed by n + k boundary condition residuals. + + There are n * m + k variables: m vectors of y, each containing n + components, followed by k values of vector p. + + For example, let m = 4, n = 2 and k = 1, then the Jacobian will have + the following sparsity structure: + + 1 1 2 2 0 0 0 0 5 + 1 1 2 2 0 0 0 0 5 + 0 0 1 1 2 2 0 0 5 + 0 0 1 1 2 2 0 0 5 + 0 0 0 0 1 1 2 2 5 + 0 0 0 0 1 1 2 2 5 + + 3 3 0 0 0 0 4 4 6 + 3 3 0 0 0 0 4 4 6 + 3 3 0 0 0 0 4 4 6 + + Zeros denote identically zero values, other values denote different kinds + of blocks in the matrix (see below). The blank row indicates the separation + of collocation residuals from boundary conditions. And the blank column + indicates the separation of y values from p values. + + Refer to [1]_ (p. 306) for the formula of n x n blocks for derivatives + of collocation residuals with respect to y. + + Parameters + ---------- + n : int + Number of equations in the ODE system. + m : int + Number of nodes in the mesh. + k : int + Number of the unknown parameters. + i_jac, j_jac : ndarray + Row and column indices returned by `compute_jac_indices`. They + represent different blocks in the Jacobian matrix in the following + order (see the scheme above): + + * 1: m - 1 diagonal n x n blocks for the collocation residuals. + * 2: m - 1 off-diagonal n x n blocks for the collocation residuals. + * 3 : (n + k) x n block for the dependency of the boundary + conditions on ya. + * 4: (n + k) x n block for the dependency of the boundary + conditions on yb. + * 5: (m - 1) * n x k block for the dependency of the collocation + residuals on p. + * 6: (n + k) x k block for the dependency of the boundary + conditions on p. + + df_dy : ndarray, shape (n, n, m) + Jacobian of f with respect to y computed at the mesh nodes. + df_dy_middle : ndarray, shape (n, n, m - 1) + Jacobian of f with respect to y computed at the middle between the + mesh nodes. + df_dp : ndarray with shape (n, k, m) or None + Jacobian of f with respect to p computed at the mesh nodes. + df_dp_middle : ndarray with shape (n, k, m - 1) or None + Jacobian of f with respect to p computed at the middle between the + mesh nodes. + dbc_dya, dbc_dyb : ndarray, shape (n, n) + Jacobian of bc with respect to ya and yb. + dbc_dp : ndarray with shape (n, k) or None + Jacobian of bc with respect to p. + + Returns + ------- + J : csc_matrix, shape (n * m + k, n * m + k) + Jacobian of the collocation system in a sparse form. + + References + ---------- + .. [1] J. Kierzenka, L. F. Shampine, "A BVP Solver Based on Residual + Control and the Maltab PSE", ACM Trans. Math. Softw., Vol. 27, + Number 3, pp. 299-316, 2001. + """ + df_dy = np.transpose(df_dy, (2, 0, 1)) + df_dy_middle = np.transpose(df_dy_middle, (2, 0, 1)) + + h = h[:, np.newaxis, np.newaxis] + + dtype = df_dy.dtype + + # Computing diagonal n x n blocks. + dPhi_dy_0 = np.empty((m - 1, n, n), dtype=dtype) + dPhi_dy_0[:] = -np.identity(n) + dPhi_dy_0 -= h / 6 * (df_dy[:-1] + 2 * df_dy_middle) + T = stacked_matmul(df_dy_middle, df_dy[:-1]) + dPhi_dy_0 -= h**2 / 12 * T + + # Computing off-diagonal n x n blocks. + dPhi_dy_1 = np.empty((m - 1, n, n), dtype=dtype) + dPhi_dy_1[:] = np.identity(n) + dPhi_dy_1 -= h / 6 * (df_dy[1:] + 2 * df_dy_middle) + T = stacked_matmul(df_dy_middle, df_dy[1:]) + dPhi_dy_1 += h**2 / 12 * T + + values = np.hstack((dPhi_dy_0.ravel(), dPhi_dy_1.ravel(), dbc_dya.ravel(), + dbc_dyb.ravel())) + + if k > 0: + df_dp = np.transpose(df_dp, (2, 0, 1)) + df_dp_middle = np.transpose(df_dp_middle, (2, 0, 1)) + T = stacked_matmul(df_dy_middle, df_dp[:-1] - df_dp[1:]) + df_dp_middle += 0.125 * h * T + dPhi_dp = -h/6 * (df_dp[:-1] + df_dp[1:] + 4 * df_dp_middle) + values = np.hstack((values, dPhi_dp.ravel(), dbc_dp.ravel())) + + J = coo_matrix((values, (i_jac, j_jac))) + return csc_matrix(J) + + +def collocation_fun(fun, y, p, x, h): + """Evaluate collocation residuals. + + This function lies in the core of the method. The solution is sought + as a cubic C1 continuous spline with derivatives matching the ODE rhs + at given nodes `x`. Collocation conditions are formed from the equality + of the spline derivatives and rhs of the ODE system in the middle points + between nodes. + + Such method is classified to Lobbato IIIA family in ODE literature. + Refer to [1]_ for the formula and some discussion. + + Returns + ------- + col_res : ndarray, shape (n, m - 1) + Collocation residuals at the middle points of the mesh intervals. + y_middle : ndarray, shape (n, m - 1) + Values of the cubic spline evaluated at the middle points of the mesh + intervals. + f : ndarray, shape (n, m) + RHS of the ODE system evaluated at the mesh nodes. + f_middle : ndarray, shape (n, m - 1) + RHS of the ODE system evaluated at the middle points of the mesh + intervals (and using `y_middle`). + + References + ---------- + .. [1] J. Kierzenka, L. F. Shampine, "A BVP Solver Based on Residual + Control and the Maltab PSE", ACM Trans. Math. Softw., Vol. 27, + Number 3, pp. 299-316, 2001. + """ + f = fun(x, y, p) + y_middle = (0.5 * (y[:, 1:] + y[:, :-1]) - + 0.125 * h * (f[:, 1:] - f[:, :-1])) + f_middle = fun(x[:-1] + 0.5 * h, y_middle, p) + col_res = y[:, 1:] - y[:, :-1] - h / 6 * (f[:, :-1] + f[:, 1:] + + 4 * f_middle) + + return col_res, y_middle, f, f_middle + + +def prepare_sys(n, m, k, fun, bc, fun_jac, bc_jac, x, h): + """Create the function and the Jacobian for the collocation system.""" + x_middle = x[:-1] + 0.5 * h + i_jac, j_jac = compute_jac_indices(n, m, k) + + def col_fun(y, p): + return collocation_fun(fun, y, p, x, h) + + def sys_jac(y, p, y_middle, f, f_middle, bc0): + if fun_jac is None: + df_dy, df_dp = estimate_fun_jac(fun, x, y, p, f) + df_dy_middle, df_dp_middle = estimate_fun_jac( + fun, x_middle, y_middle, p, f_middle) + else: + df_dy, df_dp = fun_jac(x, y, p) + df_dy_middle, df_dp_middle = fun_jac(x_middle, y_middle, p) + + if bc_jac is None: + dbc_dya, dbc_dyb, dbc_dp = estimate_bc_jac(bc, y[:, 0], y[:, -1], + p, bc0) + else: + dbc_dya, dbc_dyb, dbc_dp = bc_jac(y[:, 0], y[:, -1], p) + + return construct_global_jac(n, m, k, i_jac, j_jac, h, df_dy, + df_dy_middle, df_dp, df_dp_middle, dbc_dya, + dbc_dyb, dbc_dp) + + return col_fun, sys_jac + + +def solve_newton(n, m, h, col_fun, bc, jac, y, p, B, bvp_tol, bc_tol): + """Solve the nonlinear collocation system by a Newton method. + + This is a simple Newton method with a backtracking line search. As + advised in [1]_, an affine-invariant criterion function F = ||J^-1 r||^2 + is used, where J is the Jacobian matrix at the current iteration and r is + the vector or collocation residuals (values of the system lhs). + + The method alters between full Newton iterations and the fixed-Jacobian + iterations based + + There are other tricks proposed in [1]_, but they are not used as they + don't seem to improve anything significantly, and even break the + convergence on some test problems I tried. + + All important parameters of the algorithm are defined inside the function. + + Parameters + ---------- + n : int + Number of equations in the ODE system. + m : int + Number of nodes in the mesh. + h : ndarray, shape (m-1,) + Mesh intervals. + col_fun : callable + Function computing collocation residuals. + bc : callable + Function computing boundary condition residuals. + jac : callable + Function computing the Jacobian of the whole system (including + collocation and boundary condition residuals). It is supposed to + return csc_matrix. + y : ndarray, shape (n, m) + Initial guess for the function values at the mesh nodes. + p : ndarray, shape (k,) + Initial guess for the unknown parameters. + B : ndarray with shape (n, n) or None + Matrix to force the S y(a) = 0 condition for a problems with the + singular term. If None, the singular term is assumed to be absent. + bvp_tol : float + Tolerance to which we want to solve a BVP. + bc_tol : float + Tolerance to which we want to satisfy the boundary conditions. + + Returns + ------- + y : ndarray, shape (n, m) + Final iterate for the function values at the mesh nodes. + p : ndarray, shape (k,) + Final iterate for the unknown parameters. + singular : bool + True, if the LU decomposition failed because Jacobian turned out + to be singular. + + References + ---------- + .. [1] U. Ascher, R. Mattheij and R. Russell "Numerical Solution of + Boundary Value Problems for Ordinary Differential Equations" + """ + # We know that the solution residuals at the middle points of the mesh + # are connected with collocation residuals r_middle = 1.5 * col_res / h. + # As our BVP solver tries to decrease relative residuals below a certain + # tolerance, it seems reasonable to terminated Newton iterations by + # comparison of r_middle / (1 + np.abs(f_middle)) with a certain threshold, + # which we choose to be 1.5 orders lower than the BVP tolerance. We rewrite + # the condition as col_res < tol_r * (1 + np.abs(f_middle)), then tol_r + # should be computed as follows: + tol_r = 2/3 * h * 5e-2 * bvp_tol + + # Maximum allowed number of Jacobian evaluation and factorization, in + # other words, the maximum number of full Newton iterations. A small value + # is recommended in the literature. + max_njev = 4 + + # Maximum number of iterations, considering that some of them can be + # performed with the fixed Jacobian. In theory, such iterations are cheap, + # but it's not that simple in Python. + max_iter = 8 + + # Minimum relative improvement of the criterion function to accept the + # step (Armijo constant). + sigma = 0.2 + + # Step size decrease factor for backtracking. + tau = 0.5 + + # Maximum number of backtracking steps, the minimum step is then + # tau ** n_trial. + n_trial = 4 + + col_res, y_middle, f, f_middle = col_fun(y, p) + bc_res = bc(y[:, 0], y[:, -1], p) + res = np.hstack((col_res.ravel(order='F'), bc_res)) + + njev = 0 + singular = False + recompute_jac = True + for iteration in range(max_iter): + if recompute_jac: + J = jac(y, p, y_middle, f, f_middle, bc_res) + njev += 1 + try: + LU = splu(J) + except RuntimeError: + singular = True + break + + step = LU.solve(res) + cost = np.dot(step, step) + + y_step = step[:m * n].reshape((n, m), order='F') + p_step = step[m * n:] + + alpha = 1 + for trial in range(n_trial + 1): + y_new = y - alpha * y_step + if B is not None: + y_new[:, 0] = np.dot(B, y_new[:, 0]) + p_new = p - alpha * p_step + + col_res, y_middle, f, f_middle = col_fun(y_new, p_new) + bc_res = bc(y_new[:, 0], y_new[:, -1], p_new) + res = np.hstack((col_res.ravel(order='F'), bc_res)) + + step_new = LU.solve(res) + cost_new = np.dot(step_new, step_new) + if cost_new < (1 - 2 * alpha * sigma) * cost: + break + + if trial < n_trial: + alpha *= tau + + y = y_new + p = p_new + + if njev == max_njev: + break + + if (np.all(np.abs(col_res) < tol_r * (1 + np.abs(f_middle))) and + np.all(np.abs(bc_res) < bc_tol)): + break + + # If the full step was taken, then we are going to continue with + # the same Jacobian. This is the approach of BVP_SOLVER. + if alpha == 1: + step = step_new + cost = cost_new + recompute_jac = False + else: + recompute_jac = True + + return y, p, singular + + +def print_iteration_header(): + print(f"{'Iteration':^15}{'Max residual':^15}{'Max BC residual':^15}" + f"{'Total nodes':^15}{'Nodes added':^15}") + + +def print_iteration_progress(iteration, residual, bc_residual, total_nodes, + nodes_added): + print(f"{iteration:^15}{residual:^15.2e}{bc_residual:^15.2e}" + f"{total_nodes:^15}{nodes_added:^15}") + + +class BVPResult(OptimizeResult): + pass + + +TERMINATION_MESSAGES = { + 0: "The algorithm converged to the desired accuracy.", + 1: "The maximum number of mesh nodes is exceeded.", + 2: "A singular Jacobian encountered when solving the collocation system.", + 3: "The solver was unable to satisfy boundary conditions tolerance on iteration 10." +} + + +def estimate_rms_residuals(fun, sol, x, h, p, r_middle, f_middle): + """Estimate rms values of collocation residuals using Lobatto quadrature. + + The residuals are defined as the difference between the derivatives of + our solution and rhs of the ODE system. We use relative residuals, i.e., + normalized by 1 + np.abs(f). RMS values are computed as sqrt from the + normalized integrals of the squared relative residuals over each interval. + Integrals are estimated using 5-point Lobatto quadrature [1]_, we use the + fact that residuals at the mesh nodes are identically zero. + + In [2] they don't normalize integrals by interval lengths, which gives + a higher rate of convergence of the residuals by the factor of h**0.5. + I chose to do such normalization for an ease of interpretation of return + values as RMS estimates. + + Returns + ------- + rms_res : ndarray, shape (m - 1,) + Estimated rms values of the relative residuals over each interval. + + References + ---------- + .. [1] http://mathworld.wolfram.com/LobattoQuadrature.html + .. [2] J. Kierzenka, L. F. Shampine, "A BVP Solver Based on Residual + Control and the Maltab PSE", ACM Trans. Math. Softw., Vol. 27, + Number 3, pp. 299-316, 2001. + """ + x_middle = x[:-1] + 0.5 * h + s = 0.5 * h * (3/7)**0.5 + x1 = x_middle + s + x2 = x_middle - s + y1 = sol(x1) + y2 = sol(x2) + y1_prime = sol(x1, 1) + y2_prime = sol(x2, 1) + f1 = fun(x1, y1, p) + f2 = fun(x2, y2, p) + r1 = y1_prime - f1 + r2 = y2_prime - f2 + + r_middle /= 1 + np.abs(f_middle) + r1 /= 1 + np.abs(f1) + r2 /= 1 + np.abs(f2) + + r1 = np.sum(np.real(r1 * np.conj(r1)), axis=0) + r2 = np.sum(np.real(r2 * np.conj(r2)), axis=0) + r_middle = np.sum(np.real(r_middle * np.conj(r_middle)), axis=0) + + return (0.5 * (32 / 45 * r_middle + 49 / 90 * (r1 + r2))) ** 0.5 + + +def create_spline(y, yp, x, h): + """Create a cubic spline given values and derivatives. + + Formulas for the coefficients are taken from interpolate.CubicSpline. + + Returns + ------- + sol : PPoly + Constructed spline as a PPoly instance. + """ + from scipy.interpolate import PPoly + + n, m = y.shape + c = np.empty((4, n, m - 1), dtype=y.dtype) + slope = (y[:, 1:] - y[:, :-1]) / h + t = (yp[:, :-1] + yp[:, 1:] - 2 * slope) / h + c[0] = t / h + c[1] = (slope - yp[:, :-1]) / h - t + c[2] = yp[:, :-1] + c[3] = y[:, :-1] + c = np.moveaxis(c, 1, 0) + + return PPoly(c, x, extrapolate=True, axis=1) + + +def modify_mesh(x, insert_1, insert_2): + """Insert nodes into a mesh. + + Nodes removal logic is not established, its impact on the solver is + presumably negligible. So, only insertion is done in this function. + + Parameters + ---------- + x : ndarray, shape (m,) + Mesh nodes. + insert_1 : ndarray + Intervals to each insert 1 new node in the middle. + insert_2 : ndarray + Intervals to each insert 2 new nodes, such that divide an interval + into 3 equal parts. + + Returns + ------- + x_new : ndarray + New mesh nodes. + + Notes + ----- + `insert_1` and `insert_2` should not have common values. + """ + # Because np.insert implementation apparently varies with a version of + # NumPy, we use a simple and reliable approach with sorting. + return np.sort(np.hstack(( + x, + 0.5 * (x[insert_1] + x[insert_1 + 1]), + (2 * x[insert_2] + x[insert_2 + 1]) / 3, + (x[insert_2] + 2 * x[insert_2 + 1]) / 3 + ))) + + +def wrap_functions(fun, bc, fun_jac, bc_jac, k, a, S, D, dtype): + """Wrap functions for unified usage in the solver.""" + if fun_jac is None: + fun_jac_wrapped = None + + if bc_jac is None: + bc_jac_wrapped = None + + if k == 0: + def fun_p(x, y, _): + return np.asarray(fun(x, y), dtype) + + def bc_wrapped(ya, yb, _): + return np.asarray(bc(ya, yb), dtype) + + if fun_jac is not None: + def fun_jac_p(x, y, _): + return np.asarray(fun_jac(x, y), dtype), None + + if bc_jac is not None: + def bc_jac_wrapped(ya, yb, _): + dbc_dya, dbc_dyb = bc_jac(ya, yb) + return (np.asarray(dbc_dya, dtype), + np.asarray(dbc_dyb, dtype), None) + else: + def fun_p(x, y, p): + return np.asarray(fun(x, y, p), dtype) + + def bc_wrapped(x, y, p): + return np.asarray(bc(x, y, p), dtype) + + if fun_jac is not None: + def fun_jac_p(x, y, p): + df_dy, df_dp = fun_jac(x, y, p) + return np.asarray(df_dy, dtype), np.asarray(df_dp, dtype) + + if bc_jac is not None: + def bc_jac_wrapped(ya, yb, p): + dbc_dya, dbc_dyb, dbc_dp = bc_jac(ya, yb, p) + return (np.asarray(dbc_dya, dtype), np.asarray(dbc_dyb, dtype), + np.asarray(dbc_dp, dtype)) + + if S is None: + fun_wrapped = fun_p + else: + def fun_wrapped(x, y, p): + f = fun_p(x, y, p) + if x[0] == a: + f[:, 0] = np.dot(D, f[:, 0]) + f[:, 1:] += np.dot(S, y[:, 1:]) / (x[1:] - a) + else: + f += np.dot(S, y) / (x - a) + return f + + if fun_jac is not None: + if S is None: + fun_jac_wrapped = fun_jac_p + else: + Sr = S[:, :, np.newaxis] + + def fun_jac_wrapped(x, y, p): + df_dy, df_dp = fun_jac_p(x, y, p) + if x[0] == a: + df_dy[:, :, 0] = np.dot(D, df_dy[:, :, 0]) + df_dy[:, :, 1:] += Sr / (x[1:] - a) + else: + df_dy += Sr / (x - a) + + return df_dy, df_dp + + return fun_wrapped, bc_wrapped, fun_jac_wrapped, bc_jac_wrapped + + +def solve_bvp(fun, bc, x, y, p=None, S=None, fun_jac=None, bc_jac=None, + tol=1e-3, max_nodes=1000, verbose=0, bc_tol=None): + """Solve a boundary value problem for a system of ODEs. + + This function numerically solves a first order system of ODEs subject to + two-point boundary conditions:: + + dy / dx = f(x, y, p) + S * y / (x - a), a <= x <= b + bc(y(a), y(b), p) = 0 + + Here x is a 1-D independent variable, y(x) is an n-D + vector-valued function and p is a k-D vector of unknown + parameters which is to be found along with y(x). For the problem to be + determined, there must be n + k boundary conditions, i.e., bc must be an + (n + k)-D function. + + The last singular term on the right-hand side of the system is optional. + It is defined by an n-by-n matrix S, such that the solution must satisfy + S y(a) = 0. This condition will be forced during iterations, so it must not + contradict boundary conditions. See [2]_ for the explanation how this term + is handled when solving BVPs numerically. + + Problems in a complex domain can be solved as well. In this case, y and p + are considered to be complex, and f and bc are assumed to be complex-valued + functions, but x stays real. Note that f and bc must be complex + differentiable (satisfy Cauchy-Riemann equations [4]_), otherwise you + should rewrite your problem for real and imaginary parts separately. To + solve a problem in a complex domain, pass an initial guess for y with a + complex data type (see below). + + Parameters + ---------- + fun : callable + Right-hand side of the system. The calling signature is ``fun(x, y)``, + or ``fun(x, y, p)`` if parameters are present. All arguments are + ndarray: ``x`` with shape (m,), ``y`` with shape (n, m), meaning that + ``y[:, i]`` corresponds to ``x[i]``, and ``p`` with shape (k,). The + return value must be an array with shape (n, m) and with the same + layout as ``y``. + bc : callable + Function evaluating residuals of the boundary conditions. The calling + signature is ``bc(ya, yb)``, or ``bc(ya, yb, p)`` if parameters are + present. All arguments are ndarray: ``ya`` and ``yb`` with shape (n,), + and ``p`` with shape (k,). The return value must be an array with + shape (n + k,). + x : array_like, shape (m,) + Initial mesh. Must be a strictly increasing sequence of real numbers + with ``x[0]=a`` and ``x[-1]=b``. + y : array_like, shape (n, m) + Initial guess for the function values at the mesh nodes, ith column + corresponds to ``x[i]``. For problems in a complex domain pass `y` + with a complex data type (even if the initial guess is purely real). + p : array_like with shape (k,) or None, optional + Initial guess for the unknown parameters. If None (default), it is + assumed that the problem doesn't depend on any parameters. + S : array_like with shape (n, n) or None + Matrix defining the singular term. If None (default), the problem is + solved without the singular term. + fun_jac : callable or None, optional + Function computing derivatives of f with respect to y and p. The + calling signature is ``fun_jac(x, y)``, or ``fun_jac(x, y, p)`` if + parameters are present. The return must contain 1 or 2 elements in the + following order: + + * df_dy : array_like with shape (n, n, m), where an element + (i, j, q) equals to d f_i(x_q, y_q, p) / d (y_q)_j. + * df_dp : array_like with shape (n, k, m), where an element + (i, j, q) equals to d f_i(x_q, y_q, p) / d p_j. + + Here q numbers nodes at which x and y are defined, whereas i and j + number vector components. If the problem is solved without unknown + parameters, df_dp should not be returned. + + If `fun_jac` is None (default), the derivatives will be estimated + by the forward finite differences. + bc_jac : callable or None, optional + Function computing derivatives of bc with respect to ya, yb, and p. + The calling signature is ``bc_jac(ya, yb)``, or ``bc_jac(ya, yb, p)`` + if parameters are present. The return must contain 2 or 3 elements in + the following order: + + * dbc_dya : array_like with shape (n, n), where an element (i, j) + equals to d bc_i(ya, yb, p) / d ya_j. + * dbc_dyb : array_like with shape (n, n), where an element (i, j) + equals to d bc_i(ya, yb, p) / d yb_j. + * dbc_dp : array_like with shape (n, k), where an element (i, j) + equals to d bc_i(ya, yb, p) / d p_j. + + If the problem is solved without unknown parameters, dbc_dp should not + be returned. + + If `bc_jac` is None (default), the derivatives will be estimated by + the forward finite differences. + tol : float, optional + Desired tolerance of the solution. If we define ``r = y' - f(x, y)``, + where y is the found solution, then the solver tries to achieve on each + mesh interval ``norm(r / (1 + abs(f)) < tol``, where ``norm`` is + estimated in a root mean squared sense (using a numerical quadrature + formula). Default is 1e-3. + max_nodes : int, optional + Maximum allowed number of the mesh nodes. If exceeded, the algorithm + terminates. Default is 1000. + verbose : {0, 1, 2}, optional + Level of algorithm's verbosity: + + * 0 (default) : work silently. + * 1 : display a termination report. + * 2 : display progress during iterations. + bc_tol : float, optional + Desired absolute tolerance for the boundary condition residuals: `bc` + value should satisfy ``abs(bc) < bc_tol`` component-wise. + Equals to `tol` by default. Up to 10 iterations are allowed to achieve this + tolerance. + + Returns + ------- + Bunch object with the following fields defined: + sol : PPoly + Found solution for y as `scipy.interpolate.PPoly` instance, a C1 + continuous cubic spline. + p : ndarray or None, shape (k,) + Found parameters. None, if the parameters were not present in the + problem. + x : ndarray, shape (m,) + Nodes of the final mesh. + y : ndarray, shape (n, m) + Solution values at the mesh nodes. + yp : ndarray, shape (n, m) + Solution derivatives at the mesh nodes. + rms_residuals : ndarray, shape (m - 1,) + RMS values of the relative residuals over each mesh interval (see the + description of `tol` parameter). + niter : int + Number of completed iterations. + status : int + Reason for algorithm termination: + + * 0: The algorithm converged to the desired accuracy. + * 1: The maximum number of mesh nodes is exceeded. + * 2: A singular Jacobian encountered when solving the collocation + system. + + message : string + Verbal description of the termination reason. + success : bool + True if the algorithm converged to the desired accuracy (``status=0``). + + Notes + ----- + This function implements a 4th order collocation algorithm with the + control of residuals similar to [1]_. A collocation system is solved + by a damped Newton method with an affine-invariant criterion function as + described in [3]_. + + Note that in [1]_ integral residuals are defined without normalization + by interval lengths. So, their definition is different by a multiplier of + h**0.5 (h is an interval length) from the definition used here. + + .. versionadded:: 0.18.0 + + References + ---------- + .. [1] J. Kierzenka, L. F. Shampine, "A BVP Solver Based on Residual + Control and the Maltab PSE", ACM Trans. Math. Softw., Vol. 27, + Number 3, pp. 299-316, 2001. + .. [2] L.F. Shampine, P. H. Muir and H. Xu, "A User-Friendly Fortran BVP + Solver". + .. [3] U. Ascher, R. Mattheij and R. Russell "Numerical Solution of + Boundary Value Problems for Ordinary Differential Equations". + .. [4] `Cauchy-Riemann equations + `_ on + Wikipedia. + + Examples + -------- + In the first example, we solve Bratu's problem:: + + y'' + k * exp(y) = 0 + y(0) = y(1) = 0 + + for k = 1. + + We rewrite the equation as a first-order system and implement its + right-hand side evaluation:: + + y1' = y2 + y2' = -exp(y1) + + >>> import numpy as np + >>> def fun(x, y): + ... return np.vstack((y[1], -np.exp(y[0]))) + + Implement evaluation of the boundary condition residuals: + + >>> def bc(ya, yb): + ... return np.array([ya[0], yb[0]]) + + Define the initial mesh with 5 nodes: + + >>> x = np.linspace(0, 1, 5) + + This problem is known to have two solutions. To obtain both of them, we + use two different initial guesses for y. We denote them by subscripts + a and b. + + >>> y_a = np.zeros((2, x.size)) + >>> y_b = np.zeros((2, x.size)) + >>> y_b[0] = 3 + + Now we are ready to run the solver. + + >>> from scipy.integrate import solve_bvp + >>> res_a = solve_bvp(fun, bc, x, y_a) + >>> res_b = solve_bvp(fun, bc, x, y_b) + + Let's plot the two found solutions. We take an advantage of having the + solution in a spline form to produce a smooth plot. + + >>> x_plot = np.linspace(0, 1, 100) + >>> y_plot_a = res_a.sol(x_plot)[0] + >>> y_plot_b = res_b.sol(x_plot)[0] + >>> import matplotlib.pyplot as plt + >>> plt.plot(x_plot, y_plot_a, label='y_a') + >>> plt.plot(x_plot, y_plot_b, label='y_b') + >>> plt.legend() + >>> plt.xlabel("x") + >>> plt.ylabel("y") + >>> plt.show() + + We see that the two solutions have similar shape, but differ in scale + significantly. + + In the second example, we solve a simple Sturm-Liouville problem:: + + y'' + k**2 * y = 0 + y(0) = y(1) = 0 + + It is known that a non-trivial solution y = A * sin(k * x) is possible for + k = pi * n, where n is an integer. To establish the normalization constant + A = 1 we add a boundary condition:: + + y'(0) = k + + Again, we rewrite our equation as a first-order system and implement its + right-hand side evaluation:: + + y1' = y2 + y2' = -k**2 * y1 + + >>> def fun(x, y, p): + ... k = p[0] + ... return np.vstack((y[1], -k**2 * y[0])) + + Note that parameters p are passed as a vector (with one element in our + case). + + Implement the boundary conditions: + + >>> def bc(ya, yb, p): + ... k = p[0] + ... return np.array([ya[0], yb[0], ya[1] - k]) + + Set up the initial mesh and guess for y. We aim to find the solution for + k = 2 * pi, to achieve that we set values of y to approximately follow + sin(2 * pi * x): + + >>> x = np.linspace(0, 1, 5) + >>> y = np.zeros((2, x.size)) + >>> y[0, 1] = 1 + >>> y[0, 3] = -1 + + Run the solver with 6 as an initial guess for k. + + >>> sol = solve_bvp(fun, bc, x, y, p=[6]) + + We see that the found k is approximately correct: + + >>> sol.p[0] + 6.28329460046 + + And, finally, plot the solution to see the anticipated sinusoid: + + >>> x_plot = np.linspace(0, 1, 100) + >>> y_plot = sol.sol(x_plot)[0] + >>> plt.plot(x_plot, y_plot) + >>> plt.xlabel("x") + >>> plt.ylabel("y") + >>> plt.show() + """ + x = np.asarray(x, dtype=float) + if x.ndim != 1: + raise ValueError("`x` must be 1 dimensional.") + h = np.diff(x) + if np.any(h <= 0): + raise ValueError("`x` must be strictly increasing.") + a = x[0] + + y = np.asarray(y) + if np.issubdtype(y.dtype, np.complexfloating): + dtype = complex + else: + dtype = float + y = y.astype(dtype, copy=False) + + if y.ndim != 2: + raise ValueError("`y` must be 2 dimensional.") + if y.shape[1] != x.shape[0]: + raise ValueError(f"`y` is expected to have {x.shape[0]} columns, but actually " + f"has {y.shape[1]}.") + + if p is None: + p = np.array([]) + else: + p = np.asarray(p, dtype=dtype) + if p.ndim != 1: + raise ValueError("`p` must be 1 dimensional.") + + if tol < 100 * EPS: + warn(f"`tol` is too low, setting to {100 * EPS:.2e}", stacklevel=2) + tol = 100 * EPS + + if verbose not in [0, 1, 2]: + raise ValueError("`verbose` must be in [0, 1, 2].") + + n = y.shape[0] + k = p.shape[0] + + if S is not None: + S = np.asarray(S, dtype=dtype) + if S.shape != (n, n): + raise ValueError(f"`S` is expected to have shape {(n, n)}, " + f"but actually has {S.shape}") + + # Compute I - S^+ S to impose necessary boundary conditions. + B = np.identity(n) - np.dot(pinv(S), S) + + y[:, 0] = np.dot(B, y[:, 0]) + + # Compute (I - S)^+ to correct derivatives at x=a. + D = pinv(np.identity(n) - S) + else: + B = None + D = None + + if bc_tol is None: + bc_tol = tol + + # Maximum number of iterations + max_iteration = 10 + + fun_wrapped, bc_wrapped, fun_jac_wrapped, bc_jac_wrapped = wrap_functions( + fun, bc, fun_jac, bc_jac, k, a, S, D, dtype) + + f = fun_wrapped(x, y, p) + if f.shape != y.shape: + raise ValueError(f"`fun` return is expected to have shape {y.shape}, " + f"but actually has {f.shape}.") + + bc_res = bc_wrapped(y[:, 0], y[:, -1], p) + if bc_res.shape != (n + k,): + raise ValueError(f"`bc` return is expected to have shape {(n + k,)}, " + f"but actually has {bc_res.shape}.") + + status = 0 + iteration = 0 + if verbose == 2: + print_iteration_header() + + while True: + m = x.shape[0] + + col_fun, jac_sys = prepare_sys(n, m, k, fun_wrapped, bc_wrapped, + fun_jac_wrapped, bc_jac_wrapped, x, h) + y, p, singular = solve_newton(n, m, h, col_fun, bc_wrapped, jac_sys, + y, p, B, tol, bc_tol) + iteration += 1 + + col_res, y_middle, f, f_middle = collocation_fun(fun_wrapped, y, + p, x, h) + bc_res = bc_wrapped(y[:, 0], y[:, -1], p) + max_bc_res = np.max(abs(bc_res)) + + # This relation is not trivial, but can be verified. + r_middle = 1.5 * col_res / h + sol = create_spline(y, f, x, h) + rms_res = estimate_rms_residuals(fun_wrapped, sol, x, h, p, + r_middle, f_middle) + max_rms_res = np.max(rms_res) + + if singular: + status = 2 + break + + insert_1, = np.nonzero((rms_res > tol) & (rms_res < 100 * tol)) + insert_2, = np.nonzero(rms_res >= 100 * tol) + nodes_added = insert_1.shape[0] + 2 * insert_2.shape[0] + + if m + nodes_added > max_nodes: + status = 1 + if verbose == 2: + nodes_added = f"({nodes_added})" + print_iteration_progress(iteration, max_rms_res, max_bc_res, + m, nodes_added) + break + + if verbose == 2: + print_iteration_progress(iteration, max_rms_res, max_bc_res, m, + nodes_added) + + if nodes_added > 0: + x = modify_mesh(x, insert_1, insert_2) + h = np.diff(x) + y = sol(x) + elif max_bc_res <= bc_tol: + status = 0 + break + elif iteration >= max_iteration: + status = 3 + break + + if verbose > 0: + if status == 0: + print(f"Solved in {iteration} iterations, number of nodes {x.shape[0]}. \n" + f"Maximum relative residual: {max_rms_res:.2e} \n" + f"Maximum boundary residual: {max_bc_res:.2e}") + elif status == 1: + print(f"Number of nodes is exceeded after iteration {iteration}. \n" + f"Maximum relative residual: {max_rms_res:.2e} \n" + f"Maximum boundary residual: {max_bc_res:.2e}") + elif status == 2: + print("Singular Jacobian encountered when solving the collocation " + f"system on iteration {iteration}. \n" + f"Maximum relative residual: {max_rms_res:.2e} \n" + f"Maximum boundary residual: {max_bc_res:.2e}") + elif status == 3: + print("The solver was unable to satisfy boundary conditions " + f"tolerance on iteration {iteration}. \n" + f"Maximum relative residual: {max_rms_res:.2e} \n" + f"Maximum boundary residual: {max_bc_res:.2e}") + + if p.size == 0: + p = None + + return BVPResult(sol=sol, p=p, x=x, y=y, yp=f, rms_residuals=rms_res, + niter=iteration, status=status, + message=TERMINATION_MESSAGES[status], success=status == 0) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_cubature.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_cubature.py new file mode 100644 index 0000000000000000000000000000000000000000..3e6d8911d13eeaa2420ef65a12e9b4ba34400ca0 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_cubature.py @@ -0,0 +1,728 @@ +import math +import heapq +import itertools + +from dataclasses import dataclass, field +from types import ModuleType +from typing import Any, TypeAlias + +from scipy._lib._array_api import ( + array_namespace, + xp_size, + xp_copy, + xp_broadcast_promote +) +from scipy._lib._util import MapWrapper + +from scipy.integrate._rules import ( + ProductNestedFixed, + GaussKronrodQuadrature, + GenzMalikCubature, +) +from scipy.integrate._rules._base import _split_subregion + +__all__ = ['cubature'] + +Array: TypeAlias = Any # To be changed to an array-api-typing Protocol later + + +@dataclass +class CubatureRegion: + estimate: Array + error: Array + a: Array + b: Array + _xp: ModuleType = field(repr=False) + + def __lt__(self, other): + # Consider regions with higher error estimates as being "less than" regions with + # lower order estimates, so that regions with high error estimates are placed at + # the top of the heap. + + this_err = self._xp.max(self._xp.abs(self.error)) + other_err = self._xp.max(self._xp.abs(other.error)) + + return this_err > other_err + + +@dataclass +class CubatureResult: + estimate: Array + error: Array + status: str + regions: list[CubatureRegion] + subdivisions: int + atol: float + rtol: float + + +def cubature(f, a, b, *, rule="gk21", rtol=1e-8, atol=0, max_subdivisions=10000, + args=(), workers=1, points=None): + r""" + Adaptive cubature of multidimensional array-valued function. + + Given an arbitrary integration rule, this function returns an estimate of the + integral to the requested tolerance over the region defined by the arrays `a` and + `b` specifying the corners of a hypercube. + + Convergence is not guaranteed for all integrals. + + Parameters + ---------- + f : callable + Function to integrate. `f` must have the signature:: + + f(x : ndarray, *args) -> ndarray + + `f` should accept arrays ``x`` of shape:: + + (npoints, ndim) + + and output arrays of shape:: + + (npoints, output_dim_1, ..., output_dim_n) + + In this case, `cubature` will return arrays of shape:: + + (output_dim_1, ..., output_dim_n) + a, b : array_like + Lower and upper limits of integration as 1D arrays specifying the left and right + endpoints of the intervals being integrated over. Limits can be infinite. + rule : str, optional + Rule used to estimate the integral. If passing a string, the options are + "gauss-kronrod" (21 node), or "genz-malik" (degree 7). If a rule like + "gauss-kronrod" is specified for an ``n``-dim integrand, the corresponding + Cartesian product rule is used. "gk21", "gk15" are also supported for + compatibility with `quad_vec`. See Notes. + rtol, atol : float, optional + Relative and absolute tolerances. Iterations are performed until the error is + estimated to be less than ``atol + rtol * abs(est)``. Here `rtol` controls + relative accuracy (number of correct digits), while `atol` controls absolute + accuracy (number of correct decimal places). To achieve the desired `rtol`, set + `atol` to be smaller than the smallest value that can be expected from + ``rtol * abs(y)`` so that `rtol` dominates the allowable error. If `atol` is + larger than ``rtol * abs(y)`` the number of correct digits is not guaranteed. + Conversely, to achieve the desired `atol`, set `rtol` such that + ``rtol * abs(y)`` is always smaller than `atol`. Default values are 1e-8 for + `rtol` and 0 for `atol`. + max_subdivisions : int, optional + Upper bound on the number of subdivisions to perform. Default is 10,000. + args : tuple, optional + Additional positional args passed to `f`, if any. + workers : int or map-like callable, optional + If `workers` is an integer, part of the computation is done in parallel + subdivided to this many tasks (using :class:`python:multiprocessing.pool.Pool`). + Supply `-1` to use all cores available to the Process. Alternatively, supply a + map-like callable, such as :meth:`python:multiprocessing.pool.Pool.map` for + evaluating the population in parallel. This evaluation is carried out as + ``workers(func, iterable)``. + points : list of array_like, optional + List of points to avoid evaluating `f` at, under the condition that the rule + being used does not evaluate `f` on the boundary of a region (which is the + case for all Genz-Malik and Gauss-Kronrod rules). This can be useful if `f` has + a singularity at the specified point. This should be a list of array-likes where + each element has length ``ndim``. Default is empty. See Examples. + + Returns + ------- + res : object + Object containing the results of the estimation. It has the following + attributes: + + estimate : ndarray + Estimate of the value of the integral over the overall region specified. + error : ndarray + Estimate of the error of the approximation over the overall region + specified. + status : str + Whether the estimation was successful. Can be either: "converged", + "not_converged". + subdivisions : int + Number of subdivisions performed. + atol, rtol : float + Requested tolerances for the approximation. + regions: list of object + List of objects containing the estimates of the integral over smaller + regions of the domain. + + Each object in ``regions`` has the following attributes: + + a, b : ndarray + Points describing the corners of the region. If the original integral + contained infinite limits or was over a region described by `region`, + then `a` and `b` are in the transformed coordinates. + estimate : ndarray + Estimate of the value of the integral over this region. + error : ndarray + Estimate of the error of the approximation over this region. + + Notes + ----- + The algorithm uses a similar algorithm to `quad_vec`, which itself is based on the + implementation of QUADPACK's DQAG* algorithms, implementing global error control and + adaptive subdivision. + + The source of the nodes and weights used for Gauss-Kronrod quadrature can be found + in [1]_, and the algorithm for calculating the nodes and weights in Genz-Malik + cubature can be found in [2]_. + + The rules currently supported via the `rule` argument are: + + - ``"gauss-kronrod"``, 21-node Gauss-Kronrod + - ``"genz-malik"``, n-node Genz-Malik + + If using Gauss-Kronrod for an ``n``-dim integrand where ``n > 2``, then the + corresponding Cartesian product rule will be found by taking the Cartesian product + of the nodes in the 1D case. This means that the number of nodes scales + exponentially as ``21^n`` in the Gauss-Kronrod case, which may be problematic in a + moderate number of dimensions. + + Genz-Malik is typically less accurate than Gauss-Kronrod but has much fewer nodes, + so in this situation using "genz-malik" might be preferable. + + Infinite limits are handled with an appropriate variable transformation. Assuming + ``a = [a_1, ..., a_n]`` and ``b = [b_1, ..., b_n]``: + + If :math:`a_i = -\infty` and :math:`b_i = \infty`, the i-th integration variable + will use the transformation :math:`x = \frac{1-|t|}{t}` and :math:`t \in (-1, 1)`. + + If :math:`a_i \ne \pm\infty` and :math:`b_i = \infty`, the i-th integration variable + will use the transformation :math:`x = a_i + \frac{1-t}{t}` and + :math:`t \in (0, 1)`. + + If :math:`a_i = -\infty` and :math:`b_i \ne \pm\infty`, the i-th integration + variable will use the transformation :math:`x = b_i - \frac{1-t}{t}` and + :math:`t \in (0, 1)`. + + References + ---------- + .. [1] R. Piessens, E. de Doncker, Quadpack: A Subroutine Package for Automatic + Integration, files: dqk21.f, dqk15.f (1983). + + .. [2] A.C. Genz, A.A. Malik, Remarks on algorithm 006: An adaptive algorithm for + numerical integration over an N-dimensional rectangular region, Journal of + Computational and Applied Mathematics, Volume 6, Issue 4, 1980, Pages 295-302, + ISSN 0377-0427 + :doi:`10.1016/0771-050X(80)90039-X` + + Examples + -------- + **1D integral with vector output**: + + .. math:: + + \int^1_0 \mathbf f(x) \text dx + + Where ``f(x) = x^n`` and ``n = np.arange(10)`` is a vector. Since no rule is + specified, the default "gk21" is used, which corresponds to Gauss-Kronrod + integration with 21 nodes. + + >>> import numpy as np + >>> from scipy.integrate import cubature + >>> def f(x, n): + ... # Make sure x and n are broadcastable + ... return x[:, np.newaxis]**n[np.newaxis, :] + >>> res = cubature( + ... f, + ... a=[0], + ... b=[1], + ... args=(np.arange(10),), + ... ) + >>> res.estimate + array([1. , 0.5 , 0.33333333, 0.25 , 0.2 , + 0.16666667, 0.14285714, 0.125 , 0.11111111, 0.1 ]) + + **7D integral with arbitrary-shaped array output**:: + + f(x) = cos(2*pi*r + alphas @ x) + + for some ``r`` and ``alphas``, and the integral is performed over the unit + hybercube, :math:`[0, 1]^7`. Since the integral is in a moderate number of + dimensions, "genz-malik" is used rather than the default "gauss-kronrod" to + avoid constructing a product rule with :math:`21^7 \approx 2 \times 10^9` nodes. + + >>> import numpy as np + >>> from scipy.integrate import cubature + >>> def f(x, r, alphas): + ... # f(x) = cos(2*pi*r + alphas @ x) + ... # Need to allow r and alphas to be arbitrary shape + ... npoints, ndim = x.shape[0], x.shape[-1] + ... alphas = alphas[np.newaxis, ...] + ... x = x.reshape(npoints, *([1]*(len(alphas.shape) - 1)), ndim) + ... return np.cos(2*np.pi*r + np.sum(alphas * x, axis=-1)) + >>> rng = np.random.default_rng() + >>> r, alphas = rng.random((2, 3)), rng.random((2, 3, 7)) + >>> res = cubature( + ... f=f, + ... a=np.array([0, 0, 0, 0, 0, 0, 0]), + ... b=np.array([1, 1, 1, 1, 1, 1, 1]), + ... rtol=1e-5, + ... rule="genz-malik", + ... args=(r, alphas), + ... ) + >>> res.estimate + array([[-0.79812452, 0.35246913, -0.52273628], + [ 0.88392779, 0.59139899, 0.41895111]]) + + **Parallel computation with** `workers`: + + >>> from concurrent.futures import ThreadPoolExecutor + >>> with ThreadPoolExecutor() as executor: + ... res = cubature( + ... f=f, + ... a=np.array([0, 0, 0, 0, 0, 0, 0]), + ... b=np.array([1, 1, 1, 1, 1, 1, 1]), + ... rtol=1e-5, + ... rule="genz-malik", + ... args=(r, alphas), + ... workers=executor.map, + ... ) + >>> res.estimate + array([[-0.79812452, 0.35246913, -0.52273628], + [ 0.88392779, 0.59139899, 0.41895111]]) + + **2D integral with infinite limits**: + + .. math:: + + \int^{ \infty }_{ -\infty } + \int^{ \infty }_{ -\infty } + e^{-x^2-y^2} + \text dy + \text dx + + >>> def gaussian(x): + ... return np.exp(-np.sum(x**2, axis=-1)) + >>> res = cubature(gaussian, [-np.inf, -np.inf], [np.inf, np.inf]) + >>> res.estimate + 3.1415926 + + **1D integral with singularities avoided using** `points`: + + .. math:: + + \int^{ 1 }_{ -1 } + \frac{\sin(x)}{x} + \text dx + + It is necessary to use the `points` parameter to avoid evaluating `f` at the origin. + + >>> def sinc(x): + ... return np.sin(x)/x + >>> res = cubature(sinc, [-1], [1], points=[[0]]) + >>> res.estimate + 1.8921661 + """ + + # It is also possible to use a custom rule, but this is not yet part of the public + # API. An example of this can be found in the class scipy.integrate._rules.Rule. + + xp = array_namespace(a, b) + max_subdivisions = float("inf") if max_subdivisions is None else max_subdivisions + points = [] if points is None else points + + # Convert a and b to arrays and convert each point in points to an array, promoting + # each to a common floating dtype. + a, b, *points = xp_broadcast_promote(a, b, *points, force_floating=True) + result_dtype = a.dtype + + if xp_size(a) == 0 or xp_size(b) == 0: + raise ValueError("`a` and `b` must be nonempty") + + if a.ndim != 1 or b.ndim != 1: + raise ValueError("`a` and `b` must be 1D arrays") + + # If the rule is a string, convert to a corresponding product rule + if isinstance(rule, str): + ndim = xp_size(a) + + if rule == "genz-malik": + rule = GenzMalikCubature(ndim, xp=xp) + else: + quadratues = { + "gauss-kronrod": GaussKronrodQuadrature(21, xp=xp), + + # Also allow names quad_vec uses: + "gk21": GaussKronrodQuadrature(21, xp=xp), + "gk15": GaussKronrodQuadrature(15, xp=xp), + } + + base_rule = quadratues.get(rule) + + if base_rule is None: + raise ValueError(f"unknown rule {rule}") + + rule = ProductNestedFixed([base_rule] * ndim) + + # If any of limits are the wrong way around (a > b), flip them and keep track of + # the sign. + sign = (-1) ** xp.sum(xp.astype(a > b, xp.int8), dtype=result_dtype) + + a_flipped = xp.min(xp.stack([a, b]), axis=0) + b_flipped = xp.max(xp.stack([a, b]), axis=0) + + a, b = a_flipped, b_flipped + + # If any of the limits are infinite, apply a transformation + if xp.any(xp.isinf(a)) or xp.any(xp.isinf(b)): + f = _InfiniteLimitsTransform(f, a, b, xp=xp) + a, b = f.transformed_limits + + # Map points from the original coordinates to the new transformed coordinates. + # + # `points` is a list of arrays of shape (ndim,), but transformations are applied + # to arrays of shape (npoints, ndim). + # + # It is not possible to combine all the points into one array and then apply + # f.inv to all of them at once since `points` needs to remain iterable. + # Instead, each point is reshaped to an array of shape (1, ndim), `f.inv` is + # applied, and then each is reshaped back to (ndim,). + points = [xp.reshape(point, (1, -1)) for point in points] + points = [f.inv(point) for point in points] + points = [xp.reshape(point, (-1,)) for point in points] + + # Include any problematic points introduced by the transformation + points.extend(f.points) + + # If any problematic points are specified, divide the initial region so that these + # points lie on the edge of a subregion. + # + # This means ``f`` won't be evaluated there if the rule being used has no evaluation + # points on the boundary. + if len(points) == 0: + initial_regions = [(a, b)] + else: + initial_regions = _split_region_at_points(a, b, points, xp) + + regions = [] + est = 0.0 + err = 0.0 + + for a_k, b_k in initial_regions: + est_k = rule.estimate(f, a_k, b_k, args) + err_k = rule.estimate_error(f, a_k, b_k, args) + regions.append(CubatureRegion(est_k, err_k, a_k, b_k, xp)) + + est += est_k + err += err_k + + subdivisions = 0 + success = True + + with MapWrapper(workers) as mapwrapper: + while xp.any(err > atol + rtol * xp.abs(est)): + # region_k is the region with highest estimated error + region_k = heapq.heappop(regions) + + est_k = region_k.estimate + err_k = region_k.error + + a_k, b_k = region_k.a, region_k.b + + # Subtract the estimate of the integral and its error over this region from + # the current global estimates, since these will be refined in the loop over + # all subregions. + est -= est_k + err -= err_k + + # Find all 2^ndim subregions formed by splitting region_k along each axis, + # e.g. for 1D integrals this splits an estimate over an interval into an + # estimate over two subintervals, for 3D integrals this splits an estimate + # over a cube into 8 subcubes. + # + # For each of the new subregions, calculate an estimate for the integral and + # the error there, and push these regions onto the heap for potential + # further subdividing. + + executor_args = zip( + itertools.repeat(f), + itertools.repeat(rule), + itertools.repeat(args), + _split_subregion(a_k, b_k, xp), + ) + + for subdivision_result in mapwrapper(_process_subregion, executor_args): + a_k_sub, b_k_sub, est_sub, err_sub = subdivision_result + + est += est_sub + err += err_sub + + new_region = CubatureRegion(est_sub, err_sub, a_k_sub, b_k_sub, xp) + + heapq.heappush(regions, new_region) + + subdivisions += 1 + + if subdivisions >= max_subdivisions: + success = False + break + + status = "converged" if success else "not_converged" + + # Apply sign change to handle any limits which were initially flipped. + est = sign * est + + return CubatureResult( + estimate=est, + error=err, + status=status, + subdivisions=subdivisions, + regions=regions, + atol=atol, + rtol=rtol, + ) + + +def _process_subregion(data): + f, rule, args, coord = data + a_k_sub, b_k_sub = coord + + est_sub = rule.estimate(f, a_k_sub, b_k_sub, args) + err_sub = rule.estimate_error(f, a_k_sub, b_k_sub, args) + + return a_k_sub, b_k_sub, est_sub, err_sub + + +def _is_strictly_in_region(a, b, point, xp): + if xp.all(point == a) or xp.all(point == b): + return False + + return xp.all(a <= point) and xp.all(point <= b) + + +def _split_region_at_points(a, b, points, xp): + """ + Given the integration limits `a` and `b` describing a rectangular region and a list + of `points`, find the list of ``[(a_1, b_1), ..., (a_l, b_l)]`` which breaks up the + initial region into smaller subregion such that no `points` lie strictly inside + any of the subregions. + """ + + regions = [(a, b)] + + for point in points: + if xp.any(xp.isinf(point)): + # If a point is specified at infinity, ignore. + # + # This case occurs when points are given by the user to avoid, but after + # applying a transformation, they are removed. + continue + + new_subregions = [] + + for a_k, b_k in regions: + if _is_strictly_in_region(a_k, b_k, point, xp): + subregions = _split_subregion(a_k, b_k, xp, point) + + for left, right in subregions: + # Skip any zero-width regions. + if xp.any(left == right): + continue + else: + new_subregions.append((left, right)) + + new_subregions.extend(subregions) + + else: + new_subregions.append((a_k, b_k)) + + regions = new_subregions + + return regions + + +class _VariableTransform: + """ + A transformation that can be applied to an integral. + """ + + @property + def transformed_limits(self): + """ + New limits of integration after applying the transformation. + """ + + raise NotImplementedError + + @property + def points(self): + """ + Any problematic points introduced by the transformation. + + These should be specified as points where ``_VariableTransform(f)(self, point)`` + would be problematic. + + For example, if the transformation ``x = 1/((1-t)(1+t))`` is applied to a + univariate integral, then points should return ``[ [1], [-1] ]``. + """ + + return [] + + def inv(self, x): + """ + Map points ``x`` to ``t`` such that if ``f`` is the original function and ``g`` + is the function after the transformation is applied, then:: + + f(x) = g(self.inv(x)) + """ + + raise NotImplementedError + + def __call__(self, t, *args, **kwargs): + """ + Apply the transformation to ``f`` and multiply by the Jacobian determinant. + This should be the new integrand after the transformation has been applied so + that the following is satisfied:: + + f_transformed = _VariableTransform(f) + + cubature(f, a, b) == cubature( + f_transformed, + *f_transformed.transformed_limits(a, b), + ) + """ + + raise NotImplementedError + + +class _InfiniteLimitsTransform(_VariableTransform): + r""" + Transformation for handling infinite limits. + + Assuming ``a = [a_1, ..., a_n]`` and ``b = [b_1, ..., b_n]``: + + If :math:`a_i = -\infty` and :math:`b_i = \infty`, the i-th integration variable + will use the transformation :math:`x = \frac{1-|t|}{t}` and :math:`t \in (-1, 1)`. + + If :math:`a_i \ne \pm\infty` and :math:`b_i = \infty`, the i-th integration variable + will use the transformation :math:`x = a_i + \frac{1-t}{t}` and + :math:`t \in (0, 1)`. + + If :math:`a_i = -\infty` and :math:`b_i \ne \pm\infty`, the i-th integration + variable will use the transformation :math:`x = b_i - \frac{1-t}{t}` and + :math:`t \in (0, 1)`. + """ + + def __init__(self, f, a, b, xp): + self._xp = xp + + self._f = f + self._orig_a = a + self._orig_b = b + + # (-oo, oo) will be mapped to (-1, 1). + self._double_inf_pos = (a == -math.inf) & (b == math.inf) + + # (start, oo) will be mapped to (0, 1). + start_inf_mask = (a != -math.inf) & (b == math.inf) + + # (-oo, end) will be mapped to (0, 1). + inf_end_mask = (a == -math.inf) & (b != math.inf) + + # This is handled by making the transformation t = -x and reducing it to + # the other semi-infinite case. + self._semi_inf_pos = start_inf_mask | inf_end_mask + + # Since we flip the limits, we don't need to separately multiply the + # integrand by -1. + self._orig_a[inf_end_mask] = -b[inf_end_mask] + self._orig_b[inf_end_mask] = -a[inf_end_mask] + + self._num_inf = self._xp.sum( + self._xp.astype(self._double_inf_pos | self._semi_inf_pos, self._xp.int64), + ).__int__() + + @property + def transformed_limits(self): + a = xp_copy(self._orig_a) + b = xp_copy(self._orig_b) + + a[self._double_inf_pos] = -1 + b[self._double_inf_pos] = 1 + + a[self._semi_inf_pos] = 0 + b[self._semi_inf_pos] = 1 + + return a, b + + @property + def points(self): + # If there are infinite limits, then the origin becomes a problematic point + # due to a division by zero there. + + # If the function using this class only wraps f when a and b contain infinite + # limits, this condition will always be met (as is the case with cubature). + # + # If a and b do not contain infinite limits but f is still wrapped with this + # class, then without this condition the initial region of integration will + # be split around the origin unnecessarily. + if self._num_inf != 0: + return [self._xp.zeros(self._orig_a.shape)] + else: + return [] + + def inv(self, x): + t = xp_copy(x) + npoints = x.shape[0] + + double_inf_mask = self._xp.tile( + self._double_inf_pos[self._xp.newaxis, :], + (npoints, 1), + ) + + semi_inf_mask = self._xp.tile( + self._semi_inf_pos[self._xp.newaxis, :], + (npoints, 1), + ) + + # If any components of x are 0, then this component will be mapped to infinity + # under the transformation used for doubly-infinite limits. + # + # Handle the zero values and non-zero values separately to avoid division by + # zero. + zero_mask = x[double_inf_mask] == 0 + non_zero_mask = double_inf_mask & ~zero_mask + t[zero_mask] = math.inf + t[non_zero_mask] = 1/(x[non_zero_mask] + self._xp.sign(x[non_zero_mask])) + + start = self._xp.tile(self._orig_a[self._semi_inf_pos], (npoints,)) + t[semi_inf_mask] = 1/(x[semi_inf_mask] - start + 1) + + return t + + def __call__(self, t, *args, **kwargs): + x = xp_copy(t) + npoints = t.shape[0] + + double_inf_mask = self._xp.tile( + self._double_inf_pos[self._xp.newaxis, :], + (npoints, 1), + ) + + semi_inf_mask = self._xp.tile( + self._semi_inf_pos[self._xp.newaxis, :], + (npoints, 1), + ) + + # For (-oo, oo) -> (-1, 1), use the transformation x = (1-|t|)/t. + x[double_inf_mask] = ( + (1 - self._xp.abs(t[double_inf_mask])) / t[double_inf_mask] + ) + + start = self._xp.tile(self._orig_a[self._semi_inf_pos], (npoints,)) + + # For (start, oo) -> (0, 1), use the transformation x = start + (1-t)/t. + x[semi_inf_mask] = start + (1 - t[semi_inf_mask]) / t[semi_inf_mask] + + jacobian_det = 1/self._xp.prod( + self._xp.reshape( + t[semi_inf_mask | double_inf_mask]**2, + (-1, self._num_inf), + ), + axis=-1, + ) + + f_x = self._f(x, *args, **kwargs) + jacobian_det = self._xp.reshape(jacobian_det, (-1, *([1]*(len(f_x.shape) - 1)))) + + return f_x * jacobian_det diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..f3c8aaa36588651ae5e48b58fbb1d443bc71fc77 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/__init__.py @@ -0,0 +1,8 @@ +"""Suite of ODE solvers implemented in Python.""" +from .ivp import solve_ivp +from .rk import RK23, RK45, DOP853 +from .radau import Radau +from .bdf import BDF +from .lsoda import LSODA +from .common import OdeSolution +from .base import DenseOutput, OdeSolver diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/base.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/base.py new file mode 100644 index 0000000000000000000000000000000000000000..46db9a69dfb3e7aee5c150ac6795234cd455dfe5 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/base.py @@ -0,0 +1,290 @@ +import numpy as np + + +def check_arguments(fun, y0, support_complex): + """Helper function for checking arguments common to all solvers.""" + y0 = np.asarray(y0) + if np.issubdtype(y0.dtype, np.complexfloating): + if not support_complex: + raise ValueError("`y0` is complex, but the chosen solver does " + "not support integration in a complex domain.") + dtype = complex + else: + dtype = float + y0 = y0.astype(dtype, copy=False) + + if y0.ndim != 1: + raise ValueError("`y0` must be 1-dimensional.") + + if not np.isfinite(y0).all(): + raise ValueError("All components of the initial state `y0` must be finite.") + + def fun_wrapped(t, y): + return np.asarray(fun(t, y), dtype=dtype) + + return fun_wrapped, y0 + + +class OdeSolver: + """Base class for ODE solvers. + + In order to implement a new solver you need to follow the guidelines: + + 1. A constructor must accept parameters presented in the base class + (listed below) along with any other parameters specific to a solver. + 2. A constructor must accept arbitrary extraneous arguments + ``**extraneous``, but warn that these arguments are irrelevant + using `common.warn_extraneous` function. Do not pass these + arguments to the base class. + 3. A solver must implement a private method `_step_impl(self)` which + propagates a solver one step further. It must return tuple + ``(success, message)``, where ``success`` is a boolean indicating + whether a step was successful, and ``message`` is a string + containing description of a failure if a step failed or None + otherwise. + 4. A solver must implement a private method `_dense_output_impl(self)`, + which returns a `DenseOutput` object covering the last successful + step. + 5. A solver must have attributes listed below in Attributes section. + Note that ``t_old`` and ``step_size`` are updated automatically. + 6. Use `fun(self, t, y)` method for the system rhs evaluation, this + way the number of function evaluations (`nfev`) will be tracked + automatically. + 7. For convenience, a base class provides `fun_single(self, t, y)` and + `fun_vectorized(self, t, y)` for evaluating the rhs in + non-vectorized and vectorized fashions respectively (regardless of + how `fun` from the constructor is implemented). These calls don't + increment `nfev`. + 8. If a solver uses a Jacobian matrix and LU decompositions, it should + track the number of Jacobian evaluations (`njev`) and the number of + LU decompositions (`nlu`). + 9. By convention, the function evaluations used to compute a finite + difference approximation of the Jacobian should not be counted in + `nfev`, thus use `fun_single(self, t, y)` or + `fun_vectorized(self, t, y)` when computing a finite difference + approximation of the Jacobian. + + Parameters + ---------- + fun : callable + Right-hand side of the system: the time derivative of the state ``y`` + at time ``t``. The calling signature is ``fun(t, y)``, where ``t`` is a + scalar and ``y`` is an ndarray with ``len(y) = len(y0)``. ``fun`` must + return an array of the same shape as ``y``. See `vectorized` for more + information. + t0 : float + Initial time. + y0 : array_like, shape (n,) + Initial state. + t_bound : float + Boundary time --- the integration won't continue beyond it. It also + determines the direction of the integration. + vectorized : bool + Whether `fun` can be called in a vectorized fashion. Default is False. + + If ``vectorized`` is False, `fun` will always be called with ``y`` of + shape ``(n,)``, where ``n = len(y0)``. + + If ``vectorized`` is True, `fun` may be called with ``y`` of shape + ``(n, k)``, where ``k`` is an integer. In this case, `fun` must behave + such that ``fun(t, y)[:, i] == fun(t, y[:, i])`` (i.e. each column of + the returned array is the time derivative of the state corresponding + with a column of ``y``). + + Setting ``vectorized=True`` allows for faster finite difference + approximation of the Jacobian by methods 'Radau' and 'BDF', but + will result in slower execution for other methods. It can also + result in slower overall execution for 'Radau' and 'BDF' in some + circumstances (e.g. small ``len(y0)``). + support_complex : bool, optional + Whether integration in a complex domain should be supported. + Generally determined by a derived solver class capabilities. + Default is False. + + Attributes + ---------- + n : int + Number of equations. + status : string + Current status of the solver: 'running', 'finished' or 'failed'. + t_bound : float + Boundary time. + direction : float + Integration direction: +1 or -1. + t : float + Current time. + y : ndarray + Current state. + t_old : float + Previous time. None if no steps were made yet. + step_size : float + Size of the last successful step. None if no steps were made yet. + nfev : int + Number of the system's rhs evaluations. + njev : int + Number of the Jacobian evaluations. + nlu : int + Number of LU decompositions. + """ + TOO_SMALL_STEP = "Required step size is less than spacing between numbers." + + def __init__(self, fun, t0, y0, t_bound, vectorized, + support_complex=False): + self.t_old = None + self.t = t0 + self._fun, self.y = check_arguments(fun, y0, support_complex) + self.t_bound = t_bound + self.vectorized = vectorized + + if vectorized: + def fun_single(t, y): + return self._fun(t, y[:, None]).ravel() + fun_vectorized = self._fun + else: + fun_single = self._fun + + def fun_vectorized(t, y): + f = np.empty_like(y) + for i, yi in enumerate(y.T): + f[:, i] = self._fun(t, yi) + return f + + def fun(t, y): + self.nfev += 1 + return self.fun_single(t, y) + + self.fun = fun + self.fun_single = fun_single + self.fun_vectorized = fun_vectorized + + self.direction = np.sign(t_bound - t0) if t_bound != t0 else 1 + self.n = self.y.size + self.status = 'running' + + self.nfev = 0 + self.njev = 0 + self.nlu = 0 + + @property + def step_size(self): + if self.t_old is None: + return None + else: + return np.abs(self.t - self.t_old) + + def step(self): + """Perform one integration step. + + Returns + ------- + message : string or None + Report from the solver. Typically a reason for a failure if + `self.status` is 'failed' after the step was taken or None + otherwise. + """ + if self.status != 'running': + raise RuntimeError("Attempt to step on a failed or finished " + "solver.") + + if self.n == 0 or self.t == self.t_bound: + # Handle corner cases of empty solver or no integration. + self.t_old = self.t + self.t = self.t_bound + message = None + self.status = 'finished' + else: + t = self.t + success, message = self._step_impl() + + if not success: + self.status = 'failed' + else: + self.t_old = t + if self.direction * (self.t - self.t_bound) >= 0: + self.status = 'finished' + + return message + + def dense_output(self): + """Compute a local interpolant over the last successful step. + + Returns + ------- + sol : `DenseOutput` + Local interpolant over the last successful step. + """ + if self.t_old is None: + raise RuntimeError("Dense output is available after a successful " + "step was made.") + + if self.n == 0 or self.t == self.t_old: + # Handle corner cases of empty solver and no integration. + return ConstantDenseOutput(self.t_old, self.t, self.y) + else: + return self._dense_output_impl() + + def _step_impl(self): + raise NotImplementedError + + def _dense_output_impl(self): + raise NotImplementedError + + +class DenseOutput: + """Base class for local interpolant over step made by an ODE solver. + + It interpolates between `t_min` and `t_max` (see Attributes below). + Evaluation outside this interval is not forbidden, but the accuracy is not + guaranteed. + + Attributes + ---------- + t_min, t_max : float + Time range of the interpolation. + """ + def __init__(self, t_old, t): + self.t_old = t_old + self.t = t + self.t_min = min(t, t_old) + self.t_max = max(t, t_old) + + def __call__(self, t): + """Evaluate the interpolant. + + Parameters + ---------- + t : float or array_like with shape (n_points,) + Points to evaluate the solution at. + + Returns + ------- + y : ndarray, shape (n,) or (n, n_points) + Computed values. Shape depends on whether `t` was a scalar or a + 1-D array. + """ + t = np.asarray(t) + if t.ndim > 1: + raise ValueError("`t` must be a float or a 1-D array.") + return self._call_impl(t) + + def _call_impl(self, t): + raise NotImplementedError + + +class ConstantDenseOutput(DenseOutput): + """Constant value interpolator. + + This class used for degenerate integration cases: equal integration limits + or a system with 0 equations. + """ + def __init__(self, t_old, t, value): + super().__init__(t_old, t) + self.value = value + + def _call_impl(self, t): + if t.ndim == 0: + return self.value + else: + ret = np.empty((self.value.shape[0], t.shape[0])) + ret[:] = self.value[:, None] + return ret diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/bdf.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/bdf.py new file mode 100644 index 0000000000000000000000000000000000000000..33b47a642b976e623edc9047f6465e328095dcd2 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/bdf.py @@ -0,0 +1,478 @@ +import numpy as np +from scipy.linalg import lu_factor, lu_solve +from scipy.sparse import issparse, csc_matrix, eye +from scipy.sparse.linalg import splu +from scipy.optimize._numdiff import group_columns +from .common import (validate_max_step, validate_tol, select_initial_step, + norm, EPS, num_jac, validate_first_step, + warn_extraneous) +from .base import OdeSolver, DenseOutput + + +MAX_ORDER = 5 +NEWTON_MAXITER = 4 +MIN_FACTOR = 0.2 +MAX_FACTOR = 10 + + +def compute_R(order, factor): + """Compute the matrix for changing the differences array.""" + I = np.arange(1, order + 1)[:, None] + J = np.arange(1, order + 1) + M = np.zeros((order + 1, order + 1)) + M[1:, 1:] = (I - 1 - factor * J) / I + M[0] = 1 + return np.cumprod(M, axis=0) + + +def change_D(D, order, factor): + """Change differences array in-place when step size is changed.""" + R = compute_R(order, factor) + U = compute_R(order, 1) + RU = R.dot(U) + D[:order + 1] = np.dot(RU.T, D[:order + 1]) + + +def solve_bdf_system(fun, t_new, y_predict, c, psi, LU, solve_lu, scale, tol): + """Solve the algebraic system resulting from BDF method.""" + d = 0 + y = y_predict.copy() + dy_norm_old = None + converged = False + for k in range(NEWTON_MAXITER): + f = fun(t_new, y) + if not np.all(np.isfinite(f)): + break + + dy = solve_lu(LU, c * f - psi - d) + dy_norm = norm(dy / scale) + + if dy_norm_old is None: + rate = None + else: + rate = dy_norm / dy_norm_old + + if (rate is not None and (rate >= 1 or + rate ** (NEWTON_MAXITER - k) / (1 - rate) * dy_norm > tol)): + break + + y += dy + d += dy + + if (dy_norm == 0 or + rate is not None and rate / (1 - rate) * dy_norm < tol): + converged = True + break + + dy_norm_old = dy_norm + + return converged, k + 1, y, d + + +class BDF(OdeSolver): + """Implicit method based on backward-differentiation formulas. + + This is a variable order method with the order varying automatically from + 1 to 5. The general framework of the BDF algorithm is described in [1]_. + This class implements a quasi-constant step size as explained in [2]_. + The error estimation strategy for the constant-step BDF is derived in [3]_. + An accuracy enhancement using modified formulas (NDF) [2]_ is also implemented. + + Can be applied in the complex domain. + + Parameters + ---------- + fun : callable + Right-hand side of the system: the time derivative of the state ``y`` + at time ``t``. The calling signature is ``fun(t, y)``, where ``t`` is a + scalar and ``y`` is an ndarray with ``len(y) = len(y0)``. ``fun`` must + return an array of the same shape as ``y``. See `vectorized` for more + information. + t0 : float + Initial time. + y0 : array_like, shape (n,) + Initial state. + t_bound : float + Boundary time - the integration won't continue beyond it. It also + determines the direction of the integration. + first_step : float or None, optional + Initial step size. Default is ``None`` which means that the algorithm + should choose. + max_step : float, optional + Maximum allowed step size. Default is np.inf, i.e., the step size is not + bounded and determined solely by the solver. + rtol, atol : float and array_like, optional + Relative and absolute tolerances. The solver keeps the local error + estimates less than ``atol + rtol * abs(y)``. Here `rtol` controls a + relative accuracy (number of correct digits), while `atol` controls + absolute accuracy (number of correct decimal places). To achieve the + desired `rtol`, set `atol` to be smaller than the smallest value that + can be expected from ``rtol * abs(y)`` so that `rtol` dominates the + allowable error. If `atol` is larger than ``rtol * abs(y)`` the + number of correct digits is not guaranteed. Conversely, to achieve the + desired `atol` set `rtol` such that ``rtol * abs(y)`` is always smaller + than `atol`. If components of y have different scales, it might be + beneficial to set different `atol` values for different components by + passing array_like with shape (n,) for `atol`. Default values are + 1e-3 for `rtol` and 1e-6 for `atol`. + jac : {None, array_like, sparse_matrix, callable}, optional + Jacobian matrix of the right-hand side of the system with respect to y, + required by this method. The Jacobian matrix has shape (n, n) and its + element (i, j) is equal to ``d f_i / d y_j``. + There are three ways to define the Jacobian: + + * If array_like or sparse_matrix, the Jacobian is assumed to + be constant. + * If callable, the Jacobian is assumed to depend on both + t and y; it will be called as ``jac(t, y)`` as necessary. + For the 'Radau' and 'BDF' methods, the return value might be a + sparse matrix. + * If None (default), the Jacobian will be approximated by + finite differences. + + It is generally recommended to provide the Jacobian rather than + relying on a finite-difference approximation. + jac_sparsity : {None, array_like, sparse matrix}, optional + Defines a sparsity structure of the Jacobian matrix for a + finite-difference approximation. Its shape must be (n, n). This argument + is ignored if `jac` is not `None`. If the Jacobian has only few non-zero + elements in *each* row, providing the sparsity structure will greatly + speed up the computations [4]_. A zero entry means that a corresponding + element in the Jacobian is always zero. If None (default), the Jacobian + is assumed to be dense. + vectorized : bool, optional + Whether `fun` can be called in a vectorized fashion. Default is False. + + If ``vectorized`` is False, `fun` will always be called with ``y`` of + shape ``(n,)``, where ``n = len(y0)``. + + If ``vectorized`` is True, `fun` may be called with ``y`` of shape + ``(n, k)``, where ``k`` is an integer. In this case, `fun` must behave + such that ``fun(t, y)[:, i] == fun(t, y[:, i])`` (i.e. each column of + the returned array is the time derivative of the state corresponding + with a column of ``y``). + + Setting ``vectorized=True`` allows for faster finite difference + approximation of the Jacobian by this method, but may result in slower + execution overall in some circumstances (e.g. small ``len(y0)``). + + Attributes + ---------- + n : int + Number of equations. + status : string + Current status of the solver: 'running', 'finished' or 'failed'. + t_bound : float + Boundary time. + direction : float + Integration direction: +1 or -1. + t : float + Current time. + y : ndarray + Current state. + t_old : float + Previous time. None if no steps were made yet. + step_size : float + Size of the last successful step. None if no steps were made yet. + nfev : int + Number of evaluations of the right-hand side. + njev : int + Number of evaluations of the Jacobian. + nlu : int + Number of LU decompositions. + + References + ---------- + .. [1] G. D. Byrne, A. C. Hindmarsh, "A Polyalgorithm for the Numerical + Solution of Ordinary Differential Equations", ACM Transactions on + Mathematical Software, Vol. 1, No. 1, pp. 71-96, March 1975. + .. [2] L. F. Shampine, M. W. Reichelt, "THE MATLAB ODE SUITE", SIAM J. SCI. + COMPUTE., Vol. 18, No. 1, pp. 1-22, January 1997. + .. [3] E. Hairer, G. Wanner, "Solving Ordinary Differential Equations I: + Nonstiff Problems", Sec. III.2. + .. [4] A. Curtis, M. J. D. Powell, and J. Reid, "On the estimation of + sparse Jacobian matrices", Journal of the Institute of Mathematics + and its Applications, 13, pp. 117-120, 1974. + """ + def __init__(self, fun, t0, y0, t_bound, max_step=np.inf, + rtol=1e-3, atol=1e-6, jac=None, jac_sparsity=None, + vectorized=False, first_step=None, **extraneous): + warn_extraneous(extraneous) + super().__init__(fun, t0, y0, t_bound, vectorized, + support_complex=True) + self.max_step = validate_max_step(max_step) + self.rtol, self.atol = validate_tol(rtol, atol, self.n) + f = self.fun(self.t, self.y) + if first_step is None: + self.h_abs = select_initial_step(self.fun, self.t, self.y, + t_bound, max_step, f, + self.direction, 1, + self.rtol, self.atol) + else: + self.h_abs = validate_first_step(first_step, t0, t_bound) + self.h_abs_old = None + self.error_norm_old = None + + self.newton_tol = max(10 * EPS / rtol, min(0.03, rtol ** 0.5)) + + self.jac_factor = None + self.jac, self.J = self._validate_jac(jac, jac_sparsity) + if issparse(self.J): + def lu(A): + self.nlu += 1 + return splu(A) + + def solve_lu(LU, b): + return LU.solve(b) + + I = eye(self.n, format='csc', dtype=self.y.dtype) + else: + def lu(A): + self.nlu += 1 + return lu_factor(A, overwrite_a=True) + + def solve_lu(LU, b): + return lu_solve(LU, b, overwrite_b=True) + + I = np.identity(self.n, dtype=self.y.dtype) + + self.lu = lu + self.solve_lu = solve_lu + self.I = I + + kappa = np.array([0, -0.1850, -1/9, -0.0823, -0.0415, 0]) + self.gamma = np.hstack((0, np.cumsum(1 / np.arange(1, MAX_ORDER + 1)))) + self.alpha = (1 - kappa) * self.gamma + self.error_const = kappa * self.gamma + 1 / np.arange(1, MAX_ORDER + 2) + + D = np.empty((MAX_ORDER + 3, self.n), dtype=self.y.dtype) + D[0] = self.y + D[1] = f * self.h_abs * self.direction + self.D = D + + self.order = 1 + self.n_equal_steps = 0 + self.LU = None + + def _validate_jac(self, jac, sparsity): + t0 = self.t + y0 = self.y + + if jac is None: + if sparsity is not None: + if issparse(sparsity): + sparsity = csc_matrix(sparsity) + groups = group_columns(sparsity) + sparsity = (sparsity, groups) + + def jac_wrapped(t, y): + self.njev += 1 + f = self.fun_single(t, y) + J, self.jac_factor = num_jac(self.fun_vectorized, t, y, f, + self.atol, self.jac_factor, + sparsity) + return J + J = jac_wrapped(t0, y0) + elif callable(jac): + J = jac(t0, y0) + self.njev += 1 + if issparse(J): + J = csc_matrix(J, dtype=y0.dtype) + + def jac_wrapped(t, y): + self.njev += 1 + return csc_matrix(jac(t, y), dtype=y0.dtype) + else: + J = np.asarray(J, dtype=y0.dtype) + + def jac_wrapped(t, y): + self.njev += 1 + return np.asarray(jac(t, y), dtype=y0.dtype) + + if J.shape != (self.n, self.n): + raise ValueError(f"`jac` is expected to have shape {(self.n, self.n)}," + f" but actually has {J.shape}.") + else: + if issparse(jac): + J = csc_matrix(jac, dtype=y0.dtype) + else: + J = np.asarray(jac, dtype=y0.dtype) + + if J.shape != (self.n, self.n): + raise ValueError(f"`jac` is expected to have shape {(self.n, self.n)}," + f" but actually has {J.shape}.") + jac_wrapped = None + + return jac_wrapped, J + + def _step_impl(self): + t = self.t + D = self.D + + max_step = self.max_step + min_step = 10 * np.abs(np.nextafter(t, self.direction * np.inf) - t) + if self.h_abs > max_step: + h_abs = max_step + change_D(D, self.order, max_step / self.h_abs) + self.n_equal_steps = 0 + elif self.h_abs < min_step: + h_abs = min_step + change_D(D, self.order, min_step / self.h_abs) + self.n_equal_steps = 0 + else: + h_abs = self.h_abs + + atol = self.atol + rtol = self.rtol + order = self.order + + alpha = self.alpha + gamma = self.gamma + error_const = self.error_const + + J = self.J + LU = self.LU + current_jac = self.jac is None + + step_accepted = False + while not step_accepted: + if h_abs < min_step: + return False, self.TOO_SMALL_STEP + + h = h_abs * self.direction + t_new = t + h + + if self.direction * (t_new - self.t_bound) > 0: + t_new = self.t_bound + change_D(D, order, np.abs(t_new - t) / h_abs) + self.n_equal_steps = 0 + LU = None + + h = t_new - t + h_abs = np.abs(h) + + y_predict = np.sum(D[:order + 1], axis=0) + + scale = atol + rtol * np.abs(y_predict) + psi = np.dot(D[1: order + 1].T, gamma[1: order + 1]) / alpha[order] + + converged = False + c = h / alpha[order] + while not converged: + if LU is None: + LU = self.lu(self.I - c * J) + + converged, n_iter, y_new, d = solve_bdf_system( + self.fun, t_new, y_predict, c, psi, LU, self.solve_lu, + scale, self.newton_tol) + + if not converged: + if current_jac: + break + J = self.jac(t_new, y_predict) + LU = None + current_jac = True + + if not converged: + factor = 0.5 + h_abs *= factor + change_D(D, order, factor) + self.n_equal_steps = 0 + LU = None + continue + + safety = 0.9 * (2 * NEWTON_MAXITER + 1) / (2 * NEWTON_MAXITER + + n_iter) + + scale = atol + rtol * np.abs(y_new) + error = error_const[order] * d + error_norm = norm(error / scale) + + if error_norm > 1: + factor = max(MIN_FACTOR, + safety * error_norm ** (-1 / (order + 1))) + h_abs *= factor + change_D(D, order, factor) + self.n_equal_steps = 0 + # As we didn't have problems with convergence, we don't + # reset LU here. + else: + step_accepted = True + + self.n_equal_steps += 1 + + self.t = t_new + self.y = y_new + + self.h_abs = h_abs + self.J = J + self.LU = LU + + # Update differences. The principal relation here is + # D^{j + 1} y_n = D^{j} y_n - D^{j} y_{n - 1}. Keep in mind that D + # contained difference for previous interpolating polynomial and + # d = D^{k + 1} y_n. Thus this elegant code follows. + D[order + 2] = d - D[order + 1] + D[order + 1] = d + for i in reversed(range(order + 1)): + D[i] += D[i + 1] + + if self.n_equal_steps < order + 1: + return True, None + + if order > 1: + error_m = error_const[order - 1] * D[order] + error_m_norm = norm(error_m / scale) + else: + error_m_norm = np.inf + + if order < MAX_ORDER: + error_p = error_const[order + 1] * D[order + 2] + error_p_norm = norm(error_p / scale) + else: + error_p_norm = np.inf + + error_norms = np.array([error_m_norm, error_norm, error_p_norm]) + with np.errstate(divide='ignore'): + factors = error_norms ** (-1 / np.arange(order, order + 3)) + + delta_order = np.argmax(factors) - 1 + order += delta_order + self.order = order + + factor = min(MAX_FACTOR, safety * np.max(factors)) + self.h_abs *= factor + change_D(D, order, factor) + self.n_equal_steps = 0 + self.LU = None + + return True, None + + def _dense_output_impl(self): + return BdfDenseOutput(self.t_old, self.t, self.h_abs * self.direction, + self.order, self.D[:self.order + 1].copy()) + + +class BdfDenseOutput(DenseOutput): + def __init__(self, t_old, t, h, order, D): + super().__init__(t_old, t) + self.order = order + self.t_shift = self.t - h * np.arange(self.order) + self.denom = h * (1 + np.arange(self.order)) + self.D = D + + def _call_impl(self, t): + if t.ndim == 0: + x = (t - self.t_shift) / self.denom + p = np.cumprod(x) + else: + x = (t - self.t_shift[:, None]) / self.denom[:, None] + p = np.cumprod(x, axis=0) + + y = np.dot(self.D[1:].T, p) + if y.ndim == 1: + y += self.D[0] + else: + y += self.D[0, :, None] + + return y diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/common.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/common.py new file mode 100644 index 0000000000000000000000000000000000000000..0c820ad97f5a26955e20f98d80b71168dac54b0a --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/common.py @@ -0,0 +1,451 @@ +from itertools import groupby +from warnings import warn +import numpy as np +from scipy.sparse import find, coo_matrix + + +EPS = np.finfo(float).eps + + +def validate_first_step(first_step, t0, t_bound): + """Assert that first_step is valid and return it.""" + if first_step <= 0: + raise ValueError("`first_step` must be positive.") + if first_step > np.abs(t_bound - t0): + raise ValueError("`first_step` exceeds bounds.") + return first_step + + +def validate_max_step(max_step): + """Assert that max_Step is valid and return it.""" + if max_step <= 0: + raise ValueError("`max_step` must be positive.") + return max_step + + +def warn_extraneous(extraneous): + """Display a warning for extraneous keyword arguments. + + The initializer of each solver class is expected to collect keyword + arguments that it doesn't understand and warn about them. This function + prints a warning for each key in the supplied dictionary. + + Parameters + ---------- + extraneous : dict + Extraneous keyword arguments + """ + if extraneous: + warn("The following arguments have no effect for a chosen solver: " + f"{', '.join(f'`{x}`' for x in extraneous)}.", + stacklevel=3) + + +def validate_tol(rtol, atol, n): + """Validate tolerance values.""" + + if np.any(rtol < 100 * EPS): + warn("At least one element of `rtol` is too small. " + f"Setting `rtol = np.maximum(rtol, {100 * EPS})`.", + stacklevel=3) + rtol = np.maximum(rtol, 100 * EPS) + + atol = np.asarray(atol) + if atol.ndim > 0 and atol.shape != (n,): + raise ValueError("`atol` has wrong shape.") + + if np.any(atol < 0): + raise ValueError("`atol` must be positive.") + + return rtol, atol + + +def norm(x): + """Compute RMS norm.""" + return np.linalg.norm(x) / x.size ** 0.5 + + +def select_initial_step(fun, t0, y0, t_bound, + max_step, f0, direction, order, rtol, atol): + """Empirically select a good initial step. + + The algorithm is described in [1]_. + + Parameters + ---------- + fun : callable + Right-hand side of the system. + t0 : float + Initial value of the independent variable. + y0 : ndarray, shape (n,) + Initial value of the dependent variable. + t_bound : float + End-point of integration interval; used to ensure that t0+step<=tbound + and that fun is only evaluated in the interval [t0,tbound] + max_step : float + Maximum allowable step size. + f0 : ndarray, shape (n,) + Initial value of the derivative, i.e., ``fun(t0, y0)``. + direction : float + Integration direction. + order : float + Error estimator order. It means that the error controlled by the + algorithm is proportional to ``step_size ** (order + 1)`. + rtol : float + Desired relative tolerance. + atol : float + Desired absolute tolerance. + + Returns + ------- + h_abs : float + Absolute value of the suggested initial step. + + References + ---------- + .. [1] E. Hairer, S. P. Norsett G. Wanner, "Solving Ordinary Differential + Equations I: Nonstiff Problems", Sec. II.4. + """ + if y0.size == 0: + return np.inf + + interval_length = abs(t_bound - t0) + if interval_length == 0.0: + return 0.0 + + scale = atol + np.abs(y0) * rtol + d0 = norm(y0 / scale) + d1 = norm(f0 / scale) + if d0 < 1e-5 or d1 < 1e-5: + h0 = 1e-6 + else: + h0 = 0.01 * d0 / d1 + # Check t0+h0*direction doesn't take us beyond t_bound + h0 = min(h0, interval_length) + y1 = y0 + h0 * direction * f0 + f1 = fun(t0 + h0 * direction, y1) + d2 = norm((f1 - f0) / scale) / h0 + + if d1 <= 1e-15 and d2 <= 1e-15: + h1 = max(1e-6, h0 * 1e-3) + else: + h1 = (0.01 / max(d1, d2)) ** (1 / (order + 1)) + + return min(100 * h0, h1, interval_length, max_step) + + +class OdeSolution: + """Continuous ODE solution. + + It is organized as a collection of `DenseOutput` objects which represent + local interpolants. It provides an algorithm to select a right interpolant + for each given point. + + The interpolants cover the range between `t_min` and `t_max` (see + Attributes below). Evaluation outside this interval is not forbidden, but + the accuracy is not guaranteed. + + When evaluating at a breakpoint (one of the values in `ts`) a segment with + the lower index is selected. + + Parameters + ---------- + ts : array_like, shape (n_segments + 1,) + Time instants between which local interpolants are defined. Must + be strictly increasing or decreasing (zero segment with two points is + also allowed). + interpolants : list of DenseOutput with n_segments elements + Local interpolants. An i-th interpolant is assumed to be defined + between ``ts[i]`` and ``ts[i + 1]``. + alt_segment : boolean + Requests the alternative interpolant segment selection scheme. At each + solver integration point, two interpolant segments are available. The + default (False) and alternative (True) behaviours select the segment + for which the requested time corresponded to ``t`` and ``t_old``, + respectively. This functionality is only relevant for testing the + interpolants' accuracy: different integrators use different + construction strategies. + + Attributes + ---------- + t_min, t_max : float + Time range of the interpolation. + """ + def __init__(self, ts, interpolants, alt_segment=False): + ts = np.asarray(ts) + d = np.diff(ts) + # The first case covers integration on zero segment. + if not ((ts.size == 2 and ts[0] == ts[-1]) + or np.all(d > 0) or np.all(d < 0)): + raise ValueError("`ts` must be strictly increasing or decreasing.") + + self.n_segments = len(interpolants) + if ts.shape != (self.n_segments + 1,): + raise ValueError("Numbers of time stamps and interpolants " + "don't match.") + + self.ts = ts + self.interpolants = interpolants + if ts[-1] >= ts[0]: + self.t_min = ts[0] + self.t_max = ts[-1] + self.ascending = True + self.side = "right" if alt_segment else "left" + self.ts_sorted = ts + else: + self.t_min = ts[-1] + self.t_max = ts[0] + self.ascending = False + self.side = "left" if alt_segment else "right" + self.ts_sorted = ts[::-1] + + def _call_single(self, t): + # Here we preserve a certain symmetry that when t is in self.ts, + # if alt_segment=False, then we prioritize a segment with a lower + # index. + ind = np.searchsorted(self.ts_sorted, t, side=self.side) + + segment = min(max(ind - 1, 0), self.n_segments - 1) + if not self.ascending: + segment = self.n_segments - 1 - segment + + return self.interpolants[segment](t) + + def __call__(self, t): + """Evaluate the solution. + + Parameters + ---------- + t : float or array_like with shape (n_points,) + Points to evaluate at. + + Returns + ------- + y : ndarray, shape (n_states,) or (n_states, n_points) + Computed values. Shape depends on whether `t` is a scalar or a + 1-D array. + """ + t = np.asarray(t) + + if t.ndim == 0: + return self._call_single(t) + + order = np.argsort(t) + reverse = np.empty_like(order) + reverse[order] = np.arange(order.shape[0]) + t_sorted = t[order] + + # See comment in self._call_single. + segments = np.searchsorted(self.ts_sorted, t_sorted, side=self.side) + segments -= 1 + segments[segments < 0] = 0 + segments[segments > self.n_segments - 1] = self.n_segments - 1 + if not self.ascending: + segments = self.n_segments - 1 - segments + + ys = [] + group_start = 0 + for segment, group in groupby(segments): + group_end = group_start + len(list(group)) + y = self.interpolants[segment](t_sorted[group_start:group_end]) + ys.append(y) + group_start = group_end + + ys = np.hstack(ys) + ys = ys[:, reverse] + + return ys + + +NUM_JAC_DIFF_REJECT = EPS ** 0.875 +NUM_JAC_DIFF_SMALL = EPS ** 0.75 +NUM_JAC_DIFF_BIG = EPS ** 0.25 +NUM_JAC_MIN_FACTOR = 1e3 * EPS +NUM_JAC_FACTOR_INCREASE = 10 +NUM_JAC_FACTOR_DECREASE = 0.1 + + +def num_jac(fun, t, y, f, threshold, factor, sparsity=None): + """Finite differences Jacobian approximation tailored for ODE solvers. + + This function computes finite difference approximation to the Jacobian + matrix of `fun` with respect to `y` using forward differences. + The Jacobian matrix has shape (n, n) and its element (i, j) is equal to + ``d f_i / d y_j``. + + A special feature of this function is the ability to correct the step + size from iteration to iteration. The main idea is to keep the finite + difference significantly separated from its round-off error which + approximately equals ``EPS * np.abs(f)``. It reduces a possibility of a + huge error and assures that the estimated derivative are reasonably close + to the true values (i.e., the finite difference approximation is at least + qualitatively reflects the structure of the true Jacobian). + + Parameters + ---------- + fun : callable + Right-hand side of the system implemented in a vectorized fashion. + t : float + Current time. + y : ndarray, shape (n,) + Current state. + f : ndarray, shape (n,) + Value of the right hand side at (t, y). + threshold : float + Threshold for `y` value used for computing the step size as + ``factor * np.maximum(np.abs(y), threshold)``. Typically, the value of + absolute tolerance (atol) for a solver should be passed as `threshold`. + factor : ndarray with shape (n,) or None + Factor to use for computing the step size. Pass None for the very + evaluation, then use the value returned from this function. + sparsity : tuple (structure, groups) or None + Sparsity structure of the Jacobian, `structure` must be csc_matrix. + + Returns + ------- + J : ndarray or csc_matrix, shape (n, n) + Jacobian matrix. + factor : ndarray, shape (n,) + Suggested `factor` for the next evaluation. + """ + y = np.asarray(y) + n = y.shape[0] + if n == 0: + return np.empty((0, 0)), factor + + if factor is None: + factor = np.full(n, EPS ** 0.5) + else: + factor = factor.copy() + + # Direct the step as ODE dictates, hoping that such a step won't lead to + # a problematic region. For complex ODEs it makes sense to use the real + # part of f as we use steps along real axis. + f_sign = 2 * (np.real(f) >= 0).astype(float) - 1 + y_scale = f_sign * np.maximum(threshold, np.abs(y)) + h = (y + factor * y_scale) - y + + # Make sure that the step is not 0 to start with. Not likely it will be + # executed often. + for i in np.nonzero(h == 0)[0]: + while h[i] == 0: + factor[i] *= 10 + h[i] = (y[i] + factor[i] * y_scale[i]) - y[i] + + if sparsity is None: + return _dense_num_jac(fun, t, y, f, h, factor, y_scale) + else: + structure, groups = sparsity + return _sparse_num_jac(fun, t, y, f, h, factor, y_scale, + structure, groups) + + +def _dense_num_jac(fun, t, y, f, h, factor, y_scale): + n = y.shape[0] + h_vecs = np.diag(h) + f_new = fun(t, y[:, None] + h_vecs) + diff = f_new - f[:, None] + max_ind = np.argmax(np.abs(diff), axis=0) + r = np.arange(n) + max_diff = np.abs(diff[max_ind, r]) + scale = np.maximum(np.abs(f[max_ind]), np.abs(f_new[max_ind, r])) + + diff_too_small = max_diff < NUM_JAC_DIFF_REJECT * scale + if np.any(diff_too_small): + ind, = np.nonzero(diff_too_small) + new_factor = NUM_JAC_FACTOR_INCREASE * factor[ind] + h_new = (y[ind] + new_factor * y_scale[ind]) - y[ind] + h_vecs[ind, ind] = h_new + f_new = fun(t, y[:, None] + h_vecs[:, ind]) + diff_new = f_new - f[:, None] + max_ind = np.argmax(np.abs(diff_new), axis=0) + r = np.arange(ind.shape[0]) + max_diff_new = np.abs(diff_new[max_ind, r]) + scale_new = np.maximum(np.abs(f[max_ind]), np.abs(f_new[max_ind, r])) + + update = max_diff[ind] * scale_new < max_diff_new * scale[ind] + if np.any(update): + update, = np.nonzero(update) + update_ind = ind[update] + factor[update_ind] = new_factor[update] + h[update_ind] = h_new[update] + diff[:, update_ind] = diff_new[:, update] + scale[update_ind] = scale_new[update] + max_diff[update_ind] = max_diff_new[update] + + diff /= h + + factor[max_diff < NUM_JAC_DIFF_SMALL * scale] *= NUM_JAC_FACTOR_INCREASE + factor[max_diff > NUM_JAC_DIFF_BIG * scale] *= NUM_JAC_FACTOR_DECREASE + factor = np.maximum(factor, NUM_JAC_MIN_FACTOR) + + return diff, factor + + +def _sparse_num_jac(fun, t, y, f, h, factor, y_scale, structure, groups): + n = y.shape[0] + n_groups = np.max(groups) + 1 + h_vecs = np.empty((n_groups, n)) + for group in range(n_groups): + e = np.equal(group, groups) + h_vecs[group] = h * e + h_vecs = h_vecs.T + + f_new = fun(t, y[:, None] + h_vecs) + df = f_new - f[:, None] + + i, j, _ = find(structure) + diff = coo_matrix((df[i, groups[j]], (i, j)), shape=(n, n)).tocsc() + max_ind = np.array(abs(diff).argmax(axis=0)).ravel() + r = np.arange(n) + max_diff = np.asarray(np.abs(diff[max_ind, r])).ravel() + scale = np.maximum(np.abs(f[max_ind]), + np.abs(f_new[max_ind, groups[r]])) + + diff_too_small = max_diff < NUM_JAC_DIFF_REJECT * scale + if np.any(diff_too_small): + ind, = np.nonzero(diff_too_small) + new_factor = NUM_JAC_FACTOR_INCREASE * factor[ind] + h_new = (y[ind] + new_factor * y_scale[ind]) - y[ind] + h_new_all = np.zeros(n) + h_new_all[ind] = h_new + + groups_unique = np.unique(groups[ind]) + groups_map = np.empty(n_groups, dtype=int) + h_vecs = np.empty((groups_unique.shape[0], n)) + for k, group in enumerate(groups_unique): + e = np.equal(group, groups) + h_vecs[k] = h_new_all * e + groups_map[group] = k + h_vecs = h_vecs.T + + f_new = fun(t, y[:, None] + h_vecs) + df = f_new - f[:, None] + i, j, _ = find(structure[:, ind]) + diff_new = coo_matrix((df[i, groups_map[groups[ind[j]]]], + (i, j)), shape=(n, ind.shape[0])).tocsc() + + max_ind_new = np.array(abs(diff_new).argmax(axis=0)).ravel() + r = np.arange(ind.shape[0]) + max_diff_new = np.asarray(np.abs(diff_new[max_ind_new, r])).ravel() + scale_new = np.maximum( + np.abs(f[max_ind_new]), + np.abs(f_new[max_ind_new, groups_map[groups[ind]]])) + + update = max_diff[ind] * scale_new < max_diff_new * scale[ind] + if np.any(update): + update, = np.nonzero(update) + update_ind = ind[update] + factor[update_ind] = new_factor[update] + h[update_ind] = h_new[update] + diff[:, update_ind] = diff_new[:, update] + scale[update_ind] = scale_new[update] + max_diff[update_ind] = max_diff_new[update] + + diff.data /= np.repeat(h, np.diff(diff.indptr)) + + factor[max_diff < NUM_JAC_DIFF_SMALL * scale] *= NUM_JAC_FACTOR_INCREASE + factor[max_diff > NUM_JAC_DIFF_BIG * scale] *= NUM_JAC_FACTOR_DECREASE + factor = np.maximum(factor, NUM_JAC_MIN_FACTOR) + + return diff, factor diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/dop853_coefficients.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/dop853_coefficients.py new file mode 100644 index 0000000000000000000000000000000000000000..f39f2f3650d321e2c475d4e220f9769139118a5e --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/dop853_coefficients.py @@ -0,0 +1,193 @@ +import numpy as np + +N_STAGES = 12 +N_STAGES_EXTENDED = 16 +INTERPOLATOR_POWER = 7 + +C = np.array([0.0, + 0.526001519587677318785587544488e-01, + 0.789002279381515978178381316732e-01, + 0.118350341907227396726757197510, + 0.281649658092772603273242802490, + 0.333333333333333333333333333333, + 0.25, + 0.307692307692307692307692307692, + 0.651282051282051282051282051282, + 0.6, + 0.857142857142857142857142857142, + 1.0, + 1.0, + 0.1, + 0.2, + 0.777777777777777777777777777778]) + +A = np.zeros((N_STAGES_EXTENDED, N_STAGES_EXTENDED)) +A[1, 0] = 5.26001519587677318785587544488e-2 + +A[2, 0] = 1.97250569845378994544595329183e-2 +A[2, 1] = 5.91751709536136983633785987549e-2 + +A[3, 0] = 2.95875854768068491816892993775e-2 +A[3, 2] = 8.87627564304205475450678981324e-2 + +A[4, 0] = 2.41365134159266685502369798665e-1 +A[4, 2] = -8.84549479328286085344864962717e-1 +A[4, 3] = 9.24834003261792003115737966543e-1 + +A[5, 0] = 3.7037037037037037037037037037e-2 +A[5, 3] = 1.70828608729473871279604482173e-1 +A[5, 4] = 1.25467687566822425016691814123e-1 + +A[6, 0] = 3.7109375e-2 +A[6, 3] = 1.70252211019544039314978060272e-1 +A[6, 4] = 6.02165389804559606850219397283e-2 +A[6, 5] = -1.7578125e-2 + +A[7, 0] = 3.70920001185047927108779319836e-2 +A[7, 3] = 1.70383925712239993810214054705e-1 +A[7, 4] = 1.07262030446373284651809199168e-1 +A[7, 5] = -1.53194377486244017527936158236e-2 +A[7, 6] = 8.27378916381402288758473766002e-3 + +A[8, 0] = 6.24110958716075717114429577812e-1 +A[8, 3] = -3.36089262944694129406857109825 +A[8, 4] = -8.68219346841726006818189891453e-1 +A[8, 5] = 2.75920996994467083049415600797e1 +A[8, 6] = 2.01540675504778934086186788979e1 +A[8, 7] = -4.34898841810699588477366255144e1 + +A[9, 0] = 4.77662536438264365890433908527e-1 +A[9, 3] = -2.48811461997166764192642586468 +A[9, 4] = -5.90290826836842996371446475743e-1 +A[9, 5] = 2.12300514481811942347288949897e1 +A[9, 6] = 1.52792336328824235832596922938e1 +A[9, 7] = -3.32882109689848629194453265587e1 +A[9, 8] = -2.03312017085086261358222928593e-2 + +A[10, 0] = -9.3714243008598732571704021658e-1 +A[10, 3] = 5.18637242884406370830023853209 +A[10, 4] = 1.09143734899672957818500254654 +A[10, 5] = -8.14978701074692612513997267357 +A[10, 6] = -1.85200656599969598641566180701e1 +A[10, 7] = 2.27394870993505042818970056734e1 +A[10, 8] = 2.49360555267965238987089396762 +A[10, 9] = -3.0467644718982195003823669022 + +A[11, 0] = 2.27331014751653820792359768449 +A[11, 3] = -1.05344954667372501984066689879e1 +A[11, 4] = -2.00087205822486249909675718444 +A[11, 5] = -1.79589318631187989172765950534e1 +A[11, 6] = 2.79488845294199600508499808837e1 +A[11, 7] = -2.85899827713502369474065508674 +A[11, 8] = -8.87285693353062954433549289258 +A[11, 9] = 1.23605671757943030647266201528e1 +A[11, 10] = 6.43392746015763530355970484046e-1 + +A[12, 0] = 5.42937341165687622380535766363e-2 +A[12, 5] = 4.45031289275240888144113950566 +A[12, 6] = 1.89151789931450038304281599044 +A[12, 7] = -5.8012039600105847814672114227 +A[12, 8] = 3.1116436695781989440891606237e-1 +A[12, 9] = -1.52160949662516078556178806805e-1 +A[12, 10] = 2.01365400804030348374776537501e-1 +A[12, 11] = 4.47106157277725905176885569043e-2 + +A[13, 0] = 5.61675022830479523392909219681e-2 +A[13, 6] = 2.53500210216624811088794765333e-1 +A[13, 7] = -2.46239037470802489917441475441e-1 +A[13, 8] = -1.24191423263816360469010140626e-1 +A[13, 9] = 1.5329179827876569731206322685e-1 +A[13, 10] = 8.20105229563468988491666602057e-3 +A[13, 11] = 7.56789766054569976138603589584e-3 +A[13, 12] = -8.298e-3 + +A[14, 0] = 3.18346481635021405060768473261e-2 +A[14, 5] = 2.83009096723667755288322961402e-2 +A[14, 6] = 5.35419883074385676223797384372e-2 +A[14, 7] = -5.49237485713909884646569340306e-2 +A[14, 10] = -1.08347328697249322858509316994e-4 +A[14, 11] = 3.82571090835658412954920192323e-4 +A[14, 12] = -3.40465008687404560802977114492e-4 +A[14, 13] = 1.41312443674632500278074618366e-1 + +A[15, 0] = -4.28896301583791923408573538692e-1 +A[15, 5] = -4.69762141536116384314449447206 +A[15, 6] = 7.68342119606259904184240953878 +A[15, 7] = 4.06898981839711007970213554331 +A[15, 8] = 3.56727187455281109270669543021e-1 +A[15, 12] = -1.39902416515901462129418009734e-3 +A[15, 13] = 2.9475147891527723389556272149 +A[15, 14] = -9.15095847217987001081870187138 + + +B = A[N_STAGES, :N_STAGES] + +E3 = np.zeros(N_STAGES + 1) +E3[:-1] = B.copy() +E3[0] -= 0.244094488188976377952755905512 +E3[8] -= 0.733846688281611857341361741547 +E3[11] -= 0.220588235294117647058823529412e-1 + +E5 = np.zeros(N_STAGES + 1) +E5[0] = 0.1312004499419488073250102996e-1 +E5[5] = -0.1225156446376204440720569753e+1 +E5[6] = -0.4957589496572501915214079952 +E5[7] = 0.1664377182454986536961530415e+1 +E5[8] = -0.3503288487499736816886487290 +E5[9] = 0.3341791187130174790297318841 +E5[10] = 0.8192320648511571246570742613e-1 +E5[11] = -0.2235530786388629525884427845e-1 + +# First 3 coefficients are computed separately. +D = np.zeros((INTERPOLATOR_POWER - 3, N_STAGES_EXTENDED)) +D[0, 0] = -0.84289382761090128651353491142e+1 +D[0, 5] = 0.56671495351937776962531783590 +D[0, 6] = -0.30689499459498916912797304727e+1 +D[0, 7] = 0.23846676565120698287728149680e+1 +D[0, 8] = 0.21170345824450282767155149946e+1 +D[0, 9] = -0.87139158377797299206789907490 +D[0, 10] = 0.22404374302607882758541771650e+1 +D[0, 11] = 0.63157877876946881815570249290 +D[0, 12] = -0.88990336451333310820698117400e-1 +D[0, 13] = 0.18148505520854727256656404962e+2 +D[0, 14] = -0.91946323924783554000451984436e+1 +D[0, 15] = -0.44360363875948939664310572000e+1 + +D[1, 0] = 0.10427508642579134603413151009e+2 +D[1, 5] = 0.24228349177525818288430175319e+3 +D[1, 6] = 0.16520045171727028198505394887e+3 +D[1, 7] = -0.37454675472269020279518312152e+3 +D[1, 8] = -0.22113666853125306036270938578e+2 +D[1, 9] = 0.77334326684722638389603898808e+1 +D[1, 10] = -0.30674084731089398182061213626e+2 +D[1, 11] = -0.93321305264302278729567221706e+1 +D[1, 12] = 0.15697238121770843886131091075e+2 +D[1, 13] = -0.31139403219565177677282850411e+2 +D[1, 14] = -0.93529243588444783865713862664e+1 +D[1, 15] = 0.35816841486394083752465898540e+2 + +D[2, 0] = 0.19985053242002433820987653617e+2 +D[2, 5] = -0.38703730874935176555105901742e+3 +D[2, 6] = -0.18917813819516756882830838328e+3 +D[2, 7] = 0.52780815920542364900561016686e+3 +D[2, 8] = -0.11573902539959630126141871134e+2 +D[2, 9] = 0.68812326946963000169666922661e+1 +D[2, 10] = -0.10006050966910838403183860980e+1 +D[2, 11] = 0.77771377980534432092869265740 +D[2, 12] = -0.27782057523535084065932004339e+1 +D[2, 13] = -0.60196695231264120758267380846e+2 +D[2, 14] = 0.84320405506677161018159903784e+2 +D[2, 15] = 0.11992291136182789328035130030e+2 + +D[3, 0] = -0.25693933462703749003312586129e+2 +D[3, 5] = -0.15418974869023643374053993627e+3 +D[3, 6] = -0.23152937917604549567536039109e+3 +D[3, 7] = 0.35763911791061412378285349910e+3 +D[3, 8] = 0.93405324183624310003907691704e+2 +D[3, 9] = -0.37458323136451633156875139351e+2 +D[3, 10] = 0.10409964950896230045147246184e+3 +D[3, 11] = 0.29840293426660503123344363579e+2 +D[3, 12] = -0.43533456590011143754432175058e+2 +D[3, 13] = 0.96324553959188282948394950600e+2 +D[3, 14] = -0.39177261675615439165231486172e+2 +D[3, 15] = -0.14972683625798562581422125276e+3 diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/ivp.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/ivp.py new file mode 100644 index 0000000000000000000000000000000000000000..8186982e4fddbd8c1058b59c745ca66ab3a9c224 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/ivp.py @@ -0,0 +1,755 @@ +import inspect +import numpy as np +from .bdf import BDF +from .radau import Radau +from .rk import RK23, RK45, DOP853 +from .lsoda import LSODA +from scipy.optimize import OptimizeResult +from .common import EPS, OdeSolution +from .base import OdeSolver + + +METHODS = {'RK23': RK23, + 'RK45': RK45, + 'DOP853': DOP853, + 'Radau': Radau, + 'BDF': BDF, + 'LSODA': LSODA} + + +MESSAGES = {0: "The solver successfully reached the end of the integration interval.", + 1: "A termination event occurred."} + + +class OdeResult(OptimizeResult): + pass + + +def prepare_events(events): + """Standardize event functions and extract attributes.""" + if callable(events): + events = (events,) + + max_events = np.empty(len(events)) + direction = np.empty(len(events)) + for i, event in enumerate(events): + terminal = getattr(event, 'terminal', None) + direction[i] = getattr(event, 'direction', 0) + + message = ('The `terminal` attribute of each event ' + 'must be a boolean or positive integer.') + if terminal is None or terminal == 0: + max_events[i] = np.inf + elif int(terminal) == terminal and terminal > 0: + max_events[i] = terminal + else: + raise ValueError(message) + + return events, max_events, direction + + +def solve_event_equation(event, sol, t_old, t): + """Solve an equation corresponding to an ODE event. + + The equation is ``event(t, y(t)) = 0``, here ``y(t)`` is known from an + ODE solver using some sort of interpolation. It is solved by + `scipy.optimize.brentq` with xtol=atol=4*EPS. + + Parameters + ---------- + event : callable + Function ``event(t, y)``. + sol : callable + Function ``sol(t)`` which evaluates an ODE solution between `t_old` + and `t`. + t_old, t : float + Previous and new values of time. They will be used as a bracketing + interval. + + Returns + ------- + root : float + Found solution. + """ + from scipy.optimize import brentq + return brentq(lambda t: event(t, sol(t)), t_old, t, + xtol=4 * EPS, rtol=4 * EPS) + + +def handle_events(sol, events, active_events, event_count, max_events, + t_old, t): + """Helper function to handle events. + + Parameters + ---------- + sol : DenseOutput + Function ``sol(t)`` which evaluates an ODE solution between `t_old` + and `t`. + events : list of callables, length n_events + Event functions with signatures ``event(t, y)``. + active_events : ndarray + Indices of events which occurred. + event_count : ndarray + Current number of occurrences for each event. + max_events : ndarray, shape (n_events,) + Number of occurrences allowed for each event before integration + termination is issued. + t_old, t : float + Previous and new values of time. + + Returns + ------- + root_indices : ndarray + Indices of events which take zero between `t_old` and `t` and before + a possible termination. + roots : ndarray + Values of t at which events occurred. + terminate : bool + Whether a terminal event occurred. + """ + roots = [solve_event_equation(events[event_index], sol, t_old, t) + for event_index in active_events] + + roots = np.asarray(roots) + + if np.any(event_count[active_events] >= max_events[active_events]): + if t > t_old: + order = np.argsort(roots) + else: + order = np.argsort(-roots) + active_events = active_events[order] + roots = roots[order] + t = np.nonzero(event_count[active_events] + >= max_events[active_events])[0][0] + active_events = active_events[:t + 1] + roots = roots[:t + 1] + terminate = True + else: + terminate = False + + return active_events, roots, terminate + + +def find_active_events(g, g_new, direction): + """Find which event occurred during an integration step. + + Parameters + ---------- + g, g_new : array_like, shape (n_events,) + Values of event functions at a current and next points. + direction : ndarray, shape (n_events,) + Event "direction" according to the definition in `solve_ivp`. + + Returns + ------- + active_events : ndarray + Indices of events which occurred during the step. + """ + g, g_new = np.asarray(g), np.asarray(g_new) + up = (g <= 0) & (g_new >= 0) + down = (g >= 0) & (g_new <= 0) + either = up | down + mask = (up & (direction > 0) | + down & (direction < 0) | + either & (direction == 0)) + + return np.nonzero(mask)[0] + + +def solve_ivp(fun, t_span, y0, method='RK45', t_eval=None, dense_output=False, + events=None, vectorized=False, args=None, **options): + """Solve an initial value problem for a system of ODEs. + + This function numerically integrates a system of ordinary differential + equations given an initial value:: + + dy / dt = f(t, y) + y(t0) = y0 + + Here t is a 1-D independent variable (time), y(t) is an + N-D vector-valued function (state), and an N-D + vector-valued function f(t, y) determines the differential equations. + The goal is to find y(t) approximately satisfying the differential + equations, given an initial value y(t0)=y0. + + Some of the solvers support integration in the complex domain, but note + that for stiff ODE solvers, the right-hand side must be + complex-differentiable (satisfy Cauchy-Riemann equations [11]_). + To solve a problem in the complex domain, pass y0 with a complex data type. + Another option always available is to rewrite your problem for real and + imaginary parts separately. + + Parameters + ---------- + fun : callable + Right-hand side of the system: the time derivative of the state ``y`` + at time ``t``. The calling signature is ``fun(t, y)``, where ``t`` is a + scalar and ``y`` is an ndarray with ``len(y) = len(y0)``. Additional + arguments need to be passed if ``args`` is used (see documentation of + ``args`` argument). ``fun`` must return an array of the same shape as + ``y``. See `vectorized` for more information. + t_span : 2-member sequence + Interval of integration (t0, tf). The solver starts with t=t0 and + integrates until it reaches t=tf. Both t0 and tf must be floats + or values interpretable by the float conversion function. + y0 : array_like, shape (n,) + Initial state. For problems in the complex domain, pass `y0` with a + complex data type (even if the initial value is purely real). + method : string or `OdeSolver`, optional + Integration method to use: + + * 'RK45' (default): Explicit Runge-Kutta method of order 5(4) [1]_. + The error is controlled assuming accuracy of the fourth-order + method, but steps are taken using the fifth-order accurate + formula (local extrapolation is done). A quartic interpolation + polynomial is used for the dense output [2]_. Can be applied in + the complex domain. + * 'RK23': Explicit Runge-Kutta method of order 3(2) [3]_. The error + is controlled assuming accuracy of the second-order method, but + steps are taken using the third-order accurate formula (local + extrapolation is done). A cubic Hermite polynomial is used for the + dense output. Can be applied in the complex domain. + * 'DOP853': Explicit Runge-Kutta method of order 8 [13]_. + Python implementation of the "DOP853" algorithm originally + written in Fortran [14]_. A 7-th order interpolation polynomial + accurate to 7-th order is used for the dense output. + Can be applied in the complex domain. + * 'Radau': Implicit Runge-Kutta method of the Radau IIA family of + order 5 [4]_. The error is controlled with a third-order accurate + embedded formula. A cubic polynomial which satisfies the + collocation conditions is used for the dense output. + * 'BDF': Implicit multi-step variable-order (1 to 5) method based + on a backward differentiation formula for the derivative + approximation [5]_. The implementation follows the one described + in [6]_. A quasi-constant step scheme is used and accuracy is + enhanced using the NDF modification. Can be applied in the + complex domain. + * 'LSODA': Adams/BDF method with automatic stiffness detection and + switching [7]_, [8]_. This is a wrapper of the Fortran solver + from ODEPACK. + + Explicit Runge-Kutta methods ('RK23', 'RK45', 'DOP853') should be used + for non-stiff problems and implicit methods ('Radau', 'BDF') for + stiff problems [9]_. Among Runge-Kutta methods, 'DOP853' is recommended + for solving with high precision (low values of `rtol` and `atol`). + + If not sure, first try to run 'RK45'. If it makes unusually many + iterations, diverges, or fails, your problem is likely to be stiff and + you should use 'Radau' or 'BDF'. 'LSODA' can also be a good universal + choice, but it might be somewhat less convenient to work with as it + wraps old Fortran code. + + You can also pass an arbitrary class derived from `OdeSolver` which + implements the solver. + t_eval : array_like or None, optional + Times at which to store the computed solution, must be sorted and lie + within `t_span`. If None (default), use points selected by the solver. + dense_output : bool, optional + Whether to compute a continuous solution. Default is False. + events : callable, or list of callables, optional + Events to track. If None (default), no events will be tracked. + Each event occurs at the zeros of a continuous function of time and + state. Each function must have the signature ``event(t, y)`` where + additional argument have to be passed if ``args`` is used (see + documentation of ``args`` argument). Each function must return a + float. The solver will find an accurate value of `t` at which + ``event(t, y(t)) = 0`` using a root-finding algorithm. By default, + all zeros will be found. The solver looks for a sign change over + each step, so if multiple zero crossings occur within one step, + events may be missed. Additionally each `event` function might + have the following attributes: + + terminal: bool or int, optional + When boolean, whether to terminate integration if this event occurs. + When integral, termination occurs after the specified the number of + occurrences of this event. + Implicitly False if not assigned. + direction: float, optional + Direction of a zero crossing. If `direction` is positive, + `event` will only trigger when going from negative to positive, + and vice versa if `direction` is negative. If 0, then either + direction will trigger event. Implicitly 0 if not assigned. + + You can assign attributes like ``event.terminal = True`` to any + function in Python. + vectorized : bool, optional + Whether `fun` can be called in a vectorized fashion. Default is False. + + If ``vectorized`` is False, `fun` will always be called with ``y`` of + shape ``(n,)``, where ``n = len(y0)``. + + If ``vectorized`` is True, `fun` may be called with ``y`` of shape + ``(n, k)``, where ``k`` is an integer. In this case, `fun` must behave + such that ``fun(t, y)[:, i] == fun(t, y[:, i])`` (i.e. each column of + the returned array is the time derivative of the state corresponding + with a column of ``y``). + + Setting ``vectorized=True`` allows for faster finite difference + approximation of the Jacobian by methods 'Radau' and 'BDF', but + will result in slower execution for other methods and for 'Radau' and + 'BDF' in some circumstances (e.g. small ``len(y0)``). + args : tuple, optional + Additional arguments to pass to the user-defined functions. If given, + the additional arguments are passed to all user-defined functions. + So if, for example, `fun` has the signature ``fun(t, y, a, b, c)``, + then `jac` (if given) and any event functions must have the same + signature, and `args` must be a tuple of length 3. + **options + Options passed to a chosen solver. All options available for already + implemented solvers are listed below. + first_step : float or None, optional + Initial step size. Default is `None` which means that the algorithm + should choose. + max_step : float, optional + Maximum allowed step size. Default is np.inf, i.e., the step size is not + bounded and determined solely by the solver. + rtol, atol : float or array_like, optional + Relative and absolute tolerances. The solver keeps the local error + estimates less than ``atol + rtol * abs(y)``. Here `rtol` controls a + relative accuracy (number of correct digits), while `atol` controls + absolute accuracy (number of correct decimal places). To achieve the + desired `rtol`, set `atol` to be smaller than the smallest value that + can be expected from ``rtol * abs(y)`` so that `rtol` dominates the + allowable error. If `atol` is larger than ``rtol * abs(y)`` the + number of correct digits is not guaranteed. Conversely, to achieve the + desired `atol` set `rtol` such that ``rtol * abs(y)`` is always smaller + than `atol`. If components of y have different scales, it might be + beneficial to set different `atol` values for different components by + passing array_like with shape (n,) for `atol`. Default values are + 1e-3 for `rtol` and 1e-6 for `atol`. + jac : array_like, sparse_matrix, callable or None, optional + Jacobian matrix of the right-hand side of the system with respect + to y, required by the 'Radau', 'BDF' and 'LSODA' method. The + Jacobian matrix has shape (n, n) and its element (i, j) is equal to + ``d f_i / d y_j``. There are three ways to define the Jacobian: + + * If array_like or sparse_matrix, the Jacobian is assumed to + be constant. Not supported by 'LSODA'. + * If callable, the Jacobian is assumed to depend on both + t and y; it will be called as ``jac(t, y)``, as necessary. + Additional arguments have to be passed if ``args`` is + used (see documentation of ``args`` argument). + For 'Radau' and 'BDF' methods, the return value might be a + sparse matrix. + * If None (default), the Jacobian will be approximated by + finite differences. + + It is generally recommended to provide the Jacobian rather than + relying on a finite-difference approximation. + jac_sparsity : array_like, sparse matrix or None, optional + Defines a sparsity structure of the Jacobian matrix for a finite- + difference approximation. Its shape must be (n, n). This argument + is ignored if `jac` is not `None`. If the Jacobian has only few + non-zero elements in *each* row, providing the sparsity structure + will greatly speed up the computations [10]_. A zero entry means that + a corresponding element in the Jacobian is always zero. If None + (default), the Jacobian is assumed to be dense. + Not supported by 'LSODA', see `lband` and `uband` instead. + lband, uband : int or None, optional + Parameters defining the bandwidth of the Jacobian for the 'LSODA' + method, i.e., ``jac[i, j] != 0 only for i - lband <= j <= i + uband``. + Default is None. Setting these requires your jac routine to return the + Jacobian in the packed format: the returned array must have ``n`` + columns and ``uband + lband + 1`` rows in which Jacobian diagonals are + written. Specifically ``jac_packed[uband + i - j , j] = jac[i, j]``. + The same format is used in `scipy.linalg.solve_banded` (check for an + illustration). These parameters can be also used with ``jac=None`` to + reduce the number of Jacobian elements estimated by finite differences. + min_step : float, optional + The minimum allowed step size for 'LSODA' method. + By default `min_step` is zero. + + Returns + ------- + Bunch object with the following fields defined: + t : ndarray, shape (n_points,) + Time points. + y : ndarray, shape (n, n_points) + Values of the solution at `t`. + sol : `OdeSolution` or None + Found solution as `OdeSolution` instance; None if `dense_output` was + set to False. + t_events : list of ndarray or None + Contains for each event type a list of arrays at which an event of + that type event was detected. None if `events` was None. + y_events : list of ndarray or None + For each value of `t_events`, the corresponding value of the solution. + None if `events` was None. + nfev : int + Number of evaluations of the right-hand side. + njev : int + Number of evaluations of the Jacobian. + nlu : int + Number of LU decompositions. + status : int + Reason for algorithm termination: + + * -1: Integration step failed. + * 0: The solver successfully reached the end of `tspan`. + * 1: A termination event occurred. + + message : string + Human-readable description of the termination reason. + success : bool + True if the solver reached the interval end or a termination event + occurred (``status >= 0``). + + References + ---------- + .. [1] J. R. Dormand, P. J. Prince, "A family of embedded Runge-Kutta + formulae", Journal of Computational and Applied Mathematics, Vol. 6, + No. 1, pp. 19-26, 1980. + .. [2] L. W. Shampine, "Some Practical Runge-Kutta Formulas", Mathematics + of Computation,, Vol. 46, No. 173, pp. 135-150, 1986. + .. [3] P. Bogacki, L.F. Shampine, "A 3(2) Pair of Runge-Kutta Formulas", + Appl. Math. Lett. Vol. 2, No. 4. pp. 321-325, 1989. + .. [4] E. Hairer, G. Wanner, "Solving Ordinary Differential Equations II: + Stiff and Differential-Algebraic Problems", Sec. IV.8. + .. [5] `Backward Differentiation Formula + `_ + on Wikipedia. + .. [6] L. F. Shampine, M. W. Reichelt, "THE MATLAB ODE SUITE", SIAM J. SCI. + COMPUTE., Vol. 18, No. 1, pp. 1-22, January 1997. + .. [7] A. C. Hindmarsh, "ODEPACK, A Systematized Collection of ODE + Solvers," IMACS Transactions on Scientific Computation, Vol 1., + pp. 55-64, 1983. + .. [8] L. Petzold, "Automatic selection of methods for solving stiff and + nonstiff systems of ordinary differential equations", SIAM Journal + on Scientific and Statistical Computing, Vol. 4, No. 1, pp. 136-148, + 1983. + .. [9] `Stiff equation `_ on + Wikipedia. + .. [10] A. Curtis, M. J. D. Powell, and J. Reid, "On the estimation of + sparse Jacobian matrices", Journal of the Institute of Mathematics + and its Applications, 13, pp. 117-120, 1974. + .. [11] `Cauchy-Riemann equations + `_ on + Wikipedia. + .. [12] `Lotka-Volterra equations + `_ + on Wikipedia. + .. [13] E. Hairer, S. P. Norsett G. Wanner, "Solving Ordinary Differential + Equations I: Nonstiff Problems", Sec. II. + .. [14] `Page with original Fortran code of DOP853 + `_. + + Examples + -------- + Basic exponential decay showing automatically chosen time points. + + >>> import numpy as np + >>> from scipy.integrate import solve_ivp + >>> def exponential_decay(t, y): return -0.5 * y + >>> sol = solve_ivp(exponential_decay, [0, 10], [2, 4, 8]) + >>> print(sol.t) + [ 0. 0.11487653 1.26364188 3.06061781 4.81611105 6.57445806 + 8.33328988 10. ] + >>> print(sol.y) + [[2. 1.88836035 1.06327177 0.43319312 0.18017253 0.07483045 + 0.03107158 0.01350781] + [4. 3.7767207 2.12654355 0.86638624 0.36034507 0.14966091 + 0.06214316 0.02701561] + [8. 7.5534414 4.25308709 1.73277247 0.72069014 0.29932181 + 0.12428631 0.05403123]] + + Specifying points where the solution is desired. + + >>> sol = solve_ivp(exponential_decay, [0, 10], [2, 4, 8], + ... t_eval=[0, 1, 2, 4, 10]) + >>> print(sol.t) + [ 0 1 2 4 10] + >>> print(sol.y) + [[2. 1.21305369 0.73534021 0.27066736 0.01350938] + [4. 2.42610739 1.47068043 0.54133472 0.02701876] + [8. 4.85221478 2.94136085 1.08266944 0.05403753]] + + Cannon fired upward with terminal event upon impact. The ``terminal`` and + ``direction`` fields of an event are applied by monkey patching a function. + Here ``y[0]`` is position and ``y[1]`` is velocity. The projectile starts + at position 0 with velocity +10. Note that the integration never reaches + t=100 because the event is terminal. + + >>> def upward_cannon(t, y): return [y[1], -0.5] + >>> def hit_ground(t, y): return y[0] + >>> hit_ground.terminal = True + >>> hit_ground.direction = -1 + >>> sol = solve_ivp(upward_cannon, [0, 100], [0, 10], events=hit_ground) + >>> print(sol.t_events) + [array([40.])] + >>> print(sol.t) + [0.00000000e+00 9.99900010e-05 1.09989001e-03 1.10988901e-02 + 1.11088891e-01 1.11098890e+00 1.11099890e+01 4.00000000e+01] + + Use `dense_output` and `events` to find position, which is 100, at the apex + of the cannonball's trajectory. Apex is not defined as terminal, so both + apex and hit_ground are found. There is no information at t=20, so the sol + attribute is used to evaluate the solution. The sol attribute is returned + by setting ``dense_output=True``. Alternatively, the `y_events` attribute + can be used to access the solution at the time of the event. + + >>> def apex(t, y): return y[1] + >>> sol = solve_ivp(upward_cannon, [0, 100], [0, 10], + ... events=(hit_ground, apex), dense_output=True) + >>> print(sol.t_events) + [array([40.]), array([20.])] + >>> print(sol.t) + [0.00000000e+00 9.99900010e-05 1.09989001e-03 1.10988901e-02 + 1.11088891e-01 1.11098890e+00 1.11099890e+01 4.00000000e+01] + >>> print(sol.sol(sol.t_events[1][0])) + [100. 0.] + >>> print(sol.y_events) + [array([[-5.68434189e-14, -1.00000000e+01]]), + array([[1.00000000e+02, 1.77635684e-15]])] + + As an example of a system with additional parameters, we'll implement + the Lotka-Volterra equations [12]_. + + >>> def lotkavolterra(t, z, a, b, c, d): + ... x, y = z + ... return [a*x - b*x*y, -c*y + d*x*y] + ... + + We pass in the parameter values a=1.5, b=1, c=3 and d=1 with the `args` + argument. + + >>> sol = solve_ivp(lotkavolterra, [0, 15], [10, 5], args=(1.5, 1, 3, 1), + ... dense_output=True) + + Compute a dense solution and plot it. + + >>> t = np.linspace(0, 15, 300) + >>> z = sol.sol(t) + >>> import matplotlib.pyplot as plt + >>> plt.plot(t, z.T) + >>> plt.xlabel('t') + >>> plt.legend(['x', 'y'], shadow=True) + >>> plt.title('Lotka-Volterra System') + >>> plt.show() + + A couple examples of using solve_ivp to solve the differential + equation ``y' = Ay`` with complex matrix ``A``. + + >>> A = np.array([[-0.25 + 0.14j, 0, 0.33 + 0.44j], + ... [0.25 + 0.58j, -0.2 + 0.14j, 0], + ... [0, 0.2 + 0.4j, -0.1 + 0.97j]]) + + Solving an IVP with ``A`` from above and ``y`` as 3x1 vector: + + >>> def deriv_vec(t, y): + ... return A @ y + >>> result = solve_ivp(deriv_vec, [0, 25], + ... np.array([10 + 0j, 20 + 0j, 30 + 0j]), + ... t_eval=np.linspace(0, 25, 101)) + >>> print(result.y[:, 0]) + [10.+0.j 20.+0.j 30.+0.j] + >>> print(result.y[:, -1]) + [18.46291039+45.25653651j 10.01569306+36.23293216j + -4.98662741+80.07360388j] + + Solving an IVP with ``A`` from above with ``y`` as 3x3 matrix : + + >>> def deriv_mat(t, y): + ... return (A @ y.reshape(3, 3)).flatten() + >>> y0 = np.array([[2 + 0j, 3 + 0j, 4 + 0j], + ... [5 + 0j, 6 + 0j, 7 + 0j], + ... [9 + 0j, 34 + 0j, 78 + 0j]]) + + >>> result = solve_ivp(deriv_mat, [0, 25], y0.flatten(), + ... t_eval=np.linspace(0, 25, 101)) + >>> print(result.y[:, 0].reshape(3, 3)) + [[ 2.+0.j 3.+0.j 4.+0.j] + [ 5.+0.j 6.+0.j 7.+0.j] + [ 9.+0.j 34.+0.j 78.+0.j]] + >>> print(result.y[:, -1].reshape(3, 3)) + [[ 5.67451179 +12.07938445j 17.2888073 +31.03278837j + 37.83405768 +63.25138759j] + [ 3.39949503 +11.82123994j 21.32530996 +44.88668871j + 53.17531184+103.80400411j] + [ -2.26105874 +22.19277664j -15.1255713 +70.19616341j + -38.34616845+153.29039931j]] + + + """ + if method not in METHODS and not ( + inspect.isclass(method) and issubclass(method, OdeSolver)): + raise ValueError(f"`method` must be one of {METHODS} or OdeSolver class.") + + t0, tf = map(float, t_span) + + if args is not None: + # Wrap the user's fun (and jac, if given) in lambdas to hide the + # additional parameters. Pass in the original fun as a keyword + # argument to keep it in the scope of the lambda. + try: + _ = [*(args)] + except TypeError as exp: + suggestion_tuple = ( + "Supplied 'args' cannot be unpacked. Please supply `args`" + f" as a tuple (e.g. `args=({args},)`)" + ) + raise TypeError(suggestion_tuple) from exp + + def fun(t, x, fun=fun): + return fun(t, x, *args) + jac = options.get('jac') + if callable(jac): + options['jac'] = lambda t, x: jac(t, x, *args) + + if t_eval is not None: + t_eval = np.asarray(t_eval) + if t_eval.ndim != 1: + raise ValueError("`t_eval` must be 1-dimensional.") + + if np.any(t_eval < min(t0, tf)) or np.any(t_eval > max(t0, tf)): + raise ValueError("Values in `t_eval` are not within `t_span`.") + + d = np.diff(t_eval) + if tf > t0 and np.any(d <= 0) or tf < t0 and np.any(d >= 0): + raise ValueError("Values in `t_eval` are not properly sorted.") + + if tf > t0: + t_eval_i = 0 + else: + # Make order of t_eval decreasing to use np.searchsorted. + t_eval = t_eval[::-1] + # This will be an upper bound for slices. + t_eval_i = t_eval.shape[0] + + if method in METHODS: + method = METHODS[method] + + solver = method(fun, t0, y0, tf, vectorized=vectorized, **options) + + if t_eval is None: + ts = [t0] + ys = [y0] + elif t_eval is not None and dense_output: + ts = [] + ti = [t0] + ys = [] + else: + ts = [] + ys = [] + + interpolants = [] + + if events is not None: + events, max_events, event_dir = prepare_events(events) + event_count = np.zeros(len(events)) + if args is not None: + # Wrap user functions in lambdas to hide the additional parameters. + # The original event function is passed as a keyword argument to the + # lambda to keep the original function in scope (i.e., avoid the + # late binding closure "gotcha"). + events = [lambda t, x, event=event: event(t, x, *args) + for event in events] + g = [event(t0, y0) for event in events] + t_events = [[] for _ in range(len(events))] + y_events = [[] for _ in range(len(events))] + else: + t_events = None + y_events = None + + status = None + while status is None: + message = solver.step() + + if solver.status == 'finished': + status = 0 + elif solver.status == 'failed': + status = -1 + break + + t_old = solver.t_old + t = solver.t + y = solver.y + + if dense_output: + sol = solver.dense_output() + interpolants.append(sol) + else: + sol = None + + if events is not None: + g_new = [event(t, y) for event in events] + active_events = find_active_events(g, g_new, event_dir) + if active_events.size > 0: + if sol is None: + sol = solver.dense_output() + + event_count[active_events] += 1 + root_indices, roots, terminate = handle_events( + sol, events, active_events, event_count, max_events, + t_old, t) + + for e, te in zip(root_indices, roots): + t_events[e].append(te) + y_events[e].append(sol(te)) + + if terminate: + status = 1 + t = roots[-1] + y = sol(t) + + g = g_new + + if t_eval is None: + donot_append = (len(ts) > 1 and + ts[-1] == t and + dense_output) + if not donot_append: + ts.append(t) + ys.append(y) + else: + if len(interpolants) > 0: + interpolants.pop() + else: + # The value in t_eval equal to t will be included. + if solver.direction > 0: + t_eval_i_new = np.searchsorted(t_eval, t, side='right') + t_eval_step = t_eval[t_eval_i:t_eval_i_new] + else: + t_eval_i_new = np.searchsorted(t_eval, t, side='left') + # It has to be done with two slice operations, because + # you can't slice to 0th element inclusive using backward + # slicing. + t_eval_step = t_eval[t_eval_i_new:t_eval_i][::-1] + + if t_eval_step.size > 0: + if sol is None: + sol = solver.dense_output() + ts.append(t_eval_step) + ys.append(sol(t_eval_step)) + t_eval_i = t_eval_i_new + + if t_eval is not None and dense_output: + ti.append(t) + + message = MESSAGES.get(status, message) + + if t_events is not None: + t_events = [np.asarray(te) for te in t_events] + y_events = [np.asarray(ye) for ye in y_events] + + if t_eval is None: + ts = np.array(ts) + ys = np.vstack(ys).T + elif ts: + ts = np.hstack(ts) + ys = np.hstack(ys) + + if dense_output: + if t_eval is None: + sol = OdeSolution( + ts, interpolants, alt_segment=True if method in [BDF, LSODA] else False + ) + else: + sol = OdeSolution( + ti, interpolants, alt_segment=True if method in [BDF, LSODA] else False + ) + else: + sol = None + + return OdeResult(t=ts, y=ys, sol=sol, t_events=t_events, y_events=y_events, + nfev=solver.nfev, njev=solver.njev, nlu=solver.nlu, + status=status, message=message, success=status >= 0) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/lsoda.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/lsoda.py new file mode 100644 index 0000000000000000000000000000000000000000..2a5a7c530c04eddc9beff44e2d4f6df439d5ef01 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/lsoda.py @@ -0,0 +1,224 @@ +import numpy as np +from scipy.integrate import ode +from .common import validate_tol, validate_first_step, warn_extraneous +from .base import OdeSolver, DenseOutput + + +class LSODA(OdeSolver): + """Adams/BDF method with automatic stiffness detection and switching. + + This is a wrapper to the Fortran solver from ODEPACK [1]_. It switches + automatically between the nonstiff Adams method and the stiff BDF method. + The method was originally detailed in [2]_. + + Parameters + ---------- + fun : callable + Right-hand side of the system: the time derivative of the state ``y`` + at time ``t``. The calling signature is ``fun(t, y)``, where ``t`` is a + scalar and ``y`` is an ndarray with ``len(y) = len(y0)``. ``fun`` must + return an array of the same shape as ``y``. See `vectorized` for more + information. + t0 : float + Initial time. + y0 : array_like, shape (n,) + Initial state. + t_bound : float + Boundary time - the integration won't continue beyond it. It also + determines the direction of the integration. + first_step : float or None, optional + Initial step size. Default is ``None`` which means that the algorithm + should choose. + min_step : float, optional + Minimum allowed step size. Default is 0.0, i.e., the step size is not + bounded and determined solely by the solver. + max_step : float, optional + Maximum allowed step size. Default is np.inf, i.e., the step size is not + bounded and determined solely by the solver. + rtol, atol : float and array_like, optional + Relative and absolute tolerances. The solver keeps the local error + estimates less than ``atol + rtol * abs(y)``. Here `rtol` controls a + relative accuracy (number of correct digits), while `atol` controls + absolute accuracy (number of correct decimal places). To achieve the + desired `rtol`, set `atol` to be smaller than the smallest value that + can be expected from ``rtol * abs(y)`` so that `rtol` dominates the + allowable error. If `atol` is larger than ``rtol * abs(y)`` the + number of correct digits is not guaranteed. Conversely, to achieve the + desired `atol` set `rtol` such that ``rtol * abs(y)`` is always smaller + than `atol`. If components of y have different scales, it might be + beneficial to set different `atol` values for different components by + passing array_like with shape (n,) for `atol`. Default values are + 1e-3 for `rtol` and 1e-6 for `atol`. + jac : None or callable, optional + Jacobian matrix of the right-hand side of the system with respect to + ``y``. The Jacobian matrix has shape (n, n) and its element (i, j) is + equal to ``d f_i / d y_j``. The function will be called as + ``jac(t, y)``. If None (default), the Jacobian will be + approximated by finite differences. It is generally recommended to + provide the Jacobian rather than relying on a finite-difference + approximation. + lband, uband : int or None + Parameters defining the bandwidth of the Jacobian, + i.e., ``jac[i, j] != 0 only for i - lband <= j <= i + uband``. Setting + these requires your jac routine to return the Jacobian in the packed format: + the returned array must have ``n`` columns and ``uband + lband + 1`` + rows in which Jacobian diagonals are written. Specifically + ``jac_packed[uband + i - j , j] = jac[i, j]``. The same format is used + in `scipy.linalg.solve_banded` (check for an illustration). + These parameters can be also used with ``jac=None`` to reduce the + number of Jacobian elements estimated by finite differences. + vectorized : bool, optional + Whether `fun` may be called in a vectorized fashion. False (default) + is recommended for this solver. + + If ``vectorized`` is False, `fun` will always be called with ``y`` of + shape ``(n,)``, where ``n = len(y0)``. + + If ``vectorized`` is True, `fun` may be called with ``y`` of shape + ``(n, k)``, where ``k`` is an integer. In this case, `fun` must behave + such that ``fun(t, y)[:, i] == fun(t, y[:, i])`` (i.e. each column of + the returned array is the time derivative of the state corresponding + with a column of ``y``). + + Setting ``vectorized=True`` allows for faster finite difference + approximation of the Jacobian by methods 'Radau' and 'BDF', but + will result in slower execution for this solver. + + Attributes + ---------- + n : int + Number of equations. + status : string + Current status of the solver: 'running', 'finished' or 'failed'. + t_bound : float + Boundary time. + direction : float + Integration direction: +1 or -1. + t : float + Current time. + y : ndarray + Current state. + t_old : float + Previous time. None if no steps were made yet. + nfev : int + Number of evaluations of the right-hand side. + njev : int + Number of evaluations of the Jacobian. + + References + ---------- + .. [1] A. C. Hindmarsh, "ODEPACK, A Systematized Collection of ODE + Solvers," IMACS Transactions on Scientific Computation, Vol 1., + pp. 55-64, 1983. + .. [2] L. Petzold, "Automatic selection of methods for solving stiff and + nonstiff systems of ordinary differential equations", SIAM Journal + on Scientific and Statistical Computing, Vol. 4, No. 1, pp. 136-148, + 1983. + """ + def __init__(self, fun, t0, y0, t_bound, first_step=None, min_step=0.0, + max_step=np.inf, rtol=1e-3, atol=1e-6, jac=None, lband=None, + uband=None, vectorized=False, **extraneous): + warn_extraneous(extraneous) + super().__init__(fun, t0, y0, t_bound, vectorized) + + if first_step is None: + first_step = 0 # LSODA value for automatic selection. + else: + first_step = validate_first_step(first_step, t0, t_bound) + + first_step *= self.direction + + if max_step == np.inf: + max_step = 0 # LSODA value for infinity. + elif max_step <= 0: + raise ValueError("`max_step` must be positive.") + + if min_step < 0: + raise ValueError("`min_step` must be nonnegative.") + + rtol, atol = validate_tol(rtol, atol, self.n) + + solver = ode(self.fun, jac) + solver.set_integrator('lsoda', rtol=rtol, atol=atol, max_step=max_step, + min_step=min_step, first_step=first_step, + lband=lband, uband=uband) + solver.set_initial_value(y0, t0) + + # Inject t_bound into rwork array as needed for itask=5. + solver._integrator.rwork[0] = self.t_bound + solver._integrator.call_args[4] = solver._integrator.rwork + + self._lsoda_solver = solver + + def _step_impl(self): + solver = self._lsoda_solver + integrator = solver._integrator + + # From lsoda.step and lsoda.integrate itask=5 means take a single + # step and do not go past t_bound. + itask = integrator.call_args[2] + integrator.call_args[2] = 5 + solver._y, solver.t = integrator.run( + solver.f, solver.jac or (lambda: None), solver._y, solver.t, + self.t_bound, solver.f_params, solver.jac_params) + integrator.call_args[2] = itask + + if solver.successful(): + self.t = solver.t + self.y = solver._y + # From LSODA Fortran source njev is equal to nlu. + self.njev = integrator.iwork[12] + self.nlu = integrator.iwork[12] + return True, None + else: + return False, 'Unexpected istate in LSODA.' + + def _dense_output_impl(self): + iwork = self._lsoda_solver._integrator.iwork + rwork = self._lsoda_solver._integrator.rwork + + # We want to produce the Nordsieck history array, yh, up to the order + # used in the last successful iteration. The step size is unimportant + # because it will be scaled out in LsodaDenseOutput. Some additional + # work may be required because ODEPACK's LSODA implementation produces + # the Nordsieck history in the state needed for the next iteration. + + # iwork[13] contains order from last successful iteration, while + # iwork[14] contains order to be attempted next. + order = iwork[13] + + # rwork[11] contains the step size to be attempted next, while + # rwork[10] contains step size from last successful iteration. + h = rwork[11] + + # rwork[20:20 + (iwork[14] + 1) * self.n] contains entries of the + # Nordsieck array in state needed for next iteration. We want + # the entries up to order for the last successful step so use the + # following. + yh = np.reshape(rwork[20:20 + (order + 1) * self.n], + (self.n, order + 1), order='F').copy() + if iwork[14] < order: + # If the order is set to decrease then the final column of yh + # has not been updated within ODEPACK's LSODA + # implementation because this column will not be used in the + # next iteration. We must rescale this column to make the + # associated step size consistent with the other columns. + yh[:, -1] *= (h / rwork[10]) ** order + + return LsodaDenseOutput(self.t_old, self.t, h, order, yh) + + +class LsodaDenseOutput(DenseOutput): + def __init__(self, t_old, t, h, order, yh): + super().__init__(t_old, t) + self.h = h + self.yh = yh + self.p = np.arange(order + 1) + + def _call_impl(self, t): + if t.ndim == 0: + x = ((t - self.t) / self.h) ** self.p + else: + x = ((t - self.t) / self.h) ** self.p[:, None] + + return np.dot(self.yh, x) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/radau.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/radau.py new file mode 100644 index 0000000000000000000000000000000000000000..0d572b48de51ebc7e8f8fd278ce1000bdef581b5 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/radau.py @@ -0,0 +1,572 @@ +import numpy as np +from scipy.linalg import lu_factor, lu_solve +from scipy.sparse import csc_matrix, issparse, eye +from scipy.sparse.linalg import splu +from scipy.optimize._numdiff import group_columns +from .common import (validate_max_step, validate_tol, select_initial_step, + norm, num_jac, EPS, warn_extraneous, + validate_first_step) +from .base import OdeSolver, DenseOutput + +S6 = 6 ** 0.5 + +# Butcher tableau. A is not used directly, see below. +C = np.array([(4 - S6) / 10, (4 + S6) / 10, 1]) +E = np.array([-13 - 7 * S6, -13 + 7 * S6, -1]) / 3 + +# Eigendecomposition of A is done: A = T L T**-1. There is 1 real eigenvalue +# and a complex conjugate pair. They are written below. +MU_REAL = 3 + 3 ** (2 / 3) - 3 ** (1 / 3) +MU_COMPLEX = (3 + 0.5 * (3 ** (1 / 3) - 3 ** (2 / 3)) + - 0.5j * (3 ** (5 / 6) + 3 ** (7 / 6))) + +# These are transformation matrices. +T = np.array([ + [0.09443876248897524, -0.14125529502095421, 0.03002919410514742], + [0.25021312296533332, 0.20412935229379994, -0.38294211275726192], + [1, 1, 0]]) +TI = np.array([ + [4.17871859155190428, 0.32768282076106237, 0.52337644549944951], + [-4.17871859155190428, -0.32768282076106237, 0.47662355450055044], + [0.50287263494578682, -2.57192694985560522, 0.59603920482822492]]) +# These linear combinations are used in the algorithm. +TI_REAL = TI[0] +TI_COMPLEX = TI[1] + 1j * TI[2] + +# Interpolator coefficients. +P = np.array([ + [13/3 + 7*S6/3, -23/3 - 22*S6/3, 10/3 + 5 * S6], + [13/3 - 7*S6/3, -23/3 + 22*S6/3, 10/3 - 5 * S6], + [1/3, -8/3, 10/3]]) + + +NEWTON_MAXITER = 6 # Maximum number of Newton iterations. +MIN_FACTOR = 0.2 # Minimum allowed decrease in a step size. +MAX_FACTOR = 10 # Maximum allowed increase in a step size. + + +def solve_collocation_system(fun, t, y, h, Z0, scale, tol, + LU_real, LU_complex, solve_lu): + """Solve the collocation system. + + Parameters + ---------- + fun : callable + Right-hand side of the system. + t : float + Current time. + y : ndarray, shape (n,) + Current state. + h : float + Step to try. + Z0 : ndarray, shape (3, n) + Initial guess for the solution. It determines new values of `y` at + ``t + h * C`` as ``y + Z0``, where ``C`` is the Radau method constants. + scale : ndarray, shape (n) + Problem tolerance scale, i.e. ``rtol * abs(y) + atol``. + tol : float + Tolerance to which solve the system. This value is compared with + the normalized by `scale` error. + LU_real, LU_complex + LU decompositions of the system Jacobians. + solve_lu : callable + Callable which solves a linear system given a LU decomposition. The + signature is ``solve_lu(LU, b)``. + + Returns + ------- + converged : bool + Whether iterations converged. + n_iter : int + Number of completed iterations. + Z : ndarray, shape (3, n) + Found solution. + rate : float + The rate of convergence. + """ + n = y.shape[0] + M_real = MU_REAL / h + M_complex = MU_COMPLEX / h + + W = TI.dot(Z0) + Z = Z0 + + F = np.empty((3, n)) + ch = h * C + + dW_norm_old = None + dW = np.empty_like(W) + converged = False + rate = None + for k in range(NEWTON_MAXITER): + for i in range(3): + F[i] = fun(t + ch[i], y + Z[i]) + + if not np.all(np.isfinite(F)): + break + + f_real = F.T.dot(TI_REAL) - M_real * W[0] + f_complex = F.T.dot(TI_COMPLEX) - M_complex * (W[1] + 1j * W[2]) + + dW_real = solve_lu(LU_real, f_real) + dW_complex = solve_lu(LU_complex, f_complex) + + dW[0] = dW_real + dW[1] = dW_complex.real + dW[2] = dW_complex.imag + + dW_norm = norm(dW / scale) + if dW_norm_old is not None: + rate = dW_norm / dW_norm_old + + if (rate is not None and (rate >= 1 or + rate ** (NEWTON_MAXITER - k) / (1 - rate) * dW_norm > tol)): + break + + W += dW + Z = T.dot(W) + + if (dW_norm == 0 or + rate is not None and rate / (1 - rate) * dW_norm < tol): + converged = True + break + + dW_norm_old = dW_norm + + return converged, k + 1, Z, rate + + +def predict_factor(h_abs, h_abs_old, error_norm, error_norm_old): + """Predict by which factor to increase/decrease the step size. + + The algorithm is described in [1]_. + + Parameters + ---------- + h_abs, h_abs_old : float + Current and previous values of the step size, `h_abs_old` can be None + (see Notes). + error_norm, error_norm_old : float + Current and previous values of the error norm, `error_norm_old` can + be None (see Notes). + + Returns + ------- + factor : float + Predicted factor. + + Notes + ----- + If `h_abs_old` and `error_norm_old` are both not None then a two-step + algorithm is used, otherwise a one-step algorithm is used. + + References + ---------- + .. [1] E. Hairer, S. P. Norsett G. Wanner, "Solving Ordinary Differential + Equations II: Stiff and Differential-Algebraic Problems", Sec. IV.8. + """ + if error_norm_old is None or h_abs_old is None or error_norm == 0: + multiplier = 1 + else: + multiplier = h_abs / h_abs_old * (error_norm_old / error_norm) ** 0.25 + + with np.errstate(divide='ignore'): + factor = min(1, multiplier) * error_norm ** -0.25 + + return factor + + +class Radau(OdeSolver): + """Implicit Runge-Kutta method of Radau IIA family of order 5. + + The implementation follows [1]_. The error is controlled with a + third-order accurate embedded formula. A cubic polynomial which satisfies + the collocation conditions is used for the dense output. + + Parameters + ---------- + fun : callable + Right-hand side of the system: the time derivative of the state ``y`` + at time ``t``. The calling signature is ``fun(t, y)``, where ``t`` is a + scalar and ``y`` is an ndarray with ``len(y) = len(y0)``. ``fun`` must + return an array of the same shape as ``y``. See `vectorized` for more + information. + t0 : float + Initial time. + y0 : array_like, shape (n,) + Initial state. + t_bound : float + Boundary time - the integration won't continue beyond it. It also + determines the direction of the integration. + first_step : float or None, optional + Initial step size. Default is ``None`` which means that the algorithm + should choose. + max_step : float, optional + Maximum allowed step size. Default is np.inf, i.e., the step size is not + bounded and determined solely by the solver. + rtol, atol : float and array_like, optional + Relative and absolute tolerances. The solver keeps the local error + estimates less than ``atol + rtol * abs(y)``. HHere `rtol` controls a + relative accuracy (number of correct digits), while `atol` controls + absolute accuracy (number of correct decimal places). To achieve the + desired `rtol`, set `atol` to be smaller than the smallest value that + can be expected from ``rtol * abs(y)`` so that `rtol` dominates the + allowable error. If `atol` is larger than ``rtol * abs(y)`` the + number of correct digits is not guaranteed. Conversely, to achieve the + desired `atol` set `rtol` such that ``rtol * abs(y)`` is always smaller + than `atol`. If components of y have different scales, it might be + beneficial to set different `atol` values for different components by + passing array_like with shape (n,) for `atol`. Default values are + 1e-3 for `rtol` and 1e-6 for `atol`. + jac : {None, array_like, sparse_matrix, callable}, optional + Jacobian matrix of the right-hand side of the system with respect to + y, required by this method. The Jacobian matrix has shape (n, n) and + its element (i, j) is equal to ``d f_i / d y_j``. + There are three ways to define the Jacobian: + + * If array_like or sparse_matrix, the Jacobian is assumed to + be constant. + * If callable, the Jacobian is assumed to depend on both + t and y; it will be called as ``jac(t, y)`` as necessary. + For the 'Radau' and 'BDF' methods, the return value might be a + sparse matrix. + * If None (default), the Jacobian will be approximated by + finite differences. + + It is generally recommended to provide the Jacobian rather than + relying on a finite-difference approximation. + jac_sparsity : {None, array_like, sparse matrix}, optional + Defines a sparsity structure of the Jacobian matrix for a + finite-difference approximation. Its shape must be (n, n). This argument + is ignored if `jac` is not `None`. If the Jacobian has only few non-zero + elements in *each* row, providing the sparsity structure will greatly + speed up the computations [2]_. A zero entry means that a corresponding + element in the Jacobian is always zero. If None (default), the Jacobian + is assumed to be dense. + vectorized : bool, optional + Whether `fun` can be called in a vectorized fashion. Default is False. + + If ``vectorized`` is False, `fun` will always be called with ``y`` of + shape ``(n,)``, where ``n = len(y0)``. + + If ``vectorized`` is True, `fun` may be called with ``y`` of shape + ``(n, k)``, where ``k`` is an integer. In this case, `fun` must behave + such that ``fun(t, y)[:, i] == fun(t, y[:, i])`` (i.e. each column of + the returned array is the time derivative of the state corresponding + with a column of ``y``). + + Setting ``vectorized=True`` allows for faster finite difference + approximation of the Jacobian by this method, but may result in slower + execution overall in some circumstances (e.g. small ``len(y0)``). + + Attributes + ---------- + n : int + Number of equations. + status : string + Current status of the solver: 'running', 'finished' or 'failed'. + t_bound : float + Boundary time. + direction : float + Integration direction: +1 or -1. + t : float + Current time. + y : ndarray + Current state. + t_old : float + Previous time. None if no steps were made yet. + step_size : float + Size of the last successful step. None if no steps were made yet. + nfev : int + Number of evaluations of the right-hand side. + njev : int + Number of evaluations of the Jacobian. + nlu : int + Number of LU decompositions. + + References + ---------- + .. [1] E. Hairer, G. Wanner, "Solving Ordinary Differential Equations II: + Stiff and Differential-Algebraic Problems", Sec. IV.8. + .. [2] A. Curtis, M. J. D. Powell, and J. Reid, "On the estimation of + sparse Jacobian matrices", Journal of the Institute of Mathematics + and its Applications, 13, pp. 117-120, 1974. + """ + def __init__(self, fun, t0, y0, t_bound, max_step=np.inf, + rtol=1e-3, atol=1e-6, jac=None, jac_sparsity=None, + vectorized=False, first_step=None, **extraneous): + warn_extraneous(extraneous) + super().__init__(fun, t0, y0, t_bound, vectorized) + self.y_old = None + self.max_step = validate_max_step(max_step) + self.rtol, self.atol = validate_tol(rtol, atol, self.n) + self.f = self.fun(self.t, self.y) + # Select initial step assuming the same order which is used to control + # the error. + if first_step is None: + self.h_abs = select_initial_step( + self.fun, self.t, self.y, t_bound, max_step, self.f, self.direction, + 3, self.rtol, self.atol) + else: + self.h_abs = validate_first_step(first_step, t0, t_bound) + self.h_abs_old = None + self.error_norm_old = None + + self.newton_tol = max(10 * EPS / rtol, min(0.03, rtol ** 0.5)) + self.sol = None + + self.jac_factor = None + self.jac, self.J = self._validate_jac(jac, jac_sparsity) + if issparse(self.J): + def lu(A): + self.nlu += 1 + return splu(A) + + def solve_lu(LU, b): + return LU.solve(b) + + I = eye(self.n, format='csc') + else: + def lu(A): + self.nlu += 1 + return lu_factor(A, overwrite_a=True) + + def solve_lu(LU, b): + return lu_solve(LU, b, overwrite_b=True) + + I = np.identity(self.n) + + self.lu = lu + self.solve_lu = solve_lu + self.I = I + + self.current_jac = True + self.LU_real = None + self.LU_complex = None + self.Z = None + + def _validate_jac(self, jac, sparsity): + t0 = self.t + y0 = self.y + + if jac is None: + if sparsity is not None: + if issparse(sparsity): + sparsity = csc_matrix(sparsity) + groups = group_columns(sparsity) + sparsity = (sparsity, groups) + + def jac_wrapped(t, y, f): + self.njev += 1 + J, self.jac_factor = num_jac(self.fun_vectorized, t, y, f, + self.atol, self.jac_factor, + sparsity) + return J + J = jac_wrapped(t0, y0, self.f) + elif callable(jac): + J = jac(t0, y0) + self.njev = 1 + if issparse(J): + J = csc_matrix(J) + + def jac_wrapped(t, y, _=None): + self.njev += 1 + return csc_matrix(jac(t, y), dtype=float) + + else: + J = np.asarray(J, dtype=float) + + def jac_wrapped(t, y, _=None): + self.njev += 1 + return np.asarray(jac(t, y), dtype=float) + + if J.shape != (self.n, self.n): + raise ValueError(f"`jac` is expected to have shape {(self.n, self.n)}," + f" but actually has {J.shape}.") + else: + if issparse(jac): + J = csc_matrix(jac) + else: + J = np.asarray(jac, dtype=float) + + if J.shape != (self.n, self.n): + raise ValueError(f"`jac` is expected to have shape {(self.n, self.n)}," + f" but actually has {J.shape}.") + jac_wrapped = None + + return jac_wrapped, J + + def _step_impl(self): + t = self.t + y = self.y + f = self.f + + max_step = self.max_step + atol = self.atol + rtol = self.rtol + + min_step = 10 * np.abs(np.nextafter(t, self.direction * np.inf) - t) + if self.h_abs > max_step: + h_abs = max_step + h_abs_old = None + error_norm_old = None + elif self.h_abs < min_step: + h_abs = min_step + h_abs_old = None + error_norm_old = None + else: + h_abs = self.h_abs + h_abs_old = self.h_abs_old + error_norm_old = self.error_norm_old + + J = self.J + LU_real = self.LU_real + LU_complex = self.LU_complex + + current_jac = self.current_jac + jac = self.jac + + rejected = False + step_accepted = False + message = None + while not step_accepted: + if h_abs < min_step: + return False, self.TOO_SMALL_STEP + + h = h_abs * self.direction + t_new = t + h + + if self.direction * (t_new - self.t_bound) > 0: + t_new = self.t_bound + + h = t_new - t + h_abs = np.abs(h) + + if self.sol is None: + Z0 = np.zeros((3, y.shape[0])) + else: + Z0 = self.sol(t + h * C).T - y + + scale = atol + np.abs(y) * rtol + + converged = False + while not converged: + if LU_real is None or LU_complex is None: + LU_real = self.lu(MU_REAL / h * self.I - J) + LU_complex = self.lu(MU_COMPLEX / h * self.I - J) + + converged, n_iter, Z, rate = solve_collocation_system( + self.fun, t, y, h, Z0, scale, self.newton_tol, + LU_real, LU_complex, self.solve_lu) + + if not converged: + if current_jac: + break + + J = self.jac(t, y, f) + current_jac = True + LU_real = None + LU_complex = None + + if not converged: + h_abs *= 0.5 + LU_real = None + LU_complex = None + continue + + y_new = y + Z[-1] + ZE = Z.T.dot(E) / h + error = self.solve_lu(LU_real, f + ZE) + scale = atol + np.maximum(np.abs(y), np.abs(y_new)) * rtol + error_norm = norm(error / scale) + safety = 0.9 * (2 * NEWTON_MAXITER + 1) / (2 * NEWTON_MAXITER + + n_iter) + + if rejected and error_norm > 1: + error = self.solve_lu(LU_real, self.fun(t, y + error) + ZE) + error_norm = norm(error / scale) + + if error_norm > 1: + factor = predict_factor(h_abs, h_abs_old, + error_norm, error_norm_old) + h_abs *= max(MIN_FACTOR, safety * factor) + + LU_real = None + LU_complex = None + rejected = True + else: + step_accepted = True + + recompute_jac = jac is not None and n_iter > 2 and rate > 1e-3 + + factor = predict_factor(h_abs, h_abs_old, error_norm, error_norm_old) + factor = min(MAX_FACTOR, safety * factor) + + if not recompute_jac and factor < 1.2: + factor = 1 + else: + LU_real = None + LU_complex = None + + f_new = self.fun(t_new, y_new) + if recompute_jac: + J = jac(t_new, y_new, f_new) + current_jac = True + elif jac is not None: + current_jac = False + + self.h_abs_old = self.h_abs + self.error_norm_old = error_norm + + self.h_abs = h_abs * factor + + self.y_old = y + + self.t = t_new + self.y = y_new + self.f = f_new + + self.Z = Z + + self.LU_real = LU_real + self.LU_complex = LU_complex + self.current_jac = current_jac + self.J = J + + self.t_old = t + self.sol = self._compute_dense_output() + + return step_accepted, message + + def _compute_dense_output(self): + Q = np.dot(self.Z.T, P) + return RadauDenseOutput(self.t_old, self.t, self.y_old, Q) + + def _dense_output_impl(self): + return self.sol + + +class RadauDenseOutput(DenseOutput): + def __init__(self, t_old, t, y_old, Q): + super().__init__(t_old, t) + self.h = t - t_old + self.Q = Q + self.order = Q.shape[1] - 1 + self.y_old = y_old + + def _call_impl(self, t): + x = (t - self.t_old) / self.h + if t.ndim == 0: + p = np.tile(x, self.order + 1) + p = np.cumprod(p) + else: + p = np.tile(x, (self.order + 1, 1)) + p = np.cumprod(p, axis=0) + # Here we don't multiply by h, not a mistake. + y = np.dot(self.Q, p) + if y.ndim == 2: + y += self.y_old[:, None] + else: + y += self.y_old + + return y diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/rk.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/rk.py new file mode 100644 index 0000000000000000000000000000000000000000..62a5347ffe91afc754e9b818d0b34c010d0c4d12 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/rk.py @@ -0,0 +1,601 @@ +import numpy as np +from .base import OdeSolver, DenseOutput +from .common import (validate_max_step, validate_tol, select_initial_step, + norm, warn_extraneous, validate_first_step) +from . import dop853_coefficients + +# Multiply steps computed from asymptotic behaviour of errors by this. +SAFETY = 0.9 + +MIN_FACTOR = 0.2 # Minimum allowed decrease in a step size. +MAX_FACTOR = 10 # Maximum allowed increase in a step size. + + +def rk_step(fun, t, y, f, h, A, B, C, K): + """Perform a single Runge-Kutta step. + + This function computes a prediction of an explicit Runge-Kutta method and + also estimates the error of a less accurate method. + + Notation for Butcher tableau is as in [1]_. + + Parameters + ---------- + fun : callable + Right-hand side of the system. + t : float + Current time. + y : ndarray, shape (n,) + Current state. + f : ndarray, shape (n,) + Current value of the derivative, i.e., ``fun(x, y)``. + h : float + Step to use. + A : ndarray, shape (n_stages, n_stages) + Coefficients for combining previous RK stages to compute the next + stage. For explicit methods the coefficients at and above the main + diagonal are zeros. + B : ndarray, shape (n_stages,) + Coefficients for combining RK stages for computing the final + prediction. + C : ndarray, shape (n_stages,) + Coefficients for incrementing time for consecutive RK stages. + The value for the first stage is always zero. + K : ndarray, shape (n_stages + 1, n) + Storage array for putting RK stages here. Stages are stored in rows. + The last row is a linear combination of the previous rows with + coefficients + + Returns + ------- + y_new : ndarray, shape (n,) + Solution at t + h computed with a higher accuracy. + f_new : ndarray, shape (n,) + Derivative ``fun(t + h, y_new)``. + + References + ---------- + .. [1] E. Hairer, S. P. Norsett G. Wanner, "Solving Ordinary Differential + Equations I: Nonstiff Problems", Sec. II.4. + """ + K[0] = f + for s, (a, c) in enumerate(zip(A[1:], C[1:]), start=1): + dy = np.dot(K[:s].T, a[:s]) * h + K[s] = fun(t + c * h, y + dy) + + y_new = y + h * np.dot(K[:-1].T, B) + f_new = fun(t + h, y_new) + + K[-1] = f_new + + return y_new, f_new + + +class RungeKutta(OdeSolver): + """Base class for explicit Runge-Kutta methods.""" + C: np.ndarray = NotImplemented + A: np.ndarray = NotImplemented + B: np.ndarray = NotImplemented + E: np.ndarray = NotImplemented + P: np.ndarray = NotImplemented + order: int = NotImplemented + error_estimator_order: int = NotImplemented + n_stages: int = NotImplemented + + def __init__(self, fun, t0, y0, t_bound, max_step=np.inf, + rtol=1e-3, atol=1e-6, vectorized=False, + first_step=None, **extraneous): + warn_extraneous(extraneous) + super().__init__(fun, t0, y0, t_bound, vectorized, + support_complex=True) + self.y_old = None + self.max_step = validate_max_step(max_step) + self.rtol, self.atol = validate_tol(rtol, atol, self.n) + self.f = self.fun(self.t, self.y) + if first_step is None: + self.h_abs = select_initial_step( + self.fun, self.t, self.y, t_bound, max_step, self.f, self.direction, + self.error_estimator_order, self.rtol, self.atol) + else: + self.h_abs = validate_first_step(first_step, t0, t_bound) + self.K = np.empty((self.n_stages + 1, self.n), dtype=self.y.dtype) + self.error_exponent = -1 / (self.error_estimator_order + 1) + self.h_previous = None + + def _estimate_error(self, K, h): + return np.dot(K.T, self.E) * h + + def _estimate_error_norm(self, K, h, scale): + return norm(self._estimate_error(K, h) / scale) + + def _step_impl(self): + t = self.t + y = self.y + + max_step = self.max_step + rtol = self.rtol + atol = self.atol + + min_step = 10 * np.abs(np.nextafter(t, self.direction * np.inf) - t) + + if self.h_abs > max_step: + h_abs = max_step + elif self.h_abs < min_step: + h_abs = min_step + else: + h_abs = self.h_abs + + step_accepted = False + step_rejected = False + + while not step_accepted: + if h_abs < min_step: + return False, self.TOO_SMALL_STEP + + h = h_abs * self.direction + t_new = t + h + + if self.direction * (t_new - self.t_bound) > 0: + t_new = self.t_bound + + h = t_new - t + h_abs = np.abs(h) + + y_new, f_new = rk_step(self.fun, t, y, self.f, h, self.A, + self.B, self.C, self.K) + scale = atol + np.maximum(np.abs(y), np.abs(y_new)) * rtol + error_norm = self._estimate_error_norm(self.K, h, scale) + + if error_norm < 1: + if error_norm == 0: + factor = MAX_FACTOR + else: + factor = min(MAX_FACTOR, + SAFETY * error_norm ** self.error_exponent) + + if step_rejected: + factor = min(1, factor) + + h_abs *= factor + + step_accepted = True + else: + h_abs *= max(MIN_FACTOR, + SAFETY * error_norm ** self.error_exponent) + step_rejected = True + + self.h_previous = h + self.y_old = y + + self.t = t_new + self.y = y_new + + self.h_abs = h_abs + self.f = f_new + + return True, None + + def _dense_output_impl(self): + Q = self.K.T.dot(self.P) + return RkDenseOutput(self.t_old, self.t, self.y_old, Q) + + +class RK23(RungeKutta): + """Explicit Runge-Kutta method of order 3(2). + + This uses the Bogacki-Shampine pair of formulas [1]_. The error is controlled + assuming accuracy of the second-order method, but steps are taken using the + third-order accurate formula (local extrapolation is done). A cubic Hermite + polynomial is used for the dense output. + + Can be applied in the complex domain. + + Parameters + ---------- + fun : callable + Right-hand side of the system: the time derivative of the state ``y`` + at time ``t``. The calling signature is ``fun(t, y)``, where ``t`` is a + scalar and ``y`` is an ndarray with ``len(y) = len(y0)``. ``fun`` must + return an array of the same shape as ``y``. See `vectorized` for more + information. + t0 : float + Initial time. + y0 : array_like, shape (n,) + Initial state. + t_bound : float + Boundary time - the integration won't continue beyond it. It also + determines the direction of the integration. + first_step : float or None, optional + Initial step size. Default is ``None`` which means that the algorithm + should choose. + max_step : float, optional + Maximum allowed step size. Default is np.inf, i.e., the step size is not + bounded and determined solely by the solver. + rtol, atol : float and array_like, optional + Relative and absolute tolerances. The solver keeps the local error + estimates less than ``atol + rtol * abs(y)``. Here `rtol` controls a + relative accuracy (number of correct digits), while `atol` controls + absolute accuracy (number of correct decimal places). To achieve the + desired `rtol`, set `atol` to be smaller than the smallest value that + can be expected from ``rtol * abs(y)`` so that `rtol` dominates the + allowable error. If `atol` is larger than ``rtol * abs(y)`` the + number of correct digits is not guaranteed. Conversely, to achieve the + desired `atol` set `rtol` such that ``rtol * abs(y)`` is always smaller + than `atol`. If components of y have different scales, it might be + beneficial to set different `atol` values for different components by + passing array_like with shape (n,) for `atol`. Default values are + 1e-3 for `rtol` and 1e-6 for `atol`. + vectorized : bool, optional + Whether `fun` may be called in a vectorized fashion. False (default) + is recommended for this solver. + + If ``vectorized`` is False, `fun` will always be called with ``y`` of + shape ``(n,)``, where ``n = len(y0)``. + + If ``vectorized`` is True, `fun` may be called with ``y`` of shape + ``(n, k)``, where ``k`` is an integer. In this case, `fun` must behave + such that ``fun(t, y)[:, i] == fun(t, y[:, i])`` (i.e. each column of + the returned array is the time derivative of the state corresponding + with a column of ``y``). + + Setting ``vectorized=True`` allows for faster finite difference + approximation of the Jacobian by methods 'Radau' and 'BDF', but + will result in slower execution for this solver. + + Attributes + ---------- + n : int + Number of equations. + status : string + Current status of the solver: 'running', 'finished' or 'failed'. + t_bound : float + Boundary time. + direction : float + Integration direction: +1 or -1. + t : float + Current time. + y : ndarray + Current state. + t_old : float + Previous time. None if no steps were made yet. + step_size : float + Size of the last successful step. None if no steps were made yet. + nfev : int + Number evaluations of the system's right-hand side. + njev : int + Number of evaluations of the Jacobian. + Is always 0 for this solver as it does not use the Jacobian. + nlu : int + Number of LU decompositions. Is always 0 for this solver. + + References + ---------- + .. [1] P. Bogacki, L.F. Shampine, "A 3(2) Pair of Runge-Kutta Formulas", + Appl. Math. Lett. Vol. 2, No. 4. pp. 321-325, 1989. + """ + order = 3 + error_estimator_order = 2 + n_stages = 3 + C = np.array([0, 1/2, 3/4]) + A = np.array([ + [0, 0, 0], + [1/2, 0, 0], + [0, 3/4, 0] + ]) + B = np.array([2/9, 1/3, 4/9]) + E = np.array([5/72, -1/12, -1/9, 1/8]) + P = np.array([[1, -4 / 3, 5 / 9], + [0, 1, -2/3], + [0, 4/3, -8/9], + [0, -1, 1]]) + + +class RK45(RungeKutta): + """Explicit Runge-Kutta method of order 5(4). + + This uses the Dormand-Prince pair of formulas [1]_. The error is controlled + assuming accuracy of the fourth-order method accuracy, but steps are taken + using the fifth-order accurate formula (local extrapolation is done). + A quartic interpolation polynomial is used for the dense output [2]_. + + Can be applied in the complex domain. + + Parameters + ---------- + fun : callable + Right-hand side of the system. The calling signature is ``fun(t, y)``. + Here ``t`` is a scalar, and there are two options for the ndarray ``y``: + It can either have shape (n,); then ``fun`` must return array_like with + shape (n,). Alternatively it can have shape (n, k); then ``fun`` + must return an array_like with shape (n, k), i.e., each column + corresponds to a single column in ``y``. The choice between the two + options is determined by `vectorized` argument (see below). + t0 : float + Initial time. + y0 : array_like, shape (n,) + Initial state. + t_bound : float + Boundary time - the integration won't continue beyond it. It also + determines the direction of the integration. + first_step : float or None, optional + Initial step size. Default is ``None`` which means that the algorithm + should choose. + max_step : float, optional + Maximum allowed step size. Default is np.inf, i.e., the step size is not + bounded and determined solely by the solver. + rtol, atol : float and array_like, optional + Relative and absolute tolerances. The solver keeps the local error + estimates less than ``atol + rtol * abs(y)``. Here `rtol` controls a + relative accuracy (number of correct digits), while `atol` controls + absolute accuracy (number of correct decimal places). To achieve the + desired `rtol`, set `atol` to be smaller than the smallest value that + can be expected from ``rtol * abs(y)`` so that `rtol` dominates the + allowable error. If `atol` is larger than ``rtol * abs(y)`` the + number of correct digits is not guaranteed. Conversely, to achieve the + desired `atol` set `rtol` such that ``rtol * abs(y)`` is always smaller + than `atol`. If components of y have different scales, it might be + beneficial to set different `atol` values for different components by + passing array_like with shape (n,) for `atol`. Default values are + 1e-3 for `rtol` and 1e-6 for `atol`. + vectorized : bool, optional + Whether `fun` is implemented in a vectorized fashion. Default is False. + + Attributes + ---------- + n : int + Number of equations. + status : string + Current status of the solver: 'running', 'finished' or 'failed'. + t_bound : float + Boundary time. + direction : float + Integration direction: +1 or -1. + t : float + Current time. + y : ndarray + Current state. + t_old : float + Previous time. None if no steps were made yet. + step_size : float + Size of the last successful step. None if no steps were made yet. + nfev : int + Number evaluations of the system's right-hand side. + njev : int + Number of evaluations of the Jacobian. + Is always 0 for this solver as it does not use the Jacobian. + nlu : int + Number of LU decompositions. Is always 0 for this solver. + + References + ---------- + .. [1] J. R. Dormand, P. J. Prince, "A family of embedded Runge-Kutta + formulae", Journal of Computational and Applied Mathematics, Vol. 6, + No. 1, pp. 19-26, 1980. + .. [2] L. W. Shampine, "Some Practical Runge-Kutta Formulas", Mathematics + of Computation,, Vol. 46, No. 173, pp. 135-150, 1986. + """ + order = 5 + error_estimator_order = 4 + n_stages = 6 + C = np.array([0, 1/5, 3/10, 4/5, 8/9, 1]) + A = np.array([ + [0, 0, 0, 0, 0], + [1/5, 0, 0, 0, 0], + [3/40, 9/40, 0, 0, 0], + [44/45, -56/15, 32/9, 0, 0], + [19372/6561, -25360/2187, 64448/6561, -212/729, 0], + [9017/3168, -355/33, 46732/5247, 49/176, -5103/18656] + ]) + B = np.array([35/384, 0, 500/1113, 125/192, -2187/6784, 11/84]) + E = np.array([-71/57600, 0, 71/16695, -71/1920, 17253/339200, -22/525, + 1/40]) + # Corresponds to the optimum value of c_6 from [2]_. + P = np.array([ + [1, -8048581381/2820520608, 8663915743/2820520608, + -12715105075/11282082432], + [0, 0, 0, 0], + [0, 131558114200/32700410799, -68118460800/10900136933, + 87487479700/32700410799], + [0, -1754552775/470086768, 14199869525/1410260304, + -10690763975/1880347072], + [0, 127303824393/49829197408, -318862633887/49829197408, + 701980252875 / 199316789632], + [0, -282668133/205662961, 2019193451/616988883, -1453857185/822651844], + [0, 40617522/29380423, -110615467/29380423, 69997945/29380423]]) + + +class DOP853(RungeKutta): + """Explicit Runge-Kutta method of order 8. + + This is a Python implementation of "DOP853" algorithm originally written + in Fortran [1]_, [2]_. Note that this is not a literal translation, but + the algorithmic core and coefficients are the same. + + Can be applied in the complex domain. + + Parameters + ---------- + fun : callable + Right-hand side of the system. The calling signature is ``fun(t, y)``. + Here, ``t`` is a scalar, and there are two options for the ndarray ``y``: + It can either have shape (n,); then ``fun`` must return array_like with + shape (n,). Alternatively it can have shape (n, k); then ``fun`` + must return an array_like with shape (n, k), i.e. each column + corresponds to a single column in ``y``. The choice between the two + options is determined by `vectorized` argument (see below). + t0 : float + Initial time. + y0 : array_like, shape (n,) + Initial state. + t_bound : float + Boundary time - the integration won't continue beyond it. It also + determines the direction of the integration. + first_step : float or None, optional + Initial step size. Default is ``None`` which means that the algorithm + should choose. + max_step : float, optional + Maximum allowed step size. Default is np.inf, i.e. the step size is not + bounded and determined solely by the solver. + rtol, atol : float and array_like, optional + Relative and absolute tolerances. The solver keeps the local error + estimates less than ``atol + rtol * abs(y)``. Here `rtol` controls a + relative accuracy (number of correct digits), while `atol` controls + absolute accuracy (number of correct decimal places). To achieve the + desired `rtol`, set `atol` to be smaller than the smallest value that + can be expected from ``rtol * abs(y)`` so that `rtol` dominates the + allowable error. If `atol` is larger than ``rtol * abs(y)`` the + number of correct digits is not guaranteed. Conversely, to achieve the + desired `atol` set `rtol` such that ``rtol * abs(y)`` is always smaller + than `atol`. If components of y have different scales, it might be + beneficial to set different `atol` values for different components by + passing array_like with shape (n,) for `atol`. Default values are + 1e-3 for `rtol` and 1e-6 for `atol`. + vectorized : bool, optional + Whether `fun` is implemented in a vectorized fashion. Default is False. + + Attributes + ---------- + n : int + Number of equations. + status : string + Current status of the solver: 'running', 'finished' or 'failed'. + t_bound : float + Boundary time. + direction : float + Integration direction: +1 or -1. + t : float + Current time. + y : ndarray + Current state. + t_old : float + Previous time. None if no steps were made yet. + step_size : float + Size of the last successful step. None if no steps were made yet. + nfev : int + Number evaluations of the system's right-hand side. + njev : int + Number of evaluations of the Jacobian. Is always 0 for this solver + as it does not use the Jacobian. + nlu : int + Number of LU decompositions. Is always 0 for this solver. + + References + ---------- + .. [1] E. Hairer, S. P. Norsett G. Wanner, "Solving Ordinary Differential + Equations I: Nonstiff Problems", Sec. II. + .. [2] `Page with original Fortran code of DOP853 + `_. + """ + n_stages = dop853_coefficients.N_STAGES + order = 8 + error_estimator_order = 7 + A = dop853_coefficients.A[:n_stages, :n_stages] + B = dop853_coefficients.B + C = dop853_coefficients.C[:n_stages] + E3 = dop853_coefficients.E3 + E5 = dop853_coefficients.E5 + D = dop853_coefficients.D + + A_EXTRA = dop853_coefficients.A[n_stages + 1:] + C_EXTRA = dop853_coefficients.C[n_stages + 1:] + + def __init__(self, fun, t0, y0, t_bound, max_step=np.inf, + rtol=1e-3, atol=1e-6, vectorized=False, + first_step=None, **extraneous): + super().__init__(fun, t0, y0, t_bound, max_step, rtol, atol, + vectorized, first_step, **extraneous) + self.K_extended = np.empty((dop853_coefficients.N_STAGES_EXTENDED, + self.n), dtype=self.y.dtype) + self.K = self.K_extended[:self.n_stages + 1] + + def _estimate_error(self, K, h): # Left for testing purposes. + err5 = np.dot(K.T, self.E5) + err3 = np.dot(K.T, self.E3) + denom = np.hypot(np.abs(err5), 0.1 * np.abs(err3)) + correction_factor = np.ones_like(err5) + mask = denom > 0 + correction_factor[mask] = np.abs(err5[mask]) / denom[mask] + return h * err5 * correction_factor + + def _estimate_error_norm(self, K, h, scale): + err5 = np.dot(K.T, self.E5) / scale + err3 = np.dot(K.T, self.E3) / scale + err5_norm_2 = np.linalg.norm(err5)**2 + err3_norm_2 = np.linalg.norm(err3)**2 + if err5_norm_2 == 0 and err3_norm_2 == 0: + return 0.0 + denom = err5_norm_2 + 0.01 * err3_norm_2 + return np.abs(h) * err5_norm_2 / np.sqrt(denom * len(scale)) + + def _dense_output_impl(self): + K = self.K_extended + h = self.h_previous + for s, (a, c) in enumerate(zip(self.A_EXTRA, self.C_EXTRA), + start=self.n_stages + 1): + dy = np.dot(K[:s].T, a[:s]) * h + K[s] = self.fun(self.t_old + c * h, self.y_old + dy) + + F = np.empty((dop853_coefficients.INTERPOLATOR_POWER, self.n), + dtype=self.y_old.dtype) + + f_old = K[0] + delta_y = self.y - self.y_old + + F[0] = delta_y + F[1] = h * f_old - delta_y + F[2] = 2 * delta_y - h * (self.f + f_old) + F[3:] = h * np.dot(self.D, K) + + return Dop853DenseOutput(self.t_old, self.t, self.y_old, F) + + +class RkDenseOutput(DenseOutput): + def __init__(self, t_old, t, y_old, Q): + super().__init__(t_old, t) + self.h = t - t_old + self.Q = Q + self.order = Q.shape[1] - 1 + self.y_old = y_old + + def _call_impl(self, t): + x = (t - self.t_old) / self.h + if t.ndim == 0: + p = np.tile(x, self.order + 1) + p = np.cumprod(p) + else: + p = np.tile(x, (self.order + 1, 1)) + p = np.cumprod(p, axis=0) + y = self.h * np.dot(self.Q, p) + if y.ndim == 2: + y += self.y_old[:, None] + else: + y += self.y_old + + return y + + +class Dop853DenseOutput(DenseOutput): + def __init__(self, t_old, t, y_old, F): + super().__init__(t_old, t) + self.h = t - t_old + self.F = F + self.y_old = y_old + + def _call_impl(self, t): + x = (t - self.t_old) / self.h + + if t.ndim == 0: + y = np.zeros_like(self.y_old) + else: + x = x[:, None] + y = np.zeros((len(x), len(self.y_old)), dtype=self.y_old.dtype) + + for i, f in enumerate(reversed(self.F)): + y += f + if i % 2 == 0: + y *= x + else: + y *= 1 - x + y += self.y_old + + return y.T diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/tests/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/tests/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/tests/test_ivp.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/tests/test_ivp.py new file mode 100644 index 0000000000000000000000000000000000000000..cd318b9a165051293ac13b9b0e63be2df322963b --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/tests/test_ivp.py @@ -0,0 +1,1287 @@ +from itertools import product +from numpy.testing import (assert_, assert_allclose, assert_array_less, + assert_equal, assert_no_warnings, suppress_warnings) +import pytest +from pytest import raises as assert_raises +import numpy as np +from scipy.optimize._numdiff import group_columns +from scipy.integrate import solve_ivp, RK23, RK45, DOP853, Radau, BDF, LSODA +from scipy.integrate import OdeSolution +from scipy.integrate._ivp.common import num_jac, select_initial_step +from scipy.integrate._ivp.base import ConstantDenseOutput +from scipy.sparse import coo_matrix, csc_matrix + + +def fun_zero(t, y): + return np.zeros_like(y) + + +def fun_linear(t, y): + return np.array([-y[0] - 5 * y[1], y[0] + y[1]]) + + +def jac_linear(): + return np.array([[-1, -5], [1, 1]]) + + +def sol_linear(t): + return np.vstack((-5 * np.sin(2 * t), + 2 * np.cos(2 * t) + np.sin(2 * t))) + + +def fun_rational(t, y): + return np.array([y[1] / t, + y[1] * (y[0] + 2 * y[1] - 1) / (t * (y[0] - 1))]) + + +def fun_rational_vectorized(t, y): + return np.vstack((y[1] / t, + y[1] * (y[0] + 2 * y[1] - 1) / (t * (y[0] - 1)))) + + +def jac_rational(t, y): + return np.array([ + [0, 1 / t], + [-2 * y[1] ** 2 / (t * (y[0] - 1) ** 2), + (y[0] + 4 * y[1] - 1) / (t * (y[0] - 1))] + ]) + + +def jac_rational_sparse(t, y): + return csc_matrix([ + [0, 1 / t], + [-2 * y[1] ** 2 / (t * (y[0] - 1) ** 2), + (y[0] + 4 * y[1] - 1) / (t * (y[0] - 1))] + ]) + + +def sol_rational(t): + return np.asarray((t / (t + 10), 10 * t / (t + 10) ** 2)) + + +def fun_medazko(t, y): + n = y.shape[0] // 2 + k = 100 + c = 4 + + phi = 2 if t <= 5 else 0 + y = np.hstack((phi, 0, y, y[-2])) + + d = 1 / n + j = np.arange(n) + 1 + alpha = 2 * (j * d - 1) ** 3 / c ** 2 + beta = (j * d - 1) ** 4 / c ** 2 + + j_2_p1 = 2 * j + 2 + j_2_m3 = 2 * j - 2 + j_2_m1 = 2 * j + j_2 = 2 * j + 1 + + f = np.empty(2 * n) + f[::2] = (alpha * (y[j_2_p1] - y[j_2_m3]) / (2 * d) + + beta * (y[j_2_m3] - 2 * y[j_2_m1] + y[j_2_p1]) / d ** 2 - + k * y[j_2_m1] * y[j_2]) + f[1::2] = -k * y[j_2] * y[j_2_m1] + + return f + + +def medazko_sparsity(n): + cols = [] + rows = [] + + i = np.arange(n) * 2 + + cols.append(i[1:]) + rows.append(i[1:] - 2) + + cols.append(i) + rows.append(i) + + cols.append(i) + rows.append(i + 1) + + cols.append(i[:-1]) + rows.append(i[:-1] + 2) + + i = np.arange(n) * 2 + 1 + + cols.append(i) + rows.append(i) + + cols.append(i) + rows.append(i - 1) + + cols = np.hstack(cols) + rows = np.hstack(rows) + + return coo_matrix((np.ones_like(cols), (cols, rows))) + + +def fun_complex(t, y): + return -y + + +def jac_complex(t, y): + return -np.eye(y.shape[0]) + + +def jac_complex_sparse(t, y): + return csc_matrix(jac_complex(t, y)) + + +def sol_complex(t): + y = (0.5 + 1j) * np.exp(-t) + return y.reshape((1, -1)) + + +def fun_event_dense_output_LSODA(t, y): + return y * (t - 2) + + +def jac_event_dense_output_LSODA(t, y): + return t - 2 + + +def sol_event_dense_output_LSODA(t): + return np.exp(t ** 2 / 2 - 2 * t + np.log(0.05) - 6) + + +def compute_error(y, y_true, rtol, atol): + e = (y - y_true) / (atol + rtol * np.abs(y_true)) + return np.linalg.norm(e, axis=0) / np.sqrt(e.shape[0]) + +def test_duplicate_timestamps(): + def upward_cannon(t, y): + return [y[1], -9.80665] + + def hit_ground(t, y): + return y[0] + + hit_ground.terminal = True + hit_ground.direction = -1 + + sol = solve_ivp(upward_cannon, [0, np.inf], [0, 0.01], + max_step=0.05 * 0.001 / 9.80665, + events=hit_ground, dense_output=True) + assert_allclose(sol.sol(0.01), np.asarray([-0.00039033, -0.08806632]), + rtol=1e-5, atol=1e-8) + assert_allclose(sol.t_events, np.asarray([[0.00203943]]), rtol=1e-5, atol=1e-8) + assert_allclose(sol.y_events, [np.asarray([[ 0.0, -0.01 ]])], atol=1e-9) + assert sol.success + assert_equal(sol.status, 1) + +@pytest.mark.thread_unsafe +def test_integration(): + rtol = 1e-3 + atol = 1e-6 + y0 = [1/3, 2/9] + + for vectorized, method, t_span, jac in product( + [False, True], + ['RK23', 'RK45', 'DOP853', 'Radau', 'BDF', 'LSODA'], + [[5, 9], [5, 1]], + [None, jac_rational, jac_rational_sparse]): + + if vectorized: + fun = fun_rational_vectorized + else: + fun = fun_rational + + with suppress_warnings() as sup: + sup.filter(UserWarning, + "The following arguments have no effect for a chosen " + "solver: `jac`") + res = solve_ivp(fun, t_span, y0, rtol=rtol, + atol=atol, method=method, dense_output=True, + jac=jac, vectorized=vectorized) + assert_equal(res.t[0], t_span[0]) + assert_(res.t_events is None) + assert_(res.y_events is None) + assert_(res.success) + assert_equal(res.status, 0) + + if method == 'DOP853': + # DOP853 spends more functions evaluation because it doesn't + # have enough time to develop big enough step size. + assert_(res.nfev < 50) + else: + assert_(res.nfev < 40) + + if method in ['RK23', 'RK45', 'DOP853', 'LSODA']: + assert_equal(res.njev, 0) + assert_equal(res.nlu, 0) + else: + assert_(0 < res.njev < 3) + assert_(0 < res.nlu < 10) + + y_true = sol_rational(res.t) + e = compute_error(res.y, y_true, rtol, atol) + assert_(np.all(e < 5)) + + tc = np.linspace(*t_span) + yc_true = sol_rational(tc) + yc = res.sol(tc) + + e = compute_error(yc, yc_true, rtol, atol) + assert_(np.all(e < 5)) + + tc = (t_span[0] + t_span[-1]) / 2 + yc_true = sol_rational(tc) + yc = res.sol(tc) + + e = compute_error(yc, yc_true, rtol, atol) + assert_(np.all(e < 5)) + + assert_allclose(res.sol(res.t), res.y, rtol=1e-15, atol=1e-15) + + +@pytest.mark.thread_unsafe +def test_integration_complex(): + rtol = 1e-3 + atol = 1e-6 + y0 = [0.5 + 1j] + t_span = [0, 1] + tc = np.linspace(t_span[0], t_span[1]) + for method, jac in product(['RK23', 'RK45', 'DOP853', 'BDF'], + [None, jac_complex, jac_complex_sparse]): + with suppress_warnings() as sup: + sup.filter(UserWarning, + "The following arguments have no effect for a chosen " + "solver: `jac`") + res = solve_ivp(fun_complex, t_span, y0, method=method, + dense_output=True, rtol=rtol, atol=atol, jac=jac) + + assert_equal(res.t[0], t_span[0]) + assert_(res.t_events is None) + assert_(res.y_events is None) + assert_(res.success) + assert_equal(res.status, 0) + + if method == 'DOP853': + assert res.nfev < 35 + else: + assert res.nfev < 25 + + if method == 'BDF': + assert_equal(res.njev, 1) + assert res.nlu < 6 + else: + assert res.njev == 0 + assert res.nlu == 0 + + y_true = sol_complex(res.t) + e = compute_error(res.y, y_true, rtol, atol) + assert np.all(e < 5) + + yc_true = sol_complex(tc) + yc = res.sol(tc) + e = compute_error(yc, yc_true, rtol, atol) + + assert np.all(e < 5) + + +@pytest.mark.fail_slow(5) +def test_integration_sparse_difference(): + n = 200 + t_span = [0, 20] + y0 = np.zeros(2 * n) + y0[1::2] = 1 + sparsity = medazko_sparsity(n) + + for method in ['BDF', 'Radau']: + res = solve_ivp(fun_medazko, t_span, y0, method=method, + jac_sparsity=sparsity) + + assert_equal(res.t[0], t_span[0]) + assert_(res.t_events is None) + assert_(res.y_events is None) + assert_(res.success) + assert_equal(res.status, 0) + + assert_allclose(res.y[78, -1], 0.233994e-3, rtol=1e-2) + assert_allclose(res.y[79, -1], 0, atol=1e-3) + assert_allclose(res.y[148, -1], 0.359561e-3, rtol=1e-2) + assert_allclose(res.y[149, -1], 0, atol=1e-3) + assert_allclose(res.y[198, -1], 0.117374129e-3, rtol=1e-2) + assert_allclose(res.y[199, -1], 0.6190807e-5, atol=1e-3) + assert_allclose(res.y[238, -1], 0, atol=1e-3) + assert_allclose(res.y[239, -1], 0.9999997, rtol=1e-2) + + +def test_integration_const_jac(): + rtol = 1e-3 + atol = 1e-6 + y0 = [0, 2] + t_span = [0, 2] + J = jac_linear() + J_sparse = csc_matrix(J) + + for method, jac in product(['Radau', 'BDF'], [J, J_sparse]): + res = solve_ivp(fun_linear, t_span, y0, rtol=rtol, atol=atol, + method=method, dense_output=True, jac=jac) + assert_equal(res.t[0], t_span[0]) + assert_(res.t_events is None) + assert_(res.y_events is None) + assert_(res.success) + assert_equal(res.status, 0) + + assert_(res.nfev < 100) + assert_equal(res.njev, 0) + assert_(0 < res.nlu < 15) + + y_true = sol_linear(res.t) + e = compute_error(res.y, y_true, rtol, atol) + assert_(np.all(e < 10)) + + tc = np.linspace(*t_span) + yc_true = sol_linear(tc) + yc = res.sol(tc) + + e = compute_error(yc, yc_true, rtol, atol) + assert_(np.all(e < 15)) + + assert_allclose(res.sol(res.t), res.y, rtol=1e-14, atol=1e-14) + + +@pytest.mark.slow +@pytest.mark.parametrize('method', ['Radau', 'BDF', 'LSODA']) +def test_integration_stiff(method, num_parallel_threads): + rtol = 1e-6 + atol = 1e-6 + y0 = [1e4, 0, 0] + tspan = [0, 1e8] + + if method == 'LSODA' and num_parallel_threads > 1: + pytest.skip(reason='LSODA does not allow for concurrent calls') + + def fun_robertson(t, state): + x, y, z = state + return [ + -0.04 * x + 1e4 * y * z, + 0.04 * x - 1e4 * y * z - 3e7 * y * y, + 3e7 * y * y, + ] + + res = solve_ivp(fun_robertson, tspan, y0, rtol=rtol, + atol=atol, method=method) + + # If the stiff mode is not activated correctly, these numbers will be much bigger + assert res.nfev < 5000 + assert res.njev < 200 + + +def test_events(num_parallel_threads): + def event_rational_1(t, y): + return y[0] - y[1] ** 0.7 + + def event_rational_2(t, y): + return y[1] ** 0.6 - y[0] + + def event_rational_3(t, y): + return t - 7.4 + + event_rational_3.terminal = True + + for method in ['RK23', 'RK45', 'DOP853', 'Radau', 'BDF', 'LSODA']: + if method == 'LSODA' and num_parallel_threads > 1: + continue + + res = solve_ivp(fun_rational, [5, 8], [1/3, 2/9], method=method, + events=(event_rational_1, event_rational_2)) + assert_equal(res.status, 0) + assert_equal(res.t_events[0].size, 1) + assert_equal(res.t_events[1].size, 1) + assert_(5.3 < res.t_events[0][0] < 5.7) + assert_(7.3 < res.t_events[1][0] < 7.7) + + assert_equal(res.y_events[0].shape, (1, 2)) + assert_equal(res.y_events[1].shape, (1, 2)) + assert np.isclose( + event_rational_1(res.t_events[0][0], res.y_events[0][0]), 0) + assert np.isclose( + event_rational_2(res.t_events[1][0], res.y_events[1][0]), 0) + + event_rational_1.direction = 1 + event_rational_2.direction = 1 + res = solve_ivp(fun_rational, [5, 8], [1 / 3, 2 / 9], method=method, + events=(event_rational_1, event_rational_2)) + assert_equal(res.status, 0) + assert_equal(res.t_events[0].size, 1) + assert_equal(res.t_events[1].size, 0) + assert_(5.3 < res.t_events[0][0] < 5.7) + assert_equal(res.y_events[0].shape, (1, 2)) + assert_equal(res.y_events[1].shape, (0,)) + assert np.isclose( + event_rational_1(res.t_events[0][0], res.y_events[0][0]), 0) + + event_rational_1.direction = -1 + event_rational_2.direction = -1 + res = solve_ivp(fun_rational, [5, 8], [1 / 3, 2 / 9], method=method, + events=(event_rational_1, event_rational_2)) + assert_equal(res.status, 0) + assert_equal(res.t_events[0].size, 0) + assert_equal(res.t_events[1].size, 1) + assert_(7.3 < res.t_events[1][0] < 7.7) + assert_equal(res.y_events[0].shape, (0,)) + assert_equal(res.y_events[1].shape, (1, 2)) + assert np.isclose( + event_rational_2(res.t_events[1][0], res.y_events[1][0]), 0) + + event_rational_1.direction = 0 + event_rational_2.direction = 0 + + res = solve_ivp(fun_rational, [5, 8], [1 / 3, 2 / 9], method=method, + events=(event_rational_1, event_rational_2, + event_rational_3), dense_output=True) + assert_equal(res.status, 1) + assert_equal(res.t_events[0].size, 1) + assert_equal(res.t_events[1].size, 0) + assert_equal(res.t_events[2].size, 1) + assert_(5.3 < res.t_events[0][0] < 5.7) + assert_(7.3 < res.t_events[2][0] < 7.5) + assert_equal(res.y_events[0].shape, (1, 2)) + assert_equal(res.y_events[1].shape, (0,)) + assert_equal(res.y_events[2].shape, (1, 2)) + assert np.isclose( + event_rational_1(res.t_events[0][0], res.y_events[0][0]), 0) + assert np.isclose( + event_rational_3(res.t_events[2][0], res.y_events[2][0]), 0) + + res = solve_ivp(fun_rational, [5, 8], [1 / 3, 2 / 9], method=method, + events=event_rational_1, dense_output=True) + assert_equal(res.status, 0) + assert_equal(res.t_events[0].size, 1) + assert_(5.3 < res.t_events[0][0] < 5.7) + + assert_equal(res.y_events[0].shape, (1, 2)) + assert np.isclose( + event_rational_1(res.t_events[0][0], res.y_events[0][0]), 0) + + # Also test that termination by event doesn't break interpolants. + tc = np.linspace(res.t[0], res.t[-1]) + yc_true = sol_rational(tc) + yc = res.sol(tc) + e = compute_error(yc, yc_true, 1e-3, 1e-6) + assert_(np.all(e < 5)) + + # Test that the y_event matches solution + assert np.allclose(sol_rational(res.t_events[0][0]), res.y_events[0][0], + rtol=1e-3, atol=1e-6) + + # Test in backward direction. + event_rational_1.direction = 0 + event_rational_2.direction = 0 + for method in ['RK23', 'RK45', 'DOP853', 'Radau', 'BDF', 'LSODA']: + if method == 'LSODA' and num_parallel_threads > 1: + continue + + res = solve_ivp(fun_rational, [8, 5], [4/9, 20/81], method=method, + events=(event_rational_1, event_rational_2)) + assert_equal(res.status, 0) + assert_equal(res.t_events[0].size, 1) + assert_equal(res.t_events[1].size, 1) + assert_(5.3 < res.t_events[0][0] < 5.7) + assert_(7.3 < res.t_events[1][0] < 7.7) + + assert_equal(res.y_events[0].shape, (1, 2)) + assert_equal(res.y_events[1].shape, (1, 2)) + assert np.isclose( + event_rational_1(res.t_events[0][0], res.y_events[0][0]), 0) + assert np.isclose( + event_rational_2(res.t_events[1][0], res.y_events[1][0]), 0) + + event_rational_1.direction = -1 + event_rational_2.direction = -1 + res = solve_ivp(fun_rational, [8, 5], [4/9, 20/81], method=method, + events=(event_rational_1, event_rational_2)) + assert_equal(res.status, 0) + assert_equal(res.t_events[0].size, 1) + assert_equal(res.t_events[1].size, 0) + assert_(5.3 < res.t_events[0][0] < 5.7) + + assert_equal(res.y_events[0].shape, (1, 2)) + assert_equal(res.y_events[1].shape, (0,)) + assert np.isclose( + event_rational_1(res.t_events[0][0], res.y_events[0][0]), 0) + + event_rational_1.direction = 1 + event_rational_2.direction = 1 + res = solve_ivp(fun_rational, [8, 5], [4/9, 20/81], method=method, + events=(event_rational_1, event_rational_2)) + assert_equal(res.status, 0) + assert_equal(res.t_events[0].size, 0) + assert_equal(res.t_events[1].size, 1) + assert_(7.3 < res.t_events[1][0] < 7.7) + + assert_equal(res.y_events[0].shape, (0,)) + assert_equal(res.y_events[1].shape, (1, 2)) + assert np.isclose( + event_rational_2(res.t_events[1][0], res.y_events[1][0]), 0) + + event_rational_1.direction = 0 + event_rational_2.direction = 0 + + res = solve_ivp(fun_rational, [8, 5], [4/9, 20/81], method=method, + events=(event_rational_1, event_rational_2, + event_rational_3), dense_output=True) + assert_equal(res.status, 1) + assert_equal(res.t_events[0].size, 0) + assert_equal(res.t_events[1].size, 1) + assert_equal(res.t_events[2].size, 1) + assert_(7.3 < res.t_events[1][0] < 7.7) + assert_(7.3 < res.t_events[2][0] < 7.5) + + assert_equal(res.y_events[0].shape, (0,)) + assert_equal(res.y_events[1].shape, (1, 2)) + assert_equal(res.y_events[2].shape, (1, 2)) + assert np.isclose( + event_rational_2(res.t_events[1][0], res.y_events[1][0]), 0) + assert np.isclose( + event_rational_3(res.t_events[2][0], res.y_events[2][0]), 0) + + # Also test that termination by event doesn't break interpolants. + tc = np.linspace(res.t[-1], res.t[0]) + yc_true = sol_rational(tc) + yc = res.sol(tc) + e = compute_error(yc, yc_true, 1e-3, 1e-6) + assert_(np.all(e < 5)) + + assert np.allclose(sol_rational(res.t_events[1][0]), res.y_events[1][0], + rtol=1e-3, atol=1e-6) + assert np.allclose(sol_rational(res.t_events[2][0]), res.y_events[2][0], + rtol=1e-3, atol=1e-6) + + +def _get_harmonic_oscillator(): + def f(t, y): + return [y[1], -y[0]] + + def event(t, y): + return y[0] + + return f, event + + +@pytest.mark.parametrize('n_events', [3, 4]) +def test_event_terminal_integer(n_events): + f, event = _get_harmonic_oscillator() + event.terminal = n_events + res = solve_ivp(f, (0, 100), [1, 0], events=event) + assert len(res.t_events[0]) == n_events + assert len(res.y_events[0]) == n_events + assert_allclose(res.y_events[0][:, 0], 0, atol=1e-14) + + +def test_event_terminal_iv(): + f, event = _get_harmonic_oscillator() + args = (f, (0, 100), [1, 0]) + + event.terminal = None + res = solve_ivp(*args, events=event) + event.terminal = 0 + ref = solve_ivp(*args, events=event) + assert_allclose(res.t_events, ref.t_events) + + message = "The `terminal` attribute..." + event.terminal = -1 + with pytest.raises(ValueError, match=message): + solve_ivp(*args, events=event) + event.terminal = 3.5 + with pytest.raises(ValueError, match=message): + solve_ivp(*args, events=event) + + +def test_max_step(num_parallel_threads): + rtol = 1e-3 + atol = 1e-6 + y0 = [1/3, 2/9] + for method in [RK23, RK45, DOP853, Radau, BDF, LSODA]: + if method is LSODA and num_parallel_threads > 1: + continue + for t_span in ([5, 9], [5, 1]): + res = solve_ivp(fun_rational, t_span, y0, rtol=rtol, + max_step=0.5, atol=atol, method=method, + dense_output=True) + assert_equal(res.t[0], t_span[0]) + assert_equal(res.t[-1], t_span[-1]) + assert_(np.all(np.abs(np.diff(res.t)) <= 0.5 + 1e-15)) + assert_(res.t_events is None) + assert_(res.success) + assert_equal(res.status, 0) + + y_true = sol_rational(res.t) + e = compute_error(res.y, y_true, rtol, atol) + assert_(np.all(e < 5)) + + tc = np.linspace(*t_span) + yc_true = sol_rational(tc) + yc = res.sol(tc) + + e = compute_error(yc, yc_true, rtol, atol) + assert_(np.all(e < 5)) + + assert_allclose(res.sol(res.t), res.y, rtol=1e-15, atol=1e-15) + + assert_raises(ValueError, method, fun_rational, t_span[0], y0, + t_span[1], max_step=-1) + + if method is not LSODA: + solver = method(fun_rational, t_span[0], y0, t_span[1], + rtol=rtol, atol=atol, max_step=1e-20) + message = solver.step() + message = solver.step() # First step succeeds but second step fails. + assert_equal(solver.status, 'failed') + assert_("step size is less" in message) + assert_raises(RuntimeError, solver.step) + + +def test_first_step(num_parallel_threads): + rtol = 1e-3 + atol = 1e-6 + y0 = [1/3, 2/9] + first_step = 0.1 + for method in [RK23, RK45, DOP853, Radau, BDF, LSODA]: + if method is LSODA and num_parallel_threads > 1: + continue + for t_span in ([5, 9], [5, 1]): + res = solve_ivp(fun_rational, t_span, y0, rtol=rtol, + max_step=0.5, atol=atol, method=method, + dense_output=True, first_step=first_step) + + assert_equal(res.t[0], t_span[0]) + assert_equal(res.t[-1], t_span[-1]) + assert_allclose(first_step, np.abs(res.t[1] - 5)) + assert_(res.t_events is None) + assert_(res.success) + assert_equal(res.status, 0) + + y_true = sol_rational(res.t) + e = compute_error(res.y, y_true, rtol, atol) + assert_(np.all(e < 5)) + + tc = np.linspace(*t_span) + yc_true = sol_rational(tc) + yc = res.sol(tc) + + e = compute_error(yc, yc_true, rtol, atol) + assert_(np.all(e < 5)) + + assert_allclose(res.sol(res.t), res.y, rtol=1e-15, atol=1e-15) + + assert_raises(ValueError, method, fun_rational, t_span[0], y0, + t_span[1], first_step=-1) + assert_raises(ValueError, method, fun_rational, t_span[0], y0, + t_span[1], first_step=5) + + +def test_t_eval(): + rtol = 1e-3 + atol = 1e-6 + y0 = [1/3, 2/9] + for t_span in ([5, 9], [5, 1]): + t_eval = np.linspace(t_span[0], t_span[1], 10) + res = solve_ivp(fun_rational, t_span, y0, rtol=rtol, atol=atol, + t_eval=t_eval) + assert_equal(res.t, t_eval) + assert_(res.t_events is None) + assert_(res.success) + assert_equal(res.status, 0) + + y_true = sol_rational(res.t) + e = compute_error(res.y, y_true, rtol, atol) + assert_(np.all(e < 5)) + + t_eval = [5, 5.01, 7, 8, 8.01, 9] + res = solve_ivp(fun_rational, [5, 9], y0, rtol=rtol, atol=atol, + t_eval=t_eval) + assert_equal(res.t, t_eval) + assert_(res.t_events is None) + assert_(res.success) + assert_equal(res.status, 0) + + y_true = sol_rational(res.t) + e = compute_error(res.y, y_true, rtol, atol) + assert_(np.all(e < 5)) + + t_eval = [5, 4.99, 3, 1.5, 1.1, 1.01, 1] + res = solve_ivp(fun_rational, [5, 1], y0, rtol=rtol, atol=atol, + t_eval=t_eval) + assert_equal(res.t, t_eval) + assert_(res.t_events is None) + assert_(res.success) + assert_equal(res.status, 0) + + t_eval = [5.01, 7, 8, 8.01] + res = solve_ivp(fun_rational, [5, 9], y0, rtol=rtol, atol=atol, + t_eval=t_eval) + assert_equal(res.t, t_eval) + assert_(res.t_events is None) + assert_(res.success) + assert_equal(res.status, 0) + + y_true = sol_rational(res.t) + e = compute_error(res.y, y_true, rtol, atol) + assert_(np.all(e < 5)) + + t_eval = [4.99, 3, 1.5, 1.1, 1.01] + res = solve_ivp(fun_rational, [5, 1], y0, rtol=rtol, atol=atol, + t_eval=t_eval) + assert_equal(res.t, t_eval) + assert_(res.t_events is None) + assert_(res.success) + assert_equal(res.status, 0) + + t_eval = [4, 6] + assert_raises(ValueError, solve_ivp, fun_rational, [5, 9], y0, + rtol=rtol, atol=atol, t_eval=t_eval) + + +def test_t_eval_dense_output(): + rtol = 1e-3 + atol = 1e-6 + y0 = [1/3, 2/9] + t_span = [5, 9] + t_eval = np.linspace(t_span[0], t_span[1], 10) + res = solve_ivp(fun_rational, t_span, y0, rtol=rtol, atol=atol, + t_eval=t_eval) + res_d = solve_ivp(fun_rational, t_span, y0, rtol=rtol, atol=atol, + t_eval=t_eval, dense_output=True) + assert_equal(res.t, t_eval) + assert_(res.t_events is None) + assert_(res.success) + assert_equal(res.status, 0) + + assert_equal(res.t, res_d.t) + assert_equal(res.y, res_d.y) + assert_(res_d.t_events is None) + assert_(res_d.success) + assert_equal(res_d.status, 0) + + # if t and y are equal only test values for one case + y_true = sol_rational(res.t) + e = compute_error(res.y, y_true, rtol, atol) + assert_(np.all(e < 5)) + + +@pytest.mark.thread_unsafe +def test_t_eval_early_event(): + def early_event(t, y): + return t - 7 + + early_event.terminal = True + + rtol = 1e-3 + atol = 1e-6 + y0 = [1/3, 2/9] + t_span = [5, 9] + t_eval = np.linspace(7.5, 9, 16) + for method in ['RK23', 'RK45', 'DOP853', 'Radau', 'BDF', 'LSODA']: + with suppress_warnings() as sup: + sup.filter(UserWarning, + "The following arguments have no effect for a chosen " + "solver: `jac`") + res = solve_ivp(fun_rational, t_span, y0, rtol=rtol, atol=atol, + method=method, t_eval=t_eval, events=early_event, + jac=jac_rational) + assert res.success + assert res.message == 'A termination event occurred.' + assert res.status == 1 + assert not res.t and not res.y + assert len(res.t_events) == 1 + assert res.t_events[0].size == 1 + assert res.t_events[0][0] == 7 + + +def test_event_dense_output_LSODA(num_parallel_threads): + if num_parallel_threads > 1: + pytest.skip('LSODA does not allow for concurrent execution') + + def event_lsoda(t, y): + return y[0] - 2.02e-5 + + rtol = 1e-3 + atol = 1e-6 + y0 = [0.05] + t_span = [-2, 2] + first_step = 1e-3 + res = solve_ivp( + fun_event_dense_output_LSODA, + t_span, + y0, + method="LSODA", + dense_output=True, + events=event_lsoda, + first_step=first_step, + max_step=1, + rtol=rtol, + atol=atol, + jac=jac_event_dense_output_LSODA, + ) + + assert_equal(res.t[0], t_span[0]) + assert_equal(res.t[-1], t_span[-1]) + assert_allclose(first_step, np.abs(res.t[1] - t_span[0])) + assert res.success + assert_equal(res.status, 0) + + y_true = sol_event_dense_output_LSODA(res.t) + e = compute_error(res.y, y_true, rtol, atol) + assert_array_less(e, 5) + + tc = np.linspace(*t_span) + yc_true = sol_event_dense_output_LSODA(tc) + yc = res.sol(tc) + e = compute_error(yc, yc_true, rtol, atol) + assert_array_less(e, 5) + + assert_allclose(res.sol(res.t), res.y, rtol=1e-15, atol=1e-15) + + +def test_no_integration(): + for method in ['RK23', 'RK45', 'DOP853', 'Radau', 'BDF', 'LSODA']: + sol = solve_ivp(lambda t, y: -y, [4, 4], [2, 3], + method=method, dense_output=True) + assert_equal(sol.sol(4), [2, 3]) + assert_equal(sol.sol([4, 5, 6]), [[2, 2, 2], [3, 3, 3]]) + + +def test_no_integration_class(): + for method in [RK23, RK45, DOP853, Radau, BDF, LSODA]: + solver = method(lambda t, y: -y, 0.0, [10.0, 0.0], 0.0) + solver.step() + assert_equal(solver.status, 'finished') + sol = solver.dense_output() + assert_equal(sol(0.0), [10.0, 0.0]) + assert_equal(sol([0, 1, 2]), [[10, 10, 10], [0, 0, 0]]) + + solver = method(lambda t, y: -y, 0.0, [], np.inf) + solver.step() + assert_equal(solver.status, 'finished') + sol = solver.dense_output() + assert_equal(sol(100.0), []) + assert_equal(sol([0, 1, 2]), np.empty((0, 3))) + + +def test_empty(): + def fun(t, y): + return np.zeros((0,)) + + y0 = np.zeros((0,)) + + for method in ['RK23', 'RK45', 'DOP853', 'Radau', 'BDF', 'LSODA']: + sol = assert_no_warnings(solve_ivp, fun, [0, 10], y0, + method=method, dense_output=True) + assert_equal(sol.sol(10), np.zeros((0,))) + assert_equal(sol.sol([1, 2, 3]), np.zeros((0, 3))) + + for method in ['RK23', 'RK45', 'DOP853', 'Radau', 'BDF', 'LSODA']: + sol = assert_no_warnings(solve_ivp, fun, [0, np.inf], y0, + method=method, dense_output=True) + assert_equal(sol.sol(10), np.zeros((0,))) + assert_equal(sol.sol([1, 2, 3]), np.zeros((0, 3))) + + +def test_ConstantDenseOutput(): + sol = ConstantDenseOutput(0, 1, np.array([1, 2])) + assert_allclose(sol(1.5), [1, 2]) + assert_allclose(sol([1, 1.5, 2]), [[1, 1, 1], [2, 2, 2]]) + + sol = ConstantDenseOutput(0, 1, np.array([])) + assert_allclose(sol(1.5), np.empty(0)) + assert_allclose(sol([1, 1.5, 2]), np.empty((0, 3))) + + +def test_classes(): + y0 = [1 / 3, 2 / 9] + for cls in [RK23, RK45, DOP853, Radau, BDF, LSODA]: + solver = cls(fun_rational, 5, y0, np.inf) + assert_equal(solver.n, 2) + assert_equal(solver.status, 'running') + assert_equal(solver.t_bound, np.inf) + assert_equal(solver.direction, 1) + assert_equal(solver.t, 5) + assert_equal(solver.y, y0) + assert_(solver.step_size is None) + if cls is not LSODA: + assert_(solver.nfev > 0) + assert_(solver.njev >= 0) + assert_equal(solver.nlu, 0) + else: + assert_equal(solver.nfev, 0) + assert_equal(solver.njev, 0) + assert_equal(solver.nlu, 0) + + assert_raises(RuntimeError, solver.dense_output) + + message = solver.step() + assert_equal(solver.status, 'running') + assert_equal(message, None) + assert_equal(solver.n, 2) + assert_equal(solver.t_bound, np.inf) + assert_equal(solver.direction, 1) + assert_(solver.t > 5) + assert_(not np.all(np.equal(solver.y, y0))) + assert_(solver.step_size > 0) + assert_(solver.nfev > 0) + assert_(solver.njev >= 0) + assert_(solver.nlu >= 0) + sol = solver.dense_output() + assert_allclose(sol(5), y0, rtol=1e-15, atol=0) + + +def test_OdeSolution(): + ts = np.array([0, 2, 5], dtype=float) + s1 = ConstantDenseOutput(ts[0], ts[1], np.array([-1])) + s2 = ConstantDenseOutput(ts[1], ts[2], np.array([1])) + + sol = OdeSolution(ts, [s1, s2]) + + assert_equal(sol(-1), [-1]) + assert_equal(sol(1), [-1]) + assert_equal(sol(2), [-1]) + assert_equal(sol(3), [1]) + assert_equal(sol(5), [1]) + assert_equal(sol(6), [1]) + + assert_equal(sol([0, 6, -2, 1.5, 4.5, 2.5, 5, 5.5, 2]), + np.array([[-1, 1, -1, -1, 1, 1, 1, 1, -1]])) + + ts = np.array([10, 4, -3]) + s1 = ConstantDenseOutput(ts[0], ts[1], np.array([-1])) + s2 = ConstantDenseOutput(ts[1], ts[2], np.array([1])) + + sol = OdeSolution(ts, [s1, s2]) + assert_equal(sol(11), [-1]) + assert_equal(sol(10), [-1]) + assert_equal(sol(5), [-1]) + assert_equal(sol(4), [-1]) + assert_equal(sol(0), [1]) + assert_equal(sol(-3), [1]) + assert_equal(sol(-4), [1]) + + assert_equal(sol([12, -5, 10, -3, 6, 1, 4]), + np.array([[-1, 1, -1, 1, -1, 1, -1]])) + + ts = np.array([1, 1]) + s = ConstantDenseOutput(1, 1, np.array([10])) + sol = OdeSolution(ts, [s]) + assert_equal(sol(0), [10]) + assert_equal(sol(1), [10]) + assert_equal(sol(2), [10]) + + assert_equal(sol([2, 1, 0]), np.array([[10, 10, 10]])) + + +def test_num_jac(): + def fun(t, y): + return np.vstack([ + -0.04 * y[0] + 1e4 * y[1] * y[2], + 0.04 * y[0] - 1e4 * y[1] * y[2] - 3e7 * y[1] ** 2, + 3e7 * y[1] ** 2 + ]) + + def jac(t, y): + return np.array([ + [-0.04, 1e4 * y[2], 1e4 * y[1]], + [0.04, -1e4 * y[2] - 6e7 * y[1], -1e4 * y[1]], + [0, 6e7 * y[1], 0] + ]) + + t = 1 + y = np.array([1, 0, 0]) + J_true = jac(t, y) + threshold = 1e-5 + f = fun(t, y).ravel() + + J_num, factor = num_jac(fun, t, y, f, threshold, None) + assert_allclose(J_num, J_true, rtol=1e-5, atol=1e-5) + + J_num, factor = num_jac(fun, t, y, f, threshold, factor) + assert_allclose(J_num, J_true, rtol=1e-5, atol=1e-5) + + +def test_num_jac_sparse(): + def fun(t, y): + e = y[1:]**3 - y[:-1]**2 + z = np.zeros(y.shape[1]) + return np.vstack((z, 3 * e)) + np.vstack((2 * e, z)) + + def structure(n): + A = np.zeros((n, n), dtype=int) + A[0, 0] = 1 + A[0, 1] = 1 + for i in range(1, n - 1): + A[i, i - 1: i + 2] = 1 + A[-1, -1] = 1 + A[-1, -2] = 1 + + return A + + np.random.seed(0) + n = 20 + y = np.random.randn(n) + A = structure(n) + groups = group_columns(A) + + f = fun(0, y[:, None]).ravel() + + # Compare dense and sparse results, assuming that dense implementation + # is correct (as it is straightforward). + J_num_sparse, factor_sparse = num_jac(fun, 0, y.ravel(), f, 1e-8, None, + sparsity=(A, groups)) + J_num_dense, factor_dense = num_jac(fun, 0, y.ravel(), f, 1e-8, None) + assert_allclose(J_num_dense, J_num_sparse.toarray(), + rtol=1e-12, atol=1e-14) + assert_allclose(factor_dense, factor_sparse, rtol=1e-12, atol=1e-14) + + # Take small factors to trigger their recomputing inside. + factor = np.random.uniform(0, 1e-12, size=n) + J_num_sparse, factor_sparse = num_jac(fun, 0, y.ravel(), f, 1e-8, factor, + sparsity=(A, groups)) + J_num_dense, factor_dense = num_jac(fun, 0, y.ravel(), f, 1e-8, factor) + + assert_allclose(J_num_dense, J_num_sparse.toarray(), + rtol=1e-12, atol=1e-14) + assert_allclose(factor_dense, factor_sparse, rtol=1e-12, atol=1e-14) + + +def test_args(): + + # sys3 is actually two decoupled systems. (x, y) form a + # linear oscillator, while z is a nonlinear first order + # system with equilibria at z=0 and z=1. If k > 0, z=1 + # is stable and z=0 is unstable. + + def sys3(t, w, omega, k, zfinal): + x, y, z = w + return [-omega*y, omega*x, k*z*(1 - z)] + + def sys3_jac(t, w, omega, k, zfinal): + x, y, z = w + J = np.array([[0, -omega, 0], + [omega, 0, 0], + [0, 0, k*(1 - 2*z)]]) + return J + + def sys3_x0decreasing(t, w, omega, k, zfinal): + x, y, z = w + return x + + def sys3_y0increasing(t, w, omega, k, zfinal): + x, y, z = w + return y + + def sys3_zfinal(t, w, omega, k, zfinal): + x, y, z = w + return z - zfinal + + # Set the event flags for the event functions. + sys3_x0decreasing.direction = -1 + sys3_y0increasing.direction = 1 + sys3_zfinal.terminal = True + + omega = 2 + k = 4 + + tfinal = 5 + zfinal = 0.99 + # Find z0 such that when z(0) = z0, z(tfinal) = zfinal. + # The condition z(tfinal) = zfinal is the terminal event. + z0 = np.exp(-k*tfinal)/((1 - zfinal)/zfinal + np.exp(-k*tfinal)) + + w0 = [0, -1, z0] + + # Provide the jac argument and use the Radau method to ensure that the use + # of the Jacobian function is exercised. + # If event handling is working, the solution will stop at tfinal, not tend. + tend = 2*tfinal + sol = solve_ivp(sys3, [0, tend], w0, + events=[sys3_x0decreasing, sys3_y0increasing, sys3_zfinal], + dense_output=True, args=(omega, k, zfinal), + method='Radau', jac=sys3_jac, + rtol=1e-10, atol=1e-13) + + # Check that we got the expected events at the expected times. + x0events_t = sol.t_events[0] + y0events_t = sol.t_events[1] + zfinalevents_t = sol.t_events[2] + assert_allclose(x0events_t, [0.5*np.pi, 1.5*np.pi]) + assert_allclose(y0events_t, [0.25*np.pi, 1.25*np.pi]) + assert_allclose(zfinalevents_t, [tfinal]) + + # Check that the solution agrees with the known exact solution. + t = np.linspace(0, zfinalevents_t[0], 250) + w = sol.sol(t) + assert_allclose(w[0], np.sin(omega*t), rtol=1e-9, atol=1e-12) + assert_allclose(w[1], -np.cos(omega*t), rtol=1e-9, atol=1e-12) + assert_allclose(w[2], 1/(((1 - z0)/z0)*np.exp(-k*t) + 1), + rtol=1e-9, atol=1e-12) + + # Check that the state variables have the expected values at the events. + x0events = sol.sol(x0events_t) + y0events = sol.sol(y0events_t) + zfinalevents = sol.sol(zfinalevents_t) + assert_allclose(x0events[0], np.zeros_like(x0events[0]), atol=5e-14) + assert_allclose(x0events[1], np.ones_like(x0events[1])) + assert_allclose(y0events[0], np.ones_like(y0events[0])) + assert_allclose(y0events[1], np.zeros_like(y0events[1]), atol=5e-14) + assert_allclose(zfinalevents[2], [zfinal]) + + +@pytest.mark.thread_unsafe +def test_array_rtol(): + # solve_ivp had a bug with array_like `rtol`; see gh-15482 + # check that it's fixed + def f(t, y): + return y[0], y[1] + + # no warning (or error) when `rtol` is array_like + sol = solve_ivp(f, (0, 1), [1., 1.], rtol=[1e-1, 1e-1]) + err1 = np.abs(np.linalg.norm(sol.y[:, -1] - np.exp(1))) + + # warning when an element of `rtol` is too small + with pytest.warns(UserWarning, match="At least one element..."): + sol = solve_ivp(f, (0, 1), [1., 1.], rtol=[1e-1, 1e-16]) + err2 = np.abs(np.linalg.norm(sol.y[:, -1] - np.exp(1))) + + # tighter rtol improves the error + assert err2 < err1 + + +@pytest.mark.parametrize('method', ['RK23', 'RK45', 'DOP853', 'Radau', 'BDF', 'LSODA']) +def test_integration_zero_rhs(method, num_parallel_threads): + if method == 'LSODA' and num_parallel_threads > 1: + pytest.skip(reason='LSODA does not allow for concurrent execution') + + result = solve_ivp(fun_zero, [0, 10], np.ones(3), method=method) + assert_(result.success) + assert_equal(result.status, 0) + assert_allclose(result.y, 1.0, rtol=1e-15) + + +def test_args_single_value(): + def fun_with_arg(t, y, a): + return a*y + + message = "Supplied 'args' cannot be unpacked." + with pytest.raises(TypeError, match=message): + solve_ivp(fun_with_arg, (0, 0.1), [1], args=-1) + + sol = solve_ivp(fun_with_arg, (0, 0.1), [1], args=(-1,)) + assert_allclose(sol.y[0, -1], np.exp(-0.1)) + + +@pytest.mark.parametrize("f0_fill", [np.nan, np.inf]) +def test_initial_state_finiteness(f0_fill): + # regression test for gh-17846 + msg = "All components of the initial state `y0` must be finite." + with pytest.raises(ValueError, match=msg): + solve_ivp(fun_zero, [0, 10], np.full(3, f0_fill)) + + +@pytest.mark.parametrize('method', ['RK23', 'RK45', 'DOP853', 'Radau', 'BDF']) +def test_zero_interval(method): + # Case where upper and lower limits of integration are the same + # Result of integration should match initial state. + # f[y(t)] = 2y(t) + def f(t, y): + return 2 * y + res = solve_ivp(f, (0.0, 0.0), np.array([1.0]), method=method) + assert res.success + assert_allclose(res.y[0, -1], 1.0) + + +@pytest.mark.parametrize('method', ['RK23', 'RK45', 'DOP853', 'Radau', 'BDF']) +def test_tbound_respected_small_interval(method): + """Regression test for gh-17341""" + SMALL = 1e-4 + + # f[y(t)] = 2y(t) on t in [0,SMALL] + # undefined otherwise + def f(t, y): + if t > SMALL: + raise ValueError("Function was evaluated outside interval") + return 2 * y + res = solve_ivp(f, (0.0, SMALL), np.array([1]), method=method) + assert res.success + + +@pytest.mark.parametrize('method', ['RK23', 'RK45', 'DOP853', 'Radau', 'BDF']) +def test_tbound_respected_larger_interval(method): + """Regression test for gh-8848""" + def V(r): + return -11/r + 10 * r / (0.05 + r**2) + + def func(t, p): + if t < -17 or t > 2: + raise ValueError("Function was evaluated outside interval") + P = p[0] + Q = p[1] + r = np.exp(t) + dPdr = r * Q + dQdr = -2.0 * r * ((-0.2 - V(r)) * P + 1 / r * Q) + return np.array([dPdr, dQdr]) + + result = solve_ivp(func, + (-17, 2), + y0=np.array([1, -11]), + max_step=0.03, + vectorized=False, + t_eval=None, + atol=1e-8, + rtol=1e-5) + assert result.success + + +@pytest.mark.parametrize('method', ['RK23', 'RK45', 'DOP853', 'Radau', 'BDF']) +def test_tbound_respected_oscillator(method): + "Regression test for gh-9198" + def reactions_func(t, y): + if (t > 205): + raise ValueError("Called outside interval") + yprime = np.array([1.73307544e-02, + 6.49376470e-06, + 0.00000000e+00, + 0.00000000e+00]) + return yprime + + def run_sim2(t_end, n_timepoints=10, shortest_delay_line=10000000): + init_state = np.array([134.08298555, 138.82348612, 100., 0.]) + t0 = 100.0 + t1 = 200.0 + return solve_ivp(reactions_func, + (t0, t1), + init_state.copy(), + dense_output=True, + max_step=t1 - t0) + result = run_sim2(1000, 100, 100) + assert result.success + + +def test_inital_maxstep(): + """Verify that select_inital_step respects max_step""" + rtol = 1e-3 + atol = 1e-6 + y0 = np.array([1/3, 2/9]) + for (t0, t_bound) in ((5, 9), (5, 1)): + for method_order in [RK23.error_estimator_order, + RK45.error_estimator_order, + DOP853.error_estimator_order, + 3, #RADAU + 1 #BDF + ]: + step_no_max = select_initial_step(fun_rational, t0, y0, t_bound, + np.inf, + fun_rational(t0,y0), + np.sign(t_bound - t0), + method_order, + rtol, atol) + max_step = step_no_max/2 + step_with_max = select_initial_step(fun_rational, t0, y0, t_bound, + max_step, + fun_rational(t0, y0), + np.sign(t_bound - t0), + method_order, + rtol, atol) + assert_equal(max_step, step_with_max) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/tests/test_rk.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/tests/test_rk.py new file mode 100644 index 0000000000000000000000000000000000000000..33cb27d0323d037c0937ab94b4de8f63b46be3d7 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ivp/tests/test_rk.py @@ -0,0 +1,37 @@ +import pytest +from numpy.testing import assert_allclose, assert_ +import numpy as np +from scipy.integrate import RK23, RK45, DOP853 +from scipy.integrate._ivp import dop853_coefficients + + +@pytest.mark.parametrize("solver", [RK23, RK45, DOP853]) +def test_coefficient_properties(solver): + assert_allclose(np.sum(solver.B), 1, rtol=1e-15) + assert_allclose(np.sum(solver.A, axis=1), solver.C, rtol=1e-14) + + +def test_coefficient_properties_dop853(): + assert_allclose(np.sum(dop853_coefficients.B), 1, rtol=1e-15) + assert_allclose(np.sum(dop853_coefficients.A, axis=1), + dop853_coefficients.C, + rtol=1e-14) + + +@pytest.mark.parametrize("solver_class", [RK23, RK45, DOP853]) +def test_error_estimation(solver_class): + step = 0.2 + solver = solver_class(lambda t, y: y, 0, [1], 1, first_step=step) + solver.step() + error_estimate = solver._estimate_error(solver.K, step) + error = solver.y - np.exp([step]) + assert_(np.abs(error) < np.abs(error_estimate)) + + +@pytest.mark.parametrize("solver_class", [RK23, RK45, DOP853]) +def test_error_estimation_complex(solver_class): + h = 0.2 + solver = solver_class(lambda t, y: 1j * y, 0, [1j], 1, first_step=h) + solver.step() + err_norm = solver._estimate_error_norm(solver.K, h, scale=[1]) + assert np.isrealobj(err_norm) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_lebedev.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_lebedev.py new file mode 100644 index 0000000000000000000000000000000000000000..da200972f9d475162f84294ed335149dc86fe94b --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_lebedev.py @@ -0,0 +1,5450 @@ +# getLebedevSphere +# Copyright (c) 2010, Robert Parrish +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the distribution +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +# Brainlessly translated to Python + +import numpy as np +from numpy import pi, zeros, sqrt + + +__all__ = ['lebedev_rule'] + + +def get_lebedev_sphere(degree): + # getLebedevSphere + # @author Rob Parrish, The Sherrill Group, CCMST Georgia Tech + # @email robparrish@gmail.com + # @date 03/24/2010 + # + # @description - function to compute normalized points and weights + # for Lebedev quadratures on the surface of the unit sphere at double precision. + # **********Relative error is generally expected to be ~2.0E-14 [1]******** + # Lebedev quadratures are superbly accurate and efficient quadrature rules for + # approximating integrals of the form $v = \iint_{4\pi} f(\Omega) \ \ud + # \Omega$, where $\Omega is the solid angle on the surface of the unit + # sphere. Lebedev quadratures integrate all spherical harmonics up to $l = + # order$, where $degree \approx order(order+1)/3$. These grids may be easily + # combined with radial quadratures to provide robust cubature formulae. For + # example, see 'A. Becke, 1988c, J. Chem. Phys., 88(4), pp. 2547' (The first + # paper on tractable molecular Density Functional Theory methods, of which + # Lebedev grids and numerical cubature are an intrinsic part). + # + # @param degree - positive integer specifying number of points in the + # requested quadrature. Allowed values are (degree -> order): + # degree: { 6, 14, 26, 38, 50, 74, 86, 110, 146, 170, 194, 230, 266, 302, + # 350, 434, 590, 770, 974, 1202, 1454, 1730, 2030, 2354, 2702, 3074, + # 3470, 3890, 4334, 4802, 5294, 5810 } + # order: {3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,35,41,47,53,59,65,71,77, + # 83,89,95,101,107,113,119,125,131} + # + # + # @return leb_tmp - struct containing fields: + # x - x values of quadrature, constrained to unit sphere + # y - y values of quadrature, constrained to unit sphere + # z - z values of quadrature, constrained to unit sphere + # w - quadrature weights, normalized to $4\pi$. + # + # @example: $\int_S x^2+y^2-z^2 \ud \Omega = 4.188790204786399$ + # f = @(x,y,z) x.^2+y.^2-z.^2 + # leb = getLebedevSphere(590) + # v = f(leb.x,leb.y,leb.z) + # int = sum(v.*leb.w) + # + # @citation - Translated from a Fortran code kindly provided by Christoph van + # Wuellen (Ruhr-Universitaet, Bochum, Germany), which in turn came from the + # original C routines coded by Dmitri Laikov (Moscow State University, + # Moscow, Russia). The MATLAB implementation of this code is designed for + # benchmarking of new DFT integration techniques to be implemented in the + # open source Psi4 ab initio quantum chemistry program. + # + # As per Professor Wuellen's request, any papers published using this code + # or its derivatives are requested to include the following citation: + # + # [1] V.I. Lebedev, and D.N. Laikov + # "A quadrature formula for the sphere of the 131st + # algebraic order of accuracy" + # Doklady Mathematics, Vol. 59, No. 3, 1999, pp. 477-481. + + class Leb: + x, y, z, w = None, None, None, None + + leb_tmp = Leb() + + leb_tmp.x = zeros(degree) + leb_tmp.y = zeros(degree) + leb_tmp.z = zeros(degree) + leb_tmp.w = zeros(degree) + + start = 0 + a = 0.0 + b = 0.0 + + match degree: + + case 6: + + v = 0.1666666666666667E+0 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + + case 14: + + v = 0.6666666666666667E-1 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + v = 0.7500000000000000E-1 + leb_tmp, start = get_lebedev_recurrence_points(3, start, a, b, v, leb_tmp) + + case 26: + + v = 0.4761904761904762E-1 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + v = 0.3809523809523810E-1 + leb_tmp, start = get_lebedev_recurrence_points(2, start, a, b, v, leb_tmp) + v = 0.3214285714285714E-1 + leb_tmp, start = get_lebedev_recurrence_points(3, start, a, b, v, leb_tmp) + + case 38: + + v = 0.9523809523809524E-2 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + v = 0.3214285714285714E-1 + leb_tmp, start = get_lebedev_recurrence_points(3, start, a, b, v, leb_tmp) + a = 0.4597008433809831E+0 + v = 0.2857142857142857E-1 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + + case 50: + + v = 0.1269841269841270E-1 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + v = 0.2257495590828924E-1 + leb_tmp, start = get_lebedev_recurrence_points(2, start, a, b, v, leb_tmp) + v = 0.2109375000000000E-1 + leb_tmp, start = get_lebedev_recurrence_points(3, start, a, b, v, leb_tmp) + a = 0.3015113445777636E+0 + v = 0.2017333553791887E-1 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + + case 74: + + v = 0.5130671797338464E-3 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + v = 0.1660406956574204E-1 + leb_tmp, start = get_lebedev_recurrence_points(2, start, a, b, v, leb_tmp) + v = -0.2958603896103896E-1 + leb_tmp, start = get_lebedev_recurrence_points(3, start, a, b, v, leb_tmp) + a = 0.4803844614152614E+0 + v = 0.2657620708215946E-1 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3207726489807764E+0 + v = 0.1652217099371571E-1 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + + case 86: + + v = 0.1154401154401154E-1 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + v = 0.1194390908585628E-1 + leb_tmp, start = get_lebedev_recurrence_points(3, start, a, b, v, leb_tmp) + a = 0.3696028464541502E+0 + v = 0.1111055571060340E-1 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6943540066026664E+0 + v = 0.1187650129453714E-1 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3742430390903412E+0 + v = 0.1181230374690448E-1 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + + case 110: + + v = 0.3828270494937162E-2 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + v = 0.9793737512487512E-2 + leb_tmp, start = get_lebedev_recurrence_points(3, start, a, b, v, leb_tmp) + a = 0.1851156353447362E+0 + v = 0.8211737283191111E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6904210483822922E+0 + v = 0.9942814891178103E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3956894730559419E+0 + v = 0.9595471336070963E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4783690288121502E+0 + v = 0.9694996361663028E-2 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + + case 146: + + v = 0.5996313688621381E-3 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + v = 0.7372999718620756E-2 + leb_tmp, start = get_lebedev_recurrence_points(2, start, a, b, v, leb_tmp) + v = 0.7210515360144488E-2 + leb_tmp, start = get_lebedev_recurrence_points(3, start, a, b, v, leb_tmp) + a = 0.6764410400114264E+0 + v = 0.7116355493117555E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4174961227965453E+0 + v = 0.6753829486314477E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1574676672039082E+0 + v = 0.7574394159054034E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1403553811713183E+0 + b = 0.4493328323269557E+0 + v = 0.6991087353303262E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + + case 170: + + v = 0.5544842902037365E-2 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + v = 0.6071332770670752E-2 + leb_tmp, start = get_lebedev_recurrence_points(2, start, a, b, v, leb_tmp) + v = 0.6383674773515093E-2 + leb_tmp, start = get_lebedev_recurrence_points(3, start, a, b, v, leb_tmp) + a = 0.2551252621114134E+0 + v = 0.5183387587747790E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6743601460362766E+0 + v = 0.6317929009813725E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4318910696719410E+0 + v = 0.6201670006589077E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2613931360335988E+0 + v = 0.5477143385137348E-2 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.4990453161796037E+0 + b = 0.1446630744325115E+0 + v = 0.5968383987681156E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + + case 194: + + v = 0.1782340447244611E-2 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + v = 0.5716905949977102E-2 + leb_tmp, start = get_lebedev_recurrence_points(2, start, a, b, v, leb_tmp) + v = 0.5573383178848738E-2 + leb_tmp, start = get_lebedev_recurrence_points(3, start, a, b, v, leb_tmp) + a = 0.6712973442695226E+0 + v = 0.5608704082587997E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2892465627575439E+0 + v = 0.5158237711805383E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4446933178717437E+0 + v = 0.5518771467273614E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1299335447650067E+0 + v = 0.4106777028169394E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3457702197611283E+0 + v = 0.5051846064614808E-2 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.1590417105383530E+0 + b = 0.8360360154824589E+0 + v = 0.5530248916233094E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + + case 230: + + v = -0.5522639919727325E-1 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + v = 0.4450274607445226E-2 + leb_tmp, start = get_lebedev_recurrence_points(3, start, a, b, v, leb_tmp) + a = 0.4492044687397611E+0 + v = 0.4496841067921404E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2520419490210201E+0 + v = 0.5049153450478750E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6981906658447242E+0 + v = 0.3976408018051883E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6587405243460960E+0 + v = 0.4401400650381014E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4038544050097660E-1 + v = 0.1724544350544401E-1 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5823842309715585E+0 + v = 0.4231083095357343E-2 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.3545877390518688E+0 + v = 0.5198069864064399E-2 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.2272181808998187E+0 + b = 0.4864661535886647E+0 + v = 0.4695720972568883E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + + case 266: + + v = -0.1313769127326952E-2 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + v = -0.2522728704859336E-2 + leb_tmp, start = get_lebedev_recurrence_points(2, start, a, b, v, leb_tmp) + v = 0.4186853881700583E-2 + leb_tmp, start = get_lebedev_recurrence_points(3, start, a, b, v, leb_tmp) + a = 0.7039373391585475E+0 + v = 0.5315167977810885E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1012526248572414E+0 + v = 0.4047142377086219E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4647448726420539E+0 + v = 0.4112482394406990E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3277420654971629E+0 + v = 0.3595584899758782E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6620338663699974E+0 + v = 0.4256131351428158E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.8506508083520399E+0 + v = 0.4229582700647240E-2 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.3233484542692899E+0 + b = 0.1153112011009701E+0 + v = 0.4080914225780505E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2314790158712601E+0 + b = 0.5244939240922365E+0 + v = 0.4071467593830964E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + + case 302: + + v = 0.8545911725128148E-3 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + v = 0.3599119285025571E-2 + leb_tmp, start = get_lebedev_recurrence_points(3, start, a, b, v, leb_tmp) + a = 0.3515640345570105E+0 + v = 0.3449788424305883E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6566329410219612E+0 + v = 0.3604822601419882E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4729054132581005E+0 + v = 0.3576729661743367E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.9618308522614784E-1 + v = 0.2352101413689164E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2219645236294178E+0 + v = 0.3108953122413675E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.7011766416089545E+0 + v = 0.3650045807677255E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2644152887060663E+0 + v = 0.2982344963171804E-2 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.5718955891878961E+0 + v = 0.3600820932216460E-2 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.2510034751770465E+0 + b = 0.8000727494073952E+0 + v = 0.3571540554273387E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1233548532583327E+0 + b = 0.4127724083168531E+0 + v = 0.3392312205006170E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + + case 350: + + v = 0.3006796749453936E-2 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + v = 0.3050627745650771E-2 + leb_tmp, start = get_lebedev_recurrence_points(3, start, a, b, v, leb_tmp) + a = 0.7068965463912316E+0 + v = 0.1621104600288991E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4794682625712025E+0 + v = 0.3005701484901752E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1927533154878019E+0 + v = 0.2990992529653774E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6930357961327123E+0 + v = 0.2982170644107595E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3608302115520091E+0 + v = 0.2721564237310992E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6498486161496169E+0 + v = 0.3033513795811141E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1932945013230339E+0 + v = 0.3007949555218533E-2 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.3800494919899303E+0 + v = 0.2881964603055307E-2 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.2899558825499574E+0 + b = 0.7934537856582316E+0 + v = 0.2958357626535696E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.9684121455103957E-1 + b = 0.8280801506686862E+0 + v = 0.3036020026407088E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1833434647041659E+0 + b = 0.9074658265305127E+0 + v = 0.2832187403926303E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + + case 434: + + v = 0.5265897968224436E-3 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + v = 0.2548219972002607E-2 + leb_tmp, start = get_lebedev_recurrence_points(2, start, a, b, v, leb_tmp) + v = 0.2512317418927307E-2 + leb_tmp, start = get_lebedev_recurrence_points(3, start, a, b, v, leb_tmp) + a = 0.6909346307509111E+0 + v = 0.2530403801186355E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1774836054609158E+0 + v = 0.2014279020918528E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4914342637784746E+0 + v = 0.2501725168402936E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6456664707424256E+0 + v = 0.2513267174597564E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2861289010307638E+0 + v = 0.2302694782227416E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.7568084367178018E-1 + v = 0.1462495621594614E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3927259763368002E+0 + v = 0.2445373437312980E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.8818132877794288E+0 + v = 0.2417442375638981E-2 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.9776428111182649E+0 + v = 0.1910951282179532E-2 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.2054823696403044E+0 + b = 0.8689460322872412E+0 + v = 0.2416930044324775E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5905157048925271E+0 + b = 0.7999278543857286E+0 + v = 0.2512236854563495E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5550152361076807E+0 + b = 0.7717462626915901E+0 + v = 0.2496644054553086E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.9371809858553722E+0 + b = 0.3344363145343455E+0 + v = 0.2236607760437849E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + + case 590: + + v = 0.3095121295306187E-3 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + v = 0.1852379698597489E-2 + leb_tmp, start = get_lebedev_recurrence_points(3, start, a, b, v, leb_tmp) + a = 0.7040954938227469E+0 + v = 0.1871790639277744E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6807744066455243E+0 + v = 0.1858812585438317E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6372546939258752E+0 + v = 0.1852028828296213E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5044419707800358E+0 + v = 0.1846715956151242E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4215761784010967E+0 + v = 0.1818471778162769E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3317920736472123E+0 + v = 0.1749564657281154E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2384736701421887E+0 + v = 0.1617210647254411E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1459036449157763E+0 + v = 0.1384737234851692E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6095034115507196E-1 + v = 0.9764331165051050E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6116843442009876E+0 + v = 0.1857161196774078E-2 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.3964755348199858E+0 + v = 0.1705153996395864E-2 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.1724782009907724E+0 + v = 0.1300321685886048E-2 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.5610263808622060E+0 + b = 0.3518280927733519E+0 + v = 0.1842866472905286E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4742392842551980E+0 + b = 0.2634716655937950E+0 + v = 0.1802658934377451E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5984126497885380E+0 + b = 0.1816640840360209E+0 + v = 0.1849830560443660E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3791035407695563E+0 + b = 0.1720795225656878E+0 + v = 0.1713904507106709E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2778673190586244E+0 + b = 0.8213021581932511E-1 + v = 0.1555213603396808E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5033564271075117E+0 + b = 0.8999205842074875E-1 + v = 0.1802239128008525E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + + case 770: + + v = 0.2192942088181184E-3 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + v = 0.1436433617319080E-2 + leb_tmp, start = get_lebedev_recurrence_points(2, start, a, b, v, leb_tmp) + v = 0.1421940344335877E-2 + leb_tmp, start = get_lebedev_recurrence_points(3, start, a, b, v, leb_tmp) + a = 0.5087204410502360E-1 + v = 0.6798123511050502E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1228198790178831E+0 + v = 0.9913184235294912E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2026890814408786E+0 + v = 0.1180207833238949E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2847745156464294E+0 + v = 0.1296599602080921E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3656719078978026E+0 + v = 0.1365871427428316E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4428264886713469E+0 + v = 0.1402988604775325E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5140619627249735E+0 + v = 0.1418645563595609E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6306401219166803E+0 + v = 0.1421376741851662E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6716883332022612E+0 + v = 0.1423996475490962E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6979792685336881E+0 + v = 0.1431554042178567E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1446865674195309E+0 + v = 0.9254401499865368E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.3390263475411216E+0 + v = 0.1250239995053509E-2 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.5335804651263506E+0 + v = 0.1394365843329230E-2 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.6944024393349413E-1 + b = 0.2355187894242326E+0 + v = 0.1127089094671749E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2269004109529460E+0 + b = 0.4102182474045730E+0 + v = 0.1345753760910670E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.8025574607775339E-1 + b = 0.6214302417481605E+0 + v = 0.1424957283316783E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1467999527896572E+0 + b = 0.3245284345717394E+0 + v = 0.1261523341237750E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1571507769824727E+0 + b = 0.5224482189696630E+0 + v = 0.1392547106052696E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2365702993157246E+0 + b = 0.6017546634089558E+0 + v = 0.1418761677877656E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.7714815866765732E-1 + b = 0.4346575516141163E+0 + v = 0.1338366684479554E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3062936666210730E+0 + b = 0.4908826589037616E+0 + v = 0.1393700862676131E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3822477379524787E+0 + b = 0.5648768149099500E+0 + v = 0.1415914757466932E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + + case 974: + + v = 0.1438294190527431E-3 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + v = 0.1125772288287004E-2 + leb_tmp, start = get_lebedev_recurrence_points(3, start, a, b, v, leb_tmp) + a = 0.4292963545341347E-1 + v = 0.4948029341949241E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1051426854086404E+0 + v = 0.7357990109125470E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1750024867623087E+0 + v = 0.8889132771304384E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2477653379650257E+0 + v = 0.9888347838921435E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3206567123955957E+0 + v = 0.1053299681709471E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3916520749849983E+0 + v = 0.1092778807014578E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4590825874187624E+0 + v = 0.1114389394063227E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5214563888415861E+0 + v = 0.1123724788051555E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6253170244654199E+0 + v = 0.1125239325243814E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6637926744523170E+0 + v = 0.1126153271815905E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6910410398498301E+0 + v = 0.1130286931123841E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.7052907007457760E+0 + v = 0.1134986534363955E-2 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1236686762657990E+0 + v = 0.6823367927109931E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.2940777114468387E+0 + v = 0.9454158160447096E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.4697753849207649E+0 + v = 0.1074429975385679E-2 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.6334563241139567E+0 + v = 0.1129300086569132E-2 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.5974048614181342E-1 + b = 0.2029128752777523E+0 + v = 0.8436884500901954E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1375760408473636E+0 + b = 0.4602621942484054E+0 + v = 0.1075255720448885E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3391016526336286E+0 + b = 0.5030673999662036E+0 + v = 0.1108577236864462E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1271675191439820E+0 + b = 0.2817606422442134E+0 + v = 0.9566475323783357E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2693120740413512E+0 + b = 0.4331561291720157E+0 + v = 0.1080663250717391E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1419786452601918E+0 + b = 0.6256167358580814E+0 + v = 0.1126797131196295E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6709284600738255E-1 + b = 0.3798395216859157E+0 + v = 0.1022568715358061E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.7057738183256172E-1 + b = 0.5517505421423520E+0 + v = 0.1108960267713108E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2783888477882155E+0 + b = 0.6029619156159187E+0 + v = 0.1122790653435766E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1979578938917407E+0 + b = 0.3589606329589096E+0 + v = 0.1032401847117460E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2087307061103274E+0 + b = 0.5348666438135476E+0 + v = 0.1107249382283854E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4055122137872836E+0 + b = 0.5674997546074373E+0 + v = 0.1121780048519972E-2 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + + case 1202: + + v = 0.1105189233267572E-3 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + v = 0.9205232738090741E-3 + leb_tmp, start = get_lebedev_recurrence_points(2, start, a, b, v, leb_tmp) + v = 0.9133159786443561E-3 + leb_tmp, start = get_lebedev_recurrence_points(3, start, a, b, v, leb_tmp) + a = 0.3712636449657089E-1 + v = 0.3690421898017899E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.9140060412262223E-1 + v = 0.5603990928680660E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1531077852469906E+0 + v = 0.6865297629282609E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2180928891660612E+0 + v = 0.7720338551145630E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2839874532200175E+0 + v = 0.8301545958894795E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3491177600963764E+0 + v = 0.8686692550179628E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4121431461444309E+0 + v = 0.8927076285846890E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4718993627149127E+0 + v = 0.9060820238568219E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5273145452842337E+0 + v = 0.9119777254940867E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6209475332444019E+0 + v = 0.9128720138604181E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6569722711857291E+0 + v = 0.9130714935691735E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6841788309070143E+0 + v = 0.9152873784554116E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.7012604330123631E+0 + v = 0.9187436274321654E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1072382215478166E+0 + v = 0.5176977312965694E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.2582068959496968E+0 + v = 0.7331143682101417E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.4172752955306717E+0 + v = 0.8463232836379928E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.5700366911792503E+0 + v = 0.9031122694253992E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.9827986018263947E+0 + b = 0.1771774022615325E+0 + v = 0.6485778453163257E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.9624249230326228E+0 + b = 0.2475716463426288E+0 + v = 0.7435030910982369E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.9402007994128811E+0 + b = 0.3354616289066489E+0 + v = 0.7998527891839054E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.9320822040143202E+0 + b = 0.3173615246611977E+0 + v = 0.8101731497468018E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.9043674199393299E+0 + b = 0.4090268427085357E+0 + v = 0.8483389574594331E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.8912407560074747E+0 + b = 0.3854291150669224E+0 + v = 0.8556299257311812E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.8676435628462708E+0 + b = 0.4932221184851285E+0 + v = 0.8803208679738260E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.8581979986041619E+0 + b = 0.4785320675922435E+0 + v = 0.8811048182425720E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.8396753624049856E+0 + b = 0.4507422593157064E+0 + v = 0.8850282341265444E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.8165288564022188E+0 + b = 0.5632123020762100E+0 + v = 0.9021342299040653E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.8015469370783529E+0 + b = 0.5434303569693900E+0 + v = 0.9010091677105086E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.7773563069070351E+0 + b = 0.5123518486419871E+0 + v = 0.9022692938426915E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.7661621213900394E+0 + b = 0.6394279634749102E+0 + v = 0.9158016174693465E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.7553584143533510E+0 + b = 0.6269805509024392E+0 + v = 0.9131578003189435E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.7344305757559503E+0 + b = 0.6031161693096310E+0 + v = 0.9107813579482705E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.7043837184021765E+0 + b = 0.5693702498468441E+0 + v = 0.9105760258970126E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + + case 1454: + + v = 0.7777160743261247E-4 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + v = 0.7557646413004701E-3 + leb_tmp, start = get_lebedev_recurrence_points(3, start, a, b, v, leb_tmp) + a = 0.3229290663413854E-1 + v = 0.2841633806090617E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.8036733271462222E-1 + v = 0.4374419127053555E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1354289960531653E+0 + v = 0.5417174740872172E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1938963861114426E+0 + v = 0.6148000891358593E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2537343715011275E+0 + v = 0.6664394485800705E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3135251434752570E+0 + v = 0.7025039356923220E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3721558339375338E+0 + v = 0.7268511789249627E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4286809575195696E+0 + v = 0.7422637534208629E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4822510128282994E+0 + v = 0.7509545035841214E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5320679333566263E+0 + v = 0.7548535057718401E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6172998195394274E+0 + v = 0.7554088969774001E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6510679849127481E+0 + v = 0.7553147174442808E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6777315251687360E+0 + v = 0.7564767653292297E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6963109410648741E+0 + v = 0.7587991808518730E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.7058935009831749E+0 + v = 0.7608261832033027E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.9955546194091857E+0 + v = 0.4021680447874916E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.9734115901794209E+0 + v = 0.5804871793945964E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.9275693732388626E+0 + v = 0.6792151955945159E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.8568022422795103E+0 + v = 0.7336741211286294E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.7623495553719372E+0 + v = 0.7581866300989608E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.5707522908892223E+0 + b = 0.4387028039889501E+0 + v = 0.7538257859800743E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5196463388403083E+0 + b = 0.3858908414762617E+0 + v = 0.7483517247053123E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4646337531215351E+0 + b = 0.3301937372343854E+0 + v = 0.7371763661112059E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4063901697557691E+0 + b = 0.2725423573563777E+0 + v = 0.7183448895756934E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3456329466643087E+0 + b = 0.2139510237495250E+0 + v = 0.6895815529822191E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2831395121050332E+0 + b = 0.1555922309786647E+0 + v = 0.6480105801792886E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2197682022925330E+0 + b = 0.9892878979686097E-1 + v = 0.5897558896594636E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1564696098650355E+0 + b = 0.4598642910675510E-1 + v = 0.5095708849247346E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6027356673721295E+0 + b = 0.3376625140173426E+0 + v = 0.7536906428909755E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5496032320255096E+0 + b = 0.2822301309727988E+0 + v = 0.7472505965575118E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4921707755234567E+0 + b = 0.2248632342592540E+0 + v = 0.7343017132279698E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4309422998598483E+0 + b = 0.1666224723456479E+0 + v = 0.7130871582177445E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3664108182313672E+0 + b = 0.1086964901822169E+0 + v = 0.6817022032112776E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2990189057758436E+0 + b = 0.5251989784120085E-1 + v = 0.6380941145604121E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6268724013144998E+0 + b = 0.2297523657550023E+0 + v = 0.7550381377920310E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5707324144834607E+0 + b = 0.1723080607093800E+0 + v = 0.7478646640144802E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5096360901960365E+0 + b = 0.1140238465390513E+0 + v = 0.7335918720601220E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4438729938312456E+0 + b = 0.5611522095882537E-1 + v = 0.7110120527658118E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6419978471082389E+0 + b = 0.1164174423140873E+0 + v = 0.7571363978689501E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5817218061802611E+0 + b = 0.5797589531445219E-1 + v = 0.7489908329079234E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + + case 1730: + + v = 0.6309049437420976E-4 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + v = 0.6398287705571748E-3 + leb_tmp, start = get_lebedev_recurrence_points(2, start, a, b, v, leb_tmp) + v = 0.6357185073530720E-3 + leb_tmp, start = get_lebedev_recurrence_points(3, start, a, b, v, leb_tmp) + a = 0.2860923126194662E-1 + v = 0.2221207162188168E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.7142556767711522E-1 + v = 0.3475784022286848E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1209199540995559E+0 + v = 0.4350742443589804E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1738673106594379E+0 + v = 0.4978569136522127E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2284645438467734E+0 + v = 0.5435036221998053E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2834807671701512E+0 + v = 0.5765913388219542E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3379680145467339E+0 + v = 0.6001200359226003E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3911355454819537E+0 + v = 0.6162178172717512E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4422860353001403E+0 + v = 0.6265218152438485E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4907781568726057E+0 + v = 0.6323987160974212E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5360006153211468E+0 + v = 0.6350767851540569E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6142105973596603E+0 + v = 0.6354362775297107E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6459300387977504E+0 + v = 0.6352302462706235E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6718056125089225E+0 + v = 0.6358117881417972E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6910888533186254E+0 + v = 0.6373101590310117E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.7030467416823252E+0 + v = 0.6390428961368665E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.8354951166354646E-1 + v = 0.3186913449946576E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.2050143009099486E+0 + v = 0.4678028558591711E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.3370208290706637E+0 + v = 0.5538829697598626E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.4689051484233963E+0 + v = 0.6044475907190476E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.5939400424557334E+0 + v = 0.6313575103509012E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.1394983311832261E+0 + b = 0.4097581162050343E-1 + v = 0.4078626431855630E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1967999180485014E+0 + b = 0.8851987391293348E-1 + v = 0.4759933057812725E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2546183732548967E+0 + b = 0.1397680182969819E+0 + v = 0.5268151186413440E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3121281074713875E+0 + b = 0.1929452542226526E+0 + v = 0.5643048560507316E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3685981078502492E+0 + b = 0.2467898337061562E+0 + v = 0.5914501076613073E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4233760321547856E+0 + b = 0.3003104124785409E+0 + v = 0.6104561257874195E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4758671236059246E+0 + b = 0.3526684328175033E+0 + v = 0.6230252860707806E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5255178579796463E+0 + b = 0.4031134861145713E+0 + v = 0.6305618761760796E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5718025633734589E+0 + b = 0.4509426448342351E+0 + v = 0.6343092767597889E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2686927772723415E+0 + b = 0.4711322502423248E-1 + v = 0.5176268945737826E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3306006819904809E+0 + b = 0.9784487303942695E-1 + v = 0.5564840313313692E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3904906850594983E+0 + b = 0.1505395810025273E+0 + v = 0.5856426671038980E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4479957951904390E+0 + b = 0.2039728156296050E+0 + v = 0.6066386925777091E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5027076848919780E+0 + b = 0.2571529941121107E+0 + v = 0.6208824962234458E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5542087392260217E+0 + b = 0.3092191375815670E+0 + v = 0.6296314297822907E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6020850887375187E+0 + b = 0.3593807506130276E+0 + v = 0.6340423756791859E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4019851409179594E+0 + b = 0.5063389934378671E-1 + v = 0.5829627677107342E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4635614567449800E+0 + b = 0.1032422269160612E+0 + v = 0.6048693376081110E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5215860931591575E+0 + b = 0.1566322094006254E+0 + v = 0.6202362317732461E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5758202499099271E+0 + b = 0.2098082827491099E+0 + v = 0.6299005328403779E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6259893683876795E+0 + b = 0.2618824114553391E+0 + v = 0.6347722390609353E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5313795124811891E+0 + b = 0.5263245019338556E-1 + v = 0.6203778981238834E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5893317955931995E+0 + b = 0.1061059730982005E+0 + v = 0.6308414671239979E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6426246321215801E+0 + b = 0.1594171564034221E+0 + v = 0.6362706466959498E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6511904367376113E+0 + b = 0.5354789536565540E-1 + v = 0.6375414170333233E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + + case 2030: + + v = 0.4656031899197431E-4 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + v = 0.5421549195295507E-3 + leb_tmp, start = get_lebedev_recurrence_points(3, start, a, b, v, leb_tmp) + a = 0.2540835336814348E-1 + v = 0.1778522133346553E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6399322800504915E-1 + v = 0.2811325405682796E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1088269469804125E+0 + v = 0.3548896312631459E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1570670798818287E+0 + v = 0.4090310897173364E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2071163932282514E+0 + v = 0.4493286134169965E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2578914044450844E+0 + v = 0.4793728447962723E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3085687558169623E+0 + v = 0.5015415319164265E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3584719706267024E+0 + v = 0.5175127372677937E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4070135594428709E+0 + v = 0.5285522262081019E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4536618626222638E+0 + v = 0.5356832703713962E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4979195686463577E+0 + v = 0.5397914736175170E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5393075111126999E+0 + v = 0.5416899441599930E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6115617676843916E+0 + v = 0.5419308476889938E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6414308435160159E+0 + v = 0.5416936902030596E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6664099412721607E+0 + v = 0.5419544338703164E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6859161771214913E+0 + v = 0.5428983656630975E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6993625593503890E+0 + v = 0.5442286500098193E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.7062393387719380E+0 + v = 0.5452250345057301E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.7479028168349763E-1 + v = 0.2568002497728530E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.1848951153969366E+0 + v = 0.3827211700292145E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.3059529066581305E+0 + v = 0.4579491561917824E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.4285556101021362E+0 + v = 0.5042003969083574E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.5468758653496526E+0 + v = 0.5312708889976025E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.6565821978343439E+0 + v = 0.5438401790747117E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.1253901572367117E+0 + b = 0.3681917226439641E-1 + v = 0.3316041873197344E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1775721510383941E+0 + b = 0.7982487607213301E-1 + v = 0.3899113567153771E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2305693358216114E+0 + b = 0.1264640966592335E+0 + v = 0.4343343327201309E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2836502845992063E+0 + b = 0.1751585683418957E+0 + v = 0.4679415262318919E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3361794746232590E+0 + b = 0.2247995907632670E+0 + v = 0.4930847981631031E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3875979172264824E+0 + b = 0.2745299257422246E+0 + v = 0.5115031867540091E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4374019316999074E+0 + b = 0.3236373482441118E+0 + v = 0.5245217148457367E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4851275843340022E+0 + b = 0.3714967859436741E+0 + v = 0.5332041499895321E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5303391803806868E+0 + b = 0.4175353646321745E+0 + v = 0.5384583126021542E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5726197380596287E+0 + b = 0.4612084406355461E+0 + v = 0.5411067210798852E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2431520732564863E+0 + b = 0.4258040133043952E-1 + v = 0.4259797391468714E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3002096800895869E+0 + b = 0.8869424306722721E-1 + v = 0.4604931368460021E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3558554457457432E+0 + b = 0.1368811706510655E+0 + v = 0.4871814878255202E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4097782537048887E+0 + b = 0.1860739985015033E+0 + v = 0.5072242910074885E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4616337666067458E+0 + b = 0.2354235077395853E+0 + v = 0.5217069845235350E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5110707008417874E+0 + b = 0.2842074921347011E+0 + v = 0.5315785966280310E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5577415286163795E+0 + b = 0.3317784414984102E+0 + v = 0.5376833708758905E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6013060431366950E+0 + b = 0.3775299002040700E+0 + v = 0.5408032092069521E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3661596767261781E+0 + b = 0.4599367887164592E-1 + v = 0.4842744917904866E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4237633153506581E+0 + b = 0.9404893773654421E-1 + v = 0.5048926076188130E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4786328454658452E+0 + b = 0.1431377109091971E+0 + v = 0.5202607980478373E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5305702076789774E+0 + b = 0.1924186388843570E+0 + v = 0.5309932388325743E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5793436224231788E+0 + b = 0.2411590944775190E+0 + v = 0.5377419770895208E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6247069017094747E+0 + b = 0.2886871491583605E+0 + v = 0.5411696331677717E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4874315552535204E+0 + b = 0.4804978774953206E-1 + v = 0.5197996293282420E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5427337322059053E+0 + b = 0.9716857199366665E-1 + v = 0.5311120836622945E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5943493747246700E+0 + b = 0.1465205839795055E+0 + v = 0.5384309319956951E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6421314033564943E+0 + b = 0.1953579449803574E+0 + v = 0.5421859504051886E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6020628374713980E+0 + b = 0.4916375015738108E-1 + v = 0.5390948355046314E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6529222529856881E+0 + b = 0.9861621540127005E-1 + v = 0.5433312705027845E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + + case 2354: + + v = 0.3922616270665292E-4 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + v = 0.4703831750854424E-3 + leb_tmp, start = get_lebedev_recurrence_points(2, start, a, b, v, leb_tmp) + v = 0.4678202801282136E-3 + leb_tmp, start = get_lebedev_recurrence_points(3, start, a, b, v, leb_tmp) + a = 0.2290024646530589E-1 + v = 0.1437832228979900E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5779086652271284E-1 + v = 0.2303572493577644E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.9863103576375984E-1 + v = 0.2933110752447454E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1428155792982185E+0 + v = 0.3402905998359838E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1888978116601463E+0 + v = 0.3759138466870372E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2359091682970210E+0 + v = 0.4030638447899798E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2831228833706171E+0 + v = 0.4236591432242211E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3299495857966693E+0 + v = 0.4390522656946746E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3758840802660796E+0 + v = 0.4502523466626247E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4204751831009480E+0 + v = 0.4580577727783541E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4633068518751051E+0 + v = 0.4631391616615899E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5039849474507313E+0 + v = 0.4660928953698676E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5421265793440747E+0 + v = 0.4674751807936953E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6092660230557310E+0 + v = 0.4676414903932920E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6374654204984869E+0 + v = 0.4674086492347870E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6615136472609892E+0 + v = 0.4674928539483207E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6809487285958127E+0 + v = 0.4680748979686447E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6952980021665196E+0 + v = 0.4690449806389040E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.7041245497695400E+0 + v = 0.4699877075860818E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6744033088306065E-1 + v = 0.2099942281069176E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.1678684485334166E+0 + v = 0.3172269150712804E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.2793559049539613E+0 + v = 0.3832051358546523E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.3935264218057639E+0 + v = 0.4252193818146985E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.5052629268232558E+0 + v = 0.4513807963755000E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.6107905315437531E+0 + v = 0.4657797469114178E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.1135081039843524E+0 + b = 0.3331954884662588E-1 + v = 0.2733362800522836E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1612866626099378E+0 + b = 0.7247167465436538E-1 + v = 0.3235485368463559E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2100786550168205E+0 + b = 0.1151539110849745E+0 + v = 0.3624908726013453E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2592282009459942E+0 + b = 0.1599491097143677E+0 + v = 0.3925540070712828E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3081740561320203E+0 + b = 0.2058699956028027E+0 + v = 0.4156129781116235E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3564289781578164E+0 + b = 0.2521624953502911E+0 + v = 0.4330644984623263E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4035587288240703E+0 + b = 0.2982090785797674E+0 + v = 0.4459677725921312E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4491671196373903E+0 + b = 0.3434762087235733E+0 + v = 0.4551593004456795E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4928854782917489E+0 + b = 0.3874831357203437E+0 + v = 0.4613341462749918E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5343646791958988E+0 + b = 0.4297814821746926E+0 + v = 0.4651019618269806E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5732683216530990E+0 + b = 0.4699402260943537E+0 + v = 0.4670249536100625E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2214131583218986E+0 + b = 0.3873602040643895E-1 + v = 0.3549555576441708E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2741796504750071E+0 + b = 0.8089496256902013E-1 + v = 0.3856108245249010E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3259797439149485E+0 + b = 0.1251732177620872E+0 + v = 0.4098622845756882E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3765441148826891E+0 + b = 0.1706260286403185E+0 + v = 0.4286328604268950E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4255773574530558E+0 + b = 0.2165115147300408E+0 + v = 0.4427802198993945E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4727795117058430E+0 + b = 0.2622089812225259E+0 + v = 0.4530473511488561E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5178546895819012E+0 + b = 0.3071721431296201E+0 + v = 0.4600805475703138E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5605141192097460E+0 + b = 0.3508998998801138E+0 + v = 0.4644599059958017E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6004763319352512E+0 + b = 0.3929160876166931E+0 + v = 0.4667274455712508E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3352842634946949E+0 + b = 0.4202563457288019E-1 + v = 0.4069360518020356E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3891971629814670E+0 + b = 0.8614309758870850E-1 + v = 0.4260442819919195E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4409875565542281E+0 + b = 0.1314500879380001E+0 + v = 0.4408678508029063E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4904893058592484E+0 + b = 0.1772189657383859E+0 + v = 0.4518748115548597E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5375056138769549E+0 + b = 0.2228277110050294E+0 + v = 0.4595564875375116E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5818255708669969E+0 + b = 0.2677179935014386E+0 + v = 0.4643988774315846E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6232334858144959E+0 + b = 0.3113675035544165E+0 + v = 0.4668827491646946E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4489485354492058E+0 + b = 0.4409162378368174E-1 + v = 0.4400541823741973E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5015136875933150E+0 + b = 0.8939009917748489E-1 + v = 0.4514512890193797E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5511300550512623E+0 + b = 0.1351806029383365E+0 + v = 0.4596198627347549E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5976720409858000E+0 + b = 0.1808370355053196E+0 + v = 0.4648659016801781E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6409956378989354E+0 + b = 0.2257852192301602E+0 + v = 0.4675502017157673E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5581222330827514E+0 + b = 0.4532173421637160E-1 + v = 0.4598494476455523E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6074705984161695E+0 + b = 0.9117488031840314E-1 + v = 0.4654916955152048E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6532272537379033E+0 + b = 0.1369294213140155E+0 + v = 0.4684709779505137E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6594761494500487E+0 + b = 0.4589901487275583E-1 + v = 0.4691445539106986E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + + case 2702: + + v = 0.2998675149888161E-4 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + v = 0.4077860529495355E-3 + leb_tmp, start = get_lebedev_recurrence_points(3, start, a, b, v, leb_tmp) + a = 0.2065562538818703E-1 + v = 0.1185349192520667E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5250918173022379E-1 + v = 0.1913408643425751E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.8993480082038376E-1 + v = 0.2452886577209897E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1306023924436019E+0 + v = 0.2862408183288702E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1732060388531418E+0 + v = 0.3178032258257357E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2168727084820249E+0 + v = 0.3422945667633690E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2609528309173586E+0 + v = 0.3612790520235922E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3049252927938952E+0 + v = 0.3758638229818521E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3483484138084404E+0 + v = 0.3868711798859953E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3908321549106406E+0 + v = 0.3949429933189938E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4320210071894814E+0 + v = 0.4006068107541156E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4715824795890053E+0 + v = 0.4043192149672723E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5091984794078453E+0 + v = 0.4064947495808078E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5445580145650803E+0 + v = 0.4075245619813152E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6072575796841768E+0 + v = 0.4076423540893566E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6339484505755803E+0 + v = 0.4074280862251555E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6570718257486958E+0 + v = 0.4074163756012244E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6762557330090709E+0 + v = 0.4077647795071246E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6911161696923790E+0 + v = 0.4084517552782530E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.7012841911659961E+0 + v = 0.4092468459224052E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.7064559272410020E+0 + v = 0.4097872687240906E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6123554989894765E-1 + v = 0.1738986811745028E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.1533070348312393E+0 + v = 0.2659616045280191E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.2563902605244206E+0 + v = 0.3240596008171533E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.3629346991663361E+0 + v = 0.3621195964432943E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.4683949968987538E+0 + v = 0.3868838330760539E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.5694479240657952E+0 + v = 0.4018911532693111E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.6634465430993955E+0 + v = 0.4089929432983252E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.1033958573552305E+0 + b = 0.3034544009063584E-1 + v = 0.2279907527706409E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1473521412414395E+0 + b = 0.6618803044247135E-1 + v = 0.2715205490578897E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1924552158705967E+0 + b = 0.1054431128987715E+0 + v = 0.3057917896703976E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2381094362890328E+0 + b = 0.1468263551238858E+0 + v = 0.3326913052452555E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2838121707936760E+0 + b = 0.1894486108187886E+0 + v = 0.3537334711890037E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3291323133373415E+0 + b = 0.2326374238761579E+0 + v = 0.3700567500783129E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3736896978741460E+0 + b = 0.2758485808485768E+0 + v = 0.3825245372589122E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4171406040760013E+0 + b = 0.3186179331996921E+0 + v = 0.3918125171518296E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4591677985256915E+0 + b = 0.3605329796303794E+0 + v = 0.3984720419937579E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4994733831718418E+0 + b = 0.4012147253586509E+0 + v = 0.4029746003338211E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5377731830445096E+0 + b = 0.4403050025570692E+0 + v = 0.4057428632156627E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5737917830001331E+0 + b = 0.4774565904277483E+0 + v = 0.4071719274114857E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2027323586271389E+0 + b = 0.3544122504976147E-1 + v = 0.2990236950664119E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2516942375187273E+0 + b = 0.7418304388646328E-1 + v = 0.3262951734212878E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3000227995257181E+0 + b = 0.1150502745727186E+0 + v = 0.3482634608242413E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3474806691046342E+0 + b = 0.1571963371209364E+0 + v = 0.3656596681700892E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3938103180359209E+0 + b = 0.1999631877247100E+0 + v = 0.3791740467794218E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4387519590455703E+0 + b = 0.2428073457846535E+0 + v = 0.3894034450156905E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4820503960077787E+0 + b = 0.2852575132906155E+0 + v = 0.3968600245508371E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5234573778475101E+0 + b = 0.3268884208674639E+0 + v = 0.4019931351420050E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5627318647235282E+0 + b = 0.3673033321675939E+0 + v = 0.4052108801278599E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5996390607156954E+0 + b = 0.4061211551830290E+0 + v = 0.4068978613940934E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3084780753791947E+0 + b = 0.3860125523100059E-1 + v = 0.3454275351319704E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3589988275920223E+0 + b = 0.7928938987104867E-1 + v = 0.3629963537007920E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4078628415881973E+0 + b = 0.1212614643030087E+0 + v = 0.3770187233889873E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4549287258889735E+0 + b = 0.1638770827382693E+0 + v = 0.3878608613694378E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5000278512957279E+0 + b = 0.2065965798260176E+0 + v = 0.3959065270221274E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5429785044928199E+0 + b = 0.2489436378852235E+0 + v = 0.4015286975463570E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5835939850491711E+0 + b = 0.2904811368946891E+0 + v = 0.4050866785614717E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6216870353444856E+0 + b = 0.3307941957666609E+0 + v = 0.4069320185051913E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4151104662709091E+0 + b = 0.4064829146052554E-1 + v = 0.3760120964062763E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4649804275009218E+0 + b = 0.8258424547294755E-1 + v = 0.3870969564418064E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5124695757009662E+0 + b = 0.1251841962027289E+0 + v = 0.3955287790534055E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5574711100606224E+0 + b = 0.1679107505976331E+0 + v = 0.4015361911302668E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5998597333287227E+0 + b = 0.2102805057358715E+0 + v = 0.4053836986719548E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6395007148516600E+0 + b = 0.2518418087774107E+0 + v = 0.4073578673299117E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5188456224746252E+0 + b = 0.4194321676077518E-1 + v = 0.3954628379231406E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5664190707942778E+0 + b = 0.8457661551921499E-1 + v = 0.4017645508847530E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6110464353283153E+0 + b = 0.1273652932519396E+0 + v = 0.4059030348651293E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6526430302051563E+0 + b = 0.1698173239076354E+0 + v = 0.4080565809484880E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6167551880377548E+0 + b = 0.4266398851548864E-1 + v = 0.4063018753664651E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6607195418355383E+0 + b = 0.8551925814238349E-1 + v = 0.4087191292799671E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + + case 3074: + + v = 0.2599095953754734E-4 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + v = 0.3603134089687541E-3 + leb_tmp, start = get_lebedev_recurrence_points(2, start, a, b, v, leb_tmp) + v = 0.3586067974412447E-3 + leb_tmp, start = get_lebedev_recurrence_points(3, start, a, b, v, leb_tmp) + a = 0.1886108518723392E-1 + v = 0.9831528474385880E-4 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4800217244625303E-1 + v = 0.1605023107954450E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.8244922058397242E-1 + v = 0.2072200131464099E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1200408362484023E+0 + v = 0.2431297618814187E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1595773530809965E+0 + v = 0.2711819064496707E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2002635973434064E+0 + v = 0.2932762038321116E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2415127590139982E+0 + v = 0.3107032514197368E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2828584158458477E+0 + v = 0.3243808058921213E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3239091015338138E+0 + v = 0.3349899091374030E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3643225097962194E+0 + v = 0.3430580688505218E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4037897083691802E+0 + v = 0.3490124109290343E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4420247515194127E+0 + v = 0.3532148948561955E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4787572538464938E+0 + v = 0.3559862669062833E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5137265251275234E+0 + v = 0.3576224317551411E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5466764056654611E+0 + v = 0.3584050533086076E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6054859420813535E+0 + v = 0.3584903581373224E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6308106701764562E+0 + v = 0.3582991879040586E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6530369230179584E+0 + v = 0.3582371187963125E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6718609524611158E+0 + v = 0.3584353631122350E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6869676499894013E+0 + v = 0.3589120166517785E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6980467077240748E+0 + v = 0.3595445704531601E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.7048241721250522E+0 + v = 0.3600943557111074E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5591105222058232E-1 + v = 0.1456447096742039E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.1407384078513916E+0 + v = 0.2252370188283782E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.2364035438976309E+0 + v = 0.2766135443474897E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.3360602737818170E+0 + v = 0.3110729491500851E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.4356292630054665E+0 + v = 0.3342506712303391E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.5321569415256174E+0 + v = 0.3491981834026860E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.6232956305040554E+0 + v = 0.3576003604348932E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.9469870086838469E-1 + b = 0.2778748387309470E-1 + v = 0.1921921305788564E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1353170300568141E+0 + b = 0.6076569878628364E-1 + v = 0.2301458216495632E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1771679481726077E+0 + b = 0.9703072762711040E-1 + v = 0.2604248549522893E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2197066664231751E+0 + b = 0.1354112458524762E+0 + v = 0.2845275425870697E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2624783557374927E+0 + b = 0.1750996479744100E+0 + v = 0.3036870897974840E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3050969521214442E+0 + b = 0.2154896907449802E+0 + v = 0.3188414832298066E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3472252637196021E+0 + b = 0.2560954625740152E+0 + v = 0.3307046414722089E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3885610219026360E+0 + b = 0.2965070050624096E+0 + v = 0.3398330969031360E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4288273776062765E+0 + b = 0.3363641488734497E+0 + v = 0.3466757899705373E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4677662471302948E+0 + b = 0.3753400029836788E+0 + v = 0.3516095923230054E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5051333589553359E+0 + b = 0.4131297522144286E+0 + v = 0.3549645184048486E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5406942145810492E+0 + b = 0.4494423776081795E+0 + v = 0.3570415969441392E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5742204122576457E+0 + b = 0.4839938958841502E+0 + v = 0.3581251798496118E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1865407027225188E+0 + b = 0.3259144851070796E-1 + v = 0.2543491329913348E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2321186453689432E+0 + b = 0.6835679505297343E-1 + v = 0.2786711051330776E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2773159142523882E+0 + b = 0.1062284864451989E+0 + v = 0.2985552361083679E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3219200192237254E+0 + b = 0.1454404409323047E+0 + v = 0.3145867929154039E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3657032593944029E+0 + b = 0.1854018282582510E+0 + v = 0.3273290662067609E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4084376778363622E+0 + b = 0.2256297412014750E+0 + v = 0.3372705511943501E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4499004945751427E+0 + b = 0.2657104425000896E+0 + v = 0.3448274437851510E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4898758141326335E+0 + b = 0.3052755487631557E+0 + v = 0.3503592783048583E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5281547442266309E+0 + b = 0.3439863920645423E+0 + v = 0.3541854792663162E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5645346989813992E+0 + b = 0.3815229456121914E+0 + v = 0.3565995517909428E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5988181252159848E+0 + b = 0.4175752420966734E+0 + v = 0.3578802078302898E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2850425424471603E+0 + b = 0.3562149509862536E-1 + v = 0.2958644592860982E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3324619433027876E+0 + b = 0.7330318886871096E-1 + v = 0.3119548129116835E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3785848333076282E+0 + b = 0.1123226296008472E+0 + v = 0.3250745225005984E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4232891028562115E+0 + b = 0.1521084193337708E+0 + v = 0.3355153415935208E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4664287050829722E+0 + b = 0.1921844459223610E+0 + v = 0.3435847568549328E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5078458493735726E+0 + b = 0.2321360989678303E+0 + v = 0.3495786831622488E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5473779816204180E+0 + b = 0.2715886486360520E+0 + v = 0.3537767805534621E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5848617133811376E+0 + b = 0.3101924707571355E+0 + v = 0.3564459815421428E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6201348281584888E+0 + b = 0.3476121052890973E+0 + v = 0.3578464061225468E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3852191185387871E+0 + b = 0.3763224880035108E-1 + v = 0.3239748762836212E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4325025061073423E+0 + b = 0.7659581935637135E-1 + v = 0.3345491784174287E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4778486229734490E+0 + b = 0.1163381306083900E+0 + v = 0.3429126177301782E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5211663693009000E+0 + b = 0.1563890598752899E+0 + v = 0.3492420343097421E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5623469504853703E+0 + b = 0.1963320810149200E+0 + v = 0.3537399050235257E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6012718188659246E+0 + b = 0.2357847407258738E+0 + v = 0.3566209152659172E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6378179206390117E+0 + b = 0.2743846121244060E+0 + v = 0.3581084321919782E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4836936460214534E+0 + b = 0.3895902610739024E-1 + v = 0.3426522117591512E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5293792562683797E+0 + b = 0.7871246819312640E-1 + v = 0.3491848770121379E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5726281253100033E+0 + b = 0.1187963808202981E+0 + v = 0.3539318235231476E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6133658776169068E+0 + b = 0.1587914708061787E+0 + v = 0.3570231438458694E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6515085491865307E+0 + b = 0.1983058575227646E+0 + v = 0.3586207335051714E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5778692716064976E+0 + b = 0.3977209689791542E-1 + v = 0.3541196205164025E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6207904288086192E+0 + b = 0.7990157592981152E-1 + v = 0.3574296911573953E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6608688171046802E+0 + b = 0.1199671308754309E+0 + v = 0.3591993279818963E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6656263089489130E+0 + b = 0.4015955957805969E-1 + v = 0.3595855034661997E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + + case 3470: + + v = 0.2040382730826330E-4 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + v = 0.3178149703889544E-3 + leb_tmp, start = get_lebedev_recurrence_points(3, start, a, b, v, leb_tmp) + a = 0.1721420832906233E-1 + v = 0.8288115128076110E-4 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4408875374981770E-1 + v = 0.1360883192522954E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.7594680813878681E-1 + v = 0.1766854454542662E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1108335359204799E+0 + v = 0.2083153161230153E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1476517054388567E+0 + v = 0.2333279544657158E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1856731870860615E+0 + v = 0.2532809539930247E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2243634099428821E+0 + v = 0.2692472184211158E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2633006881662727E+0 + v = 0.2819949946811885E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3021340904916283E+0 + v = 0.2920953593973030E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3405594048030089E+0 + v = 0.2999889782948352E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3783044434007372E+0 + v = 0.3060292120496902E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4151194767407910E+0 + v = 0.3105109167522192E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4507705766443257E+0 + v = 0.3136902387550312E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4850346056573187E+0 + v = 0.3157984652454632E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5176950817792470E+0 + v = 0.3170516518425422E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5485384240820989E+0 + v = 0.3176568425633755E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6039117238943308E+0 + v = 0.3177198411207062E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6279956655573113E+0 + v = 0.3175519492394733E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6493636169568952E+0 + v = 0.3174654952634756E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6677644117704504E+0 + v = 0.3175676415467654E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6829368572115624E+0 + v = 0.3178923417835410E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6946195818184121E+0 + v = 0.3183788287531909E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.7025711542057026E+0 + v = 0.3188755151918807E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.7066004767140119E+0 + v = 0.3191916889313849E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5132537689946062E-1 + v = 0.1231779611744508E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.1297994661331225E+0 + v = 0.1924661373839880E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.2188852049401307E+0 + v = 0.2380881867403424E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.3123174824903457E+0 + v = 0.2693100663037885E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.4064037620738195E+0 + v = 0.2908673382834366E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.4984958396944782E+0 + v = 0.3053914619381535E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.5864975046021365E+0 + v = 0.3143916684147777E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.6686711634580175E+0 + v = 0.3187042244055363E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.8715738780835950E-1 + b = 0.2557175233367578E-1 + v = 0.1635219535869790E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1248383123134007E+0 + b = 0.5604823383376681E-1 + v = 0.1968109917696070E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1638062693383378E+0 + b = 0.8968568601900765E-1 + v = 0.2236754342249974E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2035586203373176E+0 + b = 0.1254086651976279E+0 + v = 0.2453186687017181E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2436798975293774E+0 + b = 0.1624780150162012E+0 + v = 0.2627551791580541E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2838207507773806E+0 + b = 0.2003422342683208E+0 + v = 0.2767654860152220E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3236787502217692E+0 + b = 0.2385628026255263E+0 + v = 0.2879467027765895E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3629849554840691E+0 + b = 0.2767731148783578E+0 + v = 0.2967639918918702E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4014948081992087E+0 + b = 0.3146542308245309E+0 + v = 0.3035900684660351E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4389818379260225E+0 + b = 0.3519196415895088E+0 + v = 0.3087338237298308E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4752331143674377E+0 + b = 0.3883050984023654E+0 + v = 0.3124608838860167E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5100457318374018E+0 + b = 0.4235613423908649E+0 + v = 0.3150084294226743E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5432238388954868E+0 + b = 0.4574484717196220E+0 + v = 0.3165958398598402E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5745758685072442E+0 + b = 0.4897311639255524E+0 + v = 0.3174320440957372E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1723981437592809E+0 + b = 0.3010630597881105E-1 + v = 0.2182188909812599E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2149553257844597E+0 + b = 0.6326031554204694E-1 + v = 0.2399727933921445E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2573256081247422E+0 + b = 0.9848566980258631E-1 + v = 0.2579796133514652E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2993163751238106E+0 + b = 0.1350835952384266E+0 + v = 0.2727114052623535E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3407238005148000E+0 + b = 0.1725184055442181E+0 + v = 0.2846327656281355E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3813454978483264E+0 + b = 0.2103559279730725E+0 + v = 0.2941491102051334E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4209848104423343E+0 + b = 0.2482278774554860E+0 + v = 0.3016049492136107E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4594519699996300E+0 + b = 0.2858099509982883E+0 + v = 0.3072949726175648E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4965640166185930E+0 + b = 0.3228075659915428E+0 + v = 0.3114768142886460E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5321441655571562E+0 + b = 0.3589459907204151E+0 + v = 0.3143823673666223E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5660208438582166E+0 + b = 0.3939630088864310E+0 + v = 0.3162269764661535E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5980264315964364E+0 + b = 0.4276029922949089E+0 + v = 0.3172164663759821E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2644215852350733E+0 + b = 0.3300939429072552E-1 + v = 0.2554575398967435E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3090113743443063E+0 + b = 0.6803887650078501E-1 + v = 0.2701704069135677E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3525871079197808E+0 + b = 0.1044326136206709E+0 + v = 0.2823693413468940E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3950418005354029E+0 + b = 0.1416751597517679E+0 + v = 0.2922898463214289E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4362475663430163E+0 + b = 0.1793408610504821E+0 + v = 0.3001829062162428E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4760661812145854E+0 + b = 0.2170630750175722E+0 + v = 0.3062890864542953E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5143551042512103E+0 + b = 0.2545145157815807E+0 + v = 0.3108328279264746E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5509709026935597E+0 + b = 0.2913940101706601E+0 + v = 0.3140243146201245E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5857711030329428E+0 + b = 0.3274169910910705E+0 + v = 0.3160638030977130E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6186149917404392E+0 + b = 0.3623081329317265E+0 + v = 0.3171462882206275E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3586894569557064E+0 + b = 0.3497354386450040E-1 + v = 0.2812388416031796E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4035266610019441E+0 + b = 0.7129736739757095E-1 + v = 0.2912137500288045E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4467775312332510E+0 + b = 0.1084758620193165E+0 + v = 0.2993241256502206E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4883638346608543E+0 + b = 0.1460915689241772E+0 + v = 0.3057101738983822E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5281908348434601E+0 + b = 0.1837790832369980E+0 + v = 0.3105319326251432E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5661542687149311E+0 + b = 0.2212075390874021E+0 + v = 0.3139565514428167E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6021450102031452E+0 + b = 0.2580682841160985E+0 + v = 0.3161543006806366E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6360520783610050E+0 + b = 0.2940656362094121E+0 + v = 0.3172985960613294E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4521611065087196E+0 + b = 0.3631055365867002E-1 + v = 0.2989400336901431E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4959365651560963E+0 + b = 0.7348318468484350E-1 + v = 0.3054555883947677E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5376815804038283E+0 + b = 0.1111087643812648E+0 + v = 0.3104764960807702E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5773314480243768E+0 + b = 0.1488226085145408E+0 + v = 0.3141015825977616E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6148113245575056E+0 + b = 0.1862892274135151E+0 + v = 0.3164520621159896E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6500407462842380E+0 + b = 0.2231909701714456E+0 + v = 0.3176652305912204E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5425151448707213E+0 + b = 0.3718201306118944E-1 + v = 0.3105097161023939E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5841860556907931E+0 + b = 0.7483616335067346E-1 + v = 0.3143014117890550E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6234632186851500E+0 + b = 0.1125990834266120E+0 + v = 0.3168172866287200E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6602934551848843E+0 + b = 0.1501303813157619E+0 + v = 0.3181401865570968E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6278573968375105E+0 + b = 0.3767559930245720E-1 + v = 0.3170663659156037E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6665611711264577E+0 + b = 0.7548443301360158E-1 + v = 0.3185447944625510E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + + case 3890: + + v = 0.1807395252196920E-4 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + v = 0.2848008782238827E-3 + leb_tmp, start = get_lebedev_recurrence_points(2, start, a, b, v, leb_tmp) + v = 0.2836065837530581E-3 + leb_tmp, start = get_lebedev_recurrence_points(3, start, a, b, v, leb_tmp) + a = 0.1587876419858352E-1 + v = 0.7013149266673816E-4 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4069193593751206E-1 + v = 0.1162798021956766E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.7025888115257997E-1 + v = 0.1518728583972105E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1027495450028704E+0 + v = 0.1798796108216934E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1371457730893426E+0 + v = 0.2022593385972785E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1727758532671953E+0 + v = 0.2203093105575464E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2091492038929037E+0 + v = 0.2349294234299855E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2458813281751915E+0 + v = 0.2467682058747003E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2826545859450066E+0 + v = 0.2563092683572224E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3191957291799622E+0 + v = 0.2639253896763318E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3552621469299578E+0 + v = 0.2699137479265108E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3906329503406230E+0 + v = 0.2745196420166739E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4251028614093031E+0 + v = 0.2779529197397593E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4584777520111870E+0 + v = 0.2803996086684265E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4905711358710193E+0 + v = 0.2820302356715842E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5212011669847385E+0 + v = 0.2830056747491068E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5501878488737995E+0 + v = 0.2834808950776839E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6025037877479342E+0 + v = 0.2835282339078929E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6254572689549016E+0 + v = 0.2833819267065800E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6460107179528248E+0 + v = 0.2832858336906784E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6639541138154251E+0 + v = 0.2833268235451244E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6790688515667495E+0 + v = 0.2835432677029253E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6911338580371512E+0 + v = 0.2839091722743049E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6999385956126490E+0 + v = 0.2843308178875841E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.7053037748656896E+0 + v = 0.2846703550533846E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4732224387180115E-1 + v = 0.1051193406971900E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.1202100529326803E+0 + v = 0.1657871838796974E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.2034304820664855E+0 + v = 0.2064648113714232E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.2912285643573002E+0 + v = 0.2347942745819741E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.3802361792726768E+0 + v = 0.2547775326597726E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.4680598511056146E+0 + v = 0.2686876684847025E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.5528151052155599E+0 + v = 0.2778665755515867E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.6329386307803041E+0 + v = 0.2830996616782929E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.8056516651369069E-1 + b = 0.2363454684003124E-1 + v = 0.1403063340168372E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1156476077139389E+0 + b = 0.5191291632545936E-1 + v = 0.1696504125939477E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1520473382760421E+0 + b = 0.8322715736994519E-1 + v = 0.1935787242745390E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1892986699745931E+0 + b = 0.1165855667993712E+0 + v = 0.2130614510521968E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2270194446777792E+0 + b = 0.1513077167409504E+0 + v = 0.2289381265931048E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2648908185093273E+0 + b = 0.1868882025807859E+0 + v = 0.2418630292816186E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3026389259574136E+0 + b = 0.2229277629776224E+0 + v = 0.2523400495631193E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3400220296151384E+0 + b = 0.2590951840746235E+0 + v = 0.2607623973449605E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3768217953335510E+0 + b = 0.2951047291750847E+0 + v = 0.2674441032689209E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4128372900921884E+0 + b = 0.3307019714169930E+0 + v = 0.2726432360343356E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4478807131815630E+0 + b = 0.3656544101087634E+0 + v = 0.2765787685924545E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4817742034089257E+0 + b = 0.3997448951939695E+0 + v = 0.2794428690642224E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5143472814653344E+0 + b = 0.4327667110812024E+0 + v = 0.2814099002062895E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5454346213905650E+0 + b = 0.4645196123532293E+0 + v = 0.2826429531578994E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5748739313170252E+0 + b = 0.4948063555703345E+0 + v = 0.2832983542550884E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1599598738286342E+0 + b = 0.2792357590048985E-1 + v = 0.1886695565284976E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1998097412500951E+0 + b = 0.5877141038139065E-1 + v = 0.2081867882748234E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2396228952566202E+0 + b = 0.9164573914691377E-1 + v = 0.2245148680600796E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2792228341097746E+0 + b = 0.1259049641962687E+0 + v = 0.2380370491511872E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3184251107546741E+0 + b = 0.1610594823400863E+0 + v = 0.2491398041852455E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3570481164426244E+0 + b = 0.1967151653460898E+0 + v = 0.2581632405881230E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3949164710492144E+0 + b = 0.2325404606175168E+0 + v = 0.2653965506227417E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4318617293970503E+0 + b = 0.2682461141151439E+0 + v = 0.2710857216747087E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4677221009931678E+0 + b = 0.3035720116011973E+0 + v = 0.2754434093903659E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5023417939270955E+0 + b = 0.3382781859197439E+0 + v = 0.2786579932519380E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5355701836636128E+0 + b = 0.3721383065625942E+0 + v = 0.2809011080679474E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5672608451328771E+0 + b = 0.4049346360466055E+0 + v = 0.2823336184560987E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5972704202540162E+0 + b = 0.4364538098633802E+0 + v = 0.2831101175806309E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2461687022333596E+0 + b = 0.3070423166833368E-1 + v = 0.2221679970354546E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2881774566286831E+0 + b = 0.6338034669281885E-1 + v = 0.2356185734270703E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3293963604116978E+0 + b = 0.9742862487067941E-1 + v = 0.2469228344805590E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3697303822241377E+0 + b = 0.1323799532282290E+0 + v = 0.2562726348642046E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4090663023135127E+0 + b = 0.1678497018129336E+0 + v = 0.2638756726753028E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4472819355411712E+0 + b = 0.2035095105326114E+0 + v = 0.2699311157390862E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4842513377231437E+0 + b = 0.2390692566672091E+0 + v = 0.2746233268403837E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5198477629962928E+0 + b = 0.2742649818076149E+0 + v = 0.2781225674454771E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5539453011883145E+0 + b = 0.3088503806580094E+0 + v = 0.2805881254045684E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5864196762401251E+0 + b = 0.3425904245906614E+0 + v = 0.2821719877004913E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6171484466668390E+0 + b = 0.3752562294789468E+0 + v = 0.2830222502333124E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3350337830565727E+0 + b = 0.3261589934634747E-1 + v = 0.2457995956744870E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3775773224758284E+0 + b = 0.6658438928081572E-1 + v = 0.2551474407503706E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4188155229848973E+0 + b = 0.1014565797157954E+0 + v = 0.2629065335195311E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4586805892009344E+0 + b = 0.1368573320843822E+0 + v = 0.2691900449925075E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4970895714224235E+0 + b = 0.1724614851951608E+0 + v = 0.2741275485754276E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5339505133960747E+0 + b = 0.2079779381416412E+0 + v = 0.2778530970122595E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5691665792531440E+0 + b = 0.2431385788322288E+0 + v = 0.2805010567646741E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6026387682680377E+0 + b = 0.2776901883049853E+0 + v = 0.2822055834031040E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6342676150163307E+0 + b = 0.3113881356386632E+0 + v = 0.2831016901243473E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4237951119537067E+0 + b = 0.3394877848664351E-1 + v = 0.2624474901131803E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4656918683234929E+0 + b = 0.6880219556291447E-1 + v = 0.2688034163039377E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5058857069185980E+0 + b = 0.1041946859721635E+0 + v = 0.2738932751287636E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5443204666713996E+0 + b = 0.1398039738736393E+0 + v = 0.2777944791242523E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5809298813759742E+0 + b = 0.1753373381196155E+0 + v = 0.2806011661660987E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6156416039447128E+0 + b = 0.2105215793514010E+0 + v = 0.2824181456597460E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6483801351066604E+0 + b = 0.2450953312157051E+0 + v = 0.2833585216577828E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5103616577251688E+0 + b = 0.3485560643800719E-1 + v = 0.2738165236962878E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5506738792580681E+0 + b = 0.7026308631512033E-1 + v = 0.2778365208203180E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5889573040995292E+0 + b = 0.1059035061296403E+0 + v = 0.2807852940418966E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6251641589516930E+0 + b = 0.1414823925236026E+0 + v = 0.2827245949674705E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6592414921570178E+0 + b = 0.1767207908214530E+0 + v = 0.2837342344829828E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5930314017533384E+0 + b = 0.3542189339561672E-1 + v = 0.2809233907610981E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6309812253390175E+0 + b = 0.7109574040369549E-1 + v = 0.2829930809742694E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6666296011353230E+0 + b = 0.1067259792282730E+0 + v = 0.2841097874111479E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6703715271049922E+0 + b = 0.3569455268820809E-1 + v = 0.2843455206008783E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + + case 4334: + + v = 0.1449063022537883E-4 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + v = 0.2546377329828424E-3 + leb_tmp, start = get_lebedev_recurrence_points(3, start, a, b, v, leb_tmp) + a = 0.1462896151831013E-1 + v = 0.6018432961087496E-4 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3769840812493139E-1 + v = 0.1002286583263673E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6524701904096891E-1 + v = 0.1315222931028093E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.9560543416134648E-1 + v = 0.1564213746876724E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1278335898929198E+0 + v = 0.1765118841507736E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1613096104466031E+0 + v = 0.1928737099311080E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1955806225745371E+0 + v = 0.2062658534263270E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2302935218498028E+0 + v = 0.2172395445953787E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2651584344113027E+0 + v = 0.2262076188876047E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2999276825183209E+0 + v = 0.2334885699462397E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3343828669718798E+0 + v = 0.2393355273179203E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3683265013750518E+0 + v = 0.2439559200468863E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4015763206518108E+0 + v = 0.2475251866060002E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4339612026399770E+0 + v = 0.2501965558158773E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4653180651114582E+0 + v = 0.2521081407925925E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4954893331080803E+0 + v = 0.2533881002388081E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5243207068924930E+0 + v = 0.2541582900848261E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5516590479041704E+0 + v = 0.2545365737525860E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6012371927804176E+0 + v = 0.2545726993066799E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6231574466449819E+0 + v = 0.2544456197465555E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6429416514181271E+0 + v = 0.2543481596881064E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6604124272943595E+0 + v = 0.2543506451429194E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6753851470408250E+0 + v = 0.2544905675493763E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6876717970626160E+0 + v = 0.2547611407344429E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6970895061319234E+0 + v = 0.2551060375448869E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.7034746912553310E+0 + v = 0.2554291933816039E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.7067017217542295E+0 + v = 0.2556255710686343E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4382223501131123E-1 + v = 0.9041339695118195E-4 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.1117474077400006E+0 + v = 0.1438426330079022E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.1897153252911440E+0 + v = 0.1802523089820518E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.2724023009910331E+0 + v = 0.2060052290565496E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.3567163308709902E+0 + v = 0.2245002248967466E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.4404784483028087E+0 + v = 0.2377059847731150E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.5219833154161411E+0 + v = 0.2468118955882525E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.5998179868977553E+0 + v = 0.2525410872966528E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.6727803154548222E+0 + v = 0.2553101409933397E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.7476563943166086E-1 + b = 0.2193168509461185E-1 + v = 0.1212879733668632E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1075341482001416E+0 + b = 0.4826419281533887E-1 + v = 0.1472872881270931E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1416344885203259E+0 + b = 0.7751191883575742E-1 + v = 0.1686846601010828E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1766325315388586E+0 + b = 0.1087558139247680E+0 + v = 0.1862698414660208E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2121744174481514E+0 + b = 0.1413661374253096E+0 + v = 0.2007430956991861E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2479669443408145E+0 + b = 0.1748768214258880E+0 + v = 0.2126568125394796E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2837600452294113E+0 + b = 0.2089216406612073E+0 + v = 0.2224394603372113E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3193344933193984E+0 + b = 0.2431987685545972E+0 + v = 0.2304264522673135E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3544935442438745E+0 + b = 0.2774497054377770E+0 + v = 0.2368854288424087E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3890571932288154E+0 + b = 0.3114460356156915E+0 + v = 0.2420352089461772E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4228581214259090E+0 + b = 0.3449806851913012E+0 + v = 0.2460597113081295E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4557387211304052E+0 + b = 0.3778618641248256E+0 + v = 0.2491181912257687E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4875487950541643E+0 + b = 0.4099086391698978E+0 + v = 0.2513528194205857E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5181436529962997E+0 + b = 0.4409474925853973E+0 + v = 0.2528943096693220E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5473824095600661E+0 + b = 0.4708094517711291E+0 + v = 0.2538660368488136E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5751263398976174E+0 + b = 0.4993275140354637E+0 + v = 0.2543868648299022E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1489515746840028E+0 + b = 0.2599381993267017E-1 + v = 0.1642595537825183E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1863656444351767E+0 + b = 0.5479286532462190E-1 + v = 0.1818246659849308E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2238602880356348E+0 + b = 0.8556763251425254E-1 + v = 0.1966565649492420E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2612723375728160E+0 + b = 0.1177257802267011E+0 + v = 0.2090677905657991E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2984332990206190E+0 + b = 0.1508168456192700E+0 + v = 0.2193820409510504E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3351786584663333E+0 + b = 0.1844801892177727E+0 + v = 0.2278870827661928E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3713505522209120E+0 + b = 0.2184145236087598E+0 + v = 0.2348283192282090E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4067981098954663E+0 + b = 0.2523590641486229E+0 + v = 0.2404139755581477E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4413769993687534E+0 + b = 0.2860812976901373E+0 + v = 0.2448227407760734E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4749487182516394E+0 + b = 0.3193686757808996E+0 + v = 0.2482110455592573E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5073798105075426E+0 + b = 0.3520226949547602E+0 + v = 0.2507192397774103E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5385410448878654E+0 + b = 0.3838544395667890E+0 + v = 0.2524765968534880E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5683065353670530E+0 + b = 0.4146810037640963E+0 + v = 0.2536052388539425E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5965527620663510E+0 + b = 0.4443224094681121E+0 + v = 0.2542230588033068E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2299227700856157E+0 + b = 0.2865757664057584E-1 + v = 0.1944817013047896E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2695752998553267E+0 + b = 0.5923421684485993E-1 + v = 0.2067862362746635E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3086178716611389E+0 + b = 0.9117817776057715E-1 + v = 0.2172440734649114E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3469649871659077E+0 + b = 0.1240593814082605E+0 + v = 0.2260125991723423E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3845153566319655E+0 + b = 0.1575272058259175E+0 + v = 0.2332655008689523E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4211600033403215E+0 + b = 0.1912845163525413E+0 + v = 0.2391699681532458E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4567867834329882E+0 + b = 0.2250710177858171E+0 + v = 0.2438801528273928E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4912829319232061E+0 + b = 0.2586521303440910E+0 + v = 0.2475370504260665E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5245364793303812E+0 + b = 0.2918112242865407E+0 + v = 0.2502707235640574E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5564369788915756E+0 + b = 0.3243439239067890E+0 + v = 0.2522031701054241E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5868757697775287E+0 + b = 0.3560536787835351E+0 + v = 0.2534511269978784E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6157458853519617E+0 + b = 0.3867480821242581E+0 + v = 0.2541284914955151E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3138461110672113E+0 + b = 0.3051374637507278E-1 + v = 0.2161509250688394E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3542495872050569E+0 + b = 0.6237111233730755E-1 + v = 0.2248778513437852E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3935751553120181E+0 + b = 0.9516223952401907E-1 + v = 0.2322388803404617E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4317634668111147E+0 + b = 0.1285467341508517E+0 + v = 0.2383265471001355E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4687413842250821E+0 + b = 0.1622318931656033E+0 + v = 0.2432476675019525E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5044274237060283E+0 + b = 0.1959581153836453E+0 + v = 0.2471122223750674E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5387354077925727E+0 + b = 0.2294888081183837E+0 + v = 0.2500291752486870E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5715768898356105E+0 + b = 0.2626031152713945E+0 + v = 0.2521055942764682E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6028627200136111E+0 + b = 0.2950904075286713E+0 + v = 0.2534472785575503E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6325039812653463E+0 + b = 0.3267458451113286E+0 + v = 0.2541599713080121E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3981986708423407E+0 + b = 0.3183291458749821E-1 + v = 0.2317380975862936E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4382791182133300E+0 + b = 0.6459548193880908E-1 + v = 0.2378550733719775E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4769233057218166E+0 + b = 0.9795757037087952E-1 + v = 0.2428884456739118E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5140823911194238E+0 + b = 0.1316307235126655E+0 + v = 0.2469002655757292E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5496977833862983E+0 + b = 0.1653556486358704E+0 + v = 0.2499657574265851E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5837047306512727E+0 + b = 0.1988931724126510E+0 + v = 0.2521676168486082E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6160349566926879E+0 + b = 0.2320174581438950E+0 + v = 0.2535935662645334E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6466185353209440E+0 + b = 0.2645106562168662E+0 + v = 0.2543356743363214E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4810835158795404E+0 + b = 0.3275917807743992E-1 + v = 0.2427353285201535E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5199925041324341E+0 + b = 0.6612546183967181E-1 + v = 0.2468258039744386E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5571717692207494E+0 + b = 0.9981498331474143E-1 + v = 0.2500060956440310E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5925789250836378E+0 + b = 0.1335687001410374E+0 + v = 0.2523238365420979E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6261658523859670E+0 + b = 0.1671444402896463E+0 + v = 0.2538399260252846E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6578811126669331E+0 + b = 0.2003106382156076E+0 + v = 0.2546255927268069E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5609624612998100E+0 + b = 0.3337500940231335E-1 + v = 0.2500583360048449E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5979959659984670E+0 + b = 0.6708750335901803E-1 + v = 0.2524777638260203E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6330523711054002E+0 + b = 0.1008792126424850E+0 + v = 0.2540951193860656E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6660960998103972E+0 + b = 0.1345050343171794E+0 + v = 0.2549524085027472E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6365384364585819E+0 + b = 0.3372799460737052E-1 + v = 0.2542569507009158E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6710994302899275E+0 + b = 0.6755249309678028E-1 + v = 0.2552114127580376E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + + case 4802: + + v = 0.9687521879420705E-4 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + v = 0.2307897895367918E-3 + leb_tmp, start = get_lebedev_recurrence_points(2, start, a, b, v, leb_tmp) + v = 0.2297310852498558E-3 + leb_tmp, start = get_lebedev_recurrence_points(3, start, a, b, v, leb_tmp) + a = 0.2335728608887064E-1 + v = 0.7386265944001919E-4 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4352987836550653E-1 + v = 0.8257977698542210E-4 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6439200521088801E-1 + v = 0.9706044762057630E-4 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.9003943631993181E-1 + v = 0.1302393847117003E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1196706615548473E+0 + v = 0.1541957004600968E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1511715412838134E+0 + v = 0.1704459770092199E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1835982828503801E+0 + v = 0.1827374890942906E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2165081259155405E+0 + v = 0.1926360817436107E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2496208720417563E+0 + v = 0.2008010239494833E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2827200673567900E+0 + v = 0.2075635983209175E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3156190823994346E+0 + v = 0.2131306638690909E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3481476793749115E+0 + v = 0.2176562329937335E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3801466086947226E+0 + v = 0.2212682262991018E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4114652119634011E+0 + v = 0.2240799515668565E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4419598786519751E+0 + v = 0.2261959816187525E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4714925949329543E+0 + v = 0.2277156368808855E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4999293972879466E+0 + v = 0.2287351772128336E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5271387221431248E+0 + v = 0.2293490814084085E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5529896780837761E+0 + v = 0.2296505312376273E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6000856099481712E+0 + v = 0.2296793832318756E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6210562192785175E+0 + v = 0.2295785443842974E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6401165879934240E+0 + v = 0.2295017931529102E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6571144029244334E+0 + v = 0.2295059638184868E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6718910821718863E+0 + v = 0.2296232343237362E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6842845591099010E+0 + v = 0.2298530178740771E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6941353476269816E+0 + v = 0.2301579790280501E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.7012965242212991E+0 + v = 0.2304690404996513E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.7056471428242644E+0 + v = 0.2307027995907102E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4595557643585895E-1 + v = 0.9312274696671092E-4 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.1049316742435023E+0 + v = 0.1199919385876926E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.1773548879549274E+0 + v = 0.1598039138877690E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.2559071411236127E+0 + v = 0.1822253763574900E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.3358156837985898E+0 + v = 0.1988579593655040E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.4155835743763893E+0 + v = 0.2112620102533307E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.4937894296167472E+0 + v = 0.2201594887699007E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.5691569694793316E+0 + v = 0.2261622590895036E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.6405840854894251E+0 + v = 0.2296458453435705E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.7345133894143348E-1 + b = 0.2177844081486067E-1 + v = 0.1006006990267000E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1009859834044931E+0 + b = 0.4590362185775188E-1 + v = 0.1227676689635876E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1324289619748758E+0 + b = 0.7255063095690877E-1 + v = 0.1467864280270117E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1654272109607127E+0 + b = 0.1017825451960684E+0 + v = 0.1644178912101232E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1990767186776461E+0 + b = 0.1325652320980364E+0 + v = 0.1777664890718961E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2330125945523278E+0 + b = 0.1642765374496765E+0 + v = 0.1884825664516690E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2670080611108287E+0 + b = 0.1965360374337889E+0 + v = 0.1973269246453848E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3008753376294316E+0 + b = 0.2290726770542238E+0 + v = 0.2046767775855328E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3344475596167860E+0 + b = 0.2616645495370823E+0 + v = 0.2107600125918040E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3675709724070786E+0 + b = 0.2941150728843141E+0 + v = 0.2157416362266829E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4001000887587812E+0 + b = 0.3262440400919066E+0 + v = 0.2197557816920721E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4318956350436028E+0 + b = 0.3578835350611916E+0 + v = 0.2229192611835437E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4628239056795531E+0 + b = 0.3888751854043678E+0 + v = 0.2253385110212775E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4927563229773636E+0 + b = 0.4190678003222840E+0 + v = 0.2271137107548774E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5215687136707969E+0 + b = 0.4483151836883852E+0 + v = 0.2283414092917525E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5491402346984905E+0 + b = 0.4764740676087880E+0 + v = 0.2291161673130077E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5753520160126075E+0 + b = 0.5034021310998277E+0 + v = 0.2295313908576598E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1388326356417754E+0 + b = 0.2435436510372806E-1 + v = 0.1438204721359031E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1743686900537244E+0 + b = 0.5118897057342652E-1 + v = 0.1607738025495257E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2099737037950268E+0 + b = 0.8014695048539634E-1 + v = 0.1741483853528379E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2454492590908548E+0 + b = 0.1105117874155699E+0 + v = 0.1851918467519151E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2807219257864278E+0 + b = 0.1417950531570966E+0 + v = 0.1944628638070613E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3156842271975842E+0 + b = 0.1736604945719597E+0 + v = 0.2022495446275152E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3502090945177752E+0 + b = 0.2058466324693981E+0 + v = 0.2087462382438514E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3841684849519686E+0 + b = 0.2381284261195919E+0 + v = 0.2141074754818308E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4174372367906016E+0 + b = 0.2703031270422569E+0 + v = 0.2184640913748162E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4498926465011892E+0 + b = 0.3021845683091309E+0 + v = 0.2219309165220329E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4814146229807701E+0 + b = 0.3335993355165720E+0 + v = 0.2246123118340624E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5118863625734701E+0 + b = 0.3643833735518232E+0 + v = 0.2266062766915125E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5411947455119144E+0 + b = 0.3943789541958179E+0 + v = 0.2280072952230796E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5692301500357246E+0 + b = 0.4234320144403542E+0 + v = 0.2289082025202583E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5958857204139576E+0 + b = 0.4513897947419260E+0 + v = 0.2294012695120025E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2156270284785766E+0 + b = 0.2681225755444491E-1 + v = 0.1722434488736947E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2532385054909710E+0 + b = 0.5557495747805614E-1 + v = 0.1830237421455091E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2902564617771537E+0 + b = 0.8569368062950249E-1 + v = 0.1923855349997633E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3266979823143256E+0 + b = 0.1167367450324135E+0 + v = 0.2004067861936271E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3625039627493614E+0 + b = 0.1483861994003304E+0 + v = 0.2071817297354263E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3975838937548699E+0 + b = 0.1803821503011405E+0 + v = 0.2128250834102103E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4318396099009774E+0 + b = 0.2124962965666424E+0 + v = 0.2174513719440102E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4651706555732742E+0 + b = 0.2445221837805913E+0 + v = 0.2211661839150214E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4974752649620969E+0 + b = 0.2762701224322987E+0 + v = 0.2240665257813102E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5286517579627517E+0 + b = 0.3075627775211328E+0 + v = 0.2262439516632620E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5586001195731895E+0 + b = 0.3382311089826877E+0 + v = 0.2277874557231869E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5872229902021319E+0 + b = 0.3681108834741399E+0 + v = 0.2287854314454994E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6144258616235123E+0 + b = 0.3970397446872839E+0 + v = 0.2293268499615575E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2951676508064861E+0 + b = 0.2867499538750441E-1 + v = 0.1912628201529828E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3335085485472725E+0 + b = 0.5867879341903510E-1 + v = 0.1992499672238701E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3709561760636381E+0 + b = 0.8961099205022284E-1 + v = 0.2061275533454027E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4074722861667498E+0 + b = 0.1211627927626297E+0 + v = 0.2119318215968572E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4429923648839117E+0 + b = 0.1530748903554898E+0 + v = 0.2167416581882652E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4774428052721736E+0 + b = 0.1851176436721877E+0 + v = 0.2206430730516600E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5107446539535904E+0 + b = 0.2170829107658179E+0 + v = 0.2237186938699523E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5428151370542935E+0 + b = 0.2487786689026271E+0 + v = 0.2260480075032884E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5735699292556964E+0 + b = 0.2800239952795016E+0 + v = 0.2277098884558542E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6029253794562866E+0 + b = 0.3106445702878119E+0 + v = 0.2287845715109671E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6307998987073145E+0 + b = 0.3404689500841194E+0 + v = 0.2293547268236294E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3752652273692719E+0 + b = 0.2997145098184479E-1 + v = 0.2056073839852528E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4135383879344028E+0 + b = 0.6086725898678011E-1 + v = 0.2114235865831876E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4506113885153907E+0 + b = 0.9238849548435643E-1 + v = 0.2163175629770551E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4864401554606072E+0 + b = 0.1242786603851851E+0 + v = 0.2203392158111650E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5209708076611709E+0 + b = 0.1563086731483386E+0 + v = 0.2235473176847839E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5541422135830122E+0 + b = 0.1882696509388506E+0 + v = 0.2260024141501235E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5858880915113817E+0 + b = 0.2199672979126059E+0 + v = 0.2277675929329182E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6161399390603444E+0 + b = 0.2512165482924867E+0 + v = 0.2289102112284834E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6448296482255090E+0 + b = 0.2818368701871888E+0 + v = 0.2295027954625118E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4544796274917948E+0 + b = 0.3088970405060312E-1 + v = 0.2161281589879992E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4919389072146628E+0 + b = 0.6240947677636835E-1 + v = 0.2201980477395102E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5279313026985183E+0 + b = 0.9430706144280313E-1 + v = 0.2234952066593166E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5624169925571135E+0 + b = 0.1263547818770374E+0 + v = 0.2260540098520838E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5953484627093287E+0 + b = 0.1583430788822594E+0 + v = 0.2279157981899988E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6266730715339185E+0 + b = 0.1900748462555988E+0 + v = 0.2291296918565571E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6563363204278871E+0 + b = 0.2213599519592567E+0 + v = 0.2297533752536649E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5314574716585696E+0 + b = 0.3152508811515374E-1 + v = 0.2234927356465995E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5674614932298185E+0 + b = 0.6343865291465561E-1 + v = 0.2261288012985219E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6017706004970264E+0 + b = 0.9551503504223951E-1 + v = 0.2280818160923688E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6343471270264178E+0 + b = 0.1275440099801196E+0 + v = 0.2293773295180159E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6651494599127802E+0 + b = 0.1593252037671960E+0 + v = 0.2300528767338634E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6050184986005704E+0 + b = 0.3192538338496105E-1 + v = 0.2281893855065666E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6390163550880400E+0 + b = 0.6402824353962306E-1 + v = 0.2295720444840727E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6711199107088448E+0 + b = 0.9609805077002909E-1 + v = 0.2303227649026753E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6741354429572275E+0 + b = 0.3211853196273233E-1 + v = 0.2304831913227114E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + + case 5294: + + v = 0.9080510764308163E-4 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + v = 0.2084824361987793E-3 + leb_tmp, start = get_lebedev_recurrence_points(3, start, a, b, v, leb_tmp) + a = 0.2303261686261450E-1 + v = 0.5011105657239616E-4 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3757208620162394E-1 + v = 0.5942520409683854E-4 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5821912033821852E-1 + v = 0.9564394826109721E-4 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.8403127529194872E-1 + v = 0.1185530657126338E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1122927798060578E+0 + v = 0.1364510114230331E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1420125319192987E+0 + v = 0.1505828825605415E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1726396437341978E+0 + v = 0.1619298749867023E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2038170058115696E+0 + v = 0.1712450504267789E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2352849892876508E+0 + v = 0.1789891098164999E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2668363354312461E+0 + v = 0.1854474955629795E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2982941279900452E+0 + v = 0.1908148636673661E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3295002922087076E+0 + v = 0.1952377405281833E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3603094918363593E+0 + v = 0.1988349254282232E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3905857895173920E+0 + v = 0.2017079807160050E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4202005758160837E+0 + v = 0.2039473082709094E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4490310061597227E+0 + v = 0.2056360279288953E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4769586160311491E+0 + v = 0.2068525823066865E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5038679887049750E+0 + v = 0.2076724877534488E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5296454286519961E+0 + v = 0.2081694278237885E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5541776207164850E+0 + v = 0.2084157631219326E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5990467321921213E+0 + v = 0.2084381531128593E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6191467096294587E+0 + v = 0.2083476277129307E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6375251212901849E+0 + v = 0.2082686194459732E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6540514381131168E+0 + v = 0.2082475686112415E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6685899064391510E+0 + v = 0.2083139860289915E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6810013009681648E+0 + v = 0.2084745561831237E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6911469578730340E+0 + v = 0.2087091313375890E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6988956915141736E+0 + v = 0.2089718413297697E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.7041335794868720E+0 + v = 0.2092003303479793E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.7067754398018567E+0 + v = 0.2093336148263241E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3840368707853623E-1 + v = 0.7591708117365267E-4 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.9835485954117399E-1 + v = 0.1083383968169186E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.1665774947612998E+0 + v = 0.1403019395292510E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.2405702335362910E+0 + v = 0.1615970179286436E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.3165270770189046E+0 + v = 0.1771144187504911E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.3927386145645443E+0 + v = 0.1887760022988168E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.4678825918374656E+0 + v = 0.1973474670768214E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.5408022024266935E+0 + v = 0.2033787661234659E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.6104967445752438E+0 + v = 0.2072343626517331E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.6760910702685738E+0 + v = 0.2091177834226918E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.6655644120217392E-1 + b = 0.1936508874588424E-1 + v = 0.9316684484675566E-4 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.9446246161270182E-1 + b = 0.4252442002115869E-1 + v = 0.1116193688682976E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1242651925452509E+0 + b = 0.6806529315354374E-1 + v = 0.1298623551559414E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1553438064846751E+0 + b = 0.9560957491205369E-1 + v = 0.1450236832456426E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1871137110542670E+0 + b = 0.1245931657452888E+0 + v = 0.1572719958149914E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2192612628836257E+0 + b = 0.1545385828778978E+0 + v = 0.1673234785867195E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2515682807206955E+0 + b = 0.1851004249723368E+0 + v = 0.1756860118725188E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2838535866287290E+0 + b = 0.2160182608272384E+0 + v = 0.1826776290439367E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3159578817528521E+0 + b = 0.2470799012277111E+0 + v = 0.1885116347992865E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3477370882791392E+0 + b = 0.2781014208986402E+0 + v = 0.1933457860170574E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3790576960890540E+0 + b = 0.3089172523515731E+0 + v = 0.1973060671902064E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4097938317810200E+0 + b = 0.3393750055472244E+0 + v = 0.2004987099616311E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4398256572859637E+0 + b = 0.3693322470987730E+0 + v = 0.2030170909281499E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4690384114718480E+0 + b = 0.3986541005609877E+0 + v = 0.2049461460119080E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4973216048301053E+0 + b = 0.4272112491408562E+0 + v = 0.2063653565200186E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5245681526132446E+0 + b = 0.4548781735309936E+0 + v = 0.2073507927381027E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5506733911803888E+0 + b = 0.4815315355023251E+0 + v = 0.2079764593256122E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5755339829522475E+0 + b = 0.5070486445801855E+0 + v = 0.2083150534968778E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1305472386056362E+0 + b = 0.2284970375722366E-1 + v = 0.1262715121590664E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1637327908216477E+0 + b = 0.4812254338288384E-1 + v = 0.1414386128545972E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1972734634149637E+0 + b = 0.7531734457511935E-1 + v = 0.1538740401313898E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2308694653110130E+0 + b = 0.1039043639882017E+0 + v = 0.1642434942331432E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2643899218338160E+0 + b = 0.1334526587117626E+0 + v = 0.1729790609237496E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2977171599622171E+0 + b = 0.1636414868936382E+0 + v = 0.1803505190260828E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3307293903032310E+0 + b = 0.1942195406166568E+0 + v = 0.1865475350079657E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3633069198219073E+0 + b = 0.2249752879943753E+0 + v = 0.1917182669679069E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3953346955922727E+0 + b = 0.2557218821820032E+0 + v = 0.1959851709034382E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4267018394184914E+0 + b = 0.2862897925213193E+0 + v = 0.1994529548117882E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4573009622571704E+0 + b = 0.3165224536636518E+0 + v = 0.2022138911146548E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4870279559856109E+0 + b = 0.3462730221636496E+0 + v = 0.2043518024208592E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5157819581450322E+0 + b = 0.3754016870282835E+0 + v = 0.2059450313018110E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5434651666465393E+0 + b = 0.4037733784993613E+0 + v = 0.2070685715318472E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5699823887764627E+0 + b = 0.4312557784139123E+0 + v = 0.2077955310694373E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5952403350947741E+0 + b = 0.4577175367122110E+0 + v = 0.2081980387824712E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2025152599210369E+0 + b = 0.2520253617719557E-1 + v = 0.1521318610377956E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2381066653274425E+0 + b = 0.5223254506119000E-1 + v = 0.1622772720185755E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2732823383651612E+0 + b = 0.8060669688588620E-1 + v = 0.1710498139420709E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3080137692611118E+0 + b = 0.1099335754081255E+0 + v = 0.1785911149448736E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3422405614587601E+0 + b = 0.1399120955959857E+0 + v = 0.1850125313687736E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3758808773890420E+0 + b = 0.1702977801651705E+0 + v = 0.1904229703933298E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4088458383438932E+0 + b = 0.2008799256601680E+0 + v = 0.1949259956121987E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4410450550841152E+0 + b = 0.2314703052180836E+0 + v = 0.1986161545363960E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4723879420561312E+0 + b = 0.2618972111375892E+0 + v = 0.2015790585641370E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5027843561874343E+0 + b = 0.2920013195600270E+0 + v = 0.2038934198707418E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5321453674452458E+0 + b = 0.3216322555190551E+0 + v = 0.2056334060538251E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5603839113834030E+0 + b = 0.3506456615934198E+0 + v = 0.2068705959462289E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5874150706875146E+0 + b = 0.3789007181306267E+0 + v = 0.2076753906106002E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6131559381660038E+0 + b = 0.4062580170572782E+0 + v = 0.2081179391734803E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2778497016394506E+0 + b = 0.2696271276876226E-1 + v = 0.1700345216228943E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3143733562261912E+0 + b = 0.5523469316960465E-1 + v = 0.1774906779990410E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3501485810261827E+0 + b = 0.8445193201626464E-1 + v = 0.1839659377002642E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3851430322303653E+0 + b = 0.1143263119336083E+0 + v = 0.1894987462975169E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4193013979470415E+0 + b = 0.1446177898344475E+0 + v = 0.1941548809452595E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4525585960458567E+0 + b = 0.1751165438438091E+0 + v = 0.1980078427252384E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4848447779622947E+0 + b = 0.2056338306745660E+0 + v = 0.2011296284744488E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5160871208276894E+0 + b = 0.2359965487229226E+0 + v = 0.2035888456966776E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5462112185696926E+0 + b = 0.2660430223139146E+0 + v = 0.2054516325352142E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5751425068101757E+0 + b = 0.2956193664498032E+0 + v = 0.2067831033092635E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6028073872853596E+0 + b = 0.3245763905312779E+0 + v = 0.2076485320284876E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6291338275278409E+0 + b = 0.3527670026206972E+0 + v = 0.2081141439525255E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3541797528439391E+0 + b = 0.2823853479435550E-1 + v = 0.1834383015469222E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3908234972074657E+0 + b = 0.5741296374713106E-1 + v = 0.1889540591777677E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4264408450107590E+0 + b = 0.8724646633650199E-1 + v = 0.1936677023597375E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4609949666553286E+0 + b = 0.1175034422915616E+0 + v = 0.1976176495066504E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4944389496536006E+0 + b = 0.1479755652628428E+0 + v = 0.2008536004560983E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5267194884346086E+0 + b = 0.1784740659484352E+0 + v = 0.2034280351712291E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5577787810220990E+0 + b = 0.2088245700431244E+0 + v = 0.2053944466027758E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5875563763536670E+0 + b = 0.2388628136570763E+0 + v = 0.2068077642882360E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6159910016391269E+0 + b = 0.2684308928769185E+0 + v = 0.2077250949661599E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6430219602956268E+0 + b = 0.2973740761960252E+0 + v = 0.2082062440705320E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4300647036213646E+0 + b = 0.2916399920493977E-1 + v = 0.1934374486546626E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4661486308935531E+0 + b = 0.5898803024755659E-1 + v = 0.1974107010484300E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5009658555287261E+0 + b = 0.8924162698525409E-1 + v = 0.2007129290388658E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5344824270447704E+0 + b = 0.1197185199637321E+0 + v = 0.2033736947471293E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5666575997416371E+0 + b = 0.1502300756161382E+0 + v = 0.2054287125902493E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5974457471404752E+0 + b = 0.1806004191913564E+0 + v = 0.2069184936818894E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6267984444116886E+0 + b = 0.2106621764786252E+0 + v = 0.2078883689808782E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6546664713575417E+0 + b = 0.2402526932671914E+0 + v = 0.2083886366116359E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5042711004437253E+0 + b = 0.2982529203607657E-1 + v = 0.2006593275470817E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5392127456774380E+0 + b = 0.6008728062339922E-1 + v = 0.2033728426135397E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5726819437668618E+0 + b = 0.9058227674571398E-1 + v = 0.2055008781377608E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6046469254207278E+0 + b = 0.1211219235803400E+0 + v = 0.2070651783518502E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6350716157434952E+0 + b = 0.1515286404791580E+0 + v = 0.2080953335094320E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6639177679185454E+0 + b = 0.1816314681255552E+0 + v = 0.2086284998988521E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5757276040972253E+0 + b = 0.3026991752575440E-1 + v = 0.2055549387644668E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6090265823139755E+0 + b = 0.6078402297870770E-1 + v = 0.2071871850267654E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6406735344387661E+0 + b = 0.9135459984176636E-1 + v = 0.2082856600431965E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6706397927793709E+0 + b = 0.1218024155966590E+0 + v = 0.2088705858819358E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6435019674426665E+0 + b = 0.3052608357660639E-1 + v = 0.2083995867536322E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6747218676375681E+0 + b = 0.6112185773983089E-1 + v = 0.2090509712889637E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + + case 5810: + + v = 0.9735347946175486E-5 + leb_tmp, start = get_lebedev_recurrence_points(1, start, a, b, v, leb_tmp) + v = 0.1907581241803167E-3 + leb_tmp, start = get_lebedev_recurrence_points(2, start, a, b, v, leb_tmp) + v = 0.1901059546737578E-3 + leb_tmp, start = get_lebedev_recurrence_points(3, start, a, b, v, leb_tmp) + a = 0.1182361662400277E-1 + v = 0.3926424538919212E-4 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3062145009138958E-1 + v = 0.6667905467294382E-4 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5329794036834243E-1 + v = 0.8868891315019135E-4 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.7848165532862220E-1 + v = 0.1066306000958872E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1054038157636201E+0 + v = 0.1214506743336128E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1335577797766211E+0 + v = 0.1338054681640871E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1625769955502252E+0 + v = 0.1441677023628504E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.1921787193412792E+0 + v = 0.1528880200826557E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2221340534690548E+0 + v = 0.1602330623773609E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2522504912791132E+0 + v = 0.1664102653445244E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.2823610860679697E+0 + v = 0.1715845854011323E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3123173966267560E+0 + v = 0.1758901000133069E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3419847036953789E+0 + v = 0.1794382485256736E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3712386456999758E+0 + v = 0.1823238106757407E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3999627649876828E+0 + v = 0.1846293252959976E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4280466458648093E+0 + v = 0.1864284079323098E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4553844360185711E+0 + v = 0.1877882694626914E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.4818736094437834E+0 + v = 0.1887716321852025E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5074138709260629E+0 + v = 0.1894381638175673E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5319061304570707E+0 + v = 0.1898454899533629E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5552514978677286E+0 + v = 0.1900497929577815E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.5981009025246183E+0 + v = 0.1900671501924092E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6173990192228116E+0 + v = 0.1899837555533510E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6351365239411131E+0 + v = 0.1899014113156229E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6512010228227200E+0 + v = 0.1898581257705106E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6654758363948120E+0 + v = 0.1898804756095753E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6778410414853370E+0 + v = 0.1899793610426402E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6881760887484110E+0 + v = 0.1901464554844117E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.6963645267094598E+0 + v = 0.1903533246259542E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.7023010617153579E+0 + v = 0.1905556158463228E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.7059004636628753E+0 + v = 0.1907037155663528E-3 + leb_tmp, start = get_lebedev_recurrence_points(4, start, a, b, v, leb_tmp) + a = 0.3552470312472575E-1 + v = 0.5992997844249967E-4 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.9151176620841283E-1 + v = 0.9749059382456978E-4 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.1566197930068980E+0 + v = 0.1241680804599158E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.2265467599271907E+0 + v = 0.1437626154299360E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.2988242318581361E+0 + v = 0.1584200054793902E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.3717482419703886E+0 + v = 0.1694436550982744E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.4440094491758889E+0 + v = 0.1776617014018108E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.5145337096756642E+0 + v = 0.1836132434440077E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.5824053672860230E+0 + v = 0.1876494727075983E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.6468283961043370E+0 + v = 0.1899906535336482E-3 + leb_tmp, start = get_lebedev_recurrence_points(5, start, a, b, v, leb_tmp) + a = 0.6095964259104373E-1 + b = 0.1787828275342931E-1 + v = 0.8143252820767350E-4 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.8811962270959388E-1 + b = 0.3953888740792096E-1 + v = 0.9998859890887728E-4 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1165936722428831E+0 + b = 0.6378121797722990E-1 + v = 0.1156199403068359E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1460232857031785E+0 + b = 0.8985890813745037E-1 + v = 0.1287632092635513E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1761197110181755E+0 + b = 0.1172606510576162E+0 + v = 0.1398378643365139E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2066471190463718E+0 + b = 0.1456102876970995E+0 + v = 0.1491876468417391E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2374076026328152E+0 + b = 0.1746153823011775E+0 + v = 0.1570855679175456E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2682305474337051E+0 + b = 0.2040383070295584E+0 + v = 0.1637483948103775E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2989653312142369E+0 + b = 0.2336788634003698E+0 + v = 0.1693500566632843E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3294762752772209E+0 + b = 0.2633632752654219E+0 + v = 0.1740322769393633E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3596390887276086E+0 + b = 0.2929369098051601E+0 + v = 0.1779126637278296E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3893383046398812E+0 + b = 0.3222592785275512E+0 + v = 0.1810908108835412E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4184653789358347E+0 + b = 0.3512004791195743E+0 + v = 0.1836529132600190E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4469172319076166E+0 + b = 0.3796385677684537E+0 + v = 0.1856752841777379E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4745950813276976E+0 + b = 0.4074575378263879E+0 + v = 0.1872270566606832E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5014034601410262E+0 + b = 0.4345456906027828E+0 + v = 0.1883722645591307E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5272493404551239E+0 + b = 0.4607942515205134E+0 + v = 0.1891714324525297E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5520413051846366E+0 + b = 0.4860961284181720E+0 + v = 0.1896827480450146E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5756887237503077E+0 + b = 0.5103447395342790E+0 + v = 0.1899628417059528E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1225039430588352E+0 + b = 0.2136455922655793E-1 + v = 0.1123301829001669E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1539113217321372E+0 + b = 0.4520926166137188E-1 + v = 0.1253698826711277E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1856213098637712E+0 + b = 0.7086468177864818E-1 + v = 0.1366266117678531E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2174998728035131E+0 + b = 0.9785239488772918E-1 + v = 0.1462736856106918E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2494128336938330E+0 + b = 0.1258106396267210E+0 + v = 0.1545076466685412E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2812321562143480E+0 + b = 0.1544529125047001E+0 + v = 0.1615096280814007E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3128372276456111E+0 + b = 0.1835433512202753E+0 + v = 0.1674366639741759E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3441145160177973E+0 + b = 0.2128813258619585E+0 + v = 0.1724225002437900E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3749567714853510E+0 + b = 0.2422913734880829E+0 + v = 0.1765810822987288E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4052621732015610E+0 + b = 0.2716163748391453E+0 + v = 0.1800104126010751E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4349335453522385E+0 + b = 0.3007127671240280E+0 + v = 0.1827960437331284E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4638776641524965E+0 + b = 0.3294470677216479E+0 + v = 0.1850140300716308E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4920046410462687E+0 + b = 0.3576932543699155E+0 + v = 0.1867333507394938E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5192273554861704E+0 + b = 0.3853307059757764E+0 + v = 0.1880178688638289E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5454609081136522E+0 + b = 0.4122425044452694E+0 + v = 0.1889278925654758E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5706220661424140E+0 + b = 0.4383139587781027E+0 + v = 0.1895213832507346E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5946286755181518E+0 + b = 0.4634312536300553E+0 + v = 0.1898548277397420E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.1905370790924295E+0 + b = 0.2371311537781979E-1 + v = 0.1349105935937341E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2242518717748009E+0 + b = 0.4917878059254806E-1 + v = 0.1444060068369326E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2577190808025936E+0 + b = 0.7595498960495142E-1 + v = 0.1526797390930008E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2908724534927187E+0 + b = 0.1036991083191100E+0 + v = 0.1598208771406474E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3236354020056219E+0 + b = 0.1321348584450234E+0 + v = 0.1659354368615331E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3559267359304543E+0 + b = 0.1610316571314789E+0 + v = 0.1711279910946440E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3876637123676956E+0 + b = 0.1901912080395707E+0 + v = 0.1754952725601440E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4187636705218842E+0 + b = 0.2194384950137950E+0 + v = 0.1791247850802529E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4491449019883107E+0 + b = 0.2486155334763858E+0 + v = 0.1820954300877716E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4787270932425445E+0 + b = 0.2775768931812335E+0 + v = 0.1844788524548449E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5074315153055574E+0 + b = 0.3061863786591120E+0 + v = 0.1863409481706220E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5351810507738336E+0 + b = 0.3343144718152556E+0 + v = 0.1877433008795068E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5619001025975381E+0 + b = 0.3618362729028427E+0 + v = 0.1887444543705232E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5875144035268046E+0 + b = 0.3886297583620408E+0 + v = 0.1894009829375006E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6119507308734495E+0 + b = 0.4145742277792031E+0 + v = 0.1897683345035198E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2619733870119463E+0 + b = 0.2540047186389353E-1 + v = 0.1517327037467653E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.2968149743237949E+0 + b = 0.5208107018543989E-1 + v = 0.1587740557483543E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3310451504860488E+0 + b = 0.7971828470885599E-1 + v = 0.1649093382274097E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3646215567376676E+0 + b = 0.1080465999177927E+0 + v = 0.1701915216193265E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3974916785279360E+0 + b = 0.1368413849366629E+0 + v = 0.1746847753144065E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4295967403772029E+0 + b = 0.1659073184763559E+0 + v = 0.1784555512007570E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4608742854473447E+0 + b = 0.1950703730454614E+0 + v = 0.1815687562112174E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4912598858949903E+0 + b = 0.2241721144376724E+0 + v = 0.1840864370663302E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5206882758945558E+0 + b = 0.2530655255406489E+0 + v = 0.1860676785390006E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5490940914019819E+0 + b = 0.2816118409731066E+0 + v = 0.1875690583743703E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5764123302025542E+0 + b = 0.3096780504593238E+0 + v = 0.1886453236347225E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6025786004213506E+0 + b = 0.3371348366394987E+0 + v = 0.1893501123329645E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6275291964794956E+0 + b = 0.3638547827694396E+0 + v = 0.1897366184519868E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3348189479861771E+0 + b = 0.2664841935537443E-1 + v = 0.1643908815152736E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.3699515545855295E+0 + b = 0.5424000066843495E-1 + v = 0.1696300350907768E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4042003071474669E+0 + b = 0.8251992715430854E-1 + v = 0.1741553103844483E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4375320100182624E+0 + b = 0.1112695182483710E+0 + v = 0.1780015282386092E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4699054490335947E+0 + b = 0.1402964116467816E+0 + v = 0.1812116787077125E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5012739879431952E+0 + b = 0.1694275117584291E+0 + v = 0.1838323158085421E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5315874883754966E+0 + b = 0.1985038235312689E+0 + v = 0.1859113119837737E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5607937109622117E+0 + b = 0.2273765660020893E+0 + v = 0.1874969220221698E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5888393223495521E+0 + b = 0.2559041492849764E+0 + v = 0.1886375612681076E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6156705979160163E+0 + b = 0.2839497251976899E+0 + v = 0.1893819575809276E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6412338809078123E+0 + b = 0.3113791060500690E+0 + v = 0.1897794748256767E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4076051259257167E+0 + b = 0.2757792290858463E-1 + v = 0.1738963926584846E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4423788125791520E+0 + b = 0.5584136834984293E-1 + v = 0.1777442359873466E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4760480917328258E+0 + b = 0.8457772087727143E-1 + v = 0.1810010815068719E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5085838725946297E+0 + b = 0.1135975846359248E+0 + v = 0.1836920318248129E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5399513637391218E+0 + b = 0.1427286904765053E+0 + v = 0.1858489473214328E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5701118433636380E+0 + b = 0.1718112740057635E+0 + v = 0.1875079342496592E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5990240530606021E+0 + b = 0.2006944855985351E+0 + v = 0.1887080239102310E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6266452685139695E+0 + b = 0.2292335090598907E+0 + v = 0.1894905752176822E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6529320971415942E+0 + b = 0.2572871512353714E+0 + v = 0.1898991061200695E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.4791583834610126E+0 + b = 0.2826094197735932E-1 + v = 0.1809065016458791E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5130373952796940E+0 + b = 0.5699871359683649E-1 + v = 0.1836297121596799E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5456252429628476E+0 + b = 0.8602712528554394E-1 + v = 0.1858426916241869E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5768956329682385E+0 + b = 0.1151748137221281E+0 + v = 0.1875654101134641E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6068186944699046E+0 + b = 0.1442811654136362E+0 + v = 0.1888240751833503E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6353622248024907E+0 + b = 0.1731930321657680E+0 + v = 0.1896497383866979E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6624927035731797E+0 + b = 0.2017619958756061E+0 + v = 0.1900775530219121E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5484933508028488E+0 + b = 0.2874219755907391E-1 + v = 0.1858525041478814E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.5810207682142106E+0 + b = 0.5778312123713695E-1 + v = 0.1876248690077947E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6120955197181352E+0 + b = 0.8695262371439526E-1 + v = 0.1889404439064607E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6416944284294319E+0 + b = 0.1160893767057166E+0 + v = 0.1898168539265290E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6697926391731260E+0 + b = 0.1450378826743251E+0 + v = 0.1902779940661772E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6147594390585488E+0 + b = 0.2904957622341456E-1 + v = 0.1890125641731815E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6455390026356783E+0 + b = 0.5823809152617197E-1 + v = 0.1899434637795751E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6747258588365477E+0 + b = 0.8740384899884715E-1 + v = 0.1904520856831751E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + a = 0.6772135750395347E+0 + b = 0.2919946135808105E-1 + v = 0.1905534498734563E-3 + leb_tmp, start = get_lebedev_recurrence_points(6, start, a, b, v, leb_tmp) + + case _: + raise Exception('Angular grid unrecognized, choices are 6, 14, 26, 38, 50, 74, 86, 110, 146, 170, 194, 230, 266, 302, 350, 434, 590, 770, 974, 1202, 1454, 1730, 2030, 2354, 2702, 3074, 3470, 3890, 4334, 4802, 5294, 5810') # noqa: E501 + + leb_tmp.n = degree + return leb_tmp + + +def get_lebedev_recurrence_points(type_, start, a, b, v, leb): + c = 0.0 + + match type_: + + case 1: + a = 1.0 + + leb.x[start] = a + leb.y[start] = 0.0 + leb.z[start] = 0.0 + leb.w[start] = 4.0 * pi * v + + leb.x[start + 1] = -a + leb.y[start + 1] = 0.0 + leb.z[start + 1] = 0.0 + leb.w[start + 1] = 4.0 * pi * v + + leb.x[start + 2] = 0.0 + leb.y[start + 2] = a + leb.z[start + 2] = 0.0 + leb.w[start + 2] = 4.0 * pi * v + + leb.x[start + 3] = 0.0 + leb.y[start + 3] = -a + leb.z[start + 3] = 0.0 + leb.w[start + 3] = 4.0 * pi * v + + leb.x[start + 4] = 0.0 + leb.y[start + 4] = 0.0 + leb.z[start + 4] = a + leb.w[start + 4] = 4.0 * pi * v + + leb.x[start + 5] = 0.0 + leb.y[start + 5] = 0.0 + leb.z[start + 5] = -a + leb.w[start + 5] = 4.0 * pi * v + start = start + 6 + + case 2: + a = sqrt(0.5) + leb.x[start] = 0.0 + leb.y[start] = a + leb.z[start] = a + leb.w[start] = 4.0 * pi * v + + leb.x[start + 1] = 0.0 + leb.y[start + 1] = -a + leb.z[start + 1] = a + leb.w[start + 1] = 4.0 * pi * v + + leb.x[start + 2] = 0.0 + leb.y[start + 2] = a + leb.z[start + 2] = -a + leb.w[start + 2] = 4.0 * pi * v + + leb.x[start + 3] = 0.0 + leb.y[start + 3] = -a + leb.z[start + 3] = -a + leb.w[start + 3] = 4.0 * pi * v + + leb.x[start + 4] = a + leb.y[start + 4] = 0.0 + leb.z[start + 4] = a + leb.w[start + 4] = 4.0 * pi * v + + leb.x[start + 5] = a + leb.y[start + 5] = 0.0 + leb.z[start + 5] = -a + leb.w[start + 5] = 4.0 * pi * v + + leb.x[start + 6] = -a + leb.y[start + 6] = 0.0 + leb.z[start + 6] = a + leb.w[start + 6] = 4.0 * pi * v + + leb.x[start + 7] = -a + leb.y[start + 7] = 0.0 + leb.z[start + 7] = -a + leb.w[start + 7] = 4.0 * pi * v + + leb.x[start + 8] = a + leb.y[start + 8] = a + leb.z[start + 8] = 0.0 + leb.w[start + 8] = 4.0 * pi * v + + leb.x[start + 9] = -a + leb.y[start + 9] = a + leb.z[start + 9] = 0.0 + leb.w[start + 9] = 4.0 * pi * v + + leb.x[start + 10] = a + leb.y[start + 10] = -a + leb.z[start + 10] = 0.0 + leb.w[start + 10] = 4.0 * pi * v + + leb.x[start + 11] = -a + leb.y[start + 11] = -a + leb.z[start + 11] = 0.0 + leb.w[start + 11] = 4.0 * pi * v + start = start + 12 + + case 3: + a = sqrt(1.0 / 3.0) + leb.x[start] = a + leb.y[start] = a + leb.z[start] = a + leb.w[start] = 4.0 * pi * v + + leb.x[start + 1] = -a + leb.y[start + 1] = a + leb.z[start + 1] = a + leb.w[start + 1] = 4.0 * pi * v + + leb.x[start + 2] = a + leb.y[start + 2] = -a + leb.z[start + 2] = a + leb.w[start + 2] = 4.0 * pi * v + + leb.x[start + 3] = a + leb.y[start + 3] = a + leb.z[start + 3] = -a + leb.w[start + 3] = 4.0 * pi * v + + leb.x[start + 4] = -a + leb.y[start + 4] = -a + leb.z[start + 4] = a + leb.w[start + 4] = 4.0 * pi * v + + leb.x[start + 5] = a + leb.y[start + 5] = -a + leb.z[start + 5] = -a + leb.w[start + 5] = 4.0 * pi * v + + leb.x[start + 6] = -a + leb.y[start + 6] = a + leb.z[start + 6] = -a + leb.w[start + 6] = 4.0 * pi * v + + leb.x[start + 7] = -a + leb.y[start + 7] = -a + leb.z[start + 7] = -a + leb.w[start + 7] = 4.0 * pi * v + start = start + 8 + + case 4: + # /* In this case A is inputed */ + b = sqrt(1.0 - 2.0 * a * a) + leb.x[start] = a + leb.y[start] = a + leb.z[start] = b + leb.w[start] = 4.0 * pi * v + + leb.x[start + 1] = -a + leb.y[start + 1] = a + leb.z[start + 1] = b + leb.w[start + 1] = 4.0 * pi * v + + leb.x[start + 2] = a + leb.y[start + 2] = -a + leb.z[start + 2] = b + leb.w[start + 2] = 4.0 * pi * v + + leb.x[start + 3] = a + leb.y[start + 3] = a + leb.z[start + 3] = -b + leb.w[start + 3] = 4.0 * pi * v + + leb.x[start + 4] = -a + leb.y[start + 4] = -a + leb.z[start + 4] = b + leb.w[start + 4] = 4.0 * pi * v + + leb.x[start + 5] = -a + leb.y[start + 5] = a + leb.z[start + 5] = -b + leb.w[start + 5] = 4.0 * pi * v + + leb.x[start + 6] = a + leb.y[start + 6] = -a + leb.z[start + 6] = -b + leb.w[start + 6] = 4.0 * pi * v + + leb.x[start + 7] = -a + leb.y[start + 7] = -a + leb.z[start + 7] = -b + leb.w[start + 7] = 4.0 * pi * v + + leb.x[start + 8] = -a + leb.y[start + 8] = b + leb.z[start + 8] = a + leb.w[start + 8] = 4.0 * pi * v + + leb.x[start + 9] = a + leb.y[start + 9] = -b + leb.z[start + 9] = a + leb.w[start + 9] = 4.0 * pi * v + + leb.x[start + 10] = a + leb.y[start + 10] = b + leb.z[start + 10] = -a + leb.w[start + 10] = 4.0 * pi * v + + leb.x[start + 11] = -a + leb.y[start + 11] = -b + leb.z[start + 11] = a + leb.w[start + 11] = 4.0 * pi * v + + leb.x[start + 12] = -a + leb.y[start + 12] = b + leb.z[start + 12] = -a + leb.w[start + 12] = 4.0 * pi * v + + leb.x[start + 13] = a + leb.y[start + 13] = -b + leb.z[start + 13] = -a + leb.w[start + 13] = 4.0 * pi * v + + leb.x[start + 14] = -a + leb.y[start + 14] = -b + leb.z[start + 14] = -a + leb.w[start + 14] = 4.0 * pi * v + + leb.x[start + 15] = a + leb.y[start + 15] = b + leb.z[start + 15] = a + leb.w[start + 15] = 4.0 * pi * v + + leb.x[start + 16] = b + leb.y[start + 16] = a + leb.z[start + 16] = a + leb.w[start + 16] = 4.0 * pi * v + + leb.x[start + 17] = -b + leb.y[start + 17] = a + leb.z[start + 17] = a + leb.w[start + 17] = 4.0 * pi * v + + leb.x[start + 18] = b + leb.y[start + 18] = -a + leb.z[start + 18] = a + leb.w[start + 18] = 4.0 * pi * v + + leb.x[start + 19] = b + leb.y[start + 19] = a + leb.z[start + 19] = -a + leb.w[start + 19] = 4.0 * pi * v + + leb.x[start + 20] = -b + leb.y[start + 20] = -a + leb.z[start + 20] = a + leb.w[start + 20] = 4.0 * pi * v + + leb.x[start + 21] = -b + leb.y[start + 21] = a + leb.z[start + 21] = -a + leb.w[start + 21] = 4.0 * pi * v + + leb.x[start + 22] = b + leb.y[start + 22] = -a + leb.z[start + 22] = -a + leb.w[start + 22] = 4.0 * pi * v + + leb.x[start + 23] = -b + leb.y[start + 23] = -a + leb.z[start + 23] = -a + leb.w[start + 23] = 4.0 * pi * v + start = start + 24 + + case 5: + # /* A is inputed in this case as well*/ + b = sqrt(1 - a * a) + leb.x[start] = a + leb.y[start] = b + leb.z[start] = 0.0 + leb.w[start] = 4.0 * pi * v + + leb.x[start + 1] = -a + leb.y[start + 1] = b + leb.z[start + 1] = 0.0 + leb.w[start + 1] = 4.0 * pi * v + + leb.x[start + 2] = a + leb.y[start + 2] = -b + leb.z[start + 2] = 0.0 + leb.w[start + 2] = 4.0 * pi * v + + leb.x[start + 3] = -a + leb.y[start + 3] = -b + leb.z[start + 3] = 0.0 + leb.w[start + 3] = 4.0 * pi * v + + leb.x[start + 4] = b + leb.y[start + 4] = a + leb.z[start + 4] = 0.0 + leb.w[start + 4] = 4.0 * pi * v + + leb.x[start + 5] = -b + leb.y[start + 5] = a + leb.z[start + 5] = 0.0 + leb.w[start + 5] = 4.0 * pi * v + + leb.x[start + 6] = b + leb.y[start + 6] = -a + leb.z[start + 6] = 0.0 + leb.w[start + 6] = 4.0 * pi * v + + leb.x[start + 7] = -b + leb.y[start + 7] = -a + leb.z[start + 7] = 0.0 + leb.w[start + 7] = 4.0 * pi * v + + leb.x[start + 8] = a + leb.y[start + 8] = 0.0 + leb.z[start + 8] = b + leb.w[start + 8] = 4.0 * pi * v + + leb.x[start + 9] = -a + leb.y[start + 9] = 0.0 + leb.z[start + 9] = b + leb.w[start + 9] = 4.0 * pi * v + + leb.x[start + 10] = a + leb.y[start + 10] = 0.0 + leb.z[start + 10] = -b + leb.w[start + 10] = 4.0 * pi * v + + leb.x[start + 11] = -a + leb.y[start + 11] = 0.0 + leb.z[start + 11] = -b + leb.w[start + 11] = 4.0 * pi * v + + leb.x[start + 12] = b + leb.y[start + 12] = 0.0 + leb.z[start + 12] = a + leb.w[start + 12] = 4.0 * pi * v + + leb.x[start + 13] = -b + leb.y[start + 13] = 0.0 + leb.z[start + 13] = a + leb.w[start + 13] = 4.0 * pi * v + + leb.x[start + 14] = b + leb.y[start + 14] = 0.0 + leb.z[start + 14] = -a + leb.w[start + 14] = 4.0 * pi * v + + leb.x[start + 15] = -b + leb.y[start + 15] = 0.0 + leb.z[start + 15] = -a + leb.w[start + 15] = 4.0 * pi * v + + leb.x[start + 16] = 0.0 + leb.y[start + 16] = a + leb.z[start + 16] = b + leb.w[start + 16] = 4.0 * pi * v + + leb.x[start + 17] = 0.0 + leb.y[start + 17] = -a + leb.z[start + 17] = b + leb.w[start + 17] = 4.0 * pi * v + + leb.x[start + 18] = 0.0 + leb.y[start + 18] = a + leb.z[start + 18] = -b + leb.w[start + 18] = 4.0 * pi * v + + leb.x[start + 19] = 0.0 + leb.y[start + 19] = -a + leb.z[start + 19] = -b + leb.w[start + 19] = 4.0 * pi * v + + leb.x[start + 20] = 0.0 + leb.y[start + 20] = b + leb.z[start + 20] = a + leb.w[start + 20] = 4.0 * pi * v + + leb.x[start + 21] = 0.0 + leb.y[start + 21] = -b + leb.z[start + 21] = a + leb.w[start + 21] = 4.0 * pi * v + + leb.x[start + 22] = 0.0 + leb.y[start + 22] = b + leb.z[start + 22] = -a + leb.w[start + 22] = 4.0 * pi * v + + leb.x[start + 23] = 0.0 + leb.y[start + 23] = -b + leb.z[start + 23] = -a + leb.w[start + 23] = 4.0 * pi * v + start = start + 24 + + case 6: + # /* both A and B are inputed in this case */ + c = sqrt(1.0 - a * a - b * b) + leb.x[start] = a + leb.y[start] = b + leb.z[start] = c + leb.w[start] = 4.0 * pi * v + + leb.x[start + 1] = -a + leb.y[start + 1] = b + leb.z[start + 1] = c + leb.w[start + 1] = 4.0 * pi * v + + leb.x[start + 2] = a + leb.y[start + 2] = -b + leb.z[start + 2] = c + leb.w[start + 2] = 4.0 * pi * v + + leb.x[start + 3] = a + leb.y[start + 3] = b + leb.z[start + 3] = -c + leb.w[start + 3] = 4.0 * pi * v + + leb.x[start + 4] = -a + leb.y[start + 4] = -b + leb.z[start + 4] = c + leb.w[start + 4] = 4.0 * pi * v + + leb.x[start + 5] = a + leb.y[start + 5] = -b + leb.z[start + 5] = -c + leb.w[start + 5] = 4.0 * pi * v + + leb.x[start + 6] = -a + leb.y[start + 6] = b + leb.z[start + 6] = -c + leb.w[start + 6] = 4.0 * pi * v + + leb.x[start + 7] = -a + leb.y[start + 7] = -b + leb.z[start + 7] = -c + leb.w[start + 7] = 4.0 * pi * v + + leb.x[start + 8] = b + leb.y[start + 8] = a + leb.z[start + 8] = c + leb.w[start + 8] = 4.0 * pi * v + + leb.x[start + 9] = -b + leb.y[start + 9] = a + leb.z[start + 9] = c + leb.w[start + 9] = 4.0 * pi * v + + leb.x[start + 10] = b + leb.y[start + 10] = -a + leb.z[start + 10] = c + leb.w[start + 10] = 4.0 * pi * v + + leb.x[start + 11] = b + leb.y[start + 11] = a + leb.z[start + 11] = -c + leb.w[start + 11] = 4.0 * pi * v + + leb.x[start + 12] = -b + leb.y[start + 12] = -a + leb.z[start + 12] = c + leb.w[start + 12] = 4.0 * pi * v + + leb.x[start + 13] = b + leb.y[start + 13] = -a + leb.z[start + 13] = -c + leb.w[start + 13] = 4.0 * pi * v + + leb.x[start + 14] = -b + leb.y[start + 14] = a + leb.z[start + 14] = -c + leb.w[start + 14] = 4.0 * pi * v + + leb.x[start + 15] = -b + leb.y[start + 15] = -a + leb.z[start + 15] = -c + leb.w[start + 15] = 4.0 * pi * v + + leb.x[start + 16] = c + leb.y[start + 16] = a + leb.z[start + 16] = b + leb.w[start + 16] = 4.0 * pi * v + + leb.x[start + 17] = -c + leb.y[start + 17] = a + leb.z[start + 17] = b + leb.w[start + 17] = 4.0 * pi * v + + leb.x[start + 18] = c + leb.y[start + 18] = -a + leb.z[start + 18] = b + leb.w[start + 18] = 4.0 * pi * v + + leb.x[start + 19] = c + leb.y[start + 19] = a + leb.z[start + 19] = -b + leb.w[start + 19] = 4.0 * pi * v + + leb.x[start + 20] = -c + leb.y[start + 20] = -a + leb.z[start + 20] = b + leb.w[start + 20] = 4.0 * pi * v + + leb.x[start + 21] = c + leb.y[start + 21] = -a + leb.z[start + 21] = -b + leb.w[start + 21] = 4.0 * pi * v + + leb.x[start + 22] = -c + leb.y[start + 22] = a + leb.z[start + 22] = -b + leb.w[start + 22] = 4.0 * pi * v + + leb.x[start + 23] = -c + leb.y[start + 23] = -a + leb.z[start + 23] = -b + leb.w[start + 23] = 4.0 * pi * v + + leb.x[start + 24] = c + leb.y[start + 24] = b + leb.z[start + 24] = a + leb.w[start + 24] = 4.0 * pi * v + + leb.x[start + 25] = -c + leb.y[start + 25] = b + leb.z[start + 25] = a + leb.w[start + 25] = 4.0 * pi * v + + leb.x[start + 26] = c + leb.y[start + 26] = -b + leb.z[start + 26] = a + leb.w[start + 26] = 4.0 * pi * v + + leb.x[start + 27] = c + leb.y[start + 27] = b + leb.z[start + 27] = -a + leb.w[start + 27] = 4.0 * pi * v + + leb.x[start + 28] = -c + leb.y[start + 28] = -b + leb.z[start + 28] = a + leb.w[start + 28] = 4.0 * pi * v + + leb.x[start + 29] = c + leb.y[start + 29] = -b + leb.z[start + 29] = -a + leb.w[start + 29] = 4.0 * pi * v + + leb.x[start + 30] = -c + leb.y[start + 30] = b + leb.z[start + 30] = -a + leb.w[start + 30] = 4.0 * pi * v + + leb.x[start + 31] = -c + leb.y[start + 31] = -b + leb.z[start + 31] = -a + leb.w[start + 31] = 4.0 * pi * v + + leb.x[start + 32] = a + leb.y[start + 32] = c + leb.z[start + 32] = b + leb.w[start + 32] = 4.0 * pi * v + + leb.x[start + 33] = -a + leb.y[start + 33] = c + leb.z[start + 33] = b + leb.w[start + 33] = 4.0 * pi * v + + leb.x[start + 34] = a + leb.y[start + 34] = -c + leb.z[start + 34] = b + leb.w[start + 34] = 4.0 * pi * v + + leb.x[start + 35] = a + leb.y[start + 35] = c + leb.z[start + 35] = -b + leb.w[start + 35] = 4.0 * pi * v + + leb.x[start + 36] = -a + leb.y[start + 36] = -c + leb.z[start + 36] = b + leb.w[start + 36] = 4.0 * pi * v + + leb.x[start + 37] = a + leb.y[start + 37] = -c + leb.z[start + 37] = -b + leb.w[start + 37] = 4.0 * pi * v + + leb.x[start + 38] = -a + leb.y[start + 38] = c + leb.z[start + 38] = -b + leb.w[start + 38] = 4.0 * pi * v + + leb.x[start + 39] = -a + leb.y[start + 39] = -c + leb.z[start + 39] = -b + leb.w[start + 39] = 4.0 * pi * v + + leb.x[start + 40] = b + leb.y[start + 40] = c + leb.z[start + 40] = a + leb.w[start + 40] = 4.0 * pi * v + + leb.x[start + 41] = -b + leb.y[start + 41] = c + leb.z[start + 41] = a + leb.w[start + 41] = 4.0 * pi * v + + leb.x[start + 42] = b + leb.y[start + 42] = -c + leb.z[start + 42] = a + leb.w[start + 42] = 4.0 * pi * v + + leb.x[start + 43] = b + leb.y[start + 43] = c + leb.z[start + 43] = -a + leb.w[start + 43] = 4.0 * pi * v + + leb.x[start + 44] = -b + leb.y[start + 44] = -c + leb.z[start + 44] = a + leb.w[start + 44] = 4.0 * pi * v + + leb.x[start + 45] = b + leb.y[start + 45] = -c + leb.z[start + 45] = -a + leb.w[start + 45] = 4.0 * pi * v + + leb.x[start + 46] = -b + leb.y[start + 46] = c + leb.z[start + 46] = -a + leb.w[start + 46] = 4.0 * pi * v + + leb.x[start + 47] = -b + leb.y[start + 47] = -c + leb.z[start + 47] = -a + leb.w[start + 47] = 4.0 * pi * v + start = start + 48 + + case _: + raise Exception('Bad grid order') + + return leb, start + + +def lebedev_rule(n): + r"""Lebedev quadrature. + + Compute the sample points and weights for Lebedev quadrature [1]_ + for integration of a function over the surface of a unit sphere. + + Parameters + ---------- + n : int + Quadrature order. See Notes for supported values. + + Returns + ------- + x : ndarray of shape ``(3, m)`` + Sample points on the unit sphere in Cartesian coordinates. + ``m`` is the "degree" corresponding with the specified order; see Notes. + w : ndarray of shape ``(m,)`` + Weights + + Notes + ----- + Implemented by translating the Matlab code of [2]_ to Python. + + The available orders (argument `n`) are:: + + 3, 5, 7, 9, 11, 13, 15, 17, + 19, 21, 23, 25, 27, 29, 31, 35, + 41, 47, 53, 59, 65, 71, 77, 83, + 89, 95, 101, 107, 113, 119, 125, 131 + + The corresponding degrees ``m`` are:: + + 6, 14, 26, 38, 50, 74, 86, 110, + 146, 170, 194, 230, 266, 302, 350, 434, + 590, 770, 974, 1202, 1454, 1730, 2030, 2354, + 2702, 3074, 3470, 3890, 4334, 4802, 5294, 5810 + + References + ---------- + .. [1] V.I. Lebedev, and D.N. Laikov. "A quadrature formula for the sphere of + the 131st algebraic order of accuracy". Doklady Mathematics, Vol. 59, + No. 3, 1999, pp. 477-481. + .. [2] R. Parrish. ``getLebedevSphere``. Matlab Central File Exchange. + https://www.mathworks.com/matlabcentral/fileexchange/27097-getlebedevsphere. + .. [3] Bellet, Jean-Baptiste, Matthieu Brachet, and Jean-Pierre Croisille. + "Quadrature and symmetry on the Cubed Sphere." Journal of Computational and + Applied Mathematics 409 (2022): 114142. :doi:`10.1016/j.cam.2022.114142`. + + Examples + -------- + An example given in [3]_ is integration of :math:`f(x, y, z) = \exp(x)` over a + sphere of radius :math:`1`; the reference there is ``14.7680137457653``. + Show the convergence to the expected result as the order increases: + + >>> import matplotlib.pyplot as plt + >>> import numpy as np + >>> from scipy.integrate import lebedev_rule + >>> + >>> def f(x): + ... return np.exp(x[0]) + >>> + >>> res = [] + >>> orders = np.arange(3, 20, 2) + >>> for n in orders: + ... x, w = lebedev_rule(n) + ... res.append(w @ f(x)) + >>> + >>> ref = np.full_like(res, 14.7680137457653) + >>> err = abs(res - ref)/abs(ref) + >>> plt.semilogy(orders, err) + >>> plt.xlabel('order $n$') + >>> plt.ylabel('relative error') + >>> plt.title(r'Convergence for $f(x, y, z) = \exp(x)$') + >>> plt.show() + + """ + degree = [6, 14, 26, 38, 50, 74, 86, 110, 146, 170, 194, 230, 266, 302, 350, + 434, 590, 770, 974, 1202, 1454, 1730, 2030, 2354, 2702, 3074, 3470, + 3890, 4334, 4802, 5294, 5810] + order = [3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 35, 41, 47, + 53, 59, 65, 71, 77, 83, 89, 95, 101, 107, 113, 119, 125, 131] + order_degree_map = dict(zip(order, degree)) + + if n not in order_degree_map: + message = f"Order {n=} not available. Available orders are {order}." + raise NotImplementedError(message) + + degree = order_degree_map[n] + res = get_lebedev_sphere(degree) + x = np.stack((res.x, res.y, res.z)) + w = res.w + + return x, w diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ode.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ode.py new file mode 100644 index 0000000000000000000000000000000000000000..72d9da2495da768753f45796e8df1996cd70d382 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_ode.py @@ -0,0 +1,1388 @@ +# Authors: Pearu Peterson, Pauli Virtanen, John Travers +""" +First-order ODE integrators. + +User-friendly interface to various numerical integrators for solving a +system of first order ODEs with prescribed initial conditions:: + + d y(t)[i] + --------- = f(t,y(t))[i], + d t + + y(t=0)[i] = y0[i], + +where:: + + i = 0, ..., len(y0) - 1 + +class ode +--------- + +A generic interface class to numeric integrators. It has the following +methods:: + + integrator = ode(f, jac=None) + integrator = integrator.set_integrator(name, **params) + integrator = integrator.set_initial_value(y0, t0=0.0) + integrator = integrator.set_f_params(*args) + integrator = integrator.set_jac_params(*args) + y1 = integrator.integrate(t1, step=False, relax=False) + flag = integrator.successful() + +class complex_ode +----------------- + +This class has the same generic interface as ode, except it can handle complex +f, y and Jacobians by transparently translating them into the equivalent +real-valued system. It supports the real-valued solvers (i.e., not zvode) and is +an alternative to ode with the zvode solver, sometimes performing better. +""" +# XXX: Integrators must have: +# =========================== +# cvode - C version of vode and vodpk with many improvements. +# Get it from http://www.netlib.org/ode/cvode.tar.gz. +# To wrap cvode to Python, one must write the extension module by +# hand. Its interface is too much 'advanced C' that using f2py +# would be too complicated (or impossible). +# +# How to define a new integrator: +# =============================== +# +# class myodeint(IntegratorBase): +# +# runner = or None +# +# def __init__(self,...): # required +# +# +# def reset(self,n,has_jac): # optional +# # n - the size of the problem (number of equations) +# # has_jac - whether user has supplied its own routine for Jacobian +# +# +# def run(self,f,jac,y0,t0,t1,f_params,jac_params): # required +# # this method is called to integrate from t=t0 to t=t1 +# # with initial condition y0. f and jac are user-supplied functions +# # that define the problem. f_params,jac_params are additional +# # arguments +# # to these functions. +# +# if : +# self.success = 0 +# return t1,y1 +# +# # In addition, one can define step() and run_relax() methods (they +# # take the same arguments as run()) if the integrator can support +# # these features (see IntegratorBase doc strings). +# +# if myodeint.runner: +# IntegratorBase.integrator_classes.append(myodeint) + +__all__ = ['ode', 'complex_ode'] + +import re +import threading +import warnings + +from numpy import asarray, array, zeros, isscalar, real, imag, vstack + +from . import _vode +from . import _dop +from . import _lsoda + + +_dop_int_dtype = _dop.types.intvar.dtype +_vode_int_dtype = _vode.types.intvar.dtype +_lsoda_int_dtype = _lsoda.types.intvar.dtype + + +# lsoda, vode and zvode are not thread-safe. VODE_LOCK protects both vode and +# zvode; they share the `def run` implementation +LSODA_LOCK = threading.Lock() +VODE_LOCK = threading.Lock() + + +# ------------------------------------------------------------------------------ +# User interface +# ------------------------------------------------------------------------------ + + +class ode: + """ + A generic interface class to numeric integrators. + + Solve an equation system :math:`y'(t) = f(t,y)` with (optional) ``jac = df/dy``. + + *Note*: The first two arguments of ``f(t, y, ...)`` are in the + opposite order of the arguments in the system definition function used + by `scipy.integrate.odeint`. + + Parameters + ---------- + f : callable ``f(t, y, *f_args)`` + Right-hand side of the differential equation. t is a scalar, + ``y.shape == (n,)``. + ``f_args`` is set by calling ``set_f_params(*args)``. + `f` should return a scalar, array or list (not a tuple). + jac : callable ``jac(t, y, *jac_args)``, optional + Jacobian of the right-hand side, ``jac[i,j] = d f[i] / d y[j]``. + ``jac_args`` is set by calling ``set_jac_params(*args)``. + + Attributes + ---------- + t : float + Current time. + y : ndarray + Current variable values. + + See also + -------- + odeint : an integrator with a simpler interface based on lsoda from ODEPACK + quad : for finding the area under a curve + + Notes + ----- + Available integrators are listed below. They can be selected using + the `set_integrator` method. + + "vode" + + Real-valued Variable-coefficient Ordinary Differential Equation + solver, with fixed-leading-coefficient implementation. It provides + implicit Adams method (for non-stiff problems) and a method based on + backward differentiation formulas (BDF) (for stiff problems). + + Source: http://www.netlib.org/ode/vode.f + + .. warning:: + + This integrator is not re-entrant. You cannot have two `ode` + instances using the "vode" integrator at the same time. + + This integrator accepts the following parameters in `set_integrator` + method of the `ode` class: + + - atol : float or sequence + absolute tolerance for solution + - rtol : float or sequence + relative tolerance for solution + - lband : None or int + - uband : None or int + Jacobian band width, jac[i,j] != 0 for i-lband <= j <= i+uband. + Setting these requires your jac routine to return the jacobian + in packed format, jac_packed[i-j+uband, j] = jac[i,j]. The + dimension of the matrix must be (lband+uband+1, len(y)). + - method: 'adams' or 'bdf' + Which solver to use, Adams (non-stiff) or BDF (stiff) + - with_jacobian : bool + This option is only considered when the user has not supplied a + Jacobian function and has not indicated (by setting either band) + that the Jacobian is banded. In this case, `with_jacobian` specifies + whether the iteration method of the ODE solver's correction step is + chord iteration with an internally generated full Jacobian or + functional iteration with no Jacobian. + - nsteps : int + Maximum number of (internally defined) steps allowed during one + call to the solver. + - first_step : float + - min_step : float + - max_step : float + Limits for the step sizes used by the integrator. + - order : int + Maximum order used by the integrator, + order <= 12 for Adams, <= 5 for BDF. + + "zvode" + + Complex-valued Variable-coefficient Ordinary Differential Equation + solver, with fixed-leading-coefficient implementation. It provides + implicit Adams method (for non-stiff problems) and a method based on + backward differentiation formulas (BDF) (for stiff problems). + + Source: http://www.netlib.org/ode/zvode.f + + .. warning:: + + This integrator is not re-entrant. You cannot have two `ode` + instances using the "zvode" integrator at the same time. + + This integrator accepts the same parameters in `set_integrator` + as the "vode" solver. + + .. note:: + + When using ZVODE for a stiff system, it should only be used for + the case in which the function f is analytic, that is, when each f(i) + is an analytic function of each y(j). Analyticity means that the + partial derivative df(i)/dy(j) is a unique complex number, and this + fact is critical in the way ZVODE solves the dense or banded linear + systems that arise in the stiff case. For a complex stiff ODE system + in which f is not analytic, ZVODE is likely to have convergence + failures, and for this problem one should instead use DVODE on the + equivalent real system (in the real and imaginary parts of y). + + "lsoda" + + Real-valued Variable-coefficient Ordinary Differential Equation + solver, with fixed-leading-coefficient implementation. It provides + automatic method switching between implicit Adams method (for non-stiff + problems) and a method based on backward differentiation formulas (BDF) + (for stiff problems). + + Source: http://www.netlib.org/odepack + + .. warning:: + + This integrator is not re-entrant. You cannot have two `ode` + instances using the "lsoda" integrator at the same time. + + This integrator accepts the following parameters in `set_integrator` + method of the `ode` class: + + - atol : float or sequence + absolute tolerance for solution + - rtol : float or sequence + relative tolerance for solution + - lband : None or int + - uband : None or int + Jacobian band width, jac[i,j] != 0 for i-lband <= j <= i+uband. + Setting these requires your jac routine to return the jacobian + in packed format, jac_packed[i-j+uband, j] = jac[i,j]. + - with_jacobian : bool + *Not used.* + - nsteps : int + Maximum number of (internally defined) steps allowed during one + call to the solver. + - first_step : float + - min_step : float + - max_step : float + Limits for the step sizes used by the integrator. + - max_order_ns : int + Maximum order used in the nonstiff case (default 12). + - max_order_s : int + Maximum order used in the stiff case (default 5). + - max_hnil : int + Maximum number of messages reporting too small step size (t + h = t) + (default 0) + - ixpr : int + Whether to generate extra printing at method switches (default False). + + "dopri5" + + This is an explicit runge-kutta method of order (4)5 due to Dormand & + Prince (with stepsize control and dense output). + + Authors: + + E. Hairer and G. Wanner + Universite de Geneve, Dept. de Mathematiques + CH-1211 Geneve 24, Switzerland + e-mail: ernst.hairer@math.unige.ch, gerhard.wanner@math.unige.ch + + This code is described in [HNW93]_. + + This integrator accepts the following parameters in set_integrator() + method of the ode class: + + - atol : float or sequence + absolute tolerance for solution + - rtol : float or sequence + relative tolerance for solution + - nsteps : int + Maximum number of (internally defined) steps allowed during one + call to the solver. + - first_step : float + - max_step : float + - safety : float + Safety factor on new step selection (default 0.9) + - ifactor : float + - dfactor : float + Maximum factor to increase/decrease step size by in one step + - beta : float + Beta parameter for stabilised step size control. + - verbosity : int + Switch for printing messages (< 0 for no messages). + + "dop853" + + This is an explicit runge-kutta method of order 8(5,3) due to Dormand + & Prince (with stepsize control and dense output). + + Options and references the same as "dopri5". + + Examples + -------- + + A problem to integrate and the corresponding jacobian: + + >>> from scipy.integrate import ode + >>> + >>> y0, t0 = [1.0j, 2.0], 0 + >>> + >>> def f(t, y, arg1): + ... return [1j*arg1*y[0] + y[1], -arg1*y[1]**2] + >>> def jac(t, y, arg1): + ... return [[1j*arg1, 1], [0, -arg1*2*y[1]]] + + The integration: + + >>> r = ode(f, jac).set_integrator('zvode', method='bdf') + >>> r.set_initial_value(y0, t0).set_f_params(2.0).set_jac_params(2.0) + >>> t1 = 10 + >>> dt = 1 + >>> while r.successful() and r.t < t1: + ... print(r.t+dt, r.integrate(r.t+dt)) + 1 [-0.71038232+0.23749653j 0.40000271+0.j ] + 2.0 [0.19098503-0.52359246j 0.22222356+0.j ] + 3.0 [0.47153208+0.52701229j 0.15384681+0.j ] + 4.0 [-0.61905937+0.30726255j 0.11764744+0.j ] + 5.0 [0.02340997-0.61418799j 0.09523835+0.j ] + 6.0 [0.58643071+0.339819j 0.08000018+0.j ] + 7.0 [-0.52070105+0.44525141j 0.06896565+0.j ] + 8.0 [-0.15986733-0.61234476j 0.06060616+0.j ] + 9.0 [0.64850462+0.15048982j 0.05405414+0.j ] + 10.0 [-0.38404699+0.56382299j 0.04878055+0.j ] + + References + ---------- + .. [HNW93] E. Hairer, S.P. Norsett and G. Wanner, Solving Ordinary + Differential Equations i. Nonstiff Problems. 2nd edition. + Springer Series in Computational Mathematics, + Springer-Verlag (1993) + + """ + + def __init__(self, f, jac=None): + self.stiff = 0 + self.f = f + self.jac = jac + self.f_params = () + self.jac_params = () + self._y = [] + + @property + def y(self): + return self._y + + def set_initial_value(self, y, t=0.0): + """Set initial conditions y(t) = y.""" + if isscalar(y): + y = [y] + n_prev = len(self._y) + if not n_prev: + self.set_integrator('') # find first available integrator + self._y = asarray(y, self._integrator.scalar) + self.t = t + self._integrator.reset(len(self._y), self.jac is not None) + return self + + def set_integrator(self, name, **integrator_params): + """ + Set integrator by name. + + Parameters + ---------- + name : str + Name of the integrator. + **integrator_params + Additional parameters for the integrator. + """ + integrator = find_integrator(name) + if integrator is None: + # FIXME: this really should be raise an exception. Will that break + # any code? + message = f'No integrator name match with {name!r} or is not available.' + warnings.warn(message, stacklevel=2) + else: + self._integrator = integrator(**integrator_params) + if not len(self._y): + self.t = 0.0 + self._y = array([0.0], self._integrator.scalar) + self._integrator.reset(len(self._y), self.jac is not None) + return self + + def integrate(self, t, step=False, relax=False): + """Find y=y(t), set y as an initial condition, and return y. + + Parameters + ---------- + t : float + The endpoint of the integration step. + step : bool + If True, and if the integrator supports the step method, + then perform a single integration step and return. + This parameter is provided in order to expose internals of + the implementation, and should not be changed from its default + value in most cases. + relax : bool + If True and if the integrator supports the run_relax method, + then integrate until t_1 >= t and return. ``relax`` is not + referenced if ``step=True``. + This parameter is provided in order to expose internals of + the implementation, and should not be changed from its default + value in most cases. + + Returns + ------- + y : float + The integrated value at t + """ + if step and self._integrator.supports_step: + mth = self._integrator.step + elif relax and self._integrator.supports_run_relax: + mth = self._integrator.run_relax + else: + mth = self._integrator.run + + try: + self._y, self.t = mth(self.f, self.jac or (lambda: None), + self._y, self.t, t, + self.f_params, self.jac_params) + except SystemError as e: + # f2py issue with tuple returns, see ticket 1187. + raise ValueError( + 'Function to integrate must not return a tuple.' + ) from e + + return self._y + + def successful(self): + """Check if integration was successful.""" + try: + self._integrator + except AttributeError: + self.set_integrator('') + return self._integrator.success == 1 + + def get_return_code(self): + """Extracts the return code for the integration to enable better control + if the integration fails. + + In general, a return code > 0 implies success, while a return code < 0 + implies failure. + + Notes + ----- + This section describes possible return codes and their meaning, for available + integrators that can be selected by `set_integrator` method. + + "vode" + + =========== ======= + Return Code Message + =========== ======= + 2 Integration successful. + -1 Excess work done on this call. (Perhaps wrong MF.) + -2 Excess accuracy requested. (Tolerances too small.) + -3 Illegal input detected. (See printed message.) + -4 Repeated error test failures. (Check all input.) + -5 Repeated convergence failures. (Perhaps bad Jacobian + supplied or wrong choice of MF or tolerances.) + -6 Error weight became zero during problem. (Solution + component i vanished, and ATOL or ATOL(i) = 0.) + =========== ======= + + "zvode" + + =========== ======= + Return Code Message + =========== ======= + 2 Integration successful. + -1 Excess work done on this call. (Perhaps wrong MF.) + -2 Excess accuracy requested. (Tolerances too small.) + -3 Illegal input detected. (See printed message.) + -4 Repeated error test failures. (Check all input.) + -5 Repeated convergence failures. (Perhaps bad Jacobian + supplied or wrong choice of MF or tolerances.) + -6 Error weight became zero during problem. (Solution + component i vanished, and ATOL or ATOL(i) = 0.) + =========== ======= + + "dopri5" + + =========== ======= + Return Code Message + =========== ======= + 1 Integration successful. + 2 Integration successful (interrupted by solout). + -1 Input is not consistent. + -2 Larger nsteps is needed. + -3 Step size becomes too small. + -4 Problem is probably stiff (interrupted). + =========== ======= + + "dop853" + + =========== ======= + Return Code Message + =========== ======= + 1 Integration successful. + 2 Integration successful (interrupted by solout). + -1 Input is not consistent. + -2 Larger nsteps is needed. + -3 Step size becomes too small. + -4 Problem is probably stiff (interrupted). + =========== ======= + + "lsoda" + + =========== ======= + Return Code Message + =========== ======= + 2 Integration successful. + -1 Excess work done on this call (perhaps wrong Dfun type). + -2 Excess accuracy requested (tolerances too small). + -3 Illegal input detected (internal error). + -4 Repeated error test failures (internal error). + -5 Repeated convergence failures (perhaps bad Jacobian or tolerances). + -6 Error weight became zero during problem. + -7 Internal workspace insufficient to finish (internal error). + =========== ======= + """ + try: + self._integrator + except AttributeError: + self.set_integrator('') + return self._integrator.istate + + def set_f_params(self, *args): + """Set extra parameters for user-supplied function f.""" + self.f_params = args + return self + + def set_jac_params(self, *args): + """Set extra parameters for user-supplied function jac.""" + self.jac_params = args + return self + + def set_solout(self, solout): + """ + Set callable to be called at every successful integration step. + + Parameters + ---------- + solout : callable + ``solout(t, y)`` is called at each internal integrator step, + t is a scalar providing the current independent position + y is the current solution ``y.shape == (n,)`` + solout should return -1 to stop integration + otherwise it should return None or 0 + + """ + if self._integrator.supports_solout: + self._integrator.set_solout(solout) + if self._y is not None: + self._integrator.reset(len(self._y), self.jac is not None) + else: + raise ValueError("selected integrator does not support solout," + " choose another one") + + +def _transform_banded_jac(bjac): + """ + Convert a real matrix of the form (for example) + + [0 0 A B] [0 0 0 B] + [0 0 C D] [0 0 A D] + [E F G H] to [0 F C H] + [I J K L] [E J G L] + [I 0 K 0] + + That is, every other column is shifted up one. + """ + # Shift every other column. + newjac = zeros((bjac.shape[0] + 1, bjac.shape[1])) + newjac[1:, ::2] = bjac[:, ::2] + newjac[:-1, 1::2] = bjac[:, 1::2] + return newjac + + +class complex_ode(ode): + """ + A wrapper of ode for complex systems. + + This functions similarly as `ode`, but re-maps a complex-valued + equation system to a real-valued one before using the integrators. + + Parameters + ---------- + f : callable ``f(t, y, *f_args)`` + Rhs of the equation. t is a scalar, ``y.shape == (n,)``. + ``f_args`` is set by calling ``set_f_params(*args)``. + jac : callable ``jac(t, y, *jac_args)`` + Jacobian of the rhs, ``jac[i,j] = d f[i] / d y[j]``. + ``jac_args`` is set by calling ``set_f_params(*args)``. + + Attributes + ---------- + t : float + Current time. + y : ndarray + Current variable values. + + Examples + -------- + For usage examples, see `ode`. + + """ + + def __init__(self, f, jac=None): + self.cf = f + self.cjac = jac + if jac is None: + ode.__init__(self, self._wrap, None) + else: + ode.__init__(self, self._wrap, self._wrap_jac) + + def _wrap(self, t, y, *f_args): + f = self.cf(*((t, y[::2] + 1j * y[1::2]) + f_args)) + # self.tmp is a real-valued array containing the interleaved + # real and imaginary parts of f. + self.tmp[::2] = real(f) + self.tmp[1::2] = imag(f) + return self.tmp + + def _wrap_jac(self, t, y, *jac_args): + # jac is the complex Jacobian computed by the user-defined function. + jac = self.cjac(*((t, y[::2] + 1j * y[1::2]) + jac_args)) + + # jac_tmp is the real version of the complex Jacobian. Each complex + # entry in jac, say 2+3j, becomes a 2x2 block of the form + # [2 -3] + # [3 2] + jac_tmp = zeros((2 * jac.shape[0], 2 * jac.shape[1])) + jac_tmp[1::2, 1::2] = jac_tmp[::2, ::2] = real(jac) + jac_tmp[1::2, ::2] = imag(jac) + jac_tmp[::2, 1::2] = -jac_tmp[1::2, ::2] + + ml = getattr(self._integrator, 'ml', None) + mu = getattr(self._integrator, 'mu', None) + if ml is not None or mu is not None: + # Jacobian is banded. The user's Jacobian function has computed + # the complex Jacobian in packed format. The corresponding + # real-valued version has every other column shifted up. + jac_tmp = _transform_banded_jac(jac_tmp) + + return jac_tmp + + @property + def y(self): + return self._y[::2] + 1j * self._y[1::2] + + def set_integrator(self, name, **integrator_params): + """ + Set integrator by name. + + Parameters + ---------- + name : str + Name of the integrator + **integrator_params + Additional parameters for the integrator. + """ + if name == 'zvode': + raise ValueError("zvode must be used with ode, not complex_ode") + + lband = integrator_params.get('lband') + uband = integrator_params.get('uband') + if lband is not None or uband is not None: + # The Jacobian is banded. Override the user-supplied bandwidths + # (which are for the complex Jacobian) with the bandwidths of + # the corresponding real-valued Jacobian wrapper of the complex + # Jacobian. + integrator_params['lband'] = 2 * (lband or 0) + 1 + integrator_params['uband'] = 2 * (uband or 0) + 1 + + return ode.set_integrator(self, name, **integrator_params) + + def set_initial_value(self, y, t=0.0): + """Set initial conditions y(t) = y.""" + y = asarray(y) + self.tmp = zeros(y.size * 2, 'float') + self.tmp[::2] = real(y) + self.tmp[1::2] = imag(y) + return ode.set_initial_value(self, self.tmp, t) + + def integrate(self, t, step=False, relax=False): + """Find y=y(t), set y as an initial condition, and return y. + + Parameters + ---------- + t : float + The endpoint of the integration step. + step : bool + If True, and if the integrator supports the step method, + then perform a single integration step and return. + This parameter is provided in order to expose internals of + the implementation, and should not be changed from its default + value in most cases. + relax : bool + If True and if the integrator supports the run_relax method, + then integrate until t_1 >= t and return. ``relax`` is not + referenced if ``step=True``. + This parameter is provided in order to expose internals of + the implementation, and should not be changed from its default + value in most cases. + + Returns + ------- + y : float + The integrated value at t + """ + y = ode.integrate(self, t, step, relax) + return y[::2] + 1j * y[1::2] + + def set_solout(self, solout): + """ + Set callable to be called at every successful integration step. + + Parameters + ---------- + solout : callable + ``solout(t, y)`` is called at each internal integrator step, + t is a scalar providing the current independent position + y is the current solution ``y.shape == (n,)`` + solout should return -1 to stop integration + otherwise it should return None or 0 + + """ + if self._integrator.supports_solout: + self._integrator.set_solout(solout, complex=True) + else: + raise TypeError("selected integrator does not support solouta, " + "choose another one") + + +# ------------------------------------------------------------------------------ +# ODE integrators +# ------------------------------------------------------------------------------ + +def find_integrator(name): + for cl in IntegratorBase.integrator_classes: + if re.match(name, cl.__name__, re.I): + return cl + return None + + +class IntegratorConcurrencyError(RuntimeError): + """ + Failure due to concurrent usage of an integrator that can be used + only for a single problem at a time. + + """ + + def __init__(self, name): + msg = (f"Integrator `{name}` can be used to solve only a single problem " + "at a time. If you want to integrate multiple problems, " + "consider using a different integrator (see `ode.set_integrator`)") + RuntimeError.__init__(self, msg) + + +class IntegratorBase: + runner = None # runner is None => integrator is not available + success = None # success==1 if integrator was called successfully + istate = None # istate > 0 means success, istate < 0 means failure + supports_run_relax = None + supports_step = None + supports_solout = False + integrator_classes = [] + scalar = float + + def acquire_new_handle(self): + # Some of the integrators have internal state (ancient + # Fortran...), and so only one instance can use them at a time. + # We keep track of this, and fail when concurrent usage is tried. + self.__class__.active_global_handle += 1 + self.handle = self.__class__.active_global_handle + + def check_handle(self): + if self.handle is not self.__class__.active_global_handle: + raise IntegratorConcurrencyError(self.__class__.__name__) + + def reset(self, n, has_jac): + """Prepare integrator for call: allocate memory, set flags, etc. + n - number of equations. + has_jac - if user has supplied function for evaluating Jacobian. + """ + + def run(self, f, jac, y0, t0, t1, f_params, jac_params): + """Integrate from t=t0 to t=t1 using y0 as an initial condition. + Return 2-tuple (y1,t1) where y1 is the result and t=t1 + defines the stoppage coordinate of the result. + """ + raise NotImplementedError('all integrators must define ' + 'run(f, jac, t0, t1, y0, f_params, jac_params)') + + def step(self, f, jac, y0, t0, t1, f_params, jac_params): + """Make one integration step and return (y1,t1).""" + raise NotImplementedError(f'{self.__class__.__name__} ' + 'does not support step() method') + + def run_relax(self, f, jac, y0, t0, t1, f_params, jac_params): + """Integrate from t=t0 to t>=t1 and return (y1,t).""" + raise NotImplementedError(f'{self.__class__.__name__} ' + 'does not support run_relax() method') + + # XXX: __str__ method for getting visual state of the integrator + + +def _vode_banded_jac_wrapper(jacfunc, ml, jac_params): + """ + Wrap a banded Jacobian function with a function that pads + the Jacobian with `ml` rows of zeros. + """ + + def jac_wrapper(t, y): + jac = asarray(jacfunc(t, y, *jac_params)) + padded_jac = vstack((jac, zeros((ml, jac.shape[1])))) + return padded_jac + + return jac_wrapper + + +class vode(IntegratorBase): + runner = getattr(_vode, 'dvode', None) + + messages = {-1: 'Excess work done on this call. (Perhaps wrong MF.)', + -2: 'Excess accuracy requested. (Tolerances too small.)', + -3: 'Illegal input detected. (See printed message.)', + -4: 'Repeated error test failures. (Check all input.)', + -5: 'Repeated convergence failures. (Perhaps bad' + ' Jacobian supplied or wrong choice of MF or tolerances.)', + -6: 'Error weight became zero during problem. (Solution' + ' component i vanished, and ATOL or ATOL(i) = 0.)' + } + supports_run_relax = 1 + supports_step = 1 + active_global_handle = 0 + + def __init__(self, + method='adams', + with_jacobian=False, + rtol=1e-6, atol=1e-12, + lband=None, uband=None, + order=12, + nsteps=500, + max_step=0.0, # corresponds to infinite + min_step=0.0, + first_step=0.0, # determined by solver + ): + + if re.match(method, r'adams', re.I): + self.meth = 1 + elif re.match(method, r'bdf', re.I): + self.meth = 2 + else: + raise ValueError(f'Unknown integration method {method}') + self.with_jacobian = with_jacobian + self.rtol = rtol + self.atol = atol + self.mu = uband + self.ml = lband + + self.order = order + self.nsteps = nsteps + self.max_step = max_step + self.min_step = min_step + self.first_step = first_step + self.success = 1 + + self.initialized = False + + def _determine_mf_and_set_bands(self, has_jac): + """ + Determine the `MF` parameter (Method Flag) for the Fortran subroutine `dvode`. + + In the Fortran code, the legal values of `MF` are: + 10, 11, 12, 13, 14, 15, 20, 21, 22, 23, 24, 25, + -11, -12, -14, -15, -21, -22, -24, -25 + but this Python wrapper does not use negative values. + + Returns + + mf = 10*self.meth + miter + + self.meth is the linear multistep method: + self.meth == 1: method="adams" + self.meth == 2: method="bdf" + + miter is the correction iteration method: + miter == 0: Functional iteration; no Jacobian involved. + miter == 1: Chord iteration with user-supplied full Jacobian. + miter == 2: Chord iteration with internally computed full Jacobian. + miter == 3: Chord iteration with internally computed diagonal Jacobian. + miter == 4: Chord iteration with user-supplied banded Jacobian. + miter == 5: Chord iteration with internally computed banded Jacobian. + + Side effects: If either self.mu or self.ml is not None and the other is None, + then the one that is None is set to 0. + """ + + jac_is_banded = self.mu is not None or self.ml is not None + if jac_is_banded: + if self.mu is None: + self.mu = 0 + if self.ml is None: + self.ml = 0 + + # has_jac is True if the user provided a Jacobian function. + if has_jac: + if jac_is_banded: + miter = 4 + else: + miter = 1 + else: + if jac_is_banded: + if self.ml == self.mu == 0: + miter = 3 # Chord iteration with internal diagonal Jacobian. + else: + miter = 5 # Chord iteration with internal banded Jacobian. + else: + # self.with_jacobian is set by the user in + # the call to ode.set_integrator. + if self.with_jacobian: + miter = 2 # Chord iteration with internal full Jacobian. + else: + miter = 0 # Functional iteration; no Jacobian involved. + + mf = 10 * self.meth + miter + return mf + + def reset(self, n, has_jac): + mf = self._determine_mf_and_set_bands(has_jac) + + if mf == 10: + lrw = 20 + 16 * n + elif mf in [11, 12]: + lrw = 22 + 16 * n + 2 * n * n + elif mf == 13: + lrw = 22 + 17 * n + elif mf in [14, 15]: + lrw = 22 + 18 * n + (3 * self.ml + 2 * self.mu) * n + elif mf == 20: + lrw = 20 + 9 * n + elif mf in [21, 22]: + lrw = 22 + 9 * n + 2 * n * n + elif mf == 23: + lrw = 22 + 10 * n + elif mf in [24, 25]: + lrw = 22 + 11 * n + (3 * self.ml + 2 * self.mu) * n + else: + raise ValueError(f'Unexpected mf={mf}') + + if mf % 10 in [0, 3]: + liw = 30 + else: + liw = 30 + n + + rwork = zeros((lrw,), float) + rwork[4] = self.first_step + rwork[5] = self.max_step + rwork[6] = self.min_step + self.rwork = rwork + + iwork = zeros((liw,), _vode_int_dtype) + if self.ml is not None: + iwork[0] = self.ml + if self.mu is not None: + iwork[1] = self.mu + iwork[4] = self.order + iwork[5] = self.nsteps + iwork[6] = 2 # mxhnil + self.iwork = iwork + + self.call_args = [self.rtol, self.atol, 1, 1, + self.rwork, self.iwork, mf] + self.success = 1 + self.initialized = False + + def run(self, f, jac, y0, t0, t1, f_params, jac_params): + if self.initialized: + self.check_handle() + else: + self.initialized = True + self.acquire_new_handle() + + if self.ml is not None and self.ml > 0: + # Banded Jacobian. Wrap the user-provided function with one + # that pads the Jacobian array with the extra `self.ml` rows + # required by the f2py-generated wrapper. + jac = _vode_banded_jac_wrapper(jac, self.ml, jac_params) + + args = ((f, jac, y0, t0, t1) + tuple(self.call_args) + + (f_params, jac_params)) + + with VODE_LOCK: + y1, t, istate = self.runner(*args) + + self.istate = istate + if istate < 0: + unexpected_istate_msg = f'Unexpected istate={istate:d}' + warnings.warn(f'{self.__class__.__name__:s}: ' + f'{self.messages.get(istate, unexpected_istate_msg):s}', + stacklevel=2) + self.success = 0 + else: + self.call_args[3] = 2 # upgrade istate from 1 to 2 + self.istate = 2 + return y1, t + + def step(self, *args): + itask = self.call_args[2] + self.call_args[2] = 2 + r = self.run(*args) + self.call_args[2] = itask + return r + + def run_relax(self, *args): + itask = self.call_args[2] + self.call_args[2] = 3 + r = self.run(*args) + self.call_args[2] = itask + return r + + +if vode.runner is not None: + IntegratorBase.integrator_classes.append(vode) + + +class zvode(vode): + runner = getattr(_vode, 'zvode', None) + + supports_run_relax = 1 + supports_step = 1 + scalar = complex + active_global_handle = 0 + + def reset(self, n, has_jac): + mf = self._determine_mf_and_set_bands(has_jac) + + if mf in (10,): + lzw = 15 * n + elif mf in (11, 12): + lzw = 15 * n + 2 * n ** 2 + elif mf in (-11, -12): + lzw = 15 * n + n ** 2 + elif mf in (13,): + lzw = 16 * n + elif mf in (14, 15): + lzw = 17 * n + (3 * self.ml + 2 * self.mu) * n + elif mf in (-14, -15): + lzw = 16 * n + (2 * self.ml + self.mu) * n + elif mf in (20,): + lzw = 8 * n + elif mf in (21, 22): + lzw = 8 * n + 2 * n ** 2 + elif mf in (-21, -22): + lzw = 8 * n + n ** 2 + elif mf in (23,): + lzw = 9 * n + elif mf in (24, 25): + lzw = 10 * n + (3 * self.ml + 2 * self.mu) * n + elif mf in (-24, -25): + lzw = 9 * n + (2 * self.ml + self.mu) * n + + lrw = 20 + n + + if mf % 10 in (0, 3): + liw = 30 + else: + liw = 30 + n + + zwork = zeros((lzw,), complex) + self.zwork = zwork + + rwork = zeros((lrw,), float) + rwork[4] = self.first_step + rwork[5] = self.max_step + rwork[6] = self.min_step + self.rwork = rwork + + iwork = zeros((liw,), _vode_int_dtype) + if self.ml is not None: + iwork[0] = self.ml + if self.mu is not None: + iwork[1] = self.mu + iwork[4] = self.order + iwork[5] = self.nsteps + iwork[6] = 2 # mxhnil + self.iwork = iwork + + self.call_args = [self.rtol, self.atol, 1, 1, + self.zwork, self.rwork, self.iwork, mf] + self.success = 1 + self.initialized = False + + +if zvode.runner is not None: + IntegratorBase.integrator_classes.append(zvode) + + +class dopri5(IntegratorBase): + runner = getattr(_dop, 'dopri5', None) + name = 'dopri5' + supports_solout = True + + messages = {1: 'computation successful', + 2: 'computation successful (interrupted by solout)', + -1: 'input is not consistent', + -2: 'larger nsteps is needed', + -3: 'step size becomes too small', + -4: 'problem is probably stiff (interrupted)', + } + + def __init__(self, + rtol=1e-6, atol=1e-12, + nsteps=500, + max_step=0.0, + first_step=0.0, # determined by solver + safety=0.9, + ifactor=10.0, + dfactor=0.2, + beta=0.0, + method=None, + verbosity=-1, # no messages if negative + ): + self.rtol = rtol + self.atol = atol + self.nsteps = nsteps + self.max_step = max_step + self.first_step = first_step + self.safety = safety + self.ifactor = ifactor + self.dfactor = dfactor + self.beta = beta + self.verbosity = verbosity + self.success = 1 + self.set_solout(None) + + def set_solout(self, solout, complex=False): + self.solout = solout + self.solout_cmplx = complex + if solout is None: + self.iout = 0 + else: + self.iout = 1 + + def reset(self, n, has_jac): + work = zeros((8 * n + 21,), float) + work[1] = self.safety + work[2] = self.dfactor + work[3] = self.ifactor + work[4] = self.beta + work[5] = self.max_step + work[6] = self.first_step + self.work = work + iwork = zeros((21,), _dop_int_dtype) + iwork[0] = self.nsteps + iwork[2] = self.verbosity + self.iwork = iwork + self.call_args = [self.rtol, self.atol, self._solout, + self.iout, self.work, self.iwork] + self.success = 1 + + def run(self, f, jac, y0, t0, t1, f_params, jac_params): + x, y, iwork, istate = self.runner(*((f, t0, y0, t1) + + tuple(self.call_args) + (f_params,))) + self.istate = istate + if istate < 0: + unexpected_istate_msg = f'Unexpected istate={istate:d}' + warnings.warn(f'{self.__class__.__name__:s}: ' + f'{self.messages.get(istate, unexpected_istate_msg):s}', + stacklevel=2) + self.success = 0 + return y, x + + def _solout(self, nr, xold, x, y, nd, icomp, con): + if self.solout is not None: + if self.solout_cmplx: + y = y[::2] + 1j * y[1::2] + return self.solout(x, y) + else: + return 1 + + +if dopri5.runner is not None: + IntegratorBase.integrator_classes.append(dopri5) + + +class dop853(dopri5): + runner = getattr(_dop, 'dop853', None) + name = 'dop853' + + def __init__(self, + rtol=1e-6, atol=1e-12, + nsteps=500, + max_step=0.0, + first_step=0.0, # determined by solver + safety=0.9, + ifactor=6.0, + dfactor=0.3, + beta=0.0, + method=None, + verbosity=-1, # no messages if negative + ): + super().__init__(rtol, atol, nsteps, max_step, first_step, safety, + ifactor, dfactor, beta, method, verbosity) + + def reset(self, n, has_jac): + work = zeros((11 * n + 21,), float) + work[1] = self.safety + work[2] = self.dfactor + work[3] = self.ifactor + work[4] = self.beta + work[5] = self.max_step + work[6] = self.first_step + self.work = work + iwork = zeros((21,), _dop_int_dtype) + iwork[0] = self.nsteps + iwork[2] = self.verbosity + self.iwork = iwork + self.call_args = [self.rtol, self.atol, self._solout, + self.iout, self.work, self.iwork] + self.success = 1 + + +if dop853.runner is not None: + IntegratorBase.integrator_classes.append(dop853) + + +class lsoda(IntegratorBase): + runner = getattr(_lsoda, 'lsoda', None) + active_global_handle = 0 + + messages = { + 2: "Integration successful.", + -1: "Excess work done on this call (perhaps wrong Dfun type).", + -2: "Excess accuracy requested (tolerances too small).", + -3: "Illegal input detected (internal error).", + -4: "Repeated error test failures (internal error).", + -5: "Repeated convergence failures (perhaps bad Jacobian or tolerances).", + -6: "Error weight became zero during problem.", + -7: "Internal workspace insufficient to finish (internal error)." + } + + def __init__(self, + with_jacobian=False, + rtol=1e-6, atol=1e-12, + lband=None, uband=None, + nsteps=500, + max_step=0.0, # corresponds to infinite + min_step=0.0, + first_step=0.0, # determined by solver + ixpr=0, + max_hnil=0, + max_order_ns=12, + max_order_s=5, + method=None + ): + + self.with_jacobian = with_jacobian + self.rtol = rtol + self.atol = atol + self.mu = uband + self.ml = lband + + self.max_order_ns = max_order_ns + self.max_order_s = max_order_s + self.nsteps = nsteps + self.max_step = max_step + self.min_step = min_step + self.first_step = first_step + self.ixpr = ixpr + self.max_hnil = max_hnil + self.success = 1 + + self.initialized = False + + def reset(self, n, has_jac): + # Calculate parameters for Fortran subroutine dvode. + if has_jac: + if self.mu is None and self.ml is None: + jt = 1 + else: + if self.mu is None: + self.mu = 0 + if self.ml is None: + self.ml = 0 + jt = 4 + else: + if self.mu is None and self.ml is None: + jt = 2 + else: + if self.mu is None: + self.mu = 0 + if self.ml is None: + self.ml = 0 + jt = 5 + lrn = 20 + (self.max_order_ns + 4) * n + if jt in [1, 2]: + lrs = 22 + (self.max_order_s + 4) * n + n * n + elif jt in [4, 5]: + lrs = 22 + (self.max_order_s + 5 + 2 * self.ml + self.mu) * n + else: + raise ValueError(f'Unexpected jt={jt}') + lrw = max(lrn, lrs) + liw = 20 + n + rwork = zeros((lrw,), float) + rwork[4] = self.first_step + rwork[5] = self.max_step + rwork[6] = self.min_step + self.rwork = rwork + iwork = zeros((liw,), _lsoda_int_dtype) + if self.ml is not None: + iwork[0] = self.ml + if self.mu is not None: + iwork[1] = self.mu + iwork[4] = self.ixpr + iwork[5] = self.nsteps + iwork[6] = self.max_hnil + iwork[7] = self.max_order_ns + iwork[8] = self.max_order_s + self.iwork = iwork + self.call_args = [self.rtol, self.atol, 1, 1, + self.rwork, self.iwork, jt] + self.success = 1 + self.initialized = False + + def run(self, f, jac, y0, t0, t1, f_params, jac_params): + if self.initialized: + self.check_handle() + else: + self.initialized = True + self.acquire_new_handle() + args = [f, y0, t0, t1] + self.call_args[:-1] + \ + [jac, self.call_args[-1], f_params, 0, jac_params] + + with LSODA_LOCK: + y1, t, istate = self.runner(*args) + + self.istate = istate + if istate < 0: + unexpected_istate_msg = f'Unexpected istate={istate:d}' + warnings.warn(f'{self.__class__.__name__:s}: ' + f'{self.messages.get(istate, unexpected_istate_msg):s}', + stacklevel=2) + self.success = 0 + else: + self.call_args[3] = 2 # upgrade istate from 1 to 2 + self.istate = 2 + return y1, t + + def step(self, *args): + itask = self.call_args[2] + self.call_args[2] = 2 + r = self.run(*args) + self.call_args[2] = itask + return r + + def run_relax(self, *args): + itask = self.call_args[2] + self.call_args[2] = 3 + r = self.run(*args) + self.call_args[2] = itask + return r + + +if lsoda.runner: + IntegratorBase.integrator_classes.append(lsoda) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_odepack_py.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_odepack_py.py new file mode 100644 index 0000000000000000000000000000000000000000..75dfe925b312ae609d19ccbec27927c6c945176f --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_odepack_py.py @@ -0,0 +1,273 @@ +# Author: Travis Oliphant + +__all__ = ['odeint', 'ODEintWarning'] + +import numpy as np +from . import _odepack +from copy import copy +import warnings + +from threading import Lock + + +ODE_LOCK = Lock() + + +class ODEintWarning(Warning): + """Warning raised during the execution of `odeint`.""" + pass + + +_msgs = {2: "Integration successful.", + 1: "Nothing was done; the integration time was 0.", + -1: "Excess work done on this call (perhaps wrong Dfun type).", + -2: "Excess accuracy requested (tolerances too small).", + -3: "Illegal input detected (internal error).", + -4: "Repeated error test failures (internal error).", + -5: "Repeated convergence failures (perhaps bad Jacobian or tolerances).", + -6: "Error weight became zero during problem.", + -7: "Internal workspace insufficient to finish (internal error).", + -8: "Run terminated (internal error)." + } + + +def odeint(func, y0, t, args=(), Dfun=None, col_deriv=0, full_output=0, + ml=None, mu=None, rtol=None, atol=None, tcrit=None, h0=0.0, + hmax=0.0, hmin=0.0, ixpr=0, mxstep=0, mxhnil=0, mxordn=12, + mxords=5, printmessg=0, tfirst=False): + """ + Integrate a system of ordinary differential equations. + + .. note:: For new code, use `scipy.integrate.solve_ivp` to solve a + differential equation. + + Solve a system of ordinary differential equations using lsoda from the + FORTRAN library odepack. + + Solves the initial value problem for stiff or non-stiff systems + of first order ode-s:: + + dy/dt = func(y, t, ...) [or func(t, y, ...)] + + where y can be a vector. + + .. note:: By default, the required order of the first two arguments of + `func` are in the opposite order of the arguments in the system + definition function used by the `scipy.integrate.ode` class and + the function `scipy.integrate.solve_ivp`. To use a function with + the signature ``func(t, y, ...)``, the argument `tfirst` must be + set to ``True``. + + Parameters + ---------- + func : callable(y, t, ...) or callable(t, y, ...) + Computes the derivative of y at t. + If the signature is ``callable(t, y, ...)``, then the argument + `tfirst` must be set ``True``. + `func` must not modify the data in `y`, as it is a + view of the data used internally by the ODE solver. + y0 : array + Initial condition on y (can be a vector). + t : array + A sequence of time points for which to solve for y. The initial + value point should be the first element of this sequence. + This sequence must be monotonically increasing or monotonically + decreasing; repeated values are allowed. + args : tuple, optional + Extra arguments to pass to function. + Dfun : callable(y, t, ...) or callable(t, y, ...) + Gradient (Jacobian) of `func`. + If the signature is ``callable(t, y, ...)``, then the argument + `tfirst` must be set ``True``. + `Dfun` must not modify the data in `y`, as it is a + view of the data used internally by the ODE solver. + col_deriv : bool, optional + True if `Dfun` defines derivatives down columns (faster), + otherwise `Dfun` should define derivatives across rows. + full_output : bool, optional + True if to return a dictionary of optional outputs as the second output + printmessg : bool, optional + Whether to print the convergence message + tfirst : bool, optional + If True, the first two arguments of `func` (and `Dfun`, if given) + must ``t, y`` instead of the default ``y, t``. + + .. versionadded:: 1.1.0 + + Returns + ------- + y : array, shape (len(t), len(y0)) + Array containing the value of y for each desired time in t, + with the initial value `y0` in the first row. + infodict : dict, only returned if full_output == True + Dictionary containing additional output information + + ======= ============================================================ + key meaning + ======= ============================================================ + 'hu' vector of step sizes successfully used for each time step + 'tcur' vector with the value of t reached for each time step + (will always be at least as large as the input times) + 'tolsf' vector of tolerance scale factors, greater than 1.0, + computed when a request for too much accuracy was detected + 'tsw' value of t at the time of the last method switch + (given for each time step) + 'nst' cumulative number of time steps + 'nfe' cumulative number of function evaluations for each time step + 'nje' cumulative number of jacobian evaluations for each time step + 'nqu' a vector of method orders for each successful step + 'imxer' index of the component of largest magnitude in the + weighted local error vector (e / ewt) on an error return, -1 + otherwise + 'lenrw' the length of the double work array required + 'leniw' the length of integer work array required + 'mused' a vector of method indicators for each successful time step: + 1: adams (nonstiff), 2: bdf (stiff) + ======= ============================================================ + + Other Parameters + ---------------- + ml, mu : int, optional + If either of these are not None or non-negative, then the + Jacobian is assumed to be banded. These give the number of + lower and upper non-zero diagonals in this banded matrix. + For the banded case, `Dfun` should return a matrix whose + rows contain the non-zero bands (starting with the lowest diagonal). + Thus, the return matrix `jac` from `Dfun` should have shape + ``(ml + mu + 1, len(y0))`` when ``ml >=0`` or ``mu >=0``. + The data in `jac` must be stored such that ``jac[i - j + mu, j]`` + holds the derivative of the ``i``\\ th equation with respect to the + ``j``\\ th state variable. If `col_deriv` is True, the transpose of + this `jac` must be returned. + rtol, atol : float, optional + The input parameters `rtol` and `atol` determine the error + control performed by the solver. The solver will control the + vector, e, of estimated local errors in y, according to an + inequality of the form ``max-norm of (e / ewt) <= 1``, + where ewt is a vector of positive error weights computed as + ``ewt = rtol * abs(y) + atol``. + rtol and atol can be either vectors the same length as y or scalars. + Defaults to 1.49012e-8. + tcrit : ndarray, optional + Vector of critical points (e.g., singularities) where integration + care should be taken. + h0 : float, (0: solver-determined), optional + The step size to be attempted on the first step. + hmax : float, (0: solver-determined), optional + The maximum absolute step size allowed. + hmin : float, (0: solver-determined), optional + The minimum absolute step size allowed. + ixpr : bool, optional + Whether to generate extra printing at method switches. + mxstep : int, (0: solver-determined), optional + Maximum number of (internally defined) steps allowed for each + integration point in t. + mxhnil : int, (0: solver-determined), optional + Maximum number of messages printed. + mxordn : int, (0: solver-determined), optional + Maximum order to be allowed for the non-stiff (Adams) method. + mxords : int, (0: solver-determined), optional + Maximum order to be allowed for the stiff (BDF) method. + + See Also + -------- + solve_ivp : solve an initial value problem for a system of ODEs + ode : a more object-oriented integrator based on VODE + quad : for finding the area under a curve + + Examples + -------- + The second order differential equation for the angle `theta` of a + pendulum acted on by gravity with friction can be written:: + + theta''(t) + b*theta'(t) + c*sin(theta(t)) = 0 + + where `b` and `c` are positive constants, and a prime (') denotes a + derivative. To solve this equation with `odeint`, we must first convert + it to a system of first order equations. By defining the angular + velocity ``omega(t) = theta'(t)``, we obtain the system:: + + theta'(t) = omega(t) + omega'(t) = -b*omega(t) - c*sin(theta(t)) + + Let `y` be the vector [`theta`, `omega`]. We implement this system + in Python as: + + >>> import numpy as np + >>> def pend(y, t, b, c): + ... theta, omega = y + ... dydt = [omega, -b*omega - c*np.sin(theta)] + ... return dydt + ... + + We assume the constants are `b` = 0.25 and `c` = 5.0: + + >>> b = 0.25 + >>> c = 5.0 + + For initial conditions, we assume the pendulum is nearly vertical + with `theta(0)` = `pi` - 0.1, and is initially at rest, so + `omega(0)` = 0. Then the vector of initial conditions is + + >>> y0 = [np.pi - 0.1, 0.0] + + We will generate a solution at 101 evenly spaced samples in the interval + 0 <= `t` <= 10. So our array of times is: + + >>> t = np.linspace(0, 10, 101) + + Call `odeint` to generate the solution. To pass the parameters + `b` and `c` to `pend`, we give them to `odeint` using the `args` + argument. + + >>> from scipy.integrate import odeint + >>> sol = odeint(pend, y0, t, args=(b, c)) + + The solution is an array with shape (101, 2). The first column + is `theta(t)`, and the second is `omega(t)`. The following code + plots both components. + + >>> import matplotlib.pyplot as plt + >>> plt.plot(t, sol[:, 0], 'b', label='theta(t)') + >>> plt.plot(t, sol[:, 1], 'g', label='omega(t)') + >>> plt.legend(loc='best') + >>> plt.xlabel('t') + >>> plt.grid() + >>> plt.show() + """ + + if ml is None: + ml = -1 # changed to zero inside function call + if mu is None: + mu = -1 # changed to zero inside function call + + dt = np.diff(t) + if not ((dt >= 0).all() or (dt <= 0).all()): + raise ValueError("The values in t must be monotonically increasing " + "or monotonically decreasing; repeated values are " + "allowed.") + + t = copy(t) + y0 = copy(y0) + + with ODE_LOCK: + output = _odepack.odeint(func, y0, t, args, Dfun, col_deriv, ml, mu, + full_output, rtol, atol, tcrit, h0, hmax, hmin, + ixpr, mxstep, mxhnil, mxordn, mxords, + int(bool(tfirst))) + if output[-1] < 0: + warning_msg = (f"{_msgs[output[-1]]} Run with full_output = 1 to " + f"get quantitative information.") + warnings.warn(warning_msg, ODEintWarning, stacklevel=2) + elif printmessg: + warning_msg = _msgs[output[-1]] + warnings.warn(warning_msg, ODEintWarning, stacklevel=2) + + if full_output: + output[1]['message'] = _msgs[output[-1]] + + output = output[:-1] + if len(output) == 1: + return output[0] + else: + return output diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_quad_vec.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_quad_vec.py new file mode 100644 index 0000000000000000000000000000000000000000..758bac5138777dbe152c2b455b5160196d2282ca --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_quad_vec.py @@ -0,0 +1,682 @@ +import sys +import copy +import heapq +import collections +import functools +import warnings + +import numpy as np + +from scipy._lib._util import MapWrapper, _FunctionWrapper + + +class LRUDict(collections.OrderedDict): + def __init__(self, max_size): + self.__max_size = max_size + + def __setitem__(self, key, value): + existing_key = (key in self) + super().__setitem__(key, value) + if existing_key: + self.move_to_end(key) + elif len(self) > self.__max_size: + self.popitem(last=False) + + def update(self, other): + # Not needed below + raise NotImplementedError() + + +class SemiInfiniteFunc: + """ + Argument transform from (start, +-oo) to (0, 1) + """ + def __init__(self, func, start, infty): + self._func = func + self._start = start + self._sgn = -1 if infty < 0 else 1 + + # Overflow threshold for the 1/t**2 factor + self._tmin = sys.float_info.min**0.5 + + def get_t(self, x): + z = self._sgn * (x - self._start) + 1 + if z == 0: + # Can happen only if point not in range + return np.inf + return 1 / z + + def __call__(self, t): + if t < self._tmin: + return 0.0 + else: + x = self._start + self._sgn * (1 - t) / t + f = self._func(x) + return self._sgn * (f / t) / t + + +class DoubleInfiniteFunc: + """ + Argument transform from (-oo, oo) to (-1, 1) + """ + def __init__(self, func): + self._func = func + + # Overflow threshold for the 1/t**2 factor + self._tmin = sys.float_info.min**0.5 + + def get_t(self, x): + s = -1 if x < 0 else 1 + return s / (abs(x) + 1) + + def __call__(self, t): + if abs(t) < self._tmin: + return 0.0 + else: + x = (1 - abs(t)) / t + f = self._func(x) + return (f / t) / t + + +def _max_norm(x): + return np.amax(abs(x)) + + +def _get_sizeof(obj): + try: + return sys.getsizeof(obj) + except TypeError: + # occurs on pypy + if hasattr(obj, '__sizeof__'): + return int(obj.__sizeof__()) + return 64 + + +class _Bunch: + def __init__(self, **kwargs): + self.__keys = kwargs.keys() + self.__dict__.update(**kwargs) + + def __repr__(self): + key_value_pairs = ', '.join( + f'{k}={repr(self.__dict__[k])}' for k in self.__keys + ) + return f"_Bunch({key_value_pairs})" + + +def quad_vec(f, a, b, epsabs=1e-200, epsrel=1e-8, norm='2', cache_size=100e6, + limit=10000, workers=1, points=None, quadrature=None, full_output=False, + *, args=()): + r"""Adaptive integration of a vector-valued function. + + Parameters + ---------- + f : callable + Vector-valued function f(x) to integrate. + a : float + Initial point. + b : float + Final point. + epsabs : float, optional + Absolute tolerance. + epsrel : float, optional + Relative tolerance. + norm : {'max', '2'}, optional + Vector norm to use for error estimation. + cache_size : int, optional + Number of bytes to use for memoization. + limit : float or int, optional + An upper bound on the number of subintervals used in the adaptive + algorithm. + workers : int or map-like callable, optional + If `workers` is an integer, part of the computation is done in + parallel subdivided to this many tasks (using + :class:`python:multiprocessing.pool.Pool`). + Supply `-1` to use all cores available to the Process. + Alternatively, supply a map-like callable, such as + :meth:`python:multiprocessing.pool.Pool.map` for evaluating the + population in parallel. + This evaluation is carried out as ``workers(func, iterable)``. + points : list, optional + List of additional breakpoints. + quadrature : {'gk21', 'gk15', 'trapezoid'}, optional + Quadrature rule to use on subintervals. + Options: 'gk21' (Gauss-Kronrod 21-point rule), + 'gk15' (Gauss-Kronrod 15-point rule), + 'trapezoid' (composite trapezoid rule). + Default: 'gk21' for finite intervals and 'gk15' for (semi-)infinite + full_output : bool, optional + Return an additional ``info`` dictionary. + args : tuple, optional + Extra arguments to pass to function, if any. + + .. versionadded:: 1.8.0 + + Returns + ------- + res : {float, array-like} + Estimate for the result + err : float + Error estimate for the result in the given norm + info : dict + Returned only when ``full_output=True``. + Info dictionary. Is an object with the attributes: + + success : bool + Whether integration reached target precision. + status : int + Indicator for convergence, success (0), + failure (1), and failure due to rounding error (2). + neval : int + Number of function evaluations. + intervals : ndarray, shape (num_intervals, 2) + Start and end points of subdivision intervals. + integrals : ndarray, shape (num_intervals, ...) + Integral for each interval. + Note that at most ``cache_size`` values are recorded, + and the array may contains *nan* for missing items. + errors : ndarray, shape (num_intervals,) + Estimated integration error for each interval. + + Notes + ----- + The algorithm mainly follows the implementation of QUADPACK's + DQAG* algorithms, implementing global error control and adaptive + subdivision. + + The algorithm here has some differences to the QUADPACK approach: + + Instead of subdividing one interval at a time, the algorithm + subdivides N intervals with largest errors at once. This enables + (partial) parallelization of the integration. + + The logic of subdividing "next largest" intervals first is then + not implemented, and we rely on the above extension to avoid + concentrating on "small" intervals only. + + The Wynn epsilon table extrapolation is not used (QUADPACK uses it + for infinite intervals). This is because the algorithm here is + supposed to work on vector-valued functions, in an user-specified + norm, and the extension of the epsilon algorithm to this case does + not appear to be widely agreed. For max-norm, using elementwise + Wynn epsilon could be possible, but we do not do this here with + the hope that the epsilon extrapolation is mainly useful in + special cases. + + References + ---------- + [1] R. Piessens, E. de Doncker, QUADPACK (1983). + + Examples + -------- + We can compute integrations of a vector-valued function: + + >>> from scipy.integrate import quad_vec + >>> import numpy as np + >>> import matplotlib.pyplot as plt + >>> alpha = np.linspace(0.0, 2.0, num=30) + >>> f = lambda x: x**alpha + >>> x0, x1 = 0, 2 + >>> y, err = quad_vec(f, x0, x1) + >>> plt.plot(alpha, y) + >>> plt.xlabel(r"$\alpha$") + >>> plt.ylabel(r"$\int_{0}^{2} x^\alpha dx$") + >>> plt.show() + + When using the argument `workers`, one should ensure + that the main module is import-safe, for instance + by rewriting the example above as: + + .. code-block:: python + + from scipy.integrate import quad_vec + import numpy as np + import matplotlib.pyplot as plt + + alpha = np.linspace(0.0, 2.0, num=30) + x0, x1 = 0, 2 + def f(x): + return x**alpha + + if __name__ == "__main__": + y, err = quad_vec(f, x0, x1, workers=2) + """ + a = float(a) + b = float(b) + + if args: + if not isinstance(args, tuple): + args = (args,) + + # create a wrapped function to allow the use of map and Pool.map + f = _FunctionWrapper(f, args) + + # Use simple transformations to deal with integrals over infinite + # intervals. + kwargs = dict(epsabs=epsabs, + epsrel=epsrel, + norm=norm, + cache_size=cache_size, + limit=limit, + workers=workers, + points=points, + quadrature='gk15' if quadrature is None else quadrature, + full_output=full_output) + if np.isfinite(a) and np.isinf(b): + f2 = SemiInfiniteFunc(f, start=a, infty=b) + if points is not None: + kwargs['points'] = tuple(f2.get_t(xp) for xp in points) + return quad_vec(f2, 0, 1, **kwargs) + elif np.isfinite(b) and np.isinf(a): + f2 = SemiInfiniteFunc(f, start=b, infty=a) + if points is not None: + kwargs['points'] = tuple(f2.get_t(xp) for xp in points) + res = quad_vec(f2, 0, 1, **kwargs) + return (-res[0],) + res[1:] + elif np.isinf(a) and np.isinf(b): + sgn = -1 if b < a else 1 + + # NB. explicitly split integral at t=0, which separates + # the positive and negative sides + f2 = DoubleInfiniteFunc(f) + if points is not None: + kwargs['points'] = (0,) + tuple(f2.get_t(xp) for xp in points) + else: + kwargs['points'] = (0,) + + if a != b: + res = quad_vec(f2, -1, 1, **kwargs) + else: + res = quad_vec(f2, 1, 1, **kwargs) + + return (res[0]*sgn,) + res[1:] + elif not (np.isfinite(a) and np.isfinite(b)): + raise ValueError(f"invalid integration bounds a={a}, b={b}") + + norm_funcs = { + None: _max_norm, + 'max': _max_norm, + '2': np.linalg.norm + } + if callable(norm): + norm_func = norm + else: + norm_func = norm_funcs[norm] + + parallel_count = 128 + min_intervals = 2 + + try: + _quadrature = {None: _quadrature_gk21, + 'gk21': _quadrature_gk21, + 'gk15': _quadrature_gk15, + 'trapz': _quadrature_trapezoid, # alias for backcompat + 'trapezoid': _quadrature_trapezoid}[quadrature] + except KeyError as e: + raise ValueError(f"unknown quadrature {quadrature!r}") from e + + if quadrature == "trapz": + msg = ("`quadrature='trapz'` is deprecated in favour of " + "`quadrature='trapezoid' and will raise an error from SciPy 1.16.0 " + "onwards.") + warnings.warn(msg, DeprecationWarning, stacklevel=2) + + # Initial interval set + if points is None: + initial_intervals = [(a, b)] + else: + prev = a + initial_intervals = [] + for p in sorted(points): + p = float(p) + if not (a < p < b) or p == prev: + continue + initial_intervals.append((prev, p)) + prev = p + initial_intervals.append((prev, b)) + + global_integral = None + global_error = None + rounding_error = None + interval_cache = None + intervals = [] + neval = 0 + + for x1, x2 in initial_intervals: + ig, err, rnd = _quadrature(x1, x2, f, norm_func) + neval += _quadrature.num_eval + + if global_integral is None: + if isinstance(ig, (float, complex)): + # Specialize for scalars + if norm_func in (_max_norm, np.linalg.norm): + norm_func = abs + + global_integral = ig + global_error = float(err) + rounding_error = float(rnd) + + cache_count = cache_size // _get_sizeof(ig) + interval_cache = LRUDict(cache_count) + else: + global_integral += ig + global_error += err + rounding_error += rnd + + interval_cache[(x1, x2)] = copy.copy(ig) + intervals.append((-err, x1, x2)) + + heapq.heapify(intervals) + + CONVERGED = 0 + NOT_CONVERGED = 1 + ROUNDING_ERROR = 2 + NOT_A_NUMBER = 3 + + status_msg = { + CONVERGED: "Target precision reached.", + NOT_CONVERGED: "Target precision not reached.", + ROUNDING_ERROR: "Target precision could not be reached due to rounding error.", + NOT_A_NUMBER: "Non-finite values encountered." + } + + # Process intervals + with MapWrapper(workers) as mapwrapper: + ier = NOT_CONVERGED + + while intervals and len(intervals) < limit: + # Select intervals with largest errors for subdivision + tol = max(epsabs, epsrel*norm_func(global_integral)) + + to_process = [] + err_sum = 0 + + for j in range(parallel_count): + if not intervals: + break + + if j > 0 and err_sum > global_error - tol/8: + # avoid unnecessary parallel splitting + break + + interval = heapq.heappop(intervals) + + neg_old_err, a, b = interval + old_int = interval_cache.pop((a, b), None) + to_process.append( + ((-neg_old_err, a, b, old_int), f, norm_func, _quadrature) + ) + err_sum += -neg_old_err + + # Subdivide intervals + for parts in mapwrapper(_subdivide_interval, to_process): + dint, derr, dround_err, subint, dneval = parts + neval += dneval + global_integral += dint + global_error += derr + rounding_error += dround_err + for x in subint: + x1, x2, ig, err = x + interval_cache[(x1, x2)] = ig + heapq.heappush(intervals, (-err, x1, x2)) + + # Termination check + if len(intervals) >= min_intervals: + tol = max(epsabs, epsrel*norm_func(global_integral)) + if global_error < tol/8: + ier = CONVERGED + break + if global_error < rounding_error: + ier = ROUNDING_ERROR + break + + if not (np.isfinite(global_error) and np.isfinite(rounding_error)): + ier = NOT_A_NUMBER + break + + res = global_integral + err = global_error + rounding_error + + if full_output: + res_arr = np.asarray(res) + dummy = np.full(res_arr.shape, np.nan, dtype=res_arr.dtype) + integrals = np.array([interval_cache.get((z[1], z[2]), dummy) + for z in intervals], dtype=res_arr.dtype) + errors = np.array([-z[0] for z in intervals]) + intervals = np.array([[z[1], z[2]] for z in intervals]) + + info = _Bunch(neval=neval, + success=(ier == CONVERGED), + status=ier, + message=status_msg[ier], + intervals=intervals, + integrals=integrals, + errors=errors) + return (res, err, info) + else: + return (res, err) + + +def _subdivide_interval(args): + interval, f, norm_func, _quadrature = args + old_err, a, b, old_int = interval + + c = 0.5 * (a + b) + + # Left-hand side + if getattr(_quadrature, 'cache_size', 0) > 0: + f = functools.lru_cache(_quadrature.cache_size)(f) + + s1, err1, round1 = _quadrature(a, c, f, norm_func) + dneval = _quadrature.num_eval + s2, err2, round2 = _quadrature(c, b, f, norm_func) + dneval += _quadrature.num_eval + if old_int is None: + old_int, _, _ = _quadrature(a, b, f, norm_func) + dneval += _quadrature.num_eval + + if getattr(_quadrature, 'cache_size', 0) > 0: + dneval = f.cache_info().misses + + dint = s1 + s2 - old_int + derr = err1 + err2 - old_err + dround_err = round1 + round2 + + subintervals = ((a, c, s1, err1), (c, b, s2, err2)) + return dint, derr, dround_err, subintervals, dneval + + +def _quadrature_trapezoid(x1, x2, f, norm_func): + """ + Composite trapezoid quadrature + """ + x3 = 0.5*(x1 + x2) + f1 = f(x1) + f2 = f(x2) + f3 = f(x3) + + s2 = 0.25 * (x2 - x1) * (f1 + 2*f3 + f2) + + round_err = 0.25 * abs(x2 - x1) * (float(norm_func(f1)) + + 2*float(norm_func(f3)) + + float(norm_func(f2))) * 2e-16 + + s1 = 0.5 * (x2 - x1) * (f1 + f2) + err = 1/3 * float(norm_func(s1 - s2)) + return s2, err, round_err + + +_quadrature_trapezoid.cache_size = 3 * 3 +_quadrature_trapezoid.num_eval = 3 + + +def _quadrature_gk(a, b, f, norm_func, x, w, v): + """ + Generic Gauss-Kronrod quadrature + """ + + fv = [0.0]*len(x) + + c = 0.5 * (a + b) + h = 0.5 * (b - a) + + # Gauss-Kronrod + s_k = 0.0 + s_k_abs = 0.0 + for i in range(len(x)): + ff = f(c + h*x[i]) + fv[i] = ff + + vv = v[i] + + # \int f(x) + s_k += vv * ff + # \int |f(x)| + s_k_abs += vv * abs(ff) + + # Gauss + s_g = 0.0 + for i in range(len(w)): + s_g += w[i] * fv[2*i + 1] + + # Quadrature of abs-deviation from average + s_k_dabs = 0.0 + y0 = s_k / 2.0 + for i in range(len(x)): + # \int |f(x) - y0| + s_k_dabs += v[i] * abs(fv[i] - y0) + + # Use similar error estimation as quadpack + err = float(norm_func((s_k - s_g) * h)) + dabs = float(norm_func(s_k_dabs * h)) + if dabs != 0 and err != 0: + err = dabs * min(1.0, (200 * err / dabs)**1.5) + + eps = sys.float_info.epsilon + round_err = float(norm_func(50 * eps * h * s_k_abs)) + + if round_err > sys.float_info.min: + err = max(err, round_err) + + return h * s_k, err, round_err + + +def _quadrature_gk21(a, b, f, norm_func): + """ + Gauss-Kronrod 21 quadrature with error estimate + """ + # Gauss-Kronrod points + x = (0.995657163025808080735527280689003, + 0.973906528517171720077964012084452, + 0.930157491355708226001207180059508, + 0.865063366688984510732096688423493, + 0.780817726586416897063717578345042, + 0.679409568299024406234327365114874, + 0.562757134668604683339000099272694, + 0.433395394129247190799265943165784, + 0.294392862701460198131126603103866, + 0.148874338981631210884826001129720, + 0, + -0.148874338981631210884826001129720, + -0.294392862701460198131126603103866, + -0.433395394129247190799265943165784, + -0.562757134668604683339000099272694, + -0.679409568299024406234327365114874, + -0.780817726586416897063717578345042, + -0.865063366688984510732096688423493, + -0.930157491355708226001207180059508, + -0.973906528517171720077964012084452, + -0.995657163025808080735527280689003) + + # 10-point weights + w = (0.066671344308688137593568809893332, + 0.149451349150580593145776339657697, + 0.219086362515982043995534934228163, + 0.269266719309996355091226921569469, + 0.295524224714752870173892994651338, + 0.295524224714752870173892994651338, + 0.269266719309996355091226921569469, + 0.219086362515982043995534934228163, + 0.149451349150580593145776339657697, + 0.066671344308688137593568809893332) + + # 21-point weights + v = (0.011694638867371874278064396062192, + 0.032558162307964727478818972459390, + 0.054755896574351996031381300244580, + 0.075039674810919952767043140916190, + 0.093125454583697605535065465083366, + 0.109387158802297641899210590325805, + 0.123491976262065851077958109831074, + 0.134709217311473325928054001771707, + 0.142775938577060080797094273138717, + 0.147739104901338491374841515972068, + 0.149445554002916905664936468389821, + 0.147739104901338491374841515972068, + 0.142775938577060080797094273138717, + 0.134709217311473325928054001771707, + 0.123491976262065851077958109831074, + 0.109387158802297641899210590325805, + 0.093125454583697605535065465083366, + 0.075039674810919952767043140916190, + 0.054755896574351996031381300244580, + 0.032558162307964727478818972459390, + 0.011694638867371874278064396062192) + + return _quadrature_gk(a, b, f, norm_func, x, w, v) + + +_quadrature_gk21.num_eval = 21 + + +def _quadrature_gk15(a, b, f, norm_func): + """ + Gauss-Kronrod 15 quadrature with error estimate + """ + # Gauss-Kronrod points + x = (0.991455371120812639206854697526329, + 0.949107912342758524526189684047851, + 0.864864423359769072789712788640926, + 0.741531185599394439863864773280788, + 0.586087235467691130294144838258730, + 0.405845151377397166906606412076961, + 0.207784955007898467600689403773245, + 0.000000000000000000000000000000000, + -0.207784955007898467600689403773245, + -0.405845151377397166906606412076961, + -0.586087235467691130294144838258730, + -0.741531185599394439863864773280788, + -0.864864423359769072789712788640926, + -0.949107912342758524526189684047851, + -0.991455371120812639206854697526329) + + # 7-point weights + w = (0.129484966168869693270611432679082, + 0.279705391489276667901467771423780, + 0.381830050505118944950369775488975, + 0.417959183673469387755102040816327, + 0.381830050505118944950369775488975, + 0.279705391489276667901467771423780, + 0.129484966168869693270611432679082) + + # 15-point weights + v = (0.022935322010529224963732008058970, + 0.063092092629978553290700663189204, + 0.104790010322250183839876322541518, + 0.140653259715525918745189590510238, + 0.169004726639267902826583426598550, + 0.190350578064785409913256402421014, + 0.204432940075298892414161999234649, + 0.209482141084727828012999174891714, + 0.204432940075298892414161999234649, + 0.190350578064785409913256402421014, + 0.169004726639267902826583426598550, + 0.140653259715525918745189590510238, + 0.104790010322250183839876322541518, + 0.063092092629978553290700663189204, + 0.022935322010529224963732008058970) + + return _quadrature_gk(a, b, f, norm_func, x, w, v) + + +_quadrature_gk15.num_eval = 15 diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_quadpack_py.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_quadpack_py.py new file mode 100644 index 0000000000000000000000000000000000000000..0d273f6d2c9943f4a35f9b0c761a944b7be84cfe --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_quadpack_py.py @@ -0,0 +1,1279 @@ +# Author: Travis Oliphant 2001 +# Author: Nathan Woods 2013 (nquad &c) +import sys +import warnings +from functools import partial + +from . import _quadpack +import numpy as np + +__all__ = ["quad", "dblquad", "tplquad", "nquad", "IntegrationWarning"] + + +class IntegrationWarning(UserWarning): + """ + Warning on issues during integration. + """ + pass + + +def quad(func, a, b, args=(), full_output=0, epsabs=1.49e-8, epsrel=1.49e-8, + limit=50, points=None, weight=None, wvar=None, wopts=None, maxp1=50, + limlst=50, complex_func=False): + """ + Compute a definite integral. + + Integrate func from `a` to `b` (possibly infinite interval) using a + technique from the Fortran library QUADPACK. + + Parameters + ---------- + func : {function, scipy.LowLevelCallable} + A Python function or method to integrate. If `func` takes many + arguments, it is integrated along the axis corresponding to the + first argument. + + If the user desires improved integration performance, then `f` may + be a `scipy.LowLevelCallable` with one of the signatures:: + + double func(double x) + double func(double x, void *user_data) + double func(int n, double *xx) + double func(int n, double *xx, void *user_data) + + The ``user_data`` is the data contained in the `scipy.LowLevelCallable`. + In the call forms with ``xx``, ``n`` is the length of the ``xx`` + array which contains ``xx[0] == x`` and the rest of the items are + numbers contained in the ``args`` argument of quad. + + In addition, certain ctypes call signatures are supported for + backward compatibility, but those should not be used in new code. + a : float + Lower limit of integration (use -numpy.inf for -infinity). + b : float + Upper limit of integration (use numpy.inf for +infinity). + args : tuple, optional + Extra arguments to pass to `func`. + full_output : int, optional + Non-zero to return a dictionary of integration information. + If non-zero, warning messages are also suppressed and the + message is appended to the output tuple. + complex_func : bool, optional + Indicate if the function's (`func`) return type is real + (``complex_func=False``: default) or complex (``complex_func=True``). + In both cases, the function's argument is real. + If full_output is also non-zero, the `infodict`, `message`, and + `explain` for the real and complex components are returned in + a dictionary with keys "real output" and "imag output". + + Returns + ------- + y : float + The integral of func from `a` to `b`. + abserr : float + An estimate of the absolute error in the result. + infodict : dict + A dictionary containing additional information. + message + A convergence message. + explain + Appended only with 'cos' or 'sin' weighting and infinite + integration limits, it contains an explanation of the codes in + infodict['ierlst'] + + Other Parameters + ---------------- + epsabs : float or int, optional + Absolute error tolerance. Default is 1.49e-8. `quad` tries to obtain + an accuracy of ``abs(i-result) <= max(epsabs, epsrel*abs(i))`` + where ``i`` = integral of `func` from `a` to `b`, and ``result`` is the + numerical approximation. See `epsrel` below. + epsrel : float or int, optional + Relative error tolerance. Default is 1.49e-8. + If ``epsabs <= 0``, `epsrel` must be greater than both 5e-29 + and ``50 * (machine epsilon)``. See `epsabs` above. + limit : float or int, optional + An upper bound on the number of subintervals used in the adaptive + algorithm. + points : (sequence of floats,ints), optional + A sequence of break points in the bounded integration interval + where local difficulties of the integrand may occur (e.g., + singularities, discontinuities). The sequence does not have + to be sorted. Note that this option cannot be used in conjunction + with ``weight``. + weight : float or int, optional + String indicating weighting function. Full explanation for this + and the remaining arguments can be found below. + wvar : optional + Variables for use with weighting functions. + wopts : optional + Optional input for reusing Chebyshev moments. + maxp1 : float or int, optional + An upper bound on the number of Chebyshev moments. + limlst : int, optional + Upper bound on the number of cycles (>=3) for use with a sinusoidal + weighting and an infinite end-point. + + See Also + -------- + dblquad : double integral + tplquad : triple integral + nquad : n-dimensional integrals (uses `quad` recursively) + fixed_quad : fixed-order Gaussian quadrature + simpson : integrator for sampled data + romb : integrator for sampled data + scipy.special : for coefficients and roots of orthogonal polynomials + + Notes + ----- + For valid results, the integral must converge; behavior for divergent + integrals is not guaranteed. + + **Extra information for quad() inputs and outputs** + + If full_output is non-zero, then the third output argument + (infodict) is a dictionary with entries as tabulated below. For + infinite limits, the range is transformed to (0,1) and the + optional outputs are given with respect to this transformed range. + Let M be the input argument limit and let K be infodict['last']. + The entries are: + + 'neval' + The number of function evaluations. + 'last' + The number, K, of subintervals produced in the subdivision process. + 'alist' + A rank-1 array of length M, the first K elements of which are the + left end points of the subintervals in the partition of the + integration range. + 'blist' + A rank-1 array of length M, the first K elements of which are the + right end points of the subintervals. + 'rlist' + A rank-1 array of length M, the first K elements of which are the + integral approximations on the subintervals. + 'elist' + A rank-1 array of length M, the first K elements of which are the + moduli of the absolute error estimates on the subintervals. + 'iord' + A rank-1 integer array of length M, the first L elements of + which are pointers to the error estimates over the subintervals + with ``L=K`` if ``K<=M/2+2`` or ``L=M+1-K`` otherwise. Let I be the + sequence ``infodict['iord']`` and let E be the sequence + ``infodict['elist']``. Then ``E[I[1]], ..., E[I[L]]`` forms a + decreasing sequence. + + If the input argument points is provided (i.e., it is not None), + the following additional outputs are placed in the output + dictionary. Assume the points sequence is of length P. + + 'pts' + A rank-1 array of length P+2 containing the integration limits + and the break points of the intervals in ascending order. + This is an array giving the subintervals over which integration + will occur. + 'level' + A rank-1 integer array of length M (=limit), containing the + subdivision levels of the subintervals, i.e., if (aa,bb) is a + subinterval of ``(pts[1], pts[2])`` where ``pts[0]`` and ``pts[2]`` + are adjacent elements of ``infodict['pts']``, then (aa,bb) has level l + if ``|bb-aa| = |pts[2]-pts[1]| * 2**(-l)``. + 'ndin' + A rank-1 integer array of length P+2. After the first integration + over the intervals (pts[1], pts[2]), the error estimates over some + of the intervals may have been increased artificially in order to + put their subdivision forward. This array has ones in slots + corresponding to the subintervals for which this happens. + + **Weighting the integrand** + + The input variables, *weight* and *wvar*, are used to weight the + integrand by a select list of functions. Different integration + methods are used to compute the integral with these weighting + functions, and these do not support specifying break points. The + possible values of weight and the corresponding weighting functions are. + + ========== =================================== ===================== + ``weight`` Weight function used ``wvar`` + ========== =================================== ===================== + 'cos' cos(w*x) wvar = w + 'sin' sin(w*x) wvar = w + 'alg' g(x) = ((x-a)**alpha)*((b-x)**beta) wvar = (alpha, beta) + 'alg-loga' g(x)*log(x-a) wvar = (alpha, beta) + 'alg-logb' g(x)*log(b-x) wvar = (alpha, beta) + 'alg-log' g(x)*log(x-a)*log(b-x) wvar = (alpha, beta) + 'cauchy' 1/(x-c) wvar = c + ========== =================================== ===================== + + wvar holds the parameter w, (alpha, beta), or c depending on the weight + selected. In these expressions, a and b are the integration limits. + + For the 'cos' and 'sin' weighting, additional inputs and outputs are + available. + + For finite integration limits, the integration is performed using a + Clenshaw-Curtis method which uses Chebyshev moments. For repeated + calculations, these moments are saved in the output dictionary: + + 'momcom' + The maximum level of Chebyshev moments that have been computed, + i.e., if ``M_c`` is ``infodict['momcom']`` then the moments have been + computed for intervals of length ``|b-a| * 2**(-l)``, + ``l=0,1,...,M_c``. + 'nnlog' + A rank-1 integer array of length M(=limit), containing the + subdivision levels of the subintervals, i.e., an element of this + array is equal to l if the corresponding subinterval is + ``|b-a|* 2**(-l)``. + 'chebmo' + A rank-2 array of shape (25, maxp1) containing the computed + Chebyshev moments. These can be passed on to an integration + over the same interval by passing this array as the second + element of the sequence wopts and passing infodict['momcom'] as + the first element. + + If one of the integration limits is infinite, then a Fourier integral is + computed (assuming w neq 0). If full_output is 1 and a numerical error + is encountered, besides the error message attached to the output tuple, + a dictionary is also appended to the output tuple which translates the + error codes in the array ``info['ierlst']`` to English messages. The + output information dictionary contains the following entries instead of + 'last', 'alist', 'blist', 'rlist', and 'elist': + + 'lst' + The number of subintervals needed for the integration (call it ``K_f``). + 'rslst' + A rank-1 array of length M_f=limlst, whose first ``K_f`` elements + contain the integral contribution over the interval + ``(a+(k-1)c, a+kc)`` where ``c = (2*floor(|w|) + 1) * pi / |w|`` + and ``k=1,2,...,K_f``. + 'erlst' + A rank-1 array of length ``M_f`` containing the error estimate + corresponding to the interval in the same position in + ``infodict['rslist']``. + 'ierlst' + A rank-1 integer array of length ``M_f`` containing an error flag + corresponding to the interval in the same position in + ``infodict['rslist']``. See the explanation dictionary (last entry + in the output tuple) for the meaning of the codes. + + + **Details of QUADPACK level routines** + + `quad` calls routines from the FORTRAN library QUADPACK. This section + provides details on the conditions for each routine to be called and a + short description of each routine. The routine called depends on + `weight`, `points` and the integration limits `a` and `b`. + + ================ ============== ========== ===================== + QUADPACK routine `weight` `points` infinite bounds + ================ ============== ========== ===================== + qagse None No No + qagie None No Yes + qagpe None Yes No + qawoe 'sin', 'cos' No No + qawfe 'sin', 'cos' No either `a` or `b` + qawse 'alg*' No No + qawce 'cauchy' No No + ================ ============== ========== ===================== + + The following provides a short description from [1]_ for each + routine. + + qagse + is an integrator based on globally adaptive interval + subdivision in connection with extrapolation, which will + eliminate the effects of integrand singularities of + several types. + qagie + handles integration over infinite intervals. The infinite range is + mapped onto a finite interval and subsequently the same strategy as + in ``QAGS`` is applied. + qagpe + serves the same purposes as QAGS, but also allows the + user to provide explicit information about the location + and type of trouble-spots i.e. the abscissae of internal + singularities, discontinuities and other difficulties of + the integrand function. + qawoe + is an integrator for the evaluation of + :math:`\\int^b_a \\cos(\\omega x)f(x)dx` or + :math:`\\int^b_a \\sin(\\omega x)f(x)dx` + over a finite interval [a,b], where :math:`\\omega` and :math:`f` + are specified by the user. The rule evaluation component is based + on the modified Clenshaw-Curtis technique + + An adaptive subdivision scheme is used in connection + with an extrapolation procedure, which is a modification + of that in ``QAGS`` and allows the algorithm to deal with + singularities in :math:`f(x)`. + qawfe + calculates the Fourier transform + :math:`\\int^\\infty_a \\cos(\\omega x)f(x)dx` or + :math:`\\int^\\infty_a \\sin(\\omega x)f(x)dx` + for user-provided :math:`\\omega` and :math:`f`. The procedure of + ``QAWO`` is applied on successive finite intervals, and convergence + acceleration by means of the :math:`\\varepsilon`-algorithm is applied + to the series of integral approximations. + qawse + approximate :math:`\\int^b_a w(x)f(x)dx`, with :math:`a < b` where + :math:`w(x) = (x-a)^{\\alpha}(b-x)^{\\beta}v(x)` with + :math:`\\alpha,\\beta > -1`, where :math:`v(x)` may be one of the + following functions: :math:`1`, :math:`\\log(x-a)`, :math:`\\log(b-x)`, + :math:`\\log(x-a)\\log(b-x)`. + + The user specifies :math:`\\alpha`, :math:`\\beta` and the type of the + function :math:`v`. A globally adaptive subdivision strategy is + applied, with modified Clenshaw-Curtis integration on those + subintervals which contain `a` or `b`. + qawce + compute :math:`\\int^b_a f(x) / (x-c)dx` where the integral must be + interpreted as a Cauchy principal value integral, for user specified + :math:`c` and :math:`f`. The strategy is globally adaptive. Modified + Clenshaw-Curtis integration is used on those intervals containing the + point :math:`x = c`. + + **Integration of Complex Function of a Real Variable** + + A complex valued function, :math:`f`, of a real variable can be written as + :math:`f = g + ih`. Similarly, the integral of :math:`f` can be + written as + + .. math:: + \\int_a^b f(x) dx = \\int_a^b g(x) dx + i\\int_a^b h(x) dx + + assuming that the integrals of :math:`g` and :math:`h` exist + over the interval :math:`[a,b]` [2]_. Therefore, ``quad`` integrates + complex-valued functions by integrating the real and imaginary components + separately. + + + References + ---------- + + .. [1] Piessens, Robert; de Doncker-Kapenga, Elise; + Überhuber, Christoph W.; Kahaner, David (1983). + QUADPACK: A subroutine package for automatic integration. + Springer-Verlag. + ISBN 978-3-540-12553-2. + + .. [2] McCullough, Thomas; Phillips, Keith (1973). + Foundations of Analysis in the Complex Plane. + Holt Rinehart Winston. + ISBN 0-03-086370-8 + + Examples + -------- + Calculate :math:`\\int^4_0 x^2 dx` and compare with an analytic result + + >>> from scipy import integrate + >>> import numpy as np + >>> x2 = lambda x: x**2 + >>> integrate.quad(x2, 0, 4) + (21.333333333333332, 2.3684757858670003e-13) + >>> print(4**3 / 3.) # analytical result + 21.3333333333 + + Calculate :math:`\\int^\\infty_0 e^{-x} dx` + + >>> invexp = lambda x: np.exp(-x) + >>> integrate.quad(invexp, 0, np.inf) + (1.0, 5.842605999138044e-11) + + Calculate :math:`\\int^1_0 a x \\,dx` for :math:`a = 1, 3` + + >>> f = lambda x, a: a*x + >>> y, err = integrate.quad(f, 0, 1, args=(1,)) + >>> y + 0.5 + >>> y, err = integrate.quad(f, 0, 1, args=(3,)) + >>> y + 1.5 + + Calculate :math:`\\int^1_0 x^2 + y^2 dx` with ctypes, holding + y parameter as 1:: + + testlib.c => + double func(int n, double args[n]){ + return args[0]*args[0] + args[1]*args[1];} + compile to library testlib.* + + :: + + from scipy import integrate + import ctypes + lib = ctypes.CDLL('/home/.../testlib.*') #use absolute path + lib.func.restype = ctypes.c_double + lib.func.argtypes = (ctypes.c_int,ctypes.c_double) + integrate.quad(lib.func,0,1,(1)) + #(1.3333333333333333, 1.4802973661668752e-14) + print((1.0**3/3.0 + 1.0) - (0.0**3/3.0 + 0.0)) #Analytic result + # 1.3333333333333333 + + Be aware that pulse shapes and other sharp features as compared to the + size of the integration interval may not be integrated correctly using + this method. A simplified example of this limitation is integrating a + y-axis reflected step function with many zero values within the integrals + bounds. + + >>> y = lambda x: 1 if x<=0 else 0 + >>> integrate.quad(y, -1, 1) + (1.0, 1.1102230246251565e-14) + >>> integrate.quad(y, -1, 100) + (1.0000000002199108, 1.0189464580163188e-08) + >>> integrate.quad(y, -1, 10000) + (0.0, 0.0) + + """ + if not isinstance(args, tuple): + args = (args,) + + # check the limits of integration: \int_a^b, expect a < b + flip, a, b = b < a, min(a, b), max(a, b) + + if complex_func: + def imfunc(x, *args): + return func(x, *args).imag + + def refunc(x, *args): + return func(x, *args).real + + re_retval = quad(refunc, a, b, args, full_output, epsabs, + epsrel, limit, points, weight, wvar, wopts, + maxp1, limlst, complex_func=False) + im_retval = quad(imfunc, a, b, args, full_output, epsabs, + epsrel, limit, points, weight, wvar, wopts, + maxp1, limlst, complex_func=False) + integral = re_retval[0] + 1j*im_retval[0] + error_estimate = re_retval[1] + 1j*im_retval[1] + retval = integral, error_estimate + if full_output: + msgexp = {} + msgexp["real"] = re_retval[2:] + msgexp["imag"] = im_retval[2:] + retval = retval + (msgexp,) + + return retval + + if weight is None: + retval = _quad(func, a, b, args, full_output, epsabs, epsrel, limit, + points) + else: + if points is not None: + msg = ("Break points cannot be specified when using weighted integrand.\n" + "Continuing, ignoring specified points.") + warnings.warn(msg, IntegrationWarning, stacklevel=2) + retval = _quad_weight(func, a, b, args, full_output, epsabs, epsrel, + limlst, limit, maxp1, weight, wvar, wopts) + + if flip: + retval = (-retval[0],) + retval[1:] + + ier = retval[-1] + if ier == 0: + return retval[:-1] + + msgs = {80: "A Python error occurred possibly while calling the function.", + 1: f"The maximum number of subdivisions ({limit}) has been achieved.\n " + f"If increasing the limit yields no improvement it is advised to " + f"analyze \n the integrand in order to determine the difficulties. " + f"If the position of a \n local difficulty can be determined " + f"(singularity, discontinuity) one will \n probably gain from " + f"splitting up the interval and calling the integrator \n on the " + f"subranges. Perhaps a special-purpose integrator should be used.", + 2: "The occurrence of roundoff error is detected, which prevents \n " + "the requested tolerance from being achieved. " + "The error may be \n underestimated.", + 3: "Extremely bad integrand behavior occurs at some points of the\n " + "integration interval.", + 4: "The algorithm does not converge. Roundoff error is detected\n " + "in the extrapolation table. It is assumed that the requested " + "tolerance\n cannot be achieved, and that the returned result " + "(if full_output = 1) is \n the best which can be obtained.", + 5: "The integral is probably divergent, or slowly convergent.", + 6: "The input is invalid.", + 7: "Abnormal termination of the routine. The estimates for result\n " + "and error are less reliable. It is assumed that the requested " + "accuracy\n has not been achieved.", + 'unknown': "Unknown error."} + + if weight in ['cos','sin'] and (b == np.inf or a == -np.inf): + msgs[1] = ( + "The maximum number of cycles allowed has been achieved., e.e.\n of " + "subintervals (a+(k-1)c, a+kc) where c = (2*int(abs(omega)+1))\n " + "*pi/abs(omega), for k = 1, 2, ..., lst. " + "One can allow more cycles by increasing the value of limlst. " + "Look at info['ierlst'] with full_output=1." + ) + msgs[4] = ( + "The extrapolation table constructed for convergence acceleration\n of " + "the series formed by the integral contributions over the cycles, \n does " + "not converge to within the requested accuracy. " + "Look at \n info['ierlst'] with full_output=1." + ) + msgs[7] = ( + "Bad integrand behavior occurs within one or more of the cycles.\n " + "Location and type of the difficulty involved can be determined from \n " + "the vector info['ierlist'] obtained with full_output=1." + ) + explain = {1: "The maximum number of subdivisions (= limit) has been \n " + "achieved on this cycle.", + 2: "The occurrence of roundoff error is detected and prevents\n " + "the tolerance imposed on this cycle from being achieved.", + 3: "Extremely bad integrand behavior occurs at some points of\n " + "this cycle.", + 4: "The integral over this cycle does not converge (to within the " + "required accuracy) due to roundoff in the extrapolation " + "procedure invoked on this cycle. It is assumed that the result " + "on this interval is the best which can be obtained.", + 5: "The integral over this cycle is probably divergent or " + "slowly convergent."} + + try: + msg = msgs[ier] + except KeyError: + msg = msgs['unknown'] + + if ier in [1,2,3,4,5,7]: + if full_output: + if weight in ['cos', 'sin'] and (b == np.inf or a == -np.inf): + return retval[:-1] + (msg, explain) + else: + return retval[:-1] + (msg,) + else: + warnings.warn(msg, IntegrationWarning, stacklevel=2) + return retval[:-1] + + elif ier == 6: # Forensic decision tree when QUADPACK throws ier=6 + if epsabs <= 0: # Small error tolerance - applies to all methods + if epsrel < max(50 * sys.float_info.epsilon, 5e-29): + msg = ("If 'epsabs'<=0, 'epsrel' must be greater than both" + " 5e-29 and 50*(machine epsilon).") + elif weight in ['sin', 'cos'] and (abs(a) + abs(b) == np.inf): + msg = ("Sine or cosine weighted integrals with infinite domain" + " must have 'epsabs'>0.") + + elif weight is None: + if points is None: # QAGSE/QAGIE + msg = ("Invalid 'limit' argument. There must be" + " at least one subinterval") + else: # QAGPE + if not (min(a, b) <= min(points) <= max(points) <= max(a, b)): + msg = ("All break points in 'points' must lie within the" + " integration limits.") + elif len(points) >= limit: + msg = (f"Number of break points ({len(points):d}) " + f"must be less than subinterval limit ({limit:d})") + + else: + if maxp1 < 1: + msg = "Chebyshev moment limit maxp1 must be >=1." + + elif weight in ('cos', 'sin') and abs(a+b) == np.inf: # QAWFE + msg = "Cycle limit limlst must be >=3." + + elif weight.startswith('alg'): # QAWSE + if min(wvar) < -1: + msg = "wvar parameters (alpha, beta) must both be >= -1." + if b < a: + msg = "Integration limits a, b must satistfy a>> import numpy as np + >>> from scipy import integrate + >>> f = lambda y, x: x*y**2 + >>> integrate.dblquad(f, 0, 2, 0, 1) + (0.6666666666666667, 7.401486830834377e-15) + + Calculate :math:`\\int^{x=\\pi/4}_{x=0} \\int^{y=\\cos(x)}_{y=\\sin(x)} 1 + \\,dy \\,dx`. + + >>> f = lambda y, x: 1 + >>> integrate.dblquad(f, 0, np.pi/4, np.sin, np.cos) + (0.41421356237309503, 1.1083280054755938e-14) + + Calculate :math:`\\int^{x=1}_{x=0} \\int^{y=2-x}_{y=x} a x y \\,dy \\,dx` + for :math:`a=1, 3`. + + >>> f = lambda y, x, a: a*x*y + >>> integrate.dblquad(f, 0, 1, lambda x: x, lambda x: 2-x, args=(1,)) + (0.33333333333333337, 5.551115123125783e-15) + >>> integrate.dblquad(f, 0, 1, lambda x: x, lambda x: 2-x, args=(3,)) + (0.9999999999999999, 1.6653345369377348e-14) + + Compute the two-dimensional Gaussian Integral, which is the integral of the + Gaussian function :math:`f(x,y) = e^{-(x^{2} + y^{2})}`, over + :math:`(-\\infty,+\\infty)`. That is, compute the integral + :math:`\\iint^{+\\infty}_{-\\infty} e^{-(x^{2} + y^{2})} \\,dy\\,dx`. + + >>> f = lambda x, y: np.exp(-(x ** 2 + y ** 2)) + >>> integrate.dblquad(f, -np.inf, np.inf, -np.inf, np.inf) + (3.141592653589777, 2.5173086737433208e-08) + + """ + + def temp_ranges(*args): + return [gfun(args[0]) if callable(gfun) else gfun, + hfun(args[0]) if callable(hfun) else hfun] + + return nquad(func, [temp_ranges, [a, b]], args=args, + opts={"epsabs": epsabs, "epsrel": epsrel}) + + +def tplquad(func, a, b, gfun, hfun, qfun, rfun, args=(), epsabs=1.49e-8, + epsrel=1.49e-8): + """ + Compute a triple (definite) integral. + + Return the triple integral of ``func(z, y, x)`` from ``x = a..b``, + ``y = gfun(x)..hfun(x)``, and ``z = qfun(x,y)..rfun(x,y)``. + + Parameters + ---------- + func : function + A Python function or method of at least three variables in the + order (z, y, x). + a, b : float + The limits of integration in x: `a` < `b` + gfun : function or float + The lower boundary curve in y which is a function taking a single + floating point argument (x) and returning a floating point result + or a float indicating a constant boundary curve. + hfun : function or float + The upper boundary curve in y (same requirements as `gfun`). + qfun : function or float + The lower boundary surface in z. It must be a function that takes + two floats in the order (x, y) and returns a float or a float + indicating a constant boundary surface. + rfun : function or float + The upper boundary surface in z. (Same requirements as `qfun`.) + args : tuple, optional + Extra arguments to pass to `func`. + epsabs : float, optional + Absolute tolerance passed directly to the innermost 1-D quadrature + integration. Default is 1.49e-8. + epsrel : float, optional + Relative tolerance of the innermost 1-D integrals. Default is 1.49e-8. + + Returns + ------- + y : float + The resultant integral. + abserr : float + An estimate of the error. + + See Also + -------- + quad : Adaptive quadrature using QUADPACK + fixed_quad : Fixed-order Gaussian quadrature + dblquad : Double integrals + nquad : N-dimensional integrals + romb : Integrators for sampled data + simpson : Integrators for sampled data + scipy.special : For coefficients and roots of orthogonal polynomials + + Notes + ----- + For valid results, the integral must converge; behavior for divergent + integrals is not guaranteed. + + **Details of QUADPACK level routines** + + `quad` calls routines from the FORTRAN library QUADPACK. This section + provides details on the conditions for each routine to be called and a + short description of each routine. For each level of integration, ``qagse`` + is used for finite limits or ``qagie`` is used, if either limit (or both!) + are infinite. The following provides a short description from [1]_ for each + routine. + + qagse + is an integrator based on globally adaptive interval + subdivision in connection with extrapolation, which will + eliminate the effects of integrand singularities of + several types. + qagie + handles integration over infinite intervals. The infinite range is + mapped onto a finite interval and subsequently the same strategy as + in ``QAGS`` is applied. + + References + ---------- + + .. [1] Piessens, Robert; de Doncker-Kapenga, Elise; + Überhuber, Christoph W.; Kahaner, David (1983). + QUADPACK: A subroutine package for automatic integration. + Springer-Verlag. + ISBN 978-3-540-12553-2. + + Examples + -------- + Compute the triple integral of ``x * y * z``, over ``x`` ranging + from 1 to 2, ``y`` ranging from 2 to 3, ``z`` ranging from 0 to 1. + That is, :math:`\\int^{x=2}_{x=1} \\int^{y=3}_{y=2} \\int^{z=1}_{z=0} x y z + \\,dz \\,dy \\,dx`. + + >>> import numpy as np + >>> from scipy import integrate + >>> f = lambda z, y, x: x*y*z + >>> integrate.tplquad(f, 1, 2, 2, 3, 0, 1) + (1.8749999999999998, 3.3246447942574074e-14) + + Calculate :math:`\\int^{x=1}_{x=0} \\int^{y=1-2x}_{y=0} + \\int^{z=1-x-2y}_{z=0} x y z \\,dz \\,dy \\,dx`. + Note: `qfun`/`rfun` takes arguments in the order (x, y), even though ``f`` + takes arguments in the order (z, y, x). + + >>> f = lambda z, y, x: x*y*z + >>> integrate.tplquad(f, 0, 1, 0, lambda x: 1-2*x, 0, lambda x, y: 1-x-2*y) + (0.05416666666666668, 2.1774196738157757e-14) + + Calculate :math:`\\int^{x=1}_{x=0} \\int^{y=1}_{y=0} \\int^{z=1}_{z=0} + a x y z \\,dz \\,dy \\,dx` for :math:`a=1, 3`. + + >>> f = lambda z, y, x, a: a*x*y*z + >>> integrate.tplquad(f, 0, 1, 0, 1, 0, 1, args=(1,)) + (0.125, 5.527033708952211e-15) + >>> integrate.tplquad(f, 0, 1, 0, 1, 0, 1, args=(3,)) + (0.375, 1.6581101126856635e-14) + + Compute the three-dimensional Gaussian Integral, which is the integral of + the Gaussian function :math:`f(x,y,z) = e^{-(x^{2} + y^{2} + z^{2})}`, over + :math:`(-\\infty,+\\infty)`. That is, compute the integral + :math:`\\iiint^{+\\infty}_{-\\infty} e^{-(x^{2} + y^{2} + z^{2})} \\,dz + \\,dy\\,dx`. + + >>> f = lambda x, y, z: np.exp(-(x ** 2 + y ** 2 + z ** 2)) + >>> integrate.tplquad(f, -np.inf, np.inf, -np.inf, np.inf, -np.inf, np.inf) + (5.568327996830833, 4.4619078828029765e-08) + + """ + # f(z, y, x) + # qfun/rfun(x, y) + # gfun/hfun(x) + # nquad will hand (y, x, t0, ...) to ranges0 + # nquad will hand (x, t0, ...) to ranges1 + # Only qfun / rfun is different API... + + def ranges0(*args): + return [qfun(args[1], args[0]) if callable(qfun) else qfun, + rfun(args[1], args[0]) if callable(rfun) else rfun] + + def ranges1(*args): + return [gfun(args[0]) if callable(gfun) else gfun, + hfun(args[0]) if callable(hfun) else hfun] + + ranges = [ranges0, ranges1, [a, b]] + return nquad(func, ranges, args=args, + opts={"epsabs": epsabs, "epsrel": epsrel}) + + +def nquad(func, ranges, args=None, opts=None, full_output=False): + r""" + Integration over multiple variables. + + Wraps `quad` to enable integration over multiple variables. + Various options allow improved integration of discontinuous functions, as + well as the use of weighted integration, and generally finer control of the + integration process. + + Parameters + ---------- + func : {callable, scipy.LowLevelCallable} + The function to be integrated. Has arguments of ``x0, ... xn``, + ``t0, ... tm``, where integration is carried out over ``x0, ... xn``, + which must be floats. Where ``t0, ... tm`` are extra arguments + passed in args. + Function signature should be ``func(x0, x1, ..., xn, t0, t1, ..., tm)``. + Integration is carried out in order. That is, integration over ``x0`` + is the innermost integral, and ``xn`` is the outermost. + + If the user desires improved integration performance, then `f` may + be a `scipy.LowLevelCallable` with one of the signatures:: + + double func(int n, double *xx) + double func(int n, double *xx, void *user_data) + + where ``n`` is the number of variables and args. The ``xx`` array + contains the coordinates and extra arguments. ``user_data`` is the data + contained in the `scipy.LowLevelCallable`. + ranges : iterable object + Each element of ranges may be either a sequence of 2 numbers, or else + a callable that returns such a sequence. ``ranges[0]`` corresponds to + integration over x0, and so on. If an element of ranges is a callable, + then it will be called with all of the integration arguments available, + as well as any parametric arguments. e.g., if + ``func = f(x0, x1, x2, t0, t1)``, then ``ranges[0]`` may be defined as + either ``(a, b)`` or else as ``(a, b) = range0(x1, x2, t0, t1)``. + args : iterable object, optional + Additional arguments ``t0, ... tn``, required by ``func``, ``ranges``, + and ``opts``. + opts : iterable object or dict, optional + Options to be passed to `quad`. May be empty, a dict, or + a sequence of dicts or functions that return a dict. If empty, the + default options from scipy.integrate.quad are used. If a dict, the same + options are used for all levels of integraion. If a sequence, then each + element of the sequence corresponds to a particular integration. e.g., + ``opts[0]`` corresponds to integration over ``x0``, and so on. If a + callable, the signature must be the same as for ``ranges``. The + available options together with their default values are: + + - epsabs = 1.49e-08 + - epsrel = 1.49e-08 + - limit = 50 + - points = None + - weight = None + - wvar = None + - wopts = None + + For more information on these options, see `quad`. + + full_output : bool, optional + Partial implementation of ``full_output`` from scipy.integrate.quad. + The number of integrand function evaluations ``neval`` can be obtained + by setting ``full_output=True`` when calling nquad. + + Returns + ------- + result : float + The result of the integration. + abserr : float + The maximum of the estimates of the absolute error in the various + integration results. + out_dict : dict, optional + A dict containing additional information on the integration. + + See Also + -------- + quad : 1-D numerical integration + dblquad, tplquad : double and triple integrals + fixed_quad : fixed-order Gaussian quadrature + + Notes + ----- + For valid results, the integral must converge; behavior for divergent + integrals is not guaranteed. + + **Details of QUADPACK level routines** + + `nquad` calls routines from the FORTRAN library QUADPACK. This section + provides details on the conditions for each routine to be called and a + short description of each routine. The routine called depends on + `weight`, `points` and the integration limits `a` and `b`. + + ================ ============== ========== ===================== + QUADPACK routine `weight` `points` infinite bounds + ================ ============== ========== ===================== + qagse None No No + qagie None No Yes + qagpe None Yes No + qawoe 'sin', 'cos' No No + qawfe 'sin', 'cos' No either `a` or `b` + qawse 'alg*' No No + qawce 'cauchy' No No + ================ ============== ========== ===================== + + The following provides a short description from [1]_ for each + routine. + + qagse + is an integrator based on globally adaptive interval + subdivision in connection with extrapolation, which will + eliminate the effects of integrand singularities of + several types. + qagie + handles integration over infinite intervals. The infinite range is + mapped onto a finite interval and subsequently the same strategy as + in ``QAGS`` is applied. + qagpe + serves the same purposes as QAGS, but also allows the + user to provide explicit information about the location + and type of trouble-spots i.e. the abscissae of internal + singularities, discontinuities and other difficulties of + the integrand function. + qawoe + is an integrator for the evaluation of + :math:`\int^b_a \cos(\omega x)f(x)dx` or + :math:`\int^b_a \sin(\omega x)f(x)dx` + over a finite interval [a,b], where :math:`\omega` and :math:`f` + are specified by the user. The rule evaluation component is based + on the modified Clenshaw-Curtis technique + + An adaptive subdivision scheme is used in connection + with an extrapolation procedure, which is a modification + of that in ``QAGS`` and allows the algorithm to deal with + singularities in :math:`f(x)`. + qawfe + calculates the Fourier transform + :math:`\int^\infty_a \cos(\omega x)f(x)dx` or + :math:`\int^\infty_a \sin(\omega x)f(x)dx` + for user-provided :math:`\omega` and :math:`f`. The procedure of + ``QAWO`` is applied on successive finite intervals, and convergence + acceleration by means of the :math:`\varepsilon`-algorithm is applied + to the series of integral approximations. + qawse + approximate :math:`\int^b_a w(x)f(x)dx`, with :math:`a < b` where + :math:`w(x) = (x-a)^{\alpha}(b-x)^{\beta}v(x)` with + :math:`\alpha,\beta > -1`, where :math:`v(x)` may be one of the + following functions: :math:`1`, :math:`\log(x-a)`, :math:`\log(b-x)`, + :math:`\log(x-a)\log(b-x)`. + + The user specifies :math:`\alpha`, :math:`\beta` and the type of the + function :math:`v`. A globally adaptive subdivision strategy is + applied, with modified Clenshaw-Curtis integration on those + subintervals which contain `a` or `b`. + qawce + compute :math:`\int^b_a f(x) / (x-c)dx` where the integral must be + interpreted as a Cauchy principal value integral, for user specified + :math:`c` and :math:`f`. The strategy is globally adaptive. Modified + Clenshaw-Curtis integration is used on those intervals containing the + point :math:`x = c`. + + References + ---------- + + .. [1] Piessens, Robert; de Doncker-Kapenga, Elise; + Überhuber, Christoph W.; Kahaner, David (1983). + QUADPACK: A subroutine package for automatic integration. + Springer-Verlag. + ISBN 978-3-540-12553-2. + + Examples + -------- + Calculate + + .. math:: + + \int^{1}_{-0.15} \int^{0.8}_{0.13} \int^{1}_{-1} \int^{1}_{0} + f(x_0, x_1, x_2, x_3) \,dx_0 \,dx_1 \,dx_2 \,dx_3 , + + where + + .. math:: + + f(x_0, x_1, x_2, x_3) = \begin{cases} + x_0^2+x_1 x_2-x_3^3+ \sin{x_0}+1 & (x_0-0.2 x_3-0.5-0.25 x_1 > 0) \\ + x_0^2+x_1 x_2-x_3^3+ \sin{x_0}+0 & (x_0-0.2 x_3-0.5-0.25 x_1 \leq 0) + \end{cases} . + + >>> import numpy as np + >>> from scipy import integrate + >>> func = lambda x0,x1,x2,x3 : x0**2 + x1*x2 - x3**3 + np.sin(x0) + ( + ... 1 if (x0-.2*x3-.5-.25*x1>0) else 0) + >>> def opts0(*args, **kwargs): + ... return {'points':[0.2*args[2] + 0.5 + 0.25*args[0]]} + >>> integrate.nquad(func, [[0,1], [-1,1], [.13,.8], [-.15,1]], + ... opts=[opts0,{},{},{}], full_output=True) + (1.5267454070738633, 2.9437360001402324e-14, {'neval': 388962}) + + Calculate + + .. math:: + + \int^{t_0+t_1+1}_{t_0+t_1-1} + \int^{x_2+t_0^2 t_1^3+1}_{x_2+t_0^2 t_1^3-1} + \int^{t_0 x_1+t_1 x_2+1}_{t_0 x_1+t_1 x_2-1} + f(x_0,x_1, x_2,t_0,t_1) + \,dx_0 \,dx_1 \,dx_2, + + where + + .. math:: + + f(x_0, x_1, x_2, t_0, t_1) = \begin{cases} + x_0 x_2^2 + \sin{x_1}+2 & (x_0+t_1 x_1-t_0 > 0) \\ + x_0 x_2^2 +\sin{x_1}+1 & (x_0+t_1 x_1-t_0 \leq 0) + \end{cases} + + and :math:`(t_0, t_1) = (0, 1)` . + + >>> def func2(x0, x1, x2, t0, t1): + ... return x0*x2**2 + np.sin(x1) + 1 + (1 if x0+t1*x1-t0>0 else 0) + >>> def lim0(x1, x2, t0, t1): + ... return [t0*x1 + t1*x2 - 1, t0*x1 + t1*x2 + 1] + >>> def lim1(x2, t0, t1): + ... return [x2 + t0**2*t1**3 - 1, x2 + t0**2*t1**3 + 1] + >>> def lim2(t0, t1): + ... return [t0 + t1 - 1, t0 + t1 + 1] + >>> def opts0(x1, x2, t0, t1): + ... return {'points' : [t0 - t1*x1]} + >>> def opts1(x2, t0, t1): + ... return {} + >>> def opts2(t0, t1): + ... return {} + >>> integrate.nquad(func2, [lim0, lim1, lim2], args=(0,1), + ... opts=[opts0, opts1, opts2]) + (36.099919226771625, 1.8546948553373528e-07) + + """ + depth = len(ranges) + ranges = [rng if callable(rng) else _RangeFunc(rng) for rng in ranges] + if args is None: + args = () + if opts is None: + opts = [dict([])] * depth + + if isinstance(opts, dict): + opts = [_OptFunc(opts)] * depth + else: + opts = [opt if callable(opt) else _OptFunc(opt) for opt in opts] + return _NQuad(func, ranges, opts, full_output).integrate(*args) + + +class _RangeFunc: + def __init__(self, range_): + self.range_ = range_ + + def __call__(self, *args): + """Return stored value. + + *args needed because range_ can be float or func, and is called with + variable number of parameters. + """ + return self.range_ + + +class _OptFunc: + def __init__(self, opt): + self.opt = opt + + def __call__(self, *args): + """Return stored dict.""" + return self.opt + + +class _NQuad: + def __init__(self, func, ranges, opts, full_output): + self.abserr = 0 + self.func = func + self.ranges = ranges + self.opts = opts + self.maxdepth = len(ranges) + self.full_output = full_output + if self.full_output: + self.out_dict = {'neval': 0} + + def integrate(self, *args, **kwargs): + depth = kwargs.pop('depth', 0) + if kwargs: + raise ValueError('unexpected kwargs') + + # Get the integration range and options for this depth. + ind = -(depth + 1) + fn_range = self.ranges[ind] + low, high = fn_range(*args) + fn_opt = self.opts[ind] + opt = dict(fn_opt(*args)) + + if 'points' in opt: + opt['points'] = [x for x in opt['points'] if low <= x <= high] + if depth + 1 == self.maxdepth: + f = self.func + else: + f = partial(self.integrate, depth=depth+1) + quad_r = quad(f, low, high, args=args, full_output=self.full_output, + **opt) + value = quad_r[0] + abserr = quad_r[1] + if self.full_output: + infodict = quad_r[2] + # The 'neval' parameter in full_output returns the total + # number of times the integrand function was evaluated. + # Therefore, only the innermost integration loop counts. + if depth + 1 == self.maxdepth: + self.out_dict['neval'] += infodict['neval'] + self.abserr = max(self.abserr, abserr) + if depth > 0: + return value + else: + # Final result of N-D integration with error + if self.full_output: + return value, self.abserr, self.out_dict + else: + return value, self.abserr diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_quadrature.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_quadrature.py new file mode 100644 index 0000000000000000000000000000000000000000..44cf10b32335014cd7cb26459c8dc89ac8f851ff --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_quadrature.py @@ -0,0 +1,1336 @@ +import numpy as np +import numpy.typing as npt +import math +import warnings +from collections import namedtuple +from collections.abc import Callable + +from scipy.special import roots_legendre +from scipy.special import gammaln, logsumexp +from scipy._lib._util import _rng_spawn +from scipy._lib._array_api import _asarray, array_namespace, xp_broadcast_promote + + +__all__ = ['fixed_quad', 'romb', + 'trapezoid', 'simpson', + 'cumulative_trapezoid', 'newton_cotes', + 'qmc_quad', 'cumulative_simpson'] + + +def trapezoid(y, x=None, dx=1.0, axis=-1): + r""" + Integrate along the given axis using the composite trapezoidal rule. + + If `x` is provided, the integration happens in sequence along its + elements - they are not sorted. + + Integrate `y` (`x`) along each 1d slice on the given axis, compute + :math:`\int y(x) dx`. + When `x` is specified, this integrates along the parametric curve, + computing :math:`\int_t y(t) dt = + \int_t y(t) \left.\frac{dx}{dt}\right|_{x=x(t)} dt`. + + Parameters + ---------- + y : array_like + Input array to integrate. + x : array_like, optional + The sample points corresponding to the `y` values. If `x` is None, + the sample points are assumed to be evenly spaced `dx` apart. The + default is None. + dx : scalar, optional + The spacing between sample points when `x` is None. The default is 1. + axis : int, optional + The axis along which to integrate. The default is the last axis. + + Returns + ------- + trapezoid : float or ndarray + Definite integral of `y` = n-dimensional array as approximated along + a single axis by the trapezoidal rule. If `y` is a 1-dimensional array, + then the result is a float. If `n` is greater than 1, then the result + is an `n`-1 dimensional array. + + See Also + -------- + cumulative_trapezoid, simpson, romb + + Notes + ----- + Image [2]_ illustrates trapezoidal rule -- y-axis locations of points + will be taken from `y` array, by default x-axis distances between + points will be 1.0, alternatively they can be provided with `x` array + or with `dx` scalar. Return value will be equal to combined area under + the red lines. + + References + ---------- + .. [1] Wikipedia page: https://en.wikipedia.org/wiki/Trapezoidal_rule + + .. [2] Illustration image: + https://en.wikipedia.org/wiki/File:Composite_trapezoidal_rule_illustration.png + + Examples + -------- + Use the trapezoidal rule on evenly spaced points: + + >>> import numpy as np + >>> from scipy import integrate + >>> integrate.trapezoid([1, 2, 3]) + 4.0 + + The spacing between sample points can be selected by either the + ``x`` or ``dx`` arguments: + + >>> integrate.trapezoid([1, 2, 3], x=[4, 6, 8]) + 8.0 + >>> integrate.trapezoid([1, 2, 3], dx=2) + 8.0 + + Using a decreasing ``x`` corresponds to integrating in reverse: + + >>> integrate.trapezoid([1, 2, 3], x=[8, 6, 4]) + -8.0 + + More generally ``x`` is used to integrate along a parametric curve. We can + estimate the integral :math:`\int_0^1 x^2 = 1/3` using: + + >>> x = np.linspace(0, 1, num=50) + >>> y = x**2 + >>> integrate.trapezoid(y, x) + 0.33340274885464394 + + Or estimate the area of a circle, noting we repeat the sample which closes + the curve: + + >>> theta = np.linspace(0, 2 * np.pi, num=1000, endpoint=True) + >>> integrate.trapezoid(np.cos(theta), x=np.sin(theta)) + 3.141571941375841 + + ``trapezoid`` can be applied along a specified axis to do multiple + computations in one call: + + >>> a = np.arange(6).reshape(2, 3) + >>> a + array([[0, 1, 2], + [3, 4, 5]]) + >>> integrate.trapezoid(a, axis=0) + array([1.5, 2.5, 3.5]) + >>> integrate.trapezoid(a, axis=1) + array([2., 8.]) + """ + xp = array_namespace(y) + y = _asarray(y, xp=xp, subok=True) + # Cannot just use the broadcasted arrays that are returned + # because trapezoid does not follow normal broadcasting rules + # cf. https://github.com/scipy/scipy/pull/21524#issuecomment-2354105942 + result_dtype = xp_broadcast_promote(y, force_floating=True, xp=xp)[0].dtype + nd = y.ndim + slice1 = [slice(None)]*nd + slice2 = [slice(None)]*nd + slice1[axis] = slice(1, None) + slice2[axis] = slice(None, -1) + if x is None: + d = dx + else: + x = _asarray(x, xp=xp, subok=True) + if x.ndim == 1: + d = x[1:] - x[:-1] + # make d broadcastable to y + slice3 = [None] * nd + slice3[axis] = slice(None) + d = d[tuple(slice3)] + else: + # if x is n-D it should be broadcastable to y + x = xp.broadcast_to(x, y.shape) + d = x[tuple(slice1)] - x[tuple(slice2)] + try: + ret = xp.sum( + d * (y[tuple(slice1)] + y[tuple(slice2)]) / 2.0, + axis=axis, dtype=result_dtype + ) + except ValueError: + # Operations didn't work, cast to ndarray + d = xp.asarray(d) + y = xp.asarray(y) + ret = xp.sum( + d * (y[tuple(slice1)] + y[tuple(slice2)]) / 2.0, + axis=axis, dtype=result_dtype + ) + return ret + + +def _cached_roots_legendre(n): + """ + Cache roots_legendre results to speed up calls of the fixed_quad + function. + """ + if n in _cached_roots_legendre.cache: + return _cached_roots_legendre.cache[n] + + _cached_roots_legendre.cache[n] = roots_legendre(n) + return _cached_roots_legendre.cache[n] + + +_cached_roots_legendre.cache = dict() + + +def fixed_quad(func, a, b, args=(), n=5): + """ + Compute a definite integral using fixed-order Gaussian quadrature. + + Integrate `func` from `a` to `b` using Gaussian quadrature of + order `n`. + + Parameters + ---------- + func : callable + A Python function or method to integrate (must accept vector inputs). + If integrating a vector-valued function, the returned array must have + shape ``(..., len(x))``. + a : float + Lower limit of integration. + b : float + Upper limit of integration. + args : tuple, optional + Extra arguments to pass to function, if any. + n : int, optional + Order of quadrature integration. Default is 5. + + Returns + ------- + val : float + Gaussian quadrature approximation to the integral + none : None + Statically returned value of None + + See Also + -------- + quad : adaptive quadrature using QUADPACK + dblquad : double integrals + tplquad : triple integrals + romb : integrators for sampled data + simpson : integrators for sampled data + cumulative_trapezoid : cumulative integration for sampled data + + Examples + -------- + >>> from scipy import integrate + >>> import numpy as np + >>> f = lambda x: x**8 + >>> integrate.fixed_quad(f, 0.0, 1.0, n=4) + (0.1110884353741496, None) + >>> integrate.fixed_quad(f, 0.0, 1.0, n=5) + (0.11111111111111102, None) + >>> print(1/9.0) # analytical result + 0.1111111111111111 + + >>> integrate.fixed_quad(np.cos, 0.0, np.pi/2, n=4) + (0.9999999771971152, None) + >>> integrate.fixed_quad(np.cos, 0.0, np.pi/2, n=5) + (1.000000000039565, None) + >>> np.sin(np.pi/2)-np.sin(0) # analytical result + 1.0 + + """ + x, w = _cached_roots_legendre(n) + x = np.real(x) + if np.isinf(a) or np.isinf(b): + raise ValueError("Gaussian quadrature is only available for " + "finite limits.") + y = (b-a)*(x+1)/2.0 + a + return (b-a)/2.0 * np.sum(w*func(y, *args), axis=-1), None + + +def tupleset(t, i, value): + l = list(t) + l[i] = value + return tuple(l) + + +def cumulative_trapezoid(y, x=None, dx=1.0, axis=-1, initial=None): + """ + Cumulatively integrate y(x) using the composite trapezoidal rule. + + Parameters + ---------- + y : array_like + Values to integrate. + x : array_like, optional + The coordinate to integrate along. If None (default), use spacing `dx` + between consecutive elements in `y`. + dx : float, optional + Spacing between elements of `y`. Only used if `x` is None. + axis : int, optional + Specifies the axis to cumulate. Default is -1 (last axis). + initial : scalar, optional + If given, insert this value at the beginning of the returned result. + 0 or None are the only values accepted. Default is None, which means + `res` has one element less than `y` along the axis of integration. + + Returns + ------- + res : ndarray + The result of cumulative integration of `y` along `axis`. + If `initial` is None, the shape is such that the axis of integration + has one less value than `y`. If `initial` is given, the shape is equal + to that of `y`. + + See Also + -------- + numpy.cumsum, numpy.cumprod + cumulative_simpson : cumulative integration using Simpson's 1/3 rule + quad : adaptive quadrature using QUADPACK + fixed_quad : fixed-order Gaussian quadrature + dblquad : double integrals + tplquad : triple integrals + romb : integrators for sampled data + + Examples + -------- + >>> from scipy import integrate + >>> import numpy as np + >>> import matplotlib.pyplot as plt + + >>> x = np.linspace(-2, 2, num=20) + >>> y = x + >>> y_int = integrate.cumulative_trapezoid(y, x, initial=0) + >>> plt.plot(x, y_int, 'ro', x, y[0] + 0.5 * x**2, 'b-') + >>> plt.show() + + """ + y = np.asarray(y) + if y.shape[axis] == 0: + raise ValueError("At least one point is required along `axis`.") + if x is None: + d = dx + else: + x = np.asarray(x) + if x.ndim == 1: + d = np.diff(x) + # reshape to correct shape + shape = [1] * y.ndim + shape[axis] = -1 + d = d.reshape(shape) + elif len(x.shape) != len(y.shape): + raise ValueError("If given, shape of x must be 1-D or the " + "same as y.") + else: + d = np.diff(x, axis=axis) + + if d.shape[axis] != y.shape[axis] - 1: + raise ValueError("If given, length of x along axis must be the " + "same as y.") + + nd = len(y.shape) + slice1 = tupleset((slice(None),)*nd, axis, slice(1, None)) + slice2 = tupleset((slice(None),)*nd, axis, slice(None, -1)) + res = np.cumsum(d * (y[slice1] + y[slice2]) / 2.0, axis=axis) + + if initial is not None: + if initial != 0: + raise ValueError("`initial` must be `None` or `0`.") + if not np.isscalar(initial): + raise ValueError("`initial` parameter should be a scalar.") + + shape = list(res.shape) + shape[axis] = 1 + res = np.concatenate([np.full(shape, initial, dtype=res.dtype), res], + axis=axis) + + return res + + +def _basic_simpson(y, start, stop, x, dx, axis): + nd = len(y.shape) + if start is None: + start = 0 + step = 2 + slice_all = (slice(None),)*nd + slice0 = tupleset(slice_all, axis, slice(start, stop, step)) + slice1 = tupleset(slice_all, axis, slice(start+1, stop+1, step)) + slice2 = tupleset(slice_all, axis, slice(start+2, stop+2, step)) + + if x is None: # Even-spaced Simpson's rule. + result = np.sum(y[slice0] + 4.0*y[slice1] + y[slice2], axis=axis) + result *= dx / 3.0 + else: + # Account for possibly different spacings. + # Simpson's rule changes a bit. + h = np.diff(x, axis=axis) + sl0 = tupleset(slice_all, axis, slice(start, stop, step)) + sl1 = tupleset(slice_all, axis, slice(start+1, stop+1, step)) + h0 = h[sl0].astype(float, copy=False) + h1 = h[sl1].astype(float, copy=False) + hsum = h0 + h1 + hprod = h0 * h1 + h0divh1 = np.true_divide(h0, h1, out=np.zeros_like(h0), where=h1 != 0) + tmp = hsum/6.0 * (y[slice0] * + (2.0 - np.true_divide(1.0, h0divh1, + out=np.zeros_like(h0divh1), + where=h0divh1 != 0)) + + y[slice1] * (hsum * + np.true_divide(hsum, hprod, + out=np.zeros_like(hsum), + where=hprod != 0)) + + y[slice2] * (2.0 - h0divh1)) + result = np.sum(tmp, axis=axis) + return result + + +def simpson(y, x=None, *, dx=1.0, axis=-1): + """ + Integrate y(x) using samples along the given axis and the composite + Simpson's rule. If x is None, spacing of dx is assumed. + + Parameters + ---------- + y : array_like + Array to be integrated. + x : array_like, optional + If given, the points at which `y` is sampled. + dx : float, optional + Spacing of integration points along axis of `x`. Only used when + `x` is None. Default is 1. + axis : int, optional + Axis along which to integrate. Default is the last axis. + + Returns + ------- + float + The estimated integral computed with the composite Simpson's rule. + + See Also + -------- + quad : adaptive quadrature using QUADPACK + fixed_quad : fixed-order Gaussian quadrature + dblquad : double integrals + tplquad : triple integrals + romb : integrators for sampled data + cumulative_trapezoid : cumulative integration for sampled data + cumulative_simpson : cumulative integration using Simpson's 1/3 rule + + Notes + ----- + For an odd number of samples that are equally spaced the result is + exact if the function is a polynomial of order 3 or less. If + the samples are not equally spaced, then the result is exact only + if the function is a polynomial of order 2 or less. + + References + ---------- + .. [1] Cartwright, Kenneth V. Simpson's Rule Cumulative Integration with + MS Excel and Irregularly-spaced Data. Journal of Mathematical + Sciences and Mathematics Education. 12 (2): 1-9 + + Examples + -------- + >>> from scipy import integrate + >>> import numpy as np + >>> x = np.arange(0, 10) + >>> y = np.arange(0, 10) + + >>> integrate.simpson(y, x=x) + 40.5 + + >>> y = np.power(x, 3) + >>> integrate.simpson(y, x=x) + 1640.5 + >>> integrate.quad(lambda x: x**3, 0, 9)[0] + 1640.25 + + """ + y = np.asarray(y) + nd = len(y.shape) + N = y.shape[axis] + last_dx = dx + returnshape = 0 + if x is not None: + x = np.asarray(x) + if len(x.shape) == 1: + shapex = [1] * nd + shapex[axis] = x.shape[0] + saveshape = x.shape + returnshape = 1 + x = x.reshape(tuple(shapex)) + elif len(x.shape) != len(y.shape): + raise ValueError("If given, shape of x must be 1-D or the " + "same as y.") + if x.shape[axis] != N: + raise ValueError("If given, length of x along axis must be the " + "same as y.") + + if N % 2 == 0: + val = 0.0 + result = 0.0 + slice_all = (slice(None),) * nd + + if N == 2: + # need at least 3 points in integration axis to form parabolic + # segment. If there are two points then any of 'avg', 'first', + # 'last' should give the same result. + slice1 = tupleset(slice_all, axis, -1) + slice2 = tupleset(slice_all, axis, -2) + if x is not None: + last_dx = x[slice1] - x[slice2] + val += 0.5 * last_dx * (y[slice1] + y[slice2]) + else: + # use Simpson's rule on first intervals + result = _basic_simpson(y, 0, N-3, x, dx, axis) + + slice1 = tupleset(slice_all, axis, -1) + slice2 = tupleset(slice_all, axis, -2) + slice3 = tupleset(slice_all, axis, -3) + + h = np.asarray([dx, dx], dtype=np.float64) + if x is not None: + # grab the last two spacings from the appropriate axis + hm2 = tupleset(slice_all, axis, slice(-2, -1, 1)) + hm1 = tupleset(slice_all, axis, slice(-1, None, 1)) + + diffs = np.float64(np.diff(x, axis=axis)) + h = [np.squeeze(diffs[hm2], axis=axis), + np.squeeze(diffs[hm1], axis=axis)] + + # This is the correction for the last interval according to + # Cartwright. + # However, I used the equations given at + # https://en.wikipedia.org/wiki/Simpson%27s_rule#Composite_Simpson's_rule_for_irregularly_spaced_data + # A footnote on Wikipedia says: + # Cartwright 2017, Equation 8. The equation in Cartwright is + # calculating the first interval whereas the equations in the + # Wikipedia article are adjusting for the last integral. If the + # proper algebraic substitutions are made, the equation results in + # the values shown. + num = 2 * h[1] ** 2 + 3 * h[0] * h[1] + den = 6 * (h[1] + h[0]) + alpha = np.true_divide( + num, + den, + out=np.zeros_like(den), + where=den != 0 + ) + + num = h[1] ** 2 + 3.0 * h[0] * h[1] + den = 6 * h[0] + beta = np.true_divide( + num, + den, + out=np.zeros_like(den), + where=den != 0 + ) + + num = 1 * h[1] ** 3 + den = 6 * h[0] * (h[0] + h[1]) + eta = np.true_divide( + num, + den, + out=np.zeros_like(den), + where=den != 0 + ) + + result += alpha*y[slice1] + beta*y[slice2] - eta*y[slice3] + + result += val + else: + result = _basic_simpson(y, 0, N-2, x, dx, axis) + if returnshape: + x = x.reshape(saveshape) + return result + + +def _cumulatively_sum_simpson_integrals( + y: np.ndarray, + dx: np.ndarray, + integration_func: Callable[[np.ndarray, np.ndarray], np.ndarray], +) -> np.ndarray: + """Calculate cumulative sum of Simpson integrals. + Takes as input the integration function to be used. + The integration_func is assumed to return the cumulative sum using + composite Simpson's rule. Assumes the axis of summation is -1. + """ + sub_integrals_h1 = integration_func(y, dx) + sub_integrals_h2 = integration_func(y[..., ::-1], dx[..., ::-1])[..., ::-1] + + shape = list(sub_integrals_h1.shape) + shape[-1] += 1 + sub_integrals = np.empty(shape) + sub_integrals[..., :-1:2] = sub_integrals_h1[..., ::2] + sub_integrals[..., 1::2] = sub_integrals_h2[..., ::2] + # Integral over last subinterval can only be calculated from + # formula for h2 + sub_integrals[..., -1] = sub_integrals_h2[..., -1] + res = np.cumsum(sub_integrals, axis=-1) + return res + + +def _cumulative_simpson_equal_intervals(y: np.ndarray, dx: np.ndarray) -> np.ndarray: + """Calculate the Simpson integrals for all h1 intervals assuming equal interval + widths. The function can also be used to calculate the integral for all + h2 intervals by reversing the inputs, `y` and `dx`. + """ + d = dx[..., :-1] + f1 = y[..., :-2] + f2 = y[..., 1:-1] + f3 = y[..., 2:] + + # Calculate integral over the subintervals (eqn (10) of Reference [2]) + return d / 3 * (5 * f1 / 4 + 2 * f2 - f3 / 4) + + +def _cumulative_simpson_unequal_intervals(y: np.ndarray, dx: np.ndarray) -> np.ndarray: + """Calculate the Simpson integrals for all h1 intervals assuming unequal interval + widths. The function can also be used to calculate the integral for all + h2 intervals by reversing the inputs, `y` and `dx`. + """ + x21 = dx[..., :-1] + x32 = dx[..., 1:] + f1 = y[..., :-2] + f2 = y[..., 1:-1] + f3 = y[..., 2:] + + x31 = x21 + x32 + x21_x31 = x21/x31 + x21_x32 = x21/x32 + x21x21_x31x32 = x21_x31 * x21_x32 + + # Calculate integral over the subintervals (eqn (8) of Reference [2]) + coeff1 = 3 - x21_x31 + coeff2 = 3 + x21x21_x31x32 + x21_x31 + coeff3 = -x21x21_x31x32 + + return x21/6 * (coeff1*f1 + coeff2*f2 + coeff3*f3) + + +def _ensure_float_array(arr: npt.ArrayLike) -> np.ndarray: + arr = np.asarray(arr) + if np.issubdtype(arr.dtype, np.integer): + arr = arr.astype(float, copy=False) + return arr + + +def cumulative_simpson(y, *, x=None, dx=1.0, axis=-1, initial=None): + r""" + Cumulatively integrate y(x) using the composite Simpson's 1/3 rule. + The integral of the samples at every point is calculated by assuming a + quadratic relationship between each point and the two adjacent points. + + Parameters + ---------- + y : array_like + Values to integrate. Requires at least one point along `axis`. If two or fewer + points are provided along `axis`, Simpson's integration is not possible and the + result is calculated with `cumulative_trapezoid`. + x : array_like, optional + The coordinate to integrate along. Must have the same shape as `y` or + must be 1D with the same length as `y` along `axis`. `x` must also be + strictly increasing along `axis`. + If `x` is None (default), integration is performed using spacing `dx` + between consecutive elements in `y`. + dx : scalar or array_like, optional + Spacing between elements of `y`. Only used if `x` is None. Can either + be a float, or an array with the same shape as `y`, but of length one along + `axis`. Default is 1.0. + axis : int, optional + Specifies the axis to integrate along. Default is -1 (last axis). + initial : scalar or array_like, optional + If given, insert this value at the beginning of the returned result, + and add it to the rest of the result. Default is None, which means no + value at ``x[0]`` is returned and `res` has one element less than `y` + along the axis of integration. Can either be a float, or an array with + the same shape as `y`, but of length one along `axis`. + + Returns + ------- + res : ndarray + The result of cumulative integration of `y` along `axis`. + If `initial` is None, the shape is such that the axis of integration + has one less value than `y`. If `initial` is given, the shape is equal + to that of `y`. + + See Also + -------- + numpy.cumsum + cumulative_trapezoid : cumulative integration using the composite + trapezoidal rule + simpson : integrator for sampled data using the Composite Simpson's Rule + + Notes + ----- + + .. versionadded:: 1.12.0 + + The composite Simpson's 1/3 method can be used to approximate the definite + integral of a sampled input function :math:`y(x)` [1]_. The method assumes + a quadratic relationship over the interval containing any three consecutive + sampled points. + + Consider three consecutive points: + :math:`(x_1, y_1), (x_2, y_2), (x_3, y_3)`. + + Assuming a quadratic relationship over the three points, the integral over + the subinterval between :math:`x_1` and :math:`x_2` is given by formula + (8) of [2]_: + + .. math:: + \int_{x_1}^{x_2} y(x) dx\ &= \frac{x_2-x_1}{6}\left[\ + \left\{3-\frac{x_2-x_1}{x_3-x_1}\right\} y_1 + \ + \left\{3 + \frac{(x_2-x_1)^2}{(x_3-x_2)(x_3-x_1)} + \ + \frac{x_2-x_1}{x_3-x_1}\right\} y_2\\ + - \frac{(x_2-x_1)^2}{(x_3-x_2)(x_3-x_1)} y_3\right] + + The integral between :math:`x_2` and :math:`x_3` is given by swapping + appearances of :math:`x_1` and :math:`x_3`. The integral is estimated + separately for each subinterval and then cumulatively summed to obtain + the final result. + + For samples that are equally spaced, the result is exact if the function + is a polynomial of order three or less [1]_ and the number of subintervals + is even. Otherwise, the integral is exact for polynomials of order two or + less. + + References + ---------- + .. [1] Wikipedia page: https://en.wikipedia.org/wiki/Simpson's_rule + .. [2] Cartwright, Kenneth V. Simpson's Rule Cumulative Integration with + MS Excel and Irregularly-spaced Data. Journal of Mathematical + Sciences and Mathematics Education. 12 (2): 1-9 + + Examples + -------- + >>> from scipy import integrate + >>> import numpy as np + >>> import matplotlib.pyplot as plt + >>> x = np.linspace(-2, 2, num=20) + >>> y = x**2 + >>> y_int = integrate.cumulative_simpson(y, x=x, initial=0) + >>> fig, ax = plt.subplots() + >>> ax.plot(x, y_int, 'ro', x, x**3/3 - (x[0])**3/3, 'b-') + >>> ax.grid() + >>> plt.show() + + The output of `cumulative_simpson` is similar to that of iteratively + calling `simpson` with successively higher upper limits of integration, but + not identical. + + >>> def cumulative_simpson_reference(y, x): + ... return np.asarray([integrate.simpson(y[:i], x=x[:i]) + ... for i in range(2, len(y) + 1)]) + >>> + >>> rng = np.random.default_rng(354673834679465) + >>> x, y = rng.random(size=(2, 10)) + >>> x.sort() + >>> + >>> res = integrate.cumulative_simpson(y, x=x) + >>> ref = cumulative_simpson_reference(y, x) + >>> equal = np.abs(res - ref) < 1e-15 + >>> equal # not equal when `simpson` has even number of subintervals + array([False, True, False, True, False, True, False, True, True]) + + This is expected: because `cumulative_simpson` has access to more + information than `simpson`, it can typically produce more accurate + estimates of the underlying integral over subintervals. + + """ + y = _ensure_float_array(y) + + # validate `axis` and standardize to work along the last axis + original_y = y + original_shape = y.shape + try: + y = np.swapaxes(y, axis, -1) + except IndexError as e: + message = f"`axis={axis}` is not valid for `y` with `y.ndim={y.ndim}`." + raise ValueError(message) from e + if y.shape[-1] < 3: + res = cumulative_trapezoid(original_y, x, dx=dx, axis=axis, initial=None) + res = np.swapaxes(res, axis, -1) + + elif x is not None: + x = _ensure_float_array(x) + message = ("If given, shape of `x` must be the same as `y` or 1-D with " + "the same length as `y` along `axis`.") + if not (x.shape == original_shape + or (x.ndim == 1 and len(x) == original_shape[axis])): + raise ValueError(message) + + x = np.broadcast_to(x, y.shape) if x.ndim == 1 else np.swapaxes(x, axis, -1) + dx = np.diff(x, axis=-1) + if np.any(dx <= 0): + raise ValueError("Input x must be strictly increasing.") + res = _cumulatively_sum_simpson_integrals( + y, dx, _cumulative_simpson_unequal_intervals + ) + + else: + dx = _ensure_float_array(dx) + final_dx_shape = tupleset(original_shape, axis, original_shape[axis] - 1) + alt_input_dx_shape = tupleset(original_shape, axis, 1) + message = ("If provided, `dx` must either be a scalar or have the same " + "shape as `y` but with only 1 point along `axis`.") + if not (dx.ndim == 0 or dx.shape == alt_input_dx_shape): + raise ValueError(message) + dx = np.broadcast_to(dx, final_dx_shape) + dx = np.swapaxes(dx, axis, -1) + res = _cumulatively_sum_simpson_integrals( + y, dx, _cumulative_simpson_equal_intervals + ) + + if initial is not None: + initial = _ensure_float_array(initial) + alt_initial_input_shape = tupleset(original_shape, axis, 1) + message = ("If provided, `initial` must either be a scalar or have the " + "same shape as `y` but with only 1 point along `axis`.") + if not (initial.ndim == 0 or initial.shape == alt_initial_input_shape): + raise ValueError(message) + initial = np.broadcast_to(initial, alt_initial_input_shape) + initial = np.swapaxes(initial, axis, -1) + + res += initial + res = np.concatenate((initial, res), axis=-1) + + res = np.swapaxes(res, -1, axis) + return res + + +def romb(y, dx=1.0, axis=-1, show=False): + """ + Romberg integration using samples of a function. + + Parameters + ---------- + y : array_like + A vector of ``2**k + 1`` equally-spaced samples of a function. + dx : float, optional + The sample spacing. Default is 1. + axis : int, optional + The axis along which to integrate. Default is -1 (last axis). + show : bool, optional + When `y` is a single 1-D array, then if this argument is True + print the table showing Richardson extrapolation from the + samples. Default is False. + + Returns + ------- + romb : ndarray + The integrated result for `axis`. + + See Also + -------- + quad : adaptive quadrature using QUADPACK + fixed_quad : fixed-order Gaussian quadrature + dblquad : double integrals + tplquad : triple integrals + simpson : integrators for sampled data + cumulative_trapezoid : cumulative integration for sampled data + + Examples + -------- + >>> from scipy import integrate + >>> import numpy as np + >>> x = np.arange(10, 14.25, 0.25) + >>> y = np.arange(3, 12) + + >>> integrate.romb(y) + 56.0 + + >>> y = np.sin(np.power(x, 2.5)) + >>> integrate.romb(y) + -0.742561336672229 + + >>> integrate.romb(y, show=True) + Richardson Extrapolation Table for Romberg Integration + ====================================================== + -0.81576 + 4.63862 6.45674 + -1.10581 -3.02062 -3.65245 + -2.57379 -3.06311 -3.06595 -3.05664 + -1.34093 -0.92997 -0.78776 -0.75160 -0.74256 + ====================================================== + -0.742561336672229 # may vary + + """ + y = np.asarray(y) + nd = len(y.shape) + Nsamps = y.shape[axis] + Ninterv = Nsamps-1 + n = 1 + k = 0 + while n < Ninterv: + n <<= 1 + k += 1 + if n != Ninterv: + raise ValueError("Number of samples must be one plus a " + "non-negative power of 2.") + + R = {} + slice_all = (slice(None),) * nd + slice0 = tupleset(slice_all, axis, 0) + slicem1 = tupleset(slice_all, axis, -1) + h = Ninterv * np.asarray(dx, dtype=float) + R[(0, 0)] = (y[slice0] + y[slicem1])/2.0*h + slice_R = slice_all + start = stop = step = Ninterv + for i in range(1, k+1): + start >>= 1 + slice_R = tupleset(slice_R, axis, slice(start, stop, step)) + step >>= 1 + R[(i, 0)] = 0.5*(R[(i-1, 0)] + h*y[slice_R].sum(axis=axis)) + for j in range(1, i+1): + prev = R[(i, j-1)] + R[(i, j)] = prev + (prev-R[(i-1, j-1)]) / ((1 << (2*j))-1) + h /= 2.0 + + if show: + if not np.isscalar(R[(0, 0)]): + print("*** Printing table only supported for integrals" + + " of a single data set.") + else: + try: + precis = show[0] + except (TypeError, IndexError): + precis = 5 + try: + width = show[1] + except (TypeError, IndexError): + width = 8 + formstr = "%%%d.%df" % (width, precis) + + title = "Richardson Extrapolation Table for Romberg Integration" + print(title, "=" * len(title), sep="\n", end="\n") + for i in range(k+1): + for j in range(i+1): + print(formstr % R[(i, j)], end=" ") + print() + print("=" * len(title)) + + return R[(k, k)] + + +# Coefficients for Newton-Cotes quadrature +# +# These are the points being used +# to construct the local interpolating polynomial +# a are the weights for Newton-Cotes integration +# B is the error coefficient. +# error in these coefficients grows as N gets larger. +# or as samples are closer and closer together + +# You can use maxima to find these rational coefficients +# for equally spaced data using the commands +# a(i,N) := (integrate(product(r-j,j,0,i-1) * product(r-j,j,i+1,N),r,0,N) +# / ((N-i)! * i!) * (-1)^(N-i)); +# Be(N) := N^(N+2)/(N+2)! * (N/(N+3) - sum((i/N)^(N+2)*a(i,N),i,0,N)); +# Bo(N) := N^(N+1)/(N+1)! * (N/(N+2) - sum((i/N)^(N+1)*a(i,N),i,0,N)); +# B(N) := (if (mod(N,2)=0) then Be(N) else Bo(N)); +# +# pre-computed for equally-spaced weights +# +# num_a, den_a, int_a, num_B, den_B = _builtincoeffs[N] +# +# a = num_a*array(int_a)/den_a +# B = num_B*1.0 / den_B +# +# integrate(f(x),x,x_0,x_N) = dx*sum(a*f(x_i)) + B*(dx)^(2k+3) f^(2k+2)(x*) +# where k = N // 2 +# +_builtincoeffs = { + 1: (1,2,[1,1],-1,12), + 2: (1,3,[1,4,1],-1,90), + 3: (3,8,[1,3,3,1],-3,80), + 4: (2,45,[7,32,12,32,7],-8,945), + 5: (5,288,[19,75,50,50,75,19],-275,12096), + 6: (1,140,[41,216,27,272,27,216,41],-9,1400), + 7: (7,17280,[751,3577,1323,2989,2989,1323,3577,751],-8183,518400), + 8: (4,14175,[989,5888,-928,10496,-4540,10496,-928,5888,989], + -2368,467775), + 9: (9,89600,[2857,15741,1080,19344,5778,5778,19344,1080, + 15741,2857], -4671, 394240), + 10: (5,299376,[16067,106300,-48525,272400,-260550,427368, + -260550,272400,-48525,106300,16067], + -673175, 163459296), + 11: (11,87091200,[2171465,13486539,-3237113, 25226685,-9595542, + 15493566,15493566,-9595542,25226685,-3237113, + 13486539,2171465], -2224234463, 237758976000), + 12: (1, 5255250, [1364651,9903168,-7587864,35725120,-51491295, + 87516288,-87797136,87516288,-51491295,35725120, + -7587864,9903168,1364651], -3012, 875875), + 13: (13, 402361344000,[8181904909, 56280729661, -31268252574, + 156074417954,-151659573325,206683437987, + -43111992612,-43111992612,206683437987, + -151659573325,156074417954,-31268252574, + 56280729661,8181904909], -2639651053, + 344881152000), + 14: (7, 2501928000, [90241897,710986864,-770720657,3501442784, + -6625093363,12630121616,-16802270373,19534438464, + -16802270373,12630121616,-6625093363,3501442784, + -770720657,710986864,90241897], -3740727473, + 1275983280000) + } + + +def newton_cotes(rn, equal=0): + r""" + Return weights and error coefficient for Newton-Cotes integration. + + Suppose we have (N+1) samples of f at the positions + x_0, x_1, ..., x_N. Then an N-point Newton-Cotes formula for the + integral between x_0 and x_N is: + + :math:`\int_{x_0}^{x_N} f(x)dx = \Delta x \sum_{i=0}^{N} a_i f(x_i) + + B_N (\Delta x)^{N+2} f^{N+1} (\xi)` + + where :math:`\xi \in [x_0,x_N]` + and :math:`\Delta x = \frac{x_N-x_0}{N}` is the average samples spacing. + + If the samples are equally-spaced and N is even, then the error + term is :math:`B_N (\Delta x)^{N+3} f^{N+2}(\xi)`. + + Parameters + ---------- + rn : int + The integer order for equally-spaced data or the relative positions of + the samples with the first sample at 0 and the last at N, where N+1 is + the length of `rn`. N is the order of the Newton-Cotes integration. + equal : int, optional + Set to 1 to enforce equally spaced data. + + Returns + ------- + an : ndarray + 1-D array of weights to apply to the function at the provided sample + positions. + B : float + Error coefficient. + + Notes + ----- + Normally, the Newton-Cotes rules are used on smaller integration + regions and a composite rule is used to return the total integral. + + Examples + -------- + Compute the integral of sin(x) in [0, :math:`\pi`]: + + >>> from scipy.integrate import newton_cotes + >>> import numpy as np + >>> def f(x): + ... return np.sin(x) + >>> a = 0 + >>> b = np.pi + >>> exact = 2 + >>> for N in [2, 4, 6, 8, 10]: + ... x = np.linspace(a, b, N + 1) + ... an, B = newton_cotes(N, 1) + ... dx = (b - a) / N + ... quad = dx * np.sum(an * f(x)) + ... error = abs(quad - exact) + ... print('{:2d} {:10.9f} {:.5e}'.format(N, quad, error)) + ... + 2 2.094395102 9.43951e-02 + 4 1.998570732 1.42927e-03 + 6 2.000017814 1.78136e-05 + 8 1.999999835 1.64725e-07 + 10 2.000000001 1.14677e-09 + + """ + try: + N = len(rn)-1 + if equal: + rn = np.arange(N+1) + elif np.all(np.diff(rn) == 1): + equal = 1 + except Exception: + N = rn + rn = np.arange(N+1) + equal = 1 + + if equal and N in _builtincoeffs: + na, da, vi, nb, db = _builtincoeffs[N] + an = na * np.array(vi, dtype=float) / da + return an, float(nb)/db + + if (rn[0] != 0) or (rn[-1] != N): + raise ValueError("The sample positions must start at 0" + " and end at N") + yi = rn / float(N) + ti = 2 * yi - 1 + nvec = np.arange(N+1) + C = ti ** nvec[:, np.newaxis] + Cinv = np.linalg.inv(C) + # improve precision of result + for i in range(2): + Cinv = 2*Cinv - Cinv.dot(C).dot(Cinv) + vec = 2.0 / (nvec[::2]+1) + ai = Cinv[:, ::2].dot(vec) * (N / 2.) + + if (N % 2 == 0) and equal: + BN = N/(N+3.) + power = N+2 + else: + BN = N/(N+2.) + power = N+1 + + BN = BN - np.dot(yi**power, ai) + p1 = power+1 + fac = power*math.log(N) - gammaln(p1) + fac = math.exp(fac) + return ai, BN*fac + + +def _qmc_quad_iv(func, a, b, n_points, n_estimates, qrng, log): + + # lazy import to avoid issues with partially-initialized submodule + if not hasattr(qmc_quad, 'qmc'): + from scipy import stats + qmc_quad.stats = stats + else: + stats = qmc_quad.stats + + if not callable(func): + message = "`func` must be callable." + raise TypeError(message) + + # a, b will be modified, so copy. Oh well if it's copied twice. + a = np.atleast_1d(a).copy() + b = np.atleast_1d(b).copy() + a, b = np.broadcast_arrays(a, b) + dim = a.shape[0] + + try: + func((a + b) / 2) + except Exception as e: + message = ("`func` must evaluate the integrand at points within " + "the integration range; e.g. `func( (a + b) / 2)` " + "must return the integrand at the centroid of the " + "integration volume.") + raise ValueError(message) from e + + try: + func(np.array([a, b]).T) + vfunc = func + except Exception as e: + message = ("Exception encountered when attempting vectorized call to " + f"`func`: {e}. For better performance, `func` should " + "accept two-dimensional array `x` with shape `(len(a), " + "n_points)` and return an array of the integrand value at " + "each of the `n_points.") + warnings.warn(message, stacklevel=3) + + def vfunc(x): + return np.apply_along_axis(func, axis=-1, arr=x) + + n_points_int = np.int64(n_points) + if n_points != n_points_int: + message = "`n_points` must be an integer." + raise TypeError(message) + + n_estimates_int = np.int64(n_estimates) + if n_estimates != n_estimates_int: + message = "`n_estimates` must be an integer." + raise TypeError(message) + + if qrng is None: + qrng = stats.qmc.Halton(dim) + elif not isinstance(qrng, stats.qmc.QMCEngine): + message = "`qrng` must be an instance of scipy.stats.qmc.QMCEngine." + raise TypeError(message) + + if qrng.d != a.shape[0]: + message = ("`qrng` must be initialized with dimensionality equal to " + "the number of variables in `a`, i.e., " + "`qrng.random().shape[-1]` must equal `a.shape[0]`.") + raise ValueError(message) + + rng_seed = getattr(qrng, 'rng_seed', None) + rng = stats._qmc.check_random_state(rng_seed) + + if log not in {True, False}: + message = "`log` must be boolean (`True` or `False`)." + raise TypeError(message) + + return (vfunc, a, b, n_points_int, n_estimates_int, qrng, rng, log, stats) + + +QMCQuadResult = namedtuple('QMCQuadResult', ['integral', 'standard_error']) + + +def qmc_quad(func, a, b, *, n_estimates=8, n_points=1024, qrng=None, + log=False): + """ + Compute an integral in N-dimensions using Quasi-Monte Carlo quadrature. + + Parameters + ---------- + func : callable + The integrand. Must accept a single argument ``x``, an array which + specifies the point(s) at which to evaluate the scalar-valued + integrand, and return the value(s) of the integrand. + For efficiency, the function should be vectorized to accept an array of + shape ``(d, n_points)``, where ``d`` is the number of variables (i.e. + the dimensionality of the function domain) and `n_points` is the number + of quadrature points, and return an array of shape ``(n_points,)``, + the integrand at each quadrature point. + a, b : array-like + One-dimensional arrays specifying the lower and upper integration + limits, respectively, of each of the ``d`` variables. + n_estimates, n_points : int, optional + `n_estimates` (default: 8) statistically independent QMC samples, each + of `n_points` (default: 1024) points, will be generated by `qrng`. + The total number of points at which the integrand `func` will be + evaluated is ``n_points * n_estimates``. See Notes for details. + qrng : `~scipy.stats.qmc.QMCEngine`, optional + An instance of the QMCEngine from which to sample QMC points. + The QMCEngine must be initialized to a number of dimensions ``d`` + corresponding with the number of variables ``x1, ..., xd`` passed to + `func`. + The provided QMCEngine is used to produce the first integral estimate. + If `n_estimates` is greater than one, additional QMCEngines are + spawned from the first (with scrambling enabled, if it is an option.) + If a QMCEngine is not provided, the default `scipy.stats.qmc.Halton` + will be initialized with the number of dimensions determine from + the length of `a`. + log : boolean, default: False + When set to True, `func` returns the log of the integrand, and + the result object contains the log of the integral. + + Returns + ------- + result : object + A result object with attributes: + + integral : float + The estimate of the integral. + standard_error : + The error estimate. See Notes for interpretation. + + Notes + ----- + Values of the integrand at each of the `n_points` points of a QMC sample + are used to produce an estimate of the integral. This estimate is drawn + from a population of possible estimates of the integral, the value of + which we obtain depends on the particular points at which the integral + was evaluated. We perform this process `n_estimates` times, each time + evaluating the integrand at different scrambled QMC points, effectively + drawing i.i.d. random samples from the population of integral estimates. + The sample mean :math:`m` of these integral estimates is an + unbiased estimator of the true value of the integral, and the standard + error of the mean :math:`s` of these estimates may be used to generate + confidence intervals using the t distribution with ``n_estimates - 1`` + degrees of freedom. Perhaps counter-intuitively, increasing `n_points` + while keeping the total number of function evaluation points + ``n_points * n_estimates`` fixed tends to reduce the actual error, whereas + increasing `n_estimates` tends to decrease the error estimate. + + Examples + -------- + QMC quadrature is particularly useful for computing integrals in higher + dimensions. An example integrand is the probability density function + of a multivariate normal distribution. + + >>> import numpy as np + >>> from scipy import stats + >>> dim = 8 + >>> mean = np.zeros(dim) + >>> cov = np.eye(dim) + >>> def func(x): + ... # `multivariate_normal` expects the _last_ axis to correspond with + ... # the dimensionality of the space, so `x` must be transposed + ... return stats.multivariate_normal.pdf(x.T, mean, cov) + + To compute the integral over the unit hypercube: + + >>> from scipy.integrate import qmc_quad + >>> a = np.zeros(dim) + >>> b = np.ones(dim) + >>> rng = np.random.default_rng() + >>> qrng = stats.qmc.Halton(d=dim, seed=rng) + >>> n_estimates = 8 + >>> res = qmc_quad(func, a, b, n_estimates=n_estimates, qrng=qrng) + >>> res.integral, res.standard_error + (0.00018429555666024108, 1.0389431116001344e-07) + + A two-sided, 99% confidence interval for the integral may be estimated + as: + + >>> t = stats.t(df=n_estimates-1, loc=res.integral, + ... scale=res.standard_error) + >>> t.interval(0.99) + (0.0001839319802536469, 0.00018465913306683527) + + Indeed, the value reported by `scipy.stats.multivariate_normal` is + within this range. + + >>> stats.multivariate_normal.cdf(b, mean, cov, lower_limit=a) + 0.00018430867675187443 + + """ + args = _qmc_quad_iv(func, a, b, n_points, n_estimates, qrng, log) + func, a, b, n_points, n_estimates, qrng, rng, log, stats = args + + def sum_product(integrands, dA, log=False): + if log: + return logsumexp(integrands) + np.log(dA) + else: + return np.sum(integrands * dA) + + def mean(estimates, log=False): + if log: + return logsumexp(estimates) - np.log(n_estimates) + else: + return np.mean(estimates) + + def std(estimates, m=None, ddof=0, log=False): + m = m or mean(estimates, log) + if log: + estimates, m = np.broadcast_arrays(estimates, m) + temp = np.vstack((estimates, m + np.pi * 1j)) + diff = logsumexp(temp, axis=0) + return np.real(0.5 * (logsumexp(2 * diff) + - np.log(n_estimates - ddof))) + else: + return np.std(estimates, ddof=ddof) + + def sem(estimates, m=None, s=None, log=False): + m = m or mean(estimates, log) + s = s or std(estimates, m, ddof=1, log=log) + if log: + return s - 0.5*np.log(n_estimates) + else: + return s / np.sqrt(n_estimates) + + # The sign of the integral depends on the order of the limits. Fix this by + # ensuring that lower bounds are indeed lower and setting sign of resulting + # integral manually + if np.any(a == b): + message = ("A lower limit was equal to an upper limit, so the value " + "of the integral is zero by definition.") + warnings.warn(message, stacklevel=2) + return QMCQuadResult(-np.inf if log else 0, 0) + + i_swap = b < a + sign = (-1)**(i_swap.sum(axis=-1)) # odd # of swaps -> negative + a[i_swap], b[i_swap] = b[i_swap], a[i_swap] + + A = np.prod(b - a) + dA = A / n_points + + estimates = np.zeros(n_estimates) + rngs = _rng_spawn(qrng.rng, n_estimates) + for i in range(n_estimates): + # Generate integral estimate + sample = qrng.random(n_points) + # The rationale for transposing is that this allows users to easily + # unpack `x` into separate variables, if desired. This is consistent + # with the `xx` array passed into the `scipy.integrate.nquad` `func`. + x = stats.qmc.scale(sample, a, b).T # (n_dim, n_points) + integrands = func(x) + estimates[i] = sum_product(integrands, dA, log) + + # Get a new, independently-scrambled QRNG for next time + qrng = type(qrng)(seed=rngs[i], **qrng._init_quad) + + integral = mean(estimates, log) + standard_error = sem(estimates, m=integral, log=log) + integral = integral + np.pi*1j if (log and sign < 0) else integral*sign + return QMCQuadResult(integral, standard_error) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_rules/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_rules/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..4c91aa324478d49a8723f05618801f9b256d07af --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_rules/__init__.py @@ -0,0 +1,12 @@ +"""Numerical cubature algorithms""" + +from ._base import ( + Rule, FixedRule, + NestedFixedRule, + ProductNestedFixed, +) +from ._genz_malik import GenzMalikCubature +from ._gauss_kronrod import GaussKronrodQuadrature +from ._gauss_legendre import GaussLegendreQuadrature + +__all__ = [s for s in dir() if not s.startswith('_')] diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_rules/_base.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_rules/_base.py new file mode 100644 index 0000000000000000000000000000000000000000..3a3ae5f506505c9c03b2ac8be33d301d60074681 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_rules/_base.py @@ -0,0 +1,518 @@ +from scipy._lib._array_api import array_namespace, xp_size + +from functools import cached_property + + +class Rule: + """ + Base class for numerical integration algorithms (cubatures). + + Finds an estimate for the integral of ``f`` over the region described by two arrays + ``a`` and ``b`` via `estimate`, and find an estimate for the error of this + approximation via `estimate_error`. + + If a subclass does not implement its own `estimate_error`, then it will use a + default error estimate based on the difference between the estimate over the whole + region and the sum of estimates over that region divided into ``2^ndim`` subregions. + + See Also + -------- + FixedRule + + Examples + -------- + In the following, a custom rule is created which uses 3D Genz-Malik cubature for + the estimate of the integral, and the difference between this estimate and a less + accurate estimate using 5-node Gauss-Legendre quadrature as an estimate for the + error. + + >>> import numpy as np + >>> from scipy.integrate import cubature + >>> from scipy.integrate._rules import ( + ... Rule, ProductNestedFixed, GenzMalikCubature, GaussLegendreQuadrature + ... ) + >>> def f(x, r, alphas): + ... # f(x) = cos(2*pi*r + alpha @ x) + ... # Need to allow r and alphas to be arbitrary shape + ... npoints, ndim = x.shape[0], x.shape[-1] + ... alphas_reshaped = alphas[np.newaxis, :] + ... x_reshaped = x.reshape(npoints, *([1]*(len(alphas.shape) - 1)), ndim) + ... return np.cos(2*np.pi*r + np.sum(alphas_reshaped * x_reshaped, axis=-1)) + >>> genz = GenzMalikCubature(ndim=3) + >>> gauss = GaussKronrodQuadrature(npoints=21) + >>> # Gauss-Kronrod is 1D, so we find the 3D product rule: + >>> gauss_3d = ProductNestedFixed([gauss, gauss, gauss]) + >>> class CustomRule(Rule): + ... def estimate(self, f, a, b, args=()): + ... return genz.estimate(f, a, b, args) + ... def estimate_error(self, f, a, b, args=()): + ... return np.abs( + ... genz.estimate(f, a, b, args) + ... - gauss_3d.estimate(f, a, b, args) + ... ) + >>> rng = np.random.default_rng() + >>> res = cubature( + ... f=f, + ... a=np.array([0, 0, 0]), + ... b=np.array([1, 1, 1]), + ... rule=CustomRule(), + ... args=(rng.random((2,)), rng.random((3, 2, 3))) + ... ) + >>> res.estimate + array([[-0.95179502, 0.12444608], + [-0.96247411, 0.60866385], + [-0.97360014, 0.25515587]]) + """ + + def estimate(self, f, a, b, args=()): + r""" + Calculate estimate of integral of `f` in rectangular region described by + corners `a` and ``b``. + + Parameters + ---------- + f : callable + Function to integrate. `f` must have the signature:: + f(x : ndarray, \*args) -> ndarray + + `f` should accept arrays ``x`` of shape:: + (npoints, ndim) + + and output arrays of shape:: + (npoints, output_dim_1, ..., output_dim_n) + + In this case, `estimate` will return arrays of shape:: + (output_dim_1, ..., output_dim_n) + a, b : ndarray + Lower and upper limits of integration as rank-1 arrays specifying the left + and right endpoints of the intervals being integrated over. Infinite limits + are currently not supported. + args : tuple, optional + Additional positional args passed to ``f``, if any. + + Returns + ------- + est : ndarray + Result of estimation. If `f` returns arrays of shape ``(npoints, + output_dim_1, ..., output_dim_n)``, then `est` will be of shape + ``(output_dim_1, ..., output_dim_n)``. + """ + raise NotImplementedError + + def estimate_error(self, f, a, b, args=()): + r""" + Estimate the error of the approximation for the integral of `f` in rectangular + region described by corners `a` and `b`. + + If a subclass does not override this method, then a default error estimator is + used. This estimates the error as ``|est - refined_est|`` where ``est`` is + ``estimate(f, a, b)`` and ``refined_est`` is the sum of + ``estimate(f, a_k, b_k)`` where ``a_k, b_k`` are the coordinates of each + subregion of the region described by ``a`` and ``b``. In the 1D case, this + is equivalent to comparing the integral over an entire interval ``[a, b]`` to + the sum of the integrals over the left and right subintervals, ``[a, (a+b)/2]`` + and ``[(a+b)/2, b]``. + + Parameters + ---------- + f : callable + Function to estimate error for. `f` must have the signature:: + f(x : ndarray, \*args) -> ndarray + + `f` should accept arrays `x` of shape:: + (npoints, ndim) + + and output arrays of shape:: + (npoints, output_dim_1, ..., output_dim_n) + + In this case, `estimate` will return arrays of shape:: + (output_dim_1, ..., output_dim_n) + a, b : ndarray + Lower and upper limits of integration as rank-1 arrays specifying the left + and right endpoints of the intervals being integrated over. Infinite limits + are currently not supported. + args : tuple, optional + Additional positional args passed to `f`, if any. + + Returns + ------- + err_est : ndarray + Result of error estimation. If `f` returns arrays of shape + ``(npoints, output_dim_1, ..., output_dim_n)``, then `est` will be + of shape ``(output_dim_1, ..., output_dim_n)``. + """ + + est = self.estimate(f, a, b, args) + refined_est = 0 + + for a_k, b_k in _split_subregion(a, b): + refined_est += self.estimate(f, a_k, b_k, args) + + return self.xp.abs(est - refined_est) + + +class FixedRule(Rule): + """ + A rule implemented as the weighted sum of function evaluations at fixed nodes. + + Attributes + ---------- + nodes_and_weights : (ndarray, ndarray) + A tuple ``(nodes, weights)`` of nodes at which to evaluate ``f`` and the + corresponding weights. ``nodes`` should be of shape ``(num_nodes,)`` for 1D + cubature rules (quadratures) and more generally for N-D cubature rules, it + should be of shape ``(num_nodes, ndim)``. ``weights`` should be of shape + ``(num_nodes,)``. The nodes and weights should be for integrals over + :math:`[-1, 1]^n`. + + See Also + -------- + GaussLegendreQuadrature, GaussKronrodQuadrature, GenzMalikCubature + + Examples + -------- + + Implementing Simpson's 1/3 rule: + + >>> import numpy as np + >>> from scipy.integrate._rules import FixedRule + >>> class SimpsonsQuad(FixedRule): + ... @property + ... def nodes_and_weights(self): + ... nodes = np.array([-1, 0, 1]) + ... weights = np.array([1/3, 4/3, 1/3]) + ... return (nodes, weights) + >>> rule = SimpsonsQuad() + >>> rule.estimate( + ... f=lambda x: x**2, + ... a=np.array([0]), + ... b=np.array([1]), + ... ) + [0.3333333] + """ + + def __init__(self): + self.xp = None + + @property + def nodes_and_weights(self): + raise NotImplementedError + + def estimate(self, f, a, b, args=()): + r""" + Calculate estimate of integral of `f` in rectangular region described by + corners `a` and `b` as ``sum(weights * f(nodes))``. + + Nodes and weights will automatically be adjusted from calculating integrals over + :math:`[-1, 1]^n` to :math:`[a, b]^n`. + + Parameters + ---------- + f : callable + Function to integrate. `f` must have the signature:: + f(x : ndarray, \*args) -> ndarray + + `f` should accept arrays `x` of shape:: + (npoints, ndim) + + and output arrays of shape:: + (npoints, output_dim_1, ..., output_dim_n) + + In this case, `estimate` will return arrays of shape:: + (output_dim_1, ..., output_dim_n) + a, b : ndarray + Lower and upper limits of integration as rank-1 arrays specifying the left + and right endpoints of the intervals being integrated over. Infinite limits + are currently not supported. + args : tuple, optional + Additional positional args passed to `f`, if any. + + Returns + ------- + est : ndarray + Result of estimation. If `f` returns arrays of shape ``(npoints, + output_dim_1, ..., output_dim_n)``, then `est` will be of shape + ``(output_dim_1, ..., output_dim_n)``. + """ + nodes, weights = self.nodes_and_weights + + if self.xp is None: + self.xp = array_namespace(nodes) + + return _apply_fixed_rule(f, a, b, nodes, weights, args, self.xp) + + +class NestedFixedRule(FixedRule): + r""" + A cubature rule with error estimate given by the difference between two underlying + fixed rules. + + If constructed as ``NestedFixedRule(higher, lower)``, this will use:: + + estimate(f, a, b) := higher.estimate(f, a, b) + estimate_error(f, a, b) := \|higher.estimate(f, a, b) - lower.estimate(f, a, b)| + + (where the absolute value is taken elementwise). + + Attributes + ---------- + higher : Rule + Higher accuracy rule. + + lower : Rule + Lower accuracy rule. + + See Also + -------- + GaussKronrodQuadrature + + Examples + -------- + + >>> from scipy.integrate import cubature + >>> from scipy.integrate._rules import ( + ... GaussLegendreQuadrature, NestedFixedRule, ProductNestedFixed + ... ) + >>> higher = GaussLegendreQuadrature(10) + >>> lower = GaussLegendreQuadrature(5) + >>> rule = NestedFixedRule( + ... higher, + ... lower + ... ) + >>> rule_2d = ProductNestedFixed([rule, rule]) + """ + + def __init__(self, higher, lower): + self.higher = higher + self.lower = lower + self.xp = None + + @property + def nodes_and_weights(self): + if self.higher is not None: + return self.higher.nodes_and_weights + else: + raise NotImplementedError + + @property + def lower_nodes_and_weights(self): + if self.lower is not None: + return self.lower.nodes_and_weights + else: + raise NotImplementedError + + def estimate_error(self, f, a, b, args=()): + r""" + Estimate the error of the approximation for the integral of `f` in rectangular + region described by corners `a` and `b`. + + Parameters + ---------- + f : callable + Function to estimate error for. `f` must have the signature:: + f(x : ndarray, \*args) -> ndarray + + `f` should accept arrays `x` of shape:: + (npoints, ndim) + + and output arrays of shape:: + (npoints, output_dim_1, ..., output_dim_n) + + In this case, `estimate` will return arrays of shape:: + (output_dim_1, ..., output_dim_n) + a, b : ndarray + Lower and upper limits of integration as rank-1 arrays specifying the left + and right endpoints of the intervals being integrated over. Infinite limits + are currently not supported. + args : tuple, optional + Additional positional args passed to `f`, if any. + + Returns + ------- + err_est : ndarray + Result of error estimation. If `f` returns arrays of shape + ``(npoints, output_dim_1, ..., output_dim_n)``, then `est` will be + of shape ``(output_dim_1, ..., output_dim_n)``. + """ + + nodes, weights = self.nodes_and_weights + lower_nodes, lower_weights = self.lower_nodes_and_weights + + if self.xp is None: + self.xp = array_namespace(nodes) + + error_nodes = self.xp.concat([nodes, lower_nodes], axis=0) + error_weights = self.xp.concat([weights, -lower_weights], axis=0) + + return self.xp.abs( + _apply_fixed_rule(f, a, b, error_nodes, error_weights, args, self.xp) + ) + + +class ProductNestedFixed(NestedFixedRule): + """ + Find the n-dimensional cubature rule constructed from the Cartesian product of 1-D + `NestedFixedRule` quadrature rules. + + Given a list of N 1-dimensional quadrature rules which support error estimation + using NestedFixedRule, this will find the N-dimensional cubature rule obtained by + taking the Cartesian product of their nodes, and estimating the error by taking the + difference with a lower-accuracy N-dimensional cubature rule obtained using the + ``.lower_nodes_and_weights`` rule in each of the base 1-dimensional rules. + + Parameters + ---------- + base_rules : list of NestedFixedRule + List of base 1-dimensional `NestedFixedRule` quadrature rules. + + Attributes + ---------- + base_rules : list of NestedFixedRule + List of base 1-dimensional `NestedFixedRule` qudarature rules. + + Examples + -------- + + Evaluate a 2D integral by taking the product of two 1D rules: + + >>> import numpy as np + >>> from scipy.integrate import cubature + >>> from scipy.integrate._rules import ( + ... ProductNestedFixed, GaussKronrodQuadrature + ... ) + >>> def f(x): + ... # f(x) = cos(x_1) + cos(x_2) + ... return np.sum(np.cos(x), axis=-1) + >>> rule = ProductNestedFixed( + ... [GaussKronrodQuadrature(15), GaussKronrodQuadrature(15)] + ... ) # Use 15-point Gauss-Kronrod, which implements NestedFixedRule + >>> a, b = np.array([0, 0]), np.array([1, 1]) + >>> rule.estimate(f, a, b) # True value 2*sin(1), approximately 1.6829 + np.float64(1.682941969615793) + >>> rule.estimate_error(f, a, b) + np.float64(2.220446049250313e-16) + """ + + def __init__(self, base_rules): + for rule in base_rules: + if not isinstance(rule, NestedFixedRule): + raise ValueError("base rules for product need to be instance of" + "NestedFixedRule") + + self.base_rules = base_rules + self.xp = None + + @cached_property + def nodes_and_weights(self): + nodes = _cartesian_product( + [rule.nodes_and_weights[0] for rule in self.base_rules] + ) + + if self.xp is None: + self.xp = array_namespace(nodes) + + weights = self.xp.prod( + _cartesian_product( + [rule.nodes_and_weights[1] for rule in self.base_rules] + ), + axis=-1, + ) + + return nodes, weights + + @cached_property + def lower_nodes_and_weights(self): + nodes = _cartesian_product( + [cubature.lower_nodes_and_weights[0] for cubature in self.base_rules] + ) + + if self.xp is None: + self.xp = array_namespace(nodes) + + weights = self.xp.prod( + _cartesian_product( + [cubature.lower_nodes_and_weights[1] for cubature in self.base_rules] + ), + axis=-1, + ) + + return nodes, weights + + +def _cartesian_product(arrays): + xp = array_namespace(*arrays) + + arrays_ix = xp.meshgrid(*arrays, indexing='ij') + result = xp.reshape(xp.stack(arrays_ix, axis=-1), (-1, len(arrays))) + + return result + + +def _split_subregion(a, b, xp, split_at=None): + """ + Given the coordinates of a region like a=[0, 0] and b=[1, 1], yield the coordinates + of all subregions, which in this case would be:: + + ([0, 0], [1/2, 1/2]), + ([0, 1/2], [1/2, 1]), + ([1/2, 0], [1, 1/2]), + ([1/2, 1/2], [1, 1]) + """ + xp = array_namespace(a, b) + + if split_at is None: + split_at = (a + b) / 2 + + left = [xp.asarray([a[i], split_at[i]]) for i in range(a.shape[0])] + right = [xp.asarray([split_at[i], b[i]]) for i in range(b.shape[0])] + + a_sub = _cartesian_product(left) + b_sub = _cartesian_product(right) + + for i in range(a_sub.shape[0]): + yield a_sub[i, ...], b_sub[i, ...] + + +def _apply_fixed_rule(f, a, b, orig_nodes, orig_weights, args, xp): + # Downcast nodes and weights to common dtype of a and b + result_dtype = a.dtype + orig_nodes = xp.astype(orig_nodes, result_dtype) + orig_weights = xp.astype(orig_weights, result_dtype) + + # Ensure orig_nodes are at least 2D, since 1D cubature methods can return arrays of + # shape (npoints,) rather than (npoints, 1) + if orig_nodes.ndim == 1: + orig_nodes = orig_nodes[:, None] + + rule_ndim = orig_nodes.shape[-1] + + a_ndim = xp_size(a) + b_ndim = xp_size(b) + + if rule_ndim != a_ndim or rule_ndim != b_ndim: + raise ValueError(f"rule and function are of incompatible dimension, nodes have" + f"ndim {rule_ndim}, while limit of integration has ndim" + f"a_ndim={a_ndim}, b_ndim={b_ndim}") + + lengths = b - a + + # The underlying rule is for the hypercube [-1, 1]^n. + # + # To handle arbitrary regions of integration, it's necessary to apply a linear + # change of coordinates to map each interval [a[i], b[i]] to [-1, 1]. + nodes = (orig_nodes + 1) * (lengths * 0.5) + a + + # Also need to multiply the weights by a scale factor equal to the determinant + # of the Jacobian for this coordinate change. + weight_scale_factor = xp.prod(lengths, dtype=result_dtype) / 2**rule_ndim + weights = orig_weights * weight_scale_factor + + f_nodes = f(nodes, *args) + weights_reshaped = xp.reshape(weights, (-1, *([1] * (f_nodes.ndim - 1)))) + + # f(nodes) will have shape (num_nodes, output_dim_1, ..., output_dim_n) + # Summing along the first axis means estimate will shape (output_dim_1, ..., + # output_dim_n) + est = xp.sum(weights_reshaped * f_nodes, axis=0, dtype=result_dtype) + + return est diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_rules/_gauss_kronrod.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_rules/_gauss_kronrod.py new file mode 100644 index 0000000000000000000000000000000000000000..b2a3518c55cf49cd14c777d243ea7e93a489f86c --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_rules/_gauss_kronrod.py @@ -0,0 +1,202 @@ +from scipy._lib._array_api import np_compat, array_namespace + +from functools import cached_property + +from ._base import NestedFixedRule +from ._gauss_legendre import GaussLegendreQuadrature + + +class GaussKronrodQuadrature(NestedFixedRule): + """ + Gauss-Kronrod quadrature. + + Gauss-Kronrod rules consist of two quadrature rules, one higher-order and one + lower-order. The higher-order rule is used as the estimate of the integral and the + difference between them is used as an estimate for the error. + + Gauss-Kronrod is a 1D rule. To use it for multidimensional integrals, it will be + necessary to use ProductNestedFixed and multiple Gauss-Kronrod rules. See Examples. + + For n-node Gauss-Kronrod, the lower-order rule has ``n//2`` nodes, which are the + ordinary Gauss-Legendre nodes with corresponding weights. The higher-order rule has + ``n`` nodes, ``n//2`` of which are the same as the lower-order rule and the + remaining nodes are the Kronrod extension of those nodes. + + Parameters + ---------- + npoints : int + Number of nodes for the higher-order rule. + + xp : array_namespace, optional + The namespace for the node and weight arrays. Default is None, where NumPy is + used. + + Attributes + ---------- + lower : Rule + Lower-order rule. + + References + ---------- + .. [1] R. Piessens, E. de Doncker, Quadpack: A Subroutine Package for Automatic + Integration, files: dqk21.f, dqk15.f (1983). + + Examples + -------- + Evaluate a 1D integral. Note in this example that ``f`` returns an array, so the + estimates will also be arrays, despite the fact that this is a 1D problem. + + >>> import numpy as np + >>> from scipy.integrate import cubature + >>> from scipy.integrate._rules import GaussKronrodQuadrature + >>> def f(x): + ... return np.cos(x) + >>> rule = GaussKronrodQuadrature(21) # Use 21-point GaussKronrod + >>> a, b = np.array([0]), np.array([1]) + >>> rule.estimate(f, a, b) # True value sin(1), approximately 0.84147 + array([0.84147098]) + >>> rule.estimate_error(f, a, b) + array([1.11022302e-16]) + + Evaluate a 2D integral. Note that in this example ``f`` returns a float, so the + estimates will also be floats. + + >>> import numpy as np + >>> from scipy.integrate import cubature + >>> from scipy.integrate._rules import ( + ... ProductNestedFixed, GaussKronrodQuadrature + ... ) + >>> def f(x): + ... # f(x) = cos(x_1) + cos(x_2) + ... return np.sum(np.cos(x), axis=-1) + >>> rule = ProductNestedFixed( + ... [GaussKronrodQuadrature(15), GaussKronrodQuadrature(15)] + ... ) # Use 15-point Gauss-Kronrod + >>> a, b = np.array([0, 0]), np.array([1, 1]) + >>> rule.estimate(f, a, b) # True value 2*sin(1), approximately 1.6829 + np.float64(1.682941969615793) + >>> rule.estimate_error(f, a, b) + np.float64(2.220446049250313e-16) + """ + + def __init__(self, npoints, xp=None): + # TODO: nodes and weights are currently hard-coded for values 15 and 21, but in + # the future it would be best to compute the Kronrod extension of the lower rule + if npoints != 15 and npoints != 21: + raise NotImplementedError("Gauss-Kronrod quadrature is currently only" + "supported for 15 or 21 nodes") + + self.npoints = npoints + + if xp is None: + xp = np_compat + + self.xp = array_namespace(xp.empty(0)) + + self.gauss = GaussLegendreQuadrature(npoints//2, xp=self.xp) + + @cached_property + def nodes_and_weights(self): + # These values are from QUADPACK's `dqk21.f` and `dqk15.f` (1983). + if self.npoints == 21: + nodes = self.xp.asarray( + [ + 0.995657163025808080735527280689003, + 0.973906528517171720077964012084452, + 0.930157491355708226001207180059508, + 0.865063366688984510732096688423493, + 0.780817726586416897063717578345042, + 0.679409568299024406234327365114874, + 0.562757134668604683339000099272694, + 0.433395394129247190799265943165784, + 0.294392862701460198131126603103866, + 0.148874338981631210884826001129720, + 0, + -0.148874338981631210884826001129720, + -0.294392862701460198131126603103866, + -0.433395394129247190799265943165784, + -0.562757134668604683339000099272694, + -0.679409568299024406234327365114874, + -0.780817726586416897063717578345042, + -0.865063366688984510732096688423493, + -0.930157491355708226001207180059508, + -0.973906528517171720077964012084452, + -0.995657163025808080735527280689003, + ], + dtype=self.xp.float64, + ) + + weights = self.xp.asarray( + [ + 0.011694638867371874278064396062192, + 0.032558162307964727478818972459390, + 0.054755896574351996031381300244580, + 0.075039674810919952767043140916190, + 0.093125454583697605535065465083366, + 0.109387158802297641899210590325805, + 0.123491976262065851077958109831074, + 0.134709217311473325928054001771707, + 0.142775938577060080797094273138717, + 0.147739104901338491374841515972068, + 0.149445554002916905664936468389821, + 0.147739104901338491374841515972068, + 0.142775938577060080797094273138717, + 0.134709217311473325928054001771707, + 0.123491976262065851077958109831074, + 0.109387158802297641899210590325805, + 0.093125454583697605535065465083366, + 0.075039674810919952767043140916190, + 0.054755896574351996031381300244580, + 0.032558162307964727478818972459390, + 0.011694638867371874278064396062192, + ], + dtype=self.xp.float64, + ) + elif self.npoints == 15: + nodes = self.xp.asarray( + [ + 0.991455371120812639206854697526329, + 0.949107912342758524526189684047851, + 0.864864423359769072789712788640926, + 0.741531185599394439863864773280788, + 0.586087235467691130294144838258730, + 0.405845151377397166906606412076961, + 0.207784955007898467600689403773245, + 0.000000000000000000000000000000000, + -0.207784955007898467600689403773245, + -0.405845151377397166906606412076961, + -0.586087235467691130294144838258730, + -0.741531185599394439863864773280788, + -0.864864423359769072789712788640926, + -0.949107912342758524526189684047851, + -0.991455371120812639206854697526329, + ], + dtype=self.xp.float64, + ) + + weights = self.xp.asarray( + [ + 0.022935322010529224963732008058970, + 0.063092092629978553290700663189204, + 0.104790010322250183839876322541518, + 0.140653259715525918745189590510238, + 0.169004726639267902826583426598550, + 0.190350578064785409913256402421014, + 0.204432940075298892414161999234649, + 0.209482141084727828012999174891714, + 0.204432940075298892414161999234649, + 0.190350578064785409913256402421014, + 0.169004726639267902826583426598550, + 0.140653259715525918745189590510238, + 0.104790010322250183839876322541518, + 0.063092092629978553290700663189204, + 0.022935322010529224963732008058970, + ], + dtype=self.xp.float64, + ) + + return nodes, weights + + @property + def lower_nodes_and_weights(self): + return self.gauss.nodes_and_weights diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_rules/_gauss_legendre.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_rules/_gauss_legendre.py new file mode 100644 index 0000000000000000000000000000000000000000..1163aec5370fb93951402ab99ee2ae4b79158d52 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_rules/_gauss_legendre.py @@ -0,0 +1,62 @@ +from scipy._lib._array_api import array_namespace, np_compat + +from functools import cached_property + +from scipy.special import roots_legendre + +from ._base import FixedRule + + +class GaussLegendreQuadrature(FixedRule): + """ + Gauss-Legendre quadrature. + + Parameters + ---------- + npoints : int + Number of nodes for the higher-order rule. + + xp : array_namespace, optional + The namespace for the node and weight arrays. Default is None, where NumPy is + used. + + Examples + -------- + Evaluate a 1D integral. Note in this example that ``f`` returns an array, so the + estimates will also be arrays. + + >>> import numpy as np + >>> from scipy.integrate import cubature + >>> from scipy.integrate._rules import GaussLegendreQuadrature + >>> def f(x): + ... return np.cos(x) + >>> rule = GaussLegendreQuadrature(21) # Use 21-point GaussLegendre + >>> a, b = np.array([0]), np.array([1]) + >>> rule.estimate(f, a, b) # True value sin(1), approximately 0.84147 + array([0.84147098]) + >>> rule.estimate_error(f, a, b) + array([1.11022302e-16]) + """ + + def __init__(self, npoints, xp=None): + if npoints < 2: + raise ValueError( + "At least 2 nodes required for Gauss-Legendre cubature" + ) + + self.npoints = npoints + + if xp is None: + xp = np_compat + + self.xp = array_namespace(xp.empty(0)) + + @cached_property + def nodes_and_weights(self): + # TODO: current converting to/from numpy + nodes, weights = roots_legendre(self.npoints) + + return ( + self.xp.asarray(nodes, dtype=self.xp.float64), + self.xp.asarray(weights, dtype=self.xp.float64) + ) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_rules/_genz_malik.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_rules/_genz_malik.py new file mode 100644 index 0000000000000000000000000000000000000000..4873805e3364b10a3366de47c15fe3c4b306e5d6 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_rules/_genz_malik.py @@ -0,0 +1,210 @@ +import math +import itertools + +from functools import cached_property + +from scipy._lib._array_api import array_namespace, np_compat + +from scipy.integrate._rules import NestedFixedRule + + +class GenzMalikCubature(NestedFixedRule): + """ + Genz-Malik cubature. + + Genz-Malik is only defined for integrals of dimension >= 2. + + Parameters + ---------- + ndim : int + The spatial dimension of the integrand. + + xp : array_namespace, optional + The namespace for the node and weight arrays. Default is None, where NumPy is + used. + + Attributes + ---------- + higher : Cubature + Higher-order rule. + + lower : Cubature + Lower-order rule. + + References + ---------- + .. [1] A.C. Genz, A.A. Malik, Remarks on algorithm 006: An adaptive algorithm for + numerical integration over an N-dimensional rectangular region, Journal of + Computational and Applied Mathematics, Volume 6, Issue 4, 1980, Pages 295-302, + ISSN 0377-0427, https://doi.org/10.1016/0771-050X(80)90039-X. + + Examples + -------- + Evaluate a 3D integral: + + >>> import numpy as np + >>> from scipy.integrate import cubature + >>> from scipy.integrate._rules import GenzMalikCubature + >>> def f(x): + ... # f(x) = cos(x_1) + cos(x_2) + cos(x_3) + ... return np.sum(np.cos(x), axis=-1) + >>> rule = GenzMalikCubature(3) # Use 3D Genz-Malik + >>> a, b = np.array([0, 0, 0]), np.array([1, 1, 1]) + >>> rule.estimate(f, a, b) # True value 3*sin(1), approximately 2.5244 + np.float64(2.5244129547230862) + >>> rule.estimate_error(f, a, b) + np.float64(1.378269656626685e-06) + """ + + def __init__(self, ndim, degree=7, lower_degree=5, xp=None): + if ndim < 2: + raise ValueError("Genz-Malik cubature is only defined for ndim >= 2") + + if degree != 7 or lower_degree != 5: + raise NotImplementedError("Genz-Malik cubature is currently only supported" + "for degree=7, lower_degree=5") + + self.ndim = ndim + self.degree = degree + self.lower_degree = lower_degree + + if xp is None: + xp = np_compat + + self.xp = array_namespace(xp.empty(0)) + + @cached_property + def nodes_and_weights(self): + # TODO: Currently only support for degree 7 Genz-Malik cubature, should aim to + # support arbitrary degree + l_2 = math.sqrt(9/70) + l_3 = math.sqrt(9/10) + l_4 = math.sqrt(9/10) + l_5 = math.sqrt(9/19) + + its = itertools.chain( + [(0,) * self.ndim], + _distinct_permutations((l_2,) + (0,) * (self.ndim - 1)), + _distinct_permutations((-l_2,) + (0,) * (self.ndim - 1)), + _distinct_permutations((l_3,) + (0,) * (self.ndim - 1)), + _distinct_permutations((-l_3,) + (0,) * (self.ndim - 1)), + _distinct_permutations((l_4, l_4) + (0,) * (self.ndim - 2)), + _distinct_permutations((l_4, -l_4) + (0,) * (self.ndim - 2)), + _distinct_permutations((-l_4, -l_4) + (0,) * (self.ndim - 2)), + itertools.product((l_5, -l_5), repeat=self.ndim), + ) + + nodes_size = 1 + (2 * (self.ndim + 1) * self.ndim) + 2**self.ndim + + nodes = self.xp.asarray( + list(zip(*its)), + dtype=self.xp.float64, + ) + + nodes = self.xp.reshape(nodes, (self.ndim, nodes_size)) + + # It's convenient to generate the nodes as a sequence of evaluation points + # as an array of shape (npoints, ndim), but nodes needs to have shape + # (ndim, npoints) + nodes = nodes.T + + w_1 = ( + (2**self.ndim) * (12824 - 9120*self.ndim + (400 * self.ndim**2)) / 19683 + ) + w_2 = (2**self.ndim) * 980/6561 + w_3 = (2**self.ndim) * (1820 - 400 * self.ndim) / 19683 + w_4 = (2**self.ndim) * (200 / 19683) + w_5 = 6859 / 19683 + + weights = self.xp.concat([ + self.xp.asarray([w_1] * 1, dtype=self.xp.float64), + self.xp.asarray([w_2] * (2 * self.ndim), dtype=self.xp.float64), + self.xp.asarray([w_3] * (2 * self.ndim), dtype=self.xp.float64), + self.xp.asarray( + [w_4] * (2 * (self.ndim - 1) * self.ndim), + dtype=self.xp.float64, + ), + self.xp.asarray([w_5] * (2**self.ndim), dtype=self.xp.float64), + ]) + + return nodes, weights + + @cached_property + def lower_nodes_and_weights(self): + # TODO: Currently only support for the degree 5 lower rule, in the future it + # would be worth supporting arbitrary degree + + # Nodes are almost the same as the full rule, but there are no nodes + # corresponding to l_5. + l_2 = math.sqrt(9/70) + l_3 = math.sqrt(9/10) + l_4 = math.sqrt(9/10) + + its = itertools.chain( + [(0,) * self.ndim], + _distinct_permutations((l_2,) + (0,) * (self.ndim - 1)), + _distinct_permutations((-l_2,) + (0,) * (self.ndim - 1)), + _distinct_permutations((l_3,) + (0,) * (self.ndim - 1)), + _distinct_permutations((-l_3,) + (0,) * (self.ndim - 1)), + _distinct_permutations((l_4, l_4) + (0,) * (self.ndim - 2)), + _distinct_permutations((l_4, -l_4) + (0,) * (self.ndim - 2)), + _distinct_permutations((-l_4, -l_4) + (0,) * (self.ndim - 2)), + ) + + nodes_size = 1 + (2 * (self.ndim + 1) * self.ndim) + + nodes = self.xp.asarray(list(zip(*its)), dtype=self.xp.float64) + nodes = self.xp.reshape(nodes, (self.ndim, nodes_size)) + nodes = nodes.T + + # Weights are different from those in the full rule. + w_1 = (2**self.ndim) * (729 - 950*self.ndim + 50*self.ndim**2) / 729 + w_2 = (2**self.ndim) * (245 / 486) + w_3 = (2**self.ndim) * (265 - 100*self.ndim) / 1458 + w_4 = (2**self.ndim) * (25 / 729) + + weights = self.xp.concat([ + self.xp.asarray([w_1] * 1, dtype=self.xp.float64), + self.xp.asarray([w_2] * (2 * self.ndim), dtype=self.xp.float64), + self.xp.asarray([w_3] * (2 * self.ndim), dtype=self.xp.float64), + self.xp.asarray( + [w_4] * (2 * (self.ndim - 1) * self.ndim), + dtype=self.xp.float64, + ), + ]) + + return nodes, weights + + +def _distinct_permutations(iterable): + """ + Find the number of distinct permutations of elements of `iterable`. + """ + + # Algorithm: https://w.wiki/Qai + + items = sorted(iterable) + size = len(items) + + while True: + # Yield the permutation we have + yield tuple(items) + + # Find the largest index i such that A[i] < A[i + 1] + for i in range(size - 2, -1, -1): + if items[i] < items[i + 1]: + break + + # If no such index exists, this permutation is the last one + else: + return + + # Find the largest index j greater than j such that A[i] < A[j] + for j in range(size - 1, i, -1): + if items[i] < items[j]: + break + + # Swap the value of A[i] with that of A[j], then reverse the + # sequence from A[i + 1] to form the new permutation + items[i], items[j] = items[j], items[i] + items[i+1:] = items[:i-size:-1] # A[i + 1:][::-1] diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_tanhsinh.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_tanhsinh.py new file mode 100644 index 0000000000000000000000000000000000000000..de1d844f88f999d96d4616d8060b0b47de1d8dbe --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_tanhsinh.py @@ -0,0 +1,1384 @@ +# mypy: disable-error-code="attr-defined" +import math +import numpy as np +from scipy import special +import scipy._lib._elementwise_iterative_method as eim +from scipy._lib._util import _RichResult +from scipy._lib._array_api import (array_namespace, xp_copy, xp_ravel, + xp_real, xp_take_along_axis) + + +__all__ = ['nsum'] + + +# todo: +# figure out warning situation +# address https://github.com/scipy/scipy/pull/18650#discussion_r1233032521 +# without `minweight`, we are also suppressing infinities within the interval. +# Is that OK? If so, we can probably get rid of `status=3`. +# Add heuristic to stop when improvement is too slow / antithrashing +# support singularities? interval subdivision? this feature will be added +# eventually, but do we adjust the interface now? +# When doing log-integration, should the tolerances control the error of the +# log-integral or the error of the integral? The trouble is that `log` +# inherently looses some precision so it may not be possible to refine +# the integral further. Example: 7th moment of stats.f(15, 20) +# respect function evaluation limit? +# make public? + + +def tanhsinh(f, a, b, *, args=(), log=False, maxlevel=None, minlevel=2, + atol=None, rtol=None, preserve_shape=False, callback=None): + """Evaluate a convergent integral numerically using tanh-sinh quadrature. + + In practice, tanh-sinh quadrature achieves quadratic convergence for + many integrands: the number of accurate *digits* scales roughly linearly + with the number of function evaluations [1]_. + + Either or both of the limits of integration may be infinite, and + singularities at the endpoints are acceptable. Divergent integrals and + integrands with non-finite derivatives or singularities within an interval + are out of scope, but the latter may be evaluated be calling `tanhsinh` on + each sub-interval separately. + + Parameters + ---------- + f : callable + The function to be integrated. The signature must be:: + + f(xi: ndarray, *argsi) -> ndarray + + where each element of ``xi`` is a finite real number and ``argsi`` is a tuple, + which may contain an arbitrary number of arrays that are broadcastable + with ``xi``. `f` must be an elementwise function: see documentation of parameter + `preserve_shape` for details. It must not mutate the array ``xi`` or the arrays + in ``argsi``. + If ``f`` returns a value with complex dtype when evaluated at + either endpoint, subsequent arguments ``x`` will have complex dtype + (but zero imaginary part). + a, b : float array_like + Real lower and upper limits of integration. Must be broadcastable with one + another and with arrays in `args`. Elements may be infinite. + args : tuple of array_like, optional + Additional positional array arguments to be passed to `f`. Arrays + must be broadcastable with one another and the arrays of `a` and `b`. + If the callable for which the root is desired requires arguments that are + not broadcastable with `x`, wrap that callable with `f` such that `f` + accepts only `x` and broadcastable ``*args``. + log : bool, default: False + Setting to True indicates that `f` returns the log of the integrand + and that `atol` and `rtol` are expressed as the logs of the absolute + and relative errors. In this case, the result object will contain the + log of the integral and error. This is useful for integrands for which + numerical underflow or overflow would lead to inaccuracies. + When ``log=True``, the integrand (the exponential of `f`) must be real, + but it may be negative, in which case the log of the integrand is a + complex number with an imaginary part that is an odd multiple of π. + maxlevel : int, default: 10 + The maximum refinement level of the algorithm. + + At the zeroth level, `f` is called once, performing 16 function + evaluations. At each subsequent level, `f` is called once more, + approximately doubling the number of function evaluations that have + been performed. Accordingly, for many integrands, each successive level + will double the number of accurate digits in the result (up to the + limits of floating point precision). + + The algorithm will terminate after completing level `maxlevel` or after + another termination condition is satisfied, whichever comes first. + minlevel : int, default: 2 + The level at which to begin iteration (default: 2). This does not + change the total number of function evaluations or the abscissae at + which the function is evaluated; it changes only the *number of times* + `f` is called. If ``minlevel=k``, then the integrand is evaluated at + all abscissae from levels ``0`` through ``k`` in a single call. + Note that if `minlevel` exceeds `maxlevel`, the provided `minlevel` is + ignored, and `minlevel` is set equal to `maxlevel`. + atol, rtol : float, optional + Absolute termination tolerance (default: 0) and relative termination + tolerance (default: ``eps**0.75``, where ``eps`` is the precision of + the result dtype), respectively. Iteration will stop when + ``res.error < atol`` or ``res.error < res.integral * rtol``. The error + estimate is as described in [1]_ Section 5 but with a lower bound of + ``eps * res.integral``. While not theoretically rigorous or + conservative, it is said to work well in practice. Must be non-negative + and finite if `log` is False, and must be expressed as the log of a + non-negative and finite number if `log` is True. + preserve_shape : bool, default: False + In the following, "arguments of `f`" refers to the array ``xi`` and + any arrays within ``argsi``. Let ``shape`` be the broadcasted shape + of `a`, `b`, and all elements of `args` (which is conceptually + distinct from ``xi` and ``argsi`` passed into `f`). + + - When ``preserve_shape=False`` (default), `f` must accept arguments + of *any* broadcastable shapes. + + - When ``preserve_shape=True``, `f` must accept arguments of shape + ``shape`` *or* ``shape + (n,)``, where ``(n,)`` is the number of + abscissae at which the function is being evaluated. + + In either case, for each scalar element ``xi[j]`` within ``xi``, the array + returned by `f` must include the scalar ``f(xi[j])`` at the same index. + Consequently, the shape of the output is always the shape of the input + ``xi``. + + See Examples. + + callback : callable, optional + An optional user-supplied function to be called before the first + iteration and after each iteration. + Called as ``callback(res)``, where ``res`` is a ``_RichResult`` + similar to that returned by `_differentiate` (but containing the + current iterate's values of all variables). If `callback` raises a + ``StopIteration``, the algorithm will terminate immediately and + `tanhsinh` will return a result object. `callback` must not mutate + `res` or its attributes. + + Returns + ------- + res : _RichResult + An object similar to an instance of `scipy.optimize.OptimizeResult` with the + following attributes. (The descriptions are written as though the values will + be scalars; however, if `f` returns an array, the outputs will be + arrays of the same shape.) + + success : bool array + ``True`` when the algorithm terminated successfully (status ``0``). + ``False`` otherwise. + status : int array + An integer representing the exit status of the algorithm. + + ``0`` : The algorithm converged to the specified tolerances. + ``-1`` : (unused) + ``-2`` : The maximum number of iterations was reached. + ``-3`` : A non-finite value was encountered. + ``-4`` : Iteration was terminated by `callback`. + ``1`` : The algorithm is proceeding normally (in `callback` only). + + integral : float array + An estimate of the integral. + error : float array + An estimate of the error. Only available if level two or higher + has been completed; otherwise NaN. + maxlevel : int array + The maximum refinement level used. + nfev : int array + The number of points at which `f` was evaluated. + + See Also + -------- + quad + + Notes + ----- + Implements the algorithm as described in [1]_ with minor adaptations for + finite-precision arithmetic, including some described by [2]_ and [3]_. The + tanh-sinh scheme was originally introduced in [4]_. + + Due to floating-point error in the abscissae, the function may be evaluated + at the endpoints of the interval during iterations, but the values returned by + the function at the endpoints will be ignored. + + References + ---------- + .. [1] Bailey, David H., Karthik Jeyabalan, and Xiaoye S. Li. "A comparison of + three high-precision quadrature schemes." Experimental Mathematics 14.3 + (2005): 317-329. + .. [2] Vanherck, Joren, Bart Sorée, and Wim Magnus. "Tanh-sinh quadrature for + single and multiple integration using floating-point arithmetic." + arXiv preprint arXiv:2007.15057 (2020). + .. [3] van Engelen, Robert A. "Improving the Double Exponential Quadrature + Tanh-Sinh, Sinh-Sinh and Exp-Sinh Formulas." + https://www.genivia.com/files/qthsh.pdf + .. [4] Takahasi, Hidetosi, and Masatake Mori. "Double exponential formulas for + numerical integration." Publications of the Research Institute for + Mathematical Sciences 9.3 (1974): 721-741. + + Examples + -------- + Evaluate the Gaussian integral: + + >>> import numpy as np + >>> from scipy.integrate import tanhsinh + >>> def f(x): + ... return np.exp(-x**2) + >>> res = tanhsinh(f, -np.inf, np.inf) + >>> res.integral # true value is np.sqrt(np.pi), 1.7724538509055159 + 1.7724538509055159 + >>> res.error # actual error is 0 + 4.0007963937534104e-16 + + The value of the Gaussian function (bell curve) is nearly zero for + arguments sufficiently far from zero, so the value of the integral + over a finite interval is nearly the same. + + >>> tanhsinh(f, -20, 20).integral + 1.772453850905518 + + However, with unfavorable integration limits, the integration scheme + may not be able to find the important region. + + >>> tanhsinh(f, -np.inf, 1000).integral + 4.500490856616431 + + In such cases, or when there are singularities within the interval, + break the integral into parts with endpoints at the important points. + + >>> tanhsinh(f, -np.inf, 0).integral + tanhsinh(f, 0, 1000).integral + 1.772453850905404 + + For integration involving very large or very small magnitudes, use + log-integration. (For illustrative purposes, the following example shows a + case in which both regular and log-integration work, but for more extreme + limits of integration, log-integration would avoid the underflow + experienced when evaluating the integral normally.) + + >>> res = tanhsinh(f, 20, 30, rtol=1e-10) + >>> res.integral, res.error + (4.7819613911309014e-176, 4.670364401645202e-187) + >>> def log_f(x): + ... return -x**2 + >>> res = tanhsinh(log_f, 20, 30, log=True, rtol=np.log(1e-10)) + >>> np.exp(res.integral), np.exp(res.error) + (4.7819613911306924e-176, 4.670364401645093e-187) + + The limits of integration and elements of `args` may be broadcastable + arrays, and integration is performed elementwise. + + >>> from scipy import stats + >>> dist = stats.gausshyper(13.8, 3.12, 2.51, 5.18) + >>> a, b = dist.support() + >>> x = np.linspace(a, b, 100) + >>> res = tanhsinh(dist.pdf, a, x) + >>> ref = dist.cdf(x) + >>> np.allclose(res.integral, ref) + True + + By default, `preserve_shape` is False, and therefore the callable + `f` may be called with arrays of any broadcastable shapes. + For example: + + >>> shapes = [] + >>> def f(x, c): + ... shape = np.broadcast_shapes(x.shape, c.shape) + ... shapes.append(shape) + ... return np.sin(c*x) + >>> + >>> c = [1, 10, 30, 100] + >>> res = tanhsinh(f, 0, 1, args=(c,), minlevel=1) + >>> shapes + [(4,), (4, 34), (4, 32), (3, 64), (2, 128), (1, 256)] + + To understand where these shapes are coming from - and to better + understand how `tanhsinh` computes accurate results - note that + higher values of ``c`` correspond with higher frequency sinusoids. + The higher frequency sinusoids make the integrand more complicated, + so more function evaluations are required to achieve the target + accuracy: + + >>> res.nfev + array([ 67, 131, 259, 515], dtype=int32) + + The initial ``shape``, ``(4,)``, corresponds with evaluating the + integrand at a single abscissa and all four frequencies; this is used + for input validation and to determine the size and dtype of the arrays + that store results. The next shape corresponds with evaluating the + integrand at an initial grid of abscissae and all four frequencies. + Successive calls to the function double the total number of abscissae at + which the function has been evaluated. However, in later function + evaluations, the integrand is evaluated at fewer frequencies because + the corresponding integral has already converged to the required + tolerance. This saves function evaluations to improve performance, but + it requires the function to accept arguments of any shape. + + "Vector-valued" integrands, such as those written for use with + `scipy.integrate.quad_vec`, are unlikely to satisfy this requirement. + For example, consider + + >>> def f(x): + ... return [x, np.sin(10*x), np.cos(30*x), x*np.sin(100*x)**2] + + This integrand is not compatible with `tanhsinh` as written; for instance, + the shape of the output will not be the same as the shape of ``x``. Such a + function *could* be converted to a compatible form with the introduction of + additional parameters, but this would be inconvenient. In such cases, + a simpler solution would be to use `preserve_shape`. + + >>> shapes = [] + >>> def f(x): + ... shapes.append(x.shape) + ... x0, x1, x2, x3 = x + ... return [x0, np.sin(10*x1), np.cos(30*x2), x3*np.sin(100*x3)] + >>> + >>> a = np.zeros(4) + >>> res = tanhsinh(f, a, 1, preserve_shape=True) + >>> shapes + [(4,), (4, 66), (4, 64), (4, 128), (4, 256)] + + Here, the broadcasted shape of `a` and `b` is ``(4,)``. With + ``preserve_shape=True``, the function may be called with argument + ``x`` of shape ``(4,)`` or ``(4, n)``, and this is what we observe. + + """ + maxfun = None # unused right now + (f, a, b, log, maxfun, maxlevel, minlevel, + atol, rtol, args, preserve_shape, callback, xp) = _tanhsinh_iv( + f, a, b, log, maxfun, maxlevel, minlevel, atol, + rtol, args, preserve_shape, callback) + + # Initialization + # `eim._initialize` does several important jobs, including + # ensuring that limits, each of the `args`, and the output of `f` + # broadcast correctly and are of consistent types. To save a function + # evaluation, I pass the midpoint of the integration interval. This comes + # at a cost of some gymnastics to ensure that the midpoint has the right + # shape and dtype. Did you know that 0d and >0d arrays follow different + # type promotion rules? + with np.errstate(over='ignore', invalid='ignore', divide='ignore'): + c = xp.reshape((xp_ravel(a) + xp_ravel(b))/2, a.shape) + inf_a, inf_b = xp.isinf(a), xp.isinf(b) + c[inf_a] = b[inf_a] - 1. # takes care of infinite a + c[inf_b] = a[inf_b] + 1. # takes care of infinite b + c[inf_a & inf_b] = 0. # takes care of infinite a and b + temp = eim._initialize(f, (c,), args, complex_ok=True, + preserve_shape=preserve_shape, xp=xp) + f, xs, fs, args, shape, dtype, xp = temp + a = xp_ravel(xp.astype(xp.broadcast_to(a, shape), dtype)) + b = xp_ravel(xp.astype(xp.broadcast_to(b, shape), dtype)) + + # Transform improper integrals + a, b, a0, negative, abinf, ainf, binf = _transform_integrals(a, b, xp) + + # Define variables we'll need + nit, nfev = 0, 1 # one function evaluation performed above + zero = -xp.inf if log else 0 + pi = xp.asarray(xp.pi, dtype=dtype)[()] + maxiter = maxlevel - minlevel + 1 + eps = xp.finfo(dtype).eps + if rtol is None: + rtol = 0.75*math.log(eps) if log else eps**0.75 + + Sn = xp_ravel(xp.full(shape, zero, dtype=dtype)) # latest integral estimate + Sn[xp.isnan(a) | xp.isnan(b) | xp.isnan(fs[0])] = xp.nan + Sk = xp.reshape(xp.empty_like(Sn), (-1, 1))[:, 0:0] # all integral estimates + aerr = xp_ravel(xp.full(shape, xp.nan, dtype=dtype)) # absolute error + status = xp_ravel(xp.full(shape, eim._EINPROGRESS, dtype=xp.int32)) + h0 = _get_base_step(dtype, xp) + h0 = xp_real(h0) # base step + + # For term `d4` of error estimate ([1] Section 5), we need to keep the + # most extreme abscissae and corresponding `fj`s, `wj`s in Euler-Maclaurin + # sum. Here, we initialize these variables. + xr0 = xp_ravel(xp.full(shape, -xp.inf, dtype=dtype)) + fr0 = xp_ravel(xp.full(shape, xp.nan, dtype=dtype)) + wr0 = xp_ravel(xp.zeros(shape, dtype=dtype)) + xl0 = xp_ravel(xp.full(shape, xp.inf, dtype=dtype)) + fl0 = xp_ravel(xp.full(shape, xp.nan, dtype=dtype)) + wl0 = xp_ravel(xp.zeros(shape, dtype=dtype)) + d4 = xp_ravel(xp.zeros(shape, dtype=dtype)) + + work = _RichResult( + Sn=Sn, Sk=Sk, aerr=aerr, h=h0, log=log, dtype=dtype, pi=pi, eps=eps, + a=xp.reshape(a, (-1, 1)), b=xp.reshape(b, (-1, 1)), # integration limits + n=minlevel, nit=nit, nfev=nfev, status=status, # iter/eval counts + xr0=xr0, fr0=fr0, wr0=wr0, xl0=xl0, fl0=fl0, wl0=wl0, d4=d4, # err est + ainf=ainf, binf=binf, abinf=abinf, a0=xp.reshape(a0, (-1, 1)), # transforms + # Store the xjc/wj pair cache in an object so they can't get compressed + # Using RichResult to allow dot notation, but a dictionary would suffice + pair_cache=_RichResult(xjc=None, wj=None, indices=[0], h0=None)) # pair cache + + # Constant scalars don't need to be put in `work` unless they need to be + # passed outside `tanhsinh`. Examples: atol, rtol, h0, minlevel. + + # Correspondence between terms in the `work` object and the result + res_work_pairs = [('status', 'status'), ('integral', 'Sn'), + ('error', 'aerr'), ('nit', 'nit'), ('nfev', 'nfev')] + + def pre_func_eval(work): + # Determine abscissae at which to evaluate `f` + work.h = h0 / 2**work.n + xjc, wj = _get_pairs(work.n, h0, dtype=work.dtype, + inclusive=(work.n == minlevel), xp=xp, work=work) + work.xj, work.wj = _transform_to_limits(xjc, wj, work.a, work.b, xp) + + # Perform abscissae substitutions for infinite limits of integration + xj = xp_copy(work.xj) + # use xp_real here to avoid cupy/cupy#8434 + xj[work.abinf] = xj[work.abinf] / (1 - xp_real(xj[work.abinf])**2) + xj[work.binf] = 1/xj[work.binf] - 1 + work.a0[work.binf] + xj[work.ainf] *= -1 + return xj + + def post_func_eval(x, fj, work): + # Weight integrand as required by substitutions for infinite limits + if work.log: + fj[work.abinf] += (xp.log(1 + work.xj[work.abinf]**2) + - 2*xp.log(1 - work.xj[work.abinf]**2)) + fj[work.binf] -= 2 * xp.log(work.xj[work.binf]) + else: + fj[work.abinf] *= ((1 + work.xj[work.abinf]**2) / + (1 - work.xj[work.abinf]**2)**2) + fj[work.binf] *= work.xj[work.binf]**-2. + + # Estimate integral with Euler-Maclaurin Sum + fjwj, Sn = _euler_maclaurin_sum(fj, work, xp) + if work.Sk.shape[-1]: + Snm1 = work.Sk[:, -1] + Sn = (special.logsumexp(xp.stack([Snm1 - math.log(2), Sn]), axis=0) if log + else Snm1 / 2 + Sn) + + work.fjwj = fjwj + work.Sn = Sn + + def check_termination(work): + """Terminate due to convergence or encountering non-finite values""" + stop = xp.zeros(work.Sn.shape, dtype=bool) + + # Terminate before first iteration if integration limits are equal + if work.nit == 0: + i = xp_ravel(work.a == work.b) # ravel singleton dimension + zero = xp.asarray(-xp.inf if log else 0.) + zero = xp.full(work.Sn.shape, zero, dtype=Sn.dtype) + zero[xp.isnan(Sn)] = xp.nan + work.Sn[i] = zero[i] + work.aerr[i] = zero[i] + work.status[i] = eim._ECONVERGED + stop[i] = True + else: + # Terminate if convergence criterion is met + rerr, aerr = _estimate_error(work, xp) + i = (rerr < rtol) | (aerr < atol) + work.aerr = xp.reshape(xp.astype(aerr, work.dtype), work.Sn.shape) + work.status[i] = eim._ECONVERGED + stop[i] = True + + # Terminate if integral estimate becomes invalid + if log: + Sn_real = xp_real(work.Sn) + Sn_pos_inf = xp.isinf(Sn_real) & (Sn_real > 0) + i = (Sn_pos_inf | xp.isnan(work.Sn)) & ~stop + else: + i = ~xp.isfinite(work.Sn) & ~stop + work.status[i] = eim._EVALUEERR + stop[i] = True + + return stop + + def post_termination_check(work): + work.n += 1 + work.Sk = xp.concat((work.Sk, work.Sn[:, xp.newaxis]), axis=-1) + return + + def customize_result(res, shape): + # If the integration limits were such that b < a, we reversed them + # to perform the calculation, and the final result needs to be negated. + if log and xp.any(negative): + dtype = res['integral'].dtype + pi = xp.asarray(xp.pi, dtype=dtype)[()] + j = xp.asarray(1j, dtype=xp.complex64)[()] # minimum complex type + res['integral'] = res['integral'] + negative*pi*j + else: + res['integral'][negative] *= -1 + + # For this algorithm, it seems more appropriate to report the maximum + # level rather than the number of iterations in which it was performed. + res['maxlevel'] = minlevel + res['nit'] - 1 + res['maxlevel'][res['nit'] == 0] = -1 + del res['nit'] + return shape + + # Suppress all warnings initially, since there are many places in the code + # for which this is expected behavior. + with np.errstate(over='ignore', invalid='ignore', divide='ignore'): + res = eim._loop(work, callback, shape, maxiter, f, args, dtype, pre_func_eval, + post_func_eval, check_termination, post_termination_check, + customize_result, res_work_pairs, xp, preserve_shape) + return res + + +def _get_base_step(dtype, xp): + # Compute the base step length for the provided dtype. Theoretically, the + # Euler-Maclaurin sum is infinite, but it gets cut off when either the + # weights underflow or the abscissae cannot be distinguished from the + # limits of integration. The latter happens to occur first for float32 and + # float64, and it occurs when `xjc` (the abscissa complement) + # in `_compute_pair` underflows. We can solve for the argument `tmax` at + # which it will underflow using [2] Eq. 13. + fmin = 4*xp.finfo(dtype).smallest_normal # stay a little away from the limit + tmax = math.asinh(math.log(2/fmin - 1) / xp.pi) + + # Based on this, we can choose a base step size `h` for level 0. + # The number of function evaluations will be `2 + m*2^(k+1)`, where `k` is + # the level and `m` is an integer we get to choose. I choose + # m = _N_BASE_STEPS = `8` somewhat arbitrarily, but a rationale is that a + # power of 2 makes floating point arithmetic more predictable. It also + # results in a base step size close to `1`, which is what [1] uses (and I + # used here until I found [2] and these ideas settled). + h0 = tmax / _N_BASE_STEPS + return xp.asarray(h0, dtype=dtype)[()] + + +_N_BASE_STEPS = 8 + + +def _compute_pair(k, h0, xp): + # Compute the abscissa-weight pairs for each level k. See [1] page 9. + + # For now, we compute and store in 64-bit precision. If higher-precision + # data types become better supported, it would be good to compute these + # using the highest precision available. Or, once there is an Array API- + # compatible arbitrary precision array, we can compute at the required + # precision. + + # "....each level k of abscissa-weight pairs uses h = 2 **-k" + # We adapt to floating point arithmetic using ideas of [2]. + h = h0 / 2**k + max = _N_BASE_STEPS * 2**k + + # For iterations after the first, "....the integrand function needs to be + # evaluated only at the odd-indexed abscissas at each level." + j = xp.arange(max+1) if k == 0 else xp.arange(1, max+1, 2) + jh = j * h + + # "In this case... the weights wj = u1/cosh(u2)^2, where..." + pi_2 = xp.pi / 2 + u1 = pi_2*xp.cosh(jh) + u2 = pi_2*xp.sinh(jh) + # Denominators get big here. Overflow then underflow doesn't need warning. + # with np.errstate(under='ignore', over='ignore'): + wj = u1 / xp.cosh(u2)**2 + # "We actually store 1-xj = 1/(...)." + xjc = 1 / (xp.exp(u2) * xp.cosh(u2)) # complement of xj = xp.tanh(u2) + + # When level k == 0, the zeroth xj corresponds with xj = 0. To simplify + # code, the function will be evaluated there twice; each gets half weight. + wj[0] = wj[0] / 2 if k == 0 else wj[0] + + return xjc, wj # store at full precision + + +def _pair_cache(k, h0, xp, work): + # Cache the abscissa-weight pairs up to a specified level. + # Abscissae and weights of consecutive levels are concatenated. + # `index` records the indices that correspond with each level: + # `xjc[index[k]:index[k+1]` extracts the level `k` abscissae. + if not isinstance(h0, type(work.pair_cache.h0)) or h0 != work.pair_cache.h0: + work.pair_cache.xjc = xp.empty(0) + work.pair_cache.wj = xp.empty(0) + work.pair_cache.indices = [0] + + xjcs = [work.pair_cache.xjc] + wjs = [work.pair_cache.wj] + + for i in range(len(work.pair_cache.indices)-1, k + 1): + xjc, wj = _compute_pair(i, h0, xp) + xjcs.append(xjc) + wjs.append(wj) + work.pair_cache.indices.append(work.pair_cache.indices[-1] + xjc.shape[0]) + + work.pair_cache.xjc = xp.concat(xjcs) + work.pair_cache.wj = xp.concat(wjs) + work.pair_cache.h0 = h0 + + +def _get_pairs(k, h0, inclusive, dtype, xp, work): + # Retrieve the specified abscissa-weight pairs from the cache + # If `inclusive`, return all up to and including the specified level + if (len(work.pair_cache.indices) <= k+2 + or not isinstance (h0, type(work.pair_cache.h0)) + or h0 != work.pair_cache.h0): + _pair_cache(k, h0, xp, work) + + xjc = work.pair_cache.xjc + wj = work.pair_cache.wj + indices = work.pair_cache.indices + + start = 0 if inclusive else indices[k] + end = indices[k+1] + + return xp.astype(xjc[start:end], dtype), xp.astype(wj[start:end], dtype) + + +def _transform_to_limits(xjc, wj, a, b, xp): + # Transform integral according to user-specified limits. This is just + # math that follows from the fact that the standard limits are (-1, 1). + # Note: If we had stored xj instead of xjc, we would have + # xj = alpha * xj + beta, where beta = (a + b)/2 + alpha = (b - a) / 2 + xj = xp.concat((-alpha * xjc + b, alpha * xjc + a), axis=-1) + wj = wj*alpha # arguments get broadcasted, so we can't use *= + wj = xp.concat((wj, wj), axis=-1) + + # Points at the boundaries can be generated due to finite precision + # arithmetic, but these function values aren't supposed to be included in + # the Euler-Maclaurin sum. Ideally we wouldn't evaluate the function at + # these points; however, we can't easily filter out points since this + # function is vectorized. Instead, zero the weights. + # Note: values may have complex dtype, but have zero imaginary part + xj_real, a_real, b_real = xp_real(xj), xp_real(a), xp_real(b) + invalid = (xj_real <= a_real) | (xj_real >= b_real) + wj[invalid] = 0 + return xj, wj + + +def _euler_maclaurin_sum(fj, work, xp): + # Perform the Euler-Maclaurin Sum, [1] Section 4 + + # The error estimate needs to know the magnitude of the last term + # omitted from the Euler-Maclaurin sum. This is a bit involved because + # it may have been computed at a previous level. I sure hope it's worth + # all the trouble. + xr0, fr0, wr0 = work.xr0, work.fr0, work.wr0 + xl0, fl0, wl0 = work.xl0, work.fl0, work.wl0 + + # It is much more convenient to work with the transposes of our work + # variables here. + xj, fj, wj = work.xj.T, fj.T, work.wj.T + n_x, n_active = xj.shape # number of abscissae, number of active elements + + # We'll work with the left and right sides separately + xr, xl = xp_copy(xp.reshape(xj, (2, n_x // 2, n_active))) # this gets modified + fr, fl = xp.reshape(fj, (2, n_x // 2, n_active)) + wr, wl = xp.reshape(wj, (2, n_x // 2, n_active)) + + invalid_r = ~xp.isfinite(fr) | (wr == 0) + invalid_l = ~xp.isfinite(fl) | (wl == 0) + + # integer index of the maximum abscissa at this level + xr[invalid_r] = -xp.inf + ir = xp.argmax(xp_real(xr), axis=0, keepdims=True) + # abscissa, function value, and weight at this index + ### Not Array API Compatible... yet ### + xr_max = xp_take_along_axis(xr, ir, axis=0)[0] + fr_max = xp_take_along_axis(fr, ir, axis=0)[0] + wr_max = xp_take_along_axis(wr, ir, axis=0)[0] + # boolean indices at which maximum abscissa at this level exceeds + # the incumbent maximum abscissa (from all previous levels) + # note: abscissa may have complex dtype, but will have zero imaginary part + j = xp_real(xr_max) > xp_real(xr0) + # Update record of the incumbent abscissa, function value, and weight + xr0[j] = xr_max[j] + fr0[j] = fr_max[j] + wr0[j] = wr_max[j] + + # integer index of the minimum abscissa at this level + xl[invalid_l] = xp.inf + il = xp.argmin(xp_real(xl), axis=0, keepdims=True) + # abscissa, function value, and weight at this index + xl_min = xp_take_along_axis(xl, il, axis=0)[0] + fl_min = xp_take_along_axis(fl, il, axis=0)[0] + wl_min = xp_take_along_axis(wl, il, axis=0)[0] + # boolean indices at which minimum abscissa at this level is less than + # the incumbent minimum abscissa (from all previous levels) + # note: abscissa may have complex dtype, but will have zero imaginary part + j = xp_real(xl_min) < xp_real(xl0) + # Update record of the incumbent abscissa, function value, and weight + xl0[j] = xl_min[j] + fl0[j] = fl_min[j] + wl0[j] = wl_min[j] + fj = fj.T + + # Compute the error estimate `d4` - the magnitude of the leftmost or + # rightmost term, whichever is greater. + flwl0 = fl0 + xp.log(wl0) if work.log else fl0 * wl0 # leftmost term + frwr0 = fr0 + xp.log(wr0) if work.log else fr0 * wr0 # rightmost term + magnitude = xp_real if work.log else xp.abs + work.d4 = xp.maximum(magnitude(flwl0), magnitude(frwr0)) + + # There are two approaches to dealing with function values that are + # numerically infinite due to approaching a singularity - zero them, or + # replace them with the function value at the nearest non-infinite point. + # [3] pg. 22 suggests the latter, so let's do that given that we have the + # information. + fr0b = xp.broadcast_to(fr0[xp.newaxis, :], fr.shape) + fl0b = xp.broadcast_to(fl0[xp.newaxis, :], fl.shape) + fr[invalid_r] = fr0b[invalid_r] + fl[invalid_l] = fl0b[invalid_l] + + # When wj is zero, log emits a warning + # with np.errstate(divide='ignore'): + fjwj = fj + xp.log(work.wj) if work.log else fj * work.wj + + # update integral estimate + Sn = (special.logsumexp(fjwj + xp.log(work.h), axis=-1) if work.log + else xp.sum(fjwj, axis=-1) * work.h) + + work.xr0, work.fr0, work.wr0 = xr0, fr0, wr0 + work.xl0, work.fl0, work.wl0 = xl0, fl0, wl0 + + return fjwj, Sn + + +def _estimate_error(work, xp): + # Estimate the error according to [1] Section 5 + + if work.n == 0 or work.nit == 0: + # The paper says to use "one" as the error before it can be calculated. + # NaN seems to be more appropriate. + nan = xp.full_like(work.Sn, xp.nan) + return nan, nan + + indices = work.pair_cache.indices + + n_active = work.Sn.shape[0] # number of active elements + axis_kwargs = dict(axis=-1, keepdims=True) + + # With a jump start (starting at level higher than 0), we haven't + # explicitly calculated the integral estimate at lower levels. But we have + # all the function value-weight products, so we can compute the + # lower-level estimates. + if work.Sk.shape[-1] == 0: + h = 2 * work.h # step size at this level + n_x = indices[work.n] # number of abscissa up to this level + # The right and left fjwj terms from all levels are concatenated along + # the last axis. Get out only the terms up to this level. + fjwj_rl = xp.reshape(work.fjwj, (n_active, 2, -1)) + fjwj = xp.reshape(fjwj_rl[:, :, :n_x], (n_active, 2*n_x)) + # Compute the Euler-Maclaurin sum at this level + Snm1 = (special.logsumexp(fjwj, **axis_kwargs) + xp.log(h) if work.log + else xp.sum(fjwj, **axis_kwargs) * h) + work.Sk = xp.concat((Snm1, work.Sk), axis=-1) + + if work.n == 1: + nan = xp.full_like(work.Sn, xp.nan) + return nan, nan + + # The paper says not to calculate the error for n<=2, but it's not clear + # about whether it starts at level 0 or level 1. We start at level 0, so + # why not compute the error beginning in level 2? + if work.Sk.shape[-1] < 2: + h = 4 * work.h # step size at this level + n_x = indices[work.n-1] # number of abscissa up to this level + # The right and left fjwj terms from all levels are concatenated along + # the last axis. Get out only the terms up to this level. + fjwj_rl = xp.reshape(work.fjwj, (work.Sn.shape[0], 2, -1)) + fjwj = xp.reshape(fjwj_rl[..., :n_x], (n_active, 2*n_x)) + # Compute the Euler-Maclaurin sum at this level + Snm2 = (special.logsumexp(fjwj, **axis_kwargs) + xp.log(h) if work.log + else xp.sum(fjwj, **axis_kwargs) * h) + work.Sk = xp.concat((Snm2, work.Sk), axis=-1) + + Snm2 = work.Sk[..., -2] + Snm1 = work.Sk[..., -1] + + e1 = xp.asarray(work.eps)[()] + + if work.log: + log_e1 = xp.log(e1) + # Currently, only real integrals are supported in log-scale. All + # complex values have imaginary part in increments of pi*j, which just + # carries sign information of the original integral, so use of + # `xp.real` here is equivalent to absolute value in real scale. + d1 = xp_real(special.logsumexp(xp.stack([work.Sn, Snm1 + work.pi*1j]), axis=0)) + d2 = xp_real(special.logsumexp(xp.stack([work.Sn, Snm2 + work.pi*1j]), axis=0)) + d3 = log_e1 + xp.max(xp_real(work.fjwj), axis=-1) + d4 = work.d4 + d5 = log_e1 + xp.real(work.Sn) + temp = xp.where(d1 > -xp.inf, d1 ** 2 / d2, -xp.inf) + ds = xp.stack([temp, 2 * d1, d3, d4, d5]) + aerr = xp.max(ds, axis=0) + rerr = aerr - xp.real(work.Sn) + else: + # Note: explicit computation of log10 of each of these is unnecessary. + d1 = xp.abs(work.Sn - Snm1) + d2 = xp.abs(work.Sn - Snm2) + d3 = e1 * xp.max(xp.abs(work.fjwj), axis=-1) + d4 = work.d4 + d5 = e1 * xp.abs(work.Sn) + temp = xp.where(d1 > 0, d1**(xp.log(d1)/xp.log(d2)), 0) + ds = xp.stack([temp, d1**2, d3, d4, d5]) + aerr = xp.max(ds, axis=0) + rerr = aerr/xp.abs(work.Sn) + + return rerr, aerr + + +def _transform_integrals(a, b, xp): + # Transform integrals to a form with finite a <= b + # For b == a (even infinite), we ensure that the limits remain equal + # For b < a, we reverse the limits and will multiply the final result by -1 + # For infinite limit on the right, we use the substitution x = 1/t - 1 + a + # For infinite limit on the left, we substitute x = -x and treat as above + # For infinite limits, we substitute x = t / (1-t**2) + ab_same = (a == b) + a[ab_same], b[ab_same] = 1, 1 + + # `a, b` may have complex dtype but have zero imaginary part + negative = xp_real(b) < xp_real(a) + a[negative], b[negative] = b[negative], a[negative] + + abinf = xp.isinf(a) & xp.isinf(b) + a[abinf], b[abinf] = -1, 1 + + ainf = xp.isinf(a) + a[ainf], b[ainf] = -b[ainf], -a[ainf] + + binf = xp.isinf(b) + a0 = xp_copy(a) + a[binf], b[binf] = 0, 1 + + return a, b, a0, negative, abinf, ainf, binf + + +def _tanhsinh_iv(f, a, b, log, maxfun, maxlevel, minlevel, + atol, rtol, args, preserve_shape, callback): + # Input validation and standardization + + xp = array_namespace(a, b) + + message = '`f` must be callable.' + if not callable(f): + raise ValueError(message) + + message = 'All elements of `a` and `b` must be real numbers.' + a, b = xp.asarray(a), xp.asarray(b) + a, b = xp.broadcast_arrays(a, b) + if (xp.isdtype(a.dtype, 'complex floating') + or xp.isdtype(b.dtype, 'complex floating')): + raise ValueError(message) + + message = '`log` must be True or False.' + if log not in {True, False}: + raise ValueError(message) + log = bool(log) + + if atol is None: + atol = -xp.inf if log else 0 + + rtol_temp = rtol if rtol is not None else 0. + + # using NumPy for convenience here; these are just floats, not arrays + params = np.asarray([atol, rtol_temp, 0.]) + message = "`atol` and `rtol` must be real numbers." + if not np.issubdtype(params.dtype, np.floating): + raise ValueError(message) + + if log: + message = '`atol` and `rtol` may not be positive infinity.' + if np.any(np.isposinf(params)): + raise ValueError(message) + else: + message = '`atol` and `rtol` must be non-negative and finite.' + if np.any(params < 0) or np.any(np.isinf(params)): + raise ValueError(message) + atol = params[0] + rtol = rtol if rtol is None else params[1] + + BIGINT = float(2**62) + if maxfun is None and maxlevel is None: + maxlevel = 10 + + maxfun = BIGINT if maxfun is None else maxfun + maxlevel = BIGINT if maxlevel is None else maxlevel + + message = '`maxfun`, `maxlevel`, and `minlevel` must be integers.' + params = np.asarray([maxfun, maxlevel, minlevel]) + if not (np.issubdtype(params.dtype, np.number) + and np.all(np.isreal(params)) + and np.all(params.astype(np.int64) == params)): + raise ValueError(message) + message = '`maxfun`, `maxlevel`, and `minlevel` must be non-negative.' + if np.any(params < 0): + raise ValueError(message) + maxfun, maxlevel, minlevel = params.astype(np.int64) + minlevel = min(minlevel, maxlevel) + + if not np.iterable(args): + args = (args,) + args = (xp.asarray(arg) for arg in args) + + message = '`preserve_shape` must be True or False.' + if preserve_shape not in {True, False}: + raise ValueError(message) + + if callback is not None and not callable(callback): + raise ValueError('`callback` must be callable.') + + return (f, a, b, log, maxfun, maxlevel, minlevel, + atol, rtol, args, preserve_shape, callback, xp) + + +def _nsum_iv(f, a, b, step, args, log, maxterms, tolerances): + # Input validation and standardization + + xp = array_namespace(a, b) + + message = '`f` must be callable.' + if not callable(f): + raise ValueError(message) + + message = 'All elements of `a`, `b`, and `step` must be real numbers.' + a, b, step = xp.broadcast_arrays(xp.asarray(a), xp.asarray(b), xp.asarray(step)) + dtype = xp.result_type(a.dtype, b.dtype, step.dtype) + if not xp.isdtype(dtype, 'numeric') or xp.isdtype(dtype, 'complex floating'): + raise ValueError(message) + + valid_b = b >= a # NaNs will be False + valid_step = xp.isfinite(step) & (step > 0) + valid_abstep = valid_b & valid_step + + message = '`log` must be True or False.' + if log not in {True, False}: + raise ValueError(message) + + tolerances = {} if tolerances is None else tolerances + + atol = tolerances.get('atol', None) + if atol is None: + atol = -xp.inf if log else 0 + + rtol = tolerances.get('rtol', None) + rtol_temp = rtol if rtol is not None else 0. + + # using NumPy for convenience here; these are just floats, not arrays + params = np.asarray([atol, rtol_temp, 0.]) + message = "`atol` and `rtol` must be real numbers." + if not np.issubdtype(params.dtype, np.floating): + raise ValueError(message) + + if log: + message = '`atol`, `rtol` may not be positive infinity or NaN.' + if np.any(np.isposinf(params) | np.isnan(params)): + raise ValueError(message) + else: + message = '`atol`, and `rtol` must be non-negative and finite.' + if np.any((params < 0) | (~np.isfinite(params))): + raise ValueError(message) + atol = params[0] + rtol = rtol if rtol is None else params[1] + + maxterms_int = int(maxterms) + if maxterms_int != maxterms or maxterms < 0: + message = "`maxterms` must be a non-negative integer." + raise ValueError(message) + + if not np.iterable(args): + args = (args,) + + return f, a, b, step, valid_abstep, args, log, maxterms_int, atol, rtol, xp + + +def nsum(f, a, b, *, step=1, args=(), log=False, maxterms=int(2**20), tolerances=None): + r"""Evaluate a convergent finite or infinite series. + + For finite `a` and `b`, this evaluates:: + + f(a + np.arange(n)*step).sum() + + where ``n = int((b - a) / step) + 1``, where `f` is smooth, positive, and + unimodal. The number of terms in the sum may be very large or infinite, + in which case a partial sum is evaluated directly and the remainder is + approximated using integration. + + Parameters + ---------- + f : callable + The function that evaluates terms to be summed. The signature must be:: + + f(x: ndarray, *args) -> ndarray + + where each element of ``x`` is a finite real and ``args`` is a tuple, + which may contain an arbitrary number of arrays that are broadcastable + with ``x``. + + `f` must be an elementwise function: each element ``f(x)[i]`` + must equal ``f(x[i])`` for all indices ``i``. It must not mutate the + array ``x`` or the arrays in ``args``, and it must return NaN where + the argument is NaN. + + `f` must represent a smooth, positive, unimodal function of `x` defined at + *all reals* between `a` and `b`. + a, b : float array_like + Real lower and upper limits of summed terms. Must be broadcastable. + Each element of `a` must be less than the corresponding element in `b`. + step : float array_like + Finite, positive, real step between summed terms. Must be broadcastable + with `a` and `b`. Note that the number of terms included in the sum will + be ``floor((b - a) / step)`` + 1; adjust `b` accordingly to ensure + that ``f(b)`` is included if intended. + args : tuple of array_like, optional + Additional positional arguments to be passed to `f`. Must be arrays + broadcastable with `a`, `b`, and `step`. If the callable to be summed + requires arguments that are not broadcastable with `a`, `b`, and `step`, + wrap that callable with `f` such that `f` accepts only `x` and + broadcastable ``*args``. See Examples. + log : bool, default: False + Setting to True indicates that `f` returns the log of the terms + and that `atol` and `rtol` are expressed as the logs of the absolute + and relative errors. In this case, the result object will contain the + log of the sum and error. This is useful for summands for which + numerical underflow or overflow would lead to inaccuracies. + maxterms : int, default: 2**20 + The maximum number of terms to evaluate for direct summation. + Additional function evaluations may be performed for input + validation and integral evaluation. + atol, rtol : float, optional + Absolute termination tolerance (default: 0) and relative termination + tolerance (default: ``eps**0.5``, where ``eps`` is the precision of + the result dtype), respectively. Must be non-negative + and finite if `log` is False, and must be expressed as the log of a + non-negative and finite number if `log` is True. + + Returns + ------- + res : _RichResult + An object similar to an instance of `scipy.optimize.OptimizeResult` with the + following attributes. (The descriptions are written as though the values will + be scalars; however, if `f` returns an array, the outputs will be + arrays of the same shape.) + + success : bool + ``True`` when the algorithm terminated successfully (status ``0``); + ``False`` otherwise. + status : int array + An integer representing the exit status of the algorithm. + + - ``0`` : The algorithm converged to the specified tolerances. + - ``-1`` : Element(s) of `a`, `b`, or `step` are invalid + - ``-2`` : Numerical integration reached its iteration limit; + the sum may be divergent. + - ``-3`` : A non-finite value was encountered. + - ``-4`` : The magnitude of the last term of the partial sum exceeds + the tolerances, so the error estimate exceeds the tolerances. + Consider increasing `maxterms` or loosening `tolerances`. + Alternatively, the callable may not be unimodal, or the limits of + summation may be too far from the function maximum. Consider + increasing `maxterms` or breaking the sum into pieces. + + sum : float array + An estimate of the sum. + error : float array + An estimate of the absolute error, assuming all terms are non-negative, + the function is computed exactly, and direct summation is accurate to + the precision of the result dtype. + nfev : int array + The number of points at which `f` was evaluated. + + See Also + -------- + mpmath.nsum + + Notes + ----- + The method implemented for infinite summation is related to the integral + test for convergence of an infinite series: assuming `step` size 1 for + simplicity of exposition, the sum of a monotone decreasing function is bounded by + + .. math:: + + \int_u^\infty f(x) dx \leq \sum_{k=u}^\infty f(k) \leq \int_u^\infty f(x) dx + f(u) + + Let :math:`a` represent `a`, :math:`n` represent `maxterms`, :math:`\epsilon_a` + represent `atol`, and :math:`\epsilon_r` represent `rtol`. + The implementation first evaluates the integral :math:`S_l=\int_a^\infty f(x) dx` + as a lower bound of the infinite sum. Then, it seeks a value :math:`c > a` such + that :math:`f(c) < \epsilon_a + S_l \epsilon_r`, if it exists; otherwise, + let :math:`c = a + n`. Then the infinite sum is approximated as + + .. math:: + + \sum_{k=a}^{c-1} f(k) + \int_c^\infty f(x) dx + f(c)/2 + + and the reported error is :math:`f(c)/2` plus the error estimate of + numerical integration. Note that the integral approximations may require + evaluation of the function at points besides those that appear in the sum, + so `f` must be a continuous and monotonically decreasing function defined + for all reals within the integration interval. However, due to the nature + of the integral approximation, the shape of the function between points + that appear in the sum has little effect. If there is not a natural + extension of the function to all reals, consider using linear interpolation, + which is easy to evaluate and preserves monotonicity. + + The approach described above is generalized for non-unit + `step` and finite `b` that is too large for direct evaluation of the sum, + i.e. ``b - a + 1 > maxterms``. It is further generalized to unimodal + functions by directly summing terms surrounding the maximum. + This strategy may fail: + + - If the left limit is finite and the maximum is far from it. + - If the right limit is finite and the maximum is far from it. + - If both limits are finite and the maximum is far from the origin. + + In these cases, accuracy may be poor, and `nsum` may return status code ``4``. + + Although the callable `f` must be non-negative and unimodal, + `nsum` can be used to evaluate more general forms of series. For instance, to + evaluate an alternating series, pass a callable that returns the difference + between pairs of adjacent terms, and adjust `step` accordingly. See Examples. + + References + ---------- + .. [1] Wikipedia. "Integral test for convergence." + https://en.wikipedia.org/wiki/Integral_test_for_convergence + + Examples + -------- + Compute the infinite sum of the reciprocals of squared integers. + + >>> import numpy as np + >>> from scipy.integrate import nsum + >>> res = nsum(lambda k: 1/k**2, 1, np.inf) + >>> ref = np.pi**2/6 # true value + >>> res.error # estimated error + np.float64(7.448762306416137e-09) + >>> (res.sum - ref)/ref # true error + np.float64(-1.839871898894426e-13) + >>> res.nfev # number of points at which callable was evaluated + np.int32(8561) + + Compute the infinite sums of the reciprocals of integers raised to powers ``p``, + where ``p`` is an array. + + >>> from scipy import special + >>> p = np.arange(3, 10) + >>> res = nsum(lambda k, p: 1/k**p, 1, np.inf, maxterms=1e3, args=(p,)) + >>> ref = special.zeta(p, 1) + >>> np.allclose(res.sum, ref) + True + + Evaluate the alternating harmonic series. + + >>> res = nsum(lambda x: 1/x - 1/(x+1), 1, np.inf, step=2) + >>> res.sum, res.sum - np.log(2) # result, difference vs analytical sum + (np.float64(0.6931471805598691), np.float64(-7.616129948928574e-14)) + + """ # noqa: E501 + # Potential future work: + # - improve error estimate of `_direct` sum + # - add other methods for convergence acceleration (Richardson, epsilon) + # - support negative monotone increasing functions? + # - b < a / negative step? + # - complex-valued function? + # - check for violations of monotonicity? + + # Function-specific input validation / standardization + tmp = _nsum_iv(f, a, b, step, args, log, maxterms, tolerances) + f, a, b, step, valid_abstep, args, log, maxterms, atol, rtol, xp = tmp + + # Additional elementwise algorithm input validation / standardization + tmp = eim._initialize(f, (a,), args, complex_ok=False, xp=xp) + f, xs, fs, args, shape, dtype, xp = tmp + + # Finish preparing `a`, `b`, and `step` arrays + a = xs[0] + b = xp.astype(xp_ravel(xp.broadcast_to(b, shape)), dtype) + step = xp.astype(xp_ravel(xp.broadcast_to(step, shape)), dtype) + valid_abstep = xp_ravel(xp.broadcast_to(valid_abstep, shape)) + nterms = xp.floor((b - a) / step) + finite_terms = xp.isfinite(nterms) + b[finite_terms] = a[finite_terms] + nterms[finite_terms]*step[finite_terms] + + # Define constants + eps = xp.finfo(dtype).eps + zero = xp.asarray(-xp.inf if log else 0, dtype=dtype)[()] + if rtol is None: + rtol = 0.5*math.log(eps) if log else eps**0.5 + constants = (dtype, log, eps, zero, rtol, atol, maxterms) + + # Prepare result arrays + S = xp.empty_like(a) + E = xp.empty_like(a) + status = xp.zeros(len(a), dtype=xp.int32) + nfev = xp.ones(len(a), dtype=xp.int32) # one function evaluation above + + # Branch for direct sum evaluation / integral approximation / invalid input + i0 = ~valid_abstep # invalid + i1 = (nterms + 1 <= maxterms) & ~i0 # direct sum evaluation + i2 = xp.isfinite(a) & ~i1 & ~i0 # infinite sum to the right + i3 = xp.isfinite(b) & ~i2 & ~i1 & ~i0 # infinite sum to the left + i4 = ~i3 & ~i2 & ~i1 & ~i0 # infinite sum on both sides + + if xp.any(i0): + S[i0], E[i0] = xp.nan, xp.nan + status[i0] = -1 + + if xp.any(i1): + args_direct = [arg[i1] for arg in args] + tmp = _direct(f, a[i1], b[i1], step[i1], args_direct, constants, xp) + S[i1], E[i1] = tmp[:-1] + nfev[i1] += tmp[-1] + status[i1] = -3 * xp.asarray(~xp.isfinite(S[i1]), dtype=xp.int32) + + if xp.any(i2): + args_indirect = [arg[i2] for arg in args] + tmp = _integral_bound(f, a[i2], b[i2], step[i2], + args_indirect, constants, xp) + S[i2], E[i2], status[i2] = tmp[:-1] + nfev[i2] += tmp[-1] + + if xp.any(i3): + args_indirect = [arg[i3] for arg in args] + def _f(x, *args): return f(-x, *args) + tmp = _integral_bound(_f, -b[i3], -a[i3], step[i3], + args_indirect, constants, xp) + S[i3], E[i3], status[i3] = tmp[:-1] + nfev[i3] += tmp[-1] + + if xp.any(i4): + args_indirect = [arg[i4] for arg in args] + + # There are two obvious high-level strategies: + # - Do two separate half-infinite sums (e.g. from -inf to 0 and 1 to inf) + # - Make a callable that returns f(x) + f(-x) and do a single half-infinite sum + # I thought the latter would have about half the overhead, so I went that way. + # Then there are two ways of ensuring that f(0) doesn't get counted twice. + # - Evaluate the sum from 1 to inf and add f(0) + # - Evaluate the sum from 0 to inf and subtract f(0) + # - Evaluate the sum from 0 to inf, but apply a weight of 0.5 when `x = 0` + # The last option has more overhead, but is simpler to implement correctly + # (especially getting the status message right) + if log: + def _f(x, *args): + log_factor = xp.where(x==0, math.log(0.5), 0) + out = xp.stack([f(x, *args), f(-x, *args)], axis=0) + return special.logsumexp(out, axis=0) + log_factor + + else: + def _f(x, *args): + factor = xp.where(x==0, 0.5, 1) + return (f(x, *args) + f(-x, *args)) * factor + + zero = xp.zeros_like(a[i4]) + tmp = _integral_bound(_f, zero, b[i4], step[i4], args_indirect, constants, xp) + S[i4], E[i4], status[i4] = tmp[:-1] + nfev[i4] += 2*tmp[-1] + + # Return results + S, E = S.reshape(shape)[()], E.reshape(shape)[()] + status, nfev = status.reshape(shape)[()], nfev.reshape(shape)[()] + return _RichResult(sum=S, error=E, status=status, success=status == 0, + nfev=nfev) + + +def _direct(f, a, b, step, args, constants, xp, inclusive=True): + # Directly evaluate the sum. + + # When used in the context of distributions, `args` would contain the + # distribution parameters. We have broadcasted for simplicity, but we could + # reduce function evaluations when distribution parameters are the same but + # sum limits differ. Roughly: + # - compute the function at all points between min(a) and max(b), + # - compute the cumulative sum, + # - take the difference between elements of the cumulative sum + # corresponding with b and a. + # This is left to future enhancement + + dtype, log, eps, zero, _, _, _ = constants + + # To allow computation in a single vectorized call, find the maximum number + # of points (over all slices) at which the function needs to be evaluated. + # Note: if `inclusive` is `True`, then we want `1` more term in the sum. + # I didn't think it was great style to use `True` as `1` in Python, so I + # explicitly converted it to an `int` before using it. + inclusive_adjustment = int(inclusive) + steps = xp.round((b - a) / step) + inclusive_adjustment + # Equivalently, steps = xp.round((b - a) / step) + inclusive + max_steps = int(xp.max(steps)) + + # In each slice, the function will be evaluated at the same number of points, + # but excessive points (those beyond the right sum limit `b`) are replaced + # with NaN to (potentially) reduce the time of these unnecessary calculations. + # Use a new last axis for these calculations for consistency with other + # elementwise algorithms. + a2, b2, step2 = a[:, xp.newaxis], b[:, xp.newaxis], step[:, xp.newaxis] + args2 = [arg[:, xp.newaxis] for arg in args] + ks = a2 + xp.arange(max_steps, dtype=dtype) * step2 + i_nan = ks >= (b2 + inclusive_adjustment*step2/2) + ks[i_nan] = xp.nan + fs = f(ks, *args2) + + # The function evaluated at NaN is NaN, and NaNs are zeroed in the sum. + # In some cases it may be faster to loop over slices than to vectorize + # like this. This is an optimization that can be added later. + fs[i_nan] = zero + nfev = max_steps - i_nan.sum(axis=-1) + S = special.logsumexp(fs, axis=-1) if log else xp.sum(fs, axis=-1) + # Rough, non-conservative error estimate. See gh-19667 for improvement ideas. + E = xp_real(S) + math.log(eps) if log else eps * abs(S) + return S, E, nfev + + +def _integral_bound(f, a, b, step, args, constants, xp): + # Estimate the sum with integral approximation + dtype, log, _, _, rtol, atol, maxterms = constants + log2 = xp.asarray(math.log(2), dtype=dtype) + + # Get a lower bound on the sum and compute effective absolute tolerance + lb = tanhsinh(f, a, b, args=args, atol=atol, rtol=rtol, log=log) + tol = xp.broadcast_to(xp.asarray(atol), lb.integral.shape) + if log: + tol = special.logsumexp(xp.stack((tol, rtol + lb.integral)), axis=0) + else: + tol = tol + rtol*lb.integral + i_skip = lb.status < 0 # avoid unnecessary f_evals if integral is divergent + tol[i_skip] = xp.nan + status = lb.status + + # As in `_direct`, we'll need a temporary new axis for points + # at which to evaluate the function. Append axis at the end for + # consistency with other elementwise algorithms. + a2 = a[..., xp.newaxis] + step2 = step[..., xp.newaxis] + args2 = [arg[..., xp.newaxis] for arg in args] + + # Find the location of a term that is less than the tolerance (if possible) + log2maxterms = math.floor(math.log2(maxterms)) if maxterms else 0 + n_steps = xp.concat((2**xp.arange(0, log2maxterms), xp.asarray([maxterms]))) + n_steps = xp.astype(n_steps, dtype) + nfev = len(n_steps) * 2 + ks = a2 + n_steps * step2 + fks = f(ks, *args2) + fksp1 = f(ks + step2, *args2) # check that the function is decreasing + fk_insufficient = (fks > tol[:, xp.newaxis]) | (fksp1 > fks) + n_fk_insufficient = xp.sum(fk_insufficient, axis=-1) + nt = xp.minimum(n_fk_insufficient, xp.asarray(n_steps.shape[-1]-1)) + n_steps = n_steps[nt] + + # If `maxterms` is insufficient (i.e. either the magnitude of the last term of the + # partial sum exceeds the tolerance or the function is not decreasing), finish the + # calculation, but report nonzero status. (Improvement: separate the status codes + # for these two cases.) + i_fk_insufficient = (n_fk_insufficient == nfev//2) + + # Directly evaluate the sum up to this term + k = a + n_steps * step + left, left_error, left_nfev = _direct(f, a, k, step, args, + constants, xp, inclusive=False) + left_is_pos_inf = xp.isinf(left) & (left > 0) + i_skip |= left_is_pos_inf # if sum is infinite, no sense in continuing + status[left_is_pos_inf] = -3 + k[i_skip] = xp.nan + + # Use integration to estimate the remaining sum + # Possible optimization for future work: if there were no terms less than + # the tolerance, there is no need to compute the integral to better accuracy. + # Something like: + # atol = xp.maximum(atol, xp.minimum(fk/2 - fb/2)) + # rtol = xp.maximum(rtol, xp.minimum((fk/2 - fb/2)/left)) + # where `fk`/`fb` are currently calculated below. + right = tanhsinh(f, k, b, args=args, atol=atol, rtol=rtol, log=log) + + # Calculate the full estimate and error from the pieces + fk = fks[xp.arange(len(fks)), nt] + + # fb = f(b, *args), but some functions return NaN at infinity. + # instead of 0 like they must (for the sum to be convergent). + fb = xp.full_like(fk, -xp.inf) if log else xp.zeros_like(fk) + i = xp.isfinite(b) + if xp.any(i): # better not call `f` with empty arrays + fb[i] = f(b[i], *[arg[i] for arg in args]) + nfev = nfev + xp.asarray(i, dtype=left_nfev.dtype) + + if log: + log_step = xp.log(step) + S_terms = (left, right.integral - log_step, fk - log2, fb - log2) + S = special.logsumexp(xp.stack(S_terms), axis=0) + E_terms = (left_error, right.error - log_step, fk-log2, fb-log2+xp.pi*1j) + E = xp_real(special.logsumexp(xp.stack(E_terms), axis=0)) + else: + S = left + right.integral/step + fk/2 + fb/2 + E = left_error + right.error/step + fk/2 - fb/2 + status[~i_skip] = right.status[~i_skip] + + status[(status == 0) & i_fk_insufficient] = -4 + return S, E, status, left_nfev + right.nfev + nfev + lb.nfev diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_test_multivariate.cpython-310-x86_64-linux-gnu.so b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_test_multivariate.cpython-310-x86_64-linux-gnu.so new file mode 100644 index 0000000000000000000000000000000000000000..fbe799fa8bfe4c5f1b2d2ed5edc07fe91db628ef Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/_test_multivariate.cpython-310-x86_64-linux-gnu.so differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/dop.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/dop.py new file mode 100644 index 0000000000000000000000000000000000000000..bf67a9a35b7d2959c2617aadc5638b577a45b9b5 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/dop.py @@ -0,0 +1,15 @@ +# This file is not meant for public use and will be removed in SciPy v2.0.0. + +from scipy._lib.deprecation import _sub_module_deprecation + +__all__: list[str] = [] + + +def __dir__(): + return __all__ + + +def __getattr__(name): + return _sub_module_deprecation(sub_package="integrate", module="dop", + private_modules=["_dop"], all=__all__, + attribute=name) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/lsoda.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/lsoda.py new file mode 100644 index 0000000000000000000000000000000000000000..1bc1f1da3c4f0aefad9da73b6405b957ce9335b4 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/lsoda.py @@ -0,0 +1,15 @@ +# This file is not meant for public use and will be removed in SciPy v2.0.0. + +from scipy._lib.deprecation import _sub_module_deprecation + +__all__ = ['lsoda'] # noqa: F822 + + +def __dir__(): + return __all__ + + +def __getattr__(name): + return _sub_module_deprecation(sub_package="integrate", module="lsoda", + private_modules=["_lsoda"], all=__all__, + attribute=name) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/odepack.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/odepack.py new file mode 100644 index 0000000000000000000000000000000000000000..7bb4c1a8c9be375df855abe6e1b30ca9711f2607 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/odepack.py @@ -0,0 +1,17 @@ +# This file is not meant for public use and will be removed in SciPy v2.0.0. +# Use the `scipy.integrate` namespace for importing the functions +# included below. + +from scipy._lib.deprecation import _sub_module_deprecation + +__all__ = ['odeint', 'ODEintWarning'] # noqa: F822 + + +def __dir__(): + return __all__ + + +def __getattr__(name): + return _sub_module_deprecation(sub_package="integrate", module="odepack", + private_modules=["_odepack_py"], all=__all__, + attribute=name) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/quadpack.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/quadpack.py new file mode 100644 index 0000000000000000000000000000000000000000..144584988095c8855da8c34253c045f1a3940572 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/quadpack.py @@ -0,0 +1,23 @@ +# This file is not meant for public use and will be removed in SciPy v2.0.0. +# Use the `scipy.integrate` namespace for importing the functions +# included below. + +from scipy._lib.deprecation import _sub_module_deprecation + +__all__ = [ # noqa: F822 + "quad", + "dblquad", + "tplquad", + "nquad", + "IntegrationWarning", +] + + +def __dir__(): + return __all__ + + +def __getattr__(name): + return _sub_module_deprecation(sub_package="integrate", module="quadpack", + private_modules=["_quadpack_py"], all=__all__, + attribute=name) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/tests/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/tests/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/tests/test__quad_vec.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/tests/test__quad_vec.py new file mode 100644 index 0000000000000000000000000000000000000000..851d28f5671c3eb5821a7379547c1ba66a7e1340 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/tests/test__quad_vec.py @@ -0,0 +1,217 @@ +import pytest + +import numpy as np +from numpy.testing import assert_allclose + +from scipy.integrate import quad_vec + +from multiprocessing.dummy import Pool + + +quadrature_params = pytest.mark.parametrize( + 'quadrature', [None, "gk15", "gk21", "trapezoid"]) + + +@quadrature_params +def test_quad_vec_simple(quadrature): + n = np.arange(10) + def f(x): + return x ** n + for epsabs in [0.1, 1e-3, 1e-6]: + if quadrature == 'trapezoid' and epsabs < 1e-4: + # slow: skip + continue + + kwargs = dict(epsabs=epsabs, quadrature=quadrature) + + exact = 2**(n+1)/(n + 1) + + res, err = quad_vec(f, 0, 2, norm='max', **kwargs) + assert_allclose(res, exact, rtol=0, atol=epsabs) + + res, err = quad_vec(f, 0, 2, norm='2', **kwargs) + assert np.linalg.norm(res - exact) < epsabs + + res, err = quad_vec(f, 0, 2, norm='max', points=(0.5, 1.0), **kwargs) + assert_allclose(res, exact, rtol=0, atol=epsabs) + + res, err, *rest = quad_vec(f, 0, 2, norm='max', + epsrel=1e-8, + full_output=True, + limit=10000, + **kwargs) + assert_allclose(res, exact, rtol=0, atol=epsabs) + + +@quadrature_params +def test_quad_vec_simple_inf(quadrature): + def f(x): + return 1 / (1 + np.float64(x) ** 2) + + for epsabs in [0.1, 1e-3, 1e-6]: + if quadrature == 'trapezoid' and epsabs < 1e-4: + # slow: skip + continue + + kwargs = dict(norm='max', epsabs=epsabs, quadrature=quadrature) + + res, err = quad_vec(f, 0, np.inf, **kwargs) + assert_allclose(res, np.pi/2, rtol=0, atol=max(epsabs, err)) + + res, err = quad_vec(f, 0, -np.inf, **kwargs) + assert_allclose(res, -np.pi/2, rtol=0, atol=max(epsabs, err)) + + res, err = quad_vec(f, -np.inf, 0, **kwargs) + assert_allclose(res, np.pi/2, rtol=0, atol=max(epsabs, err)) + + res, err = quad_vec(f, np.inf, 0, **kwargs) + assert_allclose(res, -np.pi/2, rtol=0, atol=max(epsabs, err)) + + res, err = quad_vec(f, -np.inf, np.inf, **kwargs) + assert_allclose(res, np.pi, rtol=0, atol=max(epsabs, err)) + + res, err = quad_vec(f, np.inf, -np.inf, **kwargs) + assert_allclose(res, -np.pi, rtol=0, atol=max(epsabs, err)) + + res, err = quad_vec(f, np.inf, np.inf, **kwargs) + assert_allclose(res, 0, rtol=0, atol=max(epsabs, err)) + + res, err = quad_vec(f, -np.inf, -np.inf, **kwargs) + assert_allclose(res, 0, rtol=0, atol=max(epsabs, err)) + + res, err = quad_vec(f, 0, np.inf, points=(1.0, 2.0), **kwargs) + assert_allclose(res, np.pi/2, rtol=0, atol=max(epsabs, err)) + + def f(x): + return np.sin(x + 2) / (1 + x ** 2) + exact = np.pi / np.e * np.sin(2) + epsabs = 1e-5 + + res, err, info = quad_vec(f, -np.inf, np.inf, limit=1000, norm='max', epsabs=epsabs, + quadrature=quadrature, full_output=True) + assert info.status == 1 + assert_allclose(res, exact, rtol=0, atol=max(epsabs, 1.5 * err)) + + +def test_quad_vec_args(): + def f(x, a): + return x * (x + a) * np.arange(3) + a = 2 + exact = np.array([0, 4/3, 8/3]) + + res, err = quad_vec(f, 0, 1, args=(a,)) + assert_allclose(res, exact, rtol=0, atol=1e-4) + + +def _lorenzian(x): + return 1 / (1 + x**2) + + +@pytest.mark.fail_slow(10) +def test_quad_vec_pool(): + f = _lorenzian + res, err = quad_vec(f, -np.inf, np.inf, norm='max', epsabs=1e-4, workers=4) + assert_allclose(res, np.pi, rtol=0, atol=1e-4) + + with Pool(10) as pool: + def f(x): + return 1 / (1 + x ** 2) + res, _ = quad_vec(f, -np.inf, np.inf, norm='max', epsabs=1e-4, workers=pool.map) + assert_allclose(res, np.pi, rtol=0, atol=1e-4) + + +def _func_with_args(x, a): + return x * (x + a) * np.arange(3) + + +@pytest.mark.fail_slow(10) +@pytest.mark.parametrize('extra_args', [2, (2,)]) +@pytest.mark.parametrize('workers', [1, 10]) +def test_quad_vec_pool_args(extra_args, workers): + f = _func_with_args + exact = np.array([0, 4/3, 8/3]) + + res, err = quad_vec(f, 0, 1, args=extra_args, workers=workers) + assert_allclose(res, exact, rtol=0, atol=1e-4) + + with Pool(workers) as pool: + res, err = quad_vec(f, 0, 1, args=extra_args, workers=pool.map) + assert_allclose(res, exact, rtol=0, atol=1e-4) + + +@quadrature_params +def test_num_eval(quadrature): + def f(x): + count[0] += 1 + return x**5 + + count = [0] + res = quad_vec(f, 0, 1, norm='max', full_output=True, quadrature=quadrature) + assert res[2].neval == count[0] + + +def test_info(): + def f(x): + return np.ones((3, 2, 1)) + + res, err, info = quad_vec(f, 0, 1, norm='max', full_output=True) + + assert info.success is True + assert info.status == 0 + assert info.message == 'Target precision reached.' + assert info.neval > 0 + assert info.intervals.shape[1] == 2 + assert info.integrals.shape == (info.intervals.shape[0], 3, 2, 1) + assert info.errors.shape == (info.intervals.shape[0],) + + +def test_nan_inf(): + def f_nan(x): + return np.nan + + def f_inf(x): + return np.inf if x < 0.1 else 1/x + + res, err, info = quad_vec(f_nan, 0, 1, full_output=True) + assert info.status == 3 + + res, err, info = quad_vec(f_inf, 0, 1, full_output=True) + assert info.status == 3 + + +@pytest.mark.parametrize('a,b', [(0, 1), (0, np.inf), (np.inf, 0), + (-np.inf, np.inf), (np.inf, -np.inf)]) +def test_points(a, b): + # Check that initial interval splitting is done according to + # `points`, by checking that consecutive sets of 15 point (for + # gk15) function evaluations lie between `points` + + points = (0, 0.25, 0.5, 0.75, 1.0) + points += tuple(-x for x in points) + + quadrature_points = 15 + interval_sets = [] + count = 0 + + def f(x): + nonlocal count + + if count % quadrature_points == 0: + interval_sets.append(set()) + + count += 1 + interval_sets[-1].add(float(x)) + return 0.0 + + quad_vec(f, a, b, points=points, quadrature='gk15', limit=0) + + # Check that all point sets lie in a single `points` interval + for p in interval_sets: + j = np.searchsorted(sorted(points), tuple(p)) + assert np.all(j == j[0]) + + +@pytest.mark.thread_unsafe +def test_trapz_deprecation(): + with pytest.deprecated_call(match="`quadrature='trapz'`"): + quad_vec(lambda x: x, 0, 1, quadrature="trapz") diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/tests/test_banded_ode_solvers.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/tests/test_banded_ode_solvers.py new file mode 100644 index 0000000000000000000000000000000000000000..358c5e3d1fcfe7ccd7e3691bd9af2f47656f4e2b --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/tests/test_banded_ode_solvers.py @@ -0,0 +1,220 @@ +import itertools +import pytest +import numpy as np +from numpy.testing import assert_allclose +from scipy.integrate import ode + + +def _band_count(a): + """Returns ml and mu, the lower and upper band sizes of a.""" + nrows, ncols = a.shape + ml = 0 + for k in range(-nrows+1, 0): + if np.diag(a, k).any(): + ml = -k + break + mu = 0 + for k in range(nrows-1, 0, -1): + if np.diag(a, k).any(): + mu = k + break + return ml, mu + + +def _linear_func(t, y, a): + """Linear system dy/dt = a * y""" + return a.dot(y) + + +def _linear_jac(t, y, a): + """Jacobian of a * y is a.""" + return a + + +def _linear_banded_jac(t, y, a): + """Banded Jacobian.""" + ml, mu = _band_count(a) + bjac = [np.r_[[0] * k, np.diag(a, k)] for k in range(mu, 0, -1)] + bjac.append(np.diag(a)) + for k in range(-1, -ml-1, -1): + bjac.append(np.r_[np.diag(a, k), [0] * (-k)]) + return bjac + + +def _solve_linear_sys(a, y0, tend=1, dt=0.1, + solver=None, method='bdf', use_jac=True, + with_jacobian=False, banded=False): + """Use scipy.integrate.ode to solve a linear system of ODEs. + + a : square ndarray + Matrix of the linear system to be solved. + y0 : ndarray + Initial condition + tend : float + Stop time. + dt : float + Step size of the output. + solver : str + If not None, this must be "vode", "lsoda" or "zvode". + method : str + Either "bdf" or "adams". + use_jac : bool + Determines if the jacobian function is passed to ode(). + with_jacobian : bool + Passed to ode.set_integrator(). + banded : bool + Determines whether a banded or full jacobian is used. + If `banded` is True, `lband` and `uband` are determined by the + values in `a`. + """ + if banded: + lband, uband = _band_count(a) + else: + lband = None + uband = None + + if use_jac: + if banded: + r = ode(_linear_func, _linear_banded_jac) + else: + r = ode(_linear_func, _linear_jac) + else: + r = ode(_linear_func) + + if solver is None: + if np.iscomplexobj(a): + solver = "zvode" + else: + solver = "vode" + + r.set_integrator(solver, + with_jacobian=with_jacobian, + method=method, + lband=lband, uband=uband, + rtol=1e-9, atol=1e-10, + ) + t0 = 0 + r.set_initial_value(y0, t0) + r.set_f_params(a) + r.set_jac_params(a) + + t = [t0] + y = [y0] + while r.successful() and r.t < tend: + r.integrate(r.t + dt) + t.append(r.t) + y.append(r.y) + + t = np.array(t) + y = np.array(y) + return t, y + + +def _analytical_solution(a, y0, t): + """ + Analytical solution to the linear differential equations dy/dt = a*y. + + The solution is only valid if `a` is diagonalizable. + + Returns a 2-D array with shape (len(t), len(y0)). + """ + lam, v = np.linalg.eig(a) + c = np.linalg.solve(v, y0) + e = c * np.exp(lam * t.reshape(-1, 1)) + sol = e.dot(v.T) + return sol + + +@pytest.mark.thread_unsafe +def test_banded_ode_solvers(): + # Test the "lsoda", "vode" and "zvode" solvers of the `ode` class + # with a system that has a banded Jacobian matrix. + + t_exact = np.linspace(0, 1.0, 5) + + # --- Real arrays for testing the "lsoda" and "vode" solvers --- + + # lband = 2, uband = 1: + a_real = np.array([[-0.6, 0.1, 0.0, 0.0, 0.0], + [0.2, -0.5, 0.9, 0.0, 0.0], + [0.1, 0.1, -0.4, 0.1, 0.0], + [0.0, 0.3, -0.1, -0.9, -0.3], + [0.0, 0.0, 0.1, 0.1, -0.7]]) + + # lband = 0, uband = 1: + a_real_upper = np.triu(a_real) + + # lband = 2, uband = 0: + a_real_lower = np.tril(a_real) + + # lband = 0, uband = 0: + a_real_diag = np.triu(a_real_lower) + + real_matrices = [a_real, a_real_upper, a_real_lower, a_real_diag] + real_solutions = [] + + for a in real_matrices: + y0 = np.arange(1, a.shape[0] + 1) + y_exact = _analytical_solution(a, y0, t_exact) + real_solutions.append((y0, t_exact, y_exact)) + + def check_real(idx, solver, meth, use_jac, with_jac, banded): + a = real_matrices[idx] + y0, t_exact, y_exact = real_solutions[idx] + t, y = _solve_linear_sys(a, y0, + tend=t_exact[-1], + dt=t_exact[1] - t_exact[0], + solver=solver, + method=meth, + use_jac=use_jac, + with_jacobian=with_jac, + banded=banded) + assert_allclose(t, t_exact) + assert_allclose(y, y_exact) + + for idx in range(len(real_matrices)): + p = [['vode', 'lsoda'], # solver + ['bdf', 'adams'], # method + [False, True], # use_jac + [False, True], # with_jacobian + [False, True]] # banded + for solver, meth, use_jac, with_jac, banded in itertools.product(*p): + check_real(idx, solver, meth, use_jac, with_jac, banded) + + # --- Complex arrays for testing the "zvode" solver --- + + # complex, lband = 2, uband = 1: + a_complex = a_real - 0.5j * a_real + + # complex, lband = 0, uband = 0: + a_complex_diag = np.diag(np.diag(a_complex)) + + complex_matrices = [a_complex, a_complex_diag] + complex_solutions = [] + + for a in complex_matrices: + y0 = np.arange(1, a.shape[0] + 1) + 1j + y_exact = _analytical_solution(a, y0, t_exact) + complex_solutions.append((y0, t_exact, y_exact)) + + def check_complex(idx, solver, meth, use_jac, with_jac, banded): + a = complex_matrices[idx] + y0, t_exact, y_exact = complex_solutions[idx] + t, y = _solve_linear_sys(a, y0, + tend=t_exact[-1], + dt=t_exact[1] - t_exact[0], + solver=solver, + method=meth, + use_jac=use_jac, + with_jacobian=with_jac, + banded=banded) + assert_allclose(t, t_exact) + assert_allclose(y, y_exact) + + for idx in range(len(complex_matrices)): + p = [['bdf', 'adams'], # method + [False, True], # use_jac + [False, True], # with_jacobian + [False, True]] # banded + for meth, use_jac, with_jac, banded in itertools.product(*p): + check_complex(idx, "zvode", meth, use_jac, with_jac, banded) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/tests/test_bvp.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/tests/test_bvp.py new file mode 100644 index 0000000000000000000000000000000000000000..4ef9eb6ff0502e1113d6bea7ad1e0088633d3151 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/tests/test_bvp.py @@ -0,0 +1,714 @@ +import sys + +try: + from StringIO import StringIO +except ImportError: + from io import StringIO + +import numpy as np +from numpy.testing import (assert_, assert_array_equal, assert_allclose, + assert_equal) +from pytest import raises as assert_raises + +from scipy.sparse import coo_matrix +from scipy.special import erf +from scipy.integrate._bvp import (modify_mesh, estimate_fun_jac, + estimate_bc_jac, compute_jac_indices, + construct_global_jac, solve_bvp) + +import pytest + + +def exp_fun(x, y): + return np.vstack((y[1], y[0])) + + +def exp_fun_jac(x, y): + df_dy = np.empty((2, 2, x.shape[0])) + df_dy[0, 0] = 0 + df_dy[0, 1] = 1 + df_dy[1, 0] = 1 + df_dy[1, 1] = 0 + return df_dy + + +def exp_bc(ya, yb): + return np.hstack((ya[0] - 1, yb[0])) + + +def exp_bc_complex(ya, yb): + return np.hstack((ya[0] - 1 - 1j, yb[0])) + + +def exp_bc_jac(ya, yb): + dbc_dya = np.array([ + [1, 0], + [0, 0] + ]) + dbc_dyb = np.array([ + [0, 0], + [1, 0] + ]) + return dbc_dya, dbc_dyb + + +def exp_sol(x): + return (np.exp(-x) - np.exp(x - 2)) / (1 - np.exp(-2)) + + +def sl_fun(x, y, p): + return np.vstack((y[1], -p[0]**2 * y[0])) + + +def sl_fun_jac(x, y, p): + n, m = y.shape + df_dy = np.empty((n, 2, m)) + df_dy[0, 0] = 0 + df_dy[0, 1] = 1 + df_dy[1, 0] = -p[0]**2 + df_dy[1, 1] = 0 + + df_dp = np.empty((n, 1, m)) + df_dp[0, 0] = 0 + df_dp[1, 0] = -2 * p[0] * y[0] + + return df_dy, df_dp + + +def sl_bc(ya, yb, p): + return np.hstack((ya[0], yb[0], ya[1] - p[0])) + + +def sl_bc_jac(ya, yb, p): + dbc_dya = np.zeros((3, 2)) + dbc_dya[0, 0] = 1 + dbc_dya[2, 1] = 1 + + dbc_dyb = np.zeros((3, 2)) + dbc_dyb[1, 0] = 1 + + dbc_dp = np.zeros((3, 1)) + dbc_dp[2, 0] = -1 + + return dbc_dya, dbc_dyb, dbc_dp + + +def sl_sol(x, p): + return np.sin(p[0] * x) + + +def emden_fun(x, y): + return np.vstack((y[1], -y[0]**5)) + + +def emden_fun_jac(x, y): + df_dy = np.empty((2, 2, x.shape[0])) + df_dy[0, 0] = 0 + df_dy[0, 1] = 1 + df_dy[1, 0] = -5 * y[0]**4 + df_dy[1, 1] = 0 + return df_dy + + +def emden_bc(ya, yb): + return np.array([ya[1], yb[0] - (3/4)**0.5]) + + +def emden_bc_jac(ya, yb): + dbc_dya = np.array([ + [0, 1], + [0, 0] + ]) + dbc_dyb = np.array([ + [0, 0], + [1, 0] + ]) + return dbc_dya, dbc_dyb + + +def emden_sol(x): + return (1 + x**2/3)**-0.5 + + +def undefined_fun(x, y): + return np.zeros_like(y) + + +def undefined_bc(ya, yb): + return np.array([ya[0], yb[0] - 1]) + + +def big_fun(x, y): + f = np.zeros_like(y) + f[::2] = y[1::2] + return f + + +def big_bc(ya, yb): + return np.hstack((ya[::2], yb[::2] - 1)) + + +def big_sol(x, n): + y = np.ones((2 * n, x.size)) + y[::2] = x + return x + + +def big_fun_with_parameters(x, y, p): + """ Big version of sl_fun, with two parameters. + + The two differential equations represented by sl_fun are broadcast to the + number of rows of y, rotating between the parameters p[0] and p[1]. + Here are the differential equations: + + dy[0]/dt = y[1] + dy[1]/dt = -p[0]**2 * y[0] + dy[2]/dt = y[3] + dy[3]/dt = -p[1]**2 * y[2] + dy[4]/dt = y[5] + dy[5]/dt = -p[0]**2 * y[4] + dy[6]/dt = y[7] + dy[7]/dt = -p[1]**2 * y[6] + . + . + . + + """ + f = np.zeros_like(y) + f[::2] = y[1::2] + f[1::4] = -p[0]**2 * y[::4] + f[3::4] = -p[1]**2 * y[2::4] + return f + + +def big_fun_with_parameters_jac(x, y, p): + # big version of sl_fun_jac, with two parameters + n, m = y.shape + df_dy = np.zeros((n, n, m)) + df_dy[range(0, n, 2), range(1, n, 2)] = 1 + df_dy[range(1, n, 4), range(0, n, 4)] = -p[0]**2 + df_dy[range(3, n, 4), range(2, n, 4)] = -p[1]**2 + + df_dp = np.zeros((n, 2, m)) + df_dp[range(1, n, 4), 0] = -2 * p[0] * y[range(0, n, 4)] + df_dp[range(3, n, 4), 1] = -2 * p[1] * y[range(2, n, 4)] + + return df_dy, df_dp + + +def big_bc_with_parameters(ya, yb, p): + # big version of sl_bc, with two parameters + return np.hstack((ya[::2], yb[::2], ya[1] - p[0], ya[3] - p[1])) + + +def big_bc_with_parameters_jac(ya, yb, p): + # big version of sl_bc_jac, with two parameters + n = ya.shape[0] + dbc_dya = np.zeros((n + 2, n)) + dbc_dyb = np.zeros((n + 2, n)) + + dbc_dya[range(n // 2), range(0, n, 2)] = 1 + dbc_dyb[range(n // 2, n), range(0, n, 2)] = 1 + + dbc_dp = np.zeros((n + 2, 2)) + dbc_dp[n, 0] = -1 + dbc_dya[n, 1] = 1 + dbc_dp[n + 1, 1] = -1 + dbc_dya[n + 1, 3] = 1 + + return dbc_dya, dbc_dyb, dbc_dp + + +def big_sol_with_parameters(x, p): + # big version of sl_sol, with two parameters + return np.vstack((np.sin(p[0] * x), np.sin(p[1] * x))) + + +def shock_fun(x, y): + eps = 1e-3 + return np.vstack(( + y[1], + -(x * y[1] + eps * np.pi**2 * np.cos(np.pi * x) + + np.pi * x * np.sin(np.pi * x)) / eps + )) + + +def shock_bc(ya, yb): + return np.array([ya[0] + 2, yb[0]]) + + +def shock_sol(x): + eps = 1e-3 + k = np.sqrt(2 * eps) + return np.cos(np.pi * x) + erf(x / k) / erf(1 / k) + + +def nonlin_bc_fun(x, y): + # laplace eq. + return np.stack([y[1], np.zeros_like(x)]) + + +def nonlin_bc_bc(ya, yb): + phiA, phipA = ya + phiC, phipC = yb + + kappa, ioA, ioC, V, f = 1.64, 0.01, 1.0e-4, 0.5, 38.9 + + # Butler-Volmer Kinetics at Anode + hA = 0.0-phiA-0.0 + iA = ioA * (np.exp(f*hA) - np.exp(-f*hA)) + res0 = iA + kappa * phipA + + # Butler-Volmer Kinetics at Cathode + hC = V - phiC - 1.0 + iC = ioC * (np.exp(f*hC) - np.exp(-f*hC)) + res1 = iC - kappa*phipC + + return np.array([res0, res1]) + + +def nonlin_bc_sol(x): + return -0.13426436116763119 - 1.1308709 * x + + +def test_modify_mesh(): + x = np.array([0, 1, 3, 9], dtype=float) + x_new = modify_mesh(x, np.array([0]), np.array([2])) + assert_array_equal(x_new, np.array([0, 0.5, 1, 3, 5, 7, 9])) + + x = np.array([-6, -3, 0, 3, 6], dtype=float) + x_new = modify_mesh(x, np.array([1], dtype=int), np.array([0, 2, 3])) + assert_array_equal(x_new, [-6, -5, -4, -3, -1.5, 0, 1, 2, 3, 4, 5, 6]) + + +def test_compute_fun_jac(): + x = np.linspace(0, 1, 5) + y = np.empty((2, x.shape[0])) + y[0] = 0.01 + y[1] = 0.02 + p = np.array([]) + df_dy, df_dp = estimate_fun_jac(lambda x, y, p: exp_fun(x, y), x, y, p) + df_dy_an = exp_fun_jac(x, y) + assert_allclose(df_dy, df_dy_an) + assert_(df_dp is None) + + x = np.linspace(0, np.pi, 5) + y = np.empty((2, x.shape[0])) + y[0] = np.sin(x) + y[1] = np.cos(x) + p = np.array([1.0]) + df_dy, df_dp = estimate_fun_jac(sl_fun, x, y, p) + df_dy_an, df_dp_an = sl_fun_jac(x, y, p) + assert_allclose(df_dy, df_dy_an) + assert_allclose(df_dp, df_dp_an) + + x = np.linspace(0, 1, 10) + y = np.empty((2, x.shape[0])) + y[0] = (3/4)**0.5 + y[1] = 1e-4 + p = np.array([]) + df_dy, df_dp = estimate_fun_jac(lambda x, y, p: emden_fun(x, y), x, y, p) + df_dy_an = emden_fun_jac(x, y) + assert_allclose(df_dy, df_dy_an) + assert_(df_dp is None) + + +def test_compute_bc_jac(): + ya = np.array([-1.0, 2]) + yb = np.array([0.5, 3]) + p = np.array([]) + dbc_dya, dbc_dyb, dbc_dp = estimate_bc_jac( + lambda ya, yb, p: exp_bc(ya, yb), ya, yb, p) + dbc_dya_an, dbc_dyb_an = exp_bc_jac(ya, yb) + assert_allclose(dbc_dya, dbc_dya_an) + assert_allclose(dbc_dyb, dbc_dyb_an) + assert_(dbc_dp is None) + + ya = np.array([0.0, 1]) + yb = np.array([0.0, -1]) + p = np.array([0.5]) + dbc_dya, dbc_dyb, dbc_dp = estimate_bc_jac(sl_bc, ya, yb, p) + dbc_dya_an, dbc_dyb_an, dbc_dp_an = sl_bc_jac(ya, yb, p) + assert_allclose(dbc_dya, dbc_dya_an) + assert_allclose(dbc_dyb, dbc_dyb_an) + assert_allclose(dbc_dp, dbc_dp_an) + + ya = np.array([0.5, 100]) + yb = np.array([-1000, 10.5]) + p = np.array([]) + dbc_dya, dbc_dyb, dbc_dp = estimate_bc_jac( + lambda ya, yb, p: emden_bc(ya, yb), ya, yb, p) + dbc_dya_an, dbc_dyb_an = emden_bc_jac(ya, yb) + assert_allclose(dbc_dya, dbc_dya_an) + assert_allclose(dbc_dyb, dbc_dyb_an) + assert_(dbc_dp is None) + + +def test_compute_jac_indices(): + n = 2 + m = 4 + k = 2 + i, j = compute_jac_indices(n, m, k) + s = coo_matrix((np.ones_like(i), (i, j))).toarray() + s_true = np.array([ + [1, 1, 1, 1, 0, 0, 0, 0, 1, 1], + [1, 1, 1, 1, 0, 0, 0, 0, 1, 1], + [0, 0, 1, 1, 1, 1, 0, 0, 1, 1], + [0, 0, 1, 1, 1, 1, 0, 0, 1, 1], + [0, 0, 0, 0, 1, 1, 1, 1, 1, 1], + [0, 0, 0, 0, 1, 1, 1, 1, 1, 1], + [1, 1, 0, 0, 0, 0, 1, 1, 1, 1], + [1, 1, 0, 0, 0, 0, 1, 1, 1, 1], + [1, 1, 0, 0, 0, 0, 1, 1, 1, 1], + [1, 1, 0, 0, 0, 0, 1, 1, 1, 1], + ]) + assert_array_equal(s, s_true) + + +def test_compute_global_jac(): + n = 2 + m = 5 + k = 1 + i_jac, j_jac = compute_jac_indices(2, 5, 1) + x = np.linspace(0, 1, 5) + h = np.diff(x) + y = np.vstack((np.sin(np.pi * x), np.pi * np.cos(np.pi * x))) + p = np.array([3.0]) + + f = sl_fun(x, y, p) + + x_middle = x[:-1] + 0.5 * h + y_middle = 0.5 * (y[:, :-1] + y[:, 1:]) - h/8 * (f[:, 1:] - f[:, :-1]) + + df_dy, df_dp = sl_fun_jac(x, y, p) + df_dy_middle, df_dp_middle = sl_fun_jac(x_middle, y_middle, p) + dbc_dya, dbc_dyb, dbc_dp = sl_bc_jac(y[:, 0], y[:, -1], p) + + J = construct_global_jac(n, m, k, i_jac, j_jac, h, df_dy, df_dy_middle, + df_dp, df_dp_middle, dbc_dya, dbc_dyb, dbc_dp) + J = J.toarray() + + def J_block(h, p): + return np.array([ + [h**2*p**2/12 - 1, -0.5*h, -h**2*p**2/12 + 1, -0.5*h], + [0.5*h*p**2, h**2*p**2/12 - 1, 0.5*h*p**2, 1 - h**2*p**2/12] + ]) + + J_true = np.zeros((m * n + k, m * n + k)) + for i in range(m - 1): + J_true[i * n: (i + 1) * n, i * n: (i + 2) * n] = J_block(h[i], p[0]) + + J_true[:(m - 1) * n:2, -1] = p * h**2/6 * (y[0, :-1] - y[0, 1:]) + J_true[1:(m - 1) * n:2, -1] = p * (h * (y[0, :-1] + y[0, 1:]) + + h**2/6 * (y[1, :-1] - y[1, 1:])) + + J_true[8, 0] = 1 + J_true[9, 8] = 1 + J_true[10, 1] = 1 + J_true[10, 10] = -1 + + assert_allclose(J, J_true, rtol=1e-10) + + df_dy, df_dp = estimate_fun_jac(sl_fun, x, y, p) + df_dy_middle, df_dp_middle = estimate_fun_jac(sl_fun, x_middle, y_middle, p) + dbc_dya, dbc_dyb, dbc_dp = estimate_bc_jac(sl_bc, y[:, 0], y[:, -1], p) + J = construct_global_jac(n, m, k, i_jac, j_jac, h, df_dy, df_dy_middle, + df_dp, df_dp_middle, dbc_dya, dbc_dyb, dbc_dp) + J = J.toarray() + assert_allclose(J, J_true, rtol=2e-8, atol=2e-8) + + +def test_parameter_validation(): + x = [0, 1, 0.5] + y = np.zeros((2, 3)) + assert_raises(ValueError, solve_bvp, exp_fun, exp_bc, x, y) + + x = np.linspace(0, 1, 5) + y = np.zeros((2, 4)) + assert_raises(ValueError, solve_bvp, exp_fun, exp_bc, x, y) + + def fun(x, y, p): + return exp_fun(x, y) + def bc(ya, yb, p): + return exp_bc(ya, yb) + + y = np.zeros((2, x.shape[0])) + assert_raises(ValueError, solve_bvp, fun, bc, x, y, p=[1]) + + def wrong_shape_fun(x, y): + return np.zeros(3) + + assert_raises(ValueError, solve_bvp, wrong_shape_fun, bc, x, y) + + S = np.array([[0, 0]]) + assert_raises(ValueError, solve_bvp, exp_fun, exp_bc, x, y, S=S) + + +def test_no_params(): + x = np.linspace(0, 1, 5) + x_test = np.linspace(0, 1, 100) + y = np.zeros((2, x.shape[0])) + for fun_jac in [None, exp_fun_jac]: + for bc_jac in [None, exp_bc_jac]: + sol = solve_bvp(exp_fun, exp_bc, x, y, fun_jac=fun_jac, + bc_jac=bc_jac) + + assert_equal(sol.status, 0) + assert_(sol.success) + + assert_equal(sol.x.size, 5) + + sol_test = sol.sol(x_test) + + assert_allclose(sol_test[0], exp_sol(x_test), atol=1e-5) + + f_test = exp_fun(x_test, sol_test) + r = sol.sol(x_test, 1) - f_test + rel_res = r / (1 + np.abs(f_test)) + norm_res = np.sum(rel_res**2, axis=0)**0.5 + assert_(np.all(norm_res < 1e-3)) + + assert_(np.all(sol.rms_residuals < 1e-3)) + assert_allclose(sol.sol(sol.x), sol.y, rtol=1e-10, atol=1e-10) + assert_allclose(sol.sol(sol.x, 1), sol.yp, rtol=1e-10, atol=1e-10) + + +def test_with_params(): + x = np.linspace(0, np.pi, 5) + x_test = np.linspace(0, np.pi, 100) + y = np.ones((2, x.shape[0])) + + for fun_jac in [None, sl_fun_jac]: + for bc_jac in [None, sl_bc_jac]: + sol = solve_bvp(sl_fun, sl_bc, x, y, p=[0.5], fun_jac=fun_jac, + bc_jac=bc_jac) + + assert_equal(sol.status, 0) + assert_(sol.success) + + assert_(sol.x.size < 10) + + assert_allclose(sol.p, [1], rtol=1e-4) + + sol_test = sol.sol(x_test) + + assert_allclose(sol_test[0], sl_sol(x_test, [1]), + rtol=1e-4, atol=1e-4) + + f_test = sl_fun(x_test, sol_test, [1]) + r = sol.sol(x_test, 1) - f_test + rel_res = r / (1 + np.abs(f_test)) + norm_res = np.sum(rel_res ** 2, axis=0) ** 0.5 + assert_(np.all(norm_res < 1e-3)) + + assert_(np.all(sol.rms_residuals < 1e-3)) + assert_allclose(sol.sol(sol.x), sol.y, rtol=1e-10, atol=1e-10) + assert_allclose(sol.sol(sol.x, 1), sol.yp, rtol=1e-10, atol=1e-10) + + +def test_singular_term(): + x = np.linspace(0, 1, 10) + x_test = np.linspace(0.05, 1, 100) + y = np.empty((2, 10)) + y[0] = (3/4)**0.5 + y[1] = 1e-4 + S = np.array([[0, 0], [0, -2]]) + + for fun_jac in [None, emden_fun_jac]: + for bc_jac in [None, emden_bc_jac]: + sol = solve_bvp(emden_fun, emden_bc, x, y, S=S, fun_jac=fun_jac, + bc_jac=bc_jac) + + assert_equal(sol.status, 0) + assert_(sol.success) + + assert_equal(sol.x.size, 10) + + sol_test = sol.sol(x_test) + assert_allclose(sol_test[0], emden_sol(x_test), atol=1e-5) + + f_test = emden_fun(x_test, sol_test) + S.dot(sol_test) / x_test + r = sol.sol(x_test, 1) - f_test + rel_res = r / (1 + np.abs(f_test)) + norm_res = np.sum(rel_res ** 2, axis=0) ** 0.5 + + assert_(np.all(norm_res < 1e-3)) + assert_allclose(sol.sol(sol.x), sol.y, rtol=1e-10, atol=1e-10) + assert_allclose(sol.sol(sol.x, 1), sol.yp, rtol=1e-10, atol=1e-10) + + +def test_complex(): + # The test is essentially the same as test_no_params, but boundary + # conditions are turned into complex. + x = np.linspace(0, 1, 5) + x_test = np.linspace(0, 1, 100) + y = np.zeros((2, x.shape[0]), dtype=complex) + for fun_jac in [None, exp_fun_jac]: + for bc_jac in [None, exp_bc_jac]: + sol = solve_bvp(exp_fun, exp_bc_complex, x, y, fun_jac=fun_jac, + bc_jac=bc_jac) + + assert_equal(sol.status, 0) + assert_(sol.success) + + sol_test = sol.sol(x_test) + + assert_allclose(sol_test[0].real, exp_sol(x_test), atol=1e-5) + assert_allclose(sol_test[0].imag, exp_sol(x_test), atol=1e-5) + + f_test = exp_fun(x_test, sol_test) + r = sol.sol(x_test, 1) - f_test + rel_res = r / (1 + np.abs(f_test)) + norm_res = np.sum(np.real(rel_res * np.conj(rel_res)), + axis=0) ** 0.5 + assert_(np.all(norm_res < 1e-3)) + + assert_(np.all(sol.rms_residuals < 1e-3)) + assert_allclose(sol.sol(sol.x), sol.y, rtol=1e-10, atol=1e-10) + assert_allclose(sol.sol(sol.x, 1), sol.yp, rtol=1e-10, atol=1e-10) + + +def test_failures(): + x = np.linspace(0, 1, 2) + y = np.zeros((2, x.size)) + res = solve_bvp(exp_fun, exp_bc, x, y, tol=1e-5, max_nodes=5) + assert_equal(res.status, 1) + assert_(not res.success) + + x = np.linspace(0, 1, 5) + y = np.zeros((2, x.size)) + res = solve_bvp(undefined_fun, undefined_bc, x, y) + assert_equal(res.status, 2) + assert_(not res.success) + + +def test_big_problem(): + n = 30 + x = np.linspace(0, 1, 5) + y = np.zeros((2 * n, x.size)) + sol = solve_bvp(big_fun, big_bc, x, y) + + assert_equal(sol.status, 0) + assert_(sol.success) + + sol_test = sol.sol(x) + + assert_allclose(sol_test[0], big_sol(x, n)) + + f_test = big_fun(x, sol_test) + r = sol.sol(x, 1) - f_test + rel_res = r / (1 + np.abs(f_test)) + norm_res = np.sum(np.real(rel_res * np.conj(rel_res)), axis=0) ** 0.5 + assert_(np.all(norm_res < 1e-3)) + + assert_(np.all(sol.rms_residuals < 1e-3)) + assert_allclose(sol.sol(sol.x), sol.y, rtol=1e-10, atol=1e-10) + assert_allclose(sol.sol(sol.x, 1), sol.yp, rtol=1e-10, atol=1e-10) + + +def test_big_problem_with_parameters(): + n = 30 + x = np.linspace(0, np.pi, 5) + x_test = np.linspace(0, np.pi, 100) + y = np.ones((2 * n, x.size)) + + for fun_jac in [None, big_fun_with_parameters_jac]: + for bc_jac in [None, big_bc_with_parameters_jac]: + sol = solve_bvp(big_fun_with_parameters, big_bc_with_parameters, x, + y, p=[0.5, 0.5], fun_jac=fun_jac, bc_jac=bc_jac) + + assert_equal(sol.status, 0) + assert_(sol.success) + + assert_allclose(sol.p, [1, 1], rtol=1e-4) + + sol_test = sol.sol(x_test) + + for isol in range(0, n, 4): + assert_allclose(sol_test[isol], + big_sol_with_parameters(x_test, [1, 1])[0], + rtol=1e-4, atol=1e-4) + assert_allclose(sol_test[isol + 2], + big_sol_with_parameters(x_test, [1, 1])[1], + rtol=1e-4, atol=1e-4) + + f_test = big_fun_with_parameters(x_test, sol_test, [1, 1]) + r = sol.sol(x_test, 1) - f_test + rel_res = r / (1 + np.abs(f_test)) + norm_res = np.sum(rel_res ** 2, axis=0) ** 0.5 + assert_(np.all(norm_res < 1e-3)) + + assert_(np.all(sol.rms_residuals < 1e-3)) + assert_allclose(sol.sol(sol.x), sol.y, rtol=1e-10, atol=1e-10) + assert_allclose(sol.sol(sol.x, 1), sol.yp, rtol=1e-10, atol=1e-10) + + +def test_shock_layer(): + x = np.linspace(-1, 1, 5) + x_test = np.linspace(-1, 1, 100) + y = np.zeros((2, x.size)) + sol = solve_bvp(shock_fun, shock_bc, x, y) + + assert_equal(sol.status, 0) + assert_(sol.success) + + assert_(sol.x.size < 110) + + sol_test = sol.sol(x_test) + assert_allclose(sol_test[0], shock_sol(x_test), rtol=1e-5, atol=1e-5) + + f_test = shock_fun(x_test, sol_test) + r = sol.sol(x_test, 1) - f_test + rel_res = r / (1 + np.abs(f_test)) + norm_res = np.sum(rel_res ** 2, axis=0) ** 0.5 + + assert_(np.all(norm_res < 1e-3)) + assert_allclose(sol.sol(sol.x), sol.y, rtol=1e-10, atol=1e-10) + assert_allclose(sol.sol(sol.x, 1), sol.yp, rtol=1e-10, atol=1e-10) + + +def test_nonlin_bc(): + x = np.linspace(0, 0.1, 5) + x_test = x + y = np.zeros([2, x.size]) + sol = solve_bvp(nonlin_bc_fun, nonlin_bc_bc, x, y) + + assert_equal(sol.status, 0) + assert_(sol.success) + + assert_(sol.x.size < 8) + + sol_test = sol.sol(x_test) + assert_allclose(sol_test[0], nonlin_bc_sol(x_test), rtol=1e-5, atol=1e-5) + + f_test = nonlin_bc_fun(x_test, sol_test) + r = sol.sol(x_test, 1) - f_test + rel_res = r / (1 + np.abs(f_test)) + norm_res = np.sum(rel_res ** 2, axis=0) ** 0.5 + + assert_(np.all(norm_res < 1e-3)) + assert_allclose(sol.sol(sol.x), sol.y, rtol=1e-10, atol=1e-10) + assert_allclose(sol.sol(sol.x, 1), sol.yp, rtol=1e-10, atol=1e-10) + + +@pytest.mark.thread_unsafe +def test_verbose(): + # Smoke test that checks the printing does something and does not crash + x = np.linspace(0, 1, 5) + y = np.zeros((2, x.shape[0])) + for verbose in [0, 1, 2]: + old_stdout = sys.stdout + sys.stdout = StringIO() + try: + sol = solve_bvp(exp_fun, exp_bc, x, y, verbose=verbose) + text = sys.stdout.getvalue() + finally: + sys.stdout = old_stdout + + assert_(sol.success) + if verbose == 0: + assert_(not text, text) + if verbose >= 1: + assert_("Solved in" in text, text) + if verbose >= 2: + assert_("Max residual" in text, text) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/tests/test_cubature.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/tests/test_cubature.py new file mode 100644 index 0000000000000000000000000000000000000000..899655c7631fbc86d06eb97c514761d4c882a632 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/tests/test_cubature.py @@ -0,0 +1,1389 @@ +import math +import scipy +import itertools + +import pytest + +from scipy._lib._array_api import ( + array_namespace, + xp_assert_close, + xp_size, + np_compat, + is_array_api_strict, +) +from scipy.conftest import array_api_compatible + +from scipy.integrate import cubature + +from scipy.integrate._rules import ( + Rule, FixedRule, + NestedFixedRule, + GaussLegendreQuadrature, GaussKronrodQuadrature, + GenzMalikCubature, +) + +from scipy.integrate._cubature import _InfiniteLimitsTransform + +pytestmark = [pytest.mark.usefixtures("skip_xp_backends"),] +skip_xp_backends = pytest.mark.skip_xp_backends + +# The integrands ``genz_malik_1980_*`` come from the paper: +# A.C. Genz, A.A. Malik, Remarks on algorithm 006: An adaptive algorithm for +# numerical integration over an N-dimensional rectangular region, Journal of +# Computational and Applied Mathematics, Volume 6, Issue 4, 1980, Pages 295-302, +# ISSN 0377-0427, https://doi.org/10.1016/0771-050X(80)90039-X. + + +def basic_1d_integrand(x, n, xp): + x_reshaped = xp.reshape(x, (-1, 1, 1)) + n_reshaped = xp.reshape(n, (1, -1, 1)) + + return x_reshaped**n_reshaped + + +def basic_1d_integrand_exact(n, xp): + # Exact only for integration over interval [0, 2]. + return xp.reshape(2**(n+1)/(n+1), (-1, 1)) + + +def basic_nd_integrand(x, n, xp): + return xp.reshape(xp.sum(x, axis=-1), (-1, 1))**xp.reshape(n, (1, -1)) + + +def basic_nd_integrand_exact(n, xp): + # Exact only for integration over interval [0, 2]. + return (-2**(3+n) + 4**(2+n))/((1+n)*(2+n)) + + +def genz_malik_1980_f_1(x, r, alphas, xp): + r""" + .. math:: f_1(\mathbf x) = \cos\left(2\pi r + \sum^n_{i = 1}\alpha_i x_i\right) + + .. code-block:: mathematica + + genzMalik1980f1[x_List, r_, alphas_List] := Cos[2*Pi*r + Total[x*alphas]] + """ + + npoints, ndim = x.shape[0], x.shape[-1] + + alphas_reshaped = alphas[None, ...] + x_reshaped = xp.reshape(x, (npoints, *([1]*(len(alphas.shape) - 1)), ndim)) + + return xp.cos(2*math.pi*r + xp.sum(alphas_reshaped * x_reshaped, axis=-1)) + + +def genz_malik_1980_f_1_exact(a, b, r, alphas, xp): + ndim = xp_size(a) + a = xp.reshape(a, (*([1]*(len(alphas.shape) - 1)), ndim)) + b = xp.reshape(b, (*([1]*(len(alphas.shape) - 1)), ndim)) + + return ( + (-2)**ndim + * 1/xp.prod(alphas, axis=-1) + * xp.cos(2*math.pi*r + xp.sum(alphas * (a+b) * 0.5, axis=-1)) + * xp.prod(xp.sin(alphas * (a-b)/2), axis=-1) + ) + + +def genz_malik_1980_f_1_random_args(rng, shape, xp): + r = xp.asarray(rng.random(shape[:-1])) + alphas = xp.asarray(rng.random(shape)) + + difficulty = 9 + normalisation_factors = xp.sum(alphas, axis=-1)[..., None] + alphas = difficulty * alphas / normalisation_factors + + return (r, alphas) + + +def genz_malik_1980_f_2(x, alphas, betas, xp): + r""" + .. math:: f_2(\mathbf x) = \prod^n_{i = 1} (\alpha_i^2 + (x_i - \beta_i)^2)^{-1} + + .. code-block:: mathematica + + genzMalik1980f2[x_List, alphas_List, betas_List] := + 1/Times @@ ((alphas^2 + (x - betas)^2)) + """ + npoints, ndim = x.shape[0], x.shape[-1] + + alphas_reshaped = alphas[None, ...] + betas_reshaped = betas[None, ...] + + x_reshaped = xp.reshape(x, (npoints, *([1]*(len(alphas.shape) - 1)), ndim)) + + return 1/xp.prod(alphas_reshaped**2 + (x_reshaped-betas_reshaped)**2, axis=-1) + + +def genz_malik_1980_f_2_exact(a, b, alphas, betas, xp): + ndim = xp_size(a) + a = xp.reshape(a, (*([1]*(len(alphas.shape) - 1)), ndim)) + b = xp.reshape(b, (*([1]*(len(alphas.shape) - 1)), ndim)) + + # `xp` is the unwrapped namespace, so `.atan` won't work for `xp = np` and np<2. + xp_test = array_namespace(a) + + return ( + (-1)**ndim * 1/xp.prod(alphas, axis=-1) + * xp.prod( + xp_test.atan((a - betas)/alphas) - xp_test.atan((b - betas)/alphas), + axis=-1, + ) + ) + + +def genz_malik_1980_f_2_random_args(rng, shape, xp): + ndim = shape[-1] + alphas = xp.asarray(rng.random(shape)) + betas = xp.asarray(rng.random(shape)) + + difficulty = 25.0 + products = xp.prod(alphas**xp.asarray(-2.0), axis=-1) + normalisation_factors = (products**xp.asarray(1 / (2*ndim)))[..., None] + alphas = alphas * normalisation_factors * math.pow(difficulty, 1 / (2*ndim)) + + # Adjust alphas from distribution used in Genz and Malik 1980 since denominator + # is very small for high dimensions. + alphas *= 10 + + return alphas, betas + + +def genz_malik_1980_f_3(x, alphas, xp): + r""" + .. math:: f_3(\mathbf x) = \exp\left(\sum^n_{i = 1} \alpha_i x_i\right) + + .. code-block:: mathematica + + genzMalik1980f3[x_List, alphas_List] := Exp[Dot[x, alphas]] + """ + + npoints, ndim = x.shape[0], x.shape[-1] + + alphas_reshaped = alphas[None, ...] + x_reshaped = xp.reshape(x, (npoints, *([1]*(len(alphas.shape) - 1)), ndim)) + + return xp.exp(xp.sum(alphas_reshaped * x_reshaped, axis=-1)) + + +def genz_malik_1980_f_3_exact(a, b, alphas, xp): + ndim = xp_size(a) + a = xp.reshape(a, (*([1]*(len(alphas.shape) - 1)), ndim)) + b = xp.reshape(b, (*([1]*(len(alphas.shape) - 1)), ndim)) + + return ( + (-1)**ndim * 1/xp.prod(alphas, axis=-1) + * xp.prod(xp.exp(alphas * a) - xp.exp(alphas * b), axis=-1) + ) + + +def genz_malik_1980_f_3_random_args(rng, shape, xp): + alphas = xp.asarray(rng.random(shape)) + normalisation_factors = xp.sum(alphas, axis=-1)[..., None] + difficulty = 12.0 + alphas = difficulty * alphas / normalisation_factors + + return (alphas,) + + +def genz_malik_1980_f_4(x, alphas, xp): + r""" + .. math:: f_4(\mathbf x) = \left(1 + \sum^n_{i = 1} \alpha_i x_i\right)^{-n-1} + + .. code-block:: mathematica + genzMalik1980f4[x_List, alphas_List] := + (1 + Dot[x, alphas])^(-Length[alphas] - 1) + """ + + npoints, ndim = x.shape[0], x.shape[-1] + + alphas_reshaped = alphas[None, ...] + x_reshaped = xp.reshape(x, (npoints, *([1]*(len(alphas.shape) - 1)), ndim)) + + return (1 + xp.sum(alphas_reshaped * x_reshaped, axis=-1))**(-ndim-1) + + +def genz_malik_1980_f_4_exact(a, b, alphas, xp): + ndim = xp_size(a) + + def F(x): + x_reshaped = xp.reshape(x, (*([1]*(len(alphas.shape) - 1)), ndim)) + + return ( + (-1)**ndim/xp.prod(alphas, axis=-1) + / math.factorial(ndim) + / (1 + xp.sum(alphas * x_reshaped, axis=-1)) + ) + + return _eval_indefinite_integral(F, a, b, xp) + + +def _eval_indefinite_integral(F, a, b, xp): + """ + Calculates a definite integral from points `a` to `b` by summing up over the corners + of the corresponding hyperrectangle. + """ + + ndim = xp_size(a) + points = xp.stack([a, b], axis=0) + + out = 0 + for ind in itertools.product(range(2), repeat=ndim): + selected_points = xp.asarray([points[i, j] for i, j in zip(ind, range(ndim))]) + out += pow(-1, sum(ind) + ndim) * F(selected_points) + + return out + + +def genz_malik_1980_f_4_random_args(rng, shape, xp): + ndim = shape[-1] + + alphas = xp.asarray(rng.random(shape)) + normalisation_factors = xp.sum(alphas, axis=-1)[..., None] + difficulty = 14.0 + alphas = (difficulty / ndim) * alphas / normalisation_factors + + return (alphas,) + + +def genz_malik_1980_f_5(x, alphas, betas, xp): + r""" + .. math:: + + f_5(\mathbf x) = \exp\left(-\sum^n_{i = 1} \alpha^2_i (x_i - \beta_i)^2\right) + + .. code-block:: mathematica + + genzMalik1980f5[x_List, alphas_List, betas_List] := + Exp[-Total[alphas^2 * (x - betas)^2]] + """ + + npoints, ndim = x.shape[0], x.shape[-1] + + alphas_reshaped = alphas[None, ...] + betas_reshaped = betas[None, ...] + + x_reshaped = xp.reshape(x, (npoints, *([1]*(len(alphas.shape) - 1)), ndim)) + + return xp.exp( + -xp.sum(alphas_reshaped**2 * (x_reshaped - betas_reshaped)**2, axis=-1) + ) + + +def genz_malik_1980_f_5_exact(a, b, alphas, betas, xp): + ndim = xp_size(a) + a = xp.reshape(a, (*([1]*(len(alphas.shape) - 1)), ndim)) + b = xp.reshape(b, (*([1]*(len(alphas.shape) - 1)), ndim)) + + return ( + (1/2)**ndim + * 1/xp.prod(alphas, axis=-1) + * (math.pi**(ndim/2)) + * xp.prod( + scipy.special.erf(alphas * (betas - a)) + + scipy.special.erf(alphas * (b - betas)), + axis=-1, + ) + ) + + +def genz_malik_1980_f_5_random_args(rng, shape, xp): + alphas = xp.asarray(rng.random(shape)) + betas = xp.asarray(rng.random(shape)) + + difficulty = 21.0 + normalisation_factors = xp.sqrt(xp.sum(alphas**xp.asarray(2.0), axis=-1))[..., None] + alphas = alphas / normalisation_factors * math.sqrt(difficulty) + + return alphas, betas + + +def f_gaussian(x, alphas, xp): + r""" + .. math:: + + f(\mathbf x) = \exp\left(-\sum^n_{i = 1} (\alpha_i x_i)^2 \right) + """ + npoints, ndim = x.shape[0], x.shape[-1] + alphas_reshaped = alphas[None, ...] + x_reshaped = xp.reshape(x, (npoints, *([1]*(len(alphas.shape) - 1)), ndim)) + + return xp.exp(-xp.sum((alphas_reshaped * x_reshaped)**2, axis=-1)) + + +def f_gaussian_exact(a, b, alphas, xp): + # Exact only when `a` and `b` are one of: + # (-oo, oo), or + # (0, oo), or + # (-oo, 0) + # `alphas` can be arbitrary. + + ndim = xp_size(a) + double_infinite_count = 0 + semi_infinite_count = 0 + + for i in range(ndim): + if xp.isinf(a[i]) and xp.isinf(b[i]): # doubly-infinite + double_infinite_count += 1 + elif xp.isinf(a[i]) != xp.isinf(b[i]): # exclusive or, so semi-infinite + semi_infinite_count += 1 + + return (math.sqrt(math.pi) ** ndim) / ( + 2**semi_infinite_count * xp.prod(alphas, axis=-1) + ) + + +def f_gaussian_random_args(rng, shape, xp): + alphas = xp.asarray(rng.random(shape)) + + # If alphas are very close to 0 this makes the problem very difficult due to large + # values of ``f``. + alphas *= 100 + + return (alphas,) + + +def f_modified_gaussian(x_arr, n, xp): + r""" + .. math:: + + f(x, y, z, w) = x^n \sqrt{y} \exp(-y-z^2-w^2) + """ + x, y, z, w = x_arr[:, 0], x_arr[:, 1], x_arr[:, 2], x_arr[:, 3] + res = (x ** n[:, None]) * xp.sqrt(y) * xp.exp(-y-z**2-w**2) + + return res.T + + +def f_modified_gaussian_exact(a, b, n, xp): + # Exact only for the limits + # a = (0, 0, -oo, -oo) + # b = (1, oo, oo, oo) + # but defined here as a function to match the format of the other integrands. + return 1/(2 + 2*n) * math.pi ** (3/2) + + +def f_with_problematic_points(x_arr, points, xp): + """ + This emulates a function with a list of singularities given by `points`. + + If no `x_arr` are one of the `points`, then this function returns 1. + """ + + for point in points: + if xp.any(x_arr == point): + raise ValueError("called with a problematic point") + + return xp.ones(x_arr.shape[0]) + + +@array_api_compatible +class TestCubature: + """ + Tests related to the interface of `cubature`. + """ + + @pytest.mark.parametrize("rule_str", [ + "gauss-kronrod", + "genz-malik", + "gk21", + "gk15", + ]) + def test_pass_str(self, rule_str, xp): + n = xp.arange(5, dtype=xp.float64) + a = xp.asarray([0, 0], dtype=xp.float64) + b = xp.asarray([2, 2], dtype=xp.float64) + + res = cubature(basic_nd_integrand, a, b, rule=rule_str, args=(n, xp)) + + xp_assert_close( + res.estimate, + basic_nd_integrand_exact(n, xp), + rtol=1e-8, + atol=0, + ) + + @skip_xp_backends(np_only=True, + reason='array-likes only supported for NumPy backend') + def test_pass_array_like_not_array(self, xp): + n = np_compat.arange(5, dtype=np_compat.float64) + a = [0] + b = [2] + + res = cubature( + basic_1d_integrand, + a, + b, + args=(n, xp) + ) + + xp_assert_close( + res.estimate, + basic_1d_integrand_exact(n, xp), + rtol=1e-8, + atol=0, + ) + + def test_stops_after_max_subdivisions(self, xp): + a = xp.asarray([0]) + b = xp.asarray([1]) + rule = BadErrorRule() + + res = cubature( + basic_1d_integrand, # Any function would suffice + a, + b, + rule=rule, + max_subdivisions=10, + args=(xp.arange(5, dtype=xp.float64), xp), + ) + + assert res.subdivisions == 10 + assert res.status == "not_converged" + + def test_a_and_b_must_be_1d(self, xp): + a = xp.asarray([[0]], dtype=xp.float64) + b = xp.asarray([[1]], dtype=xp.float64) + + with pytest.raises(Exception, match="`a` and `b` must be 1D arrays"): + cubature(basic_1d_integrand, a, b, args=(xp,)) + + def test_a_and_b_must_be_nonempty(self, xp): + a = xp.asarray([]) + b = xp.asarray([]) + + with pytest.raises(Exception, match="`a` and `b` must be nonempty"): + cubature(basic_1d_integrand, a, b, args=(xp,)) + + def test_zero_width_limits(self, xp): + n = xp.arange(5, dtype=xp.float64) + + a = xp.asarray([0], dtype=xp.float64) + b = xp.asarray([0], dtype=xp.float64) + + res = cubature( + basic_1d_integrand, + a, + b, + args=(n, xp), + ) + + xp_assert_close( + res.estimate, + xp.asarray([[0], [0], [0], [0], [0]], dtype=xp.float64), + rtol=1e-8, + atol=0, + ) + + def test_limits_other_way_around(self, xp): + n = xp.arange(5, dtype=xp.float64) + + a = xp.asarray([2], dtype=xp.float64) + b = xp.asarray([0], dtype=xp.float64) + + res = cubature( + basic_1d_integrand, + a, + b, + args=(n, xp), + ) + + xp_assert_close( + res.estimate, + -basic_1d_integrand_exact(n, xp), + rtol=1e-8, + atol=0, + ) + + def test_result_dtype_promoted_correctly(self, xp): + result_dtype = cubature( + basic_1d_integrand, + xp.asarray([0], dtype=xp.float64), + xp.asarray([1], dtype=xp.float64), + points=[], + args=(xp.asarray([1], dtype=xp.float64), xp), + ).estimate.dtype + + assert result_dtype == xp.float64 + + result_dtype = cubature( + basic_1d_integrand, + xp.asarray([0], dtype=xp.float32), + xp.asarray([1], dtype=xp.float32), + points=[], + args=(xp.asarray([1], dtype=xp.float32), xp), + ).estimate.dtype + + assert result_dtype == xp.float32 + + result_dtype = cubature( + basic_1d_integrand, + xp.asarray([0], dtype=xp.float32), + xp.asarray([1], dtype=xp.float64), + points=[], + args=(xp.asarray([1], dtype=xp.float32), xp), + ).estimate.dtype + + assert result_dtype == xp.float64 + + +@pytest.mark.parametrize("rtol", [1e-4]) +@pytest.mark.parametrize("atol", [1e-5]) +@pytest.mark.parametrize("rule", [ + "gk15", + "gk21", + "genz-malik", +]) +@array_api_compatible +class TestCubatureProblems: + """ + Tests that `cubature` gives the correct answer. + """ + + @pytest.mark.parametrize("problem", [ + # -- f1 -- + ( + # Function to integrate, like `f(x, *args)` + genz_malik_1980_f_1, + + # Exact solution, like `exact(a, b, *args)` + genz_malik_1980_f_1_exact, + + # Coordinates of `a` + [0], + + # Coordinates of `b` + [10], + + # Arguments to pass to `f` and `exact` + ( + 1/4, + [5], + ) + ), + ( + genz_malik_1980_f_1, + genz_malik_1980_f_1_exact, + [0, 0], + [1, 1], + ( + 1/4, + [2, 4], + ), + ), + ( + genz_malik_1980_f_1, + genz_malik_1980_f_1_exact, + [0, 0], + [5, 5], + ( + 1/2, + [2, 4], + ) + ), + ( + genz_malik_1980_f_1, + genz_malik_1980_f_1_exact, + [0, 0, 0], + [5, 5, 5], + ( + 1/2, + [1, 1, 1], + ) + ), + + # -- f2 -- + ( + genz_malik_1980_f_2, + genz_malik_1980_f_2_exact, + [-1], + [1], + ( + [5], + [4], + ) + ), + ( + genz_malik_1980_f_2, + genz_malik_1980_f_2_exact, + + [0, 0], + [10, 50], + ( + [-3, 3], + [-2, 2], + ), + ), + ( + genz_malik_1980_f_2, + genz_malik_1980_f_2_exact, + [0, 0, 0], + [1, 1, 1], + ( + [1, 1, 1], + [1, 1, 1], + ) + ), + ( + genz_malik_1980_f_2, + genz_malik_1980_f_2_exact, + [0, 0, 0], + [1, 1, 1], + ( + [2, 3, 4], + [2, 3, 4], + ) + ), + ( + genz_malik_1980_f_2, + genz_malik_1980_f_2_exact, + [-1, -1, -1], + [1, 1, 1], + ( + [1, 1, 1], + [2, 2, 2], + ) + ), + ( + genz_malik_1980_f_2, + genz_malik_1980_f_2_exact, + [-1, -1, -1, -1], + [1, 1, 1, 1], + ( + [1, 1, 1, 1], + [1, 1, 1, 1], + ) + ), + + # -- f3 -- + ( + genz_malik_1980_f_3, + genz_malik_1980_f_3_exact, + [-1], + [1], + ( + [1/2], + ), + ), + ( + genz_malik_1980_f_3, + genz_malik_1980_f_3_exact, + [0, -1], + [1, 1], + ( + [5, 5], + ), + ), + ( + genz_malik_1980_f_3, + genz_malik_1980_f_3_exact, + [-1, -1, -1], + [1, 1, 1], + ( + [1, 1, 1], + ), + ), + + # -- f4 -- + ( + genz_malik_1980_f_4, + genz_malik_1980_f_4_exact, + [0], + [2], + ( + [1], + ), + ), + ( + genz_malik_1980_f_4, + genz_malik_1980_f_4_exact, + [0, 0], + [2, 1], + ([1, 1],), + ), + ( + genz_malik_1980_f_4, + genz_malik_1980_f_4_exact, + [0, 0, 0], + [1, 1, 1], + ([1, 1, 1],), + ), + + # -- f5 -- + ( + genz_malik_1980_f_5, + genz_malik_1980_f_5_exact, + [-1], + [1], + ( + [-2], + [2], + ), + ), + ( + genz_malik_1980_f_5, + genz_malik_1980_f_5_exact, + [-1, -1], + [1, 1], + ( + [2, 3], + [4, 5], + ), + ), + ( + genz_malik_1980_f_5, + genz_malik_1980_f_5_exact, + [-1, -1], + [1, 1], + ( + [-1, 1], + [0, 0], + ), + ), + ( + genz_malik_1980_f_5, + genz_malik_1980_f_5_exact, + [-1, -1, -1], + [1, 1, 1], + ( + [1, 1, 1], + [1, 1, 1], + ), + ), + ]) + def test_scalar_output(self, problem, rule, rtol, atol, xp): + f, exact, a, b, args = problem + + a = xp.asarray(a, dtype=xp.float64) + b = xp.asarray(b, dtype=xp.float64) + args = tuple(xp.asarray(arg, dtype=xp.float64) for arg in args) + + ndim = xp_size(a) + + if rule == "genz-malik" and ndim < 2: + pytest.skip("Genz-Malik cubature does not support 1D integrals") + + res = cubature( + f, + a, + b, + rule=rule, + rtol=rtol, + atol=atol, + args=(*args, xp), + ) + + assert res.status == "converged" + + est = res.estimate + exact_sol = exact(a, b, *args, xp) + + xp_assert_close( + est, + exact_sol, + rtol=rtol, + atol=atol, + err_msg=f"estimate_error={res.error}, subdivisions={res.subdivisions}", + ) + + @pytest.mark.parametrize("problem", [ + ( + # Function to integrate, like `f(x, *args)` + genz_malik_1980_f_1, + + # Exact solution, like `exact(a, b, *args)` + genz_malik_1980_f_1_exact, + + # Function that generates random args of a certain shape. + genz_malik_1980_f_1_random_args, + ), + ( + genz_malik_1980_f_2, + genz_malik_1980_f_2_exact, + genz_malik_1980_f_2_random_args, + ), + ( + genz_malik_1980_f_3, + genz_malik_1980_f_3_exact, + genz_malik_1980_f_3_random_args + ), + ( + genz_malik_1980_f_4, + genz_malik_1980_f_4_exact, + genz_malik_1980_f_4_random_args + ), + ( + genz_malik_1980_f_5, + genz_malik_1980_f_5_exact, + genz_malik_1980_f_5_random_args, + ), + ]) + @pytest.mark.parametrize("shape", [ + (2,), + (3,), + (4,), + (1, 2), + (1, 3), + (1, 4), + (3, 2), + (3, 4, 2), + (2, 1, 3), + ]) + def test_array_output(self, problem, rule, shape, rtol, atol, xp): + rng = np_compat.random.default_rng(1) + ndim = shape[-1] + + if rule == "genz-malik" and ndim < 2: + pytest.skip("Genz-Malik cubature does not support 1D integrals") + + if rule == "genz-malik" and ndim >= 5: + pytest.mark.slow("Gauss-Kronrod is slow in >= 5 dim") + + f, exact, random_args = problem + args = random_args(rng, shape, xp) + + a = xp.asarray([0] * ndim, dtype=xp.float64) + b = xp.asarray([1] * ndim, dtype=xp.float64) + + res = cubature( + f, + a, + b, + rule=rule, + rtol=rtol, + atol=atol, + args=(*args, xp), + ) + + est = res.estimate + exact_sol = exact(a, b, *args, xp) + + xp_assert_close( + est, + exact_sol, + rtol=rtol, + atol=atol, + err_msg=f"estimate_error={res.error}, subdivisions={res.subdivisions}", + ) + + err_msg = (f"estimate_error={res.error}, " + f"subdivisions= {res.subdivisions}, " + f"true_error={xp.abs(res.estimate - exact_sol)}") + assert res.status == "converged", err_msg + + assert res.estimate.shape == shape[:-1] + + @pytest.mark.parametrize("problem", [ + ( + # Function to integrate + lambda x, xp: x, + + # Exact value + [50.0], + + # Coordinates of `a` + [0], + + # Coordinates of `b` + [10], + + # Points by which to split up the initial region + None, + ), + ( + lambda x, xp: xp.sin(x)/x, + [2.551496047169878], # si(1) + si(2), + [-1], + [2], + [ + [0.0], + ], + ), + ( + lambda x, xp: xp.ones((x.shape[0], 1)), + [1.0], + [0, 0, 0], + [1, 1, 1], + [ + [0.5, 0.5, 0.5], + ], + ), + ( + lambda x, xp: xp.ones((x.shape[0], 1)), + [1.0], + [0, 0, 0], + [1, 1, 1], + [ + [0.25, 0.25, 0.25], + [0.5, 0.5, 0.5], + ], + ), + ( + lambda x, xp: xp.ones((x.shape[0], 1)), + [1.0], + [0, 0, 0], + [1, 1, 1], + [ + [0.1, 0.25, 0.5], + [0.25, 0.25, 0.25], + [0.5, 0.5, 0.5], + ], + ) + ]) + def test_break_points(self, problem, rule, rtol, atol, xp): + f, exact, a, b, points = problem + + a = xp.asarray(a, dtype=xp.float64) + b = xp.asarray(b, dtype=xp.float64) + exact = xp.asarray(exact, dtype=xp.float64) + + if points is not None: + points = [xp.asarray(point, dtype=xp.float64) for point in points] + + ndim = xp_size(a) + + if rule == "genz-malik" and ndim < 2: + pytest.skip("Genz-Malik cubature does not support 1D integrals") + + if rule == "genz-malik" and ndim >= 5: + pytest.mark.slow("Gauss-Kronrod is slow in >= 5 dim") + + res = cubature( + f, + a, + b, + rule=rule, + rtol=rtol, + atol=atol, + points=points, + args=(xp,), + ) + + xp_assert_close( + res.estimate, + exact, + rtol=rtol, + atol=atol, + err_msg=f"estimate_error={res.error}, subdivisions={res.subdivisions}", + check_dtype=False, + ) + + err_msg = (f"estimate_error={res.error}, " + f"subdivisions= {res.subdivisions}, " + f"true_error={xp.abs(res.estimate - exact)}") + assert res.status == "converged", err_msg + + @skip_xp_backends( + "jax.numpy", + reasons=["transforms make use of indexing assignment"], + ) + @pytest.mark.parametrize("problem", [ + ( + # Function to integrate + f_gaussian, + + # Exact solution + f_gaussian_exact, + + # Arguments passed to f + f_gaussian_random_args, + (1, 1), + + # Limits, have to match the shape of the arguments + [-math.inf], # a + [math.inf], # b + ), + ( + f_gaussian, + f_gaussian_exact, + f_gaussian_random_args, + (2, 2), + [-math.inf, -math.inf], + [math.inf, math.inf], + ), + ( + f_gaussian, + f_gaussian_exact, + f_gaussian_random_args, + (1, 1), + [0], + [math.inf], + ), + ( + f_gaussian, + f_gaussian_exact, + f_gaussian_random_args, + (1, 1), + [-math.inf], + [0], + ), + ( + f_gaussian, + f_gaussian_exact, + f_gaussian_random_args, + (2, 2), + [0, 0], + [math.inf, math.inf], + ), + ( + f_gaussian, + f_gaussian_exact, + f_gaussian_random_args, + (2, 2), + [0, -math.inf], + [math.inf, math.inf], + ), + ( + f_gaussian, + f_gaussian_exact, + f_gaussian_random_args, + (1, 4), + [0, 0, -math.inf, -math.inf], + [math.inf, math.inf, math.inf, math.inf], + ), + ( + f_gaussian, + f_gaussian_exact, + f_gaussian_random_args, + (1, 4), + [-math.inf, -math.inf, -math.inf, -math.inf], + [0, 0, math.inf, math.inf], + ), + ( + lambda x, xp: 1/xp.prod(x, axis=-1)**2, + + # Exact only for the below limits, not for general `a` and `b`. + lambda a, b, xp: xp.asarray(1/6, dtype=xp.float64), + + # Arguments + lambda rng, shape, xp: tuple(), + tuple(), + + [1, -math.inf, 3], + [math.inf, -2, math.inf], + ), + + # This particular problem can be slow + pytest.param( + ( + # f(x, y, z, w) = x^n * sqrt(y) * exp(-y-z**2-w**2) for n in [0,1,2,3] + f_modified_gaussian, + + # This exact solution is for the below limits, not in general + f_modified_gaussian_exact, + + # Constant arguments + lambda rng, shape, xp: (xp.asarray([0, 1, 2, 3, 4], dtype=xp.float64),), + tuple(), + + [0, 0, -math.inf, -math.inf], + [1, math.inf, math.inf, math.inf] + ), + + marks=pytest.mark.xslow, + ), + ]) + def test_infinite_limits(self, problem, rule, rtol, atol, xp): + rng = np_compat.random.default_rng(1) + f, exact, random_args_func, random_args_shape, a, b = problem + + a = xp.asarray(a, dtype=xp.float64) + b = xp.asarray(b, dtype=xp.float64) + args = random_args_func(rng, random_args_shape, xp) + + ndim = xp_size(a) + + if rule == "genz-malik" and ndim < 2: + pytest.skip("Genz-Malik cubature does not support 1D integrals") + + if rule == "genz-malik" and ndim >= 4: + pytest.mark.slow("Genz-Malik is slow in >= 5 dim") + + if rule == "genz-malik" and ndim >= 4 and is_array_api_strict(xp): + pytest.mark.xslow("Genz-Malik very slow for array_api_strict in >= 4 dim") + + res = cubature( + f, + a, + b, + rule=rule, + rtol=rtol, + atol=atol, + args=(*args, xp), + ) + + assert res.status == "converged" + + xp_assert_close( + res.estimate, + exact(a, b, *args, xp), + rtol=rtol, + atol=atol, + err_msg=f"error_estimate={res.error}, subdivisions={res.subdivisions}", + check_0d=False, + ) + + @skip_xp_backends( + "jax.numpy", + reasons=["transforms make use of indexing assignment"], + ) + @pytest.mark.parametrize("problem", [ + ( + # Function to integrate + lambda x, xp: (xp.sin(x) / x)**8, + + # Exact value + [151/315 * math.pi], + + # Limits + [-math.inf], + [math.inf], + + # Breakpoints + [[0]], + + ), + ( + # Function to integrate + lambda x, xp: (xp.sin(x[:, 0]) / x[:, 0])**8, + + # Exact value + 151/315 * math.pi, + + # Limits + [-math.inf, 0], + [math.inf, 1], + + # Breakpoints + [[0, 0.5]], + + ) + ]) + def test_infinite_limits_and_break_points(self, problem, rule, rtol, atol, xp): + f, exact, a, b, points = problem + + a = xp.asarray(a, dtype=xp.float64) + b = xp.asarray(b, dtype=xp.float64) + exact = xp.asarray(exact, dtype=xp.float64) + + ndim = xp_size(a) + + if rule == "genz-malik" and ndim < 2: + pytest.skip("Genz-Malik cubature does not support 1D integrals") + + if points is not None: + points = [xp.asarray(point, dtype=xp.float64) for point in points] + + res = cubature( + f, + a, + b, + rule=rule, + rtol=rtol, + atol=atol, + points=points, + args=(xp,), + ) + + assert res.status == "converged" + + xp_assert_close( + res.estimate, + exact, + rtol=rtol, + atol=atol, + err_msg=f"error_estimate={res.error}, subdivisions={res.subdivisions}", + check_0d=False, + ) + + +@array_api_compatible +class TestRules: + """ + Tests related to the general Rule interface (currently private). + """ + + @pytest.mark.parametrize("problem", [ + ( + # 2D problem, 1D rule + [0, 0], + [1, 1], + GaussKronrodQuadrature, + (21,), + ), + ( + # 1D problem, 2D rule + [0], + [1], + GenzMalikCubature, + (2,), + ) + ]) + def test_incompatible_dimension_raises_error(self, problem, xp): + a, b, quadrature, quadrature_args = problem + rule = quadrature(*quadrature_args, xp=xp) + + a = xp.asarray(a, dtype=xp.float64) + b = xp.asarray(b, dtype=xp.float64) + + with pytest.raises(Exception, match="incompatible dimension"): + rule.estimate(basic_1d_integrand, a, b, args=(xp,)) + + def test_estimate_with_base_classes_raise_error(self, xp): + a = xp.asarray([0]) + b = xp.asarray([1]) + + for base_class in [Rule(), FixedRule()]: + with pytest.raises(Exception): + base_class.estimate(basic_1d_integrand, a, b, args=(xp,)) + + +@array_api_compatible +class TestRulesQuadrature: + """ + Tests underlying quadrature rules (ndim == 1). + """ + + @pytest.mark.parametrize(("rule", "rule_args"), [ + (GaussLegendreQuadrature, (3,)), + (GaussLegendreQuadrature, (5,)), + (GaussLegendreQuadrature, (10,)), + (GaussKronrodQuadrature, (15,)), + (GaussKronrodQuadrature, (21,)), + ]) + def test_base_1d_quadratures_simple(self, rule, rule_args, xp): + quadrature = rule(*rule_args, xp=xp) + + n = xp.arange(5, dtype=xp.float64) + + def f(x): + x_reshaped = xp.reshape(x, (-1, 1, 1)) + n_reshaped = xp.reshape(n, (1, -1, 1)) + + return x_reshaped**n_reshaped + + a = xp.asarray([0], dtype=xp.float64) + b = xp.asarray([2], dtype=xp.float64) + + exact = xp.reshape(2**(n+1)/(n+1), (-1, 1)) + estimate = quadrature.estimate(f, a, b) + + xp_assert_close( + estimate, + exact, + rtol=1e-8, + atol=0, + ) + + @pytest.mark.parametrize(("rule_pair", "rule_pair_args"), [ + ((GaussLegendreQuadrature, GaussLegendreQuadrature), (10, 5)), + ]) + def test_base_1d_quadratures_error_from_difference(self, rule_pair, rule_pair_args, + xp): + n = xp.arange(5, dtype=xp.float64) + a = xp.asarray([0], dtype=xp.float64) + b = xp.asarray([2], dtype=xp.float64) + + higher = rule_pair[0](rule_pair_args[0], xp=xp) + lower = rule_pair[1](rule_pair_args[1], xp=xp) + + rule = NestedFixedRule(higher, lower) + res = cubature( + basic_1d_integrand, + a, b, + rule=rule, + rtol=1e-8, + args=(n, xp), + ) + + xp_assert_close( + res.estimate, + basic_1d_integrand_exact(n, xp), + rtol=1e-8, + atol=0, + ) + + @pytest.mark.parametrize("quadrature", [ + GaussLegendreQuadrature + ]) + def test_one_point_fixed_quad_impossible(self, quadrature, xp): + with pytest.raises(Exception): + quadrature(1, xp=xp) + + +@array_api_compatible +class TestRulesCubature: + """ + Tests underlying cubature rules (ndim >= 2). + """ + + @pytest.mark.parametrize("ndim", range(2, 11)) + def test_genz_malik_func_evaluations(self, ndim, xp): + """ + Tests that the number of function evaluations required for Genz-Malik cubature + matches the number in Genz and Malik 1980. + """ + + nodes, _ = GenzMalikCubature(ndim, xp=xp).nodes_and_weights + + assert nodes.shape[0] == (2**ndim) + 2*ndim**2 + 2*ndim + 1 + + def test_genz_malik_1d_raises_error(self, xp): + with pytest.raises(Exception, match="only defined for ndim >= 2"): + GenzMalikCubature(1, xp=xp) + + +@array_api_compatible +@skip_xp_backends( + "jax.numpy", + reasons=["transforms make use of indexing assignment"], +) +class TestTransformations: + @pytest.mark.parametrize(("a", "b", "points"), [ + ( + [0, 1, -math.inf], + [1, math.inf, math.inf], + [ + [1, 1, 1], + [0.5, 10, 10], + ] + ) + ]) + def test_infinite_limits_maintains_points(self, a, b, points, xp): + """ + Test that break points are correctly mapped under the _InfiniteLimitsTransform + transformation. + """ + + xp_compat = array_namespace(xp.empty(0)) + points = [xp.asarray(p, dtype=xp.float64) for p in points] + + f_transformed = _InfiniteLimitsTransform( + # Bind `points` and `xp` argument in f + lambda x: f_with_problematic_points(x, points, xp_compat), + xp.asarray(a, dtype=xp_compat.float64), + xp.asarray(b, dtype=xp_compat.float64), + xp=xp_compat, + ) + + for point in points: + transformed_point = f_transformed.inv(xp_compat.reshape(point, (1, -1))) + + with pytest.raises(Exception, match="called with a problematic point"): + f_transformed(transformed_point) + + +class BadErrorRule(Rule): + """ + A rule with fake high error so that cubature will keep on subdividing. + """ + + def estimate(self, f, a, b, args=()): + xp = array_namespace(a, b) + underlying = GaussLegendreQuadrature(10, xp=xp) + + return underlying.estimate(f, a, b, args) + + def estimate_error(self, f, a, b, args=()): + xp = array_namespace(a, b) + return xp.asarray(1e6, dtype=xp.float64) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/tests/test_integrate.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/tests/test_integrate.py new file mode 100644 index 0000000000000000000000000000000000000000..44bfecdaac0f00b413538510c61dd1317a076261 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/tests/test_integrate.py @@ -0,0 +1,840 @@ +# Authors: Nils Wagner, Ed Schofield, Pauli Virtanen, John Travers +""" +Tests for numerical integration. +""" +import numpy as np +from numpy import (arange, zeros, array, dot, sqrt, cos, sin, eye, pi, exp, + allclose) + +from numpy.testing import ( + assert_, assert_array_almost_equal, + assert_allclose, assert_array_equal, assert_equal, assert_warns) +import pytest +from pytest import raises as assert_raises +from scipy.integrate import odeint, ode, complex_ode + +#------------------------------------------------------------------------------ +# Test ODE integrators +#------------------------------------------------------------------------------ + + +class TestOdeint: + # Check integrate.odeint + + def _do_problem(self, problem): + t = arange(0.0, problem.stop_t, 0.05) + + # Basic case + z, infodict = odeint(problem.f, problem.z0, t, full_output=True) + assert_(problem.verify(z, t)) + + # Use tfirst=True + z, infodict = odeint(lambda t, y: problem.f(y, t), problem.z0, t, + full_output=True, tfirst=True) + assert_(problem.verify(z, t)) + + if hasattr(problem, 'jac'): + # Use Dfun + z, infodict = odeint(problem.f, problem.z0, t, Dfun=problem.jac, + full_output=True) + assert_(problem.verify(z, t)) + + # Use Dfun and tfirst=True + z, infodict = odeint(lambda t, y: problem.f(y, t), problem.z0, t, + Dfun=lambda t, y: problem.jac(y, t), + full_output=True, tfirst=True) + assert_(problem.verify(z, t)) + + def test_odeint(self): + for problem_cls in PROBLEMS: + problem = problem_cls() + if problem.cmplx: + continue + self._do_problem(problem) + + +class TestODEClass: + + ode_class = None # Set in subclass. + + def _do_problem(self, problem, integrator, method='adams'): + + # ode has callback arguments in different order than odeint + def f(t, z): + return problem.f(z, t) + jac = None + if hasattr(problem, 'jac'): + def jac(t, z): + return problem.jac(z, t) + + integrator_params = {} + if problem.lband is not None or problem.uband is not None: + integrator_params['uband'] = problem.uband + integrator_params['lband'] = problem.lband + + ig = self.ode_class(f, jac) + ig.set_integrator(integrator, + atol=problem.atol/10, + rtol=problem.rtol/10, + method=method, + **integrator_params) + + ig.set_initial_value(problem.z0, t=0.0) + z = ig.integrate(problem.stop_t) + + assert_array_equal(z, ig.y) + assert_(ig.successful(), (problem, method)) + assert_(ig.get_return_code() > 0, (problem, method)) + assert_(problem.verify(array([z]), problem.stop_t), (problem, method)) + + +class TestOde(TestODEClass): + + ode_class = ode + + def test_vode(self): + # Check the vode solver + for problem_cls in PROBLEMS: + problem = problem_cls() + if problem.cmplx: + continue + if not problem.stiff: + self._do_problem(problem, 'vode', 'adams') + self._do_problem(problem, 'vode', 'bdf') + + def test_zvode(self): + # Check the zvode solver + for problem_cls in PROBLEMS: + problem = problem_cls() + if not problem.stiff: + self._do_problem(problem, 'zvode', 'adams') + self._do_problem(problem, 'zvode', 'bdf') + + def test_lsoda(self): + # Check the lsoda solver + for problem_cls in PROBLEMS: + problem = problem_cls() + if problem.cmplx: + continue + self._do_problem(problem, 'lsoda') + + def test_dopri5(self): + # Check the dopri5 solver + for problem_cls in PROBLEMS: + problem = problem_cls() + if problem.cmplx: + continue + if problem.stiff: + continue + if hasattr(problem, 'jac'): + continue + self._do_problem(problem, 'dopri5') + + def test_dop853(self): + # Check the dop853 solver + for problem_cls in PROBLEMS: + problem = problem_cls() + if problem.cmplx: + continue + if problem.stiff: + continue + if hasattr(problem, 'jac'): + continue + self._do_problem(problem, 'dop853') + + @pytest.mark.thread_unsafe + def test_concurrent_fail(self): + for sol in ('vode', 'zvode', 'lsoda'): + def f(t, y): + return 1.0 + + r = ode(f).set_integrator(sol) + r.set_initial_value(0, 0) + + r2 = ode(f).set_integrator(sol) + r2.set_initial_value(0, 0) + + r.integrate(r.t + 0.1) + r2.integrate(r2.t + 0.1) + + assert_raises(RuntimeError, r.integrate, r.t + 0.1) + + def test_concurrent_ok(self, num_parallel_threads): + def f(t, y): + return 1.0 + + for k in range(3): + for sol in ('vode', 'zvode', 'lsoda', 'dopri5', 'dop853'): + if sol in {'vode', 'zvode', 'lsoda'} and num_parallel_threads > 1: + continue + r = ode(f).set_integrator(sol) + r.set_initial_value(0, 0) + + r2 = ode(f).set_integrator(sol) + r2.set_initial_value(0, 0) + + r.integrate(r.t + 0.1) + r2.integrate(r2.t + 0.1) + r2.integrate(r2.t + 0.1) + + assert_allclose(r.y, 0.1) + assert_allclose(r2.y, 0.2) + + for sol in ('dopri5', 'dop853'): + r = ode(f).set_integrator(sol) + r.set_initial_value(0, 0) + + r2 = ode(f).set_integrator(sol) + r2.set_initial_value(0, 0) + + r.integrate(r.t + 0.1) + r.integrate(r.t + 0.1) + r2.integrate(r2.t + 0.1) + r.integrate(r.t + 0.1) + r2.integrate(r2.t + 0.1) + + assert_allclose(r.y, 0.3) + assert_allclose(r2.y, 0.2) + + +class TestComplexOde(TestODEClass): + + ode_class = complex_ode + + def test_vode(self): + # Check the vode solver + for problem_cls in PROBLEMS: + problem = problem_cls() + if not problem.stiff: + self._do_problem(problem, 'vode', 'adams') + else: + self._do_problem(problem, 'vode', 'bdf') + + def test_lsoda(self): + + # Check the lsoda solver + for problem_cls in PROBLEMS: + problem = problem_cls() + self._do_problem(problem, 'lsoda') + + def test_dopri5(self): + # Check the dopri5 solver + for problem_cls in PROBLEMS: + problem = problem_cls() + if problem.stiff: + continue + if hasattr(problem, 'jac'): + continue + self._do_problem(problem, 'dopri5') + + def test_dop853(self): + # Check the dop853 solver + for problem_cls in PROBLEMS: + problem = problem_cls() + if problem.stiff: + continue + if hasattr(problem, 'jac'): + continue + self._do_problem(problem, 'dop853') + + +class TestSolout: + # Check integrate.ode correctly handles solout for dopri5 and dop853 + def _run_solout_test(self, integrator): + # Check correct usage of solout + ts = [] + ys = [] + t0 = 0.0 + tend = 10.0 + y0 = [1.0, 2.0] + + def solout(t, y): + ts.append(t) + ys.append(y.copy()) + + def rhs(t, y): + return [y[0] + y[1], -y[1]**2] + + ig = ode(rhs).set_integrator(integrator) + ig.set_solout(solout) + ig.set_initial_value(y0, t0) + ret = ig.integrate(tend) + assert_array_equal(ys[0], y0) + assert_array_equal(ys[-1], ret) + assert_equal(ts[0], t0) + assert_equal(ts[-1], tend) + + def test_solout(self): + for integrator in ('dopri5', 'dop853'): + self._run_solout_test(integrator) + + def _run_solout_after_initial_test(self, integrator): + # Check if solout works even if it is set after the initial value. + ts = [] + ys = [] + t0 = 0.0 + tend = 10.0 + y0 = [1.0, 2.0] + + def solout(t, y): + ts.append(t) + ys.append(y.copy()) + + def rhs(t, y): + return [y[0] + y[1], -y[1]**2] + + ig = ode(rhs).set_integrator(integrator) + ig.set_initial_value(y0, t0) + ig.set_solout(solout) + ret = ig.integrate(tend) + assert_array_equal(ys[0], y0) + assert_array_equal(ys[-1], ret) + assert_equal(ts[0], t0) + assert_equal(ts[-1], tend) + + def test_solout_after_initial(self): + for integrator in ('dopri5', 'dop853'): + self._run_solout_after_initial_test(integrator) + + def _run_solout_break_test(self, integrator): + # Check correct usage of stopping via solout + ts = [] + ys = [] + t0 = 0.0 + tend = 10.0 + y0 = [1.0, 2.0] + + def solout(t, y): + ts.append(t) + ys.append(y.copy()) + if t > tend/2.0: + return -1 + + def rhs(t, y): + return [y[0] + y[1], -y[1]**2] + + ig = ode(rhs).set_integrator(integrator) + ig.set_solout(solout) + ig.set_initial_value(y0, t0) + ret = ig.integrate(tend) + assert_array_equal(ys[0], y0) + assert_array_equal(ys[-1], ret) + assert_equal(ts[0], t0) + assert_(ts[-1] > tend/2.0) + assert_(ts[-1] < tend) + + def test_solout_break(self): + for integrator in ('dopri5', 'dop853'): + self._run_solout_break_test(integrator) + + +class TestComplexSolout: + # Check integrate.ode correctly handles solout for dopri5 and dop853 + def _run_solout_test(self, integrator): + # Check correct usage of solout + ts = [] + ys = [] + t0 = 0.0 + tend = 20.0 + y0 = [0.0] + + def solout(t, y): + ts.append(t) + ys.append(y.copy()) + + def rhs(t, y): + return [1.0/(t - 10.0 - 1j)] + + ig = complex_ode(rhs).set_integrator(integrator) + ig.set_solout(solout) + ig.set_initial_value(y0, t0) + ret = ig.integrate(tend) + assert_array_equal(ys[0], y0) + assert_array_equal(ys[-1], ret) + assert_equal(ts[0], t0) + assert_equal(ts[-1], tend) + + def test_solout(self): + for integrator in ('dopri5', 'dop853'): + self._run_solout_test(integrator) + + def _run_solout_break_test(self, integrator): + # Check correct usage of stopping via solout + ts = [] + ys = [] + t0 = 0.0 + tend = 20.0 + y0 = [0.0] + + def solout(t, y): + ts.append(t) + ys.append(y.copy()) + if t > tend/2.0: + return -1 + + def rhs(t, y): + return [1.0/(t - 10.0 - 1j)] + + ig = complex_ode(rhs).set_integrator(integrator) + ig.set_solout(solout) + ig.set_initial_value(y0, t0) + ret = ig.integrate(tend) + assert_array_equal(ys[0], y0) + assert_array_equal(ys[-1], ret) + assert_equal(ts[0], t0) + assert_(ts[-1] > tend/2.0) + assert_(ts[-1] < tend) + + def test_solout_break(self): + for integrator in ('dopri5', 'dop853'): + self._run_solout_break_test(integrator) + + +#------------------------------------------------------------------------------ +# Test problems +#------------------------------------------------------------------------------ + + +class ODE: + """ + ODE problem + """ + stiff = False + cmplx = False + stop_t = 1 + z0 = [] + + lband = None + uband = None + + atol = 1e-6 + rtol = 1e-5 + + +class SimpleOscillator(ODE): + r""" + Free vibration of a simple oscillator:: + m \ddot{u} + k u = 0, u(0) = u_0 \dot{u}(0) \dot{u}_0 + Solution:: + u(t) = u_0*cos(sqrt(k/m)*t)+\dot{u}_0*sin(sqrt(k/m)*t)/sqrt(k/m) + """ + stop_t = 1 + 0.09 + z0 = array([1.0, 0.1], float) + + k = 4.0 + m = 1.0 + + def f(self, z, t): + tmp = zeros((2, 2), float) + tmp[0, 1] = 1.0 + tmp[1, 0] = -self.k / self.m + return dot(tmp, z) + + def verify(self, zs, t): + omega = sqrt(self.k / self.m) + u = self.z0[0]*cos(omega*t) + self.z0[1]*sin(omega*t)/omega + return allclose(u, zs[:, 0], atol=self.atol, rtol=self.rtol) + + +class ComplexExp(ODE): + r"""The equation :lm:`\dot u = i u`""" + stop_t = 1.23*pi + z0 = exp([1j, 2j, 3j, 4j, 5j]) + cmplx = True + + def f(self, z, t): + return 1j*z + + def jac(self, z, t): + return 1j*eye(5) + + def verify(self, zs, t): + u = self.z0 * exp(1j*t) + return allclose(u, zs, atol=self.atol, rtol=self.rtol) + + +class Pi(ODE): + r"""Integrate 1/(t + 1j) from t=-10 to t=10""" + stop_t = 20 + z0 = [0] + cmplx = True + + def f(self, z, t): + return array([1./(t - 10 + 1j)]) + + def verify(self, zs, t): + u = -2j * np.arctan(10) + return allclose(u, zs[-1, :], atol=self.atol, rtol=self.rtol) + + +class CoupledDecay(ODE): + r""" + 3 coupled decays suited for banded treatment + (banded mode makes it necessary when N>>3) + """ + + stiff = True + stop_t = 0.5 + z0 = [5.0, 7.0, 13.0] + lband = 1 + uband = 0 + + lmbd = [0.17, 0.23, 0.29] # fictitious decay constants + + def f(self, z, t): + lmbd = self.lmbd + return np.array([-lmbd[0]*z[0], + -lmbd[1]*z[1] + lmbd[0]*z[0], + -lmbd[2]*z[2] + lmbd[1]*z[1]]) + + def jac(self, z, t): + # The full Jacobian is + # + # [-lmbd[0] 0 0 ] + # [ lmbd[0] -lmbd[1] 0 ] + # [ 0 lmbd[1] -lmbd[2]] + # + # The lower and upper bandwidths are lband=1 and uband=0, resp. + # The representation of this array in packed format is + # + # [-lmbd[0] -lmbd[1] -lmbd[2]] + # [ lmbd[0] lmbd[1] 0 ] + + lmbd = self.lmbd + j = np.zeros((self.lband + self.uband + 1, 3), order='F') + + def set_j(ri, ci, val): + j[self.uband + ri - ci, ci] = val + set_j(0, 0, -lmbd[0]) + set_j(1, 0, lmbd[0]) + set_j(1, 1, -lmbd[1]) + set_j(2, 1, lmbd[1]) + set_j(2, 2, -lmbd[2]) + return j + + def verify(self, zs, t): + # Formulae derived by hand + lmbd = np.array(self.lmbd) + d10 = lmbd[1] - lmbd[0] + d21 = lmbd[2] - lmbd[1] + d20 = lmbd[2] - lmbd[0] + e0 = np.exp(-lmbd[0] * t) + e1 = np.exp(-lmbd[1] * t) + e2 = np.exp(-lmbd[2] * t) + u = np.vstack(( + self.z0[0] * e0, + self.z0[1] * e1 + self.z0[0] * lmbd[0] / d10 * (e0 - e1), + self.z0[2] * e2 + self.z0[1] * lmbd[1] / d21 * (e1 - e2) + + lmbd[1] * lmbd[0] * self.z0[0] / d10 * + (1 / d20 * (e0 - e2) - 1 / d21 * (e1 - e2)))).transpose() + return allclose(u, zs, atol=self.atol, rtol=self.rtol) + + +PROBLEMS = [SimpleOscillator, ComplexExp, Pi, CoupledDecay] + +#------------------------------------------------------------------------------ + + +def f(t, x): + dxdt = [x[1], -x[0]] + return dxdt + + +def jac(t, x): + j = array([[0.0, 1.0], + [-1.0, 0.0]]) + return j + + +def f1(t, x, omega): + dxdt = [omega*x[1], -omega*x[0]] + return dxdt + + +def jac1(t, x, omega): + j = array([[0.0, omega], + [-omega, 0.0]]) + return j + + +def f2(t, x, omega1, omega2): + dxdt = [omega1*x[1], -omega2*x[0]] + return dxdt + + +def jac2(t, x, omega1, omega2): + j = array([[0.0, omega1], + [-omega2, 0.0]]) + return j + + +def fv(t, x, omega): + dxdt = [omega[0]*x[1], -omega[1]*x[0]] + return dxdt + + +def jacv(t, x, omega): + j = array([[0.0, omega[0]], + [-omega[1], 0.0]]) + return j + + +class ODECheckParameterUse: + """Call an ode-class solver with several cases of parameter use.""" + + # solver_name must be set before tests can be run with this class. + + # Set these in subclasses. + solver_name = '' + solver_uses_jac = False + + def _get_solver(self, f, jac): + solver = ode(f, jac) + if self.solver_uses_jac: + solver.set_integrator(self.solver_name, atol=1e-9, rtol=1e-7, + with_jacobian=self.solver_uses_jac) + else: + # XXX Shouldn't set_integrator *always* accept the keyword arg + # 'with_jacobian', and perhaps raise an exception if it is set + # to True if the solver can't actually use it? + solver.set_integrator(self.solver_name, atol=1e-9, rtol=1e-7) + return solver + + def _check_solver(self, solver): + ic = [1.0, 0.0] + solver.set_initial_value(ic, 0.0) + solver.integrate(pi) + assert_array_almost_equal(solver.y, [-1.0, 0.0]) + + def test_no_params(self): + solver = self._get_solver(f, jac) + self._check_solver(solver) + + def test_one_scalar_param(self): + solver = self._get_solver(f1, jac1) + omega = 1.0 + solver.set_f_params(omega) + if self.solver_uses_jac: + solver.set_jac_params(omega) + self._check_solver(solver) + + def test_two_scalar_params(self): + solver = self._get_solver(f2, jac2) + omega1 = 1.0 + omega2 = 1.0 + solver.set_f_params(omega1, omega2) + if self.solver_uses_jac: + solver.set_jac_params(omega1, omega2) + self._check_solver(solver) + + def test_vector_param(self): + solver = self._get_solver(fv, jacv) + omega = [1.0, 1.0] + solver.set_f_params(omega) + if self.solver_uses_jac: + solver.set_jac_params(omega) + self._check_solver(solver) + + @pytest.mark.thread_unsafe + def test_warns_on_failure(self): + # Set nsteps small to ensure failure + solver = self._get_solver(f, jac) + solver.set_integrator(self.solver_name, nsteps=1) + ic = [1.0, 0.0] + solver.set_initial_value(ic, 0.0) + assert_warns(UserWarning, solver.integrate, pi) + + +class TestDOPRI5CheckParameterUse(ODECheckParameterUse): + solver_name = 'dopri5' + solver_uses_jac = False + + +class TestDOP853CheckParameterUse(ODECheckParameterUse): + solver_name = 'dop853' + solver_uses_jac = False + + +class TestVODECheckParameterUse(ODECheckParameterUse): + solver_name = 'vode' + solver_uses_jac = True + + +class TestZVODECheckParameterUse(ODECheckParameterUse): + solver_name = 'zvode' + solver_uses_jac = True + + +class TestLSODACheckParameterUse(ODECheckParameterUse): + solver_name = 'lsoda' + solver_uses_jac = True + + +def test_odeint_trivial_time(): + # Test that odeint succeeds when given a single time point + # and full_output=True. This is a regression test for gh-4282. + y0 = 1 + t = [0] + y, info = odeint(lambda y, t: -y, y0, t, full_output=True) + assert_array_equal(y, np.array([[y0]])) + + +def test_odeint_banded_jacobian(): + # Test the use of the `Dfun`, `ml` and `mu` options of odeint. + + def func(y, t, c): + return c.dot(y) + + def jac(y, t, c): + return c + + def jac_transpose(y, t, c): + return c.T.copy(order='C') + + def bjac_rows(y, t, c): + jac = np.vstack((np.r_[0, np.diag(c, 1)], + np.diag(c), + np.r_[np.diag(c, -1), 0], + np.r_[np.diag(c, -2), 0, 0])) + return jac + + def bjac_cols(y, t, c): + return bjac_rows(y, t, c).T.copy(order='C') + + c = array([[-205, 0.01, 0.00, 0.0], + [0.1, -2.50, 0.02, 0.0], + [1e-3, 0.01, -2.0, 0.01], + [0.00, 0.00, 0.1, -1.0]]) + + y0 = np.ones(4) + t = np.array([0, 5, 10, 100]) + + # Use the full Jacobian. + sol1, info1 = odeint(func, y0, t, args=(c,), full_output=True, + atol=1e-13, rtol=1e-11, mxstep=10000, + Dfun=jac) + + # Use the transposed full Jacobian, with col_deriv=True. + sol2, info2 = odeint(func, y0, t, args=(c,), full_output=True, + atol=1e-13, rtol=1e-11, mxstep=10000, + Dfun=jac_transpose, col_deriv=True) + + # Use the banded Jacobian. + sol3, info3 = odeint(func, y0, t, args=(c,), full_output=True, + atol=1e-13, rtol=1e-11, mxstep=10000, + Dfun=bjac_rows, ml=2, mu=1) + + # Use the transposed banded Jacobian, with col_deriv=True. + sol4, info4 = odeint(func, y0, t, args=(c,), full_output=True, + atol=1e-13, rtol=1e-11, mxstep=10000, + Dfun=bjac_cols, ml=2, mu=1, col_deriv=True) + + assert_allclose(sol1, sol2, err_msg="sol1 != sol2") + assert_allclose(sol1, sol3, atol=1e-12, err_msg="sol1 != sol3") + assert_allclose(sol3, sol4, err_msg="sol3 != sol4") + + # Verify that the number of jacobian evaluations was the same for the + # calls of odeint with a full jacobian and with a banded jacobian. This is + # a regression test--there was a bug in the handling of banded jacobians + # that resulted in an incorrect jacobian matrix being passed to the LSODA + # code. That would cause errors or excessive jacobian evaluations. + assert_array_equal(info1['nje'], info2['nje']) + assert_array_equal(info3['nje'], info4['nje']) + + # Test the use of tfirst + sol1ty, info1ty = odeint(lambda t, y, c: func(y, t, c), y0, t, args=(c,), + full_output=True, atol=1e-13, rtol=1e-11, + mxstep=10000, + Dfun=lambda t, y, c: jac(y, t, c), tfirst=True) + # The code should execute the exact same sequence of floating point + # calculations, so these should be exactly equal. We'll be safe and use + # a small tolerance. + assert_allclose(sol1, sol1ty, rtol=1e-12, err_msg="sol1 != sol1ty") + + +def test_odeint_errors(): + def sys1d(x, t): + return -100*x + + def bad1(x, t): + return 1.0/0 + + def bad2(x, t): + return "foo" + + def bad_jac1(x, t): + return 1.0/0 + + def bad_jac2(x, t): + return [["foo"]] + + def sys2d(x, t): + return [-100*x[0], -0.1*x[1]] + + def sys2d_bad_jac(x, t): + return [[1.0/0, 0], [0, -0.1]] + + assert_raises(ZeroDivisionError, odeint, bad1, 1.0, [0, 1]) + assert_raises(ValueError, odeint, bad2, 1.0, [0, 1]) + + assert_raises(ZeroDivisionError, odeint, sys1d, 1.0, [0, 1], Dfun=bad_jac1) + assert_raises(ValueError, odeint, sys1d, 1.0, [0, 1], Dfun=bad_jac2) + + assert_raises(ZeroDivisionError, odeint, sys2d, [1.0, 1.0], [0, 1], + Dfun=sys2d_bad_jac) + + +def test_odeint_bad_shapes(): + # Tests of some errors that can occur with odeint. + + def badrhs(x, t): + return [1, -1] + + def sys1(x, t): + return -100*x + + def badjac(x, t): + return [[0, 0, 0]] + + # y0 must be at most 1-d. + bad_y0 = [[0, 0], [0, 0]] + assert_raises(ValueError, odeint, sys1, bad_y0, [0, 1]) + + # t must be at most 1-d. + bad_t = [[0, 1], [2, 3]] + assert_raises(ValueError, odeint, sys1, [10.0], bad_t) + + # y0 is 10, but badrhs(x, t) returns [1, -1]. + assert_raises(RuntimeError, odeint, badrhs, 10, [0, 1]) + + # shape of array returned by badjac(x, t) is not correct. + assert_raises(RuntimeError, odeint, sys1, [10, 10], [0, 1], Dfun=badjac) + + +def test_repeated_t_values(): + """Regression test for gh-8217.""" + + def func(x, t): + return -0.25*x + + t = np.zeros(10) + sol = odeint(func, [1.], t) + assert_array_equal(sol, np.ones((len(t), 1))) + + tau = 4*np.log(2) + t = [0]*9 + [tau, 2*tau, 2*tau, 3*tau] + sol = odeint(func, [1, 2], t, rtol=1e-12, atol=1e-12) + expected_sol = np.array([[1.0, 2.0]]*9 + + [[0.5, 1.0], + [0.25, 0.5], + [0.25, 0.5], + [0.125, 0.25]]) + assert_allclose(sol, expected_sol) + + # Edge case: empty t sequence. + sol = odeint(func, [1.], []) + assert_array_equal(sol, np.array([], dtype=np.float64).reshape((0, 1))) + + # t values are not monotonic. + assert_raises(ValueError, odeint, func, [1.], [0, 1, 0.5, 0]) + assert_raises(ValueError, odeint, func, [1, 2, 3], [0, -1, -2, 3]) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/tests/test_odeint_jac.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/tests/test_odeint_jac.py new file mode 100644 index 0000000000000000000000000000000000000000..7d28ccc93f4444f3f2e0b71da01c573d4f903dbc --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/tests/test_odeint_jac.py @@ -0,0 +1,74 @@ +import numpy as np +from numpy.testing import assert_equal, assert_allclose +from scipy.integrate import odeint +import scipy.integrate._test_odeint_banded as banded5x5 + + +def rhs(y, t): + dydt = np.zeros_like(y) + banded5x5.banded5x5(t, y, dydt) + return dydt + + +def jac(y, t): + n = len(y) + jac = np.zeros((n, n), order='F') + banded5x5.banded5x5_jac(t, y, 1, 1, jac) + return jac + + +def bjac(y, t): + n = len(y) + bjac = np.zeros((4, n), order='F') + banded5x5.banded5x5_bjac(t, y, 1, 1, bjac) + return bjac + + +JACTYPE_FULL = 1 +JACTYPE_BANDED = 4 + + +def check_odeint(jactype): + if jactype == JACTYPE_FULL: + ml = None + mu = None + jacobian = jac + elif jactype == JACTYPE_BANDED: + ml = 2 + mu = 1 + jacobian = bjac + else: + raise ValueError(f"invalid jactype: {jactype!r}") + + y0 = np.arange(1.0, 6.0) + # These tolerances must match the tolerances used in banded5x5.f. + rtol = 1e-11 + atol = 1e-13 + dt = 0.125 + nsteps = 64 + t = dt * np.arange(nsteps+1) + + sol, info = odeint(rhs, y0, t, + Dfun=jacobian, ml=ml, mu=mu, + atol=atol, rtol=rtol, full_output=True) + yfinal = sol[-1] + odeint_nst = info['nst'][-1] + odeint_nfe = info['nfe'][-1] + odeint_nje = info['nje'][-1] + + y1 = y0.copy() + # Pure Fortran solution. y1 is modified in-place. + nst, nfe, nje = banded5x5.banded5x5_solve(y1, nsteps, dt, jactype) + + # It is likely that yfinal and y1 are *exactly* the same, but + # we'll be cautious and use assert_allclose. + assert_allclose(yfinal, y1, rtol=1e-12) + assert_equal((odeint_nst, odeint_nfe, odeint_nje), (nst, nfe, nje)) + + +def test_odeint_full_jac(): + check_odeint(JACTYPE_FULL) + + +def test_odeint_banded_jac(): + check_odeint(JACTYPE_BANDED) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/tests/test_quadpack.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/tests/test_quadpack.py new file mode 100644 index 0000000000000000000000000000000000000000..e61a69df40f9b5975a6f02f40e6f72e34dbbf297 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/tests/test_quadpack.py @@ -0,0 +1,680 @@ +import sys +import math +import numpy as np +from numpy import sqrt, cos, sin, arctan, exp, log, pi +from numpy.testing import (assert_, + assert_allclose, assert_array_less, assert_almost_equal) +import pytest + +from scipy.integrate import quad, dblquad, tplquad, nquad +from scipy.special import erf, erfc +from scipy._lib._ccallback import LowLevelCallable + +import ctypes +import ctypes.util +from scipy._lib._ccallback_c import sine_ctypes + +import scipy.integrate._test_multivariate as clib_test + + +def assert_quad(value_and_err, tabled_value, error_tolerance=1.5e-8): + value, err = value_and_err + assert_allclose(value, tabled_value, atol=err, rtol=0) + if error_tolerance is not None: + assert_array_less(err, error_tolerance) + + +def get_clib_test_routine(name, restype, *argtypes): + ptr = getattr(clib_test, name) + return ctypes.cast(ptr, ctypes.CFUNCTYPE(restype, *argtypes)) + + +class TestCtypesQuad: + def setup_method(self): + if sys.platform == 'win32': + files = ['api-ms-win-crt-math-l1-1-0.dll'] + elif sys.platform == 'darwin': + files = ['libm.dylib'] + else: + files = ['libm.so', 'libm.so.6'] + + for file in files: + try: + self.lib = ctypes.CDLL(file) + break + except OSError: + pass + else: + # This test doesn't work on some Linux platforms (Fedora for + # example) that put an ld script in libm.so - see gh-5370 + pytest.skip("Ctypes can't import libm.so") + + restype = ctypes.c_double + argtypes = (ctypes.c_double,) + for name in ['sin', 'cos', 'tan']: + func = getattr(self.lib, name) + func.restype = restype + func.argtypes = argtypes + + def test_typical(self): + assert_quad(quad(self.lib.sin, 0, 5), quad(math.sin, 0, 5)[0]) + assert_quad(quad(self.lib.cos, 0, 5), quad(math.cos, 0, 5)[0]) + assert_quad(quad(self.lib.tan, 0, 1), quad(math.tan, 0, 1)[0]) + + def test_ctypes_sine(self): + quad(LowLevelCallable(sine_ctypes), 0, 1) + + def test_ctypes_variants(self): + sin_0 = get_clib_test_routine('_sin_0', ctypes.c_double, + ctypes.c_double, ctypes.c_void_p) + + sin_1 = get_clib_test_routine('_sin_1', ctypes.c_double, + ctypes.c_int, ctypes.POINTER(ctypes.c_double), + ctypes.c_void_p) + + sin_2 = get_clib_test_routine('_sin_2', ctypes.c_double, + ctypes.c_double) + + sin_3 = get_clib_test_routine('_sin_3', ctypes.c_double, + ctypes.c_int, ctypes.POINTER(ctypes.c_double)) + + sin_4 = get_clib_test_routine('_sin_3', ctypes.c_double, + ctypes.c_int, ctypes.c_double) + + all_sigs = [sin_0, sin_1, sin_2, sin_3, sin_4] + legacy_sigs = [sin_2, sin_4] + legacy_only_sigs = [sin_4] + + # LowLevelCallables work for new signatures + for j, func in enumerate(all_sigs): + callback = LowLevelCallable(func) + if func in legacy_only_sigs: + pytest.raises(ValueError, quad, callback, 0, pi) + else: + assert_allclose(quad(callback, 0, pi)[0], 2.0) + + # Plain ctypes items work only for legacy signatures + for j, func in enumerate(legacy_sigs): + if func in legacy_sigs: + assert_allclose(quad(func, 0, pi)[0], 2.0) + else: + pytest.raises(ValueError, quad, func, 0, pi) + + +class TestMultivariateCtypesQuad: + def setup_method(self): + restype = ctypes.c_double + argtypes = (ctypes.c_int, ctypes.c_double) + for name in ['_multivariate_typical', '_multivariate_indefinite', + '_multivariate_sin']: + func = get_clib_test_routine(name, restype, *argtypes) + setattr(self, name, func) + + def test_typical(self): + # 1) Typical function with two extra arguments: + assert_quad(quad(self._multivariate_typical, 0, pi, (2, 1.8)), + 0.30614353532540296487) + + def test_indefinite(self): + # 2) Infinite integration limits --- Euler's constant + assert_quad(quad(self._multivariate_indefinite, 0, np.inf), + 0.577215664901532860606512) + + def test_threadsafety(self): + # Ensure multivariate ctypes are threadsafe + def threadsafety(y): + return y + quad(self._multivariate_sin, 0, 1)[0] + assert_quad(quad(threadsafety, 0, 1), 0.9596976941318602) + + +class TestQuad: + def test_typical(self): + # 1) Typical function with two extra arguments: + def myfunc(x, n, z): # Bessel function integrand + return cos(n*x-z*sin(x))/pi + assert_quad(quad(myfunc, 0, pi, (2, 1.8)), 0.30614353532540296487) + + def test_indefinite(self): + # 2) Infinite integration limits --- Euler's constant + def myfunc(x): # Euler's constant integrand + return -exp(-x)*log(x) + assert_quad(quad(myfunc, 0, np.inf), 0.577215664901532860606512) + + def test_singular(self): + # 3) Singular points in region of integration. + def myfunc(x): + if 0 < x < 2.5: + return sin(x) + elif 2.5 <= x <= 5.0: + return exp(-x) + else: + return 0.0 + + assert_quad(quad(myfunc, 0, 10, points=[2.5, 5.0]), + 1 - cos(2.5) + exp(-2.5) - exp(-5.0)) + + def test_sine_weighted_finite(self): + # 4) Sine weighted integral (finite limits) + def myfunc(x, a): + return exp(a*(x-1)) + + ome = 2.0**3.4 + assert_quad(quad(myfunc, 0, 1, args=20, weight='sin', wvar=ome), + (20*sin(ome)-ome*cos(ome)+ome*exp(-20))/(20**2 + ome**2)) + + def test_sine_weighted_infinite(self): + # 5) Sine weighted integral (infinite limits) + def myfunc(x, a): + return exp(-x*a) + + a = 4.0 + ome = 3.0 + assert_quad(quad(myfunc, 0, np.inf, args=a, weight='sin', wvar=ome), + ome/(a**2 + ome**2)) + + def test_cosine_weighted_infinite(self): + # 6) Cosine weighted integral (negative infinite limits) + def myfunc(x, a): + return exp(x*a) + + a = 2.5 + ome = 2.3 + assert_quad(quad(myfunc, -np.inf, 0, args=a, weight='cos', wvar=ome), + a/(a**2 + ome**2)) + + def test_algebraic_log_weight(self): + # 6) Algebraic-logarithmic weight. + def myfunc(x, a): + return 1/(1+x+2**(-a)) + + a = 1.5 + assert_quad(quad(myfunc, -1, 1, args=a, weight='alg', + wvar=(-0.5, -0.5)), + pi/sqrt((1+2**(-a))**2 - 1)) + + def test_cauchypv_weight(self): + # 7) Cauchy prinicpal value weighting w(x) = 1/(x-c) + def myfunc(x, a): + return 2.0**(-a)/((x-1)**2+4.0**(-a)) + + a = 0.4 + tabledValue = ((2.0**(-0.4)*log(1.5) - + 2.0**(-1.4)*log((4.0**(-a)+16) / (4.0**(-a)+1)) - + arctan(2.0**(a+2)) - + arctan(2.0**a)) / + (4.0**(-a) + 1)) + assert_quad(quad(myfunc, 0, 5, args=0.4, weight='cauchy', wvar=2.0), + tabledValue, error_tolerance=1.9e-8) + + def test_b_less_than_a(self): + def f(x, p, q): + return p * np.exp(-q*x) + + val_1, err_1 = quad(f, 0, np.inf, args=(2, 3)) + val_2, err_2 = quad(f, np.inf, 0, args=(2, 3)) + assert_allclose(val_1, -val_2, atol=max(err_1, err_2)) + + def test_b_less_than_a_2(self): + def f(x, s): + return np.exp(-x**2 / 2 / s) / np.sqrt(2.*s) + + val_1, err_1 = quad(f, -np.inf, np.inf, args=(2,)) + val_2, err_2 = quad(f, np.inf, -np.inf, args=(2,)) + assert_allclose(val_1, -val_2, atol=max(err_1, err_2)) + + def test_b_less_than_a_3(self): + def f(x): + return 1.0 + + val_1, err_1 = quad(f, 0, 1, weight='alg', wvar=(0, 0)) + val_2, err_2 = quad(f, 1, 0, weight='alg', wvar=(0, 0)) + assert_allclose(val_1, -val_2, atol=max(err_1, err_2)) + + def test_b_less_than_a_full_output(self): + def f(x): + return 1.0 + + res_1 = quad(f, 0, 1, weight='alg', wvar=(0, 0), full_output=True) + res_2 = quad(f, 1, 0, weight='alg', wvar=(0, 0), full_output=True) + err = max(res_1[1], res_2[1]) + assert_allclose(res_1[0], -res_2[0], atol=err) + + def test_double_integral(self): + # 8) Double Integral test + def simpfunc(y, x): # Note order of arguments. + return x+y + + a, b = 1.0, 2.0 + assert_quad(dblquad(simpfunc, a, b, lambda x: x, lambda x: 2*x), + 5/6.0 * (b**3.0-a**3.0)) + + def test_double_integral2(self): + def func(x0, x1, t0, t1): + return x0 + x1 + t0 + t1 + def g(x): + return x + def h(x): + return 2 * x + args = 1, 2 + assert_quad(dblquad(func, 1, 2, g, h, args=args),35./6 + 9*.5) + + def test_double_integral3(self): + def func(x0, x1): + return x0 + x1 + 1 + 2 + assert_quad(dblquad(func, 1, 2, 1, 2),6.) + + @pytest.mark.parametrize( + "x_lower, x_upper, y_lower, y_upper, expected", + [ + # Multiple integration of a function in n = 2 variables: f(x, y, z) + # over domain D = [-inf, 0] for all n. + (-np.inf, 0, -np.inf, 0, np.pi / 4), + # Multiple integration of a function in n = 2 variables: f(x, y, z) + # over domain D = [-inf, -1] for each n (one at a time). + (-np.inf, -1, -np.inf, 0, np.pi / 4 * erfc(1)), + (-np.inf, 0, -np.inf, -1, np.pi / 4 * erfc(1)), + # Multiple integration of a function in n = 2 variables: f(x, y, z) + # over domain D = [-inf, -1] for all n. + (-np.inf, -1, -np.inf, -1, np.pi / 4 * (erfc(1) ** 2)), + # Multiple integration of a function in n = 2 variables: f(x, y, z) + # over domain D = [-inf, 1] for each n (one at a time). + (-np.inf, 1, -np.inf, 0, np.pi / 4 * (erf(1) + 1)), + (-np.inf, 0, -np.inf, 1, np.pi / 4 * (erf(1) + 1)), + # Multiple integration of a function in n = 2 variables: f(x, y, z) + # over domain D = [-inf, 1] for all n. + (-np.inf, 1, -np.inf, 1, np.pi / 4 * ((erf(1) + 1) ** 2)), + # Multiple integration of a function in n = 2 variables: f(x, y, z) + # over domain Dx = [-inf, -1] and Dy = [-inf, 1]. + (-np.inf, -1, -np.inf, 1, np.pi / 4 * ((erf(1) + 1) * erfc(1))), + # Multiple integration of a function in n = 2 variables: f(x, y, z) + # over domain Dx = [-inf, 1] and Dy = [-inf, -1]. + (-np.inf, 1, -np.inf, -1, np.pi / 4 * ((erf(1) + 1) * erfc(1))), + # Multiple integration of a function in n = 2 variables: f(x, y, z) + # over domain D = [0, inf] for all n. + (0, np.inf, 0, np.inf, np.pi / 4), + # Multiple integration of a function in n = 2 variables: f(x, y, z) + # over domain D = [1, inf] for each n (one at a time). + (1, np.inf, 0, np.inf, np.pi / 4 * erfc(1)), + (0, np.inf, 1, np.inf, np.pi / 4 * erfc(1)), + # Multiple integration of a function in n = 2 variables: f(x, y, z) + # over domain D = [1, inf] for all n. + (1, np.inf, 1, np.inf, np.pi / 4 * (erfc(1) ** 2)), + # Multiple integration of a function in n = 2 variables: f(x, y, z) + # over domain D = [-1, inf] for each n (one at a time). + (-1, np.inf, 0, np.inf, np.pi / 4 * (erf(1) + 1)), + (0, np.inf, -1, np.inf, np.pi / 4 * (erf(1) + 1)), + # Multiple integration of a function in n = 2 variables: f(x, y, z) + # over domain D = [-1, inf] for all n. + (-1, np.inf, -1, np.inf, np.pi / 4 * ((erf(1) + 1) ** 2)), + # Multiple integration of a function in n = 2 variables: f(x, y, z) + # over domain Dx = [-1, inf] and Dy = [1, inf]. + (-1, np.inf, 1, np.inf, np.pi / 4 * ((erf(1) + 1) * erfc(1))), + # Multiple integration of a function in n = 2 variables: f(x, y, z) + # over domain Dx = [1, inf] and Dy = [-1, inf]. + (1, np.inf, -1, np.inf, np.pi / 4 * ((erf(1) + 1) * erfc(1))), + # Multiple integration of a function in n = 2 variables: f(x, y, z) + # over domain D = [-inf, inf] for all n. + (-np.inf, np.inf, -np.inf, np.inf, np.pi) + ] + ) + def test_double_integral_improper( + self, x_lower, x_upper, y_lower, y_upper, expected + ): + # The Gaussian Integral. + def f(x, y): + return np.exp(-x ** 2 - y ** 2) + + assert_quad( + dblquad(f, x_lower, x_upper, y_lower, y_upper), + expected, + error_tolerance=3e-8 + ) + + def test_triple_integral(self): + # 9) Triple Integral test + def simpfunc(z, y, x, t): # Note order of arguments. + return (x+y+z)*t + + a, b = 1.0, 2.0 + assert_quad(tplquad(simpfunc, a, b, + lambda x: x, lambda x: 2*x, + lambda x, y: x - y, lambda x, y: x + y, + (2.,)), + 2*8/3.0 * (b**4.0 - a**4.0)) + + @pytest.mark.xslow + @pytest.mark.parametrize( + "x_lower, x_upper, y_lower, y_upper, z_lower, z_upper, expected", + [ + # Multiple integration of a function in n = 3 variables: f(x, y, z) + # over domain D = [-inf, 0] for all n. + (-np.inf, 0, -np.inf, 0, -np.inf, 0, (np.pi ** (3 / 2)) / 8), + # Multiple integration of a function in n = 3 variables: f(x, y, z) + # over domain D = [-inf, -1] for each n (one at a time). + (-np.inf, -1, -np.inf, 0, -np.inf, 0, + (np.pi ** (3 / 2)) / 8 * erfc(1)), + (-np.inf, 0, -np.inf, -1, -np.inf, 0, + (np.pi ** (3 / 2)) / 8 * erfc(1)), + (-np.inf, 0, -np.inf, 0, -np.inf, -1, + (np.pi ** (3 / 2)) / 8 * erfc(1)), + # Multiple integration of a function in n = 3 variables: f(x, y, z) + # over domain D = [-inf, -1] for each n (two at a time). + (-np.inf, -1, -np.inf, -1, -np.inf, 0, + (np.pi ** (3 / 2)) / 8 * (erfc(1) ** 2)), + (-np.inf, -1, -np.inf, 0, -np.inf, -1, + (np.pi ** (3 / 2)) / 8 * (erfc(1) ** 2)), + (-np.inf, 0, -np.inf, -1, -np.inf, -1, + (np.pi ** (3 / 2)) / 8 * (erfc(1) ** 2)), + # Multiple integration of a function in n = 3 variables: f(x, y, z) + # over domain D = [-inf, -1] for all n. + (-np.inf, -1, -np.inf, -1, -np.inf, -1, + (np.pi ** (3 / 2)) / 8 * (erfc(1) ** 3)), + # Multiple integration of a function in n = 3 variables: f(x, y, z) + # over domain Dx = [-inf, -1] and Dy = Dz = [-inf, 1]. + (-np.inf, -1, -np.inf, 1, -np.inf, 1, + (np.pi ** (3 / 2)) / 8 * (((erf(1) + 1) ** 2) * erfc(1))), + # Multiple integration of a function in n = 3 variables: f(x, y, z) + # over domain Dx = Dy = [-inf, -1] and Dz = [-inf, 1]. + (-np.inf, -1, -np.inf, -1, -np.inf, 1, + (np.pi ** (3 / 2)) / 8 * ((erf(1) + 1) * (erfc(1) ** 2))), + # Multiple integration of a function in n = 3 variables: f(x, y, z) + # over domain Dx = Dz = [-inf, -1] and Dy = [-inf, 1]. + (-np.inf, -1, -np.inf, 1, -np.inf, -1, + (np.pi ** (3 / 2)) / 8 * ((erf(1) + 1) * (erfc(1) ** 2))), + # Multiple integration of a function in n = 3 variables: f(x, y, z) + # over domain Dx = [-inf, 1] and Dy = Dz = [-inf, -1]. + (-np.inf, 1, -np.inf, -1, -np.inf, -1, + (np.pi ** (3 / 2)) / 8 * ((erf(1) + 1) * (erfc(1) ** 2))), + # Multiple integration of a function in n = 3 variables: f(x, y, z) + # over domain Dx = Dy = [-inf, 1] and Dz = [-inf, -1]. + (-np.inf, 1, -np.inf, 1, -np.inf, -1, + (np.pi ** (3 / 2)) / 8 * (((erf(1) + 1) ** 2) * erfc(1))), + # Multiple integration of a function in n = 3 variables: f(x, y, z) + # over domain Dx = Dz = [-inf, 1] and Dy = [-inf, -1]. + (-np.inf, 1, -np.inf, -1, -np.inf, 1, + (np.pi ** (3 / 2)) / 8 * (((erf(1) + 1) ** 2) * erfc(1))), + # Multiple integration of a function in n = 3 variables: f(x, y, z) + # over domain D = [-inf, 1] for each n (one at a time). + (-np.inf, 1, -np.inf, 0, -np.inf, 0, + (np.pi ** (3 / 2)) / 8 * (erf(1) + 1)), + (-np.inf, 0, -np.inf, 1, -np.inf, 0, + (np.pi ** (3 / 2)) / 8 * (erf(1) + 1)), + (-np.inf, 0, -np.inf, 0, -np.inf, 1, + (np.pi ** (3 / 2)) / 8 * (erf(1) + 1)), + # Multiple integration of a function in n = 3 variables: f(x, y, z) + # over domain D = [-inf, 1] for each n (two at a time). + (-np.inf, 1, -np.inf, 1, -np.inf, 0, + (np.pi ** (3 / 2)) / 8 * ((erf(1) + 1) ** 2)), + (-np.inf, 1, -np.inf, 0, -np.inf, 1, + (np.pi ** (3 / 2)) / 8 * ((erf(1) + 1) ** 2)), + (-np.inf, 0, -np.inf, 1, -np.inf, 1, + (np.pi ** (3 / 2)) / 8 * ((erf(1) + 1) ** 2)), + # Multiple integration of a function in n = 3 variables: f(x, y, z) + # over domain D = [-inf, 1] for all n. + (-np.inf, 1, -np.inf, 1, -np.inf, 1, + (np.pi ** (3 / 2)) / 8 * ((erf(1) + 1) ** 3)), + # Multiple integration of a function in n = 3 variables: f(x, y, z) + # over domain D = [0, inf] for all n. + (0, np.inf, 0, np.inf, 0, np.inf, (np.pi ** (3 / 2)) / 8), + # Multiple integration of a function in n = 3 variables: f(x, y, z) + # over domain D = [1, inf] for each n (one at a time). + (1, np.inf, 0, np.inf, 0, np.inf, + (np.pi ** (3 / 2)) / 8 * erfc(1)), + (0, np.inf, 1, np.inf, 0, np.inf, + (np.pi ** (3 / 2)) / 8 * erfc(1)), + (0, np.inf, 0, np.inf, 1, np.inf, + (np.pi ** (3 / 2)) / 8 * erfc(1)), + # Multiple integration of a function in n = 3 variables: f(x, y, z) + # over domain D = [1, inf] for each n (two at a time). + (1, np.inf, 1, np.inf, 0, np.inf, + (np.pi ** (3 / 2)) / 8 * (erfc(1) ** 2)), + (1, np.inf, 0, np.inf, 1, np.inf, + (np.pi ** (3 / 2)) / 8 * (erfc(1) ** 2)), + (0, np.inf, 1, np.inf, 1, np.inf, + (np.pi ** (3 / 2)) / 8 * (erfc(1) ** 2)), + # Multiple integration of a function in n = 3 variables: f(x, y, z) + # over domain D = [1, inf] for all n. + (1, np.inf, 1, np.inf, 1, np.inf, + (np.pi ** (3 / 2)) / 8 * (erfc(1) ** 3)), + # Multiple integration of a function in n = 3 variables: f(x, y, z) + # over domain D = [-1, inf] for each n (one at a time). + (-1, np.inf, 0, np.inf, 0, np.inf, + (np.pi ** (3 / 2)) / 8 * (erf(1) + 1)), + (0, np.inf, -1, np.inf, 0, np.inf, + (np.pi ** (3 / 2)) / 8 * (erf(1) + 1)), + (0, np.inf, 0, np.inf, -1, np.inf, + (np.pi ** (3 / 2)) / 8 * (erf(1) + 1)), + # Multiple integration of a function in n = 3 variables: f(x, y, z) + # over domain D = [-1, inf] for each n (two at a time). + (-1, np.inf, -1, np.inf, 0, np.inf, + (np.pi ** (3 / 2)) / 8 * ((erf(1) + 1) ** 2)), + (-1, np.inf, 0, np.inf, -1, np.inf, + (np.pi ** (3 / 2)) / 8 * ((erf(1) + 1) ** 2)), + (0, np.inf, -1, np.inf, -1, np.inf, + (np.pi ** (3 / 2)) / 8 * ((erf(1) + 1) ** 2)), + # Multiple integration of a function in n = 3 variables: f(x, y, z) + # over domain D = [-1, inf] for all n. + (-1, np.inf, -1, np.inf, -1, np.inf, + (np.pi ** (3 / 2)) / 8 * ((erf(1) + 1) ** 3)), + # Multiple integration of a function in n = 3 variables: f(x, y, z) + # over domain Dx = [1, inf] and Dy = Dz = [-1, inf]. + (1, np.inf, -1, np.inf, -1, np.inf, + (np.pi ** (3 / 2)) / 8 * (((erf(1) + 1) ** 2) * erfc(1))), + # Multiple integration of a function in n = 3 variables: f(x, y, z) + # over domain Dx = Dy = [1, inf] and Dz = [-1, inf]. + (1, np.inf, 1, np.inf, -1, np.inf, + (np.pi ** (3 / 2)) / 8 * ((erf(1) + 1) * (erfc(1) ** 2))), + # Multiple integration of a function in n = 3 variables: f(x, y, z) + # over domain Dx = Dz = [1, inf] and Dy = [-1, inf]. + (1, np.inf, -1, np.inf, 1, np.inf, + (np.pi ** (3 / 2)) / 8 * ((erf(1) + 1) * (erfc(1) ** 2))), + # Multiple integration of a function in n = 3 variables: f(x, y, z) + # over domain Dx = [-1, inf] and Dy = Dz = [1, inf]. + (-1, np.inf, 1, np.inf, 1, np.inf, + (np.pi ** (3 / 2)) / 8 * ((erf(1) + 1) * (erfc(1) ** 2))), + # Multiple integration of a function in n = 3 variables: f(x, y, z) + # over domain Dx = Dy = [-1, inf] and Dz = [1, inf]. + (-1, np.inf, -1, np.inf, 1, np.inf, + (np.pi ** (3 / 2)) / 8 * (((erf(1) + 1) ** 2) * erfc(1))), + # Multiple integration of a function in n = 3 variables: f(x, y, z) + # over domain Dx = Dz = [-1, inf] and Dy = [1, inf]. + (-1, np.inf, 1, np.inf, -1, np.inf, + (np.pi ** (3 / 2)) / 8 * (((erf(1) + 1) ** 2) * erfc(1))), + # Multiple integration of a function in n = 3 variables: f(x, y, z) + # over domain D = [-inf, inf] for all n. + (-np.inf, np.inf, -np.inf, np.inf, -np.inf, np.inf, + np.pi ** (3 / 2)), + ], + ) + def test_triple_integral_improper( + self, + x_lower, + x_upper, + y_lower, + y_upper, + z_lower, + z_upper, + expected + ): + # The Gaussian Integral. + def f(x, y, z): + return np.exp(-x ** 2 - y ** 2 - z ** 2) + + assert_quad( + tplquad(f, x_lower, x_upper, y_lower, y_upper, z_lower, z_upper), + expected, + error_tolerance=6e-8 + ) + + def test_complex(self): + def tfunc(x): + return np.exp(1j*x) + + assert np.allclose( + quad(tfunc, 0, np.pi/2, complex_func=True)[0], + 1+1j) + + # We consider a divergent case in order to force quadpack + # to return an error message. The output is compared + # against what is returned by explicit integration + # of the parts. + kwargs = {'a': 0, 'b': np.inf, 'full_output': True, + 'weight': 'cos', 'wvar': 1} + res_c = quad(tfunc, complex_func=True, **kwargs) + res_r = quad(lambda x: np.real(np.exp(1j*x)), + complex_func=False, + **kwargs) + res_i = quad(lambda x: np.imag(np.exp(1j*x)), + complex_func=False, + **kwargs) + + np.testing.assert_equal(res_c[0], res_r[0] + 1j*res_i[0]) + np.testing.assert_equal(res_c[1], res_r[1] + 1j*res_i[1]) + + assert len(res_c[2]['real']) == len(res_r[2:]) == 3 + assert res_c[2]['real'][2] == res_r[4] + assert res_c[2]['real'][1] == res_r[3] + assert res_c[2]['real'][0]['lst'] == res_r[2]['lst'] + + assert len(res_c[2]['imag']) == len(res_i[2:]) == 1 + assert res_c[2]['imag'][0]['lst'] == res_i[2]['lst'] + + +class TestNQuad: + @pytest.mark.fail_slow(5) + def test_fixed_limits(self): + def func1(x0, x1, x2, x3): + val = (x0**2 + x1*x2 - x3**3 + np.sin(x0) + + (1 if (x0 - 0.2*x3 - 0.5 - 0.25*x1 > 0) else 0)) + return val + + def opts_basic(*args): + return {'points': [0.2*args[2] + 0.5 + 0.25*args[0]]} + + res = nquad(func1, [[0, 1], [-1, 1], [.13, .8], [-.15, 1]], + opts=[opts_basic, {}, {}, {}], full_output=True) + assert_quad(res[:-1], 1.5267454070738635) + assert_(res[-1]['neval'] > 0 and res[-1]['neval'] < 4e5) + + @pytest.mark.fail_slow(5) + def test_variable_limits(self): + scale = .1 + + def func2(x0, x1, x2, x3, t0, t1): + val = (x0*x1*x3**2 + np.sin(x2) + 1 + + (1 if x0 + t1*x1 - t0 > 0 else 0)) + return val + + def lim0(x1, x2, x3, t0, t1): + return [scale * (x1**2 + x2 + np.cos(x3)*t0*t1 + 1) - 1, + scale * (x1**2 + x2 + np.cos(x3)*t0*t1 + 1) + 1] + + def lim1(x2, x3, t0, t1): + return [scale * (t0*x2 + t1*x3) - 1, + scale * (t0*x2 + t1*x3) + 1] + + def lim2(x3, t0, t1): + return [scale * (x3 + t0**2*t1**3) - 1, + scale * (x3 + t0**2*t1**3) + 1] + + def lim3(t0, t1): + return [scale * (t0 + t1) - 1, scale * (t0 + t1) + 1] + + def opts0(x1, x2, x3, t0, t1): + return {'points': [t0 - t1*x1]} + + def opts1(x2, x3, t0, t1): + return {} + + def opts2(x3, t0, t1): + return {} + + def opts3(t0, t1): + return {} + + res = nquad(func2, [lim0, lim1, lim2, lim3], args=(0, 0), + opts=[opts0, opts1, opts2, opts3]) + assert_quad(res, 25.066666666666663) + + def test_square_separate_ranges_and_opts(self): + def f(y, x): + return 1.0 + + assert_quad(nquad(f, [[-1, 1], [-1, 1]], opts=[{}, {}]), 4.0) + + def test_square_aliased_ranges_and_opts(self): + def f(y, x): + return 1.0 + + r = [-1, 1] + opt = {} + assert_quad(nquad(f, [r, r], opts=[opt, opt]), 4.0) + + def test_square_separate_fn_ranges_and_opts(self): + def f(y, x): + return 1.0 + + def fn_range0(*args): + return (-1, 1) + + def fn_range1(*args): + return (-1, 1) + + def fn_opt0(*args): + return {} + + def fn_opt1(*args): + return {} + + ranges = [fn_range0, fn_range1] + opts = [fn_opt0, fn_opt1] + assert_quad(nquad(f, ranges, opts=opts), 4.0) + + def test_square_aliased_fn_ranges_and_opts(self): + def f(y, x): + return 1.0 + + def fn_range(*args): + return (-1, 1) + + def fn_opt(*args): + return {} + + ranges = [fn_range, fn_range] + opts = [fn_opt, fn_opt] + assert_quad(nquad(f, ranges, opts=opts), 4.0) + + def test_matching_quad(self): + def func(x): + return x**2 + 1 + + res, reserr = quad(func, 0, 4) + res2, reserr2 = nquad(func, ranges=[[0, 4]]) + assert_almost_equal(res, res2) + assert_almost_equal(reserr, reserr2) + + def test_matching_dblquad(self): + def func2d(x0, x1): + return x0**2 + x1**3 - x0 * x1 + 1 + + res, reserr = dblquad(func2d, -2, 2, lambda x: -3, lambda x: 3) + res2, reserr2 = nquad(func2d, [[-3, 3], (-2, 2)]) + assert_almost_equal(res, res2) + assert_almost_equal(reserr, reserr2) + + def test_matching_tplquad(self): + def func3d(x0, x1, x2, c0, c1): + return x0**2 + c0 * x1**3 - x0 * x1 + 1 + c1 * np.sin(x2) + + res = tplquad(func3d, -1, 2, lambda x: -2, lambda x: 2, + lambda x, y: -np.pi, lambda x, y: np.pi, + args=(2, 3)) + res2 = nquad(func3d, [[-np.pi, np.pi], [-2, 2], (-1, 2)], args=(2, 3)) + assert_almost_equal(res, res2) + + def test_dict_as_opts(self): + try: + nquad(lambda x, y: x * y, [[0, 1], [0, 1]], opts={'epsrel': 0.0001}) + except TypeError: + assert False + diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/tests/test_quadrature.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/tests/test_quadrature.py new file mode 100644 index 0000000000000000000000000000000000000000..0198b53093a79c15d2fd644956cb0d2862ca92a2 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/tests/test_quadrature.py @@ -0,0 +1,732 @@ +# mypy: disable-error-code="attr-defined" +import pytest +import numpy as np +from numpy.testing import assert_equal, assert_almost_equal, assert_allclose +from hypothesis import given +import hypothesis.strategies as st +import hypothesis.extra.numpy as hyp_num + +from scipy.integrate import (romb, newton_cotes, + cumulative_trapezoid, trapezoid, + quad, simpson, fixed_quad, + qmc_quad, cumulative_simpson) +from scipy.integrate._quadrature import _cumulative_simpson_unequal_intervals + +from scipy import stats, special, integrate +from scipy.conftest import array_api_compatible, skip_xp_invalid_arg +from scipy._lib._array_api_no_0d import xp_assert_close + +skip_xp_backends = pytest.mark.skip_xp_backends + + +class TestFixedQuad: + def test_scalar(self): + n = 4 + expected = 1/(2*n) + got, _ = fixed_quad(lambda x: x**(2*n - 1), 0, 1, n=n) + # quadrature exact for this input + assert_allclose(got, expected, rtol=1e-12) + + def test_vector(self): + n = 4 + p = np.arange(1, 2*n) + expected = 1/(p + 1) + got, _ = fixed_quad(lambda x: x**p[:, None], 0, 1, n=n) + assert_allclose(got, expected, rtol=1e-12) + + +class TestQuadrature: + def quad(self, x, a, b, args): + raise NotImplementedError + + def test_romb(self): + assert_equal(romb(np.arange(17)), 128) + + def test_romb_gh_3731(self): + # Check that romb makes maximal use of data points + x = np.arange(2**4+1) + y = np.cos(0.2*x) + val = romb(y) + val2, err = quad(lambda x: np.cos(0.2*x), x.min(), x.max()) + assert_allclose(val, val2, rtol=1e-8, atol=0) + + def test_newton_cotes(self): + """Test the first few degrees, for evenly spaced points.""" + n = 1 + wts, errcoff = newton_cotes(n, 1) + assert_equal(wts, n*np.array([0.5, 0.5])) + assert_almost_equal(errcoff, -n**3/12.0) + + n = 2 + wts, errcoff = newton_cotes(n, 1) + assert_almost_equal(wts, n*np.array([1.0, 4.0, 1.0])/6.0) + assert_almost_equal(errcoff, -n**5/2880.0) + + n = 3 + wts, errcoff = newton_cotes(n, 1) + assert_almost_equal(wts, n*np.array([1.0, 3.0, 3.0, 1.0])/8.0) + assert_almost_equal(errcoff, -n**5/6480.0) + + n = 4 + wts, errcoff = newton_cotes(n, 1) + assert_almost_equal(wts, n*np.array([7.0, 32.0, 12.0, 32.0, 7.0])/90.0) + assert_almost_equal(errcoff, -n**7/1935360.0) + + def test_newton_cotes2(self): + """Test newton_cotes with points that are not evenly spaced.""" + + x = np.array([0.0, 1.5, 2.0]) + y = x**2 + wts, errcoff = newton_cotes(x) + exact_integral = 8.0/3 + numeric_integral = np.dot(wts, y) + assert_almost_equal(numeric_integral, exact_integral) + + x = np.array([0.0, 1.4, 2.1, 3.0]) + y = x**2 + wts, errcoff = newton_cotes(x) + exact_integral = 9.0 + numeric_integral = np.dot(wts, y) + assert_almost_equal(numeric_integral, exact_integral) + + def test_simpson(self): + y = np.arange(17) + assert_equal(simpson(y), 128) + assert_equal(simpson(y, dx=0.5), 64) + assert_equal(simpson(y, x=np.linspace(0, 4, 17)), 32) + + # integral should be exactly 21 + x = np.linspace(1, 4, 4) + def f(x): + return x**2 + + assert_allclose(simpson(f(x), x=x), 21.0) + + # integral should be exactly 114 + x = np.linspace(1, 7, 4) + assert_allclose(simpson(f(x), dx=2.0), 114) + + # test multi-axis behaviour + a = np.arange(16).reshape(4, 4) + x = np.arange(64.).reshape(4, 4, 4) + y = f(x) + for i in range(3): + r = simpson(y, x=x, axis=i) + it = np.nditer(a, flags=['multi_index']) + for _ in it: + idx = list(it.multi_index) + idx.insert(i, slice(None)) + integral = x[tuple(idx)][-1]**3 / 3 - x[tuple(idx)][0]**3 / 3 + assert_allclose(r[it.multi_index], integral) + + # test when integration axis only has two points + x = np.arange(16).reshape(8, 2) + y = f(x) + r = simpson(y, x=x, axis=-1) + + integral = 0.5 * (y[:, 1] + y[:, 0]) * (x[:, 1] - x[:, 0]) + assert_allclose(r, integral) + + # odd points, test multi-axis behaviour + a = np.arange(25).reshape(5, 5) + x = np.arange(125).reshape(5, 5, 5) + y = f(x) + for i in range(3): + r = simpson(y, x=x, axis=i) + it = np.nditer(a, flags=['multi_index']) + for _ in it: + idx = list(it.multi_index) + idx.insert(i, slice(None)) + integral = x[tuple(idx)][-1]**3 / 3 - x[tuple(idx)][0]**3 / 3 + assert_allclose(r[it.multi_index], integral) + + # Tests for checking base case + x = np.array([3]) + y = np.power(x, 2) + assert_allclose(simpson(y, x=x, axis=0), 0.0) + assert_allclose(simpson(y, x=x, axis=-1), 0.0) + + x = np.array([3, 3, 3, 3]) + y = np.power(x, 2) + assert_allclose(simpson(y, x=x, axis=0), 0.0) + assert_allclose(simpson(y, x=x, axis=-1), 0.0) + + x = np.array([[1, 2, 4, 8], [1, 2, 4, 8], [1, 2, 4, 8]]) + y = np.power(x, 2) + zero_axis = [0.0, 0.0, 0.0, 0.0] + default_axis = [170 + 1/3] * 3 # 8**3 / 3 - 1/3 + assert_allclose(simpson(y, x=x, axis=0), zero_axis) + # the following should be exact + assert_allclose(simpson(y, x=x, axis=-1), default_axis) + + x = np.array([[1, 2, 4, 8], [1, 2, 4, 8], [1, 8, 16, 32]]) + y = np.power(x, 2) + zero_axis = [0.0, 136.0, 1088.0, 8704.0] + default_axis = [170 + 1/3, 170 + 1/3, 32**3 / 3 - 1/3] + assert_allclose(simpson(y, x=x, axis=0), zero_axis) + assert_allclose(simpson(y, x=x, axis=-1), default_axis) + + + @pytest.mark.parametrize('droplast', [False, True]) + def test_simpson_2d_integer_no_x(self, droplast): + # The inputs are 2d integer arrays. The results should be + # identical to the results when the inputs are floating point. + y = np.array([[2, 2, 4, 4, 8, 8, -4, 5], + [4, 4, 2, -4, 10, 22, -2, 10]]) + if droplast: + y = y[:, :-1] + result = simpson(y, axis=-1) + expected = simpson(np.array(y, dtype=np.float64), axis=-1) + assert_equal(result, expected) + + +class TestCumulative_trapezoid: + def test_1d(self): + x = np.linspace(-2, 2, num=5) + y = x + y_int = cumulative_trapezoid(y, x, initial=0) + y_expected = [0., -1.5, -2., -1.5, 0.] + assert_allclose(y_int, y_expected) + + y_int = cumulative_trapezoid(y, x, initial=None) + assert_allclose(y_int, y_expected[1:]) + + def test_y_nd_x_nd(self): + x = np.arange(3 * 2 * 4).reshape(3, 2, 4) + y = x + y_int = cumulative_trapezoid(y, x, initial=0) + y_expected = np.array([[[0., 0.5, 2., 4.5], + [0., 4.5, 10., 16.5]], + [[0., 8.5, 18., 28.5], + [0., 12.5, 26., 40.5]], + [[0., 16.5, 34., 52.5], + [0., 20.5, 42., 64.5]]]) + + assert_allclose(y_int, y_expected) + + # Try with all axes + shapes = [(2, 2, 4), (3, 1, 4), (3, 2, 3)] + for axis, shape in zip([0, 1, 2], shapes): + y_int = cumulative_trapezoid(y, x, initial=0, axis=axis) + assert_equal(y_int.shape, (3, 2, 4)) + y_int = cumulative_trapezoid(y, x, initial=None, axis=axis) + assert_equal(y_int.shape, shape) + + def test_y_nd_x_1d(self): + y = np.arange(3 * 2 * 4).reshape(3, 2, 4) + x = np.arange(4)**2 + # Try with all axes + ys_expected = ( + np.array([[[4., 5., 6., 7.], + [8., 9., 10., 11.]], + [[40., 44., 48., 52.], + [56., 60., 64., 68.]]]), + np.array([[[2., 3., 4., 5.]], + [[10., 11., 12., 13.]], + [[18., 19., 20., 21.]]]), + np.array([[[0.5, 5., 17.5], + [4.5, 21., 53.5]], + [[8.5, 37., 89.5], + [12.5, 53., 125.5]], + [[16.5, 69., 161.5], + [20.5, 85., 197.5]]])) + + for axis, y_expected in zip([0, 1, 2], ys_expected): + y_int = cumulative_trapezoid(y, x=x[:y.shape[axis]], axis=axis, + initial=None) + assert_allclose(y_int, y_expected) + + def test_x_none(self): + y = np.linspace(-2, 2, num=5) + + y_int = cumulative_trapezoid(y) + y_expected = [-1.5, -2., -1.5, 0.] + assert_allclose(y_int, y_expected) + + y_int = cumulative_trapezoid(y, initial=0) + y_expected = [0, -1.5, -2., -1.5, 0.] + assert_allclose(y_int, y_expected) + + y_int = cumulative_trapezoid(y, dx=3) + y_expected = [-4.5, -6., -4.5, 0.] + assert_allclose(y_int, y_expected) + + y_int = cumulative_trapezoid(y, dx=3, initial=0) + y_expected = [0, -4.5, -6., -4.5, 0.] + assert_allclose(y_int, y_expected) + + @pytest.mark.parametrize( + "initial", [1, 0.5] + ) + def test_initial_error(self, initial): + """If initial is not None or 0, a ValueError is raised.""" + y = np.linspace(0, 10, num=10) + with pytest.raises(ValueError, match="`initial`"): + cumulative_trapezoid(y, initial=initial) + + def test_zero_len_y(self): + with pytest.raises(ValueError, match="At least one point is required"): + cumulative_trapezoid(y=[]) + + +@array_api_compatible +class TestTrapezoid: + def test_simple(self, xp): + x = xp.arange(-10, 10, .1) + r = trapezoid(xp.exp(-.5 * x ** 2) / xp.sqrt(2 * xp.asarray(xp.pi)), dx=0.1) + # check integral of normal equals 1 + xp_assert_close(r, xp.asarray(1.0)) + + @skip_xp_backends('jax.numpy', + reasons=["JAX arrays do not support item assignment"]) + @pytest.mark.usefixtures("skip_xp_backends") + def test_ndim(self, xp): + x = xp.linspace(0, 1, 3) + y = xp.linspace(0, 2, 8) + z = xp.linspace(0, 3, 13) + + wx = xp.ones_like(x) * (x[1] - x[0]) + wx[0] /= 2 + wx[-1] /= 2 + wy = xp.ones_like(y) * (y[1] - y[0]) + wy[0] /= 2 + wy[-1] /= 2 + wz = xp.ones_like(z) * (z[1] - z[0]) + wz[0] /= 2 + wz[-1] /= 2 + + q = x[:, None, None] + y[None,:, None] + z[None, None,:] + + qx = xp.sum(q * wx[:, None, None], axis=0) + qy = xp.sum(q * wy[None, :, None], axis=1) + qz = xp.sum(q * wz[None, None, :], axis=2) + + # n-d `x` + r = trapezoid(q, x=x[:, None, None], axis=0) + xp_assert_close(r, qx) + r = trapezoid(q, x=y[None,:, None], axis=1) + xp_assert_close(r, qy) + r = trapezoid(q, x=z[None, None,:], axis=2) + xp_assert_close(r, qz) + + # 1-d `x` + r = trapezoid(q, x=x, axis=0) + xp_assert_close(r, qx) + r = trapezoid(q, x=y, axis=1) + xp_assert_close(r, qy) + r = trapezoid(q, x=z, axis=2) + xp_assert_close(r, qz) + + @skip_xp_backends('jax.numpy', + reasons=["JAX arrays do not support item assignment"]) + @pytest.mark.usefixtures("skip_xp_backends") + def test_gh21908(self, xp): + # extended testing for n-dim arrays + x = xp.reshape(xp.linspace(0, 29, 30), (3, 10)) + y = xp.reshape(xp.linspace(0, 29, 30), (3, 10)) + + out0 = xp.linspace(200, 380, 10) + xp_assert_close(trapezoid(y, x=x, axis=0), out0) + xp_assert_close(trapezoid(y, x=xp.asarray([0, 10., 20.]), axis=0), out0) + # x needs to be broadcastable against y + xp_assert_close( + trapezoid(y, x=xp.asarray([0, 10., 20.])[:, None], axis=0), + out0 + ) + with pytest.raises(Exception): + # x is not broadcastable against y + trapezoid(y, x=xp.asarray([0, 10., 20.])[None, :], axis=0) + + out1 = xp.asarray([ 40.5, 130.5, 220.5]) + xp_assert_close(trapezoid(y, x=x, axis=1), out1) + xp_assert_close( + trapezoid(y, x=xp.linspace(0, 9, 10), axis=1), + out1 + ) + + @skip_xp_invalid_arg + def test_masked(self, xp): + # Testing that masked arrays behave as if the function is 0 where + # masked + x = np.arange(5) + y = x * x + mask = x == 2 + ym = np.ma.array(y, mask=mask) + r = 13.0 # sum(0.5 * (0 + 1) * 1.0 + 0.5 * (9 + 16)) + assert_allclose(trapezoid(ym, x), r) + + xm = np.ma.array(x, mask=mask) + assert_allclose(trapezoid(ym, xm), r) + + xm = np.ma.array(x, mask=mask) + assert_allclose(trapezoid(y, xm), r) + + @skip_xp_backends(np_only=True, + reasons=['array-likes only supported for NumPy backend']) + @pytest.mark.usefixtures("skip_xp_backends") + def test_array_like(self, xp): + x = list(range(5)) + y = [t * t for t in x] + xarr = xp.asarray(x, dtype=xp.float64) + yarr = xp.asarray(y, dtype=xp.float64) + res = trapezoid(y, x) + resarr = trapezoid(yarr, xarr) + xp_assert_close(res, resarr) + + +class TestQMCQuad: + @pytest.mark.thread_unsafe + def test_input_validation(self): + message = "`func` must be callable." + with pytest.raises(TypeError, match=message): + qmc_quad("a duck", [0, 0], [1, 1]) + + message = "`func` must evaluate the integrand at points..." + with pytest.raises(ValueError, match=message): + qmc_quad(lambda: 1, [0, 0], [1, 1]) + + def func(x): + assert x.ndim == 1 + return np.sum(x) + message = "Exception encountered when attempting vectorized call..." + with pytest.warns(UserWarning, match=message): + qmc_quad(func, [0, 0], [1, 1]) + + message = "`n_points` must be an integer." + with pytest.raises(TypeError, match=message): + qmc_quad(lambda x: 1, [0, 0], [1, 1], n_points=1024.5) + + message = "`n_estimates` must be an integer." + with pytest.raises(TypeError, match=message): + qmc_quad(lambda x: 1, [0, 0], [1, 1], n_estimates=8.5) + + message = "`qrng` must be an instance of scipy.stats.qmc.QMCEngine." + with pytest.raises(TypeError, match=message): + qmc_quad(lambda x: 1, [0, 0], [1, 1], qrng="a duck") + + message = "`qrng` must be initialized with dimensionality equal to " + with pytest.raises(ValueError, match=message): + qmc_quad(lambda x: 1, [0, 0], [1, 1], qrng=stats.qmc.Sobol(1)) + + message = r"`log` must be boolean \(`True` or `False`\)." + with pytest.raises(TypeError, match=message): + qmc_quad(lambda x: 1, [0, 0], [1, 1], log=10) + + def basic_test(self, n_points=2**8, n_estimates=8, signs=None): + if signs is None: + signs = np.ones(2) + ndim = 2 + mean = np.zeros(ndim) + cov = np.eye(ndim) + + def func(x): + return stats.multivariate_normal.pdf(x.T, mean, cov) + + rng = np.random.default_rng(2879434385674690281) + qrng = stats.qmc.Sobol(ndim, seed=rng) + a = np.zeros(ndim) + b = np.ones(ndim) * signs + res = qmc_quad(func, a, b, n_points=n_points, + n_estimates=n_estimates, qrng=qrng) + ref = stats.multivariate_normal.cdf(b, mean, cov, lower_limit=a) + atol = special.stdtrit(n_estimates-1, 0.995) * res.standard_error # 99% CI + assert_allclose(res.integral, ref, atol=atol) + assert np.prod(signs)*res.integral > 0 + + rng = np.random.default_rng(2879434385674690281) + qrng = stats.qmc.Sobol(ndim, seed=rng) + logres = qmc_quad(lambda *args: np.log(func(*args)), a, b, + n_points=n_points, n_estimates=n_estimates, + log=True, qrng=qrng) + assert_allclose(np.exp(logres.integral), res.integral, rtol=1e-14) + assert np.imag(logres.integral) == (np.pi if np.prod(signs) < 0 else 0) + assert_allclose(np.exp(logres.standard_error), + res.standard_error, rtol=1e-14, atol=1e-16) + + @pytest.mark.parametrize("n_points", [2**8, 2**12]) + @pytest.mark.parametrize("n_estimates", [8, 16]) + def test_basic(self, n_points, n_estimates): + self.basic_test(n_points, n_estimates) + + @pytest.mark.parametrize("signs", [[1, 1], [-1, -1], [-1, 1], [1, -1]]) + def test_sign(self, signs): + self.basic_test(signs=signs) + + @pytest.mark.thread_unsafe + @pytest.mark.parametrize("log", [False, True]) + def test_zero(self, log): + message = "A lower limit was equal to an upper limit, so" + with pytest.warns(UserWarning, match=message): + res = qmc_quad(lambda x: 1, [0, 0], [0, 1], log=log) + assert res.integral == (-np.inf if log else 0) + assert res.standard_error == 0 + + def test_flexible_input(self): + # check that qrng is not required + # also checks that for 1d problems, a and b can be scalars + def func(x): + return stats.norm.pdf(x, scale=2) + + res = qmc_quad(func, 0, 1) + ref = stats.norm.cdf(1, scale=2) - stats.norm.cdf(0, scale=2) + assert_allclose(res.integral, ref, 1e-2) + + +def cumulative_simpson_nd_reference(y, *, x=None, dx=None, initial=None, axis=-1): + # Use cumulative_trapezoid if length of y < 3 + if y.shape[axis] < 3: + if initial is None: + return cumulative_trapezoid(y, x=x, dx=dx, axis=axis, initial=None) + else: + return initial + cumulative_trapezoid(y, x=x, dx=dx, axis=axis, initial=0) + + # Ensure that working axis is last axis + y = np.moveaxis(y, axis, -1) + x = np.moveaxis(x, axis, -1) if np.ndim(x) > 1 else x + dx = np.moveaxis(dx, axis, -1) if np.ndim(dx) > 1 else dx + initial = np.moveaxis(initial, axis, -1) if np.ndim(initial) > 1 else initial + + # If `x` is not present, create it from `dx` + n = y.shape[-1] + x = dx * np.arange(n) if dx is not None else x + # Similarly, if `initial` is not present, set it to 0 + initial_was_none = initial is None + initial = 0 if initial_was_none else initial + + # `np.apply_along_axis` accepts only one array, so concatenate arguments + x = np.broadcast_to(x, y.shape) + initial = np.broadcast_to(initial, y.shape[:-1] + (1,)) + z = np.concatenate((y, x, initial), axis=-1) + + # Use `np.apply_along_axis` to compute result + def f(z): + return cumulative_simpson(z[:n], x=z[n:2*n], initial=z[2*n:]) + res = np.apply_along_axis(f, -1, z) + + # Remove `initial` and undo axis move as needed + res = res[..., 1:] if initial_was_none else res + res = np.moveaxis(res, -1, axis) + return res + + +class TestCumulativeSimpson: + x0 = np.arange(4) + y0 = x0**2 + + @pytest.mark.parametrize('use_dx', (False, True)) + @pytest.mark.parametrize('use_initial', (False, True)) + def test_1d(self, use_dx, use_initial): + # Test for exact agreement with polynomial of highest + # possible order (3 if `dx` is constant, 2 otherwise). + rng = np.random.default_rng(82456839535679456794) + n = 10 + + # Generate random polynomials and ground truth + # integral of appropriate order + order = 3 if use_dx else 2 + dx = rng.random() + x = (np.sort(rng.random(n)) if order == 2 + else np.arange(n)*dx + rng.random()) + i = np.arange(order + 1)[:, np.newaxis] + c = rng.random(order + 1)[:, np.newaxis] + y = np.sum(c*x**i, axis=0) + Y = np.sum(c*x**(i + 1)/(i + 1), axis=0) + ref = Y if use_initial else (Y-Y[0])[1:] + + # Integrate with `cumulative_simpson` + initial = Y[0] if use_initial else None + kwarg = {'dx': dx} if use_dx else {'x': x} + res = cumulative_simpson(y, **kwarg, initial=initial) + + # Compare result against reference + if not use_dx: + assert_allclose(res, ref, rtol=2e-15) + else: + i0 = 0 if use_initial else 1 + # all terms are "close" + assert_allclose(res, ref, rtol=0.0025) + # only even-interval terms are "exact" + assert_allclose(res[i0::2], ref[i0::2], rtol=2e-15) + + @pytest.mark.parametrize('axis', np.arange(-3, 3)) + @pytest.mark.parametrize('x_ndim', (1, 3)) + @pytest.mark.parametrize('x_len', (1, 2, 7)) + @pytest.mark.parametrize('i_ndim', (None, 0, 3,)) + @pytest.mark.parametrize('dx', (None, True)) + def test_nd(self, axis, x_ndim, x_len, i_ndim, dx): + # Test behavior of `cumulative_simpson` with N-D `y` + rng = np.random.default_rng(82456839535679456794) + + # determine shapes + shape = [5, 6, x_len] + shape[axis], shape[-1] = shape[-1], shape[axis] + shape_len_1 = shape.copy() + shape_len_1[axis] = 1 + i_shape = shape_len_1 if i_ndim == 3 else () + + # initialize arguments + y = rng.random(size=shape) + x, dx = None, None + if dx: + dx = rng.random(size=shape_len_1) if x_ndim > 1 else rng.random() + else: + x = (np.sort(rng.random(size=shape), axis=axis) if x_ndim > 1 + else np.sort(rng.random(size=shape[axis]))) + initial = None if i_ndim is None else rng.random(size=i_shape) + + # compare results + res = cumulative_simpson(y, x=x, dx=dx, initial=initial, axis=axis) + ref = cumulative_simpson_nd_reference(y, x=x, dx=dx, initial=initial, axis=axis) + np.testing.assert_allclose(res, ref, rtol=1e-15) + + @pytest.mark.parametrize(('message', 'kwarg_update'), [ + ("x must be strictly increasing", dict(x=[2, 2, 3, 4])), + ("x must be strictly increasing", dict(x=[x0, [2, 2, 4, 8]], y=[y0, y0])), + ("x must be strictly increasing", dict(x=[x0, x0, x0], y=[y0, y0, y0], axis=0)), + ("At least one point is required", dict(x=[], y=[])), + ("`axis=4` is not valid for `y` with `y.ndim=1`", dict(axis=4)), + ("shape of `x` must be the same as `y` or 1-D", dict(x=np.arange(5))), + ("`initial` must either be a scalar or...", dict(initial=np.arange(5))), + ("`dx` must either be a scalar or...", dict(x=None, dx=np.arange(5))), + ]) + def test_simpson_exceptions(self, message, kwarg_update): + kwargs0 = dict(y=self.y0, x=self.x0, dx=None, initial=None, axis=-1) + with pytest.raises(ValueError, match=message): + cumulative_simpson(**dict(kwargs0, **kwarg_update)) + + def test_special_cases(self): + # Test special cases not checked elsewhere + rng = np.random.default_rng(82456839535679456794) + y = rng.random(size=10) + res = cumulative_simpson(y, dx=0) + assert_equal(res, 0) + + # Should add tests of: + # - all elements of `x` identical + # These should work as they do for `simpson` + + def _get_theoretical_diff_between_simps_and_cum_simps(self, y, x): + """`cumulative_simpson` and `simpson` can be tested against other to verify + they give consistent results. `simpson` will iteratively be called with + successively higher upper limits of integration. This function calculates + the theoretical correction required to `simpson` at even intervals to match + with `cumulative_simpson`. + """ + d = np.diff(x, axis=-1) + sub_integrals_h1 = _cumulative_simpson_unequal_intervals(y, d) + sub_integrals_h2 = _cumulative_simpson_unequal_intervals( + y[..., ::-1], d[..., ::-1] + )[..., ::-1] + + # Concatenate to build difference array + zeros_shape = (*y.shape[:-1], 1) + theoretical_difference = np.concatenate( + [ + np.zeros(zeros_shape), + (sub_integrals_h1[..., 1:] - sub_integrals_h2[..., :-1]), + np.zeros(zeros_shape), + ], + axis=-1, + ) + # Differences only expected at even intervals. Odd intervals will + # match exactly so there is no correction + theoretical_difference[..., 1::2] = 0.0 + # Note: the first interval will not match from this correction as + # `simpson` uses the trapezoidal rule + return theoretical_difference + + @pytest.mark.thread_unsafe + @pytest.mark.slow + @given( + y=hyp_num.arrays( + np.float64, + hyp_num.array_shapes(max_dims=4, min_side=3, max_side=10), + elements=st.floats(-10, 10, allow_nan=False).filter(lambda x: abs(x) > 1e-7) + ) + ) + def test_cumulative_simpson_against_simpson_with_default_dx( + self, y + ): + """Theoretically, the output of `cumulative_simpson` will be identical + to `simpson` at all even indices and in the last index. The first index + will not match as `simpson` uses the trapezoidal rule when there are only two + data points. Odd indices after the first index are shown to match with + a mathematically-derived correction.""" + def simpson_reference(y): + return np.stack( + [simpson(y[..., :i], dx=1.0) for i in range(2, y.shape[-1]+1)], axis=-1, + ) + + res = cumulative_simpson(y, dx=1.0) + ref = simpson_reference(y) + theoretical_difference = self._get_theoretical_diff_between_simps_and_cum_simps( + y, x=np.arange(y.shape[-1]) + ) + np.testing.assert_allclose( + res[..., 1:], ref[..., 1:] + theoretical_difference[..., 1:], atol=1e-16 + ) + + @pytest.mark.thread_unsafe + @pytest.mark.slow + @given( + y=hyp_num.arrays( + np.float64, + hyp_num.array_shapes(max_dims=4, min_side=3, max_side=10), + elements=st.floats(-10, 10, allow_nan=False).filter(lambda x: abs(x) > 1e-7) + ) + ) + def test_cumulative_simpson_against_simpson( + self, y + ): + """Theoretically, the output of `cumulative_simpson` will be identical + to `simpson` at all even indices and in the last index. The first index + will not match as `simpson` uses the trapezoidal rule when there are only two + data points. Odd indices after the first index are shown to match with + a mathematically-derived correction.""" + interval = 10/(y.shape[-1] - 1) + x = np.linspace(0, 10, num=y.shape[-1]) + x[1:] = x[1:] + 0.2*interval*np.random.uniform(-1, 1, len(x) - 1) + + def simpson_reference(y, x): + return np.stack( + [simpson(y[..., :i], x=x[..., :i]) for i in range(2, y.shape[-1]+1)], + axis=-1, + ) + + res = cumulative_simpson(y, x=x) + ref = simpson_reference(y, x) + theoretical_difference = self._get_theoretical_diff_between_simps_and_cum_simps( + y, x + ) + np.testing.assert_allclose( + res[..., 1:], ref[..., 1:] + theoretical_difference[..., 1:] + ) + +class TestLebedev: + def test_input_validation(self): + # only certain rules are available + message = "Order n=-1 not available..." + with pytest.raises(NotImplementedError, match=message): + integrate.lebedev_rule(-1) + + def test_quadrature(self): + # Test points/weights to integrate an example function + + def f(x): + return np.exp(x[0]) + + x, w = integrate.lebedev_rule(15) + res = w @ f(x) + ref = 14.7680137457653 # lebedev_rule reference [3] + assert_allclose(res, ref, rtol=1e-14) + assert_allclose(np.sum(w), 4 * np.pi) + + @pytest.mark.parametrize('order', list(range(3, 32, 2)) + list(range(35, 132, 6))) + def test_properties(self, order): + x, w = integrate.lebedev_rule(order) + # dispersion should be maximal; no clear spherical mean + with np.errstate(divide='ignore', invalid='ignore'): + res = stats.directional_stats(x.T, axis=0) + assert_allclose(res.mean_resultant_length, 0, atol=1e-15) + # weights should sum to 4*pi (surface area of unit sphere) + assert_allclose(np.sum(w), 4*np.pi) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/tests/test_tanhsinh.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/tests/test_tanhsinh.py new file mode 100644 index 0000000000000000000000000000000000000000..15782ba13efcb16cf8982adf94b8b2f74be63a18 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/tests/test_tanhsinh.py @@ -0,0 +1,1163 @@ +# mypy: disable-error-code="attr-defined" +import os +import pytest +import math + +import numpy as np +from numpy.testing import assert_allclose + +from scipy.conftest import array_api_compatible +import scipy._lib._elementwise_iterative_method as eim +from scipy._lib._array_api_no_0d import xp_assert_close, xp_assert_equal +from scipy._lib._array_api import array_namespace, xp_size, xp_ravel, xp_copy, is_numpy +from scipy import special, stats +from scipy.integrate import quad_vec, nsum, tanhsinh as _tanhsinh +from scipy.integrate._tanhsinh import _pair_cache +from scipy.stats._discrete_distns import _gen_harmonic_gt1 + + +def norm_pdf(x, xp=None): + xp = array_namespace(x) if xp is None else xp + return 1/(2*xp.pi)**0.5 * xp.exp(-x**2/2) + +def norm_logpdf(x, xp=None): + xp = array_namespace(x) if xp is None else xp + return -0.5*math.log(2*xp.pi) - x**2/2 + + +def _vectorize(xp): + # xp-compatible version of np.vectorize + # assumes arguments are all arrays of the same shape + def decorator(f): + def wrapped(*arg_arrays): + shape = arg_arrays[0].shape + arg_arrays = [xp_ravel(arg_array) for arg_array in arg_arrays] + res = [] + for i in range(math.prod(shape)): + arg_scalars = [arg_array[i] for arg_array in arg_arrays] + res.append(f(*arg_scalars)) + return res + + return wrapped + + return decorator + + +@array_api_compatible +@pytest.mark.usefixtures("skip_xp_backends") +@pytest.mark.skip_xp_backends( + 'array_api_strict', reason='Currently uses fancy indexing assignment.' +) +@pytest.mark.skip_xp_backends( + 'jax.numpy', reason='JAX arrays do not support item assignment.' +) +class TestTanhSinh: + + # Test problems from [1] Section 6 + def f1(self, t): + return t * np.log(1 + t) + + f1.ref = 0.25 + f1.b = 1 + + def f2(self, t): + return t ** 2 * np.arctan(t) + + f2.ref = (np.pi - 2 + 2 * np.log(2)) / 12 + f2.b = 1 + + def f3(self, t): + return np.exp(t) * np.cos(t) + + f3.ref = (np.exp(np.pi / 2) - 1) / 2 + f3.b = np.pi / 2 + + def f4(self, t): + a = np.sqrt(2 + t ** 2) + return np.arctan(a) / ((1 + t ** 2) * a) + + f4.ref = 5 * np.pi ** 2 / 96 + f4.b = 1 + + def f5(self, t): + return np.sqrt(t) * np.log(t) + + f5.ref = -4 / 9 + f5.b = 1 + + def f6(self, t): + return np.sqrt(1 - t ** 2) + + f6.ref = np.pi / 4 + f6.b = 1 + + def f7(self, t): + return np.sqrt(t) / np.sqrt(1 - t ** 2) + + f7.ref = 2 * np.sqrt(np.pi) * special.gamma(3 / 4) / special.gamma(1 / 4) + f7.b = 1 + + def f8(self, t): + return np.log(t) ** 2 + + f8.ref = 2 + f8.b = 1 + + def f9(self, t): + return np.log(np.cos(t)) + + f9.ref = -np.pi * np.log(2) / 2 + f9.b = np.pi / 2 + + def f10(self, t): + return np.sqrt(np.tan(t)) + + f10.ref = np.pi * np.sqrt(2) / 2 + f10.b = np.pi / 2 + + def f11(self, t): + return 1 / (1 + t ** 2) + + f11.ref = np.pi / 2 + f11.b = np.inf + + def f12(self, t): + return np.exp(-t) / np.sqrt(t) + + f12.ref = np.sqrt(np.pi) + f12.b = np.inf + + def f13(self, t): + return np.exp(-t ** 2 / 2) + + f13.ref = np.sqrt(np.pi / 2) + f13.b = np.inf + + def f14(self, t): + return np.exp(-t) * np.cos(t) + + f14.ref = 0.5 + f14.b = np.inf + + def f15(self, t): + return np.sin(t) / t + + f15.ref = np.pi / 2 + f15.b = np.inf + + def error(self, res, ref, log=False, xp=None): + xp = array_namespace(res, ref) if xp is None else xp + err = abs(res - ref) + + if not log: + return err + + with np.errstate(divide='ignore'): + return xp.log10(err) + + def test_input_validation(self, xp): + f = self.f1 + + zero = xp.asarray(0) + f_b = xp.asarray(f.b) + + message = '`f` must be callable.' + with pytest.raises(ValueError, match=message): + _tanhsinh(42, zero, f_b) + + message = '...must be True or False.' + with pytest.raises(ValueError, match=message): + _tanhsinh(f, zero, f_b, log=2) + + message = '...must be real numbers.' + with pytest.raises(ValueError, match=message): + _tanhsinh(f, xp.asarray(1+1j), f_b) + with pytest.raises(ValueError, match=message): + _tanhsinh(f, zero, f_b, atol='ekki') + with pytest.raises(ValueError, match=message): + _tanhsinh(f, zero, f_b, rtol=pytest) + + message = '...must be non-negative and finite.' + with pytest.raises(ValueError, match=message): + _tanhsinh(f, zero, f_b, rtol=-1) + with pytest.raises(ValueError, match=message): + _tanhsinh(f, zero, f_b, atol=xp.inf) + + message = '...may not be positive infinity.' + with pytest.raises(ValueError, match=message): + _tanhsinh(f, zero, f_b, rtol=xp.inf, log=True) + with pytest.raises(ValueError, match=message): + _tanhsinh(f, zero, f_b, atol=xp.inf, log=True) + + message = '...must be integers.' + with pytest.raises(ValueError, match=message): + _tanhsinh(f, zero, f_b, maxlevel=object()) + # with pytest.raises(ValueError, match=message): # unused for now + # _tanhsinh(f, zero, f_b, maxfun=1+1j) + with pytest.raises(ValueError, match=message): + _tanhsinh(f, zero, f_b, minlevel="migratory coconut") + + message = '...must be non-negative.' + with pytest.raises(ValueError, match=message): + _tanhsinh(f, zero, f_b, maxlevel=-1) + # with pytest.raises(ValueError, match=message): # unused for now + # _tanhsinh(f, zero, f_b, maxfun=-1) + with pytest.raises(ValueError, match=message): + _tanhsinh(f, zero, f_b, minlevel=-1) + + message = '...must be True or False.' + with pytest.raises(ValueError, match=message): + _tanhsinh(f, zero, f_b, preserve_shape=2) + + message = '...must be callable.' + with pytest.raises(ValueError, match=message): + _tanhsinh(f, zero, f_b, callback='elderberry') + + @pytest.mark.parametrize("limits, ref", [ + [(0, math.inf), 0.5], # b infinite + [(-math.inf, 0), 0.5], # a infinite + [(-math.inf, math.inf), 1.], # a and b infinite + [(math.inf, -math.inf), -1.], # flipped limits + [(1, -1), stats.norm.cdf(-1.) - stats.norm.cdf(1.)], # flipped limits + ]) + def test_integral_transforms(self, limits, ref, xp): + # Check that the integral transforms are behaving for both normal and + # log integration + limits = [xp.asarray(limit) for limit in limits] + dtype = xp.asarray(float(limits[0])).dtype + ref = xp.asarray(ref, dtype=dtype) + + res = _tanhsinh(norm_pdf, *limits) + xp_assert_close(res.integral, ref) + + logres = _tanhsinh(norm_logpdf, *limits, log=True) + xp_assert_close(xp.exp(logres.integral), ref, check_dtype=False) + # Transformation should not make the result complex unnecessarily + xp_test = array_namespace(*limits) # we need xp.isdtype + assert (xp_test.isdtype(logres.integral.dtype, "real floating") if ref > 0 + else xp_test.isdtype(logres.integral.dtype, "complex floating")) + + xp_assert_close(xp.exp(logres.error), res.error, atol=1e-16, check_dtype=False) + + # 15 skipped intentionally; it's very difficult numerically + @pytest.mark.skip_xp_backends(np_only=True, + reason='Cumbersome to convert everything.') + @pytest.mark.parametrize('f_number', range(1, 15)) + def test_basic(self, f_number, xp): + f = getattr(self, f"f{f_number}") + rtol = 2e-8 + res = _tanhsinh(f, 0, f.b, rtol=rtol) + assert_allclose(res.integral, f.ref, rtol=rtol) + if f_number not in {14}: # mildly underestimates error here + true_error = abs(self.error(res.integral, f.ref)/res.integral) + assert true_error < res.error + + if f_number in {7, 10, 12}: # succeeds, but doesn't know it + return + + assert res.success + assert res.status == 0 + + @pytest.mark.skip_xp_backends(np_only=True, + reason="Distributions aren't xp-compatible.") + @pytest.mark.parametrize('ref', (0.5, [0.4, 0.6])) + @pytest.mark.parametrize('case', stats._distr_params.distcont) + def test_accuracy(self, ref, case, xp): + distname, params = case + if distname in {'dgamma', 'dweibull', 'laplace', 'kstwo'}: + # should split up interval at first-derivative discontinuity + pytest.skip('tanh-sinh is not great for non-smooth integrands') + if (distname in {'studentized_range', 'levy_stable'} + and not int(os.getenv('SCIPY_XSLOW', 0))): + pytest.skip('This case passes, but it is too slow.') + dist = getattr(stats, distname)(*params) + x = dist.interval(ref) + res = _tanhsinh(dist.pdf, *x) + assert_allclose(res.integral, ref) + + @pytest.mark.parametrize('shape', [tuple(), (12,), (3, 4), (3, 2, 2)]) + def test_vectorization(self, shape, xp): + # Test for correct functionality, output shapes, and dtypes for various + # input shapes. + rng = np.random.default_rng(82456839535679456794) + a = xp.asarray(rng.random(shape)) + b = xp.asarray(rng.random(shape)) + p = xp.asarray(rng.random(shape)) + n = math.prod(shape) + + def f(x, p): + f.ncall += 1 + f.feval += 1 if (xp_size(x) == n or x.ndim <= 1) else x.shape[-1] + return x**p + f.ncall = 0 + f.feval = 0 + + @_vectorize(xp) + def _tanhsinh_single(a, b, p): + return _tanhsinh(lambda x: x**p, a, b) + + res = _tanhsinh(f, a, b, args=(p,)) + refs = _tanhsinh_single(a, b, p) + + xp_test = array_namespace(a) # need xp.stack, isdtype + attrs = ['integral', 'error', 'success', 'status', 'nfev', 'maxlevel'] + for attr in attrs: + ref_attr = xp_test.stack([getattr(ref, attr) for ref in refs]) + res_attr = xp_ravel(getattr(res, attr)) + xp_assert_close(res_attr, ref_attr, rtol=1e-15) + assert getattr(res, attr).shape == shape + + assert xp_test.isdtype(res.success.dtype, 'bool') + assert xp_test.isdtype(res.status.dtype, 'integral') + assert xp_test.isdtype(res.nfev.dtype, 'integral') + assert xp_test.isdtype(res.maxlevel.dtype, 'integral') + assert xp.max(res.nfev) == f.feval + # maxlevel = 2 -> 3 function calls (2 initialization, 1 work) + assert xp.max(res.maxlevel) >= 2 + assert xp.max(res.maxlevel) == f.ncall + + def test_flags(self, xp): + # Test cases that should produce different status flags; show that all + # can be produced simultaneously. + def f(xs, js): + f.nit += 1 + funcs = [lambda x: xp.exp(-x**2), # converges + lambda x: xp.exp(x), # reaches maxiter due to order=2 + lambda x: xp.full_like(x, xp.nan)] # stops due to NaN + res = [] + for i in range(xp_size(js)): + x = xs[i, ...] + j = int(xp_ravel(js)[i]) + res.append(funcs[j](x)) + return xp.stack(res) + f.nit = 0 + + args = (xp.arange(3, dtype=xp.int64),) + a = xp.asarray([xp.inf]*3) + b = xp.asarray([-xp.inf] * 3) + res = _tanhsinh(f, a, b, maxlevel=5, args=args) + ref_flags = xp.asarray([0, -2, -3], dtype=xp.int32) + xp_assert_equal(res.status, ref_flags) + + def test_flags_preserve_shape(self, xp): + # Same test as above but using `preserve_shape` option to simplify. + def f(x): + res = [xp.exp(-x[0]**2), # converges + xp.exp(x[1]), # reaches maxiter due to order=2 + xp.full_like(x[2], xp.nan)] # stops due to NaN + return xp.stack(res) + + a = xp.asarray([xp.inf] * 3) + b = xp.asarray([-xp.inf] * 3) + res = _tanhsinh(f, a, b, maxlevel=5, preserve_shape=True) + ref_flags = xp.asarray([0, -2, -3], dtype=xp.int32) + xp_assert_equal(res.status, ref_flags) + + def test_preserve_shape(self, xp): + # Test `preserve_shape` option + def f(x, xp): + return xp.stack([xp.stack([x, xp.sin(10 * x)]), + xp.stack([xp.cos(30 * x), x * xp.sin(100 * x)])]) + + ref = quad_vec(lambda x: f(x, np), 0, 1) + res = _tanhsinh(lambda x: f(x, xp), xp.asarray(0), xp.asarray(1), + preserve_shape=True) + dtype = xp.asarray(0.).dtype + xp_assert_close(res.integral, xp.asarray(ref[0], dtype=dtype)) + + def test_convergence(self, xp): + # demonstrate that number of accurate digits doubles each iteration + dtype = xp.float64 # this only works with good precision + def f(t): + return t * xp.log(1 + t) + ref = xp.asarray(0.25, dtype=dtype) + a, b = xp.asarray(0., dtype=dtype), xp.asarray(1., dtype=dtype) + + last_logerr = 0 + for i in range(4): + res = _tanhsinh(f, a, b, minlevel=0, maxlevel=i) + logerr = self.error(res.integral, ref, log=True, xp=xp) + assert (logerr < last_logerr * 2 or logerr < -15.5) + last_logerr = logerr + + def test_options_and_result_attributes(self, xp): + # demonstrate that options are behaving as advertised and status + # messages are as intended + xp_test = array_namespace(xp.asarray(1.)) # need xp.atan + + def f(x): + f.calls += 1 + f.feval += xp_size(xp.asarray(x)) + return x**2 * xp_test.atan(x) + + f.ref = xp.asarray((math.pi - 2 + 2 * math.log(2)) / 12, dtype=xp.float64) + + default_rtol = 1e-12 + default_atol = f.ref * default_rtol # effective default absolute tol + + # Keep things simpler by leaving tolerances fixed rather than + # having to make them dtype-dependent + a = xp.asarray(0., dtype=xp.float64) + b = xp.asarray(1., dtype=xp.float64) + + # Test default options + f.feval, f.calls = 0, 0 + ref = _tanhsinh(f, a, b) + assert self.error(ref.integral, f.ref) < ref.error < default_atol + assert ref.nfev == f.feval + ref.calls = f.calls # reference number of function calls + assert ref.success + assert ref.status == 0 + + # Test `maxlevel` equal to required max level + # We should get all the same results + f.feval, f.calls = 0, 0 + maxlevel = int(ref.maxlevel) + res = _tanhsinh(f, a, b, maxlevel=maxlevel) + res.calls = f.calls + assert res == ref + + # Now reduce the maximum level. We won't meet tolerances. + f.feval, f.calls = 0, 0 + maxlevel -= 1 + assert maxlevel >= 2 # can't compare errors otherwise + res = _tanhsinh(f, a, b, maxlevel=maxlevel) + assert self.error(res.integral, f.ref) < res.error > default_atol + assert res.nfev == f.feval < ref.nfev + assert f.calls == ref.calls - 1 + assert not res.success + assert res.status == eim._ECONVERR + + # `maxfun` is currently not enforced + + # # Test `maxfun` equal to required number of function evaluations + # # We should get all the same results + # f.feval, f.calls = 0, 0 + # maxfun = ref.nfev + # res = _tanhsinh(f, 0, f.b, maxfun = maxfun) + # assert res == ref + # + # # Now reduce `maxfun`. We won't meet tolerances. + # f.feval, f.calls = 0, 0 + # maxfun -= 1 + # res = _tanhsinh(f, 0, f.b, maxfun=maxfun) + # assert self.error(res.integral, f.ref) < res.error > default_atol + # assert res.nfev == f.feval < ref.nfev + # assert f.calls == ref.calls - 1 + # assert not res.success + # assert res.status == 2 + + # Take this result to be the new reference + ref = res + ref.calls = f.calls + + # Test `atol` + f.feval, f.calls = 0, 0 + # With this tolerance, we should get the exact same result as ref + atol = np.nextafter(float(ref.error), np.inf) + res = _tanhsinh(f, a, b, rtol=0, atol=atol) + assert res.integral == ref.integral + assert res.error == ref.error + assert res.nfev == f.feval == ref.nfev + assert f.calls == ref.calls + # Except the result is considered to be successful + assert res.success + assert res.status == 0 + + f.feval, f.calls = 0, 0 + # With a tighter tolerance, we should get a more accurate result + atol = np.nextafter(float(ref.error), -np.inf) + res = _tanhsinh(f, a, b, rtol=0, atol=atol) + assert self.error(res.integral, f.ref) < res.error < atol + assert res.nfev == f.feval > ref.nfev + assert f.calls > ref.calls + assert res.success + assert res.status == 0 + + # Test `rtol` + f.feval, f.calls = 0, 0 + # With this tolerance, we should get the exact same result as ref + rtol = np.nextafter(float(ref.error/ref.integral), np.inf) + res = _tanhsinh(f, a, b, rtol=rtol) + assert res.integral == ref.integral + assert res.error == ref.error + assert res.nfev == f.feval == ref.nfev + assert f.calls == ref.calls + # Except the result is considered to be successful + assert res.success + assert res.status == 0 + + f.feval, f.calls = 0, 0 + # With a tighter tolerance, we should get a more accurate result + rtol = np.nextafter(float(ref.error/ref.integral), -np.inf) + res = _tanhsinh(f, a, b, rtol=rtol) + assert self.error(res.integral, f.ref)/f.ref < res.error/res.integral < rtol + assert res.nfev == f.feval > ref.nfev + assert f.calls > ref.calls + assert res.success + assert res.status == 0 + + @pytest.mark.skip_xp_backends('torch', reason= + 'https://github.com/scipy/scipy/pull/21149#issuecomment-2330477359', + ) + @pytest.mark.parametrize('rtol', [1e-4, 1e-14]) + def test_log(self, rtol, xp): + # Test equivalence of log-integration and regular integration + test_tols = dict(atol=1e-18, rtol=1e-15) + + # Positive integrand (real log-integrand) + a = xp.asarray(-1., dtype=xp.float64) + b = xp.asarray(2., dtype=xp.float64) + res = _tanhsinh(norm_logpdf, a, b, log=True, rtol=math.log(rtol)) + ref = _tanhsinh(norm_pdf, a, b, rtol=rtol) + xp_assert_close(xp.exp(res.integral), ref.integral, **test_tols) + xp_assert_close(xp.exp(res.error), ref.error, **test_tols) + assert res.nfev == ref.nfev + + # Real integrand (complex log-integrand) + def f(x): + return -norm_logpdf(x)*norm_pdf(x) + + def logf(x): + return xp.log(norm_logpdf(x) + 0j) + norm_logpdf(x) + xp.pi * 1j + + a = xp.asarray(-xp.inf, dtype=xp.float64) + b = xp.asarray(xp.inf, dtype=xp.float64) + res = _tanhsinh(logf, a, b, log=True) + ref = _tanhsinh(f, a, b) + # In gh-19173, we saw `invalid` warnings on one CI platform. + # Silencing `all` because I can't reproduce locally and don't want + # to risk the need to run CI again. + with np.errstate(all='ignore'): + xp_assert_close(xp.exp(res.integral), ref.integral, **test_tols, + check_dtype=False) + xp_assert_close(xp.exp(res.error), ref.error, **test_tols, + check_dtype=False) + assert res.nfev == ref.nfev + + def test_complex(self, xp): + # Test integration of complex integrand + # Finite limits + def f(x): + return xp.exp(1j * x) + + a, b = xp.asarray(0.), xp.asarray(xp.pi/4) + res = _tanhsinh(f, a, b) + ref = math.sqrt(2)/2 + (1-math.sqrt(2)/2)*1j + xp_assert_close(res.integral, xp.asarray(ref)) + + # Infinite limits + def f(x): + return norm_pdf(x) + 1j/2*norm_pdf(x/2) + + a, b = xp.asarray(xp.inf), xp.asarray(-xp.inf) + res = _tanhsinh(f, a, b) + xp_assert_close(res.integral, xp.asarray(-(1+1j))) + + @pytest.mark.parametrize("maxlevel", range(4)) + def test_minlevel(self, maxlevel, xp): + # Verify that minlevel does not change the values at which the + # integrand is evaluated or the integral/error estimates, only the + # number of function calls + + # need `xp.concat`, `xp.atan`, and `xp.sort` + xp_test = array_namespace(xp.asarray(1.)) + + def f(x): + f.calls += 1 + f.feval += xp_size(xp.asarray(x)) + f.x = xp_test.concat((f.x, xp_ravel(x))) + return x**2 * xp_test.atan(x) + + f.feval, f.calls, f.x = 0, 0, xp.asarray([]) + + a = xp.asarray(0, dtype=xp.float64) + b = xp.asarray(1, dtype=xp.float64) + ref = _tanhsinh(f, a, b, minlevel=0, maxlevel=maxlevel) + ref_x = xp_test.sort(f.x) + + for minlevel in range(0, maxlevel + 1): + f.feval, f.calls, f.x = 0, 0, xp.asarray([]) + options = dict(minlevel=minlevel, maxlevel=maxlevel) + res = _tanhsinh(f, a, b, **options) + # Should be very close; all that has changed is the order of values + xp_assert_close(res.integral, ref.integral, rtol=4e-16) + # Difference in absolute errors << magnitude of integral + xp_assert_close(res.error, ref.error, atol=4e-16 * ref.integral) + assert res.nfev == f.feval == f.x.shape[0] + assert f.calls == maxlevel - minlevel + 1 + 1 # 1 validation call + assert res.status == ref.status + xp_assert_equal(ref_x, xp_test.sort(f.x)) + + def test_improper_integrals(self, xp): + # Test handling of infinite limits of integration (mixed with finite limits) + def f(x): + x[xp.isinf(x)] = xp.nan + return xp.exp(-x**2) + a = xp.asarray([-xp.inf, 0, -xp.inf, xp.inf, -20, -xp.inf, -20]) + b = xp.asarray([xp.inf, xp.inf, 0, -xp.inf, 20, 20, xp.inf]) + ref = math.sqrt(math.pi) + ref = xp.asarray([ref, ref/2, ref/2, -ref, ref, ref, ref]) + res = _tanhsinh(f, a, b) + xp_assert_close(res.integral, ref) + + @pytest.mark.parametrize("limits", ((0, 3), ([-math.inf, 0], [3, 3]))) + @pytest.mark.parametrize("dtype", ('float32', 'float64')) + def test_dtype(self, limits, dtype, xp): + # Test that dtypes are preserved + dtype = getattr(xp, dtype) + a, b = xp.asarray(limits, dtype=dtype) + + def f(x): + assert x.dtype == dtype + return xp.exp(x) + + rtol = 1e-12 if dtype == xp.float64 else 1e-5 + res = _tanhsinh(f, a, b, rtol=rtol) + assert res.integral.dtype == dtype + assert res.error.dtype == dtype + assert xp.all(res.success) + xp_assert_close(res.integral, xp.exp(b)-xp.exp(a)) + + def test_maxiter_callback(self, xp): + # Test behavior of `maxiter` parameter and `callback` interface + a, b = xp.asarray(-xp.inf), xp.asarray(xp.inf) + def f(x): + return xp.exp(-x*x) + + minlevel, maxlevel = 0, 2 + maxiter = maxlevel - minlevel + 1 + kwargs = dict(minlevel=minlevel, maxlevel=maxlevel, rtol=1e-15) + res = _tanhsinh(f, a, b, **kwargs) + assert not res.success + assert res.maxlevel == maxlevel + + def callback(res): + callback.iter += 1 + callback.res = res + assert hasattr(res, 'integral') + assert res.status == 1 + if callback.iter == maxiter: + raise StopIteration + callback.iter = -1 # callback called once before first iteration + callback.res = None + + del kwargs['maxlevel'] + res2 = _tanhsinh(f, a, b, **kwargs, callback=callback) + # terminating with callback is identical to terminating due to maxiter + # (except for `status`) + for key in res.keys(): + if key == 'status': + assert res[key] == -2 + assert res2[key] == -4 + else: + assert res2[key] == callback.res[key] == res[key] + + def test_jumpstart(self, xp): + # The intermediate results at each level i should be the same as the + # final results when jumpstarting at level i; i.e. minlevel=maxlevel=i + a = xp.asarray(-xp.inf, dtype=xp.float64) + b = xp.asarray(xp.inf, dtype=xp.float64) + + def f(x): + return xp.exp(-x*x) + + def callback(res): + callback.integrals.append(xp_copy(res.integral)[()]) + callback.errors.append(xp_copy(res.error)[()]) + callback.integrals = [] + callback.errors = [] + + maxlevel = 4 + _tanhsinh(f, a, b, minlevel=0, maxlevel=maxlevel, callback=callback) + + for i in range(maxlevel + 1): + res = _tanhsinh(f, a, b, minlevel=i, maxlevel=i) + xp_assert_close(callback.integrals[1+i], res.integral, rtol=1e-15) + xp_assert_close(callback.errors[1+i], res.error, rtol=1e-15, atol=1e-16) + + def test_special_cases(self, xp): + # Test edge cases and other special cases + a, b = xp.asarray(0), xp.asarray(1) + xp_test = array_namespace(a, b) # need `xp.isdtype` + + def f(x): + assert xp_test.isdtype(x.dtype, "real floating") + return x + + res = _tanhsinh(f, a, b) + assert res.success + xp_assert_close(res.integral, xp.asarray(0.5)) + + # Test levels 0 and 1; error is NaN + res = _tanhsinh(f, a, b, maxlevel=0) + assert res.integral > 0 + xp_assert_equal(res.error, xp.asarray(xp.nan)) + res = _tanhsinh(f, a, b, maxlevel=1) + assert res.integral > 0 + xp_assert_equal(res.error, xp.asarray(xp.nan)) + + # Test equal left and right integration limits + res = _tanhsinh(f, b, b) + assert res.success + assert res.maxlevel == -1 + xp_assert_close(res.integral, xp.asarray(0.)) + + # Test scalar `args` (not in tuple) + def f(x, c): + return x**c + + res = _tanhsinh(f, a, b, args=29) + xp_assert_close(res.integral, xp.asarray(1/30)) + + # Test NaNs + a = xp.asarray([xp.nan, 0, 0, 0]) + b = xp.asarray([1, xp.nan, 1, 1]) + c = xp.asarray([1, 1, xp.nan, 1]) + res = _tanhsinh(f, a, b, args=(c,)) + xp_assert_close(res.integral, xp.asarray([xp.nan, xp.nan, xp.nan, 0.5])) + xp_assert_equal(res.error[:3], xp.full((3,), xp.nan)) + xp_assert_equal(res.status, xp.asarray([-3, -3, -3, 0], dtype=xp.int32)) + xp_assert_equal(res.success, xp.asarray([False, False, False, True])) + xp_assert_equal(res.nfev[:3], xp.full((3,), 1, dtype=xp.int32)) + + # Test complex integral followed by real integral + # Previously, h0 was of the result dtype. If the `dtype` were complex, + # this could lead to complex cached abscissae/weights. If these get + # cast to real dtype for a subsequent real integral, we would get a + # ComplexWarning. Check that this is avoided. + _pair_cache.xjc = xp.empty(0) + _pair_cache.wj = xp.empty(0) + _pair_cache.indices = [0] + _pair_cache.h0 = None + a, b = xp.asarray(0), xp.asarray(1) + res = _tanhsinh(lambda x: xp.asarray(x*1j), a, b) + xp_assert_close(res.integral, xp.asarray(0.5*1j)) + res = _tanhsinh(lambda x: x, a, b) + xp_assert_close(res.integral, xp.asarray(0.5)) + + # Test zero-size + shape = (0, 3) + res = _tanhsinh(lambda x: x, xp.asarray(0), xp.zeros(shape)) + attrs = ['integral', 'error', 'success', 'status', 'nfev', 'maxlevel'] + for attr in attrs: + assert res[attr].shape == shape + + @pytest.mark.skip_xp_backends(np_only=True) + def test_compress_nodes_weights_gh21496(self, xp): + # See discussion in: + # https://github.com/scipy/scipy/pull/21496#discussion_r1878681049 + # This would cause "ValueError: attempt to get argmax of an empty sequence" + # Check that this has been resolved. + x = np.full(65, 3) + x[-1] = 1000 + _tanhsinh(np.sin, 1, x) + + def test_gh_22681_finite_error(self, xp): + # gh-22681 noted a case in which the error was NaN on some platforms; + # check that this does in fact fail in CI. + a = complex(12, -10) + b = complex(12, 39) + def f(t): + return xp.sin(a * (1 - t) + b * t) + res = _tanhsinh(f, xp.asarray(0.), xp.asarray(1.), atol=0, rtol=0, maxlevel=10) + assert xp.isfinite(res.error) + + +@array_api_compatible +@pytest.mark.usefixtures("skip_xp_backends") +@pytest.mark.skip_xp_backends('array_api_strict', reason='No fancy indexing.') +@pytest.mark.skip_xp_backends('jax.numpy', reason='No mutation.') +class TestNSum: + rng = np.random.default_rng(5895448232066142650) + p = rng.uniform(1, 10, size=10).tolist() + + def f1(self, k): + # Integers are never passed to `f1`; if they were, we'd get + # integer to negative integer power error + return k**(-2) + + f1.ref = np.pi**2/6 + f1.a = 1 + f1.b = np.inf + f1.args = tuple() + + def f2(self, k, p): + return 1 / k**p + + f2.ref = special.zeta(p, 1) + f2.a = 1. + f2.b = np.inf + f2.args = (p,) + + def f3(self, k, p): + return 1 / k**p + + f3.a = 1 + f3.b = rng.integers(5, 15, size=(3, 1)) + f3.ref = _gen_harmonic_gt1(f3.b, p) + f3.args = (p,) + + def test_input_validation(self, xp): + f = self.f1 + a, b = xp.asarray(f.a), xp.asarray(f.b) + + message = '`f` must be callable.' + with pytest.raises(ValueError, match=message): + nsum(42, a, b) + + message = '...must be True or False.' + with pytest.raises(ValueError, match=message): + nsum(f, a, b, log=2) + + message = '...must be real numbers.' + with pytest.raises(ValueError, match=message): + nsum(f, xp.asarray(1+1j), b) + with pytest.raises(ValueError, match=message): + nsum(f, a, xp.asarray(1+1j)) + with pytest.raises(ValueError, match=message): + nsum(f, a, b, step=xp.asarray(1+1j)) + with pytest.raises(ValueError, match=message): + nsum(f, a, b, tolerances=dict(atol='ekki')) + with pytest.raises(ValueError, match=message): + nsum(f, a, b, tolerances=dict(rtol=pytest)) + + with np.errstate(all='ignore'): + res = nsum(f, xp.asarray([np.nan, np.inf]), xp.asarray(1.)) + assert xp.all((res.status == -1) & xp.isnan(res.sum) + & xp.isnan(res.error) & ~res.success & res.nfev == 1) + res = nsum(f, xp.asarray(10.), xp.asarray([np.nan, 1])) + assert xp.all((res.status == -1) & xp.isnan(res.sum) + & xp.isnan(res.error) & ~res.success & res.nfev == 1) + res = nsum(f, xp.asarray(1.), xp.asarray(10.), + step=xp.asarray([xp.nan, -xp.inf, xp.inf, -1, 0])) + assert xp.all((res.status == -1) & xp.isnan(res.sum) + & xp.isnan(res.error) & ~res.success & res.nfev == 1) + + message = '...must be non-negative and finite.' + with pytest.raises(ValueError, match=message): + nsum(f, a, b, tolerances=dict(rtol=-1)) + with pytest.raises(ValueError, match=message): + nsum(f, a, b, tolerances=dict(atol=np.inf)) + + message = '...may not be positive infinity.' + with pytest.raises(ValueError, match=message): + nsum(f, a, b, tolerances=dict(rtol=np.inf), log=True) + with pytest.raises(ValueError, match=message): + nsum(f, a, b, tolerances=dict(atol=np.inf), log=True) + + message = '...must be a non-negative integer.' + with pytest.raises(ValueError, match=message): + nsum(f, a, b, maxterms=3.5) + with pytest.raises(ValueError, match=message): + nsum(f, a, b, maxterms=-2) + + @pytest.mark.parametrize('f_number', range(1, 4)) + def test_basic(self, f_number, xp): + dtype = xp.asarray(1.).dtype + f = getattr(self, f"f{f_number}") + a, b = xp.asarray(f.a), xp.asarray(f.b), + args = tuple(xp.asarray(arg) for arg in f.args) + ref = xp.asarray(f.ref, dtype=dtype) + res = nsum(f, a, b, args=args) + xp_assert_close(res.sum, ref) + xp_assert_equal(res.status, xp.zeros(ref.shape, dtype=xp.int32)) + xp_test = array_namespace(a) # CuPy doesn't have `bool` + xp_assert_equal(res.success, xp.ones(ref.shape, dtype=xp_test.bool)) + + with np.errstate(divide='ignore'): + logres = nsum(lambda *args: xp.log(f(*args)), + a, b, log=True, args=args) + xp_assert_close(xp.exp(logres.sum), res.sum) + xp_assert_close(xp.exp(logres.error), res.error, atol=1e-15) + xp_assert_equal(logres.status, res.status) + xp_assert_equal(logres.success, res.success) + + @pytest.mark.parametrize('maxterms', [0, 1, 10, 20, 100]) + def test_integral(self, maxterms, xp): + # test precise behavior of integral approximation + f = self.f1 + + def logf(x): + return -2*xp.log(x) + + def F(x): + return -1 / x + + a = xp.asarray([1, 5], dtype=xp.float64)[:, xp.newaxis] + b = xp.asarray([20, 100, xp.inf], dtype=xp.float64)[:, xp.newaxis, xp.newaxis] + step = xp.asarray([0.5, 1, 2], dtype=xp.float64).reshape((-1, 1, 1, 1)) + nsteps = xp.floor((b - a)/step) + b_original = b + b = a + nsteps*step + + k = a + maxterms*step + # partial sum + direct = xp.sum(f(a + xp.arange(maxterms)*step), axis=-1, keepdims=True) + integral = (F(b) - F(k))/step # integral approximation of remainder + low = direct + integral + f(b) # theoretical lower bound + high = direct + integral + f(k) # theoretical upper bound + ref_sum = (low + high)/2 # nsum uses average of the two + ref_err = (high - low)/2 # error (assuming perfect quadrature) + + # correct reference values where number of terms < maxterms + xp_test = array_namespace(a) # torch needs broadcast_arrays + a, b, step = xp_test.broadcast_arrays(a, b, step) + for i in np.ndindex(a.shape): + ai, bi, stepi = float(a[i]), float(b[i]), float(step[i]) + if (bi - ai)/stepi + 1 <= maxterms: + direct = xp.sum(f(xp.arange(ai, bi+stepi, stepi, dtype=xp.float64))) + ref_sum[i] = direct + ref_err[i] = direct * xp.finfo(direct.dtype).eps + + rtol = 1e-12 + res = nsum(f, a, b_original, step=step, maxterms=maxterms, + tolerances=dict(rtol=rtol)) + xp_assert_close(res.sum, ref_sum, rtol=10*rtol) + xp_assert_close(res.error, ref_err, rtol=100*rtol) + + i = ((b_original - a)/step + 1 <= maxterms) + xp_assert_close(res.sum[i], ref_sum[i], rtol=1e-15) + xp_assert_close(res.error[i], ref_err[i], rtol=1e-15) + + logres = nsum(logf, a, b_original, step=step, log=True, + tolerances=dict(rtol=math.log(rtol)), maxterms=maxterms) + xp_assert_close(xp.exp(logres.sum), res.sum) + xp_assert_close(xp.exp(logres.error), res.error) + + @pytest.mark.parametrize('shape', [tuple(), (12,), (3, 4), (3, 2, 2)]) + def test_vectorization(self, shape, xp): + # Test for correct functionality, output shapes, and dtypes for various + # input shapes. + rng = np.random.default_rng(82456839535679456794) + a = rng.integers(1, 10, size=shape) + # when the sum can be computed directly or `maxterms` is large enough + # to meet `atol`, there are slight differences (for good reason) + # between vectorized call and looping. + b = np.inf + p = rng.random(shape) + 1 + n = math.prod(shape) + + def f(x, p): + f.feval += 1 if (x.size == n or x.ndim <= 1) else x.shape[-1] + return 1 / x ** p + + f.feval = 0 + + @np.vectorize + def nsum_single(a, b, p, maxterms): + return nsum(lambda x: 1 / x**p, a, b, maxterms=maxterms) + + res = nsum(f, xp.asarray(a), xp.asarray(b), maxterms=1000, + args=(xp.asarray(p),)) + refs = nsum_single(a, b, p, maxterms=1000).ravel() + + attrs = ['sum', 'error', 'success', 'status', 'nfev'] + for attr in attrs: + ref_attr = [xp.asarray(getattr(ref, attr)) for ref in refs] + res_attr = getattr(res, attr) + xp_assert_close(xp_ravel(res_attr), xp.asarray(ref_attr), rtol=1e-15) + assert res_attr.shape == shape + + xp_test = array_namespace(xp.asarray(1.)) + assert xp_test.isdtype(res.success.dtype, 'bool') + assert xp_test.isdtype(res.status.dtype, 'integral') + assert xp_test.isdtype(res.nfev.dtype, 'integral') + if is_numpy(xp): # other libraries might have different number + assert int(xp.max(res.nfev)) == f.feval + + def test_status(self, xp): + f = self.f2 + + p = [2, 2, 0.9, 1.1, 2, 2] + a = xp.asarray([0, 0, 1, 1, 1, np.nan], dtype=xp.float64) + b = xp.asarray([10, np.inf, np.inf, np.inf, np.inf, np.inf], dtype=xp.float64) + ref = special.zeta(p, 1) + p = xp.asarray(p, dtype=xp.float64) + + with np.errstate(divide='ignore'): # intentionally dividing by zero + res = nsum(f, a, b, args=(p,)) + + ref_success = xp.asarray([False, False, False, False, True, False]) + ref_status = xp.asarray([-3, -3, -2, -4, 0, -1], dtype=xp.int32) + xp_assert_equal(res.success, ref_success) + xp_assert_equal(res.status, ref_status) + xp_assert_close(res.sum[res.success], xp.asarray(ref)[res.success]) + + def test_nfev(self, xp): + def f(x): + f.nfev += xp_size(x) + return 1 / x**2 + + f.nfev = 0 + res = nsum(f, xp.asarray(1), xp.asarray(10)) + assert res.nfev == f.nfev + + f.nfev = 0 + res = nsum(f, xp.asarray(1), xp.asarray(xp.inf), tolerances=dict(atol=1e-6)) + assert res.nfev == f.nfev + + def test_inclusive(self, xp): + # There was an edge case off-by one bug when `_direct` was called with + # `inclusive=True`. Check that this is resolved. + a = xp.asarray([1, 4]) + b = xp.asarray(xp.inf) + res = nsum(lambda k: 1 / k ** 2, a, b, + maxterms=500, tolerances=dict(atol=0.1)) + ref = nsum(lambda k: 1 / k ** 2, a, b) + assert xp.all(res.sum > (ref.sum - res.error)) + assert xp.all(res.sum < (ref.sum + res.error)) + + @pytest.mark.parametrize('log', [True, False]) + def test_infinite_bounds(self, log, xp): + a = xp.asarray([1, -np.inf, -np.inf]) + b = xp.asarray([np.inf, -1, np.inf]) + c = xp.asarray([1, 2, 3]) + + def f(x, a): + return (xp.log(xp.tanh(a / 2)) - a*xp.abs(x) if log + else xp.tanh(a/2) * xp.exp(-a*xp.abs(x))) + + res = nsum(f, a, b, args=(c,), log=log) + ref = xp.asarray([stats.dlaplace.sf(0, 1), stats.dlaplace.sf(0, 2), 1]) + ref = xp.log(ref) if log else ref + atol = (1e-10 if a.dtype==xp.float64 else 1e-5) if log else 0 + xp_assert_close(res.sum, xp.asarray(ref, dtype=a.dtype), atol=atol) + + # # Make sure the sign of `x` passed into `f` is correct. + def f(x, c): + return -3*xp.log(c*x) if log else 1 / (c*x)**3 + + a = xp.asarray([1, -np.inf]) + b = xp.asarray([np.inf, -1]) + arg = xp.asarray([1, -1]) + res = nsum(f, a, b, args=(arg,), log=log) + ref = np.log(special.zeta(3)) if log else special.zeta(3) + xp_assert_close(res.sum, xp.full(a.shape, ref, dtype=a.dtype)) + + def test_decreasing_check(self, xp): + # Test accuracy when we start sum on an uphill slope. + # Without the decreasing check, the terms would look small enough to + # use the integral approximation. Because the function is not decreasing, + # the error is not bounded by the magnitude of the last term of the + # partial sum. In this case, the error would be ~1e-4, causing the test + # to fail. + def f(x): + return xp.exp(-x ** 2) + + a, b = xp.asarray(-25, dtype=xp.float64), xp.asarray(np.inf, dtype=xp.float64) + res = nsum(f, a, b) + + # Reference computed with mpmath: + # from mpmath import mp + # mp.dps = 50 + # def fmp(x): return mp.exp(-x**2) + # ref = mp.nsum(fmp, (-25, 0)) + mp.nsum(fmp, (1, mp.inf)) + ref = xp.asarray(1.772637204826652, dtype=xp.float64) + + xp_assert_close(res.sum, ref, rtol=1e-15) + + def test_special_case(self, xp): + # test equal lower/upper limit + f = self.f1 + a = b = xp.asarray(2) + res = nsum(f, a, b) + xp_assert_equal(res.sum, xp.asarray(f(2))) + + # Test scalar `args` (not in tuple) + res = nsum(self.f2, xp.asarray(1), xp.asarray(np.inf), args=xp.asarray(2)) + xp_assert_close(res.sum, xp.asarray(self.f1.ref)) # f1.ref is correct w/ args=2 + + # Test 0 size input + a = xp.empty((3, 1, 1)) # arbitrary broadcastable shapes + b = xp.empty((0, 1)) # could use Hypothesis + p = xp.empty(4) # but it's overkill + shape = np.broadcast_shapes(a.shape, b.shape, p.shape) + res = nsum(self.f2, a, b, args=(p,)) + assert res.sum.shape == shape + assert res.status.shape == shape + assert res.nfev.shape == shape + + # Test maxterms=0 + def f(x): + with np.errstate(divide='ignore'): + return 1 / x + + res = nsum(f, xp.asarray(0), xp.asarray(10), maxterms=0) + assert xp.isnan(res.sum) + assert xp.isnan(res.error) + assert res.status == -2 + + res = nsum(f, xp.asarray(0), xp.asarray(10), maxterms=1) + assert xp.isnan(res.sum) + assert xp.isnan(res.error) + assert res.status == -3 + + # Test NaNs + # should skip both direct and integral methods if there are NaNs + a = xp.asarray([xp.nan, 1, 1, 1]) + b = xp.asarray([xp.inf, xp.nan, xp.inf, xp.inf]) + p = xp.asarray([2, 2, xp.nan, 2]) + res = nsum(self.f2, a, b, args=(p,)) + xp_assert_close(res.sum, xp.asarray([xp.nan, xp.nan, xp.nan, self.f1.ref])) + xp_assert_close(res.error[:3], xp.full((3,), xp.nan)) + xp_assert_equal(res.status, xp.asarray([-1, -1, -3, 0], dtype=xp.int32)) + xp_assert_equal(res.success, xp.asarray([False, False, False, True])) + # Ideally res.nfev[2] would be 1, but `tanhsinh` has some function evals + xp_assert_equal(res.nfev[:2], xp.full((2,), 1, dtype=xp.int32)) + + @pytest.mark.parametrize('dtype', ['float32', 'float64']) + def test_dtype(self, dtype, xp): + dtype = getattr(xp, dtype) + + def f(k): + assert k.dtype == dtype + return 1 / k ** xp.asarray(2, dtype=dtype) + + a = xp.asarray(1, dtype=dtype) + b = xp.asarray([10, xp.inf], dtype=dtype) + res = nsum(f, a, b) + assert res.sum.dtype == dtype + assert res.error.dtype == dtype + + rtol = 1e-12 if dtype == xp.float64 else 1e-6 + ref = _gen_harmonic_gt1(np.asarray([10, xp.inf]), 2) + xp_assert_close(res.sum, xp.asarray(ref, dtype=dtype), rtol=rtol) + + @pytest.mark.parametrize('case', [(10, 100), (100, 10)]) + def test_nondivisible_interval(self, case, xp): + # When the limits of the sum are such that (b - a)/step + # is not exactly integral, check that only floor((b - a)/step) + # terms are included. + n, maxterms = case + + def f(k): + return 1 / k ** 2 + + a = np.e + step = 1 / 3 + b0 = a + n * step + i = np.arange(-2, 3) + b = b0 + i * np.spacing(b0) + ns = np.floor((b - a) / step) + assert len(set(ns)) == 2 + + a, b = xp.asarray(a, dtype=xp.float64), xp.asarray(b, dtype=xp.float64) + step, ns = xp.asarray(step, dtype=xp.float64), xp.asarray(ns, dtype=xp.float64) + res = nsum(f, a, b, step=step, maxterms=maxterms) + xp_assert_equal(xp.diff(ns) > 0, xp.diff(res.sum) > 0) + xp_assert_close(res.sum[-1], res.sum[0] + f(b0)) + + @pytest.mark.skip_xp_backends(np_only=True, reason='Needs beta function.') + def test_logser_kurtosis_gh20648(self, xp): + # Some functions return NaN at infinity rather than 0 like they should. + # Check that this is accounted for. + ref = stats.yulesimon.moment(4, 5) + def f(x): + return stats.yulesimon._pmf(x, 5) * x**4 + + with np.errstate(invalid='ignore'): + assert np.isnan(f(np.inf)) + + res = nsum(f, 1, np.inf) + assert_allclose(res.sum, ref) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/vode.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/vode.py new file mode 100644 index 0000000000000000000000000000000000000000..f92927901084ce33cdeb006057d85dd501b13aae --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/integrate/vode.py @@ -0,0 +1,15 @@ +# This file is not meant for public use and will be removed in SciPy v2.0.0. + +from scipy._lib.deprecation import _sub_module_deprecation + +__all__: list[str] = [] + + +def __dir__(): + return __all__ + + +def __getattr__(name): + return _sub_module_deprecation(sub_package="integrate", module="vode", + private_modules=["_vode"], all=__all__, + attribute=name) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..1c4f97134d20b8d3acb1bea54c8384c510314aaa --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/__init__.py @@ -0,0 +1,216 @@ +""" +======================================== +Interpolation (:mod:`scipy.interpolate`) +======================================== + +.. currentmodule:: scipy.interpolate + +Sub-package for objects used in interpolation. + +As listed below, this sub-package contains spline functions and classes, +1-D and multidimensional (univariate and multivariate) +interpolation classes, Lagrange and Taylor polynomial interpolators, and +wrappers for `FITPACK `__ +and DFITPACK functions. + +Univariate interpolation +======================== + +.. autosummary:: + :toctree: generated/ + + interp1d + BarycentricInterpolator + KroghInterpolator + barycentric_interpolate + krogh_interpolate + pchip_interpolate + CubicHermiteSpline + PchipInterpolator + Akima1DInterpolator + CubicSpline + PPoly + BPoly + FloaterHormannInterpolator + + +Multivariate interpolation +========================== + +Unstructured data: + +.. autosummary:: + :toctree: generated/ + + griddata + LinearNDInterpolator + NearestNDInterpolator + CloughTocher2DInterpolator + RBFInterpolator + Rbf + interp2d + +For data on a grid: + +.. autosummary:: + :toctree: generated/ + + interpn + RegularGridInterpolator + RectBivariateSpline + +.. seealso:: + + `scipy.ndimage.map_coordinates` + +Tensor product polynomials: + +.. autosummary:: + :toctree: generated/ + + NdPPoly + NdBSpline + +1-D Splines +=========== + +.. autosummary:: + :toctree: generated/ + + BSpline + make_interp_spline + make_lsq_spline + make_smoothing_spline + generate_knots + make_splrep + make_splprep + +Functional interface to FITPACK routines: + +.. autosummary:: + :toctree: generated/ + + splrep + splprep + splev + splint + sproot + spalde + splder + splantider + insert + +Object-oriented FITPACK interface: + +.. autosummary:: + :toctree: generated/ + + UnivariateSpline + InterpolatedUnivariateSpline + LSQUnivariateSpline + + + +2-D Splines +=========== + +For data on a grid: + +.. autosummary:: + :toctree: generated/ + + RectBivariateSpline + RectSphereBivariateSpline + +For unstructured data: + +.. autosummary:: + :toctree: generated/ + + BivariateSpline + SmoothBivariateSpline + SmoothSphereBivariateSpline + LSQBivariateSpline + LSQSphereBivariateSpline + +Low-level interface to FITPACK functions: + +.. autosummary:: + :toctree: generated/ + + bisplrep + bisplev + +Rational Approximation +====================== + +.. autosummary:: + :toctree: generated/ + + pade + AAA + +Additional tools +================ + +.. autosummary:: + :toctree: generated/ + + lagrange + approximate_taylor_polynomial + +.. seealso:: + + `scipy.ndimage.map_coordinates`, + `scipy.ndimage.spline_filter`, + `scipy.signal.resample`, + `scipy.signal.bspline`, + `scipy.signal.gauss_spline`, + `scipy.signal.qspline1d`, + `scipy.signal.cspline1d`, + `scipy.signal.qspline1d_eval`, + `scipy.signal.cspline1d_eval`, + `scipy.signal.qspline2d`, + `scipy.signal.cspline2d`. + +``pchip`` is an alias of `PchipInterpolator` for backward compatibility +(should not be used in new code). +""" +from ._interpolate import * +from ._fitpack_py import * + +# New interface to fitpack library: +from ._fitpack2 import * + +from ._rbf import Rbf + +from ._rbfinterp import * + +from ._polyint import * + +from ._cubic import * + +from ._ndgriddata import * + +from ._bsplines import * +from ._fitpack_repro import generate_knots, make_splrep, make_splprep + +from ._pade import * + +from ._rgi import * + +from ._ndbspline import NdBSpline + +from ._bary_rational import * + +# Deprecated namespaces, to be removed in v2.0.0 +from . import fitpack, fitpack2, interpolate, ndgriddata, polyint, rbf, interpnd + +__all__ = [s for s in dir() if not s.startswith('_')] + +from scipy._lib._testutils import PytestTester +test = PytestTester(__name__) +del PytestTester + +# Backward compatibility +pchip = PchipInterpolator diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_bary_rational.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_bary_rational.py new file mode 100644 index 0000000000000000000000000000000000000000..be13c06e27cb8df7ec4c55993dd3937e867429e5 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_bary_rational.py @@ -0,0 +1,715 @@ +# Copyright (c) 2017, The Chancellor, Masters and Scholars of the University +# of Oxford, and the Chebfun Developers. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the University of Oxford nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +import warnings +import operator + +import numpy as np +import scipy + + +__all__ = ["AAA", "FloaterHormannInterpolator"] + + +class _BarycentricRational: + """Base class for Barycentric representation of a rational function.""" + def __init__(self, x, y, **kwargs): + # input validation + z = np.asarray(x) + f = np.asarray(y) + + self._input_validation(z, f, **kwargs) + + # Remove infinite or NaN function values and repeated entries + to_keep = np.logical_and.reduce( + ((np.isfinite(f)) & (~np.isnan(f))).reshape(f.shape[0], -1), + axis=-1 + ) + f = f[to_keep, ...] + z = z[to_keep] + z, uni = np.unique(z, return_index=True) + f = f[uni, ...] + + self._shape = f.shape[1:] + self._support_points, self._support_values, self.weights = ( + self._compute_weights(z, f, **kwargs) + ) + + # only compute once + self._poles = None + self._residues = None + self._roots = None + + def _input_validation(self, x, y, **kwargs): + if x.ndim != 1: + raise ValueError("`x` must be 1-D.") + + if not y.ndim >= 1: + raise ValueError("`y` must be at least 1-D.") + + if x.size != y.shape[0]: + raise ValueError("`x` be the same size as the first dimension of `y`.") + + if not np.all(np.isfinite(x)): + raise ValueError("`x` must be finite.") + + def _compute_weights(z, f, **kwargs): + raise NotImplementedError + + def __call__(self, z): + """Evaluate the rational approximation at given values. + + Parameters + ---------- + z : array_like + Input values. + """ + # evaluate rational function in barycentric form. + z = np.asarray(z) + zv = np.ravel(z) + + support_values = self._support_values.reshape( + (self._support_values.shape[0], -1) + ) + weights = self.weights[..., np.newaxis] + + # Cauchy matrix + # Ignore errors due to inf/inf at support points, these will be fixed later + with np.errstate(invalid="ignore", divide="ignore"): + CC = 1 / np.subtract.outer(zv, self._support_points) + # Vector of values + r = CC @ (weights * support_values) / (CC @ weights) + + # Deal with input inf: `r(inf) = lim r(z) = sum(w*f) / sum(w)` + if np.any(np.isinf(zv)): + r[np.isinf(zv)] = (np.sum(weights * support_values) + / np.sum(weights)) + + # Deal with NaN + ii = np.nonzero(np.isnan(r))[0] + for jj in ii: + if np.isnan(zv[jj]) or not np.any(zv[jj] == self._support_points): + # r(NaN) = NaN is fine. + # The second case may happen if `r(zv[ii]) = 0/0` at some point. + pass + else: + # Clean up values `NaN = inf/inf` at support points. + # Find the corresponding node and set entry to correct value: + r[jj] = support_values[zv[jj] == self._support_points].squeeze() + + return np.reshape(r, z.shape + self._shape) + + def poles(self): + """Compute the poles of the rational approximation. + + Returns + ------- + poles : array + Poles of the AAA approximation, repeated according to their multiplicity + but not in any specific order. + """ + if self._poles is None: + # Compute poles via generalized eigenvalue problem + m = self.weights.size + B = np.eye(m + 1, dtype=self.weights.dtype) + B[0, 0] = 0 + + E = np.zeros_like(B, dtype=np.result_type(self.weights, + self._support_points)) + E[0, 1:] = self.weights + E[1:, 0] = 1 + np.fill_diagonal(E[1:, 1:], self._support_points) + + pol = scipy.linalg.eigvals(E, B) + self._poles = pol[np.isfinite(pol)] + return self._poles + + def residues(self): + """Compute the residues of the poles of the approximation. + + Returns + ------- + residues : array + Residues associated with the `poles` of the approximation + """ + if self._residues is None: + # Compute residues via formula for res of quotient of analytic functions + with np.errstate(divide="ignore", invalid="ignore"): + N = (1/(np.subtract.outer(self.poles(), self._support_points))) @ ( + self._support_values * self.weights + ) + Ddiff = ( + -((1/np.subtract.outer(self.poles(), self._support_points))**2) + @ self.weights + ) + self._residues = N / Ddiff + return self._residues + + def roots(self): + """Compute the zeros of the rational approximation. + + Returns + ------- + zeros : array + Zeros of the AAA approximation, repeated according to their multiplicity + but not in any specific order. + """ + if self._roots is None: + # Compute zeros via generalized eigenvalue problem + m = self.weights.size + B = np.eye(m + 1, dtype=self.weights.dtype) + B[0, 0] = 0 + E = np.zeros_like(B, dtype=np.result_type(self.weights, + self._support_values, + self._support_points)) + E[0, 1:] = self.weights * self._support_values + E[1:, 0] = 1 + np.fill_diagonal(E[1:, 1:], self._support_points) + + zer = scipy.linalg.eigvals(E, B) + self._roots = zer[np.isfinite(zer)] + return self._roots + + +class AAA(_BarycentricRational): + r""" + AAA real or complex rational approximation. + + As described in [1]_, the AAA algorithm is a greedy algorithm for approximation by + rational functions on a real or complex set of points. The rational approximation is + represented in a barycentric form from which the roots (zeros), poles, and residues + can be computed. + + Parameters + ---------- + x : 1D array_like, shape (n,) + 1-D array containing values of the independent variable. Values may be real or + complex but must be finite. + y : 1D array_like, shape (n,) + Function values ``f(x)``. Infinite and NaN values of `values` and + corresponding values of `points` will be discarded. + rtol : float, optional + Relative tolerance, defaults to ``eps**0.75``. If a small subset of the entries + in `values` are much larger than the rest the default tolerance may be too + loose. If the tolerance is too tight then the approximation may contain + Froissart doublets or the algorithm may fail to converge entirely. + max_terms : int, optional + Maximum number of terms in the barycentric representation, defaults to ``100``. + Must be greater than or equal to one. + clean_up : bool, optional + Automatic removal of Froissart doublets, defaults to ``True``. See notes for + more details. + clean_up_tol : float, optional + Poles with residues less than this number times the geometric mean + of `values` times the minimum distance to `points` are deemed spurious by the + cleanup procedure, defaults to 1e-13. See notes for more details. + + Attributes + ---------- + support_points : array + Support points of the approximation. These are a subset of the provided `x` at + which the approximation strictly interpolates `y`. + See notes for more details. + support_values : array + Value of the approximation at the `support_points`. + weights : array + Weights of the barycentric approximation. + errors : array + Error :math:`|f(z) - r(z)|_\infty` over `points` in the successive iterations + of AAA. + + Warns + ----- + RuntimeWarning + If `rtol` is not achieved in `max_terms` iterations. + + See Also + -------- + FloaterHormannInterpolator : Floater-Hormann barycentric rational interpolation. + pade : Padé approximation. + + Notes + ----- + At iteration :math:`m` (at which point there are :math:`m` terms in the both the + numerator and denominator of the approximation), the + rational approximation in the AAA algorithm takes the barycentric form + + .. math:: + + r(z) = n(z)/d(z) = + \frac{\sum_{j=1}^m\ w_j f_j / (z - z_j)}{\sum_{j=1}^m w_j / (z - z_j)}, + + where :math:`z_1,\dots,z_m` are real or complex support points selected from + `x`, :math:`f_1,\dots,f_m` are the corresponding real or complex data values + from `y`, and :math:`w_1,\dots,w_m` are real or complex weights. + + Each iteration of the algorithm has two parts: the greedy selection the next support + point and the computation of the weights. The first part of each iteration is to + select the next support point to be added :math:`z_{m+1}` from the remaining + unselected `x`, such that the nonlinear residual + :math:`|f(z_{m+1}) - n(z_{m+1})/d(z_{m+1})|` is maximised. The algorithm terminates + when this maximum is less than ``rtol * np.linalg.norm(f, ord=np.inf)``. This means + the interpolation property is only satisfied up to a tolerance, except at the + support points where approximation exactly interpolates the supplied data. + + In the second part of each iteration, the weights :math:`w_j` are selected to solve + the least-squares problem + + .. math:: + + \text{minimise}_{w_j}|fd - n| \quad \text{subject to} \quad + \sum_{j=1}^{m+1} w_j = 1, + + over the unselected elements of `x`. + + One of the challenges with working with rational approximations is the presence of + Froissart doublets, which are either poles with vanishingly small residues or + pole-zero pairs that are close enough together to nearly cancel, see [2]_. The + greedy nature of the AAA algorithm means Froissart doublets are rare. However, if + `rtol` is set too tight then the approximation will stagnate and many Froissart + doublets will appear. Froissart doublets can usually be removed by removing support + points and then resolving the least squares problem. The support point :math:`z_j`, + which is the closest support point to the pole :math:`a` with residue + :math:`\alpha`, is removed if the following is satisfied + + .. math:: + + |\alpha| / |z_j - a| < \verb|clean_up_tol| \cdot \tilde{f}, + + where :math:`\tilde{f}` is the geometric mean of `support_values`. + + + References + ---------- + .. [1] Y. Nakatsukasa, O. Sete, and L. N. Trefethen, "The AAA algorithm for + rational approximation", SIAM J. Sci. Comp. 40 (2018), A1494-A1522. + :doi:`10.1137/16M1106122` + .. [2] J. Gilewicz and M. Pindor, Pade approximants and noise: rational functions, + J. Comp. Appl. Math. 105 (1999), pp. 285-297. + :doi:`10.1016/S0377-0427(02)00674-X` + + Examples + -------- + + Here we reproduce a number of the numerical examples from [1]_ as a demonstration + of the functionality offered by this method. + + >>> import numpy as np + >>> import matplotlib.pyplot as plt + >>> from scipy.interpolate import AAA + >>> import warnings + + For the first example we approximate the gamma function on ``[-3.5, 4.5]`` by + extrapolating from 100 samples in ``[-1.5, 1.5]``. + + >>> from scipy.special import gamma + >>> sample_points = np.linspace(-1.5, 1.5, num=100) + >>> r = AAA(sample_points, gamma(sample_points)) + >>> z = np.linspace(-3.5, 4.5, num=1000) + >>> fig, ax = plt.subplots() + >>> ax.plot(z, gamma(z), label="Gamma") + >>> ax.plot(sample_points, gamma(sample_points), label="Sample points") + >>> ax.plot(z, r(z).real, '--', label="AAA approximation") + >>> ax.set(xlabel="z", ylabel="r(z)", ylim=[-8, 8], xlim=[-3.5, 4.5]) + >>> ax.legend() + >>> plt.show() + + We can also view the poles of the rational approximation and their residues: + + >>> order = np.argsort(r.poles()) + >>> r.poles()[order] + array([-3.81591039e+00+0.j , -3.00269049e+00+0.j , + -1.99999988e+00+0.j , -1.00000000e+00+0.j , + 5.85842812e-17+0.j , 4.77485458e+00-3.06919376j, + 4.77485458e+00+3.06919376j, 5.29095868e+00-0.97373072j, + 5.29095868e+00+0.97373072j]) + >>> r.residues()[order] + array([ 0.03658074 +0.j , -0.16915426 -0.j , + 0.49999915 +0.j , -1. +0.j , + 1. +0.j , -0.81132013 -2.30193429j, + -0.81132013 +2.30193429j, 0.87326839+10.70148546j, + 0.87326839-10.70148546j]) + + For the second example, we call `AAA` with a spiral of 1000 points that wind 7.5 + times around the origin in the complex plane. + + >>> z = np.exp(np.linspace(-0.5, 0.5 + 15j*np.pi, 1000)) + >>> r = AAA(z, np.tan(np.pi*z/2), rtol=1e-13) + + We see that AAA takes 12 steps to converge with the following errors: + + >>> r.errors.size + 12 + >>> r.errors + array([2.49261500e+01, 4.28045609e+01, 1.71346935e+01, 8.65055336e-02, + 1.27106444e-02, 9.90889874e-04, 5.86910543e-05, 1.28735561e-06, + 3.57007424e-08, 6.37007837e-10, 1.67103357e-11, 1.17112299e-13]) + + We can also plot the computed poles: + + >>> fig, ax = plt.subplots() + >>> ax.plot(z.real, z.imag, '.', markersize=2, label="Sample points") + >>> ax.plot(r.poles().real, r.poles().imag, '.', markersize=5, + ... label="Computed poles") + >>> ax.set(xlim=[-3.5, 3.5], ylim=[-3.5, 3.5], aspect="equal") + >>> ax.legend() + >>> plt.show() + + We now demonstrate the removal of Froissart doublets using the `clean_up` method + using an example from [1]_. Here we approximate the function + :math:`f(z)=\log(2 + z^4)/(1 + 16z^4)` by sampling it at 1000 roots of unity. The + algorithm is run with ``rtol=0`` and ``clean_up=False`` to deliberately cause + Froissart doublets to appear. + + >>> z = np.exp(1j*2*np.pi*np.linspace(0,1, num=1000)) + >>> def f(z): + ... return np.log(2 + z**4)/(1 - 16*z**4) + >>> with warnings.catch_warnings(): # filter convergence warning due to rtol=0 + ... warnings.simplefilter('ignore', RuntimeWarning) + ... r = AAA(z, f(z), rtol=0, max_terms=50, clean_up=False) + >>> mask = np.abs(r.residues()) < 1e-13 + >>> fig, axs = plt.subplots(ncols=2) + >>> axs[0].plot(r.poles().real[~mask], r.poles().imag[~mask], '.') + >>> axs[0].plot(r.poles().real[mask], r.poles().imag[mask], 'r.') + + Now we call the `clean_up` method to remove Froissart doublets. + + >>> with warnings.catch_warnings(): + ... warnings.simplefilter('ignore', RuntimeWarning) + ... r.clean_up() + 4 + >>> mask = np.abs(r.residues()) < 1e-13 + >>> axs[1].plot(r.poles().real[~mask], r.poles().imag[~mask], '.') + >>> axs[1].plot(r.poles().real[mask], r.poles().imag[mask], 'r.') + >>> plt.show() + + The left image shows the poles prior of the approximation ``clean_up=False`` with + poles with residue less than ``10^-13`` in absolute value shown in red. The right + image then shows the poles after the `clean_up` method has been called. + """ + def __init__(self, x, y, *, rtol=None, max_terms=100, clean_up=True, + clean_up_tol=1e-13): + super().__init__(x, y, rtol=rtol, max_terms=max_terms) + + if clean_up: + self.clean_up(clean_up_tol) + + def _input_validation(self, x, y, rtol=None, max_terms=100, clean_up=True, + clean_up_tol=1e-13): + max_terms = operator.index(max_terms) + if max_terms < 1: + raise ValueError("`max_terms` must be an integer value greater than or " + "equal to one.") + + if y.ndim != 1: + raise ValueError("`y` must be 1-D.") + + super()._input_validation(x, y) + + @property + def support_points(self): + return self._support_points + + @property + def support_values(self): + return self._support_values + + def _compute_weights(self, z, f, rtol, max_terms): + # Initialization for AAA iteration + M = np.size(z) + mask = np.ones(M, dtype=np.bool_) + dtype = np.result_type(z, f, 1.0) + rtol = np.finfo(dtype).eps**0.75 if rtol is None else rtol + atol = rtol * np.linalg.norm(f, ord=np.inf) + zj = np.empty(max_terms, dtype=dtype) + fj = np.empty(max_terms, dtype=dtype) + # Cauchy matrix + C = np.empty((M, max_terms), dtype=dtype) + # Loewner matrix + A = np.empty((M, max_terms), dtype=dtype) + errors = np.empty(max_terms, dtype=A.real.dtype) + R = np.repeat(np.mean(f), M) + + # AAA iteration + for m in range(max_terms): + # Introduce next support point + # Select next support point + jj = np.argmax(np.abs(f[mask] - R[mask])) + # Update support points + zj[m] = z[mask][jj] + # Update data values + fj[m] = f[mask][jj] + # Next column of Cauchy matrix + # Ignore errors as we manually interpolate at support points + with np.errstate(divide="ignore", invalid="ignore"): + C[:, m] = 1 / (z - z[mask][jj]) + # Update mask + mask[np.nonzero(mask)[0][jj]] = False + # Update Loewner matrix + # Ignore errors as inf values will be masked out in SVD call + with np.errstate(invalid="ignore"): + A[:, m] = (f - fj[m]) * C[:, m] + + # Compute weights + rows = mask.sum() + if rows >= m + 1: + # The usual tall-skinny case + _, s, V = scipy.linalg.svd( + A[mask, : m + 1], full_matrices=False, check_finite=False, + ) + # Treat case of multiple min singular values + mm = s == np.min(s) + # Aim for non-sparse weight vector + wj = (V.conj()[mm, :].sum(axis=0) / np.sqrt(mm.sum())).astype(dtype) + else: + # Fewer rows than columns + V = scipy.linalg.null_space(A[mask, : m + 1], check_finite=False) + nm = V.shape[-1] + # Aim for non-sparse wt vector + wj = V.sum(axis=-1) / np.sqrt(nm) + + # Compute rational approximant + # Omit columns with `wj == 0` + i0 = wj != 0 + # Ignore errors as we manually interpolate at support points + with np.errstate(invalid="ignore"): + # Numerator + N = C[:, : m + 1][:, i0] @ (wj[i0] * fj[: m + 1][i0]) + # Denominator + D = C[:, : m + 1][:, i0] @ wj[i0] + # Interpolate at support points with `wj !=0` + D_inf = np.isinf(D) | np.isnan(D) + D[D_inf] = 1 + N[D_inf] = f[D_inf] + R = N / D + + # Check if converged + max_error = np.linalg.norm(f - R, ord=np.inf) + errors[m] = max_error + if max_error <= atol: + break + + if m == max_terms - 1: + warnings.warn(f"AAA failed to converge within {max_terms} iterations.", + RuntimeWarning, stacklevel=2) + + # Trim off unused array allocation + zj = zj[: m + 1] + fj = fj[: m + 1] + + # Remove support points with zero weight + i_non_zero = wj != 0 + self.errors = errors[: m + 1] + self._points = z + self._values = f + return zj[i_non_zero], fj[i_non_zero], wj[i_non_zero] + + def clean_up(self, cleanup_tol=1e-13): + """Automatic removal of Froissart doublets. + + Parameters + ---------- + cleanup_tol : float, optional + Poles with residues less than this number times the geometric mean + of `values` times the minimum distance to `points` are deemed spurious by + the cleanup procedure, defaults to 1e-13. + + Returns + ------- + int + Number of Froissart doublets detected + """ + # Find negligible residues + geom_mean_abs_f = scipy.stats.gmean(np.abs(self._values)) + + Z_distances = np.min( + np.abs(np.subtract.outer(self.poles(), self._points)), axis=1 + ) + + with np.errstate(divide="ignore", invalid="ignore"): + ii = np.nonzero( + np.abs(self.residues()) / Z_distances < cleanup_tol * geom_mean_abs_f + ) + + ni = ii[0].size + if ni == 0: + return ni + + warnings.warn(f"{ni} Froissart doublets detected.", RuntimeWarning, + stacklevel=2) + + # For each spurious pole find and remove closest support point + closest_spt_point = np.argmin( + np.abs(np.subtract.outer(self._support_points, self.poles()[ii])), axis=0 + ) + self._support_points = np.delete(self._support_points, closest_spt_point) + self._support_values = np.delete(self._support_values, closest_spt_point) + + # Remove support points z from sample set + mask = np.logical_and.reduce( + np.not_equal.outer(self._points, self._support_points), axis=1 + ) + f = self._values[mask] + z = self._points[mask] + + # recompute weights, we resolve the least squares problem for the remaining + # support points + + m = self._support_points.size + + # Cauchy matrix + C = 1 / np.subtract.outer(z, self._support_points) + # Loewner matrix + A = f[:, np.newaxis] * C - C * self._support_values + + # Solve least-squares problem to obtain weights + _, _, V = scipy.linalg.svd(A, check_finite=False) + self.weights = np.conj(V[m - 1,:]) + + # reset roots, poles, residues as cached values will be wrong with new weights + self._poles = None + self._residues = None + self._roots = None + + return ni + + +class FloaterHormannInterpolator(_BarycentricRational): + r""" + Floater-Hormann barycentric rational interpolation. + + As described in [1]_, the method of Floater and Hormann computes weights for a + Barycentric rational interpolant with no poles on the real axis. + + Parameters + ---------- + x : 1D array_like, shape (n,) + 1-D array containing values of the independent variable. Values may be real or + complex but must be finite. + y : array_like, shape (n, ...) + Array containing values of the dependent variable. Infinite and NaN values + of `values` and corresponding values of `x` will be discarded. + d : int, optional + Blends ``n - d`` degree `d` polynomials together. For ``d = n - 1`` it is + equivalent to polynomial interpolation. Must satisfy ``0 <= d < n``, + defaults to 3. + + Attributes + ---------- + weights : array + Weights of the barycentric approximation. + + See Also + -------- + AAA : Barycentric rational approximation of real and complex functions. + pade : Padé approximation. + + Notes + ----- + The Floater-Hormann interpolant is a rational function that interpolates the data + with approximation order :math:`O(h^{d+1})`. The rational function blends ``n - d`` + polynomials of degree `d` together to produce a rational interpolant that contains + no poles on the real axis, unlike `AAA`. The interpolant is given + by + + .. math:: + + r(x) = \frac{\sum_{i=0}^{n-d} \lambda_i(x) p_i(x)} + {\sum_{i=0}^{n-d} \lambda_i(x)}, + + where :math:`p_i(x)` is an interpolating polynomials of at most degree `d` through + the points :math:`(x_i,y_i),\dots,(x_{i+d},y_{i+d}), and :math:`\lambda_i(z)` are + blending functions defined by + + .. math:: + + \lambda_i(x) = \frac{(-1)^i}{(x - x_i)\cdots(x - x_{i+d})}. + + When ``d = n - 1`` this reduces to polynomial interpolation. + + Due to its stability following barycentric representation of the above equation + is used instead for computation + + .. math:: + + r(z) = \frac{\sum_{k=1}^m\ w_k f_k / (x - x_k)}{\sum_{k=1}^m w_k / (x - x_k)}, + + where the weights :math:`w_j` are computed as + + .. math:: + + w_k &= (-1)^{k - d} \sum_{i \in J_k} \prod_{j = i, j \neq k}^{i + d} + 1/|x_k - x_j|, \\ + J_k &= \{ i \in I: k - d \leq i \leq k\},\\ + I &= \{0, 1, \dots, n - d\}. + + References + ---------- + .. [1] M.S. Floater and K. Hormann, "Barycentric rational interpolation with no + poles and high rates of approximation", Numer. Math. 107, 315 (2007). + :doi:`10.1007/s00211-007-0093-y` + + Examples + -------- + + Here we compare the method against polynomial interpolation for an example where + the polynomial interpolation fails due to Runge's phenomenon. + + >>> import numpy as np + >>> from scipy.interpolate import (FloaterHormannInterpolator, + ... BarycentricInterpolator) + >>> def f(z): + ... return 1/(1 + z**2) + >>> z = np.linspace(-5, 5, num=15) + >>> r = FloaterHormannInterpolator(z, f(z)) + >>> p = BarycentricInterpolator(z, f(z)) + >>> zz = np.linspace(-5, 5, num=1000) + >>> import matplotlib.pyplot as plt + >>> fig, ax = plt.subplots() + >>> ax.plot(zz, r(zz), label="Floater=Hormann") + >>> ax.plot(zz, p(zz), label="Polynomial") + >>> ax.legend() + >>> plt.show() + """ + def __init__(self, points, values, *, d=3): + super().__init__(points, values, d=d) + + def _input_validation(self, x, y, d): + d = operator.index(d) + if not (0 <= d < len(x)): + raise ValueError("`d` must satisfy 0 <= d < n") + + super()._input_validation(x, y) + + def _compute_weights(self, z, f, d): + # Floater and Hormann 2007 Eqn. (18) 3 equations later + w = np.zeros_like(z, dtype=np.result_type(z, 1.0)) + n = w.size + for k in range(n): + for i in range(max(k-d, 0), min(k+1, n-d)): + w[k] += 1/np.prod(np.abs(np.delete(z[k] - z[i : i + d + 1], k - i))) + w *= (-1.)**(np.arange(n) - d) + + return z, f, w diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_bsplines.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_bsplines.py new file mode 100644 index 0000000000000000000000000000000000000000..3d68e8d532100f4926328d68cb68d1048f4290e8 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_bsplines.py @@ -0,0 +1,2416 @@ +import operator +from math import prod + +import numpy as np +from scipy._lib._util import normalize_axis_index +from scipy.linalg import (get_lapack_funcs, LinAlgError, + cholesky_banded, cho_solve_banded, + solve, solve_banded) +from scipy.optimize import minimize_scalar +from . import _dierckx +from . import _fitpack_impl +from scipy.sparse import csr_array +from scipy.special import poch +from itertools import combinations + + +__all__ = ["BSpline", "make_interp_spline", "make_lsq_spline", + "make_smoothing_spline"] + + +def _get_dtype(dtype): + """Return np.complex128 for complex dtypes, np.float64 otherwise.""" + if np.issubdtype(dtype, np.complexfloating): + return np.complex128 + else: + return np.float64 + + +def _as_float_array(x, check_finite=False): + """Convert the input into a C contiguous float array. + + NB: Upcasts half- and single-precision floats to double precision. + """ + x = np.ascontiguousarray(x) + dtyp = _get_dtype(x.dtype) + x = x.astype(dtyp, copy=False) + if check_finite and not np.isfinite(x).all(): + raise ValueError("Array must not contain infs or nans.") + return x + + +def _dual_poly(j, k, t, y): + """ + Dual polynomial of the B-spline B_{j,k,t} - + polynomial which is associated with B_{j,k,t}: + $p_{j,k}(y) = (y - t_{j+1})(y - t_{j+2})...(y - t_{j+k})$ + """ + if k == 0: + return 1 + return np.prod([(y - t[j + i]) for i in range(1, k + 1)]) + + +def _diff_dual_poly(j, k, y, d, t): + """ + d-th derivative of the dual polynomial $p_{j,k}(y)$ + """ + if d == 0: + return _dual_poly(j, k, t, y) + if d == k: + return poch(1, k) + comb = list(combinations(range(j + 1, j + k + 1), d)) + res = 0 + for i in range(len(comb) * len(comb[0])): + res += np.prod([(y - t[j + p]) for p in range(1, k + 1) + if (j + p) not in comb[i//d]]) + return res + + +class BSpline: + r"""Univariate spline in the B-spline basis. + + .. math:: + + S(x) = \sum_{j=0}^{n-1} c_j B_{j, k; t}(x) + + where :math:`B_{j, k; t}` are B-spline basis functions of degree `k` + and knots `t`. + + Parameters + ---------- + t : ndarray, shape (n+k+1,) + knots + c : ndarray, shape (>=n, ...) + spline coefficients + k : int + B-spline degree + extrapolate : bool or 'periodic', optional + whether to extrapolate beyond the base interval, ``t[k] .. t[n]``, + or to return nans. + If True, extrapolates the first and last polynomial pieces of b-spline + functions active on the base interval. + If 'periodic', periodic extrapolation is used. + Default is True. + axis : int, optional + Interpolation axis. Default is zero. + + Attributes + ---------- + t : ndarray + knot vector + c : ndarray + spline coefficients + k : int + spline degree + extrapolate : bool + If True, extrapolates the first and last polynomial pieces of b-spline + functions active on the base interval. + axis : int + Interpolation axis. + tck : tuple + A read-only equivalent of ``(self.t, self.c, self.k)`` + + Methods + ------- + __call__ + basis_element + derivative + antiderivative + integrate + insert_knot + construct_fast + design_matrix + from_power_basis + + Notes + ----- + B-spline basis elements are defined via + + .. math:: + + B_{i, 0}(x) = 1, \textrm{if $t_i \le x < t_{i+1}$, otherwise $0$,} + + B_{i, k}(x) = \frac{x - t_i}{t_{i+k} - t_i} B_{i, k-1}(x) + + \frac{t_{i+k+1} - x}{t_{i+k+1} - t_{i+1}} B_{i+1, k-1}(x) + + **Implementation details** + + - At least ``k+1`` coefficients are required for a spline of degree `k`, + so that ``n >= k+1``. Additional coefficients, ``c[j]`` with + ``j > n``, are ignored. + + - B-spline basis elements of degree `k` form a partition of unity on the + *base interval*, ``t[k] <= x <= t[n]``. + + + Examples + -------- + + Translating the recursive definition of B-splines into Python code, we have: + + >>> def B(x, k, i, t): + ... if k == 0: + ... return 1.0 if t[i] <= x < t[i+1] else 0.0 + ... if t[i+k] == t[i]: + ... c1 = 0.0 + ... else: + ... c1 = (x - t[i])/(t[i+k] - t[i]) * B(x, k-1, i, t) + ... if t[i+k+1] == t[i+1]: + ... c2 = 0.0 + ... else: + ... c2 = (t[i+k+1] - x)/(t[i+k+1] - t[i+1]) * B(x, k-1, i+1, t) + ... return c1 + c2 + + >>> def bspline(x, t, c, k): + ... n = len(t) - k - 1 + ... assert (n >= k+1) and (len(c) >= n) + ... return sum(c[i] * B(x, k, i, t) for i in range(n)) + + Note that this is an inefficient (if straightforward) way to + evaluate B-splines --- this spline class does it in an equivalent, + but much more efficient way. + + Here we construct a quadratic spline function on the base interval + ``2 <= x <= 4`` and compare with the naive way of evaluating the spline: + + >>> from scipy.interpolate import BSpline + >>> k = 2 + >>> t = [0, 1, 2, 3, 4, 5, 6] + >>> c = [-1, 2, 0, -1] + >>> spl = BSpline(t, c, k) + >>> spl(2.5) + array(1.375) + >>> bspline(2.5, t, c, k) + 1.375 + + Note that outside of the base interval results differ. This is because + `BSpline` extrapolates the first and last polynomial pieces of B-spline + functions active on the base interval. + + >>> import matplotlib.pyplot as plt + >>> import numpy as np + >>> fig, ax = plt.subplots() + >>> xx = np.linspace(1.5, 4.5, 50) + >>> ax.plot(xx, [bspline(x, t, c ,k) for x in xx], 'r-', lw=3, label='naive') + >>> ax.plot(xx, spl(xx), 'b-', lw=4, alpha=0.7, label='BSpline') + >>> ax.grid(True) + >>> ax.legend(loc='best') + >>> plt.show() + + + References + ---------- + .. [1] Tom Lyche and Knut Morken, Spline methods, + http://www.uio.no/studier/emner/matnat/ifi/INF-MAT5340/v05/undervisningsmateriale/ + .. [2] Carl de Boor, A practical guide to splines, Springer, 2001. + + """ + + def __init__(self, t, c, k, extrapolate=True, axis=0): + super().__init__() + + self.k = operator.index(k) + self.c = np.asarray(c) + self.t = np.ascontiguousarray(t, dtype=np.float64) + + if extrapolate == 'periodic': + self.extrapolate = extrapolate + else: + self.extrapolate = bool(extrapolate) + + n = self.t.shape[0] - self.k - 1 + + axis = normalize_axis_index(axis, self.c.ndim) + + # Note that the normalized axis is stored in the object. + self.axis = axis + if axis != 0: + # roll the interpolation axis to be the first one in self.c + # More specifically, the target shape for self.c is (n, ...), + # and axis !=0 means that we have c.shape (..., n, ...) + # ^ + # axis + self.c = np.moveaxis(self.c, axis, 0) + + if k < 0: + raise ValueError("Spline order cannot be negative.") + if self.t.ndim != 1: + raise ValueError("Knot vector must be one-dimensional.") + if n < self.k + 1: + raise ValueError("Need at least %d knots for degree %d" % + (2*k + 2, k)) + if (np.diff(self.t) < 0).any(): + raise ValueError("Knots must be in a non-decreasing order.") + if len(np.unique(self.t[k:n+1])) < 2: + raise ValueError("Need at least two internal knots.") + if not np.isfinite(self.t).all(): + raise ValueError("Knots should not have nans or infs.") + if self.c.ndim < 1: + raise ValueError("Coefficients must be at least 1-dimensional.") + if self.c.shape[0] < n: + raise ValueError("Knots, coefficients and degree are inconsistent.") + + dt = _get_dtype(self.c.dtype) + self.c = np.ascontiguousarray(self.c, dtype=dt) + + @classmethod + def construct_fast(cls, t, c, k, extrapolate=True, axis=0): + """Construct a spline without making checks. + + Accepts same parameters as the regular constructor. Input arrays + `t` and `c` must of correct shape and dtype. + """ + self = object.__new__(cls) + self.t, self.c, self.k = t, c, k + self.extrapolate = extrapolate + self.axis = axis + return self + + @property + def tck(self): + """Equivalent to ``(self.t, self.c, self.k)`` (read-only). + """ + return self.t, self.c, self.k + + @classmethod + def basis_element(cls, t, extrapolate=True): + """Return a B-spline basis element ``B(x | t[0], ..., t[k+1])``. + + Parameters + ---------- + t : ndarray, shape (k+2,) + internal knots + extrapolate : bool or 'periodic', optional + whether to extrapolate beyond the base interval, ``t[0] .. t[k+1]``, + or to return nans. + If 'periodic', periodic extrapolation is used. + Default is True. + + Returns + ------- + basis_element : callable + A callable representing a B-spline basis element for the knot + vector `t`. + + Notes + ----- + The degree of the B-spline, `k`, is inferred from the length of `t` as + ``len(t)-2``. The knot vector is constructed by appending and prepending + ``k+1`` elements to internal knots `t`. + + Examples + -------- + + Construct a cubic B-spline: + + >>> import numpy as np + >>> from scipy.interpolate import BSpline + >>> b = BSpline.basis_element([0, 1, 2, 3, 4]) + >>> k = b.k + >>> b.t[k:-k] + array([ 0., 1., 2., 3., 4.]) + >>> k + 3 + + Construct a quadratic B-spline on ``[0, 1, 1, 2]``, and compare + to its explicit form: + + >>> t = [0, 1, 1, 2] + >>> b = BSpline.basis_element(t) + >>> def f(x): + ... return np.where(x < 1, x*x, (2. - x)**2) + + >>> import matplotlib.pyplot as plt + >>> fig, ax = plt.subplots() + >>> x = np.linspace(0, 2, 51) + >>> ax.plot(x, b(x), 'g', lw=3) + >>> ax.plot(x, f(x), 'r', lw=8, alpha=0.4) + >>> ax.grid(True) + >>> plt.show() + + """ + k = len(t) - 2 + t = _as_float_array(t) + t = np.r_[(t[0]-1,) * k, t, (t[-1]+1,) * k] + c = np.zeros_like(t) + c[k] = 1. + return cls.construct_fast(t, c, k, extrapolate) + + @classmethod + def design_matrix(cls, x, t, k, extrapolate=False): + """ + Returns a design matrix as a CSR format sparse array. + + Parameters + ---------- + x : array_like, shape (n,) + Points to evaluate the spline at. + t : array_like, shape (nt,) + Sorted 1D array of knots. + k : int + B-spline degree. + extrapolate : bool or 'periodic', optional + Whether to extrapolate based on the first and last intervals + or raise an error. If 'periodic', periodic extrapolation is used. + Default is False. + + .. versionadded:: 1.10.0 + + Returns + ------- + design_matrix : `csr_array` object + Sparse matrix in CSR format where each row contains all the basis + elements of the input row (first row = basis elements of x[0], + ..., last row = basis elements x[-1]). + + Examples + -------- + Construct a design matrix for a B-spline + + >>> from scipy.interpolate import make_interp_spline, BSpline + >>> import numpy as np + >>> x = np.linspace(0, np.pi * 2, 4) + >>> y = np.sin(x) + >>> k = 3 + >>> bspl = make_interp_spline(x, y, k=k) + >>> design_matrix = bspl.design_matrix(x, bspl.t, k) + >>> design_matrix.toarray() + [[1. , 0. , 0. , 0. ], + [0.2962963 , 0.44444444, 0.22222222, 0.03703704], + [0.03703704, 0.22222222, 0.44444444, 0.2962963 ], + [0. , 0. , 0. , 1. ]] + + Construct a design matrix for some vector of knots + + >>> k = 2 + >>> t = [-1, 0, 1, 2, 3, 4, 5, 6] + >>> x = [1, 2, 3, 4] + >>> design_matrix = BSpline.design_matrix(x, t, k).toarray() + >>> design_matrix + [[0.5, 0.5, 0. , 0. , 0. ], + [0. , 0.5, 0.5, 0. , 0. ], + [0. , 0. , 0.5, 0.5, 0. ], + [0. , 0. , 0. , 0.5, 0.5]] + + This result is equivalent to the one created in the sparse format + + >>> c = np.eye(len(t) - k - 1) + >>> design_matrix_gh = BSpline(t, c, k)(x) + >>> np.allclose(design_matrix, design_matrix_gh, atol=1e-14) + True + + Notes + ----- + .. versionadded:: 1.8.0 + + In each row of the design matrix all the basis elements are evaluated + at the certain point (first row - x[0], ..., last row - x[-1]). + + `nt` is a length of the vector of knots: as far as there are + `nt - k - 1` basis elements, `nt` should be not less than `2 * k + 2` + to have at least `k + 1` basis element. + + Out of bounds `x` raises a ValueError. + """ + x = _as_float_array(x, True) + t = _as_float_array(t, True) + + if extrapolate != 'periodic': + extrapolate = bool(extrapolate) + + if k < 0: + raise ValueError("Spline order cannot be negative.") + if t.ndim != 1 or np.any(t[1:] < t[:-1]): + raise ValueError(f"Expect t to be a 1-D sorted array_like, but " + f"got t={t}.") + # There are `nt - k - 1` basis elements in a BSpline built on the + # vector of knots with length `nt`, so to have at least `k + 1` basis + # elements we need to have at least `2 * k + 2` elements in the vector + # of knots. + if len(t) < 2 * k + 2: + raise ValueError(f"Length t is not enough for k={k}.") + + if extrapolate == 'periodic': + # With periodic extrapolation we map x to the segment + # [t[k], t[n]]. + n = t.size - k - 1 + x = t[k] + (x - t[k]) % (t[n] - t[k]) + extrapolate = False + elif not extrapolate and ( + (min(x) < t[k]) or (max(x) > t[t.shape[0] - k - 1]) + ): + # Checks from `find_interval` function + raise ValueError(f'Out of bounds w/ x = {x}.') + + # Compute number of non-zeros of final CSR array in order to determine + # the dtype of indices and indptr of the CSR array. + n = x.shape[0] + nnz = n * (k + 1) + if nnz < np.iinfo(np.int32).max: + int_dtype = np.int32 + else: + int_dtype = np.int64 + + # Get the non-zero elements of the design matrix and per-row `offsets`: + # In row `i`, k+1 nonzero elements are consecutive, and start from `offset[i]` + data, offsets, _ = _dierckx.data_matrix(x, t, k, np.ones_like(x), extrapolate) + data = data.ravel() + + if offsets.dtype != int_dtype: + offsets = offsets.astype(int_dtype) + + # Convert from per-row offsets to the CSR indices/indptr format + indices = np.repeat(offsets, k+1).reshape(-1, k+1) + indices = indices + np.arange(k+1, dtype=int_dtype) + indices = indices.ravel() + + indptr = np.arange(0, (n + 1) * (k + 1), k + 1, dtype=int_dtype) + + return csr_array( + (data, indices, indptr), + shape=(x.shape[0], t.shape[0] - k - 1) + ) + + def __call__(self, x, nu=0, extrapolate=None): + """ + Evaluate a spline function. + + Parameters + ---------- + x : array_like + points to evaluate the spline at. + nu : int, optional + derivative to evaluate (default is 0). + extrapolate : bool or 'periodic', optional + whether to extrapolate based on the first and last intervals + or return nans. If 'periodic', periodic extrapolation is used. + Default is `self.extrapolate`. + + Returns + ------- + y : array_like + Shape is determined by replacing the interpolation axis + in the coefficient array with the shape of `x`. + + """ + if extrapolate is None: + extrapolate = self.extrapolate + x = np.asarray(x) + x_shape, x_ndim = x.shape, x.ndim + x = np.ascontiguousarray(x.ravel(), dtype=np.float64) + + # With periodic extrapolation we map x to the segment + # [self.t[k], self.t[n]]. + if extrapolate == 'periodic': + n = self.t.size - self.k - 1 + x = self.t[self.k] + (x - self.t[self.k]) % (self.t[n] - + self.t[self.k]) + extrapolate = False + + out = np.empty((len(x), prod(self.c.shape[1:])), dtype=self.c.dtype) + self._ensure_c_contiguous() + + # if self.c is complex, so is `out`; cython code in _bspl.pyx expectes + # floats though, so make a view---this expands the last axis, and + # the view is C contiguous if the original is. + # if c.dtype is complex of shape (n,), c.view(float).shape == (2*n,) + # if c.dtype is complex of shape (n, m), c.view(float).shape == (n, 2*m) + + cc = self.c.view(float) + if self.c.ndim == 1 and self.c.dtype.kind == 'c': + cc = cc.reshape(self.c.shape[0], 2) + + _dierckx.evaluate_spline(self.t, cc.reshape(cc.shape[0], -1), + self.k, x, nu, extrapolate, out.view(float)) + + out = out.reshape(x_shape + self.c.shape[1:]) + if self.axis != 0: + # transpose to move the calculated values to the interpolation axis + l = list(range(out.ndim)) + l = l[x_ndim:x_ndim+self.axis] + l[:x_ndim] + l[x_ndim+self.axis:] + out = out.transpose(l) + return out + + def _ensure_c_contiguous(self): + """ + c and t may be modified by the user. The Cython code expects + that they are C contiguous. + + """ + if not self.t.flags.c_contiguous: + self.t = self.t.copy() + if not self.c.flags.c_contiguous: + self.c = self.c.copy() + + def derivative(self, nu=1): + """Return a B-spline representing the derivative. + + Parameters + ---------- + nu : int, optional + Derivative order. + Default is 1. + + Returns + ------- + b : BSpline object + A new instance representing the derivative. + + See Also + -------- + splder, splantider + + """ + c = self.c.copy() + # pad the c array if needed + ct = len(self.t) - len(c) + if ct > 0: + c = np.r_[c, np.zeros((ct,) + c.shape[1:])] + tck = _fitpack_impl.splder((self.t, c, self.k), nu) + return self.construct_fast(*tck, extrapolate=self.extrapolate, + axis=self.axis) + + def antiderivative(self, nu=1): + """Return a B-spline representing the antiderivative. + + Parameters + ---------- + nu : int, optional + Antiderivative order. Default is 1. + + Returns + ------- + b : BSpline object + A new instance representing the antiderivative. + + Notes + ----- + If antiderivative is computed and ``self.extrapolate='periodic'``, + it will be set to False for the returned instance. This is done because + the antiderivative is no longer periodic and its correct evaluation + outside of the initially given x interval is difficult. + + See Also + -------- + splder, splantider + + """ + c = self.c.copy() + # pad the c array if needed + ct = len(self.t) - len(c) + if ct > 0: + c = np.r_[c, np.zeros((ct,) + c.shape[1:])] + tck = _fitpack_impl.splantider((self.t, c, self.k), nu) + + if self.extrapolate == 'periodic': + extrapolate = False + else: + extrapolate = self.extrapolate + + return self.construct_fast(*tck, extrapolate=extrapolate, + axis=self.axis) + + def integrate(self, a, b, extrapolate=None): + """Compute a definite integral of the spline. + + Parameters + ---------- + a : float + Lower limit of integration. + b : float + Upper limit of integration. + extrapolate : bool or 'periodic', optional + whether to extrapolate beyond the base interval, + ``t[k] .. t[-k-1]``, or take the spline to be zero outside of the + base interval. If 'periodic', periodic extrapolation is used. + If None (default), use `self.extrapolate`. + + Returns + ------- + I : array_like + Definite integral of the spline over the interval ``[a, b]``. + + Examples + -------- + Construct the linear spline ``x if x < 1 else 2 - x`` on the base + interval :math:`[0, 2]`, and integrate it + + >>> from scipy.interpolate import BSpline + >>> b = BSpline.basis_element([0, 1, 2]) + >>> b.integrate(0, 1) + array(0.5) + + If the integration limits are outside of the base interval, the result + is controlled by the `extrapolate` parameter + + >>> b.integrate(-1, 1) + array(0.0) + >>> b.integrate(-1, 1, extrapolate=False) + array(0.5) + + >>> import matplotlib.pyplot as plt + >>> fig, ax = plt.subplots() + >>> ax.grid(True) + >>> ax.axvline(0, c='r', lw=5, alpha=0.5) # base interval + >>> ax.axvline(2, c='r', lw=5, alpha=0.5) + >>> xx = [-1, 1, 2] + >>> ax.plot(xx, b(xx)) + >>> plt.show() + + """ + if extrapolate is None: + extrapolate = self.extrapolate + + # Prepare self.t and self.c. + self._ensure_c_contiguous() + + # Swap integration bounds if needed. + sign = 1 + if b < a: + a, b = b, a + sign = -1 + n = self.t.size - self.k - 1 + + if extrapolate != "periodic" and not extrapolate: + # Shrink the integration interval, if needed. + a = max(a, self.t[self.k]) + b = min(b, self.t[n]) + + if self.c.ndim == 1: + # Fast path: use FITPACK's routine + # (cf _fitpack_impl.splint). + integral = _fitpack_impl.splint(a, b, self.tck) + return np.asarray(integral * sign) + + out = np.empty((2, prod(self.c.shape[1:])), dtype=self.c.dtype) + + # Compute the antiderivative. + c = self.c + ct = len(self.t) - len(c) + if ct > 0: + c = np.r_[c, np.zeros((ct,) + c.shape[1:])] + ta, ca, ka = _fitpack_impl.splantider((self.t, c, self.k), 1) + + if extrapolate == 'periodic': + # Split the integral into the part over period (can be several + # of them) and the remaining part. + + ts, te = self.t[self.k], self.t[n] + period = te - ts + interval = b - a + n_periods, left = divmod(interval, period) + + if n_periods > 0: + # Evaluate the difference of antiderivatives. + x = np.asarray([ts, te], dtype=np.float64) + _dierckx.evaluate_spline(ta, ca.reshape(ca.shape[0], -1), + ka, x, 0, False, out) + integral = out[1] - out[0] + integral *= n_periods + else: + integral = np.zeros((1, prod(self.c.shape[1:])), + dtype=self.c.dtype) + + # Map a to [ts, te], b is always a + left. + a = ts + (a - ts) % period + b = a + left + + # If b <= te then we need to integrate over [a, b], otherwise + # over [a, te] and from xs to what is remained. + if b <= te: + x = np.asarray([a, b], dtype=np.float64) + _dierckx.evaluate_spline(ta, ca.reshape(ca.shape[0], -1), + ka, x, 0, False, out) + integral += out[1] - out[0] + else: + x = np.asarray([a, te], dtype=np.float64) + _dierckx.evaluate_spline(ta, ca.reshape(ca.shape[0], -1), + ka, x, 0, False, out) + integral += out[1] - out[0] + + x = np.asarray([ts, ts + b - te], dtype=np.float64) + _dierckx.evaluate_spline(ta, ca.reshape(ca.shape[0], -1), + ka, x, 0, False, out) + integral += out[1] - out[0] + else: + # Evaluate the difference of antiderivatives. + x = np.asarray([a, b], dtype=np.float64) + _dierckx.evaluate_spline(ta, ca.reshape(ca.shape[0], -1), + ka, x, 0, extrapolate, out) + integral = out[1] - out[0] + + integral *= sign + return integral.reshape(ca.shape[1:]) + + @classmethod + def from_power_basis(cls, pp, bc_type='not-a-knot'): + r""" + Construct a polynomial in the B-spline basis + from a piecewise polynomial in the power basis. + + For now, accepts ``CubicSpline`` instances only. + + Parameters + ---------- + pp : CubicSpline + A piecewise polynomial in the power basis, as created + by ``CubicSpline`` + bc_type : string, optional + Boundary condition type as in ``CubicSpline``: one of the + ``not-a-knot``, ``natural``, ``clamped``, or ``periodic``. + Necessary for construction an instance of ``BSpline`` class. + Default is ``not-a-knot``. + + Returns + ------- + b : BSpline object + A new instance representing the initial polynomial + in the B-spline basis. + + Notes + ----- + .. versionadded:: 1.8.0 + + Accepts only ``CubicSpline`` instances for now. + + The algorithm follows from differentiation + the Marsden's identity [1]: each of coefficients of spline + interpolation function in the B-spline basis is computed as follows: + + .. math:: + + c_j = \sum_{m=0}^{k} \frac{(k-m)!}{k!} + c_{m,i} (-1)^{k-m} D^m p_{j,k}(x_i) + + :math:`c_{m, i}` - a coefficient of CubicSpline, + :math:`D^m p_{j, k}(x_i)` - an m-th defivative of a dual polynomial + in :math:`x_i`. + + ``k`` always equals 3 for now. + + First ``n - 2`` coefficients are computed in :math:`x_i = x_j`, e.g. + + .. math:: + + c_1 = \sum_{m=0}^{k} \frac{(k-1)!}{k!} c_{m,1} D^m p_{j,3}(x_1) + + Last ``nod + 2`` coefficients are computed in ``x[-2]``, + ``nod`` - number of derivatives at the ends. + + For example, consider :math:`x = [0, 1, 2, 3, 4]`, + :math:`y = [1, 1, 1, 1, 1]` and bc_type = ``natural`` + + The coefficients of CubicSpline in the power basis: + + :math:`[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0], [1, 1, 1, 1, 1]]` + + The knot vector: :math:`t = [0, 0, 0, 0, 1, 2, 3, 4, 4, 4, 4]` + + In this case + + .. math:: + + c_j = \frac{0!}{k!} c_{3, i} k! = c_{3, i} = 1,~j = 0, ..., 6 + + References + ---------- + .. [1] Tom Lyche and Knut Morken, Spline Methods, 2005, Section 3.1.2 + + """ + from ._cubic import CubicSpline + if not isinstance(pp, CubicSpline): + raise NotImplementedError(f"Only CubicSpline objects are accepted " + f"for now. Got {type(pp)} instead.") + x = pp.x + coef = pp.c + k = pp.c.shape[0] - 1 + n = x.shape[0] + + if bc_type == 'not-a-knot': + t = _not_a_knot(x, k) + elif bc_type == 'natural' or bc_type == 'clamped': + t = _augknt(x, k) + elif bc_type == 'periodic': + t = _periodic_knots(x, k) + else: + raise TypeError(f'Unknown boundary condition: {bc_type}') + + nod = t.shape[0] - (n + k + 1) # number of derivatives at the ends + c = np.zeros(n + nod, dtype=pp.c.dtype) + for m in range(k + 1): + for i in range(n - 2): + c[i] += poch(k + 1, -m) * coef[m, i]\ + * np.power(-1, k - m)\ + * _diff_dual_poly(i, k, x[i], m, t) + for j in range(n - 2, n + nod): + c[j] += poch(k + 1, -m) * coef[m, n - 2]\ + * np.power(-1, k - m)\ + * _diff_dual_poly(j, k, x[n - 2], m, t) + return cls.construct_fast(t, c, k, pp.extrapolate, pp.axis) + + def insert_knot(self, x, m=1): + """Insert a new knot at `x` of multiplicity `m`. + + Given the knots and coefficients of a B-spline representation, create a + new B-spline with a knot inserted `m` times at point `x`. + + Parameters + ---------- + x : float + The position of the new knot + m : int, optional + The number of times to insert the given knot (its multiplicity). + Default is 1. + + Returns + ------- + spl : BSpline object + A new BSpline object with the new knot inserted. + + Notes + ----- + Based on algorithms from [1]_ and [2]_. + + In case of a periodic spline (``self.extrapolate == "periodic"``) + there must be either at least k interior knots t(j) satisfying + ``t(k+1)>> import numpy as np + >>> from scipy.interpolate import BSpline, make_interp_spline + >>> x = np.linspace(0, 10, 5) + >>> y = np.sin(x) + >>> spl = make_interp_spline(x, y, k=3) + >>> spl.t + array([ 0., 0., 0., 0., 5., 10., 10., 10., 10.]) + + Insert a single knot + + >>> spl_1 = spl.insert_knot(3) + >>> spl_1.t + array([ 0., 0., 0., 0., 3., 5., 10., 10., 10., 10.]) + + Insert a multiple knot + + >>> spl_2 = spl.insert_knot(8, m=3) + >>> spl_2.t + array([ 0., 0., 0., 0., 5., 8., 8., 8., 10., 10., 10., 10.]) + + """ + if x < self.t[self.k] or x > self.t[-self.k-1]: + raise ValueError(f"Cannot insert a knot at {x}.") + if m <= 0: + raise ValueError(f"`m` must be positive, got {m = }.") + + tt = self.t.copy() + cc = self.c.copy() + + for _ in range(m): + tt, cc = _insert(x, tt, cc, self.k, self.extrapolate == "periodic") + return self.construct_fast(tt, cc, self.k, self.extrapolate, self.axis) + + +def _insert(xval, t, c, k, periodic=False): + """Insert a single knot at `xval`.""" + # + # This is a port of the FORTRAN `insert` routine by P. Dierckx, + # https://github.com/scipy/scipy/blob/maintenance/1.11.x/scipy/interpolate/fitpack/insert.f + # which carries the following comment: + # + # subroutine insert inserts a new knot x into a spline function s(x) + # of degree k and calculates the b-spline representation of s(x) with + # respect to the new set of knots. in addition, if iopt.ne.0, s(x) + # will be considered as a periodic spline with period per=t(n-k)-t(k+1) + # satisfying the boundary constraints + # t(i+n-2*k-1) = t(i)+per ,i=1,2,...,2*k+1 + # c(i+n-2*k-1) = c(i) ,i=1,2,...,k + # in that case, the knots and b-spline coefficients returned will also + # satisfy these boundary constraints, i.e. + # tt(i+nn-2*k-1) = tt(i)+per ,i=1,2,...,2*k+1 + # cc(i+nn-2*k-1) = cc(i) ,i=1,2,...,k + interval = _dierckx.find_interval(t, k, float(xval), k, False) + if interval < 0: + # extrapolated values are guarded for in BSpline.insert_knot + raise ValueError(f"Cannot insert the knot at {xval}.") + + # super edge case: a knot with multiplicity > k+1 + # see https://github.com/scipy/scipy/commit/037204c3e91 + if t[interval] == t[interval + k + 1]: + interval -= 1 + + if periodic: + if (interval + 1 <= 2*k) and (interval + 1 >= t.shape[0] - 2*k): + # in case of a periodic spline (iopt.ne.0) there must be + # either at least k interior knots t(j) satisfying t(k+1)= nk - k: + # adjust the left-hand boundary knots & coefs + tt[:k] = tt[nk - k:nk] - T + cc[:k, ...] = cc[n2k:n2k + k, ...] + + if interval <= 2*k-1: + # adjust the right-hand boundary knots & coefs + tt[n-k:] = tt[k+1:k+1+k] + T + cc[n2k:n2k + k, ...] = cc[:k, ...] + + return tt, cc + + +################################# +# Interpolating spline helpers # +################################# + +def _not_a_knot(x, k): + """Given data x, construct the knot vector w/ not-a-knot BC. + cf de Boor, XIII(12). + + For even k, it's a bit ad hoc: Greville sites + omit 2nd and 2nd-to-last + data points, a la not-a-knot. + This seems to match what Dierckx does, too: + https://github.com/scipy/scipy/blob/maintenance/1.11.x/scipy/interpolate/fitpack/fpcurf.f#L63-L80 + """ + x = np.asarray(x) + if k % 2 == 1: + k2 = (k + 1) // 2 + t = x.copy() + else: + k2 = k // 2 + t = (x[1:] + x[:-1]) / 2 + + t = t[k2:-k2] + t = np.r_[(x[0],)*(k+1), t, (x[-1],)*(k+1)] + return t + + +def _augknt(x, k): + """Construct a knot vector appropriate for the order-k interpolation.""" + return np.r_[(x[0],)*k, x, (x[-1],)*k] + + +def _convert_string_aliases(deriv, target_shape): + if isinstance(deriv, str): + if deriv == "clamped": + deriv = [(1, np.zeros(target_shape))] + elif deriv == "natural": + deriv = [(2, np.zeros(target_shape))] + else: + raise ValueError(f"Unknown boundary condition : {deriv}") + return deriv + + +def _process_deriv_spec(deriv): + if deriv is not None: + try: + ords, vals = zip(*deriv) + except TypeError as e: + msg = ("Derivatives, `bc_type`, should be specified as a pair of " + "iterables of pairs of (order, value).") + raise ValueError(msg) from e + else: + ords, vals = [], [] + return np.atleast_1d(ords, vals) + + +def _woodbury_algorithm(A, ur, ll, b, k): + ''' + Solve a cyclic banded linear system with upper right + and lower blocks of size ``(k-1) / 2`` using + the Woodbury formula + + Parameters + ---------- + A : 2-D array, shape(k, n) + Matrix of diagonals of original matrix (see + ``solve_banded`` documentation). + ur : 2-D array, shape(bs, bs) + Upper right block matrix. + ll : 2-D array, shape(bs, bs) + Lower left block matrix. + b : 1-D array, shape(n,) + Vector of constant terms of the system of linear equations. + k : int + B-spline degree. + + Returns + ------- + c : 1-D array, shape(n,) + Solution of the original system of linear equations. + + Notes + ----- + This algorithm works only for systems with banded matrix A plus + a correction term U @ V.T, where the matrix U @ V.T gives upper right + and lower left block of A + The system is solved with the following steps: + 1. New systems of linear equations are constructed: + A @ z_i = u_i, + u_i - column vector of U, + i = 1, ..., k - 1 + 2. Matrix Z is formed from vectors z_i: + Z = [ z_1 | z_2 | ... | z_{k - 1} ] + 3. Matrix H = (1 + V.T @ Z)^{-1} + 4. The system A' @ y = b is solved + 5. x = y - Z @ (H @ V.T @ y) + Also, ``n`` should be greater than ``k``, otherwise corner block + elements will intersect with diagonals. + + Examples + -------- + Consider the case of n = 8, k = 5 (size of blocks - 2 x 2). + The matrix of a system: U: V: + x x x * * a b a b 0 0 0 0 1 0 + x x x x * * c 0 c 0 0 0 0 0 1 + x x x x x * * 0 0 0 0 0 0 0 0 + * x x x x x * 0 0 0 0 0 0 0 0 + * * x x x x x 0 0 0 0 0 0 0 0 + d * * x x x x 0 0 d 0 1 0 0 0 + e f * * x x x 0 0 e f 0 1 0 0 + + References + ---------- + .. [1] William H. Press, Saul A. Teukolsky, William T. Vetterling + and Brian P. Flannery, Numerical Recipes, 2007, Section 2.7.3 + + ''' + k_mod = k - k % 2 + bs = int((k - 1) / 2) + (k + 1) % 2 + + n = A.shape[1] + 1 + U = np.zeros((n - 1, k_mod)) + VT = np.zeros((k_mod, n - 1)) # V transpose + + # upper right block + U[:bs, :bs] = ur + VT[np.arange(bs), np.arange(bs) - bs] = 1 + + # lower left block + U[-bs:, -bs:] = ll + VT[np.arange(bs) - bs, np.arange(bs)] = 1 + + Z = solve_banded((bs, bs), A, U) + + H = solve(np.identity(k_mod) + VT @ Z, np.identity(k_mod)) + + y = solve_banded((bs, bs), A, b) + c = y - Z @ (H @ (VT @ y)) + + return c + + +def _periodic_knots(x, k): + ''' + returns vector of nodes on circle + ''' + xc = np.copy(x) + n = len(xc) + if k % 2 == 0: + dx = np.diff(xc) + xc[1: -1] -= dx[:-1] / 2 + dx = np.diff(xc) + t = np.zeros(n + 2 * k) + t[k: -k] = xc + for i in range(0, k): + # filling first `k` elements in descending order + t[k - i - 1] = t[k - i] - dx[-(i % (n - 1)) - 1] + # filling last `k` elements in ascending order + t[-k + i] = t[-k + i - 1] + dx[i % (n - 1)] + return t + + +def _make_interp_per_full_matr(x, y, t, k): + ''' + Returns a solution of a system for B-spline interpolation with periodic + boundary conditions. First ``k - 1`` rows of matrix are conditions of + periodicity (continuity of ``k - 1`` derivatives at the boundary points). + Last ``n`` rows are interpolation conditions. + RHS is ``k - 1`` zeros and ``n`` ordinates in this case. + + Parameters + ---------- + x : 1-D array, shape (n,) + Values of x - coordinate of a given set of points. + y : 1-D array, shape (n,) + Values of y - coordinate of a given set of points. + t : 1-D array, shape(n+2*k,) + Vector of knots. + k : int + The maximum degree of spline + + Returns + ------- + c : 1-D array, shape (n+k-1,) + B-spline coefficients + + Notes + ----- + ``t`` is supposed to be taken on circle. + + ''' + + x, y, t = map(np.asarray, (x, y, t)) + + n = x.size + # LHS: the colocation matrix + derivatives at edges + matr = np.zeros((n + k - 1, n + k - 1)) + + # derivatives at x[0] and x[-1]: + for i in range(k - 1): + bb = _dierckx.evaluate_all_bspl(t, k, x[0], k, i + 1) + matr[i, : k + 1] += bb + bb = _dierckx.evaluate_all_bspl(t, k, x[-1], n + k - 1, i + 1)[:-1] + matr[i, -k:] -= bb + + # colocation matrix + for i in range(n): + xval = x[i] + # find interval + if xval == t[k]: + left = k + else: + left = np.searchsorted(t, xval) - 1 + + # fill a row + bb = _dierckx.evaluate_all_bspl(t, k, xval, left) + matr[i + k - 1, left-k:left+1] = bb + + # RHS + b = np.r_[[0] * (k - 1), y] + + c = solve(matr, b) + return c + + +def _handle_lhs_derivatives(t, k, xval, ab, kl, ku, deriv_ords, offset=0): + """ Fill in the entries of the colocation matrix corresponding to known + derivatives at `xval`. + + The colocation matrix is in the banded storage, as prepared by _coloc. + No error checking. + + Parameters + ---------- + t : ndarray, shape (nt + k + 1,) + knots + k : integer + B-spline order + xval : float + The value at which to evaluate the derivatives at. + ab : ndarray, shape(2*kl + ku + 1, nt), Fortran order + B-spline colocation matrix. + This argument is modified *in-place*. + kl : integer + Number of lower diagonals of ab. + ku : integer + Number of upper diagonals of ab. + deriv_ords : 1D ndarray + Orders of derivatives known at xval + offset : integer, optional + Skip this many rows of the matrix ab. + + """ + # find where `xval` is in the knot vector, `t` + left = _dierckx.find_interval(t, k, float(xval), k, False) + + # compute and fill in the derivatives @ xval + for row in range(deriv_ords.shape[0]): + nu = deriv_ords[row] + wrk = _dierckx.evaluate_all_bspl(t, k, xval, left, nu) + + # if A were a full matrix, it would be just + # ``A[row + offset, left-k:left+1] = bb``. + for a in range(k+1): + clmn = left - k + a + ab[kl + ku + offset + row - clmn, clmn] = wrk[a] + + +def _make_periodic_spline(x, y, t, k, axis): + ''' + Compute the (coefficients of) interpolating B-spline with periodic + boundary conditions. + + Parameters + ---------- + x : array_like, shape (n,) + Abscissas. + y : array_like, shape (n,) + Ordinates. + k : int + B-spline degree. + t : array_like, shape (n + 2 * k,). + Knots taken on a circle, ``k`` on the left and ``k`` on the right + of the vector ``x``. + + Returns + ------- + b : a BSpline object of the degree ``k`` and with knots ``t``. + + Notes + ----- + The original system is formed by ``n + k - 1`` equations where the first + ``k - 1`` of them stand for the ``k - 1`` derivatives continuity on the + edges while the other equations correspond to an interpolating case + (matching all the input points). Due to a special form of knot vector, it + can be proved that in the original system the first and last ``k`` + coefficients of a spline function are the same, respectively. It follows + from the fact that all ``k - 1`` derivatives are equal term by term at ends + and that the matrix of the original system of linear equations is + non-degenerate. So, we can reduce the number of equations to ``n - 1`` + (first ``k - 1`` equations could be reduced). Another trick of this + implementation is cyclic shift of values of B-splines due to equality of + ``k`` unknown coefficients. With this we can receive matrix of the system + with upper right and lower left blocks, and ``k`` diagonals. It allows + to use Woodbury formula to optimize the computations. + + ''' + n = y.shape[0] + + extradim = prod(y.shape[1:]) + y_new = y.reshape(n, extradim) + c = np.zeros((n + k - 1, extradim)) + + # n <= k case is solved with full matrix + if n <= k: + for i in range(extradim): + c[:, i] = _make_interp_per_full_matr(x, y_new[:, i], t, k) + c = np.ascontiguousarray(c.reshape((n + k - 1,) + y.shape[1:])) + return BSpline.construct_fast(t, c, k, extrapolate='periodic', axis=axis) + + nt = len(t) - k - 1 + + # size of block elements + kul = int(k / 2) + + # kl = ku = k + ab = np.zeros((3 * k + 1, nt), dtype=np.float64, order='F') + + # upper right and lower left blocks + ur = np.zeros((kul, kul)) + ll = np.zeros_like(ur) + + # `offset` is made to shift all the non-zero elements to the end of the + # matrix + # NB: 1. drop the last element of `x` because `x[0] = x[-1] + T` & `y[0] == y[-1]` + # 2. pass ab.T to _coloc to make it C-ordered; below it'll be fed to banded + # LAPACK, which needs F-ordered arrays + _dierckx._coloc(x[:-1], t, k, ab.T, k) + + # remove zeros before the matrix + ab = ab[-k - (k + 1) % 2:, :] + + # The least elements in rows (except repetitions) are diagonals + # of block matrices. Upper right matrix is an upper triangular + # matrix while lower left is a lower triangular one. + for i in range(kul): + ur += np.diag(ab[-i - 1, i: kul], k=i) + ll += np.diag(ab[i, -kul - (k % 2): n - 1 + 2 * kul - i], k=-i) + + # remove elements that occur in the last point + # (first and last points are equivalent) + A = ab[:, kul: -k + kul] + + for i in range(extradim): + cc = _woodbury_algorithm(A, ur, ll, y_new[:, i][:-1], k) + c[:, i] = np.concatenate((cc[-kul:], cc, cc[:kul + k % 2])) + c = np.ascontiguousarray(c.reshape((n + k - 1,) + y.shape[1:])) + return BSpline.construct_fast(t, c, k, extrapolate='periodic', axis=axis) + + +def make_interp_spline(x, y, k=3, t=None, bc_type=None, axis=0, + check_finite=True): + """Compute the (coefficients of) interpolating B-spline. + + Parameters + ---------- + x : array_like, shape (n,) + Abscissas. + y : array_like, shape (n, ...) + Ordinates. + k : int, optional + B-spline degree. Default is cubic, ``k = 3``. + t : array_like, shape (nt + k + 1,), optional. + Knots. + The number of knots needs to agree with the number of data points and + the number of derivatives at the edges. Specifically, ``nt - n`` must + equal ``len(deriv_l) + len(deriv_r)``. + bc_type : 2-tuple or None + Boundary conditions. + Default is None, which means choosing the boundary conditions + automatically. Otherwise, it must be a length-two tuple where the first + element (``deriv_l``) sets the boundary conditions at ``x[0]`` and + the second element (``deriv_r``) sets the boundary conditions at + ``x[-1]``. Each of these must be an iterable of pairs + ``(order, value)`` which gives the values of derivatives of specified + orders at the given edge of the interpolation interval. + Alternatively, the following string aliases are recognized: + + * ``"clamped"``: The first derivatives at the ends are zero. This is + equivalent to ``bc_type=([(1, 0.0)], [(1, 0.0)])``. + * ``"natural"``: The second derivatives at ends are zero. This is + equivalent to ``bc_type=([(2, 0.0)], [(2, 0.0)])``. + * ``"not-a-knot"`` (default): The first and second segments are the + same polynomial. This is equivalent to having ``bc_type=None``. + * ``"periodic"``: The values and the first ``k-1`` derivatives at the + ends are equivalent. + + axis : int, optional + Interpolation axis. Default is 0. + check_finite : bool, optional + Whether to check that the input arrays contain only finite numbers. + Disabling may give a performance gain, but may result in problems + (crashes, non-termination) if the inputs do contain infinities or NaNs. + Default is True. + + Returns + ------- + b : a BSpline object of the degree ``k`` and with knots ``t``. + + See Also + -------- + BSpline : base class representing the B-spline objects + CubicSpline : a cubic spline in the polynomial basis + make_lsq_spline : a similar factory function for spline fitting + UnivariateSpline : a wrapper over FITPACK spline fitting routines + splrep : a wrapper over FITPACK spline fitting routines + + Examples + -------- + + Use cubic interpolation on Chebyshev nodes: + + >>> import numpy as np + >>> import matplotlib.pyplot as plt + >>> def cheb_nodes(N): + ... jj = 2.*np.arange(N) + 1 + ... x = np.cos(np.pi * jj / 2 / N)[::-1] + ... return x + + >>> x = cheb_nodes(20) + >>> y = np.sqrt(1 - x**2) + + >>> from scipy.interpolate import BSpline, make_interp_spline + >>> b = make_interp_spline(x, y) + >>> np.allclose(b(x), y) + True + + Note that the default is a cubic spline with a not-a-knot boundary condition + + >>> b.k + 3 + + Here we use a 'natural' spline, with zero 2nd derivatives at edges: + + >>> l, r = [(2, 0.0)], [(2, 0.0)] + >>> b_n = make_interp_spline(x, y, bc_type=(l, r)) # or, bc_type="natural" + >>> np.allclose(b_n(x), y) + True + >>> x0, x1 = x[0], x[-1] + >>> np.allclose([b_n(x0, 2), b_n(x1, 2)], [0, 0]) + True + + Interpolation of parametric curves is also supported. As an example, we + compute a discretization of a snail curve in polar coordinates + + >>> phi = np.linspace(0, 2.*np.pi, 40) + >>> r = 0.3 + np.cos(phi) + >>> x, y = r*np.cos(phi), r*np.sin(phi) # convert to Cartesian coordinates + + Build an interpolating curve, parameterizing it by the angle + + >>> spl = make_interp_spline(phi, np.c_[x, y]) + + Evaluate the interpolant on a finer grid (note that we transpose the result + to unpack it into a pair of x- and y-arrays) + + >>> phi_new = np.linspace(0, 2.*np.pi, 100) + >>> x_new, y_new = spl(phi_new).T + + Plot the result + + >>> plt.plot(x, y, 'o') + >>> plt.plot(x_new, y_new, '-') + >>> plt.show() + + Build a B-spline curve with 2 dimensional y + + >>> x = np.linspace(0, 2*np.pi, 10) + >>> y = np.array([np.sin(x), np.cos(x)]) + + Periodic condition is satisfied because y coordinates of points on the ends + are equivalent + + >>> ax = plt.axes(projection='3d') + >>> xx = np.linspace(0, 2*np.pi, 100) + >>> bspl = make_interp_spline(x, y, k=5, bc_type='periodic', axis=1) + >>> ax.plot3D(xx, *bspl(xx)) + >>> ax.scatter3D(x, *y, color='red') + >>> plt.show() + + """ + # convert string aliases for the boundary conditions + if bc_type is None or bc_type == 'not-a-knot' or bc_type == 'periodic': + deriv_l, deriv_r = None, None + elif isinstance(bc_type, str): + deriv_l, deriv_r = bc_type, bc_type + else: + try: + deriv_l, deriv_r = bc_type + except TypeError as e: + raise ValueError(f"Unknown boundary condition: {bc_type}") from e + + y = np.asarray(y) + + axis = normalize_axis_index(axis, y.ndim) + + x = _as_float_array(x, check_finite) + y = _as_float_array(y, check_finite) + + y = np.moveaxis(y, axis, 0) # now internally interp axis is zero + + # sanity check the input + if bc_type == 'periodic' and not np.allclose(y[0], y[-1], atol=1e-15): + raise ValueError("First and last points does not match while " + "periodic case expected") + if x.size != y.shape[0]: + raise ValueError(f'Shapes of x {x.shape} and y {y.shape} are incompatible') + if np.any(x[1:] == x[:-1]): + raise ValueError("Expect x to not have duplicates") + if x.ndim != 1 or np.any(x[1:] < x[:-1]): + raise ValueError("Expect x to be a 1D strictly increasing sequence.") + + # special-case k=0 right away + if k == 0: + if any(_ is not None for _ in (t, deriv_l, deriv_r)): + raise ValueError("Too much info for k=0: t and bc_type can only " + "be None.") + t = np.r_[x, x[-1]] + c = np.asarray(y) + c = np.ascontiguousarray(c, dtype=_get_dtype(c.dtype)) + return BSpline.construct_fast(t, c, k, axis=axis) + + # special-case k=1 (e.g., Lyche and Morken, Eq.(2.16)) + if k == 1 and t is None: + if not (deriv_l is None and deriv_r is None): + raise ValueError("Too much info for k=1: bc_type can only be None.") + t = np.r_[x[0], x, x[-1]] + c = np.asarray(y) + c = np.ascontiguousarray(c, dtype=_get_dtype(c.dtype)) + return BSpline.construct_fast(t, c, k, axis=axis) + + k = operator.index(k) + + if bc_type == 'periodic' and t is not None: + raise NotImplementedError("For periodic case t is constructed " + "automatically and can not be passed " + "manually") + + # come up with a sensible knot vector, if needed + if t is None: + if deriv_l is None and deriv_r is None: + if bc_type == 'periodic': + t = _periodic_knots(x, k) + else: + t = _not_a_knot(x, k) + else: + t = _augknt(x, k) + + t = _as_float_array(t, check_finite) + + if k < 0: + raise ValueError("Expect non-negative k.") + if t.ndim != 1 or np.any(t[1:] < t[:-1]): + raise ValueError("Expect t to be a 1-D sorted array_like.") + if t.size < x.size + k + 1: + raise ValueError('Got %d knots, need at least %d.' % + (t.size, x.size + k + 1)) + if (x[0] < t[k]) or (x[-1] > t[-k]): + raise ValueError(f'Out of bounds w/ x = {x}.') + + if bc_type == 'periodic': + return _make_periodic_spline(x, y, t, k, axis) + + # Here : deriv_l, r = [(nu, value), ...] + deriv_l = _convert_string_aliases(deriv_l, y.shape[1:]) + deriv_l_ords, deriv_l_vals = _process_deriv_spec(deriv_l) + nleft = deriv_l_ords.shape[0] + + deriv_r = _convert_string_aliases(deriv_r, y.shape[1:]) + deriv_r_ords, deriv_r_vals = _process_deriv_spec(deriv_r) + nright = deriv_r_ords.shape[0] + + if not all(0 <= i <= k for i in deriv_l_ords): + raise ValueError(f"Bad boundary conditions at {x[0]}.") + + if not all(0 <= i <= k for i in deriv_r_ords): + raise ValueError(f"Bad boundary conditions at {x[-1]}.") + + # have `n` conditions for `nt` coefficients; need nt-n derivatives + n = x.size + nt = t.size - k - 1 + + if nt - n != nleft + nright: + raise ValueError("The number of derivatives at boundaries does not " + f"match: expected {nt-n}, got {nleft}+{nright}") + + # bail out if the `y` array is zero-sized + if y.size == 0: + c = np.zeros((nt,) + y.shape[1:], dtype=float) + return BSpline.construct_fast(t, c, k, axis=axis) + + # set up the LHS: the colocation matrix + derivatives at boundaries + # NB: ab is in F order for banded LAPACK; _coloc needs C-ordered arrays, + # this pass ab.T into _coloc + kl = ku = k + ab = np.zeros((2*kl + ku + 1, nt), dtype=np.float64, order='F') + _dierckx._coloc(x, t, k, ab.T, nleft) + if nleft > 0: + _handle_lhs_derivatives(t, k, x[0], ab, kl, ku, deriv_l_ords) + if nright > 0: + _handle_lhs_derivatives(t, k, x[-1], ab, kl, ku, deriv_r_ords, + offset=nt-nright) + + # set up the RHS: values to interpolate (+ derivative values, if any) + extradim = prod(y.shape[1:]) + rhs = np.empty((nt, extradim), dtype=y.dtype) + if nleft > 0: + rhs[:nleft] = deriv_l_vals.reshape(-1, extradim) + rhs[nleft:nt - nright] = y.reshape(-1, extradim) + if nright > 0: + rhs[nt - nright:] = deriv_r_vals.reshape(-1, extradim) + + # solve Ab @ x = rhs; this is the relevant part of linalg.solve_banded + if check_finite: + ab, rhs = map(np.asarray_chkfinite, (ab, rhs)) + gbsv, = get_lapack_funcs(('gbsv',), (ab, rhs)) + lu, piv, c, info = gbsv(kl, ku, ab, rhs, + overwrite_ab=True, overwrite_b=True) + + if info > 0: + raise LinAlgError("Colocation matrix is singular.") + elif info < 0: + raise ValueError('illegal value in %d-th argument of internal gbsv' % -info) + + c = np.ascontiguousarray(c.reshape((nt,) + y.shape[1:])) + return BSpline.construct_fast(t, c, k, axis=axis) + + +def make_lsq_spline(x, y, t, k=3, w=None, axis=0, check_finite=True, *, method="qr"): + r"""Compute the (coefficients of) an LSQ (Least SQuared) based + fitting B-spline. + + The result is a linear combination + + .. math:: + + S(x) = \sum_j c_j B_j(x; t) + + of the B-spline basis elements, :math:`B_j(x; t)`, which minimizes + + .. math:: + + \sum_{j} \left( w_j \times (S(x_j) - y_j) \right)^2 + + Parameters + ---------- + x : array_like, shape (m,) + Abscissas. + y : array_like, shape (m, ...) + Ordinates. + t : array_like, shape (n + k + 1,). + Knots. + Knots and data points must satisfy Schoenberg-Whitney conditions. + k : int, optional + B-spline degree. Default is cubic, ``k = 3``. + w : array_like, shape (m,), optional + Weights for spline fitting. Must be positive. If ``None``, + then weights are all equal. + Default is ``None``. + axis : int, optional + Interpolation axis. Default is zero. + check_finite : bool, optional + Whether to check that the input arrays contain only finite numbers. + Disabling may give a performance gain, but may result in problems + (crashes, non-termination) if the inputs do contain infinities or NaNs. + Default is True. + method : str, optional + Method for solving the linear LSQ problem. Allowed values are "norm-eq" + (Explicitly construct and solve the normal system of equations), and + "qr" (Use the QR factorization of the design matrix). + Default is "qr". + + Returns + ------- + b : a BSpline object of the degree ``k`` with knots ``t``. + + See Also + -------- + BSpline : base class representing the B-spline objects + make_interp_spline : a similar factory function for interpolating splines + LSQUnivariateSpline : a FITPACK-based spline fitting routine + splrep : a FITPACK-based fitting routine + + Notes + ----- + The number of data points must be larger than the spline degree ``k``. + + Knots ``t`` must satisfy the Schoenberg-Whitney conditions, + i.e., there must be a subset of data points ``x[j]`` such that + ``t[j] < x[j] < t[j+k+1]``, for ``j=0, 1,...,n-k-2``. + + Examples + -------- + Generate some noisy data: + + >>> import numpy as np + >>> import matplotlib.pyplot as plt + >>> rng = np.random.default_rng() + >>> x = np.linspace(-3, 3, 50) + >>> y = np.exp(-x**2) + 0.1 * rng.standard_normal(50) + + Now fit a smoothing cubic spline with a pre-defined internal knots. + Here we make the knot vector (k+1)-regular by adding boundary knots: + + >>> from scipy.interpolate import make_lsq_spline, BSpline + >>> t = [-1, 0, 1] + >>> k = 3 + >>> t = np.r_[(x[0],)*(k+1), + ... t, + ... (x[-1],)*(k+1)] + >>> spl = make_lsq_spline(x, y, t, k) + + For comparison, we also construct an interpolating spline for the same + set of data: + + >>> from scipy.interpolate import make_interp_spline + >>> spl_i = make_interp_spline(x, y) + + Plot both: + + >>> xs = np.linspace(-3, 3, 100) + >>> plt.plot(x, y, 'ro', ms=5) + >>> plt.plot(xs, spl(xs), 'g-', lw=3, label='LSQ spline') + >>> plt.plot(xs, spl_i(xs), 'b-', lw=3, alpha=0.7, label='interp spline') + >>> plt.legend(loc='best') + >>> plt.show() + + **NaN handling**: If the input arrays contain ``nan`` values, the result is + not useful since the underlying spline fitting routines cannot deal with + ``nan``. A workaround is to use zero weights for not-a-number data points: + + >>> y[8] = np.nan + >>> w = np.isnan(y) + >>> y[w] = 0. + >>> tck = make_lsq_spline(x, y, t, w=~w) + + Notice the need to replace a ``nan`` by a numerical value (precise value + does not matter as long as the corresponding weight is zero.) + + """ + x = _as_float_array(x, check_finite) + y = _as_float_array(y, check_finite) + t = _as_float_array(t, check_finite) + if w is not None: + w = _as_float_array(w, check_finite) + else: + w = np.ones_like(x) + k = operator.index(k) + + axis = normalize_axis_index(axis, y.ndim) + + y = np.moveaxis(y, axis, 0) # now internally interp axis is zero + + if x.ndim != 1: + raise ValueError("Expect x to be a 1-D sequence.") + if x.shape[0] < k+1: + raise ValueError("Need more x points.") + if k < 0: + raise ValueError("Expect non-negative k.") + if t.ndim != 1 or np.any(t[1:] - t[:-1] < 0): + raise ValueError("Expect t to be a 1D strictly increasing sequence.") + if x.size != y.shape[0]: + raise ValueError(f'Shapes of x {x.shape} and y {y.shape} are incompatible') + if k > 0 and np.any((x < t[k]) | (x > t[-k])): + raise ValueError(f'Out of bounds w/ x = {x}.') + if x.size != w.size: + raise ValueError(f'Shapes of x {x.shape} and w {w.shape} are incompatible') + if method == "norm-eq" and np.any(x[1:] - x[:-1] <= 0): + raise ValueError("Expect x to be a 1D strictly increasing sequence.") + if method == "qr" and any(x[1:] - x[:-1] < 0): + raise ValueError("Expect x to be a 1D non-decreasing sequence.") + + # number of coefficients + n = t.size - k - 1 + + # complex y: view as float, preserve the length + was_complex = y.dtype.kind == 'c' + yy = y.view(float) + if was_complex and y.ndim == 1: + yy = yy.reshape(y.shape[0], 2) + + # multiple r.h.s + extradim = prod(yy.shape[1:]) + yy = yy.reshape(-1, extradim) + + # complex y: view as float, preserve the length + was_complex = y.dtype.kind == 'c' + yy = y.view(float) + if was_complex and y.ndim == 1: + yy = yy.reshape(y.shape[0], 2) + + # multiple r.h.s + extradim = prod(yy.shape[1:]) + yy = yy.reshape(-1, extradim) + + if method == "norm-eq": + # construct A.T @ A and rhs with A the colocation matrix, and + # rhs = A.T @ y for solving the LSQ problem ``A.T @ A @ c = A.T @ y`` + lower = True + ab = np.zeros((k+1, n), dtype=np.float64, order='F') + rhs = np.zeros((n, extradim), dtype=np.float64) + _dierckx._norm_eq_lsq(x, t, k, + yy, + w, + ab.T, rhs) + + # undo complex -> float and flattening the trailing dims + if was_complex: + rhs = rhs.view(complex) + + rhs = rhs.reshape((n,) + y.shape[1:]) + + # have observation matrix & rhs, can solve the LSQ problem + cho_decomp = cholesky_banded(ab, overwrite_ab=True, lower=lower, + check_finite=check_finite) + c = cho_solve_banded((cho_decomp, lower), rhs, overwrite_b=True, + check_finite=check_finite) + elif method == "qr": + _, _, c = _lsq_solve_qr(x, yy, t, k, w) + + if was_complex: + c = c.view(complex) + + else: + raise ValueError(f"Unknown {method =}.") + + + # restore the shape of `c` for both single and multiple r.h.s. + c = c.reshape((n,) + y.shape[1:]) + c = np.ascontiguousarray(c) + return BSpline.construct_fast(t, c, k, axis=axis) + + +###################### +# LSQ spline helpers # +###################### + +def _lsq_solve_qr(x, y, t, k, w): + """Solve for the LSQ spline coeffs given x, y and knots. + + `y` is always 2D: for 1D data, the shape is ``(m, 1)``. + `w` is always 1D: one weight value per `x` value. + + """ + assert y.ndim == 2 + + y_w = y * w[:, None] + A, offset, nc = _dierckx.data_matrix(x, t, k, w) + _dierckx.qr_reduce(A, offset, nc, y_w) # modifies arguments in-place + c = _dierckx.fpback(A, nc, y_w) + + return A, y_w, c + + +############################# +# Smoothing spline helpers # +############################# + +def _compute_optimal_gcv_parameter(X, wE, y, w): + """ + Returns an optimal regularization parameter from the GCV criteria [1]. + + Parameters + ---------- + X : array, shape (5, n) + 5 bands of the design matrix ``X`` stored in LAPACK banded storage. + wE : array, shape (5, n) + 5 bands of the penalty matrix :math:`W^{-1} E` stored in LAPACK banded + storage. + y : array, shape (n,) + Ordinates. + w : array, shape (n,) + Vector of weights. + + Returns + ------- + lam : float + An optimal from the GCV criteria point of view regularization + parameter. + + Notes + ----- + No checks are performed. + + References + ---------- + .. [1] G. Wahba, "Estimating the smoothing parameter" in Spline models + for observational data, Philadelphia, Pennsylvania: Society for + Industrial and Applied Mathematics, 1990, pp. 45-65. + :doi:`10.1137/1.9781611970128` + + """ + + def compute_banded_symmetric_XT_W_Y(X, w, Y): + """ + Assuming that the product :math:`X^T W Y` is symmetric and both ``X`` + and ``Y`` are 5-banded, compute the unique bands of the product. + + Parameters + ---------- + X : array, shape (5, n) + 5 bands of the matrix ``X`` stored in LAPACK banded storage. + w : array, shape (n,) + Array of weights + Y : array, shape (5, n) + 5 bands of the matrix ``Y`` stored in LAPACK banded storage. + + Returns + ------- + res : array, shape (4, n) + The result of the product :math:`X^T Y` stored in the banded way. + + Notes + ----- + As far as the matrices ``X`` and ``Y`` are 5-banded, their product + :math:`X^T W Y` is 7-banded. It is also symmetric, so we can store only + unique diagonals. + + """ + # compute W Y + W_Y = np.copy(Y) + + W_Y[2] *= w + for i in range(2): + W_Y[i, 2 - i:] *= w[:-2 + i] + W_Y[3 + i, :-1 - i] *= w[1 + i:] + + n = X.shape[1] + res = np.zeros((4, n)) + for i in range(n): + for j in range(min(n-i, 4)): + res[-j-1, i + j] = sum(X[j:, i] * W_Y[:5-j, i + j]) + return res + + def compute_b_inv(A): + """ + Inverse 3 central bands of matrix :math:`A=U^T D^{-1} U` assuming that + ``U`` is a unit upper triangular banded matrix using an algorithm + proposed in [1]. + + Parameters + ---------- + A : array, shape (4, n) + Matrix to inverse, stored in LAPACK banded storage. + + Returns + ------- + B : array, shape (4, n) + 3 unique bands of the symmetric matrix that is an inverse to ``A``. + The first row is filled with zeros. + + Notes + ----- + The algorithm is based on the cholesky decomposition and, therefore, + in case matrix ``A`` is close to not positive defined, the function + raises LinalgError. + + Both matrices ``A`` and ``B`` are stored in LAPACK banded storage. + + References + ---------- + .. [1] M. F. Hutchinson and F. R. de Hoog, "Smoothing noisy data with + spline functions," Numerische Mathematik, vol. 47, no. 1, + pp. 99-106, 1985. + :doi:`10.1007/BF01389878` + + """ + + def find_b_inv_elem(i, j, U, D, B): + rng = min(3, n - i - 1) + rng_sum = 0. + if j == 0: + # use 2-nd formula from [1] + for k in range(1, rng + 1): + rng_sum -= U[-k - 1, i + k] * B[-k - 1, i + k] + rng_sum += D[i] + B[-1, i] = rng_sum + else: + # use 1-st formula from [1] + for k in range(1, rng + 1): + diag = abs(k - j) + ind = i + min(k, j) + rng_sum -= U[-k - 1, i + k] * B[-diag - 1, ind + diag] + B[-j - 1, i + j] = rng_sum + + U = cholesky_banded(A) + for i in range(2, 5): + U[-i, i-1:] /= U[-1, :-i+1] + D = 1. / (U[-1])**2 + U[-1] /= U[-1] + + n = U.shape[1] + + B = np.zeros(shape=(4, n)) + for i in range(n - 1, -1, -1): + for j in range(min(3, n - i - 1), -1, -1): + find_b_inv_elem(i, j, U, D, B) + # the first row contains garbage and should be removed + B[0] = [0.] * n + return B + + def _gcv(lam, X, XtWX, wE, XtE): + r""" + Computes the generalized cross-validation criteria [1]. + + Parameters + ---------- + lam : float, (:math:`\lambda \geq 0`) + Regularization parameter. + X : array, shape (5, n) + Matrix is stored in LAPACK banded storage. + XtWX : array, shape (4, n) + Product :math:`X^T W X` stored in LAPACK banded storage. + wE : array, shape (5, n) + Matrix :math:`W^{-1} E` stored in LAPACK banded storage. + XtE : array, shape (4, n) + Product :math:`X^T E` stored in LAPACK banded storage. + + Returns + ------- + res : float + Value of the GCV criteria with the regularization parameter + :math:`\lambda`. + + Notes + ----- + Criteria is computed from the formula (1.3.2) [3]: + + .. math: + + GCV(\lambda) = \dfrac{1}{n} \sum\limits_{k = 1}^{n} \dfrac{ \left( + y_k - f_{\lambda}(x_k) \right)^2}{\left( 1 - \Tr{A}/n\right)^2}$. + The criteria is discussed in section 1.3 [3]. + + The numerator is computed using (2.2.4) [3] and the denominator is + computed using an algorithm from [2] (see in the ``compute_b_inv`` + function). + + References + ---------- + .. [1] G. Wahba, "Estimating the smoothing parameter" in Spline models + for observational data, Philadelphia, Pennsylvania: Society for + Industrial and Applied Mathematics, 1990, pp. 45-65. + :doi:`10.1137/1.9781611970128` + .. [2] M. F. Hutchinson and F. R. de Hoog, "Smoothing noisy data with + spline functions," Numerische Mathematik, vol. 47, no. 1, + pp. 99-106, 1985. + :doi:`10.1007/BF01389878` + .. [3] E. Zemlyanoy, "Generalized cross-validation smoothing splines", + BSc thesis, 2022. Might be available (in Russian) + `here `_ + + """ + # Compute the numerator from (2.2.4) [3] + n = X.shape[1] + c = solve_banded((2, 2), X + lam * wE, y) + res = np.zeros(n) + # compute ``W^{-1} E c`` with respect to banded-storage of ``E`` + tmp = wE * c + for i in range(n): + for j in range(max(0, i - n + 3), min(5, i + 3)): + res[i] += tmp[j, i + 2 - j] + numer = np.linalg.norm(lam * res)**2 / n + + # compute the denominator + lhs = XtWX + lam * XtE + try: + b_banded = compute_b_inv(lhs) + # compute the trace of the product b_banded @ XtX + tr = b_banded * XtWX + tr[:-1] *= 2 + # find the denominator + denom = (1 - sum(sum(tr)) / n)**2 + except LinAlgError: + # cholesky decomposition cannot be performed + raise ValueError('Seems like the problem is ill-posed') + + res = numer / denom + + return res + + n = X.shape[1] + + XtWX = compute_banded_symmetric_XT_W_Y(X, w, X) + XtE = compute_banded_symmetric_XT_W_Y(X, w, wE) + + def fun(lam): + return _gcv(lam, X, XtWX, wE, XtE) + + gcv_est = minimize_scalar(fun, bounds=(0, n), method='Bounded') + if gcv_est.success: + return gcv_est.x + raise ValueError(f"Unable to find minimum of the GCV " + f"function: {gcv_est.message}") + + +def _coeff_of_divided_diff(x): + """ + Returns the coefficients of the divided difference. + + Parameters + ---------- + x : array, shape (n,) + Array which is used for the computation of divided difference. + + Returns + ------- + res : array_like, shape (n,) + Coefficients of the divided difference. + + Notes + ----- + Vector ``x`` should have unique elements, otherwise an error division by + zero might be raised. + + No checks are performed. + + """ + n = x.shape[0] + res = np.zeros(n) + for i in range(n): + pp = 1. + for k in range(n): + if k != i: + pp *= (x[i] - x[k]) + res[i] = 1. / pp + return res + + +def make_smoothing_spline(x, y, w=None, lam=None): + r""" + Compute the (coefficients of) smoothing cubic spline function using + ``lam`` to control the tradeoff between the amount of smoothness of the + curve and its proximity to the data. In case ``lam`` is None, using the + GCV criteria [1] to find it. + + A smoothing spline is found as a solution to the regularized weighted + linear regression problem: + + .. math:: + + \sum\limits_{i=1}^n w_i\lvert y_i - f(x_i) \rvert^2 + + \lambda\int\limits_{x_1}^{x_n} (f^{(2)}(u))^2 d u + + where :math:`f` is a spline function, :math:`w` is a vector of weights and + :math:`\lambda` is a regularization parameter. + + If ``lam`` is None, we use the GCV criteria to find an optimal + regularization parameter, otherwise we solve the regularized weighted + linear regression problem with given parameter. The parameter controls + the tradeoff in the following way: the larger the parameter becomes, the + smoother the function gets. + + Parameters + ---------- + x : array_like, shape (n,) + Abscissas. `n` must be at least 5. + y : array_like, shape (n,) + Ordinates. `n` must be at least 5. + w : array_like, shape (n,), optional + Vector of weights. Default is ``np.ones_like(x)``. + lam : float, (:math:`\lambda \geq 0`), optional + Regularization parameter. If ``lam`` is None, then it is found from + the GCV criteria. Default is None. + + Returns + ------- + func : a BSpline object. + A callable representing a spline in the B-spline basis + as a solution of the problem of smoothing splines using + the GCV criteria [1] in case ``lam`` is None, otherwise using the + given parameter ``lam``. + + Notes + ----- + This algorithm is a clean room reimplementation of the algorithm + introduced by Woltring in FORTRAN [2]. The original version cannot be used + in SciPy source code because of the license issues. The details of the + reimplementation are discussed here (available only in Russian) [4]. + + If the vector of weights ``w`` is None, we assume that all the points are + equal in terms of weights, and vector of weights is vector of ones. + + Note that in weighted residual sum of squares, weights are not squared: + :math:`\sum\limits_{i=1}^n w_i\lvert y_i - f(x_i) \rvert^2` while in + ``splrep`` the sum is built from the squared weights. + + In cases when the initial problem is ill-posed (for example, the product + :math:`X^T W X` where :math:`X` is a design matrix is not a positive + defined matrix) a ValueError is raised. + + References + ---------- + .. [1] G. Wahba, "Estimating the smoothing parameter" in Spline models for + observational data, Philadelphia, Pennsylvania: Society for Industrial + and Applied Mathematics, 1990, pp. 45-65. + :doi:`10.1137/1.9781611970128` + .. [2] H. J. Woltring, A Fortran package for generalized, cross-validatory + spline smoothing and differentiation, Advances in Engineering + Software, vol. 8, no. 2, pp. 104-113, 1986. + :doi:`10.1016/0141-1195(86)90098-7` + .. [3] T. Hastie, J. Friedman, and R. Tisbshirani, "Smoothing Splines" in + The elements of Statistical Learning: Data Mining, Inference, and + prediction, New York: Springer, 2017, pp. 241-249. + :doi:`10.1007/978-0-387-84858-7` + .. [4] E. Zemlyanoy, "Generalized cross-validation smoothing splines", + BSc thesis, 2022. + ``_ (in + Russian) + + Examples + -------- + Generate some noisy data + + >>> import numpy as np + >>> np.random.seed(1234) + >>> n = 200 + >>> def func(x): + ... return x**3 + x**2 * np.sin(4 * x) + >>> x = np.sort(np.random.random_sample(n) * 4 - 2) + >>> y = func(x) + np.random.normal(scale=1.5, size=n) + + Make a smoothing spline function + + >>> from scipy.interpolate import make_smoothing_spline + >>> spl = make_smoothing_spline(x, y) + + Plot both + + >>> import matplotlib.pyplot as plt + >>> grid = np.linspace(x[0], x[-1], 400) + >>> plt.plot(grid, spl(grid), label='Spline') + >>> plt.plot(grid, func(grid), label='Original function') + >>> plt.scatter(x, y, marker='.') + >>> plt.legend(loc='best') + >>> plt.show() + + """ + + x = np.ascontiguousarray(x, dtype=float) + y = np.ascontiguousarray(y, dtype=float) + + if any(x[1:] - x[:-1] <= 0): + raise ValueError('``x`` should be an ascending array') + + if x.ndim != 1 or y.ndim != 1 or x.shape[0] != y.shape[0]: + raise ValueError('``x`` and ``y`` should be one dimensional and the' + ' same size') + + if w is None: + w = np.ones(len(x)) + else: + w = np.ascontiguousarray(w) + if any(w <= 0): + raise ValueError('Invalid vector of weights') + + t = np.r_[[x[0]] * 3, x, [x[-1]] * 3] + n = x.shape[0] + + if n <= 4: + raise ValueError('``x`` and ``y`` length must be at least 5') + + # It is known that the solution to the stated minimization problem exists + # and is a natural cubic spline with vector of knots equal to the unique + # elements of ``x`` [3], so we will solve the problem in the basis of + # natural splines. + + # create design matrix in the B-spline basis + X_bspl = BSpline.design_matrix(x, t, 3) + # move from B-spline basis to the basis of natural splines using equations + # (2.1.7) [4] + # central elements + X = np.zeros((5, n)) + for i in range(1, 4): + X[i, 2: -2] = X_bspl[i: i - 4, 3: -3][np.diag_indices(n - 4)] + + # first elements + X[1, 1] = X_bspl[0, 0] + X[2, :2] = ((x[2] + x[1] - 2 * x[0]) * X_bspl[0, 0], + X_bspl[1, 1] + X_bspl[1, 2]) + X[3, :2] = ((x[2] - x[0]) * X_bspl[1, 1], X_bspl[2, 2]) + + # last elements + X[1, -2:] = (X_bspl[-3, -3], (x[-1] - x[-3]) * X_bspl[-2, -2]) + X[2, -2:] = (X_bspl[-2, -3] + X_bspl[-2, -2], + (2 * x[-1] - x[-2] - x[-3]) * X_bspl[-1, -1]) + X[3, -2] = X_bspl[-1, -1] + + # create penalty matrix and divide it by vector of weights: W^{-1} E + wE = np.zeros((5, n)) + wE[2:, 0] = _coeff_of_divided_diff(x[:3]) / w[:3] + wE[1:, 1] = _coeff_of_divided_diff(x[:4]) / w[:4] + for j in range(2, n - 2): + wE[:, j] = (x[j+2] - x[j-2]) * _coeff_of_divided_diff(x[j-2:j+3])\ + / w[j-2: j+3] + + wE[:-1, -2] = -_coeff_of_divided_diff(x[-4:]) / w[-4:] + wE[:-2, -1] = _coeff_of_divided_diff(x[-3:]) / w[-3:] + wE *= 6 + + if lam is None: + lam = _compute_optimal_gcv_parameter(X, wE, y, w) + elif lam < 0.: + raise ValueError('Regularization parameter should be non-negative') + + # solve the initial problem in the basis of natural splines + c = solve_banded((2, 2), X + lam * wE, y) + # move back to B-spline basis using equations (2.2.10) [4] + c_ = np.r_[c[0] * (t[5] + t[4] - 2 * t[3]) + c[1], + c[0] * (t[5] - t[3]) + c[1], + c[1: -1], + c[-1] * (t[-4] - t[-6]) + c[-2], + c[-1] * (2 * t[-4] - t[-5] - t[-6]) + c[-2]] + + return BSpline.construct_fast(t, c_, 3) + + +######################## +# FITPACK look-alikes # +######################## + +def fpcheck(x, t, k): + """ Check consistency of the data vector `x` and the knot vector `t`. + + Return None if inputs are consistent, raises a ValueError otherwise. + """ + # This routine is a clone of the `fpchec` Fortran routine, + # https://github.com/scipy/scipy/blob/main/scipy/interpolate/fitpack/fpchec.f + # which carries the following comment: + # + # subroutine fpchec verifies the number and the position of the knots + # t(j),j=1,2,...,n of a spline of degree k, in relation to the number + # and the position of the data points x(i),i=1,2,...,m. if all of the + # following conditions are fulfilled, the error parameter ier is set + # to zero. if one of the conditions is violated ier is set to ten. + # 1) k+1 <= n-k-1 <= m + # 2) t(1) <= t(2) <= ... <= t(k+1) + # t(n-k) <= t(n-k+1) <= ... <= t(n) + # 3) t(k+1) < t(k+2) < ... < t(n-k) + # 4) t(k+1) <= x(i) <= t(n-k) + # 5) the conditions specified by schoenberg and whitney must hold + # for at least one subset of data points, i.e. there must be a + # subset of data points y(j) such that + # t(j) < y(j) < t(j+k+1), j=1,2,...,n-k-1 + x = np.asarray(x) + t = np.asarray(t) + + if x.ndim != 1 or t.ndim != 1: + raise ValueError(f"Expect `x` and `t` be 1D sequences. Got {x = } and {t = }") + + m = x.shape[0] + n = t.shape[0] + nk1 = n - k - 1 + + # check condition no 1 + # c 1) k+1 <= n-k-1 <= m + if not (k + 1 <= nk1 <= m): + raise ValueError(f"Need k+1 <= n-k-1 <= m. Got {m = }, {n = } and {k = }.") + + # check condition no 2 + # c 2) t(1) <= t(2) <= ... <= t(k+1) + # c t(n-k) <= t(n-k+1) <= ... <= t(n) + if (t[:k+1] > t[1:k+2]).any(): + raise ValueError(f"First k knots must be ordered; got {t = }.") + + if (t[nk1:] < t[nk1-1:-1]).any(): + raise ValueError(f"Last k knots must be ordered; got {t = }.") + + # c check condition no 3 + # c 3) t(k+1) < t(k+2) < ... < t(n-k) + if (t[k+1:n-k] <= t[k:n-k-1]).any(): + raise ValueError(f"Internal knots must be distinct. Got {t = }.") + + # c check condition no 4 + # c 4) t(k+1) <= x(i) <= t(n-k) + # NB: FITPACK's fpchec only checks x[0] & x[-1], so we follow. + if (x[0] < t[k]) or (x[-1] > t[n-k-1]): + raise ValueError(f"Out of bounds: {x = } and {t = }.") + + # c check condition no 5 + # c 5) the conditions specified by schoenberg and whitney must hold + # c for at least one subset of data points, i.e. there must be a + # c subset of data points y(j) such that + # c t(j) < y(j) < t(j+k+1), j=1,2,...,n-k-1 + mesg = f"Schoenberg-Whitney condition is violated with {t = } and {x =}." + + if (x[0] >= t[k+1]) or (x[-1] <= t[n-k-2]): + raise ValueError(mesg) + + m = x.shape[0] + l = k+1 + nk3 = n - k - 3 + if nk3 < 2: + return + for j in range(1, nk3+1): + tj = t[j] + l += 1 + tl = t[l] + i = np.argmax(x > tj) + if i >= m-1: + raise ValueError(mesg) + if x[i] >= tl: + raise ValueError(mesg) + return diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_cubic.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_cubic.py new file mode 100644 index 0000000000000000000000000000000000000000..3139e145916fa0637552331974ce531da625836f --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_cubic.py @@ -0,0 +1,958 @@ +"""Interpolation algorithms using piecewise cubic polynomials.""" + +from typing import Literal + +import numpy as np + +from scipy.linalg import solve, solve_banded + +from . import PPoly +from ._polyint import _isscalar + +__all__ = ["CubicHermiteSpline", "PchipInterpolator", "pchip_interpolate", + "Akima1DInterpolator", "CubicSpline"] + + +def prepare_input(x, y, axis, dydx=None): + """Prepare input for cubic spline interpolators. + + All data are converted to numpy arrays and checked for correctness. + Axes equal to `axis` of arrays `y` and `dydx` are moved to be the 0th + axis. The value of `axis` is converted to lie in + [0, number of dimensions of `y`). + """ + + x, y = map(np.asarray, (x, y)) + if np.issubdtype(x.dtype, np.complexfloating): + raise ValueError("`x` must contain real values.") + x = x.astype(float) + + if np.issubdtype(y.dtype, np.complexfloating): + dtype = complex + else: + dtype = float + + if dydx is not None: + dydx = np.asarray(dydx) + if y.shape != dydx.shape: + raise ValueError("The shapes of `y` and `dydx` must be identical.") + if np.issubdtype(dydx.dtype, np.complexfloating): + dtype = complex + dydx = dydx.astype(dtype, copy=False) + + y = y.astype(dtype, copy=False) + axis = axis % y.ndim + if x.ndim != 1: + raise ValueError("`x` must be 1-dimensional.") + if x.shape[0] < 2: + raise ValueError("`x` must contain at least 2 elements.") + if x.shape[0] != y.shape[axis]: + raise ValueError(f"The length of `y` along `axis`={axis} doesn't " + "match the length of `x`") + + if not np.all(np.isfinite(x)): + raise ValueError("`x` must contain only finite values.") + if not np.all(np.isfinite(y)): + raise ValueError("`y` must contain only finite values.") + + if dydx is not None and not np.all(np.isfinite(dydx)): + raise ValueError("`dydx` must contain only finite values.") + + dx = np.diff(x) + if np.any(dx <= 0): + raise ValueError("`x` must be strictly increasing sequence.") + + y = np.moveaxis(y, axis, 0) + if dydx is not None: + dydx = np.moveaxis(dydx, axis, 0) + + return x, dx, y, axis, dydx + + +class CubicHermiteSpline(PPoly): + """Piecewise-cubic interpolator matching values and first derivatives. + + The result is represented as a `PPoly` instance. + + Parameters + ---------- + x : array_like, shape (n,) + 1-D array containing values of the independent variable. + Values must be real, finite and in strictly increasing order. + y : array_like + Array containing values of the dependent variable. It can have + arbitrary number of dimensions, but the length along ``axis`` + (see below) must match the length of ``x``. Values must be finite. + dydx : array_like + Array containing derivatives of the dependent variable. It can have + arbitrary number of dimensions, but the length along ``axis`` + (see below) must match the length of ``x``. Values must be finite. + axis : int, optional + Axis along which `y` is assumed to be varying. Meaning that for + ``x[i]`` the corresponding values are ``np.take(y, i, axis=axis)``. + Default is 0. + extrapolate : {bool, 'periodic', None}, optional + If bool, determines whether to extrapolate to out-of-bounds points + based on first and last intervals, or to return NaNs. If 'periodic', + periodic extrapolation is used. If None (default), it is set to True. + + Attributes + ---------- + x : ndarray, shape (n,) + Breakpoints. The same ``x`` which was passed to the constructor. + c : ndarray, shape (4, n-1, ...) + Coefficients of the polynomials on each segment. The trailing + dimensions match the dimensions of `y`, excluding ``axis``. + For example, if `y` is 1-D, then ``c[k, i]`` is a coefficient for + ``(x-x[i])**(3-k)`` on the segment between ``x[i]`` and ``x[i+1]``. + axis : int + Interpolation axis. The same axis which was passed to the + constructor. + + Methods + ------- + __call__ + derivative + antiderivative + integrate + roots + + See Also + -------- + Akima1DInterpolator : Akima 1D interpolator. + PchipInterpolator : PCHIP 1-D monotonic cubic interpolator. + CubicSpline : Cubic spline data interpolator. + PPoly : Piecewise polynomial in terms of coefficients and breakpoints + + Notes + ----- + If you want to create a higher-order spline matching higher-order + derivatives, use `BPoly.from_derivatives`. + + References + ---------- + .. [1] `Cubic Hermite spline + `_ + on Wikipedia. + """ + + def __init__(self, x, y, dydx, axis=0, extrapolate=None): + if extrapolate is None: + extrapolate = True + + x, dx, y, axis, dydx = prepare_input(x, y, axis, dydx) + + dxr = dx.reshape([dx.shape[0]] + [1] * (y.ndim - 1)) + slope = np.diff(y, axis=0) / dxr + t = (dydx[:-1] + dydx[1:] - 2 * slope) / dxr + + c = np.empty((4, len(x) - 1) + y.shape[1:], dtype=t.dtype) + c[0] = t / dxr + c[1] = (slope - dydx[:-1]) / dxr - t + c[2] = dydx[:-1] + c[3] = y[:-1] + + super().__init__(c, x, extrapolate=extrapolate) + self.axis = axis + + +class PchipInterpolator(CubicHermiteSpline): + r"""PCHIP 1-D monotonic cubic interpolation. + + ``x`` and ``y`` are arrays of values used to approximate some function f, + with ``y = f(x)``. The interpolant uses monotonic cubic splines + to find the value of new points. (PCHIP stands for Piecewise Cubic + Hermite Interpolating Polynomial). + + Parameters + ---------- + x : ndarray, shape (npoints, ) + A 1-D array of monotonically increasing real values. ``x`` cannot + include duplicate values (otherwise f is overspecified) + y : ndarray, shape (..., npoints, ...) + A N-D array of real values. ``y``'s length along the interpolation + axis must be equal to the length of ``x``. Use the ``axis`` + parameter to select the interpolation axis. + axis : int, optional + Axis in the ``y`` array corresponding to the x-coordinate values. Defaults + to ``axis=0``. + extrapolate : bool, optional + Whether to extrapolate to out-of-bounds points based on first + and last intervals, or to return NaNs. + + Methods + ------- + __call__ + derivative + antiderivative + roots + + See Also + -------- + CubicHermiteSpline : Piecewise-cubic interpolator. + Akima1DInterpolator : Akima 1D interpolator. + CubicSpline : Cubic spline data interpolator. + PPoly : Piecewise polynomial in terms of coefficients and breakpoints. + + Notes + ----- + The interpolator preserves monotonicity in the interpolation data and does + not overshoot if the data is not smooth. + + The first derivatives are guaranteed to be continuous, but the second + derivatives may jump at :math:`x_k`. + + Determines the derivatives at the points :math:`x_k`, :math:`f'_k`, + by using PCHIP algorithm [1]_. + + Let :math:`h_k = x_{k+1} - x_k`, and :math:`d_k = (y_{k+1} - y_k) / h_k` + are the slopes at internal points :math:`x_k`. + If the signs of :math:`d_k` and :math:`d_{k-1}` are different or either of + them equals zero, then :math:`f'_k = 0`. Otherwise, it is given by the + weighted harmonic mean + + .. math:: + + \frac{w_1 + w_2}{f'_k} = \frac{w_1}{d_{k-1}} + \frac{w_2}{d_k} + + where :math:`w_1 = 2 h_k + h_{k-1}` and :math:`w_2 = h_k + 2 h_{k-1}`. + + The end slopes are set using a one-sided scheme [2]_. + + + References + ---------- + .. [1] F. N. Fritsch and J. Butland, + A method for constructing local + monotone piecewise cubic interpolants, + SIAM J. Sci. Comput., 5(2), 300-304 (1984). + :doi:`10.1137/0905021`. + .. [2] see, e.g., C. Moler, Numerical Computing with Matlab, 2004. + :doi:`10.1137/1.9780898717952` + + """ + + def __init__(self, x, y, axis=0, extrapolate=None): + x, _, y, axis, _ = prepare_input(x, y, axis) + if np.iscomplexobj(y): + msg = ("`PchipInterpolator` only works with real values for `y`. " + "If you are trying to use the real components of the passed array, " + "use `np.real` on the array before passing to `PchipInterpolator`.") + raise ValueError(msg) + xp = x.reshape((x.shape[0],) + (1,)*(y.ndim-1)) + dk = self._find_derivatives(xp, y) + super().__init__(x, y, dk, axis=0, extrapolate=extrapolate) + self.axis = axis + + @staticmethod + def _edge_case(h0, h1, m0, m1): + # one-sided three-point estimate for the derivative + d = ((2*h0 + h1)*m0 - h0*m1) / (h0 + h1) + + # try to preserve shape + mask = np.sign(d) != np.sign(m0) + mask2 = (np.sign(m0) != np.sign(m1)) & (np.abs(d) > 3.*np.abs(m0)) + mmm = (~mask) & mask2 + + d[mask] = 0. + d[mmm] = 3.*m0[mmm] + + return d + + @staticmethod + def _find_derivatives(x, y): + # Determine the derivatives at the points y_k, d_k, by using + # PCHIP algorithm is: + # We choose the derivatives at the point x_k by + # Let m_k be the slope of the kth segment (between k and k+1) + # If m_k=0 or m_{k-1}=0 or sgn(m_k) != sgn(m_{k-1}) then d_k == 0 + # else use weighted harmonic mean: + # w_1 = 2h_k + h_{k-1}, w_2 = h_k + 2h_{k-1} + # 1/d_k = 1/(w_1 + w_2)*(w_1 / m_k + w_2 / m_{k-1}) + # where h_k is the spacing between x_k and x_{k+1} + y_shape = y.shape + if y.ndim == 1: + # So that _edge_case doesn't end up assigning to scalars + x = x[:, None] + y = y[:, None] + + hk = x[1:] - x[:-1] + mk = (y[1:] - y[:-1]) / hk + + if y.shape[0] == 2: + # edge case: only have two points, use linear interpolation + dk = np.zeros_like(y) + dk[0] = mk + dk[1] = mk + return dk.reshape(y_shape) + + smk = np.sign(mk) + condition = (smk[1:] != smk[:-1]) | (mk[1:] == 0) | (mk[:-1] == 0) + + w1 = 2*hk[1:] + hk[:-1] + w2 = hk[1:] + 2*hk[:-1] + + # values where division by zero occurs will be excluded + # by 'condition' afterwards + with np.errstate(divide='ignore', invalid='ignore'): + whmean = (w1/mk[:-1] + w2/mk[1:]) / (w1 + w2) + + dk = np.zeros_like(y) + dk[1:-1][condition] = 0.0 + dk[1:-1][~condition] = 1.0 / whmean[~condition] + + # special case endpoints, as suggested in + # Cleve Moler, Numerical Computing with MATLAB, Chap 3.6 (pchiptx.m) + dk[0] = PchipInterpolator._edge_case(hk[0], hk[1], mk[0], mk[1]) + dk[-1] = PchipInterpolator._edge_case(hk[-1], hk[-2], mk[-1], mk[-2]) + + return dk.reshape(y_shape) + + +def pchip_interpolate(xi, yi, x, der=0, axis=0): + """ + Convenience function for pchip interpolation. + + xi and yi are arrays of values used to approximate some function f, + with ``yi = f(xi)``. The interpolant uses monotonic cubic splines + to find the value of new points x and the derivatives there. + + See `scipy.interpolate.PchipInterpolator` for details. + + Parameters + ---------- + xi : array_like + A sorted list of x-coordinates, of length N. + yi : array_like + A 1-D array of real values. `yi`'s length along the interpolation + axis must be equal to the length of `xi`. If N-D array, use axis + parameter to select correct axis. + + .. deprecated:: 1.13.0 + Complex data is deprecated and will raise an error in + SciPy 1.15.0. If you are trying to use the real components of + the passed array, use ``np.real`` on `yi`. + + x : scalar or array_like + Of length M. + der : int or list, optional + Derivatives to extract. The 0th derivative can be included to + return the function value. + axis : int, optional + Axis in the yi array corresponding to the x-coordinate values. + + Returns + ------- + y : scalar or array_like + The result, of length R or length M or M by R. + + See Also + -------- + PchipInterpolator : PCHIP 1-D monotonic cubic interpolator. + + Examples + -------- + We can interpolate 2D observed data using pchip interpolation: + + >>> import numpy as np + >>> import matplotlib.pyplot as plt + >>> from scipy.interpolate import pchip_interpolate + >>> x_observed = np.linspace(0.0, 10.0, 11) + >>> y_observed = np.sin(x_observed) + >>> x = np.linspace(min(x_observed), max(x_observed), num=100) + >>> y = pchip_interpolate(x_observed, y_observed, x) + >>> plt.plot(x_observed, y_observed, "o", label="observation") + >>> plt.plot(x, y, label="pchip interpolation") + >>> plt.legend() + >>> plt.show() + + """ + P = PchipInterpolator(xi, yi, axis=axis) + + if der == 0: + return P(x) + elif _isscalar(der): + return P.derivative(der)(x) + else: + return [P.derivative(nu)(x) for nu in der] + + +class Akima1DInterpolator(CubicHermiteSpline): + r""" + Akima interpolator + + Fit piecewise cubic polynomials, given vectors x and y. The interpolation + method by Akima uses a continuously differentiable sub-spline built from + piecewise cubic polynomials. The resultant curve passes through the given + data points and will appear smooth and natural. + + Parameters + ---------- + x : ndarray, shape (npoints, ) + 1-D array of monotonically increasing real values. + y : ndarray, shape (..., npoints, ...) + N-D array of real values. The length of ``y`` along the interpolation axis + must be equal to the length of ``x``. Use the ``axis`` parameter to + select the interpolation axis. + axis : int, optional + Axis in the ``y`` array corresponding to the x-coordinate values. Defaults + to ``axis=0``. + method : {'akima', 'makima'}, optional + If ``"makima"``, use the modified Akima interpolation [2]_. + Defaults to ``"akima"``, use the Akima interpolation [1]_. + + .. versionadded:: 1.13.0 + + extrapolate : {bool, None}, optional + If bool, determines whether to extrapolate to out-of-bounds points + based on first and last intervals, or to return NaNs. If None, + ``extrapolate`` is set to False. + + Methods + ------- + __call__ + derivative + antiderivative + roots + + See Also + -------- + PchipInterpolator : PCHIP 1-D monotonic cubic interpolator. + CubicSpline : Cubic spline data interpolator. + PPoly : Piecewise polynomial in terms of coefficients and breakpoints + + Notes + ----- + .. versionadded:: 0.14 + + Use only for precise data, as the fitted curve passes through the given + points exactly. This routine is useful for plotting a pleasingly smooth + curve through a few given points for purposes of plotting. + + Let :math:`\delta_i = (y_{i+1} - y_i) / (x_{i+1} - x_i)` be the slopes of + the interval :math:`\left[x_i, x_{i+1}\right)`. Akima's derivative at + :math:`x_i` is defined as: + + .. math:: + + d_i = \frac{w_1}{w_1 + w_2}\delta_{i-1} + \frac{w_2}{w_1 + w_2}\delta_i + + In the Akima interpolation [1]_ (``method="akima"``), the weights are: + + .. math:: + + \begin{aligned} + w_1 &= |\delta_{i+1} - \delta_i| \\ + w_2 &= |\delta_{i-1} - \delta_{i-2}| + \end{aligned} + + In the modified Akima interpolation [2]_ (``method="makima"``), + to eliminate overshoot and avoid edge cases of both numerator and + denominator being equal to 0, the weights are modified as follows: + + .. math:: + + \begin{align*} + w_1 &= |\delta_{i+1} - \delta_i| + |\delta_{i+1} + \delta_i| / 2 \\ + w_2 &= |\delta_{i-1} - \delta_{i-2}| + |\delta_{i-1} + \delta_{i-2}| / 2 + \end{align*} + + Examples + -------- + Comparison of ``method="akima"`` and ``method="makima"``: + + >>> import numpy as np + >>> from scipy.interpolate import Akima1DInterpolator + >>> import matplotlib.pyplot as plt + >>> x = np.linspace(1, 7, 7) + >>> y = np.array([-1, -1, -1, 0, 1, 1, 1]) + >>> xs = np.linspace(min(x), max(x), num=100) + >>> y_akima = Akima1DInterpolator(x, y, method="akima")(xs) + >>> y_makima = Akima1DInterpolator(x, y, method="makima")(xs) + + >>> fig, ax = plt.subplots() + >>> ax.plot(x, y, "o", label="data") + >>> ax.plot(xs, y_akima, label="akima") + >>> ax.plot(xs, y_makima, label="makima") + >>> ax.legend() + >>> fig.show() + + The overshoot that occurred in ``"akima"`` has been avoided in ``"makima"``. + + References + ---------- + .. [1] A new method of interpolation and smooth curve fitting based + on local procedures. Hiroshi Akima, J. ACM, October 1970, 17(4), + 589-602. :doi:`10.1145/321607.321609` + .. [2] Makima Piecewise Cubic Interpolation. Cleve Moler and Cosmin Ionita, 2019. + https://blogs.mathworks.com/cleve/2019/04/29/makima-piecewise-cubic-interpolation/ + + """ + + def __init__(self, x, y, axis=0, *, method: Literal["akima", "makima"]="akima", + extrapolate:bool | None = None): + if method not in {"akima", "makima"}: + raise NotImplementedError(f"`method`={method} is unsupported.") + # Original implementation in MATLAB by N. Shamsundar (BSD licensed), see + # https://www.mathworks.com/matlabcentral/fileexchange/1814-akima-interpolation + x, dx, y, axis, _ = prepare_input(x, y, axis) + + if np.iscomplexobj(y): + msg = ("`Akima1DInterpolator` only works with real values for `y`. " + "If you are trying to use the real components of the passed array, " + "use `np.real` on the array before passing to " + "`Akima1DInterpolator`.") + raise ValueError(msg) + + # Akima extrapolation historically False; parent class defaults to True. + extrapolate = False if extrapolate is None else extrapolate + + # determine slopes between breakpoints + m = np.empty((x.size + 3, ) + y.shape[1:]) + dx = dx[(slice(None), ) + (None, ) * (y.ndim - 1)] + m[2:-2] = np.diff(y, axis=0) / dx + + # add two additional points on the left ... + m[1] = 2. * m[2] - m[3] + m[0] = 2. * m[1] - m[2] + # ... and on the right + m[-2] = 2. * m[-3] - m[-4] + m[-1] = 2. * m[-2] - m[-3] + + # if m1 == m2 != m3 == m4, the slope at the breakpoint is not + # defined. This is the fill value: + t = .5 * (m[3:] + m[:-3]) + # get the denominator of the slope t + dm = np.abs(np.diff(m, axis=0)) + if method == "makima": + pm = np.abs(m[1:] + m[:-1]) + f1 = dm[2:] + 0.5 * pm[2:] + f2 = dm[:-2] + 0.5 * pm[:-2] + else: + f1 = dm[2:] + f2 = dm[:-2] + f12 = f1 + f2 + # These are the mask of where the slope at breakpoint is defined: + ind = np.nonzero(f12 > 1e-9 * np.max(f12, initial=-np.inf)) + x_ind, y_ind = ind[0], ind[1:] + # Set the slope at breakpoint + t[ind] = (f1[ind] * m[(x_ind + 1,) + y_ind] + + f2[ind] * m[(x_ind + 2,) + y_ind]) / f12[ind] + + super().__init__(x, y, t, axis=0, extrapolate=extrapolate) + self.axis = axis + + def extend(self, c, x, right=True): + raise NotImplementedError("Extending a 1-D Akima interpolator is not " + "yet implemented") + + # These are inherited from PPoly, but they do not produce an Akima + # interpolator. Hence stub them out. + @classmethod + def from_spline(cls, tck, extrapolate=None): + raise NotImplementedError("This method does not make sense for " + "an Akima interpolator.") + + @classmethod + def from_bernstein_basis(cls, bp, extrapolate=None): + raise NotImplementedError("This method does not make sense for " + "an Akima interpolator.") + + +class CubicSpline(CubicHermiteSpline): + """Cubic spline data interpolator. + + Interpolate data with a piecewise cubic polynomial which is twice + continuously differentiable [1]_. The result is represented as a `PPoly` + instance with breakpoints matching the given data. + + Parameters + ---------- + x : array_like, shape (n,) + 1-D array containing values of the independent variable. + Values must be real, finite and in strictly increasing order. + y : array_like + Array containing values of the dependent variable. It can have + arbitrary number of dimensions, but the length along ``axis`` + (see below) must match the length of ``x``. Values must be finite. + axis : int, optional + Axis along which `y` is assumed to be varying. Meaning that for + ``x[i]`` the corresponding values are ``np.take(y, i, axis=axis)``. + Default is 0. + bc_type : string or 2-tuple, optional + Boundary condition type. Two additional equations, given by the + boundary conditions, are required to determine all coefficients of + polynomials on each segment [2]_. + + If `bc_type` is a string, then the specified condition will be applied + at both ends of a spline. Available conditions are: + + * 'not-a-knot' (default): The first and second segment at a curve end + are the same polynomial. It is a good default when there is no + information on boundary conditions. + * 'periodic': The interpolated functions is assumed to be periodic + of period ``x[-1] - x[0]``. The first and last value of `y` must be + identical: ``y[0] == y[-1]``. This boundary condition will result in + ``y'[0] == y'[-1]`` and ``y''[0] == y''[-1]``. + * 'clamped': The first derivative at curves ends are zero. Assuming + a 1D `y`, ``bc_type=((1, 0.0), (1, 0.0))`` is the same condition. + * 'natural': The second derivative at curve ends are zero. Assuming + a 1D `y`, ``bc_type=((2, 0.0), (2, 0.0))`` is the same condition. + + If `bc_type` is a 2-tuple, the first and the second value will be + applied at the curve start and end respectively. The tuple values can + be one of the previously mentioned strings (except 'periodic') or a + tuple ``(order, deriv_values)`` allowing to specify arbitrary + derivatives at curve ends: + + * `order`: the derivative order, 1 or 2. + * `deriv_value`: array_like containing derivative values, shape must + be the same as `y`, excluding ``axis`` dimension. For example, if + `y` is 1-D, then `deriv_value` must be a scalar. If `y` is 3-D with + the shape (n0, n1, n2) and axis=2, then `deriv_value` must be 2-D + and have the shape (n0, n1). + extrapolate : {bool, 'periodic', None}, optional + If bool, determines whether to extrapolate to out-of-bounds points + based on first and last intervals, or to return NaNs. If 'periodic', + periodic extrapolation is used. If None (default), ``extrapolate`` is + set to 'periodic' for ``bc_type='periodic'`` and to True otherwise. + + Attributes + ---------- + x : ndarray, shape (n,) + Breakpoints. The same ``x`` which was passed to the constructor. + c : ndarray, shape (4, n-1, ...) + Coefficients of the polynomials on each segment. The trailing + dimensions match the dimensions of `y`, excluding ``axis``. + For example, if `y` is 1-d, then ``c[k, i]`` is a coefficient for + ``(x-x[i])**(3-k)`` on the segment between ``x[i]`` and ``x[i+1]``. + axis : int + Interpolation axis. The same axis which was passed to the + constructor. + + Methods + ------- + __call__ + derivative + antiderivative + integrate + roots + + See Also + -------- + Akima1DInterpolator : Akima 1D interpolator. + PchipInterpolator : PCHIP 1-D monotonic cubic interpolator. + PPoly : Piecewise polynomial in terms of coefficients and breakpoints. + + Notes + ----- + Parameters `bc_type` and ``extrapolate`` work independently, i.e. the + former controls only construction of a spline, and the latter only + evaluation. + + When a boundary condition is 'not-a-knot' and n = 2, it is replaced by + a condition that the first derivative is equal to the linear interpolant + slope. When both boundary conditions are 'not-a-knot' and n = 3, the + solution is sought as a parabola passing through given points. + + When 'not-a-knot' boundary conditions is applied to both ends, the + resulting spline will be the same as returned by `splrep` (with ``s=0``) + and `InterpolatedUnivariateSpline`, but these two methods use a + representation in B-spline basis. + + .. versionadded:: 0.18.0 + + Examples + -------- + In this example the cubic spline is used to interpolate a sampled sinusoid. + You can see that the spline continuity property holds for the first and + second derivatives and violates only for the third derivative. + + >>> import numpy as np + >>> from scipy.interpolate import CubicSpline + >>> import matplotlib.pyplot as plt + >>> x = np.arange(10) + >>> y = np.sin(x) + >>> cs = CubicSpline(x, y) + >>> xs = np.arange(-0.5, 9.6, 0.1) + >>> fig, ax = plt.subplots(figsize=(6.5, 4)) + >>> ax.plot(x, y, 'o', label='data') + >>> ax.plot(xs, np.sin(xs), label='true') + >>> ax.plot(xs, cs(xs), label="S") + >>> ax.plot(xs, cs(xs, 1), label="S'") + >>> ax.plot(xs, cs(xs, 2), label="S''") + >>> ax.plot(xs, cs(xs, 3), label="S'''") + >>> ax.set_xlim(-0.5, 9.5) + >>> ax.legend(loc='lower left', ncol=2) + >>> plt.show() + + In the second example, the unit circle is interpolated with a spline. A + periodic boundary condition is used. You can see that the first derivative + values, ds/dx=0, ds/dy=1 at the periodic point (1, 0) are correctly + computed. Note that a circle cannot be exactly represented by a cubic + spline. To increase precision, more breakpoints would be required. + + >>> theta = 2 * np.pi * np.linspace(0, 1, 5) + >>> y = np.c_[np.cos(theta), np.sin(theta)] + >>> cs = CubicSpline(theta, y, bc_type='periodic') + >>> print("ds/dx={:.1f} ds/dy={:.1f}".format(cs(0, 1)[0], cs(0, 1)[1])) + ds/dx=0.0 ds/dy=1.0 + >>> xs = 2 * np.pi * np.linspace(0, 1, 100) + >>> fig, ax = plt.subplots(figsize=(6.5, 4)) + >>> ax.plot(y[:, 0], y[:, 1], 'o', label='data') + >>> ax.plot(np.cos(xs), np.sin(xs), label='true') + >>> ax.plot(cs(xs)[:, 0], cs(xs)[:, 1], label='spline') + >>> ax.axes.set_aspect('equal') + >>> ax.legend(loc='center') + >>> plt.show() + + The third example is the interpolation of a polynomial y = x**3 on the + interval 0 <= x<= 1. A cubic spline can represent this function exactly. + To achieve that we need to specify values and first derivatives at + endpoints of the interval. Note that y' = 3 * x**2 and thus y'(0) = 0 and + y'(1) = 3. + + >>> cs = CubicSpline([0, 1], [0, 1], bc_type=((1, 0), (1, 3))) + >>> x = np.linspace(0, 1) + >>> np.allclose(x**3, cs(x)) + True + + References + ---------- + .. [1] `Cubic Spline Interpolation + `_ + on Wikiversity. + .. [2] Carl de Boor, "A Practical Guide to Splines", Springer-Verlag, 1978. + """ + + def __init__(self, x, y, axis=0, bc_type='not-a-knot', extrapolate=None): + x, dx, y, axis, _ = prepare_input(x, y, axis) + n = len(x) + + bc, y = self._validate_bc(bc_type, y, y.shape[1:], axis) + + if extrapolate is None: + if bc[0] == 'periodic': + extrapolate = 'periodic' + else: + extrapolate = True + + if y.size == 0: + # bail out early for zero-sized arrays + s = np.zeros_like(y) + else: + dxr = dx.reshape([dx.shape[0]] + [1] * (y.ndim - 1)) + slope = np.diff(y, axis=0) / dxr + + # If bc is 'not-a-knot' this change is just a convention. + # If bc is 'periodic' then we already checked that y[0] == y[-1], + # and the spline is just a constant, we handle this case in the + # same way by setting the first derivatives to slope, which is 0. + if n == 2: + if bc[0] in ['not-a-knot', 'periodic']: + bc[0] = (1, slope[0]) + if bc[1] in ['not-a-knot', 'periodic']: + bc[1] = (1, slope[0]) + + # This is a special case, when both conditions are 'not-a-knot' + # and n == 3. In this case 'not-a-knot' can't be handled regularly + # as the both conditions are identical. We handle this case by + # constructing a parabola passing through given points. + if n == 3 and bc[0] == 'not-a-knot' and bc[1] == 'not-a-knot': + A = np.zeros((3, 3)) # This is a standard matrix. + b = np.empty((3,) + y.shape[1:], dtype=y.dtype) + + A[0, 0] = 1 + A[0, 1] = 1 + A[1, 0] = dx[1] + A[1, 1] = 2 * (dx[0] + dx[1]) + A[1, 2] = dx[0] + A[2, 1] = 1 + A[2, 2] = 1 + + b[0] = 2 * slope[0] + b[1] = 3 * (dxr[0] * slope[1] + dxr[1] * slope[0]) + b[2] = 2 * slope[1] + + s = solve(A, b, overwrite_a=True, overwrite_b=True, + check_finite=False) + elif n == 3 and bc[0] == 'periodic': + # In case when number of points is 3 we compute the derivatives + # manually + t = (slope / dxr).sum(0) / (1. / dxr).sum(0) + s = np.broadcast_to(t, (n,) + y.shape[1:]) + else: + # Find derivative values at each x[i] by solving a tridiagonal + # system. + A = np.zeros((3, n)) # This is a banded matrix representation. + b = np.empty((n,) + y.shape[1:], dtype=y.dtype) + + # Filling the system for i=1..n-2 + # (x[i-1] - x[i]) * s[i-1] +\ + # 2 * ((x[i] - x[i-1]) + (x[i+1] - x[i])) * s[i] +\ + # (x[i] - x[i-1]) * s[i+1] =\ + # 3 * ((x[i+1] - x[i])*(y[i] - y[i-1])/(x[i] - x[i-1]) +\ + # (x[i] - x[i-1])*(y[i+1] - y[i])/(x[i+1] - x[i])) + + A[1, 1:-1] = 2 * (dx[:-1] + dx[1:]) # The diagonal + A[0, 2:] = dx[:-1] # The upper diagonal + A[-1, :-2] = dx[1:] # The lower diagonal + + b[1:-1] = 3 * (dxr[1:] * slope[:-1] + dxr[:-1] * slope[1:]) + + bc_start, bc_end = bc + + if bc_start == 'periodic': + # Due to the periodicity, and because y[-1] = y[0], the + # linear system has (n-1) unknowns/equations instead of n: + A = A[:, 0:-1] + A[1, 0] = 2 * (dx[-1] + dx[0]) + A[0, 1] = dx[-1] + + b = b[:-1] + + # Also, due to the periodicity, the system is not tri-diagonal. + # We need to compute a "condensed" matrix of shape (n-2, n-2). + # See https://web.archive.org/web/20151220180652/http://www.cfm.brown.edu/people/gk/chap6/node14.html + # for more explanations. + # The condensed matrix is obtained by removing the last column + # and last row of the (n-1, n-1) system matrix. The removed + # values are saved in scalar variables with the (n-1, n-1) + # system matrix indices forming their names: + a_m1_0 = dx[-2] # lower left corner value: A[-1, 0] + a_m1_m2 = dx[-1] + a_m1_m1 = 2 * (dx[-1] + dx[-2]) + a_m2_m1 = dx[-3] + a_0_m1 = dx[0] + + b[0] = 3 * (dxr[0] * slope[-1] + dxr[-1] * slope[0]) + b[-1] = 3 * (dxr[-1] * slope[-2] + dxr[-2] * slope[-1]) + + Ac = A[:, :-1] + b1 = b[:-1] + b2 = np.zeros_like(b1) + b2[0] = -a_0_m1 + b2[-1] = -a_m2_m1 + + # s1 and s2 are the solutions of (n-2, n-2) system + s1 = solve_banded((1, 1), Ac, b1, overwrite_ab=False, + overwrite_b=False, check_finite=False) + + s2 = solve_banded((1, 1), Ac, b2, overwrite_ab=False, + overwrite_b=False, check_finite=False) + + # computing the s[n-2] solution: + s_m1 = ((b[-1] - a_m1_0 * s1[0] - a_m1_m2 * s1[-1]) / + (a_m1_m1 + a_m1_0 * s2[0] + a_m1_m2 * s2[-1])) + + # s is the solution of the (n, n) system: + s = np.empty((n,) + y.shape[1:], dtype=y.dtype) + s[:-2] = s1 + s_m1 * s2 + s[-2] = s_m1 + s[-1] = s[0] + else: + if bc_start == 'not-a-knot': + A[1, 0] = dx[1] + A[0, 1] = x[2] - x[0] + d = x[2] - x[0] + b[0] = ((dxr[0] + 2*d) * dxr[1] * slope[0] + + dxr[0]**2 * slope[1]) / d + elif bc_start[0] == 1: + A[1, 0] = 1 + A[0, 1] = 0 + b[0] = bc_start[1] + elif bc_start[0] == 2: + A[1, 0] = 2 * dx[0] + A[0, 1] = dx[0] + b[0] = -0.5 * bc_start[1] * dx[0]**2 + 3 * (y[1] - y[0]) + + if bc_end == 'not-a-knot': + A[1, -1] = dx[-2] + A[-1, -2] = x[-1] - x[-3] + d = x[-1] - x[-3] + b[-1] = ((dxr[-1]**2*slope[-2] + + (2*d + dxr[-1])*dxr[-2]*slope[-1]) / d) + elif bc_end[0] == 1: + A[1, -1] = 1 + A[-1, -2] = 0 + b[-1] = bc_end[1] + elif bc_end[0] == 2: + A[1, -1] = 2 * dx[-1] + A[-1, -2] = dx[-1] + b[-1] = 0.5 * bc_end[1] * dx[-1]**2 + 3 * (y[-1] - y[-2]) + + s = solve_banded((1, 1), A, b, overwrite_ab=True, + overwrite_b=True, check_finite=False) + + super().__init__(x, y, s, axis=0, extrapolate=extrapolate) + self.axis = axis + + @staticmethod + def _validate_bc(bc_type, y, expected_deriv_shape, axis): + """Validate and prepare boundary conditions. + + Returns + ------- + validated_bc : 2-tuple + Boundary conditions for a curve start and end. + y : ndarray + y casted to complex dtype if one of the boundary conditions has + complex dtype. + """ + if isinstance(bc_type, str): + if bc_type == 'periodic': + if not np.allclose(y[0], y[-1], rtol=1e-15, atol=1e-15): + raise ValueError( + f"The first and last `y` point along axis {axis} must " + "be identical (within machine precision) when " + "bc_type='periodic'.") + + bc_type = (bc_type, bc_type) + + else: + if len(bc_type) != 2: + raise ValueError("`bc_type` must contain 2 elements to " + "specify start and end conditions.") + + if 'periodic' in bc_type: + raise ValueError("'periodic' `bc_type` is defined for both " + "curve ends and cannot be used with other " + "boundary conditions.") + + validated_bc = [] + for bc in bc_type: + if isinstance(bc, str): + if bc == 'clamped': + validated_bc.append((1, np.zeros(expected_deriv_shape))) + elif bc == 'natural': + validated_bc.append((2, np.zeros(expected_deriv_shape))) + elif bc in ['not-a-knot', 'periodic']: + validated_bc.append(bc) + else: + raise ValueError(f"bc_type={bc} is not allowed.") + else: + try: + deriv_order, deriv_value = bc + except Exception as e: + raise ValueError( + "A specified derivative value must be " + "given in the form (order, value)." + ) from e + + if deriv_order not in [1, 2]: + raise ValueError("The specified derivative order must " + "be 1 or 2.") + + deriv_value = np.asarray(deriv_value) + if deriv_value.shape != expected_deriv_shape: + raise ValueError( + f"`deriv_value` shape {deriv_value.shape} is not " + f"the expected one {expected_deriv_shape}." + ) + + if np.issubdtype(deriv_value.dtype, np.complexfloating): + y = y.astype(complex, copy=False) + + validated_bc.append((deriv_order, deriv_value)) + + return validated_bc, y diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_fitpack.cpython-310-x86_64-linux-gnu.so b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_fitpack.cpython-310-x86_64-linux-gnu.so new file mode 100644 index 0000000000000000000000000000000000000000..5f1de8a506d90631543927894cd35196744a879a Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_fitpack.cpython-310-x86_64-linux-gnu.so differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_fitpack2.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_fitpack2.py new file mode 100644 index 0000000000000000000000000000000000000000..daa7773a0f3be6590ea9c0ab328d610efa745859 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_fitpack2.py @@ -0,0 +1,2394 @@ +""" +fitpack --- curve and surface fitting with splines + +fitpack is based on a collection of Fortran routines DIERCKX +by P. Dierckx (see http://www.netlib.org/dierckx/) transformed +to double routines by Pearu Peterson. +""" +# Created by Pearu Peterson, June,August 2003 +__all__ = [ + 'UnivariateSpline', + 'InterpolatedUnivariateSpline', + 'LSQUnivariateSpline', + 'BivariateSpline', + 'LSQBivariateSpline', + 'SmoothBivariateSpline', + 'LSQSphereBivariateSpline', + 'SmoothSphereBivariateSpline', + 'RectBivariateSpline', + 'RectSphereBivariateSpline'] + + +import warnings +from threading import Lock + +from numpy import zeros, concatenate, ravel, diff, array +import numpy as np + +from . import _fitpack_impl +from . import _dfitpack as dfitpack + + +dfitpack_int = dfitpack.types.intvar.dtype +FITPACK_LOCK = Lock() + + +# ############### Univariate spline #################### + +_curfit_messages = {1: """ +The required storage space exceeds the available storage space, as +specified by the parameter nest: nest too small. If nest is already +large (say nest > m/2), it may also indicate that s is too small. +The approximation returned is the weighted least-squares spline +according to the knots t[0],t[1],...,t[n-1]. (n=nest) the parameter fp +gives the corresponding weighted sum of squared residuals (fp>s). +""", + 2: """ +A theoretically impossible result was found during the iteration +process for finding a smoothing spline with fp = s: s too small. +There is an approximation returned but the corresponding weighted sum +of squared residuals does not satisfy the condition abs(fp-s)/s < tol.""", + 3: """ +The maximal number of iterations maxit (set to 20 by the program) +allowed for finding a smoothing spline with fp=s has been reached: s +too small. +There is an approximation returned but the corresponding weighted sum +of squared residuals does not satisfy the condition abs(fp-s)/s < tol.""", + 10: """ +Error on entry, no approximation returned. The following conditions +must hold: +xb<=x[0]0, i=0..m-1 +if iopt=-1: + xb>> import numpy as np + >>> from scipy.interpolate import UnivariateSpline + >>> x, y = np.array([1, 2, 3, 4]), np.array([1, np.nan, 3, 4]) + >>> w = np.isnan(y) + >>> y[w] = 0. + >>> spl = UnivariateSpline(x, y, w=~w) + + Notice the need to replace a ``nan`` by a numerical value (precise value + does not matter as long as the corresponding weight is zero.) + + References + ---------- + Based on algorithms described in [1]_, [2]_, [3]_, and [4]_: + + .. [1] P. Dierckx, "An algorithm for smoothing, differentiation and + integration of experimental data using spline functions", + J.Comp.Appl.Maths 1 (1975) 165-184. + .. [2] P. Dierckx, "A fast algorithm for smoothing data on a rectangular + grid while using spline functions", SIAM J.Numer.Anal. 19 (1982) + 1286-1304. + .. [3] P. Dierckx, "An improved algorithm for curve fitting with spline + functions", report tw54, Dept. Computer Science,K.U. Leuven, 1981. + .. [4] P. Dierckx, "Curve and surface fitting with splines", Monographs on + Numerical Analysis, Oxford University Press, 1993. + + Examples + -------- + >>> import numpy as np + >>> import matplotlib.pyplot as plt + >>> from scipy.interpolate import UnivariateSpline + >>> rng = np.random.default_rng() + >>> x = np.linspace(-3, 3, 50) + >>> y = np.exp(-x**2) + 0.1 * rng.standard_normal(50) + >>> plt.plot(x, y, 'ro', ms=5) + + Use the default value for the smoothing parameter: + + >>> spl = UnivariateSpline(x, y) + >>> xs = np.linspace(-3, 3, 1000) + >>> plt.plot(xs, spl(xs), 'g', lw=3) + + Manually change the amount of smoothing: + + >>> spl.set_smoothing_factor(0.5) + >>> plt.plot(xs, spl(xs), 'b', lw=3) + >>> plt.show() + + """ + + def __init__(self, x, y, w=None, bbox=[None]*2, k=3, s=None, + ext=0, check_finite=False): + + x, y, w, bbox, self.ext = self.validate_input(x, y, w, bbox, k, s, ext, + check_finite) + + # _data == x,y,w,xb,xe,k,s,n,t,c,fp,fpint,nrdata,ier + with FITPACK_LOCK: + data = dfitpack.fpcurf0(x, y, k, w=w, xb=bbox[0], + xe=bbox[1], s=s) + if data[-1] == 1: + # nest too small, setting to maximum bound + data = self._reset_nest(data) + self._data = data + self._reset_class() + + @staticmethod + def validate_input(x, y, w, bbox, k, s, ext, check_finite): + x, y, bbox = np.asarray(x), np.asarray(y), np.asarray(bbox) + if w is not None: + w = np.asarray(w) + if check_finite: + w_finite = np.isfinite(w).all() if w is not None else True + if (not np.isfinite(x).all() or not np.isfinite(y).all() or + not w_finite): + raise ValueError("x and y array must not contain " + "NaNs or infs.") + if s is None or s > 0: + if not np.all(diff(x) >= 0.0): + raise ValueError("x must be increasing if s > 0") + else: + if not np.all(diff(x) > 0.0): + raise ValueError("x must be strictly increasing if s = 0") + if x.size != y.size: + raise ValueError("x and y should have a same length") + elif w is not None and not x.size == y.size == w.size: + raise ValueError("x, y, and w should have a same length") + elif bbox.shape != (2,): + raise ValueError("bbox shape should be (2,)") + elif not (1 <= k <= 5): + raise ValueError("k should be 1 <= k <= 5") + elif s is not None and not s >= 0.0: + raise ValueError("s should be s >= 0.0") + + try: + ext = _extrap_modes[ext] + except KeyError as e: + raise ValueError(f"Unknown extrapolation mode {ext}.") from e + + return x, y, w, bbox, ext + + @classmethod + def _from_tck(cls, tck, ext=0): + """Construct a spline object from given tck""" + self = cls.__new__(cls) + t, c, k = tck + self._eval_args = tck + # _data == x,y,w,xb,xe,k,s,n,t,c,fp,fpint,nrdata,ier + self._data = (None, None, None, None, None, k, None, len(t), t, + c, None, None, None, None) + self.ext = ext + return self + + def _reset_class(self): + data = self._data + n, t, c, k, ier = data[7], data[8], data[9], data[5], data[-1] + self._eval_args = t[:n], c[:n], k + if ier == 0: + # the spline returned has a residual sum of squares fp + # such that abs(fp-s)/s <= tol with tol a relative + # tolerance set to 0.001 by the program + pass + elif ier == -1: + # the spline returned is an interpolating spline + self._set_class(InterpolatedUnivariateSpline) + elif ier == -2: + # the spline returned is the weighted least-squares + # polynomial of degree k. In this extreme case fp gives + # the upper bound fp0 for the smoothing factor s. + self._set_class(LSQUnivariateSpline) + else: + # error + if ier == 1: + self._set_class(LSQUnivariateSpline) + message = _curfit_messages.get(ier, f'ier={ier}') + warnings.warn(message, stacklevel=3) + + def _set_class(self, cls): + self._spline_class = cls + if self.__class__ in (UnivariateSpline, InterpolatedUnivariateSpline, + LSQUnivariateSpline): + self.__class__ = cls + else: + # It's an unknown subclass -- don't change class. cf. #731 + pass + + def _reset_nest(self, data, nest=None): + n = data[10] + if nest is None: + k, m = data[5], len(data[0]) + nest = m+k+1 # this is the maximum bound for nest + else: + if not n <= nest: + raise ValueError("`nest` can only be increased") + t, c, fpint, nrdata = (np.resize(data[j], nest) for j in + [8, 9, 11, 12]) + + args = data[:8] + (t, c, n, fpint, nrdata, data[13]) + with FITPACK_LOCK: + data = dfitpack.fpcurf1(*args) + return data + + def set_smoothing_factor(self, s): + """ Continue spline computation with the given smoothing + factor s and with the knots found at the last call. + + This routine modifies the spline in place. + + """ + data = self._data + if data[6] == -1: + warnings.warn('smoothing factor unchanged for' + 'LSQ spline with fixed knots', + stacklevel=2) + return + args = data[:6] + (s,) + data[7:] + with FITPACK_LOCK: + data = dfitpack.fpcurf1(*args) + if data[-1] == 1: + # nest too small, setting to maximum bound + data = self._reset_nest(data) + self._data = data + self._reset_class() + + def __call__(self, x, nu=0, ext=None): + """ + Evaluate spline (or its nu-th derivative) at positions x. + + Parameters + ---------- + x : array_like + A 1-D array of points at which to return the value of the smoothed + spline or its derivatives. Note: `x` can be unordered but the + evaluation is more efficient if `x` is (partially) ordered. + nu : int + The order of derivative of the spline to compute. + ext : int + Controls the value returned for elements of `x` not in the + interval defined by the knot sequence. + + * if ext=0 or 'extrapolate', return the extrapolated value. + * if ext=1 or 'zeros', return 0 + * if ext=2 or 'raise', raise a ValueError + * if ext=3 or 'const', return the boundary value. + + The default value is 0, passed from the initialization of + UnivariateSpline. + + """ + x = np.asarray(x) + # empty input yields empty output + if x.size == 0: + return array([]) + if ext is None: + ext = self.ext + else: + try: + ext = _extrap_modes[ext] + except KeyError as e: + raise ValueError(f"Unknown extrapolation mode {ext}.") from e + with FITPACK_LOCK: + return _fitpack_impl.splev(x, self._eval_args, der=nu, ext=ext) + + def get_knots(self): + """ Return positions of interior knots of the spline. + + Internally, the knot vector contains ``2*k`` additional boundary knots. + """ + data = self._data + k, n = data[5], data[7] + return data[8][k:n-k] + + def get_coeffs(self): + """Return spline coefficients.""" + data = self._data + k, n = data[5], data[7] + return data[9][:n-k-1] + + def get_residual(self): + """Return weighted sum of squared residuals of the spline approximation. + + This is equivalent to:: + + sum((w[i] * (y[i]-spl(x[i])))**2, axis=0) + + """ + return self._data[10] + + def integral(self, a, b): + """ Return definite integral of the spline between two given points. + + Parameters + ---------- + a : float + Lower limit of integration. + b : float + Upper limit of integration. + + Returns + ------- + integral : float + The value of the definite integral of the spline between limits. + + Examples + -------- + >>> import numpy as np + >>> from scipy.interpolate import UnivariateSpline + >>> x = np.linspace(0, 3, 11) + >>> y = x**2 + >>> spl = UnivariateSpline(x, y) + >>> spl.integral(0, 3) + 9.0 + + which agrees with :math:`\\int x^2 dx = x^3 / 3` between the limits + of 0 and 3. + + A caveat is that this routine assumes the spline to be zero outside of + the data limits: + + >>> spl.integral(-1, 4) + 9.0 + >>> spl.integral(-1, 0) + 0.0 + + """ + with FITPACK_LOCK: + return _fitpack_impl.splint(a, b, self._eval_args) + + def derivatives(self, x): + """ Return all derivatives of the spline at the point x. + + Parameters + ---------- + x : float + The point to evaluate the derivatives at. + + Returns + ------- + der : ndarray, shape(k+1,) + Derivatives of the orders 0 to k. + + Examples + -------- + >>> import numpy as np + >>> from scipy.interpolate import UnivariateSpline + >>> x = np.linspace(0, 3, 11) + >>> y = x**2 + >>> spl = UnivariateSpline(x, y) + >>> spl.derivatives(1.5) + array([2.25, 3.0, 2.0, 0]) + + """ + with FITPACK_LOCK: + return _fitpack_impl.spalde(x, self._eval_args) + + def roots(self): + """ Return the zeros of the spline. + + Notes + ----- + Restriction: only cubic splines are supported by FITPACK. For non-cubic + splines, use `PPoly.root` (see below for an example). + + Examples + -------- + + For some data, this method may miss a root. This happens when one of + the spline knots (which FITPACK places automatically) happens to + coincide with the true root. A workaround is to convert to `PPoly`, + which uses a different root-finding algorithm. + + For example, + + >>> x = [1.96, 1.97, 1.98, 1.99, 2.00, 2.01, 2.02, 2.03, 2.04, 2.05] + >>> y = [-6.365470e-03, -4.790580e-03, -3.204320e-03, -1.607270e-03, + ... 4.440892e-16, 1.616930e-03, 3.243000e-03, 4.877670e-03, + ... 6.520430e-03, 8.170770e-03] + >>> from scipy.interpolate import UnivariateSpline + >>> spl = UnivariateSpline(x, y, s=0) + >>> spl.roots() + array([], dtype=float64) + + Converting to a PPoly object does find the roots at `x=2`: + + >>> from scipy.interpolate import splrep, PPoly + >>> tck = splrep(x, y, s=0) + >>> ppoly = PPoly.from_spline(tck) + >>> ppoly.roots(extrapolate=False) + array([2.]) + + See Also + -------- + sproot + PPoly.roots + + """ + k = self._data[5] + if k == 3: + t = self._eval_args[0] + mest = 3 * (len(t) - 7) + with FITPACK_LOCK: + return _fitpack_impl.sproot(self._eval_args, mest=mest) + raise NotImplementedError('finding roots unsupported for ' + 'non-cubic splines') + + def derivative(self, n=1): + """ + Construct a new spline representing the derivative of this spline. + + Parameters + ---------- + n : int, optional + Order of derivative to evaluate. Default: 1 + + Returns + ------- + spline : UnivariateSpline + Spline of order k2=k-n representing the derivative of this + spline. + + See Also + -------- + splder, antiderivative + + Notes + ----- + + .. versionadded:: 0.13.0 + + Examples + -------- + This can be used for finding maxima of a curve: + + >>> import numpy as np + >>> from scipy.interpolate import UnivariateSpline + >>> x = np.linspace(0, 10, 70) + >>> y = np.sin(x) + >>> spl = UnivariateSpline(x, y, k=4, s=0) + + Now, differentiate the spline and find the zeros of the + derivative. (NB: `sproot` only works for order 3 splines, so we + fit an order 4 spline): + + >>> spl.derivative().roots() / np.pi + array([ 0.50000001, 1.5 , 2.49999998]) + + This agrees well with roots :math:`\\pi/2 + n\\pi` of + :math:`\\cos(x) = \\sin'(x)`. + + """ + with FITPACK_LOCK: + tck = _fitpack_impl.splder(self._eval_args, n) + # if self.ext is 'const', derivative.ext will be 'zeros' + ext = 1 if self.ext == 3 else self.ext + return UnivariateSpline._from_tck(tck, ext=ext) + + def antiderivative(self, n=1): + """ + Construct a new spline representing the antiderivative of this spline. + + Parameters + ---------- + n : int, optional + Order of antiderivative to evaluate. Default: 1 + + Returns + ------- + spline : UnivariateSpline + Spline of order k2=k+n representing the antiderivative of this + spline. + + Notes + ----- + + .. versionadded:: 0.13.0 + + See Also + -------- + splantider, derivative + + Examples + -------- + >>> import numpy as np + >>> from scipy.interpolate import UnivariateSpline + >>> x = np.linspace(0, np.pi/2, 70) + >>> y = 1 / np.sqrt(1 - 0.8*np.sin(x)**2) + >>> spl = UnivariateSpline(x, y, s=0) + + The derivative is the inverse operation of the antiderivative, + although some floating point error accumulates: + + >>> spl(1.7), spl.antiderivative().derivative()(1.7) + (array(2.1565429877197317), array(2.1565429877201865)) + + Antiderivative can be used to evaluate definite integrals: + + >>> ispl = spl.antiderivative() + >>> ispl(np.pi/2) - ispl(0) + 2.2572053588768486 + + This is indeed an approximation to the complete elliptic integral + :math:`K(m) = \\int_0^{\\pi/2} [1 - m\\sin^2 x]^{-1/2} dx`: + + >>> from scipy.special import ellipk + >>> ellipk(0.8) + 2.2572053268208538 + + """ + with FITPACK_LOCK: + tck = _fitpack_impl.splantider(self._eval_args, n) + return UnivariateSpline._from_tck(tck, self.ext) + + +class InterpolatedUnivariateSpline(UnivariateSpline): + """ + 1-D interpolating spline for a given set of data points. + + .. legacy:: class + + Specifically, we recommend using `make_interp_spline` instead. + + Fits a spline y = spl(x) of degree `k` to the provided `x`, `y` data. + Spline function passes through all provided points. Equivalent to + `UnivariateSpline` with `s` = 0. + + Parameters + ---------- + x : (N,) array_like + Input dimension of data points -- must be strictly increasing + y : (N,) array_like + input dimension of data points + w : (N,) array_like, optional + Weights for spline fitting. Must be positive. If None (default), + weights are all 1. + bbox : (2,) array_like, optional + 2-sequence specifying the boundary of the approximation interval. If + None (default), ``bbox=[x[0], x[-1]]``. + k : int, optional + Degree of the smoothing spline. Must be ``1 <= k <= 5``. Default is + ``k = 3``, a cubic spline. + ext : int or str, optional + Controls the extrapolation mode for elements + not in the interval defined by the knot sequence. + + * if ext=0 or 'extrapolate', return the extrapolated value. + * if ext=1 or 'zeros', return 0 + * if ext=2 or 'raise', raise a ValueError + * if ext=3 of 'const', return the boundary value. + + The default value is 0. + + check_finite : bool, optional + Whether to check that the input arrays contain only finite numbers. + Disabling may give a performance gain, but may result in problems + (crashes, non-termination or non-sensical results) if the inputs + do contain infinities or NaNs. + Default is False. + + See Also + -------- + UnivariateSpline : + a smooth univariate spline to fit a given set of data points. + LSQUnivariateSpline : + a spline for which knots are user-selected + SmoothBivariateSpline : + a smoothing bivariate spline through the given points + LSQBivariateSpline : + a bivariate spline using weighted least-squares fitting + splrep : + a function to find the B-spline representation of a 1-D curve + splev : + a function to evaluate a B-spline or its derivatives + sproot : + a function to find the roots of a cubic B-spline + splint : + a function to evaluate the definite integral of a B-spline between two + given points + spalde : + a function to evaluate all derivatives of a B-spline + + Notes + ----- + The number of data points must be larger than the spline degree `k`. + + Examples + -------- + >>> import numpy as np + >>> import matplotlib.pyplot as plt + >>> from scipy.interpolate import InterpolatedUnivariateSpline + >>> rng = np.random.default_rng() + >>> x = np.linspace(-3, 3, 50) + >>> y = np.exp(-x**2) + 0.1 * rng.standard_normal(50) + >>> spl = InterpolatedUnivariateSpline(x, y) + >>> plt.plot(x, y, 'ro', ms=5) + >>> xs = np.linspace(-3, 3, 1000) + >>> plt.plot(xs, spl(xs), 'g', lw=3, alpha=0.7) + >>> plt.show() + + Notice that the ``spl(x)`` interpolates `y`: + + >>> spl.get_residual() + 0.0 + + """ + + def __init__(self, x, y, w=None, bbox=[None]*2, k=3, + ext=0, check_finite=False): + + x, y, w, bbox, self.ext = self.validate_input(x, y, w, bbox, k, None, + ext, check_finite) + if not np.all(diff(x) > 0.0): + raise ValueError('x must be strictly increasing') + + # _data == x,y,w,xb,xe,k,s,n,t,c,fp,fpint,nrdata,ier + with FITPACK_LOCK: + self._data = dfitpack.fpcurf0(x, y, k, w=w, xb=bbox[0], + xe=bbox[1], s=0) + self._reset_class() + + +_fpchec_error_string = """The input parameters have been rejected by fpchec. \ +This means that at least one of the following conditions is violated: + +1) k+1 <= n-k-1 <= m +2) t(1) <= t(2) <= ... <= t(k+1) + t(n-k) <= t(n-k+1) <= ... <= t(n) +3) t(k+1) < t(k+2) < ... < t(n-k) +4) t(k+1) <= x(i) <= t(n-k) +5) The conditions specified by Schoenberg and Whitney must hold + for at least one subset of data points, i.e., there must be a + subset of data points y(j) such that + t(j) < y(j) < t(j+k+1), j=1,2,...,n-k-1 +""" + + +class LSQUnivariateSpline(UnivariateSpline): + """ + 1-D spline with explicit internal knots. + + .. legacy:: class + + Specifically, we recommend using `make_lsq_spline` instead. + + + Fits a spline y = spl(x) of degree `k` to the provided `x`, `y` data. `t` + specifies the internal knots of the spline + + Parameters + ---------- + x : (N,) array_like + Input dimension of data points -- must be increasing + y : (N,) array_like + Input dimension of data points + t : (M,) array_like + interior knots of the spline. Must be in ascending order and:: + + bbox[0] < t[0] < ... < t[-1] < bbox[-1] + + w : (N,) array_like, optional + weights for spline fitting. Must be positive. If None (default), + weights are all 1. + bbox : (2,) array_like, optional + 2-sequence specifying the boundary of the approximation interval. If + None (default), ``bbox = [x[0], x[-1]]``. + k : int, optional + Degree of the smoothing spline. Must be 1 <= `k` <= 5. + Default is `k` = 3, a cubic spline. + ext : int or str, optional + Controls the extrapolation mode for elements + not in the interval defined by the knot sequence. + + * if ext=0 or 'extrapolate', return the extrapolated value. + * if ext=1 or 'zeros', return 0 + * if ext=2 or 'raise', raise a ValueError + * if ext=3 of 'const', return the boundary value. + + The default value is 0. + + check_finite : bool, optional + Whether to check that the input arrays contain only finite numbers. + Disabling may give a performance gain, but may result in problems + (crashes, non-termination or non-sensical results) if the inputs + do contain infinities or NaNs. + Default is False. + + Raises + ------ + ValueError + If the interior knots do not satisfy the Schoenberg-Whitney conditions + + See Also + -------- + UnivariateSpline : + a smooth univariate spline to fit a given set of data points. + InterpolatedUnivariateSpline : + a interpolating univariate spline for a given set of data points. + splrep : + a function to find the B-spline representation of a 1-D curve + splev : + a function to evaluate a B-spline or its derivatives + sproot : + a function to find the roots of a cubic B-spline + splint : + a function to evaluate the definite integral of a B-spline between two + given points + spalde : + a function to evaluate all derivatives of a B-spline + + Notes + ----- + The number of data points must be larger than the spline degree `k`. + + Knots `t` must satisfy the Schoenberg-Whitney conditions, + i.e., there must be a subset of data points ``x[j]`` such that + ``t[j] < x[j] < t[j+k+1]``, for ``j=0, 1,...,n-k-2``. + + Examples + -------- + >>> import numpy as np + >>> from scipy.interpolate import LSQUnivariateSpline, UnivariateSpline + >>> import matplotlib.pyplot as plt + >>> rng = np.random.default_rng() + >>> x = np.linspace(-3, 3, 50) + >>> y = np.exp(-x**2) + 0.1 * rng.standard_normal(50) + + Fit a smoothing spline with a pre-defined internal knots: + + >>> t = [-1, 0, 1] + >>> spl = LSQUnivariateSpline(x, y, t) + + >>> xs = np.linspace(-3, 3, 1000) + >>> plt.plot(x, y, 'ro', ms=5) + >>> plt.plot(xs, spl(xs), 'g-', lw=3) + >>> plt.show() + + Check the knot vector: + + >>> spl.get_knots() + array([-3., -1., 0., 1., 3.]) + + Constructing lsq spline using the knots from another spline: + + >>> x = np.arange(10) + >>> s = UnivariateSpline(x, x, s=0) + >>> s.get_knots() + array([ 0., 2., 3., 4., 5., 6., 7., 9.]) + >>> knt = s.get_knots() + >>> s1 = LSQUnivariateSpline(x, x, knt[1:-1]) # Chop 1st and last knot + >>> s1.get_knots() + array([ 0., 2., 3., 4., 5., 6., 7., 9.]) + + """ + + def __init__(self, x, y, t, w=None, bbox=[None]*2, k=3, + ext=0, check_finite=False): + + x, y, w, bbox, self.ext = self.validate_input(x, y, w, bbox, k, None, + ext, check_finite) + if not np.all(diff(x) >= 0.0): + raise ValueError('x must be increasing') + + # _data == x,y,w,xb,xe,k,s,n,t,c,fp,fpint,nrdata,ier + xb = bbox[0] + xe = bbox[1] + if xb is None: + xb = x[0] + if xe is None: + xe = x[-1] + t = concatenate(([xb]*(k+1), t, [xe]*(k+1))) + n = len(t) + if not np.all(t[k+1:n-k]-t[k:n-k-1] > 0, axis=0): + raise ValueError('Interior knots t must satisfy ' + 'Schoenberg-Whitney conditions') + with FITPACK_LOCK: + if not dfitpack.fpchec(x, t, k) == 0: + raise ValueError(_fpchec_error_string) + data = dfitpack.fpcurfm1(x, y, k, t, w=w, xb=xb, xe=xe) + self._data = data[:-3] + (None, None, data[-1]) + self._reset_class() + + +# ############### Bivariate spline #################### + +class _BivariateSplineBase: + """ Base class for Bivariate spline s(x,y) interpolation on the rectangle + [xb,xe] x [yb, ye] calculated from a given set of data points + (x,y,z). + + See Also + -------- + bisplrep : + a function to find a bivariate B-spline representation of a surface + bisplev : + a function to evaluate a bivariate B-spline and its derivatives + BivariateSpline : + a base class for bivariate splines. + SphereBivariateSpline : + a bivariate spline on a spherical grid + """ + + @classmethod + def _from_tck(cls, tck): + """Construct a spline object from given tck and degree""" + self = cls.__new__(cls) + if len(tck) != 5: + raise ValueError("tck should be a 5 element tuple of tx," + " ty, c, kx, ky") + self.tck = tck[:3] + self.degrees = tck[3:] + return self + + def get_residual(self): + """ Return weighted sum of squared residuals of the spline + approximation: sum ((w[i]*(z[i]-s(x[i],y[i])))**2,axis=0) + """ + return self.fp + + def get_knots(self): + """ Return a tuple (tx,ty) where tx,ty contain knots positions + of the spline with respect to x-, y-variable, respectively. + The position of interior and additional knots are given as + t[k+1:-k-1] and t[:k+1]=b, t[-k-1:]=e, respectively. + """ + return self.tck[:2] + + def get_coeffs(self): + """ Return spline coefficients.""" + return self.tck[2] + + def __call__(self, x, y, dx=0, dy=0, grid=True): + """ + Evaluate the spline or its derivatives at given positions. + + Parameters + ---------- + x, y : array_like + Input coordinates. + + If `grid` is False, evaluate the spline at points ``(x[i], + y[i]), i=0, ..., len(x)-1``. Standard Numpy broadcasting + is obeyed. + + If `grid` is True: evaluate spline at the grid points + defined by the coordinate arrays x, y. The arrays must be + sorted to increasing order. + + The ordering of axes is consistent with + ``np.meshgrid(..., indexing="ij")`` and inconsistent with the + default ordering ``np.meshgrid(..., indexing="xy")``. + dx : int + Order of x-derivative + + .. versionadded:: 0.14.0 + dy : int + Order of y-derivative + + .. versionadded:: 0.14.0 + grid : bool + Whether to evaluate the results on a grid spanned by the + input arrays, or at points specified by the input arrays. + + .. versionadded:: 0.14.0 + + Examples + -------- + Suppose that we want to bilinearly interpolate an exponentially decaying + function in 2 dimensions. + + >>> import numpy as np + >>> from scipy.interpolate import RectBivariateSpline + + We sample the function on a coarse grid. Note that the default indexing="xy" + of meshgrid would result in an unexpected (transposed) result after + interpolation. + + >>> xarr = np.linspace(-3, 3, 100) + >>> yarr = np.linspace(-3, 3, 100) + >>> xgrid, ygrid = np.meshgrid(xarr, yarr, indexing="ij") + + The function to interpolate decays faster along one axis than the other. + + >>> zdata = np.exp(-np.sqrt((xgrid / 2) ** 2 + ygrid**2)) + + Next we sample on a finer grid using interpolation (kx=ky=1 for bilinear). + + >>> rbs = RectBivariateSpline(xarr, yarr, zdata, kx=1, ky=1) + >>> xarr_fine = np.linspace(-3, 3, 200) + >>> yarr_fine = np.linspace(-3, 3, 200) + >>> xgrid_fine, ygrid_fine = np.meshgrid(xarr_fine, yarr_fine, indexing="ij") + >>> zdata_interp = rbs(xgrid_fine, ygrid_fine, grid=False) + + And check that the result agrees with the input by plotting both. + + >>> import matplotlib.pyplot as plt + >>> fig = plt.figure() + >>> ax1 = fig.add_subplot(1, 2, 1, aspect="equal") + >>> ax2 = fig.add_subplot(1, 2, 2, aspect="equal") + >>> ax1.imshow(zdata) + >>> ax2.imshow(zdata_interp) + >>> plt.show() + """ + x = np.asarray(x) + y = np.asarray(y) + + tx, ty, c = self.tck[:3] + kx, ky = self.degrees + if grid: + if x.size == 0 or y.size == 0: + return np.zeros((x.size, y.size), dtype=self.tck[2].dtype) + + if (x.size >= 2) and (not np.all(np.diff(x) >= 0.0)): + raise ValueError("x must be strictly increasing when `grid` is True") + if (y.size >= 2) and (not np.all(np.diff(y) >= 0.0)): + raise ValueError("y must be strictly increasing when `grid` is True") + + if dx or dy: + with FITPACK_LOCK: + z, ier = dfitpack.parder(tx, ty, c, kx, ky, dx, dy, x, y) + if not ier == 0: + raise ValueError(f"Error code returned by parder: {ier}") + else: + with FITPACK_LOCK: + z, ier = dfitpack.bispev(tx, ty, c, kx, ky, x, y) + if not ier == 0: + raise ValueError(f"Error code returned by bispev: {ier}") + else: + # standard Numpy broadcasting + if x.shape != y.shape: + x, y = np.broadcast_arrays(x, y) + + shape = x.shape + x = x.ravel() + y = y.ravel() + + if x.size == 0 or y.size == 0: + return np.zeros(shape, dtype=self.tck[2].dtype) + + if dx or dy: + with FITPACK_LOCK: + z, ier = dfitpack.pardeu(tx, ty, c, kx, ky, dx, dy, x, y) + if not ier == 0: + raise ValueError(f"Error code returned by pardeu: {ier}") + else: + with FITPACK_LOCK: + z, ier = dfitpack.bispeu(tx, ty, c, kx, ky, x, y) + if not ier == 0: + raise ValueError(f"Error code returned by bispeu: {ier}") + + z = z.reshape(shape) + return z + + def partial_derivative(self, dx, dy): + """Construct a new spline representing a partial derivative of this + spline. + + Parameters + ---------- + dx, dy : int + Orders of the derivative in x and y respectively. They must be + non-negative integers and less than the respective degree of the + original spline (self) in that direction (``kx``, ``ky``). + + Returns + ------- + spline : + A new spline of degrees (``kx - dx``, ``ky - dy``) representing the + derivative of this spline. + + Notes + ----- + + .. versionadded:: 1.9.0 + + """ + if dx == 0 and dy == 0: + return self + else: + kx, ky = self.degrees + if not (dx >= 0 and dy >= 0): + raise ValueError("order of derivative must be positive or" + " zero") + if not (dx < kx and dy < ky): + raise ValueError("order of derivative must be less than" + " degree of spline") + tx, ty, c = self.tck[:3] + with FITPACK_LOCK: + newc, ier = dfitpack.pardtc(tx, ty, c, kx, ky, dx, dy) + if ier != 0: + # This should not happen under normal conditions. + raise ValueError("Unexpected error code returned by" + " pardtc: %d" % ier) + nx = len(tx) + ny = len(ty) + newtx = tx[dx:nx - dx] + newty = ty[dy:ny - dy] + newkx, newky = kx - dx, ky - dy + newclen = (nx - dx - kx - 1) * (ny - dy - ky - 1) + return _DerivedBivariateSpline._from_tck((newtx, newty, + newc[:newclen], + newkx, newky)) + + +_surfit_messages = {1: """ +The required storage space exceeds the available storage space: nxest +or nyest too small, or s too small. +The weighted least-squares spline corresponds to the current set of +knots.""", + 2: """ +A theoretically impossible result was found during the iteration +process for finding a smoothing spline with fp = s: s too small or +badly chosen eps. +Weighted sum of squared residuals does not satisfy abs(fp-s)/s < tol.""", + 3: """ +the maximal number of iterations maxit (set to 20 by the program) +allowed for finding a smoothing spline with fp=s has been reached: +s too small. +Weighted sum of squared residuals does not satisfy abs(fp-s)/s < tol.""", + 4: """ +No more knots can be added because the number of b-spline coefficients +(nx-kx-1)*(ny-ky-1) already exceeds the number of data points m: +either s or m too small. +The weighted least-squares spline corresponds to the current set of +knots.""", + 5: """ +No more knots can be added because the additional knot would (quasi) +coincide with an old one: s too small or too large a weight to an +inaccurate data point. +The weighted least-squares spline corresponds to the current set of +knots.""", + 10: """ +Error on entry, no approximation returned. The following conditions +must hold: +xb<=x[i]<=xe, yb<=y[i]<=ye, w[i]>0, i=0..m-1 +If iopt==-1, then + xb>> import numpy as np + >>> from scipy.interpolate import RectBivariateSpline + >>> def f(x, y): + ... return np.exp(-np.sqrt((x / 2) ** 2 + y**2)) + + We sample the function on a coarse grid and set up the interpolator. Note that + the default ``indexing="xy"`` of meshgrid would result in an unexpected + (transposed) result after interpolation. + + >>> xarr = np.linspace(-3, 3, 21) + >>> yarr = np.linspace(-3, 3, 21) + >>> xgrid, ygrid = np.meshgrid(xarr, yarr, indexing="ij") + >>> zdata = f(xgrid, ygrid) + >>> rbs = RectBivariateSpline(xarr, yarr, zdata, kx=1, ky=1) + + Next we sample the function along a diagonal slice through the coordinate space + on a finer grid using interpolation. + + >>> xinterp = np.linspace(-3, 3, 201) + >>> yinterp = np.linspace(3, -3, 201) + >>> zinterp = rbs.ev(xinterp, yinterp) + + And check that the interpolation passes through the function evaluations as a + function of the distance from the origin along the slice. + + >>> import matplotlib.pyplot as plt + >>> fig = plt.figure() + >>> ax1 = fig.add_subplot(1, 1, 1) + >>> ax1.plot(np.sqrt(xarr**2 + yarr**2), np.diag(zdata), "or") + >>> ax1.plot(np.sqrt(xinterp**2 + yinterp**2), zinterp, "-b") + >>> plt.show() + """ + return self.__call__(xi, yi, dx=dx, dy=dy, grid=False) + + def integral(self, xa, xb, ya, yb): + """ + Evaluate the integral of the spline over area [xa,xb] x [ya,yb]. + + Parameters + ---------- + xa, xb : float + The end-points of the x integration interval. + ya, yb : float + The end-points of the y integration interval. + + Returns + ------- + integ : float + The value of the resulting integral. + + """ + tx, ty, c = self.tck[:3] + kx, ky = self.degrees + with FITPACK_LOCK: + return dfitpack.dblint(tx, ty, c, kx, ky, xa, xb, ya, yb) + + @staticmethod + def _validate_input(x, y, z, w, kx, ky, eps): + x, y, z = np.asarray(x), np.asarray(y), np.asarray(z) + if not x.size == y.size == z.size: + raise ValueError('x, y, and z should have a same length') + + if w is not None: + w = np.asarray(w) + if x.size != w.size: + raise ValueError('x, y, z, and w should have a same length') + elif not np.all(w >= 0.0): + raise ValueError('w should be positive') + if (eps is not None) and (not 0.0 < eps < 1.0): + raise ValueError('eps should be between (0, 1)') + if not x.size >= (kx + 1) * (ky + 1): + raise ValueError('The length of x, y and z should be at least' + ' (kx+1) * (ky+1)') + return x, y, z, w + + +class _DerivedBivariateSpline(_BivariateSplineBase): + """Bivariate spline constructed from the coefficients and knots of another + spline. + + Notes + ----- + The class is not meant to be instantiated directly from the data to be + interpolated or smoothed. As a result, its ``fp`` attribute and + ``get_residual`` method are inherited but overridden; ``AttributeError`` is + raised when they are accessed. + + The other inherited attributes can be used as usual. + """ + _invalid_why = ("is unavailable, because _DerivedBivariateSpline" + " instance is not constructed from data that are to be" + " interpolated or smoothed, but derived from the" + " underlying knots and coefficients of another spline" + " object") + + @property + def fp(self): + raise AttributeError(f"attribute \"fp\" {self._invalid_why}") + + def get_residual(self): + raise AttributeError(f"method \"get_residual\" {self._invalid_why}") + + +class SmoothBivariateSpline(BivariateSpline): + """ + Smooth bivariate spline approximation. + + Parameters + ---------- + x, y, z : array_like + 1-D sequences of data points (order is not important). + w : array_like, optional + Positive 1-D sequence of weights, of same length as `x`, `y` and `z`. + bbox : array_like, optional + Sequence of length 4 specifying the boundary of the rectangular + approximation domain. By default, + ``bbox=[min(x), max(x), min(y), max(y)]``. + kx, ky : ints, optional + Degrees of the bivariate spline. Default is 3. + s : float, optional + Positive smoothing factor defined for estimation condition: + ``sum((w[i]*(z[i]-s(x[i], y[i])))**2, axis=0) <= s`` + Default ``s=len(w)`` which should be a good value if ``1/w[i]`` is an + estimate of the standard deviation of ``z[i]``. + eps : float, optional + A threshold for determining the effective rank of an over-determined + linear system of equations. `eps` should have a value within the open + interval ``(0, 1)``, the default is 1e-16. + + See Also + -------- + BivariateSpline : + a base class for bivariate splines. + UnivariateSpline : + a smooth univariate spline to fit a given set of data points. + LSQBivariateSpline : + a bivariate spline using weighted least-squares fitting + RectSphereBivariateSpline : + a bivariate spline over a rectangular mesh on a sphere + SmoothSphereBivariateSpline : + a smoothing bivariate spline in spherical coordinates + LSQSphereBivariateSpline : + a bivariate spline in spherical coordinates using weighted + least-squares fitting + RectBivariateSpline : + a bivariate spline over a rectangular mesh + bisplrep : + a function to find a bivariate B-spline representation of a surface + bisplev : + a function to evaluate a bivariate B-spline and its derivatives + + Notes + ----- + The length of `x`, `y` and `z` should be at least ``(kx+1) * (ky+1)``. + + If the input data is such that input dimensions have incommensurate + units and differ by many orders of magnitude, the interpolant may have + numerical artifacts. Consider rescaling the data before interpolating. + + This routine constructs spline knot vectors automatically via the FITPACK + algorithm. The spline knots may be placed away from the data points. For + some data sets, this routine may fail to construct an interpolating spline, + even if one is requested via ``s=0`` parameter. In such situations, it is + recommended to use `bisplrep` / `bisplev` directly instead of this routine + and, if needed, increase the values of ``nxest`` and ``nyest`` parameters + of `bisplrep`. + + For linear interpolation, prefer `LinearNDInterpolator`. + See ``https://gist.github.com/ev-br/8544371b40f414b7eaf3fe6217209bff`` + for discussion. + + """ + + def __init__(self, x, y, z, w=None, bbox=[None] * 4, kx=3, ky=3, s=None, + eps=1e-16): + + x, y, z, w = self._validate_input(x, y, z, w, kx, ky, eps) + bbox = ravel(bbox) + if not bbox.shape == (4,): + raise ValueError('bbox shape should be (4,)') + if s is not None and not s >= 0.0: + raise ValueError("s should be s >= 0.0") + + xb, xe, yb, ye = bbox + with FITPACK_LOCK: + nx, tx, ny, ty, c, fp, wrk1, ier = dfitpack.surfit_smth( + x, y, z, w, xb, xe, yb, ye, kx, ky, s=s, eps=eps, lwrk2=1) + if ier > 10: # lwrk2 was to small, re-run + nx, tx, ny, ty, c, fp, wrk1, ier = dfitpack.surfit_smth( + x, y, z, w, xb, xe, yb, ye, kx, ky, s=s, eps=eps, + lwrk2=ier) + if ier in [0, -1, -2]: # normal return + pass + else: + message = _surfit_messages.get(ier, f'ier={ier}') + warnings.warn(message, stacklevel=2) + + self.fp = fp + self.tck = tx[:nx], ty[:ny], c[:(nx-kx-1)*(ny-ky-1)] + self.degrees = kx, ky + + +class LSQBivariateSpline(BivariateSpline): + """ + Weighted least-squares bivariate spline approximation. + + Parameters + ---------- + x, y, z : array_like + 1-D sequences of data points (order is not important). + tx, ty : array_like + Strictly ordered 1-D sequences of knots coordinates. + w : array_like, optional + Positive 1-D array of weights, of the same length as `x`, `y` and `z`. + bbox : (4,) array_like, optional + Sequence of length 4 specifying the boundary of the rectangular + approximation domain. By default, + ``bbox=[min(x,tx),max(x,tx), min(y,ty),max(y,ty)]``. + kx, ky : ints, optional + Degrees of the bivariate spline. Default is 3. + eps : float, optional + A threshold for determining the effective rank of an over-determined + linear system of equations. `eps` should have a value within the open + interval ``(0, 1)``, the default is 1e-16. + + See Also + -------- + BivariateSpline : + a base class for bivariate splines. + UnivariateSpline : + a smooth univariate spline to fit a given set of data points. + SmoothBivariateSpline : + a smoothing bivariate spline through the given points + RectSphereBivariateSpline : + a bivariate spline over a rectangular mesh on a sphere + SmoothSphereBivariateSpline : + a smoothing bivariate spline in spherical coordinates + LSQSphereBivariateSpline : + a bivariate spline in spherical coordinates using weighted + least-squares fitting + RectBivariateSpline : + a bivariate spline over a rectangular mesh. + bisplrep : + a function to find a bivariate B-spline representation of a surface + bisplev : + a function to evaluate a bivariate B-spline and its derivatives + + Notes + ----- + The length of `x`, `y` and `z` should be at least ``(kx+1) * (ky+1)``. + + If the input data is such that input dimensions have incommensurate + units and differ by many orders of magnitude, the interpolant may have + numerical artifacts. Consider rescaling the data before interpolating. + + """ + + def __init__(self, x, y, z, tx, ty, w=None, bbox=[None]*4, kx=3, ky=3, + eps=None): + + x, y, z, w = self._validate_input(x, y, z, w, kx, ky, eps) + bbox = ravel(bbox) + if not bbox.shape == (4,): + raise ValueError('bbox shape should be (4,)') + + nx = 2*kx+2+len(tx) + ny = 2*ky+2+len(ty) + # The Fortran subroutine "surfit" (called as dfitpack.surfit_lsq) + # requires that the knot arrays passed as input should be "real + # array(s) of dimension nmax" where "nmax" refers to the greater of nx + # and ny. We pad the tx1/ty1 arrays here so that this is satisfied, and + # slice them to the desired sizes upon return. + nmax = max(nx, ny) + tx1 = zeros((nmax,), float) + ty1 = zeros((nmax,), float) + tx1[kx+1:nx-kx-1] = tx + ty1[ky+1:ny-ky-1] = ty + + xb, xe, yb, ye = bbox + with FITPACK_LOCK: + tx1, ty1, c, fp, ier = dfitpack.surfit_lsq(x, y, z, nx, tx1, ny, ty1, + w, xb, xe, yb, ye, + kx, ky, eps, lwrk2=1) + if ier > 10: + tx1, ty1, c, fp, ier = dfitpack.surfit_lsq(x, y, z, + nx, tx1, ny, ty1, w, + xb, xe, yb, ye, + kx, ky, eps, lwrk2=ier) + if ier in [0, -1, -2]: # normal return + pass + else: + if ier < -2: + deficiency = (nx-kx-1)*(ny-ky-1)+ier + message = _surfit_messages.get(-3) % (deficiency) + else: + message = _surfit_messages.get(ier, f'ier={ier}') + warnings.warn(message, stacklevel=2) + self.fp = fp + self.tck = tx1[:nx], ty1[:ny], c + self.degrees = kx, ky + + +class RectBivariateSpline(BivariateSpline): + """ + Bivariate spline approximation over a rectangular mesh. + + Can be used for both smoothing and interpolating data. + + Parameters + ---------- + x,y : array_like + 1-D arrays of coordinates in strictly ascending order. + Evaluated points outside the data range will be extrapolated. + z : array_like + 2-D array of data with shape (x.size,y.size). + bbox : array_like, optional + Sequence of length 4 specifying the boundary of the rectangular + approximation domain, which means the start and end spline knots of + each dimension are set by these values. By default, + ``bbox=[min(x), max(x), min(y), max(y)]``. + kx, ky : ints, optional + Degrees of the bivariate spline. Default is 3. + s : float, optional + Positive smoothing factor defined for estimation condition: + ``sum((z[i]-f(x[i], y[i]))**2, axis=0) <= s`` where f is a spline + function. Default is ``s=0``, which is for interpolation. + + See Also + -------- + BivariateSpline : + a base class for bivariate splines. + UnivariateSpline : + a smooth univariate spline to fit a given set of data points. + SmoothBivariateSpline : + a smoothing bivariate spline through the given points + LSQBivariateSpline : + a bivariate spline using weighted least-squares fitting + RectSphereBivariateSpline : + a bivariate spline over a rectangular mesh on a sphere + SmoothSphereBivariateSpline : + a smoothing bivariate spline in spherical coordinates + LSQSphereBivariateSpline : + a bivariate spline in spherical coordinates using weighted + least-squares fitting + bisplrep : + a function to find a bivariate B-spline representation of a surface + bisplev : + a function to evaluate a bivariate B-spline and its derivatives + + Notes + ----- + + If the input data is such that input dimensions have incommensurate + units and differ by many orders of magnitude, the interpolant may have + numerical artifacts. Consider rescaling the data before interpolating. + + """ + + def __init__(self, x, y, z, bbox=[None] * 4, kx=3, ky=3, s=0): + x, y, bbox = ravel(x), ravel(y), ravel(bbox) + z = np.asarray(z) + if not np.all(diff(x) > 0.0): + raise ValueError('x must be strictly increasing') + if not np.all(diff(y) > 0.0): + raise ValueError('y must be strictly increasing') + if not x.size == z.shape[0]: + raise ValueError('x dimension of z must have same number of ' + 'elements as x') + if not y.size == z.shape[1]: + raise ValueError('y dimension of z must have same number of ' + 'elements as y') + if not bbox.shape == (4,): + raise ValueError('bbox shape should be (4,)') + if s is not None and not s >= 0.0: + raise ValueError("s should be s >= 0.0") + + z = ravel(z) + xb, xe, yb, ye = bbox + with FITPACK_LOCK: + nx, tx, ny, ty, c, fp, ier = dfitpack.regrid_smth(x, y, z, xb, xe, yb, + ye, kx, ky, s) + + if ier not in [0, -1, -2]: + msg = _surfit_messages.get(ier, f'ier={ier}') + raise ValueError(msg) + + self.fp = fp + self.tck = tx[:nx], ty[:ny], c[:(nx - kx - 1) * (ny - ky - 1)] + self.degrees = kx, ky + + +_spherefit_messages = _surfit_messages.copy() +_spherefit_messages[10] = """ +ERROR. On entry, the input data are controlled on validity. The following + restrictions must be satisfied: + -1<=iopt<=1, m>=2, ntest>=8 ,npest >=8, 00, i=1,...,m + lwrk1 >= 185+52*v+10*u+14*u*v+8*(u-1)*v**2+8*m + kwrk >= m+(ntest-7)*(npest-7) + if iopt=-1: 8<=nt<=ntest , 9<=np<=npest + 0=0: s>=0 + if one of these conditions is found to be violated,control + is immediately repassed to the calling program. in that + case there is no approximation returned.""" +_spherefit_messages[-3] = """ +WARNING. The coefficients of the spline returned have been computed as the + minimal norm least-squares solution of a (numerically) rank + deficient system (deficiency=%i, rank=%i). Especially if the rank + deficiency, which is computed by 6+(nt-8)*(np-7)+ier, is large, + the results may be inaccurate. They could also seriously depend on + the value of eps.""" + + +class SphereBivariateSpline(_BivariateSplineBase): + """ + Bivariate spline s(x,y) of degrees 3 on a sphere, calculated from a + given set of data points (theta,phi,r). + + .. versionadded:: 0.11.0 + + See Also + -------- + bisplrep : + a function to find a bivariate B-spline representation of a surface + bisplev : + a function to evaluate a bivariate B-spline and its derivatives + UnivariateSpline : + a smooth univariate spline to fit a given set of data points. + SmoothBivariateSpline : + a smoothing bivariate spline through the given points + LSQUnivariateSpline : + a univariate spline using weighted least-squares fitting + """ + + def __call__(self, theta, phi, dtheta=0, dphi=0, grid=True): + """ + Evaluate the spline or its derivatives at given positions. + + Parameters + ---------- + theta, phi : array_like + Input coordinates. + + If `grid` is False, evaluate the spline at points + ``(theta[i], phi[i]), i=0, ..., len(x)-1``. Standard + Numpy broadcasting is obeyed. + + If `grid` is True: evaluate spline at the grid points + defined by the coordinate arrays theta, phi. The arrays + must be sorted to increasing order. + The ordering of axes is consistent with + ``np.meshgrid(..., indexing="ij")`` and inconsistent with the + default ordering ``np.meshgrid(..., indexing="xy")``. + dtheta : int, optional + Order of theta-derivative + + .. versionadded:: 0.14.0 + dphi : int + Order of phi-derivative + + .. versionadded:: 0.14.0 + grid : bool + Whether to evaluate the results on a grid spanned by the + input arrays, or at points specified by the input arrays. + + .. versionadded:: 0.14.0 + + Examples + -------- + + Suppose that we want to use splines to interpolate a bivariate function on a + sphere. The value of the function is known on a grid of longitudes and + colatitudes. + + >>> import numpy as np + >>> from scipy.interpolate import RectSphereBivariateSpline + >>> def f(theta, phi): + ... return np.sin(theta) * np.cos(phi) + + We evaluate the function on the grid. Note that the default indexing="xy" + of meshgrid would result in an unexpected (transposed) result after + interpolation. + + >>> thetaarr = np.linspace(0, np.pi, 22)[1:-1] + >>> phiarr = np.linspace(0, 2 * np.pi, 21)[:-1] + >>> thetagrid, phigrid = np.meshgrid(thetaarr, phiarr, indexing="ij") + >>> zdata = f(thetagrid, phigrid) + + We next set up the interpolator and use it to evaluate the function + on a finer grid. + + >>> rsbs = RectSphereBivariateSpline(thetaarr, phiarr, zdata) + >>> thetaarr_fine = np.linspace(0, np.pi, 200) + >>> phiarr_fine = np.linspace(0, 2 * np.pi, 200) + >>> zdata_fine = rsbs(thetaarr_fine, phiarr_fine) + + Finally we plot the coarsly-sampled input data alongside the + finely-sampled interpolated data to check that they agree. + + >>> import matplotlib.pyplot as plt + >>> fig = plt.figure() + >>> ax1 = fig.add_subplot(1, 2, 1) + >>> ax2 = fig.add_subplot(1, 2, 2) + >>> ax1.imshow(zdata) + >>> ax2.imshow(zdata_fine) + >>> plt.show() + """ + theta = np.asarray(theta) + phi = np.asarray(phi) + + if theta.size > 0 and (theta.min() < 0. or theta.max() > np.pi): + raise ValueError("requested theta out of bounds.") + + return _BivariateSplineBase.__call__(self, theta, phi, + dx=dtheta, dy=dphi, grid=grid) + + def ev(self, theta, phi, dtheta=0, dphi=0): + """ + Evaluate the spline at points + + Returns the interpolated value at ``(theta[i], phi[i]), + i=0,...,len(theta)-1``. + + Parameters + ---------- + theta, phi : array_like + Input coordinates. Standard Numpy broadcasting is obeyed. + The ordering of axes is consistent with + np.meshgrid(..., indexing="ij") and inconsistent with the + default ordering np.meshgrid(..., indexing="xy"). + dtheta : int, optional + Order of theta-derivative + + .. versionadded:: 0.14.0 + dphi : int, optional + Order of phi-derivative + + .. versionadded:: 0.14.0 + + Examples + -------- + Suppose that we want to use splines to interpolate a bivariate function on a + sphere. The value of the function is known on a grid of longitudes and + colatitudes. + + >>> import numpy as np + >>> from scipy.interpolate import RectSphereBivariateSpline + >>> def f(theta, phi): + ... return np.sin(theta) * np.cos(phi) + + We evaluate the function on the grid. Note that the default indexing="xy" + of meshgrid would result in an unexpected (transposed) result after + interpolation. + + >>> thetaarr = np.linspace(0, np.pi, 22)[1:-1] + >>> phiarr = np.linspace(0, 2 * np.pi, 21)[:-1] + >>> thetagrid, phigrid = np.meshgrid(thetaarr, phiarr, indexing="ij") + >>> zdata = f(thetagrid, phigrid) + + We next set up the interpolator and use it to evaluate the function + at points not on the original grid. + + >>> rsbs = RectSphereBivariateSpline(thetaarr, phiarr, zdata) + >>> thetainterp = np.linspace(thetaarr[0], thetaarr[-1], 200) + >>> phiinterp = np.linspace(phiarr[0], phiarr[-1], 200) + >>> zinterp = rsbs.ev(thetainterp, phiinterp) + + Finally we plot the original data for a diagonal slice through the + initial grid, and the spline approximation along the same slice. + + >>> import matplotlib.pyplot as plt + >>> fig = plt.figure() + >>> ax1 = fig.add_subplot(1, 1, 1) + >>> ax1.plot(np.sin(thetaarr) * np.sin(phiarr), np.diag(zdata), "or") + >>> ax1.plot(np.sin(thetainterp) * np.sin(phiinterp), zinterp, "-b") + >>> plt.show() + """ + return self.__call__(theta, phi, dtheta=dtheta, dphi=dphi, grid=False) + + +class SmoothSphereBivariateSpline(SphereBivariateSpline): + """ + Smooth bivariate spline approximation in spherical coordinates. + + .. versionadded:: 0.11.0 + + Parameters + ---------- + theta, phi, r : array_like + 1-D sequences of data points (order is not important). Coordinates + must be given in radians. Theta must lie within the interval + ``[0, pi]``, and phi must lie within the interval ``[0, 2pi]``. + w : array_like, optional + Positive 1-D sequence of weights. + s : float, optional + Positive smoothing factor defined for estimation condition: + ``sum((w(i)*(r(i) - s(theta(i), phi(i))))**2, axis=0) <= s`` + Default ``s=len(w)`` which should be a good value if ``1/w[i]`` is an + estimate of the standard deviation of ``r[i]``. + eps : float, optional + A threshold for determining the effective rank of an over-determined + linear system of equations. `eps` should have a value within the open + interval ``(0, 1)``, the default is 1e-16. + + See Also + -------- + BivariateSpline : + a base class for bivariate splines. + UnivariateSpline : + a smooth univariate spline to fit a given set of data points. + SmoothBivariateSpline : + a smoothing bivariate spline through the given points + LSQBivariateSpline : + a bivariate spline using weighted least-squares fitting + RectSphereBivariateSpline : + a bivariate spline over a rectangular mesh on a sphere + LSQSphereBivariateSpline : + a bivariate spline in spherical coordinates using weighted + least-squares fitting + RectBivariateSpline : + a bivariate spline over a rectangular mesh. + bisplrep : + a function to find a bivariate B-spline representation of a surface + bisplev : + a function to evaluate a bivariate B-spline and its derivatives + + Notes + ----- + For more information, see the FITPACK_ site about this function. + + .. _FITPACK: http://www.netlib.org/dierckx/sphere.f + + Examples + -------- + Suppose we have global data on a coarse grid (the input data does not + have to be on a grid): + + >>> import numpy as np + >>> theta = np.linspace(0., np.pi, 7) + >>> phi = np.linspace(0., 2*np.pi, 9) + >>> data = np.empty((theta.shape[0], phi.shape[0])) + >>> data[:,0], data[0,:], data[-1,:] = 0., 0., 0. + >>> data[1:-1,1], data[1:-1,-1] = 1., 1. + >>> data[1,1:-1], data[-2,1:-1] = 1., 1. + >>> data[2:-2,2], data[2:-2,-2] = 2., 2. + >>> data[2,2:-2], data[-3,2:-2] = 2., 2. + >>> data[3,3:-2] = 3. + >>> data = np.roll(data, 4, 1) + + We need to set up the interpolator object + + >>> lats, lons = np.meshgrid(theta, phi) + >>> from scipy.interpolate import SmoothSphereBivariateSpline + >>> lut = SmoothSphereBivariateSpline(lats.ravel(), lons.ravel(), + ... data.T.ravel(), s=3.5) + + As a first test, we'll see what the algorithm returns when run on the + input coordinates + + >>> data_orig = lut(theta, phi) + + Finally we interpolate the data to a finer grid + + >>> fine_lats = np.linspace(0., np.pi, 70) + >>> fine_lons = np.linspace(0., 2 * np.pi, 90) + + >>> data_smth = lut(fine_lats, fine_lons) + + >>> import matplotlib.pyplot as plt + >>> fig = plt.figure() + >>> ax1 = fig.add_subplot(131) + >>> ax1.imshow(data, interpolation='nearest') + >>> ax2 = fig.add_subplot(132) + >>> ax2.imshow(data_orig, interpolation='nearest') + >>> ax3 = fig.add_subplot(133) + >>> ax3.imshow(data_smth, interpolation='nearest') + >>> plt.show() + + """ + + def __init__(self, theta, phi, r, w=None, s=0., eps=1E-16): + + theta, phi, r = np.asarray(theta), np.asarray(phi), np.asarray(r) + + # input validation + if not ((0.0 <= theta).all() and (theta <= np.pi).all()): + raise ValueError('theta should be between [0, pi]') + if not ((0.0 <= phi).all() and (phi <= 2.0 * np.pi).all()): + raise ValueError('phi should be between [0, 2pi]') + if w is not None: + w = np.asarray(w) + if not (w >= 0.0).all(): + raise ValueError('w should be positive') + if not s >= 0.0: + raise ValueError('s should be positive') + if not 0.0 < eps < 1.0: + raise ValueError('eps should be between (0, 1)') + + with FITPACK_LOCK: + nt_, tt_, np_, tp_, c, fp, ier = dfitpack.spherfit_smth(theta, phi, + r, w=w, s=s, + eps=eps) + if ier not in [0, -1, -2]: + message = _spherefit_messages.get(ier, f'ier={ier}') + raise ValueError(message) + + self.fp = fp + self.tck = tt_[:nt_], tp_[:np_], c[:(nt_ - 4) * (np_ - 4)] + self.degrees = (3, 3) + + def __call__(self, theta, phi, dtheta=0, dphi=0, grid=True): + + theta = np.asarray(theta) + phi = np.asarray(phi) + + if phi.size > 0 and (phi.min() < 0. or phi.max() > 2. * np.pi): + raise ValueError("requested phi out of bounds.") + + return SphereBivariateSpline.__call__(self, theta, phi, dtheta=dtheta, + dphi=dphi, grid=grid) + + +class LSQSphereBivariateSpline(SphereBivariateSpline): + """ + Weighted least-squares bivariate spline approximation in spherical + coordinates. + + Determines a smoothing bicubic spline according to a given + set of knots in the `theta` and `phi` directions. + + .. versionadded:: 0.11.0 + + Parameters + ---------- + theta, phi, r : array_like + 1-D sequences of data points (order is not important). Coordinates + must be given in radians. Theta must lie within the interval + ``[0, pi]``, and phi must lie within the interval ``[0, 2pi]``. + tt, tp : array_like + Strictly ordered 1-D sequences of knots coordinates. + Coordinates must satisfy ``0 < tt[i] < pi``, ``0 < tp[i] < 2*pi``. + w : array_like, optional + Positive 1-D sequence of weights, of the same length as `theta`, `phi` + and `r`. + eps : float, optional + A threshold for determining the effective rank of an over-determined + linear system of equations. `eps` should have a value within the + open interval ``(0, 1)``, the default is 1e-16. + + See Also + -------- + BivariateSpline : + a base class for bivariate splines. + UnivariateSpline : + a smooth univariate spline to fit a given set of data points. + SmoothBivariateSpline : + a smoothing bivariate spline through the given points + LSQBivariateSpline : + a bivariate spline using weighted least-squares fitting + RectSphereBivariateSpline : + a bivariate spline over a rectangular mesh on a sphere + SmoothSphereBivariateSpline : + a smoothing bivariate spline in spherical coordinates + RectBivariateSpline : + a bivariate spline over a rectangular mesh. + bisplrep : + a function to find a bivariate B-spline representation of a surface + bisplev : + a function to evaluate a bivariate B-spline and its derivatives + + Notes + ----- + For more information, see the FITPACK_ site about this function. + + .. _FITPACK: http://www.netlib.org/dierckx/sphere.f + + Examples + -------- + Suppose we have global data on a coarse grid (the input data does not + have to be on a grid): + + >>> from scipy.interpolate import LSQSphereBivariateSpline + >>> import numpy as np + >>> import matplotlib.pyplot as plt + + >>> theta = np.linspace(0, np.pi, num=7) + >>> phi = np.linspace(0, 2*np.pi, num=9) + >>> data = np.empty((theta.shape[0], phi.shape[0])) + >>> data[:,0], data[0,:], data[-1,:] = 0., 0., 0. + >>> data[1:-1,1], data[1:-1,-1] = 1., 1. + >>> data[1,1:-1], data[-2,1:-1] = 1., 1. + >>> data[2:-2,2], data[2:-2,-2] = 2., 2. + >>> data[2,2:-2], data[-3,2:-2] = 2., 2. + >>> data[3,3:-2] = 3. + >>> data = np.roll(data, 4, 1) + + We need to set up the interpolator object. Here, we must also specify the + coordinates of the knots to use. + + >>> lats, lons = np.meshgrid(theta, phi) + >>> knotst, knotsp = theta.copy(), phi.copy() + >>> knotst[0] += .0001 + >>> knotst[-1] -= .0001 + >>> knotsp[0] += .0001 + >>> knotsp[-1] -= .0001 + >>> lut = LSQSphereBivariateSpline(lats.ravel(), lons.ravel(), + ... data.T.ravel(), knotst, knotsp) + + As a first test, we'll see what the algorithm returns when run on the + input coordinates + + >>> data_orig = lut(theta, phi) + + Finally we interpolate the data to a finer grid + + >>> fine_lats = np.linspace(0., np.pi, 70) + >>> fine_lons = np.linspace(0., 2*np.pi, 90) + >>> data_lsq = lut(fine_lats, fine_lons) + + >>> fig = plt.figure() + >>> ax1 = fig.add_subplot(131) + >>> ax1.imshow(data, interpolation='nearest') + >>> ax2 = fig.add_subplot(132) + >>> ax2.imshow(data_orig, interpolation='nearest') + >>> ax3 = fig.add_subplot(133) + >>> ax3.imshow(data_lsq, interpolation='nearest') + >>> plt.show() + + """ + + def __init__(self, theta, phi, r, tt, tp, w=None, eps=1E-16): + + theta, phi, r = np.asarray(theta), np.asarray(phi), np.asarray(r) + tt, tp = np.asarray(tt), np.asarray(tp) + + if not ((0.0 <= theta).all() and (theta <= np.pi).all()): + raise ValueError('theta should be between [0, pi]') + if not ((0.0 <= phi).all() and (phi <= 2*np.pi).all()): + raise ValueError('phi should be between [0, 2pi]') + if not ((0.0 < tt).all() and (tt < np.pi).all()): + raise ValueError('tt should be between (0, pi)') + if not ((0.0 < tp).all() and (tp < 2*np.pi).all()): + raise ValueError('tp should be between (0, 2pi)') + if w is not None: + w = np.asarray(w) + if not (w >= 0.0).all(): + raise ValueError('w should be positive') + if not 0.0 < eps < 1.0: + raise ValueError('eps should be between (0, 1)') + + nt_, np_ = 8 + len(tt), 8 + len(tp) + tt_, tp_ = zeros((nt_,), float), zeros((np_,), float) + tt_[4:-4], tp_[4:-4] = tt, tp + tt_[-4:], tp_[-4:] = np.pi, 2. * np.pi + with FITPACK_LOCK: + tt_, tp_, c, fp, ier = dfitpack.spherfit_lsq(theta, phi, r, tt_, tp_, + w=w, eps=eps) + if ier > 0: + message = _spherefit_messages.get(ier, f'ier={ier}') + raise ValueError(message) + + self.fp = fp + self.tck = tt_, tp_, c + self.degrees = (3, 3) + + def __call__(self, theta, phi, dtheta=0, dphi=0, grid=True): + + theta = np.asarray(theta) + phi = np.asarray(phi) + + if phi.size > 0 and (phi.min() < 0. or phi.max() > 2. * np.pi): + raise ValueError("requested phi out of bounds.") + + return SphereBivariateSpline.__call__(self, theta, phi, dtheta=dtheta, + dphi=dphi, grid=grid) + + +_spfit_messages = _surfit_messages.copy() +_spfit_messages[10] = """ +ERROR: on entry, the input data are controlled on validity + the following restrictions must be satisfied. + -1<=iopt(1)<=1, 0<=iopt(2)<=1, 0<=iopt(3)<=1, + -1<=ider(1)<=1, 0<=ider(2)<=1, ider(2)=0 if iopt(2)=0. + -1<=ider(3)<=1, 0<=ider(4)<=1, ider(4)=0 if iopt(3)=0. + mu >= mumin (see above), mv >= 4, nuest >=8, nvest >= 8, + kwrk>=5+mu+mv+nuest+nvest, + lwrk >= 12+nuest*(mv+nvest+3)+nvest*24+4*mu+8*mv+max(nuest,mv+nvest) + 0< u(i-1)=0: s>=0 + if s=0: nuest>=mu+6+iopt(2)+iopt(3), nvest>=mv+7 + if one of these conditions is found to be violated,control is + immediately repassed to the calling program. in that case there is no + approximation returned.""" + + +class RectSphereBivariateSpline(SphereBivariateSpline): + """ + Bivariate spline approximation over a rectangular mesh on a sphere. + + Can be used for smoothing data. + + .. versionadded:: 0.11.0 + + Parameters + ---------- + u : array_like + 1-D array of colatitude coordinates in strictly ascending order. + Coordinates must be given in radians and lie within the open interval + ``(0, pi)``. + v : array_like + 1-D array of longitude coordinates in strictly ascending order. + Coordinates must be given in radians. First element (``v[0]``) must lie + within the interval ``[-pi, pi)``. Last element (``v[-1]``) must satisfy + ``v[-1] <= v[0] + 2*pi``. + r : array_like + 2-D array of data with shape ``(u.size, v.size)``. + s : float, optional + Positive smoothing factor defined for estimation condition + (``s=0`` is for interpolation). + pole_continuity : bool or (bool, bool), optional + Order of continuity at the poles ``u=0`` (``pole_continuity[0]``) and + ``u=pi`` (``pole_continuity[1]``). The order of continuity at the pole + will be 1 or 0 when this is True or False, respectively. + Defaults to False. + pole_values : float or (float, float), optional + Data values at the poles ``u=0`` and ``u=pi``. Either the whole + parameter or each individual element can be None. Defaults to None. + pole_exact : bool or (bool, bool), optional + Data value exactness at the poles ``u=0`` and ``u=pi``. If True, the + value is considered to be the right function value, and it will be + fitted exactly. If False, the value will be considered to be a data + value just like the other data values. Defaults to False. + pole_flat : bool or (bool, bool), optional + For the poles at ``u=0`` and ``u=pi``, specify whether or not the + approximation has vanishing derivatives. Defaults to False. + + See Also + -------- + BivariateSpline : + a base class for bivariate splines. + UnivariateSpline : + a smooth univariate spline to fit a given set of data points. + SmoothBivariateSpline : + a smoothing bivariate spline through the given points + LSQBivariateSpline : + a bivariate spline using weighted least-squares fitting + SmoothSphereBivariateSpline : + a smoothing bivariate spline in spherical coordinates + LSQSphereBivariateSpline : + a bivariate spline in spherical coordinates using weighted + least-squares fitting + RectBivariateSpline : + a bivariate spline over a rectangular mesh. + bisplrep : + a function to find a bivariate B-spline representation of a surface + bisplev : + a function to evaluate a bivariate B-spline and its derivatives + + Notes + ----- + Currently, only the smoothing spline approximation (``iopt[0] = 0`` and + ``iopt[0] = 1`` in the FITPACK routine) is supported. The exact + least-squares spline approximation is not implemented yet. + + When actually performing the interpolation, the requested `v` values must + lie within the same length 2pi interval that the original `v` values were + chosen from. + + For more information, see the FITPACK_ site about this function. + + .. _FITPACK: http://www.netlib.org/dierckx/spgrid.f + + Examples + -------- + Suppose we have global data on a coarse grid + + >>> import numpy as np + >>> lats = np.linspace(10, 170, 9) * np.pi / 180. + >>> lons = np.linspace(0, 350, 18) * np.pi / 180. + >>> data = np.dot(np.atleast_2d(90. - np.linspace(-80., 80., 18)).T, + ... np.atleast_2d(180. - np.abs(np.linspace(0., 350., 9)))).T + + We want to interpolate it to a global one-degree grid + + >>> new_lats = np.linspace(1, 180, 180) * np.pi / 180 + >>> new_lons = np.linspace(1, 360, 360) * np.pi / 180 + >>> new_lats, new_lons = np.meshgrid(new_lats, new_lons) + + We need to set up the interpolator object + + >>> from scipy.interpolate import RectSphereBivariateSpline + >>> lut = RectSphereBivariateSpline(lats, lons, data) + + Finally we interpolate the data. The `RectSphereBivariateSpline` object + only takes 1-D arrays as input, therefore we need to do some reshaping. + + >>> data_interp = lut.ev(new_lats.ravel(), + ... new_lons.ravel()).reshape((360, 180)).T + + Looking at the original and the interpolated data, one can see that the + interpolant reproduces the original data very well: + + >>> import matplotlib.pyplot as plt + >>> fig = plt.figure() + >>> ax1 = fig.add_subplot(211) + >>> ax1.imshow(data, interpolation='nearest') + >>> ax2 = fig.add_subplot(212) + >>> ax2.imshow(data_interp, interpolation='nearest') + >>> plt.show() + + Choosing the optimal value of ``s`` can be a delicate task. Recommended + values for ``s`` depend on the accuracy of the data values. If the user + has an idea of the statistical errors on the data, she can also find a + proper estimate for ``s``. By assuming that, if she specifies the + right ``s``, the interpolator will use a spline ``f(u,v)`` which exactly + reproduces the function underlying the data, she can evaluate + ``sum((r(i,j)-s(u(i),v(j)))**2)`` to find a good estimate for this ``s``. + For example, if she knows that the statistical errors on her + ``r(i,j)``-values are not greater than 0.1, she may expect that a good + ``s`` should have a value not larger than ``u.size * v.size * (0.1)**2``. + + If nothing is known about the statistical error in ``r(i,j)``, ``s`` must + be determined by trial and error. The best is then to start with a very + large value of ``s`` (to determine the least-squares polynomial and the + corresponding upper bound ``fp0`` for ``s``) and then to progressively + decrease the value of ``s`` (say by a factor 10 in the beginning, i.e. + ``s = fp0 / 10, fp0 / 100, ...`` and more carefully as the approximation + shows more detail) to obtain closer fits. + + The interpolation results for different values of ``s`` give some insight + into this process: + + >>> fig2 = plt.figure() + >>> s = [3e9, 2e9, 1e9, 1e8] + >>> for idx, sval in enumerate(s, 1): + ... lut = RectSphereBivariateSpline(lats, lons, data, s=sval) + ... data_interp = lut.ev(new_lats.ravel(), + ... new_lons.ravel()).reshape((360, 180)).T + ... ax = fig2.add_subplot(2, 2, idx) + ... ax.imshow(data_interp, interpolation='nearest') + ... ax.set_title(f"s = {sval:g}") + >>> plt.show() + + """ + + def __init__(self, u, v, r, s=0., pole_continuity=False, pole_values=None, + pole_exact=False, pole_flat=False): + iopt = np.array([0, 0, 0], dtype=dfitpack_int) + ider = np.array([-1, 0, -1, 0], dtype=dfitpack_int) + if pole_values is None: + pole_values = (None, None) + elif isinstance(pole_values, (float, np.float32, np.float64)): + pole_values = (pole_values, pole_values) + if isinstance(pole_continuity, bool): + pole_continuity = (pole_continuity, pole_continuity) + if isinstance(pole_exact, bool): + pole_exact = (pole_exact, pole_exact) + if isinstance(pole_flat, bool): + pole_flat = (pole_flat, pole_flat) + + r0, r1 = pole_values + iopt[1:] = pole_continuity + if r0 is None: + ider[0] = -1 + else: + ider[0] = pole_exact[0] + + if r1 is None: + ider[2] = -1 + else: + ider[2] = pole_exact[1] + + ider[1], ider[3] = pole_flat + + u, v = np.ravel(u), np.ravel(v) + r = np.asarray(r) + + if not (0.0 < u[0] and u[-1] < np.pi): + raise ValueError('u should be between (0, pi)') + if not -np.pi <= v[0] < np.pi: + raise ValueError('v[0] should be between [-pi, pi)') + if not v[-1] <= v[0] + 2*np.pi: + raise ValueError('v[-1] should be v[0] + 2pi or less ') + + if not np.all(np.diff(u) > 0.0): + raise ValueError('u must be strictly increasing') + if not np.all(np.diff(v) > 0.0): + raise ValueError('v must be strictly increasing') + + if not u.size == r.shape[0]: + raise ValueError('u dimension of r must have same number of ' + 'elements as u') + if not v.size == r.shape[1]: + raise ValueError('v dimension of r must have same number of ' + 'elements as v') + + if pole_continuity[1] is False and pole_flat[1] is True: + raise ValueError('if pole_continuity is False, so must be ' + 'pole_flat') + if pole_continuity[0] is False and pole_flat[0] is True: + raise ValueError('if pole_continuity is False, so must be ' + 'pole_flat') + + if not s >= 0.0: + raise ValueError('s should be positive') + + r = np.ravel(r) + with FITPACK_LOCK: + nu, tu, nv, tv, c, fp, ier = dfitpack.regrid_smth_spher(iopt, ider, + u.copy(), + v.copy(), + r.copy(), + r0, r1, s) + + if ier not in [0, -1, -2]: + msg = _spfit_messages.get(ier, f'ier={ier}') + raise ValueError(msg) + + self.fp = fp + self.tck = tu[:nu], tv[:nv], c[:(nu - 4) * (nv-4)] + self.degrees = (3, 3) + self.v0 = v[0] + + def __call__(self, theta, phi, dtheta=0, dphi=0, grid=True): + + theta = np.asarray(theta) + phi = np.asarray(phi) + + return SphereBivariateSpline.__call__(self, theta, phi, dtheta=dtheta, + dphi=dphi, grid=grid) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_fitpack_impl.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_fitpack_impl.py new file mode 100644 index 0000000000000000000000000000000000000000..a00ca101b591dd69b6b590278083f0bdafbd3a01 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_fitpack_impl.py @@ -0,0 +1,805 @@ +""" +fitpack (dierckx in netlib) --- A Python-C wrapper to FITPACK (by P. Dierckx). + FITPACK is a collection of FORTRAN programs for curve and surface + fitting with splines and tensor product splines. + +See + https://web.archive.org/web/20010524124604/http://www.cs.kuleuven.ac.be:80/cwis/research/nalag/research/topics/fitpack.html +or + http://www.netlib.org/dierckx/ + +Copyright 2002 Pearu Peterson all rights reserved, +Pearu Peterson +Permission to use, modify, and distribute this software is given under the +terms of the SciPy (BSD style) license. See LICENSE.txt that came with +this distribution for specifics. + +NO WARRANTY IS EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK. + +TODO: Make interfaces to the following fitpack functions: + For univariate splines: cocosp, concon, fourco, insert + For bivariate splines: profil, regrid, parsur, surev +""" + +__all__ = ['splrep', 'splprep', 'splev', 'splint', 'sproot', 'spalde', + 'bisplrep', 'bisplev', 'insert', 'splder', 'splantider'] + +import warnings +import numpy as np +from . import _fitpack +from numpy import (atleast_1d, array, ones, zeros, sqrt, ravel, transpose, + empty, iinfo, asarray) + +# Try to replace _fitpack interface with +# f2py-generated version +from . import _dfitpack as dfitpack + + +dfitpack_int = dfitpack.types.intvar.dtype + + +def _int_overflow(x, exception, msg=None): + """Cast the value to an dfitpack_int and raise an OverflowError if the value + cannot fit. + """ + if x > iinfo(dfitpack_int).max: + if msg is None: + msg = f'{x!r} cannot fit into an {dfitpack_int!r}' + raise exception(msg) + return dfitpack_int.type(x) + + +_iermess = { + 0: ["The spline has a residual sum of squares fp such that " + "abs(fp-s)/s<=0.001", None], + -1: ["The spline is an interpolating spline (fp=0)", None], + -2: ["The spline is weighted least-squares polynomial of degree k.\n" + "fp gives the upper bound fp0 for the smoothing factor s", None], + 1: ["The required storage space exceeds the available storage space.\n" + "Probable causes: data (x,y) size is too small or smoothing parameter" + "\ns is too small (fp>s).", ValueError], + 2: ["A theoretically impossible result when finding a smoothing spline\n" + "with fp = s. Probable cause: s too small. (abs(fp-s)/s>0.001)", + ValueError], + 3: ["The maximal number of iterations (20) allowed for finding smoothing\n" + "spline with fp=s has been reached. Probable cause: s too small.\n" + "(abs(fp-s)/s>0.001)", ValueError], + 10: ["Error on input data", ValueError], + 'unknown': ["An error occurred", TypeError] +} + +_iermess2 = { + 0: ["The spline has a residual sum of squares fp such that " + "abs(fp-s)/s<=0.001", None], + -1: ["The spline is an interpolating spline (fp=0)", None], + -2: ["The spline is weighted least-squares polynomial of degree kx and ky." + "\nfp gives the upper bound fp0 for the smoothing factor s", None], + -3: ["Warning. The coefficients of the spline have been computed as the\n" + "minimal norm least-squares solution of a rank deficient system.", + None], + 1: ["The required storage space exceeds the available storage space.\n" + "Probable causes: nxest or nyest too small or s is too small. (fp>s)", + ValueError], + 2: ["A theoretically impossible result when finding a smoothing spline\n" + "with fp = s. Probable causes: s too small or badly chosen eps.\n" + "(abs(fp-s)/s>0.001)", ValueError], + 3: ["The maximal number of iterations (20) allowed for finding smoothing\n" + "spline with fp=s has been reached. Probable cause: s too small.\n" + "(abs(fp-s)/s>0.001)", ValueError], + 4: ["No more knots can be added because the number of B-spline\n" + "coefficients already exceeds the number of data points m.\n" + "Probable causes: either s or m too small. (fp>s)", ValueError], + 5: ["No more knots can be added because the additional knot would\n" + "coincide with an old one. Probable cause: s too small or too large\n" + "a weight to an inaccurate data point. (fp>s)", ValueError], + 10: ["Error on input data", ValueError], + 11: ["rwrk2 too small, i.e., there is not enough workspace for computing\n" + "the minimal least-squares solution of a rank deficient system of\n" + "linear equations.", ValueError], + 'unknown': ["An error occurred", TypeError] +} + +_parcur_cache = {'t': array([], float), 'wrk': array([], float), + 'iwrk': array([], dfitpack_int), 'u': array([], float), + 'ub': 0, 'ue': 1} + + +def splprep(x, w=None, u=None, ub=None, ue=None, k=3, task=0, s=None, t=None, + full_output=0, nest=None, per=0, quiet=1): + # see the docstring of `_fitpack_py/splprep` + if task <= 0: + _parcur_cache = {'t': array([], float), 'wrk': array([], float), + 'iwrk': array([], dfitpack_int), 'u': array([], float), + 'ub': 0, 'ue': 1} + x = atleast_1d(x) + idim, m = x.shape + if per: + for i in range(idim): + if x[i][0] != x[i][-1]: + if not quiet: + warnings.warn(RuntimeWarning('Setting x[%d][%d]=x[%d][0]' % + (i, m, i)), + stacklevel=2) + x[i][-1] = x[i][0] + if not 0 < idim < 11: + raise TypeError('0 < idim < 11 must hold') + if w is None: + w = ones(m, float) + else: + w = atleast_1d(w) + ipar = (u is not None) + if ipar: + _parcur_cache['u'] = u + if ub is None: + _parcur_cache['ub'] = u[0] + else: + _parcur_cache['ub'] = ub + if ue is None: + _parcur_cache['ue'] = u[-1] + else: + _parcur_cache['ue'] = ue + else: + _parcur_cache['u'] = zeros(m, float) + if not (1 <= k <= 5): + raise TypeError('1 <= k= %d <=5 must hold' % k) + if not (-1 <= task <= 1): + raise TypeError('task must be -1, 0 or 1') + if (not len(w) == m) or (ipar == 1 and (not len(u) == m)): + raise TypeError('Mismatch of input dimensions') + if s is None: + s = m - sqrt(2*m) + if t is None and task == -1: + raise TypeError('Knots must be given for task=-1') + if t is not None: + _parcur_cache['t'] = atleast_1d(t) + n = len(_parcur_cache['t']) + if task == -1 and n < 2*k + 2: + raise TypeError('There must be at least 2*k+2 knots for task=-1') + if m <= k: + raise TypeError('m > k must hold') + if nest is None: + nest = m + 2*k + + if (task >= 0 and s == 0) or (nest < 0): + if per: + nest = m + 2*k + else: + nest = m + k + 1 + nest = max(nest, 2*k + 3) + u = _parcur_cache['u'] + ub = _parcur_cache['ub'] + ue = _parcur_cache['ue'] + t = _parcur_cache['t'] + wrk = _parcur_cache['wrk'] + iwrk = _parcur_cache['iwrk'] + t, c, o = _fitpack._parcur(ravel(transpose(x)), w, u, ub, ue, k, + task, ipar, s, t, nest, wrk, iwrk, per) + _parcur_cache['u'] = o['u'] + _parcur_cache['ub'] = o['ub'] + _parcur_cache['ue'] = o['ue'] + _parcur_cache['t'] = t + _parcur_cache['wrk'] = o['wrk'] + _parcur_cache['iwrk'] = o['iwrk'] + ier = o['ier'] + fp = o['fp'] + n = len(t) + u = o['u'] + c.shape = idim, n - k - 1 + tcku = [t, list(c), k], u + if ier <= 0 and not quiet: + warnings.warn(RuntimeWarning(_iermess[ier][0] + + "\tk=%d n=%d m=%d fp=%f s=%f" % + (k, len(t), m, fp, s)), + stacklevel=2) + if ier > 0 and not full_output: + if ier in [1, 2, 3]: + warnings.warn(RuntimeWarning(_iermess[ier][0]), stacklevel=2) + else: + try: + raise _iermess[ier][1](_iermess[ier][0]) + except KeyError as e: + raise _iermess['unknown'][1](_iermess['unknown'][0]) from e + if full_output: + try: + return tcku, fp, ier, _iermess[ier][0] + except KeyError: + return tcku, fp, ier, _iermess['unknown'][0] + else: + return tcku + + +_curfit_cache = {'t': array([], float), 'wrk': array([], float), + 'iwrk': array([], dfitpack_int)} + + +def splrep(x, y, w=None, xb=None, xe=None, k=3, task=0, s=None, t=None, + full_output=0, per=0, quiet=1): + # see the docstring of `_fitpack_py/splrep` + if task <= 0: + _curfit_cache = {} + x, y = map(atleast_1d, [x, y]) + m = len(x) + if w is None: + w = ones(m, float) + if s is None: + s = 0.0 + else: + w = atleast_1d(w) + if s is None: + s = m - sqrt(2*m) + if not len(w) == m: + raise TypeError('len(w)=%d is not equal to m=%d' % (len(w), m)) + if (m != len(y)) or (m != len(w)): + raise TypeError('Lengths of the first three arguments (x,y,w) must ' + 'be equal') + if not (1 <= k <= 5): + raise TypeError('Given degree of the spline (k=%d) is not supported. ' + '(1<=k<=5)' % k) + if m <= k: + raise TypeError('m > k must hold') + if xb is None: + xb = x[0] + if xe is None: + xe = x[-1] + if not (-1 <= task <= 1): + raise TypeError('task must be -1, 0 or 1') + if t is not None: + task = -1 + if task == -1: + if t is None: + raise TypeError('Knots must be given for task=-1') + numknots = len(t) + _curfit_cache['t'] = empty((numknots + 2*k + 2,), float) + _curfit_cache['t'][k+1:-k-1] = t + nest = len(_curfit_cache['t']) + elif task == 0: + if per: + nest = max(m + 2*k, 2*k + 3) + else: + nest = max(m + k + 1, 2*k + 3) + t = empty((nest,), float) + _curfit_cache['t'] = t + if task <= 0: + if per: + _curfit_cache['wrk'] = empty((m*(k + 1) + nest*(8 + 5*k),), float) + else: + _curfit_cache['wrk'] = empty((m*(k + 1) + nest*(7 + 3*k),), float) + _curfit_cache['iwrk'] = empty((nest,), dfitpack_int) + try: + t = _curfit_cache['t'] + wrk = _curfit_cache['wrk'] + iwrk = _curfit_cache['iwrk'] + except KeyError as e: + raise TypeError("must call with task=1 only after" + " call with task=0,-1") from e + if not per: + n, c, fp, ier = dfitpack.curfit(task, x, y, w, t, wrk, iwrk, + xb, xe, k, s) + else: + n, c, fp, ier = dfitpack.percur(task, x, y, w, t, wrk, iwrk, k, s) + tck = (t[:n], c[:n], k) + if ier <= 0 and not quiet: + _mess = (_iermess[ier][0] + "\tk=%d n=%d m=%d fp=%f s=%f" % + (k, len(t), m, fp, s)) + warnings.warn(RuntimeWarning(_mess), stacklevel=2) + if ier > 0 and not full_output: + if ier in [1, 2, 3]: + warnings.warn(RuntimeWarning(_iermess[ier][0]), stacklevel=2) + else: + try: + raise _iermess[ier][1](_iermess[ier][0]) + except KeyError as e: + raise _iermess['unknown'][1](_iermess['unknown'][0]) from e + if full_output: + try: + return tck, fp, ier, _iermess[ier][0] + except KeyError: + return tck, fp, ier, _iermess['unknown'][0] + else: + return tck + + +def splev(x, tck, der=0, ext=0): + # see the docstring of `_fitpack_py/splev` + t, c, k = tck + try: + c[0][0] + parametric = True + except Exception: + parametric = False + if parametric: + return list(map(lambda c, x=x, t=t, k=k, der=der: + splev(x, [t, c, k], der, ext), c)) + else: + if not (0 <= der <= k): + raise ValueError("0<=der=%d<=k=%d must hold" % (der, k)) + if ext not in (0, 1, 2, 3): + raise ValueError(f"ext = {ext} not in (0, 1, 2, 3) ") + + x = asarray(x) + shape = x.shape + x = atleast_1d(x).ravel() + if der == 0: + y, ier = dfitpack.splev(t, c, k, x, ext) + else: + y, ier = dfitpack.splder(t, c, k, x, der, ext) + + if ier == 10: + raise ValueError("Invalid input data") + if ier == 1: + raise ValueError("Found x value not in the domain") + if ier: + raise TypeError("An error occurred") + + return y.reshape(shape) + + +def splint(a, b, tck, full_output=0): + # see the docstring of `_fitpack_py/splint` + t, c, k = tck + try: + c[0][0] + parametric = True + except Exception: + parametric = False + if parametric: + return list(map(lambda c, a=a, b=b, t=t, k=k: + splint(a, b, [t, c, k]), c)) + else: + aint, wrk = dfitpack.splint(t, c, k, a, b) + if full_output: + return aint, wrk + else: + return aint + + +def sproot(tck, mest=10): + # see the docstring of `_fitpack_py/sproot` + t, c, k = tck + if k != 3: + raise ValueError("sproot works only for cubic (k=3) splines") + try: + c[0][0] + parametric = True + except Exception: + parametric = False + if parametric: + return list(map(lambda c, t=t, k=k, mest=mest: + sproot([t, c, k], mest), c)) + else: + if len(t) < 8: + raise TypeError(f"The number of knots {len(t)}>=8") + z, m, ier = dfitpack.sproot(t, c, mest) + if ier == 10: + raise TypeError("Invalid input data. " + "t1<=..<=t4 1: + return list(map(lambda x, tck=tck: spalde(x, tck), x)) + d, ier = dfitpack.spalde(t, c, k+1, x[0]) + if ier == 0: + return d + if ier == 10: + raise TypeError("Invalid input data. t(k)<=x<=t(n-k+1) must hold.") + raise TypeError("Unknown error") + +# def _curfit(x,y,w=None,xb=None,xe=None,k=3,task=0,s=None,t=None, +# full_output=0,nest=None,per=0,quiet=1): + + +_surfit_cache = {'tx': array([], float), 'ty': array([], float), + 'wrk': array([], float), 'iwrk': array([], dfitpack_int)} + + +def bisplrep(x, y, z, w=None, xb=None, xe=None, yb=None, ye=None, + kx=3, ky=3, task=0, s=None, eps=1e-16, tx=None, ty=None, + full_output=0, nxest=None, nyest=None, quiet=1): + """ + Find a bivariate B-spline representation of a surface. + + Given a set of data points (x[i], y[i], z[i]) representing a surface + z=f(x,y), compute a B-spline representation of the surface. Based on + the routine SURFIT from FITPACK. + + Parameters + ---------- + x, y, z : ndarray + Rank-1 arrays of data points. + w : ndarray, optional + Rank-1 array of weights. By default ``w=np.ones(len(x))``. + xb, xe : float, optional + End points of approximation interval in `x`. + By default ``xb = x.min(), xe=x.max()``. + yb, ye : float, optional + End points of approximation interval in `y`. + By default ``yb=y.min(), ye = y.max()``. + kx, ky : int, optional + The degrees of the spline (1 <= kx, ky <= 5). + Third order (kx=ky=3) is recommended. + task : int, optional + If task=0, find knots in x and y and coefficients for a given + smoothing factor, s. + If task=1, find knots and coefficients for another value of the + smoothing factor, s. bisplrep must have been previously called + with task=0 or task=1. + If task=-1, find coefficients for a given set of knots tx, ty. + s : float, optional + A non-negative smoothing factor. If weights correspond + to the inverse of the standard-deviation of the errors in z, + then a good s-value should be found in the range + ``(m-sqrt(2*m),m+sqrt(2*m))`` where m=len(x). + eps : float, optional + A threshold for determining the effective rank of an + over-determined linear system of equations (0 < eps < 1). + `eps` is not likely to need changing. + tx, ty : ndarray, optional + Rank-1 arrays of the knots of the spline for task=-1 + full_output : int, optional + Non-zero to return optional outputs. + nxest, nyest : int, optional + Over-estimates of the total number of knots. If None then + ``nxest = max(kx+sqrt(m/2),2*kx+3)``, + ``nyest = max(ky+sqrt(m/2),2*ky+3)``. + quiet : int, optional + Non-zero to suppress printing of messages. + + Returns + ------- + tck : array_like + A list [tx, ty, c, kx, ky] containing the knots (tx, ty) and + coefficients (c) of the bivariate B-spline representation of the + surface along with the degree of the spline. + fp : ndarray + The weighted sum of squared residuals of the spline approximation. + ier : int + An integer flag about splrep success. Success is indicated if + ier<=0. If ier in [1,2,3] an error occurred but was not raised. + Otherwise an error is raised. + msg : str + A message corresponding to the integer flag, ier. + + See Also + -------- + splprep, splrep, splint, sproot, splev + UnivariateSpline, BivariateSpline + + Notes + ----- + See `bisplev` to evaluate the value of the B-spline given its tck + representation. + + If the input data is such that input dimensions have incommensurate + units and differ by many orders of magnitude, the interpolant may have + numerical artifacts. Consider rescaling the data before interpolation. + + References + ---------- + .. [1] Dierckx P.:An algorithm for surface fitting with spline functions + Ima J. Numer. Anal. 1 (1981) 267-283. + .. [2] Dierckx P.:An algorithm for surface fitting with spline functions + report tw50, Dept. Computer Science,K.U.Leuven, 1980. + .. [3] Dierckx P.:Curve and surface fitting with splines, Monographs on + Numerical Analysis, Oxford University Press, 1993. + + Examples + -------- + Examples are given :ref:`in the tutorial `. + + """ + x, y, z = map(ravel, [x, y, z]) # ensure 1-d arrays. + m = len(x) + if not (m == len(y) == len(z)): + raise TypeError('len(x)==len(y)==len(z) must hold.') + if w is None: + w = ones(m, float) + else: + w = atleast_1d(w) + if not len(w) == m: + raise TypeError('len(w)=%d is not equal to m=%d' % (len(w), m)) + if xb is None: + xb = x.min() + if xe is None: + xe = x.max() + if yb is None: + yb = y.min() + if ye is None: + ye = y.max() + if not (-1 <= task <= 1): + raise TypeError('task must be -1, 0 or 1') + if s is None: + s = m - sqrt(2*m) + if tx is None and task == -1: + raise TypeError('Knots_x must be given for task=-1') + if tx is not None: + _surfit_cache['tx'] = atleast_1d(tx) + nx = len(_surfit_cache['tx']) + if ty is None and task == -1: + raise TypeError('Knots_y must be given for task=-1') + if ty is not None: + _surfit_cache['ty'] = atleast_1d(ty) + ny = len(_surfit_cache['ty']) + if task == -1 and nx < 2*kx+2: + raise TypeError('There must be at least 2*kx+2 knots_x for task=-1') + if task == -1 and ny < 2*ky+2: + raise TypeError('There must be at least 2*ky+2 knots_x for task=-1') + if not ((1 <= kx <= 5) and (1 <= ky <= 5)): + raise TypeError('Given degree of the spline (kx,ky=%d,%d) is not ' + 'supported. (1<=k<=5)' % (kx, ky)) + if m < (kx + 1)*(ky + 1): + raise TypeError('m >= (kx+1)(ky+1) must hold') + if nxest is None: + nxest = int(kx + sqrt(m/2)) + if nyest is None: + nyest = int(ky + sqrt(m/2)) + nxest, nyest = max(nxest, 2*kx + 3), max(nyest, 2*ky + 3) + if task >= 0 and s == 0: + nxest = int(kx + sqrt(3*m)) + nyest = int(ky + sqrt(3*m)) + if task == -1: + _surfit_cache['tx'] = atleast_1d(tx) + _surfit_cache['ty'] = atleast_1d(ty) + tx, ty = _surfit_cache['tx'], _surfit_cache['ty'] + wrk = _surfit_cache['wrk'] + u = nxest - kx - 1 + v = nyest - ky - 1 + km = max(kx, ky) + 1 + ne = max(nxest, nyest) + bx, by = kx*v + ky + 1, ky*u + kx + 1 + b1, b2 = bx, bx + v - ky + if bx > by: + b1, b2 = by, by + u - kx + msg = "Too many data points to interpolate" + lwrk1 = _int_overflow(u*v*(2 + b1 + b2) + + 2*(u + v + km*(m + ne) + ne - kx - ky) + b2 + 1, + OverflowError, + msg=msg) + lwrk2 = _int_overflow(u*v*(b2 + 1) + b2, OverflowError, msg=msg) + tx, ty, c, o = _fitpack._surfit(x, y, z, w, xb, xe, yb, ye, kx, ky, + task, s, eps, tx, ty, nxest, nyest, + wrk, lwrk1, lwrk2) + _curfit_cache['tx'] = tx + _curfit_cache['ty'] = ty + _curfit_cache['wrk'] = o['wrk'] + ier, fp = o['ier'], o['fp'] + tck = [tx, ty, c, kx, ky] + + ierm = min(11, max(-3, ier)) + if ierm <= 0 and not quiet: + _mess = (_iermess2[ierm][0] + + "\tkx,ky=%d,%d nx,ny=%d,%d m=%d fp=%f s=%f" % + (kx, ky, len(tx), len(ty), m, fp, s)) + warnings.warn(RuntimeWarning(_mess), stacklevel=2) + if ierm > 0 and not full_output: + if ier in [1, 2, 3, 4, 5]: + _mess = ("\n\tkx,ky=%d,%d nx,ny=%d,%d m=%d fp=%f s=%f" % + (kx, ky, len(tx), len(ty), m, fp, s)) + warnings.warn(RuntimeWarning(_iermess2[ierm][0] + _mess), stacklevel=2) + else: + try: + raise _iermess2[ierm][1](_iermess2[ierm][0]) + except KeyError as e: + raise _iermess2['unknown'][1](_iermess2['unknown'][0]) from e + if full_output: + try: + return tck, fp, ier, _iermess2[ierm][0] + except KeyError: + return tck, fp, ier, _iermess2['unknown'][0] + else: + return tck + + +def bisplev(x, y, tck, dx=0, dy=0): + """ + Evaluate a bivariate B-spline and its derivatives. + + Return a rank-2 array of spline function values (or spline derivative + values) at points given by the cross-product of the rank-1 arrays `x` and + `y`. In special cases, return an array or just a float if either `x` or + `y` or both are floats. Based on BISPEV and PARDER from FITPACK. + + Parameters + ---------- + x, y : ndarray + Rank-1 arrays specifying the domain over which to evaluate the + spline or its derivative. + tck : tuple + A sequence of length 5 returned by `bisplrep` containing the knot + locations, the coefficients, and the degree of the spline: + [tx, ty, c, kx, ky]. + dx, dy : int, optional + The orders of the partial derivatives in `x` and `y` respectively. + + Returns + ------- + vals : ndarray + The B-spline or its derivative evaluated over the set formed by + the cross-product of `x` and `y`. + + See Also + -------- + splprep, splrep, splint, sproot, splev + UnivariateSpline, BivariateSpline + + Notes + ----- + See `bisplrep` to generate the `tck` representation. + + References + ---------- + .. [1] Dierckx P. : An algorithm for surface fitting + with spline functions + Ima J. Numer. Anal. 1 (1981) 267-283. + .. [2] Dierckx P. : An algorithm for surface fitting + with spline functions + report tw50, Dept. Computer Science,K.U.Leuven, 1980. + .. [3] Dierckx P. : Curve and surface fitting with splines, + Monographs on Numerical Analysis, Oxford University Press, 1993. + + Examples + -------- + Examples are given :ref:`in the tutorial `. + + """ + tx, ty, c, kx, ky = tck + if not (0 <= dx < kx): + raise ValueError("0 <= dx = %d < kx = %d must hold" % (dx, kx)) + if not (0 <= dy < ky): + raise ValueError("0 <= dy = %d < ky = %d must hold" % (dy, ky)) + x, y = map(atleast_1d, [x, y]) + if (len(x.shape) != 1) or (len(y.shape) != 1): + raise ValueError("First two entries should be rank-1 arrays.") + + msg = "Too many data points to interpolate." + + _int_overflow(x.size * y.size, MemoryError, msg=msg) + + if dx != 0 or dy != 0: + _int_overflow((tx.size - kx - 1)*(ty.size - ky - 1), + MemoryError, msg=msg) + z, ier = dfitpack.parder(tx, ty, c, kx, ky, dx, dy, x, y) + else: + z, ier = dfitpack.bispev(tx, ty, c, kx, ky, x, y) + + if ier == 10: + raise ValueError("Invalid input data") + if ier: + raise TypeError("An error occurred") + z.shape = len(x), len(y) + if len(z) > 1: + return z + if len(z[0]) > 1: + return z[0] + return z[0][0] + + +def dblint(xa, xb, ya, yb, tck): + """Evaluate the integral of a spline over area [xa,xb] x [ya,yb]. + + Parameters + ---------- + xa, xb : float + The end-points of the x integration interval. + ya, yb : float + The end-points of the y integration interval. + tck : list [tx, ty, c, kx, ky] + A sequence of length 5 returned by bisplrep containing the knot + locations tx, ty, the coefficients c, and the degrees kx, ky + of the spline. + + Returns + ------- + integ : float + The value of the resulting integral. + """ + tx, ty, c, kx, ky = tck + return dfitpack.dblint(tx, ty, c, kx, ky, xa, xb, ya, yb) + + +def insert(x, tck, m=1, per=0): + # see the docstring of `_fitpack_py/insert` + t, c, k = tck + try: + c[0][0] + parametric = True + except Exception: + parametric = False + if parametric: + cc = [] + for c_vals in c: + tt, cc_val, kk = insert(x, [t, c_vals, k], m) + cc.append(cc_val) + return (tt, cc, kk) + else: + tt, cc, ier = _fitpack._insert(per, t, c, k, x, m) + if ier == 10: + raise ValueError("Invalid input data") + if ier: + raise TypeError("An error occurred") + return (tt, cc, k) + + +def splder(tck, n=1): + # see the docstring of `_fitpack_py/splder` + if n < 0: + return splantider(tck, -n) + + t, c, k = tck + + if n > k: + raise ValueError(f"Order of derivative (n = {n!r}) must be <= " + f"order of spline (k = {tck[2]!r})") + + # Extra axes for the trailing dims of the `c` array: + sh = (slice(None),) + ((None,)*len(c.shape[1:])) + + with np.errstate(invalid='raise', divide='raise'): + try: + for j in range(n): + # See e.g. Schumaker, Spline Functions: Basic Theory, Chapter 5 + + # Compute the denominator in the differentiation formula. + # (and append trailing dims, if necessary) + dt = t[k+1:-1] - t[1:-k-1] + dt = dt[sh] + # Compute the new coefficients + c = (c[1:-1-k] - c[:-2-k]) * k / dt + # Pad coefficient array to same size as knots (FITPACK + # convention) + c = np.r_[c, np.zeros((k,) + c.shape[1:])] + # Adjust knots + t = t[1:-1] + k -= 1 + except FloatingPointError as e: + raise ValueError(("The spline has internal repeated knots " + "and is not differentiable %d times") % n) from e + + return t, c, k + + +def splantider(tck, n=1): + # see the docstring of `_fitpack_py/splantider` + if n < 0: + return splder(tck, -n) + + t, c, k = tck + + # Extra axes for the trailing dims of the `c` array: + sh = (slice(None),) + (None,)*len(c.shape[1:]) + + for j in range(n): + # This is the inverse set of operations to splder. + + # Compute the multiplier in the antiderivative formula. + dt = t[k+1:] - t[:-k-1] + dt = dt[sh] + # Compute the new coefficients + c = np.cumsum(c[:-k-1] * dt, axis=0) / (k + 1) + c = np.r_[np.zeros((1,) + c.shape[1:]), + c, + [c[-1]] * (k+2)] + # New knots + t = np.r_[t[0], t, t[-1]] + k += 1 + + return t, c, k diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_fitpack_py.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_fitpack_py.py new file mode 100644 index 0000000000000000000000000000000000000000..9f7a2ded7e46885b4e0e0e4ccdb8065c25742e6a --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_fitpack_py.py @@ -0,0 +1,898 @@ +__all__ = ['splrep', 'splprep', 'splev', 'splint', 'sproot', 'spalde', + 'bisplrep', 'bisplev', 'insert', 'splder', 'splantider'] + + +import numpy as np + +# These are in the API for fitpack even if not used in fitpack.py itself. +from ._fitpack_impl import bisplrep, bisplev, dblint # noqa: F401 +from . import _fitpack_impl as _impl +from ._bsplines import BSpline + + +def splprep(x, w=None, u=None, ub=None, ue=None, k=3, task=0, s=None, t=None, + full_output=0, nest=None, per=0, quiet=1): + """ + Find the B-spline representation of an N-D curve. + + .. legacy:: function + + Specifically, we recommend using `make_splprep` in new code. + + Given a list of N rank-1 arrays, `x`, which represent a curve in + N-dimensional space parametrized by `u`, find a smooth approximating + spline curve g(`u`). Uses the FORTRAN routine parcur from FITPACK. + + Parameters + ---------- + x : array_like + A list of sample vector arrays representing the curve. + w : array_like, optional + Strictly positive rank-1 array of weights the same length as `x[0]`. + The weights are used in computing the weighted least-squares spline + fit. If the errors in the `x` values have standard-deviation given by + the vector d, then `w` should be 1/d. Default is ``ones(len(x[0]))``. + u : array_like, optional + An array of parameter values. If not given, these values are + calculated automatically as ``M = len(x[0])``, where + + v[0] = 0 + + v[i] = v[i-1] + distance(`x[i]`, `x[i-1]`) + + u[i] = v[i] / v[M-1] + + ub, ue : int, optional + The end-points of the parameters interval. Defaults to + u[0] and u[-1]. + k : int, optional + Degree of the spline. Cubic splines are recommended. + Even values of `k` should be avoided especially with a small s-value. + ``1 <= k <= 5``, default is 3. + task : int, optional + If task==0 (default), find t and c for a given smoothing factor, s. + If task==1, find t and c for another value of the smoothing factor, s. + There must have been a previous call with task=0 or task=1 + for the same set of data. + If task=-1 find the weighted least square spline for a given set of + knots, t. + s : float, optional + A smoothing condition. The amount of smoothness is determined by + satisfying the conditions: ``sum((w * (y - g))**2,axis=0) <= s``, + where g(x) is the smoothed interpolation of (x,y). The user can + use `s` to control the trade-off between closeness and smoothness + of fit. Larger `s` means more smoothing while smaller values of `s` + indicate less smoothing. Recommended values of `s` depend on the + weights, w. If the weights represent the inverse of the + standard-deviation of y, then a good `s` value should be found in + the range ``(m-sqrt(2*m),m+sqrt(2*m))``, where m is the number of + data points in x, y, and w. + t : array, optional + The knots needed for ``task=-1``. + There must be at least ``2*k+2`` knots. + full_output : int, optional + If non-zero, then return optional outputs. + nest : int, optional + An over-estimate of the total number of knots of the spline to + help in determining the storage space. By default nest=m/2. + Always large enough is nest=m+k+1. + per : int, optional + If non-zero, data points are considered periodic with period + ``x[m-1] - x[0]`` and a smooth periodic spline approximation is + returned. Values of ``y[m-1]`` and ``w[m-1]`` are not used. + quiet : int, optional + Non-zero to suppress messages. + + Returns + ------- + tck : tuple + A tuple, ``(t,c,k)`` containing the vector of knots, the B-spline + coefficients, and the degree of the spline. + u : array + An array of the values of the parameter. + fp : float + The weighted sum of squared residuals of the spline approximation. + ier : int + An integer flag about splrep success. Success is indicated + if ier<=0. If ier in [1,2,3] an error occurred but was not raised. + Otherwise an error is raised. + msg : str + A message corresponding to the integer flag, ier. + + See Also + -------- + splrep, splev, sproot, spalde, splint, + bisplrep, bisplev + UnivariateSpline, BivariateSpline + BSpline + make_interp_spline + + Notes + ----- + See `splev` for evaluation of the spline and its derivatives. + The number of dimensions N must be smaller than 11. + + The number of coefficients in the `c` array is ``k+1`` less than the number + of knots, ``len(t)``. This is in contrast with `splrep`, which zero-pads + the array of coefficients to have the same length as the array of knots. + These additional coefficients are ignored by evaluation routines, `splev` + and `BSpline`. + + References + ---------- + .. [1] P. Dierckx, "Algorithms for smoothing data with periodic and + parametric splines, Computer Graphics and Image Processing", + 20 (1982) 171-184. + .. [2] P. Dierckx, "Algorithms for smoothing data with periodic and + parametric splines", report tw55, Dept. Computer Science, + K.U.Leuven, 1981. + .. [3] P. Dierckx, "Curve and surface fitting with splines", Monographs on + Numerical Analysis, Oxford University Press, 1993. + + Examples + -------- + Generate a discretization of a limacon curve in the polar coordinates: + + >>> import numpy as np + >>> phi = np.linspace(0, 2.*np.pi, 40) + >>> r = 0.5 + np.cos(phi) # polar coords + >>> x, y = r * np.cos(phi), r * np.sin(phi) # convert to cartesian + + And interpolate: + + >>> from scipy.interpolate import splprep, splev + >>> tck, u = splprep([x, y], s=0) + >>> new_points = splev(u, tck) + + Notice that (i) we force interpolation by using ``s=0``, + (ii) the parameterization, ``u``, is generated automatically. + Now plot the result: + + >>> import matplotlib.pyplot as plt + >>> fig, ax = plt.subplots() + >>> ax.plot(x, y, 'ro') + >>> ax.plot(new_points[0], new_points[1], 'r-') + >>> plt.show() + + """ + + res = _impl.splprep(x, w, u, ub, ue, k, task, s, t, full_output, nest, per, + quiet) + return res + + +def splrep(x, y, w=None, xb=None, xe=None, k=3, task=0, s=None, t=None, + full_output=0, per=0, quiet=1): + """ + Find the B-spline representation of a 1-D curve. + + .. legacy:: function + + Specifically, we recommend using `make_splrep` in new code. + + + Given the set of data points ``(x[i], y[i])`` determine a smooth spline + approximation of degree k on the interval ``xb <= x <= xe``. + + Parameters + ---------- + x, y : array_like + The data points defining a curve ``y = f(x)``. + w : array_like, optional + Strictly positive rank-1 array of weights the same length as `x` and `y`. + The weights are used in computing the weighted least-squares spline + fit. If the errors in the `y` values have standard-deviation given by the + vector ``d``, then `w` should be ``1/d``. Default is ``ones(len(x))``. + xb, xe : float, optional + The interval to fit. If None, these default to ``x[0]`` and ``x[-1]`` + respectively. + k : int, optional + The degree of the spline fit. It is recommended to use cubic splines. + Even values of `k` should be avoided especially with small `s` values. + ``1 <= k <= 5``. + task : {1, 0, -1}, optional + If ``task==0``, find ``t`` and ``c`` for a given smoothing factor, `s`. + + If ``task==1`` find ``t`` and ``c`` for another value of the smoothing factor, + `s`. There must have been a previous call with ``task=0`` or ``task=1`` for + the same set of data (``t`` will be stored an used internally) + + If ``task=-1`` find the weighted least square spline for a given set of + knots, ``t``. These should be interior knots as knots on the ends will be + added automatically. + s : float, optional + A smoothing condition. The amount of smoothness is determined by + satisfying the conditions: ``sum((w * (y - g))**2,axis=0) <= s`` where ``g(x)`` + is the smoothed interpolation of ``(x,y)``. The user can use `s` to control + the tradeoff between closeness and smoothness of fit. Larger `s` means + more smoothing while smaller values of `s` indicate less smoothing. + Recommended values of `s` depend on the weights, `w`. If the weights + represent the inverse of the standard-deviation of `y`, then a good `s` + value should be found in the range ``(m-sqrt(2*m),m+sqrt(2*m))`` where ``m`` is + the number of datapoints in `x`, `y`, and `w`. default : ``s=m-sqrt(2*m)`` if + weights are supplied. ``s = 0.0`` (interpolating) if no weights are + supplied. + t : array_like, optional + The knots needed for ``task=-1``. If given then task is automatically set + to ``-1``. + full_output : bool, optional + If non-zero, then return optional outputs. + per : bool, optional + If non-zero, data points are considered periodic with period ``x[m-1]`` - + ``x[0]`` and a smooth periodic spline approximation is returned. Values of + ``y[m-1]`` and ``w[m-1]`` are not used. + The default is zero, corresponding to boundary condition 'not-a-knot'. + quiet : bool, optional + Non-zero to suppress messages. + + Returns + ------- + tck : tuple + A tuple ``(t,c,k)`` containing the vector of knots, the B-spline + coefficients, and the degree of the spline. + fp : array, optional + The weighted sum of squared residuals of the spline approximation. + ier : int, optional + An integer flag about splrep success. Success is indicated if ``ier<=0``. + If ``ier in [1,2,3]``, an error occurred but was not raised. Otherwise an + error is raised. + msg : str, optional + A message corresponding to the integer flag, `ier`. + + See Also + -------- + UnivariateSpline, BivariateSpline + splprep, splev, sproot, spalde, splint + bisplrep, bisplev + BSpline + make_interp_spline + + Notes + ----- + See `splev` for evaluation of the spline and its derivatives. Uses the + FORTRAN routine ``curfit`` from FITPACK. + + The user is responsible for assuring that the values of `x` are unique. + Otherwise, `splrep` will not return sensible results. + + If provided, knots `t` must satisfy the Schoenberg-Whitney conditions, + i.e., there must be a subset of data points ``x[j]`` such that + ``t[j] < x[j] < t[j+k+1]``, for ``j=0, 1,...,n-k-2``. + + This routine zero-pads the coefficients array ``c`` to have the same length + as the array of knots ``t`` (the trailing ``k + 1`` coefficients are ignored + by the evaluation routines, `splev` and `BSpline`.) This is in contrast with + `splprep`, which does not zero-pad the coefficients. + + The default boundary condition is 'not-a-knot', i.e. the first and second + segment at a curve end are the same polynomial. More boundary conditions are + available in `CubicSpline`. + + References + ---------- + Based on algorithms described in [1]_, [2]_, [3]_, and [4]_: + + .. [1] P. Dierckx, "An algorithm for smoothing, differentiation and + integration of experimental data using spline functions", + J.Comp.Appl.Maths 1 (1975) 165-184. + .. [2] P. Dierckx, "A fast algorithm for smoothing data on a rectangular + grid while using spline functions", SIAM J.Numer.Anal. 19 (1982) + 1286-1304. + .. [3] P. Dierckx, "An improved algorithm for curve fitting with spline + functions", report tw54, Dept. Computer Science,K.U. Leuven, 1981. + .. [4] P. Dierckx, "Curve and surface fitting with splines", Monographs on + Numerical Analysis, Oxford University Press, 1993. + + Examples + -------- + You can interpolate 1-D points with a B-spline curve. + Further examples are given in + :ref:`in the tutorial `. + + >>> import numpy as np + >>> import matplotlib.pyplot as plt + >>> from scipy.interpolate import splev, splrep + >>> x = np.linspace(0, 10, 10) + >>> y = np.sin(x) + >>> spl = splrep(x, y) + >>> x2 = np.linspace(0, 10, 200) + >>> y2 = splev(x2, spl) + >>> plt.plot(x, y, 'o', x2, y2) + >>> plt.show() + + """ + res = _impl.splrep(x, y, w, xb, xe, k, task, s, t, full_output, per, quiet) + return res + + +def splev(x, tck, der=0, ext=0): + """ + Evaluate a B-spline or its derivatives. + + .. legacy:: function + + Specifically, we recommend constructing a `BSpline` object and using + its ``__call__`` method. + + Given the knots and coefficients of a B-spline representation, evaluate + the value of the smoothing polynomial and its derivatives. This is a + wrapper around the FORTRAN routines splev and splder of FITPACK. + + Parameters + ---------- + x : array_like + An array of points at which to return the value of the smoothed + spline or its derivatives. If `tck` was returned from `splprep`, + then the parameter values, u should be given. + tck : BSpline instance or tuple + If a tuple, then it should be a sequence of length 3 returned by + `splrep` or `splprep` containing the knots, coefficients, and degree + of the spline. (Also see Notes.) + der : int, optional + The order of derivative of the spline to compute (must be less than + or equal to k, the degree of the spline). + ext : int, optional + Controls the value returned for elements of ``x`` not in the + interval defined by the knot sequence. + + * if ext=0, return the extrapolated value. + * if ext=1, return 0 + * if ext=2, raise a ValueError + * if ext=3, return the boundary value. + + The default value is 0. + + Returns + ------- + y : ndarray or list of ndarrays + An array of values representing the spline function evaluated at + the points in `x`. If `tck` was returned from `splprep`, then this + is a list of arrays representing the curve in an N-D space. + + See Also + -------- + splprep, splrep, sproot, spalde, splint + bisplrep, bisplev + BSpline + + Notes + ----- + Manipulating the tck-tuples directly is not recommended. In new code, + prefer using `BSpline` objects. + + References + ---------- + .. [1] C. de Boor, "On calculating with b-splines", J. Approximation + Theory, 6, p.50-62, 1972. + .. [2] M. G. Cox, "The numerical evaluation of b-splines", J. Inst. Maths + Applics, 10, p.134-149, 1972. + .. [3] P. Dierckx, "Curve and surface fitting with splines", Monographs + on Numerical Analysis, Oxford University Press, 1993. + + Examples + -------- + Examples are given :ref:`in the tutorial `. + + A comparison between `splev`, `splder` and `spalde` to compute the derivatives of a + B-spline can be found in the `spalde` examples section. + + """ + if isinstance(tck, BSpline): + if tck.c.ndim > 1: + mesg = ("Calling splev() with BSpline objects with c.ndim > 1 is " + "not allowed. Use BSpline.__call__(x) instead.") + raise ValueError(mesg) + + # remap the out-of-bounds behavior + try: + extrapolate = {0: True, }[ext] + except KeyError as e: + raise ValueError(f"Extrapolation mode {ext} is not supported " + "by BSpline.") from e + + return tck(x, der, extrapolate=extrapolate) + else: + return _impl.splev(x, tck, der, ext) + + +def splint(a, b, tck, full_output=0): + """ + Evaluate the definite integral of a B-spline between two given points. + + .. legacy:: function + + Specifically, we recommend constructing a `BSpline` object and using its + ``integrate`` method. + + Parameters + ---------- + a, b : float + The end-points of the integration interval. + tck : tuple or a BSpline instance + If a tuple, then it should be a sequence of length 3, containing the + vector of knots, the B-spline coefficients, and the degree of the + spline (see `splev`). + full_output : int, optional + Non-zero to return optional output. + + Returns + ------- + integral : float + The resulting integral. + wrk : ndarray + An array containing the integrals of the normalized B-splines + defined on the set of knots. + (Only returned if `full_output` is non-zero) + + See Also + -------- + splprep, splrep, sproot, spalde, splev + bisplrep, bisplev + BSpline + + Notes + ----- + `splint` silently assumes that the spline function is zero outside the data + interval (`a`, `b`). + + Manipulating the tck-tuples directly is not recommended. In new code, + prefer using the `BSpline` objects. + + References + ---------- + .. [1] P.W. Gaffney, The calculation of indefinite integrals of b-splines", + J. Inst. Maths Applics, 17, p.37-41, 1976. + .. [2] P. Dierckx, "Curve and surface fitting with splines", Monographs + on Numerical Analysis, Oxford University Press, 1993. + + Examples + -------- + Examples are given :ref:`in the tutorial `. + + """ + if isinstance(tck, BSpline): + if tck.c.ndim > 1: + mesg = ("Calling splint() with BSpline objects with c.ndim > 1 is " + "not allowed. Use BSpline.integrate() instead.") + raise ValueError(mesg) + + if full_output != 0: + mesg = (f"full_output = {full_output} is not supported. Proceeding as if " + "full_output = 0") + + return tck.integrate(a, b, extrapolate=False) + else: + return _impl.splint(a, b, tck, full_output) + + +def sproot(tck, mest=10): + """ + Find the roots of a cubic B-spline. + + .. legacy:: function + + Specifically, we recommend constructing a `BSpline` object and using the + following pattern: `PPoly.from_spline(spl).roots()`. + + Given the knots (>=8) and coefficients of a cubic B-spline return the + roots of the spline. + + Parameters + ---------- + tck : tuple or a BSpline object + If a tuple, then it should be a sequence of length 3, containing the + vector of knots, the B-spline coefficients, and the degree of the + spline. + The number of knots must be >= 8, and the degree must be 3. + The knots must be a montonically increasing sequence. + mest : int, optional + An estimate of the number of zeros (Default is 10). + + Returns + ------- + zeros : ndarray + An array giving the roots of the spline. + + See Also + -------- + splprep, splrep, splint, spalde, splev + bisplrep, bisplev + BSpline + + Notes + ----- + Manipulating the tck-tuples directly is not recommended. In new code, + prefer using the `BSpline` objects. + + References + ---------- + .. [1] C. de Boor, "On calculating with b-splines", J. Approximation + Theory, 6, p.50-62, 1972. + .. [2] M. G. Cox, "The numerical evaluation of b-splines", J. Inst. Maths + Applics, 10, p.134-149, 1972. + .. [3] P. Dierckx, "Curve and surface fitting with splines", Monographs + on Numerical Analysis, Oxford University Press, 1993. + + Examples + -------- + + For some data, this method may miss a root. This happens when one of + the spline knots (which FITPACK places automatically) happens to + coincide with the true root. A workaround is to convert to `PPoly`, + which uses a different root-finding algorithm. + + For example, + + >>> x = [1.96, 1.97, 1.98, 1.99, 2.00, 2.01, 2.02, 2.03, 2.04, 2.05] + >>> y = [-6.365470e-03, -4.790580e-03, -3.204320e-03, -1.607270e-03, + ... 4.440892e-16, 1.616930e-03, 3.243000e-03, 4.877670e-03, + ... 6.520430e-03, 8.170770e-03] + >>> from scipy.interpolate import splrep, sproot, PPoly + >>> tck = splrep(x, y, s=0) + >>> sproot(tck) + array([], dtype=float64) + + Converting to a PPoly object does find the roots at ``x=2``: + + >>> ppoly = PPoly.from_spline(tck) + >>> ppoly.roots(extrapolate=False) + array([2.]) + + + Further examples are given :ref:`in the tutorial + `. + + """ + if isinstance(tck, BSpline): + if tck.c.ndim > 1: + mesg = ("Calling sproot() with BSpline objects with c.ndim > 1 is " + "not allowed.") + raise ValueError(mesg) + + t, c, k = tck.tck + + # _impl.sproot expects the interpolation axis to be last, so roll it. + # NB: This transpose is a no-op if c is 1D. + sh = tuple(range(c.ndim)) + c = c.transpose(sh[1:] + (0,)) + return _impl.sproot((t, c, k), mest) + else: + return _impl.sproot(tck, mest) + + +def spalde(x, tck): + """ + Evaluate a B-spline and all its derivatives at one point (or set of points) up + to order k (the degree of the spline), being 0 the spline itself. + + .. legacy:: function + + Specifically, we recommend constructing a `BSpline` object and evaluate + its derivative in a loop or a list comprehension. + + Parameters + ---------- + x : array_like + A point or a set of points at which to evaluate the derivatives. + Note that ``t(k) <= x <= t(n-k+1)`` must hold for each `x`. + tck : tuple + A tuple (t,c,k) containing the vector of knots, + the B-spline coefficients, and the degree of the spline whose + derivatives to compute. + + Returns + ------- + results : {ndarray, list of ndarrays} + An array (or a list of arrays) containing all derivatives + up to order k inclusive for each point `x`, being the first element the + spline itself. + + See Also + -------- + splprep, splrep, splint, sproot, splev, bisplrep, bisplev, + UnivariateSpline, BivariateSpline + + References + ---------- + .. [1] de Boor C : On calculating with b-splines, J. Approximation Theory + 6 (1972) 50-62. + .. [2] Cox M.G. : The numerical evaluation of b-splines, J. Inst. Maths + applics 10 (1972) 134-149. + .. [3] Dierckx P. : Curve and surface fitting with splines, Monographs on + Numerical Analysis, Oxford University Press, 1993. + + Examples + -------- + To calculate the derivatives of a B-spline there are several aproaches. + In this example, we will demonstrate that `spalde` is equivalent to + calling `splev` and `splder`. + + >>> import numpy as np + >>> import matplotlib.pyplot as plt + >>> from scipy.interpolate import BSpline, spalde, splder, splev + + >>> # Store characteristic parameters of a B-spline + >>> tck = ((-2, -2, -2, -2, -1, 0, 1, 2, 2, 2, 2), # knots + ... (0, 0, 0, 6, 0, 0, 0), # coefficients + ... 3) # degree (cubic) + >>> # Instance a B-spline object + >>> # `BSpline` objects are preferred, except for spalde() + >>> bspl = BSpline(tck[0], tck[1], tck[2]) + >>> # Generate extra points to get a smooth curve + >>> x = np.linspace(min(tck[0]), max(tck[0]), 100) + + Evaluate the curve and all derivatives + + >>> # The order of derivative must be less or equal to k, the degree of the spline + >>> # Method 1: spalde() + >>> f1_y_bsplin = [spalde(i, tck)[0] for i in x ] # The B-spline itself + >>> f1_y_deriv1 = [spalde(i, tck)[1] for i in x ] # 1st derivative + >>> f1_y_deriv2 = [spalde(i, tck)[2] for i in x ] # 2nd derivative + >>> f1_y_deriv3 = [spalde(i, tck)[3] for i in x ] # 3rd derivative + >>> # You can reach the same result by using `splev`and `splder` + >>> f2_y_deriv3 = splev(x, bspl, der=3) + >>> f3_y_deriv3 = splder(bspl, n=3)(x) + + >>> # Generate a figure with three axes for graphic comparison + >>> fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(16, 5)) + >>> suptitle = fig.suptitle(f'Evaluate a B-spline and all derivatives') + >>> # Plot B-spline and all derivatives using the three methods + >>> orders = range(4) + >>> linetypes = ['-', '--', '-.', ':'] + >>> labels = ['B-Spline', '1st deriv.', '2nd deriv.', '3rd deriv.'] + >>> functions = ['splev()', 'splder()', 'spalde()'] + >>> for order, linetype, label in zip(orders, linetypes, labels): + ... ax1.plot(x, splev(x, bspl, der=order), linetype, label=label) + ... ax2.plot(x, splder(bspl, n=order)(x), linetype, label=label) + ... ax3.plot(x, [spalde(i, tck)[order] for i in x], linetype, label=label) + >>> for ax, function in zip((ax1, ax2, ax3), functions): + ... ax.set_title(function) + ... ax.legend() + >>> plt.tight_layout() + >>> plt.show() + + """ + if isinstance(tck, BSpline): + raise TypeError("spalde does not accept BSpline instances.") + else: + return _impl.spalde(x, tck) + + +def insert(x, tck, m=1, per=0): + """ + Insert knots into a B-spline. + + .. legacy:: function + + Specifically, we recommend constructing a `BSpline` object and using + its ``insert_knot`` method. + + Given the knots and coefficients of a B-spline representation, create a + new B-spline with a knot inserted `m` times at point `x`. + This is a wrapper around the FORTRAN routine insert of FITPACK. + + Parameters + ---------- + x (u) : float + A knot value at which to insert a new knot. If `tck` was returned + from ``splprep``, then the parameter values, u should be given. + tck : a `BSpline` instance or a tuple + If tuple, then it is expected to be a tuple (t,c,k) containing + the vector of knots, the B-spline coefficients, and the degree of + the spline. + m : int, optional + The number of times to insert the given knot (its multiplicity). + Default is 1. + per : int, optional + If non-zero, the input spline is considered periodic. + + Returns + ------- + BSpline instance or a tuple + A new B-spline with knots t, coefficients c, and degree k. + ``t(k+1) <= x <= t(n-k)``, where k is the degree of the spline. + In case of a periodic spline (``per != 0``) there must be + either at least k interior knots t(j) satisfying ``t(k+1)>> from scipy.interpolate import splrep, insert + >>> import numpy as np + >>> x = np.linspace(0, 10, 5) + >>> y = np.sin(x) + >>> tck = splrep(x, y) + >>> tck[0] + array([ 0., 0., 0., 0., 5., 10., 10., 10., 10.]) + + A knot is inserted: + + >>> tck_inserted = insert(3, tck) + >>> tck_inserted[0] + array([ 0., 0., 0., 0., 3., 5., 10., 10., 10., 10.]) + + Some knots are inserted: + + >>> tck_inserted2 = insert(8, tck, m=3) + >>> tck_inserted2[0] + array([ 0., 0., 0., 0., 5., 8., 8., 8., 10., 10., 10., 10.]) + + """ + if isinstance(tck, BSpline): + + t, c, k = tck.tck + + # FITPACK expects the interpolation axis to be last, so roll it over + # NB: if c array is 1D, transposes are no-ops + sh = tuple(range(c.ndim)) + c = c.transpose(sh[1:] + (0,)) + t_, c_, k_ = _impl.insert(x, (t, c, k), m, per) + + # and roll the last axis back + c_ = np.asarray(c_) + c_ = c_.transpose((sh[-1],) + sh[:-1]) + return BSpline(t_, c_, k_) + else: + return _impl.insert(x, tck, m, per) + + +def splder(tck, n=1): + """ + Compute the spline representation of the derivative of a given spline + + .. legacy:: function + + Specifically, we recommend constructing a `BSpline` object and using its + ``derivative`` method. + + Parameters + ---------- + tck : BSpline instance or tuple + BSpline instance or a tuple (t,c,k) containing the vector of knots, + the B-spline coefficients, and the degree of the spline whose + derivative to compute + n : int, optional + Order of derivative to evaluate. Default: 1 + + Returns + ------- + `BSpline` instance or tuple + Spline of order k2=k-n representing the derivative + of the input spline. + A tuple is returned if the input argument `tck` is a tuple, otherwise + a BSpline object is constructed and returned. + + See Also + -------- + splantider, splev, spalde + BSpline + + Notes + ----- + + .. versionadded:: 0.13.0 + + Examples + -------- + This can be used for finding maxima of a curve: + + >>> from scipy.interpolate import splrep, splder, sproot + >>> import numpy as np + >>> x = np.linspace(0, 10, 70) + >>> y = np.sin(x) + >>> spl = splrep(x, y, k=4) + + Now, differentiate the spline and find the zeros of the + derivative. (NB: `sproot` only works for order 3 splines, so we + fit an order 4 spline): + + >>> dspl = splder(spl) + >>> sproot(dspl) / np.pi + array([ 0.50000001, 1.5 , 2.49999998]) + + This agrees well with roots :math:`\\pi/2 + n\\pi` of + :math:`\\cos(x) = \\sin'(x)`. + + A comparison between `splev`, `splder` and `spalde` to compute the derivatives of a + B-spline can be found in the `spalde` examples section. + + """ + if isinstance(tck, BSpline): + return tck.derivative(n) + else: + return _impl.splder(tck, n) + + +def splantider(tck, n=1): + """ + Compute the spline for the antiderivative (integral) of a given spline. + + .. legacy:: function + + Specifically, we recommend constructing a `BSpline` object and using its + ``antiderivative`` method. + + Parameters + ---------- + tck : BSpline instance or a tuple of (t, c, k) + Spline whose antiderivative to compute + n : int, optional + Order of antiderivative to evaluate. Default: 1 + + Returns + ------- + BSpline instance or a tuple of (t2, c2, k2) + Spline of order k2=k+n representing the antiderivative of the input + spline. + A tuple is returned iff the input argument `tck` is a tuple, otherwise + a BSpline object is constructed and returned. + + See Also + -------- + splder, splev, spalde + BSpline + + Notes + ----- + The `splder` function is the inverse operation of this function. + Namely, ``splder(splantider(tck))`` is identical to `tck`, modulo + rounding error. + + .. versionadded:: 0.13.0 + + Examples + -------- + >>> from scipy.interpolate import splrep, splder, splantider, splev + >>> import numpy as np + >>> x = np.linspace(0, np.pi/2, 70) + >>> y = 1 / np.sqrt(1 - 0.8*np.sin(x)**2) + >>> spl = splrep(x, y) + + The derivative is the inverse operation of the antiderivative, + although some floating point error accumulates: + + >>> splev(1.7, spl), splev(1.7, splder(splantider(spl))) + (array(2.1565429877197317), array(2.1565429877201865)) + + Antiderivative can be used to evaluate definite integrals: + + >>> ispl = splantider(spl) + >>> splev(np.pi/2, ispl) - splev(0, ispl) + 2.2572053588768486 + + This is indeed an approximation to the complete elliptic integral + :math:`K(m) = \\int_0^{\\pi/2} [1 - m\\sin^2 x]^{-1/2} dx`: + + >>> from scipy.special import ellipk + >>> ellipk(0.8) + 2.2572053268208538 + + """ + if isinstance(tck, BSpline): + return tck.antiderivative(n) + else: + return _impl.splantider(tck, n) + diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_fitpack_repro.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_fitpack_repro.py new file mode 100644 index 0000000000000000000000000000000000000000..f5697f3ad716500f6557175e88adead6b3b4caac --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_fitpack_repro.py @@ -0,0 +1,992 @@ +""" Replicate FITPACK's logic for constructing smoothing spline functions and curves. + + Currently provides analogs of splrep and splprep python routines, i.e. + curfit.f and parcur.f routines (the drivers are fpcurf.f and fppara.f, respectively) + + The Fortran sources are from + https://github.com/scipy/scipy/blob/maintenance/1.11.x/scipy/interpolate/fitpack/ + + .. [1] P. Dierckx, "Algorithms for smoothing data with periodic and + parametric splines, Computer Graphics and Image Processing", + 20 (1982) 171-184. + :doi:`10.1016/0146-664X(82)90043-0`. + .. [2] P. Dierckx, "Curve and surface fitting with splines", Monographs on + Numerical Analysis, Oxford University Press, 1993. + .. [3] P. Dierckx, "An algorithm for smoothing, differentiation and integration + of experimental data using spline functions", + Journal of Computational and Applied Mathematics, vol. I, no 3, p. 165 (1975). + https://doi.org/10.1016/0771-050X(75)90034-0 +""" +import warnings +import operator +import numpy as np + +from ._bsplines import ( + _not_a_knot, make_interp_spline, BSpline, fpcheck, _lsq_solve_qr +) +from . import _dierckx # type: ignore[attr-defined] + + +# cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc +# c part 1: determination of the number of knots and their position c +# c ************************************************************** c +# +# https://github.com/scipy/scipy/blob/maintenance/1.11.x/scipy/interpolate/fitpack/fpcurf.f#L31 + + +# Hardcoded in curfit.f +TOL = 0.001 +MAXIT = 20 + + +def _get_residuals(x, y, t, k, w): + # FITPACK has (w*(spl(x)-y))**2; make_lsq_spline has w*(spl(x)-y)**2 + w2 = w**2 + + # inline the relevant part of + # >>> spl = make_lsq_spline(x, y, w=w2, t=t, k=k) + # NB: + # 1. y is assumed to be 2D here. For 1D case (parametric=False), + # the call must have been preceded by y = y[:, None] (cf _validate_inputs) + # 2. We always sum the squares across axis=1: + # * For 1D (parametric=False), the last dimension has size one, + # so the summation is a no-op. + # * For 2D (parametric=True), the summation is actually how the + # 'residuals' are defined, see Eq. (42) in Dierckx1982 + # (the reference is in the docstring of `class F`) below. + _, _, c = _lsq_solve_qr(x, y, t, k, w) + c = np.ascontiguousarray(c) + spl = BSpline(t, c, k) + return _compute_residuals(w2, spl(x), y) + + +def _compute_residuals(w2, splx, y): + delta = ((splx - y)**2).sum(axis=1) + return w2 * delta + + +def add_knot(x, t, k, residuals): + """Add a new knot. + + (Approximately) replicate FITPACK's logic: + 1. split the `x` array into knot intervals, ``t(j+k) <= x(i) <= t(j+k+1)`` + 2. find the interval with the maximum sum of residuals + 3. insert a new knot into the middle of that interval. + + NB: a new knot is in fact an `x` value at the middle of the interval. + So *the knots are a subset of `x`*. + + This routine is an analog of + https://github.com/scipy/scipy/blob/v1.11.4/scipy/interpolate/fitpack/fpcurf.f#L190-L215 + (cf _split function) + + and https://github.com/scipy/scipy/blob/v1.11.4/scipy/interpolate/fitpack/fpknot.f + """ + new_knot = _dierckx.fpknot(x, t, k, residuals) + + idx_t = np.searchsorted(t, new_knot) + t_new = np.r_[t[:idx_t], new_knot, t[idx_t:]] + return t_new + + +def _validate_inputs(x, y, w, k, s, xb, xe, parametric): + """Common input validations for generate_knots and make_splrep. + """ + x = np.asarray(x, dtype=float) + y = np.asarray(y, dtype=float) + + if w is None: + w = np.ones_like(x, dtype=float) + else: + w = np.asarray(w, dtype=float) + if w.ndim != 1: + raise ValueError(f"{w.ndim = } not implemented yet.") + if (w < 0).any(): + raise ValueError("Weights must be non-negative") + + if y.ndim == 0 or y.ndim > 2: + raise ValueError(f"{y.ndim = } not supported (must be 1 or 2.)") + + parametric = bool(parametric) + if parametric: + if y.ndim != 2: + raise ValueError(f"{y.ndim = } != 2 not supported with {parametric =}.") + else: + if y.ndim != 1: + raise ValueError(f"{y.ndim = } != 1 not supported with {parametric =}.") + # all _impl functions expect y.ndim = 2 + y = y[:, None] + + if w.shape[0] != x.shape[0]: + raise ValueError(f"Weights is incompatible: {w.shape =} != {x.shape}.") + + if x.shape[0] != y.shape[0]: + raise ValueError(f"Data is incompatible: {x.shape = } and {y.shape = }.") + if x.ndim != 1 or (x[1:] < x[:-1]).any(): + raise ValueError("Expect `x` to be an ordered 1D sequence.") + + k = operator.index(k) + + if s < 0: + raise ValueError(f"`s` must be non-negative. Got {s = }") + + if xb is None: + xb = min(x) + if xe is None: + xe = max(x) + + return x, y, w, k, s, xb, xe + + +def generate_knots(x, y, *, w=None, xb=None, xe=None, k=3, s=0, nest=None): + """Replicate FITPACK's constructing the knot vector. + + Parameters + ---------- + x, y : array_like + The data points defining the curve ``y = f(x)``. + w : array_like, optional + Weights. + xb : float, optional + The boundary of the approximation interval. If None (default), + is set to ``x[0]``. + xe : float, optional + The boundary of the approximation interval. If None (default), + is set to ``x[-1]``. + k : int, optional + The spline degree. Default is cubic, ``k = 3``. + s : float, optional + The smoothing factor. Default is ``s = 0``. + nest : int, optional + Stop when at least this many knots are placed. + + Yields + ------ + t : ndarray + Knot vectors with an increasing number of knots. + The generator is finite: it stops when the smoothing critetion is + satisfied, or when then number of knots exceeds the maximum value: + the user-provided `nest` or `x.size + k + 1` --- which is the knot vector + for the interpolating spline. + + Examples + -------- + Generate some noisy data and fit a sequence of LSQ splines: + + >>> import numpy as np + >>> import matplotlib.pyplot as plt + >>> from scipy.interpolate import make_lsq_spline, generate_knots + >>> rng = np.random.default_rng(12345) + >>> x = np.linspace(-3, 3, 50) + >>> y = np.exp(-x**2) + 0.1 * rng.standard_normal(size=50) + + >>> knots = list(generate_knots(x, y, s=1e-10)) + >>> for t in knots[::3]: + ... spl = make_lsq_spline(x, y, t) + ... xs = xs = np.linspace(-3, 3, 201) + ... plt.plot(xs, spl(xs), '-', label=f'n = {len(t)}', lw=3, alpha=0.7) + >>> plt.plot(x, y, 'o', label='data') + >>> plt.plot(xs, np.exp(-xs**2), '--') + >>> plt.legend() + + Note that increasing the number of knots make the result follow the data + more and more closely. + + Also note that a step of the generator may add multiple knots: + + >>> [len(t) for t in knots] + [8, 9, 10, 12, 16, 24, 40, 48, 52, 54] + + Notes + ----- + The routine generates successive knots vectors of increasing length, starting + from ``2*(k+1)`` to ``len(x) + k + 1``, trying to make knots more dense + in the regions where the deviation of the LSQ spline from data is large. + + When the maximum number of knots, ``len(x) + k + 1`` is reached + (this happens when ``s`` is small and ``nest`` is large), the generator + stops, and the last output is the knots for the interpolation with the + not-a-knot boundary condition. + + Knots are located at data sites, unless ``k`` is even and the number of knots + is ``len(x) + k + 1``. In that case, the last output of the generator + has internal knots at Greville sites, ``(x[1:] + x[:-1]) / 2``. + + .. versionadded:: 1.15.0 + + """ + if s == 0: + if nest is not None or w is not None: + raise ValueError("s == 0 is interpolation only") + t = _not_a_knot(x, k) + yield t + return + + x, y, w, k, s, xb, xe = _validate_inputs( + x, y, w, k, s, xb, xe, parametric=np.ndim(y) == 2 + ) + + yield from _generate_knots_impl(x, y, w=w, xb=xb, xe=xe, k=k, s=s, nest=nest) + + +def _generate_knots_impl(x, y, *, w=None, xb=None, xe=None, k=3, s=0, nest=None): + + acc = s * TOL + m = x.size # the number of data points + + if nest is None: + # the max number of knots. This is set in _fitpack_impl.py line 274 + # and fitpack.pyf line 198 + nest = max(m + k + 1, 2*k + 3) + else: + if nest < 2*(k + 1): + raise ValueError(f"`nest` too small: {nest = } < 2*(k+1) = {2*(k+1)}.") + + nmin = 2*(k + 1) # the number of knots for an LSQ polynomial approximation + nmax = m + k + 1 # the number of knots for the spline interpolation + + # start from no internal knots + t = np.asarray([xb]*(k+1) + [xe]*(k+1), dtype=float) + n = t.shape[0] + fp = 0.0 + fpold = 0.0 + + # c main loop for the different sets of knots. m is a safe upper bound + # c for the number of trials. + for _ in range(m): + yield t + + # construct the LSQ spline with this set of knots + fpold = fp + residuals = _get_residuals(x, y, t, k, w=w) + fp = residuals.sum() + fpms = fp - s + + # c test whether the approximation sinf(x) is an acceptable solution. + # c if f(p=inf) < s accept the choice of knots. + if (abs(fpms) < acc) or (fpms < 0): + return + + # ### c increase the number of knots. ### + + # c determine the number of knots nplus we are going to add. + if n == nmin: + # the first iteration + nplus = 1 + else: + delta = fpold - fp + npl1 = int(nplus * fpms / delta) if delta > acc else nplus*2 + nplus = min(nplus*2, max(npl1, nplus//2, 1)) + + # actually add knots + for j in range(nplus): + t = add_knot(x, t, k, residuals) + + # check if we have enough knots already + + n = t.shape[0] + # c if n = nmax, sinf(x) is an interpolating spline. + # c if n=nmax we locate the knots as for interpolation. + if n >= nmax: + t = _not_a_knot(x, k) + yield t + return + + # c if n=nest we cannot increase the number of knots because of + # c the storage capacity limitation. + if n >= nest: + yield t + return + + # recompute if needed + if j < nplus - 1: + residuals = _get_residuals(x, y, t, k, w=w) + + # this should never be reached + return + + +# cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc +# c part 2: determination of the smoothing spline sp(x). c +# c *************************************************** c +# c we have determined the number of knots and their position. c +# c we now compute the b-spline coefficients of the smoothing spline c +# c sp(x). the observation matrix a is extended by the rows of matrix c +# c b expressing that the kth derivative discontinuities of sp(x) at c +# c the interior knots t(k+2),...t(n-k-1) must be zero. the corres- c +# c ponding weights of these additional rows are set to 1/p. c +# c iteratively we then have to determine the value of p such that c +# c f(p)=sum((w(i)*(y(i)-sp(x(i))))**2) be = s. we already know that c +# c the least-squares kth degree polynomial corresponds to p=0, and c +# c that the least-squares spline corresponds to p=infinity. the c +# c iteration process which is proposed here, makes use of rational c +# c interpolation. since f(p) is a convex and strictly decreasing c +# c function of p, it can be approximated by a rational function c +# c r(p) = (u*p+v)/(p+w). three values of p(p1,p2,p3) with correspond- c +# c ing values of f(p) (f1=f(p1)-s,f2=f(p2)-s,f3=f(p3)-s) are used c +# c to calculate the new value of p such that r(p)=s. convergence is c +# c guaranteed by taking f1>0 and f3<0. c +# cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc + + +def prodd(t, i, j, k): + res = 1.0 + for s in range(k+2): + if i + s != j: + res *= (t[j] - t[i+s]) + return res + + +def disc(t, k): + """Discontinuity matrix: jumps of k-th derivatives of b-splines at internal knots. + + See Eqs. (9)-(10) of Ref. [1], or, equivalently, Eq. (3.43) of Ref. [2]. + + This routine assumes internal knots are all simple (have multiplicity =1). + + Parameters + ---------- + t : ndarray, 1D, shape(n,) + Knots. + k : int + The spline degree + + Returns + ------- + disc : ndarray, shape(n-2*k-1, k+2) + The jumps of the k-th derivatives of b-splines at internal knots, + ``t[k+1], ...., t[n-k-1]``. + offset : ndarray, shape(2-2*k-1,) + Offsets + nc : int + + Notes + ----- + + The normalization here follows FITPACK: + (https://github.com/scipy/scipy/blob/maintenance/1.11.x/scipy/interpolate/fitpack/fpdisc.f#L36) + + The k-th derivative jumps are multiplied by a factor:: + + (delta / nrint)**k / k! + + where ``delta`` is the length of the interval spanned by internal knots, and + ``nrint`` is one less the number of internal knots (i.e., the number of + subintervals between them). + + References + ---------- + .. [1] Paul Dierckx, Algorithms for smoothing data with periodic and parametric + splines, Computer Graphics and Image Processing, vol. 20, p. 171 (1982). + :doi:`10.1016/0146-664X(82)90043-0` + + .. [2] Tom Lyche and Knut Morken, Spline methods, + http://www.uio.no/studier/emner/matnat/ifi/INF-MAT5340/v05/undervisningsmateriale/ + + """ + n = t.shape[0] + + # the length of the base interval spanned by internal knots & the number + # of subintervas between these internal knots + delta = t[n - k - 1] - t[k] + nrint = n - 2*k - 1 + + matr = np.empty((nrint - 1, k + 2), dtype=float) + for jj in range(nrint - 1): + j = jj + k + 1 + for ii in range(k + 2): + i = jj + ii + matr[jj, ii] = (t[i + k + 1] - t[i]) / prodd(t, i, j, k) + # NB: equivalent to + # row = [(t[i + k + 1] - t[i]) / prodd(t, i, j, k) for i in range(j-k-1, j+1)] + # assert (matr[j-k-1, :] == row).all() + + # follow FITPACK + matr *= (delta/ nrint)**k + + # make it packed + offset = np.array([i for i in range(nrint-1)], dtype=np.int64) + nc = n - k - 1 + return matr, offset, nc + + +class F: + """ The r.h.s. of ``f(p) = s``. + + Given scalar `p`, we solve the system of equations in the LSQ sense: + + | A | @ | c | = | y | + | B / p | | 0 | | 0 | + + where `A` is the matrix of b-splines and `b` is the discontinuity matrix + (the jumps of the k-th derivatives of b-spline basis elements at knots). + + Since we do that repeatedly while minimizing over `p`, we QR-factorize + `A` only once and update the QR factorization only of the `B` rows of the + augmented matrix |A, B/p|. + + The system of equations is Eq. (15) Ref. [1]_, the strategy and implementation + follows that of FITPACK, see specific links below. + + References + ---------- + [1] P. Dierckx, Algorithms for Smoothing Data with Periodic and Parametric Splines, + COMPUTER GRAPHICS AND IMAGE PROCESSING vol. 20, pp 171-184 (1982.) + https://doi.org/10.1016/0146-664X(82)90043-0 + + """ + def __init__(self, x, y, t, k, s, w=None, *, R=None, Y=None): + self.x = x + self.y = y + self.t = t + self.k = k + w = np.ones_like(x, dtype=float) if w is None else w + if w.ndim != 1: + raise ValueError(f"{w.ndim = } != 1.") + self.w = w + self.s = s + + if y.ndim != 2: + raise ValueError(f"F: expected y.ndim == 2, got {y.ndim = } instead.") + + # ### precompute what we can ### + + # https://github.com/scipy/scipy/blob/maintenance/1.11.x/scipy/interpolate/fitpack/fpcurf.f#L250 + # c evaluate the discontinuity jump of the kth derivative of the + # c b-splines at the knots t(l),l=k+2,...n-k-1 and store in b. + b, b_offset, b_nc = disc(t, k) + + # the QR factorization of the data matrix, if not provided + # NB: otherwise, must be consistent with x,y & s, but this is not checked + if R is None and Y is None: + R, Y, _ = _lsq_solve_qr(x, y, t, k, w) + + # prepare to combine R and the discontinuity matrix (AB); also r.h.s. (YY) + # https://github.com/scipy/scipy/blob/maintenance/1.11.x/scipy/interpolate/fitpack/fpcurf.f#L269 + # c the rows of matrix b with weight 1/p are rotated into the + # c triangularised observation matrix a which is stored in g. + nc = t.shape[0] - k - 1 + nz = k + 1 + if R.shape[1] != nz: + raise ValueError(f"Internal error: {R.shape[1] =} != {k+1 =}.") + + # r.h.s. of the augmented system + z = np.zeros((b.shape[0], Y.shape[1]), dtype=float) + self.YY = np.r_[Y[:nc], z] + + # l.h.s. of the augmented system + AA = np.zeros((nc + b.shape[0], self.k+2), dtype=float) + AA[:nc, :nz] = R[:nc, :] + # AA[nc:, :] = b.a / p # done in __call__(self, p) + self.AA = AA + self.offset = np.r_[np.arange(nc, dtype=np.int64), b_offset] + + self.nc = nc + self.b = b + + def __call__(self, p): + # https://github.com/scipy/scipy/blob/maintenance/1.11.x/scipy/interpolate/fitpack/fpcurf.f#L279 + # c the row of matrix b is rotated into triangle by givens transformation + + # copy the precomputed matrices over for in-place work + # R = PackedMatrix(self.AB.a.copy(), self.AB.offset.copy(), nc) + AB = self.AA.copy() + offset = self.offset.copy() + nc = self.nc + + AB[nc:, :] = self.b / p + QY = self.YY.copy() + + # heavy lifting happens here, in-place + _dierckx.qr_reduce(AB, offset, nc, QY, startrow=nc) + + # solve for the coefficients + c = _dierckx.fpback(AB, nc, QY) + + spl = BSpline(self.t, c, self.k) + residuals = _compute_residuals(self.w**2, spl(self.x), self.y) + fp = residuals.sum() + + self.spl = spl # store it + + return fp - self.s + + +def fprati(p1, f1, p2, f2, p3, f3): + """The root of r(p) = (u*p + v) / (p + w) given three points and values, + (p1, f2), (p2, f2) and (p3, f3). + + The FITPACK analog adjusts the bounds, and we do not + https://github.com/scipy/scipy/blob/maintenance/1.11.x/scipy/interpolate/fitpack/fprati.f + + NB: FITPACK uses p < 0 to encode p=infinity. We just use the infinity itself. + Since the bracket is ``p1 <= p2 <= p3``, ``p3`` can be infinite (in fact, + this is what the minimizer starts with, ``p3=inf``). + """ + h1 = f1 * (f2 - f3) + h2 = f2 * (f3 - f1) + h3 = f3 * (f1 - f2) + if p3 == np.inf: + return -(p2*h1 + p1*h2) / h3 + return -(p1*p2*h3 + p2*p3*h1 + p1*p3*h2) / (p1*h1 + p2*h2 + p3*h3) + + +class Bunch: + def __init__(self, **kwargs): + self.__dict__.update(**kwargs) + + +_iermesg = { +2: """error. a theoretically impossible result was found during +the iteration process for finding a smoothing spline with +fp = s. probably causes : s too small. +there is an approximation returned but the corresponding +weighted sum of squared residuals does not satisfy the +condition abs(fp-s)/s < tol. +""", +3: """error. the maximal number of iterations maxit (set to 20 +by the program) allowed for finding a smoothing spline +with fp=s has been reached. probably causes : s too small +there is an approximation returned but the corresponding +weighted sum of squared residuals does not satisfy the +condition abs(fp-s)/s < tol. +""" +} + + +def root_rati(f, p0, bracket, acc): + """Solve `f(p) = 0` using a rational function approximation. + + In a nutshell, since the function f(p) is known to be monotonically decreasing, we + - maintain the bracket (p1, f1), (p2, f2) and (p3, f3) + - at each iteration step, approximate f(p) by a rational function + r(p) = (u*p + v) / (p + w) + and make a step to p_new to the root of f(p): r(p_new) = 0. + The coefficients u, v and w are found from the bracket values p1..3 and f1...3 + + The algorithm and implementation follows + https://github.com/scipy/scipy/blob/maintenance/1.11.x/scipy/interpolate/fitpack/fpcurf.f#L229 + and + https://github.com/scipy/scipy/blob/maintenance/1.11.x/scipy/interpolate/fitpack/fppara.f#L290 + + Note that the latter is for parametric splines and the former is for 1D spline + functions. The minimization is indentical though [modulo a summation over the + dimensions in the computation of f(p)], so we reuse the minimizer for both + d=1 and d>1. + """ + # Magic values from + # https://github.com/scipy/scipy/blob/maintenance/1.11.x/scipy/interpolate/fitpack/fpcurf.f#L27 + con1 = 0.1 + con9 = 0.9 + con4 = 0.04 + + # bracketing flags (follow FITPACK) + # https://github.com/scipy/scipy/blob/maintenance/1.11.x/scipy/interpolate/fitpack/fppara.f#L365 + ich1, ich3 = 0, 0 + + (p1, f1), (p3, f3) = bracket + p = p0 + + for it in range(MAXIT): + p2, f2 = p, f(p) + + # c test whether the approximation sp(x) is an acceptable solution. + if abs(f2) < acc: + ier, converged = 0, True + break + + # c carry out one more step of the iteration process. + if ich3 == 0: + if f2 - f3 <= acc: + # c our initial choice of p is too large. + p3 = p2 + f3 = f2 + p = p*con4 + if p <= p1: + p = p1*con9 + p2*con1 + continue + else: + if f2 < 0: + ich3 = 1 + + if ich1 == 0: + if f1 - f2 <= acc: + # c our initial choice of p is too small + p1 = p2 + f1 = f2 + p = p/con4 + if p3 != np.inf and p <= p3: + p = p2*con1 + p3*con9 + continue + else: + if f2 > 0: + ich1 = 1 + + # c test whether the iteration process proceeds as theoretically expected. + # [f(p) should be monotonically decreasing] + if f1 <= f2 or f2 <= f3: + ier, converged = 2, False + break + + # actually make the iteration step + p = fprati(p1, f1, p2, f2, p3, f3) + + # c adjust the value of p1,f1,p3 and f3 such that f1 > 0 and f3 < 0. + if f2 < 0: + p3, f3 = p2, f2 + else: + p1, f1 = p2, f2 + + else: + # not converged in MAXIT iterations + ier, converged = 3, False + + if ier != 0: + warnings.warn(RuntimeWarning(_iermesg[ier]), stacklevel=2) + + return Bunch(converged=converged, root=p, iterations=it, ier=ier) + + +def _make_splrep_impl(x, y, *, w=None, xb=None, xe=None, k=3, s=0, t=None, nest=None): + """Shared infra for make_splrep and make_splprep. + """ + acc = s * TOL + m = x.size # the number of data points + + if nest is None: + # the max number of knots. This is set in _fitpack_impl.py line 274 + # and fitpack.pyf line 198 + nest = max(m + k + 1, 2*k + 3) + else: + if nest < 2*(k + 1): + raise ValueError(f"`nest` too small: {nest = } < 2*(k+1) = {2*(k+1)}.") + if t is not None: + raise ValueError("Either supply `t` or `nest`.") + + if t is None: + gen = _generate_knots_impl(x, y, w=w, k=k, s=s, xb=xb, xe=xe, nest=nest) + t = list(gen)[-1] + else: + fpcheck(x, t, k) + + if t.shape[0] == 2 * (k + 1): + # nothing to optimize + _, _, c = _lsq_solve_qr(x, y, t, k, w) + return BSpline(t, c, k) + + ### solve ### + + # c initial value for p. + # https://github.com/scipy/scipy/blob/maintenance/1.11.x/scipy/interpolate/fitpack/fpcurf.f#L253 + R, Y, _ = _lsq_solve_qr(x, y, t, k, w) + nc = t.shape[0] -k -1 + p = nc / R[:, 0].sum() + + # ### bespoke solver #### + # initial conditions + # f(p=inf) : LSQ spline with knots t (XXX: reuse R, c) + residuals = _get_residuals(x, y, t, k, w=w) + fp = residuals.sum() + fpinf = fp - s + + # f(p=0): LSQ spline without internal knots + residuals = _get_residuals(x, y, np.array([xb]*(k+1) + [xe]*(k+1)), k, w) + fp0 = residuals.sum() + fp0 = fp0 - s + + # solve + bracket = (0, fp0), (np.inf, fpinf) + f = F(x, y, t, k=k, s=s, w=w, R=R, Y=Y) + _ = root_rati(f, p, bracket, acc) + + # solve ALTERNATIVE: is roughly equivalent, gives slightly different results + # starting from scratch, that would have probably been tolerable; + # backwards compatibility dictates that we replicate the FITPACK minimizer though. + # f = F(x, y, t, k=k, s=s, w=w, R=R, Y=Y) + # from scipy.optimize import root_scalar + # res_ = root_scalar(f, x0=p, rtol=acc) + # assert res_.converged + + # f.spl is the spline corresponding to the found `p` value + return f.spl + + +def make_splrep(x, y, *, w=None, xb=None, xe=None, k=3, s=0, t=None, nest=None): + r"""Find the B-spline representation of a 1D function. + + Given the set of data points ``(x[i], y[i])``, determine a smooth spline + approximation of degree ``k`` on the interval ``xb <= x <= xe``. + + Parameters + ---------- + x, y : array_like, shape (m,) + The data points defining a curve ``y = f(x)``. + w : array_like, shape (m,), optional + Strictly positive 1D array of weights, of the same length as `x` and `y`. + The weights are used in computing the weighted least-squares spline + fit. If the errors in the y values have standard-deviation given by the + vector ``d``, then `w` should be ``1/d``. + Default is ``np.ones(m)``. + xb, xe : float, optional + The interval to fit. If None, these default to ``x[0]`` and ``x[-1]``, + respectively. + k : int, optional + The degree of the spline fit. It is recommended to use cubic splines, + ``k=3``, which is the default. Even values of `k` should be avoided, + especially with small `s` values. + s : float, optional + The smoothing condition. The amount of smoothness is determined by + satisfying the conditions:: + + sum((w * (g(x) - y))**2 ) <= s + + where ``g(x)`` is the smoothed fit to ``(x, y)``. The user can use `s` + to control the tradeoff between closeness to data and smoothness of fit. + Larger `s` means more smoothing while smaller values of `s` indicate less + smoothing. + Recommended values of `s` depend on the weights, `w`. If the weights + represent the inverse of the standard deviation of `y`, then a good `s` + value should be found in the range ``(m-sqrt(2*m), m+sqrt(2*m))`` where + ``m`` is the number of datapoints in `x`, `y`, and `w`. + Default is ``s = 0.0``, i.e. interpolation. + t : array_like, optional + The spline knots. If None (default), the knots will be constructed + automatically. + There must be at least ``2*k + 2`` and at most ``m + k + 1`` knots. + nest : int, optional + The target length of the knot vector. Should be between ``2*(k + 1)`` + (the minimum number of knots for a degree-``k`` spline), and + ``m + k + 1`` (the number of knots of the interpolating spline). + The actual number of knots returned by this routine may be slightly + larger than `nest`. + Default is None (no limit, add up to ``m + k + 1`` knots). + + Returns + ------- + spl : a `BSpline` instance + For `s=0`, ``spl(x) == y``. + For non-zero values of `s` the `spl` represents the smoothed approximation + to `(x, y)`, generally with fewer knots. + + See Also + -------- + generate_knots : is used under the hood for generating the knots + make_splprep : the analog of this routine for parametric curves + make_interp_spline : construct an interpolating spline (``s = 0``) + make_lsq_spline : construct the least-squares spline given the knot vector + splrep : a FITPACK analog of this routine + + References + ---------- + .. [1] P. Dierckx, "Algorithms for smoothing data with periodic and + parametric splines, Computer Graphics and Image Processing", + 20 (1982) 171-184. + .. [2] P. Dierckx, "Curve and surface fitting with splines", Monographs on + Numerical Analysis, Oxford University Press, 1993. + + Notes + ----- + This routine constructs the smoothing spline function, :math:`g(x)`, to + minimize the sum of jumps, :math:`D_j`, of the ``k``-th derivative at the + internal knots (:math:`x_b < t_i < x_e`), where + + .. math:: + + D_i = g^{(k)}(t_i + 0) - g^{(k)}(t_i - 0) + + Specifically, the routine constructs the spline function :math:`g(x)` which + minimizes + + .. math:: + + \sum_i | D_i |^2 \to \mathrm{min} + + provided that + + .. math:: + + \sum_{j=1}^m (w_j \times (g(x_j) - y_j))^2 \leqslant s , + + where :math:`s > 0` is the input parameter. + + In other words, we balance maximizing the smoothness (measured as the jumps + of the derivative, the first criterion), and the deviation of :math:`g(x_j)` + from the data :math:`y_j` (the second criterion). + + Note that the summation in the second criterion is over all data points, + and in the first criterion it is over the internal spline knots (i.e. + those with ``xb < t[i] < xe``). The spline knots are in general a subset + of data, see `generate_knots` for details. + + Also note the difference of this routine to `make_lsq_spline`: the latter + routine does not consider smoothness and simply solves a least-squares + problem + + .. math:: + + \sum w_j \times (g(x_j) - y_j)^2 \to \mathrm{min} + + for a spline function :math:`g(x)` with a _fixed_ knot vector ``t``. + + .. versionadded:: 1.15.0 + """ + if s == 0: + if t is not None or w is not None or nest is not None: + raise ValueError("s==0 is for interpolation only") + return make_interp_spline(x, y, k=k) + + x, y, w, k, s, xb, xe = _validate_inputs(x, y, w, k, s, xb, xe, parametric=False) + + spl = _make_splrep_impl(x, y, w=w, xb=xb, xe=xe, k=k, s=s, t=t, nest=nest) + + # postprocess: squeeze out the last dimension: was added to simplify the internals. + spl.c = spl.c[:, 0] + return spl + + +def make_splprep(x, *, w=None, u=None, ub=None, ue=None, k=3, s=0, t=None, nest=None): + r""" + Find a smoothed B-spline representation of a parametric N-D curve. + + Given a list of N 1D arrays, `x`, which represent a curve in + N-dimensional space parametrized by `u`, find a smooth approximating + spline curve ``g(u)``. + + Parameters + ---------- + x : array_like, shape (m, ndim) + Sampled data points representing the curve in ``ndim`` dimensions. + The typical use is a list of 1D arrays, each of length ``m``. + w : array_like, shape(m,), optional + Strictly positive 1D array of weights. + The weights are used in computing the weighted least-squares spline + fit. If the errors in the `x` values have standard deviation given by + the vector d, then `w` should be 1/d. Default is ``np.ones(m)``. + u : array_like, optional + An array of parameter values for the curve in the parametric form. + If not given, these values are calculated automatically, according to:: + + v[0] = 0 + v[i] = v[i-1] + distance(x[i], x[i-1]) + u[i] = v[i] / v[-1] + + ub, ue : float, optional + The end-points of the parameters interval. Default to ``u[0]`` and ``u[-1]``. + k : int, optional + Degree of the spline. Cubic splines, ``k=3``, are recommended. + Even values of `k` should be avoided especially with a small ``s`` value. + Default is ``k=3`` + s : float, optional + A smoothing condition. The amount of smoothness is determined by + satisfying the conditions:: + + sum((w * (g(u) - x))**2) <= s, + + where ``g(u)`` is the smoothed approximation to ``x``. The user can + use `s` to control the trade-off between closeness and smoothness + of fit. Larger ``s`` means more smoothing while smaller values of ``s`` + indicate less smoothing. + Recommended values of ``s`` depend on the weights, ``w``. If the weights + represent the inverse of the standard deviation of ``x``, then a good + ``s`` value should be found in the range ``(m - sqrt(2*m), m + sqrt(2*m))``, + where ``m`` is the number of data points in ``x`` and ``w``. + t : array_like, optional + The spline knots. If None (default), the knots will be constructed + automatically. + There must be at least ``2*k + 2`` and at most ``m + k + 1`` knots. + nest : int, optional + The target length of the knot vector. Should be between ``2*(k + 1)`` + (the minimum number of knots for a degree-``k`` spline), and + ``m + k + 1`` (the number of knots of the interpolating spline). + The actual number of knots returned by this routine may be slightly + larger than `nest`. + Default is None (no limit, add up to ``m + k + 1`` knots). + + Returns + ------- + spl : a `BSpline` instance + For `s=0`, ``spl(u) == x``. + For non-zero values of ``s``, `spl` represents the smoothed approximation + to ``x``, generally with fewer knots. + u : ndarray + The values of the parameters + + See Also + -------- + generate_knots : is used under the hood for generating the knots + make_splrep : the analog of this routine 1D functions + make_interp_spline : construct an interpolating spline (``s = 0``) + make_lsq_spline : construct the least-squares spline given the knot vector + splprep : a FITPACK analog of this routine + + Notes + ----- + Given a set of :math:`m` data points in :math:`D` dimensions, :math:`\vec{x}_j`, + with :math:`j=1, ..., m` and :math:`\vec{x}_j = (x_{j; 1}, ..., x_{j; D})`, + this routine constructs the parametric spline curve :math:`g_a(u)` with + :math:`a=1, ..., D`, to minimize the sum of jumps, :math:`D_{i; a}`, of the + ``k``-th derivative at the internal knots (:math:`u_b < t_i < u_e`), where + + .. math:: + + D_{i; a} = g_a^{(k)}(t_i + 0) - g_a^{(k)}(t_i - 0) + + Specifically, the routine constructs the spline function :math:`g(u)` which + minimizes + + .. math:: + + \sum_i \sum_{a=1}^D | D_{i; a} |^2 \to \mathrm{min} + + provided that + + .. math:: + + \sum_{j=1}^m \sum_{a=1}^D (w_j \times (g_a(u_j) - x_{j; a}))^2 \leqslant s + + where :math:`u_j` is the value of the parameter corresponding to the data point + :math:`(x_{j; 1}, ..., x_{j; D})`, and :math:`s > 0` is the input parameter. + + In other words, we balance maximizing the smoothness (measured as the jumps + of the derivative, the first criterion), and the deviation of :math:`g(u_j)` + from the data :math:`x_j` (the second criterion). + + Note that the summation in the second criterion is over all data points, + and in the first criterion it is over the internal spline knots (i.e. + those with ``ub < t[i] < ue``). The spline knots are in general a subset + of data, see `generate_knots` for details. + + .. versionadded:: 1.15.0 + + References + ---------- + .. [1] P. Dierckx, "Algorithms for smoothing data with periodic and + parametric splines, Computer Graphics and Image Processing", + 20 (1982) 171-184. + .. [2] P. Dierckx, "Curve and surface fitting with splines", Monographs on + Numerical Analysis, Oxford University Press, 1993. + """ + x = np.stack(x, axis=1) + + # construct the default parametrization of the curve + if u is None: + dp = (x[1:, :] - x[:-1, :])**2 + u = np.sqrt((dp).sum(axis=1)).cumsum() + u = np.r_[0, u / u[-1]] + + if s == 0: + if t is not None or w is not None or nest is not None: + raise ValueError("s==0 is for interpolation only") + return make_interp_spline(u, x.T, k=k, axis=1), u + + u, x, w, k, s, ub, ue = _validate_inputs(u, x, w, k, s, ub, ue, parametric=True) + + spl = _make_splrep_impl(u, x, w=w, xb=ub, xe=ue, k=k, s=s, t=t, nest=nest) + + # posprocess: `axis=1` so that spl(u).shape == np.shape(x) + # when `x` is a list of 1D arrays (cf original splPrep) + cc = spl.c.T + spl1 = BSpline(spl.t, cc, spl.k, axis=1) + + return spl1, u + diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_interpolate.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_interpolate.py new file mode 100644 index 0000000000000000000000000000000000000000..7558bd7db25cbd60206f908aabbcb6dc9c567fa4 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_interpolate.py @@ -0,0 +1,2248 @@ +__all__ = ['interp1d', 'interp2d', 'lagrange', 'PPoly', 'BPoly', 'NdPPoly'] + +from math import prod + +import numpy as np +from numpy import array, asarray, intp, poly1d, searchsorted + +import scipy.special as spec +from scipy._lib._util import copy_if_needed +from scipy.special import comb + +from . import _fitpack_py +from ._polyint import _Interpolator1D +from . import _ppoly +from ._interpnd import _ndim_coords_from_arrays +from ._bsplines import make_interp_spline, BSpline + + +def lagrange(x, w): + r""" + Return a Lagrange interpolating polynomial. + + Given two 1-D arrays `x` and `w,` returns the Lagrange interpolating + polynomial through the points ``(x, w)``. + + Warning: This implementation is numerically unstable. Do not expect to + be able to use more than about 20 points even if they are chosen optimally. + + Parameters + ---------- + x : array_like + `x` represents the x-coordinates of a set of datapoints. + w : array_like + `w` represents the y-coordinates of a set of datapoints, i.e., f(`x`). + + Returns + ------- + lagrange : `numpy.poly1d` instance + The Lagrange interpolating polynomial. + + Examples + -------- + Interpolate :math:`f(x) = x^3` by 3 points. + + >>> import numpy as np + >>> from scipy.interpolate import lagrange + >>> x = np.array([0, 1, 2]) + >>> y = x**3 + >>> poly = lagrange(x, y) + + Since there are only 3 points, Lagrange polynomial has degree 2. Explicitly, + it is given by + + .. math:: + + \begin{aligned} + L(x) &= 1\times \frac{x (x - 2)}{-1} + 8\times \frac{x (x-1)}{2} \\ + &= x (-2 + 3x) + \end{aligned} + + >>> from numpy.polynomial.polynomial import Polynomial + >>> Polynomial(poly.coef[::-1]).coef + array([ 0., -2., 3.]) + + >>> import matplotlib.pyplot as plt + >>> x_new = np.arange(0, 2.1, 0.1) + >>> plt.scatter(x, y, label='data') + >>> plt.plot(x_new, Polynomial(poly.coef[::-1])(x_new), label='Polynomial') + >>> plt.plot(x_new, 3*x_new**2 - 2*x_new + 0*x_new, + ... label=r"$3 x^2 - 2 x$", linestyle='-.') + >>> plt.legend() + >>> plt.show() + + """ + + M = len(x) + p = poly1d(0.0) + for j in range(M): + pt = poly1d(w[j]) + for k in range(M): + if k == j: + continue + fac = x[j]-x[k] + pt *= poly1d([1.0, -x[k]])/fac + p += pt + return p + + +# !! Need to find argument for keeping initialize. If it isn't +# !! found, get rid of it! + + +err_mesg = """\ +`interp2d` has been removed in SciPy 1.14.0. + +For legacy code, nearly bug-for-bug compatible replacements are +`RectBivariateSpline` on regular grids, and `bisplrep`/`bisplev` for +scattered 2D data. + +In new code, for regular grids use `RegularGridInterpolator` instead. +For scattered data, prefer `LinearNDInterpolator` or +`CloughTocher2DInterpolator`. + +For more details see +https://scipy.github.io/devdocs/tutorial/interpolate/interp_transition_guide.html +""" + +class interp2d: + """ + interp2d(x, y, z, kind='linear', copy=True, bounds_error=False, + fill_value=None) + + .. versionremoved:: 1.14.0 + + `interp2d` has been removed in SciPy 1.14.0. + + For legacy code, nearly bug-for-bug compatible replacements are + `RectBivariateSpline` on regular grids, and `bisplrep`/`bisplev` for + scattered 2D data. + + In new code, for regular grids use `RegularGridInterpolator` instead. + For scattered data, prefer `LinearNDInterpolator` or + `CloughTocher2DInterpolator`. + + For more details see :ref:`interp-transition-guide`. + """ + def __init__(self, x, y, z, kind='linear', copy=True, bounds_error=False, + fill_value=None): + raise NotImplementedError(err_mesg) + + +def _check_broadcast_up_to(arr_from, shape_to, name): + """Helper to check that arr_from broadcasts up to shape_to""" + shape_from = arr_from.shape + if len(shape_to) >= len(shape_from): + for t, f in zip(shape_to[::-1], shape_from[::-1]): + if f != 1 and f != t: + break + else: # all checks pass, do the upcasting that we need later + if arr_from.size != 1 and arr_from.shape != shape_to: + arr_from = np.ones(shape_to, arr_from.dtype) * arr_from + return arr_from.ravel() + # at least one check failed + raise ValueError(f'{name} argument must be able to broadcast up ' + f'to shape {shape_to} but had shape {shape_from}') + + +def _do_extrapolate(fill_value): + """Helper to check if fill_value == "extrapolate" without warnings""" + return (isinstance(fill_value, str) and + fill_value == 'extrapolate') + + +class interp1d(_Interpolator1D): + """ + Interpolate a 1-D function. + + .. legacy:: class + + For a guide to the intended replacements for `interp1d` see + :ref:`tutorial-interpolate_1Dsection`. + + `x` and `y` are arrays of values used to approximate some function f: + ``y = f(x)``. This class returns a function whose call method uses + interpolation to find the value of new points. + + Parameters + ---------- + x : (npoints, ) array_like + A 1-D array of real values. + y : (..., npoints, ...) array_like + A N-D array of real values. The length of `y` along the interpolation + axis must be equal to the length of `x`. Use the ``axis`` parameter + to select correct axis. Unlike other interpolators, the default + interpolation axis is the last axis of `y`. + kind : str or int, optional + Specifies the kind of interpolation as a string or as an integer + specifying the order of the spline interpolator to use. + The string has to be one of 'linear', 'nearest', 'nearest-up', 'zero', + 'slinear', 'quadratic', 'cubic', 'previous', or 'next'. 'zero', + 'slinear', 'quadratic' and 'cubic' refer to a spline interpolation of + zeroth, first, second or third order; 'previous' and 'next' simply + return the previous or next value of the point; 'nearest-up' and + 'nearest' differ when interpolating half-integers (e.g. 0.5, 1.5) + in that 'nearest-up' rounds up and 'nearest' rounds down. Default + is 'linear'. + axis : int, optional + Axis in the ``y`` array corresponding to the x-coordinate values. Unlike + other interpolators, defaults to ``axis=-1``. + copy : bool, optional + If ``True``, the class makes internal copies of x and y. If ``False``, + references to ``x`` and ``y`` are used if possible. The default is to copy. + bounds_error : bool, optional + If True, a ValueError is raised any time interpolation is attempted on + a value outside of the range of x (where extrapolation is + necessary). If False, out of bounds values are assigned `fill_value`. + By default, an error is raised unless ``fill_value="extrapolate"``. + fill_value : array-like or (array-like, array_like) or "extrapolate", optional + - if a ndarray (or float), this value will be used to fill in for + requested points outside of the data range. If not provided, then + the default is NaN. The array-like must broadcast properly to the + dimensions of the non-interpolation axes. + - If a two-element tuple, then the first element is used as a + fill value for ``x_new < x[0]`` and the second element is used for + ``x_new > x[-1]``. Anything that is not a 2-element tuple (e.g., + list or ndarray, regardless of shape) is taken to be a single + array-like argument meant to be used for both bounds as + ``below, above = fill_value, fill_value``. Using a two-element tuple + or ndarray requires ``bounds_error=False``. + + .. versionadded:: 0.17.0 + - If "extrapolate", then points outside the data range will be + extrapolated. + + .. versionadded:: 0.17.0 + assume_sorted : bool, optional + If False, values of `x` can be in any order and they are sorted first. + If True, `x` has to be an array of monotonically increasing values. + + Attributes + ---------- + fill_value + + Methods + ------- + __call__ + + See Also + -------- + splrep, splev + Spline interpolation/smoothing based on FITPACK. + UnivariateSpline : An object-oriented wrapper of the FITPACK routines. + interp2d : 2-D interpolation + + Notes + ----- + Calling `interp1d` with NaNs present in input values results in + undefined behaviour. + + Input values `x` and `y` must be convertible to `float` values like + `int` or `float`. + + If the values in `x` are not unique, the resulting behavior is + undefined and specific to the choice of `kind`, i.e., changing + `kind` will change the behavior for duplicates. + + + Examples + -------- + >>> import numpy as np + >>> import matplotlib.pyplot as plt + >>> from scipy import interpolate + >>> x = np.arange(0, 10) + >>> y = np.exp(-x/3.0) + >>> f = interpolate.interp1d(x, y) + + >>> xnew = np.arange(0, 9, 0.1) + >>> ynew = f(xnew) # use interpolation function returned by `interp1d` + >>> plt.plot(x, y, 'o', xnew, ynew, '-') + >>> plt.show() + """ + + def __init__(self, x, y, kind='linear', axis=-1, + copy=True, bounds_error=None, fill_value=np.nan, + assume_sorted=False): + """ Initialize a 1-D linear interpolation class.""" + _Interpolator1D.__init__(self, x, y, axis=axis) + + self.bounds_error = bounds_error # used by fill_value setter + + # `copy` keyword semantics changed in NumPy 2.0, once that is + # the minimum version this can use `copy=None`. + self.copy = copy + if not copy: + self.copy = copy_if_needed + + if kind in ['zero', 'slinear', 'quadratic', 'cubic']: + order = {'zero': 0, 'slinear': 1, + 'quadratic': 2, 'cubic': 3}[kind] + kind = 'spline' + elif isinstance(kind, int): + order = kind + kind = 'spline' + elif kind not in ('linear', 'nearest', 'nearest-up', 'previous', + 'next'): + raise NotImplementedError(f"{kind} is unsupported: Use fitpack " + "routines for other types.") + x = array(x, copy=self.copy) + y = array(y, copy=self.copy) + + if not assume_sorted: + ind = np.argsort(x, kind="mergesort") + x = x[ind] + y = np.take(y, ind, axis=axis) + + if x.ndim != 1: + raise ValueError("the x array must have exactly one dimension.") + if y.ndim == 0: + raise ValueError("the y array must have at least one dimension.") + + # Force-cast y to a floating-point type, if it's not yet one + if not issubclass(y.dtype.type, np.inexact): + y = y.astype(np.float64) + + # Backward compatibility + self.axis = axis % y.ndim + + # Interpolation goes internally along the first axis + self.y = y + self._y = self._reshape_yi(self.y) + self.x = x + del y, x # clean up namespace to prevent misuse; use attributes + self._kind = kind + + # Adjust to interpolation kind; store reference to *unbound* + # interpolation methods, in order to avoid circular references to self + # stored in the bound instance methods, and therefore delayed garbage + # collection. See: https://docs.python.org/reference/datamodel.html + if kind in ('linear', 'nearest', 'nearest-up', 'previous', 'next'): + # Make a "view" of the y array that is rotated to the interpolation + # axis. + minval = 1 + if kind == 'nearest': + # Do division before addition to prevent possible integer + # overflow + self._side = 'left' + self.x_bds = self.x / 2.0 + self.x_bds = self.x_bds[1:] + self.x_bds[:-1] + + self._call = self.__class__._call_nearest + elif kind == 'nearest-up': + # Do division before addition to prevent possible integer + # overflow + self._side = 'right' + self.x_bds = self.x / 2.0 + self.x_bds = self.x_bds[1:] + self.x_bds[:-1] + + self._call = self.__class__._call_nearest + elif kind == 'previous': + # Side for np.searchsorted and index for clipping + self._side = 'left' + self._ind = 0 + # Move x by one floating point value to the left + self._x_shift = np.nextafter(self.x, -np.inf) + self._call = self.__class__._call_previousnext + if _do_extrapolate(fill_value): + self._check_and_update_bounds_error_for_extrapolation() + # assume y is sorted by x ascending order here. + fill_value = (np.nan, np.take(self.y, -1, axis)) + elif kind == 'next': + self._side = 'right' + self._ind = 1 + # Move x by one floating point value to the right + self._x_shift = np.nextafter(self.x, np.inf) + self._call = self.__class__._call_previousnext + if _do_extrapolate(fill_value): + self._check_and_update_bounds_error_for_extrapolation() + # assume y is sorted by x ascending order here. + fill_value = (np.take(self.y, 0, axis), np.nan) + else: + # Check if we can delegate to numpy.interp (2x-10x faster). + np_dtypes = (np.dtype(np.float64), np.dtype(int)) + cond = self.x.dtype in np_dtypes and self.y.dtype in np_dtypes + cond = cond and self.y.ndim == 1 + cond = cond and not _do_extrapolate(fill_value) + + if cond: + self._call = self.__class__._call_linear_np + else: + self._call = self.__class__._call_linear + else: + minval = order + 1 + + rewrite_nan = False + xx, yy = self.x, self._y + if order > 1: + # Quadratic or cubic spline. If input contains even a single + # nan, then the output is all nans. We cannot just feed data + # with nans to make_interp_spline because it calls LAPACK. + # So, we make up a bogus x and y with no nans and use it + # to get the correct shape of the output, which we then fill + # with nans. + # For slinear or zero order spline, we just pass nans through. + mask = np.isnan(self.x) + if mask.any(): + sx = self.x[~mask] + if sx.size == 0: + raise ValueError("`x` array is all-nan") + xx = np.linspace(np.nanmin(self.x), + np.nanmax(self.x), + len(self.x)) + rewrite_nan = True + if np.isnan(self._y).any(): + yy = np.ones_like(self._y) + rewrite_nan = True + + self._spline = make_interp_spline(xx, yy, k=order, + check_finite=False) + if rewrite_nan: + self._call = self.__class__._call_nan_spline + else: + self._call = self.__class__._call_spline + + if len(self.x) < minval: + raise ValueError("x and y arrays must have at " + "least %d entries" % minval) + + self.fill_value = fill_value # calls the setter, can modify bounds_err + + @property + def fill_value(self): + """The fill value.""" + # backwards compat: mimic a public attribute + return self._fill_value_orig + + @fill_value.setter + def fill_value(self, fill_value): + # extrapolation only works for nearest neighbor and linear methods + if _do_extrapolate(fill_value): + self._check_and_update_bounds_error_for_extrapolation() + self._extrapolate = True + else: + broadcast_shape = (self.y.shape[:self.axis] + + self.y.shape[self.axis + 1:]) + if len(broadcast_shape) == 0: + broadcast_shape = (1,) + # it's either a pair (_below_range, _above_range) or a single value + # for both above and below range + if isinstance(fill_value, tuple) and len(fill_value) == 2: + below_above = [np.asarray(fill_value[0]), + np.asarray(fill_value[1])] + names = ('fill_value (below)', 'fill_value (above)') + for ii in range(2): + below_above[ii] = _check_broadcast_up_to( + below_above[ii], broadcast_shape, names[ii]) + else: + fill_value = np.asarray(fill_value) + below_above = [_check_broadcast_up_to( + fill_value, broadcast_shape, 'fill_value')] * 2 + self._fill_value_below, self._fill_value_above = below_above + self._extrapolate = False + if self.bounds_error is None: + self.bounds_error = True + # backwards compat: fill_value was a public attr; make it writeable + self._fill_value_orig = fill_value + + def _check_and_update_bounds_error_for_extrapolation(self): + if self.bounds_error: + raise ValueError("Cannot extrapolate and raise " + "at the same time.") + self.bounds_error = False + + def _call_linear_np(self, x_new): + # Note that out-of-bounds values are taken care of in self._evaluate + return np.interp(x_new, self.x, self.y) + + def _call_linear(self, x_new): + # 2. Find where in the original data, the values to interpolate + # would be inserted. + # Note: If x_new[n] == x[m], then m is returned by searchsorted. + x_new_indices = searchsorted(self.x, x_new) + + # 3. Clip x_new_indices so that they are within the range of + # self.x indices and at least 1. Removes mis-interpolation + # of x_new[n] = x[0] + x_new_indices = x_new_indices.clip(1, len(self.x)-1).astype(int) + + # 4. Calculate the slope of regions that each x_new value falls in. + lo = x_new_indices - 1 + hi = x_new_indices + + x_lo = self.x[lo] + x_hi = self.x[hi] + y_lo = self._y[lo] + y_hi = self._y[hi] + + # Note that the following two expressions rely on the specifics of the + # broadcasting semantics. + slope = (y_hi - y_lo) / (x_hi - x_lo)[:, None] + + # 5. Calculate the actual value for each entry in x_new. + y_new = slope*(x_new - x_lo)[:, None] + y_lo + + return y_new + + def _call_nearest(self, x_new): + """ Find nearest neighbor interpolated y_new = f(x_new).""" + + # 2. Find where in the averaged data the values to interpolate + # would be inserted. + # Note: use side='left' (right) to searchsorted() to define the + # halfway point to be nearest to the left (right) neighbor + x_new_indices = searchsorted(self.x_bds, x_new, side=self._side) + + # 3. Clip x_new_indices so that they are within the range of x indices. + x_new_indices = x_new_indices.clip(0, len(self.x)-1).astype(intp) + + # 4. Calculate the actual value for each entry in x_new. + y_new = self._y[x_new_indices] + + return y_new + + def _call_previousnext(self, x_new): + """Use previous/next neighbor of x_new, y_new = f(x_new).""" + + # 1. Get index of left/right value + x_new_indices = searchsorted(self._x_shift, x_new, side=self._side) + + # 2. Clip x_new_indices so that they are within the range of x indices. + x_new_indices = x_new_indices.clip(1-self._ind, + len(self.x)-self._ind).astype(intp) + + # 3. Calculate the actual value for each entry in x_new. + y_new = self._y[x_new_indices+self._ind-1] + + return y_new + + def _call_spline(self, x_new): + return self._spline(x_new) + + def _call_nan_spline(self, x_new): + out = self._spline(x_new) + out[...] = np.nan + return out + + def _evaluate(self, x_new): + # 1. Handle values in x_new that are outside of x. Throw error, + # or return a list of mask array indicating the outofbounds values. + # The behavior is set by the bounds_error variable. + x_new = asarray(x_new) + y_new = self._call(self, x_new) + if not self._extrapolate: + below_bounds, above_bounds = self._check_bounds(x_new) + if len(y_new) > 0: + # Note fill_value must be broadcast up to the proper size + # and flattened to work here + y_new[below_bounds] = self._fill_value_below + y_new[above_bounds] = self._fill_value_above + return y_new + + def _check_bounds(self, x_new): + """Check the inputs for being in the bounds of the interpolated data. + + Parameters + ---------- + x_new : array + + Returns + ------- + out_of_bounds : bool array + The mask on x_new of values that are out of the bounds. + """ + + # If self.bounds_error is True, we raise an error if any x_new values + # fall outside the range of x. Otherwise, we return an array indicating + # which values are outside the boundary region. + below_bounds = x_new < self.x[0] + above_bounds = x_new > self.x[-1] + + if self.bounds_error and below_bounds.any(): + below_bounds_value = x_new[np.argmax(below_bounds)] + raise ValueError(f"A value ({below_bounds_value}) in x_new is below " + f"the interpolation range's minimum value ({self.x[0]}).") + if self.bounds_error and above_bounds.any(): + above_bounds_value = x_new[np.argmax(above_bounds)] + raise ValueError(f"A value ({above_bounds_value}) in x_new is above " + f"the interpolation range's maximum value ({self.x[-1]}).") + + # !! Should we emit a warning if some values are out of bounds? + # !! matlab does not. + return below_bounds, above_bounds + + +class _PPolyBase: + """Base class for piecewise polynomials.""" + __slots__ = ('c', 'x', 'extrapolate', 'axis') + + def __init__(self, c, x, extrapolate=None, axis=0): + self.c = np.asarray(c) + self.x = np.ascontiguousarray(x, dtype=np.float64) + + if extrapolate is None: + extrapolate = True + elif extrapolate != 'periodic': + extrapolate = bool(extrapolate) + self.extrapolate = extrapolate + + if self.c.ndim < 2: + raise ValueError("Coefficients array must be at least " + "2-dimensional.") + + if not (0 <= axis < self.c.ndim - 1): + raise ValueError(f"axis={axis} must be between 0 and {self.c.ndim-1}") + + self.axis = axis + if axis != 0: + # move the interpolation axis to be the first one in self.c + # More specifically, the target shape for self.c is (k, m, ...), + # and axis !=0 means that we have c.shape (..., k, m, ...) + # ^ + # axis + # So we roll two of them. + self.c = np.moveaxis(self.c, axis+1, 0) + self.c = np.moveaxis(self.c, axis+1, 0) + + if self.x.ndim != 1: + raise ValueError("x must be 1-dimensional") + if self.x.size < 2: + raise ValueError("at least 2 breakpoints are needed") + if self.c.ndim < 2: + raise ValueError("c must have at least 2 dimensions") + if self.c.shape[0] == 0: + raise ValueError("polynomial must be at least of order 0") + if self.c.shape[1] != self.x.size-1: + raise ValueError("number of coefficients != len(x)-1") + dx = np.diff(self.x) + if not (np.all(dx >= 0) or np.all(dx <= 0)): + raise ValueError("`x` must be strictly increasing or decreasing.") + + dtype = self._get_dtype(self.c.dtype) + self.c = np.ascontiguousarray(self.c, dtype=dtype) + + def _get_dtype(self, dtype): + if np.issubdtype(dtype, np.complexfloating) \ + or np.issubdtype(self.c.dtype, np.complexfloating): + return np.complex128 + else: + return np.float64 + + @classmethod + def construct_fast(cls, c, x, extrapolate=None, axis=0): + """ + Construct the piecewise polynomial without making checks. + + Takes the same parameters as the constructor. Input arguments + ``c`` and ``x`` must be arrays of the correct shape and type. The + ``c`` array can only be of dtypes float and complex, and ``x`` + array must have dtype float. + """ + self = object.__new__(cls) + self.c = c + self.x = x + self.axis = axis + if extrapolate is None: + extrapolate = True + self.extrapolate = extrapolate + return self + + def _ensure_c_contiguous(self): + """ + c and x may be modified by the user. The Cython code expects + that they are C contiguous. + """ + if not self.x.flags.c_contiguous: + self.x = self.x.copy() + if not self.c.flags.c_contiguous: + self.c = self.c.copy() + + def extend(self, c, x): + """ + Add additional breakpoints and coefficients to the polynomial. + + Parameters + ---------- + c : ndarray, size (k, m, ...) + Additional coefficients for polynomials in intervals. Note that + the first additional interval will be formed using one of the + ``self.x`` end points. + x : ndarray, size (m,) + Additional breakpoints. Must be sorted in the same order as + ``self.x`` and either to the right or to the left of the current + breakpoints. + + Notes + ----- + This method is not thread safe and must not be executed concurrently + with other methods available in this class. Doing so may cause + unexpected errors or numerical output mismatches. + """ + + c = np.asarray(c) + x = np.asarray(x) + + if c.ndim < 2: + raise ValueError("invalid dimensions for c") + if x.ndim != 1: + raise ValueError("invalid dimensions for x") + if x.shape[0] != c.shape[1]: + raise ValueError(f"Shapes of x {x.shape} and c {c.shape} are incompatible") + if c.shape[2:] != self.c.shape[2:] or c.ndim != self.c.ndim: + raise ValueError( + f"Shapes of c {c.shape} and self.c {self.c.shape} are incompatible" + ) + + if c.size == 0: + return + + dx = np.diff(x) + if not (np.all(dx >= 0) or np.all(dx <= 0)): + raise ValueError("`x` is not sorted.") + + if self.x[-1] >= self.x[0]: + if not x[-1] >= x[0]: + raise ValueError("`x` is in the different order " + "than `self.x`.") + + if x[0] >= self.x[-1]: + action = 'append' + elif x[-1] <= self.x[0]: + action = 'prepend' + else: + raise ValueError("`x` is neither on the left or on the right " + "from `self.x`.") + else: + if not x[-1] <= x[0]: + raise ValueError("`x` is in the different order " + "than `self.x`.") + + if x[0] <= self.x[-1]: + action = 'append' + elif x[-1] >= self.x[0]: + action = 'prepend' + else: + raise ValueError("`x` is neither on the left or on the right " + "from `self.x`.") + + dtype = self._get_dtype(c.dtype) + + k2 = max(c.shape[0], self.c.shape[0]) + c2 = np.zeros((k2, self.c.shape[1] + c.shape[1]) + self.c.shape[2:], + dtype=dtype) + + if action == 'append': + c2[k2-self.c.shape[0]:, :self.c.shape[1]] = self.c + c2[k2-c.shape[0]:, self.c.shape[1]:] = c + self.x = np.r_[self.x, x] + elif action == 'prepend': + c2[k2-self.c.shape[0]:, :c.shape[1]] = c + c2[k2-c.shape[0]:, c.shape[1]:] = self.c + self.x = np.r_[x, self.x] + + self.c = c2 + + def __call__(self, x, nu=0, extrapolate=None): + """ + Evaluate the piecewise polynomial or its derivative. + + Parameters + ---------- + x : array_like + Points to evaluate the interpolant at. + nu : int, optional + Order of derivative to evaluate. Must be non-negative. + extrapolate : {bool, 'periodic', None}, optional + If bool, determines whether to extrapolate to out-of-bounds points + based on first and last intervals, or to return NaNs. + If 'periodic', periodic extrapolation is used. + If None (default), use `self.extrapolate`. + + Returns + ------- + y : array_like + Interpolated values. Shape is determined by replacing + the interpolation axis in the original array with the shape of x. + + Notes + ----- + Derivatives are evaluated piecewise for each polynomial + segment, even if the polynomial is not differentiable at the + breakpoints. The polynomial intervals are considered half-open, + ``[a, b)``, except for the last interval which is closed + ``[a, b]``. + """ + if extrapolate is None: + extrapolate = self.extrapolate + x = np.asarray(x) + x_shape, x_ndim = x.shape, x.ndim + x = np.ascontiguousarray(x.ravel(), dtype=np.float64) + + # With periodic extrapolation we map x to the segment + # [self.x[0], self.x[-1]]. + if extrapolate == 'periodic': + x = self.x[0] + (x - self.x[0]) % (self.x[-1] - self.x[0]) + extrapolate = False + + out = np.empty((len(x), prod(self.c.shape[2:])), dtype=self.c.dtype) + self._ensure_c_contiguous() + self._evaluate(x, nu, extrapolate, out) + out = out.reshape(x_shape + self.c.shape[2:]) + if self.axis != 0: + # transpose to move the calculated values to the interpolation axis + l = list(range(out.ndim)) + l = l[x_ndim:x_ndim+self.axis] + l[:x_ndim] + l[x_ndim+self.axis:] + out = out.transpose(l) + return out + + +class PPoly(_PPolyBase): + """ + Piecewise polynomial in terms of coefficients and breakpoints + + The polynomial between ``x[i]`` and ``x[i + 1]`` is written in the + local power basis:: + + S = sum(c[m, i] * (xp - x[i])**(k-m) for m in range(k+1)) + + where ``k`` is the degree of the polynomial. + + Parameters + ---------- + c : ndarray, shape (k, m, ...) + Polynomial coefficients, order `k` and `m` intervals. + x : ndarray, shape (m+1,) + Polynomial breakpoints. Must be sorted in either increasing or + decreasing order. + extrapolate : bool or 'periodic', optional + If bool, determines whether to extrapolate to out-of-bounds points + based on first and last intervals, or to return NaNs. If 'periodic', + periodic extrapolation is used. Default is True. + axis : int, optional + Interpolation axis. Default is zero. + + Attributes + ---------- + x : ndarray + Breakpoints. + c : ndarray + Coefficients of the polynomials. They are reshaped + to a 3-D array with the last dimension representing + the trailing dimensions of the original coefficient array. + axis : int + Interpolation axis. + + Methods + ------- + __call__ + derivative + antiderivative + integrate + solve + roots + extend + from_spline + from_bernstein_basis + construct_fast + + See also + -------- + BPoly : piecewise polynomials in the Bernstein basis + + Notes + ----- + High-order polynomials in the power basis can be numerically + unstable. Precision problems can start to appear for orders + larger than 20-30. + """ + + def _evaluate(self, x, nu, extrapolate, out): + _ppoly.evaluate(self.c.reshape(self.c.shape[0], self.c.shape[1], -1), + self.x, x, nu, bool(extrapolate), out) + + def derivative(self, nu=1): + """ + Construct a new piecewise polynomial representing the derivative. + + Parameters + ---------- + nu : int, optional + Order of derivative to evaluate. Default is 1, i.e., compute the + first derivative. If negative, the antiderivative is returned. + + Returns + ------- + pp : PPoly + Piecewise polynomial of order k2 = k - n representing the derivative + of this polynomial. + + Notes + ----- + Derivatives are evaluated piecewise for each polynomial + segment, even if the polynomial is not differentiable at the + breakpoints. The polynomial intervals are considered half-open, + ``[a, b)``, except for the last interval which is closed + ``[a, b]``. + """ + if nu < 0: + return self.antiderivative(-nu) + + # reduce order + if nu == 0: + c2 = self.c.copy() + else: + c2 = self.c[:-nu, :].copy() + + if c2.shape[0] == 0: + # derivative of order 0 is zero + c2 = np.zeros((1,) + c2.shape[1:], dtype=c2.dtype) + + # multiply by the correct rising factorials + factor = spec.poch(np.arange(c2.shape[0], 0, -1), nu) + c2 *= factor[(slice(None),) + (None,)*(c2.ndim-1)] + + # construct a compatible polynomial + return self.construct_fast(c2, self.x, self.extrapolate, self.axis) + + def antiderivative(self, nu=1): + """ + Construct a new piecewise polynomial representing the antiderivative. + + Antiderivative is also the indefinite integral of the function, + and derivative is its inverse operation. + + Parameters + ---------- + nu : int, optional + Order of antiderivative to evaluate. Default is 1, i.e., compute + the first integral. If negative, the derivative is returned. + + Returns + ------- + pp : PPoly + Piecewise polynomial of order k2 = k + n representing + the antiderivative of this polynomial. + + Notes + ----- + The antiderivative returned by this function is continuous and + continuously differentiable to order n-1, up to floating point + rounding error. + + If antiderivative is computed and ``self.extrapolate='periodic'``, + it will be set to False for the returned instance. This is done because + the antiderivative is no longer periodic and its correct evaluation + outside of the initially given x interval is difficult. + """ + if nu <= 0: + return self.derivative(-nu) + + c = np.zeros((self.c.shape[0] + nu, self.c.shape[1]) + self.c.shape[2:], + dtype=self.c.dtype) + c[:-nu] = self.c + + # divide by the correct rising factorials + factor = spec.poch(np.arange(self.c.shape[0], 0, -1), nu) + c[:-nu] /= factor[(slice(None),) + (None,)*(c.ndim-1)] + + # fix continuity of added degrees of freedom + self._ensure_c_contiguous() + _ppoly.fix_continuity(c.reshape(c.shape[0], c.shape[1], -1), + self.x, nu - 1) + + if self.extrapolate == 'periodic': + extrapolate = False + else: + extrapolate = self.extrapolate + + # construct a compatible polynomial + return self.construct_fast(c, self.x, extrapolate, self.axis) + + def integrate(self, a, b, extrapolate=None): + """ + Compute a definite integral over a piecewise polynomial. + + Parameters + ---------- + a : float + Lower integration bound + b : float + Upper integration bound + extrapolate : {bool, 'periodic', None}, optional + If bool, determines whether to extrapolate to out-of-bounds points + based on first and last intervals, or to return NaNs. + If 'periodic', periodic extrapolation is used. + If None (default), use `self.extrapolate`. + + Returns + ------- + ig : array_like + Definite integral of the piecewise polynomial over [a, b] + """ + if extrapolate is None: + extrapolate = self.extrapolate + + # Swap integration bounds if needed + sign = 1 + if b < a: + a, b = b, a + sign = -1 + + range_int = np.empty((prod(self.c.shape[2:]),), dtype=self.c.dtype) + self._ensure_c_contiguous() + + # Compute the integral. + if extrapolate == 'periodic': + # Split the integral into the part over period (can be several + # of them) and the remaining part. + + xs, xe = self.x[0], self.x[-1] + period = xe - xs + interval = b - a + n_periods, left = divmod(interval, period) + + if n_periods > 0: + _ppoly.integrate( + self.c.reshape(self.c.shape[0], self.c.shape[1], -1), + self.x, xs, xe, False, out=range_int) + range_int *= n_periods + else: + range_int.fill(0) + + # Map a to [xs, xe], b is always a + left. + a = xs + (a - xs) % period + b = a + left + + # If b <= xe then we need to integrate over [a, b], otherwise + # over [a, xe] and from xs to what is remained. + remainder_int = np.empty_like(range_int) + if b <= xe: + _ppoly.integrate( + self.c.reshape(self.c.shape[0], self.c.shape[1], -1), + self.x, a, b, False, out=remainder_int) + range_int += remainder_int + else: + _ppoly.integrate( + self.c.reshape(self.c.shape[0], self.c.shape[1], -1), + self.x, a, xe, False, out=remainder_int) + range_int += remainder_int + + _ppoly.integrate( + self.c.reshape(self.c.shape[0], self.c.shape[1], -1), + self.x, xs, xs + left + a - xe, False, out=remainder_int) + range_int += remainder_int + else: + _ppoly.integrate( + self.c.reshape(self.c.shape[0], self.c.shape[1], -1), + self.x, a, b, bool(extrapolate), out=range_int) + + # Return + range_int *= sign + return range_int.reshape(self.c.shape[2:]) + + def solve(self, y=0., discontinuity=True, extrapolate=None): + """ + Find real solutions of the equation ``pp(x) == y``. + + Parameters + ---------- + y : float, optional + Right-hand side. Default is zero. + discontinuity : bool, optional + Whether to report sign changes across discontinuities at + breakpoints as roots. + extrapolate : {bool, 'periodic', None}, optional + If bool, determines whether to return roots from the polynomial + extrapolated based on first and last intervals, 'periodic' works + the same as False. If None (default), use `self.extrapolate`. + + Returns + ------- + roots : ndarray + Roots of the polynomial(s). + + If the PPoly object describes multiple polynomials, the + return value is an object array whose each element is an + ndarray containing the roots. + + Notes + ----- + This routine works only on real-valued polynomials. + + If the piecewise polynomial contains sections that are + identically zero, the root list will contain the start point + of the corresponding interval, followed by a ``nan`` value. + + If the polynomial is discontinuous across a breakpoint, and + there is a sign change across the breakpoint, this is reported + if the `discont` parameter is True. + + Examples + -------- + + Finding roots of ``[x**2 - 1, (x - 1)**2]`` defined on intervals + ``[-2, 1], [1, 2]``: + + >>> import numpy as np + >>> from scipy.interpolate import PPoly + >>> pp = PPoly(np.array([[1, -4, 3], [1, 0, 0]]).T, [-2, 1, 2]) + >>> pp.solve() + array([-1., 1.]) + """ + if extrapolate is None: + extrapolate = self.extrapolate + + self._ensure_c_contiguous() + + if np.issubdtype(self.c.dtype, np.complexfloating): + raise ValueError("Root finding is only for " + "real-valued polynomials") + + y = float(y) + r = _ppoly.real_roots(self.c.reshape(self.c.shape[0], self.c.shape[1], -1), + self.x, y, bool(discontinuity), + bool(extrapolate)) + if self.c.ndim == 2: + return r[0] + else: + r2 = np.empty(prod(self.c.shape[2:]), dtype=object) + # this for-loop is equivalent to ``r2[...] = r``, but that's broken + # in NumPy 1.6.0 + for ii, root in enumerate(r): + r2[ii] = root + + return r2.reshape(self.c.shape[2:]) + + def roots(self, discontinuity=True, extrapolate=None): + """ + Find real roots of the piecewise polynomial. + + Parameters + ---------- + discontinuity : bool, optional + Whether to report sign changes across discontinuities at + breakpoints as roots. + extrapolate : {bool, 'periodic', None}, optional + If bool, determines whether to return roots from the polynomial + extrapolated based on first and last intervals, 'periodic' works + the same as False. If None (default), use `self.extrapolate`. + + Returns + ------- + roots : ndarray + Roots of the polynomial(s). + + If the PPoly object describes multiple polynomials, the + return value is an object array whose each element is an + ndarray containing the roots. + + See Also + -------- + PPoly.solve + """ + return self.solve(0, discontinuity, extrapolate) + + @classmethod + def from_spline(cls, tck, extrapolate=None): + """ + Construct a piecewise polynomial from a spline + + Parameters + ---------- + tck + A spline, as returned by `splrep` or a BSpline object. + extrapolate : bool or 'periodic', optional + If bool, determines whether to extrapolate to out-of-bounds points + based on first and last intervals, or to return NaNs. + If 'periodic', periodic extrapolation is used. Default is True. + + Examples + -------- + Construct an interpolating spline and convert it to a `PPoly` instance + + >>> import numpy as np + >>> from scipy.interpolate import splrep, PPoly + >>> x = np.linspace(0, 1, 11) + >>> y = np.sin(2*np.pi*x) + >>> tck = splrep(x, y, s=0) + >>> p = PPoly.from_spline(tck) + >>> isinstance(p, PPoly) + True + + Note that this function only supports 1D splines out of the box. + + If the ``tck`` object represents a parametric spline (e.g. constructed + by `splprep` or a `BSpline` with ``c.ndim > 1``), you will need to loop + over the dimensions manually. + + >>> from scipy.interpolate import splprep, splev + >>> t = np.linspace(0, 1, 11) + >>> x = np.sin(2*np.pi*t) + >>> y = np.cos(2*np.pi*t) + >>> (t, c, k), u = splprep([x, y], s=0) + + Note that ``c`` is a list of two arrays of length 11. + + >>> unew = np.arange(0, 1.01, 0.01) + >>> out = splev(unew, (t, c, k)) + + To convert this spline to the power basis, we convert each + component of the list of b-spline coefficients, ``c``, into the + corresponding cubic polynomial. + + >>> polys = [PPoly.from_spline((t, cj, k)) for cj in c] + >>> polys[0].c.shape + (4, 14) + + Note that the coefficients of the polynomials `polys` are in the + power basis and their dimensions reflect just that: here 4 is the order + (degree+1), and 14 is the number of intervals---which is nothing but + the length of the knot array of the original `tck` minus one. + + Optionally, we can stack the components into a single `PPoly` along + the third dimension: + + >>> cc = np.dstack([p.c for p in polys]) # has shape = (4, 14, 2) + >>> poly = PPoly(cc, polys[0].x) + >>> np.allclose(poly(unew).T, # note the transpose to match `splev` + ... out, atol=1e-15) + True + + """ + if isinstance(tck, BSpline): + t, c, k = tck.tck + if extrapolate is None: + extrapolate = tck.extrapolate + else: + t, c, k = tck + + cvals = np.empty((k + 1, len(t)-1), dtype=c.dtype) + for m in range(k, -1, -1): + y = _fitpack_py.splev(t[:-1], tck, der=m) + cvals[k - m, :] = y/spec.gamma(m+1) + + return cls.construct_fast(cvals, t, extrapolate) + + @classmethod + def from_bernstein_basis(cls, bp, extrapolate=None): + """ + Construct a piecewise polynomial in the power basis + from a polynomial in Bernstein basis. + + Parameters + ---------- + bp : BPoly + A Bernstein basis polynomial, as created by BPoly + extrapolate : bool or 'periodic', optional + If bool, determines whether to extrapolate to out-of-bounds points + based on first and last intervals, or to return NaNs. + If 'periodic', periodic extrapolation is used. Default is True. + """ + if not isinstance(bp, BPoly): + raise TypeError(f".from_bernstein_basis only accepts BPoly instances. " + f"Got {type(bp)} instead.") + + dx = np.diff(bp.x) + k = bp.c.shape[0] - 1 # polynomial order + + rest = (None,)*(bp.c.ndim-2) + + c = np.zeros_like(bp.c) + for a in range(k+1): + factor = (-1)**a * comb(k, a) * bp.c[a] + for s in range(a, k+1): + val = comb(k-a, s-a) * (-1)**s + c[k-s] += factor * val / dx[(slice(None),)+rest]**s + + if extrapolate is None: + extrapolate = bp.extrapolate + + return cls.construct_fast(c, bp.x, extrapolate, bp.axis) + + +class BPoly(_PPolyBase): + """Piecewise polynomial in terms of coefficients and breakpoints. + + The polynomial between ``x[i]`` and ``x[i + 1]`` is written in the + Bernstein polynomial basis:: + + S = sum(c[a, i] * b(a, k; x) for a in range(k+1)), + + where ``k`` is the degree of the polynomial, and:: + + b(a, k; x) = binom(k, a) * t**a * (1 - t)**(k - a), + + with ``t = (x - x[i]) / (x[i+1] - x[i])`` and ``binom`` is the binomial + coefficient. + + Parameters + ---------- + c : ndarray, shape (k, m, ...) + Polynomial coefficients, order `k` and `m` intervals + x : ndarray, shape (m+1,) + Polynomial breakpoints. Must be sorted in either increasing or + decreasing order. + extrapolate : bool, optional + If bool, determines whether to extrapolate to out-of-bounds points + based on first and last intervals, or to return NaNs. If 'periodic', + periodic extrapolation is used. Default is True. + axis : int, optional + Interpolation axis. Default is zero. + + Attributes + ---------- + x : ndarray + Breakpoints. + c : ndarray + Coefficients of the polynomials. They are reshaped + to a 3-D array with the last dimension representing + the trailing dimensions of the original coefficient array. + axis : int + Interpolation axis. + + Methods + ------- + __call__ + extend + derivative + antiderivative + integrate + construct_fast + from_power_basis + from_derivatives + + See also + -------- + PPoly : piecewise polynomials in the power basis + + Notes + ----- + Properties of Bernstein polynomials are well documented in the literature, + see for example [1]_ [2]_ [3]_. + + References + ---------- + .. [1] https://en.wikipedia.org/wiki/Bernstein_polynomial + + .. [2] Kenneth I. Joy, Bernstein polynomials, + http://www.idav.ucdavis.edu/education/CAGDNotes/Bernstein-Polynomials.pdf + + .. [3] E. H. Doha, A. H. Bhrawy, and M. A. Saker, Boundary Value Problems, + vol 2011, article ID 829546, :doi:`10.1155/2011/829543`. + + Examples + -------- + >>> from scipy.interpolate import BPoly + >>> x = [0, 1] + >>> c = [[1], [2], [3]] + >>> bp = BPoly(c, x) + + This creates a 2nd order polynomial + + .. math:: + + B(x) = 1 \\times b_{0, 2}(x) + 2 \\times b_{1, 2}(x) + 3 + \\times b_{2, 2}(x) \\\\ + = 1 \\times (1-x)^2 + 2 \\times 2 x (1 - x) + 3 \\times x^2 + + """ # noqa: E501 + + def _evaluate(self, x, nu, extrapolate, out): + _ppoly.evaluate_bernstein( + self.c.reshape(self.c.shape[0], self.c.shape[1], -1), + self.x, x, nu, bool(extrapolate), out) + + def derivative(self, nu=1): + """ + Construct a new piecewise polynomial representing the derivative. + + Parameters + ---------- + nu : int, optional + Order of derivative to evaluate. Default is 1, i.e., compute the + first derivative. If negative, the antiderivative is returned. + + Returns + ------- + bp : BPoly + Piecewise polynomial of order k - nu representing the derivative of + this polynomial. + + """ + if nu < 0: + return self.antiderivative(-nu) + + if nu > 1: + bp = self + for k in range(nu): + bp = bp.derivative() + return bp + + # reduce order + if nu == 0: + c2 = self.c.copy() + else: + # For a polynomial + # B(x) = \sum_{a=0}^{k} c_a b_{a, k}(x), + # we use the fact that + # b'_{a, k} = k ( b_{a-1, k-1} - b_{a, k-1} ), + # which leads to + # B'(x) = \sum_{a=0}^{k-1} (c_{a+1} - c_a) b_{a, k-1} + # + # finally, for an interval [y, y + dy] with dy != 1, + # we need to correct for an extra power of dy + + rest = (None,)*(self.c.ndim-2) + + k = self.c.shape[0] - 1 + dx = np.diff(self.x)[(None, slice(None))+rest] + c2 = k * np.diff(self.c, axis=0) / dx + + if c2.shape[0] == 0: + # derivative of order 0 is zero + c2 = np.zeros((1,) + c2.shape[1:], dtype=c2.dtype) + + # construct a compatible polynomial + return self.construct_fast(c2, self.x, self.extrapolate, self.axis) + + def antiderivative(self, nu=1): + """ + Construct a new piecewise polynomial representing the antiderivative. + + Parameters + ---------- + nu : int, optional + Order of antiderivative to evaluate. Default is 1, i.e., compute + the first integral. If negative, the derivative is returned. + + Returns + ------- + bp : BPoly + Piecewise polynomial of order k + nu representing the + antiderivative of this polynomial. + + Notes + ----- + If antiderivative is computed and ``self.extrapolate='periodic'``, + it will be set to False for the returned instance. This is done because + the antiderivative is no longer periodic and its correct evaluation + outside of the initially given x interval is difficult. + """ + if nu <= 0: + return self.derivative(-nu) + + if nu > 1: + bp = self + for k in range(nu): + bp = bp.antiderivative() + return bp + + # Construct the indefinite integrals on individual intervals + c, x = self.c, self.x + k = c.shape[0] + c2 = np.zeros((k+1,) + c.shape[1:], dtype=c.dtype) + + c2[1:, ...] = np.cumsum(c, axis=0) / k + delta = x[1:] - x[:-1] + c2 *= delta[(None, slice(None)) + (None,)*(c.ndim-2)] + + # Now fix continuity: on the very first interval, take the integration + # constant to be zero; on an interval [x_j, x_{j+1}) with j>0, + # the integration constant is then equal to the jump of the `bp` at x_j. + # The latter is given by the coefficient of B_{n+1, n+1} + # *on the previous interval* (other B. polynomials are zero at the + # breakpoint). Finally, use the fact that BPs form a partition of unity. + c2[:,1:] += np.cumsum(c2[k, :], axis=0)[:-1] + + if self.extrapolate == 'periodic': + extrapolate = False + else: + extrapolate = self.extrapolate + + return self.construct_fast(c2, x, extrapolate, axis=self.axis) + + def integrate(self, a, b, extrapolate=None): + """ + Compute a definite integral over a piecewise polynomial. + + Parameters + ---------- + a : float + Lower integration bound + b : float + Upper integration bound + extrapolate : {bool, 'periodic', None}, optional + Whether to extrapolate to out-of-bounds points based on first + and last intervals, or to return NaNs. If 'periodic', periodic + extrapolation is used. If None (default), use `self.extrapolate`. + + Returns + ------- + array_like + Definite integral of the piecewise polynomial over [a, b] + + """ + # XXX: can probably use instead the fact that + # \int_0^{1} B_{j, n}(x) \dx = 1/(n+1) + ib = self.antiderivative() + if extrapolate is None: + extrapolate = self.extrapolate + + # ib.extrapolate shouldn't be 'periodic', it is converted to + # False for 'periodic. in antiderivative() call. + if extrapolate != 'periodic': + ib.extrapolate = extrapolate + + if extrapolate == 'periodic': + # Split the integral into the part over period (can be several + # of them) and the remaining part. + + # For simplicity and clarity convert to a <= b case. + if a <= b: + sign = 1 + else: + a, b = b, a + sign = -1 + + xs, xe = self.x[0], self.x[-1] + period = xe - xs + interval = b - a + n_periods, left = divmod(interval, period) + res = n_periods * (ib(xe) - ib(xs)) + + # Map a and b to [xs, xe]. + a = xs + (a - xs) % period + b = a + left + + # If b <= xe then we need to integrate over [a, b], otherwise + # over [a, xe] and from xs to what is remained. + if b <= xe: + res += ib(b) - ib(a) + else: + res += ib(xe) - ib(a) + ib(xs + left + a - xe) - ib(xs) + + return sign * res + else: + return ib(b) - ib(a) + + def extend(self, c, x): + k = max(self.c.shape[0], c.shape[0]) + self.c = self._raise_degree(self.c, k - self.c.shape[0]) + c = self._raise_degree(c, k - c.shape[0]) + return _PPolyBase.extend(self, c, x) + extend.__doc__ = _PPolyBase.extend.__doc__ + + @classmethod + def from_power_basis(cls, pp, extrapolate=None): + """ + Construct a piecewise polynomial in Bernstein basis + from a power basis polynomial. + + Parameters + ---------- + pp : PPoly + A piecewise polynomial in the power basis + extrapolate : bool or 'periodic', optional + If bool, determines whether to extrapolate to out-of-bounds points + based on first and last intervals, or to return NaNs. + If 'periodic', periodic extrapolation is used. Default is True. + """ + if not isinstance(pp, PPoly): + raise TypeError(f".from_power_basis only accepts PPoly instances. " + f"Got {type(pp)} instead.") + + dx = np.diff(pp.x) + k = pp.c.shape[0] - 1 # polynomial order + + rest = (None,)*(pp.c.ndim-2) + + c = np.zeros_like(pp.c) + for a in range(k+1): + factor = pp.c[a] / comb(k, k-a) * dx[(slice(None),)+rest]**(k-a) + for j in range(k-a, k+1): + c[j] += factor * comb(j, k-a) + + if extrapolate is None: + extrapolate = pp.extrapolate + + return cls.construct_fast(c, pp.x, extrapolate, pp.axis) + + @classmethod + def from_derivatives(cls, xi, yi, orders=None, extrapolate=None): + """Construct a piecewise polynomial in the Bernstein basis, + compatible with the specified values and derivatives at breakpoints. + + Parameters + ---------- + xi : array_like + sorted 1-D array of x-coordinates + yi : array_like or list of array_likes + ``yi[i][j]`` is the ``j``\\ th derivative known at ``xi[i]`` + orders : None or int or array_like of ints. Default: None. + Specifies the degree of local polynomials. If not None, some + derivatives are ignored. + extrapolate : bool or 'periodic', optional + If bool, determines whether to extrapolate to out-of-bounds points + based on first and last intervals, or to return NaNs. + If 'periodic', periodic extrapolation is used. Default is True. + + Notes + ----- + If ``k`` derivatives are specified at a breakpoint ``x``, the + constructed polynomial is exactly ``k`` times continuously + differentiable at ``x``, unless the ``order`` is provided explicitly. + In the latter case, the smoothness of the polynomial at + the breakpoint is controlled by the ``order``. + + Deduces the number of derivatives to match at each end + from ``order`` and the number of derivatives available. If + possible it uses the same number of derivatives from + each end; if the number is odd it tries to take the + extra one from y2. In any case if not enough derivatives + are available at one end or another it draws enough to + make up the total from the other end. + + If the order is too high and not enough derivatives are available, + an exception is raised. + + Examples + -------- + + >>> from scipy.interpolate import BPoly + >>> BPoly.from_derivatives([0, 1], [[1, 2], [3, 4]]) + + Creates a polynomial `f(x)` of degree 3, defined on ``[0, 1]`` + such that `f(0) = 1, df/dx(0) = 2, f(1) = 3, df/dx(1) = 4` + + >>> BPoly.from_derivatives([0, 1, 2], [[0, 1], [0], [2]]) + + Creates a piecewise polynomial `f(x)`, such that + `f(0) = f(1) = 0`, `f(2) = 2`, and `df/dx(0) = 1`. + Based on the number of derivatives provided, the order of the + local polynomials is 2 on ``[0, 1]`` and 1 on ``[1, 2]``. + Notice that no restriction is imposed on the derivatives at + ``x = 1`` and ``x = 2``. + + Indeed, the explicit form of the polynomial is:: + + f(x) = | x * (1 - x), 0 <= x < 1 + | 2 * (x - 1), 1 <= x <= 2 + + So that f'(1-0) = -1 and f'(1+0) = 2 + + """ + xi = np.asarray(xi) + if len(xi) != len(yi): + raise ValueError("xi and yi need to have the same length") + if np.any(xi[1:] - xi[:1] <= 0): + raise ValueError("x coordinates are not in increasing order") + + # number of intervals + m = len(xi) - 1 + + # global poly order is k-1, local orders are <=k and can vary + try: + k = max(len(yi[i]) + len(yi[i+1]) for i in range(m)) + except TypeError as e: + raise ValueError( + "Using a 1-D array for y? Please .reshape(-1, 1)." + ) from e + + if orders is None: + orders = [None] * m + else: + if isinstance(orders, (int, np.integer)): + orders = [orders] * m + k = max(k, max(orders)) + + if any(o <= 0 for o in orders): + raise ValueError("Orders must be positive.") + + c = [] + for i in range(m): + y1, y2 = yi[i], yi[i+1] + if orders[i] is None: + n1, n2 = len(y1), len(y2) + else: + n = orders[i]+1 + n1 = min(n//2, len(y1)) + n2 = min(n - n1, len(y2)) + n1 = min(n - n2, len(y2)) + if n1+n2 != n: + mesg = ("Point %g has %d derivatives, point %g" + " has %d derivatives, but order %d requested" % ( + xi[i], len(y1), xi[i+1], len(y2), orders[i])) + raise ValueError(mesg) + + if not (n1 <= len(y1) and n2 <= len(y2)): + raise ValueError("`order` input incompatible with" + " length y1 or y2.") + + b = BPoly._construct_from_derivatives(xi[i], xi[i+1], + y1[:n1], y2[:n2]) + if len(b) < k: + b = BPoly._raise_degree(b, k - len(b)) + c.append(b) + + c = np.asarray(c) + return cls(c.swapaxes(0, 1), xi, extrapolate) + + @staticmethod + def _construct_from_derivatives(xa, xb, ya, yb): + r"""Compute the coefficients of a polynomial in the Bernstein basis + given the values and derivatives at the edges. + + Return the coefficients of a polynomial in the Bernstein basis + defined on ``[xa, xb]`` and having the values and derivatives at the + endpoints `xa` and `xb` as specified by `ya` and `yb`. + The polynomial constructed is of the minimal possible degree, i.e., + if the lengths of `ya` and `yb` are `na` and `nb`, the degree + of the polynomial is ``na + nb - 1``. + + Parameters + ---------- + xa : float + Left-hand end point of the interval + xb : float + Right-hand end point of the interval + ya : array_like + Derivatives at `xa`. ``ya[0]`` is the value of the function, and + ``ya[i]`` for ``i > 0`` is the value of the ``i``\ th derivative. + yb : array_like + Derivatives at `xb`. + + Returns + ------- + array + coefficient array of a polynomial having specified derivatives + + Notes + ----- + This uses several facts from life of Bernstein basis functions. + First of all, + + .. math:: b'_{a, n} = n (b_{a-1, n-1} - b_{a, n-1}) + + If B(x) is a linear combination of the form + + .. math:: B(x) = \sum_{a=0}^{n} c_a b_{a, n}, + + then :math: B'(x) = n \sum_{a=0}^{n-1} (c_{a+1} - c_{a}) b_{a, n-1}. + Iterating the latter one, one finds for the q-th derivative + + .. math:: B^{q}(x) = n!/(n-q)! \sum_{a=0}^{n-q} Q_a b_{a, n-q}, + + with + + .. math:: Q_a = \sum_{j=0}^{q} (-)^{j+q} comb(q, j) c_{j+a} + + This way, only `a=0` contributes to :math: `B^{q}(x = xa)`, and + `c_q` are found one by one by iterating `q = 0, ..., na`. + + At ``x = xb`` it's the same with ``a = n - q``. + + """ + ya, yb = np.asarray(ya), np.asarray(yb) + if ya.shape[1:] != yb.shape[1:]: + raise ValueError( + f"Shapes of ya {ya.shape} and yb {yb.shape} are incompatible" + ) + + dta, dtb = ya.dtype, yb.dtype + if (np.issubdtype(dta, np.complexfloating) or + np.issubdtype(dtb, np.complexfloating)): + dt = np.complex128 + else: + dt = np.float64 + + na, nb = len(ya), len(yb) + n = na + nb + + c = np.empty((na+nb,) + ya.shape[1:], dtype=dt) + + # compute coefficients of a polynomial degree na+nb-1 + # walk left-to-right + for q in range(0, na): + c[q] = ya[q] / spec.poch(n - q, q) * (xb - xa)**q + for j in range(0, q): + c[q] -= (-1)**(j+q) * comb(q, j) * c[j] + + # now walk right-to-left + for q in range(0, nb): + c[-q-1] = yb[q] / spec.poch(n - q, q) * (-1)**q * (xb - xa)**q + for j in range(0, q): + c[-q-1] -= (-1)**(j+1) * comb(q, j+1) * c[-q+j] + + return c + + @staticmethod + def _raise_degree(c, d): + r"""Raise a degree of a polynomial in the Bernstein basis. + + Given the coefficients of a polynomial degree `k`, return (the + coefficients of) the equivalent polynomial of degree `k+d`. + + Parameters + ---------- + c : array_like + coefficient array, 1-D + d : integer + + Returns + ------- + array + coefficient array, 1-D array of length `c.shape[0] + d` + + Notes + ----- + This uses the fact that a Bernstein polynomial `b_{a, k}` can be + identically represented as a linear combination of polynomials of + a higher degree `k+d`: + + .. math:: b_{a, k} = comb(k, a) \sum_{j=0}^{d} b_{a+j, k+d} \ + comb(d, j) / comb(k+d, a+j) + + """ + if d == 0: + return c + + k = c.shape[0] - 1 + out = np.zeros((c.shape[0] + d,) + c.shape[1:], dtype=c.dtype) + + for a in range(c.shape[0]): + f = c[a] * comb(k, a) + for j in range(d+1): + out[a+j] += f * comb(d, j) / comb(k+d, a+j) + return out + + +class NdPPoly: + """ + Piecewise tensor product polynomial + + The value at point ``xp = (x', y', z', ...)`` is evaluated by first + computing the interval indices `i` such that:: + + x[0][i[0]] <= x' < x[0][i[0]+1] + x[1][i[1]] <= y' < x[1][i[1]+1] + ... + + and then computing:: + + S = sum(c[k0-m0-1,...,kn-mn-1,i[0],...,i[n]] + * (xp[0] - x[0][i[0]])**m0 + * ... + * (xp[n] - x[n][i[n]])**mn + for m0 in range(k[0]+1) + ... + for mn in range(k[n]+1)) + + where ``k[j]`` is the degree of the polynomial in dimension j. This + representation is the piecewise multivariate power basis. + + Parameters + ---------- + c : ndarray, shape (k0, ..., kn, m0, ..., mn, ...) + Polynomial coefficients, with polynomial order `kj` and + `mj+1` intervals for each dimension `j`. + x : ndim-tuple of ndarrays, shapes (mj+1,) + Polynomial breakpoints for each dimension. These must be + sorted in increasing order. + extrapolate : bool, optional + Whether to extrapolate to out-of-bounds points based on first + and last intervals, or to return NaNs. Default: True. + + Attributes + ---------- + x : tuple of ndarrays + Breakpoints. + c : ndarray + Coefficients of the polynomials. + + Methods + ------- + __call__ + derivative + antiderivative + integrate + integrate_1d + construct_fast + + See also + -------- + PPoly : piecewise polynomials in 1D + + Notes + ----- + High-order polynomials in the power basis can be numerically + unstable. + + """ + + def __init__(self, c, x, extrapolate=None): + self.x = tuple(np.ascontiguousarray(v, dtype=np.float64) for v in x) + self.c = np.asarray(c) + if extrapolate is None: + extrapolate = True + self.extrapolate = bool(extrapolate) + + ndim = len(self.x) + if any(v.ndim != 1 for v in self.x): + raise ValueError("x arrays must all be 1-dimensional") + if any(v.size < 2 for v in self.x): + raise ValueError("x arrays must all contain at least 2 points") + if c.ndim < 2*ndim: + raise ValueError("c must have at least 2*len(x) dimensions") + if any(np.any(v[1:] - v[:-1] < 0) for v in self.x): + raise ValueError("x-coordinates are not in increasing order") + if any(a != b.size - 1 for a, b in zip(c.shape[ndim:2*ndim], self.x)): + raise ValueError("x and c do not agree on the number of intervals") + + dtype = self._get_dtype(self.c.dtype) + self.c = np.ascontiguousarray(self.c, dtype=dtype) + + @classmethod + def construct_fast(cls, c, x, extrapolate=None): + """ + Construct the piecewise polynomial without making checks. + + Takes the same parameters as the constructor. Input arguments + ``c`` and ``x`` must be arrays of the correct shape and type. The + ``c`` array can only be of dtypes float and complex, and ``x`` + array must have dtype float. + + """ + self = object.__new__(cls) + self.c = c + self.x = x + if extrapolate is None: + extrapolate = True + self.extrapolate = extrapolate + return self + + def _get_dtype(self, dtype): + if np.issubdtype(dtype, np.complexfloating) \ + or np.issubdtype(self.c.dtype, np.complexfloating): + return np.complex128 + else: + return np.float64 + + def _ensure_c_contiguous(self): + if not self.c.flags.c_contiguous: + self.c = self.c.copy() + if not isinstance(self.x, tuple): + self.x = tuple(self.x) + + def __call__(self, x, nu=None, extrapolate=None): + """ + Evaluate the piecewise polynomial or its derivative + + Parameters + ---------- + x : array-like + Points to evaluate the interpolant at. + nu : tuple, optional + Orders of derivatives to evaluate. Each must be non-negative. + extrapolate : bool, optional + Whether to extrapolate to out-of-bounds points based on first + and last intervals, or to return NaNs. + + Returns + ------- + y : array-like + Interpolated values. Shape is determined by replacing + the interpolation axis in the original array with the shape of x. + + Notes + ----- + Derivatives are evaluated piecewise for each polynomial + segment, even if the polynomial is not differentiable at the + breakpoints. The polynomial intervals are considered half-open, + ``[a, b)``, except for the last interval which is closed + ``[a, b]``. + + """ + if extrapolate is None: + extrapolate = self.extrapolate + else: + extrapolate = bool(extrapolate) + + ndim = len(self.x) + + x = _ndim_coords_from_arrays(x) + x_shape = x.shape + x = np.ascontiguousarray(x.reshape(-1, x.shape[-1]), dtype=np.float64) + + if nu is None: + nu = np.zeros((ndim,), dtype=np.intc) + else: + nu = np.asarray(nu, dtype=np.intc) + if nu.ndim != 1 or nu.shape[0] != ndim: + raise ValueError("invalid number of derivative orders nu") + + dim1 = prod(self.c.shape[:ndim]) + dim2 = prod(self.c.shape[ndim:2*ndim]) + dim3 = prod(self.c.shape[2*ndim:]) + ks = np.array(self.c.shape[:ndim], dtype=np.intc) + + out = np.empty((x.shape[0], dim3), dtype=self.c.dtype) + self._ensure_c_contiguous() + + _ppoly.evaluate_nd(self.c.reshape(dim1, dim2, dim3), + self.x, + ks, + x, + nu, + bool(extrapolate), + out) + + return out.reshape(x_shape[:-1] + self.c.shape[2*ndim:]) + + def _derivative_inplace(self, nu, axis): + """ + Compute 1-D derivative along a selected dimension in-place + May result to non-contiguous c array. + """ + if nu < 0: + return self._antiderivative_inplace(-nu, axis) + + ndim = len(self.x) + axis = axis % ndim + + # reduce order + if nu == 0: + # noop + return + else: + sl = [slice(None)]*ndim + sl[axis] = slice(None, -nu, None) + c2 = self.c[tuple(sl)] + + if c2.shape[axis] == 0: + # derivative of order 0 is zero + shp = list(c2.shape) + shp[axis] = 1 + c2 = np.zeros(shp, dtype=c2.dtype) + + # multiply by the correct rising factorials + factor = spec.poch(np.arange(c2.shape[axis], 0, -1), nu) + sl = [None]*c2.ndim + sl[axis] = slice(None) + c2 *= factor[tuple(sl)] + + self.c = c2 + + def _antiderivative_inplace(self, nu, axis): + """ + Compute 1-D antiderivative along a selected dimension + May result to non-contiguous c array. + """ + if nu <= 0: + return self._derivative_inplace(-nu, axis) + + ndim = len(self.x) + axis = axis % ndim + + perm = list(range(ndim)) + perm[0], perm[axis] = perm[axis], perm[0] + perm = perm + list(range(ndim, self.c.ndim)) + + c = self.c.transpose(perm) + + c2 = np.zeros((c.shape[0] + nu,) + c.shape[1:], + dtype=c.dtype) + c2[:-nu] = c + + # divide by the correct rising factorials + factor = spec.poch(np.arange(c.shape[0], 0, -1), nu) + c2[:-nu] /= factor[(slice(None),) + (None,)*(c.ndim-1)] + + # fix continuity of added degrees of freedom + perm2 = list(range(c2.ndim)) + perm2[1], perm2[ndim+axis] = perm2[ndim+axis], perm2[1] + + c2 = c2.transpose(perm2) + c2 = c2.copy() + _ppoly.fix_continuity(c2.reshape(c2.shape[0], c2.shape[1], -1), + self.x[axis], nu-1) + + c2 = c2.transpose(perm2) + c2 = c2.transpose(perm) + + # Done + self.c = c2 + + def derivative(self, nu): + """ + Construct a new piecewise polynomial representing the derivative. + + Parameters + ---------- + nu : ndim-tuple of int + Order of derivatives to evaluate for each dimension. + If negative, the antiderivative is returned. + + Returns + ------- + pp : NdPPoly + Piecewise polynomial of orders (k[0] - nu[0], ..., k[n] - nu[n]) + representing the derivative of this polynomial. + + Notes + ----- + Derivatives are evaluated piecewise for each polynomial + segment, even if the polynomial is not differentiable at the + breakpoints. The polynomial intervals in each dimension are + considered half-open, ``[a, b)``, except for the last interval + which is closed ``[a, b]``. + + """ + p = self.construct_fast(self.c.copy(), self.x, self.extrapolate) + + for axis, n in enumerate(nu): + p._derivative_inplace(n, axis) + + p._ensure_c_contiguous() + return p + + def antiderivative(self, nu): + """ + Construct a new piecewise polynomial representing the antiderivative. + + Antiderivative is also the indefinite integral of the function, + and derivative is its inverse operation. + + Parameters + ---------- + nu : ndim-tuple of int + Order of derivatives to evaluate for each dimension. + If negative, the derivative is returned. + + Returns + ------- + pp : PPoly + Piecewise polynomial of order k2 = k + n representing + the antiderivative of this polynomial. + + Notes + ----- + The antiderivative returned by this function is continuous and + continuously differentiable to order n-1, up to floating point + rounding error. + + """ + p = self.construct_fast(self.c.copy(), self.x, self.extrapolate) + + for axis, n in enumerate(nu): + p._antiderivative_inplace(n, axis) + + p._ensure_c_contiguous() + return p + + def integrate_1d(self, a, b, axis, extrapolate=None): + r""" + Compute NdPPoly representation for one dimensional definite integral + + The result is a piecewise polynomial representing the integral: + + .. math:: + + p(y, z, ...) = \int_a^b dx\, p(x, y, z, ...) + + where the dimension integrated over is specified with the + `axis` parameter. + + Parameters + ---------- + a, b : float + Lower and upper bound for integration. + axis : int + Dimension over which to compute the 1-D integrals + extrapolate : bool, optional + Whether to extrapolate to out-of-bounds points based on first + and last intervals, or to return NaNs. + + Returns + ------- + ig : NdPPoly or array-like + Definite integral of the piecewise polynomial over [a, b]. + If the polynomial was 1D, an array is returned, + otherwise, an NdPPoly object. + + """ + if extrapolate is None: + extrapolate = self.extrapolate + else: + extrapolate = bool(extrapolate) + + ndim = len(self.x) + axis = int(axis) % ndim + + # reuse 1-D integration routines + c = self.c + swap = list(range(c.ndim)) + swap.insert(0, swap[axis]) + del swap[axis + 1] + swap.insert(1, swap[ndim + axis]) + del swap[ndim + axis + 1] + + c = c.transpose(swap) + p = PPoly.construct_fast(c.reshape(c.shape[0], c.shape[1], -1), + self.x[axis], + extrapolate=extrapolate) + out = p.integrate(a, b, extrapolate=extrapolate) + + # Construct result + if ndim == 1: + return out.reshape(c.shape[2:]) + else: + c = out.reshape(c.shape[2:]) + x = self.x[:axis] + self.x[axis+1:] + return self.construct_fast(c, x, extrapolate=extrapolate) + + def integrate(self, ranges, extrapolate=None): + """ + Compute a definite integral over a piecewise polynomial. + + Parameters + ---------- + ranges : ndim-tuple of 2-tuples float + Sequence of lower and upper bounds for each dimension, + ``[(a[0], b[0]), ..., (a[ndim-1], b[ndim-1])]`` + extrapolate : bool, optional + Whether to extrapolate to out-of-bounds points based on first + and last intervals, or to return NaNs. + + Returns + ------- + ig : array_like + Definite integral of the piecewise polynomial over + [a[0], b[0]] x ... x [a[ndim-1], b[ndim-1]] + + """ + + ndim = len(self.x) + + if extrapolate is None: + extrapolate = self.extrapolate + else: + extrapolate = bool(extrapolate) + + if not hasattr(ranges, '__len__') or len(ranges) != ndim: + raise ValueError("Range not a sequence of correct length") + + self._ensure_c_contiguous() + + # Reuse 1D integration routine + c = self.c + for n, (a, b) in enumerate(ranges): + swap = list(range(c.ndim)) + swap.insert(1, swap[ndim - n]) + del swap[ndim - n + 1] + + c = c.transpose(swap) + + p = PPoly.construct_fast(c, self.x[n], extrapolate=extrapolate) + out = p.integrate(a, b, extrapolate=extrapolate) + c = out.reshape(c.shape[2:]) + + return c diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_ndbspline.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_ndbspline.py new file mode 100644 index 0000000000000000000000000000000000000000..51ac566ed5ff1271a46ffafcc04c0e180f2ec3f1 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_ndbspline.py @@ -0,0 +1,420 @@ +import itertools +import functools +import operator +import numpy as np + +from math import prod + +from . import _bspl # type: ignore[attr-defined] + +import scipy.sparse.linalg as ssl +from scipy.sparse import csr_array + +from ._bsplines import _not_a_knot + +__all__ = ["NdBSpline"] + + +def _get_dtype(dtype): + """Return np.complex128 for complex dtypes, np.float64 otherwise.""" + if np.issubdtype(dtype, np.complexfloating): + return np.complex128 + else: + return np.float64 + + +class NdBSpline: + """Tensor product spline object. + + The value at point ``xp = (x1, x2, ..., xN)`` is evaluated as a linear + combination of products of one-dimensional b-splines in each of the ``N`` + dimensions:: + + c[i1, i2, ..., iN] * B(x1; i1, t1) * B(x2; i2, t2) * ... * B(xN; iN, tN) + + + Here ``B(x; i, t)`` is the ``i``-th b-spline defined by the knot vector + ``t`` evaluated at ``x``. + + Parameters + ---------- + t : tuple of 1D ndarrays + knot vectors in directions 1, 2, ... N, + ``len(t[i]) == n[i] + k + 1`` + c : ndarray, shape (n1, n2, ..., nN, ...) + b-spline coefficients + k : int or length-d tuple of integers + spline degrees. + A single integer is interpreted as having this degree for + all dimensions. + extrapolate : bool, optional + Whether to extrapolate out-of-bounds inputs, or return `nan`. + Default is to extrapolate. + + Attributes + ---------- + t : tuple of ndarrays + Knots vectors. + c : ndarray + Coefficients of the tensor-product spline. + k : tuple of integers + Degrees for each dimension. + extrapolate : bool, optional + Whether to extrapolate or return nans for out-of-bounds inputs. + Defaults to true. + + Methods + ------- + __call__ + design_matrix + + See Also + -------- + BSpline : a one-dimensional B-spline object + NdPPoly : an N-dimensional piecewise tensor product polynomial + + """ + def __init__(self, t, c, k, *, extrapolate=None): + self._k, self._indices_k1d, (self._t, self._len_t) = _preprocess_inputs(k, t) + + if extrapolate is None: + extrapolate = True + self.extrapolate = bool(extrapolate) + + self.c = np.asarray(c) + + ndim = self._t.shape[0] # == len(self.t) + if self.c.ndim < ndim: + raise ValueError(f"Coefficients must be at least {ndim}-dimensional.") + + for d in range(ndim): + td = self.t[d] + kd = self.k[d] + n = td.shape[0] - kd - 1 + + if self.c.shape[d] != n: + raise ValueError(f"Knots, coefficients and degree in dimension" + f" {d} are inconsistent:" + f" got {self.c.shape[d]} coefficients for" + f" {len(td)} knots, need at least {n} for" + f" k={k}.") + + dt = _get_dtype(self.c.dtype) + self.c = np.ascontiguousarray(self.c, dtype=dt) + + @property + def k(self): + return tuple(self._k) + + @property + def t(self): + # repack the knots into a tuple + return tuple(self._t[d, :self._len_t[d]] for d in range(self._t.shape[0])) + + def __call__(self, xi, *, nu=None, extrapolate=None): + """Evaluate the tensor product b-spline at ``xi``. + + Parameters + ---------- + xi : array_like, shape(..., ndim) + The coordinates to evaluate the interpolator at. + This can be a list or tuple of ndim-dimensional points + or an array with the shape (num_points, ndim). + nu : array_like, optional, shape (ndim,) + Orders of derivatives to evaluate. Each must be non-negative. + Defaults to the zeroth derivivative. + extrapolate : bool, optional + Whether to exrapolate based on first and last intervals in each + dimension, or return `nan`. Default is to ``self.extrapolate``. + + Returns + ------- + values : ndarray, shape ``xi.shape[:-1] + self.c.shape[ndim:]`` + Interpolated values at ``xi`` + """ + ndim = self._t.shape[0] # == len(self.t) + + if extrapolate is None: + extrapolate = self.extrapolate + extrapolate = bool(extrapolate) + + if nu is None: + nu = np.zeros((ndim,), dtype=np.intc) + else: + nu = np.asarray(nu, dtype=np.intc) + if nu.ndim != 1 or nu.shape[0] != ndim: + raise ValueError( + f"invalid number of derivative orders {nu = } for " + f"ndim = {len(self.t)}.") + if any(nu < 0): + raise ValueError(f"derivatives must be positive, got {nu = }") + + # prepare xi : shape (..., m1, ..., md) -> (1, m1, ..., md) + xi = np.asarray(xi, dtype=float) + xi_shape = xi.shape + xi = xi.reshape(-1, xi_shape[-1]) + xi = np.ascontiguousarray(xi) + + if xi_shape[-1] != ndim: + raise ValueError(f"Shapes: xi.shape={xi_shape} and ndim={ndim}") + + # complex -> double + was_complex = self.c.dtype.kind == 'c' + cc = self.c + if was_complex and self.c.ndim == ndim: + # make sure that core dimensions are intact, and complex->float + # size doubling only adds a trailing dimension + cc = self.c[..., None] + cc = cc.view(float) + + # prepare the coefficients: flatten the trailing dimensions + c1 = cc.reshape(cc.shape[:ndim] + (-1,)) + c1r = c1.ravel() + + # replacement for np.ravel_multi_index for indexing of `c1`: + _strides_c1 = np.asarray([s // c1.dtype.itemsize + for s in c1.strides], dtype=np.intp) + + num_c_tr = c1.shape[-1] # # of trailing coefficients + out = np.empty(xi.shape[:-1] + (num_c_tr,), dtype=c1.dtype) + + _bspl.evaluate_ndbspline(xi, + self._t, + self._len_t, + self._k, + nu, + extrapolate, + c1r, + num_c_tr, + _strides_c1, + self._indices_k1d, + out,) + out = out.view(self.c.dtype) + return out.reshape(xi_shape[:-1] + self.c.shape[ndim:]) + + @classmethod + def design_matrix(cls, xvals, t, k, extrapolate=True): + """Construct the design matrix as a CSR format sparse array. + + Parameters + ---------- + xvals : ndarray, shape(npts, ndim) + Data points. ``xvals[j, :]`` gives the ``j``-th data point as an + ``ndim``-dimensional array. + t : tuple of 1D ndarrays, length-ndim + Knot vectors in directions 1, 2, ... ndim, + k : int + B-spline degree. + extrapolate : bool, optional + Whether to extrapolate out-of-bounds values of raise a `ValueError` + + Returns + ------- + design_matrix : a CSR array + Each row of the design matrix corresponds to a value in `xvals` and + contains values of b-spline basis elements which are non-zero + at this value. + + """ + xvals = np.asarray(xvals, dtype=float) + ndim = xvals.shape[-1] + if len(t) != ndim: + raise ValueError( + f"Data and knots are inconsistent: len(t) = {len(t)} for " + f" {ndim = }." + ) + + # tabulate the flat indices for iterating over the (k+1)**ndim subarray + k, _indices_k1d, (_t, len_t) = _preprocess_inputs(k, t) + + # Precompute the shape and strides of the 'coefficients array'. + # This would have been the NdBSpline coefficients; in the present context + # this is a helper to compute the indices into the colocation matrix. + c_shape = tuple(len_t[d] - k[d] - 1 for d in range(ndim)) + + # The strides of the coeffs array: the computation is equivalent to + # >>> cstrides = [s // 8 for s in np.empty(c_shape).strides] + cs = c_shape[1:] + (1,) + cstrides = np.cumprod(cs[::-1], dtype=np.intp)[::-1].copy() + + # heavy lifting happens here + data, indices, indptr = _bspl._colloc_nd(xvals, + _t, + len_t, + k, + _indices_k1d, + cstrides) + return csr_array((data, indices, indptr)) + + +def _preprocess_inputs(k, t_tpl): + """Helpers: validate and preprocess NdBSpline inputs. + + Parameters + ---------- + k : int or tuple + Spline orders + t_tpl : tuple or array-likes + Knots. + """ + # 1. Make sure t_tpl is a tuple + if not isinstance(t_tpl, tuple): + raise ValueError(f"Expect `t` to be a tuple of array-likes. " + f"Got {t_tpl} instead." + ) + + # 2. Make ``k`` a tuple of integers + ndim = len(t_tpl) + try: + len(k) + except TypeError: + # make k a tuple + k = (k,)*ndim + + k = np.asarray([operator.index(ki) for ki in k], dtype=np.int32) + + if len(k) != ndim: + raise ValueError(f"len(t) = {len(t_tpl)} != {len(k) = }.") + + # 3. Validate inputs + ndim = len(t_tpl) + for d in range(ndim): + td = np.asarray(t_tpl[d]) + kd = k[d] + n = td.shape[0] - kd - 1 + if kd < 0: + raise ValueError(f"Spline degree in dimension {d} cannot be" + f" negative.") + if td.ndim != 1: + raise ValueError(f"Knot vector in dimension {d} must be" + f" one-dimensional.") + if n < kd + 1: + raise ValueError(f"Need at least {2*kd + 2} knots for degree" + f" {kd} in dimension {d}.") + if (np.diff(td) < 0).any(): + raise ValueError(f"Knots in dimension {d} must be in a" + f" non-decreasing order.") + if len(np.unique(td[kd:n + 1])) < 2: + raise ValueError(f"Need at least two internal knots in" + f" dimension {d}.") + if not np.isfinite(td).all(): + raise ValueError(f"Knots in dimension {d} should not have" + f" nans or infs.") + + # 4. tabulate the flat indices for iterating over the (k+1)**ndim subarray + # non-zero b-spline elements + shape = tuple(kd + 1 for kd in k) + indices = np.unravel_index(np.arange(prod(shape)), shape) + _indices_k1d = np.asarray(indices, dtype=np.intp).T.copy() + + # 5. pack the knots into a single array: + # ([1, 2, 3, 4], [5, 6], (7, 8, 9)) --> + # array([[1, 2, 3, 4], + # [5, 6, nan, nan], + # [7, 8, 9, nan]]) + ndim = len(t_tpl) + len_t = [len(ti) for ti in t_tpl] + _t = np.empty((ndim, max(len_t)), dtype=float) + _t.fill(np.nan) + for d in range(ndim): + _t[d, :len(t_tpl[d])] = t_tpl[d] + len_t = np.asarray(len_t, dtype=np.int32) + + return k, _indices_k1d, (_t, len_t) + + +def _iter_solve(a, b, solver=ssl.gcrotmk, **solver_args): + # work around iterative solvers not accepting multiple r.h.s. + + # also work around a.dtype == float64 and b.dtype == complex128 + # cf https://github.com/scipy/scipy/issues/19644 + if np.issubdtype(b.dtype, np.complexfloating): + real = _iter_solve(a, b.real, solver, **solver_args) + imag = _iter_solve(a, b.imag, solver, **solver_args) + return real + 1j*imag + + if b.ndim == 2 and b.shape[1] !=1: + res = np.empty_like(b) + for j in range(b.shape[1]): + res[:, j], info = solver(a, b[:, j], **solver_args) + if info != 0: + raise ValueError(f"{solver = } returns {info =} for column {j}.") + return res + else: + res, info = solver(a, b, **solver_args) + if info != 0: + raise ValueError(f"{solver = } returns {info = }.") + return res + + +def make_ndbspl(points, values, k=3, *, solver=ssl.gcrotmk, **solver_args): + """Construct an interpolating NdBspline. + + Parameters + ---------- + points : tuple of ndarrays of float, with shapes (m1,), ... (mN,) + The points defining the regular grid in N dimensions. The points in + each dimension (i.e. every element of the `points` tuple) must be + strictly ascending or descending. + values : ndarray of float, shape (m1, ..., mN, ...) + The data on the regular grid in n dimensions. + k : int, optional + The spline degree. Must be odd. Default is cubic, k=3 + solver : a `scipy.sparse.linalg` solver (iterative or direct), optional. + An iterative solver from `scipy.sparse.linalg` or a direct one, + `sparse.sparse.linalg.spsolve`. + Used to solve the sparse linear system + ``design_matrix @ coefficients = rhs`` for the coefficients. + Default is `scipy.sparse.linalg.gcrotmk` + solver_args : dict, optional + Additional arguments for the solver. The call signature is + ``solver(csr_array, rhs_vector, **solver_args)`` + + Returns + ------- + spl : NdBSpline object + + Notes + ----- + Boundary conditions are not-a-knot in all dimensions. + """ + ndim = len(points) + xi_shape = tuple(len(x) for x in points) + + try: + len(k) + except TypeError: + # make k a tuple + k = (k,)*ndim + + for d, point in enumerate(points): + numpts = len(np.atleast_1d(point)) + if numpts <= k[d]: + raise ValueError(f"There are {numpts} points in dimension {d}," + f" but order {k[d]} requires at least " + f" {k[d]+1} points per dimension.") + + t = tuple(_not_a_knot(np.asarray(points[d], dtype=float), k[d]) + for d in range(ndim)) + xvals = np.asarray([xv for xv in itertools.product(*points)], dtype=float) + + # construct the colocation matrix + matr = NdBSpline.design_matrix(xvals, t, k) + + # Solve for the coefficients given `values`. + # Trailing dimensions: first ndim dimensions are data, the rest are batch + # dimensions, so stack `values` into a 2D array for `spsolve` to undestand. + v_shape = values.shape + vals_shape = (prod(v_shape[:ndim]), prod(v_shape[ndim:])) + vals = values.reshape(vals_shape) + + if solver != ssl.spsolve: + solver = functools.partial(_iter_solve, solver=solver) + if "atol" not in solver_args: + # avoid a DeprecationWarning, grumble grumble + solver_args["atol"] = 1e-6 + + coef = solver(matr, vals, **solver_args) + coef = coef.reshape(xi_shape + v_shape[ndim:]) + return NdBSpline(t, coef, k) + diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_ndgriddata.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_ndgriddata.py new file mode 100644 index 0000000000000000000000000000000000000000..78fe9d6995141ad238002e0b48feb94017dc272a --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_ndgriddata.py @@ -0,0 +1,332 @@ +""" +Convenience interface to N-D interpolation + +.. versionadded:: 0.9 + +""" +import numpy as np +from ._interpnd import (LinearNDInterpolator, NDInterpolatorBase, + CloughTocher2DInterpolator, _ndim_coords_from_arrays) +from scipy.spatial import cKDTree + +__all__ = ['griddata', 'NearestNDInterpolator', 'LinearNDInterpolator', + 'CloughTocher2DInterpolator'] + +#------------------------------------------------------------------------------ +# Nearest-neighbor interpolation +#------------------------------------------------------------------------------ + + +class NearestNDInterpolator(NDInterpolatorBase): + """NearestNDInterpolator(x, y). + + Nearest-neighbor interpolator in N > 1 dimensions. + + .. versionadded:: 0.9 + + Methods + ------- + __call__ + + Parameters + ---------- + x : (npoints, ndims) 2-D ndarray of floats + Data point coordinates. + y : (npoints, ) 1-D ndarray of float or complex + Data values. + rescale : boolean, optional + Rescale points to unit cube before performing interpolation. + This is useful if some of the input dimensions have + incommensurable units and differ by many orders of magnitude. + + .. versionadded:: 0.14.0 + tree_options : dict, optional + Options passed to the underlying ``cKDTree``. + + .. versionadded:: 0.17.0 + + See Also + -------- + griddata : + Interpolate unstructured D-D data. + LinearNDInterpolator : + Piecewise linear interpolator in N dimensions. + CloughTocher2DInterpolator : + Piecewise cubic, C1 smooth, curvature-minimizing interpolator in 2D. + interpn : Interpolation on a regular grid or rectilinear grid. + RegularGridInterpolator : Interpolator on a regular or rectilinear grid + in arbitrary dimensions (`interpn` wraps this + class). + + Notes + ----- + Uses ``scipy.spatial.cKDTree`` + + .. note:: For data on a regular grid use `interpn` instead. + + Examples + -------- + We can interpolate values on a 2D plane: + + >>> from scipy.interpolate import NearestNDInterpolator + >>> import numpy as np + >>> import matplotlib.pyplot as plt + >>> rng = np.random.default_rng() + >>> x = rng.random(10) - 0.5 + >>> y = rng.random(10) - 0.5 + >>> z = np.hypot(x, y) + >>> X = np.linspace(min(x), max(x)) + >>> Y = np.linspace(min(y), max(y)) + >>> X, Y = np.meshgrid(X, Y) # 2D grid for interpolation + >>> interp = NearestNDInterpolator(list(zip(x, y)), z) + >>> Z = interp(X, Y) + >>> plt.pcolormesh(X, Y, Z, shading='auto') + >>> plt.plot(x, y, "ok", label="input point") + >>> plt.legend() + >>> plt.colorbar() + >>> plt.axis("equal") + >>> plt.show() + + """ + + def __init__(self, x, y, rescale=False, tree_options=None): + NDInterpolatorBase.__init__(self, x, y, rescale=rescale, + need_contiguous=False, + need_values=False) + if tree_options is None: + tree_options = dict() + self.tree = cKDTree(self.points, **tree_options) + self.values = np.asarray(y) + + def __call__(self, *args, **query_options): + """ + Evaluate interpolator at given points. + + Parameters + ---------- + x1, x2, ... xn : array-like of float + Points where to interpolate data at. + x1, x2, ... xn can be array-like of float with broadcastable shape. + or x1 can be array-like of float with shape ``(..., ndim)`` + **query_options + This allows ``eps``, ``p``, ``distance_upper_bound``, and ``workers`` + being passed to the cKDTree's query function to be explicitly set. + See `scipy.spatial.cKDTree.query` for an overview of the different options. + + .. versionadded:: 1.12.0 + + """ + # For the sake of enabling subclassing, NDInterpolatorBase._set_xi performs + # some operations which are not required by NearestNDInterpolator.__call__, + # hence here we operate on xi directly, without calling a parent class function. + xi = _ndim_coords_from_arrays(args, ndim=self.points.shape[1]) + xi = self._check_call_shape(xi) + xi = self._scale_x(xi) + + # We need to handle two important cases: + # (1) the case where xi has trailing dimensions (..., ndim), and + # (2) the case where y has trailing dimensions + # We will first flatten xi to deal with case (1), + # do the computation in flattened array while retaining y's dimensionality, + # and then reshape the interpolated values back to match xi's shape. + + # Flatten xi for the query + xi_flat = xi.reshape(-1, xi.shape[-1]) + original_shape = xi.shape + flattened_shape = xi_flat.shape + + # if distance_upper_bound is set to not be infinite, + # then we need to consider the case where cKDtree + # does not find any points within distance_upper_bound to return. + # It marks those points as having infinte distance, which is what will be used + # below to mask the array and return only the points that were deemed + # to have a close enough neighbor to return something useful. + dist, i = self.tree.query(xi_flat, **query_options) + valid_mask = np.isfinite(dist) + + # create a holder interp_values array and fill with nans. + if self.values.ndim > 1: + interp_shape = flattened_shape[:-1] + self.values.shape[1:] + else: + interp_shape = flattened_shape[:-1] + + if np.issubdtype(self.values.dtype, np.complexfloating): + interp_values = np.full(interp_shape, np.nan, dtype=self.values.dtype) + else: + interp_values = np.full(interp_shape, np.nan) + + interp_values[valid_mask] = self.values[i[valid_mask], ...] + + if self.values.ndim > 1: + new_shape = original_shape[:-1] + self.values.shape[1:] + else: + new_shape = original_shape[:-1] + interp_values = interp_values.reshape(new_shape) + + return interp_values + + +#------------------------------------------------------------------------------ +# Convenience interface function +#------------------------------------------------------------------------------ + + +def griddata(points, values, xi, method='linear', fill_value=np.nan, + rescale=False): + """ + Interpolate unstructured D-D data. + + Parameters + ---------- + points : 2-D ndarray of floats with shape (n, D), or length D tuple of 1-D ndarrays with shape (n,). + Data point coordinates. + values : ndarray of float or complex, shape (n,) + Data values. + xi : 2-D ndarray of floats with shape (m, D), or length D tuple of ndarrays broadcastable to the same shape. + Points at which to interpolate data. + method : {'linear', 'nearest', 'cubic'}, optional + Method of interpolation. One of + + ``nearest`` + return the value at the data point closest to + the point of interpolation. See `NearestNDInterpolator` for + more details. + + ``linear`` + tessellate the input point set to N-D + simplices, and interpolate linearly on each simplex. See + `LinearNDInterpolator` for more details. + + ``cubic`` (1-D) + return the value determined from a cubic + spline. + + ``cubic`` (2-D) + return the value determined from a + piecewise cubic, continuously differentiable (C1), and + approximately curvature-minimizing polynomial surface. See + `CloughTocher2DInterpolator` for more details. + fill_value : float, optional + Value used to fill in for requested points outside of the + convex hull of the input points. If not provided, then the + default is ``nan``. This option has no effect for the + 'nearest' method. + rescale : bool, optional + Rescale points to unit cube before performing interpolation. + This is useful if some of the input dimensions have + incommensurable units and differ by many orders of magnitude. + + .. versionadded:: 0.14.0 + + Returns + ------- + ndarray + Array of interpolated values. + + See Also + -------- + LinearNDInterpolator : + Piecewise linear interpolator in N dimensions. + NearestNDInterpolator : + Nearest-neighbor interpolator in N dimensions. + CloughTocher2DInterpolator : + Piecewise cubic, C1 smooth, curvature-minimizing interpolator in 2D. + interpn : Interpolation on a regular grid or rectilinear grid. + RegularGridInterpolator : Interpolator on a regular or rectilinear grid + in arbitrary dimensions (`interpn` wraps this + class). + + Notes + ----- + + .. versionadded:: 0.9 + + .. note:: For data on a regular grid use `interpn` instead. + + Examples + -------- + + Suppose we want to interpolate the 2-D function + + >>> import numpy as np + >>> def func(x, y): + ... return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2 + + on a grid in [0, 1]x[0, 1] + + >>> grid_x, grid_y = np.mgrid[0:1:100j, 0:1:200j] + + but we only know its values at 1000 data points: + + >>> rng = np.random.default_rng() + >>> points = rng.random((1000, 2)) + >>> values = func(points[:,0], points[:,1]) + + This can be done with `griddata` -- below we try out all of the + interpolation methods: + + >>> from scipy.interpolate import griddata + >>> grid_z0 = griddata(points, values, (grid_x, grid_y), method='nearest') + >>> grid_z1 = griddata(points, values, (grid_x, grid_y), method='linear') + >>> grid_z2 = griddata(points, values, (grid_x, grid_y), method='cubic') + + One can see that the exact result is reproduced by all of the + methods to some degree, but for this smooth function the piecewise + cubic interpolant gives the best results: + + >>> import matplotlib.pyplot as plt + >>> plt.subplot(221) + >>> plt.imshow(func(grid_x, grid_y).T, extent=(0,1,0,1), origin='lower') + >>> plt.plot(points[:,0], points[:,1], 'k.', ms=1) + >>> plt.title('Original') + >>> plt.subplot(222) + >>> plt.imshow(grid_z0.T, extent=(0,1,0,1), origin='lower') + >>> plt.title('Nearest') + >>> plt.subplot(223) + >>> plt.imshow(grid_z1.T, extent=(0,1,0,1), origin='lower') + >>> plt.title('Linear') + >>> plt.subplot(224) + >>> plt.imshow(grid_z2.T, extent=(0,1,0,1), origin='lower') + >>> plt.title('Cubic') + >>> plt.gcf().set_size_inches(6, 6) + >>> plt.show() + + """ # numpy/numpydoc#87 # noqa: E501 + + points = _ndim_coords_from_arrays(points) + + if points.ndim < 2: + ndim = points.ndim + else: + ndim = points.shape[-1] + + if ndim == 1 and method in ('nearest', 'linear', 'cubic'): + from ._interpolate import interp1d + points = points.ravel() + if isinstance(xi, tuple): + if len(xi) != 1: + raise ValueError("invalid number of dimensions in xi") + xi, = xi + # Sort points/values together, necessary as input for interp1d + idx = np.argsort(points) + points = points[idx] + values = values[idx] + if method == 'nearest': + fill_value = 'extrapolate' + ip = interp1d(points, values, kind=method, axis=0, bounds_error=False, + fill_value=fill_value) + return ip(xi) + elif method == 'nearest': + ip = NearestNDInterpolator(points, values, rescale=rescale) + return ip(xi) + elif method == 'linear': + ip = LinearNDInterpolator(points, values, fill_value=fill_value, + rescale=rescale) + return ip(xi) + elif method == 'cubic' and ndim == 2: + ip = CloughTocher2DInterpolator(points, values, fill_value=fill_value, + rescale=rescale) + return ip(xi) + else: + raise ValueError("Unknown interpolation method %r for " + "%d dimensional data" % (method, ndim)) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_pade.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_pade.py new file mode 100644 index 0000000000000000000000000000000000000000..387ef11dde5d3ace8a15324058c10fa31899c92c --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_pade.py @@ -0,0 +1,67 @@ +from numpy import zeros, asarray, eye, poly1d, hstack, r_ +from scipy import linalg + +__all__ = ["pade"] + +def pade(an, m, n=None): + """ + Return Pade approximation to a polynomial as the ratio of two polynomials. + + Parameters + ---------- + an : (N,) array_like + Taylor series coefficients. + m : int + The order of the returned approximating polynomial `q`. + n : int, optional + The order of the returned approximating polynomial `p`. By default, + the order is ``len(an)-1-m``. + + Returns + ------- + p, q : Polynomial class + The Pade approximation of the polynomial defined by `an` is + ``p(x)/q(x)``. + + Examples + -------- + >>> import numpy as np + >>> from scipy.interpolate import pade + >>> e_exp = [1.0, 1.0, 1.0/2.0, 1.0/6.0, 1.0/24.0, 1.0/120.0] + >>> p, q = pade(e_exp, 2) + + >>> e_exp.reverse() + >>> e_poly = np.poly1d(e_exp) + + Compare ``e_poly(x)`` and the Pade approximation ``p(x)/q(x)`` + + >>> e_poly(1) + 2.7166666666666668 + + >>> p(1)/q(1) + 2.7179487179487181 + + """ + an = asarray(an) + if n is None: + n = len(an) - 1 - m + if n < 0: + raise ValueError("Order of q must be smaller than len(an)-1.") + if n < 0: + raise ValueError("Order of p must be greater than 0.") + N = m + n + if N > len(an)-1: + raise ValueError("Order of q+p must be smaller than len(an).") + an = an[:N+1] + Akj = eye(N+1, n+1, dtype=an.dtype) + Bkj = zeros((N+1, m), dtype=an.dtype) + for row in range(1, m+1): + Bkj[row,:row] = -(an[:row])[::-1] + for row in range(m+1, N+1): + Bkj[row,:] = -(an[row-m:row])[::-1] + C = hstack((Akj, Bkj)) + pq = linalg.solve(C, an) + p = pq[:n+1] + q = r_[1.0, pq[n+1:]] + return poly1d(p[::-1]), poly1d(q[::-1]) + diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_polyint.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_polyint.py new file mode 100644 index 0000000000000000000000000000000000000000..9cec3eb9939abba36505010334911c167797b750 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_polyint.py @@ -0,0 +1,961 @@ +import warnings + +import numpy as np +from scipy.special import factorial +from scipy._lib._util import (_asarray_validated, float_factorial, check_random_state, + _transition_to_rng) + + +__all__ = ["KroghInterpolator", "krogh_interpolate", + "BarycentricInterpolator", "barycentric_interpolate", + "approximate_taylor_polynomial"] + + +def _isscalar(x): + """Check whether x is if a scalar type, or 0-dim""" + return np.isscalar(x) or hasattr(x, 'shape') and x.shape == () + + +class _Interpolator1D: + """ + Common features in univariate interpolation + + Deal with input data type and interpolation axis rolling. The + actual interpolator can assume the y-data is of shape (n, r) where + `n` is the number of x-points, and `r` the number of variables, + and use self.dtype as the y-data type. + + Attributes + ---------- + _y_axis + Axis along which the interpolation goes in the original array + _y_extra_shape + Additional trailing shape of the input arrays, excluding + the interpolation axis. + dtype + Dtype of the y-data arrays. Can be set via _set_dtype, which + forces it to be float or complex. + + Methods + ------- + __call__ + _prepare_x + _finish_y + _reshape_yi + _set_yi + _set_dtype + _evaluate + + """ + + __slots__ = ('_y_axis', '_y_extra_shape', 'dtype') + + def __init__(self, xi=None, yi=None, axis=None): + self._y_axis = axis + self._y_extra_shape = None + self.dtype = None + if yi is not None: + self._set_yi(yi, xi=xi, axis=axis) + + def __call__(self, x): + """ + Evaluate the interpolant + + Parameters + ---------- + x : array_like + Point or points at which to evaluate the interpolant. + + Returns + ------- + y : array_like + Interpolated values. Shape is determined by replacing + the interpolation axis in the original array with the shape of `x`. + + Notes + ----- + Input values `x` must be convertible to `float` values like `int` + or `float`. + + """ + x, x_shape = self._prepare_x(x) + y = self._evaluate(x) + return self._finish_y(y, x_shape) + + def _evaluate(self, x): + """ + Actually evaluate the value of the interpolator. + """ + raise NotImplementedError() + + def _prepare_x(self, x): + """Reshape input x array to 1-D""" + x = _asarray_validated(x, check_finite=False, as_inexact=True) + x_shape = x.shape + return x.ravel(), x_shape + + def _finish_y(self, y, x_shape): + """Reshape interpolated y back to an N-D array similar to initial y""" + y = y.reshape(x_shape + self._y_extra_shape) + if self._y_axis != 0 and x_shape != (): + nx = len(x_shape) + ny = len(self._y_extra_shape) + s = (list(range(nx, nx + self._y_axis)) + + list(range(nx)) + list(range(nx+self._y_axis, nx+ny))) + y = y.transpose(s) + return y + + def _reshape_yi(self, yi, check=False): + yi = np.moveaxis(np.asarray(yi), self._y_axis, 0) + if check and yi.shape[1:] != self._y_extra_shape: + ok_shape = (f"{self._y_extra_shape[-self._y_axis:]!r} + (N,) + " + f"{self._y_extra_shape[:-self._y_axis]!r}") + raise ValueError(f"Data must be of shape {ok_shape}") + return yi.reshape((yi.shape[0], -1)) + + def _set_yi(self, yi, xi=None, axis=None): + if axis is None: + axis = self._y_axis + if axis is None: + raise ValueError("no interpolation axis specified") + + yi = np.asarray(yi) + + shape = yi.shape + if shape == (): + shape = (1,) + if xi is not None and shape[axis] != len(xi): + raise ValueError("x and y arrays must be equal in length along " + "interpolation axis.") + + self._y_axis = (axis % yi.ndim) + self._y_extra_shape = yi.shape[:self._y_axis] + yi.shape[self._y_axis+1:] + self.dtype = None + self._set_dtype(yi.dtype) + + def _set_dtype(self, dtype, union=False): + if np.issubdtype(dtype, np.complexfloating) \ + or np.issubdtype(self.dtype, np.complexfloating): + self.dtype = np.complex128 + else: + if not union or self.dtype != np.complex128: + self.dtype = np.float64 + + +class _Interpolator1DWithDerivatives(_Interpolator1D): + def derivatives(self, x, der=None): + """ + Evaluate several derivatives of the polynomial at the point `x` + + Produce an array of derivatives evaluated at the point `x`. + + Parameters + ---------- + x : array_like + Point or points at which to evaluate the derivatives + der : int or list or None, optional + How many derivatives to evaluate, or None for all potentially + nonzero derivatives (that is, a number equal to the number + of points), or a list of derivatives to evaluate. This number + includes the function value as the '0th' derivative. + + Returns + ------- + d : ndarray + Array with derivatives; ``d[j]`` contains the jth derivative. + Shape of ``d[j]`` is determined by replacing the interpolation + axis in the original array with the shape of `x`. + + Examples + -------- + >>> from scipy.interpolate import KroghInterpolator + >>> KroghInterpolator([0,0,0],[1,2,3]).derivatives(0) + array([1.0,2.0,3.0]) + >>> KroghInterpolator([0,0,0],[1,2,3]).derivatives([0,0]) + array([[1.0,1.0], + [2.0,2.0], + [3.0,3.0]]) + + """ + x, x_shape = self._prepare_x(x) + y = self._evaluate_derivatives(x, der) + + y = y.reshape((y.shape[0],) + x_shape + self._y_extra_shape) + if self._y_axis != 0 and x_shape != (): + nx = len(x_shape) + ny = len(self._y_extra_shape) + s = ([0] + list(range(nx+1, nx + self._y_axis+1)) + + list(range(1, nx+1)) + + list(range(nx+1+self._y_axis, nx+ny+1))) + y = y.transpose(s) + return y + + def derivative(self, x, der=1): + """ + Evaluate a single derivative of the polynomial at the point `x`. + + Parameters + ---------- + x : array_like + Point or points at which to evaluate the derivatives + + der : integer, optional + Which derivative to evaluate (default: first derivative). + This number includes the function value as 0th derivative. + + Returns + ------- + d : ndarray + Derivative interpolated at the x-points. Shape of `d` is + determined by replacing the interpolation axis in the + original array with the shape of `x`. + + Notes + ----- + This may be computed by evaluating all derivatives up to the desired + one (using self.derivatives()) and then discarding the rest. + + """ + x, x_shape = self._prepare_x(x) + y = self._evaluate_derivatives(x, der+1) + return self._finish_y(y[der], x_shape) + + def _evaluate_derivatives(self, x, der=None): + """ + Actually evaluate the derivatives. + + Parameters + ---------- + x : array_like + 1D array of points at which to evaluate the derivatives + der : integer, optional + The number of derivatives to evaluate, from 'order 0' (der=1) + to order der-1. If omitted, return all possibly-non-zero + derivatives, ie 0 to order n-1. + + Returns + ------- + d : ndarray + Array of shape ``(der, x.size, self.yi.shape[1])`` containing + the derivatives from 0 to der-1 + """ + raise NotImplementedError() + + +class KroghInterpolator(_Interpolator1DWithDerivatives): + """ + Interpolating polynomial for a set of points. + + The polynomial passes through all the pairs ``(xi, yi)``. One may + additionally specify a number of derivatives at each point `xi`; + this is done by repeating the value `xi` and specifying the + derivatives as successive `yi` values. + + Allows evaluation of the polynomial and all its derivatives. + For reasons of numerical stability, this function does not compute + the coefficients of the polynomial, although they can be obtained + by evaluating all the derivatives. + + Parameters + ---------- + xi : array_like, shape (npoints, ) + Known x-coordinates. Must be sorted in increasing order. + yi : array_like, shape (..., npoints, ...) + Known y-coordinates. When an xi occurs two or more times in + a row, the corresponding yi's represent derivative values. The length of `yi` + along the interpolation axis must be equal to the length of `xi`. Use the + `axis` parameter to select the correct axis. + axis : int, optional + Axis in the `yi` array corresponding to the x-coordinate values. Defaults to + ``axis=0``. + + Notes + ----- + Be aware that the algorithms implemented here are not necessarily + the most numerically stable known. Moreover, even in a world of + exact computation, unless the x coordinates are chosen very + carefully - Chebyshev zeros (e.g., cos(i*pi/n)) are a good choice - + polynomial interpolation itself is a very ill-conditioned process + due to the Runge phenomenon. In general, even with well-chosen + x values, degrees higher than about thirty cause problems with + numerical instability in this code. + + Based on [1]_. + + References + ---------- + .. [1] Krogh, "Efficient Algorithms for Polynomial Interpolation + and Numerical Differentiation", 1970. + + Examples + -------- + To produce a polynomial that is zero at 0 and 1 and has + derivative 2 at 0, call + + >>> from scipy.interpolate import KroghInterpolator + >>> KroghInterpolator([0,0,1],[0,2,0]) + + This constructs the quadratic :math:`2x^2-2x`. The derivative condition + is indicated by the repeated zero in the `xi` array; the corresponding + yi values are 0, the function value, and 2, the derivative value. + + For another example, given `xi`, `yi`, and a derivative `ypi` for each + point, appropriate arrays can be constructed as: + + >>> import numpy as np + >>> rng = np.random.default_rng() + >>> xi = np.linspace(0, 1, 5) + >>> yi, ypi = rng.random((2, 5)) + >>> xi_k, yi_k = np.repeat(xi, 2), np.ravel(np.dstack((yi,ypi))) + >>> KroghInterpolator(xi_k, yi_k) + + To produce a vector-valued polynomial, supply a higher-dimensional + array for `yi`: + + >>> KroghInterpolator([0,1],[[2,3],[4,5]]) + + This constructs a linear polynomial giving (2,3) at 0 and (4,5) at 1. + + """ + + def __init__(self, xi, yi, axis=0): + super().__init__(xi, yi, axis) + + self.xi = np.asarray(xi) + self.yi = self._reshape_yi(yi) + self.n, self.r = self.yi.shape + + if (deg := self.xi.size) > 30: + warnings.warn(f"{deg} degrees provided, degrees higher than about" + " thirty cause problems with numerical instability " + "with 'KroghInterpolator'", stacklevel=2) + + c = np.zeros((self.n+1, self.r), dtype=self.dtype) + c[0] = self.yi[0] + Vk = np.zeros((self.n, self.r), dtype=self.dtype) + for k in range(1, self.n): + s = 0 + while s <= k and xi[k-s] == xi[k]: + s += 1 + s -= 1 + Vk[0] = self.yi[k]/float_factorial(s) + for i in range(k-s): + if xi[i] == xi[k]: + raise ValueError("Elements of `xi` can't be equal.") + if s == 0: + Vk[i+1] = (c[i]-Vk[i])/(xi[i]-xi[k]) + else: + Vk[i+1] = (Vk[i+1]-Vk[i])/(xi[i]-xi[k]) + c[k] = Vk[k-s] + self.c = c + + def _evaluate(self, x): + pi = 1 + p = np.zeros((len(x), self.r), dtype=self.dtype) + p += self.c[0,np.newaxis,:] + for k in range(1, self.n): + w = x - self.xi[k-1] + pi = w*pi + p += pi[:,np.newaxis] * self.c[k] + return p + + def _evaluate_derivatives(self, x, der=None): + n = self.n + r = self.r + + if der is None: + der = self.n + + pi = np.zeros((n, len(x))) + w = np.zeros((n, len(x))) + pi[0] = 1 + p = np.zeros((len(x), self.r), dtype=self.dtype) + p += self.c[0, np.newaxis, :] + + for k in range(1, n): + w[k-1] = x - self.xi[k-1] + pi[k] = w[k-1] * pi[k-1] + p += pi[k, :, np.newaxis] * self.c[k] + + cn = np.zeros((max(der, n+1), len(x), r), dtype=self.dtype) + cn[:n+1, :, :] += self.c[:n+1, np.newaxis, :] + cn[0] = p + for k in range(1, n): + for i in range(1, n-k+1): + pi[i] = w[k+i-1]*pi[i-1] + pi[i] + cn[k] = cn[k] + pi[i, :, np.newaxis]*cn[k+i] + cn[k] *= float_factorial(k) + + cn[n, :, :] = 0 + return cn[:der] + + +def krogh_interpolate(xi, yi, x, der=0, axis=0): + """ + Convenience function for polynomial interpolation. + + See `KroghInterpolator` for more details. + + Parameters + ---------- + xi : array_like + Interpolation points (known x-coordinates). + yi : array_like + Known y-coordinates, of shape ``(xi.size, R)``. Interpreted as + vectors of length R, or scalars if R=1. + x : array_like + Point or points at which to evaluate the derivatives. + der : int or list or None, optional + How many derivatives to evaluate, or None for all potentially + nonzero derivatives (that is, a number equal to the number + of points), or a list of derivatives to evaluate. This number + includes the function value as the '0th' derivative. + axis : int, optional + Axis in the `yi` array corresponding to the x-coordinate values. + + Returns + ------- + d : ndarray + If the interpolator's values are R-D then the + returned array will be the number of derivatives by N by R. + If `x` is a scalar, the middle dimension will be dropped; if + the `yi` are scalars then the last dimension will be dropped. + + See Also + -------- + KroghInterpolator : Krogh interpolator + + Notes + ----- + Construction of the interpolating polynomial is a relatively expensive + process. If you want to evaluate it repeatedly consider using the class + KroghInterpolator (which is what this function uses). + + Examples + -------- + We can interpolate 2D observed data using Krogh interpolation: + + >>> import numpy as np + >>> import matplotlib.pyplot as plt + >>> from scipy.interpolate import krogh_interpolate + >>> x_observed = np.linspace(0.0, 10.0, 11) + >>> y_observed = np.sin(x_observed) + >>> x = np.linspace(min(x_observed), max(x_observed), num=100) + >>> y = krogh_interpolate(x_observed, y_observed, x) + >>> plt.plot(x_observed, y_observed, "o", label="observation") + >>> plt.plot(x, y, label="krogh interpolation") + >>> plt.legend() + >>> plt.show() + """ + + P = KroghInterpolator(xi, yi, axis=axis) + if der == 0: + return P(x) + elif _isscalar(der): + return P.derivative(x, der=der) + else: + return P.derivatives(x, der=np.amax(der)+1)[der] + + +def approximate_taylor_polynomial(f,x,degree,scale,order=None): + """ + Estimate the Taylor polynomial of f at x by polynomial fitting. + + Parameters + ---------- + f : callable + The function whose Taylor polynomial is sought. Should accept + a vector of `x` values. + x : scalar + The point at which the polynomial is to be evaluated. + degree : int + The degree of the Taylor polynomial + scale : scalar + The width of the interval to use to evaluate the Taylor polynomial. + Function values spread over a range this wide are used to fit the + polynomial. Must be chosen carefully. + order : int or None, optional + The order of the polynomial to be used in the fitting; `f` will be + evaluated ``order+1`` times. If None, use `degree`. + + Returns + ------- + p : poly1d instance + The Taylor polynomial (translated to the origin, so that + for example p(0)=f(x)). + + Notes + ----- + The appropriate choice of "scale" is a trade-off; too large and the + function differs from its Taylor polynomial too much to get a good + answer, too small and round-off errors overwhelm the higher-order terms. + The algorithm used becomes numerically unstable around order 30 even + under ideal circumstances. + + Choosing order somewhat larger than degree may improve the higher-order + terms. + + Examples + -------- + We can calculate Taylor approximation polynomials of sin function with + various degrees: + + >>> import numpy as np + >>> import matplotlib.pyplot as plt + >>> from scipy.interpolate import approximate_taylor_polynomial + >>> x = np.linspace(-10.0, 10.0, num=100) + >>> plt.plot(x, np.sin(x), label="sin curve") + >>> for degree in np.arange(1, 15, step=2): + ... sin_taylor = approximate_taylor_polynomial(np.sin, 0, degree, 1, + ... order=degree + 2) + ... plt.plot(x, sin_taylor(x), label=f"degree={degree}") + >>> plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', + ... borderaxespad=0.0, shadow=True) + >>> plt.tight_layout() + >>> plt.axis([-10, 10, -10, 10]) + >>> plt.show() + + """ + if order is None: + order = degree + + n = order+1 + # Choose n points that cluster near the endpoints of the interval in + # a way that avoids the Runge phenomenon. Ensure, by including the + # endpoint or not as appropriate, that one point always falls at x + # exactly. + xs = scale*np.cos(np.linspace(0,np.pi,n,endpoint=n % 1)) + x + + P = KroghInterpolator(xs, f(xs)) + d = P.derivatives(x,der=degree+1) + + return np.poly1d((d/factorial(np.arange(degree+1)))[::-1]) + + +class BarycentricInterpolator(_Interpolator1DWithDerivatives): + r"""Interpolating polynomial for a set of points. + + Constructs a polynomial that passes through a given set of points. + Allows evaluation of the polynomial and all its derivatives, + efficient changing of the y-values to be interpolated, + and updating by adding more x- and y-values. + + For reasons of numerical stability, this function does not compute + the coefficients of the polynomial. + + The values `yi` need to be provided before the function is + evaluated, but none of the preprocessing depends on them, so rapid + updates are possible. + + Parameters + ---------- + xi : array_like, shape (npoints, ) + 1-D array of x coordinates of the points the polynomial + should pass through + yi : array_like, shape (..., npoints, ...), optional + N-D array of y coordinates of the points the polynomial should pass through. + If None, the y values will be supplied later via the `set_y` method. + The length of `yi` along the interpolation axis must be equal to the length + of `xi`. Use the ``axis`` parameter to select correct axis. + axis : int, optional + Axis in the yi array corresponding to the x-coordinate values. Defaults + to ``axis=0``. + wi : array_like, optional + The barycentric weights for the chosen interpolation points `xi`. + If absent or None, the weights will be computed from `xi` (default). + This allows for the reuse of the weights `wi` if several interpolants + are being calculated using the same nodes `xi`, without re-computation. + rng : {None, int, `numpy.random.Generator`}, optional + If `rng` is passed by keyword, types other than `numpy.random.Generator` are + passed to `numpy.random.default_rng` to instantiate a ``Generator``. + If `rng` is already a ``Generator`` instance, then the provided instance is + used. Specify `rng` for repeatable interpolation. + + If this argument `random_state` is passed by keyword, + legacy behavior for the argument `random_state` applies: + + - If `random_state` is None (or `numpy.random`), the `numpy.random.RandomState` + singleton is used. + - If `random_state` is an int, a new ``RandomState`` instance is used, + seeded with `random_state`. + - If `random_state` is already a ``Generator`` or ``RandomState`` instance then + that instance is used. + + .. versionchanged:: 1.15.0 + As part of the `SPEC-007 `_ + transition from use of `numpy.random.RandomState` to + `numpy.random.Generator` this keyword was changed from `random_state` to `rng`. + For an interim period, both keywords will continue to work (only specify + one of them). After the interim period using the `random_state` keyword will emit + warnings. The behavior of the `random_state` and `rng` keywords is outlined above. + + Notes + ----- + This class uses a "barycentric interpolation" method that treats + the problem as a special case of rational function interpolation. + This algorithm is quite stable, numerically, but even in a world of + exact computation, unless the x coordinates are chosen very + carefully - Chebyshev zeros (e.g., cos(i*pi/n)) are a good choice - + polynomial interpolation itself is a very ill-conditioned process + due to the Runge phenomenon. + + Based on Berrut and Trefethen 2004, "Barycentric Lagrange Interpolation". + + Examples + -------- + To produce a quintic barycentric interpolant approximating the function + :math:`\sin x`, and its first four derivatives, using six randomly-spaced + nodes in :math:`(0, \frac{\pi}{2})`: + + >>> import numpy as np + >>> import matplotlib.pyplot as plt + >>> from scipy.interpolate import BarycentricInterpolator + >>> rng = np.random.default_rng() + >>> xi = rng.random(6) * np.pi/2 + >>> f, f_d1, f_d2, f_d3, f_d4 = np.sin, np.cos, lambda x: -np.sin(x), lambda x: -np.cos(x), np.sin + >>> P = BarycentricInterpolator(xi, f(xi), random_state=rng) + >>> fig, axs = plt.subplots(5, 1, sharex=True, layout='constrained', figsize=(7,10)) + >>> x = np.linspace(0, np.pi, 100) + >>> axs[0].plot(x, P(x), 'r:', x, f(x), 'k--', xi, f(xi), 'xk') + >>> axs[1].plot(x, P.derivative(x), 'r:', x, f_d1(x), 'k--', xi, f_d1(xi), 'xk') + >>> axs[2].plot(x, P.derivative(x, 2), 'r:', x, f_d2(x), 'k--', xi, f_d2(xi), 'xk') + >>> axs[3].plot(x, P.derivative(x, 3), 'r:', x, f_d3(x), 'k--', xi, f_d3(xi), 'xk') + >>> axs[4].plot(x, P.derivative(x, 4), 'r:', x, f_d4(x), 'k--', xi, f_d4(xi), 'xk') + >>> axs[0].set_xlim(0, np.pi) + >>> axs[4].set_xlabel(r"$x$") + >>> axs[4].set_xticks([i * np.pi / 4 for i in range(5)], + ... ["0", r"$\frac{\pi}{4}$", r"$\frac{\pi}{2}$", r"$\frac{3\pi}{4}$", r"$\pi$"]) + >>> axs[0].set_ylabel("$f(x)$") + >>> axs[1].set_ylabel("$f'(x)$") + >>> axs[2].set_ylabel("$f''(x)$") + >>> axs[3].set_ylabel("$f^{(3)}(x)$") + >>> axs[4].set_ylabel("$f^{(4)}(x)$") + >>> labels = ['Interpolation nodes', 'True function $f$', 'Barycentric interpolation'] + >>> axs[0].legend(axs[0].get_lines()[::-1], labels, bbox_to_anchor=(0., 1.02, 1., .102), + ... loc='lower left', ncols=3, mode="expand", borderaxespad=0., frameon=False) + >>> plt.show() + """ # numpy/numpydoc#87 # noqa: E501 + + @_transition_to_rng("random_state", replace_doc=False) + def __init__(self, xi, yi=None, axis=0, *, wi=None, rng=None): + super().__init__(xi, yi, axis) + + rng = check_random_state(rng) + + self.xi = np.asarray(xi, dtype=np.float64) + self.set_yi(yi) + self.n = len(self.xi) + + # cache derivative object to avoid re-computing the weights with every call. + self._diff_cij = None + + if wi is not None: + self.wi = wi + else: + # See page 510 of Berrut and Trefethen 2004 for an explanation of the + # capacity scaling and the suggestion of using a random permutation of + # the input factors. + # At the moment, the permutation is not performed for xi that are + # appended later through the add_xi interface. It's not clear to me how + # to implement that and it seems that most situations that require + # these numerical stability improvements will be able to provide all + # the points to the constructor. + self._inv_capacity = 4.0 / (np.max(self.xi) - np.min(self.xi)) + permute = rng.permutation(self.n, ) + inv_permute = np.zeros(self.n, dtype=np.int32) + inv_permute[permute] = np.arange(self.n) + self.wi = np.zeros(self.n) + + for i in range(self.n): + dist = self._inv_capacity * (self.xi[i] - self.xi[permute]) + dist[inv_permute[i]] = 1.0 + prod = np.prod(dist) + if prod == 0.0: + raise ValueError("Interpolation points xi must be" + " distinct.") + self.wi[i] = 1.0 / prod + + def set_yi(self, yi, axis=None): + """ + Update the y values to be interpolated + + The barycentric interpolation algorithm requires the calculation + of weights, but these depend only on the `xi`. The `yi` can be changed + at any time. + + Parameters + ---------- + yi : array_like + The y-coordinates of the points the polynomial will pass through. + If None, the y values must be supplied later. + axis : int, optional + Axis in the `yi` array corresponding to the x-coordinate values. + + """ + if yi is None: + self.yi = None + return + self._set_yi(yi, xi=self.xi, axis=axis) + self.yi = self._reshape_yi(yi) + self.n, self.r = self.yi.shape + self._diff_baryint = None + + def add_xi(self, xi, yi=None): + """ + Add more x values to the set to be interpolated + + The barycentric interpolation algorithm allows easy updating by + adding more points for the polynomial to pass through. + + Parameters + ---------- + xi : array_like + The x coordinates of the points that the polynomial should pass + through. + yi : array_like, optional + The y coordinates of the points the polynomial should pass through. + Should have shape ``(xi.size, R)``; if R > 1 then the polynomial is + vector-valued. + If `yi` is not given, the y values will be supplied later. `yi` + should be given if and only if the interpolator has y values + specified. + + Notes + ----- + The new points added by `add_xi` are not randomly permuted + so there is potential for numerical instability, + especially for a large number of points. If this + happens, please reconstruct interpolation from scratch instead. + """ + if yi is not None: + if self.yi is None: + raise ValueError("No previous yi value to update!") + yi = self._reshape_yi(yi, check=True) + self.yi = np.vstack((self.yi,yi)) + else: + if self.yi is not None: + raise ValueError("No update to yi provided!") + old_n = self.n + self.xi = np.concatenate((self.xi,xi)) + self.n = len(self.xi) + self.wi **= -1 + old_wi = self.wi + self.wi = np.zeros(self.n) + self.wi[:old_n] = old_wi + for j in range(old_n, self.n): + self.wi[:j] *= self._inv_capacity * (self.xi[j]-self.xi[:j]) + self.wi[j] = np.multiply.reduce( + self._inv_capacity * (self.xi[:j]-self.xi[j]) + ) + self.wi **= -1 + self._diff_cij = None + self._diff_baryint = None + + def __call__(self, x): + """Evaluate the interpolating polynomial at the points x + + Parameters + ---------- + x : array_like + Point or points at which to evaluate the interpolant. + + Returns + ------- + y : array_like + Interpolated values. Shape is determined by replacing + the interpolation axis in the original array with the shape of `x`. + + Notes + ----- + Currently the code computes an outer product between `x` and the + weights, that is, it constructs an intermediate array of size + ``(N, len(x))``, where N is the degree of the polynomial. + """ + return _Interpolator1D.__call__(self, x) + + def _evaluate(self, x): + if x.size == 0: + p = np.zeros((0, self.r), dtype=self.dtype) + else: + c = x[..., np.newaxis] - self.xi + z = c == 0 + c[z] = 1 + c = self.wi / c + with np.errstate(divide='ignore'): + p = np.dot(c, self.yi) / np.sum(c, axis=-1)[..., np.newaxis] + # Now fix where x==some xi + r = np.nonzero(z) + if len(r) == 1: # evaluation at a scalar + if len(r[0]) > 0: # equals one of the points + p = self.yi[r[0][0]] + else: + p[r[:-1]] = self.yi[r[-1]] + return p + + def derivative(self, x, der=1): + """ + Evaluate a single derivative of the polynomial at the point x. + + Parameters + ---------- + x : array_like + Point or points at which to evaluate the derivatives + der : integer, optional + Which derivative to evaluate (default: first derivative). + This number includes the function value as 0th derivative. + + Returns + ------- + d : ndarray + Derivative interpolated at the x-points. Shape of `d` is + determined by replacing the interpolation axis in the + original array with the shape of `x`. + """ + x, x_shape = self._prepare_x(x) + y = self._evaluate_derivatives(x, der+1, all_lower=False) + return self._finish_y(y, x_shape) + + def _evaluate_derivatives(self, x, der=None, all_lower=True): + # NB: der here is not the order of the highest derivative; + # instead, it is the size of the derivatives matrix that + # would be returned with all_lower=True, including the + # '0th' derivative (the undifferentiated function). + # E.g. to evaluate the 5th derivative alone, call + # _evaluate_derivatives(x, der=6, all_lower=False). + + if (not all_lower) and (x.size == 0 or self.r == 0): + return np.zeros((0, self.r), dtype=self.dtype) + + if (not all_lower) and der == 1: + return self._evaluate(x) + + if (not all_lower) and (der > self.n): + return np.zeros((len(x), self.r), dtype=self.dtype) + + if der is None: + der = self.n + + if all_lower and (x.size == 0 or self.r == 0): + return np.zeros((der, len(x), self.r), dtype=self.dtype) + + if self._diff_cij is None: + # c[i,j] = xi[i] - xi[j] + c = self.xi[:, np.newaxis] - self.xi + + # avoid division by 0 (diagonal entries are so far zero by construction) + np.fill_diagonal(c, 1) + + # c[i,j] = (w[j] / w[i]) / (xi[i] - xi[j]) (equation 9.4) + c = self.wi/ (c * self.wi[..., np.newaxis]) + + # fill in correct diagonal entries: each column sums to 0 + np.fill_diagonal(c, 0) + + # calculate diagonal + # c[j,j] = -sum_{i != j} c[i,j] (equation 9.5) + d = -c.sum(axis=1) + # c[i,j] = l_j(x_i) + np.fill_diagonal(c, d) + + self._diff_cij = c + + if self._diff_baryint is None: + # initialise and cache derivative interpolator and cijs; + # reuse weights wi (which depend only on interpolation points xi), + # to avoid unnecessary re-computation + self._diff_baryint = BarycentricInterpolator(xi=self.xi, + yi=self._diff_cij @ self.yi, + wi=self.wi) + self._diff_baryint._diff_cij = self._diff_cij + + if all_lower: + # assemble matrix of derivatives from order 0 to order der-1, + # in the format required by _Interpolator1DWithDerivatives. + cn = np.zeros((der, len(x), self.r), dtype=self.dtype) + for d in range(der): + cn[d, :, :] = self._evaluate_derivatives(x, d+1, all_lower=False) + return cn + + # recursively evaluate only the derivative requested + return self._diff_baryint._evaluate_derivatives(x, der-1, all_lower=False) + + +def barycentric_interpolate(xi, yi, x, axis=0, *, der=0, rng=None): + """ + Convenience function for polynomial interpolation. + + Constructs a polynomial that passes through a given set of points, + then evaluates the polynomial. For reasons of numerical stability, + this function does not compute the coefficients of the polynomial. + + This function uses a "barycentric interpolation" method that treats + the problem as a special case of rational function interpolation. + This algorithm is quite stable, numerically, but even in a world of + exact computation, unless the `x` coordinates are chosen very + carefully - Chebyshev zeros (e.g., cos(i*pi/n)) are a good choice - + polynomial interpolation itself is a very ill-conditioned process + due to the Runge phenomenon. + + Parameters + ---------- + xi : array_like + 1-D array of x coordinates of the points the polynomial should + pass through + yi : array_like + The y coordinates of the points the polynomial should pass through. + x : scalar or array_like + Point or points at which to evaluate the interpolant. + axis : int, optional + Axis in the `yi` array corresponding to the x-coordinate values. + der : int or list or None, optional + How many derivatives to evaluate, or None for all potentially + nonzero derivatives (that is, a number equal to the number + of points), or a list of derivatives to evaluate. This number + includes the function value as the '0th' derivative. + rng : `numpy.random.Generator`, optional + Pseudorandom number generator state. When `rng` is None, a new + `numpy.random.Generator` is created using entropy from the + operating system. Types other than `numpy.random.Generator` are + passed to `numpy.random.default_rng` to instantiate a ``Generator``. + + Returns + ------- + y : scalar or array_like + Interpolated values. Shape is determined by replacing + the interpolation axis in the original array with the shape of `x`. + + See Also + -------- + BarycentricInterpolator : Barycentric interpolator + + Notes + ----- + Construction of the interpolation weights is a relatively slow process. + If you want to call this many times with the same xi (but possibly + varying yi or x) you should use the class `BarycentricInterpolator`. + This is what this function uses internally. + + Examples + -------- + We can interpolate 2D observed data using barycentric interpolation: + + >>> import numpy as np + >>> import matplotlib.pyplot as plt + >>> from scipy.interpolate import barycentric_interpolate + >>> x_observed = np.linspace(0.0, 10.0, 11) + >>> y_observed = np.sin(x_observed) + >>> x = np.linspace(min(x_observed), max(x_observed), num=100) + >>> y = barycentric_interpolate(x_observed, y_observed, x) + >>> plt.plot(x_observed, y_observed, "o", label="observation") + >>> plt.plot(x, y, label="barycentric interpolation") + >>> plt.legend() + >>> plt.show() + + """ + P = BarycentricInterpolator(xi, yi, axis=axis, rng=rng) + if der == 0: + return P(x) + elif _isscalar(der): + return P.derivative(x, der=der) + else: + return P.derivatives(x, der=np.amax(der)+1)[der] diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_rbf.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_rbf.py new file mode 100644 index 0000000000000000000000000000000000000000..ed52230dd1cce678e56ca4427e10bafd07e501c0 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_rbf.py @@ -0,0 +1,290 @@ +"""rbf - Radial basis functions for interpolation/smoothing scattered N-D data. + +Written by John Travers , February 2007 +Based closely on Matlab code by Alex Chirokov +Additional, large, improvements by Robert Hetland +Some additional alterations by Travis Oliphant +Interpolation with multi-dimensional target domain by Josua Sassen + +Permission to use, modify, and distribute this software is given under the +terms of the SciPy (BSD style) license. See LICENSE.txt that came with +this distribution for specifics. + +NO WARRANTY IS EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK. + +Copyright (c) 2006-2007, Robert Hetland +Copyright (c) 2007, John Travers + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of Robert Hetland nor the names of any + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +""" +import numpy as np + +from scipy import linalg +from scipy.special import xlogy +from scipy.spatial.distance import cdist, pdist, squareform + +__all__ = ['Rbf'] + + +class Rbf: + """ + Rbf(*args, **kwargs) + + A class for radial basis function interpolation of functions from + N-D scattered data to an M-D domain. + + .. legacy:: class + + `Rbf` is legacy code, for new usage please use `RBFInterpolator` + instead. + + Parameters + ---------- + *args : arrays + x, y, z, ..., d, where x, y, z, ... are the coordinates of the nodes + and d is the array of values at the nodes + function : str or callable, optional + The radial basis function, based on the radius, r, given by the norm + (default is Euclidean distance); the default is 'multiquadric':: + + 'multiquadric': sqrt((r/self.epsilon)**2 + 1) + 'inverse': 1.0/sqrt((r/self.epsilon)**2 + 1) + 'gaussian': exp(-(r/self.epsilon)**2) + 'linear': r + 'cubic': r**3 + 'quintic': r**5 + 'thin_plate': r**2 * log(r) + + If callable, then it must take 2 arguments (self, r). The epsilon + parameter will be available as self.epsilon. Other keyword + arguments passed in will be available as well. + + epsilon : float, optional + Adjustable constant for gaussian or multiquadrics functions + - defaults to approximate average distance between nodes (which is + a good start). + smooth : float, optional + Values greater than zero increase the smoothness of the + approximation. 0 is for interpolation (default), the function will + always go through the nodal points in this case. + norm : str, callable, optional + A function that returns the 'distance' between two points, with + inputs as arrays of positions (x, y, z, ...), and an output as an + array of distance. E.g., the default: 'euclidean', such that the result + is a matrix of the distances from each point in ``x1`` to each point in + ``x2``. For more options, see documentation of + `scipy.spatial.distances.cdist`. + mode : str, optional + Mode of the interpolation, can be '1-D' (default) or 'N-D'. When it is + '1-D' the data `d` will be considered as 1-D and flattened + internally. When it is 'N-D' the data `d` is assumed to be an array of + shape (n_samples, m), where m is the dimension of the target domain. + + + Attributes + ---------- + N : int + The number of data points (as determined by the input arrays). + di : ndarray + The 1-D array of data values at each of the data coordinates `xi`. + xi : ndarray + The 2-D array of data coordinates. + function : str or callable + The radial basis function. See description under Parameters. + epsilon : float + Parameter used by gaussian or multiquadrics functions. See Parameters. + smooth : float + Smoothing parameter. See description under Parameters. + norm : str or callable + The distance function. See description under Parameters. + mode : str + Mode of the interpolation. See description under Parameters. + nodes : ndarray + A 1-D array of node values for the interpolation. + A : internal property, do not use + + See Also + -------- + RBFInterpolator + + Examples + -------- + >>> import numpy as np + >>> from scipy.interpolate import Rbf + >>> rng = np.random.default_rng() + >>> x, y, z, d = rng.random((4, 50)) + >>> rbfi = Rbf(x, y, z, d) # radial basis function interpolator instance + >>> xi = yi = zi = np.linspace(0, 1, 20) + >>> di = rbfi(xi, yi, zi) # interpolated values + >>> di.shape + (20,) + + """ + # Available radial basis functions that can be selected as strings; + # they all start with _h_ (self._init_function relies on that) + def _h_multiquadric(self, r): + return np.sqrt((1.0/self.epsilon*r)**2 + 1) + + def _h_inverse_multiquadric(self, r): + return 1.0/np.sqrt((1.0/self.epsilon*r)**2 + 1) + + def _h_gaussian(self, r): + return np.exp(-(1.0/self.epsilon*r)**2) + + def _h_linear(self, r): + return r + + def _h_cubic(self, r): + return r**3 + + def _h_quintic(self, r): + return r**5 + + def _h_thin_plate(self, r): + return xlogy(r**2, r) + + # Setup self._function and do smoke test on initial r + def _init_function(self, r): + if isinstance(self.function, str): + self.function = self.function.lower() + _mapped = {'inverse': 'inverse_multiquadric', + 'inverse multiquadric': 'inverse_multiquadric', + 'thin-plate': 'thin_plate'} + if self.function in _mapped: + self.function = _mapped[self.function] + + func_name = "_h_" + self.function + if hasattr(self, func_name): + self._function = getattr(self, func_name) + else: + functionlist = [x[3:] for x in dir(self) + if x.startswith('_h_')] + raise ValueError("function must be a callable or one of " + + ", ".join(functionlist)) + self._function = getattr(self, "_h_"+self.function) + elif callable(self.function): + allow_one = False + if hasattr(self.function, 'func_code') or \ + hasattr(self.function, '__code__'): + val = self.function + allow_one = True + elif hasattr(self.function, "__call__"): + val = self.function.__call__.__func__ + else: + raise ValueError("Cannot determine number of arguments to " + "function") + + argcount = val.__code__.co_argcount + if allow_one and argcount == 1: + self._function = self.function + elif argcount == 2: + self._function = self.function.__get__(self, Rbf) + else: + raise ValueError("Function argument must take 1 or 2 " + "arguments.") + + a0 = self._function(r) + if a0.shape != r.shape: + raise ValueError("Callable must take array and return array of " + "the same shape") + return a0 + + def __init__(self, *args, **kwargs): + # `args` can be a variable number of arrays; we flatten them and store + # them as a single 2-D array `xi` of shape (n_args-1, array_size), + # plus a 1-D array `di` for the values. + # All arrays must have the same number of elements + self.xi = np.asarray([np.asarray(a, dtype=np.float64).flatten() + for a in args[:-1]]) + self.N = self.xi.shape[-1] + + self.mode = kwargs.pop('mode', '1-D') + + if self.mode == '1-D': + self.di = np.asarray(args[-1]).flatten() + self._target_dim = 1 + elif self.mode == 'N-D': + self.di = np.asarray(args[-1]) + self._target_dim = self.di.shape[-1] + else: + raise ValueError("Mode has to be 1-D or N-D.") + + if not all([x.size == self.di.shape[0] for x in self.xi]): + raise ValueError("All arrays must be equal length.") + + self.norm = kwargs.pop('norm', 'euclidean') + self.epsilon = kwargs.pop('epsilon', None) + if self.epsilon is None: + # default epsilon is the "the average distance between nodes" based + # on a bounding hypercube + ximax = np.amax(self.xi, axis=1) + ximin = np.amin(self.xi, axis=1) + edges = ximax - ximin + edges = edges[np.nonzero(edges)] + self.epsilon = np.power(np.prod(edges)/self.N, 1.0/edges.size) + + self.smooth = kwargs.pop('smooth', 0.0) + self.function = kwargs.pop('function', 'multiquadric') + + # attach anything left in kwargs to self for use by any user-callable + # function or to save on the object returned. + for item, value in kwargs.items(): + setattr(self, item, value) + + # Compute weights + if self._target_dim > 1: # If we have more than one target dimension, + # we first factorize the matrix + self.nodes = np.zeros((self.N, self._target_dim), dtype=self.di.dtype) + lu, piv = linalg.lu_factor(self.A) + for i in range(self._target_dim): + self.nodes[:, i] = linalg.lu_solve((lu, piv), self.di[:, i]) + else: + self.nodes = linalg.solve(self.A, self.di) + + @property + def A(self): + # this only exists for backwards compatibility: self.A was available + # and, at least technically, public. + r = squareform(pdist(self.xi.T, self.norm)) # Pairwise norm + return self._init_function(r) - np.eye(self.N)*self.smooth + + def _call_norm(self, x1, x2): + return cdist(x1.T, x2.T, self.norm) + + def __call__(self, *args): + args = [np.asarray(x) for x in args] + if not all([x.shape == y.shape for x in args for y in args]): + raise ValueError("Array lengths must be equal") + if self._target_dim > 1: + shp = args[0].shape + (self._target_dim,) + else: + shp = args[0].shape + xa = np.asarray([a.flatten() for a in args], dtype=np.float64) + r = self._call_norm(xa, self.xi) + return np.dot(self._function(r), self.nodes).reshape(shp) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_rbfinterp.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_rbfinterp.py new file mode 100644 index 0000000000000000000000000000000000000000..6690e6ccf7d5499db10efffb0ef1c0139a90d2ba --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_rbfinterp.py @@ -0,0 +1,550 @@ +"""Module for RBF interpolation.""" +import warnings +from itertools import combinations_with_replacement + +import numpy as np +from numpy.linalg import LinAlgError +from scipy.spatial import KDTree +from scipy.special import comb +from scipy.linalg.lapack import dgesv # type: ignore[attr-defined] + +from ._rbfinterp_pythran import (_build_system, + _build_evaluation_coefficients, + _polynomial_matrix) + + +__all__ = ["RBFInterpolator"] + + +# These RBFs are implemented. +_AVAILABLE = { + "linear", + "thin_plate_spline", + "cubic", + "quintic", + "multiquadric", + "inverse_multiquadric", + "inverse_quadratic", + "gaussian" + } + + +# The shape parameter does not need to be specified when using these RBFs. +_SCALE_INVARIANT = {"linear", "thin_plate_spline", "cubic", "quintic"} + + +# For RBFs that are conditionally positive definite of order m, the interpolant +# should include polynomial terms with degree >= m - 1. Define the minimum +# degrees here. These values are from Chapter 8 of Fasshauer's "Meshfree +# Approximation Methods with MATLAB". The RBFs that are not in this dictionary +# are positive definite and do not need polynomial terms. +_NAME_TO_MIN_DEGREE = { + "multiquadric": 0, + "linear": 0, + "thin_plate_spline": 1, + "cubic": 1, + "quintic": 2 + } + + +def _monomial_powers(ndim, degree): + """Return the powers for each monomial in a polynomial. + + Parameters + ---------- + ndim : int + Number of variables in the polynomial. + degree : int + Degree of the polynomial. + + Returns + ------- + (nmonos, ndim) int ndarray + Array where each row contains the powers for each variable in a + monomial. + + """ + nmonos = comb(degree + ndim, ndim, exact=True) + out = np.zeros((nmonos, ndim), dtype=np.dtype("long")) + count = 0 + for deg in range(degree + 1): + for mono in combinations_with_replacement(range(ndim), deg): + # `mono` is a tuple of variables in the current monomial with + # multiplicity indicating power (e.g., (0, 1, 1) represents x*y**2) + for var in mono: + out[count, var] += 1 + + count += 1 + + return out + + +def _build_and_solve_system(y, d, smoothing, kernel, epsilon, powers): + """Build and solve the RBF interpolation system of equations. + + Parameters + ---------- + y : (P, N) float ndarray + Data point coordinates. + d : (P, S) float ndarray + Data values at `y`. + smoothing : (P,) float ndarray + Smoothing parameter for each data point. + kernel : str + Name of the RBF. + epsilon : float + Shape parameter. + powers : (R, N) int ndarray + The exponents for each monomial in the polynomial. + + Returns + ------- + coeffs : (P + R, S) float ndarray + Coefficients for each RBF and monomial. + shift : (N,) float ndarray + Domain shift used to create the polynomial matrix. + scale : (N,) float ndarray + Domain scaling used to create the polynomial matrix. + + """ + lhs, rhs, shift, scale = _build_system( + y, d, smoothing, kernel, epsilon, powers + ) + _, _, coeffs, info = dgesv(lhs, rhs, overwrite_a=True, overwrite_b=True) + if info < 0: + raise ValueError(f"The {-info}-th argument had an illegal value.") + elif info > 0: + msg = "Singular matrix." + nmonos = powers.shape[0] + if nmonos > 0: + pmat = _polynomial_matrix((y - shift)/scale, powers) + rank = np.linalg.matrix_rank(pmat) + if rank < nmonos: + msg = ( + "Singular matrix. The matrix of monomials evaluated at " + "the data point coordinates does not have full column " + f"rank ({rank}/{nmonos})." + ) + + raise LinAlgError(msg) + + return shift, scale, coeffs + + +class RBFInterpolator: + """Radial basis function (RBF) interpolation in N dimensions. + + Parameters + ---------- + y : (npoints, ndims) array_like + 2-D array of data point coordinates. + d : (npoints, ...) array_like + N-D array of data values at `y`. The length of `d` along the first + axis must be equal to the length of `y`. Unlike some interpolators, the + interpolation axis cannot be changed. + neighbors : int, optional + If specified, the value of the interpolant at each evaluation point + will be computed using only this many nearest data points. All the data + points are used by default. + smoothing : float or (npoints, ) array_like, optional + Smoothing parameter. The interpolant perfectly fits the data when this + is set to 0. For large values, the interpolant approaches a least + squares fit of a polynomial with the specified degree. Default is 0. + kernel : str, optional + Type of RBF. This should be one of + + - 'linear' : ``-r`` + - 'thin_plate_spline' : ``r**2 * log(r)`` + - 'cubic' : ``r**3`` + - 'quintic' : ``-r**5`` + - 'multiquadric' : ``-sqrt(1 + r**2)`` + - 'inverse_multiquadric' : ``1/sqrt(1 + r**2)`` + - 'inverse_quadratic' : ``1/(1 + r**2)`` + - 'gaussian' : ``exp(-r**2)`` + + Default is 'thin_plate_spline'. + epsilon : float, optional + Shape parameter that scales the input to the RBF. If `kernel` is + 'linear', 'thin_plate_spline', 'cubic', or 'quintic', this defaults to + 1 and can be ignored because it has the same effect as scaling the + smoothing parameter. Otherwise, this must be specified. + degree : int, optional + Degree of the added polynomial. For some RBFs the interpolant may not + be well-posed if the polynomial degree is too small. Those RBFs and + their corresponding minimum degrees are + + - 'multiquadric' : 0 + - 'linear' : 0 + - 'thin_plate_spline' : 1 + - 'cubic' : 1 + - 'quintic' : 2 + + The default value is the minimum degree for `kernel` or 0 if there is + no minimum degree. Set this to -1 for no added polynomial. + + Notes + ----- + An RBF is a scalar valued function in N-dimensional space whose value at + :math:`x` can be expressed in terms of :math:`r=||x - c||`, where :math:`c` + is the center of the RBF. + + An RBF interpolant for the vector of data values :math:`d`, which are from + locations :math:`y`, is a linear combination of RBFs centered at :math:`y` + plus a polynomial with a specified degree. The RBF interpolant is written + as + + .. math:: + f(x) = K(x, y) a + P(x) b, + + where :math:`K(x, y)` is a matrix of RBFs with centers at :math:`y` + evaluated at the points :math:`x`, and :math:`P(x)` is a matrix of + monomials, which span polynomials with the specified degree, evaluated at + :math:`x`. The coefficients :math:`a` and :math:`b` are the solution to the + linear equations + + .. math:: + (K(y, y) + \\lambda I) a + P(y) b = d + + and + + .. math:: + P(y)^T a = 0, + + where :math:`\\lambda` is a non-negative smoothing parameter that controls + how well we want to fit the data. The data are fit exactly when the + smoothing parameter is 0. + + The above system is uniquely solvable if the following requirements are + met: + + - :math:`P(y)` must have full column rank. :math:`P(y)` always has full + column rank when `degree` is -1 or 0. When `degree` is 1, + :math:`P(y)` has full column rank if the data point locations are not + all collinear (N=2), coplanar (N=3), etc. + - If `kernel` is 'multiquadric', 'linear', 'thin_plate_spline', + 'cubic', or 'quintic', then `degree` must not be lower than the + minimum value listed above. + - If `smoothing` is 0, then each data point location must be distinct. + + When using an RBF that is not scale invariant ('multiquadric', + 'inverse_multiquadric', 'inverse_quadratic', or 'gaussian'), an appropriate + shape parameter must be chosen (e.g., through cross validation). Smaller + values for the shape parameter correspond to wider RBFs. The problem can + become ill-conditioned or singular when the shape parameter is too small. + + The memory required to solve for the RBF interpolation coefficients + increases quadratically with the number of data points, which can become + impractical when interpolating more than about a thousand data points. + To overcome memory limitations for large interpolation problems, the + `neighbors` argument can be specified to compute an RBF interpolant for + each evaluation point using only the nearest data points. + + .. versionadded:: 1.7.0 + + See Also + -------- + NearestNDInterpolator + LinearNDInterpolator + CloughTocher2DInterpolator + + References + ---------- + .. [1] Fasshauer, G., 2007. Meshfree Approximation Methods with Matlab. + World Scientific Publishing Co. + + .. [2] http://amadeus.math.iit.edu/~fass/603_ch3.pdf + + .. [3] Wahba, G., 1990. Spline Models for Observational Data. SIAM. + + .. [4] http://pages.stat.wisc.edu/~wahba/stat860public/lect/lect8/lect8.pdf + + Examples + -------- + Demonstrate interpolating scattered data to a grid in 2-D. + + >>> import numpy as np + >>> import matplotlib.pyplot as plt + >>> from scipy.interpolate import RBFInterpolator + >>> from scipy.stats.qmc import Halton + + >>> rng = np.random.default_rng() + >>> xobs = 2*Halton(2, seed=rng).random(100) - 1 + >>> yobs = np.sum(xobs, axis=1)*np.exp(-6*np.sum(xobs**2, axis=1)) + + >>> xgrid = np.mgrid[-1:1:50j, -1:1:50j] + >>> xflat = xgrid.reshape(2, -1).T + >>> yflat = RBFInterpolator(xobs, yobs)(xflat) + >>> ygrid = yflat.reshape(50, 50) + + >>> fig, ax = plt.subplots() + >>> ax.pcolormesh(*xgrid, ygrid, vmin=-0.25, vmax=0.25, shading='gouraud') + >>> p = ax.scatter(*xobs.T, c=yobs, s=50, ec='k', vmin=-0.25, vmax=0.25) + >>> fig.colorbar(p) + >>> plt.show() + + """ + + def __init__(self, y, d, + neighbors=None, + smoothing=0.0, + kernel="thin_plate_spline", + epsilon=None, + degree=None): + y = np.asarray(y, dtype=float, order="C") + if y.ndim != 2: + raise ValueError("`y` must be a 2-dimensional array.") + + ny, ndim = y.shape + + d_dtype = complex if np.iscomplexobj(d) else float + d = np.asarray(d, dtype=d_dtype, order="C") + if d.shape[0] != ny: + raise ValueError( + f"Expected the first axis of `d` to have length {ny}." + ) + + d_shape = d.shape[1:] + d = d.reshape((ny, -1)) + # If `d` is complex, convert it to a float array with twice as many + # columns. Otherwise, the LHS matrix would need to be converted to + # complex and take up 2x more memory than necessary. + d = d.view(float) + + if np.isscalar(smoothing): + smoothing = np.full(ny, smoothing, dtype=float) + else: + smoothing = np.asarray(smoothing, dtype=float, order="C") + if smoothing.shape != (ny,): + raise ValueError( + "Expected `smoothing` to be a scalar or have shape " + f"({ny},)." + ) + + kernel = kernel.lower() + if kernel not in _AVAILABLE: + raise ValueError(f"`kernel` must be one of {_AVAILABLE}.") + + if epsilon is None: + if kernel in _SCALE_INVARIANT: + epsilon = 1.0 + else: + raise ValueError( + "`epsilon` must be specified if `kernel` is not one of " + f"{_SCALE_INVARIANT}." + ) + else: + epsilon = float(epsilon) + + min_degree = _NAME_TO_MIN_DEGREE.get(kernel, -1) + if degree is None: + degree = max(min_degree, 0) + else: + degree = int(degree) + if degree < -1: + raise ValueError("`degree` must be at least -1.") + elif -1 < degree < min_degree: + warnings.warn( + f"`degree` should not be below {min_degree} except -1 " + f"when `kernel` is '{kernel}'." + f"The interpolant may not be uniquely " + f"solvable, and the smoothing parameter may have an " + f"unintuitive effect.", + UserWarning, stacklevel=2 + ) + + if neighbors is None: + nobs = ny + else: + # Make sure the number of nearest neighbors used for interpolation + # does not exceed the number of observations. + neighbors = int(min(neighbors, ny)) + nobs = neighbors + + powers = _monomial_powers(ndim, degree) + # The polynomial matrix must have full column rank in order for the + # interpolant to be well-posed, which is not possible if there are + # fewer observations than monomials. + if powers.shape[0] > nobs: + raise ValueError( + f"At least {powers.shape[0]} data points are required when " + f"`degree` is {degree} and the number of dimensions is {ndim}." + ) + + if neighbors is None: + shift, scale, coeffs = _build_and_solve_system( + y, d, smoothing, kernel, epsilon, powers + ) + + # Make these attributes private since they do not always exist. + self._shift = shift + self._scale = scale + self._coeffs = coeffs + + else: + self._tree = KDTree(y) + + self.y = y + self.d = d + self.d_shape = d_shape + self.d_dtype = d_dtype + self.neighbors = neighbors + self.smoothing = smoothing + self.kernel = kernel + self.epsilon = epsilon + self.powers = powers + + def _chunk_evaluator( + self, + x, + y, + shift, + scale, + coeffs, + memory_budget=1000000 + ): + """ + Evaluate the interpolation while controlling memory consumption. + We chunk the input if we need more memory than specified. + + Parameters + ---------- + x : (Q, N) float ndarray + array of points on which to evaluate + y: (P, N) float ndarray + array of points on which we know function values + shift: (N, ) ndarray + Domain shift used to create the polynomial matrix. + scale : (N,) float ndarray + Domain scaling used to create the polynomial matrix. + coeffs: (P+R, S) float ndarray + Coefficients in front of basis functions + memory_budget: int + Total amount of memory (in units of sizeof(float)) we wish + to devote for storing the array of coefficients for + interpolated points. If we need more memory than that, we + chunk the input. + + Returns + ------- + (Q, S) float ndarray + Interpolated array + """ + nx, ndim = x.shape + if self.neighbors is None: + nnei = len(y) + else: + nnei = self.neighbors + # in each chunk we consume the same space we already occupy + chunksize = memory_budget // (self.powers.shape[0] + nnei) + 1 + if chunksize <= nx: + out = np.empty((nx, self.d.shape[1]), dtype=float) + for i in range(0, nx, chunksize): + vec = _build_evaluation_coefficients( + x[i:i + chunksize, :], + y, + self.kernel, + self.epsilon, + self.powers, + shift, + scale) + out[i:i + chunksize, :] = np.dot(vec, coeffs) + else: + vec = _build_evaluation_coefficients( + x, + y, + self.kernel, + self.epsilon, + self.powers, + shift, + scale) + out = np.dot(vec, coeffs) + return out + + def __call__(self, x): + """Evaluate the interpolant at `x`. + + Parameters + ---------- + x : (Q, N) array_like + Evaluation point coordinates. + + Returns + ------- + (Q, ...) ndarray + Values of the interpolant at `x`. + + """ + x = np.asarray(x, dtype=float, order="C") + if x.ndim != 2: + raise ValueError("`x` must be a 2-dimensional array.") + + nx, ndim = x.shape + if ndim != self.y.shape[1]: + raise ValueError("Expected the second axis of `x` to have length " + f"{self.y.shape[1]}.") + + # Our memory budget for storing RBF coefficients is + # based on how many floats in memory we already occupy + # If this number is below 1e6 we just use 1e6 + # This memory budget is used to decide how we chunk + # the inputs + memory_budget = max(x.size + self.y.size + self.d.size, 1000000) + + if self.neighbors is None: + out = self._chunk_evaluator( + x, + self.y, + self._shift, + self._scale, + self._coeffs, + memory_budget=memory_budget) + else: + # Get the indices of the k nearest observation points to each + # evaluation point. + _, yindices = self._tree.query(x, self.neighbors) + if self.neighbors == 1: + # `KDTree` squeezes the output when neighbors=1. + yindices = yindices[:, None] + + # Multiple evaluation points may have the same neighborhood of + # observation points. Make the neighborhoods unique so that we only + # compute the interpolation coefficients once for each + # neighborhood. + yindices = np.sort(yindices, axis=1) + yindices, inv = np.unique(yindices, return_inverse=True, axis=0) + inv = np.reshape(inv, (-1,)) # flatten, we need 1-D indices + # `inv` tells us which neighborhood will be used by each evaluation + # point. Now we find which evaluation points will be using each + # neighborhood. + xindices = [[] for _ in range(len(yindices))] + for i, j in enumerate(inv): + xindices[j].append(i) + + out = np.empty((nx, self.d.shape[1]), dtype=float) + for xidx, yidx in zip(xindices, yindices): + # `yidx` are the indices of the observations in this + # neighborhood. `xidx` are the indices of the evaluation points + # that are using this neighborhood. + xnbr = x[xidx] + ynbr = self.y[yidx] + dnbr = self.d[yidx] + snbr = self.smoothing[yidx] + shift, scale, coeffs = _build_and_solve_system( + ynbr, + dnbr, + snbr, + self.kernel, + self.epsilon, + self.powers, + ) + out[xidx] = self._chunk_evaluator( + xnbr, + ynbr, + shift, + scale, + coeffs, + memory_budget=memory_budget) + + out = out.view(self.d_dtype) + out = out.reshape((nx, ) + self.d_shape) + return out diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_rgi.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_rgi.py new file mode 100644 index 0000000000000000000000000000000000000000..8e20200568ed961849b5510e8626cdbe6e5b9643 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/_rgi.py @@ -0,0 +1,759 @@ +__all__ = ['RegularGridInterpolator', 'interpn'] + +import itertools + +import numpy as np + +import scipy.sparse.linalg as ssl + +from ._interpnd import _ndim_coords_from_arrays +from ._cubic import PchipInterpolator +from ._rgi_cython import evaluate_linear_2d, find_indices +from ._bsplines import make_interp_spline +from ._fitpack2 import RectBivariateSpline +from ._ndbspline import make_ndbspl + + +def _check_points(points): + descending_dimensions = [] + grid = [] + for i, p in enumerate(points): + # early make points float + # see https://github.com/scipy/scipy/pull/17230 + p = np.asarray(p, dtype=float) + if not np.all(p[1:] > p[:-1]): + if np.all(p[1:] < p[:-1]): + # input is descending, so make it ascending + descending_dimensions.append(i) + p = np.flip(p) + else: + raise ValueError( + "The points in dimension %d must be strictly " + "ascending or descending" % i) + # see https://github.com/scipy/scipy/issues/17716 + p = np.ascontiguousarray(p) + grid.append(p) + return tuple(grid), tuple(descending_dimensions) + + +def _check_dimensionality(points, values): + if len(points) > values.ndim: + raise ValueError("There are %d point arrays, but values has %d " + "dimensions" % (len(points), values.ndim)) + for i, p in enumerate(points): + if not np.asarray(p).ndim == 1: + raise ValueError("The points in dimension %d must be " + "1-dimensional" % i) + if not values.shape[i] == len(p): + raise ValueError("There are %d points and %d values in " + "dimension %d" % (len(p), values.shape[i], i)) + + +class RegularGridInterpolator: + """ + Interpolator on a regular or rectilinear grid in arbitrary dimensions. + + The data must be defined on a rectilinear grid; that is, a rectangular + grid with even or uneven spacing. Linear, nearest-neighbor, spline + interpolations are supported. After setting up the interpolator object, + the interpolation method may be chosen at each evaluation. + + Parameters + ---------- + points : tuple of ndarray of float, with shapes (m1, ), ..., (mn, ) + The points defining the regular grid in n dimensions. The points in + each dimension (i.e. every elements of the points tuple) must be + strictly ascending or descending. + + values : array_like, shape (m1, ..., mn, ...) + The data on the regular grid in n dimensions. Complex data is + accepted. + + method : str, optional + The method of interpolation to perform. Supported are "linear", + "nearest", "slinear", "cubic", "quintic" and "pchip". This + parameter will become the default for the object's ``__call__`` + method. Default is "linear". + + bounds_error : bool, optional + If True, when interpolated values are requested outside of the + domain of the input data, a ValueError is raised. + If False, then `fill_value` is used. + Default is True. + + fill_value : float or None, optional + The value to use for points outside of the interpolation domain. + If None, values outside the domain are extrapolated. + Default is ``np.nan``. + + solver : callable, optional + Only used for methods "slinear", "cubic" and "quintic". + Sparse linear algebra solver for construction of the NdBSpline instance. + Default is the iterative solver `scipy.sparse.linalg.gcrotmk`. + + .. versionadded:: 1.13 + + solver_args: dict, optional + Additional arguments to pass to `solver`, if any. + + .. versionadded:: 1.13 + + Methods + ------- + __call__ + + Attributes + ---------- + grid : tuple of ndarrays + The points defining the regular grid in n dimensions. + This tuple defines the full grid via + ``np.meshgrid(*grid, indexing='ij')`` + values : ndarray + Data values at the grid. + method : str + Interpolation method. + fill_value : float or ``None`` + Use this value for out-of-bounds arguments to `__call__`. + bounds_error : bool + If ``True``, out-of-bounds argument raise a ``ValueError``. + + Notes + ----- + Contrary to `LinearNDInterpolator` and `NearestNDInterpolator`, this class + avoids expensive triangulation of the input data by taking advantage of the + regular grid structure. + + In other words, this class assumes that the data is defined on a + *rectilinear* grid. + + .. versionadded:: 0.14 + + The 'slinear'(k=1), 'cubic'(k=3), and 'quintic'(k=5) methods are + tensor-product spline interpolators, where `k` is the spline degree, + If any dimension has fewer points than `k` + 1, an error will be raised. + + .. versionadded:: 1.9 + + If the input data is such that dimensions have incommensurate + units and differ by many orders of magnitude, the interpolant may have + numerical artifacts. Consider rescaling the data before interpolating. + + **Choosing a solver for spline methods** + + Spline methods, "slinear", "cubic" and "quintic" involve solving a + large sparse linear system at instantiation time. Depending on data, + the default solver may or may not be adequate. When it is not, you may + need to experiment with an optional `solver` argument, where you may + choose between the direct solver (`scipy.sparse.linalg.spsolve`) or + iterative solvers from `scipy.sparse.linalg`. You may need to supply + additional parameters via the optional `solver_args` parameter (for instance, + you may supply the starting value or target tolerance). See the + `scipy.sparse.linalg` documentation for the full list of available options. + + Alternatively, you may instead use the legacy methods, "slinear_legacy", + "cubic_legacy" and "quintic_legacy". These methods allow faster construction + but evaluations will be much slower. + + Examples + -------- + **Evaluate a function on the points of a 3-D grid** + + As a first example, we evaluate a simple example function on the points of + a 3-D grid: + + >>> from scipy.interpolate import RegularGridInterpolator + >>> import numpy as np + >>> def f(x, y, z): + ... return 2 * x**3 + 3 * y**2 - z + >>> x = np.linspace(1, 4, 11) + >>> y = np.linspace(4, 7, 22) + >>> z = np.linspace(7, 9, 33) + >>> xg, yg ,zg = np.meshgrid(x, y, z, indexing='ij', sparse=True) + >>> data = f(xg, yg, zg) + + ``data`` is now a 3-D array with ``data[i, j, k] = f(x[i], y[j], z[k])``. + Next, define an interpolating function from this data: + + >>> interp = RegularGridInterpolator((x, y, z), data) + + Evaluate the interpolating function at the two points + ``(x,y,z) = (2.1, 6.2, 8.3)`` and ``(3.3, 5.2, 7.1)``: + + >>> pts = np.array([[2.1, 6.2, 8.3], + ... [3.3, 5.2, 7.1]]) + >>> interp(pts) + array([ 125.80469388, 146.30069388]) + + which is indeed a close approximation to + + >>> f(2.1, 6.2, 8.3), f(3.3, 5.2, 7.1) + (125.54200000000002, 145.894) + + **Interpolate and extrapolate a 2D dataset** + + As a second example, we interpolate and extrapolate a 2D data set: + + >>> x, y = np.array([-2, 0, 4]), np.array([-2, 0, 2, 5]) + >>> def ff(x, y): + ... return x**2 + y**2 + + >>> xg, yg = np.meshgrid(x, y, indexing='ij') + >>> data = ff(xg, yg) + >>> interp = RegularGridInterpolator((x, y), data, + ... bounds_error=False, fill_value=None) + + >>> import matplotlib.pyplot as plt + >>> fig = plt.figure() + >>> ax = fig.add_subplot(projection='3d') + >>> ax.scatter(xg.ravel(), yg.ravel(), data.ravel(), + ... s=60, c='k', label='data') + + Evaluate and plot the interpolator on a finer grid + + >>> xx = np.linspace(-4, 9, 31) + >>> yy = np.linspace(-4, 9, 31) + >>> X, Y = np.meshgrid(xx, yy, indexing='ij') + + >>> # interpolator + >>> ax.plot_wireframe(X, Y, interp((X, Y)), rstride=3, cstride=3, + ... alpha=0.4, color='m', label='linear interp') + + >>> # ground truth + >>> ax.plot_wireframe(X, Y, ff(X, Y), rstride=3, cstride=3, + ... alpha=0.4, label='ground truth') + >>> plt.legend() + >>> plt.show() + + Other examples are given + :ref:`in the tutorial `. + + See Also + -------- + NearestNDInterpolator : Nearest neighbor interpolator on *unstructured* + data in N dimensions + + LinearNDInterpolator : Piecewise linear interpolator on *unstructured* data + in N dimensions + + interpn : a convenience function which wraps `RegularGridInterpolator` + + scipy.ndimage.map_coordinates : interpolation on grids with equal spacing + (suitable for e.g., N-D image resampling) + + References + ---------- + .. [1] Python package *regulargrid* by Johannes Buchner, see + https://pypi.python.org/pypi/regulargrid/ + .. [2] Wikipedia, "Trilinear interpolation", + https://en.wikipedia.org/wiki/Trilinear_interpolation + .. [3] Weiser, Alan, and Sergio E. Zarantonello. "A note on piecewise linear + and multilinear table interpolation in many dimensions." MATH. + COMPUT. 50.181 (1988): 189-196. + https://www.ams.org/journals/mcom/1988-50-181/S0025-5718-1988-0917826-0/S0025-5718-1988-0917826-0.pdf + :doi:`10.1090/S0025-5718-1988-0917826-0` + + """ + # this class is based on code originally programmed by Johannes Buchner, + # see https://github.com/JohannesBuchner/regulargrid + + _SPLINE_DEGREE_MAP = {"slinear": 1, "cubic": 3, "quintic": 5, 'pchip': 3, + "slinear_legacy": 1, "cubic_legacy": 3, "quintic_legacy": 5,} + _SPLINE_METHODS_recursive = {"slinear_legacy", "cubic_legacy", + "quintic_legacy", "pchip"} + _SPLINE_METHODS_ndbspl = {"slinear", "cubic", "quintic"} + _SPLINE_METHODS = list(_SPLINE_DEGREE_MAP.keys()) + _ALL_METHODS = ["linear", "nearest"] + _SPLINE_METHODS + + def __init__(self, points, values, method="linear", bounds_error=True, + fill_value=np.nan, *, solver=None, solver_args=None): + if method not in self._ALL_METHODS: + raise ValueError(f"Method '{method}' is not defined") + elif method in self._SPLINE_METHODS: + self._validate_grid_dimensions(points, method) + self.method = method + self._spline = None + self.bounds_error = bounds_error + self.grid, self._descending_dimensions = _check_points(points) + self.values = self._check_values(values) + self._check_dimensionality(self.grid, self.values) + self.fill_value = self._check_fill_value(self.values, fill_value) + if self._descending_dimensions: + self.values = np.flip(values, axis=self._descending_dimensions) + if self.method == "pchip" and np.iscomplexobj(self.values): + msg = ("`PchipInterpolator` only works with real values. If you are trying " + "to use the real components of the passed array, use `np.real` on " + "the array before passing to `RegularGridInterpolator`.") + raise ValueError(msg) + if method in self._SPLINE_METHODS_ndbspl: + if solver_args is None: + solver_args = {} + self._spline = self._construct_spline(method, solver, **solver_args) + else: + if solver is not None or solver_args: + raise ValueError( + f"{method =} does not accept the 'solver' argument. Got " + f" {solver = } and with arguments {solver_args}." + ) + + def _construct_spline(self, method, solver=None, **solver_args): + if solver is None: + solver = ssl.gcrotmk + spl = make_ndbspl( + self.grid, self.values, self._SPLINE_DEGREE_MAP[method], + solver=solver, **solver_args + ) + return spl + + def _check_dimensionality(self, grid, values): + _check_dimensionality(grid, values) + + def _check_points(self, points): + return _check_points(points) + + def _check_values(self, values): + if not hasattr(values, 'ndim'): + # allow reasonable duck-typed values + values = np.asarray(values) + + if hasattr(values, 'dtype') and hasattr(values, 'astype'): + if not np.issubdtype(values.dtype, np.inexact): + values = values.astype(float) + + return values + + def _check_fill_value(self, values, fill_value): + if fill_value is not None: + fill_value_dtype = np.asarray(fill_value).dtype + if (hasattr(values, 'dtype') and not + np.can_cast(fill_value_dtype, values.dtype, + casting='same_kind')): + raise ValueError("fill_value must be either 'None' or " + "of a type compatible with values") + return fill_value + + def __call__(self, xi, method=None, *, nu=None): + """ + Interpolation at coordinates. + + Parameters + ---------- + xi : ndarray of shape (..., ndim) + The coordinates to evaluate the interpolator at. + + method : str, optional + The method of interpolation to perform. Supported are "linear", + "nearest", "slinear", "cubic", "quintic" and "pchip". Default is + the method chosen when the interpolator was created. + + nu : sequence of ints, length ndim, optional + If not None, the orders of the derivatives to evaluate. + Each entry must be non-negative. + Only allowed for methods "slinear", "cubic" and "quintic". + + .. versionadded:: 1.13 + + Returns + ------- + values_x : ndarray, shape xi.shape[:-1] + values.shape[ndim:] + Interpolated values at `xi`. See notes for behaviour when + ``xi.ndim == 1``. + + Notes + ----- + In the case that ``xi.ndim == 1`` a new axis is inserted into + the 0 position of the returned array, values_x, so its shape is + instead ``(1,) + values.shape[ndim:]``. + + Examples + -------- + Here we define a nearest-neighbor interpolator of a simple function + + >>> import numpy as np + >>> x, y = np.array([0, 1, 2]), np.array([1, 3, 7]) + >>> def f(x, y): + ... return x**2 + y**2 + >>> data = f(*np.meshgrid(x, y, indexing='ij', sparse=True)) + >>> from scipy.interpolate import RegularGridInterpolator + >>> interp = RegularGridInterpolator((x, y), data, method='nearest') + + By construction, the interpolator uses the nearest-neighbor + interpolation + + >>> interp([[1.5, 1.3], [0.3, 4.5]]) + array([2., 9.]) + + We can however evaluate the linear interpolant by overriding the + `method` parameter + + >>> interp([[1.5, 1.3], [0.3, 4.5]], method='linear') + array([ 4.7, 24.3]) + """ + _spline = self._spline + method = self.method if method is None else method + is_method_changed = self.method != method + if method not in self._ALL_METHODS: + raise ValueError(f"Method '{method}' is not defined") + if is_method_changed and method in self._SPLINE_METHODS_ndbspl: + _spline = self._construct_spline(method) + + if nu is not None and method not in self._SPLINE_METHODS_ndbspl: + raise ValueError( + f"Can only compute derivatives for methods " + f"{self._SPLINE_METHODS_ndbspl}, got {method =}." + ) + + xi, xi_shape, ndim, nans, out_of_bounds = self._prepare_xi(xi) + + if method == "linear": + indices, norm_distances = self._find_indices(xi.T) + if (ndim == 2 and hasattr(self.values, 'dtype') and + self.values.ndim == 2 and self.values.flags.writeable and + self.values.dtype in (np.float64, np.complex128) and + self.values.dtype.byteorder == '='): + # until cython supports const fused types, the fast path + # cannot support non-writeable values + # a fast path + out = np.empty(indices.shape[1], dtype=self.values.dtype) + result = evaluate_linear_2d(self.values, + indices, + norm_distances, + self.grid, + out) + else: + result = self._evaluate_linear(indices, norm_distances) + elif method == "nearest": + indices, norm_distances = self._find_indices(xi.T) + result = self._evaluate_nearest(indices, norm_distances) + elif method in self._SPLINE_METHODS: + if is_method_changed: + self._validate_grid_dimensions(self.grid, method) + if method in self._SPLINE_METHODS_recursive: + result = self._evaluate_spline(xi, method) + else: + result = _spline(xi, nu=nu) + + if not self.bounds_error and self.fill_value is not None: + result[out_of_bounds] = self.fill_value + + # f(nan) = nan, if any + if np.any(nans): + result[nans] = np.nan + return result.reshape(xi_shape[:-1] + self.values.shape[ndim:]) + + def _prepare_xi(self, xi): + ndim = len(self.grid) + xi = _ndim_coords_from_arrays(xi, ndim=ndim) + if xi.shape[-1] != len(self.grid): + raise ValueError("The requested sample points xi have dimension " + f"{xi.shape[-1]} but this " + f"RegularGridInterpolator has dimension {ndim}") + + xi_shape = xi.shape + xi = xi.reshape(-1, xi_shape[-1]) + xi = np.asarray(xi, dtype=float) + + # find nans in input + nans = np.any(np.isnan(xi), axis=-1) + + if self.bounds_error: + for i, p in enumerate(xi.T): + if not np.logical_and(np.all(self.grid[i][0] <= p), + np.all(p <= self.grid[i][-1])): + raise ValueError("One of the requested xi is out of bounds " + "in dimension %d" % i) + out_of_bounds = None + else: + out_of_bounds = self._find_out_of_bounds(xi.T) + + return xi, xi_shape, ndim, nans, out_of_bounds + + def _evaluate_linear(self, indices, norm_distances): + # slice for broadcasting over trailing dimensions in self.values + vslice = (slice(None),) + (None,)*(self.values.ndim - len(indices)) + + # Compute shifting up front before zipping everything together + shift_norm_distances = [1 - yi for yi in norm_distances] + shift_indices = [i + 1 for i in indices] + + # The formula for linear interpolation in 2d takes the form: + # values = self.values[(i0, i1)] * (1 - y0) * (1 - y1) + \ + # self.values[(i0, i1 + 1)] * (1 - y0) * y1 + \ + # self.values[(i0 + 1, i1)] * y0 * (1 - y1) + \ + # self.values[(i0 + 1, i1 + 1)] * y0 * y1 + # We pair i with 1 - yi (zipped1) and i + 1 with yi (zipped2) + zipped1 = zip(indices, shift_norm_distances) + zipped2 = zip(shift_indices, norm_distances) + + # Take all products of zipped1 and zipped2 and iterate over them + # to get the terms in the above formula. This corresponds to iterating + # over the vertices of a hypercube. + hypercube = itertools.product(*zip(zipped1, zipped2)) + value = np.array([0.]) + for h in hypercube: + edge_indices, weights = zip(*h) + weight = np.array([1.]) + for w in weights: + weight = weight * w + term = np.asarray(self.values[edge_indices]) * weight[vslice] + value = value + term # cannot use += because broadcasting + return value + + def _evaluate_nearest(self, indices, norm_distances): + idx_res = [np.where(yi <= .5, i, i + 1) + for i, yi in zip(indices, norm_distances)] + return self.values[tuple(idx_res)] + + def _validate_grid_dimensions(self, points, method): + k = self._SPLINE_DEGREE_MAP[method] + for i, point in enumerate(points): + ndim = len(np.atleast_1d(point)) + if ndim <= k: + raise ValueError(f"There are {ndim} points in dimension {i}," + f" but method {method} requires at least " + f" {k+1} points per dimension.") + + def _evaluate_spline(self, xi, method): + # ensure xi is 2D list of points to evaluate (`m` is the number of + # points and `n` is the number of interpolation dimensions, + # ``n == len(self.grid)``.) + if xi.ndim == 1: + xi = xi.reshape((1, xi.size)) + m, n = xi.shape + + # Reorder the axes: n-dimensional process iterates over the + # interpolation axes from the last axis downwards: E.g. for a 4D grid + # the order of axes is 3, 2, 1, 0. Each 1D interpolation works along + # the 0th axis of its argument array (for 1D routine it's its ``y`` + # array). Thus permute the interpolation axes of `values` *and keep + # trailing dimensions trailing*. + axes = tuple(range(self.values.ndim)) + axx = axes[:n][::-1] + axes[n:] + values = self.values.transpose(axx) + + if method == 'pchip': + _eval_func = self._do_pchip + else: + _eval_func = self._do_spline_fit + k = self._SPLINE_DEGREE_MAP[method] + + # Non-stationary procedure: difficult to vectorize this part entirely + # into numpy-level operations. Unfortunately this requires explicit + # looping over each point in xi. + + # can at least vectorize the first pass across all points in the + # last variable of xi. + last_dim = n - 1 + first_values = _eval_func(self.grid[last_dim], + values, + xi[:, last_dim], + k) + + # the rest of the dimensions have to be on a per point-in-xi basis + shape = (m, *self.values.shape[n:]) + result = np.empty(shape, dtype=self.values.dtype) + for j in range(m): + # Main process: Apply 1D interpolate in each dimension + # sequentially, starting with the last dimension. + # These are then "folded" into the next dimension in-place. + folded_values = first_values[j, ...] + for i in range(last_dim-1, -1, -1): + # Interpolate for each 1D from the last dimensions. + # This collapses each 1D sequence into a scalar. + folded_values = _eval_func(self.grid[i], + folded_values, + xi[j, i], + k) + result[j, ...] = folded_values + + return result + + @staticmethod + def _do_spline_fit(x, y, pt, k): + local_interp = make_interp_spline(x, y, k=k, axis=0) + values = local_interp(pt) + return values + + @staticmethod + def _do_pchip(x, y, pt, k): + local_interp = PchipInterpolator(x, y, axis=0) + values = local_interp(pt) + return values + + def _find_indices(self, xi): + return find_indices(self.grid, xi) + + def _find_out_of_bounds(self, xi): + # check for out of bounds xi + out_of_bounds = np.zeros((xi.shape[1]), dtype=bool) + # iterate through dimensions + for x, grid in zip(xi, self.grid): + out_of_bounds += x < grid[0] + out_of_bounds += x > grid[-1] + return out_of_bounds + + +def interpn(points, values, xi, method="linear", bounds_error=True, + fill_value=np.nan): + """ + Multidimensional interpolation on regular or rectilinear grids. + + Strictly speaking, not all regular grids are supported - this function + works on *rectilinear* grids, that is, a rectangular grid with even or + uneven spacing. + + Parameters + ---------- + points : tuple of ndarray of float, with shapes (m1, ), ..., (mn, ) + The points defining the regular grid in n dimensions. The points in + each dimension (i.e. every elements of the points tuple) must be + strictly ascending or descending. + + values : array_like, shape (m1, ..., mn, ...) + The data on the regular grid in n dimensions. Complex data is + accepted. + + .. deprecated:: 1.13.0 + Complex data is deprecated with ``method="pchip"`` and will raise an + error in SciPy 1.15.0. This is because ``PchipInterpolator`` only + works with real values. If you are trying to use the real components of + the passed array, use ``np.real`` on ``values``. + + xi : ndarray of shape (..., ndim) + The coordinates to sample the gridded data at + + method : str, optional + The method of interpolation to perform. Supported are "linear", + "nearest", "slinear", "cubic", "quintic", "pchip", and "splinef2d". + "splinef2d" is only supported for 2-dimensional data. + + bounds_error : bool, optional + If True, when interpolated values are requested outside of the + domain of the input data, a ValueError is raised. + If False, then `fill_value` is used. + + fill_value : number, optional + If provided, the value to use for points outside of the + interpolation domain. If None, values outside + the domain are extrapolated. Extrapolation is not supported by method + "splinef2d". + + Returns + ------- + values_x : ndarray, shape xi.shape[:-1] + values.shape[ndim:] + Interpolated values at `xi`. See notes for behaviour when + ``xi.ndim == 1``. + + See Also + -------- + NearestNDInterpolator : Nearest neighbor interpolation on unstructured + data in N dimensions + LinearNDInterpolator : Piecewise linear interpolant on unstructured data + in N dimensions + RegularGridInterpolator : interpolation on a regular or rectilinear grid + in arbitrary dimensions (`interpn` wraps this + class). + RectBivariateSpline : Bivariate spline approximation over a rectangular mesh + scipy.ndimage.map_coordinates : interpolation on grids with equal spacing + (suitable for e.g., N-D image resampling) + + Notes + ----- + + .. versionadded:: 0.14 + + In the case that ``xi.ndim == 1`` a new axis is inserted into + the 0 position of the returned array, values_x, so its shape is + instead ``(1,) + values.shape[ndim:]``. + + If the input data is such that input dimensions have incommensurate + units and differ by many orders of magnitude, the interpolant may have + numerical artifacts. Consider rescaling the data before interpolation. + + Examples + -------- + Evaluate a simple example function on the points of a regular 3-D grid: + + >>> import numpy as np + >>> from scipy.interpolate import interpn + >>> def value_func_3d(x, y, z): + ... return 2 * x + 3 * y - z + >>> x = np.linspace(0, 4, 5) + >>> y = np.linspace(0, 5, 6) + >>> z = np.linspace(0, 6, 7) + >>> points = (x, y, z) + >>> values = value_func_3d(*np.meshgrid(*points, indexing='ij')) + + Evaluate the interpolating function at a point + + >>> point = np.array([2.21, 3.12, 1.15]) + >>> print(interpn(points, values, point)) + [12.63] + + """ + # sanity check 'method' kwarg + if method not in ["linear", "nearest", "cubic", "quintic", "pchip", + "splinef2d", "slinear", + "slinear_legacy", "cubic_legacy", "quintic_legacy"]: + raise ValueError("interpn only understands the methods 'linear', " + "'nearest', 'slinear', 'cubic', 'quintic', 'pchip', " + f"and 'splinef2d'. You provided {method}.") + + if not hasattr(values, 'ndim'): + values = np.asarray(values) + + ndim = values.ndim + if ndim > 2 and method == "splinef2d": + raise ValueError("The method splinef2d can only be used for " + "2-dimensional input data") + if not bounds_error and fill_value is None and method == "splinef2d": + raise ValueError("The method splinef2d does not support extrapolation.") + + # sanity check consistency of input dimensions + if len(points) > ndim: + raise ValueError("There are %d point arrays, but values has %d " + "dimensions" % (len(points), ndim)) + if len(points) != ndim and method == 'splinef2d': + raise ValueError("The method splinef2d can only be used for " + "scalar data with one point per coordinate") + + grid, descending_dimensions = _check_points(points) + _check_dimensionality(grid, values) + + # sanity check requested xi + xi = _ndim_coords_from_arrays(xi, ndim=len(grid)) + if xi.shape[-1] != len(grid): + raise ValueError("The requested sample points xi have dimension " + "%d, but this RegularGridInterpolator has " + "dimension %d" % (xi.shape[-1], len(grid))) + + if bounds_error: + for i, p in enumerate(xi.T): + if not np.logical_and(np.all(grid[i][0] <= p), + np.all(p <= grid[i][-1])): + raise ValueError("One of the requested xi is out of bounds " + "in dimension %d" % i) + + # perform interpolation + if method in RegularGridInterpolator._ALL_METHODS: + interp = RegularGridInterpolator(points, values, method=method, + bounds_error=bounds_error, + fill_value=fill_value) + return interp(xi) + elif method == "splinef2d": + xi_shape = xi.shape + xi = xi.reshape(-1, xi.shape[-1]) + + # RectBivariateSpline doesn't support fill_value; we need to wrap here + idx_valid = np.all((grid[0][0] <= xi[:, 0], xi[:, 0] <= grid[0][-1], + grid[1][0] <= xi[:, 1], xi[:, 1] <= grid[1][-1]), + axis=0) + result = np.empty_like(xi[:, 0]) + + # make a copy of values for RectBivariateSpline + interp = RectBivariateSpline(points[0], points[1], values[:]) + result[idx_valid] = interp.ev(xi[idx_valid, 0], xi[idx_valid, 1]) + result[np.logical_not(idx_valid)] = fill_value + + return result.reshape(xi_shape[:-1]) + else: + raise ValueError(f"unknown {method = }") diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/dfitpack.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/dfitpack.py new file mode 100644 index 0000000000000000000000000000000000000000..e10da3b3fd0c69dede4767dc17b62c327818ecce --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/dfitpack.py @@ -0,0 +1,44 @@ +# This file is not meant for public use and will be removed in SciPy v2.0.0. +# Use the `scipy.interpolate` namespace for importing the functions +# included below. + +from scipy._lib.deprecation import _sub_module_deprecation + + +__all__ = [ # noqa: F822 + 'bispeu', + 'bispev', + 'curfit', + 'dblint', + 'fpchec', + 'fpcurf0', + 'fpcurf1', + 'fpcurfm1', + 'parcur', + 'parder', + 'pardeu', + 'pardtc', + 'percur', + 'regrid_smth', + 'regrid_smth_spher', + 'spalde', + 'spherfit_lsq', + 'spherfit_smth', + 'splder', + 'splev', + 'splint', + 'sproot', + 'surfit_lsq', + 'surfit_smth', + 'types', +] + + +def __dir__(): + return __all__ + + +def __getattr__(name): + return _sub_module_deprecation(sub_package="interpolate", module="dfitpack", + private_modules=["_dfitpack"], all=__all__, + attribute=name) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/fitpack.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/fitpack.py new file mode 100644 index 0000000000000000000000000000000000000000..6490c93fe02b4c665b032d09e2ad3c269e1f7970 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/fitpack.py @@ -0,0 +1,31 @@ +# This file is not meant for public use and will be removed in SciPy v2.0.0. +# Use the `scipy.interpolate` namespace for importing the functions +# included below. + +from scipy._lib.deprecation import _sub_module_deprecation + + +__all__ = [ # noqa: F822 + 'BSpline', + 'bisplev', + 'bisplrep', + 'insert', + 'spalde', + 'splantider', + 'splder', + 'splev', + 'splint', + 'splprep', + 'splrep', + 'sproot', +] + + +def __dir__(): + return __all__ + + +def __getattr__(name): + return _sub_module_deprecation(sub_package="interpolate", module="fitpack", + private_modules=["_fitpack_py"], all=__all__, + attribute=name) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/fitpack2.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/fitpack2.py new file mode 100644 index 0000000000000000000000000000000000000000..f993961f94d913d632aa3d2cc7b1348659a6a613 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/fitpack2.py @@ -0,0 +1,29 @@ +# This file is not meant for public use and will be removed in SciPy v2.0.0. +# Use the `scipy.interpolate` namespace for importing the functions +# included below. + +from scipy._lib.deprecation import _sub_module_deprecation + + +__all__ = [ # noqa: F822 + 'BivariateSpline', + 'InterpolatedUnivariateSpline', + 'LSQBivariateSpline', + 'LSQSphereBivariateSpline', + 'LSQUnivariateSpline', + 'RectBivariateSpline', + 'RectSphereBivariateSpline', + 'SmoothBivariateSpline', + 'SmoothSphereBivariateSpline', + 'UnivariateSpline', +] + + +def __dir__(): + return __all__ + + +def __getattr__(name): + return _sub_module_deprecation(sub_package="interpolate", module="fitpack2", + private_modules=["_fitpack2"], all=__all__, + attribute=name) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/interpnd.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/interpnd.py new file mode 100644 index 0000000000000000000000000000000000000000..4288ac233fdde98dbb19aed84b916cfd15302f4c --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/interpnd.py @@ -0,0 +1,25 @@ +# This file is not meant for public use and will be removed in SciPy v2.0.0. +# Use the `scipy.interpolate` namespace for importing the functions +# included below. + +from scipy._lib.deprecation import _sub_module_deprecation + + +__all__ = [ # noqa: F822 + 'CloughTocher2DInterpolator', + 'GradientEstimationWarning', + 'LinearNDInterpolator', + 'NDInterpolatorBase', + 'estimate_gradients_2d_global', +] + + +def __dir__(): + return __all__ + + +def __getattr__(name): + return _sub_module_deprecation(sub_package="interpolate", module="interpnd", + private_modules=["_interpnd"], all=__all__, + attribute=name) + diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/interpolate.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/interpolate.py new file mode 100644 index 0000000000000000000000000000000000000000..341d13954c81130cceb8afe070db023a82550e7a --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/interpolate.py @@ -0,0 +1,30 @@ +# This file is not meant for public use and will be removed in SciPy v2.0.0. +# Use the `scipy.interpolate` namespace for importing the functions +# included below. + +from scipy._lib.deprecation import _sub_module_deprecation + + +__all__ = [ # noqa: F822 + 'BPoly', + 'BSpline', + 'NdPPoly', + 'PPoly', + 'RectBivariateSpline', + 'RegularGridInterpolator', + 'interp1d', + 'interp2d', + 'interpn', + 'lagrange', + 'make_interp_spline', +] + + +def __dir__(): + return __all__ + + +def __getattr__(name): + return _sub_module_deprecation(sub_package="interpolate", module="interpolate", + private_modules=["_interpolate", "fitpack2", "_rgi"], + all=__all__, attribute=name) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/ndgriddata.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/ndgriddata.py new file mode 100644 index 0000000000000000000000000000000000000000..20373eaaedaa1cdec6c7a4bc12639d9658bfa85b --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/ndgriddata.py @@ -0,0 +1,23 @@ +# This file is not meant for public use and will be removed in SciPy v2.0.0. +# Use the `scipy.interpolate` namespace for importing the functions +# included below. + +from scipy._lib.deprecation import _sub_module_deprecation + + +__all__ = [ # noqa: F822 + 'CloughTocher2DInterpolator', + 'LinearNDInterpolator', + 'NearestNDInterpolator', + 'griddata', +] + + +def __dir__(): + return __all__ + + +def __getattr__(name): + return _sub_module_deprecation(sub_package="interpolate", module="ndgriddata", + private_modules=["_ndgriddata"], all=__all__, + attribute=name) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/polyint.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/polyint.py new file mode 100644 index 0000000000000000000000000000000000000000..e81306304abffb313ab5abe09116a162642a9d67 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/polyint.py @@ -0,0 +1,24 @@ +# This file is not meant for public use and will be removed in SciPy v2.0.0. +# Use the `scipy.interpolate` namespace for importing the functions +# included below. + +from scipy._lib.deprecation import _sub_module_deprecation + + +__all__ = [ # noqa: F822 + 'BarycentricInterpolator', + 'KroghInterpolator', + 'approximate_taylor_polynomial', + 'barycentric_interpolate', + 'krogh_interpolate', +] + + +def __dir__(): + return __all__ + + +def __getattr__(name): + return _sub_module_deprecation(sub_package="interpolate", module="polyint", + private_modules=["_polyint"], all=__all__, + attribute=name) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/rbf.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/rbf.py new file mode 100644 index 0000000000000000000000000000000000000000..772752ef536f4a3b47fb6f9b5d250c5d7f198d85 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/rbf.py @@ -0,0 +1,18 @@ +# This file is not meant for public use and will be removed in SciPy v2.0.0. +# Use the `scipy.interpolate` namespace for importing the functions +# included below. + +from scipy._lib.deprecation import _sub_module_deprecation + + +__all__ = ["Rbf"] # noqa: F822 + + +def __dir__(): + return __all__ + + +def __getattr__(name): + return _sub_module_deprecation(sub_package="interpolate", module="rbf", + private_modules=["_rbf"], all=__all__, + attribute=name) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_bary_rational.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_bary_rational.py new file mode 100644 index 0000000000000000000000000000000000000000..fbeea868ea24292693ced4cfb230badc3a551a89 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_bary_rational.py @@ -0,0 +1,368 @@ +# Copyright (c) 2017, The Chancellor, Masters and Scholars of the University +# of Oxford, and the Chebfun Developers. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the University of Oxford nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +from math import factorial + +import numpy as np +from numpy.testing import assert_allclose, assert_equal, assert_array_less +import pytest +import scipy +from scipy.interpolate import AAA, FloaterHormannInterpolator, BarycentricInterpolator + +TOL = 1e4 * np.finfo(np.float64).eps +UNIT_INTERVAL = np.linspace(-1, 1, num=1000) +PTS = np.logspace(-15, 0, base=10, num=500) +PTS = np.concatenate([-PTS[::-1], [0], PTS]) + + +@pytest.mark.parametrize("method", [AAA, FloaterHormannInterpolator]) +@pytest.mark.parametrize("dtype", [np.float32, np.float64, np.complex64, np.complex128]) +def test_dtype_preservation(method, dtype): + rtol = np.finfo(dtype).eps ** 0.75 * 100 + if method is FloaterHormannInterpolator: + rtol *= 100 + rng = np.random.default_rng(59846294526092468) + + z = np.linspace(-1, 1, dtype=dtype) + r = method(z, np.sin(z)) + + z2 = rng.uniform(-1, 1, size=100).astype(dtype) + assert_allclose(r(z2), np.sin(z2), rtol=rtol) + assert r(z2).dtype == dtype + + if method is AAA: + assert r.support_points.dtype == dtype + assert r.support_values.dtype == dtype + assert r.errors.dtype == z.real.dtype + assert r.weights.dtype == dtype + assert r.poles().dtype == np.result_type(dtype, 1j) + assert r.residues().dtype == np.result_type(dtype, 1j) + assert r.roots().dtype == np.result_type(dtype, 1j) + + +@pytest.mark.parametrize("method", [AAA, FloaterHormannInterpolator]) +@pytest.mark.parametrize("dtype", [np.int16, np.int32, np.int64]) +def test_integer_promotion(method, dtype): + z = np.arange(10, dtype=dtype) + r = method(z, z) + assert r.weights.dtype == np.result_type(dtype, 1.0) + if method is AAA: + assert r.support_points.dtype == np.result_type(dtype, 1.0) + assert r.support_values.dtype == np.result_type(dtype, 1.0) + assert r.errors.dtype == np.result_type(dtype, 1.0) + assert r.poles().dtype == np.result_type(dtype, 1j) + assert r.residues().dtype == np.result_type(dtype, 1j) + assert r.roots().dtype == np.result_type(dtype, 1j) + + assert r(z).dtype == np.result_type(dtype, 1.0) + + +class TestAAA: + def test_input_validation(self): + with pytest.raises(ValueError, match="same size"): + AAA([0], [1, 1]) + with pytest.raises(ValueError, match="1-D"): + AAA([[0], [0]], [[1], [1]]) + with pytest.raises(ValueError, match="finite"): + AAA([np.inf], [1]) + with pytest.raises(TypeError): + AAA([1], [1], max_terms=1.0) + with pytest.raises(ValueError, match="greater"): + AAA([1], [1], max_terms=-1) + + @pytest.mark.thread_unsafe + def test_convergence_error(self): + with pytest.warns(RuntimeWarning, match="AAA failed"): + AAA(UNIT_INTERVAL, np.exp(UNIT_INTERVAL), max_terms=1) + + # The following tests are based on: + # https://github.com/chebfun/chebfun/blob/master/tests/chebfun/test_aaa.m + def test_exp(self): + f = np.exp(UNIT_INTERVAL) + r = AAA(UNIT_INTERVAL, f) + + assert_allclose(r(UNIT_INTERVAL), f, atol=TOL) + assert_equal(r(np.nan), np.nan) + assert np.isfinite(r(np.inf)) + + m1 = r.support_points.size + r = AAA(UNIT_INTERVAL, f, rtol=1e-3) + assert r.support_points.size < m1 + + def test_tan(self): + f = np.tan(np.pi * UNIT_INTERVAL) + r = AAA(UNIT_INTERVAL, f) + + assert_allclose(r(UNIT_INTERVAL), f, atol=10 * TOL, rtol=1.4e-7) + assert_allclose(np.min(np.abs(r.roots())), 0, atol=3e-10) + assert_allclose(np.min(np.abs(r.poles() - 0.5)), 0, atol=TOL) + # Test for spurious poles (poles with tiny residue are likely spurious) + assert np.min(np.abs(r.residues())) > 1e-13 + + def test_short_cases(self): + # Computed using Chebfun: + # >> format long + # >> [r, pol, res, zer, zj, fj, wj, errvec] = aaa([1 2], [0 1]) + z = np.array([0, 1]) + f = np.array([1, 2]) + r = AAA(z, f, rtol=1e-13) + assert_allclose(r(z), f, atol=TOL) + assert_allclose(r.poles(), 0.5) + assert_allclose(r.residues(), 0.25) + assert_allclose(r.roots(), 1/3) + assert_equal(r.support_points, z) + assert_equal(r.support_values, f) + assert_allclose(r.weights, [0.707106781186547, 0.707106781186547]) + assert_equal(r.errors, [1, 0]) + + # >> format long + # >> [r, pol, res, zer, zj, fj, wj, errvec] = aaa([1 0 0], [0 1 2]) + z = np.array([0, 1, 2]) + f = np.array([1, 0, 0]) + r = AAA(z, f, rtol=1e-13) + assert_allclose(r(z), f, atol=TOL) + assert_allclose(np.sort(r.poles()), + np.sort([1.577350269189626, 0.422649730810374])) + assert_allclose(np.sort(r.residues()), + np.sort([-0.070441621801729, -0.262891711531604])) + assert_allclose(np.sort(r.roots()), np.sort([2, 1])) + assert_equal(r.support_points, z) + assert_equal(r.support_values, f) + assert_allclose(r.weights, [0.577350269189626, 0.577350269189626, + 0.577350269189626]) + assert_equal(r.errors, [1, 1, 0]) + + def test_scale_invariance(self): + z = np.linspace(0.3, 1.5) + f = np.exp(z) / (1 + 1j) + r1 = AAA(z, f) + r2 = AAA(z, (2**311 * f).astype(np.complex128)) + r3 = AAA(z, (2**-311 * f).astype(np.complex128)) + assert_equal(r1(0.2j), 2**-311 * r2(0.2j)) + assert_equal(r1(1.4), 2**311 * r3(1.4)) + + def test_log_func(self): + rng = np.random.default_rng(1749382759832758297) + z = rng.standard_normal(10000) + 3j * rng.standard_normal(10000) + + def f(z): + return np.log(5 - z) / (1 + z**2) + + r = AAA(z, f(z)) + assert_allclose(r(0), f(0), atol=TOL) + + def test_infinite_data(self): + z = np.linspace(-1, 1) + r = AAA(z, scipy.special.gamma(z)) + assert_allclose(r(0.63), scipy.special.gamma(0.63), atol=1e-15) + + def test_nan(self): + x = np.linspace(0, 20) + with np.errstate(invalid="ignore"): + f = np.sin(x) / x + r = AAA(x, f) + assert_allclose(r(2), np.sin(2) / 2, atol=1e-15) + + def test_residues(self): + x = np.linspace(-1.337, 2, num=537) + r = AAA(x, np.exp(x) / x) + ii = np.flatnonzero(np.abs(r.poles()) < 1e-8) + assert_allclose(r.residues()[ii], 1, atol=1e-15) + + r = AAA(x, (1 + 1j) * scipy.special.gamma(x)) + ii = np.flatnonzero(abs(r.poles() - (-1)) < 1e-8) + assert_allclose(r.residues()[ii], -1 - 1j, atol=1e-15) + + # The following tests are based on: + # https://github.com/complexvariables/RationalFunctionApproximation.jl/blob/main/test/interval.jl + @pytest.mark.parametrize("func,atol,rtol", + [(lambda x: np.abs(x + 0.5 + 0.01j), 5e-13, 1e-7), + (lambda x: np.sin(1/(1.05 - x)), 2e-13, 1e-7), + (lambda x: np.exp(-1/(x**2)), 3.5e-13, 0), + (lambda x: np.exp(-100*x**2), 8e-13, 0), + (lambda x: np.exp(-10/(1.2 - x)), 1e-14, 0), + (lambda x: 1/(1+np.exp(100*(x + 0.5))), 2e-13, 1e-7), + (lambda x: np.abs(x - 0.95), 1e-6, 1e-7)]) + def test_basic_functions(self, func, atol, rtol): + with np.errstate(divide="ignore"): + f = func(PTS) + assert_allclose(AAA(UNIT_INTERVAL, func(UNIT_INTERVAL))(PTS), + f, atol=atol, rtol=rtol) + + def test_poles_zeros_residues(self): + def f(z): + return (z+1) * (z+2) / ((z+3) * (z+4)) + r = AAA(UNIT_INTERVAL, f(UNIT_INTERVAL)) + assert_allclose(np.sum(r.poles() + r.roots()), -10, atol=1e-12) + + def f(z): + return 2/(3 + z) + 5/(z - 2j) + r = AAA(UNIT_INTERVAL, f(UNIT_INTERVAL)) + assert_allclose(r.residues().prod(), 10, atol=1e-8) + + r = AAA(UNIT_INTERVAL, np.sin(10*np.pi*UNIT_INTERVAL)) + assert_allclose(np.sort(np.abs(r.roots()))[18], 0.9, atol=1e-12) + + def f(z): + return (z - (3 + 3j))/(z + 2) + r = AAA(UNIT_INTERVAL, f(UNIT_INTERVAL)) + assert_allclose(r.poles()[0]*r.roots()[0], -6-6j, atol=1e-12) + + @pytest.mark.parametrize("func", + [lambda z: np.zeros_like(z), lambda z: z, lambda z: 1j*z, + lambda z: z**2 + z, lambda z: z**3 + z, + lambda z: 1/(1.1 + z), lambda z: 1/(1 + 1j*z), + lambda z: 1/(3 + z + z**2), lambda z: 1/(1.01 + z**3)]) + def test_polynomials_and_reciprocals(self, func): + assert_allclose(AAA(UNIT_INTERVAL, func(UNIT_INTERVAL))(PTS), + func(PTS), atol=2e-13) + + # The following tests are taken from: + # https://github.com/macd/BaryRational.jl/blob/main/test/test_aaa.jl + def test_spiral(self): + z = np.exp(np.linspace(-0.5, 0.5 + 15j*np.pi, num=1000)) + r = AAA(z, np.tan(np.pi*z/2)) + assert_allclose(np.sort(np.abs(r.poles()))[:4], [1, 1, 3, 3], rtol=9e-7) + + @pytest.mark.thread_unsafe + def test_spiral_cleanup(self): + z = np.exp(np.linspace(-0.5, 0.5 + 15j*np.pi, num=1000)) + # here we set `rtol=0` to force froissart doublets, without cleanup there + # are many spurious poles + with pytest.warns(RuntimeWarning): + r = AAA(z, np.tan(np.pi*z/2), rtol=0, max_terms=60, clean_up=False) + n_spurious = np.sum(np.abs(r.residues()) < 1e-14) + with pytest.warns(RuntimeWarning): + assert r.clean_up() >= 1 + # check there are less potentially spurious poles than before + assert np.sum(np.abs(r.residues()) < 1e-14) < n_spurious + # check accuracy + assert_allclose(r(z), np.tan(np.pi*z/2), atol=6e-12, rtol=3e-12) + + +class TestFloaterHormann: + def runge(self, z): + return 1/(1 + z**2) + + def scale(self, n, d): + return (-1)**(np.arange(n) + d) * factorial(d) + + def test_iv(self): + with pytest.raises(ValueError, match="`x`"): + FloaterHormannInterpolator([[0]], [0], d=0) + with pytest.raises(ValueError, match="`y`"): + FloaterHormannInterpolator([0], 0, d=0) + with pytest.raises(ValueError, match="dimension"): + FloaterHormannInterpolator([0], [[1, 1], [1, 1]], d=0) + with pytest.raises(ValueError, match="finite"): + FloaterHormannInterpolator([np.inf], [1], d=0) + with pytest.raises(ValueError, match="`d`"): + FloaterHormannInterpolator([0], [0], d=-1) + with pytest.raises(ValueError, match="`d`"): + FloaterHormannInterpolator([0], [0], d=10) + with pytest.raises(TypeError): + FloaterHormannInterpolator([0], [0], d=0.0) + + # reference values from Floater and Hormann 2007 page 8. + @pytest.mark.parametrize("d,expected", [ + (0, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]), + (1, [1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1]), + (2, [1, 3, 4, 4, 4, 4, 4, 4, 4, 3, 1]), + (3, [1, 4, 7, 8, 8, 8, 8, 8, 7, 4, 1]), + (4, [1, 5, 11, 15, 16, 16, 16, 15, 11, 5, 1]) + ]) + def test_uniform_grid(self, d, expected): + # Check against explicit results on an uniform grid + x = np.arange(11) + r = FloaterHormannInterpolator(x, 0.0*x, d=d) + assert_allclose(r.weights.ravel()*self.scale(x.size, d), expected, + rtol=1e-15, atol=1e-15) + + @pytest.mark.parametrize("d", range(10)) + def test_runge(self, d): + x = np.linspace(0, 1, 51) + rng = np.random.default_rng(802754237598370893) + xx = rng.uniform(0, 1, size=1000) + y = self.runge(x) + h = x[1] - x[0] + + r = FloaterHormannInterpolator(x, y, d=d) + + tol = 10*h**(d+1) + assert_allclose(r(xx), self.runge(xx), atol=1e-10, rtol=tol) + # check interpolation property + assert_equal(r(x), self.runge(x)) + + def test_complex(self): + x = np.linspace(-1, 1) + z = x + x*1j + r = FloaterHormannInterpolator(z, np.sin(z), d=12) + xx = np.linspace(-1, 1, num=1000) + zz = xx + xx*1j + assert_allclose(r(zz), np.sin(zz), rtol=1e-12) + + def test_polyinterp(self): + # check that when d=n-1 FH gives a polynomial interpolant + x = np.linspace(0, 1, 11) + xx = np.linspace(0, 1, 1001) + y = np.sin(x) + r = FloaterHormannInterpolator(x, y, d=x.size-1) + p = BarycentricInterpolator(x, y) + assert_allclose(r(xx), p(xx), rtol=1e-12, atol=1e-12) + + @pytest.mark.parametrize("y_shape", [(2,), (2, 3, 1), (1, 5, 6, 4)]) + @pytest.mark.parametrize("xx_shape", [(100), (10, 10)]) + def test_trailing_dim(self, y_shape, xx_shape): + x = np.linspace(0, 1) + y = np.broadcast_to( + np.expand_dims(np.sin(x), tuple(range(1, len(y_shape) + 1))), + x.shape + y_shape + ) + + r = FloaterHormannInterpolator(x, y) + + rng = np.random.default_rng(897138947238097528091759187597) + xx = rng.random(xx_shape) + yy = np.broadcast_to( + np.expand_dims(np.sin(xx), tuple(range(xx.ndim, len(y_shape) + xx.ndim))), + xx.shape + y_shape + ) + rr = r(xx) + assert rr.shape == xx.shape + y_shape + assert_allclose(rr, yy, rtol=1e-6) + + def test_zeros(self): + x = np.linspace(0, 10, num=100) + r = FloaterHormannInterpolator(x, np.sin(np.pi*x)) + + err = np.abs(np.subtract.outer(r.roots(), np.arange(11))).min(axis=0) + assert_array_less(err, 1e-5) + + def test_no_poles(self): + x = np.linspace(-1, 1) + r = FloaterHormannInterpolator(x, 1/x**2) + p = r.poles() + mask = (p.real >= -1) & (p.real <= 1) & (np.abs(p.imag) < 1.e-12) + assert np.sum(mask) == 0 diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_bsplines.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_bsplines.py new file mode 100644 index 0000000000000000000000000000000000000000..e8b1b2b58afcf5ad5a34babe65fff96bf268df03 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_bsplines.py @@ -0,0 +1,3658 @@ +import os +import operator +import itertools +import math +import threading + +import numpy as np +from numpy.testing import suppress_warnings +from scipy._lib._array_api import xp_assert_equal, xp_assert_close +from pytest import raises as assert_raises +import pytest + +from scipy.interpolate import ( + BSpline, BPoly, PPoly, make_interp_spline, make_lsq_spline, + splev, splrep, splprep, splder, splantider, sproot, splint, insert, + CubicSpline, NdBSpline, make_smoothing_spline, RegularGridInterpolator, +) +import scipy.linalg as sl +import scipy.sparse.linalg as ssl + +from scipy.interpolate._bsplines import (_not_a_knot, _augknt, + _woodbury_algorithm, _periodic_knots, + _make_interp_per_full_matr) + +from scipy.interpolate import generate_knots, make_splrep, make_splprep + +import scipy.interpolate._fitpack_impl as _impl +from scipy._lib._util import AxisError +from scipy._lib._testutils import _run_concurrent_barrier + +# XXX: move to the interpolate namespace +from scipy.interpolate._ndbspline import make_ndbspl + +from scipy.interpolate import _dfitpack as dfitpack +from scipy.interpolate import _bsplines as _b +from scipy.interpolate import _dierckx + + +class TestBSpline: + + def test_ctor(self): + # knots should be an ordered 1-D array of finite real numbers + assert_raises((TypeError, ValueError), BSpline, + **dict(t=[1, 1.j], c=[1.], k=0)) + with np.errstate(invalid='ignore'): + assert_raises(ValueError, BSpline, **dict(t=[1, np.nan], c=[1.], k=0)) + assert_raises(ValueError, BSpline, **dict(t=[1, np.inf], c=[1.], k=0)) + assert_raises(ValueError, BSpline, **dict(t=[1, -1], c=[1.], k=0)) + assert_raises(ValueError, BSpline, **dict(t=[[1], [1]], c=[1.], k=0)) + + # for n+k+1 knots and degree k need at least n coefficients + assert_raises(ValueError, BSpline, **dict(t=[0, 1, 2], c=[1], k=0)) + assert_raises(ValueError, BSpline, + **dict(t=[0, 1, 2, 3, 4], c=[1., 1.], k=2)) + + # non-integer orders + assert_raises(TypeError, BSpline, + **dict(t=[0., 0., 1., 2., 3., 4.], c=[1., 1., 1.], k="cubic")) + assert_raises(TypeError, BSpline, + **dict(t=[0., 0., 1., 2., 3., 4.], c=[1., 1., 1.], k=2.5)) + + # basic interval cannot have measure zero (here: [1..1]) + assert_raises(ValueError, BSpline, + **dict(t=[0., 0, 1, 1, 2, 3], c=[1., 1, 1], k=2)) + + # tck vs self.tck + n, k = 11, 3 + t = np.arange(n+k+1, dtype=np.float64) + c = np.random.random(n) + b = BSpline(t, c, k) + + xp_assert_close(t, b.t) + xp_assert_close(c, b.c) + assert k == b.k + + def test_tck(self): + b = _make_random_spline() + tck = b.tck + + xp_assert_close(b.t, tck[0], atol=1e-15, rtol=1e-15) + xp_assert_close(b.c, tck[1], atol=1e-15, rtol=1e-15) + assert b.k == tck[2] + + # b.tck is read-only + with pytest.raises(AttributeError): + b.tck = 'foo' + + def test_degree_0(self): + xx = np.linspace(0, 1, 10) + + b = BSpline(t=[0, 1], c=[3.], k=0) + xp_assert_close(b(xx), np.ones_like(xx) * 3.0) + + b = BSpline(t=[0, 0.35, 1], c=[3, 4], k=0) + xp_assert_close(b(xx), np.where(xx < 0.35, 3.0, 4.0)) + + def test_degree_1(self): + t = [0, 1, 2, 3, 4] + c = [1, 2, 3] + k = 1 + b = BSpline(t, c, k) + + x = np.linspace(1, 3, 50) + xp_assert_close(c[0]*B_012(x) + c[1]*B_012(x-1) + c[2]*B_012(x-2), + b(x), atol=1e-14) + xp_assert_close(splev(x, (t, c, k)), b(x), atol=1e-14) + + def test_bernstein(self): + # a special knot vector: Bernstein polynomials + k = 3 + t = np.asarray([0]*(k+1) + [1]*(k+1)) + c = np.asarray([1., 2., 3., 4.]) + bp = BPoly(c.reshape(-1, 1), [0, 1]) + bspl = BSpline(t, c, k) + + xx = np.linspace(-1., 2., 10) + xp_assert_close(bp(xx, extrapolate=True), + bspl(xx, extrapolate=True), atol=1e-14) + xp_assert_close(splev(xx, (t, c, k)), + bspl(xx), atol=1e-14) + + def test_rndm_naive_eval(self): + # test random coefficient spline *on the base interval*, + # t[k] <= x < t[-k-1] + b = _make_random_spline() + t, c, k = b.tck + xx = np.linspace(t[k], t[-k-1], 50) + y_b = b(xx) + + y_n = [_naive_eval(x, t, c, k) for x in xx] + xp_assert_close(y_b, y_n, atol=1e-14) + + y_n2 = [_naive_eval_2(x, t, c, k) for x in xx] + xp_assert_close(y_b, y_n2, atol=1e-14) + + def test_rndm_splev(self): + b = _make_random_spline() + t, c, k = b.tck + xx = np.linspace(t[k], t[-k-1], 50) + xp_assert_close(b(xx), splev(xx, (t, c, k)), atol=1e-14) + + def test_rndm_splrep(self): + rng = np.random.RandomState(1234) + x = np.sort(rng.random(20)) + y = rng.random(20) + + tck = splrep(x, y) + b = BSpline(*tck) + + t, k = b.t, b.k + xx = np.linspace(t[k], t[-k-1], 80) + xp_assert_close(b(xx), splev(xx, tck), atol=1e-14) + + def test_rndm_unity(self): + b = _make_random_spline() + b.c = np.ones_like(b.c) + xx = np.linspace(b.t[b.k], b.t[-b.k-1], 100) + xp_assert_close(b(xx), np.ones_like(xx)) + + def test_vectorization(self): + rng = np.random.RandomState(1234) + n, k = 22, 3 + t = np.sort(rng.random(n)) + c = rng.random(size=(n, 6, 7)) + b = BSpline(t, c, k) + tm, tp = t[k], t[-k-1] + xx = tm + (tp - tm) * rng.random((3, 4, 5)) + assert b(xx).shape == (3, 4, 5, 6, 7) + + def test_len_c(self): + # for n+k+1 knots, only first n coefs are used. + # and BTW this is consistent with FITPACK + rng = np.random.RandomState(1234) + n, k = 33, 3 + t = np.sort(rng.random(n+k+1)) + c = rng.random(n) + + # pad coefficients with random garbage + c_pad = np.r_[c, rng.random(k+1)] + + b, b_pad = BSpline(t, c, k), BSpline(t, c_pad, k) + + dt = t[-1] - t[0] + xx = np.linspace(t[0] - dt, t[-1] + dt, 50) + xp_assert_close(b(xx), b_pad(xx), atol=1e-14) + xp_assert_close(b(xx), splev(xx, (t, c, k)), atol=1e-14) + xp_assert_close(b(xx), splev(xx, (t, c_pad, k)), atol=1e-14) + + def test_endpoints(self, num_parallel_threads): + # base interval is closed + b = _make_random_spline() + t, _, k = b.tck + tm, tp = t[k], t[-k-1] + # atol = 1e-9 if num_parallel_threads == 1 else 1e-7 + for extrap in (True, False): + xp_assert_close(b([tm, tp], extrap), + b([tm + 1e-10, tp - 1e-10], extrap), atol=1e-9, rtol=1e-7) + + def test_continuity(self, num_parallel_threads): + # assert continuity at internal knots + b = _make_random_spline() + t, _, k = b.tck + xp_assert_close(b(t[k+1:-k-1] - 1e-10), b(t[k+1:-k-1] + 1e-10), + atol=1e-9) + + def test_extrap(self): + b = _make_random_spline() + t, c, k = b.tck + dt = t[-1] - t[0] + xx = np.linspace(t[k] - dt, t[-k-1] + dt, 50) + mask = (t[k] < xx) & (xx < t[-k-1]) + + # extrap has no effect within the base interval + xp_assert_close(b(xx[mask], extrapolate=True), + b(xx[mask], extrapolate=False)) + + # extrapolated values agree with FITPACK + xp_assert_close(b(xx, extrapolate=True), + splev(xx, (t, c, k), ext=0)) + + def test_default_extrap(self): + # BSpline defaults to extrapolate=True + b = _make_random_spline() + t, _, k = b.tck + xx = [t[0] - 1, t[-1] + 1] + yy = b(xx) + assert not np.all(np.isnan(yy)) + + def test_periodic_extrap(self): + rng = np.random.RandomState(1234) + t = np.sort(rng.random(8)) + c = rng.random(4) + k = 3 + b = BSpline(t, c, k, extrapolate='periodic') + n = t.size - (k + 1) + + dt = t[-1] - t[0] + xx = np.linspace(t[k] - dt, t[n] + dt, 50) + xy = t[k] + (xx - t[k]) % (t[n] - t[k]) + xp_assert_close(b(xx), splev(xy, (t, c, k))) + + # Direct check + xx = [-1, 0, 0.5, 1] + xy = t[k] + (xx - t[k]) % (t[n] - t[k]) + xp_assert_equal(b(xx, extrapolate='periodic'), b(xy, extrapolate=True)) + + def test_ppoly(self): + b = _make_random_spline() + t, c, k = b.tck + pp = PPoly.from_spline((t, c, k)) + + xx = np.linspace(t[k], t[-k], 100) + xp_assert_close(b(xx), pp(xx), atol=1e-14, rtol=1e-14) + + def test_derivative_rndm(self): + b = _make_random_spline() + t, c, k = b.tck + xx = np.linspace(t[0], t[-1], 50) + xx = np.r_[xx, t] + + for der in range(1, k+1): + yd = splev(xx, (t, c, k), der=der) + xp_assert_close(yd, b(xx, nu=der), atol=1e-14) + + # higher derivatives all vanish + xp_assert_close(b(xx, nu=k+1), np.zeros_like(xx), atol=1e-14) + + def test_derivative_jumps(self): + # example from de Boor, Chap IX, example (24) + # NB: knots augmented & corresp coefs are zeroed out + # in agreement with the convention (29) + k = 2 + t = [-1, -1, 0, 1, 1, 3, 4, 6, 6, 6, 7, 7] + rng = np.random.RandomState(1234) + c = np.r_[0, 0, rng.random(5), 0, 0] + b = BSpline(t, c, k) + + # b is continuous at x != 6 (triple knot) + x = np.asarray([1, 3, 4, 6]) + xp_assert_close(b(x[x != 6] - 1e-10), + b(x[x != 6] + 1e-10)) + assert not np.allclose(b(6.-1e-10), b(6+1e-10)) + + # 1st derivative jumps at double knots, 1 & 6: + x0 = np.asarray([3, 4]) + xp_assert_close(b(x0 - 1e-10, nu=1), + b(x0 + 1e-10, nu=1)) + x1 = np.asarray([1, 6]) + assert not np.allclose(b(x1 - 1e-10, nu=1), b(x1 + 1e-10, nu=1)) + + # 2nd derivative is not guaranteed to be continuous either + assert not np.allclose(b(x - 1e-10, nu=2), b(x + 1e-10, nu=2)) + + def test_basis_element_quadratic(self): + xx = np.linspace(-1, 4, 20) + b = BSpline.basis_element(t=[0, 1, 2, 3]) + xp_assert_close(b(xx), + splev(xx, (b.t, b.c, b.k)), atol=1e-14) + xp_assert_close(b(xx), + B_0123(xx), atol=1e-14) + + b = BSpline.basis_element(t=[0, 1, 1, 2]) + xx = np.linspace(0, 2, 10) + xp_assert_close(b(xx), + np.where(xx < 1, xx*xx, (2.-xx)**2), atol=1e-14) + + def test_basis_element_rndm(self): + b = _make_random_spline() + t, c, k = b.tck + xx = np.linspace(t[k], t[-k-1], 20) + xp_assert_close(b(xx), _sum_basis_elements(xx, t, c, k), atol=1e-14) + + def test_cmplx(self): + b = _make_random_spline() + t, c, k = b.tck + cc = c * (1. + 3.j) + + b = BSpline(t, cc, k) + b_re = BSpline(t, b.c.real, k) + b_im = BSpline(t, b.c.imag, k) + + xx = np.linspace(t[k], t[-k-1], 20) + xp_assert_close(b(xx).real, b_re(xx), atol=1e-14) + xp_assert_close(b(xx).imag, b_im(xx), atol=1e-14) + + def test_nan(self): + # nan in, nan out. + b = BSpline.basis_element([0, 1, 1, 2]) + assert np.isnan(b(np.nan)) + + def test_derivative_method(self): + b = _make_random_spline(k=5) + t, c, k = b.tck + b0 = BSpline(t, c, k) + xx = np.linspace(t[k], t[-k-1], 20) + for j in range(1, k): + b = b.derivative() + xp_assert_close(b0(xx, j), b(xx), atol=1e-12, rtol=1e-12) + + def test_antiderivative_method(self): + b = _make_random_spline() + t, c, k = b.tck + xx = np.linspace(t[k], t[-k-1], 20) + xp_assert_close(b.antiderivative().derivative()(xx), + b(xx), atol=1e-14, rtol=1e-14) + + # repeat with N-D array for c + c = np.c_[c, c, c] + c = np.dstack((c, c)) + b = BSpline(t, c, k) + xp_assert_close(b.antiderivative().derivative()(xx), + b(xx), atol=1e-14, rtol=1e-14) + + def test_integral(self): + b = BSpline.basis_element([0, 1, 2]) # x for x < 1 else 2 - x + xp_assert_close(b.integrate(0, 1), np.asarray(0.5)) + xp_assert_close(b.integrate(1, 0), np.asarray(-1 * 0.5)) + xp_assert_close(b.integrate(1, 0), np.asarray(-0.5)) + + # extrapolate or zeros outside of [0, 2]; default is yes + xp_assert_close(b.integrate(-1, 1), np.asarray(0.0)) + xp_assert_close(b.integrate(-1, 1, extrapolate=True), np.asarray(0.0)) + xp_assert_close(b.integrate(-1, 1, extrapolate=False), np.asarray(0.5)) + xp_assert_close(b.integrate(1, -1, extrapolate=False), np.asarray(-1 * 0.5)) + + # Test ``_fitpack._splint()`` + xp_assert_close(b.integrate(1, -1, extrapolate=False), + np.asarray(_impl.splint(1, -1, b.tck))) + + # Test ``extrapolate='periodic'``. + b.extrapolate = 'periodic' + i = b.antiderivative() + period_int = np.asarray(i(2) - i(0)) + + xp_assert_close(b.integrate(0, 2), period_int) + xp_assert_close(b.integrate(2, 0), np.asarray(-1 * period_int)) + xp_assert_close(b.integrate(-9, -7), period_int) + xp_assert_close(b.integrate(-8, -4), np.asarray(2 * period_int)) + + xp_assert_close(b.integrate(0.5, 1.5), + np.asarray(i(1.5) - i(0.5))) + xp_assert_close(b.integrate(1.5, 3), + np.asarray(i(1) - i(0) + i(2) - i(1.5))) + xp_assert_close(b.integrate(1.5 + 12, 3 + 12), + np.asarray(i(1) - i(0) + i(2) - i(1.5))) + xp_assert_close(b.integrate(1.5, 3 + 12), + np.asarray(i(1) - i(0) + i(2) - i(1.5) + 6 * period_int)) + + xp_assert_close(b.integrate(0, -1), np.asarray(i(0) - i(1))) + xp_assert_close(b.integrate(-9, -10), np.asarray(i(0) - i(1))) + xp_assert_close(b.integrate(0, -9), + np.asarray(i(1) - i(2) - 4 * period_int)) + + def test_integrate_ppoly(self): + # test .integrate method to be consistent with PPoly.integrate + x = [0, 1, 2, 3, 4] + b = make_interp_spline(x, x) + b.extrapolate = 'periodic' + p = PPoly.from_spline(b) + + for x0, x1 in [(-5, 0.5), (0.5, 5), (-4, 13)]: + xp_assert_close(b.integrate(x0, x1), + p.integrate(x0, x1)) + + def test_integrate_0D_always(self): + # make sure the result is always a 0D array (not a python scalar) + b = BSpline.basis_element([0, 1, 2]) + for extrapolate in (True, False): + res = b.integrate(0, 1, extrapolate=extrapolate) + assert isinstance(res, np.ndarray) + assert res.ndim == 0 + + def test_subclassing(self): + # classmethods should not decay to the base class + class B(BSpline): + pass + + b = B.basis_element([0, 1, 2, 2]) + assert b.__class__ == B + assert b.derivative().__class__ == B + assert b.antiderivative().__class__ == B + + @pytest.mark.parametrize('axis', range(-4, 4)) + def test_axis(self, axis): + n, k = 22, 3 + t = np.linspace(0, 1, n + k + 1) + sh = [6, 7, 8] + # We need the positive axis for some of the indexing and slices used + # in this test. + pos_axis = axis % 4 + sh.insert(pos_axis, n) # [22, 6, 7, 8] etc + sh = tuple(sh) + rng = np.random.RandomState(1234) + c = rng.random(size=sh) + b = BSpline(t, c, k, axis=axis) + assert b.c.shape == (sh[pos_axis],) + sh[:pos_axis] + sh[pos_axis+1:] + + xp = rng.random((3, 4, 5)) + assert b(xp).shape == sh[:pos_axis] + xp.shape + sh[pos_axis+1:] + + # -c.ndim <= axis < c.ndim + for ax in [-c.ndim - 1, c.ndim]: + assert_raises(AxisError, BSpline, + **dict(t=t, c=c, k=k, axis=ax)) + + # derivative, antiderivative keeps the axis + for b1 in [BSpline(t, c, k, axis=axis).derivative(), + BSpline(t, c, k, axis=axis).derivative(2), + BSpline(t, c, k, axis=axis).antiderivative(), + BSpline(t, c, k, axis=axis).antiderivative(2)]: + assert b1.axis == b.axis + + def test_neg_axis(self): + k = 2 + t = [0, 1, 2, 3, 4, 5, 6] + c = np.array([[-1, 2, 0, -1], [2, 0, -3, 1]]) + + spl = BSpline(t, c, k, axis=-1) + spl0 = BSpline(t, c[0], k) + spl1 = BSpline(t, c[1], k) + xp_assert_equal(spl(2.5), [spl0(2.5), spl1(2.5)]) + + @pytest.mark.thread_unsafe + def test_design_matrix_bc_types(self): + ''' + Splines with different boundary conditions are built on different + types of vectors of knots. As far as design matrix depends only on + vector of knots, `k` and `x` it is useful to make tests for different + boundary conditions (and as following different vectors of knots). + ''' + def run_design_matrix_tests(n, k, bc_type): + ''' + To avoid repetition of code the following function is provided. + ''' + rng = np.random.RandomState(1234) + x = np.sort(rng.random_sample(n) * 40 - 20) + y = rng.random_sample(n) * 40 - 20 + if bc_type == "periodic": + y[0] = y[-1] + + bspl = make_interp_spline(x, y, k=k, bc_type=bc_type) + + c = np.eye(len(bspl.t) - k - 1) + des_matr_def = BSpline(bspl.t, c, k)(x) + des_matr_csr = BSpline.design_matrix(x, + bspl.t, + k).toarray() + xp_assert_close(des_matr_csr @ bspl.c, y, atol=1e-14) + xp_assert_close(des_matr_def, des_matr_csr, atol=1e-14) + + # "clamped" and "natural" work only with `k = 3` + n = 11 + k = 3 + for bc in ["clamped", "natural"]: + run_design_matrix_tests(n, k, bc) + + # "not-a-knot" works with odd `k` + for k in range(3, 8, 2): + run_design_matrix_tests(n, k, "not-a-knot") + + # "periodic" works with any `k` (even more than `n`) + n = 5 # smaller `n` to test `k > n` case + for k in range(2, 7): + run_design_matrix_tests(n, k, "periodic") + + @pytest.mark.parametrize('extrapolate', [False, True, 'periodic']) + @pytest.mark.parametrize('degree', range(5)) + def test_design_matrix_same_as_BSpline_call(self, extrapolate, degree): + """Test that design_matrix(x) is equivalent to BSpline(..)(x).""" + rng = np.random.RandomState(1234) + x = rng.random_sample(10 * (degree + 1)) + xmin, xmax = np.amin(x), np.amax(x) + k = degree + t = np.r_[np.linspace(xmin - 2, xmin - 1, degree), + np.linspace(xmin, xmax, 2 * (degree + 1)), + np.linspace(xmax + 1, xmax + 2, degree)] + c = np.eye(len(t) - k - 1) + bspline = BSpline(t, c, k, extrapolate) + xp_assert_close( + bspline(x), BSpline.design_matrix(x, t, k, extrapolate).toarray() + ) + + # extrapolation regime + x = np.array([xmin - 10, xmin - 1, xmax + 1.5, xmax + 10]) + if not extrapolate: + with pytest.raises(ValueError): + BSpline.design_matrix(x, t, k, extrapolate) + else: + xp_assert_close( + bspline(x), + BSpline.design_matrix(x, t, k, extrapolate).toarray() + ) + + def test_design_matrix_x_shapes(self): + # test for different `x` shapes + rng = np.random.RandomState(1234) + n = 10 + k = 3 + x = np.sort(rng.random_sample(n) * 40 - 20) + y = rng.random_sample(n) * 40 - 20 + + bspl = make_interp_spline(x, y, k=k) + for i in range(1, 4): + xc = x[:i] + yc = y[:i] + des_matr_csr = BSpline.design_matrix(xc, + bspl.t, + k).toarray() + xp_assert_close(des_matr_csr @ bspl.c, yc, atol=1e-14) + + def test_design_matrix_t_shapes(self): + # test for minimal possible `t` shape + t = [1., 1., 1., 2., 3., 4., 4., 4.] + des_matr = BSpline.design_matrix(2., t, 3).toarray() + xp_assert_close(des_matr, + [[0.25, 0.58333333, 0.16666667, 0.]], + atol=1e-14) + + def test_design_matrix_asserts(self): + rng = np.random.RandomState(1234) + n = 10 + k = 3 + x = np.sort(rng.random_sample(n) * 40 - 20) + y = rng.random_sample(n) * 40 - 20 + bspl = make_interp_spline(x, y, k=k) + # invalid vector of knots (should be a 1D non-descending array) + # here the actual vector of knots is reversed, so it is invalid + with assert_raises(ValueError): + BSpline.design_matrix(x, bspl.t[::-1], k) + k = 2 + t = [0., 1., 2., 3., 4., 5.] + x = [1., 2., 3., 4.] + # out of bounds + with assert_raises(ValueError): + BSpline.design_matrix(x, t, k) + + @pytest.mark.parametrize('bc_type', ['natural', 'clamped', + 'periodic', 'not-a-knot']) + def test_from_power_basis(self, bc_type): + rng = np.random.RandomState(1234) + x = np.sort(rng.random(20)) + y = rng.random(20) + if bc_type == 'periodic': + y[-1] = y[0] + cb = CubicSpline(x, y, bc_type=bc_type) + bspl = BSpline.from_power_basis(cb, bc_type=bc_type) + xx = np.linspace(0, 1, 20) + xp_assert_close(cb(xx), bspl(xx), atol=1e-15) + bspl_new = make_interp_spline(x, y, bc_type=bc_type) + xp_assert_close(bspl.c, bspl_new.c, atol=1e-15) + + @pytest.mark.parametrize('bc_type', ['natural', 'clamped', + 'periodic', 'not-a-knot']) + def test_from_power_basis_complex(self, bc_type): + rng = np.random.RandomState(1234) + x = np.sort(rng.random(20)) + y = rng.random(20) + rng.random(20) * 1j + if bc_type == 'periodic': + y[-1] = y[0] + cb = CubicSpline(x, y, bc_type=bc_type) + bspl = BSpline.from_power_basis(cb, bc_type=bc_type) + bspl_new_real = make_interp_spline(x, y.real, bc_type=bc_type) + bspl_new_imag = make_interp_spline(x, y.imag, bc_type=bc_type) + xp_assert_close(bspl.c, bspl_new_real.c + 1j * bspl_new_imag.c, atol=1e-15) + + def test_from_power_basis_exmp(self): + ''' + For x = [0, 1, 2, 3, 4] and y = [1, 1, 1, 1, 1] + the coefficients of Cubic Spline in the power basis: + + $[[0, 0, 0, 0, 0],\\$ + $[0, 0, 0, 0, 0],\\$ + $[0, 0, 0, 0, 0],\\$ + $[1, 1, 1, 1, 1]]$ + + It could be shown explicitly that coefficients of the interpolating + function in B-spline basis are c = [1, 1, 1, 1, 1, 1, 1] + ''' + x = np.array([0, 1, 2, 3, 4]) + y = np.array([1, 1, 1, 1, 1]) + bspl = BSpline.from_power_basis(CubicSpline(x, y, bc_type='natural'), + bc_type='natural') + xp_assert_close(bspl.c, [1.0, 1, 1, 1, 1, 1, 1], atol=1e-15) + + def test_read_only(self): + # BSpline must work on read-only knots and coefficients. + t = np.array([0, 1]) + c = np.array([3.0]) + t.setflags(write=False) + c.setflags(write=False) + + xx = np.linspace(0, 1, 10) + xx.setflags(write=False) + + b = BSpline(t=t, c=c, k=0) + xp_assert_close(b(xx), np.ones_like(xx) * 3.0) + + @pytest.mark.thread_unsafe + def test_concurrency(self): + # Check that no segfaults appear with concurrent access to BSpline + b = _make_random_spline() + + def worker_fn(_, b): + t, _, k = b.tck + xx = np.linspace(t[k], t[-k-1], 10000) + b(xx) + + _run_concurrent_barrier(10, worker_fn, b) + + + def test_memmap(self, tmpdir): + # Make sure that memmaps can be used as t and c atrributes after the + # spline has been constructed. This is similar to what happens in a + # scikit-learn context, where joblib can create read-only memmap to + # share objects between workers. For more details, see + # https://github.com/scipy/scipy/issues/22143 + b = _make_random_spline() + xx = np.linspace(0, 1, 10) + + expected = b(xx) + + tid = threading.get_native_id() + t_mm = np.memmap(str(tmpdir.join(f't{tid}.dat')), mode='w+', + dtype=b.t.dtype, shape=b.t.shape) + t_mm[:] = b.t + c_mm = np.memmap(str(tmpdir.join(f'c{tid}.dat')), mode='w+', + dtype=b.c.dtype, shape=b.c.shape) + c_mm[:] = b.c + b.t = t_mm + b.c = c_mm + + xp_assert_close(b(xx), expected) + +class TestInsert: + + @pytest.mark.parametrize('xval', [0.0, 1.0, 2.5, 4, 6.5, 7.0]) + def test_insert(self, xval): + # insert a knot, incl edges (0.0, 7.0) and exactly at an existing knot (4.0) + x = np.arange(8) + y = np.sin(x)**3 + spl = make_interp_spline(x, y, k=3) + + spl_1f = insert(xval, spl) # FITPACK + spl_1 = spl.insert_knot(xval) + + xp_assert_close(spl_1.t, spl_1f.t, atol=1e-15) + xp_assert_close(spl_1.c, spl_1f.c[:-spl.k-1], atol=1e-15) + + # knot insertion preserves values, unless multiplicity >= k+1 + xx = x if xval != x[-1] else x[:-1] + xx = np.r_[xx, 0.5*(x[1:] + x[:-1])] + xp_assert_close(spl(xx), spl_1(xx), atol=1e-15) + + # ... repeat with ndim > 1 + y1 = np.cos(x)**3 + spl_y1 = make_interp_spline(x, y1, k=3) + spl_yy = make_interp_spline(x, np.c_[y, y1], k=3) + spl_yy1 = spl_yy.insert_knot(xval) + + xp_assert_close(spl_yy1.t, spl_1.t, atol=1e-15) + xp_assert_close(spl_yy1.c, np.c_[spl.insert_knot(xval).c, + spl_y1.insert_knot(xval).c], atol=1e-15) + + xx = x if xval != x[-1] else x[:-1] + xx = np.r_[xx, 0.5*(x[1:] + x[:-1])] + xp_assert_close(spl_yy(xx), spl_yy1(xx), atol=1e-15) + + + @pytest.mark.parametrize( + 'xval, m', [(0.0, 2), (1.0, 3), (1.5, 5), (4, 2), (7.0, 2)] + ) + def test_insert_multi(self, xval, m): + x = np.arange(8) + y = np.sin(x)**3 + spl = make_interp_spline(x, y, k=3) + + spl_1f = insert(xval, spl, m=m) + spl_1 = spl.insert_knot(xval, m) + + xp_assert_close(spl_1.t, spl_1f.t, atol=1e-15) + xp_assert_close(spl_1.c, spl_1f.c[:-spl.k-1], atol=1e-15) + + xx = x if xval != x[-1] else x[:-1] + xx = np.r_[xx, 0.5*(x[1:] + x[:-1])] + xp_assert_close(spl(xx), spl_1(xx), atol=1e-15) + + def test_insert_random(self): + rng = np.random.default_rng(12345) + n, k = 11, 3 + + t = np.sort(rng.uniform(size=n+k+1)) + c = rng.uniform(size=(n, 3, 2)) + spl = BSpline(t, c, k) + + xv = rng.uniform(low=t[k+1], high=t[-k-1]) + spl_1 = spl.insert_knot(xv) + + xx = rng.uniform(low=t[k+1], high=t[-k-1], size=33) + xp_assert_close(spl(xx), spl_1(xx), atol=1e-15) + + @pytest.mark.parametrize('xv', [0, 0.1, 2.0, 4.0, 4.5, # l.h. edge + 5.5, 6.0, 6.1, 7.0] # r.h. edge + ) + def test_insert_periodic(self, xv): + x = np.arange(8) + y = np.sin(x)**3 + tck = splrep(x, y, k=3) + spl = BSpline(*tck, extrapolate="periodic") + + spl_1 = spl.insert_knot(xv) + tf, cf, k = insert(xv, spl.tck, per=True) + + xp_assert_close(spl_1.t, tf, atol=1e-15) + xp_assert_close(spl_1.c[:-k-1], cf[:-k-1], atol=1e-15) + + xx = np.random.default_rng(1234).uniform(low=0, high=7, size=41) + xp_assert_close(spl_1(xx), splev(xx, (tf, cf, k)), atol=1e-15) + + @pytest.mark.parametrize('extrapolate', [None, 'periodic']) + def test_complex(self, extrapolate): + x = np.arange(8)*2*np.pi + y_re, y_im = np.sin(x), np.cos(x) + + spl = make_interp_spline(x, y_re + 1j*y_im, k=3) + spl.extrapolate = extrapolate + + spl_re = make_interp_spline(x, y_re, k=3) + spl_re.extrapolate = extrapolate + + spl_im = make_interp_spline(x, y_im, k=3) + spl_im.extrapolate = extrapolate + + xv = 3.5 + spl_1 = spl.insert_knot(xv) + spl_1re = spl_re.insert_knot(xv) + spl_1im = spl_im.insert_knot(xv) + + xp_assert_close(spl_1.t, spl_1re.t, atol=1e-15) + xp_assert_close(spl_1.t, spl_1im.t, atol=1e-15) + xp_assert_close(spl_1.c, spl_1re.c + 1j*spl_1im.c, atol=1e-15) + + def test_insert_periodic_too_few_internal_knots(self): + # both FITPACK and spl.insert_knot raise when there's not enough + # internal knots to make a periodic extension. + # Below the internal knots are 2, 3, , 4, 5 + # ^ + # 2, 3, 3.5, 4, 5 + # so two knots from each side from the new one, while need at least + # from either left or right. + xv = 3.5 + k = 3 + t = np.array([0]*(k+1) + [2, 3, 4, 5] + [7]*(k+1)) + c = np.ones(len(t) - k - 1) + spl = BSpline(t, c, k, extrapolate="periodic") + + with assert_raises(ValueError): + insert(xv, (t, c, k), per=True) + + with assert_raises(ValueError): + spl.insert_knot(xv) + + def test_insert_no_extrap(self): + k = 3 + t = np.array([0]*(k+1) + [2, 3, 4, 5] + [7]*(k+1)) + c = np.ones(len(t) - k - 1) + spl = BSpline(t, c, k) + + with assert_raises(ValueError): + spl.insert_knot(-1) + + with assert_raises(ValueError): + spl.insert_knot(8) + + with assert_raises(ValueError): + spl.insert_knot(3, m=0) + + +def test_knots_multiplicity(): + # Take a spline w/ random coefficients, throw in knots of varying + # multiplicity. + + def check_splev(b, j, der=0, atol=1e-14, rtol=1e-14): + # check evaluations against FITPACK, incl extrapolations + t, c, k = b.tck + x = np.unique(t) + x = np.r_[t[0]-0.1, 0.5*(x[1:] + x[:1]), t[-1]+0.1] + xp_assert_close(splev(x, (t, c, k), der), b(x, der), + atol=atol, rtol=rtol, err_msg=f'der = {der} k = {b.k}') + + # test loop itself + # [the index `j` is for interpreting the traceback in case of a failure] + for k in [1, 2, 3, 4, 5]: + b = _make_random_spline(k=k) + for j, b1 in enumerate(_make_multiples(b)): + check_splev(b1, j) + for der in range(1, k+1): + check_splev(b1, j, der, 1e-12, 1e-12) + + +### stolen from @pv, verbatim +def _naive_B(x, k, i, t): + """ + Naive way to compute B-spline basis functions. Useful only for testing! + computes B(x; t[i],..., t[i+k+1]) + """ + if k == 0: + return 1.0 if t[i] <= x < t[i+1] else 0.0 + if t[i+k] == t[i]: + c1 = 0.0 + else: + c1 = (x - t[i])/(t[i+k] - t[i]) * _naive_B(x, k-1, i, t) + if t[i+k+1] == t[i+1]: + c2 = 0.0 + else: + c2 = (t[i+k+1] - x)/(t[i+k+1] - t[i+1]) * _naive_B(x, k-1, i+1, t) + return (c1 + c2) + + +### stolen from @pv, verbatim +def _naive_eval(x, t, c, k): + """ + Naive B-spline evaluation. Useful only for testing! + """ + if x == t[k]: + i = k + else: + i = np.searchsorted(t, x) - 1 + assert t[i] <= x <= t[i+1] + assert i >= k and i < len(t) - k + return sum(c[i-j] * _naive_B(x, k, i-j, t) for j in range(0, k+1)) + + +def _naive_eval_2(x, t, c, k): + """Naive B-spline evaluation, another way.""" + n = len(t) - (k+1) + assert n >= k+1 + assert len(c) >= n + assert t[k] <= x <= t[n] + return sum(c[i] * _naive_B(x, k, i, t) for i in range(n)) + + +def _sum_basis_elements(x, t, c, k): + n = len(t) - (k+1) + assert n >= k+1 + assert len(c) >= n + s = 0. + for i in range(n): + b = BSpline.basis_element(t[i:i+k+2], extrapolate=False)(x) + s += c[i] * np.nan_to_num(b) # zero out out-of-bounds elements + return s + + +def B_012(x): + """ A linear B-spline function B(x | 0, 1, 2).""" + x = np.atleast_1d(x) + return np.piecewise(x, [(x < 0) | (x > 2), + (x >= 0) & (x < 1), + (x >= 1) & (x <= 2)], + [lambda x: 0., lambda x: x, lambda x: 2.-x]) + + +def B_0123(x, der=0): + """A quadratic B-spline function B(x | 0, 1, 2, 3).""" + x = np.atleast_1d(x) + conds = [x < 1, (x > 1) & (x < 2), x > 2] + if der == 0: + funcs = [lambda x: x*x/2., + lambda x: 3./4 - (x-3./2)**2, + lambda x: (3.-x)**2 / 2] + elif der == 2: + funcs = [lambda x: 1., + lambda x: -2., + lambda x: 1.] + else: + raise ValueError(f'never be here: der={der}') + pieces = np.piecewise(x, conds, funcs) + return pieces + + +def _make_random_spline(n=35, k=3): + rng = np.random.RandomState(123) + t = np.sort(rng.random(n+k+1)) + c = rng.random(n) + return BSpline.construct_fast(t, c, k) + + +def _make_multiples(b): + """Increase knot multiplicity.""" + c, k = b.c, b.k + + t1 = b.t.copy() + t1[17:19] = t1[17] + t1[22] = t1[21] + yield BSpline(t1, c, k) + + t1 = b.t.copy() + t1[:k+1] = t1[0] + yield BSpline(t1, c, k) + + t1 = b.t.copy() + t1[-k-1:] = t1[-1] + yield BSpline(t1, c, k) + + +class TestInterop: + # + # Test that FITPACK-based spl* functions can deal with BSpline objects + # + def setup_method(self): + xx = np.linspace(0, 4.*np.pi, 41) + yy = np.cos(xx) + b = make_interp_spline(xx, yy) + self.tck = (b.t, b.c, b.k) + self.xx, self.yy, self.b = xx, yy, b + + self.xnew = np.linspace(0, 4.*np.pi, 21) + + c2 = np.c_[b.c, b.c, b.c] + self.c2 = np.dstack((c2, c2)) + self.b2 = BSpline(b.t, self.c2, b.k) + + def test_splev(self): + xnew, b, b2 = self.xnew, self.b, self.b2 + + # check that splev works with 1-D array of coefficients + # for array and scalar `x` + xp_assert_close(splev(xnew, b), + b(xnew), atol=1e-15, rtol=1e-15) + xp_assert_close(splev(xnew, b.tck), + b(xnew), atol=1e-15, rtol=1e-15) + xp_assert_close(np.asarray([splev(x, b) for x in xnew]), + b(xnew), atol=1e-15, rtol=1e-15) + + # With N-D coefficients, there's a quirck: + # splev(x, BSpline) is equivalent to BSpline(x) + with assert_raises(ValueError, match="Calling splev.. with BSpline"): + splev(xnew, b2) + + # However, splev(x, BSpline.tck) needs some transposes. This is because + # BSpline interpolates along the first axis, while the legacy FITPACK + # wrapper does list(map(...)) which effectively interpolates along the + # last axis. Like so: + sh = tuple(range(1, b2.c.ndim)) + (0,) # sh = (1, 2, 0) + cc = b2.c.transpose(sh) + tck = (b2.t, cc, b2.k) + xp_assert_close(np.asarray(splev(xnew, tck)), + b2(xnew).transpose(sh), atol=1e-15, rtol=1e-15) + + def test_splrep(self): + x, y = self.xx, self.yy + # test that "new" splrep is equivalent to _impl.splrep + tck = splrep(x, y) + t, c, k = _impl.splrep(x, y) + xp_assert_close(tck[0], t, atol=1e-15) + xp_assert_close(tck[1], c, atol=1e-15) + assert tck[2] == k + + # also cover the `full_output=True` branch + tck_f, _, _, _ = splrep(x, y, full_output=True) + xp_assert_close(tck_f[0], t, atol=1e-15) + xp_assert_close(tck_f[1], c, atol=1e-15) + assert tck_f[2] == k + + # test that the result of splrep roundtrips with splev: + # evaluate the spline on the original `x` points + yy = splev(x, tck) + xp_assert_close(y, yy, atol=1e-15) + + # ... and also it roundtrips if wrapped in a BSpline + b = BSpline(*tck) + xp_assert_close(y, b(x), atol=1e-15) + + def test_splrep_errors(self): + # test that both "old" and "new" splrep raise for an N-D ``y`` array + # with n > 1 + x, y = self.xx, self.yy + y2 = np.c_[y, y] + with assert_raises(ValueError): + splrep(x, y2) + with assert_raises(ValueError): + _impl.splrep(x, y2) + + # input below minimum size + with assert_raises(TypeError, match="m > k must hold"): + splrep(x[:3], y[:3]) + with assert_raises(TypeError, match="m > k must hold"): + _impl.splrep(x[:3], y[:3]) + + def test_splprep(self): + x = np.arange(15, dtype=np.float64).reshape((3, 5)) + b, u = splprep(x) + tck, u1 = _impl.splprep(x) + + # test the roundtrip with splev for both "old" and "new" output + xp_assert_close(u, u1, atol=1e-15) + xp_assert_close(np.asarray(splev(u, b)), x, atol=1e-15) + xp_assert_close(np.asarray(splev(u, tck)), x, atol=1e-15) + + # cover the ``full_output=True`` branch + (b_f, u_f), _, _, _ = splprep(x, s=0, full_output=True) + xp_assert_close(u, u_f, atol=1e-15) + xp_assert_close(np.asarray(splev(u_f, b_f)), x, atol=1e-15) + + def test_splprep_errors(self): + # test that both "old" and "new" code paths raise for x.ndim > 2 + x = np.arange(3*4*5).reshape((3, 4, 5)) + with assert_raises(ValueError, match="too many values to unpack"): + splprep(x) + with assert_raises(ValueError, match="too many values to unpack"): + _impl.splprep(x) + + # input below minimum size + x = np.linspace(0, 40, num=3) + with assert_raises(TypeError, match="m > k must hold"): + splprep([x]) + with assert_raises(TypeError, match="m > k must hold"): + _impl.splprep([x]) + + # automatically calculated parameters are non-increasing + # see gh-7589 + x = [-50.49072266, -50.49072266, -54.49072266, -54.49072266] + with assert_raises(ValueError, match="Invalid inputs"): + splprep([x]) + with assert_raises(ValueError, match="Invalid inputs"): + _impl.splprep([x]) + + # given non-increasing parameter values u + x = [1, 3, 2, 4] + u = [0, 0.3, 0.2, 1] + with assert_raises(ValueError, match="Invalid inputs"): + splprep(*[[x], None, u]) + + def test_sproot(self): + b, b2 = self.b, self.b2 + roots = np.array([0.5, 1.5, 2.5, 3.5])*np.pi + # sproot accepts a BSpline obj w/ 1-D coef array + xp_assert_close(sproot(b), roots, atol=1e-7, rtol=1e-7) + xp_assert_close(sproot((b.t, b.c, b.k)), roots, atol=1e-7, rtol=1e-7) + + # ... and deals with trailing dimensions if coef array is N-D + with assert_raises(ValueError, match="Calling sproot.. with BSpline"): + sproot(b2, mest=50) + + # and legacy behavior is preserved for a tck tuple w/ N-D coef + c2r = b2.c.transpose(1, 2, 0) + rr = np.asarray(sproot((b2.t, c2r, b2.k), mest=50)) + assert rr.shape == (3, 2, 4) + xp_assert_close(rr - roots, np.zeros_like(rr), atol=1e-12) + + def test_splint(self): + # test that splint accepts BSpline objects + b, b2 = self.b, self.b2 + + xp_assert_close(splint(0, 1, b), + splint(0, 1, b.tck), atol=1e-14, check_0d=False) + xp_assert_close(splint(0, 1, b), + b.integrate(0, 1), atol=1e-14, check_0d=False) + + # ... and deals with N-D arrays of coefficients + with assert_raises(ValueError, match="Calling splint.. with BSpline"): + splint(0, 1, b2) + + # and the legacy behavior is preserved for a tck tuple w/ N-D coef + c2r = b2.c.transpose(1, 2, 0) + integr = np.asarray(splint(0, 1, (b2.t, c2r, b2.k))) + assert integr.shape == (3, 2) + xp_assert_close(integr, + splint(0, 1, b), atol=1e-14, check_shape=False) + + def test_splder(self): + for b in [self.b, self.b2]: + # pad the c array (FITPACK convention) + ct = len(b.t) - len(b.c) + b_c = b.c.copy() + if ct > 0: + b_c = np.r_[b_c, np.zeros((ct,) + b_c.shape[1:])] + + for n in [1, 2, 3]: + bd = splder(b) + tck_d = _impl.splder((b.t.copy(), b_c, b.k)) + xp_assert_close(bd.t, tck_d[0], atol=1e-15) + xp_assert_close(bd.c, tck_d[1], atol=1e-15) + assert bd.k == tck_d[2] + assert isinstance(bd, BSpline) + assert isinstance(tck_d, tuple) # back-compat: tck in and out + + def test_splantider(self): + for b in [self.b, self.b2]: + # pad the c array (FITPACK convention) + ct = len(b.t) - len(b.c) + b_c = b.c.copy() + if ct > 0: + b_c = np.r_[b_c, np.zeros((ct,) + b_c.shape[1:])] + + for n in [1, 2, 3]: + bd = splantider(b) + tck_d = _impl.splantider((b.t.copy(), b_c, b.k)) + xp_assert_close(bd.t, tck_d[0], atol=1e-15) + xp_assert_close(bd.c, tck_d[1], atol=1e-15) + assert bd.k == tck_d[2] + assert isinstance(bd, BSpline) + assert isinstance(tck_d, tuple) # back-compat: tck in and out + + def test_insert(self): + b, b2, xx = self.b, self.b2, self.xx + + j = b.t.size // 2 + tn = 0.5*(b.t[j] + b.t[j+1]) + + bn, tck_n = insert(tn, b), insert(tn, (b.t, b.c, b.k)) + xp_assert_close(splev(xx, bn), + splev(xx, tck_n), atol=1e-15) + assert isinstance(bn, BSpline) + assert isinstance(tck_n, tuple) # back-compat: tck in, tck out + + # for N-D array of coefficients, BSpline.c needs to be transposed + # after that, the results are equivalent. + sh = tuple(range(b2.c.ndim)) + c_ = b2.c.transpose(sh[1:] + (0,)) + tck_n2 = insert(tn, (b2.t, c_, b2.k)) + + bn2 = insert(tn, b2) + + # need a transpose for comparing the results, cf test_splev + xp_assert_close(np.asarray(splev(xx, tck_n2)).transpose(2, 0, 1), + bn2(xx), atol=1e-15) + assert isinstance(bn2, BSpline) + assert isinstance(tck_n2, tuple) # back-compat: tck in, tck out + + +class TestInterp: + # + # Test basic ways of constructing interpolating splines. + # + xx = np.linspace(0., 2.*np.pi) + yy = np.sin(xx) + + def test_non_int_order(self): + with assert_raises(TypeError): + make_interp_spline(self.xx, self.yy, k=2.5) + + def test_order_0(self): + b = make_interp_spline(self.xx, self.yy, k=0) + xp_assert_close(b(self.xx), self.yy, atol=1e-14, rtol=1e-14) + b = make_interp_spline(self.xx, self.yy, k=0, axis=-1) + xp_assert_close(b(self.xx), self.yy, atol=1e-14, rtol=1e-14) + + def test_linear(self): + b = make_interp_spline(self.xx, self.yy, k=1) + xp_assert_close(b(self.xx), self.yy, atol=1e-14, rtol=1e-14) + b = make_interp_spline(self.xx, self.yy, k=1, axis=-1) + xp_assert_close(b(self.xx), self.yy, atol=1e-14, rtol=1e-14) + + @pytest.mark.parametrize('k', [0, 1, 2, 3]) + def test_incompatible_x_y(self, k): + x = [0, 1, 2, 3, 4, 5] + y = [0, 1, 2, 3, 4, 5, 6, 7] + with assert_raises(ValueError, match="Shapes of x"): + make_interp_spline(x, y, k=k) + + @pytest.mark.parametrize('k', [0, 1, 2, 3]) + def test_broken_x(self, k): + x = [0, 1, 1, 2, 3, 4] # duplicates + y = [0, 1, 2, 3, 4, 5] + with assert_raises(ValueError, match="x to not have duplicates"): + make_interp_spline(x, y, k=k) + + x = [0, 2, 1, 3, 4, 5] # unsorted + with assert_raises(ValueError, match="Expect x to be a 1D strictly"): + make_interp_spline(x, y, k=k) + + x = [0, 1, 2, 3, 4, 5] + x = np.asarray(x).reshape((1, -1)) # 1D + with assert_raises(ValueError, match="Expect x to be a 1D strictly"): + make_interp_spline(x, y, k=k) + + def test_not_a_knot(self): + for k in [2, 3, 4, 5, 6, 7]: + b = make_interp_spline(self.xx, self.yy, k) + xp_assert_close(b(self.xx), self.yy, atol=1e-14, rtol=1e-14) + + def test_periodic(self): + # k = 5 here for more derivatives + b = make_interp_spline(self.xx, self.yy, k=5, bc_type='periodic') + xp_assert_close(b(self.xx), self.yy, atol=1e-14, rtol=1e-14) + # in periodic case it is expected equality of k-1 first + # derivatives at the boundaries + for i in range(1, 5): + xp_assert_close(b(self.xx[0], nu=i), b(self.xx[-1], nu=i), atol=1e-11) + # tests for axis=-1 + b = make_interp_spline(self.xx, self.yy, k=5, bc_type='periodic', axis=-1) + xp_assert_close(b(self.xx), self.yy, atol=1e-14, rtol=1e-14) + for i in range(1, 5): + xp_assert_close(b(self.xx[0], nu=i), b(self.xx[-1], nu=i), atol=1e-11) + + @pytest.mark.parametrize('k', [2, 3, 4, 5, 6, 7]) + def test_periodic_random(self, k): + # tests for both cases (k > n and k <= n) + n = 5 + rng = np.random.RandomState(1234) + x = np.sort(rng.random_sample(n) * 10) + y = rng.random_sample(n) * 100 + y[0] = y[-1] + b = make_interp_spline(x, y, k=k, bc_type='periodic') + xp_assert_close(b(x), y, atol=1e-14) + + def test_periodic_axis(self): + n = self.xx.shape[0] + rng = np.random.RandomState(1234) + x = rng.random_sample(n) * 2 * np.pi + x = np.sort(x) + x[0] = 0. + x[-1] = 2 * np.pi + y = np.zeros((2, n)) + y[0] = np.sin(x) + y[1] = np.cos(x) + b = make_interp_spline(x, y, k=5, bc_type='periodic', axis=1) + for i in range(n): + xp_assert_close(b(x[i]), y[:, i], atol=1e-14) + xp_assert_close(b(x[0]), b(x[-1]), atol=1e-14) + + def test_periodic_points_exception(self): + # first and last points should match when periodic case expected + rng = np.random.RandomState(1234) + k = 5 + n = 8 + x = np.sort(rng.random_sample(n)) + y = rng.random_sample(n) + y[0] = y[-1] - 1 # to be sure that they are not equal + with assert_raises(ValueError): + make_interp_spline(x, y, k=k, bc_type='periodic') + + def test_periodic_knots_exception(self): + # `periodic` case does not work with passed vector of knots + rng = np.random.RandomState(1234) + k = 3 + n = 7 + x = np.sort(rng.random_sample(n)) + y = rng.random_sample(n) + t = np.zeros(n + 2 * k) + with assert_raises(ValueError): + make_interp_spline(x, y, k, t, 'periodic') + + @pytest.mark.parametrize('k', [2, 3, 4, 5]) + def test_periodic_splev(self, k): + # comparison values of periodic b-spline with splev + b = make_interp_spline(self.xx, self.yy, k=k, bc_type='periodic') + tck = splrep(self.xx, self.yy, per=True, k=k) + spl = splev(self.xx, tck) + xp_assert_close(spl, b(self.xx), atol=1e-14) + + # comparison derivatives of periodic b-spline with splev + for i in range(1, k): + spl = splev(self.xx, tck, der=i) + xp_assert_close(spl, b(self.xx, nu=i), atol=1e-10) + + def test_periodic_cubic(self): + # comparison values of cubic periodic b-spline with CubicSpline + b = make_interp_spline(self.xx, self.yy, k=3, bc_type='periodic') + cub = CubicSpline(self.xx, self.yy, bc_type='periodic') + xp_assert_close(b(self.xx), cub(self.xx), atol=1e-14) + + # edge case: Cubic interpolation on 3 points + rng = np.random.RandomState(1234) + n = 3 + x = np.sort(rng.random_sample(n) * 10) + y = rng.random_sample(n) * 100 + y[0] = y[-1] + b = make_interp_spline(x, y, k=3, bc_type='periodic') + cub = CubicSpline(x, y, bc_type='periodic') + xp_assert_close(b(x), cub(x), atol=1e-14) + + def test_periodic_full_matrix(self): + # comparison values of cubic periodic b-spline with + # solution of the system with full matrix + k = 3 + b = make_interp_spline(self.xx, self.yy, k=k, bc_type='periodic') + t = _periodic_knots(self.xx, k) + c = _make_interp_per_full_matr(self.xx, self.yy, t, k) + b1 = np.vectorize(lambda x: _naive_eval(x, t, c, k)) + xp_assert_close(b(self.xx), b1(self.xx), atol=1e-14) + + def test_quadratic_deriv(self): + der = [(1, 8.)] # order, value: f'(x) = 8. + + # derivative at right-hand edge + b = make_interp_spline(self.xx, self.yy, k=2, bc_type=(None, der)) + xp_assert_close(b(self.xx), self.yy, atol=1e-14, rtol=1e-14) + xp_assert_close( + b(self.xx[-1], 1), der[0][1], atol=1e-14, rtol=1e-14, check_0d=False + ) + + # derivative at left-hand edge + b = make_interp_spline(self.xx, self.yy, k=2, bc_type=(der, None)) + xp_assert_close(b(self.xx), self.yy, atol=1e-14, rtol=1e-14) + xp_assert_close( + b(self.xx[0], 1), der[0][1], atol=1e-14, rtol=1e-14, check_0d=False + ) + + def test_cubic_deriv(self): + k = 3 + + # first derivatives at left & right edges: + der_l, der_r = [(1, 3.)], [(1, 4.)] + b = make_interp_spline(self.xx, self.yy, k, bc_type=(der_l, der_r)) + xp_assert_close(b(self.xx), self.yy, atol=1e-14, rtol=1e-14) + xp_assert_close(np.asarray([b(self.xx[0], 1), b(self.xx[-1], 1)]), + np.asarray([der_l[0][1], der_r[0][1]]), atol=1e-14, rtol=1e-14) + + # 'natural' cubic spline, zero out 2nd derivatives at the boundaries + der_l, der_r = [(2, 0)], [(2, 0)] + b = make_interp_spline(self.xx, self.yy, k, bc_type=(der_l, der_r)) + xp_assert_close(b(self.xx), self.yy, atol=1e-14, rtol=1e-14) + + def test_quintic_derivs(self): + k, n = 5, 7 + x = np.arange(n).astype(np.float64) + y = np.sin(x) + der_l = [(1, -12.), (2, 1)] + der_r = [(1, 8.), (2, 3.)] + b = make_interp_spline(x, y, k=k, bc_type=(der_l, der_r)) + xp_assert_close(b(x), y, atol=1e-14, rtol=1e-14) + xp_assert_close(np.asarray([b(x[0], 1), b(x[0], 2)]), + np.asarray([val for (nu, val) in der_l])) + xp_assert_close(np.asarray([b(x[-1], 1), b(x[-1], 2)]), + np.asarray([val for (nu, val) in der_r])) + + @pytest.mark.xfail(reason='unstable') + def test_cubic_deriv_unstable(self): + # 1st and 2nd derivative at x[0], no derivative information at x[-1] + # The problem is not that it fails [who would use this anyway], + # the problem is that it fails *silently*, and I've no idea + # how to detect this sort of instability. + # In this particular case: it's OK for len(t) < 20, goes haywire + # at larger `len(t)`. + k = 3 + t = _augknt(self.xx, k) + + der_l = [(1, 3.), (2, 4.)] + b = make_interp_spline(self.xx, self.yy, k, t, bc_type=(der_l, None)) + xp_assert_close(b(self.xx), self.yy, atol=1e-14, rtol=1e-14) + + def test_knots_not_data_sites(self): + # Knots need not coincide with the data sites. + # use a quadratic spline, knots are at data averages, + # two additional constraints are zero 2nd derivatives at edges + k = 2 + t = np.r_[(self.xx[0],)*(k+1), + (self.xx[1:] + self.xx[:-1]) / 2., + (self.xx[-1],)*(k+1)] + b = make_interp_spline(self.xx, self.yy, k, t, + bc_type=([(2, 0)], [(2, 0)])) + + xp_assert_close(b(self.xx), self.yy, atol=1e-14, rtol=1e-14) + xp_assert_close(b(self.xx[0], 2), np.asarray(0.0), atol=1e-14) + xp_assert_close(b(self.xx[-1], 2), np.asarray(0.0), atol=1e-14) + + def test_minimum_points_and_deriv(self): + # interpolation of f(x) = x**3 between 0 and 1. f'(x) = 3 * xx**2 and + # f'(0) = 0, f'(1) = 3. + k = 3 + x = [0., 1.] + y = [0., 1.] + b = make_interp_spline(x, y, k, bc_type=([(1, 0.)], [(1, 3.)])) + + xx = np.linspace(0., 1.) + yy = xx**3 + xp_assert_close(b(xx), yy, atol=1e-14, rtol=1e-14) + + def test_deriv_spec(self): + # If one of the derivatives is omitted, the spline definition is + # incomplete. + x = y = [1.0, 2, 3, 4, 5, 6] + + with assert_raises(ValueError): + make_interp_spline(x, y, bc_type=([(1, 0.)], None)) + + with assert_raises(ValueError): + make_interp_spline(x, y, bc_type=(1, 0.)) + + with assert_raises(ValueError): + make_interp_spline(x, y, bc_type=[(1, 0.)]) + + with assert_raises(ValueError): + make_interp_spline(x, y, bc_type=42) + + # CubicSpline expects`bc_type=(left_pair, right_pair)`, while + # here we expect `bc_type=(iterable, iterable)`. + l, r = (1, 0.0), (1, 0.0) + with assert_raises(ValueError): + make_interp_spline(x, y, bc_type=(l, r)) + + def test_deriv_order_too_large(self): + x = np.arange(7) + y = x**2 + l, r = [(6, 0)], [(1, 0)] # 6th derivative = 0 at x[0] for k=3 + with assert_raises(ValueError, match="Bad boundary conditions at 0."): + # cannot fix 6th derivative at x[0]: does not segfault + make_interp_spline(x, y, bc_type=(l, r)) + + l, r = [(1, 0)], [(-6, 0)] # derivative order < 0 at x[-1] + with assert_raises(ValueError, match="Bad boundary conditions at 6."): + # does not segfault + make_interp_spline(x, y, bc_type=(l, r)) + + def test_complex(self): + k = 3 + xx = self.xx + yy = self.yy + 1.j*self.yy + + # first derivatives at left & right edges: + der_l, der_r = [(1, 3.j)], [(1, 4.+2.j)] + b = make_interp_spline(xx, yy, k, bc_type=(der_l, der_r)) + xp_assert_close(b(xx), yy, atol=1e-14, rtol=1e-14) + xp_assert_close( + b(xx[0], 1), der_l[0][1], atol=1e-14, rtol=1e-14, check_0d=False + ) + xp_assert_close( + b(xx[-1], 1), der_r[0][1], atol=1e-14, rtol=1e-14, check_0d=False + ) + + # also test zero and first order + for k in (0, 1): + b = make_interp_spline(xx, yy, k=k) + xp_assert_close(b(xx), yy, atol=1e-14, rtol=1e-14) + + def test_int_xy(self): + x = np.arange(10).astype(int) + y = np.arange(10).astype(int) + + # Cython chokes on "buffer type mismatch" (construction) or + # "no matching signature found" (evaluation) + for k in (0, 1, 2, 3): + b = make_interp_spline(x, y, k=k) + b(x) + + def test_sliced_input(self): + # Cython code chokes on non C contiguous arrays + xx = np.linspace(-1, 1, 100) + + x = xx[::5] + y = xx[::5] + + for k in (0, 1, 2, 3): + make_interp_spline(x, y, k=k) + + def test_check_finite(self): + # check_finite defaults to True; nans and such trigger a ValueError + x = np.arange(10).astype(float) + y = x**2 + + for z in [np.nan, np.inf, -np.inf]: + y[-1] = z + assert_raises(ValueError, make_interp_spline, x, y) + + @pytest.mark.parametrize('k', [1, 2, 3, 5]) + def test_list_input(self, k): + # regression test for gh-8714: TypeError for x, y being lists and k=2 + x = list(range(10)) + y = [a**2 for a in x] + make_interp_spline(x, y, k=k) + + def test_multiple_rhs(self): + yy = np.c_[np.sin(self.xx), np.cos(self.xx)] + der_l = [(1, [1., 2.])] + der_r = [(1, [3., 4.])] + + b = make_interp_spline(self.xx, yy, k=3, bc_type=(der_l, der_r)) + xp_assert_close(b(self.xx), yy, atol=1e-14, rtol=1e-14) + xp_assert_close(b(self.xx[0], 1), der_l[0][1], atol=1e-14, rtol=1e-14) + xp_assert_close(b(self.xx[-1], 1), der_r[0][1], atol=1e-14, rtol=1e-14) + + def test_shapes(self): + rng = np.random.RandomState(1234) + k, n = 3, 22 + x = np.sort(rng.random(size=n)) + y = rng.random(size=(n, 5, 6, 7)) + + b = make_interp_spline(x, y, k) + assert b.c.shape == (n, 5, 6, 7) + + # now throw in some derivatives + d_l = [(1, rng.random((5, 6, 7)))] + d_r = [(1, rng.random((5, 6, 7)))] + b = make_interp_spline(x, y, k, bc_type=(d_l, d_r)) + assert b.c.shape == (n + k - 1, 5, 6, 7) + + def test_string_aliases(self): + yy = np.sin(self.xx) + + # a single string is duplicated + b1 = make_interp_spline(self.xx, yy, k=3, bc_type='natural') + b2 = make_interp_spline(self.xx, yy, k=3, bc_type=([(2, 0)], [(2, 0)])) + xp_assert_close(b1.c, b2.c, atol=1e-15) + + # two strings are handled + b1 = make_interp_spline(self.xx, yy, k=3, + bc_type=('natural', 'clamped')) + b2 = make_interp_spline(self.xx, yy, k=3, + bc_type=([(2, 0)], [(1, 0)])) + xp_assert_close(b1.c, b2.c, atol=1e-15) + + # one-sided BCs are OK + b1 = make_interp_spline(self.xx, yy, k=2, bc_type=(None, 'clamped')) + b2 = make_interp_spline(self.xx, yy, k=2, bc_type=(None, [(1, 0.0)])) + xp_assert_close(b1.c, b2.c, atol=1e-15) + + # 'not-a-knot' is equivalent to None + b1 = make_interp_spline(self.xx, yy, k=3, bc_type='not-a-knot') + b2 = make_interp_spline(self.xx, yy, k=3, bc_type=None) + xp_assert_close(b1.c, b2.c, atol=1e-15) + + # unknown strings do not pass + with assert_raises(ValueError): + make_interp_spline(self.xx, yy, k=3, bc_type='typo') + + # string aliases are handled for 2D values + yy = np.c_[np.sin(self.xx), np.cos(self.xx)] + der_l = [(1, [0., 0.])] + der_r = [(2, [0., 0.])] + b2 = make_interp_spline(self.xx, yy, k=3, bc_type=(der_l, der_r)) + b1 = make_interp_spline(self.xx, yy, k=3, + bc_type=('clamped', 'natural')) + xp_assert_close(b1.c, b2.c, atol=1e-15) + + # ... and for N-D values: + rng = np.random.RandomState(1234) + k, n = 3, 22 + x = np.sort(rng.random(size=n)) + y = rng.random(size=(n, 5, 6, 7)) + + # now throw in some derivatives + d_l = [(1, np.zeros((5, 6, 7)))] + d_r = [(1, np.zeros((5, 6, 7)))] + b1 = make_interp_spline(x, y, k, bc_type=(d_l, d_r)) + b2 = make_interp_spline(x, y, k, bc_type='clamped') + xp_assert_close(b1.c, b2.c, atol=1e-15) + + def test_full_matrix(self): + rng = np.random.RandomState(1234) + k, n = 3, 7 + x = np.sort(rng.random(size=n)) + y = rng.random(size=n) + t = _not_a_knot(x, k) + + b = make_interp_spline(x, y, k, t) + cf = make_interp_full_matr(x, y, t, k) + xp_assert_close(b.c, cf, atol=1e-14, rtol=1e-14) + + def test_woodbury(self): + ''' + Random elements in diagonal matrix with blocks in the + left lower and right upper corners checking the + implementation of Woodbury algorithm. + ''' + rng = np.random.RandomState(1234) + n = 201 + for k in range(3, 32, 2): + offset = int((k - 1) / 2) + a = np.diagflat(rng.random((1, n))) + for i in range(1, offset + 1): + a[:-i, i:] += np.diagflat(rng.random((1, n - i))) + a[i:, :-i] += np.diagflat(rng.random((1, n - i))) + ur = rng.random((offset, offset)) + a[:offset, -offset:] = ur + ll = rng.random((offset, offset)) + a[-offset:, :offset] = ll + d = np.zeros((k, n)) + for i, j in enumerate(range(offset, -offset - 1, -1)): + if j < 0: + d[i, :j] = np.diagonal(a, offset=j) + else: + d[i, j:] = np.diagonal(a, offset=j) + b = rng.random(n) + xp_assert_close(_woodbury_algorithm(d, ur, ll, b, k), + np.linalg.solve(a, b), atol=1e-14) + + +def make_interp_full_matr(x, y, t, k): + """Assemble an spline order k with knots t to interpolate + y(x) using full matrices. + Not-a-knot BC only. + + This routine is here for testing only (even though it's functional). + """ + assert x.size == y.size + assert t.size == x.size + k + 1 + n = x.size + + A = np.zeros((n, n), dtype=np.float64) + + for j in range(n): + xval = x[j] + if xval == t[k]: + left = k + else: + left = np.searchsorted(t, xval) - 1 + + # fill a row + bb = _dierckx.evaluate_all_bspl(t, k, xval, left) + A[j, left-k:left+1] = bb + + c = sl.solve(A, y) + return c + + +def make_lsq_full_matrix(x, y, t, k=3): + """Make the least-square spline, full matrices.""" + x, y, t = map(np.asarray, (x, y, t)) + m = x.size + n = t.size - k - 1 + + A = np.zeros((m, n), dtype=np.float64) + + for j in range(m): + xval = x[j] + # find interval + if xval == t[k]: + left = k + else: + left = np.searchsorted(t, xval) - 1 + + # fill a row + bb = _dierckx.evaluate_all_bspl(t, k, xval, left) + A[j, left-k:left+1] = bb + + # have observation matrix, can solve the LSQ problem + B = np.dot(A.T, A) + Y = np.dot(A.T, y) + c = sl.solve(B, Y) + + return c, (A, Y) + + +parametrize_lsq_methods = pytest.mark.parametrize("method", ["norm-eq", "qr"]) + +class TestLSQ: + # + # Test make_lsq_spline + # + rng = np.random.RandomState(1234) + n, k = 13, 3 + x = np.sort(rng.random(n)) + y = rng.random(n) + t = _augknt(np.linspace(x[0], x[-1], 7), k) + + @parametrize_lsq_methods + def test_lstsq(self, method): + # check LSQ construction vs a full matrix version + x, y, t, k = self.x, self.y, self.t, self.k + + c0, AY = make_lsq_full_matrix(x, y, t, k) + b = make_lsq_spline(x, y, t, k, method=method) + + xp_assert_close(b.c, c0) + assert b.c.shape == (t.size - k - 1,) + + # also check against numpy.lstsq + aa, yy = AY + c1, _, _, _ = np.linalg.lstsq(aa, y, rcond=-1) + xp_assert_close(b.c, c1) + + @parametrize_lsq_methods + def test_weights(self, method): + # weights = 1 is same as None + x, y, t, k = self.x, self.y, self.t, self.k + w = np.ones_like(x) + + b = make_lsq_spline(x, y, t, k, method=method) + b_w = make_lsq_spline(x, y, t, k, w=w, method=method) + + xp_assert_close(b.t, b_w.t, atol=1e-14) + xp_assert_close(b.c, b_w.c, atol=1e-14) + assert b.k == b_w.k + + def test_weights_same(self): + # both methods treat weights + x, y, t, k = self.x, self.y, self.t, self.k + w = np.random.default_rng(1234).uniform(size=x.shape[0]) + + b_ne = make_lsq_spline(x, y, t, k, w=w, method="norm-eq") + b_qr = make_lsq_spline(x, y, t, k, w=w, method="qr") + b_no_w = make_lsq_spline(x, y, t, k, method="qr") + + xp_assert_close(b_ne.c, b_qr.c, atol=1e-14) + assert not np.allclose(b_no_w.c, b_qr.c, atol=1e-14) + + @parametrize_lsq_methods + def test_multiple_rhs(self, method): + x, t, k, n = self.x, self.t, self.k, self.n + rng = np.random.RandomState(1234) + y = rng.random(size=(n, 5, 6, 7)) + b = make_lsq_spline(x, y, t, k, method=method) + assert b.c.shape == (t.size-k-1, 5, 6, 7) + + @parametrize_lsq_methods + def test_multiple_rhs_2(self, method): + x, t, k, n = self.x, self.t, self.k, self.n + nrhs = 3 + rng = np.random.RandomState(1234) + y = rng.random(size=(n, nrhs)) + b = make_lsq_spline(x, y, t, k, method=method) + + bb = [make_lsq_spline(x, y[:, i], t, k, method=method) + for i in range(nrhs)] + coefs = np.vstack([bb[i].c for i in range(nrhs)]).T + + xp_assert_close(coefs, b.c, atol=1e-15) + + def test_multiple_rhs_3(self): + x, t, k, n = self.x, self.t, self.k, self.n + nrhs = 3 + y = np.random.random(size=(n, nrhs)) + b_qr = make_lsq_spline(x, y, t, k, method="qr") + b_neq = make_lsq_spline(x, y, t, k, method="norm-eq") + xp_assert_close(b_qr.c, b_neq.c, atol=1e-15) + + @parametrize_lsq_methods + def test_complex(self, method): + # cmplx-valued `y` + x, t, k = self.x, self.t, self.k + yc = self.y * (1. + 2.j) + + b = make_lsq_spline(x, yc, t, k, method=method) + b_re = make_lsq_spline(x, yc.real, t, k, method=method) + b_im = make_lsq_spline(x, yc.imag, t, k, method=method) + + xp_assert_close(b(x), b_re(x) + 1.j*b_im(x), atol=1e-15, rtol=1e-15) + + def test_complex_2(self): + # test complex-valued y with y.ndim > 1 + + x, t, k = self.x, self.t, self.k + yc = self.y * (1. + 2.j) + yc = np.stack((yc, yc), axis=1) + + b = make_lsq_spline(x, yc, t, k) + b_re = make_lsq_spline(x, yc.real, t, k) + b_im = make_lsq_spline(x, yc.imag, t, k) + + xp_assert_close(b(x), b_re(x) + 1.j*b_im(x), atol=1e-15, rtol=1e-15) + + # repeat with num_trailing_dims > 1 : yc.shape[1:] = (2, 2) + yc = np.stack((yc, yc), axis=1) + + b = make_lsq_spline(x, yc, t, k) + b_re = make_lsq_spline(x, yc.real, t, k) + b_im = make_lsq_spline(x, yc.imag, t, k) + + xp_assert_close(b(x), b_re(x) + 1.j*b_im(x), atol=1e-15, rtol=1e-15) + + @parametrize_lsq_methods + def test_int_xy(self, method): + x = np.arange(10).astype(int) + y = np.arange(10).astype(int) + t = _augknt(x, k=1) + # Cython chokes on "buffer type mismatch" + make_lsq_spline(x, y, t, k=1, method=method) + + @parametrize_lsq_methods + def test_f32_xy(self, method): + x = np.arange(10, dtype=np.float32) + y = np.arange(10, dtype=np.float32) + t = _augknt(x, k=1) + spl_f32 = make_lsq_spline(x, y, t, k=1, method=method) + spl_f64 = make_lsq_spline( + x.astype(float), y.astype(float), t.astype(float), k=1, method=method + ) + + x2 = (x[1:] + x[:-1]) / 2.0 + xp_assert_close(spl_f32(x2), spl_f64(x2), atol=1e-15) + + @parametrize_lsq_methods + def test_sliced_input(self, method): + # Cython code chokes on non C contiguous arrays + xx = np.linspace(-1, 1, 100) + + x = xx[::3] + y = xx[::3] + t = _augknt(x, 1) + make_lsq_spline(x, y, t, k=1, method=method) + + @parametrize_lsq_methods + def test_checkfinite(self, method): + # check_finite defaults to True; nans and such trigger a ValueError + x = np.arange(12).astype(float) + y = x**2 + t = _augknt(x, 3) + + for z in [np.nan, np.inf, -np.inf]: + y[-1] = z + assert_raises(ValueError, make_lsq_spline, x, y, t, method=method) + + @parametrize_lsq_methods + def test_read_only(self, method): + # Check that make_lsq_spline works with read only arrays + x, y, t = self.x, self.y, self.t + x.setflags(write=False) + y.setflags(write=False) + t.setflags(write=False) + make_lsq_spline(x=x, y=y, t=t, method=method) + + @pytest.mark.parametrize('k', list(range(1, 7))) + def test_qr_vs_norm_eq(self, k): + # check that QR and normal eq solutions match + x, y = self.x, self.y + t = _augknt(np.linspace(x[0], x[-1], 7), k) + spl_norm_eq = make_lsq_spline(x, y, t, k=k, method='norm-eq') + spl_qr = make_lsq_spline(x, y, t, k=k, method='qr') + + xx = (x[1:] + x[:-1]) / 2.0 + xp_assert_close(spl_norm_eq(xx), spl_qr(xx), atol=1e-15) + + def test_duplicates(self): + # method="qr" can handle duplicated data points + x = np.repeat(self.x, 2) + y = np.repeat(self.y, 2) + spl_1 = make_lsq_spline(self.x, self.y, self.t, k=3, method='qr') + spl_2 = make_lsq_spline(x, y, self.t, k=3, method='qr') + + xx = (x[1:] + x[:-1]) / 2.0 + xp_assert_close(spl_1(xx), spl_2(xx), atol=1e-15) + + +class PackedMatrix: + """A simplified CSR format for when non-zeros in each row are consecutive. + + Assuming that each row of an `(m, nc)` matrix 1) only has `nz` non-zeros, and + 2) these non-zeros are consecutive, we only store an `(m, nz)` matrix of + non-zeros and a 1D array of row offsets. This way, a row `i` of the original + matrix A is ``A[i, offset[i]: offset[i] + nz]``. + + """ + def __init__(self, a, offset, nc): + self.a = a + self.offset = offset + self.nc = nc + + assert a.ndim == 2 + assert offset.ndim == 1 + assert a.shape[0] == offset.shape[0] + + @property + def shape(self): + return self.a.shape[0], self.nc + + def todense(self): + out = np.zeros(self.shape) + nelem = self.a.shape[1] + for i in range(out.shape[0]): + nel = min(self.nc - self.offset[i], nelem) + out[i, self.offset[i]:self.offset[i] + nel] = self.a[i, :nel] + return out + + +def _qr_reduce_py(a_p, y, startrow=1): + """This is a python counterpart of the `_qr_reduce` routine, + declared in interpolate/src/__fitpack.h + """ + from scipy.linalg.lapack import dlartg + + # unpack the packed format + a = a_p.a + offset = a_p.offset + nc = a_p.nc + + m, nz = a.shape + + assert y.shape[0] == m + R = a.copy() + y1 = y.copy() + + for i in range(startrow, m): + oi = offset[i] + for j in range(oi, nc): + # rotate only the lower diagonal + if j >= min(i, nc): + break + + # In dense format: diag a1[j, j] vs a1[i, j] + c, s, r = dlartg(R[j, 0], R[i, 0]) + + # rotate l.h.s. + R[j, 0] = r + for l in range(1, nz): + R[j, l], R[i, l-1] = fprota(c, s, R[j, l], R[i, l]) + R[i, -1] = 0.0 + + # rotate r.h.s. + for l in range(y1.shape[1]): + y1[j, l], y1[i, l] = fprota(c, s, y1[j, l], y1[i, l]) + + # convert to packed + offs = list(range(R.shape[0])) + R_p = PackedMatrix(R, np.array(offs, dtype=np.int64), nc) + + return R_p, y1 + + +def fprota(c, s, a, b): + """Givens rotate [a, b]. + + [aa] = [ c s] @ [a] + [bb] [-s c] [b] + + """ + aa = c*a + s*b + bb = -s*a + c*b + return aa, bb + + +def fpback(R_p, y): + """Backsubsitution solve upper triangular banded `R @ c = y.` + + `R` is in the "packed" format: `R[i, :]` is `a[i, i:i+k+1]` + """ + R = R_p.a + _, nz = R.shape + nc = R_p.nc + assert y.shape[0] == R.shape[0] + + c = np.zeros_like(y[:nc]) + c[nc-1, ...] = y[nc-1] / R[nc-1, 0] + for i in range(nc-2, -1, -1): + nel = min(nz, nc-i) + # NB: broadcast R across trailing dimensions of `c`. + summ = (R[i, 1:nel, None] * c[i+1:i+nel, ...]).sum(axis=0) + c[i, ...] = ( y[i] - summ ) / R[i, 0] + return c + + +class TestGivensQR: + # Test row-by-row QR factorization, used for the LSQ spline construction. + # This is implementation detail; still test it separately. + def _get_xyt(self, n): + k = 3 + x = np.arange(n, dtype=float) + y = x**3 + 1/(1+x) + t = _not_a_knot(x, k) + return x, y, t, k + + def test_vs_full(self): + n = 10 + x, y, t, k = self._get_xyt(n) + + # design matrix + a_csr = BSpline.design_matrix(x, t, k) + + # dense QR + q, r = sl.qr(a_csr.todense()) + qTy = q.T @ y + + # prepare the PackedMatrix to factorize + # convert to "packed" format + m, nc = a_csr.shape + assert nc == t.shape[0] - k - 1 + + offset = a_csr.indices[::(k+1)] + offset = np.ascontiguousarray(offset, dtype=np.int64) + A = a_csr.data.reshape(m, k+1) + + R = PackedMatrix(A, offset, nc) + y_ = y[:, None] # _qr_reduce requires `y` a 2D array + _dierckx.qr_reduce(A, offset, nc, y_) # modifies arguments in-place + + # signs may differ + xp_assert_close(np.minimum(R.todense() + r, + R.todense() - r), np.zeros_like(r), atol=1e-15) + xp_assert_close(np.minimum(abs(qTy - y_[:, 0]), + abs(qTy + y_[:, 0])), np.zeros_like(qTy), atol=2e-13) + + # sign changes are consistent between Q and R: + c_full = sl.solve(r, qTy) + c_banded = _dierckx.fpback(R.a, R.nc, y_) + xp_assert_close(c_full, c_banded[:, 0], atol=5e-13) + + def test_py_vs_compiled(self): + # test _qr_reduce vs a python implementation + n = 10 + x, y, t, k = self._get_xyt(n) + + # design matrix + a_csr = BSpline.design_matrix(x, t, k) + m, nc = a_csr.shape + assert nc == t.shape[0] - k - 1 + + offset = a_csr.indices[::(k+1)] + offset = np.ascontiguousarray(offset, dtype=np.int64) + A = a_csr.data.reshape(m, k+1) + + R = PackedMatrix(A, offset, nc) + y_ = y[:, None] + + RR, yy = _qr_reduce_py(R, y_) + _dierckx.qr_reduce(A, offset, nc , y_) # in-place + + xp_assert_close(RR.a, R.a, atol=1e-15) + xp_assert_equal(RR.offset, R.offset, check_dtype=False) + assert RR.nc == R.nc + xp_assert_close(yy, y_, atol=1e-15) + + # Test C-level construction of the design matrix + + def test_data_matrix(self): + n = 10 + x, y, t, k = self._get_xyt(n) + w = np.arange(1, n+1, dtype=float) + + A, offset, nc = _dierckx.data_matrix(x, t, k, w) + + m = x.shape[0] + a_csr = BSpline.design_matrix(x, t, k) + a_w = (a_csr * w[:, None]).tocsr() + A_ = a_w.data.reshape((m, k+1)) + offset_ = a_w.indices[::(k+1)].astype(np.int64) + + xp_assert_close(A, A_, atol=1e-15) + xp_assert_equal(offset, offset_) + assert nc == t.shape[0] - k - 1 + + def test_fpback(self): + n = 10 + x, y, t, k = self._get_xyt(n) + y = np.c_[y, y**2] + A, offset, nc = _dierckx.data_matrix(x, t, k, np.ones_like(x)) + R = PackedMatrix(A, offset, nc) + _dierckx.qr_reduce(A, offset, nc, y) + + c = fpback(R, y) + cc = _dierckx.fpback(A, nc, y) + + xp_assert_close(cc, c, atol=1e-14) + + +def data_file(basename): + return os.path.join(os.path.abspath(os.path.dirname(__file__)), + 'data', basename) + + +class TestSmoothingSpline: + # + # test make_smoothing_spline + # + def test_invalid_input(self): + rng = np.random.RandomState(1234) + n = 100 + x = np.sort(rng.random_sample(n) * 4 - 2) + y = x**2 * np.sin(4 * x) + x**3 + rng.normal(0., 1.5, n) + + # ``x`` and ``y`` should have same shapes (1-D array) + with assert_raises(ValueError): + make_smoothing_spline(x, y[1:]) + with assert_raises(ValueError): + make_smoothing_spline(x[1:], y) + with assert_raises(ValueError): + make_smoothing_spline(x.reshape(1, n), y) + + # ``x`` should be an ascending array + with assert_raises(ValueError): + make_smoothing_spline(x[::-1], y) + + x_dupl = np.copy(x) + x_dupl[0] = x_dupl[1] + + with assert_raises(ValueError): + make_smoothing_spline(x_dupl, y) + + # x and y length must be >= 5 + x = np.arange(4) + y = np.ones(4) + exception_message = "``x`` and ``y`` length must be at least 5" + with pytest.raises(ValueError, match=exception_message): + make_smoothing_spline(x, y) + + def test_compare_with_GCVSPL(self): + """ + Data is generated in the following way: + >>> np.random.seed(1234) + >>> n = 100 + >>> x = np.sort(np.random.random_sample(n) * 4 - 2) + >>> y = np.sin(x) + np.random.normal(scale=.5, size=n) + >>> np.savetxt('x.csv', x) + >>> np.savetxt('y.csv', y) + + We obtain the result of performing the GCV smoothing splines + package (by Woltring, gcvspl) on the sample data points + using its version for Octave (https://github.com/srkuberski/gcvspl). + In order to use this implementation, one should clone the repository + and open the folder in Octave. + In Octave, we load up ``x`` and ``y`` (generated from Python code + above): + + >>> x = csvread('x.csv'); + >>> y = csvread('y.csv'); + + Then, in order to access the implementation, we compile gcvspl files in + Octave: + + >>> mex gcvsplmex.c gcvspl.c + >>> mex spldermex.c gcvspl.c + + The first function computes the vector of unknowns from the dataset + (x, y) while the second one evaluates the spline in certain points + with known vector of coefficients. + + >>> c = gcvsplmex( x, y, 2 ); + >>> y0 = spldermex( x, c, 2, x, 0 ); + + If we want to compare the results of the gcvspl code, we can save + ``y0`` in csv file: + + >>> csvwrite('y0.csv', y0); + + """ + # load the data sample + with np.load(data_file('gcvspl.npz')) as data: + # data points + x = data['x'] + y = data['y'] + + y_GCVSPL = data['y_GCVSPL'] + y_compr = make_smoothing_spline(x, y)(x) + + # such tolerance is explained by the fact that the spline is built + # using an iterative algorithm for minimizing the GCV criteria. These + # algorithms may vary, so the tolerance should be rather low. + # Not checking dtypes as gcvspl.npz stores little endian arrays, which + # result in conflicting dtypes on big endian systems. + xp_assert_close(y_compr, y_GCVSPL, atol=1e-4, rtol=1e-4, check_dtype=False) + + def test_non_regularized_case(self): + """ + In case the regularization parameter is 0, the resulting spline + is an interpolation spline with natural boundary conditions. + """ + # create data sample + rng = np.random.RandomState(1234) + n = 100 + x = np.sort(rng.random_sample(n) * 4 - 2) + y = x**2 * np.sin(4 * x) + x**3 + rng.normal(0., 1.5, n) + + spline_GCV = make_smoothing_spline(x, y, lam=0.) + spline_interp = make_interp_spline(x, y, 3, bc_type='natural') + + grid = np.linspace(x[0], x[-1], 2 * n) + xp_assert_close(spline_GCV(grid), + spline_interp(grid), + atol=1e-15) + + @pytest.mark.fail_slow(2) + def test_weighted_smoothing_spline(self): + # create data sample + rng = np.random.RandomState(1234) + n = 100 + x = np.sort(rng.random_sample(n) * 4 - 2) + y = x**2 * np.sin(4 * x) + x**3 + rng.normal(0., 1.5, n) + + spl = make_smoothing_spline(x, y) + + # in order not to iterate over all of the indices, we select 10 of + # them randomly + for ind in rng.choice(range(100), size=10): + w = np.ones(n) + w[ind] = 30. + spl_w = make_smoothing_spline(x, y, w) + # check that spline with weight in a certain point is closer to the + # original point than the one without weights + orig = abs(spl(x[ind]) - y[ind]) + weighted = abs(spl_w(x[ind]) - y[ind]) + + if orig < weighted: + raise ValueError(f'Spline with weights should be closer to the' + f' points than the original one: {orig:.4} < ' + f'{weighted:.4}') + + +################################ +# NdBSpline tests +def bspline2(xy, t, c, k): + """A naive 2D tensort product spline evaluation.""" + x, y = xy + tx, ty = t + nx = len(tx) - k - 1 + assert (nx >= k+1) + ny = len(ty) - k - 1 + assert (ny >= k+1) + res = sum(c[ix, iy] * B(x, k, ix, tx) * B(y, k, iy, ty) + for ix in range(nx) for iy in range(ny)) + return np.asarray(res) + + +def B(x, k, i, t): + if k == 0: + return 1.0 if t[i] <= x < t[i+1] else 0.0 + if t[i+k] == t[i]: + c1 = 0.0 + else: + c1 = (x - t[i])/(t[i+k] - t[i]) * B(x, k-1, i, t) + if t[i+k+1] == t[i+1]: + c2 = 0.0 + else: + c2 = (t[i+k+1] - x)/(t[i+k+1] - t[i+1]) * B(x, k-1, i+1, t) + return c1 + c2 + + +def bspline(x, t, c, k): + n = len(t) - k - 1 + assert (n >= k+1) and (len(c) >= n) + return sum(c[i] * B(x, k, i, t) for i in range(n)) + + +class NdBSpline0: + def __init__(self, t, c, k=3): + """Tensor product spline object. + + c[i1, i2, ..., id] * B(x1, i1) * B(x2, i2) * ... * B(xd, id) + + Parameters + ---------- + c : ndarray, shape (n1, n2, ..., nd, ...) + b-spline coefficients + t : tuple of 1D ndarrays + knot vectors in directions 1, 2, ... d + ``len(t[i]) == n[i] + k + 1`` + k : int or length-d tuple of integers + spline degrees. + """ + ndim = len(t) + assert ndim <= len(c.shape) + + try: + len(k) + except TypeError: + # make k a tuple + k = (k,)*ndim + + self.k = tuple(operator.index(ki) for ki in k) + self.t = tuple(np.asarray(ti, dtype=float) for ti in t) + self.c = c + + def __call__(self, x): + ndim = len(self.t) + # a single evaluation point: `x` is a 1D array_like, shape (ndim,) + assert len(x) == ndim + + # get the indices in an ndim-dimensional vector + i = ['none', ]*ndim + for d in range(ndim): + td, xd = self.t[d], x[d] + k = self.k[d] + + # find the index for x[d] + if xd == td[k]: + i[d] = k + else: + i[d] = np.searchsorted(td, xd) - 1 + assert td[i[d]] <= xd <= td[i[d]+1] + assert i[d] >= k and i[d] < len(td) - k + i = tuple(i) + + # iterate over the dimensions, form linear combinations of + # products B(x_1) * B(x_2) * ... B(x_N) of (k+1)**N b-splines + # which are non-zero at `i = (i_1, i_2, ..., i_N)`. + result = 0 + iters = [range(i[d] - self.k[d], i[d] + 1) for d in range(ndim)] + for idx in itertools.product(*iters): + term = self.c[idx] * np.prod([B(x[d], self.k[d], idx[d], self.t[d]) + for d in range(ndim)]) + result += term + return np.asarray(result) + + +class TestNdBSpline: + + def test_1D(self): + # test ndim=1 agrees with BSpline + rng = np.random.default_rng(12345) + n, k = 11, 3 + n_tr = 7 + t = np.sort(rng.uniform(size=n + k + 1)) + c = rng.uniform(size=(n, n_tr)) + + b = BSpline(t, c, k) + nb = NdBSpline((t,), c, k) + + xi = rng.uniform(size=21) + # NdBSpline expects xi.shape=(npts, ndim) + xp_assert_close(nb(xi[:, None]), + b(xi), atol=1e-14) + assert nb(xi[:, None]).shape == (xi.shape[0], c.shape[1]) + + def make_2d_case(self): + # make a 2D separable spline + x = np.arange(6) + y = x**3 + spl = make_interp_spline(x, y, k=3) + + y_1 = x**3 + 2*x + spl_1 = make_interp_spline(x, y_1, k=3) + + t2 = (spl.t, spl_1.t) + c2 = spl.c[:, None] * spl_1.c[None, :] + + return t2, c2, 3 + + def make_2d_mixed(self): + # make a 2D separable spline w/ kx=3, ky=2 + x = np.arange(6) + y = x**3 + spl = make_interp_spline(x, y, k=3) + + x = np.arange(5) + 1.5 + y_1 = x**2 + 2*x + spl_1 = make_interp_spline(x, y_1, k=2) + + t2 = (spl.t, spl_1.t) + c2 = spl.c[:, None] * spl_1.c[None, :] + + return t2, c2, spl.k, spl_1.k + + def test_2D_separable(self): + xi = [(1.5, 2.5), (2.5, 1), (0.5, 1.5)] + t2, c2, k = self.make_2d_case() + target = [x**3 * (y**3 + 2*y) for (x, y) in xi] + + # sanity check: bspline2 gives the product as constructed + xp_assert_close(np.asarray([bspline2(xy, t2, c2, k) for xy in xi]), + np.asarray(target), + check_shape=False, + atol=1e-14) + + # check evaluation on a 2D array: the 1D array of 2D points + bspl2 = NdBSpline(t2, c2, k=3) + assert bspl2(xi).shape == (len(xi), ) + xp_assert_close(bspl2(xi), + target, atol=1e-14) + + # now check on a multidim xi + rng = np.random.default_rng(12345) + xi = rng.uniform(size=(4, 3, 2)) * 5 + result = bspl2(xi) + assert result.shape == (4, 3) + + # also check the values + x, y = xi.reshape((-1, 2)).T + xp_assert_close(result.ravel(), + x**3 * (y**3 + 2*y), atol=1e-14) + + def test_2D_separable_2(self): + # test `c` with trailing dimensions, i.e. c.ndim > ndim + ndim = 2 + xi = [(1.5, 2.5), (2.5, 1), (0.5, 1.5)] + target = [x**3 * (y**3 + 2*y) for (x, y) in xi] + + t2, c2, k = self.make_2d_case() + c2_4 = np.dstack((c2, c2, c2, c2)) # c22.shape = (6, 6, 4) + + xy = (1.5, 2.5) + bspl2_4 = NdBSpline(t2, c2_4, k=3) + result = bspl2_4(xy) + val_single = NdBSpline(t2, c2, k)(xy) + assert result.shape == (4,) + xp_assert_close(result, + [val_single, ]*4, atol=1e-14) + + # now try the array xi : the output.shape is (3, 4) where 3 + # is the number of points in xi and 4 is the trailing dimension of c + assert bspl2_4(xi).shape == np.shape(xi)[:-1] + bspl2_4.c.shape[ndim:] + xp_assert_close(bspl2_4(xi), np.asarray(target)[:, None], + check_shape=False, + atol=5e-14) + + # two trailing dimensions + c2_22 = c2_4.reshape((6, 6, 2, 2)) + bspl2_22 = NdBSpline(t2, c2_22, k=3) + + result = bspl2_22(xy) + assert result.shape == (2, 2) + xp_assert_close(result, + [[val_single, val_single], + [val_single, val_single]], atol=1e-14) + + # now try the array xi : the output shape is (3, 2, 2) + # for 3 points in xi and c trailing dimensions being (2, 2) + assert (bspl2_22(xi).shape == + np.shape(xi)[:-1] + bspl2_22.c.shape[ndim:]) + xp_assert_close(bspl2_22(xi), np.asarray(target)[:, None, None], + check_shape=False, + atol=5e-14) + + + def test_2D_separable_2_complex(self): + # test `c` with c.dtype == complex, with and w/o trailing dims + xi = [(1.5, 2.5), (2.5, 1), (0.5, 1.5)] + target = [x**3 * (y**3 + 2*y) for (x, y) in xi] + + target = [t + 2j*t for t in target] + + t2, c2, k = self.make_2d_case() + c2 = c2 * (1 + 2j) + c2_4 = np.dstack((c2, c2, c2, c2)) # c2_4.shape = (6, 6, 4) + + xy = (1.5, 2.5) + bspl2_4 = NdBSpline(t2, c2_4, k=3) + result = bspl2_4(xy) + val_single = NdBSpline(t2, c2, k)(xy) + assert result.shape == (4,) + xp_assert_close(result, + [val_single, ]*4, atol=1e-14) + + def test_2D_random(self): + rng = np.random.default_rng(12345) + k = 3 + tx = np.r_[0, 0, 0, 0, np.sort(rng.uniform(size=7)) * 3, 3, 3, 3, 3] + ty = np.r_[0, 0, 0, 0, np.sort(rng.uniform(size=8)) * 4, 4, 4, 4, 4] + c = rng.uniform(size=(tx.size-k-1, ty.size-k-1)) + + spl = NdBSpline((tx, ty), c, k=k) + + xi = (1., 1.) + xp_assert_close(spl(xi), + bspline2(xi, (tx, ty), c, k), atol=1e-14) + + xi = np.c_[[1, 1.5, 2], + [1.1, 1.6, 2.1]] + xp_assert_close(spl(xi), + [bspline2(xy, (tx, ty), c, k) for xy in xi], + atol=1e-14) + + def test_2D_mixed(self): + t2, c2, kx, ky = self.make_2d_mixed() + xi = [(1.4, 4.5), (2.5, 2.4), (4.5, 3.5)] + target = [x**3 * (y**2 + 2*y) for (x, y) in xi] + bspl2 = NdBSpline(t2, c2, k=(kx, ky)) + assert bspl2(xi).shape == (len(xi), ) + xp_assert_close(bspl2(xi), + target, atol=1e-14) + + def test_2D_derivative(self): + t2, c2, kx, ky = self.make_2d_mixed() + xi = [(1.4, 4.5), (2.5, 2.4), (4.5, 3.5)] + bspl2 = NdBSpline(t2, c2, k=(kx, ky)) + + der = bspl2(xi, nu=(1, 0)) + xp_assert_close(der, + [3*x**2 * (y**2 + 2*y) for x, y in xi], atol=1e-14) + + der = bspl2(xi, nu=(1, 1)) + xp_assert_close(der, + [3*x**2 * (2*y + 2) for x, y in xi], atol=1e-14) + + der = bspl2(xi, nu=(0, 0)) + xp_assert_close(der, + [x**3 * (y**2 + 2*y) for x, y in xi], atol=1e-14) + + with assert_raises(ValueError): + # all(nu >= 0) + der = bspl2(xi, nu=(-1, 0)) + + with assert_raises(ValueError): + # len(nu) == ndim + der = bspl2(xi, nu=(-1, 0, 1)) + + def test_2D_mixed_random(self): + rng = np.random.default_rng(12345) + kx, ky = 2, 3 + tx = np.r_[0, 0, 0, 0, np.sort(rng.uniform(size=7)) * 3, 3, 3, 3, 3] + ty = np.r_[0, 0, 0, 0, np.sort(rng.uniform(size=8)) * 4, 4, 4, 4, 4] + c = rng.uniform(size=(tx.size - kx - 1, ty.size - ky - 1)) + + xi = np.c_[[1, 1.5, 2], + [1.1, 1.6, 2.1]] + + bspl2 = NdBSpline((tx, ty), c, k=(kx, ky)) + bspl2_0 = NdBSpline0((tx, ty), c, k=(kx, ky)) + + xp_assert_close(bspl2(xi), + [bspl2_0(xp) for xp in xi], atol=1e-14) + + def test_tx_neq_ty(self): + # 2D separable spline w/ len(tx) != len(ty) + x = np.arange(6) + y = np.arange(7) + 1.5 + + spl_x = make_interp_spline(x, x**3, k=3) + spl_y = make_interp_spline(y, y**2 + 2*y, k=3) + cc = spl_x.c[:, None] * spl_y.c[None, :] + bspl = NdBSpline((spl_x.t, spl_y.t), cc, (spl_x.k, spl_y.k)) + + values = (x**3)[:, None] * (y**2 + 2*y)[None, :] + rgi = RegularGridInterpolator((x, y), values) + + xi = [(a, b) for a, b in itertools.product(x, y)] + bxi = bspl(xi) + + assert not np.isnan(bxi).any() + xp_assert_close(bxi, rgi(xi), atol=1e-14) + xp_assert_close(bxi.reshape(values.shape), values, atol=1e-14) + + def make_3d_case(self): + # make a 3D separable spline + x = np.arange(6) + y = x**3 + spl = make_interp_spline(x, y, k=3) + + y_1 = x**3 + 2*x + spl_1 = make_interp_spline(x, y_1, k=3) + + y_2 = x**3 + 3*x + 1 + spl_2 = make_interp_spline(x, y_2, k=3) + + t2 = (spl.t, spl_1.t, spl_2.t) + c2 = (spl.c[:, None, None] * + spl_1.c[None, :, None] * + spl_2.c[None, None, :]) + + return t2, c2, 3 + + def test_3D_separable(self): + rng = np.random.default_rng(12345) + x, y, z = rng.uniform(size=(3, 11)) * 5 + target = x**3 * (y**3 + 2*y) * (z**3 + 3*z + 1) + + t3, c3, k = self.make_3d_case() + bspl3 = NdBSpline(t3, c3, k=3) + + xi = [_ for _ in zip(x, y, z)] + result = bspl3(xi) + assert result.shape == (11,) + xp_assert_close(result, target, atol=1e-14) + + def test_3D_derivative(self): + t3, c3, k = self.make_3d_case() + bspl3 = NdBSpline(t3, c3, k=3) + rng = np.random.default_rng(12345) + x, y, z = rng.uniform(size=(3, 11)) * 5 + xi = [_ for _ in zip(x, y, z)] + + xp_assert_close(bspl3(xi, nu=(1, 0, 0)), + 3*x**2 * (y**3 + 2*y) * (z**3 + 3*z + 1), atol=1e-14) + + xp_assert_close(bspl3(xi, nu=(2, 0, 0)), + 6*x * (y**3 + 2*y) * (z**3 + 3*z + 1), atol=1e-14) + + xp_assert_close(bspl3(xi, nu=(2, 1, 0)), + 6*x * (3*y**2 + 2) * (z**3 + 3*z + 1), atol=1e-14) + + xp_assert_close(bspl3(xi, nu=(2, 1, 3)), + 6*x * (3*y**2 + 2) * (6), atol=1e-14) + + xp_assert_close(bspl3(xi, nu=(2, 1, 4)), + np.zeros(len(xi)), atol=1e-14) + + def test_3D_random(self): + rng = np.random.default_rng(12345) + k = 3 + tx = np.r_[0, 0, 0, 0, np.sort(rng.uniform(size=7)) * 3, 3, 3, 3, 3] + ty = np.r_[0, 0, 0, 0, np.sort(rng.uniform(size=8)) * 4, 4, 4, 4, 4] + tz = np.r_[0, 0, 0, 0, np.sort(rng.uniform(size=8)) * 4, 4, 4, 4, 4] + c = rng.uniform(size=(tx.size-k-1, ty.size-k-1, tz.size-k-1)) + + spl = NdBSpline((tx, ty, tz), c, k=k) + spl_0 = NdBSpline0((tx, ty, tz), c, k=k) + + xi = (1., 1., 1) + xp_assert_close(spl(xi), spl_0(xi), atol=1e-14) + + xi = np.c_[[1, 1.5, 2], + [1.1, 1.6, 2.1], + [0.9, 1.4, 1.9]] + xp_assert_close(spl(xi), [spl_0(xp) for xp in xi], atol=1e-14) + + def test_3D_random_complex(self): + rng = np.random.default_rng(12345) + k = 3 + tx = np.r_[0, 0, 0, 0, np.sort(rng.uniform(size=7)) * 3, 3, 3, 3, 3] + ty = np.r_[0, 0, 0, 0, np.sort(rng.uniform(size=8)) * 4, 4, 4, 4, 4] + tz = np.r_[0, 0, 0, 0, np.sort(rng.uniform(size=8)) * 4, 4, 4, 4, 4] + c = (rng.uniform(size=(tx.size-k-1, ty.size-k-1, tz.size-k-1)) + + rng.uniform(size=(tx.size-k-1, ty.size-k-1, tz.size-k-1))*1j) + + spl = NdBSpline((tx, ty, tz), c, k=k) + spl_re = NdBSpline((tx, ty, tz), c.real, k=k) + spl_im = NdBSpline((tx, ty, tz), c.imag, k=k) + + xi = np.c_[[1, 1.5, 2], + [1.1, 1.6, 2.1], + [0.9, 1.4, 1.9]] + xp_assert_close(spl(xi), + spl_re(xi) + 1j*spl_im(xi), atol=1e-14) + + @pytest.mark.parametrize('cls_extrap', [None, True]) + @pytest.mark.parametrize('call_extrap', [None, True]) + def test_extrapolate_3D_separable(self, cls_extrap, call_extrap): + # test that extrapolate=True does extrapolate + t3, c3, k = self.make_3d_case() + bspl3 = NdBSpline(t3, c3, k=3, extrapolate=cls_extrap) + + # evaluate out of bounds + x, y, z = [-2, -1, 7], [-3, -0.5, 6.5], [-1, -1.5, 7.5] + x, y, z = map(np.asarray, (x, y, z)) + xi = [_ for _ in zip(x, y, z)] + target = x**3 * (y**3 + 2*y) * (z**3 + 3*z + 1) + + result = bspl3(xi, extrapolate=call_extrap) + xp_assert_close(result, target, atol=1e-14) + + @pytest.mark.parametrize('extrap', [(False, True), (True, None)]) + def test_extrapolate_3D_separable_2(self, extrap): + # test that call(..., extrapolate=None) defers to self.extrapolate, + # otherwise supersedes self.extrapolate + t3, c3, k = self.make_3d_case() + cls_extrap, call_extrap = extrap + bspl3 = NdBSpline(t3, c3, k=3, extrapolate=cls_extrap) + + # evaluate out of bounds + x, y, z = [-2, -1, 7], [-3, -0.5, 6.5], [-1, -1.5, 7.5] + x, y, z = map(np.asarray, (x, y, z)) + xi = [_ for _ in zip(x, y, z)] + target = x**3 * (y**3 + 2*y) * (z**3 + 3*z + 1) + + result = bspl3(xi, extrapolate=call_extrap) + xp_assert_close(result, target, atol=1e-14) + + def test_extrapolate_false_3D_separable(self): + # test that extrapolate=False produces nans for out-of-bounds values + t3, c3, k = self.make_3d_case() + bspl3 = NdBSpline(t3, c3, k=3) + + # evaluate out of bounds and inside + x, y, z = [-2, 1, 7], [-3, 0.5, 6.5], [-1, 1.5, 7.5] + x, y, z = map(np.asarray, (x, y, z)) + xi = [_ for _ in zip(x, y, z)] + target = x**3 * (y**3 + 2*y) * (z**3 + 3*z + 1) + + result = bspl3(xi, extrapolate=False) + assert np.isnan(result[0]) + assert np.isnan(result[-1]) + xp_assert_close(result[1:-1], target[1:-1], atol=1e-14) + + def test_x_nan_3D(self): + # test that spline(nan) is nan + t3, c3, k = self.make_3d_case() + bspl3 = NdBSpline(t3, c3, k=3) + + # evaluate out of bounds and inside + x = np.asarray([-2, 3, np.nan, 1, 2, 7, np.nan]) + y = np.asarray([-3, 3.5, 1, np.nan, 3, 6.5, 6.5]) + z = np.asarray([-1, 3.5, 2, 3, np.nan, 7.5, 7.5]) + xi = [_ for _ in zip(x, y, z)] + target = x**3 * (y**3 + 2*y) * (z**3 + 3*z + 1) + mask = np.isnan(x) | np.isnan(y) | np.isnan(z) + target[mask] = np.nan + + result = bspl3(xi) + assert np.isnan(result[mask]).all() + xp_assert_close(result, target, atol=1e-14) + + def test_non_c_contiguous(self): + # check that non C-contiguous inputs are OK + rng = np.random.default_rng(12345) + kx, ky = 3, 3 + tx = np.sort(rng.uniform(low=0, high=4, size=16)) + tx = np.r_[(tx[0],)*kx, tx, (tx[-1],)*kx] + ty = np.sort(rng.uniform(low=0, high=4, size=16)) + ty = np.r_[(ty[0],)*ky, ty, (ty[-1],)*ky] + + assert not tx[::2].flags.c_contiguous + assert not ty[::2].flags.c_contiguous + + c = rng.uniform(size=(tx.size//2 - kx - 1, ty.size//2 - ky - 1)) + c = c.T + assert not c.flags.c_contiguous + + xi = np.c_[[1, 1.5, 2], + [1.1, 1.6, 2.1]] + + bspl2 = NdBSpline((tx[::2], ty[::2]), c, k=(kx, ky)) + bspl2_0 = NdBSpline0((tx[::2], ty[::2]), c, k=(kx, ky)) + + xp_assert_close(bspl2(xi), + [bspl2_0(xp) for xp in xi], atol=1e-14) + + def test_readonly(self): + t3, c3, k = self.make_3d_case() + bspl3 = NdBSpline(t3, c3, k=3) + + for i in range(3): + t3[i].flags.writeable = False + c3.flags.writeable = False + + bspl3_ = NdBSpline(t3, c3, k=3) + + assert bspl3((1, 2, 3)) == bspl3_((1, 2, 3)) + + def test_design_matrix(self): + t3, c3, k = self.make_3d_case() + + xi = np.asarray([[1, 2, 3], [4, 5, 6]]) + dm = NdBSpline(t3, c3, k).design_matrix(xi, t3, k) + dm1 = NdBSpline.design_matrix(xi, t3, [k, k, k]) + assert dm.shape[0] == xi.shape[0] + xp_assert_close(dm.todense(), dm1.todense(), atol=1e-16) + + with assert_raises(ValueError): + NdBSpline.design_matrix([1, 2, 3], t3, [k]*3) + + with assert_raises(ValueError, match="Data and knots*"): + NdBSpline.design_matrix([[1, 2]], t3, [k]*3) + + @pytest.mark.thread_unsafe + def test_concurrency(self): + rng = np.random.default_rng(12345) + k = 3 + tx = np.r_[0, 0, 0, 0, np.sort(rng.uniform(size=7)) * 3, 3, 3, 3, 3] + ty = np.r_[0, 0, 0, 0, np.sort(rng.uniform(size=8)) * 4, 4, 4, 4, 4] + tz = np.r_[0, 0, 0, 0, np.sort(rng.uniform(size=8)) * 4, 4, 4, 4, 4] + c = rng.uniform(size=(tx.size-k-1, ty.size-k-1, tz.size-k-1)) + + spl = NdBSpline((tx, ty, tz), c, k=k) + + def worker_fn(_, spl): + xi = np.c_[[1, 1.5, 2], + [1.1, 1.6, 2.1], + [0.9, 1.4, 1.9]] + spl(xi) + + _run_concurrent_barrier(10, worker_fn, spl) + + +class TestMakeND: + def test_2D_separable_simple(self): + x = np.arange(6) + y = np.arange(6) + 0.5 + values = x[:, None]**3 * (y**3 + 2*y)[None, :] + xi = [(a, b) for a, b in itertools.product(x, y)] + + bspl = make_ndbspl((x, y), values, k=1) + xp_assert_close(bspl(xi), values.ravel(), atol=1e-15) + + # test the coefficients vs outer product of 1D coefficients + spl_x = make_interp_spline(x, x**3, k=1) + spl_y = make_interp_spline(y, y**3 + 2*y, k=1) + cc = spl_x.c[:, None] * spl_y.c[None, :] + xp_assert_close(cc, bspl.c, atol=1e-11, rtol=0) + + # test against RGI + from scipy.interpolate import RegularGridInterpolator as RGI + rgi = RGI((x, y), values, method='linear') + xp_assert_close(rgi(xi), bspl(xi), atol=1e-14) + + def test_2D_separable_trailing_dims(self): + # test `c` with trailing dimensions, i.e. c.ndim > ndim + x = np.arange(6) + y = np.arange(6) + xi = [(a, b) for a, b in itertools.product(x, y)] + + # make values4.shape = (6, 6, 4) + values = x[:, None]**3 * (y**3 + 2*y)[None, :] + values4 = np.dstack((values, values, values, values)) + bspl = make_ndbspl((x, y), values4, k=3, solver=ssl.spsolve) + + result = bspl(xi) + target = np.dstack((values, values, values, values)).astype(float) + assert result.shape == (36, 4) + xp_assert_close(result.reshape(6, 6, 4), + target, atol=1e-14) + + # now two trailing dimensions + values22 = values4.reshape((6, 6, 2, 2)) + bspl = make_ndbspl((x, y), values22, k=3, solver=ssl.spsolve) + + result = bspl(xi) + assert result.shape == (36, 2, 2) + xp_assert_close(result.reshape(6, 6, 2, 2), + target.reshape((6, 6, 2, 2)), atol=1e-14) + + @pytest.mark.parametrize('k', [(3, 3), (1, 1), (3, 1), (1, 3), (3, 5)]) + def test_2D_mixed(self, k): + # make a 2D separable spline w/ len(tx) != len(ty) + x = np.arange(6) + y = np.arange(7) + 1.5 + xi = [(a, b) for a, b in itertools.product(x, y)] + + values = (x**3)[:, None] * (y**2 + 2*y)[None, :] + bspl = make_ndbspl((x, y), values, k=k, solver=ssl.spsolve) + xp_assert_close(bspl(xi), values.ravel(), atol=1e-15) + + def _get_sample_2d_data(self): + # from test_rgi.py::TestIntepN + x = np.array([.5, 2., 3., 4., 5.5, 6.]) + y = np.array([.5, 2., 3., 4., 5.5, 6.]) + z = np.array( + [ + [1, 2, 1, 2, 1, 1], + [1, 2, 1, 2, 1, 1], + [1, 2, 3, 2, 1, 1], + [1, 2, 2, 2, 1, 1], + [1, 2, 1, 2, 1, 1], + [1, 2, 2, 2, 1, 1], + ] + ) + return x, y, z + + def test_2D_vs_RGI_linear(self): + x, y, z = self._get_sample_2d_data() + bspl = make_ndbspl((x, y), z, k=1) + rgi = RegularGridInterpolator((x, y), z, method='linear') + + xi = np.array([[1, 2.3, 5.3, 0.5, 3.3, 1.2, 3], + [1, 3.3, 1.2, 4.0, 5.0, 1.0, 3]]).T + + xp_assert_close(bspl(xi), rgi(xi), atol=1e-14) + + def test_2D_vs_RGI_cubic(self): + x, y, z = self._get_sample_2d_data() + bspl = make_ndbspl((x, y), z, k=3, solver=ssl.spsolve) + rgi = RegularGridInterpolator((x, y), z, method='cubic_legacy') + + xi = np.array([[1, 2.3, 5.3, 0.5, 3.3, 1.2, 3], + [1, 3.3, 1.2, 4.0, 5.0, 1.0, 3]]).T + + xp_assert_close(bspl(xi), rgi(xi), atol=1e-14) + + @pytest.mark.parametrize('solver', [ssl.gmres, ssl.gcrotmk]) + def test_2D_vs_RGI_cubic_iterative(self, solver): + # same as `test_2D_vs_RGI_cubic`, only with an iterative solver. + # Note the need to add an explicit `rtol` solver_arg to achieve the + # target accuracy of 1e-14. (the relation between solver atol/rtol + # and the accuracy of the final result is not direct and needs experimenting) + x, y, z = self._get_sample_2d_data() + bspl = make_ndbspl((x, y), z, k=3, solver=solver, rtol=1e-6) + rgi = RegularGridInterpolator((x, y), z, method='cubic_legacy') + + xi = np.array([[1, 2.3, 5.3, 0.5, 3.3, 1.2, 3], + [1, 3.3, 1.2, 4.0, 5.0, 1.0, 3]]).T + + xp_assert_close(bspl(xi), rgi(xi), atol=1e-14, rtol=1e-7) + + def test_2D_vs_RGI_quintic(self): + x, y, z = self._get_sample_2d_data() + bspl = make_ndbspl((x, y), z, k=5, solver=ssl.spsolve) + rgi = RegularGridInterpolator((x, y), z, method='quintic_legacy') + + xi = np.array([[1, 2.3, 5.3, 0.5, 3.3, 1.2, 3], + [1, 3.3, 1.2, 4.0, 5.0, 1.0, 3]]).T + + xp_assert_close(bspl(xi), rgi(xi), atol=1e-14) + + @pytest.mark.parametrize( + 'k, meth', [(1, 'linear'), (3, 'cubic_legacy'), (5, 'quintic_legacy')] + ) + def test_3D_random_vs_RGI(self, k, meth): + rndm = np.random.default_rng(123456) + x = np.cumsum(rndm.uniform(size=6)) + y = np.cumsum(rndm.uniform(size=7)) + z = np.cumsum(rndm.uniform(size=8)) + values = rndm.uniform(size=(6, 7, 8)) + + bspl = make_ndbspl((x, y, z), values, k=k, solver=ssl.spsolve) + rgi = RegularGridInterpolator((x, y, z), values, method=meth) + + xi = np.random.uniform(low=0.7, high=2.1, size=(11, 3)) + xp_assert_close(bspl(xi), rgi(xi), atol=1e-14) + + def test_solver_err_not_converged(self): + x, y, z = self._get_sample_2d_data() + solver_args = {'maxiter': 1} + with assert_raises(ValueError, match='solver'): + make_ndbspl((x, y), z, k=3, **solver_args) + + with assert_raises(ValueError, match='solver'): + make_ndbspl((x, y), np.dstack((z, z)), k=3, **solver_args) + + +class TestFpchec: + # https://github.com/scipy/scipy/blob/main/scipy/interpolate/fitpack/fpchec.f + + def test_1D_x_t(self): + k = 1 + t = np.arange(12).reshape(2, 6) + x = np.arange(12) + + with pytest.raises(ValueError, match="1D sequence"): + _b.fpcheck(x, t, k) + + with pytest.raises(ValueError, match="1D sequence"): + _b.fpcheck(t, x, k) + + def test_condition_1(self): + # c 1) k+1 <= n-k-1 <= m + k = 3 + n = 2*(k + 1) - 1 # not OK + m = n + 11 # OK + t = np.arange(n) + x = np.arange(m) + + assert dfitpack.fpchec(x, t, k) == 10 + with pytest.raises(ValueError, match="Need k+1*"): + _b.fpcheck(x, t, k) + + n = 2*(k+1) + 1 # OK + m = n - k - 2 # not OK + t = np.arange(n) + x = np.arange(m) + + assert dfitpack.fpchec(x, t, k) == 10 + with pytest.raises(ValueError, match="Need k+1*"): + _b.fpcheck(x, t, k) + + def test_condition_2(self): + # c 2) t(1) <= t(2) <= ... <= t(k+1) + # c t(n-k) <= t(n-k+1) <= ... <= t(n) + k = 3 + t = [0]*(k+1) + [2] + [5]*(k+1) # this is OK + x = [1, 2, 3, 4, 4.5] + + assert dfitpack.fpchec(x, t, k) == 0 + assert _b.fpcheck(x, t, k) is None # does not raise + + tt = t.copy() + tt[-1] = tt[0] # not OK + assert dfitpack.fpchec(x, tt, k) == 20 + with pytest.raises(ValueError, match="Last k knots*"): + _b.fpcheck(x, tt, k) + + tt = t.copy() + tt[0] = tt[-1] # not OK + assert dfitpack.fpchec(x, tt, k) == 20 + with pytest.raises(ValueError, match="First k knots*"): + _b.fpcheck(x, tt, k) + + def test_condition_3(self): + # c 3) t(k+1) < t(k+2) < ... < t(n-k) + k = 3 + t = [0]*(k+1) + [2, 3] + [5]*(k+1) # this is OK + x = [1, 2, 3, 3.5, 4, 4.5] + assert dfitpack.fpchec(x, t, k) == 0 + assert _b.fpcheck(x, t, k) is None + + t = [0]*(k+1) + [2, 2] + [5]*(k+1) # this is not OK + assert dfitpack.fpchec(x, t, k) == 30 + with pytest.raises(ValueError, match="Internal knots*"): + _b.fpcheck(x, t, k) + + def test_condition_4(self): + # c 4) t(k+1) <= x(i) <= t(n-k) + # NB: FITPACK's fpchec only checks x[0] & x[-1], so we follow. + k = 3 + t = [0]*(k+1) + [5]*(k+1) + x = [1, 2, 3, 3.5, 4, 4.5] # this is OK + assert dfitpack.fpchec(x, t, k) == 0 + assert _b.fpcheck(x, t, k) is None + + xx = x.copy() + xx[0] = t[0] # still OK + assert dfitpack.fpchec(xx, t, k) == 0 + assert _b.fpcheck(x, t, k) is None + + xx = x.copy() + xx[0] = t[0] - 1 # not OK + assert dfitpack.fpchec(xx, t, k) == 40 + with pytest.raises(ValueError, match="Out of bounds*"): + _b.fpcheck(xx, t, k) + + xx = x.copy() + xx[-1] = t[-1] + 1 # not OK + assert dfitpack.fpchec(xx, t, k) == 40 + with pytest.raises(ValueError, match="Out of bounds*"): + _b.fpcheck(xx, t, k) + + # ### Test the S-W condition (no 5) + # c 5) the conditions specified by schoenberg and whitney must hold + # c for at least one subset of data points, i.e. there must be a + # c subset of data points y(j) such that + # c t(j) < y(j) < t(j+k+1), j=1,2,...,n-k-1 + def test_condition_5_x1xm(self): + # x(1).ge.t(k2) .or. x(m).le.t(nk1) + k = 1 + t = [0, 0, 1, 2, 2] + x = [1.1, 1.1, 1.1] + assert dfitpack.fpchec(x, t, k) == 50 + with pytest.raises(ValueError, match="Schoenberg-Whitney*"): + _b.fpcheck(x, t, k) + + x = [0.5, 0.5, 0.5] + assert dfitpack.fpchec(x, t, k) == 50 + with pytest.raises(ValueError, match="Schoenberg-Whitney*"): + _b.fpcheck(x, t, k) + + def test_condition_5_k1(self): + # special case nk3 (== n - k - 2) < 2 + k = 1 + t = [0, 0, 1, 1] + x = [0.5, 0.6] + assert dfitpack.fpchec(x, t, k) == 0 + assert _b.fpcheck(x, t, k) is None + + def test_condition_5_1(self): + # basically, there can't be an interval of t[j]..t[j+k+1] with no x + k = 3 + t = [0]*(k+1) + [2] + [5]*(k+1) + x = [3]*5 + assert dfitpack.fpchec(x, t, k) == 50 + with pytest.raises(ValueError, match="Schoenberg-Whitney*"): + _b.fpcheck(x, t, k) + + t = [0]*(k+1) + [2] + [5]*(k+1) + x = [1]*5 + assert dfitpack.fpchec(x, t, k) == 50 + with pytest.raises(ValueError, match="Schoenberg-Whitney*"): + _b.fpcheck(x, t, k) + + def test_condition_5_2(self): + # same as _5_1, only the empty interval is in the middle + k = 3 + t = [0]*(k+1) + [2, 3] + [5]*(k+1) + x = [1.1]*5 + [4] + + assert dfitpack.fpchec(x, t, k) == 50 + with pytest.raises(ValueError, match="Schoenberg-Whitney*"): + _b.fpcheck(x, t, k) + + # and this one is OK + x = [1.1]*4 + [4, 4] + assert dfitpack.fpchec(x, t, k) == 0 + assert _b.fpcheck(x, t, k) is None + + def test_condition_5_3(self): + # similar to _5_2, covers a different failure branch + k = 1 + t = [0, 0, 2, 3, 4, 5, 6, 7, 7] + x = [1, 1, 1, 5.2, 5.2, 5.2, 6.5] + + assert dfitpack.fpchec(x, t, k) == 50 + with pytest.raises(ValueError, match="Schoenberg-Whitney*"): + _b.fpcheck(x, t, k) + + +# ### python replicas of generate_knots(...) implementation details, for testing. +# ### see TestGenerateKnots::test_split_and_add_knot +def _split(x, t, k, residuals): + """Split the knot interval into "runs". + """ + ix = np.searchsorted(x, t[k:-k]) + # sum half-open intervals + fparts = [residuals[ix[i]:ix[i+1]].sum() for i in range(len(ix)-1)] + carries = residuals[ix[1:-1]] + + for i in range(len(carries)): # split residuals at internal knots + carry = carries[i] / 2 + fparts[i] += carry + fparts[i+1] -= carry + + fparts[-1] += residuals[-1] # add the contribution of the last knot + + xp_assert_close(sum(fparts), sum(residuals), atol=1e-15) + + return fparts, ix + + +def _add_knot(x, t, k, residuals): + """Insert a new knot given reduals.""" + fparts, ix = _split(x, t, k, residuals) + + # find the interval with max fparts and non-zero number of x values inside + idx_max = -101 + fpart_max = -1e100 + for i in range(len(fparts)): + if ix[i+1] - ix[i] > 1 and fparts[i] > fpart_max: + idx_max = i + fpart_max = fparts[i] + + if idx_max == -101: + raise ValueError("Internal error, please report it to SciPy developers.") + + # round up, like Dierckx does? This is really arbitrary though. + idx_newknot = (ix[idx_max] + ix[idx_max+1] + 1) // 2 + new_knot = x[idx_newknot] + idx_t = np.searchsorted(t, new_knot) + t_new = np.r_[t[:idx_t], new_knot, t[idx_t:]] + return t_new + + +class TestGenerateKnots: + def test_split_add_knot(self): + # smoke test implementation details: insert a new knot given residuals + x = np.arange(8, dtype=float) + y = x**3 + 1./(1 + x) + k = 3 + t = np.array([0.]*(k+1) + [7.]*(k+1)) + spl = make_lsq_spline(x, y, k=k, t=t) + residuals = (spl(x) - y)**2 + + from scipy.interpolate import _fitpack_repro as _fr + new_t = _fr.add_knot(x, t, k, residuals) + new_t_py = _add_knot(x, t, k, residuals) + + xp_assert_close(new_t, new_t_py, atol=1e-15) + + # redo with new knots + spl2 = make_lsq_spline(x, y, k=k, t=new_t) + residuals2 = (spl2(x) - y)**2 + + new_t2 = _fr.add_knot(x, new_t, k, residuals2) + new_t2_py = _add_knot(x, new_t, k, residuals2) + + xp_assert_close(new_t2, new_t2_py, atol=1e-15) + + @pytest.mark.parametrize('k', [1, 2, 3, 4, 5]) + def test_s0(self, k): + x = np.arange(8, dtype=np.float64) + y = np.sin(x*np.pi/8) + t = list(generate_knots(x, y, k=k, s=0))[-1] + + tt = splrep(x, y, k=k, s=0)[0] + xp_assert_close(t, tt, atol=1e-15) + + def test_s0_1(self): + # with these data, naive algorithm tries to insert >= nmax knots + n = 10 + x = np.arange(n) + y = x**3 + knots = list(generate_knots(x, y, k=3, s=0)) # does not error out + xp_assert_close(knots[-1], _not_a_knot(x, 3), atol=1e-15) + + def test_s0_n20(self): + n = 20 + x = np.arange(n) + y = x**3 + knots = list(generate_knots(x, y, k=3, s=0)) + xp_assert_close(knots[-1], _not_a_knot(x, 3), atol=1e-15) + + def test_s0_nest(self): + # s=0 and non-default nest: not implemented, errors out + x = np.arange(10) + y = x**3 + with assert_raises(ValueError): + list(generate_knots(x, y, k=3, s=0, nest=10)) + + def test_s_switch(self): + # test the process switching to interpolating knots when len(t) == m + k + 1 + """ + To generate the `wanted` list below apply the following diff and rerun + the test. The stdout will contain successive iterations of the `t` + array. + +$ git diff scipy/interpolate/fitpack/fpcurf.f +diff --git a/scipy/interpolate/fitpack/fpcurf.f b/scipy/interpolate/fitpack/fpcurf.f +index 1afb1900f1..d817e51ad8 100644 +--- a/scipy/interpolate/fitpack/fpcurf.f ++++ b/scipy/interpolate/fitpack/fpcurf.f +@@ -216,6 +216,9 @@ c t(j+k) <= x(i) <= t(j+k+1) and store it in fpint(j),j=1,2,...nrint. + do 190 l=1,nplus + c add a new knot. + call fpknot(x,m,t,n,fpint,nrdata,nrint,nest,1) ++ print*, l, nest, ': ', t ++ print*, "n, nmax = ", n, nmax ++ + c if n=nmax we locate the knots as for interpolation. + if(n.eq.nmax) go to 10 + c test whether we cannot further increase the number of knots. + """ # NOQA: E501 + x = np.arange(8) + y = np.sin(x*np.pi/8) + k = 3 + + knots = list(generate_knots(x, y, k=k, s=1e-7)) + wanted = [[0., 0., 0., 0., 7., 7., 7., 7.], + [0., 0., 0., 0., 4., 7., 7., 7., 7.], + [0., 0., 0., 0., 2., 4., 7., 7., 7., 7.], + [0., 0., 0., 0., 2., 4., 6., 7., 7., 7., 7.], + [0., 0., 0., 0., 2., 3., 4., 5., 7, 7., 7., 7.] + ] + + assert len(knots) == len(wanted) + for t, tt in zip(knots, wanted): + xp_assert_close(t, tt, atol=1e-15) + + # also check that the last knot vector matches FITPACK + t, _, _ = splrep(x, y, k=k, s=1e-7) + xp_assert_close(knots[-1], t, atol=1e-15) + + def test_list_input(self): + # test that list inputs are accepted + x = list(range(8)) + gen = generate_knots(x, x, s=0.1, k=1) + next(gen) + + def test_nest(self): + # test that nest < nmax stops the process early (and we get 10 knots not 12) + x = np.arange(8) + y = np.sin(x*np.pi/8) + s = 1e-7 + + knots = list(generate_knots(x, y, k=3, s=s, nest=10)) + xp_assert_close(knots[-1], + [0., 0., 0., 0., 2., 4., 7., 7., 7., 7.], atol=1e-15) + + with assert_raises(ValueError): + # nest < 2*(k+1) + list(generate_knots(x, y, k=3, nest=4)) + + def test_weights(self): + x = np.arange(8) + y = np.sin(x*np.pi/8) + + with assert_raises(ValueError): + list(generate_knots(x, y, w=np.arange(11))) # len(w) != len(x) + + with assert_raises(ValueError): + list(generate_knots(x, y, w=-np.ones(8))) # w < 0 + + @pytest.mark.parametrize("npts", [30, 50, 100]) + @pytest.mark.parametrize("s", [0.1, 1e-2, 0]) + def test_vs_splrep(self, s, npts): + # XXX this test is brittle: differences start apearing for k=3 and s=1e-6, + # also for k != 3. Might be worth investigating at some point. + # I think we do not really guarantee exact agreement with splrep. Instead, + # we guarantee it is the same *in most cases*; otherwise slight differences + # are allowed. There is no theorem, it is al heuristics by P. Dierckx. + # The best we can do it to best-effort reproduce it. + rndm = np.random.RandomState(12345) + x = 10*np.sort(rndm.uniform(size=npts)) + y = np.sin(x*np.pi/10) + np.exp(-(x-6)**2) + + k = 3 + t = splrep(x, y, k=k, s=s)[0] + tt = list(generate_knots(x, y, k=k, s=s))[-1] + + xp_assert_close(tt, t, atol=1e-15) + + @pytest.mark.thread_unsafe + def test_s_too_small(self): + n = 14 + x = np.arange(n) + y = x**3 + + # XXX splrep warns that "s too small": ier=2 + knots = list(generate_knots(x, y, k=3, s=1e-50)) + + with suppress_warnings() as sup: + r = sup.record(RuntimeWarning) + tck = splrep(x, y, k=3, s=1e-50) + assert len(r) == 1 + xp_assert_equal(knots[-1], tck[0]) + + +def disc_naive(t, k): + """Straitforward way to compute the discontinuity matrix. For testing ONLY. + + This routine returns a dense matrix, while `_fitpack_repro.disc` returns + a packed one. + """ + n = t.shape[0] + + delta = t[n - k - 1] - t[k] + nrint = n - 2*k - 1 + + ti = t[k+1:n-k-1] # internal knots + tii = np.repeat(ti, 2) + tii[::2] += 1e-10 + tii[1::2] -= 1e-10 + m = BSpline(t, np.eye(n - k - 1), k)(tii, nu=k) + + matr = np.empty((nrint-1, m.shape[1]), dtype=float) + for i in range(0, m.shape[0], 2): + matr[i//2, :] = m[i, :] - m[i+1, :] + + matr *= (delta/nrint)**k / math.factorial(k) + return matr + + +class F_dense: + """ The r.h.s. of ``f(p) = s``, an analog of _fitpack_repro.F + Uses full matrices, so is for tests only. + """ + def __init__(self, x, y, t, k, s, w=None): + self.x = x + self.y = y + self.t = t + self.k = k + self.w = np.ones_like(x, dtype=float) if w is None else w + assert self.w.ndim == 1 + + # lhs + a_dense = BSpline(t, np.eye(t.shape[0] - k - 1), k)(x) + self.a_dense = a_dense * self.w[:, None] + + from scipy.interpolate import _fitpack_repro as _fr + self.b_dense = PackedMatrix(*_fr.disc(t, k)).todense() + + # rhs + assert y.ndim == 1 + yy = y * self.w + self.yy = np.r_[yy, np.zeros(self.b_dense.shape[0])] + + self.s = s + + def __call__(self, p): + ab = np.vstack((self.a_dense, self.b_dense / p)) + + # LSQ solution of ab @ c = yy + from scipy.linalg import qr, solve + q, r = qr(ab, mode='economic') + + qy = q.T @ self.yy + + nc = r.shape[1] + c = solve(r[:nc, :nc], qy[:nc]) + + spl = BSpline(self.t, c, self.k) + fp = np.sum(self.w**2 * (spl(self.x) - self.y)**2) + + self.spl = spl # store it + + return fp - self.s + + +class TestMakeSplrep: + def test_input_errors(self): + x = np.linspace(0, 10, 11) + y = np.linspace(0, 10, 12) + with assert_raises(ValueError): + # len(x) != len(y) + make_splrep(x, y) + + with assert_raises(ValueError): + # 0D inputs + make_splrep(1, 2, s=0.1) + + with assert_raises(ValueError): + # y.ndim > 2 + y = np.ones((x.size, 2, 2, 2)) + make_splrep(x, y, s=0.1) + + w = np.ones(12) + with assert_raises(ValueError): + # len(weights) != len(x) + make_splrep(x, x**3, w=w, s=0.1) + + w = -np.ones(12) + with assert_raises(ValueError): + # w < 0 + make_splrep(x, x**3, w=w, s=0.1) + + w = np.ones((x.shape[0], 2)) + with assert_raises(ValueError): + # w.ndim != 1 + make_splrep(x, x**3, w=w, s=0.1) + + with assert_raises(ValueError): + # x not ordered + make_splrep(x[::-1], x**3, s=0.1) + + with assert_raises(TypeError): + # k != int(k) + make_splrep(x, x**3, k=2.5, s=0.1) + + with assert_raises(ValueError): + # s < 0 + make_splrep(x, x**3, s=-1) + + with assert_raises(ValueError): + # nest < 2*k + 2 + make_splrep(x, x**3, k=3, nest=2, s=0.1) + + with assert_raises(ValueError): + # nest not None and s==0 + make_splrep(x, x**3, s=0, nest=11) + + with assert_raises(ValueError): + # len(x) != len(y) + make_splrep(np.arange(8), np.arange(9), s=0.1) + + def _get_xykt(self): + x = np.linspace(0, 5, 11) + y = np.sin(x*3.14 / 5)**2 + k = 3 + s = 1.7e-4 + tt = np.array([0]*(k+1) + [2.5, 4.0] + [5]*(k+1)) + + return x, y, k, s, tt + + def test_fitpack_F(self): + # test an implementation detail: banded/packed linalg vs full matrices + from scipy.interpolate._fitpack_repro import F + + x, y, k, s, t = self._get_xykt() + f = F(x, y[:, None], t, k, s) # F expects y to be 2D + f_d = F_dense(x, y, t, k, s) + for p in [1, 10, 100]: + xp_assert_close(f(p), f_d(p), atol=1e-15) + + def test_fitpack_F_with_weights(self): + # repeat test_fitpack_F, with weights + from scipy.interpolate._fitpack_repro import F + + x, y, k, s, t = self._get_xykt() + w = np.arange(x.shape[0], dtype=float) + fw = F(x, y[:, None], t, k, s, w=w) # F expects y to be 2D + fw_d = F_dense(x, y, t, k, s, w=w) + + f_d = F_dense(x, y, t, k, s) # no weights + + for p in [1, 10, 100]: + xp_assert_close(fw(p), fw_d(p), atol=1e-15) + assert not np.allclose(f_d(p), fw_d(p), atol=1e-15) + + def test_disc_matrix(self): + # test an implementation detail: discontinuity matrix + # (jumps of k-th derivative at knots) + import scipy.interpolate._fitpack_repro as _fr + + rng = np.random.default_rng(12345) + t = np.r_[0, 0, 0, 0, np.sort(rng.uniform(size=7))*5, 5, 5, 5, 5] + + n, k = len(t), 3 + D = PackedMatrix(*_fr.disc(t, k)).todense() + D_dense = disc_naive(t, k) + assert D.shape[0] == n - 2*k - 2 # number of internal knots + xp_assert_close(D, D_dense, atol=1e-15) + + def test_simple_vs_splrep(self): + x, y, k, s, tt = self._get_xykt() + tt = np.array([0]*(k+1) + [2.5, 4.0] + [5]*(k+1)) + + t,c,k = splrep(x, y, k=k, s=s) + assert all(t == tt) + + spl = make_splrep(x, y, k=k, s=s) + xp_assert_close(c[:spl.c.size], spl.c, atol=1e-15) + + def test_with_knots(self): + x, y, k, s, _ = self._get_xykt() + + t = list(generate_knots(x, y, k=k, s=s))[-1] + + spl_auto = make_splrep(x, y, k=k, s=s) + spl_t = make_splrep(x, y, t=t, k=k, s=s) + + xp_assert_close(spl_auto.t, spl_t.t, atol=1e-15) + xp_assert_close(spl_auto.c, spl_t.c, atol=1e-15) + assert spl_auto.k == spl_t.k + + def test_no_internal_knots(self): + # should not fail if there are no internal knots + n = 10 + x = np.arange(n) + y = x**3 + k = 3 + spl = make_splrep(x, y, k=k, s=1) + assert spl.t.shape[0] == 2*(k+1) + + def test_default_s(self): + n = 10 + x = np.arange(n) + y = x**3 + spl = make_splrep(x, y, k=3) + spl_i = make_interp_spline(x, y, k=3) + + xp_assert_close(spl.c, spl_i.c, atol=1e-15) + + @pytest.mark.thread_unsafe + def test_s_too_small(self): + # both splrep and make_splrep warn that "s too small": ier=2 + n = 14 + x = np.arange(n) + y = x**3 + + with suppress_warnings() as sup: + r = sup.record(RuntimeWarning) + tck = splrep(x, y, k=3, s=1e-50) + spl = make_splrep(x, y, k=3, s=1e-50) + assert len(r) == 2 + xp_assert_equal(spl.t, tck[0]) + xp_assert_close(np.r_[spl.c, [0]*(spl.k+1)], + tck[1], atol=5e-13) + + def test_shape(self): + # make sure coefficients have the right shape (not extra dims) + n, k = 10, 3 + x = np.arange(n) + y = x**3 + + spl = make_splrep(x, y, k=k) + spl_1 = make_splrep(x, y, k=k, s=1e-5) + + assert spl.c.ndim == 1 + assert spl_1.c.ndim == 1 + + # force the general code path, not shortcuts + spl_2 = make_splrep(x, y + 1/(1+y), k=k, s=1e-5) + assert spl_2.c.ndim == 1 + + def test_s0_vs_not(self): + # check that the shapes are consistent + n, k = 10, 3 + x = np.arange(n) + y = x**3 + + spl_0 = make_splrep(x, y, k=3, s=0) + spl_1 = make_splrep(x, y, k=3, s=1) + + assert spl_0.c.ndim == 1 + assert spl_1.c.ndim == 1 + + assert spl_0.t.shape[0] == n + k + 1 + assert spl_1.t.shape[0] == 2 * (k + 1) + + +class TestMakeSplprep: + def _get_xyk(self, m=10, k=3): + x = np.arange(m) * np.pi / m + y = [np.sin(x), np.cos(x)] + return x, y, k + + @pytest.mark.parametrize('s', [0, 0.1, 1e-3, 1e-5]) + def test_simple_vs_splprep(self, s): + # Check/document the interface vs splPrep + # The four values of `s` are to probe all code paths and shortcuts + m, k = 10, 3 + x = np.arange(m) * np.pi / m + y = [np.sin(x), np.cos(x)] + + # the number of knots depends on `s` (this is by construction) + num_knots = {0: 14, 0.1: 8, 1e-3: 8 + 1, 1e-5: 8 + 2} + + # construct the splines + (t, c, k), u_ = splprep(y, s=s) + spl, u = make_splprep(y, s=s) + + # parameters + xp_assert_close(u, u_, atol=1e-15) + + # knots + xp_assert_close(spl.t, t, atol=1e-15) + assert len(t) == num_knots[s] + + # coefficients: note the transpose + cc = np.asarray(c).T + xp_assert_close(spl.c, cc, atol=1e-15) + + # values: note axis=1 + xp_assert_close(spl(u), + BSpline(t, c, k, axis=1)(u), atol=1e-15) + + @pytest.mark.parametrize('s', [0, 0.1, 1e-3, 1e-5]) + def test_array_not_list(self, s): + # the argument of splPrep is either a list of arrays or a 2D array (sigh) + _, y, _ = self._get_xyk() + assert isinstance(y, list) + assert np.shape(y)[0] == 2 + + # assert the behavior of FITPACK's splrep + tck, u = splprep(y, s=s) + tck_a, u_a = splprep(np.asarray(y), s=s) + xp_assert_close(u, u_a, atol=s) + xp_assert_close(tck[0], tck_a[0], atol=1e-15) + assert len(tck[1]) == len(tck_a[1]) + for c1, c2 in zip(tck[1], tck_a[1]): + xp_assert_close(c1, c2, atol=1e-15) + assert tck[2] == tck_a[2] + assert np.shape(splev(u, tck)) == np.shape(y) + + spl, u = make_splprep(y, s=s) + xp_assert_close(u, u_a, atol=1e-15) + xp_assert_close(spl.t, tck_a[0], atol=1e-15) + xp_assert_close(spl.c.T, tck_a[1], atol=1e-15) + assert spl.k == tck_a[2] + assert spl(u).shape == np.shape(y) + + spl, u = make_splprep(np.asarray(y), s=s) + xp_assert_close(u, u_a, atol=1e-15) + xp_assert_close(spl.t, tck_a[0], atol=1e-15) + xp_assert_close(spl.c.T, tck_a[1], atol=1e-15) + assert spl.k == tck_a[2] + assert spl(u).shape == np.shape(y) + + with assert_raises(ValueError): + make_splprep(np.asarray(y).T, s=s) + + def test_default_s_is_zero(self): + x, y, k = self._get_xyk(m=10) + + spl, u = make_splprep(y) + xp_assert_close(spl(u), y, atol=1e-15) + + def test_s_zero_vs_near_zero(self): + # s=0 and s \approx 0 are consistent + x, y, k = self._get_xyk(m=10) + + spl_i, u_i = make_splprep(y, s=0) + spl_n, u_n = make_splprep(y, s=1e-15) + + xp_assert_close(u_i, u_n, atol=1e-15) + xp_assert_close(spl_i(u_i), y, atol=1e-15) + xp_assert_close(spl_n(u_n), y, atol=1e-7) + assert spl_i.axis == spl_n.axis + assert spl_i.c.shape == spl_n.c.shape + + def test_1D(self): + x = np.arange(8, dtype=float) + with assert_raises(ValueError): + splprep(x) + + with assert_raises(ValueError): + make_splprep(x, s=0) + + with assert_raises(ValueError): + make_splprep(x, s=0.1) + + tck, u_ = splprep([x], s=1e-5) + spl, u = make_splprep([x], s=1e-5) + + assert spl(u).shape == (1, 8) + xp_assert_close(spl(u), [x], atol=1e-15) + diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_fitpack.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_fitpack.py new file mode 100644 index 0000000000000000000000000000000000000000..d798f0eda4eb0c099bdf46cb4b3468628013c9d3 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_fitpack.py @@ -0,0 +1,519 @@ +import itertools +import os + +import numpy as np +from scipy._lib._array_api import ( + xp_assert_equal, xp_assert_close, assert_almost_equal, assert_array_almost_equal +) +from pytest import raises as assert_raises +import pytest +from scipy._lib._testutils import check_free_memory + +from scipy.interpolate import RectBivariateSpline +from scipy.interpolate import make_splrep + +from scipy.interpolate._fitpack_py import (splrep, splev, bisplrep, bisplev, + sproot, splprep, splint, spalde, splder, splantider, insert, dblint) +from scipy.interpolate._dfitpack import regrid_smth +from scipy.interpolate._fitpack2 import dfitpack_int + + +def data_file(basename): + return os.path.join(os.path.abspath(os.path.dirname(__file__)), + 'data', basename) + + +def norm2(x): + return np.sqrt(np.dot(x.T, x)) + + +def f1(x, d=0): + """Derivatives of sin->cos->-sin->-cos.""" + if d % 4 == 0: + return np.sin(x) + if d % 4 == 1: + return np.cos(x) + if d % 4 == 2: + return -np.sin(x) + if d % 4 == 3: + return -np.cos(x) + + +def makepairs(x, y): + """Helper function to create an array of pairs of x and y.""" + xy = np.array(list(itertools.product(np.asarray(x), np.asarray(y)))) + return xy.T + + +class TestSmokeTests: + """ + Smoke tests (with a few asserts) for fitpack routines -- mostly + check that they are runnable + """ + def check_1(self, per=0, s=0, a=0, b=2*np.pi, at_nodes=False, + xb=None, xe=None): + if xb is None: + xb = a + if xe is None: + xe = b + + N = 20 + # nodes and middle points of the nodes + x = np.linspace(a, b, N + 1) + x1 = a + (b - a) * np.arange(1, N, dtype=float) / float(N - 1) + v = f1(x) + + def err_est(k, d): + # Assume f has all derivatives < 1 + h = 1.0 / N + tol = 5 * h**(.75*(k-d)) + if s > 0: + tol += 1e5*s + return tol + + for k in range(1, 6): + tck = splrep(x, v, s=s, per=per, k=k, xe=xe) + tt = tck[0][k:-k] if at_nodes else x1 + + for d in range(k+1): + tol = err_est(k, d) + err = norm2(f1(tt, d) - splev(tt, tck, d)) / norm2(f1(tt, d)) + assert err < tol + + # smoke test make_splrep + if not per: + spl = make_splrep(x, v, k=k, s=s, xb=xb, xe=xe) + if len(spl.t) == len(tck[0]): + xp_assert_close(spl.t, tck[0], atol=1e-15) + xp_assert_close(spl.c, tck[1][:spl.c.size], atol=1e-13) + else: + assert k == 5 # knot length differ in some k=5 cases + + def check_2(self, per=0, N=20, ia=0, ib=2*np.pi): + a, b, dx = 0, 2*np.pi, 0.2*np.pi + x = np.linspace(a, b, N+1) # nodes + v = np.sin(x) + + def err_est(k, d): + # Assume f has all derivatives < 1 + h = 1.0 / N + tol = 5 * h**(.75*(k-d)) + return tol + + nk = [] + for k in range(1, 6): + tck = splrep(x, v, s=0, per=per, k=k, xe=b) + nk.append([splint(ia, ib, tck), spalde(dx, tck)]) + + k = 1 + for r in nk: + d = 0 + for dr in r[1]: + tol = err_est(k, d) + xp_assert_close(dr, f1(dx, d), atol=0, rtol=tol) + d = d+1 + k = k+1 + + def test_smoke_splrep_splev(self): + self.check_1(s=1e-6) + self.check_1(b=1.5*np.pi) + self.check_1(b=1.5*np.pi, xe=2*np.pi, per=1, s=1e-1) + + @pytest.mark.parametrize('per', [0, 1]) + @pytest.mark.parametrize('at_nodes', [True, False]) + def test_smoke_splrep_splev_2(self, per, at_nodes): + self.check_1(per=per, at_nodes=at_nodes) + + @pytest.mark.parametrize('N', [20, 50]) + @pytest.mark.parametrize('per', [0, 1]) + def test_smoke_splint_spalde(self, N, per): + self.check_2(per=per, N=N) + + @pytest.mark.parametrize('N', [20, 50]) + @pytest.mark.parametrize('per', [0, 1]) + def test_smoke_splint_spalde_iaib(self, N, per): + self.check_2(ia=0.2*np.pi, ib=np.pi, N=N, per=per) + + def test_smoke_sproot(self): + # sproot is only implemented for k=3 + a, b = 0.1, 15 + x = np.linspace(a, b, 20) + v = np.sin(x) + + for k in [1, 2, 4, 5]: + tck = splrep(x, v, s=0, per=0, k=k, xe=b) + with assert_raises(ValueError): + sproot(tck) + + k = 3 + tck = splrep(x, v, s=0, k=3) + roots = sproot(tck) + xp_assert_close(splev(roots, tck), np.zeros(len(roots)), atol=1e-10, rtol=1e-10) + xp_assert_close(roots, np.pi * np.array([1, 2, 3, 4]), rtol=1e-3) + + @pytest.mark.parametrize('N', [20, 50]) + @pytest.mark.parametrize('k', [1, 2, 3, 4, 5]) + def test_smoke_splprep_splrep_splev(self, N, k): + a, b, dx = 0, 2.*np.pi, 0.2*np.pi + x = np.linspace(a, b, N+1) # nodes + v = np.sin(x) + + tckp, u = splprep([x, v], s=0, per=0, k=k, nest=-1) + uv = splev(dx, tckp) + err1 = abs(uv[1] - np.sin(uv[0])) + assert err1 < 1e-2 + + tck = splrep(x, v, s=0, per=0, k=k) + err2 = abs(splev(uv[0], tck) - np.sin(uv[0])) + assert err2 < 1e-2 + + # Derivatives of parametric cubic spline at u (first function) + if k == 3: + tckp, u = splprep([x, v], s=0, per=0, k=k, nest=-1) + for d in range(1, k+1): + uv = splev(dx, tckp, d) + + def test_smoke_bisplrep_bisplev(self): + xb, xe = 0, 2.*np.pi + yb, ye = 0, 2.*np.pi + kx, ky = 3, 3 + Nx, Ny = 20, 20 + + def f2(x, y): + return np.sin(x+y) + + x = np.linspace(xb, xe, Nx + 1) + y = np.linspace(yb, ye, Ny + 1) + xy = makepairs(x, y) + tck = bisplrep(xy[0], xy[1], f2(xy[0], xy[1]), s=0, kx=kx, ky=ky) + + tt = [tck[0][kx:-kx], tck[1][ky:-ky]] + t2 = makepairs(tt[0], tt[1]) + v1 = bisplev(tt[0], tt[1], tck) + v2 = f2(t2[0], t2[1]) + v2.shape = len(tt[0]), len(tt[1]) + + assert norm2(np.ravel(v1 - v2)) < 1e-2 + + +class TestSplev: + def test_1d_shape(self): + x = [1,2,3,4,5] + y = [4,5,6,7,8] + tck = splrep(x, y) + z = splev([1], tck) + assert z.shape == (1,) + z = splev(1, tck) + assert z.shape == () + + def test_2d_shape(self): + x = [1, 2, 3, 4, 5] + y = [4, 5, 6, 7, 8] + tck = splrep(x, y) + t = np.array([[1.0, 1.5, 2.0, 2.5], + [3.0, 3.5, 4.0, 4.5]]) + z = splev(t, tck) + z0 = splev(t[0], tck) + z1 = splev(t[1], tck) + xp_assert_equal(z, np.vstack((z0, z1))) + + def test_extrapolation_modes(self): + # test extrapolation modes + # * if ext=0, return the extrapolated value. + # * if ext=1, return 0 + # * if ext=2, raise a ValueError + # * if ext=3, return the boundary value. + x = [1,2,3] + y = [0,2,4] + tck = splrep(x, y, k=1) + + rstl = [[-2, 6], [0, 0], None, [0, 4]] + for ext in (0, 1, 3): + assert_array_almost_equal(splev([0, 4], tck, ext=ext), rstl[ext]) + + assert_raises(ValueError, splev, [0, 4], tck, ext=2) + + +class TestSplder: + def setup_method(self): + # non-uniform grid, just to make it sure + x = np.linspace(0, 1, 100)**3 + y = np.sin(20 * x) + self.spl = splrep(x, y) + + # double check that knots are non-uniform + assert np.ptp(np.diff(self.spl[0])) > 0 + + def test_inverse(self): + # Check that antiderivative + derivative is identity. + for n in range(5): + spl2 = splantider(self.spl, n) + spl3 = splder(spl2, n) + xp_assert_close(self.spl[0], spl3[0]) + xp_assert_close(self.spl[1], spl3[1]) + assert self.spl[2] == spl3[2] + + def test_splder_vs_splev(self): + # Check derivative vs. FITPACK + + for n in range(3+1): + # Also extrapolation! + xx = np.linspace(-1, 2, 2000) + if n == 3: + # ... except that FITPACK extrapolates strangely for + # order 0, so let's not check that. + xx = xx[(xx >= 0) & (xx <= 1)] + + dy = splev(xx, self.spl, n) + spl2 = splder(self.spl, n) + dy2 = splev(xx, spl2) + if n == 1: + xp_assert_close(dy, dy2, rtol=2e-6) + else: + xp_assert_close(dy, dy2) + + def test_splantider_vs_splint(self): + # Check antiderivative vs. FITPACK + spl2 = splantider(self.spl) + + # no extrapolation, splint assumes function is zero outside + # range + xx = np.linspace(0, 1, 20) + + for x1 in xx: + for x2 in xx: + y1 = splint(x1, x2, self.spl) + y2 = splev(x2, spl2) - splev(x1, spl2) + xp_assert_close(np.asarray(y1), np.asarray(y2)) + + def test_order0_diff(self): + assert_raises(ValueError, splder, self.spl, 4) + + def test_kink(self): + # Should refuse to differentiate splines with kinks + + spl2 = insert(0.5, self.spl, m=2) + splder(spl2, 2) # Should work + assert_raises(ValueError, splder, spl2, 3) + + spl2 = insert(0.5, self.spl, m=3) + splder(spl2, 1) # Should work + assert_raises(ValueError, splder, spl2, 2) + + spl2 = insert(0.5, self.spl, m=4) + assert_raises(ValueError, splder, spl2, 1) + + def test_multidim(self): + # c can have trailing dims + for n in range(3): + t, c, k = self.spl + c2 = np.c_[c, c, c] + c2 = np.dstack((c2, c2)) + + spl2 = splantider((t, c2, k), n) + spl3 = splder(spl2, n) + + xp_assert_close(t, spl3[0]) + xp_assert_close(c2, spl3[1]) + assert k == spl3[2] + + +class TestSplint: + def test_len_c(self): + n, k = 7, 3 + x = np.arange(n) + y = x**3 + t, c, k = splrep(x, y, s=0) + + # note that len(c) == len(t) == 11 (== len(x) + 2*(k-1)) + assert len(t) == len(c) == n + 2*(k-1) + + # integrate directly: $\int_0^6 x^3 dx = 6^4 / 4$ + res = splint(0, 6, (t, c, k)) + expected = 6**4 / 4 + assert abs(res - expected) < 1e-13 + + # check that the coefficients past len(t) - k - 1 are ignored + c0 = c.copy() + c0[len(t) - k - 1:] = np.nan + res0 = splint(0, 6, (t, c0, k)) + assert abs(res0 - expected) < 1e-13 + + # however, all other coefficients *are* used + c0[6] = np.nan + assert np.isnan(splint(0, 6, (t, c0, k))) + + # check that the coefficient array can have length `len(t) - k - 1` + c1 = c[:len(t) - k - 1] + res1 = splint(0, 6, (t, c1, k)) + assert (res1 - expected) < 1e-13 + + + # however shorter c arrays raise. The error from f2py is a + # `dftipack.error`, which is an Exception but not ValueError etc. + with assert_raises(Exception, match=r">=n-k-1"): + splint(0, 1, (np.ones(10), np.ones(5), 3)) + + +class TestBisplrep: + def test_overflow(self): + from numpy.lib.stride_tricks import as_strided + if dfitpack_int.itemsize == 8: + size = 1500000**2 + else: + size = 400**2 + # Don't allocate a real array, as it's very big, but rely + # on that it's not referenced + x = as_strided(np.zeros(()), shape=(size,)) + assert_raises(OverflowError, bisplrep, x, x, x, w=x, + xb=0, xe=1, yb=0, ye=1, s=0) + + def test_regression_1310(self): + # Regression test for gh-1310 + with np.load(data_file('bug-1310.npz')) as loaded_data: + data = loaded_data['data'] + + # Shouldn't crash -- the input data triggers work array sizes + # that caused previously some data to not be aligned on + # sizeof(double) boundaries in memory, which made the Fortran + # code to crash when compiled with -O3 + bisplrep(data[:,0], data[:,1], data[:,2], kx=3, ky=3, s=0, + full_output=True) + + @pytest.mark.skipif(dfitpack_int != np.int64, reason="needs ilp64 fitpack") + def test_ilp64_bisplrep(self): + check_free_memory(28000) # VM size, doesn't actually use the pages + x = np.linspace(0, 1, 400) + y = np.linspace(0, 1, 400) + x, y = np.meshgrid(x, y) + z = np.zeros_like(x) + tck = bisplrep(x, y, z, kx=3, ky=3, s=0) + xp_assert_close(bisplev(0.5, 0.5, tck), 0.0) + + +def test_dblint(): + # Basic test to see it runs and gives the correct result on a trivial + # problem. Note that `dblint` is not exposed in the interpolate namespace. + x = np.linspace(0, 1) + y = np.linspace(0, 1) + xx, yy = np.meshgrid(x, y) + rect = RectBivariateSpline(x, y, 4 * xx * yy) + tck = list(rect.tck) + tck.extend(rect.degrees) + + assert abs(dblint(0, 1, 0, 1, tck) - 1) < 1e-10 + assert abs(dblint(0, 0.5, 0, 1, tck) - 0.25) < 1e-10 + assert abs(dblint(0.5, 1, 0, 1, tck) - 0.75) < 1e-10 + assert abs(dblint(-100, 100, -100, 100, tck) - 1) < 1e-10 + + +def test_splev_der_k(): + # regression test for gh-2188: splev(x, tck, der=k) gives garbage or crashes + # for x outside of knot range + + # test case from gh-2188 + tck = (np.array([0., 0., 2.5, 2.5]), + np.array([-1.56679978, 2.43995873, 0., 0.]), + 1) + t, c, k = tck + x = np.array([-3, 0, 2.5, 3]) + + # an explicit form of the linear spline + xp_assert_close(splev(x, tck), c[0] + (c[1] - c[0]) * x/t[2]) + xp_assert_close(splev(x, tck, 1), + np.ones_like(x) * (c[1] - c[0]) / t[2] + ) + + # now check a random spline vs splder + np.random.seed(1234) + x = np.sort(np.random.random(30)) + y = np.random.random(30) + t, c, k = splrep(x, y) + + x = [t[0] - 1., t[-1] + 1.] + tck2 = splder((t, c, k), k) + xp_assert_close(splev(x, (t, c, k), k), splev(x, tck2)) + + +def test_splprep_segfault(): + # regression test for gh-3847: splprep segfaults if knots are specified + # for task=-1 + t = np.arange(0, 1.1, 0.1) + x = np.sin(2*np.pi*t) + y = np.cos(2*np.pi*t) + tck, u = splprep([x, y], s=0) + np.arange(0, 1.01, 0.01) + + uknots = tck[0] # using the knots from the previous fitting + tck, u = splprep([x, y], task=-1, t=uknots) # here is the crash + + +def test_bisplev_integer_overflow(): + np.random.seed(1) + + x = np.linspace(0, 1, 11) + y = x + z = np.random.randn(11, 11).ravel() + kx = 1 + ky = 1 + + nx, tx, ny, ty, c, fp, ier = regrid_smth( + x, y, z, None, None, None, None, kx=kx, ky=ky, s=0.0) + tck = (tx[:nx], ty[:ny], c[:(nx - kx - 1) * (ny - ky - 1)], kx, ky) + + xp = np.zeros([2621440]) + yp = np.zeros([2621440]) + + assert_raises((RuntimeError, MemoryError), bisplev, xp, yp, tck) + + +@pytest.mark.xslow +def test_gh_1766(): + # this should fail gracefully instead of segfaulting (int overflow) + size = 22 + kx, ky = 3, 3 + def f2(x, y): + return np.sin(x+y) + + x = np.linspace(0, 10, size) + y = np.linspace(50, 700, size) + xy = makepairs(x, y) + tck = bisplrep(xy[0], xy[1], f2(xy[0], xy[1]), s=0, kx=kx, ky=ky) + # the size value here can either segfault + # or produce a MemoryError on main + tx_ty_size = 500000 + tck[0] = np.arange(tx_ty_size) + tck[1] = np.arange(tx_ty_size) * 4 + tt_0 = np.arange(50) + tt_1 = np.arange(50) * 3 + with pytest.raises(MemoryError): + bisplev(tt_0, tt_1, tck, 1, 1) + + +def test_spalde_scalar_input(): + # Ticket #629 + x = np.linspace(0, 10) + y = x**3 + tck = splrep(x, y, k=3, t=[5]) + res = spalde(np.float64(1), tck) + des = np.array([1., 3., 6., 6.]) + assert_almost_equal(res, des) + + +def test_spalde_nc(): + # regression test for https://github.com/scipy/scipy/issues/19002 + # here len(t) = 29 and len(c) = 25 (== len(t) - k - 1) + x = np.asarray([-10., -9., -8., -7., -6., -5., -4., -3., -2.5, -2., -1.5, + -1., -0.5, 0., 0.5, 1., 1.5, 2., 2.5, 3., 4., 5., 6.], + dtype="float") + t = [-10.0, -10.0, -10.0, -10.0, -9.0, -8.0, -7.0, -6.0, -5.0, -4.0, -3.0, + -2.5, -2.0, -1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 4.0, + 5.0, 6.0, 6.0, 6.0, 6.0] + c = np.asarray([1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., + 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]) + k = 3 + + res = spalde(x, (t, c, k)) + res = np.vstack(res) + res_splev = np.asarray([splev(x, (t, c, k), nu) for nu in range(4)]) + xp_assert_close(res, res_splev.T, atol=1e-15) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_fitpack2.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_fitpack2.py new file mode 100644 index 0000000000000000000000000000000000000000..044ace830bc6af26ee12edbcc6488e46bdfbcce6 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_fitpack2.py @@ -0,0 +1,1393 @@ +# Created by Pearu Peterson, June 2003 +import itertools +from threading import Lock +import numpy as np +from numpy.testing import suppress_warnings +import pytest +from pytest import raises as assert_raises +from scipy._lib._array_api import ( + xp_assert_equal, xp_assert_close, assert_almost_equal, assert_array_almost_equal +) + +from numpy import array, diff, linspace, meshgrid, ones, pi, shape +from scipy.interpolate._fitpack_py import bisplrep, bisplev, splrep, spalde +from scipy.interpolate._fitpack2 import (UnivariateSpline, + LSQUnivariateSpline, InterpolatedUnivariateSpline, + LSQBivariateSpline, SmoothBivariateSpline, RectBivariateSpline, + LSQSphereBivariateSpline, SmoothSphereBivariateSpline, + RectSphereBivariateSpline) + +from scipy._lib._testutils import _run_concurrent_barrier + +from scipy.interpolate import make_splrep + +class TestUnivariateSpline: + def test_linear_constant(self): + x = [1,2,3] + y = [3,3,3] + lut = UnivariateSpline(x,y,k=1) + assert_array_almost_equal(lut.get_knots(), [1, 3]) + assert_array_almost_equal(lut.get_coeffs(), [3, 3]) + assert abs(lut.get_residual()) < 1e-10 + assert_array_almost_equal(lut([1, 1.5, 2]), [3, 3, 3]) + + spl = make_splrep(x, y, k=1, s=len(x)) + xp_assert_close(spl.t[1:-1], lut.get_knots(), atol=1e-15) + xp_assert_close(spl.c, lut.get_coeffs(), atol=1e-15) + + def test_preserve_shape(self): + x = [1, 2, 3] + y = [0, 2, 4] + lut = UnivariateSpline(x, y, k=1) + arg = 2 + assert shape(arg) == shape(lut(arg)) + assert shape(arg) == shape(lut(arg, nu=1)) + arg = [1.5, 2, 2.5] + assert shape(arg) == shape(lut(arg)) + assert shape(arg) == shape(lut(arg, nu=1)) + + def test_linear_1d(self): + x = [1,2,3] + y = [0,2,4] + lut = UnivariateSpline(x,y,k=1) + assert_array_almost_equal(lut.get_knots(),[1,3]) + assert_array_almost_equal(lut.get_coeffs(),[0,4]) + assert abs(lut.get_residual()) < 1e-15 + assert_array_almost_equal(lut([1,1.5,2]),[0,1,2]) + + def test_subclassing(self): + # See #731 + + class ZeroSpline(UnivariateSpline): + def __call__(self, x): + return 0*array(x) + + sp = ZeroSpline([1,2,3,4,5], [3,2,3,2,3], k=2) + xp_assert_equal(sp([1.5, 2.5]), [0., 0.]) + + def test_empty_input(self): + # Test whether empty input returns an empty output. Ticket 1014 + x = [1,3,5,7,9] + y = [0,4,9,12,21] + spl = UnivariateSpline(x, y, k=3) + xp_assert_equal(spl([]), array([])) + + def test_roots(self): + x = [1, 3, 5, 7, 9] + y = [0, 4, 9, 12, 21] + spl = UnivariateSpline(x, y, k=3) + assert_almost_equal(spl.roots()[0], 1.050290639101332) + + def test_roots_length(self): # for gh18335 + x = np.linspace(0, 50 * np.pi, 1000) + y = np.cos(x) + spl = UnivariateSpline(x, y, s=0) + assert len(spl.roots()) == 50 + + def test_derivatives(self): + x = [1, 3, 5, 7, 9] + y = [0, 4, 9, 12, 21] + spl = UnivariateSpline(x, y, k=3) + assert_almost_equal(spl.derivatives(3.5), + [5.5152902, 1.7146577, -0.1830357, 0.3125]) + + def test_derivatives_2(self): + x = np.arange(8) + y = x**3 + 2.*x**2 + + tck = splrep(x, y, s=0) + ders = spalde(3, tck) + xp_assert_close(ders, [45., # 3**3 + 2*(3)**2 + 39., # 3*(3)**2 + 4*(3) + 22., # 6*(3) + 4 + 6.], # 6*3**0 + atol=1e-15) + spl = UnivariateSpline(x, y, s=0, k=3) + xp_assert_close(spl.derivatives(3), + ders, + atol=1e-15) + + def test_resize_regression(self): + """Regression test for #1375.""" + x = [-1., -0.65016502, -0.58856235, -0.26903553, -0.17370892, + -0.10011001, 0., 0.10011001, 0.17370892, 0.26903553, 0.58856235, + 0.65016502, 1.] + y = [1.,0.62928599, 0.5797223, 0.39965815, 0.36322694, 0.3508061, + 0.35214793, 0.3508061, 0.36322694, 0.39965815, 0.5797223, + 0.62928599, 1.] + w = [1.00000000e+12, 6.88875973e+02, 4.89314737e+02, 4.26864807e+02, + 6.07746770e+02, 4.51341444e+02, 3.17480210e+02, 4.51341444e+02, + 6.07746770e+02, 4.26864807e+02, 4.89314737e+02, 6.88875973e+02, + 1.00000000e+12] + spl = UnivariateSpline(x=x, y=y, w=w, s=None) + desired = array([0.35100374, 0.51715855, 0.87789547, 0.98719344]) + xp_assert_close(spl([0.1, 0.5, 0.9, 0.99]), desired, atol=5e-4) + + def test_out_of_range_regression(self): + # Test different extrapolation modes. See ticket 3557 + x = np.arange(5, dtype=float) + y = x**3 + + xp = linspace(-8, 13, 100) + xp_zeros = xp.copy() + xp_zeros[np.logical_or(xp_zeros < 0., xp_zeros > 4.)] = 0 + xp_clip = xp.copy() + xp_clip[xp_clip < x[0]] = x[0] + xp_clip[xp_clip > x[-1]] = x[-1] + + for cls in [UnivariateSpline, InterpolatedUnivariateSpline]: + spl = cls(x=x, y=y) + for ext in [0, 'extrapolate']: + xp_assert_close(spl(xp, ext=ext), xp**3, atol=1e-16) + xp_assert_close(cls(x, y, ext=ext)(xp), xp**3, atol=1e-16) + for ext in [1, 'zeros']: + xp_assert_close(spl(xp, ext=ext), xp_zeros**3, atol=1e-16) + xp_assert_close(cls(x, y, ext=ext)(xp), xp_zeros**3, atol=1e-16) + for ext in [2, 'raise']: + assert_raises(ValueError, spl, xp, **dict(ext=ext)) + for ext in [3, 'const']: + xp_assert_close(spl(xp, ext=ext), xp_clip**3, atol=2e-16) + xp_assert_close(cls(x, y, ext=ext)(xp), xp_clip**3, atol=2e-16) + + # also test LSQUnivariateSpline [which needs explicit knots] + t = spl.get_knots()[3:4] # interior knots w/ default k=3 + spl = LSQUnivariateSpline(x, y, t) + xp_assert_close(spl(xp, ext=0), xp**3, atol=1e-16) + xp_assert_close(spl(xp, ext=1), xp_zeros**3, atol=1e-16) + assert_raises(ValueError, spl, xp, **dict(ext=2)) + xp_assert_close(spl(xp, ext=3), xp_clip**3, atol=1e-16) + + # also make sure that unknown values for `ext` are caught early + for ext in [-1, 'unknown']: + spl = UnivariateSpline(x, y) + assert_raises(ValueError, spl, xp, **dict(ext=ext)) + assert_raises(ValueError, UnivariateSpline, + **dict(x=x, y=y, ext=ext)) + + def test_lsq_fpchec(self): + xs = np.arange(100) * 1. + ys = np.arange(100) * 1. + knots = np.linspace(0, 99, 10) + bbox = (-1, 101) + assert_raises(ValueError, LSQUnivariateSpline, xs, ys, knots, + bbox=bbox) + + def test_derivative_and_antiderivative(self): + # Thin wrappers to splder/splantider, so light smoke test only. + x = np.linspace(0, 1, 70)**3 + y = np.cos(x) + + spl = UnivariateSpline(x, y, s=0) + spl2 = spl.antiderivative(2).derivative(2) + xp_assert_close(spl(0.3), spl2(0.3)) + + spl2 = spl.antiderivative(1) + xp_assert_close(spl2(0.6) - spl2(0.2), + spl.integral(0.2, 0.6)) + + def test_derivative_extrapolation(self): + # Regression test for gh-10195: for a const-extrapolation spline + # its derivative evaluates to zero for extrapolation + x_values = [1, 2, 4, 6, 8.5] + y_values = [0.5, 0.8, 1.3, 2.5, 5] + f = UnivariateSpline(x_values, y_values, ext='const', k=3) + + x = [-1, 0, -0.5, 9, 9.5, 10] + xp_assert_close(f.derivative()(x), np.zeros_like(x), atol=1e-15) + + def test_integral_out_of_bounds(self): + # Regression test for gh-7906: .integral(a, b) is wrong if both + # a and b are out-of-bounds + x = np.linspace(0., 1., 7) + for ext in range(4): + f = UnivariateSpline(x, x, s=0, ext=ext) + for (a, b) in [(1, 1), (1, 5), (2, 5), + (0, 0), (-2, 0), (-2, -1)]: + assert abs(f.integral(a, b)) < 1e-15 + + def test_nan(self): + # bail out early if the input data contains nans + x = np.arange(10, dtype=float) + y = x**3 + w = np.ones_like(x) + # also test LSQUnivariateSpline [which needs explicit knots] + spl = UnivariateSpline(x, y, check_finite=True) + t = spl.get_knots()[3:4] # interior knots w/ default k=3 + y_end = y[-1] + for z in [np.nan, np.inf, -np.inf]: + y[-1] = z + assert_raises(ValueError, UnivariateSpline, + **dict(x=x, y=y, check_finite=True)) + assert_raises(ValueError, InterpolatedUnivariateSpline, + **dict(x=x, y=y, check_finite=True)) + assert_raises(ValueError, LSQUnivariateSpline, + **dict(x=x, y=y, t=t, check_finite=True)) + y[-1] = y_end # check valid y but invalid w + w[-1] = z + assert_raises(ValueError, UnivariateSpline, + **dict(x=x, y=y, w=w, check_finite=True)) + assert_raises(ValueError, InterpolatedUnivariateSpline, + **dict(x=x, y=y, w=w, check_finite=True)) + assert_raises(ValueError, LSQUnivariateSpline, + **dict(x=x, y=y, t=t, w=w, check_finite=True)) + + def test_strictly_increasing_x(self): + # Test the x is required to be strictly increasing for + # UnivariateSpline if s=0 and for InterpolatedUnivariateSpline, + # but merely increasing for UnivariateSpline if s>0 + # and for LSQUnivariateSpline; see gh-8535 + xx = np.arange(10, dtype=float) + yy = xx**3 + x = np.arange(10, dtype=float) + x[1] = x[0] + y = x**3 + w = np.ones_like(x) + # also test LSQUnivariateSpline [which needs explicit knots] + spl = UnivariateSpline(xx, yy, check_finite=True) + t = spl.get_knots()[3:4] # interior knots w/ default k=3 + UnivariateSpline(x=x, y=y, w=w, s=1, check_finite=True) + LSQUnivariateSpline(x=x, y=y, t=t, w=w, check_finite=True) + assert_raises(ValueError, UnivariateSpline, + **dict(x=x, y=y, s=0, check_finite=True)) + assert_raises(ValueError, InterpolatedUnivariateSpline, + **dict(x=x, y=y, check_finite=True)) + + def test_increasing_x(self): + # Test that x is required to be increasing, see gh-8535 + xx = np.arange(10, dtype=float) + yy = xx**3 + x = np.arange(10, dtype=float) + x[1] = x[0] - 1.0 + y = x**3 + w = np.ones_like(x) + # also test LSQUnivariateSpline [which needs explicit knots] + spl = UnivariateSpline(xx, yy, check_finite=True) + t = spl.get_knots()[3:4] # interior knots w/ default k=3 + assert_raises(ValueError, UnivariateSpline, + **dict(x=x, y=y, check_finite=True)) + assert_raises(ValueError, InterpolatedUnivariateSpline, + **dict(x=x, y=y, check_finite=True)) + assert_raises(ValueError, LSQUnivariateSpline, + **dict(x=x, y=y, t=t, w=w, check_finite=True)) + + def test_invalid_input_for_univariate_spline(self): + + with assert_raises(ValueError) as info: + x_values = [1, 2, 4, 6, 8.5] + y_values = [0.5, 0.8, 1.3, 2.5] + UnivariateSpline(x_values, y_values) + assert "x and y should have a same length" in str(info.value) + + with assert_raises(ValueError) as info: + x_values = [1, 2, 4, 6, 8.5] + y_values = [0.5, 0.8, 1.3, 2.5, 2.8] + w_values = [-1.0, 1.0, 1.0, 1.0] + UnivariateSpline(x_values, y_values, w=w_values) + assert "x, y, and w should have a same length" in str(info.value) + + with assert_raises(ValueError) as info: + bbox = (-1) + UnivariateSpline(x_values, y_values, bbox=bbox) + assert "bbox shape should be (2,)" in str(info.value) + + with assert_raises(ValueError) as info: + UnivariateSpline(x_values, y_values, k=6) + assert "k should be 1 <= k <= 5" in str(info.value) + + with assert_raises(ValueError) as info: + UnivariateSpline(x_values, y_values, s=-1.0) + assert "s should be s >= 0.0" in str(info.value) + + def test_invalid_input_for_interpolated_univariate_spline(self): + + with assert_raises(ValueError) as info: + x_values = [1, 2, 4, 6, 8.5] + y_values = [0.5, 0.8, 1.3, 2.5] + InterpolatedUnivariateSpline(x_values, y_values) + assert "x and y should have a same length" in str(info.value) + + with assert_raises(ValueError) as info: + x_values = [1, 2, 4, 6, 8.5] + y_values = [0.5, 0.8, 1.3, 2.5, 2.8] + w_values = [-1.0, 1.0, 1.0, 1.0] + InterpolatedUnivariateSpline(x_values, y_values, w=w_values) + assert "x, y, and w should have a same length" in str(info.value) + + with assert_raises(ValueError) as info: + bbox = (-1) + InterpolatedUnivariateSpline(x_values, y_values, bbox=bbox) + assert "bbox shape should be (2,)" in str(info.value) + + with assert_raises(ValueError) as info: + InterpolatedUnivariateSpline(x_values, y_values, k=6) + assert "k should be 1 <= k <= 5" in str(info.value) + + def test_invalid_input_for_lsq_univariate_spline(self): + + x_values = [1, 2, 4, 6, 8.5] + y_values = [0.5, 0.8, 1.3, 2.5, 2.8] + spl = UnivariateSpline(x_values, y_values, check_finite=True) + t_values = spl.get_knots()[3:4] # interior knots w/ default k=3 + + with assert_raises(ValueError) as info: + x_values = [1, 2, 4, 6, 8.5] + y_values = [0.5, 0.8, 1.3, 2.5] + LSQUnivariateSpline(x_values, y_values, t_values) + assert "x and y should have a same length" in str(info.value) + + with assert_raises(ValueError) as info: + x_values = [1, 2, 4, 6, 8.5] + y_values = [0.5, 0.8, 1.3, 2.5, 2.8] + w_values = [1.0, 1.0, 1.0, 1.0] + LSQUnivariateSpline(x_values, y_values, t_values, w=w_values) + assert "x, y, and w should have a same length" in str(info.value) + + message = "Interior knots t must satisfy Schoenberg-Whitney conditions" + with assert_raises(ValueError, match=message) as info: + bbox = (100, -100) + LSQUnivariateSpline(x_values, y_values, t_values, bbox=bbox) + + with assert_raises(ValueError) as info: + bbox = (-1) + LSQUnivariateSpline(x_values, y_values, t_values, bbox=bbox) + assert "bbox shape should be (2,)" in str(info.value) + + with assert_raises(ValueError) as info: + LSQUnivariateSpline(x_values, y_values, t_values, k=6) + assert "k should be 1 <= k <= 5" in str(info.value) + + def test_array_like_input(self): + x_values = np.array([1, 2, 4, 6, 8.5]) + y_values = np.array([0.5, 0.8, 1.3, 2.5, 2.8]) + w_values = np.array([1.0, 1.0, 1.0, 1.0, 1.0]) + bbox = np.array([-100, 100]) + # np.array input + spl1 = UnivariateSpline(x=x_values, y=y_values, w=w_values, + bbox=bbox) + # list input + spl2 = UnivariateSpline(x=x_values.tolist(), y=y_values.tolist(), + w=w_values.tolist(), bbox=bbox.tolist()) + + xp_assert_close(spl1([0.1, 0.5, 0.9, 0.99]), + spl2([0.1, 0.5, 0.9, 0.99])) + + @pytest.mark.thread_unsafe + def test_fpknot_oob_crash(self): + # https://github.com/scipy/scipy/issues/3691 + x = range(109) + y = [0., 0., 0., 0., 0., 10.9, 0., 11., 0., + 0., 0., 10.9, 0., 0., 0., 0., 0., 0., + 10.9, 0., 0., 0., 11., 0., 0., 0., 10.9, + 0., 0., 0., 10.5, 0., 0., 0., 10.7, 0., + 0., 0., 11., 0., 0., 0., 0., 0., 0., + 10.9, 0., 0., 10.7, 0., 0., 0., 10.6, 0., + 0., 0., 10.5, 0., 0., 10.7, 0., 0., 10.5, + 0., 0., 11.5, 0., 0., 0., 10.7, 0., 0., + 10.7, 0., 0., 10.9, 0., 0., 10.8, 0., 0., + 0., 10.7, 0., 0., 10.6, 0., 0., 0., 10.4, + 0., 0., 10.6, 0., 0., 10.5, 0., 0., 0., + 10.7, 0., 0., 0., 10.4, 0., 0., 0., 10.8, 0.] + with suppress_warnings() as sup: + r = sup.record( + UserWarning, + r""" +The maximal number of iterations maxit \(set to 20 by the program\) +allowed for finding a smoothing spline with fp=s has been reached: s +too small. +There is an approximation returned but the corresponding weighted sum +of squared residuals does not satisfy the condition abs\(fp-s\)/s < tol.""") + UnivariateSpline(x, y, k=1) + assert len(r) == 1 + + def test_concurrency(self): + # Check that no segfaults appear with concurrent access to + # UnivariateSpline + xx = np.arange(100, dtype=float) + yy = xx**3 + x = np.arange(100, dtype=float) + x[1] = x[0] + spl = UnivariateSpline(xx, yy, check_finite=True) + + def worker_fn(_, interp, x): + interp(x) + + _run_concurrent_barrier(10, worker_fn, spl, x) + + +class TestLSQBivariateSpline: + # NOTE: The systems in this test class are rank-deficient + @pytest.mark.thread_unsafe + def test_linear_constant(self): + x = [1,1,1,2,2,2,3,3,3] + y = [1,2,3,1,2,3,1,2,3] + z = [3,3,3,3,3,3,3,3,3] + s = 0.1 + tx = [1+s,3-s] + ty = [1+s,3-s] + with suppress_warnings() as sup: + r = sup.record(UserWarning, "\nThe coefficients of the spline") + lut = LSQBivariateSpline(x,y,z,tx,ty,kx=1,ky=1) + assert len(r) == 1 + + assert_almost_equal(lut(2, 2), np.asarray(3.)) + + def test_bilinearity(self): + x = [1,1,1,2,2,2,3,3,3] + y = [1,2,3,1,2,3,1,2,3] + z = [0,7,8,3,4,7,1,3,4] + s = 0.1 + tx = [1+s,3-s] + ty = [1+s,3-s] + with suppress_warnings() as sup: + # This seems to fail (ier=1, see ticket 1642). + sup.filter(UserWarning, "\nThe coefficients of the spline") + lut = LSQBivariateSpline(x,y,z,tx,ty,kx=1,ky=1) + + tx, ty = lut.get_knots() + for xa, xb in zip(tx[:-1], tx[1:]): + for ya, yb in zip(ty[:-1], ty[1:]): + for t in [0.1, 0.5, 0.9]: + for s in [0.3, 0.4, 0.7]: + xp = xa*(1-t) + xb*t + yp = ya*(1-s) + yb*s + zp = (+ lut(xa, ya)*(1-t)*(1-s) + + lut(xb, ya)*t*(1-s) + + lut(xa, yb)*(1-t)*s + + lut(xb, yb)*t*s) + assert_almost_equal(lut(xp,yp), zp) + + @pytest.mark.thread_unsafe + def test_integral(self): + x = [1,1,1,2,2,2,8,8,8] + y = [1,2,3,1,2,3,1,2,3] + z = array([0,7,8,3,4,7,1,3,4]) + + s = 0.1 + tx = [1+s,3-s] + ty = [1+s,3-s] + with suppress_warnings() as sup: + r = sup.record(UserWarning, "\nThe coefficients of the spline") + lut = LSQBivariateSpline(x, y, z, tx, ty, kx=1, ky=1) + assert len(r) == 1 + tx, ty = lut.get_knots() + tz = lut(tx, ty) + trpz = .25*(diff(tx)[:,None]*diff(ty)[None,:] + * (tz[:-1,:-1]+tz[1:,:-1]+tz[:-1,1:]+tz[1:,1:])).sum() + + assert_almost_equal(np.asarray(lut.integral(tx[0], tx[-1], ty[0], ty[-1])), + np.asarray(trpz)) + + @pytest.mark.thread_unsafe + def test_empty_input(self): + # Test whether empty inputs returns an empty output. Ticket 1014 + x = [1,1,1,2,2,2,3,3,3] + y = [1,2,3,1,2,3,1,2,3] + z = [3,3,3,3,3,3,3,3,3] + s = 0.1 + tx = [1+s,3-s] + ty = [1+s,3-s] + with suppress_warnings() as sup: + r = sup.record(UserWarning, "\nThe coefficients of the spline") + lut = LSQBivariateSpline(x, y, z, tx, ty, kx=1, ky=1) + assert len(r) == 1 + + xp_assert_equal(lut([], []), np.zeros((0,0))) + xp_assert_equal(lut([], [], grid=False), np.zeros((0,))) + + def test_invalid_input(self): + s = 0.1 + tx = [1 + s, 3 - s] + ty = [1 + s, 3 - s] + + with assert_raises(ValueError) as info: + x = np.linspace(1.0, 10.0) + y = np.linspace(1.0, 10.0) + z = np.linspace(1.0, 10.0, num=10) + LSQBivariateSpline(x, y, z, tx, ty) + assert "x, y, and z should have a same length" in str(info.value) + + with assert_raises(ValueError) as info: + x = np.linspace(1.0, 10.0) + y = np.linspace(1.0, 10.0) + z = np.linspace(1.0, 10.0) + w = np.linspace(1.0, 10.0, num=20) + LSQBivariateSpline(x, y, z, tx, ty, w=w) + assert "x, y, z, and w should have a same length" in str(info.value) + + with assert_raises(ValueError) as info: + w = np.linspace(-1.0, 10.0) + LSQBivariateSpline(x, y, z, tx, ty, w=w) + assert "w should be positive" in str(info.value) + + with assert_raises(ValueError) as info: + bbox = (-100, 100, -100) + LSQBivariateSpline(x, y, z, tx, ty, bbox=bbox) + assert "bbox shape should be (4,)" in str(info.value) + + with assert_raises(ValueError) as info: + LSQBivariateSpline(x, y, z, tx, ty, kx=10, ky=10) + assert "The length of x, y and z should be at least (kx+1) * (ky+1)" in \ + str(info.value) + + with assert_raises(ValueError) as exc_info: + LSQBivariateSpline(x, y, z, tx, ty, eps=0.0) + assert "eps should be between (0, 1)" in str(exc_info.value) + + with assert_raises(ValueError) as exc_info: + LSQBivariateSpline(x, y, z, tx, ty, eps=1.0) + assert "eps should be between (0, 1)" in str(exc_info.value) + + @pytest.mark.thread_unsafe + def test_array_like_input(self): + s = 0.1 + tx = np.array([1 + s, 3 - s]) + ty = np.array([1 + s, 3 - s]) + x = np.linspace(1.0, 10.0) + y = np.linspace(1.0, 10.0) + z = np.linspace(1.0, 10.0) + w = np.linspace(1.0, 10.0) + bbox = np.array([1.0, 10.0, 1.0, 10.0]) + + with suppress_warnings() as sup: + r = sup.record(UserWarning, "\nThe coefficients of the spline") + # np.array input + spl1 = LSQBivariateSpline(x, y, z, tx, ty, w=w, bbox=bbox) + # list input + spl2 = LSQBivariateSpline(x.tolist(), y.tolist(), z.tolist(), + tx.tolist(), ty.tolist(), w=w.tolist(), + bbox=bbox) + xp_assert_close(spl1(2.0, 2.0), spl2(2.0, 2.0)) + assert len(r) == 2 + + @pytest.mark.thread_unsafe + def test_unequal_length_of_knots(self): + """Test for the case when the input knot-location arrays in x and y are + of different lengths. + """ + x, y = np.mgrid[0:100, 0:100] + x = x.ravel() + y = y.ravel() + z = 3.0 * np.ones_like(x) + tx = np.linspace(0.1, 98.0, 29) + ty = np.linspace(0.1, 98.0, 33) + with suppress_warnings() as sup: + r = sup.record(UserWarning, "\nThe coefficients of the spline") + lut = LSQBivariateSpline(x,y,z,tx,ty) + assert len(r) == 1 + + assert_almost_equal(lut(x, y, grid=False), z) + + +class TestSmoothBivariateSpline: + def test_linear_constant(self): + x = [1,1,1,2,2,2,3,3,3] + y = [1,2,3,1,2,3,1,2,3] + z = [3,3,3,3,3,3,3,3,3] + lut = SmoothBivariateSpline(x,y,z,kx=1,ky=1) + for t in lut.get_knots(): + assert_array_almost_equal(t, [1, 1, 3, 3]) + + assert_array_almost_equal(lut.get_coeffs(), [3, 3, 3, 3]) + assert abs(lut.get_residual()) < 1e-15 + assert_array_almost_equal(lut([1, 1.5, 2], [1, 1.5]), [[3, 3], [3, 3], [3, 3]]) + + def test_linear_1d(self): + x = [1,1,1,2,2,2,3,3,3] + y = [1,2,3,1,2,3,1,2,3] + z = [0,0,0,2,2,2,4,4,4] + lut = SmoothBivariateSpline(x,y,z,kx=1,ky=1) + for t in lut.get_knots(): + xp_assert_close(t, np.asarray([1.0, 1, 3, 3])) + assert_array_almost_equal(lut.get_coeffs(), [0, 0, 4, 4]) + assert abs(lut.get_residual()) < 1e-15 + assert_array_almost_equal(lut([1,1.5,2],[1,1.5]),[[0,0],[1,1],[2,2]]) + + @pytest.mark.thread_unsafe + def test_integral(self): + x = [1,1,1,2,2,2,4,4,4] + y = [1,2,3,1,2,3,1,2,3] + z = array([0,7,8,3,4,7,1,3,4]) + + with suppress_warnings() as sup: + # This seems to fail (ier=1, see ticket 1642). + sup.filter(UserWarning, "\nThe required storage space") + lut = SmoothBivariateSpline(x, y, z, kx=1, ky=1, s=0) + + tx = [1,2,4] + ty = [1,2,3] + + tz = lut(tx, ty) + trpz = .25*(diff(tx)[:,None]*diff(ty)[None,:] + * (tz[:-1,:-1]+tz[1:,:-1]+tz[:-1,1:]+tz[1:,1:])).sum() + assert_almost_equal(np.asarray(lut.integral(tx[0], tx[-1], ty[0], ty[-1])), + np.asarray(trpz)) + + lut2 = SmoothBivariateSpline(x, y, z, kx=2, ky=2, s=0) + assert_almost_equal(np.asarray(lut2.integral(tx[0], tx[-1], ty[0], ty[-1])), + np.asarray(trpz), + decimal=0) # the quadratures give 23.75 and 23.85 + + tz = lut(tx[:-1], ty[:-1]) + trpz = .25*(diff(tx[:-1])[:,None]*diff(ty[:-1])[None,:] + * (tz[:-1,:-1]+tz[1:,:-1]+tz[:-1,1:]+tz[1:,1:])).sum() + assert_almost_equal(np.asarray(lut.integral(tx[0], tx[-2], ty[0], ty[-2])), + np.asarray(trpz)) + + def test_rerun_lwrk2_too_small(self): + # in this setting, lwrk2 is too small in the default run. Here we + # check for equality with the bisplrep/bisplev output because there, + # an automatic re-run of the spline representation is done if ier>10. + x = np.linspace(-2, 2, 80) + y = np.linspace(-2, 2, 80) + z = x + y + xi = np.linspace(-1, 1, 100) + yi = np.linspace(-2, 2, 100) + tck = bisplrep(x, y, z) + res1 = bisplev(xi, yi, tck) + interp_ = SmoothBivariateSpline(x, y, z) + res2 = interp_(xi, yi) + assert_almost_equal(res1, res2) + + def test_invalid_input(self): + + with assert_raises(ValueError) as info: + x = np.linspace(1.0, 10.0) + y = np.linspace(1.0, 10.0) + z = np.linspace(1.0, 10.0, num=10) + SmoothBivariateSpline(x, y, z) + assert "x, y, and z should have a same length" in str(info.value) + + with assert_raises(ValueError) as info: + x = np.linspace(1.0, 10.0) + y = np.linspace(1.0, 10.0) + z = np.linspace(1.0, 10.0) + w = np.linspace(1.0, 10.0, num=20) + SmoothBivariateSpline(x, y, z, w=w) + assert "x, y, z, and w should have a same length" in str(info.value) + + with assert_raises(ValueError) as info: + w = np.linspace(-1.0, 10.0) + SmoothBivariateSpline(x, y, z, w=w) + assert "w should be positive" in str(info.value) + + with assert_raises(ValueError) as info: + bbox = (-100, 100, -100) + SmoothBivariateSpline(x, y, z, bbox=bbox) + assert "bbox shape should be (4,)" in str(info.value) + + with assert_raises(ValueError) as info: + SmoothBivariateSpline(x, y, z, kx=10, ky=10) + assert "The length of x, y and z should be at least (kx+1) * (ky+1)" in\ + str(info.value) + + with assert_raises(ValueError) as info: + SmoothBivariateSpline(x, y, z, s=-1.0) + assert "s should be s >= 0.0" in str(info.value) + + with assert_raises(ValueError) as exc_info: + SmoothBivariateSpline(x, y, z, eps=0.0) + assert "eps should be between (0, 1)" in str(exc_info.value) + + with assert_raises(ValueError) as exc_info: + SmoothBivariateSpline(x, y, z, eps=1.0) + assert "eps should be between (0, 1)" in str(exc_info.value) + + def test_array_like_input(self): + x = np.array([1, 1, 1, 2, 2, 2, 3, 3, 3]) + y = np.array([1, 2, 3, 1, 2, 3, 1, 2, 3]) + z = np.array([3, 3, 3, 3, 3, 3, 3, 3, 3]) + w = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1]) + bbox = np.array([1.0, 3.0, 1.0, 3.0]) + # np.array input + spl1 = SmoothBivariateSpline(x, y, z, w=w, bbox=bbox, kx=1, ky=1) + # list input + spl2 = SmoothBivariateSpline(x.tolist(), y.tolist(), z.tolist(), + bbox=bbox.tolist(), w=w.tolist(), + kx=1, ky=1) + xp_assert_close(spl1(0.1, 0.5), spl2(0.1, 0.5)) + + +class TestLSQSphereBivariateSpline: + def setup_method(self): + # define the input data and coordinates + ntheta, nphi = 70, 90 + theta = linspace(0.5/(ntheta - 1), 1 - 0.5/(ntheta - 1), ntheta) * pi + phi = linspace(0.5/(nphi - 1), 1 - 0.5/(nphi - 1), nphi) * 2. * pi + data = ones((theta.shape[0], phi.shape[0])) + # define knots and extract data values at the knots + knotst = theta[::5] + knotsp = phi[::5] + knotdata = data[::5, ::5] + # calculate spline coefficients + lats, lons = meshgrid(theta, phi) + lut_lsq = LSQSphereBivariateSpline(lats.ravel(), lons.ravel(), + data.T.ravel(), knotst, knotsp) + self.lut_lsq = lut_lsq + self.data = knotdata + self.new_lons, self.new_lats = knotsp, knotst + + def test_linear_constant(self): + assert abs(self.lut_lsq.get_residual()) < 1e-15 + assert_array_almost_equal(self.lut_lsq(self.new_lats, self.new_lons), + self.data) + + def test_empty_input(self): + assert_array_almost_equal(self.lut_lsq([], []), np.zeros((0,0))) + assert_array_almost_equal(self.lut_lsq([], [], grid=False), np.zeros((0,))) + + def test_invalid_input(self): + ntheta, nphi = 70, 90 + theta = linspace(0.5 / (ntheta - 1), 1 - 0.5 / (ntheta - 1), + ntheta) * pi + phi = linspace(0.5 / (nphi - 1), 1 - 0.5 / (nphi - 1), nphi) * 2. * pi + data = ones((theta.shape[0], phi.shape[0])) + # define knots and extract data values at the knots + knotst = theta[::5] + knotsp = phi[::5] + + with assert_raises(ValueError) as exc_info: + invalid_theta = linspace(-0.1, 1.0, num=ntheta) * pi + invalid_lats, lons = meshgrid(invalid_theta, phi) + LSQSphereBivariateSpline(invalid_lats.ravel(), lons.ravel(), + data.T.ravel(), knotst, knotsp) + assert "theta should be between [0, pi]" in str(exc_info.value) + + with assert_raises(ValueError) as exc_info: + invalid_theta = linspace(0.1, 1.1, num=ntheta) * pi + invalid_lats, lons = meshgrid(invalid_theta, phi) + LSQSphereBivariateSpline(invalid_lats.ravel(), lons.ravel(), + data.T.ravel(), knotst, knotsp) + assert "theta should be between [0, pi]" in str(exc_info.value) + + with assert_raises(ValueError) as exc_info: + invalid_phi = linspace(-0.1, 1.0, num=ntheta) * 2.0 * pi + lats, invalid_lons = meshgrid(theta, invalid_phi) + LSQSphereBivariateSpline(lats.ravel(), invalid_lons.ravel(), + data.T.ravel(), knotst, knotsp) + assert "phi should be between [0, 2pi]" in str(exc_info.value) + + with assert_raises(ValueError) as exc_info: + invalid_phi = linspace(0.0, 1.1, num=ntheta) * 2.0 * pi + lats, invalid_lons = meshgrid(theta, invalid_phi) + LSQSphereBivariateSpline(lats.ravel(), invalid_lons.ravel(), + data.T.ravel(), knotst, knotsp) + assert "phi should be between [0, 2pi]" in str(exc_info.value) + + lats, lons = meshgrid(theta, phi) + + with assert_raises(ValueError) as exc_info: + invalid_knotst = np.copy(knotst) + invalid_knotst[0] = -0.1 + LSQSphereBivariateSpline(lats.ravel(), lons.ravel(), + data.T.ravel(), invalid_knotst, knotsp) + assert "tt should be between (0, pi)" in str(exc_info.value) + + with assert_raises(ValueError) as exc_info: + invalid_knotst = np.copy(knotst) + invalid_knotst[0] = pi + LSQSphereBivariateSpline(lats.ravel(), lons.ravel(), + data.T.ravel(), invalid_knotst, knotsp) + assert "tt should be between (0, pi)" in str(exc_info.value) + + with assert_raises(ValueError) as exc_info: + invalid_knotsp = np.copy(knotsp) + invalid_knotsp[0] = -0.1 + LSQSphereBivariateSpline(lats.ravel(), lons.ravel(), + data.T.ravel(), knotst, invalid_knotsp) + assert "tp should be between (0, 2pi)" in str(exc_info.value) + + with assert_raises(ValueError) as exc_info: + invalid_knotsp = np.copy(knotsp) + invalid_knotsp[0] = 2 * pi + LSQSphereBivariateSpline(lats.ravel(), lons.ravel(), + data.T.ravel(), knotst, invalid_knotsp) + assert "tp should be between (0, 2pi)" in str(exc_info.value) + + with assert_raises(ValueError) as exc_info: + invalid_w = array([-1.0, 1.0, 1.5, 0.5, 1.0, 1.5, 0.5, 1.0, 1.0]) + LSQSphereBivariateSpline(lats.ravel(), lons.ravel(), data.T.ravel(), + knotst, knotsp, w=invalid_w) + assert "w should be positive" in str(exc_info.value) + + with assert_raises(ValueError) as exc_info: + LSQSphereBivariateSpline(lats.ravel(), lons.ravel(), data.T.ravel(), + knotst, knotsp, eps=0.0) + assert "eps should be between (0, 1)" in str(exc_info.value) + + with assert_raises(ValueError) as exc_info: + LSQSphereBivariateSpline(lats.ravel(), lons.ravel(), data.T.ravel(), + knotst, knotsp, eps=1.0) + assert "eps should be between (0, 1)" in str(exc_info.value) + + def test_array_like_input(self): + ntheta, nphi = 70, 90 + theta = linspace(0.5 / (ntheta - 1), 1 - 0.5 / (ntheta - 1), + ntheta) * pi + phi = linspace(0.5 / (nphi - 1), 1 - 0.5 / (nphi - 1), + nphi) * 2. * pi + lats, lons = meshgrid(theta, phi) + data = ones((theta.shape[0], phi.shape[0])) + # define knots and extract data values at the knots + knotst = theta[::5] + knotsp = phi[::5] + w = ones(lats.ravel().shape[0]) + + # np.array input + spl1 = LSQSphereBivariateSpline(lats.ravel(), lons.ravel(), + data.T.ravel(), knotst, knotsp, w=w) + # list input + spl2 = LSQSphereBivariateSpline(lats.ravel().tolist(), + lons.ravel().tolist(), + data.T.ravel().tolist(), + knotst.tolist(), + knotsp.tolist(), w=w.tolist()) + assert_array_almost_equal(spl1(1.0, 1.0), spl2(1.0, 1.0)) + + +class TestSmoothSphereBivariateSpline: + def setup_method(self): + theta = array([.25*pi, .25*pi, .25*pi, .5*pi, .5*pi, .5*pi, .75*pi, + .75*pi, .75*pi]) + phi = array([.5 * pi, pi, 1.5 * pi, .5 * pi, pi, 1.5 * pi, .5 * pi, pi, + 1.5 * pi]) + r = array([3, 3, 3, 3, 3, 3, 3, 3, 3]) + self.lut = SmoothSphereBivariateSpline(theta, phi, r, s=1E10) + + def test_linear_constant(self): + assert abs(self.lut.get_residual()) < 1e-15 + assert_array_almost_equal(self.lut([1, 1.5, 2],[1, 1.5]), + [[3, 3], [3, 3], [3, 3]]) + + def test_empty_input(self): + assert_array_almost_equal(self.lut([], []), np.zeros((0,0))) + assert_array_almost_equal(self.lut([], [], grid=False), np.zeros((0,))) + + def test_invalid_input(self): + theta = array([.25 * pi, .25 * pi, .25 * pi, .5 * pi, .5 * pi, .5 * pi, + .75 * pi, .75 * pi, .75 * pi]) + phi = array([.5 * pi, pi, 1.5 * pi, .5 * pi, pi, 1.5 * pi, .5 * pi, pi, + 1.5 * pi]) + r = array([3, 3, 3, 3, 3, 3, 3, 3, 3]) + + with assert_raises(ValueError) as exc_info: + invalid_theta = array([-0.1 * pi, .25 * pi, .25 * pi, .5 * pi, + .5 * pi, .5 * pi, .75 * pi, .75 * pi, + .75 * pi]) + SmoothSphereBivariateSpline(invalid_theta, phi, r, s=1E10) + assert "theta should be between [0, pi]" in str(exc_info.value) + + with assert_raises(ValueError) as exc_info: + invalid_theta = array([.25 * pi, .25 * pi, .25 * pi, .5 * pi, + .5 * pi, .5 * pi, .75 * pi, .75 * pi, + 1.1 * pi]) + SmoothSphereBivariateSpline(invalid_theta, phi, r, s=1E10) + assert "theta should be between [0, pi]" in str(exc_info.value) + + with assert_raises(ValueError) as exc_info: + invalid_phi = array([-.1 * pi, pi, 1.5 * pi, .5 * pi, pi, 1.5 * pi, + .5 * pi, pi, 1.5 * pi]) + SmoothSphereBivariateSpline(theta, invalid_phi, r, s=1E10) + assert "phi should be between [0, 2pi]" in str(exc_info.value) + + with assert_raises(ValueError) as exc_info: + invalid_phi = array([1.0 * pi, pi, 1.5 * pi, .5 * pi, pi, 1.5 * pi, + .5 * pi, pi, 2.1 * pi]) + SmoothSphereBivariateSpline(theta, invalid_phi, r, s=1E10) + assert "phi should be between [0, 2pi]" in str(exc_info.value) + + with assert_raises(ValueError) as exc_info: + invalid_w = array([-1.0, 1.0, 1.5, 0.5, 1.0, 1.5, 0.5, 1.0, 1.0]) + SmoothSphereBivariateSpline(theta, phi, r, w=invalid_w, s=1E10) + assert "w should be positive" in str(exc_info.value) + + with assert_raises(ValueError) as exc_info: + SmoothSphereBivariateSpline(theta, phi, r, s=-1.0) + assert "s should be positive" in str(exc_info.value) + + with assert_raises(ValueError) as exc_info: + SmoothSphereBivariateSpline(theta, phi, r, eps=-1.0) + assert "eps should be between (0, 1)" in str(exc_info.value) + + with assert_raises(ValueError) as exc_info: + SmoothSphereBivariateSpline(theta, phi, r, eps=1.0) + assert "eps should be between (0, 1)" in str(exc_info.value) + + def test_array_like_input(self): + theta = np.array([.25 * pi, .25 * pi, .25 * pi, .5 * pi, .5 * pi, + .5 * pi, .75 * pi, .75 * pi, .75 * pi]) + phi = np.array([.5 * pi, pi, 1.5 * pi, .5 * pi, pi, 1.5 * pi, .5 * pi, + pi, 1.5 * pi]) + r = np.array([3, 3, 3, 3, 3, 3, 3, 3, 3]) + w = np.array([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]) + + # np.array input + spl1 = SmoothSphereBivariateSpline(theta, phi, r, w=w, s=1E10) + + # list input + spl2 = SmoothSphereBivariateSpline(theta.tolist(), phi.tolist(), + r.tolist(), w=w.tolist(), s=1E10) + assert_array_almost_equal(spl1(1.0, 1.0), spl2(1.0, 1.0)) + + +class TestRectBivariateSpline: + def test_defaults(self): + x = array([1,2,3,4,5]) + y = array([1,2,3,4,5]) + z = array([[1,2,1,2,1],[1,2,1,2,1],[1,2,3,2,1],[1,2,2,2,1],[1,2,1,2,1]]) + lut = RectBivariateSpline(x,y,z) + assert_array_almost_equal(lut(x,y),z) + + def test_evaluate(self): + x = array([1,2,3,4,5]) + y = array([1,2,3,4,5]) + z = array([[1,2,1,2,1],[1,2,1,2,1],[1,2,3,2,1],[1,2,2,2,1],[1,2,1,2,1]]) + lut = RectBivariateSpline(x,y,z) + + xi = [1, 2.3, 5.3, 0.5, 3.3, 1.2, 3] + yi = [1, 3.3, 1.2, 4.0, 5.0, 1.0, 3] + zi = lut.ev(xi, yi) + zi2 = array([lut(xp, yp)[0,0] for xp, yp in zip(xi, yi)]) + + assert_almost_equal(zi, zi2) + + def test_derivatives_grid(self): + x = array([1,2,3,4,5]) + y = array([1,2,3,4,5]) + z = array([[1,2,1,2,1],[1,2,1,2,1],[1,2,3,2,1],[1,2,2,2,1],[1,2,1,2,1]]) + dx = array([[0,0,-20,0,0],[0,0,13,0,0],[0,0,4,0,0], + [0,0,-11,0,0],[0,0,4,0,0]])/6. + dy = array([[4,-1,0,1,-4],[4,-1,0,1,-4],[0,1.5,0,-1.5,0], + [2,.25,0,-.25,-2],[4,-1,0,1,-4]]) + dxdy = array([[40,-25,0,25,-40],[-26,16.25,0,-16.25,26], + [-8,5,0,-5,8],[22,-13.75,0,13.75,-22],[-8,5,0,-5,8]])/6. + lut = RectBivariateSpline(x,y,z) + assert_array_almost_equal(lut(x,y,dx=1),dx) + assert_array_almost_equal(lut(x,y,dy=1),dy) + assert_array_almost_equal(lut(x,y,dx=1,dy=1),dxdy) + + def test_derivatives(self): + x = array([1,2,3,4,5]) + y = array([1,2,3,4,5]) + z = array([[1,2,1,2,1],[1,2,1,2,1],[1,2,3,2,1],[1,2,2,2,1],[1,2,1,2,1]]) + dx = array([0,0,2./3,0,0]) + dy = array([4,-1,0,-.25,-4]) + dxdy = array([160,65,0,55,32])/24. + lut = RectBivariateSpline(x,y,z) + assert_array_almost_equal(lut(x,y,dx=1,grid=False),dx) + assert_array_almost_equal(lut(x,y,dy=1,grid=False),dy) + assert_array_almost_equal(lut(x,y,dx=1,dy=1,grid=False),dxdy) + + def test_partial_derivative_method_grid(self): + x = array([1, 2, 3, 4, 5]) + y = array([1, 2, 3, 4, 5]) + z = array([[1, 2, 1, 2, 1], + [1, 2, 1, 2, 1], + [1, 2, 3, 2, 1], + [1, 2, 2, 2, 1], + [1, 2, 1, 2, 1]]) + dx = array([[0, 0, -20, 0, 0], + [0, 0, 13, 0, 0], + [0, 0, 4, 0, 0], + [0, 0, -11, 0, 0], + [0, 0, 4, 0, 0]]) / 6. + dy = array([[4, -1, 0, 1, -4], + [4, -1, 0, 1, -4], + [0, 1.5, 0, -1.5, 0], + [2, .25, 0, -.25, -2], + [4, -1, 0, 1, -4]]) + dxdy = array([[40, -25, 0, 25, -40], + [-26, 16.25, 0, -16.25, 26], + [-8, 5, 0, -5, 8], + [22, -13.75, 0, 13.75, -22], + [-8, 5, 0, -5, 8]]) / 6. + lut = RectBivariateSpline(x, y, z) + assert_array_almost_equal(lut.partial_derivative(1, 0)(x, y), dx) + assert_array_almost_equal(lut.partial_derivative(0, 1)(x, y), dy) + assert_array_almost_equal(lut.partial_derivative(1, 1)(x, y), dxdy) + + def test_partial_derivative_method(self): + x = array([1, 2, 3, 4, 5]) + y = array([1, 2, 3, 4, 5]) + z = array([[1, 2, 1, 2, 1], + [1, 2, 1, 2, 1], + [1, 2, 3, 2, 1], + [1, 2, 2, 2, 1], + [1, 2, 1, 2, 1]]) + dx = array([0, 0, 2./3, 0, 0]) + dy = array([4, -1, 0, -.25, -4]) + dxdy = array([160, 65, 0, 55, 32]) / 24. + lut = RectBivariateSpline(x, y, z) + assert_array_almost_equal(lut.partial_derivative(1, 0)(x, y, + grid=False), + dx) + assert_array_almost_equal(lut.partial_derivative(0, 1)(x, y, + grid=False), + dy) + assert_array_almost_equal(lut.partial_derivative(1, 1)(x, y, + grid=False), + dxdy) + + def test_partial_derivative_order_too_large(self): + x = array([0, 1, 2, 3, 4], dtype=float) + y = x.copy() + z = ones((x.size, y.size)) + lut = RectBivariateSpline(x, y, z) + with assert_raises(ValueError): + lut.partial_derivative(4, 1) + + def test_broadcast(self): + x = array([1,2,3,4,5]) + y = array([1,2,3,4,5]) + z = array([[1,2,1,2,1],[1,2,1,2,1],[1,2,3,2,1],[1,2,2,2,1],[1,2,1,2,1]]) + lut = RectBivariateSpline(x,y,z) + xp_assert_close(lut(x, y), lut(x[:,None], y[None,:], grid=False)) + + def test_invalid_input(self): + + with assert_raises(ValueError) as info: + x = array([6, 2, 3, 4, 5]) + y = array([1, 2, 3, 4, 5]) + z = array([[1, 2, 1, 2, 1], [1, 2, 1, 2, 1], [1, 2, 3, 2, 1], + [1, 2, 2, 2, 1], [1, 2, 1, 2, 1]]) + RectBivariateSpline(x, y, z) + assert "x must be strictly increasing" in str(info.value) + + with assert_raises(ValueError) as info: + x = array([1, 2, 3, 4, 5]) + y = array([2, 2, 3, 4, 5]) + z = array([[1, 2, 1, 2, 1], [1, 2, 1, 2, 1], [1, 2, 3, 2, 1], + [1, 2, 2, 2, 1], [1, 2, 1, 2, 1]]) + RectBivariateSpline(x, y, z) + assert "y must be strictly increasing" in str(info.value) + + with assert_raises(ValueError) as info: + x = array([1, 2, 3, 4, 5]) + y = array([1, 2, 3, 4, 5]) + z = array([[1, 2, 1, 2, 1], [1, 2, 1, 2, 1], [1, 2, 3, 2, 1], + [1, 2, 2, 2, 1]]) + RectBivariateSpline(x, y, z) + assert "x dimension of z must have same number of elements as x"\ + in str(info.value) + + with assert_raises(ValueError) as info: + x = array([1, 2, 3, 4, 5]) + y = array([1, 2, 3, 4, 5]) + z = array([[1, 2, 1, 2], [1, 2, 1, 2], [1, 2, 3, 2], + [1, 2, 2, 2], [1, 2, 1, 2]]) + RectBivariateSpline(x, y, z) + assert "y dimension of z must have same number of elements as y"\ + in str(info.value) + + with assert_raises(ValueError) as info: + x = array([1, 2, 3, 4, 5]) + y = array([1, 2, 3, 4, 5]) + z = array([[1, 2, 1, 2, 1], [1, 2, 1, 2, 1], [1, 2, 3, 2, 1], + [1, 2, 2, 2, 1], [1, 2, 1, 2, 1]]) + bbox = (-100, 100, -100) + RectBivariateSpline(x, y, z, bbox=bbox) + assert "bbox shape should be (4,)" in str(info.value) + + with assert_raises(ValueError) as info: + RectBivariateSpline(x, y, z, s=-1.0) + assert "s should be s >= 0.0" in str(info.value) + + def test_array_like_input(self): + x = array([1, 2, 3, 4, 5]) + y = array([1, 2, 3, 4, 5]) + z = array([[1, 2, 1, 2, 1], [1, 2, 1, 2, 1], [1, 2, 3, 2, 1], + [1, 2, 2, 2, 1], [1, 2, 1, 2, 1]]) + bbox = array([1, 5, 1, 5]) + + spl1 = RectBivariateSpline(x, y, z, bbox=bbox) + spl2 = RectBivariateSpline(x.tolist(), y.tolist(), z.tolist(), + bbox=bbox.tolist()) + assert_array_almost_equal(spl1(1.0, 1.0), spl2(1.0, 1.0)) + + def test_not_increasing_input(self): + # gh-8565 + NSamp = 20 + Theta = np.random.uniform(0, np.pi, NSamp) + Phi = np.random.uniform(0, 2 * np.pi, NSamp) + Data = np.ones(NSamp) + + Interpolator = SmoothSphereBivariateSpline(Theta, Phi, Data, s=3.5) + + NLon = 6 + NLat = 3 + GridPosLats = np.arange(NLat) / NLat * np.pi + GridPosLons = np.arange(NLon) / NLon * 2 * np.pi + + # No error + Interpolator(GridPosLats, GridPosLons) + + nonGridPosLats = GridPosLats.copy() + nonGridPosLats[2] = 0.001 + with assert_raises(ValueError) as exc_info: + Interpolator(nonGridPosLats, GridPosLons) + assert "x must be strictly increasing" in str(exc_info.value) + + nonGridPosLons = GridPosLons.copy() + nonGridPosLons[2] = 0.001 + with assert_raises(ValueError) as exc_info: + Interpolator(GridPosLats, nonGridPosLons) + assert "y must be strictly increasing" in str(exc_info.value) + + +class TestRectSphereBivariateSpline: + def test_defaults(self): + y = linspace(0.01, 2*pi-0.01, 7) + x = linspace(0.01, pi-0.01, 7) + z = array([[1,2,1,2,1,2,1],[1,2,1,2,1,2,1],[1,2,3,2,1,2,1], + [1,2,2,2,1,2,1],[1,2,1,2,1,2,1],[1,2,2,2,1,2,1], + [1,2,1,2,1,2,1]]) + lut = RectSphereBivariateSpline(x,y,z) + assert_array_almost_equal(lut(x,y),z) + + def test_evaluate(self): + y = linspace(0.01, 2*pi-0.01, 7) + x = linspace(0.01, pi-0.01, 7) + z = array([[1,2,1,2,1,2,1],[1,2,1,2,1,2,1],[1,2,3,2,1,2,1], + [1,2,2,2,1,2,1],[1,2,1,2,1,2,1],[1,2,2,2,1,2,1], + [1,2,1,2,1,2,1]]) + lut = RectSphereBivariateSpline(x,y,z) + yi = [0.2, 1, 2.3, 2.35, 3.0, 3.99, 5.25] + xi = [1.5, 0.4, 1.1, 0.45, 0.2345, 1., 0.0001] + zi = lut.ev(xi, yi) + zi2 = array([lut(xp, yp)[0,0] for xp, yp in zip(xi, yi)]) + assert_almost_equal(zi, zi2) + + def test_invalid_input(self): + data = np.dot(np.atleast_2d(90. - np.linspace(-80., 80., 18)).T, + np.atleast_2d(180. - np.abs(np.linspace(0., 350., 9)))).T + + with assert_raises(ValueError) as exc_info: + lats = np.linspace(-1, 170, 9) * np.pi / 180. + lons = np.linspace(0, 350, 18) * np.pi / 180. + RectSphereBivariateSpline(lats, lons, data) + assert "u should be between (0, pi)" in str(exc_info.value) + + with assert_raises(ValueError) as exc_info: + lats = np.linspace(10, 181, 9) * np.pi / 180. + lons = np.linspace(0, 350, 18) * np.pi / 180. + RectSphereBivariateSpline(lats, lons, data) + assert "u should be between (0, pi)" in str(exc_info.value) + + with assert_raises(ValueError) as exc_info: + lats = np.linspace(10, 170, 9) * np.pi / 180. + lons = np.linspace(-181, 10, 18) * np.pi / 180. + RectSphereBivariateSpline(lats, lons, data) + assert "v[0] should be between [-pi, pi)" in str(exc_info.value) + + with assert_raises(ValueError) as exc_info: + lats = np.linspace(10, 170, 9) * np.pi / 180. + lons = np.linspace(-10, 360, 18) * np.pi / 180. + RectSphereBivariateSpline(lats, lons, data) + assert "v[-1] should be v[0] + 2pi or less" in str(exc_info.value) + + with assert_raises(ValueError) as exc_info: + lats = np.linspace(10, 170, 9) * np.pi / 180. + lons = np.linspace(10, 350, 18) * np.pi / 180. + RectSphereBivariateSpline(lats, lons, data, s=-1) + assert "s should be positive" in str(exc_info.value) + + def test_derivatives_grid(self): + y = linspace(0.01, 2*pi-0.01, 7) + x = linspace(0.01, pi-0.01, 7) + z = array([[1,2,1,2,1,2,1],[1,2,1,2,1,2,1],[1,2,3,2,1,2,1], + [1,2,2,2,1,2,1],[1,2,1,2,1,2,1],[1,2,2,2,1,2,1], + [1,2,1,2,1,2,1]]) + + lut = RectSphereBivariateSpline(x,y,z) + + y = linspace(0.02, 2*pi-0.02, 7) + x = linspace(0.02, pi-0.02, 7) + + xp_assert_close(lut(x, y, dtheta=1), _numdiff_2d(lut, x, y, dx=1), + rtol=1e-4, atol=1e-4) + xp_assert_close(lut(x, y, dphi=1), _numdiff_2d(lut, x, y, dy=1), + rtol=1e-4, atol=1e-4) + xp_assert_close(lut(x, y, dtheta=1, dphi=1), + _numdiff_2d(lut, x, y, dx=1, dy=1, eps=1e-6), + rtol=1e-3, atol=1e-3) + + xp_assert_equal(lut(x, y, dtheta=1), + lut.partial_derivative(1, 0)(x, y)) + xp_assert_equal(lut(x, y, dphi=1), + lut.partial_derivative(0, 1)(x, y)) + xp_assert_equal(lut(x, y, dtheta=1, dphi=1), + lut.partial_derivative(1, 1)(x, y)) + + xp_assert_equal(lut(x, y, dtheta=1, grid=False), + lut.partial_derivative(1, 0)(x, y, grid=False)) + xp_assert_equal(lut(x, y, dphi=1, grid=False), + lut.partial_derivative(0, 1)(x, y, grid=False)) + xp_assert_equal(lut(x, y, dtheta=1, dphi=1, grid=False), + lut.partial_derivative(1, 1)(x, y, grid=False)) + + def test_derivatives(self): + y = linspace(0.01, 2*pi-0.01, 7) + x = linspace(0.01, pi-0.01, 7) + z = array([[1,2,1,2,1,2,1],[1,2,1,2,1,2,1],[1,2,3,2,1,2,1], + [1,2,2,2,1,2,1],[1,2,1,2,1,2,1],[1,2,2,2,1,2,1], + [1,2,1,2,1,2,1]]) + + lut = RectSphereBivariateSpline(x,y,z) + + y = linspace(0.02, 2*pi-0.02, 7) + x = linspace(0.02, pi-0.02, 7) + + assert lut(x, y, dtheta=1, grid=False).shape == x.shape + xp_assert_close(lut(x, y, dtheta=1, grid=False), + _numdiff_2d(lambda x,y: lut(x,y,grid=False), x, y, dx=1), + rtol=1e-4, atol=1e-4) + xp_assert_close(lut(x, y, dphi=1, grid=False), + _numdiff_2d(lambda x,y: lut(x,y,grid=False), x, y, dy=1), + rtol=1e-4, atol=1e-4) + xp_assert_close(lut(x, y, dtheta=1, dphi=1, grid=False), + _numdiff_2d(lambda x,y: lut(x,y,grid=False), + x, y, dx=1, dy=1, eps=1e-6), + rtol=1e-3, atol=1e-3) + + def test_invalid_input_2(self): + data = np.dot(np.atleast_2d(90. - np.linspace(-80., 80., 18)).T, + np.atleast_2d(180. - np.abs(np.linspace(0., 350., 9)))).T + + with assert_raises(ValueError) as exc_info: + lats = np.linspace(0, 170, 9) * np.pi / 180. + lons = np.linspace(0, 350, 18) * np.pi / 180. + RectSphereBivariateSpline(lats, lons, data) + assert "u should be between (0, pi)" in str(exc_info.value) + + with assert_raises(ValueError) as exc_info: + lats = np.linspace(10, 180, 9) * np.pi / 180. + lons = np.linspace(0, 350, 18) * np.pi / 180. + RectSphereBivariateSpline(lats, lons, data) + assert "u should be between (0, pi)" in str(exc_info.value) + + with assert_raises(ValueError) as exc_info: + lats = np.linspace(10, 170, 9) * np.pi / 180. + lons = np.linspace(-181, 10, 18) * np.pi / 180. + RectSphereBivariateSpline(lats, lons, data) + assert "v[0] should be between [-pi, pi)" in str(exc_info.value) + + with assert_raises(ValueError) as exc_info: + lats = np.linspace(10, 170, 9) * np.pi / 180. + lons = np.linspace(-10, 360, 18) * np.pi / 180. + RectSphereBivariateSpline(lats, lons, data) + assert "v[-1] should be v[0] + 2pi or less" in str(exc_info.value) + + with assert_raises(ValueError) as exc_info: + lats = np.linspace(10, 170, 9) * np.pi / 180. + lons = np.linspace(10, 350, 18) * np.pi / 180. + RectSphereBivariateSpline(lats, lons, data, s=-1) + assert "s should be positive" in str(exc_info.value) + + def test_array_like_input(self): + y = linspace(0.01, 2 * pi - 0.01, 7) + x = linspace(0.01, pi - 0.01, 7) + z = array([[1, 2, 1, 2, 1, 2, 1], [1, 2, 1, 2, 1, 2, 1], + [1, 2, 3, 2, 1, 2, 1], + [1, 2, 2, 2, 1, 2, 1], [1, 2, 1, 2, 1, 2, 1], + [1, 2, 2, 2, 1, 2, 1], + [1, 2, 1, 2, 1, 2, 1]]) + # np.array input + spl1 = RectSphereBivariateSpline(x, y, z) + # list input + spl2 = RectSphereBivariateSpline(x.tolist(), y.tolist(), z.tolist()) + assert_array_almost_equal(spl1(x, y), spl2(x, y)) + + def test_negative_evaluation(self): + lats = np.array([25, 30, 35, 40, 45]) + lons = np.array([-90, -85, -80, -75, 70]) + mesh = np.meshgrid(lats, lons) + data = mesh[0] + mesh[1] # lon + lat value + lat_r = np.radians(lats) + lon_r = np.radians(lons) + interpolator = RectSphereBivariateSpline(lat_r, lon_r, data) + query_lat = np.radians(np.array([35, 37.5])) + query_lon = np.radians(np.array([-80, -77.5])) + data_interp = interpolator(query_lat, query_lon) + ans = np.array([[-45.0, -42.480862], + [-49.0625, -46.54315]]) + assert_array_almost_equal(data_interp, ans) + + def test_pole_continuity_gh_14591(self): + # regression test for https://github.com/scipy/scipy/issues/14591 + # with pole_continuty=(True, True), the internal work array size + # was too small, leading to a FITPACK data validation error. + + # The reproducer in gh-14591 was using a NetCDF4 file with + # 361x507 arrays, so here we trivialize array sizes to a minimum + # which still demonstrates the issue. + u = np.arange(1, 10) * np.pi / 10 + v = np.arange(1, 10) * np.pi / 10 + r = np.zeros((9, 9)) + for p in [(True, True), (True, False), (False, False)]: + RectSphereBivariateSpline(u, v, r, s=0, pole_continuity=p) + + +def _numdiff_2d(func, x, y, dx=0, dy=0, eps=1e-8): + if dx == 0 and dy == 0: + return func(x, y) + elif dx == 1 and dy == 0: + return (func(x + eps, y) - func(x - eps, y)) / (2*eps) + elif dx == 0 and dy == 1: + return (func(x, y + eps) - func(x, y - eps)) / (2*eps) + elif dx == 1 and dy == 1: + return (func(x + eps, y + eps) - func(x - eps, y + eps) + - func(x + eps, y - eps) + func(x - eps, y - eps)) / (2*eps)**2 + else: + raise ValueError("invalid derivative order") + + +class Test_DerivedBivariateSpline: + """Test the creation, usage, and attribute access of the (private) + _DerivedBivariateSpline class. + """ + def setup_method(self): + x = np.concatenate(list(zip(range(10), range(10)))) + y = np.concatenate(list(zip(range(10), range(1, 11)))) + z = np.concatenate((np.linspace(3, 1, 10), np.linspace(1, 3, 10))) + with suppress_warnings() as sup: + sup.record(UserWarning, "\nThe coefficients of the spline") + self.lut_lsq = LSQBivariateSpline(x, y, z, + linspace(0.5, 19.5, 4), + linspace(1.5, 20.5, 4), + eps=1e-2) + self.lut_smooth = SmoothBivariateSpline(x, y, z) + xx = linspace(0, 1, 20) + yy = xx + 1.0 + zz = array([np.roll(z, i) for i in range(z.size)]) + self.lut_rect = RectBivariateSpline(xx, yy, zz) + self.orders = list(itertools.product(range(3), range(3))) + + def test_creation_from_LSQ(self): + for nux, nuy in self.orders: + lut_der = self.lut_lsq.partial_derivative(nux, nuy) + a = lut_der(3.5, 3.5, grid=False) + b = self.lut_lsq(3.5, 3.5, dx=nux, dy=nuy, grid=False) + assert a == b + + def test_creation_from_Smooth(self): + for nux, nuy in self.orders: + lut_der = self.lut_smooth.partial_derivative(nux, nuy) + a = lut_der(5.5, 5.5, grid=False) + b = self.lut_smooth(5.5, 5.5, dx=nux, dy=nuy, grid=False) + assert a == b + + def test_creation_from_Rect(self): + for nux, nuy in self.orders: + lut_der = self.lut_rect.partial_derivative(nux, nuy) + a = lut_der(0.5, 1.5, grid=False) + b = self.lut_rect(0.5, 1.5, dx=nux, dy=nuy, grid=False) + assert a == b + + def test_invalid_attribute_fp(self): + der = self.lut_rect.partial_derivative(1, 1) + with assert_raises(AttributeError): + der.fp + + def test_invalid_attribute_get_residual(self): + der = self.lut_smooth.partial_derivative(1, 1) + with assert_raises(AttributeError): + der.get_residual() diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_gil.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_gil.py new file mode 100644 index 0000000000000000000000000000000000000000..48197062e0b83a9ef54e45089d9089d49b8ad367 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_gil.py @@ -0,0 +1,64 @@ +import itertools +import threading +import time + +import numpy as np +import pytest +import scipy.interpolate + + +class TestGIL: + """Check if the GIL is properly released by scipy.interpolate functions.""" + + def setup_method(self): + self.messages = [] + + def log(self, message): + self.messages.append(message) + + def make_worker_thread(self, target, args): + log = self.log + + class WorkerThread(threading.Thread): + def run(self): + log('interpolation started') + target(*args) + log('interpolation complete') + + return WorkerThread() + + @pytest.mark.xslow + @pytest.mark.xfail(reason='race conditions, may depend on system load') + def test_rectbivariatespline(self): + def generate_params(n_points): + x = y = np.linspace(0, 1000, n_points) + x_grid, y_grid = np.meshgrid(x, y) + z = x_grid * y_grid + return x, y, z + + def calibrate_delay(requested_time): + for n_points in itertools.count(5000, 1000): + args = generate_params(n_points) + time_started = time.time() + interpolate(*args) + if time.time() - time_started > requested_time: + return args + + def interpolate(x, y, z): + scipy.interpolate.RectBivariateSpline(x, y, z) + + args = calibrate_delay(requested_time=3) + worker_thread = self.make_worker_thread(interpolate, args) + worker_thread.start() + for i in range(3): + time.sleep(0.5) + self.log('working') + worker_thread.join() + assert self.messages == [ + 'interpolation started', + 'working', + 'working', + 'working', + 'interpolation complete', + ] + diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_interpnd.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_interpnd.py new file mode 100644 index 0000000000000000000000000000000000000000..981cd99d9d56e11e8d8ce0635e7b7240c19eef1f --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_interpnd.py @@ -0,0 +1,440 @@ +import os +import sys + +import numpy as np +from numpy.testing import suppress_warnings +from pytest import raises as assert_raises +import pytest +from scipy._lib._array_api import xp_assert_close, assert_almost_equal + +from scipy._lib._testutils import check_free_memory +import scipy.interpolate._interpnd as interpnd +import scipy.spatial._qhull as qhull + +import pickle +import threading + +_IS_32BIT = (sys.maxsize < 2**32) + + +def data_file(basename): + return os.path.join(os.path.abspath(os.path.dirname(__file__)), + 'data', basename) + + +class TestLinearNDInterpolation: + def test_smoketest(self): + # Test at single points + x = np.array([(0,0), (-0.5,-0.5), (-0.5,0.5), (0.5, 0.5), (0.25, 0.3)], + dtype=np.float64) + y = np.arange(x.shape[0], dtype=np.float64) + + yi = interpnd.LinearNDInterpolator(x, y)(x) + assert_almost_equal(y, yi) + + def test_smoketest_alternate(self): + # Test at single points, alternate calling convention + x = np.array([(0,0), (-0.5,-0.5), (-0.5,0.5), (0.5, 0.5), (0.25, 0.3)], + dtype=np.float64) + y = np.arange(x.shape[0], dtype=np.float64) + + yi = interpnd.LinearNDInterpolator((x[:,0], x[:,1]), y)(x[:,0], x[:,1]) + assert_almost_equal(y, yi) + + def test_complex_smoketest(self): + # Test at single points + x = np.array([(0,0), (-0.5,-0.5), (-0.5,0.5), (0.5, 0.5), (0.25, 0.3)], + dtype=np.float64) + y = np.arange(x.shape[0], dtype=np.float64) + y = y - 3j*y + + yi = interpnd.LinearNDInterpolator(x, y)(x) + assert_almost_equal(y, yi) + + def test_tri_input(self): + # Test at single points + x = np.array([(0,0), (-0.5,-0.5), (-0.5,0.5), (0.5, 0.5), (0.25, 0.3)], + dtype=np.float64) + y = np.arange(x.shape[0], dtype=np.float64) + y = y - 3j*y + + tri = qhull.Delaunay(x) + interpolator = interpnd.LinearNDInterpolator(tri, y) + yi = interpolator(x) + assert_almost_equal(y, yi) + assert interpolator.tri is tri + + def test_square(self): + # Test barycentric interpolation on a square against a manual + # implementation + + points = np.array([(0,0), (0,1), (1,1), (1,0)], dtype=np.float64) + values = np.array([1., 2., -3., 5.], dtype=np.float64) + + # NB: assume triangles (0, 1, 3) and (1, 2, 3) + # + # 1----2 + # | \ | + # | \ | + # 0----3 + + def ip(x, y): + t1 = (x + y <= 1) + t2 = ~t1 + + x1 = x[t1] + y1 = y[t1] + + x2 = x[t2] + y2 = y[t2] + + z = 0*x + + z[t1] = (values[0]*(1 - x1 - y1) + + values[1]*y1 + + values[3]*x1) + + z[t2] = (values[2]*(x2 + y2 - 1) + + values[1]*(1 - x2) + + values[3]*(1 - y2)) + return z + + xx, yy = np.broadcast_arrays(np.linspace(0, 1, 14)[:,None], + np.linspace(0, 1, 14)[None,:]) + xx = xx.ravel() + yy = yy.ravel() + + xi = np.array([xx, yy]).T.copy() + zi = interpnd.LinearNDInterpolator(points, values)(xi) + + assert_almost_equal(zi, ip(xx, yy)) + + def test_smoketest_rescale(self): + # Test at single points + x = np.array([(0, 0), (-5, -5), (-5, 5), (5, 5), (2.5, 3)], + dtype=np.float64) + y = np.arange(x.shape[0], dtype=np.float64) + + yi = interpnd.LinearNDInterpolator(x, y, rescale=True)(x) + assert_almost_equal(y, yi) + + def test_square_rescale(self): + # Test barycentric interpolation on a rectangle with rescaling + # agaings the same implementation without rescaling + + points = np.array([(0,0), (0,100), (10,100), (10,0)], dtype=np.float64) + values = np.array([1., 2., -3., 5.], dtype=np.float64) + + xx, yy = np.broadcast_arrays(np.linspace(0, 10, 14)[:,None], + np.linspace(0, 100, 14)[None,:]) + xx = xx.ravel() + yy = yy.ravel() + xi = np.array([xx, yy]).T.copy() + zi = interpnd.LinearNDInterpolator(points, values)(xi) + zi_rescaled = interpnd.LinearNDInterpolator(points, values, + rescale=True)(xi) + + assert_almost_equal(zi, zi_rescaled) + + def test_tripoints_input_rescale(self): + # Test at single points + x = np.array([(0,0), (-5,-5), (-5,5), (5, 5), (2.5, 3)], + dtype=np.float64) + y = np.arange(x.shape[0], dtype=np.float64) + y = y - 3j*y + + tri = qhull.Delaunay(x) + yi = interpnd.LinearNDInterpolator(tri.points, y)(x) + yi_rescale = interpnd.LinearNDInterpolator(tri.points, y, + rescale=True)(x) + assert_almost_equal(yi, yi_rescale) + + def test_tri_input_rescale(self): + # Test at single points + x = np.array([(0,0), (-5,-5), (-5,5), (5, 5), (2.5, 3)], + dtype=np.float64) + y = np.arange(x.shape[0], dtype=np.float64) + y = y - 3j*y + + tri = qhull.Delaunay(x) + match = ("Rescaling is not supported when passing a " + "Delaunay triangulation as ``points``.") + with pytest.raises(ValueError, match=match): + interpnd.LinearNDInterpolator(tri, y, rescale=True)(x) + + def test_pickle(self): + # Test at single points + np.random.seed(1234) + x = np.random.rand(30, 2) + y = np.random.rand(30) + 1j*np.random.rand(30) + + ip = interpnd.LinearNDInterpolator(x, y) + ip2 = pickle.loads(pickle.dumps(ip)) + + assert_almost_equal(ip(0.5, 0.5), ip2(0.5, 0.5)) + + @pytest.mark.slow + @pytest.mark.thread_unsafe + @pytest.mark.skipif(_IS_32BIT, reason='it fails on 32-bit') + def test_threading(self): + # This test was taken from issue 8856 + # https://github.com/scipy/scipy/issues/8856 + check_free_memory(10000) + + r_ticks = np.arange(0, 4200, 10) + phi_ticks = np.arange(0, 4200, 10) + r_grid, phi_grid = np.meshgrid(r_ticks, phi_ticks) + + def do_interp(interpolator, slice_rows, slice_cols): + grid_x, grid_y = np.mgrid[slice_rows, slice_cols] + res = interpolator((grid_x, grid_y)) + return res + + points = np.vstack((r_grid.ravel(), phi_grid.ravel())).T + values = (r_grid * phi_grid).ravel() + interpolator = interpnd.LinearNDInterpolator(points, values) + + worker_thread_1 = threading.Thread( + target=do_interp, + args=(interpolator, slice(0, 2100), slice(0, 2100))) + worker_thread_2 = threading.Thread( + target=do_interp, + args=(interpolator, slice(2100, 4200), slice(0, 2100))) + worker_thread_3 = threading.Thread( + target=do_interp, + args=(interpolator, slice(0, 2100), slice(2100, 4200))) + worker_thread_4 = threading.Thread( + target=do_interp, + args=(interpolator, slice(2100, 4200), slice(2100, 4200))) + + worker_thread_1.start() + worker_thread_2.start() + worker_thread_3.start() + worker_thread_4.start() + + worker_thread_1.join() + worker_thread_2.join() + worker_thread_3.join() + worker_thread_4.join() + + +class TestEstimateGradients2DGlobal: + def test_smoketest(self): + x = np.array([(0, 0), (0, 2), + (1, 0), (1, 2), (0.25, 0.75), (0.6, 0.8)], dtype=float) + tri = qhull.Delaunay(x) + + # Should be exact for linear functions, independent of triangulation + + funcs = [ + (lambda x, y: 0*x + 1, (0, 0)), + (lambda x, y: 0 + x, (1, 0)), + (lambda x, y: -2 + y, (0, 1)), + (lambda x, y: 3 + 3*x + 14.15*y, (3, 14.15)) + ] + + for j, (func, grad) in enumerate(funcs): + z = func(x[:,0], x[:,1]) + dz = interpnd.estimate_gradients_2d_global(tri, z, tol=1e-6) + + assert dz.shape == (6, 2) + xp_assert_close(dz, np.array(grad)[None,:] + 0*dz, + rtol=1e-5, atol=1e-5, err_msg="item %d" % j) + + def test_regression_2359(self): + # Check regression --- for certain point sets, gradient + # estimation could end up in an infinite loop + points = np.load(data_file('estimate_gradients_hang.npy')) + values = np.random.rand(points.shape[0]) + tri = qhull.Delaunay(points) + + # This should not hang + with suppress_warnings() as sup: + sup.filter(interpnd.GradientEstimationWarning, + "Gradient estimation did not converge") + interpnd.estimate_gradients_2d_global(tri, values, maxiter=1) + + +class TestCloughTocher2DInterpolator: + + def _check_accuracy(self, func, x=None, tol=1e-6, alternate=False, + rescale=False, **kw): + rng = np.random.RandomState(1234) + # np.random.seed(1234) + if x is None: + x = np.array([(0, 0), (0, 1), + (1, 0), (1, 1), (0.25, 0.75), (0.6, 0.8), + (0.5, 0.2)], + dtype=float) + + if not alternate: + ip = interpnd.CloughTocher2DInterpolator(x, func(x[:,0], x[:,1]), + tol=1e-6, rescale=rescale) + else: + ip = interpnd.CloughTocher2DInterpolator((x[:,0], x[:,1]), + func(x[:,0], x[:,1]), + tol=1e-6, rescale=rescale) + + p = rng.rand(50, 2) + + if not alternate: + a = ip(p) + else: + a = ip(p[:,0], p[:,1]) + b = func(p[:,0], p[:,1]) + + try: + xp_assert_close(a, b, **kw) + except AssertionError: + print("_check_accuracy: abs(a-b):", abs(a - b)) + print("ip.grad:", ip.grad) + raise + + def test_linear_smoketest(self): + # Should be exact for linear functions, independent of triangulation + funcs = [ + lambda x, y: 0*x + 1, + lambda x, y: 0 + x, + lambda x, y: -2 + y, + lambda x, y: 3 + 3*x + 14.15*y, + ] + + for j, func in enumerate(funcs): + self._check_accuracy(func, tol=1e-13, atol=1e-7, rtol=1e-7, + err_msg="Function %d" % j) + self._check_accuracy(func, tol=1e-13, atol=1e-7, rtol=1e-7, + alternate=True, + err_msg="Function (alternate) %d" % j) + # check rescaling + self._check_accuracy(func, tol=1e-13, atol=1e-7, rtol=1e-7, + err_msg="Function (rescaled) %d" % j, rescale=True) + self._check_accuracy(func, tol=1e-13, atol=1e-7, rtol=1e-7, + alternate=True, rescale=True, + err_msg="Function (alternate, rescaled) %d" % j) + + def test_quadratic_smoketest(self): + # Should be reasonably accurate for quadratic functions + funcs = [ + lambda x, y: x**2, + lambda x, y: y**2, + lambda x, y: x**2 - y**2, + lambda x, y: x*y, + ] + + for j, func in enumerate(funcs): + self._check_accuracy(func, tol=1e-9, atol=0.22, rtol=0, + err_msg="Function %d" % j) + self._check_accuracy(func, tol=1e-9, atol=0.22, rtol=0, + err_msg="Function %d" % j, rescale=True) + + def test_tri_input(self): + # Test at single points + x = np.array([(0,0), (-0.5,-0.5), (-0.5,0.5), (0.5, 0.5), (0.25, 0.3)], + dtype=np.float64) + y = np.arange(x.shape[0], dtype=np.float64) + y = y - 3j*y + + tri = qhull.Delaunay(x) + yi = interpnd.CloughTocher2DInterpolator(tri, y)(x) + assert_almost_equal(y, yi) + + def test_tri_input_rescale(self): + # Test at single points + x = np.array([(0,0), (-5,-5), (-5,5), (5, 5), (2.5, 3)], + dtype=np.float64) + y = np.arange(x.shape[0], dtype=np.float64) + y = y - 3j*y + + tri = qhull.Delaunay(x) + match = ("Rescaling is not supported when passing a " + "Delaunay triangulation as ``points``.") + with pytest.raises(ValueError, match=match): + interpnd.CloughTocher2DInterpolator(tri, y, rescale=True)(x) + + def test_tripoints_input_rescale(self): + # Test at single points + x = np.array([(0,0), (-5,-5), (-5,5), (5, 5), (2.5, 3)], + dtype=np.float64) + y = np.arange(x.shape[0], dtype=np.float64) + y = y - 3j*y + + tri = qhull.Delaunay(x) + yi = interpnd.CloughTocher2DInterpolator(tri.points, y)(x) + yi_rescale = interpnd.CloughTocher2DInterpolator(tri.points, y, rescale=True)(x) + assert_almost_equal(yi, yi_rescale) + + @pytest.mark.fail_slow(5) + def test_dense(self): + # Should be more accurate for dense meshes + funcs = [ + lambda x, y: x**2, + lambda x, y: y**2, + lambda x, y: x**2 - y**2, + lambda x, y: x*y, + lambda x, y: np.cos(2*np.pi*x)*np.sin(2*np.pi*y) + ] + + rng = np.random.RandomState(4321) # use a different seed than the check! + grid = np.r_[np.array([(0,0), (0,1), (1,0), (1,1)], dtype=float), + rng.rand(30*30, 2)] + + for j, func in enumerate(funcs): + self._check_accuracy(func, x=grid, tol=1e-9, atol=5e-3, rtol=1e-2, + err_msg="Function %d" % j) + self._check_accuracy(func, x=grid, tol=1e-9, atol=5e-3, rtol=1e-2, + err_msg="Function %d" % j, rescale=True) + + def test_wrong_ndim(self): + x = np.random.randn(30, 3) + y = np.random.randn(30) + assert_raises(ValueError, interpnd.CloughTocher2DInterpolator, x, y) + + def test_pickle(self): + # Test at single points + rng = np.random.RandomState(1234) + x = rng.rand(30, 2) + y = rng.rand(30) + 1j*rng.rand(30) + + ip = interpnd.CloughTocher2DInterpolator(x, y) + ip2 = pickle.loads(pickle.dumps(ip)) + + assert_almost_equal(ip(0.5, 0.5), ip2(0.5, 0.5)) + + def test_boundary_tri_symmetry(self): + # Interpolation at neighbourless triangles should retain + # symmetry with mirroring the triangle. + + # Equilateral triangle + points = np.array([(0, 0), (1, 0), (0.5, np.sqrt(3)/2)]) + values = np.array([1, 0, 0]) + + ip = interpnd.CloughTocher2DInterpolator(points, values) + + # Set gradient to zero at vertices + ip.grad[...] = 0 + + # Interpolation should be symmetric vs. bisector + alpha = 0.3 + p1 = np.array([0.5 * np.cos(alpha), 0.5 * np.sin(alpha)]) + p2 = np.array([0.5 * np.cos(np.pi/3 - alpha), 0.5 * np.sin(np.pi/3 - alpha)]) + + v1 = ip(p1) + v2 = ip(p2) + xp_assert_close(v1, v2) + + # ... and affine invariant + rng = np.random.RandomState(1) + A = rng.randn(2, 2) + b = rng.randn(2) + + points = A.dot(points.T).T + b[None,:] + p1 = A.dot(p1) + b + p2 = A.dot(p2) + b + + ip = interpnd.CloughTocher2DInterpolator(points, values) + ip.grad[...] = 0 + + w1 = ip(p1) + w2 = ip(p2) + xp_assert_close(w1, v1) + xp_assert_close(w2, v2) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_interpolate.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_interpolate.py new file mode 100644 index 0000000000000000000000000000000000000000..24a6907b7b050ddb0686cf8b1761bfd50eaf692a --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_interpolate.py @@ -0,0 +1,2586 @@ +from scipy._lib._array_api import ( + xp_assert_equal, xp_assert_close, assert_almost_equal, assert_array_almost_equal +) +from pytest import raises as assert_raises +import pytest + +from numpy import mgrid, pi, sin, poly1d +import numpy as np + +from scipy.interpolate import (interp1d, interp2d, lagrange, PPoly, BPoly, + splrep, splev, splantider, splint, sproot, Akima1DInterpolator, + NdPPoly, BSpline, PchipInterpolator) + +from scipy.special import poch, gamma + +from scipy.interpolate import _ppoly + +from scipy._lib._gcutils import assert_deallocated, IS_PYPY +from scipy._lib._testutils import _run_concurrent_barrier + +from scipy.integrate import nquad + +from scipy.special import binom + + +class TestInterp2D: + def test_interp2d(self): + y, x = mgrid[0:2:20j, 0:pi:21j] + z = sin(x+0.5*y) + with assert_raises(NotImplementedError): + interp2d(x, y, z) + + +class TestInterp1D: + + def setup_method(self): + self.x5 = np.arange(5.) + self.x10 = np.arange(10.) + self.y10 = np.arange(10.) + self.x25 = self.x10.reshape((2,5)) + self.x2 = np.arange(2.) + self.y2 = np.arange(2.) + self.x1 = np.array([0.]) + self.y1 = np.array([0.]) + + self.y210 = np.arange(20.).reshape((2, 10)) + self.y102 = np.arange(20.).reshape((10, 2)) + self.y225 = np.arange(20.).reshape((2, 2, 5)) + self.y25 = np.arange(10.).reshape((2, 5)) + self.y235 = np.arange(30.).reshape((2, 3, 5)) + self.y325 = np.arange(30.).reshape((3, 2, 5)) + + # Edge updated test matrix 1 + # array([[ 30, 1, 2, 3, 4, 5, 6, 7, 8, -30], + # [ 30, 11, 12, 13, 14, 15, 16, 17, 18, -30]]) + self.y210_edge_updated = np.arange(20.).reshape((2, 10)) + self.y210_edge_updated[:, 0] = 30 + self.y210_edge_updated[:, -1] = -30 + + # Edge updated test matrix 2 + # array([[ 30, 30], + # [ 2, 3], + # [ 4, 5], + # [ 6, 7], + # [ 8, 9], + # [ 10, 11], + # [ 12, 13], + # [ 14, 15], + # [ 16, 17], + # [-30, -30]]) + self.y102_edge_updated = np.arange(20.).reshape((10, 2)) + self.y102_edge_updated[0, :] = 30 + self.y102_edge_updated[-1, :] = -30 + + self.fill_value = -100.0 + + def test_validation(self): + # Make sure that appropriate exceptions are raised when invalid values + # are given to the constructor. + + # These should all work. + for kind in ('nearest', 'nearest-up', 'zero', 'linear', 'slinear', + 'quadratic', 'cubic', 'previous', 'next'): + interp1d(self.x10, self.y10, kind=kind) + interp1d(self.x10, self.y10, kind=kind, fill_value="extrapolate") + interp1d(self.x10, self.y10, kind='linear', fill_value=(-1, 1)) + interp1d(self.x10, self.y10, kind='linear', + fill_value=np.array([-1])) + interp1d(self.x10, self.y10, kind='linear', + fill_value=(-1,)) + interp1d(self.x10, self.y10, kind='linear', + fill_value=-1) + interp1d(self.x10, self.y10, kind='linear', + fill_value=(-1, -1)) + interp1d(self.x10, self.y10, kind=0) + interp1d(self.x10, self.y10, kind=1) + interp1d(self.x10, self.y10, kind=2) + interp1d(self.x10, self.y10, kind=3) + interp1d(self.x10, self.y210, kind='linear', axis=-1, + fill_value=(-1, -1)) + interp1d(self.x2, self.y210, kind='linear', axis=0, + fill_value=np.ones(10)) + interp1d(self.x2, self.y210, kind='linear', axis=0, + fill_value=(np.ones(10), np.ones(10))) + interp1d(self.x2, self.y210, kind='linear', axis=0, + fill_value=(np.ones(10), -1)) + + # x array must be 1D. + assert_raises(ValueError, interp1d, self.x25, self.y10) + + # y array cannot be a scalar. + assert_raises(ValueError, interp1d, self.x10, np.array(0)) + + # Check for x and y arrays having the same length. + assert_raises(ValueError, interp1d, self.x10, self.y2) + assert_raises(ValueError, interp1d, self.x2, self.y10) + assert_raises(ValueError, interp1d, self.x10, self.y102) + interp1d(self.x10, self.y210) + interp1d(self.x10, self.y102, axis=0) + + # Check for x and y having at least 1 element. + assert_raises(ValueError, interp1d, self.x1, self.y10) + assert_raises(ValueError, interp1d, self.x10, self.y1) + + # Bad fill values + assert_raises(ValueError, interp1d, self.x10, self.y10, kind='linear', + fill_value=(-1, -1, -1)) # doesn't broadcast + assert_raises(ValueError, interp1d, self.x10, self.y10, kind='linear', + fill_value=[-1, -1, -1]) # doesn't broadcast + assert_raises(ValueError, interp1d, self.x10, self.y10, kind='linear', + fill_value=np.array((-1, -1, -1))) # doesn't broadcast + assert_raises(ValueError, interp1d, self.x10, self.y10, kind='linear', + fill_value=[[-1]]) # doesn't broadcast + assert_raises(ValueError, interp1d, self.x10, self.y10, kind='linear', + fill_value=[-1, -1]) # doesn't broadcast + assert_raises(ValueError, interp1d, self.x10, self.y10, kind='linear', + fill_value=np.array([])) # doesn't broadcast + assert_raises(ValueError, interp1d, self.x10, self.y10, kind='linear', + fill_value=()) # doesn't broadcast + assert_raises(ValueError, interp1d, self.x2, self.y210, kind='linear', + axis=0, fill_value=[-1, -1]) # doesn't broadcast + assert_raises(ValueError, interp1d, self.x2, self.y210, kind='linear', + axis=0, fill_value=(0., [-1, -1])) # above doesn't bc + + def test_init(self): + # Check that the attributes are initialized appropriately by the + # constructor. + assert interp1d(self.x10, self.y10).copy + assert not interp1d(self.x10, self.y10, copy=False).copy + assert interp1d(self.x10, self.y10).bounds_error + assert not interp1d(self.x10, self.y10, bounds_error=False).bounds_error + assert np.isnan(interp1d(self.x10, self.y10).fill_value) + assert interp1d(self.x10, self.y10, fill_value=3.0).fill_value == 3.0 + assert (interp1d(self.x10, self.y10, fill_value=(1.0, 2.0)).fill_value == + (1.0, 2.0) + ) + assert interp1d(self.x10, self.y10).axis == 0 + assert interp1d(self.x10, self.y210).axis == 1 + assert interp1d(self.x10, self.y102, axis=0).axis == 0 + xp_assert_equal(interp1d(self.x10, self.y10).x, self.x10) + xp_assert_equal(interp1d(self.x10, self.y10).y, self.y10) + xp_assert_equal(interp1d(self.x10, self.y210).y, self.y210) + + def test_assume_sorted(self): + # Check for unsorted arrays + interp10 = interp1d(self.x10, self.y10) + interp10_unsorted = interp1d(self.x10[::-1], self.y10[::-1]) + + assert_array_almost_equal(interp10_unsorted(self.x10), self.y10) + assert_array_almost_equal(interp10_unsorted(1.2), np.array(1.2)) + assert_array_almost_equal(interp10_unsorted([2.4, 5.6, 6.0]), + interp10([2.4, 5.6, 6.0])) + + # Check assume_sorted keyword (defaults to False) + interp10_assume_kw = interp1d(self.x10[::-1], self.y10[::-1], + assume_sorted=False) + assert_array_almost_equal(interp10_assume_kw(self.x10), self.y10) + + interp10_assume_kw2 = interp1d(self.x10[::-1], self.y10[::-1], + assume_sorted=True) + # Should raise an error for unsorted input if assume_sorted=True + assert_raises(ValueError, interp10_assume_kw2, self.x10) + + # Check that if y is a 2-D array, things are still consistent + interp10_y_2d = interp1d(self.x10, self.y210) + interp10_y_2d_unsorted = interp1d(self.x10[::-1], self.y210[:, ::-1]) + assert_array_almost_equal(interp10_y_2d(self.x10), + interp10_y_2d_unsorted(self.x10)) + + def test_linear(self): + for kind in ['linear', 'slinear']: + self._check_linear(kind) + + def _check_linear(self, kind): + # Check the actual implementation of linear interpolation. + interp10 = interp1d(self.x10, self.y10, kind=kind) + assert_array_almost_equal(interp10(self.x10), self.y10) + assert_array_almost_equal(interp10(1.2), np.array(1.2)) + assert_array_almost_equal(interp10([2.4, 5.6, 6.0]), + np.array([2.4, 5.6, 6.0])) + + # test fill_value="extrapolate" + extrapolator = interp1d(self.x10, self.y10, kind=kind, + fill_value='extrapolate') + xp_assert_close(extrapolator([-1., 0, 9, 11]), + np.asarray([-1.0, 0, 9, 11]), rtol=1e-14) + + opts = dict(kind=kind, + fill_value='extrapolate', + bounds_error=True) + assert_raises(ValueError, interp1d, self.x10, self.y10, **opts) + + def test_linear_dtypes(self): + # regression test for gh-5898, where 1D linear interpolation has been + # delegated to numpy.interp for all float dtypes, and the latter was + # not handling e.g. np.float128. + for dtyp in [np.float16, + np.float32, + np.float64, + np.longdouble]: + x = np.arange(8, dtype=dtyp) + y = x + yp = interp1d(x, y, kind='linear')(x) + assert yp.dtype == dtyp + xp_assert_close(yp, y, atol=1e-15) + + # regression test for gh-14531, where 1D linear interpolation has been + # has been extended to delegate to numpy.interp for integer dtypes + x = [0, 1, 2] + y = [np.nan, 0, 1] + yp = interp1d(x, y)(x) + xp_assert_close(yp, y, atol=1e-15) + + def test_slinear_dtypes(self): + # regression test for gh-7273: 1D slinear interpolation fails with + # float32 inputs + dt_r = [np.float16, np.float32, np.float64] + dt_rc = dt_r + [np.complex64, np.complex128] + spline_kinds = ['slinear', 'zero', 'quadratic', 'cubic'] + for dtx in dt_r: + x = np.arange(0, 10, dtype=dtx) + for dty in dt_rc: + y = np.exp(-x/3.0).astype(dty) + for dtn in dt_r: + xnew = x.astype(dtn) + for kind in spline_kinds: + f = interp1d(x, y, kind=kind, bounds_error=False) + xp_assert_close(f(xnew), y, atol=1e-7, + check_dtype=False, + err_msg=f"{dtx}, {dty} {dtn}") + + def test_cubic(self): + # Check the actual implementation of spline interpolation. + interp10 = interp1d(self.x10, self.y10, kind='cubic') + assert_array_almost_equal(interp10(self.x10), self.y10) + assert_array_almost_equal(interp10(1.2), np.array(1.2)) + assert_array_almost_equal(interp10(1.5), np.array(1.5)) + assert_array_almost_equal(interp10([2.4, 5.6, 6.0]), + np.array([2.4, 5.6, 6.0]),) + + def test_nearest(self): + # Check the actual implementation of nearest-neighbour interpolation. + # Nearest asserts that half-integer case (1.5) rounds down to 1 + interp10 = interp1d(self.x10, self.y10, kind='nearest') + assert_array_almost_equal(interp10(self.x10), self.y10) + assert_array_almost_equal(interp10(1.2), np.array(1.)) + assert_array_almost_equal(interp10(1.5), np.array(1.)) + assert_array_almost_equal(interp10([2.4, 5.6, 6.0]), + np.array([2., 6., 6.]),) + + # test fill_value="extrapolate" + extrapolator = interp1d(self.x10, self.y10, kind='nearest', + fill_value='extrapolate') + xp_assert_close(extrapolator([-1., 0, 9, 11]), + [0.0, 0, 9, 9], rtol=1e-14) + + opts = dict(kind='nearest', + fill_value='extrapolate', + bounds_error=True) + assert_raises(ValueError, interp1d, self.x10, self.y10, **opts) + + def test_nearest_up(self): + # Check the actual implementation of nearest-neighbour interpolation. + # Nearest-up asserts that half-integer case (1.5) rounds up to 2 + interp10 = interp1d(self.x10, self.y10, kind='nearest-up') + assert_array_almost_equal(interp10(self.x10), self.y10) + assert_array_almost_equal(interp10(1.2), np.array(1.)) + assert_array_almost_equal(interp10(1.5), np.array(2.)) + assert_array_almost_equal(interp10([2.4, 5.6, 6.0]), + np.array([2., 6., 6.]),) + + # test fill_value="extrapolate" + extrapolator = interp1d(self.x10, self.y10, kind='nearest-up', + fill_value='extrapolate') + xp_assert_close(extrapolator([-1., 0, 9, 11]), + [0.0, 0, 9, 9], rtol=1e-14) + + opts = dict(kind='nearest-up', + fill_value='extrapolate', + bounds_error=True) + assert_raises(ValueError, interp1d, self.x10, self.y10, **opts) + + def test_previous(self): + # Check the actual implementation of previous interpolation. + interp10 = interp1d(self.x10, self.y10, kind='previous') + assert_array_almost_equal(interp10(self.x10), self.y10) + assert_array_almost_equal(interp10(1.2), np.array(1.)) + assert_array_almost_equal(interp10(1.5), np.array(1.)) + assert_array_almost_equal(interp10([2.4, 5.6, 6.0]), + np.array([2., 5., 6.]),) + + # test fill_value="extrapolate" + extrapolator = interp1d(self.x10, self.y10, kind='previous', + fill_value='extrapolate') + xp_assert_close(extrapolator([-1., 0, 9, 11]), + [np.nan, 0, 9, 9], rtol=1e-14) + + # Tests for gh-9591 + interpolator1D = interp1d(self.x10, self.y10, kind="previous", + fill_value='extrapolate') + xp_assert_close(interpolator1D([-1, -2, 5, 8, 12, 25]), + [np.nan, np.nan, 5, 8, 9, 9]) + + interpolator2D = interp1d(self.x10, self.y210, kind="previous", + fill_value='extrapolate') + xp_assert_close(interpolator2D([-1, -2, 5, 8, 12, 25]), + [[np.nan, np.nan, 5, 8, 9, 9], + [np.nan, np.nan, 15, 18, 19, 19]]) + + interpolator2DAxis0 = interp1d(self.x10, self.y102, kind="previous", + axis=0, fill_value='extrapolate') + xp_assert_close(interpolator2DAxis0([-2, 5, 12]), + [[np.nan, np.nan], + [10, 11], + [18, 19]]) + + opts = dict(kind='previous', + fill_value='extrapolate', + bounds_error=True) + assert_raises(ValueError, interp1d, self.x10, self.y10, **opts) + + # Tests for gh-16813 + interpolator1D = interp1d([0, 1, 2], + [0, 1, -1], kind="previous", + fill_value='extrapolate', + assume_sorted=True) + xp_assert_close(interpolator1D([-2, -1, 0, 1, 2, 3, 5]), + [np.nan, np.nan, 0, 1, -1, -1, -1]) + + interpolator1D = interp1d([2, 0, 1], # x is not ascending + [-1, 0, 1], kind="previous", + fill_value='extrapolate', + assume_sorted=False) + xp_assert_close(interpolator1D([-2, -1, 0, 1, 2, 3, 5]), + [np.nan, np.nan, 0, 1, -1, -1, -1]) + + interpolator2D = interp1d(self.x10, self.y210_edge_updated, + kind="previous", + fill_value='extrapolate') + xp_assert_close(interpolator2D([-1, -2, 5, 8, 12, 25]), + [[np.nan, np.nan, 5, 8, -30, -30], + [np.nan, np.nan, 15, 18, -30, -30]]) + + interpolator2DAxis0 = interp1d(self.x10, self.y102_edge_updated, + kind="previous", + axis=0, fill_value='extrapolate') + xp_assert_close(interpolator2DAxis0([-2, 5, 12]), + [[np.nan, np.nan], + [10, 11], + [-30, -30]]) + + def test_next(self): + # Check the actual implementation of next interpolation. + interp10 = interp1d(self.x10, self.y10, kind='next') + assert_array_almost_equal(interp10(self.x10), self.y10) + assert_array_almost_equal(interp10(1.2), np.array(2.)) + assert_array_almost_equal(interp10(1.5), np.array(2.)) + assert_array_almost_equal(interp10([2.4, 5.6, 6.0]), + np.array([3., 6., 6.]),) + + # test fill_value="extrapolate" + extrapolator = interp1d(self.x10, self.y10, kind='next', + fill_value='extrapolate') + xp_assert_close(extrapolator([-1., 0, 9, 11]), + [0, 0, 9, np.nan], rtol=1e-14) + + # Tests for gh-9591 + interpolator1D = interp1d(self.x10, self.y10, kind="next", + fill_value='extrapolate') + xp_assert_close(interpolator1D([-1, -2, 5, 8, 12, 25]), + [0, 0, 5, 8, np.nan, np.nan]) + + interpolator2D = interp1d(self.x10, self.y210, kind="next", + fill_value='extrapolate') + xp_assert_close(interpolator2D([-1, -2, 5, 8, 12, 25]), + [[0, 0, 5, 8, np.nan, np.nan], + [10, 10, 15, 18, np.nan, np.nan]]) + + interpolator2DAxis0 = interp1d(self.x10, self.y102, kind="next", + axis=0, fill_value='extrapolate') + xp_assert_close(interpolator2DAxis0([-2, 5, 12]), + [[0, 1], + [10, 11], + [np.nan, np.nan]]) + + opts = dict(kind='next', + fill_value='extrapolate', + bounds_error=True) + assert_raises(ValueError, interp1d, self.x10, self.y10, **opts) + + # Tests for gh-16813 + interpolator1D = interp1d([0, 1, 2], + [0, 1, -1], kind="next", + fill_value='extrapolate', + assume_sorted=True) + xp_assert_close(interpolator1D([-2, -1, 0, 1, 2, 3, 5]), + [0, 0, 0, 1, -1, np.nan, np.nan]) + + interpolator1D = interp1d([2, 0, 1], # x is not ascending + [-1, 0, 1], kind="next", + fill_value='extrapolate', + assume_sorted=False) + xp_assert_close(interpolator1D([-2, -1, 0, 1, 2, 3, 5]), + [0, 0, 0, 1, -1, np.nan, np.nan]) + + interpolator2D = interp1d(self.x10, self.y210_edge_updated, + kind="next", + fill_value='extrapolate') + xp_assert_close(interpolator2D([-1, -2, 5, 8, 12, 25]), + [[30, 30, 5, 8, np.nan, np.nan], + [30, 30, 15, 18, np.nan, np.nan]]) + + interpolator2DAxis0 = interp1d(self.x10, self.y102_edge_updated, + kind="next", + axis=0, fill_value='extrapolate') + xp_assert_close(interpolator2DAxis0([-2, 5, 12]), + [[30, 30], + [10, 11], + [np.nan, np.nan]]) + + def test_zero(self): + # Check the actual implementation of zero-order spline interpolation. + interp10 = interp1d(self.x10, self.y10, kind='zero') + assert_array_almost_equal(interp10(self.x10), self.y10) + assert_array_almost_equal(interp10(1.2), np.array(1.)) + assert_array_almost_equal(interp10(1.5), np.array(1.)) + assert_array_almost_equal(interp10([2.4, 5.6, 6.0]), + np.array([2., 5., 6.])) + + def bounds_check_helper(self, interpolant, test_array, fail_value): + # Asserts that a ValueError is raised and that the error message + # contains the value causing this exception. + assert_raises(ValueError, interpolant, test_array) + try: + interpolant(test_array) + except ValueError as err: + assert (f"{fail_value}" in str(err)) + + def _bounds_check(self, kind='linear'): + # Test that our handling of out-of-bounds input is correct. + extrap10 = interp1d(self.x10, self.y10, fill_value=self.fill_value, + bounds_error=False, kind=kind) + + xp_assert_equal(extrap10(11.2), np.array(self.fill_value)) + xp_assert_equal(extrap10(-3.4), np.array(self.fill_value)) + xp_assert_equal(extrap10([[[11.2], [-3.4], [12.6], [19.3]]]), + np.array(self.fill_value), check_shape=False) + xp_assert_equal(extrap10._check_bounds( + np.array([-1.0, 0.0, 5.0, 9.0, 11.0])), + np.array([[True, False, False, False, False], + [False, False, False, False, True]])) + + raises_bounds_error = interp1d(self.x10, self.y10, bounds_error=True, + kind=kind) + + self.bounds_check_helper(raises_bounds_error, -1.0, -1.0) + self.bounds_check_helper(raises_bounds_error, 11.0, 11.0) + self.bounds_check_helper(raises_bounds_error, [0.0, -1.0, 0.0], -1.0) + self.bounds_check_helper(raises_bounds_error, [0.0, 1.0, 21.0], 21.0) + + raises_bounds_error([0.0, 5.0, 9.0]) + + def _bounds_check_int_nan_fill(self, kind='linear'): + x = np.arange(10).astype(int) + y = np.arange(10).astype(int) + c = interp1d(x, y, kind=kind, fill_value=np.nan, bounds_error=False) + yi = c(x - 1) + assert np.isnan(yi[0]) + assert_array_almost_equal(yi, np.r_[np.nan, y[:-1]]) + + def test_bounds(self): + for kind in ('linear', 'cubic', 'nearest', 'previous', 'next', + 'slinear', 'zero', 'quadratic'): + self._bounds_check(kind) + self._bounds_check_int_nan_fill(kind) + + def _check_fill_value(self, kind): + interp = interp1d(self.x10, self.y10, kind=kind, + fill_value=(-100, 100), bounds_error=False) + assert_array_almost_equal(interp(10), np.asarray(100.)) + assert_array_almost_equal(interp(-10), np.asarray(-100.)) + assert_array_almost_equal(interp([-10, 10]), [-100, 100]) + + # Proper broadcasting: + # interp along axis of length 5 + # other dim=(2, 3), (3, 2), (2, 2), or (2,) + + # one singleton fill_value (works for all) + for y in (self.y235, self.y325, self.y225, self.y25): + interp = interp1d(self.x5, y, kind=kind, axis=-1, + fill_value=100, bounds_error=False) + assert_array_almost_equal(interp(10), np.asarray(100.)) + assert_array_almost_equal(interp(-10), np.asarray(100.)) + assert_array_almost_equal(interp([-10, 10]), np.asarray(100.)) + + # singleton lower, singleton upper + interp = interp1d(self.x5, y, kind=kind, axis=-1, + fill_value=(-100, 100), bounds_error=False) + assert_array_almost_equal(interp(10), np.asarray(100.)) + assert_array_almost_equal(interp(-10), np.asarray(-100.)) + if y.ndim == 3: + result = [[[-100, 100]] * y.shape[1]] * y.shape[0] + else: + result = [[-100, 100]] * y.shape[0] + assert_array_almost_equal(interp([-10, 10]), result) + + # one broadcastable (3,) fill_value + fill_value = [100, 200, 300] + for y in (self.y325, self.y225): + assert_raises(ValueError, interp1d, self.x5, y, kind=kind, + axis=-1, fill_value=fill_value, bounds_error=False) + interp = interp1d(self.x5, self.y235, kind=kind, axis=-1, + fill_value=fill_value, bounds_error=False) + assert_array_almost_equal(interp(10), [[100, 200, 300]] * 2) + assert_array_almost_equal(interp(-10), [[100, 200, 300]] * 2) + assert_array_almost_equal(interp([-10, 10]), [[[100, 100], + [200, 200], + [300, 300]]] * 2) + + # one broadcastable (2,) fill_value + fill_value = [100, 200] + assert_raises(ValueError, interp1d, self.x5, self.y235, kind=kind, + axis=-1, fill_value=fill_value, bounds_error=False) + for y in (self.y225, self.y325, self.y25): + interp = interp1d(self.x5, y, kind=kind, axis=-1, + fill_value=fill_value, bounds_error=False) + result = [100, 200] + if y.ndim == 3: + result = [result] * y.shape[0] + assert_array_almost_equal(interp(10), result) + assert_array_almost_equal(interp(-10), result) + result = [[100, 100], [200, 200]] + if y.ndim == 3: + result = [result] * y.shape[0] + assert_array_almost_equal(interp([-10, 10]), result) + + # broadcastable (3,) lower, singleton upper + fill_value = (np.array([-100, -200, -300]), 100) + for y in (self.y325, self.y225): + assert_raises(ValueError, interp1d, self.x5, y, kind=kind, + axis=-1, fill_value=fill_value, bounds_error=False) + interp = interp1d(self.x5, self.y235, kind=kind, axis=-1, + fill_value=fill_value, bounds_error=False) + assert_array_almost_equal(interp(10), np.asarray(100.)) + assert_array_almost_equal(interp(-10), [[-100, -200, -300]] * 2) + assert_array_almost_equal(interp([-10, 10]), [[[-100, 100], + [-200, 100], + [-300, 100]]] * 2) + + # broadcastable (2,) lower, singleton upper + fill_value = (np.array([-100, -200]), 100) + assert_raises(ValueError, interp1d, self.x5, self.y235, kind=kind, + axis=-1, fill_value=fill_value, bounds_error=False) + for y in (self.y225, self.y325, self.y25): + interp = interp1d(self.x5, y, kind=kind, axis=-1, + fill_value=fill_value, bounds_error=False) + assert_array_almost_equal(interp(10), np.asarray(100)) + result = [-100, -200] + if y.ndim == 3: + result = [result] * y.shape[0] + assert_array_almost_equal(interp(-10), result) + result = [[-100, 100], [-200, 100]] + if y.ndim == 3: + result = [result] * y.shape[0] + assert_array_almost_equal(interp([-10, 10]), result) + + # broadcastable (3,) lower, broadcastable (3,) upper + fill_value = ([-100, -200, -300], [100, 200, 300]) + for y in (self.y325, self.y225): + assert_raises(ValueError, interp1d, self.x5, y, kind=kind, + axis=-1, fill_value=fill_value, bounds_error=False) + for ii in range(2): # check ndarray as well as list here + if ii == 1: + fill_value = tuple(np.array(f) for f in fill_value) + interp = interp1d(self.x5, self.y235, kind=kind, axis=-1, + fill_value=fill_value, bounds_error=False) + assert_array_almost_equal(interp(10), [[100, 200, 300]] * 2) + assert_array_almost_equal(interp(-10), [[-100, -200, -300]] * 2) + assert_array_almost_equal(interp([-10, 10]), [[[-100, 100], + [-200, 200], + [-300, 300]]] * 2) + # broadcastable (2,) lower, broadcastable (2,) upper + fill_value = ([-100, -200], [100, 200]) + assert_raises(ValueError, interp1d, self.x5, self.y235, kind=kind, + axis=-1, fill_value=fill_value, bounds_error=False) + for y in (self.y325, self.y225, self.y25): + interp = interp1d(self.x5, y, kind=kind, axis=-1, + fill_value=fill_value, bounds_error=False) + result = [100, 200] + if y.ndim == 3: + result = [result] * y.shape[0] + assert_array_almost_equal(interp(10), result) + result = [-100, -200] + if y.ndim == 3: + result = [result] * y.shape[0] + assert_array_almost_equal(interp(-10), result) + result = [[-100, 100], [-200, 200]] + if y.ndim == 3: + result = [result] * y.shape[0] + assert_array_almost_equal(interp([-10, 10]), result) + + # one broadcastable (2, 2) array-like + fill_value = [[100, 200], [1000, 2000]] + for y in (self.y235, self.y325, self.y25): + assert_raises(ValueError, interp1d, self.x5, y, kind=kind, + axis=-1, fill_value=fill_value, bounds_error=False) + for ii in range(2): + if ii == 1: + fill_value = np.array(fill_value) + interp = interp1d(self.x5, self.y225, kind=kind, axis=-1, + fill_value=fill_value, bounds_error=False) + assert_array_almost_equal(interp(10), [[100, 200], [1000, 2000]]) + assert_array_almost_equal(interp(-10), [[100, 200], [1000, 2000]]) + assert_array_almost_equal(interp([-10, 10]), [[[100, 100], + [200, 200]], + [[1000, 1000], + [2000, 2000]]]) + + # broadcastable (2, 2) lower, broadcastable (2, 2) upper + fill_value = ([[-100, -200], [-1000, -2000]], + [[100, 200], [1000, 2000]]) + for y in (self.y235, self.y325, self.y25): + assert_raises(ValueError, interp1d, self.x5, y, kind=kind, + axis=-1, fill_value=fill_value, bounds_error=False) + for ii in range(2): + if ii == 1: + fill_value = (np.array(fill_value[0]), np.array(fill_value[1])) + interp = interp1d(self.x5, self.y225, kind=kind, axis=-1, + fill_value=fill_value, bounds_error=False) + assert_array_almost_equal(interp(10), [[100, 200], [1000, 2000]]) + assert_array_almost_equal(interp(-10), [[-100, -200], + [-1000, -2000]]) + assert_array_almost_equal(interp([-10, 10]), [[[-100, 100], + [-200, 200]], + [[-1000, 1000], + [-2000, 2000]]]) + + def test_fill_value(self): + # test that two-element fill value works + for kind in ('linear', 'nearest', 'cubic', 'slinear', 'quadratic', + 'zero', 'previous', 'next'): + self._check_fill_value(kind) + + def test_fill_value_writeable(self): + # backwards compat: fill_value is a public writeable attribute + interp = interp1d(self.x10, self.y10, fill_value=123.0) + assert interp.fill_value == 123.0 + interp.fill_value = 321.0 + assert interp.fill_value == 321.0 + + def _nd_check_interp(self, kind='linear'): + # Check the behavior when the inputs and outputs are multidimensional. + + # Multidimensional input. + interp10 = interp1d(self.x10, self.y10, kind=kind) + assert_array_almost_equal(interp10(np.array([[3., 5.], [2., 7.]])), + np.array([[3., 5.], [2., 7.]])) + + # Scalar input -> 0-dim scalar array output + assert isinstance(interp10(1.2), np.ndarray) + assert interp10(1.2).shape == () + + # Multidimensional outputs. + interp210 = interp1d(self.x10, self.y210, kind=kind) + assert_array_almost_equal(interp210(1.), np.array([1., 11.])) + assert_array_almost_equal(interp210(np.array([1., 2.])), + np.array([[1., 2.], [11., 12.]])) + + interp102 = interp1d(self.x10, self.y102, axis=0, kind=kind) + assert_array_almost_equal(interp102(1.), np.array([2.0, 3.0])) + assert_array_almost_equal(interp102(np.array([1., 3.])), + np.array([[2., 3.], [6., 7.]])) + + # Both at the same time! + x_new = np.array([[3., 5.], [2., 7.]]) + assert_array_almost_equal(interp210(x_new), + np.array([[[3., 5.], [2., 7.]], + [[13., 15.], [12., 17.]]])) + assert_array_almost_equal(interp102(x_new), + np.array([[[6., 7.], [10., 11.]], + [[4., 5.], [14., 15.]]])) + + def _nd_check_shape(self, kind='linear'): + # Check large N-D output shape + a = [4, 5, 6, 7] + y = np.arange(np.prod(a)).reshape(*a) + for n, s in enumerate(a): + x = np.arange(s) + z = interp1d(x, y, axis=n, kind=kind) + assert_array_almost_equal(z(x), y, err_msg=kind) + + x2 = np.arange(2*3*1).reshape((2,3,1)) / 12. + b = list(a) + b[n:n+1] = [2, 3, 1] + assert z(x2).shape == tuple(b), kind + + def test_nd(self): + for kind in ('linear', 'cubic', 'slinear', 'quadratic', 'nearest', + 'zero', 'previous', 'next'): + self._nd_check_interp(kind) + self._nd_check_shape(kind) + + def _check_complex(self, dtype=np.complex128, kind='linear'): + x = np.array([1, 2.5, 3, 3.1, 4, 6.4, 7.9, 8.0, 9.5, 10]) + y = x * x ** (1 + 2j) + y = y.astype(dtype) + + # simple test + c = interp1d(x, y, kind=kind) + assert_array_almost_equal(y[:-1], c(x)[:-1]) + + # check against interpolating real+imag separately + xi = np.linspace(1, 10, 31) + cr = interp1d(x, y.real, kind=kind) + ci = interp1d(x, y.imag, kind=kind) + assert_array_almost_equal(c(xi).real, cr(xi)) + assert_array_almost_equal(c(xi).imag, ci(xi)) + + def test_complex(self): + for kind in ('linear', 'nearest', 'cubic', 'slinear', 'quadratic', + 'zero', 'previous', 'next'): + self._check_complex(np.complex64, kind) + self._check_complex(np.complex128, kind) + + @pytest.mark.skipif(IS_PYPY, reason="Test not meaningful on PyPy") + def test_circular_refs(self): + # Test interp1d can be automatically garbage collected + x = np.linspace(0, 1) + y = np.linspace(0, 1) + # Confirm interp can be released from memory after use + with assert_deallocated(interp1d, x, y) as interp: + interp([0.1, 0.2]) + del interp + + def test_overflow_nearest(self): + # Test that the x range doesn't overflow when given integers as input + for kind in ('nearest', 'previous', 'next'): + x = np.array([0, 50, 127], dtype=np.int8) + ii = interp1d(x, x, kind=kind) + assert_array_almost_equal(ii(x), x) + + def test_local_nans(self): + # check that for local interpolation kinds (slinear, zero) a single nan + # only affects its local neighborhood + x = np.arange(10).astype(float) + y = x.copy() + y[6] = np.nan + for kind in ('zero', 'slinear'): + ir = interp1d(x, y, kind=kind) + vals = ir([4.9, 7.0]) + assert np.isfinite(vals).all() + + def test_spline_nans(self): + # Backwards compat: a single nan makes the whole spline interpolation + # return nans in an array of the correct shape. And it doesn't raise, + # just quiet nans because of backcompat. + x = np.arange(8).astype(float) + y = x.copy() + yn = y.copy() + yn[3] = np.nan + + for kind in ['quadratic', 'cubic']: + ir = interp1d(x, y, kind=kind) + irn = interp1d(x, yn, kind=kind) + for xnew in (6, [1, 6], [[1, 6], [3, 5]]): + xnew = np.asarray(xnew) + out, outn = ir(x), irn(x) + assert np.isnan(outn).all() + assert out.shape == outn.shape + + def test_all_nans(self): + # regression test for gh-11637: interp1d core dumps with all-nan `x` + x = np.ones(10) * np.nan + y = np.arange(10) + with assert_raises(ValueError): + interp1d(x, y, kind='cubic') + + def test_read_only(self): + x = np.arange(0, 10) + y = np.exp(-x / 3.0) + xnew = np.arange(0, 9, 0.1) + # Check both read-only and not read-only: + for xnew_writeable in (True, False): + xnew.flags.writeable = xnew_writeable + x.flags.writeable = False + for kind in ('linear', 'nearest', 'zero', 'slinear', 'quadratic', + 'cubic'): + f = interp1d(x, y, kind=kind) + vals = f(xnew) + assert np.isfinite(vals).all() + + @pytest.mark.parametrize( + "kind", ("linear", "nearest", "nearest-up", "previous", "next") + ) + def test_single_value(self, kind): + # https://github.com/scipy/scipy/issues/4043 + f = interp1d([1.5], [6], kind=kind, bounds_error=False, + fill_value=(2, 10)) + xp_assert_equal(f([1, 1.5, 2]), np.asarray([2.0, 6, 10])) + # check still error if bounds_error=True + f = interp1d([1.5], [6], kind=kind, bounds_error=True) + with assert_raises(ValueError, match="x_new is above"): + f(2.0) + + +class TestLagrange: + + def test_lagrange(self): + p = poly1d([5,2,1,4,3]) + xs = np.arange(len(p.coeffs)) + ys = p(xs) + pl = lagrange(xs,ys) + assert_array_almost_equal(p.coeffs,pl.coeffs) + + +class TestAkima1DInterpolator: + def test_eval(self): + x = np.arange(0., 11.) + y = np.array([0., 2., 1., 3., 2., 6., 5.5, 5.5, 2.7, 5.1, 3.]) + ak = Akima1DInterpolator(x, y) + xi = np.array([0., 0.5, 1., 1.5, 2.5, 3.5, 4.5, 5.1, 6.5, 7.2, + 8.6, 9.9, 10.]) + yi = np.array([0., 1.375, 2., 1.5, 1.953125, 2.484375, + 4.1363636363636366866103344, 5.9803623910336236590978842, + 5.5067291516462386624652936, 5.2031367459745245795943447, + 4.1796554159017080820603951, 3.4110386597938129327189927, + 3.]) + xp_assert_close(ak(xi), yi) + + def test_eval_mod(self): + # Reference values generated with the following MATLAB code: + # format longG + # x = 0:10; y = [0. 2. 1. 3. 2. 6. 5.5 5.5 2.7 5.1 3.]; + # xi = [0. 0.5 1. 1.5 2.5 3.5 4.5 5.1 6.5 7.2 8.6 9.9 10.]; + # makima(x, y, xi) + x = np.arange(0., 11.) + y = np.array([0., 2., 1., 3., 2., 6., 5.5, 5.5, 2.7, 5.1, 3.]) + ak = Akima1DInterpolator(x, y, method="makima") + xi = np.array([0., 0.5, 1., 1.5, 2.5, 3.5, 4.5, 5.1, 6.5, 7.2, + 8.6, 9.9, 10.]) + yi = np.array([ + 0.0, 1.34471153846154, 2.0, 1.44375, 1.94375, 2.51939102564103, + 4.10366931918656, 5.98501550899192, 5.51756330960439, 5.1757231914014, + 4.12326636931311, 3.32931513157895, 3.0]) + xp_assert_close(ak(xi), yi) + + def test_eval_2d(self): + x = np.arange(0., 11.) + y = np.array([0., 2., 1., 3., 2., 6., 5.5, 5.5, 2.7, 5.1, 3.]) + y = np.column_stack((y, 2. * y)) + ak = Akima1DInterpolator(x, y) + xi = np.array([0., 0.5, 1., 1.5, 2.5, 3.5, 4.5, 5.1, 6.5, 7.2, + 8.6, 9.9, 10.]) + yi = np.array([0., 1.375, 2., 1.5, 1.953125, 2.484375, + 4.1363636363636366866103344, + 5.9803623910336236590978842, + 5.5067291516462386624652936, + 5.2031367459745245795943447, + 4.1796554159017080820603951, + 3.4110386597938129327189927, 3.]) + yi = np.column_stack((yi, 2. * yi)) + xp_assert_close(ak(xi), yi) + + def test_eval_3d(self): + x = np.arange(0., 11.) + y_ = np.array([0., 2., 1., 3., 2., 6., 5.5, 5.5, 2.7, 5.1, 3.]) + y = np.empty((11, 2, 2)) + y[:, 0, 0] = y_ + y[:, 1, 0] = 2. * y_ + y[:, 0, 1] = 3. * y_ + y[:, 1, 1] = 4. * y_ + ak = Akima1DInterpolator(x, y) + xi = np.array([0., 0.5, 1., 1.5, 2.5, 3.5, 4.5, 5.1, 6.5, 7.2, + 8.6, 9.9, 10.]) + yi = np.empty((13, 2, 2)) + yi_ = np.array([0., 1.375, 2., 1.5, 1.953125, 2.484375, + 4.1363636363636366866103344, + 5.9803623910336236590978842, + 5.5067291516462386624652936, + 5.2031367459745245795943447, + 4.1796554159017080820603951, + 3.4110386597938129327189927, 3.]) + yi[:, 0, 0] = yi_ + yi[:, 1, 0] = 2. * yi_ + yi[:, 0, 1] = 3. * yi_ + yi[:, 1, 1] = 4. * yi_ + xp_assert_close(ak(xi), yi) + + def test_degenerate_case_multidimensional(self): + # This test is for issue #5683. + x = np.array([0, 1, 2]) + y = np.vstack((x, x**2)).T + ak = Akima1DInterpolator(x, y) + x_eval = np.array([0.5, 1.5]) + y_eval = ak(x_eval) + xp_assert_close(y_eval, np.vstack((x_eval, x_eval**2)).T) + + def test_extend(self): + x = np.arange(0., 11.) + y = np.array([0., 2., 1., 3., 2., 6., 5.5, 5.5, 2.7, 5.1, 3.]) + ak = Akima1DInterpolator(x, y) + match = "Extending a 1-D Akima interpolator is not yet implemented" + with pytest.raises(NotImplementedError, match=match): + ak.extend(None, None) + + def test_mod_invalid_method(self): + x = np.arange(0., 11.) + y = np.array([0., 2., 1., 3., 2., 6., 5.5, 5.5, 2.7, 5.1, 3.]) + match = "`method`=invalid is unsupported." + with pytest.raises(NotImplementedError, match=match): + Akima1DInterpolator(x, y, method="invalid") # type: ignore + + def test_extrapolate_attr(self): + # + x = np.linspace(-5, 5, 11) + y = x**2 + x_ext = np.linspace(-10, 10, 17) + y_ext = x_ext**2 + # Testing all extrapolate cases. + ak_true = Akima1DInterpolator(x, y, extrapolate=True) + ak_false = Akima1DInterpolator(x, y, extrapolate=False) + ak_none = Akima1DInterpolator(x, y, extrapolate=None) + # None should default to False; extrapolated points are NaN. + xp_assert_close(ak_false(x_ext), ak_none(x_ext), atol=1e-15) + xp_assert_equal(ak_false(x_ext)[0:4], np.full(4, np.nan)) + xp_assert_equal(ak_false(x_ext)[-4:-1], np.full(3, np.nan)) + # Extrapolation on call and attribute should be equal. + xp_assert_close(ak_false(x_ext, extrapolate=True), ak_true(x_ext), atol=1e-15) + # Testing extrapoation to actual function. + xp_assert_close(y_ext, ak_true(x_ext), atol=1e-15) + + +@pytest.mark.parametrize("method", [Akima1DInterpolator, PchipInterpolator]) +def test_complex(method): + # Complex-valued data deprecated + x = np.arange(0., 11.) + y = np.array([0., 2., 1., 3., 2., 6., 5.5, 5.5, 2.7, 5.1, 3.]) + y = y - 2j*y + msg = "real values" + with pytest.raises(ValueError, match=msg): + method(x, y) + + def test_concurrency(self): + # Check that no segfaults appear with concurrent access to Akima1D + x = np.linspace(-5, 5, 11) + y = x**2 + x_ext = np.linspace(-10, 10, 17) + ak = Akima1DInterpolator(x, y, extrapolate=True) + + def worker_fn(_, ak, x_ext): + ak(x_ext) + + _run_concurrent_barrier(10, worker_fn, ak, x_ext) + + +class TestPPolyCommon: + # test basic functionality for PPoly and BPoly + def test_sort_check(self): + c = np.array([[1, 4], [2, 5], [3, 6]]) + x = np.array([0, 1, 0.5]) + assert_raises(ValueError, PPoly, c, x) + assert_raises(ValueError, BPoly, c, x) + + def test_ctor_c(self): + # wrong shape: `c` must be at least 2D + with assert_raises(ValueError): + PPoly([1, 2], [0, 1]) + + def test_extend(self): + # Test adding new points to the piecewise polynomial + np.random.seed(1234) + + order = 3 + x = np.unique(np.r_[0, 10 * np.random.rand(30), 10]) + c = 2*np.random.rand(order+1, len(x)-1, 2, 3) - 1 + + for cls in (PPoly, BPoly): + pp = cls(c[:,:9], x[:10]) + pp.extend(c[:,9:], x[10:]) + + pp2 = cls(c[:, 10:], x[10:]) + pp2.extend(c[:, :10], x[:10]) + + pp3 = cls(c, x) + + xp_assert_equal(pp.c, pp3.c) + xp_assert_equal(pp.x, pp3.x) + xp_assert_equal(pp2.c, pp3.c) + xp_assert_equal(pp2.x, pp3.x) + + def test_extend_diff_orders(self): + # Test extending polynomial with different order one + np.random.seed(1234) + + x = np.linspace(0, 1, 6) + c = np.random.rand(2, 5) + + x2 = np.linspace(1, 2, 6) + c2 = np.random.rand(4, 5) + + for cls in (PPoly, BPoly): + pp1 = cls(c, x) + pp2 = cls(c2, x2) + + pp_comb = cls(c, x) + pp_comb.extend(c2, x2[1:]) + + # NB. doesn't match to pp1 at the endpoint, because pp1 is not + # continuous with pp2 as we took random coefs. + xi1 = np.linspace(0, 1, 300, endpoint=False) + xi2 = np.linspace(1, 2, 300) + + xp_assert_close(pp1(xi1), pp_comb(xi1)) + xp_assert_close(pp2(xi2), pp_comb(xi2)) + + def test_extend_descending(self): + np.random.seed(0) + + order = 3 + x = np.sort(np.random.uniform(0, 10, 20)) + c = np.random.rand(order + 1, x.shape[0] - 1, 2, 3) + + for cls in (PPoly, BPoly): + p = cls(c, x) + + p1 = cls(c[:, :9], x[:10]) + p1.extend(c[:, 9:], x[10:]) + + p2 = cls(c[:, 10:], x[10:]) + p2.extend(c[:, :10], x[:10]) + + xp_assert_equal(p1.c, p.c) + xp_assert_equal(p1.x, p.x) + xp_assert_equal(p2.c, p.c) + xp_assert_equal(p2.x, p.x) + + def test_shape(self): + np.random.seed(1234) + c = np.random.rand(8, 12, 5, 6, 7) + x = np.sort(np.random.rand(13)) + xp = np.random.rand(3, 4) + for cls in (PPoly, BPoly): + p = cls(c, x) + assert p(xp).shape == (3, 4, 5, 6, 7) + + # 'scalars' + for cls in (PPoly, BPoly): + p = cls(c[..., 0, 0, 0], x) + + assert np.shape(p(0.5)) == () + assert np.shape(p(np.array(0.5))) == () + + assert_raises(ValueError, p, np.array([[0.1, 0.2], [0.4]], dtype=object)) + + def test_concurrency(self): + # Check that no segfaults appear with concurrent access to BPoly, PPoly + c = np.random.rand(8, 12, 5, 6, 7) + x = np.sort(np.random.rand(13)) + xp = np.random.rand(3, 4) + + for cls in (PPoly, BPoly): + interp = cls(c, x) + + def worker_fn(_, interp, xp): + interp(xp) + + _run_concurrent_barrier(10, worker_fn, interp, xp) + + + def test_complex_coef(self): + np.random.seed(12345) + x = np.sort(np.random.random(13)) + c = np.random.random((8, 12)) * (1. + 0.3j) + c_re, c_im = c.real, c.imag + xp = np.random.random(5) + for cls in (PPoly, BPoly): + p, p_re, p_im = cls(c, x), cls(c_re, x), cls(c_im, x) + for nu in [0, 1, 2]: + xp_assert_close(p(xp, nu).real, p_re(xp, nu)) + xp_assert_close(p(xp, nu).imag, p_im(xp, nu)) + + def test_axis(self): + np.random.seed(12345) + c = np.random.rand(3, 4, 5, 6, 7, 8) + c_s = c.shape + xp = np.random.random((1, 2)) + for axis in (0, 1, 2, 3): + m = c.shape[axis+1] + x = np.sort(np.random.rand(m+1)) + for cls in (PPoly, BPoly): + p = cls(c, x, axis=axis) + assert p.c.shape == c_s[axis:axis+2] + c_s[:axis] + c_s[axis+2:] + res = p(xp) + targ_shape = c_s[:axis] + xp.shape + c_s[2+axis:] + assert res.shape == targ_shape + + # deriv/antideriv does not drop the axis + for p1 in [cls(c, x, axis=axis).derivative(), + cls(c, x, axis=axis).derivative(2), + cls(c, x, axis=axis).antiderivative(), + cls(c, x, axis=axis).antiderivative(2)]: + assert p1.axis == p.axis + + # c array needs two axes for the coefficients and intervals, so + # 0 <= axis < c.ndim-1; raise otherwise + for axis in (-1, 4, 5, 6): + for cls in (BPoly, PPoly): + assert_raises(ValueError, cls, **dict(c=c, x=x, axis=axis)) + + +class TestPolySubclassing: + class P(PPoly): + pass + + class B(BPoly): + pass + + def _make_polynomials(self): + np.random.seed(1234) + x = np.sort(np.random.random(3)) + c = np.random.random((4, 2)) + return self.P(c, x), self.B(c, x) + + def test_derivative(self): + pp, bp = self._make_polynomials() + for p in (pp, bp): + pd = p.derivative() + assert p.__class__ == pd.__class__ + + ppa = pp.antiderivative() + assert pp.__class__ == ppa.__class__ + + def test_from_spline(self): + np.random.seed(1234) + x = np.sort(np.r_[0, np.random.rand(11), 1]) + y = np.random.rand(len(x)) + + spl = splrep(x, y, s=0) + pp = self.P.from_spline(spl) + assert pp.__class__ == self.P + + def test_conversions(self): + pp, bp = self._make_polynomials() + + pp1 = self.P.from_bernstein_basis(bp) + assert pp1.__class__ == self.P + + bp1 = self.B.from_power_basis(pp) + assert bp1.__class__ == self.B + + def test_from_derivatives(self): + x = [0, 1, 2] + y = [[1], [2], [3]] + bp = self.B.from_derivatives(x, y) + assert bp.__class__ == self.B + + +class TestPPoly: + def test_simple(self): + c = np.array([[1, 4], [2, 5], [3, 6]]) + x = np.array([0, 0.5, 1]) + p = PPoly(c, x) + xp_assert_close(p(0.3), np.asarray(1*0.3**2 + 2*0.3 + 3)) + xp_assert_close(p(0.7), np.asarray(4*(0.7-0.5)**2 + 5*(0.7-0.5) + 6)) + + def test_periodic(self): + c = np.array([[1, 4], [2, 5], [3, 6]]) + x = np.array([0, 0.5, 1]) + p = PPoly(c, x, extrapolate='periodic') + + xp_assert_close(p(1.3), + np.asarray(1 * 0.3 ** 2 + 2 * 0.3 + 3)) + xp_assert_close(p(-0.3), + np.asarray(4 * (0.7 - 0.5) ** 2 + 5 * (0.7 - 0.5) + 6)) + + xp_assert_close(p(1.3, 1), np.asarray(2 * 0.3 + 2)) + xp_assert_close(p(-0.3, 1), np.asarray(8 * (0.7 - 0.5) + 5)) + + def test_read_only(self): + c = np.array([[1, 4], [2, 5], [3, 6]]) + x = np.array([0, 0.5, 1]) + xnew = np.array([0, 0.1, 0.2]) + PPoly(c, x, extrapolate='periodic') + + for writeable in (True, False): + x.flags.writeable = writeable + c.flags.writeable = writeable + f = PPoly(c, x) + vals = f(xnew) + assert np.isfinite(vals).all() + + def test_descending(self): + def binom_matrix(power): + n = np.arange(power + 1).reshape(-1, 1) + k = np.arange(power + 1) + B = binom(n, k) + return B[::-1, ::-1] + + rng = np.random.RandomState(0) + + power = 3 + for m in [10, 20, 30]: + x = np.sort(rng.uniform(0, 10, m + 1)) + ca = rng.uniform(-2, 2, size=(power + 1, m)) + + h = np.diff(x) + h_powers = h[None, :] ** np.arange(power + 1)[::-1, None] + B = binom_matrix(power) + cap = ca * h_powers + cdp = np.dot(B.T, cap) + cd = cdp / h_powers + + pa = PPoly(ca, x, extrapolate=True) + pd = PPoly(cd[:, ::-1], x[::-1], extrapolate=True) + + x_test = rng.uniform(-10, 20, 100) + xp_assert_close(pa(x_test), pd(x_test), rtol=1e-13) + xp_assert_close(pa(x_test, 1), pd(x_test, 1), rtol=1e-13) + + pa_d = pa.derivative() + pd_d = pd.derivative() + + xp_assert_close(pa_d(x_test), pd_d(x_test), rtol=1e-13) + + # Antiderivatives won't be equal because fixing continuity is + # done in the reverse order, but surely the differences should be + # equal. + pa_i = pa.antiderivative() + pd_i = pd.antiderivative() + for a, b in rng.uniform(-10, 20, (5, 2)): + int_a = pa.integrate(a, b) + int_d = pd.integrate(a, b) + xp_assert_close(int_a, int_d, rtol=1e-13) + xp_assert_close(pa_i(b) - pa_i(a), pd_i(b) - pd_i(a), + rtol=1e-13) + + roots_d = pd.roots() + roots_a = pa.roots() + xp_assert_close(roots_a, np.sort(roots_d), rtol=1e-12) + + def test_multi_shape(self): + c = np.random.rand(6, 2, 1, 2, 3) + x = np.array([0, 0.5, 1]) + p = PPoly(c, x) + assert p.x.shape == x.shape + assert p.c.shape == c.shape + assert p(0.3).shape == c.shape[2:] + + assert p(np.random.rand(5, 6)).shape == (5, 6) + c.shape[2:] + + dp = p.derivative() + assert dp.c.shape == (5, 2, 1, 2, 3) + ip = p.antiderivative() + assert ip.c.shape == (7, 2, 1, 2, 3) + + def test_construct_fast(self): + np.random.seed(1234) + c = np.array([[1, 4], [2, 5], [3, 6]], dtype=float) + x = np.array([0, 0.5, 1]) + p = PPoly.construct_fast(c, x) + xp_assert_close(p(0.3), np.asarray(1*0.3**2 + 2*0.3 + 3)) + xp_assert_close(p(0.7), np.asarray(4*(0.7-0.5)**2 + 5*(0.7-0.5) + 6)) + + def test_vs_alternative_implementations(self): + rng = np.random.RandomState(1234) + c = rng.rand(3, 12, 22) + x = np.sort(np.r_[0, rng.rand(11), 1]) + + p = PPoly(c, x) + + xp = np.r_[0.3, 0.5, 0.33, 0.6] + expected = _ppoly_eval_1(c, x, xp) + xp_assert_close(p(xp), expected) + + expected = _ppoly_eval_2(c[:,:,0], x, xp) + xp_assert_close(p(xp)[:, 0], expected) + + def test_from_spline(self): + rng = np.random.RandomState(1234) + x = np.sort(np.r_[0, rng.rand(11), 1]) + y = rng.rand(len(x)) + + spl = splrep(x, y, s=0) + pp = PPoly.from_spline(spl) + + xi = np.linspace(0, 1, 200) + xp_assert_close(pp(xi), splev(xi, spl)) + + # make sure .from_spline accepts BSpline objects + b = BSpline(*spl) + ppp = PPoly.from_spline(b) + xp_assert_close(ppp(xi), b(xi)) + + # BSpline's extrapolate attribute propagates unless overridden + t, c, k = spl + for extrap in (None, True, False): + b = BSpline(t, c, k, extrapolate=extrap) + p = PPoly.from_spline(b) + assert p.extrapolate == b.extrapolate + + def test_derivative_simple(self): + np.random.seed(1234) + c = np.array([[4, 3, 2, 1]]).T + dc = np.array([[3*4, 2*3, 2]]).T + ddc = np.array([[2*3*4, 1*2*3]]).T + x = np.array([0, 1]) + + pp = PPoly(c, x) + dpp = PPoly(dc, x) + ddpp = PPoly(ddc, x) + + xp_assert_close(pp.derivative().c, dpp.c) + xp_assert_close(pp.derivative(2).c, ddpp.c) + + def test_derivative_eval(self): + rng = np.random.RandomState(1234) + x = np.sort(np.r_[0, rng.rand(11), 1]) + y = rng.rand(len(x)) + + spl = splrep(x, y, s=0) + pp = PPoly.from_spline(spl) + + xi = np.linspace(0, 1, 200) + for dx in range(0, 3): + xp_assert_close(pp(xi, dx), splev(xi, spl, dx)) + + def test_derivative(self): + rng = np.random.RandomState(1234) + x = np.sort(np.r_[0, rng.rand(11), 1]) + y = rng.rand(len(x)) + + spl = splrep(x, y, s=0, k=5) + pp = PPoly.from_spline(spl) + + xi = np.linspace(0, 1, 200) + for dx in range(0, 10): + xp_assert_close(pp(xi, dx), pp.derivative(dx)(xi), + err_msg="dx=%d" % (dx,)) + + def test_antiderivative_of_constant(self): + # https://github.com/scipy/scipy/issues/4216 + p = PPoly([[1.]], [0, 1]) + xp_assert_equal(p.antiderivative().c, PPoly([[1], [0]], [0, 1]).c) + xp_assert_equal(p.antiderivative().x, PPoly([[1], [0]], [0, 1]).x) + + def test_antiderivative_regression_4355(self): + # https://github.com/scipy/scipy/issues/4355 + p = PPoly([[1., 0.5]], [0, 1, 2]) + q = p.antiderivative() + xp_assert_equal(q.c, [[1, 0.5], [0, 1]]) + xp_assert_equal(q.x, [0.0, 1, 2]) + xp_assert_close(p.integrate(0, 2), np.asarray(1.5)) + xp_assert_close(np.asarray(q(2) - q(0)), + np.asarray(1.5)) + + def test_antiderivative_simple(self): + np.random.seed(1234) + # [ p1(x) = 3*x**2 + 2*x + 1, + # p2(x) = 1.6875] + c = np.array([[3, 2, 1], [0, 0, 1.6875]]).T + # [ pp1(x) = x**3 + x**2 + x, + # pp2(x) = 1.6875*(x - 0.25) + pp1(0.25)] + ic = np.array([[1, 1, 1, 0], [0, 0, 1.6875, 0.328125]]).T + # [ ppp1(x) = (1/4)*x**4 + (1/3)*x**3 + (1/2)*x**2, + # ppp2(x) = (1.6875/2)*(x - 0.25)**2 + pp1(0.25)*x + ppp1(0.25)] + iic = np.array([[1/4, 1/3, 1/2, 0, 0], + [0, 0, 1.6875/2, 0.328125, 0.037434895833333336]]).T + x = np.array([0, 0.25, 1]) + + pp = PPoly(c, x) + ipp = pp.antiderivative() + iipp = pp.antiderivative(2) + iipp2 = ipp.antiderivative() + + xp_assert_close(ipp.x, x) + xp_assert_close(ipp.c.T, ic.T) + xp_assert_close(iipp.c.T, iic.T) + xp_assert_close(iipp2.c.T, iic.T) + + def test_antiderivative_vs_derivative(self): + rng = np.random.RandomState(1234) + x = np.linspace(0, 1, 30)**2 + y = rng.rand(len(x)) + spl = splrep(x, y, s=0, k=5) + pp = PPoly.from_spline(spl) + + for dx in range(0, 10): + ipp = pp.antiderivative(dx) + + # check that derivative is inverse op + pp2 = ipp.derivative(dx) + xp_assert_close(pp.c, pp2.c) + + # check continuity + for k in range(dx): + pp2 = ipp.derivative(k) + + r = 1e-13 + endpoint = r*pp2.x[:-1] + (1 - r)*pp2.x[1:] + + xp_assert_close(pp2(pp2.x[1:]), pp2(endpoint), + rtol=1e-7, err_msg="dx=%d k=%d" % (dx, k)) + + def test_antiderivative_vs_spline(self): + rng = np.random.RandomState(1234) + x = np.sort(np.r_[0, rng.rand(11), 1]) + y = rng.rand(len(x)) + + spl = splrep(x, y, s=0, k=5) + pp = PPoly.from_spline(spl) + + for dx in range(0, 10): + pp2 = pp.antiderivative(dx) + spl2 = splantider(spl, dx) + + xi = np.linspace(0, 1, 200) + xp_assert_close(pp2(xi), splev(xi, spl2), + rtol=1e-7) + + def test_antiderivative_continuity(self): + c = np.array([[2, 1, 2, 2], [2, 1, 3, 3]]).T + x = np.array([0, 0.5, 1]) + + p = PPoly(c, x) + ip = p.antiderivative() + + # check continuity + xp_assert_close(ip(0.5 - 1e-9), ip(0.5 + 1e-9), rtol=1e-8) + + # check that only lowest order coefficients were changed + p2 = ip.derivative() + xp_assert_close(p2.c, p.c) + + def test_integrate(self): + rng = np.random.RandomState(1234) + x = np.sort(np.r_[0, rng.rand(11), 1]) + y = rng.rand(len(x)) + + spl = splrep(x, y, s=0, k=5) + pp = PPoly.from_spline(spl) + + a, b = 0.3, 0.9 + ig = pp.integrate(a, b) + + ipp = pp.antiderivative() + xp_assert_close(ig, ipp(b) - ipp(a), check_0d=False) + xp_assert_close(ig, splint(a, b, spl), check_0d=False) + + a, b = -0.3, 0.9 + ig = pp.integrate(a, b, extrapolate=True) + xp_assert_close(ig, ipp(b) - ipp(a), check_0d=False) + + assert np.isnan(pp.integrate(a, b, extrapolate=False)).all() + + def test_integrate_readonly(self): + x = np.array([1, 2, 4]) + c = np.array([[0., 0.], [-1., -1.], [2., -0.], [1., 2.]]) + + for writeable in (True, False): + x.flags.writeable = writeable + + P = PPoly(c, x) + vals = P.integrate(1, 4) + + assert np.isfinite(vals).all() + + def test_integrate_periodic(self): + x = np.array([1, 2, 4]) + c = np.array([[0., 0.], [-1., -1.], [2., -0.], [1., 2.]]) + + P = PPoly(c, x, extrapolate='periodic') + I = P.antiderivative() + + period_int = np.asarray(I(4) - I(1)) + + xp_assert_close(P.integrate(1, 4), period_int) + xp_assert_close(P.integrate(-10, -7), period_int) + xp_assert_close(P.integrate(-10, -4), np.asarray(2 * period_int)) + + xp_assert_close(P.integrate(1.5, 2.5), + np.asarray(I(2.5) - I(1.5))) + xp_assert_close(P.integrate(3.5, 5), + np.asarray(I(2) - I(1) + I(4) - I(3.5))) + xp_assert_close(P.integrate(3.5 + 12, 5 + 12), + np.asarray(I(2) - I(1) + I(4) - I(3.5))) + xp_assert_close(P.integrate(3.5, 5 + 12), + np.asarray(I(2) - I(1) + I(4) - I(3.5) + 4 * period_int)) + xp_assert_close(P.integrate(0, -1), + np.asarray(I(2) - I(3))) + xp_assert_close(P.integrate(-9, -10), + np.asarray(I(2) - I(3))) + xp_assert_close(P.integrate(0, -10), + np.asarray(I(2) - I(3) - 3 * period_int)) + + def test_roots(self): + x = np.linspace(0, 1, 31)**2 + y = np.sin(30*x) + + spl = splrep(x, y, s=0, k=3) + pp = PPoly.from_spline(spl) + + r = pp.roots() + r = r[(r >= 0 - 1e-15) & (r <= 1 + 1e-15)] + xp_assert_close(r, sproot(spl), atol=1e-15) + + def test_roots_idzero(self): + # Roots for piecewise polynomials with identically zero + # sections. + c = np.array([[-1, 0.25], [0, 0], [-1, 0.25]]).T + x = np.array([0, 0.4, 0.6, 1.0]) + + pp = PPoly(c, x) + xp_assert_equal(pp.roots(), + [0.25, 0.4, np.nan, 0.6 + 0.25]) + + # ditto for p.solve(const) with sections identically equal const + const = 2. + c1 = c.copy() + c1[1, :] += const + pp1 = PPoly(c1, x) + + xp_assert_equal(pp1.solve(const), + [0.25, 0.4, np.nan, 0.6 + 0.25]) + + def test_roots_all_zero(self): + # test the code path for the polynomial being identically zero everywhere + c = [[0], [0]] + x = [0, 1] + p = PPoly(c, x) + xp_assert_equal(p.roots(), [0, np.nan]) + xp_assert_equal(p.solve(0), [0, np.nan]) + xp_assert_equal(p.solve(1), []) + + c = [[0, 0], [0, 0]] + x = [0, 1, 2] + p = PPoly(c, x) + xp_assert_equal(p.roots(), [0, np.nan, 1, np.nan]) + xp_assert_equal(p.solve(0), [0, np.nan, 1, np.nan]) + xp_assert_equal(p.solve(1), []) + + def test_roots_repeated(self): + # Check roots repeated in multiple sections are reported only + # once. + + # [(x + 1)**2 - 1, -x**2] ; x == 0 is a repeated root + c = np.array([[1, 0, -1], [-1, 0, 0]]).T + x = np.array([-1, 0, 1]) + + pp = PPoly(c, x) + xp_assert_equal(pp.roots(), np.asarray([-2.0, 0.0])) + xp_assert_equal(pp.roots(extrapolate=False), np.asarray([0.0])) + + def test_roots_discont(self): + # Check that a discontinuity across zero is reported as root + c = np.array([[1], [-1]]).T + x = np.array([0, 0.5, 1]) + pp = PPoly(c, x) + xp_assert_equal(pp.roots(), np.asarray([0.5])) + xp_assert_equal(pp.roots(discontinuity=False), np.asarray([])) + + # ditto for a discontinuity across y: + xp_assert_equal(pp.solve(0.5), np.asarray([0.5])) + xp_assert_equal(pp.solve(0.5, discontinuity=False), np.asarray([])) + + xp_assert_equal(pp.solve(1.5), np.asarray([])) + xp_assert_equal(pp.solve(1.5, discontinuity=False), np.asarray([])) + + def test_roots_random(self): + # Check high-order polynomials with random coefficients + rng = np.random.RandomState(1234) + + num = 0 + + for extrapolate in (True, False): + for order in range(0, 20): + x = np.unique(np.r_[0, 10 * rng.rand(30), 10]) + c = 2*rng.rand(order+1, len(x)-1, 2, 3) - 1 + + pp = PPoly(c, x) + for y in [0, rng.random()]: + r = pp.solve(y, discontinuity=False, extrapolate=extrapolate) + + for i in range(2): + for j in range(3): + rr = r[i,j] + if rr.size > 0: + # Check that the reported roots indeed are roots + num += rr.size + val = pp(rr, extrapolate=extrapolate)[:,i,j] + cmpval = pp(rr, nu=1, + extrapolate=extrapolate)[:,i,j] + msg = f"({extrapolate!r}) r = {repr(rr)}" + xp_assert_close((val-y) / cmpval, np.asarray(0.0), + atol=1e-7, + err_msg=msg, check_shape=False) + + # Check that we checked a number of roots + assert num > 100, repr(num) + + def test_roots_croots(self): + # Test the complex root finding algorithm + rng = np.random.RandomState(1234) + + for k in range(1, 15): + c = rng.rand(k, 1, 130) + + if k == 3: + # add a case with zero discriminant + c[:,0,0] = 1, 2, 1 + + for y in [0, rng.random()]: + w = np.empty(c.shape, dtype=complex) + _ppoly._croots_poly1(c, w, y) + + if k == 1: + assert np.isnan(w).all() + continue + + res = -y + cres = 0 + for i in range(k): + res += c[i,None] * w**(k-1-i) + cres += abs(c[i,None] * w**(k-1-i)) + with np.errstate(invalid='ignore'): + res /= cres + res = res.ravel() + res = res[~np.isnan(res)] + xp_assert_close(res, np.zeros_like(res), atol=1e-10) + + def test_extrapolate_attr(self): + # [ 1 - x**2 ] + c = np.array([[-1, 0, 1]]).T + x = np.array([0, 1]) + + for extrapolate in [True, False, None]: + pp = PPoly(c, x, extrapolate=extrapolate) + pp_d = pp.derivative() + pp_i = pp.antiderivative() + + if extrapolate is False: + assert np.isnan(pp([-0.1, 1.1])).all() + assert np.isnan(pp_i([-0.1, 1.1])).all() + assert np.isnan(pp_d([-0.1, 1.1])).all() + assert pp.roots() == [1] + else: + xp_assert_close(pp([-0.1, 1.1]), [1-0.1**2, 1-1.1**2]) + assert not np.isnan(pp_i([-0.1, 1.1])).any() + assert not np.isnan(pp_d([-0.1, 1.1])).any() + xp_assert_close(pp.roots(), np.asarray([1.0, -1.0])) + + +class TestBPoly: + def test_simple(self): + x = [0, 1] + c = [[3]] + bp = BPoly(c, x) + xp_assert_close(bp(0.1), np.asarray(3.)) + + def test_simple2(self): + x = [0, 1] + c = [[3], [1]] + bp = BPoly(c, x) # 3*(1-x) + 1*x + xp_assert_close(bp(0.1), np.asarray(3*0.9 + 1.*0.1)) + + def test_simple3(self): + x = [0, 1] + c = [[3], [1], [4]] + bp = BPoly(c, x) # 3 * (1-x)**2 + 2 * x (1-x) + 4 * x**2 + xp_assert_close(bp(0.2), + np.asarray(3 * 0.8*0.8 + 1 * 2*0.2*0.8 + 4 * 0.2*0.2)) + + def test_simple4(self): + x = [0, 1] + c = [[1], [1], [1], [2]] + bp = BPoly(c, x) + xp_assert_close(bp(0.3), + np.asarray( 0.7**3 + + 3 * 0.7**2 * 0.3 + + 3 * 0.7 * 0.3**2 + + 2 * 0.3**3) + ) + + def test_simple5(self): + x = [0, 1] + c = [[1], [1], [8], [2], [1]] + bp = BPoly(c, x) + xp_assert_close(bp(0.3), + np.asarray( 0.7**4 + + 4 * 0.7**3 * 0.3 + + 8 * 6 * 0.7**2 * 0.3**2 + + 2 * 4 * 0.7 * 0.3**3 + + 0.3**4) + ) + + def test_periodic(self): + x = [0, 1, 3] + c = [[3, 0], [0, 0], [0, 2]] + # [3*(1-x)**2, 2*((x-1)/2)**2] + bp = BPoly(c, x, extrapolate='periodic') + + xp_assert_close(bp(3.4), np.asarray(3 * 0.6**2)) + xp_assert_close(bp(-1.3), np.asarray(2 * (0.7/2)**2)) + + xp_assert_close(bp(3.4, 1), np.asarray(-6 * 0.6)) + xp_assert_close(bp(-1.3, 1), np.asarray(2 * (0.7/2))) + + def test_descending(self): + rng = np.random.RandomState(0) + + power = 3 + for m in [10, 20, 30]: + x = np.sort(rng.uniform(0, 10, m + 1)) + ca = rng.uniform(-0.1, 0.1, size=(power + 1, m)) + # We need only to flip coefficients to get it right! + cd = ca[::-1].copy() + + pa = BPoly(ca, x, extrapolate=True) + pd = BPoly(cd[:, ::-1], x[::-1], extrapolate=True) + + x_test = rng.uniform(-10, 20, 100) + xp_assert_close(pa(x_test), pd(x_test), rtol=1e-13) + xp_assert_close(pa(x_test, 1), pd(x_test, 1), rtol=1e-13) + + pa_d = pa.derivative() + pd_d = pd.derivative() + + xp_assert_close(pa_d(x_test), pd_d(x_test), rtol=1e-13) + + # Antiderivatives won't be equal because fixing continuity is + # done in the reverse order, but surely the differences should be + # equal. + pa_i = pa.antiderivative() + pd_i = pd.antiderivative() + for a, b in rng.uniform(-10, 20, (5, 2)): + int_a = pa.integrate(a, b) + int_d = pd.integrate(a, b) + xp_assert_close(int_a, int_d, rtol=1e-12) + xp_assert_close(pa_i(b) - pa_i(a), pd_i(b) - pd_i(a), + rtol=1e-12) + + def test_multi_shape(self): + rng = np.random.RandomState(1234) + c = rng.rand(6, 2, 1, 2, 3) + x = np.array([0, 0.5, 1]) + p = BPoly(c, x) + assert p.x.shape == x.shape + assert p.c.shape == c.shape + assert p(0.3).shape == c.shape[2:] + assert p(rng.rand(5, 6)).shape == (5, 6) + c.shape[2:] + + dp = p.derivative() + assert dp.c.shape == (5, 2, 1, 2, 3) + + def test_interval_length(self): + x = [0, 2] + c = [[3], [1], [4]] + bp = BPoly(c, x) + xval = 0.1 + s = xval / 2 # s = (x - xa) / (xb - xa) + xp_assert_close(bp(xval), + np.asarray(3 * (1-s)*(1-s) + 1 * 2*s*(1-s) + 4 * s*s) + ) + + def test_two_intervals(self): + x = [0, 1, 3] + c = [[3, 0], [0, 0], [0, 2]] + bp = BPoly(c, x) # [3*(1-x)**2, 2*((x-1)/2)**2] + + xp_assert_close(bp(0.4), np.asarray(3 * 0.6*0.6)) + xp_assert_close(bp(1.7), np.asarray(2 * (0.7/2)**2)) + + def test_extrapolate_attr(self): + x = [0, 2] + c = [[3], [1], [4]] + bp = BPoly(c, x) + + for extrapolate in (True, False, None): + bp = BPoly(c, x, extrapolate=extrapolate) + bp_d = bp.derivative() + if extrapolate is False: + assert np.isnan(bp([-0.1, 2.1])).all() + assert np.isnan(bp_d([-0.1, 2.1])).all() + else: + assert not np.isnan(bp([-0.1, 2.1])).any() + assert not np.isnan(bp_d([-0.1, 2.1])).any() + + +class TestBPolyCalculus: + def test_derivative(self): + x = [0, 1, 3] + c = [[3, 0], [0, 0], [0, 2]] + bp = BPoly(c, x) # [3*(1-x)**2, 2*((x-1)/2)**2] + bp_der = bp.derivative() + xp_assert_close(bp_der(0.4), np.asarray(-6*(0.6))) + xp_assert_close(bp_der(1.7), np.asarray(0.7)) + + # derivatives in-place + xp_assert_close(np.asarray([bp(0.4, nu) for nu in [1, 2, 3]]), + np.asarray([-6*(1-0.4), 6., 0.]) + ) + xp_assert_close(np.asarray([bp(1.7, nu) for nu in [1, 2, 3]]), + np.asarray([0.7, 1., 0]) + ) + + def test_derivative_ppoly(self): + # make sure it's consistent w/ power basis + rng = np.random.RandomState(1234) + m, k = 5, 8 # number of intervals, order + x = np.sort(rng.random(m)) + c = rng.random((k, m-1)) + bp = BPoly(c, x) + pp = PPoly.from_bernstein_basis(bp) + + for d in range(k): + bp = bp.derivative() + pp = pp.derivative() + xp = np.linspace(x[0], x[-1], 21) + xp_assert_close(bp(xp), pp(xp)) + + def test_deriv_inplace(self): + rng = np.random.RandomState(1234) + m, k = 5, 8 # number of intervals, order + x = np.sort(rng.random(m)) + c = rng.random((k, m-1)) + + # test both real and complex coefficients + for cc in [c.copy(), c*(1. + 2.j)]: + bp = BPoly(cc, x) + xp = np.linspace(x[0], x[-1], 21) + for i in range(k): + xp_assert_close(bp(xp, i), bp.derivative(i)(xp)) + + def test_antiderivative_simple(self): + # f(x) = x for x \in [0, 1), + # (x-1)/2 for x \in [1, 3] + # + # antiderivative is then + # F(x) = x**2 / 2 for x \in [0, 1), + # 0.5*x*(x/2 - 1) + A for x \in [1, 3] + # where A = 3/4 for continuity at x = 1. + x = [0, 1, 3] + c = [[0, 0], [1, 1]] + + bp = BPoly(c, x) + bi = bp.antiderivative() + + xx = np.linspace(0, 3, 11) + xp_assert_close(bi(xx), + np.where(xx < 1, xx**2 / 2., + 0.5 * xx * (xx/2. - 1) + 3./4), + atol=1e-12, rtol=1e-12) + + def test_der_antider(self): + rng = np.random.RandomState(1234) + x = np.sort(rng.random(11)) + c = rng.random((4, 10, 2, 3)) + bp = BPoly(c, x) + + xx = np.linspace(x[0], x[-1], 100) + xp_assert_close(bp.antiderivative().derivative()(xx), + bp(xx), atol=1e-12, rtol=1e-12) + + def test_antider_ppoly(self): + rng = np.random.RandomState(1234) + x = np.sort(rng.random(11)) + c = rng.random((4, 10, 2, 3)) + bp = BPoly(c, x) + pp = PPoly.from_bernstein_basis(bp) + + xx = np.linspace(x[0], x[-1], 10) + + xp_assert_close(bp.antiderivative(2)(xx), + pp.antiderivative(2)(xx), atol=1e-12, rtol=1e-12) + + def test_antider_continuous(self): + rng = np.random.RandomState(1234) + x = np.sort(rng.random(11)) + c = rng.random((4, 10)) + bp = BPoly(c, x).antiderivative() + + xx = bp.x[1:-1] + xp_assert_close(bp(xx - 1e-14), + bp(xx + 1e-14), atol=1e-12, rtol=1e-12) + + def test_integrate(self): + rng = np.random.RandomState(1234) + x = np.sort(rng.random(11)) + c = rng.random((4, 10)) + bp = BPoly(c, x) + pp = PPoly.from_bernstein_basis(bp) + xp_assert_close(bp.integrate(0, 1), + pp.integrate(0, 1), atol=1e-12, rtol=1e-12, check_0d=False) + + def test_integrate_extrap(self): + c = [[1]] + x = [0, 1] + b = BPoly(c, x) + + # default is extrapolate=True + xp_assert_close(b.integrate(0, 2), np.asarray(2.), + atol=1e-14, check_0d=False) + + # .integrate argument overrides self.extrapolate + b1 = BPoly(c, x, extrapolate=False) + assert np.isnan(b1.integrate(0, 2)) + xp_assert_close(b1.integrate(0, 2, extrapolate=True), + np.asarray(2.), atol=1e-14, check_0d=False) + + def test_integrate_periodic(self): + x = np.array([1, 2, 4]) + c = np.array([[0., 0.], [-1., -1.], [2., -0.], [1., 2.]]) + + P = BPoly.from_power_basis(PPoly(c, x), extrapolate='periodic') + I = P.antiderivative() + + period_int = I(4) - I(1) + + xp_assert_close(P.integrate(1, 4), period_int) #, check_0d=False) + xp_assert_close(P.integrate(-10, -7), period_int) + xp_assert_close(P.integrate(-10, -4), 2 * period_int) + + xp_assert_close(P.integrate(1.5, 2.5), I(2.5) - I(1.5)) + xp_assert_close(P.integrate(3.5, 5), I(2) - I(1) + I(4) - I(3.5)) + xp_assert_close(P.integrate(3.5 + 12, 5 + 12), + I(2) - I(1) + I(4) - I(3.5)) + xp_assert_close(P.integrate(3.5, 5 + 12), + I(2) - I(1) + I(4) - I(3.5) + 4 * period_int) + + xp_assert_close(P.integrate(0, -1), I(2) - I(3)) + xp_assert_close(P.integrate(-9, -10), I(2) - I(3)) + xp_assert_close(P.integrate(0, -10), I(2) - I(3) - 3 * period_int) + + def test_antider_neg(self): + # .derivative(-nu) ==> .andiderivative(nu) and vice versa + c = [[1]] + x = [0, 1] + b = BPoly(c, x) + + xx = np.linspace(0, 1, 21) + + xp_assert_close(b.derivative(-1)(xx), b.antiderivative()(xx), + atol=1e-12, rtol=1e-12) + xp_assert_close(b.derivative(1)(xx), b.antiderivative(-1)(xx), + atol=1e-12, rtol=1e-12) + + +class TestPolyConversions: + def test_bp_from_pp(self): + x = [0, 1, 3] + c = [[3, 2], [1, 8], [4, 3]] + pp = PPoly(c, x) + bp = BPoly.from_power_basis(pp) + pp1 = PPoly.from_bernstein_basis(bp) + + xp = [0.1, 1.4] + xp_assert_close(pp(xp), bp(xp)) + xp_assert_close(pp(xp), pp1(xp)) + + def test_bp_from_pp_random(self): + rng = np.random.RandomState(1234) + m, k = 5, 8 # number of intervals, order + x = np.sort(rng.random(m)) + c = rng.random((k, m-1)) + pp = PPoly(c, x) + bp = BPoly.from_power_basis(pp) + pp1 = PPoly.from_bernstein_basis(bp) + + xp = np.linspace(x[0], x[-1], 21) + xp_assert_close(pp(xp), bp(xp)) + xp_assert_close(pp(xp), pp1(xp)) + + def test_pp_from_bp(self): + x = [0, 1, 3] + c = [[3, 3], [1, 1], [4, 2]] + bp = BPoly(c, x) + pp = PPoly.from_bernstein_basis(bp) + bp1 = BPoly.from_power_basis(pp) + + xp = [0.1, 1.4] + xp_assert_close(bp(xp), pp(xp)) + xp_assert_close(bp(xp), bp1(xp)) + + def test_broken_conversions(self): + # regression test for gh-10597: from_power_basis only accepts PPoly etc. + x = [0, 1, 3] + c = [[3, 3], [1, 1], [4, 2]] + pp = PPoly(c, x) + with assert_raises(TypeError): + PPoly.from_bernstein_basis(pp) + + bp = BPoly(c, x) + with assert_raises(TypeError): + BPoly.from_power_basis(bp) + + +class TestBPolyFromDerivatives: + def test_make_poly_1(self): + c1 = BPoly._construct_from_derivatives(0, 1, [2], [3]) + xp_assert_close(c1, [2., 3.]) + + def test_make_poly_2(self): + c1 = BPoly._construct_from_derivatives(0, 1, [1, 0], [1]) + xp_assert_close(c1, [1., 1., 1.]) + + # f'(0) = 3 + c2 = BPoly._construct_from_derivatives(0, 1, [2, 3], [1]) + xp_assert_close(c2, [2., 7./2, 1.]) + + # f'(1) = 3 + c3 = BPoly._construct_from_derivatives(0, 1, [2], [1, 3]) + xp_assert_close(c3, [2., -0.5, 1.]) + + def test_make_poly_3(self): + # f'(0)=2, f''(0)=3 + c1 = BPoly._construct_from_derivatives(0, 1, [1, 2, 3], [4]) + xp_assert_close(c1, [1., 5./3, 17./6, 4.]) + + # f'(1)=2, f''(1)=3 + c2 = BPoly._construct_from_derivatives(0, 1, [1], [4, 2, 3]) + xp_assert_close(c2, [1., 19./6, 10./3, 4.]) + + # f'(0)=2, f'(1)=3 + c3 = BPoly._construct_from_derivatives(0, 1, [1, 2], [4, 3]) + xp_assert_close(c3, [1., 5./3, 3., 4.]) + + def test_make_poly_12(self): + rng = np.random.RandomState(12345) + ya = np.r_[0, rng.random(5)] + yb = np.r_[0, rng.random(5)] + + c = BPoly._construct_from_derivatives(0, 1, ya, yb) + pp = BPoly(c[:, None], [0, 1]) + for j in range(6): + xp_assert_close(pp(0.), ya[j], check_0d=False) + xp_assert_close(pp(1.), yb[j], check_0d=False) + pp = pp.derivative() + + def test_raise_degree(self): + rng = np.random.RandomState(12345) + x = [0, 1] + k, d = 8, 5 + c = rng.random((k, 1, 2, 3, 4)) + bp = BPoly(c, x) + + c1 = BPoly._raise_degree(c, d) + bp1 = BPoly(c1, x) + + xp = np.linspace(0, 1, 11) + xp_assert_close(bp(xp), bp1(xp)) + + def test_xi_yi(self): + assert_raises(ValueError, BPoly.from_derivatives, [0, 1], [0]) + + def test_coords_order(self): + xi = [0, 0, 1] + yi = [[0], [0], [0]] + assert_raises(ValueError, BPoly.from_derivatives, xi, yi) + + def test_zeros(self): + xi = [0, 1, 2, 3] + yi = [[0, 0], [0], [0, 0], [0, 0]] # NB: will have to raise the degree + pp = BPoly.from_derivatives(xi, yi) + assert pp.c.shape == (4, 3) + + ppd = pp.derivative() + for xp in [0., 0.1, 1., 1.1, 1.9, 2., 2.5]: + xp_assert_close(pp(xp), np.asarray(0.0)) + xp_assert_close(ppd(xp), np.asarray(0.0)) + + + def _make_random_mk(self, m, k): + # k derivatives at each breakpoint + rng = np.random.RandomState(1234) + xi = np.asarray([1. * j**2 for j in range(m+1)]) + yi = [rng.random(k) for j in range(m+1)] + return xi, yi + + def test_random_12(self): + m, k = 5, 12 + xi, yi = self._make_random_mk(m, k) + pp = BPoly.from_derivatives(xi, yi) + + for order in range(k//2): + xp_assert_close(pp(xi), [yy[order] for yy in yi]) + pp = pp.derivative() + + def test_order_zero(self): + m, k = 5, 12 + xi, yi = self._make_random_mk(m, k) + assert_raises(ValueError, BPoly.from_derivatives, + **dict(xi=xi, yi=yi, orders=0)) + + def test_orders_too_high(self): + m, k = 5, 12 + xi, yi = self._make_random_mk(m, k) + + BPoly.from_derivatives(xi, yi, orders=2*k-1) # this is still ok + assert_raises(ValueError, BPoly.from_derivatives, # but this is not + **dict(xi=xi, yi=yi, orders=2*k)) + + def test_orders_global(self): + m, k = 5, 12 + xi, yi = self._make_random_mk(m, k) + + # ok, this is confusing. Local polynomials will be of the order 5 + # which means that up to the 2nd derivatives will be used at each point + order = 5 + pp = BPoly.from_derivatives(xi, yi, orders=order) + + for j in range(order//2+1): + xp_assert_close(pp(xi[1:-1] - 1e-12), pp(xi[1:-1] + 1e-12)) + pp = pp.derivative() + assert not np.allclose(pp(xi[1:-1] - 1e-12), pp(xi[1:-1] + 1e-12)) + + # now repeat with `order` being even: on each interval, it uses + # order//2 'derivatives' @ the right-hand endpoint and + # order//2+1 @ 'derivatives' the left-hand endpoint + order = 6 + pp = BPoly.from_derivatives(xi, yi, orders=order) + for j in range(order//2): + xp_assert_close(pp(xi[1:-1] - 1e-12), pp(xi[1:-1] + 1e-12)) + pp = pp.derivative() + assert not np.allclose(pp(xi[1:-1] - 1e-12), pp(xi[1:-1] + 1e-12)) + + def test_orders_local(self): + m, k = 7, 12 + xi, yi = self._make_random_mk(m, k) + + orders = [o + 1 for o in range(m)] + for i, x in enumerate(xi[1:-1]): + pp = BPoly.from_derivatives(xi, yi, orders=orders) + for j in range(orders[i] // 2 + 1): + xp_assert_close(pp(x - 1e-12), pp(x + 1e-12)) + pp = pp.derivative() + assert not np.allclose(pp(x - 1e-12), pp(x + 1e-12)) + + def test_yi_trailing_dims(self): + rng = np.random.RandomState(1234) + m, k = 7, 5 + xi = np.sort(rng.random(m+1)) + yi = rng.random((m+1, k, 6, 7, 8)) + pp = BPoly.from_derivatives(xi, yi) + assert pp.c.shape == (2*k, m, 6, 7, 8) + + def test_gh_5430(self): + # At least one of these raises an error unless gh-5430 is + # fixed. In py2k an int is implemented using a C long, so + # which one fails depends on your system. In py3k there is only + # one arbitrary precision integer type, so both should fail. + orders = np.int32(1) + p = BPoly.from_derivatives([0, 1], [[0], [0]], orders=orders) + assert_almost_equal(p(0), np.asarray(0)) + orders = np.int64(1) + p = BPoly.from_derivatives([0, 1], [[0], [0]], orders=orders) + assert_almost_equal(p(0), np.asarray(0)) + orders = 1 + # This worked before; make sure it still works + p = BPoly.from_derivatives([0, 1], [[0], [0]], orders=orders) + assert_almost_equal(p(0), np.asarray(0)) + orders = 1 + + +class TestNdPPoly: + def test_simple_1d(self): + rng = np.random.RandomState(1234) + + c = rng.rand(4, 5) + x = np.linspace(0, 1, 5+1) + + xi = rng.rand(200) + + p = NdPPoly(c, (x,)) + v1 = p((xi,)) + + v2 = _ppoly_eval_1(c[:,:,None], x, xi).ravel() + xp_assert_close(v1, v2) + + def test_simple_2d(self): + rng = np.random.RandomState(1234) + + c = rng.rand(4, 5, 6, 7) + x = np.linspace(0, 1, 6+1) + y = np.linspace(0, 1, 7+1)**2 + + xi = rng.rand(200) + yi = rng.rand(200) + + v1 = np.empty([len(xi), 1], dtype=c.dtype) + v1.fill(np.nan) + _ppoly.evaluate_nd(c.reshape(4*5, 6*7, 1), + (x, y), + np.array([4, 5], dtype=np.intc), + np.c_[xi, yi], + np.array([0, 0], dtype=np.intc), + 1, + v1) + v1 = v1.ravel() + v2 = _ppoly2d_eval(c, (x, y), xi, yi) + xp_assert_close(v1, v2) + + p = NdPPoly(c, (x, y)) + for nu in (None, (0, 0), (0, 1), (1, 0), (2, 3), (9, 2)): + v1 = p(np.c_[xi, yi], nu=nu) + v2 = _ppoly2d_eval(c, (x, y), xi, yi, nu=nu) + xp_assert_close(v1, v2, err_msg=repr(nu)) + + def test_simple_3d(self): + rng = np.random.RandomState(1234) + + c = rng.rand(4, 5, 6, 7, 8, 9) + x = np.linspace(0, 1, 7+1) + y = np.linspace(0, 1, 8+1)**2 + z = np.linspace(0, 1, 9+1)**3 + + xi = rng.rand(40) + yi = rng.rand(40) + zi = rng.rand(40) + + p = NdPPoly(c, (x, y, z)) + + for nu in (None, (0, 0, 0), (0, 1, 0), (1, 0, 0), (2, 3, 0), + (6, 0, 2)): + v1 = p((xi, yi, zi), nu=nu) + v2 = _ppoly3d_eval(c, (x, y, z), xi, yi, zi, nu=nu) + xp_assert_close(v1, v2, err_msg=repr(nu)) + + def test_simple_4d(self): + rng = np.random.RandomState(1234) + + c = rng.rand(4, 5, 6, 7, 8, 9, 10, 11) + x = np.linspace(0, 1, 8+1) + y = np.linspace(0, 1, 9+1)**2 + z = np.linspace(0, 1, 10+1)**3 + u = np.linspace(0, 1, 11+1)**4 + + xi = rng.rand(20) + yi = rng.rand(20) + zi = rng.rand(20) + ui = rng.rand(20) + + p = NdPPoly(c, (x, y, z, u)) + v1 = p((xi, yi, zi, ui)) + + v2 = _ppoly4d_eval(c, (x, y, z, u), xi, yi, zi, ui) + xp_assert_close(v1, v2) + + def test_deriv_1d(self): + rng = np.random.RandomState(1234) + + c = rng.rand(4, 5) + x = np.linspace(0, 1, 5+1) + + p = NdPPoly(c, (x,)) + + # derivative + dp = p.derivative(nu=[1]) + p1 = PPoly(c, x) + dp1 = p1.derivative() + xp_assert_close(dp.c, dp1.c) + + # antiderivative + dp = p.antiderivative(nu=[2]) + p1 = PPoly(c, x) + dp1 = p1.antiderivative(2) + xp_assert_close(dp.c, dp1.c) + + def test_deriv_3d(self): + rng = np.random.RandomState(1234) + + c = rng.rand(4, 5, 6, 7, 8, 9) + x = np.linspace(0, 1, 7+1) + y = np.linspace(0, 1, 8+1)**2 + z = np.linspace(0, 1, 9+1)**3 + + p = NdPPoly(c, (x, y, z)) + + # differentiate vs x + p1 = PPoly(c.transpose(0, 3, 1, 2, 4, 5), x) + dp = p.derivative(nu=[2]) + dp1 = p1.derivative(2) + xp_assert_close(dp.c, + dp1.c.transpose(0, 2, 3, 1, 4, 5)) + + # antidifferentiate vs y + p1 = PPoly(c.transpose(1, 4, 0, 2, 3, 5), y) + dp = p.antiderivative(nu=[0, 1, 0]) + dp1 = p1.antiderivative(1) + xp_assert_close(dp.c, + dp1.c.transpose(2, 0, 3, 4, 1, 5)) + + # differentiate vs z + p1 = PPoly(c.transpose(2, 5, 0, 1, 3, 4), z) + dp = p.derivative(nu=[0, 0, 3]) + dp1 = p1.derivative(3) + xp_assert_close(dp.c, + dp1.c.transpose(2, 3, 0, 4, 5, 1)) + + def test_deriv_3d_simple(self): + # Integrate to obtain function x y**2 z**4 / (2! 4!) + rng = np.random.RandomState(1234) + + c = np.ones((1, 1, 1, 3, 4, 5)) + x = np.linspace(0, 1, 3+1)**1 + y = np.linspace(0, 1, 4+1)**2 + z = np.linspace(0, 1, 5+1)**3 + + p = NdPPoly(c, (x, y, z)) + ip = p.antiderivative((1, 0, 4)) + ip = ip.antiderivative((0, 2, 0)) + + xi = rng.rand(20) + yi = rng.rand(20) + zi = rng.rand(20) + + xp_assert_close(ip((xi, yi, zi)), + xi * yi**2 * zi**4 / (gamma(3)*gamma(5))) + + def test_integrate_2d(self): + rng = np.random.RandomState(1234) + c = rng.rand(4, 5, 16, 17) + x = np.linspace(0, 1, 16+1)**1 + y = np.linspace(0, 1, 17+1)**2 + + # make continuously differentiable so that nquad() has an + # easier time + c = c.transpose(0, 2, 1, 3) + cx = c.reshape(c.shape[0], c.shape[1], -1).copy() + _ppoly.fix_continuity(cx, x, 2) + c = cx.reshape(c.shape) + c = c.transpose(0, 2, 1, 3) + c = c.transpose(1, 3, 0, 2) + cx = c.reshape(c.shape[0], c.shape[1], -1).copy() + _ppoly.fix_continuity(cx, y, 2) + c = cx.reshape(c.shape) + c = c.transpose(2, 0, 3, 1).copy() + + # Check integration + p = NdPPoly(c, (x, y)) + + for ranges in [[(0, 1), (0, 1)], + [(0, 0.5), (0, 1)], + [(0, 1), (0, 0.5)], + [(0.3, 0.7), (0.6, 0.2)]]: + + ig = p.integrate(ranges) + ig2, err2 = nquad(lambda x, y: p((x, y)), ranges, + opts=[dict(epsrel=1e-5, epsabs=1e-5)]*2) + xp_assert_close(ig, ig2, rtol=1e-5, atol=1e-5, check_0d=False, + err_msg=repr(ranges)) + + def test_integrate_1d(self): + rng = np.random.RandomState(1234) + c = rng.rand(4, 5, 6, 16, 17, 18) + x = np.linspace(0, 1, 16+1)**1 + y = np.linspace(0, 1, 17+1)**2 + z = np.linspace(0, 1, 18+1)**3 + + # Check 1-D integration + p = NdPPoly(c, (x, y, z)) + + u = rng.rand(200) + v = rng.rand(200) + a, b = 0.2, 0.7 + + px = p.integrate_1d(a, b, axis=0) + pax = p.antiderivative((1, 0, 0)) + xp_assert_close(px((u, v)), pax((b, u, v)) - pax((a, u, v))) + + py = p.integrate_1d(a, b, axis=1) + pay = p.antiderivative((0, 1, 0)) + xp_assert_close(py((u, v)), pay((u, b, v)) - pay((u, a, v))) + + pz = p.integrate_1d(a, b, axis=2) + paz = p.antiderivative((0, 0, 1)) + xp_assert_close(pz((u, v)), paz((u, v, b)) - paz((u, v, a))) + + @pytest.mark.thread_unsafe + def test_concurrency(self): + rng = np.random.default_rng(12345) + + c = rng.uniform(size=(4, 5, 6, 7, 8, 9)) + x = np.linspace(0, 1, 7+1) + y = np.linspace(0, 1, 8+1)**2 + z = np.linspace(0, 1, 9+1)**3 + + p = NdPPoly(c, (x, y, z)) + + def worker_fn(_, spl): + xi = rng.uniform(size=40) + yi = rng.uniform(size=40) + zi = rng.uniform(size=40) + spl((xi, yi, zi)) + + _run_concurrent_barrier(10, worker_fn, p) + + +def _ppoly_eval_1(c, x, xps): + """Evaluate piecewise polynomial manually""" + out = np.zeros((len(xps), c.shape[2])) + for i, xp in enumerate(xps): + if xp < 0 or xp > 1: + out[i,:] = np.nan + continue + j = np.searchsorted(x, xp) - 1 + d = xp - x[j] + assert x[j] <= xp < x[j+1] + r = sum(c[k,j] * d**(c.shape[0]-k-1) + for k in range(c.shape[0])) + out[i,:] = r + return out + + +def _ppoly_eval_2(coeffs, breaks, xnew, fill=np.nan): + """Evaluate piecewise polynomial manually (another way)""" + a = breaks[0] + b = breaks[-1] + K = coeffs.shape[0] + + saveshape = np.shape(xnew) + xnew = np.ravel(xnew) + res = np.empty_like(xnew) + mask = (xnew >= a) & (xnew <= b) + res[~mask] = fill + xx = xnew.compress(mask) + indxs = np.searchsorted(breaks, xx)-1 + indxs = indxs.clip(0, len(breaks)) + pp = coeffs + diff = xx - breaks.take(indxs) + V = np.vander(diff, N=K) + values = np.array([np.dot(V[k, :], pp[:, indxs[k]]) for k in range(len(xx))]) + res[mask] = values + res.shape = saveshape + return res + + +def _dpow(x, y, n): + """ + d^n (x**y) / dx^n + """ + if n < 0: + raise ValueError("invalid derivative order") + elif n > y: + return 0 + else: + return poch(y - n + 1, n) * x**(y - n) + + +def _ppoly2d_eval(c, xs, xnew, ynew, nu=None): + """ + Straightforward evaluation of 2-D piecewise polynomial + """ + if nu is None: + nu = (0, 0) + + out = np.empty((len(xnew),), dtype=c.dtype) + + nx, ny = c.shape[:2] + + for jout, (x, y) in enumerate(zip(xnew, ynew)): + if not ((xs[0][0] <= x <= xs[0][-1]) and + (xs[1][0] <= y <= xs[1][-1])): + out[jout] = np.nan + continue + + j1 = np.searchsorted(xs[0], x) - 1 + j2 = np.searchsorted(xs[1], y) - 1 + + s1 = x - xs[0][j1] + s2 = y - xs[1][j2] + + val = 0 + + for k1 in range(c.shape[0]): + for k2 in range(c.shape[1]): + val += (c[nx-k1-1,ny-k2-1,j1,j2] + * _dpow(s1, k1, nu[0]) + * _dpow(s2, k2, nu[1])) + + out[jout] = val + + return out + + +def _ppoly3d_eval(c, xs, xnew, ynew, znew, nu=None): + """ + Straightforward evaluation of 3-D piecewise polynomial + """ + if nu is None: + nu = (0, 0, 0) + + out = np.empty((len(xnew),), dtype=c.dtype) + + nx, ny, nz = c.shape[:3] + + for jout, (x, y, z) in enumerate(zip(xnew, ynew, znew)): + if not ((xs[0][0] <= x <= xs[0][-1]) and + (xs[1][0] <= y <= xs[1][-1]) and + (xs[2][0] <= z <= xs[2][-1])): + out[jout] = np.nan + continue + + j1 = np.searchsorted(xs[0], x) - 1 + j2 = np.searchsorted(xs[1], y) - 1 + j3 = np.searchsorted(xs[2], z) - 1 + + s1 = x - xs[0][j1] + s2 = y - xs[1][j2] + s3 = z - xs[2][j3] + + val = 0 + for k1 in range(c.shape[0]): + for k2 in range(c.shape[1]): + for k3 in range(c.shape[2]): + val += (c[nx-k1-1,ny-k2-1,nz-k3-1,j1,j2,j3] + * _dpow(s1, k1, nu[0]) + * _dpow(s2, k2, nu[1]) + * _dpow(s3, k3, nu[2])) + + out[jout] = val + + return out + + +def _ppoly4d_eval(c, xs, xnew, ynew, znew, unew, nu=None): + """ + Straightforward evaluation of 4-D piecewise polynomial + """ + if nu is None: + nu = (0, 0, 0, 0) + + out = np.empty((len(xnew),), dtype=c.dtype) + + mx, my, mz, mu = c.shape[:4] + + for jout, (x, y, z, u) in enumerate(zip(xnew, ynew, znew, unew)): + if not ((xs[0][0] <= x <= xs[0][-1]) and + (xs[1][0] <= y <= xs[1][-1]) and + (xs[2][0] <= z <= xs[2][-1]) and + (xs[3][0] <= u <= xs[3][-1])): + out[jout] = np.nan + continue + + j1 = np.searchsorted(xs[0], x) - 1 + j2 = np.searchsorted(xs[1], y) - 1 + j3 = np.searchsorted(xs[2], z) - 1 + j4 = np.searchsorted(xs[3], u) - 1 + + s1 = x - xs[0][j1] + s2 = y - xs[1][j2] + s3 = z - xs[2][j3] + s4 = u - xs[3][j4] + + val = 0 + for k1 in range(c.shape[0]): + for k2 in range(c.shape[1]): + for k3 in range(c.shape[2]): + for k4 in range(c.shape[3]): + val += (c[mx-k1-1,my-k2-1,mz-k3-1,mu-k4-1,j1,j2,j3,j4] + * _dpow(s1, k1, nu[0]) + * _dpow(s2, k2, nu[1]) + * _dpow(s3, k3, nu[2]) + * _dpow(s4, k4, nu[3])) + + out[jout] = val + + return out diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_ndgriddata.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_ndgriddata.py new file mode 100644 index 0000000000000000000000000000000000000000..047a940b3efcb24ec85a94a87dd1050baa01f165 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_ndgriddata.py @@ -0,0 +1,308 @@ +import numpy as np +from scipy._lib._array_api import ( + xp_assert_equal, xp_assert_close +) +import pytest +from pytest import raises as assert_raises + +from scipy.interpolate import (griddata, NearestNDInterpolator, + LinearNDInterpolator, + CloughTocher2DInterpolator) +from scipy._lib._testutils import _run_concurrent_barrier + + +parametrize_interpolators = pytest.mark.parametrize( + "interpolator", [NearestNDInterpolator, LinearNDInterpolator, + CloughTocher2DInterpolator] +) +parametrize_methods = pytest.mark.parametrize( + 'method', + ('nearest', 'linear', 'cubic'), +) +parametrize_rescale = pytest.mark.parametrize( + 'rescale', + (True, False), +) + + +class TestGriddata: + def test_fill_value(self): + x = [(0,0), (0,1), (1,0)] + y = [1, 2, 3] + + yi = griddata(x, y, [(1,1), (1,2), (0,0)], fill_value=-1) + xp_assert_equal(yi, [-1., -1, 1]) + + yi = griddata(x, y, [(1,1), (1,2), (0,0)]) + xp_assert_equal(yi, [np.nan, np.nan, 1]) + + @parametrize_methods + @parametrize_rescale + def test_alternative_call(self, method, rescale): + x = np.array([(0,0), (-0.5,-0.5), (-0.5,0.5), (0.5, 0.5), (0.25, 0.3)], + dtype=np.float64) + y = (np.arange(x.shape[0], dtype=np.float64)[:,None] + + np.array([0,1])[None,:]) + + msg = repr((method, rescale)) + yi = griddata((x[:,0], x[:,1]), y, (x[:,0], x[:,1]), method=method, + rescale=rescale) + xp_assert_close(y, yi, atol=1e-14, err_msg=msg) + + @parametrize_methods + @parametrize_rescale + def test_multivalue_2d(self, method, rescale): + x = np.array([(0,0), (-0.5,-0.5), (-0.5,0.5), (0.5, 0.5), (0.25, 0.3)], + dtype=np.float64) + y = (np.arange(x.shape[0], dtype=np.float64)[:,None] + + np.array([0,1])[None,:]) + + msg = repr((method, rescale)) + yi = griddata(x, y, x, method=method, rescale=rescale) + xp_assert_close(y, yi, atol=1e-14, err_msg=msg) + + @parametrize_methods + @parametrize_rescale + def test_multipoint_2d(self, method, rescale): + x = np.array([(0,0), (-0.5,-0.5), (-0.5,0.5), (0.5, 0.5), (0.25, 0.3)], + dtype=np.float64) + y = np.arange(x.shape[0], dtype=np.float64) + + xi = x[:,None,:] + np.array([0,0,0])[None,:,None] + + msg = repr((method, rescale)) + yi = griddata(x, y, xi, method=method, rescale=rescale) + + assert yi.shape == (5, 3), msg + xp_assert_close(yi, np.tile(y[:,None], (1, 3)), + atol=1e-14, err_msg=msg) + + @parametrize_methods + @parametrize_rescale + def test_complex_2d(self, method, rescale): + x = np.array([(0,0), (-0.5,-0.5), (-0.5,0.5), (0.5, 0.5), (0.25, 0.3)], + dtype=np.float64) + y = np.arange(x.shape[0], dtype=np.float64) + y = y - 2j*y[::-1] + + xi = x[:,None,:] + np.array([0,0,0])[None,:,None] + + msg = repr((method, rescale)) + yi = griddata(x, y, xi, method=method, rescale=rescale) + + assert yi.shape == (5, 3) + xp_assert_close(yi, np.tile(y[:,None], (1, 3)), + atol=1e-14, err_msg=msg) + + @parametrize_methods + def test_1d(self, method): + x = np.array([1, 2.5, 3, 4.5, 5, 6]) + y = np.array([1, 2, 0, 3.9, 2, 1]) + + xp_assert_close(griddata(x, y, x, method=method), y, + err_msg=method, atol=1e-14) + xp_assert_close(griddata(x.reshape(6, 1), y, x, method=method), y, + err_msg=method, atol=1e-14) + xp_assert_close(griddata((x,), y, (x,), method=method), y, + err_msg=method, atol=1e-14) + + def test_1d_borders(self): + # Test for nearest neighbor case with xi outside + # the range of the values. + x = np.array([1, 2.5, 3, 4.5, 5, 6]) + y = np.array([1, 2, 0, 3.9, 2, 1]) + xi = np.array([0.9, 6.5]) + yi_should = np.array([1.0, 1.0]) + + method = 'nearest' + xp_assert_close(griddata(x, y, xi, + method=method), yi_should, + err_msg=method, + atol=1e-14) + xp_assert_close(griddata(x.reshape(6, 1), y, xi, + method=method), yi_should, + err_msg=method, + atol=1e-14) + xp_assert_close(griddata((x, ), y, (xi, ), + method=method), yi_should, + err_msg=method, + atol=1e-14) + + @parametrize_methods + def test_1d_unsorted(self, method): + x = np.array([2.5, 1, 4.5, 5, 6, 3]) + y = np.array([1, 2, 0, 3.9, 2, 1]) + + xp_assert_close(griddata(x, y, x, method=method), y, + err_msg=method, atol=1e-10) + xp_assert_close(griddata(x.reshape(6, 1), y, x, method=method), y, + err_msg=method, atol=1e-10) + xp_assert_close(griddata((x,), y, (x,), method=method), y, + err_msg=method, atol=1e-10) + + @parametrize_methods + def test_square_rescale_manual(self, method): + points = np.array([(0,0), (0,100), (10,100), (10,0), (1, 5)], dtype=np.float64) + points_rescaled = np.array([(0,0), (0,1), (1,1), (1,0), (0.1, 0.05)], + dtype=np.float64) + values = np.array([1., 2., -3., 5., 9.], dtype=np.float64) + + xx, yy = np.broadcast_arrays(np.linspace(0, 10, 14)[:,None], + np.linspace(0, 100, 14)[None,:]) + xx = xx.ravel() + yy = yy.ravel() + xi = np.array([xx, yy]).T.copy() + + msg = method + zi = griddata(points_rescaled, values, xi/np.array([10, 100.]), + method=method) + zi_rescaled = griddata(points, values, xi, method=method, + rescale=True) + xp_assert_close(zi, zi_rescaled, err_msg=msg, + atol=1e-12) + + @parametrize_methods + def test_xi_1d(self, method): + # Check that 1-D xi is interpreted as a coordinate + x = np.array([(0,0), (-0.5,-0.5), (-0.5,0.5), (0.5, 0.5), (0.25, 0.3)], + dtype=np.float64) + y = np.arange(x.shape[0], dtype=np.float64) + y = y - 2j*y[::-1] + + xi = np.array([0.5, 0.5]) + + p1 = griddata(x, y, xi, method=method) + p2 = griddata(x, y, xi[None,:], method=method) + xp_assert_close(p1, p2, err_msg=method) + + xi1 = np.array([0.5]) + xi3 = np.array([0.5, 0.5, 0.5]) + assert_raises(ValueError, griddata, x, y, xi1, + method=method) + assert_raises(ValueError, griddata, x, y, xi3, + method=method) + + +class TestNearestNDInterpolator: + def test_nearest_options(self): + # smoke test that NearestNDInterpolator accept cKDTree options + npts, nd = 4, 3 + x = np.arange(npts*nd).reshape((npts, nd)) + y = np.arange(npts) + nndi = NearestNDInterpolator(x, y) + + opts = {'balanced_tree': False, 'compact_nodes': False} + nndi_o = NearestNDInterpolator(x, y, tree_options=opts) + xp_assert_close(nndi(x), nndi_o(x), atol=1e-14) + + def test_nearest_list_argument(self): + nd = np.array([[0, 0, 0, 0, 1, 0, 1], + [0, 0, 0, 0, 0, 1, 1], + [0, 0, 0, 0, 1, 1, 2]]) + d = nd[:, 3:] + + # z is np.array + NI = NearestNDInterpolator((d[0], d[1]), d[2]) + xp_assert_equal(NI([0.1, 0.9], [0.1, 0.9]), [0.0, 2.0]) + + # z is list + NI = NearestNDInterpolator((d[0], d[1]), list(d[2])) + xp_assert_equal(NI([0.1, 0.9], [0.1, 0.9]), [0.0, 2.0]) + + def test_nearest_query_options(self): + nd = np.array([[0, 0.5, 0, 1], + [0, 0, 0.5, 1], + [0, 1, 1, 2]]) + delta = 0.1 + query_points = [0 + delta, 1 + delta], [0 + delta, 1 + delta] + + # case 1 - query max_dist is smaller than + # the query points' nearest distance to nd. + NI = NearestNDInterpolator((nd[0], nd[1]), nd[2]) + distance_upper_bound = np.sqrt(delta ** 2 + delta ** 2) - 1e-7 + xp_assert_equal(NI(query_points, distance_upper_bound=distance_upper_bound), + [np.nan, np.nan]) + + # case 2 - query p is inf, will return [0, 2] + distance_upper_bound = np.sqrt(delta ** 2 + delta ** 2) - 1e-7 + p = np.inf + xp_assert_equal( + NI(query_points, distance_upper_bound=distance_upper_bound, p=p), + [0.0, 2.0] + ) + + # case 3 - query max_dist is larger, so should return non np.nan + distance_upper_bound = np.sqrt(delta ** 2 + delta ** 2) + 1e-7 + xp_assert_equal( + NI(query_points, distance_upper_bound=distance_upper_bound), + [0.0, 2.0] + ) + + def test_nearest_query_valid_inputs(self): + nd = np.array([[0, 1, 0, 1], + [0, 0, 1, 1], + [0, 1, 1, 2]]) + NI = NearestNDInterpolator((nd[0], nd[1]), nd[2]) + with assert_raises(TypeError): + NI([0.5, 0.5], query_options="not a dictionary") + + @pytest.mark.thread_unsafe + def test_concurrency(self): + npts, nd = 50, 3 + x = np.arange(npts * nd).reshape((npts, nd)) + y = np.arange(npts) + nndi = NearestNDInterpolator(x, y) + + def worker_fn(_, spl): + spl(x) + + _run_concurrent_barrier(10, worker_fn, nndi) + + +class TestNDInterpolators: + @parametrize_interpolators + def test_broadcastable_input(self, interpolator): + # input data + rng = np.random.RandomState(0) + x = rng.random(10) + y = rng.random(10) + z = np.hypot(x, y) + + # x-y grid for interpolation + X = np.linspace(min(x), max(x)) + Y = np.linspace(min(y), max(y)) + X, Y = np.meshgrid(X, Y) + XY = np.vstack((X.ravel(), Y.ravel())).T + interp = interpolator(list(zip(x, y)), z) + # single array input + interp_points0 = interp(XY) + # tuple input + interp_points1 = interp((X, Y)) + interp_points2 = interp((X, 0.0)) + # broadcastable input + interp_points3 = interp(X, Y) + interp_points4 = interp(X, 0.0) + + assert (interp_points0.size == + interp_points1.size == + interp_points2.size == + interp_points3.size == + interp_points4.size) + + @parametrize_interpolators + def test_read_only(self, interpolator): + # input data + rng = np.random.RandomState(0) + xy = rng.random((10, 2)) + x, y = xy[:, 0], xy[:, 1] + z = np.hypot(x, y) + + # interpolation points + XY = rng.random((50, 2)) + + xy.setflags(write=False) + z.setflags(write=False) + XY.setflags(write=False) + + interp = interpolator(xy, z) + interp(XY) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_pade.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_pade.py new file mode 100644 index 0000000000000000000000000000000000000000..119b7d1c5667368b284fbf6458174ea14e71957a --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_pade.py @@ -0,0 +1,107 @@ +import numpy as np +from scipy.interpolate import pade +from scipy._lib._array_api import ( + xp_assert_equal, assert_array_almost_equal +) + +def test_pade_trivial(): + nump, denomp = pade([1.0], 0) + xp_assert_equal(nump.c, np.asarray([1.0])) + xp_assert_equal(denomp.c, np.asarray([1.0])) + + nump, denomp = pade([1.0], 0, 0) + xp_assert_equal(nump.c, np.asarray([1.0])) + xp_assert_equal(denomp.c, np.asarray([1.0])) + + +def test_pade_4term_exp(): + # First four Taylor coefficients of exp(x). + # Unlike poly1d, the first array element is the zero-order term. + an = [1.0, 1.0, 0.5, 1.0/6] + + nump, denomp = pade(an, 0) + assert_array_almost_equal(nump.c, [1.0/6, 0.5, 1.0, 1.0]) + assert_array_almost_equal(denomp.c, [1.0]) + + nump, denomp = pade(an, 1) + assert_array_almost_equal(nump.c, [1.0/6, 2.0/3, 1.0]) + assert_array_almost_equal(denomp.c, [-1.0/3, 1.0]) + + nump, denomp = pade(an, 2) + assert_array_almost_equal(nump.c, [1.0/3, 1.0]) + assert_array_almost_equal(denomp.c, [1.0/6, -2.0/3, 1.0]) + + nump, denomp = pade(an, 3) + assert_array_almost_equal(nump.c, [1.0]) + assert_array_almost_equal(denomp.c, [-1.0/6, 0.5, -1.0, 1.0]) + + # Testing inclusion of optional parameter + nump, denomp = pade(an, 0, 3) + assert_array_almost_equal(nump.c, [1.0/6, 0.5, 1.0, 1.0]) + assert_array_almost_equal(denomp.c, [1.0]) + + nump, denomp = pade(an, 1, 2) + assert_array_almost_equal(nump.c, [1.0/6, 2.0/3, 1.0]) + assert_array_almost_equal(denomp.c, [-1.0/3, 1.0]) + + nump, denomp = pade(an, 2, 1) + assert_array_almost_equal(nump.c, [1.0/3, 1.0]) + assert_array_almost_equal(denomp.c, [1.0/6, -2.0/3, 1.0]) + + nump, denomp = pade(an, 3, 0) + assert_array_almost_equal(nump.c, [1.0]) + assert_array_almost_equal(denomp.c, [-1.0/6, 0.5, -1.0, 1.0]) + + # Testing reducing array. + nump, denomp = pade(an, 0, 2) + assert_array_almost_equal(nump.c, [0.5, 1.0, 1.0]) + assert_array_almost_equal(denomp.c, [1.0]) + + nump, denomp = pade(an, 1, 1) + assert_array_almost_equal(nump.c, [1.0/2, 1.0]) + assert_array_almost_equal(denomp.c, [-1.0/2, 1.0]) + + nump, denomp = pade(an, 2, 0) + assert_array_almost_equal(nump.c, [1.0]) + assert_array_almost_equal(denomp.c, [1.0/2, -1.0, 1.0]) + + +def test_pade_ints(): + # Simple test sequences (one of ints, one of floats). + an_int = [1, 2, 3, 4] + an_flt = [1.0, 2.0, 3.0, 4.0] + + # Make sure integer arrays give the same result as float arrays with same values. + for i in range(0, len(an_int)): + for j in range(0, len(an_int) - i): + + # Create float and int pade approximation for given order. + nump_int, denomp_int = pade(an_int, i, j) + nump_flt, denomp_flt = pade(an_flt, i, j) + + # Check that they are the same. + xp_assert_equal(nump_int.c, nump_flt.c) + xp_assert_equal(denomp_int.c, denomp_flt.c) + + +def test_pade_complex(): + # Test sequence with known solutions - see page 6 of 10.1109/PESGM.2012.6344759. + # Variable x is parameter - these tests will work with any complex number. + x = 0.2 + 0.6j + an = [1.0, x, -x*x.conjugate(), x.conjugate()*(x**2) + x*(x.conjugate()**2), + -(x**3)*x.conjugate() - 3*(x*x.conjugate())**2 - x*(x.conjugate()**3)] + + nump, denomp = pade(an, 1, 1) + assert_array_almost_equal(nump.c, [x + x.conjugate(), 1.0]) + assert_array_almost_equal(denomp.c, [x.conjugate(), 1.0]) + + nump, denomp = pade(an, 1, 2) + assert_array_almost_equal(nump.c, [x**2, 2*x + x.conjugate(), 1.0]) + assert_array_almost_equal(denomp.c, [x + x.conjugate(), 1.0]) + + nump, denomp = pade(an, 2, 2) + assert_array_almost_equal( + nump.c, + [x**2 + x*x.conjugate() + x.conjugate()**2, 2*(x + x.conjugate()), 1.0] + ) + assert_array_almost_equal(denomp.c, [x.conjugate()**2, x + 2*x.conjugate(), 1.0]) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_polyint.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_polyint.py new file mode 100644 index 0000000000000000000000000000000000000000..e3e6cb7894ea344289c12b522b45c5e0f22748e6 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_polyint.py @@ -0,0 +1,972 @@ +import warnings +import io +import numpy as np + +from scipy._lib._array_api import ( + xp_assert_equal, xp_assert_close, assert_array_almost_equal, assert_almost_equal +) +from pytest import raises as assert_raises +import pytest + +from scipy.interpolate import ( + KroghInterpolator, krogh_interpolate, + BarycentricInterpolator, barycentric_interpolate, + approximate_taylor_polynomial, CubicHermiteSpline, pchip, + PchipInterpolator, pchip_interpolate, Akima1DInterpolator, CubicSpline, + make_interp_spline) +from scipy._lib._testutils import _run_concurrent_barrier + + +def check_shape(interpolator_cls, x_shape, y_shape, deriv_shape=None, axis=0, + extra_args=None): + if extra_args is None: + extra_args = {} + rng = np.random.RandomState(1234) + + x = [-1, 0, 1, 2, 3, 4] + s = list(range(1, len(y_shape)+1)) + s.insert(axis % (len(y_shape)+1), 0) + y = rng.rand(*((6,) + y_shape)).transpose(s) + + xi = np.zeros(x_shape) + if interpolator_cls is CubicHermiteSpline: + dydx = rng.rand(*((6,) + y_shape)).transpose(s) + yi = interpolator_cls(x, y, dydx, axis=axis, **extra_args)(xi) + else: + yi = interpolator_cls(x, y, axis=axis, **extra_args)(xi) + + target_shape = ((deriv_shape or ()) + y.shape[:axis] + + x_shape + y.shape[axis:][1:]) + assert yi.shape == target_shape + + # check it works also with lists + if x_shape and y.size > 0: + if interpolator_cls is CubicHermiteSpline: + interpolator_cls(list(x), list(y), list(dydx), axis=axis, + **extra_args)(list(xi)) + else: + interpolator_cls(list(x), list(y), axis=axis, + **extra_args)(list(xi)) + + # check also values + if xi.size > 0 and deriv_shape is None: + bs_shape = y.shape[:axis] + (1,)*len(x_shape) + y.shape[axis:][1:] + yv = y[((slice(None,),)*(axis % y.ndim)) + (1,)] + yv = yv.reshape(bs_shape) + + yi, y = np.broadcast_arrays(yi, yv) + xp_assert_close(yi, y) + + +SHAPES = [(), (0,), (1,), (6, 2, 5)] + + +def test_shapes(): + + def spl_interp(x, y, axis): + return make_interp_spline(x, y, axis=axis) + + for ip in [KroghInterpolator, BarycentricInterpolator, CubicHermiteSpline, + pchip, Akima1DInterpolator, CubicSpline, spl_interp]: + for s1 in SHAPES: + for s2 in SHAPES: + for axis in range(-len(s2), len(s2)): + if ip != CubicSpline: + check_shape(ip, s1, s2, None, axis) + else: + for bc in ['natural', 'clamped']: + extra = {'bc_type': bc} + check_shape(ip, s1, s2, None, axis, extra) + +def test_derivs_shapes(): + for ip in [KroghInterpolator, BarycentricInterpolator]: + def interpolator_derivs(x, y, axis=0): + return ip(x, y, axis).derivatives + + for s1 in SHAPES: + for s2 in SHAPES: + for axis in range(-len(s2), len(s2)): + check_shape(interpolator_derivs, s1, s2, (6,), axis) + + +def test_deriv_shapes(): + def krogh_deriv(x, y, axis=0): + return KroghInterpolator(x, y, axis).derivative + + def bary_deriv(x, y, axis=0): + return BarycentricInterpolator(x, y, axis).derivative + + def pchip_deriv(x, y, axis=0): + return pchip(x, y, axis).derivative() + + def pchip_deriv2(x, y, axis=0): + return pchip(x, y, axis).derivative(2) + + def pchip_antideriv(x, y, axis=0): + return pchip(x, y, axis).antiderivative() + + def pchip_antideriv2(x, y, axis=0): + return pchip(x, y, axis).antiderivative(2) + + def pchip_deriv_inplace(x, y, axis=0): + class P(PchipInterpolator): + def __call__(self, x): + return PchipInterpolator.__call__(self, x, 1) + pass + return P(x, y, axis) + + def akima_deriv(x, y, axis=0): + return Akima1DInterpolator(x, y, axis).derivative() + + def akima_antideriv(x, y, axis=0): + return Akima1DInterpolator(x, y, axis).antiderivative() + + def cspline_deriv(x, y, axis=0): + return CubicSpline(x, y, axis).derivative() + + def cspline_antideriv(x, y, axis=0): + return CubicSpline(x, y, axis).antiderivative() + + def bspl_deriv(x, y, axis=0): + return make_interp_spline(x, y, axis=axis).derivative() + + def bspl_antideriv(x, y, axis=0): + return make_interp_spline(x, y, axis=axis).antiderivative() + + for ip in [krogh_deriv, bary_deriv, pchip_deriv, pchip_deriv2, pchip_deriv_inplace, + pchip_antideriv, pchip_antideriv2, akima_deriv, akima_antideriv, + cspline_deriv, cspline_antideriv, bspl_deriv, bspl_antideriv]: + for s1 in SHAPES: + for s2 in SHAPES: + for axis in range(-len(s2), len(s2)): + check_shape(ip, s1, s2, (), axis) + + +def test_complex(): + x = [1, 2, 3, 4] + y = [1, 2, 1j, 3] + + for ip in [KroghInterpolator, BarycentricInterpolator, CubicSpline]: + p = ip(x, y) + xp_assert_close(p(x), np.asarray(y)) + + dydx = [0, -1j, 2, 3j] + p = CubicHermiteSpline(x, y, dydx) + xp_assert_close(p(x), np.asarray(y)) + xp_assert_close(p(x, 1), np.asarray(dydx)) + + +class TestKrogh: + def setup_method(self): + self.true_poly = np.polynomial.Polynomial([-4, 5, 1, 3, -2]) + self.test_xs = np.linspace(-1,1,100) + self.xs = np.linspace(-1,1,5) + self.ys = self.true_poly(self.xs) + + def test_lagrange(self): + P = KroghInterpolator(self.xs,self.ys) + assert_almost_equal(self.true_poly(self.test_xs),P(self.test_xs)) + + def test_scalar(self): + P = KroghInterpolator(self.xs,self.ys) + assert_almost_equal(self.true_poly(7), P(7), check_0d=False) + assert_almost_equal(self.true_poly(np.array(7)), P(np.array(7)), check_0d=False) + + def test_derivatives(self): + P = KroghInterpolator(self.xs,self.ys) + D = P.derivatives(self.test_xs) + for i in range(D.shape[0]): + assert_almost_equal(self.true_poly.deriv(i)(self.test_xs), + D[i]) + + def test_low_derivatives(self): + P = KroghInterpolator(self.xs,self.ys) + D = P.derivatives(self.test_xs,len(self.xs)+2) + for i in range(D.shape[0]): + assert_almost_equal(self.true_poly.deriv(i)(self.test_xs), + D[i]) + + def test_derivative(self): + P = KroghInterpolator(self.xs,self.ys) + m = 10 + r = P.derivatives(self.test_xs,m) + for i in range(m): + assert_almost_equal(P.derivative(self.test_xs,i),r[i]) + + def test_high_derivative(self): + P = KroghInterpolator(self.xs,self.ys) + for i in range(len(self.xs), 2*len(self.xs)): + assert_almost_equal(P.derivative(self.test_xs,i), + np.zeros(len(self.test_xs))) + + def test_ndim_derivatives(self): + poly1 = self.true_poly + poly2 = np.polynomial.Polynomial([-2, 5, 3, -1]) + poly3 = np.polynomial.Polynomial([12, -3, 4, -5, 6]) + ys = np.stack((poly1(self.xs), poly2(self.xs), poly3(self.xs)), axis=-1) + + P = KroghInterpolator(self.xs, ys, axis=0) + D = P.derivatives(self.test_xs) + for i in range(D.shape[0]): + xp_assert_close(D[i], + np.stack((poly1.deriv(i)(self.test_xs), + poly2.deriv(i)(self.test_xs), + poly3.deriv(i)(self.test_xs)), + axis=-1)) + + def test_ndim_derivative(self): + poly1 = self.true_poly + poly2 = np.polynomial.Polynomial([-2, 5, 3, -1]) + poly3 = np.polynomial.Polynomial([12, -3, 4, -5, 6]) + ys = np.stack((poly1(self.xs), poly2(self.xs), poly3(self.xs)), axis=-1) + + P = KroghInterpolator(self.xs, ys, axis=0) + for i in range(P.n): + xp_assert_close(P.derivative(self.test_xs, i), + np.stack((poly1.deriv(i)(self.test_xs), + poly2.deriv(i)(self.test_xs), + poly3.deriv(i)(self.test_xs)), + axis=-1)) + + def test_hermite(self): + P = KroghInterpolator(self.xs,self.ys) + assert_almost_equal(self.true_poly(self.test_xs),P(self.test_xs)) + + def test_vector(self): + xs = [0, 1, 2] + ys = np.array([[0,1],[1,0],[2,1]]) + P = KroghInterpolator(xs,ys) + Pi = [KroghInterpolator(xs,ys[:,i]) for i in range(ys.shape[1])] + test_xs = np.linspace(-1,3,100) + assert_almost_equal(P(test_xs), + np.asarray([p(test_xs) for p in Pi]).T) + assert_almost_equal(P.derivatives(test_xs), + np.transpose(np.asarray([p.derivatives(test_xs) for p in Pi]), + (1,2,0))) + + def test_empty(self): + P = KroghInterpolator(self.xs,self.ys) + xp_assert_equal(P([]), np.asarray([])) + + def test_shapes_scalarvalue(self): + P = KroghInterpolator(self.xs,self.ys) + assert np.shape(P(0)) == () + assert np.shape(P(np.array(0))) == () + assert np.shape(P([0])) == (1,) + assert np.shape(P([0,1])) == (2,) + + def test_shapes_scalarvalue_derivative(self): + P = KroghInterpolator(self.xs,self.ys) + n = P.n + assert np.shape(P.derivatives(0)) == (n,) + assert np.shape(P.derivatives(np.array(0))) == (n,) + assert np.shape(P.derivatives([0])) == (n, 1) + assert np.shape(P.derivatives([0, 1])) == (n, 2) + + def test_shapes_vectorvalue(self): + P = KroghInterpolator(self.xs,np.outer(self.ys,np.arange(3))) + assert np.shape(P(0)) == (3,) + assert np.shape(P([0])) == (1, 3) + assert np.shape(P([0, 1])) == (2, 3) + + def test_shapes_1d_vectorvalue(self): + P = KroghInterpolator(self.xs,np.outer(self.ys,[1])) + assert np.shape(P(0)) == (1,) + assert np.shape(P([0])) == (1, 1) + assert np.shape(P([0,1])) == (2, 1) + + def test_shapes_vectorvalue_derivative(self): + P = KroghInterpolator(self.xs,np.outer(self.ys,np.arange(3))) + n = P.n + assert np.shape(P.derivatives(0)) == (n, 3) + assert np.shape(P.derivatives([0])) == (n, 1, 3) + assert np.shape(P.derivatives([0,1])) == (n, 2, 3) + + def test_wrapper(self): + P = KroghInterpolator(self.xs, self.ys) + ki = krogh_interpolate + assert_almost_equal(P(self.test_xs), ki(self.xs, self.ys, self.test_xs)) + assert_almost_equal(P.derivative(self.test_xs, 2), + ki(self.xs, self.ys, self.test_xs, der=2)) + assert_almost_equal(P.derivatives(self.test_xs, 2), + ki(self.xs, self.ys, self.test_xs, der=[0, 1])) + + def test_int_inputs(self): + # Check input args are cast correctly to floats, gh-3669 + x = [0, 234, 468, 702, 936, 1170, 1404, 2340, 3744, 6084, 8424, + 13104, 60000] + offset_cdf = np.array([-0.95, -0.86114777, -0.8147762, -0.64072425, + -0.48002351, -0.34925329, -0.26503107, + -0.13148093, -0.12988833, -0.12979296, + -0.12973574, -0.08582937, 0.05]) + f = KroghInterpolator(x, offset_cdf) + + xp_assert_close(abs((f(x) - offset_cdf) / f.derivative(x, 1)), + np.zeros_like(offset_cdf), atol=1e-10) + + def test_derivatives_complex(self): + # regression test for gh-7381: krogh.derivatives(0) fails complex y + x, y = np.array([-1, -1, 0, 1, 1]), np.array([1, 1.0j, 0, -1, 1.0j]) + func = KroghInterpolator(x, y) + cmplx = func.derivatives(0) + + cmplx2 = (KroghInterpolator(x, y.real).derivatives(0) + + 1j*KroghInterpolator(x, y.imag).derivatives(0)) + xp_assert_close(cmplx, cmplx2, atol=1e-15) + + @pytest.mark.thread_unsafe + def test_high_degree_warning(self): + with pytest.warns(UserWarning, match="40 degrees provided,"): + KroghInterpolator(np.arange(40), np.ones(40)) + + @pytest.mark.thread_unsafe + def test_concurrency(self): + P = KroghInterpolator(self.xs, self.ys) + + def worker_fn(_, interp): + interp(self.xs) + + _run_concurrent_barrier(10, worker_fn, P) + + +class TestTaylor: + def test_exponential(self): + degree = 5 + p = approximate_taylor_polynomial(np.exp, 0, degree, 1, 15) + for i in range(degree+1): + assert_almost_equal(p(0),1) + p = p.deriv() + assert_almost_equal(p(0),0) + + +class TestBarycentric: + def setup_method(self): + self.true_poly = np.polynomial.Polynomial([-4, 5, 1, 3, -2]) + self.test_xs = np.linspace(-1, 1, 100) + self.xs = np.linspace(-1, 1, 5) + self.ys = self.true_poly(self.xs) + + def test_lagrange(self): + # Ensure backwards compatible post SPEC7 + P = BarycentricInterpolator(self.xs, self.ys, random_state=1) + xp_assert_close(P(self.test_xs), self.true_poly(self.test_xs)) + + def test_scalar(self): + P = BarycentricInterpolator(self.xs, self.ys, rng=1) + xp_assert_close(P(7), self.true_poly(7), check_0d=False) + xp_assert_close(P(np.array(7)), self.true_poly(np.array(7)), check_0d=False) + + def test_derivatives(self): + P = BarycentricInterpolator(self.xs, self.ys) + D = P.derivatives(self.test_xs) + for i in range(D.shape[0]): + xp_assert_close(self.true_poly.deriv(i)(self.test_xs), D[i]) + + def test_low_derivatives(self): + P = BarycentricInterpolator(self.xs, self.ys) + D = P.derivatives(self.test_xs, len(self.xs)+2) + for i in range(D.shape[0]): + xp_assert_close(self.true_poly.deriv(i)(self.test_xs), + D[i], + atol=1e-12) + + def test_derivative(self): + P = BarycentricInterpolator(self.xs, self.ys) + m = 10 + r = P.derivatives(self.test_xs, m) + for i in range(m): + xp_assert_close(P.derivative(self.test_xs, i), r[i]) + + def test_high_derivative(self): + P = BarycentricInterpolator(self.xs, self.ys) + for i in range(len(self.xs), 5*len(self.xs)): + xp_assert_close(P.derivative(self.test_xs, i), + np.zeros(len(self.test_xs))) + + def test_ndim_derivatives(self): + poly1 = self.true_poly + poly2 = np.polynomial.Polynomial([-2, 5, 3, -1]) + poly3 = np.polynomial.Polynomial([12, -3, 4, -5, 6]) + ys = np.stack((poly1(self.xs), poly2(self.xs), poly3(self.xs)), axis=-1) + + P = BarycentricInterpolator(self.xs, ys, axis=0) + D = P.derivatives(self.test_xs) + for i in range(D.shape[0]): + xp_assert_close(D[i], + np.stack((poly1.deriv(i)(self.test_xs), + poly2.deriv(i)(self.test_xs), + poly3.deriv(i)(self.test_xs)), + axis=-1), + atol=1e-12) + + def test_ndim_derivative(self): + poly1 = self.true_poly + poly2 = np.polynomial.Polynomial([-2, 5, 3, -1]) + poly3 = np.polynomial.Polynomial([12, -3, 4, -5, 6]) + ys = np.stack((poly1(self.xs), poly2(self.xs), poly3(self.xs)), axis=-1) + + P = BarycentricInterpolator(self.xs, ys, axis=0) + for i in range(P.n): + xp_assert_close(P.derivative(self.test_xs, i), + np.stack((poly1.deriv(i)(self.test_xs), + poly2.deriv(i)(self.test_xs), + poly3.deriv(i)(self.test_xs)), + axis=-1), + atol=1e-12) + + def test_delayed(self): + P = BarycentricInterpolator(self.xs) + P.set_yi(self.ys) + assert_almost_equal(self.true_poly(self.test_xs), P(self.test_xs)) + + def test_append(self): + P = BarycentricInterpolator(self.xs[:3], self.ys[:3]) + P.add_xi(self.xs[3:], self.ys[3:]) + assert_almost_equal(self.true_poly(self.test_xs), P(self.test_xs)) + + def test_vector(self): + xs = [0, 1, 2] + ys = np.array([[0, 1], [1, 0], [2, 1]]) + BI = BarycentricInterpolator + P = BI(xs, ys) + Pi = [BI(xs, ys[:, i]) for i in range(ys.shape[1])] + test_xs = np.linspace(-1, 3, 100) + assert_almost_equal(P(test_xs), + np.asarray([p(test_xs) for p in Pi]).T) + + def test_shapes_scalarvalue(self): + P = BarycentricInterpolator(self.xs, self.ys) + assert np.shape(P(0)) == () + assert np.shape(P(np.array(0))) == () + assert np.shape(P([0])) == (1,) + assert np.shape(P([0, 1])) == (2,) + + def test_shapes_scalarvalue_derivative(self): + P = BarycentricInterpolator(self.xs,self.ys) + n = P.n + assert np.shape(P.derivatives(0)) == (n,) + assert np.shape(P.derivatives(np.array(0))) == (n,) + assert np.shape(P.derivatives([0])) == (n,1) + assert np.shape(P.derivatives([0,1])) == (n,2) + + def test_shapes_vectorvalue(self): + P = BarycentricInterpolator(self.xs, np.outer(self.ys, np.arange(3))) + assert np.shape(P(0)) == (3,) + assert np.shape(P([0])) == (1, 3) + assert np.shape(P([0, 1])) == (2, 3) + + def test_shapes_1d_vectorvalue(self): + P = BarycentricInterpolator(self.xs, np.outer(self.ys, [1])) + assert np.shape(P(0)) == (1,) + assert np.shape(P([0])) == (1, 1) + assert np.shape(P([0, 1])) == (2, 1) + + def test_shapes_vectorvalue_derivative(self): + P = BarycentricInterpolator(self.xs,np.outer(self.ys,np.arange(3))) + n = P.n + assert np.shape(P.derivatives(0)) == (n, 3) + assert np.shape(P.derivatives([0])) == (n, 1, 3) + assert np.shape(P.derivatives([0, 1])) == (n, 2, 3) + + def test_wrapper(self): + P = BarycentricInterpolator(self.xs, self.ys, rng=1) + bi = barycentric_interpolate + xp_assert_close(P(self.test_xs), bi(self.xs, self.ys, self.test_xs, rng=1)) + xp_assert_close(P.derivative(self.test_xs, 2), + bi(self.xs, self.ys, self.test_xs, der=2, rng=1)) + xp_assert_close(P.derivatives(self.test_xs, 2), + bi(self.xs, self.ys, self.test_xs, der=[0, 1], rng=1)) + + def test_int_input(self): + x = 1000 * np.arange(1, 11) # np.prod(x[-1] - x[:-1]) overflows + y = np.arange(1, 11) + value = barycentric_interpolate(x, y, 1000 * 9.5) + assert_almost_equal(value, np.asarray(9.5)) + + def test_large_chebyshev(self): + # The weights for Chebyshev points of the second kind have analytically + # solvable weights. Naive calculation of barycentric weights will fail + # for large N because of numerical underflow and overflow. We test + # correctness for large N against analytical Chebyshev weights. + + # Without capacity scaling or permutation, n=800 fails, + # With just capacity scaling, n=1097 fails + # With both capacity scaling and random permutation, n=30000 succeeds + n = 1100 + j = np.arange(n + 1).astype(np.float64) + x = np.cos(j * np.pi / n) + + # See page 506 of Berrut and Trefethen 2004 for this formula + w = (-1) ** j + w[0] *= 0.5 + w[-1] *= 0.5 + + P = BarycentricInterpolator(x) + + # It's okay to have a constant scaling factor in the weights because it + # cancels out in the evaluation of the polynomial. + factor = P.wi[0] + assert_almost_equal(P.wi / (2 * factor), w) + + def test_warning(self): + # Test if the divide-by-zero warning is properly ignored when computing + # interpolated values equals to interpolation points + P = BarycentricInterpolator([0, 1], [1, 2]) + with np.errstate(divide='raise'): + yi = P(P.xi) + + # Check if the interpolated values match the input values + # at the nodes + assert_almost_equal(yi, P.yi.ravel()) + + @pytest.mark.thread_unsafe + def test_repeated_node(self): + # check that a repeated node raises a ValueError + # (computing the weights requires division by xi[i] - xi[j]) + xis = np.array([0.1, 0.5, 0.9, 0.5]) + ys = np.array([1, 2, 3, 4]) + with pytest.raises(ValueError, + match="Interpolation points xi must be distinct."): + BarycentricInterpolator(xis, ys) + + @pytest.mark.thread_unsafe + def test_concurrency(self): + P = BarycentricInterpolator(self.xs, self.ys) + + def worker_fn(_, interp): + interp(self.xs) + + _run_concurrent_barrier(10, worker_fn, P) + + +class TestPCHIP: + def _make_random(self, npts=20): + rng = np.random.RandomState(1234) + xi = np.sort(rng.random(npts)) + yi = rng.random(npts) + return pchip(xi, yi), xi, yi + + def test_overshoot(self): + # PCHIP should not overshoot + p, xi, yi = self._make_random() + for i in range(len(xi)-1): + x1, x2 = xi[i], xi[i+1] + y1, y2 = yi[i], yi[i+1] + if y1 > y2: + y1, y2 = y2, y1 + xp = np.linspace(x1, x2, 10) + yp = p(xp) + assert ((y1 <= yp + 1e-15) & (yp <= y2 + 1e-15)).all() + + def test_monotone(self): + # PCHIP should preserve monotonicty + p, xi, yi = self._make_random() + for i in range(len(xi)-1): + x1, x2 = xi[i], xi[i+1] + y1, y2 = yi[i], yi[i+1] + xp = np.linspace(x1, x2, 10) + yp = p(xp) + assert ((y2-y1) * (yp[1:] - yp[:1]) > 0).all() + + def test_cast(self): + # regression test for integer input data, see gh-3453 + data = np.array([[0, 4, 12, 27, 47, 60, 79, 87, 99, 100], + [-33, -33, -19, -2, 12, 26, 38, 45, 53, 55]]) + xx = np.arange(100) + curve = pchip(data[0], data[1])(xx) + + data1 = data * 1.0 + curve1 = pchip(data1[0], data1[1])(xx) + + xp_assert_close(curve, curve1, atol=1e-14, rtol=1e-14) + + def test_nag(self): + # Example from NAG C implementation, + # http://nag.com/numeric/cl/nagdoc_cl25/html/e01/e01bec.html + # suggested in gh-5326 as a smoke test for the way the derivatives + # are computed (see also gh-3453) + dataStr = ''' + 7.99 0.00000E+0 + 8.09 0.27643E-4 + 8.19 0.43750E-1 + 8.70 0.16918E+0 + 9.20 0.46943E+0 + 10.00 0.94374E+0 + 12.00 0.99864E+0 + 15.00 0.99992E+0 + 20.00 0.99999E+0 + ''' + data = np.loadtxt(io.StringIO(dataStr)) + pch = pchip(data[:,0], data[:,1]) + + resultStr = ''' + 7.9900 0.0000 + 9.1910 0.4640 + 10.3920 0.9645 + 11.5930 0.9965 + 12.7940 0.9992 + 13.9950 0.9998 + 15.1960 0.9999 + 16.3970 1.0000 + 17.5980 1.0000 + 18.7990 1.0000 + 20.0000 1.0000 + ''' + result = np.loadtxt(io.StringIO(resultStr)) + xp_assert_close(result[:,1], pch(result[:,0]), rtol=0., atol=5e-5) + + def test_endslopes(self): + # this is a smoke test for gh-3453: PCHIP interpolator should not + # set edge slopes to zero if the data do not suggest zero edge derivatives + x = np.array([0.0, 0.1, 0.25, 0.35]) + y1 = np.array([279.35, 0.5e3, 1.0e3, 2.5e3]) + y2 = np.array([279.35, 2.5e3, 1.50e3, 1.0e3]) + for pp in (pchip(x, y1), pchip(x, y2)): + for t in (x[0], x[-1]): + assert pp(t, 1) != 0 + + @pytest.mark.thread_unsafe + def test_all_zeros(self): + x = np.arange(10) + y = np.zeros_like(x) + + # this should work and not generate any warnings + with warnings.catch_warnings(): + warnings.filterwarnings('error') + pch = pchip(x, y) + + xx = np.linspace(0, 9, 101) + assert all(pch(xx) == 0.) + + def test_two_points(self): + # regression test for gh-6222: pchip([0, 1], [0, 1]) fails because + # it tries to use a three-point scheme to estimate edge derivatives, + # while there are only two points available. + # Instead, it should construct a linear interpolator. + x = np.linspace(0, 1, 11) + p = pchip([0, 1], [0, 2]) + xp_assert_close(p(x), 2*x, atol=1e-15) + + def test_pchip_interpolate(self): + assert_array_almost_equal( + pchip_interpolate([1, 2, 3], [4, 5, 6], [0.5], der=1), + np.asarray([1.])) + + assert_array_almost_equal( + pchip_interpolate([1, 2, 3], [4, 5, 6], [0.5], der=0), + np.asarray([3.5])) + + assert_array_almost_equal( + np.asarray(pchip_interpolate([1, 2, 3], [4, 5, 6], [0.5], der=[0, 1])), + np.asarray([[3.5], [1]])) + + def test_roots(self): + # regression test for gh-6357: .roots method should work + p = pchip([0, 1], [-1, 1]) + r = p.roots() + xp_assert_close(r, np.asarray([0.5])) + + +class TestCubicSpline: + @staticmethod + def check_correctness(S, bc_start='not-a-knot', bc_end='not-a-knot', + tol=1e-14): + """Check that spline coefficients satisfy the continuity and boundary + conditions.""" + x = S.x + c = S.c + dx = np.diff(x) + dx = dx.reshape([dx.shape[0]] + [1] * (c.ndim - 2)) + dxi = dx[:-1] + + # Check C2 continuity. + xp_assert_close(c[3, 1:], c[0, :-1] * dxi**3 + c[1, :-1] * dxi**2 + + c[2, :-1] * dxi + c[3, :-1], rtol=tol, atol=tol) + xp_assert_close(c[2, 1:], 3 * c[0, :-1] * dxi**2 + + 2 * c[1, :-1] * dxi + c[2, :-1], rtol=tol, atol=tol) + xp_assert_close(c[1, 1:], 3 * c[0, :-1] * dxi + c[1, :-1], + rtol=tol, atol=tol) + + # Check that we found a parabola, the third derivative is 0. + if x.size == 3 and bc_start == 'not-a-knot' and bc_end == 'not-a-knot': + xp_assert_close(c[0], np.zeros_like(c[0]), rtol=tol, atol=tol) + return + + # Check periodic boundary conditions. + if bc_start == 'periodic': + xp_assert_close(S(x[0], 0), S(x[-1], 0), rtol=tol, atol=tol) + xp_assert_close(S(x[0], 1), S(x[-1], 1), rtol=tol, atol=tol) + xp_assert_close(S(x[0], 2), S(x[-1], 2), rtol=tol, atol=tol) + return + + # Check other boundary conditions. + if bc_start == 'not-a-knot': + if x.size == 2: + slope = (S(x[1]) - S(x[0])) / dx[0] + slope = np.asarray(slope) + xp_assert_close(S(x[0], 1), slope, rtol=tol, atol=tol) + else: + xp_assert_close(c[0, 0], c[0, 1], rtol=tol, atol=tol) + elif bc_start == 'clamped': + xp_assert_close( + S(x[0], 1), np.zeros_like(S(x[0], 1)), rtol=tol, atol=tol) + elif bc_start == 'natural': + xp_assert_close( + S(x[0], 2), np.zeros_like(S(x[0], 2)), rtol=tol, atol=tol) + else: + order, value = bc_start + xp_assert_close(S(x[0], order), np.asarray(value), rtol=tol, atol=tol) + + if bc_end == 'not-a-knot': + if x.size == 2: + slope = (S(x[1]) - S(x[0])) / dx[0] + slope = np.asarray(slope) + xp_assert_close(S(x[1], 1), slope, rtol=tol, atol=tol) + else: + xp_assert_close(c[0, -1], c[0, -2], rtol=tol, atol=tol) + elif bc_end == 'clamped': + xp_assert_close(S(x[-1], 1), np.zeros_like(S(x[-1], 1)), + rtol=tol, atol=tol) + elif bc_end == 'natural': + xp_assert_close(S(x[-1], 2), np.zeros_like(S(x[-1], 2)), + rtol=2*tol, atol=2*tol) + else: + order, value = bc_end + xp_assert_close(S(x[-1], order), np.asarray(value), rtol=tol, atol=tol) + + def check_all_bc(self, x, y, axis): + deriv_shape = list(y.shape) + del deriv_shape[axis] + first_deriv = np.empty(deriv_shape) + first_deriv.fill(2) + second_deriv = np.empty(deriv_shape) + second_deriv.fill(-1) + bc_all = [ + 'not-a-knot', + 'natural', + 'clamped', + (1, first_deriv), + (2, second_deriv) + ] + for bc in bc_all[:3]: + S = CubicSpline(x, y, axis=axis, bc_type=bc) + self.check_correctness(S, bc, bc) + + for bc_start in bc_all: + for bc_end in bc_all: + S = CubicSpline(x, y, axis=axis, bc_type=(bc_start, bc_end)) + self.check_correctness(S, bc_start, bc_end, tol=2e-14) + + def test_general(self): + x = np.array([-1, 0, 0.5, 2, 4, 4.5, 5.5, 9]) + y = np.array([0, -0.5, 2, 3, 2.5, 1, 1, 0.5]) + for n in [2, 3, x.size]: + self.check_all_bc(x[:n], y[:n], 0) + + Y = np.empty((2, n, 2)) + Y[0, :, 0] = y[:n] + Y[0, :, 1] = y[:n] - 1 + Y[1, :, 0] = y[:n] + 2 + Y[1, :, 1] = y[:n] + 3 + self.check_all_bc(x[:n], Y, 1) + + def test_periodic(self): + for n in [2, 3, 5]: + x = np.linspace(0, 2 * np.pi, n) + y = np.cos(x) + S = CubicSpline(x, y, bc_type='periodic') + self.check_correctness(S, 'periodic', 'periodic') + + Y = np.empty((2, n, 2)) + Y[0, :, 0] = y + Y[0, :, 1] = y + 2 + Y[1, :, 0] = y - 1 + Y[1, :, 1] = y + 5 + S = CubicSpline(x, Y, axis=1, bc_type='periodic') + self.check_correctness(S, 'periodic', 'periodic') + + def test_periodic_eval(self): + x = np.linspace(0, 2 * np.pi, 10) + y = np.cos(x) + S = CubicSpline(x, y, bc_type='periodic') + assert_almost_equal(S(1), S(1 + 2 * np.pi), decimal=15) + + def test_second_derivative_continuity_gh_11758(self): + # gh-11758: C2 continuity fail + x = np.array([0.9, 1.3, 1.9, 2.1, 2.6, 3.0, 3.9, 4.4, 4.7, 5.0, 6.0, + 7.0, 8.0, 9.2, 10.5, 11.3, 11.6, 12.0, 12.6, 13.0, 13.3]) + y = np.array([1.3, 1.5, 1.85, 2.1, 2.6, 2.7, 2.4, 2.15, 2.05, 2.1, + 2.25, 2.3, 2.25, 1.95, 1.4, 0.9, 0.7, 0.6, 0.5, 0.4, 1.3]) + S = CubicSpline(x, y, bc_type='periodic', extrapolate='periodic') + self.check_correctness(S, 'periodic', 'periodic') + + def test_three_points(self): + # gh-11758: Fails computing a_m2_m1 + # In this case, s (first derivatives) could be found manually by solving + # system of 2 linear equations. Due to solution of this system, + # s[i] = (h1m2 + h2m1) / (h1 + h2), where h1 = x[1] - x[0], h2 = x[2] - x[1], + # m1 = (y[1] - y[0]) / h1, m2 = (y[2] - y[1]) / h2 + x = np.array([1.0, 2.75, 3.0]) + y = np.array([1.0, 15.0, 1.0]) + S = CubicSpline(x, y, bc_type='periodic') + self.check_correctness(S, 'periodic', 'periodic') + xp_assert_close(S.derivative(1)(x), np.array([-48.0, -48.0, -48.0])) + + def test_periodic_three_points_multidim(self): + # make sure one multidimensional interpolator does the same as multiple + # one-dimensional interpolators + x = np.array([0.0, 1.0, 3.0]) + y = np.array([[0.0, 1.0], [1.0, 0.0], [0.0, 1.0]]) + S = CubicSpline(x, y, bc_type="periodic") + self.check_correctness(S, 'periodic', 'periodic') + S0 = CubicSpline(x, y[:, 0], bc_type="periodic") + S1 = CubicSpline(x, y[:, 1], bc_type="periodic") + q = np.linspace(0, 2, 5) + xp_assert_close(S(q)[:, 0], S0(q)) + xp_assert_close(S(q)[:, 1], S1(q)) + + def test_dtypes(self): + x = np.array([0, 1, 2, 3], dtype=int) + y = np.array([-5, 2, 3, 1], dtype=int) + S = CubicSpline(x, y) + self.check_correctness(S) + + y = np.array([-1+1j, 0.0, 1-1j, 0.5-1.5j]) + S = CubicSpline(x, y) + self.check_correctness(S) + + S = CubicSpline(x, x ** 3, bc_type=("natural", (1, 2j))) + self.check_correctness(S, "natural", (1, 2j)) + + y = np.array([-5, 2, 3, 1]) + S = CubicSpline(x, y, bc_type=[(1, 2 + 0.5j), (2, 0.5 - 1j)]) + self.check_correctness(S, (1, 2 + 0.5j), (2, 0.5 - 1j)) + + def test_small_dx(self): + rng = np.random.RandomState(0) + x = np.sort(rng.uniform(size=100)) + y = 1e4 + rng.uniform(size=100) + S = CubicSpline(x, y) + self.check_correctness(S, tol=1e-13) + + def test_incorrect_inputs(self): + x = np.array([1, 2, 3, 4]) + y = np.array([1, 2, 3, 4]) + xc = np.array([1 + 1j, 2, 3, 4]) + xn = np.array([np.nan, 2, 3, 4]) + xo = np.array([2, 1, 3, 4]) + yn = np.array([np.nan, 2, 3, 4]) + y3 = [1, 2, 3] + x1 = [1] + y1 = [1] + + assert_raises(ValueError, CubicSpline, xc, y) + assert_raises(ValueError, CubicSpline, xn, y) + assert_raises(ValueError, CubicSpline, x, yn) + assert_raises(ValueError, CubicSpline, xo, y) + assert_raises(ValueError, CubicSpline, x, y3) + assert_raises(ValueError, CubicSpline, x[:, np.newaxis], y) + assert_raises(ValueError, CubicSpline, x1, y1) + + wrong_bc = [('periodic', 'clamped'), + ((2, 0), (3, 10)), + ((1, 0), ), + (0., 0.), + 'not-a-typo'] + + for bc_type in wrong_bc: + assert_raises(ValueError, CubicSpline, x, y, 0, bc_type, True) + + # Shapes mismatch when giving arbitrary derivative values: + Y = np.c_[y, y] + bc1 = ('clamped', (1, 0)) + bc2 = ('clamped', (1, [0, 0, 0])) + bc3 = ('clamped', (1, [[0, 0]])) + assert_raises(ValueError, CubicSpline, x, Y, 0, bc1, True) + assert_raises(ValueError, CubicSpline, x, Y, 0, bc2, True) + assert_raises(ValueError, CubicSpline, x, Y, 0, bc3, True) + + # periodic condition, y[-1] must be equal to y[0]: + assert_raises(ValueError, CubicSpline, x, y, 0, 'periodic', True) + + +def test_CubicHermiteSpline_correctness(): + x = [0, 2, 7] + y = [-1, 2, 3] + dydx = [0, 3, 7] + s = CubicHermiteSpline(x, y, dydx) + xp_assert_close(s(x), y, check_shape=False, check_dtype=False, rtol=1e-15) + xp_assert_close(s(x, 1), dydx, check_shape=False, check_dtype=False, rtol=1e-15) + + +def test_CubicHermiteSpline_error_handling(): + x = [1, 2, 3] + y = [0, 3, 5] + dydx = [1, -1, 2, 3] + assert_raises(ValueError, CubicHermiteSpline, x, y, dydx) + + dydx_with_nan = [1, 0, np.nan] + assert_raises(ValueError, CubicHermiteSpline, x, y, dydx_with_nan) + + +def test_roots_extrapolate_gh_11185(): + x = np.array([0.001, 0.002]) + y = np.array([1.66066935e-06, 1.10410807e-06]) + dy = np.array([-1.60061854, -1.600619]) + p = CubicHermiteSpline(x, y, dy) + + # roots(extrapolate=True) for a polynomial with a single interval + # should return all three real roots + r = p.roots(extrapolate=True) + assert p.c.shape[1] == 1 + assert r.size == 3 + + +class TestZeroSizeArrays: + # regression tests for gh-17241 : CubicSpline et al must not segfault + # when y.size == 0 + # The two methods below are _almost_ the same, but not quite: + # one is for objects which have the `bc_type` argument (CubicSpline) + # and the other one is for those which do not (Pchip, Akima1D) + + @pytest.mark.parametrize('y', [np.zeros((10, 0, 5)), + np.zeros((10, 5, 0))]) + @pytest.mark.parametrize('bc_type', + ['not-a-knot', 'periodic', 'natural', 'clamped']) + @pytest.mark.parametrize('axis', [0, 1, 2]) + @pytest.mark.parametrize('cls', [make_interp_spline, CubicSpline]) + def test_zero_size(self, cls, y, bc_type, axis): + x = np.arange(10) + xval = np.arange(3) + + obj = cls(x, y, bc_type=bc_type) + assert obj(xval).size == 0 + assert obj(xval).shape == xval.shape + y.shape[1:] + + # Also check with an explicit non-default axis + yt = np.moveaxis(y, 0, axis) # (10, 0, 5) --> (0, 10, 5) if axis=1 etc + + obj = cls(x, yt, bc_type=bc_type, axis=axis) + sh = yt.shape[:axis] + (xval.size, ) + yt.shape[axis+1:] + assert obj(xval).size == 0 + assert obj(xval).shape == sh + + @pytest.mark.parametrize('y', [np.zeros((10, 0, 5)), + np.zeros((10, 5, 0))]) + @pytest.mark.parametrize('axis', [0, 1, 2]) + @pytest.mark.parametrize('cls', [PchipInterpolator, Akima1DInterpolator]) + def test_zero_size_2(self, cls, y, axis): + x = np.arange(10) + xval = np.arange(3) + + obj = cls(x, y) + assert obj(xval).size == 0 + assert obj(xval).shape == xval.shape + y.shape[1:] + + # Also check with an explicit non-default axis + yt = np.moveaxis(y, 0, axis) # (10, 0, 5) --> (0, 10, 5) if axis=1 etc + + obj = cls(x, yt, axis=axis) + sh = yt.shape[:axis] + (xval.size, ) + yt.shape[axis+1:] + assert obj(xval).size == 0 + assert obj(xval).shape == sh diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_rbf.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_rbf.py new file mode 100644 index 0000000000000000000000000000000000000000..d824a84a80eda316b680ba4e43d0f418c774af99 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_rbf.py @@ -0,0 +1,246 @@ +# Created by John Travers, Robert Hetland, 2007 +""" Test functions for rbf module """ + +import numpy as np + +from scipy._lib._array_api import assert_array_almost_equal, assert_almost_equal + +from numpy import linspace, sin, cos, exp, allclose +from scipy.interpolate._rbf import Rbf +from scipy._lib._testutils import _run_concurrent_barrier + +import pytest + + +FUNCTIONS = ('multiquadric', 'inverse multiquadric', 'gaussian', + 'cubic', 'quintic', 'thin-plate', 'linear') + + +def check_rbf1d_interpolation(function): + # Check that the Rbf function interpolates through the nodes (1D) + x = linspace(0,10,9) + y = sin(x) + rbf = Rbf(x, y, function=function) + yi = rbf(x) + assert_array_almost_equal(y, yi) + assert_almost_equal(rbf(float(x[0])), y[0], check_0d=False) + + +def check_rbf2d_interpolation(function): + # Check that the Rbf function interpolates through the nodes (2D). + rng = np.random.RandomState(1234) + x = rng.rand(50,1)*4-2 + y = rng.rand(50,1)*4-2 + z = x*exp(-x**2-1j*y**2) + rbf = Rbf(x, y, z, epsilon=2, function=function) + zi = rbf(x, y) + zi.shape = x.shape + assert_array_almost_equal(z, zi) + + +def check_rbf3d_interpolation(function): + # Check that the Rbf function interpolates through the nodes (3D). + rng = np.random.RandomState(1234) + x = rng.rand(50, 1)*4 - 2 + y = rng.rand(50, 1)*4 - 2 + z = rng.rand(50, 1)*4 - 2 + d = x*exp(-x**2 - y**2) + rbf = Rbf(x, y, z, d, epsilon=2, function=function) + di = rbf(x, y, z) + di.shape = x.shape + assert_array_almost_equal(di, d) + + +def test_rbf_interpolation(): + for function in FUNCTIONS: + check_rbf1d_interpolation(function) + check_rbf2d_interpolation(function) + check_rbf3d_interpolation(function) + + +def check_2drbf1d_interpolation(function): + # Check that the 2-D Rbf function interpolates through the nodes (1D) + x = linspace(0, 10, 9) + y0 = sin(x) + y1 = cos(x) + y = np.vstack([y0, y1]).T + rbf = Rbf(x, y, function=function, mode='N-D') + yi = rbf(x) + assert_array_almost_equal(y, yi) + assert_almost_equal(rbf(float(x[0])), y[0]) + + +def check_2drbf2d_interpolation(function): + # Check that the 2-D Rbf function interpolates through the nodes (2D). + rng = np.random.RandomState(1234) + x = rng.rand(50, ) * 4 - 2 + y = rng.rand(50, ) * 4 - 2 + z0 = x * exp(-x ** 2 - 1j * y ** 2) + z1 = y * exp(-y ** 2 - 1j * x ** 2) + z = np.vstack([z0, z1]).T + rbf = Rbf(x, y, z, epsilon=2, function=function, mode='N-D') + zi = rbf(x, y) + zi.shape = z.shape + assert_array_almost_equal(z, zi) + + +def check_2drbf3d_interpolation(function): + # Check that the 2-D Rbf function interpolates through the nodes (3D). + rng = np.random.RandomState(1234) + x = rng.rand(50, ) * 4 - 2 + y = rng.rand(50, ) * 4 - 2 + z = rng.rand(50, ) * 4 - 2 + d0 = x * exp(-x ** 2 - y ** 2) + d1 = y * exp(-y ** 2 - x ** 2) + d = np.vstack([d0, d1]).T + rbf = Rbf(x, y, z, d, epsilon=2, function=function, mode='N-D') + di = rbf(x, y, z) + di.shape = d.shape + assert_array_almost_equal(di, d) + + +def test_2drbf_interpolation(): + for function in FUNCTIONS: + check_2drbf1d_interpolation(function) + check_2drbf2d_interpolation(function) + check_2drbf3d_interpolation(function) + + +def check_rbf1d_regularity(function, atol): + # Check that the Rbf function approximates a smooth function well away + # from the nodes. + x = linspace(0, 10, 9) + y = sin(x) + rbf = Rbf(x, y, function=function) + xi = linspace(0, 10, 100) + yi = rbf(xi) + msg = f"abs-diff: {abs(yi - sin(xi)).max():f}" + assert allclose(yi, sin(xi), atol=atol), msg + + +def test_rbf_regularity(): + tolerances = { + 'multiquadric': 0.1, + 'inverse multiquadric': 0.15, + 'gaussian': 0.15, + 'cubic': 0.15, + 'quintic': 0.1, + 'thin-plate': 0.1, + 'linear': 0.2 + } + for function in FUNCTIONS: + check_rbf1d_regularity(function, tolerances.get(function, 1e-2)) + + +def check_2drbf1d_regularity(function, atol): + # Check that the 2-D Rbf function approximates a smooth function well away + # from the nodes. + x = linspace(0, 10, 9) + y0 = sin(x) + y1 = cos(x) + y = np.vstack([y0, y1]).T + rbf = Rbf(x, y, function=function, mode='N-D') + xi = linspace(0, 10, 100) + yi = rbf(xi) + msg = f"abs-diff: {abs(yi - np.vstack([sin(xi), cos(xi)]).T).max():f}" + assert allclose(yi, np.vstack([sin(xi), cos(xi)]).T, atol=atol), msg + + +def test_2drbf_regularity(): + tolerances = { + 'multiquadric': 0.1, + 'inverse multiquadric': 0.15, + 'gaussian': 0.15, + 'cubic': 0.15, + 'quintic': 0.1, + 'thin-plate': 0.15, + 'linear': 0.2 + } + for function in FUNCTIONS: + check_2drbf1d_regularity(function, tolerances.get(function, 1e-2)) + + +def check_rbf1d_stability(function): + # Check that the Rbf function with default epsilon is not subject + # to overshoot. Regression for issue #4523. + # + # Generate some data (fixed random seed hence deterministic) + rng = np.random.RandomState(1234) + x = np.linspace(0, 10, 50) + z = x + 4.0 * rng.randn(len(x)) + + rbf = Rbf(x, z, function=function) + xi = np.linspace(0, 10, 1000) + yi = rbf(xi) + + # subtract the linear trend and make sure there no spikes + assert np.abs(yi-xi).max() / np.abs(z-x).max() < 1.1 + +def test_rbf_stability(): + for function in FUNCTIONS: + check_rbf1d_stability(function) + + +def test_default_construction(): + # Check that the Rbf class can be constructed with the default + # multiquadric basis function. Regression test for ticket #1228. + x = linspace(0,10,9) + y = sin(x) + rbf = Rbf(x, y) + yi = rbf(x) + assert_array_almost_equal(y, yi) + + +def test_function_is_callable(): + # Check that the Rbf class can be constructed with function=callable. + x = linspace(0,10,9) + y = sin(x) + def linfunc(x): + return x + rbf = Rbf(x, y, function=linfunc) + yi = rbf(x) + assert_array_almost_equal(y, yi) + + +def test_two_arg_function_is_callable(): + # Check that the Rbf class can be constructed with a two argument + # function=callable. + def _func(self, r): + return self.epsilon + r + + x = linspace(0,10,9) + y = sin(x) + rbf = Rbf(x, y, function=_func) + yi = rbf(x) + assert_array_almost_equal(y, yi) + + +def test_rbf_epsilon_none(): + x = linspace(0, 10, 9) + y = sin(x) + Rbf(x, y, epsilon=None) + + +def test_rbf_epsilon_none_collinear(): + # Check that collinear points in one dimension doesn't cause an error + # due to epsilon = 0 + x = [1, 2, 3] + y = [4, 4, 4] + z = [5, 6, 7] + rbf = Rbf(x, y, z, epsilon=None) + assert rbf.epsilon > 0 + + +@pytest.mark.thread_unsafe +def test_rbf_concurrency(): + x = linspace(0, 10, 100) + y0 = sin(x) + y1 = cos(x) + y = np.vstack([y0, y1]).T + rbf = Rbf(x, y, mode='N-D') + + def worker_fn(_, interp, xp): + interp(xp) + + _run_concurrent_barrier(10, worker_fn, rbf, x) + diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_rbfinterp.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_rbfinterp.py new file mode 100644 index 0000000000000000000000000000000000000000..3d2759fdee41fa64c09bb00979f10b70e49a2855 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_rbfinterp.py @@ -0,0 +1,534 @@ +import pickle +import pytest +import numpy as np +from numpy.linalg import LinAlgError +from scipy._lib._array_api import xp_assert_close +from scipy.stats.qmc import Halton +from scipy.spatial import cKDTree # type: ignore[attr-defined] +from scipy.interpolate._rbfinterp import ( + _AVAILABLE, _SCALE_INVARIANT, _NAME_TO_MIN_DEGREE, _monomial_powers, + RBFInterpolator + ) +from scipy.interpolate import _rbfinterp_pythran +from scipy._lib._testutils import _run_concurrent_barrier + + +def _vandermonde(x, degree): + # Returns a matrix of monomials that span polynomials with the specified + # degree evaluated at x. + powers = _monomial_powers(x.shape[1], degree) + return _rbfinterp_pythran._polynomial_matrix(x, powers) + + +def _1d_test_function(x): + # Test function used in Wahba's "Spline Models for Observational Data". + # domain ~= (0, 3), range ~= (-1.0, 0.2) + x = x[:, 0] + y = 4.26*(np.exp(-x) - 4*np.exp(-2*x) + 3*np.exp(-3*x)) + return y + + +def _2d_test_function(x): + # Franke's test function. + # domain ~= (0, 1) X (0, 1), range ~= (0.0, 1.2) + x1, x2 = x[:, 0], x[:, 1] + term1 = 0.75 * np.exp(-(9*x1-2)**2/4 - (9*x2-2)**2/4) + term2 = 0.75 * np.exp(-(9*x1+1)**2/49 - (9*x2+1)/10) + term3 = 0.5 * np.exp(-(9*x1-7)**2/4 - (9*x2-3)**2/4) + term4 = -0.2 * np.exp(-(9*x1-4)**2 - (9*x2-7)**2) + y = term1 + term2 + term3 + term4 + return y + + +def _is_conditionally_positive_definite(kernel, m): + # Tests whether the kernel is conditionally positive definite of order m. + # See chapter 7 of Fasshauer's "Meshfree Approximation Methods with + # MATLAB". + nx = 10 + ntests = 100 + for ndim in [1, 2, 3, 4, 5]: + # Generate sample points with a Halton sequence to avoid samples that + # are too close to each other, which can make the matrix singular. + seq = Halton(ndim, scramble=False, seed=np.random.RandomState()) + for _ in range(ntests): + x = 2*seq.random(nx) - 1 + A = _rbfinterp_pythran._kernel_matrix(x, kernel) + P = _vandermonde(x, m - 1) + Q, R = np.linalg.qr(P, mode='complete') + # Q2 forms a basis spanning the space where P.T.dot(x) = 0. Project + # A onto this space, and then see if it is positive definite using + # the Cholesky decomposition. If not, then the kernel is not c.p.d. + # of order m. + Q2 = Q[:, P.shape[1]:] + B = Q2.T.dot(A).dot(Q2) + try: + np.linalg.cholesky(B) + except np.linalg.LinAlgError: + return False + + return True + + +# Sorting the parametrize arguments is necessary to avoid a parallelization +# issue described here: https://github.com/pytest-dev/pytest-xdist/issues/432. +@pytest.mark.parametrize('kernel', sorted(_AVAILABLE)) +def test_conditionally_positive_definite(kernel): + # Test if each kernel in _AVAILABLE is conditionally positive definite of + # order m, where m comes from _NAME_TO_MIN_DEGREE. This is a necessary + # condition for the smoothed RBF interpolant to be well-posed in general. + m = _NAME_TO_MIN_DEGREE.get(kernel, -1) + 1 + assert _is_conditionally_positive_definite(kernel, m) + + +class _TestRBFInterpolator: + @pytest.mark.parametrize('kernel', sorted(_SCALE_INVARIANT)) + def test_scale_invariance_1d(self, kernel): + # Verify that the functions in _SCALE_INVARIANT are insensitive to the + # shape parameter (when smoothing == 0) in 1d. + seq = Halton(1, scramble=False, seed=np.random.RandomState()) + x = 3*seq.random(50) + y = _1d_test_function(x) + xitp = 3*seq.random(50) + yitp1 = self.build(x, y, epsilon=1.0, kernel=kernel)(xitp) + yitp2 = self.build(x, y, epsilon=2.0, kernel=kernel)(xitp) + xp_assert_close(yitp1, yitp2, atol=1e-8) + + @pytest.mark.parametrize('kernel', sorted(_SCALE_INVARIANT)) + def test_scale_invariance_2d(self, kernel): + # Verify that the functions in _SCALE_INVARIANT are insensitive to the + # shape parameter (when smoothing == 0) in 2d. + seq = Halton(2, scramble=False, seed=np.random.RandomState()) + x = seq.random(100) + y = _2d_test_function(x) + xitp = seq.random(100) + yitp1 = self.build(x, y, epsilon=1.0, kernel=kernel)(xitp) + yitp2 = self.build(x, y, epsilon=2.0, kernel=kernel)(xitp) + xp_assert_close(yitp1, yitp2, atol=1e-8) + + @pytest.mark.parametrize('kernel', sorted(_AVAILABLE)) + def test_extreme_domains(self, kernel): + # Make sure the interpolant remains numerically stable for very + # large/small domains. + seq = Halton(2, scramble=False, seed=np.random.RandomState()) + scale = 1e50 + shift = 1e55 + + x = seq.random(100) + y = _2d_test_function(x) + xitp = seq.random(100) + + if kernel in _SCALE_INVARIANT: + yitp1 = self.build(x, y, kernel=kernel)(xitp) + yitp2 = self.build( + x*scale + shift, y, + kernel=kernel + )(xitp*scale + shift) + else: + yitp1 = self.build(x, y, epsilon=5.0, kernel=kernel)(xitp) + yitp2 = self.build( + x*scale + shift, y, + epsilon=5.0/scale, + kernel=kernel + )(xitp*scale + shift) + + xp_assert_close(yitp1, yitp2, atol=1e-8) + + def test_polynomial_reproduction(self): + # If the observed data comes from a polynomial, then the interpolant + # should be able to reproduce the polynomial exactly, provided that + # `degree` is sufficiently high. + rng = np.random.RandomState(0) + seq = Halton(2, scramble=False, seed=rng) + degree = 3 + + x = seq.random(50) + xitp = seq.random(50) + + P = _vandermonde(x, degree) + Pitp = _vandermonde(xitp, degree) + + poly_coeffs = rng.normal(0.0, 1.0, P.shape[1]) + + y = P.dot(poly_coeffs) + yitp1 = Pitp.dot(poly_coeffs) + yitp2 = self.build(x, y, degree=degree)(xitp) + + xp_assert_close(yitp1, yitp2, atol=1e-8) + + @pytest.mark.slow + def test_chunking(self, monkeypatch): + # If the observed data comes from a polynomial, then the interpolant + # should be able to reproduce the polynomial exactly, provided that + # `degree` is sufficiently high. + rng = np.random.RandomState(0) + seq = Halton(2, scramble=False, seed=rng) + degree = 3 + + largeN = 1000 + 33 + # this is large to check that chunking of the RBFInterpolator is tested + x = seq.random(50) + xitp = seq.random(largeN) + + P = _vandermonde(x, degree) + Pitp = _vandermonde(xitp, degree) + + poly_coeffs = rng.normal(0.0, 1.0, P.shape[1]) + + y = P.dot(poly_coeffs) + yitp1 = Pitp.dot(poly_coeffs) + interp = self.build(x, y, degree=degree) + ce_real = interp._chunk_evaluator + + def _chunk_evaluator(*args, **kwargs): + kwargs.update(memory_budget=100) + return ce_real(*args, **kwargs) + + monkeypatch.setattr(interp, '_chunk_evaluator', _chunk_evaluator) + yitp2 = interp(xitp) + xp_assert_close(yitp1, yitp2, atol=1e-8) + + def test_vector_data(self): + # Make sure interpolating a vector field is the same as interpolating + # each component separately. + seq = Halton(2, scramble=False, seed=np.random.RandomState()) + + x = seq.random(100) + xitp = seq.random(100) + + y = np.array([_2d_test_function(x), + _2d_test_function(x[:, ::-1])]).T + + yitp1 = self.build(x, y)(xitp) + yitp2 = self.build(x, y[:, 0])(xitp) + yitp3 = self.build(x, y[:, 1])(xitp) + + xp_assert_close(yitp1[:, 0], yitp2) + xp_assert_close(yitp1[:, 1], yitp3) + + def test_complex_data(self): + # Interpolating complex input should be the same as interpolating the + # real and complex components. + seq = Halton(2, scramble=False, seed=np.random.RandomState()) + + x = seq.random(100) + xitp = seq.random(100) + + y = _2d_test_function(x) + 1j*_2d_test_function(x[:, ::-1]) + + yitp1 = self.build(x, y)(xitp) + yitp2 = self.build(x, y.real)(xitp) + yitp3 = self.build(x, y.imag)(xitp) + + xp_assert_close(yitp1.real, yitp2) + xp_assert_close(yitp1.imag, yitp3) + + @pytest.mark.parametrize('kernel', sorted(_AVAILABLE)) + def test_interpolation_misfit_1d(self, kernel): + # Make sure that each kernel, with its default `degree` and an + # appropriate `epsilon`, does a good job at interpolation in 1d. + seq = Halton(1, scramble=False, seed=np.random.RandomState()) + + x = 3*seq.random(50) + xitp = 3*seq.random(50) + + y = _1d_test_function(x) + ytrue = _1d_test_function(xitp) + yitp = self.build(x, y, epsilon=5.0, kernel=kernel)(xitp) + + mse = np.mean((yitp - ytrue)**2) + assert mse < 1.0e-4 + + @pytest.mark.parametrize('kernel', sorted(_AVAILABLE)) + def test_interpolation_misfit_2d(self, kernel): + # Make sure that each kernel, with its default `degree` and an + # appropriate `epsilon`, does a good job at interpolation in 2d. + seq = Halton(2, scramble=False, seed=np.random.RandomState()) + + x = seq.random(100) + xitp = seq.random(100) + + y = _2d_test_function(x) + ytrue = _2d_test_function(xitp) + yitp = self.build(x, y, epsilon=5.0, kernel=kernel)(xitp) + + mse = np.mean((yitp - ytrue)**2) + assert mse < 2.0e-4 + + @pytest.mark.parametrize('kernel', sorted(_AVAILABLE)) + def test_smoothing_misfit(self, kernel): + # Make sure we can find a smoothing parameter for each kernel that + # removes a sufficient amount of noise. + rng = np.random.RandomState(0) + seq = Halton(1, scramble=False, seed=rng) + + noise = 0.2 + rmse_tol = 0.1 + smoothing_range = 10**np.linspace(-4, 1, 20) + + x = 3*seq.random(100) + y = _1d_test_function(x) + rng.normal(0.0, noise, (100,)) + ytrue = _1d_test_function(x) + rmse_within_tol = False + for smoothing in smoothing_range: + ysmooth = self.build( + x, y, + epsilon=1.0, + smoothing=smoothing, + kernel=kernel)(x) + rmse = np.sqrt(np.mean((ysmooth - ytrue)**2)) + if rmse < rmse_tol: + rmse_within_tol = True + break + + assert rmse_within_tol + + def test_array_smoothing(self): + # Test using an array for `smoothing` to give less weight to a known + # outlier. + rng = np.random.RandomState(0) + seq = Halton(1, scramble=False, seed=rng) + degree = 2 + + x = seq.random(50) + P = _vandermonde(x, degree) + poly_coeffs = rng.normal(0.0, 1.0, P.shape[1]) + y = P.dot(poly_coeffs) + y_with_outlier = np.copy(y) + y_with_outlier[10] += 1.0 + smoothing = np.zeros((50,)) + smoothing[10] = 1000.0 + yitp = self.build(x, y_with_outlier, smoothing=smoothing)(x) + # Should be able to reproduce the uncorrupted data almost exactly. + xp_assert_close(yitp, y, atol=1e-4) + + def test_inconsistent_x_dimensions_error(self): + # ValueError should be raised if the observation points and evaluation + # points have a different number of dimensions. + y = Halton(2, scramble=False, seed=np.random.RandomState()).random(10) + d = _2d_test_function(y) + x = Halton(1, scramble=False, seed=np.random.RandomState()).random(10) + match = 'Expected the second axis of `x`' + with pytest.raises(ValueError, match=match): + self.build(y, d)(x) + + def test_inconsistent_d_length_error(self): + y = np.linspace(0, 1, 5)[:, None] + d = np.zeros(1) + match = 'Expected the first axis of `d`' + with pytest.raises(ValueError, match=match): + self.build(y, d) + + def test_y_not_2d_error(self): + y = np.linspace(0, 1, 5) + d = np.zeros(5) + match = '`y` must be a 2-dimensional array.' + with pytest.raises(ValueError, match=match): + self.build(y, d) + + def test_inconsistent_smoothing_length_error(self): + y = np.linspace(0, 1, 5)[:, None] + d = np.zeros(5) + smoothing = np.ones(1) + match = 'Expected `smoothing` to be' + with pytest.raises(ValueError, match=match): + self.build(y, d, smoothing=smoothing) + + def test_invalid_kernel_name_error(self): + y = np.linspace(0, 1, 5)[:, None] + d = np.zeros(5) + match = '`kernel` must be one of' + with pytest.raises(ValueError, match=match): + self.build(y, d, kernel='test') + + def test_epsilon_not_specified_error(self): + y = np.linspace(0, 1, 5)[:, None] + d = np.zeros(5) + for kernel in _AVAILABLE: + if kernel in _SCALE_INVARIANT: + continue + + match = '`epsilon` must be specified' + with pytest.raises(ValueError, match=match): + self.build(y, d, kernel=kernel) + + def test_x_not_2d_error(self): + y = np.linspace(0, 1, 5)[:, None] + x = np.linspace(0, 1, 5) + d = np.zeros(5) + match = '`x` must be a 2-dimensional array.' + with pytest.raises(ValueError, match=match): + self.build(y, d)(x) + + def test_not_enough_observations_error(self): + y = np.linspace(0, 1, 1)[:, None] + d = np.zeros(1) + match = 'At least 2 data points are required' + with pytest.raises(ValueError, match=match): + self.build(y, d, kernel='thin_plate_spline') + + @pytest.mark.thread_unsafe + def test_degree_warning(self): + y = np.linspace(0, 1, 5)[:, None] + d = np.zeros(5) + for kernel, deg in _NAME_TO_MIN_DEGREE.items(): + # Only test for kernels that its minimum degree is not 0. + if deg >= 1: + match = f'`degree` should not be below {deg}' + with pytest.warns(Warning, match=match): + self.build(y, d, epsilon=1.0, kernel=kernel, degree=deg-1) + + def test_minus_one_degree(self): + # Make sure a degree of -1 is accepted without any warning. + y = np.linspace(0, 1, 5)[:, None] + d = np.zeros(5) + for kernel, _ in _NAME_TO_MIN_DEGREE.items(): + self.build(y, d, epsilon=1.0, kernel=kernel, degree=-1) + + def test_rank_error(self): + # An error should be raised when `kernel` is "thin_plate_spline" and + # observations are 2-D and collinear. + y = np.array([[2.0, 0.0], [1.0, 0.0], [0.0, 0.0]]) + d = np.array([0.0, 0.0, 0.0]) + match = 'does not have full column rank' + with pytest.raises(LinAlgError, match=match): + self.build(y, d, kernel='thin_plate_spline')(y) + + def test_single_point(self): + # Make sure interpolation still works with only one point (in 1, 2, and + # 3 dimensions). + for dim in [1, 2, 3]: + y = np.zeros((1, dim)) + d = np.ones((1,)) + f = self.build(y, d, kernel='linear')(y) + xp_assert_close(d, f) + + def test_pickleable(self): + # Make sure we can pickle and unpickle the interpolant without any + # changes in the behavior. + seq = Halton(1, scramble=False, seed=np.random.RandomState(2305982309)) + + x = 3*seq.random(50) + xitp = 3*seq.random(50) + + y = _1d_test_function(x) + + interp = self.build(x, y) + + yitp1 = interp(xitp) + yitp2 = pickle.loads(pickle.dumps(interp))(xitp) + + xp_assert_close(yitp1, yitp2, atol=1e-16) + + +class TestRBFInterpolatorNeighborsNone(_TestRBFInterpolator): + def build(self, *args, **kwargs): + return RBFInterpolator(*args, **kwargs) + + def test_smoothing_limit_1d(self): + # For large smoothing parameters, the interpolant should approach a + # least squares fit of a polynomial with the specified degree. + seq = Halton(1, scramble=False, seed=np.random.RandomState()) + + degree = 3 + smoothing = 1e8 + + x = 3*seq.random(50) + xitp = 3*seq.random(50) + + y = _1d_test_function(x) + + yitp1 = self.build( + x, y, + degree=degree, + smoothing=smoothing + )(xitp) + + P = _vandermonde(x, degree) + Pitp = _vandermonde(xitp, degree) + yitp2 = Pitp.dot(np.linalg.lstsq(P, y, rcond=None)[0]) + + xp_assert_close(yitp1, yitp2, atol=1e-8) + + def test_smoothing_limit_2d(self): + # For large smoothing parameters, the interpolant should approach a + # least squares fit of a polynomial with the specified degree. + seq = Halton(2, scramble=False, seed=np.random.RandomState()) + + degree = 3 + smoothing = 1e8 + + x = seq.random(100) + xitp = seq.random(100) + + y = _2d_test_function(x) + + yitp1 = self.build( + x, y, + degree=degree, + smoothing=smoothing + )(xitp) + + P = _vandermonde(x, degree) + Pitp = _vandermonde(xitp, degree) + yitp2 = Pitp.dot(np.linalg.lstsq(P, y, rcond=None)[0]) + + xp_assert_close(yitp1, yitp2, atol=1e-8) + + +class TestRBFInterpolatorNeighbors20(_TestRBFInterpolator): + # RBFInterpolator using 20 nearest neighbors. + def build(self, *args, **kwargs): + return RBFInterpolator(*args, **kwargs, neighbors=20) + + def test_equivalent_to_rbf_interpolator(self): + seq = Halton(2, scramble=False, seed=np.random.RandomState()) + + x = seq.random(100) + xitp = seq.random(100) + + y = _2d_test_function(x) + + yitp1 = self.build(x, y)(xitp) + + yitp2 = [] + tree = cKDTree(x) + for xi in xitp: + _, nbr = tree.query(xi, 20) + yitp2.append(RBFInterpolator(x[nbr], y[nbr])(xi[None])[0]) + + xp_assert_close(yitp1, yitp2, atol=1e-8) + + def test_concurrency(self): + # Check that no segfaults appear with concurrent access to + # RbfInterpolator + seq = Halton(2, scramble=False, seed=np.random.RandomState(0)) + x = seq.random(100) + xitp = seq.random(100) + + y = _2d_test_function(x) + + interp = self.build(x, y) + + def worker_fn(_, interp, xp): + interp(xp) + + _run_concurrent_barrier(10, worker_fn, interp, xitp) + + +class TestRBFInterpolatorNeighborsInf(TestRBFInterpolatorNeighborsNone): + # RBFInterpolator using neighbors=np.inf. This should give exactly the same + # results as neighbors=None, but it will be slower. + def build(self, *args, **kwargs): + return RBFInterpolator(*args, **kwargs, neighbors=np.inf) + + def test_equivalent_to_rbf_interpolator(self): + seq = Halton(1, scramble=False, seed=np.random.RandomState()) + + x = 3*seq.random(50) + xitp = 3*seq.random(50) + + y = _1d_test_function(x) + yitp1 = self.build(x, y)(xitp) + yitp2 = RBFInterpolator(x, y)(xitp) + + xp_assert_close(yitp1, yitp2, atol=1e-8) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_rgi.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_rgi.py new file mode 100644 index 0000000000000000000000000000000000000000..54c4f380ad7d51f54a5947cfc7764a60c1fc235e --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/interpolate/tests/test_rgi.py @@ -0,0 +1,1150 @@ +import itertools + +import pytest +import numpy as np + +from numpy.testing import assert_warns +from scipy._lib._array_api import ( + xp_assert_equal, xp_assert_close, assert_array_almost_equal +) +from scipy.conftest import skip_xp_invalid_arg + +from pytest import raises as assert_raises + +from scipy.interpolate import (RegularGridInterpolator, interpn, + RectBivariateSpline, + NearestNDInterpolator, LinearNDInterpolator) + +from scipy.sparse._sputils import matrix +from scipy._lib._util import ComplexWarning +from scipy._lib._testutils import _run_concurrent_barrier + + +parametrize_rgi_interp_methods = pytest.mark.parametrize( + "method", RegularGridInterpolator._ALL_METHODS +) + +class TestRegularGridInterpolator: + def _get_sample_4d(self): + # create a 4-D grid of 3 points in each dimension + points = [(0., .5, 1.)] * 4 + values = np.asarray([0., .5, 1.]) + values0 = values[:, np.newaxis, np.newaxis, np.newaxis] + values1 = values[np.newaxis, :, np.newaxis, np.newaxis] + values2 = values[np.newaxis, np.newaxis, :, np.newaxis] + values3 = values[np.newaxis, np.newaxis, np.newaxis, :] + values = (values0 + values1 * 10 + values2 * 100 + values3 * 1000) + return points, values + + def _get_sample_4d_2(self): + # create another 4-D grid of 3 points in each dimension + points = [(0., .5, 1.)] * 2 + [(0., 5., 10.)] * 2 + values = np.asarray([0., .5, 1.]) + values0 = values[:, np.newaxis, np.newaxis, np.newaxis] + values1 = values[np.newaxis, :, np.newaxis, np.newaxis] + values2 = values[np.newaxis, np.newaxis, :, np.newaxis] + values3 = values[np.newaxis, np.newaxis, np.newaxis, :] + values = (values0 + values1 * 10 + values2 * 100 + values3 * 1000) + return points, values + + def _get_sample_4d_3(self): + # create another 4-D grid of 7 points in each dimension + points = [(0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0)] * 4 + values = np.asarray([0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]) + values0 = values[:, np.newaxis, np.newaxis, np.newaxis] + values1 = values[np.newaxis, :, np.newaxis, np.newaxis] + values2 = values[np.newaxis, np.newaxis, :, np.newaxis] + values3 = values[np.newaxis, np.newaxis, np.newaxis, :] + values = (values0 + values1 * 10 + values2 * 100 + values3 * 1000) + return points, values + + def _get_sample_4d_4(self): + # create another 4-D grid of 2 points in each dimension + points = [(0.0, 1.0)] * 4 + values = np.asarray([0.0, 1.0]) + values0 = values[:, np.newaxis, np.newaxis, np.newaxis] + values1 = values[np.newaxis, :, np.newaxis, np.newaxis] + values2 = values[np.newaxis, np.newaxis, :, np.newaxis] + values3 = values[np.newaxis, np.newaxis, np.newaxis, :] + values = (values0 + values1 * 10 + values2 * 100 + values3 * 1000) + return points, values + + @parametrize_rgi_interp_methods + def test_list_input(self, method): + points, values = self._get_sample_4d_3() + + sample = np.asarray([[0.1, 0.1, 1., .9], [0.2, 0.1, .45, .8], + [0.5, 0.5, .5, .5]]) + + interp = RegularGridInterpolator(points, + values.tolist(), + method=method) + v1 = interp(sample.tolist()) + interp = RegularGridInterpolator(points, + values, + method=method) + v2 = interp(sample) + xp_assert_close(v1, v2) + + @pytest.mark.parametrize('method', ['cubic', 'quintic', 'pchip']) + def test_spline_dim_error(self, method): + points, values = self._get_sample_4d_4() + match = "points in dimension" + + # Check error raise when creating interpolator + with pytest.raises(ValueError, match=match): + RegularGridInterpolator(points, values, method=method) + + # Check error raise when creating interpolator + interp = RegularGridInterpolator(points, values) + sample = np.asarray([[0.1, 0.1, 1., .9], [0.2, 0.1, .45, .8], + [0.5, 0.5, .5, .5]]) + with pytest.raises(ValueError, match=match): + interp(sample, method=method) + + @pytest.mark.parametrize( + "points_values, sample", + [ + ( + _get_sample_4d, + np.asarray( + [[0.1, 0.1, 1.0, 0.9], + [0.2, 0.1, 0.45, 0.8], + [0.5, 0.5, 0.5, 0.5]] + ), + ), + (_get_sample_4d_2, np.asarray([0.1, 0.1, 10.0, 9.0])), + ], + ) + def test_linear_and_slinear_close(self, points_values, sample): + points, values = points_values(self) + interp = RegularGridInterpolator(points, values, method="linear") + v1 = interp(sample) + interp = RegularGridInterpolator(points, values, method="slinear") + v2 = interp(sample) + xp_assert_close(v1, v2) + + def test_derivatives(self): + points, values = self._get_sample_4d() + sample = np.array([[0.1 , 0.1 , 1. , 0.9 ], + [0.2 , 0.1 , 0.45, 0.8 ], + [0.5 , 0.5 , 0.5 , 0.5 ]]) + interp = RegularGridInterpolator(points, values, method="slinear") + + with assert_raises(ValueError): + # wrong number of derivatives (need 4) + interp(sample, nu=1) + + xp_assert_close(interp(sample, nu=(1, 0, 0, 0)), + np.asarray([1.0, 1, 1]), atol=1e-15) + xp_assert_close(interp(sample, nu=(0, 1, 0, 0)), + np.asarray([10.0, 10, 10]), atol=1e-15) + + # 2nd derivatives of a linear function are zero + xp_assert_close(interp(sample, nu=(0, 1, 1, 0)), + np.asarray([0.0, 0, 0]), atol=2e-12) + + @parametrize_rgi_interp_methods + def test_complex(self, method): + if method == "pchip": + pytest.skip("pchip does not make sense for complex data") + points, values = self._get_sample_4d_3() + values = values - 2j*values + sample = np.asarray([[0.1, 0.1, 1., .9], [0.2, 0.1, .45, .8], + [0.5, 0.5, .5, .5]]) + + interp = RegularGridInterpolator(points, values, method=method) + rinterp = RegularGridInterpolator(points, values.real, method=method) + iinterp = RegularGridInterpolator(points, values.imag, method=method) + + v1 = interp(sample) + v2 = rinterp(sample) + 1j*iinterp(sample) + xp_assert_close(v1, v2) + + def test_cubic_vs_pchip(self): + x, y = [1, 2, 3, 4], [1, 2, 3, 4] + xg, yg = np.meshgrid(x, y, indexing='ij') + + values = (lambda x, y: x**4 * y**4)(xg, yg) + cubic = RegularGridInterpolator((x, y), values, method='cubic') + pchip = RegularGridInterpolator((x, y), values, method='pchip') + + vals_cubic = cubic([1.5, 2]) + vals_pchip = pchip([1.5, 2]) + assert not np.allclose(vals_cubic, vals_pchip, atol=1e-14, rtol=0) + + def test_linear_xi1d(self): + points, values = self._get_sample_4d_2() + interp = RegularGridInterpolator(points, values) + sample = np.asarray([0.1, 0.1, 10., 9.]) + wanted = np.asarray([1001.1]) + assert_array_almost_equal(interp(sample), wanted) + + def test_linear_xi3d(self): + points, values = self._get_sample_4d() + interp = RegularGridInterpolator(points, values) + sample = np.asarray([[0.1, 0.1, 1., .9], [0.2, 0.1, .45, .8], + [0.5, 0.5, .5, .5]]) + wanted = np.asarray([1001.1, 846.2, 555.5]) + assert_array_almost_equal(interp(sample), wanted) + + @pytest.mark.parametrize( + "sample, wanted", + [ + (np.asarray([0.1, 0.1, 0.9, 0.9]), 1100.0), + (np.asarray([0.1, 0.1, 0.1, 0.1]), 0.0), + (np.asarray([0.0, 0.0, 0.0, 0.0]), 0.0), + (np.asarray([1.0, 1.0, 1.0, 1.0]), 1111.0), + (np.asarray([0.1, 0.4, 0.6, 0.9]), 1055.0), + ], + ) + def test_nearest(self, sample, wanted): + points, values = self._get_sample_4d() + interp = RegularGridInterpolator(points, values, method="nearest") + wanted = np.asarray([wanted]) + assert_array_almost_equal(interp(sample), wanted) + + def test_linear_edges(self): + points, values = self._get_sample_4d() + interp = RegularGridInterpolator(points, values) + sample = np.asarray([[0., 0., 0., 0.], [1., 1., 1., 1.]]) + wanted = np.asarray([0., 1111.]) + assert_array_almost_equal(interp(sample), wanted) + + def test_valid_create(self): + # create a 2-D grid of 3 points in each dimension + points = [(0., .5, 1.), (0., 1., .5)] + values = np.asarray([0., .5, 1.]) + values0 = values[:, np.newaxis] + values1 = values[np.newaxis, :] + values = (values0 + values1 * 10) + assert_raises(ValueError, RegularGridInterpolator, points, values) + points = [((0., .5, 1.), ), (0., .5, 1.)] + assert_raises(ValueError, RegularGridInterpolator, points, values) + points = [(0., .5, .75, 1.), (0., .5, 1.)] + assert_raises(ValueError, RegularGridInterpolator, points, values) + points = [(0., .5, 1.), (0., .5, 1.), (0., .5, 1.)] + assert_raises(ValueError, RegularGridInterpolator, points, values) + points = [(0., .5, 1.), (0., .5, 1.)] + assert_raises(ValueError, RegularGridInterpolator, points, values, + method="undefmethod") + + def test_valid_call(self): + points, values = self._get_sample_4d() + interp = RegularGridInterpolator(points, values) + sample = np.asarray([[0., 0., 0., 0.], [1., 1., 1., 1.]]) + assert_raises(ValueError, interp, sample, "undefmethod") + sample = np.asarray([[0., 0., 0.], [1., 1., 1.]]) + assert_raises(ValueError, interp, sample) + sample = np.asarray([[0., 0., 0., 0.], [1., 1., 1., 1.1]]) + assert_raises(ValueError, interp, sample) + + def test_out_of_bounds_extrap(self): + points, values = self._get_sample_4d() + interp = RegularGridInterpolator(points, values, bounds_error=False, + fill_value=None) + sample = np.asarray([[-.1, -.1, -.1, -.1], [1.1, 1.1, 1.1, 1.1], + [21, 2.1, -1.1, -11], [2.1, 2.1, -1.1, -1.1]]) + wanted = np.asarray([0., 1111., 11., 11.]) + assert_array_almost_equal(interp(sample, method="nearest"), wanted) + wanted = np.asarray([-111.1, 1222.1, -11068., -1186.9]) + assert_array_almost_equal(interp(sample, method="linear"), wanted) + + def test_out_of_bounds_extrap2(self): + points, values = self._get_sample_4d_2() + interp = RegularGridInterpolator(points, values, bounds_error=False, + fill_value=None) + sample = np.asarray([[-.1, -.1, -.1, -.1], [1.1, 1.1, 1.1, 1.1], + [21, 2.1, -1.1, -11], [2.1, 2.1, -1.1, -1.1]]) + wanted = np.asarray([0., 11., 11., 11.]) + assert_array_almost_equal(interp(sample, method="nearest"), wanted) + wanted = np.asarray([-12.1, 133.1, -1069., -97.9]) + assert_array_almost_equal(interp(sample, method="linear"), wanted) + + def test_out_of_bounds_fill(self): + points, values = self._get_sample_4d() + interp = RegularGridInterpolator(points, values, bounds_error=False, + fill_value=np.nan) + sample = np.asarray([[-.1, -.1, -.1, -.1], [1.1, 1.1, 1.1, 1.1], + [2.1, 2.1, -1.1, -1.1]]) + wanted = np.asarray([np.nan, np.nan, np.nan]) + assert_array_almost_equal(interp(sample, method="nearest"), wanted) + assert_array_almost_equal(interp(sample, method="linear"), wanted) + sample = np.asarray([[0.1, 0.1, 1., .9], [0.2, 0.1, .45, .8], + [0.5, 0.5, .5, .5]]) + wanted = np.asarray([1001.1, 846.2, 555.5]) + assert_array_almost_equal(interp(sample), wanted) + + def test_nearest_compare_qhull(self): + points, values = self._get_sample_4d() + interp = RegularGridInterpolator(points, values, method="nearest") + points_qhull = itertools.product(*points) + points_qhull = [p for p in points_qhull] + points_qhull = np.asarray(points_qhull) + values_qhull = values.reshape(-1) + interp_qhull = NearestNDInterpolator(points_qhull, values_qhull) + sample = np.asarray([[0.1, 0.1, 1., .9], [0.2, 0.1, .45, .8], + [0.5, 0.5, .5, .5]]) + assert_array_almost_equal(interp(sample), interp_qhull(sample)) + + def test_linear_compare_qhull(self): + points, values = self._get_sample_4d() + interp = RegularGridInterpolator(points, values) + points_qhull = itertools.product(*points) + points_qhull = [p for p in points_qhull] + points_qhull = np.asarray(points_qhull) + values_qhull = values.reshape(-1) + interp_qhull = LinearNDInterpolator(points_qhull, values_qhull) + sample = np.asarray([[0.1, 0.1, 1., .9], [0.2, 0.1, .45, .8], + [0.5, 0.5, .5, .5]]) + assert_array_almost_equal(interp(sample), interp_qhull(sample)) + + @pytest.mark.parametrize("method", ["nearest", "linear"]) + def test_duck_typed_values(self, method): + x = np.linspace(0, 2, 5) + y = np.linspace(0, 1, 7) + + values = MyValue((5, 7)) + + interp = RegularGridInterpolator((x, y), values, method=method) + v1 = interp([0.4, 0.7]) + + interp = RegularGridInterpolator((x, y), values._v, method=method) + v2 = interp([0.4, 0.7]) + xp_assert_close(v1, v2, check_dtype=False) + + def test_invalid_fill_value(self): + np.random.seed(1234) + x = np.linspace(0, 2, 5) + y = np.linspace(0, 1, 7) + values = np.random.rand(5, 7) + + # integers can be cast to floats + RegularGridInterpolator((x, y), values, fill_value=1) + + # complex values cannot + assert_raises(ValueError, RegularGridInterpolator, + (x, y), values, fill_value=1+2j) + + def test_fillvalue_type(self): + # from #3703; test that interpolator object construction succeeds + values = np.ones((10, 20, 30), dtype='>f4') + points = [np.arange(n) for n in values.shape] + # xi = [(1, 1, 1)] + RegularGridInterpolator(points, values) + RegularGridInterpolator(points, values, fill_value=0.) + + def test_length_one_axis(self): + # gh-5890, gh-9524 : length-1 axis is legal for method='linear'. + # Along the axis it's linear interpolation; away from the length-1 + # axis, it's an extrapolation, so fill_value should be used. + def f(x, y): + return x + y + x = np.linspace(1, 1, 1) + y = np.linspace(1, 10, 10) + data = f(*np.meshgrid(x, y, indexing="ij", sparse=True)) + + interp = RegularGridInterpolator((x, y), data, method="linear", + bounds_error=False, fill_value=101) + + # check values at the grid + xp_assert_close(interp(np.array([[1, 1], [1, 5], [1, 10]])), + np.asarray([2.0, 6, 11]), + atol=1e-14) + + # check off-grid interpolation is indeed linear + xp_assert_close(interp(np.array([[1, 1.4], [1, 5.3], [1, 10]])), + [2.4, 6.3, 11], + atol=1e-14) + + # check exrapolation w/ fill_value + xp_assert_close(interp(np.array([1.1, 2.4])), + interp.fill_value, + check_dtype=False, check_shape=False, check_0d=False, + atol=1e-14) + + # check extrapolation: linear along the `y` axis, const along `x` + interp.fill_value = None + xp_assert_close(interp([[1, 0.3], [1, 11.5]]), + [1.3, 12.5], atol=1e-15) + + xp_assert_close(interp([[1.5, 0.3], [1.9, 11.5]]), + [1.3, 12.5], atol=1e-15) + + # extrapolation with method='nearest' + interp = RegularGridInterpolator((x, y), data, method="nearest", + bounds_error=False, fill_value=None) + xp_assert_close(interp([[1.5, 1.8], [-4, 5.1]]), + np.asarray([3.0, 6]), + atol=1e-15) + + @pytest.mark.parametrize("fill_value", [None, np.nan, np.pi]) + @pytest.mark.parametrize("method", ['linear', 'nearest']) + def test_length_one_axis2(self, fill_value, method): + options = {"fill_value": fill_value, "bounds_error": False, + "method": method} + + x = np.linspace(0, 2*np.pi, 20) + z = np.sin(x) + + fa = RegularGridInterpolator((x,), z[:], **options) + fb = RegularGridInterpolator((x, [0]), z[:, None], **options) + + x1a = np.linspace(-1, 2*np.pi+1, 100) + za = fa(x1a) + + # evaluated at provided y-value, fb should behave exactly as fa + y1b = np.zeros(100) + zb = fb(np.vstack([x1a, y1b]).T) + xp_assert_close(zb, za) + + # evaluated at a different y-value, fb should return fill value + y1b = np.ones(100) + zb = fb(np.vstack([x1a, y1b]).T) + if fill_value is None: + xp_assert_close(zb, za) + else: + xp_assert_close(zb, np.full_like(zb, fill_value)) + + @pytest.mark.parametrize("method", ['nearest', 'linear']) + def test_nan_x_1d(self, method): + # gh-6624 : if x is nan, result should be nan + f = RegularGridInterpolator(([1, 2, 3],), [10, 20, 30], fill_value=1, + bounds_error=False, method=method) + assert np.isnan(f([np.nan])) + + # test arbitrary nan pattern + rng = np.random.default_rng(8143215468) + x = rng.random(size=100)*4 + i = rng.random(size=100) > 0.5 + x[i] = np.nan + with np.errstate(invalid='ignore'): + # out-of-bounds comparisons, `out_of_bounds += x < grid[0]`, + # generate numpy warnings if `x` contains nans. + # These warnings should propagate to user (since `x` is user + # input) and we simply filter them out. + res = f(x) + + assert np.isnan(res[i]).all() + xp_assert_equal(res[~i], f(x[~i])) + + # also test the length-one axis f(nan) + x = [1, 2, 3] + y = [1, ] + data = np.ones((3, 1)) + f = RegularGridInterpolator((x, y), data, fill_value=1, + bounds_error=False, method=method) + assert np.all(np.isnan(f([np.nan, 1]))) + assert np.all(np.isnan(f([1, np.nan]))) + + @pytest.mark.parametrize("method", ['nearest', 'linear']) + def test_nan_x_2d(self, method): + x, y = np.array([0, 1, 2]), np.array([1, 3, 7]) + + def f(x, y): + return x**2 + y**2 + + xg, yg = np.meshgrid(x, y, indexing='ij', sparse=True) + data = f(xg, yg) + interp = RegularGridInterpolator((x, y), data, + method=method, bounds_error=False) + + with np.errstate(invalid='ignore'): + res = interp([[1.5, np.nan], [1, 1]]) + xp_assert_close(res[1], 2.0, atol=1e-14) + assert np.isnan(res[0]) + + # test arbitrary nan pattern + rng = np.random.default_rng(8143215468) + x = rng.random(size=100)*4-1 + y = rng.random(size=100)*8 + i1 = rng.random(size=100) > 0.5 + i2 = rng.random(size=100) > 0.5 + i = i1 | i2 + x[i1] = np.nan + y[i2] = np.nan + z = np.array([x, y]).T + with np.errstate(invalid='ignore'): + # out-of-bounds comparisons, `out_of_bounds += x < grid[0]`, + # generate numpy warnings if `x` contains nans. + # These warnings should propagate to user (since `x` is user + # input) and we simply filter them out. + res = interp(z) + + assert np.isnan(res[i]).all() + xp_assert_equal(res[~i], interp(z[~i]), check_dtype=False) + + @pytest.mark.fail_slow(10) + @parametrize_rgi_interp_methods + @pytest.mark.parametrize(("ndims", "func"), [ + (2, lambda x, y: 2 * x ** 3 + 3 * y ** 2), + (3, lambda x, y, z: 2 * x ** 3 + 3 * y ** 2 - z), + (4, lambda x, y, z, a: 2 * x ** 3 + 3 * y ** 2 - z + a), + (5, lambda x, y, z, a, b: 2 * x ** 3 + 3 * y ** 2 - z + a * b), + ]) + def test_descending_points_nd(self, method, ndims, func): + + if ndims >= 4 and method in {"cubic", "quintic"}: + pytest.skip("too slow; OOM (quintic); or nearly so (cubic)") + + rng = np.random.default_rng(42) + sample_low = 1 + sample_high = 5 + test_points = rng.uniform(sample_low, sample_high, size=(2, ndims)) + + ascending_points = [np.linspace(sample_low, sample_high, 12) + for _ in range(ndims)] + + ascending_values = func(*np.meshgrid(*ascending_points, + indexing="ij", + sparse=True)) + + ascending_interp = RegularGridInterpolator(ascending_points, + ascending_values, + method=method) + ascending_result = ascending_interp(test_points) + + descending_points = [xi[::-1] for xi in ascending_points] + descending_values = func(*np.meshgrid(*descending_points, + indexing="ij", + sparse=True)) + descending_interp = RegularGridInterpolator(descending_points, + descending_values, + method=method) + descending_result = descending_interp(test_points) + + xp_assert_equal(ascending_result, descending_result) + + def test_invalid_points_order(self): + def val_func_2d(x, y): + return 2 * x ** 3 + 3 * y ** 2 + + x = np.array([.5, 2., 0., 4., 5.5]) # not ascending or descending + y = np.array([.5, 2., 3., 4., 5.5]) + points = (x, y) + values = val_func_2d(*np.meshgrid(*points, indexing='ij', + sparse=True)) + match = "must be strictly ascending or descending" + with pytest.raises(ValueError, match=match): + RegularGridInterpolator(points, values) + + @parametrize_rgi_interp_methods + def test_fill_value(self, method): + interp = RegularGridInterpolator([np.arange(6)], np.ones(6), + method=method, bounds_error=False) + assert np.isnan(interp([10])) + + @pytest.mark.fail_slow(5) + @parametrize_rgi_interp_methods + def test_nonscalar_values(self, method): + + if method == "quintic": + pytest.skip("Way too slow.") + + # Verify that non-scalar valued values also works + points = [(0.0, 0.5, 1.0, 1.5, 2.0, 2.5)] * 2 + [ + (0.0, 5.0, 10.0, 15.0, 20, 25.0) + ] * 2 + + rng = np.random.default_rng(1234) + values = rng.random((6, 6, 6, 6, 8)) + sample = rng.random((7, 3, 4)) + + interp = RegularGridInterpolator(points, values, method=method, + bounds_error=False) + v = interp(sample) + assert v.shape == (7, 3, 8), method + + vs = [] + for j in range(8): + interp = RegularGridInterpolator(points, values[..., j], + method=method, + bounds_error=False) + vs.append(interp(sample)) + v2 = np.array(vs).transpose(1, 2, 0) + + xp_assert_close(v, v2, atol=1e-14, err_msg=method) + + @parametrize_rgi_interp_methods + @pytest.mark.parametrize("flip_points", [False, True]) + def test_nonscalar_values_2(self, method, flip_points): + + if method in {"cubic", "quintic"}: + pytest.skip("Way too slow.") + + # Verify that non-scalar valued values also work : use different + # lengths of axes to simplify tracing the internals + points = [(0.0, 0.5, 1.0, 1.5, 2.0, 2.5), + (0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0), + (0.0, 5.0, 10.0, 15.0, 20, 25.0, 35.0, 36.0), + (0.0, 5.0, 10.0, 15.0, 20, 25.0, 35.0, 36.0, 47)] + + # verify, that strictly decreasing dimensions work + if flip_points: + points = [tuple(reversed(p)) for p in points] + + rng = np.random.default_rng(1234) + + trailing_points = (3, 2) + # NB: values has a `num_trailing_dims` trailing dimension + values = rng.random((6, 7, 8, 9, *trailing_points)) + sample = rng.random(4) # a single sample point ! + + interp = RegularGridInterpolator(points, values, method=method, + bounds_error=False) + v = interp(sample) + + # v has a single sample point *per entry in the trailing dimensions* + assert v.shape == (1, *trailing_points) + + # check the values, too : manually loop over the trailing dimensions + vs = np.empty(values.shape[-2:]) + for i in range(values.shape[-2]): + for j in range(values.shape[-1]): + interp = RegularGridInterpolator(points, values[..., i, j], + method=method, + bounds_error=False) + vs[i, j] = interp(sample).item() + v2 = np.expand_dims(vs, axis=0) + xp_assert_close(v, v2, atol=1e-14, err_msg=method) + + def test_nonscalar_values_linear_2D(self): + # Verify that non-scalar values work in the 2D fast path + method = 'linear' + points = [(0.0, 0.5, 1.0, 1.5, 2.0, 2.5), + (0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0), ] + + rng = np.random.default_rng(1234) + + trailing_points = (3, 4) + # NB: values has a `num_trailing_dims` trailing dimension + values = rng.random((6, 7, *trailing_points)) + sample = rng.random(2) # a single sample point ! + + interp = RegularGridInterpolator(points, values, method=method, + bounds_error=False) + v = interp(sample) + + # v has a single sample point *per entry in the trailing dimensions* + assert v.shape == (1, *trailing_points) + + # check the values, too : manually loop over the trailing dimensions + vs = np.empty(values.shape[-2:]) + for i in range(values.shape[-2]): + for j in range(values.shape[-1]): + interp = RegularGridInterpolator(points, values[..., i, j], + method=method, + bounds_error=False) + vs[i, j] = interp(sample).item() + v2 = np.expand_dims(vs, axis=0) + xp_assert_close(v, v2, atol=1e-14, err_msg=method) + + @pytest.mark.parametrize( + "dtype", + [np.float32, np.float64, np.complex64, np.complex128] + ) + @pytest.mark.parametrize("xi_dtype", [np.float32, np.float64]) + def test_float32_values(self, dtype, xi_dtype): + # regression test for gh-17718: values.dtype=float32 fails + def f(x, y): + return 2 * x**3 + 3 * y**2 + + x = np.linspace(1, 4, 11) + y = np.linspace(4, 7, 22) + + xg, yg = np.meshgrid(x, y, indexing='ij', sparse=True) + data = f(xg, yg) + + data = data.astype(dtype) + + interp = RegularGridInterpolator((x, y), data) + + pts = np.array([[2.1, 6.2], + [3.3, 5.2]], dtype=xi_dtype) + + # the values here are just what the call returns; the test checks that + # that the call succeeds at all, instead of failing with cython not + # having a float32 kernel + xp_assert_close(interp(pts), [134.10469388, 153.40069388], + atol=1e-7, rtol=1e-7, check_dtype=False) + + def test_bad_solver(self): + x = np.linspace(0, 3, 7) + y = np.linspace(0, 3, 7) + xg, yg = np.meshgrid(x, y, indexing='ij', sparse=True) + data = xg + yg + + # default method 'linear' does not accept 'solver' + with assert_raises(ValueError): + RegularGridInterpolator((x, y), data, solver=lambda x: x) + + with assert_raises(TypeError): + # wrong solver interface + RegularGridInterpolator( + (x, y), data, method='slinear', solver=lambda x: x + ) + + with assert_raises(TypeError): + # unknown argument + RegularGridInterpolator( + (x, y), data, method='slinear', solver=lambda x: x, woof='woof' + ) + + with assert_raises(TypeError): + # unknown argument + RegularGridInterpolator( + (x, y), data, method='slinear', solver_args={'woof': 42} + ) + + @pytest.mark.thread_unsafe + def test_concurrency(self): + points, values = self._get_sample_4d() + sample = np.array([[0.1 , 0.1 , 1. , 0.9 ], + [0.2 , 0.1 , 0.45, 0.8 ], + [0.5 , 0.5 , 0.5 , 0.5 ], + [0.3 , 0.1 , 0.2 , 0.4 ]]) + interp = RegularGridInterpolator(points, values, method="slinear") + + # A call to RGI with a method different from the one specified on the + # constructor, should not mutate it. + methods = ['slinear', 'nearest'] + def worker_fn(tid, interp): + spline = interp._spline + method = methods[tid % 2] + interp(sample, method=method) + assert interp._spline is spline + + _run_concurrent_barrier(10, worker_fn, interp) + + +class MyValue: + """ + Minimal indexable object + """ + + def __init__(self, shape): + self.ndim = 2 + self.shape = shape + self._v = np.arange(np.prod(shape)).reshape(shape) + + def __getitem__(self, idx): + return self._v[idx] + + def __array_interface__(self): + return None + + def __array__(self, dtype=None, copy=None): + raise RuntimeError("No array representation") + + +class TestInterpN: + def _sample_2d_data(self): + x = np.array([.5, 2., 3., 4., 5.5, 6.]) + y = np.array([.5, 2., 3., 4., 5.5, 6.]) + z = np.array( + [ + [1, 2, 1, 2, 1, 1], + [1, 2, 1, 2, 1, 1], + [1, 2, 3, 2, 1, 1], + [1, 2, 2, 2, 1, 1], + [1, 2, 1, 2, 1, 1], + [1, 2, 2, 2, 1, 1], + ] + ) + return x, y, z + + def test_spline_2d(self): + x, y, z = self._sample_2d_data() + lut = RectBivariateSpline(x, y, z) + + xi = np.array([[1, 2.3, 5.3, 0.5, 3.3, 1.2, 3], + [1, 3.3, 1.2, 4.0, 5.0, 1.0, 3]]).T + assert_array_almost_equal(interpn((x, y), z, xi, method="splinef2d"), + lut.ev(xi[:, 0], xi[:, 1])) + + @parametrize_rgi_interp_methods + def test_list_input(self, method): + x, y, z = self._sample_2d_data() + xi = np.array([[1, 2.3, 5.3, 0.5, 3.3, 1.2, 3], + [1, 3.3, 1.2, 4.0, 5.0, 1.0, 3]]).T + v1 = interpn((x, y), z, xi, method=method) + v2 = interpn( + (x.tolist(), y.tolist()), z.tolist(), xi.tolist(), method=method + ) + xp_assert_close(v1, v2, err_msg=method) + + def test_spline_2d_outofbounds(self): + x = np.array([.5, 2., 3., 4., 5.5]) + y = np.array([.5, 2., 3., 4., 5.5]) + z = np.array([[1, 2, 1, 2, 1], [1, 2, 1, 2, 1], [1, 2, 3, 2, 1], + [1, 2, 2, 2, 1], [1, 2, 1, 2, 1]]) + lut = RectBivariateSpline(x, y, z) + + xi = np.array([[1, 2.3, 6.3, 0.5, 3.3, 1.2, 3], + [1, 3.3, 1.2, -4.0, 5.0, 1.0, 3]]).T + actual = interpn((x, y), z, xi, method="splinef2d", + bounds_error=False, fill_value=999.99) + expected = lut.ev(xi[:, 0], xi[:, 1]) + expected[2:4] = 999.99 + assert_array_almost_equal(actual, expected) + + # no extrapolation for splinef2d + assert_raises(ValueError, interpn, (x, y), z, xi, method="splinef2d", + bounds_error=False, fill_value=None) + + def _sample_4d_data(self): + points = [(0., .5, 1.)] * 2 + [(0., 5., 10.)] * 2 + values = np.asarray([0., .5, 1.]) + values0 = values[:, np.newaxis, np.newaxis, np.newaxis] + values1 = values[np.newaxis, :, np.newaxis, np.newaxis] + values2 = values[np.newaxis, np.newaxis, :, np.newaxis] + values3 = values[np.newaxis, np.newaxis, np.newaxis, :] + values = (values0 + values1 * 10 + values2 * 100 + values3 * 1000) + return points, values + + def test_linear_4d(self): + # create a 4-D grid of 3 points in each dimension + points, values = self._sample_4d_data() + interp_rg = RegularGridInterpolator(points, values) + sample = np.asarray([[0.1, 0.1, 10., 9.]]) + wanted = interpn(points, values, sample, method="linear") + assert_array_almost_equal(interp_rg(sample), wanted) + + def test_4d_linear_outofbounds(self): + # create a 4-D grid of 3 points in each dimension + points, values = self._sample_4d_data() + sample = np.asarray([[0.1, -0.1, 10.1, 9.]]) + wanted = np.asarray([999.99]) + actual = interpn(points, values, sample, method="linear", + bounds_error=False, fill_value=999.99) + assert_array_almost_equal(actual, wanted) + + def test_nearest_4d(self): + # create a 4-D grid of 3 points in each dimension + points, values = self._sample_4d_data() + interp_rg = RegularGridInterpolator(points, values, method="nearest") + sample = np.asarray([[0.1, 0.1, 10., 9.]]) + wanted = interpn(points, values, sample, method="nearest") + assert_array_almost_equal(interp_rg(sample), wanted) + + def test_4d_nearest_outofbounds(self): + # create a 4-D grid of 3 points in each dimension + points, values = self._sample_4d_data() + sample = np.asarray([[0.1, -0.1, 10.1, 9.]]) + wanted = np.asarray([999.99]) + actual = interpn(points, values, sample, method="nearest", + bounds_error=False, fill_value=999.99) + assert_array_almost_equal(actual, wanted) + + def test_xi_1d(self): + # verify that 1-D xi works as expected + points, values = self._sample_4d_data() + sample = np.asarray([0.1, 0.1, 10., 9.]) + v1 = interpn(points, values, sample, bounds_error=False) + v2 = interpn(points, values, sample[None,:], bounds_error=False) + xp_assert_close(v1, v2) + + def test_xi_nd(self): + # verify that higher-d xi works as expected + points, values = self._sample_4d_data() + + np.random.seed(1234) + sample = np.random.rand(2, 3, 4) + + v1 = interpn(points, values, sample, method='nearest', + bounds_error=False) + assert v1.shape == (2, 3) + + v2 = interpn(points, values, sample.reshape(-1, 4), + method='nearest', bounds_error=False) + xp_assert_close(v1, v2.reshape(v1.shape)) + + @parametrize_rgi_interp_methods + def test_xi_broadcast(self, method): + # verify that the interpolators broadcast xi + x, y, values = self._sample_2d_data() + points = (x, y) + + xi = np.linspace(0, 1, 2) + yi = np.linspace(0, 3, 3) + + sample = (xi[:, None], yi[None, :]) + v1 = interpn(points, values, sample, method=method, bounds_error=False) + assert v1.shape == (2, 3) + + xx, yy = np.meshgrid(xi, yi) + sample = np.c_[xx.T.ravel(), yy.T.ravel()] + + v2 = interpn(points, values, sample, + method=method, bounds_error=False) + xp_assert_close(v1, v2.reshape(v1.shape)) + + @pytest.mark.fail_slow(5) + @parametrize_rgi_interp_methods + def test_nonscalar_values(self, method): + + if method == "quintic": + pytest.skip("Way too slow.") + + # Verify that non-scalar valued values also works + points = [(0.0, 0.5, 1.0, 1.5, 2.0, 2.5)] * 2 + [ + (0.0, 5.0, 10.0, 15.0, 20, 25.0) + ] * 2 + + rng = np.random.default_rng(1234) + values = rng.random((6, 6, 6, 6, 8)) + sample = rng.random((7, 3, 4)) + + v = interpn(points, values, sample, method=method, + bounds_error=False) + assert v.shape == (7, 3, 8), method + + vs = [interpn(points, values[..., j], sample, method=method, + bounds_error=False) for j in range(8)] + v2 = np.array(vs).transpose(1, 2, 0) + + xp_assert_close(v, v2, atol=1e-14, err_msg=method) + + @parametrize_rgi_interp_methods + def test_nonscalar_values_2(self, method): + + if method in {"cubic", "quintic"}: + pytest.skip("Way too slow.") + + # Verify that non-scalar valued values also work : use different + # lengths of axes to simplify tracing the internals + points = [(0.0, 0.5, 1.0, 1.5, 2.0, 2.5), + (0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0), + (0.0, 5.0, 10.0, 15.0, 20, 25.0, 35.0, 36.0), + (0.0, 5.0, 10.0, 15.0, 20, 25.0, 35.0, 36.0, 47)] + + rng = np.random.default_rng(1234) + + trailing_points = (3, 2) + # NB: values has a `num_trailing_dims` trailing dimension + values = rng.random((6, 7, 8, 9, *trailing_points)) + sample = rng.random(4) # a single sample point ! + + v = interpn(points, values, sample, method=method, bounds_error=False) + + # v has a single sample point *per entry in the trailing dimensions* + assert v.shape == (1, *trailing_points) + + # check the values, too : manually loop over the trailing dimensions + vs = [[ + interpn(points, values[..., i, j], sample, method=method, + bounds_error=False) for i in range(values.shape[-2]) + ] for j in range(values.shape[-1])] + + xp_assert_close(v, np.asarray(vs).T, atol=1e-14, err_msg=method) + + def test_non_scalar_values_splinef2d(self): + # Vector-valued splines supported with fitpack + points, values = self._sample_4d_data() + + np.random.seed(1234) + values = np.random.rand(3, 3, 3, 3, 6) + sample = np.random.rand(7, 11, 4) + assert_raises(ValueError, interpn, points, values, sample, + method='splinef2d') + + @parametrize_rgi_interp_methods + def test_complex(self, method): + if method == "pchip": + pytest.skip("pchip does not make sense for complex data") + + x, y, values = self._sample_2d_data() + points = (x, y) + values = values - 2j*values + + sample = np.array([[1, 2.3, 5.3, 0.5, 3.3, 1.2, 3], + [1, 3.3, 1.2, 4.0, 5.0, 1.0, 3]]).T + + v1 = interpn(points, values, sample, method=method) + v2r = interpn(points, values.real, sample, method=method) + v2i = interpn(points, values.imag, sample, method=method) + v2 = v2r + 1j*v2i + + xp_assert_close(v1, v2) + + @pytest.mark.thread_unsafe + def test_complex_pchip(self): + # Complex-valued data deprecated for pchip + x, y, values = self._sample_2d_data() + points = (x, y) + values = values - 2j*values + + sample = np.array([[1, 2.3, 5.3, 0.5, 3.3, 1.2, 3], + [1, 3.3, 1.2, 4.0, 5.0, 1.0, 3]]).T + with pytest.raises(ValueError, match='real'): + interpn(points, values, sample, method='pchip') + + @pytest.mark.thread_unsafe + def test_complex_spline2fd(self): + # Complex-valued data not supported by spline2fd + x, y, values = self._sample_2d_data() + points = (x, y) + values = values - 2j*values + + sample = np.array([[1, 2.3, 5.3, 0.5, 3.3, 1.2, 3], + [1, 3.3, 1.2, 4.0, 5.0, 1.0, 3]]).T + with assert_warns(ComplexWarning): + interpn(points, values, sample, method='splinef2d') + + @pytest.mark.parametrize( + "method", + ["linear", "nearest"] + ) + def test_duck_typed_values(self, method): + x = np.linspace(0, 2, 5) + y = np.linspace(0, 1, 7) + + values = MyValue((5, 7)) + + v1 = interpn((x, y), values, [0.4, 0.7], method=method) + v2 = interpn((x, y), values._v, [0.4, 0.7], method=method) + xp_assert_close(v1, v2, check_dtype=False) + + @skip_xp_invalid_arg + @parametrize_rgi_interp_methods + def test_matrix_input(self, method): + """np.matrix inputs are allowed for backwards compatibility""" + x = np.linspace(0, 2, 6) + y = np.linspace(0, 1, 7) + + values = matrix(np.random.rand(6, 7)) + + sample = np.random.rand(3, 7, 2) + + v1 = interpn((x, y), values, sample, method=method) + v2 = interpn((x, y), np.asarray(values), sample, method=method) + if method == "quintic": + # https://github.com/scipy/scipy/issues/20472 + xp_assert_close(v1, v2, atol=5e-5, rtol=2e-6) + else: + xp_assert_close(v1, v2) + + def test_length_one_axis(self): + # gh-5890, gh-9524 : length-1 axis is legal for method='linear'. + # Along the axis it's linear interpolation; away from the length-1 + # axis, it's an extrapolation, so fill_value should be used. + + values = np.array([[0.1, 1, 10]]) + xi = np.array([[1, 2.2], [1, 3.2], [1, 3.8]]) + + res = interpn(([1], [2, 3, 4]), values, xi) + wanted = [0.9*0.2 + 0.1, # on [2, 3) it's 0.9*(x-2) + 0.1 + 9*0.2 + 1, # on [3, 4] it's 9*(x-3) + 1 + 9*0.8 + 1] + + xp_assert_close(res, wanted, atol=1e-15) + + # check extrapolation + xi = np.array([[1.1, 2.2], [1.5, 3.2], [-2.3, 3.8]]) + res = interpn(([1], [2, 3, 4]), values, xi, + bounds_error=False, fill_value=None) + + xp_assert_close(res, wanted, atol=1e-15) + + def test_descending_points(self): + def value_func_4d(x, y, z, a): + return 2 * x ** 3 + 3 * y ** 2 - z - a + + x1 = np.array([0, 1, 2, 3]) + x2 = np.array([0, 10, 20, 30]) + x3 = np.array([0, 10, 20, 30]) + x4 = np.array([0, .1, .2, .30]) + points = (x1, x2, x3, x4) + values = value_func_4d( + *np.meshgrid(*points, indexing='ij', sparse=True)) + pts = (0.1, 0.3, np.transpose(np.linspace(0, 30, 4)), + np.linspace(0, 0.3, 4)) + correct_result = interpn(points, values, pts) + + x1_descend = x1[::-1] + x2_descend = x2[::-1] + x3_descend = x3[::-1] + x4_descend = x4[::-1] + points_shuffled = (x1_descend, x2_descend, x3_descend, x4_descend) + values_shuffled = value_func_4d( + *np.meshgrid(*points_shuffled, indexing='ij', sparse=True)) + test_result = interpn(points_shuffled, values_shuffled, pts) + + xp_assert_equal(correct_result, test_result) + + def test_invalid_points_order(self): + x = np.array([.5, 2., 0., 4., 5.5]) # not ascending or descending + y = np.array([.5, 2., 3., 4., 5.5]) + z = np.array([[1, 2, 1, 2, 1], [1, 2, 1, 2, 1], [1, 2, 3, 2, 1], + [1, 2, 2, 2, 1], [1, 2, 1, 2, 1]]) + xi = np.array([[1, 2.3, 6.3, 0.5, 3.3, 1.2, 3], + [1, 3.3, 1.2, -4.0, 5.0, 1.0, 3]]).T + + match = "must be strictly ascending or descending" + with pytest.raises(ValueError, match=match): + interpn((x, y), z, xi) + + def test_invalid_xi_dimensions(self): + # https://github.com/scipy/scipy/issues/16519 + points = [(0, 1)] + values = [0, 1] + xi = np.ones((1, 1, 3)) + msg = ("The requested sample points xi have dimension 3, but this " + "RegularGridInterpolator has dimension 1") + with assert_raises(ValueError, match=msg): + interpn(points, values, xi) + + def test_readonly_grid(self): + # https://github.com/scipy/scipy/issues/17716 + x = np.linspace(0, 4, 5) + y = np.linspace(0, 5, 6) + z = np.linspace(0, 6, 7) + points = (x, y, z) + values = np.ones((5, 6, 7)) + point = np.array([2.21, 3.12, 1.15]) + for d in points: + d.flags.writeable = False + values.flags.writeable = False + point.flags.writeable = False + interpn(points, values, point) + RegularGridInterpolator(points, values)(point) + + def test_2d_readonly_grid(self): + # https://github.com/scipy/scipy/issues/17716 + # test special 2d case + x = np.linspace(0, 4, 5) + y = np.linspace(0, 5, 6) + points = (x, y) + values = np.ones((5, 6)) + point = np.array([2.21, 3.12]) + for d in points: + d.flags.writeable = False + values.flags.writeable = False + point.flags.writeable = False + interpn(points, values, point) + RegularGridInterpolator(points, values)(point) + + def test_non_c_contiguous_grid(self): + # https://github.com/scipy/scipy/issues/17716 + x = np.linspace(0, 4, 5) + x = np.vstack((x, np.empty_like(x))).T.copy()[:, 0] + assert not x.flags.c_contiguous + y = np.linspace(0, 5, 6) + z = np.linspace(0, 6, 7) + points = (x, y, z) + values = np.ones((5, 6, 7)) + point = np.array([2.21, 3.12, 1.15]) + interpn(points, values, point) + RegularGridInterpolator(points, values)(point) + + @pytest.mark.parametrize("dtype", ['>f8', '`__ + +MATLAB® files +============= + +.. autosummary:: + :toctree: generated/ + + loadmat - Read a MATLAB style mat file (version 4 through 7.1) + savemat - Write a MATLAB style mat file (version 4 through 7.1) + whosmat - List contents of a MATLAB style mat file (version 4 through 7.1) + +For low-level MATLAB reading and writing utilities, see `scipy.io.matlab`. + +IDL® files +========== + +.. autosummary:: + :toctree: generated/ + + readsav - Read an IDL 'save' file + +Matrix Market files +=================== + +.. autosummary:: + :toctree: generated/ + + mminfo - Query matrix info from Matrix Market formatted file + mmread - Read matrix from Matrix Market formatted file + mmwrite - Write matrix to Matrix Market formatted file + +Unformatted Fortran files +=============================== + +.. autosummary:: + :toctree: generated/ + + FortranFile - A file object for unformatted sequential Fortran files + FortranEOFError - Exception indicating the end of a well-formed file + FortranFormattingError - Exception indicating an inappropriate end + +Netcdf +====== + +.. autosummary:: + :toctree: generated/ + + netcdf_file - A file object for NetCDF data + netcdf_variable - A data object for the netcdf module + +Harwell-Boeing files +==================== + +.. autosummary:: + :toctree: generated/ + + hb_read -- read H-B file + hb_write -- write H-B file + +Wav sound files (:mod:`scipy.io.wavfile`) +========================================= + +.. module:: scipy.io.wavfile + +.. autosummary:: + :toctree: generated/ + + read + write + WavFileWarning + +Arff files (:mod:`scipy.io.arff`) +================================= + +.. module:: scipy.io.arff + +.. autosummary:: + :toctree: generated/ + + loadarff + MetaData + ArffError + ParseArffError +""" +# matfile read and write +from .matlab import loadmat, savemat, whosmat + +# netCDF file support +from ._netcdf import netcdf_file, netcdf_variable + +# Fortran file support +from ._fortran import FortranFile, FortranEOFError, FortranFormattingError + +from ._fast_matrix_market import mminfo, mmread, mmwrite +from ._idl import readsav +from ._harwell_boeing import hb_read, hb_write + +# Deprecated namespaces, to be removed in v2.0.0 +from . import arff, harwell_boeing, idl, mmio, netcdf, wavfile + +__all__ = [s for s in dir() if not s.startswith('_')] + +from scipy._lib._testutils import PytestTester +test = PytestTester(__name__) +del PytestTester diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/_fast_matrix_market/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/_fast_matrix_market/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..cfd6f8fb30ea8dcc2a9de7b1be23a9538c130718 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/_fast_matrix_market/__init__.py @@ -0,0 +1,598 @@ +# Copyright (C) 2022-2023 Adam Lugowski. All rights reserved. +# Use of this source code is governed by the BSD 2-clause license found in +# the LICENSE.txt file. +# SPDX-License-Identifier: BSD-2-Clause +""" +Matrix Market I/O with a C++ backend. +See http://math.nist.gov/MatrixMarket/formats.html +for information about the Matrix Market format. + +.. versionadded:: 1.12.0 +""" +import io +import os + +import numpy as np +from scipy.sparse import coo_array, issparse, coo_matrix +from scipy.io import _mmio + +__all__ = ['mminfo', 'mmread', 'mmwrite'] + +PARALLELISM = 0 +""" +Number of threads that `mmread()` and `mmwrite()` use. +0 means number of CPUs in the system. +Use `threadpoolctl` to set this value. +""" + +ALWAYS_FIND_SYMMETRY = False +""" +Whether mmwrite() with symmetry='AUTO' will always search for symmetry +inside the matrix. This is scipy.io._mmio.mmwrite()'s default behavior, +but has a significant performance cost on large matrices. +""" + +_field_to_dtype = { + "integer": "int64", + "unsigned-integer": "uint64", + "real": "float64", + "complex": "complex", + "pattern": "float64", +} + + +def _fmm_version(): + from . import _fmm_core + return _fmm_core.__version__ + + +# Register with threadpoolctl, if available +try: + import threadpoolctl + + class _FMMThreadPoolCtlController(threadpoolctl.LibController): + user_api = "scipy" + internal_api = "scipy_mmio" + + filename_prefixes = ("_fmm_core",) + + def get_num_threads(self): + global PARALLELISM + return PARALLELISM + + def set_num_threads(self, num_threads): + global PARALLELISM + PARALLELISM = num_threads + + def get_version(self): + return _fmm_version + + def set_additional_attributes(self): + pass + + threadpoolctl.register(_FMMThreadPoolCtlController) +except (ImportError, AttributeError): + # threadpoolctl not installed or version too old + pass + + +class _TextToBytesWrapper(io.BufferedReader): + """ + Convert a TextIOBase string stream to a byte stream. + """ + + def __init__(self, text_io_buffer, encoding=None, errors=None, **kwargs): + super().__init__(text_io_buffer, **kwargs) + self.encoding = encoding or text_io_buffer.encoding or 'utf-8' + self.errors = errors or text_io_buffer.errors or 'strict' + + def __del__(self): + # do not close the wrapped stream + self.detach() + + def _encoding_call(self, method_name, *args, **kwargs): + raw_method = getattr(self.raw, method_name) + val = raw_method(*args, **kwargs) + return val.encode(self.encoding, errors=self.errors) + + def read(self, size=-1): + return self._encoding_call('read', size) + + def read1(self, size=-1): + return self._encoding_call('read1', size) + + def peek(self, size=-1): + return self._encoding_call('peek', size) + + def seek(self, offset, whence=0): + # Random seeks are not allowed because of non-trivial conversion + # between byte and character offsets, + # with the possibility of a byte offset landing within a character. + if offset == 0 and whence == 0 or \ + offset == 0 and whence == 2: + # seek to start or end is ok + super().seek(offset, whence) + else: + # Drop any other seek + # In this application this may happen when pystreambuf seeks during sync(), + # which can happen when closing a partially-read stream. + # Ex. when mminfo() only reads the header then exits. + pass + + +def _read_body_array(cursor): + """ + Read MatrixMarket array body + """ + from . import _fmm_core + + vals = np.zeros(cursor.header.shape, dtype=_field_to_dtype.get(cursor.header.field)) + _fmm_core.read_body_array(cursor, vals) + return vals + + +def _read_body_coo(cursor, generalize_symmetry=True): + """ + Read MatrixMarket coordinate body + """ + from . import _fmm_core + + index_dtype = "int32" + if cursor.header.nrows >= 2**31 or cursor.header.ncols >= 2**31: + # Dimensions are too large to fit in int32 + index_dtype = "int64" + + i = np.zeros(cursor.header.nnz, dtype=index_dtype) + j = np.zeros(cursor.header.nnz, dtype=index_dtype) + data = np.zeros(cursor.header.nnz, dtype=_field_to_dtype.get(cursor.header.field)) + + _fmm_core.read_body_coo(cursor, i, j, data) + + if generalize_symmetry and cursor.header.symmetry != "general": + off_diagonal_mask = (i != j) + off_diagonal_rows = i[off_diagonal_mask] + off_diagonal_cols = j[off_diagonal_mask] + off_diagonal_data = data[off_diagonal_mask] + + if cursor.header.symmetry == "skew-symmetric": + off_diagonal_data *= -1 + elif cursor.header.symmetry == "hermitian": + off_diagonal_data = off_diagonal_data.conjugate() + + i = np.concatenate((i, off_diagonal_cols)) + j = np.concatenate((j, off_diagonal_rows)) + data = np.concatenate((data, off_diagonal_data)) + + return (data, (i, j)), cursor.header.shape + + +def _get_read_cursor(source, parallelism=None): + """ + Open file for reading. + """ + from . import _fmm_core + + ret_stream_to_close = None + if parallelism is None: + parallelism = PARALLELISM + + try: + source = os.fspath(source) + # It's a file path + is_path = True + except TypeError: + is_path = False + + if is_path: + path = str(source) + if path.endswith('.gz'): + import gzip + source = gzip.GzipFile(path, 'r') + ret_stream_to_close = source + elif path.endswith('.bz2'): + import bz2 + source = bz2.BZ2File(path, 'rb') + ret_stream_to_close = source + else: + return _fmm_core.open_read_file(path, parallelism), ret_stream_to_close + + # Stream object. + if hasattr(source, "read"): + if isinstance(source, io.TextIOBase): + source = _TextToBytesWrapper(source) + return _fmm_core.open_read_stream(source, parallelism), ret_stream_to_close + else: + raise TypeError("Unknown source type") + + +def _get_write_cursor(target, h=None, comment=None, parallelism=None, + symmetry="general", precision=None): + """ + Open file for writing. + """ + from . import _fmm_core + + if parallelism is None: + parallelism = PARALLELISM + if comment is None: + comment = '' + if symmetry is None: + symmetry = "general" + if precision is None: + precision = -1 + + if not h: + h = _fmm_core.header(comment=comment, symmetry=symmetry) + + try: + target = os.fspath(target) + # It's a file path + if target[-4:] != '.mtx': + target += '.mtx' + return _fmm_core.open_write_file(str(target), h, parallelism, precision) + except TypeError: + pass + + if hasattr(target, "write"): + # Stream object. + if isinstance(target, io.TextIOBase): + raise TypeError("target stream must be open in binary mode.") + return _fmm_core.open_write_stream(target, h, parallelism, precision) + else: + raise TypeError("Unknown source object") + + +def _apply_field(data, field, no_pattern=False): + """ + Ensure that ``data.dtype`` is compatible with the specified MatrixMarket field type. + + Parameters + ---------- + data : ndarray + Input array. + + field : str + Matrix Market field, such as 'real', 'complex', 'integer', 'pattern'. + + no_pattern : bool, optional + Whether an empty array may be returned for a 'pattern' field. + + Returns + ------- + data : ndarray + Input data if no conversion necessary, or a converted version + """ + + if field is None: + return data + if field == "pattern": + if no_pattern: + return data + else: + return np.zeros(0) + + dtype = _field_to_dtype.get(field, None) + if dtype is None: + raise ValueError("Invalid field.") + + return np.asarray(data, dtype=dtype) + + +def _validate_symmetry(symmetry): + """ + Check that the symmetry parameter is one that MatrixMarket allows.. + """ + if symmetry is None: + return "general" + + symmetry = str(symmetry).lower() + symmetries = ["general", "symmetric", "skew-symmetric", "hermitian"] + if symmetry not in symmetries: + raise ValueError("Invalid symmetry. Must be one of: " + ", ".join(symmetries)) + + return symmetry + + +def mmread(source, *, spmatrix=True): + """ + Reads the contents of a Matrix Market file-like 'source' into a matrix. + + Parameters + ---------- + source : str or file-like + Matrix Market filename (extensions .mtx, .mtz.gz) + or open file-like object. + spmatrix : bool, optional (default: True) + If ``True``, return sparse ``coo_matrix``. Otherwise return ``coo_array``. + + Returns + ------- + a : ndarray or coo_array + Dense or sparse array depending on the matrix format in the + Matrix Market file. + + Notes + ----- + .. versionchanged:: 1.12.0 + C++ implementation. + + Examples + -------- + >>> from io import StringIO + >>> from scipy.io import mmread + + >>> text = '''%%MatrixMarket matrix coordinate real general + ... 5 5 7 + ... 2 3 1.0 + ... 3 4 2.0 + ... 3 5 3.0 + ... 4 1 4.0 + ... 4 2 5.0 + ... 4 3 6.0 + ... 4 4 7.0 + ... ''' + + ``mmread(source)`` returns the data as sparse array in COO format. + + >>> m = mmread(StringIO(text), spmatrix=False) + >>> m + + >>> m.toarray() + array([[0., 0., 0., 0., 0.], + [0., 0., 1., 0., 0.], + [0., 0., 0., 2., 3.], + [4., 5., 6., 7., 0.], + [0., 0., 0., 0., 0.]]) + + This method is threaded. + The default number of threads is equal to the number of CPUs in the system. + Use `threadpoolctl `_ to override: + + >>> import threadpoolctl + >>> + >>> with threadpoolctl.threadpool_limits(limits=2): + ... m = mmread(StringIO(text), spmatrix=False) + + """ + cursor, stream_to_close = _get_read_cursor(source) + + if cursor.header.format == "array": + mat = _read_body_array(cursor) + if stream_to_close: + stream_to_close.close() + return mat + else: + triplet, shape = _read_body_coo(cursor, generalize_symmetry=True) + if stream_to_close: + stream_to_close.close() + if spmatrix: + return coo_matrix(triplet, shape=shape) + return coo_array(triplet, shape=shape) + + +def mmwrite(target, a, comment=None, field=None, precision=None, symmetry="AUTO"): + r""" + Writes the sparse or dense array `a` to Matrix Market file-like `target`. + + Parameters + ---------- + target : str or file-like + Matrix Market filename (extension .mtx) or open file-like object. + a : array like + Sparse or dense 2-D array. + comment : str, optional + Comments to be prepended to the Matrix Market file. + field : None or str, optional + Either 'real', 'complex', 'pattern', or 'integer'. + precision : None or int, optional + Number of digits to display for real or complex values. + symmetry : None or str, optional + Either 'AUTO', 'general', 'symmetric', 'skew-symmetric', or 'hermitian'. + If symmetry is None the symmetry type of 'a' is determined by its + values. If symmetry is 'AUTO' the symmetry type of 'a' is either + determined or set to 'general', at mmwrite's discretion. + + Returns + ------- + None + + Notes + ----- + .. versionchanged:: 1.12.0 + C++ implementation. + + Examples + -------- + >>> from io import BytesIO + >>> import numpy as np + >>> from scipy.sparse import coo_array + >>> from scipy.io import mmwrite + + Write a small NumPy array to a matrix market file. The file will be + written in the ``'array'`` format. + + >>> a = np.array([[1.0, 0, 0, 0], [0, 2.5, 0, 6.25]]) + >>> target = BytesIO() + >>> mmwrite(target, a) + >>> print(target.getvalue().decode('latin1')) + %%MatrixMarket matrix array real general + % + 2 4 + 1 + 0 + 0 + 2.5 + 0 + 0 + 0 + 6.25 + + Add a comment to the output file, and set the precision to 3. + + >>> target = BytesIO() + >>> mmwrite(target, a, comment='\n Some test data.\n', precision=3) + >>> print(target.getvalue().decode('latin1')) + %%MatrixMarket matrix array real general + % + % Some test data. + % + 2 4 + 1.00e+00 + 0.00e+00 + 0.00e+00 + 2.50e+00 + 0.00e+00 + 0.00e+00 + 0.00e+00 + 6.25e+00 + + Convert to a sparse matrix before calling ``mmwrite``. This will + result in the output format being ``'coordinate'`` rather than + ``'array'``. + + >>> target = BytesIO() + >>> mmwrite(target, coo_array(a), precision=3) + >>> print(target.getvalue().decode('latin1')) + %%MatrixMarket matrix coordinate real general + % + 2 4 3 + 1 1 1.00e+00 + 2 2 2.50e+00 + 2 4 6.25e+00 + + Write a complex Hermitian array to a matrix market file. Note that + only six values are actually written to the file; the other values + are implied by the symmetry. + + >>> z = np.array([[3, 1+2j, 4-3j], [1-2j, 1, -5j], [4+3j, 5j, 2.5]]) + >>> z + array([[ 3. +0.j, 1. +2.j, 4. -3.j], + [ 1. -2.j, 1. +0.j, -0. -5.j], + [ 4. +3.j, 0. +5.j, 2.5+0.j]]) + + >>> target = BytesIO() + >>> mmwrite(target, z, precision=2) + >>> print(target.getvalue().decode('latin1')) + %%MatrixMarket matrix array complex hermitian + % + 3 3 + 3.0e+00 0.0e+00 + 1.0e+00 -2.0e+00 + 4.0e+00 3.0e+00 + 1.0e+00 0.0e+00 + 0.0e+00 5.0e+00 + 2.5e+00 0.0e+00 + + This method is threaded. + The default number of threads is equal to the number of CPUs in the system. + Use `threadpoolctl `_ to override: + + >>> import threadpoolctl + >>> + >>> target = BytesIO() + >>> with threadpoolctl.threadpool_limits(limits=2): + ... mmwrite(target, a) + + """ + from . import _fmm_core + + if isinstance(a, list) or isinstance(a, tuple) or hasattr(a, "__array__"): + a = np.asarray(a) + + if symmetry == "AUTO": + if ALWAYS_FIND_SYMMETRY or (hasattr(a, "shape") and max(a.shape) < 100): + symmetry = None + else: + symmetry = "general" + + if symmetry is None: + symmetry = _mmio.MMFile()._get_symmetry(a) + + symmetry = _validate_symmetry(symmetry) + cursor = _get_write_cursor(target, comment=comment, + precision=precision, symmetry=symmetry) + + if isinstance(a, np.ndarray): + # Write dense numpy arrays + a = _apply_field(a, field, no_pattern=True) + _fmm_core.write_body_array(cursor, a) + + elif issparse(a): + # Write sparse scipy matrices + a = a.tocoo() + + if symmetry is not None and symmetry != "general": + # A symmetric matrix only specifies the elements below the diagonal. + # Ensure that the matrix satisfies this requirement. + lower_triangle_mask = a.row >= a.col + a = coo_array((a.data[lower_triangle_mask], + (a.row[lower_triangle_mask], + a.col[lower_triangle_mask])), shape=a.shape) + + data = _apply_field(a.data, field) + _fmm_core.write_body_coo(cursor, a.shape, a.row, a.col, data) + + else: + raise ValueError(f"unknown matrix type: {type(a)}") + + +def mminfo(source): + """ + Return size and storage parameters from Matrix Market file-like 'source'. + + Parameters + ---------- + source : str or file-like + Matrix Market filename (extension .mtx) or open file-like object + + Returns + ------- + rows : int + Number of matrix rows. + cols : int + Number of matrix columns. + entries : int + Number of non-zero entries of a sparse matrix + or rows*cols for a dense matrix. + format : str + Either 'coordinate' or 'array'. + field : str + Either 'real', 'complex', 'pattern', or 'integer'. + symmetry : str + Either 'general', 'symmetric', 'skew-symmetric', or 'hermitian'. + + Notes + ----- + .. versionchanged:: 1.12.0 + C++ implementation. + + Examples + -------- + >>> from io import StringIO + >>> from scipy.io import mminfo + + >>> text = '''%%MatrixMarket matrix coordinate real general + ... 5 5 7 + ... 2 3 1.0 + ... 3 4 2.0 + ... 3 5 3.0 + ... 4 1 4.0 + ... 4 2 5.0 + ... 4 3 6.0 + ... 4 4 7.0 + ... ''' + + + ``mminfo(source)`` returns the number of rows, number of columns, + format, field type and symmetry attribute of the source file. + + >>> mminfo(StringIO(text)) + (5, 5, 7, 'coordinate', 'real', 'general') + """ + cursor, stream_to_close = _get_read_cursor(source, 1) + h = cursor.header + cursor.close() + if stream_to_close: + stream_to_close.close() + return h.nrows, h.ncols, h.nnz, h.format, h.field, h.symmetry diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/_fortran.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/_fortran.py new file mode 100644 index 0000000000000000000000000000000000000000..ac491dce68fe2f2f171dcee5a3097b0f4c4ea10c --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/_fortran.py @@ -0,0 +1,354 @@ +""" +Module to read / write Fortran unformatted sequential files. + +This is in the spirit of code written by Neil Martinsen-Burrell and Joe Zuntz. + +""" +import warnings +import numpy as np + +__all__ = ['FortranFile', 'FortranEOFError', 'FortranFormattingError'] + + +class FortranEOFError(TypeError, OSError): + """Indicates that the file ended properly. + + This error descends from TypeError because the code used to raise + TypeError (and this was the only way to know that the file had + ended) so users might have ``except TypeError:``. + + """ + pass + + +class FortranFormattingError(TypeError, OSError): + """Indicates that the file ended mid-record. + + Descends from TypeError for backward compatibility. + + """ + pass + + +class FortranFile: + """ + A file object for unformatted sequential files from Fortran code. + + Parameters + ---------- + filename : file or str + Open file object or filename. + mode : {'r', 'w'}, optional + Read-write mode, default is 'r'. + header_dtype : dtype, optional + Data type of the header. Size and endianness must match the input/output file. + + Notes + ----- + These files are broken up into records of unspecified types. The size of + each record is given at the start (although the size of this header is not + standard) and the data is written onto disk without any formatting. Fortran + compilers supporting the BACKSPACE statement will write a second copy of + the size to facilitate backwards seeking. + + This class only supports files written with both sizes for the record. + It also does not support the subrecords used in Intel and gfortran compilers + for records which are greater than 2GB with a 4-byte header. + + An example of an unformatted sequential file in Fortran would be written as:: + + OPEN(1, FILE=myfilename, FORM='unformatted') + + WRITE(1) myvariable + + Since this is a non-standard file format, whose contents depend on the + compiler and the endianness of the machine, caution is advised. Files from + gfortran 4.8.0 and gfortran 4.1.2 on x86_64 are known to work. + + Consider using Fortran direct-access files or files from the newer Stream + I/O, which can be easily read by `numpy.fromfile`. + + Examples + -------- + To create an unformatted sequential Fortran file: + + >>> from scipy.io import FortranFile + >>> import numpy as np + >>> f = FortranFile('test.unf', 'w') + >>> f.write_record(np.array([1,2,3,4,5], dtype=np.int32)) + >>> f.write_record(np.linspace(0,1,20).reshape((5,4)).T) + >>> f.close() + + To read this file: + + >>> f = FortranFile('test.unf', 'r') + >>> print(f.read_ints(np.int32)) + [1 2 3 4 5] + >>> print(f.read_reals(float).reshape((5,4), order="F")) + [[0. 0.05263158 0.10526316 0.15789474] + [0.21052632 0.26315789 0.31578947 0.36842105] + [0.42105263 0.47368421 0.52631579 0.57894737] + [0.63157895 0.68421053 0.73684211 0.78947368] + [0.84210526 0.89473684 0.94736842 1. ]] + >>> f.close() + + Or, in Fortran:: + + integer :: a(5), i + double precision :: b(5,4) + open(1, file='test.unf', form='unformatted') + read(1) a + read(1) b + close(1) + write(*,*) a + do i = 1, 5 + write(*,*) b(i,:) + end do + + """ + def __init__(self, filename, mode='r', header_dtype=np.uint32): + if header_dtype is None: + raise ValueError('Must specify dtype') + + header_dtype = np.dtype(header_dtype) + if header_dtype.kind != 'u': + warnings.warn("Given a dtype which is not unsigned.", stacklevel=2) + + if mode not in 'rw' or len(mode) != 1: + raise ValueError('mode must be either r or w') + + if hasattr(filename, 'seek'): + self._fp = filename + else: + self._fp = open(filename, f'{mode}b') + + self._header_dtype = header_dtype + + def _read_size(self, eof_ok=False): + n = self._header_dtype.itemsize + b = self._fp.read(n) + if (not b) and eof_ok: + raise FortranEOFError("End of file occurred at end of record") + elif len(b) < n: + raise FortranFormattingError( + "End of file in the middle of the record size") + return int(np.frombuffer(b, dtype=self._header_dtype, count=1)[0]) + + def write_record(self, *items): + """ + Write a record (including sizes) to the file. + + Parameters + ---------- + *items : array_like + The data arrays to write. + + Notes + ----- + Writes data items to a file:: + + write_record(a.T, b.T, c.T, ...) + + write(1) a, b, c, ... + + Note that data in multidimensional arrays is written in + row-major order --- to make them read correctly by Fortran + programs, you need to transpose the arrays yourself when + writing them. + + """ + items = tuple(np.asarray(item) for item in items) + total_size = sum(item.nbytes for item in items) + + nb = np.array([total_size], dtype=self._header_dtype) + + nb.tofile(self._fp) + for item in items: + item.tofile(self._fp) + nb.tofile(self._fp) + + def read_record(self, *dtypes, **kwargs): + """ + Reads a record of a given type from the file. + + Parameters + ---------- + *dtypes : dtypes, optional + Data type(s) specifying the size and endianness of the data. + + Returns + ------- + data : ndarray + A 1-D array object. + + Raises + ------ + FortranEOFError + To signal that no further records are available + FortranFormattingError + To signal that the end of the file was encountered + part-way through a record + + Notes + ----- + If the record contains a multidimensional array, you can specify + the size in the dtype. For example:: + + INTEGER var(5,4) + + can be read with:: + + read_record('(4,5)i4').T + + Note that this function does **not** assume the file data is in Fortran + column major order, so you need to (i) swap the order of dimensions + when reading and (ii) transpose the resulting array. + + Alternatively, you can read the data as a 1-D array and handle the + ordering yourself. For example:: + + read_record('i4').reshape(5, 4, order='F') + + For records that contain several variables or mixed types (as opposed + to single scalar or array types), give them as separate arguments:: + + double precision :: a + integer :: b + write(1) a, b + + record = f.read_record(' 0, -n and n if n < 0 + + Parameters + ---------- + n : int + max number one wants to be able to represent + min : int + minimum number of characters to use for the format + + Returns + ------- + res : IntFormat + IntFormat instance with reasonable (see Notes) computed width + + Notes + ----- + Reasonable should be understood as the minimal string length necessary + without losing precision. For example, IntFormat.from_number(1) will + return an IntFormat instance of width 2, so that any 0 and 1 may be + represented as 1-character strings without loss of information. + """ + width = number_digits(n) + 1 + if n < 0: + width += 1 + repeat = 80 // width + return cls(width, min, repeat=repeat) + + def __init__(self, width, min=None, repeat=None): + self.width = width + self.repeat = repeat + self.min = min + + def __repr__(self): + r = "IntFormat(" + if self.repeat: + r += "%d" % self.repeat + r += "I%d" % self.width + if self.min: + r += ".%d" % self.min + return r + ")" + + @property + def fortran_format(self): + r = "(" + if self.repeat: + r += "%d" % self.repeat + r += "I%d" % self.width + if self.min: + r += ".%d" % self.min + return r + ")" + + @property + def python_format(self): + return "%" + str(self.width) + "d" + + +class ExpFormat: + @classmethod + def from_number(cls, n, min=None): + """Given a float number, returns a "reasonable" ExpFormat instance to + represent any number between -n and n. + + Parameters + ---------- + n : float + max number one wants to be able to represent + min : int + minimum number of characters to use for the format + + Returns + ------- + res : ExpFormat + ExpFormat instance with reasonable (see Notes) computed width + + Notes + ----- + Reasonable should be understood as the minimal string length necessary + to avoid losing precision. + """ + # len of one number in exp format: sign + 1|0 + "." + + # number of digit for fractional part + 'E' + sign of exponent + + # len of exponent + finfo = np.finfo(n.dtype) + # Number of digits for fractional part + n_prec = finfo.precision + 1 + # Number of digits for exponential part + n_exp = number_digits(np.max(np.abs([finfo.maxexp, finfo.minexp]))) + width = 1 + 1 + n_prec + 1 + n_exp + 1 + if n < 0: + width += 1 + repeat = int(np.floor(80 / width)) + return cls(width, n_prec, min, repeat=repeat) + + def __init__(self, width, significand, min=None, repeat=None): + """\ + Parameters + ---------- + width : int + number of characters taken by the string (includes space). + """ + self.width = width + self.significand = significand + self.repeat = repeat + self.min = min + + def __repr__(self): + r = "ExpFormat(" + if self.repeat: + r += "%d" % self.repeat + r += "E%d.%d" % (self.width, self.significand) + if self.min: + r += "E%d" % self.min + return r + ")" + + @property + def fortran_format(self): + r = "(" + if self.repeat: + r += "%d" % self.repeat + r += "E%d.%d" % (self.width, self.significand) + if self.min: + r += "E%d" % self.min + return r + ")" + + @property + def python_format(self): + return "%" + str(self.width-1) + "." + str(self.significand) + "E" + + +class Token: + def __init__(self, type, value, pos): + self.type = type + self.value = value + self.pos = pos + + def __str__(self): + return f"""Token('{self.type}', "{self.value}")""" + + def __repr__(self): + return self.__str__() + + +class Tokenizer: + def __init__(self): + self.tokens = list(TOKENS.keys()) + self.res = [re.compile(TOKENS[i]) for i in self.tokens] + + def input(self, s): + self.data = s + self.curpos = 0 + self.len = len(s) + + def next_token(self): + curpos = self.curpos + + while curpos < self.len: + for i, r in enumerate(self.res): + m = r.match(self.data, curpos) + if m is None: + continue + else: + self.curpos = m.end() + return Token(self.tokens[i], m.group(), self.curpos) + raise SyntaxError("Unknown character at position %d (%s)" + % (self.curpos, self.data[curpos])) + + +# Grammar for fortran format: +# format : LPAR format_string RPAR +# format_string : repeated | simple +# repeated : repeat simple +# simple : int_fmt | exp_fmt +# int_fmt : INT_ID width +# exp_fmt : simple_exp_fmt +# simple_exp_fmt : EXP_ID width DOT significand +# extended_exp_fmt : EXP_ID width DOT significand EXP_ID ndigits +# repeat : INT +# width : INT +# significand : INT +# ndigits : INT + +# Naive fortran formatter - parser is hand-made +class FortranFormatParser: + """Parser for Fortran format strings. The parse method returns a *Format + instance. + + Notes + ----- + Only ExpFormat (exponential format for floating values) and IntFormat + (integer format) for now. + """ + def __init__(self): + self.tokenizer = threading.local() + + def parse(self, s): + if not hasattr(self.tokenizer, 't'): + self.tokenizer.t = Tokenizer() + + self.tokenizer.t.input(s) + + tokens = [] + + try: + while True: + t = self.tokenizer.t.next_token() + if t is None: + break + else: + tokens.append(t) + return self._parse_format(tokens) + except SyntaxError as e: + raise BadFortranFormat(str(e)) from e + + def _get_min(self, tokens): + next = tokens.pop(0) + if not next.type == "DOT": + raise SyntaxError() + next = tokens.pop(0) + return next.value + + def _expect(self, token, tp): + if not token.type == tp: + raise SyntaxError() + + def _parse_format(self, tokens): + if not tokens[0].type == "LPAR": + raise SyntaxError("Expected left parenthesis at position " + "%d (got '%s')" % (0, tokens[0].value)) + elif not tokens[-1].type == "RPAR": + raise SyntaxError("Expected right parenthesis at position " + f"{len(tokens)} (got '{tokens[-1].value}')") + + tokens = tokens[1:-1] + types = [t.type for t in tokens] + if types[0] == "INT": + repeat = int(tokens.pop(0).value) + else: + repeat = None + + next = tokens.pop(0) + if next.type == "INT_ID": + next = self._next(tokens, "INT") + width = int(next.value) + if tokens: + min = int(self._get_min(tokens)) + else: + min = None + return IntFormat(width, min, repeat) + elif next.type == "EXP_ID": + next = self._next(tokens, "INT") + width = int(next.value) + + next = self._next(tokens, "DOT") + + next = self._next(tokens, "INT") + significand = int(next.value) + + if tokens: + next = self._next(tokens, "EXP_ID") + + next = self._next(tokens, "INT") + min = int(next.value) + else: + min = None + return ExpFormat(width, significand, min, repeat) + else: + raise SyntaxError(f"Invalid formatter type {next.value}") + + def _next(self, tokens, tp): + if not len(tokens) > 0: + raise SyntaxError() + next = tokens.pop(0) + self._expect(next, tp) + return next diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/_harwell_boeing/hb.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/_harwell_boeing/hb.py new file mode 100644 index 0000000000000000000000000000000000000000..96fef89ac35a271f3a5501beaefd06da13ede685 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/_harwell_boeing/hb.py @@ -0,0 +1,575 @@ +""" +Implementation of Harwell-Boeing read/write. + +At the moment not the full Harwell-Boeing format is supported. Supported +features are: + + - assembled, non-symmetric, real matrices + - integer for pointer/indices + - exponential format for float values, and int format + +""" +# TODO: +# - Add more support (symmetric/complex matrices, non-assembled matrices ?) + +# XXX: reading is reasonably efficient (>= 85 % is in numpy.fromstring), but +# takes a lot of memory. Being faster would require compiled code. +# write is not efficient. Although not a terribly exciting task, +# having reusable facilities to efficiently read/write fortran-formatted files +# would be useful outside this module. + +import warnings + +import numpy as np +from scipy.sparse import csc_array, csc_matrix +from ._fortran_format_parser import FortranFormatParser, IntFormat, ExpFormat + +__all__ = ["hb_read", "hb_write"] + + +class MalformedHeader(Exception): + pass + + +class LineOverflow(Warning): + pass + + +def _nbytes_full(fmt, nlines): + """Return the number of bytes to read to get every full lines for the + given parsed fortran format.""" + return (fmt.repeat * fmt.width + 1) * (nlines - 1) + + +class HBInfo: + @classmethod + def from_data(cls, m, title="Default title", key="0", mxtype=None, fmt=None): + """Create a HBInfo instance from an existing sparse matrix. + + Parameters + ---------- + m : sparse array or matrix + the HBInfo instance will derive its parameters from m + title : str + Title to put in the HB header + key : str + Key + mxtype : HBMatrixType + type of the input matrix + fmt : dict + not implemented + + Returns + ------- + hb_info : HBInfo instance + """ + m = m.tocsc(copy=False) + + pointer = m.indptr + indices = m.indices + values = m.data + + nrows, ncols = m.shape + nnon_zeros = m.nnz + + if fmt is None: + # +1 because HB use one-based indexing (Fortran), and we will write + # the indices /pointer as such + pointer_fmt = IntFormat.from_number(np.max(pointer+1)) + indices_fmt = IntFormat.from_number(np.max(indices+1)) + + if values.dtype.kind in np.typecodes["AllFloat"]: + values_fmt = ExpFormat.from_number(-np.max(np.abs(values))) + elif values.dtype.kind in np.typecodes["AllInteger"]: + values_fmt = IntFormat.from_number(-np.max(np.abs(values))) + else: + message = f"type {values.dtype.kind} not implemented yet" + raise NotImplementedError(message) + else: + raise NotImplementedError("fmt argument not supported yet.") + + if mxtype is None: + if not np.isrealobj(values): + raise ValueError("Complex values not supported yet") + if values.dtype.kind in np.typecodes["AllInteger"]: + tp = "integer" + elif values.dtype.kind in np.typecodes["AllFloat"]: + tp = "real" + else: + raise NotImplementedError( + f"type {values.dtype} for values not implemented") + mxtype = HBMatrixType(tp, "unsymmetric", "assembled") + else: + raise ValueError("mxtype argument not handled yet.") + + def _nlines(fmt, size): + nlines = size // fmt.repeat + if nlines * fmt.repeat != size: + nlines += 1 + return nlines + + pointer_nlines = _nlines(pointer_fmt, pointer.size) + indices_nlines = _nlines(indices_fmt, indices.size) + values_nlines = _nlines(values_fmt, values.size) + + total_nlines = pointer_nlines + indices_nlines + values_nlines + + return cls(title, key, + total_nlines, pointer_nlines, indices_nlines, values_nlines, + mxtype, nrows, ncols, nnon_zeros, + pointer_fmt.fortran_format, indices_fmt.fortran_format, + values_fmt.fortran_format) + + @classmethod + def from_file(cls, fid): + """Create a HBInfo instance from a file object containing a matrix in the + HB format. + + Parameters + ---------- + fid : file-like matrix + File or file-like object containing a matrix in the HB format. + + Returns + ------- + hb_info : HBInfo instance + """ + # First line + line = fid.readline().strip("\n") + if not len(line) > 72: + raise ValueError("Expected at least 72 characters for first line, " + f"got: \n{line}") + title = line[:72] + key = line[72:] + + # Second line + line = fid.readline().strip("\n") + if not len(line.rstrip()) >= 56: + raise ValueError("Expected at least 56 characters for second line, " + f"got: \n{line}") + total_nlines = _expect_int(line[:14]) + pointer_nlines = _expect_int(line[14:28]) + indices_nlines = _expect_int(line[28:42]) + values_nlines = _expect_int(line[42:56]) + + rhs_nlines = line[56:72].strip() + if rhs_nlines == '': + rhs_nlines = 0 + else: + rhs_nlines = _expect_int(rhs_nlines) + if not rhs_nlines == 0: + raise ValueError("Only files without right hand side supported for " + "now.") + + # Third line + line = fid.readline().strip("\n") + if not len(line) >= 70: + raise ValueError(f"Expected at least 72 character for third line, " + f"got:\n{line}") + + mxtype_s = line[:3].upper() + if not len(mxtype_s) == 3: + raise ValueError("mxtype expected to be 3 characters long") + + mxtype = HBMatrixType.from_fortran(mxtype_s) + if mxtype.value_type not in ["real", "integer"]: + raise ValueError("Only real or integer matrices supported for " + f"now (detected {mxtype})") + if not mxtype.structure == "unsymmetric": + raise ValueError("Only unsymmetric matrices supported for " + f"now (detected {mxtype})") + if not mxtype.storage == "assembled": + raise ValueError("Only assembled matrices supported for now") + + if not line[3:14] == " " * 11: + raise ValueError(f"Malformed data for third line: {line}") + + nrows = _expect_int(line[14:28]) + ncols = _expect_int(line[28:42]) + nnon_zeros = _expect_int(line[42:56]) + nelementals = _expect_int(line[56:70]) + if not nelementals == 0: + raise ValueError("Unexpected value %d for nltvl (last entry of line 3)" + % nelementals) + + # Fourth line + line = fid.readline().strip("\n") + + ct = line.split() + if not len(ct) == 3: + raise ValueError(f"Expected 3 formats, got {ct}") + + return cls(title, key, + total_nlines, pointer_nlines, indices_nlines, values_nlines, + mxtype, nrows, ncols, nnon_zeros, + ct[0], ct[1], ct[2], + rhs_nlines, nelementals) + + def __init__(self, title, key, + total_nlines, pointer_nlines, indices_nlines, values_nlines, + mxtype, nrows, ncols, nnon_zeros, + pointer_format_str, indices_format_str, values_format_str, + right_hand_sides_nlines=0, nelementals=0): + """Do not use this directly, but the class ctrs (from_* functions).""" + if title is None: + title = "No Title" + if len(title) > 72: + raise ValueError("title cannot be > 72 characters") + + if key is None: + key = "|No Key" + if len(key) > 8: + warnings.warn(f"key is > 8 characters (key is {key})", + LineOverflow, stacklevel=3) + self.title = title + self.key = key + + self.total_nlines = total_nlines + self.pointer_nlines = pointer_nlines + self.indices_nlines = indices_nlines + self.values_nlines = values_nlines + + parser = FortranFormatParser() + pointer_format = parser.parse(pointer_format_str) + if not isinstance(pointer_format, IntFormat): + raise ValueError("Expected int format for pointer format, got " + f"{pointer_format}") + + indices_format = parser.parse(indices_format_str) + if not isinstance(indices_format, IntFormat): + raise ValueError("Expected int format for indices format, got " + f"{indices_format}") + + values_format = parser.parse(values_format_str) + if isinstance(values_format, ExpFormat): + if mxtype.value_type not in ["real", "complex"]: + raise ValueError(f"Inconsistency between matrix type {mxtype} and " + f"value type {values_format}") + values_dtype = np.float64 + elif isinstance(values_format, IntFormat): + if mxtype.value_type not in ["integer"]: + raise ValueError(f"Inconsistency between matrix type {mxtype} and " + f"value type {values_format}") + # XXX: fortran int -> dtype association ? + values_dtype = int + else: + raise ValueError(f"Unsupported format for values {values_format!r}") + + self.pointer_format = pointer_format + self.indices_format = indices_format + self.values_format = values_format + + self.pointer_dtype = np.int32 + self.indices_dtype = np.int32 + self.values_dtype = values_dtype + + self.pointer_nlines = pointer_nlines + self.pointer_nbytes_full = _nbytes_full(pointer_format, pointer_nlines) + + self.indices_nlines = indices_nlines + self.indices_nbytes_full = _nbytes_full(indices_format, indices_nlines) + + self.values_nlines = values_nlines + self.values_nbytes_full = _nbytes_full(values_format, values_nlines) + + self.nrows = nrows + self.ncols = ncols + self.nnon_zeros = nnon_zeros + self.nelementals = nelementals + self.mxtype = mxtype + + def dump(self): + """Gives the header corresponding to this instance as a string.""" + header = [self.title.ljust(72) + self.key.ljust(8)] + + header.append("%14d%14d%14d%14d" % + (self.total_nlines, self.pointer_nlines, + self.indices_nlines, self.values_nlines)) + header.append("%14s%14d%14d%14d%14d" % + (self.mxtype.fortran_format.ljust(14), self.nrows, + self.ncols, self.nnon_zeros, 0)) + + pffmt = self.pointer_format.fortran_format + iffmt = self.indices_format.fortran_format + vffmt = self.values_format.fortran_format + header.append("%16s%16s%20s" % + (pffmt.ljust(16), iffmt.ljust(16), vffmt.ljust(20))) + return "\n".join(header) + + +def _expect_int(value, msg=None): + try: + return int(value) + except ValueError as e: + if msg is None: + msg = "Expected an int, got %s" + raise ValueError(msg % value) from e + + +def _read_hb_data(content, header): + # XXX: look at a way to reduce memory here (big string creation) + ptr_string = "".join([content.read(header.pointer_nbytes_full), + content.readline()]) + ptr = np.fromstring(ptr_string, + dtype=int, sep=' ') + + ind_string = "".join([content.read(header.indices_nbytes_full), + content.readline()]) + ind = np.fromstring(ind_string, + dtype=int, sep=' ') + + val_string = "".join([content.read(header.values_nbytes_full), + content.readline()]) + val = np.fromstring(val_string, + dtype=header.values_dtype, sep=' ') + + return csc_array((val, ind-1, ptr-1), shape=(header.nrows, header.ncols)) + + +def _write_data(m, fid, header): + m = m.tocsc(copy=False) + + def write_array(f, ar, nlines, fmt): + # ar_nlines is the number of full lines, n is the number of items per + # line, ffmt the fortran format + pyfmt = fmt.python_format + pyfmt_full = pyfmt * fmt.repeat + + # for each array to write, we first write the full lines, and special + # case for partial line + full = ar[:(nlines - 1) * fmt.repeat] + for row in full.reshape((nlines-1, fmt.repeat)): + f.write(pyfmt_full % tuple(row) + "\n") + nremain = ar.size - full.size + if nremain > 0: + f.write((pyfmt * nremain) % tuple(ar[ar.size - nremain:]) + "\n") + + fid.write(header.dump()) + fid.write("\n") + # +1 is for Fortran one-based indexing + write_array(fid, m.indptr+1, header.pointer_nlines, + header.pointer_format) + write_array(fid, m.indices+1, header.indices_nlines, + header.indices_format) + write_array(fid, m.data, header.values_nlines, + header.values_format) + + +class HBMatrixType: + """Class to hold the matrix type.""" + # q2f* translates qualified names to Fortran character + _q2f_type = { + "real": "R", + "complex": "C", + "pattern": "P", + "integer": "I", + } + _q2f_structure = { + "symmetric": "S", + "unsymmetric": "U", + "hermitian": "H", + "skewsymmetric": "Z", + "rectangular": "R" + } + _q2f_storage = { + "assembled": "A", + "elemental": "E", + } + + _f2q_type = {j: i for i, j in _q2f_type.items()} + _f2q_structure = {j: i for i, j in _q2f_structure.items()} + _f2q_storage = {j: i for i, j in _q2f_storage.items()} + + @classmethod + def from_fortran(cls, fmt): + if not len(fmt) == 3: + raise ValueError("Fortran format for matrix type should be 3 " + "characters long") + try: + value_type = cls._f2q_type[fmt[0]] + structure = cls._f2q_structure[fmt[1]] + storage = cls._f2q_storage[fmt[2]] + return cls(value_type, structure, storage) + except KeyError as e: + raise ValueError(f"Unrecognized format {fmt}") from e + + def __init__(self, value_type, structure, storage="assembled"): + self.value_type = value_type + self.structure = structure + self.storage = storage + + if value_type not in self._q2f_type: + raise ValueError(f"Unrecognized type {value_type}") + if structure not in self._q2f_structure: + raise ValueError(f"Unrecognized structure {structure}") + if storage not in self._q2f_storage: + raise ValueError(f"Unrecognized storage {storage}") + + @property + def fortran_format(self): + return self._q2f_type[self.value_type] + \ + self._q2f_structure[self.structure] + \ + self._q2f_storage[self.storage] + + def __repr__(self): + return f"HBMatrixType({self.value_type}, {self.structure}, {self.storage})" + + +class HBFile: + def __init__(self, file, hb_info=None): + """Create a HBFile instance. + + Parameters + ---------- + file : file-object + StringIO work as well + hb_info : HBInfo, optional + Should be given as an argument for writing, in which case the file + should be writable. + """ + self._fid = file + if hb_info is None: + self._hb_info = HBInfo.from_file(file) + else: + #raise OSError("file %s is not writable, and hb_info " + # "was given." % file) + self._hb_info = hb_info + + @property + def title(self): + return self._hb_info.title + + @property + def key(self): + return self._hb_info.key + + @property + def type(self): + return self._hb_info.mxtype.value_type + + @property + def structure(self): + return self._hb_info.mxtype.structure + + @property + def storage(self): + return self._hb_info.mxtype.storage + + def read_matrix(self): + return _read_hb_data(self._fid, self._hb_info) + + def write_matrix(self, m): + return _write_data(m, self._fid, self._hb_info) + + +def hb_read(path_or_open_file, *, spmatrix=True): + """Read HB-format file. + + Parameters + ---------- + path_or_open_file : path-like or file-like + If a file-like object, it is used as-is. Otherwise, it is opened + before reading. + spmatrix : bool, optional (default: True) + If ``True``, return sparse ``coo_matrix``. Otherwise return ``coo_array``. + + Returns + ------- + data : csc_array or csc_matrix + The data read from the HB file as a sparse array. + + Notes + ----- + At the moment not the full Harwell-Boeing format is supported. Supported + features are: + + - assembled, non-symmetric, real matrices + - integer for pointer/indices + - exponential format for float values, and int format + + Examples + -------- + We can read and write a harwell-boeing format file: + + >>> from scipy.io import hb_read, hb_write + >>> from scipy.sparse import csr_array, eye + >>> data = csr_array(eye(3)) # create a sparse array + >>> hb_write("data.hb", data) # write a hb file + >>> print(hb_read("data.hb", spmatrix=False)) # read a hb file + + Coords Values + (0, 0) 1.0 + (1, 1) 1.0 + (2, 2) 1.0 + """ + def _get_matrix(fid): + hb = HBFile(fid) + return hb.read_matrix() + + if hasattr(path_or_open_file, 'read'): + data = _get_matrix(path_or_open_file) + else: + with open(path_or_open_file) as f: + data = _get_matrix(f) + if spmatrix: + return csc_matrix(data) + return data + + +def hb_write(path_or_open_file, m, hb_info=None): + """Write HB-format file. + + Parameters + ---------- + path_or_open_file : path-like or file-like + If a file-like object, it is used as-is. Otherwise, it is opened + before writing. + m : sparse array or matrix + the sparse array to write + hb_info : HBInfo + contains the meta-data for write + + Returns + ------- + None + + Notes + ----- + At the moment not the full Harwell-Boeing format is supported. Supported + features are: + + - assembled, non-symmetric, real matrices + - integer for pointer/indices + - exponential format for float values, and int format + + Examples + -------- + We can read and write a harwell-boeing format file: + + >>> from scipy.io import hb_read, hb_write + >>> from scipy.sparse import csr_array, eye + >>> data = csr_array(eye(3)) # create a sparse array + >>> hb_write("data.hb", data) # write a hb file + >>> print(hb_read("data.hb", spmatrix=False)) # read a hb file + + Coords Values + (0, 0) 1.0 + (1, 1) 1.0 + (2, 2) 1.0 + """ + m = m.tocsc(copy=False) + + if hb_info is None: + hb_info = HBInfo.from_data(m) + + def _set_matrix(fid): + hb = HBFile(fid, hb_info) + return hb.write_matrix(m) + + if hasattr(path_or_open_file, 'write'): + return _set_matrix(path_or_open_file) + else: + with open(path_or_open_file, 'w') as f: + return _set_matrix(f) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/_harwell_boeing/tests/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/_harwell_boeing/tests/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/_harwell_boeing/tests/test_fortran_format.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/_harwell_boeing/tests/test_fortran_format.py new file mode 100644 index 0000000000000000000000000000000000000000..dae040c523d6a6d618e89402d39a0cb05bad927a --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/_harwell_boeing/tests/test_fortran_format.py @@ -0,0 +1,74 @@ +import numpy as np + +from numpy.testing import assert_equal +from pytest import raises as assert_raises + +from scipy.io._harwell_boeing._fortran_format_parser import ( + FortranFormatParser, IntFormat, ExpFormat, BadFortranFormat) + + +class TestFortranFormatParser: + def setup_method(self): + self.parser = FortranFormatParser() + + def _test_equal(self, format, ref): + ret = self.parser.parse(format) + assert_equal(ret.__dict__, ref.__dict__) + + def test_simple_int(self): + self._test_equal("(I4)", IntFormat(4)) + + def test_simple_repeated_int(self): + self._test_equal("(3I4)", IntFormat(4, repeat=3)) + + def test_simple_exp(self): + self._test_equal("(E4.3)", ExpFormat(4, 3)) + + def test_exp_exp(self): + self._test_equal("(E8.3E3)", ExpFormat(8, 3, 3)) + + def test_repeat_exp(self): + self._test_equal("(2E4.3)", ExpFormat(4, 3, repeat=2)) + + def test_repeat_exp_exp(self): + self._test_equal("(2E8.3E3)", ExpFormat(8, 3, 3, repeat=2)) + + def test_wrong_formats(self): + def _test_invalid(bad_format): + assert_raises(BadFortranFormat, lambda: self.parser.parse(bad_format)) + _test_invalid("I4") + _test_invalid("(E4)") + _test_invalid("(E4.)") + _test_invalid("(E4.E3)") + + +class TestIntFormat: + def test_to_fortran(self): + f = [IntFormat(10), IntFormat(12, 10), IntFormat(12, 10, 3)] + res = ["(I10)", "(I12.10)", "(3I12.10)"] + + for i, j in zip(f, res): + assert_equal(i.fortran_format, j) + + def test_from_number(self): + f = [10, -12, 123456789] + r_f = [IntFormat(3, repeat=26), IntFormat(4, repeat=20), + IntFormat(10, repeat=8)] + for i, j in zip(f, r_f): + assert_equal(IntFormat.from_number(i).__dict__, j.__dict__) + + +class TestExpFormat: + def test_to_fortran(self): + f = [ExpFormat(10, 5), ExpFormat(12, 10), ExpFormat(12, 10, min=3), + ExpFormat(10, 5, repeat=3)] + res = ["(E10.5)", "(E12.10)", "(E12.10E3)", "(3E10.5)"] + + for i, j in zip(f, res): + assert_equal(i.fortran_format, j) + + def test_from_number(self): + f = np.array([1.0, -1.2]) + r_f = [ExpFormat(24, 16, repeat=3), ExpFormat(25, 16, repeat=3)] + for i, j in zip(f, r_f): + assert_equal(ExpFormat.from_number(i).__dict__, j.__dict__) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/_harwell_boeing/tests/test_hb.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/_harwell_boeing/tests/test_hb.py new file mode 100644 index 0000000000000000000000000000000000000000..d0c9ee5635fdd45c0f6560e686c9e40176ef95e4 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/_harwell_boeing/tests/test_hb.py @@ -0,0 +1,70 @@ +from io import StringIO +import tempfile + +import numpy as np + +from numpy.testing import assert_equal, \ + assert_array_almost_equal_nulp + +from scipy.sparse import coo_array, csc_array, random_array, isspmatrix + +from scipy.io import hb_read, hb_write + + +SIMPLE = """\ +No Title |No Key + 9 4 1 4 +RUA 100 100 10 0 +(26I3) (26I3) (3E23.15) +1 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 +3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 +3 3 3 3 3 3 3 4 4 4 6 6 6 6 6 6 6 6 6 6 6 8 9 9 9 9 +9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 11 +37 71 89 18 30 45 70 19 25 52 +2.971243799687726e-01 3.662366682877375e-01 4.786962174699534e-01 +6.490068647991184e-01 6.617490424831662e-02 8.870370343191623e-01 +4.196478590163001e-01 5.649603072111251e-01 9.934423887087086e-01 +6.912334991524289e-01 +""" + +SIMPLE_MATRIX = coo_array( + ((0.297124379969, 0.366236668288, 0.47869621747, 0.649006864799, + 0.0661749042483, 0.887037034319, 0.419647859016, + 0.564960307211, 0.993442388709, 0.691233499152,), + (np.array([[36, 70, 88, 17, 29, 44, 69, 18, 24, 51], + [0, 4, 58, 61, 61, 72, 72, 73, 99, 99]])))) + + +def assert_csc_almost_equal(r, l): + r = csc_array(r) + l = csc_array(l) + assert_equal(r.indptr, l.indptr) + assert_equal(r.indices, l.indices) + assert_array_almost_equal_nulp(r.data, l.data, 10000) + + +class TestHBReader: + def test_simple(self): + m = hb_read(StringIO(SIMPLE), spmatrix=False) + assert_csc_almost_equal(m, SIMPLE_MATRIX) + assert not isspmatrix(m) + m = hb_read(StringIO(SIMPLE), spmatrix=True) + assert isspmatrix(m) + m = hb_read(StringIO(SIMPLE)) # default + assert isspmatrix(m) + + +class TestHBReadWrite: + + def check_save_load(self, value): + with tempfile.NamedTemporaryFile(mode='w+t') as file: + hb_write(file, value) + file.file.seek(0) + value_loaded = hb_read(file, spmatrix=False) + assert_csc_almost_equal(value, value_loaded) + + def test_simple(self): + random_arr = random_array((10, 100), density=0.1) + for format in ('coo', 'csc', 'csr', 'bsr', 'dia', 'dok', 'lil'): + arr = random_arr.asformat(format, copy=False) + self.check_save_load(arr) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/_idl.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/_idl.py new file mode 100644 index 0000000000000000000000000000000000000000..5730a9d4fe1beda5a71aa668bda13d17f2fc2436 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/_idl.py @@ -0,0 +1,919 @@ +# IDLSave - a python module to read IDL 'save' files +# Copyright (c) 2010 Thomas P. Robitaille + +# Many thanks to Craig Markwardt for publishing the Unofficial Format +# Specification for IDL .sav files, without which this Python module would not +# exist (http://cow.physics.wisc.edu/~craigm/idl/savefmt). + +# This code was developed by with permission from ITT Visual Information +# Systems. IDL(r) is a registered trademark of ITT Visual Information Systems, +# Inc. for their Interactive Data Language software. + +# Permission is hereby granted, free of charge, to any person obtaining a +# copy of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following conditions: + +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. + +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +# DEALINGS IN THE SOFTWARE. + +__all__ = ['readsav'] + +import struct +import numpy as np +import tempfile +import zlib +import warnings + +# Define the different data types that can be found in an IDL save file +DTYPE_DICT = {1: '>u1', + 2: '>i2', + 3: '>i4', + 4: '>f4', + 5: '>f8', + 6: '>c8', + 7: '|O', + 8: '|O', + 9: '>c16', + 10: '|O', + 11: '|O', + 12: '>u2', + 13: '>u4', + 14: '>i8', + 15: '>u8'} + +# Define the different record types that can be found in an IDL save file +RECTYPE_DICT = {0: "START_MARKER", + 1: "COMMON_VARIABLE", + 2: "VARIABLE", + 3: "SYSTEM_VARIABLE", + 6: "END_MARKER", + 10: "TIMESTAMP", + 12: "COMPILED", + 13: "IDENTIFICATION", + 14: "VERSION", + 15: "HEAP_HEADER", + 16: "HEAP_DATA", + 17: "PROMOTE64", + 19: "NOTICE", + 20: "DESCRIPTION"} + +# Define a dictionary to contain structure definitions +STRUCT_DICT = {} + + +def _align_32(f): + '''Align to the next 32-bit position in a file''' + + pos = f.tell() + if pos % 4 != 0: + f.seek(pos + 4 - pos % 4) + return + + +def _skip_bytes(f, n): + '''Skip `n` bytes''' + f.read(n) + return + + +def _read_bytes(f, n): + '''Read the next `n` bytes''' + return f.read(n) + + +def _read_byte(f): + '''Read a single byte''' + return np.uint8(struct.unpack('>B', f.read(4)[:1])[0]) + + +def _read_long(f): + '''Read a signed 32-bit integer''' + return np.int32(struct.unpack('>l', f.read(4))[0]) + + +def _read_int16(f): + '''Read a signed 16-bit integer''' + return np.int16(struct.unpack('>h', f.read(4)[2:4])[0]) + + +def _read_int32(f): + '''Read a signed 32-bit integer''' + return np.int32(struct.unpack('>i', f.read(4))[0]) + + +def _read_int64(f): + '''Read a signed 64-bit integer''' + return np.int64(struct.unpack('>q', f.read(8))[0]) + + +def _read_uint16(f): + '''Read an unsigned 16-bit integer''' + return np.uint16(struct.unpack('>H', f.read(4)[2:4])[0]) + + +def _read_uint32(f): + '''Read an unsigned 32-bit integer''' + return np.uint32(struct.unpack('>I', f.read(4))[0]) + + +def _read_uint64(f): + '''Read an unsigned 64-bit integer''' + return np.uint64(struct.unpack('>Q', f.read(8))[0]) + + +def _read_float32(f): + '''Read a 32-bit float''' + return np.float32(struct.unpack('>f', f.read(4))[0]) + + +def _read_float64(f): + '''Read a 64-bit float''' + return np.float64(struct.unpack('>d', f.read(8))[0]) + + +class Pointer: + '''Class used to define pointers''' + + def __init__(self, index): + self.index = index + return + + +class ObjectPointer(Pointer): + '''Class used to define object pointers''' + pass + + +def _read_string(f): + '''Read a string''' + length = _read_long(f) + if length > 0: + chars = _read_bytes(f, length).decode('latin1') + _align_32(f) + else: + chars = '' + return chars + + +def _read_string_data(f): + '''Read a data string (length is specified twice)''' + length = _read_long(f) + if length > 0: + length = _read_long(f) + string_data = _read_bytes(f, length) + _align_32(f) + else: + string_data = '' + return string_data + + +def _read_data(f, dtype): + '''Read a variable with a specified data type''' + if dtype == 1: + if _read_int32(f) != 1: + raise Exception("Error occurred while reading byte variable") + return _read_byte(f) + elif dtype == 2: + return _read_int16(f) + elif dtype == 3: + return _read_int32(f) + elif dtype == 4: + return _read_float32(f) + elif dtype == 5: + return _read_float64(f) + elif dtype == 6: + real = _read_float32(f) + imag = _read_float32(f) + return np.complex64(real + imag * 1j) + elif dtype == 7: + return _read_string_data(f) + elif dtype == 8: + raise Exception("Should not be here - please report this") + elif dtype == 9: + real = _read_float64(f) + imag = _read_float64(f) + return np.complex128(real + imag * 1j) + elif dtype == 10: + return Pointer(_read_int32(f)) + elif dtype == 11: + return ObjectPointer(_read_int32(f)) + elif dtype == 12: + return _read_uint16(f) + elif dtype == 13: + return _read_uint32(f) + elif dtype == 14: + return _read_int64(f) + elif dtype == 15: + return _read_uint64(f) + else: + raise Exception("Unknown IDL type: %i - please report this" % dtype) + + +def _read_structure(f, array_desc, struct_desc): + ''' + Read a structure, with the array and structure descriptors given as + `array_desc` and `structure_desc` respectively. + ''' + + nrows = array_desc['nelements'] + columns = struct_desc['tagtable'] + + dtype = [] + for col in columns: + if col['structure'] or col['array']: + dtype.append(((col['name'].lower(), col['name']), np.object_)) + else: + if col['typecode'] in DTYPE_DICT: + dtype.append(((col['name'].lower(), col['name']), + DTYPE_DICT[col['typecode']])) + else: + raise Exception("Variable type %i not implemented" % + col['typecode']) + + structure = np.rec.recarray((nrows, ), dtype=dtype) + + for i in range(nrows): + for col in columns: + dtype = col['typecode'] + if col['structure']: + structure[col['name']][i] = _read_structure(f, + struct_desc['arrtable'][col['name']], + struct_desc['structtable'][col['name']]) + elif col['array']: + structure[col['name']][i] = _read_array(f, dtype, + struct_desc['arrtable'][col['name']]) + else: + structure[col['name']][i] = _read_data(f, dtype) + + # Reshape structure if needed + if array_desc['ndims'] > 1: + dims = array_desc['dims'][:int(array_desc['ndims'])] + dims.reverse() + structure = structure.reshape(dims) + + return structure + + +def _read_array(f, typecode, array_desc): + ''' + Read an array of type `typecode`, with the array descriptor given as + `array_desc`. + ''' + + if typecode in [1, 3, 4, 5, 6, 9, 13, 14, 15]: + + if typecode == 1: + nbytes = _read_int32(f) + if nbytes != array_desc['nbytes']: + warnings.warn("Not able to verify number of bytes from header", + stacklevel=3) + + # Read bytes as numpy array + array = np.frombuffer(f.read(array_desc['nbytes']), + dtype=DTYPE_DICT[typecode]) + + elif typecode in [2, 12]: + + # These are 2 byte types, need to skip every two as they are not packed + + array = np.frombuffer(f.read(array_desc['nbytes']*2), + dtype=DTYPE_DICT[typecode])[1::2] + + else: + + # Read bytes into list + array = [] + for i in range(array_desc['nelements']): + dtype = typecode + data = _read_data(f, dtype) + array.append(data) + + array = np.array(array, dtype=np.object_) + + # Reshape array if needed + if array_desc['ndims'] > 1: + dims = array_desc['dims'][:int(array_desc['ndims'])] + dims.reverse() + array = array.reshape(dims) + + # Go to next alignment position + _align_32(f) + + return array + + +def _read_record(f): + '''Function to read in a full record''' + + record = {'rectype': _read_long(f)} + + nextrec = _read_uint32(f) + nextrec += _read_uint32(f).astype(np.int64) * 2**32 + + _skip_bytes(f, 4) + + if record['rectype'] not in RECTYPE_DICT: + raise Exception("Unknown RECTYPE: %i" % record['rectype']) + + record['rectype'] = RECTYPE_DICT[record['rectype']] + + if record['rectype'] in ["VARIABLE", "HEAP_DATA"]: + + if record['rectype'] == "VARIABLE": + record['varname'] = _read_string(f) + else: + record['heap_index'] = _read_long(f) + _skip_bytes(f, 4) + + rectypedesc = _read_typedesc(f) + + if rectypedesc['typecode'] == 0: + + if nextrec == f.tell(): + record['data'] = None # Indicates NULL value + else: + raise ValueError("Unexpected type code: 0") + + else: + + varstart = _read_long(f) + if varstart != 7: + raise Exception("VARSTART is not 7") + + if rectypedesc['structure']: + record['data'] = _read_structure(f, rectypedesc['array_desc'], + rectypedesc['struct_desc']) + elif rectypedesc['array']: + record['data'] = _read_array(f, rectypedesc['typecode'], + rectypedesc['array_desc']) + else: + dtype = rectypedesc['typecode'] + record['data'] = _read_data(f, dtype) + + elif record['rectype'] == "TIMESTAMP": + + _skip_bytes(f, 4*256) + record['date'] = _read_string(f) + record['user'] = _read_string(f) + record['host'] = _read_string(f) + + elif record['rectype'] == "VERSION": + + record['format'] = _read_long(f) + record['arch'] = _read_string(f) + record['os'] = _read_string(f) + record['release'] = _read_string(f) + + elif record['rectype'] == "IDENTIFICATON": + + record['author'] = _read_string(f) + record['title'] = _read_string(f) + record['idcode'] = _read_string(f) + + elif record['rectype'] == "NOTICE": + + record['notice'] = _read_string(f) + + elif record['rectype'] == "DESCRIPTION": + + record['description'] = _read_string_data(f) + + elif record['rectype'] == "HEAP_HEADER": + + record['nvalues'] = _read_long(f) + record['indices'] = [_read_long(f) for _ in range(record['nvalues'])] + + elif record['rectype'] == "COMMONBLOCK": + + record['nvars'] = _read_long(f) + record['name'] = _read_string(f) + record['varnames'] = [_read_string(f) for _ in range(record['nvars'])] + + elif record['rectype'] == "END_MARKER": + + record['end'] = True + + elif record['rectype'] == "UNKNOWN": + + warnings.warn("Skipping UNKNOWN record", stacklevel=3) + + elif record['rectype'] == "SYSTEM_VARIABLE": + + warnings.warn("Skipping SYSTEM_VARIABLE record", stacklevel=3) + + else: + + raise Exception(f"record['rectype']={record['rectype']} not implemented") + + f.seek(nextrec) + + return record + + +def _read_typedesc(f): + '''Function to read in a type descriptor''' + + typedesc = {'typecode': _read_long(f), 'varflags': _read_long(f)} + + if typedesc['varflags'] & 2 == 2: + raise Exception("System variables not implemented") + + typedesc['array'] = typedesc['varflags'] & 4 == 4 + typedesc['structure'] = typedesc['varflags'] & 32 == 32 + + if typedesc['structure']: + typedesc['array_desc'] = _read_arraydesc(f) + typedesc['struct_desc'] = _read_structdesc(f) + elif typedesc['array']: + typedesc['array_desc'] = _read_arraydesc(f) + + return typedesc + + +def _read_arraydesc(f): + '''Function to read in an array descriptor''' + + arraydesc = {'arrstart': _read_long(f)} + + if arraydesc['arrstart'] == 8: + + _skip_bytes(f, 4) + + arraydesc['nbytes'] = _read_long(f) + arraydesc['nelements'] = _read_long(f) + arraydesc['ndims'] = _read_long(f) + + _skip_bytes(f, 8) + + arraydesc['nmax'] = _read_long(f) + + arraydesc['dims'] = [_read_long(f) for _ in range(arraydesc['nmax'])] + + elif arraydesc['arrstart'] == 18: + + warnings.warn("Using experimental 64-bit array read", stacklevel=3) + + _skip_bytes(f, 8) + + arraydesc['nbytes'] = _read_uint64(f) + arraydesc['nelements'] = _read_uint64(f) + arraydesc['ndims'] = _read_long(f) + + _skip_bytes(f, 8) + + arraydesc['nmax'] = 8 + + arraydesc['dims'] = [] + for d in range(arraydesc['nmax']): + v = _read_long(f) + if v != 0: + raise Exception("Expected a zero in ARRAY_DESC") + arraydesc['dims'].append(_read_long(f)) + + else: + + raise Exception("Unknown ARRSTART: %i" % arraydesc['arrstart']) + + return arraydesc + + +def _read_structdesc(f): + '''Function to read in a structure descriptor''' + + structdesc = {} + + structstart = _read_long(f) + if structstart != 9: + raise Exception("STRUCTSTART should be 9") + + structdesc['name'] = _read_string(f) + predef = _read_long(f) + structdesc['ntags'] = _read_long(f) + structdesc['nbytes'] = _read_long(f) + + structdesc['predef'] = predef & 1 + structdesc['inherits'] = predef & 2 + structdesc['is_super'] = predef & 4 + + if not structdesc['predef']: + + structdesc['tagtable'] = [_read_tagdesc(f) + for _ in range(structdesc['ntags'])] + + for tag in structdesc['tagtable']: + tag['name'] = _read_string(f) + + structdesc['arrtable'] = {tag['name']: _read_arraydesc(f) + for tag in structdesc['tagtable'] + if tag['array']} + + structdesc['structtable'] = {tag['name']: _read_structdesc(f) + for tag in structdesc['tagtable'] + if tag['structure']} + + if structdesc['inherits'] or structdesc['is_super']: + structdesc['classname'] = _read_string(f) + structdesc['nsupclasses'] = _read_long(f) + structdesc['supclassnames'] = [ + _read_string(f) for _ in range(structdesc['nsupclasses'])] + structdesc['supclasstable'] = [ + _read_structdesc(f) for _ in range(structdesc['nsupclasses'])] + + STRUCT_DICT[structdesc['name']] = structdesc + + else: + + if structdesc['name'] not in STRUCT_DICT: + raise Exception("PREDEF=1 but can't find definition") + + structdesc = STRUCT_DICT[structdesc['name']] + + return structdesc + + +def _read_tagdesc(f): + '''Function to read in a tag descriptor''' + + tagdesc = {'offset': _read_long(f)} + + if tagdesc['offset'] == -1: + tagdesc['offset'] = _read_uint64(f) + + tagdesc['typecode'] = _read_long(f) + tagflags = _read_long(f) + + tagdesc['array'] = tagflags & 4 == 4 + tagdesc['structure'] = tagflags & 32 == 32 + tagdesc['scalar'] = tagdesc['typecode'] in DTYPE_DICT + # Assume '10'x is scalar + + return tagdesc + + +def _replace_heap(variable, heap): + + if isinstance(variable, Pointer): + + while isinstance(variable, Pointer): + + if variable.index == 0: + variable = None + else: + if variable.index in heap: + variable = heap[variable.index] + else: + warnings.warn("Variable referenced by pointer not found " + "in heap: variable will be set to None", + stacklevel=3) + variable = None + + replace, new = _replace_heap(variable, heap) + + if replace: + variable = new + + return True, variable + + elif isinstance(variable, np.rec.recarray): + + # Loop over records + for ir, record in enumerate(variable): + + replace, new = _replace_heap(record, heap) + + if replace: + variable[ir] = new + + return False, variable + + elif isinstance(variable, np.record): + + # Loop over values + for iv, value in enumerate(variable): + + replace, new = _replace_heap(value, heap) + + if replace: + variable[iv] = new + + return False, variable + + elif isinstance(variable, np.ndarray): + + # Loop over values if type is np.object_ + if variable.dtype.type is np.object_: + + for iv in range(variable.size): + + replace, new = _replace_heap(variable.item(iv), heap) + + if replace: + variable.reshape(-1)[iv] = new + + return False, variable + + else: + + return False, variable + + +class AttrDict(dict): + ''' + A case-insensitive dictionary with access via item, attribute, and call + notations: + + >>> from scipy.io._idl import AttrDict + >>> d = AttrDict() + >>> d['Variable'] = 123 + >>> d['Variable'] + 123 + >>> d.Variable + 123 + >>> d.variable + 123 + >>> d('VARIABLE') + 123 + >>> d['missing'] + Traceback (most recent error last): + ... + KeyError: 'missing' + >>> d.missing + Traceback (most recent error last): + ... + AttributeError: 'AttrDict' object has no attribute 'missing' + ''' + + def __init__(self, init=None): + if init is None: + init = {} + dict.__init__(self, init) + + def __getitem__(self, name): + return super().__getitem__(name.lower()) + + def __setitem__(self, key, value): + return super().__setitem__(key.lower(), value) + + def __getattr__(self, name): + try: + return self.__getitem__(name) + except KeyError: + raise AttributeError( + f"'{type(self)}' object has no attribute '{name}'") from None + + __setattr__ = __setitem__ + __call__ = __getitem__ + + +def readsav(file_name, idict=None, python_dict=False, + uncompressed_file_name=None, verbose=False): + """ + Read an IDL .sav file. + + Parameters + ---------- + file_name : str + Name of the IDL save file. + idict : dict, optional + Dictionary in which to insert .sav file variables. + python_dict : bool, optional + By default, the object return is not a Python dictionary, but a + case-insensitive dictionary with item, attribute, and call access + to variables. To get a standard Python dictionary, set this option + to True. + uncompressed_file_name : str, optional + This option only has an effect for .sav files written with the + /compress option. If a file name is specified, compressed .sav + files are uncompressed to this file. Otherwise, readsav will use + the `tempfile` module to determine a temporary filename + automatically, and will remove the temporary file upon successfully + reading it in. + verbose : bool, optional + Whether to print out information about the save file, including + the records read, and available variables. + + Returns + ------- + idl_dict : AttrDict or dict + If `python_dict` is set to False (default), this function returns a + case-insensitive dictionary with item, attribute, and call access + to variables. If `python_dict` is set to True, this function + returns a Python dictionary with all variable names in lowercase. + If `idict` was specified, then variables are written to the + dictionary specified, and the updated dictionary is returned. + + Examples + -------- + >>> from os.path import dirname, join as pjoin + >>> import scipy.io as sio + >>> from scipy.io import readsav + + Get the filename for an example .sav file from the tests/data directory. + + >>> data_dir = pjoin(dirname(sio.__file__), 'tests', 'data') + >>> sav_fname = pjoin(data_dir, 'array_float32_1d.sav') + + Load the .sav file contents. + + >>> sav_data = readsav(sav_fname) + + Get keys of the .sav file contents. + + >>> print(sav_data.keys()) + dict_keys(['array1d']) + + Access a content with a key. + + >>> print(sav_data['array1d']) + [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. + 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. + 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. + 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. + 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. + 0. 0. 0.] + + """ + + # Initialize record and variable holders + records = [] + if python_dict or idict: + variables = {} + else: + variables = AttrDict() + + # Open the IDL file + f = open(file_name, 'rb') + + # Read the signature, which should be 'SR' + signature = _read_bytes(f, 2) + if signature != b'SR': + raise Exception(f"Invalid SIGNATURE: {signature}") + + # Next, the record format, which is '\x00\x04' for normal .sav + # files, and '\x00\x06' for compressed .sav files. + recfmt = _read_bytes(f, 2) + + if recfmt == b'\x00\x04': + pass + + elif recfmt == b'\x00\x06': + + if verbose: + print("IDL Save file is compressed") + + if uncompressed_file_name: + fout = open(uncompressed_file_name, 'w+b') + else: + fout = tempfile.NamedTemporaryFile(suffix='.sav') + + if verbose: + print(f" -> expanding to {fout.name}") + + # Write header + fout.write(b'SR\x00\x04') + + # Cycle through records + while True: + + # Read record type + rectype = _read_long(f) + fout.write(struct.pack('>l', int(rectype))) + + # Read position of next record and return as int + nextrec = _read_uint32(f) + nextrec += _read_uint32(f).astype(np.int64) * 2**32 + + # Read the unknown 4 bytes + unknown = f.read(4) + + # Check if the end of the file has been reached + if RECTYPE_DICT[rectype] == 'END_MARKER': + modval = np.int64(2**32) + fout.write(struct.pack('>I', int(nextrec) % modval)) + fout.write( + struct.pack('>I', int((nextrec - (nextrec % modval)) / modval)) + ) + fout.write(unknown) + break + + # Find current position + pos = f.tell() + + # Decompress record + rec_string = zlib.decompress(f.read(nextrec-pos)) + + # Find new position of next record + nextrec = fout.tell() + len(rec_string) + 12 + + # Write out record + fout.write(struct.pack('>I', int(nextrec % 2**32))) + fout.write(struct.pack('>I', int((nextrec - (nextrec % 2**32)) / 2**32))) + fout.write(unknown) + fout.write(rec_string) + + # Close the original compressed file + f.close() + + # Set f to be the decompressed file, and skip the first four bytes + f = fout + f.seek(4) + + else: + raise Exception(f"Invalid RECFMT: {recfmt}") + + # Loop through records, and add them to the list + while True: + r = _read_record(f) + records.append(r) + if 'end' in r: + if r['end']: + break + + # Close the file + f.close() + + # Find heap data variables + heap = {} + for r in records: + if r['rectype'] == "HEAP_DATA": + heap[r['heap_index']] = r['data'] + + # Find all variables + for r in records: + if r['rectype'] == "VARIABLE": + replace, new = _replace_heap(r['data'], heap) + if replace: + r['data'] = new + variables[r['varname'].lower()] = r['data'] + + if verbose: + + # Print out timestamp info about the file + for record in records: + if record['rectype'] == "TIMESTAMP": + print("-"*50) + print(f"Date: {record['date']}") + print(f"User: {record['user']}") + print(f"Host: {record['host']}") + break + + # Print out version info about the file + for record in records: + if record['rectype'] == "VERSION": + print("-"*50) + print(f"Format: {record['format']}") + print(f"Architecture: {record['arch']}") + print(f"Operating System: {record['os']}") + print(f"IDL Version: {record['release']}") + break + + # Print out identification info about the file + for record in records: + if record['rectype'] == "IDENTIFICATON": + print("-"*50) + print(f"Author: {record['author']}") + print(f"Title: {record['title']}") + print(f"ID Code: {record['idcode']}") + break + + # Print out descriptions saved with the file + for record in records: + if record['rectype'] == "DESCRIPTION": + print("-"*50) + print(f"Description: {record['description']}") + break + + print("-"*50) + print(f"Successfully read {len(records)} records of which:") + + # Create convenience list of record types + rectypes = [r['rectype'] for r in records] + + for rt in set(rectypes): + if rt != 'END_MARKER': + print(" - %i are of type %s" % (rectypes.count(rt), rt)) + print("-"*50) + + if 'VARIABLE' in rectypes: + print("Available variables:") + for var in variables: + print(f" - {var} [{type(variables[var])}]") + print("-"*50) + + if idict: + for var in variables: + idict[var] = variables[var] + return idict + else: + return variables diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/_mmio.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/_mmio.py new file mode 100644 index 0000000000000000000000000000000000000000..32db20065d632d582f04addcf766daa4e6b5fd8e --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/_mmio.py @@ -0,0 +1,968 @@ +""" + Matrix Market I/O in Python. + See http://math.nist.gov/MatrixMarket/formats.html + for information about the Matrix Market format. +""" +# +# Author: Pearu Peterson +# Created: October, 2004 +# +# References: +# http://math.nist.gov/MatrixMarket/ +# +import os + +import numpy as np +from numpy import (asarray, real, imag, conj, zeros, ndarray, concatenate, + ones, can_cast) + +from scipy.sparse import coo_array, issparse, coo_matrix + +__all__ = ['mminfo', 'mmread', 'mmwrite', 'MMFile'] + + +# ----------------------------------------------------------------------------- +def asstr(s): + if isinstance(s, bytes): + return s.decode('latin1') + return str(s) + + +def mminfo(source): + """ + Return size and storage parameters from Matrix Market file-like 'source'. + + Parameters + ---------- + source : str or file-like + Matrix Market filename (extension .mtx) or open file-like object + + Returns + ------- + rows : int + Number of matrix rows. + cols : int + Number of matrix columns. + entries : int + Number of non-zero entries of a sparse matrix + or rows*cols for a dense matrix. + format : str + Either 'coordinate' or 'array'. + field : str + Either 'real', 'complex', 'pattern', or 'integer'. + symmetry : str + Either 'general', 'symmetric', 'skew-symmetric', or 'hermitian'. + + Examples + -------- + >>> from io import StringIO + >>> from scipy.io import mminfo + + >>> text = '''%%MatrixMarket matrix coordinate real general + ... 5 5 7 + ... 2 3 1.0 + ... 3 4 2.0 + ... 3 5 3.0 + ... 4 1 4.0 + ... 4 2 5.0 + ... 4 3 6.0 + ... 4 4 7.0 + ... ''' + + + ``mminfo(source)`` returns the number of rows, number of columns, + format, field type and symmetry attribute of the source file. + + >>> mminfo(StringIO(text)) + (5, 5, 7, 'coordinate', 'real', 'general') + """ + return MMFile.info(source) + +# ----------------------------------------------------------------------------- + + +def mmread(source, *, spmatrix=True): + """ + Reads the contents of a Matrix Market file-like 'source' into a matrix. + + Parameters + ---------- + source : str or file-like + Matrix Market filename (extensions .mtx, .mtz.gz) + or open file-like object. + spmatrix : bool, optional (default: True) + If ``True``, return sparse ``coo_matrix``. Otherwise return ``coo_array``. + + Returns + ------- + a : ndarray or coo_array or coo_matrix + Dense or sparse array depending on the matrix format in the + Matrix Market file. + + Examples + -------- + >>> from io import StringIO + >>> from scipy.io import mmread + + >>> text = '''%%MatrixMarket matrix coordinate real general + ... 5 5 7 + ... 2 3 1.0 + ... 3 4 2.0 + ... 3 5 3.0 + ... 4 1 4.0 + ... 4 2 5.0 + ... 4 3 6.0 + ... 4 4 7.0 + ... ''' + + ``mmread(source)`` returns the data as sparse matrix in COO format. + + >>> m = mmread(StringIO(text), spmatrix=False) + >>> m + + >>> m.toarray() + array([[0., 0., 0., 0., 0.], + [0., 0., 1., 0., 0.], + [0., 0., 0., 2., 3.], + [4., 5., 6., 7., 0.], + [0., 0., 0., 0., 0.]]) + """ + return MMFile().read(source, spmatrix=spmatrix) + +# ----------------------------------------------------------------------------- + + +def mmwrite(target, a, comment='', field=None, precision=None, symmetry=None): + r""" + Writes the sparse or dense array `a` to Matrix Market file-like `target`. + + Parameters + ---------- + target : str or file-like + Matrix Market filename (extension .mtx) or open file-like object. + a : array like + Sparse or dense 2-D array. + comment : str, optional + Comments to be prepended to the Matrix Market file. + field : None or str, optional + Either 'real', 'complex', 'pattern', or 'integer'. + precision : None or int, optional + Number of digits to display for real or complex values. + symmetry : None or str, optional + Either 'general', 'symmetric', 'skew-symmetric', or 'hermitian'. + If symmetry is None the symmetry type of 'a' is determined by its + values. + + Returns + ------- + None + + Examples + -------- + >>> from io import BytesIO + >>> import numpy as np + >>> from scipy.sparse import coo_array + >>> from scipy.io import mmwrite + + Write a small NumPy array to a matrix market file. The file will be + written in the ``'array'`` format. + + >>> a = np.array([[1.0, 0, 0, 0], [0, 2.5, 0, 6.25]]) + >>> target = BytesIO() + >>> mmwrite(target, a) + >>> print(target.getvalue().decode('latin1')) + %%MatrixMarket matrix array real general + % + 2 4 + 1 + 0 + 0 + 2.5 + 0 + 0 + 0 + 6.25 + + Add a comment to the output file, and set the precision to 3. + + >>> target = BytesIO() + >>> mmwrite(target, a, comment='\n Some test data.\n', precision=3) + >>> print(target.getvalue().decode('latin1')) + %%MatrixMarket matrix array real general + % + % Some test data. + % + 2 4 + 1.00e+00 + 0.00e+00 + 0.00e+00 + 2.50e+00 + 0.00e+00 + 0.00e+00 + 0.00e+00 + 6.25e+00 + + Convert to a sparse matrix before calling ``mmwrite``. This will + result in the output format being ``'coordinate'`` rather than + ``'array'``. + + >>> target = BytesIO() + >>> mmwrite(target, coo_array(a), precision=3) + >>> print(target.getvalue().decode('latin1')) + %%MatrixMarket matrix coordinate real general + % + 2 4 3 + 1 1 1.00e+00 + 2 2 2.50e+00 + 2 4 6.25e+00 + + Write a complex Hermitian array to a matrix market file. Note that + only six values are actually written to the file; the other values + are implied by the symmetry. + + >>> z = np.array([[3, 1+2j, 4-3j], [1-2j, 1, -5j], [4+3j, 5j, 2.5]]) + >>> z + array([[ 3. +0.j, 1. +2.j, 4. -3.j], + [ 1. -2.j, 1. +0.j, -0. -5.j], + [ 4. +3.j, 0. +5.j, 2.5+0.j]]) + + >>> target = BytesIO() + >>> mmwrite(target, z, precision=2) + >>> print(target.getvalue().decode('latin1')) + %%MatrixMarket matrix array complex hermitian + % + 3 3 + 3.0e+00 0.0e+00 + 1.0e+00 -2.0e+00 + 4.0e+00 3.0e+00 + 1.0e+00 0.0e+00 + 0.0e+00 5.0e+00 + 2.5e+00 0.0e+00 + + """ + MMFile().write(target, a, comment, field, precision, symmetry) + + +############################################################################### +class MMFile: + __slots__ = ('_rows', + '_cols', + '_entries', + '_format', + '_field', + '_symmetry') + + @property + def rows(self): + return self._rows + + @property + def cols(self): + return self._cols + + @property + def entries(self): + return self._entries + + @property + def format(self): + return self._format + + @property + def field(self): + return self._field + + @property + def symmetry(self): + return self._symmetry + + @property + def has_symmetry(self): + return self._symmetry in (self.SYMMETRY_SYMMETRIC, + self.SYMMETRY_SKEW_SYMMETRIC, + self.SYMMETRY_HERMITIAN) + + # format values + FORMAT_COORDINATE = 'coordinate' + FORMAT_ARRAY = 'array' + FORMAT_VALUES = (FORMAT_COORDINATE, FORMAT_ARRAY) + + @classmethod + def _validate_format(self, format): + if format not in self.FORMAT_VALUES: + msg = f'unknown format type {format}, must be one of {self.FORMAT_VALUES}' + raise ValueError(msg) + + # field values + FIELD_INTEGER = 'integer' + FIELD_UNSIGNED = 'unsigned-integer' + FIELD_REAL = 'real' + FIELD_COMPLEX = 'complex' + FIELD_PATTERN = 'pattern' + FIELD_VALUES = (FIELD_INTEGER, FIELD_UNSIGNED, FIELD_REAL, FIELD_COMPLEX, + FIELD_PATTERN) + + @classmethod + def _validate_field(self, field): + if field not in self.FIELD_VALUES: + msg = f'unknown field type {field}, must be one of {self.FIELD_VALUES}' + raise ValueError(msg) + + # symmetry values + SYMMETRY_GENERAL = 'general' + SYMMETRY_SYMMETRIC = 'symmetric' + SYMMETRY_SKEW_SYMMETRIC = 'skew-symmetric' + SYMMETRY_HERMITIAN = 'hermitian' + SYMMETRY_VALUES = (SYMMETRY_GENERAL, SYMMETRY_SYMMETRIC, + SYMMETRY_SKEW_SYMMETRIC, SYMMETRY_HERMITIAN) + + @classmethod + def _validate_symmetry(self, symmetry): + if symmetry not in self.SYMMETRY_VALUES: + raise ValueError(f'unknown symmetry type {symmetry}, ' + f'must be one of {self.SYMMETRY_VALUES}') + + DTYPES_BY_FIELD = {FIELD_INTEGER: 'intp', + FIELD_UNSIGNED: 'uint64', + FIELD_REAL: 'd', + FIELD_COMPLEX: 'D', + FIELD_PATTERN: 'd'} + + # ------------------------------------------------------------------------- + @staticmethod + def reader(): + pass + + # ------------------------------------------------------------------------- + @staticmethod + def writer(): + pass + + # ------------------------------------------------------------------------- + @classmethod + def info(self, source): + """ + Return size, storage parameters from Matrix Market file-like 'source'. + + Parameters + ---------- + source : str or file-like + Matrix Market filename (extension .mtx) or open file-like object + + Returns + ------- + rows : int + Number of matrix rows. + cols : int + Number of matrix columns. + entries : int + Number of non-zero entries of a sparse matrix + or rows*cols for a dense matrix. + format : str + Either 'coordinate' or 'array'. + field : str + Either 'real', 'complex', 'pattern', or 'integer'. + symmetry : str + Either 'general', 'symmetric', 'skew-symmetric', or 'hermitian'. + """ + + stream, close_it = self._open(source) + + try: + + # read and validate header line + line = stream.readline() + mmid, matrix, format, field, symmetry = \ + (asstr(part.strip()) for part in line.split()) + if not mmid.startswith('%%MatrixMarket'): + raise ValueError('source is not in Matrix Market format') + if not matrix.lower() == 'matrix': + raise ValueError("Problem reading file header: " + line) + + # http://math.nist.gov/MatrixMarket/formats.html + if format.lower() == 'array': + format = self.FORMAT_ARRAY + elif format.lower() == 'coordinate': + format = self.FORMAT_COORDINATE + + # skip comments + # line.startswith('%') + while line: + if line.lstrip() and line.lstrip()[0] in ['%', 37]: + line = stream.readline() + else: + break + + # skip empty lines + while not line.strip(): + line = stream.readline() + + split_line = line.split() + if format == self.FORMAT_ARRAY: + if not len(split_line) == 2: + raise ValueError("Header line not of length 2: " + + line.decode('ascii')) + rows, cols = map(int, split_line) + entries = rows * cols + else: + if not len(split_line) == 3: + raise ValueError("Header line not of length 3: " + + line.decode('ascii')) + rows, cols, entries = map(int, split_line) + + return (rows, cols, entries, format, field.lower(), + symmetry.lower()) + + finally: + if close_it: + stream.close() + + # ------------------------------------------------------------------------- + @staticmethod + def _open(filespec, mode='rb'): + """ Return an open file stream for reading based on source. + + If source is a file name, open it (after trying to find it with mtx and + gzipped mtx extensions). Otherwise, just return source. + + Parameters + ---------- + filespec : str or file-like + String giving file name or file-like object + mode : str, optional + Mode with which to open file, if `filespec` is a file name. + + Returns + ------- + fobj : file-like + Open file-like object. + close_it : bool + True if the calling function should close this file when done, + false otherwise. + """ + # If 'filespec' is path-like (str, pathlib.Path, os.DirEntry, other class + # implementing a '__fspath__' method), try to convert it to str. If this + # fails by throwing a 'TypeError', assume it's an open file handle and + # return it as-is. + try: + filespec = os.fspath(filespec) + except TypeError: + return filespec, False + + # 'filespec' is definitely a str now + + # open for reading + if mode[0] == 'r': + + # determine filename plus extension + if not os.path.isfile(filespec): + if os.path.isfile(filespec+'.mtx'): + filespec = filespec + '.mtx' + elif os.path.isfile(filespec+'.mtx.gz'): + filespec = filespec + '.mtx.gz' + elif os.path.isfile(filespec+'.mtx.bz2'): + filespec = filespec + '.mtx.bz2' + # open filename + if filespec.endswith('.gz'): + import gzip + stream = gzip.open(filespec, mode) + elif filespec.endswith('.bz2'): + import bz2 + stream = bz2.BZ2File(filespec, 'rb') + else: + stream = open(filespec, mode) + + # open for writing + else: + if filespec[-4:] != '.mtx': + filespec = filespec + '.mtx' + stream = open(filespec, mode) + + return stream, True + + # ------------------------------------------------------------------------- + @staticmethod + def _get_symmetry(a): + m, n = a.shape + if m != n: + return MMFile.SYMMETRY_GENERAL + issymm = True + isskew = True + isherm = a.dtype.char in 'FD' + + # sparse input + if issparse(a): + # check if number of nonzero entries of lower and upper triangle + # matrix are equal + a = a.tocoo() + (row, col) = a.nonzero() + if (row < col).sum() != (row > col).sum(): + return MMFile.SYMMETRY_GENERAL + + # define iterator over symmetric pair entries + a = a.todok() + + def symm_iterator(): + for ((i, j), aij) in a.items(): + if i > j: + aji = a[j, i] + yield (aij, aji, False) + elif i == j: + yield (aij, aij, True) + + # non-sparse input + else: + # define iterator over symmetric pair entries + def symm_iterator(): + for j in range(n): + for i in range(j, n): + aij, aji = a[i][j], a[j][i] + yield (aij, aji, i == j) + + # check for symmetry + # yields aij, aji, is_diagonal + for (aij, aji, is_diagonal) in symm_iterator(): + if isskew and is_diagonal and aij != 0: + isskew = False + else: + if issymm and aij != aji: + issymm = False + with np.errstate(over="ignore"): + # This can give a warning for uint dtypes, so silence that + if isskew and aij != -aji: + isskew = False + if isherm and aij != conj(aji): + isherm = False + if not (issymm or isskew or isherm): + break + + # return symmetry value + if issymm: + return MMFile.SYMMETRY_SYMMETRIC + if isskew: + return MMFile.SYMMETRY_SKEW_SYMMETRIC + if isherm: + return MMFile.SYMMETRY_HERMITIAN + return MMFile.SYMMETRY_GENERAL + + # ------------------------------------------------------------------------- + @staticmethod + def _field_template(field, precision): + return {MMFile.FIELD_REAL: '%%.%ie\n' % precision, + MMFile.FIELD_INTEGER: '%i\n', + MMFile.FIELD_UNSIGNED: '%u\n', + MMFile.FIELD_COMPLEX: '%%.%ie %%.%ie\n' % + (precision, precision) + }.get(field, None) + + # ------------------------------------------------------------------------- + def __init__(self, **kwargs): + self._init_attrs(**kwargs) + + # ------------------------------------------------------------------------- + def read(self, source, *, spmatrix=True): + """ + Reads the contents of a Matrix Market file-like 'source' into a matrix. + + Parameters + ---------- + source : str or file-like + Matrix Market filename (extensions .mtx, .mtz.gz) + or open file object. + spmatrix : bool, optional (default: True) + If ``True``, return sparse ``coo_matrix``. Otherwise return ``coo_array``. + + Returns + ------- + a : ndarray or coo_array or coo_matrix + Dense or sparse array depending on the matrix format in the + Matrix Market file. + """ + stream, close_it = self._open(source) + + try: + self._parse_header(stream) + data = self._parse_body(stream) + + finally: + if close_it: + stream.close() + if spmatrix and isinstance(data, coo_array): + data = coo_matrix(data) + return data + + + # ------------------------------------------------------------------------- + def write(self, target, a, comment='', field=None, precision=None, + symmetry=None): + """ + Writes sparse or dense array `a` to Matrix Market file-like `target`. + + Parameters + ---------- + target : str or file-like + Matrix Market filename (extension .mtx) or open file-like object. + a : array like + Sparse or dense 2-D array. + comment : str, optional + Comments to be prepended to the Matrix Market file. + field : None or str, optional + Either 'real', 'complex', 'pattern', or 'integer'. + precision : None or int, optional + Number of digits to display for real or complex values. + symmetry : None or str, optional + Either 'general', 'symmetric', 'skew-symmetric', or 'hermitian'. + If symmetry is None the symmetry type of 'a' is determined by its + values. + """ + + stream, close_it = self._open(target, 'wb') + + try: + self._write(stream, a, comment, field, precision, symmetry) + + finally: + if close_it: + stream.close() + else: + stream.flush() + + # ------------------------------------------------------------------------- + def _init_attrs(self, **kwargs): + """ + Initialize each attributes with the corresponding keyword arg value + or a default of None + """ + + attrs = self.__class__.__slots__ + public_attrs = [attr[1:] for attr in attrs] + invalid_keys = set(kwargs.keys()) - set(public_attrs) + + if invalid_keys: + raise ValueError(f"found {tuple(invalid_keys)} invalid keyword " + f"arguments, please only use {public_attrs}") + + for attr in attrs: + setattr(self, attr, kwargs.get(attr[1:], None)) + + # ------------------------------------------------------------------------- + def _parse_header(self, stream): + rows, cols, entries, format, field, symmetry = \ + self.__class__.info(stream) + self._init_attrs(rows=rows, cols=cols, entries=entries, format=format, + field=field, symmetry=symmetry) + + # ------------------------------------------------------------------------- + def _parse_body(self, stream): + rows, cols, entries, format, field, symm = (self.rows, self.cols, + self.entries, self.format, + self.field, self.symmetry) + + dtype = self.DTYPES_BY_FIELD.get(field, None) + + has_symmetry = self.has_symmetry + is_integer = field == self.FIELD_INTEGER + is_unsigned_integer = field == self.FIELD_UNSIGNED + is_complex = field == self.FIELD_COMPLEX + is_skew = symm == self.SYMMETRY_SKEW_SYMMETRIC + is_herm = symm == self.SYMMETRY_HERMITIAN + is_pattern = field == self.FIELD_PATTERN + + if format == self.FORMAT_ARRAY: + a = zeros((rows, cols), dtype=dtype) + line = 1 + i, j = 0, 0 + if is_skew: + a[i, j] = 0 + if i < rows - 1: + i += 1 + while line: + line = stream.readline() + # line.startswith('%') + if not line or line[0] in ['%', 37] or not line.strip(): + continue + if is_integer: + aij = int(line) + elif is_unsigned_integer: + aij = int(line) + elif is_complex: + aij = complex(*map(float, line.split())) + else: + aij = float(line) + a[i, j] = aij + if has_symmetry and i != j: + if is_skew: + a[j, i] = -aij + elif is_herm: + a[j, i] = conj(aij) + else: + a[j, i] = aij + if i < rows-1: + i = i + 1 + else: + j = j + 1 + if not has_symmetry: + i = 0 + else: + i = j + if is_skew: + a[i, j] = 0 + if i < rows-1: + i += 1 + + if is_skew: + if not (i in [0, j] and j == cols - 1): + raise ValueError("Parse error, did not read all lines.") + else: + if not (i in [0, j] and j == cols): + raise ValueError("Parse error, did not read all lines.") + + elif format == self.FORMAT_COORDINATE: + # Read sparse COOrdinate format + + if entries == 0: + # empty matrix + return coo_array((rows, cols), dtype=dtype) + + I = zeros(entries, dtype='intc') + J = zeros(entries, dtype='intc') + if is_pattern: + V = ones(entries, dtype='int8') + elif is_integer: + V = zeros(entries, dtype='intp') + elif is_unsigned_integer: + V = zeros(entries, dtype='uint64') + elif is_complex: + V = zeros(entries, dtype='complex') + else: + V = zeros(entries, dtype='float') + + entry_number = 0 + for line in stream: + # line.startswith('%') + if not line or line[0] in ['%', 37] or not line.strip(): + continue + + if entry_number+1 > entries: + raise ValueError("'entries' in header is smaller than " + "number of entries") + l = line.split() + I[entry_number], J[entry_number] = map(int, l[:2]) + + if not is_pattern: + if is_integer: + V[entry_number] = int(l[2]) + elif is_unsigned_integer: + V[entry_number] = int(l[2]) + elif is_complex: + V[entry_number] = complex(*map(float, l[2:])) + else: + V[entry_number] = float(l[2]) + entry_number += 1 + if entry_number < entries: + raise ValueError("'entries' in header is larger than " + "number of entries") + + I -= 1 # adjust indices (base 1 -> base 0) + J -= 1 + + if has_symmetry: + mask = (I != J) # off diagonal mask + od_I = I[mask] + od_J = J[mask] + od_V = V[mask] + + I = concatenate((I, od_J)) + J = concatenate((J, od_I)) + + if is_skew: + od_V *= -1 + elif is_herm: + od_V = od_V.conjugate() + + V = concatenate((V, od_V)) + + a = coo_array((V, (I, J)), shape=(rows, cols), dtype=dtype) + else: + raise NotImplementedError(format) + + return a + + # ------------------------------------------------------------------------ + def _write(self, stream, a, comment='', field=None, precision=None, + symmetry=None): + if isinstance(a, list) or isinstance(a, ndarray) or \ + isinstance(a, tuple) or hasattr(a, '__array__'): + rep = self.FORMAT_ARRAY + a = asarray(a) + if len(a.shape) != 2: + raise ValueError('Expected 2 dimensional array') + rows, cols = a.shape + + if field is not None: + + if field == self.FIELD_INTEGER: + if not can_cast(a.dtype, 'intp'): + raise OverflowError("mmwrite does not support integer " + "dtypes larger than native 'intp'.") + a = a.astype('intp') + elif field == self.FIELD_REAL: + if a.dtype.char not in 'fd': + a = a.astype('d') + elif field == self.FIELD_COMPLEX: + if a.dtype.char not in 'FD': + a = a.astype('D') + + else: + if not issparse(a): + raise ValueError(f'unknown matrix type: {type(a)}') + + rep = 'coordinate' + rows, cols = a.shape + + typecode = a.dtype.char + + if precision is None: + if typecode in 'fF': + precision = 8 + else: + precision = 16 + if field is None: + kind = a.dtype.kind + if kind == 'i': + if not can_cast(a.dtype, 'intp'): + raise OverflowError("mmwrite does not support integer " + "dtypes larger than native 'intp'.") + field = 'integer' + elif kind == 'f': + field = 'real' + elif kind == 'c': + field = 'complex' + elif kind == 'u': + field = 'unsigned-integer' + else: + raise TypeError('unexpected dtype kind ' + kind) + + if symmetry is None: + symmetry = self._get_symmetry(a) + + # validate rep, field, and symmetry + self.__class__._validate_format(rep) + self.__class__._validate_field(field) + self.__class__._validate_symmetry(symmetry) + + # write initial header line + data = f'%%MatrixMarket matrix {rep} {field} {symmetry}\n' + stream.write(data.encode('latin1')) + + # write comments + for line in comment.split('\n'): + data = f'%{line}\n' + stream.write(data.encode('latin1')) + + template = self._field_template(field, precision) + # write dense format + if rep == self.FORMAT_ARRAY: + # write shape spec + data = '%i %i\n' % (rows, cols) + stream.write(data.encode('latin1')) + + if field in (self.FIELD_INTEGER, self.FIELD_REAL, + self.FIELD_UNSIGNED): + if symmetry == self.SYMMETRY_GENERAL: + for j in range(cols): + for i in range(rows): + data = template % a[i, j] + stream.write(data.encode('latin1')) + + elif symmetry == self.SYMMETRY_SKEW_SYMMETRIC: + for j in range(cols): + for i in range(j + 1, rows): + data = template % a[i, j] + stream.write(data.encode('latin1')) + + else: + for j in range(cols): + for i in range(j, rows): + data = template % a[i, j] + stream.write(data.encode('latin1')) + + elif field == self.FIELD_COMPLEX: + + if symmetry == self.SYMMETRY_GENERAL: + for j in range(cols): + for i in range(rows): + aij = a[i, j] + data = template % (real(aij), imag(aij)) + stream.write(data.encode('latin1')) + else: + for j in range(cols): + for i in range(j, rows): + aij = a[i, j] + data = template % (real(aij), imag(aij)) + stream.write(data.encode('latin1')) + + elif field == self.FIELD_PATTERN: + raise ValueError('pattern type inconsisted with dense format') + + else: + raise TypeError(f'Unknown field type {field}') + + # write sparse format + else: + coo = a.tocoo() # convert to COOrdinate format + + # if symmetry format used, remove values above main diagonal + if symmetry != self.SYMMETRY_GENERAL: + lower_triangle_mask = coo.row >= coo.col + coo = coo_array((coo.data[lower_triangle_mask], + (coo.row[lower_triangle_mask], + coo.col[lower_triangle_mask])), + shape=coo.shape) + + # write shape spec + data = '%i %i %i\n' % (rows, cols, coo.nnz) + stream.write(data.encode('latin1')) + + template = self._field_template(field, precision-1) + + if field == self.FIELD_PATTERN: + for r, c in zip(coo.row+1, coo.col+1): + data = "%i %i\n" % (r, c) + stream.write(data.encode('latin1')) + elif field in (self.FIELD_INTEGER, self.FIELD_REAL, + self.FIELD_UNSIGNED): + for r, c, d in zip(coo.row+1, coo.col+1, coo.data): + data = ("%i %i " % (r, c)) + (template % d) + stream.write(data.encode('latin1')) + elif field == self.FIELD_COMPLEX: + for r, c, d in zip(coo.row+1, coo.col+1, coo.data): + data = ("%i %i " % (r, c)) + (template % (d.real, d.imag)) + stream.write(data.encode('latin1')) + else: + raise TypeError(f'Unknown field type {field}') + + +def _is_fromfile_compatible(stream): + """ + Check whether `stream` is compatible with numpy.fromfile. + + Passing a gzipped file object to ``fromfile/fromstring`` doesn't work with + Python 3. + """ + + bad_cls = [] + try: + import gzip + bad_cls.append(gzip.GzipFile) + except ImportError: + pass + try: + import bz2 + bad_cls.append(bz2.BZ2File) + except ImportError: + pass + + bad_cls = tuple(bad_cls) + return not isinstance(stream, bad_cls) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/_netcdf.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/_netcdf.py new file mode 100644 index 0000000000000000000000000000000000000000..3f4bddd0126facebd39c7ae996eb885a630cf550 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/_netcdf.py @@ -0,0 +1,1094 @@ +""" +NetCDF reader/writer module. + +This module is used to read and create NetCDF files. NetCDF files are +accessed through the `netcdf_file` object. Data written to and from NetCDF +files are contained in `netcdf_variable` objects. Attributes are given +as member variables of the `netcdf_file` and `netcdf_variable` objects. + +This module implements the Scientific.IO.NetCDF API to read and create +NetCDF files. The same API is also used in the PyNIO and pynetcdf +modules, allowing these modules to be used interchangeably when working +with NetCDF files. + +Only NetCDF3 is supported here; for NetCDF4 see +`netCDF4-python `__, +which has a similar API. + +""" + +# TODO: +# * properly implement ``_FillValue``. +# * fix character variables. +# * implement PAGESIZE for Python 2.6? + +# The Scientific.IO.NetCDF API allows attributes to be added directly to +# instances of ``netcdf_file`` and ``netcdf_variable``. To differentiate +# between user-set attributes and instance attributes, user-set attributes +# are automatically stored in the ``_attributes`` attribute by overloading +#``__setattr__``. This is the reason why the code sometimes uses +#``obj.__dict__['key'] = value``, instead of simply ``obj.key = value``; +# otherwise the key would be inserted into userspace attributes. + + +__all__ = ['netcdf_file', 'netcdf_variable'] + + +import warnings +import weakref +from operator import mul +from platform import python_implementation + +import mmap as mm + +import numpy as np +from numpy import frombuffer, dtype, empty, array, asarray +from numpy import little_endian as LITTLE_ENDIAN +from functools import reduce + + +IS_PYPY = python_implementation() == 'PyPy' + +ABSENT = b'\x00\x00\x00\x00\x00\x00\x00\x00' +ZERO = b'\x00\x00\x00\x00' +NC_BYTE = b'\x00\x00\x00\x01' +NC_CHAR = b'\x00\x00\x00\x02' +NC_SHORT = b'\x00\x00\x00\x03' +NC_INT = b'\x00\x00\x00\x04' +NC_FLOAT = b'\x00\x00\x00\x05' +NC_DOUBLE = b'\x00\x00\x00\x06' +NC_DIMENSION = b'\x00\x00\x00\n' +NC_VARIABLE = b'\x00\x00\x00\x0b' +NC_ATTRIBUTE = b'\x00\x00\x00\x0c' +FILL_BYTE = b'\x81' +FILL_CHAR = b'\x00' +FILL_SHORT = b'\x80\x01' +FILL_INT = b'\x80\x00\x00\x01' +FILL_FLOAT = b'\x7C\xF0\x00\x00' +FILL_DOUBLE = b'\x47\x9E\x00\x00\x00\x00\x00\x00' + +TYPEMAP = {NC_BYTE: ('b', 1), + NC_CHAR: ('c', 1), + NC_SHORT: ('h', 2), + NC_INT: ('i', 4), + NC_FLOAT: ('f', 4), + NC_DOUBLE: ('d', 8)} + +FILLMAP = {NC_BYTE: FILL_BYTE, + NC_CHAR: FILL_CHAR, + NC_SHORT: FILL_SHORT, + NC_INT: FILL_INT, + NC_FLOAT: FILL_FLOAT, + NC_DOUBLE: FILL_DOUBLE} + +REVERSE = {('b', 1): NC_BYTE, + ('B', 1): NC_CHAR, + ('c', 1): NC_CHAR, + ('h', 2): NC_SHORT, + ('i', 4): NC_INT, + ('f', 4): NC_FLOAT, + ('d', 8): NC_DOUBLE, + + # these come from asarray(1).dtype.char and asarray('foo').dtype.char, + # used when getting the types from generic attributes. + ('l', 4): NC_INT, + ('S', 1): NC_CHAR} + + +class netcdf_file: + """ + A file object for NetCDF data. + + A `netcdf_file` object has two standard attributes: `dimensions` and + `variables`. The values of both are dictionaries, mapping dimension + names to their associated lengths and variable names to variables, + respectively. Application programs should never modify these + dictionaries. + + All other attributes correspond to global attributes defined in the + NetCDF file. Global file attributes are created by assigning to an + attribute of the `netcdf_file` object. + + Parameters + ---------- + filename : string or file-like + string -> filename + mode : {'r', 'w', 'a'}, optional + read-write-append mode, default is 'r' + mmap : None or bool, optional + Whether to mmap `filename` when reading. Default is True + when `filename` is a file name, False when `filename` is a + file-like object. Note that when mmap is in use, data arrays + returned refer directly to the mmapped data on disk, and the + file cannot be closed as long as references to it exist. + version : {1, 2}, optional + version of netcdf to read / write, where 1 means *Classic + format* and 2 means *64-bit offset format*. Default is 1. See + `here `__ + for more info. + maskandscale : bool, optional + Whether to automatically scale and/or mask data based on attributes. + Default is False. + + Notes + ----- + The major advantage of this module over other modules is that it doesn't + require the code to be linked to the NetCDF libraries. This module is + derived from `pupynere `_. + + NetCDF files are a self-describing binary data format. The file contains + metadata that describes the dimensions and variables in the file. More + details about NetCDF files can be found `here + `__. There + are three main sections to a NetCDF data structure: + + 1. Dimensions + 2. Variables + 3. Attributes + + The dimensions section records the name and length of each dimension used + by the variables. The variables would then indicate which dimensions it + uses and any attributes such as data units, along with containing the data + values for the variable. It is good practice to include a + variable that is the same name as a dimension to provide the values for + that axes. Lastly, the attributes section would contain additional + information such as the name of the file creator or the instrument used to + collect the data. + + When writing data to a NetCDF file, there is often the need to indicate the + 'record dimension'. A record dimension is the unbounded dimension for a + variable. For example, a temperature variable may have dimensions of + latitude, longitude and time. If one wants to add more temperature data to + the NetCDF file as time progresses, then the temperature variable should + have the time dimension flagged as the record dimension. + + In addition, the NetCDF file header contains the position of the data in + the file, so access can be done in an efficient manner without loading + unnecessary data into memory. It uses the ``mmap`` module to create + Numpy arrays mapped to the data on disk, for the same purpose. + + Note that when `netcdf_file` is used to open a file with mmap=True + (default for read-only), arrays returned by it refer to data + directly on the disk. The file should not be closed, and cannot be cleanly + closed when asked, if such arrays are alive. You may want to copy data arrays + obtained from mmapped Netcdf file if they are to be processed after the file + is closed, see the example below. + + Examples + -------- + To create a NetCDF file: + + >>> from scipy.io import netcdf_file + >>> import numpy as np + >>> f = netcdf_file('simple.nc', 'w') + >>> f.history = 'Created for a test' + >>> f.createDimension('time', 10) + >>> time = f.createVariable('time', 'i', ('time',)) + >>> time[:] = np.arange(10) + >>> time.units = 'days since 2008-01-01' + >>> f.close() + + Note the assignment of ``arange(10)`` to ``time[:]``. Exposing the slice + of the time variable allows for the data to be set in the object, rather + than letting ``arange(10)`` overwrite the ``time`` variable. + + To read the NetCDF file we just created: + + >>> from scipy.io import netcdf_file + >>> f = netcdf_file('simple.nc', 'r') + >>> print(f.history) + b'Created for a test' + >>> time = f.variables['time'] + >>> print(time.units) + b'days since 2008-01-01' + >>> print(time.shape) + (10,) + >>> print(time[-1]) + 9 + + NetCDF files, when opened read-only, return arrays that refer + directly to memory-mapped data on disk: + + >>> data = time[:] + + If the data is to be processed after the file is closed, it needs + to be copied to main memory: + + >>> data = time[:].copy() + >>> del time + >>> f.close() + >>> data.mean() + 4.5 + + A NetCDF file can also be used as context manager: + + >>> from scipy.io import netcdf_file + >>> with netcdf_file('simple.nc', 'r') as f: + ... print(f.history) + b'Created for a test' + + """ + def __init__(self, filename, mode='r', mmap=None, version=1, + maskandscale=False): + """Initialize netcdf_file from fileobj (str or file-like).""" + if mode not in 'rwa': + raise ValueError("Mode must be either 'r', 'w' or 'a'.") + + if hasattr(filename, 'seek'): # file-like + self.fp = filename + self.filename = 'None' + if mmap is None: + mmap = False + elif mmap and not hasattr(filename, 'fileno'): + raise ValueError('Cannot use file object for mmap') + else: # maybe it's a string + self.filename = filename + omode = 'r+' if mode == 'a' else mode + self.fp = open(self.filename, f'{omode}b') + if mmap is None: + # Mmapped files on PyPy cannot be usually closed + # before the GC runs, so it's better to use mmap=False + # as the default. + mmap = (not IS_PYPY) + + if mode != 'r': + # Cannot read write-only files + mmap = False + + self.use_mmap = mmap + self.mode = mode + self.version_byte = version + self.maskandscale = maskandscale + + self.dimensions = {} + self.variables = {} + + self._dims = [] + self._recs = 0 + self._recsize = 0 + + self._mm = None + self._mm_buf = None + if self.use_mmap: + self._mm = mm.mmap(self.fp.fileno(), 0, access=mm.ACCESS_READ) + self._mm_buf = np.frombuffer(self._mm, dtype=np.int8) + + self._attributes = {} + + if mode in 'ra': + self._read() + + def __setattr__(self, attr, value): + # Store user defined attributes in a separate dict, + # so we can save them to file later. + try: + self._attributes[attr] = value + except AttributeError: + pass + self.__dict__[attr] = value + + def close(self): + """Closes the NetCDF file.""" + if hasattr(self, 'fp') and not self.fp.closed: + try: + self.flush() + finally: + self.variables = {} + if self._mm_buf is not None: + ref = weakref.ref(self._mm_buf) + self._mm_buf = None + if ref() is None: + # self._mm_buf is gc'd, and we can close the mmap + self._mm.close() + else: + # we cannot close self._mm, since self._mm_buf is + # alive and there may still be arrays referring to it + warnings.warn( + "Cannot close a netcdf_file opened with mmap=True, when " + "netcdf_variables or arrays referring to its data still " + "exist. All data arrays obtained from such files refer " + "directly to data on disk, and must be copied before the " + "file can be cleanly closed. " + "(See netcdf_file docstring for more information on mmap.)", + category=RuntimeWarning, stacklevel=2, + ) + self._mm = None + self.fp.close() + __del__ = close + + def __enter__(self): + return self + + def __exit__(self, type, value, traceback): + self.close() + + def createDimension(self, name, length): + """ + Adds a dimension to the Dimension section of the NetCDF data structure. + + Note that this function merely adds a new dimension that the variables can + reference. The values for the dimension, if desired, should be added as + a variable using `createVariable`, referring to this dimension. + + Parameters + ---------- + name : str + Name of the dimension (Eg, 'lat' or 'time'). + length : int + Length of the dimension. + + See Also + -------- + createVariable + + """ + if length is None and self._dims: + raise ValueError("Only first dimension may be unlimited!") + + self.dimensions[name] = length + self._dims.append(name) + + def createVariable(self, name, type, dimensions): + """ + Create an empty variable for the `netcdf_file` object, specifying its data + type and the dimensions it uses. + + Parameters + ---------- + name : str + Name of the new variable. + type : dtype or str + Data type of the variable. + dimensions : sequence of str + List of the dimension names used by the variable, in the desired order. + + Returns + ------- + variable : netcdf_variable + The newly created ``netcdf_variable`` object. + This object has also been added to the `netcdf_file` object as well. + + See Also + -------- + createDimension + + Notes + ----- + Any dimensions to be used by the variable should already exist in the + NetCDF data structure or should be created by `createDimension` prior to + creating the NetCDF variable. + + """ + shape = tuple([self.dimensions[dim] for dim in dimensions]) + shape_ = tuple([dim or 0 for dim in shape]) # replace None with 0 for NumPy + + type = dtype(type) + typecode, size = type.char, type.itemsize + if (typecode, size) not in REVERSE: + raise ValueError(f"NetCDF 3 does not support type {type}") + + # convert to big endian always for NetCDF 3 + data = empty(shape_, dtype=type.newbyteorder("B")) + self.variables[name] = netcdf_variable( + data, typecode, size, shape, dimensions, + maskandscale=self.maskandscale) + return self.variables[name] + + def flush(self): + """ + Perform a sync-to-disk flush if the `netcdf_file` object is in write mode. + + See Also + -------- + sync : Identical function + + """ + if hasattr(self, 'mode') and self.mode in 'wa': + self._write() + sync = flush + + def _write(self): + self.fp.seek(0) + self.fp.write(b'CDF') + self.fp.write(array(self.version_byte, '>b').tobytes()) + + # Write headers and data. + self._write_numrecs() + self._write_dim_array() + self._write_gatt_array() + self._write_var_array() + + def _write_numrecs(self): + # Get highest record count from all record variables. + for var in self.variables.values(): + if var.isrec and len(var.data) > self._recs: + self.__dict__['_recs'] = len(var.data) + self._pack_int(self._recs) + + def _write_dim_array(self): + if self.dimensions: + self.fp.write(NC_DIMENSION) + self._pack_int(len(self.dimensions)) + for name in self._dims: + self._pack_string(name) + length = self.dimensions[name] + self._pack_int(length or 0) # replace None with 0 for record dimension + else: + self.fp.write(ABSENT) + + def _write_gatt_array(self): + self._write_att_array(self._attributes) + + def _write_att_array(self, attributes): + if attributes: + self.fp.write(NC_ATTRIBUTE) + self._pack_int(len(attributes)) + for name, values in attributes.items(): + self._pack_string(name) + self._write_att_values(values) + else: + self.fp.write(ABSENT) + + def _write_var_array(self): + if self.variables: + self.fp.write(NC_VARIABLE) + self._pack_int(len(self.variables)) + + # Sort variable names non-recs first, then recs. + def sortkey(n): + v = self.variables[n] + if v.isrec: + return (-1,) + return v._shape + variables = sorted(self.variables, key=sortkey, reverse=True) + + # Set the metadata for all variables. + for name in variables: + self._write_var_metadata(name) + # Now that we have the metadata, we know the vsize of + # each record variable, so we can calculate recsize. + self.__dict__['_recsize'] = sum([ + var._vsize for var in self.variables.values() + if var.isrec]) + # Set the data for all variables. + for name in variables: + self._write_var_data(name) + else: + self.fp.write(ABSENT) + + def _write_var_metadata(self, name): + var = self.variables[name] + + self._pack_string(name) + self._pack_int(len(var.dimensions)) + for dimname in var.dimensions: + dimid = self._dims.index(dimname) + self._pack_int(dimid) + + self._write_att_array(var._attributes) + + nc_type = REVERSE[var.typecode(), var.itemsize()] + self.fp.write(nc_type) + + if not var.isrec: + vsize = var.data.size * var.data.itemsize + vsize += -vsize % 4 + else: # record variable + try: + vsize = var.data[0].size * var.data.itemsize + except IndexError: + vsize = 0 + rec_vars = len([v for v in self.variables.values() + if v.isrec]) + if rec_vars > 1: + vsize += -vsize % 4 + self.variables[name].__dict__['_vsize'] = vsize + self._pack_int(vsize) + + # Pack a bogus begin, and set the real value later. + self.variables[name].__dict__['_begin'] = self.fp.tell() + self._pack_begin(0) + + def _write_var_data(self, name): + var = self.variables[name] + + # Set begin in file header. + the_beguine = self.fp.tell() + self.fp.seek(var._begin) + self._pack_begin(the_beguine) + self.fp.seek(the_beguine) + + # Write data. + if not var.isrec: + self.fp.write(var.data.tobytes()) + count = var.data.size * var.data.itemsize + self._write_var_padding(var, var._vsize - count) + else: # record variable + # Handle rec vars with shape[0] < nrecs. + if self._recs > len(var.data): + shape = (self._recs,) + var.data.shape[1:] + # Resize in-place does not always work since + # the array might not be single-segment + try: + var.data.resize(shape) + except ValueError: + dtype = var.data.dtype + var.__dict__['data'] = np.resize(var.data, shape).astype(dtype) + + pos0 = pos = self.fp.tell() + for rec in var.data: + # Apparently scalars cannot be converted to big endian. If we + # try to convert a ``=i4`` scalar to, say, '>i4' the dtype + # will remain as ``=i4``. + if not rec.shape and (rec.dtype.byteorder == '<' or + (rec.dtype.byteorder == '=' and LITTLE_ENDIAN)): + rec = rec.byteswap() + self.fp.write(rec.tobytes()) + # Padding + count = rec.size * rec.itemsize + self._write_var_padding(var, var._vsize - count) + pos += self._recsize + self.fp.seek(pos) + self.fp.seek(pos0 + var._vsize) + + def _write_var_padding(self, var, size): + encoded_fill_value = var._get_encoded_fill_value() + num_fills = size // len(encoded_fill_value) + self.fp.write(encoded_fill_value * num_fills) + + def _write_att_values(self, values): + if hasattr(values, 'dtype'): + nc_type = REVERSE[values.dtype.char, values.dtype.itemsize] + else: + types = [(int, NC_INT), (float, NC_FLOAT), (str, NC_CHAR)] + + # bytes index into scalars in py3k. Check for "string" types + if isinstance(values, (str, bytes)): + sample = values + else: + try: + sample = values[0] # subscriptable? + except TypeError: + sample = values # scalar + + for class_, nc_type in types: + if isinstance(sample, class_): + break + + typecode, size = TYPEMAP[nc_type] + dtype_ = f'>{typecode}' + # asarray() dies with bytes and '>c' in py3k. Change to 'S' + dtype_ = 'S' if dtype_ == '>c' else dtype_ + + values = asarray(values, dtype=dtype_) + + self.fp.write(nc_type) + + if values.dtype.char == 'S': + nelems = values.itemsize + else: + nelems = values.size + self._pack_int(nelems) + + if not values.shape and (values.dtype.byteorder == '<' or + (values.dtype.byteorder == '=' and LITTLE_ENDIAN)): + values = values.byteswap() + self.fp.write(values.tobytes()) + count = values.size * values.itemsize + self.fp.write(b'\x00' * (-count % 4)) # pad + + def _read(self): + # Check magic bytes and version + magic = self.fp.read(3) + if not magic == b'CDF': + raise TypeError(f"Error: {self.filename} is not a valid NetCDF 3 file") + self.__dict__['version_byte'] = frombuffer(self.fp.read(1), '>b')[0] + + # Read file headers and set data. + self._read_numrecs() + self._read_dim_array() + self._read_gatt_array() + self._read_var_array() + + def _read_numrecs(self): + self.__dict__['_recs'] = self._unpack_int() + + def _read_dim_array(self): + header = self.fp.read(4) + if header not in [ZERO, NC_DIMENSION]: + raise ValueError("Unexpected header.") + count = self._unpack_int() + + for dim in range(count): + name = self._unpack_string().decode('latin1') + length = self._unpack_int() or None # None for record dimension + self.dimensions[name] = length + self._dims.append(name) # preserve order + + def _read_gatt_array(self): + for k, v in self._read_att_array().items(): + self.__setattr__(k, v) + + def _read_att_array(self): + header = self.fp.read(4) + if header not in [ZERO, NC_ATTRIBUTE]: + raise ValueError("Unexpected header.") + count = self._unpack_int() + + attributes = {} + for attr in range(count): + name = self._unpack_string().decode('latin1') + attributes[name] = self._read_att_values() + return attributes + + def _read_var_array(self): + header = self.fp.read(4) + if header not in [ZERO, NC_VARIABLE]: + raise ValueError("Unexpected header.") + + begin = 0 + dtypes = {'names': [], 'formats': []} + rec_vars = [] + count = self._unpack_int() + for var in range(count): + (name, dimensions, shape, attributes, + typecode, size, dtype_, begin_, vsize) = self._read_var() + # https://www.unidata.ucar.edu/software/netcdf/guide_toc.html + # Note that vsize is the product of the dimension lengths + # (omitting the record dimension) and the number of bytes + # per value (determined from the type), increased to the + # next multiple of 4, for each variable. If a record + # variable, this is the amount of space per record. The + # netCDF "record size" is calculated as the sum of the + # vsize's of all the record variables. + # + # The vsize field is actually redundant, because its value + # may be computed from other information in the header. The + # 32-bit vsize field is not large enough to contain the size + # of variables that require more than 2^32 - 4 bytes, so + # 2^32 - 1 is used in the vsize field for such variables. + if shape and shape[0] is None: # record variable + rec_vars.append(name) + # The netCDF "record size" is calculated as the sum of + # the vsize's of all the record variables. + self.__dict__['_recsize'] += vsize + if begin == 0: + begin = begin_ + dtypes['names'].append(name) + dtypes['formats'].append(str(shape[1:]) + dtype_) + + # Handle padding with a virtual variable. + if typecode in 'bch': + actual_size = reduce(mul, (1,) + shape[1:]) * size + padding = -actual_size % 4 + if padding: + dtypes['names'].append('_padding_%d' % var) + dtypes['formats'].append('(%d,)>b' % padding) + + # Data will be set later. + data = None + else: # not a record variable + # Calculate size to avoid problems with vsize (above) + a_size = reduce(mul, shape, 1) * size + if self.use_mmap: + data = self._mm_buf[begin_:begin_+a_size].view(dtype=dtype_) + data.shape = shape + else: + pos = self.fp.tell() + self.fp.seek(begin_) + data = frombuffer(self.fp.read(a_size), dtype=dtype_ + ).copy() + data.shape = shape + self.fp.seek(pos) + + # Add variable. + self.variables[name] = netcdf_variable( + data, typecode, size, shape, dimensions, attributes, + maskandscale=self.maskandscale) + + if rec_vars: + # Remove padding when only one record variable. + if len(rec_vars) == 1: + dtypes['names'] = dtypes['names'][:1] + dtypes['formats'] = dtypes['formats'][:1] + + # Build rec array. + if self.use_mmap: + buf = self._mm_buf[begin:begin+self._recs*self._recsize] + rec_array = buf.view(dtype=dtypes) + rec_array.shape = (self._recs,) + else: + pos = self.fp.tell() + self.fp.seek(begin) + rec_array = frombuffer(self.fp.read(self._recs*self._recsize), + dtype=dtypes).copy() + rec_array.shape = (self._recs,) + self.fp.seek(pos) + + for var in rec_vars: + self.variables[var].__dict__['data'] = rec_array[var] + + def _read_var(self): + name = self._unpack_string().decode('latin1') + dimensions = [] + shape = [] + dims = self._unpack_int() + + for i in range(dims): + dimid = self._unpack_int() + dimname = self._dims[dimid] + dimensions.append(dimname) + dim = self.dimensions[dimname] + shape.append(dim) + dimensions = tuple(dimensions) + shape = tuple(shape) + + attributes = self._read_att_array() + nc_type = self.fp.read(4) + vsize = self._unpack_int() + begin = [self._unpack_int, self._unpack_int64][self.version_byte-1]() + + typecode, size = TYPEMAP[nc_type] + dtype_ = f'>{typecode}' + + return name, dimensions, shape, attributes, typecode, size, dtype_, begin, vsize + + def _read_att_values(self): + nc_type = self.fp.read(4) + n = self._unpack_int() + + typecode, size = TYPEMAP[nc_type] + + count = n*size + values = self.fp.read(int(count)) + self.fp.read(-count % 4) # read padding + + if typecode != 'c': + values = frombuffer(values, dtype=f'>{typecode}').copy() + if values.shape == (1,): + values = values[0] + else: + values = values.rstrip(b'\x00') + return values + + def _pack_begin(self, begin): + if self.version_byte == 1: + self._pack_int(begin) + elif self.version_byte == 2: + self._pack_int64(begin) + + def _pack_int(self, value): + self.fp.write(array(value, '>i').tobytes()) + _pack_int32 = _pack_int + + def _unpack_int(self): + return int(frombuffer(self.fp.read(4), '>i')[0]) + _unpack_int32 = _unpack_int + + def _pack_int64(self, value): + self.fp.write(array(value, '>q').tobytes()) + + def _unpack_int64(self): + return frombuffer(self.fp.read(8), '>q')[0] + + def _pack_string(self, s): + count = len(s) + self._pack_int(count) + self.fp.write(s.encode('latin1')) + self.fp.write(b'\x00' * (-count % 4)) # pad + + def _unpack_string(self): + count = self._unpack_int() + s = self.fp.read(count).rstrip(b'\x00') + self.fp.read(-count % 4) # read padding + return s + + +class netcdf_variable: + """ + A data object for netcdf files. + + `netcdf_variable` objects are constructed by calling the method + `netcdf_file.createVariable` on the `netcdf_file` object. `netcdf_variable` + objects behave much like array objects defined in numpy, except that their + data resides in a file. Data is read by indexing and written by assigning + to an indexed subset; the entire array can be accessed by the index ``[:]`` + or (for scalars) by using the methods `getValue` and `assignValue`. + `netcdf_variable` objects also have attribute `shape` with the same meaning + as for arrays, but the shape cannot be modified. There is another read-only + attribute `dimensions`, whose value is the tuple of dimension names. + + All other attributes correspond to variable attributes defined in + the NetCDF file. Variable attributes are created by assigning to an + attribute of the `netcdf_variable` object. + + Parameters + ---------- + data : array_like + The data array that holds the values for the variable. + Typically, this is initialized as empty, but with the proper shape. + typecode : dtype character code + Desired data-type for the data array. + size : int + Desired element size for the data array. + shape : sequence of ints + The shape of the array. This should match the lengths of the + variable's dimensions. + dimensions : sequence of strings + The names of the dimensions used by the variable. Must be in the + same order of the dimension lengths given by `shape`. + attributes : dict, optional + Attribute values (any type) keyed by string names. These attributes + become attributes for the netcdf_variable object. + maskandscale : bool, optional + Whether to automatically scale and/or mask data based on attributes. + Default is False. + + + Attributes + ---------- + dimensions : list of str + List of names of dimensions used by the variable object. + isrec, shape + Properties + + See also + -------- + isrec, shape + + """ + def __init__(self, data, typecode, size, shape, dimensions, + attributes=None, + maskandscale=False): + self.data = data + self._typecode = typecode + self._size = size + self._shape = shape + self.dimensions = dimensions + self.maskandscale = maskandscale + + self._attributes = attributes or {} + for k, v in self._attributes.items(): + self.__dict__[k] = v + + def __setattr__(self, attr, value): + # Store user defined attributes in a separate dict, + # so we can save them to file later. + try: + self._attributes[attr] = value + except AttributeError: + pass + self.__dict__[attr] = value + + def isrec(self): + """Returns whether the variable has a record dimension or not. + + A record dimension is a dimension along which additional data could be + easily appended in the netcdf data structure without much rewriting of + the data file. This attribute is a read-only property of the + `netcdf_variable`. + + """ + return bool(self.data.shape) and not self._shape[0] + isrec = property(isrec) + + def shape(self): + """Returns the shape tuple of the data variable. + + This is a read-only attribute and can not be modified in the + same manner of other numpy arrays. + """ + return self.data.shape + shape = property(shape) + + def getValue(self): + """ + Retrieve a scalar value from a `netcdf_variable` of length one. + + Raises + ------ + ValueError + If the netcdf variable is an array of length greater than one, + this exception will be raised. + + """ + return self.data.item() + + def assignValue(self, value): + """ + Assign a scalar value to a `netcdf_variable` of length one. + + Parameters + ---------- + value : scalar + Scalar value (of compatible type) to assign to a length-one netcdf + variable. This value will be written to file. + + Raises + ------ + ValueError + If the input is not a scalar, or if the destination is not a length-one + netcdf variable. + + """ + if not self.data.flags.writeable: + # Work-around for a bug in NumPy. Calling itemset() on a read-only + # memory-mapped array causes a seg. fault. + # See NumPy ticket #1622, and SciPy ticket #1202. + # This check for `writeable` can be removed when the oldest version + # of NumPy still supported by scipy contains the fix for #1622. + raise RuntimeError("variable is not writeable") + + self.data[:] = value + + def typecode(self): + """ + Return the typecode of the variable. + + Returns + ------- + typecode : char + The character typecode of the variable (e.g., 'i' for int). + + """ + return self._typecode + + def itemsize(self): + """ + Return the itemsize of the variable. + + Returns + ------- + itemsize : int + The element size of the variable (e.g., 8 for float64). + + """ + return self._size + + def __getitem__(self, index): + if not self.maskandscale: + return self.data[index] + + data = self.data[index].copy() + missing_value = self._get_missing_value() + data = self._apply_missing_value(data, missing_value) + scale_factor = self._attributes.get('scale_factor') + add_offset = self._attributes.get('add_offset') + if add_offset is not None or scale_factor is not None: + data = data.astype(np.float64) + if scale_factor is not None: + data = data * scale_factor + if add_offset is not None: + data += add_offset + + return data + + def __setitem__(self, index, data): + if self.maskandscale: + missing_value = ( + self._get_missing_value() or + getattr(data, 'fill_value', 999999)) + self._attributes.setdefault('missing_value', missing_value) + self._attributes.setdefault('_FillValue', missing_value) + data = ((data - self._attributes.get('add_offset', 0.0)) / + self._attributes.get('scale_factor', 1.0)) + data = np.ma.asarray(data).filled(missing_value) + if self._typecode not in 'fd' and data.dtype.kind == 'f': + data = np.round(data) + + # Expand data for record vars? + if self.isrec: + if isinstance(index, tuple): + rec_index = index[0] + else: + rec_index = index + if isinstance(rec_index, slice): + recs = (rec_index.start or 0) + len(data) + else: + recs = rec_index + 1 + if recs > len(self.data): + shape = (recs,) + self._shape[1:] + # Resize in-place does not always work since + # the array might not be single-segment + try: + self.data.resize(shape) + except ValueError: + dtype = self.data.dtype + self.__dict__['data'] = np.resize(self.data, shape).astype(dtype) + self.data[index] = data + + def _default_encoded_fill_value(self): + """ + The default encoded fill-value for this Variable's data type. + """ + nc_type = REVERSE[self.typecode(), self.itemsize()] + return FILLMAP[nc_type] + + def _get_encoded_fill_value(self): + """ + Returns the encoded fill value for this variable as bytes. + + This is taken from either the _FillValue attribute, or the default fill + value for this variable's data type. + """ + if '_FillValue' in self._attributes: + fill_value = np.array(self._attributes['_FillValue'], + dtype=self.data.dtype).tobytes() + if len(fill_value) == self.itemsize(): + return fill_value + else: + return self._default_encoded_fill_value() + else: + return self._default_encoded_fill_value() + + def _get_missing_value(self): + """ + Returns the value denoting "no data" for this variable. + + If this variable does not have a missing/fill value, returns None. + + If both _FillValue and missing_value are given, give precedence to + _FillValue. The netCDF standard gives special meaning to _FillValue; + missing_value is just used for compatibility with old datasets. + """ + + if '_FillValue' in self._attributes: + missing_value = self._attributes['_FillValue'] + elif 'missing_value' in self._attributes: + missing_value = self._attributes['missing_value'] + else: + missing_value = None + + return missing_value + + @staticmethod + def _apply_missing_value(data, missing_value): + """ + Applies the given missing value to the data array. + + Returns a numpy.ma array, with any value equal to missing_value masked + out (unless missing_value is None, in which case the original array is + returned). + """ + + if missing_value is None: + newdata = data + else: + try: + missing_value_isnan = np.isnan(missing_value) + except (TypeError, NotImplementedError): + # some data types (e.g., characters) cannot be tested for NaN + missing_value_isnan = False + + if missing_value_isnan: + mymask = np.isnan(data) + else: + mymask = (data == missing_value) + + newdata = np.ma.masked_where(mymask, data) + + return newdata + + +NetCDFFile = netcdf_file +NetCDFVariable = netcdf_variable diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/_test_fortran.cpython-310-x86_64-linux-gnu.so b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/_test_fortran.cpython-310-x86_64-linux-gnu.so new file mode 100644 index 0000000000000000000000000000000000000000..8a863a591370a3b1bd4afdb05e621c1af8eab3e2 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/_test_fortran.cpython-310-x86_64-linux-gnu.so differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..dcfe1c4237e69054b582a0fa52f710b25a1d7914 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/__init__.py @@ -0,0 +1,28 @@ +""" +Module to read ARFF files +========================= +ARFF is the standard data format for WEKA. +It is a text file format which support numerical, string and data values. +The format can also represent missing data and sparse data. + +Notes +----- +The ARFF support in ``scipy.io`` provides file reading functionality only. +For more extensive ARFF functionality, see `liac-arff +`_. + +See the `WEKA website `_ +for more details about the ARFF format and available datasets. + +""" +from ._arffread import * +from . import _arffread + +# Deprecated namespaces, to be removed in v2.0.0 +from .import arffread + +__all__ = _arffread.__all__ + ['arffread'] + +from scipy._lib._testutils import PytestTester +test = PytestTester(__name__) +del PytestTester diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/_arffread.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/_arffread.py new file mode 100644 index 0000000000000000000000000000000000000000..65495b8d98386492eecbccb8968715590c0faf7c --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/_arffread.py @@ -0,0 +1,873 @@ +# Last Change: Mon Aug 20 08:00 PM 2007 J +import re +import datetime + +import numpy as np + +import csv +import ctypes + +"""A module to read arff files.""" + +__all__ = ['MetaData', 'loadarff', 'ArffError', 'ParseArffError'] + +# An Arff file is basically two parts: +# - header +# - data +# +# A header has each of its components starting by @META where META is one of +# the keyword (attribute of relation, for now). + +# TODO: +# - both integer and reals are treated as numeric -> the integer info +# is lost! +# - Replace ValueError by ParseError or something + +# We know can handle the following: +# - numeric and nominal attributes +# - missing values for numeric attributes + +r_meta = re.compile(r'^\s*@') +# Match a comment +r_comment = re.compile(r'^%') +# Match an empty line +r_empty = re.compile(r'^\s+$') +# Match a header line, that is a line which starts by @ + a word +r_headerline = re.compile(r'^\s*@\S*') +r_datameta = re.compile(r'^@[Dd][Aa][Tt][Aa]') +r_relation = re.compile(r'^@[Rr][Ee][Ll][Aa][Tt][Ii][Oo][Nn]\s*(\S*)') +r_attribute = re.compile(r'^\s*@[Aa][Tt][Tt][Rr][Ii][Bb][Uu][Tt][Ee]\s*(..*$)') + +r_nominal = re.compile(r'{(.+)}') +r_date = re.compile(r"[Dd][Aa][Tt][Ee]\s+[\"']?(.+?)[\"']?$") + +# To get attributes name enclosed with '' +r_comattrval = re.compile(r"'(..+)'\s+(..+$)") +# To get normal attributes +r_wcomattrval = re.compile(r"(\S+)\s+(..+$)") + +# ------------------------ +# Module defined exception +# ------------------------ + + +class ArffError(OSError): + pass + + +class ParseArffError(ArffError): + pass + + +# ---------- +# Attributes +# ---------- +class Attribute: + + type_name = None + + def __init__(self, name): + self.name = name + self.range = None + self.dtype = np.object_ + + @classmethod + def parse_attribute(cls, name, attr_string): + """ + Parse the attribute line if it knows how. Returns the parsed + attribute, or None. + """ + return None + + def parse_data(self, data_str): + """ + Parse a value of this type. + """ + return None + + def __str__(self): + """ + Parse a value of this type. + """ + return self.name + ',' + self.type_name + + +class NominalAttribute(Attribute): + + type_name = 'nominal' + + def __init__(self, name, values): + super().__init__(name) + self.values = values + self.range = values + self.dtype = (np.bytes_, max(len(i) for i in values)) + + @staticmethod + def _get_nom_val(atrv): + """Given a string containing a nominal type, returns a tuple of the + possible values. + + A nominal type is defined as something framed between braces ({}). + + Parameters + ---------- + atrv : str + Nominal type definition + + Returns + ------- + poss_vals : tuple + possible values + + Examples + -------- + >>> from scipy.io.arff._arffread import NominalAttribute + >>> NominalAttribute._get_nom_val("{floup, bouga, fl, ratata}") + ('floup', 'bouga', 'fl', 'ratata') + """ + m = r_nominal.match(atrv) + if m: + attrs, _ = split_data_line(m.group(1)) + return tuple(attrs) + else: + raise ValueError("This does not look like a nominal string") + + @classmethod + def parse_attribute(cls, name, attr_string): + """ + Parse the attribute line if it knows how. Returns the parsed + attribute, or None. + + For nominal attributes, the attribute string would be like '{, + , }'. + """ + if attr_string[0] == '{': + values = cls._get_nom_val(attr_string) + return cls(name, values) + else: + return None + + def parse_data(self, data_str): + """ + Parse a value of this type. + """ + if data_str in self.values: + return data_str + elif data_str == '?': + return data_str + else: + raise ValueError(f"{str(data_str)} value not in {str(self.values)}") + + def __str__(self): + msg = self.name + ",{" + for i in range(len(self.values)-1): + msg += self.values[i] + "," + msg += self.values[-1] + msg += "}" + return msg + + +class NumericAttribute(Attribute): + + def __init__(self, name): + super().__init__(name) + self.type_name = 'numeric' + self.dtype = np.float64 + + @classmethod + def parse_attribute(cls, name, attr_string): + """ + Parse the attribute line if it knows how. Returns the parsed + attribute, or None. + + For numeric attributes, the attribute string would be like + 'numeric' or 'int' or 'real'. + """ + + attr_string = attr_string.lower().strip() + + if (attr_string[:len('numeric')] == 'numeric' or + attr_string[:len('int')] == 'int' or + attr_string[:len('real')] == 'real'): + return cls(name) + else: + return None + + def parse_data(self, data_str): + """ + Parse a value of this type. + + Parameters + ---------- + data_str : str + string to convert + + Returns + ------- + f : float + where float can be nan + + Examples + -------- + >>> from scipy.io.arff._arffread import NumericAttribute + >>> atr = NumericAttribute('atr') + >>> atr.parse_data('1') + 1.0 + >>> atr.parse_data('1\\n') + 1.0 + >>> atr.parse_data('?\\n') + nan + """ + if '?' in data_str: + return np.nan + else: + return float(data_str) + + def _basic_stats(self, data): + nbfac = data.size * 1. / (data.size - 1) + return (np.nanmin(data), np.nanmax(data), + np.mean(data), np.std(data) * nbfac) + + +class StringAttribute(Attribute): + + def __init__(self, name): + super().__init__(name) + self.type_name = 'string' + + @classmethod + def parse_attribute(cls, name, attr_string): + """ + Parse the attribute line if it knows how. Returns the parsed + attribute, or None. + + For string attributes, the attribute string would be like + 'string'. + """ + + attr_string = attr_string.lower().strip() + + if attr_string[:len('string')] == 'string': + return cls(name) + else: + return None + + +class DateAttribute(Attribute): + + def __init__(self, name, date_format, datetime_unit): + super().__init__(name) + self.date_format = date_format + self.datetime_unit = datetime_unit + self.type_name = 'date' + self.range = date_format + self.dtype = np.datetime64(0, self.datetime_unit) + + @staticmethod + def _get_date_format(atrv): + m = r_date.match(atrv) + if m: + pattern = m.group(1).strip() + # convert time pattern from Java's SimpleDateFormat to C's format + datetime_unit = None + if "yyyy" in pattern: + pattern = pattern.replace("yyyy", "%Y") + datetime_unit = "Y" + elif "yy": + pattern = pattern.replace("yy", "%y") + datetime_unit = "Y" + if "MM" in pattern: + pattern = pattern.replace("MM", "%m") + datetime_unit = "M" + if "dd" in pattern: + pattern = pattern.replace("dd", "%d") + datetime_unit = "D" + if "HH" in pattern: + pattern = pattern.replace("HH", "%H") + datetime_unit = "h" + if "mm" in pattern: + pattern = pattern.replace("mm", "%M") + datetime_unit = "m" + if "ss" in pattern: + pattern = pattern.replace("ss", "%S") + datetime_unit = "s" + if "z" in pattern or "Z" in pattern: + raise ValueError("Date type attributes with time zone not " + "supported, yet") + + if datetime_unit is None: + raise ValueError("Invalid or unsupported date format") + + return pattern, datetime_unit + else: + raise ValueError("Invalid or no date format") + + @classmethod + def parse_attribute(cls, name, attr_string): + """ + Parse the attribute line if it knows how. Returns the parsed + attribute, or None. + + For date attributes, the attribute string would be like + 'date '. + """ + + attr_string_lower = attr_string.lower().strip() + + if attr_string_lower[:len('date')] == 'date': + date_format, datetime_unit = cls._get_date_format(attr_string) + return cls(name, date_format, datetime_unit) + else: + return None + + def parse_data(self, data_str): + """ + Parse a value of this type. + """ + date_str = data_str.strip().strip("'").strip('"') + if date_str == '?': + return np.datetime64('NaT', self.datetime_unit) + else: + dt = datetime.datetime.strptime(date_str, self.date_format) + return np.datetime64(dt).astype( + f"datetime64[{self.datetime_unit}]") + + def __str__(self): + return super().__str__() + ',' + self.date_format + + +class RelationalAttribute(Attribute): + + def __init__(self, name): + super().__init__(name) + self.type_name = 'relational' + self.dtype = np.object_ + self.attributes = [] + self.dialect = None + + @classmethod + def parse_attribute(cls, name, attr_string): + """ + Parse the attribute line if it knows how. Returns the parsed + attribute, or None. + + For date attributes, the attribute string would be like + 'date '. + """ + + attr_string_lower = attr_string.lower().strip() + + if attr_string_lower[:len('relational')] == 'relational': + return cls(name) + else: + return None + + def parse_data(self, data_str): + # Copy-pasted + elems = list(range(len(self.attributes))) + + escaped_string = data_str.encode().decode("unicode-escape") + + row_tuples = [] + + for raw in escaped_string.split("\n"): + row, self.dialect = split_data_line(raw, self.dialect) + + row_tuples.append(tuple( + [self.attributes[i].parse_data(row[i]) for i in elems])) + + return np.array(row_tuples, + [(a.name, a.dtype) for a in self.attributes]) + + def __str__(self): + return (super().__str__() + '\n\t' + + '\n\t'.join(str(a) for a in self.attributes)) + + +# ----------------- +# Various utilities +# ----------------- +def to_attribute(name, attr_string): + attr_classes = (NominalAttribute, NumericAttribute, DateAttribute, + StringAttribute, RelationalAttribute) + + for cls in attr_classes: + attr = cls.parse_attribute(name, attr_string) + if attr is not None: + return attr + + raise ParseArffError(f"unknown attribute {attr_string}") + + +def csv_sniffer_has_bug_last_field(): + """ + Checks if the bug https://bugs.python.org/issue30157 is unpatched. + """ + + # We only compute this once. + has_bug = getattr(csv_sniffer_has_bug_last_field, "has_bug", None) + + if has_bug is None: + dialect = csv.Sniffer().sniff("3, 'a'") + csv_sniffer_has_bug_last_field.has_bug = dialect.quotechar != "'" + has_bug = csv_sniffer_has_bug_last_field.has_bug + + return has_bug + + +def workaround_csv_sniffer_bug_last_field(sniff_line, dialect, delimiters): + """ + Workaround for the bug https://bugs.python.org/issue30157 if is unpatched. + """ + if csv_sniffer_has_bug_last_field(): + # Reuses code from the csv module + right_regex = r'(?P[^\w\n"\'])(?P ?)(?P["\']).*?(?P=quote)(?:$|\n)' # noqa: E501 + + for restr in (r'(?P[^\w\n"\'])(?P ?)(?P["\']).*?(?P=quote)(?P=delim)', # ,".*?", # noqa: E501 + r'(?:^|\n)(?P["\']).*?(?P=quote)(?P[^\w\n"\'])(?P ?)', # .*?", # noqa: E501 + right_regex, # ,".*?" + r'(?:^|\n)(?P["\']).*?(?P=quote)(?:$|\n)'): # ".*?" (no delim, no space) # noqa: E501 + regexp = re.compile(restr, re.DOTALL | re.MULTILINE) + matches = regexp.findall(sniff_line) + if matches: + break + + # If it does not match the expression that was bugged, + # then this bug does not apply + if restr != right_regex: + return + + groupindex = regexp.groupindex + + # There is only one end of the string + assert len(matches) == 1 + m = matches[0] + + n = groupindex['quote'] - 1 + quote = m[n] + + n = groupindex['delim'] - 1 + delim = m[n] + + n = groupindex['space'] - 1 + space = bool(m[n]) + + dq_regexp = re.compile( + rf"(({re.escape(delim)})|^)\W*{quote}[^{re.escape(delim)}\n]*{quote}[^{re.escape(delim)}\n]*{quote}\W*(({re.escape(delim)})|$)", re.MULTILINE # noqa: E501 + ) + + doublequote = bool(dq_regexp.search(sniff_line)) + + dialect.quotechar = quote + if delim in delimiters: + dialect.delimiter = delim + dialect.doublequote = doublequote + dialect.skipinitialspace = space + + +def split_data_line(line, dialect=None): + delimiters = ",\t" + + # This can not be done in a per reader basis, and relational fields + # can be HUGE + csv.field_size_limit(int(ctypes.c_ulong(-1).value // 2)) + + # Remove the line end if any + if line[-1] == '\n': + line = line[:-1] + + # Remove potential trailing whitespace + line = line.strip() + + sniff_line = line + + # Add a delimiter if none is present, so that the csv.Sniffer + # does not complain for a single-field CSV. + if not any(d in line for d in delimiters): + sniff_line += "," + + if dialect is None: + dialect = csv.Sniffer().sniff(sniff_line, delimiters=delimiters) + workaround_csv_sniffer_bug_last_field(sniff_line=sniff_line, + dialect=dialect, + delimiters=delimiters) + + row = next(csv.reader([line], dialect)) + + return row, dialect + + +# -------------- +# Parsing header +# -------------- +def tokenize_attribute(iterable, attribute): + """Parse a raw string in header (e.g., starts by @attribute). + + Given a raw string attribute, try to get the name and type of the + attribute. Constraints: + + * The first line must start with @attribute (case insensitive, and + space like characters before @attribute are allowed) + * Works also if the attribute is spread on multilines. + * Works if empty lines or comments are in between + + Parameters + ---------- + attribute : str + the attribute string. + + Returns + ------- + name : str + name of the attribute + value : str + value of the attribute + next : str + next line to be parsed + + Examples + -------- + If attribute is a string defined in python as r"floupi real", will + return floupi as name, and real as value. + + >>> from scipy.io.arff._arffread import tokenize_attribute + >>> iterable = iter([0] * 10) # dummy iterator + >>> tokenize_attribute(iterable, r"@attribute floupi real") + ('floupi', 'real', 0) + + If attribute is r"'floupi 2' real", will return 'floupi 2' as name, + and real as value. + + >>> tokenize_attribute(iterable, r" @attribute 'floupi 2' real ") + ('floupi 2', 'real', 0) + + """ + sattr = attribute.strip() + mattr = r_attribute.match(sattr) + if mattr: + # atrv is everything after @attribute + atrv = mattr.group(1) + if r_comattrval.match(atrv): + name, type = tokenize_single_comma(atrv) + next_item = next(iterable) + elif r_wcomattrval.match(atrv): + name, type = tokenize_single_wcomma(atrv) + next_item = next(iterable) + else: + # Not sure we should support this, as it does not seem supported by + # weka. + raise ValueError("multi line not supported yet") + else: + raise ValueError(f"First line unparsable: {sattr}") + + attribute = to_attribute(name, type) + + if type.lower() == 'relational': + next_item = read_relational_attribute(iterable, attribute, next_item) + # raise ValueError("relational attributes not supported yet") + + return attribute, next_item + + +def tokenize_single_comma(val): + # XXX we match twice the same string (here and at the caller level). It is + # stupid, but it is easier for now... + m = r_comattrval.match(val) + if m: + try: + name = m.group(1).strip() + type = m.group(2).strip() + except IndexError as e: + raise ValueError("Error while tokenizing attribute") from e + else: + raise ValueError(f"Error while tokenizing single {val}") + return name, type + + +def tokenize_single_wcomma(val): + # XXX we match twice the same string (here and at the caller level). It is + # stupid, but it is easier for now... + m = r_wcomattrval.match(val) + if m: + try: + name = m.group(1).strip() + type = m.group(2).strip() + except IndexError as e: + raise ValueError("Error while tokenizing attribute") from e + else: + raise ValueError(f"Error while tokenizing single {val}") + return name, type + + +def read_relational_attribute(ofile, relational_attribute, i): + """Read the nested attributes of a relational attribute""" + + r_end_relational = re.compile(r'^@[Ee][Nn][Dd]\s*' + + relational_attribute.name + r'\s*$') + + while not r_end_relational.match(i): + m = r_headerline.match(i) + if m: + isattr = r_attribute.match(i) + if isattr: + attr, i = tokenize_attribute(ofile, i) + relational_attribute.attributes.append(attr) + else: + raise ValueError(f"Error parsing line {i}") + else: + i = next(ofile) + + i = next(ofile) + return i + + +def read_header(ofile): + """Read the header of the iterable ofile.""" + i = next(ofile) + + # Pass first comments + while r_comment.match(i): + i = next(ofile) + + # Header is everything up to DATA attribute ? + relation = None + attributes = [] + while not r_datameta.match(i): + m = r_headerline.match(i) + if m: + isattr = r_attribute.match(i) + if isattr: + attr, i = tokenize_attribute(ofile, i) + attributes.append(attr) + else: + isrel = r_relation.match(i) + if isrel: + relation = isrel.group(1) + else: + raise ValueError(f"Error parsing line {i}") + i = next(ofile) + else: + i = next(ofile) + + return relation, attributes + + +class MetaData: + """Small container to keep useful information on a ARFF dataset. + + Knows about attributes names and types. + + Examples + -------- + :: + + data, meta = loadarff('iris.arff') + # This will print the attributes names of the iris.arff dataset + for i in meta: + print(i) + # This works too + meta.names() + # Getting attribute type + types = meta.types() + + Methods + ------- + names + types + + Notes + ----- + Also maintains the list of attributes in order, i.e., doing for i in + meta, where meta is an instance of MetaData, will return the + different attribute names in the order they were defined. + """ + def __init__(self, rel, attr): + self.name = rel + self._attributes = {a.name: a for a in attr} + + def __repr__(self): + msg = "" + msg += f"Dataset: {self.name}\n" + for i in self._attributes: + msg += f"\t{i}'s type is {self._attributes[i].type_name}" + if self._attributes[i].range: + msg += f", range is {str(self._attributes[i].range)}" + msg += '\n' + return msg + + def __iter__(self): + return iter(self._attributes) + + def __getitem__(self, key): + attr = self._attributes[key] + + return (attr.type_name, attr.range) + + def names(self): + """Return the list of attribute names. + + Returns + ------- + attrnames : list of str + The attribute names. + """ + return list(self._attributes) + + def types(self): + """Return the list of attribute types. + + Returns + ------- + attr_types : list of str + The attribute types. + """ + attr_types = [self._attributes[name].type_name + for name in self._attributes] + return attr_types + + +def loadarff(f): + """ + Read an arff file. + + The data is returned as a record array, which can be accessed much like + a dictionary of NumPy arrays. For example, if one of the attributes is + called 'pressure', then its first 10 data points can be accessed from the + ``data`` record array like so: ``data['pressure'][0:10]`` + + + Parameters + ---------- + f : file-like or str + File-like object to read from, or filename to open. + + Returns + ------- + data : record array + The data of the arff file, accessible by attribute names. + meta : `MetaData` + Contains information about the arff file such as name and + type of attributes, the relation (name of the dataset), etc. + + Raises + ------ + ParseArffError + This is raised if the given file is not ARFF-formatted. + NotImplementedError + The ARFF file has an attribute which is not supported yet. + + Notes + ----- + + This function should be able to read most arff files. Not + implemented functionality include: + + * date type attributes + * string type attributes + + It can read files with numeric and nominal attributes. It cannot read + files with sparse data ({} in the file). However, this function can + read files with missing data (? in the file), representing the data + points as NaNs. + + Examples + -------- + >>> from scipy.io import arff + >>> from io import StringIO + >>> content = \"\"\" + ... @relation foo + ... @attribute width numeric + ... @attribute height numeric + ... @attribute color {red,green,blue,yellow,black} + ... @data + ... 5.0,3.25,blue + ... 4.5,3.75,green + ... 3.0,4.00,red + ... \"\"\" + >>> f = StringIO(content) + >>> data, meta = arff.loadarff(f) + >>> data + array([(5.0, 3.25, 'blue'), (4.5, 3.75, 'green'), (3.0, 4.0, 'red')], + dtype=[('width', '>> meta + Dataset: foo + \twidth's type is numeric + \theight's type is numeric + \tcolor's type is nominal, range is ('red', 'green', 'blue', 'yellow', 'black') + + """ + if hasattr(f, 'read'): + ofile = f + else: + ofile = open(f) + try: + return _loadarff(ofile) + finally: + if ofile is not f: # only close what we opened + ofile.close() + + +def _loadarff(ofile): + # Parse the header file + try: + rel, attr = read_header(ofile) + except ValueError as e: + msg = "Error while parsing header, error was: " + str(e) + raise ParseArffError(msg) from e + + # Check whether we have a string attribute (not supported yet) + hasstr = False + for a in attr: + if isinstance(a, StringAttribute): + hasstr = True + + meta = MetaData(rel, attr) + + # XXX The following code is not great + # Build the type descriptor descr and the list of converters to convert + # each attribute to the suitable type (which should match the one in + # descr). + + # This can be used once we want to support integer as integer values and + # not as numeric anymore (using masked arrays ?). + + if hasstr: + # How to support string efficiently ? Ideally, we should know the max + # size of the string before allocating the numpy array. + raise NotImplementedError("String attributes not supported yet, sorry") + + ni = len(attr) + + def generator(row_iter, delim=','): + # TODO: this is where we are spending time (~80%). I think things + # could be made more efficiently: + # - We could for example "compile" the function, because some values + # do not change here. + # - The function to convert a line to dtyped values could also be + # generated on the fly from a string and be executed instead of + # looping. + # - The regex are overkill: for comments, checking that a line starts + # by % should be enough and faster, and for empty lines, same thing + # --> this does not seem to change anything. + + # 'compiling' the range since it does not change + # Note, I have already tried zipping the converters and + # row elements and got slightly worse performance. + elems = list(range(ni)) + + dialect = None + for raw in row_iter: + # We do not abstract skipping comments and empty lines for + # performance reasons. + if r_comment.match(raw) or r_empty.match(raw): + continue + + row, dialect = split_data_line(raw, dialect) + + yield tuple([attr[i].parse_data(row[i]) for i in elems]) + + a = list(generator(ofile)) + # No error should happen here: it is a bug otherwise + data = np.array(a, [(a.name, a.dtype) for a in attr]) + return data, meta + diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/arffread.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/arffread.py new file mode 100644 index 0000000000000000000000000000000000000000..c42ae31db6bde3987bd059cc7451d1ae87f0073c --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/arffread.py @@ -0,0 +1,19 @@ +# This file is not meant for public use and will be removed in SciPy v2.0.0. +# Use the `scipy.io.arff` namespace for importing the functions +# included below. + +from scipy._lib.deprecation import _sub_module_deprecation + +__all__ = [ # noqa: F822 + 'MetaData', 'loadarff', 'ArffError', 'ParseArffError', +] + + +def __dir__(): + return __all__ + + +def __getattr__(name): + return _sub_module_deprecation(sub_package="io.arff", module="arffread", + private_modules=["_arffread"], all=__all__, + attribute=name) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/iris.arff b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/iris.arff new file mode 100644 index 0000000000000000000000000000000000000000..780480c7c6b9a68bf71aaf357c7d3f7a5b3b3f57 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/iris.arff @@ -0,0 +1,225 @@ +% 1. Title: Iris Plants Database +% +% 2. Sources: +% (a) Creator: R.A. Fisher +% (b) Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov) +% (c) Date: July, 1988 +% +% 3. Past Usage: +% - Publications: too many to mention!!! Here are a few. +% 1. Fisher,R.A. "The use of multiple measurements in taxonomic problems" +% Annual Eugenics, 7, Part II, 179-188 (1936); also in "Contributions +% to Mathematical Statistics" (John Wiley, NY, 1950). +% 2. Duda,R.O., & Hart,P.E. (1973) Pattern Classification and Scene Analysis. +% (Q327.D83) John Wiley & Sons. ISBN 0-471-22361-1. See page 218. +% 3. Dasarathy, B.V. (1980) "Nosing Around the Neighborhood: A New System +% Structure and Classification Rule for Recognition in Partially Exposed +% Environments". IEEE Transactions on Pattern Analysis and Machine +% Intelligence, Vol. PAMI-2, No. 1, 67-71. +% -- Results: +% -- very low misclassification rates (0% for the setosa class) +% 4. Gates, G.W. (1972) "The Reduced Nearest Neighbor Rule". IEEE +% Transactions on Information Theory, May 1972, 431-433. +% -- Results: +% -- very low misclassification rates again +% 5. See also: 1988 MLC Proceedings, 54-64. Cheeseman et al's AUTOCLASS II +% conceptual clustering system finds 3 classes in the data. +% +% 4. Relevant Information: +% --- This is perhaps the best known database to be found in the pattern +% recognition literature. Fisher's paper is a classic in the field +% and is referenced frequently to this day. (See Duda & Hart, for +% example.) The data set contains 3 classes of 50 instances each, +% where each class refers to a type of iris plant. One class is +% linearly separable from the other 2; the latter are NOT linearly +% separable from each other. +% --- Predicted attribute: class of iris plant. +% --- This is an exceedingly simple domain. +% +% 5. Number of Instances: 150 (50 in each of three classes) +% +% 6. Number of Attributes: 4 numeric, predictive attributes and the class +% +% 7. Attribute Information: +% 1. sepal length in cm +% 2. sepal width in cm +% 3. petal length in cm +% 4. petal width in cm +% 5. class: +% -- Iris Setosa +% -- Iris Versicolour +% -- Iris Virginica +% +% 8. Missing Attribute Values: None +% +% Summary Statistics: +% Min Max Mean SD Class Correlation +% sepal length: 4.3 7.9 5.84 0.83 0.7826 +% sepal width: 2.0 4.4 3.05 0.43 -0.4194 +% petal length: 1.0 6.9 3.76 1.76 0.9490 (high!) +% petal width: 0.1 2.5 1.20 0.76 0.9565 (high!) +% +% 9. Class Distribution: 33.3% for each of 3 classes. + +@RELATION iris + +@ATTRIBUTE sepallength REAL +@ATTRIBUTE sepalwidth REAL +@ATTRIBUTE petallength REAL +@ATTRIBUTE petalwidth REAL +@ATTRIBUTE class {Iris-setosa,Iris-versicolor,Iris-virginica} + +@DATA +5.1,3.5,1.4,0.2,Iris-setosa +4.9,3.0,1.4,0.2,Iris-setosa +4.7,3.2,1.3,0.2,Iris-setosa +4.6,3.1,1.5,0.2,Iris-setosa +5.0,3.6,1.4,0.2,Iris-setosa +5.4,3.9,1.7,0.4,Iris-setosa +4.6,3.4,1.4,0.3,Iris-setosa +5.0,3.4,1.5,0.2,Iris-setosa +4.4,2.9,1.4,0.2,Iris-setosa +4.9,3.1,1.5,0.1,Iris-setosa +5.4,3.7,1.5,0.2,Iris-setosa +4.8,3.4,1.6,0.2,Iris-setosa +4.8,3.0,1.4,0.1,Iris-setosa +4.3,3.0,1.1,0.1,Iris-setosa +5.8,4.0,1.2,0.2,Iris-setosa +5.7,4.4,1.5,0.4,Iris-setosa +5.4,3.9,1.3,0.4,Iris-setosa +5.1,3.5,1.4,0.3,Iris-setosa +5.7,3.8,1.7,0.3,Iris-setosa +5.1,3.8,1.5,0.3,Iris-setosa +5.4,3.4,1.7,0.2,Iris-setosa +5.1,3.7,1.5,0.4,Iris-setosa +4.6,3.6,1.0,0.2,Iris-setosa +5.1,3.3,1.7,0.5,Iris-setosa +4.8,3.4,1.9,0.2,Iris-setosa +5.0,3.0,1.6,0.2,Iris-setosa +5.0,3.4,1.6,0.4,Iris-setosa +5.2,3.5,1.5,0.2,Iris-setosa +5.2,3.4,1.4,0.2,Iris-setosa +4.7,3.2,1.6,0.2,Iris-setosa +4.8,3.1,1.6,0.2,Iris-setosa +5.4,3.4,1.5,0.4,Iris-setosa +5.2,4.1,1.5,0.1,Iris-setosa +5.5,4.2,1.4,0.2,Iris-setosa +4.9,3.1,1.5,0.1,Iris-setosa +5.0,3.2,1.2,0.2,Iris-setosa +5.5,3.5,1.3,0.2,Iris-setosa +4.9,3.1,1.5,0.1,Iris-setosa +4.4,3.0,1.3,0.2,Iris-setosa +5.1,3.4,1.5,0.2,Iris-setosa +5.0,3.5,1.3,0.3,Iris-setosa +4.5,2.3,1.3,0.3,Iris-setosa +4.4,3.2,1.3,0.2,Iris-setosa +5.0,3.5,1.6,0.6,Iris-setosa +5.1,3.8,1.9,0.4,Iris-setosa +4.8,3.0,1.4,0.3,Iris-setosa +5.1,3.8,1.6,0.2,Iris-setosa +4.6,3.2,1.4,0.2,Iris-setosa +5.3,3.7,1.5,0.2,Iris-setosa +5.0,3.3,1.4,0.2,Iris-setosa +7.0,3.2,4.7,1.4,Iris-versicolor +6.4,3.2,4.5,1.5,Iris-versicolor +6.9,3.1,4.9,1.5,Iris-versicolor +5.5,2.3,4.0,1.3,Iris-versicolor +6.5,2.8,4.6,1.5,Iris-versicolor +5.7,2.8,4.5,1.3,Iris-versicolor +6.3,3.3,4.7,1.6,Iris-versicolor +4.9,2.4,3.3,1.0,Iris-versicolor +6.6,2.9,4.6,1.3,Iris-versicolor +5.2,2.7,3.9,1.4,Iris-versicolor +5.0,2.0,3.5,1.0,Iris-versicolor +5.9,3.0,4.2,1.5,Iris-versicolor +6.0,2.2,4.0,1.0,Iris-versicolor +6.1,2.9,4.7,1.4,Iris-versicolor +5.6,2.9,3.6,1.3,Iris-versicolor +6.7,3.1,4.4,1.4,Iris-versicolor +5.6,3.0,4.5,1.5,Iris-versicolor +5.8,2.7,4.1,1.0,Iris-versicolor +6.2,2.2,4.5,1.5,Iris-versicolor +5.6,2.5,3.9,1.1,Iris-versicolor +5.9,3.2,4.8,1.8,Iris-versicolor +6.1,2.8,4.0,1.3,Iris-versicolor +6.3,2.5,4.9,1.5,Iris-versicolor +6.1,2.8,4.7,1.2,Iris-versicolor +6.4,2.9,4.3,1.3,Iris-versicolor +6.6,3.0,4.4,1.4,Iris-versicolor +6.8,2.8,4.8,1.4,Iris-versicolor +6.7,3.0,5.0,1.7,Iris-versicolor +6.0,2.9,4.5,1.5,Iris-versicolor +5.7,2.6,3.5,1.0,Iris-versicolor +5.5,2.4,3.8,1.1,Iris-versicolor +5.5,2.4,3.7,1.0,Iris-versicolor +5.8,2.7,3.9,1.2,Iris-versicolor +6.0,2.7,5.1,1.6,Iris-versicolor +5.4,3.0,4.5,1.5,Iris-versicolor +6.0,3.4,4.5,1.6,Iris-versicolor +6.7,3.1,4.7,1.5,Iris-versicolor +6.3,2.3,4.4,1.3,Iris-versicolor +5.6,3.0,4.1,1.3,Iris-versicolor +5.5,2.5,4.0,1.3,Iris-versicolor +5.5,2.6,4.4,1.2,Iris-versicolor +6.1,3.0,4.6,1.4,Iris-versicolor +5.8,2.6,4.0,1.2,Iris-versicolor +5.0,2.3,3.3,1.0,Iris-versicolor +5.6,2.7,4.2,1.3,Iris-versicolor +5.7,3.0,4.2,1.2,Iris-versicolor +5.7,2.9,4.2,1.3,Iris-versicolor +6.2,2.9,4.3,1.3,Iris-versicolor +5.1,2.5,3.0,1.1,Iris-versicolor +5.7,2.8,4.1,1.3,Iris-versicolor +6.3,3.3,6.0,2.5,Iris-virginica +5.8,2.7,5.1,1.9,Iris-virginica +7.1,3.0,5.9,2.1,Iris-virginica +6.3,2.9,5.6,1.8,Iris-virginica +6.5,3.0,5.8,2.2,Iris-virginica +7.6,3.0,6.6,2.1,Iris-virginica +4.9,2.5,4.5,1.7,Iris-virginica +7.3,2.9,6.3,1.8,Iris-virginica +6.7,2.5,5.8,1.8,Iris-virginica +7.2,3.6,6.1,2.5,Iris-virginica +6.5,3.2,5.1,2.0,Iris-virginica +6.4,2.7,5.3,1.9,Iris-virginica +6.8,3.0,5.5,2.1,Iris-virginica +5.7,2.5,5.0,2.0,Iris-virginica +5.8,2.8,5.1,2.4,Iris-virginica +6.4,3.2,5.3,2.3,Iris-virginica +6.5,3.0,5.5,1.8,Iris-virginica +7.7,3.8,6.7,2.2,Iris-virginica +7.7,2.6,6.9,2.3,Iris-virginica +6.0,2.2,5.0,1.5,Iris-virginica +6.9,3.2,5.7,2.3,Iris-virginica +5.6,2.8,4.9,2.0,Iris-virginica +7.7,2.8,6.7,2.0,Iris-virginica +6.3,2.7,4.9,1.8,Iris-virginica +6.7,3.3,5.7,2.1,Iris-virginica +7.2,3.2,6.0,1.8,Iris-virginica +6.2,2.8,4.8,1.8,Iris-virginica +6.1,3.0,4.9,1.8,Iris-virginica +6.4,2.8,5.6,2.1,Iris-virginica +7.2,3.0,5.8,1.6,Iris-virginica +7.4,2.8,6.1,1.9,Iris-virginica +7.9,3.8,6.4,2.0,Iris-virginica +6.4,2.8,5.6,2.2,Iris-virginica +6.3,2.8,5.1,1.5,Iris-virginica +6.1,2.6,5.6,1.4,Iris-virginica +7.7,3.0,6.1,2.3,Iris-virginica +6.3,3.4,5.6,2.4,Iris-virginica +6.4,3.1,5.5,1.8,Iris-virginica +6.0,3.0,4.8,1.8,Iris-virginica +6.9,3.1,5.4,2.1,Iris-virginica +6.7,3.1,5.6,2.4,Iris-virginica +6.9,3.1,5.1,2.3,Iris-virginica +5.8,2.7,5.1,1.9,Iris-virginica +6.8,3.2,5.9,2.3,Iris-virginica +6.7,3.3,5.7,2.5,Iris-virginica +6.7,3.0,5.2,2.3,Iris-virginica +6.3,2.5,5.0,1.9,Iris-virginica +6.5,3.0,5.2,2.0,Iris-virginica +6.2,3.4,5.4,2.3,Iris-virginica +5.9,3.0,5.1,1.8,Iris-virginica +% +% +% diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/missing.arff b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/missing.arff new file mode 100644 index 0000000000000000000000000000000000000000..dedc64c8fa2fcdc0081b30b7804be85114495ce2 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/missing.arff @@ -0,0 +1,8 @@ +% This arff file contains some missing data +@relation missing +@attribute yop real +@attribute yap real +@data +1,5 +2,4 +?,? diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/nodata.arff b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/nodata.arff new file mode 100644 index 0000000000000000000000000000000000000000..5766aeb229a1b31378026274c366e8e9e44fd487 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/nodata.arff @@ -0,0 +1,11 @@ +@RELATION iris + +@ATTRIBUTE sepallength REAL +@ATTRIBUTE sepalwidth REAL +@ATTRIBUTE petallength REAL +@ATTRIBUTE petalwidth REAL +@ATTRIBUTE class {Iris-setosa,Iris-versicolor,Iris-virginica} + +@DATA + +% This file has no data diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/quoted_nominal.arff b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/quoted_nominal.arff new file mode 100644 index 0000000000000000000000000000000000000000..7cd16d1ef9b50cc1194d034ef4d458ef3cf0d417 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/quoted_nominal.arff @@ -0,0 +1,13 @@ +% Regression test for issue #10232 : Exception in loadarff with quoted nominal attributes +% Spaces between elements are stripped by the parser + +@relation SOME_DATA +@attribute age numeric +@attribute smoker {'yes', 'no'} +@data +18, 'no' +24, 'yes' +44, 'no' +56, 'no' +89,'yes' +11, 'no' diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/quoted_nominal_spaces.arff b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/quoted_nominal_spaces.arff new file mode 100644 index 0000000000000000000000000000000000000000..c799127862b6060442b29c9a0382836cc9c55537 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/quoted_nominal_spaces.arff @@ -0,0 +1,13 @@ +% Regression test for issue #10232 : Exception in loadarff with quoted nominal attributes +% Spaces inside quotes are NOT stripped by the parser + +@relation SOME_DATA +@attribute age numeric +@attribute smoker {' yes', 'no '} +@data +18,'no ' +24,' yes' +44,'no ' +56,'no ' +89,' yes' +11,'no ' diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test1.arff b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test1.arff new file mode 100644 index 0000000000000000000000000000000000000000..ccc8e0cc7c43dc66ad7b3a8e4738c3322d3f79d8 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test1.arff @@ -0,0 +1,10 @@ +@RELATION test1 + +@ATTRIBUTE attr0 REAL +@ATTRIBUTE attr1 REAL +@ATTRIBUTE attr2 REAL +@ATTRIBUTE attr3 REAL +@ATTRIBUTE class {class0, class1, class2, class3} + +@DATA +0.1, 0.2, 0.3, 0.4,class1 diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test10.arff b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test10.arff new file mode 100644 index 0000000000000000000000000000000000000000..094ac5094a842866666726b358d2c66bf927c9d2 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test10.arff @@ -0,0 +1,8 @@ +@relation test9 + +@attribute attr_relational relational + @attribute attr_number integer +@end attr_relational + +@data +'0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40\n41\n42\n43\n44\n45\n46\n47\n48\n49\n50\n51\n52\n53\n54\n55\n56\n57\n58\n59\n60\n61\n62\n63\n64\n65\n66\n67\n68\n69\n70\n71\n72\n73\n74\n75\n76\n77\n78\n79\n80\n81\n82\n83\n84\n85\n86\n87\n88\n89\n90\n91\n92\n93\n94\n95\n96\n97\n98\n99\n100\n101\n102\n103\n104\n105\n106\n107\n108\n109\n110\n111\n112\n113\n114\n115\n116\n117\n118\n119\n120\n121\n122\n123\n124\n125\n126\n127\n128\n129\n130\n131\n132\n133\n134\n135\n136\n137\n138\n139\n140\n141\n142\n143\n144\n145\n146\n147\n148\n149\n150\n151\n152\n153\n154\n155\n156\n157\n158\n159\n160\n161\n162\n163\n164\n165\n166\n167\n168\n169\n170\n171\n172\n173\n174\n175\n176\n177\n178\n179\n180\n181\n182\n183\n184\n185\n186\n187\n188\n189\n190\n191\n192\n193\n194\n195\n196\n197\n198\n199\n200\n201\n202\n203\n204\n205\n206\n207\n208\n209\n210\n211\n212\n213\n214\n215\n216\n217\n218\n219\n220\n221\n222\n223\n224\n225\n226\n227\n228\n229\n230\n231\n232\n233\n234\n235\n236\n237\n238\n239\n240\n241\n242\n243\n244\n245\n246\n247\n248\n249\n250\n251\n252\n253\n254\n255\n256\n257\n258\n259\n260\n261\n262\n263\n264\n265\n266\n267\n268\n269\n270\n271\n272\n273\n274\n275\n276\n277\n278\n279\n280\n281\n282\n283\n284\n285\n286\n287\n288\n289\n290\n291\n292\n293\n294\n295\n296\n297\n298\n299\n300\n301\n302\n303\n304\n305\n306\n307\n308\n309\n310\n311\n312\n313\n314\n315\n316\n317\n318\n319\n320\n321\n322\n323\n324\n325\n326\n327\n328\n329\n330\n331\n332\n333\n334\n335\n336\n337\n338\n339\n340\n341\n342\n343\n344\n345\n346\n347\n348\n349\n350\n351\n352\n353\n354\n355\n356\n357\n358\n359\n360\n361\n362\n363\n364\n365\n366\n367\n368\n369\n370\n371\n372\n373\n374\n375\n376\n377\n378\n379\n380\n381\n382\n383\n384\n385\n386\n387\n388\n389\n390\n391\n392\n393\n394\n395\n396\n397\n398\n399\n400\n401\n402\n403\n404\n405\n406\n407\n408\n409\n410\n411\n412\n413\n414\n415\n416\n417\n418\n419\n420\n421\n422\n423\n424\n425\n426\n427\n428\n429\n430\n431\n432\n433\n434\n435\n436\n437\n438\n439\n440\n441\n442\n443\n444\n445\n446\n447\n448\n449\n450\n451\n452\n453\n454\n455\n456\n457\n458\n459\n460\n461\n462\n463\n464\n465\n466\n467\n468\n469\n470\n471\n472\n473\n474\n475\n476\n477\n478\n479\n480\n481\n482\n483\n484\n485\n486\n487\n488\n489\n490\n491\n492\n493\n494\n495\n496\n497\n498\n499\n500\n501\n502\n503\n504\n505\n506\n507\n508\n509\n510\n511\n512\n513\n514\n515\n516\n517\n518\n519\n520\n521\n522\n523\n524\n525\n526\n527\n528\n529\n530\n531\n532\n533\n534\n535\n536\n537\n538\n539\n540\n541\n542\n543\n544\n545\n546\n547\n548\n549\n550\n551\n552\n553\n554\n555\n556\n557\n558\n559\n560\n561\n562\n563\n564\n565\n566\n567\n568\n569\n570\n571\n572\n573\n574\n575\n576\n577\n578\n579\n580\n581\n582\n583\n584\n585\n586\n587\n588\n589\n590\n591\n592\n593\n594\n595\n596\n597\n598\n599\n600\n601\n602\n603\n604\n605\n606\n607\n608\n609\n610\n611\n612\n613\n614\n615\n616\n617\n618\n619\n620\n621\n622\n623\n624\n625\n626\n627\n628\n629\n630\n631\n632\n633\n634\n635\n636\n637\n638\n639\n640\n641\n642\n643\n644\n645\n646\n647\n648\n649\n650\n651\n652\n653\n654\n655\n656\n657\n658\n659\n660\n661\n662\n663\n664\n665\n666\n667\n668\n669\n670\n671\n672\n673\n674\n675\n676\n677\n678\n679\n680\n681\n682\n683\n684\n685\n686\n687\n688\n689\n690\n691\n692\n693\n694\n695\n696\n697\n698\n699\n700\n701\n702\n703\n704\n705\n706\n707\n708\n709\n710\n711\n712\n713\n714\n715\n716\n717\n718\n719\n720\n721\n722\n723\n724\n725\n726\n727\n728\n729\n730\n731\n732\n733\n734\n735\n736\n737\n738\n739\n740\n741\n742\n743\n744\n745\n746\n747\n748\n749\n750\n751\n752\n753\n754\n755\n756\n757\n758\n759\n760\n761\n762\n763\n764\n765\n766\n767\n768\n769\n770\n771\n772\n773\n774\n775\n776\n777\n778\n779\n780\n781\n782\n783\n784\n785\n786\n787\n788\n789\n790\n791\n792\n793\n794\n795\n796\n797\n798\n799\n800\n801\n802\n803\n804\n805\n806\n807\n808\n809\n810\n811\n812\n813\n814\n815\n816\n817\n818\n819\n820\n821\n822\n823\n824\n825\n826\n827\n828\n829\n830\n831\n832\n833\n834\n835\n836\n837\n838\n839\n840\n841\n842\n843\n844\n845\n846\n847\n848\n849\n850\n851\n852\n853\n854\n855\n856\n857\n858\n859\n860\n861\n862\n863\n864\n865\n866\n867\n868\n869\n870\n871\n872\n873\n874\n875\n876\n877\n878\n879\n880\n881\n882\n883\n884\n885\n886\n887\n888\n889\n890\n891\n892\n893\n894\n895\n896\n897\n898\n899\n900\n901\n902\n903\n904\n905\n906\n907\n908\n909\n910\n911\n912\n913\n914\n915\n916\n917\n918\n919\n920\n921\n922\n923\n924\n925\n926\n927\n928\n929\n930\n931\n932\n933\n934\n935\n936\n937\n938\n939\n940\n941\n942\n943\n944\n945\n946\n947\n948\n949\n950\n951\n952\n953\n954\n955\n956\n957\n958\n959\n960\n961\n962\n963\n964\n965\n966\n967\n968\n969\n970\n971\n972\n973\n974\n975\n976\n977\n978\n979\n980\n981\n982\n983\n984\n985\n986\n987\n988\n989\n990\n991\n992\n993\n994\n995\n996\n997\n998\n999\n1000\n1001\n1002\n1003\n1004\n1005\n1006\n1007\n1008\n1009\n1010\n1011\n1012\n1013\n1014\n1015\n1016\n1017\n1018\n1019\n1020\n1021\n1022\n1023\n1024\n1025\n1026\n1027\n1028\n1029\n1030\n1031\n1032\n1033\n1034\n1035\n1036\n1037\n1038\n1039\n1040\n1041\n1042\n1043\n1044\n1045\n1046\n1047\n1048\n1049\n1050\n1051\n1052\n1053\n1054\n1055\n1056\n1057\n1058\n1059\n1060\n1061\n1062\n1063\n1064\n1065\n1066\n1067\n1068\n1069\n1070\n1071\n1072\n1073\n1074\n1075\n1076\n1077\n1078\n1079\n1080\n1081\n1082\n1083\n1084\n1085\n1086\n1087\n1088\n1089\n1090\n1091\n1092\n1093\n1094\n1095\n1096\n1097\n1098\n1099\n1100\n1101\n1102\n1103\n1104\n1105\n1106\n1107\n1108\n1109\n1110\n1111\n1112\n1113\n1114\n1115\n1116\n1117\n1118\n1119\n1120\n1121\n1122\n1123\n1124\n1125\n1126\n1127\n1128\n1129\n1130\n1131\n1132\n1133\n1134\n1135\n1136\n1137\n1138\n1139\n1140\n1141\n1142\n1143\n1144\n1145\n1146\n1147\n1148\n1149\n1150\n1151\n1152\n1153\n1154\n1155\n1156\n1157\n1158\n1159\n1160\n1161\n1162\n1163\n1164\n1165\n1166\n1167\n1168\n1169\n1170\n1171\n1172\n1173\n1174\n1175\n1176\n1177\n1178\n1179\n1180\n1181\n1182\n1183\n1184\n1185\n1186\n1187\n1188\n1189\n1190\n1191\n1192\n1193\n1194\n1195\n1196\n1197\n1198\n1199\n1200\n1201\n1202\n1203\n1204\n1205\n1206\n1207\n1208\n1209\n1210\n1211\n1212\n1213\n1214\n1215\n1216\n1217\n1218\n1219\n1220\n1221\n1222\n1223\n1224\n1225\n1226\n1227\n1228\n1229\n1230\n1231\n1232\n1233\n1234\n1235\n1236\n1237\n1238\n1239\n1240\n1241\n1242\n1243\n1244\n1245\n1246\n1247\n1248\n1249\n1250\n1251\n1252\n1253\n1254\n1255\n1256\n1257\n1258\n1259\n1260\n1261\n1262\n1263\n1264\n1265\n1266\n1267\n1268\n1269\n1270\n1271\n1272\n1273\n1274\n1275\n1276\n1277\n1278\n1279\n1280\n1281\n1282\n1283\n1284\n1285\n1286\n1287\n1288\n1289\n1290\n1291\n1292\n1293\n1294\n1295\n1296\n1297\n1298\n1299\n1300\n1301\n1302\n1303\n1304\n1305\n1306\n1307\n1308\n1309\n1310\n1311\n1312\n1313\n1314\n1315\n1316\n1317\n1318\n1319\n1320\n1321\n1322\n1323\n1324\n1325\n1326\n1327\n1328\n1329\n1330\n1331\n1332\n1333\n1334\n1335\n1336\n1337\n1338\n1339\n1340\n1341\n1342\n1343\n1344\n1345\n1346\n1347\n1348\n1349\n1350\n1351\n1352\n1353\n1354\n1355\n1356\n1357\n1358\n1359\n1360\n1361\n1362\n1363\n1364\n1365\n1366\n1367\n1368\n1369\n1370\n1371\n1372\n1373\n1374\n1375\n1376\n1377\n1378\n1379\n1380\n1381\n1382\n1383\n1384\n1385\n1386\n1387\n1388\n1389\n1390\n1391\n1392\n1393\n1394\n1395\n1396\n1397\n1398\n1399\n1400\n1401\n1402\n1403\n1404\n1405\n1406\n1407\n1408\n1409\n1410\n1411\n1412\n1413\n1414\n1415\n1416\n1417\n1418\n1419\n1420\n1421\n1422\n1423\n1424\n1425\n1426\n1427\n1428\n1429\n1430\n1431\n1432\n1433\n1434\n1435\n1436\n1437\n1438\n1439\n1440\n1441\n1442\n1443\n1444\n1445\n1446\n1447\n1448\n1449\n1450\n1451\n1452\n1453\n1454\n1455\n1456\n1457\n1458\n1459\n1460\n1461\n1462\n1463\n1464\n1465\n1466\n1467\n1468\n1469\n1470\n1471\n1472\n1473\n1474\n1475\n1476\n1477\n1478\n1479\n1480\n1481\n1482\n1483\n1484\n1485\n1486\n1487\n1488\n1489\n1490\n1491\n1492\n1493\n1494\n1495\n1496\n1497\n1498\n1499\n1500\n1501\n1502\n1503\n1504\n1505\n1506\n1507\n1508\n1509\n1510\n1511\n1512\n1513\n1514\n1515\n1516\n1517\n1518\n1519\n1520\n1521\n1522\n1523\n1524\n1525\n1526\n1527\n1528\n1529\n1530\n1531\n1532\n1533\n1534\n1535\n1536\n1537\n1538\n1539\n1540\n1541\n1542\n1543\n1544\n1545\n1546\n1547\n1548\n1549\n1550\n1551\n1552\n1553\n1554\n1555\n1556\n1557\n1558\n1559\n1560\n1561\n1562\n1563\n1564\n1565\n1566\n1567\n1568\n1569\n1570\n1571\n1572\n1573\n1574\n1575\n1576\n1577\n1578\n1579\n1580\n1581\n1582\n1583\n1584\n1585\n1586\n1587\n1588\n1589\n1590\n1591\n1592\n1593\n1594\n1595\n1596\n1597\n1598\n1599\n1600\n1601\n1602\n1603\n1604\n1605\n1606\n1607\n1608\n1609\n1610\n1611\n1612\n1613\n1614\n1615\n1616\n1617\n1618\n1619\n1620\n1621\n1622\n1623\n1624\n1625\n1626\n1627\n1628\n1629\n1630\n1631\n1632\n1633\n1634\n1635\n1636\n1637\n1638\n1639\n1640\n1641\n1642\n1643\n1644\n1645\n1646\n1647\n1648\n1649\n1650\n1651\n1652\n1653\n1654\n1655\n1656\n1657\n1658\n1659\n1660\n1661\n1662\n1663\n1664\n1665\n1666\n1667\n1668\n1669\n1670\n1671\n1672\n1673\n1674\n1675\n1676\n1677\n1678\n1679\n1680\n1681\n1682\n1683\n1684\n1685\n1686\n1687\n1688\n1689\n1690\n1691\n1692\n1693\n1694\n1695\n1696\n1697\n1698\n1699\n1700\n1701\n1702\n1703\n1704\n1705\n1706\n1707\n1708\n1709\n1710\n1711\n1712\n1713\n1714\n1715\n1716\n1717\n1718\n1719\n1720\n1721\n1722\n1723\n1724\n1725\n1726\n1727\n1728\n1729\n1730\n1731\n1732\n1733\n1734\n1735\n1736\n1737\n1738\n1739\n1740\n1741\n1742\n1743\n1744\n1745\n1746\n1747\n1748\n1749\n1750\n1751\n1752\n1753\n1754\n1755\n1756\n1757\n1758\n1759\n1760\n1761\n1762\n1763\n1764\n1765\n1766\n1767\n1768\n1769\n1770\n1771\n1772\n1773\n1774\n1775\n1776\n1777\n1778\n1779\n1780\n1781\n1782\n1783\n1784\n1785\n1786\n1787\n1788\n1789\n1790\n1791\n1792\n1793\n1794\n1795\n1796\n1797\n1798\n1799\n1800\n1801\n1802\n1803\n1804\n1805\n1806\n1807\n1808\n1809\n1810\n1811\n1812\n1813\n1814\n1815\n1816\n1817\n1818\n1819\n1820\n1821\n1822\n1823\n1824\n1825\n1826\n1827\n1828\n1829\n1830\n1831\n1832\n1833\n1834\n1835\n1836\n1837\n1838\n1839\n1840\n1841\n1842\n1843\n1844\n1845\n1846\n1847\n1848\n1849\n1850\n1851\n1852\n1853\n1854\n1855\n1856\n1857\n1858\n1859\n1860\n1861\n1862\n1863\n1864\n1865\n1866\n1867\n1868\n1869\n1870\n1871\n1872\n1873\n1874\n1875\n1876\n1877\n1878\n1879\n1880\n1881\n1882\n1883\n1884\n1885\n1886\n1887\n1888\n1889\n1890\n1891\n1892\n1893\n1894\n1895\n1896\n1897\n1898\n1899\n1900\n1901\n1902\n1903\n1904\n1905\n1906\n1907\n1908\n1909\n1910\n1911\n1912\n1913\n1914\n1915\n1916\n1917\n1918\n1919\n1920\n1921\n1922\n1923\n1924\n1925\n1926\n1927\n1928\n1929\n1930\n1931\n1932\n1933\n1934\n1935\n1936\n1937\n1938\n1939\n1940\n1941\n1942\n1943\n1944\n1945\n1946\n1947\n1948\n1949\n1950\n1951\n1952\n1953\n1954\n1955\n1956\n1957\n1958\n1959\n1960\n1961\n1962\n1963\n1964\n1965\n1966\n1967\n1968\n1969\n1970\n1971\n1972\n1973\n1974\n1975\n1976\n1977\n1978\n1979\n1980\n1981\n1982\n1983\n1984\n1985\n1986\n1987\n1988\n1989\n1990\n1991\n1992\n1993\n1994\n1995\n1996\n1997\n1998\n1999\n2000\n2001\n2002\n2003\n2004\n2005\n2006\n2007\n2008\n2009\n2010\n2011\n2012\n2013\n2014\n2015\n2016\n2017\n2018\n2019\n2020\n2021\n2022\n2023\n2024\n2025\n2026\n2027\n2028\n2029\n2030\n2031\n2032\n2033\n2034\n2035\n2036\n2037\n2038\n2039\n2040\n2041\n2042\n2043\n2044\n2045\n2046\n2047\n2048\n2049\n2050\n2051\n2052\n2053\n2054\n2055\n2056\n2057\n2058\n2059\n2060\n2061\n2062\n2063\n2064\n2065\n2066\n2067\n2068\n2069\n2070\n2071\n2072\n2073\n2074\n2075\n2076\n2077\n2078\n2079\n2080\n2081\n2082\n2083\n2084\n2085\n2086\n2087\n2088\n2089\n2090\n2091\n2092\n2093\n2094\n2095\n2096\n2097\n2098\n2099\n2100\n2101\n2102\n2103\n2104\n2105\n2106\n2107\n2108\n2109\n2110\n2111\n2112\n2113\n2114\n2115\n2116\n2117\n2118\n2119\n2120\n2121\n2122\n2123\n2124\n2125\n2126\n2127\n2128\n2129\n2130\n2131\n2132\n2133\n2134\n2135\n2136\n2137\n2138\n2139\n2140\n2141\n2142\n2143\n2144\n2145\n2146\n2147\n2148\n2149\n2150\n2151\n2152\n2153\n2154\n2155\n2156\n2157\n2158\n2159\n2160\n2161\n2162\n2163\n2164\n2165\n2166\n2167\n2168\n2169\n2170\n2171\n2172\n2173\n2174\n2175\n2176\n2177\n2178\n2179\n2180\n2181\n2182\n2183\n2184\n2185\n2186\n2187\n2188\n2189\n2190\n2191\n2192\n2193\n2194\n2195\n2196\n2197\n2198\n2199\n2200\n2201\n2202\n2203\n2204\n2205\n2206\n2207\n2208\n2209\n2210\n2211\n2212\n2213\n2214\n2215\n2216\n2217\n2218\n2219\n2220\n2221\n2222\n2223\n2224\n2225\n2226\n2227\n2228\n2229\n2230\n2231\n2232\n2233\n2234\n2235\n2236\n2237\n2238\n2239\n2240\n2241\n2242\n2243\n2244\n2245\n2246\n2247\n2248\n2249\n2250\n2251\n2252\n2253\n2254\n2255\n2256\n2257\n2258\n2259\n2260\n2261\n2262\n2263\n2264\n2265\n2266\n2267\n2268\n2269\n2270\n2271\n2272\n2273\n2274\n2275\n2276\n2277\n2278\n2279\n2280\n2281\n2282\n2283\n2284\n2285\n2286\n2287\n2288\n2289\n2290\n2291\n2292\n2293\n2294\n2295\n2296\n2297\n2298\n2299\n2300\n2301\n2302\n2303\n2304\n2305\n2306\n2307\n2308\n2309\n2310\n2311\n2312\n2313\n2314\n2315\n2316\n2317\n2318\n2319\n2320\n2321\n2322\n2323\n2324\n2325\n2326\n2327\n2328\n2329\n2330\n2331\n2332\n2333\n2334\n2335\n2336\n2337\n2338\n2339\n2340\n2341\n2342\n2343\n2344\n2345\n2346\n2347\n2348\n2349\n2350\n2351\n2352\n2353\n2354\n2355\n2356\n2357\n2358\n2359\n2360\n2361\n2362\n2363\n2364\n2365\n2366\n2367\n2368\n2369\n2370\n2371\n2372\n2373\n2374\n2375\n2376\n2377\n2378\n2379\n2380\n2381\n2382\n2383\n2384\n2385\n2386\n2387\n2388\n2389\n2390\n2391\n2392\n2393\n2394\n2395\n2396\n2397\n2398\n2399\n2400\n2401\n2402\n2403\n2404\n2405\n2406\n2407\n2408\n2409\n2410\n2411\n2412\n2413\n2414\n2415\n2416\n2417\n2418\n2419\n2420\n2421\n2422\n2423\n2424\n2425\n2426\n2427\n2428\n2429\n2430\n2431\n2432\n2433\n2434\n2435\n2436\n2437\n2438\n2439\n2440\n2441\n2442\n2443\n2444\n2445\n2446\n2447\n2448\n2449\n2450\n2451\n2452\n2453\n2454\n2455\n2456\n2457\n2458\n2459\n2460\n2461\n2462\n2463\n2464\n2465\n2466\n2467\n2468\n2469\n2470\n2471\n2472\n2473\n2474\n2475\n2476\n2477\n2478\n2479\n2480\n2481\n2482\n2483\n2484\n2485\n2486\n2487\n2488\n2489\n2490\n2491\n2492\n2493\n2494\n2495\n2496\n2497\n2498\n2499\n2500\n2501\n2502\n2503\n2504\n2505\n2506\n2507\n2508\n2509\n2510\n2511\n2512\n2513\n2514\n2515\n2516\n2517\n2518\n2519\n2520\n2521\n2522\n2523\n2524\n2525\n2526\n2527\n2528\n2529\n2530\n2531\n2532\n2533\n2534\n2535\n2536\n2537\n2538\n2539\n2540\n2541\n2542\n2543\n2544\n2545\n2546\n2547\n2548\n2549\n2550\n2551\n2552\n2553\n2554\n2555\n2556\n2557\n2558\n2559\n2560\n2561\n2562\n2563\n2564\n2565\n2566\n2567\n2568\n2569\n2570\n2571\n2572\n2573\n2574\n2575\n2576\n2577\n2578\n2579\n2580\n2581\n2582\n2583\n2584\n2585\n2586\n2587\n2588\n2589\n2590\n2591\n2592\n2593\n2594\n2595\n2596\n2597\n2598\n2599\n2600\n2601\n2602\n2603\n2604\n2605\n2606\n2607\n2608\n2609\n2610\n2611\n2612\n2613\n2614\n2615\n2616\n2617\n2618\n2619\n2620\n2621\n2622\n2623\n2624\n2625\n2626\n2627\n2628\n2629\n2630\n2631\n2632\n2633\n2634\n2635\n2636\n2637\n2638\n2639\n2640\n2641\n2642\n2643\n2644\n2645\n2646\n2647\n2648\n2649\n2650\n2651\n2652\n2653\n2654\n2655\n2656\n2657\n2658\n2659\n2660\n2661\n2662\n2663\n2664\n2665\n2666\n2667\n2668\n2669\n2670\n2671\n2672\n2673\n2674\n2675\n2676\n2677\n2678\n2679\n2680\n2681\n2682\n2683\n2684\n2685\n2686\n2687\n2688\n2689\n2690\n2691\n2692\n2693\n2694\n2695\n2696\n2697\n2698\n2699\n2700\n2701\n2702\n2703\n2704\n2705\n2706\n2707\n2708\n2709\n2710\n2711\n2712\n2713\n2714\n2715\n2716\n2717\n2718\n2719\n2720\n2721\n2722\n2723\n2724\n2725\n2726\n2727\n2728\n2729\n2730\n2731\n2732\n2733\n2734\n2735\n2736\n2737\n2738\n2739\n2740\n2741\n2742\n2743\n2744\n2745\n2746\n2747\n2748\n2749\n2750\n2751\n2752\n2753\n2754\n2755\n2756\n2757\n2758\n2759\n2760\n2761\n2762\n2763\n2764\n2765\n2766\n2767\n2768\n2769\n2770\n2771\n2772\n2773\n2774\n2775\n2776\n2777\n2778\n2779\n2780\n2781\n2782\n2783\n2784\n2785\n2786\n2787\n2788\n2789\n2790\n2791\n2792\n2793\n2794\n2795\n2796\n2797\n2798\n2799\n2800\n2801\n2802\n2803\n2804\n2805\n2806\n2807\n2808\n2809\n2810\n2811\n2812\n2813\n2814\n2815\n2816\n2817\n2818\n2819\n2820\n2821\n2822\n2823\n2824\n2825\n2826\n2827\n2828\n2829\n2830\n2831\n2832\n2833\n2834\n2835\n2836\n2837\n2838\n2839\n2840\n2841\n2842\n2843\n2844\n2845\n2846\n2847\n2848\n2849\n2850\n2851\n2852\n2853\n2854\n2855\n2856\n2857\n2858\n2859\n2860\n2861\n2862\n2863\n2864\n2865\n2866\n2867\n2868\n2869\n2870\n2871\n2872\n2873\n2874\n2875\n2876\n2877\n2878\n2879\n2880\n2881\n2882\n2883\n2884\n2885\n2886\n2887\n2888\n2889\n2890\n2891\n2892\n2893\n2894\n2895\n2896\n2897\n2898\n2899\n2900\n2901\n2902\n2903\n2904\n2905\n2906\n2907\n2908\n2909\n2910\n2911\n2912\n2913\n2914\n2915\n2916\n2917\n2918\n2919\n2920\n2921\n2922\n2923\n2924\n2925\n2926\n2927\n2928\n2929\n2930\n2931\n2932\n2933\n2934\n2935\n2936\n2937\n2938\n2939\n2940\n2941\n2942\n2943\n2944\n2945\n2946\n2947\n2948\n2949\n2950\n2951\n2952\n2953\n2954\n2955\n2956\n2957\n2958\n2959\n2960\n2961\n2962\n2963\n2964\n2965\n2966\n2967\n2968\n2969\n2970\n2971\n2972\n2973\n2974\n2975\n2976\n2977\n2978\n2979\n2980\n2981\n2982\n2983\n2984\n2985\n2986\n2987\n2988\n2989\n2990\n2991\n2992\n2993\n2994\n2995\n2996\n2997\n2998\n2999\n3000\n3001\n3002\n3003\n3004\n3005\n3006\n3007\n3008\n3009\n3010\n3011\n3012\n3013\n3014\n3015\n3016\n3017\n3018\n3019\n3020\n3021\n3022\n3023\n3024\n3025\n3026\n3027\n3028\n3029\n3030\n3031\n3032\n3033\n3034\n3035\n3036\n3037\n3038\n3039\n3040\n3041\n3042\n3043\n3044\n3045\n3046\n3047\n3048\n3049\n3050\n3051\n3052\n3053\n3054\n3055\n3056\n3057\n3058\n3059\n3060\n3061\n3062\n3063\n3064\n3065\n3066\n3067\n3068\n3069\n3070\n3071\n3072\n3073\n3074\n3075\n3076\n3077\n3078\n3079\n3080\n3081\n3082\n3083\n3084\n3085\n3086\n3087\n3088\n3089\n3090\n3091\n3092\n3093\n3094\n3095\n3096\n3097\n3098\n3099\n3100\n3101\n3102\n3103\n3104\n3105\n3106\n3107\n3108\n3109\n3110\n3111\n3112\n3113\n3114\n3115\n3116\n3117\n3118\n3119\n3120\n3121\n3122\n3123\n3124\n3125\n3126\n3127\n3128\n3129\n3130\n3131\n3132\n3133\n3134\n3135\n3136\n3137\n3138\n3139\n3140\n3141\n3142\n3143\n3144\n3145\n3146\n3147\n3148\n3149\n3150\n3151\n3152\n3153\n3154\n3155\n3156\n3157\n3158\n3159\n3160\n3161\n3162\n3163\n3164\n3165\n3166\n3167\n3168\n3169\n3170\n3171\n3172\n3173\n3174\n3175\n3176\n3177\n3178\n3179\n3180\n3181\n3182\n3183\n3184\n3185\n3186\n3187\n3188\n3189\n3190\n3191\n3192\n3193\n3194\n3195\n3196\n3197\n3198\n3199\n3200\n3201\n3202\n3203\n3204\n3205\n3206\n3207\n3208\n3209\n3210\n3211\n3212\n3213\n3214\n3215\n3216\n3217\n3218\n3219\n3220\n3221\n3222\n3223\n3224\n3225\n3226\n3227\n3228\n3229\n3230\n3231\n3232\n3233\n3234\n3235\n3236\n3237\n3238\n3239\n3240\n3241\n3242\n3243\n3244\n3245\n3246\n3247\n3248\n3249\n3250\n3251\n3252\n3253\n3254\n3255\n3256\n3257\n3258\n3259\n3260\n3261\n3262\n3263\n3264\n3265\n3266\n3267\n3268\n3269\n3270\n3271\n3272\n3273\n3274\n3275\n3276\n3277\n3278\n3279\n3280\n3281\n3282\n3283\n3284\n3285\n3286\n3287\n3288\n3289\n3290\n3291\n3292\n3293\n3294\n3295\n3296\n3297\n3298\n3299\n3300\n3301\n3302\n3303\n3304\n3305\n3306\n3307\n3308\n3309\n3310\n3311\n3312\n3313\n3314\n3315\n3316\n3317\n3318\n3319\n3320\n3321\n3322\n3323\n3324\n3325\n3326\n3327\n3328\n3329\n3330\n3331\n3332\n3333\n3334\n3335\n3336\n3337\n3338\n3339\n3340\n3341\n3342\n3343\n3344\n3345\n3346\n3347\n3348\n3349\n3350\n3351\n3352\n3353\n3354\n3355\n3356\n3357\n3358\n3359\n3360\n3361\n3362\n3363\n3364\n3365\n3366\n3367\n3368\n3369\n3370\n3371\n3372\n3373\n3374\n3375\n3376\n3377\n3378\n3379\n3380\n3381\n3382\n3383\n3384\n3385\n3386\n3387\n3388\n3389\n3390\n3391\n3392\n3393\n3394\n3395\n3396\n3397\n3398\n3399\n3400\n3401\n3402\n3403\n3404\n3405\n3406\n3407\n3408\n3409\n3410\n3411\n3412\n3413\n3414\n3415\n3416\n3417\n3418\n3419\n3420\n3421\n3422\n3423\n3424\n3425\n3426\n3427\n3428\n3429\n3430\n3431\n3432\n3433\n3434\n3435\n3436\n3437\n3438\n3439\n3440\n3441\n3442\n3443\n3444\n3445\n3446\n3447\n3448\n3449\n3450\n3451\n3452\n3453\n3454\n3455\n3456\n3457\n3458\n3459\n3460\n3461\n3462\n3463\n3464\n3465\n3466\n3467\n3468\n3469\n3470\n3471\n3472\n3473\n3474\n3475\n3476\n3477\n3478\n3479\n3480\n3481\n3482\n3483\n3484\n3485\n3486\n3487\n3488\n3489\n3490\n3491\n3492\n3493\n3494\n3495\n3496\n3497\n3498\n3499\n3500\n3501\n3502\n3503\n3504\n3505\n3506\n3507\n3508\n3509\n3510\n3511\n3512\n3513\n3514\n3515\n3516\n3517\n3518\n3519\n3520\n3521\n3522\n3523\n3524\n3525\n3526\n3527\n3528\n3529\n3530\n3531\n3532\n3533\n3534\n3535\n3536\n3537\n3538\n3539\n3540\n3541\n3542\n3543\n3544\n3545\n3546\n3547\n3548\n3549\n3550\n3551\n3552\n3553\n3554\n3555\n3556\n3557\n3558\n3559\n3560\n3561\n3562\n3563\n3564\n3565\n3566\n3567\n3568\n3569\n3570\n3571\n3572\n3573\n3574\n3575\n3576\n3577\n3578\n3579\n3580\n3581\n3582\n3583\n3584\n3585\n3586\n3587\n3588\n3589\n3590\n3591\n3592\n3593\n3594\n3595\n3596\n3597\n3598\n3599\n3600\n3601\n3602\n3603\n3604\n3605\n3606\n3607\n3608\n3609\n3610\n3611\n3612\n3613\n3614\n3615\n3616\n3617\n3618\n3619\n3620\n3621\n3622\n3623\n3624\n3625\n3626\n3627\n3628\n3629\n3630\n3631\n3632\n3633\n3634\n3635\n3636\n3637\n3638\n3639\n3640\n3641\n3642\n3643\n3644\n3645\n3646\n3647\n3648\n3649\n3650\n3651\n3652\n3653\n3654\n3655\n3656\n3657\n3658\n3659\n3660\n3661\n3662\n3663\n3664\n3665\n3666\n3667\n3668\n3669\n3670\n3671\n3672\n3673\n3674\n3675\n3676\n3677\n3678\n3679\n3680\n3681\n3682\n3683\n3684\n3685\n3686\n3687\n3688\n3689\n3690\n3691\n3692\n3693\n3694\n3695\n3696\n3697\n3698\n3699\n3700\n3701\n3702\n3703\n3704\n3705\n3706\n3707\n3708\n3709\n3710\n3711\n3712\n3713\n3714\n3715\n3716\n3717\n3718\n3719\n3720\n3721\n3722\n3723\n3724\n3725\n3726\n3727\n3728\n3729\n3730\n3731\n3732\n3733\n3734\n3735\n3736\n3737\n3738\n3739\n3740\n3741\n3742\n3743\n3744\n3745\n3746\n3747\n3748\n3749\n3750\n3751\n3752\n3753\n3754\n3755\n3756\n3757\n3758\n3759\n3760\n3761\n3762\n3763\n3764\n3765\n3766\n3767\n3768\n3769\n3770\n3771\n3772\n3773\n3774\n3775\n3776\n3777\n3778\n3779\n3780\n3781\n3782\n3783\n3784\n3785\n3786\n3787\n3788\n3789\n3790\n3791\n3792\n3793\n3794\n3795\n3796\n3797\n3798\n3799\n3800\n3801\n3802\n3803\n3804\n3805\n3806\n3807\n3808\n3809\n3810\n3811\n3812\n3813\n3814\n3815\n3816\n3817\n3818\n3819\n3820\n3821\n3822\n3823\n3824\n3825\n3826\n3827\n3828\n3829\n3830\n3831\n3832\n3833\n3834\n3835\n3836\n3837\n3838\n3839\n3840\n3841\n3842\n3843\n3844\n3845\n3846\n3847\n3848\n3849\n3850\n3851\n3852\n3853\n3854\n3855\n3856\n3857\n3858\n3859\n3860\n3861\n3862\n3863\n3864\n3865\n3866\n3867\n3868\n3869\n3870\n3871\n3872\n3873\n3874\n3875\n3876\n3877\n3878\n3879\n3880\n3881\n3882\n3883\n3884\n3885\n3886\n3887\n3888\n3889\n3890\n3891\n3892\n3893\n3894\n3895\n3896\n3897\n3898\n3899\n3900\n3901\n3902\n3903\n3904\n3905\n3906\n3907\n3908\n3909\n3910\n3911\n3912\n3913\n3914\n3915\n3916\n3917\n3918\n3919\n3920\n3921\n3922\n3923\n3924\n3925\n3926\n3927\n3928\n3929\n3930\n3931\n3932\n3933\n3934\n3935\n3936\n3937\n3938\n3939\n3940\n3941\n3942\n3943\n3944\n3945\n3946\n3947\n3948\n3949\n3950\n3951\n3952\n3953\n3954\n3955\n3956\n3957\n3958\n3959\n3960\n3961\n3962\n3963\n3964\n3965\n3966\n3967\n3968\n3969\n3970\n3971\n3972\n3973\n3974\n3975\n3976\n3977\n3978\n3979\n3980\n3981\n3982\n3983\n3984\n3985\n3986\n3987\n3988\n3989\n3990\n3991\n3992\n3993\n3994\n3995\n3996\n3997\n3998\n3999\n4000\n4001\n4002\n4003\n4004\n4005\n4006\n4007\n4008\n4009\n4010\n4011\n4012\n4013\n4014\n4015\n4016\n4017\n4018\n4019\n4020\n4021\n4022\n4023\n4024\n4025\n4026\n4027\n4028\n4029\n4030\n4031\n4032\n4033\n4034\n4035\n4036\n4037\n4038\n4039\n4040\n4041\n4042\n4043\n4044\n4045\n4046\n4047\n4048\n4049\n4050\n4051\n4052\n4053\n4054\n4055\n4056\n4057\n4058\n4059\n4060\n4061\n4062\n4063\n4064\n4065\n4066\n4067\n4068\n4069\n4070\n4071\n4072\n4073\n4074\n4075\n4076\n4077\n4078\n4079\n4080\n4081\n4082\n4083\n4084\n4085\n4086\n4087\n4088\n4089\n4090\n4091\n4092\n4093\n4094\n4095\n4096\n4097\n4098\n4099\n4100\n4101\n4102\n4103\n4104\n4105\n4106\n4107\n4108\n4109\n4110\n4111\n4112\n4113\n4114\n4115\n4116\n4117\n4118\n4119\n4120\n4121\n4122\n4123\n4124\n4125\n4126\n4127\n4128\n4129\n4130\n4131\n4132\n4133\n4134\n4135\n4136\n4137\n4138\n4139\n4140\n4141\n4142\n4143\n4144\n4145\n4146\n4147\n4148\n4149\n4150\n4151\n4152\n4153\n4154\n4155\n4156\n4157\n4158\n4159\n4160\n4161\n4162\n4163\n4164\n4165\n4166\n4167\n4168\n4169\n4170\n4171\n4172\n4173\n4174\n4175\n4176\n4177\n4178\n4179\n4180\n4181\n4182\n4183\n4184\n4185\n4186\n4187\n4188\n4189\n4190\n4191\n4192\n4193\n4194\n4195\n4196\n4197\n4198\n4199\n4200\n4201\n4202\n4203\n4204\n4205\n4206\n4207\n4208\n4209\n4210\n4211\n4212\n4213\n4214\n4215\n4216\n4217\n4218\n4219\n4220\n4221\n4222\n4223\n4224\n4225\n4226\n4227\n4228\n4229\n4230\n4231\n4232\n4233\n4234\n4235\n4236\n4237\n4238\n4239\n4240\n4241\n4242\n4243\n4244\n4245\n4246\n4247\n4248\n4249\n4250\n4251\n4252\n4253\n4254\n4255\n4256\n4257\n4258\n4259\n4260\n4261\n4262\n4263\n4264\n4265\n4266\n4267\n4268\n4269\n4270\n4271\n4272\n4273\n4274\n4275\n4276\n4277\n4278\n4279\n4280\n4281\n4282\n4283\n4284\n4285\n4286\n4287\n4288\n4289\n4290\n4291\n4292\n4293\n4294\n4295\n4296\n4297\n4298\n4299\n4300\n4301\n4302\n4303\n4304\n4305\n4306\n4307\n4308\n4309\n4310\n4311\n4312\n4313\n4314\n4315\n4316\n4317\n4318\n4319\n4320\n4321\n4322\n4323\n4324\n4325\n4326\n4327\n4328\n4329\n4330\n4331\n4332\n4333\n4334\n4335\n4336\n4337\n4338\n4339\n4340\n4341\n4342\n4343\n4344\n4345\n4346\n4347\n4348\n4349\n4350\n4351\n4352\n4353\n4354\n4355\n4356\n4357\n4358\n4359\n4360\n4361\n4362\n4363\n4364\n4365\n4366\n4367\n4368\n4369\n4370\n4371\n4372\n4373\n4374\n4375\n4376\n4377\n4378\n4379\n4380\n4381\n4382\n4383\n4384\n4385\n4386\n4387\n4388\n4389\n4390\n4391\n4392\n4393\n4394\n4395\n4396\n4397\n4398\n4399\n4400\n4401\n4402\n4403\n4404\n4405\n4406\n4407\n4408\n4409\n4410\n4411\n4412\n4413\n4414\n4415\n4416\n4417\n4418\n4419\n4420\n4421\n4422\n4423\n4424\n4425\n4426\n4427\n4428\n4429\n4430\n4431\n4432\n4433\n4434\n4435\n4436\n4437\n4438\n4439\n4440\n4441\n4442\n4443\n4444\n4445\n4446\n4447\n4448\n4449\n4450\n4451\n4452\n4453\n4454\n4455\n4456\n4457\n4458\n4459\n4460\n4461\n4462\n4463\n4464\n4465\n4466\n4467\n4468\n4469\n4470\n4471\n4472\n4473\n4474\n4475\n4476\n4477\n4478\n4479\n4480\n4481\n4482\n4483\n4484\n4485\n4486\n4487\n4488\n4489\n4490\n4491\n4492\n4493\n4494\n4495\n4496\n4497\n4498\n4499\n4500\n4501\n4502\n4503\n4504\n4505\n4506\n4507\n4508\n4509\n4510\n4511\n4512\n4513\n4514\n4515\n4516\n4517\n4518\n4519\n4520\n4521\n4522\n4523\n4524\n4525\n4526\n4527\n4528\n4529\n4530\n4531\n4532\n4533\n4534\n4535\n4536\n4537\n4538\n4539\n4540\n4541\n4542\n4543\n4544\n4545\n4546\n4547\n4548\n4549\n4550\n4551\n4552\n4553\n4554\n4555\n4556\n4557\n4558\n4559\n4560\n4561\n4562\n4563\n4564\n4565\n4566\n4567\n4568\n4569\n4570\n4571\n4572\n4573\n4574\n4575\n4576\n4577\n4578\n4579\n4580\n4581\n4582\n4583\n4584\n4585\n4586\n4587\n4588\n4589\n4590\n4591\n4592\n4593\n4594\n4595\n4596\n4597\n4598\n4599\n4600\n4601\n4602\n4603\n4604\n4605\n4606\n4607\n4608\n4609\n4610\n4611\n4612\n4613\n4614\n4615\n4616\n4617\n4618\n4619\n4620\n4621\n4622\n4623\n4624\n4625\n4626\n4627\n4628\n4629\n4630\n4631\n4632\n4633\n4634\n4635\n4636\n4637\n4638\n4639\n4640\n4641\n4642\n4643\n4644\n4645\n4646\n4647\n4648\n4649\n4650\n4651\n4652\n4653\n4654\n4655\n4656\n4657\n4658\n4659\n4660\n4661\n4662\n4663\n4664\n4665\n4666\n4667\n4668\n4669\n4670\n4671\n4672\n4673\n4674\n4675\n4676\n4677\n4678\n4679\n4680\n4681\n4682\n4683\n4684\n4685\n4686\n4687\n4688\n4689\n4690\n4691\n4692\n4693\n4694\n4695\n4696\n4697\n4698\n4699\n4700\n4701\n4702\n4703\n4704\n4705\n4706\n4707\n4708\n4709\n4710\n4711\n4712\n4713\n4714\n4715\n4716\n4717\n4718\n4719\n4720\n4721\n4722\n4723\n4724\n4725\n4726\n4727\n4728\n4729\n4730\n4731\n4732\n4733\n4734\n4735\n4736\n4737\n4738\n4739\n4740\n4741\n4742\n4743\n4744\n4745\n4746\n4747\n4748\n4749\n4750\n4751\n4752\n4753\n4754\n4755\n4756\n4757\n4758\n4759\n4760\n4761\n4762\n4763\n4764\n4765\n4766\n4767\n4768\n4769\n4770\n4771\n4772\n4773\n4774\n4775\n4776\n4777\n4778\n4779\n4780\n4781\n4782\n4783\n4784\n4785\n4786\n4787\n4788\n4789\n4790\n4791\n4792\n4793\n4794\n4795\n4796\n4797\n4798\n4799\n4800\n4801\n4802\n4803\n4804\n4805\n4806\n4807\n4808\n4809\n4810\n4811\n4812\n4813\n4814\n4815\n4816\n4817\n4818\n4819\n4820\n4821\n4822\n4823\n4824\n4825\n4826\n4827\n4828\n4829\n4830\n4831\n4832\n4833\n4834\n4835\n4836\n4837\n4838\n4839\n4840\n4841\n4842\n4843\n4844\n4845\n4846\n4847\n4848\n4849\n4850\n4851\n4852\n4853\n4854\n4855\n4856\n4857\n4858\n4859\n4860\n4861\n4862\n4863\n4864\n4865\n4866\n4867\n4868\n4869\n4870\n4871\n4872\n4873\n4874\n4875\n4876\n4877\n4878\n4879\n4880\n4881\n4882\n4883\n4884\n4885\n4886\n4887\n4888\n4889\n4890\n4891\n4892\n4893\n4894\n4895\n4896\n4897\n4898\n4899\n4900\n4901\n4902\n4903\n4904\n4905\n4906\n4907\n4908\n4909\n4910\n4911\n4912\n4913\n4914\n4915\n4916\n4917\n4918\n4919\n4920\n4921\n4922\n4923\n4924\n4925\n4926\n4927\n4928\n4929\n4930\n4931\n4932\n4933\n4934\n4935\n4936\n4937\n4938\n4939\n4940\n4941\n4942\n4943\n4944\n4945\n4946\n4947\n4948\n4949\n4950\n4951\n4952\n4953\n4954\n4955\n4956\n4957\n4958\n4959\n4960\n4961\n4962\n4963\n4964\n4965\n4966\n4967\n4968\n4969\n4970\n4971\n4972\n4973\n4974\n4975\n4976\n4977\n4978\n4979\n4980\n4981\n4982\n4983\n4984\n4985\n4986\n4987\n4988\n4989\n4990\n4991\n4992\n4993\n4994\n4995\n4996\n4997\n4998\n4999\n5000\n5001\n5002\n5003\n5004\n5005\n5006\n5007\n5008\n5009\n5010\n5011\n5012\n5013\n5014\n5015\n5016\n5017\n5018\n5019\n5020\n5021\n5022\n5023\n5024\n5025\n5026\n5027\n5028\n5029\n5030\n5031\n5032\n5033\n5034\n5035\n5036\n5037\n5038\n5039\n5040\n5041\n5042\n5043\n5044\n5045\n5046\n5047\n5048\n5049\n5050\n5051\n5052\n5053\n5054\n5055\n5056\n5057\n5058\n5059\n5060\n5061\n5062\n5063\n5064\n5065\n5066\n5067\n5068\n5069\n5070\n5071\n5072\n5073\n5074\n5075\n5076\n5077\n5078\n5079\n5080\n5081\n5082\n5083\n5084\n5085\n5086\n5087\n5088\n5089\n5090\n5091\n5092\n5093\n5094\n5095\n5096\n5097\n5098\n5099\n5100\n5101\n5102\n5103\n5104\n5105\n5106\n5107\n5108\n5109\n5110\n5111\n5112\n5113\n5114\n5115\n5116\n5117\n5118\n5119\n5120\n5121\n5122\n5123\n5124\n5125\n5126\n5127\n5128\n5129\n5130\n5131\n5132\n5133\n5134\n5135\n5136\n5137\n5138\n5139\n5140\n5141\n5142\n5143\n5144\n5145\n5146\n5147\n5148\n5149\n5150\n5151\n5152\n5153\n5154\n5155\n5156\n5157\n5158\n5159\n5160\n5161\n5162\n5163\n5164\n5165\n5166\n5167\n5168\n5169\n5170\n5171\n5172\n5173\n5174\n5175\n5176\n5177\n5178\n5179\n5180\n5181\n5182\n5183\n5184\n5185\n5186\n5187\n5188\n5189\n5190\n5191\n5192\n5193\n5194\n5195\n5196\n5197\n5198\n5199\n5200\n5201\n5202\n5203\n5204\n5205\n5206\n5207\n5208\n5209\n5210\n5211\n5212\n5213\n5214\n5215\n5216\n5217\n5218\n5219\n5220\n5221\n5222\n5223\n5224\n5225\n5226\n5227\n5228\n5229\n5230\n5231\n5232\n5233\n5234\n5235\n5236\n5237\n5238\n5239\n5240\n5241\n5242\n5243\n5244\n5245\n5246\n5247\n5248\n5249\n5250\n5251\n5252\n5253\n5254\n5255\n5256\n5257\n5258\n5259\n5260\n5261\n5262\n5263\n5264\n5265\n5266\n5267\n5268\n5269\n5270\n5271\n5272\n5273\n5274\n5275\n5276\n5277\n5278\n5279\n5280\n5281\n5282\n5283\n5284\n5285\n5286\n5287\n5288\n5289\n5290\n5291\n5292\n5293\n5294\n5295\n5296\n5297\n5298\n5299\n5300\n5301\n5302\n5303\n5304\n5305\n5306\n5307\n5308\n5309\n5310\n5311\n5312\n5313\n5314\n5315\n5316\n5317\n5318\n5319\n5320\n5321\n5322\n5323\n5324\n5325\n5326\n5327\n5328\n5329\n5330\n5331\n5332\n5333\n5334\n5335\n5336\n5337\n5338\n5339\n5340\n5341\n5342\n5343\n5344\n5345\n5346\n5347\n5348\n5349\n5350\n5351\n5352\n5353\n5354\n5355\n5356\n5357\n5358\n5359\n5360\n5361\n5362\n5363\n5364\n5365\n5366\n5367\n5368\n5369\n5370\n5371\n5372\n5373\n5374\n5375\n5376\n5377\n5378\n5379\n5380\n5381\n5382\n5383\n5384\n5385\n5386\n5387\n5388\n5389\n5390\n5391\n5392\n5393\n5394\n5395\n5396\n5397\n5398\n5399\n5400\n5401\n5402\n5403\n5404\n5405\n5406\n5407\n5408\n5409\n5410\n5411\n5412\n5413\n5414\n5415\n5416\n5417\n5418\n5419\n5420\n5421\n5422\n5423\n5424\n5425\n5426\n5427\n5428\n5429\n5430\n5431\n5432\n5433\n5434\n5435\n5436\n5437\n5438\n5439\n5440\n5441\n5442\n5443\n5444\n5445\n5446\n5447\n5448\n5449\n5450\n5451\n5452\n5453\n5454\n5455\n5456\n5457\n5458\n5459\n5460\n5461\n5462\n5463\n5464\n5465\n5466\n5467\n5468\n5469\n5470\n5471\n5472\n5473\n5474\n5475\n5476\n5477\n5478\n5479\n5480\n5481\n5482\n5483\n5484\n5485\n5486\n5487\n5488\n5489\n5490\n5491\n5492\n5493\n5494\n5495\n5496\n5497\n5498\n5499\n5500\n5501\n5502\n5503\n5504\n5505\n5506\n5507\n5508\n5509\n5510\n5511\n5512\n5513\n5514\n5515\n5516\n5517\n5518\n5519\n5520\n5521\n5522\n5523\n5524\n5525\n5526\n5527\n5528\n5529\n5530\n5531\n5532\n5533\n5534\n5535\n5536\n5537\n5538\n5539\n5540\n5541\n5542\n5543\n5544\n5545\n5546\n5547\n5548\n5549\n5550\n5551\n5552\n5553\n5554\n5555\n5556\n5557\n5558\n5559\n5560\n5561\n5562\n5563\n5564\n5565\n5566\n5567\n5568\n5569\n5570\n5571\n5572\n5573\n5574\n5575\n5576\n5577\n5578\n5579\n5580\n5581\n5582\n5583\n5584\n5585\n5586\n5587\n5588\n5589\n5590\n5591\n5592\n5593\n5594\n5595\n5596\n5597\n5598\n5599\n5600\n5601\n5602\n5603\n5604\n5605\n5606\n5607\n5608\n5609\n5610\n5611\n5612\n5613\n5614\n5615\n5616\n5617\n5618\n5619\n5620\n5621\n5622\n5623\n5624\n5625\n5626\n5627\n5628\n5629\n5630\n5631\n5632\n5633\n5634\n5635\n5636\n5637\n5638\n5639\n5640\n5641\n5642\n5643\n5644\n5645\n5646\n5647\n5648\n5649\n5650\n5651\n5652\n5653\n5654\n5655\n5656\n5657\n5658\n5659\n5660\n5661\n5662\n5663\n5664\n5665\n5666\n5667\n5668\n5669\n5670\n5671\n5672\n5673\n5674\n5675\n5676\n5677\n5678\n5679\n5680\n5681\n5682\n5683\n5684\n5685\n5686\n5687\n5688\n5689\n5690\n5691\n5692\n5693\n5694\n5695\n5696\n5697\n5698\n5699\n5700\n5701\n5702\n5703\n5704\n5705\n5706\n5707\n5708\n5709\n5710\n5711\n5712\n5713\n5714\n5715\n5716\n5717\n5718\n5719\n5720\n5721\n5722\n5723\n5724\n5725\n5726\n5727\n5728\n5729\n5730\n5731\n5732\n5733\n5734\n5735\n5736\n5737\n5738\n5739\n5740\n5741\n5742\n5743\n5744\n5745\n5746\n5747\n5748\n5749\n5750\n5751\n5752\n5753\n5754\n5755\n5756\n5757\n5758\n5759\n5760\n5761\n5762\n5763\n5764\n5765\n5766\n5767\n5768\n5769\n5770\n5771\n5772\n5773\n5774\n5775\n5776\n5777\n5778\n5779\n5780\n5781\n5782\n5783\n5784\n5785\n5786\n5787\n5788\n5789\n5790\n5791\n5792\n5793\n5794\n5795\n5796\n5797\n5798\n5799\n5800\n5801\n5802\n5803\n5804\n5805\n5806\n5807\n5808\n5809\n5810\n5811\n5812\n5813\n5814\n5815\n5816\n5817\n5818\n5819\n5820\n5821\n5822\n5823\n5824\n5825\n5826\n5827\n5828\n5829\n5830\n5831\n5832\n5833\n5834\n5835\n5836\n5837\n5838\n5839\n5840\n5841\n5842\n5843\n5844\n5845\n5846\n5847\n5848\n5849\n5850\n5851\n5852\n5853\n5854\n5855\n5856\n5857\n5858\n5859\n5860\n5861\n5862\n5863\n5864\n5865\n5866\n5867\n5868\n5869\n5870\n5871\n5872\n5873\n5874\n5875\n5876\n5877\n5878\n5879\n5880\n5881\n5882\n5883\n5884\n5885\n5886\n5887\n5888\n5889\n5890\n5891\n5892\n5893\n5894\n5895\n5896\n5897\n5898\n5899\n5900\n5901\n5902\n5903\n5904\n5905\n5906\n5907\n5908\n5909\n5910\n5911\n5912\n5913\n5914\n5915\n5916\n5917\n5918\n5919\n5920\n5921\n5922\n5923\n5924\n5925\n5926\n5927\n5928\n5929\n5930\n5931\n5932\n5933\n5934\n5935\n5936\n5937\n5938\n5939\n5940\n5941\n5942\n5943\n5944\n5945\n5946\n5947\n5948\n5949\n5950\n5951\n5952\n5953\n5954\n5955\n5956\n5957\n5958\n5959\n5960\n5961\n5962\n5963\n5964\n5965\n5966\n5967\n5968\n5969\n5970\n5971\n5972\n5973\n5974\n5975\n5976\n5977\n5978\n5979\n5980\n5981\n5982\n5983\n5984\n5985\n5986\n5987\n5988\n5989\n5990\n5991\n5992\n5993\n5994\n5995\n5996\n5997\n5998\n5999\n6000\n6001\n6002\n6003\n6004\n6005\n6006\n6007\n6008\n6009\n6010\n6011\n6012\n6013\n6014\n6015\n6016\n6017\n6018\n6019\n6020\n6021\n6022\n6023\n6024\n6025\n6026\n6027\n6028\n6029\n6030\n6031\n6032\n6033\n6034\n6035\n6036\n6037\n6038\n6039\n6040\n6041\n6042\n6043\n6044\n6045\n6046\n6047\n6048\n6049\n6050\n6051\n6052\n6053\n6054\n6055\n6056\n6057\n6058\n6059\n6060\n6061\n6062\n6063\n6064\n6065\n6066\n6067\n6068\n6069\n6070\n6071\n6072\n6073\n6074\n6075\n6076\n6077\n6078\n6079\n6080\n6081\n6082\n6083\n6084\n6085\n6086\n6087\n6088\n6089\n6090\n6091\n6092\n6093\n6094\n6095\n6096\n6097\n6098\n6099\n6100\n6101\n6102\n6103\n6104\n6105\n6106\n6107\n6108\n6109\n6110\n6111\n6112\n6113\n6114\n6115\n6116\n6117\n6118\n6119\n6120\n6121\n6122\n6123\n6124\n6125\n6126\n6127\n6128\n6129\n6130\n6131\n6132\n6133\n6134\n6135\n6136\n6137\n6138\n6139\n6140\n6141\n6142\n6143\n6144\n6145\n6146\n6147\n6148\n6149\n6150\n6151\n6152\n6153\n6154\n6155\n6156\n6157\n6158\n6159\n6160\n6161\n6162\n6163\n6164\n6165\n6166\n6167\n6168\n6169\n6170\n6171\n6172\n6173\n6174\n6175\n6176\n6177\n6178\n6179\n6180\n6181\n6182\n6183\n6184\n6185\n6186\n6187\n6188\n6189\n6190\n6191\n6192\n6193\n6194\n6195\n6196\n6197\n6198\n6199\n6200\n6201\n6202\n6203\n6204\n6205\n6206\n6207\n6208\n6209\n6210\n6211\n6212\n6213\n6214\n6215\n6216\n6217\n6218\n6219\n6220\n6221\n6222\n6223\n6224\n6225\n6226\n6227\n6228\n6229\n6230\n6231\n6232\n6233\n6234\n6235\n6236\n6237\n6238\n6239\n6240\n6241\n6242\n6243\n6244\n6245\n6246\n6247\n6248\n6249\n6250\n6251\n6252\n6253\n6254\n6255\n6256\n6257\n6258\n6259\n6260\n6261\n6262\n6263\n6264\n6265\n6266\n6267\n6268\n6269\n6270\n6271\n6272\n6273\n6274\n6275\n6276\n6277\n6278\n6279\n6280\n6281\n6282\n6283\n6284\n6285\n6286\n6287\n6288\n6289\n6290\n6291\n6292\n6293\n6294\n6295\n6296\n6297\n6298\n6299\n6300\n6301\n6302\n6303\n6304\n6305\n6306\n6307\n6308\n6309\n6310\n6311\n6312\n6313\n6314\n6315\n6316\n6317\n6318\n6319\n6320\n6321\n6322\n6323\n6324\n6325\n6326\n6327\n6328\n6329\n6330\n6331\n6332\n6333\n6334\n6335\n6336\n6337\n6338\n6339\n6340\n6341\n6342\n6343\n6344\n6345\n6346\n6347\n6348\n6349\n6350\n6351\n6352\n6353\n6354\n6355\n6356\n6357\n6358\n6359\n6360\n6361\n6362\n6363\n6364\n6365\n6366\n6367\n6368\n6369\n6370\n6371\n6372\n6373\n6374\n6375\n6376\n6377\n6378\n6379\n6380\n6381\n6382\n6383\n6384\n6385\n6386\n6387\n6388\n6389\n6390\n6391\n6392\n6393\n6394\n6395\n6396\n6397\n6398\n6399\n6400\n6401\n6402\n6403\n6404\n6405\n6406\n6407\n6408\n6409\n6410\n6411\n6412\n6413\n6414\n6415\n6416\n6417\n6418\n6419\n6420\n6421\n6422\n6423\n6424\n6425\n6426\n6427\n6428\n6429\n6430\n6431\n6432\n6433\n6434\n6435\n6436\n6437\n6438\n6439\n6440\n6441\n6442\n6443\n6444\n6445\n6446\n6447\n6448\n6449\n6450\n6451\n6452\n6453\n6454\n6455\n6456\n6457\n6458\n6459\n6460\n6461\n6462\n6463\n6464\n6465\n6466\n6467\n6468\n6469\n6470\n6471\n6472\n6473\n6474\n6475\n6476\n6477\n6478\n6479\n6480\n6481\n6482\n6483\n6484\n6485\n6486\n6487\n6488\n6489\n6490\n6491\n6492\n6493\n6494\n6495\n6496\n6497\n6498\n6499\n6500\n6501\n6502\n6503\n6504\n6505\n6506\n6507\n6508\n6509\n6510\n6511\n6512\n6513\n6514\n6515\n6516\n6517\n6518\n6519\n6520\n6521\n6522\n6523\n6524\n6525\n6526\n6527\n6528\n6529\n6530\n6531\n6532\n6533\n6534\n6535\n6536\n6537\n6538\n6539\n6540\n6541\n6542\n6543\n6544\n6545\n6546\n6547\n6548\n6549\n6550\n6551\n6552\n6553\n6554\n6555\n6556\n6557\n6558\n6559\n6560\n6561\n6562\n6563\n6564\n6565\n6566\n6567\n6568\n6569\n6570\n6571\n6572\n6573\n6574\n6575\n6576\n6577\n6578\n6579\n6580\n6581\n6582\n6583\n6584\n6585\n6586\n6587\n6588\n6589\n6590\n6591\n6592\n6593\n6594\n6595\n6596\n6597\n6598\n6599\n6600\n6601\n6602\n6603\n6604\n6605\n6606\n6607\n6608\n6609\n6610\n6611\n6612\n6613\n6614\n6615\n6616\n6617\n6618\n6619\n6620\n6621\n6622\n6623\n6624\n6625\n6626\n6627\n6628\n6629\n6630\n6631\n6632\n6633\n6634\n6635\n6636\n6637\n6638\n6639\n6640\n6641\n6642\n6643\n6644\n6645\n6646\n6647\n6648\n6649\n6650\n6651\n6652\n6653\n6654\n6655\n6656\n6657\n6658\n6659\n6660\n6661\n6662\n6663\n6664\n6665\n6666\n6667\n6668\n6669\n6670\n6671\n6672\n6673\n6674\n6675\n6676\n6677\n6678\n6679\n6680\n6681\n6682\n6683\n6684\n6685\n6686\n6687\n6688\n6689\n6690\n6691\n6692\n6693\n6694\n6695\n6696\n6697\n6698\n6699\n6700\n6701\n6702\n6703\n6704\n6705\n6706\n6707\n6708\n6709\n6710\n6711\n6712\n6713\n6714\n6715\n6716\n6717\n6718\n6719\n6720\n6721\n6722\n6723\n6724\n6725\n6726\n6727\n6728\n6729\n6730\n6731\n6732\n6733\n6734\n6735\n6736\n6737\n6738\n6739\n6740\n6741\n6742\n6743\n6744\n6745\n6746\n6747\n6748\n6749\n6750\n6751\n6752\n6753\n6754\n6755\n6756\n6757\n6758\n6759\n6760\n6761\n6762\n6763\n6764\n6765\n6766\n6767\n6768\n6769\n6770\n6771\n6772\n6773\n6774\n6775\n6776\n6777\n6778\n6779\n6780\n6781\n6782\n6783\n6784\n6785\n6786\n6787\n6788\n6789\n6790\n6791\n6792\n6793\n6794\n6795\n6796\n6797\n6798\n6799\n6800\n6801\n6802\n6803\n6804\n6805\n6806\n6807\n6808\n6809\n6810\n6811\n6812\n6813\n6814\n6815\n6816\n6817\n6818\n6819\n6820\n6821\n6822\n6823\n6824\n6825\n6826\n6827\n6828\n6829\n6830\n6831\n6832\n6833\n6834\n6835\n6836\n6837\n6838\n6839\n6840\n6841\n6842\n6843\n6844\n6845\n6846\n6847\n6848\n6849\n6850\n6851\n6852\n6853\n6854\n6855\n6856\n6857\n6858\n6859\n6860\n6861\n6862\n6863\n6864\n6865\n6866\n6867\n6868\n6869\n6870\n6871\n6872\n6873\n6874\n6875\n6876\n6877\n6878\n6879\n6880\n6881\n6882\n6883\n6884\n6885\n6886\n6887\n6888\n6889\n6890\n6891\n6892\n6893\n6894\n6895\n6896\n6897\n6898\n6899\n6900\n6901\n6902\n6903\n6904\n6905\n6906\n6907\n6908\n6909\n6910\n6911\n6912\n6913\n6914\n6915\n6916\n6917\n6918\n6919\n6920\n6921\n6922\n6923\n6924\n6925\n6926\n6927\n6928\n6929\n6930\n6931\n6932\n6933\n6934\n6935\n6936\n6937\n6938\n6939\n6940\n6941\n6942\n6943\n6944\n6945\n6946\n6947\n6948\n6949\n6950\n6951\n6952\n6953\n6954\n6955\n6956\n6957\n6958\n6959\n6960\n6961\n6962\n6963\n6964\n6965\n6966\n6967\n6968\n6969\n6970\n6971\n6972\n6973\n6974\n6975\n6976\n6977\n6978\n6979\n6980\n6981\n6982\n6983\n6984\n6985\n6986\n6987\n6988\n6989\n6990\n6991\n6992\n6993\n6994\n6995\n6996\n6997\n6998\n6999\n7000\n7001\n7002\n7003\n7004\n7005\n7006\n7007\n7008\n7009\n7010\n7011\n7012\n7013\n7014\n7015\n7016\n7017\n7018\n7019\n7020\n7021\n7022\n7023\n7024\n7025\n7026\n7027\n7028\n7029\n7030\n7031\n7032\n7033\n7034\n7035\n7036\n7037\n7038\n7039\n7040\n7041\n7042\n7043\n7044\n7045\n7046\n7047\n7048\n7049\n7050\n7051\n7052\n7053\n7054\n7055\n7056\n7057\n7058\n7059\n7060\n7061\n7062\n7063\n7064\n7065\n7066\n7067\n7068\n7069\n7070\n7071\n7072\n7073\n7074\n7075\n7076\n7077\n7078\n7079\n7080\n7081\n7082\n7083\n7084\n7085\n7086\n7087\n7088\n7089\n7090\n7091\n7092\n7093\n7094\n7095\n7096\n7097\n7098\n7099\n7100\n7101\n7102\n7103\n7104\n7105\n7106\n7107\n7108\n7109\n7110\n7111\n7112\n7113\n7114\n7115\n7116\n7117\n7118\n7119\n7120\n7121\n7122\n7123\n7124\n7125\n7126\n7127\n7128\n7129\n7130\n7131\n7132\n7133\n7134\n7135\n7136\n7137\n7138\n7139\n7140\n7141\n7142\n7143\n7144\n7145\n7146\n7147\n7148\n7149\n7150\n7151\n7152\n7153\n7154\n7155\n7156\n7157\n7158\n7159\n7160\n7161\n7162\n7163\n7164\n7165\n7166\n7167\n7168\n7169\n7170\n7171\n7172\n7173\n7174\n7175\n7176\n7177\n7178\n7179\n7180\n7181\n7182\n7183\n7184\n7185\n7186\n7187\n7188\n7189\n7190\n7191\n7192\n7193\n7194\n7195\n7196\n7197\n7198\n7199\n7200\n7201\n7202\n7203\n7204\n7205\n7206\n7207\n7208\n7209\n7210\n7211\n7212\n7213\n7214\n7215\n7216\n7217\n7218\n7219\n7220\n7221\n7222\n7223\n7224\n7225\n7226\n7227\n7228\n7229\n7230\n7231\n7232\n7233\n7234\n7235\n7236\n7237\n7238\n7239\n7240\n7241\n7242\n7243\n7244\n7245\n7246\n7247\n7248\n7249\n7250\n7251\n7252\n7253\n7254\n7255\n7256\n7257\n7258\n7259\n7260\n7261\n7262\n7263\n7264\n7265\n7266\n7267\n7268\n7269\n7270\n7271\n7272\n7273\n7274\n7275\n7276\n7277\n7278\n7279\n7280\n7281\n7282\n7283\n7284\n7285\n7286\n7287\n7288\n7289\n7290\n7291\n7292\n7293\n7294\n7295\n7296\n7297\n7298\n7299\n7300\n7301\n7302\n7303\n7304\n7305\n7306\n7307\n7308\n7309\n7310\n7311\n7312\n7313\n7314\n7315\n7316\n7317\n7318\n7319\n7320\n7321\n7322\n7323\n7324\n7325\n7326\n7327\n7328\n7329\n7330\n7331\n7332\n7333\n7334\n7335\n7336\n7337\n7338\n7339\n7340\n7341\n7342\n7343\n7344\n7345\n7346\n7347\n7348\n7349\n7350\n7351\n7352\n7353\n7354\n7355\n7356\n7357\n7358\n7359\n7360\n7361\n7362\n7363\n7364\n7365\n7366\n7367\n7368\n7369\n7370\n7371\n7372\n7373\n7374\n7375\n7376\n7377\n7378\n7379\n7380\n7381\n7382\n7383\n7384\n7385\n7386\n7387\n7388\n7389\n7390\n7391\n7392\n7393\n7394\n7395\n7396\n7397\n7398\n7399\n7400\n7401\n7402\n7403\n7404\n7405\n7406\n7407\n7408\n7409\n7410\n7411\n7412\n7413\n7414\n7415\n7416\n7417\n7418\n7419\n7420\n7421\n7422\n7423\n7424\n7425\n7426\n7427\n7428\n7429\n7430\n7431\n7432\n7433\n7434\n7435\n7436\n7437\n7438\n7439\n7440\n7441\n7442\n7443\n7444\n7445\n7446\n7447\n7448\n7449\n7450\n7451\n7452\n7453\n7454\n7455\n7456\n7457\n7458\n7459\n7460\n7461\n7462\n7463\n7464\n7465\n7466\n7467\n7468\n7469\n7470\n7471\n7472\n7473\n7474\n7475\n7476\n7477\n7478\n7479\n7480\n7481\n7482\n7483\n7484\n7485\n7486\n7487\n7488\n7489\n7490\n7491\n7492\n7493\n7494\n7495\n7496\n7497\n7498\n7499\n7500\n7501\n7502\n7503\n7504\n7505\n7506\n7507\n7508\n7509\n7510\n7511\n7512\n7513\n7514\n7515\n7516\n7517\n7518\n7519\n7520\n7521\n7522\n7523\n7524\n7525\n7526\n7527\n7528\n7529\n7530\n7531\n7532\n7533\n7534\n7535\n7536\n7537\n7538\n7539\n7540\n7541\n7542\n7543\n7544\n7545\n7546\n7547\n7548\n7549\n7550\n7551\n7552\n7553\n7554\n7555\n7556\n7557\n7558\n7559\n7560\n7561\n7562\n7563\n7564\n7565\n7566\n7567\n7568\n7569\n7570\n7571\n7572\n7573\n7574\n7575\n7576\n7577\n7578\n7579\n7580\n7581\n7582\n7583\n7584\n7585\n7586\n7587\n7588\n7589\n7590\n7591\n7592\n7593\n7594\n7595\n7596\n7597\n7598\n7599\n7600\n7601\n7602\n7603\n7604\n7605\n7606\n7607\n7608\n7609\n7610\n7611\n7612\n7613\n7614\n7615\n7616\n7617\n7618\n7619\n7620\n7621\n7622\n7623\n7624\n7625\n7626\n7627\n7628\n7629\n7630\n7631\n7632\n7633\n7634\n7635\n7636\n7637\n7638\n7639\n7640\n7641\n7642\n7643\n7644\n7645\n7646\n7647\n7648\n7649\n7650\n7651\n7652\n7653\n7654\n7655\n7656\n7657\n7658\n7659\n7660\n7661\n7662\n7663\n7664\n7665\n7666\n7667\n7668\n7669\n7670\n7671\n7672\n7673\n7674\n7675\n7676\n7677\n7678\n7679\n7680\n7681\n7682\n7683\n7684\n7685\n7686\n7687\n7688\n7689\n7690\n7691\n7692\n7693\n7694\n7695\n7696\n7697\n7698\n7699\n7700\n7701\n7702\n7703\n7704\n7705\n7706\n7707\n7708\n7709\n7710\n7711\n7712\n7713\n7714\n7715\n7716\n7717\n7718\n7719\n7720\n7721\n7722\n7723\n7724\n7725\n7726\n7727\n7728\n7729\n7730\n7731\n7732\n7733\n7734\n7735\n7736\n7737\n7738\n7739\n7740\n7741\n7742\n7743\n7744\n7745\n7746\n7747\n7748\n7749\n7750\n7751\n7752\n7753\n7754\n7755\n7756\n7757\n7758\n7759\n7760\n7761\n7762\n7763\n7764\n7765\n7766\n7767\n7768\n7769\n7770\n7771\n7772\n7773\n7774\n7775\n7776\n7777\n7778\n7779\n7780\n7781\n7782\n7783\n7784\n7785\n7786\n7787\n7788\n7789\n7790\n7791\n7792\n7793\n7794\n7795\n7796\n7797\n7798\n7799\n7800\n7801\n7802\n7803\n7804\n7805\n7806\n7807\n7808\n7809\n7810\n7811\n7812\n7813\n7814\n7815\n7816\n7817\n7818\n7819\n7820\n7821\n7822\n7823\n7824\n7825\n7826\n7827\n7828\n7829\n7830\n7831\n7832\n7833\n7834\n7835\n7836\n7837\n7838\n7839\n7840\n7841\n7842\n7843\n7844\n7845\n7846\n7847\n7848\n7849\n7850\n7851\n7852\n7853\n7854\n7855\n7856\n7857\n7858\n7859\n7860\n7861\n7862\n7863\n7864\n7865\n7866\n7867\n7868\n7869\n7870\n7871\n7872\n7873\n7874\n7875\n7876\n7877\n7878\n7879\n7880\n7881\n7882\n7883\n7884\n7885\n7886\n7887\n7888\n7889\n7890\n7891\n7892\n7893\n7894\n7895\n7896\n7897\n7898\n7899\n7900\n7901\n7902\n7903\n7904\n7905\n7906\n7907\n7908\n7909\n7910\n7911\n7912\n7913\n7914\n7915\n7916\n7917\n7918\n7919\n7920\n7921\n7922\n7923\n7924\n7925\n7926\n7927\n7928\n7929\n7930\n7931\n7932\n7933\n7934\n7935\n7936\n7937\n7938\n7939\n7940\n7941\n7942\n7943\n7944\n7945\n7946\n7947\n7948\n7949\n7950\n7951\n7952\n7953\n7954\n7955\n7956\n7957\n7958\n7959\n7960\n7961\n7962\n7963\n7964\n7965\n7966\n7967\n7968\n7969\n7970\n7971\n7972\n7973\n7974\n7975\n7976\n7977\n7978\n7979\n7980\n7981\n7982\n7983\n7984\n7985\n7986\n7987\n7988\n7989\n7990\n7991\n7992\n7993\n7994\n7995\n7996\n7997\n7998\n7999\n8000\n8001\n8002\n8003\n8004\n8005\n8006\n8007\n8008\n8009\n8010\n8011\n8012\n8013\n8014\n8015\n8016\n8017\n8018\n8019\n8020\n8021\n8022\n8023\n8024\n8025\n8026\n8027\n8028\n8029\n8030\n8031\n8032\n8033\n8034\n8035\n8036\n8037\n8038\n8039\n8040\n8041\n8042\n8043\n8044\n8045\n8046\n8047\n8048\n8049\n8050\n8051\n8052\n8053\n8054\n8055\n8056\n8057\n8058\n8059\n8060\n8061\n8062\n8063\n8064\n8065\n8066\n8067\n8068\n8069\n8070\n8071\n8072\n8073\n8074\n8075\n8076\n8077\n8078\n8079\n8080\n8081\n8082\n8083\n8084\n8085\n8086\n8087\n8088\n8089\n8090\n8091\n8092\n8093\n8094\n8095\n8096\n8097\n8098\n8099\n8100\n8101\n8102\n8103\n8104\n8105\n8106\n8107\n8108\n8109\n8110\n8111\n8112\n8113\n8114\n8115\n8116\n8117\n8118\n8119\n8120\n8121\n8122\n8123\n8124\n8125\n8126\n8127\n8128\n8129\n8130\n8131\n8132\n8133\n8134\n8135\n8136\n8137\n8138\n8139\n8140\n8141\n8142\n8143\n8144\n8145\n8146\n8147\n8148\n8149\n8150\n8151\n8152\n8153\n8154\n8155\n8156\n8157\n8158\n8159\n8160\n8161\n8162\n8163\n8164\n8165\n8166\n8167\n8168\n8169\n8170\n8171\n8172\n8173\n8174\n8175\n8176\n8177\n8178\n8179\n8180\n8181\n8182\n8183\n8184\n8185\n8186\n8187\n8188\n8189\n8190\n8191\n8192\n8193\n8194\n8195\n8196\n8197\n8198\n8199\n8200\n8201\n8202\n8203\n8204\n8205\n8206\n8207\n8208\n8209\n8210\n8211\n8212\n8213\n8214\n8215\n8216\n8217\n8218\n8219\n8220\n8221\n8222\n8223\n8224\n8225\n8226\n8227\n8228\n8229\n8230\n8231\n8232\n8233\n8234\n8235\n8236\n8237\n8238\n8239\n8240\n8241\n8242\n8243\n8244\n8245\n8246\n8247\n8248\n8249\n8250\n8251\n8252\n8253\n8254\n8255\n8256\n8257\n8258\n8259\n8260\n8261\n8262\n8263\n8264\n8265\n8266\n8267\n8268\n8269\n8270\n8271\n8272\n8273\n8274\n8275\n8276\n8277\n8278\n8279\n8280\n8281\n8282\n8283\n8284\n8285\n8286\n8287\n8288\n8289\n8290\n8291\n8292\n8293\n8294\n8295\n8296\n8297\n8298\n8299\n8300\n8301\n8302\n8303\n8304\n8305\n8306\n8307\n8308\n8309\n8310\n8311\n8312\n8313\n8314\n8315\n8316\n8317\n8318\n8319\n8320\n8321\n8322\n8323\n8324\n8325\n8326\n8327\n8328\n8329\n8330\n8331\n8332\n8333\n8334\n8335\n8336\n8337\n8338\n8339\n8340\n8341\n8342\n8343\n8344\n8345\n8346\n8347\n8348\n8349\n8350\n8351\n8352\n8353\n8354\n8355\n8356\n8357\n8358\n8359\n8360\n8361\n8362\n8363\n8364\n8365\n8366\n8367\n8368\n8369\n8370\n8371\n8372\n8373\n8374\n8375\n8376\n8377\n8378\n8379\n8380\n8381\n8382\n8383\n8384\n8385\n8386\n8387\n8388\n8389\n8390\n8391\n8392\n8393\n8394\n8395\n8396\n8397\n8398\n8399\n8400\n8401\n8402\n8403\n8404\n8405\n8406\n8407\n8408\n8409\n8410\n8411\n8412\n8413\n8414\n8415\n8416\n8417\n8418\n8419\n8420\n8421\n8422\n8423\n8424\n8425\n8426\n8427\n8428\n8429\n8430\n8431\n8432\n8433\n8434\n8435\n8436\n8437\n8438\n8439\n8440\n8441\n8442\n8443\n8444\n8445\n8446\n8447\n8448\n8449\n8450\n8451\n8452\n8453\n8454\n8455\n8456\n8457\n8458\n8459\n8460\n8461\n8462\n8463\n8464\n8465\n8466\n8467\n8468\n8469\n8470\n8471\n8472\n8473\n8474\n8475\n8476\n8477\n8478\n8479\n8480\n8481\n8482\n8483\n8484\n8485\n8486\n8487\n8488\n8489\n8490\n8491\n8492\n8493\n8494\n8495\n8496\n8497\n8498\n8499\n8500\n8501\n8502\n8503\n8504\n8505\n8506\n8507\n8508\n8509\n8510\n8511\n8512\n8513\n8514\n8515\n8516\n8517\n8518\n8519\n8520\n8521\n8522\n8523\n8524\n8525\n8526\n8527\n8528\n8529\n8530\n8531\n8532\n8533\n8534\n8535\n8536\n8537\n8538\n8539\n8540\n8541\n8542\n8543\n8544\n8545\n8546\n8547\n8548\n8549\n8550\n8551\n8552\n8553\n8554\n8555\n8556\n8557\n8558\n8559\n8560\n8561\n8562\n8563\n8564\n8565\n8566\n8567\n8568\n8569\n8570\n8571\n8572\n8573\n8574\n8575\n8576\n8577\n8578\n8579\n8580\n8581\n8582\n8583\n8584\n8585\n8586\n8587\n8588\n8589\n8590\n8591\n8592\n8593\n8594\n8595\n8596\n8597\n8598\n8599\n8600\n8601\n8602\n8603\n8604\n8605\n8606\n8607\n8608\n8609\n8610\n8611\n8612\n8613\n8614\n8615\n8616\n8617\n8618\n8619\n8620\n8621\n8622\n8623\n8624\n8625\n8626\n8627\n8628\n8629\n8630\n8631\n8632\n8633\n8634\n8635\n8636\n8637\n8638\n8639\n8640\n8641\n8642\n8643\n8644\n8645\n8646\n8647\n8648\n8649\n8650\n8651\n8652\n8653\n8654\n8655\n8656\n8657\n8658\n8659\n8660\n8661\n8662\n8663\n8664\n8665\n8666\n8667\n8668\n8669\n8670\n8671\n8672\n8673\n8674\n8675\n8676\n8677\n8678\n8679\n8680\n8681\n8682\n8683\n8684\n8685\n8686\n8687\n8688\n8689\n8690\n8691\n8692\n8693\n8694\n8695\n8696\n8697\n8698\n8699\n8700\n8701\n8702\n8703\n8704\n8705\n8706\n8707\n8708\n8709\n8710\n8711\n8712\n8713\n8714\n8715\n8716\n8717\n8718\n8719\n8720\n8721\n8722\n8723\n8724\n8725\n8726\n8727\n8728\n8729\n8730\n8731\n8732\n8733\n8734\n8735\n8736\n8737\n8738\n8739\n8740\n8741\n8742\n8743\n8744\n8745\n8746\n8747\n8748\n8749\n8750\n8751\n8752\n8753\n8754\n8755\n8756\n8757\n8758\n8759\n8760\n8761\n8762\n8763\n8764\n8765\n8766\n8767\n8768\n8769\n8770\n8771\n8772\n8773\n8774\n8775\n8776\n8777\n8778\n8779\n8780\n8781\n8782\n8783\n8784\n8785\n8786\n8787\n8788\n8789\n8790\n8791\n8792\n8793\n8794\n8795\n8796\n8797\n8798\n8799\n8800\n8801\n8802\n8803\n8804\n8805\n8806\n8807\n8808\n8809\n8810\n8811\n8812\n8813\n8814\n8815\n8816\n8817\n8818\n8819\n8820\n8821\n8822\n8823\n8824\n8825\n8826\n8827\n8828\n8829\n8830\n8831\n8832\n8833\n8834\n8835\n8836\n8837\n8838\n8839\n8840\n8841\n8842\n8843\n8844\n8845\n8846\n8847\n8848\n8849\n8850\n8851\n8852\n8853\n8854\n8855\n8856\n8857\n8858\n8859\n8860\n8861\n8862\n8863\n8864\n8865\n8866\n8867\n8868\n8869\n8870\n8871\n8872\n8873\n8874\n8875\n8876\n8877\n8878\n8879\n8880\n8881\n8882\n8883\n8884\n8885\n8886\n8887\n8888\n8889\n8890\n8891\n8892\n8893\n8894\n8895\n8896\n8897\n8898\n8899\n8900\n8901\n8902\n8903\n8904\n8905\n8906\n8907\n8908\n8909\n8910\n8911\n8912\n8913\n8914\n8915\n8916\n8917\n8918\n8919\n8920\n8921\n8922\n8923\n8924\n8925\n8926\n8927\n8928\n8929\n8930\n8931\n8932\n8933\n8934\n8935\n8936\n8937\n8938\n8939\n8940\n8941\n8942\n8943\n8944\n8945\n8946\n8947\n8948\n8949\n8950\n8951\n8952\n8953\n8954\n8955\n8956\n8957\n8958\n8959\n8960\n8961\n8962\n8963\n8964\n8965\n8966\n8967\n8968\n8969\n8970\n8971\n8972\n8973\n8974\n8975\n8976\n8977\n8978\n8979\n8980\n8981\n8982\n8983\n8984\n8985\n8986\n8987\n8988\n8989\n8990\n8991\n8992\n8993\n8994\n8995\n8996\n8997\n8998\n8999\n9000\n9001\n9002\n9003\n9004\n9005\n9006\n9007\n9008\n9009\n9010\n9011\n9012\n9013\n9014\n9015\n9016\n9017\n9018\n9019\n9020\n9021\n9022\n9023\n9024\n9025\n9026\n9027\n9028\n9029\n9030\n9031\n9032\n9033\n9034\n9035\n9036\n9037\n9038\n9039\n9040\n9041\n9042\n9043\n9044\n9045\n9046\n9047\n9048\n9049\n9050\n9051\n9052\n9053\n9054\n9055\n9056\n9057\n9058\n9059\n9060\n9061\n9062\n9063\n9064\n9065\n9066\n9067\n9068\n9069\n9070\n9071\n9072\n9073\n9074\n9075\n9076\n9077\n9078\n9079\n9080\n9081\n9082\n9083\n9084\n9085\n9086\n9087\n9088\n9089\n9090\n9091\n9092\n9093\n9094\n9095\n9096\n9097\n9098\n9099\n9100\n9101\n9102\n9103\n9104\n9105\n9106\n9107\n9108\n9109\n9110\n9111\n9112\n9113\n9114\n9115\n9116\n9117\n9118\n9119\n9120\n9121\n9122\n9123\n9124\n9125\n9126\n9127\n9128\n9129\n9130\n9131\n9132\n9133\n9134\n9135\n9136\n9137\n9138\n9139\n9140\n9141\n9142\n9143\n9144\n9145\n9146\n9147\n9148\n9149\n9150\n9151\n9152\n9153\n9154\n9155\n9156\n9157\n9158\n9159\n9160\n9161\n9162\n9163\n9164\n9165\n9166\n9167\n9168\n9169\n9170\n9171\n9172\n9173\n9174\n9175\n9176\n9177\n9178\n9179\n9180\n9181\n9182\n9183\n9184\n9185\n9186\n9187\n9188\n9189\n9190\n9191\n9192\n9193\n9194\n9195\n9196\n9197\n9198\n9199\n9200\n9201\n9202\n9203\n9204\n9205\n9206\n9207\n9208\n9209\n9210\n9211\n9212\n9213\n9214\n9215\n9216\n9217\n9218\n9219\n9220\n9221\n9222\n9223\n9224\n9225\n9226\n9227\n9228\n9229\n9230\n9231\n9232\n9233\n9234\n9235\n9236\n9237\n9238\n9239\n9240\n9241\n9242\n9243\n9244\n9245\n9246\n9247\n9248\n9249\n9250\n9251\n9252\n9253\n9254\n9255\n9256\n9257\n9258\n9259\n9260\n9261\n9262\n9263\n9264\n9265\n9266\n9267\n9268\n9269\n9270\n9271\n9272\n9273\n9274\n9275\n9276\n9277\n9278\n9279\n9280\n9281\n9282\n9283\n9284\n9285\n9286\n9287\n9288\n9289\n9290\n9291\n9292\n9293\n9294\n9295\n9296\n9297\n9298\n9299\n9300\n9301\n9302\n9303\n9304\n9305\n9306\n9307\n9308\n9309\n9310\n9311\n9312\n9313\n9314\n9315\n9316\n9317\n9318\n9319\n9320\n9321\n9322\n9323\n9324\n9325\n9326\n9327\n9328\n9329\n9330\n9331\n9332\n9333\n9334\n9335\n9336\n9337\n9338\n9339\n9340\n9341\n9342\n9343\n9344\n9345\n9346\n9347\n9348\n9349\n9350\n9351\n9352\n9353\n9354\n9355\n9356\n9357\n9358\n9359\n9360\n9361\n9362\n9363\n9364\n9365\n9366\n9367\n9368\n9369\n9370\n9371\n9372\n9373\n9374\n9375\n9376\n9377\n9378\n9379\n9380\n9381\n9382\n9383\n9384\n9385\n9386\n9387\n9388\n9389\n9390\n9391\n9392\n9393\n9394\n9395\n9396\n9397\n9398\n9399\n9400\n9401\n9402\n9403\n9404\n9405\n9406\n9407\n9408\n9409\n9410\n9411\n9412\n9413\n9414\n9415\n9416\n9417\n9418\n9419\n9420\n9421\n9422\n9423\n9424\n9425\n9426\n9427\n9428\n9429\n9430\n9431\n9432\n9433\n9434\n9435\n9436\n9437\n9438\n9439\n9440\n9441\n9442\n9443\n9444\n9445\n9446\n9447\n9448\n9449\n9450\n9451\n9452\n9453\n9454\n9455\n9456\n9457\n9458\n9459\n9460\n9461\n9462\n9463\n9464\n9465\n9466\n9467\n9468\n9469\n9470\n9471\n9472\n9473\n9474\n9475\n9476\n9477\n9478\n9479\n9480\n9481\n9482\n9483\n9484\n9485\n9486\n9487\n9488\n9489\n9490\n9491\n9492\n9493\n9494\n9495\n9496\n9497\n9498\n9499\n9500\n9501\n9502\n9503\n9504\n9505\n9506\n9507\n9508\n9509\n9510\n9511\n9512\n9513\n9514\n9515\n9516\n9517\n9518\n9519\n9520\n9521\n9522\n9523\n9524\n9525\n9526\n9527\n9528\n9529\n9530\n9531\n9532\n9533\n9534\n9535\n9536\n9537\n9538\n9539\n9540\n9541\n9542\n9543\n9544\n9545\n9546\n9547\n9548\n9549\n9550\n9551\n9552\n9553\n9554\n9555\n9556\n9557\n9558\n9559\n9560\n9561\n9562\n9563\n9564\n9565\n9566\n9567\n9568\n9569\n9570\n9571\n9572\n9573\n9574\n9575\n9576\n9577\n9578\n9579\n9580\n9581\n9582\n9583\n9584\n9585\n9586\n9587\n9588\n9589\n9590\n9591\n9592\n9593\n9594\n9595\n9596\n9597\n9598\n9599\n9600\n9601\n9602\n9603\n9604\n9605\n9606\n9607\n9608\n9609\n9610\n9611\n9612\n9613\n9614\n9615\n9616\n9617\n9618\n9619\n9620\n9621\n9622\n9623\n9624\n9625\n9626\n9627\n9628\n9629\n9630\n9631\n9632\n9633\n9634\n9635\n9636\n9637\n9638\n9639\n9640\n9641\n9642\n9643\n9644\n9645\n9646\n9647\n9648\n9649\n9650\n9651\n9652\n9653\n9654\n9655\n9656\n9657\n9658\n9659\n9660\n9661\n9662\n9663\n9664\n9665\n9666\n9667\n9668\n9669\n9670\n9671\n9672\n9673\n9674\n9675\n9676\n9677\n9678\n9679\n9680\n9681\n9682\n9683\n9684\n9685\n9686\n9687\n9688\n9689\n9690\n9691\n9692\n9693\n9694\n9695\n9696\n9697\n9698\n9699\n9700\n9701\n9702\n9703\n9704\n9705\n9706\n9707\n9708\n9709\n9710\n9711\n9712\n9713\n9714\n9715\n9716\n9717\n9718\n9719\n9720\n9721\n9722\n9723\n9724\n9725\n9726\n9727\n9728\n9729\n9730\n9731\n9732\n9733\n9734\n9735\n9736\n9737\n9738\n9739\n9740\n9741\n9742\n9743\n9744\n9745\n9746\n9747\n9748\n9749\n9750\n9751\n9752\n9753\n9754\n9755\n9756\n9757\n9758\n9759\n9760\n9761\n9762\n9763\n9764\n9765\n9766\n9767\n9768\n9769\n9770\n9771\n9772\n9773\n9774\n9775\n9776\n9777\n9778\n9779\n9780\n9781\n9782\n9783\n9784\n9785\n9786\n9787\n9788\n9789\n9790\n9791\n9792\n9793\n9794\n9795\n9796\n9797\n9798\n9799\n9800\n9801\n9802\n9803\n9804\n9805\n9806\n9807\n9808\n9809\n9810\n9811\n9812\n9813\n9814\n9815\n9816\n9817\n9818\n9819\n9820\n9821\n9822\n9823\n9824\n9825\n9826\n9827\n9828\n9829\n9830\n9831\n9832\n9833\n9834\n9835\n9836\n9837\n9838\n9839\n9840\n9841\n9842\n9843\n9844\n9845\n9846\n9847\n9848\n9849\n9850\n9851\n9852\n9853\n9854\n9855\n9856\n9857\n9858\n9859\n9860\n9861\n9862\n9863\n9864\n9865\n9866\n9867\n9868\n9869\n9870\n9871\n9872\n9873\n9874\n9875\n9876\n9877\n9878\n9879\n9880\n9881\n9882\n9883\n9884\n9885\n9886\n9887\n9888\n9889\n9890\n9891\n9892\n9893\n9894\n9895\n9896\n9897\n9898\n9899\n9900\n9901\n9902\n9903\n9904\n9905\n9906\n9907\n9908\n9909\n9910\n9911\n9912\n9913\n9914\n9915\n9916\n9917\n9918\n9919\n9920\n9921\n9922\n9923\n9924\n9925\n9926\n9927\n9928\n9929\n9930\n9931\n9932\n9933\n9934\n9935\n9936\n9937\n9938\n9939\n9940\n9941\n9942\n9943\n9944\n9945\n9946\n9947\n9948\n9949\n9950\n9951\n9952\n9953\n9954\n9955\n9956\n9957\n9958\n9959\n9960\n9961\n9962\n9963\n9964\n9965\n9966\n9967\n9968\n9969\n9970\n9971\n9972\n9973\n9974\n9975\n9976\n9977\n9978\n9979\n9980\n9981\n9982\n9983\n9984\n9985\n9986\n9987\n9988\n9989\n9990\n9991\n9992\n9993\n9994\n9995\n9996\n9997\n9998\n9999\n10000\n10001\n10002\n10003\n10004\n10005\n10006\n10007\n10008\n10009\n10010\n10011\n10012\n10013\n10014\n10015\n10016\n10017\n10018\n10019\n10020\n10021\n10022\n10023\n10024\n10025\n10026\n10027\n10028\n10029\n10030\n10031\n10032\n10033\n10034\n10035\n10036\n10037\n10038\n10039\n10040\n10041\n10042\n10043\n10044\n10045\n10046\n10047\n10048\n10049\n10050\n10051\n10052\n10053\n10054\n10055\n10056\n10057\n10058\n10059\n10060\n10061\n10062\n10063\n10064\n10065\n10066\n10067\n10068\n10069\n10070\n10071\n10072\n10073\n10074\n10075\n10076\n10077\n10078\n10079\n10080\n10081\n10082\n10083\n10084\n10085\n10086\n10087\n10088\n10089\n10090\n10091\n10092\n10093\n10094\n10095\n10096\n10097\n10098\n10099\n10100\n10101\n10102\n10103\n10104\n10105\n10106\n10107\n10108\n10109\n10110\n10111\n10112\n10113\n10114\n10115\n10116\n10117\n10118\n10119\n10120\n10121\n10122\n10123\n10124\n10125\n10126\n10127\n10128\n10129\n10130\n10131\n10132\n10133\n10134\n10135\n10136\n10137\n10138\n10139\n10140\n10141\n10142\n10143\n10144\n10145\n10146\n10147\n10148\n10149\n10150\n10151\n10152\n10153\n10154\n10155\n10156\n10157\n10158\n10159\n10160\n10161\n10162\n10163\n10164\n10165\n10166\n10167\n10168\n10169\n10170\n10171\n10172\n10173\n10174\n10175\n10176\n10177\n10178\n10179\n10180\n10181\n10182\n10183\n10184\n10185\n10186\n10187\n10188\n10189\n10190\n10191\n10192\n10193\n10194\n10195\n10196\n10197\n10198\n10199\n10200\n10201\n10202\n10203\n10204\n10205\n10206\n10207\n10208\n10209\n10210\n10211\n10212\n10213\n10214\n10215\n10216\n10217\n10218\n10219\n10220\n10221\n10222\n10223\n10224\n10225\n10226\n10227\n10228\n10229\n10230\n10231\n10232\n10233\n10234\n10235\n10236\n10237\n10238\n10239\n10240\n10241\n10242\n10243\n10244\n10245\n10246\n10247\n10248\n10249\n10250\n10251\n10252\n10253\n10254\n10255\n10256\n10257\n10258\n10259\n10260\n10261\n10262\n10263\n10264\n10265\n10266\n10267\n10268\n10269\n10270\n10271\n10272\n10273\n10274\n10275\n10276\n10277\n10278\n10279\n10280\n10281\n10282\n10283\n10284\n10285\n10286\n10287\n10288\n10289\n10290\n10291\n10292\n10293\n10294\n10295\n10296\n10297\n10298\n10299\n10300\n10301\n10302\n10303\n10304\n10305\n10306\n10307\n10308\n10309\n10310\n10311\n10312\n10313\n10314\n10315\n10316\n10317\n10318\n10319\n10320\n10321\n10322\n10323\n10324\n10325\n10326\n10327\n10328\n10329\n10330\n10331\n10332\n10333\n10334\n10335\n10336\n10337\n10338\n10339\n10340\n10341\n10342\n10343\n10344\n10345\n10346\n10347\n10348\n10349\n10350\n10351\n10352\n10353\n10354\n10355\n10356\n10357\n10358\n10359\n10360\n10361\n10362\n10363\n10364\n10365\n10366\n10367\n10368\n10369\n10370\n10371\n10372\n10373\n10374\n10375\n10376\n10377\n10378\n10379\n10380\n10381\n10382\n10383\n10384\n10385\n10386\n10387\n10388\n10389\n10390\n10391\n10392\n10393\n10394\n10395\n10396\n10397\n10398\n10399\n10400\n10401\n10402\n10403\n10404\n10405\n10406\n10407\n10408\n10409\n10410\n10411\n10412\n10413\n10414\n10415\n10416\n10417\n10418\n10419\n10420\n10421\n10422\n10423\n10424\n10425\n10426\n10427\n10428\n10429\n10430\n10431\n10432\n10433\n10434\n10435\n10436\n10437\n10438\n10439\n10440\n10441\n10442\n10443\n10444\n10445\n10446\n10447\n10448\n10449\n10450\n10451\n10452\n10453\n10454\n10455\n10456\n10457\n10458\n10459\n10460\n10461\n10462\n10463\n10464\n10465\n10466\n10467\n10468\n10469\n10470\n10471\n10472\n10473\n10474\n10475\n10476\n10477\n10478\n10479\n10480\n10481\n10482\n10483\n10484\n10485\n10486\n10487\n10488\n10489\n10490\n10491\n10492\n10493\n10494\n10495\n10496\n10497\n10498\n10499\n10500\n10501\n10502\n10503\n10504\n10505\n10506\n10507\n10508\n10509\n10510\n10511\n10512\n10513\n10514\n10515\n10516\n10517\n10518\n10519\n10520\n10521\n10522\n10523\n10524\n10525\n10526\n10527\n10528\n10529\n10530\n10531\n10532\n10533\n10534\n10535\n10536\n10537\n10538\n10539\n10540\n10541\n10542\n10543\n10544\n10545\n10546\n10547\n10548\n10549\n10550\n10551\n10552\n10553\n10554\n10555\n10556\n10557\n10558\n10559\n10560\n10561\n10562\n10563\n10564\n10565\n10566\n10567\n10568\n10569\n10570\n10571\n10572\n10573\n10574\n10575\n10576\n10577\n10578\n10579\n10580\n10581\n10582\n10583\n10584\n10585\n10586\n10587\n10588\n10589\n10590\n10591\n10592\n10593\n10594\n10595\n10596\n10597\n10598\n10599\n10600\n10601\n10602\n10603\n10604\n10605\n10606\n10607\n10608\n10609\n10610\n10611\n10612\n10613\n10614\n10615\n10616\n10617\n10618\n10619\n10620\n10621\n10622\n10623\n10624\n10625\n10626\n10627\n10628\n10629\n10630\n10631\n10632\n10633\n10634\n10635\n10636\n10637\n10638\n10639\n10640\n10641\n10642\n10643\n10644\n10645\n10646\n10647\n10648\n10649\n10650\n10651\n10652\n10653\n10654\n10655\n10656\n10657\n10658\n10659\n10660\n10661\n10662\n10663\n10664\n10665\n10666\n10667\n10668\n10669\n10670\n10671\n10672\n10673\n10674\n10675\n10676\n10677\n10678\n10679\n10680\n10681\n10682\n10683\n10684\n10685\n10686\n10687\n10688\n10689\n10690\n10691\n10692\n10693\n10694\n10695\n10696\n10697\n10698\n10699\n10700\n10701\n10702\n10703\n10704\n10705\n10706\n10707\n10708\n10709\n10710\n10711\n10712\n10713\n10714\n10715\n10716\n10717\n10718\n10719\n10720\n10721\n10722\n10723\n10724\n10725\n10726\n10727\n10728\n10729\n10730\n10731\n10732\n10733\n10734\n10735\n10736\n10737\n10738\n10739\n10740\n10741\n10742\n10743\n10744\n10745\n10746\n10747\n10748\n10749\n10750\n10751\n10752\n10753\n10754\n10755\n10756\n10757\n10758\n10759\n10760\n10761\n10762\n10763\n10764\n10765\n10766\n10767\n10768\n10769\n10770\n10771\n10772\n10773\n10774\n10775\n10776\n10777\n10778\n10779\n10780\n10781\n10782\n10783\n10784\n10785\n10786\n10787\n10788\n10789\n10790\n10791\n10792\n10793\n10794\n10795\n10796\n10797\n10798\n10799\n10800\n10801\n10802\n10803\n10804\n10805\n10806\n10807\n10808\n10809\n10810\n10811\n10812\n10813\n10814\n10815\n10816\n10817\n10818\n10819\n10820\n10821\n10822\n10823\n10824\n10825\n10826\n10827\n10828\n10829\n10830\n10831\n10832\n10833\n10834\n10835\n10836\n10837\n10838\n10839\n10840\n10841\n10842\n10843\n10844\n10845\n10846\n10847\n10848\n10849\n10850\n10851\n10852\n10853\n10854\n10855\n10856\n10857\n10858\n10859\n10860\n10861\n10862\n10863\n10864\n10865\n10866\n10867\n10868\n10869\n10870\n10871\n10872\n10873\n10874\n10875\n10876\n10877\n10878\n10879\n10880\n10881\n10882\n10883\n10884\n10885\n10886\n10887\n10888\n10889\n10890\n10891\n10892\n10893\n10894\n10895\n10896\n10897\n10898\n10899\n10900\n10901\n10902\n10903\n10904\n10905\n10906\n10907\n10908\n10909\n10910\n10911\n10912\n10913\n10914\n10915\n10916\n10917\n10918\n10919\n10920\n10921\n10922\n10923\n10924\n10925\n10926\n10927\n10928\n10929\n10930\n10931\n10932\n10933\n10934\n10935\n10936\n10937\n10938\n10939\n10940\n10941\n10942\n10943\n10944\n10945\n10946\n10947\n10948\n10949\n10950\n10951\n10952\n10953\n10954\n10955\n10956\n10957\n10958\n10959\n10960\n10961\n10962\n10963\n10964\n10965\n10966\n10967\n10968\n10969\n10970\n10971\n10972\n10973\n10974\n10975\n10976\n10977\n10978\n10979\n10980\n10981\n10982\n10983\n10984\n10985\n10986\n10987\n10988\n10989\n10990\n10991\n10992\n10993\n10994\n10995\n10996\n10997\n10998\n10999\n11000\n11001\n11002\n11003\n11004\n11005\n11006\n11007\n11008\n11009\n11010\n11011\n11012\n11013\n11014\n11015\n11016\n11017\n11018\n11019\n11020\n11021\n11022\n11023\n11024\n11025\n11026\n11027\n11028\n11029\n11030\n11031\n11032\n11033\n11034\n11035\n11036\n11037\n11038\n11039\n11040\n11041\n11042\n11043\n11044\n11045\n11046\n11047\n11048\n11049\n11050\n11051\n11052\n11053\n11054\n11055\n11056\n11057\n11058\n11059\n11060\n11061\n11062\n11063\n11064\n11065\n11066\n11067\n11068\n11069\n11070\n11071\n11072\n11073\n11074\n11075\n11076\n11077\n11078\n11079\n11080\n11081\n11082\n11083\n11084\n11085\n11086\n11087\n11088\n11089\n11090\n11091\n11092\n11093\n11094\n11095\n11096\n11097\n11098\n11099\n11100\n11101\n11102\n11103\n11104\n11105\n11106\n11107\n11108\n11109\n11110\n11111\n11112\n11113\n11114\n11115\n11116\n11117\n11118\n11119\n11120\n11121\n11122\n11123\n11124\n11125\n11126\n11127\n11128\n11129\n11130\n11131\n11132\n11133\n11134\n11135\n11136\n11137\n11138\n11139\n11140\n11141\n11142\n11143\n11144\n11145\n11146\n11147\n11148\n11149\n11150\n11151\n11152\n11153\n11154\n11155\n11156\n11157\n11158\n11159\n11160\n11161\n11162\n11163\n11164\n11165\n11166\n11167\n11168\n11169\n11170\n11171\n11172\n11173\n11174\n11175\n11176\n11177\n11178\n11179\n11180\n11181\n11182\n11183\n11184\n11185\n11186\n11187\n11188\n11189\n11190\n11191\n11192\n11193\n11194\n11195\n11196\n11197\n11198\n11199\n11200\n11201\n11202\n11203\n11204\n11205\n11206\n11207\n11208\n11209\n11210\n11211\n11212\n11213\n11214\n11215\n11216\n11217\n11218\n11219\n11220\n11221\n11222\n11223\n11224\n11225\n11226\n11227\n11228\n11229\n11230\n11231\n11232\n11233\n11234\n11235\n11236\n11237\n11238\n11239\n11240\n11241\n11242\n11243\n11244\n11245\n11246\n11247\n11248\n11249\n11250\n11251\n11252\n11253\n11254\n11255\n11256\n11257\n11258\n11259\n11260\n11261\n11262\n11263\n11264\n11265\n11266\n11267\n11268\n11269\n11270\n11271\n11272\n11273\n11274\n11275\n11276\n11277\n11278\n11279\n11280\n11281\n11282\n11283\n11284\n11285\n11286\n11287\n11288\n11289\n11290\n11291\n11292\n11293\n11294\n11295\n11296\n11297\n11298\n11299\n11300\n11301\n11302\n11303\n11304\n11305\n11306\n11307\n11308\n11309\n11310\n11311\n11312\n11313\n11314\n11315\n11316\n11317\n11318\n11319\n11320\n11321\n11322\n11323\n11324\n11325\n11326\n11327\n11328\n11329\n11330\n11331\n11332\n11333\n11334\n11335\n11336\n11337\n11338\n11339\n11340\n11341\n11342\n11343\n11344\n11345\n11346\n11347\n11348\n11349\n11350\n11351\n11352\n11353\n11354\n11355\n11356\n11357\n11358\n11359\n11360\n11361\n11362\n11363\n11364\n11365\n11366\n11367\n11368\n11369\n11370\n11371\n11372\n11373\n11374\n11375\n11376\n11377\n11378\n11379\n11380\n11381\n11382\n11383\n11384\n11385\n11386\n11387\n11388\n11389\n11390\n11391\n11392\n11393\n11394\n11395\n11396\n11397\n11398\n11399\n11400\n11401\n11402\n11403\n11404\n11405\n11406\n11407\n11408\n11409\n11410\n11411\n11412\n11413\n11414\n11415\n11416\n11417\n11418\n11419\n11420\n11421\n11422\n11423\n11424\n11425\n11426\n11427\n11428\n11429\n11430\n11431\n11432\n11433\n11434\n11435\n11436\n11437\n11438\n11439\n11440\n11441\n11442\n11443\n11444\n11445\n11446\n11447\n11448\n11449\n11450\n11451\n11452\n11453\n11454\n11455\n11456\n11457\n11458\n11459\n11460\n11461\n11462\n11463\n11464\n11465\n11466\n11467\n11468\n11469\n11470\n11471\n11472\n11473\n11474\n11475\n11476\n11477\n11478\n11479\n11480\n11481\n11482\n11483\n11484\n11485\n11486\n11487\n11488\n11489\n11490\n11491\n11492\n11493\n11494\n11495\n11496\n11497\n11498\n11499\n11500\n11501\n11502\n11503\n11504\n11505\n11506\n11507\n11508\n11509\n11510\n11511\n11512\n11513\n11514\n11515\n11516\n11517\n11518\n11519\n11520\n11521\n11522\n11523\n11524\n11525\n11526\n11527\n11528\n11529\n11530\n11531\n11532\n11533\n11534\n11535\n11536\n11537\n11538\n11539\n11540\n11541\n11542\n11543\n11544\n11545\n11546\n11547\n11548\n11549\n11550\n11551\n11552\n11553\n11554\n11555\n11556\n11557\n11558\n11559\n11560\n11561\n11562\n11563\n11564\n11565\n11566\n11567\n11568\n11569\n11570\n11571\n11572\n11573\n11574\n11575\n11576\n11577\n11578\n11579\n11580\n11581\n11582\n11583\n11584\n11585\n11586\n11587\n11588\n11589\n11590\n11591\n11592\n11593\n11594\n11595\n11596\n11597\n11598\n11599\n11600\n11601\n11602\n11603\n11604\n11605\n11606\n11607\n11608\n11609\n11610\n11611\n11612\n11613\n11614\n11615\n11616\n11617\n11618\n11619\n11620\n11621\n11622\n11623\n11624\n11625\n11626\n11627\n11628\n11629\n11630\n11631\n11632\n11633\n11634\n11635\n11636\n11637\n11638\n11639\n11640\n11641\n11642\n11643\n11644\n11645\n11646\n11647\n11648\n11649\n11650\n11651\n11652\n11653\n11654\n11655\n11656\n11657\n11658\n11659\n11660\n11661\n11662\n11663\n11664\n11665\n11666\n11667\n11668\n11669\n11670\n11671\n11672\n11673\n11674\n11675\n11676\n11677\n11678\n11679\n11680\n11681\n11682\n11683\n11684\n11685\n11686\n11687\n11688\n11689\n11690\n11691\n11692\n11693\n11694\n11695\n11696\n11697\n11698\n11699\n11700\n11701\n11702\n11703\n11704\n11705\n11706\n11707\n11708\n11709\n11710\n11711\n11712\n11713\n11714\n11715\n11716\n11717\n11718\n11719\n11720\n11721\n11722\n11723\n11724\n11725\n11726\n11727\n11728\n11729\n11730\n11731\n11732\n11733\n11734\n11735\n11736\n11737\n11738\n11739\n11740\n11741\n11742\n11743\n11744\n11745\n11746\n11747\n11748\n11749\n11750\n11751\n11752\n11753\n11754\n11755\n11756\n11757\n11758\n11759\n11760\n11761\n11762\n11763\n11764\n11765\n11766\n11767\n11768\n11769\n11770\n11771\n11772\n11773\n11774\n11775\n11776\n11777\n11778\n11779\n11780\n11781\n11782\n11783\n11784\n11785\n11786\n11787\n11788\n11789\n11790\n11791\n11792\n11793\n11794\n11795\n11796\n11797\n11798\n11799\n11800\n11801\n11802\n11803\n11804\n11805\n11806\n11807\n11808\n11809\n11810\n11811\n11812\n11813\n11814\n11815\n11816\n11817\n11818\n11819\n11820\n11821\n11822\n11823\n11824\n11825\n11826\n11827\n11828\n11829\n11830\n11831\n11832\n11833\n11834\n11835\n11836\n11837\n11838\n11839\n11840\n11841\n11842\n11843\n11844\n11845\n11846\n11847\n11848\n11849\n11850\n11851\n11852\n11853\n11854\n11855\n11856\n11857\n11858\n11859\n11860\n11861\n11862\n11863\n11864\n11865\n11866\n11867\n11868\n11869\n11870\n11871\n11872\n11873\n11874\n11875\n11876\n11877\n11878\n11879\n11880\n11881\n11882\n11883\n11884\n11885\n11886\n11887\n11888\n11889\n11890\n11891\n11892\n11893\n11894\n11895\n11896\n11897\n11898\n11899\n11900\n11901\n11902\n11903\n11904\n11905\n11906\n11907\n11908\n11909\n11910\n11911\n11912\n11913\n11914\n11915\n11916\n11917\n11918\n11919\n11920\n11921\n11922\n11923\n11924\n11925\n11926\n11927\n11928\n11929\n11930\n11931\n11932\n11933\n11934\n11935\n11936\n11937\n11938\n11939\n11940\n11941\n11942\n11943\n11944\n11945\n11946\n11947\n11948\n11949\n11950\n11951\n11952\n11953\n11954\n11955\n11956\n11957\n11958\n11959\n11960\n11961\n11962\n11963\n11964\n11965\n11966\n11967\n11968\n11969\n11970\n11971\n11972\n11973\n11974\n11975\n11976\n11977\n11978\n11979\n11980\n11981\n11982\n11983\n11984\n11985\n11986\n11987\n11988\n11989\n11990\n11991\n11992\n11993\n11994\n11995\n11996\n11997\n11998\n11999\n12000\n12001\n12002\n12003\n12004\n12005\n12006\n12007\n12008\n12009\n12010\n12011\n12012\n12013\n12014\n12015\n12016\n12017\n12018\n12019\n12020\n12021\n12022\n12023\n12024\n12025\n12026\n12027\n12028\n12029\n12030\n12031\n12032\n12033\n12034\n12035\n12036\n12037\n12038\n12039\n12040\n12041\n12042\n12043\n12044\n12045\n12046\n12047\n12048\n12049\n12050\n12051\n12052\n12053\n12054\n12055\n12056\n12057\n12058\n12059\n12060\n12061\n12062\n12063\n12064\n12065\n12066\n12067\n12068\n12069\n12070\n12071\n12072\n12073\n12074\n12075\n12076\n12077\n12078\n12079\n12080\n12081\n12082\n12083\n12084\n12085\n12086\n12087\n12088\n12089\n12090\n12091\n12092\n12093\n12094\n12095\n12096\n12097\n12098\n12099\n12100\n12101\n12102\n12103\n12104\n12105\n12106\n12107\n12108\n12109\n12110\n12111\n12112\n12113\n12114\n12115\n12116\n12117\n12118\n12119\n12120\n12121\n12122\n12123\n12124\n12125\n12126\n12127\n12128\n12129\n12130\n12131\n12132\n12133\n12134\n12135\n12136\n12137\n12138\n12139\n12140\n12141\n12142\n12143\n12144\n12145\n12146\n12147\n12148\n12149\n12150\n12151\n12152\n12153\n12154\n12155\n12156\n12157\n12158\n12159\n12160\n12161\n12162\n12163\n12164\n12165\n12166\n12167\n12168\n12169\n12170\n12171\n12172\n12173\n12174\n12175\n12176\n12177\n12178\n12179\n12180\n12181\n12182\n12183\n12184\n12185\n12186\n12187\n12188\n12189\n12190\n12191\n12192\n12193\n12194\n12195\n12196\n12197\n12198\n12199\n12200\n12201\n12202\n12203\n12204\n12205\n12206\n12207\n12208\n12209\n12210\n12211\n12212\n12213\n12214\n12215\n12216\n12217\n12218\n12219\n12220\n12221\n12222\n12223\n12224\n12225\n12226\n12227\n12228\n12229\n12230\n12231\n12232\n12233\n12234\n12235\n12236\n12237\n12238\n12239\n12240\n12241\n12242\n12243\n12244\n12245\n12246\n12247\n12248\n12249\n12250\n12251\n12252\n12253\n12254\n12255\n12256\n12257\n12258\n12259\n12260\n12261\n12262\n12263\n12264\n12265\n12266\n12267\n12268\n12269\n12270\n12271\n12272\n12273\n12274\n12275\n12276\n12277\n12278\n12279\n12280\n12281\n12282\n12283\n12284\n12285\n12286\n12287\n12288\n12289\n12290\n12291\n12292\n12293\n12294\n12295\n12296\n12297\n12298\n12299\n12300\n12301\n12302\n12303\n12304\n12305\n12306\n12307\n12308\n12309\n12310\n12311\n12312\n12313\n12314\n12315\n12316\n12317\n12318\n12319\n12320\n12321\n12322\n12323\n12324\n12325\n12326\n12327\n12328\n12329\n12330\n12331\n12332\n12333\n12334\n12335\n12336\n12337\n12338\n12339\n12340\n12341\n12342\n12343\n12344\n12345\n12346\n12347\n12348\n12349\n12350\n12351\n12352\n12353\n12354\n12355\n12356\n12357\n12358\n12359\n12360\n12361\n12362\n12363\n12364\n12365\n12366\n12367\n12368\n12369\n12370\n12371\n12372\n12373\n12374\n12375\n12376\n12377\n12378\n12379\n12380\n12381\n12382\n12383\n12384\n12385\n12386\n12387\n12388\n12389\n12390\n12391\n12392\n12393\n12394\n12395\n12396\n12397\n12398\n12399\n12400\n12401\n12402\n12403\n12404\n12405\n12406\n12407\n12408\n12409\n12410\n12411\n12412\n12413\n12414\n12415\n12416\n12417\n12418\n12419\n12420\n12421\n12422\n12423\n12424\n12425\n12426\n12427\n12428\n12429\n12430\n12431\n12432\n12433\n12434\n12435\n12436\n12437\n12438\n12439\n12440\n12441\n12442\n12443\n12444\n12445\n12446\n12447\n12448\n12449\n12450\n12451\n12452\n12453\n12454\n12455\n12456\n12457\n12458\n12459\n12460\n12461\n12462\n12463\n12464\n12465\n12466\n12467\n12468\n12469\n12470\n12471\n12472\n12473\n12474\n12475\n12476\n12477\n12478\n12479\n12480\n12481\n12482\n12483\n12484\n12485\n12486\n12487\n12488\n12489\n12490\n12491\n12492\n12493\n12494\n12495\n12496\n12497\n12498\n12499\n12500\n12501\n12502\n12503\n12504\n12505\n12506\n12507\n12508\n12509\n12510\n12511\n12512\n12513\n12514\n12515\n12516\n12517\n12518\n12519\n12520\n12521\n12522\n12523\n12524\n12525\n12526\n12527\n12528\n12529\n12530\n12531\n12532\n12533\n12534\n12535\n12536\n12537\n12538\n12539\n12540\n12541\n12542\n12543\n12544\n12545\n12546\n12547\n12548\n12549\n12550\n12551\n12552\n12553\n12554\n12555\n12556\n12557\n12558\n12559\n12560\n12561\n12562\n12563\n12564\n12565\n12566\n12567\n12568\n12569\n12570\n12571\n12572\n12573\n12574\n12575\n12576\n12577\n12578\n12579\n12580\n12581\n12582\n12583\n12584\n12585\n12586\n12587\n12588\n12589\n12590\n12591\n12592\n12593\n12594\n12595\n12596\n12597\n12598\n12599\n12600\n12601\n12602\n12603\n12604\n12605\n12606\n12607\n12608\n12609\n12610\n12611\n12612\n12613\n12614\n12615\n12616\n12617\n12618\n12619\n12620\n12621\n12622\n12623\n12624\n12625\n12626\n12627\n12628\n12629\n12630\n12631\n12632\n12633\n12634\n12635\n12636\n12637\n12638\n12639\n12640\n12641\n12642\n12643\n12644\n12645\n12646\n12647\n12648\n12649\n12650\n12651\n12652\n12653\n12654\n12655\n12656\n12657\n12658\n12659\n12660\n12661\n12662\n12663\n12664\n12665\n12666\n12667\n12668\n12669\n12670\n12671\n12672\n12673\n12674\n12675\n12676\n12677\n12678\n12679\n12680\n12681\n12682\n12683\n12684\n12685\n12686\n12687\n12688\n12689\n12690\n12691\n12692\n12693\n12694\n12695\n12696\n12697\n12698\n12699\n12700\n12701\n12702\n12703\n12704\n12705\n12706\n12707\n12708\n12709\n12710\n12711\n12712\n12713\n12714\n12715\n12716\n12717\n12718\n12719\n12720\n12721\n12722\n12723\n12724\n12725\n12726\n12727\n12728\n12729\n12730\n12731\n12732\n12733\n12734\n12735\n12736\n12737\n12738\n12739\n12740\n12741\n12742\n12743\n12744\n12745\n12746\n12747\n12748\n12749\n12750\n12751\n12752\n12753\n12754\n12755\n12756\n12757\n12758\n12759\n12760\n12761\n12762\n12763\n12764\n12765\n12766\n12767\n12768\n12769\n12770\n12771\n12772\n12773\n12774\n12775\n12776\n12777\n12778\n12779\n12780\n12781\n12782\n12783\n12784\n12785\n12786\n12787\n12788\n12789\n12790\n12791\n12792\n12793\n12794\n12795\n12796\n12797\n12798\n12799\n12800\n12801\n12802\n12803\n12804\n12805\n12806\n12807\n12808\n12809\n12810\n12811\n12812\n12813\n12814\n12815\n12816\n12817\n12818\n12819\n12820\n12821\n12822\n12823\n12824\n12825\n12826\n12827\n12828\n12829\n12830\n12831\n12832\n12833\n12834\n12835\n12836\n12837\n12838\n12839\n12840\n12841\n12842\n12843\n12844\n12845\n12846\n12847\n12848\n12849\n12850\n12851\n12852\n12853\n12854\n12855\n12856\n12857\n12858\n12859\n12860\n12861\n12862\n12863\n12864\n12865\n12866\n12867\n12868\n12869\n12870\n12871\n12872\n12873\n12874\n12875\n12876\n12877\n12878\n12879\n12880\n12881\n12882\n12883\n12884\n12885\n12886\n12887\n12888\n12889\n12890\n12891\n12892\n12893\n12894\n12895\n12896\n12897\n12898\n12899\n12900\n12901\n12902\n12903\n12904\n12905\n12906\n12907\n12908\n12909\n12910\n12911\n12912\n12913\n12914\n12915\n12916\n12917\n12918\n12919\n12920\n12921\n12922\n12923\n12924\n12925\n12926\n12927\n12928\n12929\n12930\n12931\n12932\n12933\n12934\n12935\n12936\n12937\n12938\n12939\n12940\n12941\n12942\n12943\n12944\n12945\n12946\n12947\n12948\n12949\n12950\n12951\n12952\n12953\n12954\n12955\n12956\n12957\n12958\n12959\n12960\n12961\n12962\n12963\n12964\n12965\n12966\n12967\n12968\n12969\n12970\n12971\n12972\n12973\n12974\n12975\n12976\n12977\n12978\n12979\n12980\n12981\n12982\n12983\n12984\n12985\n12986\n12987\n12988\n12989\n12990\n12991\n12992\n12993\n12994\n12995\n12996\n12997\n12998\n12999\n13000\n13001\n13002\n13003\n13004\n13005\n13006\n13007\n13008\n13009\n13010\n13011\n13012\n13013\n13014\n13015\n13016\n13017\n13018\n13019\n13020\n13021\n13022\n13023\n13024\n13025\n13026\n13027\n13028\n13029\n13030\n13031\n13032\n13033\n13034\n13035\n13036\n13037\n13038\n13039\n13040\n13041\n13042\n13043\n13044\n13045\n13046\n13047\n13048\n13049\n13050\n13051\n13052\n13053\n13054\n13055\n13056\n13057\n13058\n13059\n13060\n13061\n13062\n13063\n13064\n13065\n13066\n13067\n13068\n13069\n13070\n13071\n13072\n13073\n13074\n13075\n13076\n13077\n13078\n13079\n13080\n13081\n13082\n13083\n13084\n13085\n13086\n13087\n13088\n13089\n13090\n13091\n13092\n13093\n13094\n13095\n13096\n13097\n13098\n13099\n13100\n13101\n13102\n13103\n13104\n13105\n13106\n13107\n13108\n13109\n13110\n13111\n13112\n13113\n13114\n13115\n13116\n13117\n13118\n13119\n13120\n13121\n13122\n13123\n13124\n13125\n13126\n13127\n13128\n13129\n13130\n13131\n13132\n13133\n13134\n13135\n13136\n13137\n13138\n13139\n13140\n13141\n13142\n13143\n13144\n13145\n13146\n13147\n13148\n13149\n13150\n13151\n13152\n13153\n13154\n13155\n13156\n13157\n13158\n13159\n13160\n13161\n13162\n13163\n13164\n13165\n13166\n13167\n13168\n13169\n13170\n13171\n13172\n13173\n13174\n13175\n13176\n13177\n13178\n13179\n13180\n13181\n13182\n13183\n13184\n13185\n13186\n13187\n13188\n13189\n13190\n13191\n13192\n13193\n13194\n13195\n13196\n13197\n13198\n13199\n13200\n13201\n13202\n13203\n13204\n13205\n13206\n13207\n13208\n13209\n13210\n13211\n13212\n13213\n13214\n13215\n13216\n13217\n13218\n13219\n13220\n13221\n13222\n13223\n13224\n13225\n13226\n13227\n13228\n13229\n13230\n13231\n13232\n13233\n13234\n13235\n13236\n13237\n13238\n13239\n13240\n13241\n13242\n13243\n13244\n13245\n13246\n13247\n13248\n13249\n13250\n13251\n13252\n13253\n13254\n13255\n13256\n13257\n13258\n13259\n13260\n13261\n13262\n13263\n13264\n13265\n13266\n13267\n13268\n13269\n13270\n13271\n13272\n13273\n13274\n13275\n13276\n13277\n13278\n13279\n13280\n13281\n13282\n13283\n13284\n13285\n13286\n13287\n13288\n13289\n13290\n13291\n13292\n13293\n13294\n13295\n13296\n13297\n13298\n13299\n13300\n13301\n13302\n13303\n13304\n13305\n13306\n13307\n13308\n13309\n13310\n13311\n13312\n13313\n13314\n13315\n13316\n13317\n13318\n13319\n13320\n13321\n13322\n13323\n13324\n13325\n13326\n13327\n13328\n13329\n13330\n13331\n13332\n13333\n13334\n13335\n13336\n13337\n13338\n13339\n13340\n13341\n13342\n13343\n13344\n13345\n13346\n13347\n13348\n13349\n13350\n13351\n13352\n13353\n13354\n13355\n13356\n13357\n13358\n13359\n13360\n13361\n13362\n13363\n13364\n13365\n13366\n13367\n13368\n13369\n13370\n13371\n13372\n13373\n13374\n13375\n13376\n13377\n13378\n13379\n13380\n13381\n13382\n13383\n13384\n13385\n13386\n13387\n13388\n13389\n13390\n13391\n13392\n13393\n13394\n13395\n13396\n13397\n13398\n13399\n13400\n13401\n13402\n13403\n13404\n13405\n13406\n13407\n13408\n13409\n13410\n13411\n13412\n13413\n13414\n13415\n13416\n13417\n13418\n13419\n13420\n13421\n13422\n13423\n13424\n13425\n13426\n13427\n13428\n13429\n13430\n13431\n13432\n13433\n13434\n13435\n13436\n13437\n13438\n13439\n13440\n13441\n13442\n13443\n13444\n13445\n13446\n13447\n13448\n13449\n13450\n13451\n13452\n13453\n13454\n13455\n13456\n13457\n13458\n13459\n13460\n13461\n13462\n13463\n13464\n13465\n13466\n13467\n13468\n13469\n13470\n13471\n13472\n13473\n13474\n13475\n13476\n13477\n13478\n13479\n13480\n13481\n13482\n13483\n13484\n13485\n13486\n13487\n13488\n13489\n13490\n13491\n13492\n13493\n13494\n13495\n13496\n13497\n13498\n13499\n13500\n13501\n13502\n13503\n13504\n13505\n13506\n13507\n13508\n13509\n13510\n13511\n13512\n13513\n13514\n13515\n13516\n13517\n13518\n13519\n13520\n13521\n13522\n13523\n13524\n13525\n13526\n13527\n13528\n13529\n13530\n13531\n13532\n13533\n13534\n13535\n13536\n13537\n13538\n13539\n13540\n13541\n13542\n13543\n13544\n13545\n13546\n13547\n13548\n13549\n13550\n13551\n13552\n13553\n13554\n13555\n13556\n13557\n13558\n13559\n13560\n13561\n13562\n13563\n13564\n13565\n13566\n13567\n13568\n13569\n13570\n13571\n13572\n13573\n13574\n13575\n13576\n13577\n13578\n13579\n13580\n13581\n13582\n13583\n13584\n13585\n13586\n13587\n13588\n13589\n13590\n13591\n13592\n13593\n13594\n13595\n13596\n13597\n13598\n13599\n13600\n13601\n13602\n13603\n13604\n13605\n13606\n13607\n13608\n13609\n13610\n13611\n13612\n13613\n13614\n13615\n13616\n13617\n13618\n13619\n13620\n13621\n13622\n13623\n13624\n13625\n13626\n13627\n13628\n13629\n13630\n13631\n13632\n13633\n13634\n13635\n13636\n13637\n13638\n13639\n13640\n13641\n13642\n13643\n13644\n13645\n13646\n13647\n13648\n13649\n13650\n13651\n13652\n13653\n13654\n13655\n13656\n13657\n13658\n13659\n13660\n13661\n13662\n13663\n13664\n13665\n13666\n13667\n13668\n13669\n13670\n13671\n13672\n13673\n13674\n13675\n13676\n13677\n13678\n13679\n13680\n13681\n13682\n13683\n13684\n13685\n13686\n13687\n13688\n13689\n13690\n13691\n13692\n13693\n13694\n13695\n13696\n13697\n13698\n13699\n13700\n13701\n13702\n13703\n13704\n13705\n13706\n13707\n13708\n13709\n13710\n13711\n13712\n13713\n13714\n13715\n13716\n13717\n13718\n13719\n13720\n13721\n13722\n13723\n13724\n13725\n13726\n13727\n13728\n13729\n13730\n13731\n13732\n13733\n13734\n13735\n13736\n13737\n13738\n13739\n13740\n13741\n13742\n13743\n13744\n13745\n13746\n13747\n13748\n13749\n13750\n13751\n13752\n13753\n13754\n13755\n13756\n13757\n13758\n13759\n13760\n13761\n13762\n13763\n13764\n13765\n13766\n13767\n13768\n13769\n13770\n13771\n13772\n13773\n13774\n13775\n13776\n13777\n13778\n13779\n13780\n13781\n13782\n13783\n13784\n13785\n13786\n13787\n13788\n13789\n13790\n13791\n13792\n13793\n13794\n13795\n13796\n13797\n13798\n13799\n13800\n13801\n13802\n13803\n13804\n13805\n13806\n13807\n13808\n13809\n13810\n13811\n13812\n13813\n13814\n13815\n13816\n13817\n13818\n13819\n13820\n13821\n13822\n13823\n13824\n13825\n13826\n13827\n13828\n13829\n13830\n13831\n13832\n13833\n13834\n13835\n13836\n13837\n13838\n13839\n13840\n13841\n13842\n13843\n13844\n13845\n13846\n13847\n13848\n13849\n13850\n13851\n13852\n13853\n13854\n13855\n13856\n13857\n13858\n13859\n13860\n13861\n13862\n13863\n13864\n13865\n13866\n13867\n13868\n13869\n13870\n13871\n13872\n13873\n13874\n13875\n13876\n13877\n13878\n13879\n13880\n13881\n13882\n13883\n13884\n13885\n13886\n13887\n13888\n13889\n13890\n13891\n13892\n13893\n13894\n13895\n13896\n13897\n13898\n13899\n13900\n13901\n13902\n13903\n13904\n13905\n13906\n13907\n13908\n13909\n13910\n13911\n13912\n13913\n13914\n13915\n13916\n13917\n13918\n13919\n13920\n13921\n13922\n13923\n13924\n13925\n13926\n13927\n13928\n13929\n13930\n13931\n13932\n13933\n13934\n13935\n13936\n13937\n13938\n13939\n13940\n13941\n13942\n13943\n13944\n13945\n13946\n13947\n13948\n13949\n13950\n13951\n13952\n13953\n13954\n13955\n13956\n13957\n13958\n13959\n13960\n13961\n13962\n13963\n13964\n13965\n13966\n13967\n13968\n13969\n13970\n13971\n13972\n13973\n13974\n13975\n13976\n13977\n13978\n13979\n13980\n13981\n13982\n13983\n13984\n13985\n13986\n13987\n13988\n13989\n13990\n13991\n13992\n13993\n13994\n13995\n13996\n13997\n13998\n13999\n14000\n14001\n14002\n14003\n14004\n14005\n14006\n14007\n14008\n14009\n14010\n14011\n14012\n14013\n14014\n14015\n14016\n14017\n14018\n14019\n14020\n14021\n14022\n14023\n14024\n14025\n14026\n14027\n14028\n14029\n14030\n14031\n14032\n14033\n14034\n14035\n14036\n14037\n14038\n14039\n14040\n14041\n14042\n14043\n14044\n14045\n14046\n14047\n14048\n14049\n14050\n14051\n14052\n14053\n14054\n14055\n14056\n14057\n14058\n14059\n14060\n14061\n14062\n14063\n14064\n14065\n14066\n14067\n14068\n14069\n14070\n14071\n14072\n14073\n14074\n14075\n14076\n14077\n14078\n14079\n14080\n14081\n14082\n14083\n14084\n14085\n14086\n14087\n14088\n14089\n14090\n14091\n14092\n14093\n14094\n14095\n14096\n14097\n14098\n14099\n14100\n14101\n14102\n14103\n14104\n14105\n14106\n14107\n14108\n14109\n14110\n14111\n14112\n14113\n14114\n14115\n14116\n14117\n14118\n14119\n14120\n14121\n14122\n14123\n14124\n14125\n14126\n14127\n14128\n14129\n14130\n14131\n14132\n14133\n14134\n14135\n14136\n14137\n14138\n14139\n14140\n14141\n14142\n14143\n14144\n14145\n14146\n14147\n14148\n14149\n14150\n14151\n14152\n14153\n14154\n14155\n14156\n14157\n14158\n14159\n14160\n14161\n14162\n14163\n14164\n14165\n14166\n14167\n14168\n14169\n14170\n14171\n14172\n14173\n14174\n14175\n14176\n14177\n14178\n14179\n14180\n14181\n14182\n14183\n14184\n14185\n14186\n14187\n14188\n14189\n14190\n14191\n14192\n14193\n14194\n14195\n14196\n14197\n14198\n14199\n14200\n14201\n14202\n14203\n14204\n14205\n14206\n14207\n14208\n14209\n14210\n14211\n14212\n14213\n14214\n14215\n14216\n14217\n14218\n14219\n14220\n14221\n14222\n14223\n14224\n14225\n14226\n14227\n14228\n14229\n14230\n14231\n14232\n14233\n14234\n14235\n14236\n14237\n14238\n14239\n14240\n14241\n14242\n14243\n14244\n14245\n14246\n14247\n14248\n14249\n14250\n14251\n14252\n14253\n14254\n14255\n14256\n14257\n14258\n14259\n14260\n14261\n14262\n14263\n14264\n14265\n14266\n14267\n14268\n14269\n14270\n14271\n14272\n14273\n14274\n14275\n14276\n14277\n14278\n14279\n14280\n14281\n14282\n14283\n14284\n14285\n14286\n14287\n14288\n14289\n14290\n14291\n14292\n14293\n14294\n14295\n14296\n14297\n14298\n14299\n14300\n14301\n14302\n14303\n14304\n14305\n14306\n14307\n14308\n14309\n14310\n14311\n14312\n14313\n14314\n14315\n14316\n14317\n14318\n14319\n14320\n14321\n14322\n14323\n14324\n14325\n14326\n14327\n14328\n14329\n14330\n14331\n14332\n14333\n14334\n14335\n14336\n14337\n14338\n14339\n14340\n14341\n14342\n14343\n14344\n14345\n14346\n14347\n14348\n14349\n14350\n14351\n14352\n14353\n14354\n14355\n14356\n14357\n14358\n14359\n14360\n14361\n14362\n14363\n14364\n14365\n14366\n14367\n14368\n14369\n14370\n14371\n14372\n14373\n14374\n14375\n14376\n14377\n14378\n14379\n14380\n14381\n14382\n14383\n14384\n14385\n14386\n14387\n14388\n14389\n14390\n14391\n14392\n14393\n14394\n14395\n14396\n14397\n14398\n14399\n14400\n14401\n14402\n14403\n14404\n14405\n14406\n14407\n14408\n14409\n14410\n14411\n14412\n14413\n14414\n14415\n14416\n14417\n14418\n14419\n14420\n14421\n14422\n14423\n14424\n14425\n14426\n14427\n14428\n14429\n14430\n14431\n14432\n14433\n14434\n14435\n14436\n14437\n14438\n14439\n14440\n14441\n14442\n14443\n14444\n14445\n14446\n14447\n14448\n14449\n14450\n14451\n14452\n14453\n14454\n14455\n14456\n14457\n14458\n14459\n14460\n14461\n14462\n14463\n14464\n14465\n14466\n14467\n14468\n14469\n14470\n14471\n14472\n14473\n14474\n14475\n14476\n14477\n14478\n14479\n14480\n14481\n14482\n14483\n14484\n14485\n14486\n14487\n14488\n14489\n14490\n14491\n14492\n14493\n14494\n14495\n14496\n14497\n14498\n14499\n14500\n14501\n14502\n14503\n14504\n14505\n14506\n14507\n14508\n14509\n14510\n14511\n14512\n14513\n14514\n14515\n14516\n14517\n14518\n14519\n14520\n14521\n14522\n14523\n14524\n14525\n14526\n14527\n14528\n14529\n14530\n14531\n14532\n14533\n14534\n14535\n14536\n14537\n14538\n14539\n14540\n14541\n14542\n14543\n14544\n14545\n14546\n14547\n14548\n14549\n14550\n14551\n14552\n14553\n14554\n14555\n14556\n14557\n14558\n14559\n14560\n14561\n14562\n14563\n14564\n14565\n14566\n14567\n14568\n14569\n14570\n14571\n14572\n14573\n14574\n14575\n14576\n14577\n14578\n14579\n14580\n14581\n14582\n14583\n14584\n14585\n14586\n14587\n14588\n14589\n14590\n14591\n14592\n14593\n14594\n14595\n14596\n14597\n14598\n14599\n14600\n14601\n14602\n14603\n14604\n14605\n14606\n14607\n14608\n14609\n14610\n14611\n14612\n14613\n14614\n14615\n14616\n14617\n14618\n14619\n14620\n14621\n14622\n14623\n14624\n14625\n14626\n14627\n14628\n14629\n14630\n14631\n14632\n14633\n14634\n14635\n14636\n14637\n14638\n14639\n14640\n14641\n14642\n14643\n14644\n14645\n14646\n14647\n14648\n14649\n14650\n14651\n14652\n14653\n14654\n14655\n14656\n14657\n14658\n14659\n14660\n14661\n14662\n14663\n14664\n14665\n14666\n14667\n14668\n14669\n14670\n14671\n14672\n14673\n14674\n14675\n14676\n14677\n14678\n14679\n14680\n14681\n14682\n14683\n14684\n14685\n14686\n14687\n14688\n14689\n14690\n14691\n14692\n14693\n14694\n14695\n14696\n14697\n14698\n14699\n14700\n14701\n14702\n14703\n14704\n14705\n14706\n14707\n14708\n14709\n14710\n14711\n14712\n14713\n14714\n14715\n14716\n14717\n14718\n14719\n14720\n14721\n14722\n14723\n14724\n14725\n14726\n14727\n14728\n14729\n14730\n14731\n14732\n14733\n14734\n14735\n14736\n14737\n14738\n14739\n14740\n14741\n14742\n14743\n14744\n14745\n14746\n14747\n14748\n14749\n14750\n14751\n14752\n14753\n14754\n14755\n14756\n14757\n14758\n14759\n14760\n14761\n14762\n14763\n14764\n14765\n14766\n14767\n14768\n14769\n14770\n14771\n14772\n14773\n14774\n14775\n14776\n14777\n14778\n14779\n14780\n14781\n14782\n14783\n14784\n14785\n14786\n14787\n14788\n14789\n14790\n14791\n14792\n14793\n14794\n14795\n14796\n14797\n14798\n14799\n14800\n14801\n14802\n14803\n14804\n14805\n14806\n14807\n14808\n14809\n14810\n14811\n14812\n14813\n14814\n14815\n14816\n14817\n14818\n14819\n14820\n14821\n14822\n14823\n14824\n14825\n14826\n14827\n14828\n14829\n14830\n14831\n14832\n14833\n14834\n14835\n14836\n14837\n14838\n14839\n14840\n14841\n14842\n14843\n14844\n14845\n14846\n14847\n14848\n14849\n14850\n14851\n14852\n14853\n14854\n14855\n14856\n14857\n14858\n14859\n14860\n14861\n14862\n14863\n14864\n14865\n14866\n14867\n14868\n14869\n14870\n14871\n14872\n14873\n14874\n14875\n14876\n14877\n14878\n14879\n14880\n14881\n14882\n14883\n14884\n14885\n14886\n14887\n14888\n14889\n14890\n14891\n14892\n14893\n14894\n14895\n14896\n14897\n14898\n14899\n14900\n14901\n14902\n14903\n14904\n14905\n14906\n14907\n14908\n14909\n14910\n14911\n14912\n14913\n14914\n14915\n14916\n14917\n14918\n14919\n14920\n14921\n14922\n14923\n14924\n14925\n14926\n14927\n14928\n14929\n14930\n14931\n14932\n14933\n14934\n14935\n14936\n14937\n14938\n14939\n14940\n14941\n14942\n14943\n14944\n14945\n14946\n14947\n14948\n14949\n14950\n14951\n14952\n14953\n14954\n14955\n14956\n14957\n14958\n14959\n14960\n14961\n14962\n14963\n14964\n14965\n14966\n14967\n14968\n14969\n14970\n14971\n14972\n14973\n14974\n14975\n14976\n14977\n14978\n14979\n14980\n14981\n14982\n14983\n14984\n14985\n14986\n14987\n14988\n14989\n14990\n14991\n14992\n14993\n14994\n14995\n14996\n14997\n14998\n14999\n15000\n15001\n15002\n15003\n15004\n15005\n15006\n15007\n15008\n15009\n15010\n15011\n15012\n15013\n15014\n15015\n15016\n15017\n15018\n15019\n15020\n15021\n15022\n15023\n15024\n15025\n15026\n15027\n15028\n15029\n15030\n15031\n15032\n15033\n15034\n15035\n15036\n15037\n15038\n15039\n15040\n15041\n15042\n15043\n15044\n15045\n15046\n15047\n15048\n15049\n15050\n15051\n15052\n15053\n15054\n15055\n15056\n15057\n15058\n15059\n15060\n15061\n15062\n15063\n15064\n15065\n15066\n15067\n15068\n15069\n15070\n15071\n15072\n15073\n15074\n15075\n15076\n15077\n15078\n15079\n15080\n15081\n15082\n15083\n15084\n15085\n15086\n15087\n15088\n15089\n15090\n15091\n15092\n15093\n15094\n15095\n15096\n15097\n15098\n15099\n15100\n15101\n15102\n15103\n15104\n15105\n15106\n15107\n15108\n15109\n15110\n15111\n15112\n15113\n15114\n15115\n15116\n15117\n15118\n15119\n15120\n15121\n15122\n15123\n15124\n15125\n15126\n15127\n15128\n15129\n15130\n15131\n15132\n15133\n15134\n15135\n15136\n15137\n15138\n15139\n15140\n15141\n15142\n15143\n15144\n15145\n15146\n15147\n15148\n15149\n15150\n15151\n15152\n15153\n15154\n15155\n15156\n15157\n15158\n15159\n15160\n15161\n15162\n15163\n15164\n15165\n15166\n15167\n15168\n15169\n15170\n15171\n15172\n15173\n15174\n15175\n15176\n15177\n15178\n15179\n15180\n15181\n15182\n15183\n15184\n15185\n15186\n15187\n15188\n15189\n15190\n15191\n15192\n15193\n15194\n15195\n15196\n15197\n15198\n15199\n15200\n15201\n15202\n15203\n15204\n15205\n15206\n15207\n15208\n15209\n15210\n15211\n15212\n15213\n15214\n15215\n15216\n15217\n15218\n15219\n15220\n15221\n15222\n15223\n15224\n15225\n15226\n15227\n15228\n15229\n15230\n15231\n15232\n15233\n15234\n15235\n15236\n15237\n15238\n15239\n15240\n15241\n15242\n15243\n15244\n15245\n15246\n15247\n15248\n15249\n15250\n15251\n15252\n15253\n15254\n15255\n15256\n15257\n15258\n15259\n15260\n15261\n15262\n15263\n15264\n15265\n15266\n15267\n15268\n15269\n15270\n15271\n15272\n15273\n15274\n15275\n15276\n15277\n15278\n15279\n15280\n15281\n15282\n15283\n15284\n15285\n15286\n15287\n15288\n15289\n15290\n15291\n15292\n15293\n15294\n15295\n15296\n15297\n15298\n15299\n15300\n15301\n15302\n15303\n15304\n15305\n15306\n15307\n15308\n15309\n15310\n15311\n15312\n15313\n15314\n15315\n15316\n15317\n15318\n15319\n15320\n15321\n15322\n15323\n15324\n15325\n15326\n15327\n15328\n15329\n15330\n15331\n15332\n15333\n15334\n15335\n15336\n15337\n15338\n15339\n15340\n15341\n15342\n15343\n15344\n15345\n15346\n15347\n15348\n15349\n15350\n15351\n15352\n15353\n15354\n15355\n15356\n15357\n15358\n15359\n15360\n15361\n15362\n15363\n15364\n15365\n15366\n15367\n15368\n15369\n15370\n15371\n15372\n15373\n15374\n15375\n15376\n15377\n15378\n15379\n15380\n15381\n15382\n15383\n15384\n15385\n15386\n15387\n15388\n15389\n15390\n15391\n15392\n15393\n15394\n15395\n15396\n15397\n15398\n15399\n15400\n15401\n15402\n15403\n15404\n15405\n15406\n15407\n15408\n15409\n15410\n15411\n15412\n15413\n15414\n15415\n15416\n15417\n15418\n15419\n15420\n15421\n15422\n15423\n15424\n15425\n15426\n15427\n15428\n15429\n15430\n15431\n15432\n15433\n15434\n15435\n15436\n15437\n15438\n15439\n15440\n15441\n15442\n15443\n15444\n15445\n15446\n15447\n15448\n15449\n15450\n15451\n15452\n15453\n15454\n15455\n15456\n15457\n15458\n15459\n15460\n15461\n15462\n15463\n15464\n15465\n15466\n15467\n15468\n15469\n15470\n15471\n15472\n15473\n15474\n15475\n15476\n15477\n15478\n15479\n15480\n15481\n15482\n15483\n15484\n15485\n15486\n15487\n15488\n15489\n15490\n15491\n15492\n15493\n15494\n15495\n15496\n15497\n15498\n15499\n15500\n15501\n15502\n15503\n15504\n15505\n15506\n15507\n15508\n15509\n15510\n15511\n15512\n15513\n15514\n15515\n15516\n15517\n15518\n15519\n15520\n15521\n15522\n15523\n15524\n15525\n15526\n15527\n15528\n15529\n15530\n15531\n15532\n15533\n15534\n15535\n15536\n15537\n15538\n15539\n15540\n15541\n15542\n15543\n15544\n15545\n15546\n15547\n15548\n15549\n15550\n15551\n15552\n15553\n15554\n15555\n15556\n15557\n15558\n15559\n15560\n15561\n15562\n15563\n15564\n15565\n15566\n15567\n15568\n15569\n15570\n15571\n15572\n15573\n15574\n15575\n15576\n15577\n15578\n15579\n15580\n15581\n15582\n15583\n15584\n15585\n15586\n15587\n15588\n15589\n15590\n15591\n15592\n15593\n15594\n15595\n15596\n15597\n15598\n15599\n15600\n15601\n15602\n15603\n15604\n15605\n15606\n15607\n15608\n15609\n15610\n15611\n15612\n15613\n15614\n15615\n15616\n15617\n15618\n15619\n15620\n15621\n15622\n15623\n15624\n15625\n15626\n15627\n15628\n15629\n15630\n15631\n15632\n15633\n15634\n15635\n15636\n15637\n15638\n15639\n15640\n15641\n15642\n15643\n15644\n15645\n15646\n15647\n15648\n15649\n15650\n15651\n15652\n15653\n15654\n15655\n15656\n15657\n15658\n15659\n15660\n15661\n15662\n15663\n15664\n15665\n15666\n15667\n15668\n15669\n15670\n15671\n15672\n15673\n15674\n15675\n15676\n15677\n15678\n15679\n15680\n15681\n15682\n15683\n15684\n15685\n15686\n15687\n15688\n15689\n15690\n15691\n15692\n15693\n15694\n15695\n15696\n15697\n15698\n15699\n15700\n15701\n15702\n15703\n15704\n15705\n15706\n15707\n15708\n15709\n15710\n15711\n15712\n15713\n15714\n15715\n15716\n15717\n15718\n15719\n15720\n15721\n15722\n15723\n15724\n15725\n15726\n15727\n15728\n15729\n15730\n15731\n15732\n15733\n15734\n15735\n15736\n15737\n15738\n15739\n15740\n15741\n15742\n15743\n15744\n15745\n15746\n15747\n15748\n15749\n15750\n15751\n15752\n15753\n15754\n15755\n15756\n15757\n15758\n15759\n15760\n15761\n15762\n15763\n15764\n15765\n15766\n15767\n15768\n15769\n15770\n15771\n15772\n15773\n15774\n15775\n15776\n15777\n15778\n15779\n15780\n15781\n15782\n15783\n15784\n15785\n15786\n15787\n15788\n15789\n15790\n15791\n15792\n15793\n15794\n15795\n15796\n15797\n15798\n15799\n15800\n15801\n15802\n15803\n15804\n15805\n15806\n15807\n15808\n15809\n15810\n15811\n15812\n15813\n15814\n15815\n15816\n15817\n15818\n15819\n15820\n15821\n15822\n15823\n15824\n15825\n15826\n15827\n15828\n15829\n15830\n15831\n15832\n15833\n15834\n15835\n15836\n15837\n15838\n15839\n15840\n15841\n15842\n15843\n15844\n15845\n15846\n15847\n15848\n15849\n15850\n15851\n15852\n15853\n15854\n15855\n15856\n15857\n15858\n15859\n15860\n15861\n15862\n15863\n15864\n15865\n15866\n15867\n15868\n15869\n15870\n15871\n15872\n15873\n15874\n15875\n15876\n15877\n15878\n15879\n15880\n15881\n15882\n15883\n15884\n15885\n15886\n15887\n15888\n15889\n15890\n15891\n15892\n15893\n15894\n15895\n15896\n15897\n15898\n15899\n15900\n15901\n15902\n15903\n15904\n15905\n15906\n15907\n15908\n15909\n15910\n15911\n15912\n15913\n15914\n15915\n15916\n15917\n15918\n15919\n15920\n15921\n15922\n15923\n15924\n15925\n15926\n15927\n15928\n15929\n15930\n15931\n15932\n15933\n15934\n15935\n15936\n15937\n15938\n15939\n15940\n15941\n15942\n15943\n15944\n15945\n15946\n15947\n15948\n15949\n15950\n15951\n15952\n15953\n15954\n15955\n15956\n15957\n15958\n15959\n15960\n15961\n15962\n15963\n15964\n15965\n15966\n15967\n15968\n15969\n15970\n15971\n15972\n15973\n15974\n15975\n15976\n15977\n15978\n15979\n15980\n15981\n15982\n15983\n15984\n15985\n15986\n15987\n15988\n15989\n15990\n15991\n15992\n15993\n15994\n15995\n15996\n15997\n15998\n15999\n16000\n16001\n16002\n16003\n16004\n16005\n16006\n16007\n16008\n16009\n16010\n16011\n16012\n16013\n16014\n16015\n16016\n16017\n16018\n16019\n16020\n16021\n16022\n16023\n16024\n16025\n16026\n16027\n16028\n16029\n16030\n16031\n16032\n16033\n16034\n16035\n16036\n16037\n16038\n16039\n16040\n16041\n16042\n16043\n16044\n16045\n16046\n16047\n16048\n16049\n16050\n16051\n16052\n16053\n16054\n16055\n16056\n16057\n16058\n16059\n16060\n16061\n16062\n16063\n16064\n16065\n16066\n16067\n16068\n16069\n16070\n16071\n16072\n16073\n16074\n16075\n16076\n16077\n16078\n16079\n16080\n16081\n16082\n16083\n16084\n16085\n16086\n16087\n16088\n16089\n16090\n16091\n16092\n16093\n16094\n16095\n16096\n16097\n16098\n16099\n16100\n16101\n16102\n16103\n16104\n16105\n16106\n16107\n16108\n16109\n16110\n16111\n16112\n16113\n16114\n16115\n16116\n16117\n16118\n16119\n16120\n16121\n16122\n16123\n16124\n16125\n16126\n16127\n16128\n16129\n16130\n16131\n16132\n16133\n16134\n16135\n16136\n16137\n16138\n16139\n16140\n16141\n16142\n16143\n16144\n16145\n16146\n16147\n16148\n16149\n16150\n16151\n16152\n16153\n16154\n16155\n16156\n16157\n16158\n16159\n16160\n16161\n16162\n16163\n16164\n16165\n16166\n16167\n16168\n16169\n16170\n16171\n16172\n16173\n16174\n16175\n16176\n16177\n16178\n16179\n16180\n16181\n16182\n16183\n16184\n16185\n16186\n16187\n16188\n16189\n16190\n16191\n16192\n16193\n16194\n16195\n16196\n16197\n16198\n16199\n16200\n16201\n16202\n16203\n16204\n16205\n16206\n16207\n16208\n16209\n16210\n16211\n16212\n16213\n16214\n16215\n16216\n16217\n16218\n16219\n16220\n16221\n16222\n16223\n16224\n16225\n16226\n16227\n16228\n16229\n16230\n16231\n16232\n16233\n16234\n16235\n16236\n16237\n16238\n16239\n16240\n16241\n16242\n16243\n16244\n16245\n16246\n16247\n16248\n16249\n16250\n16251\n16252\n16253\n16254\n16255\n16256\n16257\n16258\n16259\n16260\n16261\n16262\n16263\n16264\n16265\n16266\n16267\n16268\n16269\n16270\n16271\n16272\n16273\n16274\n16275\n16276\n16277\n16278\n16279\n16280\n16281\n16282\n16283\n16284\n16285\n16286\n16287\n16288\n16289\n16290\n16291\n16292\n16293\n16294\n16295\n16296\n16297\n16298\n16299\n16300\n16301\n16302\n16303\n16304\n16305\n16306\n16307\n16308\n16309\n16310\n16311\n16312\n16313\n16314\n16315\n16316\n16317\n16318\n16319\n16320\n16321\n16322\n16323\n16324\n16325\n16326\n16327\n16328\n16329\n16330\n16331\n16332\n16333\n16334\n16335\n16336\n16337\n16338\n16339\n16340\n16341\n16342\n16343\n16344\n16345\n16346\n16347\n16348\n16349\n16350\n16351\n16352\n16353\n16354\n16355\n16356\n16357\n16358\n16359\n16360\n16361\n16362\n16363\n16364\n16365\n16366\n16367\n16368\n16369\n16370\n16371\n16372\n16373\n16374\n16375\n16376\n16377\n16378\n16379\n16380\n16381\n16382\n16383\n16384\n16385\n16386\n16387\n16388\n16389\n16390\n16391\n16392\n16393\n16394\n16395\n16396\n16397\n16398\n16399\n16400\n16401\n16402\n16403\n16404\n16405\n16406\n16407\n16408\n16409\n16410\n16411\n16412\n16413\n16414\n16415\n16416\n16417\n16418\n16419\n16420\n16421\n16422\n16423\n16424\n16425\n16426\n16427\n16428\n16429\n16430\n16431\n16432\n16433\n16434\n16435\n16436\n16437\n16438\n16439\n16440\n16441\n16442\n16443\n16444\n16445\n16446\n16447\n16448\n16449\n16450\n16451\n16452\n16453\n16454\n16455\n16456\n16457\n16458\n16459\n16460\n16461\n16462\n16463\n16464\n16465\n16466\n16467\n16468\n16469\n16470\n16471\n16472\n16473\n16474\n16475\n16476\n16477\n16478\n16479\n16480\n16481\n16482\n16483\n16484\n16485\n16486\n16487\n16488\n16489\n16490\n16491\n16492\n16493\n16494\n16495\n16496\n16497\n16498\n16499\n16500\n16501\n16502\n16503\n16504\n16505\n16506\n16507\n16508\n16509\n16510\n16511\n16512\n16513\n16514\n16515\n16516\n16517\n16518\n16519\n16520\n16521\n16522\n16523\n16524\n16525\n16526\n16527\n16528\n16529\n16530\n16531\n16532\n16533\n16534\n16535\n16536\n16537\n16538\n16539\n16540\n16541\n16542\n16543\n16544\n16545\n16546\n16547\n16548\n16549\n16550\n16551\n16552\n16553\n16554\n16555\n16556\n16557\n16558\n16559\n16560\n16561\n16562\n16563\n16564\n16565\n16566\n16567\n16568\n16569\n16570\n16571\n16572\n16573\n16574\n16575\n16576\n16577\n16578\n16579\n16580\n16581\n16582\n16583\n16584\n16585\n16586\n16587\n16588\n16589\n16590\n16591\n16592\n16593\n16594\n16595\n16596\n16597\n16598\n16599\n16600\n16601\n16602\n16603\n16604\n16605\n16606\n16607\n16608\n16609\n16610\n16611\n16612\n16613\n16614\n16615\n16616\n16617\n16618\n16619\n16620\n16621\n16622\n16623\n16624\n16625\n16626\n16627\n16628\n16629\n16630\n16631\n16632\n16633\n16634\n16635\n16636\n16637\n16638\n16639\n16640\n16641\n16642\n16643\n16644\n16645\n16646\n16647\n16648\n16649\n16650\n16651\n16652\n16653\n16654\n16655\n16656\n16657\n16658\n16659\n16660\n16661\n16662\n16663\n16664\n16665\n16666\n16667\n16668\n16669\n16670\n16671\n16672\n16673\n16674\n16675\n16676\n16677\n16678\n16679\n16680\n16681\n16682\n16683\n16684\n16685\n16686\n16687\n16688\n16689\n16690\n16691\n16692\n16693\n16694\n16695\n16696\n16697\n16698\n16699\n16700\n16701\n16702\n16703\n16704\n16705\n16706\n16707\n16708\n16709\n16710\n16711\n16712\n16713\n16714\n16715\n16716\n16717\n16718\n16719\n16720\n16721\n16722\n16723\n16724\n16725\n16726\n16727\n16728\n16729\n16730\n16731\n16732\n16733\n16734\n16735\n16736\n16737\n16738\n16739\n16740\n16741\n16742\n16743\n16744\n16745\n16746\n16747\n16748\n16749\n16750\n16751\n16752\n16753\n16754\n16755\n16756\n16757\n16758\n16759\n16760\n16761\n16762\n16763\n16764\n16765\n16766\n16767\n16768\n16769\n16770\n16771\n16772\n16773\n16774\n16775\n16776\n16777\n16778\n16779\n16780\n16781\n16782\n16783\n16784\n16785\n16786\n16787\n16788\n16789\n16790\n16791\n16792\n16793\n16794\n16795\n16796\n16797\n16798\n16799\n16800\n16801\n16802\n16803\n16804\n16805\n16806\n16807\n16808\n16809\n16810\n16811\n16812\n16813\n16814\n16815\n16816\n16817\n16818\n16819\n16820\n16821\n16822\n16823\n16824\n16825\n16826\n16827\n16828\n16829\n16830\n16831\n16832\n16833\n16834\n16835\n16836\n16837\n16838\n16839\n16840\n16841\n16842\n16843\n16844\n16845\n16846\n16847\n16848\n16849\n16850\n16851\n16852\n16853\n16854\n16855\n16856\n16857\n16858\n16859\n16860\n16861\n16862\n16863\n16864\n16865\n16866\n16867\n16868\n16869\n16870\n16871\n16872\n16873\n16874\n16875\n16876\n16877\n16878\n16879\n16880\n16881\n16882\n16883\n16884\n16885\n16886\n16887\n16888\n16889\n16890\n16891\n16892\n16893\n16894\n16895\n16896\n16897\n16898\n16899\n16900\n16901\n16902\n16903\n16904\n16905\n16906\n16907\n16908\n16909\n16910\n16911\n16912\n16913\n16914\n16915\n16916\n16917\n16918\n16919\n16920\n16921\n16922\n16923\n16924\n16925\n16926\n16927\n16928\n16929\n16930\n16931\n16932\n16933\n16934\n16935\n16936\n16937\n16938\n16939\n16940\n16941\n16942\n16943\n16944\n16945\n16946\n16947\n16948\n16949\n16950\n16951\n16952\n16953\n16954\n16955\n16956\n16957\n16958\n16959\n16960\n16961\n16962\n16963\n16964\n16965\n16966\n16967\n16968\n16969\n16970\n16971\n16972\n16973\n16974\n16975\n16976\n16977\n16978\n16979\n16980\n16981\n16982\n16983\n16984\n16985\n16986\n16987\n16988\n16989\n16990\n16991\n16992\n16993\n16994\n16995\n16996\n16997\n16998\n16999\n17000\n17001\n17002\n17003\n17004\n17005\n17006\n17007\n17008\n17009\n17010\n17011\n17012\n17013\n17014\n17015\n17016\n17017\n17018\n17019\n17020\n17021\n17022\n17023\n17024\n17025\n17026\n17027\n17028\n17029\n17030\n17031\n17032\n17033\n17034\n17035\n17036\n17037\n17038\n17039\n17040\n17041\n17042\n17043\n17044\n17045\n17046\n17047\n17048\n17049\n17050\n17051\n17052\n17053\n17054\n17055\n17056\n17057\n17058\n17059\n17060\n17061\n17062\n17063\n17064\n17065\n17066\n17067\n17068\n17069\n17070\n17071\n17072\n17073\n17074\n17075\n17076\n17077\n17078\n17079\n17080\n17081\n17082\n17083\n17084\n17085\n17086\n17087\n17088\n17089\n17090\n17091\n17092\n17093\n17094\n17095\n17096\n17097\n17098\n17099\n17100\n17101\n17102\n17103\n17104\n17105\n17106\n17107\n17108\n17109\n17110\n17111\n17112\n17113\n17114\n17115\n17116\n17117\n17118\n17119\n17120\n17121\n17122\n17123\n17124\n17125\n17126\n17127\n17128\n17129\n17130\n17131\n17132\n17133\n17134\n17135\n17136\n17137\n17138\n17139\n17140\n17141\n17142\n17143\n17144\n17145\n17146\n17147\n17148\n17149\n17150\n17151\n17152\n17153\n17154\n17155\n17156\n17157\n17158\n17159\n17160\n17161\n17162\n17163\n17164\n17165\n17166\n17167\n17168\n17169\n17170\n17171\n17172\n17173\n17174\n17175\n17176\n17177\n17178\n17179\n17180\n17181\n17182\n17183\n17184\n17185\n17186\n17187\n17188\n17189\n17190\n17191\n17192\n17193\n17194\n17195\n17196\n17197\n17198\n17199\n17200\n17201\n17202\n17203\n17204\n17205\n17206\n17207\n17208\n17209\n17210\n17211\n17212\n17213\n17214\n17215\n17216\n17217\n17218\n17219\n17220\n17221\n17222\n17223\n17224\n17225\n17226\n17227\n17228\n17229\n17230\n17231\n17232\n17233\n17234\n17235\n17236\n17237\n17238\n17239\n17240\n17241\n17242\n17243\n17244\n17245\n17246\n17247\n17248\n17249\n17250\n17251\n17252\n17253\n17254\n17255\n17256\n17257\n17258\n17259\n17260\n17261\n17262\n17263\n17264\n17265\n17266\n17267\n17268\n17269\n17270\n17271\n17272\n17273\n17274\n17275\n17276\n17277\n17278\n17279\n17280\n17281\n17282\n17283\n17284\n17285\n17286\n17287\n17288\n17289\n17290\n17291\n17292\n17293\n17294\n17295\n17296\n17297\n17298\n17299\n17300\n17301\n17302\n17303\n17304\n17305\n17306\n17307\n17308\n17309\n17310\n17311\n17312\n17313\n17314\n17315\n17316\n17317\n17318\n17319\n17320\n17321\n17322\n17323\n17324\n17325\n17326\n17327\n17328\n17329\n17330\n17331\n17332\n17333\n17334\n17335\n17336\n17337\n17338\n17339\n17340\n17341\n17342\n17343\n17344\n17345\n17346\n17347\n17348\n17349\n17350\n17351\n17352\n17353\n17354\n17355\n17356\n17357\n17358\n17359\n17360\n17361\n17362\n17363\n17364\n17365\n17366\n17367\n17368\n17369\n17370\n17371\n17372\n17373\n17374\n17375\n17376\n17377\n17378\n17379\n17380\n17381\n17382\n17383\n17384\n17385\n17386\n17387\n17388\n17389\n17390\n17391\n17392\n17393\n17394\n17395\n17396\n17397\n17398\n17399\n17400\n17401\n17402\n17403\n17404\n17405\n17406\n17407\n17408\n17409\n17410\n17411\n17412\n17413\n17414\n17415\n17416\n17417\n17418\n17419\n17420\n17421\n17422\n17423\n17424\n17425\n17426\n17427\n17428\n17429\n17430\n17431\n17432\n17433\n17434\n17435\n17436\n17437\n17438\n17439\n17440\n17441\n17442\n17443\n17444\n17445\n17446\n17447\n17448\n17449\n17450\n17451\n17452\n17453\n17454\n17455\n17456\n17457\n17458\n17459\n17460\n17461\n17462\n17463\n17464\n17465\n17466\n17467\n17468\n17469\n17470\n17471\n17472\n17473\n17474\n17475\n17476\n17477\n17478\n17479\n17480\n17481\n17482\n17483\n17484\n17485\n17486\n17487\n17488\n17489\n17490\n17491\n17492\n17493\n17494\n17495\n17496\n17497\n17498\n17499\n17500\n17501\n17502\n17503\n17504\n17505\n17506\n17507\n17508\n17509\n17510\n17511\n17512\n17513\n17514\n17515\n17516\n17517\n17518\n17519\n17520\n17521\n17522\n17523\n17524\n17525\n17526\n17527\n17528\n17529\n17530\n17531\n17532\n17533\n17534\n17535\n17536\n17537\n17538\n17539\n17540\n17541\n17542\n17543\n17544\n17545\n17546\n17547\n17548\n17549\n17550\n17551\n17552\n17553\n17554\n17555\n17556\n17557\n17558\n17559\n17560\n17561\n17562\n17563\n17564\n17565\n17566\n17567\n17568\n17569\n17570\n17571\n17572\n17573\n17574\n17575\n17576\n17577\n17578\n17579\n17580\n17581\n17582\n17583\n17584\n17585\n17586\n17587\n17588\n17589\n17590\n17591\n17592\n17593\n17594\n17595\n17596\n17597\n17598\n17599\n17600\n17601\n17602\n17603\n17604\n17605\n17606\n17607\n17608\n17609\n17610\n17611\n17612\n17613\n17614\n17615\n17616\n17617\n17618\n17619\n17620\n17621\n17622\n17623\n17624\n17625\n17626\n17627\n17628\n17629\n17630\n17631\n17632\n17633\n17634\n17635\n17636\n17637\n17638\n17639\n17640\n17641\n17642\n17643\n17644\n17645\n17646\n17647\n17648\n17649\n17650\n17651\n17652\n17653\n17654\n17655\n17656\n17657\n17658\n17659\n17660\n17661\n17662\n17663\n17664\n17665\n17666\n17667\n17668\n17669\n17670\n17671\n17672\n17673\n17674\n17675\n17676\n17677\n17678\n17679\n17680\n17681\n17682\n17683\n17684\n17685\n17686\n17687\n17688\n17689\n17690\n17691\n17692\n17693\n17694\n17695\n17696\n17697\n17698\n17699\n17700\n17701\n17702\n17703\n17704\n17705\n17706\n17707\n17708\n17709\n17710\n17711\n17712\n17713\n17714\n17715\n17716\n17717\n17718\n17719\n17720\n17721\n17722\n17723\n17724\n17725\n17726\n17727\n17728\n17729\n17730\n17731\n17732\n17733\n17734\n17735\n17736\n17737\n17738\n17739\n17740\n17741\n17742\n17743\n17744\n17745\n17746\n17747\n17748\n17749\n17750\n17751\n17752\n17753\n17754\n17755\n17756\n17757\n17758\n17759\n17760\n17761\n17762\n17763\n17764\n17765\n17766\n17767\n17768\n17769\n17770\n17771\n17772\n17773\n17774\n17775\n17776\n17777\n17778\n17779\n17780\n17781\n17782\n17783\n17784\n17785\n17786\n17787\n17788\n17789\n17790\n17791\n17792\n17793\n17794\n17795\n17796\n17797\n17798\n17799\n17800\n17801\n17802\n17803\n17804\n17805\n17806\n17807\n17808\n17809\n17810\n17811\n17812\n17813\n17814\n17815\n17816\n17817\n17818\n17819\n17820\n17821\n17822\n17823\n17824\n17825\n17826\n17827\n17828\n17829\n17830\n17831\n17832\n17833\n17834\n17835\n17836\n17837\n17838\n17839\n17840\n17841\n17842\n17843\n17844\n17845\n17846\n17847\n17848\n17849\n17850\n17851\n17852\n17853\n17854\n17855\n17856\n17857\n17858\n17859\n17860\n17861\n17862\n17863\n17864\n17865\n17866\n17867\n17868\n17869\n17870\n17871\n17872\n17873\n17874\n17875\n17876\n17877\n17878\n17879\n17880\n17881\n17882\n17883\n17884\n17885\n17886\n17887\n17888\n17889\n17890\n17891\n17892\n17893\n17894\n17895\n17896\n17897\n17898\n17899\n17900\n17901\n17902\n17903\n17904\n17905\n17906\n17907\n17908\n17909\n17910\n17911\n17912\n17913\n17914\n17915\n17916\n17917\n17918\n17919\n17920\n17921\n17922\n17923\n17924\n17925\n17926\n17927\n17928\n17929\n17930\n17931\n17932\n17933\n17934\n17935\n17936\n17937\n17938\n17939\n17940\n17941\n17942\n17943\n17944\n17945\n17946\n17947\n17948\n17949\n17950\n17951\n17952\n17953\n17954\n17955\n17956\n17957\n17958\n17959\n17960\n17961\n17962\n17963\n17964\n17965\n17966\n17967\n17968\n17969\n17970\n17971\n17972\n17973\n17974\n17975\n17976\n17977\n17978\n17979\n17980\n17981\n17982\n17983\n17984\n17985\n17986\n17987\n17988\n17989\n17990\n17991\n17992\n17993\n17994\n17995\n17996\n17997\n17998\n17999\n18000\n18001\n18002\n18003\n18004\n18005\n18006\n18007\n18008\n18009\n18010\n18011\n18012\n18013\n18014\n18015\n18016\n18017\n18018\n18019\n18020\n18021\n18022\n18023\n18024\n18025\n18026\n18027\n18028\n18029\n18030\n18031\n18032\n18033\n18034\n18035\n18036\n18037\n18038\n18039\n18040\n18041\n18042\n18043\n18044\n18045\n18046\n18047\n18048\n18049\n18050\n18051\n18052\n18053\n18054\n18055\n18056\n18057\n18058\n18059\n18060\n18061\n18062\n18063\n18064\n18065\n18066\n18067\n18068\n18069\n18070\n18071\n18072\n18073\n18074\n18075\n18076\n18077\n18078\n18079\n18080\n18081\n18082\n18083\n18084\n18085\n18086\n18087\n18088\n18089\n18090\n18091\n18092\n18093\n18094\n18095\n18096\n18097\n18098\n18099\n18100\n18101\n18102\n18103\n18104\n18105\n18106\n18107\n18108\n18109\n18110\n18111\n18112\n18113\n18114\n18115\n18116\n18117\n18118\n18119\n18120\n18121\n18122\n18123\n18124\n18125\n18126\n18127\n18128\n18129\n18130\n18131\n18132\n18133\n18134\n18135\n18136\n18137\n18138\n18139\n18140\n18141\n18142\n18143\n18144\n18145\n18146\n18147\n18148\n18149\n18150\n18151\n18152\n18153\n18154\n18155\n18156\n18157\n18158\n18159\n18160\n18161\n18162\n18163\n18164\n18165\n18166\n18167\n18168\n18169\n18170\n18171\n18172\n18173\n18174\n18175\n18176\n18177\n18178\n18179\n18180\n18181\n18182\n18183\n18184\n18185\n18186\n18187\n18188\n18189\n18190\n18191\n18192\n18193\n18194\n18195\n18196\n18197\n18198\n18199\n18200\n18201\n18202\n18203\n18204\n18205\n18206\n18207\n18208\n18209\n18210\n18211\n18212\n18213\n18214\n18215\n18216\n18217\n18218\n18219\n18220\n18221\n18222\n18223\n18224\n18225\n18226\n18227\n18228\n18229\n18230\n18231\n18232\n18233\n18234\n18235\n18236\n18237\n18238\n18239\n18240\n18241\n18242\n18243\n18244\n18245\n18246\n18247\n18248\n18249\n18250\n18251\n18252\n18253\n18254\n18255\n18256\n18257\n18258\n18259\n18260\n18261\n18262\n18263\n18264\n18265\n18266\n18267\n18268\n18269\n18270\n18271\n18272\n18273\n18274\n18275\n18276\n18277\n18278\n18279\n18280\n18281\n18282\n18283\n18284\n18285\n18286\n18287\n18288\n18289\n18290\n18291\n18292\n18293\n18294\n18295\n18296\n18297\n18298\n18299\n18300\n18301\n18302\n18303\n18304\n18305\n18306\n18307\n18308\n18309\n18310\n18311\n18312\n18313\n18314\n18315\n18316\n18317\n18318\n18319\n18320\n18321\n18322\n18323\n18324\n18325\n18326\n18327\n18328\n18329\n18330\n18331\n18332\n18333\n18334\n18335\n18336\n18337\n18338\n18339\n18340\n18341\n18342\n18343\n18344\n18345\n18346\n18347\n18348\n18349\n18350\n18351\n18352\n18353\n18354\n18355\n18356\n18357\n18358\n18359\n18360\n18361\n18362\n18363\n18364\n18365\n18366\n18367\n18368\n18369\n18370\n18371\n18372\n18373\n18374\n18375\n18376\n18377\n18378\n18379\n18380\n18381\n18382\n18383\n18384\n18385\n18386\n18387\n18388\n18389\n18390\n18391\n18392\n18393\n18394\n18395\n18396\n18397\n18398\n18399\n18400\n18401\n18402\n18403\n18404\n18405\n18406\n18407\n18408\n18409\n18410\n18411\n18412\n18413\n18414\n18415\n18416\n18417\n18418\n18419\n18420\n18421\n18422\n18423\n18424\n18425\n18426\n18427\n18428\n18429\n18430\n18431\n18432\n18433\n18434\n18435\n18436\n18437\n18438\n18439\n18440\n18441\n18442\n18443\n18444\n18445\n18446\n18447\n18448\n18449\n18450\n18451\n18452\n18453\n18454\n18455\n18456\n18457\n18458\n18459\n18460\n18461\n18462\n18463\n18464\n18465\n18466\n18467\n18468\n18469\n18470\n18471\n18472\n18473\n18474\n18475\n18476\n18477\n18478\n18479\n18480\n18481\n18482\n18483\n18484\n18485\n18486\n18487\n18488\n18489\n18490\n18491\n18492\n18493\n18494\n18495\n18496\n18497\n18498\n18499\n18500\n18501\n18502\n18503\n18504\n18505\n18506\n18507\n18508\n18509\n18510\n18511\n18512\n18513\n18514\n18515\n18516\n18517\n18518\n18519\n18520\n18521\n18522\n18523\n18524\n18525\n18526\n18527\n18528\n18529\n18530\n18531\n18532\n18533\n18534\n18535\n18536\n18537\n18538\n18539\n18540\n18541\n18542\n18543\n18544\n18545\n18546\n18547\n18548\n18549\n18550\n18551\n18552\n18553\n18554\n18555\n18556\n18557\n18558\n18559\n18560\n18561\n18562\n18563\n18564\n18565\n18566\n18567\n18568\n18569\n18570\n18571\n18572\n18573\n18574\n18575\n18576\n18577\n18578\n18579\n18580\n18581\n18582\n18583\n18584\n18585\n18586\n18587\n18588\n18589\n18590\n18591\n18592\n18593\n18594\n18595\n18596\n18597\n18598\n18599\n18600\n18601\n18602\n18603\n18604\n18605\n18606\n18607\n18608\n18609\n18610\n18611\n18612\n18613\n18614\n18615\n18616\n18617\n18618\n18619\n18620\n18621\n18622\n18623\n18624\n18625\n18626\n18627\n18628\n18629\n18630\n18631\n18632\n18633\n18634\n18635\n18636\n18637\n18638\n18639\n18640\n18641\n18642\n18643\n18644\n18645\n18646\n18647\n18648\n18649\n18650\n18651\n18652\n18653\n18654\n18655\n18656\n18657\n18658\n18659\n18660\n18661\n18662\n18663\n18664\n18665\n18666\n18667\n18668\n18669\n18670\n18671\n18672\n18673\n18674\n18675\n18676\n18677\n18678\n18679\n18680\n18681\n18682\n18683\n18684\n18685\n18686\n18687\n18688\n18689\n18690\n18691\n18692\n18693\n18694\n18695\n18696\n18697\n18698\n18699\n18700\n18701\n18702\n18703\n18704\n18705\n18706\n18707\n18708\n18709\n18710\n18711\n18712\n18713\n18714\n18715\n18716\n18717\n18718\n18719\n18720\n18721\n18722\n18723\n18724\n18725\n18726\n18727\n18728\n18729\n18730\n18731\n18732\n18733\n18734\n18735\n18736\n18737\n18738\n18739\n18740\n18741\n18742\n18743\n18744\n18745\n18746\n18747\n18748\n18749\n18750\n18751\n18752\n18753\n18754\n18755\n18756\n18757\n18758\n18759\n18760\n18761\n18762\n18763\n18764\n18765\n18766\n18767\n18768\n18769\n18770\n18771\n18772\n18773\n18774\n18775\n18776\n18777\n18778\n18779\n18780\n18781\n18782\n18783\n18784\n18785\n18786\n18787\n18788\n18789\n18790\n18791\n18792\n18793\n18794\n18795\n18796\n18797\n18798\n18799\n18800\n18801\n18802\n18803\n18804\n18805\n18806\n18807\n18808\n18809\n18810\n18811\n18812\n18813\n18814\n18815\n18816\n18817\n18818\n18819\n18820\n18821\n18822\n18823\n18824\n18825\n18826\n18827\n18828\n18829\n18830\n18831\n18832\n18833\n18834\n18835\n18836\n18837\n18838\n18839\n18840\n18841\n18842\n18843\n18844\n18845\n18846\n18847\n18848\n18849\n18850\n18851\n18852\n18853\n18854\n18855\n18856\n18857\n18858\n18859\n18860\n18861\n18862\n18863\n18864\n18865\n18866\n18867\n18868\n18869\n18870\n18871\n18872\n18873\n18874\n18875\n18876\n18877\n18878\n18879\n18880\n18881\n18882\n18883\n18884\n18885\n18886\n18887\n18888\n18889\n18890\n18891\n18892\n18893\n18894\n18895\n18896\n18897\n18898\n18899\n18900\n18901\n18902\n18903\n18904\n18905\n18906\n18907\n18908\n18909\n18910\n18911\n18912\n18913\n18914\n18915\n18916\n18917\n18918\n18919\n18920\n18921\n18922\n18923\n18924\n18925\n18926\n18927\n18928\n18929\n18930\n18931\n18932\n18933\n18934\n18935\n18936\n18937\n18938\n18939\n18940\n18941\n18942\n18943\n18944\n18945\n18946\n18947\n18948\n18949\n18950\n18951\n18952\n18953\n18954\n18955\n18956\n18957\n18958\n18959\n18960\n18961\n18962\n18963\n18964\n18965\n18966\n18967\n18968\n18969\n18970\n18971\n18972\n18973\n18974\n18975\n18976\n18977\n18978\n18979\n18980\n18981\n18982\n18983\n18984\n18985\n18986\n18987\n18988\n18989\n18990\n18991\n18992\n18993\n18994\n18995\n18996\n18997\n18998\n18999\n19000\n19001\n19002\n19003\n19004\n19005\n19006\n19007\n19008\n19009\n19010\n19011\n19012\n19013\n19014\n19015\n19016\n19017\n19018\n19019\n19020\n19021\n19022\n19023\n19024\n19025\n19026\n19027\n19028\n19029\n19030\n19031\n19032\n19033\n19034\n19035\n19036\n19037\n19038\n19039\n19040\n19041\n19042\n19043\n19044\n19045\n19046\n19047\n19048\n19049\n19050\n19051\n19052\n19053\n19054\n19055\n19056\n19057\n19058\n19059\n19060\n19061\n19062\n19063\n19064\n19065\n19066\n19067\n19068\n19069\n19070\n19071\n19072\n19073\n19074\n19075\n19076\n19077\n19078\n19079\n19080\n19081\n19082\n19083\n19084\n19085\n19086\n19087\n19088\n19089\n19090\n19091\n19092\n19093\n19094\n19095\n19096\n19097\n19098\n19099\n19100\n19101\n19102\n19103\n19104\n19105\n19106\n19107\n19108\n19109\n19110\n19111\n19112\n19113\n19114\n19115\n19116\n19117\n19118\n19119\n19120\n19121\n19122\n19123\n19124\n19125\n19126\n19127\n19128\n19129\n19130\n19131\n19132\n19133\n19134\n19135\n19136\n19137\n19138\n19139\n19140\n19141\n19142\n19143\n19144\n19145\n19146\n19147\n19148\n19149\n19150\n19151\n19152\n19153\n19154\n19155\n19156\n19157\n19158\n19159\n19160\n19161\n19162\n19163\n19164\n19165\n19166\n19167\n19168\n19169\n19170\n19171\n19172\n19173\n19174\n19175\n19176\n19177\n19178\n19179\n19180\n19181\n19182\n19183\n19184\n19185\n19186\n19187\n19188\n19189\n19190\n19191\n19192\n19193\n19194\n19195\n19196\n19197\n19198\n19199\n19200\n19201\n19202\n19203\n19204\n19205\n19206\n19207\n19208\n19209\n19210\n19211\n19212\n19213\n19214\n19215\n19216\n19217\n19218\n19219\n19220\n19221\n19222\n19223\n19224\n19225\n19226\n19227\n19228\n19229\n19230\n19231\n19232\n19233\n19234\n19235\n19236\n19237\n19238\n19239\n19240\n19241\n19242\n19243\n19244\n19245\n19246\n19247\n19248\n19249\n19250\n19251\n19252\n19253\n19254\n19255\n19256\n19257\n19258\n19259\n19260\n19261\n19262\n19263\n19264\n19265\n19266\n19267\n19268\n19269\n19270\n19271\n19272\n19273\n19274\n19275\n19276\n19277\n19278\n19279\n19280\n19281\n19282\n19283\n19284\n19285\n19286\n19287\n19288\n19289\n19290\n19291\n19292\n19293\n19294\n19295\n19296\n19297\n19298\n19299\n19300\n19301\n19302\n19303\n19304\n19305\n19306\n19307\n19308\n19309\n19310\n19311\n19312\n19313\n19314\n19315\n19316\n19317\n19318\n19319\n19320\n19321\n19322\n19323\n19324\n19325\n19326\n19327\n19328\n19329\n19330\n19331\n19332\n19333\n19334\n19335\n19336\n19337\n19338\n19339\n19340\n19341\n19342\n19343\n19344\n19345\n19346\n19347\n19348\n19349\n19350\n19351\n19352\n19353\n19354\n19355\n19356\n19357\n19358\n19359\n19360\n19361\n19362\n19363\n19364\n19365\n19366\n19367\n19368\n19369\n19370\n19371\n19372\n19373\n19374\n19375\n19376\n19377\n19378\n19379\n19380\n19381\n19382\n19383\n19384\n19385\n19386\n19387\n19388\n19389\n19390\n19391\n19392\n19393\n19394\n19395\n19396\n19397\n19398\n19399\n19400\n19401\n19402\n19403\n19404\n19405\n19406\n19407\n19408\n19409\n19410\n19411\n19412\n19413\n19414\n19415\n19416\n19417\n19418\n19419\n19420\n19421\n19422\n19423\n19424\n19425\n19426\n19427\n19428\n19429\n19430\n19431\n19432\n19433\n19434\n19435\n19436\n19437\n19438\n19439\n19440\n19441\n19442\n19443\n19444\n19445\n19446\n19447\n19448\n19449\n19450\n19451\n19452\n19453\n19454\n19455\n19456\n19457\n19458\n19459\n19460\n19461\n19462\n19463\n19464\n19465\n19466\n19467\n19468\n19469\n19470\n19471\n19472\n19473\n19474\n19475\n19476\n19477\n19478\n19479\n19480\n19481\n19482\n19483\n19484\n19485\n19486\n19487\n19488\n19489\n19490\n19491\n19492\n19493\n19494\n19495\n19496\n19497\n19498\n19499\n19500\n19501\n19502\n19503\n19504\n19505\n19506\n19507\n19508\n19509\n19510\n19511\n19512\n19513\n19514\n19515\n19516\n19517\n19518\n19519\n19520\n19521\n19522\n19523\n19524\n19525\n19526\n19527\n19528\n19529\n19530\n19531\n19532\n19533\n19534\n19535\n19536\n19537\n19538\n19539\n19540\n19541\n19542\n19543\n19544\n19545\n19546\n19547\n19548\n19549\n19550\n19551\n19552\n19553\n19554\n19555\n19556\n19557\n19558\n19559\n19560\n19561\n19562\n19563\n19564\n19565\n19566\n19567\n19568\n19569\n19570\n19571\n19572\n19573\n19574\n19575\n19576\n19577\n19578\n19579\n19580\n19581\n19582\n19583\n19584\n19585\n19586\n19587\n19588\n19589\n19590\n19591\n19592\n19593\n19594\n19595\n19596\n19597\n19598\n19599\n19600\n19601\n19602\n19603\n19604\n19605\n19606\n19607\n19608\n19609\n19610\n19611\n19612\n19613\n19614\n19615\n19616\n19617\n19618\n19619\n19620\n19621\n19622\n19623\n19624\n19625\n19626\n19627\n19628\n19629\n19630\n19631\n19632\n19633\n19634\n19635\n19636\n19637\n19638\n19639\n19640\n19641\n19642\n19643\n19644\n19645\n19646\n19647\n19648\n19649\n19650\n19651\n19652\n19653\n19654\n19655\n19656\n19657\n19658\n19659\n19660\n19661\n19662\n19663\n19664\n19665\n19666\n19667\n19668\n19669\n19670\n19671\n19672\n19673\n19674\n19675\n19676\n19677\n19678\n19679\n19680\n19681\n19682\n19683\n19684\n19685\n19686\n19687\n19688\n19689\n19690\n19691\n19692\n19693\n19694\n19695\n19696\n19697\n19698\n19699\n19700\n19701\n19702\n19703\n19704\n19705\n19706\n19707\n19708\n19709\n19710\n19711\n19712\n19713\n19714\n19715\n19716\n19717\n19718\n19719\n19720\n19721\n19722\n19723\n19724\n19725\n19726\n19727\n19728\n19729\n19730\n19731\n19732\n19733\n19734\n19735\n19736\n19737\n19738\n19739\n19740\n19741\n19742\n19743\n19744\n19745\n19746\n19747\n19748\n19749\n19750\n19751\n19752\n19753\n19754\n19755\n19756\n19757\n19758\n19759\n19760\n19761\n19762\n19763\n19764\n19765\n19766\n19767\n19768\n19769\n19770\n19771\n19772\n19773\n19774\n19775\n19776\n19777\n19778\n19779\n19780\n19781\n19782\n19783\n19784\n19785\n19786\n19787\n19788\n19789\n19790\n19791\n19792\n19793\n19794\n19795\n19796\n19797\n19798\n19799\n19800\n19801\n19802\n19803\n19804\n19805\n19806\n19807\n19808\n19809\n19810\n19811\n19812\n19813\n19814\n19815\n19816\n19817\n19818\n19819\n19820\n19821\n19822\n19823\n19824\n19825\n19826\n19827\n19828\n19829\n19830\n19831\n19832\n19833\n19834\n19835\n19836\n19837\n19838\n19839\n19840\n19841\n19842\n19843\n19844\n19845\n19846\n19847\n19848\n19849\n19850\n19851\n19852\n19853\n19854\n19855\n19856\n19857\n19858\n19859\n19860\n19861\n19862\n19863\n19864\n19865\n19866\n19867\n19868\n19869\n19870\n19871\n19872\n19873\n19874\n19875\n19876\n19877\n19878\n19879\n19880\n19881\n19882\n19883\n19884\n19885\n19886\n19887\n19888\n19889\n19890\n19891\n19892\n19893\n19894\n19895\n19896\n19897\n19898\n19899\n19900\n19901\n19902\n19903\n19904\n19905\n19906\n19907\n19908\n19909\n19910\n19911\n19912\n19913\n19914\n19915\n19916\n19917\n19918\n19919\n19920\n19921\n19922\n19923\n19924\n19925\n19926\n19927\n19928\n19929\n19930\n19931\n19932\n19933\n19934\n19935\n19936\n19937\n19938\n19939\n19940\n19941\n19942\n19943\n19944\n19945\n19946\n19947\n19948\n19949\n19950\n19951\n19952\n19953\n19954\n19955\n19956\n19957\n19958\n19959\n19960\n19961\n19962\n19963\n19964\n19965\n19966\n19967\n19968\n19969\n19970\n19971\n19972\n19973\n19974\n19975\n19976\n19977\n19978\n19979\n19980\n19981\n19982\n19983\n19984\n19985\n19986\n19987\n19988\n19989\n19990\n19991\n19992\n19993\n19994\n19995\n19996\n19997\n19998\n19999\n20000\n20001\n20002\n20003\n20004\n20005\n20006\n20007\n20008\n20009\n20010\n20011\n20012\n20013\n20014\n20015\n20016\n20017\n20018\n20019\n20020\n20021\n20022\n20023\n20024\n20025\n20026\n20027\n20028\n20029\n20030\n20031\n20032\n20033\n20034\n20035\n20036\n20037\n20038\n20039\n20040\n20041\n20042\n20043\n20044\n20045\n20046\n20047\n20048\n20049\n20050\n20051\n20052\n20053\n20054\n20055\n20056\n20057\n20058\n20059\n20060\n20061\n20062\n20063\n20064\n20065\n20066\n20067\n20068\n20069\n20070\n20071\n20072\n20073\n20074\n20075\n20076\n20077\n20078\n20079\n20080\n20081\n20082\n20083\n20084\n20085\n20086\n20087\n20088\n20089\n20090\n20091\n20092\n20093\n20094\n20095\n20096\n20097\n20098\n20099\n20100\n20101\n20102\n20103\n20104\n20105\n20106\n20107\n20108\n20109\n20110\n20111\n20112\n20113\n20114\n20115\n20116\n20117\n20118\n20119\n20120\n20121\n20122\n20123\n20124\n20125\n20126\n20127\n20128\n20129\n20130\n20131\n20132\n20133\n20134\n20135\n20136\n20137\n20138\n20139\n20140\n20141\n20142\n20143\n20144\n20145\n20146\n20147\n20148\n20149\n20150\n20151\n20152\n20153\n20154\n20155\n20156\n20157\n20158\n20159\n20160\n20161\n20162\n20163\n20164\n20165\n20166\n20167\n20168\n20169\n20170\n20171\n20172\n20173\n20174\n20175\n20176\n20177\n20178\n20179\n20180\n20181\n20182\n20183\n20184\n20185\n20186\n20187\n20188\n20189\n20190\n20191\n20192\n20193\n20194\n20195\n20196\n20197\n20198\n20199\n20200\n20201\n20202\n20203\n20204\n20205\n20206\n20207\n20208\n20209\n20210\n20211\n20212\n20213\n20214\n20215\n20216\n20217\n20218\n20219\n20220\n20221\n20222\n20223\n20224\n20225\n20226\n20227\n20228\n20229\n20230\n20231\n20232\n20233\n20234\n20235\n20236\n20237\n20238\n20239\n20240\n20241\n20242\n20243\n20244\n20245\n20246\n20247\n20248\n20249\n20250\n20251\n20252\n20253\n20254\n20255\n20256\n20257\n20258\n20259\n20260\n20261\n20262\n20263\n20264\n20265\n20266\n20267\n20268\n20269\n20270\n20271\n20272\n20273\n20274\n20275\n20276\n20277\n20278\n20279\n20280\n20281\n20282\n20283\n20284\n20285\n20286\n20287\n20288\n20289\n20290\n20291\n20292\n20293\n20294\n20295\n20296\n20297\n20298\n20299\n20300\n20301\n20302\n20303\n20304\n20305\n20306\n20307\n20308\n20309\n20310\n20311\n20312\n20313\n20314\n20315\n20316\n20317\n20318\n20319\n20320\n20321\n20322\n20323\n20324\n20325\n20326\n20327\n20328\n20329\n20330\n20331\n20332\n20333\n20334\n20335\n20336\n20337\n20338\n20339\n20340\n20341\n20342\n20343\n20344\n20345\n20346\n20347\n20348\n20349\n20350\n20351\n20352\n20353\n20354\n20355\n20356\n20357\n20358\n20359\n20360\n20361\n20362\n20363\n20364\n20365\n20366\n20367\n20368\n20369\n20370\n20371\n20372\n20373\n20374\n20375\n20376\n20377\n20378\n20379\n20380\n20381\n20382\n20383\n20384\n20385\n20386\n20387\n20388\n20389\n20390\n20391\n20392\n20393\n20394\n20395\n20396\n20397\n20398\n20399\n20400\n20401\n20402\n20403\n20404\n20405\n20406\n20407\n20408\n20409\n20410\n20411\n20412\n20413\n20414\n20415\n20416\n20417\n20418\n20419\n20420\n20421\n20422\n20423\n20424\n20425\n20426\n20427\n20428\n20429\n20430\n20431\n20432\n20433\n20434\n20435\n20436\n20437\n20438\n20439\n20440\n20441\n20442\n20443\n20444\n20445\n20446\n20447\n20448\n20449\n20450\n20451\n20452\n20453\n20454\n20455\n20456\n20457\n20458\n20459\n20460\n20461\n20462\n20463\n20464\n20465\n20466\n20467\n20468\n20469\n20470\n20471\n20472\n20473\n20474\n20475\n20476\n20477\n20478\n20479\n20480\n20481\n20482\n20483\n20484\n20485\n20486\n20487\n20488\n20489\n20490\n20491\n20492\n20493\n20494\n20495\n20496\n20497\n20498\n20499\n20500\n20501\n20502\n20503\n20504\n20505\n20506\n20507\n20508\n20509\n20510\n20511\n20512\n20513\n20514\n20515\n20516\n20517\n20518\n20519\n20520\n20521\n20522\n20523\n20524\n20525\n20526\n20527\n20528\n20529\n20530\n20531\n20532\n20533\n20534\n20535\n20536\n20537\n20538\n20539\n20540\n20541\n20542\n20543\n20544\n20545\n20546\n20547\n20548\n20549\n20550\n20551\n20552\n20553\n20554\n20555\n20556\n20557\n20558\n20559\n20560\n20561\n20562\n20563\n20564\n20565\n20566\n20567\n20568\n20569\n20570\n20571\n20572\n20573\n20574\n20575\n20576\n20577\n20578\n20579\n20580\n20581\n20582\n20583\n20584\n20585\n20586\n20587\n20588\n20589\n20590\n20591\n20592\n20593\n20594\n20595\n20596\n20597\n20598\n20599\n20600\n20601\n20602\n20603\n20604\n20605\n20606\n20607\n20608\n20609\n20610\n20611\n20612\n20613\n20614\n20615\n20616\n20617\n20618\n20619\n20620\n20621\n20622\n20623\n20624\n20625\n20626\n20627\n20628\n20629\n20630\n20631\n20632\n20633\n20634\n20635\n20636\n20637\n20638\n20639\n20640\n20641\n20642\n20643\n20644\n20645\n20646\n20647\n20648\n20649\n20650\n20651\n20652\n20653\n20654\n20655\n20656\n20657\n20658\n20659\n20660\n20661\n20662\n20663\n20664\n20665\n20666\n20667\n20668\n20669\n20670\n20671\n20672\n20673\n20674\n20675\n20676\n20677\n20678\n20679\n20680\n20681\n20682\n20683\n20684\n20685\n20686\n20687\n20688\n20689\n20690\n20691\n20692\n20693\n20694\n20695\n20696\n20697\n20698\n20699\n20700\n20701\n20702\n20703\n20704\n20705\n20706\n20707\n20708\n20709\n20710\n20711\n20712\n20713\n20714\n20715\n20716\n20717\n20718\n20719\n20720\n20721\n20722\n20723\n20724\n20725\n20726\n20727\n20728\n20729\n20730\n20731\n20732\n20733\n20734\n20735\n20736\n20737\n20738\n20739\n20740\n20741\n20742\n20743\n20744\n20745\n20746\n20747\n20748\n20749\n20750\n20751\n20752\n20753\n20754\n20755\n20756\n20757\n20758\n20759\n20760\n20761\n20762\n20763\n20764\n20765\n20766\n20767\n20768\n20769\n20770\n20771\n20772\n20773\n20774\n20775\n20776\n20777\n20778\n20779\n20780\n20781\n20782\n20783\n20784\n20785\n20786\n20787\n20788\n20789\n20790\n20791\n20792\n20793\n20794\n20795\n20796\n20797\n20798\n20799\n20800\n20801\n20802\n20803\n20804\n20805\n20806\n20807\n20808\n20809\n20810\n20811\n20812\n20813\n20814\n20815\n20816\n20817\n20818\n20819\n20820\n20821\n20822\n20823\n20824\n20825\n20826\n20827\n20828\n20829\n20830\n20831\n20832\n20833\n20834\n20835\n20836\n20837\n20838\n20839\n20840\n20841\n20842\n20843\n20844\n20845\n20846\n20847\n20848\n20849\n20850\n20851\n20852\n20853\n20854\n20855\n20856\n20857\n20858\n20859\n20860\n20861\n20862\n20863\n20864\n20865\n20866\n20867\n20868\n20869\n20870\n20871\n20872\n20873\n20874\n20875\n20876\n20877\n20878\n20879\n20880\n20881\n20882\n20883\n20884\n20885\n20886\n20887\n20888\n20889\n20890\n20891\n20892\n20893\n20894\n20895\n20896\n20897\n20898\n20899\n20900\n20901\n20902\n20903\n20904\n20905\n20906\n20907\n20908\n20909\n20910\n20911\n20912\n20913\n20914\n20915\n20916\n20917\n20918\n20919\n20920\n20921\n20922\n20923\n20924\n20925\n20926\n20927\n20928\n20929\n20930\n20931\n20932\n20933\n20934\n20935\n20936\n20937\n20938\n20939\n20940\n20941\n20942\n20943\n20944\n20945\n20946\n20947\n20948\n20949\n20950\n20951\n20952\n20953\n20954\n20955\n20956\n20957\n20958\n20959\n20960\n20961\n20962\n20963\n20964\n20965\n20966\n20967\n20968\n20969\n20970\n20971\n20972\n20973\n20974\n20975\n20976\n20977\n20978\n20979\n20980\n20981\n20982\n20983\n20984\n20985\n20986\n20987\n20988\n20989\n20990\n20991\n20992\n20993\n20994\n20995\n20996\n20997\n20998\n20999\n21000\n21001\n21002\n21003\n21004\n21005\n21006\n21007\n21008\n21009\n21010\n21011\n21012\n21013\n21014\n21015\n21016\n21017\n21018\n21019\n21020\n21021\n21022\n21023\n21024\n21025\n21026\n21027\n21028\n21029\n21030\n21031\n21032\n21033\n21034\n21035\n21036\n21037\n21038\n21039\n21040\n21041\n21042\n21043\n21044\n21045\n21046\n21047\n21048\n21049\n21050\n21051\n21052\n21053\n21054\n21055\n21056\n21057\n21058\n21059\n21060\n21061\n21062\n21063\n21064\n21065\n21066\n21067\n21068\n21069\n21070\n21071\n21072\n21073\n21074\n21075\n21076\n21077\n21078\n21079\n21080\n21081\n21082\n21083\n21084\n21085\n21086\n21087\n21088\n21089\n21090\n21091\n21092\n21093\n21094\n21095\n21096\n21097\n21098\n21099\n21100\n21101\n21102\n21103\n21104\n21105\n21106\n21107\n21108\n21109\n21110\n21111\n21112\n21113\n21114\n21115\n21116\n21117\n21118\n21119\n21120\n21121\n21122\n21123\n21124\n21125\n21126\n21127\n21128\n21129\n21130\n21131\n21132\n21133\n21134\n21135\n21136\n21137\n21138\n21139\n21140\n21141\n21142\n21143\n21144\n21145\n21146\n21147\n21148\n21149\n21150\n21151\n21152\n21153\n21154\n21155\n21156\n21157\n21158\n21159\n21160\n21161\n21162\n21163\n21164\n21165\n21166\n21167\n21168\n21169\n21170\n21171\n21172\n21173\n21174\n21175\n21176\n21177\n21178\n21179\n21180\n21181\n21182\n21183\n21184\n21185\n21186\n21187\n21188\n21189\n21190\n21191\n21192\n21193\n21194\n21195\n21196\n21197\n21198\n21199\n21200\n21201\n21202\n21203\n21204\n21205\n21206\n21207\n21208\n21209\n21210\n21211\n21212\n21213\n21214\n21215\n21216\n21217\n21218\n21219\n21220\n21221\n21222\n21223\n21224\n21225\n21226\n21227\n21228\n21229\n21230\n21231\n21232\n21233\n21234\n21235\n21236\n21237\n21238\n21239\n21240\n21241\n21242\n21243\n21244\n21245\n21246\n21247\n21248\n21249\n21250\n21251\n21252\n21253\n21254\n21255\n21256\n21257\n21258\n21259\n21260\n21261\n21262\n21263\n21264\n21265\n21266\n21267\n21268\n21269\n21270\n21271\n21272\n21273\n21274\n21275\n21276\n21277\n21278\n21279\n21280\n21281\n21282\n21283\n21284\n21285\n21286\n21287\n21288\n21289\n21290\n21291\n21292\n21293\n21294\n21295\n21296\n21297\n21298\n21299\n21300\n21301\n21302\n21303\n21304\n21305\n21306\n21307\n21308\n21309\n21310\n21311\n21312\n21313\n21314\n21315\n21316\n21317\n21318\n21319\n21320\n21321\n21322\n21323\n21324\n21325\n21326\n21327\n21328\n21329\n21330\n21331\n21332\n21333\n21334\n21335\n21336\n21337\n21338\n21339\n21340\n21341\n21342\n21343\n21344\n21345\n21346\n21347\n21348\n21349\n21350\n21351\n21352\n21353\n21354\n21355\n21356\n21357\n21358\n21359\n21360\n21361\n21362\n21363\n21364\n21365\n21366\n21367\n21368\n21369\n21370\n21371\n21372\n21373\n21374\n21375\n21376\n21377\n21378\n21379\n21380\n21381\n21382\n21383\n21384\n21385\n21386\n21387\n21388\n21389\n21390\n21391\n21392\n21393\n21394\n21395\n21396\n21397\n21398\n21399\n21400\n21401\n21402\n21403\n21404\n21405\n21406\n21407\n21408\n21409\n21410\n21411\n21412\n21413\n21414\n21415\n21416\n21417\n21418\n21419\n21420\n21421\n21422\n21423\n21424\n21425\n21426\n21427\n21428\n21429\n21430\n21431\n21432\n21433\n21434\n21435\n21436\n21437\n21438\n21439\n21440\n21441\n21442\n21443\n21444\n21445\n21446\n21447\n21448\n21449\n21450\n21451\n21452\n21453\n21454\n21455\n21456\n21457\n21458\n21459\n21460\n21461\n21462\n21463\n21464\n21465\n21466\n21467\n21468\n21469\n21470\n21471\n21472\n21473\n21474\n21475\n21476\n21477\n21478\n21479\n21480\n21481\n21482\n21483\n21484\n21485\n21486\n21487\n21488\n21489\n21490\n21491\n21492\n21493\n21494\n21495\n21496\n21497\n21498\n21499\n21500\n21501\n21502\n21503\n21504\n21505\n21506\n21507\n21508\n21509\n21510\n21511\n21512\n21513\n21514\n21515\n21516\n21517\n21518\n21519\n21520\n21521\n21522\n21523\n21524\n21525\n21526\n21527\n21528\n21529\n21530\n21531\n21532\n21533\n21534\n21535\n21536\n21537\n21538\n21539\n21540\n21541\n21542\n21543\n21544\n21545\n21546\n21547\n21548\n21549\n21550\n21551\n21552\n21553\n21554\n21555\n21556\n21557\n21558\n21559\n21560\n21561\n21562\n21563\n21564\n21565\n21566\n21567\n21568\n21569\n21570\n21571\n21572\n21573\n21574\n21575\n21576\n21577\n21578\n21579\n21580\n21581\n21582\n21583\n21584\n21585\n21586\n21587\n21588\n21589\n21590\n21591\n21592\n21593\n21594\n21595\n21596\n21597\n21598\n21599\n21600\n21601\n21602\n21603\n21604\n21605\n21606\n21607\n21608\n21609\n21610\n21611\n21612\n21613\n21614\n21615\n21616\n21617\n21618\n21619\n21620\n21621\n21622\n21623\n21624\n21625\n21626\n21627\n21628\n21629\n21630\n21631\n21632\n21633\n21634\n21635\n21636\n21637\n21638\n21639\n21640\n21641\n21642\n21643\n21644\n21645\n21646\n21647\n21648\n21649\n21650\n21651\n21652\n21653\n21654\n21655\n21656\n21657\n21658\n21659\n21660\n21661\n21662\n21663\n21664\n21665\n21666\n21667\n21668\n21669\n21670\n21671\n21672\n21673\n21674\n21675\n21676\n21677\n21678\n21679\n21680\n21681\n21682\n21683\n21684\n21685\n21686\n21687\n21688\n21689\n21690\n21691\n21692\n21693\n21694\n21695\n21696\n21697\n21698\n21699\n21700\n21701\n21702\n21703\n21704\n21705\n21706\n21707\n21708\n21709\n21710\n21711\n21712\n21713\n21714\n21715\n21716\n21717\n21718\n21719\n21720\n21721\n21722\n21723\n21724\n21725\n21726\n21727\n21728\n21729\n21730\n21731\n21732\n21733\n21734\n21735\n21736\n21737\n21738\n21739\n21740\n21741\n21742\n21743\n21744\n21745\n21746\n21747\n21748\n21749\n21750\n21751\n21752\n21753\n21754\n21755\n21756\n21757\n21758\n21759\n21760\n21761\n21762\n21763\n21764\n21765\n21766\n21767\n21768\n21769\n21770\n21771\n21772\n21773\n21774\n21775\n21776\n21777\n21778\n21779\n21780\n21781\n21782\n21783\n21784\n21785\n21786\n21787\n21788\n21789\n21790\n21791\n21792\n21793\n21794\n21795\n21796\n21797\n21798\n21799\n21800\n21801\n21802\n21803\n21804\n21805\n21806\n21807\n21808\n21809\n21810\n21811\n21812\n21813\n21814\n21815\n21816\n21817\n21818\n21819\n21820\n21821\n21822\n21823\n21824\n21825\n21826\n21827\n21828\n21829\n21830\n21831\n21832\n21833\n21834\n21835\n21836\n21837\n21838\n21839\n21840\n21841\n21842\n21843\n21844\n21845\n21846\n21847\n21848\n21849\n21850\n21851\n21852\n21853\n21854\n21855\n21856\n21857\n21858\n21859\n21860\n21861\n21862\n21863\n21864\n21865\n21866\n21867\n21868\n21869\n21870\n21871\n21872\n21873\n21874\n21875\n21876\n21877\n21878\n21879\n21880\n21881\n21882\n21883\n21884\n21885\n21886\n21887\n21888\n21889\n21890\n21891\n21892\n21893\n21894\n21895\n21896\n21897\n21898\n21899\n21900\n21901\n21902\n21903\n21904\n21905\n21906\n21907\n21908\n21909\n21910\n21911\n21912\n21913\n21914\n21915\n21916\n21917\n21918\n21919\n21920\n21921\n21922\n21923\n21924\n21925\n21926\n21927\n21928\n21929\n21930\n21931\n21932\n21933\n21934\n21935\n21936\n21937\n21938\n21939\n21940\n21941\n21942\n21943\n21944\n21945\n21946\n21947\n21948\n21949\n21950\n21951\n21952\n21953\n21954\n21955\n21956\n21957\n21958\n21959\n21960\n21961\n21962\n21963\n21964\n21965\n21966\n21967\n21968\n21969\n21970\n21971\n21972\n21973\n21974\n21975\n21976\n21977\n21978\n21979\n21980\n21981\n21982\n21983\n21984\n21985\n21986\n21987\n21988\n21989\n21990\n21991\n21992\n21993\n21994\n21995\n21996\n21997\n21998\n21999\n22000\n22001\n22002\n22003\n22004\n22005\n22006\n22007\n22008\n22009\n22010\n22011\n22012\n22013\n22014\n22015\n22016\n22017\n22018\n22019\n22020\n22021\n22022\n22023\n22024\n22025\n22026\n22027\n22028\n22029\n22030\n22031\n22032\n22033\n22034\n22035\n22036\n22037\n22038\n22039\n22040\n22041\n22042\n22043\n22044\n22045\n22046\n22047\n22048\n22049\n22050\n22051\n22052\n22053\n22054\n22055\n22056\n22057\n22058\n22059\n22060\n22061\n22062\n22063\n22064\n22065\n22066\n22067\n22068\n22069\n22070\n22071\n22072\n22073\n22074\n22075\n22076\n22077\n22078\n22079\n22080\n22081\n22082\n22083\n22084\n22085\n22086\n22087\n22088\n22089\n22090\n22091\n22092\n22093\n22094\n22095\n22096\n22097\n22098\n22099\n22100\n22101\n22102\n22103\n22104\n22105\n22106\n22107\n22108\n22109\n22110\n22111\n22112\n22113\n22114\n22115\n22116\n22117\n22118\n22119\n22120\n22121\n22122\n22123\n22124\n22125\n22126\n22127\n22128\n22129\n22130\n22131\n22132\n22133\n22134\n22135\n22136\n22137\n22138\n22139\n22140\n22141\n22142\n22143\n22144\n22145\n22146\n22147\n22148\n22149\n22150\n22151\n22152\n22153\n22154\n22155\n22156\n22157\n22158\n22159\n22160\n22161\n22162\n22163\n22164\n22165\n22166\n22167\n22168\n22169\n22170\n22171\n22172\n22173\n22174\n22175\n22176\n22177\n22178\n22179\n22180\n22181\n22182\n22183\n22184\n22185\n22186\n22187\n22188\n22189\n22190\n22191\n22192\n22193\n22194\n22195\n22196\n22197\n22198\n22199\n22200\n22201\n22202\n22203\n22204\n22205\n22206\n22207\n22208\n22209\n22210\n22211\n22212\n22213\n22214\n22215\n22216\n22217\n22218\n22219\n22220\n22221\n22222\n22223\n22224\n22225\n22226\n22227\n22228\n22229\n22230\n22231\n22232\n22233\n22234\n22235\n22236\n22237\n22238\n22239\n22240\n22241\n22242\n22243\n22244\n22245\n22246\n22247\n22248\n22249\n22250\n22251\n22252\n22253\n22254\n22255\n22256\n22257\n22258\n22259\n22260\n22261\n22262\n22263\n22264\n22265\n22266\n22267\n22268\n22269\n22270\n22271\n22272\n22273\n22274\n22275\n22276\n22277\n22278\n22279\n22280\n22281\n22282\n22283\n22284\n22285\n22286\n22287\n22288\n22289\n22290\n22291\n22292\n22293\n22294\n22295\n22296\n22297\n22298\n22299\n22300\n22301\n22302\n22303\n22304\n22305\n22306\n22307\n22308\n22309\n22310\n22311\n22312\n22313\n22314\n22315\n22316\n22317\n22318\n22319\n22320\n22321\n22322\n22323\n22324\n22325\n22326\n22327\n22328\n22329\n22330\n22331\n22332\n22333\n22334\n22335\n22336\n22337\n22338\n22339\n22340\n22341\n22342\n22343\n22344\n22345\n22346\n22347\n22348\n22349\n22350\n22351\n22352\n22353\n22354\n22355\n22356\n22357\n22358\n22359\n22360\n22361\n22362\n22363\n22364\n22365\n22366\n22367\n22368\n22369\n22370\n22371\n22372\n22373\n22374\n22375\n22376\n22377\n22378\n22379\n22380\n22381\n22382\n22383\n22384\n22385\n22386\n22387\n22388\n22389\n22390\n22391\n22392\n22393\n22394\n22395\n22396\n22397\n22398\n22399\n22400\n22401\n22402\n22403\n22404\n22405\n22406\n22407\n22408\n22409\n22410\n22411\n22412\n22413\n22414\n22415\n22416\n22417\n22418\n22419\n22420\n22421\n22422\n22423\n22424\n22425\n22426\n22427\n22428\n22429\n22430\n22431\n22432\n22433\n22434\n22435\n22436\n22437\n22438\n22439\n22440\n22441\n22442\n22443\n22444\n22445\n22446\n22447\n22448\n22449\n22450\n22451\n22452\n22453\n22454\n22455\n22456\n22457\n22458\n22459\n22460\n22461\n22462\n22463\n22464\n22465\n22466\n22467\n22468\n22469\n22470\n22471\n22472\n22473\n22474\n22475\n22476\n22477\n22478\n22479\n22480\n22481\n22482\n22483\n22484\n22485\n22486\n22487\n22488\n22489\n22490\n22491\n22492\n22493\n22494\n22495\n22496\n22497\n22498\n22499\n22500\n22501\n22502\n22503\n22504\n22505\n22506\n22507\n22508\n22509\n22510\n22511\n22512\n22513\n22514\n22515\n22516\n22517\n22518\n22519\n22520\n22521\n22522\n22523\n22524\n22525\n22526\n22527\n22528\n22529\n22530\n22531\n22532\n22533\n22534\n22535\n22536\n22537\n22538\n22539\n22540\n22541\n22542\n22543\n22544\n22545\n22546\n22547\n22548\n22549\n22550\n22551\n22552\n22553\n22554\n22555\n22556\n22557\n22558\n22559\n22560\n22561\n22562\n22563\n22564\n22565\n22566\n22567\n22568\n22569\n22570\n22571\n22572\n22573\n22574\n22575\n22576\n22577\n22578\n22579\n22580\n22581\n22582\n22583\n22584\n22585\n22586\n22587\n22588\n22589\n22590\n22591\n22592\n22593\n22594\n22595\n22596\n22597\n22598\n22599\n22600\n22601\n22602\n22603\n22604\n22605\n22606\n22607\n22608\n22609\n22610\n22611\n22612\n22613\n22614\n22615\n22616\n22617\n22618\n22619\n22620\n22621\n22622\n22623\n22624\n22625\n22626\n22627\n22628\n22629\n22630\n22631\n22632\n22633\n22634\n22635\n22636\n22637\n22638\n22639\n22640\n22641\n22642\n22643\n22644\n22645\n22646\n22647\n22648\n22649\n22650\n22651\n22652\n22653\n22654\n22655\n22656\n22657\n22658\n22659\n22660\n22661\n22662\n22663\n22664\n22665\n22666\n22667\n22668\n22669\n22670\n22671\n22672\n22673\n22674\n22675\n22676\n22677\n22678\n22679\n22680\n22681\n22682\n22683\n22684\n22685\n22686\n22687\n22688\n22689\n22690\n22691\n22692\n22693\n22694\n22695\n22696\n22697\n22698\n22699\n22700\n22701\n22702\n22703\n22704\n22705\n22706\n22707\n22708\n22709\n22710\n22711\n22712\n22713\n22714\n22715\n22716\n22717\n22718\n22719\n22720\n22721\n22722\n22723\n22724\n22725\n22726\n22727\n22728\n22729\n22730\n22731\n22732\n22733\n22734\n22735\n22736\n22737\n22738\n22739\n22740\n22741\n22742\n22743\n22744\n22745\n22746\n22747\n22748\n22749\n22750\n22751\n22752\n22753\n22754\n22755\n22756\n22757\n22758\n22759\n22760\n22761\n22762\n22763\n22764\n22765\n22766\n22767\n22768\n22769\n22770\n22771\n22772\n22773\n22774\n22775\n22776\n22777\n22778\n22779\n22780\n22781\n22782\n22783\n22784\n22785\n22786\n22787\n22788\n22789\n22790\n22791\n22792\n22793\n22794\n22795\n22796\n22797\n22798\n22799\n22800\n22801\n22802\n22803\n22804\n22805\n22806\n22807\n22808\n22809\n22810\n22811\n22812\n22813\n22814\n22815\n22816\n22817\n22818\n22819\n22820\n22821\n22822\n22823\n22824\n22825\n22826\n22827\n22828\n22829\n22830\n22831\n22832\n22833\n22834\n22835\n22836\n22837\n22838\n22839\n22840\n22841\n22842\n22843\n22844\n22845\n22846\n22847\n22848\n22849\n22850\n22851\n22852\n22853\n22854\n22855\n22856\n22857\n22858\n22859\n22860\n22861\n22862\n22863\n22864\n22865\n22866\n22867\n22868\n22869\n22870\n22871\n22872\n22873\n22874\n22875\n22876\n22877\n22878\n22879\n22880\n22881\n22882\n22883\n22884\n22885\n22886\n22887\n22888\n22889\n22890\n22891\n22892\n22893\n22894\n22895\n22896\n22897\n22898\n22899\n22900\n22901\n22902\n22903\n22904\n22905\n22906\n22907\n22908\n22909\n22910\n22911\n22912\n22913\n22914\n22915\n22916\n22917\n22918\n22919\n22920\n22921\n22922\n22923\n22924\n22925\n22926\n22927\n22928\n22929\n22930\n22931\n22932\n22933\n22934\n22935\n22936\n22937\n22938\n22939\n22940\n22941\n22942\n22943\n22944\n22945\n22946\n22947\n22948\n22949\n22950\n22951\n22952\n22953\n22954\n22955\n22956\n22957\n22958\n22959\n22960\n22961\n22962\n22963\n22964\n22965\n22966\n22967\n22968\n22969\n22970\n22971\n22972\n22973\n22974\n22975\n22976\n22977\n22978\n22979\n22980\n22981\n22982\n22983\n22984\n22985\n22986\n22987\n22988\n22989\n22990\n22991\n22992\n22993\n22994\n22995\n22996\n22997\n22998\n22999\n23000\n23001\n23002\n23003\n23004\n23005\n23006\n23007\n23008\n23009\n23010\n23011\n23012\n23013\n23014\n23015\n23016\n23017\n23018\n23019\n23020\n23021\n23022\n23023\n23024\n23025\n23026\n23027\n23028\n23029\n23030\n23031\n23032\n23033\n23034\n23035\n23036\n23037\n23038\n23039\n23040\n23041\n23042\n23043\n23044\n23045\n23046\n23047\n23048\n23049\n23050\n23051\n23052\n23053\n23054\n23055\n23056\n23057\n23058\n23059\n23060\n23061\n23062\n23063\n23064\n23065\n23066\n23067\n23068\n23069\n23070\n23071\n23072\n23073\n23074\n23075\n23076\n23077\n23078\n23079\n23080\n23081\n23082\n23083\n23084\n23085\n23086\n23087\n23088\n23089\n23090\n23091\n23092\n23093\n23094\n23095\n23096\n23097\n23098\n23099\n23100\n23101\n23102\n23103\n23104\n23105\n23106\n23107\n23108\n23109\n23110\n23111\n23112\n23113\n23114\n23115\n23116\n23117\n23118\n23119\n23120\n23121\n23122\n23123\n23124\n23125\n23126\n23127\n23128\n23129\n23130\n23131\n23132\n23133\n23134\n23135\n23136\n23137\n23138\n23139\n23140\n23141\n23142\n23143\n23144\n23145\n23146\n23147\n23148\n23149\n23150\n23151\n23152\n23153\n23154\n23155\n23156\n23157\n23158\n23159\n23160\n23161\n23162\n23163\n23164\n23165\n23166\n23167\n23168\n23169\n23170\n23171\n23172\n23173\n23174\n23175\n23176\n23177\n23178\n23179\n23180\n23181\n23182\n23183\n23184\n23185\n23186\n23187\n23188\n23189\n23190\n23191\n23192\n23193\n23194\n23195\n23196\n23197\n23198\n23199\n23200\n23201\n23202\n23203\n23204\n23205\n23206\n23207\n23208\n23209\n23210\n23211\n23212\n23213\n23214\n23215\n23216\n23217\n23218\n23219\n23220\n23221\n23222\n23223\n23224\n23225\n23226\n23227\n23228\n23229\n23230\n23231\n23232\n23233\n23234\n23235\n23236\n23237\n23238\n23239\n23240\n23241\n23242\n23243\n23244\n23245\n23246\n23247\n23248\n23249\n23250\n23251\n23252\n23253\n23254\n23255\n23256\n23257\n23258\n23259\n23260\n23261\n23262\n23263\n23264\n23265\n23266\n23267\n23268\n23269\n23270\n23271\n23272\n23273\n23274\n23275\n23276\n23277\n23278\n23279\n23280\n23281\n23282\n23283\n23284\n23285\n23286\n23287\n23288\n23289\n23290\n23291\n23292\n23293\n23294\n23295\n23296\n23297\n23298\n23299\n23300\n23301\n23302\n23303\n23304\n23305\n23306\n23307\n23308\n23309\n23310\n23311\n23312\n23313\n23314\n23315\n23316\n23317\n23318\n23319\n23320\n23321\n23322\n23323\n23324\n23325\n23326\n23327\n23328\n23329\n23330\n23331\n23332\n23333\n23334\n23335\n23336\n23337\n23338\n23339\n23340\n23341\n23342\n23343\n23344\n23345\n23346\n23347\n23348\n23349\n23350\n23351\n23352\n23353\n23354\n23355\n23356\n23357\n23358\n23359\n23360\n23361\n23362\n23363\n23364\n23365\n23366\n23367\n23368\n23369\n23370\n23371\n23372\n23373\n23374\n23375\n23376\n23377\n23378\n23379\n23380\n23381\n23382\n23383\n23384\n23385\n23386\n23387\n23388\n23389\n23390\n23391\n23392\n23393\n23394\n23395\n23396\n23397\n23398\n23399\n23400\n23401\n23402\n23403\n23404\n23405\n23406\n23407\n23408\n23409\n23410\n23411\n23412\n23413\n23414\n23415\n23416\n23417\n23418\n23419\n23420\n23421\n23422\n23423\n23424\n23425\n23426\n23427\n23428\n23429\n23430\n23431\n23432\n23433\n23434\n23435\n23436\n23437\n23438\n23439\n23440\n23441\n23442\n23443\n23444\n23445\n23446\n23447\n23448\n23449\n23450\n23451\n23452\n23453\n23454\n23455\n23456\n23457\n23458\n23459\n23460\n23461\n23462\n23463\n23464\n23465\n23466\n23467\n23468\n23469\n23470\n23471\n23472\n23473\n23474\n23475\n23476\n23477\n23478\n23479\n23480\n23481\n23482\n23483\n23484\n23485\n23486\n23487\n23488\n23489\n23490\n23491\n23492\n23493\n23494\n23495\n23496\n23497\n23498\n23499\n23500\n23501\n23502\n23503\n23504\n23505\n23506\n23507\n23508\n23509\n23510\n23511\n23512\n23513\n23514\n23515\n23516\n23517\n23518\n23519\n23520\n23521\n23522\n23523\n23524\n23525\n23526\n23527\n23528\n23529\n23530\n23531\n23532\n23533\n23534\n23535\n23536\n23537\n23538\n23539\n23540\n23541\n23542\n23543\n23544\n23545\n23546\n23547\n23548\n23549\n23550\n23551\n23552\n23553\n23554\n23555\n23556\n23557\n23558\n23559\n23560\n23561\n23562\n23563\n23564\n23565\n23566\n23567\n23568\n23569\n23570\n23571\n23572\n23573\n23574\n23575\n23576\n23577\n23578\n23579\n23580\n23581\n23582\n23583\n23584\n23585\n23586\n23587\n23588\n23589\n23590\n23591\n23592\n23593\n23594\n23595\n23596\n23597\n23598\n23599\n23600\n23601\n23602\n23603\n23604\n23605\n23606\n23607\n23608\n23609\n23610\n23611\n23612\n23613\n23614\n23615\n23616\n23617\n23618\n23619\n23620\n23621\n23622\n23623\n23624\n23625\n23626\n23627\n23628\n23629\n23630\n23631\n23632\n23633\n23634\n23635\n23636\n23637\n23638\n23639\n23640\n23641\n23642\n23643\n23644\n23645\n23646\n23647\n23648\n23649\n23650\n23651\n23652\n23653\n23654\n23655\n23656\n23657\n23658\n23659\n23660\n23661\n23662\n23663\n23664\n23665\n23666\n23667\n23668\n23669\n23670\n23671\n23672\n23673\n23674\n23675\n23676\n23677\n23678\n23679\n23680\n23681\n23682\n23683\n23684\n23685\n23686\n23687\n23688\n23689\n23690\n23691\n23692\n23693\n23694\n23695\n23696\n23697\n23698\n23699\n23700\n23701\n23702\n23703\n23704\n23705\n23706\n23707\n23708\n23709\n23710\n23711\n23712\n23713\n23714\n23715\n23716\n23717\n23718\n23719\n23720\n23721\n23722\n23723\n23724\n23725\n23726\n23727\n23728\n23729\n23730\n23731\n23732\n23733\n23734\n23735\n23736\n23737\n23738\n23739\n23740\n23741\n23742\n23743\n23744\n23745\n23746\n23747\n23748\n23749\n23750\n23751\n23752\n23753\n23754\n23755\n23756\n23757\n23758\n23759\n23760\n23761\n23762\n23763\n23764\n23765\n23766\n23767\n23768\n23769\n23770\n23771\n23772\n23773\n23774\n23775\n23776\n23777\n23778\n23779\n23780\n23781\n23782\n23783\n23784\n23785\n23786\n23787\n23788\n23789\n23790\n23791\n23792\n23793\n23794\n23795\n23796\n23797\n23798\n23799\n23800\n23801\n23802\n23803\n23804\n23805\n23806\n23807\n23808\n23809\n23810\n23811\n23812\n23813\n23814\n23815\n23816\n23817\n23818\n23819\n23820\n23821\n23822\n23823\n23824\n23825\n23826\n23827\n23828\n23829\n23830\n23831\n23832\n23833\n23834\n23835\n23836\n23837\n23838\n23839\n23840\n23841\n23842\n23843\n23844\n23845\n23846\n23847\n23848\n23849\n23850\n23851\n23852\n23853\n23854\n23855\n23856\n23857\n23858\n23859\n23860\n23861\n23862\n23863\n23864\n23865\n23866\n23867\n23868\n23869\n23870\n23871\n23872\n23873\n23874\n23875\n23876\n23877\n23878\n23879\n23880\n23881\n23882\n23883\n23884\n23885\n23886\n23887\n23888\n23889\n23890\n23891\n23892\n23893\n23894\n23895\n23896\n23897\n23898\n23899\n23900\n23901\n23902\n23903\n23904\n23905\n23906\n23907\n23908\n23909\n23910\n23911\n23912\n23913\n23914\n23915\n23916\n23917\n23918\n23919\n23920\n23921\n23922\n23923\n23924\n23925\n23926\n23927\n23928\n23929\n23930\n23931\n23932\n23933\n23934\n23935\n23936\n23937\n23938\n23939\n23940\n23941\n23942\n23943\n23944\n23945\n23946\n23947\n23948\n23949\n23950\n23951\n23952\n23953\n23954\n23955\n23956\n23957\n23958\n23959\n23960\n23961\n23962\n23963\n23964\n23965\n23966\n23967\n23968\n23969\n23970\n23971\n23972\n23973\n23974\n23975\n23976\n23977\n23978\n23979\n23980\n23981\n23982\n23983\n23984\n23985\n23986\n23987\n23988\n23989\n23990\n23991\n23992\n23993\n23994\n23995\n23996\n23997\n23998\n23999\n24000\n24001\n24002\n24003\n24004\n24005\n24006\n24007\n24008\n24009\n24010\n24011\n24012\n24013\n24014\n24015\n24016\n24017\n24018\n24019\n24020\n24021\n24022\n24023\n24024\n24025\n24026\n24027\n24028\n24029\n24030\n24031\n24032\n24033\n24034\n24035\n24036\n24037\n24038\n24039\n24040\n24041\n24042\n24043\n24044\n24045\n24046\n24047\n24048\n24049\n24050\n24051\n24052\n24053\n24054\n24055\n24056\n24057\n24058\n24059\n24060\n24061\n24062\n24063\n24064\n24065\n24066\n24067\n24068\n24069\n24070\n24071\n24072\n24073\n24074\n24075\n24076\n24077\n24078\n24079\n24080\n24081\n24082\n24083\n24084\n24085\n24086\n24087\n24088\n24089\n24090\n24091\n24092\n24093\n24094\n24095\n24096\n24097\n24098\n24099\n24100\n24101\n24102\n24103\n24104\n24105\n24106\n24107\n24108\n24109\n24110\n24111\n24112\n24113\n24114\n24115\n24116\n24117\n24118\n24119\n24120\n24121\n24122\n24123\n24124\n24125\n24126\n24127\n24128\n24129\n24130\n24131\n24132\n24133\n24134\n24135\n24136\n24137\n24138\n24139\n24140\n24141\n24142\n24143\n24144\n24145\n24146\n24147\n24148\n24149\n24150\n24151\n24152\n24153\n24154\n24155\n24156\n24157\n24158\n24159\n24160\n24161\n24162\n24163\n24164\n24165\n24166\n24167\n24168\n24169\n24170\n24171\n24172\n24173\n24174\n24175\n24176\n24177\n24178\n24179\n24180\n24181\n24182\n24183\n24184\n24185\n24186\n24187\n24188\n24189\n24190\n24191\n24192\n24193\n24194\n24195\n24196\n24197\n24198\n24199\n24200\n24201\n24202\n24203\n24204\n24205\n24206\n24207\n24208\n24209\n24210\n24211\n24212\n24213\n24214\n24215\n24216\n24217\n24218\n24219\n24220\n24221\n24222\n24223\n24224\n24225\n24226\n24227\n24228\n24229\n24230\n24231\n24232\n24233\n24234\n24235\n24236\n24237\n24238\n24239\n24240\n24241\n24242\n24243\n24244\n24245\n24246\n24247\n24248\n24249\n24250\n24251\n24252\n24253\n24254\n24255\n24256\n24257\n24258\n24259\n24260\n24261\n24262\n24263\n24264\n24265\n24266\n24267\n24268\n24269\n24270\n24271\n24272\n24273\n24274\n24275\n24276\n24277\n24278\n24279\n24280\n24281\n24282\n24283\n24284\n24285\n24286\n24287\n24288\n24289\n24290\n24291\n24292\n24293\n24294\n24295\n24296\n24297\n24298\n24299\n24300\n24301\n24302\n24303\n24304\n24305\n24306\n24307\n24308\n24309\n24310\n24311\n24312\n24313\n24314\n24315\n24316\n24317\n24318\n24319\n24320\n24321\n24322\n24323\n24324\n24325\n24326\n24327\n24328\n24329\n24330\n24331\n24332\n24333\n24334\n24335\n24336\n24337\n24338\n24339\n24340\n24341\n24342\n24343\n24344\n24345\n24346\n24347\n24348\n24349\n24350\n24351\n24352\n24353\n24354\n24355\n24356\n24357\n24358\n24359\n24360\n24361\n24362\n24363\n24364\n24365\n24366\n24367\n24368\n24369\n24370\n24371\n24372\n24373\n24374\n24375\n24376\n24377\n24378\n24379\n24380\n24381\n24382\n24383\n24384\n24385\n24386\n24387\n24388\n24389\n24390\n24391\n24392\n24393\n24394\n24395\n24396\n24397\n24398\n24399\n24400\n24401\n24402\n24403\n24404\n24405\n24406\n24407\n24408\n24409\n24410\n24411\n24412\n24413\n24414\n24415\n24416\n24417\n24418\n24419\n24420\n24421\n24422\n24423\n24424\n24425\n24426\n24427\n24428\n24429\n24430\n24431\n24432\n24433\n24434\n24435\n24436\n24437\n24438\n24439\n24440\n24441\n24442\n24443\n24444\n24445\n24446\n24447\n24448\n24449\n24450\n24451\n24452\n24453\n24454\n24455\n24456\n24457\n24458\n24459\n24460\n24461\n24462\n24463\n24464\n24465\n24466\n24467\n24468\n24469\n24470\n24471\n24472\n24473\n24474\n24475\n24476\n24477\n24478\n24479\n24480\n24481\n24482\n24483\n24484\n24485\n24486\n24487\n24488\n24489\n24490\n24491\n24492\n24493\n24494\n24495\n24496\n24497\n24498\n24499\n24500\n24501\n24502\n24503\n24504\n24505\n24506\n24507\n24508\n24509\n24510\n24511\n24512\n24513\n24514\n24515\n24516\n24517\n24518\n24519\n24520\n24521\n24522\n24523\n24524\n24525\n24526\n24527\n24528\n24529\n24530\n24531\n24532\n24533\n24534\n24535\n24536\n24537\n24538\n24539\n24540\n24541\n24542\n24543\n24544\n24545\n24546\n24547\n24548\n24549\n24550\n24551\n24552\n24553\n24554\n24555\n24556\n24557\n24558\n24559\n24560\n24561\n24562\n24563\n24564\n24565\n24566\n24567\n24568\n24569\n24570\n24571\n24572\n24573\n24574\n24575\n24576\n24577\n24578\n24579\n24580\n24581\n24582\n24583\n24584\n24585\n24586\n24587\n24588\n24589\n24590\n24591\n24592\n24593\n24594\n24595\n24596\n24597\n24598\n24599\n24600\n24601\n24602\n24603\n24604\n24605\n24606\n24607\n24608\n24609\n24610\n24611\n24612\n24613\n24614\n24615\n24616\n24617\n24618\n24619\n24620\n24621\n24622\n24623\n24624\n24625\n24626\n24627\n24628\n24629\n24630\n24631\n24632\n24633\n24634\n24635\n24636\n24637\n24638\n24639\n24640\n24641\n24642\n24643\n24644\n24645\n24646\n24647\n24648\n24649\n24650\n24651\n24652\n24653\n24654\n24655\n24656\n24657\n24658\n24659\n24660\n24661\n24662\n24663\n24664\n24665\n24666\n24667\n24668\n24669\n24670\n24671\n24672\n24673\n24674\n24675\n24676\n24677\n24678\n24679\n24680\n24681\n24682\n24683\n24684\n24685\n24686\n24687\n24688\n24689\n24690\n24691\n24692\n24693\n24694\n24695\n24696\n24697\n24698\n24699\n24700\n24701\n24702\n24703\n24704\n24705\n24706\n24707\n24708\n24709\n24710\n24711\n24712\n24713\n24714\n24715\n24716\n24717\n24718\n24719\n24720\n24721\n24722\n24723\n24724\n24725\n24726\n24727\n24728\n24729\n24730\n24731\n24732\n24733\n24734\n24735\n24736\n24737\n24738\n24739\n24740\n24741\n24742\n24743\n24744\n24745\n24746\n24747\n24748\n24749\n24750\n24751\n24752\n24753\n24754\n24755\n24756\n24757\n24758\n24759\n24760\n24761\n24762\n24763\n24764\n24765\n24766\n24767\n24768\n24769\n24770\n24771\n24772\n24773\n24774\n24775\n24776\n24777\n24778\n24779\n24780\n24781\n24782\n24783\n24784\n24785\n24786\n24787\n24788\n24789\n24790\n24791\n24792\n24793\n24794\n24795\n24796\n24797\n24798\n24799\n24800\n24801\n24802\n24803\n24804\n24805\n24806\n24807\n24808\n24809\n24810\n24811\n24812\n24813\n24814\n24815\n24816\n24817\n24818\n24819\n24820\n24821\n24822\n24823\n24824\n24825\n24826\n24827\n24828\n24829\n24830\n24831\n24832\n24833\n24834\n24835\n24836\n24837\n24838\n24839\n24840\n24841\n24842\n24843\n24844\n24845\n24846\n24847\n24848\n24849\n24850\n24851\n24852\n24853\n24854\n24855\n24856\n24857\n24858\n24859\n24860\n24861\n24862\n24863\n24864\n24865\n24866\n24867\n24868\n24869\n24870\n24871\n24872\n24873\n24874\n24875\n24876\n24877\n24878\n24879\n24880\n24881\n24882\n24883\n24884\n24885\n24886\n24887\n24888\n24889\n24890\n24891\n24892\n24893\n24894\n24895\n24896\n24897\n24898\n24899\n24900\n24901\n24902\n24903\n24904\n24905\n24906\n24907\n24908\n24909\n24910\n24911\n24912\n24913\n24914\n24915\n24916\n24917\n24918\n24919\n24920\n24921\n24922\n24923\n24924\n24925\n24926\n24927\n24928\n24929\n24930\n24931\n24932\n24933\n24934\n24935\n24936\n24937\n24938\n24939\n24940\n24941\n24942\n24943\n24944\n24945\n24946\n24947\n24948\n24949\n24950\n24951\n24952\n24953\n24954\n24955\n24956\n24957\n24958\n24959\n24960\n24961\n24962\n24963\n24964\n24965\n24966\n24967\n24968\n24969\n24970\n24971\n24972\n24973\n24974\n24975\n24976\n24977\n24978\n24979\n24980\n24981\n24982\n24983\n24984\n24985\n24986\n24987\n24988\n24989\n24990\n24991\n24992\n24993\n24994\n24995\n24996\n24997\n24998\n24999\n25000\n25001\n25002\n25003\n25004\n25005\n25006\n25007\n25008\n25009\n25010\n25011\n25012\n25013\n25014\n25015\n25016\n25017\n25018\n25019\n25020\n25021\n25022\n25023\n25024\n25025\n25026\n25027\n25028\n25029\n25030\n25031\n25032\n25033\n25034\n25035\n25036\n25037\n25038\n25039\n25040\n25041\n25042\n25043\n25044\n25045\n25046\n25047\n25048\n25049\n25050\n25051\n25052\n25053\n25054\n25055\n25056\n25057\n25058\n25059\n25060\n25061\n25062\n25063\n25064\n25065\n25066\n25067\n25068\n25069\n25070\n25071\n25072\n25073\n25074\n25075\n25076\n25077\n25078\n25079\n25080\n25081\n25082\n25083\n25084\n25085\n25086\n25087\n25088\n25089\n25090\n25091\n25092\n25093\n25094\n25095\n25096\n25097\n25098\n25099\n25100\n25101\n25102\n25103\n25104\n25105\n25106\n25107\n25108\n25109\n25110\n25111\n25112\n25113\n25114\n25115\n25116\n25117\n25118\n25119\n25120\n25121\n25122\n25123\n25124\n25125\n25126\n25127\n25128\n25129\n25130\n25131\n25132\n25133\n25134\n25135\n25136\n25137\n25138\n25139\n25140\n25141\n25142\n25143\n25144\n25145\n25146\n25147\n25148\n25149\n25150\n25151\n25152\n25153\n25154\n25155\n25156\n25157\n25158\n25159\n25160\n25161\n25162\n25163\n25164\n25165\n25166\n25167\n25168\n25169\n25170\n25171\n25172\n25173\n25174\n25175\n25176\n25177\n25178\n25179\n25180\n25181\n25182\n25183\n25184\n25185\n25186\n25187\n25188\n25189\n25190\n25191\n25192\n25193\n25194\n25195\n25196\n25197\n25198\n25199\n25200\n25201\n25202\n25203\n25204\n25205\n25206\n25207\n25208\n25209\n25210\n25211\n25212\n25213\n25214\n25215\n25216\n25217\n25218\n25219\n25220\n25221\n25222\n25223\n25224\n25225\n25226\n25227\n25228\n25229\n25230\n25231\n25232\n25233\n25234\n25235\n25236\n25237\n25238\n25239\n25240\n25241\n25242\n25243\n25244\n25245\n25246\n25247\n25248\n25249\n25250\n25251\n25252\n25253\n25254\n25255\n25256\n25257\n25258\n25259\n25260\n25261\n25262\n25263\n25264\n25265\n25266\n25267\n25268\n25269\n25270\n25271\n25272\n25273\n25274\n25275\n25276\n25277\n25278\n25279\n25280\n25281\n25282\n25283\n25284\n25285\n25286\n25287\n25288\n25289\n25290\n25291\n25292\n25293\n25294\n25295\n25296\n25297\n25298\n25299\n25300\n25301\n25302\n25303\n25304\n25305\n25306\n25307\n25308\n25309\n25310\n25311\n25312\n25313\n25314\n25315\n25316\n25317\n25318\n25319\n25320\n25321\n25322\n25323\n25324\n25325\n25326\n25327\n25328\n25329\n25330\n25331\n25332\n25333\n25334\n25335\n25336\n25337\n25338\n25339\n25340\n25341\n25342\n25343\n25344\n25345\n25346\n25347\n25348\n25349\n25350\n25351\n25352\n25353\n25354\n25355\n25356\n25357\n25358\n25359\n25360\n25361\n25362\n25363\n25364\n25365\n25366\n25367\n25368\n25369\n25370\n25371\n25372\n25373\n25374\n25375\n25376\n25377\n25378\n25379\n25380\n25381\n25382\n25383\n25384\n25385\n25386\n25387\n25388\n25389\n25390\n25391\n25392\n25393\n25394\n25395\n25396\n25397\n25398\n25399\n25400\n25401\n25402\n25403\n25404\n25405\n25406\n25407\n25408\n25409\n25410\n25411\n25412\n25413\n25414\n25415\n25416\n25417\n25418\n25419\n25420\n25421\n25422\n25423\n25424\n25425\n25426\n25427\n25428\n25429\n25430\n25431\n25432\n25433\n25434\n25435\n25436\n25437\n25438\n25439\n25440\n25441\n25442\n25443\n25444\n25445\n25446\n25447\n25448\n25449\n25450\n25451\n25452\n25453\n25454\n25455\n25456\n25457\n25458\n25459\n25460\n25461\n25462\n25463\n25464\n25465\n25466\n25467\n25468\n25469\n25470\n25471\n25472\n25473\n25474\n25475\n25476\n25477\n25478\n25479\n25480\n25481\n25482\n25483\n25484\n25485\n25486\n25487\n25488\n25489\n25490\n25491\n25492\n25493\n25494\n25495\n25496\n25497\n25498\n25499\n25500\n25501\n25502\n25503\n25504\n25505\n25506\n25507\n25508\n25509\n25510\n25511\n25512\n25513\n25514\n25515\n25516\n25517\n25518\n25519\n25520\n25521\n25522\n25523\n25524\n25525\n25526\n25527\n25528\n25529\n25530\n25531\n25532\n25533\n25534\n25535\n25536\n25537\n25538\n25539\n25540\n25541\n25542\n25543\n25544\n25545\n25546\n25547\n25548\n25549\n25550\n25551\n25552\n25553\n25554\n25555\n25556\n25557\n25558\n25559\n25560\n25561\n25562\n25563\n25564\n25565\n25566\n25567\n25568\n25569\n25570\n25571\n25572\n25573\n25574\n25575\n25576\n25577\n25578\n25579\n25580\n25581\n25582\n25583\n25584\n25585\n25586\n25587\n25588\n25589\n25590\n25591\n25592\n25593\n25594\n25595\n25596\n25597\n25598\n25599\n25600\n25601\n25602\n25603\n25604\n25605\n25606\n25607\n25608\n25609\n25610\n25611\n25612\n25613\n25614\n25615\n25616\n25617\n25618\n25619\n25620\n25621\n25622\n25623\n25624\n25625\n25626\n25627\n25628\n25629\n25630\n25631\n25632\n25633\n25634\n25635\n25636\n25637\n25638\n25639\n25640\n25641\n25642\n25643\n25644\n25645\n25646\n25647\n25648\n25649\n25650\n25651\n25652\n25653\n25654\n25655\n25656\n25657\n25658\n25659\n25660\n25661\n25662\n25663\n25664\n25665\n25666\n25667\n25668\n25669\n25670\n25671\n25672\n25673\n25674\n25675\n25676\n25677\n25678\n25679\n25680\n25681\n25682\n25683\n25684\n25685\n25686\n25687\n25688\n25689\n25690\n25691\n25692\n25693\n25694\n25695\n25696\n25697\n25698\n25699\n25700\n25701\n25702\n25703\n25704\n25705\n25706\n25707\n25708\n25709\n25710\n25711\n25712\n25713\n25714\n25715\n25716\n25717\n25718\n25719\n25720\n25721\n25722\n25723\n25724\n25725\n25726\n25727\n25728\n25729\n25730\n25731\n25732\n25733\n25734\n25735\n25736\n25737\n25738\n25739\n25740\n25741\n25742\n25743\n25744\n25745\n25746\n25747\n25748\n25749\n25750\n25751\n25752\n25753\n25754\n25755\n25756\n25757\n25758\n25759\n25760\n25761\n25762\n25763\n25764\n25765\n25766\n25767\n25768\n25769\n25770\n25771\n25772\n25773\n25774\n25775\n25776\n25777\n25778\n25779\n25780\n25781\n25782\n25783\n25784\n25785\n25786\n25787\n25788\n25789\n25790\n25791\n25792\n25793\n25794\n25795\n25796\n25797\n25798\n25799\n25800\n25801\n25802\n25803\n25804\n25805\n25806\n25807\n25808\n25809\n25810\n25811\n25812\n25813\n25814\n25815\n25816\n25817\n25818\n25819\n25820\n25821\n25822\n25823\n25824\n25825\n25826\n25827\n25828\n25829\n25830\n25831\n25832\n25833\n25834\n25835\n25836\n25837\n25838\n25839\n25840\n25841\n25842\n25843\n25844\n25845\n25846\n25847\n25848\n25849\n25850\n25851\n25852\n25853\n25854\n25855\n25856\n25857\n25858\n25859\n25860\n25861\n25862\n25863\n25864\n25865\n25866\n25867\n25868\n25869\n25870\n25871\n25872\n25873\n25874\n25875\n25876\n25877\n25878\n25879\n25880\n25881\n25882\n25883\n25884\n25885\n25886\n25887\n25888\n25889\n25890\n25891\n25892\n25893\n25894\n25895\n25896\n25897\n25898\n25899\n25900\n25901\n25902\n25903\n25904\n25905\n25906\n25907\n25908\n25909\n25910\n25911\n25912\n25913\n25914\n25915\n25916\n25917\n25918\n25919\n25920\n25921\n25922\n25923\n25924\n25925\n25926\n25927\n25928\n25929\n25930\n25931\n25932\n25933\n25934\n25935\n25936\n25937\n25938\n25939\n25940\n25941\n25942\n25943\n25944\n25945\n25946\n25947\n25948\n25949\n25950\n25951\n25952\n25953\n25954\n25955\n25956\n25957\n25958\n25959\n25960\n25961\n25962\n25963\n25964\n25965\n25966\n25967\n25968\n25969\n25970\n25971\n25972\n25973\n25974\n25975\n25976\n25977\n25978\n25979\n25980\n25981\n25982\n25983\n25984\n25985\n25986\n25987\n25988\n25989\n25990\n25991\n25992\n25993\n25994\n25995\n25996\n25997\n25998\n25999\n26000\n26001\n26002\n26003\n26004\n26005\n26006\n26007\n26008\n26009\n26010\n26011\n26012\n26013\n26014\n26015\n26016\n26017\n26018\n26019\n26020\n26021\n26022\n26023\n26024\n26025\n26026\n26027\n26028\n26029\n26030\n26031\n26032\n26033\n26034\n26035\n26036\n26037\n26038\n26039\n26040\n26041\n26042\n26043\n26044\n26045\n26046\n26047\n26048\n26049\n26050\n26051\n26052\n26053\n26054\n26055\n26056\n26057\n26058\n26059\n26060\n26061\n26062\n26063\n26064\n26065\n26066\n26067\n26068\n26069\n26070\n26071\n26072\n26073\n26074\n26075\n26076\n26077\n26078\n26079\n26080\n26081\n26082\n26083\n26084\n26085\n26086\n26087\n26088\n26089\n26090\n26091\n26092\n26093\n26094\n26095\n26096\n26097\n26098\n26099\n26100\n26101\n26102\n26103\n26104\n26105\n26106\n26107\n26108\n26109\n26110\n26111\n26112\n26113\n26114\n26115\n26116\n26117\n26118\n26119\n26120\n26121\n26122\n26123\n26124\n26125\n26126\n26127\n26128\n26129\n26130\n26131\n26132\n26133\n26134\n26135\n26136\n26137\n26138\n26139\n26140\n26141\n26142\n26143\n26144\n26145\n26146\n26147\n26148\n26149\n26150\n26151\n26152\n26153\n26154\n26155\n26156\n26157\n26158\n26159\n26160\n26161\n26162\n26163\n26164\n26165\n26166\n26167\n26168\n26169\n26170\n26171\n26172\n26173\n26174\n26175\n26176\n26177\n26178\n26179\n26180\n26181\n26182\n26183\n26184\n26185\n26186\n26187\n26188\n26189\n26190\n26191\n26192\n26193\n26194\n26195\n26196\n26197\n26198\n26199\n26200\n26201\n26202\n26203\n26204\n26205\n26206\n26207\n26208\n26209\n26210\n26211\n26212\n26213\n26214\n26215\n26216\n26217\n26218\n26219\n26220\n26221\n26222\n26223\n26224\n26225\n26226\n26227\n26228\n26229\n26230\n26231\n26232\n26233\n26234\n26235\n26236\n26237\n26238\n26239\n26240\n26241\n26242\n26243\n26244\n26245\n26246\n26247\n26248\n26249\n26250\n26251\n26252\n26253\n26254\n26255\n26256\n26257\n26258\n26259\n26260\n26261\n26262\n26263\n26264\n26265\n26266\n26267\n26268\n26269\n26270\n26271\n26272\n26273\n26274\n26275\n26276\n26277\n26278\n26279\n26280\n26281\n26282\n26283\n26284\n26285\n26286\n26287\n26288\n26289\n26290\n26291\n26292\n26293\n26294\n26295\n26296\n26297\n26298\n26299\n26300\n26301\n26302\n26303\n26304\n26305\n26306\n26307\n26308\n26309\n26310\n26311\n26312\n26313\n26314\n26315\n26316\n26317\n26318\n26319\n26320\n26321\n26322\n26323\n26324\n26325\n26326\n26327\n26328\n26329\n26330\n26331\n26332\n26333\n26334\n26335\n26336\n26337\n26338\n26339\n26340\n26341\n26342\n26343\n26344\n26345\n26346\n26347\n26348\n26349\n26350\n26351\n26352\n26353\n26354\n26355\n26356\n26357\n26358\n26359\n26360\n26361\n26362\n26363\n26364\n26365\n26366\n26367\n26368\n26369\n26370\n26371\n26372\n26373\n26374\n26375\n26376\n26377\n26378\n26379\n26380\n26381\n26382\n26383\n26384\n26385\n26386\n26387\n26388\n26389\n26390\n26391\n26392\n26393\n26394\n26395\n26396\n26397\n26398\n26399\n26400\n26401\n26402\n26403\n26404\n26405\n26406\n26407\n26408\n26409\n26410\n26411\n26412\n26413\n26414\n26415\n26416\n26417\n26418\n26419\n26420\n26421\n26422\n26423\n26424\n26425\n26426\n26427\n26428\n26429\n26430\n26431\n26432\n26433\n26434\n26435\n26436\n26437\n26438\n26439\n26440\n26441\n26442\n26443\n26444\n26445\n26446\n26447\n26448\n26449\n26450\n26451\n26452\n26453\n26454\n26455\n26456\n26457\n26458\n26459\n26460\n26461\n26462\n26463\n26464\n26465\n26466\n26467\n26468\n26469\n26470\n26471\n26472\n26473\n26474\n26475\n26476\n26477\n26478\n26479\n26480\n26481\n26482\n26483\n26484\n26485\n26486\n26487\n26488\n26489\n26490\n26491\n26492\n26493\n26494\n26495\n26496\n26497\n26498\n26499\n26500\n26501\n26502\n26503\n26504\n26505\n26506\n26507\n26508\n26509\n26510\n26511\n26512\n26513\n26514\n26515\n26516\n26517\n26518\n26519\n26520\n26521\n26522\n26523\n26524\n26525\n26526\n26527\n26528\n26529\n26530\n26531\n26532\n26533\n26534\n26535\n26536\n26537\n26538\n26539\n26540\n26541\n26542\n26543\n26544\n26545\n26546\n26547\n26548\n26549\n26550\n26551\n26552\n26553\n26554\n26555\n26556\n26557\n26558\n26559\n26560\n26561\n26562\n26563\n26564\n26565\n26566\n26567\n26568\n26569\n26570\n26571\n26572\n26573\n26574\n26575\n26576\n26577\n26578\n26579\n26580\n26581\n26582\n26583\n26584\n26585\n26586\n26587\n26588\n26589\n26590\n26591\n26592\n26593\n26594\n26595\n26596\n26597\n26598\n26599\n26600\n26601\n26602\n26603\n26604\n26605\n26606\n26607\n26608\n26609\n26610\n26611\n26612\n26613\n26614\n26615\n26616\n26617\n26618\n26619\n26620\n26621\n26622\n26623\n26624\n26625\n26626\n26627\n26628\n26629\n26630\n26631\n26632\n26633\n26634\n26635\n26636\n26637\n26638\n26639\n26640\n26641\n26642\n26643\n26644\n26645\n26646\n26647\n26648\n26649\n26650\n26651\n26652\n26653\n26654\n26655\n26656\n26657\n26658\n26659\n26660\n26661\n26662\n26663\n26664\n26665\n26666\n26667\n26668\n26669\n26670\n26671\n26672\n26673\n26674\n26675\n26676\n26677\n26678\n26679\n26680\n26681\n26682\n26683\n26684\n26685\n26686\n26687\n26688\n26689\n26690\n26691\n26692\n26693\n26694\n26695\n26696\n26697\n26698\n26699\n26700\n26701\n26702\n26703\n26704\n26705\n26706\n26707\n26708\n26709\n26710\n26711\n26712\n26713\n26714\n26715\n26716\n26717\n26718\n26719\n26720\n26721\n26722\n26723\n26724\n26725\n26726\n26727\n26728\n26729\n26730\n26731\n26732\n26733\n26734\n26735\n26736\n26737\n26738\n26739\n26740\n26741\n26742\n26743\n26744\n26745\n26746\n26747\n26748\n26749\n26750\n26751\n26752\n26753\n26754\n26755\n26756\n26757\n26758\n26759\n26760\n26761\n26762\n26763\n26764\n26765\n26766\n26767\n26768\n26769\n26770\n26771\n26772\n26773\n26774\n26775\n26776\n26777\n26778\n26779\n26780\n26781\n26782\n26783\n26784\n26785\n26786\n26787\n26788\n26789\n26790\n26791\n26792\n26793\n26794\n26795\n26796\n26797\n26798\n26799\n26800\n26801\n26802\n26803\n26804\n26805\n26806\n26807\n26808\n26809\n26810\n26811\n26812\n26813\n26814\n26815\n26816\n26817\n26818\n26819\n26820\n26821\n26822\n26823\n26824\n26825\n26826\n26827\n26828\n26829\n26830\n26831\n26832\n26833\n26834\n26835\n26836\n26837\n26838\n26839\n26840\n26841\n26842\n26843\n26844\n26845\n26846\n26847\n26848\n26849\n26850\n26851\n26852\n26853\n26854\n26855\n26856\n26857\n26858\n26859\n26860\n26861\n26862\n26863\n26864\n26865\n26866\n26867\n26868\n26869\n26870\n26871\n26872\n26873\n26874\n26875\n26876\n26877\n26878\n26879\n26880\n26881\n26882\n26883\n26884\n26885\n26886\n26887\n26888\n26889\n26890\n26891\n26892\n26893\n26894\n26895\n26896\n26897\n26898\n26899\n26900\n26901\n26902\n26903\n26904\n26905\n26906\n26907\n26908\n26909\n26910\n26911\n26912\n26913\n26914\n26915\n26916\n26917\n26918\n26919\n26920\n26921\n26922\n26923\n26924\n26925\n26926\n26927\n26928\n26929\n26930\n26931\n26932\n26933\n26934\n26935\n26936\n26937\n26938\n26939\n26940\n26941\n26942\n26943\n26944\n26945\n26946\n26947\n26948\n26949\n26950\n26951\n26952\n26953\n26954\n26955\n26956\n26957\n26958\n26959\n26960\n26961\n26962\n26963\n26964\n26965\n26966\n26967\n26968\n26969\n26970\n26971\n26972\n26973\n26974\n26975\n26976\n26977\n26978\n26979\n26980\n26981\n26982\n26983\n26984\n26985\n26986\n26987\n26988\n26989\n26990\n26991\n26992\n26993\n26994\n26995\n26996\n26997\n26998\n26999\n27000\n27001\n27002\n27003\n27004\n27005\n27006\n27007\n27008\n27009\n27010\n27011\n27012\n27013\n27014\n27015\n27016\n27017\n27018\n27019\n27020\n27021\n27022\n27023\n27024\n27025\n27026\n27027\n27028\n27029\n27030\n27031\n27032\n27033\n27034\n27035\n27036\n27037\n27038\n27039\n27040\n27041\n27042\n27043\n27044\n27045\n27046\n27047\n27048\n27049\n27050\n27051\n27052\n27053\n27054\n27055\n27056\n27057\n27058\n27059\n27060\n27061\n27062\n27063\n27064\n27065\n27066\n27067\n27068\n27069\n27070\n27071\n27072\n27073\n27074\n27075\n27076\n27077\n27078\n27079\n27080\n27081\n27082\n27083\n27084\n27085\n27086\n27087\n27088\n27089\n27090\n27091\n27092\n27093\n27094\n27095\n27096\n27097\n27098\n27099\n27100\n27101\n27102\n27103\n27104\n27105\n27106\n27107\n27108\n27109\n27110\n27111\n27112\n27113\n27114\n27115\n27116\n27117\n27118\n27119\n27120\n27121\n27122\n27123\n27124\n27125\n27126\n27127\n27128\n27129\n27130\n27131\n27132\n27133\n27134\n27135\n27136\n27137\n27138\n27139\n27140\n27141\n27142\n27143\n27144\n27145\n27146\n27147\n27148\n27149\n27150\n27151\n27152\n27153\n27154\n27155\n27156\n27157\n27158\n27159\n27160\n27161\n27162\n27163\n27164\n27165\n27166\n27167\n27168\n27169\n27170\n27171\n27172\n27173\n27174\n27175\n27176\n27177\n27178\n27179\n27180\n27181\n27182\n27183\n27184\n27185\n27186\n27187\n27188\n27189\n27190\n27191\n27192\n27193\n27194\n27195\n27196\n27197\n27198\n27199\n27200\n27201\n27202\n27203\n27204\n27205\n27206\n27207\n27208\n27209\n27210\n27211\n27212\n27213\n27214\n27215\n27216\n27217\n27218\n27219\n27220\n27221\n27222\n27223\n27224\n27225\n27226\n27227\n27228\n27229\n27230\n27231\n27232\n27233\n27234\n27235\n27236\n27237\n27238\n27239\n27240\n27241\n27242\n27243\n27244\n27245\n27246\n27247\n27248\n27249\n27250\n27251\n27252\n27253\n27254\n27255\n27256\n27257\n27258\n27259\n27260\n27261\n27262\n27263\n27264\n27265\n27266\n27267\n27268\n27269\n27270\n27271\n27272\n27273\n27274\n27275\n27276\n27277\n27278\n27279\n27280\n27281\n27282\n27283\n27284\n27285\n27286\n27287\n27288\n27289\n27290\n27291\n27292\n27293\n27294\n27295\n27296\n27297\n27298\n27299\n27300\n27301\n27302\n27303\n27304\n27305\n27306\n27307\n27308\n27309\n27310\n27311\n27312\n27313\n27314\n27315\n27316\n27317\n27318\n27319\n27320\n27321\n27322\n27323\n27324\n27325\n27326\n27327\n27328\n27329\n27330\n27331\n27332\n27333\n27334\n27335\n27336\n27337\n27338\n27339\n27340\n27341\n27342\n27343\n27344\n27345\n27346\n27347\n27348\n27349\n27350\n27351\n27352\n27353\n27354\n27355\n27356\n27357\n27358\n27359\n27360\n27361\n27362\n27363\n27364\n27365\n27366\n27367\n27368\n27369\n27370\n27371\n27372\n27373\n27374\n27375\n27376\n27377\n27378\n27379\n27380\n27381\n27382\n27383\n27384\n27385\n27386\n27387\n27388\n27389\n27390\n27391\n27392\n27393\n27394\n27395\n27396\n27397\n27398\n27399\n27400\n27401\n27402\n27403\n27404\n27405\n27406\n27407\n27408\n27409\n27410\n27411\n27412\n27413\n27414\n27415\n27416\n27417\n27418\n27419\n27420\n27421\n27422\n27423\n27424\n27425\n27426\n27427\n27428\n27429\n27430\n27431\n27432\n27433\n27434\n27435\n27436\n27437\n27438\n27439\n27440\n27441\n27442\n27443\n27444\n27445\n27446\n27447\n27448\n27449\n27450\n27451\n27452\n27453\n27454\n27455\n27456\n27457\n27458\n27459\n27460\n27461\n27462\n27463\n27464\n27465\n27466\n27467\n27468\n27469\n27470\n27471\n27472\n27473\n27474\n27475\n27476\n27477\n27478\n27479\n27480\n27481\n27482\n27483\n27484\n27485\n27486\n27487\n27488\n27489\n27490\n27491\n27492\n27493\n27494\n27495\n27496\n27497\n27498\n27499\n27500\n27501\n27502\n27503\n27504\n27505\n27506\n27507\n27508\n27509\n27510\n27511\n27512\n27513\n27514\n27515\n27516\n27517\n27518\n27519\n27520\n27521\n27522\n27523\n27524\n27525\n27526\n27527\n27528\n27529\n27530\n27531\n27532\n27533\n27534\n27535\n27536\n27537\n27538\n27539\n27540\n27541\n27542\n27543\n27544\n27545\n27546\n27547\n27548\n27549\n27550\n27551\n27552\n27553\n27554\n27555\n27556\n27557\n27558\n27559\n27560\n27561\n27562\n27563\n27564\n27565\n27566\n27567\n27568\n27569\n27570\n27571\n27572\n27573\n27574\n27575\n27576\n27577\n27578\n27579\n27580\n27581\n27582\n27583\n27584\n27585\n27586\n27587\n27588\n27589\n27590\n27591\n27592\n27593\n27594\n27595\n27596\n27597\n27598\n27599\n27600\n27601\n27602\n27603\n27604\n27605\n27606\n27607\n27608\n27609\n27610\n27611\n27612\n27613\n27614\n27615\n27616\n27617\n27618\n27619\n27620\n27621\n27622\n27623\n27624\n27625\n27626\n27627\n27628\n27629\n27630\n27631\n27632\n27633\n27634\n27635\n27636\n27637\n27638\n27639\n27640\n27641\n27642\n27643\n27644\n27645\n27646\n27647\n27648\n27649\n27650\n27651\n27652\n27653\n27654\n27655\n27656\n27657\n27658\n27659\n27660\n27661\n27662\n27663\n27664\n27665\n27666\n27667\n27668\n27669\n27670\n27671\n27672\n27673\n27674\n27675\n27676\n27677\n27678\n27679\n27680\n27681\n27682\n27683\n27684\n27685\n27686\n27687\n27688\n27689\n27690\n27691\n27692\n27693\n27694\n27695\n27696\n27697\n27698\n27699\n27700\n27701\n27702\n27703\n27704\n27705\n27706\n27707\n27708\n27709\n27710\n27711\n27712\n27713\n27714\n27715\n27716\n27717\n27718\n27719\n27720\n27721\n27722\n27723\n27724\n27725\n27726\n27727\n27728\n27729\n27730\n27731\n27732\n27733\n27734\n27735\n27736\n27737\n27738\n27739\n27740\n27741\n27742\n27743\n27744\n27745\n27746\n27747\n27748\n27749\n27750\n27751\n27752\n27753\n27754\n27755\n27756\n27757\n27758\n27759\n27760\n27761\n27762\n27763\n27764\n27765\n27766\n27767\n27768\n27769\n27770\n27771\n27772\n27773\n27774\n27775\n27776\n27777\n27778\n27779\n27780\n27781\n27782\n27783\n27784\n27785\n27786\n27787\n27788\n27789\n27790\n27791\n27792\n27793\n27794\n27795\n27796\n27797\n27798\n27799\n27800\n27801\n27802\n27803\n27804\n27805\n27806\n27807\n27808\n27809\n27810\n27811\n27812\n27813\n27814\n27815\n27816\n27817\n27818\n27819\n27820\n27821\n27822\n27823\n27824\n27825\n27826\n27827\n27828\n27829\n27830\n27831\n27832\n27833\n27834\n27835\n27836\n27837\n27838\n27839\n27840\n27841\n27842\n27843\n27844\n27845\n27846\n27847\n27848\n27849\n27850\n27851\n27852\n27853\n27854\n27855\n27856\n27857\n27858\n27859\n27860\n27861\n27862\n27863\n27864\n27865\n27866\n27867\n27868\n27869\n27870\n27871\n27872\n27873\n27874\n27875\n27876\n27877\n27878\n27879\n27880\n27881\n27882\n27883\n27884\n27885\n27886\n27887\n27888\n27889\n27890\n27891\n27892\n27893\n27894\n27895\n27896\n27897\n27898\n27899\n27900\n27901\n27902\n27903\n27904\n27905\n27906\n27907\n27908\n27909\n27910\n27911\n27912\n27913\n27914\n27915\n27916\n27917\n27918\n27919\n27920\n27921\n27922\n27923\n27924\n27925\n27926\n27927\n27928\n27929\n27930\n27931\n27932\n27933\n27934\n27935\n27936\n27937\n27938\n27939\n27940\n27941\n27942\n27943\n27944\n27945\n27946\n27947\n27948\n27949\n27950\n27951\n27952\n27953\n27954\n27955\n27956\n27957\n27958\n27959\n27960\n27961\n27962\n27963\n27964\n27965\n27966\n27967\n27968\n27969\n27970\n27971\n27972\n27973\n27974\n27975\n27976\n27977\n27978\n27979\n27980\n27981\n27982\n27983\n27984\n27985\n27986\n27987\n27988\n27989\n27990\n27991\n27992\n27993\n27994\n27995\n27996\n27997\n27998\n27999\n28000\n28001\n28002\n28003\n28004\n28005\n28006\n28007\n28008\n28009\n28010\n28011\n28012\n28013\n28014\n28015\n28016\n28017\n28018\n28019\n28020\n28021\n28022\n28023\n28024\n28025\n28026\n28027\n28028\n28029\n28030\n28031\n28032\n28033\n28034\n28035\n28036\n28037\n28038\n28039\n28040\n28041\n28042\n28043\n28044\n28045\n28046\n28047\n28048\n28049\n28050\n28051\n28052\n28053\n28054\n28055\n28056\n28057\n28058\n28059\n28060\n28061\n28062\n28063\n28064\n28065\n28066\n28067\n28068\n28069\n28070\n28071\n28072\n28073\n28074\n28075\n28076\n28077\n28078\n28079\n28080\n28081\n28082\n28083\n28084\n28085\n28086\n28087\n28088\n28089\n28090\n28091\n28092\n28093\n28094\n28095\n28096\n28097\n28098\n28099\n28100\n28101\n28102\n28103\n28104\n28105\n28106\n28107\n28108\n28109\n28110\n28111\n28112\n28113\n28114\n28115\n28116\n28117\n28118\n28119\n28120\n28121\n28122\n28123\n28124\n28125\n28126\n28127\n28128\n28129\n28130\n28131\n28132\n28133\n28134\n28135\n28136\n28137\n28138\n28139\n28140\n28141\n28142\n28143\n28144\n28145\n28146\n28147\n28148\n28149\n28150\n28151\n28152\n28153\n28154\n28155\n28156\n28157\n28158\n28159\n28160\n28161\n28162\n28163\n28164\n28165\n28166\n28167\n28168\n28169\n28170\n28171\n28172\n28173\n28174\n28175\n28176\n28177\n28178\n28179\n28180\n28181\n28182\n28183\n28184\n28185\n28186\n28187\n28188\n28189\n28190\n28191\n28192\n28193\n28194\n28195\n28196\n28197\n28198\n28199\n28200\n28201\n28202\n28203\n28204\n28205\n28206\n28207\n28208\n28209\n28210\n28211\n28212\n28213\n28214\n28215\n28216\n28217\n28218\n28219\n28220\n28221\n28222\n28223\n28224\n28225\n28226\n28227\n28228\n28229\n28230\n28231\n28232\n28233\n28234\n28235\n28236\n28237\n28238\n28239\n28240\n28241\n28242\n28243\n28244\n28245\n28246\n28247\n28248\n28249\n28250\n28251\n28252\n28253\n28254\n28255\n28256\n28257\n28258\n28259\n28260\n28261\n28262\n28263\n28264\n28265\n28266\n28267\n28268\n28269\n28270\n28271\n28272\n28273\n28274\n28275\n28276\n28277\n28278\n28279\n28280\n28281\n28282\n28283\n28284\n28285\n28286\n28287\n28288\n28289\n28290\n28291\n28292\n28293\n28294\n28295\n28296\n28297\n28298\n28299\n28300\n28301\n28302\n28303\n28304\n28305\n28306\n28307\n28308\n28309\n28310\n28311\n28312\n28313\n28314\n28315\n28316\n28317\n28318\n28319\n28320\n28321\n28322\n28323\n28324\n28325\n28326\n28327\n28328\n28329\n28330\n28331\n28332\n28333\n28334\n28335\n28336\n28337\n28338\n28339\n28340\n28341\n28342\n28343\n28344\n28345\n28346\n28347\n28348\n28349\n28350\n28351\n28352\n28353\n28354\n28355\n28356\n28357\n28358\n28359\n28360\n28361\n28362\n28363\n28364\n28365\n28366\n28367\n28368\n28369\n28370\n28371\n28372\n28373\n28374\n28375\n28376\n28377\n28378\n28379\n28380\n28381\n28382\n28383\n28384\n28385\n28386\n28387\n28388\n28389\n28390\n28391\n28392\n28393\n28394\n28395\n28396\n28397\n28398\n28399\n28400\n28401\n28402\n28403\n28404\n28405\n28406\n28407\n28408\n28409\n28410\n28411\n28412\n28413\n28414\n28415\n28416\n28417\n28418\n28419\n28420\n28421\n28422\n28423\n28424\n28425\n28426\n28427\n28428\n28429\n28430\n28431\n28432\n28433\n28434\n28435\n28436\n28437\n28438\n28439\n28440\n28441\n28442\n28443\n28444\n28445\n28446\n28447\n28448\n28449\n28450\n28451\n28452\n28453\n28454\n28455\n28456\n28457\n28458\n28459\n28460\n28461\n28462\n28463\n28464\n28465\n28466\n28467\n28468\n28469\n28470\n28471\n28472\n28473\n28474\n28475\n28476\n28477\n28478\n28479\n28480\n28481\n28482\n28483\n28484\n28485\n28486\n28487\n28488\n28489\n28490\n28491\n28492\n28493\n28494\n28495\n28496\n28497\n28498\n28499\n28500\n28501\n28502\n28503\n28504\n28505\n28506\n28507\n28508\n28509\n28510\n28511\n28512\n28513\n28514\n28515\n28516\n28517\n28518\n28519\n28520\n28521\n28522\n28523\n28524\n28525\n28526\n28527\n28528\n28529\n28530\n28531\n28532\n28533\n28534\n28535\n28536\n28537\n28538\n28539\n28540\n28541\n28542\n28543\n28544\n28545\n28546\n28547\n28548\n28549\n28550\n28551\n28552\n28553\n28554\n28555\n28556\n28557\n28558\n28559\n28560\n28561\n28562\n28563\n28564\n28565\n28566\n28567\n28568\n28569\n28570\n28571\n28572\n28573\n28574\n28575\n28576\n28577\n28578\n28579\n28580\n28581\n28582\n28583\n28584\n28585\n28586\n28587\n28588\n28589\n28590\n28591\n28592\n28593\n28594\n28595\n28596\n28597\n28598\n28599\n28600\n28601\n28602\n28603\n28604\n28605\n28606\n28607\n28608\n28609\n28610\n28611\n28612\n28613\n28614\n28615\n28616\n28617\n28618\n28619\n28620\n28621\n28622\n28623\n28624\n28625\n28626\n28627\n28628\n28629\n28630\n28631\n28632\n28633\n28634\n28635\n28636\n28637\n28638\n28639\n28640\n28641\n28642\n28643\n28644\n28645\n28646\n28647\n28648\n28649\n28650\n28651\n28652\n28653\n28654\n28655\n28656\n28657\n28658\n28659\n28660\n28661\n28662\n28663\n28664\n28665\n28666\n28667\n28668\n28669\n28670\n28671\n28672\n28673\n28674\n28675\n28676\n28677\n28678\n28679\n28680\n28681\n28682\n28683\n28684\n28685\n28686\n28687\n28688\n28689\n28690\n28691\n28692\n28693\n28694\n28695\n28696\n28697\n28698\n28699\n28700\n28701\n28702\n28703\n28704\n28705\n28706\n28707\n28708\n28709\n28710\n28711\n28712\n28713\n28714\n28715\n28716\n28717\n28718\n28719\n28720\n28721\n28722\n28723\n28724\n28725\n28726\n28727\n28728\n28729\n28730\n28731\n28732\n28733\n28734\n28735\n28736\n28737\n28738\n28739\n28740\n28741\n28742\n28743\n28744\n28745\n28746\n28747\n28748\n28749\n28750\n28751\n28752\n28753\n28754\n28755\n28756\n28757\n28758\n28759\n28760\n28761\n28762\n28763\n28764\n28765\n28766\n28767\n28768\n28769\n28770\n28771\n28772\n28773\n28774\n28775\n28776\n28777\n28778\n28779\n28780\n28781\n28782\n28783\n28784\n28785\n28786\n28787\n28788\n28789\n28790\n28791\n28792\n28793\n28794\n28795\n28796\n28797\n28798\n28799\n28800\n28801\n28802\n28803\n28804\n28805\n28806\n28807\n28808\n28809\n28810\n28811\n28812\n28813\n28814\n28815\n28816\n28817\n28818\n28819\n28820\n28821\n28822\n28823\n28824\n28825\n28826\n28827\n28828\n28829\n28830\n28831\n28832\n28833\n28834\n28835\n28836\n28837\n28838\n28839\n28840\n28841\n28842\n28843\n28844\n28845\n28846\n28847\n28848\n28849\n28850\n28851\n28852\n28853\n28854\n28855\n28856\n28857\n28858\n28859\n28860\n28861\n28862\n28863\n28864\n28865\n28866\n28867\n28868\n28869\n28870\n28871\n28872\n28873\n28874\n28875\n28876\n28877\n28878\n28879\n28880\n28881\n28882\n28883\n28884\n28885\n28886\n28887\n28888\n28889\n28890\n28891\n28892\n28893\n28894\n28895\n28896\n28897\n28898\n28899\n28900\n28901\n28902\n28903\n28904\n28905\n28906\n28907\n28908\n28909\n28910\n28911\n28912\n28913\n28914\n28915\n28916\n28917\n28918\n28919\n28920\n28921\n28922\n28923\n28924\n28925\n28926\n28927\n28928\n28929\n28930\n28931\n28932\n28933\n28934\n28935\n28936\n28937\n28938\n28939\n28940\n28941\n28942\n28943\n28944\n28945\n28946\n28947\n28948\n28949\n28950\n28951\n28952\n28953\n28954\n28955\n28956\n28957\n28958\n28959\n28960\n28961\n28962\n28963\n28964\n28965\n28966\n28967\n28968\n28969\n28970\n28971\n28972\n28973\n28974\n28975\n28976\n28977\n28978\n28979\n28980\n28981\n28982\n28983\n28984\n28985\n28986\n28987\n28988\n28989\n28990\n28991\n28992\n28993\n28994\n28995\n28996\n28997\n28998\n28999\n29000\n29001\n29002\n29003\n29004\n29005\n29006\n29007\n29008\n29009\n29010\n29011\n29012\n29013\n29014\n29015\n29016\n29017\n29018\n29019\n29020\n29021\n29022\n29023\n29024\n29025\n29026\n29027\n29028\n29029\n29030\n29031\n29032\n29033\n29034\n29035\n29036\n29037\n29038\n29039\n29040\n29041\n29042\n29043\n29044\n29045\n29046\n29047\n29048\n29049\n29050\n29051\n29052\n29053\n29054\n29055\n29056\n29057\n29058\n29059\n29060\n29061\n29062\n29063\n29064\n29065\n29066\n29067\n29068\n29069\n29070\n29071\n29072\n29073\n29074\n29075\n29076\n29077\n29078\n29079\n29080\n29081\n29082\n29083\n29084\n29085\n29086\n29087\n29088\n29089\n29090\n29091\n29092\n29093\n29094\n29095\n29096\n29097\n29098\n29099\n29100\n29101\n29102\n29103\n29104\n29105\n29106\n29107\n29108\n29109\n29110\n29111\n29112\n29113\n29114\n29115\n29116\n29117\n29118\n29119\n29120\n29121\n29122\n29123\n29124\n29125\n29126\n29127\n29128\n29129\n29130\n29131\n29132\n29133\n29134\n29135\n29136\n29137\n29138\n29139\n29140\n29141\n29142\n29143\n29144\n29145\n29146\n29147\n29148\n29149\n29150\n29151\n29152\n29153\n29154\n29155\n29156\n29157\n29158\n29159\n29160\n29161\n29162\n29163\n29164\n29165\n29166\n29167\n29168\n29169\n29170\n29171\n29172\n29173\n29174\n29175\n29176\n29177\n29178\n29179\n29180\n29181\n29182\n29183\n29184\n29185\n29186\n29187\n29188\n29189\n29190\n29191\n29192\n29193\n29194\n29195\n29196\n29197\n29198\n29199\n29200\n29201\n29202\n29203\n29204\n29205\n29206\n29207\n29208\n29209\n29210\n29211\n29212\n29213\n29214\n29215\n29216\n29217\n29218\n29219\n29220\n29221\n29222\n29223\n29224\n29225\n29226\n29227\n29228\n29229\n29230\n29231\n29232\n29233\n29234\n29235\n29236\n29237\n29238\n29239\n29240\n29241\n29242\n29243\n29244\n29245\n29246\n29247\n29248\n29249\n29250\n29251\n29252\n29253\n29254\n29255\n29256\n29257\n29258\n29259\n29260\n29261\n29262\n29263\n29264\n29265\n29266\n29267\n29268\n29269\n29270\n29271\n29272\n29273\n29274\n29275\n29276\n29277\n29278\n29279\n29280\n29281\n29282\n29283\n29284\n29285\n29286\n29287\n29288\n29289\n29290\n29291\n29292\n29293\n29294\n29295\n29296\n29297\n29298\n29299\n29300\n29301\n29302\n29303\n29304\n29305\n29306\n29307\n29308\n29309\n29310\n29311\n29312\n29313\n29314\n29315\n29316\n29317\n29318\n29319\n29320\n29321\n29322\n29323\n29324\n29325\n29326\n29327\n29328\n29329\n29330\n29331\n29332\n29333\n29334\n29335\n29336\n29337\n29338\n29339\n29340\n29341\n29342\n29343\n29344\n29345\n29346\n29347\n29348\n29349\n29350\n29351\n29352\n29353\n29354\n29355\n29356\n29357\n29358\n29359\n29360\n29361\n29362\n29363\n29364\n29365\n29366\n29367\n29368\n29369\n29370\n29371\n29372\n29373\n29374\n29375\n29376\n29377\n29378\n29379\n29380\n29381\n29382\n29383\n29384\n29385\n29386\n29387\n29388\n29389\n29390\n29391\n29392\n29393\n29394\n29395\n29396\n29397\n29398\n29399\n29400\n29401\n29402\n29403\n29404\n29405\n29406\n29407\n29408\n29409\n29410\n29411\n29412\n29413\n29414\n29415\n29416\n29417\n29418\n29419\n29420\n29421\n29422\n29423\n29424\n29425\n29426\n29427\n29428\n29429\n29430\n29431\n29432\n29433\n29434\n29435\n29436\n29437\n29438\n29439\n29440\n29441\n29442\n29443\n29444\n29445\n29446\n29447\n29448\n29449\n29450\n29451\n29452\n29453\n29454\n29455\n29456\n29457\n29458\n29459\n29460\n29461\n29462\n29463\n29464\n29465\n29466\n29467\n29468\n29469\n29470\n29471\n29472\n29473\n29474\n29475\n29476\n29477\n29478\n29479\n29480\n29481\n29482\n29483\n29484\n29485\n29486\n29487\n29488\n29489\n29490\n29491\n29492\n29493\n29494\n29495\n29496\n29497\n29498\n29499\n29500\n29501\n29502\n29503\n29504\n29505\n29506\n29507\n29508\n29509\n29510\n29511\n29512\n29513\n29514\n29515\n29516\n29517\n29518\n29519\n29520\n29521\n29522\n29523\n29524\n29525\n29526\n29527\n29528\n29529\n29530\n29531\n29532\n29533\n29534\n29535\n29536\n29537\n29538\n29539\n29540\n29541\n29542\n29543\n29544\n29545\n29546\n29547\n29548\n29549\n29550\n29551\n29552\n29553\n29554\n29555\n29556\n29557\n29558\n29559\n29560\n29561\n29562\n29563\n29564\n29565\n29566\n29567\n29568\n29569\n29570\n29571\n29572\n29573\n29574\n29575\n29576\n29577\n29578\n29579\n29580\n29581\n29582\n29583\n29584\n29585\n29586\n29587\n29588\n29589\n29590\n29591\n29592\n29593\n29594\n29595\n29596\n29597\n29598\n29599\n29600\n29601\n29602\n29603\n29604\n29605\n29606\n29607\n29608\n29609\n29610\n29611\n29612\n29613\n29614\n29615\n29616\n29617\n29618\n29619\n29620\n29621\n29622\n29623\n29624\n29625\n29626\n29627\n29628\n29629\n29630\n29631\n29632\n29633\n29634\n29635\n29636\n29637\n29638\n29639\n29640\n29641\n29642\n29643\n29644\n29645\n29646\n29647\n29648\n29649\n29650\n29651\n29652\n29653\n29654\n29655\n29656\n29657\n29658\n29659\n29660\n29661\n29662\n29663\n29664\n29665\n29666\n29667\n29668\n29669\n29670\n29671\n29672\n29673\n29674\n29675\n29676\n29677\n29678\n29679\n29680\n29681\n29682\n29683\n29684\n29685\n29686\n29687\n29688\n29689\n29690\n29691\n29692\n29693\n29694\n29695\n29696\n29697\n29698\n29699\n29700\n29701\n29702\n29703\n29704\n29705\n29706\n29707\n29708\n29709\n29710\n29711\n29712\n29713\n29714\n29715\n29716\n29717\n29718\n29719\n29720\n29721\n29722\n29723\n29724\n29725\n29726\n29727\n29728\n29729\n29730\n29731\n29732\n29733\n29734\n29735\n29736\n29737\n29738\n29739\n29740\n29741\n29742\n29743\n29744\n29745\n29746\n29747\n29748\n29749\n29750\n29751\n29752\n29753\n29754\n29755\n29756\n29757\n29758\n29759\n29760\n29761\n29762\n29763\n29764\n29765\n29766\n29767\n29768\n29769\n29770\n29771\n29772\n29773\n29774\n29775\n29776\n29777\n29778\n29779\n29780\n29781\n29782\n29783\n29784\n29785\n29786\n29787\n29788\n29789\n29790\n29791\n29792\n29793\n29794\n29795\n29796\n29797\n29798\n29799\n29800\n29801\n29802\n29803\n29804\n29805\n29806\n29807\n29808\n29809\n29810\n29811\n29812\n29813\n29814\n29815\n29816\n29817\n29818\n29819\n29820\n29821\n29822\n29823\n29824\n29825\n29826\n29827\n29828\n29829\n29830\n29831\n29832\n29833\n29834\n29835\n29836\n29837\n29838\n29839\n29840\n29841\n29842\n29843\n29844\n29845\n29846\n29847\n29848\n29849\n29850\n29851\n29852\n29853\n29854\n29855\n29856\n29857\n29858\n29859\n29860\n29861\n29862\n29863\n29864\n29865\n29866\n29867\n29868\n29869\n29870\n29871\n29872\n29873\n29874\n29875\n29876\n29877\n29878\n29879\n29880\n29881\n29882\n29883\n29884\n29885\n29886\n29887\n29888\n29889\n29890\n29891\n29892\n29893\n29894\n29895\n29896\n29897\n29898\n29899\n29900\n29901\n29902\n29903\n29904\n29905\n29906\n29907\n29908\n29909\n29910\n29911\n29912\n29913\n29914\n29915\n29916\n29917\n29918\n29919\n29920\n29921\n29922\n29923\n29924\n29925\n29926\n29927\n29928\n29929\n29930\n29931\n29932\n29933\n29934\n29935\n29936\n29937\n29938\n29939\n29940\n29941\n29942\n29943\n29944\n29945\n29946\n29947\n29948\n29949\n29950\n29951\n29952\n29953\n29954\n29955\n29956\n29957\n29958\n29959\n29960\n29961\n29962\n29963\n29964\n29965\n29966\n29967\n29968\n29969\n29970\n29971\n29972\n29973\n29974\n29975\n29976\n29977\n29978\n29979\n29980\n29981\n29982\n29983\n29984\n29985\n29986\n29987\n29988\n29989\n29990\n29991\n29992\n29993\n29994\n29995\n29996\n29997\n29998\n29999' \ No newline at end of file diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test11.arff b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test11.arff new file mode 100644 index 0000000000000000000000000000000000000000..fadfaee884e3e91cd59f691afd954a6a6d4042da --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test11.arff @@ -0,0 +1,11 @@ +@RELATION test11 + +@ATTRIBUTE attr0 REAL +@ATTRIBUTE attr1 REAL +@ATTRIBUTE attr2 REAL +@ATTRIBUTE attr3 REAL +@ATTRIBUTE class { class0, class1, class2, class3 } +@DATA +0.1, 0.2, 0.3, 0.4,class1 +-0.1, -0.2, -0.3, -0.4,class2 +1, 2, 3, 4,class3 diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test2.arff b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test2.arff new file mode 100644 index 0000000000000000000000000000000000000000..30f0dbf91b078ef670868d5e7321f956a6a7a506 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test2.arff @@ -0,0 +1,15 @@ +@RELATION test2 + +@ATTRIBUTE attr0 REAL +@ATTRIBUTE attr1 real +@ATTRIBUTE attr2 integer +@ATTRIBUTE attr3 Integer +@ATTRIBUTE attr4 Numeric +@ATTRIBUTE attr5 numeric +@ATTRIBUTE attr6 string +@ATTRIBUTE attr7 STRING +@ATTRIBUTE attr8 {bla} +@ATTRIBUTE attr9 {bla, bla} + +@DATA +0.1, 0.2, 0.3, 0.4,class1 diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test3.arff b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test3.arff new file mode 100644 index 0000000000000000000000000000000000000000..23da3b30967fcc95d70883f70be9ef6e39d577fa --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test3.arff @@ -0,0 +1,6 @@ +@RELATION test3 + +@ATTRIBUTE attr0 crap + +@DATA +0.1, 0.2, 0.3, 0.4,class1 diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test4.arff b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test4.arff new file mode 100644 index 0000000000000000000000000000000000000000..bf5f99ca89375fbd980185fd25711901f23ff844 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test4.arff @@ -0,0 +1,11 @@ +@RELATION test5 + +@ATTRIBUTE attr0 REAL +@ATTRIBUTE attr1 REAL +@ATTRIBUTE attr2 REAL +@ATTRIBUTE attr3 REAL +@ATTRIBUTE class {class0, class1, class2, class3} +@DATA +0.1, 0.2, 0.3, 0.4,class1 +-0.1, -0.2, -0.3, -0.4,class2 +1, 2, 3, 4,class3 diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test5.arff b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test5.arff new file mode 100644 index 0000000000000000000000000000000000000000..0075daf05e7792e80dcd565e791ce40e4dd49e85 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test5.arff @@ -0,0 +1,26 @@ +@RELATION test4 + +@ATTRIBUTE attr0 REAL +@ATTRIBUTE attr1 REAL +@ATTRIBUTE attr2 REAL +@ATTRIBUTE attr3 REAL +@ATTRIBUTE class {class0, class1, class2, class3} + +@DATA + +% lsdflkjhaksjdhf + +% lsdflkjhaksjdhf + +0.1, 0.2, 0.3, 0.4,class1 +% laksjdhf + +% lsdflkjhaksjdhf +-0.1, -0.2, -0.3, -0.4,class2 + +% lsdflkjhaksjdhf +% lsdflkjhaksjdhf + +% lsdflkjhaksjdhf + +1, 2, 3, 4,class3 diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test6.arff b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test6.arff new file mode 100644 index 0000000000000000000000000000000000000000..b63280b03aef8e0553a83fbf96692d280a3f86b7 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test6.arff @@ -0,0 +1,12 @@ +@RELATION test6 + +@ATTRIBUTE attr0 REAL +@ATTRIBUTE attr1 REAL +@ATTRIBUTE attr2 REAL +@ATTRIBUTE attr3 REAL +@ATTRIBUTE class {C} + +@DATA +0.1, 0.2, 0.3, 0.4,C +-0.1, -0.2, -0.3, -0.4,C +1, 2, 3, 4,C diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test7.arff b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test7.arff new file mode 100644 index 0000000000000000000000000000000000000000..38ef6c9a7a10afb10caa5913687ea3636ab1d38e --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test7.arff @@ -0,0 +1,15 @@ +@RELATION test7 + +@ATTRIBUTE attr_year DATE yyyy +@ATTRIBUTE attr_month DATE yyyy-MM +@ATTRIBUTE attr_date DATE yyyy-MM-dd +@ATTRIBUTE attr_datetime_local DATE "yyyy-MM-dd HH:mm" +@ATTRIBUTE attr_datetime_missing DATE "yyyy-MM-dd HH:mm" + +@DATA +1999,1999-01,1999-01-31,"1999-01-31 00:01",? +2004,2004-12,2004-12-01,"2004-12-01 23:59","2004-12-01 23:59" +1817,1817-04,1817-04-28,"1817-04-28 13:00",? +2100,2100-09,2100-09-10,"2100-09-10 12:00",? +2013,2013-11,2013-11-30,"2013-11-30 04:55","2013-11-30 04:55" +1631,1631-10,1631-10-15,"1631-10-15 20:04","1631-10-15 20:04" \ No newline at end of file diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test8.arff b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test8.arff new file mode 100644 index 0000000000000000000000000000000000000000..776deb4c9e7550eafdb26d16826f5651da37ef12 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test8.arff @@ -0,0 +1,12 @@ +@RELATION test8 + +@ATTRIBUTE attr_datetime_utc DATE "yyyy-MM-dd HH:mm Z" +@ATTRIBUTE attr_datetime_full DATE "yy-MM-dd HH:mm:ss z" + +@DATA +"1999-01-31 00:01 UTC","99-01-31 00:01:08 +0430" +"2004-12-01 23:59 UTC","04-12-01 23:59:59 -0800" +"1817-04-28 13:00 UTC","17-04-28 13:00:33 +1000" +"2100-09-10 12:00 UTC","21-09-10 12:00:21 -0300" +"2013-11-30 04:55 UTC","13-11-30 04:55:48 -1100" +"1631-10-15 20:04 UTC","31-10-15 20:04:10 +0000" \ No newline at end of file diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test9.arff b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test9.arff new file mode 100644 index 0000000000000000000000000000000000000000..b3f97e32a3fd4909a3f9cbf8d5d2e8d250f8dbad --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/data/test9.arff @@ -0,0 +1,14 @@ +@RELATION test9 + +@ATTRIBUTE attr_date_number RELATIONAL + @ATTRIBUTE attr_date DATE "yyyy-MM-dd" + @ATTRIBUTE attr_number INTEGER +@END attr_date_number + +@DATA +"1999-01-31 1\n1935-11-27 10" +"2004-12-01 2\n1942-08-13 20" +"1817-04-28 3" +"2100-09-10 4\n1957-04-17 40\n1721-01-14 400" +"2013-11-30 5" +"1631-10-15 6" \ No newline at end of file diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/test_arffread.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/test_arffread.py new file mode 100644 index 0000000000000000000000000000000000000000..d13ebe6dd1af3044794b28f5375d06ed60787966 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/arff/tests/test_arffread.py @@ -0,0 +1,421 @@ +import datetime +import os +import sys +from os.path import join as pjoin + +from io import StringIO + +import numpy as np + +from numpy.testing import (assert_array_almost_equal, + assert_array_equal, assert_equal, assert_) +from pytest import raises as assert_raises + +from scipy.io.arff import loadarff +from scipy.io.arff._arffread import read_header, ParseArffError + + +data_path = pjoin(os.path.dirname(__file__), 'data') + +test1 = pjoin(data_path, 'test1.arff') +test2 = pjoin(data_path, 'test2.arff') +test3 = pjoin(data_path, 'test3.arff') + +test4 = pjoin(data_path, 'test4.arff') +test5 = pjoin(data_path, 'test5.arff') +test6 = pjoin(data_path, 'test6.arff') +test7 = pjoin(data_path, 'test7.arff') +test8 = pjoin(data_path, 'test8.arff') +test9 = pjoin(data_path, 'test9.arff') +test10 = pjoin(data_path, 'test10.arff') +test11 = pjoin(data_path, 'test11.arff') +test_quoted_nominal = pjoin(data_path, 'quoted_nominal.arff') +test_quoted_nominal_spaces = pjoin(data_path, 'quoted_nominal_spaces.arff') + +expect4_data = [(0.1, 0.2, 0.3, 0.4, 'class1'), + (-0.1, -0.2, -0.3, -0.4, 'class2'), + (1, 2, 3, 4, 'class3')] +expected_types = ['numeric', 'numeric', 'numeric', 'numeric', 'nominal'] + +missing = pjoin(data_path, 'missing.arff') +expect_missing_raw = np.array([[1, 5], [2, 4], [np.nan, np.nan]]) +expect_missing = np.empty(3, [('yop', float), ('yap', float)]) +expect_missing['yop'] = expect_missing_raw[:, 0] # type: ignore[call-overload] +expect_missing['yap'] = expect_missing_raw[:, 1] # type: ignore[call-overload] + + +class TestData: + def test1(self): + # Parsing trivial file with nothing. + self._test(test4) + + def test2(self): + # Parsing trivial file with some comments in the data section. + self._test(test5) + + def test3(self): + # Parsing trivial file with nominal attribute of 1 character. + self._test(test6) + + def test4(self): + # Parsing trivial file with trailing spaces in attribute declaration. + self._test(test11) + + def _test(self, test_file): + data, meta = loadarff(test_file) + for i in range(len(data)): + for j in range(4): + assert_array_almost_equal(expect4_data[i][j], data[i][j]) + assert_equal(meta.types(), expected_types) + + def test_filelike(self): + # Test reading from file-like object (StringIO) + with open(test1) as f1: + data1, meta1 = loadarff(f1) + with open(test1) as f2: + data2, meta2 = loadarff(StringIO(f2.read())) + assert_(data1 == data2) + assert_(repr(meta1) == repr(meta2)) + + def test_path(self): + # Test reading from `pathlib.Path` object + from pathlib import Path + + with open(test1) as f1: + data1, meta1 = loadarff(f1) + + data2, meta2 = loadarff(Path(test1)) + + assert_(data1 == data2) + assert_(repr(meta1) == repr(meta2)) + + +class TestMissingData: + def test_missing(self): + data, meta = loadarff(missing) + for i in ['yop', 'yap']: + assert_array_almost_equal(data[i], expect_missing[i]) + + +class TestNoData: + def test_nodata(self): + # The file nodata.arff has no data in the @DATA section. + # Reading it should result in an array with length 0. + nodata_filename = os.path.join(data_path, 'nodata.arff') + data, meta = loadarff(nodata_filename) + if sys.byteorder == 'big': + end = '>' + else: + end = '<' + expected_dtype = np.dtype([('sepallength', f'{end}f8'), + ('sepalwidth', f'{end}f8'), + ('petallength', f'{end}f8'), + ('petalwidth', f'{end}f8'), + ('class', 'S15')]) + assert_equal(data.dtype, expected_dtype) + assert_equal(data.size, 0) + + +class TestHeader: + def test_type_parsing(self): + # Test parsing type of attribute from their value. + with open(test2) as ofile: + rel, attrs = read_header(ofile) + + expected = ['numeric', 'numeric', 'numeric', 'numeric', 'numeric', + 'numeric', 'string', 'string', 'nominal', 'nominal'] + + for i in range(len(attrs)): + assert_(attrs[i].type_name == expected[i]) + + def test_badtype_parsing(self): + # Test parsing wrong type of attribute from their value. + def badtype_read(): + with open(test3) as ofile: + _, _ = read_header(ofile) + + assert_raises(ParseArffError, badtype_read) + + def test_fullheader1(self): + # Parsing trivial header with nothing. + with open(test1) as ofile: + rel, attrs = read_header(ofile) + + # Test relation + assert_(rel == 'test1') + + # Test numerical attributes + assert_(len(attrs) == 5) + for i in range(4): + assert_(attrs[i].name == 'attr%d' % i) + assert_(attrs[i].type_name == 'numeric') + + # Test nominal attribute + assert_(attrs[4].name == 'class') + assert_(attrs[4].values == ('class0', 'class1', 'class2', 'class3')) + + def test_dateheader(self): + with open(test7) as ofile: + rel, attrs = read_header(ofile) + + assert_(rel == 'test7') + + assert_(len(attrs) == 5) + + assert_(attrs[0].name == 'attr_year') + assert_(attrs[0].date_format == '%Y') + + assert_(attrs[1].name == 'attr_month') + assert_(attrs[1].date_format == '%Y-%m') + + assert_(attrs[2].name == 'attr_date') + assert_(attrs[2].date_format == '%Y-%m-%d') + + assert_(attrs[3].name == 'attr_datetime_local') + assert_(attrs[3].date_format == '%Y-%m-%d %H:%M') + + assert_(attrs[4].name == 'attr_datetime_missing') + assert_(attrs[4].date_format == '%Y-%m-%d %H:%M') + + def test_dateheader_unsupported(self): + def read_dateheader_unsupported(): + with open(test8) as ofile: + _, _ = read_header(ofile) + + assert_raises(ValueError, read_dateheader_unsupported) + + +class TestDateAttribute: + def setup_method(self): + self.data, self.meta = loadarff(test7) + + def test_year_attribute(self): + expected = np.array([ + '1999', + '2004', + '1817', + '2100', + '2013', + '1631' + ], dtype='datetime64[Y]') + + assert_array_equal(self.data["attr_year"], expected) + + def test_month_attribute(self): + expected = np.array([ + '1999-01', + '2004-12', + '1817-04', + '2100-09', + '2013-11', + '1631-10' + ], dtype='datetime64[M]') + + assert_array_equal(self.data["attr_month"], expected) + + def test_date_attribute(self): + expected = np.array([ + '1999-01-31', + '2004-12-01', + '1817-04-28', + '2100-09-10', + '2013-11-30', + '1631-10-15' + ], dtype='datetime64[D]') + + assert_array_equal(self.data["attr_date"], expected) + + def test_datetime_local_attribute(self): + expected = np.array([ + datetime.datetime(year=1999, month=1, day=31, hour=0, minute=1), + datetime.datetime(year=2004, month=12, day=1, hour=23, minute=59), + datetime.datetime(year=1817, month=4, day=28, hour=13, minute=0), + datetime.datetime(year=2100, month=9, day=10, hour=12, minute=0), + datetime.datetime(year=2013, month=11, day=30, hour=4, minute=55), + datetime.datetime(year=1631, month=10, day=15, hour=20, minute=4) + ], dtype='datetime64[m]') + + assert_array_equal(self.data["attr_datetime_local"], expected) + + def test_datetime_missing(self): + expected = np.array([ + 'nat', + '2004-12-01T23:59', + 'nat', + 'nat', + '2013-11-30T04:55', + '1631-10-15T20:04' + ], dtype='datetime64[m]') + + assert_array_equal(self.data["attr_datetime_missing"], expected) + + def test_datetime_timezone(self): + assert_raises(ParseArffError, loadarff, test8) + + +class TestRelationalAttribute: + def setup_method(self): + self.data, self.meta = loadarff(test9) + + def test_attributes(self): + assert_equal(len(self.meta._attributes), 1) + + relational = list(self.meta._attributes.values())[0] + + assert_equal(relational.name, 'attr_date_number') + assert_equal(relational.type_name, 'relational') + assert_equal(len(relational.attributes), 2) + assert_equal(relational.attributes[0].name, + 'attr_date') + assert_equal(relational.attributes[0].type_name, + 'date') + assert_equal(relational.attributes[1].name, + 'attr_number') + assert_equal(relational.attributes[1].type_name, + 'numeric') + + def test_data(self): + dtype_instance = [('attr_date', 'datetime64[D]'), + ('attr_number', np.float64)] + + expected = [ + np.array([('1999-01-31', 1), ('1935-11-27', 10)], + dtype=dtype_instance), + np.array([('2004-12-01', 2), ('1942-08-13', 20)], + dtype=dtype_instance), + np.array([('1817-04-28', 3)], + dtype=dtype_instance), + np.array([('2100-09-10', 4), ('1957-04-17', 40), + ('1721-01-14', 400)], + dtype=dtype_instance), + np.array([('2013-11-30', 5)], + dtype=dtype_instance), + np.array([('1631-10-15', 6)], + dtype=dtype_instance) + ] + + for i in range(len(self.data["attr_date_number"])): + assert_array_equal(self.data["attr_date_number"][i], + expected[i]) + + +class TestRelationalAttributeLong: + def setup_method(self): + self.data, self.meta = loadarff(test10) + + def test_attributes(self): + assert_equal(len(self.meta._attributes), 1) + + relational = list(self.meta._attributes.values())[0] + + assert_equal(relational.name, 'attr_relational') + assert_equal(relational.type_name, 'relational') + assert_equal(len(relational.attributes), 1) + assert_equal(relational.attributes[0].name, + 'attr_number') + assert_equal(relational.attributes[0].type_name, 'numeric') + + def test_data(self): + dtype_instance = [('attr_number', np.float64)] + + expected = np.array([(n,) for n in range(30000)], + dtype=dtype_instance) + + assert_array_equal(self.data["attr_relational"][0], + expected) + + +class TestQuotedNominal: + """ + Regression test for issue #10232: + + Exception in loadarff with quoted nominal attributes. + """ + + def setup_method(self): + self.data, self.meta = loadarff(test_quoted_nominal) + + def test_attributes(self): + assert_equal(len(self.meta._attributes), 2) + + age, smoker = self.meta._attributes.values() + + assert_equal(age.name, 'age') + assert_equal(age.type_name, 'numeric') + assert_equal(smoker.name, 'smoker') + assert_equal(smoker.type_name, 'nominal') + assert_equal(smoker.values, ['yes', 'no']) + + def test_data(self): + + age_dtype_instance = np.float64 + smoker_dtype_instance = '' (big endian) + +''' +import sys + +__all__ = [ + 'aliases', 'native_code', 'swapped_code', + 'sys_is_le', 'to_numpy_code' +] + +sys_is_le = sys.byteorder == 'little' +native_code = sys_is_le and '<' or '>' +swapped_code = sys_is_le and '>' or '<' + +aliases = {'little': ('little', '<', 'l', 'le'), + 'big': ('big', '>', 'b', 'be'), + 'native': ('native', '='), + 'swapped': ('swapped', 'S')} + + +def to_numpy_code(code): + """ + Convert various order codings to NumPy format. + + Parameters + ---------- + code : str + The code to convert. It is converted to lower case before parsing. + Legal values are: + 'little', 'big', 'l', 'b', 'le', 'be', '<', '>', 'native', '=', + 'swapped', 's'. + + Returns + ------- + out_code : {'<', '>'} + Here '<' is the numpy dtype code for little endian, + and '>' is the code for big endian. + + Examples + -------- + >>> import sys + >>> from scipy.io.matlab._byteordercodes import to_numpy_code + >>> sys_is_le = (sys.byteorder == 'little') + >>> sys_is_le + True + >>> to_numpy_code('big') + '>' + >>> to_numpy_code('little') + '<' + >>> nc = to_numpy_code('native') + >>> nc == '<' if sys_is_le else nc == '>' + True + >>> sc = to_numpy_code('swapped') + >>> sc == '>' if sys_is_le else sc == '<' + True + + """ + code = code.lower() + if code is None: + return native_code + if code in aliases['little']: + return '<' + elif code in aliases['big']: + return '>' + elif code in aliases['native']: + return native_code + elif code in aliases['swapped']: + return swapped_code + else: + raise ValueError( + f'We cannot handle byte order {code}') diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/_mio.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/_mio.py new file mode 100644 index 0000000000000000000000000000000000000000..4c86d873bd11fb45676d8db37a5d60b032276ecc --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/_mio.py @@ -0,0 +1,372 @@ +""" +Module for reading and writing matlab (TM) .mat files +""" +# Authors: Travis Oliphant, Matthew Brett + +from contextlib import contextmanager + +from ._miobase import _get_matfile_version, docfiller +from ._mio4 import MatFile4Reader, MatFile4Writer +from ._mio5 import MatFile5Reader, MatFile5Writer + +__all__ = ['loadmat', 'savemat', 'whosmat'] + + +@contextmanager +def _open_file_context(file_like, appendmat, mode='rb'): + f, opened = _open_file(file_like, appendmat, mode) + try: + yield f + finally: + if opened: + f.close() + + +def _open_file(file_like, appendmat, mode='rb'): + """ + Open `file_like` and return as file-like object. First, check if object is + already file-like; if so, return it as-is. Otherwise, try to pass it + to open(). If that fails, and `file_like` is a string, and `appendmat` is true, + append '.mat' and try again. + """ + reqs = {'read'} if set(mode) & set('r+') else set() + if set(mode) & set('wax+'): + reqs.add('write') + if reqs.issubset(dir(file_like)): + return file_like, False + + try: + return open(file_like, mode), True + except OSError as e: + # Probably "not found" + if isinstance(file_like, str): + if appendmat and not file_like.endswith('.mat'): + file_like += '.mat' + return open(file_like, mode), True + else: + raise OSError( + 'Reader needs file name or open file-like object' + ) from e + + +@docfiller +def mat_reader_factory(file_name, appendmat=True, **kwargs): + """ + Create reader for matlab .mat format files. + + Parameters + ---------- + %(file_arg)s + %(append_arg)s + %(load_args)s + %(struct_arg)s + + Returns + ------- + matreader : MatFileReader object + Initialized instance of MatFileReader class matching the mat file + type detected in `filename`. + file_opened : bool + Whether the file was opened by this routine. + + """ + byte_stream, file_opened = _open_file(file_name, appendmat) + mjv, mnv = _get_matfile_version(byte_stream) + if mjv == 0: + return MatFile4Reader(byte_stream, **kwargs), file_opened + elif mjv == 1: + return MatFile5Reader(byte_stream, **kwargs), file_opened + elif mjv == 2: + raise NotImplementedError('Please use HDF reader for matlab v7.3 ' + 'files, e.g. h5py') + else: + raise TypeError(f'Did not recognize version {mjv}') + + +@docfiller +def loadmat(file_name, mdict=None, appendmat=True, *, spmatrix=True, **kwargs): + """ + Load MATLAB file. + + Parameters + ---------- + file_name : str + Name of the mat file (do not need .mat extension if + appendmat==True). Can also pass open file-like object. + mdict : dict, optional + Dictionary in which to insert matfile variables. + appendmat : bool, optional + True to append the .mat extension to the end of the given + filename, if not already present. Default is True. + spmatrix : bool, optional (default: True) + If ``True``, return sparse ``coo_matrix``. Otherwise return ``coo_array``. + Only relevant for sparse variables. + byte_order : str or None, optional + None by default, implying byte order guessed from mat + file. Otherwise can be one of ('native', '=', 'little', '<', + 'BIG', '>'). + mat_dtype : bool, optional + If True, return arrays in same dtype as would be loaded into + MATLAB (instead of the dtype with which they are saved). + squeeze_me : bool, optional + Whether to squeeze unit matrix dimensions or not. + chars_as_strings : bool, optional + Whether to convert char arrays to string arrays. + matlab_compatible : bool, optional + Returns matrices as would be loaded by MATLAB (implies + squeeze_me=False, chars_as_strings=False, mat_dtype=True, + struct_as_record=True). + struct_as_record : bool, optional + Whether to load MATLAB structs as NumPy record arrays, or as + old-style NumPy arrays with dtype=object. Setting this flag to + False replicates the behavior of scipy version 0.7.x (returning + NumPy object arrays). The default setting is True, because it + allows easier round-trip load and save of MATLAB files. + verify_compressed_data_integrity : bool, optional + Whether the length of compressed sequences in the MATLAB file + should be checked, to ensure that they are not longer than we expect. + It is advisable to enable this (the default) because overlong + compressed sequences in MATLAB files generally indicate that the + files have experienced some sort of corruption. + variable_names : None or sequence + If None (the default) - read all variables in file. Otherwise, + `variable_names` should be a sequence of strings, giving names of the + MATLAB variables to read from the file. The reader will skip any + variable with a name not in this sequence, possibly saving some read + processing. + simplify_cells : False, optional + If True, return a simplified dict structure (which is useful if the mat + file contains cell arrays). Note that this only affects the structure + of the result and not its contents (which is identical for both output + structures). If True, this automatically sets `struct_as_record` to + False and `squeeze_me` to True, which is required to simplify cells. + uint16_codec : str, optional + The codec to use for decoding characters, which are stored as uint16 + values. The default uses the system encoding, but this can be manually + set to other values such as 'ascii', 'latin1', and 'utf-8'. This + parameter is relevant only for files stored as v6 and above, and not + for files stored as v4. + + Returns + ------- + mat_dict : dict + dictionary with variable names as keys, and loaded matrices as values. + + Notes + ----- + v4 (Level 1.0), v6 and v7 to 7.2 matfiles are supported. + + You will need an HDF5 Python library to read MATLAB 7.3 format mat + files. Because SciPy does not supply one, we do not implement the + HDF5 / 7.3 interface here. + + Examples + -------- + >>> from os.path import dirname, join as pjoin + >>> import scipy.io as sio + + Get the filename for an example .mat file from the tests/data directory. + + >>> data_dir = pjoin(dirname(sio.__file__), 'matlab', 'tests', 'data') + >>> mat_fname = pjoin(data_dir, 'testdouble_7.4_GLNX86.mat') + + Load the .mat file contents. + + >>> mat_contents = sio.loadmat(mat_fname, spmatrix=False) + + The result is a dictionary, one key/value pair for each variable: + + >>> sorted(mat_contents.keys()) + ['__globals__', '__header__', '__version__', 'testdouble'] + >>> mat_contents['testdouble'] + array([[0. , 0.78539816, 1.57079633, 2.35619449, 3.14159265, + 3.92699082, 4.71238898, 5.49778714, 6.28318531]]) + + By default SciPy reads MATLAB structs as structured NumPy arrays where the + dtype fields are of type `object` and the names correspond to the MATLAB + struct field names. This can be disabled by setting the optional argument + `struct_as_record=False`. + + Get the filename for an example .mat file that contains a MATLAB struct + called `teststruct` and load the contents. + + >>> matstruct_fname = pjoin(data_dir, 'teststruct_7.4_GLNX86.mat') + >>> matstruct_contents = sio.loadmat(matstruct_fname) + >>> teststruct = matstruct_contents['teststruct'] + >>> teststruct.dtype + dtype([('stringfield', 'O'), ('doublefield', 'O'), ('complexfield', 'O')]) + + The size of the structured array is the size of the MATLAB struct, not the + number of elements in any particular field. The shape defaults to 2-D + unless the optional argument `squeeze_me=True`, in which case all length 1 + dimensions are removed. + + >>> teststruct.size + 1 + >>> teststruct.shape + (1, 1) + + Get the 'stringfield' of the first element in the MATLAB struct. + + >>> teststruct[0, 0]['stringfield'] + array(['Rats live on no evil star.'], + dtype='>> teststruct['doublefield'][0, 0] + array([[ 1.41421356, 2.71828183, 3.14159265]]) + + Load the MATLAB struct, squeezing out length 1 dimensions, and get the item + from the 'complexfield'. + + >>> matstruct_squeezed = sio.loadmat(matstruct_fname, squeeze_me=True) + >>> matstruct_squeezed['teststruct'].shape + () + >>> matstruct_squeezed['teststruct']['complexfield'].shape + () + >>> matstruct_squeezed['teststruct']['complexfield'].item() + array([ 1.41421356+1.41421356j, 2.71828183+2.71828183j, + 3.14159265+3.14159265j]) + """ + variable_names = kwargs.pop('variable_names', None) + with _open_file_context(file_name, appendmat) as f: + MR, _ = mat_reader_factory(f, **kwargs) + matfile_dict = MR.get_variables(variable_names) + if spmatrix: + from scipy.sparse import issparse, coo_matrix + for name, var in list(matfile_dict.items()): + if issparse(var): + matfile_dict[name] = coo_matrix(var) + + if mdict is not None: + mdict.update(matfile_dict) + else: + mdict = matfile_dict + + return mdict + + +@docfiller +def savemat(file_name, mdict, + appendmat=True, + format='5', + long_field_names=False, + do_compression=False, + oned_as='row'): + """ + Save a dictionary of names and arrays into a MATLAB-style .mat file. + + This saves the array objects in the given dictionary to a MATLAB- + style .mat file. + + Parameters + ---------- + file_name : str or file-like object + Name of the .mat file (.mat extension not needed if ``appendmat == + True``). + Can also pass open file_like object. + mdict : dict + Dictionary from which to save matfile variables. + appendmat : bool, optional + True (the default) to append the .mat extension to the end of the + given filename, if not already present. + format : {'5', '4'}, string, optional + '5' (the default) for MATLAB 5 and up (to 7.2), + '4' for MATLAB 4 .mat files. + long_field_names : bool, optional + False (the default) - maximum field name length in a structure is + 31 characters which is the documented maximum length. + True - maximum field name length in a structure is 63 characters + which works for MATLAB 7.6+. + do_compression : bool, optional + Whether or not to compress matrices on write. Default is False. + oned_as : {'row', 'column'}, optional + If 'column', write 1-D NumPy arrays as column vectors. + If 'row', write 1-D NumPy arrays as row vectors. + + Examples + -------- + >>> from scipy.io import savemat + >>> import numpy as np + >>> a = np.arange(20) + >>> mdic = {"a": a, "label": "experiment"} + >>> mdic + {'a': array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, + 17, 18, 19]), + 'label': 'experiment'} + >>> savemat("matlab_matrix.mat", mdic) + """ + with _open_file_context(file_name, appendmat, 'wb') as file_stream: + if format == '4': + if long_field_names: + message = "Long field names are not available for version 4 files" + raise ValueError(message) + MW = MatFile4Writer(file_stream, oned_as) + elif format == '5': + MW = MatFile5Writer(file_stream, + do_compression=do_compression, + unicode_strings=True, + long_field_names=long_field_names, + oned_as=oned_as) + else: + raise ValueError("Format should be '4' or '5'") + MW.put_variables(mdict) + + +@docfiller +def whosmat(file_name, appendmat=True, **kwargs): + """ + List variables inside a MATLAB file. + + Parameters + ---------- + %(file_arg)s + %(append_arg)s + %(load_args)s + %(struct_arg)s + + Returns + ------- + variables : list of tuples + A list of tuples, where each tuple holds the matrix name (a string), + its shape (tuple of ints), and its data class (a string). + Possible data classes are: int8, uint8, int16, uint16, int32, uint32, + int64, uint64, single, double, cell, struct, object, char, sparse, + function, opaque, logical, unknown. + + Notes + ----- + v4 (Level 1.0), v6 and v7 to 7.2 matfiles are supported. + + You will need an HDF5 python library to read matlab 7.3 format mat + files (e.g. h5py). Because SciPy does not supply one, we do not implement the + HDF5 / 7.3 interface here. + + .. versionadded:: 0.12.0 + + Examples + -------- + >>> from io import BytesIO + >>> import numpy as np + >>> from scipy.io import savemat, whosmat + + Create some arrays, and use `savemat` to write them to a ``BytesIO`` + instance. + + >>> a = np.array([[10, 20, 30], [11, 21, 31]], dtype=np.int32) + >>> b = np.geomspace(1, 10, 5) + >>> f = BytesIO() + >>> savemat(f, {'a': a, 'b': b}) + + Use `whosmat` to inspect ``f``. Each tuple in the output list gives + the name, shape and data type of the array in ``f``. + + >>> whosmat(f) + [('a', (2, 3), 'int32'), ('b', (1, 5), 'double')] + + """ + with _open_file_context(file_name, appendmat) as f: + ML, file_opened = mat_reader_factory(f, **kwargs) + variables = ML.list_variables() + return variables diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/_mio4.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/_mio4.py new file mode 100644 index 0000000000000000000000000000000000000000..b108386d110e6062d088498259e849603583eb94 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/_mio4.py @@ -0,0 +1,632 @@ +''' Classes for read / write of matlab (TM) 4 files +''' +import sys +import warnings +import math +from operator import mul + +import numpy as np + +import scipy.sparse + +from ._miobase import (MatFileReader, docfiller, matdims, read_dtype, + convert_dtypes, arr_to_chars, arr_dtype_number) + +from ._mio_utils import squeeze_element, chars_to_strings +from functools import reduce + + +__all__ = [ + 'MatFile4Reader', 'MatFile4Writer', 'SYS_LITTLE_ENDIAN', + 'VarHeader4', 'VarReader4', 'VarWriter4', 'arr_to_2d', 'mclass_info', + 'mdtypes_template', 'miDOUBLE', 'miINT16', 'miINT32', 'miSINGLE', + 'miUINT16', 'miUINT8', 'mxCHAR_CLASS', 'mxFULL_CLASS', 'mxSPARSE_CLASS', + 'np_to_mtypes', 'order_codes' +] + + +SYS_LITTLE_ENDIAN = sys.byteorder == 'little' + +miDOUBLE = 0 +miSINGLE = 1 +miINT32 = 2 +miINT16 = 3 +miUINT16 = 4 +miUINT8 = 5 + +mdtypes_template = { + miDOUBLE: 'f8', + miSINGLE: 'f4', + miINT32: 'i4', + miINT16: 'i2', + miUINT16: 'u2', + miUINT8: 'u1', + 'header': [('mopt', 'i4'), + ('mrows', 'i4'), + ('ncols', 'i4'), + ('imagf', 'i4'), + ('namlen', 'i4')], + 'U1': 'U1', + } + +np_to_mtypes = { + 'f8': miDOUBLE, + 'c32': miDOUBLE, + 'c24': miDOUBLE, + 'c16': miDOUBLE, + 'f4': miSINGLE, + 'c8': miSINGLE, + 'i4': miINT32, + 'i2': miINT16, + 'u2': miUINT16, + 'u1': miUINT8, + 'S1': miUINT8, + } + +# matrix classes +mxFULL_CLASS = 0 +mxCHAR_CLASS = 1 +mxSPARSE_CLASS = 2 + +order_codes = { + 0: '<', + 1: '>', + 2: 'VAX D-float', # ! + 3: 'VAX G-float', + 4: 'Cray', # !! + } + +mclass_info = { + mxFULL_CLASS: 'double', + mxCHAR_CLASS: 'char', + mxSPARSE_CLASS: 'sparse', + } + + +_MAX_INTP = np.iinfo(np.intp).max + + +class VarHeader4: + # Mat4 variables never logical or global + is_logical = False + is_global = False + + def __init__(self, + name, + dtype, + mclass, + dims, + is_complex): + self.name = name + self.dtype = dtype + self.mclass = mclass + self.dims = dims + self.is_complex = is_complex + + +class VarReader4: + ''' Class to read matlab 4 variables ''' + + def __init__(self, file_reader): + self.file_reader = file_reader + self.mat_stream = file_reader.mat_stream + self.dtypes = file_reader.dtypes + self.chars_as_strings = file_reader.chars_as_strings + self.squeeze_me = file_reader.squeeze_me + + def read_header(self): + ''' Read and return header for variable ''' + data = read_dtype(self.mat_stream, self.dtypes['header']) + name = self.mat_stream.read(int(data['namlen'])).strip(b'\x00') + if data['mopt'] < 0 or data['mopt'] > 5000: + raise ValueError('Mat 4 mopt wrong format, byteswapping problem?') + M, rest = divmod(data['mopt'], 1000) # order code + if M not in (0, 1): + warnings.warn(f"We do not support byte ordering '{order_codes[M]}';" + " returned data may be corrupt", + UserWarning, stacklevel=3) + O, rest = divmod(rest, 100) # unused, should be 0 + if O != 0: + raise ValueError('O in MOPT integer should be 0, wrong format?') + P, rest = divmod(rest, 10) # data type code e.g miDOUBLE (see above) + T = rest # matrix type code e.g., mxFULL_CLASS (see above) + dims = (data['mrows'], data['ncols']) + is_complex = data['imagf'] == 1 + dtype = self.dtypes[P] + return VarHeader4( + name, + dtype, + T, + dims, + is_complex) + + def array_from_header(self, hdr, process=True): + mclass = hdr.mclass + if mclass == mxFULL_CLASS: + arr = self.read_full_array(hdr) + elif mclass == mxCHAR_CLASS: + arr = self.read_char_array(hdr) + if process and self.chars_as_strings: + arr = chars_to_strings(arr) + elif mclass == mxSPARSE_CLASS: + # no current processing (below) makes sense for sparse + return self.read_sparse_array(hdr) + else: + raise TypeError(f'No reader for class code {mclass}') + if process and self.squeeze_me: + return squeeze_element(arr) + return arr + + def read_sub_array(self, hdr, copy=True): + ''' Mat4 read using header `hdr` dtype and dims + + Parameters + ---------- + hdr : object + object with attributes ``dtype``, ``dims``. dtype is assumed to be + the correct endianness + copy : bool, optional + copies array before return if True (default True) + (buffer is usually read only) + + Returns + ------- + arr : ndarray + of dtype given by `hdr` ``dtype`` and shape given by `hdr` ``dims`` + ''' + dt = hdr.dtype + # Fast product for large (>2GB) arrays. + num_bytes = reduce(mul, hdr.dims, np.int64(dt.itemsize)) + if num_bytes > _MAX_INTP: + raise ValueError( + f"Variable '{hdr.name.decode('latin1')}' has byte length " + f"longer than largest possible NumPy array on this platform.") + buffer = self.mat_stream.read(num_bytes) + if len(buffer) != num_bytes: + raise ValueError( + f"Not enough bytes to read matrix " + f"'{hdr.name.decode('latin1')}'; is this a badly-formed file? " + f"Consider listing matrices with `whosmat` and loading named " + f"matrices with `variable_names` kwarg to `loadmat`") + arr = np.ndarray(shape=hdr.dims, + dtype=dt, + buffer=buffer, + order='F') + if copy: + arr = arr.copy() + return arr + + def read_full_array(self, hdr): + ''' Full (rather than sparse) matrix getter + + Read matrix (array) can be real or complex + + Parameters + ---------- + hdr : ``VarHeader4`` instance + + Returns + ------- + arr : ndarray + complex array if ``hdr.is_complex`` is True, otherwise a real + numeric array + ''' + if hdr.is_complex: + # avoid array copy to save memory + res = self.read_sub_array(hdr, copy=False) + res_j = self.read_sub_array(hdr, copy=False) + return res + (res_j * 1j) + return self.read_sub_array(hdr) + + def read_char_array(self, hdr): + ''' latin-1 text matrix (char matrix) reader + + Parameters + ---------- + hdr : ``VarHeader4`` instance + + Returns + ------- + arr : ndarray + with dtype 'U1', shape given by `hdr` ``dims`` + ''' + arr = self.read_sub_array(hdr).astype(np.uint8) + S = arr.tobytes().decode('latin-1') + return np.ndarray(shape=hdr.dims, + dtype=np.dtype('U1'), + buffer=np.array(S)).copy() + + def read_sparse_array(self, hdr): + ''' Read and return sparse matrix type + + Parameters + ---------- + hdr : ``VarHeader4`` instance + + Returns + ------- + arr : coo_array + with dtype ``float`` and shape read from the sparse array data + + Notes + ----- + MATLAB 4 real sparse arrays are saved in a N+1 by 3 array format, where + N is the number of non-zero values. Column 1 values [0:N] are the + (1-based) row indices of the each non-zero value, column 2 [0:N] are the + column indices, column 3 [0:N] are the (real) values. The last values + [-1,0:2] of the rows, column indices are shape[0] and shape[1] + respectively of the output matrix. The last value for the values column + is a padding 0. mrows and ncols values from the header give the shape of + the stored matrix, here [N+1, 3]. Complex data are saved as a 4 column + matrix, where the fourth column contains the imaginary component; the + last value is again 0. Complex sparse data do *not* have the header + ``imagf`` field set to True; the fact that the data are complex is only + detectable because there are 4 storage columns. + ''' + res = self.read_sub_array(hdr) + tmp = res[:-1,:] + # All numbers are float64 in Matlab, but SciPy sparse expects int shape + dims = (int(res[-1,0]), int(res[-1,1])) + I = np.ascontiguousarray(tmp[:,0],dtype='intc') # fixes byte order also + J = np.ascontiguousarray(tmp[:,1],dtype='intc') + I -= 1 # for 1-based indexing + J -= 1 + if res.shape[1] == 3: + V = np.ascontiguousarray(tmp[:,2],dtype='float') + else: + V = np.ascontiguousarray(tmp[:,2],dtype='complex') + V.imag = tmp[:,3] + return scipy.sparse.coo_array((V,(I,J)), dims) + + def shape_from_header(self, hdr): + '''Read the shape of the array described by the header. + The file position after this call is unspecified. + ''' + mclass = hdr.mclass + if mclass == mxFULL_CLASS: + shape = tuple(map(int, hdr.dims)) + elif mclass == mxCHAR_CLASS: + shape = tuple(map(int, hdr.dims)) + if self.chars_as_strings: + shape = shape[:-1] + elif mclass == mxSPARSE_CLASS: + dt = hdr.dtype + dims = hdr.dims + + if not (len(dims) == 2 and dims[0] >= 1 and dims[1] >= 1): + return () + + # Read only the row and column counts + self.mat_stream.seek(dt.itemsize * (dims[0] - 1), 1) + rows = np.ndarray(shape=(), dtype=dt, + buffer=self.mat_stream.read(dt.itemsize)) + self.mat_stream.seek(dt.itemsize * (dims[0] - 1), 1) + cols = np.ndarray(shape=(), dtype=dt, + buffer=self.mat_stream.read(dt.itemsize)) + + shape = (int(rows), int(cols)) + else: + raise TypeError(f'No reader for class code {mclass}') + + if self.squeeze_me: + shape = tuple([x for x in shape if x != 1]) + return shape + + +class MatFile4Reader(MatFileReader): + ''' Reader for Mat4 files ''' + @docfiller + def __init__(self, mat_stream, *args, **kwargs): + ''' Initialize matlab 4 file reader + + %(matstream_arg)s + %(load_args)s + ''' + super().__init__(mat_stream, *args, **kwargs) + self._matrix_reader = None + + def guess_byte_order(self): + self.mat_stream.seek(0) + mopt = read_dtype(self.mat_stream, np.dtype('i4')) + self.mat_stream.seek(0) + if mopt == 0: + return '<' + if mopt < 0 or mopt > 5000: + # Number must have been byteswapped + return SYS_LITTLE_ENDIAN and '>' or '<' + # Not byteswapped + return SYS_LITTLE_ENDIAN and '<' or '>' + + def initialize_read(self): + ''' Run when beginning read of variables + + Sets up readers from parameters in `self` + ''' + self.dtypes = convert_dtypes(mdtypes_template, self.byte_order) + self._matrix_reader = VarReader4(self) + + def read_var_header(self): + ''' Read and return header, next position + + Parameters + ---------- + None + + Returns + ------- + header : object + object that can be passed to self.read_var_array, and that + has attributes ``name`` and ``is_global`` + next_position : int + position in stream of next variable + ''' + hdr = self._matrix_reader.read_header() + # Fast product for large (>2GB) arrays. + remaining_bytes = reduce(mul, hdr.dims, np.int64(hdr.dtype.itemsize)) + if hdr.is_complex and not hdr.mclass == mxSPARSE_CLASS: + remaining_bytes *= 2 + next_position = self.mat_stream.tell() + remaining_bytes + return hdr, next_position + + def read_var_array(self, header, process=True): + ''' Read array, given `header` + + Parameters + ---------- + header : header object + object with fields defining variable header + process : {True, False}, optional + If True, apply recursive post-processing during loading of array. + + Returns + ------- + arr : array + array with post-processing applied or not according to + `process`. + ''' + return self._matrix_reader.array_from_header(header, process) + + def get_variables(self, variable_names=None): + ''' get variables from stream as dictionary + + Parameters + ---------- + variable_names : None or str or sequence of str, optional + variable name, or sequence of variable names to get from Mat file / + file stream. If None, then get all variables in file. + ''' + if isinstance(variable_names, str): + variable_names = [variable_names] + elif variable_names is not None: + variable_names = list(variable_names) + self.mat_stream.seek(0) + # set up variable reader + self.initialize_read() + mdict = {} + while not self.end_of_stream(): + hdr, next_position = self.read_var_header() + name = 'None' if hdr.name is None else hdr.name.decode('latin1') + if variable_names is not None and name not in variable_names: + self.mat_stream.seek(next_position) + continue + mdict[name] = self.read_var_array(hdr) + self.mat_stream.seek(next_position) + if variable_names is not None: + variable_names.remove(name) + if len(variable_names) == 0: + break + return mdict + + def list_variables(self): + ''' list variables from stream ''' + self.mat_stream.seek(0) + # set up variable reader + self.initialize_read() + vars = [] + while not self.end_of_stream(): + hdr, next_position = self.read_var_header() + name = 'None' if hdr.name is None else hdr.name.decode('latin1') + shape = self._matrix_reader.shape_from_header(hdr) + info = mclass_info.get(hdr.mclass, 'unknown') + vars.append((name, shape, info)) + + self.mat_stream.seek(next_position) + return vars + + +def arr_to_2d(arr, oned_as='row'): + ''' Make ``arr`` exactly two dimensional + + If `arr` has more than 2 dimensions, raise a ValueError + + Parameters + ---------- + arr : array + oned_as : {'row', 'column'}, optional + Whether to reshape 1-D vectors as row vectors or column vectors. + See documentation for ``matdims`` for more detail + + Returns + ------- + arr2d : array + 2-D version of the array + ''' + dims = matdims(arr, oned_as) + if len(dims) > 2: + raise ValueError('Matlab 4 files cannot save arrays with more than ' + '2 dimensions') + return arr.reshape(dims) + + +class VarWriter4: + def __init__(self, file_writer): + self.file_stream = file_writer.file_stream + self.oned_as = file_writer.oned_as + + def write_bytes(self, arr): + self.file_stream.write(arr.tobytes(order='F')) + + def write_string(self, s): + self.file_stream.write(s) + + def write_header(self, name, shape, P=miDOUBLE, T=mxFULL_CLASS, imagf=0): + ''' Write header for given data options + + Parameters + ---------- + name : str + name of variable + shape : sequence + Shape of array as it will be read in matlab + P : int, optional + code for mat4 data type, one of ``miDOUBLE, miSINGLE, miINT32, + miINT16, miUINT16, miUINT8`` + T : int, optional + code for mat4 matrix class, one of ``mxFULL_CLASS, mxCHAR_CLASS, + mxSPARSE_CLASS`` + imagf : int, optional + flag indicating complex + ''' + header = np.empty((), mdtypes_template['header']) + M = not SYS_LITTLE_ENDIAN + O = 0 + header['mopt'] = (M * 1000 + + O * 100 + + P * 10 + + T) + header['mrows'] = shape[0] + header['ncols'] = shape[1] + header['imagf'] = imagf + header['namlen'] = len(name) + 1 + self.write_bytes(header) + data = name + '\0' + self.write_string(data.encode('latin1')) + + def write(self, arr, name): + ''' Write matrix `arr`, with name `name` + + Parameters + ---------- + arr : array_like + array to write + name : str + name in matlab workspace + ''' + # we need to catch sparse first, because np.asarray returns an + # an object array for scipy.sparse + if scipy.sparse.issparse(arr): + self.write_sparse(arr, name) + return + arr = np.asarray(arr) + dt = arr.dtype + if not dt.isnative: + arr = arr.astype(dt.newbyteorder('=')) + dtt = dt.type + if dtt is np.object_: + raise TypeError('Cannot save object arrays in Mat4') + elif dtt is np.void: + raise TypeError('Cannot save void type arrays') + elif dtt in (np.str_, np.bytes_): + self.write_char(arr, name) + return + self.write_numeric(arr, name) + + def write_numeric(self, arr, name): + arr = arr_to_2d(arr, self.oned_as) + imagf = arr.dtype.kind == 'c' + try: + P = np_to_mtypes[arr.dtype.str[1:]] + except KeyError: + if imagf: + arr = arr.astype('c128') + else: + arr = arr.astype('f8') + P = miDOUBLE + self.write_header(name, + arr.shape, + P=P, + T=mxFULL_CLASS, + imagf=imagf) + if imagf: + self.write_bytes(arr.real) + self.write_bytes(arr.imag) + else: + self.write_bytes(arr) + + def write_char(self, arr, name): + if arr.dtype.type == np.str_ and arr.dtype.itemsize != np.dtype('U1').itemsize: + arr = arr_to_chars(arr) + arr = arr_to_2d(arr, self.oned_as) + dims = arr.shape + self.write_header( + name, + dims, + P=miUINT8, + T=mxCHAR_CLASS) + if arr.dtype.kind == 'U': + # Recode unicode to latin1 + n_chars = math.prod(dims) + st_arr = np.ndarray(shape=(), + dtype=arr_dtype_number(arr, n_chars), + buffer=arr) + st = st_arr.item().encode('latin-1') + arr = np.ndarray(shape=dims, dtype='S1', buffer=st) + self.write_bytes(arr) + + def write_sparse(self, arr, name): + ''' Sparse matrices are 2-D + + See docstring for VarReader4.read_sparse_array + ''' + A = arr.tocoo() # convert to sparse COO format (ijv) + imagf = A.dtype.kind == 'c' + ijv = np.zeros((A.nnz + 1, 3+imagf), dtype='f8') + ijv[:-1,0] = A.row + ijv[:-1,1] = A.col + ijv[:-1,0:2] += 1 # 1 based indexing + if imagf: + ijv[:-1,2] = A.data.real + ijv[:-1,3] = A.data.imag + else: + ijv[:-1,2] = A.data + ijv[-1,0:2] = A.shape + self.write_header( + name, + ijv.shape, + P=miDOUBLE, + T=mxSPARSE_CLASS) + self.write_bytes(ijv) + + +class MatFile4Writer: + ''' Class for writing matlab 4 format files ''' + def __init__(self, file_stream, oned_as=None): + self.file_stream = file_stream + if oned_as is None: + oned_as = 'row' + self.oned_as = oned_as + self._matrix_writer = None + + def put_variables(self, mdict, write_header=None): + ''' Write variables in `mdict` to stream + + Parameters + ---------- + mdict : mapping + mapping with method ``items`` return name, contents pairs + where ``name`` which will appeak in the matlab workspace in + file load, and ``contents`` is something writeable to a + matlab file, such as a NumPy array. + write_header : {None, True, False} + If True, then write the matlab file header before writing the + variables. If None (the default) then write the file header + if we are at position 0 in the stream. By setting False + here, and setting the stream position to the end of the file, + you can append variables to a matlab file + ''' + # there is no header for a matlab 4 mat file, so we ignore the + # ``write_header`` input argument. It's there for compatibility + # with the matlab 5 version of this method + self._matrix_writer = VarWriter4(self) + for name, var in mdict.items(): + self._matrix_writer.write(var, name) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/_mio5.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/_mio5.py new file mode 100644 index 0000000000000000000000000000000000000000..5c4ed0361a603e10338a8d494838ebd861f56cb8 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/_mio5.py @@ -0,0 +1,895 @@ +''' Classes for read / write of matlab (TM) 5 files + +The matfile specification last found here: + +https://www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/matfile_format.pdf + +(as of December 5 2008) + +================================= + Note on functions and mat files +================================= + +The document above does not give any hints as to the storage of matlab +function handles, or anonymous function handles. I had, therefore, to +guess the format of matlab arrays of ``mxFUNCTION_CLASS`` and +``mxOPAQUE_CLASS`` by looking at example mat files. + +``mxFUNCTION_CLASS`` stores all types of matlab functions. It seems to +contain a struct matrix with a set pattern of fields. For anonymous +functions, a sub-fields of one of these fields seems to contain the +well-named ``mxOPAQUE_CLASS``. This seems to contain: + +* array flags as for any matlab matrix +* 3 int8 strings +* a matrix + +It seems that whenever the mat file contains a ``mxOPAQUE_CLASS`` +instance, there is also an un-named matrix (name == '') at the end of +the mat file. I'll call this the ``__function_workspace__`` matrix. + +When I saved two anonymous functions in a mat file, or appended another +anonymous function to the mat file, there was still only one +``__function_workspace__`` un-named matrix at the end, but larger than +that for a mat file with a single anonymous function, suggesting that +the workspaces for the two functions had been merged. + +The ``__function_workspace__`` matrix appears to be of double class +(``mxCLASS_DOUBLE``), but stored as uint8, the memory for which is in +the format of a mini .mat file, without the first 124 bytes of the file +header (the description and the subsystem_offset), but with the version +U2 bytes, and the S2 endian test bytes. There follow 4 zero bytes, +presumably for 8 byte padding, and then a series of ``miMATRIX`` +entries, as in a standard mat file. The ``miMATRIX`` entries appear to +be series of un-named (name == '') matrices, and may also contain arrays +of this same mini-mat format. + +I guess that: + +* saving an anonymous function back to a mat file will need the + associated ``__function_workspace__`` matrix saved as well for the + anonymous function to work correctly. +* appending to a mat file that has a ``__function_workspace__`` would + involve first pulling off this workspace, appending, checking whether + there were any more anonymous functions appended, and then somehow + merging the relevant workspaces, and saving at the end of the mat + file. + +The mat files I was playing with are in ``tests/data``: + +* sqr.mat +* parabola.mat +* some_functions.mat + +See ``tests/test_mio.py:test_mio_funcs.py`` for the debugging +script I was working with. + +Small fragments of current code adapted from matfile.py by Heiko +Henkelmann; parts of the code for simplify_cells=True adapted from +http://blog.nephics.com/2019/08/28/better-loadmat-for-scipy/. +''' + +import math +import os +import time +import sys +import zlib + +from io import BytesIO + +import warnings + +import numpy as np + +import scipy.sparse + +from ._byteordercodes import native_code, swapped_code + +from ._miobase import (MatFileReader, docfiller, matdims, read_dtype, + arr_to_chars, arr_dtype_number, MatWriteError, + MatReadError, MatReadWarning) + +# Reader object for matlab 5 format variables +from ._mio5_utils import VarReader5 + +# Constants and helper objects +from ._mio5_params import (MatlabObject, MatlabFunction, MDTYPES, NP_TO_MTYPES, + NP_TO_MXTYPES, miCOMPRESSED, miMATRIX, miINT8, + miUTF8, miUINT32, mxCELL_CLASS, mxSTRUCT_CLASS, + mxOBJECT_CLASS, mxCHAR_CLASS, mxSPARSE_CLASS, + mxDOUBLE_CLASS, mclass_info, mat_struct) + +from ._streams import ZlibInputStream + + +def _has_struct(elem): + """Determine if elem is an array and if first array item is a struct.""" + return (isinstance(elem, np.ndarray) and (elem.size > 0) and (elem.ndim > 0) and + isinstance(elem[0], mat_struct)) + + +def _inspect_cell_array(ndarray): + """Construct lists from cell arrays (loaded as numpy ndarrays), recursing + into items if they contain mat_struct objects.""" + elem_list = [] + for sub_elem in ndarray: + if isinstance(sub_elem, mat_struct): + elem_list.append(_matstruct_to_dict(sub_elem)) + elif _has_struct(sub_elem): + elem_list.append(_inspect_cell_array(sub_elem)) + else: + elem_list.append(sub_elem) + return elem_list + + +def _matstruct_to_dict(matobj): + """Construct nested dicts from mat_struct objects.""" + d = {} + for f in matobj._fieldnames: + elem = matobj.__dict__[f] + if isinstance(elem, mat_struct): + d[f] = _matstruct_to_dict(elem) + elif _has_struct(elem): + d[f] = _inspect_cell_array(elem) + else: + d[f] = elem + return d + + +def _simplify_cells(d): + """Convert mat objects in dict to nested dicts.""" + for key in d: + if isinstance(d[key], mat_struct): + d[key] = _matstruct_to_dict(d[key]) + elif _has_struct(d[key]): + d[key] = _inspect_cell_array(d[key]) + return d + + +class MatFile5Reader(MatFileReader): + ''' Reader for Mat 5 mat files + Adds the following attribute to base class + + uint16_codec - char codec to use for uint16 char arrays + (defaults to system default codec) + + Uses variable reader that has the following standard interface (see + abstract class in ``miobase``:: + + __init__(self, file_reader) + read_header(self) + array_from_header(self) + + and added interface:: + + set_stream(self, stream) + read_full_tag(self) + + ''' + @docfiller + def __init__(self, + mat_stream, + byte_order=None, + mat_dtype=False, + squeeze_me=False, + chars_as_strings=True, + matlab_compatible=False, + struct_as_record=True, + verify_compressed_data_integrity=True, + uint16_codec=None, + simplify_cells=False): + '''Initializer for matlab 5 file format reader + + %(matstream_arg)s + %(load_args)s + %(struct_arg)s + uint16_codec : {None, string} + Set codec to use for uint16 char arrays (e.g., 'utf-8'). + Use system default codec if None + ''' + super().__init__( + mat_stream, + byte_order, + mat_dtype, + squeeze_me, + chars_as_strings, + matlab_compatible, + struct_as_record, + verify_compressed_data_integrity, + simplify_cells) + # Set uint16 codec + if not uint16_codec: + uint16_codec = sys.getdefaultencoding() + self.uint16_codec = uint16_codec + # placeholders for readers - see initialize_read method + self._file_reader = None + self._matrix_reader = None + + def guess_byte_order(self): + ''' Guess byte order. + Sets stream pointer to 0''' + self.mat_stream.seek(126) + mi = self.mat_stream.read(2) + self.mat_stream.seek(0) + return mi == b'IM' and '<' or '>' + + def read_file_header(self): + ''' Read in mat 5 file header ''' + hdict = {} + hdr_dtype = MDTYPES[self.byte_order]['dtypes']['file_header'] + hdr = read_dtype(self.mat_stream, hdr_dtype) + hdict['__header__'] = hdr['description'].item().strip(b' \t\n\000') + v_major = hdr['version'] >> 8 + v_minor = hdr['version'] & 0xFF + hdict['__version__'] = '%d.%d' % (v_major, v_minor) + return hdict + + def initialize_read(self): + ''' Run when beginning read of variables + + Sets up readers from parameters in `self` + ''' + # reader for top level stream. We need this extra top-level + # reader because we use the matrix_reader object to contain + # compressed matrices (so they have their own stream) + self._file_reader = VarReader5(self) + # reader for matrix streams + self._matrix_reader = VarReader5(self) + + def read_var_header(self): + ''' Read header, return header, next position + + Header has to define at least .name and .is_global + + Parameters + ---------- + None + + Returns + ------- + header : object + object that can be passed to self.read_var_array, and that + has attributes .name and .is_global + next_position : int + position in stream of next variable + ''' + mdtype, byte_count = self._file_reader.read_full_tag() + if not byte_count > 0: + raise ValueError("Did not read any bytes") + next_pos = self.mat_stream.tell() + byte_count + if mdtype == miCOMPRESSED: + # Make new stream from compressed data + stream = ZlibInputStream(self.mat_stream, byte_count) + self._matrix_reader.set_stream(stream) + check_stream_limit = self.verify_compressed_data_integrity + mdtype, byte_count = self._matrix_reader.read_full_tag() + else: + check_stream_limit = False + self._matrix_reader.set_stream(self.mat_stream) + if not mdtype == miMATRIX: + raise TypeError('Expecting miMATRIX type here, got %d' % mdtype) + header = self._matrix_reader.read_header(check_stream_limit) + return header, next_pos + + def read_var_array(self, header, process=True): + ''' Read array, given `header` + + Parameters + ---------- + header : header object + object with fields defining variable header + process : {True, False} bool, optional + If True, apply recursive post-processing during loading of + array. + + Returns + ------- + arr : array + array with post-processing applied or not according to + `process`. + ''' + return self._matrix_reader.array_from_header(header, process) + + def get_variables(self, variable_names=None): + ''' get variables from stream as dictionary + + variable_names - optional list of variable names to get + + If variable_names is None, then get all variables in file + ''' + if isinstance(variable_names, str): + variable_names = [variable_names] + elif variable_names is not None: + variable_names = list(variable_names) + + self.mat_stream.seek(0) + # Here we pass all the parameters in self to the reading objects + self.initialize_read() + mdict = self.read_file_header() + mdict['__globals__'] = [] + while not self.end_of_stream(): + hdr, next_position = self.read_var_header() + name = 'None' if hdr.name is None else hdr.name.decode('latin1') + if name in mdict: + msg = ( + f'Duplicate variable name "{name}" in stream' + " - replacing previous with new\nConsider" + "scipy.io.matlab.varmats_from_mat to split " + "file into single variable files" + ) + warnings.warn(msg, MatReadWarning, stacklevel=2) + if name == '': + # can only be a matlab 7 function workspace + name = '__function_workspace__' + # We want to keep this raw because mat_dtype processing + # will break the format (uint8 as mxDOUBLE_CLASS) + process = False + else: + process = True + if variable_names is not None and name not in variable_names: + self.mat_stream.seek(next_position) + continue + try: + res = self.read_var_array(hdr, process) + except MatReadError as err: + warnings.warn( + f'Unreadable variable "{name}", because "{err}"', + Warning, stacklevel=2) + res = f"Read error: {err}" + self.mat_stream.seek(next_position) + mdict[name] = res + if hdr.is_global: + mdict['__globals__'].append(name) + if variable_names is not None: + variable_names.remove(name) + if len(variable_names) == 0: + break + if self.simplify_cells: + return _simplify_cells(mdict) + else: + return mdict + + def list_variables(self): + ''' list variables from stream ''' + self.mat_stream.seek(0) + # Here we pass all the parameters in self to the reading objects + self.initialize_read() + self.read_file_header() + vars = [] + while not self.end_of_stream(): + hdr, next_position = self.read_var_header() + name = 'None' if hdr.name is None else hdr.name.decode('latin1') + if name == '': + # can only be a matlab 7 function workspace + name = '__function_workspace__' + + shape = self._matrix_reader.shape_from_header(hdr) + if hdr.is_logical: + info = 'logical' + else: + info = mclass_info.get(hdr.mclass, 'unknown') + vars.append((name, shape, info)) + + self.mat_stream.seek(next_position) + return vars + + +def varmats_from_mat(file_obj): + """ Pull variables out of mat 5 file as a sequence of mat file objects + + This can be useful with a difficult mat file, containing unreadable + variables. This routine pulls the variables out in raw form and puts them, + unread, back into a file stream for saving or reading. Another use is the + pathological case where there is more than one variable of the same name in + the file; this routine returns the duplicates, whereas the standard reader + will overwrite duplicates in the returned dictionary. + + The file pointer in `file_obj` will be undefined. File pointers for the + returned file-like objects are set at 0. + + Parameters + ---------- + file_obj : file-like + file object containing mat file + + Returns + ------- + named_mats : list + list contains tuples of (name, BytesIO) where BytesIO is a file-like + object containing mat file contents as for a single variable. The + BytesIO contains a string with the original header and a single var. If + ``var_file_obj`` is an individual BytesIO instance, then save as a mat + file with something like ``open('test.mat', + 'wb').write(var_file_obj.read())`` + + Examples + -------- + >>> import scipy.io + >>> import numpy as np + >>> from io import BytesIO + >>> from scipy.io.matlab._mio5 import varmats_from_mat + >>> mat_fileobj = BytesIO() + >>> scipy.io.savemat(mat_fileobj, {'b': np.arange(10), 'a': 'a string'}) + >>> varmats = varmats_from_mat(mat_fileobj) + >>> sorted([name for name, str_obj in varmats]) + ['a', 'b'] + """ + rdr = MatFile5Reader(file_obj) + file_obj.seek(0) + # Raw read of top-level file header + hdr_len = MDTYPES[native_code]['dtypes']['file_header'].itemsize + raw_hdr = file_obj.read(hdr_len) + # Initialize variable reading + file_obj.seek(0) + rdr.initialize_read() + rdr.read_file_header() + next_position = file_obj.tell() + named_mats = [] + while not rdr.end_of_stream(): + start_position = next_position + hdr, next_position = rdr.read_var_header() + name = 'None' if hdr.name is None else hdr.name.decode('latin1') + # Read raw variable string + file_obj.seek(start_position) + byte_count = next_position - start_position + var_str = file_obj.read(byte_count) + # write to stringio object + out_obj = BytesIO() + out_obj.write(raw_hdr) + out_obj.write(var_str) + out_obj.seek(0) + named_mats.append((name, out_obj)) + return named_mats + + +class EmptyStructMarker: + """ Class to indicate presence of empty matlab struct on output """ + + +def to_writeable(source): + ''' Convert input object ``source`` to something we can write + + Parameters + ---------- + source : object + + Returns + ------- + arr : None or ndarray or EmptyStructMarker + If `source` cannot be converted to something we can write to a matfile, + return None. If `source` is equivalent to an empty dictionary, return + ``EmptyStructMarker``. Otherwise return `source` converted to an + ndarray with contents for writing to matfile. + ''' + if isinstance(source, np.ndarray): + return source + if source is None: + return None + if hasattr(source, "__array__"): + return np.asarray(source) + # Objects that implement mappings + is_mapping = (hasattr(source, 'keys') and hasattr(source, 'values') and + hasattr(source, 'items')) + # Objects that don't implement mappings, but do have dicts + if isinstance(source, np.generic): + # NumPy scalars are never mappings (PyPy issue workaround) + pass + elif not is_mapping and hasattr(source, '__dict__'): + source = {key: value for key, value in source.__dict__.items() + if not key.startswith('_')} + is_mapping = True + if is_mapping: + dtype = [] + values = [] + for field, value in source.items(): + if (isinstance(field, str) and + field[0] not in '_0123456789'): + dtype.append((str(field), object)) + values.append(value) + if dtype: + return np.array([tuple(values)], dtype) + else: + return EmptyStructMarker + # Next try and convert to an array + try: + narr = np.asanyarray(source) + except ValueError: + narr = np.asanyarray(source, dtype=object) + if narr.dtype.type in (object, np.object_) and \ + narr.shape == () and narr == source: + # No interesting conversion possible + return None + return narr + + +# Native byte ordered dtypes for convenience for writers +NDT_FILE_HDR = MDTYPES[native_code]['dtypes']['file_header'] +NDT_TAG_FULL = MDTYPES[native_code]['dtypes']['tag_full'] +NDT_TAG_SMALL = MDTYPES[native_code]['dtypes']['tag_smalldata'] +NDT_ARRAY_FLAGS = MDTYPES[native_code]['dtypes']['array_flags'] + + +class VarWriter5: + ''' Generic matlab matrix writing class ''' + mat_tag = np.zeros((), NDT_TAG_FULL) + mat_tag['mdtype'] = miMATRIX # type: ignore[call-overload] + + def __init__(self, file_writer): + self.file_stream = file_writer.file_stream + self.unicode_strings = file_writer.unicode_strings + self.long_field_names = file_writer.long_field_names + self.oned_as = file_writer.oned_as + # These are used for top level writes, and unset after + self._var_name = None + self._var_is_global = False + + def write_bytes(self, arr): + self.file_stream.write(arr.tobytes(order='F')) + + def write_string(self, s): + self.file_stream.write(s) + + def write_element(self, arr, mdtype=None): + ''' write tag and data ''' + if mdtype is None: + mdtype = NP_TO_MTYPES[arr.dtype.str[1:]] + # Array needs to be in native byte order + if arr.dtype.byteorder == swapped_code: + arr = arr.byteswap().view(arr.dtype.newbyteorder()) + byte_count = arr.size*arr.itemsize + if byte_count <= 4: + self.write_smalldata_element(arr, mdtype, byte_count) + else: + self.write_regular_element(arr, mdtype, byte_count) + + def write_smalldata_element(self, arr, mdtype, byte_count): + # write tag with embedded data + tag = np.zeros((), NDT_TAG_SMALL) + tag['byte_count_mdtype'] = (byte_count << 16) + mdtype + # if arr.tobytes is < 4, the element will be zero-padded as needed. + tag['data'] = arr.tobytes(order='F') + self.write_bytes(tag) + + def write_regular_element(self, arr, mdtype, byte_count): + # write tag, data + tag = np.zeros((), NDT_TAG_FULL) + tag['mdtype'] = mdtype + tag['byte_count'] = byte_count + self.write_bytes(tag) + self.write_bytes(arr) + # pad to next 64-bit boundary + bc_mod_8 = byte_count % 8 + if bc_mod_8: + self.file_stream.write(b'\x00' * (8-bc_mod_8)) + + def write_header(self, + shape, + mclass, + is_complex=False, + is_logical=False, + nzmax=0): + ''' Write header for given data options + shape : sequence + array shape + mclass - mat5 matrix class + is_complex - True if matrix is complex + is_logical - True if matrix is logical + nzmax - max non zero elements for sparse arrays + + We get the name and the global flag from the object, and reset + them to defaults after we've used them + ''' + # get name and is_global from one-shot object store + name = self._var_name + is_global = self._var_is_global + # initialize the top-level matrix tag, store position + self._mat_tag_pos = self.file_stream.tell() + self.write_bytes(self.mat_tag) + # write array flags (complex, global, logical, class, nzmax) + af = np.zeros((), NDT_ARRAY_FLAGS) + af['data_type'] = miUINT32 + af['byte_count'] = 8 + flags = is_complex << 3 | is_global << 2 | is_logical << 1 + af['flags_class'] = mclass | flags << 8 + af['nzmax'] = nzmax + self.write_bytes(af) + # shape + self.write_element(np.array(shape, dtype='i4')) + # write name + name = np.asarray(name) + if name == '': # empty string zero-terminated + self.write_smalldata_element(name, miINT8, 0) + else: + self.write_element(name, miINT8) + # reset the one-shot store to defaults + self._var_name = '' + self._var_is_global = False + + def update_matrix_tag(self, start_pos): + curr_pos = self.file_stream.tell() + self.file_stream.seek(start_pos) + byte_count = curr_pos - start_pos - 8 + if byte_count >= 2**32: + raise MatWriteError("Matrix too large to save with Matlab " + "5 format") + self.mat_tag['byte_count'] = byte_count + self.write_bytes(self.mat_tag) + self.file_stream.seek(curr_pos) + + def write_top(self, arr, name, is_global): + """ Write variable at top level of mat file + + Parameters + ---------- + arr : array_like + array-like object to create writer for + name : str, optional + name as it will appear in matlab workspace + default is empty string + is_global : {False, True}, optional + whether variable will be global on load into matlab + """ + # these are set before the top-level header write, and unset at + # the end of the same write, because they do not apply for lower levels + self._var_is_global = is_global + self._var_name = name + # write the header and data + self.write(arr) + + def write(self, arr): + ''' Write `arr` to stream at top and sub levels + + Parameters + ---------- + arr : array_like + array-like object to create writer for + ''' + # store position, so we can update the matrix tag + mat_tag_pos = self.file_stream.tell() + # First check if these are sparse + if scipy.sparse.issparse(arr): + self.write_sparse(arr) + self.update_matrix_tag(mat_tag_pos) + return + # Try to convert things that aren't arrays + narr = to_writeable(arr) + if narr is None: + raise TypeError(f'Could not convert {arr} (type {type(arr)}) to array') + if isinstance(narr, MatlabObject): + self.write_object(narr) + elif isinstance(narr, MatlabFunction): + raise MatWriteError('Cannot write matlab functions') + elif narr is EmptyStructMarker: # empty struct array + self.write_empty_struct() + elif narr.dtype.fields: # struct array + self.write_struct(narr) + elif narr.dtype.hasobject: # cell array + self.write_cells(narr) + elif narr.dtype.kind in ('U', 'S'): + if self.unicode_strings: + codec = 'UTF8' + else: + codec = 'ascii' + self.write_char(narr, codec) + else: + self.write_numeric(narr) + self.update_matrix_tag(mat_tag_pos) + + def write_numeric(self, arr): + imagf = arr.dtype.kind == 'c' + logif = arr.dtype.kind == 'b' + try: + mclass = NP_TO_MXTYPES[arr.dtype.str[1:]] + except KeyError: + # No matching matlab type, probably complex256 / float128 / float96 + # Cast data to complex128 / float64. + if imagf: + arr = arr.astype('c128') + elif logif: + arr = arr.astype('i1') # Should only contain 0/1 + else: + arr = arr.astype('f8') + mclass = mxDOUBLE_CLASS + self.write_header(matdims(arr, self.oned_as), + mclass, + is_complex=imagf, + is_logical=logif) + if imagf: + self.write_element(arr.real) + self.write_element(arr.imag) + else: + self.write_element(arr) + + def write_char(self, arr, codec='ascii'): + ''' Write string array `arr` with given `codec` + ''' + if arr.size == 0 or np.all(arr == ''): + # This an empty string array or a string array containing + # only empty strings. Matlab cannot distinguish between a + # string array that is empty, and a string array containing + # only empty strings, because it stores strings as arrays of + # char. There is no way of having an array of char that is + # not empty, but contains an empty string. We have to + # special-case the array-with-empty-strings because even + # empty strings have zero padding, which would otherwise + # appear in matlab as a string with a space. + shape = (0,) * np.max([arr.ndim, 2]) + self.write_header(shape, mxCHAR_CLASS) + self.write_smalldata_element(arr, miUTF8, 0) + return + # non-empty string. + # + # Convert to char array + arr = arr_to_chars(arr) + # We have to write the shape directly, because we are going + # recode the characters, and the resulting stream of chars + # may have a different length + shape = arr.shape + self.write_header(shape, mxCHAR_CLASS) + if arr.dtype.kind == 'U' and arr.size: + # Make one long string from all the characters. We need to + # transpose here, because we're flattening the array, before + # we write the bytes. The bytes have to be written in + # Fortran order. + n_chars = math.prod(shape) + st_arr = np.ndarray(shape=(), + dtype=arr_dtype_number(arr, n_chars), + buffer=arr.T.copy()) # Fortran order + # Recode with codec to give byte string + st = st_arr.item().encode(codec) + # Reconstruct as 1-D byte array + arr = np.ndarray(shape=(len(st),), + dtype='S1', + buffer=st) + self.write_element(arr, mdtype=miUTF8) + + def write_sparse(self, arr): + ''' Sparse matrices are 2D + ''' + A = arr.tocsc() # convert to sparse CSC format + A.sort_indices() # MATLAB expects sorted row indices + is_complex = (A.dtype.kind == 'c') + is_logical = (A.dtype.kind == 'b') + nz = A.nnz + self.write_header(matdims(arr, self.oned_as), + mxSPARSE_CLASS, + is_complex=is_complex, + is_logical=is_logical, + # matlab won't load file with 0 nzmax + nzmax=1 if nz == 0 else nz) + self.write_element(A.indices.astype('i4')) + self.write_element(A.indptr.astype('i4')) + self.write_element(A.data.real) + if is_complex: + self.write_element(A.data.imag) + + def write_cells(self, arr): + self.write_header(matdims(arr, self.oned_as), + mxCELL_CLASS) + # loop over data, column major + A = np.atleast_2d(arr).flatten('F') + for el in A: + self.write(el) + + def write_empty_struct(self): + self.write_header((1, 1), mxSTRUCT_CLASS) + # max field name length set to 1 in an example matlab struct + self.write_element(np.array(1, dtype=np.int32)) + # Field names element is empty + self.write_element(np.array([], dtype=np.int8)) + + def write_struct(self, arr): + self.write_header(matdims(arr, self.oned_as), + mxSTRUCT_CLASS) + self._write_items(arr) + + def _write_items(self, arr): + # write fieldnames + fieldnames = [f[0] for f in arr.dtype.descr] + length = max([len(fieldname) for fieldname in fieldnames])+1 + max_length = (self.long_field_names and 64) or 32 + if length > max_length: + raise ValueError("Field names are restricted to %d characters" % + (max_length-1)) + self.write_element(np.array([length], dtype='i4')) + self.write_element( + np.array(fieldnames, dtype='S%d' % (length)), + mdtype=miINT8) + A = np.atleast_2d(arr).flatten('F') + for el in A: + for f in fieldnames: + self.write(el[f]) + + def write_object(self, arr): + '''Same as writing structs, except different mx class, and extra + classname element after header + ''' + self.write_header(matdims(arr, self.oned_as), + mxOBJECT_CLASS) + self.write_element(np.array(arr.classname, dtype='S'), + mdtype=miINT8) + self._write_items(arr) + + +class MatFile5Writer: + ''' Class for writing mat5 files ''' + + @docfiller + def __init__(self, file_stream, + do_compression=False, + unicode_strings=False, + global_vars=None, + long_field_names=False, + oned_as='row'): + ''' Initialize writer for matlab 5 format files + + Parameters + ---------- + %(do_compression)s + %(unicode_strings)s + global_vars : None or sequence of strings, optional + Names of variables to be marked as global for matlab + %(long_fields)s + %(oned_as)s + ''' + self.file_stream = file_stream + self.do_compression = do_compression + self.unicode_strings = unicode_strings + if global_vars: + self.global_vars = global_vars + else: + self.global_vars = [] + self.long_field_names = long_field_names + self.oned_as = oned_as + self._matrix_writer = None + + def write_file_header(self): + # write header + hdr = np.zeros((), NDT_FILE_HDR) + hdr['description'] = (f'MATLAB 5.0 MAT-file Platform: {os.name}, ' + f'Created on: {time.asctime()}') + hdr['version'] = 0x0100 + hdr['endian_test'] = np.ndarray(shape=(), + dtype='S2', + buffer=np.uint16(0x4d49)) + self.file_stream.write(hdr.tobytes()) + + def put_variables(self, mdict, write_header=None): + ''' Write variables in `mdict` to stream + + Parameters + ---------- + mdict : mapping + mapping with method ``items`` returns name, contents pairs where + ``name`` which will appear in the matlab workspace in file load, and + ``contents`` is something writeable to a matlab file, such as a NumPy + array. + write_header : {None, True, False}, optional + If True, then write the matlab file header before writing the + variables. If None (the default) then write the file header + if we are at position 0 in the stream. By setting False + here, and setting the stream position to the end of the file, + you can append variables to a matlab file + ''' + # write header if requested, or None and start of file + if write_header is None: + write_header = self.file_stream.tell() == 0 + if write_header: + self.write_file_header() + self._matrix_writer = VarWriter5(self) + for name, var in mdict.items(): + if name[0] == '_': + continue + is_global = name in self.global_vars + if self.do_compression: + stream = BytesIO() + self._matrix_writer.file_stream = stream + self._matrix_writer.write_top(var, name.encode('latin1'), is_global) + out_str = zlib.compress(stream.getvalue()) + tag = np.empty((), NDT_TAG_FULL) + tag['mdtype'] = miCOMPRESSED + tag['byte_count'] = len(out_str) + self.file_stream.write(tag.tobytes()) + self.file_stream.write(out_str) + else: # not compressing + self._matrix_writer.write_top(var, name.encode('latin1'), is_global) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/_mio5_params.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/_mio5_params.py new file mode 100644 index 0000000000000000000000000000000000000000..0d60b8e7a4a2dd1e6a336139f67ce984743e27bb --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/_mio5_params.py @@ -0,0 +1,281 @@ +''' Constants and classes for matlab 5 read and write + +See also mio5_utils.pyx where these same constants arise as c enums. + +If you make changes in this file, don't forget to change mio5_utils.pyx +''' +import numpy as np + +from ._miobase import convert_dtypes + + +__all__ = [ + 'MDTYPES', 'MatlabFunction', 'MatlabObject', 'MatlabOpaque', + 'NP_TO_MTYPES', 'NP_TO_MXTYPES', 'OPAQUE_DTYPE', 'codecs_template', + 'mat_struct', 'mclass_dtypes_template', 'mclass_info', 'mdtypes_template', + 'miCOMPRESSED', 'miDOUBLE', 'miINT16', 'miINT32', 'miINT64', 'miINT8', + 'miMATRIX', 'miSINGLE', 'miUINT16', 'miUINT32', 'miUINT64', 'miUINT8', + 'miUTF16', 'miUTF32', 'miUTF8', 'mxCELL_CLASS', 'mxCHAR_CLASS', + 'mxDOUBLE_CLASS', 'mxFUNCTION_CLASS', 'mxINT16_CLASS', 'mxINT32_CLASS', + 'mxINT64_CLASS', 'mxINT8_CLASS', 'mxOBJECT_CLASS', + 'mxOBJECT_CLASS_FROM_MATRIX_H', 'mxOPAQUE_CLASS', 'mxSINGLE_CLASS', + 'mxSPARSE_CLASS', 'mxSTRUCT_CLASS', 'mxUINT16_CLASS', 'mxUINT32_CLASS', + 'mxUINT64_CLASS', 'mxUINT8_CLASS' +] +miINT8 = 1 +miUINT8 = 2 +miINT16 = 3 +miUINT16 = 4 +miINT32 = 5 +miUINT32 = 6 +miSINGLE = 7 +miDOUBLE = 9 +miINT64 = 12 +miUINT64 = 13 +miMATRIX = 14 +miCOMPRESSED = 15 +miUTF8 = 16 +miUTF16 = 17 +miUTF32 = 18 + +mxCELL_CLASS = 1 +mxSTRUCT_CLASS = 2 +# The March 2008 edition of "Matlab 7 MAT-File Format" says that +# mxOBJECT_CLASS = 3, whereas matrix.h says that mxLOGICAL = 3. +# Matlab 2008a appears to save logicals as type 9, so we assume that +# the document is correct. See type 18, below. +mxOBJECT_CLASS = 3 +mxCHAR_CLASS = 4 +mxSPARSE_CLASS = 5 +mxDOUBLE_CLASS = 6 +mxSINGLE_CLASS = 7 +mxINT8_CLASS = 8 +mxUINT8_CLASS = 9 +mxINT16_CLASS = 10 +mxUINT16_CLASS = 11 +mxINT32_CLASS = 12 +mxUINT32_CLASS = 13 +# The following are not in the March 2008 edition of "Matlab 7 +# MAT-File Format," but were guessed from matrix.h. +mxINT64_CLASS = 14 +mxUINT64_CLASS = 15 +mxFUNCTION_CLASS = 16 +# Not doing anything with these at the moment. +mxOPAQUE_CLASS = 17 # This appears to be a function workspace +# Thread 'saving/loading symbol table of annymous functions', +# octave-maintainers, April-May 2007 +# https://lists.gnu.org/archive/html/octave-maintainers/2007-04/msg00031.html +# https://lists.gnu.org/archive/html/octave-maintainers/2007-05/msg00032.html +# (Was/Deprecated: https://www-old.cae.wisc.edu/pipermail/octave-maintainers/2007-May/002824.html) +mxOBJECT_CLASS_FROM_MATRIX_H = 18 + +mdtypes_template = { + miINT8: 'i1', + miUINT8: 'u1', + miINT16: 'i2', + miUINT16: 'u2', + miINT32: 'i4', + miUINT32: 'u4', + miSINGLE: 'f4', + miDOUBLE: 'f8', + miINT64: 'i8', + miUINT64: 'u8', + miUTF8: 'u1', + miUTF16: 'u2', + miUTF32: 'u4', + 'file_header': [('description', 'S116'), + ('subsystem_offset', 'i8'), + ('version', 'u2'), + ('endian_test', 'S2')], + 'tag_full': [('mdtype', 'u4'), ('byte_count', 'u4')], + 'tag_smalldata':[('byte_count_mdtype', 'u4'), ('data', 'S4')], + 'array_flags': [('data_type', 'u4'), + ('byte_count', 'u4'), + ('flags_class','u4'), + ('nzmax', 'u4')], + 'U1': 'U1', + } + +mclass_dtypes_template = { + mxINT8_CLASS: 'i1', + mxUINT8_CLASS: 'u1', + mxINT16_CLASS: 'i2', + mxUINT16_CLASS: 'u2', + mxINT32_CLASS: 'i4', + mxUINT32_CLASS: 'u4', + mxINT64_CLASS: 'i8', + mxUINT64_CLASS: 'u8', + mxSINGLE_CLASS: 'f4', + mxDOUBLE_CLASS: 'f8', + } + +mclass_info = { + mxINT8_CLASS: 'int8', + mxUINT8_CLASS: 'uint8', + mxINT16_CLASS: 'int16', + mxUINT16_CLASS: 'uint16', + mxINT32_CLASS: 'int32', + mxUINT32_CLASS: 'uint32', + mxINT64_CLASS: 'int64', + mxUINT64_CLASS: 'uint64', + mxSINGLE_CLASS: 'single', + mxDOUBLE_CLASS: 'double', + mxCELL_CLASS: 'cell', + mxSTRUCT_CLASS: 'struct', + mxOBJECT_CLASS: 'object', + mxCHAR_CLASS: 'char', + mxSPARSE_CLASS: 'sparse', + mxFUNCTION_CLASS: 'function', + mxOPAQUE_CLASS: 'opaque', + } + +NP_TO_MTYPES = { + 'f8': miDOUBLE, + 'c32': miDOUBLE, + 'c24': miDOUBLE, + 'c16': miDOUBLE, + 'f4': miSINGLE, + 'c8': miSINGLE, + 'i8': miINT64, + 'i4': miINT32, + 'i2': miINT16, + 'i1': miINT8, + 'u8': miUINT64, + 'u4': miUINT32, + 'u2': miUINT16, + 'u1': miUINT8, + 'S1': miUINT8, + 'U1': miUTF16, + 'b1': miUINT8, # not standard but seems MATLAB uses this (gh-4022) + } + + +NP_TO_MXTYPES = { + 'f8': mxDOUBLE_CLASS, + 'c32': mxDOUBLE_CLASS, + 'c24': mxDOUBLE_CLASS, + 'c16': mxDOUBLE_CLASS, + 'f4': mxSINGLE_CLASS, + 'c8': mxSINGLE_CLASS, + 'i8': mxINT64_CLASS, + 'i4': mxINT32_CLASS, + 'i2': mxINT16_CLASS, + 'i1': mxINT8_CLASS, + 'u8': mxUINT64_CLASS, + 'u4': mxUINT32_CLASS, + 'u2': mxUINT16_CLASS, + 'u1': mxUINT8_CLASS, + 'S1': mxUINT8_CLASS, + 'b1': mxUINT8_CLASS, # not standard but seems MATLAB uses this + } + +''' Before release v7.1 (release 14) matlab (TM) used the system +default character encoding scheme padded out to 16-bits. Release 14 +and later use Unicode. When saving character data, R14 checks if it +can be encoded in 7-bit ascii, and saves in that format if so.''' + +codecs_template = { + miUTF8: {'codec': 'utf_8', 'width': 1}, + miUTF16: {'codec': 'utf_16', 'width': 2}, + miUTF32: {'codec': 'utf_32','width': 4}, + } + + +def _convert_codecs(template, byte_order): + ''' Convert codec template mapping to byte order + + Set codecs not on this system to None + + Parameters + ---------- + template : mapping + key, value are respectively codec name, and root name for codec + (without byte order suffix) + byte_order : {'<', '>'} + code for little or big endian + + Returns + ------- + codecs : dict + key, value are name, codec (as in .encode(codec)) + ''' + codecs = {} + postfix = byte_order == '<' and '_le' or '_be' + for k, v in template.items(): + codec = v['codec'] + try: + " ".encode(codec) + except LookupError: + codecs[k] = None + continue + if v['width'] > 1: + codec += postfix + codecs[k] = codec + return codecs.copy() + + +MDTYPES = {} +for _bytecode in '<>': + _def = {'dtypes': convert_dtypes(mdtypes_template, _bytecode), + 'classes': convert_dtypes(mclass_dtypes_template, _bytecode), + 'codecs': _convert_codecs(codecs_template, _bytecode)} + MDTYPES[_bytecode] = _def + + +class mat_struct: + """Placeholder for holding read data from structs. + + We use instances of this class when the user passes False as a value to the + ``struct_as_record`` parameter of the :func:`scipy.io.loadmat` function. + """ + pass + + +class MatlabObject(np.ndarray): + """Subclass of ndarray to signal this is a matlab object. + + This is a simple subclass of :class:`numpy.ndarray` meant to be used + by :func:`scipy.io.loadmat` and should not be instantiated directly. + """ + + def __new__(cls, input_array, classname=None): + # Input array is an already formed ndarray instance + # We first cast to be our class type + obj = np.asarray(input_array).view(cls) + # add the new attribute to the created instance + obj.classname = classname + # Finally, we must return the newly created object: + return obj + + def __array_finalize__(self,obj): + # reset the attribute from passed original object + self.classname = getattr(obj, 'classname', None) + # We do not need to return anything + + +class MatlabFunction(np.ndarray): + """Subclass for a MATLAB function. + + This is a simple subclass of :class:`numpy.ndarray` meant to be used + by :func:`scipy.io.loadmat` and should not be directly instantiated. + """ + + def __new__(cls, input_array): + obj = np.asarray(input_array).view(cls) + return obj + + +class MatlabOpaque(np.ndarray): + """Subclass for a MATLAB opaque matrix. + + This is a simple subclass of :class:`numpy.ndarray` meant to be used + by :func:`scipy.io.loadmat` and should not be directly instantiated. + """ + + def __new__(cls, input_array): + obj = np.asarray(input_array).view(cls) + return obj + + +OPAQUE_DTYPE = np.dtype( + [('s0', 'O'), ('s1', 'O'), ('s2', 'O'), ('arr', 'O')]) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/_mio_utils.cpython-310-x86_64-linux-gnu.so b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/_mio_utils.cpython-310-x86_64-linux-gnu.so new file mode 100644 index 0000000000000000000000000000000000000000..fc8a904d06026119fb14be7ced171f184d44e58b Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/_mio_utils.cpython-310-x86_64-linux-gnu.so differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/_miobase.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/_miobase.py new file mode 100644 index 0000000000000000000000000000000000000000..1ad7fd7395bb67cc2926b03ed7a6002dc4f9e3f6 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/_miobase.py @@ -0,0 +1,432 @@ +# Authors: Travis Oliphant, Matthew Brett + +""" +Base classes for MATLAB file stream reading. + +MATLAB is a registered trademark of the Mathworks inc. +""" + +from typing import Final + +import numpy as np +from scipy._lib import doccer + +from . import _byteordercodes as boc + +__all__ = [ + 'MatReadError', 'MatReadWarning', 'MatWriteError', +] + +class MatReadError(Exception): + """Exception indicating a read issue.""" + + +class MatWriteError(Exception): + """Exception indicating a write issue.""" + + +class MatReadWarning(UserWarning): + """Warning class for read issues.""" + + +doc_dict = \ + {'file_arg': + '''file_name : str + Name of the mat file (do not need .mat extension if + appendmat==True) Can also pass open file-like object.''', + 'append_arg': + '''appendmat : bool, optional + True to append the .mat extension to the end of the given + filename, if not already present. Default is True.''', + 'load_args': + '''byte_order : str or None, optional + None by default, implying byte order guessed from mat + file. Otherwise can be one of ('native', '=', 'little', '<', + 'BIG', '>'). +mat_dtype : bool, optional + If True, return arrays in same dtype as would be loaded into + MATLAB (instead of the dtype with which they are saved). +squeeze_me : bool, optional + Whether to squeeze unit matrix dimensions or not. +chars_as_strings : bool, optional + Whether to convert char arrays to string arrays. +matlab_compatible : bool, optional + Returns matrices as would be loaded by MATLAB (implies + squeeze_me=False, chars_as_strings=False, mat_dtype=True, + struct_as_record=True).''', + 'struct_arg': + '''struct_as_record : bool, optional + Whether to load MATLAB structs as NumPy record arrays, or as + old-style NumPy arrays with dtype=object. Setting this flag to + False replicates the behavior of SciPy version 0.7.x (returning + numpy object arrays). The default setting is True, because it + allows easier round-trip load and save of MATLAB files.''', + 'matstream_arg': + '''mat_stream : file-like + Object with file API, open for reading.''', + 'long_fields': + '''long_field_names : bool, optional + * False - maximum field name length in a structure is 31 characters + which is the documented maximum length. This is the default. + * True - maximum field name length in a structure is 63 characters + which works for MATLAB 7.6''', + 'do_compression': + '''do_compression : bool, optional + Whether to compress matrices on write. Default is False.''', + 'oned_as': + '''oned_as : {'row', 'column'}, optional + If 'column', write 1-D NumPy arrays as column vectors. + If 'row', write 1D NumPy arrays as row vectors.''', + 'unicode_strings': + '''unicode_strings : bool, optional + If True, write strings as Unicode, else MATLAB usual encoding.'''} + +docfiller: Final = doccer.filldoc(doc_dict) + +''' + + Note on architecture +====================== + +There are three sets of parameters relevant for reading files. The +first are *file read parameters* - containing options that are common +for reading the whole file, and therefore every variable within that +file. At the moment these are: + +* mat_stream +* dtypes (derived from byte code) +* byte_order +* chars_as_strings +* squeeze_me +* struct_as_record (MATLAB 5 files) +* class_dtypes (derived from order code, MATLAB 5 files) +* codecs (MATLAB 5 files) +* uint16_codec (MATLAB 5 files) + +Another set of parameters are those that apply only to the current +variable being read - the *header*: + +* header related variables (different for v4 and v5 mat files) +* is_complex +* mclass +* var_stream + +With the header, we need ``next_position`` to tell us where the next +variable in the stream is. + +Then, for each element in a matrix, there can be *element read +parameters*. An element is, for example, one element in a MATLAB cell +array. At the moment, these are: + +* mat_dtype + +The file-reading object contains the *file read parameters*. The +*header* is passed around as a data object, or may be read and discarded +in a single function. The *element read parameters* - the mat_dtype in +this instance, is passed into a general post-processing function - see +``mio_utils`` for details. +''' + + +def convert_dtypes(dtype_template, order_code): + ''' Convert dtypes in mapping to given order + + Parameters + ---------- + dtype_template : mapping + mapping with values returning numpy dtype from ``np.dtype(val)`` + order_code : str + an order code suitable for using in ``dtype.newbyteorder()`` + + Returns + ------- + dtypes : mapping + mapping where values have been replaced by + ``np.dtype(val).newbyteorder(order_code)`` + + ''' + dtypes = dtype_template.copy() + for k in dtypes: + dtypes[k] = np.dtype(dtypes[k]).newbyteorder(order_code) + return dtypes + + +def read_dtype(mat_stream, a_dtype): + """ + Generic get of byte stream data of known type + + Parameters + ---------- + mat_stream : file_like object + MATLAB (tm) mat file stream + a_dtype : dtype + dtype of array to read. `a_dtype` is assumed to be correct + endianness. + + Returns + ------- + arr : ndarray + Array of dtype `a_dtype` read from stream. + + """ + num_bytes = a_dtype.itemsize + arr = np.ndarray(shape=(), + dtype=a_dtype, + buffer=mat_stream.read(num_bytes), + order='F') + return arr + + +def matfile_version(file_name, *, appendmat=True): + """ + Return major, minor tuple depending on apparent mat file type + + Where: + + #. 0,x -> version 4 format mat files + #. 1,x -> version 5 format mat files + #. 2,x -> version 7.3 format mat files (HDF format) + + Parameters + ---------- + file_name : str + Name of the mat file (do not need .mat extension if + appendmat==True). Can also pass open file-like object. + appendmat : bool, optional + True to append the .mat extension to the end of the given + filename, if not already present. Default is True. + + Returns + ------- + major_version : {0, 1, 2} + major MATLAB File format version + minor_version : int + minor MATLAB file format version + + Raises + ------ + MatReadError + If the file is empty. + ValueError + The matfile version is unknown. + + Notes + ----- + Has the side effect of setting the file read pointer to 0 + """ + from ._mio import _open_file_context + with _open_file_context(file_name, appendmat=appendmat) as fileobj: + return _get_matfile_version(fileobj) + + +get_matfile_version = matfile_version + + +_HDR_N_BYTES = 20 + + +def _get_matfile_version(fileobj): + # Mat4 files have a zero somewhere in first 4 bytes + fileobj.seek(0) + hdr_bytes = fileobj.read(_HDR_N_BYTES) + if len(hdr_bytes) < _HDR_N_BYTES: + raise MatReadError("Mat file appears to be truncated") + if hdr_bytes.count(0) == _HDR_N_BYTES: + raise MatReadError("Mat file appears to be corrupt " + f"(first {_HDR_N_BYTES} bytes == 0)") + mopt_ints = np.ndarray(shape=(4,), dtype=np.uint8, buffer=hdr_bytes[:4]) + if 0 in mopt_ints: + fileobj.seek(0) + return (0,0) + # For 5 format or 7.3 format we need to read an integer in the + # header. Bytes 124 through 128 contain a version integer and an + # endian test string + fileobj.seek(124) + tst_str = fileobj.read(4) + fileobj.seek(0) + maj_ind = int(tst_str[2] == b'I'[0]) + maj_val = int(tst_str[maj_ind]) + min_val = int(tst_str[1 - maj_ind]) + ret = (maj_val, min_val) + if maj_val in (1, 2): + return ret + raise ValueError('Unknown mat file type, version {}, {}'.format(*ret)) + + +def matdims(arr, oned_as='column'): + """ + Determine equivalent MATLAB dimensions for given array + + Parameters + ---------- + arr : ndarray + Input array + oned_as : {'column', 'row'}, optional + Whether 1-D arrays are returned as MATLAB row or column matrices. + Default is 'column'. + + Returns + ------- + dims : tuple + Shape tuple, in the form MATLAB expects it. + + Notes + ----- + We had to decide what shape a 1 dimensional array would be by + default. ``np.atleast_2d`` thinks it is a row vector. The + default for a vector in MATLAB (e.g., ``>> 1:12``) is a row vector. + + Versions of scipy up to and including 0.11 resulted (accidentally) + in 1-D arrays being read as column vectors. For the moment, we + maintain the same tradition here. + + Examples + -------- + >>> import numpy as np + >>> from scipy.io.matlab._miobase import matdims + >>> matdims(np.array(1)) # NumPy scalar + (1, 1) + >>> matdims(np.array([1])) # 1-D array, 1 element + (1, 1) + >>> matdims(np.array([1,2])) # 1-D array, 2 elements + (2, 1) + >>> matdims(np.array([[2],[3]])) # 2-D array, column vector + (2, 1) + >>> matdims(np.array([[2,3]])) # 2-D array, row vector + (1, 2) + >>> matdims(np.array([[[2,3]]])) # 3-D array, rowish vector + (1, 1, 2) + >>> matdims(np.array([])) # empty 1-D array + (0, 0) + >>> matdims(np.array([[]])) # empty 2-D array + (0, 0) + >>> matdims(np.array([[[]]])) # empty 3-D array + (0, 0, 0) + + Optional argument flips 1-D shape behavior. + + >>> matdims(np.array([1,2]), 'row') # 1-D array, 2 elements + (1, 2) + + The argument has to make sense though + + >>> matdims(np.array([1,2]), 'bizarre') + Traceback (most recent call last): + ... + ValueError: 1-D option "bizarre" is strange + + """ + shape = arr.shape + if shape == (): # scalar + return (1, 1) + if len(shape) == 1: # 1D + if shape[0] == 0: + return (0, 0) + elif oned_as == 'column': + return shape + (1,) + elif oned_as == 'row': + return (1,) + shape + else: + raise ValueError(f'1-D option "{oned_as}" is strange') + return shape + + +class MatVarReader: + ''' Abstract class defining required interface for var readers''' + def __init__(self, file_reader): + pass + + def read_header(self): + ''' Returns header ''' + pass + + def array_from_header(self, header): + ''' Reads array given header ''' + pass + + +class MatFileReader: + """ Base object for reading mat files + + To make this class functional, you will need to override the + following methods: + + matrix_getter_factory - gives object to fetch next matrix from stream + guess_byte_order - guesses file byte order from file + """ + + @docfiller + def __init__(self, mat_stream, + byte_order=None, + mat_dtype=False, + squeeze_me=False, + chars_as_strings=True, + matlab_compatible=False, + struct_as_record=True, + verify_compressed_data_integrity=True, + simplify_cells=False): + ''' + Initializer for mat file reader + + mat_stream : file-like + object with file API, open for reading + %(load_args)s + ''' + # Initialize stream + self.mat_stream = mat_stream + self.dtypes = {} + if not byte_order: + byte_order = self.guess_byte_order() + else: + byte_order = boc.to_numpy_code(byte_order) + self.byte_order = byte_order + self.struct_as_record = struct_as_record + if matlab_compatible: + self.set_matlab_compatible() + else: + self.squeeze_me = squeeze_me + self.chars_as_strings = chars_as_strings + self.mat_dtype = mat_dtype + self.verify_compressed_data_integrity = verify_compressed_data_integrity + self.simplify_cells = simplify_cells + if simplify_cells: + self.squeeze_me = True + self.struct_as_record = False + + def set_matlab_compatible(self): + ''' Sets options to return arrays as MATLAB loads them ''' + self.mat_dtype = True + self.squeeze_me = False + self.chars_as_strings = False + + def guess_byte_order(self): + ''' As we do not know what file type we have, assume native ''' + return boc.native_code + + def end_of_stream(self): + b = self.mat_stream.read(1) + curpos = self.mat_stream.tell() + self.mat_stream.seek(curpos-1) + return len(b) == 0 + + +def arr_dtype_number(arr, num): + ''' Return dtype for given number of items per element''' + return np.dtype(arr.dtype.str[:2] + str(num)) + + +def arr_to_chars(arr): + ''' Convert string array to char array ''' + dims = list(arr.shape) + if not dims: + dims = [1] + dims.append(int(arr.dtype.str[2:])) + arr = np.ndarray(shape=dims, + dtype=arr_dtype_number(arr, 1), + buffer=arr) + empties = [arr == np.array('', dtype=arr.dtype)] + if not np.any(empties): + return arr + arr = arr.copy() + arr[tuple(empties)] = ' ' + return arr diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/byteordercodes.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/byteordercodes.py new file mode 100644 index 0000000000000000000000000000000000000000..0a1c5b0f5e77fdd461d6085037bfdf2850f40fa0 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/byteordercodes.py @@ -0,0 +1,17 @@ +# This file is not meant for public use and will be removed in SciPy v2.0.0. +# Use the `scipy.io.matlab` namespace for importing the functions +# included below. + +from scipy._lib.deprecation import _sub_module_deprecation + +__all__: list[str] = [] + + +def __dir__(): + return __all__ + + +def __getattr__(name): + return _sub_module_deprecation(sub_package="io.matlab", module="byteordercodes", + private_modules=["_byteordercodes"], all=__all__, + attribute=name) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/mio.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/mio.py new file mode 100644 index 0000000000000000000000000000000000000000..65bb31e52dc719b485b12ba1294fc3d09806c9d0 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/mio.py @@ -0,0 +1,16 @@ +# This file is not meant for public use and will be removed in SciPy v2.0.0. +# Use the `scipy.io.matlab` namespace for importing the functions +# included below. + +from scipy._lib.deprecation import _sub_module_deprecation + +__all__ = ["loadmat", "savemat", "whosmat"] # noqa: F822 + +def __dir__(): + return __all__ + + +def __getattr__(name): + return _sub_module_deprecation(sub_package="io.matlab", module="mio", + private_modules=["_mio"], all=__all__, + attribute=name) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/mio4.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/mio4.py new file mode 100644 index 0000000000000000000000000000000000000000..d13b99a0bcedc9746f7681843989791e0918df2e --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/mio4.py @@ -0,0 +1,17 @@ +# This file is not meant for public use and will be removed in SciPy v2.0.0. +# Use the `scipy.io.matlab` namespace for importing the functions +# included below. + +from scipy._lib.deprecation import _sub_module_deprecation + +__all__: list[str] = [] + + +def __dir__(): + return __all__ + + +def __getattr__(name): + return _sub_module_deprecation(sub_package="io.matlab", module="mio4", + private_modules=["_mio4"], all=__all__, + attribute=name) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/mio5.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/mio5.py new file mode 100644 index 0000000000000000000000000000000000000000..b84ca19799b32999032833b4e1be1b21f6bc70da --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/mio5.py @@ -0,0 +1,19 @@ +# This file is not meant for public use and will be removed in SciPy v2.0.0. +# Use the `scipy.io.matlab` namespace for importing the functions +# included below. + +from scipy._lib.deprecation import _sub_module_deprecation + +__all__ = [ # noqa: F822 + 'MatWriteError', 'MatReadError', 'MatReadWarning', 'MatlabObject', + 'MatlabFunction', 'mat_struct', 'varmats_from_mat', +] + +def __dir__(): + return __all__ + + +def __getattr__(name): + return _sub_module_deprecation(sub_package="io.matlab", module="mio5", + private_modules=["_mio5"], all=__all__, + attribute=name) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/mio5_params.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/mio5_params.py new file mode 100644 index 0000000000000000000000000000000000000000..2dcc9a4f353794546f0d8c07f9afe369baa992f5 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/mio5_params.py @@ -0,0 +1,18 @@ +# This file is not meant for public use and will be removed in SciPy v2.0.0. +# Use the `scipy.io.matlab` namespace for importing the functions +# included below. + +from scipy._lib.deprecation import _sub_module_deprecation + +__all__ = [ # noqa: F822 + 'MatlabFunction', 'MatlabObject', 'MatlabOpaque', 'mat_struct', +] + +def __dir__(): + return __all__ + + +def __getattr__(name): + return _sub_module_deprecation(sub_package="io.matlab", module="mio5_params", + private_modules=["_mio5_params"], all=__all__, + attribute=name) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/mio5_utils.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/mio5_utils.py new file mode 100644 index 0000000000000000000000000000000000000000..37ad9e2dc2f50b85bf5aba517c4ac7d661b5039a --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/mio5_utils.py @@ -0,0 +1,17 @@ +# This file is not meant for public use and will be removed in SciPy v2.0.0. +# Use the `scipy.io.matlab` namespace for importing the functions +# included below. + +from scipy._lib.deprecation import _sub_module_deprecation + +__all__: list[str] = [] + + +def __dir__(): + return __all__ + + +def __getattr__(name): + return _sub_module_deprecation(sub_package="io.matlab", module="mio5_utils", + private_modules=["_mio5_utils"], all=__all__, + attribute=name) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/mio_utils.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/mio_utils.py new file mode 100644 index 0000000000000000000000000000000000000000..6920511d2635b44acd33ce6f5e00247daf6578d9 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/mio_utils.py @@ -0,0 +1,17 @@ +# This file is not meant for public use and will be removed in SciPy v2.0.0. +# Use the `scipy.io.matlab` namespace for importing the functions +# included below. + +from scipy._lib.deprecation import _sub_module_deprecation + +__all__: list[str] = [] + + +def __dir__(): + return __all__ + + +def __getattr__(name): + return _sub_module_deprecation(sub_package="io.matlab", module="mio_utils", + private_modules=["_mio_utils"], all=__all__, + attribute=name) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/miobase.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/miobase.py new file mode 100644 index 0000000000000000000000000000000000000000..13e16848394471f9a1744a7b27fa4e6c86a9248b --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/miobase.py @@ -0,0 +1,16 @@ +# This file is not meant for public use and will be removed in SciPy v2.0.0. +# Use the `scipy.io.matlab` namespace for importing the functions +# included below. + +from scipy._lib.deprecation import _sub_module_deprecation + +__all__ = ["MatReadError", "MatReadWarning", "MatWriteError"] # noqa: F822 + +def __dir__(): + return __all__ + + +def __getattr__(name): + return _sub_module_deprecation(sub_package="io.matlab", module="miobase", + private_modules=["_miobase"], all=__all__, + attribute=name) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/streams.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/streams.py new file mode 100644 index 0000000000000000000000000000000000000000..8125271b06cc6f44cee19b2f6079d26b8f32e268 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/streams.py @@ -0,0 +1,16 @@ +# This file is not meant for public use and will be removed in SciPy v2.0.0. +# Use the `scipy.io.matlab` namespace for importing the functions +# included below. + +from scipy._lib.deprecation import _sub_module_deprecation + +__all__: list[str] = [] + +def __dir__(): + return __all__ + + +def __getattr__(name): + return _sub_module_deprecation(sub_package="io.matlab", module="streams", + private_modules=["_streams"], all=__all__, + attribute=name) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/bad_miuint32.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/bad_miuint32.mat new file mode 100644 index 0000000000000000000000000000000000000000..c9ab357ec85972cf0014752a1e0ccb08ff284af9 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/bad_miuint32.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/bad_miutf8_array_name.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/bad_miutf8_array_name.mat new file mode 100644 index 0000000000000000000000000000000000000000..a17203fbb2a7628db644b953ac7723b866a2a0a4 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/bad_miutf8_array_name.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/big_endian.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/big_endian.mat new file mode 100644 index 0000000000000000000000000000000000000000..2a0c982c298fba9df96fd5a927a9c08ee12b09df Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/big_endian.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/broken_utf8.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/broken_utf8.mat new file mode 100644 index 0000000000000000000000000000000000000000..4f6323870368cd97a6294e108ffea9067cf5e69b Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/broken_utf8.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/corrupted_zlib_checksum.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/corrupted_zlib_checksum.mat new file mode 100644 index 0000000000000000000000000000000000000000..c88cbb6f54b70d4e795de7cf43f7b46ff6d4d5ef Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/corrupted_zlib_checksum.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/corrupted_zlib_data.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/corrupted_zlib_data.mat new file mode 100644 index 0000000000000000000000000000000000000000..45a2ef4e39755ea1f41aab045f18a035af58ea07 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/corrupted_zlib_data.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/debigged_m4.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/debigged_m4.mat new file mode 100644 index 0000000000000000000000000000000000000000..28aad199045d0b3bf31060300aff9231ee6d9a71 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/debigged_m4.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/japanese_utf8.txt b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/japanese_utf8.txt new file mode 100644 index 0000000000000000000000000000000000000000..1459b6b6ea635b17b5eb04c941e197f98cf04bf1 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/japanese_utf8.txt @@ -0,0 +1,5 @@ +Japanese: +すべての人間は、生まれながらにして自由であり、 +かつ、尊厳と権利と について平等である。 +人間は、理性と良心とを授けられており、 +互いに同胞の精神をもって行動しなければならない。 \ No newline at end of file diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/little_endian.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/little_endian.mat new file mode 100644 index 0000000000000000000000000000000000000000..df6db666dcf2b98d66e04933bd4011f649dcbe30 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/little_endian.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/logical_sparse.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/logical_sparse.mat new file mode 100644 index 0000000000000000000000000000000000000000..a60ad5b605a9dc6b0d85eb0a0e3e655c4955dd34 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/logical_sparse.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/malformed1.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/malformed1.mat new file mode 100644 index 0000000000000000000000000000000000000000..54462e27d663770bc33ef73ed70baae65767719d Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/malformed1.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/miuint32_for_miint32.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/miuint32_for_miint32.mat new file mode 100644 index 0000000000000000000000000000000000000000..fd2c4994578edbf31431902ecfcb601b11f60b0b Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/miuint32_for_miint32.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/miutf8_array_name.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/miutf8_array_name.mat new file mode 100644 index 0000000000000000000000000000000000000000..ccfdaa8adb7879ba852eab9ce55b602e11dad06d Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/miutf8_array_name.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/nasty_duplicate_fieldnames.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/nasty_duplicate_fieldnames.mat new file mode 100644 index 0000000000000000000000000000000000000000..35dcb715bca4cb7f4b0dca287648ef8ee797cd73 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/nasty_duplicate_fieldnames.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/one_by_zero_char.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/one_by_zero_char.mat new file mode 100644 index 0000000000000000000000000000000000000000..07e7dca456843004dcfd9023a800ea91d309814d Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/one_by_zero_char.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/parabola.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/parabola.mat new file mode 100644 index 0000000000000000000000000000000000000000..66350532a7737c475a3ae6ef1b1d8406543d890e Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/parabola.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/single_empty_string.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/single_empty_string.mat new file mode 100644 index 0000000000000000000000000000000000000000..293f387719e8bdcacb075e0de5737894e5dafed3 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/single_empty_string.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/some_functions.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/some_functions.mat new file mode 100644 index 0000000000000000000000000000000000000000..cc818593b48dd8d29a40a827210b54373e5acf50 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/some_functions.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/sqr.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/sqr.mat new file mode 100644 index 0000000000000000000000000000000000000000..2436d87cc5dfb6d558b841c2367bfe2363bd1b3c Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/sqr.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/test3dmatrix_6.1_SOL2.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/test3dmatrix_6.1_SOL2.mat new file mode 100644 index 0000000000000000000000000000000000000000..453712610bf46501d8dd3667ff72d8033f49d81c Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/test3dmatrix_6.1_SOL2.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/test3dmatrix_6.5.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/test3dmatrix_6.5.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..e04d27d30378655ed14634330c7a8ddcd0b98c10 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/test3dmatrix_6.5.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/test3dmatrix_7.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/test3dmatrix_7.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..4c0303039826af6f6caa928e505cec10ebb3fa81 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/test3dmatrix_7.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/test3dmatrix_7.4_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/test3dmatrix_7.4_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..232a051c774105176c28c9718c2cd46f1a1ee1af Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/test3dmatrix_7.4_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/test_empty_struct.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/test_empty_struct.mat new file mode 100644 index 0000000000000000000000000000000000000000..30c8c8ad5378be4508bd785da8b7cef38adbd13e Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/test_empty_struct.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/test_mat4_le_floats.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/test_mat4_le_floats.mat new file mode 100644 index 0000000000000000000000000000000000000000..6643c42ddcc9579930980b7eb30e11f339638404 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/test_mat4_le_floats.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/test_skip_variable.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/test_skip_variable.mat new file mode 100644 index 0000000000000000000000000000000000000000..efbe3fec64ee54c9f8b3998e5035ccfa251e74ff Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/test_skip_variable.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testbool_8_WIN64.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testbool_8_WIN64.mat new file mode 100644 index 0000000000000000000000000000000000000000..faa30b10bc61ea4889bd9e776c0a1a079e2c2a90 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testbool_8_WIN64.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcell_6.1_SOL2.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcell_6.1_SOL2.mat new file mode 100644 index 0000000000000000000000000000000000000000..512f7d889420a016094a903585f27acaa50bc658 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcell_6.1_SOL2.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcell_6.5.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcell_6.5.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..a7633104c1e4f32fe30fd43f389d7559527c8211 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcell_6.5.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcell_7.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcell_7.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..2ac1da15873c5edac27758b6f91563d2b8aaace0 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcell_7.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcell_7.4_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcell_7.4_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..fc893f331c985cf17b7ce9b7b8c179eaf2103659 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcell_7.4_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcellnest_6.1_SOL2.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcellnest_6.1_SOL2.mat new file mode 100644 index 0000000000000000000000000000000000000000..4198a4f2aeb8effcccf94a9c0114539f98124179 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcellnest_6.1_SOL2.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcellnest_6.5.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcellnest_6.5.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..2c7826eeacdb456e5290cafba343703c7596d191 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcellnest_6.5.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcellnest_7.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcellnest_7.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..b3b086cc31dce2de1e300a1d018b0bf5661b69f3 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcellnest_7.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcellnest_7.4_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcellnest_7.4_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..316f8894c5ecc88468cfa0908c277f730e3163e8 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcellnest_7.4_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcomplex_4.2c_SOL2.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcomplex_4.2c_SOL2.mat new file mode 100644 index 0000000000000000000000000000000000000000..36621b25c08f18e4545100c6eaec015123c3bf9f Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcomplex_4.2c_SOL2.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcomplex_6.1_SOL2.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcomplex_6.1_SOL2.mat new file mode 100644 index 0000000000000000000000000000000000000000..32fcd2a93c91eff478a3ab3076e5c78e31f09bf1 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcomplex_6.1_SOL2.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcomplex_6.5.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcomplex_6.5.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..f3ecd203376c17b09d97a24aceab824dae0f91c1 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcomplex_6.5.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcomplex_7.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcomplex_7.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..c0c083855f38e62e3a29460b745f198c9c79313d Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcomplex_7.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcomplex_7.4_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcomplex_7.4_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..6a187edb1828256362617d3fe24d26cf58e7ca3b Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testcomplex_7.4_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testdouble_4.2c_SOL2.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testdouble_4.2c_SOL2.mat new file mode 100644 index 0000000000000000000000000000000000000000..5dbfcf17dd0e01dc0325dd009340291158906e8d Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testdouble_4.2c_SOL2.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testdouble_6.1_SOL2.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testdouble_6.1_SOL2.mat new file mode 100644 index 0000000000000000000000000000000000000000..8e36c0c8ce62d7559b60fde454a96e8eefcbcb92 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testdouble_6.1_SOL2.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testdouble_6.5.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testdouble_6.5.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..a003b6d866f77a25d3b8b236bc95e343221e3019 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testdouble_6.5.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testdouble_7.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testdouble_7.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..3106712e1099345b48dc4e4125d5e739c24b5341 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testdouble_7.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testdouble_7.4_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testdouble_7.4_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..9097bb08712d5bfccf172b0366573f503136228d Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testdouble_7.4_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testemptycell_5.3_SOL2.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testemptycell_5.3_SOL2.mat new file mode 100644 index 0000000000000000000000000000000000000000..e7dec3b81abdae8769e0ae0329948548f4038adf Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testemptycell_5.3_SOL2.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testemptycell_6.5.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testemptycell_6.5.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..a1c93483597f364443158132b31b86693891b02a Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testemptycell_6.5.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testemptycell_7.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testemptycell_7.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..f29d4f9327aa906729234a38caa05ebfc50cfc30 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testemptycell_7.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testemptycell_7.4_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testemptycell_7.4_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..8b244044cf3028df9a019a259d8fc533b80f7fb7 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testemptycell_7.4_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testfunc_7.4_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testfunc_7.4_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..adb6c28ee95d1cf8bf3bfeb72295d1a7848020f8 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testfunc_7.4_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testhdf5_7.4_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testhdf5_7.4_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..6066c1e30f69b76afdb8d251ecefd8cd9e1acde5 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testhdf5_7.4_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testmatrix_4.2c_SOL2.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testmatrix_4.2c_SOL2.mat new file mode 100644 index 0000000000000000000000000000000000000000..3698c8853b46d4a42194002523b57fddfb225908 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testmatrix_4.2c_SOL2.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testmatrix_6.1_SOL2.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testmatrix_6.1_SOL2.mat new file mode 100644 index 0000000000000000000000000000000000000000..164be1109d977cf7681b1ea00a5df80d5e8f8e71 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testmatrix_6.1_SOL2.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testmatrix_6.5.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testmatrix_6.5.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..a8735e9a23558ce86a528ceafa8f3475b053e43b Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testmatrix_6.5.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testmatrix_7.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testmatrix_7.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..b6fb05bb7564c863d5bb6c145fe8b06928d3805a Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testmatrix_7.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testmatrix_7.4_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testmatrix_7.4_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..eb537ab1042b0f989d49711b1a36cc508946fe55 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testmatrix_7.4_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testminus_4.2c_SOL2.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testminus_4.2c_SOL2.mat new file mode 100644 index 0000000000000000000000000000000000000000..cc207ed9f32095f39b7690e2dc1e2dc0d55ee8e0 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testminus_4.2c_SOL2.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testminus_6.1_SOL2.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testminus_6.1_SOL2.mat new file mode 100644 index 0000000000000000000000000000000000000000..c2f0ba2ae4c8a1750cace6eae0267e9736272fc0 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testminus_6.1_SOL2.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testminus_6.5.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testminus_6.5.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..b4dbd152d6e9f3d289b3c4a9792729d2735a4c5c Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testminus_6.5.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testminus_7.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testminus_7.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..fadcd2366b1867239782f073291ff327c2af3001 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testminus_7.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testminus_7.4_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testminus_7.4_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..9ce65f91116f68332d1c16e21319e965541d0d73 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testminus_7.4_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testmulti_4.2c_SOL2.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testmulti_4.2c_SOL2.mat new file mode 100644 index 0000000000000000000000000000000000000000..9c6ba793cf41bf36447ab7a1890447fe5e939614 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testmulti_4.2c_SOL2.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testmulti_7.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testmulti_7.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..0c4729c56b6ab1e8945249a4d3144c79d8538e9e Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testmulti_7.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testmulti_7.4_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testmulti_7.4_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..6d3e068977edfe6407f29404f0a7d1737f7d3eba Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testmulti_7.4_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testobject_6.1_SOL2.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testobject_6.1_SOL2.mat new file mode 100644 index 0000000000000000000000000000000000000000..fc13642263a64874f6c2ac602be9cdcb9b788996 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testobject_6.1_SOL2.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testobject_6.5.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testobject_6.5.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..f68323b0c8eb7fc999dead349ea3bd3a6da66bd4 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testobject_6.5.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testobject_7.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testobject_7.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..83dcad34249afa543bf66dae9b836276246aab4a Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testobject_7.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testobject_7.4_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testobject_7.4_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..59d243c4de4fbb3fa653753e40651a6d0a4f4967 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testobject_7.4_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testonechar_4.2c_SOL2.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testonechar_4.2c_SOL2.mat new file mode 100644 index 0000000000000000000000000000000000000000..cdb4191c7d2eb0ac66d4f6add250e1f6a604d892 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testonechar_4.2c_SOL2.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testonechar_6.1_SOL2.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testonechar_6.1_SOL2.mat new file mode 100644 index 0000000000000000000000000000000000000000..3b5a428501a53ae7308c7b6edc42f4881820664d Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testonechar_6.1_SOL2.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testonechar_6.5.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testonechar_6.5.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..8cef2dd7ea6df8aac26ed067a9427935b81c7ac7 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testonechar_6.5.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testonechar_7.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testonechar_7.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..5ba4810ac67756c17b0ef3163a496e913c0b5e57 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testonechar_7.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testonechar_7.4_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testonechar_7.4_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..8964765f7bd207bfab63b4d16569cb1c3763bda7 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testonechar_7.4_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testscalarcell_7.4_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testscalarcell_7.4_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..1dcd72e51a51abdcf48bd37f68b9927421c17cb0 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testscalarcell_7.4_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsimplecell.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsimplecell.mat new file mode 100644 index 0000000000000000000000000000000000000000..2a98f48917f8f275e541eeac5ef1fe741c40bb0b Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsimplecell.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparse_4.2c_SOL2.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparse_4.2c_SOL2.mat new file mode 100644 index 0000000000000000000000000000000000000000..55cbd3c1b3d65630beae47832ffbcc7a6fd43354 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparse_4.2c_SOL2.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparse_6.1_SOL2.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparse_6.1_SOL2.mat new file mode 100644 index 0000000000000000000000000000000000000000..194ca4d7d4d4d22be5669041a25c3ca24ae6edcb Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparse_6.1_SOL2.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparse_6.5.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparse_6.5.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..3e1e9a1ec916040e94c231f428725add10a2709c Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparse_6.5.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparse_7.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparse_7.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..55b510762ee9b0ac04776e38f6b4bb46b0d10021 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparse_7.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparse_7.4_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparse_7.4_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..bdb6ce66ce79b808f044124156db4b803dab155e Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparse_7.4_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparsecomplex_4.2c_SOL2.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparsecomplex_4.2c_SOL2.mat new file mode 100644 index 0000000000000000000000000000000000000000..81c536d0b067b92cae1b7a2ee71824e2c5e730d9 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparsecomplex_4.2c_SOL2.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparsecomplex_6.1_SOL2.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparsecomplex_6.1_SOL2.mat new file mode 100644 index 0000000000000000000000000000000000000000..520e1cedb3823b859666b1fa8872e073904fd4c6 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparsecomplex_6.1_SOL2.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparsecomplex_6.5.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparsecomplex_6.5.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..969b7143dfff3bb817dbf70c54af8303c3b5822e Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparsecomplex_6.5.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparsecomplex_7.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparsecomplex_7.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..9117dce3092e3e6a39b67da9a7ad1dcfc3ded385 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparsecomplex_7.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparsecomplex_7.4_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparsecomplex_7.4_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..a8a615a320f9c8db068a9120c1ceb2e49bb0ea6d Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparsecomplex_7.4_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparsefloat_7.4_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparsefloat_7.4_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..15424266a3bd4aa1e7525a8fdc4945b51d2b5ad6 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testsparsefloat_7.4_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststring_4.2c_SOL2.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststring_4.2c_SOL2.mat new file mode 100644 index 0000000000000000000000000000000000000000..137561e1f636d7b08959e43e969a6984eb7a3b37 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststring_4.2c_SOL2.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststring_6.1_SOL2.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststring_6.1_SOL2.mat new file mode 100644 index 0000000000000000000000000000000000000000..2ad75f2e17d8b3fda285490d52b426d1f27d0d95 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststring_6.1_SOL2.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststring_6.5.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststring_6.5.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..6fd12d884d19df65f1534c13944e988e636166f1 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststring_6.5.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststring_7.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststring_7.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..ab93994f7befe7d1505c84c238d6409bcb3d438a Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststring_7.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststring_7.4_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststring_7.4_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..63059b84476749119f44ebefda795f85f6ab27d7 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststring_7.4_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststringarray_4.2c_SOL2.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststringarray_4.2c_SOL2.mat new file mode 100644 index 0000000000000000000000000000000000000000..fa687ee988ce530bca87f46235667baa30ac038b Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststringarray_4.2c_SOL2.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststringarray_6.1_SOL2.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststringarray_6.1_SOL2.mat new file mode 100644 index 0000000000000000000000000000000000000000..11afb412056ad803f0d8ac1d9dcb188d42285fdf Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststringarray_6.1_SOL2.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststringarray_6.5.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststringarray_6.5.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..75e07a0b55e008b070f41dabba7480a4e463b67a Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststringarray_6.5.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststringarray_7.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststringarray_7.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..7d76f63643737834053f80539188c9dad75ed0cb Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststringarray_7.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststringarray_7.4_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststringarray_7.4_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..954e39beb8156b460ca904ff66261d8f2fc338cb Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststringarray_7.4_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststruct_6.1_SOL2.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststruct_6.1_SOL2.mat new file mode 100644 index 0000000000000000000000000000000000000000..5086bb7acdc3773186e903000aace436c90dc565 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststruct_6.1_SOL2.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststruct_6.5.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststruct_6.5.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..6feb6e42375ebebf6dd9440ee09312204cbf1a33 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststruct_6.5.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststruct_7.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststruct_7.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..b2ff2226223181ec5c42d36afe4f56728f25972d Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststruct_7.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststruct_7.4_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststruct_7.4_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..028841f9d3aae42d6cf782db14634cbe375f0a05 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststruct_7.4_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststructarr_6.1_SOL2.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststructarr_6.1_SOL2.mat new file mode 100644 index 0000000000000000000000000000000000000000..da57365926afe1e8d7dd424a6fcd5b52bc3233ac Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststructarr_6.1_SOL2.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststructarr_6.5.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststructarr_6.5.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..d1c97a7a2e1edf9683959ec36e899ef8e355073c Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststructarr_6.5.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststructarr_7.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststructarr_7.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..c7ca09594106a765e815a55e942019d17c181270 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststructarr_7.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststructarr_7.4_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststructarr_7.4_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..8716f7e3db67d1fd479f913d12286715029ed1a4 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststructarr_7.4_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststructnest_6.1_SOL2.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststructnest_6.1_SOL2.mat new file mode 100644 index 0000000000000000000000000000000000000000..2c34c4d8c1477bc4859880a8d2f800073825dcd1 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststructnest_6.1_SOL2.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststructnest_6.5.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststructnest_6.5.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..c6dccc00289f61787b235f4299aa5a14ab4f6d07 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststructnest_6.5.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststructnest_7.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststructnest_7.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..0f6f5444b0c1e4bcd80dc0f63b28523d655b05d0 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststructnest_7.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststructnest_7.4_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststructnest_7.4_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..faf9221b776eee67cd5d2971da5ba77732ef8016 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/teststructnest_7.4_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testunicode_7.1_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testunicode_7.1_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..1b7b3d7f002080839f672e4eb858bbfbddda27ec Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testunicode_7.1_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testunicode_7.4_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testunicode_7.4_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..d22fb57c81fc3ec9ee7e9b447a05e8a89ff1fcfe Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testunicode_7.4_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testvec_4_GLNX86.mat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testvec_4_GLNX86.mat new file mode 100644 index 0000000000000000000000000000000000000000..76c51d01388a1770b348bc603ebfdd51bc011f0c Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/data/testvec_4_GLNX86.mat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/test_byteordercodes.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/test_byteordercodes.py new file mode 100644 index 0000000000000000000000000000000000000000..535434d188ff575029cc7a0de807b0daa7348f73 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/test_byteordercodes.py @@ -0,0 +1,29 @@ +''' Tests for byteorder module ''' + +import sys + +from numpy.testing import assert_ +from pytest import raises as assert_raises + +import scipy.io.matlab._byteordercodes as sibc + + +def test_native(): + native_is_le = sys.byteorder == 'little' + assert_(sibc.sys_is_le == native_is_le) + + +def test_to_numpy(): + if sys.byteorder == 'little': + assert_(sibc.to_numpy_code('native') == '<') + assert_(sibc.to_numpy_code('swapped') == '>') + else: + assert_(sibc.to_numpy_code('native') == '>') + assert_(sibc.to_numpy_code('swapped') == '<') + assert_(sibc.to_numpy_code('native') == sibc.to_numpy_code('=')) + assert_(sibc.to_numpy_code('big') == '>') + for code in ('little', '<', 'l', 'L', 'le'): + assert_(sibc.to_numpy_code(code) == '<') + for code in ('big', '>', 'b', 'B', 'be'): + assert_(sibc.to_numpy_code(code) == '>') + assert_raises(ValueError, sibc.to_numpy_code, 'silly string') diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/test_mio.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/test_mio.py new file mode 100644 index 0000000000000000000000000000000000000000..ef8b3e34ee666fa297d9e25eec1e409ef68edb5f --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/test_mio.py @@ -0,0 +1,1371 @@ +import os +from collections import OrderedDict +from os.path import join as pjoin, dirname +from glob import glob +from io import BytesIO +import re +from tempfile import mkdtemp + +import warnings +import shutil +import gzip + +from numpy.testing import (assert_array_equal, assert_array_almost_equal, + assert_equal, assert_, assert_warns, assert_allclose) +import pytest +from pytest import raises as assert_raises + +import numpy as np +from numpy import array +from scipy.sparse import issparse, eye_array, coo_array, csc_array + +import scipy.io +from scipy.io.matlab import MatlabOpaque, MatlabFunction, MatlabObject +import scipy.io.matlab._byteordercodes as boc +from scipy.io.matlab._miobase import ( + matdims, MatWriteError, MatReadError, matfile_version) +from scipy.io.matlab._mio import mat_reader_factory, loadmat, savemat, whosmat +from scipy.io.matlab._mio5 import ( + MatFile5Writer, MatFile5Reader, varmats_from_mat, to_writeable, + EmptyStructMarker) +import scipy.io.matlab._mio5_params as mio5p +from scipy._lib._util import VisibleDeprecationWarning + + +test_data_path = pjoin(dirname(__file__), 'data') +pytestmark = pytest.mark.thread_unsafe + + +def mlarr(*args, **kwargs): + """Convenience function to return matlab-compatible 2-D array.""" + arr = np.array(*args, **kwargs) + arr.shape = matdims(arr) + return arr + + +# Define cases to test +theta = np.pi/4*np.arange(9,dtype=float).reshape(1,9) +case_table4 = [ + {'name': 'double', + 'classes': {'testdouble': 'double'}, + 'expected': {'testdouble': theta} + }] +case_table4.append( + {'name': 'string', + 'classes': {'teststring': 'char'}, + 'expected': {'teststring': + array(['"Do nine men interpret?" "Nine men," I nod.'])} + }) +case_table4.append( + {'name': 'complex', + 'classes': {'testcomplex': 'double'}, + 'expected': {'testcomplex': np.cos(theta) + 1j*np.sin(theta)} + }) +A = np.zeros((3,5)) +A[0] = list(range(1,6)) +A[:,0] = list(range(1,4)) +case_table4.append( + {'name': 'matrix', + 'classes': {'testmatrix': 'double'}, + 'expected': {'testmatrix': A}, + }) +case_table4.append( + {'name': 'sparse', + 'classes': {'testsparse': 'sparse'}, + 'expected': {'testsparse': coo_array(A)}, + }) +B = A.astype(complex) +B[0,0] += 1j +case_table4.append( + {'name': 'sparsecomplex', + 'classes': {'testsparsecomplex': 'sparse'}, + 'expected': {'testsparsecomplex': coo_array(B)}, + }) +case_table4.append( + {'name': 'multi', + 'classes': {'theta': 'double', 'a': 'double'}, + 'expected': {'theta': theta, 'a': A}, + }) +case_table4.append( + {'name': 'minus', + 'classes': {'testminus': 'double'}, + 'expected': {'testminus': mlarr(-1)}, + }) +case_table4.append( + {'name': 'onechar', + 'classes': {'testonechar': 'char'}, + 'expected': {'testonechar': array(['r'])}, + }) +# Cell arrays stored as object arrays +CA = mlarr(( # tuple for object array creation + [], + mlarr([1]), + mlarr([[1,2]]), + mlarr([[1,2,3]])), dtype=object).reshape(1,-1) +CA[0,0] = array( + ['This cell contains this string and 3 arrays of increasing length']) +case_table5 = [ + {'name': 'cell', + 'classes': {'testcell': 'cell'}, + 'expected': {'testcell': CA}}] +CAE = mlarr(( # tuple for object array creation + mlarr(1), + mlarr(2), + mlarr([]), + mlarr([]), + mlarr(3)), dtype=object).reshape(1,-1) +objarr = np.empty((1,1),dtype=object) +objarr[0,0] = mlarr(1) +case_table5.append( + {'name': 'scalarcell', + 'classes': {'testscalarcell': 'cell'}, + 'expected': {'testscalarcell': objarr} + }) +case_table5.append( + {'name': 'emptycell', + 'classes': {'testemptycell': 'cell'}, + 'expected': {'testemptycell': CAE}}) +case_table5.append( + {'name': 'stringarray', + 'classes': {'teststringarray': 'char'}, + 'expected': {'teststringarray': array( + ['one ', 'two ', 'three'])}, + }) +case_table5.append( + {'name': '3dmatrix', + 'classes': {'test3dmatrix': 'double'}, + 'expected': { + 'test3dmatrix': np.transpose(np.reshape(list(range(1,25)), (4,3,2)))} + }) +st_sub_arr = array([np.sqrt(2),np.exp(1),np.pi]).reshape(1,3) +dtype = [(n, object) for n in ['stringfield', 'doublefield', 'complexfield']] +st1 = np.zeros((1,1), dtype) +st1['stringfield'][0,0] = array(['Rats live on no evil star.']) +st1['doublefield'][0,0] = st_sub_arr +st1['complexfield'][0,0] = st_sub_arr * (1 + 1j) +case_table5.append( + {'name': 'struct', + 'classes': {'teststruct': 'struct'}, + 'expected': {'teststruct': st1} + }) +CN = np.zeros((1,2), dtype=object) +CN[0,0] = mlarr(1) +CN[0,1] = np.zeros((1,3), dtype=object) +CN[0,1][0,0] = mlarr(2, dtype=np.uint8) +CN[0,1][0,1] = mlarr([[3]], dtype=np.uint8) +CN[0,1][0,2] = np.zeros((1,2), dtype=object) +CN[0,1][0,2][0,0] = mlarr(4, dtype=np.uint8) +CN[0,1][0,2][0,1] = mlarr(5, dtype=np.uint8) +case_table5.append( + {'name': 'cellnest', + 'classes': {'testcellnest': 'cell'}, + 'expected': {'testcellnest': CN}, + }) +st2 = np.empty((1,1), dtype=[(n, object) for n in ['one', 'two']]) +st2[0,0]['one'] = mlarr(1) +st2[0,0]['two'] = np.empty((1,1), dtype=[('three', object)]) +st2[0,0]['two'][0,0]['three'] = array(['number 3']) +case_table5.append( + {'name': 'structnest', + 'classes': {'teststructnest': 'struct'}, + 'expected': {'teststructnest': st2} + }) +a = np.empty((1,2), dtype=[(n, object) for n in ['one', 'two']]) +a[0,0]['one'] = mlarr(1) +a[0,0]['two'] = mlarr(2) +a[0,1]['one'] = array(['number 1']) +a[0,1]['two'] = array(['number 2']) +case_table5.append( + {'name': 'structarr', + 'classes': {'teststructarr': 'struct'}, + 'expected': {'teststructarr': a} + }) +ODT = np.dtype([(n, object) for n in + ['expr', 'inputExpr', 'args', + 'isEmpty', 'numArgs', 'version']]) +MO = MatlabObject(np.zeros((1,1), dtype=ODT), 'inline') +m0 = MO[0,0] +m0['expr'] = array(['x']) +m0['inputExpr'] = array([' x = INLINE_INPUTS_{1};']) +m0['args'] = array(['x']) +m0['isEmpty'] = mlarr(0) +m0['numArgs'] = mlarr(1) +m0['version'] = mlarr(1) +case_table5.append( + {'name': 'object', + 'classes': {'testobject': 'object'}, + 'expected': {'testobject': MO} + }) +fp_u_str = open(pjoin(test_data_path, 'japanese_utf8.txt'), 'rb') +u_str = fp_u_str.read().decode('utf-8') +fp_u_str.close() +case_table5.append( + {'name': 'unicode', + 'classes': {'testunicode': 'char'}, + 'expected': {'testunicode': array([u_str])} + }) +case_table5.append( + {'name': 'sparse', + 'classes': {'testsparse': 'sparse'}, + 'expected': {'testsparse': coo_array(A)}, + }) +case_table5.append( + {'name': 'sparsecomplex', + 'classes': {'testsparsecomplex': 'sparse'}, + 'expected': {'testsparsecomplex': coo_array(B)}, + }) +case_table5.append( + {'name': 'bool', + 'classes': {'testbools': 'logical'}, + 'expected': {'testbools': + array([[True], [False]])}, + }) + +case_table5_rt = case_table5[:] +# Inline functions can't be concatenated in matlab, so RT only +case_table5_rt.append( + {'name': 'objectarray', + 'classes': {'testobjectarray': 'object'}, + 'expected': {'testobjectarray': np.repeat(MO, 2).reshape(1,2)}}) + + +def types_compatible(var1, var2): + """Check if types are same or compatible. + + 0-D numpy scalars are compatible with bare python scalars. + """ + type1 = type(var1) + type2 = type(var2) + if type1 is type2: + return True + if type1 is np.ndarray and var1.shape == (): + return type(var1.item()) is type2 + if type2 is np.ndarray and var2.shape == (): + return type(var2.item()) is type1 + return False + + +def _check_level(label, expected, actual): + """ Check one level of a potentially nested array """ + if issparse(expected): # allow different types of sparse matrices + assert_(issparse(actual)) + assert_array_almost_equal(actual.toarray(), + expected.toarray(), + err_msg=label, + decimal=5) + return + # Check types are as expected + assert_(types_compatible(expected, actual), + f"Expected type {type(expected)}, got {type(actual)} at {label}") + # A field in a record array may not be an ndarray + # A scalar from a record array will be type np.void + if not isinstance(expected, np.void | np.ndarray | MatlabObject): + assert_equal(expected, actual) + return + # This is an ndarray-like thing + assert_(expected.shape == actual.shape, + msg=f'Expected shape {expected.shape}, got {actual.shape} at {label}') + ex_dtype = expected.dtype + if ex_dtype.hasobject: # array of objects + if isinstance(expected, MatlabObject): + assert_equal(expected.classname, actual.classname) + for i, ev in enumerate(expected): + level_label = "%s, [%d], " % (label, i) + _check_level(level_label, ev, actual[i]) + return + if ex_dtype.fields: # probably recarray + for fn in ex_dtype.fields: + level_label = f"{label}, field {fn}, " + _check_level(level_label, + expected[fn], actual[fn]) + return + if ex_dtype.type in (str, # string or bool + np.str_, + np.bool_): + assert_equal(actual, expected, err_msg=label) + return + # Something numeric + assert_array_almost_equal(actual, expected, err_msg=label, decimal=5) + + +def _load_check_case(name, files, case): + for file_name in files: + matdict = loadmat(file_name, struct_as_record=True, spmatrix=False) + label = f"test {name}; file {file_name}" + for k, expected in case.items(): + k_label = f"{label}, variable {k}" + assert_(k in matdict, f"Missing key at {k_label}") + _check_level(k_label, expected, matdict[k]) + + +def _whos_check_case(name, files, case, classes): + for file_name in files: + label = f"test {name}; file {file_name}" + + whos = whosmat(file_name) + + expected_whos = [ + (k, expected.shape, classes[k]) for k, expected in case.items()] + + whos.sort() + expected_whos.sort() + assert_equal(whos, expected_whos, + f"{label}: {whos!r} != {expected_whos!r}" + ) + + +# Round trip tests +def _rt_check_case(name, expected, format): + mat_stream = BytesIO() + savemat(mat_stream, expected, format=format) + mat_stream.seek(0) + _load_check_case(name, [mat_stream], expected) + + +# generator for tests +def _cases(version, filt='test%(name)s_*.mat'): + if version == '4': + cases = case_table4 + elif version == '5': + cases = case_table5 + else: + assert version == '5_rt' + cases = case_table5_rt + for case in cases: + name = case['name'] + expected = case['expected'] + if filt is None: + files = None + else: + use_filt = pjoin(test_data_path, filt % dict(name=name)) + files = glob(use_filt) + assert len(files) > 0, \ + f"No files for test {name} using filter {filt}" + classes = case['classes'] + yield name, files, expected, classes + + +@pytest.mark.parametrize('version', ('4', '5')) +def test_load(version): + for case in _cases(version): + _load_check_case(*case[:3]) + + +@pytest.mark.parametrize('version', ('4', '5')) +def test_whos(version): + for case in _cases(version): + _whos_check_case(*case) + + +# generator for round trip tests +@pytest.mark.parametrize('version, fmts', [ + ('4', ['4', '5']), + ('5_rt', ['5']), +]) +def test_round_trip(version, fmts): + for case in _cases(version, filt=None): + for fmt in fmts: + _rt_check_case(case[0], case[2], fmt) + + +def test_gzip_simple(): + xdense = np.zeros((20,20)) + xdense[2,3] = 2.3 + xdense[4,5] = 4.5 + x = csc_array(xdense) + + name = 'gzip_test' + expected = {'x':x} + format = '4' + + tmpdir = mkdtemp() + try: + fname = pjoin(tmpdir,name) + mat_stream = gzip.open(fname, mode='wb') + savemat(mat_stream, expected, format=format) + mat_stream.close() + + mat_stream = gzip.open(fname, mode='rb') + actual = loadmat(mat_stream, struct_as_record=True, spmatrix=False) + mat_stream.close() + finally: + shutil.rmtree(tmpdir) + + assert_array_almost_equal(actual['x'].toarray(), + expected['x'].toarray(), + err_msg=repr(actual)) + + +def test_multiple_open(): + # Ticket #1039, on Windows: check that files are not left open + tmpdir = mkdtemp() + try: + x = dict(x=np.zeros((2, 2))) + + fname = pjoin(tmpdir, "a.mat") + + # Check that file is not left open + savemat(fname, x) + os.unlink(fname) + savemat(fname, x) + loadmat(fname) + os.unlink(fname) + + # Check that stream is left open + f = open(fname, 'wb') + savemat(f, x) + f.seek(0) + f.close() + + f = open(fname, 'rb') + loadmat(f) + f.seek(0) + f.close() + finally: + shutil.rmtree(tmpdir) + + +def test_mat73(): + # Check any hdf5 files raise an error + filenames = glob( + pjoin(test_data_path, 'testhdf5*.mat')) + assert_(len(filenames) > 0) + for filename in filenames: + fp = open(filename, 'rb') + assert_raises(NotImplementedError, + loadmat, + fp, + struct_as_record=True) + fp.close() + + +def test_warnings(): + # This test is an echo of the previous behavior, which was to raise a + # warning if the user triggered a search for mat files on the Python system + # path. We can remove the test in the next version after upcoming (0.13). + fname = pjoin(test_data_path, 'testdouble_7.1_GLNX86.mat') + with warnings.catch_warnings(): + warnings.simplefilter('error') + # This should not generate a warning + loadmat(fname, struct_as_record=True) + # This neither + loadmat(fname, struct_as_record=False) + + +def test_regression_653(): + # Saving a dictionary with only invalid keys used to raise an error. Now we + # save this as an empty struct in matlab space. + sio = BytesIO() + savemat(sio, {'d':{1:2}}, format='5') + back = loadmat(sio)['d'] + # Check we got an empty struct equivalent + assert_equal(back.shape, (1,1)) + assert_equal(back.dtype, np.dtype(object)) + assert_(back[0,0] is None) + + +def test_structname_len(): + # Test limit for length of field names in structs + lim = 31 + fldname = 'a' * lim + st1 = np.zeros((1,1), dtype=[(fldname, object)]) + savemat(BytesIO(), {'longstruct': st1}, format='5') + fldname = 'a' * (lim+1) + st1 = np.zeros((1,1), dtype=[(fldname, object)]) + assert_raises(ValueError, savemat, BytesIO(), + {'longstruct': st1}, format='5') + + +def test_4_and_long_field_names_incompatible(): + # Long field names option not supported in 4 + my_struct = np.zeros((1,1),dtype=[('my_fieldname',object)]) + assert_raises(ValueError, savemat, BytesIO(), + {'my_struct':my_struct}, format='4', long_field_names=True) + + +def test_long_field_names(): + # Test limit for length of field names in structs + lim = 63 + fldname = 'a' * lim + st1 = np.zeros((1,1), dtype=[(fldname, object)]) + savemat(BytesIO(), {'longstruct': st1}, format='5',long_field_names=True) + fldname = 'a' * (lim+1) + st1 = np.zeros((1,1), dtype=[(fldname, object)]) + assert_raises(ValueError, savemat, BytesIO(), + {'longstruct': st1}, format='5',long_field_names=True) + + +def test_long_field_names_in_struct(): + # Regression test - long_field_names was erased if you passed a struct + # within a struct + lim = 63 + fldname = 'a' * lim + cell = np.ndarray((1,2),dtype=object) + st1 = np.zeros((1,1), dtype=[(fldname, object)]) + cell[0,0] = st1 + cell[0,1] = st1 + savemat(BytesIO(), {'longstruct': cell}, format='5',long_field_names=True) + # + # Check to make sure it fails with long field names off + # + assert_raises(ValueError, savemat, BytesIO(), + {'longstruct': cell}, format='5', long_field_names=False) + + +def test_cell_with_one_thing_in_it(): + # Regression test - make a cell array that's 1 x 2 and put two + # strings in it. It works. Make a cell array that's 1 x 1 and put + # a string in it. It should work but, in the old days, it didn't. + cells = np.ndarray((1,2),dtype=object) + cells[0,0] = 'Hello' + cells[0,1] = 'World' + savemat(BytesIO(), {'x': cells}, format='5') + + cells = np.ndarray((1,1),dtype=object) + cells[0,0] = 'Hello, world' + savemat(BytesIO(), {'x': cells}, format='5') + + +def test_writer_properties(): + # Tests getting, setting of properties of matrix writer + mfw = MatFile5Writer(BytesIO()) + assert_equal(mfw.global_vars, []) + mfw.global_vars = ['avar'] + assert_equal(mfw.global_vars, ['avar']) + assert_equal(mfw.unicode_strings, False) + mfw.unicode_strings = True + assert_equal(mfw.unicode_strings, True) + assert_equal(mfw.long_field_names, False) + mfw.long_field_names = True + assert_equal(mfw.long_field_names, True) + + +def test_use_small_element(): + # Test whether we're using small data element or not + sio = BytesIO() + wtr = MatFile5Writer(sio) + # First check size for no sde for name + arr = np.zeros(10) + wtr.put_variables({'aaaaa': arr}) + w_sz = len(sio.getvalue()) + # Check small name results in largish difference in size + sio.truncate(0) + sio.seek(0) + wtr.put_variables({'aaaa': arr}) + assert_(w_sz - len(sio.getvalue()) > 4) + # Whereas increasing name size makes less difference + sio.truncate(0) + sio.seek(0) + wtr.put_variables({'aaaaaa': arr}) + assert_(len(sio.getvalue()) - w_sz < 4) + + +def test_save_dict(): + # Test that both dict and OrderedDict can be saved (as recarray), + # loaded as matstruct, and preserve order + ab_exp = np.array([[(1, 2)]], dtype=[('a', object), ('b', object)]) + for dict_type in (dict, OrderedDict): + # Initialize with tuples to keep order + d = dict_type([('a', 1), ('b', 2)]) + stream = BytesIO() + savemat(stream, {'dict': d}) + stream.seek(0) + vals = loadmat(stream)['dict'] + assert_equal(vals.dtype.names, ('a', 'b')) + assert_array_equal(vals, ab_exp) + + +def test_1d_shape(): + # New 5 behavior is 1D -> row vector + arr = np.arange(5) + for format in ('4', '5'): + # Column is the default + stream = BytesIO() + savemat(stream, {'oned': arr}, format=format) + vals = loadmat(stream) + assert_equal(vals['oned'].shape, (1, 5)) + # can be explicitly 'column' for oned_as + stream = BytesIO() + savemat(stream, {'oned':arr}, + format=format, + oned_as='column') + vals = loadmat(stream) + assert_equal(vals['oned'].shape, (5,1)) + # but different from 'row' + stream = BytesIO() + savemat(stream, {'oned':arr}, + format=format, + oned_as='row') + vals = loadmat(stream) + assert_equal(vals['oned'].shape, (1,5)) + + +def test_compression(): + arr = np.zeros(100).reshape((5,20)) + arr[2,10] = 1 + stream = BytesIO() + savemat(stream, {'arr':arr}) + raw_len = len(stream.getvalue()) + vals = loadmat(stream) + assert_array_equal(vals['arr'], arr) + stream = BytesIO() + savemat(stream, {'arr':arr}, do_compression=True) + compressed_len = len(stream.getvalue()) + vals = loadmat(stream) + assert_array_equal(vals['arr'], arr) + assert_(raw_len > compressed_len) + # Concatenate, test later + arr2 = arr.copy() + arr2[0,0] = 1 + stream = BytesIO() + savemat(stream, {'arr':arr, 'arr2':arr2}, do_compression=False) + vals = loadmat(stream) + assert_array_equal(vals['arr2'], arr2) + stream = BytesIO() + savemat(stream, {'arr':arr, 'arr2':arr2}, do_compression=True) + vals = loadmat(stream) + assert_array_equal(vals['arr2'], arr2) + + +def test_single_object(): + stream = BytesIO() + savemat(stream, {'A':np.array(1, dtype=object)}) + + +def test_skip_variable(): + # Test skipping over the first of two variables in a MAT file + # using mat_reader_factory and put_variables to read them in. + # + # This is a regression test of a problem that's caused by + # using the compressed file reader seek instead of the raw file + # I/O seek when skipping over a compressed chunk. + # + # The problem arises when the chunk is large: this file has + # a 256x256 array of random (uncompressible) doubles. + # + filename = pjoin(test_data_path,'test_skip_variable.mat') + # + # Prove that it loads with loadmat + # + d = loadmat(filename, struct_as_record=True) + assert_('first' in d) + assert_('second' in d) + # + # Make the factory + # + factory, file_opened = mat_reader_factory(filename, struct_as_record=True) + # + # This is where the factory breaks with an error in MatMatrixGetter.to_next + # + d = factory.get_variables('second') + assert_('second' in d) + factory.mat_stream.close() + + +def test_empty_struct(): + # ticket 885 + filename = pjoin(test_data_path,'test_empty_struct.mat') + # before ticket fix, this would crash with ValueError, empty data + # type + d = loadmat(filename, struct_as_record=True) + a = d['a'] + assert_equal(a.shape, (1,1)) + assert_equal(a.dtype, np.dtype(object)) + assert_(a[0,0] is None) + stream = BytesIO() + arr = np.array((), dtype='U') + # before ticket fix, this used to give data type not understood + savemat(stream, {'arr':arr}) + d = loadmat(stream) + a2 = d['arr'] + assert_array_equal(a2, arr) + + +def test_save_empty_dict(): + # saving empty dict also gives empty struct + stream = BytesIO() + savemat(stream, {'arr': {}}) + d = loadmat(stream) + a = d['arr'] + assert_equal(a.shape, (1,1)) + assert_equal(a.dtype, np.dtype(object)) + assert_(a[0,0] is None) + + +def assert_any_equal(output, alternatives): + """ Assert `output` is equal to at least one element in `alternatives` + """ + one_equal = False + for expected in alternatives: + if np.all(output == expected): + one_equal = True + break + assert_(one_equal) + + +def test_to_writeable(): + # Test to_writeable function + res = to_writeable(np.array([1])) # pass through ndarrays + assert_equal(res.shape, (1,)) + assert_array_equal(res, 1) + # Dict fields can be written in any order + expected1 = np.array([(1, 2)], dtype=[('a', '|O8'), ('b', '|O8')]) + expected2 = np.array([(2, 1)], dtype=[('b', '|O8'), ('a', '|O8')]) + alternatives = (expected1, expected2) + assert_any_equal(to_writeable({'a':1,'b':2}), alternatives) + # Fields with underscores discarded + assert_any_equal(to_writeable({'a':1,'b':2, '_c':3}), alternatives) + # Not-string fields discarded + assert_any_equal(to_writeable({'a':1,'b':2, 100:3}), alternatives) + # String fields that are valid Python identifiers discarded + assert_any_equal(to_writeable({'a':1,'b':2, '99':3}), alternatives) + # Object with field names is equivalent + + class klass: + pass + + c = klass + c.a = 1 + c.b = 2 + assert_any_equal(to_writeable(c), alternatives) + # empty list and tuple go to empty array + res = to_writeable([]) + assert_equal(res.shape, (0,)) + assert_equal(res.dtype.type, np.float64) + res = to_writeable(()) + assert_equal(res.shape, (0,)) + assert_equal(res.dtype.type, np.float64) + # None -> None + assert_(to_writeable(None) is None) + # String to strings + assert_equal(to_writeable('a string').dtype.type, np.str_) + # Scalars to numpy to NumPy scalars + res = to_writeable(1) + assert_equal(res.shape, ()) + assert_equal(res.dtype.type, np.array(1).dtype.type) + assert_array_equal(res, 1) + # Empty dict returns EmptyStructMarker + assert_(to_writeable({}) is EmptyStructMarker) + # Object does not have (even empty) __dict__ + assert_(to_writeable(object()) is None) + # Custom object does have empty __dict__, returns EmptyStructMarker + + class C: + pass + + assert_(to_writeable(c()) is EmptyStructMarker) + # dict keys with legal characters are convertible + res = to_writeable({'a': 1})['a'] + assert_equal(res.shape, (1,)) + assert_equal(res.dtype.type, np.object_) + # Only fields with illegal characters, falls back to EmptyStruct + assert_(to_writeable({'1':1}) is EmptyStructMarker) + assert_(to_writeable({'_a':1}) is EmptyStructMarker) + # Unless there are valid fields, in which case structured array + assert_equal(to_writeable({'1':1, 'f': 2}), + np.array([(2,)], dtype=[('f', '|O8')])) + + +def test_recarray(): + # check roundtrip of structured array + dt = [('f1', 'f8'), + ('f2', 'S10')] + arr = np.zeros((2,), dtype=dt) + arr[0]['f1'] = 0.5 + arr[0]['f2'] = 'python' + arr[1]['f1'] = 99 + arr[1]['f2'] = 'not perl' + stream = BytesIO() + savemat(stream, {'arr': arr}) + d = loadmat(stream, struct_as_record=False) + a20 = d['arr'][0,0] + assert_equal(a20.f1, 0.5) + assert_equal(a20.f2, 'python') + d = loadmat(stream, struct_as_record=True) + a20 = d['arr'][0,0] + assert_equal(a20['f1'], 0.5) + assert_equal(a20['f2'], 'python') + # structs always come back as object types + assert_equal(a20.dtype, np.dtype([('f1', 'O'), + ('f2', 'O')])) + a21 = d['arr'].flat[1] + assert_equal(a21['f1'], 99) + assert_equal(a21['f2'], 'not perl') + + +def test_save_object(): + class C: + pass + c = C() + c.field1 = 1 + c.field2 = 'a string' + stream = BytesIO() + savemat(stream, {'c': c}) + d = loadmat(stream, struct_as_record=False) + c2 = d['c'][0,0] + assert_equal(c2.field1, 1) + assert_equal(c2.field2, 'a string') + d = loadmat(stream, struct_as_record=True) + c2 = d['c'][0,0] + assert_equal(c2['field1'], 1) + assert_equal(c2['field2'], 'a string') + + +def test_read_opts(): + # tests if read is seeing option sets, at initialization and after + # initialization + arr = np.arange(6).reshape(1,6) + stream = BytesIO() + savemat(stream, {'a': arr}) + rdr = MatFile5Reader(stream) + back_dict = rdr.get_variables() + rarr = back_dict['a'] + assert_array_equal(rarr, arr) + rdr = MatFile5Reader(stream, squeeze_me=True) + assert_array_equal(rdr.get_variables()['a'], arr.reshape((6,))) + rdr.squeeze_me = False + assert_array_equal(rarr, arr) + rdr = MatFile5Reader(stream, byte_order=boc.native_code) + assert_array_equal(rdr.get_variables()['a'], arr) + # inverted byte code leads to error on read because of swapped + # header etc. + rdr = MatFile5Reader(stream, byte_order=boc.swapped_code) + assert_raises(Exception, rdr.get_variables) + rdr.byte_order = boc.native_code + assert_array_equal(rdr.get_variables()['a'], arr) + arr = np.array(['a string']) + stream.truncate(0) + stream.seek(0) + savemat(stream, {'a': arr}) + rdr = MatFile5Reader(stream) + assert_array_equal(rdr.get_variables()['a'], arr) + rdr = MatFile5Reader(stream, chars_as_strings=False) + carr = np.atleast_2d(np.array(list(arr.item()), dtype='U1')) + assert_array_equal(rdr.get_variables()['a'], carr) + rdr.chars_as_strings = True + assert_array_equal(rdr.get_variables()['a'], arr) + + +def test_empty_string(): + # make sure reading empty string does not raise error + estring_fname = pjoin(test_data_path, 'single_empty_string.mat') + fp = open(estring_fname, 'rb') + rdr = MatFile5Reader(fp) + d = rdr.get_variables() + fp.close() + assert_array_equal(d['a'], np.array([], dtype='U1')) + # Empty string round trip. Matlab cannot distinguish + # between a string array that is empty, and a string array + # containing a single empty string, because it stores strings as + # arrays of char. There is no way of having an array of char that + # is not empty, but contains an empty string. + stream = BytesIO() + savemat(stream, {'a': np.array([''])}) + rdr = MatFile5Reader(stream) + d = rdr.get_variables() + assert_array_equal(d['a'], np.array([], dtype='U1')) + stream.truncate(0) + stream.seek(0) + savemat(stream, {'a': np.array([], dtype='U1')}) + rdr = MatFile5Reader(stream) + d = rdr.get_variables() + assert_array_equal(d['a'], np.array([], dtype='U1')) + stream.close() + + +def test_corrupted_data(): + import zlib + for exc, fname in [(ValueError, 'corrupted_zlib_data.mat'), + (zlib.error, 'corrupted_zlib_checksum.mat')]: + with open(pjoin(test_data_path, fname), 'rb') as fp: + rdr = MatFile5Reader(fp) + assert_raises(exc, rdr.get_variables) + + +def test_corrupted_data_check_can_be_disabled(): + with open(pjoin(test_data_path, 'corrupted_zlib_data.mat'), 'rb') as fp: + rdr = MatFile5Reader(fp, verify_compressed_data_integrity=False) + rdr.get_variables() + + +def test_read_both_endian(): + # make sure big- and little- endian data is read correctly + for fname in ('big_endian.mat', 'little_endian.mat'): + fp = open(pjoin(test_data_path, fname), 'rb') + rdr = MatFile5Reader(fp) + d = rdr.get_variables() + fp.close() + assert_array_equal(d['strings'], + np.array([['hello'], + ['world']], dtype=object)) + assert_array_equal(d['floats'], + np.array([[2., 3.], + [3., 4.]], dtype=np.float32)) + + +def test_write_opposite_endian(): + # We don't support writing opposite endian .mat files, but we need to behave + # correctly if the user supplies an other-endian NumPy array to write out. + float_arr = np.array([[2., 3.], + [3., 4.]]) + int_arr = np.arange(6).reshape((2, 3)) + uni_arr = np.array(['hello', 'world'], dtype='U') + stream = BytesIO() + savemat(stream, { + 'floats': float_arr.byteswap().view(float_arr.dtype.newbyteorder()), + 'ints': int_arr.byteswap().view(int_arr.dtype.newbyteorder()), + 'uni_arr': uni_arr.byteswap().view(uni_arr.dtype.newbyteorder()), + }) + rdr = MatFile5Reader(stream) + d = rdr.get_variables() + assert_array_equal(d['floats'], float_arr) + assert_array_equal(d['ints'], int_arr) + assert_array_equal(d['uni_arr'], uni_arr) + stream.close() + + +def test_logical_array(): + # The roundtrip test doesn't verify that we load the data up with the + # correct (bool) dtype + with open(pjoin(test_data_path, 'testbool_8_WIN64.mat'), 'rb') as fobj: + rdr = MatFile5Reader(fobj, mat_dtype=True) + d = rdr.get_variables() + x = np.array([[True], [False]], dtype=np.bool_) + assert_array_equal(d['testbools'], x) + assert_equal(d['testbools'].dtype, x.dtype) + + +def test_logical_out_type(): + # Confirm that bool type written as uint8, uint8 class + # See gh-4022 + stream = BytesIO() + barr = np.array([False, True, False]) + savemat(stream, {'barray': barr}) + stream.seek(0) + reader = MatFile5Reader(stream) + reader.initialize_read() + reader.read_file_header() + hdr, _ = reader.read_var_header() + assert_equal(hdr.mclass, mio5p.mxUINT8_CLASS) + assert_equal(hdr.is_logical, True) + var = reader.read_var_array(hdr, False) + assert_equal(var.dtype.type, np.uint8) + + +def test_roundtrip_zero_dimensions(): + stream = BytesIO() + savemat(stream, {'d':np.empty((10, 0))}) + d = loadmat(stream) + assert d['d'].shape == (10, 0) + + +def test_mat4_3d(): + # test behavior when writing 3-D arrays to matlab 4 files + stream = BytesIO() + arr = np.arange(24).reshape((2,3,4)) + assert_raises(ValueError, savemat, stream, {'a': arr}, True, '4') + + +def test_func_read(): + func_eg = pjoin(test_data_path, 'testfunc_7.4_GLNX86.mat') + fp = open(func_eg, 'rb') + rdr = MatFile5Reader(fp) + d = rdr.get_variables() + fp.close() + assert isinstance(d['testfunc'], MatlabFunction) + stream = BytesIO() + wtr = MatFile5Writer(stream) + assert_raises(MatWriteError, wtr.put_variables, d) + + +def test_mat_dtype(): + double_eg = pjoin(test_data_path, 'testmatrix_6.1_SOL2.mat') + fp = open(double_eg, 'rb') + rdr = MatFile5Reader(fp, mat_dtype=False) + d = rdr.get_variables() + fp.close() + assert_equal(d['testmatrix'].dtype.kind, 'u') + + fp = open(double_eg, 'rb') + rdr = MatFile5Reader(fp, mat_dtype=True) + d = rdr.get_variables() + fp.close() + assert_equal(d['testmatrix'].dtype.kind, 'f') + + +def test_sparse_in_struct(): + # reproduces bug found by DC where Cython code was insisting on + # ndarray return type, but getting sparse matrix + st = {'sparsefield': eye_array(4)} + stream = BytesIO() + savemat(stream, {'a':st}) + d = loadmat(stream, struct_as_record=True) + assert_array_equal(d['a'][0, 0]['sparsefield'].toarray(), np.eye(4)) + + +def test_mat_struct_squeeze(): + stream = BytesIO() + in_d = {'st':{'one':1, 'two':2}} + savemat(stream, in_d) + # no error without squeeze + loadmat(stream, struct_as_record=False) + # previous error was with squeeze, with mat_struct + loadmat(stream, struct_as_record=False, squeeze_me=True) + + +def test_scalar_squeeze(): + stream = BytesIO() + in_d = {'scalar': [[0.1]], 'string': 'my name', 'st':{'one':1, 'two':2}} + savemat(stream, in_d) + out_d = loadmat(stream, squeeze_me=True) + assert_(isinstance(out_d['scalar'], float)) + assert_(isinstance(out_d['string'], str)) + assert_(isinstance(out_d['st'], np.ndarray)) + + +def test_str_round(): + # from report by Angus McMorland on mailing list 3 May 2010 + stream = BytesIO() + in_arr = np.array(['Hello', 'Foob']) + out_arr = np.array(['Hello', 'Foob ']) + savemat(stream, dict(a=in_arr)) + res = loadmat(stream) + # resulted in ['HloolFoa', 'elWrdobr'] + assert_array_equal(res['a'], out_arr) + stream.truncate(0) + stream.seek(0) + # Make Fortran ordered version of string + in_str = in_arr.tobytes(order='F') + in_from_str = np.ndarray(shape=a.shape, + dtype=in_arr.dtype, + order='F', + buffer=in_str) + savemat(stream, dict(a=in_from_str)) + assert_array_equal(res['a'], out_arr) + # unicode save did lead to buffer too small error + stream.truncate(0) + stream.seek(0) + in_arr_u = in_arr.astype('U') + out_arr_u = out_arr.astype('U') + savemat(stream, {'a': in_arr_u}) + res = loadmat(stream) + assert_array_equal(res['a'], out_arr_u) + + +def test_fieldnames(): + # Check that field names are as expected + stream = BytesIO() + savemat(stream, {'a': {'a':1, 'b':2}}) + res = loadmat(stream) + field_names = res['a'].dtype.names + assert_equal(set(field_names), {'a', 'b'}) + + +def test_loadmat_varnames(): + # Test that we can get just one variable from a mat file using loadmat + mat5_sys_names = ['__globals__', + '__header__', + '__version__'] + for eg_file, sys_v_names in ( + (pjoin(test_data_path, 'testmulti_4.2c_SOL2.mat'), []), (pjoin( + test_data_path, 'testmulti_7.4_GLNX86.mat'), mat5_sys_names)): + vars = loadmat(eg_file) + assert_equal(set(vars.keys()), set(['a', 'theta'] + sys_v_names)) + vars = loadmat(eg_file, variable_names='a') + assert_equal(set(vars.keys()), set(['a'] + sys_v_names)) + vars = loadmat(eg_file, variable_names=['a']) + assert_equal(set(vars.keys()), set(['a'] + sys_v_names)) + vars = loadmat(eg_file, variable_names=['theta']) + assert_equal(set(vars.keys()), set(['theta'] + sys_v_names)) + vars = loadmat(eg_file, variable_names=('theta',)) + assert_equal(set(vars.keys()), set(['theta'] + sys_v_names)) + vars = loadmat(eg_file, variable_names=[]) + assert_equal(set(vars.keys()), set(sys_v_names)) + vnames = ['theta'] + vars = loadmat(eg_file, variable_names=vnames) + assert_equal(vnames, ['theta']) + + +def test_round_types(): + # Check that saving, loading preserves dtype in most cases + arr = np.arange(10) + stream = BytesIO() + for dts in ('f8','f4','i8','i4','i2','i1', + 'u8','u4','u2','u1','c16','c8'): + stream.truncate(0) + stream.seek(0) # needed for BytesIO in Python 3 + savemat(stream, {'arr': arr.astype(dts)}) + vars = loadmat(stream) + assert_equal(np.dtype(dts), vars['arr'].dtype) + + +def test_varmats_from_mat(): + # Make a mat file with several variables, write it, read it back + names_vars = (('arr', mlarr(np.arange(10))), + ('mystr', mlarr('a string')), + ('mynum', mlarr(10))) + + # Dict like thing to give variables in defined order + class C: + def items(self): + return names_vars + stream = BytesIO() + savemat(stream, C()) + varmats = varmats_from_mat(stream) + assert_equal(len(varmats), 3) + for i in range(3): + name, var_stream = varmats[i] + exp_name, exp_res = names_vars[i] + assert_equal(name, exp_name) + res = loadmat(var_stream) + assert_array_equal(res[name], exp_res) + + +def test_one_by_zero(): + # Test 1x0 chars get read correctly + func_eg = pjoin(test_data_path, 'one_by_zero_char.mat') + fp = open(func_eg, 'rb') + rdr = MatFile5Reader(fp) + d = rdr.get_variables() + fp.close() + assert_equal(d['var'].shape, (0,)) + + +def test_load_mat4_le(): + # We were getting byte order wrong when reading little-endian floa64 dense + # matrices on big-endian platforms + mat4_fname = pjoin(test_data_path, 'test_mat4_le_floats.mat') + vars = loadmat(mat4_fname) + assert_array_equal(vars['a'], [[0.1, 1.2]]) + + +def test_unicode_mat4(): + # Mat4 should save unicode as latin1 + bio = BytesIO() + var = {'second_cat': 'Schrödinger'} + savemat(bio, var, format='4') + var_back = loadmat(bio) + assert_equal(var_back['second_cat'], var['second_cat']) + + +def test_logical_sparse(): + # Test we can read logical sparse stored in mat file as bytes. + # See https://github.com/scipy/scipy/issues/3539. + # In some files saved by MATLAB, the sparse data elements (Real Part + # Subelement in MATLAB speak) are stored with apparent type double + # (miDOUBLE) but are in fact single bytes. + filename = pjoin(test_data_path,'logical_sparse.mat') + # Before fix, this would crash with: + # ValueError: indices and data should have the same size + d = loadmat(filename, struct_as_record=True, spmatrix=False) + log_sp = d['sp_log_5_4'] + assert_(issparse(log_sp) and log_sp.format == "csc") + assert_equal(log_sp.dtype.type, np.bool_) + assert_array_equal(log_sp.toarray(), + [[True, True, True, False], + [False, False, True, False], + [False, False, True, False], + [False, False, False, False], + [False, False, False, False]]) + + +def test_empty_sparse(): + # Can we read empty sparse matrices? + sio = BytesIO() + import scipy.sparse + empty_sparse = scipy.sparse.csr_array([[0,0],[0,0]]) + savemat(sio, dict(x=empty_sparse)) + sio.seek(0) + + res = loadmat(sio, spmatrix=False) + assert not scipy.sparse.isspmatrix(res['x']) + res = loadmat(sio, spmatrix=True) + assert scipy.sparse.isspmatrix(res['x']) + res = loadmat(sio) # chk default + assert scipy.sparse.isspmatrix(res['x']) + + assert_array_equal(res['x'].shape, empty_sparse.shape) + assert_array_equal(res['x'].toarray(), 0) + # Do empty sparse matrices get written with max nnz 1? + # See https://github.com/scipy/scipy/issues/4208 + sio.seek(0) + reader = MatFile5Reader(sio) + reader.initialize_read() + reader.read_file_header() + hdr, _ = reader.read_var_header() + assert_equal(hdr.nzmax, 1) + + +def test_empty_mat_error(): + # Test we get a specific warning for an empty mat file + sio = BytesIO() + assert_raises(MatReadError, loadmat, sio) + + +def test_miuint32_compromise(): + # Reader should accept miUINT32 for miINT32, but check signs + # mat file with miUINT32 for miINT32, but OK values + filename = pjoin(test_data_path, 'miuint32_for_miint32.mat') + res = loadmat(filename) + assert_equal(res['an_array'], np.arange(10)[None, :]) + # mat file with miUINT32 for miINT32, with negative value + filename = pjoin(test_data_path, 'bad_miuint32.mat') + with assert_raises(ValueError): + loadmat(filename) + + +def test_miutf8_for_miint8_compromise(): + # Check reader accepts ascii as miUTF8 for array names + filename = pjoin(test_data_path, 'miutf8_array_name.mat') + res = loadmat(filename) + assert_equal(res['array_name'], [[1]]) + # mat file with non-ascii utf8 name raises error + filename = pjoin(test_data_path, 'bad_miutf8_array_name.mat') + with assert_raises(ValueError): + loadmat(filename) + + +def test_bad_utf8(): + # Check that reader reads bad UTF with 'replace' option + filename = pjoin(test_data_path,'broken_utf8.mat') + res = loadmat(filename) + assert_equal(res['bad_string'], + b'\x80 am broken'.decode('utf8', 'replace')) + + +def test_save_unicode_field(tmpdir): + filename = os.path.join(str(tmpdir), 'test.mat') + test_dict = {'a':{'b':1,'c':'test_str'}} + savemat(filename, test_dict) + + +def test_save_custom_array_type(tmpdir): + class CustomArray: + def __array__(self, dtype=None, copy=None): + return np.arange(6.0).reshape(2, 3) + a = CustomArray() + filename = os.path.join(str(tmpdir), 'test.mat') + savemat(filename, {'a': a}) + out = loadmat(filename) + assert_array_equal(out['a'], np.array(a)) + + +def test_filenotfound(): + # Check the correct error is thrown + assert_raises(OSError, loadmat, "NotExistentFile00.mat") + assert_raises(OSError, loadmat, "NotExistentFile00") + + +def test_simplify_cells(): + # Test output when simplify_cells=True + filename = pjoin(test_data_path, 'testsimplecell.mat') + res1 = loadmat(filename, simplify_cells=True) + res2 = loadmat(filename, simplify_cells=False) + assert_(isinstance(res1["s"], dict)) + assert_(isinstance(res2["s"], np.ndarray)) + assert_array_equal(res1["s"]["mycell"], np.array(["a", "b", "c"])) + + +@pytest.mark.parametrize('version, filt, regex', [ + (0, '_4*_*', None), + (1, '_5*_*', None), + (1, '_6*_*', None), + (1, '_7*_*', '^((?!hdf5).)*$'), # not containing hdf5 + (2, '_7*_*', '.*hdf5.*'), + (1, '8*_*', None), +]) +def test_matfile_version(version, filt, regex): + use_filt = pjoin(test_data_path, f'test*{filt}.mat') + files = glob(use_filt) + if regex is not None: + files = [file for file in files if re.match(regex, file) is not None] + assert len(files) > 0, \ + f"No files for version {version} using filter {filt}" + for file in files: + got_version = matfile_version(file) + assert got_version[0] == version + + +def test_opaque(): + """Test that we can read a MatlabOpaque object.""" + data = loadmat(pjoin(test_data_path, 'parabola.mat')) + assert isinstance(data['parabola'], MatlabFunction) + assert isinstance(data['parabola'].item()[3].item()[3], MatlabOpaque) + + +def test_opaque_simplify(): + """Test that we can read a MatlabOpaque object when simplify_cells=True.""" + data = loadmat(pjoin(test_data_path, 'parabola.mat'), simplify_cells=True) + assert isinstance(data['parabola'], MatlabFunction) + + +def test_deprecation(): + """Test that access to previous attributes still works.""" + # This should be accessible immediately from scipy.io import + with assert_warns(DeprecationWarning): + scipy.io.matlab.mio5_params.MatlabOpaque + + # These should be importable but warn as well + with assert_warns(DeprecationWarning): + from scipy.io.matlab.miobase import MatReadError # noqa: F401 + + +def test_gh_17992(tmp_path): + rng = np.random.default_rng(12345) + outfile = tmp_path / "lists.mat" + array_one = rng.random((5,3)) + array_two = rng.random((6,3)) + list_of_arrays = [array_one, array_two] + # warning suppression only needed for NumPy < 1.24.0 + with np.testing.suppress_warnings() as sup: + sup.filter(VisibleDeprecationWarning) + savemat(outfile, + {'data': list_of_arrays}, + long_field_names=True, + do_compression=True) + # round trip check + new_dict = {} + loadmat(outfile, + new_dict) + assert_allclose(new_dict["data"][0][0], array_one) + assert_allclose(new_dict["data"][0][1], array_two) + + +def test_gh_19659(tmp_path): + d = { + "char_array": np.array([list("char"), list("char")], dtype="U1"), + "string_array": np.array(["string", "string"]), + } + outfile = tmp_path / "tmp.mat" + # should not error: + savemat(outfile, d, format="4") + + +def test_large_m4(): + # Test we can read a Matlab 4 file with array > 2GB. + # (In fact, test we get the correct error from reading a truncated + # version). + # See https://github.com/scipy/scipy/issues/21256 + # Data file is first 1024 bytes of: + # >>> a = np.zeros((134217728, 3)) + # >>> siom.savemat('big_m4.mat', {'a': a}, format='4') + truncated_mat = pjoin(test_data_path, 'debigged_m4.mat') + match = ("Not enough bytes to read matrix 'a';" + if np.intp == np.int64 else + "Variable 'a' has byte length longer than largest possible") + with pytest.raises(ValueError, match=match): + loadmat(truncated_mat) + + +def test_gh_19223(): + from scipy.io.matlab import varmats_from_mat # noqa: F401 + +def test_corrupt_files(): + # Test we can detect truncated or corrupt (all zero) files. + for n in (2, 4, 10, 19): + with pytest.raises(MatReadError, + match="Mat file appears to be truncated"): + loadmat(BytesIO(b'\x00' * n)) + with pytest.raises(MatReadError, + match="Mat file appears to be corrupt"): + loadmat(BytesIO(b'\x00' * 20)) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/test_mio5_utils.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/test_mio5_utils.py new file mode 100644 index 0000000000000000000000000000000000000000..b3f27114c4a4ed10c1a2526058f4d0dbbd0e5638 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/test_mio5_utils.py @@ -0,0 +1,179 @@ +""" Testing mio5_utils Cython module + +""" +import sys + +from io import BytesIO + +import numpy as np + +from numpy.testing import assert_array_equal, assert_equal, assert_ +from pytest import raises as assert_raises + +import scipy.io.matlab._byteordercodes as boc +import scipy.io.matlab._streams as streams +import scipy.io.matlab._mio5_params as mio5p +import scipy.io.matlab._mio5_utils as m5u + + +def test_byteswap(): + for val in ( + 1, + 0x100, + 0x10000): + a = np.array(val, dtype=np.uint32) + b = a.byteswap() + c = m5u.byteswap_u4(a) + assert_equal(b.item(), c) + d = m5u.byteswap_u4(c) + assert_equal(a.item(), d) + + +def _make_tag(base_dt, val, mdtype, sde=False): + ''' Makes a simple matlab tag, full or sde ''' + base_dt = np.dtype(base_dt) + bo = boc.to_numpy_code(base_dt.byteorder) + byte_count = base_dt.itemsize + if not sde: + udt = bo + 'u4' + padding = 8 - (byte_count % 8) + all_dt = [('mdtype', udt), + ('byte_count', udt), + ('val', base_dt)] + if padding: + all_dt.append(('padding', 'u1', padding)) + else: # is sde + udt = bo + 'u2' + padding = 4-byte_count + if bo == '<': # little endian + all_dt = [('mdtype', udt), + ('byte_count', udt), + ('val', base_dt)] + else: # big endian + all_dt = [('byte_count', udt), + ('mdtype', udt), + ('val', base_dt)] + if padding: + all_dt.append(('padding', 'u1', padding)) + tag = np.zeros((1,), dtype=all_dt) + tag['mdtype'] = mdtype + tag['byte_count'] = byte_count + tag['val'] = val + return tag + + +def _write_stream(stream, *strings): + stream.truncate(0) + stream.seek(0) + for s in strings: + stream.write(s) + stream.seek(0) + + +def _make_readerlike(stream, byte_order=boc.native_code): + class R: + pass + r = R() + r.mat_stream = stream + r.byte_order = byte_order + r.struct_as_record = True + r.uint16_codec = sys.getdefaultencoding() + r.chars_as_strings = False + r.mat_dtype = False + r.squeeze_me = False + return r + + +def test_read_tag(): + # mainly to test errors + # make reader-like thing + str_io = BytesIO() + r = _make_readerlike(str_io) + c_reader = m5u.VarReader5(r) + # This works for StringIO but _not_ BytesIO + assert_raises(OSError, c_reader.read_tag) + # bad SDE + tag = _make_tag('i4', 1, mio5p.miINT32, sde=True) + tag['byte_count'] = 5 + _write_stream(str_io, tag.tobytes()) + assert_raises(ValueError, c_reader.read_tag) + + +def test_read_stream(): + tag = _make_tag('i4', 1, mio5p.miINT32, sde=True) + tag_str = tag.tobytes() + str_io = BytesIO(tag_str) + st = streams.make_stream(str_io) + s = streams._read_into(st, tag.itemsize) + assert_equal(s, tag.tobytes()) + + +def test_read_numeric(): + # make reader-like thing + str_io = BytesIO() + r = _make_readerlike(str_io) + # check simplest of tags + for base_dt, val, mdtype in (('u2', 30, mio5p.miUINT16), + ('i4', 1, mio5p.miINT32), + ('i2', -1, mio5p.miINT16)): + for byte_code in ('<', '>'): + r.byte_order = byte_code + c_reader = m5u.VarReader5(r) + assert_equal(c_reader.little_endian, byte_code == '<') + assert_equal(c_reader.is_swapped, byte_code != boc.native_code) + for sde_f in (False, True): + dt = np.dtype(base_dt).newbyteorder(byte_code) + a = _make_tag(dt, val, mdtype, sde_f) + a_str = a.tobytes() + _write_stream(str_io, a_str) + el = c_reader.read_numeric() + assert_equal(el, val) + # two sequential reads + _write_stream(str_io, a_str, a_str) + el = c_reader.read_numeric() + assert_equal(el, val) + el = c_reader.read_numeric() + assert_equal(el, val) + + +def test_read_numeric_writeable(): + # make reader-like thing + str_io = BytesIO() + r = _make_readerlike(str_io, '<') + c_reader = m5u.VarReader5(r) + dt = np.dtype('' + rdr.mat_stream.read(4) # presumably byte padding + mdict = read_minimat_vars(rdr) + fp.close() + return mdict + + +def test_jottings(): + # example + fname = os.path.join(test_data_path, 'parabola.mat') + read_workspace_vars(fname) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/test_mio_utils.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/test_mio_utils.py new file mode 100644 index 0000000000000000000000000000000000000000..1d19a9797faa2221307a7330b69fffa26410f624 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/test_mio_utils.py @@ -0,0 +1,45 @@ +""" Testing + +""" + +import numpy as np + +from numpy.testing import assert_array_equal, assert_ + +from scipy.io.matlab._mio_utils import squeeze_element, chars_to_strings + + +def test_squeeze_element(): + a = np.zeros((1,3)) + assert_array_equal(np.squeeze(a), squeeze_element(a)) + # 0-D output from squeeze gives scalar + sq_int = squeeze_element(np.zeros((1,1), dtype=float)) + assert_(isinstance(sq_int, float)) + # Unless it's a structured array + sq_sa = squeeze_element(np.zeros((1,1),dtype=[('f1', 'f')])) + assert_(isinstance(sq_sa, np.ndarray)) + # Squeezing empty arrays maintain their dtypes. + sq_empty = squeeze_element(np.empty(0, np.uint8)) + assert sq_empty.dtype == np.uint8 + + +def test_chars_strings(): + # chars as strings + strings = ['learn ', 'python', 'fast ', 'here '] + str_arr = np.array(strings, dtype='U6') # shape (4,) + chars = [list(s) for s in strings] + char_arr = np.array(chars, dtype='U1') # shape (4,6) + assert_array_equal(chars_to_strings(char_arr), str_arr) + ca2d = char_arr.reshape((2,2,6)) + sa2d = str_arr.reshape((2,2)) + assert_array_equal(chars_to_strings(ca2d), sa2d) + ca3d = char_arr.reshape((1,2,2,6)) + sa3d = str_arr.reshape((1,2,2)) + assert_array_equal(chars_to_strings(ca3d), sa3d) + # Fortran ordered arrays + char_arrf = np.array(chars, dtype='U1', order='F') # shape (4,6) + assert_array_equal(chars_to_strings(char_arrf), str_arr) + # empty array + arr = np.array([['']], dtype='U1') + out_arr = np.array([''], dtype='U1') + assert_array_equal(chars_to_strings(arr), out_arr) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/test_miobase.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/test_miobase.py new file mode 100644 index 0000000000000000000000000000000000000000..d8c8eb2a56aaa1d1de77bfb90c859ed0af0b7bc4 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/test_miobase.py @@ -0,0 +1,32 @@ +""" Testing miobase module +""" + +import numpy as np + +from numpy.testing import assert_equal +from pytest import raises as assert_raises + +from scipy.io.matlab._miobase import matdims + + +def test_matdims(): + # Test matdims dimension finder + assert_equal(matdims(np.array(1)), (1, 1)) # NumPy scalar + assert_equal(matdims(np.array([1])), (1, 1)) # 1-D array, 1 element + assert_equal(matdims(np.array([1,2])), (2, 1)) # 1-D array, 2 elements + assert_equal(matdims(np.array([[2],[3]])), (2, 1)) # 2-D array, column vector + assert_equal(matdims(np.array([[2,3]])), (1, 2)) # 2-D array, row vector + # 3d array, rowish vector + assert_equal(matdims(np.array([[[2,3]]])), (1, 1, 2)) + assert_equal(matdims(np.array([])), (0, 0)) # empty 1-D array + assert_equal(matdims(np.array([[]])), (1, 0)) # empty 2-D array + assert_equal(matdims(np.array([[[]]])), (1, 1, 0)) # empty 3-D array + assert_equal(matdims(np.empty((1, 0, 1))), (1, 0, 1)) # empty 3-D array + # Optional argument flips 1-D shape behavior. + assert_equal(matdims(np.array([1,2]), 'row'), (1, 2)) # 1-D array, 2 elements + # The argument has to make sense though + assert_raises(ValueError, matdims, np.array([1,2]), 'bizarre') + # Check empty sparse matrices get their own shape + from scipy.sparse import csr_array, csc_array + assert_equal(matdims(csr_array(np.zeros((3, 3)))), (3, 3)) + assert_equal(matdims(csc_array(np.zeros((2, 2)))), (2, 2)) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/test_pathological.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/test_pathological.py new file mode 100644 index 0000000000000000000000000000000000000000..c5c86decb7e90f69f293e90eba74fb47dd4f1277 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/test_pathological.py @@ -0,0 +1,33 @@ +""" Test reading of files not conforming to matlab specification + +We try and read any file that matlab reads, these files included +""" +from os.path import dirname, join as pjoin + +from numpy.testing import assert_ +from pytest import raises as assert_raises + +from scipy.io.matlab._mio import loadmat + +TEST_DATA_PATH = pjoin(dirname(__file__), 'data') + + +def test_multiple_fieldnames(): + # Example provided by Dharhas Pothina + # Extracted using mio5.varmats_from_mat + multi_fname = pjoin(TEST_DATA_PATH, 'nasty_duplicate_fieldnames.mat') + vars = loadmat(multi_fname) + funny_names = vars['Summary'].dtype.names + assert_({'_1_Station_Q', '_2_Station_Q', + '_3_Station_Q'}.issubset(funny_names)) + + +def test_malformed1(): + # Example from gh-6072 + # Contains malformed header data, which previously resulted into a + # buffer overflow. + # + # Should raise an exception, not segfault + fname = pjoin(TEST_DATA_PATH, 'malformed1.mat') + with open(fname, 'rb') as f: + assert_raises(ValueError, loadmat, f) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/test_streams.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/test_streams.py new file mode 100644 index 0000000000000000000000000000000000000000..d8768d8e9251c6e47debeb65dff3ec056d38ee56 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/matlab/tests/test_streams.py @@ -0,0 +1,232 @@ +""" Testing + +""" + +import os +import zlib + +from io import BytesIO + + +from tempfile import mkstemp +from contextlib import contextmanager + +import numpy as np + +from numpy.testing import assert_, assert_equal +from pytest import raises as assert_raises + +from scipy.io.matlab._streams import (make_stream, + GenericStream, ZlibInputStream, + _read_into, _read_string, BLOCK_SIZE) + + +@contextmanager +def setup_test_file(): + val = b'a\x00string' + fd, fname = mkstemp() + + with os.fdopen(fd, 'wb') as fs: + fs.write(val) + with open(fname, 'rb') as fs: + gs = BytesIO(val) + cs = BytesIO(val) + yield fs, gs, cs + os.unlink(fname) + + +def test_make_stream(): + with setup_test_file() as (fs, gs, cs): + # test stream initialization + assert_(isinstance(make_stream(gs), GenericStream)) + + +def test_tell_seek(): + with setup_test_file() as (fs, gs, cs): + for s in (fs, gs, cs): + st = make_stream(s) + res = st.seek(0) + assert_equal(res, 0) + assert_equal(st.tell(), 0) + res = st.seek(5) + assert_equal(res, 0) + assert_equal(st.tell(), 5) + res = st.seek(2, 1) + assert_equal(res, 0) + assert_equal(st.tell(), 7) + res = st.seek(-2, 2) + assert_equal(res, 0) + assert_equal(st.tell(), 6) + + +def test_read(): + with setup_test_file() as (fs, gs, cs): + for s in (fs, gs, cs): + st = make_stream(s) + st.seek(0) + res = st.read(-1) + assert_equal(res, b'a\x00string') + st.seek(0) + res = st.read(4) + assert_equal(res, b'a\x00st') + # read into + st.seek(0) + res = _read_into(st, 4) + assert_equal(res, b'a\x00st') + res = _read_into(st, 4) + assert_equal(res, b'ring') + assert_raises(OSError, _read_into, st, 2) + # read alloc + st.seek(0) + res = _read_string(st, 4) + assert_equal(res, b'a\x00st') + res = _read_string(st, 4) + assert_equal(res, b'ring') + assert_raises(OSError, _read_string, st, 2) + + +class TestZlibInputStream: + def _get_data(self, size): + data = np.random.randint(0, 256, size).astype(np.uint8).tobytes() + compressed_data = zlib.compress(data) + stream = BytesIO(compressed_data) + return stream, len(compressed_data), data + + def test_read(self): + SIZES = [0, 1, 10, BLOCK_SIZE//2, BLOCK_SIZE-1, + BLOCK_SIZE, BLOCK_SIZE+1, 2*BLOCK_SIZE-1] + + READ_SIZES = [BLOCK_SIZE//2, BLOCK_SIZE-1, + BLOCK_SIZE, BLOCK_SIZE+1] + + def check(size, read_size): + compressed_stream, compressed_data_len, data = self._get_data(size) + stream = ZlibInputStream(compressed_stream, compressed_data_len) + data2 = b'' + so_far = 0 + while True: + block = stream.read(min(read_size, + size - so_far)) + if not block: + break + so_far += len(block) + data2 += block + assert_equal(data, data2) + + for size in SIZES: + for read_size in READ_SIZES: + check(size, read_size) + + def test_read_max_length(self): + size = 1234 + data = np.random.randint(0, 256, size).astype(np.uint8).tobytes() + compressed_data = zlib.compress(data) + compressed_stream = BytesIO(compressed_data + b"abbacaca") + stream = ZlibInputStream(compressed_stream, len(compressed_data)) + + stream.read(len(data)) + assert_equal(compressed_stream.tell(), len(compressed_data)) + + assert_raises(OSError, stream.read, 1) + + def test_read_bad_checksum(self): + data = np.random.randint(0, 256, 10).astype(np.uint8).tobytes() + compressed_data = zlib.compress(data) + + # break checksum + compressed_data = (compressed_data[:-1] + + bytes([(compressed_data[-1] + 1) & 255])) + + compressed_stream = BytesIO(compressed_data) + stream = ZlibInputStream(compressed_stream, len(compressed_data)) + + assert_raises(zlib.error, stream.read, len(data)) + + def test_seek(self): + compressed_stream, compressed_data_len, data = self._get_data(1024) + + stream = ZlibInputStream(compressed_stream, compressed_data_len) + + stream.seek(123) + p = 123 + assert_equal(stream.tell(), p) + d1 = stream.read(11) + assert_equal(d1, data[p:p+11]) + + stream.seek(321, 1) + p = 123+11+321 + assert_equal(stream.tell(), p) + d2 = stream.read(21) + assert_equal(d2, data[p:p+21]) + + stream.seek(641, 0) + p = 641 + assert_equal(stream.tell(), p) + d3 = stream.read(11) + assert_equal(d3, data[p:p+11]) + + assert_raises(OSError, stream.seek, 10, 2) + assert_raises(OSError, stream.seek, -1, 1) + assert_raises(ValueError, stream.seek, 1, 123) + + stream.seek(10000, 1) + assert_raises(OSError, stream.read, 12) + + def test_seek_bad_checksum(self): + data = np.random.randint(0, 256, 10).astype(np.uint8).tobytes() + compressed_data = zlib.compress(data) + + # break checksum + compressed_data = (compressed_data[:-1] + + bytes([(compressed_data[-1] + 1) & 255])) + + compressed_stream = BytesIO(compressed_data) + stream = ZlibInputStream(compressed_stream, len(compressed_data)) + + assert_raises(zlib.error, stream.seek, len(data)) + + def test_all_data_read(self): + compressed_stream, compressed_data_len, data = self._get_data(1024) + stream = ZlibInputStream(compressed_stream, compressed_data_len) + assert_(not stream.all_data_read()) + stream.seek(512) + assert_(not stream.all_data_read()) + stream.seek(1024) + assert_(stream.all_data_read()) + + def test_all_data_read_overlap(self): + COMPRESSION_LEVEL = 6 + + data = np.arange(33707000).astype(np.uint8).tobytes() + compressed_data = zlib.compress(data, COMPRESSION_LEVEL) + compressed_data_len = len(compressed_data) + + # check that part of the checksum overlaps + assert_(compressed_data_len == BLOCK_SIZE + 2) + + compressed_stream = BytesIO(compressed_data) + stream = ZlibInputStream(compressed_stream, compressed_data_len) + assert_(not stream.all_data_read()) + stream.seek(len(data)) + assert_(stream.all_data_read()) + + def test_all_data_read_bad_checksum(self): + COMPRESSION_LEVEL = 6 + + data = np.arange(33707000).astype(np.uint8).tobytes() + compressed_data = zlib.compress(data, COMPRESSION_LEVEL) + compressed_data_len = len(compressed_data) + + # check that part of the checksum overlaps + assert_(compressed_data_len == BLOCK_SIZE + 2) + + # break checksum + compressed_data = (compressed_data[:-1] + + bytes([(compressed_data[-1] + 1) & 255])) + + compressed_stream = BytesIO(compressed_data) + stream = ZlibInputStream(compressed_stream, compressed_data_len) + assert_(not stream.all_data_read()) + stream.seek(len(data)) + + assert_raises(zlib.error, stream.all_data_read) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/mmio.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/mmio.py new file mode 100644 index 0000000000000000000000000000000000000000..67cf0684cbf9468468027957a5b7f3da2c43c845 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/mmio.py @@ -0,0 +1,17 @@ +# This file is not meant for public use and will be removed in SciPy v2.0.0. +# Use the `scipy.io` namespace for importing the functions +# included below. + +from scipy._lib.deprecation import _sub_module_deprecation + +__all__ = ["mminfo", "mmread", "mmwrite"] # noqa: F822 + + +def __dir__(): + return __all__ + + +def __getattr__(name): + return _sub_module_deprecation(sub_package="io", module="mmio", + private_modules=["_mmio"], all=__all__, + attribute=name) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/netcdf.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/netcdf.py new file mode 100644 index 0000000000000000000000000000000000000000..c1f119dd2bad72d772c3d1db6ceec9fd3d91316d --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/netcdf.py @@ -0,0 +1,17 @@ +# This file is not meant for public use and will be removed in SciPy v2.0.0. +# Use the `scipy.io` namespace for importing the functions +# included below. + +from scipy._lib.deprecation import _sub_module_deprecation + +__all__ = ["netcdf_file", "netcdf_variable"] # noqa: F822 + + +def __dir__(): + return __all__ + + +def __getattr__(name): + return _sub_module_deprecation(sub_package="io", module="netcdf", + private_modules=["_netcdf"], all=__all__, + attribute=name) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/__init__.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/Transparent Busy.ani b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/Transparent Busy.ani new file mode 100644 index 0000000000000000000000000000000000000000..3be500032786398c3efdbd9f873f705b6c1636bd Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/Transparent Busy.ani differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_1d.sav b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_1d.sav new file mode 100644 index 0000000000000000000000000000000000000000..619a1259670a361ac76ffa86c481a813dbaec07a Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_1d.sav differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_2d.sav b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_2d.sav new file mode 100644 index 0000000000000000000000000000000000000000..804d8b1a8a90636c880e974b6f85bd385033306b Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_2d.sav differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_3d.sav b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_3d.sav new file mode 100644 index 0000000000000000000000000000000000000000..3fa56c450eaa916d9c91b492ba17e7e843df2d53 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_3d.sav differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_4d.sav b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_4d.sav new file mode 100644 index 0000000000000000000000000000000000000000..4bb951e274a399f091ff70b639d6e3b55ee1e122 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_4d.sav differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_5d.sav b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_5d.sav new file mode 100644 index 0000000000000000000000000000000000000000..2854dbc8b1e53f298ac3b135eac1f06e73940152 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_5d.sav differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_6d.sav b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_6d.sav new file mode 100644 index 0000000000000000000000000000000000000000..91588d348d5f89af354209840062202d5b28c1df Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_6d.sav differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_7d.sav b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_7d.sav new file mode 100644 index 0000000000000000000000000000000000000000..3e978fad540a8979435d4561de151573696affd8 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_7d.sav differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_8d.sav b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_8d.sav new file mode 100644 index 0000000000000000000000000000000000000000..f699fe2427dfe876283de0fcade2c2325a262061 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_8d.sav differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_pointer_1d.sav b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_pointer_1d.sav new file mode 100644 index 0000000000000000000000000000000000000000..8e3a402c60a515149811e2ca21628e97180c4956 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_pointer_1d.sav differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_pointer_2d.sav b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_pointer_2d.sav new file mode 100644 index 0000000000000000000000000000000000000000..dd3504f0ecfaed178ace02e1a8a84650111c3936 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_pointer_2d.sav differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_pointer_3d.sav b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_pointer_3d.sav new file mode 100644 index 0000000000000000000000000000000000000000..285da7f78ffbbf2155fd2e4e648f19a1d3a42ac3 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_pointer_3d.sav differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_pointer_4d.sav b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_pointer_4d.sav new file mode 100644 index 0000000000000000000000000000000000000000..d99fa48f0a43ec06c3101560f9cade829c8b1940 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_pointer_4d.sav differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_pointer_5d.sav b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_pointer_5d.sav new file mode 100644 index 0000000000000000000000000000000000000000..de5e984e49f507ae550b1ae2fd54b799e742a195 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_pointer_5d.sav differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_pointer_6d.sav b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_pointer_6d.sav new file mode 100644 index 0000000000000000000000000000000000000000..bb76671a65be41fd2a426146c6c366f1e7fb07c3 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_pointer_6d.sav differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_pointer_7d.sav b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_pointer_7d.sav new file mode 100644 index 0000000000000000000000000000000000000000..995d23c6ed05b095442b6247b09191126f797f23 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_pointer_7d.sav differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_pointer_8d.sav b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_pointer_8d.sav new file mode 100644 index 0000000000000000000000000000000000000000..4249ec62119e264d55a81d3faf9c87dcaed1c7c8 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/array_float32_pointer_8d.sav differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/example_1.nc b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/example_1.nc new file mode 100644 index 0000000000000000000000000000000000000000..5775622d0ef85828b436dffcd21366f7538fc55c Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/example_1.nc differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/example_2.nc b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/example_2.nc new file mode 100644 index 0000000000000000000000000000000000000000..07db1cd986a4c3b9929c01c1f22bcc3f562b1c16 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/example_2.nc differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/example_3_maskedvals.nc b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/example_3_maskedvals.nc new file mode 100644 index 0000000000000000000000000000000000000000..57f8bf9da3bca295c15508963c77a870222af0bc Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/example_3_maskedvals.nc differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/fortran-3x3d-2i.dat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/fortran-3x3d-2i.dat new file mode 100644 index 0000000000000000000000000000000000000000..87731eb9d4b1f2ac827a212436fe6de175431e11 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/fortran-3x3d-2i.dat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/fortran-mixed.dat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/fortran-mixed.dat new file mode 100644 index 0000000000000000000000000000000000000000..a165a7a30424b20af9a3a0636c5e655239ea6fa5 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/fortran-mixed.dat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/fortran-sf8-11x1x10.dat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/fortran-sf8-11x1x10.dat new file mode 100644 index 0000000000000000000000000000000000000000..c3bb9dcbe50ef784ce3282b28e53f4c40beb48ce Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/fortran-sf8-11x1x10.dat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/fortran-sf8-15x10x22.dat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/fortran-sf8-15x10x22.dat new file mode 100644 index 0000000000000000000000000000000000000000..351801fd47a2e3e48d9b63034fbae28f8318c9f9 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/fortran-sf8-15x10x22.dat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/fortran-sf8-1x1x1.dat b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/fortran-sf8-1x1x1.dat new file mode 100644 index 0000000000000000000000000000000000000000..64bf92f74a457d2f4bc42798493db15cc3ab1008 Binary files /dev/null and b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/data/fortran-sf8-1x1x1.dat differ diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/test_fortran.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/test_fortran.py new file mode 100644 index 0000000000000000000000000000000000000000..e6e2ecdb8cd332a0a7806bdbc442c66124225077 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/test_fortran.py @@ -0,0 +1,264 @@ +''' Tests for fortran sequential files ''' + +import tempfile +import shutil +import os +from os import path +from glob import iglob +import threading +import re + +from numpy.testing import assert_equal, assert_allclose +import numpy as np +import pytest + +from scipy.io import (FortranFile, + _test_fortran, + FortranEOFError, + FortranFormattingError) + + +DATA_PATH = path.join(path.dirname(__file__), 'data') + + +@pytest.fixture +def io_lock(): + return threading.Lock() + + +def test_fortranfiles_read(io_lock): + for filename in iglob(path.join(DATA_PATH, "fortran-*-*x*x*.dat")): + m = re.search(r'fortran-([^-]+)-(\d+)x(\d+)x(\d+).dat', filename, re.I) + if not m: + raise RuntimeError(f"Couldn't match {filename} filename to regex") + + dims = (int(m.group(2)), int(m.group(3)), int(m.group(4))) + + dtype = m.group(1).replace('s', '<') + + with io_lock: + f = FortranFile(filename, 'r', ' 0] = 1 + info = (2, 2, 3, 'coordinate', 'pattern', 'general') + mmwrite(self.fn, a, field='pattern') + assert_equal(mminfo(self.fn), info) + b = mmread(self.fn, spmatrix=False) + assert_array_almost_equal(p, b.toarray()) + assert not scipy.sparse.isspmatrix(b) + + b = mmread(self.fn, spmatrix=True) + assert scipy.sparse.isspmatrix(b) + b = mmread(self.fn) # chk default + assert scipy.sparse.isspmatrix(b) + + def test_gh13634_non_skew_symmetric_int(self): + a = scipy.sparse.csr_array([[1, 2], [-2, 99]], dtype=np.int32) + self.check_exact(a, (2, 2, 4, 'coordinate', 'integer', 'general')) + + def test_gh13634_non_skew_symmetric_float(self): + a = scipy.sparse.csr_array([[1, 2], [-2, 99.]], dtype=np.float32) + self.check(a, (2, 2, 4, 'coordinate', 'real', 'general')) + + +_32bit_integer_dense_example = '''\ +%%MatrixMarket matrix array integer general +2 2 +2147483647 +2147483646 +2147483647 +2147483646 +''' + +_32bit_integer_sparse_example = '''\ +%%MatrixMarket matrix coordinate integer symmetric +2 2 2 +1 1 2147483647 +2 2 2147483646 +''' + +_64bit_integer_dense_example = '''\ +%%MatrixMarket matrix array integer general +2 2 + 2147483648 +-9223372036854775806 + -2147483648 + 9223372036854775807 +''' + +_64bit_integer_sparse_general_example = '''\ +%%MatrixMarket matrix coordinate integer general +2 2 3 +1 1 2147483648 +1 2 9223372036854775807 +2 2 9223372036854775807 +''' + +_64bit_integer_sparse_symmetric_example = '''\ +%%MatrixMarket matrix coordinate integer symmetric +2 2 3 +1 1 2147483648 +1 2 -9223372036854775807 +2 2 9223372036854775807 +''' + +_64bit_integer_sparse_skew_example = '''\ +%%MatrixMarket matrix coordinate integer skew-symmetric +2 2 3 +1 1 2147483648 +1 2 -9223372036854775807 +2 2 9223372036854775807 +''' + +_over64bit_integer_dense_example = '''\ +%%MatrixMarket matrix array integer general +2 2 + 2147483648 +9223372036854775807 + 2147483648 +9223372036854775808 +''' + +_over64bit_integer_sparse_example = '''\ +%%MatrixMarket matrix coordinate integer symmetric +2 2 2 +1 1 2147483648 +2 2 19223372036854775808 +''' + + +class TestMMIOReadLargeIntegers: + def setup_method(self): + self.tmpdir = mkdtemp(suffix=str(threading.get_native_id())) + self.fn = os.path.join(self.tmpdir, 'testfile.mtx') + + def teardown_method(self): + shutil.rmtree(self.tmpdir) + + def check_read(self, example, a, info, dense, over32, over64): + with open(self.fn, 'w') as f: + f.write(example) + assert_equal(mminfo(self.fn), info) + if ((over32 and (np.intp(0).itemsize < 8) and mmwrite == scipy.io._mmio.mmwrite) + or over64): + assert_raises(OverflowError, mmread, self.fn) + else: + b = mmread(self.fn, spmatrix=False) + if not dense: + b = b.toarray() + assert_equal(a, b) + + def test_read_32bit_integer_dense(self): + a = array([[2**31-1, 2**31-1], + [2**31-2, 2**31-2]], dtype=np.int64) + self.check_read(_32bit_integer_dense_example, + a, + (2, 2, 4, 'array', 'integer', 'general'), + dense=True, + over32=False, + over64=False) + + def test_read_32bit_integer_sparse(self): + a = array([[2**31-1, 0], + [0, 2**31-2]], dtype=np.int64) + self.check_read(_32bit_integer_sparse_example, + a, + (2, 2, 2, 'coordinate', 'integer', 'symmetric'), + dense=False, + over32=False, + over64=False) + + def test_read_64bit_integer_dense(self): + a = array([[2**31, -2**31], + [-2**63+2, 2**63-1]], dtype=np.int64) + self.check_read(_64bit_integer_dense_example, + a, + (2, 2, 4, 'array', 'integer', 'general'), + dense=True, + over32=True, + over64=False) + + def test_read_64bit_integer_sparse_general(self): + a = array([[2**31, 2**63-1], + [0, 2**63-1]], dtype=np.int64) + self.check_read(_64bit_integer_sparse_general_example, + a, + (2, 2, 3, 'coordinate', 'integer', 'general'), + dense=False, + over32=True, + over64=False) + + def test_read_64bit_integer_sparse_symmetric(self): + a = array([[2**31, -2**63+1], + [-2**63+1, 2**63-1]], dtype=np.int64) + self.check_read(_64bit_integer_sparse_symmetric_example, + a, + (2, 2, 3, 'coordinate', 'integer', 'symmetric'), + dense=False, + over32=True, + over64=False) + + def test_read_64bit_integer_sparse_skew(self): + a = array([[2**31, -2**63+1], + [2**63-1, 2**63-1]], dtype=np.int64) + self.check_read(_64bit_integer_sparse_skew_example, + a, + (2, 2, 3, 'coordinate', 'integer', 'skew-symmetric'), + dense=False, + over32=True, + over64=False) + + def test_read_over64bit_integer_dense(self): + self.check_read(_over64bit_integer_dense_example, + None, + (2, 2, 4, 'array', 'integer', 'general'), + dense=True, + over32=True, + over64=True) + + def test_read_over64bit_integer_sparse(self): + self.check_read(_over64bit_integer_sparse_example, + None, + (2, 2, 2, 'coordinate', 'integer', 'symmetric'), + dense=False, + over32=True, + over64=True) + + +_general_example = '''\ +%%MatrixMarket matrix coordinate real general +%================================================================================= +% +% This ASCII file represents a sparse MxN matrix with L +% nonzeros in the following Matrix Market format: +% +% +----------------------------------------------+ +% |%%MatrixMarket matrix coordinate real general | <--- header line +% |% | <--+ +% |% comments | |-- 0 or more comment lines +% |% | <--+ +% | M N L | <--- rows, columns, entries +% | I1 J1 A(I1, J1) | <--+ +% | I2 J2 A(I2, J2) | | +% | I3 J3 A(I3, J3) | |-- L lines +% | . . . | | +% | IL JL A(IL, JL) | <--+ +% +----------------------------------------------+ +% +% Indices are 1-based, i.e. A(1,1) is the first element. +% +%================================================================================= + 5 5 8 + 1 1 1.000e+00 + 2 2 1.050e+01 + 3 3 1.500e-02 + 1 4 6.000e+00 + 4 2 2.505e+02 + 4 4 -2.800e+02 + 4 5 3.332e+01 + 5 5 1.200e+01 +''' + +_hermitian_example = '''\ +%%MatrixMarket matrix coordinate complex hermitian + 5 5 7 + 1 1 1.0 0 + 2 2 10.5 0 + 4 2 250.5 22.22 + 3 3 1.5e-2 0 + 4 4 -2.8e2 0 + 5 5 12. 0 + 5 4 0 33.32 +''' + +_skew_example = '''\ +%%MatrixMarket matrix coordinate real skew-symmetric + 5 5 7 + 1 1 1.0 + 2 2 10.5 + 4 2 250.5 + 3 3 1.5e-2 + 4 4 -2.8e2 + 5 5 12. + 5 4 0 +''' + +_symmetric_example = '''\ +%%MatrixMarket matrix coordinate real symmetric + 5 5 7 + 1 1 1.0 + 2 2 10.5 + 4 2 250.5 + 3 3 1.5e-2 + 4 4 -2.8e2 + 5 5 12. + 5 4 8 +''' + +_symmetric_pattern_example = '''\ +%%MatrixMarket matrix coordinate pattern symmetric + 5 5 7 + 1 1 + 2 2 + 4 2 + 3 3 + 4 4 + 5 5 + 5 4 +''' + +# example (without comment lines) from Figure 1 in +# https://math.nist.gov/MatrixMarket/reports/MMformat.ps +_empty_lines_example = '''\ +%%MatrixMarket MATRIX Coordinate Real General + + 5 5 8 + +1 1 1.0 +2 2 10.5 +3 3 1.5e-2 +4 4 -2.8E2 +5 5 12. + 1 4 6 + 4 2 250.5 + 4 5 33.32 + +''' + + +class TestMMIOCoordinate: + def setup_method(self): + self.tmpdir = mkdtemp(suffix=str(threading.get_native_id())) + self.fn = os.path.join(self.tmpdir, 'testfile.mtx') + + def teardown_method(self): + shutil.rmtree(self.tmpdir) + + def check_read(self, example, a, info): + f = open(self.fn, 'w') + f.write(example) + f.close() + assert_equal(mminfo(self.fn), info) + b = mmread(self.fn, spmatrix=False).toarray() + assert_array_almost_equal(a, b) + + def test_read_general(self): + a = [[1, 0, 0, 6, 0], + [0, 10.5, 0, 0, 0], + [0, 0, .015, 0, 0], + [0, 250.5, 0, -280, 33.32], + [0, 0, 0, 0, 12]] + self.check_read(_general_example, a, + (5, 5, 8, 'coordinate', 'real', 'general')) + + def test_read_hermitian(self): + a = [[1, 0, 0, 0, 0], + [0, 10.5, 0, 250.5 - 22.22j, 0], + [0, 0, .015, 0, 0], + [0, 250.5 + 22.22j, 0, -280, -33.32j], + [0, 0, 0, 33.32j, 12]] + self.check_read(_hermitian_example, a, + (5, 5, 7, 'coordinate', 'complex', 'hermitian')) + + def test_read_skew(self): + a = [[1, 0, 0, 0, 0], + [0, 10.5, 0, -250.5, 0], + [0, 0, .015, 0, 0], + [0, 250.5, 0, -280, 0], + [0, 0, 0, 0, 12]] + self.check_read(_skew_example, a, + (5, 5, 7, 'coordinate', 'real', 'skew-symmetric')) + + def test_read_symmetric(self): + a = [[1, 0, 0, 0, 0], + [0, 10.5, 0, 250.5, 0], + [0, 0, .015, 0, 0], + [0, 250.5, 0, -280, 8], + [0, 0, 0, 8, 12]] + self.check_read(_symmetric_example, a, + (5, 5, 7, 'coordinate', 'real', 'symmetric')) + + def test_read_symmetric_pattern(self): + a = [[1, 0, 0, 0, 0], + [0, 1, 0, 1, 0], + [0, 0, 1, 0, 0], + [0, 1, 0, 1, 1], + [0, 0, 0, 1, 1]] + self.check_read(_symmetric_pattern_example, a, + (5, 5, 7, 'coordinate', 'pattern', 'symmetric')) + + def test_read_empty_lines(self): + a = [[1, 0, 0, 6, 0], + [0, 10.5, 0, 0, 0], + [0, 0, .015, 0, 0], + [0, 250.5, 0, -280, 33.32], + [0, 0, 0, 0, 12]] + self.check_read(_empty_lines_example, a, + (5, 5, 8, 'coordinate', 'real', 'general')) + + def test_empty_write_read(self): + # https://github.com/scipy/scipy/issues/1410 (Trac #883) + + b = scipy.sparse.coo_array((10, 10)) + mmwrite(self.fn, b) + + assert_equal(mminfo(self.fn), + (10, 10, 0, 'coordinate', 'real', 'symmetric')) + a = b.toarray() + b = mmread(self.fn, spmatrix=False).toarray() + assert_array_almost_equal(a, b) + + def test_bzip2_py3(self): + # test if fix for #2152 works + try: + # bz2 module isn't always built when building Python. + import bz2 + except ImportError: + return + I = array([0, 0, 1, 2, 3, 3, 3, 4]) + J = array([0, 3, 1, 2, 1, 3, 4, 4]) + V = array([1.0, 6.0, 10.5, 0.015, 250.5, -280.0, 33.32, 12.0]) + + b = scipy.sparse.coo_array((V, (I, J)), shape=(5, 5)) + + mmwrite(self.fn, b) + + fn_bzip2 = f"{self.fn}.bz2" + with open(self.fn, 'rb') as f_in: + f_out = bz2.BZ2File(fn_bzip2, 'wb') + f_out.write(f_in.read()) + f_out.close() + + a = mmread(fn_bzip2, spmatrix=False).toarray() + assert_array_almost_equal(a, b.toarray()) + + def test_gzip_py3(self): + # test if fix for #2152 works + try: + # gzip module can be missing from Python installation + import gzip + except ImportError: + return + I = array([0, 0, 1, 2, 3, 3, 3, 4]) + J = array([0, 3, 1, 2, 1, 3, 4, 4]) + V = array([1.0, 6.0, 10.5, 0.015, 250.5, -280.0, 33.32, 12.0]) + + b = scipy.sparse.coo_array((V, (I, J)), shape=(5, 5)) + + mmwrite(self.fn, b) + + fn_gzip = f"{self.fn}.gz" + with open(self.fn, 'rb') as f_in: + f_out = gzip.open(fn_gzip, 'wb') + f_out.write(f_in.read()) + f_out.close() + + a = mmread(fn_gzip, spmatrix=False).toarray() + assert_array_almost_equal(a, b.toarray()) + + def test_real_write_read(self): + I = array([0, 0, 1, 2, 3, 3, 3, 4]) + J = array([0, 3, 1, 2, 1, 3, 4, 4]) + V = array([1.0, 6.0, 10.5, 0.015, 250.5, -280.0, 33.32, 12.0]) + + b = scipy.sparse.coo_array((V, (I, J)), shape=(5, 5)) + + mmwrite(self.fn, b) + + assert_equal(mminfo(self.fn), + (5, 5, 8, 'coordinate', 'real', 'general')) + a = b.toarray() + b = mmread(self.fn, spmatrix=False).toarray() + assert_array_almost_equal(a, b) + + def test_complex_write_read(self): + I = array([0, 0, 1, 2, 3, 3, 3, 4]) + J = array([0, 3, 1, 2, 1, 3, 4, 4]) + V = array([1.0 + 3j, 6.0 + 2j, 10.50 + 0.9j, 0.015 + -4.4j, + 250.5 + 0j, -280.0 + 5j, 33.32 + 6.4j, 12.00 + 0.8j]) + + b = scipy.sparse.coo_array((V, (I, J)), shape=(5, 5)) + + mmwrite(self.fn, b) + + assert_equal(mminfo(self.fn), + (5, 5, 8, 'coordinate', 'complex', 'general')) + a = b.toarray() + b = mmread(self.fn, spmatrix=False).toarray() + assert_array_almost_equal(a, b) + + def test_sparse_formats(self, tmp_path): + # Note: `tmp_path` is a pytest fixture, it handles cleanup + tmpdir = tmp_path / 'sparse_formats' + tmpdir.mkdir() + + mats = [] + I = array([0, 0, 1, 2, 3, 3, 3, 4]) + J = array([0, 3, 1, 2, 1, 3, 4, 4]) + + V = array([1.0, 6.0, 10.5, 0.015, 250.5, -280.0, 33.32, 12.0]) + mats.append(scipy.sparse.coo_array((V, (I, J)), shape=(5, 5))) + + V = array([1.0 + 3j, 6.0 + 2j, 10.50 + 0.9j, 0.015 + -4.4j, + 250.5 + 0j, -280.0 + 5j, 33.32 + 6.4j, 12.00 + 0.8j]) + mats.append(scipy.sparse.coo_array((V, (I, J)), shape=(5, 5))) + + for mat in mats: + expected = mat.toarray() + for fmt in ['csr', 'csc', 'coo']: + fname = tmpdir / (fmt + '.mtx') + mmwrite(fname, mat.asformat(fmt)) + result = mmread(fname, spmatrix=False).toarray() + assert_array_almost_equal(result, expected) + + def test_precision(self): + test_values = [pi] + [10**(i) for i in range(0, -10, -1)] + test_precisions = range(1, 10) + for value in test_values: + for precision in test_precisions: + # construct sparse matrix with test value at last main diagonal + n = 10**precision + 1 + A = scipy.sparse.dok_array((n, n)) + A[n-1, n-1] = value + # write matrix with test precision and read again + mmwrite(self.fn, A, precision=precision) + A = scipy.io.mmread(self.fn, spmatrix=False) + # check for right entries in matrix + assert_array_equal(A.row, [n-1]) + assert_array_equal(A.col, [n-1]) + assert_allclose(A.data, [float('%%.%dg' % precision % value)]) + + def test_bad_number_of_coordinate_header_fields(self): + s = """\ + %%MatrixMarket matrix coordinate real general + 5 5 8 999 + 1 1 1.000e+00 + 2 2 1.050e+01 + 3 3 1.500e-02 + 1 4 6.000e+00 + 4 2 2.505e+02 + 4 4 -2.800e+02 + 4 5 3.332e+01 + 5 5 1.200e+01 + """ + text = textwrap.dedent(s).encode('ascii') + with pytest.raises(ValueError, match='not of length 3'): + scipy.io.mmread(io.BytesIO(text)) + + +def test_gh11389(): + mmread(io.StringIO("%%MatrixMarket matrix coordinate complex symmetric\n" + " 1 1 1\n" + "1 1 -2.1846000000000e+02 0.0000000000000e+00"), + spmatrix=False) + + +def test_gh18123(tmp_path): + lines = [" %%MatrixMarket matrix coordinate real general\n", + "5 5 3\n", + "2 3 1.0\n", + "3 4 2.0\n", + "3 5 3.0\n"] + test_file = tmp_path / "test.mtx" + with open(test_file, "w") as f: + f.writelines(lines) + mmread(test_file, spmatrix=False) + +def test_mtx_append(tmp_path): + a = mmread(io.StringIO("%%MatrixMarket matrix coordinate complex symmetric\n" + " 1 1 1\n" + "1 1 -2.1846000000000e+02 0.0000000000000e+00"), + spmatrix=False) + test_writefile = tmp_path / "test_mtx" + test_readfile = tmp_path / "test_mtx.mtx" + mmwrite(test_writefile, a) + mmread(test_readfile, spmatrix=False) + + +def test_threadpoolctl(): + try: + import threadpoolctl + if not hasattr(threadpoolctl, "register"): + pytest.skip("threadpoolctl too old") + return + except ImportError: + pytest.skip("no threadpoolctl") + return + + with threadpoolctl.threadpool_limits(limits=4): + assert_equal(fmm.PARALLELISM, 4) + + with threadpoolctl.threadpool_limits(limits=2, user_api='scipy'): + assert_equal(fmm.PARALLELISM, 2) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/test_netcdf.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/test_netcdf.py new file mode 100644 index 0000000000000000000000000000000000000000..161406076d0b5078e8e11aa5762b7715cd83c4a7 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/test_netcdf.py @@ -0,0 +1,550 @@ +''' Tests for netcdf ''' +import os +from os.path import join as pjoin, dirname +import shutil +import tempfile +import warnings +from io import BytesIO +from glob import glob +from contextlib import contextmanager + +import numpy as np +from numpy.testing import (assert_, assert_allclose, assert_equal, + break_cycles, suppress_warnings, IS_PYPY) +import pytest +from pytest import raises as assert_raises + +from scipy.io import netcdf_file +from scipy._lib._tmpdirs import in_tempdir + +TEST_DATA_PATH = pjoin(dirname(__file__), 'data') + +N_EG_ELS = 11 # number of elements for example variable +VARTYPE_EG = 'b' # var type for example variable + + +pytestmark = pytest.mark.thread_unsafe + + +@contextmanager +def make_simple(*args, **kwargs): + f = netcdf_file(*args, **kwargs) + f.history = 'Created for a test' + f.createDimension('time', N_EG_ELS) + time = f.createVariable('time', VARTYPE_EG, ('time',)) + time[:] = np.arange(N_EG_ELS) + time.units = 'days since 2008-01-01' + f.flush() + yield f + f.close() + + +def check_simple(ncfileobj): + '''Example fileobj tests ''' + assert_equal(ncfileobj.history, b'Created for a test') + time = ncfileobj.variables['time'] + assert_equal(time.units, b'days since 2008-01-01') + assert_equal(time.shape, (N_EG_ELS,)) + assert_equal(time[-1], N_EG_ELS-1) + +def assert_mask_matches(arr, expected_mask): + ''' + Asserts that the mask of arr is effectively the same as expected_mask. + + In contrast to numpy.ma.testutils.assert_mask_equal, this function allows + testing the 'mask' of a standard numpy array (the mask in this case is treated + as all False). + + Parameters + ---------- + arr : ndarray or MaskedArray + Array to test. + expected_mask : array_like of booleans + A list giving the expected mask. + ''' + + mask = np.ma.getmaskarray(arr) + assert_equal(mask, expected_mask) + + +def test_read_write_files(): + # test round trip for example file + cwd = os.getcwd() + try: + tmpdir = tempfile.mkdtemp() + os.chdir(tmpdir) + with make_simple('simple.nc', 'w') as f: + pass + # read the file we just created in 'a' mode + with netcdf_file('simple.nc', 'a') as f: + check_simple(f) + # add something + f._attributes['appendRan'] = 1 + + # To read the NetCDF file we just created:: + with netcdf_file('simple.nc') as f: + # Using mmap is the default (but not on pypy) + assert_equal(f.use_mmap, not IS_PYPY) + check_simple(f) + assert_equal(f._attributes['appendRan'], 1) + + # Read it in append (and check mmap is off) + with netcdf_file('simple.nc', 'a') as f: + assert_(not f.use_mmap) + check_simple(f) + assert_equal(f._attributes['appendRan'], 1) + + # Now without mmap + with netcdf_file('simple.nc', mmap=False) as f: + # Using mmap is the default + assert_(not f.use_mmap) + check_simple(f) + + # To read the NetCDF file we just created, as file object, no + # mmap. When n * n_bytes(var_type) is not divisible by 4, this + # raised an error in pupynere 1.0.12 and scipy rev 5893, because + # calculated vsize was rounding up in units of 4 - see + # https://www.unidata.ucar.edu/software/netcdf/guide_toc.html + with open('simple.nc', 'rb') as fobj: + with netcdf_file(fobj) as f: + # by default, don't use mmap for file-like + assert_(not f.use_mmap) + check_simple(f) + + # Read file from fileobj, with mmap + with suppress_warnings() as sup: + if IS_PYPY: + sup.filter(RuntimeWarning, + "Cannot close a netcdf_file opened with mmap=True.*") + with open('simple.nc', 'rb') as fobj: + with netcdf_file(fobj, mmap=True) as f: + assert_(f.use_mmap) + check_simple(f) + + # Again read it in append mode (adding another att) + with open('simple.nc', 'r+b') as fobj: + with netcdf_file(fobj, 'a') as f: + assert_(not f.use_mmap) + check_simple(f) + f.createDimension('app_dim', 1) + var = f.createVariable('app_var', 'i', ('app_dim',)) + var[:] = 42 + + # And... check that app_var made it in... + with netcdf_file('simple.nc') as f: + check_simple(f) + assert_equal(f.variables['app_var'][:], 42) + + finally: + if IS_PYPY: + # windows cannot remove a dead file held by a mmap + # that has not been collected in PyPy + break_cycles() + break_cycles() + os.chdir(cwd) + shutil.rmtree(tmpdir) + + +def test_read_write_sio(): + eg_sio1 = BytesIO() + with make_simple(eg_sio1, 'w'): + str_val = eg_sio1.getvalue() + + eg_sio2 = BytesIO(str_val) + with netcdf_file(eg_sio2) as f2: + check_simple(f2) + + # Test that error is raised if attempting mmap for sio + eg_sio3 = BytesIO(str_val) + assert_raises(ValueError, netcdf_file, eg_sio3, 'r', True) + # Test 64-bit offset write / read + eg_sio_64 = BytesIO() + with make_simple(eg_sio_64, 'w', version=2) as f_64: + str_val = eg_sio_64.getvalue() + + eg_sio_64 = BytesIO(str_val) + with netcdf_file(eg_sio_64) as f_64: + check_simple(f_64) + assert_equal(f_64.version_byte, 2) + # also when version 2 explicitly specified + eg_sio_64 = BytesIO(str_val) + with netcdf_file(eg_sio_64, version=2) as f_64: + check_simple(f_64) + assert_equal(f_64.version_byte, 2) + + +def test_bytes(): + raw_file = BytesIO() + f = netcdf_file(raw_file, mode='w') + # Dataset only has a single variable, dimension and attribute to avoid + # any ambiguity related to order. + f.a = 'b' + f.createDimension('dim', 1) + var = f.createVariable('var', np.int16, ('dim',)) + var[0] = -9999 + var.c = 'd' + f.sync() + + actual = raw_file.getvalue() + + expected = (b'CDF\x01' + b'\x00\x00\x00\x00' + b'\x00\x00\x00\x0a' + b'\x00\x00\x00\x01' + b'\x00\x00\x00\x03' + b'dim\x00' + b'\x00\x00\x00\x01' + b'\x00\x00\x00\x0c' + b'\x00\x00\x00\x01' + b'\x00\x00\x00\x01' + b'a\x00\x00\x00' + b'\x00\x00\x00\x02' + b'\x00\x00\x00\x01' + b'b\x00\x00\x00' + b'\x00\x00\x00\x0b' + b'\x00\x00\x00\x01' + b'\x00\x00\x00\x03' + b'var\x00' + b'\x00\x00\x00\x01' + b'\x00\x00\x00\x00' + b'\x00\x00\x00\x0c' + b'\x00\x00\x00\x01' + b'\x00\x00\x00\x01' + b'c\x00\x00\x00' + b'\x00\x00\x00\x02' + b'\x00\x00\x00\x01' + b'd\x00\x00\x00' + b'\x00\x00\x00\x03' + b'\x00\x00\x00\x04' + b'\x00\x00\x00\x78' + b'\xd8\xf1\x80\x01') + + assert_equal(actual, expected) + + +def test_encoded_fill_value(): + with netcdf_file(BytesIO(), mode='w') as f: + f.createDimension('x', 1) + var = f.createVariable('var', 'S1', ('x',)) + assert_equal(var._get_encoded_fill_value(), b'\x00') + var._FillValue = b'\x01' + assert_equal(var._get_encoded_fill_value(), b'\x01') + var._FillValue = b'\x00\x00' # invalid, wrong size + assert_equal(var._get_encoded_fill_value(), b'\x00') + + +def test_read_example_data(): + # read any example data files + for fname in glob(pjoin(TEST_DATA_PATH, '*.nc')): + with netcdf_file(fname, 'r'): + pass + with netcdf_file(fname, 'r', mmap=False): + pass + + +def test_itemset_no_segfault_on_readonly(): + # Regression test for ticket #1202. + # Open the test file in read-only mode. + + filename = pjoin(TEST_DATA_PATH, 'example_1.nc') + with suppress_warnings() as sup: + message = ("Cannot close a netcdf_file opened with mmap=True, when " + "netcdf_variables or arrays referring to its data still exist") + sup.filter(RuntimeWarning, message) + with netcdf_file(filename, 'r', mmap=True) as f: + time_var = f.variables['time'] + + # time_var.assignValue(42) should raise a RuntimeError--not seg. fault! + assert_raises(RuntimeError, time_var.assignValue, 42) + + +def test_appending_issue_gh_8625(): + stream = BytesIO() + + with make_simple(stream, mode='w') as f: + f.createDimension('x', 2) + f.createVariable('x', float, ('x',)) + f.variables['x'][...] = 1 + f.flush() + contents = stream.getvalue() + + stream = BytesIO(contents) + with netcdf_file(stream, mode='a') as f: + f.variables['x'][...] = 2 + + +def test_write_invalid_dtype(): + dtypes = ['int64', 'uint64'] + if np.dtype('int').itemsize == 8: # 64-bit machines + dtypes.append('int') + if np.dtype('uint').itemsize == 8: # 64-bit machines + dtypes.append('uint') + + with netcdf_file(BytesIO(), 'w') as f: + f.createDimension('time', N_EG_ELS) + for dt in dtypes: + assert_raises(ValueError, f.createVariable, 'time', dt, ('time',)) + + +def test_flush_rewind(): + stream = BytesIO() + with make_simple(stream, mode='w') as f: + f.createDimension('x',4) # x is used in createVariable + v = f.createVariable('v', 'i2', ['x']) + v[:] = 1 + f.flush() + len_single = len(stream.getvalue()) + f.flush() + len_double = len(stream.getvalue()) + + assert_(len_single == len_double) + + +def test_dtype_specifiers(): + # Numpy 1.7.0-dev had a bug where 'i2' wouldn't work. + # Specifying np.int16 or similar only works from the same commit as this + # comment was made. + with make_simple(BytesIO(), mode='w') as f: + f.createDimension('x',4) + f.createVariable('v1', 'i2', ['x']) + f.createVariable('v2', np.int16, ['x']) + f.createVariable('v3', np.dtype(np.int16), ['x']) + + +def test_ticket_1720(): + io = BytesIO() + + items = [0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9] + + with netcdf_file(io, 'w') as f: + f.history = 'Created for a test' + f.createDimension('float_var', 10) + float_var = f.createVariable('float_var', 'f', ('float_var',)) + float_var[:] = items + float_var.units = 'metres' + f.flush() + contents = io.getvalue() + + io = BytesIO(contents) + with netcdf_file(io, 'r') as f: + assert_equal(f.history, b'Created for a test') + float_var = f.variables['float_var'] + assert_equal(float_var.units, b'metres') + assert_equal(float_var.shape, (10,)) + assert_allclose(float_var[:], items) + + +def test_mmaps_segfault(): + filename = pjoin(TEST_DATA_PATH, 'example_1.nc') + + if not IS_PYPY: + with warnings.catch_warnings(): + warnings.simplefilter("error") + with netcdf_file(filename, mmap=True) as f: + x = f.variables['lat'][:] + # should not raise warnings + del x + + def doit(): + with netcdf_file(filename, mmap=True) as f: + return f.variables['lat'][:] + + # should not crash + with suppress_warnings() as sup: + message = ("Cannot close a netcdf_file opened with mmap=True, when " + "netcdf_variables or arrays referring to its data still exist") + sup.filter(RuntimeWarning, message) + x = doit() + x.sum() + + +def test_zero_dimensional_var(): + io = BytesIO() + with make_simple(io, 'w') as f: + v = f.createVariable('zerodim', 'i2', []) + # This is checking that .isrec returns a boolean - don't simplify it + # to 'assert not ...' + assert v.isrec is False, v.isrec + f.flush() + + +def test_byte_gatts(): + # Check that global "string" atts work like they did before py3k + # unicode and general bytes confusion + with in_tempdir(): + filename = 'g_byte_atts.nc' + f = netcdf_file(filename, 'w') + f._attributes['holy'] = b'grail' + f._attributes['witch'] = 'floats' + f.close() + f = netcdf_file(filename, 'r') + assert_equal(f._attributes['holy'], b'grail') + assert_equal(f._attributes['witch'], b'floats') + f.close() + + +def test_open_append(): + # open 'w' put one attr + with in_tempdir(): + filename = 'append_dat.nc' + f = netcdf_file(filename, 'w') + f._attributes['Kilroy'] = 'was here' + f.close() + + # open again in 'a', read the att and a new one + f = netcdf_file(filename, 'a') + assert_equal(f._attributes['Kilroy'], b'was here') + f._attributes['naughty'] = b'Zoot' + f.close() + + # open yet again in 'r' and check both atts + f = netcdf_file(filename, 'r') + assert_equal(f._attributes['Kilroy'], b'was here') + assert_equal(f._attributes['naughty'], b'Zoot') + f.close() + + +def test_append_recordDimension(): + dataSize = 100 + + with in_tempdir(): + # Create file with record time dimension + with netcdf_file('withRecordDimension.nc', 'w') as f: + f.createDimension('time', None) + f.createVariable('time', 'd', ('time',)) + f.createDimension('x', dataSize) + x = f.createVariable('x', 'd', ('x',)) + x[:] = np.array(range(dataSize)) + f.createDimension('y', dataSize) + y = f.createVariable('y', 'd', ('y',)) + y[:] = np.array(range(dataSize)) + f.createVariable('testData', 'i', ('time', 'x', 'y')) + f.flush() + f.close() + + for i in range(2): + # Open the file in append mode and add data + with netcdf_file('withRecordDimension.nc', 'a') as f: + f.variables['time'].data = np.append(f.variables["time"].data, i) + f.variables['testData'][i, :, :] = np.full((dataSize, dataSize), i) + f.flush() + + # Read the file and check that append worked + with netcdf_file('withRecordDimension.nc') as f: + assert_equal(f.variables['time'][-1], i) + assert_equal(f.variables['testData'][-1, :, :].copy(), + np.full((dataSize, dataSize), i)) + assert_equal(f.variables['time'].data.shape[0], i+1) + assert_equal(f.variables['testData'].data.shape[0], i+1) + + # Read the file and check that 'data' was not saved as user defined + # attribute of testData variable during append operation + with netcdf_file('withRecordDimension.nc') as f: + with assert_raises(KeyError) as ar: + f.variables['testData']._attributes['data'] + ex = ar.value + assert_equal(ex.args[0], 'data') + +def test_maskandscale(): + t = np.linspace(20, 30, 15) + t[3] = 100 + tm = np.ma.masked_greater(t, 99) + fname = pjoin(TEST_DATA_PATH, 'example_2.nc') + with netcdf_file(fname, maskandscale=True) as f: + Temp = f.variables['Temperature'] + assert_equal(Temp.missing_value, 9999) + assert_equal(Temp.add_offset, 20) + assert_equal(Temp.scale_factor, np.float32(0.01)) + found = Temp[:].compressed() + del Temp # Remove ref to mmap, so file can be closed. + expected = np.round(tm.compressed(), 2) + assert_allclose(found, expected) + + with in_tempdir(): + newfname = 'ms.nc' + f = netcdf_file(newfname, 'w', maskandscale=True) + f.createDimension('Temperature', len(tm)) + temp = f.createVariable('Temperature', 'i', ('Temperature',)) + temp.missing_value = 9999 + temp.scale_factor = 0.01 + temp.add_offset = 20 + temp[:] = tm + f.close() + + with netcdf_file(newfname, maskandscale=True) as f: + Temp = f.variables['Temperature'] + assert_equal(Temp.missing_value, 9999) + assert_equal(Temp.add_offset, 20) + assert_equal(Temp.scale_factor, np.float32(0.01)) + expected = np.round(tm.compressed(), 2) + found = Temp[:].compressed() + del Temp + assert_allclose(found, expected) + + +# ------------------------------------------------------------------------ +# Test reading with masked values (_FillValue / missing_value) +# ------------------------------------------------------------------------ + +def test_read_withValuesNearFillValue(): + # Regression test for ticket #5626 + fname = pjoin(TEST_DATA_PATH, 'example_3_maskedvals.nc') + with netcdf_file(fname, maskandscale=True) as f: + vardata = f.variables['var1_fillval0'][:] + assert_mask_matches(vardata, [False, True, False]) + +def test_read_withNoFillValue(): + # For a variable with no fill value, reading data with maskandscale=True + # should return unmasked data + fname = pjoin(TEST_DATA_PATH, 'example_3_maskedvals.nc') + with netcdf_file(fname, maskandscale=True) as f: + vardata = f.variables['var2_noFillval'][:] + assert_mask_matches(vardata, [False, False, False]) + assert_equal(vardata, [1,2,3]) + +def test_read_withFillValueAndMissingValue(): + # For a variable with both _FillValue and missing_value, the _FillValue + # should be used + IRRELEVANT_VALUE = 9999 + fname = pjoin(TEST_DATA_PATH, 'example_3_maskedvals.nc') + with netcdf_file(fname, maskandscale=True) as f: + vardata = f.variables['var3_fillvalAndMissingValue'][:] + assert_mask_matches(vardata, [True, False, False]) + assert_equal(vardata, [IRRELEVANT_VALUE, 2, 3]) + +def test_read_withMissingValue(): + # For a variable with missing_value but not _FillValue, the missing_value + # should be used + fname = pjoin(TEST_DATA_PATH, 'example_3_maskedvals.nc') + with netcdf_file(fname, maskandscale=True) as f: + vardata = f.variables['var4_missingValue'][:] + assert_mask_matches(vardata, [False, True, False]) + +def test_read_withFillValNaN(): + fname = pjoin(TEST_DATA_PATH, 'example_3_maskedvals.nc') + with netcdf_file(fname, maskandscale=True) as f: + vardata = f.variables['var5_fillvalNaN'][:] + assert_mask_matches(vardata, [False, True, False]) + +def test_read_withChar(): + fname = pjoin(TEST_DATA_PATH, 'example_3_maskedvals.nc') + with netcdf_file(fname, maskandscale=True) as f: + vardata = f.variables['var6_char'][:] + assert_mask_matches(vardata, [False, True, False]) + +def test_read_with2dVar(): + fname = pjoin(TEST_DATA_PATH, 'example_3_maskedvals.nc') + with netcdf_file(fname, maskandscale=True) as f: + vardata = f.variables['var7_2d'][:] + assert_mask_matches(vardata, [[True, False], [False, False], [False, True]]) + +def test_read_withMaskAndScaleFalse(): + # If a variable has a _FillValue (or missing_value) attribute, but is read + # with maskandscale set to False, the result should be unmasked + fname = pjoin(TEST_DATA_PATH, 'example_3_maskedvals.nc') + # Open file with mmap=False to avoid problems with closing a mmap'ed file + # when arrays referring to its data still exist: + with netcdf_file(fname, maskandscale=False, mmap=False) as f: + vardata = f.variables['var3_fillvalAndMissingValue'][:] + assert_mask_matches(vardata, [False, False, False]) + assert_equal(vardata, [1, 2, 3]) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/test_paths.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/test_paths.py new file mode 100644 index 0000000000000000000000000000000000000000..1e7c4167ace335fb5fc86f6499ee54c3360ded6e --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/test_paths.py @@ -0,0 +1,93 @@ +""" +Ensure that we can use pathlib.Path objects in all relevant IO functions. +""" +from pathlib import Path + +import numpy as np + +import scipy.io +import scipy.io.wavfile +from scipy._lib._tmpdirs import tempdir +import scipy.sparse + + +class TestPaths: + data = np.arange(5).astype(np.int64) + + def test_savemat(self): + with tempdir() as temp_dir: + path = Path(temp_dir) / 'data.mat' + scipy.io.savemat(path, {'data': self.data}) + assert path.is_file() + + def test_loadmat(self): + # Save data with string path, load with pathlib.Path + with tempdir() as temp_dir: + path = Path(temp_dir) / 'data.mat' + scipy.io.savemat(str(path), {'data': self.data}) + + mat_contents = scipy.io.loadmat(path) + assert (mat_contents['data'] == self.data).all() + + def test_whosmat(self): + # Save data with string path, load with pathlib.Path + with tempdir() as temp_dir: + path = Path(temp_dir) / 'data.mat' + scipy.io.savemat(str(path), {'data': self.data}) + + contents = scipy.io.whosmat(path) + assert contents[0] == ('data', (1, 5), 'int64') + + def test_readsav(self): + path = Path(__file__).parent / 'data/scalar_string.sav' + scipy.io.readsav(path) + + def test_hb_read(self): + # Save data with string path, load with pathlib.Path + with tempdir() as temp_dir: + data = scipy.sparse.eye_array(3, format='csr') + path = Path(temp_dir) / 'data.hb' + scipy.io.hb_write(str(path), data) + + data_new = scipy.io.hb_read(path, spmatrix=False) + assert (data_new != data).nnz == 0 + + def test_hb_write(self): + with tempdir() as temp_dir: + data = scipy.sparse.eye_array(3, format='csr') + path = Path(temp_dir) / 'data.hb' + scipy.io.hb_write(path, data) + assert path.is_file() + + def test_mmio_read(self): + # Save data with string path, load with pathlib.Path + with tempdir() as temp_dir: + data = scipy.sparse.eye_array(3, format='csr') + path = Path(temp_dir) / 'data.mtx' + scipy.io.mmwrite(str(path), data) + + data_new = scipy.io.mmread(path, spmatrix=False) + assert (data_new != data).nnz == 0 + + def test_mmio_write(self): + with tempdir() as temp_dir: + data = scipy.sparse.eye_array(3, format='csr') + path = Path(temp_dir) / 'data.mtx' + scipy.io.mmwrite(path, data) + + def test_netcdf_file(self): + path = Path(__file__).parent / 'data/example_1.nc' + scipy.io.netcdf_file(path) + + def test_wavfile_read(self): + path = Path(__file__).parent / 'data/test-8000Hz-le-2ch-1byteu.wav' + scipy.io.wavfile.read(path) + + def test_wavfile_write(self): + # Read from str path, write to Path + input_path = Path(__file__).parent / 'data/test-8000Hz-le-2ch-1byteu.wav' + rate, data = scipy.io.wavfile.read(str(input_path)) + + with tempdir() as temp_dir: + output_path = Path(temp_dir) / input_path.name + scipy.io.wavfile.write(output_path, rate, data) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/test_wavfile.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/test_wavfile.py new file mode 100644 index 0000000000000000000000000000000000000000..8e0a545495a842916a3cddd48c0b1b3859ae4ca5 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/tests/test_wavfile.py @@ -0,0 +1,460 @@ +import os +import sys +from io import BytesIO +import threading + +import numpy as np +from numpy.testing import (assert_equal, assert_, assert_array_equal, + break_cycles, suppress_warnings, IS_PYPY) +import pytest +from pytest import raises, warns + +from scipy.io import wavfile + + +def datafile(fn): + return os.path.join(os.path.dirname(__file__), 'data', fn) + + +def test_read_1(): + # 32-bit PCM (which uses extensible format) + for mmap in [False, True]: + filename = 'test-44100Hz-le-1ch-4bytes.wav' + rate, data = wavfile.read(datafile(filename), mmap=mmap) + + assert_equal(rate, 44100) + assert_(np.issubdtype(data.dtype, np.int32)) + assert_equal(data.shape, (4410,)) + + del data + + +def test_read_2(): + # 8-bit unsigned PCM + for mmap in [False, True]: + filename = 'test-8000Hz-le-2ch-1byteu.wav' + rate, data = wavfile.read(datafile(filename), mmap=mmap) + + assert_equal(rate, 8000) + assert_(np.issubdtype(data.dtype, np.uint8)) + assert_equal(data.shape, (800, 2)) + + del data + + +def test_read_3(): + # Little-endian float + for mmap in [False, True]: + filename = 'test-44100Hz-2ch-32bit-float-le.wav' + rate, data = wavfile.read(datafile(filename), mmap=mmap) + + assert_equal(rate, 44100) + assert_(np.issubdtype(data.dtype, np.float32)) + assert_equal(data.shape, (441, 2)) + + del data + + +def test_read_4(): + # Contains unsupported 'PEAK' chunk + for mmap in [False, True]: + with suppress_warnings() as sup: + sup.filter(wavfile.WavFileWarning, + "Chunk .non-data. not understood, skipping it") + filename = 'test-48000Hz-2ch-64bit-float-le-wavex.wav' + rate, data = wavfile.read(datafile(filename), mmap=mmap) + + assert_equal(rate, 48000) + assert_(np.issubdtype(data.dtype, np.float64)) + assert_equal(data.shape, (480, 2)) + + del data + + +def test_read_5(): + # Big-endian float + for mmap in [False, True]: + filename = 'test-44100Hz-2ch-32bit-float-be.wav' + rate, data = wavfile.read(datafile(filename), mmap=mmap) + + assert_equal(rate, 44100) + assert_(np.issubdtype(data.dtype, np.float32)) + assert_(data.dtype.byteorder == '>' or (sys.byteorder == 'big' and + data.dtype.byteorder == '=')) + assert_equal(data.shape, (441, 2)) + + del data + + +def test_5_bit_odd_size_no_pad(): + # 5-bit, 1 B container, 5 channels, 9 samples, 45 B data chunk + # Generated by LTspice, which incorrectly omits pad byte, but should be + # readable anyway + for mmap in [False, True]: + filename = 'test-8000Hz-le-5ch-9S-5bit.wav' + rate, data = wavfile.read(datafile(filename), mmap=mmap) + + assert_equal(rate, 8000) + assert_(np.issubdtype(data.dtype, np.uint8)) + assert_equal(data.shape, (9, 5)) + + # 8-5 = 3 LSBits should be 0 + assert_equal(data & 0b00000111, 0) + + # Unsigned + assert_equal(data.max(), 0b11111000) # Highest possible + assert_equal(data[0, 0], 128) # Midpoint is 128 for <= 8-bit + assert_equal(data.min(), 0) # Lowest possible + + del data + + +def test_12_bit_even_size(): + # 12-bit, 2 B container, 4 channels, 9 samples, 72 B data chunk + # Generated by LTspice from 1 Vpk sine waves + for mmap in [False, True]: + filename = 'test-8000Hz-le-4ch-9S-12bit.wav' + rate, data = wavfile.read(datafile(filename), mmap=mmap) + + assert_equal(rate, 8000) + assert_(np.issubdtype(data.dtype, np.int16)) + assert_equal(data.shape, (9, 4)) + + # 16-12 = 4 LSBits should be 0 + assert_equal(data & 0b00000000_00001111, 0) + + # Signed + assert_equal(data.max(), 0b01111111_11110000) # Highest possible + assert_equal(data[0, 0], 0) # Midpoint is 0 for >= 9-bit + assert_equal(data.min(), -0b10000000_00000000) # Lowest possible + + del data + + +def test_24_bit_odd_size_with_pad(): + # 24-bit, 3 B container, 3 channels, 5 samples, 45 B data chunk + # Should not raise any warnings about the data chunk pad byte + filename = 'test-8000Hz-le-3ch-5S-24bit.wav' + rate, data = wavfile.read(datafile(filename), mmap=False) + + assert_equal(rate, 8000) + assert_(np.issubdtype(data.dtype, np.int32)) + assert_equal(data.shape, (5, 3)) + + # All LSBytes should be 0 + assert_equal(data & 0xff, 0) + + # Hand-made max/min samples under different conventions: + # 2**(N-1) 2**(N-1)-1 LSB + assert_equal(data, [[-0x8000_0000, -0x7fff_ff00, -0x200], + [-0x4000_0000, -0x3fff_ff00, -0x100], + [+0x0000_0000, +0x0000_0000, +0x000], + [+0x4000_0000, +0x3fff_ff00, +0x100], + [+0x7fff_ff00, +0x7fff_ff00, +0x200]]) + # ^ clipped + + +def test_20_bit_extra_data(): + # 20-bit, 3 B container, 1 channel, 10 samples, 30 B data chunk + # with extra data filling container beyond the bit depth + filename = 'test-1234Hz-le-1ch-10S-20bit-extra.wav' + rate, data = wavfile.read(datafile(filename), mmap=False) + + assert_equal(rate, 1234) + assert_(np.issubdtype(data.dtype, np.int32)) + assert_equal(data.shape, (10,)) + + # All LSBytes should still be 0, because 3 B container in 4 B dtype + assert_equal(data & 0xff, 0) + + # But it should load the data beyond 20 bits + assert_((data & 0xf00).any()) + + # Full-scale positive/negative samples, then being halved each time + assert_equal(data, [+0x7ffff000, # +full-scale 20-bit + -0x7ffff000, # -full-scale 20-bit + +0x7ffff000 >> 1, # +1/2 + -0x7ffff000 >> 1, # -1/2 + +0x7ffff000 >> 2, # +1/4 + -0x7ffff000 >> 2, # -1/4 + +0x7ffff000 >> 3, # +1/8 + -0x7ffff000 >> 3, # -1/8 + +0x7ffff000 >> 4, # +1/16 + -0x7ffff000 >> 4, # -1/16 + ]) + + +def test_36_bit_odd_size(): + # 36-bit, 5 B container, 3 channels, 5 samples, 75 B data chunk + pad + filename = 'test-8000Hz-le-3ch-5S-36bit.wav' + rate, data = wavfile.read(datafile(filename), mmap=False) + + assert_equal(rate, 8000) + assert_(np.issubdtype(data.dtype, np.int64)) + assert_equal(data.shape, (5, 3)) + + # 28 LSBits should be 0 + assert_equal(data & 0xfffffff, 0) + + # Hand-made max/min samples under different conventions: + # Fixed-point 2**(N-1) Full-scale 2**(N-1)-1 LSB + correct = [[-0x8000_0000_0000_0000, -0x7fff_ffff_f000_0000, -0x2000_0000], + [-0x4000_0000_0000_0000, -0x3fff_ffff_f000_0000, -0x1000_0000], + [+0x0000_0000_0000_0000, +0x0000_0000_0000_0000, +0x0000_0000], + [+0x4000_0000_0000_0000, +0x3fff_ffff_f000_0000, +0x1000_0000], + [+0x7fff_ffff_f000_0000, +0x7fff_ffff_f000_0000, +0x2000_0000]] + # ^ clipped + + assert_equal(data, correct) + + +def test_45_bit_even_size(): + # 45-bit, 6 B container, 3 channels, 5 samples, 90 B data chunk + filename = 'test-8000Hz-le-3ch-5S-45bit.wav' + rate, data = wavfile.read(datafile(filename), mmap=False) + + assert_equal(rate, 8000) + assert_(np.issubdtype(data.dtype, np.int64)) + assert_equal(data.shape, (5, 3)) + + # 19 LSBits should be 0 + assert_equal(data & 0x7ffff, 0) + + # Hand-made max/min samples under different conventions: + # Fixed-point 2**(N-1) Full-scale 2**(N-1)-1 LSB + correct = [[-0x8000_0000_0000_0000, -0x7fff_ffff_fff8_0000, -0x10_0000], + [-0x4000_0000_0000_0000, -0x3fff_ffff_fff8_0000, -0x08_0000], + [+0x0000_0000_0000_0000, +0x0000_0000_0000_0000, +0x00_0000], + [+0x4000_0000_0000_0000, +0x3fff_ffff_fff8_0000, +0x08_0000], + [+0x7fff_ffff_fff8_0000, +0x7fff_ffff_fff8_0000, +0x10_0000]] + # ^ clipped + + assert_equal(data, correct) + + +def test_53_bit_odd_size(): + # 53-bit, 7 B container, 3 channels, 5 samples, 105 B data chunk + pad + filename = 'test-8000Hz-le-3ch-5S-53bit.wav' + rate, data = wavfile.read(datafile(filename), mmap=False) + + assert_equal(rate, 8000) + assert_(np.issubdtype(data.dtype, np.int64)) + assert_equal(data.shape, (5, 3)) + + # 11 LSBits should be 0 + assert_equal(data & 0x7ff, 0) + + # Hand-made max/min samples under different conventions: + # Fixed-point 2**(N-1) Full-scale 2**(N-1)-1 LSB + correct = [[-0x8000_0000_0000_0000, -0x7fff_ffff_ffff_f800, -0x1000], + [-0x4000_0000_0000_0000, -0x3fff_ffff_ffff_f800, -0x0800], + [+0x0000_0000_0000_0000, +0x0000_0000_0000_0000, +0x0000], + [+0x4000_0000_0000_0000, +0x3fff_ffff_ffff_f800, +0x0800], + [+0x7fff_ffff_ffff_f800, +0x7fff_ffff_ffff_f800, +0x1000]] + # ^ clipped + + assert_equal(data, correct) + + +def test_64_bit_even_size(): + # 64-bit, 8 B container, 3 channels, 5 samples, 120 B data chunk + for mmap in [False, True]: + filename = 'test-8000Hz-le-3ch-5S-64bit.wav' + rate, data = wavfile.read(datafile(filename), mmap=mmap) + + assert_equal(rate, 8000) + assert_(np.issubdtype(data.dtype, np.int64)) + assert_equal(data.shape, (5, 3)) + + # Hand-made max/min samples under different conventions: + # Fixed-point 2**(N-1) Full-scale 2**(N-1)-1 LSB + correct = [[-0x8000_0000_0000_0000, -0x7fff_ffff_ffff_ffff, -0x2], + [-0x4000_0000_0000_0000, -0x3fff_ffff_ffff_ffff, -0x1], + [+0x0000_0000_0000_0000, +0x0000_0000_0000_0000, +0x0], + [+0x4000_0000_0000_0000, +0x3fff_ffff_ffff_ffff, +0x1], + [+0x7fff_ffff_ffff_ffff, +0x7fff_ffff_ffff_ffff, +0x2]] + # ^ clipped + + assert_equal(data, correct) + + del data + + +def test_unsupported_mmap(): + # Test containers that cannot be mapped to numpy types + for filename in {'test-8000Hz-le-3ch-5S-24bit.wav', + 'test-8000Hz-le-3ch-5S-36bit.wav', + 'test-8000Hz-le-3ch-5S-45bit.wav', + 'test-8000Hz-le-3ch-5S-53bit.wav', + 'test-1234Hz-le-1ch-10S-20bit-extra.wav'}: + with raises(ValueError, match="mmap.*not compatible"): + rate, data = wavfile.read(datafile(filename), mmap=True) + + +def test_rifx(): + # Compare equivalent RIFX and RIFF files + for rifx, riff in {('test-44100Hz-be-1ch-4bytes.wav', + 'test-44100Hz-le-1ch-4bytes.wav'), + ('test-8000Hz-be-3ch-5S-24bit.wav', + 'test-8000Hz-le-3ch-5S-24bit.wav')}: + rate1, data1 = wavfile.read(datafile(rifx), mmap=False) + rate2, data2 = wavfile.read(datafile(riff), mmap=False) + assert_equal(rate1, rate2) + assert_equal(data1, data2) + + +def test_rf64(): + # Compare equivalent RF64 and RIFF files + for rf64, riff in {('test-44100Hz-le-1ch-4bytes-rf64.wav', + 'test-44100Hz-le-1ch-4bytes.wav'), + ('test-8000Hz-le-3ch-5S-24bit-rf64.wav', + 'test-8000Hz-le-3ch-5S-24bit.wav')}: + rate1, data1 = wavfile.read(datafile(rf64), mmap=False) + rate2, data2 = wavfile.read(datafile(riff), mmap=False) + assert_array_equal(rate1, rate2) + assert_array_equal(data1, data2) + + +@pytest.mark.xslow +def test_write_roundtrip_rf64(tmpdir): + dtype = np.dtype(" 0 + assert rate == 44100 + # also test writing (gh-12176) + data[0] = 0 + + +def test_read_early_eof(): + # File ends after 'fact' chunk at boundary, no data read + for mmap in [False, True]: + filename = 'test-44100Hz-le-1ch-4bytes-early-eof-no-data.wav' + with open(datafile(filename), 'rb') as fp: + with raises(ValueError, match="Unexpected end of file."): + wavfile.read(fp, mmap=mmap) + + +def test_read_incomplete_chunk(): + # File ends inside 'fmt ' chunk ID, no data read + for mmap in [False, True]: + filename = 'test-44100Hz-le-1ch-4bytes-incomplete-chunk.wav' + with open(datafile(filename), 'rb') as fp: + with raises(ValueError, match="Incomplete chunk ID.*b'f'"): + wavfile.read(fp, mmap=mmap) + + +def test_read_inconsistent_header(): + # File header's size fields contradict each other + for mmap in [False, True]: + filename = 'test-8000Hz-le-3ch-5S-24bit-inconsistent.wav' + with open(datafile(filename), 'rb') as fp: + with raises(ValueError, match="header is invalid"): + wavfile.read(fp, mmap=mmap) + + +# signed 8-bit integer PCM is not allowed +# unsigned > 8-bit integer PCM is not allowed +# 8- or 16-bit float PCM is not expected +# g and q are platform-dependent, so not included +@pytest.mark.parametrize("dt_str", ["i2", ">i4", ">i8", ">f4", ">f8", '|u1']) +@pytest.mark.parametrize("channels", [1, 2, 5]) +@pytest.mark.parametrize("rate", [8000, 32000]) +@pytest.mark.parametrize("mmap", [False, True]) +@pytest.mark.parametrize("realfile", [False, True]) +def test_write_roundtrip(realfile, mmap, rate, channels, dt_str, tmpdir): + dtype = np.dtype(dt_str) + if realfile: + tmpfile = str(tmpdir.join(str(threading.get_native_id()), 'temp.wav')) + os.makedirs(os.path.dirname(tmpfile), exist_ok=True) + else: + tmpfile = BytesIO() + data = np.random.rand(100, channels) + if channels == 1: + data = data[:, 0] + if dtype.kind == 'f': + # The range of the float type should be in [-1, 1] + data = data.astype(dtype) + else: + data = (data*128).astype(dtype) + + wavfile.write(tmpfile, rate, data) + + rate2, data2 = wavfile.read(tmpfile, mmap=mmap) + + assert_equal(rate, rate2) + assert_(data2.dtype.byteorder in ('<', '=', '|'), msg=data2.dtype) + assert_array_equal(data, data2) + # also test writing (gh-12176) + if realfile: + data2[0] = 0 + else: + with pytest.raises(ValueError, match='read-only'): + data2[0] = 0 + + if realfile and mmap and IS_PYPY and sys.platform == 'win32': + # windows cannot remove a dead file held by a mmap but not collected + # in PyPy; since the filename gets reused in this test, clean this up + break_cycles() + break_cycles() + + +@pytest.mark.parametrize("dtype", [np.float16]) +def test_wavfile_dtype_unsupported(tmpdir, dtype): + tmpfile = str(tmpdir.join('temp.wav')) + rng = np.random.default_rng(1234) + data = rng.random((100, 5)).astype(dtype) + rate = 8000 + with pytest.raises(ValueError, match="Unsupported"): + wavfile.write(tmpfile, rate, data) diff --git a/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/wavfile.py b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/wavfile.py new file mode 100644 index 0000000000000000000000000000000000000000..b6978a1c461c825e35b8a1f0d7de39fceba38bd6 --- /dev/null +++ b/miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/io/wavfile.py @@ -0,0 +1,891 @@ +""" +Module to read / write wav files using NumPy arrays + +Functions +--------- +`read`: Return the sample rate (in samples/sec) and data from a WAV file. + +`write`: Write a NumPy array as a WAV file. + +""" +import io +import sys +import numpy as np +import struct +import warnings +from enum import IntEnum + + +__all__ = [ + 'WavFileWarning', + 'read', + 'write' +] + + +class WavFileWarning(UserWarning): + pass + + +class WAVE_FORMAT(IntEnum): + """ + WAVE form wFormatTag IDs + + Complete list is in mmreg.h in Windows 10 SDK. ALAC and OPUS are the + newest additions, in v10.0.14393 2016-07 + """ + UNKNOWN = 0x0000 + PCM = 0x0001 + ADPCM = 0x0002 + IEEE_FLOAT = 0x0003 + VSELP = 0x0004 + IBM_CVSD = 0x0005 + ALAW = 0x0006 + MULAW = 0x0007 + DTS = 0x0008 + DRM = 0x0009 + WMAVOICE9 = 0x000A + WMAVOICE10 = 0x000B + OKI_ADPCM = 0x0010 + DVI_ADPCM = 0x0011 + IMA_ADPCM = 0x0011 # Duplicate + MEDIASPACE_ADPCM = 0x0012 + SIERRA_ADPCM = 0x0013 + G723_ADPCM = 0x0014 + DIGISTD = 0x0015 + DIGIFIX = 0x0016 + DIALOGIC_OKI_ADPCM = 0x0017 + MEDIAVISION_ADPCM = 0x0018 + CU_CODEC = 0x0019 + HP_DYN_VOICE = 0x001A + YAMAHA_ADPCM = 0x0020 + SONARC = 0x0021 + DSPGROUP_TRUESPEECH = 0x0022 + ECHOSC1 = 0x0023 + AUDIOFILE_AF36 = 0x0024 + APTX = 0x0025 + AUDIOFILE_AF10 = 0x0026 + PROSODY_1612 = 0x0027 + LRC = 0x0028 + DOLBY_AC2 = 0x0030 + GSM610 = 0x0031 + MSNAUDIO = 0x0032 + ANTEX_ADPCME = 0x0033 + CONTROL_RES_VQLPC = 0x0034 + DIGIREAL = 0x0035 + DIGIADPCM = 0x0036 + CONTROL_RES_CR10 = 0x0037 + NMS_VBXADPCM = 0x0038 + CS_IMAADPCM = 0x0039 + ECHOSC3 = 0x003A + ROCKWELL_ADPCM = 0x003B + ROCKWELL_DIGITALK = 0x003C + XEBEC = 0x003D + G721_ADPCM = 0x0040 + G728_CELP = 0x0041 + MSG723 = 0x0042 + INTEL_G723_1 = 0x0043 + INTEL_G729 = 0x0044 + SHARP_G726 = 0x0045 + MPEG = 0x0050 + RT24 = 0x0052 + PAC = 0x0053 + MPEGLAYER3 = 0x0055 + LUCENT_G723 = 0x0059 + CIRRUS = 0x0060 + ESPCM = 0x0061 + VOXWARE = 0x0062 + CANOPUS_ATRAC = 0x0063 + G726_ADPCM = 0x0064 + G722_ADPCM = 0x0065 + DSAT = 0x0066 + DSAT_DISPLAY = 0x0067 + VOXWARE_BYTE_ALIGNED = 0x0069 + VOXWARE_AC8 = 0x0070 + VOXWARE_AC10 = 0x0071 + VOXWARE_AC16 = 0x0072 + VOXWARE_AC20 = 0x0073 + VOXWARE_RT24 = 0x0074 + VOXWARE_RT29 = 0x0075 + VOXWARE_RT29HW = 0x0076 + VOXWARE_VR12 = 0x0077 + VOXWARE_VR18 = 0x0078 + VOXWARE_TQ40 = 0x0079 + VOXWARE_SC3 = 0x007A + VOXWARE_SC3_1 = 0x007B + SOFTSOUND = 0x0080 + VOXWARE_TQ60 = 0x0081 + MSRT24 = 0x0082 + G729A = 0x0083 + MVI_MVI2 = 0x0084 + DF_G726 = 0x0085 + DF_GSM610 = 0x0086 + ISIAUDIO = 0x0088 + ONLIVE = 0x0089 + MULTITUDE_FT_SX20 = 0x008A + INFOCOM_ITS_G721_ADPCM = 0x008B + CONVEDIA_G729 = 0x008C + CONGRUENCY = 0x008D + SBC24 = 0x0091 + DOLBY_AC3_SPDIF = 0x0092 + MEDIASONIC_G723 = 0x0093 + PROSODY_8KBPS = 0x0094 + ZYXEL_ADPCM = 0x0097 + PHILIPS_LPCBB = 0x0098 + PACKED = 0x0099 + MALDEN_PHONYTALK = 0x00A0 + RACAL_RECORDER_GSM = 0x00A1 + RACAL_RECORDER_G720_A = 0x00A2 + RACAL_RECORDER_G723_1 = 0x00A3 + RACAL_RECORDER_TETRA_ACELP = 0x00A4 + NEC_AAC = 0x00B0 + RAW_AAC1 = 0x00FF + RHETOREX_ADPCM = 0x0100 + IRAT = 0x0101 + VIVO_G723 = 0x0111 + VIVO_SIREN = 0x0112 + PHILIPS_CELP = 0x0120 + PHILIPS_GRUNDIG = 0x0121 + DIGITAL_G723 = 0x0123 + SANYO_LD_ADPCM = 0x0125 + SIPROLAB_ACEPLNET = 0x0130 + SIPROLAB_ACELP4800 = 0x0131 + SIPROLAB_ACELP8V3 = 0x0132 + SIPROLAB_G729 = 0x0133 + SIPROLAB_G729A = 0x0134 + SIPROLAB_KELVIN = 0x0135 + VOICEAGE_AMR = 0x0136 + G726ADPCM = 0x0140 + DICTAPHONE_CELP68 = 0x0141 + DICTAPHONE_CELP54 = 0x0142 + QUALCOMM_PUREVOICE = 0x0150 + QUALCOMM_HALFRATE = 0x0151 + TUBGSM = 0x0155 + MSAUDIO1 = 0x0160 + WMAUDIO2 = 0x0161 + WMAUDIO3 = 0x0162 + WMAUDIO_LOSSLESS = 0x0163 + WMASPDIF = 0x0164 + UNISYS_NAP_ADPCM = 0x0170 + UNISYS_NAP_ULAW = 0x0171 + UNISYS_NAP_ALAW = 0x0172 + UNISYS_NAP_16K = 0x0173 + SYCOM_ACM_SYC008 = 0x0174 + SYCOM_ACM_SYC701_G726L = 0x0175 + SYCOM_ACM_SYC701_CELP54 = 0x0176 + SYCOM_ACM_SYC701_CELP68 = 0x0177 + KNOWLEDGE_ADVENTURE_ADPCM = 0x0178 + FRAUNHOFER_IIS_MPEG2_AAC = 0x0180 + DTS_DS = 0x0190 + CREATIVE_ADPCM = 0x0200 + CREATIVE_FASTSPEECH8 = 0x0202 + CREATIVE_FASTSPEECH10 = 0x0203 + UHER_ADPCM = 0x0210 + ULEAD_DV_AUDIO = 0x0215 + ULEAD_DV_AUDIO_1 = 0x0216 + QUARTERDECK = 0x0220 + ILINK_VC = 0x0230 + RAW_SPORT = 0x0240 + ESST_AC3 = 0x0241 + GENERIC_PASSTHRU = 0x0249 + IPI_HSX = 0x0250 + IPI_RPELP = 0x0251 + CS2 = 0x0260 + SONY_SCX = 0x0270 + SONY_SCY = 0x0271 + SONY_ATRAC3 = 0x0272 + SONY_SPC = 0x0273 + TELUM_AUDIO = 0x0280 + TELUM_IA_AUDIO = 0x0281 + NORCOM_VOICE_SYSTEMS_ADPCM = 0x0285 + FM_TOWNS_SND = 0x0300 + MICRONAS = 0x0350 + MICRONAS_CELP833 = 0x0351 + BTV_DIGITAL = 0x0400 + INTEL_MUSIC_CODER = 0x0401 + INDEO_AUDIO = 0x0402 + QDESIGN_MUSIC = 0x0450 + ON2_VP7_AUDIO = 0x0500 + ON2_VP6_AUDIO = 0x0501 + VME_VMPCM = 0x0680 + TPC = 0x0681 + LIGHTWAVE_LOSSLESS = 0x08AE + OLIGSM = 0x1000 + OLIADPCM = 0x1001 + OLICELP = 0x1002 + OLISBC = 0x1003 + OLIOPR = 0x1004 + LH_CODEC = 0x1100 + LH_CODEC_CELP = 0x1101 + LH_CODEC_SBC8 = 0x1102 + LH_CODEC_SBC12 = 0x1103 + LH_CODEC_SBC16 = 0x1104 + NORRIS = 0x1400 + ISIAUDIO_2 = 0x1401 + SOUNDSPACE_MUSICOMPRESS = 0x1500 + MPEG_ADTS_AAC = 0x1600 + MPEG_RAW_AAC = 0x1601 + MPEG_LOAS = 0x1602 + NOKIA_MPEG_ADTS_AAC = 0x1608 + NOKIA_MPEG_RAW_AAC = 0x1609 + VODAFONE_MPEG_ADTS_AAC = 0x160A + VODAFONE_MPEG_RAW_AAC = 0x160B + MPEG_HEAAC = 0x1610 + VOXWARE_RT24_SPEECH = 0x181C + SONICFOUNDRY_LOSSLESS = 0x1971 + INNINGS_TELECOM_ADPCM = 0x1979 + LUCENT_SX8300P = 0x1C07 + LUCENT_SX5363S = 0x1C0C + CUSEEME = 0x1F03 + NTCSOFT_ALF2CM_ACM = 0x1FC4 + DVM = 0x2000 + DTS2 = 0x2001 + MAKEAVIS = 0x3313 + DIVIO_MPEG4_AAC = 0x4143 + NOKIA_ADAPTIVE_MULTIRATE = 0x4201 + DIVIO_G726 = 0x4243 + LEAD_SPEECH = 0x434C + LEAD_VORBIS = 0x564C + WAVPACK_AUDIO = 0x5756 + OGG_VORBIS_MODE_1 = 0x674F + OGG_VORBIS_MODE_2 = 0x6750 + OGG_VORBIS_MODE_3 = 0x6751 + OGG_VORBIS_MODE_1_PLUS = 0x676F + OGG_VORBIS_MODE_2_PLUS = 0x6770 + OGG_VORBIS_MODE_3_PLUS = 0x6771 + ALAC = 0x6C61 + _3COM_NBX = 0x7000 # Can't have leading digit + OPUS = 0x704F + FAAD_AAC = 0x706D + AMR_NB = 0x7361 + AMR_WB = 0x7362 + AMR_WP = 0x7363 + GSM_AMR_CBR = 0x7A21 + GSM_AMR_VBR_SID = 0x7A22 + COMVERSE_INFOSYS_G723_1 = 0xA100 + COMVERSE_INFOSYS_AVQSBC = 0xA101 + COMVERSE_INFOSYS_SBC = 0xA102 + SYMBOL_G729_A = 0xA103 + VOICEAGE_AMR_WB = 0xA104 + INGENIENT_G726 = 0xA105 + MPEG4_AAC = 0xA106 + ENCORE_G726 = 0xA107 + ZOLL_ASAO = 0xA108 + SPEEX_VOICE = 0xA109 + VIANIX_MASC = 0xA10A + WM9_SPECTRUM_ANALYZER = 0xA10B + WMF_SPECTRUM_ANAYZER = 0xA10C + GSM_610 = 0xA10D + GSM_620 = 0xA10E + GSM_660 = 0xA10F + GSM_690 = 0xA110 + GSM_ADAPTIVE_MULTIRATE_WB = 0xA111 + POLYCOM_G722 = 0xA112 + POLYCOM_G728 = 0xA113 + POLYCOM_G729_A = 0xA114 + POLYCOM_SIREN = 0xA115 + GLOBAL_IP_ILBC = 0xA116 + RADIOTIME_TIME_SHIFT_RADIO = 0xA117 + NICE_ACA = 0xA118 + NICE_ADPCM = 0xA119 + VOCORD_G721 = 0xA11A + VOCORD_G726 = 0xA11B + VOCORD_G722_1 = 0xA11C + VOCORD_G728 = 0xA11D + VOCORD_G729 = 0xA11E + VOCORD_G729_A = 0xA11F + VOCORD_G723_1 = 0xA120 + VOCORD_LBC = 0xA121 + NICE_G728 = 0xA122 + FRACE_TELECOM_G729 = 0xA123 + CODIAN = 0xA124 + FLAC = 0xF1AC + EXTENSIBLE = 0xFFFE + DEVELOPMENT = 0xFFFF + + +KNOWN_WAVE_FORMATS = {WAVE_FORMAT.PCM, WAVE_FORMAT.IEEE_FLOAT} + + +def _raise_bad_format(format_tag): + try: + format_name = WAVE_FORMAT(format_tag).name + except ValueError: + format_name = f'{format_tag:#06x}' + raise ValueError(f"Unknown wave file format: {format_name}. Supported " + "formats: " + + ', '.join(x.name for x in KNOWN_WAVE_FORMATS)) + + +def _read_fmt_chunk(fid, is_big_endian): + """ + Returns + ------- + size : int + size of format subchunk in bytes (minus 8 for "fmt " and itself) + format_tag : int + PCM, float, or compressed format + channels : int + number of channels + fs : int + sampling frequency in samples per second + bytes_per_second : int + overall byte rate for the file + block_align : int + bytes per sample, including all channels + bit_depth : int + bits per sample + + Notes + ----- + Assumes file pointer is immediately after the 'fmt ' id + """ + if is_big_endian: + fmt = '>' + else: + fmt = '<' + + size = struct.unpack(fmt+'I', fid.read(4))[0] + + if size < 16: + raise ValueError("Binary structure of wave file is not compliant") + + res = struct.unpack(fmt+'HHIIHH', fid.read(16)) + bytes_read = 16 + + format_tag, channels, fs, bytes_per_second, block_align, bit_depth = res + + if format_tag == WAVE_FORMAT.EXTENSIBLE and size >= (16+2): + ext_chunk_size = struct.unpack(fmt+'H', fid.read(2))[0] + bytes_read += 2 + if ext_chunk_size >= 22: + extensible_chunk_data = fid.read(22) + bytes_read += 22 + raw_guid = extensible_chunk_data[2+4:2+4+16] + # GUID template {XXXXXXXX-0000-0010-8000-00AA00389B71} (RFC-2361) + # MS GUID byte order: first three groups are native byte order, + # rest is Big Endian + if is_big_endian: + tail = b'\x00\x00\x00\x10\x80\x00\x00\xAA\x00\x38\x9B\x71' + else: + tail = b'\x00\x00\x10\x00\x80\x00\x00\xAA\x00\x38\x9B\x71' + if raw_guid.endswith(tail): + format_tag = struct.unpack(fmt+'I', raw_guid[:4])[0] + else: + raise ValueError("Binary structure of wave file is not compliant") + + if format_tag not in KNOWN_WAVE_FORMATS: + _raise_bad_format(format_tag) + + # move file pointer to next chunk + if size > bytes_read: + fid.read(size - bytes_read) + + # fmt should always be 16, 18 or 40, but handle it just in case + _handle_pad_byte(fid, size) + + if format_tag == WAVE_FORMAT.PCM: + if bytes_per_second != fs * block_align: + raise ValueError("WAV header is invalid: nAvgBytesPerSec must" + " equal product of nSamplesPerSec and" + " nBlockAlign, but file has nSamplesPerSec =" + f" {fs}, nBlockAlign = {block_align}, and" + f" nAvgBytesPerSec = {bytes_per_second}") + + return (size, format_tag, channels, fs, bytes_per_second, block_align, + bit_depth) + + +def _read_data_chunk(fid, format_tag, channels, bit_depth, is_big_endian, is_rf64, + block_align, mmap=False): + """ + Notes + ----- + Assumes file pointer is immediately after the 'data' id + + It's possible to not use all available bits in a container, or to store + samples in a container bigger than necessary, so bytes_per_sample uses + the actual reported container size (nBlockAlign / nChannels). Real-world + examples: + + Adobe Audition's "24-bit packed int (type 1, 20-bit)" + + nChannels = 2, nBlockAlign = 6, wBitsPerSample = 20 + + http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/Samples/AFsp/M1F1-int12-AFsp.wav + is: + + nChannels = 2, nBlockAlign = 4, wBitsPerSample = 12 + + http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/Docs/multichaudP.pdf + gives an example of: + + nChannels = 2, nBlockAlign = 8, wBitsPerSample = 20 + """ + if is_big_endian: + fmt = '>' + else: + fmt = '<' + + # Size of the data subchunk in bytes + if not is_rf64: + size = struct.unpack(fmt+'I', fid.read(4))[0] + else: + pos = fid.tell() + # chunk size is stored in global file header for RF64 + fid.seek(28) + size = struct.unpack(' 1: + data = data.reshape(-1, channels) + return data + + +def _skip_unknown_chunk(fid, is_big_endian): + if is_big_endian: + fmt = '>I' + else: + fmt = '>> from os.path import dirname, join as pjoin + >>> from scipy.io import wavfile + >>> import scipy.io + + Get the filename for an example .wav file from the tests/data directory. + + >>> data_dir = pjoin(dirname(scipy.io.__file__), 'tests', 'data') + >>> wav_fname = pjoin(data_dir, 'test-44100Hz-2ch-32bit-float-be.wav') + + Load the .wav file contents. + + >>> samplerate, data = wavfile.read(wav_fname) + >>> print(f"number of channels = {data.shape[1]}") + number of channels = 2 + >>> length = data.shape[0] / samplerate + >>> print(f"length = {length}s") + length = 0.01s + + Plot the waveform. + + >>> import matplotlib.pyplot as plt + >>> import numpy as np + >>> time = np.linspace(0., length, data.shape[0]) + >>> plt.plot(time, data[:, 0], label="Left channel") + >>> plt.plot(time, data[:, 1], label="Right channel") + >>> plt.legend() + >>> plt.xlabel("Time [s]") + >>> plt.ylabel("Amplitude") + >>> plt.show() + + """ + if hasattr(filename, 'read'): + fid = filename + mmap = False + else: + fid = open(filename, 'rb') + + try: + file_size, is_big_endian, is_rf64 = _read_riff_chunk(fid) + fmt_chunk_received = False + data_chunk_received = False + while fid.tell() < file_size: + # read the next chunk + chunk_id = fid.read(4) + + if not chunk_id: + if data_chunk_received: + # End of file but data successfully read + warnings.warn( + f"Reached EOF prematurely; finished at {fid.tell():d} bytes, " + f"expected {file_size:d} bytes from header.", + WavFileWarning, stacklevel=2) + break + else: + raise ValueError("Unexpected end of file.") + elif len(chunk_id) < 4: + msg = f"Incomplete chunk ID: {repr(chunk_id)}" + # If we have the data, ignore the broken chunk + if fmt_chunk_received and data_chunk_received: + warnings.warn(msg + ", ignoring it.", WavFileWarning, + stacklevel=2) + else: + raise ValueError(msg) + + if chunk_id == b'fmt ': + fmt_chunk_received = True + fmt_chunk = _read_fmt_chunk(fid, is_big_endian) + format_tag, channels, fs = fmt_chunk[1:4] + bit_depth = fmt_chunk[6] + block_align = fmt_chunk[5] + elif chunk_id == b'fact': + _skip_unknown_chunk(fid, is_big_endian) + elif chunk_id == b'data': + data_chunk_received = True + if not fmt_chunk_received: + raise ValueError("No fmt chunk before data") + data = _read_data_chunk(fid, format_tag, channels, bit_depth, + is_big_endian, is_rf64, block_align, mmap) + elif chunk_id == b'LIST': + # Someday this could be handled properly but for now skip it + _skip_unknown_chunk(fid, is_big_endian) + elif chunk_id in {b'JUNK', b'Fake'}: + # Skip alignment chunks without warning + _skip_unknown_chunk(fid, is_big_endian) + else: + warnings.warn("Chunk (non-data) not understood, skipping it.", + WavFileWarning, stacklevel=2) + _skip_unknown_chunk(fid, is_big_endian) + finally: + if not hasattr(filename, 'read'): + fid.close() + else: + fid.seek(0) + + return fs, data + + +def write(filename, rate, data): + """ + Write a NumPy array as a WAV file. + + Parameters + ---------- + filename : string or open file handle + Output wav file. + rate : int + The sample rate (in samples/sec). + data : ndarray + A 1-D or 2-D NumPy array of either integer or float data-type. + + Notes + ----- + * Writes a simple uncompressed WAV file. + * To write multiple-channels, use a 2-D array of shape + (Nsamples, Nchannels). + * The bits-per-sample and PCM/float will be determined by the data-type. + + Common data types: [1]_ + + ===================== =========== =========== ============= + WAV format Min Max NumPy dtype + ===================== =========== =========== ============= + 32-bit floating-point -1.0 +1.0 float32 + 32-bit PCM -2147483648 +2147483647 int32 + 16-bit PCM -32768 +32767 int16 + 8-bit PCM 0 255 uint8 + ===================== =========== =========== ============= + + Note that 8-bit PCM is unsigned. + + References + ---------- + .. [1] IBM Corporation and Microsoft Corporation, "Multimedia Programming + Interface and Data Specifications 1.0", section "Data Format of the + Samples", August 1991 + http://www.tactilemedia.com/info/MCI_Control_Info.html + + Examples + -------- + Create a 100Hz sine wave, sampled at 44100Hz. + Write to 16-bit PCM, Mono. + + >>> from scipy.io.wavfile import write + >>> import numpy as np + >>> samplerate = 44100; fs = 100 + >>> t = np.linspace(0., 1., samplerate) + >>> amplitude = np.iinfo(np.int16).max + >>> data = amplitude * np.sin(2. * np.pi * fs * t) + >>> write("example.wav", samplerate, data.astype(np.int16)) + + """ + if hasattr(filename, 'write'): + fid = filename + else: + fid = open(filename, 'wb') + + fs = rate + + try: + dkind = data.dtype.kind + allowed_dtypes = ['float32', 'float64', + 'uint8', 'int16', 'int32', 'int64'] + if data.dtype.name not in allowed_dtypes: + raise ValueError(f"Unsupported data type '{data.dtype}'") + + header_data = b'' + + header_data += b'RIFF' + header_data += b'\x00\x00\x00\x00' + header_data += b'WAVE' + + # fmt chunk + header_data += b'fmt ' + if dkind == 'f': + format_tag = WAVE_FORMAT.IEEE_FLOAT + else: + format_tag = WAVE_FORMAT.PCM + if data.ndim == 1: + channels = 1 + else: + channels = data.shape[1] + bit_depth = data.dtype.itemsize * 8 + bytes_per_second = fs*(bit_depth // 8)*channels + block_align = channels * (bit_depth // 8) + + fmt_chunk_data = struct.pack(' 0xFFFFFFFF + if is_rf64: + header_data = b'' + header_data += b'RF64' + header_data += b'\xFF\xFF\xFF\xFF' + header_data += b'WAVE' + header_data += b'ds64' + # size of ds64 chunk + header_data += struct.pack('' or (data.dtype.byteorder == '=' and + sys.byteorder == 'big'): + data = data.byteswap() + _array_tofile(fid, data) + + # Determine file size and place it in correct + # position at start of the file or the data chunk. + size = fid.tell() + if not is_rf64: + fid.seek(4) + fid.write(struct.pack('