scipy 1.16.2__cp312-cp312-win_arm64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- scipy/__config__.py +161 -0
- scipy/__init__.py +150 -0
- scipy/_cyutility.cp312-win_arm64.lib +0 -0
- scipy/_cyutility.cp312-win_arm64.pyd +0 -0
- scipy/_distributor_init.py +18 -0
- scipy/_lib/__init__.py +14 -0
- scipy/_lib/_array_api.py +931 -0
- scipy/_lib/_array_api_compat_vendor.py +9 -0
- scipy/_lib/_array_api_no_0d.py +103 -0
- scipy/_lib/_bunch.py +229 -0
- scipy/_lib/_ccallback.py +251 -0
- scipy/_lib/_ccallback_c.cp312-win_arm64.lib +0 -0
- scipy/_lib/_ccallback_c.cp312-win_arm64.pyd +0 -0
- scipy/_lib/_disjoint_set.py +254 -0
- scipy/_lib/_docscrape.py +761 -0
- scipy/_lib/_elementwise_iterative_method.py +346 -0
- scipy/_lib/_fpumode.cp312-win_arm64.lib +0 -0
- scipy/_lib/_fpumode.cp312-win_arm64.pyd +0 -0
- scipy/_lib/_gcutils.py +105 -0
- scipy/_lib/_pep440.py +487 -0
- scipy/_lib/_sparse.py +41 -0
- scipy/_lib/_test_ccallback.cp312-win_arm64.lib +0 -0
- scipy/_lib/_test_ccallback.cp312-win_arm64.pyd +0 -0
- scipy/_lib/_test_deprecation_call.cp312-win_arm64.lib +0 -0
- scipy/_lib/_test_deprecation_call.cp312-win_arm64.pyd +0 -0
- scipy/_lib/_test_deprecation_def.cp312-win_arm64.lib +0 -0
- scipy/_lib/_test_deprecation_def.cp312-win_arm64.pyd +0 -0
- scipy/_lib/_testutils.py +373 -0
- scipy/_lib/_threadsafety.py +58 -0
- scipy/_lib/_tmpdirs.py +86 -0
- scipy/_lib/_uarray/LICENSE +29 -0
- scipy/_lib/_uarray/__init__.py +116 -0
- scipy/_lib/_uarray/_backend.py +707 -0
- scipy/_lib/_uarray/_uarray.cp312-win_arm64.lib +0 -0
- scipy/_lib/_uarray/_uarray.cp312-win_arm64.pyd +0 -0
- scipy/_lib/_util.py +1283 -0
- scipy/_lib/array_api_compat/__init__.py +22 -0
- scipy/_lib/array_api_compat/_internal.py +59 -0
- scipy/_lib/array_api_compat/common/__init__.py +1 -0
- scipy/_lib/array_api_compat/common/_aliases.py +727 -0
- scipy/_lib/array_api_compat/common/_fft.py +213 -0
- scipy/_lib/array_api_compat/common/_helpers.py +1058 -0
- scipy/_lib/array_api_compat/common/_linalg.py +232 -0
- scipy/_lib/array_api_compat/common/_typing.py +192 -0
- scipy/_lib/array_api_compat/cupy/__init__.py +13 -0
- scipy/_lib/array_api_compat/cupy/_aliases.py +156 -0
- scipy/_lib/array_api_compat/cupy/_info.py +336 -0
- scipy/_lib/array_api_compat/cupy/_typing.py +31 -0
- scipy/_lib/array_api_compat/cupy/fft.py +36 -0
- scipy/_lib/array_api_compat/cupy/linalg.py +49 -0
- scipy/_lib/array_api_compat/dask/__init__.py +0 -0
- scipy/_lib/array_api_compat/dask/array/__init__.py +12 -0
- scipy/_lib/array_api_compat/dask/array/_aliases.py +376 -0
- scipy/_lib/array_api_compat/dask/array/_info.py +416 -0
- scipy/_lib/array_api_compat/dask/array/fft.py +21 -0
- scipy/_lib/array_api_compat/dask/array/linalg.py +72 -0
- scipy/_lib/array_api_compat/numpy/__init__.py +28 -0
- scipy/_lib/array_api_compat/numpy/_aliases.py +190 -0
- scipy/_lib/array_api_compat/numpy/_info.py +366 -0
- scipy/_lib/array_api_compat/numpy/_typing.py +30 -0
- scipy/_lib/array_api_compat/numpy/fft.py +35 -0
- scipy/_lib/array_api_compat/numpy/linalg.py +143 -0
- scipy/_lib/array_api_compat/torch/__init__.py +22 -0
- scipy/_lib/array_api_compat/torch/_aliases.py +855 -0
- scipy/_lib/array_api_compat/torch/_info.py +369 -0
- scipy/_lib/array_api_compat/torch/_typing.py +3 -0
- scipy/_lib/array_api_compat/torch/fft.py +85 -0
- scipy/_lib/array_api_compat/torch/linalg.py +121 -0
- scipy/_lib/array_api_extra/__init__.py +38 -0
- scipy/_lib/array_api_extra/_delegation.py +171 -0
- scipy/_lib/array_api_extra/_lib/__init__.py +1 -0
- scipy/_lib/array_api_extra/_lib/_at.py +463 -0
- scipy/_lib/array_api_extra/_lib/_backends.py +46 -0
- scipy/_lib/array_api_extra/_lib/_funcs.py +937 -0
- scipy/_lib/array_api_extra/_lib/_lazy.py +357 -0
- scipy/_lib/array_api_extra/_lib/_testing.py +278 -0
- scipy/_lib/array_api_extra/_lib/_utils/__init__.py +1 -0
- scipy/_lib/array_api_extra/_lib/_utils/_compat.py +74 -0
- scipy/_lib/array_api_extra/_lib/_utils/_compat.pyi +45 -0
- scipy/_lib/array_api_extra/_lib/_utils/_helpers.py +559 -0
- scipy/_lib/array_api_extra/_lib/_utils/_typing.py +10 -0
- scipy/_lib/array_api_extra/_lib/_utils/_typing.pyi +105 -0
- scipy/_lib/array_api_extra/testing.py +359 -0
- scipy/_lib/cobyqa/__init__.py +20 -0
- scipy/_lib/cobyqa/framework.py +1240 -0
- scipy/_lib/cobyqa/main.py +1506 -0
- scipy/_lib/cobyqa/models.py +1529 -0
- scipy/_lib/cobyqa/problem.py +1296 -0
- scipy/_lib/cobyqa/settings.py +132 -0
- scipy/_lib/cobyqa/subsolvers/__init__.py +14 -0
- scipy/_lib/cobyqa/subsolvers/geometry.py +387 -0
- scipy/_lib/cobyqa/subsolvers/optim.py +1203 -0
- scipy/_lib/cobyqa/utils/__init__.py +18 -0
- scipy/_lib/cobyqa/utils/exceptions.py +22 -0
- scipy/_lib/cobyqa/utils/math.py +77 -0
- scipy/_lib/cobyqa/utils/versions.py +67 -0
- scipy/_lib/decorator.py +399 -0
- scipy/_lib/deprecation.py +274 -0
- scipy/_lib/doccer.py +366 -0
- scipy/_lib/messagestream.cp312-win_arm64.lib +0 -0
- scipy/_lib/messagestream.cp312-win_arm64.pyd +0 -0
- scipy/_lib/pyprima/__init__.py +212 -0
- scipy/_lib/pyprima/cobyla/__init__.py +0 -0
- scipy/_lib/pyprima/cobyla/cobyla.py +559 -0
- scipy/_lib/pyprima/cobyla/cobylb.py +714 -0
- scipy/_lib/pyprima/cobyla/geometry.py +226 -0
- scipy/_lib/pyprima/cobyla/initialize.py +215 -0
- scipy/_lib/pyprima/cobyla/trustregion.py +492 -0
- scipy/_lib/pyprima/cobyla/update.py +289 -0
- scipy/_lib/pyprima/common/__init__.py +0 -0
- scipy/_lib/pyprima/common/_bounds.py +34 -0
- scipy/_lib/pyprima/common/_linear_constraints.py +46 -0
- scipy/_lib/pyprima/common/_nonlinear_constraints.py +54 -0
- scipy/_lib/pyprima/common/_project.py +173 -0
- scipy/_lib/pyprima/common/checkbreak.py +93 -0
- scipy/_lib/pyprima/common/consts.py +47 -0
- scipy/_lib/pyprima/common/evaluate.py +99 -0
- scipy/_lib/pyprima/common/history.py +38 -0
- scipy/_lib/pyprima/common/infos.py +30 -0
- scipy/_lib/pyprima/common/linalg.py +435 -0
- scipy/_lib/pyprima/common/message.py +290 -0
- scipy/_lib/pyprima/common/powalg.py +131 -0
- scipy/_lib/pyprima/common/preproc.py +277 -0
- scipy/_lib/pyprima/common/present.py +5 -0
- scipy/_lib/pyprima/common/ratio.py +54 -0
- scipy/_lib/pyprima/common/redrho.py +47 -0
- scipy/_lib/pyprima/common/selectx.py +296 -0
- scipy/_lib/tests/__init__.py +0 -0
- scipy/_lib/tests/test__gcutils.py +110 -0
- scipy/_lib/tests/test__pep440.py +67 -0
- scipy/_lib/tests/test__testutils.py +32 -0
- scipy/_lib/tests/test__threadsafety.py +51 -0
- scipy/_lib/tests/test__util.py +641 -0
- scipy/_lib/tests/test_array_api.py +322 -0
- scipy/_lib/tests/test_bunch.py +169 -0
- scipy/_lib/tests/test_ccallback.py +196 -0
- scipy/_lib/tests/test_config.py +45 -0
- scipy/_lib/tests/test_deprecation.py +10 -0
- scipy/_lib/tests/test_doccer.py +143 -0
- scipy/_lib/tests/test_import_cycles.py +18 -0
- scipy/_lib/tests/test_public_api.py +482 -0
- scipy/_lib/tests/test_scipy_version.py +28 -0
- scipy/_lib/tests/test_tmpdirs.py +48 -0
- scipy/_lib/tests/test_warnings.py +137 -0
- scipy/_lib/uarray.py +31 -0
- scipy/cluster/__init__.py +31 -0
- scipy/cluster/_hierarchy.cp312-win_arm64.lib +0 -0
- scipy/cluster/_hierarchy.cp312-win_arm64.pyd +0 -0
- scipy/cluster/_optimal_leaf_ordering.cp312-win_arm64.lib +0 -0
- scipy/cluster/_optimal_leaf_ordering.cp312-win_arm64.pyd +0 -0
- scipy/cluster/_vq.cp312-win_arm64.lib +0 -0
- scipy/cluster/_vq.cp312-win_arm64.pyd +0 -0
- scipy/cluster/hierarchy.py +4348 -0
- scipy/cluster/tests/__init__.py +0 -0
- scipy/cluster/tests/hierarchy_test_data.py +145 -0
- scipy/cluster/tests/test_disjoint_set.py +202 -0
- scipy/cluster/tests/test_hierarchy.py +1238 -0
- scipy/cluster/tests/test_vq.py +434 -0
- scipy/cluster/vq.py +832 -0
- scipy/conftest.py +683 -0
- scipy/constants/__init__.py +358 -0
- scipy/constants/_codata.py +2266 -0
- scipy/constants/_constants.py +369 -0
- scipy/constants/codata.py +21 -0
- scipy/constants/constants.py +53 -0
- scipy/constants/tests/__init__.py +0 -0
- scipy/constants/tests/test_codata.py +78 -0
- scipy/constants/tests/test_constants.py +83 -0
- scipy/datasets/__init__.py +90 -0
- scipy/datasets/_download_all.py +71 -0
- scipy/datasets/_fetchers.py +225 -0
- scipy/datasets/_registry.py +26 -0
- scipy/datasets/_utils.py +81 -0
- scipy/datasets/tests/__init__.py +0 -0
- scipy/datasets/tests/test_data.py +128 -0
- scipy/differentiate/__init__.py +27 -0
- scipy/differentiate/_differentiate.py +1129 -0
- scipy/differentiate/tests/__init__.py +0 -0
- scipy/differentiate/tests/test_differentiate.py +694 -0
- scipy/fft/__init__.py +114 -0
- scipy/fft/_backend.py +196 -0
- scipy/fft/_basic.py +1650 -0
- scipy/fft/_basic_backend.py +197 -0
- scipy/fft/_debug_backends.py +22 -0
- scipy/fft/_fftlog.py +223 -0
- scipy/fft/_fftlog_backend.py +200 -0
- scipy/fft/_helper.py +348 -0
- scipy/fft/_pocketfft/LICENSE.md +25 -0
- scipy/fft/_pocketfft/__init__.py +9 -0
- scipy/fft/_pocketfft/basic.py +251 -0
- scipy/fft/_pocketfft/helper.py +249 -0
- scipy/fft/_pocketfft/pypocketfft.cp312-win_arm64.lib +0 -0
- scipy/fft/_pocketfft/pypocketfft.cp312-win_arm64.pyd +0 -0
- scipy/fft/_pocketfft/realtransforms.py +109 -0
- scipy/fft/_pocketfft/tests/__init__.py +0 -0
- scipy/fft/_pocketfft/tests/test_basic.py +1011 -0
- scipy/fft/_pocketfft/tests/test_real_transforms.py +505 -0
- scipy/fft/_realtransforms.py +706 -0
- scipy/fft/_realtransforms_backend.py +63 -0
- scipy/fft/tests/__init__.py +0 -0
- scipy/fft/tests/mock_backend.py +96 -0
- scipy/fft/tests/test_backend.py +98 -0
- scipy/fft/tests/test_basic.py +504 -0
- scipy/fft/tests/test_fftlog.py +215 -0
- scipy/fft/tests/test_helper.py +558 -0
- scipy/fft/tests/test_multithreading.py +84 -0
- scipy/fft/tests/test_real_transforms.py +247 -0
- scipy/fftpack/__init__.py +103 -0
- scipy/fftpack/_basic.py +428 -0
- scipy/fftpack/_helper.py +115 -0
- scipy/fftpack/_pseudo_diffs.py +554 -0
- scipy/fftpack/_realtransforms.py +598 -0
- scipy/fftpack/basic.py +20 -0
- scipy/fftpack/convolve.cp312-win_arm64.lib +0 -0
- scipy/fftpack/convolve.cp312-win_arm64.pyd +0 -0
- scipy/fftpack/helper.py +19 -0
- scipy/fftpack/pseudo_diffs.py +22 -0
- scipy/fftpack/realtransforms.py +19 -0
- scipy/fftpack/tests/__init__.py +0 -0
- scipy/fftpack/tests/fftw_double_ref.npz +0 -0
- scipy/fftpack/tests/fftw_longdouble_ref.npz +0 -0
- scipy/fftpack/tests/fftw_single_ref.npz +0 -0
- scipy/fftpack/tests/test.npz +0 -0
- scipy/fftpack/tests/test_basic.py +877 -0
- scipy/fftpack/tests/test_helper.py +54 -0
- scipy/fftpack/tests/test_import.py +33 -0
- scipy/fftpack/tests/test_pseudo_diffs.py +388 -0
- scipy/fftpack/tests/test_real_transforms.py +836 -0
- scipy/integrate/__init__.py +122 -0
- scipy/integrate/_bvp.py +1160 -0
- scipy/integrate/_cubature.py +729 -0
- scipy/integrate/_dop.cp312-win_arm64.lib +0 -0
- scipy/integrate/_dop.cp312-win_arm64.pyd +0 -0
- scipy/integrate/_ivp/__init__.py +8 -0
- scipy/integrate/_ivp/base.py +290 -0
- scipy/integrate/_ivp/bdf.py +478 -0
- scipy/integrate/_ivp/common.py +451 -0
- scipy/integrate/_ivp/dop853_coefficients.py +193 -0
- scipy/integrate/_ivp/ivp.py +755 -0
- scipy/integrate/_ivp/lsoda.py +224 -0
- scipy/integrate/_ivp/radau.py +572 -0
- scipy/integrate/_ivp/rk.py +601 -0
- scipy/integrate/_ivp/tests/__init__.py +0 -0
- scipy/integrate/_ivp/tests/test_ivp.py +1287 -0
- scipy/integrate/_ivp/tests/test_rk.py +37 -0
- scipy/integrate/_lebedev.py +5450 -0
- scipy/integrate/_lsoda.cp312-win_arm64.lib +0 -0
- scipy/integrate/_lsoda.cp312-win_arm64.pyd +0 -0
- scipy/integrate/_ode.py +1395 -0
- scipy/integrate/_odepack.cp312-win_arm64.lib +0 -0
- scipy/integrate/_odepack.cp312-win_arm64.pyd +0 -0
- scipy/integrate/_odepack_py.py +273 -0
- scipy/integrate/_quad_vec.py +674 -0
- scipy/integrate/_quadpack.cp312-win_arm64.lib +0 -0
- scipy/integrate/_quadpack.cp312-win_arm64.pyd +0 -0
- scipy/integrate/_quadpack_py.py +1283 -0
- scipy/integrate/_quadrature.py +1336 -0
- scipy/integrate/_rules/__init__.py +12 -0
- scipy/integrate/_rules/_base.py +518 -0
- scipy/integrate/_rules/_gauss_kronrod.py +202 -0
- scipy/integrate/_rules/_gauss_legendre.py +62 -0
- scipy/integrate/_rules/_genz_malik.py +210 -0
- scipy/integrate/_tanhsinh.py +1385 -0
- scipy/integrate/_test_multivariate.cp312-win_arm64.lib +0 -0
- scipy/integrate/_test_multivariate.cp312-win_arm64.pyd +0 -0
- scipy/integrate/_test_odeint_banded.cp312-win_arm64.lib +0 -0
- scipy/integrate/_test_odeint_banded.cp312-win_arm64.pyd +0 -0
- scipy/integrate/_vode.cp312-win_arm64.lib +0 -0
- scipy/integrate/_vode.cp312-win_arm64.pyd +0 -0
- scipy/integrate/dop.py +15 -0
- scipy/integrate/lsoda.py +15 -0
- scipy/integrate/odepack.py +17 -0
- scipy/integrate/quadpack.py +23 -0
- scipy/integrate/tests/__init__.py +0 -0
- scipy/integrate/tests/test__quad_vec.py +211 -0
- scipy/integrate/tests/test_banded_ode_solvers.py +305 -0
- scipy/integrate/tests/test_bvp.py +714 -0
- scipy/integrate/tests/test_cubature.py +1375 -0
- scipy/integrate/tests/test_integrate.py +840 -0
- scipy/integrate/tests/test_odeint_jac.py +74 -0
- scipy/integrate/tests/test_quadpack.py +680 -0
- scipy/integrate/tests/test_quadrature.py +730 -0
- scipy/integrate/tests/test_tanhsinh.py +1171 -0
- scipy/integrate/vode.py +15 -0
- scipy/interpolate/__init__.py +228 -0
- scipy/interpolate/_bary_rational.py +715 -0
- scipy/interpolate/_bsplines.py +2469 -0
- scipy/interpolate/_cubic.py +973 -0
- scipy/interpolate/_dfitpack.cp312-win_arm64.lib +0 -0
- scipy/interpolate/_dfitpack.cp312-win_arm64.pyd +0 -0
- scipy/interpolate/_dierckx.cp312-win_arm64.lib +0 -0
- scipy/interpolate/_dierckx.cp312-win_arm64.pyd +0 -0
- scipy/interpolate/_fitpack.cp312-win_arm64.lib +0 -0
- scipy/interpolate/_fitpack.cp312-win_arm64.pyd +0 -0
- scipy/interpolate/_fitpack2.py +2397 -0
- scipy/interpolate/_fitpack_impl.py +811 -0
- scipy/interpolate/_fitpack_py.py +898 -0
- scipy/interpolate/_fitpack_repro.py +996 -0
- scipy/interpolate/_interpnd.cp312-win_arm64.lib +0 -0
- scipy/interpolate/_interpnd.cp312-win_arm64.pyd +0 -0
- scipy/interpolate/_interpolate.py +2266 -0
- scipy/interpolate/_ndbspline.py +415 -0
- scipy/interpolate/_ndgriddata.py +329 -0
- scipy/interpolate/_pade.py +67 -0
- scipy/interpolate/_polyint.py +1025 -0
- scipy/interpolate/_ppoly.cp312-win_arm64.lib +0 -0
- scipy/interpolate/_ppoly.cp312-win_arm64.pyd +0 -0
- scipy/interpolate/_rbf.py +290 -0
- scipy/interpolate/_rbfinterp.py +550 -0
- scipy/interpolate/_rbfinterp_pythran.cp312-win_arm64.lib +0 -0
- scipy/interpolate/_rbfinterp_pythran.cp312-win_arm64.pyd +0 -0
- scipy/interpolate/_rgi.py +764 -0
- scipy/interpolate/_rgi_cython.cp312-win_arm64.lib +0 -0
- scipy/interpolate/_rgi_cython.cp312-win_arm64.pyd +0 -0
- scipy/interpolate/dfitpack.py +24 -0
- scipy/interpolate/fitpack.py +31 -0
- scipy/interpolate/fitpack2.py +29 -0
- scipy/interpolate/interpnd.py +24 -0
- scipy/interpolate/interpolate.py +30 -0
- scipy/interpolate/ndgriddata.py +23 -0
- scipy/interpolate/polyint.py +24 -0
- scipy/interpolate/rbf.py +18 -0
- scipy/interpolate/tests/__init__.py +0 -0
- scipy/interpolate/tests/data/bug-1310.npz +0 -0
- scipy/interpolate/tests/data/estimate_gradients_hang.npy +0 -0
- scipy/interpolate/tests/data/gcvspl.npz +0 -0
- scipy/interpolate/tests/test_bary_rational.py +368 -0
- scipy/interpolate/tests/test_bsplines.py +3754 -0
- scipy/interpolate/tests/test_fitpack.py +519 -0
- scipy/interpolate/tests/test_fitpack2.py +1431 -0
- scipy/interpolate/tests/test_gil.py +64 -0
- scipy/interpolate/tests/test_interpnd.py +452 -0
- scipy/interpolate/tests/test_interpolate.py +2630 -0
- scipy/interpolate/tests/test_ndgriddata.py +308 -0
- scipy/interpolate/tests/test_pade.py +107 -0
- scipy/interpolate/tests/test_polyint.py +972 -0
- scipy/interpolate/tests/test_rbf.py +246 -0
- scipy/interpolate/tests/test_rbfinterp.py +534 -0
- scipy/interpolate/tests/test_rgi.py +1151 -0
- scipy/io/__init__.py +116 -0
- scipy/io/_fast_matrix_market/__init__.py +600 -0
- scipy/io/_fast_matrix_market/_fmm_core.cp312-win_arm64.lib +0 -0
- scipy/io/_fast_matrix_market/_fmm_core.cp312-win_arm64.pyd +0 -0
- scipy/io/_fortran.py +354 -0
- scipy/io/_harwell_boeing/__init__.py +7 -0
- scipy/io/_harwell_boeing/_fortran_format_parser.py +316 -0
- scipy/io/_harwell_boeing/hb.py +571 -0
- scipy/io/_harwell_boeing/tests/__init__.py +0 -0
- scipy/io/_harwell_boeing/tests/test_fortran_format.py +74 -0
- scipy/io/_harwell_boeing/tests/test_hb.py +70 -0
- scipy/io/_idl.py +917 -0
- scipy/io/_mmio.py +968 -0
- scipy/io/_netcdf.py +1104 -0
- scipy/io/_test_fortran.cp312-win_arm64.lib +0 -0
- scipy/io/_test_fortran.cp312-win_arm64.pyd +0 -0
- scipy/io/arff/__init__.py +28 -0
- scipy/io/arff/_arffread.py +873 -0
- scipy/io/arff/arffread.py +19 -0
- scipy/io/arff/tests/__init__.py +0 -0
- scipy/io/arff/tests/data/iris.arff +225 -0
- scipy/io/arff/tests/data/missing.arff +8 -0
- scipy/io/arff/tests/data/nodata.arff +11 -0
- scipy/io/arff/tests/data/quoted_nominal.arff +13 -0
- scipy/io/arff/tests/data/quoted_nominal_spaces.arff +13 -0
- scipy/io/arff/tests/data/test1.arff +10 -0
- scipy/io/arff/tests/data/test10.arff +8 -0
- scipy/io/arff/tests/data/test11.arff +11 -0
- scipy/io/arff/tests/data/test2.arff +15 -0
- scipy/io/arff/tests/data/test3.arff +6 -0
- scipy/io/arff/tests/data/test4.arff +11 -0
- scipy/io/arff/tests/data/test5.arff +26 -0
- scipy/io/arff/tests/data/test6.arff +12 -0
- scipy/io/arff/tests/data/test7.arff +15 -0
- scipy/io/arff/tests/data/test8.arff +12 -0
- scipy/io/arff/tests/data/test9.arff +14 -0
- scipy/io/arff/tests/test_arffread.py +421 -0
- scipy/io/harwell_boeing.py +17 -0
- scipy/io/idl.py +17 -0
- scipy/io/matlab/__init__.py +66 -0
- scipy/io/matlab/_byteordercodes.py +75 -0
- scipy/io/matlab/_mio.py +375 -0
- scipy/io/matlab/_mio4.py +632 -0
- scipy/io/matlab/_mio5.py +901 -0
- scipy/io/matlab/_mio5_params.py +281 -0
- scipy/io/matlab/_mio5_utils.cp312-win_arm64.lib +0 -0
- scipy/io/matlab/_mio5_utils.cp312-win_arm64.pyd +0 -0
- scipy/io/matlab/_mio_utils.cp312-win_arm64.lib +0 -0
- scipy/io/matlab/_mio_utils.cp312-win_arm64.pyd +0 -0
- scipy/io/matlab/_miobase.py +435 -0
- scipy/io/matlab/_streams.cp312-win_arm64.lib +0 -0
- scipy/io/matlab/_streams.cp312-win_arm64.pyd +0 -0
- scipy/io/matlab/byteordercodes.py +17 -0
- scipy/io/matlab/mio.py +16 -0
- scipy/io/matlab/mio4.py +17 -0
- scipy/io/matlab/mio5.py +19 -0
- scipy/io/matlab/mio5_params.py +18 -0
- scipy/io/matlab/mio5_utils.py +17 -0
- scipy/io/matlab/mio_utils.py +17 -0
- scipy/io/matlab/miobase.py +16 -0
- scipy/io/matlab/streams.py +16 -0
- scipy/io/matlab/tests/__init__.py +0 -0
- scipy/io/matlab/tests/data/bad_miuint32.mat +0 -0
- scipy/io/matlab/tests/data/bad_miutf8_array_name.mat +0 -0
- scipy/io/matlab/tests/data/big_endian.mat +0 -0
- scipy/io/matlab/tests/data/broken_utf8.mat +0 -0
- scipy/io/matlab/tests/data/corrupted_zlib_checksum.mat +0 -0
- scipy/io/matlab/tests/data/corrupted_zlib_data.mat +0 -0
- scipy/io/matlab/tests/data/debigged_m4.mat +0 -0
- scipy/io/matlab/tests/data/japanese_utf8.txt +5 -0
- scipy/io/matlab/tests/data/little_endian.mat +0 -0
- scipy/io/matlab/tests/data/logical_sparse.mat +0 -0
- scipy/io/matlab/tests/data/malformed1.mat +0 -0
- scipy/io/matlab/tests/data/miuint32_for_miint32.mat +0 -0
- scipy/io/matlab/tests/data/miutf8_array_name.mat +0 -0
- scipy/io/matlab/tests/data/nasty_duplicate_fieldnames.mat +0 -0
- scipy/io/matlab/tests/data/one_by_zero_char.mat +0 -0
- scipy/io/matlab/tests/data/parabola.mat +0 -0
- scipy/io/matlab/tests/data/single_empty_string.mat +0 -0
- scipy/io/matlab/tests/data/some_functions.mat +0 -0
- scipy/io/matlab/tests/data/sqr.mat +0 -0
- scipy/io/matlab/tests/data/test3dmatrix_6.1_SOL2.mat +0 -0
- scipy/io/matlab/tests/data/test3dmatrix_6.5.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/test3dmatrix_7.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/test3dmatrix_7.4_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/test_empty_struct.mat +0 -0
- scipy/io/matlab/tests/data/test_mat4_le_floats.mat +0 -0
- scipy/io/matlab/tests/data/test_skip_variable.mat +0 -0
- scipy/io/matlab/tests/data/testbool_8_WIN64.mat +0 -0
- scipy/io/matlab/tests/data/testcell_6.1_SOL2.mat +0 -0
- scipy/io/matlab/tests/data/testcell_6.5.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testcell_7.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testcell_7.4_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testcellnest_6.1_SOL2.mat +0 -0
- scipy/io/matlab/tests/data/testcellnest_6.5.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testcellnest_7.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testcellnest_7.4_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testcomplex_4.2c_SOL2.mat +0 -0
- scipy/io/matlab/tests/data/testcomplex_6.1_SOL2.mat +0 -0
- scipy/io/matlab/tests/data/testcomplex_6.5.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testcomplex_7.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testcomplex_7.4_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testdouble_4.2c_SOL2.mat +0 -0
- scipy/io/matlab/tests/data/testdouble_6.1_SOL2.mat +0 -0
- scipy/io/matlab/tests/data/testdouble_6.5.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testdouble_7.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testdouble_7.4_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testemptycell_5.3_SOL2.mat +0 -0
- scipy/io/matlab/tests/data/testemptycell_6.5.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testemptycell_7.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testemptycell_7.4_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testfunc_7.4_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testhdf5_7.4_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testmatrix_4.2c_SOL2.mat +0 -0
- scipy/io/matlab/tests/data/testmatrix_6.1_SOL2.mat +0 -0
- scipy/io/matlab/tests/data/testmatrix_6.5.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testmatrix_7.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testmatrix_7.4_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testminus_4.2c_SOL2.mat +0 -0
- scipy/io/matlab/tests/data/testminus_6.1_SOL2.mat +0 -0
- scipy/io/matlab/tests/data/testminus_6.5.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testminus_7.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testminus_7.4_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testmulti_4.2c_SOL2.mat +0 -0
- scipy/io/matlab/tests/data/testmulti_7.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testmulti_7.4_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testobject_6.1_SOL2.mat +0 -0
- scipy/io/matlab/tests/data/testobject_6.5.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testobject_7.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testobject_7.4_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testonechar_4.2c_SOL2.mat +0 -0
- scipy/io/matlab/tests/data/testonechar_6.1_SOL2.mat +0 -0
- scipy/io/matlab/tests/data/testonechar_6.5.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testonechar_7.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testonechar_7.4_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testscalarcell_7.4_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testsimplecell.mat +0 -0
- scipy/io/matlab/tests/data/testsparse_4.2c_SOL2.mat +0 -0
- scipy/io/matlab/tests/data/testsparse_6.1_SOL2.mat +0 -0
- scipy/io/matlab/tests/data/testsparse_6.5.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testsparse_7.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testsparse_7.4_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testsparsecomplex_4.2c_SOL2.mat +0 -0
- scipy/io/matlab/tests/data/testsparsecomplex_6.1_SOL2.mat +0 -0
- scipy/io/matlab/tests/data/testsparsecomplex_6.5.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testsparsecomplex_7.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testsparsecomplex_7.4_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testsparsefloat_7.4_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/teststring_4.2c_SOL2.mat +0 -0
- scipy/io/matlab/tests/data/teststring_6.1_SOL2.mat +0 -0
- scipy/io/matlab/tests/data/teststring_6.5.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/teststring_7.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/teststring_7.4_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/teststringarray_4.2c_SOL2.mat +0 -0
- scipy/io/matlab/tests/data/teststringarray_6.1_SOL2.mat +0 -0
- scipy/io/matlab/tests/data/teststringarray_6.5.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/teststringarray_7.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/teststringarray_7.4_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/teststruct_6.1_SOL2.mat +0 -0
- scipy/io/matlab/tests/data/teststruct_6.5.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/teststruct_7.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/teststruct_7.4_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/teststructarr_6.1_SOL2.mat +0 -0
- scipy/io/matlab/tests/data/teststructarr_6.5.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/teststructarr_7.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/teststructarr_7.4_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/teststructnest_6.1_SOL2.mat +0 -0
- scipy/io/matlab/tests/data/teststructnest_6.5.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/teststructnest_7.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/teststructnest_7.4_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testunicode_7.1_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testunicode_7.4_GLNX86.mat +0 -0
- scipy/io/matlab/tests/data/testvec_4_GLNX86.mat +0 -0
- scipy/io/matlab/tests/test_byteordercodes.py +29 -0
- scipy/io/matlab/tests/test_mio.py +1399 -0
- scipy/io/matlab/tests/test_mio5_utils.py +179 -0
- scipy/io/matlab/tests/test_mio_funcs.py +51 -0
- scipy/io/matlab/tests/test_mio_utils.py +45 -0
- scipy/io/matlab/tests/test_miobase.py +32 -0
- scipy/io/matlab/tests/test_pathological.py +33 -0
- scipy/io/matlab/tests/test_streams.py +241 -0
- scipy/io/mmio.py +17 -0
- scipy/io/netcdf.py +17 -0
- scipy/io/tests/__init__.py +0 -0
- scipy/io/tests/data/Transparent Busy.ani +0 -0
- scipy/io/tests/data/array_float32_1d.sav +0 -0
- scipy/io/tests/data/array_float32_2d.sav +0 -0
- scipy/io/tests/data/array_float32_3d.sav +0 -0
- scipy/io/tests/data/array_float32_4d.sav +0 -0
- scipy/io/tests/data/array_float32_5d.sav +0 -0
- scipy/io/tests/data/array_float32_6d.sav +0 -0
- scipy/io/tests/data/array_float32_7d.sav +0 -0
- scipy/io/tests/data/array_float32_8d.sav +0 -0
- scipy/io/tests/data/array_float32_pointer_1d.sav +0 -0
- scipy/io/tests/data/array_float32_pointer_2d.sav +0 -0
- scipy/io/tests/data/array_float32_pointer_3d.sav +0 -0
- scipy/io/tests/data/array_float32_pointer_4d.sav +0 -0
- scipy/io/tests/data/array_float32_pointer_5d.sav +0 -0
- scipy/io/tests/data/array_float32_pointer_6d.sav +0 -0
- scipy/io/tests/data/array_float32_pointer_7d.sav +0 -0
- scipy/io/tests/data/array_float32_pointer_8d.sav +0 -0
- scipy/io/tests/data/example_1.nc +0 -0
- scipy/io/tests/data/example_2.nc +0 -0
- scipy/io/tests/data/example_3_maskedvals.nc +0 -0
- scipy/io/tests/data/fortran-3x3d-2i.dat +0 -0
- scipy/io/tests/data/fortran-mixed.dat +0 -0
- scipy/io/tests/data/fortran-sf8-11x1x10.dat +0 -0
- scipy/io/tests/data/fortran-sf8-15x10x22.dat +0 -0
- scipy/io/tests/data/fortran-sf8-1x1x1.dat +0 -0
- scipy/io/tests/data/fortran-sf8-1x1x5.dat +0 -0
- scipy/io/tests/data/fortran-sf8-1x1x7.dat +0 -0
- scipy/io/tests/data/fortran-sf8-1x3x5.dat +0 -0
- scipy/io/tests/data/fortran-si4-11x1x10.dat +0 -0
- scipy/io/tests/data/fortran-si4-15x10x22.dat +0 -0
- scipy/io/tests/data/fortran-si4-1x1x1.dat +0 -0
- scipy/io/tests/data/fortran-si4-1x1x5.dat +0 -0
- scipy/io/tests/data/fortran-si4-1x1x7.dat +0 -0
- scipy/io/tests/data/fortran-si4-1x3x5.dat +0 -0
- scipy/io/tests/data/invalid_pointer.sav +0 -0
- scipy/io/tests/data/null_pointer.sav +0 -0
- scipy/io/tests/data/scalar_byte.sav +0 -0
- scipy/io/tests/data/scalar_byte_descr.sav +0 -0
- scipy/io/tests/data/scalar_complex32.sav +0 -0
- scipy/io/tests/data/scalar_complex64.sav +0 -0
- scipy/io/tests/data/scalar_float32.sav +0 -0
- scipy/io/tests/data/scalar_float64.sav +0 -0
- scipy/io/tests/data/scalar_heap_pointer.sav +0 -0
- scipy/io/tests/data/scalar_int16.sav +0 -0
- scipy/io/tests/data/scalar_int32.sav +0 -0
- scipy/io/tests/data/scalar_int64.sav +0 -0
- scipy/io/tests/data/scalar_string.sav +0 -0
- scipy/io/tests/data/scalar_uint16.sav +0 -0
- scipy/io/tests/data/scalar_uint32.sav +0 -0
- scipy/io/tests/data/scalar_uint64.sav +0 -0
- scipy/io/tests/data/struct_arrays.sav +0 -0
- scipy/io/tests/data/struct_arrays_byte_idl80.sav +0 -0
- scipy/io/tests/data/struct_arrays_replicated.sav +0 -0
- scipy/io/tests/data/struct_arrays_replicated_3d.sav +0 -0
- scipy/io/tests/data/struct_inherit.sav +0 -0
- scipy/io/tests/data/struct_pointer_arrays.sav +0 -0
- scipy/io/tests/data/struct_pointer_arrays_replicated.sav +0 -0
- scipy/io/tests/data/struct_pointer_arrays_replicated_3d.sav +0 -0
- scipy/io/tests/data/struct_pointers.sav +0 -0
- scipy/io/tests/data/struct_pointers_replicated.sav +0 -0
- scipy/io/tests/data/struct_pointers_replicated_3d.sav +0 -0
- scipy/io/tests/data/struct_scalars.sav +0 -0
- scipy/io/tests/data/struct_scalars_replicated.sav +0 -0
- scipy/io/tests/data/struct_scalars_replicated_3d.sav +0 -0
- scipy/io/tests/data/test-1234Hz-le-1ch-10S-20bit-extra.wav +0 -0
- scipy/io/tests/data/test-44100Hz-2ch-32bit-float-be.wav +0 -0
- scipy/io/tests/data/test-44100Hz-2ch-32bit-float-le.wav +0 -0
- scipy/io/tests/data/test-44100Hz-be-1ch-4bytes.wav +0 -0
- scipy/io/tests/data/test-44100Hz-le-1ch-4bytes-early-eof-no-data.wav +0 -0
- scipy/io/tests/data/test-44100Hz-le-1ch-4bytes-early-eof.wav +0 -0
- scipy/io/tests/data/test-44100Hz-le-1ch-4bytes-incomplete-chunk.wav +0 -0
- scipy/io/tests/data/test-44100Hz-le-1ch-4bytes-rf64.wav +0 -0
- scipy/io/tests/data/test-44100Hz-le-1ch-4bytes.wav +0 -0
- scipy/io/tests/data/test-48000Hz-2ch-64bit-float-le-wavex.wav +0 -0
- scipy/io/tests/data/test-8000Hz-be-3ch-5S-24bit.wav +0 -0
- scipy/io/tests/data/test-8000Hz-le-1ch-1byte-ulaw.wav +0 -0
- scipy/io/tests/data/test-8000Hz-le-2ch-1byteu.wav +0 -0
- scipy/io/tests/data/test-8000Hz-le-3ch-5S-24bit-inconsistent.wav +0 -0
- scipy/io/tests/data/test-8000Hz-le-3ch-5S-24bit-rf64.wav +0 -0
- scipy/io/tests/data/test-8000Hz-le-3ch-5S-24bit.wav +0 -0
- scipy/io/tests/data/test-8000Hz-le-3ch-5S-36bit.wav +0 -0
- scipy/io/tests/data/test-8000Hz-le-3ch-5S-45bit.wav +0 -0
- scipy/io/tests/data/test-8000Hz-le-3ch-5S-53bit.wav +0 -0
- scipy/io/tests/data/test-8000Hz-le-3ch-5S-64bit.wav +0 -0
- scipy/io/tests/data/test-8000Hz-le-4ch-9S-12bit.wav +0 -0
- scipy/io/tests/data/test-8000Hz-le-5ch-9S-5bit.wav +0 -0
- scipy/io/tests/data/various_compressed.sav +0 -0
- scipy/io/tests/test_fortran.py +264 -0
- scipy/io/tests/test_idl.py +483 -0
- scipy/io/tests/test_mmio.py +831 -0
- scipy/io/tests/test_netcdf.py +550 -0
- scipy/io/tests/test_paths.py +93 -0
- scipy/io/tests/test_wavfile.py +501 -0
- scipy/io/wavfile.py +938 -0
- scipy/linalg/__init__.pxd +1 -0
- scipy/linalg/__init__.py +236 -0
- scipy/linalg/_basic.py +2146 -0
- scipy/linalg/_blas_subroutines.h +164 -0
- scipy/linalg/_cythonized_array_utils.cp312-win_arm64.lib +0 -0
- scipy/linalg/_cythonized_array_utils.cp312-win_arm64.pyd +0 -0
- scipy/linalg/_cythonized_array_utils.pxd +40 -0
- scipy/linalg/_cythonized_array_utils.pyi +16 -0
- scipy/linalg/_decomp.py +1645 -0
- scipy/linalg/_decomp_cholesky.py +413 -0
- scipy/linalg/_decomp_cossin.py +236 -0
- scipy/linalg/_decomp_interpolative.cp312-win_arm64.lib +0 -0
- scipy/linalg/_decomp_interpolative.cp312-win_arm64.pyd +0 -0
- scipy/linalg/_decomp_ldl.py +356 -0
- scipy/linalg/_decomp_lu.py +401 -0
- scipy/linalg/_decomp_lu_cython.cp312-win_arm64.lib +0 -0
- scipy/linalg/_decomp_lu_cython.cp312-win_arm64.pyd +0 -0
- scipy/linalg/_decomp_lu_cython.pyi +6 -0
- scipy/linalg/_decomp_polar.py +113 -0
- scipy/linalg/_decomp_qr.py +494 -0
- scipy/linalg/_decomp_qz.py +452 -0
- scipy/linalg/_decomp_schur.py +336 -0
- scipy/linalg/_decomp_svd.py +545 -0
- scipy/linalg/_decomp_update.cp312-win_arm64.lib +0 -0
- scipy/linalg/_decomp_update.cp312-win_arm64.pyd +0 -0
- scipy/linalg/_expm_frechet.py +417 -0
- scipy/linalg/_fblas.cp312-win_arm64.lib +0 -0
- scipy/linalg/_fblas.cp312-win_arm64.pyd +0 -0
- scipy/linalg/_flapack.cp312-win_arm64.lib +0 -0
- scipy/linalg/_flapack.cp312-win_arm64.pyd +0 -0
- scipy/linalg/_lapack_subroutines.h +1521 -0
- scipy/linalg/_linalg_pythran.cp312-win_arm64.lib +0 -0
- scipy/linalg/_linalg_pythran.cp312-win_arm64.pyd +0 -0
- scipy/linalg/_matfuncs.py +1050 -0
- scipy/linalg/_matfuncs_expm.cp312-win_arm64.lib +0 -0
- scipy/linalg/_matfuncs_expm.cp312-win_arm64.pyd +0 -0
- scipy/linalg/_matfuncs_expm.pyi +6 -0
- scipy/linalg/_matfuncs_inv_ssq.py +886 -0
- scipy/linalg/_matfuncs_schur_sqrtm.cp312-win_arm64.lib +0 -0
- scipy/linalg/_matfuncs_schur_sqrtm.cp312-win_arm64.pyd +0 -0
- scipy/linalg/_matfuncs_sqrtm.py +107 -0
- scipy/linalg/_matfuncs_sqrtm_triu.cp312-win_arm64.lib +0 -0
- scipy/linalg/_matfuncs_sqrtm_triu.cp312-win_arm64.pyd +0 -0
- scipy/linalg/_misc.py +191 -0
- scipy/linalg/_procrustes.py +113 -0
- scipy/linalg/_sketches.py +189 -0
- scipy/linalg/_solve_toeplitz.cp312-win_arm64.lib +0 -0
- scipy/linalg/_solve_toeplitz.cp312-win_arm64.pyd +0 -0
- scipy/linalg/_solvers.py +862 -0
- scipy/linalg/_special_matrices.py +1322 -0
- scipy/linalg/_testutils.py +65 -0
- scipy/linalg/basic.py +23 -0
- scipy/linalg/blas.py +495 -0
- scipy/linalg/cython_blas.cp312-win_arm64.lib +0 -0
- scipy/linalg/cython_blas.cp312-win_arm64.pyd +0 -0
- scipy/linalg/cython_blas.pxd +169 -0
- scipy/linalg/cython_blas.pyx +1432 -0
- scipy/linalg/cython_lapack.cp312-win_arm64.lib +0 -0
- scipy/linalg/cython_lapack.cp312-win_arm64.pyd +0 -0
- scipy/linalg/cython_lapack.pxd +1528 -0
- scipy/linalg/cython_lapack.pyx +12045 -0
- scipy/linalg/decomp.py +23 -0
- scipy/linalg/decomp_cholesky.py +21 -0
- scipy/linalg/decomp_lu.py +21 -0
- scipy/linalg/decomp_qr.py +20 -0
- scipy/linalg/decomp_schur.py +21 -0
- scipy/linalg/decomp_svd.py +21 -0
- scipy/linalg/interpolative.py +989 -0
- scipy/linalg/lapack.py +1081 -0
- scipy/linalg/matfuncs.py +23 -0
- scipy/linalg/misc.py +21 -0
- scipy/linalg/special_matrices.py +22 -0
- scipy/linalg/tests/__init__.py +0 -0
- scipy/linalg/tests/_cython_examples/extending.pyx +23 -0
- scipy/linalg/tests/_cython_examples/meson.build +34 -0
- scipy/linalg/tests/data/carex_15_data.npz +0 -0
- scipy/linalg/tests/data/carex_18_data.npz +0 -0
- scipy/linalg/tests/data/carex_19_data.npz +0 -0
- scipy/linalg/tests/data/carex_20_data.npz +0 -0
- scipy/linalg/tests/data/carex_6_data.npz +0 -0
- scipy/linalg/tests/data/gendare_20170120_data.npz +0 -0
- scipy/linalg/tests/test_basic.py +2074 -0
- scipy/linalg/tests/test_batch.py +588 -0
- scipy/linalg/tests/test_blas.py +1127 -0
- scipy/linalg/tests/test_cython_blas.py +118 -0
- scipy/linalg/tests/test_cython_lapack.py +22 -0
- scipy/linalg/tests/test_cythonized_array_utils.py +130 -0
- scipy/linalg/tests/test_decomp.py +3189 -0
- scipy/linalg/tests/test_decomp_cholesky.py +268 -0
- scipy/linalg/tests/test_decomp_cossin.py +314 -0
- scipy/linalg/tests/test_decomp_ldl.py +137 -0
- scipy/linalg/tests/test_decomp_lu.py +308 -0
- scipy/linalg/tests/test_decomp_polar.py +110 -0
- scipy/linalg/tests/test_decomp_update.py +1701 -0
- scipy/linalg/tests/test_extending.py +46 -0
- scipy/linalg/tests/test_fblas.py +607 -0
- scipy/linalg/tests/test_interpolative.py +232 -0
- scipy/linalg/tests/test_lapack.py +3620 -0
- scipy/linalg/tests/test_matfuncs.py +1125 -0
- scipy/linalg/tests/test_matmul_toeplitz.py +136 -0
- scipy/linalg/tests/test_procrustes.py +214 -0
- scipy/linalg/tests/test_sketches.py +118 -0
- scipy/linalg/tests/test_solve_toeplitz.py +150 -0
- scipy/linalg/tests/test_solvers.py +844 -0
- scipy/linalg/tests/test_special_matrices.py +636 -0
- scipy/misc/__init__.py +6 -0
- scipy/misc/common.py +6 -0
- scipy/misc/doccer.py +6 -0
- scipy/ndimage/__init__.py +174 -0
- scipy/ndimage/_ctest.cp312-win_arm64.lib +0 -0
- scipy/ndimage/_ctest.cp312-win_arm64.pyd +0 -0
- scipy/ndimage/_cytest.cp312-win_arm64.lib +0 -0
- scipy/ndimage/_cytest.cp312-win_arm64.pyd +0 -0
- scipy/ndimage/_delegators.py +303 -0
- scipy/ndimage/_filters.py +2422 -0
- scipy/ndimage/_fourier.py +306 -0
- scipy/ndimage/_interpolation.py +1033 -0
- scipy/ndimage/_measurements.py +1689 -0
- scipy/ndimage/_morphology.py +2634 -0
- scipy/ndimage/_nd_image.cp312-win_arm64.lib +0 -0
- scipy/ndimage/_nd_image.cp312-win_arm64.pyd +0 -0
- scipy/ndimage/_ndimage_api.py +16 -0
- scipy/ndimage/_ni_docstrings.py +214 -0
- scipy/ndimage/_ni_label.cp312-win_arm64.lib +0 -0
- scipy/ndimage/_ni_label.cp312-win_arm64.pyd +0 -0
- scipy/ndimage/_ni_support.py +139 -0
- scipy/ndimage/_rank_filter_1d.cp312-win_arm64.lib +0 -0
- scipy/ndimage/_rank_filter_1d.cp312-win_arm64.pyd +0 -0
- scipy/ndimage/_support_alternative_backends.py +84 -0
- scipy/ndimage/filters.py +27 -0
- scipy/ndimage/fourier.py +21 -0
- scipy/ndimage/interpolation.py +22 -0
- scipy/ndimage/measurements.py +24 -0
- scipy/ndimage/morphology.py +27 -0
- scipy/ndimage/tests/__init__.py +12 -0
- scipy/ndimage/tests/data/label_inputs.txt +21 -0
- scipy/ndimage/tests/data/label_results.txt +294 -0
- scipy/ndimage/tests/data/label_strels.txt +42 -0
- scipy/ndimage/tests/dots.png +0 -0
- scipy/ndimage/tests/test_c_api.py +102 -0
- scipy/ndimage/tests/test_datatypes.py +67 -0
- scipy/ndimage/tests/test_filters.py +3083 -0
- scipy/ndimage/tests/test_fourier.py +187 -0
- scipy/ndimage/tests/test_interpolation.py +1491 -0
- scipy/ndimage/tests/test_measurements.py +1592 -0
- scipy/ndimage/tests/test_morphology.py +2950 -0
- scipy/ndimage/tests/test_ni_support.py +78 -0
- scipy/ndimage/tests/test_splines.py +70 -0
- scipy/odr/__init__.py +131 -0
- scipy/odr/__odrpack.cp312-win_arm64.lib +0 -0
- scipy/odr/__odrpack.cp312-win_arm64.pyd +0 -0
- scipy/odr/_add_newdocs.py +34 -0
- scipy/odr/_models.py +315 -0
- scipy/odr/_odrpack.py +1154 -0
- scipy/odr/models.py +20 -0
- scipy/odr/odrpack.py +21 -0
- scipy/odr/tests/__init__.py +0 -0
- scipy/odr/tests/test_odr.py +607 -0
- scipy/optimize/__init__.pxd +1 -0
- scipy/optimize/__init__.py +460 -0
- scipy/optimize/_basinhopping.py +741 -0
- scipy/optimize/_bglu_dense.cp312-win_arm64.lib +0 -0
- scipy/optimize/_bglu_dense.cp312-win_arm64.pyd +0 -0
- scipy/optimize/_bracket.py +706 -0
- scipy/optimize/_chandrupatla.py +551 -0
- scipy/optimize/_cobyla_py.py +297 -0
- scipy/optimize/_cobyqa_py.py +72 -0
- scipy/optimize/_constraints.py +598 -0
- scipy/optimize/_dcsrch.py +728 -0
- scipy/optimize/_differentiable_functions.py +835 -0
- scipy/optimize/_differentialevolution.py +1970 -0
- scipy/optimize/_direct.cp312-win_arm64.lib +0 -0
- scipy/optimize/_direct.cp312-win_arm64.pyd +0 -0
- scipy/optimize/_direct_py.py +280 -0
- scipy/optimize/_dual_annealing.py +732 -0
- scipy/optimize/_elementwise.py +798 -0
- scipy/optimize/_group_columns.cp312-win_arm64.lib +0 -0
- scipy/optimize/_group_columns.cp312-win_arm64.pyd +0 -0
- scipy/optimize/_hessian_update_strategy.py +479 -0
- scipy/optimize/_highspy/__init__.py +0 -0
- scipy/optimize/_highspy/_core.cp312-win_arm64.lib +0 -0
- scipy/optimize/_highspy/_core.cp312-win_arm64.pyd +0 -0
- scipy/optimize/_highspy/_highs_options.cp312-win_arm64.lib +0 -0
- scipy/optimize/_highspy/_highs_options.cp312-win_arm64.pyd +0 -0
- scipy/optimize/_highspy/_highs_wrapper.py +338 -0
- scipy/optimize/_isotonic.py +157 -0
- scipy/optimize/_lbfgsb.cp312-win_arm64.lib +0 -0
- scipy/optimize/_lbfgsb.cp312-win_arm64.pyd +0 -0
- scipy/optimize/_lbfgsb_py.py +634 -0
- scipy/optimize/_linesearch.py +896 -0
- scipy/optimize/_linprog.py +733 -0
- scipy/optimize/_linprog_doc.py +1434 -0
- scipy/optimize/_linprog_highs.py +422 -0
- scipy/optimize/_linprog_ip.py +1141 -0
- scipy/optimize/_linprog_rs.py +572 -0
- scipy/optimize/_linprog_simplex.py +663 -0
- scipy/optimize/_linprog_util.py +1521 -0
- scipy/optimize/_lsap.cp312-win_arm64.lib +0 -0
- scipy/optimize/_lsap.cp312-win_arm64.pyd +0 -0
- scipy/optimize/_lsq/__init__.py +5 -0
- scipy/optimize/_lsq/bvls.py +183 -0
- scipy/optimize/_lsq/common.py +731 -0
- scipy/optimize/_lsq/dogbox.py +345 -0
- scipy/optimize/_lsq/givens_elimination.cp312-win_arm64.lib +0 -0
- scipy/optimize/_lsq/givens_elimination.cp312-win_arm64.pyd +0 -0
- scipy/optimize/_lsq/least_squares.py +1044 -0
- scipy/optimize/_lsq/lsq_linear.py +361 -0
- scipy/optimize/_lsq/trf.py +587 -0
- scipy/optimize/_lsq/trf_linear.py +249 -0
- scipy/optimize/_milp.py +394 -0
- scipy/optimize/_minimize.py +1199 -0
- scipy/optimize/_minpack.cp312-win_arm64.lib +0 -0
- scipy/optimize/_minpack.cp312-win_arm64.pyd +0 -0
- scipy/optimize/_minpack_py.py +1178 -0
- scipy/optimize/_moduleTNC.cp312-win_arm64.lib +0 -0
- scipy/optimize/_moduleTNC.cp312-win_arm64.pyd +0 -0
- scipy/optimize/_nnls.py +96 -0
- scipy/optimize/_nonlin.py +1634 -0
- scipy/optimize/_numdiff.py +963 -0
- scipy/optimize/_optimize.py +4169 -0
- scipy/optimize/_pava_pybind.cp312-win_arm64.lib +0 -0
- scipy/optimize/_pava_pybind.cp312-win_arm64.pyd +0 -0
- scipy/optimize/_qap.py +760 -0
- scipy/optimize/_remove_redundancy.py +522 -0
- scipy/optimize/_root.py +732 -0
- scipy/optimize/_root_scalar.py +538 -0
- scipy/optimize/_shgo.py +1606 -0
- scipy/optimize/_shgo_lib/__init__.py +0 -0
- scipy/optimize/_shgo_lib/_complex.py +1225 -0
- scipy/optimize/_shgo_lib/_vertex.py +460 -0
- scipy/optimize/_slsqp_py.py +603 -0
- scipy/optimize/_slsqplib.cp312-win_arm64.lib +0 -0
- scipy/optimize/_slsqplib.cp312-win_arm64.pyd +0 -0
- scipy/optimize/_spectral.py +260 -0
- scipy/optimize/_tnc.py +438 -0
- scipy/optimize/_trlib/__init__.py +12 -0
- scipy/optimize/_trlib/_trlib.cp312-win_arm64.lib +0 -0
- scipy/optimize/_trlib/_trlib.cp312-win_arm64.pyd +0 -0
- scipy/optimize/_trustregion.py +318 -0
- scipy/optimize/_trustregion_constr/__init__.py +6 -0
- scipy/optimize/_trustregion_constr/canonical_constraint.py +390 -0
- scipy/optimize/_trustregion_constr/equality_constrained_sqp.py +231 -0
- scipy/optimize/_trustregion_constr/minimize_trustregion_constr.py +584 -0
- scipy/optimize/_trustregion_constr/projections.py +411 -0
- scipy/optimize/_trustregion_constr/qp_subproblem.py +637 -0
- scipy/optimize/_trustregion_constr/report.py +49 -0
- scipy/optimize/_trustregion_constr/tests/__init__.py +0 -0
- scipy/optimize/_trustregion_constr/tests/test_canonical_constraint.py +296 -0
- scipy/optimize/_trustregion_constr/tests/test_nested_minimize.py +39 -0
- scipy/optimize/_trustregion_constr/tests/test_projections.py +214 -0
- scipy/optimize/_trustregion_constr/tests/test_qp_subproblem.py +645 -0
- scipy/optimize/_trustregion_constr/tests/test_report.py +34 -0
- scipy/optimize/_trustregion_constr/tr_interior_point.py +361 -0
- scipy/optimize/_trustregion_dogleg.py +122 -0
- scipy/optimize/_trustregion_exact.py +437 -0
- scipy/optimize/_trustregion_krylov.py +65 -0
- scipy/optimize/_trustregion_ncg.py +126 -0
- scipy/optimize/_tstutils.py +972 -0
- scipy/optimize/_zeros.cp312-win_arm64.lib +0 -0
- scipy/optimize/_zeros.cp312-win_arm64.pyd +0 -0
- scipy/optimize/_zeros_py.py +1475 -0
- scipy/optimize/cobyla.py +19 -0
- scipy/optimize/cython_optimize/__init__.py +133 -0
- scipy/optimize/cython_optimize/_zeros.cp312-win_arm64.lib +0 -0
- scipy/optimize/cython_optimize/_zeros.cp312-win_arm64.pyd +0 -0
- scipy/optimize/cython_optimize/_zeros.pxd +33 -0
- scipy/optimize/cython_optimize/c_zeros.pxd +26 -0
- scipy/optimize/cython_optimize.pxd +11 -0
- scipy/optimize/elementwise.py +38 -0
- scipy/optimize/lbfgsb.py +23 -0
- scipy/optimize/linesearch.py +18 -0
- scipy/optimize/minpack.py +27 -0
- scipy/optimize/minpack2.py +17 -0
- scipy/optimize/moduleTNC.py +19 -0
- scipy/optimize/nonlin.py +29 -0
- scipy/optimize/optimize.py +40 -0
- scipy/optimize/slsqp.py +22 -0
- scipy/optimize/tests/__init__.py +0 -0
- scipy/optimize/tests/_cython_examples/extending.pyx +43 -0
- scipy/optimize/tests/_cython_examples/meson.build +32 -0
- scipy/optimize/tests/test__basinhopping.py +535 -0
- scipy/optimize/tests/test__differential_evolution.py +1703 -0
- scipy/optimize/tests/test__dual_annealing.py +416 -0
- scipy/optimize/tests/test__linprog_clean_inputs.py +312 -0
- scipy/optimize/tests/test__numdiff.py +885 -0
- scipy/optimize/tests/test__remove_redundancy.py +228 -0
- scipy/optimize/tests/test__root.py +124 -0
- scipy/optimize/tests/test__shgo.py +1164 -0
- scipy/optimize/tests/test__spectral.py +226 -0
- scipy/optimize/tests/test_bracket.py +896 -0
- scipy/optimize/tests/test_chandrupatla.py +982 -0
- scipy/optimize/tests/test_cobyla.py +195 -0
- scipy/optimize/tests/test_cobyqa.py +252 -0
- scipy/optimize/tests/test_constraint_conversion.py +286 -0
- scipy/optimize/tests/test_constraints.py +255 -0
- scipy/optimize/tests/test_cython_optimize.py +92 -0
- scipy/optimize/tests/test_differentiable_functions.py +1025 -0
- scipy/optimize/tests/test_direct.py +321 -0
- scipy/optimize/tests/test_extending.py +28 -0
- scipy/optimize/tests/test_hessian_update_strategy.py +300 -0
- scipy/optimize/tests/test_isotonic_regression.py +167 -0
- scipy/optimize/tests/test_lbfgsb_hessinv.py +65 -0
- scipy/optimize/tests/test_lbfgsb_setulb.py +122 -0
- scipy/optimize/tests/test_least_squares.py +986 -0
- scipy/optimize/tests/test_linear_assignment.py +116 -0
- scipy/optimize/tests/test_linesearch.py +328 -0
- scipy/optimize/tests/test_linprog.py +2577 -0
- scipy/optimize/tests/test_lsq_common.py +297 -0
- scipy/optimize/tests/test_lsq_linear.py +287 -0
- scipy/optimize/tests/test_milp.py +459 -0
- scipy/optimize/tests/test_minimize_constrained.py +845 -0
- scipy/optimize/tests/test_minpack.py +1194 -0
- scipy/optimize/tests/test_nnls.py +469 -0
- scipy/optimize/tests/test_nonlin.py +572 -0
- scipy/optimize/tests/test_optimize.py +3344 -0
- scipy/optimize/tests/test_quadratic_assignment.py +455 -0
- scipy/optimize/tests/test_regression.py +40 -0
- scipy/optimize/tests/test_slsqp.py +645 -0
- scipy/optimize/tests/test_tnc.py +345 -0
- scipy/optimize/tests/test_trustregion.py +110 -0
- scipy/optimize/tests/test_trustregion_exact.py +351 -0
- scipy/optimize/tests/test_trustregion_krylov.py +170 -0
- scipy/optimize/tests/test_zeros.py +998 -0
- scipy/optimize/tnc.py +22 -0
- scipy/optimize/zeros.py +26 -0
- scipy/signal/__init__.py +316 -0
- scipy/signal/_arraytools.py +264 -0
- scipy/signal/_czt.py +575 -0
- scipy/signal/_delegators.py +568 -0
- scipy/signal/_filter_design.py +5893 -0
- scipy/signal/_fir_filter_design.py +1458 -0
- scipy/signal/_lti_conversion.py +534 -0
- scipy/signal/_ltisys.py +3546 -0
- scipy/signal/_max_len_seq.py +139 -0
- scipy/signal/_max_len_seq_inner.cp312-win_arm64.lib +0 -0
- scipy/signal/_max_len_seq_inner.cp312-win_arm64.pyd +0 -0
- scipy/signal/_peak_finding.py +1310 -0
- scipy/signal/_peak_finding_utils.cp312-win_arm64.lib +0 -0
- scipy/signal/_peak_finding_utils.cp312-win_arm64.pyd +0 -0
- scipy/signal/_polyutils.py +172 -0
- scipy/signal/_savitzky_golay.py +357 -0
- scipy/signal/_short_time_fft.py +2228 -0
- scipy/signal/_signal_api.py +30 -0
- scipy/signal/_signaltools.py +5309 -0
- scipy/signal/_sigtools.cp312-win_arm64.lib +0 -0
- scipy/signal/_sigtools.cp312-win_arm64.pyd +0 -0
- scipy/signal/_sosfilt.cp312-win_arm64.lib +0 -0
- scipy/signal/_sosfilt.cp312-win_arm64.pyd +0 -0
- scipy/signal/_spectral_py.py +2471 -0
- scipy/signal/_spline.cp312-win_arm64.lib +0 -0
- scipy/signal/_spline.cp312-win_arm64.pyd +0 -0
- scipy/signal/_spline.pyi +34 -0
- scipy/signal/_spline_filters.py +848 -0
- scipy/signal/_support_alternative_backends.py +73 -0
- scipy/signal/_upfirdn.py +219 -0
- scipy/signal/_upfirdn_apply.cp312-win_arm64.lib +0 -0
- scipy/signal/_upfirdn_apply.cp312-win_arm64.pyd +0 -0
- scipy/signal/_waveforms.py +687 -0
- scipy/signal/_wavelets.py +29 -0
- scipy/signal/bsplines.py +21 -0
- scipy/signal/filter_design.py +28 -0
- scipy/signal/fir_filter_design.py +21 -0
- scipy/signal/lti_conversion.py +20 -0
- scipy/signal/ltisys.py +25 -0
- scipy/signal/signaltools.py +27 -0
- scipy/signal/spectral.py +21 -0
- scipy/signal/spline.py +18 -0
- scipy/signal/tests/__init__.py +0 -0
- scipy/signal/tests/_scipy_spectral_test_shim.py +311 -0
- scipy/signal/tests/mpsig.py +122 -0
- scipy/signal/tests/test_array_tools.py +111 -0
- scipy/signal/tests/test_bsplines.py +365 -0
- scipy/signal/tests/test_cont2discrete.py +424 -0
- scipy/signal/tests/test_czt.py +221 -0
- scipy/signal/tests/test_dltisys.py +599 -0
- scipy/signal/tests/test_filter_design.py +4744 -0
- scipy/signal/tests/test_fir_filter_design.py +851 -0
- scipy/signal/tests/test_ltisys.py +1225 -0
- scipy/signal/tests/test_max_len_seq.py +71 -0
- scipy/signal/tests/test_peak_finding.py +915 -0
- scipy/signal/tests/test_result_type.py +51 -0
- scipy/signal/tests/test_savitzky_golay.py +363 -0
- scipy/signal/tests/test_short_time_fft.py +1107 -0
- scipy/signal/tests/test_signaltools.py +4735 -0
- scipy/signal/tests/test_spectral.py +2141 -0
- scipy/signal/tests/test_splines.py +427 -0
- scipy/signal/tests/test_upfirdn.py +322 -0
- scipy/signal/tests/test_waveforms.py +400 -0
- scipy/signal/tests/test_wavelets.py +59 -0
- scipy/signal/tests/test_windows.py +987 -0
- scipy/signal/waveforms.py +20 -0
- scipy/signal/wavelets.py +17 -0
- scipy/signal/windows/__init__.py +52 -0
- scipy/signal/windows/_windows.py +2513 -0
- scipy/signal/windows/windows.py +23 -0
- scipy/sparse/__init__.py +350 -0
- scipy/sparse/_base.py +1613 -0
- scipy/sparse/_bsr.py +880 -0
- scipy/sparse/_compressed.py +1328 -0
- scipy/sparse/_construct.py +1454 -0
- scipy/sparse/_coo.py +1581 -0
- scipy/sparse/_csc.py +367 -0
- scipy/sparse/_csparsetools.cp312-win_arm64.lib +0 -0
- scipy/sparse/_csparsetools.cp312-win_arm64.pyd +0 -0
- scipy/sparse/_csr.py +558 -0
- scipy/sparse/_data.py +569 -0
- scipy/sparse/_dia.py +677 -0
- scipy/sparse/_dok.py +669 -0
- scipy/sparse/_extract.py +178 -0
- scipy/sparse/_index.py +444 -0
- scipy/sparse/_lil.py +632 -0
- scipy/sparse/_matrix.py +169 -0
- scipy/sparse/_matrix_io.py +167 -0
- scipy/sparse/_sparsetools.cp312-win_arm64.lib +0 -0
- scipy/sparse/_sparsetools.cp312-win_arm64.pyd +0 -0
- scipy/sparse/_spfuncs.py +76 -0
- scipy/sparse/_sputils.py +632 -0
- scipy/sparse/base.py +24 -0
- scipy/sparse/bsr.py +22 -0
- scipy/sparse/compressed.py +20 -0
- scipy/sparse/construct.py +38 -0
- scipy/sparse/coo.py +23 -0
- scipy/sparse/csc.py +22 -0
- scipy/sparse/csgraph/__init__.py +210 -0
- scipy/sparse/csgraph/_flow.cp312-win_arm64.lib +0 -0
- scipy/sparse/csgraph/_flow.cp312-win_arm64.pyd +0 -0
- scipy/sparse/csgraph/_laplacian.py +563 -0
- scipy/sparse/csgraph/_matching.cp312-win_arm64.lib +0 -0
- scipy/sparse/csgraph/_matching.cp312-win_arm64.pyd +0 -0
- scipy/sparse/csgraph/_min_spanning_tree.cp312-win_arm64.lib +0 -0
- scipy/sparse/csgraph/_min_spanning_tree.cp312-win_arm64.pyd +0 -0
- scipy/sparse/csgraph/_reordering.cp312-win_arm64.lib +0 -0
- scipy/sparse/csgraph/_reordering.cp312-win_arm64.pyd +0 -0
- scipy/sparse/csgraph/_shortest_path.cp312-win_arm64.lib +0 -0
- scipy/sparse/csgraph/_shortest_path.cp312-win_arm64.pyd +0 -0
- scipy/sparse/csgraph/_tools.cp312-win_arm64.lib +0 -0
- scipy/sparse/csgraph/_tools.cp312-win_arm64.pyd +0 -0
- scipy/sparse/csgraph/_traversal.cp312-win_arm64.lib +0 -0
- scipy/sparse/csgraph/_traversal.cp312-win_arm64.pyd +0 -0
- scipy/sparse/csgraph/_validation.py +66 -0
- scipy/sparse/csgraph/tests/__init__.py +0 -0
- scipy/sparse/csgraph/tests/test_connected_components.py +119 -0
- scipy/sparse/csgraph/tests/test_conversions.py +61 -0
- scipy/sparse/csgraph/tests/test_flow.py +209 -0
- scipy/sparse/csgraph/tests/test_graph_laplacian.py +368 -0
- scipy/sparse/csgraph/tests/test_matching.py +307 -0
- scipy/sparse/csgraph/tests/test_pydata_sparse.py +197 -0
- scipy/sparse/csgraph/tests/test_reordering.py +70 -0
- scipy/sparse/csgraph/tests/test_shortest_path.py +540 -0
- scipy/sparse/csgraph/tests/test_spanning_tree.py +66 -0
- scipy/sparse/csgraph/tests/test_traversal.py +148 -0
- scipy/sparse/csr.py +22 -0
- scipy/sparse/data.py +18 -0
- scipy/sparse/dia.py +22 -0
- scipy/sparse/dok.py +22 -0
- scipy/sparse/extract.py +23 -0
- scipy/sparse/lil.py +22 -0
- scipy/sparse/linalg/__init__.py +148 -0
- scipy/sparse/linalg/_dsolve/__init__.py +71 -0
- scipy/sparse/linalg/_dsolve/_add_newdocs.py +147 -0
- scipy/sparse/linalg/_dsolve/_superlu.cp312-win_arm64.lib +0 -0
- scipy/sparse/linalg/_dsolve/_superlu.cp312-win_arm64.pyd +0 -0
- scipy/sparse/linalg/_dsolve/linsolve.py +882 -0
- scipy/sparse/linalg/_dsolve/tests/__init__.py +0 -0
- scipy/sparse/linalg/_dsolve/tests/test_linsolve.py +928 -0
- scipy/sparse/linalg/_eigen/__init__.py +22 -0
- scipy/sparse/linalg/_eigen/_svds.py +540 -0
- scipy/sparse/linalg/_eigen/_svds_doc.py +382 -0
- scipy/sparse/linalg/_eigen/arpack/COPYING +45 -0
- scipy/sparse/linalg/_eigen/arpack/__init__.py +20 -0
- scipy/sparse/linalg/_eigen/arpack/_arpack.cp312-win_arm64.lib +0 -0
- scipy/sparse/linalg/_eigen/arpack/_arpack.cp312-win_arm64.pyd +0 -0
- scipy/sparse/linalg/_eigen/arpack/arpack.py +1706 -0
- scipy/sparse/linalg/_eigen/arpack/tests/__init__.py +0 -0
- scipy/sparse/linalg/_eigen/arpack/tests/test_arpack.py +717 -0
- scipy/sparse/linalg/_eigen/lobpcg/__init__.py +16 -0
- scipy/sparse/linalg/_eigen/lobpcg/lobpcg.py +1110 -0
- scipy/sparse/linalg/_eigen/lobpcg/tests/__init__.py +0 -0
- scipy/sparse/linalg/_eigen/lobpcg/tests/test_lobpcg.py +725 -0
- scipy/sparse/linalg/_eigen/tests/__init__.py +0 -0
- scipy/sparse/linalg/_eigen/tests/test_svds.py +886 -0
- scipy/sparse/linalg/_expm_multiply.py +816 -0
- scipy/sparse/linalg/_interface.py +920 -0
- scipy/sparse/linalg/_isolve/__init__.py +20 -0
- scipy/sparse/linalg/_isolve/_gcrotmk.py +503 -0
- scipy/sparse/linalg/_isolve/iterative.py +1051 -0
- scipy/sparse/linalg/_isolve/lgmres.py +230 -0
- scipy/sparse/linalg/_isolve/lsmr.py +486 -0
- scipy/sparse/linalg/_isolve/lsqr.py +589 -0
- scipy/sparse/linalg/_isolve/minres.py +372 -0
- scipy/sparse/linalg/_isolve/tests/__init__.py +0 -0
- scipy/sparse/linalg/_isolve/tests/test_gcrotmk.py +183 -0
- scipy/sparse/linalg/_isolve/tests/test_iterative.py +809 -0
- scipy/sparse/linalg/_isolve/tests/test_lgmres.py +225 -0
- scipy/sparse/linalg/_isolve/tests/test_lsmr.py +185 -0
- scipy/sparse/linalg/_isolve/tests/test_lsqr.py +120 -0
- scipy/sparse/linalg/_isolve/tests/test_minres.py +97 -0
- scipy/sparse/linalg/_isolve/tests/test_utils.py +9 -0
- scipy/sparse/linalg/_isolve/tfqmr.py +179 -0
- scipy/sparse/linalg/_isolve/utils.py +121 -0
- scipy/sparse/linalg/_matfuncs.py +940 -0
- scipy/sparse/linalg/_norm.py +195 -0
- scipy/sparse/linalg/_onenormest.py +467 -0
- scipy/sparse/linalg/_propack/_cpropack.cp312-win_arm64.lib +0 -0
- scipy/sparse/linalg/_propack/_cpropack.cp312-win_arm64.pyd +0 -0
- scipy/sparse/linalg/_propack/_dpropack.cp312-win_arm64.lib +0 -0
- scipy/sparse/linalg/_propack/_dpropack.cp312-win_arm64.pyd +0 -0
- scipy/sparse/linalg/_propack/_spropack.cp312-win_arm64.lib +0 -0
- scipy/sparse/linalg/_propack/_spropack.cp312-win_arm64.pyd +0 -0
- scipy/sparse/linalg/_propack/_zpropack.cp312-win_arm64.lib +0 -0
- scipy/sparse/linalg/_propack/_zpropack.cp312-win_arm64.pyd +0 -0
- scipy/sparse/linalg/_special_sparse_arrays.py +949 -0
- scipy/sparse/linalg/_svdp.py +309 -0
- scipy/sparse/linalg/dsolve.py +22 -0
- scipy/sparse/linalg/eigen.py +21 -0
- scipy/sparse/linalg/interface.py +20 -0
- scipy/sparse/linalg/isolve.py +22 -0
- scipy/sparse/linalg/matfuncs.py +18 -0
- scipy/sparse/linalg/tests/__init__.py +0 -0
- scipy/sparse/linalg/tests/propack_test_data.npz +0 -0
- scipy/sparse/linalg/tests/test_expm_multiply.py +367 -0
- scipy/sparse/linalg/tests/test_interface.py +561 -0
- scipy/sparse/linalg/tests/test_matfuncs.py +592 -0
- scipy/sparse/linalg/tests/test_norm.py +154 -0
- scipy/sparse/linalg/tests/test_onenormest.py +252 -0
- scipy/sparse/linalg/tests/test_propack.py +165 -0
- scipy/sparse/linalg/tests/test_pydata_sparse.py +272 -0
- scipy/sparse/linalg/tests/test_special_sparse_arrays.py +337 -0
- scipy/sparse/sparsetools.py +17 -0
- scipy/sparse/spfuncs.py +17 -0
- scipy/sparse/sputils.py +17 -0
- scipy/sparse/tests/__init__.py +0 -0
- scipy/sparse/tests/data/csc_py2.npz +0 -0
- scipy/sparse/tests/data/csc_py3.npz +0 -0
- scipy/sparse/tests/test_arithmetic1d.py +341 -0
- scipy/sparse/tests/test_array_api.py +561 -0
- scipy/sparse/tests/test_base.py +5870 -0
- scipy/sparse/tests/test_common1d.py +447 -0
- scipy/sparse/tests/test_construct.py +872 -0
- scipy/sparse/tests/test_coo.py +1119 -0
- scipy/sparse/tests/test_csc.py +98 -0
- scipy/sparse/tests/test_csr.py +214 -0
- scipy/sparse/tests/test_dok.py +209 -0
- scipy/sparse/tests/test_extract.py +51 -0
- scipy/sparse/tests/test_indexing1d.py +603 -0
- scipy/sparse/tests/test_matrix_io.py +109 -0
- scipy/sparse/tests/test_minmax1d.py +128 -0
- scipy/sparse/tests/test_sparsetools.py +344 -0
- scipy/sparse/tests/test_spfuncs.py +97 -0
- scipy/sparse/tests/test_sputils.py +424 -0
- scipy/spatial/__init__.py +129 -0
- scipy/spatial/_ckdtree.cp312-win_arm64.lib +0 -0
- scipy/spatial/_ckdtree.cp312-win_arm64.pyd +0 -0
- scipy/spatial/_distance_pybind.cp312-win_arm64.lib +0 -0
- scipy/spatial/_distance_pybind.cp312-win_arm64.pyd +0 -0
- scipy/spatial/_distance_wrap.cp312-win_arm64.lib +0 -0
- scipy/spatial/_distance_wrap.cp312-win_arm64.pyd +0 -0
- scipy/spatial/_geometric_slerp.py +238 -0
- scipy/spatial/_hausdorff.cp312-win_arm64.lib +0 -0
- scipy/spatial/_hausdorff.cp312-win_arm64.pyd +0 -0
- scipy/spatial/_kdtree.py +920 -0
- scipy/spatial/_plotutils.py +274 -0
- scipy/spatial/_procrustes.py +132 -0
- scipy/spatial/_qhull.cp312-win_arm64.lib +0 -0
- scipy/spatial/_qhull.cp312-win_arm64.pyd +0 -0
- scipy/spatial/_qhull.pyi +213 -0
- scipy/spatial/_spherical_voronoi.py +341 -0
- scipy/spatial/_voronoi.cp312-win_arm64.lib +0 -0
- scipy/spatial/_voronoi.cp312-win_arm64.pyd +0 -0
- scipy/spatial/_voronoi.pyi +4 -0
- scipy/spatial/ckdtree.py +18 -0
- scipy/spatial/distance.py +3147 -0
- scipy/spatial/distance.pyi +210 -0
- scipy/spatial/kdtree.py +25 -0
- scipy/spatial/qhull.py +25 -0
- scipy/spatial/qhull_src/COPYING_QHULL.txt +39 -0
- scipy/spatial/tests/__init__.py +0 -0
- scipy/spatial/tests/data/cdist-X1.txt +10 -0
- scipy/spatial/tests/data/cdist-X2.txt +20 -0
- scipy/spatial/tests/data/degenerate_pointset.npz +0 -0
- scipy/spatial/tests/data/iris.txt +150 -0
- scipy/spatial/tests/data/pdist-boolean-inp.txt +20 -0
- scipy/spatial/tests/data/pdist-chebyshev-ml-iris.txt +1 -0
- scipy/spatial/tests/data/pdist-chebyshev-ml.txt +1 -0
- scipy/spatial/tests/data/pdist-cityblock-ml-iris.txt +1 -0
- scipy/spatial/tests/data/pdist-cityblock-ml.txt +1 -0
- scipy/spatial/tests/data/pdist-correlation-ml-iris.txt +1 -0
- scipy/spatial/tests/data/pdist-correlation-ml.txt +1 -0
- scipy/spatial/tests/data/pdist-cosine-ml-iris.txt +1 -0
- scipy/spatial/tests/data/pdist-cosine-ml.txt +1 -0
- scipy/spatial/tests/data/pdist-double-inp.txt +20 -0
- scipy/spatial/tests/data/pdist-euclidean-ml-iris.txt +1 -0
- scipy/spatial/tests/data/pdist-euclidean-ml.txt +1 -0
- scipy/spatial/tests/data/pdist-hamming-ml.txt +1 -0
- scipy/spatial/tests/data/pdist-jaccard-ml.txt +1 -0
- scipy/spatial/tests/data/pdist-jensenshannon-ml-iris.txt +1 -0
- scipy/spatial/tests/data/pdist-jensenshannon-ml.txt +1 -0
- scipy/spatial/tests/data/pdist-minkowski-3.2-ml-iris.txt +1 -0
- scipy/spatial/tests/data/pdist-minkowski-3.2-ml.txt +1 -0
- scipy/spatial/tests/data/pdist-minkowski-5.8-ml-iris.txt +1 -0
- scipy/spatial/tests/data/pdist-seuclidean-ml-iris.txt +1 -0
- scipy/spatial/tests/data/pdist-seuclidean-ml.txt +1 -0
- scipy/spatial/tests/data/pdist-spearman-ml.txt +1 -0
- scipy/spatial/tests/data/random-bool-data.txt +100 -0
- scipy/spatial/tests/data/random-double-data.txt +100 -0
- scipy/spatial/tests/data/random-int-data.txt +100 -0
- scipy/spatial/tests/data/random-uint-data.txt +100 -0
- scipy/spatial/tests/data/selfdual-4d-polytope.txt +27 -0
- scipy/spatial/tests/test__plotutils.py +91 -0
- scipy/spatial/tests/test__procrustes.py +116 -0
- scipy/spatial/tests/test_distance.py +2389 -0
- scipy/spatial/tests/test_hausdorff.py +199 -0
- scipy/spatial/tests/test_kdtree.py +1536 -0
- scipy/spatial/tests/test_qhull.py +1313 -0
- scipy/spatial/tests/test_slerp.py +417 -0
- scipy/spatial/tests/test_spherical_voronoi.py +358 -0
- scipy/spatial/transform/__init__.py +31 -0
- scipy/spatial/transform/_rigid_transform.cp312-win_arm64.lib +0 -0
- scipy/spatial/transform/_rigid_transform.cp312-win_arm64.pyd +0 -0
- scipy/spatial/transform/_rotation.cp312-win_arm64.lib +0 -0
- scipy/spatial/transform/_rotation.cp312-win_arm64.pyd +0 -0
- scipy/spatial/transform/_rotation_groups.py +140 -0
- scipy/spatial/transform/_rotation_spline.py +460 -0
- scipy/spatial/transform/rotation.py +21 -0
- scipy/spatial/transform/tests/__init__.py +0 -0
- scipy/spatial/transform/tests/test_rigid_transform.py +1221 -0
- scipy/spatial/transform/tests/test_rotation.py +2569 -0
- scipy/spatial/transform/tests/test_rotation_groups.py +169 -0
- scipy/spatial/transform/tests/test_rotation_spline.py +183 -0
- scipy/special/__init__.pxd +1 -0
- scipy/special/__init__.py +841 -0
- scipy/special/_add_newdocs.py +9961 -0
- scipy/special/_basic.py +3576 -0
- scipy/special/_comb.cp312-win_arm64.lib +0 -0
- scipy/special/_comb.cp312-win_arm64.pyd +0 -0
- scipy/special/_ellip_harm.py +214 -0
- scipy/special/_ellip_harm_2.cp312-win_arm64.lib +0 -0
- scipy/special/_ellip_harm_2.cp312-win_arm64.pyd +0 -0
- scipy/special/_gufuncs.cp312-win_arm64.lib +0 -0
- scipy/special/_gufuncs.cp312-win_arm64.pyd +0 -0
- scipy/special/_input_validation.py +17 -0
- scipy/special/_lambertw.py +149 -0
- scipy/special/_logsumexp.py +426 -0
- scipy/special/_mptestutils.py +453 -0
- scipy/special/_multiufuncs.py +610 -0
- scipy/special/_orthogonal.py +2592 -0
- scipy/special/_orthogonal.pyi +330 -0
- scipy/special/_precompute/__init__.py +0 -0
- scipy/special/_precompute/cosine_cdf.py +17 -0
- scipy/special/_precompute/expn_asy.py +54 -0
- scipy/special/_precompute/gammainc_asy.py +116 -0
- scipy/special/_precompute/gammainc_data.py +124 -0
- scipy/special/_precompute/hyp2f1_data.py +484 -0
- scipy/special/_precompute/lambertw.py +68 -0
- scipy/special/_precompute/loggamma.py +43 -0
- scipy/special/_precompute/struve_convergence.py +131 -0
- scipy/special/_precompute/utils.py +38 -0
- scipy/special/_precompute/wright_bessel.py +342 -0
- scipy/special/_precompute/wright_bessel_data.py +152 -0
- scipy/special/_precompute/wrightomega.py +41 -0
- scipy/special/_precompute/zetac.py +27 -0
- scipy/special/_sf_error.py +15 -0
- scipy/special/_specfun.cp312-win_arm64.lib +0 -0
- scipy/special/_specfun.cp312-win_arm64.pyd +0 -0
- scipy/special/_special_ufuncs.cp312-win_arm64.lib +0 -0
- scipy/special/_special_ufuncs.cp312-win_arm64.pyd +0 -0
- scipy/special/_spfun_stats.py +106 -0
- scipy/special/_spherical_bessel.py +397 -0
- scipy/special/_support_alternative_backends.py +295 -0
- scipy/special/_test_internal.cp312-win_arm64.lib +0 -0
- scipy/special/_test_internal.cp312-win_arm64.pyd +0 -0
- scipy/special/_test_internal.pyi +9 -0
- scipy/special/_testutils.py +321 -0
- scipy/special/_ufuncs.cp312-win_arm64.lib +0 -0
- scipy/special/_ufuncs.cp312-win_arm64.pyd +0 -0
- scipy/special/_ufuncs.pyi +522 -0
- scipy/special/_ufuncs.pyx +13173 -0
- scipy/special/_ufuncs_cxx.cp312-win_arm64.lib +0 -0
- scipy/special/_ufuncs_cxx.cp312-win_arm64.pyd +0 -0
- scipy/special/_ufuncs_cxx.pxd +142 -0
- scipy/special/_ufuncs_cxx.pyx +427 -0
- scipy/special/_ufuncs_cxx_defs.h +147 -0
- scipy/special/_ufuncs_defs.h +57 -0
- scipy/special/add_newdocs.py +15 -0
- scipy/special/basic.py +87 -0
- scipy/special/cython_special.cp312-win_arm64.lib +0 -0
- scipy/special/cython_special.cp312-win_arm64.pyd +0 -0
- scipy/special/cython_special.pxd +259 -0
- scipy/special/cython_special.pyi +3 -0
- scipy/special/orthogonal.py +45 -0
- scipy/special/sf_error.py +20 -0
- scipy/special/specfun.py +24 -0
- scipy/special/spfun_stats.py +17 -0
- scipy/special/tests/__init__.py +0 -0
- scipy/special/tests/_cython_examples/extending.pyx +12 -0
- scipy/special/tests/_cython_examples/meson.build +34 -0
- scipy/special/tests/data/__init__.py +0 -0
- scipy/special/tests/data/boost.npz +0 -0
- scipy/special/tests/data/gsl.npz +0 -0
- scipy/special/tests/data/local.npz +0 -0
- scipy/special/tests/test_basic.py +4815 -0
- scipy/special/tests/test_bdtr.py +112 -0
- scipy/special/tests/test_boost_ufuncs.py +64 -0
- scipy/special/tests/test_boxcox.py +125 -0
- scipy/special/tests/test_cdflib.py +712 -0
- scipy/special/tests/test_cdft_asymptotic.py +49 -0
- scipy/special/tests/test_cephes_intp_cast.py +29 -0
- scipy/special/tests/test_cosine_distr.py +83 -0
- scipy/special/tests/test_cython_special.py +363 -0
- scipy/special/tests/test_data.py +719 -0
- scipy/special/tests/test_dd.py +42 -0
- scipy/special/tests/test_digamma.py +45 -0
- scipy/special/tests/test_ellip_harm.py +278 -0
- scipy/special/tests/test_erfinv.py +89 -0
- scipy/special/tests/test_exponential_integrals.py +118 -0
- scipy/special/tests/test_extending.py +28 -0
- scipy/special/tests/test_faddeeva.py +85 -0
- scipy/special/tests/test_gamma.py +12 -0
- scipy/special/tests/test_gammainc.py +152 -0
- scipy/special/tests/test_hyp2f1.py +2566 -0
- scipy/special/tests/test_hypergeometric.py +234 -0
- scipy/special/tests/test_iv_ratio.py +249 -0
- scipy/special/tests/test_kolmogorov.py +491 -0
- scipy/special/tests/test_lambertw.py +109 -0
- scipy/special/tests/test_legendre.py +1518 -0
- scipy/special/tests/test_log1mexp.py +85 -0
- scipy/special/tests/test_loggamma.py +70 -0
- scipy/special/tests/test_logit.py +162 -0
- scipy/special/tests/test_logsumexp.py +469 -0
- scipy/special/tests/test_mpmath.py +2293 -0
- scipy/special/tests/test_nan_inputs.py +65 -0
- scipy/special/tests/test_ndtr.py +77 -0
- scipy/special/tests/test_ndtri_exp.py +94 -0
- scipy/special/tests/test_orthogonal.py +821 -0
- scipy/special/tests/test_orthogonal_eval.py +275 -0
- scipy/special/tests/test_owens_t.py +53 -0
- scipy/special/tests/test_pcf.py +24 -0
- scipy/special/tests/test_pdtr.py +48 -0
- scipy/special/tests/test_powm1.py +65 -0
- scipy/special/tests/test_precompute_expn_asy.py +24 -0
- scipy/special/tests/test_precompute_gammainc.py +108 -0
- scipy/special/tests/test_precompute_utils.py +36 -0
- scipy/special/tests/test_round.py +18 -0
- scipy/special/tests/test_sf_error.py +146 -0
- scipy/special/tests/test_sici.py +36 -0
- scipy/special/tests/test_specfun.py +48 -0
- scipy/special/tests/test_spence.py +32 -0
- scipy/special/tests/test_spfun_stats.py +61 -0
- scipy/special/tests/test_sph_harm.py +85 -0
- scipy/special/tests/test_spherical_bessel.py +400 -0
- scipy/special/tests/test_support_alternative_backends.py +248 -0
- scipy/special/tests/test_trig.py +72 -0
- scipy/special/tests/test_ufunc_signatures.py +46 -0
- scipy/special/tests/test_wright_bessel.py +205 -0
- scipy/special/tests/test_wrightomega.py +117 -0
- scipy/special/tests/test_zeta.py +301 -0
- scipy/stats/__init__.py +670 -0
- scipy/stats/_ansari_swilk_statistics.cp312-win_arm64.lib +0 -0
- scipy/stats/_ansari_swilk_statistics.cp312-win_arm64.pyd +0 -0
- scipy/stats/_axis_nan_policy.py +692 -0
- scipy/stats/_biasedurn.cp312-win_arm64.lib +0 -0
- scipy/stats/_biasedurn.cp312-win_arm64.pyd +0 -0
- scipy/stats/_biasedurn.pxd +27 -0
- scipy/stats/_binned_statistic.py +795 -0
- scipy/stats/_binomtest.py +375 -0
- scipy/stats/_bws_test.py +177 -0
- scipy/stats/_censored_data.py +459 -0
- scipy/stats/_common.py +5 -0
- scipy/stats/_constants.py +42 -0
- scipy/stats/_continued_fraction.py +387 -0
- scipy/stats/_continuous_distns.py +12486 -0
- scipy/stats/_correlation.py +210 -0
- scipy/stats/_covariance.py +636 -0
- scipy/stats/_crosstab.py +204 -0
- scipy/stats/_discrete_distns.py +2098 -0
- scipy/stats/_distn_infrastructure.py +4201 -0
- scipy/stats/_distr_params.py +299 -0
- scipy/stats/_distribution_infrastructure.py +5750 -0
- scipy/stats/_entropy.py +428 -0
- scipy/stats/_finite_differences.py +145 -0
- scipy/stats/_fit.py +1351 -0
- scipy/stats/_hypotests.py +2060 -0
- scipy/stats/_kde.py +732 -0
- scipy/stats/_ksstats.py +600 -0
- scipy/stats/_levy_stable/__init__.py +1231 -0
- scipy/stats/_levy_stable/levyst.cp312-win_arm64.lib +0 -0
- scipy/stats/_levy_stable/levyst.cp312-win_arm64.pyd +0 -0
- scipy/stats/_mannwhitneyu.py +492 -0
- scipy/stats/_mgc.py +550 -0
- scipy/stats/_morestats.py +4626 -0
- scipy/stats/_mstats_basic.py +3658 -0
- scipy/stats/_mstats_extras.py +521 -0
- scipy/stats/_multicomp.py +449 -0
- scipy/stats/_multivariate.py +7281 -0
- scipy/stats/_new_distributions.py +452 -0
- scipy/stats/_odds_ratio.py +466 -0
- scipy/stats/_page_trend_test.py +486 -0
- scipy/stats/_probability_distribution.py +1964 -0
- scipy/stats/_qmc.py +2956 -0
- scipy/stats/_qmc_cy.cp312-win_arm64.lib +0 -0
- scipy/stats/_qmc_cy.cp312-win_arm64.pyd +0 -0
- scipy/stats/_qmc_cy.pyi +54 -0
- scipy/stats/_qmvnt.py +454 -0
- scipy/stats/_qmvnt_cy.cp312-win_arm64.lib +0 -0
- scipy/stats/_qmvnt_cy.cp312-win_arm64.pyd +0 -0
- scipy/stats/_quantile.py +335 -0
- scipy/stats/_rcont/__init__.py +4 -0
- scipy/stats/_rcont/rcont.cp312-win_arm64.lib +0 -0
- scipy/stats/_rcont/rcont.cp312-win_arm64.pyd +0 -0
- scipy/stats/_relative_risk.py +263 -0
- scipy/stats/_resampling.py +2352 -0
- scipy/stats/_result_classes.py +40 -0
- scipy/stats/_sampling.py +1314 -0
- scipy/stats/_sensitivity_analysis.py +713 -0
- scipy/stats/_sobol.cp312-win_arm64.lib +0 -0
- scipy/stats/_sobol.cp312-win_arm64.pyd +0 -0
- scipy/stats/_sobol.pyi +54 -0
- scipy/stats/_sobol_direction_numbers.npz +0 -0
- scipy/stats/_stats.cp312-win_arm64.lib +0 -0
- scipy/stats/_stats.cp312-win_arm64.pyd +0 -0
- scipy/stats/_stats.pxd +10 -0
- scipy/stats/_stats_mstats_common.py +322 -0
- scipy/stats/_stats_py.py +11089 -0
- scipy/stats/_stats_pythran.cp312-win_arm64.lib +0 -0
- scipy/stats/_stats_pythran.cp312-win_arm64.pyd +0 -0
- scipy/stats/_survival.py +683 -0
- scipy/stats/_tukeylambda_stats.py +199 -0
- scipy/stats/_unuran/__init__.py +0 -0
- scipy/stats/_unuran/unuran_wrapper.cp312-win_arm64.lib +0 -0
- scipy/stats/_unuran/unuran_wrapper.cp312-win_arm64.pyd +0 -0
- scipy/stats/_unuran/unuran_wrapper.pyi +179 -0
- scipy/stats/_variation.py +126 -0
- scipy/stats/_warnings_errors.py +38 -0
- scipy/stats/_wilcoxon.py +265 -0
- scipy/stats/biasedurn.py +16 -0
- scipy/stats/contingency.py +521 -0
- scipy/stats/distributions.py +24 -0
- scipy/stats/kde.py +18 -0
- scipy/stats/morestats.py +27 -0
- scipy/stats/mstats.py +140 -0
- scipy/stats/mstats_basic.py +42 -0
- scipy/stats/mstats_extras.py +25 -0
- scipy/stats/mvn.py +17 -0
- scipy/stats/qmc.py +236 -0
- scipy/stats/sampling.py +73 -0
- scipy/stats/stats.py +41 -0
- scipy/stats/tests/__init__.py +0 -0
- scipy/stats/tests/common_tests.py +356 -0
- scipy/stats/tests/data/_mvt.py +171 -0
- scipy/stats/tests/data/fisher_exact_results_from_r.py +607 -0
- scipy/stats/tests/data/jf_skew_t_gamlss_pdf_data.npy +0 -0
- scipy/stats/tests/data/levy_stable/stable-Z1-cdf-sample-data.npy +0 -0
- scipy/stats/tests/data/levy_stable/stable-Z1-pdf-sample-data.npy +0 -0
- scipy/stats/tests/data/levy_stable/stable-loc-scale-sample-data.npy +0 -0
- scipy/stats/tests/data/nist_anova/AtmWtAg.dat +108 -0
- scipy/stats/tests/data/nist_anova/SiRstv.dat +85 -0
- scipy/stats/tests/data/nist_anova/SmLs01.dat +249 -0
- scipy/stats/tests/data/nist_anova/SmLs02.dat +1869 -0
- scipy/stats/tests/data/nist_anova/SmLs03.dat +18069 -0
- scipy/stats/tests/data/nist_anova/SmLs04.dat +249 -0
- scipy/stats/tests/data/nist_anova/SmLs05.dat +1869 -0
- scipy/stats/tests/data/nist_anova/SmLs06.dat +18069 -0
- scipy/stats/tests/data/nist_anova/SmLs07.dat +249 -0
- scipy/stats/tests/data/nist_anova/SmLs08.dat +1869 -0
- scipy/stats/tests/data/nist_anova/SmLs09.dat +18069 -0
- scipy/stats/tests/data/nist_linregress/Norris.dat +97 -0
- scipy/stats/tests/data/rel_breitwigner_pdf_sample_data_ROOT.npy +0 -0
- scipy/stats/tests/data/studentized_range_mpmath_ref.json +1499 -0
- scipy/stats/tests/test_axis_nan_policy.py +1388 -0
- scipy/stats/tests/test_binned_statistic.py +568 -0
- scipy/stats/tests/test_censored_data.py +152 -0
- scipy/stats/tests/test_contingency.py +294 -0
- scipy/stats/tests/test_continued_fraction.py +173 -0
- scipy/stats/tests/test_continuous.py +2198 -0
- scipy/stats/tests/test_continuous_basic.py +1053 -0
- scipy/stats/tests/test_continuous_fit_censored.py +683 -0
- scipy/stats/tests/test_correlation.py +80 -0
- scipy/stats/tests/test_crosstab.py +115 -0
- scipy/stats/tests/test_discrete_basic.py +580 -0
- scipy/stats/tests/test_discrete_distns.py +700 -0
- scipy/stats/tests/test_distributions.py +10413 -0
- scipy/stats/tests/test_entropy.py +322 -0
- scipy/stats/tests/test_fast_gen_inversion.py +435 -0
- scipy/stats/tests/test_fit.py +1090 -0
- scipy/stats/tests/test_hypotests.py +1991 -0
- scipy/stats/tests/test_kdeoth.py +676 -0
- scipy/stats/tests/test_marray.py +289 -0
- scipy/stats/tests/test_mgc.py +217 -0
- scipy/stats/tests/test_morestats.py +3259 -0
- scipy/stats/tests/test_mstats_basic.py +2071 -0
- scipy/stats/tests/test_mstats_extras.py +172 -0
- scipy/stats/tests/test_multicomp.py +405 -0
- scipy/stats/tests/test_multivariate.py +4381 -0
- scipy/stats/tests/test_odds_ratio.py +148 -0
- scipy/stats/tests/test_qmc.py +1492 -0
- scipy/stats/tests/test_quantile.py +199 -0
- scipy/stats/tests/test_rank.py +345 -0
- scipy/stats/tests/test_relative_risk.py +95 -0
- scipy/stats/tests/test_resampling.py +2000 -0
- scipy/stats/tests/test_sampling.py +1450 -0
- scipy/stats/tests/test_sensitivity_analysis.py +310 -0
- scipy/stats/tests/test_stats.py +9707 -0
- scipy/stats/tests/test_survival.py +466 -0
- scipy/stats/tests/test_tukeylambda_stats.py +85 -0
- scipy/stats/tests/test_variation.py +216 -0
- scipy/version.py +12 -0
- scipy-1.16.2.dist-info/DELVEWHEEL +2 -0
- scipy-1.16.2.dist-info/LICENSE.txt +912 -0
- scipy-1.16.2.dist-info/METADATA +1061 -0
- scipy-1.16.2.dist-info/RECORD +1530 -0
- scipy-1.16.2.dist-info/WHEEL +4 -0
- scipy.libs/msvcp140-5f1c5dd31916990d94181e07bc3afb32.dll +0 -0
- scipy.libs/scipy_openblas-f3ac85b1f412f7e86514c923dc4058d1.dll +0 -0
@@ -0,0 +1,3754 @@
|
|
1
|
+
import os
|
2
|
+
import operator
|
3
|
+
import itertools
|
4
|
+
import math
|
5
|
+
import threading
|
6
|
+
import copy
|
7
|
+
|
8
|
+
import numpy as np
|
9
|
+
from numpy.testing import suppress_warnings
|
10
|
+
from scipy._lib._array_api import xp_assert_equal, xp_assert_close
|
11
|
+
from pytest import raises as assert_raises
|
12
|
+
import pytest
|
13
|
+
|
14
|
+
from scipy.interpolate import (
|
15
|
+
BSpline, BPoly, PPoly, make_interp_spline, make_lsq_spline,
|
16
|
+
splev, splrep, splprep, splder, splantider, sproot, splint, insert,
|
17
|
+
CubicSpline, NdBSpline, make_smoothing_spline, RegularGridInterpolator,
|
18
|
+
)
|
19
|
+
import scipy.linalg as sl
|
20
|
+
import scipy.sparse.linalg as ssl
|
21
|
+
|
22
|
+
from scipy.interpolate._bsplines import (_not_a_knot, _augknt,
|
23
|
+
_woodbury_algorithm, _periodic_knots,
|
24
|
+
_make_interp_per_full_matr)
|
25
|
+
|
26
|
+
from scipy.interpolate import generate_knots, make_splrep, make_splprep
|
27
|
+
|
28
|
+
import scipy.interpolate._fitpack_impl as _impl
|
29
|
+
from scipy._lib._util import AxisError
|
30
|
+
from scipy._lib._testutils import _run_concurrent_barrier
|
31
|
+
|
32
|
+
# XXX: move to the interpolate namespace
|
33
|
+
from scipy.interpolate._ndbspline import make_ndbspl
|
34
|
+
|
35
|
+
from scipy.interpolate import _dfitpack as dfitpack
|
36
|
+
from scipy.interpolate import _bsplines as _b
|
37
|
+
from scipy.interpolate import _dierckx
|
38
|
+
|
39
|
+
|
40
|
+
class TestBSpline:
|
41
|
+
|
42
|
+
def test_ctor(self):
|
43
|
+
# knots should be an ordered 1-D array of finite real numbers
|
44
|
+
assert_raises((TypeError, ValueError), BSpline,
|
45
|
+
**dict(t=[1, 1.j], c=[1.], k=0))
|
46
|
+
with np.errstate(invalid='ignore'):
|
47
|
+
assert_raises(ValueError, BSpline, **dict(t=[1, np.nan], c=[1.], k=0))
|
48
|
+
assert_raises(ValueError, BSpline, **dict(t=[1, np.inf], c=[1.], k=0))
|
49
|
+
assert_raises(ValueError, BSpline, **dict(t=[1, -1], c=[1.], k=0))
|
50
|
+
assert_raises(ValueError, BSpline, **dict(t=[[1], [1]], c=[1.], k=0))
|
51
|
+
|
52
|
+
# for n+k+1 knots and degree k need at least n coefficients
|
53
|
+
assert_raises(ValueError, BSpline, **dict(t=[0, 1, 2], c=[1], k=0))
|
54
|
+
assert_raises(ValueError, BSpline,
|
55
|
+
**dict(t=[0, 1, 2, 3, 4], c=[1., 1.], k=2))
|
56
|
+
|
57
|
+
# non-integer orders
|
58
|
+
assert_raises(TypeError, BSpline,
|
59
|
+
**dict(t=[0., 0., 1., 2., 3., 4.], c=[1., 1., 1.], k="cubic"))
|
60
|
+
assert_raises(TypeError, BSpline,
|
61
|
+
**dict(t=[0., 0., 1., 2., 3., 4.], c=[1., 1., 1.], k=2.5))
|
62
|
+
|
63
|
+
# basic interval cannot have measure zero (here: [1..1])
|
64
|
+
assert_raises(ValueError, BSpline,
|
65
|
+
**dict(t=[0., 0, 1, 1, 2, 3], c=[1., 1, 1], k=2))
|
66
|
+
|
67
|
+
# tck vs self.tck
|
68
|
+
n, k = 11, 3
|
69
|
+
t = np.arange(n+k+1, dtype=np.float64)
|
70
|
+
c = np.random.random(n)
|
71
|
+
b = BSpline(t, c, k)
|
72
|
+
|
73
|
+
xp_assert_close(t, b.t)
|
74
|
+
xp_assert_close(c, b.c)
|
75
|
+
assert k == b.k
|
76
|
+
|
77
|
+
def test_tck(self):
|
78
|
+
b = _make_random_spline()
|
79
|
+
tck = b.tck
|
80
|
+
|
81
|
+
xp_assert_close(b.t, tck[0], atol=1e-15, rtol=1e-15)
|
82
|
+
xp_assert_close(b.c, tck[1], atol=1e-15, rtol=1e-15)
|
83
|
+
assert b.k == tck[2]
|
84
|
+
|
85
|
+
# b.tck is read-only
|
86
|
+
with pytest.raises(AttributeError):
|
87
|
+
b.tck = 'foo'
|
88
|
+
|
89
|
+
def test_degree_0(self):
|
90
|
+
xx = np.linspace(0, 1, 10)
|
91
|
+
|
92
|
+
b = BSpline(t=[0, 1], c=[3.], k=0)
|
93
|
+
xp_assert_close(b(xx), np.ones_like(xx) * 3.0)
|
94
|
+
|
95
|
+
b = BSpline(t=[0, 0.35, 1], c=[3, 4], k=0)
|
96
|
+
xp_assert_close(b(xx), np.where(xx < 0.35, 3.0, 4.0))
|
97
|
+
|
98
|
+
def test_degree_1(self):
|
99
|
+
t = [0, 1, 2, 3, 4]
|
100
|
+
c = [1, 2, 3]
|
101
|
+
k = 1
|
102
|
+
b = BSpline(t, c, k)
|
103
|
+
|
104
|
+
x = np.linspace(1, 3, 50)
|
105
|
+
xp_assert_close(c[0]*B_012(x) + c[1]*B_012(x-1) + c[2]*B_012(x-2),
|
106
|
+
b(x), atol=1e-14)
|
107
|
+
xp_assert_close(splev(x, (t, c, k)), b(x), atol=1e-14)
|
108
|
+
|
109
|
+
def test_bernstein(self):
|
110
|
+
# a special knot vector: Bernstein polynomials
|
111
|
+
k = 3
|
112
|
+
t = np.asarray([0]*(k+1) + [1]*(k+1))
|
113
|
+
c = np.asarray([1., 2., 3., 4.])
|
114
|
+
bp = BPoly(c.reshape(-1, 1), [0, 1])
|
115
|
+
bspl = BSpline(t, c, k)
|
116
|
+
|
117
|
+
xx = np.linspace(-1., 2., 10)
|
118
|
+
xp_assert_close(bp(xx, extrapolate=True),
|
119
|
+
bspl(xx, extrapolate=True), atol=1e-14)
|
120
|
+
xp_assert_close(splev(xx, (t, c, k)),
|
121
|
+
bspl(xx), atol=1e-14)
|
122
|
+
|
123
|
+
def test_rndm_naive_eval(self):
|
124
|
+
# test random coefficient spline *on the base interval*,
|
125
|
+
# t[k] <= x < t[-k-1]
|
126
|
+
b = _make_random_spline()
|
127
|
+
t, c, k = b.tck
|
128
|
+
xx = np.linspace(t[k], t[-k-1], 50)
|
129
|
+
y_b = b(xx)
|
130
|
+
|
131
|
+
y_n = [_naive_eval(x, t, c, k) for x in xx]
|
132
|
+
xp_assert_close(y_b, y_n, atol=1e-14)
|
133
|
+
|
134
|
+
y_n2 = [_naive_eval_2(x, t, c, k) for x in xx]
|
135
|
+
xp_assert_close(y_b, y_n2, atol=1e-14)
|
136
|
+
|
137
|
+
def test_rndm_splev(self):
|
138
|
+
b = _make_random_spline()
|
139
|
+
t, c, k = b.tck
|
140
|
+
xx = np.linspace(t[k], t[-k-1], 50)
|
141
|
+
xp_assert_close(b(xx), splev(xx, (t, c, k)), atol=1e-14)
|
142
|
+
|
143
|
+
def test_rndm_splrep(self):
|
144
|
+
rng = np.random.RandomState(1234)
|
145
|
+
x = np.sort(rng.random(20))
|
146
|
+
y = rng.random(20)
|
147
|
+
|
148
|
+
tck = splrep(x, y)
|
149
|
+
b = BSpline(*tck)
|
150
|
+
|
151
|
+
t, k = b.t, b.k
|
152
|
+
xx = np.linspace(t[k], t[-k-1], 80)
|
153
|
+
xp_assert_close(b(xx), splev(xx, tck), atol=1e-14)
|
154
|
+
|
155
|
+
def test_rndm_unity(self):
|
156
|
+
b = _make_random_spline()
|
157
|
+
b.c = np.ones_like(b.c)
|
158
|
+
xx = np.linspace(b.t[b.k], b.t[-b.k-1], 100)
|
159
|
+
xp_assert_close(b(xx), np.ones_like(xx))
|
160
|
+
|
161
|
+
def test_vectorization(self):
|
162
|
+
rng = np.random.RandomState(1234)
|
163
|
+
n, k = 22, 3
|
164
|
+
t = np.sort(rng.random(n))
|
165
|
+
c = rng.random(size=(n, 6, 7))
|
166
|
+
b = BSpline(t, c, k)
|
167
|
+
tm, tp = t[k], t[-k-1]
|
168
|
+
xx = tm + (tp - tm) * rng.random((3, 4, 5))
|
169
|
+
assert b(xx).shape == (3, 4, 5, 6, 7)
|
170
|
+
|
171
|
+
def test_len_c(self):
|
172
|
+
# for n+k+1 knots, only first n coefs are used.
|
173
|
+
# and BTW this is consistent with FITPACK
|
174
|
+
rng = np.random.RandomState(1234)
|
175
|
+
n, k = 33, 3
|
176
|
+
t = np.sort(rng.random(n+k+1))
|
177
|
+
c = rng.random(n)
|
178
|
+
|
179
|
+
# pad coefficients with random garbage
|
180
|
+
c_pad = np.r_[c, rng.random(k+1)]
|
181
|
+
|
182
|
+
b, b_pad = BSpline(t, c, k), BSpline(t, c_pad, k)
|
183
|
+
|
184
|
+
dt = t[-1] - t[0]
|
185
|
+
xx = np.linspace(t[0] - dt, t[-1] + dt, 50)
|
186
|
+
xp_assert_close(b(xx), b_pad(xx), atol=1e-14)
|
187
|
+
xp_assert_close(b(xx), splev(xx, (t, c, k)), atol=1e-14)
|
188
|
+
xp_assert_close(b(xx), splev(xx, (t, c_pad, k)), atol=1e-14)
|
189
|
+
|
190
|
+
def test_endpoints(self, num_parallel_threads):
|
191
|
+
# base interval is closed
|
192
|
+
b = _make_random_spline()
|
193
|
+
t, _, k = b.tck
|
194
|
+
tm, tp = t[k], t[-k-1]
|
195
|
+
# atol = 1e-9 if num_parallel_threads == 1 else 1e-7
|
196
|
+
for extrap in (True, False):
|
197
|
+
xp_assert_close(b([tm, tp], extrap),
|
198
|
+
b([tm + 1e-10, tp - 1e-10], extrap), atol=1e-9, rtol=1e-7)
|
199
|
+
|
200
|
+
def test_continuity(self, num_parallel_threads):
|
201
|
+
# assert continuity at internal knots
|
202
|
+
b = _make_random_spline()
|
203
|
+
t, _, k = b.tck
|
204
|
+
xp_assert_close(b(t[k+1:-k-1] - 1e-10), b(t[k+1:-k-1] + 1e-10),
|
205
|
+
atol=1e-9)
|
206
|
+
|
207
|
+
def test_extrap(self):
|
208
|
+
b = _make_random_spline()
|
209
|
+
t, c, k = b.tck
|
210
|
+
dt = t[-1] - t[0]
|
211
|
+
xx = np.linspace(t[k] - dt, t[-k-1] + dt, 50)
|
212
|
+
mask = (t[k] < xx) & (xx < t[-k-1])
|
213
|
+
|
214
|
+
# extrap has no effect within the base interval
|
215
|
+
xp_assert_close(b(xx[mask], extrapolate=True),
|
216
|
+
b(xx[mask], extrapolate=False))
|
217
|
+
|
218
|
+
# extrapolated values agree with FITPACK
|
219
|
+
xp_assert_close(b(xx, extrapolate=True),
|
220
|
+
splev(xx, (t, c, k), ext=0))
|
221
|
+
|
222
|
+
def test_default_extrap(self):
|
223
|
+
# BSpline defaults to extrapolate=True
|
224
|
+
b = _make_random_spline()
|
225
|
+
t, _, k = b.tck
|
226
|
+
xx = [t[0] - 1, t[-1] + 1]
|
227
|
+
yy = b(xx)
|
228
|
+
assert not np.all(np.isnan(yy))
|
229
|
+
|
230
|
+
def test_periodic_extrap(self):
|
231
|
+
rng = np.random.RandomState(1234)
|
232
|
+
t = np.sort(rng.random(8))
|
233
|
+
c = rng.random(4)
|
234
|
+
k = 3
|
235
|
+
b = BSpline(t, c, k, extrapolate='periodic')
|
236
|
+
n = t.size - (k + 1)
|
237
|
+
|
238
|
+
dt = t[-1] - t[0]
|
239
|
+
xx = np.linspace(t[k] - dt, t[n] + dt, 50)
|
240
|
+
xy = t[k] + (xx - t[k]) % (t[n] - t[k])
|
241
|
+
xp_assert_close(b(xx), splev(xy, (t, c, k)))
|
242
|
+
|
243
|
+
# Direct check
|
244
|
+
xx = [-1, 0, 0.5, 1]
|
245
|
+
xy = t[k] + (xx - t[k]) % (t[n] - t[k])
|
246
|
+
xp_assert_equal(b(xx, extrapolate='periodic'), b(xy, extrapolate=True))
|
247
|
+
|
248
|
+
def test_ppoly(self):
|
249
|
+
b = _make_random_spline()
|
250
|
+
t, c, k = b.tck
|
251
|
+
pp = PPoly.from_spline((t, c, k))
|
252
|
+
|
253
|
+
xx = np.linspace(t[k], t[-k], 100)
|
254
|
+
xp_assert_close(b(xx), pp(xx), atol=1e-14, rtol=1e-14)
|
255
|
+
|
256
|
+
def test_derivative_rndm(self):
|
257
|
+
b = _make_random_spline()
|
258
|
+
t, c, k = b.tck
|
259
|
+
xx = np.linspace(t[0], t[-1], 50)
|
260
|
+
xx = np.r_[xx, t]
|
261
|
+
|
262
|
+
for der in range(1, k+1):
|
263
|
+
yd = splev(xx, (t, c, k), der=der)
|
264
|
+
xp_assert_close(yd, b(xx, nu=der), atol=1e-14)
|
265
|
+
|
266
|
+
# higher derivatives all vanish
|
267
|
+
xp_assert_close(b(xx, nu=k+1), np.zeros_like(xx), atol=1e-14)
|
268
|
+
|
269
|
+
def test_derivative_jumps(self):
|
270
|
+
# example from de Boor, Chap IX, example (24)
|
271
|
+
# NB: knots augmented & corresp coefs are zeroed out
|
272
|
+
# in agreement with the convention (29)
|
273
|
+
k = 2
|
274
|
+
t = [-1, -1, 0, 1, 1, 3, 4, 6, 6, 6, 7, 7]
|
275
|
+
rng = np.random.RandomState(1234)
|
276
|
+
c = np.r_[0, 0, rng.random(5), 0, 0]
|
277
|
+
b = BSpline(t, c, k)
|
278
|
+
|
279
|
+
# b is continuous at x != 6 (triple knot)
|
280
|
+
x = np.asarray([1, 3, 4, 6])
|
281
|
+
xp_assert_close(b(x[x != 6] - 1e-10),
|
282
|
+
b(x[x != 6] + 1e-10))
|
283
|
+
assert not np.allclose(b(6.-1e-10), b(6+1e-10))
|
284
|
+
|
285
|
+
# 1st derivative jumps at double knots, 1 & 6:
|
286
|
+
x0 = np.asarray([3, 4])
|
287
|
+
xp_assert_close(b(x0 - 1e-10, nu=1),
|
288
|
+
b(x0 + 1e-10, nu=1))
|
289
|
+
x1 = np.asarray([1, 6])
|
290
|
+
assert not np.allclose(b(x1 - 1e-10, nu=1), b(x1 + 1e-10, nu=1))
|
291
|
+
|
292
|
+
# 2nd derivative is not guaranteed to be continuous either
|
293
|
+
assert not np.allclose(b(x - 1e-10, nu=2), b(x + 1e-10, nu=2))
|
294
|
+
|
295
|
+
def test_basis_element_quadratic(self):
|
296
|
+
xx = np.linspace(-1, 4, 20)
|
297
|
+
b = BSpline.basis_element(t=[0, 1, 2, 3])
|
298
|
+
xp_assert_close(b(xx),
|
299
|
+
splev(xx, (b.t, b.c, b.k)), atol=1e-14)
|
300
|
+
xp_assert_close(b(xx),
|
301
|
+
B_0123(xx), atol=1e-14)
|
302
|
+
|
303
|
+
b = BSpline.basis_element(t=[0, 1, 1, 2])
|
304
|
+
xx = np.linspace(0, 2, 10)
|
305
|
+
xp_assert_close(b(xx),
|
306
|
+
np.where(xx < 1, xx*xx, (2.-xx)**2), atol=1e-14)
|
307
|
+
|
308
|
+
def test_basis_element_rndm(self):
|
309
|
+
b = _make_random_spline()
|
310
|
+
t, c, k = b.tck
|
311
|
+
xx = np.linspace(t[k], t[-k-1], 20)
|
312
|
+
xp_assert_close(b(xx), _sum_basis_elements(xx, t, c, k), atol=1e-14)
|
313
|
+
|
314
|
+
def test_cmplx(self):
|
315
|
+
b = _make_random_spline()
|
316
|
+
t, c, k = b.tck
|
317
|
+
cc = c * (1. + 3.j)
|
318
|
+
|
319
|
+
b = BSpline(t, cc, k)
|
320
|
+
b_re = BSpline(t, b.c.real, k)
|
321
|
+
b_im = BSpline(t, b.c.imag, k)
|
322
|
+
|
323
|
+
xx = np.linspace(t[k], t[-k-1], 20)
|
324
|
+
xp_assert_close(b(xx).real, b_re(xx), atol=1e-14)
|
325
|
+
xp_assert_close(b(xx).imag, b_im(xx), atol=1e-14)
|
326
|
+
|
327
|
+
def test_nan(self):
|
328
|
+
# nan in, nan out.
|
329
|
+
b = BSpline.basis_element([0, 1, 1, 2])
|
330
|
+
assert np.isnan(b(np.nan))
|
331
|
+
|
332
|
+
def test_derivative_method(self):
|
333
|
+
b = _make_random_spline(k=5)
|
334
|
+
t, c, k = b.tck
|
335
|
+
b0 = BSpline(t, c, k)
|
336
|
+
xx = np.linspace(t[k], t[-k-1], 20)
|
337
|
+
for j in range(1, k):
|
338
|
+
b = b.derivative()
|
339
|
+
xp_assert_close(b0(xx, j), b(xx), atol=1e-12, rtol=1e-12)
|
340
|
+
|
341
|
+
def test_antiderivative_method(self):
|
342
|
+
b = _make_random_spline()
|
343
|
+
t, c, k = b.tck
|
344
|
+
xx = np.linspace(t[k], t[-k-1], 20)
|
345
|
+
xp_assert_close(b.antiderivative().derivative()(xx),
|
346
|
+
b(xx), atol=1e-14, rtol=1e-14)
|
347
|
+
|
348
|
+
# repeat with N-D array for c
|
349
|
+
c = np.c_[c, c, c]
|
350
|
+
c = np.dstack((c, c))
|
351
|
+
b = BSpline(t, c, k)
|
352
|
+
xp_assert_close(b.antiderivative().derivative()(xx),
|
353
|
+
b(xx), atol=1e-14, rtol=1e-14)
|
354
|
+
|
355
|
+
def test_integral(self):
|
356
|
+
b = BSpline.basis_element([0, 1, 2]) # x for x < 1 else 2 - x
|
357
|
+
xp_assert_close(b.integrate(0, 1), np.asarray(0.5))
|
358
|
+
xp_assert_close(b.integrate(1, 0), np.asarray(-1 * 0.5))
|
359
|
+
xp_assert_close(b.integrate(1, 0), np.asarray(-0.5))
|
360
|
+
|
361
|
+
# extrapolate or zeros outside of [0, 2]; default is yes
|
362
|
+
xp_assert_close(b.integrate(-1, 1), np.asarray(0.0))
|
363
|
+
xp_assert_close(b.integrate(-1, 1, extrapolate=True), np.asarray(0.0))
|
364
|
+
xp_assert_close(b.integrate(-1, 1, extrapolate=False), np.asarray(0.5))
|
365
|
+
xp_assert_close(b.integrate(1, -1, extrapolate=False), np.asarray(-1 * 0.5))
|
366
|
+
|
367
|
+
# Test ``_fitpack._splint()``
|
368
|
+
xp_assert_close(b.integrate(1, -1, extrapolate=False),
|
369
|
+
np.asarray(_impl.splint(1, -1, b.tck)))
|
370
|
+
|
371
|
+
# Test ``extrapolate='periodic'``.
|
372
|
+
b.extrapolate = 'periodic'
|
373
|
+
i = b.antiderivative()
|
374
|
+
period_int = np.asarray(i(2) - i(0))
|
375
|
+
|
376
|
+
xp_assert_close(b.integrate(0, 2), period_int)
|
377
|
+
xp_assert_close(b.integrate(2, 0), np.asarray(-1 * period_int))
|
378
|
+
xp_assert_close(b.integrate(-9, -7), period_int)
|
379
|
+
xp_assert_close(b.integrate(-8, -4), np.asarray(2 * period_int))
|
380
|
+
|
381
|
+
xp_assert_close(b.integrate(0.5, 1.5),
|
382
|
+
np.asarray(i(1.5) - i(0.5)))
|
383
|
+
xp_assert_close(b.integrate(1.5, 3),
|
384
|
+
np.asarray(i(1) - i(0) + i(2) - i(1.5)))
|
385
|
+
xp_assert_close(b.integrate(1.5 + 12, 3 + 12),
|
386
|
+
np.asarray(i(1) - i(0) + i(2) - i(1.5)))
|
387
|
+
xp_assert_close(b.integrate(1.5, 3 + 12),
|
388
|
+
np.asarray(i(1) - i(0) + i(2) - i(1.5) + 6 * period_int))
|
389
|
+
|
390
|
+
xp_assert_close(b.integrate(0, -1), np.asarray(i(0) - i(1)))
|
391
|
+
xp_assert_close(b.integrate(-9, -10), np.asarray(i(0) - i(1)))
|
392
|
+
xp_assert_close(b.integrate(0, -9),
|
393
|
+
np.asarray(i(1) - i(2) - 4 * period_int))
|
394
|
+
|
395
|
+
def test_integrate_ppoly(self):
|
396
|
+
# test .integrate method to be consistent with PPoly.integrate
|
397
|
+
x = [0, 1, 2, 3, 4]
|
398
|
+
b = make_interp_spline(x, x)
|
399
|
+
b.extrapolate = 'periodic'
|
400
|
+
p = PPoly.from_spline(b)
|
401
|
+
|
402
|
+
for x0, x1 in [(-5, 0.5), (0.5, 5), (-4, 13)]:
|
403
|
+
xp_assert_close(b.integrate(x0, x1),
|
404
|
+
p.integrate(x0, x1))
|
405
|
+
|
406
|
+
def test_integrate_0D_always(self):
|
407
|
+
# make sure the result is always a 0D array (not a python scalar)
|
408
|
+
b = BSpline.basis_element([0, 1, 2])
|
409
|
+
for extrapolate in (True, False):
|
410
|
+
res = b.integrate(0, 1, extrapolate=extrapolate)
|
411
|
+
assert isinstance(res, np.ndarray)
|
412
|
+
assert res.ndim == 0
|
413
|
+
|
414
|
+
def test_subclassing(self):
|
415
|
+
# classmethods should not decay to the base class
|
416
|
+
class B(BSpline):
|
417
|
+
pass
|
418
|
+
|
419
|
+
b = B.basis_element([0, 1, 2, 2])
|
420
|
+
assert b.__class__ == B
|
421
|
+
assert b.derivative().__class__ == B
|
422
|
+
assert b.antiderivative().__class__ == B
|
423
|
+
|
424
|
+
@pytest.mark.parametrize('axis', range(-4, 4))
|
425
|
+
def test_axis(self, axis):
|
426
|
+
n, k = 22, 3
|
427
|
+
t = np.linspace(0, 1, n + k + 1)
|
428
|
+
sh = [6, 7, 8]
|
429
|
+
# We need the positive axis for some of the indexing and slices used
|
430
|
+
# in this test.
|
431
|
+
pos_axis = axis % 4
|
432
|
+
sh.insert(pos_axis, n) # [22, 6, 7, 8] etc
|
433
|
+
sh = tuple(sh)
|
434
|
+
rng = np.random.RandomState(1234)
|
435
|
+
c = rng.random(size=sh)
|
436
|
+
b = BSpline(t, c, k, axis=axis)
|
437
|
+
assert b.c.shape == (sh[pos_axis],) + sh[:pos_axis] + sh[pos_axis+1:]
|
438
|
+
|
439
|
+
xp = rng.random((3, 4, 5))
|
440
|
+
assert b(xp).shape == sh[:pos_axis] + xp.shape + sh[pos_axis+1:]
|
441
|
+
|
442
|
+
# -c.ndim <= axis < c.ndim
|
443
|
+
for ax in [-c.ndim - 1, c.ndim]:
|
444
|
+
assert_raises(AxisError, BSpline,
|
445
|
+
**dict(t=t, c=c, k=k, axis=ax))
|
446
|
+
|
447
|
+
# derivative, antiderivative keeps the axis
|
448
|
+
for b1 in [BSpline(t, c, k, axis=axis).derivative(),
|
449
|
+
BSpline(t, c, k, axis=axis).derivative(2),
|
450
|
+
BSpline(t, c, k, axis=axis).antiderivative(),
|
451
|
+
BSpline(t, c, k, axis=axis).antiderivative(2)]:
|
452
|
+
assert b1.axis == b.axis
|
453
|
+
|
454
|
+
def test_neg_axis(self):
|
455
|
+
k = 2
|
456
|
+
t = [0, 1, 2, 3, 4, 5, 6]
|
457
|
+
c = np.array([[-1, 2, 0, -1], [2, 0, -3, 1]])
|
458
|
+
|
459
|
+
spl = BSpline(t, c, k, axis=-1)
|
460
|
+
spl0 = BSpline(t, c[0], k)
|
461
|
+
spl1 = BSpline(t, c[1], k)
|
462
|
+
xp_assert_equal(spl(2.5), [spl0(2.5), spl1(2.5)])
|
463
|
+
|
464
|
+
@pytest.mark.thread_unsafe
|
465
|
+
def test_design_matrix_bc_types(self):
|
466
|
+
'''
|
467
|
+
Splines with different boundary conditions are built on different
|
468
|
+
types of vectors of knots. As far as design matrix depends only on
|
469
|
+
vector of knots, `k` and `x` it is useful to make tests for different
|
470
|
+
boundary conditions (and as following different vectors of knots).
|
471
|
+
'''
|
472
|
+
def run_design_matrix_tests(n, k, bc_type):
|
473
|
+
'''
|
474
|
+
To avoid repetition of code the following function is provided.
|
475
|
+
'''
|
476
|
+
rng = np.random.RandomState(1234)
|
477
|
+
x = np.sort(rng.random_sample(n) * 40 - 20)
|
478
|
+
y = rng.random_sample(n) * 40 - 20
|
479
|
+
if bc_type == "periodic":
|
480
|
+
y[0] = y[-1]
|
481
|
+
|
482
|
+
bspl = make_interp_spline(x, y, k=k, bc_type=bc_type)
|
483
|
+
|
484
|
+
c = np.eye(len(bspl.t) - k - 1)
|
485
|
+
des_matr_def = BSpline(bspl.t, c, k)(x)
|
486
|
+
des_matr_csr = BSpline.design_matrix(x,
|
487
|
+
bspl.t,
|
488
|
+
k).toarray()
|
489
|
+
xp_assert_close(des_matr_csr @ bspl.c, y, atol=1e-14)
|
490
|
+
xp_assert_close(des_matr_def, des_matr_csr, atol=1e-14)
|
491
|
+
|
492
|
+
# "clamped" and "natural" work only with `k = 3`
|
493
|
+
n = 11
|
494
|
+
k = 3
|
495
|
+
for bc in ["clamped", "natural"]:
|
496
|
+
run_design_matrix_tests(n, k, bc)
|
497
|
+
|
498
|
+
# "not-a-knot" works with odd `k`
|
499
|
+
for k in range(3, 8, 2):
|
500
|
+
run_design_matrix_tests(n, k, "not-a-knot")
|
501
|
+
|
502
|
+
# "periodic" works with any `k` (even more than `n`)
|
503
|
+
n = 5 # smaller `n` to test `k > n` case
|
504
|
+
for k in range(2, 7):
|
505
|
+
run_design_matrix_tests(n, k, "periodic")
|
506
|
+
|
507
|
+
@pytest.mark.parametrize('extrapolate', [False, True, 'periodic'])
|
508
|
+
@pytest.mark.parametrize('degree', range(5))
|
509
|
+
def test_design_matrix_same_as_BSpline_call(self, extrapolate, degree):
|
510
|
+
"""Test that design_matrix(x) is equivalent to BSpline(..)(x)."""
|
511
|
+
rng = np.random.RandomState(1234)
|
512
|
+
x = rng.random_sample(10 * (degree + 1))
|
513
|
+
xmin, xmax = np.amin(x), np.amax(x)
|
514
|
+
k = degree
|
515
|
+
t = np.r_[np.linspace(xmin - 2, xmin - 1, degree),
|
516
|
+
np.linspace(xmin, xmax, 2 * (degree + 1)),
|
517
|
+
np.linspace(xmax + 1, xmax + 2, degree)]
|
518
|
+
c = np.eye(len(t) - k - 1)
|
519
|
+
bspline = BSpline(t, c, k, extrapolate)
|
520
|
+
xp_assert_close(
|
521
|
+
bspline(x), BSpline.design_matrix(x, t, k, extrapolate).toarray()
|
522
|
+
)
|
523
|
+
|
524
|
+
# extrapolation regime
|
525
|
+
x = np.array([xmin - 10, xmin - 1, xmax + 1.5, xmax + 10])
|
526
|
+
if not extrapolate:
|
527
|
+
with pytest.raises(ValueError):
|
528
|
+
BSpline.design_matrix(x, t, k, extrapolate)
|
529
|
+
else:
|
530
|
+
xp_assert_close(
|
531
|
+
bspline(x),
|
532
|
+
BSpline.design_matrix(x, t, k, extrapolate).toarray()
|
533
|
+
)
|
534
|
+
|
535
|
+
def test_design_matrix_x_shapes(self):
|
536
|
+
# test for different `x` shapes
|
537
|
+
rng = np.random.RandomState(1234)
|
538
|
+
n = 10
|
539
|
+
k = 3
|
540
|
+
x = np.sort(rng.random_sample(n) * 40 - 20)
|
541
|
+
y = rng.random_sample(n) * 40 - 20
|
542
|
+
|
543
|
+
bspl = make_interp_spline(x, y, k=k)
|
544
|
+
for i in range(1, 4):
|
545
|
+
xc = x[:i]
|
546
|
+
yc = y[:i]
|
547
|
+
des_matr_csr = BSpline.design_matrix(xc,
|
548
|
+
bspl.t,
|
549
|
+
k).toarray()
|
550
|
+
xp_assert_close(des_matr_csr @ bspl.c, yc, atol=1e-14)
|
551
|
+
|
552
|
+
def test_design_matrix_t_shapes(self):
|
553
|
+
# test for minimal possible `t` shape
|
554
|
+
t = [1., 1., 1., 2., 3., 4., 4., 4.]
|
555
|
+
des_matr = BSpline.design_matrix(2., t, 3).toarray()
|
556
|
+
xp_assert_close(des_matr,
|
557
|
+
[[0.25, 0.58333333, 0.16666667, 0.]],
|
558
|
+
atol=1e-14)
|
559
|
+
|
560
|
+
def test_design_matrix_asserts(self):
|
561
|
+
rng = np.random.RandomState(1234)
|
562
|
+
n = 10
|
563
|
+
k = 3
|
564
|
+
x = np.sort(rng.random_sample(n) * 40 - 20)
|
565
|
+
y = rng.random_sample(n) * 40 - 20
|
566
|
+
bspl = make_interp_spline(x, y, k=k)
|
567
|
+
# invalid vector of knots (should be a 1D non-descending array)
|
568
|
+
# here the actual vector of knots is reversed, so it is invalid
|
569
|
+
with assert_raises(ValueError):
|
570
|
+
BSpline.design_matrix(x, bspl.t[::-1], k)
|
571
|
+
k = 2
|
572
|
+
t = [0., 1., 2., 3., 4., 5.]
|
573
|
+
x = [1., 2., 3., 4.]
|
574
|
+
# out of bounds
|
575
|
+
with assert_raises(ValueError):
|
576
|
+
BSpline.design_matrix(x, t, k)
|
577
|
+
|
578
|
+
@pytest.mark.parametrize('bc_type', ['natural', 'clamped',
|
579
|
+
'periodic', 'not-a-knot'])
|
580
|
+
def test_from_power_basis(self, bc_type):
|
581
|
+
rng = np.random.RandomState(1234)
|
582
|
+
x = np.sort(rng.random(20))
|
583
|
+
y = rng.random(20)
|
584
|
+
if bc_type == 'periodic':
|
585
|
+
y[-1] = y[0]
|
586
|
+
cb = CubicSpline(x, y, bc_type=bc_type)
|
587
|
+
bspl = BSpline.from_power_basis(cb, bc_type=bc_type)
|
588
|
+
xx = np.linspace(0, 1, 20)
|
589
|
+
xp_assert_close(cb(xx), bspl(xx), atol=1e-15)
|
590
|
+
bspl_new = make_interp_spline(x, y, bc_type=bc_type)
|
591
|
+
xp_assert_close(bspl.c, bspl_new.c, atol=1e-15)
|
592
|
+
|
593
|
+
@pytest.mark.parametrize('bc_type', ['natural', 'clamped',
|
594
|
+
'periodic', 'not-a-knot'])
|
595
|
+
def test_from_power_basis_complex(self, bc_type):
|
596
|
+
rng = np.random.RandomState(1234)
|
597
|
+
x = np.sort(rng.random(20))
|
598
|
+
y = rng.random(20) + rng.random(20) * 1j
|
599
|
+
if bc_type == 'periodic':
|
600
|
+
y[-1] = y[0]
|
601
|
+
cb = CubicSpline(x, y, bc_type=bc_type)
|
602
|
+
bspl = BSpline.from_power_basis(cb, bc_type=bc_type)
|
603
|
+
bspl_new_real = make_interp_spline(x, y.real, bc_type=bc_type)
|
604
|
+
bspl_new_imag = make_interp_spline(x, y.imag, bc_type=bc_type)
|
605
|
+
xp_assert_close(bspl.c, bspl_new_real.c + 1j * bspl_new_imag.c, atol=1e-15)
|
606
|
+
|
607
|
+
def test_from_power_basis_exmp(self):
|
608
|
+
'''
|
609
|
+
For x = [0, 1, 2, 3, 4] and y = [1, 1, 1, 1, 1]
|
610
|
+
the coefficients of Cubic Spline in the power basis:
|
611
|
+
|
612
|
+
$[[0, 0, 0, 0, 0],\\$
|
613
|
+
$[0, 0, 0, 0, 0],\\$
|
614
|
+
$[0, 0, 0, 0, 0],\\$
|
615
|
+
$[1, 1, 1, 1, 1]]$
|
616
|
+
|
617
|
+
It could be shown explicitly that coefficients of the interpolating
|
618
|
+
function in B-spline basis are c = [1, 1, 1, 1, 1, 1, 1]
|
619
|
+
'''
|
620
|
+
x = np.array([0, 1, 2, 3, 4])
|
621
|
+
y = np.array([1, 1, 1, 1, 1])
|
622
|
+
bspl = BSpline.from_power_basis(CubicSpline(x, y, bc_type='natural'),
|
623
|
+
bc_type='natural')
|
624
|
+
xp_assert_close(bspl.c, [1.0, 1, 1, 1, 1, 1, 1], atol=1e-15)
|
625
|
+
|
626
|
+
def test_read_only(self):
|
627
|
+
# BSpline must work on read-only knots and coefficients.
|
628
|
+
t = np.array([0, 1])
|
629
|
+
c = np.array([3.0])
|
630
|
+
t.setflags(write=False)
|
631
|
+
c.setflags(write=False)
|
632
|
+
|
633
|
+
xx = np.linspace(0, 1, 10)
|
634
|
+
xx.setflags(write=False)
|
635
|
+
|
636
|
+
b = BSpline(t=t, c=c, k=0)
|
637
|
+
xp_assert_close(b(xx), np.ones_like(xx) * 3.0)
|
638
|
+
|
639
|
+
@pytest.mark.thread_unsafe
|
640
|
+
def test_concurrency(self):
|
641
|
+
# Check that no segfaults appear with concurrent access to BSpline
|
642
|
+
b = _make_random_spline()
|
643
|
+
|
644
|
+
def worker_fn(_, b):
|
645
|
+
t, _, k = b.tck
|
646
|
+
xx = np.linspace(t[k], t[-k-1], 10000)
|
647
|
+
b(xx)
|
648
|
+
|
649
|
+
_run_concurrent_barrier(10, worker_fn, b)
|
650
|
+
|
651
|
+
|
652
|
+
def test_memmap(self, tmpdir):
|
653
|
+
# Make sure that memmaps can be used as t and c atrributes after the
|
654
|
+
# spline has been constructed. This is similar to what happens in a
|
655
|
+
# scikit-learn context, where joblib can create read-only memmap to
|
656
|
+
# share objects between workers. For more details, see
|
657
|
+
# https://github.com/scipy/scipy/issues/22143
|
658
|
+
b = _make_random_spline()
|
659
|
+
xx = np.linspace(0, 1, 10)
|
660
|
+
|
661
|
+
expected = b(xx)
|
662
|
+
|
663
|
+
tid = threading.get_native_id()
|
664
|
+
t_mm = np.memmap(str(tmpdir.join(f't{tid}.dat')), mode='w+',
|
665
|
+
dtype=b.t.dtype, shape=b.t.shape)
|
666
|
+
t_mm[:] = b.t
|
667
|
+
c_mm = np.memmap(str(tmpdir.join(f'c{tid}.dat')), mode='w+',
|
668
|
+
dtype=b.c.dtype, shape=b.c.shape)
|
669
|
+
c_mm[:] = b.c
|
670
|
+
b.t = t_mm
|
671
|
+
b.c = c_mm
|
672
|
+
|
673
|
+
xp_assert_close(b(xx), expected)
|
674
|
+
|
675
|
+
|
676
|
+
class TestInsert:
|
677
|
+
|
678
|
+
@pytest.mark.parametrize('xval', [0.0, 1.0, 2.5, 4, 6.5, 7.0])
|
679
|
+
def test_insert(self, xval):
|
680
|
+
# insert a knot, incl edges (0.0, 7.0) and exactly at an existing knot (4.0)
|
681
|
+
x = np.arange(8)
|
682
|
+
y = np.sin(x)**3
|
683
|
+
spl = make_interp_spline(x, y, k=3)
|
684
|
+
|
685
|
+
spl_1f = insert(xval, spl) # FITPACK
|
686
|
+
spl_1 = spl.insert_knot(xval)
|
687
|
+
|
688
|
+
xp_assert_close(spl_1.t, spl_1f.t, atol=1e-15)
|
689
|
+
xp_assert_close(spl_1.c, spl_1f.c[:-spl.k-1], atol=1e-15)
|
690
|
+
|
691
|
+
# knot insertion preserves values, unless multiplicity >= k+1
|
692
|
+
xx = x if xval != x[-1] else x[:-1]
|
693
|
+
xx = np.r_[xx, 0.5*(x[1:] + x[:-1])]
|
694
|
+
xp_assert_close(spl(xx), spl_1(xx), atol=1e-15)
|
695
|
+
|
696
|
+
# ... repeat with ndim > 1
|
697
|
+
y1 = np.cos(x)**3
|
698
|
+
spl_y1 = make_interp_spline(x, y1, k=3)
|
699
|
+
spl_yy = make_interp_spline(x, np.c_[y, y1], k=3)
|
700
|
+
spl_yy1 = spl_yy.insert_knot(xval)
|
701
|
+
|
702
|
+
xp_assert_close(spl_yy1.t, spl_1.t, atol=1e-15)
|
703
|
+
xp_assert_close(spl_yy1.c, np.c_[spl.insert_knot(xval).c,
|
704
|
+
spl_y1.insert_knot(xval).c], atol=1e-15)
|
705
|
+
|
706
|
+
xx = x if xval != x[-1] else x[:-1]
|
707
|
+
xx = np.r_[xx, 0.5*(x[1:] + x[:-1])]
|
708
|
+
xp_assert_close(spl_yy(xx), spl_yy1(xx), atol=1e-15)
|
709
|
+
|
710
|
+
|
711
|
+
@pytest.mark.parametrize(
|
712
|
+
'xval, m', [(0.0, 2), (1.0, 3), (1.5, 5), (4, 2), (7.0, 2)]
|
713
|
+
)
|
714
|
+
def test_insert_multi(self, xval, m):
|
715
|
+
x = np.arange(8)
|
716
|
+
y = np.sin(x)**3
|
717
|
+
spl = make_interp_spline(x, y, k=3)
|
718
|
+
|
719
|
+
spl_1f = insert(xval, spl, m=m)
|
720
|
+
spl_1 = spl.insert_knot(xval, m)
|
721
|
+
|
722
|
+
xp_assert_close(spl_1.t, spl_1f.t, atol=1e-15)
|
723
|
+
xp_assert_close(spl_1.c, spl_1f.c[:-spl.k-1], atol=1e-15)
|
724
|
+
|
725
|
+
xx = x if xval != x[-1] else x[:-1]
|
726
|
+
xx = np.r_[xx, 0.5*(x[1:] + x[:-1])]
|
727
|
+
xp_assert_close(spl(xx), spl_1(xx), atol=1e-15)
|
728
|
+
|
729
|
+
def test_insert_random(self):
|
730
|
+
rng = np.random.default_rng(12345)
|
731
|
+
n, k = 11, 3
|
732
|
+
|
733
|
+
t = np.sort(rng.uniform(size=n+k+1))
|
734
|
+
c = rng.uniform(size=(n, 3, 2))
|
735
|
+
spl = BSpline(t, c, k)
|
736
|
+
|
737
|
+
xv = rng.uniform(low=t[k+1], high=t[-k-1])
|
738
|
+
spl_1 = spl.insert_knot(xv)
|
739
|
+
|
740
|
+
xx = rng.uniform(low=t[k+1], high=t[-k-1], size=33)
|
741
|
+
xp_assert_close(spl(xx), spl_1(xx), atol=1e-15)
|
742
|
+
|
743
|
+
@pytest.mark.parametrize('xv', [0, 0.1, 2.0, 4.0, 4.5, # l.h. edge
|
744
|
+
5.5, 6.0, 6.1, 7.0] # r.h. edge
|
745
|
+
)
|
746
|
+
def test_insert_periodic(self, xv):
|
747
|
+
x = np.arange(8)
|
748
|
+
y = np.sin(x)**3
|
749
|
+
tck = splrep(x, y, k=3)
|
750
|
+
spl = BSpline(*tck, extrapolate="periodic")
|
751
|
+
|
752
|
+
spl_1 = spl.insert_knot(xv)
|
753
|
+
tf, cf, k = insert(xv, spl.tck, per=True)
|
754
|
+
|
755
|
+
xp_assert_close(spl_1.t, tf, atol=1e-15)
|
756
|
+
xp_assert_close(spl_1.c[:-k-1], cf[:-k-1], atol=1e-15)
|
757
|
+
|
758
|
+
xx = np.random.default_rng(1234).uniform(low=0, high=7, size=41)
|
759
|
+
xp_assert_close(spl_1(xx), splev(xx, (tf, cf, k)), atol=1e-15)
|
760
|
+
|
761
|
+
@pytest.mark.parametrize('extrapolate', [None, 'periodic'])
|
762
|
+
def test_complex(self, extrapolate):
|
763
|
+
x = np.arange(8)*2*np.pi
|
764
|
+
y_re, y_im = np.sin(x), np.cos(x)
|
765
|
+
|
766
|
+
spl = make_interp_spline(x, y_re + 1j*y_im, k=3)
|
767
|
+
spl.extrapolate = extrapolate
|
768
|
+
|
769
|
+
spl_re = make_interp_spline(x, y_re, k=3)
|
770
|
+
spl_re.extrapolate = extrapolate
|
771
|
+
|
772
|
+
spl_im = make_interp_spline(x, y_im, k=3)
|
773
|
+
spl_im.extrapolate = extrapolate
|
774
|
+
|
775
|
+
xv = 3.5
|
776
|
+
spl_1 = spl.insert_knot(xv)
|
777
|
+
spl_1re = spl_re.insert_knot(xv)
|
778
|
+
spl_1im = spl_im.insert_knot(xv)
|
779
|
+
|
780
|
+
xp_assert_close(spl_1.t, spl_1re.t, atol=1e-15)
|
781
|
+
xp_assert_close(spl_1.t, spl_1im.t, atol=1e-15)
|
782
|
+
xp_assert_close(spl_1.c, spl_1re.c + 1j*spl_1im.c, atol=1e-15)
|
783
|
+
|
784
|
+
def test_insert_periodic_too_few_internal_knots(self):
|
785
|
+
# both FITPACK and spl.insert_knot raise when there's not enough
|
786
|
+
# internal knots to make a periodic extension.
|
787
|
+
# Below the internal knots are 2, 3, , 4, 5
|
788
|
+
# ^
|
789
|
+
# 2, 3, 3.5, 4, 5
|
790
|
+
# so two knots from each side from the new one, while need at least
|
791
|
+
# from either left or right.
|
792
|
+
xv = 3.5
|
793
|
+
k = 3
|
794
|
+
t = np.array([0]*(k+1) + [2, 3, 4, 5] + [7]*(k+1))
|
795
|
+
c = np.ones(len(t) - k - 1)
|
796
|
+
spl = BSpline(t, c, k, extrapolate="periodic")
|
797
|
+
|
798
|
+
with assert_raises(ValueError):
|
799
|
+
insert(xv, (t, c, k), per=True)
|
800
|
+
|
801
|
+
with assert_raises(ValueError):
|
802
|
+
spl.insert_knot(xv)
|
803
|
+
|
804
|
+
def test_insert_no_extrap(self):
|
805
|
+
k = 3
|
806
|
+
t = np.array([0]*(k+1) + [2, 3, 4, 5] + [7]*(k+1))
|
807
|
+
c = np.ones(len(t) - k - 1)
|
808
|
+
spl = BSpline(t, c, k)
|
809
|
+
|
810
|
+
with assert_raises(ValueError):
|
811
|
+
spl.insert_knot(-1)
|
812
|
+
|
813
|
+
with assert_raises(ValueError):
|
814
|
+
spl.insert_knot(8)
|
815
|
+
|
816
|
+
with assert_raises(ValueError):
|
817
|
+
spl.insert_knot(3, m=0)
|
818
|
+
|
819
|
+
|
820
|
+
def test_knots_multiplicity():
|
821
|
+
# Take a spline w/ random coefficients, throw in knots of varying
|
822
|
+
# multiplicity.
|
823
|
+
|
824
|
+
def check_splev(b, j, der=0, atol=1e-14, rtol=1e-14):
|
825
|
+
# check evaluations against FITPACK, incl extrapolations
|
826
|
+
t, c, k = b.tck
|
827
|
+
x = np.unique(t)
|
828
|
+
x = np.r_[t[0]-0.1, 0.5*(x[1:] + x[:1]), t[-1]+0.1]
|
829
|
+
xp_assert_close(splev(x, (t, c, k), der), b(x, der),
|
830
|
+
atol=atol, rtol=rtol, err_msg=f'der = {der} k = {b.k}')
|
831
|
+
|
832
|
+
# test loop itself
|
833
|
+
# [the index `j` is for interpreting the traceback in case of a failure]
|
834
|
+
for k in [1, 2, 3, 4, 5]:
|
835
|
+
b = _make_random_spline(k=k)
|
836
|
+
for j, b1 in enumerate(_make_multiples(b)):
|
837
|
+
check_splev(b1, j)
|
838
|
+
for der in range(1, k+1):
|
839
|
+
check_splev(b1, j, der, 1e-12, 1e-12)
|
840
|
+
|
841
|
+
|
842
|
+
### stolen from @pv, verbatim
|
843
|
+
def _naive_B(x, k, i, t):
|
844
|
+
"""
|
845
|
+
Naive way to compute B-spline basis functions. Useful only for testing!
|
846
|
+
computes B(x; t[i],..., t[i+k+1])
|
847
|
+
"""
|
848
|
+
if k == 0:
|
849
|
+
return 1.0 if t[i] <= x < t[i+1] else 0.0
|
850
|
+
if t[i+k] == t[i]:
|
851
|
+
c1 = 0.0
|
852
|
+
else:
|
853
|
+
c1 = (x - t[i])/(t[i+k] - t[i]) * _naive_B(x, k-1, i, t)
|
854
|
+
if t[i+k+1] == t[i+1]:
|
855
|
+
c2 = 0.0
|
856
|
+
else:
|
857
|
+
c2 = (t[i+k+1] - x)/(t[i+k+1] - t[i+1]) * _naive_B(x, k-1, i+1, t)
|
858
|
+
return (c1 + c2)
|
859
|
+
|
860
|
+
|
861
|
+
### stolen from @pv, verbatim
|
862
|
+
def _naive_eval(x, t, c, k):
|
863
|
+
"""
|
864
|
+
Naive B-spline evaluation. Useful only for testing!
|
865
|
+
"""
|
866
|
+
if x == t[k]:
|
867
|
+
i = k
|
868
|
+
else:
|
869
|
+
i = np.searchsorted(t, x) - 1
|
870
|
+
assert t[i] <= x <= t[i+1]
|
871
|
+
assert i >= k and i < len(t) - k
|
872
|
+
return sum(c[i-j] * _naive_B(x, k, i-j, t) for j in range(0, k+1))
|
873
|
+
|
874
|
+
|
875
|
+
def _naive_eval_2(x, t, c, k):
|
876
|
+
"""Naive B-spline evaluation, another way."""
|
877
|
+
n = len(t) - (k+1)
|
878
|
+
assert n >= k+1
|
879
|
+
assert len(c) >= n
|
880
|
+
assert t[k] <= x <= t[n]
|
881
|
+
return sum(c[i] * _naive_B(x, k, i, t) for i in range(n))
|
882
|
+
|
883
|
+
|
884
|
+
def _sum_basis_elements(x, t, c, k):
|
885
|
+
n = len(t) - (k+1)
|
886
|
+
assert n >= k+1
|
887
|
+
assert len(c) >= n
|
888
|
+
s = 0.
|
889
|
+
for i in range(n):
|
890
|
+
b = BSpline.basis_element(t[i:i+k+2], extrapolate=False)(x)
|
891
|
+
s += c[i] * np.nan_to_num(b) # zero out out-of-bounds elements
|
892
|
+
return s
|
893
|
+
|
894
|
+
|
895
|
+
def B_012(x):
|
896
|
+
""" A linear B-spline function B(x | 0, 1, 2)."""
|
897
|
+
x = np.atleast_1d(x)
|
898
|
+
return np.piecewise(x, [(x < 0) | (x > 2),
|
899
|
+
(x >= 0) & (x < 1),
|
900
|
+
(x >= 1) & (x <= 2)],
|
901
|
+
[lambda x: 0., lambda x: x, lambda x: 2.-x])
|
902
|
+
|
903
|
+
|
904
|
+
def B_0123(x, der=0):
|
905
|
+
"""A quadratic B-spline function B(x | 0, 1, 2, 3)."""
|
906
|
+
x = np.atleast_1d(x)
|
907
|
+
conds = [x < 1, (x > 1) & (x < 2), x > 2]
|
908
|
+
if der == 0:
|
909
|
+
funcs = [lambda x: x*x/2.,
|
910
|
+
lambda x: 3./4 - (x-3./2)**2,
|
911
|
+
lambda x: (3.-x)**2 / 2]
|
912
|
+
elif der == 2:
|
913
|
+
funcs = [lambda x: 1.,
|
914
|
+
lambda x: -2.,
|
915
|
+
lambda x: 1.]
|
916
|
+
else:
|
917
|
+
raise ValueError(f'never be here: der={der}')
|
918
|
+
pieces = np.piecewise(x, conds, funcs)
|
919
|
+
return pieces
|
920
|
+
|
921
|
+
|
922
|
+
def _make_random_spline(n=35, k=3):
|
923
|
+
rng = np.random.RandomState(123)
|
924
|
+
t = np.sort(rng.random(n+k+1))
|
925
|
+
c = rng.random(n)
|
926
|
+
return BSpline.construct_fast(t, c, k)
|
927
|
+
|
928
|
+
|
929
|
+
def _make_multiples(b):
|
930
|
+
"""Increase knot multiplicity."""
|
931
|
+
c, k = b.c, b.k
|
932
|
+
|
933
|
+
t1 = b.t.copy()
|
934
|
+
t1[17:19] = t1[17]
|
935
|
+
t1[22] = t1[21]
|
936
|
+
yield BSpline(t1, c, k)
|
937
|
+
|
938
|
+
t1 = b.t.copy()
|
939
|
+
t1[:k+1] = t1[0]
|
940
|
+
yield BSpline(t1, c, k)
|
941
|
+
|
942
|
+
t1 = b.t.copy()
|
943
|
+
t1[-k-1:] = t1[-1]
|
944
|
+
yield BSpline(t1, c, k)
|
945
|
+
|
946
|
+
|
947
|
+
class TestInterop:
|
948
|
+
#
|
949
|
+
# Test that FITPACK-based spl* functions can deal with BSpline objects
|
950
|
+
#
|
951
|
+
def setup_method(self):
|
952
|
+
xx = np.linspace(0, 4.*np.pi, 41)
|
953
|
+
yy = np.cos(xx)
|
954
|
+
b = make_interp_spline(xx, yy)
|
955
|
+
self.tck = (b.t, b.c, b.k)
|
956
|
+
self.xx, self.yy, self.b = xx, yy, b
|
957
|
+
|
958
|
+
self.xnew = np.linspace(0, 4.*np.pi, 21)
|
959
|
+
|
960
|
+
c2 = np.c_[b.c, b.c, b.c]
|
961
|
+
self.c2 = np.dstack((c2, c2))
|
962
|
+
self.b2 = BSpline(b.t, self.c2, b.k)
|
963
|
+
|
964
|
+
def test_splev(self):
|
965
|
+
xnew, b, b2 = self.xnew, self.b, self.b2
|
966
|
+
|
967
|
+
# check that splev works with 1-D array of coefficients
|
968
|
+
# for array and scalar `x`
|
969
|
+
xp_assert_close(splev(xnew, b),
|
970
|
+
b(xnew), atol=1e-15, rtol=1e-15)
|
971
|
+
xp_assert_close(splev(xnew, b.tck),
|
972
|
+
b(xnew), atol=1e-15, rtol=1e-15)
|
973
|
+
xp_assert_close(np.asarray([splev(x, b) for x in xnew]),
|
974
|
+
b(xnew), atol=1e-15, rtol=1e-15)
|
975
|
+
|
976
|
+
# With N-D coefficients, there's a quirck:
|
977
|
+
# splev(x, BSpline) is equivalent to BSpline(x)
|
978
|
+
with assert_raises(ValueError, match="Calling splev.. with BSpline"):
|
979
|
+
splev(xnew, b2)
|
980
|
+
|
981
|
+
# However, splev(x, BSpline.tck) needs some transposes. This is because
|
982
|
+
# BSpline interpolates along the first axis, while the legacy FITPACK
|
983
|
+
# wrapper does list(map(...)) which effectively interpolates along the
|
984
|
+
# last axis. Like so:
|
985
|
+
sh = tuple(range(1, b2.c.ndim)) + (0,) # sh = (1, 2, 0)
|
986
|
+
cc = b2.c.transpose(sh)
|
987
|
+
tck = (b2.t, cc, b2.k)
|
988
|
+
xp_assert_close(np.asarray(splev(xnew, tck)),
|
989
|
+
b2(xnew).transpose(sh), atol=1e-15, rtol=1e-15)
|
990
|
+
|
991
|
+
def test_splrep(self):
|
992
|
+
x, y = self.xx, self.yy
|
993
|
+
# test that "new" splrep is equivalent to _impl.splrep
|
994
|
+
tck = splrep(x, y)
|
995
|
+
t, c, k = _impl.splrep(x, y)
|
996
|
+
xp_assert_close(tck[0], t, atol=1e-15)
|
997
|
+
xp_assert_close(tck[1], c, atol=1e-15)
|
998
|
+
assert tck[2] == k
|
999
|
+
|
1000
|
+
# also cover the `full_output=True` branch
|
1001
|
+
tck_f, _, _, _ = splrep(x, y, full_output=True)
|
1002
|
+
xp_assert_close(tck_f[0], t, atol=1e-15)
|
1003
|
+
xp_assert_close(tck_f[1], c, atol=1e-15)
|
1004
|
+
assert tck_f[2] == k
|
1005
|
+
|
1006
|
+
# test that the result of splrep roundtrips with splev:
|
1007
|
+
# evaluate the spline on the original `x` points
|
1008
|
+
yy = splev(x, tck)
|
1009
|
+
xp_assert_close(y, yy, atol=1e-15)
|
1010
|
+
|
1011
|
+
# ... and also it roundtrips if wrapped in a BSpline
|
1012
|
+
b = BSpline(*tck)
|
1013
|
+
xp_assert_close(y, b(x), atol=1e-15)
|
1014
|
+
|
1015
|
+
def test_splrep_errors(self):
|
1016
|
+
# test that both "old" and "new" splrep raise for an N-D ``y`` array
|
1017
|
+
# with n > 1
|
1018
|
+
x, y = self.xx, self.yy
|
1019
|
+
y2 = np.c_[y, y]
|
1020
|
+
with assert_raises(ValueError):
|
1021
|
+
splrep(x, y2)
|
1022
|
+
with assert_raises(ValueError):
|
1023
|
+
_impl.splrep(x, y2)
|
1024
|
+
|
1025
|
+
# input below minimum size
|
1026
|
+
with assert_raises(TypeError, match="m > k must hold"):
|
1027
|
+
splrep(x[:3], y[:3])
|
1028
|
+
with assert_raises(TypeError, match="m > k must hold"):
|
1029
|
+
_impl.splrep(x[:3], y[:3])
|
1030
|
+
|
1031
|
+
def test_splprep(self):
|
1032
|
+
x = np.arange(15, dtype=np.float64).reshape((3, 5))
|
1033
|
+
b, u = splprep(x)
|
1034
|
+
tck, u1 = _impl.splprep(x)
|
1035
|
+
|
1036
|
+
# test the roundtrip with splev for both "old" and "new" output
|
1037
|
+
xp_assert_close(u, u1, atol=1e-15)
|
1038
|
+
xp_assert_close(np.asarray(splev(u, b)), x, atol=1e-15)
|
1039
|
+
xp_assert_close(np.asarray(splev(u, tck)), x, atol=1e-15)
|
1040
|
+
|
1041
|
+
# cover the ``full_output=True`` branch
|
1042
|
+
(b_f, u_f), _, _, _ = splprep(x, s=0, full_output=True)
|
1043
|
+
xp_assert_close(u, u_f, atol=1e-15)
|
1044
|
+
xp_assert_close(np.asarray(splev(u_f, b_f)), x, atol=1e-15)
|
1045
|
+
|
1046
|
+
def test_splprep_errors(self):
|
1047
|
+
# test that both "old" and "new" code paths raise for x.ndim > 2
|
1048
|
+
x = np.arange(3*4*5).reshape((3, 4, 5))
|
1049
|
+
with assert_raises(ValueError, match="too many values to unpack"):
|
1050
|
+
splprep(x)
|
1051
|
+
with assert_raises(ValueError, match="too many values to unpack"):
|
1052
|
+
_impl.splprep(x)
|
1053
|
+
|
1054
|
+
# input below minimum size
|
1055
|
+
x = np.linspace(0, 40, num=3)
|
1056
|
+
with assert_raises(TypeError, match="m > k must hold"):
|
1057
|
+
splprep([x])
|
1058
|
+
with assert_raises(TypeError, match="m > k must hold"):
|
1059
|
+
_impl.splprep([x])
|
1060
|
+
|
1061
|
+
# automatically calculated parameters are non-increasing
|
1062
|
+
# see gh-7589
|
1063
|
+
x = [-50.49072266, -50.49072266, -54.49072266, -54.49072266]
|
1064
|
+
with assert_raises(ValueError, match="Invalid inputs"):
|
1065
|
+
splprep([x])
|
1066
|
+
with assert_raises(ValueError, match="Invalid inputs"):
|
1067
|
+
_impl.splprep([x])
|
1068
|
+
|
1069
|
+
# given non-increasing parameter values u
|
1070
|
+
x = [1, 3, 2, 4]
|
1071
|
+
u = [0, 0.3, 0.2, 1]
|
1072
|
+
with assert_raises(ValueError, match="Invalid inputs"):
|
1073
|
+
splprep(*[[x], None, u])
|
1074
|
+
|
1075
|
+
def test_sproot(self):
|
1076
|
+
b, b2 = self.b, self.b2
|
1077
|
+
roots = np.array([0.5, 1.5, 2.5, 3.5])*np.pi
|
1078
|
+
# sproot accepts a BSpline obj w/ 1-D coef array
|
1079
|
+
xp_assert_close(sproot(b), roots, atol=1e-7, rtol=1e-7)
|
1080
|
+
xp_assert_close(sproot((b.t, b.c, b.k)), roots, atol=1e-7, rtol=1e-7)
|
1081
|
+
|
1082
|
+
# ... and deals with trailing dimensions if coef array is N-D
|
1083
|
+
with assert_raises(ValueError, match="Calling sproot.. with BSpline"):
|
1084
|
+
sproot(b2, mest=50)
|
1085
|
+
|
1086
|
+
# and legacy behavior is preserved for a tck tuple w/ N-D coef
|
1087
|
+
c2r = b2.c.transpose(1, 2, 0)
|
1088
|
+
rr = np.asarray(sproot((b2.t, c2r, b2.k), mest=50))
|
1089
|
+
assert rr.shape == (3, 2, 4)
|
1090
|
+
xp_assert_close(rr - roots, np.zeros_like(rr), atol=1e-12)
|
1091
|
+
|
1092
|
+
def test_splint(self):
|
1093
|
+
# test that splint accepts BSpline objects
|
1094
|
+
b, b2 = self.b, self.b2
|
1095
|
+
|
1096
|
+
xp_assert_close(splint(0, 1, b),
|
1097
|
+
splint(0, 1, b.tck), atol=1e-14, check_0d=False)
|
1098
|
+
xp_assert_close(splint(0, 1, b),
|
1099
|
+
b.integrate(0, 1), atol=1e-14, check_0d=False)
|
1100
|
+
|
1101
|
+
# ... and deals with N-D arrays of coefficients
|
1102
|
+
with assert_raises(ValueError, match="Calling splint.. with BSpline"):
|
1103
|
+
splint(0, 1, b2)
|
1104
|
+
|
1105
|
+
# and the legacy behavior is preserved for a tck tuple w/ N-D coef
|
1106
|
+
c2r = b2.c.transpose(1, 2, 0)
|
1107
|
+
integr = np.asarray(splint(0, 1, (b2.t, c2r, b2.k)))
|
1108
|
+
assert integr.shape == (3, 2)
|
1109
|
+
xp_assert_close(integr,
|
1110
|
+
splint(0, 1, b), atol=1e-14, check_shape=False)
|
1111
|
+
|
1112
|
+
def test_splder(self):
|
1113
|
+
for b in [self.b, self.b2]:
|
1114
|
+
# pad the c array (FITPACK convention)
|
1115
|
+
ct = len(b.t) - len(b.c)
|
1116
|
+
b_c = b.c.copy()
|
1117
|
+
if ct > 0:
|
1118
|
+
b_c = np.r_[b_c, np.zeros((ct,) + b_c.shape[1:])]
|
1119
|
+
|
1120
|
+
for n in [1, 2, 3]:
|
1121
|
+
bd = splder(b)
|
1122
|
+
tck_d = _impl.splder((b.t.copy(), b_c, b.k))
|
1123
|
+
xp_assert_close(bd.t, tck_d[0], atol=1e-15)
|
1124
|
+
xp_assert_close(bd.c, tck_d[1], atol=1e-15)
|
1125
|
+
assert bd.k == tck_d[2]
|
1126
|
+
assert isinstance(bd, BSpline)
|
1127
|
+
assert isinstance(tck_d, tuple) # back-compat: tck in and out
|
1128
|
+
|
1129
|
+
def test_splantider(self):
|
1130
|
+
for b in [self.b, self.b2]:
|
1131
|
+
# pad the c array (FITPACK convention)
|
1132
|
+
ct = len(b.t) - len(b.c)
|
1133
|
+
b_c = b.c.copy()
|
1134
|
+
if ct > 0:
|
1135
|
+
b_c = np.r_[b_c, np.zeros((ct,) + b_c.shape[1:])]
|
1136
|
+
|
1137
|
+
for n in [1, 2, 3]:
|
1138
|
+
bd = splantider(b)
|
1139
|
+
tck_d = _impl.splantider((b.t.copy(), b_c, b.k))
|
1140
|
+
xp_assert_close(bd.t, tck_d[0], atol=1e-15)
|
1141
|
+
xp_assert_close(bd.c, tck_d[1], atol=1e-15)
|
1142
|
+
assert bd.k == tck_d[2]
|
1143
|
+
assert isinstance(bd, BSpline)
|
1144
|
+
assert isinstance(tck_d, tuple) # back-compat: tck in and out
|
1145
|
+
|
1146
|
+
def test_insert(self):
|
1147
|
+
b, b2, xx = self.b, self.b2, self.xx
|
1148
|
+
|
1149
|
+
j = b.t.size // 2
|
1150
|
+
tn = 0.5*(b.t[j] + b.t[j+1])
|
1151
|
+
|
1152
|
+
bn, tck_n = insert(tn, b), insert(tn, (b.t, b.c, b.k))
|
1153
|
+
xp_assert_close(splev(xx, bn),
|
1154
|
+
splev(xx, tck_n), atol=1e-15)
|
1155
|
+
assert isinstance(bn, BSpline)
|
1156
|
+
assert isinstance(tck_n, tuple) # back-compat: tck in, tck out
|
1157
|
+
|
1158
|
+
# for N-D array of coefficients, BSpline.c needs to be transposed
|
1159
|
+
# after that, the results are equivalent.
|
1160
|
+
sh = tuple(range(b2.c.ndim))
|
1161
|
+
c_ = b2.c.transpose(sh[1:] + (0,))
|
1162
|
+
tck_n2 = insert(tn, (b2.t, c_, b2.k))
|
1163
|
+
|
1164
|
+
bn2 = insert(tn, b2)
|
1165
|
+
|
1166
|
+
# need a transpose for comparing the results, cf test_splev
|
1167
|
+
xp_assert_close(np.asarray(splev(xx, tck_n2)).transpose(2, 0, 1),
|
1168
|
+
bn2(xx), atol=1e-15)
|
1169
|
+
assert isinstance(bn2, BSpline)
|
1170
|
+
assert isinstance(tck_n2, tuple) # back-compat: tck in, tck out
|
1171
|
+
|
1172
|
+
|
1173
|
+
class TestInterp:
|
1174
|
+
#
|
1175
|
+
# Test basic ways of constructing interpolating splines.
|
1176
|
+
#
|
1177
|
+
xx = np.linspace(0., 2.*np.pi)
|
1178
|
+
yy = np.sin(xx)
|
1179
|
+
|
1180
|
+
def test_non_int_order(self):
|
1181
|
+
with assert_raises(TypeError):
|
1182
|
+
make_interp_spline(self.xx, self.yy, k=2.5)
|
1183
|
+
|
1184
|
+
def test_order_0(self):
|
1185
|
+
b = make_interp_spline(self.xx, self.yy, k=0)
|
1186
|
+
xp_assert_close(b(self.xx), self.yy, atol=1e-14, rtol=1e-14)
|
1187
|
+
b = make_interp_spline(self.xx, self.yy, k=0, axis=-1)
|
1188
|
+
xp_assert_close(b(self.xx), self.yy, atol=1e-14, rtol=1e-14)
|
1189
|
+
|
1190
|
+
def test_linear(self):
|
1191
|
+
b = make_interp_spline(self.xx, self.yy, k=1)
|
1192
|
+
xp_assert_close(b(self.xx), self.yy, atol=1e-14, rtol=1e-14)
|
1193
|
+
b = make_interp_spline(self.xx, self.yy, k=1, axis=-1)
|
1194
|
+
xp_assert_close(b(self.xx), self.yy, atol=1e-14, rtol=1e-14)
|
1195
|
+
|
1196
|
+
@pytest.mark.parametrize('k', [0, 1, 2, 3])
|
1197
|
+
def test_incompatible_x_y(self, k):
|
1198
|
+
x = [0, 1, 2, 3, 4, 5]
|
1199
|
+
y = [0, 1, 2, 3, 4, 5, 6, 7]
|
1200
|
+
with assert_raises(ValueError, match="Shapes of x"):
|
1201
|
+
make_interp_spline(x, y, k=k)
|
1202
|
+
|
1203
|
+
@pytest.mark.parametrize('k', [0, 1, 2, 3])
|
1204
|
+
def test_broken_x(self, k):
|
1205
|
+
x = [0, 1, 1, 2, 3, 4] # duplicates
|
1206
|
+
y = [0, 1, 2, 3, 4, 5]
|
1207
|
+
with assert_raises(ValueError, match="x to not have duplicates"):
|
1208
|
+
make_interp_spline(x, y, k=k)
|
1209
|
+
|
1210
|
+
x = [0, 2, 1, 3, 4, 5] # unsorted
|
1211
|
+
with assert_raises(ValueError, match="Expect x to be a 1D strictly"):
|
1212
|
+
make_interp_spline(x, y, k=k)
|
1213
|
+
|
1214
|
+
x = [0, 1, 2, 3, 4, 5]
|
1215
|
+
x = np.asarray(x).reshape((1, -1)) # 1D
|
1216
|
+
with assert_raises(ValueError, match="Expect x to be a 1D strictly"):
|
1217
|
+
make_interp_spline(x, y, k=k)
|
1218
|
+
|
1219
|
+
def test_not_a_knot(self):
|
1220
|
+
for k in [2, 3, 4, 5, 6, 7]:
|
1221
|
+
b = make_interp_spline(self.xx, self.yy, k)
|
1222
|
+
xp_assert_close(b(self.xx), self.yy, atol=1e-14, rtol=1e-14)
|
1223
|
+
|
1224
|
+
def test_periodic(self):
|
1225
|
+
# k = 5 here for more derivatives
|
1226
|
+
b = make_interp_spline(self.xx, self.yy, k=5, bc_type='periodic')
|
1227
|
+
xp_assert_close(b(self.xx), self.yy, atol=1e-14, rtol=1e-14)
|
1228
|
+
# in periodic case it is expected equality of k-1 first
|
1229
|
+
# derivatives at the boundaries
|
1230
|
+
for i in range(1, 5):
|
1231
|
+
xp_assert_close(b(self.xx[0], nu=i), b(self.xx[-1], nu=i), atol=1e-11)
|
1232
|
+
# tests for axis=-1
|
1233
|
+
b = make_interp_spline(self.xx, self.yy, k=5, bc_type='periodic', axis=-1)
|
1234
|
+
xp_assert_close(b(self.xx), self.yy, atol=1e-14, rtol=1e-14)
|
1235
|
+
for i in range(1, 5):
|
1236
|
+
xp_assert_close(b(self.xx[0], nu=i), b(self.xx[-1], nu=i), atol=1e-11)
|
1237
|
+
|
1238
|
+
@pytest.mark.parametrize('k', [2, 3, 4, 5, 6, 7])
|
1239
|
+
def test_periodic_random(self, k):
|
1240
|
+
# tests for both cases (k > n and k <= n)
|
1241
|
+
n = 5
|
1242
|
+
rng = np.random.RandomState(1234)
|
1243
|
+
x = np.sort(rng.random_sample(n) * 10)
|
1244
|
+
y = rng.random_sample(n) * 100
|
1245
|
+
y[0] = y[-1]
|
1246
|
+
b = make_interp_spline(x, y, k=k, bc_type='periodic')
|
1247
|
+
xp_assert_close(b(x), y, atol=1e-14)
|
1248
|
+
|
1249
|
+
def test_periodic_axis(self):
|
1250
|
+
n = self.xx.shape[0]
|
1251
|
+
rng = np.random.RandomState(1234)
|
1252
|
+
x = rng.random_sample(n) * 2 * np.pi
|
1253
|
+
x = np.sort(x)
|
1254
|
+
x[0] = 0.
|
1255
|
+
x[-1] = 2 * np.pi
|
1256
|
+
y = np.zeros((2, n))
|
1257
|
+
y[0] = np.sin(x)
|
1258
|
+
y[1] = np.cos(x)
|
1259
|
+
b = make_interp_spline(x, y, k=5, bc_type='periodic', axis=1)
|
1260
|
+
for i in range(n):
|
1261
|
+
xp_assert_close(b(x[i]), y[:, i], atol=1e-14)
|
1262
|
+
xp_assert_close(b(x[0]), b(x[-1]), atol=1e-14)
|
1263
|
+
|
1264
|
+
def test_periodic_points_exception(self):
|
1265
|
+
# first and last points should match when periodic case expected
|
1266
|
+
rng = np.random.RandomState(1234)
|
1267
|
+
k = 5
|
1268
|
+
n = 8
|
1269
|
+
x = np.sort(rng.random_sample(n))
|
1270
|
+
y = rng.random_sample(n)
|
1271
|
+
y[0] = y[-1] - 1 # to be sure that they are not equal
|
1272
|
+
with assert_raises(ValueError):
|
1273
|
+
make_interp_spline(x, y, k=k, bc_type='periodic')
|
1274
|
+
|
1275
|
+
def test_periodic_knots_exception(self):
|
1276
|
+
# `periodic` case does not work with passed vector of knots
|
1277
|
+
rng = np.random.RandomState(1234)
|
1278
|
+
k = 3
|
1279
|
+
n = 7
|
1280
|
+
x = np.sort(rng.random_sample(n))
|
1281
|
+
y = rng.random_sample(n)
|
1282
|
+
t = np.zeros(n + 2 * k)
|
1283
|
+
with assert_raises(ValueError):
|
1284
|
+
make_interp_spline(x, y, k, t, 'periodic')
|
1285
|
+
|
1286
|
+
@pytest.mark.parametrize('k', [2, 3, 4, 5])
|
1287
|
+
def test_periodic_splev(self, k):
|
1288
|
+
# comparison values of periodic b-spline with splev
|
1289
|
+
b = make_interp_spline(self.xx, self.yy, k=k, bc_type='periodic')
|
1290
|
+
tck = splrep(self.xx, self.yy, per=True, k=k)
|
1291
|
+
spl = splev(self.xx, tck)
|
1292
|
+
xp_assert_close(spl, b(self.xx), atol=1e-14)
|
1293
|
+
|
1294
|
+
# comparison derivatives of periodic b-spline with splev
|
1295
|
+
for i in range(1, k):
|
1296
|
+
spl = splev(self.xx, tck, der=i)
|
1297
|
+
xp_assert_close(spl, b(self.xx, nu=i), atol=1e-10)
|
1298
|
+
|
1299
|
+
def test_periodic_cubic(self):
|
1300
|
+
# comparison values of cubic periodic b-spline with CubicSpline
|
1301
|
+
b = make_interp_spline(self.xx, self.yy, k=3, bc_type='periodic')
|
1302
|
+
cub = CubicSpline(self.xx, self.yy, bc_type='periodic')
|
1303
|
+
xp_assert_close(b(self.xx), cub(self.xx), atol=1e-14)
|
1304
|
+
|
1305
|
+
# edge case: Cubic interpolation on 3 points
|
1306
|
+
rng = np.random.RandomState(1234)
|
1307
|
+
n = 3
|
1308
|
+
x = np.sort(rng.random_sample(n) * 10)
|
1309
|
+
y = rng.random_sample(n) * 100
|
1310
|
+
y[0] = y[-1]
|
1311
|
+
b = make_interp_spline(x, y, k=3, bc_type='periodic')
|
1312
|
+
cub = CubicSpline(x, y, bc_type='periodic')
|
1313
|
+
xp_assert_close(b(x), cub(x), atol=1e-14)
|
1314
|
+
|
1315
|
+
def test_periodic_full_matrix(self):
|
1316
|
+
# comparison values of cubic periodic b-spline with
|
1317
|
+
# solution of the system with full matrix
|
1318
|
+
k = 3
|
1319
|
+
b = make_interp_spline(self.xx, self.yy, k=k, bc_type='periodic')
|
1320
|
+
t = _periodic_knots(self.xx, k)
|
1321
|
+
c = _make_interp_per_full_matr(self.xx, self.yy, t, k)
|
1322
|
+
b1 = np.vectorize(lambda x: _naive_eval(x, t, c, k))
|
1323
|
+
xp_assert_close(b(self.xx), b1(self.xx), atol=1e-14)
|
1324
|
+
|
1325
|
+
def test_quadratic_deriv(self):
|
1326
|
+
der = [(1, 8.)] # order, value: f'(x) = 8.
|
1327
|
+
|
1328
|
+
# derivative at right-hand edge
|
1329
|
+
b = make_interp_spline(self.xx, self.yy, k=2, bc_type=(None, der))
|
1330
|
+
xp_assert_close(b(self.xx), self.yy, atol=1e-14, rtol=1e-14)
|
1331
|
+
xp_assert_close(
|
1332
|
+
b(self.xx[-1], 1), der[0][1], atol=1e-14, rtol=1e-14, check_0d=False
|
1333
|
+
)
|
1334
|
+
|
1335
|
+
# derivative at left-hand edge
|
1336
|
+
b = make_interp_spline(self.xx, self.yy, k=2, bc_type=(der, None))
|
1337
|
+
xp_assert_close(b(self.xx), self.yy, atol=1e-14, rtol=1e-14)
|
1338
|
+
xp_assert_close(
|
1339
|
+
b(self.xx[0], 1), der[0][1], atol=1e-14, rtol=1e-14, check_0d=False
|
1340
|
+
)
|
1341
|
+
|
1342
|
+
def test_cubic_deriv(self):
|
1343
|
+
k = 3
|
1344
|
+
|
1345
|
+
# first derivatives at left & right edges:
|
1346
|
+
der_l, der_r = [(1, 3.)], [(1, 4.)]
|
1347
|
+
b = make_interp_spline(self.xx, self.yy, k, bc_type=(der_l, der_r))
|
1348
|
+
xp_assert_close(b(self.xx), self.yy, atol=1e-14, rtol=1e-14)
|
1349
|
+
xp_assert_close(np.asarray([b(self.xx[0], 1), b(self.xx[-1], 1)]),
|
1350
|
+
np.asarray([der_l[0][1], der_r[0][1]]), atol=1e-14, rtol=1e-14)
|
1351
|
+
|
1352
|
+
# 'natural' cubic spline, zero out 2nd derivatives at the boundaries
|
1353
|
+
der_l, der_r = [(2, 0)], [(2, 0)]
|
1354
|
+
b = make_interp_spline(self.xx, self.yy, k, bc_type=(der_l, der_r))
|
1355
|
+
xp_assert_close(b(self.xx), self.yy, atol=1e-14, rtol=1e-14)
|
1356
|
+
|
1357
|
+
def test_quintic_derivs(self):
|
1358
|
+
k, n = 5, 7
|
1359
|
+
x = np.arange(n).astype(np.float64)
|
1360
|
+
y = np.sin(x)
|
1361
|
+
der_l = [(1, -12.), (2, 1)]
|
1362
|
+
der_r = [(1, 8.), (2, 3.)]
|
1363
|
+
b = make_interp_spline(x, y, k=k, bc_type=(der_l, der_r))
|
1364
|
+
xp_assert_close(b(x), y, atol=1e-14, rtol=1e-14)
|
1365
|
+
xp_assert_close(np.asarray([b(x[0], 1), b(x[0], 2)]),
|
1366
|
+
np.asarray([val for (nu, val) in der_l]))
|
1367
|
+
xp_assert_close(np.asarray([b(x[-1], 1), b(x[-1], 2)]),
|
1368
|
+
np.asarray([val for (nu, val) in der_r]))
|
1369
|
+
|
1370
|
+
@pytest.mark.xfail(reason='unstable')
|
1371
|
+
def test_cubic_deriv_unstable(self):
|
1372
|
+
# 1st and 2nd derivative at x[0], no derivative information at x[-1]
|
1373
|
+
# The problem is not that it fails [who would use this anyway],
|
1374
|
+
# the problem is that it fails *silently*, and I've no idea
|
1375
|
+
# how to detect this sort of instability.
|
1376
|
+
# In this particular case: it's OK for len(t) < 20, goes haywire
|
1377
|
+
# at larger `len(t)`.
|
1378
|
+
k = 3
|
1379
|
+
t = _augknt(self.xx, k)
|
1380
|
+
|
1381
|
+
der_l = [(1, 3.), (2, 4.)]
|
1382
|
+
b = make_interp_spline(self.xx, self.yy, k, t, bc_type=(der_l, None))
|
1383
|
+
xp_assert_close(b(self.xx), self.yy, atol=1e-14, rtol=1e-14)
|
1384
|
+
|
1385
|
+
def test_knots_not_data_sites(self):
|
1386
|
+
# Knots need not coincide with the data sites.
|
1387
|
+
# use a quadratic spline, knots are at data averages,
|
1388
|
+
# two additional constraints are zero 2nd derivatives at edges
|
1389
|
+
k = 2
|
1390
|
+
t = np.r_[(self.xx[0],)*(k+1),
|
1391
|
+
(self.xx[1:] + self.xx[:-1]) / 2.,
|
1392
|
+
(self.xx[-1],)*(k+1)]
|
1393
|
+
b = make_interp_spline(self.xx, self.yy, k, t,
|
1394
|
+
bc_type=([(2, 0)], [(2, 0)]))
|
1395
|
+
|
1396
|
+
xp_assert_close(b(self.xx), self.yy, atol=1e-14, rtol=1e-14)
|
1397
|
+
xp_assert_close(b(self.xx[0], 2), np.asarray(0.0), atol=1e-14)
|
1398
|
+
xp_assert_close(b(self.xx[-1], 2), np.asarray(0.0), atol=1e-14)
|
1399
|
+
|
1400
|
+
def test_minimum_points_and_deriv(self):
|
1401
|
+
# interpolation of f(x) = x**3 between 0 and 1. f'(x) = 3 * xx**2 and
|
1402
|
+
# f'(0) = 0, f'(1) = 3.
|
1403
|
+
k = 3
|
1404
|
+
x = [0., 1.]
|
1405
|
+
y = [0., 1.]
|
1406
|
+
b = make_interp_spline(x, y, k, bc_type=([(1, 0.)], [(1, 3.)]))
|
1407
|
+
|
1408
|
+
xx = np.linspace(0., 1.)
|
1409
|
+
yy = xx**3
|
1410
|
+
xp_assert_close(b(xx), yy, atol=1e-14, rtol=1e-14)
|
1411
|
+
|
1412
|
+
def test_deriv_spec(self):
|
1413
|
+
# If one of the derivatives is omitted, the spline definition is
|
1414
|
+
# incomplete.
|
1415
|
+
x = y = [1.0, 2, 3, 4, 5, 6]
|
1416
|
+
|
1417
|
+
with assert_raises(ValueError):
|
1418
|
+
make_interp_spline(x, y, bc_type=([(1, 0.)], None))
|
1419
|
+
|
1420
|
+
with assert_raises(ValueError):
|
1421
|
+
make_interp_spline(x, y, bc_type=(1, 0.))
|
1422
|
+
|
1423
|
+
with assert_raises(ValueError):
|
1424
|
+
make_interp_spline(x, y, bc_type=[(1, 0.)])
|
1425
|
+
|
1426
|
+
with assert_raises(ValueError):
|
1427
|
+
make_interp_spline(x, y, bc_type=42)
|
1428
|
+
|
1429
|
+
# CubicSpline expects`bc_type=(left_pair, right_pair)`, while
|
1430
|
+
# here we expect `bc_type=(iterable, iterable)`.
|
1431
|
+
l, r = (1, 0.0), (1, 0.0)
|
1432
|
+
with assert_raises(ValueError):
|
1433
|
+
make_interp_spline(x, y, bc_type=(l, r))
|
1434
|
+
|
1435
|
+
def test_deriv_order_too_large(self):
|
1436
|
+
x = np.arange(7)
|
1437
|
+
y = x**2
|
1438
|
+
l, r = [(6, 0)], [(1, 0)] # 6th derivative = 0 at x[0] for k=3
|
1439
|
+
with assert_raises(ValueError, match="Bad boundary conditions at 0."):
|
1440
|
+
# cannot fix 6th derivative at x[0]: does not segfault
|
1441
|
+
make_interp_spline(x, y, bc_type=(l, r))
|
1442
|
+
|
1443
|
+
l, r = [(1, 0)], [(-6, 0)] # derivative order < 0 at x[-1]
|
1444
|
+
with assert_raises(ValueError, match="Bad boundary conditions at 6."):
|
1445
|
+
# does not segfault
|
1446
|
+
make_interp_spline(x, y, bc_type=(l, r))
|
1447
|
+
|
1448
|
+
def test_complex(self):
|
1449
|
+
k = 3
|
1450
|
+
xx = self.xx
|
1451
|
+
yy = self.yy + 1.j*self.yy
|
1452
|
+
|
1453
|
+
# first derivatives at left & right edges:
|
1454
|
+
der_l, der_r = [(1, 3.j)], [(1, 4.+2.j)]
|
1455
|
+
b = make_interp_spline(xx, yy, k, bc_type=(der_l, der_r))
|
1456
|
+
xp_assert_close(b(xx), yy, atol=1e-14, rtol=1e-14)
|
1457
|
+
xp_assert_close(
|
1458
|
+
b(xx[0], 1), der_l[0][1], atol=1e-14, rtol=1e-14, check_0d=False
|
1459
|
+
)
|
1460
|
+
xp_assert_close(
|
1461
|
+
b(xx[-1], 1), der_r[0][1], atol=1e-14, rtol=1e-14, check_0d=False
|
1462
|
+
)
|
1463
|
+
|
1464
|
+
# also test zero and first order
|
1465
|
+
for k in (0, 1):
|
1466
|
+
b = make_interp_spline(xx, yy, k=k)
|
1467
|
+
xp_assert_close(b(xx), yy, atol=1e-14, rtol=1e-14)
|
1468
|
+
|
1469
|
+
def test_int_xy(self):
|
1470
|
+
x = np.arange(10).astype(int)
|
1471
|
+
y = np.arange(10).astype(int)
|
1472
|
+
|
1473
|
+
# Cython chokes on "buffer type mismatch" (construction) or
|
1474
|
+
# "no matching signature found" (evaluation)
|
1475
|
+
for k in (0, 1, 2, 3):
|
1476
|
+
b = make_interp_spline(x, y, k=k)
|
1477
|
+
b(x)
|
1478
|
+
|
1479
|
+
def test_sliced_input(self):
|
1480
|
+
# Cython code chokes on non C contiguous arrays
|
1481
|
+
xx = np.linspace(-1, 1, 100)
|
1482
|
+
|
1483
|
+
x = xx[::5]
|
1484
|
+
y = xx[::5]
|
1485
|
+
|
1486
|
+
for k in (0, 1, 2, 3):
|
1487
|
+
make_interp_spline(x, y, k=k)
|
1488
|
+
|
1489
|
+
def test_check_finite(self):
|
1490
|
+
# check_finite defaults to True; nans and such trigger a ValueError
|
1491
|
+
x = np.arange(10).astype(float)
|
1492
|
+
y = x**2
|
1493
|
+
|
1494
|
+
for z in [np.nan, np.inf, -np.inf]:
|
1495
|
+
y[-1] = z
|
1496
|
+
assert_raises(ValueError, make_interp_spline, x, y)
|
1497
|
+
|
1498
|
+
@pytest.mark.parametrize('k', [1, 2, 3, 5])
|
1499
|
+
def test_list_input(self, k):
|
1500
|
+
# regression test for gh-8714: TypeError for x, y being lists and k=2
|
1501
|
+
x = list(range(10))
|
1502
|
+
y = [a**2 for a in x]
|
1503
|
+
make_interp_spline(x, y, k=k)
|
1504
|
+
|
1505
|
+
def test_multiple_rhs(self):
|
1506
|
+
yy = np.c_[np.sin(self.xx), np.cos(self.xx)]
|
1507
|
+
der_l = [(1, [1., 2.])]
|
1508
|
+
der_r = [(1, [3., 4.])]
|
1509
|
+
|
1510
|
+
b = make_interp_spline(self.xx, yy, k=3, bc_type=(der_l, der_r))
|
1511
|
+
xp_assert_close(b(self.xx), yy, atol=1e-14, rtol=1e-14)
|
1512
|
+
xp_assert_close(b(self.xx[0], 1), der_l[0][1], atol=1e-14, rtol=1e-14)
|
1513
|
+
xp_assert_close(b(self.xx[-1], 1), der_r[0][1], atol=1e-14, rtol=1e-14)
|
1514
|
+
|
1515
|
+
def test_shapes(self):
|
1516
|
+
rng = np.random.RandomState(1234)
|
1517
|
+
k, n = 3, 22
|
1518
|
+
x = np.sort(rng.random(size=n))
|
1519
|
+
y = rng.random(size=(n, 5, 6, 7))
|
1520
|
+
|
1521
|
+
b = make_interp_spline(x, y, k)
|
1522
|
+
assert b.c.shape == (n, 5, 6, 7)
|
1523
|
+
|
1524
|
+
# now throw in some derivatives
|
1525
|
+
d_l = [(1, rng.random((5, 6, 7)))]
|
1526
|
+
d_r = [(1, rng.random((5, 6, 7)))]
|
1527
|
+
b = make_interp_spline(x, y, k, bc_type=(d_l, d_r))
|
1528
|
+
assert b.c.shape == (n + k - 1, 5, 6, 7)
|
1529
|
+
|
1530
|
+
def test_string_aliases(self):
|
1531
|
+
yy = np.sin(self.xx)
|
1532
|
+
|
1533
|
+
# a single string is duplicated
|
1534
|
+
b1 = make_interp_spline(self.xx, yy, k=3, bc_type='natural')
|
1535
|
+
b2 = make_interp_spline(self.xx, yy, k=3, bc_type=([(2, 0)], [(2, 0)]))
|
1536
|
+
xp_assert_close(b1.c, b2.c, atol=1e-15)
|
1537
|
+
|
1538
|
+
# two strings are handled
|
1539
|
+
b1 = make_interp_spline(self.xx, yy, k=3,
|
1540
|
+
bc_type=('natural', 'clamped'))
|
1541
|
+
b2 = make_interp_spline(self.xx, yy, k=3,
|
1542
|
+
bc_type=([(2, 0)], [(1, 0)]))
|
1543
|
+
xp_assert_close(b1.c, b2.c, atol=1e-15)
|
1544
|
+
|
1545
|
+
# one-sided BCs are OK
|
1546
|
+
b1 = make_interp_spline(self.xx, yy, k=2, bc_type=(None, 'clamped'))
|
1547
|
+
b2 = make_interp_spline(self.xx, yy, k=2, bc_type=(None, [(1, 0.0)]))
|
1548
|
+
xp_assert_close(b1.c, b2.c, atol=1e-15)
|
1549
|
+
|
1550
|
+
# 'not-a-knot' is equivalent to None
|
1551
|
+
b1 = make_interp_spline(self.xx, yy, k=3, bc_type='not-a-knot')
|
1552
|
+
b2 = make_interp_spline(self.xx, yy, k=3, bc_type=None)
|
1553
|
+
xp_assert_close(b1.c, b2.c, atol=1e-15)
|
1554
|
+
|
1555
|
+
# unknown strings do not pass
|
1556
|
+
with assert_raises(ValueError):
|
1557
|
+
make_interp_spline(self.xx, yy, k=3, bc_type='typo')
|
1558
|
+
|
1559
|
+
# string aliases are handled for 2D values
|
1560
|
+
yy = np.c_[np.sin(self.xx), np.cos(self.xx)]
|
1561
|
+
der_l = [(1, [0., 0.])]
|
1562
|
+
der_r = [(2, [0., 0.])]
|
1563
|
+
b2 = make_interp_spline(self.xx, yy, k=3, bc_type=(der_l, der_r))
|
1564
|
+
b1 = make_interp_spline(self.xx, yy, k=3,
|
1565
|
+
bc_type=('clamped', 'natural'))
|
1566
|
+
xp_assert_close(b1.c, b2.c, atol=1e-15)
|
1567
|
+
|
1568
|
+
# ... and for N-D values:
|
1569
|
+
rng = np.random.RandomState(1234)
|
1570
|
+
k, n = 3, 22
|
1571
|
+
x = np.sort(rng.random(size=n))
|
1572
|
+
y = rng.random(size=(n, 5, 6, 7))
|
1573
|
+
|
1574
|
+
# now throw in some derivatives
|
1575
|
+
d_l = [(1, np.zeros((5, 6, 7)))]
|
1576
|
+
d_r = [(1, np.zeros((5, 6, 7)))]
|
1577
|
+
b1 = make_interp_spline(x, y, k, bc_type=(d_l, d_r))
|
1578
|
+
b2 = make_interp_spline(x, y, k, bc_type='clamped')
|
1579
|
+
xp_assert_close(b1.c, b2.c, atol=1e-15)
|
1580
|
+
|
1581
|
+
def test_full_matrix(self):
|
1582
|
+
rng = np.random.RandomState(1234)
|
1583
|
+
k, n = 3, 7
|
1584
|
+
x = np.sort(rng.random(size=n))
|
1585
|
+
y = rng.random(size=n)
|
1586
|
+
t = _not_a_knot(x, k)
|
1587
|
+
|
1588
|
+
b = make_interp_spline(x, y, k, t)
|
1589
|
+
cf = make_interp_full_matr(x, y, t, k)
|
1590
|
+
xp_assert_close(b.c, cf, atol=1e-14, rtol=1e-14)
|
1591
|
+
|
1592
|
+
def test_woodbury(self):
|
1593
|
+
'''
|
1594
|
+
Random elements in diagonal matrix with blocks in the
|
1595
|
+
left lower and right upper corners checking the
|
1596
|
+
implementation of Woodbury algorithm.
|
1597
|
+
'''
|
1598
|
+
rng = np.random.RandomState(1234)
|
1599
|
+
n = 201
|
1600
|
+
for k in range(3, 32, 2):
|
1601
|
+
offset = int((k - 1) / 2)
|
1602
|
+
a = np.diagflat(rng.random((1, n)))
|
1603
|
+
for i in range(1, offset + 1):
|
1604
|
+
a[:-i, i:] += np.diagflat(rng.random((1, n - i)))
|
1605
|
+
a[i:, :-i] += np.diagflat(rng.random((1, n - i)))
|
1606
|
+
ur = rng.random((offset, offset))
|
1607
|
+
a[:offset, -offset:] = ur
|
1608
|
+
ll = rng.random((offset, offset))
|
1609
|
+
a[-offset:, :offset] = ll
|
1610
|
+
d = np.zeros((k, n))
|
1611
|
+
for i, j in enumerate(range(offset, -offset - 1, -1)):
|
1612
|
+
if j < 0:
|
1613
|
+
d[i, :j] = np.diagonal(a, offset=j)
|
1614
|
+
else:
|
1615
|
+
d[i, j:] = np.diagonal(a, offset=j)
|
1616
|
+
b = rng.random(n)
|
1617
|
+
xp_assert_close(_woodbury_algorithm(d, ur, ll, b, k),
|
1618
|
+
np.linalg.solve(a, b), atol=1e-14)
|
1619
|
+
|
1620
|
+
|
1621
|
+
def make_interp_full_matr(x, y, t, k):
|
1622
|
+
"""Assemble an spline order k with knots t to interpolate
|
1623
|
+
y(x) using full matrices.
|
1624
|
+
Not-a-knot BC only.
|
1625
|
+
|
1626
|
+
This routine is here for testing only (even though it's functional).
|
1627
|
+
"""
|
1628
|
+
assert x.size == y.size
|
1629
|
+
assert t.size == x.size + k + 1
|
1630
|
+
n = x.size
|
1631
|
+
|
1632
|
+
A = np.zeros((n, n), dtype=np.float64)
|
1633
|
+
|
1634
|
+
for j in range(n):
|
1635
|
+
xval = x[j]
|
1636
|
+
if xval == t[k]:
|
1637
|
+
left = k
|
1638
|
+
else:
|
1639
|
+
left = np.searchsorted(t, xval) - 1
|
1640
|
+
|
1641
|
+
# fill a row
|
1642
|
+
bb = _dierckx.evaluate_all_bspl(t, k, xval, left)
|
1643
|
+
A[j, left-k:left+1] = bb
|
1644
|
+
|
1645
|
+
c = sl.solve(A, y)
|
1646
|
+
return c
|
1647
|
+
|
1648
|
+
|
1649
|
+
def make_lsq_full_matrix(x, y, t, k=3):
|
1650
|
+
"""Make the least-square spline, full matrices."""
|
1651
|
+
x, y, t = map(np.asarray, (x, y, t))
|
1652
|
+
m = x.size
|
1653
|
+
n = t.size - k - 1
|
1654
|
+
|
1655
|
+
A = np.zeros((m, n), dtype=np.float64)
|
1656
|
+
|
1657
|
+
for j in range(m):
|
1658
|
+
xval = x[j]
|
1659
|
+
# find interval
|
1660
|
+
if xval == t[k]:
|
1661
|
+
left = k
|
1662
|
+
else:
|
1663
|
+
left = np.searchsorted(t, xval) - 1
|
1664
|
+
|
1665
|
+
# fill a row
|
1666
|
+
bb = _dierckx.evaluate_all_bspl(t, k, xval, left)
|
1667
|
+
A[j, left-k:left+1] = bb
|
1668
|
+
|
1669
|
+
# have observation matrix, can solve the LSQ problem
|
1670
|
+
B = np.dot(A.T, A)
|
1671
|
+
Y = np.dot(A.T, y)
|
1672
|
+
c = sl.solve(B, Y)
|
1673
|
+
|
1674
|
+
return c, (A, Y)
|
1675
|
+
|
1676
|
+
|
1677
|
+
parametrize_lsq_methods = pytest.mark.parametrize("method", ["norm-eq", "qr"])
|
1678
|
+
|
1679
|
+
class TestLSQ:
|
1680
|
+
#
|
1681
|
+
# Test make_lsq_spline
|
1682
|
+
#
|
1683
|
+
rng = np.random.RandomState(1234)
|
1684
|
+
n, k = 13, 3
|
1685
|
+
x = np.sort(rng.random(n))
|
1686
|
+
y = rng.random(n)
|
1687
|
+
t = _augknt(np.linspace(x[0], x[-1], 7), k)
|
1688
|
+
|
1689
|
+
@parametrize_lsq_methods
|
1690
|
+
def test_lstsq(self, method):
|
1691
|
+
# check LSQ construction vs a full matrix version
|
1692
|
+
x, y, t, k = self.x, self.y, self.t, self.k
|
1693
|
+
|
1694
|
+
c0, AY = make_lsq_full_matrix(x, y, t, k)
|
1695
|
+
b = make_lsq_spline(x, y, t, k, method=method)
|
1696
|
+
|
1697
|
+
xp_assert_close(b.c, c0)
|
1698
|
+
assert b.c.shape == (t.size - k - 1,)
|
1699
|
+
|
1700
|
+
# also check against numpy.lstsq
|
1701
|
+
aa, yy = AY
|
1702
|
+
c1, _, _, _ = np.linalg.lstsq(aa, y, rcond=-1)
|
1703
|
+
xp_assert_close(b.c, c1)
|
1704
|
+
|
1705
|
+
@parametrize_lsq_methods
|
1706
|
+
def test_weights(self, method):
|
1707
|
+
# weights = 1 is same as None
|
1708
|
+
x, y, t, k = self.x, self.y, self.t, self.k
|
1709
|
+
w = np.ones_like(x)
|
1710
|
+
|
1711
|
+
b = make_lsq_spline(x, y, t, k, method=method)
|
1712
|
+
b_w = make_lsq_spline(x, y, t, k, w=w, method=method)
|
1713
|
+
|
1714
|
+
xp_assert_close(b.t, b_w.t, atol=1e-14)
|
1715
|
+
xp_assert_close(b.c, b_w.c, atol=1e-14)
|
1716
|
+
assert b.k == b_w.k
|
1717
|
+
|
1718
|
+
def test_weights_same(self):
|
1719
|
+
# both methods treat weights
|
1720
|
+
x, y, t, k = self.x, self.y, self.t, self.k
|
1721
|
+
w = np.random.default_rng(1234).uniform(size=x.shape[0])
|
1722
|
+
|
1723
|
+
b_ne = make_lsq_spline(x, y, t, k, w=w, method="norm-eq")
|
1724
|
+
b_qr = make_lsq_spline(x, y, t, k, w=w, method="qr")
|
1725
|
+
b_no_w = make_lsq_spline(x, y, t, k, method="qr")
|
1726
|
+
|
1727
|
+
xp_assert_close(b_ne.c, b_qr.c, atol=1e-14)
|
1728
|
+
assert not np.allclose(b_no_w.c, b_qr.c, atol=1e-14)
|
1729
|
+
|
1730
|
+
@parametrize_lsq_methods
|
1731
|
+
def test_multiple_rhs(self, method):
|
1732
|
+
x, t, k, n = self.x, self.t, self.k, self.n
|
1733
|
+
rng = np.random.RandomState(1234)
|
1734
|
+
y = rng.random(size=(n, 5, 6, 7))
|
1735
|
+
b = make_lsq_spline(x, y, t, k, method=method)
|
1736
|
+
assert b.c.shape == (t.size-k-1, 5, 6, 7)
|
1737
|
+
|
1738
|
+
@parametrize_lsq_methods
|
1739
|
+
def test_multiple_rhs_2(self, method):
|
1740
|
+
x, t, k, n = self.x, self.t, self.k, self.n
|
1741
|
+
nrhs = 3
|
1742
|
+
rng = np.random.RandomState(1234)
|
1743
|
+
y = rng.random(size=(n, nrhs))
|
1744
|
+
b = make_lsq_spline(x, y, t, k, method=method)
|
1745
|
+
|
1746
|
+
bb = [make_lsq_spline(x, y[:, i], t, k, method=method)
|
1747
|
+
for i in range(nrhs)]
|
1748
|
+
coefs = np.vstack([bb[i].c for i in range(nrhs)]).T
|
1749
|
+
|
1750
|
+
xp_assert_close(coefs, b.c, atol=1e-15)
|
1751
|
+
|
1752
|
+
def test_multiple_rhs_3(self):
|
1753
|
+
x, t, k, n = self.x, self.t, self.k, self.n
|
1754
|
+
nrhs = 3
|
1755
|
+
y = np.random.random(size=(n, nrhs))
|
1756
|
+
b_qr = make_lsq_spline(x, y, t, k, method="qr")
|
1757
|
+
b_neq = make_lsq_spline(x, y, t, k, method="norm-eq")
|
1758
|
+
xp_assert_close(b_qr.c, b_neq.c, atol=1e-15)
|
1759
|
+
|
1760
|
+
@parametrize_lsq_methods
|
1761
|
+
def test_complex(self, method):
|
1762
|
+
# cmplx-valued `y`
|
1763
|
+
x, t, k = self.x, self.t, self.k
|
1764
|
+
yc = self.y * (1. + 2.j)
|
1765
|
+
|
1766
|
+
b = make_lsq_spline(x, yc, t, k, method=method)
|
1767
|
+
b_re = make_lsq_spline(x, yc.real, t, k, method=method)
|
1768
|
+
b_im = make_lsq_spline(x, yc.imag, t, k, method=method)
|
1769
|
+
|
1770
|
+
xp_assert_close(b(x), b_re(x) + 1.j*b_im(x), atol=1e-15, rtol=1e-15)
|
1771
|
+
|
1772
|
+
def test_complex_2(self):
|
1773
|
+
# test complex-valued y with y.ndim > 1
|
1774
|
+
|
1775
|
+
x, t, k = self.x, self.t, self.k
|
1776
|
+
yc = self.y * (1. + 2.j)
|
1777
|
+
yc = np.stack((yc, yc), axis=1)
|
1778
|
+
|
1779
|
+
b = make_lsq_spline(x, yc, t, k)
|
1780
|
+
b_re = make_lsq_spline(x, yc.real, t, k)
|
1781
|
+
b_im = make_lsq_spline(x, yc.imag, t, k)
|
1782
|
+
|
1783
|
+
xp_assert_close(b(x), b_re(x) + 1.j*b_im(x), atol=1e-15, rtol=1e-15)
|
1784
|
+
|
1785
|
+
# repeat with num_trailing_dims > 1 : yc.shape[1:] = (2, 2)
|
1786
|
+
yc = np.stack((yc, yc), axis=1)
|
1787
|
+
|
1788
|
+
b = make_lsq_spline(x, yc, t, k)
|
1789
|
+
b_re = make_lsq_spline(x, yc.real, t, k)
|
1790
|
+
b_im = make_lsq_spline(x, yc.imag, t, k)
|
1791
|
+
|
1792
|
+
xp_assert_close(b(x), b_re(x) + 1.j*b_im(x), atol=1e-15, rtol=1e-15)
|
1793
|
+
|
1794
|
+
@parametrize_lsq_methods
|
1795
|
+
def test_int_xy(self, method):
|
1796
|
+
x = np.arange(10).astype(int)
|
1797
|
+
y = np.arange(10).astype(int)
|
1798
|
+
t = _augknt(x, k=1)
|
1799
|
+
# Cython chokes on "buffer type mismatch"
|
1800
|
+
make_lsq_spline(x, y, t, k=1, method=method)
|
1801
|
+
|
1802
|
+
@parametrize_lsq_methods
|
1803
|
+
def test_f32_xy(self, method):
|
1804
|
+
x = np.arange(10, dtype=np.float32)
|
1805
|
+
y = np.arange(10, dtype=np.float32)
|
1806
|
+
t = _augknt(x, k=1)
|
1807
|
+
spl_f32 = make_lsq_spline(x, y, t, k=1, method=method)
|
1808
|
+
spl_f64 = make_lsq_spline(
|
1809
|
+
x.astype(float), y.astype(float), t.astype(float), k=1, method=method
|
1810
|
+
)
|
1811
|
+
|
1812
|
+
x2 = (x[1:] + x[:-1]) / 2.0
|
1813
|
+
xp_assert_close(spl_f32(x2), spl_f64(x2), atol=1e-15)
|
1814
|
+
|
1815
|
+
@parametrize_lsq_methods
|
1816
|
+
def test_sliced_input(self, method):
|
1817
|
+
# Cython code chokes on non C contiguous arrays
|
1818
|
+
xx = np.linspace(-1, 1, 100)
|
1819
|
+
|
1820
|
+
x = xx[::3]
|
1821
|
+
y = xx[::3]
|
1822
|
+
t = _augknt(x, 1)
|
1823
|
+
make_lsq_spline(x, y, t, k=1, method=method)
|
1824
|
+
|
1825
|
+
@parametrize_lsq_methods
|
1826
|
+
def test_checkfinite(self, method):
|
1827
|
+
# check_finite defaults to True; nans and such trigger a ValueError
|
1828
|
+
x = np.arange(12).astype(float)
|
1829
|
+
y = x**2
|
1830
|
+
t = _augknt(x, 3)
|
1831
|
+
|
1832
|
+
for z in [np.nan, np.inf, -np.inf]:
|
1833
|
+
y[-1] = z
|
1834
|
+
assert_raises(ValueError, make_lsq_spline, x, y, t, method=method)
|
1835
|
+
|
1836
|
+
@parametrize_lsq_methods
|
1837
|
+
def test_read_only(self, method):
|
1838
|
+
# Check that make_lsq_spline works with read only arrays
|
1839
|
+
x, y, t = self.x, self.y, self.t
|
1840
|
+
x.setflags(write=False)
|
1841
|
+
y.setflags(write=False)
|
1842
|
+
t.setflags(write=False)
|
1843
|
+
make_lsq_spline(x=x, y=y, t=t, method=method)
|
1844
|
+
|
1845
|
+
@pytest.mark.parametrize('k', list(range(1, 7)))
|
1846
|
+
def test_qr_vs_norm_eq(self, k):
|
1847
|
+
# check that QR and normal eq solutions match
|
1848
|
+
x, y = self.x, self.y
|
1849
|
+
t = _augknt(np.linspace(x[0], x[-1], 7), k)
|
1850
|
+
spl_norm_eq = make_lsq_spline(x, y, t, k=k, method='norm-eq')
|
1851
|
+
spl_qr = make_lsq_spline(x, y, t, k=k, method='qr')
|
1852
|
+
|
1853
|
+
xx = (x[1:] + x[:-1]) / 2.0
|
1854
|
+
xp_assert_close(spl_norm_eq(xx), spl_qr(xx), atol=1e-15)
|
1855
|
+
|
1856
|
+
def test_duplicates(self):
|
1857
|
+
# method="qr" can handle duplicated data points
|
1858
|
+
x = np.repeat(self.x, 2)
|
1859
|
+
y = np.repeat(self.y, 2)
|
1860
|
+
spl_1 = make_lsq_spline(self.x, self.y, self.t, k=3, method='qr')
|
1861
|
+
spl_2 = make_lsq_spline(x, y, self.t, k=3, method='qr')
|
1862
|
+
|
1863
|
+
xx = (x[1:] + x[:-1]) / 2.0
|
1864
|
+
xp_assert_close(spl_1(xx), spl_2(xx), atol=1e-15)
|
1865
|
+
|
1866
|
+
|
1867
|
+
class PackedMatrix:
|
1868
|
+
"""A simplified CSR format for when non-zeros in each row are consecutive.
|
1869
|
+
|
1870
|
+
Assuming that each row of an `(m, nc)` matrix 1) only has `nz` non-zeros, and
|
1871
|
+
2) these non-zeros are consecutive, we only store an `(m, nz)` matrix of
|
1872
|
+
non-zeros and a 1D array of row offsets. This way, a row `i` of the original
|
1873
|
+
matrix A is ``A[i, offset[i]: offset[i] + nz]``.
|
1874
|
+
|
1875
|
+
"""
|
1876
|
+
def __init__(self, a, offset, nc):
|
1877
|
+
self.a = a
|
1878
|
+
self.offset = offset
|
1879
|
+
self.nc = nc
|
1880
|
+
|
1881
|
+
assert a.ndim == 2
|
1882
|
+
assert offset.ndim == 1
|
1883
|
+
assert a.shape[0] == offset.shape[0]
|
1884
|
+
|
1885
|
+
@property
|
1886
|
+
def shape(self):
|
1887
|
+
return self.a.shape[0], self.nc
|
1888
|
+
|
1889
|
+
def todense(self):
|
1890
|
+
out = np.zeros(self.shape)
|
1891
|
+
nelem = self.a.shape[1]
|
1892
|
+
for i in range(out.shape[0]):
|
1893
|
+
nel = min(self.nc - self.offset[i], nelem)
|
1894
|
+
out[i, self.offset[i]:self.offset[i] + nel] = self.a[i, :nel]
|
1895
|
+
return out
|
1896
|
+
|
1897
|
+
|
1898
|
+
def _qr_reduce_py(a_p, y, startrow=1):
|
1899
|
+
"""This is a python counterpart of the `_qr_reduce` routine,
|
1900
|
+
declared in interpolate/src/__fitpack.h
|
1901
|
+
"""
|
1902
|
+
from scipy.linalg.lapack import dlartg
|
1903
|
+
|
1904
|
+
# unpack the packed format
|
1905
|
+
a = a_p.a
|
1906
|
+
offset = a_p.offset
|
1907
|
+
nc = a_p.nc
|
1908
|
+
|
1909
|
+
m, nz = a.shape
|
1910
|
+
|
1911
|
+
assert y.shape[0] == m
|
1912
|
+
R = a.copy()
|
1913
|
+
y1 = y.copy()
|
1914
|
+
|
1915
|
+
for i in range(startrow, m):
|
1916
|
+
oi = offset[i]
|
1917
|
+
for j in range(oi, nc):
|
1918
|
+
# rotate only the lower diagonal
|
1919
|
+
if j >= min(i, nc):
|
1920
|
+
break
|
1921
|
+
|
1922
|
+
# In dense format: diag a1[j, j] vs a1[i, j]
|
1923
|
+
c, s, r = dlartg(R[j, 0], R[i, 0])
|
1924
|
+
|
1925
|
+
# rotate l.h.s.
|
1926
|
+
R[j, 0] = r
|
1927
|
+
for l in range(1, nz):
|
1928
|
+
R[j, l], R[i, l-1] = fprota(c, s, R[j, l], R[i, l])
|
1929
|
+
R[i, -1] = 0.0
|
1930
|
+
|
1931
|
+
# rotate r.h.s.
|
1932
|
+
for l in range(y1.shape[1]):
|
1933
|
+
y1[j, l], y1[i, l] = fprota(c, s, y1[j, l], y1[i, l])
|
1934
|
+
|
1935
|
+
# convert to packed
|
1936
|
+
offs = list(range(R.shape[0]))
|
1937
|
+
R_p = PackedMatrix(R, np.array(offs, dtype=np.int64), nc)
|
1938
|
+
|
1939
|
+
return R_p, y1
|
1940
|
+
|
1941
|
+
|
1942
|
+
def fprota(c, s, a, b):
|
1943
|
+
"""Givens rotate [a, b].
|
1944
|
+
|
1945
|
+
[aa] = [ c s] @ [a]
|
1946
|
+
[bb] [-s c] [b]
|
1947
|
+
|
1948
|
+
"""
|
1949
|
+
aa = c*a + s*b
|
1950
|
+
bb = -s*a + c*b
|
1951
|
+
return aa, bb
|
1952
|
+
|
1953
|
+
|
1954
|
+
def fpback(R_p, y):
|
1955
|
+
"""Backsubsitution solve upper triangular banded `R @ c = y.`
|
1956
|
+
|
1957
|
+
`R` is in the "packed" format: `R[i, :]` is `a[i, i:i+k+1]`
|
1958
|
+
"""
|
1959
|
+
R = R_p.a
|
1960
|
+
_, nz = R.shape
|
1961
|
+
nc = R_p.nc
|
1962
|
+
assert y.shape[0] == R.shape[0]
|
1963
|
+
|
1964
|
+
c = np.zeros_like(y[:nc])
|
1965
|
+
c[nc-1, ...] = y[nc-1] / R[nc-1, 0]
|
1966
|
+
for i in range(nc-2, -1, -1):
|
1967
|
+
nel = min(nz, nc-i)
|
1968
|
+
# NB: broadcast R across trailing dimensions of `c`.
|
1969
|
+
summ = (R[i, 1:nel, None] * c[i+1:i+nel, ...]).sum(axis=0)
|
1970
|
+
c[i, ...] = ( y[i] - summ ) / R[i, 0]
|
1971
|
+
return c
|
1972
|
+
|
1973
|
+
|
1974
|
+
class TestGivensQR:
|
1975
|
+
# Test row-by-row QR factorization, used for the LSQ spline construction.
|
1976
|
+
# This is implementation detail; still test it separately.
|
1977
|
+
def _get_xyt(self, n):
|
1978
|
+
k = 3
|
1979
|
+
x = np.arange(n, dtype=float)
|
1980
|
+
y = x**3 + 1/(1+x)
|
1981
|
+
t = _not_a_knot(x, k)
|
1982
|
+
return x, y, t, k
|
1983
|
+
|
1984
|
+
def test_vs_full(self):
|
1985
|
+
n = 10
|
1986
|
+
x, y, t, k = self._get_xyt(n)
|
1987
|
+
|
1988
|
+
# design matrix
|
1989
|
+
a_csr = BSpline.design_matrix(x, t, k)
|
1990
|
+
|
1991
|
+
# dense QR
|
1992
|
+
q, r = sl.qr(a_csr.todense())
|
1993
|
+
qTy = q.T @ y
|
1994
|
+
|
1995
|
+
# prepare the PackedMatrix to factorize
|
1996
|
+
# convert to "packed" format
|
1997
|
+
m, nc = a_csr.shape
|
1998
|
+
assert nc == t.shape[0] - k - 1
|
1999
|
+
|
2000
|
+
offset = a_csr.indices[::(k+1)]
|
2001
|
+
offset = np.ascontiguousarray(offset, dtype=np.int64)
|
2002
|
+
A = a_csr.data.reshape(m, k+1)
|
2003
|
+
|
2004
|
+
R = PackedMatrix(A, offset, nc)
|
2005
|
+
y_ = y[:, None] # _qr_reduce requires `y` a 2D array
|
2006
|
+
_dierckx.qr_reduce(A, offset, nc, y_) # modifies arguments in-place
|
2007
|
+
|
2008
|
+
# signs may differ
|
2009
|
+
xp_assert_close(np.minimum(R.todense() + r,
|
2010
|
+
R.todense() - r), np.zeros_like(r), atol=1e-15)
|
2011
|
+
xp_assert_close(np.minimum(abs(qTy - y_[:, 0]),
|
2012
|
+
abs(qTy + y_[:, 0])), np.zeros_like(qTy), atol=2e-13)
|
2013
|
+
|
2014
|
+
# sign changes are consistent between Q and R:
|
2015
|
+
c_full = sl.solve(r, qTy)
|
2016
|
+
c_banded = _dierckx.fpback(R.a, R.nc, y_)
|
2017
|
+
xp_assert_close(c_full, c_banded[:, 0], atol=5e-13)
|
2018
|
+
|
2019
|
+
def test_py_vs_compiled(self):
|
2020
|
+
# test _qr_reduce vs a python implementation
|
2021
|
+
n = 10
|
2022
|
+
x, y, t, k = self._get_xyt(n)
|
2023
|
+
|
2024
|
+
# design matrix
|
2025
|
+
a_csr = BSpline.design_matrix(x, t, k)
|
2026
|
+
m, nc = a_csr.shape
|
2027
|
+
assert nc == t.shape[0] - k - 1
|
2028
|
+
|
2029
|
+
offset = a_csr.indices[::(k+1)]
|
2030
|
+
offset = np.ascontiguousarray(offset, dtype=np.int64)
|
2031
|
+
A = a_csr.data.reshape(m, k+1)
|
2032
|
+
|
2033
|
+
R = PackedMatrix(A, offset, nc)
|
2034
|
+
y_ = y[:, None]
|
2035
|
+
|
2036
|
+
RR, yy = _qr_reduce_py(R, y_)
|
2037
|
+
_dierckx.qr_reduce(A, offset, nc , y_) # in-place
|
2038
|
+
|
2039
|
+
xp_assert_close(RR.a, R.a, atol=1e-15)
|
2040
|
+
xp_assert_equal(RR.offset, R.offset, check_dtype=False)
|
2041
|
+
assert RR.nc == R.nc
|
2042
|
+
xp_assert_close(yy, y_, atol=1e-15)
|
2043
|
+
|
2044
|
+
# Test C-level construction of the design matrix
|
2045
|
+
|
2046
|
+
def test_data_matrix(self):
|
2047
|
+
n = 10
|
2048
|
+
x, y, t, k = self._get_xyt(n)
|
2049
|
+
w = np.arange(1, n+1, dtype=float)
|
2050
|
+
|
2051
|
+
A, offset, nc = _dierckx.data_matrix(x, t, k, w)
|
2052
|
+
|
2053
|
+
m = x.shape[0]
|
2054
|
+
a_csr = BSpline.design_matrix(x, t, k)
|
2055
|
+
a_w = (a_csr * w[:, None]).tocsr()
|
2056
|
+
A_ = a_w.data.reshape((m, k+1))
|
2057
|
+
offset_ = a_w.indices[::(k+1)].astype(np.int64)
|
2058
|
+
|
2059
|
+
xp_assert_close(A, A_, atol=1e-15)
|
2060
|
+
xp_assert_equal(offset, offset_)
|
2061
|
+
assert nc == t.shape[0] - k - 1
|
2062
|
+
|
2063
|
+
def test_fpback(self):
|
2064
|
+
n = 10
|
2065
|
+
x, y, t, k = self._get_xyt(n)
|
2066
|
+
y = np.c_[y, y**2]
|
2067
|
+
A, offset, nc = _dierckx.data_matrix(x, t, k, np.ones_like(x))
|
2068
|
+
R = PackedMatrix(A, offset, nc)
|
2069
|
+
_dierckx.qr_reduce(A, offset, nc, y)
|
2070
|
+
|
2071
|
+
c = fpback(R, y)
|
2072
|
+
cc = _dierckx.fpback(A, nc, y)
|
2073
|
+
|
2074
|
+
xp_assert_close(cc, c, atol=1e-14)
|
2075
|
+
|
2076
|
+
|
2077
|
+
def data_file(basename):
|
2078
|
+
return os.path.join(os.path.abspath(os.path.dirname(__file__)),
|
2079
|
+
'data', basename)
|
2080
|
+
|
2081
|
+
|
2082
|
+
class TestSmoothingSpline:
|
2083
|
+
#
|
2084
|
+
# test make_smoothing_spline
|
2085
|
+
#
|
2086
|
+
def test_invalid_input(self):
|
2087
|
+
rng = np.random.RandomState(1234)
|
2088
|
+
n = 100
|
2089
|
+
x = np.sort(rng.random_sample(n) * 4 - 2)
|
2090
|
+
y = x**2 * np.sin(4 * x) + x**3 + rng.normal(0., 1.5, n)
|
2091
|
+
|
2092
|
+
# ``x`` and ``y`` should have same shapes (1-D array)
|
2093
|
+
with assert_raises(ValueError):
|
2094
|
+
make_smoothing_spline(x, y[1:])
|
2095
|
+
with assert_raises(ValueError):
|
2096
|
+
make_smoothing_spline(x[1:], y)
|
2097
|
+
with assert_raises(ValueError):
|
2098
|
+
make_smoothing_spline(x.reshape(1, n), y)
|
2099
|
+
|
2100
|
+
# ``x`` should be an ascending array
|
2101
|
+
with assert_raises(ValueError):
|
2102
|
+
make_smoothing_spline(x[::-1], y)
|
2103
|
+
|
2104
|
+
x_dupl = np.copy(x)
|
2105
|
+
x_dupl[0] = x_dupl[1]
|
2106
|
+
|
2107
|
+
with assert_raises(ValueError):
|
2108
|
+
make_smoothing_spline(x_dupl, y)
|
2109
|
+
|
2110
|
+
# x and y length must be >= 5
|
2111
|
+
x = np.arange(4)
|
2112
|
+
y = np.ones(4)
|
2113
|
+
exception_message = "``x`` and ``y`` length must be at least 5"
|
2114
|
+
with pytest.raises(ValueError, match=exception_message):
|
2115
|
+
make_smoothing_spline(x, y)
|
2116
|
+
|
2117
|
+
def test_compare_with_GCVSPL(self):
|
2118
|
+
"""
|
2119
|
+
Data is generated in the following way:
|
2120
|
+
>>> np.random.seed(1234)
|
2121
|
+
>>> n = 100
|
2122
|
+
>>> x = np.sort(np.random.random_sample(n) * 4 - 2)
|
2123
|
+
>>> y = np.sin(x) + np.random.normal(scale=.5, size=n)
|
2124
|
+
>>> np.savetxt('x.csv', x)
|
2125
|
+
>>> np.savetxt('y.csv', y)
|
2126
|
+
|
2127
|
+
We obtain the result of performing the GCV smoothing splines
|
2128
|
+
package (by Woltring, gcvspl) on the sample data points
|
2129
|
+
using its version for Octave (https://github.com/srkuberski/gcvspl).
|
2130
|
+
In order to use this implementation, one should clone the repository
|
2131
|
+
and open the folder in Octave.
|
2132
|
+
In Octave, we load up ``x`` and ``y`` (generated from Python code
|
2133
|
+
above):
|
2134
|
+
|
2135
|
+
>>> x = csvread('x.csv');
|
2136
|
+
>>> y = csvread('y.csv');
|
2137
|
+
|
2138
|
+
Then, in order to access the implementation, we compile gcvspl files in
|
2139
|
+
Octave:
|
2140
|
+
|
2141
|
+
>>> mex gcvsplmex.c gcvspl.c
|
2142
|
+
>>> mex spldermex.c gcvspl.c
|
2143
|
+
|
2144
|
+
The first function computes the vector of unknowns from the dataset
|
2145
|
+
(x, y) while the second one evaluates the spline in certain points
|
2146
|
+
with known vector of coefficients.
|
2147
|
+
|
2148
|
+
>>> c = gcvsplmex( x, y, 2 );
|
2149
|
+
>>> y0 = spldermex( x, c, 2, x, 0 );
|
2150
|
+
|
2151
|
+
If we want to compare the results of the gcvspl code, we can save
|
2152
|
+
``y0`` in csv file:
|
2153
|
+
|
2154
|
+
>>> csvwrite('y0.csv', y0);
|
2155
|
+
|
2156
|
+
"""
|
2157
|
+
# load the data sample
|
2158
|
+
with np.load(data_file('gcvspl.npz')) as data:
|
2159
|
+
# data points
|
2160
|
+
x = data['x']
|
2161
|
+
y = data['y']
|
2162
|
+
|
2163
|
+
y_GCVSPL = data['y_GCVSPL']
|
2164
|
+
y_compr = make_smoothing_spline(x, y)(x)
|
2165
|
+
|
2166
|
+
# such tolerance is explained by the fact that the spline is built
|
2167
|
+
# using an iterative algorithm for minimizing the GCV criteria. These
|
2168
|
+
# algorithms may vary, so the tolerance should be rather low.
|
2169
|
+
# Not checking dtypes as gcvspl.npz stores little endian arrays, which
|
2170
|
+
# result in conflicting dtypes on big endian systems.
|
2171
|
+
xp_assert_close(y_compr, y_GCVSPL, atol=1e-4, rtol=1e-4, check_dtype=False)
|
2172
|
+
|
2173
|
+
def test_non_regularized_case(self):
|
2174
|
+
"""
|
2175
|
+
In case the regularization parameter is 0, the resulting spline
|
2176
|
+
is an interpolation spline with natural boundary conditions.
|
2177
|
+
"""
|
2178
|
+
# create data sample
|
2179
|
+
rng = np.random.RandomState(1234)
|
2180
|
+
n = 100
|
2181
|
+
x = np.sort(rng.random_sample(n) * 4 - 2)
|
2182
|
+
y = x**2 * np.sin(4 * x) + x**3 + rng.normal(0., 1.5, n)
|
2183
|
+
|
2184
|
+
spline_GCV = make_smoothing_spline(x, y, lam=0.)
|
2185
|
+
spline_interp = make_interp_spline(x, y, 3, bc_type='natural')
|
2186
|
+
|
2187
|
+
grid = np.linspace(x[0], x[-1], 2 * n)
|
2188
|
+
xp_assert_close(spline_GCV(grid),
|
2189
|
+
spline_interp(grid),
|
2190
|
+
atol=1e-15)
|
2191
|
+
|
2192
|
+
@pytest.mark.fail_slow(2)
|
2193
|
+
def test_weighted_smoothing_spline(self):
|
2194
|
+
# create data sample
|
2195
|
+
rng = np.random.RandomState(1234)
|
2196
|
+
n = 100
|
2197
|
+
x = np.sort(rng.random_sample(n) * 4 - 2)
|
2198
|
+
y = x**2 * np.sin(4 * x) + x**3 + rng.normal(0., 1.5, n)
|
2199
|
+
|
2200
|
+
spl = make_smoothing_spline(x, y)
|
2201
|
+
|
2202
|
+
# in order not to iterate over all of the indices, we select 10 of
|
2203
|
+
# them randomly
|
2204
|
+
for ind in rng.choice(range(100), size=10):
|
2205
|
+
w = np.ones(n)
|
2206
|
+
w[ind] = 30.
|
2207
|
+
spl_w = make_smoothing_spline(x, y, w)
|
2208
|
+
# check that spline with weight in a certain point is closer to the
|
2209
|
+
# original point than the one without weights
|
2210
|
+
orig = abs(spl(x[ind]) - y[ind])
|
2211
|
+
weighted = abs(spl_w(x[ind]) - y[ind])
|
2212
|
+
|
2213
|
+
if orig < weighted:
|
2214
|
+
raise ValueError(f'Spline with weights should be closer to the'
|
2215
|
+
f' points than the original one: {orig:.4} < '
|
2216
|
+
f'{weighted:.4}')
|
2217
|
+
|
2218
|
+
|
2219
|
+
################################
|
2220
|
+
# NdBSpline tests
|
2221
|
+
def bspline2(xy, t, c, k):
|
2222
|
+
"""A naive 2D tensort product spline evaluation."""
|
2223
|
+
x, y = xy
|
2224
|
+
tx, ty = t
|
2225
|
+
nx = len(tx) - k - 1
|
2226
|
+
assert (nx >= k+1)
|
2227
|
+
ny = len(ty) - k - 1
|
2228
|
+
assert (ny >= k+1)
|
2229
|
+
res = sum(c[ix, iy] * B(x, k, ix, tx) * B(y, k, iy, ty)
|
2230
|
+
for ix in range(nx) for iy in range(ny))
|
2231
|
+
return np.asarray(res)
|
2232
|
+
|
2233
|
+
|
2234
|
+
def B(x, k, i, t):
|
2235
|
+
if k == 0:
|
2236
|
+
return 1.0 if t[i] <= x < t[i+1] else 0.0
|
2237
|
+
if t[i+k] == t[i]:
|
2238
|
+
c1 = 0.0
|
2239
|
+
else:
|
2240
|
+
c1 = (x - t[i])/(t[i+k] - t[i]) * B(x, k-1, i, t)
|
2241
|
+
if t[i+k+1] == t[i+1]:
|
2242
|
+
c2 = 0.0
|
2243
|
+
else:
|
2244
|
+
c2 = (t[i+k+1] - x)/(t[i+k+1] - t[i+1]) * B(x, k-1, i+1, t)
|
2245
|
+
return c1 + c2
|
2246
|
+
|
2247
|
+
|
2248
|
+
def bspline(x, t, c, k):
|
2249
|
+
n = len(t) - k - 1
|
2250
|
+
assert (n >= k+1) and (len(c) >= n)
|
2251
|
+
return sum(c[i] * B(x, k, i, t) for i in range(n))
|
2252
|
+
|
2253
|
+
|
2254
|
+
class NdBSpline0:
|
2255
|
+
def __init__(self, t, c, k=3):
|
2256
|
+
"""Tensor product spline object.
|
2257
|
+
|
2258
|
+
c[i1, i2, ..., id] * B(x1, i1) * B(x2, i2) * ... * B(xd, id)
|
2259
|
+
|
2260
|
+
Parameters
|
2261
|
+
----------
|
2262
|
+
c : ndarray, shape (n1, n2, ..., nd, ...)
|
2263
|
+
b-spline coefficients
|
2264
|
+
t : tuple of 1D ndarrays
|
2265
|
+
knot vectors in directions 1, 2, ... d
|
2266
|
+
``len(t[i]) == n[i] + k + 1``
|
2267
|
+
k : int or length-d tuple of integers
|
2268
|
+
spline degrees.
|
2269
|
+
"""
|
2270
|
+
ndim = len(t)
|
2271
|
+
assert ndim <= len(c.shape)
|
2272
|
+
|
2273
|
+
try:
|
2274
|
+
len(k)
|
2275
|
+
except TypeError:
|
2276
|
+
# make k a tuple
|
2277
|
+
k = (k,)*ndim
|
2278
|
+
|
2279
|
+
self.k = tuple(operator.index(ki) for ki in k)
|
2280
|
+
self.t = tuple(np.asarray(ti, dtype=float) for ti in t)
|
2281
|
+
self.c = c
|
2282
|
+
|
2283
|
+
def __call__(self, x):
|
2284
|
+
ndim = len(self.t)
|
2285
|
+
# a single evaluation point: `x` is a 1D array_like, shape (ndim,)
|
2286
|
+
assert len(x) == ndim
|
2287
|
+
|
2288
|
+
# get the indices in an ndim-dimensional vector
|
2289
|
+
i = ['none', ]*ndim
|
2290
|
+
for d in range(ndim):
|
2291
|
+
td, xd = self.t[d], x[d]
|
2292
|
+
k = self.k[d]
|
2293
|
+
|
2294
|
+
# find the index for x[d]
|
2295
|
+
if xd == td[k]:
|
2296
|
+
i[d] = k
|
2297
|
+
else:
|
2298
|
+
i[d] = np.searchsorted(td, xd) - 1
|
2299
|
+
assert td[i[d]] <= xd <= td[i[d]+1]
|
2300
|
+
assert i[d] >= k and i[d] < len(td) - k
|
2301
|
+
i = tuple(i)
|
2302
|
+
|
2303
|
+
# iterate over the dimensions, form linear combinations of
|
2304
|
+
# products B(x_1) * B(x_2) * ... B(x_N) of (k+1)**N b-splines
|
2305
|
+
# which are non-zero at `i = (i_1, i_2, ..., i_N)`.
|
2306
|
+
result = 0
|
2307
|
+
iters = [range(i[d] - self.k[d], i[d] + 1) for d in range(ndim)]
|
2308
|
+
for idx in itertools.product(*iters):
|
2309
|
+
term = self.c[idx] * np.prod([B(x[d], self.k[d], idx[d], self.t[d])
|
2310
|
+
for d in range(ndim)])
|
2311
|
+
result += term
|
2312
|
+
return np.asarray(result)
|
2313
|
+
|
2314
|
+
|
2315
|
+
class TestNdBSpline:
|
2316
|
+
|
2317
|
+
def test_1D(self):
|
2318
|
+
# test ndim=1 agrees with BSpline
|
2319
|
+
rng = np.random.default_rng(12345)
|
2320
|
+
n, k = 11, 3
|
2321
|
+
n_tr = 7
|
2322
|
+
t = np.sort(rng.uniform(size=n + k + 1))
|
2323
|
+
c = rng.uniform(size=(n, n_tr))
|
2324
|
+
|
2325
|
+
b = BSpline(t, c, k)
|
2326
|
+
nb = NdBSpline((t,), c, k)
|
2327
|
+
|
2328
|
+
xi = rng.uniform(size=21)
|
2329
|
+
# NdBSpline expects xi.shape=(npts, ndim)
|
2330
|
+
xp_assert_close(nb(xi[:, None]),
|
2331
|
+
b(xi), atol=1e-14)
|
2332
|
+
assert nb(xi[:, None]).shape == (xi.shape[0], c.shape[1])
|
2333
|
+
|
2334
|
+
def make_2d_case(self):
|
2335
|
+
# make a 2D separable spline
|
2336
|
+
x = np.arange(6)
|
2337
|
+
y = x**3
|
2338
|
+
spl = make_interp_spline(x, y, k=3)
|
2339
|
+
|
2340
|
+
y_1 = x**3 + 2*x
|
2341
|
+
spl_1 = make_interp_spline(x, y_1, k=3)
|
2342
|
+
|
2343
|
+
t2 = (spl.t, spl_1.t)
|
2344
|
+
c2 = spl.c[:, None] * spl_1.c[None, :]
|
2345
|
+
|
2346
|
+
return t2, c2, 3
|
2347
|
+
|
2348
|
+
def make_2d_mixed(self):
|
2349
|
+
# make a 2D separable spline w/ kx=3, ky=2
|
2350
|
+
x = np.arange(6)
|
2351
|
+
y = x**3
|
2352
|
+
spl = make_interp_spline(x, y, k=3)
|
2353
|
+
|
2354
|
+
x = np.arange(5) + 1.5
|
2355
|
+
y_1 = x**2 + 2*x
|
2356
|
+
spl_1 = make_interp_spline(x, y_1, k=2)
|
2357
|
+
|
2358
|
+
t2 = (spl.t, spl_1.t)
|
2359
|
+
c2 = spl.c[:, None] * spl_1.c[None, :]
|
2360
|
+
|
2361
|
+
return t2, c2, spl.k, spl_1.k
|
2362
|
+
|
2363
|
+
def test_2D_separable(self):
|
2364
|
+
xi = [(1.5, 2.5), (2.5, 1), (0.5, 1.5)]
|
2365
|
+
t2, c2, k = self.make_2d_case()
|
2366
|
+
target = [x**3 * (y**3 + 2*y) for (x, y) in xi]
|
2367
|
+
|
2368
|
+
# sanity check: bspline2 gives the product as constructed
|
2369
|
+
xp_assert_close(np.asarray([bspline2(xy, t2, c2, k) for xy in xi]),
|
2370
|
+
np.asarray(target),
|
2371
|
+
check_shape=False,
|
2372
|
+
atol=1e-14)
|
2373
|
+
|
2374
|
+
# check evaluation on a 2D array: the 1D array of 2D points
|
2375
|
+
bspl2 = NdBSpline(t2, c2, k=3)
|
2376
|
+
assert bspl2(xi).shape == (len(xi), )
|
2377
|
+
xp_assert_close(bspl2(xi),
|
2378
|
+
target, atol=1e-14)
|
2379
|
+
|
2380
|
+
# test that a nan in -> nan out
|
2381
|
+
xi = np.asarray(xi)
|
2382
|
+
xi[0, 1] = np.nan
|
2383
|
+
xp_assert_equal(np.isnan(bspl2(xi)), np.asarray([True, False, False]))
|
2384
|
+
|
2385
|
+
# now check on a multidim xi
|
2386
|
+
rng = np.random.default_rng(12345)
|
2387
|
+
xi = rng.uniform(size=(4, 3, 2)) * 5
|
2388
|
+
result = bspl2(xi)
|
2389
|
+
assert result.shape == (4, 3)
|
2390
|
+
|
2391
|
+
# also check the values
|
2392
|
+
x, y = xi.reshape((-1, 2)).T
|
2393
|
+
xp_assert_close(result.ravel(),
|
2394
|
+
x**3 * (y**3 + 2*y), atol=1e-14)
|
2395
|
+
|
2396
|
+
def test_2D_separable_2(self):
|
2397
|
+
# test `c` with trailing dimensions, i.e. c.ndim > ndim
|
2398
|
+
ndim = 2
|
2399
|
+
xi = [(1.5, 2.5), (2.5, 1), (0.5, 1.5)]
|
2400
|
+
target = [x**3 * (y**3 + 2*y) for (x, y) in xi]
|
2401
|
+
|
2402
|
+
t2, c2, k = self.make_2d_case()
|
2403
|
+
c2_4 = np.dstack((c2, c2, c2, c2)) # c22.shape = (6, 6, 4)
|
2404
|
+
|
2405
|
+
xy = (1.5, 2.5)
|
2406
|
+
bspl2_4 = NdBSpline(t2, c2_4, k=3)
|
2407
|
+
result = bspl2_4(xy)
|
2408
|
+
val_single = NdBSpline(t2, c2, k)(xy)
|
2409
|
+
assert result.shape == (4,)
|
2410
|
+
xp_assert_close(result,
|
2411
|
+
[val_single, ]*4, atol=1e-14)
|
2412
|
+
|
2413
|
+
# now try the array xi : the output.shape is (3, 4) where 3
|
2414
|
+
# is the number of points in xi and 4 is the trailing dimension of c
|
2415
|
+
assert bspl2_4(xi).shape == np.shape(xi)[:-1] + bspl2_4.c.shape[ndim:]
|
2416
|
+
xp_assert_close(bspl2_4(xi), np.asarray(target)[:, None],
|
2417
|
+
check_shape=False,
|
2418
|
+
atol=5e-14)
|
2419
|
+
|
2420
|
+
# two trailing dimensions
|
2421
|
+
c2_22 = c2_4.reshape((6, 6, 2, 2))
|
2422
|
+
bspl2_22 = NdBSpline(t2, c2_22, k=3)
|
2423
|
+
|
2424
|
+
result = bspl2_22(xy)
|
2425
|
+
assert result.shape == (2, 2)
|
2426
|
+
xp_assert_close(result,
|
2427
|
+
[[val_single, val_single],
|
2428
|
+
[val_single, val_single]], atol=1e-14)
|
2429
|
+
|
2430
|
+
# now try the array xi : the output shape is (3, 2, 2)
|
2431
|
+
# for 3 points in xi and c trailing dimensions being (2, 2)
|
2432
|
+
assert (bspl2_22(xi).shape ==
|
2433
|
+
np.shape(xi)[:-1] + bspl2_22.c.shape[ndim:])
|
2434
|
+
xp_assert_close(bspl2_22(xi), np.asarray(target)[:, None, None],
|
2435
|
+
check_shape=False,
|
2436
|
+
atol=5e-14)
|
2437
|
+
|
2438
|
+
|
2439
|
+
def test_2D_separable_2_complex(self):
|
2440
|
+
# test `c` with c.dtype == complex, with and w/o trailing dims
|
2441
|
+
xi = [(1.5, 2.5), (2.5, 1), (0.5, 1.5)]
|
2442
|
+
target = [x**3 * (y**3 + 2*y) for (x, y) in xi]
|
2443
|
+
|
2444
|
+
target = [t + 2j*t for t in target]
|
2445
|
+
|
2446
|
+
t2, c2, k = self.make_2d_case()
|
2447
|
+
c2 = c2 * (1 + 2j)
|
2448
|
+
c2_4 = np.dstack((c2, c2, c2, c2)) # c2_4.shape = (6, 6, 4)
|
2449
|
+
|
2450
|
+
xy = (1.5, 2.5)
|
2451
|
+
bspl2_4 = NdBSpline(t2, c2_4, k=3)
|
2452
|
+
result = bspl2_4(xy)
|
2453
|
+
val_single = NdBSpline(t2, c2, k)(xy)
|
2454
|
+
assert result.shape == (4,)
|
2455
|
+
xp_assert_close(result,
|
2456
|
+
[val_single, ]*4, atol=1e-14)
|
2457
|
+
|
2458
|
+
def test_2D_random(self):
|
2459
|
+
rng = np.random.default_rng(12345)
|
2460
|
+
k = 3
|
2461
|
+
tx = np.r_[0, 0, 0, 0, np.sort(rng.uniform(size=7)) * 3, 3, 3, 3, 3]
|
2462
|
+
ty = np.r_[0, 0, 0, 0, np.sort(rng.uniform(size=8)) * 4, 4, 4, 4, 4]
|
2463
|
+
c = rng.uniform(size=(tx.size-k-1, ty.size-k-1))
|
2464
|
+
|
2465
|
+
spl = NdBSpline((tx, ty), c, k=k)
|
2466
|
+
|
2467
|
+
xi = (1., 1.)
|
2468
|
+
xp_assert_close(spl(xi),
|
2469
|
+
bspline2(xi, (tx, ty), c, k), atol=1e-14)
|
2470
|
+
|
2471
|
+
xi = np.c_[[1, 1.5, 2],
|
2472
|
+
[1.1, 1.6, 2.1]]
|
2473
|
+
xp_assert_close(spl(xi),
|
2474
|
+
[bspline2(xy, (tx, ty), c, k) for xy in xi],
|
2475
|
+
atol=1e-14)
|
2476
|
+
|
2477
|
+
def test_2D_mixed(self):
|
2478
|
+
t2, c2, kx, ky = self.make_2d_mixed()
|
2479
|
+
xi = [(1.4, 4.5), (2.5, 2.4), (4.5, 3.5)]
|
2480
|
+
target = [x**3 * (y**2 + 2*y) for (x, y) in xi]
|
2481
|
+
bspl2 = NdBSpline(t2, c2, k=(kx, ky))
|
2482
|
+
assert bspl2(xi).shape == (len(xi), )
|
2483
|
+
xp_assert_close(bspl2(xi),
|
2484
|
+
target, atol=1e-14)
|
2485
|
+
|
2486
|
+
def test_2D_derivative(self):
|
2487
|
+
t2, c2, kx, ky = self.make_2d_mixed()
|
2488
|
+
xi = [(1.4, 4.5), (2.5, 2.4), (4.5, 3.5)]
|
2489
|
+
bspl2 = NdBSpline(t2, c2, k=(kx, ky))
|
2490
|
+
|
2491
|
+
der = bspl2(xi, nu=(1, 0))
|
2492
|
+
xp_assert_close(der,
|
2493
|
+
[3*x**2 * (y**2 + 2*y) for x, y in xi], atol=1e-14)
|
2494
|
+
|
2495
|
+
der = bspl2(xi, nu=(1, 1))
|
2496
|
+
xp_assert_close(der,
|
2497
|
+
[3*x**2 * (2*y + 2) for x, y in xi], atol=1e-14)
|
2498
|
+
|
2499
|
+
der = bspl2(xi, nu=(0, 0))
|
2500
|
+
xp_assert_close(der,
|
2501
|
+
[x**3 * (y**2 + 2*y) for x, y in xi], atol=1e-14)
|
2502
|
+
|
2503
|
+
with assert_raises(ValueError):
|
2504
|
+
# all(nu >= 0)
|
2505
|
+
der = bspl2(xi, nu=(-1, 0))
|
2506
|
+
|
2507
|
+
with assert_raises(ValueError):
|
2508
|
+
# len(nu) == ndim
|
2509
|
+
der = bspl2(xi, nu=(-1, 0, 1))
|
2510
|
+
|
2511
|
+
def test_2D_mixed_random(self):
|
2512
|
+
rng = np.random.default_rng(12345)
|
2513
|
+
kx, ky = 2, 3
|
2514
|
+
tx = np.r_[0, 0, 0, 0, np.sort(rng.uniform(size=7)) * 3, 3, 3, 3, 3]
|
2515
|
+
ty = np.r_[0, 0, 0, 0, np.sort(rng.uniform(size=8)) * 4, 4, 4, 4, 4]
|
2516
|
+
c = rng.uniform(size=(tx.size - kx - 1, ty.size - ky - 1))
|
2517
|
+
|
2518
|
+
xi = np.c_[[1, 1.5, 2],
|
2519
|
+
[1.1, 1.6, 2.1]]
|
2520
|
+
|
2521
|
+
bspl2 = NdBSpline((tx, ty), c, k=(kx, ky))
|
2522
|
+
bspl2_0 = NdBSpline0((tx, ty), c, k=(kx, ky))
|
2523
|
+
|
2524
|
+
xp_assert_close(bspl2(xi),
|
2525
|
+
[bspl2_0(xp) for xp in xi], atol=1e-14)
|
2526
|
+
|
2527
|
+
def test_tx_neq_ty(self):
|
2528
|
+
# 2D separable spline w/ len(tx) != len(ty)
|
2529
|
+
x = np.arange(6)
|
2530
|
+
y = np.arange(7) + 1.5
|
2531
|
+
|
2532
|
+
spl_x = make_interp_spline(x, x**3, k=3)
|
2533
|
+
spl_y = make_interp_spline(y, y**2 + 2*y, k=3)
|
2534
|
+
cc = spl_x.c[:, None] * spl_y.c[None, :]
|
2535
|
+
bspl = NdBSpline((spl_x.t, spl_y.t), cc, (spl_x.k, spl_y.k))
|
2536
|
+
|
2537
|
+
values = (x**3)[:, None] * (y**2 + 2*y)[None, :]
|
2538
|
+
rgi = RegularGridInterpolator((x, y), values)
|
2539
|
+
|
2540
|
+
xi = [(a, b) for a, b in itertools.product(x, y)]
|
2541
|
+
bxi = bspl(xi)
|
2542
|
+
|
2543
|
+
assert not np.isnan(bxi).any()
|
2544
|
+
xp_assert_close(bxi, rgi(xi), atol=1e-14)
|
2545
|
+
xp_assert_close(bxi.reshape(values.shape), values, atol=1e-14)
|
2546
|
+
|
2547
|
+
def make_3d_case(self):
|
2548
|
+
# make a 3D separable spline
|
2549
|
+
x = np.arange(6)
|
2550
|
+
y = x**3
|
2551
|
+
spl = make_interp_spline(x, y, k=3)
|
2552
|
+
|
2553
|
+
y_1 = x**3 + 2*x
|
2554
|
+
spl_1 = make_interp_spline(x, y_1, k=3)
|
2555
|
+
|
2556
|
+
y_2 = x**3 + 3*x + 1
|
2557
|
+
spl_2 = make_interp_spline(x, y_2, k=3)
|
2558
|
+
|
2559
|
+
t2 = (spl.t, spl_1.t, spl_2.t)
|
2560
|
+
c2 = (spl.c[:, None, None] *
|
2561
|
+
spl_1.c[None, :, None] *
|
2562
|
+
spl_2.c[None, None, :])
|
2563
|
+
|
2564
|
+
return t2, c2, 3
|
2565
|
+
|
2566
|
+
def test_3D_separable(self):
|
2567
|
+
rng = np.random.default_rng(12345)
|
2568
|
+
x, y, z = rng.uniform(size=(3, 11)) * 5
|
2569
|
+
target = x**3 * (y**3 + 2*y) * (z**3 + 3*z + 1)
|
2570
|
+
|
2571
|
+
t3, c3, k = self.make_3d_case()
|
2572
|
+
bspl3 = NdBSpline(t3, c3, k=3)
|
2573
|
+
|
2574
|
+
xi = [_ for _ in zip(x, y, z)]
|
2575
|
+
result = bspl3(xi)
|
2576
|
+
assert result.shape == (11,)
|
2577
|
+
xp_assert_close(result, target, atol=1e-14)
|
2578
|
+
|
2579
|
+
def test_3D_derivative(self):
|
2580
|
+
t3, c3, k = self.make_3d_case()
|
2581
|
+
bspl3 = NdBSpline(t3, c3, k=3)
|
2582
|
+
rng = np.random.default_rng(12345)
|
2583
|
+
x, y, z = rng.uniform(size=(3, 11)) * 5
|
2584
|
+
xi = [_ for _ in zip(x, y, z)]
|
2585
|
+
|
2586
|
+
xp_assert_close(bspl3(xi, nu=(1, 0, 0)),
|
2587
|
+
3*x**2 * (y**3 + 2*y) * (z**3 + 3*z + 1), atol=1e-14)
|
2588
|
+
|
2589
|
+
xp_assert_close(bspl3(xi, nu=(2, 0, 0)),
|
2590
|
+
6*x * (y**3 + 2*y) * (z**3 + 3*z + 1), atol=1e-14)
|
2591
|
+
|
2592
|
+
xp_assert_close(bspl3(xi, nu=(2, 1, 0)),
|
2593
|
+
6*x * (3*y**2 + 2) * (z**3 + 3*z + 1), atol=1e-14)
|
2594
|
+
|
2595
|
+
xp_assert_close(bspl3(xi, nu=(2, 1, 3)),
|
2596
|
+
6*x * (3*y**2 + 2) * (6), atol=1e-14)
|
2597
|
+
|
2598
|
+
xp_assert_close(bspl3(xi, nu=(2, 1, 4)),
|
2599
|
+
np.zeros(len(xi)), atol=1e-14)
|
2600
|
+
|
2601
|
+
def test_3D_random(self):
|
2602
|
+
rng = np.random.default_rng(12345)
|
2603
|
+
k = 3
|
2604
|
+
tx = np.r_[0, 0, 0, 0, np.sort(rng.uniform(size=7)) * 3, 3, 3, 3, 3]
|
2605
|
+
ty = np.r_[0, 0, 0, 0, np.sort(rng.uniform(size=8)) * 4, 4, 4, 4, 4]
|
2606
|
+
tz = np.r_[0, 0, 0, 0, np.sort(rng.uniform(size=8)) * 4, 4, 4, 4, 4]
|
2607
|
+
c = rng.uniform(size=(tx.size-k-1, ty.size-k-1, tz.size-k-1))
|
2608
|
+
|
2609
|
+
spl = NdBSpline((tx, ty, tz), c, k=k)
|
2610
|
+
spl_0 = NdBSpline0((tx, ty, tz), c, k=k)
|
2611
|
+
|
2612
|
+
xi = (1., 1., 1)
|
2613
|
+
xp_assert_close(spl(xi), spl_0(xi), atol=1e-14)
|
2614
|
+
|
2615
|
+
xi = np.c_[[1, 1.5, 2],
|
2616
|
+
[1.1, 1.6, 2.1],
|
2617
|
+
[0.9, 1.4, 1.9]]
|
2618
|
+
xp_assert_close(spl(xi), [spl_0(xp) for xp in xi], atol=1e-14)
|
2619
|
+
|
2620
|
+
def test_3D_random_complex(self):
|
2621
|
+
rng = np.random.default_rng(12345)
|
2622
|
+
k = 3
|
2623
|
+
tx = np.r_[0, 0, 0, 0, np.sort(rng.uniform(size=7)) * 3, 3, 3, 3, 3]
|
2624
|
+
ty = np.r_[0, 0, 0, 0, np.sort(rng.uniform(size=8)) * 4, 4, 4, 4, 4]
|
2625
|
+
tz = np.r_[0, 0, 0, 0, np.sort(rng.uniform(size=8)) * 4, 4, 4, 4, 4]
|
2626
|
+
c = (rng.uniform(size=(tx.size-k-1, ty.size-k-1, tz.size-k-1)) +
|
2627
|
+
rng.uniform(size=(tx.size-k-1, ty.size-k-1, tz.size-k-1))*1j)
|
2628
|
+
|
2629
|
+
spl = NdBSpline((tx, ty, tz), c, k=k)
|
2630
|
+
spl_re = NdBSpline((tx, ty, tz), c.real, k=k)
|
2631
|
+
spl_im = NdBSpline((tx, ty, tz), c.imag, k=k)
|
2632
|
+
|
2633
|
+
xi = np.c_[[1, 1.5, 2],
|
2634
|
+
[1.1, 1.6, 2.1],
|
2635
|
+
[0.9, 1.4, 1.9]]
|
2636
|
+
xp_assert_close(spl(xi),
|
2637
|
+
spl_re(xi) + 1j*spl_im(xi), atol=1e-14)
|
2638
|
+
|
2639
|
+
@pytest.mark.parametrize('cls_extrap', [None, True])
|
2640
|
+
@pytest.mark.parametrize('call_extrap', [None, True])
|
2641
|
+
def test_extrapolate_3D_separable(self, cls_extrap, call_extrap):
|
2642
|
+
# test that extrapolate=True does extrapolate
|
2643
|
+
t3, c3, k = self.make_3d_case()
|
2644
|
+
bspl3 = NdBSpline(t3, c3, k=3, extrapolate=cls_extrap)
|
2645
|
+
|
2646
|
+
# evaluate out of bounds
|
2647
|
+
x, y, z = [-2, -1, 7], [-3, -0.5, 6.5], [-1, -1.5, 7.5]
|
2648
|
+
x, y, z = map(np.asarray, (x, y, z))
|
2649
|
+
xi = [_ for _ in zip(x, y, z)]
|
2650
|
+
target = x**3 * (y**3 + 2*y) * (z**3 + 3*z + 1)
|
2651
|
+
|
2652
|
+
result = bspl3(xi, extrapolate=call_extrap)
|
2653
|
+
xp_assert_close(result, target, atol=1e-14)
|
2654
|
+
|
2655
|
+
@pytest.mark.parametrize('extrap', [(False, True), (True, None)])
|
2656
|
+
def test_extrapolate_3D_separable_2(self, extrap):
|
2657
|
+
# test that call(..., extrapolate=None) defers to self.extrapolate,
|
2658
|
+
# otherwise supersedes self.extrapolate
|
2659
|
+
t3, c3, k = self.make_3d_case()
|
2660
|
+
cls_extrap, call_extrap = extrap
|
2661
|
+
bspl3 = NdBSpline(t3, c3, k=3, extrapolate=cls_extrap)
|
2662
|
+
|
2663
|
+
# evaluate out of bounds
|
2664
|
+
x, y, z = [-2, -1, 7], [-3, -0.5, 6.5], [-1, -1.5, 7.5]
|
2665
|
+
x, y, z = map(np.asarray, (x, y, z))
|
2666
|
+
xi = [_ for _ in zip(x, y, z)]
|
2667
|
+
target = x**3 * (y**3 + 2*y) * (z**3 + 3*z + 1)
|
2668
|
+
|
2669
|
+
result = bspl3(xi, extrapolate=call_extrap)
|
2670
|
+
xp_assert_close(result, target, atol=1e-14)
|
2671
|
+
|
2672
|
+
def test_extrapolate_false_3D_separable(self):
|
2673
|
+
# test that extrapolate=False produces nans for out-of-bounds values
|
2674
|
+
t3, c3, k = self.make_3d_case()
|
2675
|
+
bspl3 = NdBSpline(t3, c3, k=3)
|
2676
|
+
|
2677
|
+
# evaluate out of bounds and inside
|
2678
|
+
x, y, z = [-2, 1, 7], [-3, 0.5, 6.5], [-1, 1.5, 7.5]
|
2679
|
+
x, y, z = map(np.asarray, (x, y, z))
|
2680
|
+
xi = [_ for _ in zip(x, y, z)]
|
2681
|
+
target = x**3 * (y**3 + 2*y) * (z**3 + 3*z + 1)
|
2682
|
+
|
2683
|
+
result = bspl3(xi, extrapolate=False)
|
2684
|
+
assert np.isnan(result[0])
|
2685
|
+
assert np.isnan(result[-1])
|
2686
|
+
xp_assert_close(result[1:-1], target[1:-1], atol=1e-14)
|
2687
|
+
|
2688
|
+
def test_x_nan_3D(self):
|
2689
|
+
# test that spline(nan) is nan
|
2690
|
+
t3, c3, k = self.make_3d_case()
|
2691
|
+
bspl3 = NdBSpline(t3, c3, k=3)
|
2692
|
+
|
2693
|
+
# evaluate out of bounds and inside
|
2694
|
+
x = np.asarray([-2, 3, np.nan, 1, 2, 7, np.nan])
|
2695
|
+
y = np.asarray([-3, 3.5, 1, np.nan, 3, 6.5, 6.5])
|
2696
|
+
z = np.asarray([-1, 3.5, 2, 3, np.nan, 7.5, 7.5])
|
2697
|
+
xi = [_ for _ in zip(x, y, z)]
|
2698
|
+
target = x**3 * (y**3 + 2*y) * (z**3 + 3*z + 1)
|
2699
|
+
mask = np.isnan(x) | np.isnan(y) | np.isnan(z)
|
2700
|
+
target[mask] = np.nan
|
2701
|
+
|
2702
|
+
result = bspl3(xi)
|
2703
|
+
assert np.isnan(result[mask]).all()
|
2704
|
+
xp_assert_close(result, target, atol=1e-14)
|
2705
|
+
|
2706
|
+
def test_non_c_contiguous(self):
|
2707
|
+
# check that non C-contiguous inputs are OK
|
2708
|
+
rng = np.random.default_rng(12345)
|
2709
|
+
kx, ky = 3, 3
|
2710
|
+
tx = np.sort(rng.uniform(low=0, high=4, size=16))
|
2711
|
+
tx = np.r_[(tx[0],)*kx, tx, (tx[-1],)*kx]
|
2712
|
+
ty = np.sort(rng.uniform(low=0, high=4, size=16))
|
2713
|
+
ty = np.r_[(ty[0],)*ky, ty, (ty[-1],)*ky]
|
2714
|
+
|
2715
|
+
assert not tx[::2].flags.c_contiguous
|
2716
|
+
assert not ty[::2].flags.c_contiguous
|
2717
|
+
|
2718
|
+
c = rng.uniform(size=(tx.size//2 - kx - 1, ty.size//2 - ky - 1))
|
2719
|
+
c = c.T
|
2720
|
+
assert not c.flags.c_contiguous
|
2721
|
+
|
2722
|
+
xi = np.c_[[1, 1.5, 2],
|
2723
|
+
[1.1, 1.6, 2.1]]
|
2724
|
+
|
2725
|
+
bspl2 = NdBSpline((tx[::2], ty[::2]), c, k=(kx, ky))
|
2726
|
+
bspl2_0 = NdBSpline0((tx[::2], ty[::2]), c, k=(kx, ky))
|
2727
|
+
|
2728
|
+
xp_assert_close(bspl2(xi),
|
2729
|
+
[bspl2_0(xp) for xp in xi], atol=1e-14)
|
2730
|
+
|
2731
|
+
def test_readonly(self):
|
2732
|
+
t3, c3, k = self.make_3d_case()
|
2733
|
+
bspl3 = NdBSpline(t3, c3, k=3)
|
2734
|
+
|
2735
|
+
for i in range(3):
|
2736
|
+
t3[i].flags.writeable = False
|
2737
|
+
c3.flags.writeable = False
|
2738
|
+
|
2739
|
+
bspl3_ = NdBSpline(t3, c3, k=3)
|
2740
|
+
|
2741
|
+
assert bspl3((1, 2, 3)) == bspl3_((1, 2, 3))
|
2742
|
+
|
2743
|
+
def test_design_matrix(self):
|
2744
|
+
t3, c3, k = self.make_3d_case()
|
2745
|
+
|
2746
|
+
xi = np.asarray([[1, 2, 3], [4, 5, 6]])
|
2747
|
+
dm = NdBSpline(t3, c3, k).design_matrix(xi, t3, k)
|
2748
|
+
dm1 = NdBSpline.design_matrix(xi, t3, [k, k, k])
|
2749
|
+
assert dm.shape[0] == xi.shape[0]
|
2750
|
+
xp_assert_close(dm.todense(), dm1.todense(), atol=1e-16)
|
2751
|
+
|
2752
|
+
with assert_raises(ValueError):
|
2753
|
+
NdBSpline.design_matrix([1, 2, 3], t3, [k]*3)
|
2754
|
+
|
2755
|
+
with assert_raises(ValueError, match="Data and knots*"):
|
2756
|
+
NdBSpline.design_matrix([[1, 2]], t3, [k]*3)
|
2757
|
+
|
2758
|
+
@pytest.mark.thread_unsafe
|
2759
|
+
def test_concurrency(self):
|
2760
|
+
rng = np.random.default_rng(12345)
|
2761
|
+
k = 3
|
2762
|
+
tx = np.r_[0, 0, 0, 0, np.sort(rng.uniform(size=7)) * 3, 3, 3, 3, 3]
|
2763
|
+
ty = np.r_[0, 0, 0, 0, np.sort(rng.uniform(size=8)) * 4, 4, 4, 4, 4]
|
2764
|
+
tz = np.r_[0, 0, 0, 0, np.sort(rng.uniform(size=8)) * 4, 4, 4, 4, 4]
|
2765
|
+
c = rng.uniform(size=(tx.size-k-1, ty.size-k-1, tz.size-k-1))
|
2766
|
+
|
2767
|
+
spl = NdBSpline((tx, ty, tz), c, k=k)
|
2768
|
+
|
2769
|
+
def worker_fn(_, spl):
|
2770
|
+
xi = np.c_[[1, 1.5, 2],
|
2771
|
+
[1.1, 1.6, 2.1],
|
2772
|
+
[0.9, 1.4, 1.9]]
|
2773
|
+
spl(xi)
|
2774
|
+
|
2775
|
+
_run_concurrent_barrier(10, worker_fn, spl)
|
2776
|
+
|
2777
|
+
|
2778
|
+
class TestMakeND:
|
2779
|
+
def test_2D_separable_simple(self):
|
2780
|
+
x = np.arange(6)
|
2781
|
+
y = np.arange(6) + 0.5
|
2782
|
+
values = x[:, None]**3 * (y**3 + 2*y)[None, :]
|
2783
|
+
xi = [(a, b) for a, b in itertools.product(x, y)]
|
2784
|
+
|
2785
|
+
bspl = make_ndbspl((x, y), values, k=1)
|
2786
|
+
xp_assert_close(bspl(xi), values.ravel(), atol=1e-15)
|
2787
|
+
|
2788
|
+
# test the coefficients vs outer product of 1D coefficients
|
2789
|
+
spl_x = make_interp_spline(x, x**3, k=1)
|
2790
|
+
spl_y = make_interp_spline(y, y**3 + 2*y, k=1)
|
2791
|
+
cc = spl_x.c[:, None] * spl_y.c[None, :]
|
2792
|
+
xp_assert_close(cc, bspl.c, atol=1e-11, rtol=0)
|
2793
|
+
|
2794
|
+
# test against RGI
|
2795
|
+
from scipy.interpolate import RegularGridInterpolator as RGI
|
2796
|
+
rgi = RGI((x, y), values, method='linear')
|
2797
|
+
xp_assert_close(rgi(xi), bspl(xi), atol=1e-14)
|
2798
|
+
|
2799
|
+
def test_2D_separable_trailing_dims(self):
|
2800
|
+
# test `c` with trailing dimensions, i.e. c.ndim > ndim
|
2801
|
+
x = np.arange(6)
|
2802
|
+
y = np.arange(6)
|
2803
|
+
xi = [(a, b) for a, b in itertools.product(x, y)]
|
2804
|
+
|
2805
|
+
# make values4.shape = (6, 6, 4)
|
2806
|
+
values = x[:, None]**3 * (y**3 + 2*y)[None, :]
|
2807
|
+
values4 = np.dstack((values, values, values, values))
|
2808
|
+
bspl = make_ndbspl((x, y), values4, k=3, solver=ssl.spsolve)
|
2809
|
+
|
2810
|
+
result = bspl(xi)
|
2811
|
+
target = np.dstack((values, values, values, values)).astype(float)
|
2812
|
+
assert result.shape == (36, 4)
|
2813
|
+
xp_assert_close(result.reshape(6, 6, 4),
|
2814
|
+
target, atol=1e-14)
|
2815
|
+
|
2816
|
+
# now two trailing dimensions
|
2817
|
+
values22 = values4.reshape((6, 6, 2, 2))
|
2818
|
+
bspl = make_ndbspl((x, y), values22, k=3, solver=ssl.spsolve)
|
2819
|
+
|
2820
|
+
result = bspl(xi)
|
2821
|
+
assert result.shape == (36, 2, 2)
|
2822
|
+
xp_assert_close(result.reshape(6, 6, 2, 2),
|
2823
|
+
target.reshape((6, 6, 2, 2)), atol=1e-14)
|
2824
|
+
|
2825
|
+
@pytest.mark.parametrize('k', [(3, 3), (1, 1), (3, 1), (1, 3), (3, 5)])
|
2826
|
+
def test_2D_mixed(self, k):
|
2827
|
+
# make a 2D separable spline w/ len(tx) != len(ty)
|
2828
|
+
x = np.arange(6)
|
2829
|
+
y = np.arange(7) + 1.5
|
2830
|
+
xi = [(a, b) for a, b in itertools.product(x, y)]
|
2831
|
+
|
2832
|
+
values = (x**3)[:, None] * (y**2 + 2*y)[None, :]
|
2833
|
+
bspl = make_ndbspl((x, y), values, k=k, solver=ssl.spsolve)
|
2834
|
+
xp_assert_close(bspl(xi), values.ravel(), atol=1e-15)
|
2835
|
+
|
2836
|
+
def test_2D_nans(self):
|
2837
|
+
x = np.arange(6)
|
2838
|
+
y = np.arange(6) + 0.5
|
2839
|
+
y[-1] = np.nan
|
2840
|
+
values = x[:, None]**3 * (y**3 + 2*y)[None, :]
|
2841
|
+
|
2842
|
+
with assert_raises(ValueError):
|
2843
|
+
make_ndbspl((x, y), values, k=1)
|
2844
|
+
|
2845
|
+
def _get_sample_2d_data(self):
|
2846
|
+
# from test_rgi.py::TestIntepN
|
2847
|
+
x = np.array([.5, 2., 3., 4., 5.5, 6.])
|
2848
|
+
y = np.array([.5, 2., 3., 4., 5.5, 6.])
|
2849
|
+
z = np.array(
|
2850
|
+
[
|
2851
|
+
[1, 2, 1, 2, 1, 1],
|
2852
|
+
[1, 2, 1, 2, 1, 1],
|
2853
|
+
[1, 2, 3, 2, 1, 1],
|
2854
|
+
[1, 2, 2, 2, 1, 1],
|
2855
|
+
[1, 2, 1, 2, 1, 1],
|
2856
|
+
[1, 2, 2, 2, 1, 1],
|
2857
|
+
]
|
2858
|
+
)
|
2859
|
+
return x, y, z
|
2860
|
+
|
2861
|
+
def test_2D_vs_RGI_linear(self):
|
2862
|
+
x, y, z = self._get_sample_2d_data()
|
2863
|
+
bspl = make_ndbspl((x, y), z, k=1)
|
2864
|
+
rgi = RegularGridInterpolator((x, y), z, method='linear')
|
2865
|
+
|
2866
|
+
xi = np.array([[1, 2.3, 5.3, 0.5, 3.3, 1.2, 3],
|
2867
|
+
[1, 3.3, 1.2, 4.0, 5.0, 1.0, 3]]).T
|
2868
|
+
|
2869
|
+
xp_assert_close(bspl(xi), rgi(xi), atol=1e-14)
|
2870
|
+
|
2871
|
+
def test_2D_vs_RGI_cubic(self):
|
2872
|
+
x, y, z = self._get_sample_2d_data()
|
2873
|
+
bspl = make_ndbspl((x, y), z, k=3, solver=ssl.spsolve)
|
2874
|
+
rgi = RegularGridInterpolator((x, y), z, method='cubic_legacy')
|
2875
|
+
|
2876
|
+
xi = np.array([[1, 2.3, 5.3, 0.5, 3.3, 1.2, 3],
|
2877
|
+
[1, 3.3, 1.2, 4.0, 5.0, 1.0, 3]]).T
|
2878
|
+
|
2879
|
+
xp_assert_close(bspl(xi), rgi(xi), atol=1e-14)
|
2880
|
+
|
2881
|
+
@pytest.mark.parametrize('solver', [ssl.gmres, ssl.gcrotmk])
|
2882
|
+
def test_2D_vs_RGI_cubic_iterative(self, solver):
|
2883
|
+
# same as `test_2D_vs_RGI_cubic`, only with an iterative solver.
|
2884
|
+
# Note the need to add an explicit `rtol` solver_arg to achieve the
|
2885
|
+
# target accuracy of 1e-14. (the relation between solver atol/rtol
|
2886
|
+
# and the accuracy of the final result is not direct and needs experimenting)
|
2887
|
+
x, y, z = self._get_sample_2d_data()
|
2888
|
+
bspl = make_ndbspl((x, y), z, k=3, solver=solver, rtol=1e-6)
|
2889
|
+
rgi = RegularGridInterpolator((x, y), z, method='cubic_legacy')
|
2890
|
+
|
2891
|
+
xi = np.array([[1, 2.3, 5.3, 0.5, 3.3, 1.2, 3],
|
2892
|
+
[1, 3.3, 1.2, 4.0, 5.0, 1.0, 3]]).T
|
2893
|
+
|
2894
|
+
xp_assert_close(bspl(xi), rgi(xi), atol=1e-14, rtol=1e-7)
|
2895
|
+
|
2896
|
+
def test_2D_vs_RGI_quintic(self):
|
2897
|
+
x, y, z = self._get_sample_2d_data()
|
2898
|
+
bspl = make_ndbspl((x, y), z, k=5, solver=ssl.spsolve)
|
2899
|
+
rgi = RegularGridInterpolator((x, y), z, method='quintic_legacy')
|
2900
|
+
|
2901
|
+
xi = np.array([[1, 2.3, 5.3, 0.5, 3.3, 1.2, 3],
|
2902
|
+
[1, 3.3, 1.2, 4.0, 5.0, 1.0, 3]]).T
|
2903
|
+
|
2904
|
+
xp_assert_close(bspl(xi), rgi(xi), atol=1e-14)
|
2905
|
+
|
2906
|
+
@pytest.mark.parametrize(
|
2907
|
+
'k, meth', [(1, 'linear'), (3, 'cubic_legacy'), (5, 'quintic_legacy')]
|
2908
|
+
)
|
2909
|
+
def test_3D_random_vs_RGI(self, k, meth):
|
2910
|
+
rndm = np.random.default_rng(123456)
|
2911
|
+
x = np.cumsum(rndm.uniform(size=6))
|
2912
|
+
y = np.cumsum(rndm.uniform(size=7))
|
2913
|
+
z = np.cumsum(rndm.uniform(size=8))
|
2914
|
+
values = rndm.uniform(size=(6, 7, 8))
|
2915
|
+
|
2916
|
+
bspl = make_ndbspl((x, y, z), values, k=k, solver=ssl.spsolve)
|
2917
|
+
rgi = RegularGridInterpolator((x, y, z), values, method=meth)
|
2918
|
+
|
2919
|
+
xi = np.random.uniform(low=0.7, high=2.1, size=(11, 3))
|
2920
|
+
xp_assert_close(bspl(xi), rgi(xi), atol=1e-14)
|
2921
|
+
|
2922
|
+
def test_solver_err_not_converged(self):
|
2923
|
+
x, y, z = self._get_sample_2d_data()
|
2924
|
+
solver_args = {'maxiter': 1}
|
2925
|
+
with assert_raises(ValueError, match='solver'):
|
2926
|
+
make_ndbspl((x, y), z, k=3, **solver_args)
|
2927
|
+
|
2928
|
+
with assert_raises(ValueError, match='solver'):
|
2929
|
+
make_ndbspl((x, y), np.dstack((z, z)), k=3, **solver_args)
|
2930
|
+
|
2931
|
+
|
2932
|
+
class TestFpchec:
|
2933
|
+
# https://github.com/scipy/scipy/blob/main/scipy/interpolate/fitpack/fpchec.f
|
2934
|
+
|
2935
|
+
def test_1D_x_t(self):
|
2936
|
+
k = 1
|
2937
|
+
t = np.arange(12).reshape(2, 6)
|
2938
|
+
x = np.arange(12)
|
2939
|
+
|
2940
|
+
with pytest.raises(ValueError, match="1D sequence"):
|
2941
|
+
_b.fpcheck(x, t, k)
|
2942
|
+
|
2943
|
+
with pytest.raises(ValueError, match="1D sequence"):
|
2944
|
+
_b.fpcheck(t, x, k)
|
2945
|
+
|
2946
|
+
def test_condition_1(self):
|
2947
|
+
# c 1) k+1 <= n-k-1 <= m
|
2948
|
+
k = 3
|
2949
|
+
n = 2*(k + 1) - 1 # not OK
|
2950
|
+
m = n + 11 # OK
|
2951
|
+
t = np.arange(n)
|
2952
|
+
x = np.arange(m)
|
2953
|
+
|
2954
|
+
assert dfitpack.fpchec(x, t, k) == 10
|
2955
|
+
with pytest.raises(ValueError, match="Need k+1*"):
|
2956
|
+
_b.fpcheck(x, t, k)
|
2957
|
+
|
2958
|
+
n = 2*(k+1) + 1 # OK
|
2959
|
+
m = n - k - 2 # not OK
|
2960
|
+
t = np.arange(n)
|
2961
|
+
x = np.arange(m)
|
2962
|
+
|
2963
|
+
assert dfitpack.fpchec(x, t, k) == 10
|
2964
|
+
with pytest.raises(ValueError, match="Need k+1*"):
|
2965
|
+
_b.fpcheck(x, t, k)
|
2966
|
+
|
2967
|
+
def test_condition_2(self):
|
2968
|
+
# c 2) t(1) <= t(2) <= ... <= t(k+1)
|
2969
|
+
# c t(n-k) <= t(n-k+1) <= ... <= t(n)
|
2970
|
+
k = 3
|
2971
|
+
t = [0]*(k+1) + [2] + [5]*(k+1) # this is OK
|
2972
|
+
x = [1, 2, 3, 4, 4.5]
|
2973
|
+
|
2974
|
+
assert dfitpack.fpchec(x, t, k) == 0
|
2975
|
+
assert _b.fpcheck(x, t, k) is None # does not raise
|
2976
|
+
|
2977
|
+
tt = t.copy()
|
2978
|
+
tt[-1] = tt[0] # not OK
|
2979
|
+
assert dfitpack.fpchec(x, tt, k) == 20
|
2980
|
+
with pytest.raises(ValueError, match="Last k knots*"):
|
2981
|
+
_b.fpcheck(x, tt, k)
|
2982
|
+
|
2983
|
+
tt = t.copy()
|
2984
|
+
tt[0] = tt[-1] # not OK
|
2985
|
+
assert dfitpack.fpchec(x, tt, k) == 20
|
2986
|
+
with pytest.raises(ValueError, match="First k knots*"):
|
2987
|
+
_b.fpcheck(x, tt, k)
|
2988
|
+
|
2989
|
+
def test_condition_3(self):
|
2990
|
+
# c 3) t(k+1) < t(k+2) < ... < t(n-k)
|
2991
|
+
k = 3
|
2992
|
+
t = [0]*(k+1) + [2, 3] + [5]*(k+1) # this is OK
|
2993
|
+
x = [1, 2, 3, 3.5, 4, 4.5]
|
2994
|
+
assert dfitpack.fpchec(x, t, k) == 0
|
2995
|
+
assert _b.fpcheck(x, t, k) is None
|
2996
|
+
|
2997
|
+
t = [0]*(k+1) + [2, 2] + [5]*(k+1) # this is not OK
|
2998
|
+
assert dfitpack.fpchec(x, t, k) == 30
|
2999
|
+
with pytest.raises(ValueError, match="Internal knots*"):
|
3000
|
+
_b.fpcheck(x, t, k)
|
3001
|
+
|
3002
|
+
def test_condition_4(self):
|
3003
|
+
# c 4) t(k+1) <= x(i) <= t(n-k)
|
3004
|
+
# NB: FITPACK's fpchec only checks x[0] & x[-1], so we follow.
|
3005
|
+
k = 3
|
3006
|
+
t = [0]*(k+1) + [5]*(k+1)
|
3007
|
+
x = [1, 2, 3, 3.5, 4, 4.5] # this is OK
|
3008
|
+
assert dfitpack.fpchec(x, t, k) == 0
|
3009
|
+
assert _b.fpcheck(x, t, k) is None
|
3010
|
+
|
3011
|
+
xx = x.copy()
|
3012
|
+
xx[0] = t[0] # still OK
|
3013
|
+
assert dfitpack.fpchec(xx, t, k) == 0
|
3014
|
+
assert _b.fpcheck(x, t, k) is None
|
3015
|
+
|
3016
|
+
xx = x.copy()
|
3017
|
+
xx[0] = t[0] - 1 # not OK
|
3018
|
+
assert dfitpack.fpchec(xx, t, k) == 40
|
3019
|
+
with pytest.raises(ValueError, match="Out of bounds*"):
|
3020
|
+
_b.fpcheck(xx, t, k)
|
3021
|
+
|
3022
|
+
xx = x.copy()
|
3023
|
+
xx[-1] = t[-1] + 1 # not OK
|
3024
|
+
assert dfitpack.fpchec(xx, t, k) == 40
|
3025
|
+
with pytest.raises(ValueError, match="Out of bounds*"):
|
3026
|
+
_b.fpcheck(xx, t, k)
|
3027
|
+
|
3028
|
+
# ### Test the S-W condition (no 5)
|
3029
|
+
# c 5) the conditions specified by schoenberg and whitney must hold
|
3030
|
+
# c for at least one subset of data points, i.e. there must be a
|
3031
|
+
# c subset of data points y(j) such that
|
3032
|
+
# c t(j) < y(j) < t(j+k+1), j=1,2,...,n-k-1
|
3033
|
+
def test_condition_5_x1xm(self):
|
3034
|
+
# x(1).ge.t(k2) .or. x(m).le.t(nk1)
|
3035
|
+
k = 1
|
3036
|
+
t = [0, 0, 1, 2, 2]
|
3037
|
+
x = [1.1, 1.1, 1.1]
|
3038
|
+
assert dfitpack.fpchec(x, t, k) == 50
|
3039
|
+
with pytest.raises(ValueError, match="Schoenberg-Whitney*"):
|
3040
|
+
_b.fpcheck(x, t, k)
|
3041
|
+
|
3042
|
+
x = [0.5, 0.5, 0.5]
|
3043
|
+
assert dfitpack.fpchec(x, t, k) == 50
|
3044
|
+
with pytest.raises(ValueError, match="Schoenberg-Whitney*"):
|
3045
|
+
_b.fpcheck(x, t, k)
|
3046
|
+
|
3047
|
+
def test_condition_5_k1(self):
|
3048
|
+
# special case nk3 (== n - k - 2) < 2
|
3049
|
+
k = 1
|
3050
|
+
t = [0, 0, 1, 1]
|
3051
|
+
x = [0.5, 0.6]
|
3052
|
+
assert dfitpack.fpchec(x, t, k) == 0
|
3053
|
+
assert _b.fpcheck(x, t, k) is None
|
3054
|
+
|
3055
|
+
def test_condition_5_1(self):
|
3056
|
+
# basically, there can't be an interval of t[j]..t[j+k+1] with no x
|
3057
|
+
k = 3
|
3058
|
+
t = [0]*(k+1) + [2] + [5]*(k+1)
|
3059
|
+
x = [3]*5
|
3060
|
+
assert dfitpack.fpchec(x, t, k) == 50
|
3061
|
+
with pytest.raises(ValueError, match="Schoenberg-Whitney*"):
|
3062
|
+
_b.fpcheck(x, t, k)
|
3063
|
+
|
3064
|
+
t = [0]*(k+1) + [2] + [5]*(k+1)
|
3065
|
+
x = [1]*5
|
3066
|
+
assert dfitpack.fpchec(x, t, k) == 50
|
3067
|
+
with pytest.raises(ValueError, match="Schoenberg-Whitney*"):
|
3068
|
+
_b.fpcheck(x, t, k)
|
3069
|
+
|
3070
|
+
def test_condition_5_2(self):
|
3071
|
+
# same as _5_1, only the empty interval is in the middle
|
3072
|
+
k = 3
|
3073
|
+
t = [0]*(k+1) + [2, 3] + [5]*(k+1)
|
3074
|
+
x = [1.1]*5 + [4]
|
3075
|
+
|
3076
|
+
assert dfitpack.fpchec(x, t, k) == 50
|
3077
|
+
with pytest.raises(ValueError, match="Schoenberg-Whitney*"):
|
3078
|
+
_b.fpcheck(x, t, k)
|
3079
|
+
|
3080
|
+
# and this one is OK
|
3081
|
+
x = [1.1]*4 + [4, 4]
|
3082
|
+
assert dfitpack.fpchec(x, t, k) == 0
|
3083
|
+
assert _b.fpcheck(x, t, k) is None
|
3084
|
+
|
3085
|
+
def test_condition_5_3(self):
|
3086
|
+
# similar to _5_2, covers a different failure branch
|
3087
|
+
k = 1
|
3088
|
+
t = [0, 0, 2, 3, 4, 5, 6, 7, 7]
|
3089
|
+
x = [1, 1, 1, 5.2, 5.2, 5.2, 6.5]
|
3090
|
+
|
3091
|
+
assert dfitpack.fpchec(x, t, k) == 50
|
3092
|
+
with pytest.raises(ValueError, match="Schoenberg-Whitney*"):
|
3093
|
+
_b.fpcheck(x, t, k)
|
3094
|
+
|
3095
|
+
|
3096
|
+
# ### python replicas of generate_knots(...) implementation details, for testing.
|
3097
|
+
# ### see TestGenerateKnots::test_split_and_add_knot
|
3098
|
+
def _split(x, t, k, residuals):
|
3099
|
+
"""Split the knot interval into "runs".
|
3100
|
+
"""
|
3101
|
+
ix = np.searchsorted(x, t[k:-k])
|
3102
|
+
# sum half-open intervals
|
3103
|
+
fparts = [residuals[ix[i]:ix[i+1]].sum() for i in range(len(ix)-1)]
|
3104
|
+
carries = residuals[ix[1:-1]]
|
3105
|
+
|
3106
|
+
for i in range(len(carries)): # split residuals at internal knots
|
3107
|
+
carry = carries[i] / 2
|
3108
|
+
fparts[i] += carry
|
3109
|
+
fparts[i+1] -= carry
|
3110
|
+
|
3111
|
+
fparts[-1] += residuals[-1] # add the contribution of the last knot
|
3112
|
+
|
3113
|
+
xp_assert_close(sum(fparts), sum(residuals), atol=1e-15)
|
3114
|
+
|
3115
|
+
return fparts, ix
|
3116
|
+
|
3117
|
+
|
3118
|
+
def _add_knot(x, t, k, residuals):
|
3119
|
+
"""Insert a new knot given reduals."""
|
3120
|
+
fparts, ix = _split(x, t, k, residuals)
|
3121
|
+
|
3122
|
+
# find the interval with max fparts and non-zero number of x values inside
|
3123
|
+
idx_max = -101
|
3124
|
+
fpart_max = -1e100
|
3125
|
+
for i in range(len(fparts)):
|
3126
|
+
if ix[i+1] - ix[i] > 1 and fparts[i] > fpart_max:
|
3127
|
+
idx_max = i
|
3128
|
+
fpart_max = fparts[i]
|
3129
|
+
|
3130
|
+
if idx_max == -101:
|
3131
|
+
raise ValueError("Internal error, please report it to SciPy developers.")
|
3132
|
+
|
3133
|
+
# round up, like Dierckx does? This is really arbitrary though.
|
3134
|
+
idx_newknot = (ix[idx_max] + ix[idx_max+1] + 1) // 2
|
3135
|
+
new_knot = x[idx_newknot]
|
3136
|
+
idx_t = np.searchsorted(t, new_knot)
|
3137
|
+
t_new = np.r_[t[:idx_t], new_knot, t[idx_t:]]
|
3138
|
+
return t_new
|
3139
|
+
|
3140
|
+
|
3141
|
+
class TestGenerateKnots:
|
3142
|
+
def test_split_add_knot(self):
|
3143
|
+
# smoke test implementation details: insert a new knot given residuals
|
3144
|
+
x = np.arange(8, dtype=float)
|
3145
|
+
y = x**3 + 1./(1 + x)
|
3146
|
+
k = 3
|
3147
|
+
t = np.array([0.]*(k+1) + [7.]*(k+1))
|
3148
|
+
spl = make_lsq_spline(x, y, k=k, t=t)
|
3149
|
+
residuals = (spl(x) - y)**2
|
3150
|
+
|
3151
|
+
from scipy.interpolate import _fitpack_repro as _fr
|
3152
|
+
new_t = _fr.add_knot(x, t, k, residuals)
|
3153
|
+
new_t_py = _add_knot(x, t, k, residuals)
|
3154
|
+
|
3155
|
+
xp_assert_close(new_t, new_t_py, atol=1e-15)
|
3156
|
+
|
3157
|
+
# redo with new knots
|
3158
|
+
spl2 = make_lsq_spline(x, y, k=k, t=new_t)
|
3159
|
+
residuals2 = (spl2(x) - y)**2
|
3160
|
+
|
3161
|
+
new_t2 = _fr.add_knot(x, new_t, k, residuals2)
|
3162
|
+
new_t2_py = _add_knot(x, new_t, k, residuals2)
|
3163
|
+
|
3164
|
+
xp_assert_close(new_t2, new_t2_py, atol=1e-15)
|
3165
|
+
|
3166
|
+
@pytest.mark.parametrize('k', [1, 2, 3, 4, 5])
|
3167
|
+
def test_s0(self, k):
|
3168
|
+
x = np.arange(8, dtype=np.float64)
|
3169
|
+
y = np.sin(x*np.pi/8)
|
3170
|
+
t = list(generate_knots(x, y, k=k, s=0))[-1]
|
3171
|
+
|
3172
|
+
tt = splrep(x, y, k=k, s=0)[0]
|
3173
|
+
xp_assert_close(t, tt, atol=1e-15)
|
3174
|
+
|
3175
|
+
def test_s0_1(self):
|
3176
|
+
# with these data, naive algorithm tries to insert >= nmax knots
|
3177
|
+
n = 10
|
3178
|
+
x = np.arange(n)
|
3179
|
+
y = x**3
|
3180
|
+
knots = list(generate_knots(x, y, k=3, s=0)) # does not error out
|
3181
|
+
xp_assert_close(knots[-1], _not_a_knot(x, 3), atol=1e-15)
|
3182
|
+
|
3183
|
+
def test_s0_n20(self):
|
3184
|
+
n = 20
|
3185
|
+
x = np.arange(n)
|
3186
|
+
y = x**3
|
3187
|
+
knots = list(generate_knots(x, y, k=3, s=0))
|
3188
|
+
xp_assert_close(knots[-1], _not_a_knot(x, 3), atol=1e-15)
|
3189
|
+
|
3190
|
+
def test_s0_nest(self):
|
3191
|
+
# s=0 and non-default nest: not implemented, errors out
|
3192
|
+
x = np.arange(10)
|
3193
|
+
y = x**3
|
3194
|
+
with assert_raises(ValueError):
|
3195
|
+
list(generate_knots(x, y, k=3, s=0, nest=10))
|
3196
|
+
|
3197
|
+
def test_s_switch(self):
|
3198
|
+
# test the process switching to interpolating knots when len(t) == m + k + 1
|
3199
|
+
"""
|
3200
|
+
To generate the `wanted` list below apply the following diff and rerun
|
3201
|
+
the test. The stdout will contain successive iterations of the `t`
|
3202
|
+
array.
|
3203
|
+
|
3204
|
+
$ git diff scipy/interpolate/fitpack/fpcurf.f
|
3205
|
+
diff --git a/scipy/interpolate/fitpack/fpcurf.f b/scipy/interpolate/fitpack/fpcurf.f
|
3206
|
+
index 1afb1900f1..d817e51ad8 100644
|
3207
|
+
--- a/scipy/interpolate/fitpack/fpcurf.f
|
3208
|
+
+++ b/scipy/interpolate/fitpack/fpcurf.f
|
3209
|
+
@@ -216,6 +216,9 @@ c t(j+k) <= x(i) <= t(j+k+1) and store it in fpint(j),j=1,2,...nrint.
|
3210
|
+
do 190 l=1,nplus
|
3211
|
+
c add a new knot.
|
3212
|
+
call fpknot(x,m,t,n,fpint,nrdata,nrint,nest,1)
|
3213
|
+
+ print*, l, nest, ': ', t
|
3214
|
+
+ print*, "n, nmax = ", n, nmax
|
3215
|
+
+
|
3216
|
+
c if n=nmax we locate the knots as for interpolation.
|
3217
|
+
if(n.eq.nmax) go to 10
|
3218
|
+
c test whether we cannot further increase the number of knots.
|
3219
|
+
""" # NOQA: E501
|
3220
|
+
x = np.arange(8)
|
3221
|
+
y = np.sin(x*np.pi/8)
|
3222
|
+
k = 3
|
3223
|
+
|
3224
|
+
knots = list(generate_knots(x, y, k=k, s=1e-7))
|
3225
|
+
wanted = [[0., 0., 0., 0., 7., 7., 7., 7.],
|
3226
|
+
[0., 0., 0., 0., 4., 7., 7., 7., 7.],
|
3227
|
+
[0., 0., 0., 0., 2., 4., 7., 7., 7., 7.],
|
3228
|
+
[0., 0., 0., 0., 2., 4., 6., 7., 7., 7., 7.],
|
3229
|
+
[0., 0., 0., 0., 2., 3., 4., 5., 7, 7., 7., 7.]
|
3230
|
+
]
|
3231
|
+
|
3232
|
+
assert len(knots) == len(wanted)
|
3233
|
+
for t, tt in zip(knots, wanted):
|
3234
|
+
xp_assert_close(t, tt, atol=1e-15)
|
3235
|
+
|
3236
|
+
# also check that the last knot vector matches FITPACK
|
3237
|
+
t, _, _ = splrep(x, y, k=k, s=1e-7)
|
3238
|
+
xp_assert_close(knots[-1], t, atol=1e-15)
|
3239
|
+
|
3240
|
+
def test_list_input(self):
|
3241
|
+
# test that list inputs are accepted
|
3242
|
+
x = list(range(8))
|
3243
|
+
gen = generate_knots(x, x, s=0.1, k=1)
|
3244
|
+
next(gen)
|
3245
|
+
|
3246
|
+
def test_nest(self):
|
3247
|
+
# test that nest < nmax stops the process early (and we get 10 knots not 12)
|
3248
|
+
x = np.arange(8)
|
3249
|
+
y = np.sin(x*np.pi/8)
|
3250
|
+
s = 1e-7
|
3251
|
+
|
3252
|
+
knots = list(generate_knots(x, y, k=3, s=s, nest=10))
|
3253
|
+
xp_assert_close(knots[-1],
|
3254
|
+
[0., 0., 0., 0., 2., 4., 7., 7., 7., 7.], atol=1e-15)
|
3255
|
+
|
3256
|
+
with assert_raises(ValueError):
|
3257
|
+
# nest < 2*(k+1)
|
3258
|
+
list(generate_knots(x, y, k=3, nest=4))
|
3259
|
+
|
3260
|
+
def test_weights(self):
|
3261
|
+
x = np.arange(8)
|
3262
|
+
y = np.sin(x*np.pi/8)
|
3263
|
+
|
3264
|
+
with assert_raises(ValueError):
|
3265
|
+
list(generate_knots(x, y, w=np.arange(11))) # len(w) != len(x)
|
3266
|
+
|
3267
|
+
with assert_raises(ValueError):
|
3268
|
+
list(generate_knots(x, y, w=-np.ones(8))) # w < 0
|
3269
|
+
|
3270
|
+
@pytest.mark.parametrize("npts", [30, 50, 100])
|
3271
|
+
@pytest.mark.parametrize("s", [0.1, 1e-2, 0])
|
3272
|
+
def test_vs_splrep(self, s, npts):
|
3273
|
+
# XXX this test is brittle: differences start apearing for k=3 and s=1e-6,
|
3274
|
+
# also for k != 3. Might be worth investigating at some point.
|
3275
|
+
# I think we do not really guarantee exact agreement with splrep. Instead,
|
3276
|
+
# we guarantee it is the same *in most cases*; otherwise slight differences
|
3277
|
+
# are allowed. There is no theorem, it is al heuristics by P. Dierckx.
|
3278
|
+
# The best we can do it to best-effort reproduce it.
|
3279
|
+
rndm = np.random.RandomState(12345)
|
3280
|
+
x = 10*np.sort(rndm.uniform(size=npts))
|
3281
|
+
y = np.sin(x*np.pi/10) + np.exp(-(x-6)**2)
|
3282
|
+
|
3283
|
+
k = 3
|
3284
|
+
t = splrep(x, y, k=k, s=s)[0]
|
3285
|
+
tt = list(generate_knots(x, y, k=k, s=s))[-1]
|
3286
|
+
|
3287
|
+
xp_assert_close(tt, t, atol=1e-15)
|
3288
|
+
|
3289
|
+
@pytest.mark.thread_unsafe
|
3290
|
+
def test_s_too_small(self):
|
3291
|
+
n = 14
|
3292
|
+
x = np.arange(n)
|
3293
|
+
y = x**3
|
3294
|
+
|
3295
|
+
# XXX splrep warns that "s too small": ier=2
|
3296
|
+
knots = list(generate_knots(x, y, k=3, s=1e-50))
|
3297
|
+
|
3298
|
+
with suppress_warnings() as sup:
|
3299
|
+
r = sup.record(RuntimeWarning)
|
3300
|
+
tck = splrep(x, y, k=3, s=1e-50)
|
3301
|
+
assert len(r) == 1
|
3302
|
+
xp_assert_equal(knots[-1], tck[0])
|
3303
|
+
|
3304
|
+
|
3305
|
+
def disc_naive(t, k):
|
3306
|
+
"""Straitforward way to compute the discontinuity matrix. For testing ONLY.
|
3307
|
+
|
3308
|
+
This routine returns a dense matrix, while `_fitpack_repro.disc` returns
|
3309
|
+
a packed one.
|
3310
|
+
"""
|
3311
|
+
n = t.shape[0]
|
3312
|
+
|
3313
|
+
delta = t[n - k - 1] - t[k]
|
3314
|
+
nrint = n - 2*k - 1
|
3315
|
+
|
3316
|
+
ti = t[k+1:n-k-1] # internal knots
|
3317
|
+
tii = np.repeat(ti, 2)
|
3318
|
+
tii[::2] += 1e-10
|
3319
|
+
tii[1::2] -= 1e-10
|
3320
|
+
m = BSpline(t, np.eye(n - k - 1), k)(tii, nu=k)
|
3321
|
+
|
3322
|
+
matr = np.empty((nrint-1, m.shape[1]), dtype=float)
|
3323
|
+
for i in range(0, m.shape[0], 2):
|
3324
|
+
matr[i//2, :] = m[i, :] - m[i+1, :]
|
3325
|
+
|
3326
|
+
matr *= (delta/nrint)**k / math.factorial(k)
|
3327
|
+
return matr
|
3328
|
+
|
3329
|
+
|
3330
|
+
class F_dense:
|
3331
|
+
""" The r.h.s. of ``f(p) = s``, an analog of _fitpack_repro.F
|
3332
|
+
Uses full matrices, so is for tests only.
|
3333
|
+
"""
|
3334
|
+
def __init__(self, x, y, t, k, s, w=None):
|
3335
|
+
self.x = x
|
3336
|
+
self.y = y
|
3337
|
+
self.t = t
|
3338
|
+
self.k = k
|
3339
|
+
self.w = np.ones_like(x, dtype=float) if w is None else w
|
3340
|
+
assert self.w.ndim == 1
|
3341
|
+
|
3342
|
+
# lhs
|
3343
|
+
a_dense = BSpline(t, np.eye(t.shape[0] - k - 1), k)(x)
|
3344
|
+
self.a_dense = a_dense * self.w[:, None]
|
3345
|
+
|
3346
|
+
from scipy.interpolate import _fitpack_repro as _fr
|
3347
|
+
self.b_dense = PackedMatrix(*_fr.disc(t, k)).todense()
|
3348
|
+
|
3349
|
+
# rhs
|
3350
|
+
assert y.ndim == 1
|
3351
|
+
yy = y * self.w
|
3352
|
+
self.yy = np.r_[yy, np.zeros(self.b_dense.shape[0])]
|
3353
|
+
|
3354
|
+
self.s = s
|
3355
|
+
|
3356
|
+
def __call__(self, p):
|
3357
|
+
ab = np.vstack((self.a_dense, self.b_dense / p))
|
3358
|
+
|
3359
|
+
# LSQ solution of ab @ c = yy
|
3360
|
+
from scipy.linalg import qr, solve
|
3361
|
+
q, r = qr(ab, mode='economic')
|
3362
|
+
|
3363
|
+
qy = q.T @ self.yy
|
3364
|
+
|
3365
|
+
nc = r.shape[1]
|
3366
|
+
c = solve(r[:nc, :nc], qy[:nc])
|
3367
|
+
|
3368
|
+
spl = BSpline(self.t, c, self.k)
|
3369
|
+
fp = np.sum(self.w**2 * (spl(self.x) - self.y)**2)
|
3370
|
+
|
3371
|
+
self.spl = spl # store it
|
3372
|
+
|
3373
|
+
return fp - self.s
|
3374
|
+
|
3375
|
+
|
3376
|
+
class TestMakeSplrep:
|
3377
|
+
def test_input_errors(self):
|
3378
|
+
x = np.linspace(0, 10, 11)
|
3379
|
+
y = np.linspace(0, 10, 12)
|
3380
|
+
with assert_raises(ValueError):
|
3381
|
+
# len(x) != len(y)
|
3382
|
+
make_splrep(x, y)
|
3383
|
+
|
3384
|
+
with assert_raises(ValueError):
|
3385
|
+
# 0D inputs
|
3386
|
+
make_splrep(1, 2, s=0.1)
|
3387
|
+
|
3388
|
+
with assert_raises(ValueError):
|
3389
|
+
# y.ndim > 2
|
3390
|
+
y = np.ones((x.size, 2, 2, 2))
|
3391
|
+
make_splrep(x, y, s=0.1)
|
3392
|
+
|
3393
|
+
w = np.ones(12)
|
3394
|
+
with assert_raises(ValueError):
|
3395
|
+
# len(weights) != len(x)
|
3396
|
+
make_splrep(x, x**3, w=w, s=0.1)
|
3397
|
+
|
3398
|
+
w = -np.ones(12)
|
3399
|
+
with assert_raises(ValueError):
|
3400
|
+
# w < 0
|
3401
|
+
make_splrep(x, x**3, w=w, s=0.1)
|
3402
|
+
|
3403
|
+
w = np.ones((x.shape[0], 2))
|
3404
|
+
with assert_raises(ValueError):
|
3405
|
+
# w.ndim != 1
|
3406
|
+
make_splrep(x, x**3, w=w, s=0.1)
|
3407
|
+
|
3408
|
+
with assert_raises(ValueError):
|
3409
|
+
# x not ordered
|
3410
|
+
make_splrep(x[::-1], x**3, s=0.1)
|
3411
|
+
|
3412
|
+
with assert_raises(TypeError):
|
3413
|
+
# k != int(k)
|
3414
|
+
make_splrep(x, x**3, k=2.5, s=0.1)
|
3415
|
+
|
3416
|
+
with assert_raises(ValueError):
|
3417
|
+
# s < 0
|
3418
|
+
make_splrep(x, x**3, s=-1)
|
3419
|
+
|
3420
|
+
with assert_raises(ValueError):
|
3421
|
+
# nest < 2*k + 2
|
3422
|
+
make_splrep(x, x**3, k=3, nest=2, s=0.1)
|
3423
|
+
|
3424
|
+
with assert_raises(ValueError):
|
3425
|
+
# nest not None and s==0
|
3426
|
+
make_splrep(x, x**3, s=0, nest=11)
|
3427
|
+
|
3428
|
+
with assert_raises(ValueError):
|
3429
|
+
# len(x) != len(y)
|
3430
|
+
make_splrep(np.arange(8), np.arange(9), s=0.1)
|
3431
|
+
|
3432
|
+
def _get_xykt(self):
|
3433
|
+
x = np.linspace(0, 5, 11)
|
3434
|
+
y = np.sin(x*3.14 / 5)**2
|
3435
|
+
k = 3
|
3436
|
+
s = 1.7e-4
|
3437
|
+
tt = np.array([0]*(k+1) + [2.5, 4.0] + [5]*(k+1))
|
3438
|
+
|
3439
|
+
return x, y, k, s, tt
|
3440
|
+
|
3441
|
+
def test_fitpack_F(self):
|
3442
|
+
# test an implementation detail: banded/packed linalg vs full matrices
|
3443
|
+
from scipy.interpolate._fitpack_repro import F
|
3444
|
+
|
3445
|
+
x, y, k, s, t = self._get_xykt()
|
3446
|
+
f = F(x, y[:, None], t, k, s) # F expects y to be 2D
|
3447
|
+
f_d = F_dense(x, y, t, k, s)
|
3448
|
+
for p in [1, 10, 100]:
|
3449
|
+
xp_assert_close(f(p), f_d(p), atol=1e-15)
|
3450
|
+
|
3451
|
+
def test_fitpack_F_with_weights(self):
|
3452
|
+
# repeat test_fitpack_F, with weights
|
3453
|
+
from scipy.interpolate._fitpack_repro import F
|
3454
|
+
|
3455
|
+
x, y, k, s, t = self._get_xykt()
|
3456
|
+
w = np.arange(x.shape[0], dtype=float)
|
3457
|
+
fw = F(x, y[:, None], t, k, s, w=w) # F expects y to be 2D
|
3458
|
+
fw_d = F_dense(x, y, t, k, s, w=w)
|
3459
|
+
|
3460
|
+
f_d = F_dense(x, y, t, k, s) # no weights
|
3461
|
+
|
3462
|
+
for p in [1, 10, 100]:
|
3463
|
+
xp_assert_close(fw(p), fw_d(p), atol=1e-15)
|
3464
|
+
assert not np.allclose(f_d(p), fw_d(p), atol=1e-15)
|
3465
|
+
|
3466
|
+
def test_disc_matrix(self):
|
3467
|
+
# test an implementation detail: discontinuity matrix
|
3468
|
+
# (jumps of k-th derivative at knots)
|
3469
|
+
import scipy.interpolate._fitpack_repro as _fr
|
3470
|
+
|
3471
|
+
rng = np.random.default_rng(12345)
|
3472
|
+
t = np.r_[0, 0, 0, 0, np.sort(rng.uniform(size=7))*5, 5, 5, 5, 5]
|
3473
|
+
|
3474
|
+
n, k = len(t), 3
|
3475
|
+
D = PackedMatrix(*_fr.disc(t, k)).todense()
|
3476
|
+
D_dense = disc_naive(t, k)
|
3477
|
+
assert D.shape[0] == n - 2*k - 2 # number of internal knots
|
3478
|
+
xp_assert_close(D, D_dense, atol=1e-15)
|
3479
|
+
|
3480
|
+
def test_simple_vs_splrep(self):
|
3481
|
+
x, y, k, s, tt = self._get_xykt()
|
3482
|
+
tt = np.array([0]*(k+1) + [2.5, 4.0] + [5]*(k+1))
|
3483
|
+
|
3484
|
+
t,c,k = splrep(x, y, k=k, s=s)
|
3485
|
+
assert all(t == tt)
|
3486
|
+
|
3487
|
+
spl = make_splrep(x, y, k=k, s=s)
|
3488
|
+
xp_assert_close(c[:spl.c.size], spl.c, atol=1e-15)
|
3489
|
+
|
3490
|
+
def test_with_knots(self):
|
3491
|
+
x, y, k, s, _ = self._get_xykt()
|
3492
|
+
|
3493
|
+
t = list(generate_knots(x, y, k=k, s=s))[-1]
|
3494
|
+
|
3495
|
+
spl_auto = make_splrep(x, y, k=k, s=s)
|
3496
|
+
spl_t = make_splrep(x, y, t=t, k=k, s=s)
|
3497
|
+
|
3498
|
+
xp_assert_close(spl_auto.t, spl_t.t, atol=1e-15)
|
3499
|
+
xp_assert_close(spl_auto.c, spl_t.c, atol=1e-15)
|
3500
|
+
assert spl_auto.k == spl_t.k
|
3501
|
+
|
3502
|
+
def test_no_internal_knots(self):
|
3503
|
+
# should not fail if there are no internal knots
|
3504
|
+
n = 10
|
3505
|
+
x = np.arange(n)
|
3506
|
+
y = x**3
|
3507
|
+
k = 3
|
3508
|
+
spl = make_splrep(x, y, k=k, s=1)
|
3509
|
+
assert spl.t.shape[0] == 2*(k+1)
|
3510
|
+
|
3511
|
+
def test_default_s(self):
|
3512
|
+
n = 10
|
3513
|
+
x = np.arange(n)
|
3514
|
+
y = x**3
|
3515
|
+
spl = make_splrep(x, y, k=3)
|
3516
|
+
spl_i = make_interp_spline(x, y, k=3)
|
3517
|
+
|
3518
|
+
xp_assert_close(spl.c, spl_i.c, atol=1e-15)
|
3519
|
+
|
3520
|
+
@pytest.mark.thread_unsafe
|
3521
|
+
def test_s_too_small(self):
|
3522
|
+
# both splrep and make_splrep warn that "s too small": ier=2
|
3523
|
+
n = 14
|
3524
|
+
x = np.arange(n)
|
3525
|
+
y = x**3
|
3526
|
+
|
3527
|
+
with suppress_warnings() as sup:
|
3528
|
+
r = sup.record(RuntimeWarning)
|
3529
|
+
tck = splrep(x, y, k=3, s=1e-50)
|
3530
|
+
spl = make_splrep(x, y, k=3, s=1e-50)
|
3531
|
+
assert len(r) == 2
|
3532
|
+
xp_assert_equal(spl.t, tck[0])
|
3533
|
+
xp_assert_close(np.r_[spl.c, [0]*(spl.k+1)],
|
3534
|
+
tck[1], atol=5e-13)
|
3535
|
+
|
3536
|
+
def test_issue_22704(self):
|
3537
|
+
# Reference - https://github.com/scipy/scipy/issues/22704
|
3538
|
+
x = np.asarray([20.00, 153.81, 175.57, 202.47, 237.11,
|
3539
|
+
253.61, 258.56, 273.40, 284.54, 293.61,
|
3540
|
+
298.56, 301.86, 305.57, 307.22, 308.45,
|
3541
|
+
310.10, 310.10, 310.50], dtype=np.float64)
|
3542
|
+
y = np.asarray([53.00, 49.50, 48.60, 46.80, 43.20,
|
3543
|
+
40.32, 39.60, 36.00, 32.40, 28.80,
|
3544
|
+
25.20, 21.60, 18.00, 14.40, 10.80,
|
3545
|
+
7.20, 3.60, 0.0], dtype=np.float64)
|
3546
|
+
w = np.asarray([1.38723] * y.shape[0], dtype=np.float64)
|
3547
|
+
with assert_raises(ValueError):
|
3548
|
+
make_splrep(x, y, w=w, k=2, s=12)
|
3549
|
+
|
3550
|
+
def test_shape(self):
|
3551
|
+
# make sure coefficients have the right shape (not extra dims)
|
3552
|
+
n, k = 10, 3
|
3553
|
+
x = np.arange(n)
|
3554
|
+
y = x**3
|
3555
|
+
|
3556
|
+
spl = make_splrep(x, y, k=k)
|
3557
|
+
spl_1 = make_splrep(x, y, k=k, s=1e-5)
|
3558
|
+
|
3559
|
+
assert spl.c.ndim == 1
|
3560
|
+
assert spl_1.c.ndim == 1
|
3561
|
+
|
3562
|
+
# force the general code path, not shortcuts
|
3563
|
+
spl_2 = make_splrep(x, y + 1/(1+y), k=k, s=1e-5)
|
3564
|
+
assert spl_2.c.ndim == 1
|
3565
|
+
|
3566
|
+
def test_s0_vs_not(self):
|
3567
|
+
# check that the shapes are consistent
|
3568
|
+
n, k = 10, 3
|
3569
|
+
x = np.arange(n)
|
3570
|
+
y = x**3
|
3571
|
+
|
3572
|
+
spl_0 = make_splrep(x, y, k=3, s=0)
|
3573
|
+
spl_1 = make_splrep(x, y, k=3, s=1)
|
3574
|
+
|
3575
|
+
assert spl_0.c.ndim == 1
|
3576
|
+
assert spl_1.c.ndim == 1
|
3577
|
+
|
3578
|
+
assert spl_0.t.shape[0] == n + k + 1
|
3579
|
+
assert spl_1.t.shape[0] == 2 * (k + 1)
|
3580
|
+
|
3581
|
+
|
3582
|
+
class TestMakeSplprep:
|
3583
|
+
def _get_xyk(self, m=10, k=3):
|
3584
|
+
x = np.arange(m) * np.pi / m
|
3585
|
+
y = [np.sin(x), np.cos(x)]
|
3586
|
+
return x, y, k
|
3587
|
+
|
3588
|
+
@pytest.mark.parametrize('s', [0, 0.1, 1e-3, 1e-5])
|
3589
|
+
def test_simple_vs_splprep(self, s):
|
3590
|
+
# Check/document the interface vs splPrep
|
3591
|
+
# The four values of `s` are to probe all code paths and shortcuts
|
3592
|
+
m, k = 10, 3
|
3593
|
+
x = np.arange(m) * np.pi / m
|
3594
|
+
y = [np.sin(x), np.cos(x)]
|
3595
|
+
|
3596
|
+
# the number of knots depends on `s` (this is by construction)
|
3597
|
+
num_knots = {0: 14, 0.1: 8, 1e-3: 8 + 1, 1e-5: 8 + 2}
|
3598
|
+
|
3599
|
+
# construct the splines
|
3600
|
+
(t, c, k), u_ = splprep(y, s=s)
|
3601
|
+
spl, u = make_splprep(y, s=s)
|
3602
|
+
|
3603
|
+
# parameters
|
3604
|
+
xp_assert_close(u, u_, atol=1e-15)
|
3605
|
+
|
3606
|
+
# knots
|
3607
|
+
xp_assert_close(spl.t, t, atol=1e-15)
|
3608
|
+
assert len(t) == num_knots[s]
|
3609
|
+
|
3610
|
+
# coefficients: note the transpose
|
3611
|
+
cc = np.asarray(c).T
|
3612
|
+
xp_assert_close(spl.c, cc, atol=1e-15)
|
3613
|
+
|
3614
|
+
# values: note axis=1
|
3615
|
+
xp_assert_close(spl(u),
|
3616
|
+
BSpline(t, c, k, axis=1)(u), atol=1e-15)
|
3617
|
+
|
3618
|
+
@pytest.mark.parametrize('s', [0, 0.1, 1e-3, 1e-5])
|
3619
|
+
def test_array_not_list(self, s):
|
3620
|
+
# the argument of splPrep is either a list of arrays or a 2D array (sigh)
|
3621
|
+
_, y, _ = self._get_xyk()
|
3622
|
+
assert isinstance(y, list)
|
3623
|
+
assert np.shape(y)[0] == 2
|
3624
|
+
|
3625
|
+
# assert the behavior of FITPACK's splrep
|
3626
|
+
tck, u = splprep(y, s=s)
|
3627
|
+
tck_a, u_a = splprep(np.asarray(y), s=s)
|
3628
|
+
xp_assert_close(u, u_a, atol=s)
|
3629
|
+
xp_assert_close(tck[0], tck_a[0], atol=1e-15)
|
3630
|
+
assert len(tck[1]) == len(tck_a[1])
|
3631
|
+
for c1, c2 in zip(tck[1], tck_a[1]):
|
3632
|
+
xp_assert_close(c1, c2, atol=1e-15)
|
3633
|
+
assert tck[2] == tck_a[2]
|
3634
|
+
assert np.shape(splev(u, tck)) == np.shape(y)
|
3635
|
+
|
3636
|
+
spl, u = make_splprep(y, s=s)
|
3637
|
+
xp_assert_close(u, u_a, atol=1e-15)
|
3638
|
+
xp_assert_close(spl.t, tck_a[0], atol=1e-15)
|
3639
|
+
xp_assert_close(spl.c.T, tck_a[1], atol=1e-15)
|
3640
|
+
assert spl.k == tck_a[2]
|
3641
|
+
assert spl(u).shape == np.shape(y)
|
3642
|
+
|
3643
|
+
spl, u = make_splprep(np.asarray(y), s=s)
|
3644
|
+
xp_assert_close(u, u_a, atol=1e-15)
|
3645
|
+
xp_assert_close(spl.t, tck_a[0], atol=1e-15)
|
3646
|
+
xp_assert_close(spl.c.T, tck_a[1], atol=1e-15)
|
3647
|
+
assert spl.k == tck_a[2]
|
3648
|
+
assert spl(u).shape == np.shape(y)
|
3649
|
+
|
3650
|
+
with assert_raises(ValueError):
|
3651
|
+
make_splprep(np.asarray(y).T, s=s)
|
3652
|
+
|
3653
|
+
def test_default_s_is_zero(self):
|
3654
|
+
x, y, k = self._get_xyk(m=10)
|
3655
|
+
|
3656
|
+
spl, u = make_splprep(y)
|
3657
|
+
xp_assert_close(spl(u), y, atol=1e-15)
|
3658
|
+
|
3659
|
+
def test_s_zero_vs_near_zero(self):
|
3660
|
+
# s=0 and s \approx 0 are consistent
|
3661
|
+
x, y, k = self._get_xyk(m=10)
|
3662
|
+
|
3663
|
+
spl_i, u_i = make_splprep(y, s=0)
|
3664
|
+
spl_n, u_n = make_splprep(y, s=1e-15)
|
3665
|
+
|
3666
|
+
xp_assert_close(u_i, u_n, atol=1e-15)
|
3667
|
+
xp_assert_close(spl_i(u_i), y, atol=1e-15)
|
3668
|
+
xp_assert_close(spl_n(u_n), y, atol=1e-7)
|
3669
|
+
assert spl_i.axis == spl_n.axis
|
3670
|
+
assert spl_i.c.shape == spl_n.c.shape
|
3671
|
+
|
3672
|
+
def test_1D(self):
|
3673
|
+
x = np.arange(8, dtype=float)
|
3674
|
+
with assert_raises(ValueError):
|
3675
|
+
splprep(x)
|
3676
|
+
|
3677
|
+
with assert_raises(ValueError):
|
3678
|
+
make_splprep(x, s=0)
|
3679
|
+
|
3680
|
+
with assert_raises(ValueError):
|
3681
|
+
make_splprep(x, s=0.1)
|
3682
|
+
|
3683
|
+
tck, u_ = splprep([x], s=1e-5)
|
3684
|
+
spl, u = make_splprep([x], s=1e-5)
|
3685
|
+
|
3686
|
+
assert spl(u).shape == (1, 8)
|
3687
|
+
xp_assert_close(spl(u), [x], atol=1e-15)
|
3688
|
+
|
3689
|
+
|
3690
|
+
class BatchSpline:
|
3691
|
+
# BSpline-line class with reference batch behavior
|
3692
|
+
def __init__(self, x, y, axis, *, spline, **kwargs):
|
3693
|
+
y = np.moveaxis(y, axis, -1)
|
3694
|
+
self._batch_shape = y.shape[:-1]
|
3695
|
+
self._splines = [spline(x, yi, **kwargs) for yi in y.reshape(-1, y.shape[-1])]
|
3696
|
+
self._axis = axis
|
3697
|
+
|
3698
|
+
def __call__(self, x):
|
3699
|
+
y = [spline(x) for spline in self._splines]
|
3700
|
+
y = np.reshape(y, self._batch_shape + x.shape)
|
3701
|
+
return np.moveaxis(y, -1, self._axis) if x.shape else y
|
3702
|
+
|
3703
|
+
def integrate(self, a, b, extrapolate=None):
|
3704
|
+
y = [spline.integrate(a, b, extrapolate) for spline in self._splines]
|
3705
|
+
return np.reshape(y, self._batch_shape)
|
3706
|
+
|
3707
|
+
def derivative(self, nu):
|
3708
|
+
res = copy.deepcopy(self)
|
3709
|
+
res._splines = [spline.derivative(nu) for spline in res._splines]
|
3710
|
+
return res
|
3711
|
+
|
3712
|
+
def antiderivative(self, nu):
|
3713
|
+
res = copy.deepcopy(self)
|
3714
|
+
res._splines = [spline.antiderivative(nu) for spline in res._splines]
|
3715
|
+
return res
|
3716
|
+
|
3717
|
+
|
3718
|
+
class TestBatch:
|
3719
|
+
@pytest.mark.parametrize('make_spline, kwargs',
|
3720
|
+
[(make_interp_spline, {}),
|
3721
|
+
(make_smoothing_spline, {}),
|
3722
|
+
(make_smoothing_spline, {'lam': 1.0}),
|
3723
|
+
(make_lsq_spline, {'method': "norm-eq"}),
|
3724
|
+
(make_lsq_spline, {'method': "qr"}),
|
3725
|
+
])
|
3726
|
+
@pytest.mark.parametrize('eval_shape', [(), (1,), (3,)])
|
3727
|
+
@pytest.mark.parametrize('axis', [-1, 0, 1])
|
3728
|
+
def test_batch(self, make_spline, kwargs, axis, eval_shape):
|
3729
|
+
rng = np.random.default_rng(4329872134985134)
|
3730
|
+
n = 10
|
3731
|
+
shape = (2, 3, 4, n)
|
3732
|
+
domain = (0, 10)
|
3733
|
+
|
3734
|
+
x = np.linspace(*domain, n)
|
3735
|
+
y = np.moveaxis(rng.random(shape), -1, axis)
|
3736
|
+
|
3737
|
+
if make_spline == make_lsq_spline:
|
3738
|
+
k = 3 # spline degree, if needed
|
3739
|
+
t = (x[0],) * (k + 1) + (x[-1],) * (k + 1) # valid knots, if needed
|
3740
|
+
kwargs = kwargs | dict(t=t, k=k)
|
3741
|
+
|
3742
|
+
res = make_spline(x, y, axis=axis, **kwargs)
|
3743
|
+
ref = BatchSpline(x, y, axis=axis, spline=make_spline, **kwargs)
|
3744
|
+
|
3745
|
+
x = rng.uniform(*domain, size=eval_shape)
|
3746
|
+
np.testing.assert_allclose(res(x), ref(x))
|
3747
|
+
|
3748
|
+
res, ref = res.antiderivative(1), ref.antiderivative(1)
|
3749
|
+
np.testing.assert_allclose(res(x), ref(x))
|
3750
|
+
|
3751
|
+
res, ref = res.derivative(2), ref.derivative(2)
|
3752
|
+
np.testing.assert_allclose(res(x), ref(x))
|
3753
|
+
|
3754
|
+
np.testing.assert_allclose(res.integrate(*domain), ref.integrate(*domain))
|