Commit ·
716eb6e
1
Parent(s): a455555
Upload folder using huggingface_hub (part 14)
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .gitattributes +67 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/cython_blas.pxd +169 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/cython_blas.pyx +1432 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/cython_lapack.cpython-311-x86_64-linux-gnu.so +3 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/cython_lapack.pxd +0 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/cython_lapack.pyx +0 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/decomp.py +23 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/decomp_cholesky.py +21 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/decomp_lu.py +21 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/decomp_qr.py +20 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/decomp_schur.py +21 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/decomp_svd.py +21 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/interpolative.py +989 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/lapack.py +1061 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/matfuncs.py +23 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/misc.py +21 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/special_matrices.py +22 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/__init__.py +0 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/_cython_examples/extending.pyx +23 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/_cython_examples/meson.build +27 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/data/carex_15_data.npz +3 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/data/carex_18_data.npz +3 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/data/carex_19_data.npz +3 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/data/carex_20_data.npz +3 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/data/carex_6_data.npz +3 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/data/gendare_20170120_data.npz +3 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_basic.py +2059 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_blas.py +1127 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_cython_blas.py +118 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_cython_lapack.py +22 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_cythonized_array_utils.py +132 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_decomp.py +0 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_decomp_cholesky.py +268 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_decomp_cossin.py +300 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_decomp_ldl.py +137 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_decomp_lu.py +308 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_decomp_polar.py +110 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_decomp_update.py +1701 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_extending.py +46 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_fblas.py +607 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_interpolative.py +232 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_lapack.py +0 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_matfuncs.py +1063 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_matmul_toeplitz.py +136 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_procrustes.py +221 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_sketches.py +118 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_solve_toeplitz.py +150 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_solvers.py +844 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_special_matrices.py +640 -0
- pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/misc/__init__.py +6 -0
.gitattributes
CHANGED
|
@@ -751,3 +751,70 @@ pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/_matfuncs_e
|
|
| 751 |
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/_matfuncs_sqrtm_triu.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 752 |
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/_solve_toeplitz.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 753 |
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/cython_blas.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 751 |
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/_matfuncs_sqrtm_triu.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 752 |
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/_solve_toeplitz.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 753 |
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/cython_blas.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 754 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/cython_lapack.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 755 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/ndimage/__pycache__/_morphology.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 756 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/ndimage/_nd_image.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 757 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/ndimage/_ni_label.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 758 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/odr/__odrpack.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 759 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/optimize/__pycache__/_optimize.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 760 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/optimize/_bglu_dense.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 761 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/optimize/_cobyla.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 762 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/optimize/_cython_nnls.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 763 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/optimize/_highspy/_core.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 764 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/optimize/_highspy/_highs_options.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 765 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/optimize/_lbfgsb.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 766 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/optimize/_lsq/givens_elimination.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 767 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/optimize/_moduleTNC.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 768 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/optimize/_pava_pybind.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 769 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/optimize/_trlib/_trlib.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 770 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/optimize/cython_optimize/_zeros.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 771 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/signal/__pycache__/_filter_design.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 772 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/signal/__pycache__/_ltisys.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 773 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/signal/__pycache__/_signaltools.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 774 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/signal/_peak_finding_utils.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 775 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/signal/_sosfilt.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 776 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/signal/_upfirdn_apply.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 777 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/sparse/_csparsetools.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 778 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/sparse/_sparsetools.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 779 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/sparse/csgraph/_flow.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 780 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/sparse/csgraph/_matching.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 781 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/sparse/csgraph/_min_spanning_tree.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 782 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/sparse/csgraph/_reordering.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 783 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/sparse/csgraph/_shortest_path.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 784 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/sparse/csgraph/_tools.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 785 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/sparse/csgraph/_traversal.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 786 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/sparse/linalg/_dsolve/_superlu.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 787 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/sparse/linalg/_eigen/arpack/_arpack.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 788 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/sparse/linalg/_propack/_cpropack.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 789 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/sparse/linalg/_propack/_dpropack.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 790 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/sparse/linalg/_propack/_spropack.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 791 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/sparse/linalg/_propack/_zpropack.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 792 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/spatial/__pycache__/distance.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 793 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/spatial/_ckdtree.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 794 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/spatial/_distance_pybind.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 795 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/spatial/_distance_wrap.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 796 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/spatial/_hausdorff.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 797 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/spatial/_qhull.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 798 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/spatial/_voronoi.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 799 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/spatial/transform/_rotation.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 800 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/special/__pycache__/_basic.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 801 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/special/_ellip_harm_2.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 802 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/special/_gufuncs.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 803 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/special/_specfun.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 804 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/special/_special_ufuncs.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 805 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/special/_test_internal.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 806 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/special/_ufuncs.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 807 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/special/_ufuncs_cxx.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 808 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/special/cython_special.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 809 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/stats/__pycache__/_continuous_distns.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 810 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/stats/__pycache__/_discrete_distns.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 811 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/stats/__pycache__/_distn_infrastructure.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 812 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/stats/__pycache__/_distribution_infrastructure.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 813 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/stats/__pycache__/_morestats.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 814 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/stats/__pycache__/_mstats_basic.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 815 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/stats/__pycache__/_multivariate.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 816 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/stats/__pycache__/_qmc.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 817 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/stats/__pycache__/_resampling.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 818 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/stats/__pycache__/_stats_py.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 819 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/stats/_ansari_swilk_statistics.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 820 |
+
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/stats/_biasedurn.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/cython_blas.pxd
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
This file was generated by _generate_pyx.py.
|
| 3 |
+
Do not edit this file directly.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
# Within scipy, these wrappers can be used via relative or absolute cimport.
|
| 7 |
+
# Examples:
|
| 8 |
+
# from ..linalg cimport cython_blas
|
| 9 |
+
# from scipy.linalg cimport cython_blas
|
| 10 |
+
# cimport scipy.linalg.cython_blas as cython_blas
|
| 11 |
+
# cimport ..linalg.cython_blas as cython_blas
|
| 12 |
+
|
| 13 |
+
# Within SciPy, if BLAS functions are needed in C/C++/Fortran,
|
| 14 |
+
# these wrappers should not be used.
|
| 15 |
+
# The original libraries should be linked directly.
|
| 16 |
+
|
| 17 |
+
ctypedef float s
|
| 18 |
+
ctypedef double d
|
| 19 |
+
ctypedef float complex c
|
| 20 |
+
ctypedef double complex z
|
| 21 |
+
|
| 22 |
+
cdef void caxpy(int *n, c *ca, c *cx, int *incx, c *cy, int *incy) noexcept nogil
|
| 23 |
+
cdef void ccopy(int *n, c *cx, int *incx, c *cy, int *incy) noexcept nogil
|
| 24 |
+
cdef c cdotc(int *n, c *cx, int *incx, c *cy, int *incy) noexcept nogil
|
| 25 |
+
cdef c cdotu(int *n, c *cx, int *incx, c *cy, int *incy) noexcept nogil
|
| 26 |
+
cdef void cgbmv(char *trans, int *m, int *n, int *kl, int *ku, c *alpha, c *a, int *lda, c *x, int *incx, c *beta, c *y, int *incy) noexcept nogil
|
| 27 |
+
cdef void cgemm(char *transa, char *transb, int *m, int *n, int *k, c *alpha, c *a, int *lda, c *b, int *ldb, c *beta, c *c, int *ldc) noexcept nogil
|
| 28 |
+
cdef void cgemv(char *trans, int *m, int *n, c *alpha, c *a, int *lda, c *x, int *incx, c *beta, c *y, int *incy) noexcept nogil
|
| 29 |
+
cdef void cgerc(int *m, int *n, c *alpha, c *x, int *incx, c *y, int *incy, c *a, int *lda) noexcept nogil
|
| 30 |
+
cdef void cgeru(int *m, int *n, c *alpha, c *x, int *incx, c *y, int *incy, c *a, int *lda) noexcept nogil
|
| 31 |
+
cdef void chbmv(char *uplo, int *n, int *k, c *alpha, c *a, int *lda, c *x, int *incx, c *beta, c *y, int *incy) noexcept nogil
|
| 32 |
+
cdef void chemm(char *side, char *uplo, int *m, int *n, c *alpha, c *a, int *lda, c *b, int *ldb, c *beta, c *c, int *ldc) noexcept nogil
|
| 33 |
+
cdef void chemv(char *uplo, int *n, c *alpha, c *a, int *lda, c *x, int *incx, c *beta, c *y, int *incy) noexcept nogil
|
| 34 |
+
cdef void cher(char *uplo, int *n, s *alpha, c *x, int *incx, c *a, int *lda) noexcept nogil
|
| 35 |
+
cdef void cher2(char *uplo, int *n, c *alpha, c *x, int *incx, c *y, int *incy, c *a, int *lda) noexcept nogil
|
| 36 |
+
cdef void cher2k(char *uplo, char *trans, int *n, int *k, c *alpha, c *a, int *lda, c *b, int *ldb, s *beta, c *c, int *ldc) noexcept nogil
|
| 37 |
+
cdef void cherk(char *uplo, char *trans, int *n, int *k, s *alpha, c *a, int *lda, s *beta, c *c, int *ldc) noexcept nogil
|
| 38 |
+
cdef void chpmv(char *uplo, int *n, c *alpha, c *ap, c *x, int *incx, c *beta, c *y, int *incy) noexcept nogil
|
| 39 |
+
cdef void chpr(char *uplo, int *n, s *alpha, c *x, int *incx, c *ap) noexcept nogil
|
| 40 |
+
cdef void chpr2(char *uplo, int *n, c *alpha, c *x, int *incx, c *y, int *incy, c *ap) noexcept nogil
|
| 41 |
+
cdef void crotg(c *ca, c *cb, s *c, c *s) noexcept nogil
|
| 42 |
+
cdef void cscal(int *n, c *ca, c *cx, int *incx) noexcept nogil
|
| 43 |
+
cdef void csrot(int *n, c *cx, int *incx, c *cy, int *incy, s *c, s *s) noexcept nogil
|
| 44 |
+
cdef void csscal(int *n, s *sa, c *cx, int *incx) noexcept nogil
|
| 45 |
+
cdef void cswap(int *n, c *cx, int *incx, c *cy, int *incy) noexcept nogil
|
| 46 |
+
cdef void csymm(char *side, char *uplo, int *m, int *n, c *alpha, c *a, int *lda, c *b, int *ldb, c *beta, c *c, int *ldc) noexcept nogil
|
| 47 |
+
cdef void csyr2k(char *uplo, char *trans, int *n, int *k, c *alpha, c *a, int *lda, c *b, int *ldb, c *beta, c *c, int *ldc) noexcept nogil
|
| 48 |
+
cdef void csyrk(char *uplo, char *trans, int *n, int *k, c *alpha, c *a, int *lda, c *beta, c *c, int *ldc) noexcept nogil
|
| 49 |
+
cdef void ctbmv(char *uplo, char *trans, char *diag, int *n, int *k, c *a, int *lda, c *x, int *incx) noexcept nogil
|
| 50 |
+
cdef void ctbsv(char *uplo, char *trans, char *diag, int *n, int *k, c *a, int *lda, c *x, int *incx) noexcept nogil
|
| 51 |
+
cdef void ctpmv(char *uplo, char *trans, char *diag, int *n, c *ap, c *x, int *incx) noexcept nogil
|
| 52 |
+
cdef void ctpsv(char *uplo, char *trans, char *diag, int *n, c *ap, c *x, int *incx) noexcept nogil
|
| 53 |
+
cdef void ctrmm(char *side, char *uplo, char *transa, char *diag, int *m, int *n, c *alpha, c *a, int *lda, c *b, int *ldb) noexcept nogil
|
| 54 |
+
cdef void ctrmv(char *uplo, char *trans, char *diag, int *n, c *a, int *lda, c *x, int *incx) noexcept nogil
|
| 55 |
+
cdef void ctrsm(char *side, char *uplo, char *transa, char *diag, int *m, int *n, c *alpha, c *a, int *lda, c *b, int *ldb) noexcept nogil
|
| 56 |
+
cdef void ctrsv(char *uplo, char *trans, char *diag, int *n, c *a, int *lda, c *x, int *incx) noexcept nogil
|
| 57 |
+
cdef d dasum(int *n, d *dx, int *incx) noexcept nogil
|
| 58 |
+
cdef void daxpy(int *n, d *da, d *dx, int *incx, d *dy, int *incy) noexcept nogil
|
| 59 |
+
cdef d dcabs1(z *z) noexcept nogil
|
| 60 |
+
cdef void dcopy(int *n, d *dx, int *incx, d *dy, int *incy) noexcept nogil
|
| 61 |
+
cdef d ddot(int *n, d *dx, int *incx, d *dy, int *incy) noexcept nogil
|
| 62 |
+
cdef void dgbmv(char *trans, int *m, int *n, int *kl, int *ku, d *alpha, d *a, int *lda, d *x, int *incx, d *beta, d *y, int *incy) noexcept nogil
|
| 63 |
+
cdef void dgemm(char *transa, char *transb, int *m, int *n, int *k, d *alpha, d *a, int *lda, d *b, int *ldb, d *beta, d *c, int *ldc) noexcept nogil
|
| 64 |
+
cdef void dgemv(char *trans, int *m, int *n, d *alpha, d *a, int *lda, d *x, int *incx, d *beta, d *y, int *incy) noexcept nogil
|
| 65 |
+
cdef void dger(int *m, int *n, d *alpha, d *x, int *incx, d *y, int *incy, d *a, int *lda) noexcept nogil
|
| 66 |
+
cdef d dnrm2(int *n, d *x, int *incx) noexcept nogil
|
| 67 |
+
cdef void drot(int *n, d *dx, int *incx, d *dy, int *incy, d *c, d *s) noexcept nogil
|
| 68 |
+
cdef void drotg(d *da, d *db, d *c, d *s) noexcept nogil
|
| 69 |
+
cdef void drotm(int *n, d *dx, int *incx, d *dy, int *incy, d *dparam) noexcept nogil
|
| 70 |
+
cdef void drotmg(d *dd1, d *dd2, d *dx1, d *dy1, d *dparam) noexcept nogil
|
| 71 |
+
cdef void dsbmv(char *uplo, int *n, int *k, d *alpha, d *a, int *lda, d *x, int *incx, d *beta, d *y, int *incy) noexcept nogil
|
| 72 |
+
cdef void dscal(int *n, d *da, d *dx, int *incx) noexcept nogil
|
| 73 |
+
cdef d dsdot(int *n, s *sx, int *incx, s *sy, int *incy) noexcept nogil
|
| 74 |
+
cdef void dspmv(char *uplo, int *n, d *alpha, d *ap, d *x, int *incx, d *beta, d *y, int *incy) noexcept nogil
|
| 75 |
+
cdef void dspr(char *uplo, int *n, d *alpha, d *x, int *incx, d *ap) noexcept nogil
|
| 76 |
+
cdef void dspr2(char *uplo, int *n, d *alpha, d *x, int *incx, d *y, int *incy, d *ap) noexcept nogil
|
| 77 |
+
cdef void dswap(int *n, d *dx, int *incx, d *dy, int *incy) noexcept nogil
|
| 78 |
+
cdef void dsymm(char *side, char *uplo, int *m, int *n, d *alpha, d *a, int *lda, d *b, int *ldb, d *beta, d *c, int *ldc) noexcept nogil
|
| 79 |
+
cdef void dsymv(char *uplo, int *n, d *alpha, d *a, int *lda, d *x, int *incx, d *beta, d *y, int *incy) noexcept nogil
|
| 80 |
+
cdef void dsyr(char *uplo, int *n, d *alpha, d *x, int *incx, d *a, int *lda) noexcept nogil
|
| 81 |
+
cdef void dsyr2(char *uplo, int *n, d *alpha, d *x, int *incx, d *y, int *incy, d *a, int *lda) noexcept nogil
|
| 82 |
+
cdef void dsyr2k(char *uplo, char *trans, int *n, int *k, d *alpha, d *a, int *lda, d *b, int *ldb, d *beta, d *c, int *ldc) noexcept nogil
|
| 83 |
+
cdef void dsyrk(char *uplo, char *trans, int *n, int *k, d *alpha, d *a, int *lda, d *beta, d *c, int *ldc) noexcept nogil
|
| 84 |
+
cdef void dtbmv(char *uplo, char *trans, char *diag, int *n, int *k, d *a, int *lda, d *x, int *incx) noexcept nogil
|
| 85 |
+
cdef void dtbsv(char *uplo, char *trans, char *diag, int *n, int *k, d *a, int *lda, d *x, int *incx) noexcept nogil
|
| 86 |
+
cdef void dtpmv(char *uplo, char *trans, char *diag, int *n, d *ap, d *x, int *incx) noexcept nogil
|
| 87 |
+
cdef void dtpsv(char *uplo, char *trans, char *diag, int *n, d *ap, d *x, int *incx) noexcept nogil
|
| 88 |
+
cdef void dtrmm(char *side, char *uplo, char *transa, char *diag, int *m, int *n, d *alpha, d *a, int *lda, d *b, int *ldb) noexcept nogil
|
| 89 |
+
cdef void dtrmv(char *uplo, char *trans, char *diag, int *n, d *a, int *lda, d *x, int *incx) noexcept nogil
|
| 90 |
+
cdef void dtrsm(char *side, char *uplo, char *transa, char *diag, int *m, int *n, d *alpha, d *a, int *lda, d *b, int *ldb) noexcept nogil
|
| 91 |
+
cdef void dtrsv(char *uplo, char *trans, char *diag, int *n, d *a, int *lda, d *x, int *incx) noexcept nogil
|
| 92 |
+
cdef d dzasum(int *n, z *zx, int *incx) noexcept nogil
|
| 93 |
+
cdef d dznrm2(int *n, z *x, int *incx) noexcept nogil
|
| 94 |
+
cdef int icamax(int *n, c *cx, int *incx) noexcept nogil
|
| 95 |
+
cdef int idamax(int *n, d *dx, int *incx) noexcept nogil
|
| 96 |
+
cdef int isamax(int *n, s *sx, int *incx) noexcept nogil
|
| 97 |
+
cdef int izamax(int *n, z *zx, int *incx) noexcept nogil
|
| 98 |
+
cdef bint lsame(char *ca, char *cb) noexcept nogil
|
| 99 |
+
cdef s sasum(int *n, s *sx, int *incx) noexcept nogil
|
| 100 |
+
cdef void saxpy(int *n, s *sa, s *sx, int *incx, s *sy, int *incy) noexcept nogil
|
| 101 |
+
cdef s scasum(int *n, c *cx, int *incx) noexcept nogil
|
| 102 |
+
cdef s scnrm2(int *n, c *x, int *incx) noexcept nogil
|
| 103 |
+
cdef void scopy(int *n, s *sx, int *incx, s *sy, int *incy) noexcept nogil
|
| 104 |
+
cdef s sdot(int *n, s *sx, int *incx, s *sy, int *incy) noexcept nogil
|
| 105 |
+
cdef s sdsdot(int *n, s *sb, s *sx, int *incx, s *sy, int *incy) noexcept nogil
|
| 106 |
+
cdef void sgbmv(char *trans, int *m, int *n, int *kl, int *ku, s *alpha, s *a, int *lda, s *x, int *incx, s *beta, s *y, int *incy) noexcept nogil
|
| 107 |
+
cdef void sgemm(char *transa, char *transb, int *m, int *n, int *k, s *alpha, s *a, int *lda, s *b, int *ldb, s *beta, s *c, int *ldc) noexcept nogil
|
| 108 |
+
cdef void sgemv(char *trans, int *m, int *n, s *alpha, s *a, int *lda, s *x, int *incx, s *beta, s *y, int *incy) noexcept nogil
|
| 109 |
+
cdef void sger(int *m, int *n, s *alpha, s *x, int *incx, s *y, int *incy, s *a, int *lda) noexcept nogil
|
| 110 |
+
cdef s snrm2(int *n, s *x, int *incx) noexcept nogil
|
| 111 |
+
cdef void srot(int *n, s *sx, int *incx, s *sy, int *incy, s *c, s *s) noexcept nogil
|
| 112 |
+
cdef void srotg(s *sa, s *sb, s *c, s *s) noexcept nogil
|
| 113 |
+
cdef void srotm(int *n, s *sx, int *incx, s *sy, int *incy, s *sparam) noexcept nogil
|
| 114 |
+
cdef void srotmg(s *sd1, s *sd2, s *sx1, s *sy1, s *sparam) noexcept nogil
|
| 115 |
+
cdef void ssbmv(char *uplo, int *n, int *k, s *alpha, s *a, int *lda, s *x, int *incx, s *beta, s *y, int *incy) noexcept nogil
|
| 116 |
+
cdef void sscal(int *n, s *sa, s *sx, int *incx) noexcept nogil
|
| 117 |
+
cdef void sspmv(char *uplo, int *n, s *alpha, s *ap, s *x, int *incx, s *beta, s *y, int *incy) noexcept nogil
|
| 118 |
+
cdef void sspr(char *uplo, int *n, s *alpha, s *x, int *incx, s *ap) noexcept nogil
|
| 119 |
+
cdef void sspr2(char *uplo, int *n, s *alpha, s *x, int *incx, s *y, int *incy, s *ap) noexcept nogil
|
| 120 |
+
cdef void sswap(int *n, s *sx, int *incx, s *sy, int *incy) noexcept nogil
|
| 121 |
+
cdef void ssymm(char *side, char *uplo, int *m, int *n, s *alpha, s *a, int *lda, s *b, int *ldb, s *beta, s *c, int *ldc) noexcept nogil
|
| 122 |
+
cdef void ssymv(char *uplo, int *n, s *alpha, s *a, int *lda, s *x, int *incx, s *beta, s *y, int *incy) noexcept nogil
|
| 123 |
+
cdef void ssyr(char *uplo, int *n, s *alpha, s *x, int *incx, s *a, int *lda) noexcept nogil
|
| 124 |
+
cdef void ssyr2(char *uplo, int *n, s *alpha, s *x, int *incx, s *y, int *incy, s *a, int *lda) noexcept nogil
|
| 125 |
+
cdef void ssyr2k(char *uplo, char *trans, int *n, int *k, s *alpha, s *a, int *lda, s *b, int *ldb, s *beta, s *c, int *ldc) noexcept nogil
|
| 126 |
+
cdef void ssyrk(char *uplo, char *trans, int *n, int *k, s *alpha, s *a, int *lda, s *beta, s *c, int *ldc) noexcept nogil
|
| 127 |
+
cdef void stbmv(char *uplo, char *trans, char *diag, int *n, int *k, s *a, int *lda, s *x, int *incx) noexcept nogil
|
| 128 |
+
cdef void stbsv(char *uplo, char *trans, char *diag, int *n, int *k, s *a, int *lda, s *x, int *incx) noexcept nogil
|
| 129 |
+
cdef void stpmv(char *uplo, char *trans, char *diag, int *n, s *ap, s *x, int *incx) noexcept nogil
|
| 130 |
+
cdef void stpsv(char *uplo, char *trans, char *diag, int *n, s *ap, s *x, int *incx) noexcept nogil
|
| 131 |
+
cdef void strmm(char *side, char *uplo, char *transa, char *diag, int *m, int *n, s *alpha, s *a, int *lda, s *b, int *ldb) noexcept nogil
|
| 132 |
+
cdef void strmv(char *uplo, char *trans, char *diag, int *n, s *a, int *lda, s *x, int *incx) noexcept nogil
|
| 133 |
+
cdef void strsm(char *side, char *uplo, char *transa, char *diag, int *m, int *n, s *alpha, s *a, int *lda, s *b, int *ldb) noexcept nogil
|
| 134 |
+
cdef void strsv(char *uplo, char *trans, char *diag, int *n, s *a, int *lda, s *x, int *incx) noexcept nogil
|
| 135 |
+
cdef void zaxpy(int *n, z *za, z *zx, int *incx, z *zy, int *incy) noexcept nogil
|
| 136 |
+
cdef void zcopy(int *n, z *zx, int *incx, z *zy, int *incy) noexcept nogil
|
| 137 |
+
cdef z zdotc(int *n, z *zx, int *incx, z *zy, int *incy) noexcept nogil
|
| 138 |
+
cdef z zdotu(int *n, z *zx, int *incx, z *zy, int *incy) noexcept nogil
|
| 139 |
+
cdef void zdrot(int *n, z *cx, int *incx, z *cy, int *incy, d *c, d *s) noexcept nogil
|
| 140 |
+
cdef void zdscal(int *n, d *da, z *zx, int *incx) noexcept nogil
|
| 141 |
+
cdef void zgbmv(char *trans, int *m, int *n, int *kl, int *ku, z *alpha, z *a, int *lda, z *x, int *incx, z *beta, z *y, int *incy) noexcept nogil
|
| 142 |
+
cdef void zgemm(char *transa, char *transb, int *m, int *n, int *k, z *alpha, z *a, int *lda, z *b, int *ldb, z *beta, z *c, int *ldc) noexcept nogil
|
| 143 |
+
cdef void zgemv(char *trans, int *m, int *n, z *alpha, z *a, int *lda, z *x, int *incx, z *beta, z *y, int *incy) noexcept nogil
|
| 144 |
+
cdef void zgerc(int *m, int *n, z *alpha, z *x, int *incx, z *y, int *incy, z *a, int *lda) noexcept nogil
|
| 145 |
+
cdef void zgeru(int *m, int *n, z *alpha, z *x, int *incx, z *y, int *incy, z *a, int *lda) noexcept nogil
|
| 146 |
+
cdef void zhbmv(char *uplo, int *n, int *k, z *alpha, z *a, int *lda, z *x, int *incx, z *beta, z *y, int *incy) noexcept nogil
|
| 147 |
+
cdef void zhemm(char *side, char *uplo, int *m, int *n, z *alpha, z *a, int *lda, z *b, int *ldb, z *beta, z *c, int *ldc) noexcept nogil
|
| 148 |
+
cdef void zhemv(char *uplo, int *n, z *alpha, z *a, int *lda, z *x, int *incx, z *beta, z *y, int *incy) noexcept nogil
|
| 149 |
+
cdef void zher(char *uplo, int *n, d *alpha, z *x, int *incx, z *a, int *lda) noexcept nogil
|
| 150 |
+
cdef void zher2(char *uplo, int *n, z *alpha, z *x, int *incx, z *y, int *incy, z *a, int *lda) noexcept nogil
|
| 151 |
+
cdef void zher2k(char *uplo, char *trans, int *n, int *k, z *alpha, z *a, int *lda, z *b, int *ldb, d *beta, z *c, int *ldc) noexcept nogil
|
| 152 |
+
cdef void zherk(char *uplo, char *trans, int *n, int *k, d *alpha, z *a, int *lda, d *beta, z *c, int *ldc) noexcept nogil
|
| 153 |
+
cdef void zhpmv(char *uplo, int *n, z *alpha, z *ap, z *x, int *incx, z *beta, z *y, int *incy) noexcept nogil
|
| 154 |
+
cdef void zhpr(char *uplo, int *n, d *alpha, z *x, int *incx, z *ap) noexcept nogil
|
| 155 |
+
cdef void zhpr2(char *uplo, int *n, z *alpha, z *x, int *incx, z *y, int *incy, z *ap) noexcept nogil
|
| 156 |
+
cdef void zrotg(z *ca, z *cb, d *c, z *s) noexcept nogil
|
| 157 |
+
cdef void zscal(int *n, z *za, z *zx, int *incx) noexcept nogil
|
| 158 |
+
cdef void zswap(int *n, z *zx, int *incx, z *zy, int *incy) noexcept nogil
|
| 159 |
+
cdef void zsymm(char *side, char *uplo, int *m, int *n, z *alpha, z *a, int *lda, z *b, int *ldb, z *beta, z *c, int *ldc) noexcept nogil
|
| 160 |
+
cdef void zsyr2k(char *uplo, char *trans, int *n, int *k, z *alpha, z *a, int *lda, z *b, int *ldb, z *beta, z *c, int *ldc) noexcept nogil
|
| 161 |
+
cdef void zsyrk(char *uplo, char *trans, int *n, int *k, z *alpha, z *a, int *lda, z *beta, z *c, int *ldc) noexcept nogil
|
| 162 |
+
cdef void ztbmv(char *uplo, char *trans, char *diag, int *n, int *k, z *a, int *lda, z *x, int *incx) noexcept nogil
|
| 163 |
+
cdef void ztbsv(char *uplo, char *trans, char *diag, int *n, int *k, z *a, int *lda, z *x, int *incx) noexcept nogil
|
| 164 |
+
cdef void ztpmv(char *uplo, char *trans, char *diag, int *n, z *ap, z *x, int *incx) noexcept nogil
|
| 165 |
+
cdef void ztpsv(char *uplo, char *trans, char *diag, int *n, z *ap, z *x, int *incx) noexcept nogil
|
| 166 |
+
cdef void ztrmm(char *side, char *uplo, char *transa, char *diag, int *m, int *n, z *alpha, z *a, int *lda, z *b, int *ldb) noexcept nogil
|
| 167 |
+
cdef void ztrmv(char *uplo, char *trans, char *diag, int *n, z *a, int *lda, z *x, int *incx) noexcept nogil
|
| 168 |
+
cdef void ztrsm(char *side, char *uplo, char *transa, char *diag, int *m, int *n, z *alpha, z *a, int *lda, z *b, int *ldb) noexcept nogil
|
| 169 |
+
cdef void ztrsv(char *uplo, char *trans, char *diag, int *n, z *a, int *lda, z *x, int *incx) noexcept nogil
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/cython_blas.pyx
ADDED
|
@@ -0,0 +1,1432 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This file was generated by _generate_pyx.py.
|
| 2 |
+
# Do not edit this file directly.
|
| 3 |
+
# cython: boundscheck = False
|
| 4 |
+
# cython: wraparound = False
|
| 5 |
+
# cython: cdivision = True
|
| 6 |
+
|
| 7 |
+
"""
|
| 8 |
+
BLAS Functions for Cython
|
| 9 |
+
=========================
|
| 10 |
+
|
| 11 |
+
Usable from Cython via::
|
| 12 |
+
|
| 13 |
+
cimport scipy.linalg.cython_blas
|
| 14 |
+
|
| 15 |
+
These wrappers do not check for alignment of arrays.
|
| 16 |
+
Alignment should be checked before these wrappers are used.
|
| 17 |
+
|
| 18 |
+
If using ``cdotu``, ``cdotc``, ``zdotu``, ``zdotc``, ``sladiv``, or ``dladiv``,
|
| 19 |
+
the ``CYTHON_CCOMPLEX`` define must be set to 0 during compilation. For
|
| 20 |
+
example, in a ``meson.build`` file when using Meson::
|
| 21 |
+
|
| 22 |
+
py.extension_module('ext_module'
|
| 23 |
+
'ext_module.pyx',
|
| 24 |
+
c_args: ['-DCYTHON_CCOMPLEX=0'],
|
| 25 |
+
...
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
Raw function pointers (Fortran-style pointer arguments):
|
| 29 |
+
|
| 30 |
+
- caxpy
|
| 31 |
+
- ccopy
|
| 32 |
+
- cdotc
|
| 33 |
+
- cdotu
|
| 34 |
+
- cgbmv
|
| 35 |
+
- cgemm
|
| 36 |
+
- cgemv
|
| 37 |
+
- cgerc
|
| 38 |
+
- cgeru
|
| 39 |
+
- chbmv
|
| 40 |
+
- chemm
|
| 41 |
+
- chemv
|
| 42 |
+
- cher
|
| 43 |
+
- cher2
|
| 44 |
+
- cher2k
|
| 45 |
+
- cherk
|
| 46 |
+
- chpmv
|
| 47 |
+
- chpr
|
| 48 |
+
- chpr2
|
| 49 |
+
- crotg
|
| 50 |
+
- cscal
|
| 51 |
+
- csrot
|
| 52 |
+
- csscal
|
| 53 |
+
- cswap
|
| 54 |
+
- csymm
|
| 55 |
+
- csyr2k
|
| 56 |
+
- csyrk
|
| 57 |
+
- ctbmv
|
| 58 |
+
- ctbsv
|
| 59 |
+
- ctpmv
|
| 60 |
+
- ctpsv
|
| 61 |
+
- ctrmm
|
| 62 |
+
- ctrmv
|
| 63 |
+
- ctrsm
|
| 64 |
+
- ctrsv
|
| 65 |
+
- dasum
|
| 66 |
+
- daxpy
|
| 67 |
+
- dcabs1
|
| 68 |
+
- dcopy
|
| 69 |
+
- ddot
|
| 70 |
+
- dgbmv
|
| 71 |
+
- dgemm
|
| 72 |
+
- dgemv
|
| 73 |
+
- dger
|
| 74 |
+
- dnrm2
|
| 75 |
+
- drot
|
| 76 |
+
- drotg
|
| 77 |
+
- drotm
|
| 78 |
+
- drotmg
|
| 79 |
+
- dsbmv
|
| 80 |
+
- dscal
|
| 81 |
+
- dsdot
|
| 82 |
+
- dspmv
|
| 83 |
+
- dspr
|
| 84 |
+
- dspr2
|
| 85 |
+
- dswap
|
| 86 |
+
- dsymm
|
| 87 |
+
- dsymv
|
| 88 |
+
- dsyr
|
| 89 |
+
- dsyr2
|
| 90 |
+
- dsyr2k
|
| 91 |
+
- dsyrk
|
| 92 |
+
- dtbmv
|
| 93 |
+
- dtbsv
|
| 94 |
+
- dtpmv
|
| 95 |
+
- dtpsv
|
| 96 |
+
- dtrmm
|
| 97 |
+
- dtrmv
|
| 98 |
+
- dtrsm
|
| 99 |
+
- dtrsv
|
| 100 |
+
- dzasum
|
| 101 |
+
- dznrm2
|
| 102 |
+
- icamax
|
| 103 |
+
- idamax
|
| 104 |
+
- isamax
|
| 105 |
+
- izamax
|
| 106 |
+
- lsame
|
| 107 |
+
- sasum
|
| 108 |
+
- saxpy
|
| 109 |
+
- scasum
|
| 110 |
+
- scnrm2
|
| 111 |
+
- scopy
|
| 112 |
+
- sdot
|
| 113 |
+
- sdsdot
|
| 114 |
+
- sgbmv
|
| 115 |
+
- sgemm
|
| 116 |
+
- sgemv
|
| 117 |
+
- sger
|
| 118 |
+
- snrm2
|
| 119 |
+
- srot
|
| 120 |
+
- srotg
|
| 121 |
+
- srotm
|
| 122 |
+
- srotmg
|
| 123 |
+
- ssbmv
|
| 124 |
+
- sscal
|
| 125 |
+
- sspmv
|
| 126 |
+
- sspr
|
| 127 |
+
- sspr2
|
| 128 |
+
- sswap
|
| 129 |
+
- ssymm
|
| 130 |
+
- ssymv
|
| 131 |
+
- ssyr
|
| 132 |
+
- ssyr2
|
| 133 |
+
- ssyr2k
|
| 134 |
+
- ssyrk
|
| 135 |
+
- stbmv
|
| 136 |
+
- stbsv
|
| 137 |
+
- stpmv
|
| 138 |
+
- stpsv
|
| 139 |
+
- strmm
|
| 140 |
+
- strmv
|
| 141 |
+
- strsm
|
| 142 |
+
- strsv
|
| 143 |
+
- zaxpy
|
| 144 |
+
- zcopy
|
| 145 |
+
- zdotc
|
| 146 |
+
- zdotu
|
| 147 |
+
- zdrot
|
| 148 |
+
- zdscal
|
| 149 |
+
- zgbmv
|
| 150 |
+
- zgemm
|
| 151 |
+
- zgemv
|
| 152 |
+
- zgerc
|
| 153 |
+
- zgeru
|
| 154 |
+
- zhbmv
|
| 155 |
+
- zhemm
|
| 156 |
+
- zhemv
|
| 157 |
+
- zher
|
| 158 |
+
- zher2
|
| 159 |
+
- zher2k
|
| 160 |
+
- zherk
|
| 161 |
+
- zhpmv
|
| 162 |
+
- zhpr
|
| 163 |
+
- zhpr2
|
| 164 |
+
- zrotg
|
| 165 |
+
- zscal
|
| 166 |
+
- zswap
|
| 167 |
+
- zsymm
|
| 168 |
+
- zsyr2k
|
| 169 |
+
- zsyrk
|
| 170 |
+
- ztbmv
|
| 171 |
+
- ztbsv
|
| 172 |
+
- ztpmv
|
| 173 |
+
- ztpsv
|
| 174 |
+
- ztrmm
|
| 175 |
+
- ztrmv
|
| 176 |
+
- ztrsm
|
| 177 |
+
- ztrsv
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
"""
|
| 181 |
+
|
| 182 |
+
# Within SciPy, these wrappers can be used via relative or absolute cimport.
|
| 183 |
+
# Examples:
|
| 184 |
+
# from ..linalg cimport cython_blas
|
| 185 |
+
# from scipy.linalg cimport cython_blas
|
| 186 |
+
# cimport scipy.linalg.cython_blas as cython_blas
|
| 187 |
+
# cimport ..linalg.cython_blas as cython_blas
|
| 188 |
+
|
| 189 |
+
# Within SciPy, if BLAS functions are needed in C/C++/Fortran,
|
| 190 |
+
# these wrappers should not be used.
|
| 191 |
+
# The original libraries should be linked directly.
|
| 192 |
+
|
| 193 |
+
cdef extern from "fortran_defs.h":
|
| 194 |
+
pass
|
| 195 |
+
|
| 196 |
+
from numpy cimport npy_complex64, npy_complex128
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
cdef extern from "_blas_subroutines.h":
|
| 200 |
+
void _fortran_caxpy "BLAS_FUNC(caxpy)"(int *n, npy_complex64 *ca, npy_complex64 *cx, int *incx, npy_complex64 *cy, int *incy) nogil
|
| 201 |
+
cdef void caxpy(int *n, c *ca, c *cx, int *incx, c *cy, int *incy) noexcept nogil:
|
| 202 |
+
|
| 203 |
+
_fortran_caxpy(n, <npy_complex64*>ca, <npy_complex64*>cx, incx, <npy_complex64*>cy, incy)
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
cdef extern from "_blas_subroutines.h":
|
| 207 |
+
void _fortran_ccopy "BLAS_FUNC(ccopy)"(int *n, npy_complex64 *cx, int *incx, npy_complex64 *cy, int *incy) nogil
|
| 208 |
+
cdef void ccopy(int *n, c *cx, int *incx, c *cy, int *incy) noexcept nogil:
|
| 209 |
+
|
| 210 |
+
_fortran_ccopy(n, <npy_complex64*>cx, incx, <npy_complex64*>cy, incy)
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
cdef extern from "_blas_subroutines.h":
|
| 214 |
+
void _fortran_cdotc "F_FUNC(cdotcwrp,CDOTCWRP)"(npy_complex64 *out, int *n, npy_complex64 *cx, int *incx, npy_complex64 *cy, int *incy) nogil
|
| 215 |
+
cdef c cdotc(int *n, c *cx, int *incx, c *cy, int *incy) noexcept nogil:
|
| 216 |
+
cdef c out
|
| 217 |
+
_fortran_cdotc(<npy_complex64*>&out, n, <npy_complex64*>cx, incx, <npy_complex64*>cy, incy)
|
| 218 |
+
return out
|
| 219 |
+
|
| 220 |
+
cdef extern from "_blas_subroutines.h":
|
| 221 |
+
void _fortran_cdotu "F_FUNC(cdotuwrp,CDOTUWRP)"(npy_complex64 *out, int *n, npy_complex64 *cx, int *incx, npy_complex64 *cy, int *incy) nogil
|
| 222 |
+
cdef c cdotu(int *n, c *cx, int *incx, c *cy, int *incy) noexcept nogil:
|
| 223 |
+
cdef c out
|
| 224 |
+
_fortran_cdotu(<npy_complex64*>&out, n, <npy_complex64*>cx, incx, <npy_complex64*>cy, incy)
|
| 225 |
+
return out
|
| 226 |
+
|
| 227 |
+
cdef extern from "_blas_subroutines.h":
|
| 228 |
+
void _fortran_cgbmv "BLAS_FUNC(cgbmv)"(char *trans, int *m, int *n, int *kl, int *ku, npy_complex64 *alpha, npy_complex64 *a, int *lda, npy_complex64 *x, int *incx, npy_complex64 *beta, npy_complex64 *y, int *incy) nogil
|
| 229 |
+
cdef void cgbmv(char *trans, int *m, int *n, int *kl, int *ku, c *alpha, c *a, int *lda, c *x, int *incx, c *beta, c *y, int *incy) noexcept nogil:
|
| 230 |
+
|
| 231 |
+
_fortran_cgbmv(trans, m, n, kl, ku, <npy_complex64*>alpha, <npy_complex64*>a, lda, <npy_complex64*>x, incx, <npy_complex64*>beta, <npy_complex64*>y, incy)
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
cdef extern from "_blas_subroutines.h":
|
| 235 |
+
void _fortran_cgemm "BLAS_FUNC(cgemm)"(char *transa, char *transb, int *m, int *n, int *k, npy_complex64 *alpha, npy_complex64 *a, int *lda, npy_complex64 *b, int *ldb, npy_complex64 *beta, npy_complex64 *c, int *ldc) nogil
|
| 236 |
+
cdef void cgemm(char *transa, char *transb, int *m, int *n, int *k, c *alpha, c *a, int *lda, c *b, int *ldb, c *beta, c *c, int *ldc) noexcept nogil:
|
| 237 |
+
|
| 238 |
+
_fortran_cgemm(transa, transb, m, n, k, <npy_complex64*>alpha, <npy_complex64*>a, lda, <npy_complex64*>b, ldb, <npy_complex64*>beta, <npy_complex64*>c, ldc)
|
| 239 |
+
|
| 240 |
+
|
| 241 |
+
cdef extern from "_blas_subroutines.h":
|
| 242 |
+
void _fortran_cgemv "BLAS_FUNC(cgemv)"(char *trans, int *m, int *n, npy_complex64 *alpha, npy_complex64 *a, int *lda, npy_complex64 *x, int *incx, npy_complex64 *beta, npy_complex64 *y, int *incy) nogil
|
| 243 |
+
cdef void cgemv(char *trans, int *m, int *n, c *alpha, c *a, int *lda, c *x, int *incx, c *beta, c *y, int *incy) noexcept nogil:
|
| 244 |
+
|
| 245 |
+
_fortran_cgemv(trans, m, n, <npy_complex64*>alpha, <npy_complex64*>a, lda, <npy_complex64*>x, incx, <npy_complex64*>beta, <npy_complex64*>y, incy)
|
| 246 |
+
|
| 247 |
+
|
| 248 |
+
cdef extern from "_blas_subroutines.h":
|
| 249 |
+
void _fortran_cgerc "BLAS_FUNC(cgerc)"(int *m, int *n, npy_complex64 *alpha, npy_complex64 *x, int *incx, npy_complex64 *y, int *incy, npy_complex64 *a, int *lda) nogil
|
| 250 |
+
cdef void cgerc(int *m, int *n, c *alpha, c *x, int *incx, c *y, int *incy, c *a, int *lda) noexcept nogil:
|
| 251 |
+
|
| 252 |
+
_fortran_cgerc(m, n, <npy_complex64*>alpha, <npy_complex64*>x, incx, <npy_complex64*>y, incy, <npy_complex64*>a, lda)
|
| 253 |
+
|
| 254 |
+
|
| 255 |
+
cdef extern from "_blas_subroutines.h":
|
| 256 |
+
void _fortran_cgeru "BLAS_FUNC(cgeru)"(int *m, int *n, npy_complex64 *alpha, npy_complex64 *x, int *incx, npy_complex64 *y, int *incy, npy_complex64 *a, int *lda) nogil
|
| 257 |
+
cdef void cgeru(int *m, int *n, c *alpha, c *x, int *incx, c *y, int *incy, c *a, int *lda) noexcept nogil:
|
| 258 |
+
|
| 259 |
+
_fortran_cgeru(m, n, <npy_complex64*>alpha, <npy_complex64*>x, incx, <npy_complex64*>y, incy, <npy_complex64*>a, lda)
|
| 260 |
+
|
| 261 |
+
|
| 262 |
+
cdef extern from "_blas_subroutines.h":
|
| 263 |
+
void _fortran_chbmv "BLAS_FUNC(chbmv)"(char *uplo, int *n, int *k, npy_complex64 *alpha, npy_complex64 *a, int *lda, npy_complex64 *x, int *incx, npy_complex64 *beta, npy_complex64 *y, int *incy) nogil
|
| 264 |
+
cdef void chbmv(char *uplo, int *n, int *k, c *alpha, c *a, int *lda, c *x, int *incx, c *beta, c *y, int *incy) noexcept nogil:
|
| 265 |
+
|
| 266 |
+
_fortran_chbmv(uplo, n, k, <npy_complex64*>alpha, <npy_complex64*>a, lda, <npy_complex64*>x, incx, <npy_complex64*>beta, <npy_complex64*>y, incy)
|
| 267 |
+
|
| 268 |
+
|
| 269 |
+
cdef extern from "_blas_subroutines.h":
|
| 270 |
+
void _fortran_chemm "BLAS_FUNC(chemm)"(char *side, char *uplo, int *m, int *n, npy_complex64 *alpha, npy_complex64 *a, int *lda, npy_complex64 *b, int *ldb, npy_complex64 *beta, npy_complex64 *c, int *ldc) nogil
|
| 271 |
+
cdef void chemm(char *side, char *uplo, int *m, int *n, c *alpha, c *a, int *lda, c *b, int *ldb, c *beta, c *c, int *ldc) noexcept nogil:
|
| 272 |
+
|
| 273 |
+
_fortran_chemm(side, uplo, m, n, <npy_complex64*>alpha, <npy_complex64*>a, lda, <npy_complex64*>b, ldb, <npy_complex64*>beta, <npy_complex64*>c, ldc)
|
| 274 |
+
|
| 275 |
+
|
| 276 |
+
cdef extern from "_blas_subroutines.h":
|
| 277 |
+
void _fortran_chemv "BLAS_FUNC(chemv)"(char *uplo, int *n, npy_complex64 *alpha, npy_complex64 *a, int *lda, npy_complex64 *x, int *incx, npy_complex64 *beta, npy_complex64 *y, int *incy) nogil
|
| 278 |
+
cdef void chemv(char *uplo, int *n, c *alpha, c *a, int *lda, c *x, int *incx, c *beta, c *y, int *incy) noexcept nogil:
|
| 279 |
+
|
| 280 |
+
_fortran_chemv(uplo, n, <npy_complex64*>alpha, <npy_complex64*>a, lda, <npy_complex64*>x, incx, <npy_complex64*>beta, <npy_complex64*>y, incy)
|
| 281 |
+
|
| 282 |
+
|
| 283 |
+
cdef extern from "_blas_subroutines.h":
|
| 284 |
+
void _fortran_cher "BLAS_FUNC(cher)"(char *uplo, int *n, s *alpha, npy_complex64 *x, int *incx, npy_complex64 *a, int *lda) nogil
|
| 285 |
+
cdef void cher(char *uplo, int *n, s *alpha, c *x, int *incx, c *a, int *lda) noexcept nogil:
|
| 286 |
+
|
| 287 |
+
_fortran_cher(uplo, n, alpha, <npy_complex64*>x, incx, <npy_complex64*>a, lda)
|
| 288 |
+
|
| 289 |
+
|
| 290 |
+
cdef extern from "_blas_subroutines.h":
|
| 291 |
+
void _fortran_cher2 "BLAS_FUNC(cher2)"(char *uplo, int *n, npy_complex64 *alpha, npy_complex64 *x, int *incx, npy_complex64 *y, int *incy, npy_complex64 *a, int *lda) nogil
|
| 292 |
+
cdef void cher2(char *uplo, int *n, c *alpha, c *x, int *incx, c *y, int *incy, c *a, int *lda) noexcept nogil:
|
| 293 |
+
|
| 294 |
+
_fortran_cher2(uplo, n, <npy_complex64*>alpha, <npy_complex64*>x, incx, <npy_complex64*>y, incy, <npy_complex64*>a, lda)
|
| 295 |
+
|
| 296 |
+
|
| 297 |
+
cdef extern from "_blas_subroutines.h":
|
| 298 |
+
void _fortran_cher2k "BLAS_FUNC(cher2k)"(char *uplo, char *trans, int *n, int *k, npy_complex64 *alpha, npy_complex64 *a, int *lda, npy_complex64 *b, int *ldb, s *beta, npy_complex64 *c, int *ldc) nogil
|
| 299 |
+
cdef void cher2k(char *uplo, char *trans, int *n, int *k, c *alpha, c *a, int *lda, c *b, int *ldb, s *beta, c *c, int *ldc) noexcept nogil:
|
| 300 |
+
|
| 301 |
+
_fortran_cher2k(uplo, trans, n, k, <npy_complex64*>alpha, <npy_complex64*>a, lda, <npy_complex64*>b, ldb, beta, <npy_complex64*>c, ldc)
|
| 302 |
+
|
| 303 |
+
|
| 304 |
+
cdef extern from "_blas_subroutines.h":
|
| 305 |
+
void _fortran_cherk "BLAS_FUNC(cherk)"(char *uplo, char *trans, int *n, int *k, s *alpha, npy_complex64 *a, int *lda, s *beta, npy_complex64 *c, int *ldc) nogil
|
| 306 |
+
cdef void cherk(char *uplo, char *trans, int *n, int *k, s *alpha, c *a, int *lda, s *beta, c *c, int *ldc) noexcept nogil:
|
| 307 |
+
|
| 308 |
+
_fortran_cherk(uplo, trans, n, k, alpha, <npy_complex64*>a, lda, beta, <npy_complex64*>c, ldc)
|
| 309 |
+
|
| 310 |
+
|
| 311 |
+
cdef extern from "_blas_subroutines.h":
|
| 312 |
+
void _fortran_chpmv "BLAS_FUNC(chpmv)"(char *uplo, int *n, npy_complex64 *alpha, npy_complex64 *ap, npy_complex64 *x, int *incx, npy_complex64 *beta, npy_complex64 *y, int *incy) nogil
|
| 313 |
+
cdef void chpmv(char *uplo, int *n, c *alpha, c *ap, c *x, int *incx, c *beta, c *y, int *incy) noexcept nogil:
|
| 314 |
+
|
| 315 |
+
_fortran_chpmv(uplo, n, <npy_complex64*>alpha, <npy_complex64*>ap, <npy_complex64*>x, incx, <npy_complex64*>beta, <npy_complex64*>y, incy)
|
| 316 |
+
|
| 317 |
+
|
| 318 |
+
cdef extern from "_blas_subroutines.h":
|
| 319 |
+
void _fortran_chpr "BLAS_FUNC(chpr)"(char *uplo, int *n, s *alpha, npy_complex64 *x, int *incx, npy_complex64 *ap) nogil
|
| 320 |
+
cdef void chpr(char *uplo, int *n, s *alpha, c *x, int *incx, c *ap) noexcept nogil:
|
| 321 |
+
|
| 322 |
+
_fortran_chpr(uplo, n, alpha, <npy_complex64*>x, incx, <npy_complex64*>ap)
|
| 323 |
+
|
| 324 |
+
|
| 325 |
+
cdef extern from "_blas_subroutines.h":
|
| 326 |
+
void _fortran_chpr2 "BLAS_FUNC(chpr2)"(char *uplo, int *n, npy_complex64 *alpha, npy_complex64 *x, int *incx, npy_complex64 *y, int *incy, npy_complex64 *ap) nogil
|
| 327 |
+
cdef void chpr2(char *uplo, int *n, c *alpha, c *x, int *incx, c *y, int *incy, c *ap) noexcept nogil:
|
| 328 |
+
|
| 329 |
+
_fortran_chpr2(uplo, n, <npy_complex64*>alpha, <npy_complex64*>x, incx, <npy_complex64*>y, incy, <npy_complex64*>ap)
|
| 330 |
+
|
| 331 |
+
|
| 332 |
+
cdef extern from "_blas_subroutines.h":
|
| 333 |
+
void _fortran_crotg "BLAS_FUNC(crotg)"(npy_complex64 *ca, npy_complex64 *cb, s *c, npy_complex64 *s) nogil
|
| 334 |
+
cdef void crotg(c *ca, c *cb, s *c, c *s) noexcept nogil:
|
| 335 |
+
|
| 336 |
+
_fortran_crotg(<npy_complex64*>ca, <npy_complex64*>cb, c, <npy_complex64*>s)
|
| 337 |
+
|
| 338 |
+
|
| 339 |
+
cdef extern from "_blas_subroutines.h":
|
| 340 |
+
void _fortran_cscal "BLAS_FUNC(cscal)"(int *n, npy_complex64 *ca, npy_complex64 *cx, int *incx) nogil
|
| 341 |
+
cdef void cscal(int *n, c *ca, c *cx, int *incx) noexcept nogil:
|
| 342 |
+
|
| 343 |
+
_fortran_cscal(n, <npy_complex64*>ca, <npy_complex64*>cx, incx)
|
| 344 |
+
|
| 345 |
+
|
| 346 |
+
cdef extern from "_blas_subroutines.h":
|
| 347 |
+
void _fortran_csrot "BLAS_FUNC(csrot)"(int *n, npy_complex64 *cx, int *incx, npy_complex64 *cy, int *incy, s *c, s *s) nogil
|
| 348 |
+
cdef void csrot(int *n, c *cx, int *incx, c *cy, int *incy, s *c, s *s) noexcept nogil:
|
| 349 |
+
|
| 350 |
+
_fortran_csrot(n, <npy_complex64*>cx, incx, <npy_complex64*>cy, incy, c, s)
|
| 351 |
+
|
| 352 |
+
|
| 353 |
+
cdef extern from "_blas_subroutines.h":
|
| 354 |
+
void _fortran_csscal "BLAS_FUNC(csscal)"(int *n, s *sa, npy_complex64 *cx, int *incx) nogil
|
| 355 |
+
cdef void csscal(int *n, s *sa, c *cx, int *incx) noexcept nogil:
|
| 356 |
+
|
| 357 |
+
_fortran_csscal(n, sa, <npy_complex64*>cx, incx)
|
| 358 |
+
|
| 359 |
+
|
| 360 |
+
cdef extern from "_blas_subroutines.h":
|
| 361 |
+
void _fortran_cswap "BLAS_FUNC(cswap)"(int *n, npy_complex64 *cx, int *incx, npy_complex64 *cy, int *incy) nogil
|
| 362 |
+
cdef void cswap(int *n, c *cx, int *incx, c *cy, int *incy) noexcept nogil:
|
| 363 |
+
|
| 364 |
+
_fortran_cswap(n, <npy_complex64*>cx, incx, <npy_complex64*>cy, incy)
|
| 365 |
+
|
| 366 |
+
|
| 367 |
+
cdef extern from "_blas_subroutines.h":
|
| 368 |
+
void _fortran_csymm "BLAS_FUNC(csymm)"(char *side, char *uplo, int *m, int *n, npy_complex64 *alpha, npy_complex64 *a, int *lda, npy_complex64 *b, int *ldb, npy_complex64 *beta, npy_complex64 *c, int *ldc) nogil
|
| 369 |
+
cdef void csymm(char *side, char *uplo, int *m, int *n, c *alpha, c *a, int *lda, c *b, int *ldb, c *beta, c *c, int *ldc) noexcept nogil:
|
| 370 |
+
|
| 371 |
+
_fortran_csymm(side, uplo, m, n, <npy_complex64*>alpha, <npy_complex64*>a, lda, <npy_complex64*>b, ldb, <npy_complex64*>beta, <npy_complex64*>c, ldc)
|
| 372 |
+
|
| 373 |
+
|
| 374 |
+
cdef extern from "_blas_subroutines.h":
|
| 375 |
+
void _fortran_csyr2k "BLAS_FUNC(csyr2k)"(char *uplo, char *trans, int *n, int *k, npy_complex64 *alpha, npy_complex64 *a, int *lda, npy_complex64 *b, int *ldb, npy_complex64 *beta, npy_complex64 *c, int *ldc) nogil
|
| 376 |
+
cdef void csyr2k(char *uplo, char *trans, int *n, int *k, c *alpha, c *a, int *lda, c *b, int *ldb, c *beta, c *c, int *ldc) noexcept nogil:
|
| 377 |
+
|
| 378 |
+
_fortran_csyr2k(uplo, trans, n, k, <npy_complex64*>alpha, <npy_complex64*>a, lda, <npy_complex64*>b, ldb, <npy_complex64*>beta, <npy_complex64*>c, ldc)
|
| 379 |
+
|
| 380 |
+
|
| 381 |
+
cdef extern from "_blas_subroutines.h":
|
| 382 |
+
void _fortran_csyrk "BLAS_FUNC(csyrk)"(char *uplo, char *trans, int *n, int *k, npy_complex64 *alpha, npy_complex64 *a, int *lda, npy_complex64 *beta, npy_complex64 *c, int *ldc) nogil
|
| 383 |
+
cdef void csyrk(char *uplo, char *trans, int *n, int *k, c *alpha, c *a, int *lda, c *beta, c *c, int *ldc) noexcept nogil:
|
| 384 |
+
|
| 385 |
+
_fortran_csyrk(uplo, trans, n, k, <npy_complex64*>alpha, <npy_complex64*>a, lda, <npy_complex64*>beta, <npy_complex64*>c, ldc)
|
| 386 |
+
|
| 387 |
+
|
| 388 |
+
cdef extern from "_blas_subroutines.h":
|
| 389 |
+
void _fortran_ctbmv "BLAS_FUNC(ctbmv)"(char *uplo, char *trans, char *diag, int *n, int *k, npy_complex64 *a, int *lda, npy_complex64 *x, int *incx) nogil
|
| 390 |
+
cdef void ctbmv(char *uplo, char *trans, char *diag, int *n, int *k, c *a, int *lda, c *x, int *incx) noexcept nogil:
|
| 391 |
+
|
| 392 |
+
_fortran_ctbmv(uplo, trans, diag, n, k, <npy_complex64*>a, lda, <npy_complex64*>x, incx)
|
| 393 |
+
|
| 394 |
+
|
| 395 |
+
cdef extern from "_blas_subroutines.h":
|
| 396 |
+
void _fortran_ctbsv "BLAS_FUNC(ctbsv)"(char *uplo, char *trans, char *diag, int *n, int *k, npy_complex64 *a, int *lda, npy_complex64 *x, int *incx) nogil
|
| 397 |
+
cdef void ctbsv(char *uplo, char *trans, char *diag, int *n, int *k, c *a, int *lda, c *x, int *incx) noexcept nogil:
|
| 398 |
+
|
| 399 |
+
_fortran_ctbsv(uplo, trans, diag, n, k, <npy_complex64*>a, lda, <npy_complex64*>x, incx)
|
| 400 |
+
|
| 401 |
+
|
| 402 |
+
cdef extern from "_blas_subroutines.h":
|
| 403 |
+
void _fortran_ctpmv "BLAS_FUNC(ctpmv)"(char *uplo, char *trans, char *diag, int *n, npy_complex64 *ap, npy_complex64 *x, int *incx) nogil
|
| 404 |
+
cdef void ctpmv(char *uplo, char *trans, char *diag, int *n, c *ap, c *x, int *incx) noexcept nogil:
|
| 405 |
+
|
| 406 |
+
_fortran_ctpmv(uplo, trans, diag, n, <npy_complex64*>ap, <npy_complex64*>x, incx)
|
| 407 |
+
|
| 408 |
+
|
| 409 |
+
cdef extern from "_blas_subroutines.h":
|
| 410 |
+
void _fortran_ctpsv "BLAS_FUNC(ctpsv)"(char *uplo, char *trans, char *diag, int *n, npy_complex64 *ap, npy_complex64 *x, int *incx) nogil
|
| 411 |
+
cdef void ctpsv(char *uplo, char *trans, char *diag, int *n, c *ap, c *x, int *incx) noexcept nogil:
|
| 412 |
+
|
| 413 |
+
_fortran_ctpsv(uplo, trans, diag, n, <npy_complex64*>ap, <npy_complex64*>x, incx)
|
| 414 |
+
|
| 415 |
+
|
| 416 |
+
cdef extern from "_blas_subroutines.h":
|
| 417 |
+
void _fortran_ctrmm "BLAS_FUNC(ctrmm)"(char *side, char *uplo, char *transa, char *diag, int *m, int *n, npy_complex64 *alpha, npy_complex64 *a, int *lda, npy_complex64 *b, int *ldb) nogil
|
| 418 |
+
cdef void ctrmm(char *side, char *uplo, char *transa, char *diag, int *m, int *n, c *alpha, c *a, int *lda, c *b, int *ldb) noexcept nogil:
|
| 419 |
+
|
| 420 |
+
_fortran_ctrmm(side, uplo, transa, diag, m, n, <npy_complex64*>alpha, <npy_complex64*>a, lda, <npy_complex64*>b, ldb)
|
| 421 |
+
|
| 422 |
+
|
| 423 |
+
cdef extern from "_blas_subroutines.h":
|
| 424 |
+
void _fortran_ctrmv "BLAS_FUNC(ctrmv)"(char *uplo, char *trans, char *diag, int *n, npy_complex64 *a, int *lda, npy_complex64 *x, int *incx) nogil
|
| 425 |
+
cdef void ctrmv(char *uplo, char *trans, char *diag, int *n, c *a, int *lda, c *x, int *incx) noexcept nogil:
|
| 426 |
+
|
| 427 |
+
_fortran_ctrmv(uplo, trans, diag, n, <npy_complex64*>a, lda, <npy_complex64*>x, incx)
|
| 428 |
+
|
| 429 |
+
|
| 430 |
+
cdef extern from "_blas_subroutines.h":
|
| 431 |
+
void _fortran_ctrsm "BLAS_FUNC(ctrsm)"(char *side, char *uplo, char *transa, char *diag, int *m, int *n, npy_complex64 *alpha, npy_complex64 *a, int *lda, npy_complex64 *b, int *ldb) nogil
|
| 432 |
+
cdef void ctrsm(char *side, char *uplo, char *transa, char *diag, int *m, int *n, c *alpha, c *a, int *lda, c *b, int *ldb) noexcept nogil:
|
| 433 |
+
|
| 434 |
+
_fortran_ctrsm(side, uplo, transa, diag, m, n, <npy_complex64*>alpha, <npy_complex64*>a, lda, <npy_complex64*>b, ldb)
|
| 435 |
+
|
| 436 |
+
|
| 437 |
+
cdef extern from "_blas_subroutines.h":
|
| 438 |
+
void _fortran_ctrsv "BLAS_FUNC(ctrsv)"(char *uplo, char *trans, char *diag, int *n, npy_complex64 *a, int *lda, npy_complex64 *x, int *incx) nogil
|
| 439 |
+
cdef void ctrsv(char *uplo, char *trans, char *diag, int *n, c *a, int *lda, c *x, int *incx) noexcept nogil:
|
| 440 |
+
|
| 441 |
+
_fortran_ctrsv(uplo, trans, diag, n, <npy_complex64*>a, lda, <npy_complex64*>x, incx)
|
| 442 |
+
|
| 443 |
+
|
| 444 |
+
cdef extern from "_blas_subroutines.h":
|
| 445 |
+
d _fortran_dasum "BLAS_FUNC(dasum)"(int *n, d *dx, int *incx) nogil
|
| 446 |
+
cdef d dasum(int *n, d *dx, int *incx) noexcept nogil:
|
| 447 |
+
|
| 448 |
+
return _fortran_dasum(n, dx, incx)
|
| 449 |
+
|
| 450 |
+
|
| 451 |
+
cdef extern from "_blas_subroutines.h":
|
| 452 |
+
void _fortran_daxpy "BLAS_FUNC(daxpy)"(int *n, d *da, d *dx, int *incx, d *dy, int *incy) nogil
|
| 453 |
+
cdef void daxpy(int *n, d *da, d *dx, int *incx, d *dy, int *incy) noexcept nogil:
|
| 454 |
+
|
| 455 |
+
_fortran_daxpy(n, da, dx, incx, dy, incy)
|
| 456 |
+
|
| 457 |
+
|
| 458 |
+
cdef extern from "_blas_subroutines.h":
|
| 459 |
+
d _fortran_dcabs1 "BLAS_FUNC(dcabs1)"(npy_complex128 *z) nogil
|
| 460 |
+
cdef d dcabs1(z *z) noexcept nogil:
|
| 461 |
+
|
| 462 |
+
return _fortran_dcabs1(<npy_complex128*>z)
|
| 463 |
+
|
| 464 |
+
|
| 465 |
+
cdef extern from "_blas_subroutines.h":
|
| 466 |
+
void _fortran_dcopy "BLAS_FUNC(dcopy)"(int *n, d *dx, int *incx, d *dy, int *incy) nogil
|
| 467 |
+
cdef void dcopy(int *n, d *dx, int *incx, d *dy, int *incy) noexcept nogil:
|
| 468 |
+
|
| 469 |
+
_fortran_dcopy(n, dx, incx, dy, incy)
|
| 470 |
+
|
| 471 |
+
|
| 472 |
+
cdef extern from "_blas_subroutines.h":
|
| 473 |
+
d _fortran_ddot "BLAS_FUNC(ddot)"(int *n, d *dx, int *incx, d *dy, int *incy) nogil
|
| 474 |
+
cdef d ddot(int *n, d *dx, int *incx, d *dy, int *incy) noexcept nogil:
|
| 475 |
+
|
| 476 |
+
return _fortran_ddot(n, dx, incx, dy, incy)
|
| 477 |
+
|
| 478 |
+
|
| 479 |
+
cdef extern from "_blas_subroutines.h":
|
| 480 |
+
void _fortran_dgbmv "BLAS_FUNC(dgbmv)"(char *trans, int *m, int *n, int *kl, int *ku, d *alpha, d *a, int *lda, d *x, int *incx, d *beta, d *y, int *incy) nogil
|
| 481 |
+
cdef void dgbmv(char *trans, int *m, int *n, int *kl, int *ku, d *alpha, d *a, int *lda, d *x, int *incx, d *beta, d *y, int *incy) noexcept nogil:
|
| 482 |
+
|
| 483 |
+
_fortran_dgbmv(trans, m, n, kl, ku, alpha, a, lda, x, incx, beta, y, incy)
|
| 484 |
+
|
| 485 |
+
|
| 486 |
+
cdef extern from "_blas_subroutines.h":
|
| 487 |
+
void _fortran_dgemm "BLAS_FUNC(dgemm)"(char *transa, char *transb, int *m, int *n, int *k, d *alpha, d *a, int *lda, d *b, int *ldb, d *beta, d *c, int *ldc) nogil
|
| 488 |
+
cdef void dgemm(char *transa, char *transb, int *m, int *n, int *k, d *alpha, d *a, int *lda, d *b, int *ldb, d *beta, d *c, int *ldc) noexcept nogil:
|
| 489 |
+
|
| 490 |
+
_fortran_dgemm(transa, transb, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc)
|
| 491 |
+
|
| 492 |
+
|
| 493 |
+
cdef extern from "_blas_subroutines.h":
|
| 494 |
+
void _fortran_dgemv "BLAS_FUNC(dgemv)"(char *trans, int *m, int *n, d *alpha, d *a, int *lda, d *x, int *incx, d *beta, d *y, int *incy) nogil
|
| 495 |
+
cdef void dgemv(char *trans, int *m, int *n, d *alpha, d *a, int *lda, d *x, int *incx, d *beta, d *y, int *incy) noexcept nogil:
|
| 496 |
+
|
| 497 |
+
_fortran_dgemv(trans, m, n, alpha, a, lda, x, incx, beta, y, incy)
|
| 498 |
+
|
| 499 |
+
|
| 500 |
+
cdef extern from "_blas_subroutines.h":
|
| 501 |
+
void _fortran_dger "BLAS_FUNC(dger)"(int *m, int *n, d *alpha, d *x, int *incx, d *y, int *incy, d *a, int *lda) nogil
|
| 502 |
+
cdef void dger(int *m, int *n, d *alpha, d *x, int *incx, d *y, int *incy, d *a, int *lda) noexcept nogil:
|
| 503 |
+
|
| 504 |
+
_fortran_dger(m, n, alpha, x, incx, y, incy, a, lda)
|
| 505 |
+
|
| 506 |
+
|
| 507 |
+
cdef extern from "_blas_subroutines.h":
|
| 508 |
+
d _fortran_dnrm2 "BLAS_FUNC(dnrm2)"(int *n, d *x, int *incx) nogil
|
| 509 |
+
cdef d dnrm2(int *n, d *x, int *incx) noexcept nogil:
|
| 510 |
+
|
| 511 |
+
return _fortran_dnrm2(n, x, incx)
|
| 512 |
+
|
| 513 |
+
|
| 514 |
+
cdef extern from "_blas_subroutines.h":
|
| 515 |
+
void _fortran_drot "BLAS_FUNC(drot)"(int *n, d *dx, int *incx, d *dy, int *incy, d *c, d *s) nogil
|
| 516 |
+
cdef void drot(int *n, d *dx, int *incx, d *dy, int *incy, d *c, d *s) noexcept nogil:
|
| 517 |
+
|
| 518 |
+
_fortran_drot(n, dx, incx, dy, incy, c, s)
|
| 519 |
+
|
| 520 |
+
|
| 521 |
+
cdef extern from "_blas_subroutines.h":
|
| 522 |
+
void _fortran_drotg "BLAS_FUNC(drotg)"(d *da, d *db, d *c, d *s) nogil
|
| 523 |
+
cdef void drotg(d *da, d *db, d *c, d *s) noexcept nogil:
|
| 524 |
+
|
| 525 |
+
_fortran_drotg(da, db, c, s)
|
| 526 |
+
|
| 527 |
+
|
| 528 |
+
cdef extern from "_blas_subroutines.h":
|
| 529 |
+
void _fortran_drotm "BLAS_FUNC(drotm)"(int *n, d *dx, int *incx, d *dy, int *incy, d *dparam) nogil
|
| 530 |
+
cdef void drotm(int *n, d *dx, int *incx, d *dy, int *incy, d *dparam) noexcept nogil:
|
| 531 |
+
|
| 532 |
+
_fortran_drotm(n, dx, incx, dy, incy, dparam)
|
| 533 |
+
|
| 534 |
+
|
| 535 |
+
cdef extern from "_blas_subroutines.h":
|
| 536 |
+
void _fortran_drotmg "BLAS_FUNC(drotmg)"(d *dd1, d *dd2, d *dx1, d *dy1, d *dparam) nogil
|
| 537 |
+
cdef void drotmg(d *dd1, d *dd2, d *dx1, d *dy1, d *dparam) noexcept nogil:
|
| 538 |
+
|
| 539 |
+
_fortran_drotmg(dd1, dd2, dx1, dy1, dparam)
|
| 540 |
+
|
| 541 |
+
|
| 542 |
+
cdef extern from "_blas_subroutines.h":
|
| 543 |
+
void _fortran_dsbmv "BLAS_FUNC(dsbmv)"(char *uplo, int *n, int *k, d *alpha, d *a, int *lda, d *x, int *incx, d *beta, d *y, int *incy) nogil
|
| 544 |
+
cdef void dsbmv(char *uplo, int *n, int *k, d *alpha, d *a, int *lda, d *x, int *incx, d *beta, d *y, int *incy) noexcept nogil:
|
| 545 |
+
|
| 546 |
+
_fortran_dsbmv(uplo, n, k, alpha, a, lda, x, incx, beta, y, incy)
|
| 547 |
+
|
| 548 |
+
|
| 549 |
+
cdef extern from "_blas_subroutines.h":
|
| 550 |
+
void _fortran_dscal "BLAS_FUNC(dscal)"(int *n, d *da, d *dx, int *incx) nogil
|
| 551 |
+
cdef void dscal(int *n, d *da, d *dx, int *incx) noexcept nogil:
|
| 552 |
+
|
| 553 |
+
_fortran_dscal(n, da, dx, incx)
|
| 554 |
+
|
| 555 |
+
|
| 556 |
+
cdef extern from "_blas_subroutines.h":
|
| 557 |
+
d _fortran_dsdot "BLAS_FUNC(dsdot)"(int *n, s *sx, int *incx, s *sy, int *incy) nogil
|
| 558 |
+
cdef d dsdot(int *n, s *sx, int *incx, s *sy, int *incy) noexcept nogil:
|
| 559 |
+
|
| 560 |
+
return _fortran_dsdot(n, sx, incx, sy, incy)
|
| 561 |
+
|
| 562 |
+
|
| 563 |
+
cdef extern from "_blas_subroutines.h":
|
| 564 |
+
void _fortran_dspmv "BLAS_FUNC(dspmv)"(char *uplo, int *n, d *alpha, d *ap, d *x, int *incx, d *beta, d *y, int *incy) nogil
|
| 565 |
+
cdef void dspmv(char *uplo, int *n, d *alpha, d *ap, d *x, int *incx, d *beta, d *y, int *incy) noexcept nogil:
|
| 566 |
+
|
| 567 |
+
_fortran_dspmv(uplo, n, alpha, ap, x, incx, beta, y, incy)
|
| 568 |
+
|
| 569 |
+
|
| 570 |
+
cdef extern from "_blas_subroutines.h":
|
| 571 |
+
void _fortran_dspr "BLAS_FUNC(dspr)"(char *uplo, int *n, d *alpha, d *x, int *incx, d *ap) nogil
|
| 572 |
+
cdef void dspr(char *uplo, int *n, d *alpha, d *x, int *incx, d *ap) noexcept nogil:
|
| 573 |
+
|
| 574 |
+
_fortran_dspr(uplo, n, alpha, x, incx, ap)
|
| 575 |
+
|
| 576 |
+
|
| 577 |
+
cdef extern from "_blas_subroutines.h":
|
| 578 |
+
void _fortran_dspr2 "BLAS_FUNC(dspr2)"(char *uplo, int *n, d *alpha, d *x, int *incx, d *y, int *incy, d *ap) nogil
|
| 579 |
+
cdef void dspr2(char *uplo, int *n, d *alpha, d *x, int *incx, d *y, int *incy, d *ap) noexcept nogil:
|
| 580 |
+
|
| 581 |
+
_fortran_dspr2(uplo, n, alpha, x, incx, y, incy, ap)
|
| 582 |
+
|
| 583 |
+
|
| 584 |
+
cdef extern from "_blas_subroutines.h":
|
| 585 |
+
void _fortran_dswap "BLAS_FUNC(dswap)"(int *n, d *dx, int *incx, d *dy, int *incy) nogil
|
| 586 |
+
cdef void dswap(int *n, d *dx, int *incx, d *dy, int *incy) noexcept nogil:
|
| 587 |
+
|
| 588 |
+
_fortran_dswap(n, dx, incx, dy, incy)
|
| 589 |
+
|
| 590 |
+
|
| 591 |
+
cdef extern from "_blas_subroutines.h":
|
| 592 |
+
void _fortran_dsymm "BLAS_FUNC(dsymm)"(char *side, char *uplo, int *m, int *n, d *alpha, d *a, int *lda, d *b, int *ldb, d *beta, d *c, int *ldc) nogil
|
| 593 |
+
cdef void dsymm(char *side, char *uplo, int *m, int *n, d *alpha, d *a, int *lda, d *b, int *ldb, d *beta, d *c, int *ldc) noexcept nogil:
|
| 594 |
+
|
| 595 |
+
_fortran_dsymm(side, uplo, m, n, alpha, a, lda, b, ldb, beta, c, ldc)
|
| 596 |
+
|
| 597 |
+
|
| 598 |
+
cdef extern from "_blas_subroutines.h":
|
| 599 |
+
void _fortran_dsymv "BLAS_FUNC(dsymv)"(char *uplo, int *n, d *alpha, d *a, int *lda, d *x, int *incx, d *beta, d *y, int *incy) nogil
|
| 600 |
+
cdef void dsymv(char *uplo, int *n, d *alpha, d *a, int *lda, d *x, int *incx, d *beta, d *y, int *incy) noexcept nogil:
|
| 601 |
+
|
| 602 |
+
_fortran_dsymv(uplo, n, alpha, a, lda, x, incx, beta, y, incy)
|
| 603 |
+
|
| 604 |
+
|
| 605 |
+
cdef extern from "_blas_subroutines.h":
|
| 606 |
+
void _fortran_dsyr "BLAS_FUNC(dsyr)"(char *uplo, int *n, d *alpha, d *x, int *incx, d *a, int *lda) nogil
|
| 607 |
+
cdef void dsyr(char *uplo, int *n, d *alpha, d *x, int *incx, d *a, int *lda) noexcept nogil:
|
| 608 |
+
|
| 609 |
+
_fortran_dsyr(uplo, n, alpha, x, incx, a, lda)
|
| 610 |
+
|
| 611 |
+
|
| 612 |
+
cdef extern from "_blas_subroutines.h":
|
| 613 |
+
void _fortran_dsyr2 "BLAS_FUNC(dsyr2)"(char *uplo, int *n, d *alpha, d *x, int *incx, d *y, int *incy, d *a, int *lda) nogil
|
| 614 |
+
cdef void dsyr2(char *uplo, int *n, d *alpha, d *x, int *incx, d *y, int *incy, d *a, int *lda) noexcept nogil:
|
| 615 |
+
|
| 616 |
+
_fortran_dsyr2(uplo, n, alpha, x, incx, y, incy, a, lda)
|
| 617 |
+
|
| 618 |
+
|
| 619 |
+
cdef extern from "_blas_subroutines.h":
|
| 620 |
+
void _fortran_dsyr2k "BLAS_FUNC(dsyr2k)"(char *uplo, char *trans, int *n, int *k, d *alpha, d *a, int *lda, d *b, int *ldb, d *beta, d *c, int *ldc) nogil
|
| 621 |
+
cdef void dsyr2k(char *uplo, char *trans, int *n, int *k, d *alpha, d *a, int *lda, d *b, int *ldb, d *beta, d *c, int *ldc) noexcept nogil:
|
| 622 |
+
|
| 623 |
+
_fortran_dsyr2k(uplo, trans, n, k, alpha, a, lda, b, ldb, beta, c, ldc)
|
| 624 |
+
|
| 625 |
+
|
| 626 |
+
cdef extern from "_blas_subroutines.h":
|
| 627 |
+
void _fortran_dsyrk "BLAS_FUNC(dsyrk)"(char *uplo, char *trans, int *n, int *k, d *alpha, d *a, int *lda, d *beta, d *c, int *ldc) nogil
|
| 628 |
+
cdef void dsyrk(char *uplo, char *trans, int *n, int *k, d *alpha, d *a, int *lda, d *beta, d *c, int *ldc) noexcept nogil:
|
| 629 |
+
|
| 630 |
+
_fortran_dsyrk(uplo, trans, n, k, alpha, a, lda, beta, c, ldc)
|
| 631 |
+
|
| 632 |
+
|
| 633 |
+
cdef extern from "_blas_subroutines.h":
|
| 634 |
+
void _fortran_dtbmv "BLAS_FUNC(dtbmv)"(char *uplo, char *trans, char *diag, int *n, int *k, d *a, int *lda, d *x, int *incx) nogil
|
| 635 |
+
cdef void dtbmv(char *uplo, char *trans, char *diag, int *n, int *k, d *a, int *lda, d *x, int *incx) noexcept nogil:
|
| 636 |
+
|
| 637 |
+
_fortran_dtbmv(uplo, trans, diag, n, k, a, lda, x, incx)
|
| 638 |
+
|
| 639 |
+
|
| 640 |
+
cdef extern from "_blas_subroutines.h":
|
| 641 |
+
void _fortran_dtbsv "BLAS_FUNC(dtbsv)"(char *uplo, char *trans, char *diag, int *n, int *k, d *a, int *lda, d *x, int *incx) nogil
|
| 642 |
+
cdef void dtbsv(char *uplo, char *trans, char *diag, int *n, int *k, d *a, int *lda, d *x, int *incx) noexcept nogil:
|
| 643 |
+
|
| 644 |
+
_fortran_dtbsv(uplo, trans, diag, n, k, a, lda, x, incx)
|
| 645 |
+
|
| 646 |
+
|
| 647 |
+
cdef extern from "_blas_subroutines.h":
|
| 648 |
+
void _fortran_dtpmv "BLAS_FUNC(dtpmv)"(char *uplo, char *trans, char *diag, int *n, d *ap, d *x, int *incx) nogil
|
| 649 |
+
cdef void dtpmv(char *uplo, char *trans, char *diag, int *n, d *ap, d *x, int *incx) noexcept nogil:
|
| 650 |
+
|
| 651 |
+
_fortran_dtpmv(uplo, trans, diag, n, ap, x, incx)
|
| 652 |
+
|
| 653 |
+
|
| 654 |
+
cdef extern from "_blas_subroutines.h":
|
| 655 |
+
void _fortran_dtpsv "BLAS_FUNC(dtpsv)"(char *uplo, char *trans, char *diag, int *n, d *ap, d *x, int *incx) nogil
|
| 656 |
+
cdef void dtpsv(char *uplo, char *trans, char *diag, int *n, d *ap, d *x, int *incx) noexcept nogil:
|
| 657 |
+
|
| 658 |
+
_fortran_dtpsv(uplo, trans, diag, n, ap, x, incx)
|
| 659 |
+
|
| 660 |
+
|
| 661 |
+
cdef extern from "_blas_subroutines.h":
|
| 662 |
+
void _fortran_dtrmm "BLAS_FUNC(dtrmm)"(char *side, char *uplo, char *transa, char *diag, int *m, int *n, d *alpha, d *a, int *lda, d *b, int *ldb) nogil
|
| 663 |
+
cdef void dtrmm(char *side, char *uplo, char *transa, char *diag, int *m, int *n, d *alpha, d *a, int *lda, d *b, int *ldb) noexcept nogil:
|
| 664 |
+
|
| 665 |
+
_fortran_dtrmm(side, uplo, transa, diag, m, n, alpha, a, lda, b, ldb)
|
| 666 |
+
|
| 667 |
+
|
| 668 |
+
cdef extern from "_blas_subroutines.h":
|
| 669 |
+
void _fortran_dtrmv "BLAS_FUNC(dtrmv)"(char *uplo, char *trans, char *diag, int *n, d *a, int *lda, d *x, int *incx) nogil
|
| 670 |
+
cdef void dtrmv(char *uplo, char *trans, char *diag, int *n, d *a, int *lda, d *x, int *incx) noexcept nogil:
|
| 671 |
+
|
| 672 |
+
_fortran_dtrmv(uplo, trans, diag, n, a, lda, x, incx)
|
| 673 |
+
|
| 674 |
+
|
| 675 |
+
cdef extern from "_blas_subroutines.h":
|
| 676 |
+
void _fortran_dtrsm "BLAS_FUNC(dtrsm)"(char *side, char *uplo, char *transa, char *diag, int *m, int *n, d *alpha, d *a, int *lda, d *b, int *ldb) nogil
|
| 677 |
+
cdef void dtrsm(char *side, char *uplo, char *transa, char *diag, int *m, int *n, d *alpha, d *a, int *lda, d *b, int *ldb) noexcept nogil:
|
| 678 |
+
|
| 679 |
+
_fortran_dtrsm(side, uplo, transa, diag, m, n, alpha, a, lda, b, ldb)
|
| 680 |
+
|
| 681 |
+
|
| 682 |
+
cdef extern from "_blas_subroutines.h":
|
| 683 |
+
void _fortran_dtrsv "BLAS_FUNC(dtrsv)"(char *uplo, char *trans, char *diag, int *n, d *a, int *lda, d *x, int *incx) nogil
|
| 684 |
+
cdef void dtrsv(char *uplo, char *trans, char *diag, int *n, d *a, int *lda, d *x, int *incx) noexcept nogil:
|
| 685 |
+
|
| 686 |
+
_fortran_dtrsv(uplo, trans, diag, n, a, lda, x, incx)
|
| 687 |
+
|
| 688 |
+
|
| 689 |
+
cdef extern from "_blas_subroutines.h":
|
| 690 |
+
d _fortran_dzasum "BLAS_FUNC(dzasum)"(int *n, npy_complex128 *zx, int *incx) nogil
|
| 691 |
+
cdef d dzasum(int *n, z *zx, int *incx) noexcept nogil:
|
| 692 |
+
|
| 693 |
+
return _fortran_dzasum(n, <npy_complex128*>zx, incx)
|
| 694 |
+
|
| 695 |
+
|
| 696 |
+
cdef extern from "_blas_subroutines.h":
|
| 697 |
+
d _fortran_dznrm2 "BLAS_FUNC(dznrm2)"(int *n, npy_complex128 *x, int *incx) nogil
|
| 698 |
+
cdef d dznrm2(int *n, z *x, int *incx) noexcept nogil:
|
| 699 |
+
|
| 700 |
+
return _fortran_dznrm2(n, <npy_complex128*>x, incx)
|
| 701 |
+
|
| 702 |
+
|
| 703 |
+
cdef extern from "_blas_subroutines.h":
|
| 704 |
+
int _fortran_icamax "BLAS_FUNC(icamax)"(int *n, npy_complex64 *cx, int *incx) nogil
|
| 705 |
+
cdef int icamax(int *n, c *cx, int *incx) noexcept nogil:
|
| 706 |
+
|
| 707 |
+
return _fortran_icamax(n, <npy_complex64*>cx, incx)
|
| 708 |
+
|
| 709 |
+
|
| 710 |
+
cdef extern from "_blas_subroutines.h":
|
| 711 |
+
int _fortran_idamax "BLAS_FUNC(idamax)"(int *n, d *dx, int *incx) nogil
|
| 712 |
+
cdef int idamax(int *n, d *dx, int *incx) noexcept nogil:
|
| 713 |
+
|
| 714 |
+
return _fortran_idamax(n, dx, incx)
|
| 715 |
+
|
| 716 |
+
|
| 717 |
+
cdef extern from "_blas_subroutines.h":
|
| 718 |
+
int _fortran_isamax "BLAS_FUNC(isamax)"(int *n, s *sx, int *incx) nogil
|
| 719 |
+
cdef int isamax(int *n, s *sx, int *incx) noexcept nogil:
|
| 720 |
+
|
| 721 |
+
return _fortran_isamax(n, sx, incx)
|
| 722 |
+
|
| 723 |
+
|
| 724 |
+
cdef extern from "_blas_subroutines.h":
|
| 725 |
+
int _fortran_izamax "BLAS_FUNC(izamax)"(int *n, npy_complex128 *zx, int *incx) nogil
|
| 726 |
+
cdef int izamax(int *n, z *zx, int *incx) noexcept nogil:
|
| 727 |
+
|
| 728 |
+
return _fortran_izamax(n, <npy_complex128*>zx, incx)
|
| 729 |
+
|
| 730 |
+
|
| 731 |
+
cdef extern from "_blas_subroutines.h":
|
| 732 |
+
bint _fortran_lsame "BLAS_FUNC(lsame)"(char *ca, char *cb) nogil
|
| 733 |
+
cdef bint lsame(char *ca, char *cb) noexcept nogil:
|
| 734 |
+
|
| 735 |
+
return _fortran_lsame(ca, cb)
|
| 736 |
+
|
| 737 |
+
|
| 738 |
+
cdef extern from "_blas_subroutines.h":
|
| 739 |
+
s _fortran_sasum "BLAS_FUNC(sasum)"(int *n, s *sx, int *incx) nogil
|
| 740 |
+
cdef s sasum(int *n, s *sx, int *incx) noexcept nogil:
|
| 741 |
+
|
| 742 |
+
return _fortran_sasum(n, sx, incx)
|
| 743 |
+
|
| 744 |
+
|
| 745 |
+
cdef extern from "_blas_subroutines.h":
|
| 746 |
+
void _fortran_saxpy "BLAS_FUNC(saxpy)"(int *n, s *sa, s *sx, int *incx, s *sy, int *incy) nogil
|
| 747 |
+
cdef void saxpy(int *n, s *sa, s *sx, int *incx, s *sy, int *incy) noexcept nogil:
|
| 748 |
+
|
| 749 |
+
_fortran_saxpy(n, sa, sx, incx, sy, incy)
|
| 750 |
+
|
| 751 |
+
|
| 752 |
+
cdef extern from "_blas_subroutines.h":
|
| 753 |
+
s _fortran_scasum "BLAS_FUNC(scasum)"(int *n, npy_complex64 *cx, int *incx) nogil
|
| 754 |
+
cdef s scasum(int *n, c *cx, int *incx) noexcept nogil:
|
| 755 |
+
|
| 756 |
+
return _fortran_scasum(n, <npy_complex64*>cx, incx)
|
| 757 |
+
|
| 758 |
+
|
| 759 |
+
cdef extern from "_blas_subroutines.h":
|
| 760 |
+
s _fortran_scnrm2 "BLAS_FUNC(scnrm2)"(int *n, npy_complex64 *x, int *incx) nogil
|
| 761 |
+
cdef s scnrm2(int *n, c *x, int *incx) noexcept nogil:
|
| 762 |
+
|
| 763 |
+
return _fortran_scnrm2(n, <npy_complex64*>x, incx)
|
| 764 |
+
|
| 765 |
+
|
| 766 |
+
cdef extern from "_blas_subroutines.h":
|
| 767 |
+
void _fortran_scopy "BLAS_FUNC(scopy)"(int *n, s *sx, int *incx, s *sy, int *incy) nogil
|
| 768 |
+
cdef void scopy(int *n, s *sx, int *incx, s *sy, int *incy) noexcept nogil:
|
| 769 |
+
|
| 770 |
+
_fortran_scopy(n, sx, incx, sy, incy)
|
| 771 |
+
|
| 772 |
+
|
| 773 |
+
cdef extern from "_blas_subroutines.h":
|
| 774 |
+
s _fortran_sdot "BLAS_FUNC(sdot)"(int *n, s *sx, int *incx, s *sy, int *incy) nogil
|
| 775 |
+
cdef s sdot(int *n, s *sx, int *incx, s *sy, int *incy) noexcept nogil:
|
| 776 |
+
|
| 777 |
+
return _fortran_sdot(n, sx, incx, sy, incy)
|
| 778 |
+
|
| 779 |
+
|
| 780 |
+
cdef extern from "_blas_subroutines.h":
|
| 781 |
+
s _fortran_sdsdot "BLAS_FUNC(sdsdot)"(int *n, s *sb, s *sx, int *incx, s *sy, int *incy) nogil
|
| 782 |
+
cdef s sdsdot(int *n, s *sb, s *sx, int *incx, s *sy, int *incy) noexcept nogil:
|
| 783 |
+
|
| 784 |
+
return _fortran_sdsdot(n, sb, sx, incx, sy, incy)
|
| 785 |
+
|
| 786 |
+
|
| 787 |
+
cdef extern from "_blas_subroutines.h":
|
| 788 |
+
void _fortran_sgbmv "BLAS_FUNC(sgbmv)"(char *trans, int *m, int *n, int *kl, int *ku, s *alpha, s *a, int *lda, s *x, int *incx, s *beta, s *y, int *incy) nogil
|
| 789 |
+
cdef void sgbmv(char *trans, int *m, int *n, int *kl, int *ku, s *alpha, s *a, int *lda, s *x, int *incx, s *beta, s *y, int *incy) noexcept nogil:
|
| 790 |
+
|
| 791 |
+
_fortran_sgbmv(trans, m, n, kl, ku, alpha, a, lda, x, incx, beta, y, incy)
|
| 792 |
+
|
| 793 |
+
|
| 794 |
+
cdef extern from "_blas_subroutines.h":
|
| 795 |
+
void _fortran_sgemm "BLAS_FUNC(sgemm)"(char *transa, char *transb, int *m, int *n, int *k, s *alpha, s *a, int *lda, s *b, int *ldb, s *beta, s *c, int *ldc) nogil
|
| 796 |
+
cdef void sgemm(char *transa, char *transb, int *m, int *n, int *k, s *alpha, s *a, int *lda, s *b, int *ldb, s *beta, s *c, int *ldc) noexcept nogil:
|
| 797 |
+
|
| 798 |
+
_fortran_sgemm(transa, transb, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc)
|
| 799 |
+
|
| 800 |
+
|
| 801 |
+
cdef extern from "_blas_subroutines.h":
|
| 802 |
+
void _fortran_sgemv "BLAS_FUNC(sgemv)"(char *trans, int *m, int *n, s *alpha, s *a, int *lda, s *x, int *incx, s *beta, s *y, int *incy) nogil
|
| 803 |
+
cdef void sgemv(char *trans, int *m, int *n, s *alpha, s *a, int *lda, s *x, int *incx, s *beta, s *y, int *incy) noexcept nogil:
|
| 804 |
+
|
| 805 |
+
_fortran_sgemv(trans, m, n, alpha, a, lda, x, incx, beta, y, incy)
|
| 806 |
+
|
| 807 |
+
|
| 808 |
+
cdef extern from "_blas_subroutines.h":
|
| 809 |
+
void _fortran_sger "BLAS_FUNC(sger)"(int *m, int *n, s *alpha, s *x, int *incx, s *y, int *incy, s *a, int *lda) nogil
|
| 810 |
+
cdef void sger(int *m, int *n, s *alpha, s *x, int *incx, s *y, int *incy, s *a, int *lda) noexcept nogil:
|
| 811 |
+
|
| 812 |
+
_fortran_sger(m, n, alpha, x, incx, y, incy, a, lda)
|
| 813 |
+
|
| 814 |
+
|
| 815 |
+
cdef extern from "_blas_subroutines.h":
|
| 816 |
+
s _fortran_snrm2 "BLAS_FUNC(snrm2)"(int *n, s *x, int *incx) nogil
|
| 817 |
+
cdef s snrm2(int *n, s *x, int *incx) noexcept nogil:
|
| 818 |
+
|
| 819 |
+
return _fortran_snrm2(n, x, incx)
|
| 820 |
+
|
| 821 |
+
|
| 822 |
+
cdef extern from "_blas_subroutines.h":
|
| 823 |
+
void _fortran_srot "BLAS_FUNC(srot)"(int *n, s *sx, int *incx, s *sy, int *incy, s *c, s *s) nogil
|
| 824 |
+
cdef void srot(int *n, s *sx, int *incx, s *sy, int *incy, s *c, s *s) noexcept nogil:
|
| 825 |
+
|
| 826 |
+
_fortran_srot(n, sx, incx, sy, incy, c, s)
|
| 827 |
+
|
| 828 |
+
|
| 829 |
+
cdef extern from "_blas_subroutines.h":
|
| 830 |
+
void _fortran_srotg "BLAS_FUNC(srotg)"(s *sa, s *sb, s *c, s *s) nogil
|
| 831 |
+
cdef void srotg(s *sa, s *sb, s *c, s *s) noexcept nogil:
|
| 832 |
+
|
| 833 |
+
_fortran_srotg(sa, sb, c, s)
|
| 834 |
+
|
| 835 |
+
|
| 836 |
+
cdef extern from "_blas_subroutines.h":
|
| 837 |
+
void _fortran_srotm "BLAS_FUNC(srotm)"(int *n, s *sx, int *incx, s *sy, int *incy, s *sparam) nogil
|
| 838 |
+
cdef void srotm(int *n, s *sx, int *incx, s *sy, int *incy, s *sparam) noexcept nogil:
|
| 839 |
+
|
| 840 |
+
_fortran_srotm(n, sx, incx, sy, incy, sparam)
|
| 841 |
+
|
| 842 |
+
|
| 843 |
+
cdef extern from "_blas_subroutines.h":
|
| 844 |
+
void _fortran_srotmg "BLAS_FUNC(srotmg)"(s *sd1, s *sd2, s *sx1, s *sy1, s *sparam) nogil
|
| 845 |
+
cdef void srotmg(s *sd1, s *sd2, s *sx1, s *sy1, s *sparam) noexcept nogil:
|
| 846 |
+
|
| 847 |
+
_fortran_srotmg(sd1, sd2, sx1, sy1, sparam)
|
| 848 |
+
|
| 849 |
+
|
| 850 |
+
cdef extern from "_blas_subroutines.h":
|
| 851 |
+
void _fortran_ssbmv "BLAS_FUNC(ssbmv)"(char *uplo, int *n, int *k, s *alpha, s *a, int *lda, s *x, int *incx, s *beta, s *y, int *incy) nogil
|
| 852 |
+
cdef void ssbmv(char *uplo, int *n, int *k, s *alpha, s *a, int *lda, s *x, int *incx, s *beta, s *y, int *incy) noexcept nogil:
|
| 853 |
+
|
| 854 |
+
_fortran_ssbmv(uplo, n, k, alpha, a, lda, x, incx, beta, y, incy)
|
| 855 |
+
|
| 856 |
+
|
| 857 |
+
cdef extern from "_blas_subroutines.h":
|
| 858 |
+
void _fortran_sscal "BLAS_FUNC(sscal)"(int *n, s *sa, s *sx, int *incx) nogil
|
| 859 |
+
cdef void sscal(int *n, s *sa, s *sx, int *incx) noexcept nogil:
|
| 860 |
+
|
| 861 |
+
_fortran_sscal(n, sa, sx, incx)
|
| 862 |
+
|
| 863 |
+
|
| 864 |
+
cdef extern from "_blas_subroutines.h":
|
| 865 |
+
void _fortran_sspmv "BLAS_FUNC(sspmv)"(char *uplo, int *n, s *alpha, s *ap, s *x, int *incx, s *beta, s *y, int *incy) nogil
|
| 866 |
+
cdef void sspmv(char *uplo, int *n, s *alpha, s *ap, s *x, int *incx, s *beta, s *y, int *incy) noexcept nogil:
|
| 867 |
+
|
| 868 |
+
_fortran_sspmv(uplo, n, alpha, ap, x, incx, beta, y, incy)
|
| 869 |
+
|
| 870 |
+
|
| 871 |
+
cdef extern from "_blas_subroutines.h":
|
| 872 |
+
void _fortran_sspr "BLAS_FUNC(sspr)"(char *uplo, int *n, s *alpha, s *x, int *incx, s *ap) nogil
|
| 873 |
+
cdef void sspr(char *uplo, int *n, s *alpha, s *x, int *incx, s *ap) noexcept nogil:
|
| 874 |
+
|
| 875 |
+
_fortran_sspr(uplo, n, alpha, x, incx, ap)
|
| 876 |
+
|
| 877 |
+
|
| 878 |
+
cdef extern from "_blas_subroutines.h":
|
| 879 |
+
void _fortran_sspr2 "BLAS_FUNC(sspr2)"(char *uplo, int *n, s *alpha, s *x, int *incx, s *y, int *incy, s *ap) nogil
|
| 880 |
+
cdef void sspr2(char *uplo, int *n, s *alpha, s *x, int *incx, s *y, int *incy, s *ap) noexcept nogil:
|
| 881 |
+
|
| 882 |
+
_fortran_sspr2(uplo, n, alpha, x, incx, y, incy, ap)
|
| 883 |
+
|
| 884 |
+
|
| 885 |
+
cdef extern from "_blas_subroutines.h":
|
| 886 |
+
void _fortran_sswap "BLAS_FUNC(sswap)"(int *n, s *sx, int *incx, s *sy, int *incy) nogil
|
| 887 |
+
cdef void sswap(int *n, s *sx, int *incx, s *sy, int *incy) noexcept nogil:
|
| 888 |
+
|
| 889 |
+
_fortran_sswap(n, sx, incx, sy, incy)
|
| 890 |
+
|
| 891 |
+
|
| 892 |
+
cdef extern from "_blas_subroutines.h":
|
| 893 |
+
void _fortran_ssymm "BLAS_FUNC(ssymm)"(char *side, char *uplo, int *m, int *n, s *alpha, s *a, int *lda, s *b, int *ldb, s *beta, s *c, int *ldc) nogil
|
| 894 |
+
cdef void ssymm(char *side, char *uplo, int *m, int *n, s *alpha, s *a, int *lda, s *b, int *ldb, s *beta, s *c, int *ldc) noexcept nogil:
|
| 895 |
+
|
| 896 |
+
_fortran_ssymm(side, uplo, m, n, alpha, a, lda, b, ldb, beta, c, ldc)
|
| 897 |
+
|
| 898 |
+
|
| 899 |
+
cdef extern from "_blas_subroutines.h":
|
| 900 |
+
void _fortran_ssymv "BLAS_FUNC(ssymv)"(char *uplo, int *n, s *alpha, s *a, int *lda, s *x, int *incx, s *beta, s *y, int *incy) nogil
|
| 901 |
+
cdef void ssymv(char *uplo, int *n, s *alpha, s *a, int *lda, s *x, int *incx, s *beta, s *y, int *incy) noexcept nogil:
|
| 902 |
+
|
| 903 |
+
_fortran_ssymv(uplo, n, alpha, a, lda, x, incx, beta, y, incy)
|
| 904 |
+
|
| 905 |
+
|
| 906 |
+
cdef extern from "_blas_subroutines.h":
|
| 907 |
+
void _fortran_ssyr "BLAS_FUNC(ssyr)"(char *uplo, int *n, s *alpha, s *x, int *incx, s *a, int *lda) nogil
|
| 908 |
+
cdef void ssyr(char *uplo, int *n, s *alpha, s *x, int *incx, s *a, int *lda) noexcept nogil:
|
| 909 |
+
|
| 910 |
+
_fortran_ssyr(uplo, n, alpha, x, incx, a, lda)
|
| 911 |
+
|
| 912 |
+
|
| 913 |
+
cdef extern from "_blas_subroutines.h":
|
| 914 |
+
void _fortran_ssyr2 "BLAS_FUNC(ssyr2)"(char *uplo, int *n, s *alpha, s *x, int *incx, s *y, int *incy, s *a, int *lda) nogil
|
| 915 |
+
cdef void ssyr2(char *uplo, int *n, s *alpha, s *x, int *incx, s *y, int *incy, s *a, int *lda) noexcept nogil:
|
| 916 |
+
|
| 917 |
+
_fortran_ssyr2(uplo, n, alpha, x, incx, y, incy, a, lda)
|
| 918 |
+
|
| 919 |
+
|
| 920 |
+
cdef extern from "_blas_subroutines.h":
|
| 921 |
+
void _fortran_ssyr2k "BLAS_FUNC(ssyr2k)"(char *uplo, char *trans, int *n, int *k, s *alpha, s *a, int *lda, s *b, int *ldb, s *beta, s *c, int *ldc) nogil
|
| 922 |
+
cdef void ssyr2k(char *uplo, char *trans, int *n, int *k, s *alpha, s *a, int *lda, s *b, int *ldb, s *beta, s *c, int *ldc) noexcept nogil:
|
| 923 |
+
|
| 924 |
+
_fortran_ssyr2k(uplo, trans, n, k, alpha, a, lda, b, ldb, beta, c, ldc)
|
| 925 |
+
|
| 926 |
+
|
| 927 |
+
cdef extern from "_blas_subroutines.h":
|
| 928 |
+
void _fortran_ssyrk "BLAS_FUNC(ssyrk)"(char *uplo, char *trans, int *n, int *k, s *alpha, s *a, int *lda, s *beta, s *c, int *ldc) nogil
|
| 929 |
+
cdef void ssyrk(char *uplo, char *trans, int *n, int *k, s *alpha, s *a, int *lda, s *beta, s *c, int *ldc) noexcept nogil:
|
| 930 |
+
|
| 931 |
+
_fortran_ssyrk(uplo, trans, n, k, alpha, a, lda, beta, c, ldc)
|
| 932 |
+
|
| 933 |
+
|
| 934 |
+
cdef extern from "_blas_subroutines.h":
|
| 935 |
+
void _fortran_stbmv "BLAS_FUNC(stbmv)"(char *uplo, char *trans, char *diag, int *n, int *k, s *a, int *lda, s *x, int *incx) nogil
|
| 936 |
+
cdef void stbmv(char *uplo, char *trans, char *diag, int *n, int *k, s *a, int *lda, s *x, int *incx) noexcept nogil:
|
| 937 |
+
|
| 938 |
+
_fortran_stbmv(uplo, trans, diag, n, k, a, lda, x, incx)
|
| 939 |
+
|
| 940 |
+
|
| 941 |
+
cdef extern from "_blas_subroutines.h":
|
| 942 |
+
void _fortran_stbsv "BLAS_FUNC(stbsv)"(char *uplo, char *trans, char *diag, int *n, int *k, s *a, int *lda, s *x, int *incx) nogil
|
| 943 |
+
cdef void stbsv(char *uplo, char *trans, char *diag, int *n, int *k, s *a, int *lda, s *x, int *incx) noexcept nogil:
|
| 944 |
+
|
| 945 |
+
_fortran_stbsv(uplo, trans, diag, n, k, a, lda, x, incx)
|
| 946 |
+
|
| 947 |
+
|
| 948 |
+
cdef extern from "_blas_subroutines.h":
|
| 949 |
+
void _fortran_stpmv "BLAS_FUNC(stpmv)"(char *uplo, char *trans, char *diag, int *n, s *ap, s *x, int *incx) nogil
|
| 950 |
+
cdef void stpmv(char *uplo, char *trans, char *diag, int *n, s *ap, s *x, int *incx) noexcept nogil:
|
| 951 |
+
|
| 952 |
+
_fortran_stpmv(uplo, trans, diag, n, ap, x, incx)
|
| 953 |
+
|
| 954 |
+
|
| 955 |
+
cdef extern from "_blas_subroutines.h":
|
| 956 |
+
void _fortran_stpsv "BLAS_FUNC(stpsv)"(char *uplo, char *trans, char *diag, int *n, s *ap, s *x, int *incx) nogil
|
| 957 |
+
cdef void stpsv(char *uplo, char *trans, char *diag, int *n, s *ap, s *x, int *incx) noexcept nogil:
|
| 958 |
+
|
| 959 |
+
_fortran_stpsv(uplo, trans, diag, n, ap, x, incx)
|
| 960 |
+
|
| 961 |
+
|
| 962 |
+
cdef extern from "_blas_subroutines.h":
|
| 963 |
+
void _fortran_strmm "BLAS_FUNC(strmm)"(char *side, char *uplo, char *transa, char *diag, int *m, int *n, s *alpha, s *a, int *lda, s *b, int *ldb) nogil
|
| 964 |
+
cdef void strmm(char *side, char *uplo, char *transa, char *diag, int *m, int *n, s *alpha, s *a, int *lda, s *b, int *ldb) noexcept nogil:
|
| 965 |
+
|
| 966 |
+
_fortran_strmm(side, uplo, transa, diag, m, n, alpha, a, lda, b, ldb)
|
| 967 |
+
|
| 968 |
+
|
| 969 |
+
cdef extern from "_blas_subroutines.h":
|
| 970 |
+
void _fortran_strmv "BLAS_FUNC(strmv)"(char *uplo, char *trans, char *diag, int *n, s *a, int *lda, s *x, int *incx) nogil
|
| 971 |
+
cdef void strmv(char *uplo, char *trans, char *diag, int *n, s *a, int *lda, s *x, int *incx) noexcept nogil:
|
| 972 |
+
|
| 973 |
+
_fortran_strmv(uplo, trans, diag, n, a, lda, x, incx)
|
| 974 |
+
|
| 975 |
+
|
| 976 |
+
cdef extern from "_blas_subroutines.h":
|
| 977 |
+
void _fortran_strsm "BLAS_FUNC(strsm)"(char *side, char *uplo, char *transa, char *diag, int *m, int *n, s *alpha, s *a, int *lda, s *b, int *ldb) nogil
|
| 978 |
+
cdef void strsm(char *side, char *uplo, char *transa, char *diag, int *m, int *n, s *alpha, s *a, int *lda, s *b, int *ldb) noexcept nogil:
|
| 979 |
+
|
| 980 |
+
_fortran_strsm(side, uplo, transa, diag, m, n, alpha, a, lda, b, ldb)
|
| 981 |
+
|
| 982 |
+
|
| 983 |
+
cdef extern from "_blas_subroutines.h":
|
| 984 |
+
void _fortran_strsv "BLAS_FUNC(strsv)"(char *uplo, char *trans, char *diag, int *n, s *a, int *lda, s *x, int *incx) nogil
|
| 985 |
+
cdef void strsv(char *uplo, char *trans, char *diag, int *n, s *a, int *lda, s *x, int *incx) noexcept nogil:
|
| 986 |
+
|
| 987 |
+
_fortran_strsv(uplo, trans, diag, n, a, lda, x, incx)
|
| 988 |
+
|
| 989 |
+
|
| 990 |
+
cdef extern from "_blas_subroutines.h":
|
| 991 |
+
void _fortran_zaxpy "BLAS_FUNC(zaxpy)"(int *n, npy_complex128 *za, npy_complex128 *zx, int *incx, npy_complex128 *zy, int *incy) nogil
|
| 992 |
+
cdef void zaxpy(int *n, z *za, z *zx, int *incx, z *zy, int *incy) noexcept nogil:
|
| 993 |
+
|
| 994 |
+
_fortran_zaxpy(n, <npy_complex128*>za, <npy_complex128*>zx, incx, <npy_complex128*>zy, incy)
|
| 995 |
+
|
| 996 |
+
|
| 997 |
+
cdef extern from "_blas_subroutines.h":
|
| 998 |
+
void _fortran_zcopy "BLAS_FUNC(zcopy)"(int *n, npy_complex128 *zx, int *incx, npy_complex128 *zy, int *incy) nogil
|
| 999 |
+
cdef void zcopy(int *n, z *zx, int *incx, z *zy, int *incy) noexcept nogil:
|
| 1000 |
+
|
| 1001 |
+
_fortran_zcopy(n, <npy_complex128*>zx, incx, <npy_complex128*>zy, incy)
|
| 1002 |
+
|
| 1003 |
+
|
| 1004 |
+
cdef extern from "_blas_subroutines.h":
|
| 1005 |
+
void _fortran_zdotc "F_FUNC(zdotcwrp,ZDOTCWRP)"(npy_complex128 *out, int *n, npy_complex128 *zx, int *incx, npy_complex128 *zy, int *incy) nogil
|
| 1006 |
+
cdef z zdotc(int *n, z *zx, int *incx, z *zy, int *incy) noexcept nogil:
|
| 1007 |
+
cdef z out
|
| 1008 |
+
_fortran_zdotc(<npy_complex128*>&out, n, <npy_complex128*>zx, incx, <npy_complex128*>zy, incy)
|
| 1009 |
+
return out
|
| 1010 |
+
|
| 1011 |
+
cdef extern from "_blas_subroutines.h":
|
| 1012 |
+
void _fortran_zdotu "F_FUNC(zdotuwrp,ZDOTUWRP)"(npy_complex128 *out, int *n, npy_complex128 *zx, int *incx, npy_complex128 *zy, int *incy) nogil
|
| 1013 |
+
cdef z zdotu(int *n, z *zx, int *incx, z *zy, int *incy) noexcept nogil:
|
| 1014 |
+
cdef z out
|
| 1015 |
+
_fortran_zdotu(<npy_complex128*>&out, n, <npy_complex128*>zx, incx, <npy_complex128*>zy, incy)
|
| 1016 |
+
return out
|
| 1017 |
+
|
| 1018 |
+
cdef extern from "_blas_subroutines.h":
|
| 1019 |
+
void _fortran_zdrot "BLAS_FUNC(zdrot)"(int *n, npy_complex128 *cx, int *incx, npy_complex128 *cy, int *incy, d *c, d *s) nogil
|
| 1020 |
+
cdef void zdrot(int *n, z *cx, int *incx, z *cy, int *incy, d *c, d *s) noexcept nogil:
|
| 1021 |
+
|
| 1022 |
+
_fortran_zdrot(n, <npy_complex128*>cx, incx, <npy_complex128*>cy, incy, c, s)
|
| 1023 |
+
|
| 1024 |
+
|
| 1025 |
+
cdef extern from "_blas_subroutines.h":
|
| 1026 |
+
void _fortran_zdscal "BLAS_FUNC(zdscal)"(int *n, d *da, npy_complex128 *zx, int *incx) nogil
|
| 1027 |
+
cdef void zdscal(int *n, d *da, z *zx, int *incx) noexcept nogil:
|
| 1028 |
+
|
| 1029 |
+
_fortran_zdscal(n, da, <npy_complex128*>zx, incx)
|
| 1030 |
+
|
| 1031 |
+
|
| 1032 |
+
cdef extern from "_blas_subroutines.h":
|
| 1033 |
+
void _fortran_zgbmv "BLAS_FUNC(zgbmv)"(char *trans, int *m, int *n, int *kl, int *ku, npy_complex128 *alpha, npy_complex128 *a, int *lda, npy_complex128 *x, int *incx, npy_complex128 *beta, npy_complex128 *y, int *incy) nogil
|
| 1034 |
+
cdef void zgbmv(char *trans, int *m, int *n, int *kl, int *ku, z *alpha, z *a, int *lda, z *x, int *incx, z *beta, z *y, int *incy) noexcept nogil:
|
| 1035 |
+
|
| 1036 |
+
_fortran_zgbmv(trans, m, n, kl, ku, <npy_complex128*>alpha, <npy_complex128*>a, lda, <npy_complex128*>x, incx, <npy_complex128*>beta, <npy_complex128*>y, incy)
|
| 1037 |
+
|
| 1038 |
+
|
| 1039 |
+
cdef extern from "_blas_subroutines.h":
|
| 1040 |
+
void _fortran_zgemm "BLAS_FUNC(zgemm)"(char *transa, char *transb, int *m, int *n, int *k, npy_complex128 *alpha, npy_complex128 *a, int *lda, npy_complex128 *b, int *ldb, npy_complex128 *beta, npy_complex128 *c, int *ldc) nogil
|
| 1041 |
+
cdef void zgemm(char *transa, char *transb, int *m, int *n, int *k, z *alpha, z *a, int *lda, z *b, int *ldb, z *beta, z *c, int *ldc) noexcept nogil:
|
| 1042 |
+
|
| 1043 |
+
_fortran_zgemm(transa, transb, m, n, k, <npy_complex128*>alpha, <npy_complex128*>a, lda, <npy_complex128*>b, ldb, <npy_complex128*>beta, <npy_complex128*>c, ldc)
|
| 1044 |
+
|
| 1045 |
+
|
| 1046 |
+
cdef extern from "_blas_subroutines.h":
|
| 1047 |
+
void _fortran_zgemv "BLAS_FUNC(zgemv)"(char *trans, int *m, int *n, npy_complex128 *alpha, npy_complex128 *a, int *lda, npy_complex128 *x, int *incx, npy_complex128 *beta, npy_complex128 *y, int *incy) nogil
|
| 1048 |
+
cdef void zgemv(char *trans, int *m, int *n, z *alpha, z *a, int *lda, z *x, int *incx, z *beta, z *y, int *incy) noexcept nogil:
|
| 1049 |
+
|
| 1050 |
+
_fortran_zgemv(trans, m, n, <npy_complex128*>alpha, <npy_complex128*>a, lda, <npy_complex128*>x, incx, <npy_complex128*>beta, <npy_complex128*>y, incy)
|
| 1051 |
+
|
| 1052 |
+
|
| 1053 |
+
cdef extern from "_blas_subroutines.h":
|
| 1054 |
+
void _fortran_zgerc "BLAS_FUNC(zgerc)"(int *m, int *n, npy_complex128 *alpha, npy_complex128 *x, int *incx, npy_complex128 *y, int *incy, npy_complex128 *a, int *lda) nogil
|
| 1055 |
+
cdef void zgerc(int *m, int *n, z *alpha, z *x, int *incx, z *y, int *incy, z *a, int *lda) noexcept nogil:
|
| 1056 |
+
|
| 1057 |
+
_fortran_zgerc(m, n, <npy_complex128*>alpha, <npy_complex128*>x, incx, <npy_complex128*>y, incy, <npy_complex128*>a, lda)
|
| 1058 |
+
|
| 1059 |
+
|
| 1060 |
+
cdef extern from "_blas_subroutines.h":
|
| 1061 |
+
void _fortran_zgeru "BLAS_FUNC(zgeru)"(int *m, int *n, npy_complex128 *alpha, npy_complex128 *x, int *incx, npy_complex128 *y, int *incy, npy_complex128 *a, int *lda) nogil
|
| 1062 |
+
cdef void zgeru(int *m, int *n, z *alpha, z *x, int *incx, z *y, int *incy, z *a, int *lda) noexcept nogil:
|
| 1063 |
+
|
| 1064 |
+
_fortran_zgeru(m, n, <npy_complex128*>alpha, <npy_complex128*>x, incx, <npy_complex128*>y, incy, <npy_complex128*>a, lda)
|
| 1065 |
+
|
| 1066 |
+
|
| 1067 |
+
cdef extern from "_blas_subroutines.h":
|
| 1068 |
+
void _fortran_zhbmv "BLAS_FUNC(zhbmv)"(char *uplo, int *n, int *k, npy_complex128 *alpha, npy_complex128 *a, int *lda, npy_complex128 *x, int *incx, npy_complex128 *beta, npy_complex128 *y, int *incy) nogil
|
| 1069 |
+
cdef void zhbmv(char *uplo, int *n, int *k, z *alpha, z *a, int *lda, z *x, int *incx, z *beta, z *y, int *incy) noexcept nogil:
|
| 1070 |
+
|
| 1071 |
+
_fortran_zhbmv(uplo, n, k, <npy_complex128*>alpha, <npy_complex128*>a, lda, <npy_complex128*>x, incx, <npy_complex128*>beta, <npy_complex128*>y, incy)
|
| 1072 |
+
|
| 1073 |
+
|
| 1074 |
+
cdef extern from "_blas_subroutines.h":
|
| 1075 |
+
void _fortran_zhemm "BLAS_FUNC(zhemm)"(char *side, char *uplo, int *m, int *n, npy_complex128 *alpha, npy_complex128 *a, int *lda, npy_complex128 *b, int *ldb, npy_complex128 *beta, npy_complex128 *c, int *ldc) nogil
|
| 1076 |
+
cdef void zhemm(char *side, char *uplo, int *m, int *n, z *alpha, z *a, int *lda, z *b, int *ldb, z *beta, z *c, int *ldc) noexcept nogil:
|
| 1077 |
+
|
| 1078 |
+
_fortran_zhemm(side, uplo, m, n, <npy_complex128*>alpha, <npy_complex128*>a, lda, <npy_complex128*>b, ldb, <npy_complex128*>beta, <npy_complex128*>c, ldc)
|
| 1079 |
+
|
| 1080 |
+
|
| 1081 |
+
cdef extern from "_blas_subroutines.h":
|
| 1082 |
+
void _fortran_zhemv "BLAS_FUNC(zhemv)"(char *uplo, int *n, npy_complex128 *alpha, npy_complex128 *a, int *lda, npy_complex128 *x, int *incx, npy_complex128 *beta, npy_complex128 *y, int *incy) nogil
|
| 1083 |
+
cdef void zhemv(char *uplo, int *n, z *alpha, z *a, int *lda, z *x, int *incx, z *beta, z *y, int *incy) noexcept nogil:
|
| 1084 |
+
|
| 1085 |
+
_fortran_zhemv(uplo, n, <npy_complex128*>alpha, <npy_complex128*>a, lda, <npy_complex128*>x, incx, <npy_complex128*>beta, <npy_complex128*>y, incy)
|
| 1086 |
+
|
| 1087 |
+
|
| 1088 |
+
cdef extern from "_blas_subroutines.h":
|
| 1089 |
+
void _fortran_zher "BLAS_FUNC(zher)"(char *uplo, int *n, d *alpha, npy_complex128 *x, int *incx, npy_complex128 *a, int *lda) nogil
|
| 1090 |
+
cdef void zher(char *uplo, int *n, d *alpha, z *x, int *incx, z *a, int *lda) noexcept nogil:
|
| 1091 |
+
|
| 1092 |
+
_fortran_zher(uplo, n, alpha, <npy_complex128*>x, incx, <npy_complex128*>a, lda)
|
| 1093 |
+
|
| 1094 |
+
|
| 1095 |
+
cdef extern from "_blas_subroutines.h":
|
| 1096 |
+
void _fortran_zher2 "BLAS_FUNC(zher2)"(char *uplo, int *n, npy_complex128 *alpha, npy_complex128 *x, int *incx, npy_complex128 *y, int *incy, npy_complex128 *a, int *lda) nogil
|
| 1097 |
+
cdef void zher2(char *uplo, int *n, z *alpha, z *x, int *incx, z *y, int *incy, z *a, int *lda) noexcept nogil:
|
| 1098 |
+
|
| 1099 |
+
_fortran_zher2(uplo, n, <npy_complex128*>alpha, <npy_complex128*>x, incx, <npy_complex128*>y, incy, <npy_complex128*>a, lda)
|
| 1100 |
+
|
| 1101 |
+
|
| 1102 |
+
cdef extern from "_blas_subroutines.h":
|
| 1103 |
+
void _fortran_zher2k "BLAS_FUNC(zher2k)"(char *uplo, char *trans, int *n, int *k, npy_complex128 *alpha, npy_complex128 *a, int *lda, npy_complex128 *b, int *ldb, d *beta, npy_complex128 *c, int *ldc) nogil
|
| 1104 |
+
cdef void zher2k(char *uplo, char *trans, int *n, int *k, z *alpha, z *a, int *lda, z *b, int *ldb, d *beta, z *c, int *ldc) noexcept nogil:
|
| 1105 |
+
|
| 1106 |
+
_fortran_zher2k(uplo, trans, n, k, <npy_complex128*>alpha, <npy_complex128*>a, lda, <npy_complex128*>b, ldb, beta, <npy_complex128*>c, ldc)
|
| 1107 |
+
|
| 1108 |
+
|
| 1109 |
+
cdef extern from "_blas_subroutines.h":
|
| 1110 |
+
void _fortran_zherk "BLAS_FUNC(zherk)"(char *uplo, char *trans, int *n, int *k, d *alpha, npy_complex128 *a, int *lda, d *beta, npy_complex128 *c, int *ldc) nogil
|
| 1111 |
+
cdef void zherk(char *uplo, char *trans, int *n, int *k, d *alpha, z *a, int *lda, d *beta, z *c, int *ldc) noexcept nogil:
|
| 1112 |
+
|
| 1113 |
+
_fortran_zherk(uplo, trans, n, k, alpha, <npy_complex128*>a, lda, beta, <npy_complex128*>c, ldc)
|
| 1114 |
+
|
| 1115 |
+
|
| 1116 |
+
cdef extern from "_blas_subroutines.h":
|
| 1117 |
+
void _fortran_zhpmv "BLAS_FUNC(zhpmv)"(char *uplo, int *n, npy_complex128 *alpha, npy_complex128 *ap, npy_complex128 *x, int *incx, npy_complex128 *beta, npy_complex128 *y, int *incy) nogil
|
| 1118 |
+
cdef void zhpmv(char *uplo, int *n, z *alpha, z *ap, z *x, int *incx, z *beta, z *y, int *incy) noexcept nogil:
|
| 1119 |
+
|
| 1120 |
+
_fortran_zhpmv(uplo, n, <npy_complex128*>alpha, <npy_complex128*>ap, <npy_complex128*>x, incx, <npy_complex128*>beta, <npy_complex128*>y, incy)
|
| 1121 |
+
|
| 1122 |
+
|
| 1123 |
+
cdef extern from "_blas_subroutines.h":
|
| 1124 |
+
void _fortran_zhpr "BLAS_FUNC(zhpr)"(char *uplo, int *n, d *alpha, npy_complex128 *x, int *incx, npy_complex128 *ap) nogil
|
| 1125 |
+
cdef void zhpr(char *uplo, int *n, d *alpha, z *x, int *incx, z *ap) noexcept nogil:
|
| 1126 |
+
|
| 1127 |
+
_fortran_zhpr(uplo, n, alpha, <npy_complex128*>x, incx, <npy_complex128*>ap)
|
| 1128 |
+
|
| 1129 |
+
|
| 1130 |
+
cdef extern from "_blas_subroutines.h":
|
| 1131 |
+
void _fortran_zhpr2 "BLAS_FUNC(zhpr2)"(char *uplo, int *n, npy_complex128 *alpha, npy_complex128 *x, int *incx, npy_complex128 *y, int *incy, npy_complex128 *ap) nogil
|
| 1132 |
+
cdef void zhpr2(char *uplo, int *n, z *alpha, z *x, int *incx, z *y, int *incy, z *ap) noexcept nogil:
|
| 1133 |
+
|
| 1134 |
+
_fortran_zhpr2(uplo, n, <npy_complex128*>alpha, <npy_complex128*>x, incx, <npy_complex128*>y, incy, <npy_complex128*>ap)
|
| 1135 |
+
|
| 1136 |
+
|
| 1137 |
+
cdef extern from "_blas_subroutines.h":
|
| 1138 |
+
void _fortran_zrotg "BLAS_FUNC(zrotg)"(npy_complex128 *ca, npy_complex128 *cb, d *c, npy_complex128 *s) nogil
|
| 1139 |
+
cdef void zrotg(z *ca, z *cb, d *c, z *s) noexcept nogil:
|
| 1140 |
+
|
| 1141 |
+
_fortran_zrotg(<npy_complex128*>ca, <npy_complex128*>cb, c, <npy_complex128*>s)
|
| 1142 |
+
|
| 1143 |
+
|
| 1144 |
+
cdef extern from "_blas_subroutines.h":
|
| 1145 |
+
void _fortran_zscal "BLAS_FUNC(zscal)"(int *n, npy_complex128 *za, npy_complex128 *zx, int *incx) nogil
|
| 1146 |
+
cdef void zscal(int *n, z *za, z *zx, int *incx) noexcept nogil:
|
| 1147 |
+
|
| 1148 |
+
_fortran_zscal(n, <npy_complex128*>za, <npy_complex128*>zx, incx)
|
| 1149 |
+
|
| 1150 |
+
|
| 1151 |
+
cdef extern from "_blas_subroutines.h":
|
| 1152 |
+
void _fortran_zswap "BLAS_FUNC(zswap)"(int *n, npy_complex128 *zx, int *incx, npy_complex128 *zy, int *incy) nogil
|
| 1153 |
+
cdef void zswap(int *n, z *zx, int *incx, z *zy, int *incy) noexcept nogil:
|
| 1154 |
+
|
| 1155 |
+
_fortran_zswap(n, <npy_complex128*>zx, incx, <npy_complex128*>zy, incy)
|
| 1156 |
+
|
| 1157 |
+
|
| 1158 |
+
cdef extern from "_blas_subroutines.h":
|
| 1159 |
+
void _fortran_zsymm "BLAS_FUNC(zsymm)"(char *side, char *uplo, int *m, int *n, npy_complex128 *alpha, npy_complex128 *a, int *lda, npy_complex128 *b, int *ldb, npy_complex128 *beta, npy_complex128 *c, int *ldc) nogil
|
| 1160 |
+
cdef void zsymm(char *side, char *uplo, int *m, int *n, z *alpha, z *a, int *lda, z *b, int *ldb, z *beta, z *c, int *ldc) noexcept nogil:
|
| 1161 |
+
|
| 1162 |
+
_fortran_zsymm(side, uplo, m, n, <npy_complex128*>alpha, <npy_complex128*>a, lda, <npy_complex128*>b, ldb, <npy_complex128*>beta, <npy_complex128*>c, ldc)
|
| 1163 |
+
|
| 1164 |
+
|
| 1165 |
+
cdef extern from "_blas_subroutines.h":
|
| 1166 |
+
void _fortran_zsyr2k "BLAS_FUNC(zsyr2k)"(char *uplo, char *trans, int *n, int *k, npy_complex128 *alpha, npy_complex128 *a, int *lda, npy_complex128 *b, int *ldb, npy_complex128 *beta, npy_complex128 *c, int *ldc) nogil
|
| 1167 |
+
cdef void zsyr2k(char *uplo, char *trans, int *n, int *k, z *alpha, z *a, int *lda, z *b, int *ldb, z *beta, z *c, int *ldc) noexcept nogil:
|
| 1168 |
+
|
| 1169 |
+
_fortran_zsyr2k(uplo, trans, n, k, <npy_complex128*>alpha, <npy_complex128*>a, lda, <npy_complex128*>b, ldb, <npy_complex128*>beta, <npy_complex128*>c, ldc)
|
| 1170 |
+
|
| 1171 |
+
|
| 1172 |
+
cdef extern from "_blas_subroutines.h":
|
| 1173 |
+
void _fortran_zsyrk "BLAS_FUNC(zsyrk)"(char *uplo, char *trans, int *n, int *k, npy_complex128 *alpha, npy_complex128 *a, int *lda, npy_complex128 *beta, npy_complex128 *c, int *ldc) nogil
|
| 1174 |
+
cdef void zsyrk(char *uplo, char *trans, int *n, int *k, z *alpha, z *a, int *lda, z *beta, z *c, int *ldc) noexcept nogil:
|
| 1175 |
+
|
| 1176 |
+
_fortran_zsyrk(uplo, trans, n, k, <npy_complex128*>alpha, <npy_complex128*>a, lda, <npy_complex128*>beta, <npy_complex128*>c, ldc)
|
| 1177 |
+
|
| 1178 |
+
|
| 1179 |
+
cdef extern from "_blas_subroutines.h":
|
| 1180 |
+
void _fortran_ztbmv "BLAS_FUNC(ztbmv)"(char *uplo, char *trans, char *diag, int *n, int *k, npy_complex128 *a, int *lda, npy_complex128 *x, int *incx) nogil
|
| 1181 |
+
cdef void ztbmv(char *uplo, char *trans, char *diag, int *n, int *k, z *a, int *lda, z *x, int *incx) noexcept nogil:
|
| 1182 |
+
|
| 1183 |
+
_fortran_ztbmv(uplo, trans, diag, n, k, <npy_complex128*>a, lda, <npy_complex128*>x, incx)
|
| 1184 |
+
|
| 1185 |
+
|
| 1186 |
+
cdef extern from "_blas_subroutines.h":
|
| 1187 |
+
void _fortran_ztbsv "BLAS_FUNC(ztbsv)"(char *uplo, char *trans, char *diag, int *n, int *k, npy_complex128 *a, int *lda, npy_complex128 *x, int *incx) nogil
|
| 1188 |
+
cdef void ztbsv(char *uplo, char *trans, char *diag, int *n, int *k, z *a, int *lda, z *x, int *incx) noexcept nogil:
|
| 1189 |
+
|
| 1190 |
+
_fortran_ztbsv(uplo, trans, diag, n, k, <npy_complex128*>a, lda, <npy_complex128*>x, incx)
|
| 1191 |
+
|
| 1192 |
+
|
| 1193 |
+
cdef extern from "_blas_subroutines.h":
|
| 1194 |
+
void _fortran_ztpmv "BLAS_FUNC(ztpmv)"(char *uplo, char *trans, char *diag, int *n, npy_complex128 *ap, npy_complex128 *x, int *incx) nogil
|
| 1195 |
+
cdef void ztpmv(char *uplo, char *trans, char *diag, int *n, z *ap, z *x, int *incx) noexcept nogil:
|
| 1196 |
+
|
| 1197 |
+
_fortran_ztpmv(uplo, trans, diag, n, <npy_complex128*>ap, <npy_complex128*>x, incx)
|
| 1198 |
+
|
| 1199 |
+
|
| 1200 |
+
cdef extern from "_blas_subroutines.h":
|
| 1201 |
+
void _fortran_ztpsv "BLAS_FUNC(ztpsv)"(char *uplo, char *trans, char *diag, int *n, npy_complex128 *ap, npy_complex128 *x, int *incx) nogil
|
| 1202 |
+
cdef void ztpsv(char *uplo, char *trans, char *diag, int *n, z *ap, z *x, int *incx) noexcept nogil:
|
| 1203 |
+
|
| 1204 |
+
_fortran_ztpsv(uplo, trans, diag, n, <npy_complex128*>ap, <npy_complex128*>x, incx)
|
| 1205 |
+
|
| 1206 |
+
|
| 1207 |
+
cdef extern from "_blas_subroutines.h":
|
| 1208 |
+
void _fortran_ztrmm "BLAS_FUNC(ztrmm)"(char *side, char *uplo, char *transa, char *diag, int *m, int *n, npy_complex128 *alpha, npy_complex128 *a, int *lda, npy_complex128 *b, int *ldb) nogil
|
| 1209 |
+
cdef void ztrmm(char *side, char *uplo, char *transa, char *diag, int *m, int *n, z *alpha, z *a, int *lda, z *b, int *ldb) noexcept nogil:
|
| 1210 |
+
|
| 1211 |
+
_fortran_ztrmm(side, uplo, transa, diag, m, n, <npy_complex128*>alpha, <npy_complex128*>a, lda, <npy_complex128*>b, ldb)
|
| 1212 |
+
|
| 1213 |
+
|
| 1214 |
+
cdef extern from "_blas_subroutines.h":
|
| 1215 |
+
void _fortran_ztrmv "BLAS_FUNC(ztrmv)"(char *uplo, char *trans, char *diag, int *n, npy_complex128 *a, int *lda, npy_complex128 *x, int *incx) nogil
|
| 1216 |
+
cdef void ztrmv(char *uplo, char *trans, char *diag, int *n, z *a, int *lda, z *x, int *incx) noexcept nogil:
|
| 1217 |
+
|
| 1218 |
+
_fortran_ztrmv(uplo, trans, diag, n, <npy_complex128*>a, lda, <npy_complex128*>x, incx)
|
| 1219 |
+
|
| 1220 |
+
|
| 1221 |
+
cdef extern from "_blas_subroutines.h":
|
| 1222 |
+
void _fortran_ztrsm "BLAS_FUNC(ztrsm)"(char *side, char *uplo, char *transa, char *diag, int *m, int *n, npy_complex128 *alpha, npy_complex128 *a, int *lda, npy_complex128 *b, int *ldb) nogil
|
| 1223 |
+
cdef void ztrsm(char *side, char *uplo, char *transa, char *diag, int *m, int *n, z *alpha, z *a, int *lda, z *b, int *ldb) noexcept nogil:
|
| 1224 |
+
|
| 1225 |
+
_fortran_ztrsm(side, uplo, transa, diag, m, n, <npy_complex128*>alpha, <npy_complex128*>a, lda, <npy_complex128*>b, ldb)
|
| 1226 |
+
|
| 1227 |
+
|
| 1228 |
+
cdef extern from "_blas_subroutines.h":
|
| 1229 |
+
void _fortran_ztrsv "BLAS_FUNC(ztrsv)"(char *uplo, char *trans, char *diag, int *n, npy_complex128 *a, int *lda, npy_complex128 *x, int *incx) nogil
|
| 1230 |
+
cdef void ztrsv(char *uplo, char *trans, char *diag, int *n, z *a, int *lda, z *x, int *incx) noexcept nogil:
|
| 1231 |
+
|
| 1232 |
+
_fortran_ztrsv(uplo, trans, diag, n, <npy_complex128*>a, lda, <npy_complex128*>x, incx)
|
| 1233 |
+
|
| 1234 |
+
|
| 1235 |
+
|
| 1236 |
+
# Python-accessible wrappers for testing:
|
| 1237 |
+
|
| 1238 |
+
cdef inline bint _is_contiguous(double[:,:] a, int axis) noexcept nogil:
|
| 1239 |
+
return (a.strides[axis] == sizeof(a[0,0]) or a.shape[axis] == 1)
|
| 1240 |
+
|
| 1241 |
+
cpdef float complex _test_cdotc(float complex[:] cx, float complex[:] cy) noexcept nogil:
|
| 1242 |
+
cdef:
|
| 1243 |
+
int n = cx.shape[0]
|
| 1244 |
+
int incx = cx.strides[0] // sizeof(cx[0])
|
| 1245 |
+
int incy = cy.strides[0] // sizeof(cy[0])
|
| 1246 |
+
return cdotc(&n, &cx[0], &incx, &cy[0], &incy)
|
| 1247 |
+
|
| 1248 |
+
cpdef float complex _test_cdotu(float complex[:] cx, float complex[:] cy) noexcept nogil:
|
| 1249 |
+
cdef:
|
| 1250 |
+
int n = cx.shape[0]
|
| 1251 |
+
int incx = cx.strides[0] // sizeof(cx[0])
|
| 1252 |
+
int incy = cy.strides[0] // sizeof(cy[0])
|
| 1253 |
+
return cdotu(&n, &cx[0], &incx, &cy[0], &incy)
|
| 1254 |
+
|
| 1255 |
+
cpdef double _test_dasum(double[:] dx) noexcept nogil:
|
| 1256 |
+
cdef:
|
| 1257 |
+
int n = dx.shape[0]
|
| 1258 |
+
int incx = dx.strides[0] // sizeof(dx[0])
|
| 1259 |
+
return dasum(&n, &dx[0], &incx)
|
| 1260 |
+
|
| 1261 |
+
cpdef double _test_ddot(double[:] dx, double[:] dy) noexcept nogil:
|
| 1262 |
+
cdef:
|
| 1263 |
+
int n = dx.shape[0]
|
| 1264 |
+
int incx = dx.strides[0] // sizeof(dx[0])
|
| 1265 |
+
int incy = dy.strides[0] // sizeof(dy[0])
|
| 1266 |
+
return ddot(&n, &dx[0], &incx, &dy[0], &incy)
|
| 1267 |
+
|
| 1268 |
+
cpdef int _test_dgemm(double alpha, double[:,:] a, double[:,:] b, double beta,
|
| 1269 |
+
double[:,:] c) except -1 nogil:
|
| 1270 |
+
cdef:
|
| 1271 |
+
char *transa
|
| 1272 |
+
char *transb
|
| 1273 |
+
int m, n, k, lda, ldb, ldc
|
| 1274 |
+
double *a0=&a[0,0]
|
| 1275 |
+
double *b0=&b[0,0]
|
| 1276 |
+
double *c0=&c[0,0]
|
| 1277 |
+
# In the case that c is C contiguous, swap a and b and
|
| 1278 |
+
# swap whether or not each of them is transposed.
|
| 1279 |
+
# This can be done because a.dot(b) = b.T.dot(a.T).T.
|
| 1280 |
+
if _is_contiguous(c, 1):
|
| 1281 |
+
if _is_contiguous(a, 1):
|
| 1282 |
+
transb = 'n'
|
| 1283 |
+
ldb = (&a[1,0]) - a0 if a.shape[0] > 1 else 1
|
| 1284 |
+
elif _is_contiguous(a, 0):
|
| 1285 |
+
transb = 't'
|
| 1286 |
+
ldb = (&a[0,1]) - a0 if a.shape[1] > 1 else 1
|
| 1287 |
+
else:
|
| 1288 |
+
with gil:
|
| 1289 |
+
raise ValueError("Input 'a' is neither C nor Fortran contiguous.")
|
| 1290 |
+
if _is_contiguous(b, 1):
|
| 1291 |
+
transa = 'n'
|
| 1292 |
+
lda = (&b[1,0]) - b0 if b.shape[0] > 1 else 1
|
| 1293 |
+
elif _is_contiguous(b, 0):
|
| 1294 |
+
transa = 't'
|
| 1295 |
+
lda = (&b[0,1]) - b0 if b.shape[1] > 1 else 1
|
| 1296 |
+
else:
|
| 1297 |
+
with gil:
|
| 1298 |
+
raise ValueError("Input 'b' is neither C nor Fortran contiguous.")
|
| 1299 |
+
k = b.shape[0]
|
| 1300 |
+
if k != a.shape[1]:
|
| 1301 |
+
with gil:
|
| 1302 |
+
raise ValueError("Shape mismatch in input arrays.")
|
| 1303 |
+
m = b.shape[1]
|
| 1304 |
+
n = a.shape[0]
|
| 1305 |
+
if n != c.shape[0] or m != c.shape[1]:
|
| 1306 |
+
with gil:
|
| 1307 |
+
raise ValueError("Output array does not have the correct shape.")
|
| 1308 |
+
ldc = (&c[1,0]) - c0 if c.shape[0] > 1 else 1
|
| 1309 |
+
dgemm(transa, transb, &m, &n, &k, &alpha, b0, &lda, a0,
|
| 1310 |
+
&ldb, &beta, c0, &ldc)
|
| 1311 |
+
elif _is_contiguous(c, 0):
|
| 1312 |
+
if _is_contiguous(a, 1):
|
| 1313 |
+
transa = 't'
|
| 1314 |
+
lda = (&a[1,0]) - a0 if a.shape[0] > 1 else 1
|
| 1315 |
+
elif _is_contiguous(a, 0):
|
| 1316 |
+
transa = 'n'
|
| 1317 |
+
lda = (&a[0,1]) - a0 if a.shape[1] > 1 else 1
|
| 1318 |
+
else:
|
| 1319 |
+
with gil:
|
| 1320 |
+
raise ValueError("Input 'a' is neither C nor Fortran contiguous.")
|
| 1321 |
+
if _is_contiguous(b, 1):
|
| 1322 |
+
transb = 't'
|
| 1323 |
+
ldb = (&b[1,0]) - b0 if b.shape[0] > 1 else 1
|
| 1324 |
+
elif _is_contiguous(b, 0):
|
| 1325 |
+
transb = 'n'
|
| 1326 |
+
ldb = (&b[0,1]) - b0 if b.shape[1] > 1 else 1
|
| 1327 |
+
else:
|
| 1328 |
+
with gil:
|
| 1329 |
+
raise ValueError("Input 'b' is neither C nor Fortran contiguous.")
|
| 1330 |
+
m = a.shape[0]
|
| 1331 |
+
k = a.shape[1]
|
| 1332 |
+
if k != b.shape[0]:
|
| 1333 |
+
with gil:
|
| 1334 |
+
raise ValueError("Shape mismatch in input arrays.")
|
| 1335 |
+
n = b.shape[1]
|
| 1336 |
+
if m != c.shape[0] or n != c.shape[1]:
|
| 1337 |
+
with gil:
|
| 1338 |
+
raise ValueError("Output array does not have the correct shape.")
|
| 1339 |
+
ldc = (&c[0,1]) - c0 if c.shape[1] > 1 else 1
|
| 1340 |
+
dgemm(transa, transb, &m, &n, &k, &alpha, a0, &lda, b0,
|
| 1341 |
+
&ldb, &beta, c0, &ldc)
|
| 1342 |
+
else:
|
| 1343 |
+
with gil:
|
| 1344 |
+
raise ValueError("Input 'c' is neither C nor Fortran contiguous.")
|
| 1345 |
+
return 0
|
| 1346 |
+
|
| 1347 |
+
cpdef double _test_dnrm2(double[:] x) noexcept nogil:
|
| 1348 |
+
cdef:
|
| 1349 |
+
int n = x.shape[0]
|
| 1350 |
+
int incx = x.strides[0] // sizeof(x[0])
|
| 1351 |
+
return dnrm2(&n, &x[0], &incx)
|
| 1352 |
+
|
| 1353 |
+
cpdef double _test_dzasum(double complex[:] zx) noexcept nogil:
|
| 1354 |
+
cdef:
|
| 1355 |
+
int n = zx.shape[0]
|
| 1356 |
+
int incx = zx.strides[0] // sizeof(zx[0])
|
| 1357 |
+
return dzasum(&n, &zx[0], &incx)
|
| 1358 |
+
|
| 1359 |
+
cpdef double _test_dznrm2(double complex[:] x) noexcept nogil:
|
| 1360 |
+
cdef:
|
| 1361 |
+
int n = x.shape[0]
|
| 1362 |
+
int incx = x.strides[0] // sizeof(x[0])
|
| 1363 |
+
return dznrm2(&n, &x[0], &incx)
|
| 1364 |
+
|
| 1365 |
+
cpdef int _test_icamax(float complex[:] cx) noexcept nogil:
|
| 1366 |
+
cdef:
|
| 1367 |
+
int n = cx.shape[0]
|
| 1368 |
+
int incx = cx.strides[0] // sizeof(cx[0])
|
| 1369 |
+
return icamax(&n, &cx[0], &incx)
|
| 1370 |
+
|
| 1371 |
+
cpdef int _test_idamax(double[:] dx) noexcept nogil:
|
| 1372 |
+
cdef:
|
| 1373 |
+
int n = dx.shape[0]
|
| 1374 |
+
int incx = dx.strides[0] // sizeof(dx[0])
|
| 1375 |
+
return idamax(&n, &dx[0], &incx)
|
| 1376 |
+
|
| 1377 |
+
cpdef int _test_isamax(float[:] sx) noexcept nogil:
|
| 1378 |
+
cdef:
|
| 1379 |
+
int n = sx.shape[0]
|
| 1380 |
+
int incx = sx.strides[0] // sizeof(sx[0])
|
| 1381 |
+
return isamax(&n, &sx[0], &incx)
|
| 1382 |
+
|
| 1383 |
+
cpdef int _test_izamax(double complex[:] zx) noexcept nogil:
|
| 1384 |
+
cdef:
|
| 1385 |
+
int n = zx.shape[0]
|
| 1386 |
+
int incx = zx.strides[0] // sizeof(zx[0])
|
| 1387 |
+
return izamax(&n, &zx[0], &incx)
|
| 1388 |
+
|
| 1389 |
+
cpdef float _test_sasum(float[:] sx) noexcept nogil:
|
| 1390 |
+
cdef:
|
| 1391 |
+
int n = sx.shape[0]
|
| 1392 |
+
int incx = sx.strides[0] // sizeof(sx[0])
|
| 1393 |
+
return sasum(&n, &sx[0], &incx)
|
| 1394 |
+
|
| 1395 |
+
cpdef float _test_scasum(float complex[:] cx) noexcept nogil:
|
| 1396 |
+
cdef:
|
| 1397 |
+
int n = cx.shape[0]
|
| 1398 |
+
int incx = cx.strides[0] // sizeof(cx[0])
|
| 1399 |
+
return scasum(&n, &cx[0], &incx)
|
| 1400 |
+
|
| 1401 |
+
cpdef float _test_scnrm2(float complex[:] x) noexcept nogil:
|
| 1402 |
+
cdef:
|
| 1403 |
+
int n = x.shape[0]
|
| 1404 |
+
int incx = x.strides[0] // sizeof(x[0])
|
| 1405 |
+
return scnrm2(&n, &x[0], &incx)
|
| 1406 |
+
|
| 1407 |
+
cpdef float _test_sdot(float[:] sx, float[:] sy) noexcept nogil:
|
| 1408 |
+
cdef:
|
| 1409 |
+
int n = sx.shape[0]
|
| 1410 |
+
int incx = sx.strides[0] // sizeof(sx[0])
|
| 1411 |
+
int incy = sy.strides[0] // sizeof(sy[0])
|
| 1412 |
+
return sdot(&n, &sx[0], &incx, &sy[0], &incy)
|
| 1413 |
+
|
| 1414 |
+
cpdef float _test_snrm2(float[:] x) noexcept nogil:
|
| 1415 |
+
cdef:
|
| 1416 |
+
int n = x.shape[0]
|
| 1417 |
+
int incx = x.strides[0] // sizeof(x[0])
|
| 1418 |
+
return snrm2(&n, &x[0], &incx)
|
| 1419 |
+
|
| 1420 |
+
cpdef double complex _test_zdotc(double complex[:] zx, double complex[:] zy) noexcept nogil:
|
| 1421 |
+
cdef:
|
| 1422 |
+
int n = zx.shape[0]
|
| 1423 |
+
int incx = zx.strides[0] // sizeof(zx[0])
|
| 1424 |
+
int incy = zy.strides[0] // sizeof(zy[0])
|
| 1425 |
+
return zdotc(&n, &zx[0], &incx, &zy[0], &incy)
|
| 1426 |
+
|
| 1427 |
+
cpdef double complex _test_zdotu(double complex[:] zx, double complex[:] zy) noexcept nogil:
|
| 1428 |
+
cdef:
|
| 1429 |
+
int n = zx.shape[0]
|
| 1430 |
+
int incx = zx.strides[0] // sizeof(zx[0])
|
| 1431 |
+
int incy = zy.strides[0] // sizeof(zy[0])
|
| 1432 |
+
return zdotu(&n, &zx[0], &incx, &zy[0], &incy)
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/cython_lapack.cpython-311-x86_64-linux-gnu.so
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:35543354a907edae5d694519dd8d81ffdccea1cc64b0f7b8bb0328d584f43544
|
| 3 |
+
size 879609
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/cython_lapack.pxd
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/cython_lapack.pyx
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/decomp.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This file is not meant for public use and will be removed in SciPy v2.0.0.
|
| 2 |
+
# Use the `scipy.linalg` namespace for importing the functions
|
| 3 |
+
# included below.
|
| 4 |
+
|
| 5 |
+
from scipy._lib.deprecation import _sub_module_deprecation
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
__all__ = [ # noqa: F822
|
| 9 |
+
'eig', 'eigvals', 'eigh', 'eigvalsh',
|
| 10 |
+
'eig_banded', 'eigvals_banded',
|
| 11 |
+
'eigh_tridiagonal', 'eigvalsh_tridiagonal', 'hessenberg', 'cdf2rdf',
|
| 12 |
+
'LinAlgError', 'norm', 'get_lapack_funcs'
|
| 13 |
+
]
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def __dir__():
|
| 17 |
+
return __all__
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def __getattr__(name):
|
| 21 |
+
return _sub_module_deprecation(sub_package="linalg", module="decomp",
|
| 22 |
+
private_modules=["_decomp"], all=__all__,
|
| 23 |
+
attribute=name)
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/decomp_cholesky.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This file is not meant for public use and will be removed in SciPy v2.0.0.
|
| 2 |
+
# Use the `scipy.linalg` namespace for importing the functions
|
| 3 |
+
# included below.
|
| 4 |
+
|
| 5 |
+
from scipy._lib.deprecation import _sub_module_deprecation
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
__all__ = [ # noqa: F822
|
| 9 |
+
'cholesky', 'cho_factor', 'cho_solve', 'cholesky_banded',
|
| 10 |
+
'cho_solve_banded', 'LinAlgError', 'get_lapack_funcs'
|
| 11 |
+
]
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def __dir__():
|
| 15 |
+
return __all__
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def __getattr__(name):
|
| 19 |
+
return _sub_module_deprecation(sub_package="linalg", module="decomp_cholesky",
|
| 20 |
+
private_modules=["_decomp_cholesky"], all=__all__,
|
| 21 |
+
attribute=name)
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/decomp_lu.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This file is not meant for public use and will be removed in SciPy v2.0.0.
|
| 2 |
+
# Use the `scipy.linalg` namespace for importing the functions
|
| 3 |
+
# included below.
|
| 4 |
+
|
| 5 |
+
from scipy._lib.deprecation import _sub_module_deprecation
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
__all__ = [ # noqa: F822
|
| 9 |
+
'lu', 'lu_solve', 'lu_factor',
|
| 10 |
+
'LinAlgWarning', 'get_lapack_funcs',
|
| 11 |
+
]
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def __dir__():
|
| 15 |
+
return __all__
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def __getattr__(name):
|
| 19 |
+
return _sub_module_deprecation(sub_package="linalg", module="decomp_lu",
|
| 20 |
+
private_modules=["_decomp_lu"], all=__all__,
|
| 21 |
+
attribute=name)
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/decomp_qr.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This file is not meant for public use and will be removed in SciPy v2.0.0.
|
| 2 |
+
# Use the `scipy.linalg` namespace for importing the functions
|
| 3 |
+
# included below.
|
| 4 |
+
|
| 5 |
+
from scipy._lib.deprecation import _sub_module_deprecation
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
__all__ = [ # noqa: F822
|
| 9 |
+
'qr', 'qr_multiply', 'rq', 'get_lapack_funcs'
|
| 10 |
+
]
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def __dir__():
|
| 14 |
+
return __all__
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def __getattr__(name):
|
| 18 |
+
return _sub_module_deprecation(sub_package="linalg", module="decomp_qr",
|
| 19 |
+
private_modules=["_decomp_qr"], all=__all__,
|
| 20 |
+
attribute=name)
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/decomp_schur.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This file is not meant for public use and will be removed in SciPy v2.0.0.
|
| 2 |
+
# Use the `scipy.linalg` namespace for importing the functions
|
| 3 |
+
# included below.
|
| 4 |
+
|
| 5 |
+
from scipy._lib.deprecation import _sub_module_deprecation
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
__all__ = [ # noqa: F822
|
| 9 |
+
'schur', 'rsf2csf', 'norm', 'LinAlgError', 'get_lapack_funcs', 'eigvals',
|
| 10 |
+
]
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def __dir__():
|
| 14 |
+
return __all__
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def __getattr__(name):
|
| 18 |
+
return _sub_module_deprecation(sub_package="linalg", module="decomp_schur",
|
| 19 |
+
private_modules=["_decomp_schur"], all=__all__,
|
| 20 |
+
attribute=name)
|
| 21 |
+
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/decomp_svd.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This file is not meant for public use and will be removed in SciPy v2.0.0.
|
| 2 |
+
# Use the `scipy.linalg` namespace for importing the functions
|
| 3 |
+
# included below.
|
| 4 |
+
|
| 5 |
+
from scipy._lib.deprecation import _sub_module_deprecation
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
__all__ = [ # noqa: F822
|
| 9 |
+
'svd', 'svdvals', 'diagsvd', 'orth', 'subspace_angles', 'null_space',
|
| 10 |
+
'LinAlgError', 'get_lapack_funcs'
|
| 11 |
+
]
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def __dir__():
|
| 15 |
+
return __all__
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def __getattr__(name):
|
| 19 |
+
return _sub_module_deprecation(sub_package="linalg", module="decomp_svd",
|
| 20 |
+
private_modules=["_decomp_svd"], all=__all__,
|
| 21 |
+
attribute=name)
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/interpolative.py
ADDED
|
@@ -0,0 +1,989 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ******************************************************************************
|
| 2 |
+
# Copyright (C) 2013 Kenneth L. Ho
|
| 3 |
+
#
|
| 4 |
+
# Redistribution and use in source and binary forms, with or without
|
| 5 |
+
# modification, are permitted provided that the following conditions are met:
|
| 6 |
+
#
|
| 7 |
+
# Redistributions of source code must retain the above copyright notice, this
|
| 8 |
+
# list of conditions and the following disclaimer. Redistributions in binary
|
| 9 |
+
# form must reproduce the above copyright notice, this list of conditions and
|
| 10 |
+
# the following disclaimer in the documentation and/or other materials
|
| 11 |
+
# provided with the distribution.
|
| 12 |
+
#
|
| 13 |
+
# None of the names of the copyright holders may be used to endorse or
|
| 14 |
+
# promote products derived from this software without specific prior written
|
| 15 |
+
# permission.
|
| 16 |
+
#
|
| 17 |
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
| 18 |
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
| 19 |
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
| 20 |
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
| 21 |
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
| 22 |
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
| 23 |
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
| 24 |
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
| 25 |
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
| 26 |
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
| 27 |
+
# POSSIBILITY OF SUCH DAMAGE.
|
| 28 |
+
# ******************************************************************************
|
| 29 |
+
|
| 30 |
+
r"""
|
| 31 |
+
======================================================================
|
| 32 |
+
Interpolative matrix decomposition (:mod:`scipy.linalg.interpolative`)
|
| 33 |
+
======================================================================
|
| 34 |
+
|
| 35 |
+
.. versionadded:: 0.13
|
| 36 |
+
|
| 37 |
+
.. versionchanged:: 1.15.0
|
| 38 |
+
The underlying algorithms have been ported to Python from the original Fortran77
|
| 39 |
+
code. See references below for more details.
|
| 40 |
+
|
| 41 |
+
.. currentmodule:: scipy.linalg.interpolative
|
| 42 |
+
|
| 43 |
+
An interpolative decomposition (ID) of a matrix :math:`A \in
|
| 44 |
+
\mathbb{C}^{m \times n}` of rank :math:`k \leq \min \{ m, n \}` is a
|
| 45 |
+
factorization
|
| 46 |
+
|
| 47 |
+
.. math::
|
| 48 |
+
A \Pi =
|
| 49 |
+
\begin{bmatrix}
|
| 50 |
+
A \Pi_{1} & A \Pi_{2}
|
| 51 |
+
\end{bmatrix} =
|
| 52 |
+
A \Pi_{1}
|
| 53 |
+
\begin{bmatrix}
|
| 54 |
+
I & T
|
| 55 |
+
\end{bmatrix},
|
| 56 |
+
|
| 57 |
+
where :math:`\Pi = [\Pi_{1}, \Pi_{2}]` is a permutation matrix with
|
| 58 |
+
:math:`\Pi_{1} \in \{ 0, 1 \}^{n \times k}`, i.e., :math:`A \Pi_{2} =
|
| 59 |
+
A \Pi_{1} T`. This can equivalently be written as :math:`A = BP`,
|
| 60 |
+
where :math:`B = A \Pi_{1}` and :math:`P = [I, T] \Pi^{\mathsf{T}}`
|
| 61 |
+
are the *skeleton* and *interpolation matrices*, respectively.
|
| 62 |
+
|
| 63 |
+
If :math:`A` does not have exact rank :math:`k`, then there exists an
|
| 64 |
+
approximation in the form of an ID such that :math:`A = BP + E`, where
|
| 65 |
+
:math:`\| E \| \sim \sigma_{k + 1}` is on the order of the :math:`(k +
|
| 66 |
+
1)`-th largest singular value of :math:`A`. Note that :math:`\sigma_{k
|
| 67 |
+
+ 1}` is the best possible error for a rank-:math:`k` approximation
|
| 68 |
+
and, in fact, is achieved by the singular value decomposition (SVD)
|
| 69 |
+
:math:`A \approx U S V^{*}`, where :math:`U \in \mathbb{C}^{m \times
|
| 70 |
+
k}` and :math:`V \in \mathbb{C}^{n \times k}` have orthonormal columns
|
| 71 |
+
and :math:`S = \mathop{\mathrm{diag}} (\sigma_{i}) \in \mathbb{C}^{k
|
| 72 |
+
\times k}` is diagonal with nonnegative entries. The principal
|
| 73 |
+
advantages of using an ID over an SVD are that:
|
| 74 |
+
|
| 75 |
+
- it is cheaper to construct;
|
| 76 |
+
- it preserves the structure of :math:`A`; and
|
| 77 |
+
- it is more efficient to compute with in light of the identity submatrix of :math:`P`.
|
| 78 |
+
|
| 79 |
+
Routines
|
| 80 |
+
========
|
| 81 |
+
|
| 82 |
+
Main functionality:
|
| 83 |
+
|
| 84 |
+
.. autosummary::
|
| 85 |
+
:toctree: generated/
|
| 86 |
+
|
| 87 |
+
interp_decomp
|
| 88 |
+
reconstruct_matrix_from_id
|
| 89 |
+
reconstruct_interp_matrix
|
| 90 |
+
reconstruct_skel_matrix
|
| 91 |
+
id_to_svd
|
| 92 |
+
svd
|
| 93 |
+
estimate_spectral_norm
|
| 94 |
+
estimate_spectral_norm_diff
|
| 95 |
+
estimate_rank
|
| 96 |
+
|
| 97 |
+
Following support functions are deprecated and will be removed in SciPy 1.17.0:
|
| 98 |
+
|
| 99 |
+
.. autosummary::
|
| 100 |
+
:toctree: generated/
|
| 101 |
+
|
| 102 |
+
seed
|
| 103 |
+
rand
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
References
|
| 107 |
+
==========
|
| 108 |
+
|
| 109 |
+
This module uses the algorithms found in ID software package [1]_ by Martinsson,
|
| 110 |
+
Rokhlin, Shkolnisky, and Tygert, which is a Fortran library for computing IDs using
|
| 111 |
+
various algorithms, including the rank-revealing QR approach of [2]_ and the more
|
| 112 |
+
recent randomized methods described in [3]_, [4]_, and [5]_.
|
| 113 |
+
|
| 114 |
+
We advise the user to consult also the documentation for the `ID package
|
| 115 |
+
<http://tygert.com/software.html>`_.
|
| 116 |
+
|
| 117 |
+
.. [1] P.G. Martinsson, V. Rokhlin, Y. Shkolnisky, M. Tygert. "ID: a
|
| 118 |
+
software package for low-rank approximation of matrices via interpolative
|
| 119 |
+
decompositions, version 0.2." http://tygert.com/id_doc.4.pdf.
|
| 120 |
+
|
| 121 |
+
.. [2] H. Cheng, Z. Gimbutas, P.G. Martinsson, V. Rokhlin. "On the
|
| 122 |
+
compression of low rank matrices." *SIAM J. Sci. Comput.* 26 (4): 1389--1404,
|
| 123 |
+
2005. :doi:`10.1137/030602678`.
|
| 124 |
+
|
| 125 |
+
.. [3] E. Liberty, F. Woolfe, P.G. Martinsson, V. Rokhlin, M.
|
| 126 |
+
Tygert. "Randomized algorithms for the low-rank approximation of matrices."
|
| 127 |
+
*Proc. Natl. Acad. Sci. U.S.A.* 104 (51): 20167--20172, 2007.
|
| 128 |
+
:doi:`10.1073/pnas.0709640104`.
|
| 129 |
+
|
| 130 |
+
.. [4] P.G. Martinsson, V. Rokhlin, M. Tygert. "A randomized
|
| 131 |
+
algorithm for the decomposition of matrices." *Appl. Comput. Harmon. Anal.* 30
|
| 132 |
+
(1): 47--68, 2011. :doi:`10.1016/j.acha.2010.02.003`.
|
| 133 |
+
|
| 134 |
+
.. [5] F. Woolfe, E. Liberty, V. Rokhlin, M. Tygert. "A fast
|
| 135 |
+
randomized algorithm for the approximation of matrices." *Appl. Comput.
|
| 136 |
+
Harmon. Anal.* 25 (3): 335--366, 2008. :doi:`10.1016/j.acha.2007.12.002`.
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
Tutorial
|
| 140 |
+
========
|
| 141 |
+
|
| 142 |
+
Initializing
|
| 143 |
+
------------
|
| 144 |
+
|
| 145 |
+
The first step is to import :mod:`scipy.linalg.interpolative` by issuing the
|
| 146 |
+
command:
|
| 147 |
+
|
| 148 |
+
>>> import scipy.linalg.interpolative as sli
|
| 149 |
+
|
| 150 |
+
Now let's build a matrix. For this, we consider a Hilbert matrix, which is well
|
| 151 |
+
know to have low rank:
|
| 152 |
+
|
| 153 |
+
>>> from scipy.linalg import hilbert
|
| 154 |
+
>>> n = 1000
|
| 155 |
+
>>> A = hilbert(n)
|
| 156 |
+
|
| 157 |
+
We can also do this explicitly via:
|
| 158 |
+
|
| 159 |
+
>>> import numpy as np
|
| 160 |
+
>>> n = 1000
|
| 161 |
+
>>> A = np.empty((n, n), order='F')
|
| 162 |
+
>>> for j in range(n):
|
| 163 |
+
... for i in range(n):
|
| 164 |
+
... A[i,j] = 1. / (i + j + 1)
|
| 165 |
+
|
| 166 |
+
Note the use of the flag ``order='F'`` in :func:`numpy.empty`. This
|
| 167 |
+
instantiates the matrix in Fortran-contiguous order and is important for
|
| 168 |
+
avoiding data copying when passing to the backend.
|
| 169 |
+
|
| 170 |
+
We then define multiplication routines for the matrix by regarding it as a
|
| 171 |
+
:class:`scipy.sparse.linalg.LinearOperator`:
|
| 172 |
+
|
| 173 |
+
>>> from scipy.sparse.linalg import aslinearoperator
|
| 174 |
+
>>> L = aslinearoperator(A)
|
| 175 |
+
|
| 176 |
+
This automatically sets up methods describing the action of the matrix and its
|
| 177 |
+
adjoint on a vector.
|
| 178 |
+
|
| 179 |
+
Computing an ID
|
| 180 |
+
---------------
|
| 181 |
+
|
| 182 |
+
We have several choices of algorithm to compute an ID. These fall largely
|
| 183 |
+
according to two dichotomies:
|
| 184 |
+
|
| 185 |
+
1. how the matrix is represented, i.e., via its entries or via its action on a
|
| 186 |
+
vector; and
|
| 187 |
+
2. whether to approximate it to a fixed relative precision or to a fixed rank.
|
| 188 |
+
|
| 189 |
+
We step through each choice in turn below.
|
| 190 |
+
|
| 191 |
+
In all cases, the ID is represented by three parameters:
|
| 192 |
+
|
| 193 |
+
1. a rank ``k``;
|
| 194 |
+
2. an index array ``idx``; and
|
| 195 |
+
3. interpolation coefficients ``proj``.
|
| 196 |
+
|
| 197 |
+
The ID is specified by the relation
|
| 198 |
+
``np.dot(A[:,idx[:k]], proj) == A[:,idx[k:]]``.
|
| 199 |
+
|
| 200 |
+
From matrix entries
|
| 201 |
+
...................
|
| 202 |
+
|
| 203 |
+
We first consider a matrix given in terms of its entries.
|
| 204 |
+
|
| 205 |
+
To compute an ID to a fixed precision, type:
|
| 206 |
+
|
| 207 |
+
>>> eps = 1e-3
|
| 208 |
+
>>> k, idx, proj = sli.interp_decomp(A, eps)
|
| 209 |
+
|
| 210 |
+
where ``eps < 1`` is the desired precision.
|
| 211 |
+
|
| 212 |
+
To compute an ID to a fixed rank, use:
|
| 213 |
+
|
| 214 |
+
>>> idx, proj = sli.interp_decomp(A, k)
|
| 215 |
+
|
| 216 |
+
where ``k >= 1`` is the desired rank.
|
| 217 |
+
|
| 218 |
+
Both algorithms use random sampling and are usually faster than the
|
| 219 |
+
corresponding older, deterministic algorithms, which can be accessed via the
|
| 220 |
+
commands:
|
| 221 |
+
|
| 222 |
+
>>> k, idx, proj = sli.interp_decomp(A, eps, rand=False)
|
| 223 |
+
|
| 224 |
+
and:
|
| 225 |
+
|
| 226 |
+
>>> idx, proj = sli.interp_decomp(A, k, rand=False)
|
| 227 |
+
|
| 228 |
+
respectively.
|
| 229 |
+
|
| 230 |
+
From matrix action
|
| 231 |
+
..................
|
| 232 |
+
|
| 233 |
+
Now consider a matrix given in terms of its action on a vector as a
|
| 234 |
+
:class:`scipy.sparse.linalg.LinearOperator`.
|
| 235 |
+
|
| 236 |
+
To compute an ID to a fixed precision, type:
|
| 237 |
+
|
| 238 |
+
>>> k, idx, proj = sli.interp_decomp(L, eps)
|
| 239 |
+
|
| 240 |
+
To compute an ID to a fixed rank, use:
|
| 241 |
+
|
| 242 |
+
>>> idx, proj = sli.interp_decomp(L, k)
|
| 243 |
+
|
| 244 |
+
These algorithms are randomized.
|
| 245 |
+
|
| 246 |
+
Reconstructing an ID
|
| 247 |
+
--------------------
|
| 248 |
+
|
| 249 |
+
The ID routines above do not output the skeleton and interpolation matrices
|
| 250 |
+
explicitly but instead return the relevant information in a more compact (and
|
| 251 |
+
sometimes more useful) form. To build these matrices, write:
|
| 252 |
+
|
| 253 |
+
>>> B = sli.reconstruct_skel_matrix(A, k, idx)
|
| 254 |
+
|
| 255 |
+
for the skeleton matrix and:
|
| 256 |
+
|
| 257 |
+
>>> P = sli.reconstruct_interp_matrix(idx, proj)
|
| 258 |
+
|
| 259 |
+
for the interpolation matrix. The ID approximation can then be computed as:
|
| 260 |
+
|
| 261 |
+
>>> C = np.dot(B, P)
|
| 262 |
+
|
| 263 |
+
This can also be constructed directly using:
|
| 264 |
+
|
| 265 |
+
>>> C = sli.reconstruct_matrix_from_id(B, idx, proj)
|
| 266 |
+
|
| 267 |
+
without having to first compute ``P``.
|
| 268 |
+
|
| 269 |
+
Alternatively, this can be done explicitly as well using:
|
| 270 |
+
|
| 271 |
+
>>> B = A[:,idx[:k]]
|
| 272 |
+
>>> P = np.hstack([np.eye(k), proj])[:,np.argsort(idx)]
|
| 273 |
+
>>> C = np.dot(B, P)
|
| 274 |
+
|
| 275 |
+
Computing an SVD
|
| 276 |
+
----------------
|
| 277 |
+
|
| 278 |
+
An ID can be converted to an SVD via the command:
|
| 279 |
+
|
| 280 |
+
>>> U, S, V = sli.id_to_svd(B, idx, proj)
|
| 281 |
+
|
| 282 |
+
The SVD approximation is then:
|
| 283 |
+
|
| 284 |
+
>>> approx = U @ np.diag(S) @ V.conj().T
|
| 285 |
+
|
| 286 |
+
The SVD can also be computed "fresh" by combining both the ID and conversion
|
| 287 |
+
steps into one command. Following the various ID algorithms above, there are
|
| 288 |
+
correspondingly various SVD algorithms that one can employ.
|
| 289 |
+
|
| 290 |
+
From matrix entries
|
| 291 |
+
...................
|
| 292 |
+
|
| 293 |
+
We consider first SVD algorithms for a matrix given in terms of its entries.
|
| 294 |
+
|
| 295 |
+
To compute an SVD to a fixed precision, type:
|
| 296 |
+
|
| 297 |
+
>>> U, S, V = sli.svd(A, eps)
|
| 298 |
+
|
| 299 |
+
To compute an SVD to a fixed rank, use:
|
| 300 |
+
|
| 301 |
+
>>> U, S, V = sli.svd(A, k)
|
| 302 |
+
|
| 303 |
+
Both algorithms use random sampling; for the deterministic versions, issue the
|
| 304 |
+
keyword ``rand=False`` as above.
|
| 305 |
+
|
| 306 |
+
From matrix action
|
| 307 |
+
..................
|
| 308 |
+
|
| 309 |
+
Now consider a matrix given in terms of its action on a vector.
|
| 310 |
+
|
| 311 |
+
To compute an SVD to a fixed precision, type:
|
| 312 |
+
|
| 313 |
+
>>> U, S, V = sli.svd(L, eps)
|
| 314 |
+
|
| 315 |
+
To compute an SVD to a fixed rank, use:
|
| 316 |
+
|
| 317 |
+
>>> U, S, V = sli.svd(L, k)
|
| 318 |
+
|
| 319 |
+
Utility routines
|
| 320 |
+
----------------
|
| 321 |
+
|
| 322 |
+
Several utility routines are also available.
|
| 323 |
+
|
| 324 |
+
To estimate the spectral norm of a matrix, use:
|
| 325 |
+
|
| 326 |
+
>>> snorm = sli.estimate_spectral_norm(A)
|
| 327 |
+
|
| 328 |
+
This algorithm is based on the randomized power method and thus requires only
|
| 329 |
+
matrix-vector products. The number of iterations to take can be set using the
|
| 330 |
+
keyword ``its`` (default: ``its=20``). The matrix is interpreted as a
|
| 331 |
+
:class:`scipy.sparse.linalg.LinearOperator`, but it is also valid to supply it
|
| 332 |
+
as a :class:`numpy.ndarray`, in which case it is trivially converted using
|
| 333 |
+
:func:`scipy.sparse.linalg.aslinearoperator`.
|
| 334 |
+
|
| 335 |
+
The same algorithm can also estimate the spectral norm of the difference of two
|
| 336 |
+
matrices ``A1`` and ``A2`` as follows:
|
| 337 |
+
|
| 338 |
+
>>> A1, A2 = A**2, A
|
| 339 |
+
>>> diff = sli.estimate_spectral_norm_diff(A1, A2)
|
| 340 |
+
|
| 341 |
+
This is often useful for checking the accuracy of a matrix approximation.
|
| 342 |
+
|
| 343 |
+
Some routines in :mod:`scipy.linalg.interpolative` require estimating the rank
|
| 344 |
+
of a matrix as well. This can be done with either:
|
| 345 |
+
|
| 346 |
+
>>> k = sli.estimate_rank(A, eps)
|
| 347 |
+
|
| 348 |
+
or:
|
| 349 |
+
|
| 350 |
+
>>> k = sli.estimate_rank(L, eps)
|
| 351 |
+
|
| 352 |
+
depending on the representation. The parameter ``eps`` controls the definition
|
| 353 |
+
of the numerical rank.
|
| 354 |
+
|
| 355 |
+
Finally, the random number generation required for all randomized routines can
|
| 356 |
+
be controlled via providing NumPy pseudo-random generators with a fixed seed. See
|
| 357 |
+
:class:`numpy.random.Generator` and :func:`numpy.random.default_rng` for more details.
|
| 358 |
+
|
| 359 |
+
Remarks
|
| 360 |
+
-------
|
| 361 |
+
|
| 362 |
+
The above functions all automatically detect the appropriate interface and work
|
| 363 |
+
with both real and complex data types, passing input arguments to the proper
|
| 364 |
+
backend routine.
|
| 365 |
+
|
| 366 |
+
"""
|
| 367 |
+
|
| 368 |
+
import scipy.linalg._decomp_interpolative as _backend
|
| 369 |
+
import numpy as np
|
| 370 |
+
import warnings
|
| 371 |
+
|
| 372 |
+
__all__ = [
|
| 373 |
+
'estimate_rank',
|
| 374 |
+
'estimate_spectral_norm',
|
| 375 |
+
'estimate_spectral_norm_diff',
|
| 376 |
+
'id_to_svd',
|
| 377 |
+
'interp_decomp',
|
| 378 |
+
'rand',
|
| 379 |
+
'reconstruct_interp_matrix',
|
| 380 |
+
'reconstruct_matrix_from_id',
|
| 381 |
+
'reconstruct_skel_matrix',
|
| 382 |
+
'seed',
|
| 383 |
+
'svd',
|
| 384 |
+
]
|
| 385 |
+
|
| 386 |
+
_DTYPE_ERROR = ValueError("invalid input dtype (input must be float64 or complex128)")
|
| 387 |
+
_TYPE_ERROR = TypeError("invalid input type (must be array or LinearOperator)")
|
| 388 |
+
|
| 389 |
+
|
| 390 |
+
def _C_contiguous_copy(A):
|
| 391 |
+
"""
|
| 392 |
+
Same as np.ascontiguousarray, but ensure a copy
|
| 393 |
+
"""
|
| 394 |
+
A = np.asarray(A)
|
| 395 |
+
if A.flags.c_contiguous:
|
| 396 |
+
A = A.copy()
|
| 397 |
+
else:
|
| 398 |
+
A = np.ascontiguousarray(A)
|
| 399 |
+
return A
|
| 400 |
+
|
| 401 |
+
|
| 402 |
+
def _is_real(A):
|
| 403 |
+
try:
|
| 404 |
+
if A.dtype == np.complex128:
|
| 405 |
+
return False
|
| 406 |
+
elif A.dtype == np.float64:
|
| 407 |
+
return True
|
| 408 |
+
else:
|
| 409 |
+
raise _DTYPE_ERROR
|
| 410 |
+
except AttributeError as e:
|
| 411 |
+
raise _TYPE_ERROR from e
|
| 412 |
+
|
| 413 |
+
|
| 414 |
+
def seed(seed=None):
|
| 415 |
+
"""
|
| 416 |
+
This function, historically, used to set the seed of the randomization algorithms
|
| 417 |
+
used in the `scipy.linalg.interpolative` functions written in Fortran77.
|
| 418 |
+
|
| 419 |
+
The library has been ported to Python and now the functions use the native NumPy
|
| 420 |
+
generators and this function has no content and returns None. Thus this function
|
| 421 |
+
should not be used and will be removed in SciPy version 1.17.0.
|
| 422 |
+
"""
|
| 423 |
+
warnings.warn("`scipy.linalg.interpolative.seed` is deprecated and will be "
|
| 424 |
+
"removed in SciPy 1.17.0.", DeprecationWarning, stacklevel=3)
|
| 425 |
+
|
| 426 |
+
|
| 427 |
+
def rand(*shape):
|
| 428 |
+
"""
|
| 429 |
+
This function, historically, used to generate uniformly distributed random number
|
| 430 |
+
for the randomization algorithms used in the `scipy.linalg.interpolative` functions
|
| 431 |
+
written in Fortran77.
|
| 432 |
+
|
| 433 |
+
The library has been ported to Python and now the functions use the native NumPy
|
| 434 |
+
generators. Thus this function should not be used and will be removed in the
|
| 435 |
+
SciPy version 1.17.0.
|
| 436 |
+
|
| 437 |
+
If pseudo-random numbers are needed, NumPy pseudo-random generators should be used
|
| 438 |
+
instead.
|
| 439 |
+
|
| 440 |
+
Parameters
|
| 441 |
+
----------
|
| 442 |
+
*shape
|
| 443 |
+
Shape of output array
|
| 444 |
+
|
| 445 |
+
"""
|
| 446 |
+
warnings.warn("`scipy.linalg.interpolative.rand` is deprecated and will be "
|
| 447 |
+
"removed in SciPy 1.17.0.", DeprecationWarning, stacklevel=3)
|
| 448 |
+
rng = np.random.default_rng()
|
| 449 |
+
return rng.uniform(low=0., high=1.0, size=shape)
|
| 450 |
+
|
| 451 |
+
|
| 452 |
+
def interp_decomp(A, eps_or_k, rand=True, rng=None):
|
| 453 |
+
"""
|
| 454 |
+
Compute ID of a matrix.
|
| 455 |
+
|
| 456 |
+
An ID of a matrix `A` is a factorization defined by a rank `k`, a column
|
| 457 |
+
index array `idx`, and interpolation coefficients `proj` such that::
|
| 458 |
+
|
| 459 |
+
numpy.dot(A[:,idx[:k]], proj) = A[:,idx[k:]]
|
| 460 |
+
|
| 461 |
+
The original matrix can then be reconstructed as::
|
| 462 |
+
|
| 463 |
+
numpy.hstack([A[:,idx[:k]],
|
| 464 |
+
numpy.dot(A[:,idx[:k]], proj)]
|
| 465 |
+
)[:,numpy.argsort(idx)]
|
| 466 |
+
|
| 467 |
+
or via the routine :func:`reconstruct_matrix_from_id`. This can
|
| 468 |
+
equivalently be written as::
|
| 469 |
+
|
| 470 |
+
numpy.dot(A[:,idx[:k]],
|
| 471 |
+
numpy.hstack([numpy.eye(k), proj])
|
| 472 |
+
)[:,np.argsort(idx)]
|
| 473 |
+
|
| 474 |
+
in terms of the skeleton and interpolation matrices::
|
| 475 |
+
|
| 476 |
+
B = A[:,idx[:k]]
|
| 477 |
+
|
| 478 |
+
and::
|
| 479 |
+
|
| 480 |
+
P = numpy.hstack([numpy.eye(k), proj])[:,np.argsort(idx)]
|
| 481 |
+
|
| 482 |
+
respectively. See also :func:`reconstruct_interp_matrix` and
|
| 483 |
+
:func:`reconstruct_skel_matrix`.
|
| 484 |
+
|
| 485 |
+
The ID can be computed to any relative precision or rank (depending on the
|
| 486 |
+
value of `eps_or_k`). If a precision is specified (`eps_or_k < 1`), then
|
| 487 |
+
this function has the output signature::
|
| 488 |
+
|
| 489 |
+
k, idx, proj = interp_decomp(A, eps_or_k)
|
| 490 |
+
|
| 491 |
+
Otherwise, if a rank is specified (`eps_or_k >= 1`), then the output
|
| 492 |
+
signature is::
|
| 493 |
+
|
| 494 |
+
idx, proj = interp_decomp(A, eps_or_k)
|
| 495 |
+
|
| 496 |
+
.. This function automatically detects the form of the input parameters
|
| 497 |
+
and passes them to the appropriate backend. For details, see
|
| 498 |
+
:func:`_backend.iddp_id`, :func:`_backend.iddp_aid`,
|
| 499 |
+
:func:`_backend.iddp_rid`, :func:`_backend.iddr_id`,
|
| 500 |
+
:func:`_backend.iddr_aid`, :func:`_backend.iddr_rid`,
|
| 501 |
+
:func:`_backend.idzp_id`, :func:`_backend.idzp_aid`,
|
| 502 |
+
:func:`_backend.idzp_rid`, :func:`_backend.idzr_id`,
|
| 503 |
+
:func:`_backend.idzr_aid`, and :func:`_backend.idzr_rid`.
|
| 504 |
+
|
| 505 |
+
Parameters
|
| 506 |
+
----------
|
| 507 |
+
A : :class:`numpy.ndarray` or :class:`scipy.sparse.linalg.LinearOperator` with `rmatvec`
|
| 508 |
+
Matrix to be factored
|
| 509 |
+
eps_or_k : float or int
|
| 510 |
+
Relative error (if ``eps_or_k < 1``) or rank (if ``eps_or_k >= 1``) of
|
| 511 |
+
approximation.
|
| 512 |
+
rand : bool, optional
|
| 513 |
+
Whether to use random sampling if `A` is of type :class:`numpy.ndarray`
|
| 514 |
+
(randomized algorithms are always used if `A` is of type
|
| 515 |
+
:class:`scipy.sparse.linalg.LinearOperator`).
|
| 516 |
+
rng : `numpy.random.Generator`, optional
|
| 517 |
+
Pseudorandom number generator state. When `rng` is None, a new
|
| 518 |
+
`numpy.random.Generator` is created using entropy from the
|
| 519 |
+
operating system. Types other than `numpy.random.Generator` are
|
| 520 |
+
passed to `numpy.random.default_rng` to instantiate a ``Generator``.
|
| 521 |
+
If `rand` is ``False``, the argument is ignored.
|
| 522 |
+
|
| 523 |
+
Returns
|
| 524 |
+
-------
|
| 525 |
+
k : int
|
| 526 |
+
Rank required to achieve specified relative precision if
|
| 527 |
+
``eps_or_k < 1``.
|
| 528 |
+
idx : :class:`numpy.ndarray`
|
| 529 |
+
Column index array.
|
| 530 |
+
proj : :class:`numpy.ndarray`
|
| 531 |
+
Interpolation coefficients.
|
| 532 |
+
""" # numpy/numpydoc#87 # noqa: E501
|
| 533 |
+
from scipy.sparse.linalg import LinearOperator
|
| 534 |
+
rng = np.random.default_rng(rng)
|
| 535 |
+
real = _is_real(A)
|
| 536 |
+
|
| 537 |
+
if isinstance(A, np.ndarray):
|
| 538 |
+
A = _C_contiguous_copy(A)
|
| 539 |
+
if eps_or_k < 1:
|
| 540 |
+
eps = eps_or_k
|
| 541 |
+
if rand:
|
| 542 |
+
if real:
|
| 543 |
+
k, idx, proj = _backend.iddp_aid(A, eps, rng=rng)
|
| 544 |
+
else:
|
| 545 |
+
k, idx, proj = _backend.idzp_aid(A, eps, rng=rng)
|
| 546 |
+
else:
|
| 547 |
+
if real:
|
| 548 |
+
k, idx, proj = _backend.iddp_id(A, eps)
|
| 549 |
+
else:
|
| 550 |
+
k, idx, proj = _backend.idzp_id(A, eps)
|
| 551 |
+
return k, idx, proj
|
| 552 |
+
else:
|
| 553 |
+
k = int(eps_or_k)
|
| 554 |
+
if rand:
|
| 555 |
+
if real:
|
| 556 |
+
idx, proj = _backend.iddr_aid(A, k, rng=rng)
|
| 557 |
+
else:
|
| 558 |
+
idx, proj = _backend.idzr_aid(A, k, rng=rng)
|
| 559 |
+
else:
|
| 560 |
+
if real:
|
| 561 |
+
idx, proj = _backend.iddr_id(A, k)
|
| 562 |
+
else:
|
| 563 |
+
idx, proj = _backend.idzr_id(A, k)
|
| 564 |
+
return idx, proj
|
| 565 |
+
elif isinstance(A, LinearOperator):
|
| 566 |
+
|
| 567 |
+
if eps_or_k < 1:
|
| 568 |
+
eps = eps_or_k
|
| 569 |
+
if real:
|
| 570 |
+
k, idx, proj = _backend.iddp_rid(A, eps, rng=rng)
|
| 571 |
+
else:
|
| 572 |
+
k, idx, proj = _backend.idzp_rid(A, eps, rng=rng)
|
| 573 |
+
return k, idx, proj
|
| 574 |
+
else:
|
| 575 |
+
k = int(eps_or_k)
|
| 576 |
+
if real:
|
| 577 |
+
idx, proj = _backend.iddr_rid(A, k, rng=rng)
|
| 578 |
+
else:
|
| 579 |
+
idx, proj = _backend.idzr_rid(A, k, rng=rng)
|
| 580 |
+
return idx, proj
|
| 581 |
+
else:
|
| 582 |
+
raise _TYPE_ERROR
|
| 583 |
+
|
| 584 |
+
|
| 585 |
+
def reconstruct_matrix_from_id(B, idx, proj):
|
| 586 |
+
"""
|
| 587 |
+
Reconstruct matrix from its ID.
|
| 588 |
+
|
| 589 |
+
A matrix `A` with skeleton matrix `B` and ID indices and coefficients `idx`
|
| 590 |
+
and `proj`, respectively, can be reconstructed as::
|
| 591 |
+
|
| 592 |
+
numpy.hstack([B, numpy.dot(B, proj)])[:,numpy.argsort(idx)]
|
| 593 |
+
|
| 594 |
+
See also :func:`reconstruct_interp_matrix` and
|
| 595 |
+
:func:`reconstruct_skel_matrix`.
|
| 596 |
+
|
| 597 |
+
.. This function automatically detects the matrix data type and calls the
|
| 598 |
+
appropriate backend. For details, see :func:`_backend.idd_reconid` and
|
| 599 |
+
:func:`_backend.idz_reconid`.
|
| 600 |
+
|
| 601 |
+
Parameters
|
| 602 |
+
----------
|
| 603 |
+
B : :class:`numpy.ndarray`
|
| 604 |
+
Skeleton matrix.
|
| 605 |
+
idx : :class:`numpy.ndarray`
|
| 606 |
+
Column index array.
|
| 607 |
+
proj : :class:`numpy.ndarray`
|
| 608 |
+
Interpolation coefficients.
|
| 609 |
+
|
| 610 |
+
Returns
|
| 611 |
+
-------
|
| 612 |
+
:class:`numpy.ndarray`
|
| 613 |
+
Reconstructed matrix.
|
| 614 |
+
"""
|
| 615 |
+
if _is_real(B):
|
| 616 |
+
return _backend.idd_reconid(B, idx, proj)
|
| 617 |
+
else:
|
| 618 |
+
return _backend.idz_reconid(B, idx, proj)
|
| 619 |
+
|
| 620 |
+
|
| 621 |
+
def reconstruct_interp_matrix(idx, proj):
|
| 622 |
+
"""
|
| 623 |
+
Reconstruct interpolation matrix from ID.
|
| 624 |
+
|
| 625 |
+
The interpolation matrix can be reconstructed from the ID indices and
|
| 626 |
+
coefficients `idx` and `proj`, respectively, as::
|
| 627 |
+
|
| 628 |
+
P = numpy.hstack([numpy.eye(proj.shape[0]), proj])[:,numpy.argsort(idx)]
|
| 629 |
+
|
| 630 |
+
The original matrix can then be reconstructed from its skeleton matrix ``B``
|
| 631 |
+
via ``A = B @ P``
|
| 632 |
+
|
| 633 |
+
See also :func:`reconstruct_matrix_from_id` and
|
| 634 |
+
:func:`reconstruct_skel_matrix`.
|
| 635 |
+
|
| 636 |
+
.. This function automatically detects the matrix data type and calls the
|
| 637 |
+
appropriate backend. For details, see :func:`_backend.idd_reconint` and
|
| 638 |
+
:func:`_backend.idz_reconint`.
|
| 639 |
+
|
| 640 |
+
Parameters
|
| 641 |
+
----------
|
| 642 |
+
idx : :class:`numpy.ndarray`
|
| 643 |
+
1D column index array.
|
| 644 |
+
proj : :class:`numpy.ndarray`
|
| 645 |
+
Interpolation coefficients.
|
| 646 |
+
|
| 647 |
+
Returns
|
| 648 |
+
-------
|
| 649 |
+
:class:`numpy.ndarray`
|
| 650 |
+
Interpolation matrix.
|
| 651 |
+
"""
|
| 652 |
+
n, krank = len(idx), proj.shape[0]
|
| 653 |
+
if _is_real(proj):
|
| 654 |
+
p = np.zeros([krank, n], dtype=np.float64)
|
| 655 |
+
else:
|
| 656 |
+
p = np.zeros([krank, n], dtype=np.complex128)
|
| 657 |
+
|
| 658 |
+
for ci in range(krank):
|
| 659 |
+
p[ci, idx[ci]] = 1.0
|
| 660 |
+
p[:, idx[krank:]] = proj[:, :]
|
| 661 |
+
|
| 662 |
+
return p
|
| 663 |
+
|
| 664 |
+
|
| 665 |
+
def reconstruct_skel_matrix(A, k, idx):
|
| 666 |
+
"""
|
| 667 |
+
Reconstruct skeleton matrix from ID.
|
| 668 |
+
|
| 669 |
+
The skeleton matrix can be reconstructed from the original matrix `A` and its
|
| 670 |
+
ID rank and indices `k` and `idx`, respectively, as::
|
| 671 |
+
|
| 672 |
+
B = A[:,idx[:k]]
|
| 673 |
+
|
| 674 |
+
The original matrix can then be reconstructed via::
|
| 675 |
+
|
| 676 |
+
numpy.hstack([B, numpy.dot(B, proj)])[:,numpy.argsort(idx)]
|
| 677 |
+
|
| 678 |
+
See also :func:`reconstruct_matrix_from_id` and
|
| 679 |
+
:func:`reconstruct_interp_matrix`.
|
| 680 |
+
|
| 681 |
+
.. This function automatically detects the matrix data type and calls the
|
| 682 |
+
appropriate backend. For details, see :func:`_backend.idd_copycols` and
|
| 683 |
+
:func:`_backend.idz_copycols`.
|
| 684 |
+
|
| 685 |
+
Parameters
|
| 686 |
+
----------
|
| 687 |
+
A : :class:`numpy.ndarray`
|
| 688 |
+
Original matrix.
|
| 689 |
+
k : int
|
| 690 |
+
Rank of ID.
|
| 691 |
+
idx : :class:`numpy.ndarray`
|
| 692 |
+
Column index array.
|
| 693 |
+
|
| 694 |
+
Returns
|
| 695 |
+
-------
|
| 696 |
+
:class:`numpy.ndarray`
|
| 697 |
+
Skeleton matrix.
|
| 698 |
+
"""
|
| 699 |
+
return A[:, idx[:k]]
|
| 700 |
+
|
| 701 |
+
|
| 702 |
+
def id_to_svd(B, idx, proj):
|
| 703 |
+
"""
|
| 704 |
+
Convert ID to SVD.
|
| 705 |
+
|
| 706 |
+
The SVD reconstruction of a matrix with skeleton matrix `B` and ID indices and
|
| 707 |
+
coefficients `idx` and `proj`, respectively, is::
|
| 708 |
+
|
| 709 |
+
U, S, V = id_to_svd(B, idx, proj)
|
| 710 |
+
A = numpy.dot(U, numpy.dot(numpy.diag(S), V.conj().T))
|
| 711 |
+
|
| 712 |
+
See also :func:`svd`.
|
| 713 |
+
|
| 714 |
+
.. This function automatically detects the matrix data type and calls the
|
| 715 |
+
appropriate backend. For details, see :func:`_backend.idd_id2svd` and
|
| 716 |
+
:func:`_backend.idz_id2svd`.
|
| 717 |
+
|
| 718 |
+
Parameters
|
| 719 |
+
----------
|
| 720 |
+
B : :class:`numpy.ndarray`
|
| 721 |
+
Skeleton matrix.
|
| 722 |
+
idx : :class:`numpy.ndarray`
|
| 723 |
+
1D column index array.
|
| 724 |
+
proj : :class:`numpy.ndarray`
|
| 725 |
+
Interpolation coefficients.
|
| 726 |
+
|
| 727 |
+
Returns
|
| 728 |
+
-------
|
| 729 |
+
U : :class:`numpy.ndarray`
|
| 730 |
+
Left singular vectors.
|
| 731 |
+
S : :class:`numpy.ndarray`
|
| 732 |
+
Singular values.
|
| 733 |
+
V : :class:`numpy.ndarray`
|
| 734 |
+
Right singular vectors.
|
| 735 |
+
"""
|
| 736 |
+
B = _C_contiguous_copy(B)
|
| 737 |
+
if _is_real(B):
|
| 738 |
+
U, S, V = _backend.idd_id2svd(B, idx, proj)
|
| 739 |
+
else:
|
| 740 |
+
U, S, V = _backend.idz_id2svd(B, idx, proj)
|
| 741 |
+
|
| 742 |
+
return U, S, V
|
| 743 |
+
|
| 744 |
+
|
| 745 |
+
def estimate_spectral_norm(A, its=20, rng=None):
|
| 746 |
+
"""
|
| 747 |
+
Estimate spectral norm of a matrix by the randomized power method.
|
| 748 |
+
|
| 749 |
+
.. This function automatically detects the matrix data type and calls the
|
| 750 |
+
appropriate backend. For details, see :func:`_backend.idd_snorm` and
|
| 751 |
+
:func:`_backend.idz_snorm`.
|
| 752 |
+
|
| 753 |
+
Parameters
|
| 754 |
+
----------
|
| 755 |
+
A : :class:`scipy.sparse.linalg.LinearOperator`
|
| 756 |
+
Matrix given as a :class:`scipy.sparse.linalg.LinearOperator` with the
|
| 757 |
+
`matvec` and `rmatvec` methods (to apply the matrix and its adjoint).
|
| 758 |
+
its : int, optional
|
| 759 |
+
Number of power method iterations.
|
| 760 |
+
rng : `numpy.random.Generator`, optional
|
| 761 |
+
Pseudorandom number generator state. When `rng` is None, a new
|
| 762 |
+
`numpy.random.Generator` is created using entropy from the
|
| 763 |
+
operating system. Types other than `numpy.random.Generator` are
|
| 764 |
+
passed to `numpy.random.default_rng` to instantiate a ``Generator``.
|
| 765 |
+
If `rand` is ``False``, the argument is ignored.
|
| 766 |
+
|
| 767 |
+
Returns
|
| 768 |
+
-------
|
| 769 |
+
float
|
| 770 |
+
Spectral norm estimate.
|
| 771 |
+
"""
|
| 772 |
+
from scipy.sparse.linalg import aslinearoperator
|
| 773 |
+
rng = np.random.default_rng(rng)
|
| 774 |
+
A = aslinearoperator(A)
|
| 775 |
+
|
| 776 |
+
if _is_real(A):
|
| 777 |
+
return _backend.idd_snorm(A, its=its, rng=rng)
|
| 778 |
+
else:
|
| 779 |
+
return _backend.idz_snorm(A, its=its, rng=rng)
|
| 780 |
+
|
| 781 |
+
|
| 782 |
+
def estimate_spectral_norm_diff(A, B, its=20, rng=None):
|
| 783 |
+
"""
|
| 784 |
+
Estimate spectral norm of the difference of two matrices by the randomized
|
| 785 |
+
power method.
|
| 786 |
+
|
| 787 |
+
.. This function automatically detects the matrix data type and calls the
|
| 788 |
+
appropriate backend. For details, see :func:`_backend.idd_diffsnorm` and
|
| 789 |
+
:func:`_backend.idz_diffsnorm`.
|
| 790 |
+
|
| 791 |
+
Parameters
|
| 792 |
+
----------
|
| 793 |
+
A : :class:`scipy.sparse.linalg.LinearOperator`
|
| 794 |
+
First matrix given as a :class:`scipy.sparse.linalg.LinearOperator` with the
|
| 795 |
+
`matvec` and `rmatvec` methods (to apply the matrix and its adjoint).
|
| 796 |
+
B : :class:`scipy.sparse.linalg.LinearOperator`
|
| 797 |
+
Second matrix given as a :class:`scipy.sparse.linalg.LinearOperator` with
|
| 798 |
+
the `matvec` and `rmatvec` methods (to apply the matrix and its adjoint).
|
| 799 |
+
its : int, optional
|
| 800 |
+
Number of power method iterations.
|
| 801 |
+
rng : `numpy.random.Generator`, optional
|
| 802 |
+
Pseudorandom number generator state. When `rng` is None, a new
|
| 803 |
+
`numpy.random.Generator` is created using entropy from the
|
| 804 |
+
operating system. Types other than `numpy.random.Generator` are
|
| 805 |
+
passed to `numpy.random.default_rng` to instantiate a ``Generator``.
|
| 806 |
+
If `rand` is ``False``, the argument is ignored.
|
| 807 |
+
|
| 808 |
+
Returns
|
| 809 |
+
-------
|
| 810 |
+
float
|
| 811 |
+
Spectral norm estimate of matrix difference.
|
| 812 |
+
"""
|
| 813 |
+
from scipy.sparse.linalg import aslinearoperator
|
| 814 |
+
rng = np.random.default_rng(rng)
|
| 815 |
+
A = aslinearoperator(A)
|
| 816 |
+
B = aslinearoperator(B)
|
| 817 |
+
|
| 818 |
+
if _is_real(A):
|
| 819 |
+
return _backend.idd_diffsnorm(A, B, its=its, rng=rng)
|
| 820 |
+
else:
|
| 821 |
+
return _backend.idz_diffsnorm(A, B, its=its, rng=rng)
|
| 822 |
+
|
| 823 |
+
|
| 824 |
+
def svd(A, eps_or_k, rand=True, rng=None):
|
| 825 |
+
"""
|
| 826 |
+
Compute SVD of a matrix via an ID.
|
| 827 |
+
|
| 828 |
+
An SVD of a matrix `A` is a factorization::
|
| 829 |
+
|
| 830 |
+
A = U @ np.diag(S) @ V.conj().T
|
| 831 |
+
|
| 832 |
+
where `U` and `V` have orthonormal columns and `S` is nonnegative.
|
| 833 |
+
|
| 834 |
+
The SVD can be computed to any relative precision or rank (depending on the
|
| 835 |
+
value of `eps_or_k`).
|
| 836 |
+
|
| 837 |
+
See also :func:`interp_decomp` and :func:`id_to_svd`.
|
| 838 |
+
|
| 839 |
+
.. This function automatically detects the form of the input parameters and
|
| 840 |
+
passes them to the appropriate backend. For details, see
|
| 841 |
+
:func:`_backend.iddp_svd`, :func:`_backend.iddp_asvd`,
|
| 842 |
+
:func:`_backend.iddp_rsvd`, :func:`_backend.iddr_svd`,
|
| 843 |
+
:func:`_backend.iddr_asvd`, :func:`_backend.iddr_rsvd`,
|
| 844 |
+
:func:`_backend.idzp_svd`, :func:`_backend.idzp_asvd`,
|
| 845 |
+
:func:`_backend.idzp_rsvd`, :func:`_backend.idzr_svd`,
|
| 846 |
+
:func:`_backend.idzr_asvd`, and :func:`_backend.idzr_rsvd`.
|
| 847 |
+
|
| 848 |
+
Parameters
|
| 849 |
+
----------
|
| 850 |
+
A : :class:`numpy.ndarray` or :class:`scipy.sparse.linalg.LinearOperator`
|
| 851 |
+
Matrix to be factored, given as either a :class:`numpy.ndarray` or a
|
| 852 |
+
:class:`scipy.sparse.linalg.LinearOperator` with the `matvec` and
|
| 853 |
+
`rmatvec` methods (to apply the matrix and its adjoint).
|
| 854 |
+
eps_or_k : float or int
|
| 855 |
+
Relative error (if ``eps_or_k < 1``) or rank (if ``eps_or_k >= 1``) of
|
| 856 |
+
approximation.
|
| 857 |
+
rand : bool, optional
|
| 858 |
+
Whether to use random sampling if `A` is of type :class:`numpy.ndarray`
|
| 859 |
+
(randomized algorithms are always used if `A` is of type
|
| 860 |
+
:class:`scipy.sparse.linalg.LinearOperator`).
|
| 861 |
+
rng : `numpy.random.Generator`, optional
|
| 862 |
+
Pseudorandom number generator state. When `rng` is None, a new
|
| 863 |
+
`numpy.random.Generator` is created using entropy from the
|
| 864 |
+
operating system. Types other than `numpy.random.Generator` are
|
| 865 |
+
passed to `numpy.random.default_rng` to instantiate a ``Generator``.
|
| 866 |
+
If `rand` is ``False``, the argument is ignored.
|
| 867 |
+
|
| 868 |
+
Returns
|
| 869 |
+
-------
|
| 870 |
+
U : :class:`numpy.ndarray`
|
| 871 |
+
2D array of left singular vectors.
|
| 872 |
+
S : :class:`numpy.ndarray`
|
| 873 |
+
1D array of singular values.
|
| 874 |
+
V : :class:`numpy.ndarray`
|
| 875 |
+
2D array right singular vectors.
|
| 876 |
+
"""
|
| 877 |
+
from scipy.sparse.linalg import LinearOperator
|
| 878 |
+
rng = np.random.default_rng(rng)
|
| 879 |
+
|
| 880 |
+
real = _is_real(A)
|
| 881 |
+
|
| 882 |
+
if isinstance(A, np.ndarray):
|
| 883 |
+
A = _C_contiguous_copy(A)
|
| 884 |
+
if eps_or_k < 1:
|
| 885 |
+
eps = eps_or_k
|
| 886 |
+
if rand:
|
| 887 |
+
if real:
|
| 888 |
+
U, S, V = _backend.iddp_asvd(A, eps, rng=rng)
|
| 889 |
+
else:
|
| 890 |
+
U, S, V = _backend.idzp_asvd(A, eps, rng=rng)
|
| 891 |
+
else:
|
| 892 |
+
if real:
|
| 893 |
+
U, S, V = _backend.iddp_svd(A, eps)
|
| 894 |
+
V = V.T.conj()
|
| 895 |
+
else:
|
| 896 |
+
U, S, V = _backend.idzp_svd(A, eps)
|
| 897 |
+
V = V.T.conj()
|
| 898 |
+
else:
|
| 899 |
+
k = int(eps_or_k)
|
| 900 |
+
if k > min(A.shape):
|
| 901 |
+
raise ValueError(f"Approximation rank {k} exceeds min(A.shape) = "
|
| 902 |
+
f" {min(A.shape)} ")
|
| 903 |
+
if rand:
|
| 904 |
+
if real:
|
| 905 |
+
U, S, V = _backend.iddr_asvd(A, k, rng=rng)
|
| 906 |
+
else:
|
| 907 |
+
U, S, V = _backend.idzr_asvd(A, k, rng=rng)
|
| 908 |
+
else:
|
| 909 |
+
if real:
|
| 910 |
+
U, S, V = _backend.iddr_svd(A, k)
|
| 911 |
+
V = V.T.conj()
|
| 912 |
+
else:
|
| 913 |
+
U, S, V = _backend.idzr_svd(A, k)
|
| 914 |
+
V = V.T.conj()
|
| 915 |
+
elif isinstance(A, LinearOperator):
|
| 916 |
+
if eps_or_k < 1:
|
| 917 |
+
eps = eps_or_k
|
| 918 |
+
if real:
|
| 919 |
+
U, S, V = _backend.iddp_rsvd(A, eps, rng=rng)
|
| 920 |
+
else:
|
| 921 |
+
U, S, V = _backend.idzp_rsvd(A, eps, rng=rng)
|
| 922 |
+
else:
|
| 923 |
+
k = int(eps_or_k)
|
| 924 |
+
if real:
|
| 925 |
+
U, S, V = _backend.iddr_rsvd(A, k, rng=rng)
|
| 926 |
+
else:
|
| 927 |
+
U, S, V = _backend.idzr_rsvd(A, k, rng=rng)
|
| 928 |
+
else:
|
| 929 |
+
raise _TYPE_ERROR
|
| 930 |
+
return U, S, V
|
| 931 |
+
|
| 932 |
+
|
| 933 |
+
def estimate_rank(A, eps, rng=None):
|
| 934 |
+
"""
|
| 935 |
+
Estimate matrix rank to a specified relative precision using randomized
|
| 936 |
+
methods.
|
| 937 |
+
|
| 938 |
+
The matrix `A` can be given as either a :class:`numpy.ndarray` or a
|
| 939 |
+
:class:`scipy.sparse.linalg.LinearOperator`, with different algorithms used
|
| 940 |
+
for each case. If `A` is of type :class:`numpy.ndarray`, then the output
|
| 941 |
+
rank is typically about 8 higher than the actual numerical rank.
|
| 942 |
+
|
| 943 |
+
.. This function automatically detects the form of the input parameters and
|
| 944 |
+
passes them to the appropriate backend. For details,
|
| 945 |
+
see :func:`_backend.idd_estrank`, :func:`_backend.idd_findrank`,
|
| 946 |
+
:func:`_backend.idz_estrank`, and :func:`_backend.idz_findrank`.
|
| 947 |
+
|
| 948 |
+
Parameters
|
| 949 |
+
----------
|
| 950 |
+
A : :class:`numpy.ndarray` or :class:`scipy.sparse.linalg.LinearOperator`
|
| 951 |
+
Matrix whose rank is to be estimated, given as either a
|
| 952 |
+
:class:`numpy.ndarray` or a :class:`scipy.sparse.linalg.LinearOperator`
|
| 953 |
+
with the `rmatvec` method (to apply the matrix adjoint).
|
| 954 |
+
eps : float
|
| 955 |
+
Relative error for numerical rank definition.
|
| 956 |
+
rng : `numpy.random.Generator`, optional
|
| 957 |
+
Pseudorandom number generator state. When `rng` is None, a new
|
| 958 |
+
`numpy.random.Generator` is created using entropy from the
|
| 959 |
+
operating system. Types other than `numpy.random.Generator` are
|
| 960 |
+
passed to `numpy.random.default_rng` to instantiate a ``Generator``.
|
| 961 |
+
If `rand` is ``False``, the argument is ignored.
|
| 962 |
+
|
| 963 |
+
Returns
|
| 964 |
+
-------
|
| 965 |
+
int
|
| 966 |
+
Estimated matrix rank.
|
| 967 |
+
"""
|
| 968 |
+
from scipy.sparse.linalg import LinearOperator
|
| 969 |
+
|
| 970 |
+
rng = np.random.default_rng(rng)
|
| 971 |
+
real = _is_real(A)
|
| 972 |
+
|
| 973 |
+
if isinstance(A, np.ndarray):
|
| 974 |
+
A = _C_contiguous_copy(A)
|
| 975 |
+
if real:
|
| 976 |
+
rank, _ = _backend.idd_estrank(A, eps, rng=rng)
|
| 977 |
+
else:
|
| 978 |
+
rank, _ = _backend.idz_estrank(A, eps, rng=rng)
|
| 979 |
+
if rank == 0:
|
| 980 |
+
# special return value for nearly full rank
|
| 981 |
+
rank = min(A.shape)
|
| 982 |
+
return rank
|
| 983 |
+
elif isinstance(A, LinearOperator):
|
| 984 |
+
if real:
|
| 985 |
+
return _backend.idd_findrank(A, eps, rng=rng)[0]
|
| 986 |
+
else:
|
| 987 |
+
return _backend.idz_findrank(A, eps, rng=rng)[0]
|
| 988 |
+
else:
|
| 989 |
+
raise _TYPE_ERROR
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/lapack.py
ADDED
|
@@ -0,0 +1,1061 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Low-level LAPACK functions (:mod:`scipy.linalg.lapack`)
|
| 3 |
+
=======================================================
|
| 4 |
+
|
| 5 |
+
This module contains low-level functions from the LAPACK library.
|
| 6 |
+
|
| 7 |
+
.. versionadded:: 0.12.0
|
| 8 |
+
|
| 9 |
+
.. note::
|
| 10 |
+
|
| 11 |
+
The common ``overwrite_<>`` option in many routines, allows the
|
| 12 |
+
input arrays to be overwritten to avoid extra memory allocation.
|
| 13 |
+
However this requires the array to satisfy two conditions
|
| 14 |
+
which are memory order and the data type to match exactly the
|
| 15 |
+
order and the type expected by the routine.
|
| 16 |
+
|
| 17 |
+
As an example, if you pass a double precision float array to any
|
| 18 |
+
``S....`` routine which expects single precision arguments, f2py
|
| 19 |
+
will create an intermediate array to match the argument types and
|
| 20 |
+
overwriting will be performed on that intermediate array.
|
| 21 |
+
|
| 22 |
+
Similarly, if a C-contiguous array is passed, f2py will pass a
|
| 23 |
+
FORTRAN-contiguous array internally. Please make sure that these
|
| 24 |
+
details are satisfied. More information can be found in the f2py
|
| 25 |
+
documentation.
|
| 26 |
+
|
| 27 |
+
.. warning::
|
| 28 |
+
|
| 29 |
+
These functions do little to no error checking.
|
| 30 |
+
It is possible to cause crashes by mis-using them,
|
| 31 |
+
so prefer using the higher-level routines in `scipy.linalg`.
|
| 32 |
+
|
| 33 |
+
Finding functions
|
| 34 |
+
-----------------
|
| 35 |
+
|
| 36 |
+
.. autosummary::
|
| 37 |
+
:toctree: generated/
|
| 38 |
+
|
| 39 |
+
get_lapack_funcs
|
| 40 |
+
|
| 41 |
+
All functions
|
| 42 |
+
-------------
|
| 43 |
+
|
| 44 |
+
.. autosummary::
|
| 45 |
+
:toctree: generated/
|
| 46 |
+
|
| 47 |
+
sgbsv
|
| 48 |
+
dgbsv
|
| 49 |
+
cgbsv
|
| 50 |
+
zgbsv
|
| 51 |
+
|
| 52 |
+
sgbtrf
|
| 53 |
+
dgbtrf
|
| 54 |
+
cgbtrf
|
| 55 |
+
zgbtrf
|
| 56 |
+
|
| 57 |
+
sgbtrs
|
| 58 |
+
dgbtrs
|
| 59 |
+
cgbtrs
|
| 60 |
+
zgbtrs
|
| 61 |
+
|
| 62 |
+
sgebal
|
| 63 |
+
dgebal
|
| 64 |
+
cgebal
|
| 65 |
+
zgebal
|
| 66 |
+
|
| 67 |
+
sgecon
|
| 68 |
+
dgecon
|
| 69 |
+
cgecon
|
| 70 |
+
zgecon
|
| 71 |
+
|
| 72 |
+
sgeequ
|
| 73 |
+
dgeequ
|
| 74 |
+
cgeequ
|
| 75 |
+
zgeequ
|
| 76 |
+
|
| 77 |
+
sgeequb
|
| 78 |
+
dgeequb
|
| 79 |
+
cgeequb
|
| 80 |
+
zgeequb
|
| 81 |
+
|
| 82 |
+
sgees
|
| 83 |
+
dgees
|
| 84 |
+
cgees
|
| 85 |
+
zgees
|
| 86 |
+
|
| 87 |
+
sgeev
|
| 88 |
+
dgeev
|
| 89 |
+
cgeev
|
| 90 |
+
zgeev
|
| 91 |
+
|
| 92 |
+
sgeev_lwork
|
| 93 |
+
dgeev_lwork
|
| 94 |
+
cgeev_lwork
|
| 95 |
+
zgeev_lwork
|
| 96 |
+
|
| 97 |
+
sgehrd
|
| 98 |
+
dgehrd
|
| 99 |
+
cgehrd
|
| 100 |
+
zgehrd
|
| 101 |
+
|
| 102 |
+
sgehrd_lwork
|
| 103 |
+
dgehrd_lwork
|
| 104 |
+
cgehrd_lwork
|
| 105 |
+
zgehrd_lwork
|
| 106 |
+
|
| 107 |
+
sgejsv
|
| 108 |
+
dgejsv
|
| 109 |
+
|
| 110 |
+
sgels
|
| 111 |
+
dgels
|
| 112 |
+
cgels
|
| 113 |
+
zgels
|
| 114 |
+
|
| 115 |
+
sgels_lwork
|
| 116 |
+
dgels_lwork
|
| 117 |
+
cgels_lwork
|
| 118 |
+
zgels_lwork
|
| 119 |
+
|
| 120 |
+
sgelsd
|
| 121 |
+
dgelsd
|
| 122 |
+
cgelsd
|
| 123 |
+
zgelsd
|
| 124 |
+
|
| 125 |
+
sgelsd_lwork
|
| 126 |
+
dgelsd_lwork
|
| 127 |
+
cgelsd_lwork
|
| 128 |
+
zgelsd_lwork
|
| 129 |
+
|
| 130 |
+
sgelss
|
| 131 |
+
dgelss
|
| 132 |
+
cgelss
|
| 133 |
+
zgelss
|
| 134 |
+
|
| 135 |
+
sgelss_lwork
|
| 136 |
+
dgelss_lwork
|
| 137 |
+
cgelss_lwork
|
| 138 |
+
zgelss_lwork
|
| 139 |
+
|
| 140 |
+
sgelsy
|
| 141 |
+
dgelsy
|
| 142 |
+
cgelsy
|
| 143 |
+
zgelsy
|
| 144 |
+
|
| 145 |
+
sgelsy_lwork
|
| 146 |
+
dgelsy_lwork
|
| 147 |
+
cgelsy_lwork
|
| 148 |
+
zgelsy_lwork
|
| 149 |
+
|
| 150 |
+
sgeqp3
|
| 151 |
+
dgeqp3
|
| 152 |
+
cgeqp3
|
| 153 |
+
zgeqp3
|
| 154 |
+
|
| 155 |
+
sgeqrf
|
| 156 |
+
dgeqrf
|
| 157 |
+
cgeqrf
|
| 158 |
+
zgeqrf
|
| 159 |
+
|
| 160 |
+
sgeqrf_lwork
|
| 161 |
+
dgeqrf_lwork
|
| 162 |
+
cgeqrf_lwork
|
| 163 |
+
zgeqrf_lwork
|
| 164 |
+
|
| 165 |
+
sgeqrfp
|
| 166 |
+
dgeqrfp
|
| 167 |
+
cgeqrfp
|
| 168 |
+
zgeqrfp
|
| 169 |
+
|
| 170 |
+
sgeqrfp_lwork
|
| 171 |
+
dgeqrfp_lwork
|
| 172 |
+
cgeqrfp_lwork
|
| 173 |
+
zgeqrfp_lwork
|
| 174 |
+
|
| 175 |
+
sgerqf
|
| 176 |
+
dgerqf
|
| 177 |
+
cgerqf
|
| 178 |
+
zgerqf
|
| 179 |
+
|
| 180 |
+
sgesdd
|
| 181 |
+
dgesdd
|
| 182 |
+
cgesdd
|
| 183 |
+
zgesdd
|
| 184 |
+
|
| 185 |
+
sgesdd_lwork
|
| 186 |
+
dgesdd_lwork
|
| 187 |
+
cgesdd_lwork
|
| 188 |
+
zgesdd_lwork
|
| 189 |
+
|
| 190 |
+
sgesv
|
| 191 |
+
dgesv
|
| 192 |
+
cgesv
|
| 193 |
+
zgesv
|
| 194 |
+
|
| 195 |
+
sgesvd
|
| 196 |
+
dgesvd
|
| 197 |
+
cgesvd
|
| 198 |
+
zgesvd
|
| 199 |
+
|
| 200 |
+
sgesvd_lwork
|
| 201 |
+
dgesvd_lwork
|
| 202 |
+
cgesvd_lwork
|
| 203 |
+
zgesvd_lwork
|
| 204 |
+
|
| 205 |
+
sgesvx
|
| 206 |
+
dgesvx
|
| 207 |
+
cgesvx
|
| 208 |
+
zgesvx
|
| 209 |
+
|
| 210 |
+
sgetrf
|
| 211 |
+
dgetrf
|
| 212 |
+
cgetrf
|
| 213 |
+
zgetrf
|
| 214 |
+
|
| 215 |
+
sgetc2
|
| 216 |
+
dgetc2
|
| 217 |
+
cgetc2
|
| 218 |
+
zgetc2
|
| 219 |
+
|
| 220 |
+
sgetri
|
| 221 |
+
dgetri
|
| 222 |
+
cgetri
|
| 223 |
+
zgetri
|
| 224 |
+
|
| 225 |
+
sgetri_lwork
|
| 226 |
+
dgetri_lwork
|
| 227 |
+
cgetri_lwork
|
| 228 |
+
zgetri_lwork
|
| 229 |
+
|
| 230 |
+
sgetrs
|
| 231 |
+
dgetrs
|
| 232 |
+
cgetrs
|
| 233 |
+
zgetrs
|
| 234 |
+
|
| 235 |
+
sgesc2
|
| 236 |
+
dgesc2
|
| 237 |
+
cgesc2
|
| 238 |
+
zgesc2
|
| 239 |
+
|
| 240 |
+
sgges
|
| 241 |
+
dgges
|
| 242 |
+
cgges
|
| 243 |
+
zgges
|
| 244 |
+
|
| 245 |
+
sggev
|
| 246 |
+
dggev
|
| 247 |
+
cggev
|
| 248 |
+
zggev
|
| 249 |
+
|
| 250 |
+
sgglse
|
| 251 |
+
dgglse
|
| 252 |
+
cgglse
|
| 253 |
+
zgglse
|
| 254 |
+
|
| 255 |
+
sgglse_lwork
|
| 256 |
+
dgglse_lwork
|
| 257 |
+
cgglse_lwork
|
| 258 |
+
zgglse_lwork
|
| 259 |
+
|
| 260 |
+
sgtsv
|
| 261 |
+
dgtsv
|
| 262 |
+
cgtsv
|
| 263 |
+
zgtsv
|
| 264 |
+
|
| 265 |
+
sgtsvx
|
| 266 |
+
dgtsvx
|
| 267 |
+
cgtsvx
|
| 268 |
+
zgtsvx
|
| 269 |
+
|
| 270 |
+
chbevd
|
| 271 |
+
zhbevd
|
| 272 |
+
|
| 273 |
+
chbevx
|
| 274 |
+
zhbevx
|
| 275 |
+
|
| 276 |
+
checon
|
| 277 |
+
zhecon
|
| 278 |
+
|
| 279 |
+
cheequb
|
| 280 |
+
zheequb
|
| 281 |
+
|
| 282 |
+
cheev
|
| 283 |
+
zheev
|
| 284 |
+
|
| 285 |
+
cheev_lwork
|
| 286 |
+
zheev_lwork
|
| 287 |
+
|
| 288 |
+
cheevd
|
| 289 |
+
zheevd
|
| 290 |
+
|
| 291 |
+
cheevd_lwork
|
| 292 |
+
zheevd_lwork
|
| 293 |
+
|
| 294 |
+
cheevr
|
| 295 |
+
zheevr
|
| 296 |
+
|
| 297 |
+
cheevr_lwork
|
| 298 |
+
zheevr_lwork
|
| 299 |
+
|
| 300 |
+
cheevx
|
| 301 |
+
zheevx
|
| 302 |
+
|
| 303 |
+
cheevx_lwork
|
| 304 |
+
zheevx_lwork
|
| 305 |
+
|
| 306 |
+
chegst
|
| 307 |
+
zhegst
|
| 308 |
+
|
| 309 |
+
chegv
|
| 310 |
+
zhegv
|
| 311 |
+
|
| 312 |
+
chegv_lwork
|
| 313 |
+
zhegv_lwork
|
| 314 |
+
|
| 315 |
+
chegvd
|
| 316 |
+
zhegvd
|
| 317 |
+
|
| 318 |
+
chegvx
|
| 319 |
+
zhegvx
|
| 320 |
+
|
| 321 |
+
chegvx_lwork
|
| 322 |
+
zhegvx_lwork
|
| 323 |
+
|
| 324 |
+
chesv
|
| 325 |
+
zhesv
|
| 326 |
+
|
| 327 |
+
chesv_lwork
|
| 328 |
+
zhesv_lwork
|
| 329 |
+
|
| 330 |
+
chesvx
|
| 331 |
+
zhesvx
|
| 332 |
+
|
| 333 |
+
chesvx_lwork
|
| 334 |
+
zhesvx_lwork
|
| 335 |
+
|
| 336 |
+
chetrd
|
| 337 |
+
zhetrd
|
| 338 |
+
|
| 339 |
+
chetrd_lwork
|
| 340 |
+
zhetrd_lwork
|
| 341 |
+
|
| 342 |
+
chetrf
|
| 343 |
+
zhetrf
|
| 344 |
+
|
| 345 |
+
chetrf_lwork
|
| 346 |
+
zhetrf_lwork
|
| 347 |
+
|
| 348 |
+
chetrs
|
| 349 |
+
zhetrs
|
| 350 |
+
|
| 351 |
+
chfrk
|
| 352 |
+
zhfrk
|
| 353 |
+
|
| 354 |
+
slamch
|
| 355 |
+
dlamch
|
| 356 |
+
|
| 357 |
+
slange
|
| 358 |
+
dlange
|
| 359 |
+
clange
|
| 360 |
+
zlange
|
| 361 |
+
|
| 362 |
+
slantr
|
| 363 |
+
dlantr
|
| 364 |
+
clantr
|
| 365 |
+
zlantr
|
| 366 |
+
|
| 367 |
+
slarf
|
| 368 |
+
dlarf
|
| 369 |
+
clarf
|
| 370 |
+
zlarf
|
| 371 |
+
|
| 372 |
+
slarfg
|
| 373 |
+
dlarfg
|
| 374 |
+
clarfg
|
| 375 |
+
zlarfg
|
| 376 |
+
|
| 377 |
+
slartg
|
| 378 |
+
dlartg
|
| 379 |
+
clartg
|
| 380 |
+
zlartg
|
| 381 |
+
|
| 382 |
+
slasd4
|
| 383 |
+
dlasd4
|
| 384 |
+
|
| 385 |
+
slaswp
|
| 386 |
+
dlaswp
|
| 387 |
+
claswp
|
| 388 |
+
zlaswp
|
| 389 |
+
|
| 390 |
+
slauum
|
| 391 |
+
dlauum
|
| 392 |
+
clauum
|
| 393 |
+
zlauum
|
| 394 |
+
|
| 395 |
+
sorcsd
|
| 396 |
+
dorcsd
|
| 397 |
+
sorcsd_lwork
|
| 398 |
+
dorcsd_lwork
|
| 399 |
+
|
| 400 |
+
sorghr
|
| 401 |
+
dorghr
|
| 402 |
+
sorghr_lwork
|
| 403 |
+
dorghr_lwork
|
| 404 |
+
|
| 405 |
+
sorgqr
|
| 406 |
+
dorgqr
|
| 407 |
+
|
| 408 |
+
sorgrq
|
| 409 |
+
dorgrq
|
| 410 |
+
|
| 411 |
+
sormqr
|
| 412 |
+
dormqr
|
| 413 |
+
|
| 414 |
+
sormrz
|
| 415 |
+
dormrz
|
| 416 |
+
|
| 417 |
+
sormrz_lwork
|
| 418 |
+
dormrz_lwork
|
| 419 |
+
|
| 420 |
+
spbsv
|
| 421 |
+
dpbsv
|
| 422 |
+
cpbsv
|
| 423 |
+
zpbsv
|
| 424 |
+
|
| 425 |
+
spbtrf
|
| 426 |
+
dpbtrf
|
| 427 |
+
cpbtrf
|
| 428 |
+
zpbtrf
|
| 429 |
+
|
| 430 |
+
spbtrs
|
| 431 |
+
dpbtrs
|
| 432 |
+
cpbtrs
|
| 433 |
+
zpbtrs
|
| 434 |
+
|
| 435 |
+
spftrf
|
| 436 |
+
dpftrf
|
| 437 |
+
cpftrf
|
| 438 |
+
zpftrf
|
| 439 |
+
|
| 440 |
+
spftri
|
| 441 |
+
dpftri
|
| 442 |
+
cpftri
|
| 443 |
+
zpftri
|
| 444 |
+
|
| 445 |
+
spftrs
|
| 446 |
+
dpftrs
|
| 447 |
+
cpftrs
|
| 448 |
+
zpftrs
|
| 449 |
+
|
| 450 |
+
spocon
|
| 451 |
+
dpocon
|
| 452 |
+
cpocon
|
| 453 |
+
zpocon
|
| 454 |
+
|
| 455 |
+
spstrf
|
| 456 |
+
dpstrf
|
| 457 |
+
cpstrf
|
| 458 |
+
zpstrf
|
| 459 |
+
|
| 460 |
+
spstf2
|
| 461 |
+
dpstf2
|
| 462 |
+
cpstf2
|
| 463 |
+
zpstf2
|
| 464 |
+
|
| 465 |
+
sposv
|
| 466 |
+
dposv
|
| 467 |
+
cposv
|
| 468 |
+
zposv
|
| 469 |
+
|
| 470 |
+
sposvx
|
| 471 |
+
dposvx
|
| 472 |
+
cposvx
|
| 473 |
+
zposvx
|
| 474 |
+
|
| 475 |
+
spotrf
|
| 476 |
+
dpotrf
|
| 477 |
+
cpotrf
|
| 478 |
+
zpotrf
|
| 479 |
+
|
| 480 |
+
spotri
|
| 481 |
+
dpotri
|
| 482 |
+
cpotri
|
| 483 |
+
zpotri
|
| 484 |
+
|
| 485 |
+
spotrs
|
| 486 |
+
dpotrs
|
| 487 |
+
cpotrs
|
| 488 |
+
zpotrs
|
| 489 |
+
|
| 490 |
+
sppcon
|
| 491 |
+
dppcon
|
| 492 |
+
cppcon
|
| 493 |
+
zppcon
|
| 494 |
+
|
| 495 |
+
sppsv
|
| 496 |
+
dppsv
|
| 497 |
+
cppsv
|
| 498 |
+
zppsv
|
| 499 |
+
|
| 500 |
+
spptrf
|
| 501 |
+
dpptrf
|
| 502 |
+
cpptrf
|
| 503 |
+
zpptrf
|
| 504 |
+
|
| 505 |
+
spptri
|
| 506 |
+
dpptri
|
| 507 |
+
cpptri
|
| 508 |
+
zpptri
|
| 509 |
+
|
| 510 |
+
spptrs
|
| 511 |
+
dpptrs
|
| 512 |
+
cpptrs
|
| 513 |
+
zpptrs
|
| 514 |
+
|
| 515 |
+
sptsv
|
| 516 |
+
dptsv
|
| 517 |
+
cptsv
|
| 518 |
+
zptsv
|
| 519 |
+
|
| 520 |
+
sptsvx
|
| 521 |
+
dptsvx
|
| 522 |
+
cptsvx
|
| 523 |
+
zptsvx
|
| 524 |
+
|
| 525 |
+
spttrf
|
| 526 |
+
dpttrf
|
| 527 |
+
cpttrf
|
| 528 |
+
zpttrf
|
| 529 |
+
|
| 530 |
+
spttrs
|
| 531 |
+
dpttrs
|
| 532 |
+
cpttrs
|
| 533 |
+
zpttrs
|
| 534 |
+
|
| 535 |
+
spteqr
|
| 536 |
+
dpteqr
|
| 537 |
+
cpteqr
|
| 538 |
+
zpteqr
|
| 539 |
+
|
| 540 |
+
crot
|
| 541 |
+
zrot
|
| 542 |
+
|
| 543 |
+
ssbev
|
| 544 |
+
dsbev
|
| 545 |
+
|
| 546 |
+
ssbevd
|
| 547 |
+
dsbevd
|
| 548 |
+
|
| 549 |
+
ssbevx
|
| 550 |
+
dsbevx
|
| 551 |
+
|
| 552 |
+
ssfrk
|
| 553 |
+
dsfrk
|
| 554 |
+
|
| 555 |
+
sstebz
|
| 556 |
+
dstebz
|
| 557 |
+
|
| 558 |
+
sstein
|
| 559 |
+
dstein
|
| 560 |
+
|
| 561 |
+
sstemr
|
| 562 |
+
dstemr
|
| 563 |
+
|
| 564 |
+
sstemr_lwork
|
| 565 |
+
dstemr_lwork
|
| 566 |
+
|
| 567 |
+
ssterf
|
| 568 |
+
dsterf
|
| 569 |
+
|
| 570 |
+
sstev
|
| 571 |
+
dstev
|
| 572 |
+
|
| 573 |
+
ssycon
|
| 574 |
+
dsycon
|
| 575 |
+
csycon
|
| 576 |
+
zsycon
|
| 577 |
+
|
| 578 |
+
ssyconv
|
| 579 |
+
dsyconv
|
| 580 |
+
csyconv
|
| 581 |
+
zsyconv
|
| 582 |
+
|
| 583 |
+
ssyequb
|
| 584 |
+
dsyequb
|
| 585 |
+
csyequb
|
| 586 |
+
zsyequb
|
| 587 |
+
|
| 588 |
+
ssyev
|
| 589 |
+
dsyev
|
| 590 |
+
|
| 591 |
+
ssyev_lwork
|
| 592 |
+
dsyev_lwork
|
| 593 |
+
|
| 594 |
+
ssyevd
|
| 595 |
+
dsyevd
|
| 596 |
+
|
| 597 |
+
ssyevd_lwork
|
| 598 |
+
dsyevd_lwork
|
| 599 |
+
|
| 600 |
+
ssyevr
|
| 601 |
+
dsyevr
|
| 602 |
+
|
| 603 |
+
ssyevr_lwork
|
| 604 |
+
dsyevr_lwork
|
| 605 |
+
|
| 606 |
+
ssyevx
|
| 607 |
+
dsyevx
|
| 608 |
+
|
| 609 |
+
ssyevx_lwork
|
| 610 |
+
dsyevx_lwork
|
| 611 |
+
|
| 612 |
+
ssygst
|
| 613 |
+
dsygst
|
| 614 |
+
|
| 615 |
+
ssygv
|
| 616 |
+
dsygv
|
| 617 |
+
|
| 618 |
+
ssygv_lwork
|
| 619 |
+
dsygv_lwork
|
| 620 |
+
|
| 621 |
+
ssygvd
|
| 622 |
+
dsygvd
|
| 623 |
+
|
| 624 |
+
ssygvx
|
| 625 |
+
dsygvx
|
| 626 |
+
|
| 627 |
+
ssygvx_lwork
|
| 628 |
+
dsygvx_lwork
|
| 629 |
+
|
| 630 |
+
ssysv
|
| 631 |
+
dsysv
|
| 632 |
+
csysv
|
| 633 |
+
zsysv
|
| 634 |
+
|
| 635 |
+
ssysv_lwork
|
| 636 |
+
dsysv_lwork
|
| 637 |
+
csysv_lwork
|
| 638 |
+
zsysv_lwork
|
| 639 |
+
|
| 640 |
+
ssysvx
|
| 641 |
+
dsysvx
|
| 642 |
+
csysvx
|
| 643 |
+
zsysvx
|
| 644 |
+
|
| 645 |
+
ssysvx_lwork
|
| 646 |
+
dsysvx_lwork
|
| 647 |
+
csysvx_lwork
|
| 648 |
+
zsysvx_lwork
|
| 649 |
+
|
| 650 |
+
ssytf2
|
| 651 |
+
dsytf2
|
| 652 |
+
csytf2
|
| 653 |
+
zsytf2
|
| 654 |
+
|
| 655 |
+
ssytrd
|
| 656 |
+
dsytrd
|
| 657 |
+
|
| 658 |
+
ssytrd_lwork
|
| 659 |
+
dsytrd_lwork
|
| 660 |
+
|
| 661 |
+
ssytrf
|
| 662 |
+
dsytrf
|
| 663 |
+
csytrf
|
| 664 |
+
zsytrf
|
| 665 |
+
|
| 666 |
+
ssytrf_lwork
|
| 667 |
+
dsytrf_lwork
|
| 668 |
+
csytrf_lwork
|
| 669 |
+
zsytrf_lwork
|
| 670 |
+
|
| 671 |
+
ssytrs
|
| 672 |
+
dsytrs
|
| 673 |
+
csytrs
|
| 674 |
+
zsytrs
|
| 675 |
+
|
| 676 |
+
stbtrs
|
| 677 |
+
dtbtrs
|
| 678 |
+
ctbtrs
|
| 679 |
+
ztbtrs
|
| 680 |
+
|
| 681 |
+
stfsm
|
| 682 |
+
dtfsm
|
| 683 |
+
ctfsm
|
| 684 |
+
ztfsm
|
| 685 |
+
|
| 686 |
+
stfttp
|
| 687 |
+
dtfttp
|
| 688 |
+
ctfttp
|
| 689 |
+
ztfttp
|
| 690 |
+
|
| 691 |
+
stfttr
|
| 692 |
+
dtfttr
|
| 693 |
+
ctfttr
|
| 694 |
+
ztfttr
|
| 695 |
+
|
| 696 |
+
stgexc
|
| 697 |
+
dtgexc
|
| 698 |
+
ctgexc
|
| 699 |
+
ztgexc
|
| 700 |
+
|
| 701 |
+
stgsen
|
| 702 |
+
dtgsen
|
| 703 |
+
ctgsen
|
| 704 |
+
ztgsen
|
| 705 |
+
|
| 706 |
+
stgsen_lwork
|
| 707 |
+
dtgsen_lwork
|
| 708 |
+
ctgsen_lwork
|
| 709 |
+
ztgsen_lwork
|
| 710 |
+
|
| 711 |
+
stgsyl
|
| 712 |
+
dtgsyl
|
| 713 |
+
|
| 714 |
+
stpttf
|
| 715 |
+
dtpttf
|
| 716 |
+
ctpttf
|
| 717 |
+
ztpttf
|
| 718 |
+
|
| 719 |
+
stpttr
|
| 720 |
+
dtpttr
|
| 721 |
+
ctpttr
|
| 722 |
+
ztpttr
|
| 723 |
+
|
| 724 |
+
strcon
|
| 725 |
+
dtrcon
|
| 726 |
+
ctrcon
|
| 727 |
+
ztrcon
|
| 728 |
+
|
| 729 |
+
strexc
|
| 730 |
+
dtrexc
|
| 731 |
+
ctrexc
|
| 732 |
+
ztrexc
|
| 733 |
+
|
| 734 |
+
strsen
|
| 735 |
+
dtrsen
|
| 736 |
+
ctrsen
|
| 737 |
+
ztrsen
|
| 738 |
+
|
| 739 |
+
strsen_lwork
|
| 740 |
+
dtrsen_lwork
|
| 741 |
+
ctrsen_lwork
|
| 742 |
+
ztrsen_lwork
|
| 743 |
+
|
| 744 |
+
strsyl
|
| 745 |
+
dtrsyl
|
| 746 |
+
ctrsyl
|
| 747 |
+
ztrsyl
|
| 748 |
+
|
| 749 |
+
strtri
|
| 750 |
+
dtrtri
|
| 751 |
+
ctrtri
|
| 752 |
+
ztrtri
|
| 753 |
+
|
| 754 |
+
strtrs
|
| 755 |
+
dtrtrs
|
| 756 |
+
ctrtrs
|
| 757 |
+
ztrtrs
|
| 758 |
+
|
| 759 |
+
strttf
|
| 760 |
+
dtrttf
|
| 761 |
+
ctrttf
|
| 762 |
+
ztrttf
|
| 763 |
+
|
| 764 |
+
strttp
|
| 765 |
+
dtrttp
|
| 766 |
+
ctrttp
|
| 767 |
+
ztrttp
|
| 768 |
+
|
| 769 |
+
stzrzf
|
| 770 |
+
dtzrzf
|
| 771 |
+
ctzrzf
|
| 772 |
+
ztzrzf
|
| 773 |
+
|
| 774 |
+
stzrzf_lwork
|
| 775 |
+
dtzrzf_lwork
|
| 776 |
+
ctzrzf_lwork
|
| 777 |
+
ztzrzf_lwork
|
| 778 |
+
|
| 779 |
+
cunghr
|
| 780 |
+
zunghr
|
| 781 |
+
|
| 782 |
+
cunghr_lwork
|
| 783 |
+
zunghr_lwork
|
| 784 |
+
|
| 785 |
+
cungqr
|
| 786 |
+
zungqr
|
| 787 |
+
|
| 788 |
+
cungrq
|
| 789 |
+
zungrq
|
| 790 |
+
|
| 791 |
+
cunmqr
|
| 792 |
+
zunmqr
|
| 793 |
+
|
| 794 |
+
sgeqrt
|
| 795 |
+
dgeqrt
|
| 796 |
+
cgeqrt
|
| 797 |
+
zgeqrt
|
| 798 |
+
|
| 799 |
+
sgemqrt
|
| 800 |
+
dgemqrt
|
| 801 |
+
cgemqrt
|
| 802 |
+
zgemqrt
|
| 803 |
+
|
| 804 |
+
sgttrf
|
| 805 |
+
dgttrf
|
| 806 |
+
cgttrf
|
| 807 |
+
zgttrf
|
| 808 |
+
|
| 809 |
+
sgttrs
|
| 810 |
+
dgttrs
|
| 811 |
+
cgttrs
|
| 812 |
+
zgttrs
|
| 813 |
+
|
| 814 |
+
sgtcon
|
| 815 |
+
dgtcon
|
| 816 |
+
cgtcon
|
| 817 |
+
zgtcon
|
| 818 |
+
|
| 819 |
+
stpqrt
|
| 820 |
+
dtpqrt
|
| 821 |
+
ctpqrt
|
| 822 |
+
ztpqrt
|
| 823 |
+
|
| 824 |
+
stpmqrt
|
| 825 |
+
dtpmqrt
|
| 826 |
+
ctpmqrt
|
| 827 |
+
ztpmqrt
|
| 828 |
+
|
| 829 |
+
cuncsd
|
| 830 |
+
zuncsd
|
| 831 |
+
|
| 832 |
+
cuncsd_lwork
|
| 833 |
+
zuncsd_lwork
|
| 834 |
+
|
| 835 |
+
cunmrz
|
| 836 |
+
zunmrz
|
| 837 |
+
|
| 838 |
+
cunmrz_lwork
|
| 839 |
+
zunmrz_lwork
|
| 840 |
+
|
| 841 |
+
ilaver
|
| 842 |
+
|
| 843 |
+
"""
|
| 844 |
+
#
|
| 845 |
+
# Author: Pearu Peterson, March 2002
|
| 846 |
+
#
|
| 847 |
+
|
| 848 |
+
import numpy as np
|
| 849 |
+
from .blas import _get_funcs, _memoize_get_funcs
|
| 850 |
+
from scipy.linalg import _flapack
|
| 851 |
+
from re import compile as regex_compile
|
| 852 |
+
try:
|
| 853 |
+
from scipy.linalg import _clapack
|
| 854 |
+
except ImportError:
|
| 855 |
+
_clapack = None
|
| 856 |
+
|
| 857 |
+
try:
|
| 858 |
+
from scipy.linalg import _flapack_64
|
| 859 |
+
HAS_ILP64 = True
|
| 860 |
+
except ImportError:
|
| 861 |
+
HAS_ILP64 = False
|
| 862 |
+
_flapack_64 = None
|
| 863 |
+
|
| 864 |
+
|
| 865 |
+
# Expose all functions (only flapack --- clapack is an implementation detail)
|
| 866 |
+
empty_module = None
|
| 867 |
+
from scipy.linalg._flapack import * # noqa: E402, F403
|
| 868 |
+
del empty_module
|
| 869 |
+
|
| 870 |
+
__all__ = ['get_lapack_funcs']
|
| 871 |
+
|
| 872 |
+
# some convenience alias for complex functions
|
| 873 |
+
_lapack_alias = {
|
| 874 |
+
'corghr': 'cunghr', 'zorghr': 'zunghr',
|
| 875 |
+
'corghr_lwork': 'cunghr_lwork', 'zorghr_lwork': 'zunghr_lwork',
|
| 876 |
+
'corgqr': 'cungqr', 'zorgqr': 'zungqr',
|
| 877 |
+
'cormqr': 'cunmqr', 'zormqr': 'zunmqr',
|
| 878 |
+
'corgrq': 'cungrq', 'zorgrq': 'zungrq',
|
| 879 |
+
}
|
| 880 |
+
|
| 881 |
+
|
| 882 |
+
# Place guards against docstring rendering issues with special characters
|
| 883 |
+
p1 = regex_compile(r'with bounds (?P<b>.*?)( and (?P<s>.*?) storage){0,1}\n')
|
| 884 |
+
p2 = regex_compile(r'Default: (?P<d>.*?)\n')
|
| 885 |
+
|
| 886 |
+
|
| 887 |
+
def backtickrepl(m):
|
| 888 |
+
if m.group('s'):
|
| 889 |
+
return (f"with bounds ``{m.group('b')}`` with ``{m.group('s')}`` storage\n")
|
| 890 |
+
else:
|
| 891 |
+
return f"with bounds ``{m.group('b')}``\n"
|
| 892 |
+
|
| 893 |
+
|
| 894 |
+
for routine in [ssyevr, dsyevr, cheevr, zheevr,
|
| 895 |
+
ssyevx, dsyevx, cheevx, zheevx,
|
| 896 |
+
ssygvd, dsygvd, chegvd, zhegvd]:
|
| 897 |
+
if routine.__doc__:
|
| 898 |
+
routine.__doc__ = p1.sub(backtickrepl, routine.__doc__)
|
| 899 |
+
routine.__doc__ = p2.sub('Default ``\\1``\n', routine.__doc__)
|
| 900 |
+
else:
|
| 901 |
+
continue
|
| 902 |
+
|
| 903 |
+
del regex_compile, p1, p2, backtickrepl
|
| 904 |
+
|
| 905 |
+
|
| 906 |
+
@_memoize_get_funcs
|
| 907 |
+
def get_lapack_funcs(names, arrays=(), dtype=None, ilp64=False):
|
| 908 |
+
"""Return available LAPACK function objects from names.
|
| 909 |
+
|
| 910 |
+
Arrays are used to determine the optimal prefix of LAPACK routines.
|
| 911 |
+
|
| 912 |
+
Parameters
|
| 913 |
+
----------
|
| 914 |
+
names : str or sequence of str
|
| 915 |
+
Name(s) of LAPACK functions without type prefix.
|
| 916 |
+
|
| 917 |
+
arrays : sequence of ndarrays, optional
|
| 918 |
+
Arrays can be given to determine optimal prefix of LAPACK
|
| 919 |
+
routines. If not given, double-precision routines will be
|
| 920 |
+
used, otherwise the most generic type in arrays will be used.
|
| 921 |
+
|
| 922 |
+
dtype : str or dtype, optional
|
| 923 |
+
Data-type specifier. Not used if `arrays` is non-empty.
|
| 924 |
+
|
| 925 |
+
ilp64 : {True, False, 'preferred'}, optional
|
| 926 |
+
Whether to return ILP64 routine variant.
|
| 927 |
+
Choosing 'preferred' returns ILP64 routine if available, and
|
| 928 |
+
otherwise the 32-bit routine. Default: False
|
| 929 |
+
|
| 930 |
+
Returns
|
| 931 |
+
-------
|
| 932 |
+
funcs : list
|
| 933 |
+
List containing the found function(s).
|
| 934 |
+
|
| 935 |
+
Notes
|
| 936 |
+
-----
|
| 937 |
+
This routine automatically chooses between Fortran/C
|
| 938 |
+
interfaces. Fortran code is used whenever possible for arrays with
|
| 939 |
+
column major order. In all other cases, C code is preferred.
|
| 940 |
+
|
| 941 |
+
In LAPACK, the naming convention is that all functions start with a
|
| 942 |
+
type prefix, which depends on the type of the principal
|
| 943 |
+
matrix. These can be one of {'s', 'd', 'c', 'z'} for the NumPy
|
| 944 |
+
types {float32, float64, complex64, complex128} respectively, and
|
| 945 |
+
are stored in attribute ``typecode`` of the returned functions.
|
| 946 |
+
|
| 947 |
+
Examples
|
| 948 |
+
--------
|
| 949 |
+
Suppose we would like to use '?lange' routine which computes the selected
|
| 950 |
+
norm of an array. We pass our array in order to get the correct 'lange'
|
| 951 |
+
flavor.
|
| 952 |
+
|
| 953 |
+
>>> import numpy as np
|
| 954 |
+
>>> import scipy.linalg as LA
|
| 955 |
+
>>> rng = np.random.default_rng()
|
| 956 |
+
|
| 957 |
+
>>> a = rng.random((3,2))
|
| 958 |
+
>>> x_lange = LA.get_lapack_funcs('lange', (a,))
|
| 959 |
+
>>> x_lange.typecode
|
| 960 |
+
'd'
|
| 961 |
+
>>> x_lange = LA.get_lapack_funcs('lange',(a*1j,))
|
| 962 |
+
>>> x_lange.typecode
|
| 963 |
+
'z'
|
| 964 |
+
|
| 965 |
+
Several LAPACK routines work best when its internal WORK array has
|
| 966 |
+
the optimal size (big enough for fast computation and small enough to
|
| 967 |
+
avoid waste of memory). This size is determined also by a dedicated query
|
| 968 |
+
to the function which is often wrapped as a standalone function and
|
| 969 |
+
commonly denoted as ``###_lwork``. Below is an example for ``?sysv``
|
| 970 |
+
|
| 971 |
+
>>> a = rng.random((1000, 1000))
|
| 972 |
+
>>> b = rng.random((1000, 1)) * 1j
|
| 973 |
+
>>> # We pick up zsysv and zsysv_lwork due to b array
|
| 974 |
+
... xsysv, xlwork = LA.get_lapack_funcs(('sysv', 'sysv_lwork'), (a, b))
|
| 975 |
+
>>> opt_lwork, _ = xlwork(a.shape[0]) # returns a complex for 'z' prefix
|
| 976 |
+
>>> udut, ipiv, x, info = xsysv(a, b, lwork=int(opt_lwork.real))
|
| 977 |
+
|
| 978 |
+
"""
|
| 979 |
+
if isinstance(ilp64, str):
|
| 980 |
+
if ilp64 == 'preferred':
|
| 981 |
+
ilp64 = HAS_ILP64
|
| 982 |
+
else:
|
| 983 |
+
raise ValueError("Invalid value for 'ilp64'")
|
| 984 |
+
|
| 985 |
+
if not ilp64:
|
| 986 |
+
return _get_funcs(names, arrays, dtype,
|
| 987 |
+
"LAPACK", _flapack, _clapack,
|
| 988 |
+
"flapack", "clapack", _lapack_alias,
|
| 989 |
+
ilp64=False)
|
| 990 |
+
else:
|
| 991 |
+
if not HAS_ILP64:
|
| 992 |
+
raise RuntimeError("LAPACK ILP64 routine requested, but Scipy "
|
| 993 |
+
"compiled only with 32-bit BLAS")
|
| 994 |
+
return _get_funcs(names, arrays, dtype,
|
| 995 |
+
"LAPACK", _flapack_64, None,
|
| 996 |
+
"flapack_64", None, _lapack_alias,
|
| 997 |
+
ilp64=True)
|
| 998 |
+
|
| 999 |
+
|
| 1000 |
+
_int32_max = np.iinfo(np.int32).max
|
| 1001 |
+
_int64_max = np.iinfo(np.int64).max
|
| 1002 |
+
|
| 1003 |
+
|
| 1004 |
+
def _compute_lwork(routine, *args, **kwargs):
|
| 1005 |
+
"""
|
| 1006 |
+
Round floating-point lwork returned by lapack to integer.
|
| 1007 |
+
|
| 1008 |
+
Several LAPACK routines compute optimal values for LWORK, which
|
| 1009 |
+
they return in a floating-point variable. However, for large
|
| 1010 |
+
values of LWORK, single-precision floating point is not sufficient
|
| 1011 |
+
to hold the exact value --- some LAPACK versions (<= 3.5.0 at
|
| 1012 |
+
least) truncate the returned integer to single precision and in
|
| 1013 |
+
some cases this can be smaller than the required value.
|
| 1014 |
+
|
| 1015 |
+
Examples
|
| 1016 |
+
--------
|
| 1017 |
+
>>> from scipy.linalg import lapack
|
| 1018 |
+
>>> n = 5000
|
| 1019 |
+
>>> s_r, s_lw = lapack.get_lapack_funcs(('sysvx', 'sysvx_lwork'))
|
| 1020 |
+
>>> lwork = lapack._compute_lwork(s_lw, n)
|
| 1021 |
+
>>> lwork
|
| 1022 |
+
32000
|
| 1023 |
+
|
| 1024 |
+
"""
|
| 1025 |
+
dtype = getattr(routine, 'dtype', None)
|
| 1026 |
+
int_dtype = getattr(routine, 'int_dtype', None)
|
| 1027 |
+
ret = routine(*args, **kwargs)
|
| 1028 |
+
if ret[-1] != 0:
|
| 1029 |
+
raise ValueError("Internal work array size computation failed: "
|
| 1030 |
+
"%d" % (ret[-1],))
|
| 1031 |
+
|
| 1032 |
+
if len(ret) == 2:
|
| 1033 |
+
return _check_work_float(ret[0].real, dtype, int_dtype)
|
| 1034 |
+
else:
|
| 1035 |
+
return tuple(_check_work_float(x.real, dtype, int_dtype)
|
| 1036 |
+
for x in ret[:-1])
|
| 1037 |
+
|
| 1038 |
+
|
| 1039 |
+
def _check_work_float(value, dtype, int_dtype):
|
| 1040 |
+
"""
|
| 1041 |
+
Convert LAPACK-returned work array size float to integer,
|
| 1042 |
+
carefully for single-precision types.
|
| 1043 |
+
"""
|
| 1044 |
+
|
| 1045 |
+
if dtype == np.float32 or dtype == np.complex64:
|
| 1046 |
+
# Single-precision routine -- take next fp value to work
|
| 1047 |
+
# around possible truncation in LAPACK code
|
| 1048 |
+
value = np.nextafter(value, np.inf, dtype=np.float32)
|
| 1049 |
+
|
| 1050 |
+
value = int(value)
|
| 1051 |
+
if int_dtype.itemsize == 4:
|
| 1052 |
+
if value < 0 or value > _int32_max:
|
| 1053 |
+
raise ValueError("Too large work array required -- computation "
|
| 1054 |
+
"cannot be performed with standard 32-bit"
|
| 1055 |
+
" LAPACK.")
|
| 1056 |
+
elif int_dtype.itemsize == 8:
|
| 1057 |
+
if value < 0 or value > _int64_max:
|
| 1058 |
+
raise ValueError("Too large work array required -- computation"
|
| 1059 |
+
" cannot be performed with standard 64-bit"
|
| 1060 |
+
" LAPACK.")
|
| 1061 |
+
return value
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/matfuncs.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This file is not meant for public use and will be removed in SciPy v2.0.0.
|
| 2 |
+
# Use the `scipy.linalg` namespace for importing the functions
|
| 3 |
+
# included below.
|
| 4 |
+
|
| 5 |
+
from scipy._lib.deprecation import _sub_module_deprecation
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
__all__ = [ # noqa: F822
|
| 9 |
+
'expm', 'cosm', 'sinm', 'tanm', 'coshm', 'sinhm',
|
| 10 |
+
'tanhm', 'logm', 'funm', 'signm', 'sqrtm',
|
| 11 |
+
'expm_frechet', 'expm_cond', 'fractional_matrix_power',
|
| 12 |
+
'khatri_rao', 'norm', 'solve', 'inv', 'svd', 'schur', 'rsf2csf'
|
| 13 |
+
]
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def __dir__():
|
| 17 |
+
return __all__
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def __getattr__(name):
|
| 21 |
+
return _sub_module_deprecation(sub_package="linalg", module="matfuncs",
|
| 22 |
+
private_modules=["_matfuncs"], all=__all__,
|
| 23 |
+
attribute=name)
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/misc.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This file is not meant for public use and will be removed in SciPy v2.0.0.
|
| 2 |
+
# Use the `scipy.linalg` namespace for importing the functions
|
| 3 |
+
# included below.
|
| 4 |
+
|
| 5 |
+
from scipy._lib.deprecation import _sub_module_deprecation
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
__all__ = [ # noqa: F822
|
| 9 |
+
'LinAlgError', 'LinAlgWarning', 'norm', 'get_blas_funcs',
|
| 10 |
+
'get_lapack_funcs'
|
| 11 |
+
]
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def __dir__():
|
| 15 |
+
return __all__
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def __getattr__(name):
|
| 19 |
+
return _sub_module_deprecation(sub_package="linalg", module="misc",
|
| 20 |
+
private_modules=["_misc"], all=__all__,
|
| 21 |
+
attribute=name)
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/special_matrices.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This file is not meant for public use and will be removed in SciPy v2.0.0.
|
| 2 |
+
# Use the `scipy.linalg` namespace for importing the functions
|
| 3 |
+
# included below.
|
| 4 |
+
|
| 5 |
+
from scipy._lib.deprecation import _sub_module_deprecation
|
| 6 |
+
|
| 7 |
+
__all__ = [ # noqa: F822
|
| 8 |
+
'toeplitz', 'circulant', 'hankel',
|
| 9 |
+
'hadamard', 'leslie', 'kron', 'block_diag', 'companion',
|
| 10 |
+
'helmert', 'hilbert', 'invhilbert', 'pascal', 'invpascal', 'dft',
|
| 11 |
+
'fiedler', 'fiedler_companion', 'convolution_matrix'
|
| 12 |
+
]
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def __dir__():
|
| 16 |
+
return __all__
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def __getattr__(name):
|
| 20 |
+
return _sub_module_deprecation(sub_package="linalg", module="special_matrices",
|
| 21 |
+
private_modules=["_special_matrices"], all=__all__,
|
| 22 |
+
attribute=name)
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/__init__.py
ADDED
|
File without changes
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/_cython_examples/extending.pyx
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
#cython: language_level=3
|
| 3 |
+
#cython: boundscheck=False
|
| 4 |
+
#cython: wraparound=False
|
| 5 |
+
|
| 6 |
+
cimport scipy.linalg
|
| 7 |
+
from scipy.linalg.cython_blas cimport cdotu
|
| 8 |
+
from scipy.linalg.cython_lapack cimport dgtsv
|
| 9 |
+
|
| 10 |
+
cpdef tridiag(double[:] a, double[:] b, double[:] c, double[:] x):
|
| 11 |
+
""" Solve the system A y = x for y where A is the tridiagonal matrix with
|
| 12 |
+
subdiagonal 'a', diagonal 'b', and superdiagonal 'c'. """
|
| 13 |
+
cdef int n=b.shape[0], nrhs=1, info
|
| 14 |
+
# Solution is written over the values in x.
|
| 15 |
+
dgtsv(&n, &nrhs, &a[0], &b[0], &c[0], &x[0], &n, &info)
|
| 16 |
+
|
| 17 |
+
cpdef float complex complex_dot(float complex[:] cx, float complex[:] cy):
|
| 18 |
+
""" Take dot product of two complex vectors """
|
| 19 |
+
cdef:
|
| 20 |
+
int n = cx.shape[0]
|
| 21 |
+
int incx = cx.strides[0] // sizeof(cx[0])
|
| 22 |
+
int incy = cy.strides[0] // sizeof(cy[0])
|
| 23 |
+
return cdotu(&n, &cx[0], &incx, &cy[0], &incy)
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/_cython_examples/meson.build
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
project('random-build-examples', 'c', 'cpp', 'cython')
|
| 2 |
+
|
| 3 |
+
fs = import('fs')
|
| 4 |
+
|
| 5 |
+
py3 = import('python').find_installation(pure: false)
|
| 6 |
+
|
| 7 |
+
cy = meson.get_compiler('cython')
|
| 8 |
+
|
| 9 |
+
if not cy.version().version_compare('>=3.0.8')
|
| 10 |
+
error('tests requires Cython >= 3.0.8')
|
| 11 |
+
endif
|
| 12 |
+
|
| 13 |
+
py3.extension_module(
|
| 14 |
+
'extending',
|
| 15 |
+
'extending.pyx',
|
| 16 |
+
install: false,
|
| 17 |
+
c_args: ['-DCYTHON_CCOMPLEX=0'] # see gh-18975 for why we need this
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
extending_cpp = fs.copyfile('extending.pyx', 'extending_cpp.pyx')
|
| 21 |
+
py3.extension_module(
|
| 22 |
+
'extending_cpp',
|
| 23 |
+
extending_cpp,
|
| 24 |
+
install: false,
|
| 25 |
+
override_options : ['cython_language=cpp'],
|
| 26 |
+
cpp_args: ['-DCYTHON_CCOMPLEX=0'] # see gh-18975 for why we need this
|
| 27 |
+
)
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/data/carex_15_data.npz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:13f3e1491a876bbf59d7ea10ad29c1f9b5996a2ab99216f31d5bfcd659012c1e
|
| 3 |
+
size 34462
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/data/carex_18_data.npz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:59f839467f2752b7df6fb6d4094396edd32a5929b764f7ffa1e6666431e6cac6
|
| 3 |
+
size 161487
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/data/carex_19_data.npz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:38e8fc7b041df0b23d7e5ca15ead1a065e6467611ef9a848cc7db93f80adfd87
|
| 3 |
+
size 34050
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/data/carex_20_data.npz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:14e222d34a7118c7284a1675c6feceee77b84df951a5c6ba2a5ee9ff3054fa1d
|
| 3 |
+
size 31231
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/data/carex_6_data.npz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1b2a0736b541ebf5c4b9b4c00d6dab281e73c9fb9913c6e2581a781b37b602f9
|
| 3 |
+
size 15878
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/data/gendare_20170120_data.npz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a3dfab451d9d5c20243e0ed85cd8b6c9657669fb9a0f83b5be165585783d55b5
|
| 3 |
+
size 2164
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_basic.py
ADDED
|
@@ -0,0 +1,2059 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import itertools
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
from numpy import (arange, array, dot, zeros, identity, conjugate, transpose,
|
| 5 |
+
float32)
|
| 6 |
+
from numpy.random import random
|
| 7 |
+
|
| 8 |
+
from numpy.testing import (assert_equal, assert_almost_equal, assert_,
|
| 9 |
+
assert_array_almost_equal, assert_allclose,
|
| 10 |
+
assert_array_equal, suppress_warnings)
|
| 11 |
+
import pytest
|
| 12 |
+
from pytest import raises as assert_raises
|
| 13 |
+
|
| 14 |
+
from scipy.linalg import (solve, inv, det, lstsq, pinv, pinvh, norm,
|
| 15 |
+
solve_banded, solveh_banded, solve_triangular,
|
| 16 |
+
solve_circulant, circulant, LinAlgError, block_diag,
|
| 17 |
+
matrix_balance, qr, LinAlgWarning)
|
| 18 |
+
|
| 19 |
+
from scipy.linalg._testutils import assert_no_overwrite
|
| 20 |
+
from scipy._lib._testutils import check_free_memory, IS_MUSL
|
| 21 |
+
from scipy.linalg.blas import HAS_ILP64
|
| 22 |
+
|
| 23 |
+
REAL_DTYPES = (np.float32, np.float64, np.longdouble)
|
| 24 |
+
COMPLEX_DTYPES = (np.complex64, np.complex128, np.clongdouble)
|
| 25 |
+
DTYPES = REAL_DTYPES + COMPLEX_DTYPES
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def _eps_cast(dtyp):
|
| 29 |
+
"""Get the epsilon for dtype, possibly downcast to BLAS types."""
|
| 30 |
+
dt = dtyp
|
| 31 |
+
if dt == np.longdouble:
|
| 32 |
+
dt = np.float64
|
| 33 |
+
elif dt == np.clongdouble:
|
| 34 |
+
dt = np.complex128
|
| 35 |
+
return np.finfo(dt).eps
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
class TestSolveBanded:
|
| 39 |
+
|
| 40 |
+
def test_real(self):
|
| 41 |
+
a = array([[1.0, 20, 0, 0],
|
| 42 |
+
[-30, 4, 6, 0],
|
| 43 |
+
[2, 1, 20, 2],
|
| 44 |
+
[0, -1, 7, 14]])
|
| 45 |
+
ab = array([[0.0, 20, 6, 2],
|
| 46 |
+
[1, 4, 20, 14],
|
| 47 |
+
[-30, 1, 7, 0],
|
| 48 |
+
[2, -1, 0, 0]])
|
| 49 |
+
l, u = 2, 1
|
| 50 |
+
b4 = array([10.0, 0.0, 2.0, 14.0])
|
| 51 |
+
b4by1 = b4.reshape(-1, 1)
|
| 52 |
+
b4by2 = array([[2, 1],
|
| 53 |
+
[-30, 4],
|
| 54 |
+
[2, 3],
|
| 55 |
+
[1, 3]])
|
| 56 |
+
b4by4 = array([[1, 0, 0, 0],
|
| 57 |
+
[0, 0, 0, 1],
|
| 58 |
+
[0, 1, 0, 0],
|
| 59 |
+
[0, 1, 0, 0]])
|
| 60 |
+
for b in [b4, b4by1, b4by2, b4by4]:
|
| 61 |
+
x = solve_banded((l, u), ab, b)
|
| 62 |
+
assert_array_almost_equal(dot(a, x), b)
|
| 63 |
+
|
| 64 |
+
def test_complex(self):
|
| 65 |
+
a = array([[1.0, 20, 0, 0],
|
| 66 |
+
[-30, 4, 6, 0],
|
| 67 |
+
[2j, 1, 20, 2j],
|
| 68 |
+
[0, -1, 7, 14]])
|
| 69 |
+
ab = array([[0.0, 20, 6, 2j],
|
| 70 |
+
[1, 4, 20, 14],
|
| 71 |
+
[-30, 1, 7, 0],
|
| 72 |
+
[2j, -1, 0, 0]])
|
| 73 |
+
l, u = 2, 1
|
| 74 |
+
b4 = array([10.0, 0.0, 2.0, 14.0j])
|
| 75 |
+
b4by1 = b4.reshape(-1, 1)
|
| 76 |
+
b4by2 = array([[2, 1],
|
| 77 |
+
[-30, 4],
|
| 78 |
+
[2, 3],
|
| 79 |
+
[1, 3]])
|
| 80 |
+
b4by4 = array([[1, 0, 0, 0],
|
| 81 |
+
[0, 0, 0, 1j],
|
| 82 |
+
[0, 1, 0, 0],
|
| 83 |
+
[0, 1, 0, 0]])
|
| 84 |
+
for b in [b4, b4by1, b4by2, b4by4]:
|
| 85 |
+
x = solve_banded((l, u), ab, b)
|
| 86 |
+
assert_array_almost_equal(dot(a, x), b)
|
| 87 |
+
|
| 88 |
+
def test_tridiag_real(self):
|
| 89 |
+
ab = array([[0.0, 20, 6, 2],
|
| 90 |
+
[1, 4, 20, 14],
|
| 91 |
+
[-30, 1, 7, 0]])
|
| 92 |
+
a = np.diag(ab[0, 1:], 1) + np.diag(ab[1, :], 0) + np.diag(
|
| 93 |
+
ab[2, :-1], -1)
|
| 94 |
+
b4 = array([10.0, 0.0, 2.0, 14.0])
|
| 95 |
+
b4by1 = b4.reshape(-1, 1)
|
| 96 |
+
b4by2 = array([[2, 1],
|
| 97 |
+
[-30, 4],
|
| 98 |
+
[2, 3],
|
| 99 |
+
[1, 3]])
|
| 100 |
+
b4by4 = array([[1, 0, 0, 0],
|
| 101 |
+
[0, 0, 0, 1],
|
| 102 |
+
[0, 1, 0, 0],
|
| 103 |
+
[0, 1, 0, 0]])
|
| 104 |
+
for b in [b4, b4by1, b4by2, b4by4]:
|
| 105 |
+
x = solve_banded((1, 1), ab, b)
|
| 106 |
+
assert_array_almost_equal(dot(a, x), b)
|
| 107 |
+
|
| 108 |
+
def test_tridiag_complex(self):
|
| 109 |
+
ab = array([[0.0, 20, 6, 2j],
|
| 110 |
+
[1, 4, 20, 14],
|
| 111 |
+
[-30, 1, 7, 0]])
|
| 112 |
+
a = np.diag(ab[0, 1:], 1) + np.diag(ab[1, :], 0) + np.diag(
|
| 113 |
+
ab[2, :-1], -1)
|
| 114 |
+
b4 = array([10.0, 0.0, 2.0, 14.0j])
|
| 115 |
+
b4by1 = b4.reshape(-1, 1)
|
| 116 |
+
b4by2 = array([[2, 1],
|
| 117 |
+
[-30, 4],
|
| 118 |
+
[2, 3],
|
| 119 |
+
[1, 3]])
|
| 120 |
+
b4by4 = array([[1, 0, 0, 0],
|
| 121 |
+
[0, 0, 0, 1],
|
| 122 |
+
[0, 1, 0, 0],
|
| 123 |
+
[0, 1, 0, 0]])
|
| 124 |
+
for b in [b4, b4by1, b4by2, b4by4]:
|
| 125 |
+
x = solve_banded((1, 1), ab, b)
|
| 126 |
+
assert_array_almost_equal(dot(a, x), b)
|
| 127 |
+
|
| 128 |
+
def test_check_finite(self):
|
| 129 |
+
a = array([[1.0, 20, 0, 0],
|
| 130 |
+
[-30, 4, 6, 0],
|
| 131 |
+
[2, 1, 20, 2],
|
| 132 |
+
[0, -1, 7, 14]])
|
| 133 |
+
ab = array([[0.0, 20, 6, 2],
|
| 134 |
+
[1, 4, 20, 14],
|
| 135 |
+
[-30, 1, 7, 0],
|
| 136 |
+
[2, -1, 0, 0]])
|
| 137 |
+
l, u = 2, 1
|
| 138 |
+
b4 = array([10.0, 0.0, 2.0, 14.0])
|
| 139 |
+
x = solve_banded((l, u), ab, b4, check_finite=False)
|
| 140 |
+
assert_array_almost_equal(dot(a, x), b4)
|
| 141 |
+
|
| 142 |
+
def test_bad_shape(self):
|
| 143 |
+
ab = array([[0.0, 20, 6, 2],
|
| 144 |
+
[1, 4, 20, 14],
|
| 145 |
+
[-30, 1, 7, 0],
|
| 146 |
+
[2, -1, 0, 0]])
|
| 147 |
+
l, u = 2, 1
|
| 148 |
+
bad = array([1.0, 2.0, 3.0, 4.0]).reshape(-1, 4)
|
| 149 |
+
assert_raises(ValueError, solve_banded, (l, u), ab, bad)
|
| 150 |
+
assert_raises(ValueError, solve_banded, (l, u), ab, [1.0, 2.0])
|
| 151 |
+
|
| 152 |
+
# Values of (l,u) are not compatible with ab.
|
| 153 |
+
assert_raises(ValueError, solve_banded, (1, 1), ab, [1.0, 2.0])
|
| 154 |
+
|
| 155 |
+
def test_1x1(self):
|
| 156 |
+
# gh-8906 noted that the case of A@x = b with 1x1 A was handled
|
| 157 |
+
# incorrectly; check that this is resolved. Typical case:
|
| 158 |
+
# nupper == nlower == 0
|
| 159 |
+
# A = [[2]]
|
| 160 |
+
b = array([[1., 2., 3.]])
|
| 161 |
+
ref = array([[0.5, 1.0, 1.5]])
|
| 162 |
+
x = solve_banded((0, 0), [[2]], b)
|
| 163 |
+
assert_allclose(x, ref, rtol=1e-15)
|
| 164 |
+
|
| 165 |
+
# However, the user *can* represent the same system with garbage rows
|
| 166 |
+
# in `ab`. Test the case with `nupper == 1, nlower == 1`.
|
| 167 |
+
x = solve_banded((1, 1), [[0], [2], [0]], b)
|
| 168 |
+
assert_allclose(x, ref, rtol=1e-15)
|
| 169 |
+
assert_equal(x.dtype, np.dtype('f8'))
|
| 170 |
+
assert_array_equal(b, [[1.0, 2.0, 3.0]])
|
| 171 |
+
|
| 172 |
+
def test_native_list_arguments(self):
|
| 173 |
+
a = [[1.0, 20, 0, 0],
|
| 174 |
+
[-30, 4, 6, 0],
|
| 175 |
+
[2, 1, 20, 2],
|
| 176 |
+
[0, -1, 7, 14]]
|
| 177 |
+
ab = [[0.0, 20, 6, 2],
|
| 178 |
+
[1, 4, 20, 14],
|
| 179 |
+
[-30, 1, 7, 0],
|
| 180 |
+
[2, -1, 0, 0]]
|
| 181 |
+
l, u = 2, 1
|
| 182 |
+
b = [10.0, 0.0, 2.0, 14.0]
|
| 183 |
+
x = solve_banded((l, u), ab, b)
|
| 184 |
+
assert_array_almost_equal(dot(a, x), b)
|
| 185 |
+
|
| 186 |
+
@pytest.mark.thread_unsafe # due to Cython fused types, see cython#6506
|
| 187 |
+
@pytest.mark.parametrize('dt_ab', [int, float, np.float32, complex, np.complex64])
|
| 188 |
+
@pytest.mark.parametrize('dt_b', [int, float, np.float32, complex, np.complex64])
|
| 189 |
+
def test_empty(self, dt_ab, dt_b):
|
| 190 |
+
# ab contains one empty row corresponding to the diagonal
|
| 191 |
+
ab = np.array([[]], dtype=dt_ab)
|
| 192 |
+
b = np.array([], dtype=dt_b)
|
| 193 |
+
x = solve_banded((0, 0), ab, b)
|
| 194 |
+
|
| 195 |
+
assert x.shape == (0,)
|
| 196 |
+
assert x.dtype == solve(np.eye(1, dtype=dt_ab), np.ones(1, dtype=dt_b)).dtype
|
| 197 |
+
|
| 198 |
+
b = np.empty((0, 0), dtype=dt_b)
|
| 199 |
+
x = solve_banded((0, 0), ab, b)
|
| 200 |
+
|
| 201 |
+
assert x.shape == (0, 0)
|
| 202 |
+
assert x.dtype == solve(np.eye(1, dtype=dt_ab), np.ones(1, dtype=dt_b)).dtype
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
class TestSolveHBanded:
|
| 206 |
+
|
| 207 |
+
def test_01_upper(self):
|
| 208 |
+
# Solve
|
| 209 |
+
# [ 4 1 2 0] [1]
|
| 210 |
+
# [ 1 4 1 2] X = [4]
|
| 211 |
+
# [ 2 1 4 1] [1]
|
| 212 |
+
# [ 0 2 1 4] [2]
|
| 213 |
+
# with the RHS as a 1D array.
|
| 214 |
+
ab = array([[0.0, 0.0, 2.0, 2.0],
|
| 215 |
+
[-99, 1.0, 1.0, 1.0],
|
| 216 |
+
[4.0, 4.0, 4.0, 4.0]])
|
| 217 |
+
b = array([1.0, 4.0, 1.0, 2.0])
|
| 218 |
+
x = solveh_banded(ab, b)
|
| 219 |
+
assert_array_almost_equal(x, [0.0, 1.0, 0.0, 0.0])
|
| 220 |
+
|
| 221 |
+
def test_02_upper(self):
|
| 222 |
+
# Solve
|
| 223 |
+
# [ 4 1 2 0] [1 6]
|
| 224 |
+
# [ 1 4 1 2] X = [4 2]
|
| 225 |
+
# [ 2 1 4 1] [1 6]
|
| 226 |
+
# [ 0 2 1 4] [2 1]
|
| 227 |
+
#
|
| 228 |
+
ab = array([[0.0, 0.0, 2.0, 2.0],
|
| 229 |
+
[-99, 1.0, 1.0, 1.0],
|
| 230 |
+
[4.0, 4.0, 4.0, 4.0]])
|
| 231 |
+
b = array([[1.0, 6.0],
|
| 232 |
+
[4.0, 2.0],
|
| 233 |
+
[1.0, 6.0],
|
| 234 |
+
[2.0, 1.0]])
|
| 235 |
+
x = solveh_banded(ab, b)
|
| 236 |
+
expected = array([[0.0, 1.0],
|
| 237 |
+
[1.0, 0.0],
|
| 238 |
+
[0.0, 1.0],
|
| 239 |
+
[0.0, 0.0]])
|
| 240 |
+
assert_array_almost_equal(x, expected)
|
| 241 |
+
|
| 242 |
+
def test_03_upper(self):
|
| 243 |
+
# Solve
|
| 244 |
+
# [ 4 1 2 0] [1]
|
| 245 |
+
# [ 1 4 1 2] X = [4]
|
| 246 |
+
# [ 2 1 4 1] [1]
|
| 247 |
+
# [ 0 2 1 4] [2]
|
| 248 |
+
# with the RHS as a 2D array with shape (3,1).
|
| 249 |
+
ab = array([[0.0, 0.0, 2.0, 2.0],
|
| 250 |
+
[-99, 1.0, 1.0, 1.0],
|
| 251 |
+
[4.0, 4.0, 4.0, 4.0]])
|
| 252 |
+
b = array([1.0, 4.0, 1.0, 2.0]).reshape(-1, 1)
|
| 253 |
+
x = solveh_banded(ab, b)
|
| 254 |
+
assert_array_almost_equal(x, array([0., 1., 0., 0.]).reshape(-1, 1))
|
| 255 |
+
|
| 256 |
+
def test_01_lower(self):
|
| 257 |
+
# Solve
|
| 258 |
+
# [ 4 1 2 0] [1]
|
| 259 |
+
# [ 1 4 1 2] X = [4]
|
| 260 |
+
# [ 2 1 4 1] [1]
|
| 261 |
+
# [ 0 2 1 4] [2]
|
| 262 |
+
#
|
| 263 |
+
ab = array([[4.0, 4.0, 4.0, 4.0],
|
| 264 |
+
[1.0, 1.0, 1.0, -99],
|
| 265 |
+
[2.0, 2.0, 0.0, 0.0]])
|
| 266 |
+
b = array([1.0, 4.0, 1.0, 2.0])
|
| 267 |
+
x = solveh_banded(ab, b, lower=True)
|
| 268 |
+
assert_array_almost_equal(x, [0.0, 1.0, 0.0, 0.0])
|
| 269 |
+
|
| 270 |
+
def test_02_lower(self):
|
| 271 |
+
# Solve
|
| 272 |
+
# [ 4 1 2 0] [1 6]
|
| 273 |
+
# [ 1 4 1 2] X = [4 2]
|
| 274 |
+
# [ 2 1 4 1] [1 6]
|
| 275 |
+
# [ 0 2 1 4] [2 1]
|
| 276 |
+
#
|
| 277 |
+
ab = array([[4.0, 4.0, 4.0, 4.0],
|
| 278 |
+
[1.0, 1.0, 1.0, -99],
|
| 279 |
+
[2.0, 2.0, 0.0, 0.0]])
|
| 280 |
+
b = array([[1.0, 6.0],
|
| 281 |
+
[4.0, 2.0],
|
| 282 |
+
[1.0, 6.0],
|
| 283 |
+
[2.0, 1.0]])
|
| 284 |
+
x = solveh_banded(ab, b, lower=True)
|
| 285 |
+
expected = array([[0.0, 1.0],
|
| 286 |
+
[1.0, 0.0],
|
| 287 |
+
[0.0, 1.0],
|
| 288 |
+
[0.0, 0.0]])
|
| 289 |
+
assert_array_almost_equal(x, expected)
|
| 290 |
+
|
| 291 |
+
def test_01_float32(self):
|
| 292 |
+
# Solve
|
| 293 |
+
# [ 4 1 2 0] [1]
|
| 294 |
+
# [ 1 4 1 2] X = [4]
|
| 295 |
+
# [ 2 1 4 1] [1]
|
| 296 |
+
# [ 0 2 1 4] [2]
|
| 297 |
+
#
|
| 298 |
+
ab = array([[0.0, 0.0, 2.0, 2.0],
|
| 299 |
+
[-99, 1.0, 1.0, 1.0],
|
| 300 |
+
[4.0, 4.0, 4.0, 4.0]], dtype=float32)
|
| 301 |
+
b = array([1.0, 4.0, 1.0, 2.0], dtype=float32)
|
| 302 |
+
x = solveh_banded(ab, b)
|
| 303 |
+
assert_array_almost_equal(x, [0.0, 1.0, 0.0, 0.0])
|
| 304 |
+
|
| 305 |
+
def test_02_float32(self):
|
| 306 |
+
# Solve
|
| 307 |
+
# [ 4 1 2 0] [1 6]
|
| 308 |
+
# [ 1 4 1 2] X = [4 2]
|
| 309 |
+
# [ 2 1 4 1] [1 6]
|
| 310 |
+
# [ 0 2 1 4] [2 1]
|
| 311 |
+
#
|
| 312 |
+
ab = array([[0.0, 0.0, 2.0, 2.0],
|
| 313 |
+
[-99, 1.0, 1.0, 1.0],
|
| 314 |
+
[4.0, 4.0, 4.0, 4.0]], dtype=float32)
|
| 315 |
+
b = array([[1.0, 6.0],
|
| 316 |
+
[4.0, 2.0],
|
| 317 |
+
[1.0, 6.0],
|
| 318 |
+
[2.0, 1.0]], dtype=float32)
|
| 319 |
+
x = solveh_banded(ab, b)
|
| 320 |
+
expected = array([[0.0, 1.0],
|
| 321 |
+
[1.0, 0.0],
|
| 322 |
+
[0.0, 1.0],
|
| 323 |
+
[0.0, 0.0]])
|
| 324 |
+
assert_array_almost_equal(x, expected)
|
| 325 |
+
|
| 326 |
+
def test_01_complex(self):
|
| 327 |
+
# Solve
|
| 328 |
+
# [ 4 -j 2 0] [2-j]
|
| 329 |
+
# [ j 4 -j 2] X = [4-j]
|
| 330 |
+
# [ 2 j 4 -j] [4+j]
|
| 331 |
+
# [ 0 2 j 4] [2+j]
|
| 332 |
+
#
|
| 333 |
+
ab = array([[0.0, 0.0, 2.0, 2.0],
|
| 334 |
+
[-99, -1.0j, -1.0j, -1.0j],
|
| 335 |
+
[4.0, 4.0, 4.0, 4.0]])
|
| 336 |
+
b = array([2-1.0j, 4.0-1j, 4+1j, 2+1j])
|
| 337 |
+
x = solveh_banded(ab, b)
|
| 338 |
+
assert_array_almost_equal(x, [0.0, 1.0, 1.0, 0.0])
|
| 339 |
+
|
| 340 |
+
def test_02_complex(self):
|
| 341 |
+
# Solve
|
| 342 |
+
# [ 4 -j 2 0] [2-j 2+4j]
|
| 343 |
+
# [ j 4 -j 2] X = [4-j -1-j]
|
| 344 |
+
# [ 2 j 4 -j] [4+j 4+2j]
|
| 345 |
+
# [ 0 2 j 4] [2+j j]
|
| 346 |
+
#
|
| 347 |
+
ab = array([[0.0, 0.0, 2.0, 2.0],
|
| 348 |
+
[-99, -1.0j, -1.0j, -1.0j],
|
| 349 |
+
[4.0, 4.0, 4.0, 4.0]])
|
| 350 |
+
b = array([[2-1j, 2+4j],
|
| 351 |
+
[4.0-1j, -1-1j],
|
| 352 |
+
[4.0+1j, 4+2j],
|
| 353 |
+
[2+1j, 1j]])
|
| 354 |
+
x = solveh_banded(ab, b)
|
| 355 |
+
expected = array([[0.0, 1.0j],
|
| 356 |
+
[1.0, 0.0],
|
| 357 |
+
[1.0, 1.0],
|
| 358 |
+
[0.0, 0.0]])
|
| 359 |
+
assert_array_almost_equal(x, expected)
|
| 360 |
+
|
| 361 |
+
def test_tridiag_01_upper(self):
|
| 362 |
+
# Solve
|
| 363 |
+
# [ 4 1 0] [1]
|
| 364 |
+
# [ 1 4 1] X = [4]
|
| 365 |
+
# [ 0 1 4] [1]
|
| 366 |
+
# with the RHS as a 1D array.
|
| 367 |
+
ab = array([[-99, 1.0, 1.0], [4.0, 4.0, 4.0]])
|
| 368 |
+
b = array([1.0, 4.0, 1.0])
|
| 369 |
+
x = solveh_banded(ab, b)
|
| 370 |
+
assert_array_almost_equal(x, [0.0, 1.0, 0.0])
|
| 371 |
+
|
| 372 |
+
def test_tridiag_02_upper(self):
|
| 373 |
+
# Solve
|
| 374 |
+
# [ 4 1 0] [1 4]
|
| 375 |
+
# [ 1 4 1] X = [4 2]
|
| 376 |
+
# [ 0 1 4] [1 4]
|
| 377 |
+
#
|
| 378 |
+
ab = array([[-99, 1.0, 1.0],
|
| 379 |
+
[4.0, 4.0, 4.0]])
|
| 380 |
+
b = array([[1.0, 4.0],
|
| 381 |
+
[4.0, 2.0],
|
| 382 |
+
[1.0, 4.0]])
|
| 383 |
+
x = solveh_banded(ab, b)
|
| 384 |
+
expected = array([[0.0, 1.0],
|
| 385 |
+
[1.0, 0.0],
|
| 386 |
+
[0.0, 1.0]])
|
| 387 |
+
assert_array_almost_equal(x, expected)
|
| 388 |
+
|
| 389 |
+
def test_tridiag_03_upper(self):
|
| 390 |
+
# Solve
|
| 391 |
+
# [ 4 1 0] [1]
|
| 392 |
+
# [ 1 4 1] X = [4]
|
| 393 |
+
# [ 0 1 4] [1]
|
| 394 |
+
# with the RHS as a 2D array with shape (3,1).
|
| 395 |
+
ab = array([[-99, 1.0, 1.0], [4.0, 4.0, 4.0]])
|
| 396 |
+
b = array([1.0, 4.0, 1.0]).reshape(-1, 1)
|
| 397 |
+
x = solveh_banded(ab, b)
|
| 398 |
+
assert_array_almost_equal(x, array([0.0, 1.0, 0.0]).reshape(-1, 1))
|
| 399 |
+
|
| 400 |
+
def test_tridiag_01_lower(self):
|
| 401 |
+
# Solve
|
| 402 |
+
# [ 4 1 0] [1]
|
| 403 |
+
# [ 1 4 1] X = [4]
|
| 404 |
+
# [ 0 1 4] [1]
|
| 405 |
+
#
|
| 406 |
+
ab = array([[4.0, 4.0, 4.0],
|
| 407 |
+
[1.0, 1.0, -99]])
|
| 408 |
+
b = array([1.0, 4.0, 1.0])
|
| 409 |
+
x = solveh_banded(ab, b, lower=True)
|
| 410 |
+
assert_array_almost_equal(x, [0.0, 1.0, 0.0])
|
| 411 |
+
|
| 412 |
+
def test_tridiag_02_lower(self):
|
| 413 |
+
# Solve
|
| 414 |
+
# [ 4 1 0] [1 4]
|
| 415 |
+
# [ 1 4 1] X = [4 2]
|
| 416 |
+
# [ 0 1 4] [1 4]
|
| 417 |
+
#
|
| 418 |
+
ab = array([[4.0, 4.0, 4.0],
|
| 419 |
+
[1.0, 1.0, -99]])
|
| 420 |
+
b = array([[1.0, 4.0],
|
| 421 |
+
[4.0, 2.0],
|
| 422 |
+
[1.0, 4.0]])
|
| 423 |
+
x = solveh_banded(ab, b, lower=True)
|
| 424 |
+
expected = array([[0.0, 1.0],
|
| 425 |
+
[1.0, 0.0],
|
| 426 |
+
[0.0, 1.0]])
|
| 427 |
+
assert_array_almost_equal(x, expected)
|
| 428 |
+
|
| 429 |
+
def test_tridiag_01_float32(self):
|
| 430 |
+
# Solve
|
| 431 |
+
# [ 4 1 0] [1]
|
| 432 |
+
# [ 1 4 1] X = [4]
|
| 433 |
+
# [ 0 1 4] [1]
|
| 434 |
+
#
|
| 435 |
+
ab = array([[-99, 1.0, 1.0], [4.0, 4.0, 4.0]], dtype=float32)
|
| 436 |
+
b = array([1.0, 4.0, 1.0], dtype=float32)
|
| 437 |
+
x = solveh_banded(ab, b)
|
| 438 |
+
assert_array_almost_equal(x, [0.0, 1.0, 0.0])
|
| 439 |
+
|
| 440 |
+
def test_tridiag_02_float32(self):
|
| 441 |
+
# Solve
|
| 442 |
+
# [ 4 1 0] [1 4]
|
| 443 |
+
# [ 1 4 1] X = [4 2]
|
| 444 |
+
# [ 0 1 4] [1 4]
|
| 445 |
+
#
|
| 446 |
+
ab = array([[-99, 1.0, 1.0],
|
| 447 |
+
[4.0, 4.0, 4.0]], dtype=float32)
|
| 448 |
+
b = array([[1.0, 4.0],
|
| 449 |
+
[4.0, 2.0],
|
| 450 |
+
[1.0, 4.0]], dtype=float32)
|
| 451 |
+
x = solveh_banded(ab, b)
|
| 452 |
+
expected = array([[0.0, 1.0],
|
| 453 |
+
[1.0, 0.0],
|
| 454 |
+
[0.0, 1.0]])
|
| 455 |
+
assert_array_almost_equal(x, expected)
|
| 456 |
+
|
| 457 |
+
def test_tridiag_01_complex(self):
|
| 458 |
+
# Solve
|
| 459 |
+
# [ 4 -j 0] [ -j]
|
| 460 |
+
# [ j 4 -j] X = [4-j]
|
| 461 |
+
# [ 0 j 4] [4+j]
|
| 462 |
+
#
|
| 463 |
+
ab = array([[-99, -1.0j, -1.0j], [4.0, 4.0, 4.0]])
|
| 464 |
+
b = array([-1.0j, 4.0-1j, 4+1j])
|
| 465 |
+
x = solveh_banded(ab, b)
|
| 466 |
+
assert_array_almost_equal(x, [0.0, 1.0, 1.0])
|
| 467 |
+
|
| 468 |
+
def test_tridiag_02_complex(self):
|
| 469 |
+
# Solve
|
| 470 |
+
# [ 4 -j 0] [ -j 4j]
|
| 471 |
+
# [ j 4 -j] X = [4-j -1-j]
|
| 472 |
+
# [ 0 j 4] [4+j 4 ]
|
| 473 |
+
#
|
| 474 |
+
ab = array([[-99, -1.0j, -1.0j],
|
| 475 |
+
[4.0, 4.0, 4.0]])
|
| 476 |
+
b = array([[-1j, 4.0j],
|
| 477 |
+
[4.0-1j, -1.0-1j],
|
| 478 |
+
[4.0+1j, 4.0]])
|
| 479 |
+
x = solveh_banded(ab, b)
|
| 480 |
+
expected = array([[0.0, 1.0j],
|
| 481 |
+
[1.0, 0.0],
|
| 482 |
+
[1.0, 1.0]])
|
| 483 |
+
assert_array_almost_equal(x, expected)
|
| 484 |
+
|
| 485 |
+
def test_check_finite(self):
|
| 486 |
+
# Solve
|
| 487 |
+
# [ 4 1 0] [1]
|
| 488 |
+
# [ 1 4 1] X = [4]
|
| 489 |
+
# [ 0 1 4] [1]
|
| 490 |
+
# with the RHS as a 1D array.
|
| 491 |
+
ab = array([[-99, 1.0, 1.0], [4.0, 4.0, 4.0]])
|
| 492 |
+
b = array([1.0, 4.0, 1.0])
|
| 493 |
+
x = solveh_banded(ab, b, check_finite=False)
|
| 494 |
+
assert_array_almost_equal(x, [0.0, 1.0, 0.0])
|
| 495 |
+
|
| 496 |
+
def test_bad_shapes(self):
|
| 497 |
+
ab = array([[-99, 1.0, 1.0],
|
| 498 |
+
[4.0, 4.0, 4.0]])
|
| 499 |
+
b = array([[1.0, 4.0],
|
| 500 |
+
[4.0, 2.0]])
|
| 501 |
+
assert_raises(ValueError, solveh_banded, ab, b)
|
| 502 |
+
assert_raises(ValueError, solveh_banded, ab, [1.0, 2.0])
|
| 503 |
+
assert_raises(ValueError, solveh_banded, ab, [1.0])
|
| 504 |
+
|
| 505 |
+
def test_1x1(self):
|
| 506 |
+
x = solveh_banded([[1]], [[1, 2, 3]])
|
| 507 |
+
assert_array_equal(x, [[1.0, 2.0, 3.0]])
|
| 508 |
+
assert_equal(x.dtype, np.dtype('f8'))
|
| 509 |
+
|
| 510 |
+
def test_native_list_arguments(self):
|
| 511 |
+
# Same as test_01_upper, using python's native list.
|
| 512 |
+
ab = [[0.0, 0.0, 2.0, 2.0],
|
| 513 |
+
[-99, 1.0, 1.0, 1.0],
|
| 514 |
+
[4.0, 4.0, 4.0, 4.0]]
|
| 515 |
+
b = [1.0, 4.0, 1.0, 2.0]
|
| 516 |
+
x = solveh_banded(ab, b)
|
| 517 |
+
assert_array_almost_equal(x, [0.0, 1.0, 0.0, 0.0])
|
| 518 |
+
|
| 519 |
+
@pytest.mark.parametrize('dt_ab', [int, float, np.float32, complex, np.complex64])
|
| 520 |
+
@pytest.mark.parametrize('dt_b', [int, float, np.float32, complex, np.complex64])
|
| 521 |
+
def test_empty(self, dt_ab, dt_b):
|
| 522 |
+
# ab contains one empty row corresponding to the diagonal
|
| 523 |
+
ab = np.array([[]], dtype=dt_ab)
|
| 524 |
+
b = np.array([], dtype=dt_b)
|
| 525 |
+
x = solveh_banded(ab, b)
|
| 526 |
+
|
| 527 |
+
assert x.shape == (0,)
|
| 528 |
+
assert x.dtype == solve(np.eye(1, dtype=dt_ab), np.ones(1, dtype=dt_b)).dtype
|
| 529 |
+
|
| 530 |
+
b = np.empty((0, 0), dtype=dt_b)
|
| 531 |
+
x = solveh_banded(ab, b)
|
| 532 |
+
|
| 533 |
+
assert x.shape == (0, 0)
|
| 534 |
+
assert x.dtype == solve(np.eye(1, dtype=dt_ab), np.ones(1, dtype=dt_b)).dtype
|
| 535 |
+
|
| 536 |
+
|
| 537 |
+
class TestSolve:
|
| 538 |
+
def setup_method(self):
|
| 539 |
+
np.random.seed(1234)
|
| 540 |
+
|
| 541 |
+
@pytest.mark.thread_unsafe
|
| 542 |
+
def test_20Feb04_bug(self):
|
| 543 |
+
a = [[1, 1], [1.0, 0]] # ok
|
| 544 |
+
x0 = solve(a, [1, 0j])
|
| 545 |
+
assert_array_almost_equal(dot(a, x0), [1, 0])
|
| 546 |
+
|
| 547 |
+
# gives failure with clapack.zgesv(..,rowmajor=0)
|
| 548 |
+
a = [[1, 1], [1.2, 0]]
|
| 549 |
+
b = [1, 0j]
|
| 550 |
+
x0 = solve(a, b)
|
| 551 |
+
assert_array_almost_equal(dot(a, x0), [1, 0])
|
| 552 |
+
|
| 553 |
+
def test_simple(self):
|
| 554 |
+
a = [[1, 20], [-30, 4]]
|
| 555 |
+
for b in ([[1, 0], [0, 1]],
|
| 556 |
+
[1, 0],
|
| 557 |
+
[[2, 1], [-30, 4]]
|
| 558 |
+
):
|
| 559 |
+
x = solve(a, b)
|
| 560 |
+
assert_array_almost_equal(dot(a, x), b)
|
| 561 |
+
|
| 562 |
+
def test_simple_complex(self):
|
| 563 |
+
a = array([[5, 2], [2j, 4]], 'D')
|
| 564 |
+
for b in ([1j, 0],
|
| 565 |
+
[[1j, 1j], [0, 2]],
|
| 566 |
+
[1, 0j],
|
| 567 |
+
array([1, 0], 'D'),
|
| 568 |
+
):
|
| 569 |
+
x = solve(a, b)
|
| 570 |
+
assert_array_almost_equal(dot(a, x), b)
|
| 571 |
+
|
| 572 |
+
def test_simple_pos(self):
|
| 573 |
+
a = [[2, 3], [3, 5]]
|
| 574 |
+
for lower in [0, 1]:
|
| 575 |
+
for b in ([[1, 0], [0, 1]],
|
| 576 |
+
[1, 0]
|
| 577 |
+
):
|
| 578 |
+
x = solve(a, b, assume_a='pos', lower=lower)
|
| 579 |
+
assert_array_almost_equal(dot(a, x), b)
|
| 580 |
+
|
| 581 |
+
def test_simple_pos_complexb(self):
|
| 582 |
+
a = [[5, 2], [2, 4]]
|
| 583 |
+
for b in ([1j, 0],
|
| 584 |
+
[[1j, 1j], [0, 2]],
|
| 585 |
+
):
|
| 586 |
+
x = solve(a, b, assume_a='pos')
|
| 587 |
+
assert_array_almost_equal(dot(a, x), b)
|
| 588 |
+
|
| 589 |
+
def test_simple_sym(self):
|
| 590 |
+
a = [[2, 3], [3, -5]]
|
| 591 |
+
for lower in [0, 1]:
|
| 592 |
+
for b in ([[1, 0], [0, 1]],
|
| 593 |
+
[1, 0]
|
| 594 |
+
):
|
| 595 |
+
x = solve(a, b, assume_a='sym', lower=lower)
|
| 596 |
+
assert_array_almost_equal(dot(a, x), b)
|
| 597 |
+
|
| 598 |
+
def test_simple_sym_complexb(self):
|
| 599 |
+
a = [[5, 2], [2, -4]]
|
| 600 |
+
for b in ([1j, 0],
|
| 601 |
+
[[1j, 1j], [0, 2]]
|
| 602 |
+
):
|
| 603 |
+
x = solve(a, b, assume_a='sym')
|
| 604 |
+
assert_array_almost_equal(dot(a, x), b)
|
| 605 |
+
|
| 606 |
+
def test_simple_sym_complex(self):
|
| 607 |
+
a = [[5, 2+1j], [2+1j, -4]]
|
| 608 |
+
for b in ([1j, 0],
|
| 609 |
+
[1, 0],
|
| 610 |
+
[[1j, 1j], [0, 2]]
|
| 611 |
+
):
|
| 612 |
+
x = solve(a, b, assume_a='sym')
|
| 613 |
+
assert_array_almost_equal(dot(a, x), b)
|
| 614 |
+
|
| 615 |
+
def test_simple_her_actuallysym(self):
|
| 616 |
+
a = [[2, 3], [3, -5]]
|
| 617 |
+
for lower in [0, 1]:
|
| 618 |
+
for b in ([[1, 0], [0, 1]],
|
| 619 |
+
[1, 0],
|
| 620 |
+
[1j, 0],
|
| 621 |
+
):
|
| 622 |
+
x = solve(a, b, assume_a='her', lower=lower)
|
| 623 |
+
assert_array_almost_equal(dot(a, x), b)
|
| 624 |
+
|
| 625 |
+
def test_simple_her(self):
|
| 626 |
+
a = [[5, 2+1j], [2-1j, -4]]
|
| 627 |
+
for b in ([1j, 0],
|
| 628 |
+
[1, 0],
|
| 629 |
+
[[1j, 1j], [0, 2]]
|
| 630 |
+
):
|
| 631 |
+
x = solve(a, b, assume_a='her')
|
| 632 |
+
assert_array_almost_equal(dot(a, x), b)
|
| 633 |
+
|
| 634 |
+
def test_nils_20Feb04(self):
|
| 635 |
+
n = 2
|
| 636 |
+
A = random([n, n])+random([n, n])*1j
|
| 637 |
+
X = zeros((n, n), 'D')
|
| 638 |
+
Ainv = inv(A)
|
| 639 |
+
R = identity(n)+identity(n)*0j
|
| 640 |
+
for i in arange(0, n):
|
| 641 |
+
r = R[:, i]
|
| 642 |
+
X[:, i] = solve(A, r)
|
| 643 |
+
assert_array_almost_equal(X, Ainv)
|
| 644 |
+
|
| 645 |
+
def test_random(self):
|
| 646 |
+
|
| 647 |
+
n = 20
|
| 648 |
+
a = random([n, n])
|
| 649 |
+
for i in range(n):
|
| 650 |
+
a[i, i] = 20*(.1+a[i, i])
|
| 651 |
+
for i in range(4):
|
| 652 |
+
b = random([n, 3])
|
| 653 |
+
x = solve(a, b)
|
| 654 |
+
assert_array_almost_equal(dot(a, x), b)
|
| 655 |
+
|
| 656 |
+
def test_random_complex(self):
|
| 657 |
+
n = 20
|
| 658 |
+
a = random([n, n]) + 1j * random([n, n])
|
| 659 |
+
for i in range(n):
|
| 660 |
+
a[i, i] = 20*(.1+a[i, i])
|
| 661 |
+
for i in range(2):
|
| 662 |
+
b = random([n, 3])
|
| 663 |
+
x = solve(a, b)
|
| 664 |
+
assert_array_almost_equal(dot(a, x), b)
|
| 665 |
+
|
| 666 |
+
def test_random_sym(self):
|
| 667 |
+
n = 20
|
| 668 |
+
a = random([n, n])
|
| 669 |
+
for i in range(n):
|
| 670 |
+
a[i, i] = abs(20*(.1+a[i, i]))
|
| 671 |
+
for j in range(i):
|
| 672 |
+
a[i, j] = a[j, i]
|
| 673 |
+
for i in range(4):
|
| 674 |
+
b = random([n])
|
| 675 |
+
x = solve(a, b, assume_a="pos")
|
| 676 |
+
assert_array_almost_equal(dot(a, x), b)
|
| 677 |
+
|
| 678 |
+
def test_random_sym_complex(self):
|
| 679 |
+
n = 20
|
| 680 |
+
a = random([n, n])
|
| 681 |
+
a = a + 1j*random([n, n])
|
| 682 |
+
for i in range(n):
|
| 683 |
+
a[i, i] = abs(20*(.1+a[i, i]))
|
| 684 |
+
for j in range(i):
|
| 685 |
+
a[i, j] = conjugate(a[j, i])
|
| 686 |
+
b = random([n])+2j*random([n])
|
| 687 |
+
for i in range(2):
|
| 688 |
+
x = solve(a, b, assume_a="pos")
|
| 689 |
+
assert_array_almost_equal(dot(a, x), b)
|
| 690 |
+
|
| 691 |
+
def test_check_finite(self):
|
| 692 |
+
a = [[1, 20], [-30, 4]]
|
| 693 |
+
for b in ([[1, 0], [0, 1]], [1, 0],
|
| 694 |
+
[[2, 1], [-30, 4]]):
|
| 695 |
+
x = solve(a, b, check_finite=False)
|
| 696 |
+
assert_array_almost_equal(dot(a, x), b)
|
| 697 |
+
|
| 698 |
+
def test_scalar_a_and_1D_b(self):
|
| 699 |
+
a = 1
|
| 700 |
+
b = [1, 2, 3]
|
| 701 |
+
x = solve(a, b)
|
| 702 |
+
assert_array_almost_equal(x.ravel(), b)
|
| 703 |
+
assert_(x.shape == (3,), 'Scalar_a_1D_b test returned wrong shape')
|
| 704 |
+
|
| 705 |
+
def test_simple2(self):
|
| 706 |
+
a = np.array([[1.80, 2.88, 2.05, -0.89],
|
| 707 |
+
[525.00, -295.00, -95.00, -380.00],
|
| 708 |
+
[1.58, -2.69, -2.90, -1.04],
|
| 709 |
+
[-1.11, -0.66, -0.59, 0.80]])
|
| 710 |
+
|
| 711 |
+
b = np.array([[9.52, 18.47],
|
| 712 |
+
[2435.00, 225.00],
|
| 713 |
+
[0.77, -13.28],
|
| 714 |
+
[-6.22, -6.21]])
|
| 715 |
+
|
| 716 |
+
x = solve(a, b)
|
| 717 |
+
assert_array_almost_equal(x, np.array([[1., -1, 3, -5],
|
| 718 |
+
[3, 2, 4, 1]]).T)
|
| 719 |
+
|
| 720 |
+
def test_simple_complex2(self):
|
| 721 |
+
a = np.array([[-1.34+2.55j, 0.28+3.17j, -6.39-2.20j, 0.72-0.92j],
|
| 722 |
+
[-1.70-14.10j, 33.10-1.50j, -1.50+13.40j, 12.90+13.80j],
|
| 723 |
+
[-3.29-2.39j, -1.91+4.42j, -0.14-1.35j, 1.72+1.35j],
|
| 724 |
+
[2.41+0.39j, -0.56+1.47j, -0.83-0.69j, -1.96+0.67j]])
|
| 725 |
+
|
| 726 |
+
b = np.array([[26.26+51.78j, 31.32-6.70j],
|
| 727 |
+
[64.30-86.80j, 158.60-14.20j],
|
| 728 |
+
[-5.75+25.31j, -2.15+30.19j],
|
| 729 |
+
[1.16+2.57j, -2.56+7.55j]])
|
| 730 |
+
|
| 731 |
+
x = solve(a, b)
|
| 732 |
+
assert_array_almost_equal(x, np. array([[1+1.j, -1-2.j],
|
| 733 |
+
[2-3.j, 5+1.j],
|
| 734 |
+
[-4-5.j, -3+4.j],
|
| 735 |
+
[6.j, 2-3.j]]))
|
| 736 |
+
|
| 737 |
+
def test_hermitian(self):
|
| 738 |
+
# An upper triangular matrix will be used for hermitian matrix a
|
| 739 |
+
a = np.array([[-1.84, 0.11-0.11j, -1.78-1.18j, 3.91-1.50j],
|
| 740 |
+
[0, -4.63, -1.84+0.03j, 2.21+0.21j],
|
| 741 |
+
[0, 0, -8.87, 1.58-0.90j],
|
| 742 |
+
[0, 0, 0, -1.36]])
|
| 743 |
+
b = np.array([[2.98-10.18j, 28.68-39.89j],
|
| 744 |
+
[-9.58+3.88j, -24.79-8.40j],
|
| 745 |
+
[-0.77-16.05j, 4.23-70.02j],
|
| 746 |
+
[7.79+5.48j, -35.39+18.01j]])
|
| 747 |
+
res = np.array([[2.+1j, -8+6j],
|
| 748 |
+
[3.-2j, 7-2j],
|
| 749 |
+
[-1+2j, -1+5j],
|
| 750 |
+
[1.-1j, 3-4j]])
|
| 751 |
+
x = solve(a, b, assume_a='her')
|
| 752 |
+
assert_array_almost_equal(x, res)
|
| 753 |
+
# Also conjugate a and test for lower triangular data
|
| 754 |
+
x = solve(a.conj().T, b, assume_a='her', lower=True)
|
| 755 |
+
assert_array_almost_equal(x, res)
|
| 756 |
+
|
| 757 |
+
def test_pos_and_sym(self):
|
| 758 |
+
A = np.arange(1, 10).reshape(3, 3)
|
| 759 |
+
x = solve(np.tril(A)/9, np.ones(3), assume_a='pos')
|
| 760 |
+
assert_array_almost_equal(x, [9., 1.8, 1.])
|
| 761 |
+
x = solve(np.tril(A)/9, np.ones(3), assume_a='sym')
|
| 762 |
+
assert_array_almost_equal(x, [9., 1.8, 1.])
|
| 763 |
+
|
| 764 |
+
def test_singularity(self):
|
| 765 |
+
a = np.array([[1, 0, 0, 0, 0, 0, 1, 0, 1],
|
| 766 |
+
[1, 1, 1, 0, 0, 0, 1, 0, 1],
|
| 767 |
+
[0, 1, 1, 0, 0, 0, 1, 0, 1],
|
| 768 |
+
[1, 0, 1, 1, 1, 1, 0, 0, 0],
|
| 769 |
+
[1, 0, 1, 1, 1, 1, 0, 0, 0],
|
| 770 |
+
[1, 0, 1, 1, 1, 1, 0, 0, 0],
|
| 771 |
+
[1, 0, 1, 1, 1, 1, 0, 0, 0],
|
| 772 |
+
[1, 1, 1, 1, 1, 1, 1, 1, 1],
|
| 773 |
+
[1, 1, 1, 1, 1, 1, 1, 1, 1]])
|
| 774 |
+
b = np.arange(9)[:, None]
|
| 775 |
+
assert_raises(LinAlgError, solve, a, b)
|
| 776 |
+
|
| 777 |
+
@pytest.mark.thread_unsafe
|
| 778 |
+
@pytest.mark.parametrize('structure',
|
| 779 |
+
('diagonal', 'tridiagonal', 'lower triangular',
|
| 780 |
+
'upper triangular', 'symmetric', 'hermitian',
|
| 781 |
+
'positive definite', 'general', None))
|
| 782 |
+
def test_ill_condition_warning(self, structure):
|
| 783 |
+
rng = np.random.default_rng(234859349452)
|
| 784 |
+
n = 10
|
| 785 |
+
d = np.logspace(0, 50, n)
|
| 786 |
+
A = np.diag(d)
|
| 787 |
+
b = rng.random(size=n)
|
| 788 |
+
message = "Ill-conditioned matrix..."
|
| 789 |
+
with pytest.warns(LinAlgWarning, match=message):
|
| 790 |
+
solve(A, b, assume_a=structure)
|
| 791 |
+
|
| 792 |
+
def test_multiple_rhs(self):
|
| 793 |
+
a = np.eye(2)
|
| 794 |
+
b = np.random.rand(2, 3, 4)
|
| 795 |
+
x = solve(a, b)
|
| 796 |
+
assert_array_almost_equal(x, b)
|
| 797 |
+
|
| 798 |
+
def test_transposed_keyword(self):
|
| 799 |
+
A = np.arange(9).reshape(3, 3) + 1
|
| 800 |
+
x = solve(np.tril(A)/9, np.ones(3), transposed=True)
|
| 801 |
+
assert_array_almost_equal(x, [1.2, 0.2, 1])
|
| 802 |
+
x = solve(np.tril(A)/9, np.ones(3), transposed=False)
|
| 803 |
+
assert_array_almost_equal(x, [9, -5.4, -1.2])
|
| 804 |
+
|
| 805 |
+
def test_transposed_notimplemented(self):
|
| 806 |
+
a = np.eye(3).astype(complex)
|
| 807 |
+
with assert_raises(NotImplementedError):
|
| 808 |
+
solve(a, a, transposed=True)
|
| 809 |
+
|
| 810 |
+
def test_nonsquare_a(self):
|
| 811 |
+
assert_raises(ValueError, solve, [1, 2], 1)
|
| 812 |
+
|
| 813 |
+
def test_size_mismatch_with_1D_b(self):
|
| 814 |
+
assert_array_almost_equal(solve(np.eye(3), np.ones(3)), np.ones(3))
|
| 815 |
+
assert_raises(ValueError, solve, np.eye(3), np.ones(4))
|
| 816 |
+
|
| 817 |
+
def test_assume_a_keyword(self):
|
| 818 |
+
assert_raises(ValueError, solve, 1, 1, assume_a='zxcv')
|
| 819 |
+
|
| 820 |
+
@pytest.mark.skip(reason="Failure on OS X (gh-7500), "
|
| 821 |
+
"crash on Windows (gh-8064)")
|
| 822 |
+
def test_all_type_size_routine_combinations(self):
|
| 823 |
+
sizes = [10, 100]
|
| 824 |
+
assume_as = ['gen', 'sym', 'pos', 'her']
|
| 825 |
+
dtypes = [np.float32, np.float64, np.complex64, np.complex128]
|
| 826 |
+
for size, assume_a, dtype in itertools.product(sizes, assume_as,
|
| 827 |
+
dtypes):
|
| 828 |
+
is_complex = dtype in (np.complex64, np.complex128)
|
| 829 |
+
if assume_a == 'her' and not is_complex:
|
| 830 |
+
continue
|
| 831 |
+
|
| 832 |
+
err_msg = (f"Failed for size: {size}, assume_a: {assume_a},"
|
| 833 |
+
f"dtype: {dtype}")
|
| 834 |
+
|
| 835 |
+
a = np.random.randn(size, size).astype(dtype)
|
| 836 |
+
b = np.random.randn(size).astype(dtype)
|
| 837 |
+
if is_complex:
|
| 838 |
+
a = a + (1j*np.random.randn(size, size)).astype(dtype)
|
| 839 |
+
|
| 840 |
+
if assume_a == 'sym': # Can still be complex but only symmetric
|
| 841 |
+
a = a + a.T
|
| 842 |
+
elif assume_a == 'her': # Handle hermitian matrices here instead
|
| 843 |
+
a = a + a.T.conj()
|
| 844 |
+
elif assume_a == 'pos':
|
| 845 |
+
a = a.conj().T.dot(a) + 0.1*np.eye(size)
|
| 846 |
+
|
| 847 |
+
tol = 1e-12 if dtype in (np.float64, np.complex128) else 1e-6
|
| 848 |
+
|
| 849 |
+
if assume_a in ['gen', 'sym', 'her']:
|
| 850 |
+
# We revert the tolerance from before
|
| 851 |
+
# 4b4a6e7c34fa4060533db38f9a819b98fa81476c
|
| 852 |
+
if dtype in (np.float32, np.complex64):
|
| 853 |
+
tol *= 10
|
| 854 |
+
|
| 855 |
+
x = solve(a, b, assume_a=assume_a)
|
| 856 |
+
assert_allclose(a.dot(x), b,
|
| 857 |
+
atol=tol * size,
|
| 858 |
+
rtol=tol * size,
|
| 859 |
+
err_msg=err_msg)
|
| 860 |
+
|
| 861 |
+
if assume_a == 'sym' and dtype not in (np.complex64,
|
| 862 |
+
np.complex128):
|
| 863 |
+
x = solve(a, b, assume_a=assume_a, transposed=True)
|
| 864 |
+
assert_allclose(a.dot(x), b,
|
| 865 |
+
atol=tol * size,
|
| 866 |
+
rtol=tol * size,
|
| 867 |
+
err_msg=err_msg)
|
| 868 |
+
|
| 869 |
+
@pytest.mark.thread_unsafe
|
| 870 |
+
@pytest.mark.parametrize('dt_a', [int, float, np.float32, complex, np.complex64])
|
| 871 |
+
@pytest.mark.parametrize('dt_b', [int, float, np.float32, complex, np.complex64])
|
| 872 |
+
def test_empty(self, dt_a, dt_b):
|
| 873 |
+
a = np.empty((0, 0), dtype=dt_a)
|
| 874 |
+
b = np.empty(0, dtype=dt_b)
|
| 875 |
+
x = solve(a, b)
|
| 876 |
+
|
| 877 |
+
assert x.size == 0
|
| 878 |
+
dt_nonempty = solve(np.eye(2, dtype=dt_a), np.ones(2, dtype=dt_b)).dtype
|
| 879 |
+
assert x.dtype == dt_nonempty
|
| 880 |
+
|
| 881 |
+
def test_empty_rhs(self):
|
| 882 |
+
a = np.eye(2)
|
| 883 |
+
b = [[], []]
|
| 884 |
+
x = solve(a, b)
|
| 885 |
+
assert_(x.size == 0, 'Returned array is not empty')
|
| 886 |
+
assert_(x.shape == (2, 0), 'Returned empty array shape is wrong')
|
| 887 |
+
|
| 888 |
+
@pytest.mark.parametrize('dtype', [np.float64, np.complex128])
|
| 889 |
+
# "pos" and "positive definite" need to be added
|
| 890 |
+
@pytest.mark.parametrize('assume_a', ['diagonal', 'tridiagonal', 'banded',
|
| 891 |
+
'lower triangular', 'upper triangular',
|
| 892 |
+
'symmetric', 'hermitian',
|
| 893 |
+
'general', 'sym', 'her', 'gen'])
|
| 894 |
+
@pytest.mark.parametrize('nrhs', [(), (5,)])
|
| 895 |
+
@pytest.mark.parametrize('transposed', [True, False])
|
| 896 |
+
@pytest.mark.parametrize('overwrite', [True, False])
|
| 897 |
+
@pytest.mark.parametrize('fortran', [True, False])
|
| 898 |
+
def test_structure_detection(self, dtype, assume_a, nrhs, transposed,
|
| 899 |
+
overwrite, fortran):
|
| 900 |
+
rng = np.random.default_rng(982345982439826)
|
| 901 |
+
n = 5 if not assume_a == 'banded' else 20
|
| 902 |
+
b = rng.random(size=(n,) + nrhs)
|
| 903 |
+
A = rng.random(size=(n, n))
|
| 904 |
+
|
| 905 |
+
if np.issubdtype(dtype, np.complexfloating):
|
| 906 |
+
b = b + rng.random(size=(n,) + nrhs) * 1j
|
| 907 |
+
A = A + rng.random(size=(n, n)) * 1j
|
| 908 |
+
|
| 909 |
+
if assume_a == 'diagonal':
|
| 910 |
+
A = np.diag(np.diag(A))
|
| 911 |
+
elif assume_a == 'lower triangular':
|
| 912 |
+
A = np.tril(A)
|
| 913 |
+
elif assume_a == 'upper triangular':
|
| 914 |
+
A = np.triu(A)
|
| 915 |
+
elif assume_a == 'tridiagonal':
|
| 916 |
+
A = (np.diag(np.diag(A))
|
| 917 |
+
+ np.diag(np.diag(A, -1), -1)
|
| 918 |
+
+ np.diag(np.diag(A, 1), 1))
|
| 919 |
+
elif assume_a == 'banded':
|
| 920 |
+
A = np.triu(np.tril(A, 2), -1)
|
| 921 |
+
elif assume_a in {'symmetric', 'sym'}:
|
| 922 |
+
A = A + A.T
|
| 923 |
+
elif assume_a in {'hermitian', 'her'}:
|
| 924 |
+
A = A + A.conj().T
|
| 925 |
+
elif assume_a in {'positive definite', 'pos'}:
|
| 926 |
+
A = A + A.T
|
| 927 |
+
A += np.diag(A.sum(axis=1))
|
| 928 |
+
|
| 929 |
+
if fortran:
|
| 930 |
+
A = np.asfortranarray(A)
|
| 931 |
+
|
| 932 |
+
A_copy = A.copy(order='A')
|
| 933 |
+
b_copy = b.copy()
|
| 934 |
+
|
| 935 |
+
if np.issubdtype(dtype, np.complexfloating) and transposed:
|
| 936 |
+
message = "scipy.linalg.solve can currently..."
|
| 937 |
+
with pytest.raises(NotImplementedError, match=message):
|
| 938 |
+
solve(A, b, overwrite_a=overwrite, overwrite_b=overwrite,
|
| 939 |
+
transposed=transposed)
|
| 940 |
+
return
|
| 941 |
+
|
| 942 |
+
res = solve(A, b, overwrite_a=overwrite, overwrite_b=overwrite,
|
| 943 |
+
transposed=transposed, assume_a=assume_a)
|
| 944 |
+
|
| 945 |
+
# Check that solution this solution is *correct*
|
| 946 |
+
ref = np.linalg.solve(A_copy.T if transposed else A_copy, b_copy)
|
| 947 |
+
assert_allclose(res, ref)
|
| 948 |
+
|
| 949 |
+
# Check that `solve` correctly identifies the structure and returns
|
| 950 |
+
# *exactly* the same solution whether `assume_a` is specified or not
|
| 951 |
+
if assume_a != 'banded': # structure detection removed for banded
|
| 952 |
+
assert_equal(solve(A_copy, b_copy, transposed=transposed), res)
|
| 953 |
+
|
| 954 |
+
# Check that overwrite was respected
|
| 955 |
+
if not overwrite:
|
| 956 |
+
assert_equal(A, A_copy)
|
| 957 |
+
assert_equal(b, b_copy)
|
| 958 |
+
|
| 959 |
+
|
| 960 |
+
class TestSolveTriangular:
|
| 961 |
+
|
| 962 |
+
def test_simple(self):
|
| 963 |
+
"""
|
| 964 |
+
solve_triangular on a simple 2x2 matrix.
|
| 965 |
+
"""
|
| 966 |
+
A = array([[1, 0], [1, 2]])
|
| 967 |
+
b = [1, 1]
|
| 968 |
+
sol = solve_triangular(A, b, lower=True)
|
| 969 |
+
assert_array_almost_equal(sol, [1, 0])
|
| 970 |
+
|
| 971 |
+
# check that it works also for non-contiguous matrices
|
| 972 |
+
sol = solve_triangular(A.T, b, lower=False)
|
| 973 |
+
assert_array_almost_equal(sol, [.5, .5])
|
| 974 |
+
|
| 975 |
+
# and that it gives the same result as trans=1
|
| 976 |
+
sol = solve_triangular(A, b, lower=True, trans=1)
|
| 977 |
+
assert_array_almost_equal(sol, [.5, .5])
|
| 978 |
+
|
| 979 |
+
b = identity(2)
|
| 980 |
+
sol = solve_triangular(A, b, lower=True, trans=1)
|
| 981 |
+
assert_array_almost_equal(sol, [[1., -.5], [0, 0.5]])
|
| 982 |
+
|
| 983 |
+
def test_simple_complex(self):
|
| 984 |
+
"""
|
| 985 |
+
solve_triangular on a simple 2x2 complex matrix
|
| 986 |
+
"""
|
| 987 |
+
A = array([[1+1j, 0], [1j, 2]])
|
| 988 |
+
b = identity(2)
|
| 989 |
+
sol = solve_triangular(A, b, lower=True, trans=1)
|
| 990 |
+
assert_array_almost_equal(sol, [[.5-.5j, -.25-.25j], [0, 0.5]])
|
| 991 |
+
|
| 992 |
+
# check other option combinations with complex rhs
|
| 993 |
+
b = np.diag([1+1j, 1+2j])
|
| 994 |
+
sol = solve_triangular(A, b, lower=True, trans=0)
|
| 995 |
+
assert_array_almost_equal(sol, [[1, 0], [-0.5j, 0.5+1j]])
|
| 996 |
+
|
| 997 |
+
sol = solve_triangular(A, b, lower=True, trans=1)
|
| 998 |
+
assert_array_almost_equal(sol, [[1, 0.25-0.75j], [0, 0.5+1j]])
|
| 999 |
+
|
| 1000 |
+
sol = solve_triangular(A, b, lower=True, trans=2)
|
| 1001 |
+
assert_array_almost_equal(sol, [[1j, -0.75-0.25j], [0, 0.5+1j]])
|
| 1002 |
+
|
| 1003 |
+
sol = solve_triangular(A.T, b, lower=False, trans=0)
|
| 1004 |
+
assert_array_almost_equal(sol, [[1, 0.25-0.75j], [0, 0.5+1j]])
|
| 1005 |
+
|
| 1006 |
+
sol = solve_triangular(A.T, b, lower=False, trans=1)
|
| 1007 |
+
assert_array_almost_equal(sol, [[1, 0], [-0.5j, 0.5+1j]])
|
| 1008 |
+
|
| 1009 |
+
sol = solve_triangular(A.T, b, lower=False, trans=2)
|
| 1010 |
+
assert_array_almost_equal(sol, [[1j, 0], [-0.5, 0.5+1j]])
|
| 1011 |
+
|
| 1012 |
+
def test_check_finite(self):
|
| 1013 |
+
"""
|
| 1014 |
+
solve_triangular on a simple 2x2 matrix.
|
| 1015 |
+
"""
|
| 1016 |
+
A = array([[1, 0], [1, 2]])
|
| 1017 |
+
b = [1, 1]
|
| 1018 |
+
sol = solve_triangular(A, b, lower=True, check_finite=False)
|
| 1019 |
+
assert_array_almost_equal(sol, [1, 0])
|
| 1020 |
+
|
| 1021 |
+
@pytest.mark.parametrize('dt_a', [int, float, np.float32, complex, np.complex64])
|
| 1022 |
+
@pytest.mark.parametrize('dt_b', [int, float, np.float32, complex, np.complex64])
|
| 1023 |
+
def test_empty(self, dt_a, dt_b):
|
| 1024 |
+
a = np.empty((0, 0), dtype=dt_a)
|
| 1025 |
+
b = np.empty(0, dtype=dt_b)
|
| 1026 |
+
x = solve_triangular(a, b)
|
| 1027 |
+
|
| 1028 |
+
assert x.size == 0
|
| 1029 |
+
dt_nonempty = solve_triangular(
|
| 1030 |
+
np.eye(2, dtype=dt_a), np.ones(2, dtype=dt_b)
|
| 1031 |
+
).dtype
|
| 1032 |
+
assert x.dtype == dt_nonempty
|
| 1033 |
+
|
| 1034 |
+
def test_empty_rhs(self):
|
| 1035 |
+
a = np.eye(2)
|
| 1036 |
+
b = [[], []]
|
| 1037 |
+
x = solve_triangular(a, b)
|
| 1038 |
+
assert_(x.size == 0, 'Returned array is not empty')
|
| 1039 |
+
assert_(x.shape == (2, 0), 'Returned empty array shape is wrong')
|
| 1040 |
+
|
| 1041 |
+
|
| 1042 |
+
class TestInv:
|
| 1043 |
+
def setup_method(self):
|
| 1044 |
+
np.random.seed(1234)
|
| 1045 |
+
|
| 1046 |
+
def test_simple(self):
|
| 1047 |
+
a = [[1, 2], [3, 4]]
|
| 1048 |
+
a_inv = inv(a)
|
| 1049 |
+
assert_array_almost_equal(dot(a, a_inv), np.eye(2))
|
| 1050 |
+
a = [[1, 2, 3], [4, 5, 6], [7, 8, 10]]
|
| 1051 |
+
a_inv = inv(a)
|
| 1052 |
+
assert_array_almost_equal(dot(a, a_inv), np.eye(3))
|
| 1053 |
+
|
| 1054 |
+
def test_random(self):
|
| 1055 |
+
n = 20
|
| 1056 |
+
for i in range(4):
|
| 1057 |
+
a = random([n, n])
|
| 1058 |
+
for i in range(n):
|
| 1059 |
+
a[i, i] = 20*(.1+a[i, i])
|
| 1060 |
+
a_inv = inv(a)
|
| 1061 |
+
assert_array_almost_equal(dot(a, a_inv),
|
| 1062 |
+
identity(n))
|
| 1063 |
+
|
| 1064 |
+
def test_simple_complex(self):
|
| 1065 |
+
a = [[1, 2], [3, 4j]]
|
| 1066 |
+
a_inv = inv(a)
|
| 1067 |
+
assert_array_almost_equal(dot(a, a_inv), [[1, 0], [0, 1]])
|
| 1068 |
+
|
| 1069 |
+
def test_random_complex(self):
|
| 1070 |
+
n = 20
|
| 1071 |
+
for i in range(4):
|
| 1072 |
+
a = random([n, n])+2j*random([n, n])
|
| 1073 |
+
for i in range(n):
|
| 1074 |
+
a[i, i] = 20*(.1+a[i, i])
|
| 1075 |
+
a_inv = inv(a)
|
| 1076 |
+
assert_array_almost_equal(dot(a, a_inv),
|
| 1077 |
+
identity(n))
|
| 1078 |
+
|
| 1079 |
+
def test_check_finite(self):
|
| 1080 |
+
a = [[1, 2], [3, 4]]
|
| 1081 |
+
a_inv = inv(a, check_finite=False)
|
| 1082 |
+
assert_array_almost_equal(dot(a, a_inv), [[1, 0], [0, 1]])
|
| 1083 |
+
|
| 1084 |
+
@pytest.mark.parametrize('dt', [int, float, np.float32, complex, np.complex64])
|
| 1085 |
+
def test_empty(self, dt):
|
| 1086 |
+
a = np.empty((0, 0), dtype=dt)
|
| 1087 |
+
a_inv = inv(a)
|
| 1088 |
+
assert a_inv.size == 0
|
| 1089 |
+
assert a_inv.dtype == inv(np.eye(2, dtype=dt)).dtype
|
| 1090 |
+
|
| 1091 |
+
|
| 1092 |
+
class TestDet:
|
| 1093 |
+
def setup_method(self):
|
| 1094 |
+
self.rng = np.random.default_rng(1680305949878959)
|
| 1095 |
+
|
| 1096 |
+
def test_1x1_all_singleton_dims(self):
|
| 1097 |
+
a = np.array([[1]])
|
| 1098 |
+
deta = det(a)
|
| 1099 |
+
assert deta.dtype.char == 'd'
|
| 1100 |
+
assert np.isscalar(deta)
|
| 1101 |
+
assert deta == 1.
|
| 1102 |
+
a = np.array([[[[1]]]], dtype='f')
|
| 1103 |
+
deta = det(a)
|
| 1104 |
+
assert deta.dtype.char == 'd'
|
| 1105 |
+
assert deta.shape == (1, 1)
|
| 1106 |
+
assert_equal(deta, [[1.0]])
|
| 1107 |
+
a = np.array([[[1 + 3.j]]], dtype=np.complex64)
|
| 1108 |
+
deta = det(a)
|
| 1109 |
+
assert deta.dtype.char == 'D'
|
| 1110 |
+
assert deta.shape == (1,)
|
| 1111 |
+
assert_equal(deta, [1.+3.j])
|
| 1112 |
+
|
| 1113 |
+
def test_1by1_stacked_input_output(self):
|
| 1114 |
+
a = self.rng.random([4, 5, 1, 1], dtype=np.float32)
|
| 1115 |
+
deta = det(a)
|
| 1116 |
+
assert deta.dtype.char == 'd'
|
| 1117 |
+
assert deta.shape == (4, 5)
|
| 1118 |
+
assert_allclose(deta, np.squeeze(a))
|
| 1119 |
+
|
| 1120 |
+
a = self.rng.random([4, 5, 1, 1], dtype=np.float32)*np.complex64(1.j)
|
| 1121 |
+
deta = det(a)
|
| 1122 |
+
assert deta.dtype.char == 'D'
|
| 1123 |
+
assert deta.shape == (4, 5)
|
| 1124 |
+
assert_allclose(deta, np.squeeze(a))
|
| 1125 |
+
|
| 1126 |
+
@pytest.mark.parametrize('shape', [[2, 2], [20, 20], [3, 2, 20, 20]])
|
| 1127 |
+
def test_simple_det_shapes_real_complex(self, shape):
|
| 1128 |
+
a = self.rng.uniform(-1., 1., size=shape)
|
| 1129 |
+
d1, d2 = det(a), np.linalg.det(a)
|
| 1130 |
+
assert_allclose(d1, d2)
|
| 1131 |
+
|
| 1132 |
+
b = self.rng.uniform(-1., 1., size=shape)*1j
|
| 1133 |
+
b += self.rng.uniform(-0.5, 0.5, size=shape)
|
| 1134 |
+
d3, d4 = det(b), np.linalg.det(b)
|
| 1135 |
+
assert_allclose(d3, d4)
|
| 1136 |
+
|
| 1137 |
+
def test_for_known_det_values(self):
|
| 1138 |
+
# Hadamard8
|
| 1139 |
+
a = np.array([[1, 1, 1, 1, 1, 1, 1, 1],
|
| 1140 |
+
[1, -1, 1, -1, 1, -1, 1, -1],
|
| 1141 |
+
[1, 1, -1, -1, 1, 1, -1, -1],
|
| 1142 |
+
[1, -1, -1, 1, 1, -1, -1, 1],
|
| 1143 |
+
[1, 1, 1, 1, -1, -1, -1, -1],
|
| 1144 |
+
[1, -1, 1, -1, -1, 1, -1, 1],
|
| 1145 |
+
[1, 1, -1, -1, -1, -1, 1, 1],
|
| 1146 |
+
[1, -1, -1, 1, -1, 1, 1, -1]])
|
| 1147 |
+
assert_allclose(det(a), 4096.)
|
| 1148 |
+
|
| 1149 |
+
# consecutive number array always singular
|
| 1150 |
+
assert_allclose(det(np.arange(25).reshape(5, 5)), 0.)
|
| 1151 |
+
|
| 1152 |
+
# simple anti-diagonal block array
|
| 1153 |
+
# Upper right has det (-2+1j) and lower right has (-2-1j)
|
| 1154 |
+
# det(a) = - (-2+1j) (-2-1j) = 5.
|
| 1155 |
+
a = np.array([[0.+0.j, 0.+0.j, 0.-1.j, 1.-1.j],
|
| 1156 |
+
[0.+0.j, 0.+0.j, 1.+0.j, 0.-1.j],
|
| 1157 |
+
[0.+1.j, 1.+1.j, 0.+0.j, 0.+0.j],
|
| 1158 |
+
[1.+0.j, 0.+1.j, 0.+0.j, 0.+0.j]], dtype=np.complex64)
|
| 1159 |
+
assert_allclose(det(a), 5.+0.j)
|
| 1160 |
+
|
| 1161 |
+
# Fiedler companion complexified
|
| 1162 |
+
# >>> a = scipy.linalg.fiedler_companion(np.arange(1, 10))
|
| 1163 |
+
a = np.array([[-2., -3., 1., 0., 0., 0., 0., 0.],
|
| 1164 |
+
[1., 0., 0., 0., 0., 0., 0., 0.],
|
| 1165 |
+
[0., -4., 0., -5., 1., 0., 0., 0.],
|
| 1166 |
+
[0., 1., 0., 0., 0., 0., 0., 0.],
|
| 1167 |
+
[0., 0., 0., -6., 0., -7., 1., 0.],
|
| 1168 |
+
[0., 0., 0., 1., 0., 0., 0., 0.],
|
| 1169 |
+
[0., 0., 0., 0., 0., -8., 0., -9.],
|
| 1170 |
+
[0., 0., 0., 0., 0., 1., 0., 0.]])*1.j
|
| 1171 |
+
assert_allclose(det(a), 9.)
|
| 1172 |
+
|
| 1173 |
+
# g and G dtypes are handled differently in windows and other platforms
|
| 1174 |
+
@pytest.mark.parametrize('typ', [x for x in np.typecodes['All'][:20]
|
| 1175 |
+
if x not in 'gG'])
|
| 1176 |
+
def test_sample_compatible_dtype_input(self, typ):
|
| 1177 |
+
n = 4
|
| 1178 |
+
a = self.rng.random([n, n]).astype(typ) # value is not important
|
| 1179 |
+
assert isinstance(det(a), (np.float64 | np.complex128))
|
| 1180 |
+
|
| 1181 |
+
def test_incompatible_dtype_input(self):
|
| 1182 |
+
# Double backslashes needed for escaping pytest regex.
|
| 1183 |
+
msg = 'cannot be cast to float\\(32, 64\\)'
|
| 1184 |
+
|
| 1185 |
+
for c, t in zip('SUO', ['bytes8', 'str32', 'object']):
|
| 1186 |
+
with assert_raises(TypeError, match=msg):
|
| 1187 |
+
det(np.array([['a', 'b']]*2, dtype=c))
|
| 1188 |
+
with assert_raises(TypeError, match=msg):
|
| 1189 |
+
det(np.array([[b'a', b'b']]*2, dtype='V'))
|
| 1190 |
+
with assert_raises(TypeError, match=msg):
|
| 1191 |
+
det(np.array([[100, 200]]*2, dtype='datetime64[s]'))
|
| 1192 |
+
with assert_raises(TypeError, match=msg):
|
| 1193 |
+
det(np.array([[100, 200]]*2, dtype='timedelta64[s]'))
|
| 1194 |
+
|
| 1195 |
+
def test_empty_edge_cases(self):
|
| 1196 |
+
assert_allclose(det(np.empty([0, 0])), 1.)
|
| 1197 |
+
assert_allclose(det(np.empty([0, 0, 0])), np.array([]))
|
| 1198 |
+
assert_allclose(det(np.empty([3, 0, 0])), np.array([1., 1., 1.]))
|
| 1199 |
+
with assert_raises(ValueError, match='Last 2 dimensions'):
|
| 1200 |
+
det(np.empty([0, 0, 3]))
|
| 1201 |
+
with assert_raises(ValueError, match='at least two-dimensional'):
|
| 1202 |
+
det(np.array([]))
|
| 1203 |
+
with assert_raises(ValueError, match='Last 2 dimensions'):
|
| 1204 |
+
det(np.array([[]]))
|
| 1205 |
+
with assert_raises(ValueError, match='Last 2 dimensions'):
|
| 1206 |
+
det(np.array([[[]]]))
|
| 1207 |
+
|
| 1208 |
+
@pytest.mark.parametrize('dt', [int, float, np.float32, complex, np.complex64])
|
| 1209 |
+
def test_empty_dtype(self, dt):
|
| 1210 |
+
a = np.empty((0, 0), dtype=dt)
|
| 1211 |
+
d = det(a)
|
| 1212 |
+
assert d.shape == ()
|
| 1213 |
+
assert d.dtype == det(np.eye(2, dtype=dt)).dtype
|
| 1214 |
+
|
| 1215 |
+
a = np.empty((3, 0, 0), dtype=dt)
|
| 1216 |
+
d = det(a)
|
| 1217 |
+
assert d.shape == (3,)
|
| 1218 |
+
assert d.dtype == det(np.zeros((3, 1, 1), dtype=dt)).dtype
|
| 1219 |
+
|
| 1220 |
+
def test_overwrite_a(self):
|
| 1221 |
+
# If all conditions are met then input should be overwritten;
|
| 1222 |
+
# - dtype is one of 'fdFD'
|
| 1223 |
+
# - C-contiguous
|
| 1224 |
+
# - writeable
|
| 1225 |
+
a = np.arange(9).reshape(3, 3).astype(np.float32)
|
| 1226 |
+
ac = a.copy()
|
| 1227 |
+
deta = det(ac, overwrite_a=True)
|
| 1228 |
+
assert_allclose(deta, 0.)
|
| 1229 |
+
assert not (a == ac).all()
|
| 1230 |
+
|
| 1231 |
+
def test_readonly_array(self):
|
| 1232 |
+
a = np.array([[2., 0., 1.], [5., 3., -1.], [1., 1., 1.]])
|
| 1233 |
+
a.setflags(write=False)
|
| 1234 |
+
# overwrite_a will be overridden
|
| 1235 |
+
assert_allclose(det(a, overwrite_a=True), 10.)
|
| 1236 |
+
|
| 1237 |
+
def test_simple_check_finite(self):
|
| 1238 |
+
a = [[1, 2], [3, np.inf]]
|
| 1239 |
+
with assert_raises(ValueError, match='array must not contain'):
|
| 1240 |
+
det(a)
|
| 1241 |
+
|
| 1242 |
+
|
| 1243 |
+
def direct_lstsq(a, b, cmplx=0):
|
| 1244 |
+
at = transpose(a)
|
| 1245 |
+
if cmplx:
|
| 1246 |
+
at = conjugate(at)
|
| 1247 |
+
a1 = dot(at, a)
|
| 1248 |
+
b1 = dot(at, b)
|
| 1249 |
+
return solve(a1, b1)
|
| 1250 |
+
|
| 1251 |
+
|
| 1252 |
+
class TestLstsq:
|
| 1253 |
+
lapack_drivers = ('gelsd', 'gelss', 'gelsy', None)
|
| 1254 |
+
|
| 1255 |
+
def test_simple_exact(self):
|
| 1256 |
+
for dtype in REAL_DTYPES:
|
| 1257 |
+
a = np.array([[1, 20], [-30, 4]], dtype=dtype)
|
| 1258 |
+
for lapack_driver in TestLstsq.lapack_drivers:
|
| 1259 |
+
for overwrite in (True, False):
|
| 1260 |
+
for bt in (((1, 0), (0, 1)), (1, 0),
|
| 1261 |
+
((2, 1), (-30, 4))):
|
| 1262 |
+
# Store values in case they are overwritten
|
| 1263 |
+
# later
|
| 1264 |
+
a1 = a.copy()
|
| 1265 |
+
b = np.array(bt, dtype=dtype)
|
| 1266 |
+
b1 = b.copy()
|
| 1267 |
+
out = lstsq(a1, b1,
|
| 1268 |
+
lapack_driver=lapack_driver,
|
| 1269 |
+
overwrite_a=overwrite,
|
| 1270 |
+
overwrite_b=overwrite)
|
| 1271 |
+
x = out[0]
|
| 1272 |
+
r = out[2]
|
| 1273 |
+
assert_(r == 2,
|
| 1274 |
+
f'expected efficient rank 2, got {r}')
|
| 1275 |
+
assert_allclose(dot(a, x), b,
|
| 1276 |
+
atol=25 * _eps_cast(a1.dtype),
|
| 1277 |
+
rtol=25 * _eps_cast(a1.dtype),
|
| 1278 |
+
err_msg=f"driver: {lapack_driver}")
|
| 1279 |
+
|
| 1280 |
+
def test_simple_overdet(self):
|
| 1281 |
+
for dtype in REAL_DTYPES:
|
| 1282 |
+
a = np.array([[1, 2], [4, 5], [3, 4]], dtype=dtype)
|
| 1283 |
+
b = np.array([1, 2, 3], dtype=dtype)
|
| 1284 |
+
for lapack_driver in TestLstsq.lapack_drivers:
|
| 1285 |
+
for overwrite in (True, False):
|
| 1286 |
+
# Store values in case they are overwritten later
|
| 1287 |
+
a1 = a.copy()
|
| 1288 |
+
b1 = b.copy()
|
| 1289 |
+
out = lstsq(a1, b1, lapack_driver=lapack_driver,
|
| 1290 |
+
overwrite_a=overwrite,
|
| 1291 |
+
overwrite_b=overwrite)
|
| 1292 |
+
x = out[0]
|
| 1293 |
+
if lapack_driver == 'gelsy':
|
| 1294 |
+
residuals = np.sum((b - a.dot(x))**2)
|
| 1295 |
+
else:
|
| 1296 |
+
residuals = out[1]
|
| 1297 |
+
r = out[2]
|
| 1298 |
+
assert_(r == 2, f'expected efficient rank 2, got {r}')
|
| 1299 |
+
assert_allclose(abs((dot(a, x) - b)**2).sum(axis=0),
|
| 1300 |
+
residuals,
|
| 1301 |
+
rtol=25 * _eps_cast(a1.dtype),
|
| 1302 |
+
atol=25 * _eps_cast(a1.dtype),
|
| 1303 |
+
err_msg=f"driver: {lapack_driver}")
|
| 1304 |
+
assert_allclose(x, (-0.428571428571429, 0.85714285714285),
|
| 1305 |
+
rtol=25 * _eps_cast(a1.dtype),
|
| 1306 |
+
atol=25 * _eps_cast(a1.dtype),
|
| 1307 |
+
err_msg=f"driver: {lapack_driver}")
|
| 1308 |
+
|
| 1309 |
+
def test_simple_overdet_complex(self):
|
| 1310 |
+
for dtype in COMPLEX_DTYPES:
|
| 1311 |
+
a = np.array([[1+2j, 2], [4, 5], [3, 4]], dtype=dtype)
|
| 1312 |
+
b = np.array([1, 2+4j, 3], dtype=dtype)
|
| 1313 |
+
for lapack_driver in TestLstsq.lapack_drivers:
|
| 1314 |
+
for overwrite in (True, False):
|
| 1315 |
+
# Store values in case they are overwritten later
|
| 1316 |
+
a1 = a.copy()
|
| 1317 |
+
b1 = b.copy()
|
| 1318 |
+
out = lstsq(a1, b1, lapack_driver=lapack_driver,
|
| 1319 |
+
overwrite_a=overwrite,
|
| 1320 |
+
overwrite_b=overwrite)
|
| 1321 |
+
|
| 1322 |
+
x = out[0]
|
| 1323 |
+
if lapack_driver == 'gelsy':
|
| 1324 |
+
res = b - a.dot(x)
|
| 1325 |
+
residuals = np.sum(res * res.conj())
|
| 1326 |
+
else:
|
| 1327 |
+
residuals = out[1]
|
| 1328 |
+
r = out[2]
|
| 1329 |
+
assert_(r == 2, f'expected efficient rank 2, got {r}')
|
| 1330 |
+
assert_allclose(abs((dot(a, x) - b)**2).sum(axis=0),
|
| 1331 |
+
residuals,
|
| 1332 |
+
rtol=25 * _eps_cast(a1.dtype),
|
| 1333 |
+
atol=25 * _eps_cast(a1.dtype),
|
| 1334 |
+
err_msg=f"driver: {lapack_driver}")
|
| 1335 |
+
assert_allclose(
|
| 1336 |
+
x, (-0.4831460674157303 + 0.258426966292135j,
|
| 1337 |
+
0.921348314606741 + 0.292134831460674j),
|
| 1338 |
+
rtol=25 * _eps_cast(a1.dtype),
|
| 1339 |
+
atol=25 * _eps_cast(a1.dtype),
|
| 1340 |
+
err_msg=f"driver: {lapack_driver}")
|
| 1341 |
+
|
| 1342 |
+
def test_simple_underdet(self):
|
| 1343 |
+
for dtype in REAL_DTYPES:
|
| 1344 |
+
a = np.array([[1, 2, 3], [4, 5, 6]], dtype=dtype)
|
| 1345 |
+
b = np.array([1, 2], dtype=dtype)
|
| 1346 |
+
for lapack_driver in TestLstsq.lapack_drivers:
|
| 1347 |
+
for overwrite in (True, False):
|
| 1348 |
+
# Store values in case they are overwritten later
|
| 1349 |
+
a1 = a.copy()
|
| 1350 |
+
b1 = b.copy()
|
| 1351 |
+
out = lstsq(a1, b1, lapack_driver=lapack_driver,
|
| 1352 |
+
overwrite_a=overwrite,
|
| 1353 |
+
overwrite_b=overwrite)
|
| 1354 |
+
|
| 1355 |
+
x = out[0]
|
| 1356 |
+
r = out[2]
|
| 1357 |
+
assert_(r == 2, f'expected efficient rank 2, got {r}')
|
| 1358 |
+
assert_allclose(x, (-0.055555555555555, 0.111111111111111,
|
| 1359 |
+
0.277777777777777),
|
| 1360 |
+
rtol=25 * _eps_cast(a1.dtype),
|
| 1361 |
+
atol=25 * _eps_cast(a1.dtype),
|
| 1362 |
+
err_msg=f"driver: {lapack_driver}")
|
| 1363 |
+
|
| 1364 |
+
@pytest.mark.parametrize("dtype", REAL_DTYPES)
|
| 1365 |
+
@pytest.mark.parametrize("n", (20, 200))
|
| 1366 |
+
@pytest.mark.parametrize("lapack_driver", lapack_drivers)
|
| 1367 |
+
@pytest.mark.parametrize("overwrite", (True, False))
|
| 1368 |
+
def test_random_exact(self, dtype, n, lapack_driver, overwrite):
|
| 1369 |
+
rng = np.random.RandomState(1234)
|
| 1370 |
+
|
| 1371 |
+
a = np.asarray(rng.random([n, n]), dtype=dtype)
|
| 1372 |
+
for i in range(n):
|
| 1373 |
+
a[i, i] = 20 * (0.1 + a[i, i])
|
| 1374 |
+
for i in range(4):
|
| 1375 |
+
b = np.asarray(rng.random([n, 3]), dtype=dtype)
|
| 1376 |
+
# Store values in case they are overwritten later
|
| 1377 |
+
a1 = a.copy()
|
| 1378 |
+
b1 = b.copy()
|
| 1379 |
+
out = lstsq(a1, b1,
|
| 1380 |
+
lapack_driver=lapack_driver,
|
| 1381 |
+
overwrite_a=overwrite,
|
| 1382 |
+
overwrite_b=overwrite)
|
| 1383 |
+
x = out[0]
|
| 1384 |
+
r = out[2]
|
| 1385 |
+
assert_(r == n, f'expected efficient rank {n}, '
|
| 1386 |
+
f'got {r}')
|
| 1387 |
+
if dtype is np.float32:
|
| 1388 |
+
assert_allclose(
|
| 1389 |
+
dot(a, x), b,
|
| 1390 |
+
rtol=500 * _eps_cast(a1.dtype),
|
| 1391 |
+
atol=500 * _eps_cast(a1.dtype),
|
| 1392 |
+
err_msg=f"driver: {lapack_driver}")
|
| 1393 |
+
else:
|
| 1394 |
+
assert_allclose(
|
| 1395 |
+
dot(a, x), b,
|
| 1396 |
+
rtol=1000 * _eps_cast(a1.dtype),
|
| 1397 |
+
atol=1000 * _eps_cast(a1.dtype),
|
| 1398 |
+
err_msg=f"driver: {lapack_driver}")
|
| 1399 |
+
|
| 1400 |
+
@pytest.mark.skipif(IS_MUSL, reason="may segfault on Alpine, see gh-17630")
|
| 1401 |
+
@pytest.mark.parametrize("dtype", COMPLEX_DTYPES)
|
| 1402 |
+
@pytest.mark.parametrize("n", (20, 200))
|
| 1403 |
+
@pytest.mark.parametrize("lapack_driver", lapack_drivers)
|
| 1404 |
+
@pytest.mark.parametrize("overwrite", (True, False))
|
| 1405 |
+
def test_random_complex_exact(self, dtype, n, lapack_driver, overwrite):
|
| 1406 |
+
rng = np.random.RandomState(1234)
|
| 1407 |
+
|
| 1408 |
+
a = np.asarray(rng.random([n, n]) + 1j*rng.random([n, n]),
|
| 1409 |
+
dtype=dtype)
|
| 1410 |
+
for i in range(n):
|
| 1411 |
+
a[i, i] = 20 * (0.1 + a[i, i])
|
| 1412 |
+
for i in range(2):
|
| 1413 |
+
b = np.asarray(rng.random([n, 3]), dtype=dtype)
|
| 1414 |
+
# Store values in case they are overwritten later
|
| 1415 |
+
a1 = a.copy()
|
| 1416 |
+
b1 = b.copy()
|
| 1417 |
+
out = lstsq(a1, b1, lapack_driver=lapack_driver,
|
| 1418 |
+
overwrite_a=overwrite,
|
| 1419 |
+
overwrite_b=overwrite)
|
| 1420 |
+
x = out[0]
|
| 1421 |
+
r = out[2]
|
| 1422 |
+
assert_(r == n, f'expected efficient rank {n}, '
|
| 1423 |
+
f'got {r}')
|
| 1424 |
+
if dtype is np.complex64:
|
| 1425 |
+
assert_allclose(
|
| 1426 |
+
dot(a, x), b,
|
| 1427 |
+
rtol=400 * _eps_cast(a1.dtype),
|
| 1428 |
+
atol=400 * _eps_cast(a1.dtype),
|
| 1429 |
+
err_msg=f"driver: {lapack_driver}")
|
| 1430 |
+
else:
|
| 1431 |
+
assert_allclose(
|
| 1432 |
+
dot(a, x), b,
|
| 1433 |
+
rtol=1000 * _eps_cast(a1.dtype),
|
| 1434 |
+
atol=1000 * _eps_cast(a1.dtype),
|
| 1435 |
+
err_msg=f"driver: {lapack_driver}")
|
| 1436 |
+
|
| 1437 |
+
def test_random_overdet(self):
|
| 1438 |
+
rng = np.random.RandomState(1234)
|
| 1439 |
+
for dtype in REAL_DTYPES:
|
| 1440 |
+
for (n, m) in ((20, 15), (200, 2)):
|
| 1441 |
+
for lapack_driver in TestLstsq.lapack_drivers:
|
| 1442 |
+
for overwrite in (True, False):
|
| 1443 |
+
a = np.asarray(rng.random([n, m]), dtype=dtype)
|
| 1444 |
+
for i in range(m):
|
| 1445 |
+
a[i, i] = 20 * (0.1 + a[i, i])
|
| 1446 |
+
for i in range(4):
|
| 1447 |
+
b = np.asarray(rng.random([n, 3]), dtype=dtype)
|
| 1448 |
+
# Store values in case they are overwritten later
|
| 1449 |
+
a1 = a.copy()
|
| 1450 |
+
b1 = b.copy()
|
| 1451 |
+
out = lstsq(a1, b1,
|
| 1452 |
+
lapack_driver=lapack_driver,
|
| 1453 |
+
overwrite_a=overwrite,
|
| 1454 |
+
overwrite_b=overwrite)
|
| 1455 |
+
x = out[0]
|
| 1456 |
+
r = out[2]
|
| 1457 |
+
assert_(r == m, f'expected efficient rank {m}, '
|
| 1458 |
+
f'got {r}')
|
| 1459 |
+
assert_allclose(
|
| 1460 |
+
x, direct_lstsq(a, b, cmplx=0),
|
| 1461 |
+
rtol=25 * _eps_cast(a1.dtype),
|
| 1462 |
+
atol=25 * _eps_cast(a1.dtype),
|
| 1463 |
+
err_msg=f"driver: {lapack_driver}")
|
| 1464 |
+
|
| 1465 |
+
def test_random_complex_overdet(self):
|
| 1466 |
+
rng = np.random.RandomState(1234)
|
| 1467 |
+
for dtype in COMPLEX_DTYPES:
|
| 1468 |
+
for (n, m) in ((20, 15), (200, 2)):
|
| 1469 |
+
for lapack_driver in TestLstsq.lapack_drivers:
|
| 1470 |
+
for overwrite in (True, False):
|
| 1471 |
+
a = np.asarray(rng.random([n, m]) + 1j*rng.random([n, m]),
|
| 1472 |
+
dtype=dtype)
|
| 1473 |
+
for i in range(m):
|
| 1474 |
+
a[i, i] = 20 * (0.1 + a[i, i])
|
| 1475 |
+
for i in range(2):
|
| 1476 |
+
b = np.asarray(rng.random([n, 3]), dtype=dtype)
|
| 1477 |
+
# Store values in case they are overwritten
|
| 1478 |
+
# later
|
| 1479 |
+
a1 = a.copy()
|
| 1480 |
+
b1 = b.copy()
|
| 1481 |
+
out = lstsq(a1, b1,
|
| 1482 |
+
lapack_driver=lapack_driver,
|
| 1483 |
+
overwrite_a=overwrite,
|
| 1484 |
+
overwrite_b=overwrite)
|
| 1485 |
+
x = out[0]
|
| 1486 |
+
r = out[2]
|
| 1487 |
+
assert_(r == m, f'expected efficient rank {m}, '
|
| 1488 |
+
f'got {r}')
|
| 1489 |
+
assert_allclose(
|
| 1490 |
+
x, direct_lstsq(a, b, cmplx=1),
|
| 1491 |
+
rtol=25 * _eps_cast(a1.dtype),
|
| 1492 |
+
atol=25 * _eps_cast(a1.dtype),
|
| 1493 |
+
err_msg=f"driver: {lapack_driver}")
|
| 1494 |
+
|
| 1495 |
+
def test_check_finite(self):
|
| 1496 |
+
with suppress_warnings() as sup:
|
| 1497 |
+
# On (some) OSX this tests triggers a warning (gh-7538)
|
| 1498 |
+
sup.filter(RuntimeWarning,
|
| 1499 |
+
"internal gelsd driver lwork query error,.*"
|
| 1500 |
+
"Falling back to 'gelss' driver.")
|
| 1501 |
+
|
| 1502 |
+
at = np.array(((1, 20), (-30, 4)))
|
| 1503 |
+
for dtype, bt, lapack_driver, overwrite, check_finite in \
|
| 1504 |
+
itertools.product(REAL_DTYPES,
|
| 1505 |
+
(((1, 0), (0, 1)), (1, 0), ((2, 1), (-30, 4))),
|
| 1506 |
+
TestLstsq.lapack_drivers,
|
| 1507 |
+
(True, False),
|
| 1508 |
+
(True, False)):
|
| 1509 |
+
|
| 1510 |
+
a = at.astype(dtype)
|
| 1511 |
+
b = np.array(bt, dtype=dtype)
|
| 1512 |
+
# Store values in case they are overwritten
|
| 1513 |
+
# later
|
| 1514 |
+
a1 = a.copy()
|
| 1515 |
+
b1 = b.copy()
|
| 1516 |
+
out = lstsq(a1, b1, lapack_driver=lapack_driver,
|
| 1517 |
+
check_finite=check_finite, overwrite_a=overwrite,
|
| 1518 |
+
overwrite_b=overwrite)
|
| 1519 |
+
x = out[0]
|
| 1520 |
+
r = out[2]
|
| 1521 |
+
assert_(r == 2, f'expected efficient rank 2, got {r}')
|
| 1522 |
+
assert_allclose(dot(a, x), b,
|
| 1523 |
+
rtol=25 * _eps_cast(a.dtype),
|
| 1524 |
+
atol=25 * _eps_cast(a.dtype),
|
| 1525 |
+
err_msg=f"driver: {lapack_driver}")
|
| 1526 |
+
|
| 1527 |
+
def test_empty(self):
|
| 1528 |
+
for a_shape, b_shape in (((0, 2), (0,)),
|
| 1529 |
+
((0, 4), (0, 2)),
|
| 1530 |
+
((4, 0), (4,)),
|
| 1531 |
+
((4, 0), (4, 2))):
|
| 1532 |
+
b = np.ones(b_shape)
|
| 1533 |
+
x, residues, rank, s = lstsq(np.zeros(a_shape), b)
|
| 1534 |
+
assert_equal(x, np.zeros((a_shape[1],) + b_shape[1:]))
|
| 1535 |
+
residues_should_be = (np.empty((0,)) if a_shape[1]
|
| 1536 |
+
else np.linalg.norm(b, axis=0)**2)
|
| 1537 |
+
assert_equal(residues, residues_should_be)
|
| 1538 |
+
assert_(rank == 0, 'expected rank 0')
|
| 1539 |
+
assert_equal(s, np.empty((0,)))
|
| 1540 |
+
|
| 1541 |
+
@pytest.mark.parametrize('dt_a', [int, float, np.float32, complex, np.complex64])
|
| 1542 |
+
@pytest.mark.parametrize('dt_b', [int, float, np.float32, complex, np.complex64])
|
| 1543 |
+
def test_empty_dtype(self, dt_a, dt_b):
|
| 1544 |
+
a = np.empty((0, 0), dtype=dt_a)
|
| 1545 |
+
b = np.empty(0, dtype=dt_b)
|
| 1546 |
+
x, residues, rank, s = lstsq(a, b)
|
| 1547 |
+
|
| 1548 |
+
assert x.size == 0
|
| 1549 |
+
dt_nonempty = lstsq(np.eye(2, dtype=dt_a), np.ones(2, dtype=dt_b))[0].dtype
|
| 1550 |
+
assert x.dtype == dt_nonempty
|
| 1551 |
+
|
| 1552 |
+
|
| 1553 |
+
class TestPinv:
|
| 1554 |
+
def setup_method(self):
|
| 1555 |
+
np.random.seed(1234)
|
| 1556 |
+
|
| 1557 |
+
def test_simple_real(self):
|
| 1558 |
+
a = array([[1, 2, 3], [4, 5, 6], [7, 8, 10]], dtype=float)
|
| 1559 |
+
a_pinv = pinv(a)
|
| 1560 |
+
assert_array_almost_equal(dot(a, a_pinv), np.eye(3))
|
| 1561 |
+
|
| 1562 |
+
def test_simple_complex(self):
|
| 1563 |
+
a = (array([[1, 2, 3], [4, 5, 6], [7, 8, 10]],
|
| 1564 |
+
dtype=float) + 1j * array([[10, 8, 7], [6, 5, 4], [3, 2, 1]],
|
| 1565 |
+
dtype=float))
|
| 1566 |
+
a_pinv = pinv(a)
|
| 1567 |
+
assert_array_almost_equal(dot(a, a_pinv), np.eye(3))
|
| 1568 |
+
|
| 1569 |
+
def test_simple_singular(self):
|
| 1570 |
+
a = array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=float)
|
| 1571 |
+
a_pinv = pinv(a)
|
| 1572 |
+
expected = array([[-6.38888889e-01, -1.66666667e-01, 3.05555556e-01],
|
| 1573 |
+
[-5.55555556e-02, 1.30136518e-16, 5.55555556e-02],
|
| 1574 |
+
[5.27777778e-01, 1.66666667e-01, -1.94444444e-01]])
|
| 1575 |
+
assert_array_almost_equal(a_pinv, expected)
|
| 1576 |
+
|
| 1577 |
+
def test_simple_cols(self):
|
| 1578 |
+
a = array([[1, 2, 3], [4, 5, 6]], dtype=float)
|
| 1579 |
+
a_pinv = pinv(a)
|
| 1580 |
+
expected = array([[-0.94444444, 0.44444444],
|
| 1581 |
+
[-0.11111111, 0.11111111],
|
| 1582 |
+
[0.72222222, -0.22222222]])
|
| 1583 |
+
assert_array_almost_equal(a_pinv, expected)
|
| 1584 |
+
|
| 1585 |
+
def test_simple_rows(self):
|
| 1586 |
+
a = array([[1, 2], [3, 4], [5, 6]], dtype=float)
|
| 1587 |
+
a_pinv = pinv(a)
|
| 1588 |
+
expected = array([[-1.33333333, -0.33333333, 0.66666667],
|
| 1589 |
+
[1.08333333, 0.33333333, -0.41666667]])
|
| 1590 |
+
assert_array_almost_equal(a_pinv, expected)
|
| 1591 |
+
|
| 1592 |
+
def test_check_finite(self):
|
| 1593 |
+
a = array([[1, 2, 3], [4, 5, 6.], [7, 8, 10]])
|
| 1594 |
+
a_pinv = pinv(a, check_finite=False)
|
| 1595 |
+
assert_array_almost_equal(dot(a, a_pinv), np.eye(3))
|
| 1596 |
+
|
| 1597 |
+
def test_native_list_argument(self):
|
| 1598 |
+
a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
|
| 1599 |
+
a_pinv = pinv(a)
|
| 1600 |
+
expected = array([[-6.38888889e-01, -1.66666667e-01, 3.05555556e-01],
|
| 1601 |
+
[-5.55555556e-02, 1.30136518e-16, 5.55555556e-02],
|
| 1602 |
+
[5.27777778e-01, 1.66666667e-01, -1.94444444e-01]])
|
| 1603 |
+
assert_array_almost_equal(a_pinv, expected)
|
| 1604 |
+
|
| 1605 |
+
def test_atol_rtol(self):
|
| 1606 |
+
n = 12
|
| 1607 |
+
# get a random ortho matrix for shuffling
|
| 1608 |
+
q, _ = qr(np.random.rand(n, n))
|
| 1609 |
+
a_m = np.arange(35.0).reshape(7, 5)
|
| 1610 |
+
a = a_m.copy()
|
| 1611 |
+
a[0, 0] = 0.001
|
| 1612 |
+
atol = 1e-5
|
| 1613 |
+
rtol = 0.05
|
| 1614 |
+
# svds of a_m is ~ [116.906, 4.234, tiny, tiny, tiny]
|
| 1615 |
+
# svds of a is ~ [116.906, 4.234, 4.62959e-04, tiny, tiny]
|
| 1616 |
+
# Just abs cutoff such that we arrive at a_modified
|
| 1617 |
+
a_p = pinv(a_m, atol=atol, rtol=0.)
|
| 1618 |
+
adiff1 = a @ a_p @ a - a
|
| 1619 |
+
adiff2 = a_m @ a_p @ a_m - a_m
|
| 1620 |
+
# Now adiff1 should be around atol value while adiff2 should be
|
| 1621 |
+
# relatively tiny
|
| 1622 |
+
assert_allclose(np.linalg.norm(adiff1), 5e-4, atol=5.e-4)
|
| 1623 |
+
assert_allclose(np.linalg.norm(adiff2), 5e-14, atol=5.e-14)
|
| 1624 |
+
|
| 1625 |
+
# Now do the same but remove another sv ~4.234 via rtol
|
| 1626 |
+
a_p = pinv(a_m, atol=atol, rtol=rtol)
|
| 1627 |
+
adiff1 = a @ a_p @ a - a
|
| 1628 |
+
adiff2 = a_m @ a_p @ a_m - a_m
|
| 1629 |
+
assert_allclose(np.linalg.norm(adiff1), 4.233, rtol=0.01)
|
| 1630 |
+
assert_allclose(np.linalg.norm(adiff2), 4.233, rtol=0.01)
|
| 1631 |
+
|
| 1632 |
+
@pytest.mark.parametrize('dt', [float, np.float32, complex, np.complex64])
|
| 1633 |
+
def test_empty(self, dt):
|
| 1634 |
+
a = np.empty((0, 0), dtype=dt)
|
| 1635 |
+
a_pinv = pinv(a)
|
| 1636 |
+
assert a_pinv.size == 0
|
| 1637 |
+
assert a_pinv.dtype == pinv(np.eye(2, dtype=dt)).dtype
|
| 1638 |
+
|
| 1639 |
+
|
| 1640 |
+
class TestPinvSymmetric:
|
| 1641 |
+
|
| 1642 |
+
def setup_method(self):
|
| 1643 |
+
np.random.seed(1234)
|
| 1644 |
+
|
| 1645 |
+
def test_simple_real(self):
|
| 1646 |
+
a = array([[1, 2, 3], [4, 5, 6], [7, 8, 10]], dtype=float)
|
| 1647 |
+
a = np.dot(a, a.T)
|
| 1648 |
+
a_pinv = pinvh(a)
|
| 1649 |
+
assert_array_almost_equal(np.dot(a, a_pinv), np.eye(3))
|
| 1650 |
+
|
| 1651 |
+
def test_nonpositive(self):
|
| 1652 |
+
a = array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=float)
|
| 1653 |
+
a = np.dot(a, a.T)
|
| 1654 |
+
u, s, vt = np.linalg.svd(a)
|
| 1655 |
+
s[0] *= -1
|
| 1656 |
+
a = np.dot(u * s, vt) # a is now symmetric non-positive and singular
|
| 1657 |
+
a_pinv = pinv(a)
|
| 1658 |
+
a_pinvh = pinvh(a)
|
| 1659 |
+
assert_array_almost_equal(a_pinv, a_pinvh)
|
| 1660 |
+
|
| 1661 |
+
def test_simple_complex(self):
|
| 1662 |
+
a = (array([[1, 2, 3], [4, 5, 6], [7, 8, 10]],
|
| 1663 |
+
dtype=float) + 1j * array([[10, 8, 7], [6, 5, 4], [3, 2, 1]],
|
| 1664 |
+
dtype=float))
|
| 1665 |
+
a = np.dot(a, a.conj().T)
|
| 1666 |
+
a_pinv = pinvh(a)
|
| 1667 |
+
assert_array_almost_equal(np.dot(a, a_pinv), np.eye(3))
|
| 1668 |
+
|
| 1669 |
+
def test_native_list_argument(self):
|
| 1670 |
+
a = array([[1, 2, 3], [4, 5, 6], [7, 8, 10]], dtype=float)
|
| 1671 |
+
a = np.dot(a, a.T)
|
| 1672 |
+
a_pinv = pinvh(a.tolist())
|
| 1673 |
+
assert_array_almost_equal(np.dot(a, a_pinv), np.eye(3))
|
| 1674 |
+
|
| 1675 |
+
def test_zero_eigenvalue(self):
|
| 1676 |
+
# https://github.com/scipy/scipy/issues/12515
|
| 1677 |
+
# the SYEVR eigh driver may give the zero eigenvalue > eps
|
| 1678 |
+
a = np.array([[1, -1, 0], [-1, 2, -1], [0, -1, 1]])
|
| 1679 |
+
p = pinvh(a)
|
| 1680 |
+
assert_allclose(p @ a @ p, p, atol=1e-15)
|
| 1681 |
+
assert_allclose(a @ p @ a, a, atol=1e-15)
|
| 1682 |
+
|
| 1683 |
+
def test_atol_rtol(self):
|
| 1684 |
+
n = 12
|
| 1685 |
+
# get a random ortho matrix for shuffling
|
| 1686 |
+
q, _ = qr(np.random.rand(n, n))
|
| 1687 |
+
a = np.diag([4, 3, 2, 1, 0.99e-4, 0.99e-5] + [0.99e-6]*(n-6))
|
| 1688 |
+
a = q.T @ a @ q
|
| 1689 |
+
a_m = np.diag([4, 3, 2, 1, 0.99e-4, 0.] + [0.]*(n-6))
|
| 1690 |
+
a_m = q.T @ a_m @ q
|
| 1691 |
+
atol = 1e-5
|
| 1692 |
+
rtol = (4.01e-4 - 4e-5)/4
|
| 1693 |
+
# Just abs cutoff such that we arrive at a_modified
|
| 1694 |
+
a_p = pinvh(a, atol=atol, rtol=0.)
|
| 1695 |
+
adiff1 = a @ a_p @ a - a
|
| 1696 |
+
adiff2 = a_m @ a_p @ a_m - a_m
|
| 1697 |
+
# Now adiff1 should dance around atol value since truncation
|
| 1698 |
+
# while adiff2 should be relatively tiny
|
| 1699 |
+
assert_allclose(norm(adiff1), atol, rtol=0.1)
|
| 1700 |
+
assert_allclose(norm(adiff2), 1e-12, atol=1e-11)
|
| 1701 |
+
|
| 1702 |
+
# Now do the same but through rtol cancelling atol value
|
| 1703 |
+
a_p = pinvh(a, atol=atol, rtol=rtol)
|
| 1704 |
+
adiff1 = a @ a_p @ a - a
|
| 1705 |
+
adiff2 = a_m @ a_p @ a_m - a_m
|
| 1706 |
+
# adiff1 and adiff2 should be elevated to ~1e-4 due to mismatch
|
| 1707 |
+
assert_allclose(norm(adiff1), 1e-4, rtol=0.1)
|
| 1708 |
+
assert_allclose(norm(adiff2), 1e-4, rtol=0.1)
|
| 1709 |
+
|
| 1710 |
+
@pytest.mark.parametrize('dt', [float, np.float32, complex, np.complex64])
|
| 1711 |
+
def test_empty(self, dt):
|
| 1712 |
+
a = np.empty((0, 0), dtype=dt)
|
| 1713 |
+
a_pinv = pinvh(a)
|
| 1714 |
+
assert a_pinv.size == 0
|
| 1715 |
+
assert a_pinv.dtype == pinv(np.eye(2, dtype=dt)).dtype
|
| 1716 |
+
|
| 1717 |
+
|
| 1718 |
+
@pytest.mark.parametrize('scale', (1e-20, 1., 1e20))
|
| 1719 |
+
@pytest.mark.parametrize('pinv_', (pinv, pinvh))
|
| 1720 |
+
def test_auto_rcond(scale, pinv_):
|
| 1721 |
+
x = np.array([[1, 0], [0, 1e-10]]) * scale
|
| 1722 |
+
expected = np.diag(1. / np.diag(x))
|
| 1723 |
+
x_inv = pinv_(x)
|
| 1724 |
+
assert_allclose(x_inv, expected)
|
| 1725 |
+
|
| 1726 |
+
|
| 1727 |
+
class TestVectorNorms:
|
| 1728 |
+
|
| 1729 |
+
def test_types(self):
|
| 1730 |
+
for dtype in np.typecodes['AllFloat']:
|
| 1731 |
+
x = np.array([1, 2, 3], dtype=dtype)
|
| 1732 |
+
tol = max(1e-15, np.finfo(dtype).eps.real * 20)
|
| 1733 |
+
assert_allclose(norm(x), np.sqrt(14), rtol=tol)
|
| 1734 |
+
assert_allclose(norm(x, 2), np.sqrt(14), rtol=tol)
|
| 1735 |
+
|
| 1736 |
+
for dtype in np.typecodes['Complex']:
|
| 1737 |
+
x = np.array([1j, 2j, 3j], dtype=dtype)
|
| 1738 |
+
tol = max(1e-15, np.finfo(dtype).eps.real * 20)
|
| 1739 |
+
assert_allclose(norm(x), np.sqrt(14), rtol=tol)
|
| 1740 |
+
assert_allclose(norm(x, 2), np.sqrt(14), rtol=tol)
|
| 1741 |
+
|
| 1742 |
+
def test_overflow(self):
|
| 1743 |
+
# unlike numpy's norm, this one is
|
| 1744 |
+
# safer on overflow
|
| 1745 |
+
a = array([1e20], dtype=float32)
|
| 1746 |
+
assert_almost_equal(norm(a), a)
|
| 1747 |
+
|
| 1748 |
+
def test_stable(self):
|
| 1749 |
+
# more stable than numpy's norm
|
| 1750 |
+
a = array([1e4] + [1]*10000, dtype=float32)
|
| 1751 |
+
try:
|
| 1752 |
+
# snrm in double precision; we obtain the same as for float64
|
| 1753 |
+
# -- large atol needed due to varying blas implementations
|
| 1754 |
+
assert_allclose(norm(a) - 1e4, 0.5, atol=1e-2)
|
| 1755 |
+
except AssertionError:
|
| 1756 |
+
# snrm implemented in single precision, == np.linalg.norm result
|
| 1757 |
+
msg = ": Result should equal either 0.0 or 0.5 (depending on " \
|
| 1758 |
+
"implementation of snrm2)."
|
| 1759 |
+
assert_almost_equal(norm(a) - 1e4, 0.0, err_msg=msg)
|
| 1760 |
+
|
| 1761 |
+
def test_zero_norm(self):
|
| 1762 |
+
assert_equal(norm([1, 0, 3], 0), 2)
|
| 1763 |
+
assert_equal(norm([1, 2, 3], 0), 3)
|
| 1764 |
+
|
| 1765 |
+
def test_axis_kwd(self):
|
| 1766 |
+
a = np.array([[[2, 1], [3, 4]]] * 2, 'd')
|
| 1767 |
+
assert_allclose(norm(a, axis=1), [[3.60555128, 4.12310563]] * 2)
|
| 1768 |
+
assert_allclose(norm(a, 1, axis=1), [[5.] * 2] * 2)
|
| 1769 |
+
|
| 1770 |
+
def test_keepdims_kwd(self):
|
| 1771 |
+
a = np.array([[[2, 1], [3, 4]]] * 2, 'd')
|
| 1772 |
+
b = norm(a, axis=1, keepdims=True)
|
| 1773 |
+
assert_allclose(b, [[[3.60555128, 4.12310563]]] * 2)
|
| 1774 |
+
assert_(b.shape == (2, 1, 2))
|
| 1775 |
+
assert_allclose(norm(a, 1, axis=2, keepdims=True), [[[3.], [7.]]] * 2)
|
| 1776 |
+
|
| 1777 |
+
@pytest.mark.skipif(not HAS_ILP64, reason="64-bit BLAS required")
|
| 1778 |
+
def test_large_vector(self):
|
| 1779 |
+
check_free_memory(free_mb=17000)
|
| 1780 |
+
x = np.zeros([2**31], dtype=np.float64)
|
| 1781 |
+
x[-1] = 1
|
| 1782 |
+
res = norm(x)
|
| 1783 |
+
del x
|
| 1784 |
+
assert_allclose(res, 1.0)
|
| 1785 |
+
|
| 1786 |
+
|
| 1787 |
+
class TestMatrixNorms:
|
| 1788 |
+
|
| 1789 |
+
def test_matrix_norms(self):
|
| 1790 |
+
# Not all of these are matrix norms in the most technical sense.
|
| 1791 |
+
np.random.seed(1234)
|
| 1792 |
+
for n, m in (1, 1), (1, 3), (3, 1), (4, 4), (4, 5), (5, 4):
|
| 1793 |
+
for t in np.float32, np.float64, np.complex64, np.complex128, np.int64:
|
| 1794 |
+
A = 10 * np.random.randn(n, m).astype(t)
|
| 1795 |
+
if np.issubdtype(A.dtype, np.complexfloating):
|
| 1796 |
+
A = (A + 10j * np.random.randn(n, m)).astype(t)
|
| 1797 |
+
t_high = np.complex128
|
| 1798 |
+
else:
|
| 1799 |
+
t_high = np.float64
|
| 1800 |
+
for order in (None, 'fro', 1, -1, 2, -2, np.inf, -np.inf):
|
| 1801 |
+
actual = norm(A, ord=order)
|
| 1802 |
+
desired = np.linalg.norm(A, ord=order)
|
| 1803 |
+
# SciPy may return higher precision matrix norms.
|
| 1804 |
+
# This is a consequence of using LAPACK.
|
| 1805 |
+
if not np.allclose(actual, desired):
|
| 1806 |
+
desired = np.linalg.norm(A.astype(t_high), ord=order)
|
| 1807 |
+
assert_allclose(actual, desired)
|
| 1808 |
+
|
| 1809 |
+
def test_axis_kwd(self):
|
| 1810 |
+
a = np.array([[[2, 1], [3, 4]]] * 2, 'd')
|
| 1811 |
+
b = norm(a, ord=np.inf, axis=(1, 0))
|
| 1812 |
+
c = norm(np.swapaxes(a, 0, 1), ord=np.inf, axis=(0, 1))
|
| 1813 |
+
d = norm(a, ord=1, axis=(0, 1))
|
| 1814 |
+
assert_allclose(b, c)
|
| 1815 |
+
assert_allclose(c, d)
|
| 1816 |
+
assert_allclose(b, d)
|
| 1817 |
+
assert_(b.shape == c.shape == d.shape)
|
| 1818 |
+
b = norm(a, ord=1, axis=(1, 0))
|
| 1819 |
+
c = norm(np.swapaxes(a, 0, 1), ord=1, axis=(0, 1))
|
| 1820 |
+
d = norm(a, ord=np.inf, axis=(0, 1))
|
| 1821 |
+
assert_allclose(b, c)
|
| 1822 |
+
assert_allclose(c, d)
|
| 1823 |
+
assert_allclose(b, d)
|
| 1824 |
+
assert_(b.shape == c.shape == d.shape)
|
| 1825 |
+
|
| 1826 |
+
def test_keepdims_kwd(self):
|
| 1827 |
+
a = np.arange(120, dtype='d').reshape(2, 3, 4, 5)
|
| 1828 |
+
b = norm(a, ord=np.inf, axis=(1, 0), keepdims=True)
|
| 1829 |
+
c = norm(a, ord=1, axis=(0, 1), keepdims=True)
|
| 1830 |
+
assert_allclose(b, c)
|
| 1831 |
+
assert_(b.shape == c.shape)
|
| 1832 |
+
|
| 1833 |
+
def test_empty(self):
|
| 1834 |
+
a = np.empty((0, 0))
|
| 1835 |
+
assert_allclose(norm(a), 0.)
|
| 1836 |
+
assert_allclose(norm(a, axis=0), np.zeros((0,)))
|
| 1837 |
+
assert_allclose(norm(a, keepdims=True), np.zeros((1, 1)))
|
| 1838 |
+
|
| 1839 |
+
a = np.empty((0, 3))
|
| 1840 |
+
assert_allclose(norm(a), 0.)
|
| 1841 |
+
assert_allclose(norm(a, axis=0), np.zeros((3,)))
|
| 1842 |
+
assert_allclose(norm(a, keepdims=True), np.zeros((1, 1)))
|
| 1843 |
+
|
| 1844 |
+
|
| 1845 |
+
class TestOverwrite:
|
| 1846 |
+
def test_solve(self):
|
| 1847 |
+
assert_no_overwrite(solve, [(3, 3), (3,)])
|
| 1848 |
+
|
| 1849 |
+
def test_solve_triangular(self):
|
| 1850 |
+
assert_no_overwrite(solve_triangular, [(3, 3), (3,)])
|
| 1851 |
+
|
| 1852 |
+
def test_solve_banded(self):
|
| 1853 |
+
assert_no_overwrite(lambda ab, b: solve_banded((2, 1), ab, b),
|
| 1854 |
+
[(4, 6), (6,)])
|
| 1855 |
+
|
| 1856 |
+
def test_solveh_banded(self):
|
| 1857 |
+
assert_no_overwrite(solveh_banded, [(2, 6), (6,)])
|
| 1858 |
+
|
| 1859 |
+
def test_inv(self):
|
| 1860 |
+
assert_no_overwrite(inv, [(3, 3)])
|
| 1861 |
+
|
| 1862 |
+
def test_det(self):
|
| 1863 |
+
assert_no_overwrite(det, [(3, 3)])
|
| 1864 |
+
|
| 1865 |
+
def test_lstsq(self):
|
| 1866 |
+
assert_no_overwrite(lstsq, [(3, 2), (3,)])
|
| 1867 |
+
|
| 1868 |
+
def test_pinv(self):
|
| 1869 |
+
assert_no_overwrite(pinv, [(3, 3)])
|
| 1870 |
+
|
| 1871 |
+
def test_pinvh(self):
|
| 1872 |
+
assert_no_overwrite(pinvh, [(3, 3)])
|
| 1873 |
+
|
| 1874 |
+
|
| 1875 |
+
class TestSolveCirculant:
|
| 1876 |
+
|
| 1877 |
+
def test_basic1(self):
|
| 1878 |
+
c = np.array([1, 2, 3, 5])
|
| 1879 |
+
b = np.array([1, -1, 1, 0])
|
| 1880 |
+
x = solve_circulant(c, b)
|
| 1881 |
+
y = solve(circulant(c), b)
|
| 1882 |
+
assert_allclose(x, y)
|
| 1883 |
+
|
| 1884 |
+
def test_basic2(self):
|
| 1885 |
+
# b is a 2-d matrix.
|
| 1886 |
+
c = np.array([1, 2, -3, -5])
|
| 1887 |
+
b = np.arange(12).reshape(4, 3)
|
| 1888 |
+
x = solve_circulant(c, b)
|
| 1889 |
+
y = solve(circulant(c), b)
|
| 1890 |
+
assert_allclose(x, y)
|
| 1891 |
+
|
| 1892 |
+
def test_basic3(self):
|
| 1893 |
+
# b is a 3-d matrix.
|
| 1894 |
+
c = np.array([1, 2, -3, -5])
|
| 1895 |
+
b = np.arange(24).reshape(4, 3, 2)
|
| 1896 |
+
x = solve_circulant(c, b)
|
| 1897 |
+
y = solve(circulant(c), b)
|
| 1898 |
+
assert_allclose(x, y)
|
| 1899 |
+
|
| 1900 |
+
def test_complex(self):
|
| 1901 |
+
# Complex b and c
|
| 1902 |
+
c = np.array([1+2j, -3, 4j, 5])
|
| 1903 |
+
b = np.arange(8).reshape(4, 2) + 0.5j
|
| 1904 |
+
x = solve_circulant(c, b)
|
| 1905 |
+
y = solve(circulant(c), b)
|
| 1906 |
+
assert_allclose(x, y)
|
| 1907 |
+
|
| 1908 |
+
def test_random_b_and_c(self):
|
| 1909 |
+
# Random b and c
|
| 1910 |
+
rng = np.random.RandomState(54321)
|
| 1911 |
+
c = rng.randn(50)
|
| 1912 |
+
b = rng.randn(50)
|
| 1913 |
+
x = solve_circulant(c, b)
|
| 1914 |
+
y = solve(circulant(c), b)
|
| 1915 |
+
assert_allclose(x, y)
|
| 1916 |
+
|
| 1917 |
+
def test_singular(self):
|
| 1918 |
+
# c gives a singular circulant matrix.
|
| 1919 |
+
c = np.array([1, 1, 0, 0])
|
| 1920 |
+
b = np.array([1, 2, 3, 4])
|
| 1921 |
+
x = solve_circulant(c, b, singular='lstsq')
|
| 1922 |
+
y, res, rnk, s = lstsq(circulant(c), b)
|
| 1923 |
+
assert_allclose(x, y)
|
| 1924 |
+
assert_raises(LinAlgError, solve_circulant, x, y)
|
| 1925 |
+
|
| 1926 |
+
def test_axis_args(self):
|
| 1927 |
+
# Test use of caxis, baxis and outaxis.
|
| 1928 |
+
|
| 1929 |
+
# c has shape (2, 1, 4)
|
| 1930 |
+
c = np.array([[[-1, 2.5, 3, 3.5]], [[1, 6, 6, 6.5]]])
|
| 1931 |
+
|
| 1932 |
+
# b has shape (3, 4)
|
| 1933 |
+
b = np.array([[0, 0, 1, 1], [1, 1, 0, 0], [1, -1, 0, 0]])
|
| 1934 |
+
|
| 1935 |
+
x = solve_circulant(c, b, baxis=1)
|
| 1936 |
+
assert_equal(x.shape, (4, 2, 3))
|
| 1937 |
+
expected = np.empty_like(x)
|
| 1938 |
+
expected[:, 0, :] = solve(circulant(c[0].ravel()), b.T)
|
| 1939 |
+
expected[:, 1, :] = solve(circulant(c[1].ravel()), b.T)
|
| 1940 |
+
assert_allclose(x, expected)
|
| 1941 |
+
|
| 1942 |
+
x = solve_circulant(c, b, baxis=1, outaxis=-1)
|
| 1943 |
+
assert_equal(x.shape, (2, 3, 4))
|
| 1944 |
+
assert_allclose(np.moveaxis(x, -1, 0), expected)
|
| 1945 |
+
|
| 1946 |
+
# np.swapaxes(c, 1, 2) has shape (2, 4, 1); b.T has shape (4, 3).
|
| 1947 |
+
x = solve_circulant(np.swapaxes(c, 1, 2), b.T, caxis=1)
|
| 1948 |
+
assert_equal(x.shape, (4, 2, 3))
|
| 1949 |
+
assert_allclose(x, expected)
|
| 1950 |
+
|
| 1951 |
+
def test_native_list_arguments(self):
|
| 1952 |
+
# Same as test_basic1 using python's native list.
|
| 1953 |
+
c = [1, 2, 3, 5]
|
| 1954 |
+
b = [1, -1, 1, 0]
|
| 1955 |
+
x = solve_circulant(c, b)
|
| 1956 |
+
y = solve(circulant(c), b)
|
| 1957 |
+
assert_allclose(x, y)
|
| 1958 |
+
|
| 1959 |
+
@pytest.mark.parametrize('dt_c', [int, float, np.float32, complex, np.complex64])
|
| 1960 |
+
@pytest.mark.parametrize('dt_b', [int, float, np.float32, complex, np.complex64])
|
| 1961 |
+
def test_empty(self, dt_c, dt_b):
|
| 1962 |
+
c = np.array([], dtype=dt_c)
|
| 1963 |
+
b = np.array([], dtype=dt_b)
|
| 1964 |
+
x = solve_circulant(c, b)
|
| 1965 |
+
assert x.shape == (0,)
|
| 1966 |
+
assert x.dtype == solve_circulant(np.arange(3, dtype=dt_c),
|
| 1967 |
+
np.ones(3, dtype=dt_b)).dtype
|
| 1968 |
+
|
| 1969 |
+
b = np.empty((0, 0), dtype=dt_b)
|
| 1970 |
+
x1 = solve_circulant(c, b)
|
| 1971 |
+
assert x1.shape == (0, 0)
|
| 1972 |
+
assert x1.dtype == x.dtype
|
| 1973 |
+
|
| 1974 |
+
|
| 1975 |
+
class TestMatrix_Balance:
|
| 1976 |
+
|
| 1977 |
+
def test_string_arg(self):
|
| 1978 |
+
assert_raises(ValueError, matrix_balance, 'Some string for fail')
|
| 1979 |
+
|
| 1980 |
+
def test_infnan_arg(self):
|
| 1981 |
+
assert_raises(ValueError, matrix_balance,
|
| 1982 |
+
np.array([[1, 2], [3, np.inf]]))
|
| 1983 |
+
assert_raises(ValueError, matrix_balance,
|
| 1984 |
+
np.array([[1, 2], [3, np.nan]]))
|
| 1985 |
+
|
| 1986 |
+
def test_scaling(self):
|
| 1987 |
+
_, y = matrix_balance(np.array([[1000, 1], [1000, 0]]))
|
| 1988 |
+
# Pre/post LAPACK 3.5.0 gives the same result up to an offset
|
| 1989 |
+
# since in each case col norm is x1000 greater and
|
| 1990 |
+
# 1000 / 32 ~= 1 * 32 hence balanced with 2 ** 5.
|
| 1991 |
+
assert_allclose(np.diff(np.log2(np.diag(y))), [5])
|
| 1992 |
+
|
| 1993 |
+
def test_scaling_order(self):
|
| 1994 |
+
A = np.array([[1, 0, 1e-4], [1, 1, 1e-2], [1e4, 1e2, 1]])
|
| 1995 |
+
x, y = matrix_balance(A)
|
| 1996 |
+
assert_allclose(solve(y, A).dot(y), x)
|
| 1997 |
+
|
| 1998 |
+
def test_separate(self):
|
| 1999 |
+
_, (y, z) = matrix_balance(np.array([[1000, 1], [1000, 0]]),
|
| 2000 |
+
separate=1)
|
| 2001 |
+
assert_equal(np.diff(np.log2(y)), [5])
|
| 2002 |
+
assert_allclose(z, np.arange(2))
|
| 2003 |
+
|
| 2004 |
+
def test_permutation(self):
|
| 2005 |
+
A = block_diag(np.ones((2, 2)), np.tril(np.ones((2, 2))),
|
| 2006 |
+
np.ones((3, 3)))
|
| 2007 |
+
x, (y, z) = matrix_balance(A, separate=1)
|
| 2008 |
+
assert_allclose(y, np.ones_like(y))
|
| 2009 |
+
assert_allclose(z, np.array([0, 1, 6, 5, 4, 3, 2]))
|
| 2010 |
+
|
| 2011 |
+
def test_perm_and_scaling(self):
|
| 2012 |
+
# Matrix with its diagonal removed
|
| 2013 |
+
cases = ( # Case 0
|
| 2014 |
+
np.array([[0., 0., 0., 0., 0.000002],
|
| 2015 |
+
[0., 0., 0., 0., 0.],
|
| 2016 |
+
[2., 2., 0., 0., 0.],
|
| 2017 |
+
[2., 2., 0., 0., 0.],
|
| 2018 |
+
[0., 0., 0.000002, 0., 0.]]),
|
| 2019 |
+
# Case 1 user reported GH-7258
|
| 2020 |
+
np.array([[-0.5, 0., 0., 0.],
|
| 2021 |
+
[0., -1., 0., 0.],
|
| 2022 |
+
[1., 0., -0.5, 0.],
|
| 2023 |
+
[0., 1., 0., -1.]]),
|
| 2024 |
+
# Case 2 user reported GH-7258
|
| 2025 |
+
np.array([[-3., 0., 1., 0.],
|
| 2026 |
+
[-1., -1., -0., 1.],
|
| 2027 |
+
[-3., -0., -0., 0.],
|
| 2028 |
+
[-1., -0., 1., -1.]])
|
| 2029 |
+
)
|
| 2030 |
+
|
| 2031 |
+
for A in cases:
|
| 2032 |
+
x, y = matrix_balance(A)
|
| 2033 |
+
x, (s, p) = matrix_balance(A, separate=1)
|
| 2034 |
+
ip = np.empty_like(p)
|
| 2035 |
+
ip[p] = np.arange(A.shape[0])
|
| 2036 |
+
assert_allclose(y, np.diag(s)[ip, :])
|
| 2037 |
+
assert_allclose(solve(y, A).dot(y), x)
|
| 2038 |
+
|
| 2039 |
+
@pytest.mark.parametrize('dt', [int, float, np.float32, complex, np.complex64])
|
| 2040 |
+
def test_empty(self, dt):
|
| 2041 |
+
a = np.empty((0, 0), dtype=dt)
|
| 2042 |
+
b, t = matrix_balance(a)
|
| 2043 |
+
|
| 2044 |
+
assert b.size == 0
|
| 2045 |
+
assert t.size == 0
|
| 2046 |
+
|
| 2047 |
+
b_n, t_n = matrix_balance(np.eye(2, dtype=dt))
|
| 2048 |
+
assert b.dtype == b_n.dtype
|
| 2049 |
+
assert t.dtype == t_n.dtype
|
| 2050 |
+
|
| 2051 |
+
b, (scale, perm) = matrix_balance(a, separate=True)
|
| 2052 |
+
assert b.size == 0
|
| 2053 |
+
assert scale.size == 0
|
| 2054 |
+
assert perm.size == 0
|
| 2055 |
+
|
| 2056 |
+
b_n, (scale_n, perm_n) = matrix_balance(a, separate=True)
|
| 2057 |
+
assert b.dtype == b_n.dtype
|
| 2058 |
+
assert scale.dtype == scale_n.dtype
|
| 2059 |
+
assert perm.dtype == perm_n.dtype
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_blas.py
ADDED
|
@@ -0,0 +1,1127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# Created by: Pearu Peterson, April 2002
|
| 3 |
+
#
|
| 4 |
+
|
| 5 |
+
import math
|
| 6 |
+
import pytest
|
| 7 |
+
import numpy as np
|
| 8 |
+
import numpy.random
|
| 9 |
+
from numpy.testing import (assert_equal, assert_almost_equal, assert_,
|
| 10 |
+
assert_array_almost_equal, assert_allclose)
|
| 11 |
+
from pytest import raises as assert_raises
|
| 12 |
+
|
| 13 |
+
from numpy import float32, float64, complex64, complex128, arange, triu, \
|
| 14 |
+
tril, zeros, tril_indices, ones, mod, diag, append, eye, \
|
| 15 |
+
nonzero
|
| 16 |
+
|
| 17 |
+
import scipy
|
| 18 |
+
from scipy.linalg import _fblas as fblas, get_blas_funcs, toeplitz, solve
|
| 19 |
+
|
| 20 |
+
try:
|
| 21 |
+
from scipy.linalg import _cblas as cblas
|
| 22 |
+
except ImportError:
|
| 23 |
+
cblas = None
|
| 24 |
+
|
| 25 |
+
REAL_DTYPES = [float32, float64]
|
| 26 |
+
COMPLEX_DTYPES = [complex64, complex128]
|
| 27 |
+
DTYPES = REAL_DTYPES + COMPLEX_DTYPES
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def test_get_blas_funcs():
|
| 31 |
+
# check that it returns Fortran code for arrays that are
|
| 32 |
+
# fortran-ordered
|
| 33 |
+
f1, f2, f3 = get_blas_funcs(
|
| 34 |
+
('axpy', 'axpy', 'axpy'),
|
| 35 |
+
(np.empty((2, 2), dtype=np.complex64, order='F'),
|
| 36 |
+
np.empty((2, 2), dtype=np.complex128, order='C'))
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# get_blas_funcs will choose libraries depending on most generic
|
| 40 |
+
# array
|
| 41 |
+
assert_equal(f1.typecode, 'z')
|
| 42 |
+
assert_equal(f2.typecode, 'z')
|
| 43 |
+
if cblas is not None:
|
| 44 |
+
assert_equal(f1.module_name, 'cblas')
|
| 45 |
+
assert_equal(f2.module_name, 'cblas')
|
| 46 |
+
|
| 47 |
+
# check defaults.
|
| 48 |
+
f1 = get_blas_funcs('rotg')
|
| 49 |
+
assert_equal(f1.typecode, 'd')
|
| 50 |
+
|
| 51 |
+
# check also dtype interface
|
| 52 |
+
f1 = get_blas_funcs('gemm', dtype=np.complex64)
|
| 53 |
+
assert_equal(f1.typecode, 'c')
|
| 54 |
+
f1 = get_blas_funcs('gemm', dtype='F')
|
| 55 |
+
assert_equal(f1.typecode, 'c')
|
| 56 |
+
|
| 57 |
+
# extended precision complex
|
| 58 |
+
f1 = get_blas_funcs('gemm', dtype=np.clongdouble)
|
| 59 |
+
assert_equal(f1.typecode, 'z')
|
| 60 |
+
|
| 61 |
+
# check safe complex upcasting
|
| 62 |
+
f1 = get_blas_funcs('axpy',
|
| 63 |
+
(np.empty((2, 2), dtype=np.float64),
|
| 64 |
+
np.empty((2, 2), dtype=np.complex64))
|
| 65 |
+
)
|
| 66 |
+
assert_equal(f1.typecode, 'z')
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def test_get_blas_funcs_alias():
|
| 70 |
+
# check alias for get_blas_funcs
|
| 71 |
+
f, g = get_blas_funcs(('nrm2', 'dot'), dtype=np.complex64)
|
| 72 |
+
assert f.typecode == 'c'
|
| 73 |
+
assert g.typecode == 'c'
|
| 74 |
+
|
| 75 |
+
f, g, h = get_blas_funcs(('dot', 'dotc', 'dotu'), dtype=np.float64)
|
| 76 |
+
assert f is g
|
| 77 |
+
assert f is h
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
class TestCBLAS1Simple:
|
| 81 |
+
|
| 82 |
+
def test_axpy(self):
|
| 83 |
+
for p in 'sd':
|
| 84 |
+
f = getattr(cblas, p+'axpy', None)
|
| 85 |
+
if f is None:
|
| 86 |
+
continue
|
| 87 |
+
assert_array_almost_equal(f([1, 2, 3], [2, -1, 3], a=5),
|
| 88 |
+
[7, 9, 18])
|
| 89 |
+
for p in 'cz':
|
| 90 |
+
f = getattr(cblas, p+'axpy', None)
|
| 91 |
+
if f is None:
|
| 92 |
+
continue
|
| 93 |
+
assert_array_almost_equal(f([1, 2j, 3], [2, -1, 3], a=5),
|
| 94 |
+
[7, 10j-1, 18])
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
class TestFBLAS1Simple:
|
| 98 |
+
|
| 99 |
+
def test_axpy(self):
|
| 100 |
+
for p in 'sd':
|
| 101 |
+
f = getattr(fblas, p+'axpy', None)
|
| 102 |
+
if f is None:
|
| 103 |
+
continue
|
| 104 |
+
assert_array_almost_equal(f([1, 2, 3], [2, -1, 3], a=5),
|
| 105 |
+
[7, 9, 18])
|
| 106 |
+
for p in 'cz':
|
| 107 |
+
f = getattr(fblas, p+'axpy', None)
|
| 108 |
+
if f is None:
|
| 109 |
+
continue
|
| 110 |
+
assert_array_almost_equal(f([1, 2j, 3], [2, -1, 3], a=5),
|
| 111 |
+
[7, 10j-1, 18])
|
| 112 |
+
|
| 113 |
+
def test_copy(self):
|
| 114 |
+
for p in 'sd':
|
| 115 |
+
f = getattr(fblas, p+'copy', None)
|
| 116 |
+
if f is None:
|
| 117 |
+
continue
|
| 118 |
+
assert_array_almost_equal(f([3, 4, 5], [8]*3), [3, 4, 5])
|
| 119 |
+
for p in 'cz':
|
| 120 |
+
f = getattr(fblas, p+'copy', None)
|
| 121 |
+
if f is None:
|
| 122 |
+
continue
|
| 123 |
+
assert_array_almost_equal(f([3, 4j, 5+3j], [8]*3), [3, 4j, 5+3j])
|
| 124 |
+
|
| 125 |
+
def test_asum(self):
|
| 126 |
+
for p in 'sd':
|
| 127 |
+
f = getattr(fblas, p+'asum', None)
|
| 128 |
+
if f is None:
|
| 129 |
+
continue
|
| 130 |
+
assert_almost_equal(f([3, -4, 5]), 12)
|
| 131 |
+
for p in ['sc', 'dz']:
|
| 132 |
+
f = getattr(fblas, p+'asum', None)
|
| 133 |
+
if f is None:
|
| 134 |
+
continue
|
| 135 |
+
assert_almost_equal(f([3j, -4, 3-4j]), 14)
|
| 136 |
+
|
| 137 |
+
def test_dot(self):
|
| 138 |
+
for p in 'sd':
|
| 139 |
+
f = getattr(fblas, p+'dot', None)
|
| 140 |
+
if f is None:
|
| 141 |
+
continue
|
| 142 |
+
assert_almost_equal(f([3, -4, 5], [2, 5, 1]), -9)
|
| 143 |
+
|
| 144 |
+
def test_complex_dotu(self):
|
| 145 |
+
for p in 'cz':
|
| 146 |
+
f = getattr(fblas, p+'dotu', None)
|
| 147 |
+
if f is None:
|
| 148 |
+
continue
|
| 149 |
+
assert_almost_equal(f([3j, -4, 3-4j], [2, 3, 1]), -9+2j)
|
| 150 |
+
|
| 151 |
+
def test_complex_dotc(self):
|
| 152 |
+
for p in 'cz':
|
| 153 |
+
f = getattr(fblas, p+'dotc', None)
|
| 154 |
+
if f is None:
|
| 155 |
+
continue
|
| 156 |
+
assert_almost_equal(f([3j, -4, 3-4j], [2, 3j, 1]), 3-14j)
|
| 157 |
+
|
| 158 |
+
def test_nrm2(self):
|
| 159 |
+
for p in 'sd':
|
| 160 |
+
f = getattr(fblas, p+'nrm2', None)
|
| 161 |
+
if f is None:
|
| 162 |
+
continue
|
| 163 |
+
assert_almost_equal(f([3, -4, 5]), math.sqrt(50))
|
| 164 |
+
for p in ['c', 'z', 'sc', 'dz']:
|
| 165 |
+
f = getattr(fblas, p+'nrm2', None)
|
| 166 |
+
if f is None:
|
| 167 |
+
continue
|
| 168 |
+
assert_almost_equal(f([3j, -4, 3-4j]), math.sqrt(50))
|
| 169 |
+
|
| 170 |
+
def test_scal(self):
|
| 171 |
+
for p in 'sd':
|
| 172 |
+
f = getattr(fblas, p+'scal', None)
|
| 173 |
+
if f is None:
|
| 174 |
+
continue
|
| 175 |
+
assert_array_almost_equal(f(2, [3, -4, 5]), [6, -8, 10])
|
| 176 |
+
for p in 'cz':
|
| 177 |
+
f = getattr(fblas, p+'scal', None)
|
| 178 |
+
if f is None:
|
| 179 |
+
continue
|
| 180 |
+
assert_array_almost_equal(f(3j, [3j, -4, 3-4j]), [-9, -12j, 12+9j])
|
| 181 |
+
for p in ['cs', 'zd']:
|
| 182 |
+
f = getattr(fblas, p+'scal', None)
|
| 183 |
+
if f is None:
|
| 184 |
+
continue
|
| 185 |
+
assert_array_almost_equal(f(3, [3j, -4, 3-4j]), [9j, -12, 9-12j])
|
| 186 |
+
|
| 187 |
+
def test_swap(self):
|
| 188 |
+
for p in 'sd':
|
| 189 |
+
f = getattr(fblas, p+'swap', None)
|
| 190 |
+
if f is None:
|
| 191 |
+
continue
|
| 192 |
+
x, y = [2, 3, 1], [-2, 3, 7]
|
| 193 |
+
x1, y1 = f(x, y)
|
| 194 |
+
assert_array_almost_equal(x1, y)
|
| 195 |
+
assert_array_almost_equal(y1, x)
|
| 196 |
+
for p in 'cz':
|
| 197 |
+
f = getattr(fblas, p+'swap', None)
|
| 198 |
+
if f is None:
|
| 199 |
+
continue
|
| 200 |
+
x, y = [2, 3j, 1], [-2, 3, 7-3j]
|
| 201 |
+
x1, y1 = f(x, y)
|
| 202 |
+
assert_array_almost_equal(x1, y)
|
| 203 |
+
assert_array_almost_equal(y1, x)
|
| 204 |
+
|
| 205 |
+
def test_amax(self):
|
| 206 |
+
for p in 'sd':
|
| 207 |
+
f = getattr(fblas, 'i'+p+'amax')
|
| 208 |
+
assert_equal(f([-2, 4, 3]), 1)
|
| 209 |
+
for p in 'cz':
|
| 210 |
+
f = getattr(fblas, 'i'+p+'amax')
|
| 211 |
+
assert_equal(f([-5, 4+3j, 6]), 1)
|
| 212 |
+
# XXX: need tests for rot,rotm,rotg,rotmg
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
class TestFBLAS2Simple:
|
| 216 |
+
|
| 217 |
+
def test_gemv(self):
|
| 218 |
+
for p in 'sd':
|
| 219 |
+
f = getattr(fblas, p+'gemv', None)
|
| 220 |
+
if f is None:
|
| 221 |
+
continue
|
| 222 |
+
assert_array_almost_equal(f(3, [[3]], [-4]), [-36])
|
| 223 |
+
assert_array_almost_equal(f(3, [[3]], [-4], 3, [5]), [-21])
|
| 224 |
+
for p in 'cz':
|
| 225 |
+
f = getattr(fblas, p+'gemv', None)
|
| 226 |
+
if f is None:
|
| 227 |
+
continue
|
| 228 |
+
assert_array_almost_equal(f(3j, [[3-4j]], [-4]), [-48-36j])
|
| 229 |
+
assert_array_almost_equal(f(3j, [[3-4j]], [-4], 3, [5j]),
|
| 230 |
+
[-48-21j])
|
| 231 |
+
|
| 232 |
+
# All of these *ger* functions are segfaulting when called from multiple
|
| 233 |
+
# threads under free-threaded CPython, see gh-21936.
|
| 234 |
+
@pytest.mark.thread_unsafe
|
| 235 |
+
def test_ger(self):
|
| 236 |
+
|
| 237 |
+
for p in 'sd':
|
| 238 |
+
f = getattr(fblas, p+'ger', None)
|
| 239 |
+
if f is None:
|
| 240 |
+
continue
|
| 241 |
+
assert_array_almost_equal(f(1, [1, 2], [3, 4]), [[3, 4], [6, 8]])
|
| 242 |
+
assert_array_almost_equal(f(2, [1, 2, 3], [3, 4]),
|
| 243 |
+
[[6, 8], [12, 16], [18, 24]])
|
| 244 |
+
|
| 245 |
+
assert_array_almost_equal(f(1, [1, 2], [3, 4],
|
| 246 |
+
a=[[1, 2], [3, 4]]), [[4, 6], [9, 12]])
|
| 247 |
+
|
| 248 |
+
for p in 'cz':
|
| 249 |
+
f = getattr(fblas, p+'geru', None)
|
| 250 |
+
if f is None:
|
| 251 |
+
continue
|
| 252 |
+
assert_array_almost_equal(f(1, [1j, 2], [3, 4]),
|
| 253 |
+
[[3j, 4j], [6, 8]])
|
| 254 |
+
assert_array_almost_equal(f(-2, [1j, 2j, 3j], [3j, 4j]),
|
| 255 |
+
[[6, 8], [12, 16], [18, 24]])
|
| 256 |
+
|
| 257 |
+
for p in 'cz':
|
| 258 |
+
for name in ('ger', 'gerc'):
|
| 259 |
+
f = getattr(fblas, p+name, None)
|
| 260 |
+
if f is None:
|
| 261 |
+
continue
|
| 262 |
+
assert_array_almost_equal(f(1, [1j, 2], [3, 4]),
|
| 263 |
+
[[3j, 4j], [6, 8]])
|
| 264 |
+
assert_array_almost_equal(f(2, [1j, 2j, 3j], [3j, 4j]),
|
| 265 |
+
[[6, 8], [12, 16], [18, 24]])
|
| 266 |
+
|
| 267 |
+
def test_syr_her(self):
|
| 268 |
+
x = np.arange(1, 5, dtype='d')
|
| 269 |
+
resx = np.triu(x[:, np.newaxis] * x)
|
| 270 |
+
resx_reverse = np.triu(x[::-1, np.newaxis] * x[::-1])
|
| 271 |
+
|
| 272 |
+
y = np.linspace(0, 8.5, 17, endpoint=False)
|
| 273 |
+
|
| 274 |
+
z = np.arange(1, 9, dtype='d').view('D')
|
| 275 |
+
resz = np.triu(z[:, np.newaxis] * z)
|
| 276 |
+
resz_reverse = np.triu(z[::-1, np.newaxis] * z[::-1])
|
| 277 |
+
rehz = np.triu(z[:, np.newaxis] * z.conj())
|
| 278 |
+
rehz_reverse = np.triu(z[::-1, np.newaxis] * z[::-1].conj())
|
| 279 |
+
|
| 280 |
+
w = np.c_[np.zeros(4), z, np.zeros(4)].ravel()
|
| 281 |
+
|
| 282 |
+
for p, rtol in zip('sd', [1e-7, 1e-14]):
|
| 283 |
+
f = getattr(fblas, p+'syr', None)
|
| 284 |
+
if f is None:
|
| 285 |
+
continue
|
| 286 |
+
assert_allclose(f(1.0, x), resx, rtol=rtol)
|
| 287 |
+
assert_allclose(f(1.0, x, lower=True), resx.T, rtol=rtol)
|
| 288 |
+
assert_allclose(f(1.0, y, incx=2, offx=2, n=4), resx, rtol=rtol)
|
| 289 |
+
# negative increments imply reversed vectors in blas
|
| 290 |
+
assert_allclose(f(1.0, y, incx=-2, offx=2, n=4),
|
| 291 |
+
resx_reverse, rtol=rtol)
|
| 292 |
+
|
| 293 |
+
a = np.zeros((4, 4), 'f' if p == 's' else 'd', 'F')
|
| 294 |
+
b = f(1.0, x, a=a, overwrite_a=True)
|
| 295 |
+
assert_allclose(a, resx, rtol=rtol)
|
| 296 |
+
|
| 297 |
+
b = f(2.0, x, a=a)
|
| 298 |
+
assert_(a is not b)
|
| 299 |
+
assert_allclose(b, 3*resx, rtol=rtol)
|
| 300 |
+
|
| 301 |
+
assert_raises(Exception, f, 1.0, x, incx=0)
|
| 302 |
+
assert_raises(Exception, f, 1.0, x, offx=5)
|
| 303 |
+
assert_raises(Exception, f, 1.0, x, offx=-2)
|
| 304 |
+
assert_raises(Exception, f, 1.0, x, n=-2)
|
| 305 |
+
assert_raises(Exception, f, 1.0, x, n=5)
|
| 306 |
+
assert_raises(Exception, f, 1.0, x, lower=2)
|
| 307 |
+
assert_raises(Exception, f, 1.0, x, a=np.zeros((2, 2), 'd', 'F'))
|
| 308 |
+
|
| 309 |
+
for p, rtol in zip('cz', [1e-7, 1e-14]):
|
| 310 |
+
f = getattr(fblas, p+'syr', None)
|
| 311 |
+
if f is None:
|
| 312 |
+
continue
|
| 313 |
+
assert_allclose(f(1.0, z), resz, rtol=rtol)
|
| 314 |
+
assert_allclose(f(1.0, z, lower=True), resz.T, rtol=rtol)
|
| 315 |
+
assert_allclose(f(1.0, w, incx=3, offx=1, n=4), resz, rtol=rtol)
|
| 316 |
+
# negative increments imply reversed vectors in blas
|
| 317 |
+
assert_allclose(f(1.0, w, incx=-3, offx=1, n=4),
|
| 318 |
+
resz_reverse, rtol=rtol)
|
| 319 |
+
|
| 320 |
+
a = np.zeros((4, 4), 'F' if p == 'c' else 'D', 'F')
|
| 321 |
+
b = f(1.0, z, a=a, overwrite_a=True)
|
| 322 |
+
assert_allclose(a, resz, rtol=rtol)
|
| 323 |
+
|
| 324 |
+
b = f(2.0, z, a=a)
|
| 325 |
+
assert_(a is not b)
|
| 326 |
+
assert_allclose(b, 3*resz, rtol=rtol)
|
| 327 |
+
|
| 328 |
+
assert_raises(Exception, f, 1.0, x, incx=0)
|
| 329 |
+
assert_raises(Exception, f, 1.0, x, offx=5)
|
| 330 |
+
assert_raises(Exception, f, 1.0, x, offx=-2)
|
| 331 |
+
assert_raises(Exception, f, 1.0, x, n=-2)
|
| 332 |
+
assert_raises(Exception, f, 1.0, x, n=5)
|
| 333 |
+
assert_raises(Exception, f, 1.0, x, lower=2)
|
| 334 |
+
assert_raises(Exception, f, 1.0, x, a=np.zeros((2, 2), 'd', 'F'))
|
| 335 |
+
|
| 336 |
+
for p, rtol in zip('cz', [1e-7, 1e-14]):
|
| 337 |
+
f = getattr(fblas, p+'her', None)
|
| 338 |
+
if f is None:
|
| 339 |
+
continue
|
| 340 |
+
assert_allclose(f(1.0, z), rehz, rtol=rtol)
|
| 341 |
+
assert_allclose(f(1.0, z, lower=True), rehz.T.conj(), rtol=rtol)
|
| 342 |
+
assert_allclose(f(1.0, w, incx=3, offx=1, n=4), rehz, rtol=rtol)
|
| 343 |
+
# negative increments imply reversed vectors in blas
|
| 344 |
+
assert_allclose(f(1.0, w, incx=-3, offx=1, n=4),
|
| 345 |
+
rehz_reverse, rtol=rtol)
|
| 346 |
+
|
| 347 |
+
a = np.zeros((4, 4), 'F' if p == 'c' else 'D', 'F')
|
| 348 |
+
b = f(1.0, z, a=a, overwrite_a=True)
|
| 349 |
+
assert_allclose(a, rehz, rtol=rtol)
|
| 350 |
+
|
| 351 |
+
b = f(2.0, z, a=a)
|
| 352 |
+
assert_(a is not b)
|
| 353 |
+
assert_allclose(b, 3*rehz, rtol=rtol)
|
| 354 |
+
|
| 355 |
+
assert_raises(Exception, f, 1.0, x, incx=0)
|
| 356 |
+
assert_raises(Exception, f, 1.0, x, offx=5)
|
| 357 |
+
assert_raises(Exception, f, 1.0, x, offx=-2)
|
| 358 |
+
assert_raises(Exception, f, 1.0, x, n=-2)
|
| 359 |
+
assert_raises(Exception, f, 1.0, x, n=5)
|
| 360 |
+
assert_raises(Exception, f, 1.0, x, lower=2)
|
| 361 |
+
assert_raises(Exception, f, 1.0, x, a=np.zeros((2, 2), 'd', 'F'))
|
| 362 |
+
|
| 363 |
+
def test_syr2(self):
|
| 364 |
+
x = np.arange(1, 5, dtype='d')
|
| 365 |
+
y = np.arange(5, 9, dtype='d')
|
| 366 |
+
resxy = np.triu(x[:, np.newaxis] * y + y[:, np.newaxis] * x)
|
| 367 |
+
resxy_reverse = np.triu(x[::-1, np.newaxis] * y[::-1]
|
| 368 |
+
+ y[::-1, np.newaxis] * x[::-1])
|
| 369 |
+
|
| 370 |
+
q = np.linspace(0, 8.5, 17, endpoint=False)
|
| 371 |
+
|
| 372 |
+
for p, rtol in zip('sd', [1e-7, 1e-14]):
|
| 373 |
+
f = getattr(fblas, p+'syr2', None)
|
| 374 |
+
if f is None:
|
| 375 |
+
continue
|
| 376 |
+
assert_allclose(f(1.0, x, y), resxy, rtol=rtol)
|
| 377 |
+
assert_allclose(f(1.0, x, y, n=3), resxy[:3, :3], rtol=rtol)
|
| 378 |
+
assert_allclose(f(1.0, x, y, lower=True), resxy.T, rtol=rtol)
|
| 379 |
+
|
| 380 |
+
assert_allclose(f(1.0, q, q, incx=2, offx=2, incy=2, offy=10),
|
| 381 |
+
resxy, rtol=rtol)
|
| 382 |
+
assert_allclose(f(1.0, q, q, incx=2, offx=2, incy=2, offy=10, n=3),
|
| 383 |
+
resxy[:3, :3], rtol=rtol)
|
| 384 |
+
# negative increments imply reversed vectors in blas
|
| 385 |
+
assert_allclose(f(1.0, q, q, incx=-2, offx=2, incy=-2, offy=10),
|
| 386 |
+
resxy_reverse, rtol=rtol)
|
| 387 |
+
|
| 388 |
+
a = np.zeros((4, 4), 'f' if p == 's' else 'd', 'F')
|
| 389 |
+
b = f(1.0, x, y, a=a, overwrite_a=True)
|
| 390 |
+
assert_allclose(a, resxy, rtol=rtol)
|
| 391 |
+
|
| 392 |
+
b = f(2.0, x, y, a=a)
|
| 393 |
+
assert_(a is not b)
|
| 394 |
+
assert_allclose(b, 3*resxy, rtol=rtol)
|
| 395 |
+
|
| 396 |
+
assert_raises(Exception, f, 1.0, x, y, incx=0)
|
| 397 |
+
assert_raises(Exception, f, 1.0, x, y, offx=5)
|
| 398 |
+
assert_raises(Exception, f, 1.0, x, y, offx=-2)
|
| 399 |
+
assert_raises(Exception, f, 1.0, x, y, incy=0)
|
| 400 |
+
assert_raises(Exception, f, 1.0, x, y, offy=5)
|
| 401 |
+
assert_raises(Exception, f, 1.0, x, y, offy=-2)
|
| 402 |
+
assert_raises(Exception, f, 1.0, x, y, n=-2)
|
| 403 |
+
assert_raises(Exception, f, 1.0, x, y, n=5)
|
| 404 |
+
assert_raises(Exception, f, 1.0, x, y, lower=2)
|
| 405 |
+
assert_raises(Exception, f, 1.0, x, y,
|
| 406 |
+
a=np.zeros((2, 2), 'd', 'F'))
|
| 407 |
+
|
| 408 |
+
def test_her2(self):
|
| 409 |
+
x = np.arange(1, 9, dtype='d').view('D')
|
| 410 |
+
y = np.arange(9, 17, dtype='d').view('D')
|
| 411 |
+
resxy = x[:, np.newaxis] * y.conj() + y[:, np.newaxis] * x.conj()
|
| 412 |
+
resxy = np.triu(resxy)
|
| 413 |
+
|
| 414 |
+
resxy_reverse = x[::-1, np.newaxis] * y[::-1].conj()
|
| 415 |
+
resxy_reverse += y[::-1, np.newaxis] * x[::-1].conj()
|
| 416 |
+
resxy_reverse = np.triu(resxy_reverse)
|
| 417 |
+
|
| 418 |
+
u = np.c_[np.zeros(4), x, np.zeros(4)].ravel()
|
| 419 |
+
v = np.c_[np.zeros(4), y, np.zeros(4)].ravel()
|
| 420 |
+
|
| 421 |
+
for p, rtol in zip('cz', [1e-7, 1e-14]):
|
| 422 |
+
f = getattr(fblas, p+'her2', None)
|
| 423 |
+
if f is None:
|
| 424 |
+
continue
|
| 425 |
+
assert_allclose(f(1.0, x, y), resxy, rtol=rtol)
|
| 426 |
+
assert_allclose(f(1.0, x, y, n=3), resxy[:3, :3], rtol=rtol)
|
| 427 |
+
assert_allclose(f(1.0, x, y, lower=True), resxy.T.conj(),
|
| 428 |
+
rtol=rtol)
|
| 429 |
+
|
| 430 |
+
assert_allclose(f(1.0, u, v, incx=3, offx=1, incy=3, offy=1),
|
| 431 |
+
resxy, rtol=rtol)
|
| 432 |
+
assert_allclose(f(1.0, u, v, incx=3, offx=1, incy=3, offy=1, n=3),
|
| 433 |
+
resxy[:3, :3], rtol=rtol)
|
| 434 |
+
# negative increments imply reversed vectors in blas
|
| 435 |
+
assert_allclose(f(1.0, u, v, incx=-3, offx=1, incy=-3, offy=1),
|
| 436 |
+
resxy_reverse, rtol=rtol)
|
| 437 |
+
|
| 438 |
+
a = np.zeros((4, 4), 'F' if p == 'c' else 'D', 'F')
|
| 439 |
+
b = f(1.0, x, y, a=a, overwrite_a=True)
|
| 440 |
+
assert_allclose(a, resxy, rtol=rtol)
|
| 441 |
+
|
| 442 |
+
b = f(2.0, x, y, a=a)
|
| 443 |
+
assert_(a is not b)
|
| 444 |
+
assert_allclose(b, 3*resxy, rtol=rtol)
|
| 445 |
+
|
| 446 |
+
assert_raises(Exception, f, 1.0, x, y, incx=0)
|
| 447 |
+
assert_raises(Exception, f, 1.0, x, y, offx=5)
|
| 448 |
+
assert_raises(Exception, f, 1.0, x, y, offx=-2)
|
| 449 |
+
assert_raises(Exception, f, 1.0, x, y, incy=0)
|
| 450 |
+
assert_raises(Exception, f, 1.0, x, y, offy=5)
|
| 451 |
+
assert_raises(Exception, f, 1.0, x, y, offy=-2)
|
| 452 |
+
assert_raises(Exception, f, 1.0, x, y, n=-2)
|
| 453 |
+
assert_raises(Exception, f, 1.0, x, y, n=5)
|
| 454 |
+
assert_raises(Exception, f, 1.0, x, y, lower=2)
|
| 455 |
+
assert_raises(Exception, f, 1.0, x, y,
|
| 456 |
+
a=np.zeros((2, 2), 'd', 'F'))
|
| 457 |
+
|
| 458 |
+
def test_gbmv(self):
|
| 459 |
+
rng = np.random.default_rng(1234)
|
| 460 |
+
for ind, dtype in enumerate(DTYPES):
|
| 461 |
+
n = 7
|
| 462 |
+
m = 5
|
| 463 |
+
kl = 1
|
| 464 |
+
ku = 2
|
| 465 |
+
# fake a banded matrix via toeplitz
|
| 466 |
+
A = toeplitz(append(rng.random(kl+1), zeros(m-kl-1)),
|
| 467 |
+
append(rng.random(ku+1), zeros(n-ku-1)))
|
| 468 |
+
A = A.astype(dtype)
|
| 469 |
+
Ab = zeros((kl+ku+1, n), dtype=dtype)
|
| 470 |
+
|
| 471 |
+
# Form the banded storage
|
| 472 |
+
Ab[2, :5] = A[0, 0] # diag
|
| 473 |
+
Ab[1, 1:6] = A[0, 1] # sup1
|
| 474 |
+
Ab[0, 2:7] = A[0, 2] # sup2
|
| 475 |
+
Ab[3, :4] = A[1, 0] # sub1
|
| 476 |
+
|
| 477 |
+
x = rng.random(n).astype(dtype)
|
| 478 |
+
y = rng.random(m).astype(dtype)
|
| 479 |
+
alpha, beta = dtype(3), dtype(-5)
|
| 480 |
+
|
| 481 |
+
func, = get_blas_funcs(('gbmv',), dtype=dtype)
|
| 482 |
+
y1 = func(m=m, n=n, ku=ku, kl=kl, alpha=alpha, a=Ab,
|
| 483 |
+
x=x, y=y, beta=beta)
|
| 484 |
+
y2 = alpha * A.dot(x) + beta * y
|
| 485 |
+
assert_array_almost_equal(y1, y2)
|
| 486 |
+
|
| 487 |
+
y1 = func(m=m, n=n, ku=ku, kl=kl, alpha=alpha, a=Ab,
|
| 488 |
+
x=y, y=x, beta=beta, trans=1)
|
| 489 |
+
y2 = alpha * A.T.dot(y) + beta * x
|
| 490 |
+
assert_array_almost_equal(y1, y2)
|
| 491 |
+
|
| 492 |
+
def test_sbmv_hbmv(self):
|
| 493 |
+
rng = np.random.default_rng(1234)
|
| 494 |
+
for ind, dtype in enumerate(DTYPES):
|
| 495 |
+
n = 6
|
| 496 |
+
k = 2
|
| 497 |
+
A = zeros((n, n), dtype=dtype)
|
| 498 |
+
Ab = zeros((k+1, n), dtype=dtype)
|
| 499 |
+
|
| 500 |
+
# Form the array and its packed banded storage
|
| 501 |
+
A[arange(n), arange(n)] = rng.random(n)
|
| 502 |
+
for ind2 in range(1, k+1):
|
| 503 |
+
temp = rng.random(n-ind2)
|
| 504 |
+
A[arange(n-ind2), arange(ind2, n)] = temp
|
| 505 |
+
Ab[-1-ind2, ind2:] = temp
|
| 506 |
+
A = A.astype(dtype)
|
| 507 |
+
A = A + A.T if ind < 2 else A + A.conj().T
|
| 508 |
+
Ab[-1, :] = diag(A)
|
| 509 |
+
x = rng.random(n).astype(dtype)
|
| 510 |
+
y = rng.random(n).astype(dtype)
|
| 511 |
+
alpha, beta = dtype(1.25), dtype(3)
|
| 512 |
+
|
| 513 |
+
if ind > 1:
|
| 514 |
+
func, = get_blas_funcs(('hbmv',), dtype=dtype)
|
| 515 |
+
else:
|
| 516 |
+
func, = get_blas_funcs(('sbmv',), dtype=dtype)
|
| 517 |
+
y1 = func(k=k, alpha=alpha, a=Ab, x=x, y=y, beta=beta)
|
| 518 |
+
y2 = alpha * A.dot(x) + beta * y
|
| 519 |
+
assert_array_almost_equal(y1, y2)
|
| 520 |
+
|
| 521 |
+
def test_spmv_hpmv(self):
|
| 522 |
+
rng = np.random.default_rng(12345698)
|
| 523 |
+
for ind, dtype in enumerate(DTYPES+COMPLEX_DTYPES):
|
| 524 |
+
n = 3
|
| 525 |
+
A = rng.random((n, n)).astype(dtype)
|
| 526 |
+
if ind > 1:
|
| 527 |
+
A += rng.random((n, n))*1j
|
| 528 |
+
A = A.astype(dtype)
|
| 529 |
+
A = A + A.T if ind < 4 else A + A.conj().T
|
| 530 |
+
c, r = tril_indices(n)
|
| 531 |
+
Ap = A[r, c]
|
| 532 |
+
x = rng.random(n).astype(dtype)
|
| 533 |
+
y = rng.random(n).astype(dtype)
|
| 534 |
+
xlong = arange(2*n).astype(dtype)
|
| 535 |
+
ylong = ones(2*n).astype(dtype)
|
| 536 |
+
alpha, beta = dtype(1.25), dtype(2)
|
| 537 |
+
|
| 538 |
+
if ind > 3:
|
| 539 |
+
func, = get_blas_funcs(('hpmv',), dtype=dtype)
|
| 540 |
+
else:
|
| 541 |
+
func, = get_blas_funcs(('spmv',), dtype=dtype)
|
| 542 |
+
y1 = func(n=n, alpha=alpha, ap=Ap, x=x, y=y, beta=beta)
|
| 543 |
+
y2 = alpha * A.dot(x) + beta * y
|
| 544 |
+
assert_array_almost_equal(y1, y2)
|
| 545 |
+
|
| 546 |
+
# Test inc and offsets
|
| 547 |
+
y1 = func(n=n-1, alpha=alpha, beta=beta, x=xlong, y=ylong, ap=Ap,
|
| 548 |
+
incx=2, incy=2, offx=n, offy=n)
|
| 549 |
+
y2 = (alpha * A[:-1, :-1]).dot(xlong[3::2]) + beta * ylong[3::2]
|
| 550 |
+
assert_array_almost_equal(y1[3::2], y2)
|
| 551 |
+
assert_almost_equal(y1[4], ylong[4])
|
| 552 |
+
|
| 553 |
+
def test_spr_hpr(self):
|
| 554 |
+
rng = np.random.default_rng(1234)
|
| 555 |
+
for ind, dtype in enumerate(DTYPES+COMPLEX_DTYPES):
|
| 556 |
+
n = 3
|
| 557 |
+
A = rng.random((n, n)).astype(dtype)
|
| 558 |
+
if ind > 1:
|
| 559 |
+
A += rng.random((n, n))*1j
|
| 560 |
+
A = A.astype(dtype)
|
| 561 |
+
A = A + A.T if ind < 4 else A + A.conj().T
|
| 562 |
+
c, r = tril_indices(n)
|
| 563 |
+
Ap = A[r, c]
|
| 564 |
+
x = rng.random(n).astype(dtype)
|
| 565 |
+
alpha = (DTYPES+COMPLEX_DTYPES)[mod(ind, 4)](2.5)
|
| 566 |
+
|
| 567 |
+
if ind > 3:
|
| 568 |
+
func, = get_blas_funcs(('hpr',), dtype=dtype)
|
| 569 |
+
y2 = alpha * x[:, None].dot(x[None, :].conj()) + A
|
| 570 |
+
else:
|
| 571 |
+
func, = get_blas_funcs(('spr',), dtype=dtype)
|
| 572 |
+
y2 = alpha * x[:, None].dot(x[None, :]) + A
|
| 573 |
+
|
| 574 |
+
y1 = func(n=n, alpha=alpha, ap=Ap, x=x)
|
| 575 |
+
y1f = zeros((3, 3), dtype=dtype)
|
| 576 |
+
y1f[r, c] = y1
|
| 577 |
+
y1f[c, r] = y1.conj() if ind > 3 else y1
|
| 578 |
+
assert_array_almost_equal(y1f, y2)
|
| 579 |
+
|
| 580 |
+
def test_spr2_hpr2(self):
|
| 581 |
+
rng = np.random.default_rng(1234)
|
| 582 |
+
for ind, dtype in enumerate(DTYPES):
|
| 583 |
+
n = 3
|
| 584 |
+
A = rng.random((n, n)).astype(dtype)
|
| 585 |
+
if ind > 1:
|
| 586 |
+
A += rng.random((n, n))*1j
|
| 587 |
+
A = A.astype(dtype)
|
| 588 |
+
A = A + A.T if ind < 2 else A + A.conj().T
|
| 589 |
+
c, r = tril_indices(n)
|
| 590 |
+
Ap = A[r, c]
|
| 591 |
+
x = rng.random(n).astype(dtype)
|
| 592 |
+
y = rng.random(n).astype(dtype)
|
| 593 |
+
alpha = dtype(2)
|
| 594 |
+
|
| 595 |
+
if ind > 1:
|
| 596 |
+
func, = get_blas_funcs(('hpr2',), dtype=dtype)
|
| 597 |
+
else:
|
| 598 |
+
func, = get_blas_funcs(('spr2',), dtype=dtype)
|
| 599 |
+
|
| 600 |
+
u = alpha.conj() * x[:, None].dot(y[None, :].conj())
|
| 601 |
+
y2 = A + u + u.conj().T
|
| 602 |
+
y1 = func(n=n, alpha=alpha, x=x, y=y, ap=Ap)
|
| 603 |
+
y1f = zeros((3, 3), dtype=dtype)
|
| 604 |
+
y1f[r, c] = y1
|
| 605 |
+
y1f[[1, 2, 2], [0, 0, 1]] = y1[[1, 3, 4]].conj()
|
| 606 |
+
assert_array_almost_equal(y1f, y2)
|
| 607 |
+
|
| 608 |
+
def test_tbmv(self):
|
| 609 |
+
rng = np.random.default_rng(1234)
|
| 610 |
+
for ind, dtype in enumerate(DTYPES):
|
| 611 |
+
n = 10
|
| 612 |
+
k = 3
|
| 613 |
+
x = rng.random(n).astype(dtype)
|
| 614 |
+
A = zeros((n, n), dtype=dtype)
|
| 615 |
+
# Banded upper triangular array
|
| 616 |
+
for sup in range(k+1):
|
| 617 |
+
A[arange(n-sup), arange(sup, n)] = rng.random(n-sup)
|
| 618 |
+
|
| 619 |
+
# Add complex parts for c,z
|
| 620 |
+
if ind > 1:
|
| 621 |
+
A[nonzero(A)] += 1j * rng.random((k+1)*n-(k*(k+1)//2)).astype(dtype)
|
| 622 |
+
|
| 623 |
+
# Form the banded storage
|
| 624 |
+
Ab = zeros((k+1, n), dtype=dtype)
|
| 625 |
+
for row in range(k+1):
|
| 626 |
+
Ab[-row-1, row:] = diag(A, k=row)
|
| 627 |
+
func, = get_blas_funcs(('tbmv',), dtype=dtype)
|
| 628 |
+
|
| 629 |
+
y1 = func(k=k, a=Ab, x=x)
|
| 630 |
+
y2 = A.dot(x)
|
| 631 |
+
assert_array_almost_equal(y1, y2)
|
| 632 |
+
|
| 633 |
+
y1 = func(k=k, a=Ab, x=x, diag=1)
|
| 634 |
+
A[arange(n), arange(n)] = dtype(1)
|
| 635 |
+
y2 = A.dot(x)
|
| 636 |
+
assert_array_almost_equal(y1, y2)
|
| 637 |
+
|
| 638 |
+
y1 = func(k=k, a=Ab, x=x, diag=1, trans=1)
|
| 639 |
+
y2 = A.T.dot(x)
|
| 640 |
+
assert_array_almost_equal(y1, y2)
|
| 641 |
+
|
| 642 |
+
y1 = func(k=k, a=Ab, x=x, diag=1, trans=2)
|
| 643 |
+
y2 = A.conj().T.dot(x)
|
| 644 |
+
assert_array_almost_equal(y1, y2)
|
| 645 |
+
|
| 646 |
+
def test_tbsv(self):
|
| 647 |
+
rng = np.random.default_rng(1234)
|
| 648 |
+
for ind, dtype in enumerate(DTYPES):
|
| 649 |
+
n = 6
|
| 650 |
+
k = 3
|
| 651 |
+
x = rng.random(n).astype(dtype)
|
| 652 |
+
A = zeros((n, n), dtype=dtype)
|
| 653 |
+
# Banded upper triangular array
|
| 654 |
+
for sup in range(k+1):
|
| 655 |
+
A[arange(n-sup), arange(sup, n)] = rng.random(n-sup)
|
| 656 |
+
|
| 657 |
+
# Add complex parts for c,z
|
| 658 |
+
if ind > 1:
|
| 659 |
+
A[nonzero(A)] += 1j * rng.random((k+1)*n-(k*(k+1)//2)).astype(dtype)
|
| 660 |
+
|
| 661 |
+
# Form the banded storage
|
| 662 |
+
Ab = zeros((k+1, n), dtype=dtype)
|
| 663 |
+
for row in range(k+1):
|
| 664 |
+
Ab[-row-1, row:] = diag(A, k=row)
|
| 665 |
+
func, = get_blas_funcs(('tbsv',), dtype=dtype)
|
| 666 |
+
|
| 667 |
+
y1 = func(k=k, a=Ab, x=x)
|
| 668 |
+
y2 = solve(A, x)
|
| 669 |
+
assert_array_almost_equal(y1, y2)
|
| 670 |
+
|
| 671 |
+
y1 = func(k=k, a=Ab, x=x, diag=1)
|
| 672 |
+
A[arange(n), arange(n)] = dtype(1)
|
| 673 |
+
y2 = solve(A, x)
|
| 674 |
+
assert_array_almost_equal(y1, y2)
|
| 675 |
+
|
| 676 |
+
y1 = func(k=k, a=Ab, x=x, diag=1, trans=1)
|
| 677 |
+
y2 = solve(A.T, x)
|
| 678 |
+
assert_array_almost_equal(y1, y2)
|
| 679 |
+
|
| 680 |
+
y1 = func(k=k, a=Ab, x=x, diag=1, trans=2)
|
| 681 |
+
y2 = solve(A.conj().T, x)
|
| 682 |
+
assert_array_almost_equal(y1, y2)
|
| 683 |
+
|
| 684 |
+
def test_tpmv(self):
|
| 685 |
+
rng = np.random.default_rng(1234)
|
| 686 |
+
for ind, dtype in enumerate(DTYPES):
|
| 687 |
+
n = 10
|
| 688 |
+
x = rng.random(n).astype(dtype)
|
| 689 |
+
# Upper triangular array
|
| 690 |
+
if ind < 2:
|
| 691 |
+
A = triu(rng.random((n, n)))
|
| 692 |
+
else:
|
| 693 |
+
A = triu(rng.random((n, n)) + rng.random((n, n))*1j)
|
| 694 |
+
|
| 695 |
+
# Form the packed storage
|
| 696 |
+
c, r = tril_indices(n)
|
| 697 |
+
Ap = A[r, c]
|
| 698 |
+
func, = get_blas_funcs(('tpmv',), dtype=dtype)
|
| 699 |
+
|
| 700 |
+
y1 = func(n=n, ap=Ap, x=x)
|
| 701 |
+
y2 = A.dot(x)
|
| 702 |
+
assert_array_almost_equal(y1, y2)
|
| 703 |
+
|
| 704 |
+
y1 = func(n=n, ap=Ap, x=x, diag=1)
|
| 705 |
+
A[arange(n), arange(n)] = dtype(1)
|
| 706 |
+
y2 = A.dot(x)
|
| 707 |
+
assert_array_almost_equal(y1, y2)
|
| 708 |
+
|
| 709 |
+
y1 = func(n=n, ap=Ap, x=x, diag=1, trans=1)
|
| 710 |
+
y2 = A.T.dot(x)
|
| 711 |
+
assert_array_almost_equal(y1, y2)
|
| 712 |
+
|
| 713 |
+
y1 = func(n=n, ap=Ap, x=x, diag=1, trans=2)
|
| 714 |
+
y2 = A.conj().T.dot(x)
|
| 715 |
+
assert_array_almost_equal(y1, y2)
|
| 716 |
+
|
| 717 |
+
def test_tpsv(self):
|
| 718 |
+
rng = np.random.default_rng(1234)
|
| 719 |
+
for ind, dtype in enumerate(DTYPES):
|
| 720 |
+
n = 10
|
| 721 |
+
x = rng.random(n).astype(dtype)
|
| 722 |
+
# Upper triangular array
|
| 723 |
+
if ind < 2:
|
| 724 |
+
A = triu(rng.random((n, n)))
|
| 725 |
+
else:
|
| 726 |
+
A = triu(rng.random((n, n)) + rng.random((n, n))*1j)
|
| 727 |
+
A += eye(n)
|
| 728 |
+
# Form the packed storage
|
| 729 |
+
c, r = tril_indices(n)
|
| 730 |
+
Ap = A[r, c]
|
| 731 |
+
func, = get_blas_funcs(('tpsv',), dtype=dtype)
|
| 732 |
+
|
| 733 |
+
y1 = func(n=n, ap=Ap, x=x)
|
| 734 |
+
y2 = solve(A, x)
|
| 735 |
+
assert_array_almost_equal(y1, y2)
|
| 736 |
+
|
| 737 |
+
y1 = func(n=n, ap=Ap, x=x, diag=1)
|
| 738 |
+
A[arange(n), arange(n)] = dtype(1)
|
| 739 |
+
y2 = solve(A, x)
|
| 740 |
+
assert_array_almost_equal(y1, y2)
|
| 741 |
+
|
| 742 |
+
y1 = func(n=n, ap=Ap, x=x, diag=1, trans=1)
|
| 743 |
+
y2 = solve(A.T, x)
|
| 744 |
+
assert_array_almost_equal(y1, y2)
|
| 745 |
+
|
| 746 |
+
y1 = func(n=n, ap=Ap, x=x, diag=1, trans=2)
|
| 747 |
+
y2 = solve(A.conj().T, x)
|
| 748 |
+
assert_array_almost_equal(y1, y2)
|
| 749 |
+
|
| 750 |
+
def test_trmv(self):
|
| 751 |
+
rng = np.random.default_rng(1234)
|
| 752 |
+
for ind, dtype in enumerate(DTYPES):
|
| 753 |
+
n = 3
|
| 754 |
+
A = (rng.random((n, n))+eye(n)).astype(dtype)
|
| 755 |
+
x = rng.random(3).astype(dtype)
|
| 756 |
+
func, = get_blas_funcs(('trmv',), dtype=dtype)
|
| 757 |
+
|
| 758 |
+
y1 = func(a=A, x=x)
|
| 759 |
+
y2 = triu(A).dot(x)
|
| 760 |
+
assert_array_almost_equal(y1, y2)
|
| 761 |
+
|
| 762 |
+
y1 = func(a=A, x=x, diag=1)
|
| 763 |
+
A[arange(n), arange(n)] = dtype(1)
|
| 764 |
+
y2 = triu(A).dot(x)
|
| 765 |
+
assert_array_almost_equal(y1, y2)
|
| 766 |
+
|
| 767 |
+
y1 = func(a=A, x=x, diag=1, trans=1)
|
| 768 |
+
y2 = triu(A).T.dot(x)
|
| 769 |
+
assert_array_almost_equal(y1, y2)
|
| 770 |
+
|
| 771 |
+
y1 = func(a=A, x=x, diag=1, trans=2)
|
| 772 |
+
y2 = triu(A).conj().T.dot(x)
|
| 773 |
+
assert_array_almost_equal(y1, y2)
|
| 774 |
+
|
| 775 |
+
def test_trsv(self):
|
| 776 |
+
rng = np.random.default_rng(1234)
|
| 777 |
+
for ind, dtype in enumerate(DTYPES):
|
| 778 |
+
n = 15
|
| 779 |
+
A = (rng.random((n, n))+eye(n)).astype(dtype)
|
| 780 |
+
x = rng.random(n).astype(dtype)
|
| 781 |
+
func, = get_blas_funcs(('trsv',), dtype=dtype)
|
| 782 |
+
|
| 783 |
+
y1 = func(a=A, x=x)
|
| 784 |
+
y2 = solve(triu(A), x)
|
| 785 |
+
assert_array_almost_equal(y1, y2)
|
| 786 |
+
|
| 787 |
+
y1 = func(a=A, x=x, lower=1)
|
| 788 |
+
y2 = solve(tril(A), x)
|
| 789 |
+
assert_array_almost_equal(y1, y2)
|
| 790 |
+
|
| 791 |
+
y1 = func(a=A, x=x, diag=1)
|
| 792 |
+
A[arange(n), arange(n)] = dtype(1)
|
| 793 |
+
y2 = solve(triu(A), x)
|
| 794 |
+
assert_array_almost_equal(y1, y2)
|
| 795 |
+
|
| 796 |
+
y1 = func(a=A, x=x, diag=1, trans=1)
|
| 797 |
+
y2 = solve(triu(A).T, x)
|
| 798 |
+
assert_array_almost_equal(y1, y2)
|
| 799 |
+
|
| 800 |
+
y1 = func(a=A, x=x, diag=1, trans=2)
|
| 801 |
+
y2 = solve(triu(A).conj().T, x)
|
| 802 |
+
assert_array_almost_equal(y1, y2)
|
| 803 |
+
|
| 804 |
+
|
| 805 |
+
class TestFBLAS3Simple:
|
| 806 |
+
|
| 807 |
+
def test_gemm(self):
|
| 808 |
+
for p in 'sd':
|
| 809 |
+
f = getattr(fblas, p+'gemm', None)
|
| 810 |
+
if f is None:
|
| 811 |
+
continue
|
| 812 |
+
assert_array_almost_equal(f(3, [3], [-4]), [[-36]])
|
| 813 |
+
assert_array_almost_equal(f(3, [3], [-4], 3, [5]), [-21])
|
| 814 |
+
for p in 'cz':
|
| 815 |
+
f = getattr(fblas, p+'gemm', None)
|
| 816 |
+
if f is None:
|
| 817 |
+
continue
|
| 818 |
+
assert_array_almost_equal(f(3j, [3-4j], [-4]), [[-48-36j]])
|
| 819 |
+
assert_array_almost_equal(f(3j, [3-4j], [-4], 3, [5j]), [-48-21j])
|
| 820 |
+
|
| 821 |
+
|
| 822 |
+
def _get_func(func, ps='sdzc'):
|
| 823 |
+
"""Just a helper: return a specified BLAS function w/typecode."""
|
| 824 |
+
for p in ps:
|
| 825 |
+
f = getattr(fblas, p+func, None)
|
| 826 |
+
if f is None:
|
| 827 |
+
continue
|
| 828 |
+
yield f
|
| 829 |
+
|
| 830 |
+
|
| 831 |
+
class TestBLAS3Symm:
|
| 832 |
+
|
| 833 |
+
def setup_method(self):
|
| 834 |
+
self.a = np.array([[1., 2.],
|
| 835 |
+
[0., 1.]])
|
| 836 |
+
self.b = np.array([[1., 0., 3.],
|
| 837 |
+
[0., -1., 2.]])
|
| 838 |
+
self.c = np.ones((2, 3))
|
| 839 |
+
self.t = np.array([[2., -1., 8.],
|
| 840 |
+
[3., 0., 9.]])
|
| 841 |
+
|
| 842 |
+
def test_symm(self):
|
| 843 |
+
for f in _get_func('symm'):
|
| 844 |
+
res = f(a=self.a, b=self.b, c=self.c, alpha=1., beta=1.)
|
| 845 |
+
assert_array_almost_equal(res, self.t)
|
| 846 |
+
|
| 847 |
+
res = f(a=self.a.T, b=self.b, lower=1, c=self.c, alpha=1., beta=1.)
|
| 848 |
+
assert_array_almost_equal(res, self.t)
|
| 849 |
+
|
| 850 |
+
res = f(a=self.a, b=self.b.T, side=1, c=self.c.T,
|
| 851 |
+
alpha=1., beta=1.)
|
| 852 |
+
assert_array_almost_equal(res, self.t.T)
|
| 853 |
+
|
| 854 |
+
def test_summ_wrong_side(self):
|
| 855 |
+
f = getattr(fblas, 'dsymm', None)
|
| 856 |
+
if f is not None:
|
| 857 |
+
assert_raises(Exception, f, **{'a': self.a, 'b': self.b,
|
| 858 |
+
'alpha': 1, 'side': 1})
|
| 859 |
+
# `side=1` means C <- B*A, hence shapes of A and B are to be
|
| 860 |
+
# compatible. Otherwise, f2py exception is raised
|
| 861 |
+
|
| 862 |
+
def test_symm_wrong_uplo(self):
|
| 863 |
+
"""SYMM only considers the upper/lower part of A. Hence setting
|
| 864 |
+
wrong value for `lower` (default is lower=0, meaning upper triangle)
|
| 865 |
+
gives a wrong result.
|
| 866 |
+
"""
|
| 867 |
+
f = getattr(fblas, 'dsymm', None)
|
| 868 |
+
if f is not None:
|
| 869 |
+
res = f(a=self.a, b=self.b, c=self.c, alpha=1., beta=1.)
|
| 870 |
+
assert np.allclose(res, self.t)
|
| 871 |
+
|
| 872 |
+
res = f(a=self.a, b=self.b, lower=1, c=self.c, alpha=1., beta=1.)
|
| 873 |
+
assert not np.allclose(res, self.t)
|
| 874 |
+
|
| 875 |
+
|
| 876 |
+
class TestBLAS3Syrk:
|
| 877 |
+
def setup_method(self):
|
| 878 |
+
self.a = np.array([[1., 0.],
|
| 879 |
+
[0., -2.],
|
| 880 |
+
[2., 3.]])
|
| 881 |
+
self.t = np.array([[1., 0., 2.],
|
| 882 |
+
[0., 4., -6.],
|
| 883 |
+
[2., -6., 13.]])
|
| 884 |
+
self.tt = np.array([[5., 6.],
|
| 885 |
+
[6., 13.]])
|
| 886 |
+
|
| 887 |
+
def test_syrk(self):
|
| 888 |
+
for f in _get_func('syrk'):
|
| 889 |
+
c = f(a=self.a, alpha=1.)
|
| 890 |
+
assert_array_almost_equal(np.triu(c), np.triu(self.t))
|
| 891 |
+
|
| 892 |
+
c = f(a=self.a, alpha=1., lower=1)
|
| 893 |
+
assert_array_almost_equal(np.tril(c), np.tril(self.t))
|
| 894 |
+
|
| 895 |
+
c0 = np.ones(self.t.shape)
|
| 896 |
+
c = f(a=self.a, alpha=1., beta=1., c=c0)
|
| 897 |
+
assert_array_almost_equal(np.triu(c), np.triu(self.t+c0))
|
| 898 |
+
|
| 899 |
+
c = f(a=self.a, alpha=1., trans=1)
|
| 900 |
+
assert_array_almost_equal(np.triu(c), np.triu(self.tt))
|
| 901 |
+
|
| 902 |
+
# prints '0-th dimension must be fixed to 3 but got 5',
|
| 903 |
+
# FIXME: suppress?
|
| 904 |
+
# FIXME: how to catch the _fblas.error?
|
| 905 |
+
def test_syrk_wrong_c(self):
|
| 906 |
+
f = getattr(fblas, 'dsyrk', None)
|
| 907 |
+
if f is not None:
|
| 908 |
+
assert_raises(Exception, f, **{'a': self.a, 'alpha': 1.,
|
| 909 |
+
'c': np.ones((5, 8))})
|
| 910 |
+
# if C is supplied, it must have compatible dimensions
|
| 911 |
+
|
| 912 |
+
|
| 913 |
+
class TestBLAS3Syr2k:
|
| 914 |
+
def setup_method(self):
|
| 915 |
+
self.a = np.array([[1., 0.],
|
| 916 |
+
[0., -2.],
|
| 917 |
+
[2., 3.]])
|
| 918 |
+
self.b = np.array([[0., 1.],
|
| 919 |
+
[1., 0.],
|
| 920 |
+
[0, 1.]])
|
| 921 |
+
self.t = np.array([[0., -1., 3.],
|
| 922 |
+
[-1., 0., 0.],
|
| 923 |
+
[3., 0., 6.]])
|
| 924 |
+
self.tt = np.array([[0., 1.],
|
| 925 |
+
[1., 6]])
|
| 926 |
+
|
| 927 |
+
def test_syr2k(self):
|
| 928 |
+
for f in _get_func('syr2k'):
|
| 929 |
+
c = f(a=self.a, b=self.b, alpha=1.)
|
| 930 |
+
assert_array_almost_equal(np.triu(c), np.triu(self.t))
|
| 931 |
+
|
| 932 |
+
c = f(a=self.a, b=self.b, alpha=1., lower=1)
|
| 933 |
+
assert_array_almost_equal(np.tril(c), np.tril(self.t))
|
| 934 |
+
|
| 935 |
+
c0 = np.ones(self.t.shape)
|
| 936 |
+
c = f(a=self.a, b=self.b, alpha=1., beta=1., c=c0)
|
| 937 |
+
assert_array_almost_equal(np.triu(c), np.triu(self.t+c0))
|
| 938 |
+
|
| 939 |
+
c = f(a=self.a, b=self.b, alpha=1., trans=1)
|
| 940 |
+
assert_array_almost_equal(np.triu(c), np.triu(self.tt))
|
| 941 |
+
|
| 942 |
+
# prints '0-th dimension must be fixed to 3 but got 5', FIXME: suppress?
|
| 943 |
+
def test_syr2k_wrong_c(self):
|
| 944 |
+
f = getattr(fblas, 'dsyr2k', None)
|
| 945 |
+
if f is not None:
|
| 946 |
+
assert_raises(Exception, f, **{'a': self.a,
|
| 947 |
+
'b': self.b,
|
| 948 |
+
'alpha': 1.,
|
| 949 |
+
'c': np.zeros((15, 8))})
|
| 950 |
+
# if C is supplied, it must have compatible dimensions
|
| 951 |
+
|
| 952 |
+
|
| 953 |
+
class TestSyHe:
|
| 954 |
+
"""Quick and simple tests for (zc)-symm, syrk, syr2k."""
|
| 955 |
+
|
| 956 |
+
def setup_method(self):
|
| 957 |
+
self.sigma_y = np.array([[0., -1.j],
|
| 958 |
+
[1.j, 0.]])
|
| 959 |
+
|
| 960 |
+
def test_symm_zc(self):
|
| 961 |
+
for f in _get_func('symm', 'zc'):
|
| 962 |
+
# NB: a is symmetric w/upper diag of ONLY
|
| 963 |
+
res = f(a=self.sigma_y, b=self.sigma_y, alpha=1.)
|
| 964 |
+
assert_array_almost_equal(np.triu(res), np.diag([1, -1]))
|
| 965 |
+
|
| 966 |
+
def test_hemm_zc(self):
|
| 967 |
+
for f in _get_func('hemm', 'zc'):
|
| 968 |
+
# NB: a is hermitian w/upper diag of ONLY
|
| 969 |
+
res = f(a=self.sigma_y, b=self.sigma_y, alpha=1.)
|
| 970 |
+
assert_array_almost_equal(np.triu(res), np.diag([1, 1]))
|
| 971 |
+
|
| 972 |
+
def test_syrk_zr(self):
|
| 973 |
+
for f in _get_func('syrk', 'zc'):
|
| 974 |
+
res = f(a=self.sigma_y, alpha=1.)
|
| 975 |
+
assert_array_almost_equal(np.triu(res), np.diag([-1, -1]))
|
| 976 |
+
|
| 977 |
+
def test_herk_zr(self):
|
| 978 |
+
for f in _get_func('herk', 'zc'):
|
| 979 |
+
res = f(a=self.sigma_y, alpha=1.)
|
| 980 |
+
assert_array_almost_equal(np.triu(res), np.diag([1, 1]))
|
| 981 |
+
|
| 982 |
+
def test_syr2k_zr(self):
|
| 983 |
+
for f in _get_func('syr2k', 'zc'):
|
| 984 |
+
res = f(a=self.sigma_y, b=self.sigma_y, alpha=1.)
|
| 985 |
+
assert_array_almost_equal(np.triu(res), 2.*np.diag([-1, -1]))
|
| 986 |
+
|
| 987 |
+
def test_her2k_zr(self):
|
| 988 |
+
for f in _get_func('her2k', 'zc'):
|
| 989 |
+
res = f(a=self.sigma_y, b=self.sigma_y, alpha=1.)
|
| 990 |
+
assert_array_almost_equal(np.triu(res), 2.*np.diag([1, 1]))
|
| 991 |
+
|
| 992 |
+
|
| 993 |
+
class TestTRMM:
|
| 994 |
+
"""Quick and simple tests for dtrmm."""
|
| 995 |
+
|
| 996 |
+
def setup_method(self):
|
| 997 |
+
self.a = np.array([[1., 2., ],
|
| 998 |
+
[-2., 1.]])
|
| 999 |
+
self.b = np.array([[3., 4., -1.],
|
| 1000 |
+
[5., 6., -2.]])
|
| 1001 |
+
|
| 1002 |
+
self.a2 = np.array([[1, 1, 2, 3],
|
| 1003 |
+
[0, 1, 4, 5],
|
| 1004 |
+
[0, 0, 1, 6],
|
| 1005 |
+
[0, 0, 0, 1]], order="f")
|
| 1006 |
+
self.b2 = np.array([[1, 4], [2, 5], [3, 6], [7, 8], [9, 10]],
|
| 1007 |
+
order="f")
|
| 1008 |
+
|
| 1009 |
+
@pytest.mark.parametrize("dtype_", DTYPES)
|
| 1010 |
+
def test_side(self, dtype_):
|
| 1011 |
+
trmm = get_blas_funcs("trmm", dtype=dtype_)
|
| 1012 |
+
# Provide large A array that works for side=1 but not 0 (see gh-10841)
|
| 1013 |
+
assert_raises(Exception, trmm, 1.0, self.a2, self.b2)
|
| 1014 |
+
res = trmm(1.0, self.a2.astype(dtype_), self.b2.astype(dtype_),
|
| 1015 |
+
side=1)
|
| 1016 |
+
k = self.b2.shape[1]
|
| 1017 |
+
assert_allclose(res, self.b2 @ self.a2[:k, :k], rtol=0.,
|
| 1018 |
+
atol=100*np.finfo(dtype_).eps)
|
| 1019 |
+
|
| 1020 |
+
def test_ab(self):
|
| 1021 |
+
f = getattr(fblas, 'dtrmm', None)
|
| 1022 |
+
if f is not None:
|
| 1023 |
+
result = f(1., self.a, self.b)
|
| 1024 |
+
# default a is upper triangular
|
| 1025 |
+
expected = np.array([[13., 16., -5.],
|
| 1026 |
+
[5., 6., -2.]])
|
| 1027 |
+
assert_array_almost_equal(result, expected)
|
| 1028 |
+
|
| 1029 |
+
def test_ab_lower(self):
|
| 1030 |
+
f = getattr(fblas, 'dtrmm', None)
|
| 1031 |
+
if f is not None:
|
| 1032 |
+
result = f(1., self.a, self.b, lower=True)
|
| 1033 |
+
expected = np.array([[3., 4., -1.],
|
| 1034 |
+
[-1., -2., 0.]]) # now a is lower triangular
|
| 1035 |
+
assert_array_almost_equal(result, expected)
|
| 1036 |
+
|
| 1037 |
+
def test_b_overwrites(self):
|
| 1038 |
+
# BLAS dtrmm modifies B argument in-place.
|
| 1039 |
+
# Here the default is to copy, but this can be overridden
|
| 1040 |
+
f = getattr(fblas, 'dtrmm', None)
|
| 1041 |
+
if f is not None:
|
| 1042 |
+
for overwr in [True, False]:
|
| 1043 |
+
bcopy = self.b.copy()
|
| 1044 |
+
result = f(1., self.a, bcopy, overwrite_b=overwr)
|
| 1045 |
+
# C-contiguous arrays are copied
|
| 1046 |
+
assert_(bcopy.flags.f_contiguous is False and
|
| 1047 |
+
np.may_share_memory(bcopy, result) is False)
|
| 1048 |
+
assert_equal(bcopy, self.b)
|
| 1049 |
+
|
| 1050 |
+
bcopy = np.asfortranarray(self.b.copy()) # or just transpose it
|
| 1051 |
+
result = f(1., self.a, bcopy, overwrite_b=True)
|
| 1052 |
+
assert_(bcopy.flags.f_contiguous is True and
|
| 1053 |
+
np.may_share_memory(bcopy, result) is True)
|
| 1054 |
+
assert_array_almost_equal(bcopy, result)
|
| 1055 |
+
|
| 1056 |
+
|
| 1057 |
+
def test_trsm():
|
| 1058 |
+
rng = np.random.default_rng(1234)
|
| 1059 |
+
for ind, dtype in enumerate(DTYPES):
|
| 1060 |
+
tol = np.finfo(dtype).eps*1000
|
| 1061 |
+
func, = get_blas_funcs(('trsm',), dtype=dtype)
|
| 1062 |
+
|
| 1063 |
+
# Test protection against size mismatches
|
| 1064 |
+
A = rng.random((4, 5)).astype(dtype)
|
| 1065 |
+
B = rng.random((4, 4)).astype(dtype)
|
| 1066 |
+
alpha = dtype(1)
|
| 1067 |
+
assert_raises(Exception, func, alpha, A, B)
|
| 1068 |
+
assert_raises(Exception, func, alpha, A.T, B)
|
| 1069 |
+
|
| 1070 |
+
n = 8
|
| 1071 |
+
m = 7
|
| 1072 |
+
alpha = dtype(-2.5)
|
| 1073 |
+
if ind < 2:
|
| 1074 |
+
A = rng.random((m, m)) + eye(m)
|
| 1075 |
+
else:
|
| 1076 |
+
A = (rng.random((m, m)) + rng.random((m, m))*1j) + eye(m)
|
| 1077 |
+
A = A.astype(dtype)
|
| 1078 |
+
Au = triu(A)
|
| 1079 |
+
Al = tril(A)
|
| 1080 |
+
B1 = rng.random((m, n)).astype(dtype)
|
| 1081 |
+
B2 = rng.random((n, m)).astype(dtype)
|
| 1082 |
+
|
| 1083 |
+
x1 = func(alpha=alpha, a=A, b=B1)
|
| 1084 |
+
assert_equal(B1.shape, x1.shape)
|
| 1085 |
+
x2 = solve(Au, alpha*B1)
|
| 1086 |
+
assert_allclose(x1, x2, atol=tol)
|
| 1087 |
+
|
| 1088 |
+
x1 = func(alpha=alpha, a=A, b=B1, trans_a=1)
|
| 1089 |
+
x2 = solve(Au.T, alpha*B1)
|
| 1090 |
+
assert_allclose(x1, x2, atol=tol)
|
| 1091 |
+
|
| 1092 |
+
x1 = func(alpha=alpha, a=A, b=B1, trans_a=2)
|
| 1093 |
+
x2 = solve(Au.conj().T, alpha*B1)
|
| 1094 |
+
assert_allclose(x1, x2, atol=tol)
|
| 1095 |
+
|
| 1096 |
+
x1 = func(alpha=alpha, a=A, b=B1, diag=1)
|
| 1097 |
+
Au[arange(m), arange(m)] = dtype(1)
|
| 1098 |
+
x2 = solve(Au, alpha*B1)
|
| 1099 |
+
assert_allclose(x1, x2, atol=tol)
|
| 1100 |
+
|
| 1101 |
+
x1 = func(alpha=alpha, a=A, b=B2, diag=1, side=1)
|
| 1102 |
+
x2 = solve(Au.conj().T, alpha*B2.conj().T)
|
| 1103 |
+
assert_allclose(x1, x2.conj().T, atol=tol)
|
| 1104 |
+
|
| 1105 |
+
x1 = func(alpha=alpha, a=A, b=B2, diag=1, side=1, lower=1)
|
| 1106 |
+
Al[arange(m), arange(m)] = dtype(1)
|
| 1107 |
+
x2 = solve(Al.conj().T, alpha*B2.conj().T)
|
| 1108 |
+
assert_allclose(x1, x2.conj().T, atol=tol)
|
| 1109 |
+
|
| 1110 |
+
|
| 1111 |
+
@pytest.mark.xfail(run=False,
|
| 1112 |
+
reason="gh-16930")
|
| 1113 |
+
def test_gh_169309():
|
| 1114 |
+
x = np.repeat(10, 9)
|
| 1115 |
+
actual = scipy.linalg.blas.dnrm2(x, 5, 3, -1)
|
| 1116 |
+
expected = math.sqrt(500)
|
| 1117 |
+
assert_allclose(actual, expected)
|
| 1118 |
+
|
| 1119 |
+
|
| 1120 |
+
def test_dnrm2_neg_incx():
|
| 1121 |
+
# check that dnrm2(..., incx < 0) raises
|
| 1122 |
+
# XXX: remove the test after the lowest supported BLAS implements
|
| 1123 |
+
# negative incx (new in LAPACK 3.10)
|
| 1124 |
+
x = np.repeat(10, 9)
|
| 1125 |
+
incx = -1
|
| 1126 |
+
with assert_raises(fblas.__fblas_error):
|
| 1127 |
+
scipy.linalg.blas.dnrm2(x, 5, 3, incx)
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_cython_blas.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from numpy.testing import (assert_allclose,
|
| 3 |
+
assert_equal)
|
| 4 |
+
import scipy.linalg.cython_blas as blas
|
| 5 |
+
|
| 6 |
+
class TestDGEMM:
|
| 7 |
+
|
| 8 |
+
def test_transposes(self):
|
| 9 |
+
|
| 10 |
+
a = np.arange(12, dtype='d').reshape((3, 4))[:2,:2]
|
| 11 |
+
b = np.arange(1, 13, dtype='d').reshape((4, 3))[:2,:2]
|
| 12 |
+
c = np.empty((2, 4))[:2,:2]
|
| 13 |
+
|
| 14 |
+
blas._test_dgemm(1., a, b, 0., c)
|
| 15 |
+
assert_allclose(c, a.dot(b))
|
| 16 |
+
|
| 17 |
+
blas._test_dgemm(1., a.T, b, 0., c)
|
| 18 |
+
assert_allclose(c, a.T.dot(b))
|
| 19 |
+
|
| 20 |
+
blas._test_dgemm(1., a, b.T, 0., c)
|
| 21 |
+
assert_allclose(c, a.dot(b.T))
|
| 22 |
+
|
| 23 |
+
blas._test_dgemm(1., a.T, b.T, 0., c)
|
| 24 |
+
assert_allclose(c, a.T.dot(b.T))
|
| 25 |
+
|
| 26 |
+
blas._test_dgemm(1., a, b, 0., c.T)
|
| 27 |
+
assert_allclose(c, a.dot(b).T)
|
| 28 |
+
|
| 29 |
+
blas._test_dgemm(1., a.T, b, 0., c.T)
|
| 30 |
+
assert_allclose(c, a.T.dot(b).T)
|
| 31 |
+
|
| 32 |
+
blas._test_dgemm(1., a, b.T, 0., c.T)
|
| 33 |
+
assert_allclose(c, a.dot(b.T).T)
|
| 34 |
+
|
| 35 |
+
blas._test_dgemm(1., a.T, b.T, 0., c.T)
|
| 36 |
+
assert_allclose(c, a.T.dot(b.T).T)
|
| 37 |
+
|
| 38 |
+
def test_shapes(self):
|
| 39 |
+
a = np.arange(6, dtype='d').reshape((3, 2))
|
| 40 |
+
b = np.arange(-6, 2, dtype='d').reshape((2, 4))
|
| 41 |
+
c = np.empty((3, 4))
|
| 42 |
+
|
| 43 |
+
blas._test_dgemm(1., a, b, 0., c)
|
| 44 |
+
assert_allclose(c, a.dot(b))
|
| 45 |
+
|
| 46 |
+
blas._test_dgemm(1., b.T, a.T, 0., c.T)
|
| 47 |
+
assert_allclose(c, b.T.dot(a.T).T)
|
| 48 |
+
|
| 49 |
+
class TestWfuncPointers:
|
| 50 |
+
""" Test the function pointers that are expected to fail on
|
| 51 |
+
Mac OS X without the additional entry statement in their definitions
|
| 52 |
+
in fblas_l1.pyf.src. """
|
| 53 |
+
|
| 54 |
+
def test_complex_args(self):
|
| 55 |
+
|
| 56 |
+
cx = np.array([.5 + 1.j, .25 - .375j, 12.5 - 4.j], np.complex64)
|
| 57 |
+
cy = np.array([.8 + 2.j, .875 - .625j, -1. + 2.j], np.complex64)
|
| 58 |
+
|
| 59 |
+
assert_allclose(blas._test_cdotc(cx, cy),
|
| 60 |
+
-17.6468753815+21.3718757629j)
|
| 61 |
+
assert_allclose(blas._test_cdotu(cx, cy),
|
| 62 |
+
-6.11562538147+30.3156242371j)
|
| 63 |
+
|
| 64 |
+
assert_equal(blas._test_icamax(cx), 3)
|
| 65 |
+
|
| 66 |
+
assert_allclose(blas._test_scasum(cx), 18.625)
|
| 67 |
+
assert_allclose(blas._test_scnrm2(cx), 13.1796483994)
|
| 68 |
+
|
| 69 |
+
assert_allclose(blas._test_cdotc(cx[::2], cy[::2]),
|
| 70 |
+
-18.1000003815+21.2000007629j)
|
| 71 |
+
assert_allclose(blas._test_cdotu(cx[::2], cy[::2]),
|
| 72 |
+
-6.10000038147+30.7999992371j)
|
| 73 |
+
assert_allclose(blas._test_scasum(cx[::2]), 18.)
|
| 74 |
+
assert_allclose(blas._test_scnrm2(cx[::2]), 13.1719398499)
|
| 75 |
+
|
| 76 |
+
def test_double_args(self):
|
| 77 |
+
|
| 78 |
+
x = np.array([5., -3, -.5], np.float64)
|
| 79 |
+
y = np.array([2, 1, .5], np.float64)
|
| 80 |
+
|
| 81 |
+
assert_allclose(blas._test_dasum(x), 8.5)
|
| 82 |
+
assert_allclose(blas._test_ddot(x, y), 6.75)
|
| 83 |
+
assert_allclose(blas._test_dnrm2(x), 5.85234975815)
|
| 84 |
+
|
| 85 |
+
assert_allclose(blas._test_dasum(x[::2]), 5.5)
|
| 86 |
+
assert_allclose(blas._test_ddot(x[::2], y[::2]), 9.75)
|
| 87 |
+
assert_allclose(blas._test_dnrm2(x[::2]), 5.0249376297)
|
| 88 |
+
|
| 89 |
+
assert_equal(blas._test_idamax(x), 1)
|
| 90 |
+
|
| 91 |
+
def test_float_args(self):
|
| 92 |
+
|
| 93 |
+
x = np.array([5., -3, -.5], np.float32)
|
| 94 |
+
y = np.array([2, 1, .5], np.float32)
|
| 95 |
+
|
| 96 |
+
assert_equal(blas._test_isamax(x), 1)
|
| 97 |
+
|
| 98 |
+
assert_allclose(blas._test_sasum(x), 8.5)
|
| 99 |
+
assert_allclose(blas._test_sdot(x, y), 6.75)
|
| 100 |
+
assert_allclose(blas._test_snrm2(x), 5.85234975815)
|
| 101 |
+
|
| 102 |
+
assert_allclose(blas._test_sasum(x[::2]), 5.5)
|
| 103 |
+
assert_allclose(blas._test_sdot(x[::2], y[::2]), 9.75)
|
| 104 |
+
assert_allclose(blas._test_snrm2(x[::2]), 5.0249376297)
|
| 105 |
+
|
| 106 |
+
def test_double_complex_args(self):
|
| 107 |
+
|
| 108 |
+
cx = np.array([.5 + 1.j, .25 - .375j, 13. - 4.j], np.complex128)
|
| 109 |
+
cy = np.array([.875 + 2.j, .875 - .625j, -1. + 2.j], np.complex128)
|
| 110 |
+
|
| 111 |
+
assert_equal(blas._test_izamax(cx), 3)
|
| 112 |
+
|
| 113 |
+
assert_allclose(blas._test_zdotc(cx, cy), -18.109375+22.296875j)
|
| 114 |
+
assert_allclose(blas._test_zdotu(cx, cy), -6.578125+31.390625j)
|
| 115 |
+
|
| 116 |
+
assert_allclose(blas._test_zdotc(cx[::2], cy[::2]), -18.5625+22.125j)
|
| 117 |
+
assert_allclose(blas._test_zdotu(cx[::2], cy[::2]), -6.5625+31.875j)
|
| 118 |
+
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_cython_lapack.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from numpy.testing import assert_allclose
|
| 2 |
+
from scipy.linalg import cython_lapack as cython_lapack
|
| 3 |
+
from scipy.linalg import lapack
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class TestLamch:
|
| 7 |
+
|
| 8 |
+
def test_slamch(self):
|
| 9 |
+
for c in [b'e', b's', b'b', b'p', b'n', b'r', b'm', b'u', b'l', b'o']:
|
| 10 |
+
assert_allclose(cython_lapack._test_slamch(c),
|
| 11 |
+
lapack.slamch(c))
|
| 12 |
+
|
| 13 |
+
def test_dlamch(self):
|
| 14 |
+
for c in [b'e', b's', b'b', b'p', b'n', b'r', b'm', b'u', b'l', b'o']:
|
| 15 |
+
assert_allclose(cython_lapack._test_dlamch(c),
|
| 16 |
+
lapack.dlamch(c))
|
| 17 |
+
|
| 18 |
+
def test_complex_ladiv(self):
|
| 19 |
+
cx = .5 + 1.j
|
| 20 |
+
cy = .875 + 2.j
|
| 21 |
+
assert_allclose(cython_lapack._test_zladiv(cy, cx), 1.95+0.1j)
|
| 22 |
+
assert_allclose(cython_lapack._test_cladiv(cy, cx), 1.95+0.1j)
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_cythonized_array_utils.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from scipy.linalg import bandwidth, issymmetric, ishermitian
|
| 3 |
+
import pytest
|
| 4 |
+
from pytest import raises
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def test_bandwidth_dtypes():
|
| 8 |
+
n = 5
|
| 9 |
+
for t in np.typecodes['All']:
|
| 10 |
+
A = np.zeros([n, n], dtype=t)
|
| 11 |
+
if t in 'eUVOMm':
|
| 12 |
+
raises(TypeError, bandwidth, A)
|
| 13 |
+
elif t == 'G': # No-op test. On win these pass on others fail.
|
| 14 |
+
pass
|
| 15 |
+
else:
|
| 16 |
+
_ = bandwidth(A)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def test_bandwidth_non2d_input():
|
| 20 |
+
A = np.array([1, 2, 3])
|
| 21 |
+
raises(ValueError, bandwidth, A)
|
| 22 |
+
A = np.array([[[1, 2, 3], [4, 5, 6]]])
|
| 23 |
+
raises(ValueError, bandwidth, A)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
@pytest.mark.parametrize('T', [x for x in np.typecodes['All']
|
| 27 |
+
if x not in 'eGUVOMm'])
|
| 28 |
+
def test_bandwidth_square_inputs(T):
|
| 29 |
+
n = 20
|
| 30 |
+
k = 4
|
| 31 |
+
R = np.zeros([n, n], dtype=T, order='F')
|
| 32 |
+
# form a banded matrix inplace
|
| 33 |
+
R[[x for x in range(n)], [x for x in range(n)]] = 1
|
| 34 |
+
R[[x for x in range(n-k)], [x for x in range(k, n)]] = 1
|
| 35 |
+
R[[x for x in range(1, n)], [x for x in range(n-1)]] = 1
|
| 36 |
+
R[[x for x in range(k, n)], [x for x in range(n-k)]] = 1
|
| 37 |
+
assert bandwidth(R) == (k, k)
|
| 38 |
+
A = np.array([
|
| 39 |
+
[1, 1, 0, 0, 0, 0, 0, 0],
|
| 40 |
+
[1, 0, 0, 0, 0, 0, 0, 0],
|
| 41 |
+
[0, 0, 0, 0, 0, 0, 0, 0],
|
| 42 |
+
[0, 0, 0, 0, 0, 0, 0, 0],
|
| 43 |
+
[0, 0, 0, 0, 0, 0, 0, 0],
|
| 44 |
+
[0, 0, 0, 0, 0, 1, 1, 1],
|
| 45 |
+
[0, 0, 0, 0, 0, 1, 0, 0],
|
| 46 |
+
[0, 0, 0, 0, 0, 1, 0, 0],
|
| 47 |
+
])
|
| 48 |
+
assert bandwidth(A) == (2, 2)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
@pytest.mark.parametrize('T', [x for x in np.typecodes['All']
|
| 52 |
+
if x not in 'eGUVOMm'])
|
| 53 |
+
def test_bandwidth_rect_inputs(T):
|
| 54 |
+
n, m = 10, 20
|
| 55 |
+
k = 5
|
| 56 |
+
R = np.zeros([n, m], dtype=T, order='F')
|
| 57 |
+
# form a banded matrix inplace
|
| 58 |
+
R[[x for x in range(n)], [x for x in range(n)]] = 1
|
| 59 |
+
R[[x for x in range(n-k)], [x for x in range(k, n)]] = 1
|
| 60 |
+
R[[x for x in range(1, n)], [x for x in range(n-1)]] = 1
|
| 61 |
+
R[[x for x in range(k, n)], [x for x in range(n-k)]] = 1
|
| 62 |
+
assert bandwidth(R) == (k, k)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def test_issymetric_ishermitian_dtypes():
|
| 66 |
+
n = 5
|
| 67 |
+
for t in np.typecodes['All']:
|
| 68 |
+
A = np.zeros([n, n], dtype=t)
|
| 69 |
+
if t in 'eUVOMm':
|
| 70 |
+
raises(TypeError, issymmetric, A)
|
| 71 |
+
raises(TypeError, ishermitian, A)
|
| 72 |
+
elif t == 'G': # No-op test. On win these pass on others fail.
|
| 73 |
+
pass
|
| 74 |
+
else:
|
| 75 |
+
assert issymmetric(A)
|
| 76 |
+
assert ishermitian(A)
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def test_issymmetric_ishermitian_invalid_input():
|
| 80 |
+
A = np.array([1, 2, 3])
|
| 81 |
+
raises(ValueError, issymmetric, A)
|
| 82 |
+
raises(ValueError, ishermitian, A)
|
| 83 |
+
A = np.array([[[1, 2, 3], [4, 5, 6]]])
|
| 84 |
+
raises(ValueError, issymmetric, A)
|
| 85 |
+
raises(ValueError, ishermitian, A)
|
| 86 |
+
A = np.array([[1, 2, 3], [4, 5, 6]])
|
| 87 |
+
raises(ValueError, issymmetric, A)
|
| 88 |
+
raises(ValueError, ishermitian, A)
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def test_issymetric_complex_decimals():
|
| 92 |
+
A = np.arange(1, 10).astype(complex).reshape(3, 3)
|
| 93 |
+
A += np.arange(-4, 5).astype(complex).reshape(3, 3)*1j
|
| 94 |
+
# make entries decimal
|
| 95 |
+
A /= np.pi
|
| 96 |
+
A = A + A.T
|
| 97 |
+
assert issymmetric(A)
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def test_ishermitian_complex_decimals():
|
| 101 |
+
A = np.arange(1, 10).astype(complex).reshape(3, 3)
|
| 102 |
+
A += np.arange(-4, 5).astype(complex).reshape(3, 3)*1j
|
| 103 |
+
# make entries decimal
|
| 104 |
+
A /= np.pi
|
| 105 |
+
A = A + A.T.conj()
|
| 106 |
+
assert ishermitian(A)
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
def test_issymmetric_approximate_results():
|
| 110 |
+
n = 20
|
| 111 |
+
rng = np.random.RandomState(123456789)
|
| 112 |
+
x = rng.uniform(high=5., size=[n, n])
|
| 113 |
+
y = x @ x.T # symmetric
|
| 114 |
+
p = rng.standard_normal([n, n])
|
| 115 |
+
z = p @ y @ p.T
|
| 116 |
+
assert issymmetric(z, atol=1e-10)
|
| 117 |
+
assert issymmetric(z, atol=1e-10, rtol=0.)
|
| 118 |
+
assert issymmetric(z, atol=0., rtol=1e-12)
|
| 119 |
+
assert issymmetric(z, atol=1e-13, rtol=1e-12)
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
def test_ishermitian_approximate_results():
|
| 123 |
+
n = 20
|
| 124 |
+
rng = np.random.RandomState(987654321)
|
| 125 |
+
x = rng.uniform(high=5., size=[n, n])
|
| 126 |
+
y = x @ x.T # symmetric
|
| 127 |
+
p = rng.standard_normal([n, n]) + rng.standard_normal([n, n])*1j
|
| 128 |
+
z = p @ y @ p.conj().T
|
| 129 |
+
assert ishermitian(z, atol=1e-10)
|
| 130 |
+
assert ishermitian(z, atol=1e-10, rtol=0.)
|
| 131 |
+
assert ishermitian(z, atol=0., rtol=1e-12)
|
| 132 |
+
assert ishermitian(z, atol=1e-13, rtol=1e-12)
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_decomp.py
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_decomp_cholesky.py
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
import numpy as np
|
| 3 |
+
from numpy.testing import assert_array_almost_equal
|
| 4 |
+
from pytest import raises as assert_raises
|
| 5 |
+
|
| 6 |
+
from numpy import array, transpose, dot, conjugate, zeros_like, empty
|
| 7 |
+
from numpy.random import random
|
| 8 |
+
from scipy.linalg import (cholesky, cholesky_banded, cho_solve_banded,
|
| 9 |
+
cho_factor, cho_solve)
|
| 10 |
+
|
| 11 |
+
from scipy.linalg._testutils import assert_no_overwrite
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class TestCholesky:
|
| 15 |
+
|
| 16 |
+
def test_simple(self):
|
| 17 |
+
a = [[8, 2, 3], [2, 9, 3], [3, 3, 6]]
|
| 18 |
+
c = cholesky(a)
|
| 19 |
+
assert_array_almost_equal(dot(transpose(c), c), a)
|
| 20 |
+
c = transpose(c)
|
| 21 |
+
a = dot(c, transpose(c))
|
| 22 |
+
assert_array_almost_equal(cholesky(a, lower=1), c)
|
| 23 |
+
|
| 24 |
+
def test_check_finite(self):
|
| 25 |
+
a = [[8, 2, 3], [2, 9, 3], [3, 3, 6]]
|
| 26 |
+
c = cholesky(a, check_finite=False)
|
| 27 |
+
assert_array_almost_equal(dot(transpose(c), c), a)
|
| 28 |
+
c = transpose(c)
|
| 29 |
+
a = dot(c, transpose(c))
|
| 30 |
+
assert_array_almost_equal(cholesky(a, lower=1, check_finite=False), c)
|
| 31 |
+
|
| 32 |
+
def test_simple_complex(self):
|
| 33 |
+
m = array([[3+1j, 3+4j, 5], [0, 2+2j, 2+7j], [0, 0, 7+4j]])
|
| 34 |
+
a = dot(transpose(conjugate(m)), m)
|
| 35 |
+
c = cholesky(a)
|
| 36 |
+
a1 = dot(transpose(conjugate(c)), c)
|
| 37 |
+
assert_array_almost_equal(a, a1)
|
| 38 |
+
c = transpose(c)
|
| 39 |
+
a = dot(c, transpose(conjugate(c)))
|
| 40 |
+
assert_array_almost_equal(cholesky(a, lower=1), c)
|
| 41 |
+
|
| 42 |
+
def test_random(self):
|
| 43 |
+
n = 20
|
| 44 |
+
for k in range(2):
|
| 45 |
+
m = random([n, n])
|
| 46 |
+
for i in range(n):
|
| 47 |
+
m[i, i] = 20*(.1+m[i, i])
|
| 48 |
+
a = dot(transpose(m), m)
|
| 49 |
+
c = cholesky(a)
|
| 50 |
+
a1 = dot(transpose(c), c)
|
| 51 |
+
assert_array_almost_equal(a, a1)
|
| 52 |
+
c = transpose(c)
|
| 53 |
+
a = dot(c, transpose(c))
|
| 54 |
+
assert_array_almost_equal(cholesky(a, lower=1), c)
|
| 55 |
+
|
| 56 |
+
def test_random_complex(self):
|
| 57 |
+
n = 20
|
| 58 |
+
for k in range(2):
|
| 59 |
+
m = random([n, n])+1j*random([n, n])
|
| 60 |
+
for i in range(n):
|
| 61 |
+
m[i, i] = 20*(.1+abs(m[i, i]))
|
| 62 |
+
a = dot(transpose(conjugate(m)), m)
|
| 63 |
+
c = cholesky(a)
|
| 64 |
+
a1 = dot(transpose(conjugate(c)), c)
|
| 65 |
+
assert_array_almost_equal(a, a1)
|
| 66 |
+
c = transpose(c)
|
| 67 |
+
a = dot(c, transpose(conjugate(c)))
|
| 68 |
+
assert_array_almost_equal(cholesky(a, lower=1), c)
|
| 69 |
+
|
| 70 |
+
@pytest.mark.xslow
|
| 71 |
+
def test_int_overflow(self):
|
| 72 |
+
# regression test for
|
| 73 |
+
# https://github.com/scipy/scipy/issues/17436
|
| 74 |
+
# the problem was an int overflow in zeroing out
|
| 75 |
+
# the unused triangular part
|
| 76 |
+
n = 47_000
|
| 77 |
+
x = np.eye(n, dtype=np.float64, order='F')
|
| 78 |
+
x[:4, :4] = np.array([[4, -2, 3, -1],
|
| 79 |
+
[-2, 4, -3, 1],
|
| 80 |
+
[3, -3, 5, 0],
|
| 81 |
+
[-1, 1, 0, 5]])
|
| 82 |
+
|
| 83 |
+
cholesky(x, check_finite=False, overwrite_a=True) # should not segfault
|
| 84 |
+
|
| 85 |
+
@pytest.mark.parametrize('dt', [int, float, np.float32, complex, np.complex64])
|
| 86 |
+
@pytest.mark.parametrize('dt_b', [int, float, np.float32, complex, np.complex64])
|
| 87 |
+
def test_empty(self, dt, dt_b):
|
| 88 |
+
a = empty((0, 0), dtype=dt)
|
| 89 |
+
|
| 90 |
+
c = cholesky(a)
|
| 91 |
+
assert c.shape == (0, 0)
|
| 92 |
+
assert c.dtype == cholesky(np.eye(2, dtype=dt)).dtype
|
| 93 |
+
|
| 94 |
+
c_and_lower = (c, True)
|
| 95 |
+
b = np.asarray([], dtype=dt_b)
|
| 96 |
+
x = cho_solve(c_and_lower, b)
|
| 97 |
+
assert x.shape == (0,)
|
| 98 |
+
assert x.dtype == cho_solve((np.eye(2, dtype=dt), True),
|
| 99 |
+
np.ones(2, dtype=dt_b)).dtype
|
| 100 |
+
|
| 101 |
+
b = empty((0, 0), dtype=dt_b)
|
| 102 |
+
x = cho_solve(c_and_lower, b)
|
| 103 |
+
assert x.shape == (0, 0)
|
| 104 |
+
assert x.dtype == cho_solve((np.eye(2, dtype=dt), True),
|
| 105 |
+
np.ones(2, dtype=dt_b)).dtype
|
| 106 |
+
|
| 107 |
+
a1 = array([])
|
| 108 |
+
a2 = array([[]])
|
| 109 |
+
a3 = []
|
| 110 |
+
a4 = [[]]
|
| 111 |
+
for x in ([a1, a2, a3, a4]):
|
| 112 |
+
assert_raises(ValueError, cholesky, x)
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
class TestCholeskyBanded:
|
| 116 |
+
"""Tests for cholesky_banded() and cho_solve_banded."""
|
| 117 |
+
|
| 118 |
+
def test_check_finite(self):
|
| 119 |
+
# Symmetric positive definite banded matrix `a`
|
| 120 |
+
a = array([[4.0, 1.0, 0.0, 0.0],
|
| 121 |
+
[1.0, 4.0, 0.5, 0.0],
|
| 122 |
+
[0.0, 0.5, 4.0, 0.2],
|
| 123 |
+
[0.0, 0.0, 0.2, 4.0]])
|
| 124 |
+
# Banded storage form of `a`.
|
| 125 |
+
ab = array([[-1.0, 1.0, 0.5, 0.2],
|
| 126 |
+
[4.0, 4.0, 4.0, 4.0]])
|
| 127 |
+
c = cholesky_banded(ab, lower=False, check_finite=False)
|
| 128 |
+
ufac = zeros_like(a)
|
| 129 |
+
ufac[list(range(4)), list(range(4))] = c[-1]
|
| 130 |
+
ufac[(0, 1, 2), (1, 2, 3)] = c[0, 1:]
|
| 131 |
+
assert_array_almost_equal(a, dot(ufac.T, ufac))
|
| 132 |
+
|
| 133 |
+
b = array([0.0, 0.5, 4.2, 4.2])
|
| 134 |
+
x = cho_solve_banded((c, False), b, check_finite=False)
|
| 135 |
+
assert_array_almost_equal(x, [0.0, 0.0, 1.0, 1.0])
|
| 136 |
+
|
| 137 |
+
def test_upper_real(self):
|
| 138 |
+
# Symmetric positive definite banded matrix `a`
|
| 139 |
+
a = array([[4.0, 1.0, 0.0, 0.0],
|
| 140 |
+
[1.0, 4.0, 0.5, 0.0],
|
| 141 |
+
[0.0, 0.5, 4.0, 0.2],
|
| 142 |
+
[0.0, 0.0, 0.2, 4.0]])
|
| 143 |
+
# Banded storage form of `a`.
|
| 144 |
+
ab = array([[-1.0, 1.0, 0.5, 0.2],
|
| 145 |
+
[4.0, 4.0, 4.0, 4.0]])
|
| 146 |
+
c = cholesky_banded(ab, lower=False)
|
| 147 |
+
ufac = zeros_like(a)
|
| 148 |
+
ufac[list(range(4)), list(range(4))] = c[-1]
|
| 149 |
+
ufac[(0, 1, 2), (1, 2, 3)] = c[0, 1:]
|
| 150 |
+
assert_array_almost_equal(a, dot(ufac.T, ufac))
|
| 151 |
+
|
| 152 |
+
b = array([0.0, 0.5, 4.2, 4.2])
|
| 153 |
+
x = cho_solve_banded((c, False), b)
|
| 154 |
+
assert_array_almost_equal(x, [0.0, 0.0, 1.0, 1.0])
|
| 155 |
+
|
| 156 |
+
def test_upper_complex(self):
|
| 157 |
+
# Hermitian positive definite banded matrix `a`
|
| 158 |
+
a = array([[4.0, 1.0, 0.0, 0.0],
|
| 159 |
+
[1.0, 4.0, 0.5, 0.0],
|
| 160 |
+
[0.0, 0.5, 4.0, -0.2j],
|
| 161 |
+
[0.0, 0.0, 0.2j, 4.0]])
|
| 162 |
+
# Banded storage form of `a`.
|
| 163 |
+
ab = array([[-1.0, 1.0, 0.5, -0.2j],
|
| 164 |
+
[4.0, 4.0, 4.0, 4.0]])
|
| 165 |
+
c = cholesky_banded(ab, lower=False)
|
| 166 |
+
ufac = zeros_like(a)
|
| 167 |
+
ufac[list(range(4)), list(range(4))] = c[-1]
|
| 168 |
+
ufac[(0, 1, 2), (1, 2, 3)] = c[0, 1:]
|
| 169 |
+
assert_array_almost_equal(a, dot(ufac.conj().T, ufac))
|
| 170 |
+
|
| 171 |
+
b = array([0.0, 0.5, 4.0-0.2j, 0.2j + 4.0])
|
| 172 |
+
x = cho_solve_banded((c, False), b)
|
| 173 |
+
assert_array_almost_equal(x, [0.0, 0.0, 1.0, 1.0])
|
| 174 |
+
|
| 175 |
+
def test_lower_real(self):
|
| 176 |
+
# Symmetric positive definite banded matrix `a`
|
| 177 |
+
a = array([[4.0, 1.0, 0.0, 0.0],
|
| 178 |
+
[1.0, 4.0, 0.5, 0.0],
|
| 179 |
+
[0.0, 0.5, 4.0, 0.2],
|
| 180 |
+
[0.0, 0.0, 0.2, 4.0]])
|
| 181 |
+
# Banded storage form of `a`.
|
| 182 |
+
ab = array([[4.0, 4.0, 4.0, 4.0],
|
| 183 |
+
[1.0, 0.5, 0.2, -1.0]])
|
| 184 |
+
c = cholesky_banded(ab, lower=True)
|
| 185 |
+
lfac = zeros_like(a)
|
| 186 |
+
lfac[list(range(4)), list(range(4))] = c[0]
|
| 187 |
+
lfac[(1, 2, 3), (0, 1, 2)] = c[1, :3]
|
| 188 |
+
assert_array_almost_equal(a, dot(lfac, lfac.T))
|
| 189 |
+
|
| 190 |
+
b = array([0.0, 0.5, 4.2, 4.2])
|
| 191 |
+
x = cho_solve_banded((c, True), b)
|
| 192 |
+
assert_array_almost_equal(x, [0.0, 0.0, 1.0, 1.0])
|
| 193 |
+
|
| 194 |
+
def test_lower_complex(self):
|
| 195 |
+
# Hermitian positive definite banded matrix `a`
|
| 196 |
+
a = array([[4.0, 1.0, 0.0, 0.0],
|
| 197 |
+
[1.0, 4.0, 0.5, 0.0],
|
| 198 |
+
[0.0, 0.5, 4.0, -0.2j],
|
| 199 |
+
[0.0, 0.0, 0.2j, 4.0]])
|
| 200 |
+
# Banded storage form of `a`.
|
| 201 |
+
ab = array([[4.0, 4.0, 4.0, 4.0],
|
| 202 |
+
[1.0, 0.5, 0.2j, -1.0]])
|
| 203 |
+
c = cholesky_banded(ab, lower=True)
|
| 204 |
+
lfac = zeros_like(a)
|
| 205 |
+
lfac[list(range(4)), list(range(4))] = c[0]
|
| 206 |
+
lfac[(1, 2, 3), (0, 1, 2)] = c[1, :3]
|
| 207 |
+
assert_array_almost_equal(a, dot(lfac, lfac.conj().T))
|
| 208 |
+
|
| 209 |
+
b = array([0.0, 0.5j, 3.8j, 3.8])
|
| 210 |
+
x = cho_solve_banded((c, True), b)
|
| 211 |
+
assert_array_almost_equal(x, [0.0, 0.0, 1.0j, 1.0])
|
| 212 |
+
|
| 213 |
+
@pytest.mark.parametrize('dt', [int, float, np.float32, complex, np.complex64])
|
| 214 |
+
@pytest.mark.parametrize('dt_b', [int, float, np.float32, complex, np.complex64])
|
| 215 |
+
def test_empty(self, dt, dt_b):
|
| 216 |
+
ab = empty((0, 0), dtype=dt)
|
| 217 |
+
|
| 218 |
+
cb = cholesky_banded(ab)
|
| 219 |
+
assert cb.shape == (0, 0)
|
| 220 |
+
|
| 221 |
+
m = cholesky_banded(np.array([[0, 0], [1, 1]], dtype=dt))
|
| 222 |
+
assert cb.dtype == m.dtype
|
| 223 |
+
|
| 224 |
+
cb_and_lower = (cb, True)
|
| 225 |
+
b = np.asarray([], dtype=dt_b)
|
| 226 |
+
x = cho_solve_banded(cb_and_lower, b)
|
| 227 |
+
assert x.shape == (0,)
|
| 228 |
+
|
| 229 |
+
dtype_nonempty = cho_solve_banded((m, True), np.ones(2, dtype=dt_b)).dtype
|
| 230 |
+
assert x.dtype == dtype_nonempty
|
| 231 |
+
|
| 232 |
+
b = empty((0, 0), dtype=dt_b)
|
| 233 |
+
x = cho_solve_banded(cb_and_lower, b)
|
| 234 |
+
assert x.shape == (0, 0)
|
| 235 |
+
assert x.dtype == dtype_nonempty
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
class TestOverwrite:
|
| 239 |
+
def test_cholesky(self):
|
| 240 |
+
assert_no_overwrite(cholesky, [(3, 3)])
|
| 241 |
+
|
| 242 |
+
def test_cho_factor(self):
|
| 243 |
+
assert_no_overwrite(cho_factor, [(3, 3)])
|
| 244 |
+
|
| 245 |
+
def test_cho_solve(self):
|
| 246 |
+
x = array([[2, -1, 0], [-1, 2, -1], [0, -1, 2]])
|
| 247 |
+
xcho = cho_factor(x)
|
| 248 |
+
assert_no_overwrite(lambda b: cho_solve(xcho, b), [(3,)])
|
| 249 |
+
|
| 250 |
+
def test_cholesky_banded(self):
|
| 251 |
+
assert_no_overwrite(cholesky_banded, [(2, 3)])
|
| 252 |
+
|
| 253 |
+
def test_cho_solve_banded(self):
|
| 254 |
+
x = array([[0, -1, -1], [2, 2, 2]])
|
| 255 |
+
xcho = cholesky_banded(x)
|
| 256 |
+
assert_no_overwrite(lambda b: cho_solve_banded((xcho, False), b),
|
| 257 |
+
[(3,)])
|
| 258 |
+
|
| 259 |
+
class TestChoFactor:
|
| 260 |
+
@pytest.mark.parametrize('dt', [int, float, np.float32, complex, np.complex64])
|
| 261 |
+
def test_empty(self, dt):
|
| 262 |
+
a = np.empty((0, 0), dtype=dt)
|
| 263 |
+
x, lower = cho_factor(a)
|
| 264 |
+
|
| 265 |
+
assert x.shape == (0, 0)
|
| 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
from scipy import linalg
|
| 7 |
+
from scipy.linalg.lapack import _compute_lwork
|
| 8 |
+
from scipy.stats import ortho_group, unitary_group
|
| 9 |
+
from scipy.linalg import cossin, get_lapack_funcs
|
| 10 |
+
|
| 11 |
+
REAL_DTYPES = (np.float32, np.float64)
|
| 12 |
+
COMPLEX_DTYPES = (np.complex64, np.complex128)
|
| 13 |
+
DTYPES = REAL_DTYPES + COMPLEX_DTYPES
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
@pytest.mark.parametrize('dtype_', DTYPES)
|
| 17 |
+
@pytest.mark.parametrize('m, p, q',
|
| 18 |
+
[
|
| 19 |
+
(2, 1, 1),
|
| 20 |
+
(3, 2, 1),
|
| 21 |
+
(3, 1, 2),
|
| 22 |
+
(4, 2, 2),
|
| 23 |
+
(4, 1, 2),
|
| 24 |
+
(40, 12, 20),
|
| 25 |
+
(40, 30, 1),
|
| 26 |
+
(40, 1, 30),
|
| 27 |
+
(100, 50, 1),
|
| 28 |
+
(100, 50, 50),
|
| 29 |
+
])
|
| 30 |
+
@pytest.mark.parametrize('swap_sign', [True, False])
|
| 31 |
+
def test_cossin(dtype_, m, p, q, swap_sign):
|
| 32 |
+
rng = default_rng(1708093570726217)
|
| 33 |
+
if dtype_ in COMPLEX_DTYPES:
|
| 34 |
+
x = np.array(unitary_group.rvs(m, random_state=rng), dtype=dtype_)
|
| 35 |
+
else:
|
| 36 |
+
x = np.array(ortho_group.rvs(m, random_state=rng), dtype=dtype_)
|
| 37 |
+
|
| 38 |
+
u, cs, vh = cossin(x, p, q,
|
| 39 |
+
swap_sign=swap_sign)
|
| 40 |
+
assert_allclose(x, u @ cs @ vh, rtol=0., atol=m*1e3*np.finfo(dtype_).eps)
|
| 41 |
+
assert u.dtype == dtype_
|
| 42 |
+
# Test for float32 or float 64
|
| 43 |
+
assert cs.dtype == np.real(u).dtype
|
| 44 |
+
assert vh.dtype == dtype_
|
| 45 |
+
|
| 46 |
+
u, cs, vh = cossin([x[:p, :q], x[:p, q:], x[p:, :q], x[p:, q:]],
|
| 47 |
+
swap_sign=swap_sign)
|
| 48 |
+
assert_allclose(x, u @ cs @ vh, rtol=0., atol=m*1e3*np.finfo(dtype_).eps)
|
| 49 |
+
assert u.dtype == dtype_
|
| 50 |
+
assert cs.dtype == np.real(u).dtype
|
| 51 |
+
assert vh.dtype == dtype_
|
| 52 |
+
|
| 53 |
+
_, cs2, vh2 = cossin(x, p, q,
|
| 54 |
+
compute_u=False,
|
| 55 |
+
swap_sign=swap_sign)
|
| 56 |
+
assert_allclose(cs, cs2, rtol=0., atol=10*np.finfo(dtype_).eps)
|
| 57 |
+
assert_allclose(vh, vh2, rtol=0., atol=10*np.finfo(dtype_).eps)
|
| 58 |
+
|
| 59 |
+
u2, cs2, _ = cossin(x, p, q,
|
| 60 |
+
compute_vh=False,
|
| 61 |
+
swap_sign=swap_sign)
|
| 62 |
+
assert_allclose(u, u2, rtol=0., atol=10*np.finfo(dtype_).eps)
|
| 63 |
+
assert_allclose(cs, cs2, rtol=0., atol=10*np.finfo(dtype_).eps)
|
| 64 |
+
|
| 65 |
+
_, cs2, _ = cossin(x, p, q,
|
| 66 |
+
compute_u=False,
|
| 67 |
+
compute_vh=False,
|
| 68 |
+
swap_sign=swap_sign)
|
| 69 |
+
assert_allclose(cs, cs2, rtol=0., atol=10*np.finfo(dtype_).eps)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def test_cossin_mixed_types():
|
| 73 |
+
rng = default_rng(1708093736390459)
|
| 74 |
+
x = np.array(ortho_group.rvs(4, random_state=rng), dtype=np.float64)
|
| 75 |
+
u, cs, vh = cossin([x[:2, :2],
|
| 76 |
+
np.array(x[:2, 2:], dtype=np.complex128),
|
| 77 |
+
x[2:, :2],
|
| 78 |
+
x[2:, 2:]])
|
| 79 |
+
|
| 80 |
+
assert u.dtype == np.complex128
|
| 81 |
+
assert cs.dtype == np.float64
|
| 82 |
+
assert vh.dtype == np.complex128
|
| 83 |
+
assert_allclose(x, u @ cs @ vh, rtol=0.,
|
| 84 |
+
atol=1e4 * np.finfo(np.complex128).eps)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def test_cossin_error_incorrect_subblocks():
|
| 88 |
+
with pytest.raises(ValueError, match="be due to missing p, q arguments."):
|
| 89 |
+
cossin(([1, 2], [3, 4, 5], [6, 7], [8, 9, 10]))
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def test_cossin_error_empty_subblocks():
|
| 93 |
+
with pytest.raises(ValueError, match="x11.*empty"):
|
| 94 |
+
cossin(([], [], [], []))
|
| 95 |
+
with pytest.raises(ValueError, match="x12.*empty"):
|
| 96 |
+
cossin(([1, 2], [], [6, 7], [8, 9, 10]))
|
| 97 |
+
with pytest.raises(ValueError, match="x21.*empty"):
|
| 98 |
+
cossin(([1, 2], [3, 4, 5], [], [8, 9, 10]))
|
| 99 |
+
with pytest.raises(ValueError, match="x22.*empty"):
|
| 100 |
+
cossin(([1, 2], [3, 4, 5], [2], []))
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def test_cossin_error_missing_partitioning():
|
| 104 |
+
with pytest.raises(ValueError, match=".*exactly four arrays.* got 2"):
|
| 105 |
+
cossin(unitary_group.rvs(2))
|
| 106 |
+
|
| 107 |
+
with pytest.raises(ValueError, match=".*might be due to missing p, q"):
|
| 108 |
+
cossin(unitary_group.rvs(4))
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def test_cossin_error_non_iterable():
|
| 112 |
+
with pytest.raises(ValueError, match="containing the subblocks of X"):
|
| 113 |
+
cossin(12j)
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def test_cossin_error_non_square():
|
| 117 |
+
with pytest.raises(ValueError, match="only supports square"):
|
| 118 |
+
cossin(np.array([[1, 2]]), 1, 1)
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
def test_cossin_error_partitioning():
|
| 122 |
+
x = np.array(ortho_group.rvs(4), dtype=np.float64)
|
| 123 |
+
with pytest.raises(ValueError, match="invalid p=0.*0<p<4.*"):
|
| 124 |
+
cossin(x, 0, 1)
|
| 125 |
+
with pytest.raises(ValueError, match="invalid p=4.*0<p<4.*"):
|
| 126 |
+
cossin(x, 4, 1)
|
| 127 |
+
with pytest.raises(ValueError, match="invalid q=-2.*0<q<4.*"):
|
| 128 |
+
cossin(x, 1, -2)
|
| 129 |
+
with pytest.raises(ValueError, match="invalid q=5.*0<q<4.*"):
|
| 130 |
+
cossin(x, 1, 5)
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
@pytest.mark.parametrize("dtype_", DTYPES)
|
| 134 |
+
def test_cossin_separate(dtype_):
|
| 135 |
+
rng = default_rng(1708093590167096)
|
| 136 |
+
m, p, q = 98, 37, 61
|
| 137 |
+
|
| 138 |
+
pfx = 'or' if dtype_ in REAL_DTYPES else 'un'
|
| 139 |
+
X = (ortho_group.rvs(m, random_state=rng) if pfx == 'or'
|
| 140 |
+
else unitary_group.rvs(m, random_state=rng))
|
| 141 |
+
X = np.array(X, dtype=dtype_)
|
| 142 |
+
|
| 143 |
+
drv, dlw = get_lapack_funcs((pfx + 'csd', pfx + 'csd_lwork'), [X])
|
| 144 |
+
lwval = _compute_lwork(dlw, m, p, q)
|
| 145 |
+
lwvals = {'lwork': lwval} if pfx == 'or' else dict(zip(['lwork',
|
| 146 |
+
'lrwork'],
|
| 147 |
+
lwval))
|
| 148 |
+
|
| 149 |
+
*_, theta, u1, u2, v1t, v2t, _ = \
|
| 150 |
+
drv(X[:p, :q], X[:p, q:], X[p:, :q], X[p:, q:], **lwvals)
|
| 151 |
+
|
| 152 |
+
(u1_2, u2_2), theta2, (v1t_2, v2t_2) = cossin(X, p, q, separate=True)
|
| 153 |
+
|
| 154 |
+
assert_allclose(u1_2, u1, rtol=0., atol=10*np.finfo(dtype_).eps)
|
| 155 |
+
assert_allclose(u2_2, u2, rtol=0., atol=10*np.finfo(dtype_).eps)
|
| 156 |
+
assert_allclose(v1t_2, v1t, rtol=0., atol=10*np.finfo(dtype_).eps)
|
| 157 |
+
assert_allclose(v2t_2, v2t, rtol=0., atol=10*np.finfo(dtype_).eps)
|
| 158 |
+
assert_allclose(theta2, theta, rtol=0., atol=10*np.finfo(dtype_).eps)
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
@pytest.mark.parametrize("m", [2, 5, 10, 15, 20])
|
| 162 |
+
@pytest.mark.parametrize("p", [1, 4, 9, 14, 19])
|
| 163 |
+
@pytest.mark.parametrize("q", [1, 4, 9, 14, 19])
|
| 164 |
+
@pytest.mark.parametrize("swap_sign", [True, False])
|
| 165 |
+
def test_properties(m, p, q, swap_sign):
|
| 166 |
+
# Test all the properties advertised in `linalg.cossin` documentation.
|
| 167 |
+
# There may be some overlap with tests above, but this is sensitive to
|
| 168 |
+
# the bug reported in gh-19365 and more.
|
| 169 |
+
if (p >= m) or (q >= m):
|
| 170 |
+
pytest.skip("`0 < p < m` and `0 < q < m` must hold")
|
| 171 |
+
|
| 172 |
+
# Generate unitary input
|
| 173 |
+
rng = np.random.default_rng(329548272348596421)
|
| 174 |
+
X = unitary_group.rvs(m, random_state=rng)
|
| 175 |
+
np.testing.assert_allclose(X @ X.conj().T, np.eye(m), atol=1e-15)
|
| 176 |
+
|
| 177 |
+
# Perform the decomposition
|
| 178 |
+
u0, cs0, vh0 = linalg.cossin(X, p=p, q=q, separate=True, swap_sign=swap_sign)
|
| 179 |
+
u1, u2 = u0
|
| 180 |
+
v1, v2 = vh0
|
| 181 |
+
v1, v2 = v1.conj().T, v2.conj().T
|
| 182 |
+
|
| 183 |
+
# "U1, U2, V1, V2 are square orthogonal/unitary matrices
|
| 184 |
+
# of dimensions (p,p), (m-p,m-p), (q,q), and (m-q,m-q) respectively"
|
| 185 |
+
np.testing.assert_allclose(u1 @ u1.conj().T, np.eye(p), atol=1e-13)
|
| 186 |
+
np.testing.assert_allclose(u2 @ u2.conj().T, np.eye(m-p), atol=1e-13)
|
| 187 |
+
np.testing.assert_allclose(v1 @ v1.conj().T, np.eye(q), atol=1e-13)
|
| 188 |
+
np.testing.assert_allclose(v2 @ v2.conj().T, np.eye(m-q), atol=1e-13)
|
| 189 |
+
|
| 190 |
+
# "and C and S are (r, r) nonnegative diagonal matrices..."
|
| 191 |
+
C = np.diag(np.cos(cs0))
|
| 192 |
+
S = np.diag(np.sin(cs0))
|
| 193 |
+
# "...satisfying C^2 + S^2 = I where r = min(p, m-p, q, m-q)."
|
| 194 |
+
r = min(p, m-p, q, m-q)
|
| 195 |
+
np.testing.assert_allclose(C**2 + S**2, np.eye(r))
|
| 196 |
+
|
| 197 |
+
# "Moreover, the rank of the identity matrices are
|
| 198 |
+
# min(p, q) - r, min(p, m - q) - r, min(m - p, q) - r,
|
| 199 |
+
# and min(m - p, m - q) - r respectively."
|
| 200 |
+
I11 = np.eye(min(p, q) - r)
|
| 201 |
+
I12 = np.eye(min(p, m - q) - r)
|
| 202 |
+
I21 = np.eye(min(m - p, q) - r)
|
| 203 |
+
I22 = np.eye(min(m - p, m - q) - r)
|
| 204 |
+
|
| 205 |
+
# From:
|
| 206 |
+
# ┌ ┐
|
| 207 |
+
# │ I 0 0 │ 0 0 0 │
|
| 208 |
+
# ┌ ┐ ┌ ┐│ 0 C 0 │ 0 -S 0 │┌ ┐*
|
| 209 |
+
# │ X11 │ X12 │ │ U1 │ ││ 0 0 0 │ 0 0 -I ││ V1 │ │
|
| 210 |
+
# │ ────┼──── │ = │────┼────││─────────┼─────────││────┼────│
|
| 211 |
+
# │ X21 │ X22 │ │ │ U2 ││ 0 0 0 │ I 0 0 ││ │ V2 │
|
| 212 |
+
# └ ┘ └ ┘│ 0 S 0 │ 0 C 0 │└ ┘
|
| 213 |
+
# │ 0 0 I │ 0 0 0 │
|
| 214 |
+
# └ ┘
|
| 215 |
+
|
| 216 |
+
# We can see that U and V are block diagonal matrices like so:
|
| 217 |
+
U = linalg.block_diag(u1, u2)
|
| 218 |
+
V = linalg.block_diag(v1, v2)
|
| 219 |
+
|
| 220 |
+
# And the center matrix, which we'll call Q here, must be:
|
| 221 |
+
Q11 = np.zeros((u1.shape[1], v1.shape[0]))
|
| 222 |
+
IC11 = linalg.block_diag(I11, C)
|
| 223 |
+
Q11[:IC11.shape[0], :IC11.shape[1]] = IC11
|
| 224 |
+
|
| 225 |
+
Q12 = np.zeros((u1.shape[1], v2.shape[0]))
|
| 226 |
+
SI12 = linalg.block_diag(S, I12) if swap_sign else linalg.block_diag(-S, -I12)
|
| 227 |
+
Q12[-SI12.shape[0]:, -SI12.shape[1]:] = SI12
|
| 228 |
+
|
| 229 |
+
Q21 = np.zeros((u2.shape[1], v1.shape[0]))
|
| 230 |
+
SI21 = linalg.block_diag(-S, -I21) if swap_sign else linalg.block_diag(S, I21)
|
| 231 |
+
Q21[-SI21.shape[0]:, -SI21.shape[1]:] = SI21
|
| 232 |
+
|
| 233 |
+
Q22 = np.zeros((u2.shape[1], v2.shape[0]))
|
| 234 |
+
IC22 = linalg.block_diag(I22, C)
|
| 235 |
+
Q22[:IC22.shape[0], :IC22.shape[1]] = IC22
|
| 236 |
+
|
| 237 |
+
Q = np.block([[Q11, Q12], [Q21, Q22]])
|
| 238 |
+
|
| 239 |
+
# Confirm that `cossin` decomposes `X` as shown
|
| 240 |
+
np.testing.assert_allclose(X, U @ Q @ V.conj().T)
|
| 241 |
+
|
| 242 |
+
# And check that `separate=False` agrees
|
| 243 |
+
U0, CS0, Vh0 = linalg.cossin(X, p=p, q=q, swap_sign=swap_sign)
|
| 244 |
+
np.testing.assert_allclose(U, U0)
|
| 245 |
+
np.testing.assert_allclose(Q, CS0)
|
| 246 |
+
np.testing.assert_allclose(V, Vh0.conj().T)
|
| 247 |
+
|
| 248 |
+
# Confirm that `compute_u`/`compute_vh` don't affect the results
|
| 249 |
+
kwargs = dict(p=p, q=q, swap_sign=swap_sign)
|
| 250 |
+
|
| 251 |
+
# `compute_u=False`
|
| 252 |
+
u, cs, vh = linalg.cossin(X, separate=True, compute_u=False, **kwargs)
|
| 253 |
+
assert u[0].shape == (0, 0) # probably not ideal, but this is what it does
|
| 254 |
+
assert u[1].shape == (0, 0)
|
| 255 |
+
assert_allclose(cs, cs0, rtol=1e-15)
|
| 256 |
+
assert_allclose(vh[0], vh0[0], rtol=1e-15)
|
| 257 |
+
assert_allclose(vh[1], vh0[1], rtol=1e-15)
|
| 258 |
+
|
| 259 |
+
U, CS, Vh = linalg.cossin(X, compute_u=False, **kwargs)
|
| 260 |
+
assert U.shape == (0, 0)
|
| 261 |
+
assert_allclose(CS, CS0, rtol=1e-15)
|
| 262 |
+
assert_allclose(Vh, Vh0, rtol=1e-15)
|
| 263 |
+
|
| 264 |
+
# `compute_vh=False`
|
| 265 |
+
u, cs, vh = linalg.cossin(X, separate=True, compute_vh=False, **kwargs)
|
| 266 |
+
assert_allclose(u[0], u[0], rtol=1e-15)
|
| 267 |
+
assert_allclose(u[1], u[1], rtol=1e-15)
|
| 268 |
+
assert_allclose(cs, cs0, rtol=1e-15)
|
| 269 |
+
assert vh[0].shape == (0, 0)
|
| 270 |
+
assert vh[1].shape == (0, 0)
|
| 271 |
+
|
| 272 |
+
U, CS, Vh = linalg.cossin(X, compute_vh=False, **kwargs)
|
| 273 |
+
assert_allclose(U, U0, rtol=1e-15)
|
| 274 |
+
assert_allclose(CS, CS0, rtol=1e-15)
|
| 275 |
+
assert Vh.shape == (0, 0)
|
| 276 |
+
|
| 277 |
+
# `compute_u=False, compute_vh=False`
|
| 278 |
+
u, cs, vh = linalg.cossin(X, separate=True, compute_u=False,
|
| 279 |
+
compute_vh=False, **kwargs)
|
| 280 |
+
assert u[0].shape == (0, 0)
|
| 281 |
+
assert u[1].shape == (0, 0)
|
| 282 |
+
assert_allclose(cs, cs0, rtol=1e-15)
|
| 283 |
+
assert vh[0].shape == (0, 0)
|
| 284 |
+
assert vh[1].shape == (0, 0)
|
| 285 |
+
|
| 286 |
+
U, CS, Vh = linalg.cossin(X, compute_u=False, compute_vh=False, **kwargs)
|
| 287 |
+
assert U.shape == (0, 0)
|
| 288 |
+
assert_allclose(CS, CS0, rtol=1e-15)
|
| 289 |
+
assert Vh.shape == (0, 0)
|
| 290 |
+
|
| 291 |
+
|
| 292 |
+
def test_indexing_bug_gh19365():
|
| 293 |
+
# Regression test for gh-19365, which reported a bug with `separate=False`
|
| 294 |
+
rng = np.random.default_rng(32954827234421)
|
| 295 |
+
m = rng.integers(50, high=100)
|
| 296 |
+
p = rng.integers(10, 40) # always p < m
|
| 297 |
+
q = rng.integers(m - p + 1, m - 1) # always m-p < q < m
|
| 298 |
+
X = unitary_group.rvs(m, random_state=rng) # random unitary matrix
|
| 299 |
+
U, D, Vt = linalg.cossin(X, p=p, q=q, separate=False)
|
| 300 |
+
assert np.allclose(U @ D @ Vt, X)
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_decomp_ldl.py
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from numpy.testing import assert_array_almost_equal, assert_allclose, assert_
|
| 2 |
+
from numpy import (array, eye, zeros, empty_like, empty, tril_indices_from,
|
| 3 |
+
tril, triu_indices_from, spacing, float32, float64,
|
| 4 |
+
complex64, complex128)
|
| 5 |
+
from numpy.random import rand, randint, seed
|
| 6 |
+
from scipy.linalg import ldl
|
| 7 |
+
from scipy._lib._util import ComplexWarning
|
| 8 |
+
import pytest
|
| 9 |
+
from pytest import raises as assert_raises, warns
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@pytest.mark.thread_unsafe
|
| 13 |
+
def test_args():
|
| 14 |
+
A = eye(3)
|
| 15 |
+
# Nonsquare array
|
| 16 |
+
assert_raises(ValueError, ldl, A[:, :2])
|
| 17 |
+
# Complex matrix with imaginary diagonal entries with "hermitian=True"
|
| 18 |
+
with warns(ComplexWarning):
|
| 19 |
+
ldl(A*1j)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def test_empty_array():
|
| 23 |
+
a = empty((0, 0), dtype=complex)
|
| 24 |
+
l, d, p = ldl(empty((0, 0)))
|
| 25 |
+
assert_array_almost_equal(l, empty_like(a))
|
| 26 |
+
assert_array_almost_equal(d, empty_like(a))
|
| 27 |
+
assert_array_almost_equal(p, array([], dtype=int))
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def test_simple():
|
| 31 |
+
a = array([[-0.39-0.71j, 5.14-0.64j, -7.86-2.96j, 3.80+0.92j],
|
| 32 |
+
[5.14-0.64j, 8.86+1.81j, -3.52+0.58j, 5.32-1.59j],
|
| 33 |
+
[-7.86-2.96j, -3.52+0.58j, -2.83-0.03j, -1.54-2.86j],
|
| 34 |
+
[3.80+0.92j, 5.32-1.59j, -1.54-2.86j, -0.56+0.12j]])
|
| 35 |
+
b = array([[5., 10, 1, 18],
|
| 36 |
+
[10., 2, 11, 1],
|
| 37 |
+
[1., 11, 19, 9],
|
| 38 |
+
[18., 1, 9, 0]])
|
| 39 |
+
c = array([[52., 97, 112, 107, 50],
|
| 40 |
+
[97., 114, 89, 98, 13],
|
| 41 |
+
[112., 89, 64, 33, 6],
|
| 42 |
+
[107., 98, 33, 60, 73],
|
| 43 |
+
[50., 13, 6, 73, 77]])
|
| 44 |
+
|
| 45 |
+
d = array([[2., 2, -4, 0, 4],
|
| 46 |
+
[2., -2, -2, 10, -8],
|
| 47 |
+
[-4., -2, 6, -8, -4],
|
| 48 |
+
[0., 10, -8, 6, -6],
|
| 49 |
+
[4., -8, -4, -6, 10]])
|
| 50 |
+
e = array([[-1.36+0.00j, 0+0j, 0+0j, 0+0j],
|
| 51 |
+
[1.58-0.90j, -8.87+0j, 0+0j, 0+0j],
|
| 52 |
+
[2.21+0.21j, -1.84+0.03j, -4.63+0j, 0+0j],
|
| 53 |
+
[3.91-1.50j, -1.78-1.18j, 0.11-0.11j, -1.84+0.00j]])
|
| 54 |
+
for x in (b, c, d):
|
| 55 |
+
l, d, p = ldl(x)
|
| 56 |
+
assert_allclose(l.dot(d).dot(l.T), x, atol=spacing(1000.), rtol=0)
|
| 57 |
+
|
| 58 |
+
u, d, p = ldl(x, lower=False)
|
| 59 |
+
assert_allclose(u.dot(d).dot(u.T), x, atol=spacing(1000.), rtol=0)
|
| 60 |
+
|
| 61 |
+
l, d, p = ldl(a, hermitian=False)
|
| 62 |
+
assert_allclose(l.dot(d).dot(l.T), a, atol=spacing(1000.), rtol=0)
|
| 63 |
+
|
| 64 |
+
u, d, p = ldl(a, lower=False, hermitian=False)
|
| 65 |
+
assert_allclose(u.dot(d).dot(u.T), a, atol=spacing(1000.), rtol=0)
|
| 66 |
+
|
| 67 |
+
# Use upper part for the computation and use the lower part for comparison
|
| 68 |
+
l, d, p = ldl(e.conj().T, lower=0)
|
| 69 |
+
assert_allclose(tril(l.dot(d).dot(l.conj().T)-e), zeros((4, 4)),
|
| 70 |
+
atol=spacing(1000.), rtol=0)
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def test_permutations():
|
| 74 |
+
seed(1234)
|
| 75 |
+
for _ in range(10):
|
| 76 |
+
n = randint(1, 100)
|
| 77 |
+
# Random real/complex array
|
| 78 |
+
x = rand(n, n) if randint(2) else rand(n, n) + rand(n, n)*1j
|
| 79 |
+
x = x + x.conj().T
|
| 80 |
+
x += eye(n)*randint(5, 1e6)
|
| 81 |
+
l_ind = tril_indices_from(x, k=-1)
|
| 82 |
+
u_ind = triu_indices_from(x, k=1)
|
| 83 |
+
|
| 84 |
+
# Test whether permutations lead to a triangular array
|
| 85 |
+
u, d, p = ldl(x, lower=0)
|
| 86 |
+
# lower part should be zero
|
| 87 |
+
assert_(not any(u[p, :][l_ind]), f'Spin {_} failed')
|
| 88 |
+
|
| 89 |
+
l, d, p = ldl(x, lower=1)
|
| 90 |
+
# upper part should be zero
|
| 91 |
+
assert_(not any(l[p, :][u_ind]), f'Spin {_} failed')
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
@pytest.mark.parametrize("dtype", [float32, float64])
|
| 95 |
+
@pytest.mark.parametrize("n", [30, 150])
|
| 96 |
+
def test_ldl_type_size_combinations_real(n, dtype):
|
| 97 |
+
seed(1234)
|
| 98 |
+
msg = (f"Failed for size: {n}, dtype: {dtype}")
|
| 99 |
+
|
| 100 |
+
x = rand(n, n).astype(dtype)
|
| 101 |
+
x = x + x.T
|
| 102 |
+
x += eye(n, dtype=dtype)*dtype(randint(5, 1e6))
|
| 103 |
+
|
| 104 |
+
l, d1, p = ldl(x)
|
| 105 |
+
u, d2, p = ldl(x, lower=0)
|
| 106 |
+
rtol = 1e-4 if dtype is float32 else 1e-10
|
| 107 |
+
assert_allclose(l.dot(d1).dot(l.T), x, rtol=rtol, err_msg=msg)
|
| 108 |
+
assert_allclose(u.dot(d2).dot(u.T), x, rtol=rtol, err_msg=msg)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
@pytest.mark.parametrize("dtype", [complex64, complex128])
|
| 112 |
+
@pytest.mark.parametrize("n", [30, 150])
|
| 113 |
+
def test_ldl_type_size_combinations_complex(n, dtype):
|
| 114 |
+
seed(1234)
|
| 115 |
+
msg1 = (f"Her failed for size: {n}, dtype: {dtype}")
|
| 116 |
+
msg2 = (f"Sym failed for size: {n}, dtype: {dtype}")
|
| 117 |
+
|
| 118 |
+
# Complex hermitian upper/lower
|
| 119 |
+
x = (rand(n, n)+1j*rand(n, n)).astype(dtype)
|
| 120 |
+
x = x+x.conj().T
|
| 121 |
+
x += eye(n, dtype=dtype)*dtype(randint(5, 1e6))
|
| 122 |
+
|
| 123 |
+
l, d1, p = ldl(x)
|
| 124 |
+
u, d2, p = ldl(x, lower=0)
|
| 125 |
+
rtol = 2e-4 if dtype is complex64 else 1e-10
|
| 126 |
+
assert_allclose(l.dot(d1).dot(l.conj().T), x, rtol=rtol, err_msg=msg1)
|
| 127 |
+
assert_allclose(u.dot(d2).dot(u.conj().T), x, rtol=rtol, err_msg=msg1)
|
| 128 |
+
|
| 129 |
+
# Complex symmetric upper/lower
|
| 130 |
+
x = (rand(n, n)+1j*rand(n, n)).astype(dtype)
|
| 131 |
+
x = x+x.T
|
| 132 |
+
x += eye(n, dtype=dtype)*dtype(randint(5, 1e6))
|
| 133 |
+
|
| 134 |
+
l, d1, p = ldl(x, hermitian=0)
|
| 135 |
+
u, d2, p = ldl(x, lower=0, hermitian=0)
|
| 136 |
+
assert_allclose(l.dot(d1).dot(l.T), x, rtol=rtol, err_msg=msg2)
|
| 137 |
+
assert_allclose(u.dot(d2).dot(u.T), x, rtol=rtol, err_msg=msg2)
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/t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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
from pytest import raises as assert_raises
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
from scipy.linalg import lu, lu_factor, lu_solve, get_lapack_funcs, solve
|
| 6 |
+
from numpy.testing import assert_allclose, assert_array_equal, assert_equal
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
REAL_DTYPES = [np.float32, np.float64]
|
| 10 |
+
COMPLEX_DTYPES = [np.complex64, np.complex128]
|
| 11 |
+
DTYPES = REAL_DTYPES + COMPLEX_DTYPES
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class TestLU:
|
| 15 |
+
def setup_method(self):
|
| 16 |
+
self.rng = np.random.default_rng(1682281250228846)
|
| 17 |
+
|
| 18 |
+
def test_old_lu_smoke_tests(self):
|
| 19 |
+
"Tests from old fortran based lu test suite"
|
| 20 |
+
a = np.array([[1, 2, 3], [1, 2, 3], [2, 5, 6]])
|
| 21 |
+
p, l, u = lu(a)
|
| 22 |
+
result_lu = np.array([[2., 5., 6.], [0.5, -0.5, 0.], [0.5, 1., 0.]])
|
| 23 |
+
assert_allclose(p, np.rot90(np.eye(3)))
|
| 24 |
+
assert_allclose(l, np.tril(result_lu, k=-1)+np.eye(3))
|
| 25 |
+
assert_allclose(u, np.triu(result_lu))
|
| 26 |
+
|
| 27 |
+
a = np.array([[1, 2, 3], [1, 2, 3], [2, 5j, 6]])
|
| 28 |
+
p, l, u = lu(a)
|
| 29 |
+
result_lu = np.array([[2., 5.j, 6.], [0.5, 2-2.5j, 0.], [0.5, 1., 0.]])
|
| 30 |
+
assert_allclose(p, np.rot90(np.eye(3)))
|
| 31 |
+
assert_allclose(l, np.tril(result_lu, k=-1)+np.eye(3))
|
| 32 |
+
assert_allclose(u, np.triu(result_lu))
|
| 33 |
+
|
| 34 |
+
b = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
|
| 35 |
+
p, l, u = lu(b)
|
| 36 |
+
assert_allclose(p, np.array([[0, 1, 0], [0, 0, 1], [1, 0, 0]]))
|
| 37 |
+
assert_allclose(l, np.array([[1, 0, 0], [1/7, 1, 0], [4/7, 0.5, 1]]))
|
| 38 |
+
assert_allclose(u, np.array([[7, 8, 9], [0, 6/7, 12/7], [0, 0, 0]]),
|
| 39 |
+
rtol=0., atol=1e-14)
|
| 40 |
+
|
| 41 |
+
cb = np.array([[1.j, 2.j, 3.j], [4j, 5j, 6j], [7j, 8j, 9j]])
|
| 42 |
+
p, l, u = lu(cb)
|
| 43 |
+
assert_allclose(p, np.array([[0, 1, 0], [0, 0, 1], [1, 0, 0]]))
|
| 44 |
+
assert_allclose(l, np.array([[1, 0, 0], [1/7, 1, 0], [4/7, 0.5, 1]]))
|
| 45 |
+
assert_allclose(u, np.array([[7, 8, 9], [0, 6/7, 12/7], [0, 0, 0]])*1j,
|
| 46 |
+
rtol=0., atol=1e-14)
|
| 47 |
+
|
| 48 |
+
# Rectangular matrices
|
| 49 |
+
hrect = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 12, 12]])
|
| 50 |
+
p, l, u = lu(hrect)
|
| 51 |
+
assert_allclose(p, np.array([[0, 1, 0], [0, 0, 1], [1, 0, 0]]))
|
| 52 |
+
assert_allclose(l, np.array([[1, 0, 0], [1/9, 1, 0], [5/9, 0.5, 1]]))
|
| 53 |
+
assert_allclose(u, np.array([[9, 10, 12, 12], [0, 8/9, 15/9, 24/9],
|
| 54 |
+
[0, 0, -0.5, 0]]), rtol=0., atol=1e-14)
|
| 55 |
+
|
| 56 |
+
chrect = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 12, 12]])*1.j
|
| 57 |
+
p, l, u = lu(chrect)
|
| 58 |
+
assert_allclose(p, np.array([[0, 1, 0], [0, 0, 1], [1, 0, 0]]))
|
| 59 |
+
assert_allclose(l, np.array([[1, 0, 0], [1/9, 1, 0], [5/9, 0.5, 1]]))
|
| 60 |
+
assert_allclose(u, np.array([[9, 10, 12, 12], [0, 8/9, 15/9, 24/9],
|
| 61 |
+
[0, 0, -0.5, 0]])*1j, rtol=0., atol=1e-14)
|
| 62 |
+
|
| 63 |
+
vrect = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 12, 12]])
|
| 64 |
+
p, l, u = lu(vrect)
|
| 65 |
+
assert_allclose(p, np.eye(4)[[1, 3, 2, 0], :])
|
| 66 |
+
assert_allclose(l, np.array([[1., 0, 0], [0.1, 1, 0], [0.7, -0.5, 1],
|
| 67 |
+
[0.4, 0.25, 0.5]]))
|
| 68 |
+
assert_allclose(u, np.array([[10, 12, 12],
|
| 69 |
+
[0, 0.8, 1.8],
|
| 70 |
+
[0, 0, 1.5]]))
|
| 71 |
+
|
| 72 |
+
cvrect = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 12, 12]])*1j
|
| 73 |
+
p, l, u = lu(cvrect)
|
| 74 |
+
assert_allclose(p, np.eye(4)[[1, 3, 2, 0], :])
|
| 75 |
+
assert_allclose(l, np.array([[1., 0, 0],
|
| 76 |
+
[0.1, 1, 0],
|
| 77 |
+
[0.7, -0.5, 1],
|
| 78 |
+
[0.4, 0.25, 0.5]]))
|
| 79 |
+
assert_allclose(u, np.array([[10, 12, 12],
|
| 80 |
+
[0, 0.8, 1.8],
|
| 81 |
+
[0, 0, 1.5]])*1j)
|
| 82 |
+
|
| 83 |
+
@pytest.mark.parametrize('shape', [[2, 2], [2, 4], [4, 2], [20, 20],
|
| 84 |
+
[20, 4], [4, 20], [3, 2, 9, 9],
|
| 85 |
+
[2, 2, 17, 5], [2, 2, 11, 7]])
|
| 86 |
+
def test_simple_lu_shapes_real_complex(self, shape):
|
| 87 |
+
a = self.rng.uniform(-10., 10., size=shape)
|
| 88 |
+
p, l, u = lu(a)
|
| 89 |
+
assert_allclose(a, p @ l @ u)
|
| 90 |
+
pl, u = lu(a, permute_l=True)
|
| 91 |
+
assert_allclose(a, pl @ u)
|
| 92 |
+
|
| 93 |
+
b = self.rng.uniform(-10., 10., size=shape)*1j
|
| 94 |
+
b += self.rng.uniform(-10, 10, size=shape)
|
| 95 |
+
pl, u = lu(b, permute_l=True)
|
| 96 |
+
assert_allclose(b, pl @ u)
|
| 97 |
+
|
| 98 |
+
@pytest.mark.parametrize('shape', [[2, 2], [2, 4], [4, 2], [20, 20],
|
| 99 |
+
[20, 4], [4, 20]])
|
| 100 |
+
def test_simple_lu_shapes_real_complex_2d_indices(self, shape):
|
| 101 |
+
a = self.rng.uniform(-10., 10., size=shape)
|
| 102 |
+
p, l, u = lu(a, p_indices=True)
|
| 103 |
+
assert_allclose(a, l[p, :] @ u)
|
| 104 |
+
|
| 105 |
+
def test_1by1_input_output(self):
|
| 106 |
+
a = self.rng.random([4, 5, 1, 1], dtype=np.float32)
|
| 107 |
+
p, l, u = lu(a, p_indices=True)
|
| 108 |
+
assert_allclose(p, np.zeros(shape=(4, 5, 1), dtype=int))
|
| 109 |
+
assert_allclose(l, np.ones(shape=(4, 5, 1, 1), dtype=np.float32))
|
| 110 |
+
assert_allclose(u, a)
|
| 111 |
+
|
| 112 |
+
a = self.rng.random([4, 5, 1, 1], dtype=np.float32)
|
| 113 |
+
p, l, u = lu(a)
|
| 114 |
+
assert_allclose(p, np.ones(shape=(4, 5, 1, 1), dtype=np.float32))
|
| 115 |
+
assert_allclose(l, np.ones(shape=(4, 5, 1, 1), dtype=np.float32))
|
| 116 |
+
assert_allclose(u, a)
|
| 117 |
+
|
| 118 |
+
pl, u = lu(a, permute_l=True)
|
| 119 |
+
assert_allclose(pl, np.ones(shape=(4, 5, 1, 1), dtype=np.float32))
|
| 120 |
+
assert_allclose(u, a)
|
| 121 |
+
|
| 122 |
+
a = self.rng.random([4, 5, 1, 1], dtype=np.float32)*np.complex64(1.j)
|
| 123 |
+
p, l, u = lu(a)
|
| 124 |
+
assert_allclose(p, np.ones(shape=(4, 5, 1, 1), dtype=np.complex64))
|
| 125 |
+
assert_allclose(l, np.ones(shape=(4, 5, 1, 1), dtype=np.complex64))
|
| 126 |
+
assert_allclose(u, a)
|
| 127 |
+
|
| 128 |
+
def test_empty_edge_cases(self):
|
| 129 |
+
a = np.empty([0, 0])
|
| 130 |
+
p, l, u = lu(a)
|
| 131 |
+
assert_allclose(p, np.empty(shape=(0, 0), dtype=np.float64))
|
| 132 |
+
assert_allclose(l, np.empty(shape=(0, 0), dtype=np.float64))
|
| 133 |
+
assert_allclose(u, np.empty(shape=(0, 0), dtype=np.float64))
|
| 134 |
+
|
| 135 |
+
a = np.empty([0, 3], dtype=np.float16)
|
| 136 |
+
p, l, u = lu(a)
|
| 137 |
+
assert_allclose(p, np.empty(shape=(0, 0), dtype=np.float32))
|
| 138 |
+
assert_allclose(l, np.empty(shape=(0, 0), dtype=np.float32))
|
| 139 |
+
assert_allclose(u, np.empty(shape=(0, 3), dtype=np.float32))
|
| 140 |
+
|
| 141 |
+
a = np.empty([3, 0], dtype=np.complex64)
|
| 142 |
+
p, l, u = lu(a)
|
| 143 |
+
assert_allclose(p, np.empty(shape=(0, 0), dtype=np.float32))
|
| 144 |
+
assert_allclose(l, np.empty(shape=(3, 0), dtype=np.complex64))
|
| 145 |
+
assert_allclose(u, np.empty(shape=(0, 0), dtype=np.complex64))
|
| 146 |
+
p, l, u = lu(a, p_indices=True)
|
| 147 |
+
assert_allclose(p, np.empty(shape=(0,), dtype=int))
|
| 148 |
+
assert_allclose(l, np.empty(shape=(3, 0), dtype=np.complex64))
|
| 149 |
+
assert_allclose(u, np.empty(shape=(0, 0), dtype=np.complex64))
|
| 150 |
+
pl, u = lu(a, permute_l=True)
|
| 151 |
+
assert_allclose(pl, np.empty(shape=(3, 0), dtype=np.complex64))
|
| 152 |
+
assert_allclose(u, np.empty(shape=(0, 0), dtype=np.complex64))
|
| 153 |
+
|
| 154 |
+
a = np.empty([3, 0, 0], dtype=np.complex64)
|
| 155 |
+
p, l, u = lu(a)
|
| 156 |
+
assert_allclose(p, np.empty(shape=(3, 0, 0), dtype=np.float32))
|
| 157 |
+
assert_allclose(l, np.empty(shape=(3, 0, 0), dtype=np.complex64))
|
| 158 |
+
assert_allclose(u, np.empty(shape=(3, 0, 0), dtype=np.complex64))
|
| 159 |
+
|
| 160 |
+
a = np.empty([0, 0, 3])
|
| 161 |
+
p, l, u = lu(a)
|
| 162 |
+
assert_allclose(p, np.empty(shape=(0, 0, 0)))
|
| 163 |
+
assert_allclose(l, np.empty(shape=(0, 0, 0)))
|
| 164 |
+
assert_allclose(u, np.empty(shape=(0, 0, 3)))
|
| 165 |
+
|
| 166 |
+
with assert_raises(ValueError, match='at least two-dimensional'):
|
| 167 |
+
lu(np.array([]))
|
| 168 |
+
|
| 169 |
+
a = np.array([[]])
|
| 170 |
+
p, l, u = lu(a)
|
| 171 |
+
assert_allclose(p, np.empty(shape=(0, 0)))
|
| 172 |
+
assert_allclose(l, np.empty(shape=(1, 0)))
|
| 173 |
+
assert_allclose(u, np.empty(shape=(0, 0)))
|
| 174 |
+
|
| 175 |
+
a = np.array([[[]]])
|
| 176 |
+
p, l, u = lu(a)
|
| 177 |
+
assert_allclose(p, np.empty(shape=(1, 0, 0)))
|
| 178 |
+
assert_allclose(l, np.empty(shape=(1, 1, 0)))
|
| 179 |
+
assert_allclose(u, np.empty(shape=(1, 0, 0)))
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
class TestLUFactor:
|
| 183 |
+
def setup_method(self):
|
| 184 |
+
self.rng = np.random.default_rng(1682281250228846)
|
| 185 |
+
|
| 186 |
+
self.a = np.array([[1, 2, 3], [1, 2, 3], [2, 5, 6]])
|
| 187 |
+
self.ca = np.array([[1, 2, 3], [1, 2, 3], [2, 5j, 6]])
|
| 188 |
+
# Those matrices are more robust to detect problems in permutation
|
| 189 |
+
# matrices than the ones above
|
| 190 |
+
self.b = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
|
| 191 |
+
self.cb = np.array([[1j, 2j, 3j], [4j, 5j, 6j], [7j, 8j, 9j]])
|
| 192 |
+
|
| 193 |
+
# Rectangular matrices
|
| 194 |
+
self.hrect = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 12, 12]])
|
| 195 |
+
self.chrect = np.array([[1, 2, 3, 4], [5, 6, 7, 8],
|
| 196 |
+
[9, 10, 12, 12]]) * 1.j
|
| 197 |
+
|
| 198 |
+
self.vrect = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 12, 12]])
|
| 199 |
+
self.cvrect = 1.j * np.array([[1, 2, 3],
|
| 200 |
+
[4, 5, 6],
|
| 201 |
+
[7, 8, 9],
|
| 202 |
+
[10, 12, 12]])
|
| 203 |
+
|
| 204 |
+
# Medium sizes matrices
|
| 205 |
+
self.med = self.rng.random((30, 40))
|
| 206 |
+
self.cmed = self.rng.random((30, 40)) + 1.j*self.rng.random((30, 40))
|
| 207 |
+
|
| 208 |
+
def _test_common_lu_factor(self, data):
|
| 209 |
+
l_and_u1, piv1 = lu_factor(data)
|
| 210 |
+
(getrf,) = get_lapack_funcs(("getrf",), (data,))
|
| 211 |
+
l_and_u2, piv2, _ = getrf(data, overwrite_a=False)
|
| 212 |
+
assert_allclose(l_and_u1, l_and_u2)
|
| 213 |
+
assert_allclose(piv1, piv2)
|
| 214 |
+
|
| 215 |
+
# Simple tests.
|
| 216 |
+
# For lu_factor gives a LinAlgWarning because these matrices are singular
|
| 217 |
+
def test_hrectangular(self):
|
| 218 |
+
self._test_common_lu_factor(self.hrect)
|
| 219 |
+
|
| 220 |
+
def test_vrectangular(self):
|
| 221 |
+
self._test_common_lu_factor(self.vrect)
|
| 222 |
+
|
| 223 |
+
def test_hrectangular_complex(self):
|
| 224 |
+
self._test_common_lu_factor(self.chrect)
|
| 225 |
+
|
| 226 |
+
def test_vrectangular_complex(self):
|
| 227 |
+
self._test_common_lu_factor(self.cvrect)
|
| 228 |
+
|
| 229 |
+
# Bigger matrices
|
| 230 |
+
def test_medium1(self):
|
| 231 |
+
"""Check lu decomposition on medium size, rectangular matrix."""
|
| 232 |
+
self._test_common_lu_factor(self.med)
|
| 233 |
+
|
| 234 |
+
def test_medium1_complex(self):
|
| 235 |
+
"""Check lu decomposition on medium size, rectangular matrix."""
|
| 236 |
+
self._test_common_lu_factor(self.cmed)
|
| 237 |
+
|
| 238 |
+
def test_check_finite(self):
|
| 239 |
+
p, l, u = lu(self.a, check_finite=False)
|
| 240 |
+
assert_allclose(p @ l @ u, self.a)
|
| 241 |
+
|
| 242 |
+
def test_simple_known(self):
|
| 243 |
+
# Ticket #1458
|
| 244 |
+
for order in ['C', 'F']:
|
| 245 |
+
A = np.array([[2, 1], [0, 1.]], order=order)
|
| 246 |
+
LU, P = lu_factor(A)
|
| 247 |
+
assert_allclose(LU, np.array([[2, 1], [0, 1]]))
|
| 248 |
+
assert_array_equal(P, np.array([0, 1]))
|
| 249 |
+
|
| 250 |
+
@pytest.mark.parametrize("m", [0, 1, 2])
|
| 251 |
+
@pytest.mark.parametrize("n", [0, 1, 2])
|
| 252 |
+
@pytest.mark.parametrize('dtype', DTYPES)
|
| 253 |
+
def test_shape_dtype(self, m, n, dtype):
|
| 254 |
+
k = min(m, n)
|
| 255 |
+
|
| 256 |
+
a = np.eye(m, n, dtype=dtype)
|
| 257 |
+
lu, p = lu_factor(a)
|
| 258 |
+
assert_equal(lu.shape, (m, n))
|
| 259 |
+
assert_equal(lu.dtype, dtype)
|
| 260 |
+
assert_equal(p.shape, (k,))
|
| 261 |
+
assert_equal(p.dtype, np.int32)
|
| 262 |
+
|
| 263 |
+
@pytest.mark.parametrize(("m", "n"), [(0, 0), (0, 2), (2, 0)])
|
| 264 |
+
def test_empty(self, m, n):
|
| 265 |
+
a = np.zeros((m, n))
|
| 266 |
+
lu, p = lu_factor(a)
|
| 267 |
+
assert_allclose(lu, np.empty((m, n)))
|
| 268 |
+
assert_allclose(p, np.arange(0))
|
| 269 |
+
|
| 270 |
+
|
| 271 |
+
class TestLUSolve:
|
| 272 |
+
def setup_method(self):
|
| 273 |
+
self.rng = np.random.default_rng(1682281250228846)
|
| 274 |
+
|
| 275 |
+
def test_lu(self):
|
| 276 |
+
a0 = self.rng.random((10, 10))
|
| 277 |
+
b = self.rng.random((10,))
|
| 278 |
+
|
| 279 |
+
for order in ['C', 'F']:
|
| 280 |
+
a = np.array(a0, order=order)
|
| 281 |
+
x1 = solve(a, b)
|
| 282 |
+
lu_a = lu_factor(a)
|
| 283 |
+
x2 = lu_solve(lu_a, b)
|
| 284 |
+
assert_allclose(x1, x2)
|
| 285 |
+
|
| 286 |
+
def test_check_finite(self):
|
| 287 |
+
a = self.rng.random((10, 10))
|
| 288 |
+
b = self.rng.random((10,))
|
| 289 |
+
x1 = solve(a, b)
|
| 290 |
+
lu_a = lu_factor(a, check_finite=False)
|
| 291 |
+
x2 = lu_solve(lu_a, b, check_finite=False)
|
| 292 |
+
assert_allclose(x1, x2)
|
| 293 |
+
|
| 294 |
+
@pytest.mark.parametrize('dt', [int, float, np.float32, complex, np.complex64])
|
| 295 |
+
@pytest.mark.parametrize('dt_b', [int, float, np.float32, complex, np.complex64])
|
| 296 |
+
def test_empty(self, dt, dt_b):
|
| 297 |
+
lu_and_piv = (np.empty((0, 0), dtype=dt), np.array([]))
|
| 298 |
+
b = np.asarray([], dtype=dt_b)
|
| 299 |
+
x = lu_solve(lu_and_piv, b)
|
| 300 |
+
assert x.shape == (0,)
|
| 301 |
+
|
| 302 |
+
m = lu_solve((np.eye(2, dtype=dt), [0, 1]), np.ones(2, dtype=dt_b))
|
| 303 |
+
assert x.dtype == m.dtype
|
| 304 |
+
|
| 305 |
+
b = np.empty((0, 0), dtype=dt_b)
|
| 306 |
+
x = lu_solve(lu_and_piv, b)
|
| 307 |
+
assert x.shape == (0, 0)
|
| 308 |
+
assert x.dtype == m.dtype
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_decomp_polar.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
import numpy as np
|
| 3 |
+
from numpy.linalg import norm
|
| 4 |
+
from numpy.testing import (assert_, assert_allclose, assert_equal)
|
| 5 |
+
from scipy.linalg import polar, eigh
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
diag2 = np.array([[2, 0], [0, 3]])
|
| 9 |
+
a13 = np.array([[1, 2, 2]])
|
| 10 |
+
|
| 11 |
+
precomputed_cases = [
|
| 12 |
+
[[[0]], 'right', [[1]], [[0]]],
|
| 13 |
+
[[[0]], 'left', [[1]], [[0]]],
|
| 14 |
+
[[[9]], 'right', [[1]], [[9]]],
|
| 15 |
+
[[[9]], 'left', [[1]], [[9]]],
|
| 16 |
+
[diag2, 'right', np.eye(2), diag2],
|
| 17 |
+
[diag2, 'left', np.eye(2), diag2],
|
| 18 |
+
[a13, 'right', a13/norm(a13[0]), a13.T.dot(a13)/norm(a13[0])],
|
| 19 |
+
]
|
| 20 |
+
|
| 21 |
+
verify_cases = [
|
| 22 |
+
[[1, 2], [3, 4]],
|
| 23 |
+
[[1, 2, 3]],
|
| 24 |
+
[[1], [2], [3]],
|
| 25 |
+
[[1, 2, 3], [3, 4, 0]],
|
| 26 |
+
[[1, 2], [3, 4], [5, 5]],
|
| 27 |
+
[[1, 2], [3, 4+5j]],
|
| 28 |
+
[[1, 2, 3j]],
|
| 29 |
+
[[1], [2], [3j]],
|
| 30 |
+
[[1, 2, 3+2j], [3, 4-1j, -4j]],
|
| 31 |
+
[[1, 2], [3-2j, 4+0.5j], [5, 5]],
|
| 32 |
+
[[10000, 10, 1], [-1, 2, 3j], [0, 1, 2]],
|
| 33 |
+
np.empty((0, 0)),
|
| 34 |
+
np.empty((0, 2)),
|
| 35 |
+
np.empty((2, 0)),
|
| 36 |
+
]
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def check_precomputed_polar(a, side, expected_u, expected_p):
|
| 40 |
+
# Compare the result of the polar decomposition to a
|
| 41 |
+
# precomputed result.
|
| 42 |
+
u, p = polar(a, side=side)
|
| 43 |
+
assert_allclose(u, expected_u, atol=1e-15)
|
| 44 |
+
assert_allclose(p, expected_p, atol=1e-15)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def verify_polar(a):
|
| 48 |
+
# Compute the polar decomposition, and then verify that
|
| 49 |
+
# the result has all the expected properties.
|
| 50 |
+
product_atol = np.sqrt(np.finfo(float).eps)
|
| 51 |
+
|
| 52 |
+
aa = np.asarray(a)
|
| 53 |
+
m, n = aa.shape
|
| 54 |
+
|
| 55 |
+
u, p = polar(a, side='right')
|
| 56 |
+
assert_equal(u.shape, (m, n))
|
| 57 |
+
assert_equal(p.shape, (n, n))
|
| 58 |
+
# a = up
|
| 59 |
+
assert_allclose(u.dot(p), a, atol=product_atol)
|
| 60 |
+
if m >= n:
|
| 61 |
+
assert_allclose(u.conj().T.dot(u), np.eye(n), atol=1e-15)
|
| 62 |
+
else:
|
| 63 |
+
assert_allclose(u.dot(u.conj().T), np.eye(m), atol=1e-15)
|
| 64 |
+
# p is Hermitian positive semidefinite.
|
| 65 |
+
assert_allclose(p.conj().T, p)
|
| 66 |
+
evals = eigh(p, eigvals_only=True)
|
| 67 |
+
nonzero_evals = evals[abs(evals) > 1e-14]
|
| 68 |
+
assert_((nonzero_evals >= 0).all())
|
| 69 |
+
|
| 70 |
+
u, p = polar(a, side='left')
|
| 71 |
+
assert_equal(u.shape, (m, n))
|
| 72 |
+
assert_equal(p.shape, (m, m))
|
| 73 |
+
# a = pu
|
| 74 |
+
assert_allclose(p.dot(u), a, atol=product_atol)
|
| 75 |
+
if m >= n:
|
| 76 |
+
assert_allclose(u.conj().T.dot(u), np.eye(n), atol=1e-15)
|
| 77 |
+
else:
|
| 78 |
+
assert_allclose(u.dot(u.conj().T), np.eye(m), atol=1e-15)
|
| 79 |
+
# p is Hermitian positive semidefinite.
|
| 80 |
+
assert_allclose(p.conj().T, p)
|
| 81 |
+
evals = eigh(p, eigvals_only=True)
|
| 82 |
+
nonzero_evals = evals[abs(evals) > 1e-14]
|
| 83 |
+
assert_((nonzero_evals >= 0).all())
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def test_precomputed_cases():
|
| 87 |
+
for a, side, expected_u, expected_p in precomputed_cases:
|
| 88 |
+
check_precomputed_polar(a, side, expected_u, expected_p)
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def test_verify_cases():
|
| 92 |
+
for a in verify_cases:
|
| 93 |
+
verify_polar(a)
|
| 94 |
+
|
| 95 |
+
@pytest.mark.parametrize('dt', [int, float, np.float32, complex, np.complex64])
|
| 96 |
+
@pytest.mark.parametrize('shape', [(0, 0), (0, 2), (2, 0)])
|
| 97 |
+
@pytest.mark.parametrize('side', ['left', 'right'])
|
| 98 |
+
def test_empty(dt, shape, side):
|
| 99 |
+
a = np.empty(shape, dtype=dt)
|
| 100 |
+
m, n = shape
|
| 101 |
+
p_shape = (m, m) if side == 'left' else (n, n)
|
| 102 |
+
|
| 103 |
+
u, p = polar(a, side=side)
|
| 104 |
+
u_n, p_n = polar(np.eye(5, dtype=dt))
|
| 105 |
+
|
| 106 |
+
assert_equal(u.dtype, u_n.dtype)
|
| 107 |
+
assert_equal(p.dtype, p_n.dtype)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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import itertools
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
from numpy.testing import assert_, assert_allclose, assert_equal
|
| 5 |
+
from pytest import raises as assert_raises
|
| 6 |
+
from scipy import linalg
|
| 7 |
+
import scipy.linalg._decomp_update as _decomp_update
|
| 8 |
+
from scipy.linalg._decomp_update import qr_delete, qr_update, qr_insert
|
| 9 |
+
|
| 10 |
+
def assert_unitary(a, rtol=None, atol=None, assert_sqr=True):
|
| 11 |
+
if rtol is None:
|
| 12 |
+
rtol = 10.0 ** -(np.finfo(a.dtype).precision-2)
|
| 13 |
+
if atol is None:
|
| 14 |
+
atol = 10*np.finfo(a.dtype).eps
|
| 15 |
+
|
| 16 |
+
if assert_sqr:
|
| 17 |
+
assert_(a.shape[0] == a.shape[1], 'unitary matrices must be square')
|
| 18 |
+
aTa = np.dot(a.T.conj(), a)
|
| 19 |
+
assert_allclose(aTa, np.eye(a.shape[1]), rtol=rtol, atol=atol)
|
| 20 |
+
|
| 21 |
+
def assert_upper_tri(a, rtol=None, atol=None):
|
| 22 |
+
if rtol is None:
|
| 23 |
+
rtol = 10.0 ** -(np.finfo(a.dtype).precision-2)
|
| 24 |
+
if atol is None:
|
| 25 |
+
atol = 2*np.finfo(a.dtype).eps
|
| 26 |
+
mask = np.tri(a.shape[0], a.shape[1], -1, np.bool_)
|
| 27 |
+
assert_allclose(a[mask], 0.0, rtol=rtol, atol=atol)
|
| 28 |
+
|
| 29 |
+
def check_qr(q, r, a, rtol, atol, assert_sqr=True):
|
| 30 |
+
assert_unitary(q, rtol, atol, assert_sqr)
|
| 31 |
+
assert_upper_tri(r, rtol, atol)
|
| 32 |
+
assert_allclose(q.dot(r), a, rtol=rtol, atol=atol)
|
| 33 |
+
|
| 34 |
+
def make_strided(arrs):
|
| 35 |
+
strides = [(3, 7), (2, 2), (3, 4), (4, 2), (5, 4), (2, 3), (2, 1), (4, 5)]
|
| 36 |
+
kmax = len(strides)
|
| 37 |
+
k = 0
|
| 38 |
+
ret = []
|
| 39 |
+
for a in arrs:
|
| 40 |
+
if a.ndim == 1:
|
| 41 |
+
s = strides[k % kmax]
|
| 42 |
+
k += 1
|
| 43 |
+
base = np.zeros(s[0]*a.shape[0]+s[1], a.dtype)
|
| 44 |
+
view = base[s[1]::s[0]]
|
| 45 |
+
view[...] = a
|
| 46 |
+
elif a.ndim == 2:
|
| 47 |
+
s = strides[k % kmax]
|
| 48 |
+
t = strides[(k+1) % kmax]
|
| 49 |
+
k += 2
|
| 50 |
+
base = np.zeros((s[0]*a.shape[0]+s[1], t[0]*a.shape[1]+t[1]),
|
| 51 |
+
a.dtype)
|
| 52 |
+
view = base[s[1]::s[0], t[1]::t[0]]
|
| 53 |
+
view[...] = a
|
| 54 |
+
else:
|
| 55 |
+
raise ValueError('make_strided only works for ndim = 1 or'
|
| 56 |
+
' 2 arrays')
|
| 57 |
+
ret.append(view)
|
| 58 |
+
return ret
|
| 59 |
+
|
| 60 |
+
def negate_strides(arrs):
|
| 61 |
+
ret = []
|
| 62 |
+
for a in arrs:
|
| 63 |
+
b = np.zeros_like(a)
|
| 64 |
+
if b.ndim == 2:
|
| 65 |
+
b = b[::-1, ::-1]
|
| 66 |
+
elif b.ndim == 1:
|
| 67 |
+
b = b[::-1]
|
| 68 |
+
else:
|
| 69 |
+
raise ValueError('negate_strides only works for ndim = 1 or'
|
| 70 |
+
' 2 arrays')
|
| 71 |
+
b[...] = a
|
| 72 |
+
ret.append(b)
|
| 73 |
+
return ret
|
| 74 |
+
|
| 75 |
+
def nonitemsize_strides(arrs):
|
| 76 |
+
out = []
|
| 77 |
+
for a in arrs:
|
| 78 |
+
a_dtype = a.dtype
|
| 79 |
+
b = np.zeros(a.shape, [('a', a_dtype), ('junk', 'S1')])
|
| 80 |
+
c = b.getfield(a_dtype)
|
| 81 |
+
c[...] = a
|
| 82 |
+
out.append(c)
|
| 83 |
+
return out
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def make_nonnative(arrs):
|
| 87 |
+
return [a.astype(a.dtype.newbyteorder()) for a in arrs]
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
class BaseQRdeltas:
|
| 91 |
+
def setup_method(self):
|
| 92 |
+
self.rtol = 10.0 ** -(np.finfo(self.dtype).precision-2)
|
| 93 |
+
self.atol = 10 * np.finfo(self.dtype).eps
|
| 94 |
+
|
| 95 |
+
def generate(self, type, mode='full'):
|
| 96 |
+
np.random.seed(29382)
|
| 97 |
+
shape = {'sqr': (8, 8), 'tall': (12, 7), 'fat': (7, 12),
|
| 98 |
+
'Mx1': (8, 1), '1xN': (1, 8), '1x1': (1, 1)}[type]
|
| 99 |
+
a = np.random.random(shape)
|
| 100 |
+
if np.iscomplexobj(self.dtype.type(1)):
|
| 101 |
+
b = np.random.random(shape)
|
| 102 |
+
a = a + 1j * b
|
| 103 |
+
a = a.astype(self.dtype)
|
| 104 |
+
q, r = linalg.qr(a, mode=mode)
|
| 105 |
+
return a, q, r
|
| 106 |
+
|
| 107 |
+
class BaseQRdelete(BaseQRdeltas):
|
| 108 |
+
def test_sqr_1_row(self):
|
| 109 |
+
a, q, r = self.generate('sqr')
|
| 110 |
+
for row in range(r.shape[0]):
|
| 111 |
+
q1, r1 = qr_delete(q, r, row, overwrite_qr=False)
|
| 112 |
+
a1 = np.delete(a, row, 0)
|
| 113 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 114 |
+
|
| 115 |
+
def test_sqr_p_row(self):
|
| 116 |
+
a, q, r = self.generate('sqr')
|
| 117 |
+
for ndel in range(2, 6):
|
| 118 |
+
for row in range(a.shape[0]-ndel):
|
| 119 |
+
q1, r1 = qr_delete(q, r, row, ndel, overwrite_qr=False)
|
| 120 |
+
a1 = np.delete(a, slice(row, row+ndel), 0)
|
| 121 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 122 |
+
|
| 123 |
+
def test_sqr_1_col(self):
|
| 124 |
+
a, q, r = self.generate('sqr')
|
| 125 |
+
for col in range(r.shape[1]):
|
| 126 |
+
q1, r1 = qr_delete(q, r, col, which='col', overwrite_qr=False)
|
| 127 |
+
a1 = np.delete(a, col, 1)
|
| 128 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 129 |
+
|
| 130 |
+
def test_sqr_p_col(self):
|
| 131 |
+
a, q, r = self.generate('sqr')
|
| 132 |
+
for ndel in range(2, 6):
|
| 133 |
+
for col in range(r.shape[1]-ndel):
|
| 134 |
+
q1, r1 = qr_delete(q, r, col, ndel, which='col',
|
| 135 |
+
overwrite_qr=False)
|
| 136 |
+
a1 = np.delete(a, slice(col, col+ndel), 1)
|
| 137 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 138 |
+
|
| 139 |
+
def test_tall_1_row(self):
|
| 140 |
+
a, q, r = self.generate('tall')
|
| 141 |
+
for row in range(r.shape[0]):
|
| 142 |
+
q1, r1 = qr_delete(q, r, row, overwrite_qr=False)
|
| 143 |
+
a1 = np.delete(a, row, 0)
|
| 144 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 145 |
+
|
| 146 |
+
def test_tall_p_row(self):
|
| 147 |
+
a, q, r = self.generate('tall')
|
| 148 |
+
for ndel in range(2, 6):
|
| 149 |
+
for row in range(a.shape[0]-ndel):
|
| 150 |
+
q1, r1 = qr_delete(q, r, row, ndel, overwrite_qr=False)
|
| 151 |
+
a1 = np.delete(a, slice(row, row+ndel), 0)
|
| 152 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 153 |
+
|
| 154 |
+
def test_tall_1_col(self):
|
| 155 |
+
a, q, r = self.generate('tall')
|
| 156 |
+
for col in range(r.shape[1]):
|
| 157 |
+
q1, r1 = qr_delete(q, r, col, which='col', overwrite_qr=False)
|
| 158 |
+
a1 = np.delete(a, col, 1)
|
| 159 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 160 |
+
|
| 161 |
+
def test_tall_p_col(self):
|
| 162 |
+
a, q, r = self.generate('tall')
|
| 163 |
+
for ndel in range(2, 6):
|
| 164 |
+
for col in range(r.shape[1]-ndel):
|
| 165 |
+
q1, r1 = qr_delete(q, r, col, ndel, which='col',
|
| 166 |
+
overwrite_qr=False)
|
| 167 |
+
a1 = np.delete(a, slice(col, col+ndel), 1)
|
| 168 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 169 |
+
|
| 170 |
+
def test_fat_1_row(self):
|
| 171 |
+
a, q, r = self.generate('fat')
|
| 172 |
+
for row in range(r.shape[0]):
|
| 173 |
+
q1, r1 = qr_delete(q, r, row, overwrite_qr=False)
|
| 174 |
+
a1 = np.delete(a, row, 0)
|
| 175 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 176 |
+
|
| 177 |
+
def test_fat_p_row(self):
|
| 178 |
+
a, q, r = self.generate('fat')
|
| 179 |
+
for ndel in range(2, 6):
|
| 180 |
+
for row in range(a.shape[0]-ndel):
|
| 181 |
+
q1, r1 = qr_delete(q, r, row, ndel, overwrite_qr=False)
|
| 182 |
+
a1 = np.delete(a, slice(row, row+ndel), 0)
|
| 183 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 184 |
+
|
| 185 |
+
def test_fat_1_col(self):
|
| 186 |
+
a, q, r = self.generate('fat')
|
| 187 |
+
for col in range(r.shape[1]):
|
| 188 |
+
q1, r1 = qr_delete(q, r, col, which='col', overwrite_qr=False)
|
| 189 |
+
a1 = np.delete(a, col, 1)
|
| 190 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 191 |
+
|
| 192 |
+
def test_fat_p_col(self):
|
| 193 |
+
a, q, r = self.generate('fat')
|
| 194 |
+
for ndel in range(2, 6):
|
| 195 |
+
for col in range(r.shape[1]-ndel):
|
| 196 |
+
q1, r1 = qr_delete(q, r, col, ndel, which='col',
|
| 197 |
+
overwrite_qr=False)
|
| 198 |
+
a1 = np.delete(a, slice(col, col+ndel), 1)
|
| 199 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 200 |
+
|
| 201 |
+
def test_economic_1_row(self):
|
| 202 |
+
# this test always starts and ends with an economic decomp.
|
| 203 |
+
a, q, r = self.generate('tall', 'economic')
|
| 204 |
+
for row in range(r.shape[0]):
|
| 205 |
+
q1, r1 = qr_delete(q, r, row, overwrite_qr=False)
|
| 206 |
+
a1 = np.delete(a, row, 0)
|
| 207 |
+
check_qr(q1, r1, a1, self.rtol, self.atol, False)
|
| 208 |
+
|
| 209 |
+
# for economic row deletes
|
| 210 |
+
# eco - prow = eco
|
| 211 |
+
# eco - prow = sqr
|
| 212 |
+
# eco - prow = fat
|
| 213 |
+
def base_economic_p_row_xxx(self, ndel):
|
| 214 |
+
a, q, r = self.generate('tall', 'economic')
|
| 215 |
+
for row in range(a.shape[0]-ndel):
|
| 216 |
+
q1, r1 = qr_delete(q, r, row, ndel, overwrite_qr=False)
|
| 217 |
+
a1 = np.delete(a, slice(row, row+ndel), 0)
|
| 218 |
+
check_qr(q1, r1, a1, self.rtol, self.atol, False)
|
| 219 |
+
|
| 220 |
+
def test_economic_p_row_economic(self):
|
| 221 |
+
# (12, 7) - (3, 7) = (9,7) --> stays economic
|
| 222 |
+
self.base_economic_p_row_xxx(3)
|
| 223 |
+
|
| 224 |
+
def test_economic_p_row_sqr(self):
|
| 225 |
+
# (12, 7) - (5, 7) = (7, 7) --> becomes square
|
| 226 |
+
self.base_economic_p_row_xxx(5)
|
| 227 |
+
|
| 228 |
+
def test_economic_p_row_fat(self):
|
| 229 |
+
# (12, 7) - (7,7) = (5, 7) --> becomes fat
|
| 230 |
+
self.base_economic_p_row_xxx(7)
|
| 231 |
+
|
| 232 |
+
def test_economic_1_col(self):
|
| 233 |
+
a, q, r = self.generate('tall', 'economic')
|
| 234 |
+
for col in range(r.shape[1]):
|
| 235 |
+
q1, r1 = qr_delete(q, r, col, which='col', overwrite_qr=False)
|
| 236 |
+
a1 = np.delete(a, col, 1)
|
| 237 |
+
check_qr(q1, r1, a1, self.rtol, self.atol, False)
|
| 238 |
+
|
| 239 |
+
def test_economic_p_col(self):
|
| 240 |
+
a, q, r = self.generate('tall', 'economic')
|
| 241 |
+
for ndel in range(2, 6):
|
| 242 |
+
for col in range(r.shape[1]-ndel):
|
| 243 |
+
q1, r1 = qr_delete(q, r, col, ndel, which='col',
|
| 244 |
+
overwrite_qr=False)
|
| 245 |
+
a1 = np.delete(a, slice(col, col+ndel), 1)
|
| 246 |
+
check_qr(q1, r1, a1, self.rtol, self.atol, False)
|
| 247 |
+
|
| 248 |
+
def test_Mx1_1_row(self):
|
| 249 |
+
a, q, r = self.generate('Mx1')
|
| 250 |
+
for row in range(r.shape[0]):
|
| 251 |
+
q1, r1 = qr_delete(q, r, row, overwrite_qr=False)
|
| 252 |
+
a1 = np.delete(a, row, 0)
|
| 253 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 254 |
+
|
| 255 |
+
def test_Mx1_p_row(self):
|
| 256 |
+
a, q, r = self.generate('Mx1')
|
| 257 |
+
for ndel in range(2, 6):
|
| 258 |
+
for row in range(a.shape[0]-ndel):
|
| 259 |
+
q1, r1 = qr_delete(q, r, row, ndel, overwrite_qr=False)
|
| 260 |
+
a1 = np.delete(a, slice(row, row+ndel), 0)
|
| 261 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 262 |
+
|
| 263 |
+
def test_1xN_1_col(self):
|
| 264 |
+
a, q, r = self.generate('1xN')
|
| 265 |
+
for col in range(r.shape[1]):
|
| 266 |
+
q1, r1 = qr_delete(q, r, col, which='col', overwrite_qr=False)
|
| 267 |
+
a1 = np.delete(a, col, 1)
|
| 268 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 269 |
+
|
| 270 |
+
def test_1xN_p_col(self):
|
| 271 |
+
a, q, r = self.generate('1xN')
|
| 272 |
+
for ndel in range(2, 6):
|
| 273 |
+
for col in range(r.shape[1]-ndel):
|
| 274 |
+
q1, r1 = qr_delete(q, r, col, ndel, which='col',
|
| 275 |
+
overwrite_qr=False)
|
| 276 |
+
a1 = np.delete(a, slice(col, col+ndel), 1)
|
| 277 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 278 |
+
|
| 279 |
+
def test_Mx1_economic_1_row(self):
|
| 280 |
+
a, q, r = self.generate('Mx1', 'economic')
|
| 281 |
+
for row in range(r.shape[0]):
|
| 282 |
+
q1, r1 = qr_delete(q, r, row, overwrite_qr=False)
|
| 283 |
+
a1 = np.delete(a, row, 0)
|
| 284 |
+
check_qr(q1, r1, a1, self.rtol, self.atol, False)
|
| 285 |
+
|
| 286 |
+
def test_Mx1_economic_p_row(self):
|
| 287 |
+
a, q, r = self.generate('Mx1', 'economic')
|
| 288 |
+
for ndel in range(2, 6):
|
| 289 |
+
for row in range(a.shape[0]-ndel):
|
| 290 |
+
q1, r1 = qr_delete(q, r, row, ndel, overwrite_qr=False)
|
| 291 |
+
a1 = np.delete(a, slice(row, row+ndel), 0)
|
| 292 |
+
check_qr(q1, r1, a1, self.rtol, self.atol, False)
|
| 293 |
+
|
| 294 |
+
def test_delete_last_1_row(self):
|
| 295 |
+
# full and eco are the same for 1xN
|
| 296 |
+
a, q, r = self.generate('1xN')
|
| 297 |
+
q1, r1 = qr_delete(q, r, 0, 1, 'row')
|
| 298 |
+
assert_equal(q1, np.ndarray(shape=(0, 0), dtype=q.dtype))
|
| 299 |
+
assert_equal(r1, np.ndarray(shape=(0, r.shape[1]), dtype=r.dtype))
|
| 300 |
+
|
| 301 |
+
def test_delete_last_p_row(self):
|
| 302 |
+
a, q, r = self.generate('tall', 'full')
|
| 303 |
+
q1, r1 = qr_delete(q, r, 0, a.shape[0], 'row')
|
| 304 |
+
assert_equal(q1, np.ndarray(shape=(0, 0), dtype=q.dtype))
|
| 305 |
+
assert_equal(r1, np.ndarray(shape=(0, r.shape[1]), dtype=r.dtype))
|
| 306 |
+
|
| 307 |
+
a, q, r = self.generate('tall', 'economic')
|
| 308 |
+
q1, r1 = qr_delete(q, r, 0, a.shape[0], 'row')
|
| 309 |
+
assert_equal(q1, np.ndarray(shape=(0, 0), dtype=q.dtype))
|
| 310 |
+
assert_equal(r1, np.ndarray(shape=(0, r.shape[1]), dtype=r.dtype))
|
| 311 |
+
|
| 312 |
+
def test_delete_last_1_col(self):
|
| 313 |
+
a, q, r = self.generate('Mx1', 'economic')
|
| 314 |
+
q1, r1 = qr_delete(q, r, 0, 1, 'col')
|
| 315 |
+
assert_equal(q1, np.ndarray(shape=(q.shape[0], 0), dtype=q.dtype))
|
| 316 |
+
assert_equal(r1, np.ndarray(shape=(0, 0), dtype=r.dtype))
|
| 317 |
+
|
| 318 |
+
a, q, r = self.generate('Mx1', 'full')
|
| 319 |
+
q1, r1 = qr_delete(q, r, 0, 1, 'col')
|
| 320 |
+
assert_unitary(q1)
|
| 321 |
+
assert_(q1.dtype == q.dtype)
|
| 322 |
+
assert_(q1.shape == q.shape)
|
| 323 |
+
assert_equal(r1, np.ndarray(shape=(r.shape[0], 0), dtype=r.dtype))
|
| 324 |
+
|
| 325 |
+
def test_delete_last_p_col(self):
|
| 326 |
+
a, q, r = self.generate('tall', 'full')
|
| 327 |
+
q1, r1 = qr_delete(q, r, 0, a.shape[1], 'col')
|
| 328 |
+
assert_unitary(q1)
|
| 329 |
+
assert_(q1.dtype == q.dtype)
|
| 330 |
+
assert_(q1.shape == q.shape)
|
| 331 |
+
assert_equal(r1, np.ndarray(shape=(r.shape[0], 0), dtype=r.dtype))
|
| 332 |
+
|
| 333 |
+
a, q, r = self.generate('tall', 'economic')
|
| 334 |
+
q1, r1 = qr_delete(q, r, 0, a.shape[1], 'col')
|
| 335 |
+
assert_equal(q1, np.ndarray(shape=(q.shape[0], 0), dtype=q.dtype))
|
| 336 |
+
assert_equal(r1, np.ndarray(shape=(0, 0), dtype=r.dtype))
|
| 337 |
+
|
| 338 |
+
def test_delete_1x1_row_col(self):
|
| 339 |
+
a, q, r = self.generate('1x1')
|
| 340 |
+
q1, r1 = qr_delete(q, r, 0, 1, 'row')
|
| 341 |
+
assert_equal(q1, np.ndarray(shape=(0, 0), dtype=q.dtype))
|
| 342 |
+
assert_equal(r1, np.ndarray(shape=(0, r.shape[1]), dtype=r.dtype))
|
| 343 |
+
|
| 344 |
+
a, q, r = self.generate('1x1')
|
| 345 |
+
q1, r1 = qr_delete(q, r, 0, 1, 'col')
|
| 346 |
+
assert_unitary(q1)
|
| 347 |
+
assert_(q1.dtype == q.dtype)
|
| 348 |
+
assert_(q1.shape == q.shape)
|
| 349 |
+
assert_equal(r1, np.ndarray(shape=(r.shape[0], 0), dtype=r.dtype))
|
| 350 |
+
|
| 351 |
+
# all full qr, row deletes and single column deletes should be able to
|
| 352 |
+
# handle any non negative strides. (only row and column vector
|
| 353 |
+
# operations are used.) p column delete require fortran ordered
|
| 354 |
+
# Q and R and will make a copy as necessary. Economic qr row deletes
|
| 355 |
+
# require a contiguous q.
|
| 356 |
+
|
| 357 |
+
def base_non_simple_strides(self, adjust_strides, ks, p, which,
|
| 358 |
+
overwriteable):
|
| 359 |
+
if which == 'row':
|
| 360 |
+
qind = (slice(p,None), slice(p,None))
|
| 361 |
+
rind = (slice(p,None), slice(None))
|
| 362 |
+
else:
|
| 363 |
+
qind = (slice(None), slice(None))
|
| 364 |
+
rind = (slice(None), slice(None,-p))
|
| 365 |
+
|
| 366 |
+
for type, k in itertools.product(['sqr', 'tall', 'fat'], ks):
|
| 367 |
+
a, q0, r0, = self.generate(type)
|
| 368 |
+
qs, rs = adjust_strides((q0, r0))
|
| 369 |
+
if p == 1:
|
| 370 |
+
a1 = np.delete(a, k, 0 if which == 'row' else 1)
|
| 371 |
+
else:
|
| 372 |
+
s = slice(k,k+p)
|
| 373 |
+
if k < 0:
|
| 374 |
+
s = slice(k, k + p +
|
| 375 |
+
(a.shape[0] if which == 'row' else a.shape[1]))
|
| 376 |
+
a1 = np.delete(a, s, 0 if which == 'row' else 1)
|
| 377 |
+
|
| 378 |
+
# for each variable, q, r we try with it strided and
|
| 379 |
+
# overwrite=False. Then we try with overwrite=True, and make
|
| 380 |
+
# sure that q and r are still overwritten.
|
| 381 |
+
|
| 382 |
+
q = q0.copy('F')
|
| 383 |
+
r = r0.copy('F')
|
| 384 |
+
q1, r1 = qr_delete(qs, r, k, p, which, False)
|
| 385 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 386 |
+
q1o, r1o = qr_delete(qs, r, k, p, which, True)
|
| 387 |
+
check_qr(q1o, r1o, a1, self.rtol, self.atol)
|
| 388 |
+
if overwriteable:
|
| 389 |
+
assert_allclose(q1o, qs[qind], rtol=self.rtol, atol=self.atol)
|
| 390 |
+
assert_allclose(r1o, r[rind], rtol=self.rtol, atol=self.atol)
|
| 391 |
+
|
| 392 |
+
q = q0.copy('F')
|
| 393 |
+
r = r0.copy('F')
|
| 394 |
+
q2, r2 = qr_delete(q, rs, k, p, which, False)
|
| 395 |
+
check_qr(q2, r2, a1, self.rtol, self.atol)
|
| 396 |
+
q2o, r2o = qr_delete(q, rs, k, p, which, True)
|
| 397 |
+
check_qr(q2o, r2o, a1, self.rtol, self.atol)
|
| 398 |
+
if overwriteable:
|
| 399 |
+
assert_allclose(q2o, q[qind], rtol=self.rtol, atol=self.atol)
|
| 400 |
+
assert_allclose(r2o, rs[rind], rtol=self.rtol, atol=self.atol)
|
| 401 |
+
|
| 402 |
+
q = q0.copy('F')
|
| 403 |
+
r = r0.copy('F')
|
| 404 |
+
# since some of these were consumed above
|
| 405 |
+
qs, rs = adjust_strides((q, r))
|
| 406 |
+
q3, r3 = qr_delete(qs, rs, k, p, which, False)
|
| 407 |
+
check_qr(q3, r3, a1, self.rtol, self.atol)
|
| 408 |
+
q3o, r3o = qr_delete(qs, rs, k, p, which, True)
|
| 409 |
+
check_qr(q3o, r3o, a1, self.rtol, self.atol)
|
| 410 |
+
if overwriteable:
|
| 411 |
+
assert_allclose(q2o, qs[qind], rtol=self.rtol, atol=self.atol)
|
| 412 |
+
assert_allclose(r3o, rs[rind], rtol=self.rtol, atol=self.atol)
|
| 413 |
+
|
| 414 |
+
def test_non_unit_strides_1_row(self):
|
| 415 |
+
self.base_non_simple_strides(make_strided, [0], 1, 'row', True)
|
| 416 |
+
|
| 417 |
+
def test_non_unit_strides_p_row(self):
|
| 418 |
+
self.base_non_simple_strides(make_strided, [0], 3, 'row', True)
|
| 419 |
+
|
| 420 |
+
def test_non_unit_strides_1_col(self):
|
| 421 |
+
self.base_non_simple_strides(make_strided, [0], 1, 'col', True)
|
| 422 |
+
|
| 423 |
+
def test_non_unit_strides_p_col(self):
|
| 424 |
+
self.base_non_simple_strides(make_strided, [0], 3, 'col', False)
|
| 425 |
+
|
| 426 |
+
def test_neg_strides_1_row(self):
|
| 427 |
+
self.base_non_simple_strides(negate_strides, [0], 1, 'row', False)
|
| 428 |
+
|
| 429 |
+
def test_neg_strides_p_row(self):
|
| 430 |
+
self.base_non_simple_strides(negate_strides, [0], 3, 'row', False)
|
| 431 |
+
|
| 432 |
+
def test_neg_strides_1_col(self):
|
| 433 |
+
self.base_non_simple_strides(negate_strides, [0], 1, 'col', False)
|
| 434 |
+
|
| 435 |
+
def test_neg_strides_p_col(self):
|
| 436 |
+
self.base_non_simple_strides(negate_strides, [0], 3, 'col', False)
|
| 437 |
+
|
| 438 |
+
def test_non_itemize_strides_1_row(self):
|
| 439 |
+
self.base_non_simple_strides(nonitemsize_strides, [0], 1, 'row', False)
|
| 440 |
+
|
| 441 |
+
def test_non_itemize_strides_p_row(self):
|
| 442 |
+
self.base_non_simple_strides(nonitemsize_strides, [0], 3, 'row', False)
|
| 443 |
+
|
| 444 |
+
def test_non_itemize_strides_1_col(self):
|
| 445 |
+
self.base_non_simple_strides(nonitemsize_strides, [0], 1, 'col', False)
|
| 446 |
+
|
| 447 |
+
def test_non_itemize_strides_p_col(self):
|
| 448 |
+
self.base_non_simple_strides(nonitemsize_strides, [0], 3, 'col', False)
|
| 449 |
+
|
| 450 |
+
def test_non_native_byte_order_1_row(self):
|
| 451 |
+
self.base_non_simple_strides(make_nonnative, [0], 1, 'row', False)
|
| 452 |
+
|
| 453 |
+
def test_non_native_byte_order_p_row(self):
|
| 454 |
+
self.base_non_simple_strides(make_nonnative, [0], 3, 'row', False)
|
| 455 |
+
|
| 456 |
+
def test_non_native_byte_order_1_col(self):
|
| 457 |
+
self.base_non_simple_strides(make_nonnative, [0], 1, 'col', False)
|
| 458 |
+
|
| 459 |
+
def test_non_native_byte_order_p_col(self):
|
| 460 |
+
self.base_non_simple_strides(make_nonnative, [0], 3, 'col', False)
|
| 461 |
+
|
| 462 |
+
def test_neg_k(self):
|
| 463 |
+
a, q, r = self.generate('sqr')
|
| 464 |
+
for k, p, w in itertools.product([-3, -7], [1, 3], ['row', 'col']):
|
| 465 |
+
q1, r1 = qr_delete(q, r, k, p, w, overwrite_qr=False)
|
| 466 |
+
if w == 'row':
|
| 467 |
+
a1 = np.delete(a, slice(k+a.shape[0], k+p+a.shape[0]), 0)
|
| 468 |
+
else:
|
| 469 |
+
a1 = np.delete(a, slice(k+a.shape[0], k+p+a.shape[1]), 1)
|
| 470 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 471 |
+
|
| 472 |
+
def base_overwrite_qr(self, which, p, test_C, test_F, mode='full'):
|
| 473 |
+
assert_sqr = True if mode == 'full' else False
|
| 474 |
+
if which == 'row':
|
| 475 |
+
qind = (slice(p,None), slice(p,None))
|
| 476 |
+
rind = (slice(p,None), slice(None))
|
| 477 |
+
else:
|
| 478 |
+
qind = (slice(None), slice(None))
|
| 479 |
+
rind = (slice(None), slice(None,-p))
|
| 480 |
+
a, q0, r0 = self.generate('sqr', mode)
|
| 481 |
+
if p == 1:
|
| 482 |
+
a1 = np.delete(a, 3, 0 if which == 'row' else 1)
|
| 483 |
+
else:
|
| 484 |
+
a1 = np.delete(a, slice(3, 3+p), 0 if which == 'row' else 1)
|
| 485 |
+
|
| 486 |
+
# don't overwrite
|
| 487 |
+
q = q0.copy('F')
|
| 488 |
+
r = r0.copy('F')
|
| 489 |
+
q1, r1 = qr_delete(q, r, 3, p, which, False)
|
| 490 |
+
check_qr(q1, r1, a1, self.rtol, self.atol, assert_sqr)
|
| 491 |
+
check_qr(q, r, a, self.rtol, self.atol, assert_sqr)
|
| 492 |
+
|
| 493 |
+
if test_F:
|
| 494 |
+
q = q0.copy('F')
|
| 495 |
+
r = r0.copy('F')
|
| 496 |
+
q2, r2 = qr_delete(q, r, 3, p, which, True)
|
| 497 |
+
check_qr(q2, r2, a1, self.rtol, self.atol, assert_sqr)
|
| 498 |
+
# verify the overwriting
|
| 499 |
+
assert_allclose(q2, q[qind], rtol=self.rtol, atol=self.atol)
|
| 500 |
+
assert_allclose(r2, r[rind], rtol=self.rtol, atol=self.atol)
|
| 501 |
+
|
| 502 |
+
if test_C:
|
| 503 |
+
q = q0.copy('C')
|
| 504 |
+
r = r0.copy('C')
|
| 505 |
+
q3, r3 = qr_delete(q, r, 3, p, which, True)
|
| 506 |
+
check_qr(q3, r3, a1, self.rtol, self.atol, assert_sqr)
|
| 507 |
+
assert_allclose(q3, q[qind], rtol=self.rtol, atol=self.atol)
|
| 508 |
+
assert_allclose(r3, r[rind], rtol=self.rtol, atol=self.atol)
|
| 509 |
+
|
| 510 |
+
def test_overwrite_qr_1_row(self):
|
| 511 |
+
# any positively strided q and r.
|
| 512 |
+
self.base_overwrite_qr('row', 1, True, True)
|
| 513 |
+
|
| 514 |
+
def test_overwrite_economic_qr_1_row(self):
|
| 515 |
+
# Any contiguous q and positively strided r.
|
| 516 |
+
self.base_overwrite_qr('row', 1, True, True, 'economic')
|
| 517 |
+
|
| 518 |
+
def test_overwrite_qr_1_col(self):
|
| 519 |
+
# any positively strided q and r.
|
| 520 |
+
# full and eco share code paths
|
| 521 |
+
self.base_overwrite_qr('col', 1, True, True)
|
| 522 |
+
|
| 523 |
+
def test_overwrite_qr_p_row(self):
|
| 524 |
+
# any positively strided q and r.
|
| 525 |
+
self.base_overwrite_qr('row', 3, True, True)
|
| 526 |
+
|
| 527 |
+
def test_overwrite_economic_qr_p_row(self):
|
| 528 |
+
# any contiguous q and positively strided r
|
| 529 |
+
self.base_overwrite_qr('row', 3, True, True, 'economic')
|
| 530 |
+
|
| 531 |
+
def test_overwrite_qr_p_col(self):
|
| 532 |
+
# only F ordered q and r can be overwritten for cols
|
| 533 |
+
# full and eco share code paths
|
| 534 |
+
self.base_overwrite_qr('col', 3, False, True)
|
| 535 |
+
|
| 536 |
+
def test_bad_which(self):
|
| 537 |
+
a, q, r = self.generate('sqr')
|
| 538 |
+
assert_raises(ValueError, qr_delete, q, r, 0, which='foo')
|
| 539 |
+
|
| 540 |
+
def test_bad_k(self):
|
| 541 |
+
a, q, r = self.generate('tall')
|
| 542 |
+
assert_raises(ValueError, qr_delete, q, r, q.shape[0], 1)
|
| 543 |
+
assert_raises(ValueError, qr_delete, q, r, -q.shape[0]-1, 1)
|
| 544 |
+
assert_raises(ValueError, qr_delete, q, r, r.shape[0], 1, 'col')
|
| 545 |
+
assert_raises(ValueError, qr_delete, q, r, -r.shape[0]-1, 1, 'col')
|
| 546 |
+
|
| 547 |
+
def test_bad_p(self):
|
| 548 |
+
a, q, r = self.generate('tall')
|
| 549 |
+
# p must be positive
|
| 550 |
+
assert_raises(ValueError, qr_delete, q, r, 0, -1)
|
| 551 |
+
assert_raises(ValueError, qr_delete, q, r, 0, -1, 'col')
|
| 552 |
+
|
| 553 |
+
# and nonzero
|
| 554 |
+
assert_raises(ValueError, qr_delete, q, r, 0, 0)
|
| 555 |
+
assert_raises(ValueError, qr_delete, q, r, 0, 0, 'col')
|
| 556 |
+
|
| 557 |
+
# must have at least k+p rows or cols, depending.
|
| 558 |
+
assert_raises(ValueError, qr_delete, q, r, 3, q.shape[0]-2)
|
| 559 |
+
assert_raises(ValueError, qr_delete, q, r, 3, r.shape[1]-2, 'col')
|
| 560 |
+
|
| 561 |
+
def test_empty_q(self):
|
| 562 |
+
a, q, r = self.generate('tall')
|
| 563 |
+
# same code path for 'row' and 'col'
|
| 564 |
+
assert_raises(ValueError, qr_delete, np.array([]), r, 0, 1)
|
| 565 |
+
|
| 566 |
+
def test_empty_r(self):
|
| 567 |
+
a, q, r = self.generate('tall')
|
| 568 |
+
# same code path for 'row' and 'col'
|
| 569 |
+
assert_raises(ValueError, qr_delete, q, np.array([]), 0, 1)
|
| 570 |
+
|
| 571 |
+
def test_mismatched_q_and_r(self):
|
| 572 |
+
a, q, r = self.generate('tall')
|
| 573 |
+
r = r[1:]
|
| 574 |
+
assert_raises(ValueError, qr_delete, q, r, 0, 1)
|
| 575 |
+
|
| 576 |
+
def test_unsupported_dtypes(self):
|
| 577 |
+
dts = ['int8', 'int16', 'int32', 'int64',
|
| 578 |
+
'uint8', 'uint16', 'uint32', 'uint64',
|
| 579 |
+
'float16', 'longdouble', 'clongdouble',
|
| 580 |
+
'bool']
|
| 581 |
+
a, q0, r0 = self.generate('tall')
|
| 582 |
+
for dtype in dts:
|
| 583 |
+
q = q0.real.astype(dtype)
|
| 584 |
+
with np.errstate(invalid="ignore"):
|
| 585 |
+
r = r0.real.astype(dtype)
|
| 586 |
+
assert_raises(ValueError, qr_delete, q, r0, 0, 1, 'row')
|
| 587 |
+
assert_raises(ValueError, qr_delete, q, r0, 0, 2, 'row')
|
| 588 |
+
assert_raises(ValueError, qr_delete, q, r0, 0, 1, 'col')
|
| 589 |
+
assert_raises(ValueError, qr_delete, q, r0, 0, 2, 'col')
|
| 590 |
+
|
| 591 |
+
assert_raises(ValueError, qr_delete, q0, r, 0, 1, 'row')
|
| 592 |
+
assert_raises(ValueError, qr_delete, q0, r, 0, 2, 'row')
|
| 593 |
+
assert_raises(ValueError, qr_delete, q0, r, 0, 1, 'col')
|
| 594 |
+
assert_raises(ValueError, qr_delete, q0, r, 0, 2, 'col')
|
| 595 |
+
|
| 596 |
+
def test_check_finite(self):
|
| 597 |
+
a0, q0, r0 = self.generate('tall')
|
| 598 |
+
|
| 599 |
+
q = q0.copy('F')
|
| 600 |
+
q[1,1] = np.nan
|
| 601 |
+
assert_raises(ValueError, qr_delete, q, r0, 0, 1, 'row')
|
| 602 |
+
assert_raises(ValueError, qr_delete, q, r0, 0, 3, 'row')
|
| 603 |
+
assert_raises(ValueError, qr_delete, q, r0, 0, 1, 'col')
|
| 604 |
+
assert_raises(ValueError, qr_delete, q, r0, 0, 3, 'col')
|
| 605 |
+
|
| 606 |
+
r = r0.copy('F')
|
| 607 |
+
r[1,1] = np.nan
|
| 608 |
+
assert_raises(ValueError, qr_delete, q0, r, 0, 1, 'row')
|
| 609 |
+
assert_raises(ValueError, qr_delete, q0, r, 0, 3, 'row')
|
| 610 |
+
assert_raises(ValueError, qr_delete, q0, r, 0, 1, 'col')
|
| 611 |
+
assert_raises(ValueError, qr_delete, q0, r, 0, 3, 'col')
|
| 612 |
+
|
| 613 |
+
def test_qr_scalar(self):
|
| 614 |
+
a, q, r = self.generate('1x1')
|
| 615 |
+
assert_raises(ValueError, qr_delete, q[0, 0], r, 0, 1, 'row')
|
| 616 |
+
assert_raises(ValueError, qr_delete, q, r[0, 0], 0, 1, 'row')
|
| 617 |
+
assert_raises(ValueError, qr_delete, q[0, 0], r, 0, 1, 'col')
|
| 618 |
+
assert_raises(ValueError, qr_delete, q, r[0, 0], 0, 1, 'col')
|
| 619 |
+
|
| 620 |
+
class TestQRdelete_f(BaseQRdelete):
|
| 621 |
+
dtype = np.dtype('f')
|
| 622 |
+
|
| 623 |
+
class TestQRdelete_F(BaseQRdelete):
|
| 624 |
+
dtype = np.dtype('F')
|
| 625 |
+
|
| 626 |
+
class TestQRdelete_d(BaseQRdelete):
|
| 627 |
+
dtype = np.dtype('d')
|
| 628 |
+
|
| 629 |
+
class TestQRdelete_D(BaseQRdelete):
|
| 630 |
+
dtype = np.dtype('D')
|
| 631 |
+
|
| 632 |
+
class BaseQRinsert(BaseQRdeltas):
|
| 633 |
+
def generate(self, type, mode='full', which='row', p=1):
|
| 634 |
+
a, q, r = super().generate(type, mode)
|
| 635 |
+
|
| 636 |
+
assert_(p > 0)
|
| 637 |
+
rng = np.random.RandomState(1234)
|
| 638 |
+
|
| 639 |
+
# super call set the seed...
|
| 640 |
+
if which == 'row':
|
| 641 |
+
if p == 1:
|
| 642 |
+
u = rng.random(a.shape[1])
|
| 643 |
+
else:
|
| 644 |
+
u = rng.random((p, a.shape[1]))
|
| 645 |
+
elif which == 'col':
|
| 646 |
+
if p == 1:
|
| 647 |
+
u = rng.random(a.shape[0])
|
| 648 |
+
else:
|
| 649 |
+
u = rng.random((a.shape[0], p))
|
| 650 |
+
else:
|
| 651 |
+
ValueError('which should be either "row" or "col"')
|
| 652 |
+
|
| 653 |
+
if np.iscomplexobj(self.dtype.type(1)):
|
| 654 |
+
b = rng.random(u.shape)
|
| 655 |
+
u = u + 1j * b
|
| 656 |
+
|
| 657 |
+
u = u.astype(self.dtype)
|
| 658 |
+
return a, q, r, u
|
| 659 |
+
|
| 660 |
+
def test_sqr_1_row(self):
|
| 661 |
+
a, q, r, u = self.generate('sqr', which='row')
|
| 662 |
+
for row in range(r.shape[0] + 1):
|
| 663 |
+
q1, r1 = qr_insert(q, r, u, row)
|
| 664 |
+
a1 = np.insert(a, row, u, 0)
|
| 665 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 666 |
+
|
| 667 |
+
def test_sqr_p_row(self):
|
| 668 |
+
# sqr + rows --> fat always
|
| 669 |
+
a, q, r, u = self.generate('sqr', which='row', p=3)
|
| 670 |
+
for row in range(r.shape[0] + 1):
|
| 671 |
+
q1, r1 = qr_insert(q, r, u, row)
|
| 672 |
+
a1 = np.insert(a, np.full(3, row, np.intp), u, 0)
|
| 673 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 674 |
+
|
| 675 |
+
def test_sqr_1_col(self):
|
| 676 |
+
a, q, r, u = self.generate('sqr', which='col')
|
| 677 |
+
for col in range(r.shape[1] + 1):
|
| 678 |
+
q1, r1 = qr_insert(q, r, u, col, 'col', overwrite_qru=False)
|
| 679 |
+
a1 = np.insert(a, col, u, 1)
|
| 680 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 681 |
+
|
| 682 |
+
def test_sqr_p_col(self):
|
| 683 |
+
# sqr + cols --> fat always
|
| 684 |
+
a, q, r, u = self.generate('sqr', which='col', p=3)
|
| 685 |
+
for col in range(r.shape[1] + 1):
|
| 686 |
+
q1, r1 = qr_insert(q, r, u, col, 'col', overwrite_qru=False)
|
| 687 |
+
a1 = np.insert(a, np.full(3, col, np.intp), u, 1)
|
| 688 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 689 |
+
|
| 690 |
+
def test_tall_1_row(self):
|
| 691 |
+
a, q, r, u = self.generate('tall', which='row')
|
| 692 |
+
for row in range(r.shape[0] + 1):
|
| 693 |
+
q1, r1 = qr_insert(q, r, u, row)
|
| 694 |
+
a1 = np.insert(a, row, u, 0)
|
| 695 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 696 |
+
|
| 697 |
+
def test_tall_p_row(self):
|
| 698 |
+
# tall + rows --> tall always
|
| 699 |
+
a, q, r, u = self.generate('tall', which='row', p=3)
|
| 700 |
+
for row in range(r.shape[0] + 1):
|
| 701 |
+
q1, r1 = qr_insert(q, r, u, row)
|
| 702 |
+
a1 = np.insert(a, np.full(3, row, np.intp), u, 0)
|
| 703 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 704 |
+
|
| 705 |
+
def test_tall_1_col(self):
|
| 706 |
+
a, q, r, u = self.generate('tall', which='col')
|
| 707 |
+
for col in range(r.shape[1] + 1):
|
| 708 |
+
q1, r1 = qr_insert(q, r, u, col, 'col', overwrite_qru=False)
|
| 709 |
+
a1 = np.insert(a, col, u, 1)
|
| 710 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 711 |
+
|
| 712 |
+
# for column adds to tall matrices there are three cases to test
|
| 713 |
+
# tall + pcol --> tall
|
| 714 |
+
# tall + pcol --> sqr
|
| 715 |
+
# tall + pcol --> fat
|
| 716 |
+
def base_tall_p_col_xxx(self, p):
|
| 717 |
+
a, q, r, u = self.generate('tall', which='col', p=p)
|
| 718 |
+
for col in range(r.shape[1] + 1):
|
| 719 |
+
q1, r1 = qr_insert(q, r, u, col, 'col', overwrite_qru=False)
|
| 720 |
+
a1 = np.insert(a, np.full(p, col, np.intp), u, 1)
|
| 721 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 722 |
+
|
| 723 |
+
def test_tall_p_col_tall(self):
|
| 724 |
+
# 12x7 + 12x3 = 12x10 --> stays tall
|
| 725 |
+
self.base_tall_p_col_xxx(3)
|
| 726 |
+
|
| 727 |
+
def test_tall_p_col_sqr(self):
|
| 728 |
+
# 12x7 + 12x5 = 12x12 --> becomes sqr
|
| 729 |
+
self.base_tall_p_col_xxx(5)
|
| 730 |
+
|
| 731 |
+
def test_tall_p_col_fat(self):
|
| 732 |
+
# 12x7 + 12x7 = 12x14 --> becomes fat
|
| 733 |
+
self.base_tall_p_col_xxx(7)
|
| 734 |
+
|
| 735 |
+
def test_fat_1_row(self):
|
| 736 |
+
a, q, r, u = self.generate('fat', which='row')
|
| 737 |
+
for row in range(r.shape[0] + 1):
|
| 738 |
+
q1, r1 = qr_insert(q, r, u, row)
|
| 739 |
+
a1 = np.insert(a, row, u, 0)
|
| 740 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 741 |
+
|
| 742 |
+
# for row adds to fat matrices there are three cases to test
|
| 743 |
+
# fat + prow --> fat
|
| 744 |
+
# fat + prow --> sqr
|
| 745 |
+
# fat + prow --> tall
|
| 746 |
+
def base_fat_p_row_xxx(self, p):
|
| 747 |
+
a, q, r, u = self.generate('fat', which='row', p=p)
|
| 748 |
+
for row in range(r.shape[0] + 1):
|
| 749 |
+
q1, r1 = qr_insert(q, r, u, row)
|
| 750 |
+
a1 = np.insert(a, np.full(p, row, np.intp), u, 0)
|
| 751 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 752 |
+
|
| 753 |
+
def test_fat_p_row_fat(self):
|
| 754 |
+
# 7x12 + 3x12 = 10x12 --> stays fat
|
| 755 |
+
self.base_fat_p_row_xxx(3)
|
| 756 |
+
|
| 757 |
+
def test_fat_p_row_sqr(self):
|
| 758 |
+
# 7x12 + 5x12 = 12x12 --> becomes sqr
|
| 759 |
+
self.base_fat_p_row_xxx(5)
|
| 760 |
+
|
| 761 |
+
def test_fat_p_row_tall(self):
|
| 762 |
+
# 7x12 + 7x12 = 14x12 --> becomes tall
|
| 763 |
+
self.base_fat_p_row_xxx(7)
|
| 764 |
+
|
| 765 |
+
def test_fat_1_col(self):
|
| 766 |
+
a, q, r, u = self.generate('fat', which='col')
|
| 767 |
+
for col in range(r.shape[1] + 1):
|
| 768 |
+
q1, r1 = qr_insert(q, r, u, col, 'col', overwrite_qru=False)
|
| 769 |
+
a1 = np.insert(a, col, u, 1)
|
| 770 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 771 |
+
|
| 772 |
+
def test_fat_p_col(self):
|
| 773 |
+
# fat + cols --> fat always
|
| 774 |
+
a, q, r, u = self.generate('fat', which='col', p=3)
|
| 775 |
+
for col in range(r.shape[1] + 1):
|
| 776 |
+
q1, r1 = qr_insert(q, r, u, col, 'col', overwrite_qru=False)
|
| 777 |
+
a1 = np.insert(a, np.full(3, col, np.intp), u, 1)
|
| 778 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 779 |
+
|
| 780 |
+
def test_economic_1_row(self):
|
| 781 |
+
a, q, r, u = self.generate('tall', 'economic', 'row')
|
| 782 |
+
for row in range(r.shape[0] + 1):
|
| 783 |
+
q1, r1 = qr_insert(q, r, u, row, overwrite_qru=False)
|
| 784 |
+
a1 = np.insert(a, row, u, 0)
|
| 785 |
+
check_qr(q1, r1, a1, self.rtol, self.atol, False)
|
| 786 |
+
|
| 787 |
+
def test_economic_p_row(self):
|
| 788 |
+
# tall + rows --> tall always
|
| 789 |
+
a, q, r, u = self.generate('tall', 'economic', 'row', 3)
|
| 790 |
+
for row in range(r.shape[0] + 1):
|
| 791 |
+
q1, r1 = qr_insert(q, r, u, row, overwrite_qru=False)
|
| 792 |
+
a1 = np.insert(a, np.full(3, row, np.intp), u, 0)
|
| 793 |
+
check_qr(q1, r1, a1, self.rtol, self.atol, False)
|
| 794 |
+
|
| 795 |
+
def test_economic_1_col(self):
|
| 796 |
+
a, q, r, u = self.generate('tall', 'economic', which='col')
|
| 797 |
+
for col in range(r.shape[1] + 1):
|
| 798 |
+
q1, r1 = qr_insert(q, r, u.copy(), col, 'col', overwrite_qru=False)
|
| 799 |
+
a1 = np.insert(a, col, u, 1)
|
| 800 |
+
check_qr(q1, r1, a1, self.rtol, self.atol, False)
|
| 801 |
+
|
| 802 |
+
def test_economic_1_col_bad_update(self):
|
| 803 |
+
# When the column to be added lies in the span of Q, the update is
|
| 804 |
+
# not meaningful. This is detected, and a LinAlgError is issued.
|
| 805 |
+
q = np.eye(5, 3, dtype=self.dtype)
|
| 806 |
+
r = np.eye(3, dtype=self.dtype)
|
| 807 |
+
u = np.array([1, 0, 0, 0, 0], self.dtype)
|
| 808 |
+
assert_raises(linalg.LinAlgError, qr_insert, q, r, u, 0, 'col')
|
| 809 |
+
|
| 810 |
+
# for column adds to economic matrices there are three cases to test
|
| 811 |
+
# eco + pcol --> eco
|
| 812 |
+
# eco + pcol --> sqr
|
| 813 |
+
# eco + pcol --> fat
|
| 814 |
+
def base_economic_p_col_xxx(self, p):
|
| 815 |
+
a, q, r, u = self.generate('tall', 'economic', which='col', p=p)
|
| 816 |
+
for col in range(r.shape[1] + 1):
|
| 817 |
+
q1, r1 = qr_insert(q, r, u, col, 'col', overwrite_qru=False)
|
| 818 |
+
a1 = np.insert(a, np.full(p, col, np.intp), u, 1)
|
| 819 |
+
check_qr(q1, r1, a1, self.rtol, self.atol, False)
|
| 820 |
+
|
| 821 |
+
def test_economic_p_col_eco(self):
|
| 822 |
+
# 12x7 + 12x3 = 12x10 --> stays eco
|
| 823 |
+
self.base_economic_p_col_xxx(3)
|
| 824 |
+
|
| 825 |
+
def test_economic_p_col_sqr(self):
|
| 826 |
+
# 12x7 + 12x5 = 12x12 --> becomes sqr
|
| 827 |
+
self.base_economic_p_col_xxx(5)
|
| 828 |
+
|
| 829 |
+
def test_economic_p_col_fat(self):
|
| 830 |
+
# 12x7 + 12x7 = 12x14 --> becomes fat
|
| 831 |
+
self.base_economic_p_col_xxx(7)
|
| 832 |
+
|
| 833 |
+
def test_Mx1_1_row(self):
|
| 834 |
+
a, q, r, u = self.generate('Mx1', which='row')
|
| 835 |
+
for row in range(r.shape[0] + 1):
|
| 836 |
+
q1, r1 = qr_insert(q, r, u, row)
|
| 837 |
+
a1 = np.insert(a, row, u, 0)
|
| 838 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 839 |
+
|
| 840 |
+
def test_Mx1_p_row(self):
|
| 841 |
+
a, q, r, u = self.generate('Mx1', which='row', p=3)
|
| 842 |
+
for row in range(r.shape[0] + 1):
|
| 843 |
+
q1, r1 = qr_insert(q, r, u, row)
|
| 844 |
+
a1 = np.insert(a, np.full(3, row, np.intp), u, 0)
|
| 845 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 846 |
+
|
| 847 |
+
def test_Mx1_1_col(self):
|
| 848 |
+
a, q, r, u = self.generate('Mx1', which='col')
|
| 849 |
+
for col in range(r.shape[1] + 1):
|
| 850 |
+
q1, r1 = qr_insert(q, r, u, col, 'col', overwrite_qru=False)
|
| 851 |
+
a1 = np.insert(a, col, u, 1)
|
| 852 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 853 |
+
|
| 854 |
+
def test_Mx1_p_col(self):
|
| 855 |
+
a, q, r, u = self.generate('Mx1', which='col', p=3)
|
| 856 |
+
for col in range(r.shape[1] + 1):
|
| 857 |
+
q1, r1 = qr_insert(q, r, u, col, 'col', overwrite_qru=False)
|
| 858 |
+
a1 = np.insert(a, np.full(3, col, np.intp), u, 1)
|
| 859 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 860 |
+
|
| 861 |
+
def test_Mx1_economic_1_row(self):
|
| 862 |
+
a, q, r, u = self.generate('Mx1', 'economic', 'row')
|
| 863 |
+
for row in range(r.shape[0] + 1):
|
| 864 |
+
q1, r1 = qr_insert(q, r, u, row)
|
| 865 |
+
a1 = np.insert(a, row, u, 0)
|
| 866 |
+
check_qr(q1, r1, a1, self.rtol, self.atol, False)
|
| 867 |
+
|
| 868 |
+
def test_Mx1_economic_p_row(self):
|
| 869 |
+
a, q, r, u = self.generate('Mx1', 'economic', 'row', 3)
|
| 870 |
+
for row in range(r.shape[0] + 1):
|
| 871 |
+
q1, r1 = qr_insert(q, r, u, row)
|
| 872 |
+
a1 = np.insert(a, np.full(3, row, np.intp), u, 0)
|
| 873 |
+
check_qr(q1, r1, a1, self.rtol, self.atol, False)
|
| 874 |
+
|
| 875 |
+
def test_Mx1_economic_1_col(self):
|
| 876 |
+
a, q, r, u = self.generate('Mx1', 'economic', 'col')
|
| 877 |
+
for col in range(r.shape[1] + 1):
|
| 878 |
+
q1, r1 = qr_insert(q, r, u, col, 'col', overwrite_qru=False)
|
| 879 |
+
a1 = np.insert(a, col, u, 1)
|
| 880 |
+
check_qr(q1, r1, a1, self.rtol, self.atol, False)
|
| 881 |
+
|
| 882 |
+
def test_Mx1_economic_p_col(self):
|
| 883 |
+
a, q, r, u = self.generate('Mx1', 'economic', 'col', 3)
|
| 884 |
+
for col in range(r.shape[1] + 1):
|
| 885 |
+
q1, r1 = qr_insert(q, r, u, col, 'col', overwrite_qru=False)
|
| 886 |
+
a1 = np.insert(a, np.full(3, col, np.intp), u, 1)
|
| 887 |
+
check_qr(q1, r1, a1, self.rtol, self.atol, False)
|
| 888 |
+
|
| 889 |
+
def test_1xN_1_row(self):
|
| 890 |
+
a, q, r, u = self.generate('1xN', which='row')
|
| 891 |
+
for row in range(r.shape[0] + 1):
|
| 892 |
+
q1, r1 = qr_insert(q, r, u, row)
|
| 893 |
+
a1 = np.insert(a, row, u, 0)
|
| 894 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 895 |
+
|
| 896 |
+
def test_1xN_p_row(self):
|
| 897 |
+
a, q, r, u = self.generate('1xN', which='row', p=3)
|
| 898 |
+
for row in range(r.shape[0] + 1):
|
| 899 |
+
q1, r1 = qr_insert(q, r, u, row)
|
| 900 |
+
a1 = np.insert(a, np.full(3, row, np.intp), u, 0)
|
| 901 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 902 |
+
|
| 903 |
+
def test_1xN_1_col(self):
|
| 904 |
+
a, q, r, u = self.generate('1xN', which='col')
|
| 905 |
+
for col in range(r.shape[1] + 1):
|
| 906 |
+
q1, r1 = qr_insert(q, r, u, col, 'col', overwrite_qru=False)
|
| 907 |
+
a1 = np.insert(a, col, u, 1)
|
| 908 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 909 |
+
|
| 910 |
+
def test_1xN_p_col(self):
|
| 911 |
+
a, q, r, u = self.generate('1xN', which='col', p=3)
|
| 912 |
+
for col in range(r.shape[1] + 1):
|
| 913 |
+
q1, r1 = qr_insert(q, r, u, col, 'col', overwrite_qru=False)
|
| 914 |
+
a1 = np.insert(a, np.full(3, col, np.intp), u, 1)
|
| 915 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 916 |
+
|
| 917 |
+
def test_1x1_1_row(self):
|
| 918 |
+
a, q, r, u = self.generate('1x1', which='row')
|
| 919 |
+
for row in range(r.shape[0] + 1):
|
| 920 |
+
q1, r1 = qr_insert(q, r, u, row)
|
| 921 |
+
a1 = np.insert(a, row, u, 0)
|
| 922 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 923 |
+
|
| 924 |
+
def test_1x1_p_row(self):
|
| 925 |
+
a, q, r, u = self.generate('1x1', which='row', p=3)
|
| 926 |
+
for row in range(r.shape[0] + 1):
|
| 927 |
+
q1, r1 = qr_insert(q, r, u, row)
|
| 928 |
+
a1 = np.insert(a, np.full(3, row, np.intp), u, 0)
|
| 929 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 930 |
+
|
| 931 |
+
def test_1x1_1_col(self):
|
| 932 |
+
a, q, r, u = self.generate('1x1', which='col')
|
| 933 |
+
for col in range(r.shape[1] + 1):
|
| 934 |
+
q1, r1 = qr_insert(q, r, u, col, 'col', overwrite_qru=False)
|
| 935 |
+
a1 = np.insert(a, col, u, 1)
|
| 936 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 937 |
+
|
| 938 |
+
def test_1x1_p_col(self):
|
| 939 |
+
a, q, r, u = self.generate('1x1', which='col', p=3)
|
| 940 |
+
for col in range(r.shape[1] + 1):
|
| 941 |
+
q1, r1 = qr_insert(q, r, u, col, 'col', overwrite_qru=False)
|
| 942 |
+
a1 = np.insert(a, np.full(3, col, np.intp), u, 1)
|
| 943 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 944 |
+
|
| 945 |
+
def test_1x1_1_scalar(self):
|
| 946 |
+
a, q, r, u = self.generate('1x1', which='row')
|
| 947 |
+
assert_raises(ValueError, qr_insert, q[0, 0], r, u, 0, 'row')
|
| 948 |
+
assert_raises(ValueError, qr_insert, q, r[0, 0], u, 0, 'row')
|
| 949 |
+
assert_raises(ValueError, qr_insert, q, r, u[0], 0, 'row')
|
| 950 |
+
|
| 951 |
+
assert_raises(ValueError, qr_insert, q[0, 0], r, u, 0, 'col')
|
| 952 |
+
assert_raises(ValueError, qr_insert, q, r[0, 0], u, 0, 'col')
|
| 953 |
+
assert_raises(ValueError, qr_insert, q, r, u[0], 0, 'col')
|
| 954 |
+
|
| 955 |
+
def base_non_simple_strides(self, adjust_strides, k, p, which):
|
| 956 |
+
for type in ['sqr', 'tall', 'fat']:
|
| 957 |
+
a, q0, r0, u0 = self.generate(type, which=which, p=p)
|
| 958 |
+
qs, rs, us = adjust_strides((q0, r0, u0))
|
| 959 |
+
if p == 1:
|
| 960 |
+
ai = np.insert(a, k, u0, 0 if which == 'row' else 1)
|
| 961 |
+
else:
|
| 962 |
+
ai = np.insert(a, np.full(p, k, np.intp),
|
| 963 |
+
u0 if which == 'row' else u0,
|
| 964 |
+
0 if which == 'row' else 1)
|
| 965 |
+
|
| 966 |
+
# for each variable, q, r, u we try with it strided and
|
| 967 |
+
# overwrite=False. Then we try with overwrite=True. Nothing
|
| 968 |
+
# is checked to see if it can be overwritten, since only
|
| 969 |
+
# F ordered Q can be overwritten when adding columns.
|
| 970 |
+
|
| 971 |
+
q = q0.copy('F')
|
| 972 |
+
r = r0.copy('F')
|
| 973 |
+
u = u0.copy('F')
|
| 974 |
+
q1, r1 = qr_insert(qs, r, u, k, which, overwrite_qru=False)
|
| 975 |
+
check_qr(q1, r1, ai, self.rtol, self.atol)
|
| 976 |
+
q1o, r1o = qr_insert(qs, r, u, k, which, overwrite_qru=True)
|
| 977 |
+
check_qr(q1o, r1o, ai, self.rtol, self.atol)
|
| 978 |
+
|
| 979 |
+
q = q0.copy('F')
|
| 980 |
+
r = r0.copy('F')
|
| 981 |
+
u = u0.copy('F')
|
| 982 |
+
q2, r2 = qr_insert(q, rs, u, k, which, overwrite_qru=False)
|
| 983 |
+
check_qr(q2, r2, ai, self.rtol, self.atol)
|
| 984 |
+
q2o, r2o = qr_insert(q, rs, u, k, which, overwrite_qru=True)
|
| 985 |
+
check_qr(q2o, r2o, ai, self.rtol, self.atol)
|
| 986 |
+
|
| 987 |
+
q = q0.copy('F')
|
| 988 |
+
r = r0.copy('F')
|
| 989 |
+
u = u0.copy('F')
|
| 990 |
+
q3, r3 = qr_insert(q, r, us, k, which, overwrite_qru=False)
|
| 991 |
+
check_qr(q3, r3, ai, self.rtol, self.atol)
|
| 992 |
+
q3o, r3o = qr_insert(q, r, us, k, which, overwrite_qru=True)
|
| 993 |
+
check_qr(q3o, r3o, ai, self.rtol, self.atol)
|
| 994 |
+
|
| 995 |
+
q = q0.copy('F')
|
| 996 |
+
r = r0.copy('F')
|
| 997 |
+
u = u0.copy('F')
|
| 998 |
+
# since some of these were consumed above
|
| 999 |
+
qs, rs, us = adjust_strides((q, r, u))
|
| 1000 |
+
q5, r5 = qr_insert(qs, rs, us, k, which, overwrite_qru=False)
|
| 1001 |
+
check_qr(q5, r5, ai, self.rtol, self.atol)
|
| 1002 |
+
q5o, r5o = qr_insert(qs, rs, us, k, which, overwrite_qru=True)
|
| 1003 |
+
check_qr(q5o, r5o, ai, self.rtol, self.atol)
|
| 1004 |
+
|
| 1005 |
+
def test_non_unit_strides_1_row(self):
|
| 1006 |
+
self.base_non_simple_strides(make_strided, 0, 1, 'row')
|
| 1007 |
+
|
| 1008 |
+
def test_non_unit_strides_p_row(self):
|
| 1009 |
+
self.base_non_simple_strides(make_strided, 0, 3, 'row')
|
| 1010 |
+
|
| 1011 |
+
def test_non_unit_strides_1_col(self):
|
| 1012 |
+
self.base_non_simple_strides(make_strided, 0, 1, 'col')
|
| 1013 |
+
|
| 1014 |
+
def test_non_unit_strides_p_col(self):
|
| 1015 |
+
self.base_non_simple_strides(make_strided, 0, 3, 'col')
|
| 1016 |
+
|
| 1017 |
+
def test_neg_strides_1_row(self):
|
| 1018 |
+
self.base_non_simple_strides(negate_strides, 0, 1, 'row')
|
| 1019 |
+
|
| 1020 |
+
def test_neg_strides_p_row(self):
|
| 1021 |
+
self.base_non_simple_strides(negate_strides, 0, 3, 'row')
|
| 1022 |
+
|
| 1023 |
+
def test_neg_strides_1_col(self):
|
| 1024 |
+
self.base_non_simple_strides(negate_strides, 0, 1, 'col')
|
| 1025 |
+
|
| 1026 |
+
def test_neg_strides_p_col(self):
|
| 1027 |
+
self.base_non_simple_strides(negate_strides, 0, 3, 'col')
|
| 1028 |
+
|
| 1029 |
+
def test_non_itemsize_strides_1_row(self):
|
| 1030 |
+
self.base_non_simple_strides(nonitemsize_strides, 0, 1, 'row')
|
| 1031 |
+
|
| 1032 |
+
def test_non_itemsize_strides_p_row(self):
|
| 1033 |
+
self.base_non_simple_strides(nonitemsize_strides, 0, 3, 'row')
|
| 1034 |
+
|
| 1035 |
+
def test_non_itemsize_strides_1_col(self):
|
| 1036 |
+
self.base_non_simple_strides(nonitemsize_strides, 0, 1, 'col')
|
| 1037 |
+
|
| 1038 |
+
def test_non_itemsize_strides_p_col(self):
|
| 1039 |
+
self.base_non_simple_strides(nonitemsize_strides, 0, 3, 'col')
|
| 1040 |
+
|
| 1041 |
+
def test_non_native_byte_order_1_row(self):
|
| 1042 |
+
self.base_non_simple_strides(make_nonnative, 0, 1, 'row')
|
| 1043 |
+
|
| 1044 |
+
def test_non_native_byte_order_p_row(self):
|
| 1045 |
+
self.base_non_simple_strides(make_nonnative, 0, 3, 'row')
|
| 1046 |
+
|
| 1047 |
+
def test_non_native_byte_order_1_col(self):
|
| 1048 |
+
self.base_non_simple_strides(make_nonnative, 0, 1, 'col')
|
| 1049 |
+
|
| 1050 |
+
def test_non_native_byte_order_p_col(self):
|
| 1051 |
+
self.base_non_simple_strides(make_nonnative, 0, 3, 'col')
|
| 1052 |
+
|
| 1053 |
+
def test_overwrite_qu_rank_1(self):
|
| 1054 |
+
# when inserting rows, the size of both Q and R change, so only
|
| 1055 |
+
# column inserts can overwrite q. Only complex column inserts
|
| 1056 |
+
# with C ordered Q overwrite u. Any contiguous Q is overwritten
|
| 1057 |
+
# when inserting 1 column
|
| 1058 |
+
a, q0, r, u, = self.generate('sqr', which='col', p=1)
|
| 1059 |
+
q = q0.copy('C')
|
| 1060 |
+
u0 = u.copy()
|
| 1061 |
+
# don't overwrite
|
| 1062 |
+
q1, r1 = qr_insert(q, r, u, 0, 'col', overwrite_qru=False)
|
| 1063 |
+
a1 = np.insert(a, 0, u0, 1)
|
| 1064 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 1065 |
+
check_qr(q, r, a, self.rtol, self.atol)
|
| 1066 |
+
|
| 1067 |
+
# try overwriting
|
| 1068 |
+
q2, r2 = qr_insert(q, r, u, 0, 'col', overwrite_qru=True)
|
| 1069 |
+
check_qr(q2, r2, a1, self.rtol, self.atol)
|
| 1070 |
+
# verify the overwriting
|
| 1071 |
+
assert_allclose(q2, q, rtol=self.rtol, atol=self.atol)
|
| 1072 |
+
assert_allclose(u, u0.conj(), self.rtol, self.atol)
|
| 1073 |
+
|
| 1074 |
+
# now try with a fortran ordered Q
|
| 1075 |
+
qF = q0.copy('F')
|
| 1076 |
+
u1 = u0.copy()
|
| 1077 |
+
q3, r3 = qr_insert(qF, r, u1, 0, 'col', overwrite_qru=False)
|
| 1078 |
+
check_qr(q3, r3, a1, self.rtol, self.atol)
|
| 1079 |
+
check_qr(qF, r, a, self.rtol, self.atol)
|
| 1080 |
+
|
| 1081 |
+
# try overwriting
|
| 1082 |
+
q4, r4 = qr_insert(qF, r, u1, 0, 'col', overwrite_qru=True)
|
| 1083 |
+
check_qr(q4, r4, a1, self.rtol, self.atol)
|
| 1084 |
+
assert_allclose(q4, qF, rtol=self.rtol, atol=self.atol)
|
| 1085 |
+
|
| 1086 |
+
def test_overwrite_qu_rank_p(self):
|
| 1087 |
+
# when inserting rows, the size of both Q and R change, so only
|
| 1088 |
+
# column inserts can potentially overwrite Q. In practice, only
|
| 1089 |
+
# F ordered Q are overwritten with a rank p update.
|
| 1090 |
+
a, q0, r, u, = self.generate('sqr', which='col', p=3)
|
| 1091 |
+
q = q0.copy('F')
|
| 1092 |
+
a1 = np.insert(a, np.zeros(3, np.intp), u, 1)
|
| 1093 |
+
|
| 1094 |
+
# don't overwrite
|
| 1095 |
+
q1, r1 = qr_insert(q, r, u, 0, 'col', overwrite_qru=False)
|
| 1096 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 1097 |
+
check_qr(q, r, a, self.rtol, self.atol)
|
| 1098 |
+
|
| 1099 |
+
# try overwriting
|
| 1100 |
+
q2, r2 = qr_insert(q, r, u, 0, 'col', overwrite_qru=True)
|
| 1101 |
+
check_qr(q2, r2, a1, self.rtol, self.atol)
|
| 1102 |
+
assert_allclose(q2, q, rtol=self.rtol, atol=self.atol)
|
| 1103 |
+
|
| 1104 |
+
def test_empty_inputs(self):
|
| 1105 |
+
a, q, r, u = self.generate('sqr', which='row')
|
| 1106 |
+
assert_raises(ValueError, qr_insert, np.array([]), r, u, 0, 'row')
|
| 1107 |
+
assert_raises(ValueError, qr_insert, q, np.array([]), u, 0, 'row')
|
| 1108 |
+
assert_raises(ValueError, qr_insert, q, r, np.array([]), 0, 'row')
|
| 1109 |
+
assert_raises(ValueError, qr_insert, np.array([]), r, u, 0, 'col')
|
| 1110 |
+
assert_raises(ValueError, qr_insert, q, np.array([]), u, 0, 'col')
|
| 1111 |
+
assert_raises(ValueError, qr_insert, q, r, np.array([]), 0, 'col')
|
| 1112 |
+
|
| 1113 |
+
def test_mismatched_shapes(self):
|
| 1114 |
+
a, q, r, u = self.generate('tall', which='row')
|
| 1115 |
+
assert_raises(ValueError, qr_insert, q, r[1:], u, 0, 'row')
|
| 1116 |
+
assert_raises(ValueError, qr_insert, q[:-2], r, u, 0, 'row')
|
| 1117 |
+
assert_raises(ValueError, qr_insert, q, r, u[1:], 0, 'row')
|
| 1118 |
+
assert_raises(ValueError, qr_insert, q, r[1:], u, 0, 'col')
|
| 1119 |
+
assert_raises(ValueError, qr_insert, q[:-2], r, u, 0, 'col')
|
| 1120 |
+
assert_raises(ValueError, qr_insert, q, r, u[1:], 0, 'col')
|
| 1121 |
+
|
| 1122 |
+
def test_unsupported_dtypes(self):
|
| 1123 |
+
dts = ['int8', 'int16', 'int32', 'int64',
|
| 1124 |
+
'uint8', 'uint16', 'uint32', 'uint64',
|
| 1125 |
+
'float16', 'longdouble', 'clongdouble',
|
| 1126 |
+
'bool']
|
| 1127 |
+
a, q0, r0, u0 = self.generate('sqr', which='row')
|
| 1128 |
+
for dtype in dts:
|
| 1129 |
+
q = q0.real.astype(dtype)
|
| 1130 |
+
with np.errstate(invalid="ignore"):
|
| 1131 |
+
r = r0.real.astype(dtype)
|
| 1132 |
+
u = u0.real.astype(dtype)
|
| 1133 |
+
assert_raises(ValueError, qr_insert, q, r0, u0, 0, 'row')
|
| 1134 |
+
assert_raises(ValueError, qr_insert, q, r0, u0, 0, 'col')
|
| 1135 |
+
assert_raises(ValueError, qr_insert, q0, r, u0, 0, 'row')
|
| 1136 |
+
assert_raises(ValueError, qr_insert, q0, r, u0, 0, 'col')
|
| 1137 |
+
assert_raises(ValueError, qr_insert, q0, r0, u, 0, 'row')
|
| 1138 |
+
assert_raises(ValueError, qr_insert, q0, r0, u, 0, 'col')
|
| 1139 |
+
|
| 1140 |
+
def test_check_finite(self):
|
| 1141 |
+
a0, q0, r0, u0 = self.generate('sqr', which='row', p=3)
|
| 1142 |
+
|
| 1143 |
+
q = q0.copy('F')
|
| 1144 |
+
q[1,1] = np.nan
|
| 1145 |
+
assert_raises(ValueError, qr_insert, q, r0, u0[:,0], 0, 'row')
|
| 1146 |
+
assert_raises(ValueError, qr_insert, q, r0, u0, 0, 'row')
|
| 1147 |
+
assert_raises(ValueError, qr_insert, q, r0, u0[:,0], 0, 'col')
|
| 1148 |
+
assert_raises(ValueError, qr_insert, q, r0, u0, 0, 'col')
|
| 1149 |
+
|
| 1150 |
+
r = r0.copy('F')
|
| 1151 |
+
r[1,1] = np.nan
|
| 1152 |
+
assert_raises(ValueError, qr_insert, q0, r, u0[:,0], 0, 'row')
|
| 1153 |
+
assert_raises(ValueError, qr_insert, q0, r, u0, 0, 'row')
|
| 1154 |
+
assert_raises(ValueError, qr_insert, q0, r, u0[:,0], 0, 'col')
|
| 1155 |
+
assert_raises(ValueError, qr_insert, q0, r, u0, 0, 'col')
|
| 1156 |
+
|
| 1157 |
+
u = u0.copy('F')
|
| 1158 |
+
u[0,0] = np.nan
|
| 1159 |
+
assert_raises(ValueError, qr_insert, q0, r0, u[:,0], 0, 'row')
|
| 1160 |
+
assert_raises(ValueError, qr_insert, q0, r0, u, 0, 'row')
|
| 1161 |
+
assert_raises(ValueError, qr_insert, q0, r0, u[:,0], 0, 'col')
|
| 1162 |
+
assert_raises(ValueError, qr_insert, q0, r0, u, 0, 'col')
|
| 1163 |
+
|
| 1164 |
+
class TestQRinsert_f(BaseQRinsert):
|
| 1165 |
+
dtype = np.dtype('f')
|
| 1166 |
+
|
| 1167 |
+
class TestQRinsert_F(BaseQRinsert):
|
| 1168 |
+
dtype = np.dtype('F')
|
| 1169 |
+
|
| 1170 |
+
class TestQRinsert_d(BaseQRinsert):
|
| 1171 |
+
dtype = np.dtype('d')
|
| 1172 |
+
|
| 1173 |
+
class TestQRinsert_D(BaseQRinsert):
|
| 1174 |
+
dtype = np.dtype('D')
|
| 1175 |
+
|
| 1176 |
+
class BaseQRupdate(BaseQRdeltas):
|
| 1177 |
+
def generate(self, type, mode='full', p=1):
|
| 1178 |
+
a, q, r = super().generate(type, mode)
|
| 1179 |
+
|
| 1180 |
+
# super call set the seed...
|
| 1181 |
+
if p == 1:
|
| 1182 |
+
u = np.random.random(q.shape[0])
|
| 1183 |
+
v = np.random.random(r.shape[1])
|
| 1184 |
+
else:
|
| 1185 |
+
u = np.random.random((q.shape[0], p))
|
| 1186 |
+
v = np.random.random((r.shape[1], p))
|
| 1187 |
+
|
| 1188 |
+
if np.iscomplexobj(self.dtype.type(1)):
|
| 1189 |
+
b = np.random.random(u.shape)
|
| 1190 |
+
u = u + 1j * b
|
| 1191 |
+
|
| 1192 |
+
c = np.random.random(v.shape)
|
| 1193 |
+
v = v + 1j * c
|
| 1194 |
+
|
| 1195 |
+
u = u.astype(self.dtype)
|
| 1196 |
+
v = v.astype(self.dtype)
|
| 1197 |
+
return a, q, r, u, v
|
| 1198 |
+
|
| 1199 |
+
def test_sqr_rank_1(self):
|
| 1200 |
+
a, q, r, u, v = self.generate('sqr')
|
| 1201 |
+
q1, r1 = qr_update(q, r, u, v, False)
|
| 1202 |
+
a1 = a + np.outer(u, v.conj())
|
| 1203 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 1204 |
+
|
| 1205 |
+
def test_sqr_rank_p(self):
|
| 1206 |
+
# test ndim = 2, rank 1 updates here too
|
| 1207 |
+
for p in [1, 2, 3, 5]:
|
| 1208 |
+
a, q, r, u, v = self.generate('sqr', p=p)
|
| 1209 |
+
if p == 1:
|
| 1210 |
+
u = u.reshape(u.size, 1)
|
| 1211 |
+
v = v.reshape(v.size, 1)
|
| 1212 |
+
q1, r1 = qr_update(q, r, u, v, False)
|
| 1213 |
+
a1 = a + np.dot(u, v.T.conj())
|
| 1214 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 1215 |
+
|
| 1216 |
+
def test_tall_rank_1(self):
|
| 1217 |
+
a, q, r, u, v = self.generate('tall')
|
| 1218 |
+
q1, r1 = qr_update(q, r, u, v, False)
|
| 1219 |
+
a1 = a + np.outer(u, v.conj())
|
| 1220 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 1221 |
+
|
| 1222 |
+
def test_tall_rank_p(self):
|
| 1223 |
+
for p in [1, 2, 3, 5]:
|
| 1224 |
+
a, q, r, u, v = self.generate('tall', p=p)
|
| 1225 |
+
if p == 1:
|
| 1226 |
+
u = u.reshape(u.size, 1)
|
| 1227 |
+
v = v.reshape(v.size, 1)
|
| 1228 |
+
q1, r1 = qr_update(q, r, u, v, False)
|
| 1229 |
+
a1 = a + np.dot(u, v.T.conj())
|
| 1230 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 1231 |
+
|
| 1232 |
+
def test_fat_rank_1(self):
|
| 1233 |
+
a, q, r, u, v = self.generate('fat')
|
| 1234 |
+
q1, r1 = qr_update(q, r, u, v, False)
|
| 1235 |
+
a1 = a + np.outer(u, v.conj())
|
| 1236 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 1237 |
+
|
| 1238 |
+
def test_fat_rank_p(self):
|
| 1239 |
+
for p in [1, 2, 3, 5]:
|
| 1240 |
+
a, q, r, u, v = self.generate('fat', p=p)
|
| 1241 |
+
if p == 1:
|
| 1242 |
+
u = u.reshape(u.size, 1)
|
| 1243 |
+
v = v.reshape(v.size, 1)
|
| 1244 |
+
q1, r1 = qr_update(q, r, u, v, False)
|
| 1245 |
+
a1 = a + np.dot(u, v.T.conj())
|
| 1246 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 1247 |
+
|
| 1248 |
+
def test_economic_rank_1(self):
|
| 1249 |
+
a, q, r, u, v = self.generate('tall', 'economic')
|
| 1250 |
+
q1, r1 = qr_update(q, r, u, v, False)
|
| 1251 |
+
a1 = a + np.outer(u, v.conj())
|
| 1252 |
+
check_qr(q1, r1, a1, self.rtol, self.atol, False)
|
| 1253 |
+
|
| 1254 |
+
def test_economic_rank_p(self):
|
| 1255 |
+
for p in [1, 2, 3, 5]:
|
| 1256 |
+
a, q, r, u, v = self.generate('tall', 'economic', p)
|
| 1257 |
+
if p == 1:
|
| 1258 |
+
u = u.reshape(u.size, 1)
|
| 1259 |
+
v = v.reshape(v.size, 1)
|
| 1260 |
+
q1, r1 = qr_update(q, r, u, v, False)
|
| 1261 |
+
a1 = a + np.dot(u, v.T.conj())
|
| 1262 |
+
check_qr(q1, r1, a1, self.rtol, self.atol, False)
|
| 1263 |
+
|
| 1264 |
+
def test_Mx1_rank_1(self):
|
| 1265 |
+
a, q, r, u, v = self.generate('Mx1')
|
| 1266 |
+
q1, r1 = qr_update(q, r, u, v, False)
|
| 1267 |
+
a1 = a + np.outer(u, v.conj())
|
| 1268 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 1269 |
+
|
| 1270 |
+
def test_Mx1_rank_p(self):
|
| 1271 |
+
# when M or N == 1, only a rank 1 update is allowed. This isn't
|
| 1272 |
+
# fundamental limitation, but the code does not support it.
|
| 1273 |
+
a, q, r, u, v = self.generate('Mx1', p=1)
|
| 1274 |
+
u = u.reshape(u.size, 1)
|
| 1275 |
+
v = v.reshape(v.size, 1)
|
| 1276 |
+
q1, r1 = qr_update(q, r, u, v, False)
|
| 1277 |
+
a1 = a + np.dot(u, v.T.conj())
|
| 1278 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 1279 |
+
|
| 1280 |
+
def test_Mx1_economic_rank_1(self):
|
| 1281 |
+
a, q, r, u, v = self.generate('Mx1', 'economic')
|
| 1282 |
+
q1, r1 = qr_update(q, r, u, v, False)
|
| 1283 |
+
a1 = a + np.outer(u, v.conj())
|
| 1284 |
+
check_qr(q1, r1, a1, self.rtol, self.atol, False)
|
| 1285 |
+
|
| 1286 |
+
def test_Mx1_economic_rank_p(self):
|
| 1287 |
+
# when M or N == 1, only a rank 1 update is allowed. This isn't
|
| 1288 |
+
# fundamental limitation, but the code does not support it.
|
| 1289 |
+
a, q, r, u, v = self.generate('Mx1', 'economic', p=1)
|
| 1290 |
+
u = u.reshape(u.size, 1)
|
| 1291 |
+
v = v.reshape(v.size, 1)
|
| 1292 |
+
q1, r1 = qr_update(q, r, u, v, False)
|
| 1293 |
+
a1 = a + np.dot(u, v.T.conj())
|
| 1294 |
+
check_qr(q1, r1, a1, self.rtol, self.atol, False)
|
| 1295 |
+
|
| 1296 |
+
def test_1xN_rank_1(self):
|
| 1297 |
+
a, q, r, u, v = self.generate('1xN')
|
| 1298 |
+
q1, r1 = qr_update(q, r, u, v, False)
|
| 1299 |
+
a1 = a + np.outer(u, v.conj())
|
| 1300 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 1301 |
+
|
| 1302 |
+
def test_1xN_rank_p(self):
|
| 1303 |
+
# when M or N == 1, only a rank 1 update is allowed. This isn't
|
| 1304 |
+
# fundamental limitation, but the code does not support it.
|
| 1305 |
+
a, q, r, u, v = self.generate('1xN', p=1)
|
| 1306 |
+
u = u.reshape(u.size, 1)
|
| 1307 |
+
v = v.reshape(v.size, 1)
|
| 1308 |
+
q1, r1 = qr_update(q, r, u, v, False)
|
| 1309 |
+
a1 = a + np.dot(u, v.T.conj())
|
| 1310 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 1311 |
+
|
| 1312 |
+
def test_1x1_rank_1(self):
|
| 1313 |
+
a, q, r, u, v = self.generate('1x1')
|
| 1314 |
+
q1, r1 = qr_update(q, r, u, v, False)
|
| 1315 |
+
a1 = a + np.outer(u, v.conj())
|
| 1316 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 1317 |
+
|
| 1318 |
+
def test_1x1_rank_p(self):
|
| 1319 |
+
# when M or N == 1, only a rank 1 update is allowed. This isn't
|
| 1320 |
+
# fundamental limitation, but the code does not support it.
|
| 1321 |
+
a, q, r, u, v = self.generate('1x1', p=1)
|
| 1322 |
+
u = u.reshape(u.size, 1)
|
| 1323 |
+
v = v.reshape(v.size, 1)
|
| 1324 |
+
q1, r1 = qr_update(q, r, u, v, False)
|
| 1325 |
+
a1 = a + np.dot(u, v.T.conj())
|
| 1326 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 1327 |
+
|
| 1328 |
+
def test_1x1_rank_1_scalar(self):
|
| 1329 |
+
a, q, r, u, v = self.generate('1x1')
|
| 1330 |
+
assert_raises(ValueError, qr_update, q[0, 0], r, u, v)
|
| 1331 |
+
assert_raises(ValueError, qr_update, q, r[0, 0], u, v)
|
| 1332 |
+
assert_raises(ValueError, qr_update, q, r, u[0], v)
|
| 1333 |
+
assert_raises(ValueError, qr_update, q, r, u, v[0])
|
| 1334 |
+
|
| 1335 |
+
def base_non_simple_strides(self, adjust_strides, mode, p, overwriteable):
|
| 1336 |
+
assert_sqr = False if mode == 'economic' else True
|
| 1337 |
+
for type in ['sqr', 'tall', 'fat']:
|
| 1338 |
+
a, q0, r0, u0, v0 = self.generate(type, mode, p)
|
| 1339 |
+
qs, rs, us, vs = adjust_strides((q0, r0, u0, v0))
|
| 1340 |
+
if p == 1:
|
| 1341 |
+
aup = a + np.outer(u0, v0.conj())
|
| 1342 |
+
else:
|
| 1343 |
+
aup = a + np.dot(u0, v0.T.conj())
|
| 1344 |
+
|
| 1345 |
+
# for each variable, q, r, u, v we try with it strided and
|
| 1346 |
+
# overwrite=False. Then we try with overwrite=True, and make
|
| 1347 |
+
# sure that if p == 1, r and v are still overwritten.
|
| 1348 |
+
# a strided q and u must always be copied.
|
| 1349 |
+
|
| 1350 |
+
q = q0.copy('F')
|
| 1351 |
+
r = r0.copy('F')
|
| 1352 |
+
u = u0.copy('F')
|
| 1353 |
+
v = v0.copy('C')
|
| 1354 |
+
q1, r1 = qr_update(qs, r, u, v, False)
|
| 1355 |
+
check_qr(q1, r1, aup, self.rtol, self.atol, assert_sqr)
|
| 1356 |
+
q1o, r1o = qr_update(qs, r, u, v, True)
|
| 1357 |
+
check_qr(q1o, r1o, aup, self.rtol, self.atol, assert_sqr)
|
| 1358 |
+
if overwriteable:
|
| 1359 |
+
assert_allclose(r1o, r, rtol=self.rtol, atol=self.atol)
|
| 1360 |
+
assert_allclose(v, v0.conj(), rtol=self.rtol, atol=self.atol)
|
| 1361 |
+
|
| 1362 |
+
q = q0.copy('F')
|
| 1363 |
+
r = r0.copy('F')
|
| 1364 |
+
u = u0.copy('F')
|
| 1365 |
+
v = v0.copy('C')
|
| 1366 |
+
q2, r2 = qr_update(q, rs, u, v, False)
|
| 1367 |
+
check_qr(q2, r2, aup, self.rtol, self.atol, assert_sqr)
|
| 1368 |
+
q2o, r2o = qr_update(q, rs, u, v, True)
|
| 1369 |
+
check_qr(q2o, r2o, aup, self.rtol, self.atol, assert_sqr)
|
| 1370 |
+
if overwriteable:
|
| 1371 |
+
assert_allclose(r2o, rs, rtol=self.rtol, atol=self.atol)
|
| 1372 |
+
assert_allclose(v, v0.conj(), rtol=self.rtol, atol=self.atol)
|
| 1373 |
+
|
| 1374 |
+
q = q0.copy('F')
|
| 1375 |
+
r = r0.copy('F')
|
| 1376 |
+
u = u0.copy('F')
|
| 1377 |
+
v = v0.copy('C')
|
| 1378 |
+
q3, r3 = qr_update(q, r, us, v, False)
|
| 1379 |
+
check_qr(q3, r3, aup, self.rtol, self.atol, assert_sqr)
|
| 1380 |
+
q3o, r3o = qr_update(q, r, us, v, True)
|
| 1381 |
+
check_qr(q3o, r3o, aup, self.rtol, self.atol, assert_sqr)
|
| 1382 |
+
if overwriteable:
|
| 1383 |
+
assert_allclose(r3o, r, rtol=self.rtol, atol=self.atol)
|
| 1384 |
+
assert_allclose(v, v0.conj(), rtol=self.rtol, atol=self.atol)
|
| 1385 |
+
|
| 1386 |
+
q = q0.copy('F')
|
| 1387 |
+
r = r0.copy('F')
|
| 1388 |
+
u = u0.copy('F')
|
| 1389 |
+
v = v0.copy('C')
|
| 1390 |
+
q4, r4 = qr_update(q, r, u, vs, False)
|
| 1391 |
+
check_qr(q4, r4, aup, self.rtol, self.atol, assert_sqr)
|
| 1392 |
+
q4o, r4o = qr_update(q, r, u, vs, True)
|
| 1393 |
+
check_qr(q4o, r4o, aup, self.rtol, self.atol, assert_sqr)
|
| 1394 |
+
if overwriteable:
|
| 1395 |
+
assert_allclose(r4o, r, rtol=self.rtol, atol=self.atol)
|
| 1396 |
+
assert_allclose(vs, v0.conj(), rtol=self.rtol, atol=self.atol)
|
| 1397 |
+
|
| 1398 |
+
q = q0.copy('F')
|
| 1399 |
+
r = r0.copy('F')
|
| 1400 |
+
u = u0.copy('F')
|
| 1401 |
+
v = v0.copy('C')
|
| 1402 |
+
# since some of these were consumed above
|
| 1403 |
+
qs, rs, us, vs = adjust_strides((q, r, u, v))
|
| 1404 |
+
q5, r5 = qr_update(qs, rs, us, vs, False)
|
| 1405 |
+
check_qr(q5, r5, aup, self.rtol, self.atol, assert_sqr)
|
| 1406 |
+
q5o, r5o = qr_update(qs, rs, us, vs, True)
|
| 1407 |
+
check_qr(q5o, r5o, aup, self.rtol, self.atol, assert_sqr)
|
| 1408 |
+
if overwriteable:
|
| 1409 |
+
assert_allclose(r5o, rs, rtol=self.rtol, atol=self.atol)
|
| 1410 |
+
assert_allclose(vs, v0.conj(), rtol=self.rtol, atol=self.atol)
|
| 1411 |
+
|
| 1412 |
+
def test_non_unit_strides_rank_1(self):
|
| 1413 |
+
self.base_non_simple_strides(make_strided, 'full', 1, True)
|
| 1414 |
+
|
| 1415 |
+
def test_non_unit_strides_economic_rank_1(self):
|
| 1416 |
+
self.base_non_simple_strides(make_strided, 'economic', 1, True)
|
| 1417 |
+
|
| 1418 |
+
def test_non_unit_strides_rank_p(self):
|
| 1419 |
+
self.base_non_simple_strides(make_strided, 'full', 3, False)
|
| 1420 |
+
|
| 1421 |
+
def test_non_unit_strides_economic_rank_p(self):
|
| 1422 |
+
self.base_non_simple_strides(make_strided, 'economic', 3, False)
|
| 1423 |
+
|
| 1424 |
+
def test_neg_strides_rank_1(self):
|
| 1425 |
+
self.base_non_simple_strides(negate_strides, 'full', 1, False)
|
| 1426 |
+
|
| 1427 |
+
def test_neg_strides_economic_rank_1(self):
|
| 1428 |
+
self.base_non_simple_strides(negate_strides, 'economic', 1, False)
|
| 1429 |
+
|
| 1430 |
+
def test_neg_strides_rank_p(self):
|
| 1431 |
+
self.base_non_simple_strides(negate_strides, 'full', 3, False)
|
| 1432 |
+
|
| 1433 |
+
def test_neg_strides_economic_rank_p(self):
|
| 1434 |
+
self.base_non_simple_strides(negate_strides, 'economic', 3, False)
|
| 1435 |
+
|
| 1436 |
+
def test_non_itemsize_strides_rank_1(self):
|
| 1437 |
+
self.base_non_simple_strides(nonitemsize_strides, 'full', 1, False)
|
| 1438 |
+
|
| 1439 |
+
def test_non_itemsize_strides_economic_rank_1(self):
|
| 1440 |
+
self.base_non_simple_strides(nonitemsize_strides, 'economic', 1, False)
|
| 1441 |
+
|
| 1442 |
+
def test_non_itemsize_strides_rank_p(self):
|
| 1443 |
+
self.base_non_simple_strides(nonitemsize_strides, 'full', 3, False)
|
| 1444 |
+
|
| 1445 |
+
def test_non_itemsize_strides_economic_rank_p(self):
|
| 1446 |
+
self.base_non_simple_strides(nonitemsize_strides, 'economic', 3, False)
|
| 1447 |
+
|
| 1448 |
+
def test_non_native_byte_order_rank_1(self):
|
| 1449 |
+
self.base_non_simple_strides(make_nonnative, 'full', 1, False)
|
| 1450 |
+
|
| 1451 |
+
def test_non_native_byte_order_economic_rank_1(self):
|
| 1452 |
+
self.base_non_simple_strides(make_nonnative, 'economic', 1, False)
|
| 1453 |
+
|
| 1454 |
+
def test_non_native_byte_order_rank_p(self):
|
| 1455 |
+
self.base_non_simple_strides(make_nonnative, 'full', 3, False)
|
| 1456 |
+
|
| 1457 |
+
def test_non_native_byte_order_economic_rank_p(self):
|
| 1458 |
+
self.base_non_simple_strides(make_nonnative, 'economic', 3, False)
|
| 1459 |
+
|
| 1460 |
+
def test_overwrite_qruv_rank_1(self):
|
| 1461 |
+
# Any positive strided q, r, u, and v can be overwritten for a rank 1
|
| 1462 |
+
# update, only checking C and F contiguous.
|
| 1463 |
+
a, q0, r0, u0, v0 = self.generate('sqr')
|
| 1464 |
+
a1 = a + np.outer(u0, v0.conj())
|
| 1465 |
+
q = q0.copy('F')
|
| 1466 |
+
r = r0.copy('F')
|
| 1467 |
+
u = u0.copy('F')
|
| 1468 |
+
v = v0.copy('F')
|
| 1469 |
+
|
| 1470 |
+
# don't overwrite
|
| 1471 |
+
q1, r1 = qr_update(q, r, u, v, False)
|
| 1472 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 1473 |
+
check_qr(q, r, a, self.rtol, self.atol)
|
| 1474 |
+
|
| 1475 |
+
q2, r2 = qr_update(q, r, u, v, True)
|
| 1476 |
+
check_qr(q2, r2, a1, self.rtol, self.atol)
|
| 1477 |
+
# verify the overwriting, no good way to check u and v.
|
| 1478 |
+
assert_allclose(q2, q, rtol=self.rtol, atol=self.atol)
|
| 1479 |
+
assert_allclose(r2, r, rtol=self.rtol, atol=self.atol)
|
| 1480 |
+
|
| 1481 |
+
q = q0.copy('C')
|
| 1482 |
+
r = r0.copy('C')
|
| 1483 |
+
u = u0.copy('C')
|
| 1484 |
+
v = v0.copy('C')
|
| 1485 |
+
q3, r3 = qr_update(q, r, u, v, True)
|
| 1486 |
+
check_qr(q3, r3, a1, self.rtol, self.atol)
|
| 1487 |
+
assert_allclose(q3, q, rtol=self.rtol, atol=self.atol)
|
| 1488 |
+
assert_allclose(r3, r, rtol=self.rtol, atol=self.atol)
|
| 1489 |
+
|
| 1490 |
+
def test_overwrite_qruv_rank_1_economic(self):
|
| 1491 |
+
# updating economic decompositions can overwrite any contiguous r,
|
| 1492 |
+
# and positively strided r and u. V is only ever read.
|
| 1493 |
+
# only checking C and F contiguous.
|
| 1494 |
+
a, q0, r0, u0, v0 = self.generate('tall', 'economic')
|
| 1495 |
+
a1 = a + np.outer(u0, v0.conj())
|
| 1496 |
+
q = q0.copy('F')
|
| 1497 |
+
r = r0.copy('F')
|
| 1498 |
+
u = u0.copy('F')
|
| 1499 |
+
v = v0.copy('F')
|
| 1500 |
+
|
| 1501 |
+
# don't overwrite
|
| 1502 |
+
q1, r1 = qr_update(q, r, u, v, False)
|
| 1503 |
+
check_qr(q1, r1, a1, self.rtol, self.atol, False)
|
| 1504 |
+
check_qr(q, r, a, self.rtol, self.atol, False)
|
| 1505 |
+
|
| 1506 |
+
q2, r2 = qr_update(q, r, u, v, True)
|
| 1507 |
+
check_qr(q2, r2, a1, self.rtol, self.atol, False)
|
| 1508 |
+
# verify the overwriting, no good way to check u and v.
|
| 1509 |
+
assert_allclose(q2, q, rtol=self.rtol, atol=self.atol)
|
| 1510 |
+
assert_allclose(r2, r, rtol=self.rtol, atol=self.atol)
|
| 1511 |
+
|
| 1512 |
+
q = q0.copy('C')
|
| 1513 |
+
r = r0.copy('C')
|
| 1514 |
+
u = u0.copy('C')
|
| 1515 |
+
v = v0.copy('C')
|
| 1516 |
+
q3, r3 = qr_update(q, r, u, v, True)
|
| 1517 |
+
check_qr(q3, r3, a1, self.rtol, self.atol, False)
|
| 1518 |
+
assert_allclose(q3, q, rtol=self.rtol, atol=self.atol)
|
| 1519 |
+
assert_allclose(r3, r, rtol=self.rtol, atol=self.atol)
|
| 1520 |
+
|
| 1521 |
+
def test_overwrite_qruv_rank_p(self):
|
| 1522 |
+
# for rank p updates, q r must be F contiguous, v must be C (v.T --> F)
|
| 1523 |
+
# and u can be C or F, but is only overwritten if Q is C and complex
|
| 1524 |
+
a, q0, r0, u0, v0 = self.generate('sqr', p=3)
|
| 1525 |
+
a1 = a + np.dot(u0, v0.T.conj())
|
| 1526 |
+
q = q0.copy('F')
|
| 1527 |
+
r = r0.copy('F')
|
| 1528 |
+
u = u0.copy('F')
|
| 1529 |
+
v = v0.copy('C')
|
| 1530 |
+
|
| 1531 |
+
# don't overwrite
|
| 1532 |
+
q1, r1 = qr_update(q, r, u, v, False)
|
| 1533 |
+
check_qr(q1, r1, a1, self.rtol, self.atol)
|
| 1534 |
+
check_qr(q, r, a, self.rtol, self.atol)
|
| 1535 |
+
|
| 1536 |
+
q2, r2 = qr_update(q, r, u, v, True)
|
| 1537 |
+
check_qr(q2, r2, a1, self.rtol, self.atol)
|
| 1538 |
+
# verify the overwriting, no good way to check u and v.
|
| 1539 |
+
assert_allclose(q2, q, rtol=self.rtol, atol=self.atol)
|
| 1540 |
+
assert_allclose(r2, r, rtol=self.rtol, atol=self.atol)
|
| 1541 |
+
|
| 1542 |
+
def test_empty_inputs(self):
|
| 1543 |
+
a, q, r, u, v = self.generate('tall')
|
| 1544 |
+
assert_raises(ValueError, qr_update, np.array([]), r, u, v)
|
| 1545 |
+
assert_raises(ValueError, qr_update, q, np.array([]), u, v)
|
| 1546 |
+
assert_raises(ValueError, qr_update, q, r, np.array([]), v)
|
| 1547 |
+
assert_raises(ValueError, qr_update, q, r, u, np.array([]))
|
| 1548 |
+
|
| 1549 |
+
def test_mismatched_shapes(self):
|
| 1550 |
+
a, q, r, u, v = self.generate('tall')
|
| 1551 |
+
assert_raises(ValueError, qr_update, q, r[1:], u, v)
|
| 1552 |
+
assert_raises(ValueError, qr_update, q[:-2], r, u, v)
|
| 1553 |
+
assert_raises(ValueError, qr_update, q, r, u[1:], v)
|
| 1554 |
+
assert_raises(ValueError, qr_update, q, r, u, v[1:])
|
| 1555 |
+
|
| 1556 |
+
def test_unsupported_dtypes(self):
|
| 1557 |
+
dts = ['int8', 'int16', 'int32', 'int64',
|
| 1558 |
+
'uint8', 'uint16', 'uint32', 'uint64',
|
| 1559 |
+
'float16', 'longdouble', 'clongdouble',
|
| 1560 |
+
'bool']
|
| 1561 |
+
a, q0, r0, u0, v0 = self.generate('tall')
|
| 1562 |
+
for dtype in dts:
|
| 1563 |
+
q = q0.real.astype(dtype)
|
| 1564 |
+
with np.errstate(invalid="ignore"):
|
| 1565 |
+
r = r0.real.astype(dtype)
|
| 1566 |
+
u = u0.real.astype(dtype)
|
| 1567 |
+
v = v0.real.astype(dtype)
|
| 1568 |
+
assert_raises(ValueError, qr_update, q, r0, u0, v0)
|
| 1569 |
+
assert_raises(ValueError, qr_update, q0, r, u0, v0)
|
| 1570 |
+
assert_raises(ValueError, qr_update, q0, r0, u, v0)
|
| 1571 |
+
assert_raises(ValueError, qr_update, q0, r0, u0, v)
|
| 1572 |
+
|
| 1573 |
+
def test_integer_input(self):
|
| 1574 |
+
q = np.arange(16).reshape(4, 4)
|
| 1575 |
+
r = q.copy() # doesn't matter
|
| 1576 |
+
u = q[:, 0].copy()
|
| 1577 |
+
v = r[0, :].copy()
|
| 1578 |
+
assert_raises(ValueError, qr_update, q, r, u, v)
|
| 1579 |
+
|
| 1580 |
+
def test_check_finite(self):
|
| 1581 |
+
a0, q0, r0, u0, v0 = self.generate('tall', p=3)
|
| 1582 |
+
|
| 1583 |
+
q = q0.copy('F')
|
| 1584 |
+
q[1,1] = np.nan
|
| 1585 |
+
assert_raises(ValueError, qr_update, q, r0, u0[:,0], v0[:,0])
|
| 1586 |
+
assert_raises(ValueError, qr_update, q, r0, u0, v0)
|
| 1587 |
+
|
| 1588 |
+
r = r0.copy('F')
|
| 1589 |
+
r[1,1] = np.nan
|
| 1590 |
+
assert_raises(ValueError, qr_update, q0, r, u0[:,0], v0[:,0])
|
| 1591 |
+
assert_raises(ValueError, qr_update, q0, r, u0, v0)
|
| 1592 |
+
|
| 1593 |
+
u = u0.copy('F')
|
| 1594 |
+
u[0,0] = np.nan
|
| 1595 |
+
assert_raises(ValueError, qr_update, q0, r0, u[:,0], v0[:,0])
|
| 1596 |
+
assert_raises(ValueError, qr_update, q0, r0, u, v0)
|
| 1597 |
+
|
| 1598 |
+
v = v0.copy('F')
|
| 1599 |
+
v[0,0] = np.nan
|
| 1600 |
+
assert_raises(ValueError, qr_update, q0, r0, u[:,0], v[:,0])
|
| 1601 |
+
assert_raises(ValueError, qr_update, q0, r0, u, v)
|
| 1602 |
+
|
| 1603 |
+
def test_economic_check_finite(self):
|
| 1604 |
+
a0, q0, r0, u0, v0 = self.generate('tall', mode='economic', p=3)
|
| 1605 |
+
|
| 1606 |
+
q = q0.copy('F')
|
| 1607 |
+
q[1,1] = np.nan
|
| 1608 |
+
assert_raises(ValueError, qr_update, q, r0, u0[:,0], v0[:,0])
|
| 1609 |
+
assert_raises(ValueError, qr_update, q, r0, u0, v0)
|
| 1610 |
+
|
| 1611 |
+
r = r0.copy('F')
|
| 1612 |
+
r[1,1] = np.nan
|
| 1613 |
+
assert_raises(ValueError, qr_update, q0, r, u0[:,0], v0[:,0])
|
| 1614 |
+
assert_raises(ValueError, qr_update, q0, r, u0, v0)
|
| 1615 |
+
|
| 1616 |
+
u = u0.copy('F')
|
| 1617 |
+
u[0,0] = np.nan
|
| 1618 |
+
assert_raises(ValueError, qr_update, q0, r0, u[:,0], v0[:,0])
|
| 1619 |
+
assert_raises(ValueError, qr_update, q0, r0, u, v0)
|
| 1620 |
+
|
| 1621 |
+
v = v0.copy('F')
|
| 1622 |
+
v[0,0] = np.nan
|
| 1623 |
+
assert_raises(ValueError, qr_update, q0, r0, u[:,0], v[:,0])
|
| 1624 |
+
assert_raises(ValueError, qr_update, q0, r0, u, v)
|
| 1625 |
+
|
| 1626 |
+
def test_u_exactly_in_span_q(self):
|
| 1627 |
+
q = np.array([[0, 0], [0, 0], [1, 0], [0, 1]], self.dtype)
|
| 1628 |
+
r = np.array([[1, 0], [0, 1]], self.dtype)
|
| 1629 |
+
u = np.array([0, 0, 0, -1], self.dtype)
|
| 1630 |
+
v = np.array([1, 2], self.dtype)
|
| 1631 |
+
q1, r1 = qr_update(q, r, u, v)
|
| 1632 |
+
a1 = np.dot(q, r) + np.outer(u, v.conj())
|
| 1633 |
+
check_qr(q1, r1, a1, self.rtol, self.atol, False)
|
| 1634 |
+
|
| 1635 |
+
class TestQRupdate_f(BaseQRupdate):
|
| 1636 |
+
dtype = np.dtype('f')
|
| 1637 |
+
|
| 1638 |
+
class TestQRupdate_F(BaseQRupdate):
|
| 1639 |
+
dtype = np.dtype('F')
|
| 1640 |
+
|
| 1641 |
+
class TestQRupdate_d(BaseQRupdate):
|
| 1642 |
+
dtype = np.dtype('d')
|
| 1643 |
+
|
| 1644 |
+
class TestQRupdate_D(BaseQRupdate):
|
| 1645 |
+
dtype = np.dtype('D')
|
| 1646 |
+
|
| 1647 |
+
def test_form_qTu():
|
| 1648 |
+
# We want to ensure that all of the code paths through this function are
|
| 1649 |
+
# tested. Most of them should be hit with the rest of test suite, but
|
| 1650 |
+
# explicit tests make clear precisely what is being tested.
|
| 1651 |
+
#
|
| 1652 |
+
# This function expects that Q is either C or F contiguous and square.
|
| 1653 |
+
# Economic mode decompositions (Q is (M, N), M != N) do not go through this
|
| 1654 |
+
# function. U may have any positive strides.
|
| 1655 |
+
#
|
| 1656 |
+
# Some of these test are duplicates, since contiguous 1d arrays are both C
|
| 1657 |
+
# and F.
|
| 1658 |
+
|
| 1659 |
+
q_order = ['F', 'C']
|
| 1660 |
+
q_shape = [(8, 8), ]
|
| 1661 |
+
u_order = ['F', 'C', 'A'] # here A means is not F not C
|
| 1662 |
+
u_shape = [1, 3]
|
| 1663 |
+
dtype = ['f', 'd', 'F', 'D']
|
| 1664 |
+
|
| 1665 |
+
for qo, qs, uo, us, d in \
|
| 1666 |
+
itertools.product(q_order, q_shape, u_order, u_shape, dtype):
|
| 1667 |
+
if us == 1:
|
| 1668 |
+
check_form_qTu(qo, qs, uo, us, 1, d)
|
| 1669 |
+
check_form_qTu(qo, qs, uo, us, 2, d)
|
| 1670 |
+
else:
|
| 1671 |
+
check_form_qTu(qo, qs, uo, us, 2, d)
|
| 1672 |
+
|
| 1673 |
+
def check_form_qTu(q_order, q_shape, u_order, u_shape, u_ndim, dtype):
|
| 1674 |
+
np.random.seed(47)
|
| 1675 |
+
if u_shape == 1 and u_ndim == 1:
|
| 1676 |
+
u_shape = (q_shape[0],)
|
| 1677 |
+
else:
|
| 1678 |
+
u_shape = (q_shape[0], u_shape)
|
| 1679 |
+
dtype = np.dtype(dtype)
|
| 1680 |
+
|
| 1681 |
+
if dtype.char in 'fd':
|
| 1682 |
+
q = np.random.random(q_shape)
|
| 1683 |
+
u = np.random.random(u_shape)
|
| 1684 |
+
elif dtype.char in 'FD':
|
| 1685 |
+
q = np.random.random(q_shape) + 1j*np.random.random(q_shape)
|
| 1686 |
+
u = np.random.random(u_shape) + 1j*np.random.random(u_shape)
|
| 1687 |
+
else:
|
| 1688 |
+
ValueError("form_qTu doesn't support this dtype")
|
| 1689 |
+
|
| 1690 |
+
q = np.require(q, dtype, q_order)
|
| 1691 |
+
if u_order != 'A':
|
| 1692 |
+
u = np.require(u, dtype, u_order)
|
| 1693 |
+
else:
|
| 1694 |
+
u, = make_strided((u.astype(dtype),))
|
| 1695 |
+
|
| 1696 |
+
rtol = 10.0 ** -(np.finfo(dtype).precision-2)
|
| 1697 |
+
atol = 2*np.finfo(dtype).eps
|
| 1698 |
+
|
| 1699 |
+
expected = np.dot(q.T.conj(), u)
|
| 1700 |
+
res = _decomp_update._form_qTu(q, u)
|
| 1701 |
+
assert_allclose(res, expected, rtol=rtol, atol=atol)
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_extending.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import platform
|
| 3 |
+
import sysconfig
|
| 4 |
+
|
| 5 |
+
import numpy as np
|
| 6 |
+
import pytest
|
| 7 |
+
|
| 8 |
+
from scipy._lib._testutils import IS_EDITABLE, _test_cython_extension, cython
|
| 9 |
+
from scipy.linalg.blas import cdotu # type: ignore[attr-defined]
|
| 10 |
+
from scipy.linalg.lapack import dgtsv # type: ignore[attr-defined]
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
@pytest.mark.fail_slow(120)
|
| 14 |
+
# essential per https://github.com/scipy/scipy/pull/20487#discussion_r1567057247
|
| 15 |
+
@pytest.mark.skipif(IS_EDITABLE,
|
| 16 |
+
reason='Editable install cannot find .pxd headers.')
|
| 17 |
+
@pytest.mark.skipif((platform.system() == 'Windows' and
|
| 18 |
+
sysconfig.get_config_var('Py_GIL_DISABLED')),
|
| 19 |
+
reason='gh-22039')
|
| 20 |
+
@pytest.mark.skipif(platform.machine() in ["wasm32", "wasm64"],
|
| 21 |
+
reason="Can't start subprocess")
|
| 22 |
+
@pytest.mark.skipif(cython is None, reason="requires cython")
|
| 23 |
+
def test_cython(tmp_path):
|
| 24 |
+
srcdir = os.path.dirname(os.path.dirname(__file__))
|
| 25 |
+
extensions, extensions_cpp = _test_cython_extension(tmp_path, srcdir)
|
| 26 |
+
# actually test the cython c-extensions
|
| 27 |
+
a = np.ones(8) * 3
|
| 28 |
+
b = np.ones(9)
|
| 29 |
+
c = np.ones(8) * 4
|
| 30 |
+
x = np.ones(9)
|
| 31 |
+
_, _, _, x, _ = dgtsv(a, b, c, x)
|
| 32 |
+
a = np.ones(8) * 3
|
| 33 |
+
b = np.ones(9)
|
| 34 |
+
c = np.ones(8) * 4
|
| 35 |
+
x_c = np.ones(9)
|
| 36 |
+
extensions.tridiag(a, b, c, x_c)
|
| 37 |
+
a = np.ones(8) * 3
|
| 38 |
+
b = np.ones(9)
|
| 39 |
+
c = np.ones(8) * 4
|
| 40 |
+
x_cpp = np.ones(9)
|
| 41 |
+
extensions_cpp.tridiag(a, b, c, x_cpp)
|
| 42 |
+
np.testing.assert_array_equal(x, x_cpp)
|
| 43 |
+
cx = np.array([1-1j, 2+2j, 3-3j], dtype=np.complex64)
|
| 44 |
+
cy = np.array([4+4j, 5-5j, 6+6j], dtype=np.complex64)
|
| 45 |
+
np.testing.assert_array_equal(cdotu(cx, cy), extensions.complex_dot(cx, cy))
|
| 46 |
+
np.testing.assert_array_equal(cdotu(cx, cy), extensions_cpp.complex_dot(cx, cy))
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_fblas.py
ADDED
|
@@ -0,0 +1,607 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
| 1 |
+
# Test interfaces to fortran blas.
|
| 2 |
+
#
|
| 3 |
+
# The tests are more of interface than they are of the underlying blas.
|
| 4 |
+
# Only very small matrices checked -- N=3 or so.
|
| 5 |
+
#
|
| 6 |
+
# !! Complex calculations really aren't checked that carefully.
|
| 7 |
+
# !! Only real valued complex numbers are used in tests.
|
| 8 |
+
|
| 9 |
+
from numpy import float32, float64, complex64, complex128, arange, array, \
|
| 10 |
+
zeros, shape, transpose, newaxis, common_type, conjugate
|
| 11 |
+
|
| 12 |
+
from scipy.linalg import _fblas as fblas
|
| 13 |
+
|
| 14 |
+
from numpy.testing import assert_array_equal, \
|
| 15 |
+
assert_allclose, assert_array_almost_equal, assert_
|
| 16 |
+
|
| 17 |
+
import pytest
|
| 18 |
+
|
| 19 |
+
# decimal accuracy to require between Python and LAPACK/BLAS calculations
|
| 20 |
+
accuracy = 5
|
| 21 |
+
|
| 22 |
+
# Since numpy.dot likely uses the same blas, use this routine
|
| 23 |
+
# to check.
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def matrixmultiply(a, b):
|
| 27 |
+
if len(b.shape) == 1:
|
| 28 |
+
b_is_vector = True
|
| 29 |
+
b = b[:, newaxis]
|
| 30 |
+
else:
|
| 31 |
+
b_is_vector = False
|
| 32 |
+
assert_(a.shape[1] == b.shape[0])
|
| 33 |
+
c = zeros((a.shape[0], b.shape[1]), common_type(a, b))
|
| 34 |
+
for i in range(a.shape[0]):
|
| 35 |
+
for j in range(b.shape[1]):
|
| 36 |
+
s = 0
|
| 37 |
+
for k in range(a.shape[1]):
|
| 38 |
+
s += a[i, k] * b[k, j]
|
| 39 |
+
c[i, j] = s
|
| 40 |
+
if b_is_vector:
|
| 41 |
+
c = c.reshape((a.shape[0],))
|
| 42 |
+
return c
|
| 43 |
+
|
| 44 |
+
##################################################
|
| 45 |
+
# Test blas ?axpy
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
class BaseAxpy:
|
| 49 |
+
''' Mixin class for axpy tests '''
|
| 50 |
+
|
| 51 |
+
def test_default_a(self):
|
| 52 |
+
x = arange(3., dtype=self.dtype)
|
| 53 |
+
y = arange(3., dtype=x.dtype)
|
| 54 |
+
real_y = x*1.+y
|
| 55 |
+
y = self.blas_func(x, y)
|
| 56 |
+
assert_array_equal(real_y, y)
|
| 57 |
+
|
| 58 |
+
def test_simple(self):
|
| 59 |
+
x = arange(3., dtype=self.dtype)
|
| 60 |
+
y = arange(3., dtype=x.dtype)
|
| 61 |
+
real_y = x*3.+y
|
| 62 |
+
y = self.blas_func(x, y, a=3.)
|
| 63 |
+
assert_array_equal(real_y, y)
|
| 64 |
+
|
| 65 |
+
def test_x_stride(self):
|
| 66 |
+
x = arange(6., dtype=self.dtype)
|
| 67 |
+
y = zeros(3, x.dtype)
|
| 68 |
+
y = arange(3., dtype=x.dtype)
|
| 69 |
+
real_y = x[::2]*3.+y
|
| 70 |
+
y = self.blas_func(x, y, a=3., n=3, incx=2)
|
| 71 |
+
assert_array_equal(real_y, y)
|
| 72 |
+
|
| 73 |
+
def test_y_stride(self):
|
| 74 |
+
x = arange(3., dtype=self.dtype)
|
| 75 |
+
y = zeros(6, x.dtype)
|
| 76 |
+
real_y = x*3.+y[::2]
|
| 77 |
+
y = self.blas_func(x, y, a=3., n=3, incy=2)
|
| 78 |
+
assert_array_equal(real_y, y[::2])
|
| 79 |
+
|
| 80 |
+
def test_x_and_y_stride(self):
|
| 81 |
+
x = arange(12., dtype=self.dtype)
|
| 82 |
+
y = zeros(6, x.dtype)
|
| 83 |
+
real_y = x[::4]*3.+y[::2]
|
| 84 |
+
y = self.blas_func(x, y, a=3., n=3, incx=4, incy=2)
|
| 85 |
+
assert_array_equal(real_y, y[::2])
|
| 86 |
+
|
| 87 |
+
def test_x_bad_size(self):
|
| 88 |
+
x = arange(12., dtype=self.dtype)
|
| 89 |
+
y = zeros(6, x.dtype)
|
| 90 |
+
with pytest.raises(Exception, match='failed for 1st keyword'):
|
| 91 |
+
self.blas_func(x, y, n=4, incx=5)
|
| 92 |
+
|
| 93 |
+
def test_y_bad_size(self):
|
| 94 |
+
x = arange(12., dtype=self.dtype)
|
| 95 |
+
y = zeros(6, x.dtype)
|
| 96 |
+
with pytest.raises(Exception, match='failed for 1st keyword'):
|
| 97 |
+
self.blas_func(x, y, n=3, incy=5)
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
try:
|
| 101 |
+
class TestSaxpy(BaseAxpy):
|
| 102 |
+
blas_func = fblas.saxpy
|
| 103 |
+
dtype = float32
|
| 104 |
+
except AttributeError:
|
| 105 |
+
class TestSaxpy:
|
| 106 |
+
pass
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
class TestDaxpy(BaseAxpy):
|
| 110 |
+
blas_func = fblas.daxpy
|
| 111 |
+
dtype = float64
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
try:
|
| 115 |
+
class TestCaxpy(BaseAxpy):
|
| 116 |
+
blas_func = fblas.caxpy
|
| 117 |
+
dtype = complex64
|
| 118 |
+
except AttributeError:
|
| 119 |
+
class TestCaxpy:
|
| 120 |
+
pass
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
class TestZaxpy(BaseAxpy):
|
| 124 |
+
blas_func = fblas.zaxpy
|
| 125 |
+
dtype = complex128
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
##################################################
|
| 129 |
+
# Test blas ?scal
|
| 130 |
+
|
| 131 |
+
class BaseScal:
|
| 132 |
+
''' Mixin class for scal testing '''
|
| 133 |
+
|
| 134 |
+
def test_simple(self):
|
| 135 |
+
x = arange(3., dtype=self.dtype)
|
| 136 |
+
real_x = x*3.
|
| 137 |
+
x = self.blas_func(3., x)
|
| 138 |
+
assert_array_equal(real_x, x)
|
| 139 |
+
|
| 140 |
+
def test_x_stride(self):
|
| 141 |
+
x = arange(6., dtype=self.dtype)
|
| 142 |
+
real_x = x.copy()
|
| 143 |
+
real_x[::2] = x[::2]*array(3., self.dtype)
|
| 144 |
+
x = self.blas_func(3., x, n=3, incx=2)
|
| 145 |
+
assert_array_equal(real_x, x)
|
| 146 |
+
|
| 147 |
+
def test_x_bad_size(self):
|
| 148 |
+
x = arange(12., dtype=self.dtype)
|
| 149 |
+
with pytest.raises(Exception, match='failed for 1st keyword'):
|
| 150 |
+
self.blas_func(2., x, n=4, incx=5)
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
try:
|
| 154 |
+
class TestSscal(BaseScal):
|
| 155 |
+
blas_func = fblas.sscal
|
| 156 |
+
dtype = float32
|
| 157 |
+
except AttributeError:
|
| 158 |
+
class TestSscal:
|
| 159 |
+
pass
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
class TestDscal(BaseScal):
|
| 163 |
+
blas_func = fblas.dscal
|
| 164 |
+
dtype = float64
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
try:
|
| 168 |
+
class TestCscal(BaseScal):
|
| 169 |
+
blas_func = fblas.cscal
|
| 170 |
+
dtype = complex64
|
| 171 |
+
except AttributeError:
|
| 172 |
+
class TestCscal:
|
| 173 |
+
pass
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
class TestZscal(BaseScal):
|
| 177 |
+
blas_func = fblas.zscal
|
| 178 |
+
dtype = complex128
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
##################################################
|
| 182 |
+
# Test blas ?copy
|
| 183 |
+
|
| 184 |
+
class BaseCopy:
|
| 185 |
+
''' Mixin class for copy testing '''
|
| 186 |
+
|
| 187 |
+
def test_simple(self):
|
| 188 |
+
x = arange(3., dtype=self.dtype)
|
| 189 |
+
y = zeros(shape(x), x.dtype)
|
| 190 |
+
y = self.blas_func(x, y)
|
| 191 |
+
assert_array_equal(x, y)
|
| 192 |
+
|
| 193 |
+
def test_x_stride(self):
|
| 194 |
+
x = arange(6., dtype=self.dtype)
|
| 195 |
+
y = zeros(3, x.dtype)
|
| 196 |
+
y = self.blas_func(x, y, n=3, incx=2)
|
| 197 |
+
assert_array_equal(x[::2], y)
|
| 198 |
+
|
| 199 |
+
def test_y_stride(self):
|
| 200 |
+
x = arange(3., dtype=self.dtype)
|
| 201 |
+
y = zeros(6, x.dtype)
|
| 202 |
+
y = self.blas_func(x, y, n=3, incy=2)
|
| 203 |
+
assert_array_equal(x, y[::2])
|
| 204 |
+
|
| 205 |
+
def test_x_and_y_stride(self):
|
| 206 |
+
x = arange(12., dtype=self.dtype)
|
| 207 |
+
y = zeros(6, x.dtype)
|
| 208 |
+
y = self.blas_func(x, y, n=3, incx=4, incy=2)
|
| 209 |
+
assert_array_equal(x[::4], y[::2])
|
| 210 |
+
|
| 211 |
+
def test_x_bad_size(self):
|
| 212 |
+
x = arange(12., dtype=self.dtype)
|
| 213 |
+
y = zeros(6, x.dtype)
|
| 214 |
+
with pytest.raises(Exception, match='failed for 1st keyword'):
|
| 215 |
+
self.blas_func(x, y, n=4, incx=5)
|
| 216 |
+
|
| 217 |
+
def test_y_bad_size(self):
|
| 218 |
+
x = arange(12., dtype=self.dtype)
|
| 219 |
+
y = zeros(6, x.dtype)
|
| 220 |
+
with pytest.raises(Exception, match='failed for 1st keyword'):
|
| 221 |
+
self.blas_func(x, y, n=3, incy=5)
|
| 222 |
+
|
| 223 |
+
# def test_y_bad_type(self):
|
| 224 |
+
## Hmmm. Should this work? What should be the output.
|
| 225 |
+
# x = arange(3.,dtype=self.dtype)
|
| 226 |
+
# y = zeros(shape(x))
|
| 227 |
+
# self.blas_func(x,y)
|
| 228 |
+
# assert_array_equal(x,y)
|
| 229 |
+
|
| 230 |
+
|
| 231 |
+
try:
|
| 232 |
+
class TestScopy(BaseCopy):
|
| 233 |
+
blas_func = fblas.scopy
|
| 234 |
+
dtype = float32
|
| 235 |
+
except AttributeError:
|
| 236 |
+
class TestScopy:
|
| 237 |
+
pass
|
| 238 |
+
|
| 239 |
+
|
| 240 |
+
class TestDcopy(BaseCopy):
|
| 241 |
+
blas_func = fblas.dcopy
|
| 242 |
+
dtype = float64
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
try:
|
| 246 |
+
class TestCcopy(BaseCopy):
|
| 247 |
+
blas_func = fblas.ccopy
|
| 248 |
+
dtype = complex64
|
| 249 |
+
except AttributeError:
|
| 250 |
+
class TestCcopy:
|
| 251 |
+
pass
|
| 252 |
+
|
| 253 |
+
|
| 254 |
+
class TestZcopy(BaseCopy):
|
| 255 |
+
blas_func = fblas.zcopy
|
| 256 |
+
dtype = complex128
|
| 257 |
+
|
| 258 |
+
|
| 259 |
+
##################################################
|
| 260 |
+
# Test blas ?swap
|
| 261 |
+
|
| 262 |
+
class BaseSwap:
|
| 263 |
+
''' Mixin class for swap tests '''
|
| 264 |
+
|
| 265 |
+
def test_simple(self):
|
| 266 |
+
x = arange(3., dtype=self.dtype)
|
| 267 |
+
y = zeros(shape(x), x.dtype)
|
| 268 |
+
desired_x = y.copy()
|
| 269 |
+
desired_y = x.copy()
|
| 270 |
+
x, y = self.blas_func(x, y)
|
| 271 |
+
assert_array_equal(desired_x, x)
|
| 272 |
+
assert_array_equal(desired_y, y)
|
| 273 |
+
|
| 274 |
+
def test_x_stride(self):
|
| 275 |
+
x = arange(6., dtype=self.dtype)
|
| 276 |
+
y = zeros(3, x.dtype)
|
| 277 |
+
desired_x = y.copy()
|
| 278 |
+
desired_y = x.copy()[::2]
|
| 279 |
+
x, y = self.blas_func(x, y, n=3, incx=2)
|
| 280 |
+
assert_array_equal(desired_x, x[::2])
|
| 281 |
+
assert_array_equal(desired_y, y)
|
| 282 |
+
|
| 283 |
+
def test_y_stride(self):
|
| 284 |
+
x = arange(3., dtype=self.dtype)
|
| 285 |
+
y = zeros(6, x.dtype)
|
| 286 |
+
desired_x = y.copy()[::2]
|
| 287 |
+
desired_y = x.copy()
|
| 288 |
+
x, y = self.blas_func(x, y, n=3, incy=2)
|
| 289 |
+
assert_array_equal(desired_x, x)
|
| 290 |
+
assert_array_equal(desired_y, y[::2])
|
| 291 |
+
|
| 292 |
+
def test_x_and_y_stride(self):
|
| 293 |
+
x = arange(12., dtype=self.dtype)
|
| 294 |
+
y = zeros(6, x.dtype)
|
| 295 |
+
desired_x = y.copy()[::2]
|
| 296 |
+
desired_y = x.copy()[::4]
|
| 297 |
+
x, y = self.blas_func(x, y, n=3, incx=4, incy=2)
|
| 298 |
+
assert_array_equal(desired_x, x[::4])
|
| 299 |
+
assert_array_equal(desired_y, y[::2])
|
| 300 |
+
|
| 301 |
+
def test_x_bad_size(self):
|
| 302 |
+
x = arange(12., dtype=self.dtype)
|
| 303 |
+
y = zeros(6, x.dtype)
|
| 304 |
+
with pytest.raises(Exception, match='failed for 1st keyword'):
|
| 305 |
+
self.blas_func(x, y, n=4, incx=5)
|
| 306 |
+
|
| 307 |
+
def test_y_bad_size(self):
|
| 308 |
+
x = arange(12., dtype=self.dtype)
|
| 309 |
+
y = zeros(6, x.dtype)
|
| 310 |
+
with pytest.raises(Exception, match='failed for 1st keyword'):
|
| 311 |
+
self.blas_func(x, y, n=3, incy=5)
|
| 312 |
+
|
| 313 |
+
|
| 314 |
+
try:
|
| 315 |
+
class TestSswap(BaseSwap):
|
| 316 |
+
blas_func = fblas.sswap
|
| 317 |
+
dtype = float32
|
| 318 |
+
except AttributeError:
|
| 319 |
+
class TestSswap:
|
| 320 |
+
pass
|
| 321 |
+
|
| 322 |
+
|
| 323 |
+
class TestDswap(BaseSwap):
|
| 324 |
+
blas_func = fblas.dswap
|
| 325 |
+
dtype = float64
|
| 326 |
+
|
| 327 |
+
|
| 328 |
+
try:
|
| 329 |
+
class TestCswap(BaseSwap):
|
| 330 |
+
blas_func = fblas.cswap
|
| 331 |
+
dtype = complex64
|
| 332 |
+
except AttributeError:
|
| 333 |
+
class TestCswap:
|
| 334 |
+
pass
|
| 335 |
+
|
| 336 |
+
|
| 337 |
+
class TestZswap(BaseSwap):
|
| 338 |
+
blas_func = fblas.zswap
|
| 339 |
+
dtype = complex128
|
| 340 |
+
|
| 341 |
+
##################################################
|
| 342 |
+
# Test blas ?gemv
|
| 343 |
+
# This will be a mess to test all cases.
|
| 344 |
+
|
| 345 |
+
|
| 346 |
+
class BaseGemv:
|
| 347 |
+
''' Mixin class for gemv tests '''
|
| 348 |
+
|
| 349 |
+
def get_data(self, x_stride=1, y_stride=1):
|
| 350 |
+
mult = array(1, dtype=self.dtype)
|
| 351 |
+
if self.dtype in [complex64, complex128]:
|
| 352 |
+
mult = array(1+1j, dtype=self.dtype)
|
| 353 |
+
from numpy.random import normal, seed
|
| 354 |
+
seed(1234)
|
| 355 |
+
alpha = array(1., dtype=self.dtype) * mult
|
| 356 |
+
beta = array(1., dtype=self.dtype) * mult
|
| 357 |
+
a = normal(0., 1., (3, 3)).astype(self.dtype) * mult
|
| 358 |
+
x = arange(shape(a)[0]*x_stride, dtype=self.dtype) * mult
|
| 359 |
+
y = arange(shape(a)[1]*y_stride, dtype=self.dtype) * mult
|
| 360 |
+
return alpha, beta, a, x, y
|
| 361 |
+
|
| 362 |
+
def test_simple(self):
|
| 363 |
+
alpha, beta, a, x, y = self.get_data()
|
| 364 |
+
desired_y = alpha*matrixmultiply(a, x)+beta*y
|
| 365 |
+
y = self.blas_func(alpha, a, x, beta, y)
|
| 366 |
+
assert_array_almost_equal(desired_y, y)
|
| 367 |
+
|
| 368 |
+
def test_default_beta_y(self):
|
| 369 |
+
alpha, beta, a, x, y = self.get_data()
|
| 370 |
+
desired_y = matrixmultiply(a, x)
|
| 371 |
+
y = self.blas_func(1, a, x)
|
| 372 |
+
assert_array_almost_equal(desired_y, y)
|
| 373 |
+
|
| 374 |
+
def test_simple_transpose(self):
|
| 375 |
+
alpha, beta, a, x, y = self.get_data()
|
| 376 |
+
desired_y = alpha*matrixmultiply(transpose(a), x)+beta*y
|
| 377 |
+
y = self.blas_func(alpha, a, x, beta, y, trans=1)
|
| 378 |
+
assert_array_almost_equal(desired_y, y)
|
| 379 |
+
|
| 380 |
+
def test_simple_transpose_conj(self):
|
| 381 |
+
alpha, beta, a, x, y = self.get_data()
|
| 382 |
+
desired_y = alpha*matrixmultiply(transpose(conjugate(a)), x)+beta*y
|
| 383 |
+
y = self.blas_func(alpha, a, x, beta, y, trans=2)
|
| 384 |
+
assert_array_almost_equal(desired_y, y)
|
| 385 |
+
|
| 386 |
+
def test_x_stride(self):
|
| 387 |
+
alpha, beta, a, x, y = self.get_data(x_stride=2)
|
| 388 |
+
desired_y = alpha*matrixmultiply(a, x[::2])+beta*y
|
| 389 |
+
y = self.blas_func(alpha, a, x, beta, y, incx=2)
|
| 390 |
+
assert_array_almost_equal(desired_y, y)
|
| 391 |
+
|
| 392 |
+
def test_x_stride_transpose(self):
|
| 393 |
+
alpha, beta, a, x, y = self.get_data(x_stride=2)
|
| 394 |
+
desired_y = alpha*matrixmultiply(transpose(a), x[::2])+beta*y
|
| 395 |
+
y = self.blas_func(alpha, a, x, beta, y, trans=1, incx=2)
|
| 396 |
+
assert_array_almost_equal(desired_y, y)
|
| 397 |
+
|
| 398 |
+
def test_x_stride_assert(self):
|
| 399 |
+
# What is the use of this test?
|
| 400 |
+
alpha, beta, a, x, y = self.get_data(x_stride=2)
|
| 401 |
+
with pytest.raises(Exception, match='failed for 3rd argument'):
|
| 402 |
+
y = self.blas_func(1, a, x, 1, y, trans=0, incx=3)
|
| 403 |
+
with pytest.raises(Exception, match='failed for 3rd argument'):
|
| 404 |
+
y = self.blas_func(1, a, x, 1, y, trans=1, incx=3)
|
| 405 |
+
|
| 406 |
+
def test_y_stride(self):
|
| 407 |
+
alpha, beta, a, x, y = self.get_data(y_stride=2)
|
| 408 |
+
desired_y = y.copy()
|
| 409 |
+
desired_y[::2] = alpha*matrixmultiply(a, x)+beta*y[::2]
|
| 410 |
+
y = self.blas_func(alpha, a, x, beta, y, incy=2)
|
| 411 |
+
assert_array_almost_equal(desired_y, y)
|
| 412 |
+
|
| 413 |
+
def test_y_stride_transpose(self):
|
| 414 |
+
alpha, beta, a, x, y = self.get_data(y_stride=2)
|
| 415 |
+
desired_y = y.copy()
|
| 416 |
+
desired_y[::2] = alpha*matrixmultiply(transpose(a), x)+beta*y[::2]
|
| 417 |
+
y = self.blas_func(alpha, a, x, beta, y, trans=1, incy=2)
|
| 418 |
+
assert_array_almost_equal(desired_y, y)
|
| 419 |
+
|
| 420 |
+
def test_y_stride_assert(self):
|
| 421 |
+
# What is the use of this test?
|
| 422 |
+
alpha, beta, a, x, y = self.get_data(y_stride=2)
|
| 423 |
+
with pytest.raises(Exception, match='failed for 2nd keyword'):
|
| 424 |
+
y = self.blas_func(1, a, x, 1, y, trans=0, incy=3)
|
| 425 |
+
with pytest.raises(Exception, match='failed for 2nd keyword'):
|
| 426 |
+
y = self.blas_func(1, a, x, 1, y, trans=1, incy=3)
|
| 427 |
+
|
| 428 |
+
|
| 429 |
+
try:
|
| 430 |
+
class TestSgemv(BaseGemv):
|
| 431 |
+
blas_func = fblas.sgemv
|
| 432 |
+
dtype = float32
|
| 433 |
+
|
| 434 |
+
def test_sgemv_on_osx(self):
|
| 435 |
+
from itertools import product
|
| 436 |
+
import sys
|
| 437 |
+
import numpy as np
|
| 438 |
+
|
| 439 |
+
if sys.platform != 'darwin':
|
| 440 |
+
return
|
| 441 |
+
|
| 442 |
+
def aligned_array(shape, align, dtype, order='C'):
|
| 443 |
+
# Make array shape `shape` with aligned at `align` bytes
|
| 444 |
+
d = dtype()
|
| 445 |
+
# Make array of correct size with `align` extra bytes
|
| 446 |
+
N = np.prod(shape)
|
| 447 |
+
tmp = np.zeros(N * d.nbytes + align, dtype=np.uint8)
|
| 448 |
+
address = tmp.__array_interface__["data"][0]
|
| 449 |
+
# Find offset into array giving desired alignment
|
| 450 |
+
for offset in range(align):
|
| 451 |
+
if (address + offset) % align == 0:
|
| 452 |
+
break
|
| 453 |
+
tmp = tmp[offset:offset+N*d.nbytes].view(dtype=dtype)
|
| 454 |
+
return tmp.reshape(shape, order=order)
|
| 455 |
+
|
| 456 |
+
def as_aligned(arr, align, dtype, order='C'):
|
| 457 |
+
# Copy `arr` into an aligned array with same shape
|
| 458 |
+
aligned = aligned_array(arr.shape, align, dtype, order)
|
| 459 |
+
aligned[:] = arr[:]
|
| 460 |
+
return aligned
|
| 461 |
+
|
| 462 |
+
def assert_dot_close(A, X, desired):
|
| 463 |
+
assert_allclose(self.blas_func(1.0, A, X), desired,
|
| 464 |
+
rtol=1e-5, atol=1e-7)
|
| 465 |
+
|
| 466 |
+
testdata = product((15, 32), (10000,), (200, 89), ('C', 'F'))
|
| 467 |
+
for align, m, n, a_order in testdata:
|
| 468 |
+
A_d = np.random.rand(m, n)
|
| 469 |
+
X_d = np.random.rand(n)
|
| 470 |
+
desired = np.dot(A_d, X_d)
|
| 471 |
+
# Calculation with aligned single precision
|
| 472 |
+
A_f = as_aligned(A_d, align, np.float32, order=a_order)
|
| 473 |
+
X_f = as_aligned(X_d, align, np.float32, order=a_order)
|
| 474 |
+
assert_dot_close(A_f, X_f, desired)
|
| 475 |
+
|
| 476 |
+
except AttributeError:
|
| 477 |
+
class TestSgemv:
|
| 478 |
+
pass
|
| 479 |
+
|
| 480 |
+
|
| 481 |
+
class TestDgemv(BaseGemv):
|
| 482 |
+
blas_func = fblas.dgemv
|
| 483 |
+
dtype = float64
|
| 484 |
+
|
| 485 |
+
|
| 486 |
+
try:
|
| 487 |
+
class TestCgemv(BaseGemv):
|
| 488 |
+
blas_func = fblas.cgemv
|
| 489 |
+
dtype = complex64
|
| 490 |
+
except AttributeError:
|
| 491 |
+
class TestCgemv:
|
| 492 |
+
pass
|
| 493 |
+
|
| 494 |
+
|
| 495 |
+
class TestZgemv(BaseGemv):
|
| 496 |
+
blas_func = fblas.zgemv
|
| 497 |
+
dtype = complex128
|
| 498 |
+
|
| 499 |
+
|
| 500 |
+
"""
|
| 501 |
+
##################################################
|
| 502 |
+
### Test blas ?ger
|
| 503 |
+
### This will be a mess to test all cases.
|
| 504 |
+
|
| 505 |
+
class BaseGer:
|
| 506 |
+
def get_data(self,x_stride=1,y_stride=1):
|
| 507 |
+
from numpy.random import normal, seed
|
| 508 |
+
seed(1234)
|
| 509 |
+
alpha = array(1., dtype = self.dtype)
|
| 510 |
+
a = normal(0.,1.,(3,3)).astype(self.dtype)
|
| 511 |
+
x = arange(shape(a)[0]*x_stride,dtype=self.dtype)
|
| 512 |
+
y = arange(shape(a)[1]*y_stride,dtype=self.dtype)
|
| 513 |
+
return alpha,a,x,y
|
| 514 |
+
def test_simple(self):
|
| 515 |
+
alpha,a,x,y = self.get_data()
|
| 516 |
+
# transpose takes care of Fortran vs. C(and Python) memory layout
|
| 517 |
+
desired_a = alpha*transpose(x[:,newaxis]*y) + a
|
| 518 |
+
self.blas_func(x,y,a)
|
| 519 |
+
assert_array_almost_equal(desired_a,a)
|
| 520 |
+
def test_x_stride(self):
|
| 521 |
+
alpha,a,x,y = self.get_data(x_stride=2)
|
| 522 |
+
desired_a = alpha*transpose(x[::2,newaxis]*y) + a
|
| 523 |
+
self.blas_func(x,y,a,incx=2)
|
| 524 |
+
assert_array_almost_equal(desired_a,a)
|
| 525 |
+
def test_x_stride_assert(self):
|
| 526 |
+
alpha,a,x,y = self.get_data(x_stride=2)
|
| 527 |
+
with pytest.raises(ValueError, match='foo'):
|
| 528 |
+
self.blas_func(x,y,a,incx=3)
|
| 529 |
+
def test_y_stride(self):
|
| 530 |
+
alpha,a,x,y = self.get_data(y_stride=2)
|
| 531 |
+
desired_a = alpha*transpose(x[:,newaxis]*y[::2]) + a
|
| 532 |
+
self.blas_func(x,y,a,incy=2)
|
| 533 |
+
assert_array_almost_equal(desired_a,a)
|
| 534 |
+
|
| 535 |
+
def test_y_stride_assert(self):
|
| 536 |
+
alpha,a,x,y = self.get_data(y_stride=2)
|
| 537 |
+
with pytest.raises(ValueError, match='foo'):
|
| 538 |
+
self.blas_func(a,x,y,incy=3)
|
| 539 |
+
|
| 540 |
+
class TestSger(BaseGer):
|
| 541 |
+
blas_func = fblas.sger
|
| 542 |
+
dtype = float32
|
| 543 |
+
class TestDger(BaseGer):
|
| 544 |
+
blas_func = fblas.dger
|
| 545 |
+
dtype = float64
|
| 546 |
+
"""
|
| 547 |
+
##################################################
|
| 548 |
+
# Test blas ?gerc
|
| 549 |
+
# This will be a mess to test all cases.
|
| 550 |
+
|
| 551 |
+
"""
|
| 552 |
+
class BaseGerComplex(BaseGer):
|
| 553 |
+
def get_data(self,x_stride=1,y_stride=1):
|
| 554 |
+
from numpy.random import normal, seed
|
| 555 |
+
seed(1234)
|
| 556 |
+
alpha = array(1+1j, dtype = self.dtype)
|
| 557 |
+
a = normal(0.,1.,(3,3)).astype(self.dtype)
|
| 558 |
+
a = a + normal(0.,1.,(3,3)) * array(1j, dtype = self.dtype)
|
| 559 |
+
x = normal(0.,1.,shape(a)[0]*x_stride).astype(self.dtype)
|
| 560 |
+
x = x + x * array(1j, dtype = self.dtype)
|
| 561 |
+
y = normal(0.,1.,shape(a)[1]*y_stride).astype(self.dtype)
|
| 562 |
+
y = y + y * array(1j, dtype = self.dtype)
|
| 563 |
+
return alpha,a,x,y
|
| 564 |
+
def test_simple(self):
|
| 565 |
+
alpha,a,x,y = self.get_data()
|
| 566 |
+
# transpose takes care of Fortran vs. C(and Python) memory layout
|
| 567 |
+
a = a * array(0.,dtype = self.dtype)
|
| 568 |
+
#desired_a = alpha*transpose(x[:,newaxis]*self.transform(y)) + a
|
| 569 |
+
desired_a = alpha*transpose(x[:,newaxis]*y) + a
|
| 570 |
+
#self.blas_func(x,y,a,alpha = alpha)
|
| 571 |
+
fblas.cgeru(x,y,a,alpha = alpha)
|
| 572 |
+
assert_array_almost_equal(desired_a,a)
|
| 573 |
+
|
| 574 |
+
#def test_x_stride(self):
|
| 575 |
+
# alpha,a,x,y = self.get_data(x_stride=2)
|
| 576 |
+
# desired_a = alpha*transpose(x[::2,newaxis]*self.transform(y)) + a
|
| 577 |
+
# self.blas_func(x,y,a,incx=2)
|
| 578 |
+
# assert_array_almost_equal(desired_a,a)
|
| 579 |
+
#def test_y_stride(self):
|
| 580 |
+
# alpha,a,x,y = self.get_data(y_stride=2)
|
| 581 |
+
# desired_a = alpha*transpose(x[:,newaxis]*self.transform(y[::2])) + a
|
| 582 |
+
# self.blas_func(x,y,a,incy=2)
|
| 583 |
+
# assert_array_almost_equal(desired_a,a)
|
| 584 |
+
|
| 585 |
+
class TestCgeru(BaseGerComplex):
|
| 586 |
+
blas_func = fblas.cgeru
|
| 587 |
+
dtype = complex64
|
| 588 |
+
def transform(self,x):
|
| 589 |
+
return x
|
| 590 |
+
class TestZgeru(BaseGerComplex):
|
| 591 |
+
blas_func = fblas.zgeru
|
| 592 |
+
dtype = complex128
|
| 593 |
+
def transform(self,x):
|
| 594 |
+
return x
|
| 595 |
+
|
| 596 |
+
class TestCgerc(BaseGerComplex):
|
| 597 |
+
blas_func = fblas.cgerc
|
| 598 |
+
dtype = complex64
|
| 599 |
+
def transform(self,x):
|
| 600 |
+
return conjugate(x)
|
| 601 |
+
|
| 602 |
+
class TestZgerc(BaseGerComplex):
|
| 603 |
+
blas_func = fblas.zgerc
|
| 604 |
+
dtype = complex128
|
| 605 |
+
def transform(self,x):
|
| 606 |
+
return conjugate(x)
|
| 607 |
+
"""
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_interpolative.py
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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Redistribution and use in source and binary forms, with or without
|
| 4 |
+
# modification, are permitted provided that the following conditions are met:
|
| 5 |
+
#
|
| 6 |
+
# Redistributions of source code must retain the above copyright notice, this
|
| 7 |
+
# list of conditions and the following disclaimer. Redistributions in binary
|
| 8 |
+
# form must reproduce the above copyright notice, this list of conditions and
|
| 9 |
+
# the following disclaimer in the documentation and/or other materials
|
| 10 |
+
# provided with the distribution.
|
| 11 |
+
#
|
| 12 |
+
# None of the names of the copyright holders may be used to endorse or
|
| 13 |
+
# promote products derived from this software without specific prior written
|
| 14 |
+
# permission.
|
| 15 |
+
#
|
| 16 |
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
| 17 |
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
| 18 |
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
| 19 |
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
| 20 |
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
| 21 |
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
| 22 |
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
| 23 |
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
| 24 |
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
| 25 |
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
| 26 |
+
# POSSIBILITY OF SUCH DAMAGE.
|
| 27 |
+
# ******************************************************************************
|
| 28 |
+
|
| 29 |
+
import scipy.linalg.interpolative as pymatrixid
|
| 30 |
+
import numpy as np
|
| 31 |
+
from scipy.linalg import hilbert, svdvals, norm
|
| 32 |
+
from scipy.sparse.linalg import aslinearoperator
|
| 33 |
+
from scipy.linalg.interpolative import interp_decomp
|
| 34 |
+
|
| 35 |
+
from numpy.testing import (assert_, assert_allclose, assert_equal,
|
| 36 |
+
assert_array_equal)
|
| 37 |
+
import pytest
|
| 38 |
+
from pytest import raises as assert_raises
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
@pytest.fixture()
|
| 42 |
+
def eps():
|
| 43 |
+
yield 1e-12
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
@pytest.fixture()
|
| 47 |
+
def rng():
|
| 48 |
+
rng = np.random.default_rng(1718313768084012)
|
| 49 |
+
yield rng
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
@pytest.fixture(params=[np.float64, np.complex128])
|
| 53 |
+
def A(request):
|
| 54 |
+
# construct Hilbert matrix
|
| 55 |
+
# set parameters
|
| 56 |
+
n = 300
|
| 57 |
+
yield hilbert(n).astype(request.param)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
@pytest.fixture()
|
| 61 |
+
def L(A):
|
| 62 |
+
yield aslinearoperator(A)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
@pytest.fixture()
|
| 66 |
+
def rank(A, eps):
|
| 67 |
+
S = np.linalg.svd(A, compute_uv=False)
|
| 68 |
+
try:
|
| 69 |
+
rank = np.nonzero(S < eps)[0][0]
|
| 70 |
+
except IndexError:
|
| 71 |
+
rank = A.shape[0]
|
| 72 |
+
return rank
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
class TestInterpolativeDecomposition:
|
| 76 |
+
|
| 77 |
+
@pytest.mark.parametrize(
|
| 78 |
+
"rand,lin_op",
|
| 79 |
+
[(False, False), (True, False), (True, True)])
|
| 80 |
+
def test_real_id_fixed_precision(self, A, L, eps, rand, lin_op, rng):
|
| 81 |
+
# Test ID routines on a Hilbert matrix.
|
| 82 |
+
A_or_L = A if not lin_op else L
|
| 83 |
+
|
| 84 |
+
k, idx, proj = pymatrixid.interp_decomp(A_or_L, eps, rand=rand, rng=rng)
|
| 85 |
+
B = pymatrixid.reconstruct_matrix_from_id(A[:, idx[:k]], idx, proj)
|
| 86 |
+
assert_allclose(A, B, rtol=eps, atol=1e-08)
|
| 87 |
+
|
| 88 |
+
@pytest.mark.parametrize(
|
| 89 |
+
"rand,lin_op",
|
| 90 |
+
[(False, False), (True, False), (True, True)])
|
| 91 |
+
def test_real_id_fixed_rank(self, A, L, eps, rank, rand, lin_op, rng):
|
| 92 |
+
k = rank
|
| 93 |
+
A_or_L = A if not lin_op else L
|
| 94 |
+
|
| 95 |
+
idx, proj = pymatrixid.interp_decomp(A_or_L, k, rand=rand, rng=rng)
|
| 96 |
+
B = pymatrixid.reconstruct_matrix_from_id(A[:, idx[:k]], idx, proj)
|
| 97 |
+
assert_allclose(A, B, rtol=eps, atol=1e-08)
|
| 98 |
+
|
| 99 |
+
@pytest.mark.parametrize("rand,lin_op", [(False, False)])
|
| 100 |
+
def test_real_id_skel_and_interp_matrices(
|
| 101 |
+
self, A, L, eps, rank, rand, lin_op, rng):
|
| 102 |
+
k = rank
|
| 103 |
+
A_or_L = A if not lin_op else L
|
| 104 |
+
|
| 105 |
+
idx, proj = pymatrixid.interp_decomp(A_or_L, k, rand=rand, rng=rng)
|
| 106 |
+
P = pymatrixid.reconstruct_interp_matrix(idx, proj)
|
| 107 |
+
B = pymatrixid.reconstruct_skel_matrix(A, k, idx)
|
| 108 |
+
assert_allclose(B, A[:, idx[:k]], rtol=eps, atol=1e-08)
|
| 109 |
+
assert_allclose(B @ P, A, rtol=eps, atol=1e-08)
|
| 110 |
+
|
| 111 |
+
@pytest.mark.parametrize(
|
| 112 |
+
"rand,lin_op",
|
| 113 |
+
[(False, False), (True, False), (True, True)])
|
| 114 |
+
def test_svd_fixed_precision(self, A, L, eps, rand, lin_op, rng):
|
| 115 |
+
A_or_L = A if not lin_op else L
|
| 116 |
+
|
| 117 |
+
U, S, V = pymatrixid.svd(A_or_L, eps, rand=rand, rng=rng)
|
| 118 |
+
B = U * S @ V.T.conj()
|
| 119 |
+
assert_allclose(A, B, rtol=eps, atol=1e-08)
|
| 120 |
+
|
| 121 |
+
@pytest.mark.parametrize(
|
| 122 |
+
"rand,lin_op",
|
| 123 |
+
[(False, False), (True, False), (True, True)])
|
| 124 |
+
def test_svd_fixed_rank(self, A, L, eps, rank, rand, lin_op, rng):
|
| 125 |
+
k = rank
|
| 126 |
+
A_or_L = A if not lin_op else L
|
| 127 |
+
|
| 128 |
+
U, S, V = pymatrixid.svd(A_or_L, k, rand=rand, rng=rng)
|
| 129 |
+
B = U * S @ V.T.conj()
|
| 130 |
+
assert_allclose(A, B, rtol=eps, atol=1e-08)
|
| 131 |
+
|
| 132 |
+
def test_id_to_svd(self, A, eps, rank):
|
| 133 |
+
k = rank
|
| 134 |
+
|
| 135 |
+
idx, proj = pymatrixid.interp_decomp(A, k, rand=False)
|
| 136 |
+
U, S, V = pymatrixid.id_to_svd(A[:, idx[:k]], idx, proj)
|
| 137 |
+
B = U * S @ V.T.conj()
|
| 138 |
+
assert_allclose(A, B, rtol=eps, atol=1e-08)
|
| 139 |
+
|
| 140 |
+
def test_estimate_spectral_norm(self, A, rng):
|
| 141 |
+
s = svdvals(A)
|
| 142 |
+
norm_2_est = pymatrixid.estimate_spectral_norm(A, rng=rng)
|
| 143 |
+
assert_allclose(norm_2_est, s[0], rtol=1e-6, atol=1e-8)
|
| 144 |
+
|
| 145 |
+
def test_estimate_spectral_norm_diff(self, A, rng):
|
| 146 |
+
B = A.copy()
|
| 147 |
+
B[:, 0] *= 1.2
|
| 148 |
+
s = svdvals(A - B)
|
| 149 |
+
norm_2_est = pymatrixid.estimate_spectral_norm_diff(A, B, rng=rng)
|
| 150 |
+
assert_allclose(norm_2_est, s[0], rtol=1e-6, atol=1e-8)
|
| 151 |
+
|
| 152 |
+
def test_rank_estimates_array(self, A, rng):
|
| 153 |
+
B = np.array([[1, 1, 0], [0, 0, 1], [0, 0, 1]], dtype=A.dtype)
|
| 154 |
+
|
| 155 |
+
for M in [A, B]:
|
| 156 |
+
rank_tol = 1e-9
|
| 157 |
+
rank_np = np.linalg.matrix_rank(M, norm(M, 2) * rank_tol)
|
| 158 |
+
rank_est = pymatrixid.estimate_rank(M, rank_tol, rng=rng)
|
| 159 |
+
assert_(rank_est >= rank_np)
|
| 160 |
+
assert_(rank_est <= rank_np + 10)
|
| 161 |
+
|
| 162 |
+
def test_rank_estimates_lin_op(self, A, rng):
|
| 163 |
+
B = np.array([[1, 1, 0], [0, 0, 1], [0, 0, 1]], dtype=A.dtype)
|
| 164 |
+
|
| 165 |
+
for M in [A, B]:
|
| 166 |
+
ML = aslinearoperator(M)
|
| 167 |
+
rank_tol = 1e-9
|
| 168 |
+
rank_np = np.linalg.matrix_rank(M, norm(M, 2) * rank_tol)
|
| 169 |
+
rank_est = pymatrixid.estimate_rank(ML, rank_tol, rng=rng)
|
| 170 |
+
assert_(rank_est >= rank_np - 4)
|
| 171 |
+
assert_(rank_est <= rank_np + 4)
|
| 172 |
+
|
| 173 |
+
def test_badcall(self):
|
| 174 |
+
A = hilbert(5).astype(np.float32)
|
| 175 |
+
with assert_raises(ValueError):
|
| 176 |
+
pymatrixid.interp_decomp(A, 1e-6, rand=False)
|
| 177 |
+
|
| 178 |
+
def test_rank_too_large(self):
|
| 179 |
+
# svd(array, k) should not segfault
|
| 180 |
+
a = np.ones((4, 3))
|
| 181 |
+
with assert_raises(ValueError):
|
| 182 |
+
pymatrixid.svd(a, 4)
|
| 183 |
+
|
| 184 |
+
def test_full_rank(self):
|
| 185 |
+
eps = 1.0e-12
|
| 186 |
+
|
| 187 |
+
# fixed precision
|
| 188 |
+
A = np.random.rand(16, 8)
|
| 189 |
+
k, idx, proj = pymatrixid.interp_decomp(A, eps)
|
| 190 |
+
assert_equal(k, A.shape[1])
|
| 191 |
+
|
| 192 |
+
P = pymatrixid.reconstruct_interp_matrix(idx, proj)
|
| 193 |
+
B = pymatrixid.reconstruct_skel_matrix(A, k, idx)
|
| 194 |
+
assert_allclose(A, B @ P)
|
| 195 |
+
|
| 196 |
+
# fixed rank
|
| 197 |
+
idx, proj = pymatrixid.interp_decomp(A, k)
|
| 198 |
+
|
| 199 |
+
P = pymatrixid.reconstruct_interp_matrix(idx, proj)
|
| 200 |
+
B = pymatrixid.reconstruct_skel_matrix(A, k, idx)
|
| 201 |
+
assert_allclose(A, B @ P)
|
| 202 |
+
|
| 203 |
+
@pytest.mark.parametrize("dtype", [np.float64, np.complex128])
|
| 204 |
+
@pytest.mark.parametrize("rand", [True, False])
|
| 205 |
+
@pytest.mark.parametrize("eps", [1, 0.1])
|
| 206 |
+
def test_bug_9793(self, dtype, rand, eps):
|
| 207 |
+
A = np.array([[-1, -1, -1, 0, 0, 0],
|
| 208 |
+
[0, 0, 0, 1, 1, 1],
|
| 209 |
+
[1, 0, 0, 1, 0, 0],
|
| 210 |
+
[0, 1, 0, 0, 1, 0],
|
| 211 |
+
[0, 0, 1, 0, 0, 1]],
|
| 212 |
+
dtype=dtype, order="C")
|
| 213 |
+
B = A.copy()
|
| 214 |
+
interp_decomp(A.T, eps, rand=rand)
|
| 215 |
+
assert_array_equal(A, B)
|
| 216 |
+
|
| 217 |
+
def test_svd_aslinearoperator_shape_check(self):
|
| 218 |
+
# See gh-issue #22451
|
| 219 |
+
rng = np.random.default_rng(1744580941832515)
|
| 220 |
+
x = rng.uniform(size=[7, 5])
|
| 221 |
+
xl = aslinearoperator(x)
|
| 222 |
+
u, s, v = pymatrixid.svd(xl, 3)
|
| 223 |
+
assert_equal(u.shape, (7, 3))
|
| 224 |
+
assert_equal(s.shape, (3,))
|
| 225 |
+
assert_equal(v.shape, (5, 3))
|
| 226 |
+
|
| 227 |
+
x = rng.uniform(size=[4, 9])
|
| 228 |
+
xl = aslinearoperator(x)
|
| 229 |
+
u, s, v = pymatrixid.svd(xl, 2)
|
| 230 |
+
assert_equal(u.shape, (4, 2))
|
| 231 |
+
assert_equal(s.shape, (2,))
|
| 232 |
+
assert_equal(v.shape, (9, 2))
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_lapack.py
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_matfuncs.py
ADDED
|
@@ -0,0 +1,1063 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# Created by: Pearu Peterson, March 2002
|
| 3 |
+
#
|
| 4 |
+
""" Test functions for linalg.matfuncs module
|
| 5 |
+
|
| 6 |
+
"""
|
| 7 |
+
import functools
|
| 8 |
+
|
| 9 |
+
import numpy as np
|
| 10 |
+
from numpy import array, identity, dot, sqrt
|
| 11 |
+
from numpy.testing import (assert_array_almost_equal, assert_allclose, assert_,
|
| 12 |
+
assert_array_less, assert_array_equal, assert_warns)
|
| 13 |
+
import pytest
|
| 14 |
+
|
| 15 |
+
import scipy.linalg
|
| 16 |
+
from scipy.linalg import (funm, signm, logm, sqrtm, fractional_matrix_power,
|
| 17 |
+
expm, expm_frechet, expm_cond, norm, khatri_rao,
|
| 18 |
+
cosm, sinm, tanm, coshm, sinhm, tanhm)
|
| 19 |
+
from scipy.linalg import _matfuncs_inv_ssq
|
| 20 |
+
from scipy.linalg._matfuncs import pick_pade_structure
|
| 21 |
+
from scipy.linalg._matfuncs_inv_ssq import LogmExactlySingularWarning
|
| 22 |
+
import scipy.linalg._expm_frechet
|
| 23 |
+
|
| 24 |
+
from scipy.optimize import minimize
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def _get_al_mohy_higham_2012_experiment_1():
|
| 28 |
+
"""
|
| 29 |
+
Return the test matrix from Experiment (1) of [1]_.
|
| 30 |
+
|
| 31 |
+
References
|
| 32 |
+
----------
|
| 33 |
+
.. [1] Awad H. Al-Mohy and Nicholas J. Higham (2012)
|
| 34 |
+
"Improved Inverse Scaling and Squaring Algorithms
|
| 35 |
+
for the Matrix Logarithm."
|
| 36 |
+
SIAM Journal on Scientific Computing, 34 (4). C152-C169.
|
| 37 |
+
ISSN 1095-7197
|
| 38 |
+
|
| 39 |
+
"""
|
| 40 |
+
A = np.array([
|
| 41 |
+
[3.2346e-1, 3e4, 3e4, 3e4],
|
| 42 |
+
[0, 3.0089e-1, 3e4, 3e4],
|
| 43 |
+
[0, 0, 3.2210e-1, 3e4],
|
| 44 |
+
[0, 0, 0, 3.0744e-1]], dtype=float)
|
| 45 |
+
return A
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
class TestSignM:
|
| 49 |
+
|
| 50 |
+
def test_nils(self):
|
| 51 |
+
a = array([[29.2, -24.2, 69.5, 49.8, 7.],
|
| 52 |
+
[-9.2, 5.2, -18., -16.8, -2.],
|
| 53 |
+
[-10., 6., -20., -18., -2.],
|
| 54 |
+
[-9.6, 9.6, -25.5, -15.4, -2.],
|
| 55 |
+
[9.8, -4.8, 18., 18.2, 2.]])
|
| 56 |
+
cr = array([[11.94933333,-2.24533333,15.31733333,21.65333333,-2.24533333],
|
| 57 |
+
[-3.84266667,0.49866667,-4.59066667,-7.18666667,0.49866667],
|
| 58 |
+
[-4.08,0.56,-4.92,-7.6,0.56],
|
| 59 |
+
[-4.03466667,1.04266667,-5.59866667,-7.02666667,1.04266667],
|
| 60 |
+
[4.15733333,-0.50133333,4.90933333,7.81333333,-0.50133333]])
|
| 61 |
+
r = signm(a)
|
| 62 |
+
assert_array_almost_equal(r,cr)
|
| 63 |
+
|
| 64 |
+
def test_defective1(self):
|
| 65 |
+
a = array([[0.0,1,0,0],[1,0,1,0],[0,0,0,1],[0,0,1,0]])
|
| 66 |
+
signm(a, disp=False)
|
| 67 |
+
#XXX: what would be the correct result?
|
| 68 |
+
|
| 69 |
+
def test_defective2(self):
|
| 70 |
+
a = array((
|
| 71 |
+
[29.2,-24.2,69.5,49.8,7.0],
|
| 72 |
+
[-9.2,5.2,-18.0,-16.8,-2.0],
|
| 73 |
+
[-10.0,6.0,-20.0,-18.0,-2.0],
|
| 74 |
+
[-9.6,9.6,-25.5,-15.4,-2.0],
|
| 75 |
+
[9.8,-4.8,18.0,18.2,2.0]))
|
| 76 |
+
signm(a, disp=False)
|
| 77 |
+
#XXX: what would be the correct result?
|
| 78 |
+
|
| 79 |
+
def test_defective3(self):
|
| 80 |
+
a = array([[-2., 25., 0., 0., 0., 0., 0.],
|
| 81 |
+
[0., -3., 10., 3., 3., 3., 0.],
|
| 82 |
+
[0., 0., 2., 15., 3., 3., 0.],
|
| 83 |
+
[0., 0., 0., 0., 15., 3., 0.],
|
| 84 |
+
[0., 0., 0., 0., 3., 10., 0.],
|
| 85 |
+
[0., 0., 0., 0., 0., -2., 25.],
|
| 86 |
+
[0., 0., 0., 0., 0., 0., -3.]])
|
| 87 |
+
signm(a, disp=False)
|
| 88 |
+
#XXX: what would be the correct result?
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
class TestLogM:
|
| 92 |
+
|
| 93 |
+
def test_nils(self):
|
| 94 |
+
a = array([[-2., 25., 0., 0., 0., 0., 0.],
|
| 95 |
+
[0., -3., 10., 3., 3., 3., 0.],
|
| 96 |
+
[0., 0., 2., 15., 3., 3., 0.],
|
| 97 |
+
[0., 0., 0., 0., 15., 3., 0.],
|
| 98 |
+
[0., 0., 0., 0., 3., 10., 0.],
|
| 99 |
+
[0., 0., 0., 0., 0., -2., 25.],
|
| 100 |
+
[0., 0., 0., 0., 0., 0., -3.]])
|
| 101 |
+
m = (identity(7)*3.1+0j)-a
|
| 102 |
+
logm(m, disp=False)
|
| 103 |
+
#XXX: what would be the correct result?
|
| 104 |
+
|
| 105 |
+
def test_al_mohy_higham_2012_experiment_1_logm(self):
|
| 106 |
+
# The logm completes the round trip successfully.
|
| 107 |
+
# Note that the expm leg of the round trip is badly conditioned.
|
| 108 |
+
A = _get_al_mohy_higham_2012_experiment_1()
|
| 109 |
+
A_logm, info = logm(A, disp=False)
|
| 110 |
+
A_round_trip = expm(A_logm)
|
| 111 |
+
assert_allclose(A_round_trip, A, rtol=5e-5, atol=1e-14)
|
| 112 |
+
|
| 113 |
+
def test_al_mohy_higham_2012_experiment_1_funm_log(self):
|
| 114 |
+
# The raw funm with np.log does not complete the round trip.
|
| 115 |
+
# Note that the expm leg of the round trip is badly conditioned.
|
| 116 |
+
A = _get_al_mohy_higham_2012_experiment_1()
|
| 117 |
+
A_funm_log, info = funm(A, np.log, disp=False)
|
| 118 |
+
A_round_trip = expm(A_funm_log)
|
| 119 |
+
assert_(not np.allclose(A_round_trip, A, rtol=1e-5, atol=1e-14))
|
| 120 |
+
|
| 121 |
+
def test_round_trip_random_float(self):
|
| 122 |
+
np.random.seed(1234)
|
| 123 |
+
for n in range(1, 6):
|
| 124 |
+
M_unscaled = np.random.randn(n, n)
|
| 125 |
+
for scale in np.logspace(-4, 4, 9):
|
| 126 |
+
M = M_unscaled * scale
|
| 127 |
+
|
| 128 |
+
# Eigenvalues are related to the branch cut.
|
| 129 |
+
W = np.linalg.eigvals(M)
|
| 130 |
+
err_msg = f'M:{M} eivals:{W}'
|
| 131 |
+
|
| 132 |
+
# Check sqrtm round trip because it is used within logm.
|
| 133 |
+
M_sqrtm, info = sqrtm(M, disp=False)
|
| 134 |
+
M_sqrtm_round_trip = M_sqrtm.dot(M_sqrtm)
|
| 135 |
+
assert_allclose(M_sqrtm_round_trip, M)
|
| 136 |
+
|
| 137 |
+
# Check logm round trip.
|
| 138 |
+
M_logm, info = logm(M, disp=False)
|
| 139 |
+
M_logm_round_trip = expm(M_logm)
|
| 140 |
+
assert_allclose(M_logm_round_trip, M, err_msg=err_msg)
|
| 141 |
+
|
| 142 |
+
def test_round_trip_random_complex(self):
|
| 143 |
+
np.random.seed(1234)
|
| 144 |
+
for n in range(1, 6):
|
| 145 |
+
M_unscaled = np.random.randn(n, n) + 1j * np.random.randn(n, n)
|
| 146 |
+
for scale in np.logspace(-4, 4, 9):
|
| 147 |
+
M = M_unscaled * scale
|
| 148 |
+
M_logm, info = logm(M, disp=False)
|
| 149 |
+
M_round_trip = expm(M_logm)
|
| 150 |
+
assert_allclose(M_round_trip, M)
|
| 151 |
+
|
| 152 |
+
def test_logm_type_preservation_and_conversion(self):
|
| 153 |
+
# The logm matrix function should preserve the type of a matrix
|
| 154 |
+
# whose eigenvalues are positive with zero imaginary part.
|
| 155 |
+
# Test this preservation for variously structured matrices.
|
| 156 |
+
complex_dtype_chars = ('F', 'D', 'G')
|
| 157 |
+
for matrix_as_list in (
|
| 158 |
+
[[1, 0], [0, 1]],
|
| 159 |
+
[[1, 0], [1, 1]],
|
| 160 |
+
[[2, 1], [1, 1]],
|
| 161 |
+
[[2, 3], [1, 2]]):
|
| 162 |
+
|
| 163 |
+
# check that the spectrum has the expected properties
|
| 164 |
+
W = scipy.linalg.eigvals(matrix_as_list)
|
| 165 |
+
assert_(not any(w.imag or w.real < 0 for w in W))
|
| 166 |
+
|
| 167 |
+
# check float type preservation
|
| 168 |
+
A = np.array(matrix_as_list, dtype=float)
|
| 169 |
+
A_logm, info = logm(A, disp=False)
|
| 170 |
+
assert_(A_logm.dtype.char not in complex_dtype_chars)
|
| 171 |
+
|
| 172 |
+
# check complex type preservation
|
| 173 |
+
A = np.array(matrix_as_list, dtype=complex)
|
| 174 |
+
A_logm, info = logm(A, disp=False)
|
| 175 |
+
assert_(A_logm.dtype.char in complex_dtype_chars)
|
| 176 |
+
|
| 177 |
+
# check float->complex type conversion for the matrix negation
|
| 178 |
+
A = -np.array(matrix_as_list, dtype=float)
|
| 179 |
+
A_logm, info = logm(A, disp=False)
|
| 180 |
+
assert_(A_logm.dtype.char in complex_dtype_chars)
|
| 181 |
+
|
| 182 |
+
def test_complex_spectrum_real_logm(self):
|
| 183 |
+
# This matrix has complex eigenvalues and real logm.
|
| 184 |
+
# Its output dtype depends on its input dtype.
|
| 185 |
+
M = [[1, 1, 2], [2, 1, 1], [1, 2, 1]]
|
| 186 |
+
for dt in float, complex:
|
| 187 |
+
X = np.array(M, dtype=dt)
|
| 188 |
+
w = scipy.linalg.eigvals(X)
|
| 189 |
+
assert_(1e-2 < np.absolute(w.imag).sum())
|
| 190 |
+
Y, info = logm(X, disp=False)
|
| 191 |
+
assert_(np.issubdtype(Y.dtype, np.inexact))
|
| 192 |
+
assert_allclose(expm(Y), X)
|
| 193 |
+
|
| 194 |
+
def test_real_mixed_sign_spectrum(self):
|
| 195 |
+
# These matrices have real eigenvalues with mixed signs.
|
| 196 |
+
# The output logm dtype is complex, regardless of input dtype.
|
| 197 |
+
for M in (
|
| 198 |
+
[[1, 0], [0, -1]],
|
| 199 |
+
[[0, 1], [1, 0]]):
|
| 200 |
+
for dt in float, complex:
|
| 201 |
+
A = np.array(M, dtype=dt)
|
| 202 |
+
A_logm, info = logm(A, disp=False)
|
| 203 |
+
assert_(np.issubdtype(A_logm.dtype, np.complexfloating))
|
| 204 |
+
|
| 205 |
+
@pytest.mark.thread_unsafe
|
| 206 |
+
def test_exactly_singular(self):
|
| 207 |
+
A = np.array([[0, 0], [1j, 1j]])
|
| 208 |
+
B = np.asarray([[1, 1], [0, 0]])
|
| 209 |
+
for M in A, A.T, B, B.T:
|
| 210 |
+
expected_warning = _matfuncs_inv_ssq.LogmExactlySingularWarning
|
| 211 |
+
L, info = assert_warns(expected_warning, logm, M, disp=False)
|
| 212 |
+
E = expm(L)
|
| 213 |
+
assert_allclose(E, M, atol=1e-14)
|
| 214 |
+
|
| 215 |
+
@pytest.mark.thread_unsafe
|
| 216 |
+
def test_nearly_singular(self):
|
| 217 |
+
M = np.array([[1e-100]])
|
| 218 |
+
expected_warning = _matfuncs_inv_ssq.LogmNearlySingularWarning
|
| 219 |
+
L, info = assert_warns(expected_warning, logm, M, disp=False)
|
| 220 |
+
E = expm(L)
|
| 221 |
+
assert_allclose(E, M, atol=1e-14)
|
| 222 |
+
|
| 223 |
+
def test_opposite_sign_complex_eigenvalues(self):
|
| 224 |
+
# See gh-6113
|
| 225 |
+
E = [[0, 1], [-1, 0]]
|
| 226 |
+
L = [[0, np.pi*0.5], [-np.pi*0.5, 0]]
|
| 227 |
+
assert_allclose(expm(L), E, atol=1e-14)
|
| 228 |
+
assert_allclose(logm(E), L, atol=1e-14)
|
| 229 |
+
E = [[1j, 4], [0, -1j]]
|
| 230 |
+
L = [[1j*np.pi*0.5, 2*np.pi], [0, -1j*np.pi*0.5]]
|
| 231 |
+
assert_allclose(expm(L), E, atol=1e-14)
|
| 232 |
+
assert_allclose(logm(E), L, atol=1e-14)
|
| 233 |
+
E = [[1j, 0], [0, -1j]]
|
| 234 |
+
L = [[1j*np.pi*0.5, 0], [0, -1j*np.pi*0.5]]
|
| 235 |
+
assert_allclose(expm(L), E, atol=1e-14)
|
| 236 |
+
assert_allclose(logm(E), L, atol=1e-14)
|
| 237 |
+
|
| 238 |
+
def test_readonly(self):
|
| 239 |
+
n = 5
|
| 240 |
+
a = np.ones((n, n)) + np.identity(n)
|
| 241 |
+
a.flags.writeable = False
|
| 242 |
+
logm(a)
|
| 243 |
+
|
| 244 |
+
@pytest.mark.xfail(reason="ValueError: attempt to get argmax of an empty sequence")
|
| 245 |
+
@pytest.mark.parametrize('dt', [int, float, np.float32, complex, np.complex64])
|
| 246 |
+
def test_empty(self, dt):
|
| 247 |
+
a = np.empty((0, 0), dtype=dt)
|
| 248 |
+
log_a = logm(a)
|
| 249 |
+
a0 = np.eye(2, dtype=dt)
|
| 250 |
+
log_a0 = logm(a0)
|
| 251 |
+
|
| 252 |
+
assert log_a.shape == (0, 0)
|
| 253 |
+
assert log_a.dtype == log_a0.dtype
|
| 254 |
+
|
| 255 |
+
@pytest.mark.thread_unsafe
|
| 256 |
+
@pytest.mark.parametrize('dtype', [int, float, np.float32, complex, np.complex64])
|
| 257 |
+
def test_no_ZeroDivisionError(self, dtype):
|
| 258 |
+
# gh-17136 reported inconsistent behavior in `logm` depending on input dtype:
|
| 259 |
+
# sometimes it raised an error, and sometimes it printed a warning message.
|
| 260 |
+
# check that this is resolved and that the warning is emitted properly.
|
| 261 |
+
with (pytest.warns(RuntimeWarning, match="logm result may be inaccurate"),
|
| 262 |
+
pytest.warns(LogmExactlySingularWarning)):
|
| 263 |
+
logm(np.zeros((2, 2), dtype=dtype))
|
| 264 |
+
|
| 265 |
+
|
| 266 |
+
class TestSqrtM:
|
| 267 |
+
def test_round_trip_random_float(self):
|
| 268 |
+
rng = np.random.RandomState(1234)
|
| 269 |
+
for n in range(1, 6):
|
| 270 |
+
M_unscaled = rng.randn(n, n)
|
| 271 |
+
for scale in np.logspace(-4, 4, 9):
|
| 272 |
+
M = M_unscaled * scale
|
| 273 |
+
M_sqrtm, info = sqrtm(M, disp=False)
|
| 274 |
+
M_sqrtm_round_trip = M_sqrtm.dot(M_sqrtm)
|
| 275 |
+
assert_allclose(M_sqrtm_round_trip, M)
|
| 276 |
+
|
| 277 |
+
def test_round_trip_random_complex(self):
|
| 278 |
+
rng = np.random.RandomState(1234)
|
| 279 |
+
for n in range(1, 6):
|
| 280 |
+
M_unscaled = rng.randn(n, n) + 1j * rng.randn(n, n)
|
| 281 |
+
for scale in np.logspace(-4, 4, 9):
|
| 282 |
+
M = M_unscaled * scale
|
| 283 |
+
M_sqrtm, info = sqrtm(M, disp=False)
|
| 284 |
+
M_sqrtm_round_trip = M_sqrtm.dot(M_sqrtm)
|
| 285 |
+
assert_allclose(M_sqrtm_round_trip, M)
|
| 286 |
+
|
| 287 |
+
def test_bad(self):
|
| 288 |
+
# See https://web.archive.org/web/20051220232650/http://www.maths.man.ac.uk/~nareports/narep336.ps.gz
|
| 289 |
+
e = 2**-5
|
| 290 |
+
se = sqrt(e)
|
| 291 |
+
a = array([[1.0,0,0,1],
|
| 292 |
+
[0,e,0,0],
|
| 293 |
+
[0,0,e,0],
|
| 294 |
+
[0,0,0,1]])
|
| 295 |
+
sa = array([[1,0,0,0.5],
|
| 296 |
+
[0,se,0,0],
|
| 297 |
+
[0,0,se,0],
|
| 298 |
+
[0,0,0,1]])
|
| 299 |
+
n = a.shape[0]
|
| 300 |
+
assert_array_almost_equal(dot(sa,sa),a)
|
| 301 |
+
# Check default sqrtm.
|
| 302 |
+
esa = sqrtm(a, disp=False, blocksize=n)[0]
|
| 303 |
+
assert_array_almost_equal(dot(esa,esa),a)
|
| 304 |
+
# Check sqrtm with 2x2 blocks.
|
| 305 |
+
esa = sqrtm(a, disp=False, blocksize=2)[0]
|
| 306 |
+
assert_array_almost_equal(dot(esa,esa),a)
|
| 307 |
+
|
| 308 |
+
def test_sqrtm_type_preservation_and_conversion(self):
|
| 309 |
+
# The sqrtm matrix function should preserve the type of a matrix
|
| 310 |
+
# whose eigenvalues are nonnegative with zero imaginary part.
|
| 311 |
+
# Test this preservation for variously structured matrices.
|
| 312 |
+
complex_dtype_chars = ('F', 'D', 'G')
|
| 313 |
+
for matrix_as_list in (
|
| 314 |
+
[[1, 0], [0, 1]],
|
| 315 |
+
[[1, 0], [1, 1]],
|
| 316 |
+
[[2, 1], [1, 1]],
|
| 317 |
+
[[2, 3], [1, 2]],
|
| 318 |
+
[[1, 1], [1, 1]]):
|
| 319 |
+
|
| 320 |
+
# check that the spectrum has the expected properties
|
| 321 |
+
W = scipy.linalg.eigvals(matrix_as_list)
|
| 322 |
+
assert_(not any(w.imag or w.real < 0 for w in W))
|
| 323 |
+
|
| 324 |
+
# check float type preservation
|
| 325 |
+
A = np.array(matrix_as_list, dtype=float)
|
| 326 |
+
A_sqrtm, info = sqrtm(A, disp=False)
|
| 327 |
+
assert_(A_sqrtm.dtype.char not in complex_dtype_chars)
|
| 328 |
+
|
| 329 |
+
# check complex type preservation
|
| 330 |
+
A = np.array(matrix_as_list, dtype=complex)
|
| 331 |
+
A_sqrtm, info = sqrtm(A, disp=False)
|
| 332 |
+
assert_(A_sqrtm.dtype.char in complex_dtype_chars)
|
| 333 |
+
|
| 334 |
+
# check float->complex type conversion for the matrix negation
|
| 335 |
+
A = -np.array(matrix_as_list, dtype=float)
|
| 336 |
+
A_sqrtm, info = sqrtm(A, disp=False)
|
| 337 |
+
assert_(A_sqrtm.dtype.char in complex_dtype_chars)
|
| 338 |
+
|
| 339 |
+
def test_sqrtm_type_conversion_mixed_sign_or_complex_spectrum(self):
|
| 340 |
+
complex_dtype_chars = ('F', 'D', 'G')
|
| 341 |
+
for matrix_as_list in (
|
| 342 |
+
[[1, 0], [0, -1]],
|
| 343 |
+
[[0, 1], [1, 0]],
|
| 344 |
+
[[0, 1, 0], [0, 0, 1], [1, 0, 0]]):
|
| 345 |
+
|
| 346 |
+
# check that the spectrum has the expected properties
|
| 347 |
+
W = scipy.linalg.eigvals(matrix_as_list)
|
| 348 |
+
assert_(any(w.imag or w.real < 0 for w in W))
|
| 349 |
+
|
| 350 |
+
# check complex->complex
|
| 351 |
+
A = np.array(matrix_as_list, dtype=complex)
|
| 352 |
+
A_sqrtm, info = sqrtm(A, disp=False)
|
| 353 |
+
assert_(A_sqrtm.dtype.char in complex_dtype_chars)
|
| 354 |
+
|
| 355 |
+
# check float->complex
|
| 356 |
+
A = np.array(matrix_as_list, dtype=float)
|
| 357 |
+
A_sqrtm, info = sqrtm(A, disp=False)
|
| 358 |
+
assert_(A_sqrtm.dtype.char in complex_dtype_chars)
|
| 359 |
+
|
| 360 |
+
def test_blocksizes(self):
|
| 361 |
+
# Make sure I do not goof up the blocksizes when they do not divide n.
|
| 362 |
+
np.random.seed(1234)
|
| 363 |
+
for n in range(1, 8):
|
| 364 |
+
A = np.random.rand(n, n) + 1j*np.random.randn(n, n)
|
| 365 |
+
A_sqrtm_default, info = sqrtm(A, disp=False, blocksize=n)
|
| 366 |
+
assert_allclose(A, np.linalg.matrix_power(A_sqrtm_default, 2))
|
| 367 |
+
for blocksize in range(1, 10):
|
| 368 |
+
A_sqrtm_new, info = sqrtm(A, disp=False, blocksize=blocksize)
|
| 369 |
+
assert_allclose(A_sqrtm_default, A_sqrtm_new)
|
| 370 |
+
|
| 371 |
+
def test_al_mohy_higham_2012_experiment_1(self):
|
| 372 |
+
# Matrix square root of a tricky upper triangular matrix.
|
| 373 |
+
A = _get_al_mohy_higham_2012_experiment_1()
|
| 374 |
+
A_sqrtm, info = sqrtm(A, disp=False)
|
| 375 |
+
A_round_trip = A_sqrtm.dot(A_sqrtm)
|
| 376 |
+
assert_allclose(A_round_trip, A, rtol=1e-5)
|
| 377 |
+
assert_allclose(np.tril(A_round_trip), np.tril(A))
|
| 378 |
+
|
| 379 |
+
def test_strict_upper_triangular(self):
|
| 380 |
+
# This matrix has no square root.
|
| 381 |
+
for dt in int, float:
|
| 382 |
+
A = np.array([
|
| 383 |
+
[0, 3, 0, 0],
|
| 384 |
+
[0, 0, 3, 0],
|
| 385 |
+
[0, 0, 0, 3],
|
| 386 |
+
[0, 0, 0, 0]], dtype=dt)
|
| 387 |
+
A_sqrtm, info = sqrtm(A, disp=False)
|
| 388 |
+
assert_(np.isnan(A_sqrtm).all())
|
| 389 |
+
|
| 390 |
+
def test_weird_matrix(self):
|
| 391 |
+
# The square root of matrix B exists.
|
| 392 |
+
for dt in int, float:
|
| 393 |
+
A = np.array([
|
| 394 |
+
[0, 0, 1],
|
| 395 |
+
[0, 0, 0],
|
| 396 |
+
[0, 1, 0]], dtype=dt)
|
| 397 |
+
B = np.array([
|
| 398 |
+
[0, 1, 0],
|
| 399 |
+
[0, 0, 0],
|
| 400 |
+
[0, 0, 0]], dtype=dt)
|
| 401 |
+
assert_array_equal(B, A.dot(A))
|
| 402 |
+
|
| 403 |
+
# But scipy sqrtm is not clever enough to find it.
|
| 404 |
+
B_sqrtm, info = sqrtm(B, disp=False)
|
| 405 |
+
assert_(np.isnan(B_sqrtm).all())
|
| 406 |
+
|
| 407 |
+
def test_disp(self):
|
| 408 |
+
np.random.seed(1234)
|
| 409 |
+
|
| 410 |
+
A = np.random.rand(3, 3)
|
| 411 |
+
B = sqrtm(A, disp=True)
|
| 412 |
+
assert_allclose(B.dot(B), A)
|
| 413 |
+
|
| 414 |
+
def test_opposite_sign_complex_eigenvalues(self):
|
| 415 |
+
M = [[2j, 4], [0, -2j]]
|
| 416 |
+
R = [[1+1j, 2], [0, 1-1j]]
|
| 417 |
+
assert_allclose(np.dot(R, R), M, atol=1e-14)
|
| 418 |
+
assert_allclose(sqrtm(M), R, atol=1e-14)
|
| 419 |
+
|
| 420 |
+
def test_gh4866(self):
|
| 421 |
+
M = np.array([[1, 0, 0, 1],
|
| 422 |
+
[0, 0, 0, 0],
|
| 423 |
+
[0, 0, 0, 0],
|
| 424 |
+
[1, 0, 0, 1]])
|
| 425 |
+
R = np.array([[sqrt(0.5), 0, 0, sqrt(0.5)],
|
| 426 |
+
[0, 0, 0, 0],
|
| 427 |
+
[0, 0, 0, 0],
|
| 428 |
+
[sqrt(0.5), 0, 0, sqrt(0.5)]])
|
| 429 |
+
assert_allclose(np.dot(R, R), M, atol=1e-14)
|
| 430 |
+
assert_allclose(sqrtm(M), R, atol=1e-14)
|
| 431 |
+
|
| 432 |
+
def test_gh5336(self):
|
| 433 |
+
M = np.diag([2, 1, 0])
|
| 434 |
+
R = np.diag([sqrt(2), 1, 0])
|
| 435 |
+
assert_allclose(np.dot(R, R), M, atol=1e-14)
|
| 436 |
+
assert_allclose(sqrtm(M), R, atol=1e-14)
|
| 437 |
+
|
| 438 |
+
def test_gh7839(self):
|
| 439 |
+
M = np.zeros((2, 2))
|
| 440 |
+
R = np.zeros((2, 2))
|
| 441 |
+
assert_allclose(np.dot(R, R), M, atol=1e-14)
|
| 442 |
+
assert_allclose(sqrtm(M), R, atol=1e-14)
|
| 443 |
+
|
| 444 |
+
@pytest.mark.xfail(reason="failing on macOS after gh-20212")
|
| 445 |
+
def test_gh17918(self):
|
| 446 |
+
M = np.empty((19, 19))
|
| 447 |
+
M.fill(0.94)
|
| 448 |
+
np.fill_diagonal(M, 1)
|
| 449 |
+
assert np.isrealobj(sqrtm(M))
|
| 450 |
+
|
| 451 |
+
def test_data_size_preservation_uint_in_float_out(self):
|
| 452 |
+
M = np.zeros((10, 10), dtype=np.uint8)
|
| 453 |
+
assert sqrtm(M).dtype == np.float64
|
| 454 |
+
M = np.zeros((10, 10), dtype=np.uint16)
|
| 455 |
+
assert sqrtm(M).dtype == np.float64
|
| 456 |
+
M = np.zeros((10, 10), dtype=np.uint32)
|
| 457 |
+
assert sqrtm(M).dtype == np.float64
|
| 458 |
+
M = np.zeros((10, 10), dtype=np.uint64)
|
| 459 |
+
assert sqrtm(M).dtype == np.float64
|
| 460 |
+
|
| 461 |
+
def test_data_size_preservation_int_in_float_out(self):
|
| 462 |
+
M = np.zeros((10, 10), dtype=np.int8)
|
| 463 |
+
assert sqrtm(M).dtype == np.float64
|
| 464 |
+
M = np.zeros((10, 10), dtype=np.int16)
|
| 465 |
+
assert sqrtm(M).dtype == np.float64
|
| 466 |
+
M = np.zeros((10, 10), dtype=np.int32)
|
| 467 |
+
assert sqrtm(M).dtype == np.float64
|
| 468 |
+
M = np.zeros((10, 10), dtype=np.int64)
|
| 469 |
+
assert sqrtm(M).dtype == np.float64
|
| 470 |
+
|
| 471 |
+
def test_data_size_preservation_int_in_comp_out(self):
|
| 472 |
+
M = np.array([[2, 4], [0, -2]], dtype=np.int8)
|
| 473 |
+
assert sqrtm(M).dtype == np.complex128
|
| 474 |
+
M = np.array([[2, 4], [0, -2]], dtype=np.int16)
|
| 475 |
+
assert sqrtm(M).dtype == np.complex128
|
| 476 |
+
M = np.array([[2, 4], [0, -2]], dtype=np.int32)
|
| 477 |
+
assert sqrtm(M).dtype == np.complex128
|
| 478 |
+
M = np.array([[2, 4], [0, -2]], dtype=np.int64)
|
| 479 |
+
assert sqrtm(M).dtype == np.complex128
|
| 480 |
+
|
| 481 |
+
def test_data_size_preservation_float_in_float_out(self):
|
| 482 |
+
M = np.zeros((10, 10), dtype=np.float16)
|
| 483 |
+
assert sqrtm(M).dtype == np.float32
|
| 484 |
+
M = np.zeros((10, 10), dtype=np.float32)
|
| 485 |
+
assert sqrtm(M).dtype == np.float32
|
| 486 |
+
M = np.zeros((10, 10), dtype=np.float64)
|
| 487 |
+
assert sqrtm(M).dtype == np.float64
|
| 488 |
+
if hasattr(np, 'float128'):
|
| 489 |
+
M = np.zeros((10, 10), dtype=np.float128)
|
| 490 |
+
assert sqrtm(M).dtype == np.float64
|
| 491 |
+
|
| 492 |
+
def test_data_size_preservation_float_in_comp_out(self):
|
| 493 |
+
M = np.array([[2, 4], [0, -2]], dtype=np.float16)
|
| 494 |
+
assert sqrtm(M).dtype == np.complex64
|
| 495 |
+
M = np.array([[2, 4], [0, -2]], dtype=np.float32)
|
| 496 |
+
assert sqrtm(M).dtype == np.complex64
|
| 497 |
+
M = np.array([[2, 4], [0, -2]], dtype=np.float64)
|
| 498 |
+
assert sqrtm(M).dtype == np.complex128
|
| 499 |
+
if hasattr(np, 'float128') and hasattr(np, 'complex256'):
|
| 500 |
+
M = np.array([[2, 4], [0, -2]], dtype=np.float128)
|
| 501 |
+
assert sqrtm(M).dtype == np.complex128
|
| 502 |
+
|
| 503 |
+
def test_data_size_preservation_comp_in_comp_out(self):
|
| 504 |
+
M = np.array([[2j, 4], [0, -2j]], dtype=np.complex64)
|
| 505 |
+
assert sqrtm(M).dtype == np.complex64
|
| 506 |
+
M = np.array([[2j, 4], [0, -2j]], dtype=np.complex128)
|
| 507 |
+
assert sqrtm(M).dtype == np.complex128
|
| 508 |
+
if hasattr(np, 'complex256'):
|
| 509 |
+
M = np.array([[2j, 4], [0, -2j]], dtype=np.complex256)
|
| 510 |
+
assert sqrtm(M).dtype == np.complex128
|
| 511 |
+
|
| 512 |
+
@pytest.mark.parametrize('dt', [int, float, np.float32, complex, np.complex64])
|
| 513 |
+
def test_empty(self, dt):
|
| 514 |
+
a = np.empty((0, 0), dtype=dt)
|
| 515 |
+
s = sqrtm(a)
|
| 516 |
+
a0 = np.eye(2, dtype=dt)
|
| 517 |
+
s0 = sqrtm(a0)
|
| 518 |
+
|
| 519 |
+
assert s.shape == (0, 0)
|
| 520 |
+
assert s.dtype == s0.dtype
|
| 521 |
+
|
| 522 |
+
|
| 523 |
+
class TestFractionalMatrixPower:
|
| 524 |
+
def test_round_trip_random_complex(self):
|
| 525 |
+
np.random.seed(1234)
|
| 526 |
+
for p in range(1, 5):
|
| 527 |
+
for n in range(1, 5):
|
| 528 |
+
M_unscaled = np.random.randn(n, n) + 1j * np.random.randn(n, n)
|
| 529 |
+
for scale in np.logspace(-4, 4, 9):
|
| 530 |
+
M = M_unscaled * scale
|
| 531 |
+
M_root = fractional_matrix_power(M, 1/p)
|
| 532 |
+
M_round_trip = np.linalg.matrix_power(M_root, p)
|
| 533 |
+
assert_allclose(M_round_trip, M)
|
| 534 |
+
|
| 535 |
+
def test_round_trip_random_float(self):
|
| 536 |
+
# This test is more annoying because it can hit the branch cut;
|
| 537 |
+
# this happens when the matrix has an eigenvalue
|
| 538 |
+
# with no imaginary component and with a real negative component,
|
| 539 |
+
# and it means that the principal branch does not exist.
|
| 540 |
+
np.random.seed(1234)
|
| 541 |
+
for p in range(1, 5):
|
| 542 |
+
for n in range(1, 5):
|
| 543 |
+
M_unscaled = np.random.randn(n, n)
|
| 544 |
+
for scale in np.logspace(-4, 4, 9):
|
| 545 |
+
M = M_unscaled * scale
|
| 546 |
+
M_root = fractional_matrix_power(M, 1/p)
|
| 547 |
+
M_round_trip = np.linalg.matrix_power(M_root, p)
|
| 548 |
+
assert_allclose(M_round_trip, M)
|
| 549 |
+
|
| 550 |
+
def test_larger_abs_fractional_matrix_powers(self):
|
| 551 |
+
np.random.seed(1234)
|
| 552 |
+
for n in (2, 3, 5):
|
| 553 |
+
for i in range(10):
|
| 554 |
+
M = np.random.randn(n, n) + 1j * np.random.randn(n, n)
|
| 555 |
+
M_one_fifth = fractional_matrix_power(M, 0.2)
|
| 556 |
+
# Test the round trip.
|
| 557 |
+
M_round_trip = np.linalg.matrix_power(M_one_fifth, 5)
|
| 558 |
+
assert_allclose(M, M_round_trip)
|
| 559 |
+
# Test a large abs fractional power.
|
| 560 |
+
X = fractional_matrix_power(M, -5.4)
|
| 561 |
+
Y = np.linalg.matrix_power(M_one_fifth, -27)
|
| 562 |
+
assert_allclose(X, Y)
|
| 563 |
+
# Test another large abs fractional power.
|
| 564 |
+
X = fractional_matrix_power(M, 3.8)
|
| 565 |
+
Y = np.linalg.matrix_power(M_one_fifth, 19)
|
| 566 |
+
assert_allclose(X, Y)
|
| 567 |
+
|
| 568 |
+
def test_random_matrices_and_powers(self):
|
| 569 |
+
# Each independent iteration of this fuzz test picks random parameters.
|
| 570 |
+
# It tries to hit some edge cases.
|
| 571 |
+
rng = np.random.default_rng(1726500458620605)
|
| 572 |
+
nsamples = 20
|
| 573 |
+
for i in range(nsamples):
|
| 574 |
+
# Sample a matrix size and a random real power.
|
| 575 |
+
n = rng.integers(1, 5)
|
| 576 |
+
p = rng.random()
|
| 577 |
+
|
| 578 |
+
# Sample a random real or complex matrix.
|
| 579 |
+
matrix_scale = np.exp(rng.integers(-4, 5))
|
| 580 |
+
A = rng.random(size=[n, n])
|
| 581 |
+
if [True, False][rng.choice(2)]:
|
| 582 |
+
A = A + 1j * rng.random(size=[n, n])
|
| 583 |
+
A = A * matrix_scale
|
| 584 |
+
|
| 585 |
+
# Check a couple of analytically equivalent ways
|
| 586 |
+
# to compute the fractional matrix power.
|
| 587 |
+
# These can be compared because they both use the principal branch.
|
| 588 |
+
A_power = fractional_matrix_power(A, p)
|
| 589 |
+
A_logm, info = logm(A, disp=False)
|
| 590 |
+
A_power_expm_logm = expm(A_logm * p)
|
| 591 |
+
assert_allclose(A_power, A_power_expm_logm)
|
| 592 |
+
|
| 593 |
+
def test_al_mohy_higham_2012_experiment_1(self):
|
| 594 |
+
# Fractional powers of a tricky upper triangular matrix.
|
| 595 |
+
A = _get_al_mohy_higham_2012_experiment_1()
|
| 596 |
+
|
| 597 |
+
# Test remainder matrix power.
|
| 598 |
+
A_funm_sqrt, info = funm(A, np.sqrt, disp=False)
|
| 599 |
+
A_sqrtm, info = sqrtm(A, disp=False)
|
| 600 |
+
A_rem_power = _matfuncs_inv_ssq._remainder_matrix_power(A, 0.5)
|
| 601 |
+
A_power = fractional_matrix_power(A, 0.5)
|
| 602 |
+
assert_allclose(A_rem_power, A_power, rtol=1e-11)
|
| 603 |
+
assert_allclose(A_sqrtm, A_power)
|
| 604 |
+
assert_allclose(A_sqrtm, A_funm_sqrt)
|
| 605 |
+
|
| 606 |
+
# Test more fractional powers.
|
| 607 |
+
for p in (1/2, 5/3):
|
| 608 |
+
A_power = fractional_matrix_power(A, p)
|
| 609 |
+
A_round_trip = fractional_matrix_power(A_power, 1/p)
|
| 610 |
+
assert_allclose(A_round_trip, A, rtol=1e-2)
|
| 611 |
+
assert_allclose(np.tril(A_round_trip, 1), np.tril(A, 1))
|
| 612 |
+
|
| 613 |
+
def test_briggs_helper_function(self):
|
| 614 |
+
np.random.seed(1234)
|
| 615 |
+
for a in np.random.randn(10) + 1j * np.random.randn(10):
|
| 616 |
+
for k in range(5):
|
| 617 |
+
x_observed = _matfuncs_inv_ssq._briggs_helper_function(a, k)
|
| 618 |
+
x_expected = a ** np.exp2(-k) - 1
|
| 619 |
+
assert_allclose(x_observed, x_expected)
|
| 620 |
+
|
| 621 |
+
def test_type_preservation_and_conversion(self):
|
| 622 |
+
# The fractional_matrix_power matrix function should preserve
|
| 623 |
+
# the type of a matrix whose eigenvalues
|
| 624 |
+
# are positive with zero imaginary part.
|
| 625 |
+
# Test this preservation for variously structured matrices.
|
| 626 |
+
complex_dtype_chars = ('F', 'D', 'G')
|
| 627 |
+
for matrix_as_list in (
|
| 628 |
+
[[1, 0], [0, 1]],
|
| 629 |
+
[[1, 0], [1, 1]],
|
| 630 |
+
[[2, 1], [1, 1]],
|
| 631 |
+
[[2, 3], [1, 2]]):
|
| 632 |
+
|
| 633 |
+
# check that the spectrum has the expected properties
|
| 634 |
+
W = scipy.linalg.eigvals(matrix_as_list)
|
| 635 |
+
assert_(not any(w.imag or w.real < 0 for w in W))
|
| 636 |
+
|
| 637 |
+
# Check various positive and negative powers
|
| 638 |
+
# with absolute values bigger and smaller than 1.
|
| 639 |
+
for p in (-2.4, -0.9, 0.2, 3.3):
|
| 640 |
+
|
| 641 |
+
# check float type preservation
|
| 642 |
+
A = np.array(matrix_as_list, dtype=float)
|
| 643 |
+
A_power = fractional_matrix_power(A, p)
|
| 644 |
+
assert_(A_power.dtype.char not in complex_dtype_chars)
|
| 645 |
+
|
| 646 |
+
# check complex type preservation
|
| 647 |
+
A = np.array(matrix_as_list, dtype=complex)
|
| 648 |
+
A_power = fractional_matrix_power(A, p)
|
| 649 |
+
assert_(A_power.dtype.char in complex_dtype_chars)
|
| 650 |
+
|
| 651 |
+
# check float->complex for the matrix negation
|
| 652 |
+
A = -np.array(matrix_as_list, dtype=float)
|
| 653 |
+
A_power = fractional_matrix_power(A, p)
|
| 654 |
+
assert_(A_power.dtype.char in complex_dtype_chars)
|
| 655 |
+
|
| 656 |
+
def test_type_conversion_mixed_sign_or_complex_spectrum(self):
|
| 657 |
+
complex_dtype_chars = ('F', 'D', 'G')
|
| 658 |
+
for matrix_as_list in (
|
| 659 |
+
[[1, 0], [0, -1]],
|
| 660 |
+
[[0, 1], [1, 0]],
|
| 661 |
+
[[0, 1, 0], [0, 0, 1], [1, 0, 0]]):
|
| 662 |
+
|
| 663 |
+
# check that the spectrum has the expected properties
|
| 664 |
+
W = scipy.linalg.eigvals(matrix_as_list)
|
| 665 |
+
assert_(any(w.imag or w.real < 0 for w in W))
|
| 666 |
+
|
| 667 |
+
# Check various positive and negative powers
|
| 668 |
+
# with absolute values bigger and smaller than 1.
|
| 669 |
+
for p in (-2.4, -0.9, 0.2, 3.3):
|
| 670 |
+
|
| 671 |
+
# check complex->complex
|
| 672 |
+
A = np.array(matrix_as_list, dtype=complex)
|
| 673 |
+
A_power = fractional_matrix_power(A, p)
|
| 674 |
+
assert_(A_power.dtype.char in complex_dtype_chars)
|
| 675 |
+
|
| 676 |
+
# check float->complex
|
| 677 |
+
A = np.array(matrix_as_list, dtype=float)
|
| 678 |
+
A_power = fractional_matrix_power(A, p)
|
| 679 |
+
assert_(A_power.dtype.char in complex_dtype_chars)
|
| 680 |
+
|
| 681 |
+
@pytest.mark.xfail(reason='Too unstable across LAPACKs.')
|
| 682 |
+
def test_singular(self):
|
| 683 |
+
# Negative fractional powers do not work with singular matrices.
|
| 684 |
+
for matrix_as_list in (
|
| 685 |
+
[[0, 0], [0, 0]],
|
| 686 |
+
[[1, 1], [1, 1]],
|
| 687 |
+
[[1, 2], [3, 6]],
|
| 688 |
+
[[0, 0, 0], [0, 1, 1], [0, -1, 1]]):
|
| 689 |
+
|
| 690 |
+
# Check fractional powers both for float and for complex types.
|
| 691 |
+
for newtype in (float, complex):
|
| 692 |
+
A = np.array(matrix_as_list, dtype=newtype)
|
| 693 |
+
for p in (-0.7, -0.9, -2.4, -1.3):
|
| 694 |
+
A_power = fractional_matrix_power(A, p)
|
| 695 |
+
assert_(np.isnan(A_power).all())
|
| 696 |
+
for p in (0.2, 1.43):
|
| 697 |
+
A_power = fractional_matrix_power(A, p)
|
| 698 |
+
A_round_trip = fractional_matrix_power(A_power, 1/p)
|
| 699 |
+
assert_allclose(A_round_trip, A)
|
| 700 |
+
|
| 701 |
+
def test_opposite_sign_complex_eigenvalues(self):
|
| 702 |
+
M = [[2j, 4], [0, -2j]]
|
| 703 |
+
R = [[1+1j, 2], [0, 1-1j]]
|
| 704 |
+
assert_allclose(np.dot(R, R), M, atol=1e-14)
|
| 705 |
+
assert_allclose(fractional_matrix_power(M, 0.5), R, atol=1e-14)
|
| 706 |
+
|
| 707 |
+
|
| 708 |
+
class TestExpM:
|
| 709 |
+
def test_zero(self):
|
| 710 |
+
a = array([[0.,0],[0,0]])
|
| 711 |
+
assert_array_almost_equal(expm(a),[[1,0],[0,1]])
|
| 712 |
+
|
| 713 |
+
def test_single_elt(self):
|
| 714 |
+
elt = expm(1)
|
| 715 |
+
assert_allclose(elt, np.array([[np.e]]))
|
| 716 |
+
|
| 717 |
+
@pytest.mark.parametrize('func', [expm, cosm, sinm, tanm, coshm, sinhm, tanhm])
|
| 718 |
+
@pytest.mark.parametrize('dt',[int, float, np.float32, complex, np.complex64])
|
| 719 |
+
@pytest.mark.parametrize('shape', [(0, 0), (1, 1)])
|
| 720 |
+
def test_small_empty_matrix_input(self, func, dt, shape):
|
| 721 |
+
# regression test for gh-11082 / gh-20372 - test behavior of expm
|
| 722 |
+
# and related functions for small and zero-sized arrays.
|
| 723 |
+
A = np.zeros(shape, dtype=dt)
|
| 724 |
+
A0 = np.zeros((10, 10), dtype=dt)
|
| 725 |
+
result = func(A)
|
| 726 |
+
result0 = func(A0)
|
| 727 |
+
assert result.shape == shape
|
| 728 |
+
assert result.dtype == result0.dtype
|
| 729 |
+
|
| 730 |
+
def test_2x2_input(self):
|
| 731 |
+
E = np.e
|
| 732 |
+
a = array([[1, 4], [1, 1]])
|
| 733 |
+
aa = (E**4 + 1)/(2*E)
|
| 734 |
+
bb = (E**4 - 1)/E
|
| 735 |
+
assert_allclose(expm(a), array([[aa, bb], [bb/4, aa]]))
|
| 736 |
+
assert expm(a.astype(np.complex64)).dtype.char == 'F'
|
| 737 |
+
assert expm(a.astype(np.float32)).dtype.char == 'f'
|
| 738 |
+
|
| 739 |
+
def test_nx2x2_input(self):
|
| 740 |
+
E = np.e
|
| 741 |
+
# These are integer matrices with integer eigenvalues
|
| 742 |
+
a = np.array([[[1, 4], [1, 1]],
|
| 743 |
+
[[1, 3], [1, -1]],
|
| 744 |
+
[[1, 3], [4, 5]],
|
| 745 |
+
[[1, 3], [5, 3]],
|
| 746 |
+
[[4, 5], [-3, -4]]], order='F')
|
| 747 |
+
# Exact results are computed symbolically
|
| 748 |
+
a_res = np.array([
|
| 749 |
+
[[(E**4+1)/(2*E), (E**4-1)/E],
|
| 750 |
+
[(E**4-1)/4/E, (E**4+1)/(2*E)]],
|
| 751 |
+
[[1/(4*E**2)+(3*E**2)/4, (3*E**2)/4-3/(4*E**2)],
|
| 752 |
+
[E**2/4-1/(4*E**2), 3/(4*E**2)+E**2/4]],
|
| 753 |
+
[[3/(4*E)+E**7/4, -3/(8*E)+(3*E**7)/8],
|
| 754 |
+
[-1/(2*E)+E**7/2, 1/(4*E)+(3*E**7)/4]],
|
| 755 |
+
[[5/(8*E**2)+(3*E**6)/8, -3/(8*E**2)+(3*E**6)/8],
|
| 756 |
+
[-5/(8*E**2)+(5*E**6)/8, 3/(8*E**2)+(5*E**6)/8]],
|
| 757 |
+
[[-3/(2*E)+(5*E)/2, -5/(2*E)+(5*E)/2],
|
| 758 |
+
[3/(2*E)-(3*E)/2, 5/(2*E)-(3*E)/2]]
|
| 759 |
+
])
|
| 760 |
+
assert_allclose(expm(a), a_res)
|
| 761 |
+
|
| 762 |
+
def test_readonly(self):
|
| 763 |
+
n = 7
|
| 764 |
+
a = np.ones((n, n))
|
| 765 |
+
a.flags.writeable = False
|
| 766 |
+
expm(a)
|
| 767 |
+
|
| 768 |
+
@pytest.mark.thread_unsafe
|
| 769 |
+
@pytest.mark.fail_slow(5)
|
| 770 |
+
def test_gh18086(self):
|
| 771 |
+
A = np.zeros((400, 400), dtype=float)
|
| 772 |
+
rng = np.random.default_rng(100)
|
| 773 |
+
i = rng.integers(0, 399, 500)
|
| 774 |
+
j = rng.integers(0, 399, 500)
|
| 775 |
+
A[i, j] = rng.random(500)
|
| 776 |
+
# Problem appears when m = 9
|
| 777 |
+
Am = np.empty((5, 400, 400), dtype=float)
|
| 778 |
+
Am[0] = A.copy()
|
| 779 |
+
m, s = pick_pade_structure(Am)
|
| 780 |
+
assert m == 9
|
| 781 |
+
# Check that result is accurate
|
| 782 |
+
first_res = expm(A)
|
| 783 |
+
np.testing.assert_array_almost_equal(logm(first_res), A)
|
| 784 |
+
# Check that result is consistent
|
| 785 |
+
for i in range(5):
|
| 786 |
+
next_res = expm(A)
|
| 787 |
+
np.testing.assert_array_almost_equal(first_res, next_res)
|
| 788 |
+
|
| 789 |
+
|
| 790 |
+
class TestExpmFrechet:
|
| 791 |
+
|
| 792 |
+
def test_expm_frechet(self):
|
| 793 |
+
# a test of the basic functionality
|
| 794 |
+
M = np.array([
|
| 795 |
+
[1, 2, 3, 4],
|
| 796 |
+
[5, 6, 7, 8],
|
| 797 |
+
[0, 0, 1, 2],
|
| 798 |
+
[0, 0, 5, 6],
|
| 799 |
+
], dtype=float)
|
| 800 |
+
A = np.array([
|
| 801 |
+
[1, 2],
|
| 802 |
+
[5, 6],
|
| 803 |
+
], dtype=float)
|
| 804 |
+
E = np.array([
|
| 805 |
+
[3, 4],
|
| 806 |
+
[7, 8],
|
| 807 |
+
], dtype=float)
|
| 808 |
+
expected_expm = scipy.linalg.expm(A)
|
| 809 |
+
expected_frechet = scipy.linalg.expm(M)[:2, 2:]
|
| 810 |
+
for kwargs in ({}, {'method':'SPS'}, {'method':'blockEnlarge'}):
|
| 811 |
+
observed_expm, observed_frechet = expm_frechet(A, E, **kwargs)
|
| 812 |
+
assert_allclose(expected_expm, observed_expm)
|
| 813 |
+
assert_allclose(expected_frechet, observed_frechet)
|
| 814 |
+
|
| 815 |
+
def test_small_norm_expm_frechet(self):
|
| 816 |
+
# methodically test matrices with a range of norms, for better coverage
|
| 817 |
+
M_original = np.array([
|
| 818 |
+
[1, 2, 3, 4],
|
| 819 |
+
[5, 6, 7, 8],
|
| 820 |
+
[0, 0, 1, 2],
|
| 821 |
+
[0, 0, 5, 6],
|
| 822 |
+
], dtype=float)
|
| 823 |
+
A_original = np.array([
|
| 824 |
+
[1, 2],
|
| 825 |
+
[5, 6],
|
| 826 |
+
], dtype=float)
|
| 827 |
+
E_original = np.array([
|
| 828 |
+
[3, 4],
|
| 829 |
+
[7, 8],
|
| 830 |
+
], dtype=float)
|
| 831 |
+
A_original_norm_1 = scipy.linalg.norm(A_original, 1)
|
| 832 |
+
selected_m_list = [1, 3, 5, 7, 9, 11, 13, 15]
|
| 833 |
+
m_neighbor_pairs = zip(selected_m_list[:-1], selected_m_list[1:])
|
| 834 |
+
for ma, mb in m_neighbor_pairs:
|
| 835 |
+
ell_a = scipy.linalg._expm_frechet.ell_table_61[ma]
|
| 836 |
+
ell_b = scipy.linalg._expm_frechet.ell_table_61[mb]
|
| 837 |
+
target_norm_1 = 0.5 * (ell_a + ell_b)
|
| 838 |
+
scale = target_norm_1 / A_original_norm_1
|
| 839 |
+
M = scale * M_original
|
| 840 |
+
A = scale * A_original
|
| 841 |
+
E = scale * E_original
|
| 842 |
+
expected_expm = scipy.linalg.expm(A)
|
| 843 |
+
expected_frechet = scipy.linalg.expm(M)[:2, 2:]
|
| 844 |
+
observed_expm, observed_frechet = expm_frechet(A, E)
|
| 845 |
+
assert_allclose(expected_expm, observed_expm)
|
| 846 |
+
assert_allclose(expected_frechet, observed_frechet)
|
| 847 |
+
|
| 848 |
+
def test_fuzz(self):
|
| 849 |
+
rng = np.random.default_rng(1726500908359153)
|
| 850 |
+
# try a bunch of crazy inputs
|
| 851 |
+
rfuncs = (
|
| 852 |
+
np.random.uniform,
|
| 853 |
+
np.random.normal,
|
| 854 |
+
np.random.standard_cauchy,
|
| 855 |
+
np.random.exponential)
|
| 856 |
+
ntests = 100
|
| 857 |
+
for i in range(ntests):
|
| 858 |
+
rfunc = rfuncs[rng.choice(4)]
|
| 859 |
+
target_norm_1 = rng.exponential()
|
| 860 |
+
n = rng.integers(2, 16)
|
| 861 |
+
A_original = rfunc(size=(n,n))
|
| 862 |
+
E_original = rfunc(size=(n,n))
|
| 863 |
+
A_original_norm_1 = scipy.linalg.norm(A_original, 1)
|
| 864 |
+
scale = target_norm_1 / A_original_norm_1
|
| 865 |
+
A = scale * A_original
|
| 866 |
+
E = scale * E_original
|
| 867 |
+
M = np.vstack([
|
| 868 |
+
np.hstack([A, E]),
|
| 869 |
+
np.hstack([np.zeros_like(A), A])])
|
| 870 |
+
expected_expm = scipy.linalg.expm(A)
|
| 871 |
+
expected_frechet = scipy.linalg.expm(M)[:n, n:]
|
| 872 |
+
observed_expm, observed_frechet = expm_frechet(A, E)
|
| 873 |
+
assert_allclose(expected_expm, observed_expm, atol=5e-8)
|
| 874 |
+
assert_allclose(expected_frechet, observed_frechet, atol=1e-7)
|
| 875 |
+
|
| 876 |
+
def test_problematic_matrix(self):
|
| 877 |
+
# this test case uncovered a bug which has since been fixed
|
| 878 |
+
A = np.array([
|
| 879 |
+
[1.50591997, 1.93537998],
|
| 880 |
+
[0.41203263, 0.23443516],
|
| 881 |
+
], dtype=float)
|
| 882 |
+
E = np.array([
|
| 883 |
+
[1.87864034, 2.07055038],
|
| 884 |
+
[1.34102727, 0.67341123],
|
| 885 |
+
], dtype=float)
|
| 886 |
+
scipy.linalg.norm(A, 1)
|
| 887 |
+
sps_expm, sps_frechet = expm_frechet(
|
| 888 |
+
A, E, method='SPS')
|
| 889 |
+
blockEnlarge_expm, blockEnlarge_frechet = expm_frechet(
|
| 890 |
+
A, E, method='blockEnlarge')
|
| 891 |
+
assert_allclose(sps_expm, blockEnlarge_expm)
|
| 892 |
+
assert_allclose(sps_frechet, blockEnlarge_frechet)
|
| 893 |
+
|
| 894 |
+
@pytest.mark.slow
|
| 895 |
+
@pytest.mark.skip(reason='this test is deliberately slow')
|
| 896 |
+
def test_medium_matrix(self):
|
| 897 |
+
# profile this to see the speed difference
|
| 898 |
+
n = 1000
|
| 899 |
+
A = np.random.exponential(size=(n, n))
|
| 900 |
+
E = np.random.exponential(size=(n, n))
|
| 901 |
+
sps_expm, sps_frechet = expm_frechet(
|
| 902 |
+
A, E, method='SPS')
|
| 903 |
+
blockEnlarge_expm, blockEnlarge_frechet = expm_frechet(
|
| 904 |
+
A, E, method='blockEnlarge')
|
| 905 |
+
assert_allclose(sps_expm, blockEnlarge_expm)
|
| 906 |
+
assert_allclose(sps_frechet, blockEnlarge_frechet)
|
| 907 |
+
|
| 908 |
+
|
| 909 |
+
def _help_expm_cond_search(A, A_norm, X, X_norm, eps, p):
|
| 910 |
+
p = np.reshape(p, A.shape)
|
| 911 |
+
p_norm = norm(p)
|
| 912 |
+
perturbation = eps * p * (A_norm / p_norm)
|
| 913 |
+
X_prime = expm(A + perturbation)
|
| 914 |
+
scaled_relative_error = norm(X_prime - X) / (X_norm * eps)
|
| 915 |
+
return -scaled_relative_error
|
| 916 |
+
|
| 917 |
+
|
| 918 |
+
def _normalized_like(A, B):
|
| 919 |
+
return A * (scipy.linalg.norm(B) / scipy.linalg.norm(A))
|
| 920 |
+
|
| 921 |
+
|
| 922 |
+
def _relative_error(f, A, perturbation):
|
| 923 |
+
X = f(A)
|
| 924 |
+
X_prime = f(A + perturbation)
|
| 925 |
+
return norm(X_prime - X) / norm(X)
|
| 926 |
+
|
| 927 |
+
|
| 928 |
+
class TestExpmConditionNumber:
|
| 929 |
+
def test_expm_cond_smoke(self):
|
| 930 |
+
np.random.seed(1234)
|
| 931 |
+
for n in range(1, 4):
|
| 932 |
+
A = np.random.randn(n, n)
|
| 933 |
+
kappa = expm_cond(A)
|
| 934 |
+
assert_array_less(0, kappa)
|
| 935 |
+
|
| 936 |
+
def test_expm_bad_condition_number(self):
|
| 937 |
+
A = np.array([
|
| 938 |
+
[-1.128679820, 9.614183771e4, -4.524855739e9, 2.924969411e14],
|
| 939 |
+
[0, -1.201010529, 9.634696872e4, -4.681048289e9],
|
| 940 |
+
[0, 0, -1.132893222, 9.532491830e4],
|
| 941 |
+
[0, 0, 0, -1.179475332],
|
| 942 |
+
])
|
| 943 |
+
kappa = expm_cond(A)
|
| 944 |
+
assert_array_less(1e36, kappa)
|
| 945 |
+
|
| 946 |
+
def test_univariate(self):
|
| 947 |
+
np.random.seed(12345)
|
| 948 |
+
for x in np.linspace(-5, 5, num=11):
|
| 949 |
+
A = np.array([[x]])
|
| 950 |
+
assert_allclose(expm_cond(A), abs(x))
|
| 951 |
+
for x in np.logspace(-2, 2, num=11):
|
| 952 |
+
A = np.array([[x]])
|
| 953 |
+
assert_allclose(expm_cond(A), abs(x))
|
| 954 |
+
for i in range(10):
|
| 955 |
+
A = np.random.randn(1, 1)
|
| 956 |
+
assert_allclose(expm_cond(A), np.absolute(A)[0, 0])
|
| 957 |
+
|
| 958 |
+
@pytest.mark.slow
|
| 959 |
+
def test_expm_cond_fuzz(self):
|
| 960 |
+
rng = np.random.RandomState(12345)
|
| 961 |
+
eps = 1e-5
|
| 962 |
+
nsamples = 10
|
| 963 |
+
for i in range(nsamples):
|
| 964 |
+
n = rng.randint(2, 5)
|
| 965 |
+
A = rng.randn(n, n)
|
| 966 |
+
A_norm = scipy.linalg.norm(A)
|
| 967 |
+
X = expm(A)
|
| 968 |
+
X_norm = scipy.linalg.norm(X)
|
| 969 |
+
kappa = expm_cond(A)
|
| 970 |
+
|
| 971 |
+
# Look for the small perturbation that gives the greatest
|
| 972 |
+
# relative error.
|
| 973 |
+
f = functools.partial(_help_expm_cond_search,
|
| 974 |
+
A, A_norm, X, X_norm, eps)
|
| 975 |
+
guess = np.ones(n*n)
|
| 976 |
+
out = minimize(f, guess, method='L-BFGS-B')
|
| 977 |
+
xopt = out.x
|
| 978 |
+
yopt = f(xopt)
|
| 979 |
+
p_best = eps * _normalized_like(np.reshape(xopt, A.shape), A)
|
| 980 |
+
p_best_relerr = _relative_error(expm, A, p_best)
|
| 981 |
+
assert_allclose(p_best_relerr, -yopt * eps)
|
| 982 |
+
|
| 983 |
+
# Check that the identified perturbation indeed gives greater
|
| 984 |
+
# relative error than random perturbations with similar norms.
|
| 985 |
+
for j in range(5):
|
| 986 |
+
p_rand = eps * _normalized_like(rng.randn(*A.shape), A)
|
| 987 |
+
assert_allclose(norm(p_best), norm(p_rand))
|
| 988 |
+
p_rand_relerr = _relative_error(expm, A, p_rand)
|
| 989 |
+
assert_array_less(p_rand_relerr, p_best_relerr)
|
| 990 |
+
|
| 991 |
+
# The greatest relative error should not be much greater than
|
| 992 |
+
# eps times the condition number kappa.
|
| 993 |
+
# In the limit as eps approaches zero it should never be greater.
|
| 994 |
+
assert_array_less(p_best_relerr, (1 + 2*eps) * eps * kappa)
|
| 995 |
+
|
| 996 |
+
|
| 997 |
+
class TestKhatriRao:
|
| 998 |
+
|
| 999 |
+
def test_basic(self):
|
| 1000 |
+
a = khatri_rao(array([[1, 2], [3, 4]]),
|
| 1001 |
+
array([[5, 6], [7, 8]]))
|
| 1002 |
+
|
| 1003 |
+
assert_array_equal(a, array([[5, 12],
|
| 1004 |
+
[7, 16],
|
| 1005 |
+
[15, 24],
|
| 1006 |
+
[21, 32]]))
|
| 1007 |
+
|
| 1008 |
+
b = khatri_rao(np.empty([2, 2]), np.empty([2, 2]))
|
| 1009 |
+
assert_array_equal(b.shape, (4, 2))
|
| 1010 |
+
|
| 1011 |
+
def test_number_of_columns_equality(self):
|
| 1012 |
+
with pytest.raises(ValueError):
|
| 1013 |
+
a = array([[1, 2, 3],
|
| 1014 |
+
[4, 5, 6]])
|
| 1015 |
+
b = array([[1, 2],
|
| 1016 |
+
[3, 4]])
|
| 1017 |
+
khatri_rao(a, b)
|
| 1018 |
+
|
| 1019 |
+
def test_to_assure_2d_array(self):
|
| 1020 |
+
with pytest.raises(ValueError):
|
| 1021 |
+
# both arrays are 1-D
|
| 1022 |
+
a = array([1, 2, 3])
|
| 1023 |
+
b = array([4, 5, 6])
|
| 1024 |
+
khatri_rao(a, b)
|
| 1025 |
+
|
| 1026 |
+
with pytest.raises(ValueError):
|
| 1027 |
+
# first array is 1-D
|
| 1028 |
+
a = array([1, 2, 3])
|
| 1029 |
+
b = array([
|
| 1030 |
+
[1, 2, 3],
|
| 1031 |
+
[4, 5, 6]
|
| 1032 |
+
])
|
| 1033 |
+
khatri_rao(a, b)
|
| 1034 |
+
|
| 1035 |
+
with pytest.raises(ValueError):
|
| 1036 |
+
# second array is 1-D
|
| 1037 |
+
a = array([
|
| 1038 |
+
[1, 2, 3],
|
| 1039 |
+
[7, 8, 9]
|
| 1040 |
+
])
|
| 1041 |
+
b = array([4, 5, 6])
|
| 1042 |
+
khatri_rao(a, b)
|
| 1043 |
+
|
| 1044 |
+
def test_equality_of_two_equations(self):
|
| 1045 |
+
a = array([[1, 2], [3, 4]])
|
| 1046 |
+
b = array([[5, 6], [7, 8]])
|
| 1047 |
+
|
| 1048 |
+
res1 = khatri_rao(a, b)
|
| 1049 |
+
res2 = np.vstack([np.kron(a[:, k], b[:, k])
|
| 1050 |
+
for k in range(b.shape[1])]).T
|
| 1051 |
+
|
| 1052 |
+
assert_array_equal(res1, res2)
|
| 1053 |
+
|
| 1054 |
+
def test_empty(self):
|
| 1055 |
+
a = np.empty((0, 2))
|
| 1056 |
+
b = np.empty((3, 2))
|
| 1057 |
+
res = khatri_rao(a, b)
|
| 1058 |
+
assert_allclose(res, np.empty((0, 2)))
|
| 1059 |
+
|
| 1060 |
+
a = np.empty((3, 0))
|
| 1061 |
+
b = np.empty((5, 0))
|
| 1062 |
+
res = khatri_rao(a, b)
|
| 1063 |
+
assert_allclose(res, np.empty((15, 0)))
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_matmul_toeplitz.py
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Test functions for linalg.matmul_toeplitz function
|
| 2 |
+
"""
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
from scipy.linalg import toeplitz, matmul_toeplitz
|
| 6 |
+
|
| 7 |
+
from pytest import raises as assert_raises
|
| 8 |
+
from numpy.testing import assert_allclose
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class TestMatmulToeplitz:
|
| 12 |
+
|
| 13 |
+
def setup_method(self):
|
| 14 |
+
self.rng = np.random.RandomState(42)
|
| 15 |
+
self.tolerance = 1.5e-13
|
| 16 |
+
|
| 17 |
+
def test_real(self):
|
| 18 |
+
cases = []
|
| 19 |
+
|
| 20 |
+
n = 1
|
| 21 |
+
c = self.rng.normal(size=n)
|
| 22 |
+
r = self.rng.normal(size=n)
|
| 23 |
+
x = self.rng.normal(size=(n, 1))
|
| 24 |
+
cases.append((x, c, r, False))
|
| 25 |
+
|
| 26 |
+
n = 2
|
| 27 |
+
c = self.rng.normal(size=n)
|
| 28 |
+
r = self.rng.normal(size=n)
|
| 29 |
+
x = self.rng.normal(size=(n, 1))
|
| 30 |
+
cases.append((x, c, r, False))
|
| 31 |
+
|
| 32 |
+
n = 101
|
| 33 |
+
c = self.rng.normal(size=n)
|
| 34 |
+
r = self.rng.normal(size=n)
|
| 35 |
+
x = self.rng.normal(size=(n, 1))
|
| 36 |
+
cases.append((x, c, r, True))
|
| 37 |
+
|
| 38 |
+
n = 1000
|
| 39 |
+
c = self.rng.normal(size=n)
|
| 40 |
+
r = self.rng.normal(size=n)
|
| 41 |
+
x = self.rng.normal(size=(n, 1))
|
| 42 |
+
cases.append((x, c, r, False))
|
| 43 |
+
|
| 44 |
+
n = 100
|
| 45 |
+
c = self.rng.normal(size=n)
|
| 46 |
+
r = self.rng.normal(size=n)
|
| 47 |
+
x = self.rng.normal(size=(n, self.rng.randint(1, 10)))
|
| 48 |
+
cases.append((x, c, r, False))
|
| 49 |
+
|
| 50 |
+
n = 100
|
| 51 |
+
c = self.rng.normal(size=(n, 1))
|
| 52 |
+
r = self.rng.normal(size=(n, 1))
|
| 53 |
+
x = self.rng.normal(size=(n, self.rng.randint(1, 10)))
|
| 54 |
+
cases.append((x, c, r, True))
|
| 55 |
+
|
| 56 |
+
n = 100
|
| 57 |
+
c = self.rng.normal(size=(n, 1))
|
| 58 |
+
r = None
|
| 59 |
+
x = self.rng.normal(size=(n, self.rng.randint(1, 10)))
|
| 60 |
+
cases.append((x, c, r, True, -1))
|
| 61 |
+
|
| 62 |
+
n = 100
|
| 63 |
+
c = self.rng.normal(size=(n, 1))
|
| 64 |
+
r = None
|
| 65 |
+
x = self.rng.normal(size=n)
|
| 66 |
+
cases.append((x, c, r, False))
|
| 67 |
+
|
| 68 |
+
n = 101
|
| 69 |
+
c = self.rng.normal(size=n)
|
| 70 |
+
r = self.rng.normal(size=n-27)
|
| 71 |
+
x = self.rng.normal(size=(n-27, 1))
|
| 72 |
+
cases.append((x, c, r, True))
|
| 73 |
+
|
| 74 |
+
n = 100
|
| 75 |
+
c = self.rng.normal(size=n)
|
| 76 |
+
r = self.rng.normal(size=n//4)
|
| 77 |
+
x = self.rng.normal(size=(n//4, self.rng.randint(1, 10)))
|
| 78 |
+
cases.append((x, c, r, True))
|
| 79 |
+
|
| 80 |
+
[self.do(*i) for i in cases]
|
| 81 |
+
|
| 82 |
+
def test_complex(self):
|
| 83 |
+
n = 127
|
| 84 |
+
c = self.rng.normal(size=(n, 1)) + self.rng.normal(size=(n, 1))*1j
|
| 85 |
+
r = self.rng.normal(size=(n, 1)) + self.rng.normal(size=(n, 1))*1j
|
| 86 |
+
x = self.rng.normal(size=(n, 3)) + self.rng.normal(size=(n, 3))*1j
|
| 87 |
+
self.do(x, c, r, False)
|
| 88 |
+
|
| 89 |
+
n = 100
|
| 90 |
+
c = self.rng.normal(size=(n, 1)) + self.rng.normal(size=(n, 1))*1j
|
| 91 |
+
r = self.rng.normal(size=(n//2, 1)) +\
|
| 92 |
+
self.rng.normal(size=(n//2, 1))*1j
|
| 93 |
+
x = self.rng.normal(size=(n//2, 3)) +\
|
| 94 |
+
self.rng.normal(size=(n//2, 3))*1j
|
| 95 |
+
self.do(x, c, r, False)
|
| 96 |
+
|
| 97 |
+
def test_empty(self):
|
| 98 |
+
c = []
|
| 99 |
+
r = []
|
| 100 |
+
x = []
|
| 101 |
+
self.do(x, c, r, False)
|
| 102 |
+
|
| 103 |
+
x = np.empty((0, 0))
|
| 104 |
+
self.do(x, c, r, False)
|
| 105 |
+
|
| 106 |
+
def test_exceptions(self):
|
| 107 |
+
|
| 108 |
+
n = 100
|
| 109 |
+
c = self.rng.normal(size=n)
|
| 110 |
+
r = self.rng.normal(size=2*n)
|
| 111 |
+
x = self.rng.normal(size=n)
|
| 112 |
+
assert_raises(ValueError, matmul_toeplitz, (c, r), x, True)
|
| 113 |
+
|
| 114 |
+
n = 100
|
| 115 |
+
c = self.rng.normal(size=n)
|
| 116 |
+
r = self.rng.normal(size=n)
|
| 117 |
+
x = self.rng.normal(size=n-1)
|
| 118 |
+
assert_raises(ValueError, matmul_toeplitz, (c, r), x, True)
|
| 119 |
+
|
| 120 |
+
n = 100
|
| 121 |
+
c = self.rng.normal(size=n)
|
| 122 |
+
r = self.rng.normal(size=n//2)
|
| 123 |
+
x = self.rng.normal(size=n//2-1)
|
| 124 |
+
assert_raises(ValueError, matmul_toeplitz, (c, r), x, True)
|
| 125 |
+
|
| 126 |
+
# For toeplitz matrices, matmul_toeplitz() should be equivalent to @.
|
| 127 |
+
def do(self, x, c, r=None, check_finite=False, workers=None):
|
| 128 |
+
c = np.ravel(c)
|
| 129 |
+
if r is None:
|
| 130 |
+
actual = matmul_toeplitz(c, x, check_finite, workers)
|
| 131 |
+
else:
|
| 132 |
+
r = np.ravel(r)
|
| 133 |
+
actual = matmul_toeplitz((c, r), x, check_finite)
|
| 134 |
+
desired = toeplitz(c, r) @ x
|
| 135 |
+
assert_allclose(actual, desired,
|
| 136 |
+
rtol=self.tolerance, atol=self.tolerance)
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_procrustes.py
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from itertools import product, permutations
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pytest
|
| 5 |
+
from numpy.testing import assert_array_less, assert_allclose
|
| 6 |
+
from pytest import raises as assert_raises
|
| 7 |
+
|
| 8 |
+
from scipy.linalg import inv, eigh, norm, svd
|
| 9 |
+
from scipy.linalg import orthogonal_procrustes
|
| 10 |
+
from scipy.sparse._sputils import matrix
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def test_orthogonal_procrustes_ndim_too_large():
|
| 14 |
+
rng = np.random.RandomState(1234)
|
| 15 |
+
A = rng.randn(3, 4, 5)
|
| 16 |
+
B = rng.randn(3, 4, 5)
|
| 17 |
+
assert_raises(ValueError, orthogonal_procrustes, A, B)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def test_orthogonal_procrustes_ndim_too_small():
|
| 21 |
+
rng = np.random.RandomState(1234)
|
| 22 |
+
A = rng.randn(3)
|
| 23 |
+
B = rng.randn(3)
|
| 24 |
+
assert_raises(ValueError, orthogonal_procrustes, A, B)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def test_orthogonal_procrustes_shape_mismatch():
|
| 28 |
+
rng = np.random.RandomState(1234)
|
| 29 |
+
shapes = ((3, 3), (3, 4), (4, 3), (4, 4))
|
| 30 |
+
for a, b in permutations(shapes, 2):
|
| 31 |
+
A = rng.randn(*a)
|
| 32 |
+
B = rng.randn(*b)
|
| 33 |
+
assert_raises(ValueError, orthogonal_procrustes, A, B)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def test_orthogonal_procrustes_checkfinite_exception():
|
| 37 |
+
rng = np.random.RandomState(1234)
|
| 38 |
+
m, n = 2, 3
|
| 39 |
+
A_good = rng.randn(m, n)
|
| 40 |
+
B_good = rng.randn(m, n)
|
| 41 |
+
for bad_value in np.inf, -np.inf, np.nan:
|
| 42 |
+
A_bad = A_good.copy()
|
| 43 |
+
A_bad[1, 2] = bad_value
|
| 44 |
+
B_bad = B_good.copy()
|
| 45 |
+
B_bad[1, 2] = bad_value
|
| 46 |
+
for A, B in ((A_good, B_bad), (A_bad, B_good), (A_bad, B_bad)):
|
| 47 |
+
assert_raises(ValueError, orthogonal_procrustes, A, B)
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def test_orthogonal_procrustes_scale_invariance():
|
| 51 |
+
rng = np.random.RandomState(1234)
|
| 52 |
+
m, n = 4, 3
|
| 53 |
+
for i in range(3):
|
| 54 |
+
A_orig = rng.randn(m, n)
|
| 55 |
+
B_orig = rng.randn(m, n)
|
| 56 |
+
R_orig, s = orthogonal_procrustes(A_orig, B_orig)
|
| 57 |
+
for A_scale in np.square(rng.randn(3)):
|
| 58 |
+
for B_scale in np.square(rng.randn(3)):
|
| 59 |
+
R, s = orthogonal_procrustes(A_orig * A_scale, B_orig * B_scale)
|
| 60 |
+
assert_allclose(R, R_orig)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def test_orthogonal_procrustes_array_conversion():
|
| 64 |
+
rng = np.random.RandomState(1234)
|
| 65 |
+
for m, n in ((6, 4), (4, 4), (4, 6)):
|
| 66 |
+
A_arr = rng.randn(m, n)
|
| 67 |
+
B_arr = rng.randn(m, n)
|
| 68 |
+
As = (A_arr, A_arr.tolist(), matrix(A_arr))
|
| 69 |
+
Bs = (B_arr, B_arr.tolist(), matrix(B_arr))
|
| 70 |
+
R_arr, s = orthogonal_procrustes(A_arr, B_arr)
|
| 71 |
+
AR_arr = A_arr.dot(R_arr)
|
| 72 |
+
for A, B in product(As, Bs):
|
| 73 |
+
R, s = orthogonal_procrustes(A, B)
|
| 74 |
+
AR = A_arr.dot(R)
|
| 75 |
+
assert_allclose(AR, AR_arr)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def test_orthogonal_procrustes():
|
| 79 |
+
rng = np.random.RandomState(1234)
|
| 80 |
+
for m, n in ((6, 4), (4, 4), (4, 6)):
|
| 81 |
+
# Sample a random target matrix.
|
| 82 |
+
B = rng.randn(m, n)
|
| 83 |
+
# Sample a random orthogonal matrix
|
| 84 |
+
# by computing eigh of a sampled symmetric matrix.
|
| 85 |
+
X = rng.randn(n, n)
|
| 86 |
+
w, V = eigh(X.T + X)
|
| 87 |
+
assert_allclose(inv(V), V.T)
|
| 88 |
+
# Compute a matrix with a known orthogonal transformation that gives B.
|
| 89 |
+
A = np.dot(B, V.T)
|
| 90 |
+
# Check that an orthogonal transformation from A to B can be recovered.
|
| 91 |
+
R, s = orthogonal_procrustes(A, B)
|
| 92 |
+
assert_allclose(inv(R), R.T)
|
| 93 |
+
assert_allclose(A.dot(R), B)
|
| 94 |
+
# Create a perturbed input matrix.
|
| 95 |
+
A_perturbed = A + 1e-2 * rng.randn(m, n)
|
| 96 |
+
# Check that the orthogonal procrustes function can find an orthogonal
|
| 97 |
+
# transformation that is better than the orthogonal transformation
|
| 98 |
+
# computed from the original input matrix.
|
| 99 |
+
R_prime, s = orthogonal_procrustes(A_perturbed, B)
|
| 100 |
+
assert_allclose(inv(R_prime), R_prime.T)
|
| 101 |
+
# Compute the naive and optimal transformations of the perturbed input.
|
| 102 |
+
naive_approx = A_perturbed.dot(R)
|
| 103 |
+
optim_approx = A_perturbed.dot(R_prime)
|
| 104 |
+
# Compute the Frobenius norm errors of the matrix approximations.
|
| 105 |
+
naive_approx_error = norm(naive_approx - B, ord='fro')
|
| 106 |
+
optim_approx_error = norm(optim_approx - B, ord='fro')
|
| 107 |
+
# Check that the orthogonal Procrustes approximation is better.
|
| 108 |
+
assert_array_less(optim_approx_error, naive_approx_error)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def _centered(A):
|
| 112 |
+
mu = A.mean(axis=0)
|
| 113 |
+
return A - mu, mu
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def test_orthogonal_procrustes_exact_example():
|
| 117 |
+
# Check a small application.
|
| 118 |
+
# It uses translation, scaling, reflection, and rotation.
|
| 119 |
+
#
|
| 120 |
+
# |
|
| 121 |
+
# a b |
|
| 122 |
+
# |
|
| 123 |
+
# d c | w
|
| 124 |
+
# |
|
| 125 |
+
# --------+--- x ----- z ---
|
| 126 |
+
# |
|
| 127 |
+
# | y
|
| 128 |
+
# |
|
| 129 |
+
#
|
| 130 |
+
A_orig = np.array([[-3, 3], [-2, 3], [-2, 2], [-3, 2]], dtype=float)
|
| 131 |
+
B_orig = np.array([[3, 2], [1, 0], [3, -2], [5, 0]], dtype=float)
|
| 132 |
+
A, A_mu = _centered(A_orig)
|
| 133 |
+
B, B_mu = _centered(B_orig)
|
| 134 |
+
R, s = orthogonal_procrustes(A, B)
|
| 135 |
+
scale = s / np.square(norm(A))
|
| 136 |
+
B_approx = scale * np.dot(A, R) + B_mu
|
| 137 |
+
assert_allclose(B_approx, B_orig, atol=1e-8)
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
def test_orthogonal_procrustes_stretched_example():
|
| 141 |
+
# Try again with a target with a stretched y axis.
|
| 142 |
+
A_orig = np.array([[-3, 3], [-2, 3], [-2, 2], [-3, 2]], dtype=float)
|
| 143 |
+
B_orig = np.array([[3, 40], [1, 0], [3, -40], [5, 0]], dtype=float)
|
| 144 |
+
A, A_mu = _centered(A_orig)
|
| 145 |
+
B, B_mu = _centered(B_orig)
|
| 146 |
+
R, s = orthogonal_procrustes(A, B)
|
| 147 |
+
scale = s / np.square(norm(A))
|
| 148 |
+
B_approx = scale * np.dot(A, R) + B_mu
|
| 149 |
+
expected = np.array([[3, 21], [-18, 0], [3, -21], [24, 0]], dtype=float)
|
| 150 |
+
assert_allclose(B_approx, expected, atol=1e-8)
|
| 151 |
+
# Check disparity symmetry.
|
| 152 |
+
expected_disparity = 0.4501246882793018
|
| 153 |
+
AB_disparity = np.square(norm(B_approx - B_orig) / norm(B))
|
| 154 |
+
assert_allclose(AB_disparity, expected_disparity)
|
| 155 |
+
R, s = orthogonal_procrustes(B, A)
|
| 156 |
+
scale = s / np.square(norm(B))
|
| 157 |
+
A_approx = scale * np.dot(B, R) + A_mu
|
| 158 |
+
BA_disparity = np.square(norm(A_approx - A_orig) / norm(A))
|
| 159 |
+
assert_allclose(BA_disparity, expected_disparity)
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
def test_orthogonal_procrustes_skbio_example():
|
| 163 |
+
# This transformation is also exact.
|
| 164 |
+
# It uses translation, scaling, and reflection.
|
| 165 |
+
#
|
| 166 |
+
# |
|
| 167 |
+
# | a
|
| 168 |
+
# | b
|
| 169 |
+
# | c d
|
| 170 |
+
# --+---------
|
| 171 |
+
# |
|
| 172 |
+
# | w
|
| 173 |
+
# |
|
| 174 |
+
# | x
|
| 175 |
+
# |
|
| 176 |
+
# | z y
|
| 177 |
+
# |
|
| 178 |
+
#
|
| 179 |
+
A_orig = np.array([[4, -2], [4, -4], [4, -6], [2, -6]], dtype=float)
|
| 180 |
+
B_orig = np.array([[1, 3], [1, 2], [1, 1], [2, 1]], dtype=float)
|
| 181 |
+
B_standardized = np.array([
|
| 182 |
+
[-0.13363062, 0.6681531],
|
| 183 |
+
[-0.13363062, 0.13363062],
|
| 184 |
+
[-0.13363062, -0.40089186],
|
| 185 |
+
[0.40089186, -0.40089186]])
|
| 186 |
+
A, A_mu = _centered(A_orig)
|
| 187 |
+
B, B_mu = _centered(B_orig)
|
| 188 |
+
R, s = orthogonal_procrustes(A, B)
|
| 189 |
+
scale = s / np.square(norm(A))
|
| 190 |
+
B_approx = scale * np.dot(A, R) + B_mu
|
| 191 |
+
assert_allclose(B_approx, B_orig)
|
| 192 |
+
assert_allclose(B / norm(B), B_standardized)
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
def test_empty():
|
| 196 |
+
a = np.empty((0, 0))
|
| 197 |
+
r, s = orthogonal_procrustes(a, a)
|
| 198 |
+
assert_allclose(r, np.empty((0, 0)))
|
| 199 |
+
|
| 200 |
+
a = np.empty((0, 3))
|
| 201 |
+
r, s = orthogonal_procrustes(a, a)
|
| 202 |
+
assert_allclose(r, np.identity(3))
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
@pytest.mark.parametrize('shape', [(4, 5), (5, 5), (5, 4)])
|
| 206 |
+
def test_unitary(shape):
|
| 207 |
+
# gh-12071 added support for unitary matrices; check that it
|
| 208 |
+
# works as intended.
|
| 209 |
+
m, n = shape
|
| 210 |
+
rng = np.random.default_rng(589234981235)
|
| 211 |
+
A = rng.random(shape) + rng.random(shape) * 1j
|
| 212 |
+
Q = rng.random((n, n)) + rng.random((n, n)) * 1j
|
| 213 |
+
Q, _ = np.linalg.qr(Q)
|
| 214 |
+
B = A @ Q
|
| 215 |
+
R, scale = orthogonal_procrustes(A, B)
|
| 216 |
+
assert_allclose(R @ R.conj().T, np.eye(n), atol=1e-14)
|
| 217 |
+
assert_allclose(A @ Q, B)
|
| 218 |
+
if shape != (4, 5): # solution is unique
|
| 219 |
+
assert_allclose(R, Q)
|
| 220 |
+
_, s, _ = svd(A.conj().T @ B)
|
| 221 |
+
assert_allclose(scale, np.sum(s))
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_sketches.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for _sketches.py."""
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
from numpy.testing import assert_, assert_equal
|
| 5 |
+
from scipy.linalg import clarkson_woodruff_transform
|
| 6 |
+
from scipy.linalg._sketches import cwt_matrix
|
| 7 |
+
from scipy.sparse import issparse, rand
|
| 8 |
+
from scipy.sparse.linalg import norm
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class TestClarksonWoodruffTransform:
|
| 12 |
+
"""
|
| 13 |
+
Testing the Clarkson Woodruff Transform
|
| 14 |
+
"""
|
| 15 |
+
# set seed for generating test matrices
|
| 16 |
+
rng = np.random.default_rng(1179103485)
|
| 17 |
+
|
| 18 |
+
# Test matrix parameters
|
| 19 |
+
n_rows = 2000
|
| 20 |
+
n_cols = 100
|
| 21 |
+
density = 0.1
|
| 22 |
+
|
| 23 |
+
# Sketch matrix dimensions
|
| 24 |
+
n_sketch_rows = 200
|
| 25 |
+
|
| 26 |
+
# Seeds to test with
|
| 27 |
+
seeds = [1755490010, 934377150, 1391612830, 1752708722, 2008891431,
|
| 28 |
+
1302443994, 1521083269, 1501189312, 1126232505, 1533465685]
|
| 29 |
+
|
| 30 |
+
A_dense = rng.random((n_rows, n_cols))
|
| 31 |
+
A_csc = rand(
|
| 32 |
+
n_rows, n_cols, density=density, format='csc', random_state=rng,
|
| 33 |
+
)
|
| 34 |
+
A_csr = rand(
|
| 35 |
+
n_rows, n_cols, density=density, format='csr', random_state=rng,
|
| 36 |
+
)
|
| 37 |
+
A_coo = rand(
|
| 38 |
+
n_rows, n_cols, density=density, format='coo', random_state=rng,
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Collect the test matrices
|
| 42 |
+
test_matrices = [
|
| 43 |
+
A_dense, A_csc, A_csr, A_coo,
|
| 44 |
+
]
|
| 45 |
+
|
| 46 |
+
# Test vector with norm ~1
|
| 47 |
+
x = rng.random((n_rows, 1)) / np.sqrt(n_rows)
|
| 48 |
+
|
| 49 |
+
def test_sketch_dimensions(self):
|
| 50 |
+
for A in self.test_matrices:
|
| 51 |
+
for seed in self.seeds:
|
| 52 |
+
# seed to ensure backwards compatibility post SPEC7
|
| 53 |
+
sketch = clarkson_woodruff_transform(
|
| 54 |
+
A, self.n_sketch_rows, seed=seed
|
| 55 |
+
)
|
| 56 |
+
assert_(sketch.shape == (self.n_sketch_rows, self.n_cols))
|
| 57 |
+
|
| 58 |
+
def test_seed_returns_identical_transform_matrix(self):
|
| 59 |
+
for seed in self.seeds:
|
| 60 |
+
S1 = cwt_matrix(
|
| 61 |
+
self.n_sketch_rows, self.n_rows, rng=seed
|
| 62 |
+
).toarray()
|
| 63 |
+
S2 = cwt_matrix(
|
| 64 |
+
self.n_sketch_rows, self.n_rows, rng=seed
|
| 65 |
+
).toarray()
|
| 66 |
+
assert_equal(S1, S2)
|
| 67 |
+
|
| 68 |
+
def test_seed_returns_identically(self):
|
| 69 |
+
for A in self.test_matrices:
|
| 70 |
+
for seed in self.seeds:
|
| 71 |
+
sketch1 = clarkson_woodruff_transform(
|
| 72 |
+
A, self.n_sketch_rows, rng=seed
|
| 73 |
+
)
|
| 74 |
+
sketch2 = clarkson_woodruff_transform(
|
| 75 |
+
A, self.n_sketch_rows, rng=seed
|
| 76 |
+
)
|
| 77 |
+
if issparse(sketch1):
|
| 78 |
+
sketch1 = sketch1.toarray()
|
| 79 |
+
if issparse(sketch2):
|
| 80 |
+
sketch2 = sketch2.toarray()
|
| 81 |
+
assert_equal(sketch1, sketch2)
|
| 82 |
+
|
| 83 |
+
def test_sketch_preserves_frobenius_norm(self):
|
| 84 |
+
# Given the probabilistic nature of the sketches
|
| 85 |
+
# we run the test multiple times and check that
|
| 86 |
+
# we pass all/almost all the tries.
|
| 87 |
+
n_errors = 0
|
| 88 |
+
for A in self.test_matrices:
|
| 89 |
+
if issparse(A):
|
| 90 |
+
true_norm = norm(A)
|
| 91 |
+
else:
|
| 92 |
+
true_norm = np.linalg.norm(A)
|
| 93 |
+
for seed in self.seeds:
|
| 94 |
+
sketch = clarkson_woodruff_transform(
|
| 95 |
+
A, self.n_sketch_rows, rng=seed,
|
| 96 |
+
)
|
| 97 |
+
if issparse(sketch):
|
| 98 |
+
sketch_norm = norm(sketch)
|
| 99 |
+
else:
|
| 100 |
+
sketch_norm = np.linalg.norm(sketch)
|
| 101 |
+
|
| 102 |
+
if np.abs(true_norm - sketch_norm) > 0.1 * true_norm:
|
| 103 |
+
n_errors += 1
|
| 104 |
+
assert_(n_errors == 0)
|
| 105 |
+
|
| 106 |
+
def test_sketch_preserves_vector_norm(self):
|
| 107 |
+
n_errors = 0
|
| 108 |
+
n_sketch_rows = int(np.ceil(2. / (0.01 * 0.5**2)))
|
| 109 |
+
true_norm = np.linalg.norm(self.x)
|
| 110 |
+
for seed in self.seeds:
|
| 111 |
+
sketch = clarkson_woodruff_transform(
|
| 112 |
+
self.x, n_sketch_rows, rng=seed,
|
| 113 |
+
)
|
| 114 |
+
sketch_norm = np.linalg.norm(sketch)
|
| 115 |
+
|
| 116 |
+
if np.abs(true_norm - sketch_norm) > 0.5 * true_norm:
|
| 117 |
+
n_errors += 1
|
| 118 |
+
assert_(n_errors == 0)
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_solve_toeplitz.py
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Test functions for linalg._solve_toeplitz module
|
| 2 |
+
"""
|
| 3 |
+
import numpy as np
|
| 4 |
+
from scipy.linalg._solve_toeplitz import levinson
|
| 5 |
+
from scipy.linalg import solve, toeplitz, solve_toeplitz, matmul_toeplitz
|
| 6 |
+
from numpy.testing import assert_equal, assert_allclose
|
| 7 |
+
|
| 8 |
+
import pytest
|
| 9 |
+
from pytest import raises as assert_raises
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def test_solve_equivalence():
|
| 13 |
+
# For toeplitz matrices, solve_toeplitz() should be equivalent to solve().
|
| 14 |
+
random = np.random.RandomState(1234)
|
| 15 |
+
for n in (1, 2, 3, 10):
|
| 16 |
+
c = random.randn(n)
|
| 17 |
+
if random.rand() < 0.5:
|
| 18 |
+
c = c + 1j * random.randn(n)
|
| 19 |
+
r = random.randn(n)
|
| 20 |
+
if random.rand() < 0.5:
|
| 21 |
+
r = r + 1j * random.randn(n)
|
| 22 |
+
y = random.randn(n)
|
| 23 |
+
if random.rand() < 0.5:
|
| 24 |
+
y = y + 1j * random.randn(n)
|
| 25 |
+
|
| 26 |
+
# Check equivalence when both the column and row are provided.
|
| 27 |
+
actual = solve_toeplitz((c,r), y)
|
| 28 |
+
desired = solve(toeplitz(c, r=r), y)
|
| 29 |
+
assert_allclose(actual, desired)
|
| 30 |
+
|
| 31 |
+
# Check equivalence when the column is provided but not the row.
|
| 32 |
+
actual = solve_toeplitz(c, b=y)
|
| 33 |
+
desired = solve(toeplitz(c), y)
|
| 34 |
+
assert_allclose(actual, desired)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def test_multiple_rhs():
|
| 38 |
+
random = np.random.RandomState(1234)
|
| 39 |
+
c = random.randn(4)
|
| 40 |
+
r = random.randn(4)
|
| 41 |
+
for offset in [0, 1j]:
|
| 42 |
+
for yshape in ((4,), (4, 3), (4, 3, 2)):
|
| 43 |
+
y = random.randn(*yshape) + offset
|
| 44 |
+
actual = solve_toeplitz((c,r), b=y)
|
| 45 |
+
desired = solve(toeplitz(c, r=r), y)
|
| 46 |
+
assert_equal(actual.shape, yshape)
|
| 47 |
+
assert_equal(desired.shape, yshape)
|
| 48 |
+
assert_allclose(actual, desired)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def test_native_list_arguments():
|
| 52 |
+
c = [1,2,4,7]
|
| 53 |
+
r = [1,3,9,12]
|
| 54 |
+
y = [5,1,4,2]
|
| 55 |
+
actual = solve_toeplitz((c,r), y)
|
| 56 |
+
desired = solve(toeplitz(c, r=r), y)
|
| 57 |
+
assert_allclose(actual, desired)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def test_zero_diag_error():
|
| 61 |
+
# The Levinson-Durbin implementation fails when the diagonal is zero.
|
| 62 |
+
random = np.random.RandomState(1234)
|
| 63 |
+
n = 4
|
| 64 |
+
c = random.randn(n)
|
| 65 |
+
r = random.randn(n)
|
| 66 |
+
y = random.randn(n)
|
| 67 |
+
c[0] = 0
|
| 68 |
+
assert_raises(np.linalg.LinAlgError,
|
| 69 |
+
solve_toeplitz, (c, r), b=y)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def test_wikipedia_counterexample():
|
| 73 |
+
# The Levinson-Durbin implementation also fails in other cases.
|
| 74 |
+
# This example is from the talk page of the wikipedia article.
|
| 75 |
+
random = np.random.RandomState(1234)
|
| 76 |
+
c = [2, 2, 1]
|
| 77 |
+
y = random.randn(3)
|
| 78 |
+
assert_raises(np.linalg.LinAlgError, solve_toeplitz, c, b=y)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def test_reflection_coeffs():
|
| 82 |
+
# check that the partial solutions are given by the reflection
|
| 83 |
+
# coefficients
|
| 84 |
+
|
| 85 |
+
random = np.random.RandomState(1234)
|
| 86 |
+
y_d = random.randn(10)
|
| 87 |
+
y_z = random.randn(10) + 1j
|
| 88 |
+
reflection_coeffs_d = [1]
|
| 89 |
+
reflection_coeffs_z = [1]
|
| 90 |
+
for i in range(2, 10):
|
| 91 |
+
reflection_coeffs_d.append(solve_toeplitz(y_d[:(i-1)], b=y_d[1:i])[-1])
|
| 92 |
+
reflection_coeffs_z.append(solve_toeplitz(y_z[:(i-1)], b=y_z[1:i])[-1])
|
| 93 |
+
|
| 94 |
+
y_d_concat = np.concatenate((y_d[-2:0:-1], y_d[:-1]))
|
| 95 |
+
y_z_concat = np.concatenate((y_z[-2:0:-1].conj(), y_z[:-1]))
|
| 96 |
+
_, ref_d = levinson(y_d_concat, b=y_d[1:])
|
| 97 |
+
_, ref_z = levinson(y_z_concat, b=y_z[1:])
|
| 98 |
+
|
| 99 |
+
assert_allclose(reflection_coeffs_d, ref_d[:-1])
|
| 100 |
+
assert_allclose(reflection_coeffs_z, ref_z[:-1])
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
@pytest.mark.xfail(reason='Instability of Levinson iteration')
|
| 104 |
+
def test_unstable():
|
| 105 |
+
# this is a "Gaussian Toeplitz matrix", as mentioned in Example 2 of
|
| 106 |
+
# I. Gohbert, T. Kailath and V. Olshevsky "Fast Gaussian Elimination with
|
| 107 |
+
# Partial Pivoting for Matrices with Displacement Structure"
|
| 108 |
+
# Mathematics of Computation, 64, 212 (1995), pp 1557-1576
|
| 109 |
+
# which can be unstable for levinson recursion.
|
| 110 |
+
|
| 111 |
+
# other fast toeplitz solvers such as GKO or Burg should be better.
|
| 112 |
+
random = np.random.RandomState(1234)
|
| 113 |
+
n = 100
|
| 114 |
+
c = 0.9 ** (np.arange(n)**2)
|
| 115 |
+
y = random.randn(n)
|
| 116 |
+
|
| 117 |
+
solution1 = solve_toeplitz(c, b=y)
|
| 118 |
+
solution2 = solve(toeplitz(c), y)
|
| 119 |
+
|
| 120 |
+
assert_allclose(solution1, solution2)
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
@pytest.mark.parametrize('dt_c', [int, float, np.float32, complex, np.complex64])
|
| 124 |
+
@pytest.mark.parametrize('dt_b', [int, float, np.float32, complex, np.complex64])
|
| 125 |
+
def test_empty(dt_c, dt_b):
|
| 126 |
+
c = np.array([], dtype=dt_c)
|
| 127 |
+
b = np.array([], dtype=dt_b)
|
| 128 |
+
x = solve_toeplitz(c, b)
|
| 129 |
+
assert x.shape == (0,)
|
| 130 |
+
assert x.dtype == solve_toeplitz(np.array([2, 1], dtype=dt_c),
|
| 131 |
+
np.ones(2, dtype=dt_b)).dtype
|
| 132 |
+
|
| 133 |
+
b = np.empty((0, 0), dtype=dt_b)
|
| 134 |
+
x1 = solve_toeplitz(c, b)
|
| 135 |
+
assert x1.shape == (0, 0)
|
| 136 |
+
assert x1.dtype == x.dtype
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
@pytest.mark.parametrize('fun', [solve_toeplitz, matmul_toeplitz])
|
| 140 |
+
def test_nd_FutureWarning(fun):
|
| 141 |
+
# Test future warnings with n-D `c`/`r`
|
| 142 |
+
rng = np.random.default_rng(283592436523456)
|
| 143 |
+
c = rng.random((2, 3, 4))
|
| 144 |
+
r = rng.random((2, 3, 4))
|
| 145 |
+
b_or_x = rng.random(24)
|
| 146 |
+
message = "Beginning in SciPy 1.17, multidimensional input will be..."
|
| 147 |
+
with pytest.warns(FutureWarning, match=message):
|
| 148 |
+
fun(c, b_or_x)
|
| 149 |
+
with pytest.warns(FutureWarning, match=message):
|
| 150 |
+
fun((c, r), b_or_x)
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_solvers.py
ADDED
|
@@ -0,0 +1,844 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
from numpy.testing import assert_array_almost_equal, assert_allclose
|
| 5 |
+
import pytest
|
| 6 |
+
from pytest import raises as assert_raises
|
| 7 |
+
|
| 8 |
+
from scipy.linalg import solve_sylvester
|
| 9 |
+
from scipy.linalg import solve_continuous_lyapunov, solve_discrete_lyapunov
|
| 10 |
+
from scipy.linalg import solve_continuous_are, solve_discrete_are
|
| 11 |
+
from scipy.linalg import block_diag, solve, LinAlgError
|
| 12 |
+
from scipy.sparse._sputils import matrix
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# dtypes for testing size-0 case following precedent set in gh-20295
|
| 16 |
+
dtypes = [int, float, np.float32, complex, np.complex64]
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def _load_data(name):
|
| 20 |
+
"""
|
| 21 |
+
Load npz data file under data/
|
| 22 |
+
Returns a copy of the data, rather than keeping the npz file open.
|
| 23 |
+
"""
|
| 24 |
+
filename = os.path.join(os.path.abspath(os.path.dirname(__file__)),
|
| 25 |
+
'data', name)
|
| 26 |
+
with np.load(filename) as f:
|
| 27 |
+
return dict(f.items())
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
class TestSolveLyapunov:
|
| 31 |
+
|
| 32 |
+
cases = [
|
| 33 |
+
# empty case
|
| 34 |
+
(np.empty((0, 0)),
|
| 35 |
+
np.empty((0, 0))),
|
| 36 |
+
(np.array([[1, 2], [3, 4]]),
|
| 37 |
+
np.array([[9, 10], [11, 12]])),
|
| 38 |
+
# a, q all complex.
|
| 39 |
+
(np.array([[1.0+1j, 2.0], [3.0-4.0j, 5.0]]),
|
| 40 |
+
np.array([[2.0-2j, 2.0+2j], [-1.0-1j, 2.0]])),
|
| 41 |
+
# a real; q complex.
|
| 42 |
+
(np.array([[1.0, 2.0], [3.0, 5.0]]),
|
| 43 |
+
np.array([[2.0-2j, 2.0+2j], [-1.0-1j, 2.0]])),
|
| 44 |
+
# a complex; q real.
|
| 45 |
+
(np.array([[1.0+1j, 2.0], [3.0-4.0j, 5.0]]),
|
| 46 |
+
np.array([[2.0, 2.0], [-1.0, 2.0]])),
|
| 47 |
+
# An example from Kitagawa, 1977
|
| 48 |
+
(np.array([[3, 9, 5, 1, 4], [1, 2, 3, 8, 4], [4, 6, 6, 6, 3],
|
| 49 |
+
[1, 5, 2, 0, 7], [5, 3, 3, 1, 5]]),
|
| 50 |
+
np.array([[2, 4, 1, 0, 1], [4, 1, 0, 2, 0], [1, 0, 3, 0, 3],
|
| 51 |
+
[0, 2, 0, 1, 0], [1, 0, 3, 0, 4]])),
|
| 52 |
+
# Companion matrix example. a complex; q real; a.shape[0] = 11
|
| 53 |
+
(np.array([[0.100+0.j, 0.091+0.j, 0.082+0.j, 0.073+0.j, 0.064+0.j,
|
| 54 |
+
0.055+0.j, 0.046+0.j, 0.037+0.j, 0.028+0.j, 0.019+0.j,
|
| 55 |
+
0.010+0.j],
|
| 56 |
+
[1.000+0.j, 0.000+0.j, 0.000+0.j, 0.000+0.j, 0.000+0.j,
|
| 57 |
+
0.000+0.j, 0.000+0.j, 0.000+0.j, 0.000+0.j, 0.000+0.j,
|
| 58 |
+
0.000+0.j],
|
| 59 |
+
[0.000+0.j, 1.000+0.j, 0.000+0.j, 0.000+0.j, 0.000+0.j,
|
| 60 |
+
0.000+0.j, 0.000+0.j, 0.000+0.j, 0.000+0.j, 0.000+0.j,
|
| 61 |
+
0.000+0.j],
|
| 62 |
+
[0.000+0.j, 0.000+0.j, 1.000+0.j, 0.000+0.j, 0.000+0.j,
|
| 63 |
+
0.000+0.j, 0.000+0.j, 0.000+0.j, 0.000+0.j, 0.000+0.j,
|
| 64 |
+
0.000+0.j],
|
| 65 |
+
[0.000+0.j, 0.000+0.j, 0.000+0.j, 1.000+0.j, 0.000+0.j,
|
| 66 |
+
0.000+0.j, 0.000+0.j, 0.000+0.j, 0.000+0.j, 0.000+0.j,
|
| 67 |
+
0.000+0.j],
|
| 68 |
+
[0.000+0.j, 0.000+0.j, 0.000+0.j, 0.000+0.j, 1.000+0.j,
|
| 69 |
+
0.000+0.j, 0.000+0.j, 0.000+0.j, 0.000+0.j, 0.000+0.j,
|
| 70 |
+
0.000+0.j],
|
| 71 |
+
[0.000+0.j, 0.000+0.j, 0.000+0.j, 0.000+0.j, 0.000+0.j,
|
| 72 |
+
1.000+0.j, 0.000+0.j, 0.000+0.j, 0.000+0.j, 0.000+0.j,
|
| 73 |
+
0.000+0.j],
|
| 74 |
+
[0.000+0.j, 0.000+0.j, 0.000+0.j, 0.000+0.j, 0.000+0.j,
|
| 75 |
+
0.000+0.j, 1.000+0.j, 0.000+0.j, 0.000+0.j, 0.000+0.j,
|
| 76 |
+
0.000+0.j],
|
| 77 |
+
[0.000+0.j, 0.000+0.j, 0.000+0.j, 0.000+0.j, 0.000+0.j,
|
| 78 |
+
0.000+0.j, 0.000+0.j, 1.000+0.j, 0.000+0.j, 0.000+0.j,
|
| 79 |
+
0.000+0.j],
|
| 80 |
+
[0.000+0.j, 0.000+0.j, 0.000+0.j, 0.000+0.j, 0.000+0.j,
|
| 81 |
+
0.000+0.j, 0.000+0.j, 0.000+0.j, 1.000+0.j, 0.000+0.j,
|
| 82 |
+
0.000+0.j],
|
| 83 |
+
[0.000+0.j, 0.000+0.j, 0.000+0.j, 0.000+0.j, 0.000+0.j,
|
| 84 |
+
0.000+0.j, 0.000+0.j, 0.000+0.j, 0.000+0.j, 1.000+0.j,
|
| 85 |
+
0.000+0.j]]),
|
| 86 |
+
np.eye(11)),
|
| 87 |
+
# https://github.com/scipy/scipy/issues/4176
|
| 88 |
+
(matrix([[0, 1], [-1/2, -1]]),
|
| 89 |
+
(matrix([0, 3]).T @ matrix([0, 3]).T.T)),
|
| 90 |
+
# https://github.com/scipy/scipy/issues/4176
|
| 91 |
+
(matrix([[0, 1], [-1/2, -1]]),
|
| 92 |
+
(np.array(matrix([0, 3]).T @ matrix([0, 3]).T.T))),
|
| 93 |
+
]
|
| 94 |
+
|
| 95 |
+
def test_continuous_squareness_and_shape(self):
|
| 96 |
+
nsq = np.ones((3, 2))
|
| 97 |
+
sq = np.eye(3)
|
| 98 |
+
assert_raises(ValueError, solve_continuous_lyapunov, nsq, sq)
|
| 99 |
+
assert_raises(ValueError, solve_continuous_lyapunov, sq, nsq)
|
| 100 |
+
assert_raises(ValueError, solve_continuous_lyapunov, sq, np.eye(2))
|
| 101 |
+
|
| 102 |
+
def check_continuous_case(self, a, q):
|
| 103 |
+
x = solve_continuous_lyapunov(a, q)
|
| 104 |
+
assert_array_almost_equal(
|
| 105 |
+
np.dot(a, x) + np.dot(x, a.conj().transpose()), q)
|
| 106 |
+
|
| 107 |
+
def check_discrete_case(self, a, q, method=None):
|
| 108 |
+
x = solve_discrete_lyapunov(a, q, method=method)
|
| 109 |
+
assert_array_almost_equal(
|
| 110 |
+
np.dot(np.dot(a, x), a.conj().transpose()) - x, -1.0*q)
|
| 111 |
+
|
| 112 |
+
def test_cases(self):
|
| 113 |
+
for case in self.cases:
|
| 114 |
+
self.check_continuous_case(case[0], case[1])
|
| 115 |
+
self.check_discrete_case(case[0], case[1])
|
| 116 |
+
self.check_discrete_case(case[0], case[1], method='direct')
|
| 117 |
+
self.check_discrete_case(case[0], case[1], method='bilinear')
|
| 118 |
+
|
| 119 |
+
@pytest.mark.parametrize("dtype_a", dtypes)
|
| 120 |
+
@pytest.mark.parametrize("dtype_q", dtypes)
|
| 121 |
+
def test_size_0(self, dtype_a, dtype_q):
|
| 122 |
+
rng = np.random.default_rng(234598235)
|
| 123 |
+
|
| 124 |
+
a = np.zeros((0, 0), dtype=dtype_a)
|
| 125 |
+
q = np.zeros((0, 0), dtype=dtype_q)
|
| 126 |
+
res = solve_continuous_lyapunov(a, q)
|
| 127 |
+
|
| 128 |
+
a = (rng.random((5, 5))*100).astype(dtype_a)
|
| 129 |
+
q = (rng.random((5, 5))*100).astype(dtype_q)
|
| 130 |
+
ref = solve_continuous_lyapunov(a, q)
|
| 131 |
+
|
| 132 |
+
assert res.shape == (0, 0)
|
| 133 |
+
assert res.dtype == ref.dtype
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
class TestSolveContinuousAre:
|
| 137 |
+
mat6 = _load_data('carex_6_data.npz')
|
| 138 |
+
mat15 = _load_data('carex_15_data.npz')
|
| 139 |
+
mat18 = _load_data('carex_18_data.npz')
|
| 140 |
+
mat19 = _load_data('carex_19_data.npz')
|
| 141 |
+
mat20 = _load_data('carex_20_data.npz')
|
| 142 |
+
cases = [
|
| 143 |
+
# Carex examples taken from (with default parameters):
|
| 144 |
+
# [1] P.BENNER, A.J. LAUB, V. MEHRMANN: 'A Collection of Benchmark
|
| 145 |
+
# Examples for the Numerical Solution of Algebraic Riccati
|
| 146 |
+
# Equations II: Continuous-Time Case', Tech. Report SPC 95_23,
|
| 147 |
+
# Fak. f. Mathematik, TU Chemnitz-Zwickau (Germany), 1995.
|
| 148 |
+
#
|
| 149 |
+
# The format of the data is (a, b, q, r, knownfailure), where
|
| 150 |
+
# knownfailure is None if the test passes or a string
|
| 151 |
+
# indicating the reason for failure.
|
| 152 |
+
#
|
| 153 |
+
# Test Case 0: carex #1
|
| 154 |
+
(np.diag([1.], 1),
|
| 155 |
+
np.array([[0], [1]]),
|
| 156 |
+
block_diag(1., 2.),
|
| 157 |
+
1,
|
| 158 |
+
None),
|
| 159 |
+
# Test Case 1: carex #2
|
| 160 |
+
(np.array([[4, 3], [-4.5, -3.5]]),
|
| 161 |
+
np.array([[1], [-1]]),
|
| 162 |
+
np.array([[9, 6], [6, 4.]]),
|
| 163 |
+
1,
|
| 164 |
+
None),
|
| 165 |
+
# Test Case 2: carex #3
|
| 166 |
+
(np.array([[0, 1, 0, 0],
|
| 167 |
+
[0, -1.89, 0.39, -5.53],
|
| 168 |
+
[0, -0.034, -2.98, 2.43],
|
| 169 |
+
[0.034, -0.0011, -0.99, -0.21]]),
|
| 170 |
+
np.array([[0, 0], [0.36, -1.6], [-0.95, -0.032], [0.03, 0]]),
|
| 171 |
+
np.array([[2.313, 2.727, 0.688, 0.023],
|
| 172 |
+
[2.727, 4.271, 1.148, 0.323],
|
| 173 |
+
[0.688, 1.148, 0.313, 0.102],
|
| 174 |
+
[0.023, 0.323, 0.102, 0.083]]),
|
| 175 |
+
np.eye(2),
|
| 176 |
+
None),
|
| 177 |
+
# Test Case 3: carex #4
|
| 178 |
+
(np.array([[-0.991, 0.529, 0, 0, 0, 0, 0, 0],
|
| 179 |
+
[0.522, -1.051, 0.596, 0, 0, 0, 0, 0],
|
| 180 |
+
[0, 0.522, -1.118, 0.596, 0, 0, 0, 0],
|
| 181 |
+
[0, 0, 0.522, -1.548, 0.718, 0, 0, 0],
|
| 182 |
+
[0, 0, 0, 0.922, -1.64, 0.799, 0, 0],
|
| 183 |
+
[0, 0, 0, 0, 0.922, -1.721, 0.901, 0],
|
| 184 |
+
[0, 0, 0, 0, 0, 0.922, -1.823, 1.021],
|
| 185 |
+
[0, 0, 0, 0, 0, 0, 0.922, -1.943]]),
|
| 186 |
+
np.array([[3.84, 4.00, 37.60, 3.08, 2.36, 2.88, 3.08, 3.00],
|
| 187 |
+
[-2.88, -3.04, -2.80, -2.32, -3.32, -3.82, -4.12, -3.96]]
|
| 188 |
+
).T * 0.001,
|
| 189 |
+
np.array([[1.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.1],
|
| 190 |
+
[0.0, 1.0, 0.0, 0.0, 0.1, 0.0, 0.0, 0.0],
|
| 191 |
+
[0.0, 0.0, 1.0, 0.0, 0.0, 0.5, 0.0, 0.0],
|
| 192 |
+
[0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0],
|
| 193 |
+
[0.5, 0.1, 0.0, 0.0, 0.1, 0.0, 0.0, 0.0],
|
| 194 |
+
[0.0, 0.0, 0.5, 0.0, 0.0, 0.1, 0.0, 0.0],
|
| 195 |
+
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.1, 0.0],
|
| 196 |
+
[0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.1]]),
|
| 197 |
+
np.eye(2),
|
| 198 |
+
None),
|
| 199 |
+
# Test Case 4: carex #5
|
| 200 |
+
(np.array(
|
| 201 |
+
[[-4.019, 5.120, 0., 0., -2.082, 0., 0., 0., 0.870],
|
| 202 |
+
[-0.346, 0.986, 0., 0., -2.340, 0., 0., 0., 0.970],
|
| 203 |
+
[-7.909, 15.407, -4.069, 0., -6.450, 0., 0., 0., 2.680],
|
| 204 |
+
[-21.816, 35.606, -0.339, -3.870, -17.800, 0., 0., 0., 7.390],
|
| 205 |
+
[-60.196, 98.188, -7.907, 0.340, -53.008, 0., 0., 0., 20.400],
|
| 206 |
+
[0, 0, 0, 0, 94.000, -147.200, 0., 53.200, 0.],
|
| 207 |
+
[0, 0, 0, 0, 0, 94.000, -147.200, 0, 0],
|
| 208 |
+
[0, 0, 0, 0, 0, 12.800, 0.000, -31.600, 0],
|
| 209 |
+
[0, 0, 0, 0, 12.800, 0.000, 0.000, 18.800, -31.600]]),
|
| 210 |
+
np.array([[0.010, -0.011, -0.151],
|
| 211 |
+
[0.003, -0.021, 0.000],
|
| 212 |
+
[0.009, -0.059, 0.000],
|
| 213 |
+
[0.024, -0.162, 0.000],
|
| 214 |
+
[0.068, -0.445, 0.000],
|
| 215 |
+
[0.000, 0.000, 0.000],
|
| 216 |
+
[0.000, 0.000, 0.000],
|
| 217 |
+
[0.000, 0.000, 0.000],
|
| 218 |
+
[0.000, 0.000, 0.000]]),
|
| 219 |
+
np.eye(9),
|
| 220 |
+
np.eye(3),
|
| 221 |
+
None),
|
| 222 |
+
# Test Case 5: carex #6
|
| 223 |
+
(mat6['A'], mat6['B'], mat6['Q'], mat6['R'], None),
|
| 224 |
+
# Test Case 6: carex #7
|
| 225 |
+
(np.array([[1, 0], [0, -2.]]),
|
| 226 |
+
np.array([[1e-6], [0]]),
|
| 227 |
+
np.ones((2, 2)),
|
| 228 |
+
1.,
|
| 229 |
+
'Bad residual accuracy'),
|
| 230 |
+
# Test Case 7: carex #8
|
| 231 |
+
(block_diag(-0.1, -0.02),
|
| 232 |
+
np.array([[0.100, 0.000], [0.001, 0.010]]),
|
| 233 |
+
np.array([[100, 1000], [1000, 10000]]),
|
| 234 |
+
np.ones((2, 2)) + block_diag(1e-6, 0),
|
| 235 |
+
None),
|
| 236 |
+
# Test Case 8: carex #9
|
| 237 |
+
(np.array([[0, 1e6], [0, 0]]),
|
| 238 |
+
np.array([[0], [1.]]),
|
| 239 |
+
np.eye(2),
|
| 240 |
+
1.,
|
| 241 |
+
None),
|
| 242 |
+
# Test Case 9: carex #10
|
| 243 |
+
(np.array([[1.0000001, 1], [1., 1.0000001]]),
|
| 244 |
+
np.eye(2),
|
| 245 |
+
np.eye(2),
|
| 246 |
+
np.eye(2),
|
| 247 |
+
None),
|
| 248 |
+
# Test Case 10: carex #11
|
| 249 |
+
(np.array([[3, 1.], [4, 2]]),
|
| 250 |
+
np.array([[1], [1]]),
|
| 251 |
+
np.array([[-11, -5], [-5, -2.]]),
|
| 252 |
+
1.,
|
| 253 |
+
None),
|
| 254 |
+
# Test Case 11: carex #12
|
| 255 |
+
(np.array([[7000000., 2000000., -0.],
|
| 256 |
+
[2000000., 6000000., -2000000.],
|
| 257 |
+
[0., -2000000., 5000000.]]) / 3,
|
| 258 |
+
np.eye(3),
|
| 259 |
+
np.array([[1., -2., -2.], [-2., 1., -2.], [-2., -2., 1.]]).dot(
|
| 260 |
+
np.diag([1e-6, 1, 1e6])).dot(
|
| 261 |
+
np.array([[1., -2., -2.], [-2., 1., -2.], [-2., -2., 1.]])) / 9,
|
| 262 |
+
np.eye(3) * 1e6,
|
| 263 |
+
'Bad Residual Accuracy'),
|
| 264 |
+
# Test Case 12: carex #13
|
| 265 |
+
(np.array([[0, 0.4, 0, 0],
|
| 266 |
+
[0, 0, 0.345, 0],
|
| 267 |
+
[0, -0.524e6, -0.465e6, 0.262e6],
|
| 268 |
+
[0, 0, 0, -1e6]]),
|
| 269 |
+
np.array([[0, 0, 0, 1e6]]).T,
|
| 270 |
+
np.diag([1, 0, 1, 0]),
|
| 271 |
+
1.,
|
| 272 |
+
None),
|
| 273 |
+
# Test Case 13: carex #14
|
| 274 |
+
(np.array([[-1e-6, 1, 0, 0],
|
| 275 |
+
[-1, -1e-6, 0, 0],
|
| 276 |
+
[0, 0, 1e-6, 1],
|
| 277 |
+
[0, 0, -1, 1e-6]]),
|
| 278 |
+
np.ones((4, 1)),
|
| 279 |
+
np.ones((4, 4)),
|
| 280 |
+
1.,
|
| 281 |
+
None),
|
| 282 |
+
# Test Case 14: carex #15
|
| 283 |
+
(mat15['A'], mat15['B'], mat15['Q'], mat15['R'], None),
|
| 284 |
+
# Test Case 15: carex #16
|
| 285 |
+
(np.eye(64, 64, k=-1) + np.eye(64, 64)*(-2.) + np.rot90(
|
| 286 |
+
block_diag(1, np.zeros((62, 62)), 1)) + np.eye(64, 64, k=1),
|
| 287 |
+
np.eye(64),
|
| 288 |
+
np.eye(64),
|
| 289 |
+
np.eye(64),
|
| 290 |
+
None),
|
| 291 |
+
# Test Case 16: carex #17
|
| 292 |
+
(np.diag(np.ones((20, )), 1),
|
| 293 |
+
np.flipud(np.eye(21, 1)),
|
| 294 |
+
np.eye(21, 1) * np.eye(21, 1).T,
|
| 295 |
+
1,
|
| 296 |
+
'Bad Residual Accuracy'),
|
| 297 |
+
# Test Case 17: carex #18
|
| 298 |
+
(mat18['A'], mat18['B'], mat18['Q'], mat18['R'], None),
|
| 299 |
+
# Test Case 18: carex #19
|
| 300 |
+
(mat19['A'], mat19['B'], mat19['Q'], mat19['R'],
|
| 301 |
+
'Bad Residual Accuracy'),
|
| 302 |
+
# Test Case 19: carex #20
|
| 303 |
+
(mat20['A'], mat20['B'], mat20['Q'], mat20['R'],
|
| 304 |
+
'Bad Residual Accuracy')
|
| 305 |
+
]
|
| 306 |
+
# Makes the minimum precision requirements customized to the test.
|
| 307 |
+
# Here numbers represent the number of decimals that agrees with zero
|
| 308 |
+
# matrix when the solution x is plugged in to the equation.
|
| 309 |
+
#
|
| 310 |
+
# res = array([[8e-3,1e-16],[1e-16,1e-20]]) --> min_decimal[k] = 2
|
| 311 |
+
#
|
| 312 |
+
# If the test is failing use "None" for that entry.
|
| 313 |
+
#
|
| 314 |
+
min_decimal = (14, 12, 13, 14, 11, 6, None, 5, 7, 14, 14,
|
| 315 |
+
None, 9, 14, 13, 14, None, 12, None, None)
|
| 316 |
+
|
| 317 |
+
@pytest.mark.parametrize("j, case", enumerate(cases))
|
| 318 |
+
def test_solve_continuous_are(self, j, case):
|
| 319 |
+
"""Checks if 0 = XA + A'X - XB(R)^{-1} B'X + Q is true"""
|
| 320 |
+
a, b, q, r, knownfailure = case
|
| 321 |
+
if knownfailure:
|
| 322 |
+
pytest.xfail(reason=knownfailure)
|
| 323 |
+
|
| 324 |
+
dec = self.min_decimal[j]
|
| 325 |
+
x = solve_continuous_are(a, b, q, r)
|
| 326 |
+
res = x @ a + a.conj().T @ x + q
|
| 327 |
+
out_fact = x @ b
|
| 328 |
+
res -= out_fact @ solve(np.atleast_2d(r), out_fact.conj().T)
|
| 329 |
+
assert_array_almost_equal(res, np.zeros_like(res), decimal=dec)
|
| 330 |
+
|
| 331 |
+
|
| 332 |
+
class TestSolveDiscreteAre:
|
| 333 |
+
cases = [
|
| 334 |
+
# Darex examples taken from (with default parameters):
|
| 335 |
+
# [1] P.BENNER, A.J. LAUB, V. MEHRMANN: 'A Collection of Benchmark
|
| 336 |
+
# Examples for the Numerical Solution of Algebraic Riccati
|
| 337 |
+
# Equations II: Discrete-Time Case', Tech. Report SPC 95_23,
|
| 338 |
+
# Fak. f. Mathematik, TU Chemnitz-Zwickau (Germany), 1995.
|
| 339 |
+
# [2] T. GUDMUNDSSON, C. KENNEY, A.J. LAUB: 'Scaling of the
|
| 340 |
+
# Discrete-Time Algebraic Riccati Equation to Enhance Stability
|
| 341 |
+
# of the Schur Solution Method', IEEE Trans.Aut.Cont., vol.37(4)
|
| 342 |
+
#
|
| 343 |
+
# The format of the data is (a, b, q, r, knownfailure), where
|
| 344 |
+
# knownfailure is None if the test passes or a string
|
| 345 |
+
# indicating the reason for failure.
|
| 346 |
+
#
|
| 347 |
+
# TEST CASE 0 : Complex a; real b, q, r
|
| 348 |
+
(np.array([[2, 1-2j], [0, -3j]]),
|
| 349 |
+
np.array([[0], [1]]),
|
| 350 |
+
np.array([[1, 0], [0, 2]]),
|
| 351 |
+
np.array([[1]]),
|
| 352 |
+
None),
|
| 353 |
+
# TEST CASE 1 :Real a, q, r; complex b
|
| 354 |
+
(np.array([[2, 1], [0, -1]]),
|
| 355 |
+
np.array([[-2j], [1j]]),
|
| 356 |
+
np.array([[1, 0], [0, 2]]),
|
| 357 |
+
np.array([[1]]),
|
| 358 |
+
None),
|
| 359 |
+
# TEST CASE 2 : Real a, b; complex q, r
|
| 360 |
+
(np.array([[3, 1], [0, -1]]),
|
| 361 |
+
np.array([[1, 2], [1, 3]]),
|
| 362 |
+
np.array([[1, 1+1j], [1-1j, 2]]),
|
| 363 |
+
np.array([[2, -2j], [2j, 3]]),
|
| 364 |
+
None),
|
| 365 |
+
# TEST CASE 3 : User-reported gh-2251 (Trac #1732)
|
| 366 |
+
(np.array([[0.63399379, 0.54906824, 0.76253406],
|
| 367 |
+
[0.5404729, 0.53745766, 0.08731853],
|
| 368 |
+
[0.27524045, 0.84922129, 0.4681622]]),
|
| 369 |
+
np.array([[0.96861695], [0.05532739], [0.78934047]]),
|
| 370 |
+
np.eye(3),
|
| 371 |
+
np.eye(1),
|
| 372 |
+
None),
|
| 373 |
+
# TEST CASE 4 : darex #1
|
| 374 |
+
(np.array([[4, 3], [-4.5, -3.5]]),
|
| 375 |
+
np.array([[1], [-1]]),
|
| 376 |
+
np.array([[9, 6], [6, 4]]),
|
| 377 |
+
np.array([[1]]),
|
| 378 |
+
None),
|
| 379 |
+
# TEST CASE 5 : darex #2
|
| 380 |
+
(np.array([[0.9512, 0], [0, 0.9048]]),
|
| 381 |
+
np.array([[4.877, 4.877], [-1.1895, 3.569]]),
|
| 382 |
+
np.array([[0.005, 0], [0, 0.02]]),
|
| 383 |
+
np.array([[1/3, 0], [0, 3]]),
|
| 384 |
+
None),
|
| 385 |
+
# TEST CASE 6 : darex #3
|
| 386 |
+
(np.array([[2, -1], [1, 0]]),
|
| 387 |
+
np.array([[1], [0]]),
|
| 388 |
+
np.array([[0, 0], [0, 1]]),
|
| 389 |
+
np.array([[0]]),
|
| 390 |
+
None),
|
| 391 |
+
# TEST CASE 7 : darex #4 (skipped the gen. Ric. term S)
|
| 392 |
+
(np.array([[0, 1], [0, -1]]),
|
| 393 |
+
np.array([[1, 0], [2, 1]]),
|
| 394 |
+
np.array([[-4, -4], [-4, 7]]) * (1/11),
|
| 395 |
+
np.array([[9, 3], [3, 1]]),
|
| 396 |
+
None),
|
| 397 |
+
# TEST CASE 8 : darex #5
|
| 398 |
+
(np.array([[0, 1], [0, 0]]),
|
| 399 |
+
np.array([[0], [1]]),
|
| 400 |
+
np.array([[1, 2], [2, 4]]),
|
| 401 |
+
np.array([[1]]),
|
| 402 |
+
None),
|
| 403 |
+
# TEST CASE 9 : darex #6
|
| 404 |
+
(np.array([[0.998, 0.067, 0, 0],
|
| 405 |
+
[-.067, 0.998, 0, 0],
|
| 406 |
+
[0, 0, 0.998, 0.153],
|
| 407 |
+
[0, 0, -.153, 0.998]]),
|
| 408 |
+
np.array([[0.0033, 0.0200],
|
| 409 |
+
[0.1000, -.0007],
|
| 410 |
+
[0.0400, 0.0073],
|
| 411 |
+
[-.0028, 0.1000]]),
|
| 412 |
+
np.array([[1.87, 0, 0, -0.244],
|
| 413 |
+
[0, 0.744, 0.205, 0],
|
| 414 |
+
[0, 0.205, 0.589, 0],
|
| 415 |
+
[-0.244, 0, 0, 1.048]]),
|
| 416 |
+
np.eye(2),
|
| 417 |
+
None),
|
| 418 |
+
# TEST CASE 10 : darex #7
|
| 419 |
+
(np.array([[0.984750, -.079903, 0.0009054, -.0010765],
|
| 420 |
+
[0.041588, 0.998990, -.0358550, 0.0126840],
|
| 421 |
+
[-.546620, 0.044916, -.3299100, 0.1931800],
|
| 422 |
+
[2.662400, -.100450, -.9245500, -.2632500]]),
|
| 423 |
+
np.array([[0.0037112, 0.0007361],
|
| 424 |
+
[-.0870510, 9.3411e-6],
|
| 425 |
+
[-1.198440, -4.1378e-4],
|
| 426 |
+
[-3.192700, 9.2535e-4]]),
|
| 427 |
+
np.eye(4)*1e-2,
|
| 428 |
+
np.eye(2),
|
| 429 |
+
None),
|
| 430 |
+
# TEST CASE 11 : darex #8
|
| 431 |
+
(np.array([[-0.6000000, -2.2000000, -3.6000000, -5.4000180],
|
| 432 |
+
[1.0000000, 0.6000000, 0.8000000, 3.3999820],
|
| 433 |
+
[0.0000000, 1.0000000, 1.8000000, 3.7999820],
|
| 434 |
+
[0.0000000, 0.0000000, 0.0000000, -0.9999820]]),
|
| 435 |
+
np.array([[1.0, -1.0, -1.0, -1.0],
|
| 436 |
+
[0.0, 1.0, -1.0, -1.0],
|
| 437 |
+
[0.0, 0.0, 1.0, -1.0],
|
| 438 |
+
[0.0, 0.0, 0.0, 1.0]]),
|
| 439 |
+
np.array([[2, 1, 3, 6],
|
| 440 |
+
[1, 2, 2, 5],
|
| 441 |
+
[3, 2, 6, 11],
|
| 442 |
+
[6, 5, 11, 22]]),
|
| 443 |
+
np.eye(4),
|
| 444 |
+
None),
|
| 445 |
+
# TEST CASE 12 : darex #9
|
| 446 |
+
(np.array([[95.4070, 1.9643, 0.3597, 0.0673, 0.0190],
|
| 447 |
+
[40.8490, 41.3170, 16.0840, 4.4679, 1.1971],
|
| 448 |
+
[12.2170, 26.3260, 36.1490, 15.9300, 12.3830],
|
| 449 |
+
[4.1118, 12.8580, 27.2090, 21.4420, 40.9760],
|
| 450 |
+
[0.1305, 0.5808, 1.8750, 3.6162, 94.2800]]) * 0.01,
|
| 451 |
+
np.array([[0.0434, -0.0122],
|
| 452 |
+
[2.6606, -1.0453],
|
| 453 |
+
[3.7530, -5.5100],
|
| 454 |
+
[3.6076, -6.6000],
|
| 455 |
+
[0.4617, -0.9148]]) * 0.01,
|
| 456 |
+
np.eye(5),
|
| 457 |
+
np.eye(2),
|
| 458 |
+
None),
|
| 459 |
+
# TEST CASE 13 : darex #10
|
| 460 |
+
(np.kron(np.eye(2), np.diag([1, 1], k=1)),
|
| 461 |
+
np.kron(np.eye(2), np.array([[0], [0], [1]])),
|
| 462 |
+
np.array([[1, 1, 0, 0, 0, 0],
|
| 463 |
+
[1, 1, 0, 0, 0, 0],
|
| 464 |
+
[0, 0, 0, 0, 0, 0],
|
| 465 |
+
[0, 0, 0, 1, -1, 0],
|
| 466 |
+
[0, 0, 0, -1, 1, 0],
|
| 467 |
+
[0, 0, 0, 0, 0, 0]]),
|
| 468 |
+
np.array([[3, 0], [0, 1]]),
|
| 469 |
+
None),
|
| 470 |
+
# TEST CASE 14 : darex #11
|
| 471 |
+
(0.001 * np.array(
|
| 472 |
+
[[870.1, 135.0, 11.59, .5014, -37.22, .3484, 0, 4.242, 7.249],
|
| 473 |
+
[76.55, 897.4, 12.72, 0.5504, -40.16, .3743, 0, 4.53, 7.499],
|
| 474 |
+
[-127.2, 357.5, 817, 1.455, -102.8, .987, 0, 11.85, 18.72],
|
| 475 |
+
[-363.5, 633.9, 74.91, 796.6, -273.5, 2.653, 0, 31.72, 48.82],
|
| 476 |
+
[-960, 1645.9, -128.9, -5.597, 71.42, 7.108, 0, 84.52, 125.9],
|
| 477 |
+
[-664.4, 112.96, -88.89, -3.854, 84.47, 13.6, 0, 144.3, 101.6],
|
| 478 |
+
[-410.2, 693, -54.71, -2.371, 66.49, 12.49, .1063, 99.97, 69.67],
|
| 479 |
+
[-179.9, 301.7, -23.93, -1.035, 60.59, 22.16, 0, 213.9, 35.54],
|
| 480 |
+
[-345.1, 580.4, -45.96, -1.989, 105.6, 19.86, 0, 219.1, 215.2]]),
|
| 481 |
+
np.array([[4.7600, -0.5701, -83.6800],
|
| 482 |
+
[0.8790, -4.7730, -2.7300],
|
| 483 |
+
[1.4820, -13.1200, 8.8760],
|
| 484 |
+
[3.8920, -35.1300, 24.8000],
|
| 485 |
+
[10.3400, -92.7500, 66.8000],
|
| 486 |
+
[7.2030, -61.5900, 38.3400],
|
| 487 |
+
[4.4540, -36.8300, 20.2900],
|
| 488 |
+
[1.9710, -15.5400, 6.9370],
|
| 489 |
+
[3.7730, -30.2800, 14.6900]]) * 0.001,
|
| 490 |
+
np.diag([50, 0, 0, 0, 50, 0, 0, 0, 0]),
|
| 491 |
+
np.eye(3),
|
| 492 |
+
None),
|
| 493 |
+
# TEST CASE 15 : darex #12 - numerically least accurate example
|
| 494 |
+
(np.array([[0, 1e6], [0, 0]]),
|
| 495 |
+
np.array([[0], [1]]),
|
| 496 |
+
np.eye(2),
|
| 497 |
+
np.array([[1]]),
|
| 498 |
+
None),
|
| 499 |
+
# TEST CASE 16 : darex #13
|
| 500 |
+
(np.array([[16, 10, -2],
|
| 501 |
+
[10, 13, -8],
|
| 502 |
+
[-2, -8, 7]]) * (1/9),
|
| 503 |
+
np.eye(3),
|
| 504 |
+
1e6 * np.eye(3),
|
| 505 |
+
1e6 * np.eye(3),
|
| 506 |
+
None),
|
| 507 |
+
# TEST CASE 17 : darex #14
|
| 508 |
+
(np.array([[1 - 1/1e8, 0, 0, 0],
|
| 509 |
+
[1, 0, 0, 0],
|
| 510 |
+
[0, 1, 0, 0],
|
| 511 |
+
[0, 0, 1, 0]]),
|
| 512 |
+
np.array([[1e-08], [0], [0], [0]]),
|
| 513 |
+
np.diag([0, 0, 0, 1]),
|
| 514 |
+
np.array([[0.25]]),
|
| 515 |
+
None),
|
| 516 |
+
# TEST CASE 18 : darex #15
|
| 517 |
+
(np.eye(100, k=1),
|
| 518 |
+
np.flipud(np.eye(100, 1)),
|
| 519 |
+
np.eye(100),
|
| 520 |
+
np.array([[1]]),
|
| 521 |
+
None)
|
| 522 |
+
]
|
| 523 |
+
|
| 524 |
+
# Makes the minimum precision requirements customized to the test.
|
| 525 |
+
# Here numbers represent the number of decimals that agrees with zero
|
| 526 |
+
# matrix when the solution x is plugged in to the equation.
|
| 527 |
+
#
|
| 528 |
+
# res = array([[8e-3,1e-16],[1e-16,1e-20]]) --> min_decimal[k] = 2
|
| 529 |
+
#
|
| 530 |
+
# If the test is failing use "None" for that entry.
|
| 531 |
+
#
|
| 532 |
+
min_decimal = (12, 14, 13, 14, 13, 16, 18, 14, 14, 13,
|
| 533 |
+
14, 13, 13, 14, 12, 2, 4, 6, 10)
|
| 534 |
+
max_tol = [1.5 * 10**-ind for ind in min_decimal]
|
| 535 |
+
# relaxed tolerance in gh-18012 after bump to OpenBLAS
|
| 536 |
+
max_tol[11] = 2.5e-13
|
| 537 |
+
|
| 538 |
+
# relaxed tolerance in gh-20335 for linux-aarch64 build on Cirrus
|
| 539 |
+
# with OpenBLAS from ubuntu jammy
|
| 540 |
+
max_tol[15] = 2.0e-2
|
| 541 |
+
|
| 542 |
+
# relaxed tolerance in gh-20335 for OpenBLAS 3.20 on ubuntu jammy
|
| 543 |
+
# bump not needed for OpenBLAS 3.26
|
| 544 |
+
max_tol[16] = 2.0e-4
|
| 545 |
+
|
| 546 |
+
@pytest.mark.parametrize("j, case", enumerate(cases))
|
| 547 |
+
def test_solve_discrete_are(self, j, case):
|
| 548 |
+
"""Checks if X = A'XA-(A'XB)(R+B'XB)^-1(B'XA)+Q) is true"""
|
| 549 |
+
a, b, q, r, knownfailure = case
|
| 550 |
+
if knownfailure:
|
| 551 |
+
pytest.xfail(reason=knownfailure)
|
| 552 |
+
|
| 553 |
+
atol = self.max_tol[j]
|
| 554 |
+
|
| 555 |
+
x = solve_discrete_are(a, b, q, r)
|
| 556 |
+
bH = b.conj().T
|
| 557 |
+
xa, xb = x @ a, x @ b
|
| 558 |
+
|
| 559 |
+
res = a.conj().T @ xa - x + q
|
| 560 |
+
res -= a.conj().T @ xb @ (solve(r + bH @ xb, bH) @ xa)
|
| 561 |
+
|
| 562 |
+
# changed from
|
| 563 |
+
# assert_array_almost_equal(res, np.zeros_like(res), decimal=dec)
|
| 564 |
+
# in gh-18012 as it's easier to relax a tolerance and allclose is
|
| 565 |
+
# preferred
|
| 566 |
+
assert_allclose(res, np.zeros_like(res), atol=atol)
|
| 567 |
+
|
| 568 |
+
def test_infeasible(self):
|
| 569 |
+
# An infeasible example taken from https://arxiv.org/abs/1505.04861v1
|
| 570 |
+
A = np.triu(np.ones((3, 3)))
|
| 571 |
+
A[0, 1] = -1
|
| 572 |
+
B = np.array([[1, 1, 0], [0, 0, 1]]).T
|
| 573 |
+
Q = np.full_like(A, -2) + np.diag([8, -1, -1.9])
|
| 574 |
+
R = np.diag([-10, 0.1])
|
| 575 |
+
assert_raises(LinAlgError, solve_continuous_are, A, B, Q, R)
|
| 576 |
+
|
| 577 |
+
|
| 578 |
+
def test_solve_generalized_continuous_are():
|
| 579 |
+
cases = [
|
| 580 |
+
# Two random examples differ by s term
|
| 581 |
+
# in the absence of any literature for demanding examples.
|
| 582 |
+
(np.array([[2.769230e-01, 8.234578e-01, 9.502220e-01],
|
| 583 |
+
[4.617139e-02, 6.948286e-01, 3.444608e-02],
|
| 584 |
+
[9.713178e-02, 3.170995e-01, 4.387444e-01]]),
|
| 585 |
+
np.array([[3.815585e-01, 1.868726e-01],
|
| 586 |
+
[7.655168e-01, 4.897644e-01],
|
| 587 |
+
[7.951999e-01, 4.455862e-01]]),
|
| 588 |
+
np.eye(3),
|
| 589 |
+
np.eye(2),
|
| 590 |
+
np.array([[6.463130e-01, 2.760251e-01, 1.626117e-01],
|
| 591 |
+
[7.093648e-01, 6.797027e-01, 1.189977e-01],
|
| 592 |
+
[7.546867e-01, 6.550980e-01, 4.983641e-01]]),
|
| 593 |
+
np.zeros((3, 2)),
|
| 594 |
+
None),
|
| 595 |
+
(np.array([[2.769230e-01, 8.234578e-01, 9.502220e-01],
|
| 596 |
+
[4.617139e-02, 6.948286e-01, 3.444608e-02],
|
| 597 |
+
[9.713178e-02, 3.170995e-01, 4.387444e-01]]),
|
| 598 |
+
np.array([[3.815585e-01, 1.868726e-01],
|
| 599 |
+
[7.655168e-01, 4.897644e-01],
|
| 600 |
+
[7.951999e-01, 4.455862e-01]]),
|
| 601 |
+
np.eye(3),
|
| 602 |
+
np.eye(2),
|
| 603 |
+
np.array([[6.463130e-01, 2.760251e-01, 1.626117e-01],
|
| 604 |
+
[7.093648e-01, 6.797027e-01, 1.189977e-01],
|
| 605 |
+
[7.546867e-01, 6.550980e-01, 4.983641e-01]]),
|
| 606 |
+
np.ones((3, 2)),
|
| 607 |
+
None)
|
| 608 |
+
]
|
| 609 |
+
|
| 610 |
+
min_decimal = (10, 10)
|
| 611 |
+
|
| 612 |
+
def _test_factory(case, dec):
|
| 613 |
+
"""Checks if X = A'XA-(A'XB)(R+B'XB)^-1(B'XA)+Q) is true"""
|
| 614 |
+
a, b, q, r, e, s, knownfailure = case
|
| 615 |
+
if knownfailure:
|
| 616 |
+
pytest.xfail(reason=knownfailure)
|
| 617 |
+
|
| 618 |
+
x = solve_continuous_are(a, b, q, r, e, s)
|
| 619 |
+
res = a.conj().T.dot(x.dot(e)) + e.conj().T.dot(x.dot(a)) + q
|
| 620 |
+
out_fact = e.conj().T.dot(x).dot(b) + s
|
| 621 |
+
res -= out_fact.dot(solve(np.atleast_2d(r), out_fact.conj().T))
|
| 622 |
+
assert_array_almost_equal(res, np.zeros_like(res), decimal=dec)
|
| 623 |
+
|
| 624 |
+
for ind, case in enumerate(cases):
|
| 625 |
+
_test_factory(case, min_decimal[ind])
|
| 626 |
+
|
| 627 |
+
|
| 628 |
+
def test_solve_generalized_discrete_are():
|
| 629 |
+
mat20170120 = _load_data('gendare_20170120_data.npz')
|
| 630 |
+
|
| 631 |
+
cases = [
|
| 632 |
+
# Two random examples differ by s term
|
| 633 |
+
# in the absence of any literature for demanding examples.
|
| 634 |
+
(np.array([[2.769230e-01, 8.234578e-01, 9.502220e-01],
|
| 635 |
+
[4.617139e-02, 6.948286e-01, 3.444608e-02],
|
| 636 |
+
[9.713178e-02, 3.170995e-01, 4.387444e-01]]),
|
| 637 |
+
np.array([[3.815585e-01, 1.868726e-01],
|
| 638 |
+
[7.655168e-01, 4.897644e-01],
|
| 639 |
+
[7.951999e-01, 4.455862e-01]]),
|
| 640 |
+
np.eye(3),
|
| 641 |
+
np.eye(2),
|
| 642 |
+
np.array([[6.463130e-01, 2.760251e-01, 1.626117e-01],
|
| 643 |
+
[7.093648e-01, 6.797027e-01, 1.189977e-01],
|
| 644 |
+
[7.546867e-01, 6.550980e-01, 4.983641e-01]]),
|
| 645 |
+
np.zeros((3, 2)),
|
| 646 |
+
None),
|
| 647 |
+
(np.array([[2.769230e-01, 8.234578e-01, 9.502220e-01],
|
| 648 |
+
[4.617139e-02, 6.948286e-01, 3.444608e-02],
|
| 649 |
+
[9.713178e-02, 3.170995e-01, 4.387444e-01]]),
|
| 650 |
+
np.array([[3.815585e-01, 1.868726e-01],
|
| 651 |
+
[7.655168e-01, 4.897644e-01],
|
| 652 |
+
[7.951999e-01, 4.455862e-01]]),
|
| 653 |
+
np.eye(3),
|
| 654 |
+
np.eye(2),
|
| 655 |
+
np.array([[6.463130e-01, 2.760251e-01, 1.626117e-01],
|
| 656 |
+
[7.093648e-01, 6.797027e-01, 1.189977e-01],
|
| 657 |
+
[7.546867e-01, 6.550980e-01, 4.983641e-01]]),
|
| 658 |
+
np.ones((3, 2)),
|
| 659 |
+
None),
|
| 660 |
+
# user-reported (under PR-6616) 20-Jan-2017
|
| 661 |
+
# tests against the case where E is None but S is provided
|
| 662 |
+
(mat20170120['A'],
|
| 663 |
+
mat20170120['B'],
|
| 664 |
+
mat20170120['Q'],
|
| 665 |
+
mat20170120['R'],
|
| 666 |
+
None,
|
| 667 |
+
mat20170120['S'],
|
| 668 |
+
None),
|
| 669 |
+
]
|
| 670 |
+
|
| 671 |
+
max_atol = (1.5e-11, 1.5e-11, 3.5e-16)
|
| 672 |
+
|
| 673 |
+
def _test_factory(case, atol):
|
| 674 |
+
"""Checks if X = A'XA-(A'XB)(R+B'XB)^-1(B'XA)+Q) is true"""
|
| 675 |
+
a, b, q, r, e, s, knownfailure = case
|
| 676 |
+
if knownfailure:
|
| 677 |
+
pytest.xfail(reason=knownfailure)
|
| 678 |
+
|
| 679 |
+
x = solve_discrete_are(a, b, q, r, e, s)
|
| 680 |
+
if e is None:
|
| 681 |
+
e = np.eye(a.shape[0])
|
| 682 |
+
if s is None:
|
| 683 |
+
s = np.zeros_like(b)
|
| 684 |
+
res = a.conj().T.dot(x.dot(a)) - e.conj().T.dot(x.dot(e)) + q
|
| 685 |
+
res -= (a.conj().T.dot(x.dot(b)) + s).dot(
|
| 686 |
+
solve(r+b.conj().T.dot(x.dot(b)),
|
| 687 |
+
(b.conj().T.dot(x.dot(a)) + s.conj().T)
|
| 688 |
+
)
|
| 689 |
+
)
|
| 690 |
+
# changed from:
|
| 691 |
+
# assert_array_almost_equal(res, np.zeros_like(res), decimal=dec)
|
| 692 |
+
# in gh-17950 because of a Linux 32 bit fail.
|
| 693 |
+
assert_allclose(res, np.zeros_like(res), atol=atol)
|
| 694 |
+
|
| 695 |
+
for ind, case in enumerate(cases):
|
| 696 |
+
_test_factory(case, max_atol[ind])
|
| 697 |
+
|
| 698 |
+
|
| 699 |
+
def test_are_validate_args():
|
| 700 |
+
|
| 701 |
+
def test_square_shape():
|
| 702 |
+
nsq = np.ones((3, 2))
|
| 703 |
+
sq = np.eye(3)
|
| 704 |
+
for x in (solve_continuous_are, solve_discrete_are):
|
| 705 |
+
assert_raises(ValueError, x, nsq, 1, 1, 1)
|
| 706 |
+
assert_raises(ValueError, x, sq, sq, nsq, 1)
|
| 707 |
+
assert_raises(ValueError, x, sq, sq, sq, nsq)
|
| 708 |
+
assert_raises(ValueError, x, sq, sq, sq, sq, nsq)
|
| 709 |
+
|
| 710 |
+
def test_compatible_sizes():
|
| 711 |
+
nsq = np.ones((3, 2))
|
| 712 |
+
sq = np.eye(4)
|
| 713 |
+
for x in (solve_continuous_are, solve_discrete_are):
|
| 714 |
+
assert_raises(ValueError, x, sq, nsq, 1, 1)
|
| 715 |
+
assert_raises(ValueError, x, sq, sq, sq, sq, sq, nsq)
|
| 716 |
+
assert_raises(ValueError, x, sq, sq, np.eye(3), sq)
|
| 717 |
+
assert_raises(ValueError, x, sq, sq, sq, np.eye(3))
|
| 718 |
+
assert_raises(ValueError, x, sq, sq, sq, sq, np.eye(3))
|
| 719 |
+
|
| 720 |
+
def test_symmetry():
|
| 721 |
+
nsym = np.arange(9).reshape(3, 3)
|
| 722 |
+
sym = np.eye(3)
|
| 723 |
+
for x in (solve_continuous_are, solve_discrete_are):
|
| 724 |
+
assert_raises(ValueError, x, sym, sym, nsym, sym)
|
| 725 |
+
assert_raises(ValueError, x, sym, sym, sym, nsym)
|
| 726 |
+
|
| 727 |
+
def test_singularity():
|
| 728 |
+
sing = np.full((3, 3), 1e12)
|
| 729 |
+
sing[2, 2] -= 1
|
| 730 |
+
sq = np.eye(3)
|
| 731 |
+
for x in (solve_continuous_are, solve_discrete_are):
|
| 732 |
+
assert_raises(ValueError, x, sq, sq, sq, sq, sing)
|
| 733 |
+
|
| 734 |
+
assert_raises(ValueError, solve_continuous_are, sq, sq, sq, sing)
|
| 735 |
+
|
| 736 |
+
def test_finiteness():
|
| 737 |
+
nm = np.full((2, 2), np.nan)
|
| 738 |
+
sq = np.eye(2)
|
| 739 |
+
for x in (solve_continuous_are, solve_discrete_are):
|
| 740 |
+
assert_raises(ValueError, x, nm, sq, sq, sq)
|
| 741 |
+
assert_raises(ValueError, x, sq, nm, sq, sq)
|
| 742 |
+
assert_raises(ValueError, x, sq, sq, nm, sq)
|
| 743 |
+
assert_raises(ValueError, x, sq, sq, sq, nm)
|
| 744 |
+
assert_raises(ValueError, x, sq, sq, sq, sq, nm)
|
| 745 |
+
assert_raises(ValueError, x, sq, sq, sq, sq, sq, nm)
|
| 746 |
+
|
| 747 |
+
|
| 748 |
+
class TestSolveSylvester:
|
| 749 |
+
cases = [
|
| 750 |
+
# empty cases
|
| 751 |
+
(np.empty((0, 0)),
|
| 752 |
+
np.empty((0, 0)),
|
| 753 |
+
np.empty((0, 0))),
|
| 754 |
+
(np.empty((0, 0)),
|
| 755 |
+
np.empty((2, 2)),
|
| 756 |
+
np.empty((0, 2))),
|
| 757 |
+
(np.empty((2, 2)),
|
| 758 |
+
np.empty((0, 0)),
|
| 759 |
+
np.empty((2, 0))),
|
| 760 |
+
# a, b, c all real.
|
| 761 |
+
(np.array([[1, 2], [0, 4]]),
|
| 762 |
+
np.array([[5, 6], [0, 8]]),
|
| 763 |
+
np.array([[9, 10], [11, 12]])),
|
| 764 |
+
# a, b, c all real, 4x4. a and b have non-trivial 2x2 blocks in their
|
| 765 |
+
# quasi-triangular form.
|
| 766 |
+
(np.array([[1.0, 0, 0, 0],
|
| 767 |
+
[0, 1.0, 2.0, 0.0],
|
| 768 |
+
[0, 0, 3.0, -4],
|
| 769 |
+
[0, 0, 2, 5]]),
|
| 770 |
+
np.array([[2.0, 0, 0, 1.0],
|
| 771 |
+
[0, 1.0, 0.0, 0.0],
|
| 772 |
+
[0, 0, 1.0, -1],
|
| 773 |
+
[0, 0, 1, 1]]),
|
| 774 |
+
np.array([[1.0, 0, 0, 0],
|
| 775 |
+
[0, 1.0, 0, 0],
|
| 776 |
+
[0, 0, 1.0, 0],
|
| 777 |
+
[0, 0, 0, 1.0]])),
|
| 778 |
+
# a, b, c all complex.
|
| 779 |
+
(np.array([[1.0+1j, 2.0], [3.0-4.0j, 5.0]]),
|
| 780 |
+
np.array([[-1.0, 2j], [3.0, 4.0]]),
|
| 781 |
+
np.array([[2.0-2j, 2.0+2j], [-1.0-1j, 2.0]])),
|
| 782 |
+
# a and b real; c complex.
|
| 783 |
+
(np.array([[1.0, 2.0], [3.0, 5.0]]),
|
| 784 |
+
np.array([[-1.0, 0], [3.0, 4.0]]),
|
| 785 |
+
np.array([[2.0-2j, 2.0+2j], [-1.0-1j, 2.0]])),
|
| 786 |
+
# a and c complex; b real.
|
| 787 |
+
(np.array([[1.0+1j, 2.0], [3.0-4.0j, 5.0]]),
|
| 788 |
+
np.array([[-1.0, 0], [3.0, 4.0]]),
|
| 789 |
+
np.array([[2.0-2j, 2.0+2j], [-1.0-1j, 2.0]])),
|
| 790 |
+
# a complex; b and c real.
|
| 791 |
+
(np.array([[1.0+1j, 2.0], [3.0-4.0j, 5.0]]),
|
| 792 |
+
np.array([[-1.0, 0], [3.0, 4.0]]),
|
| 793 |
+
np.array([[2.0, 2.0], [-1.0, 2.0]])),
|
| 794 |
+
# not square matrices, real
|
| 795 |
+
(np.array([[8, 1, 6], [3, 5, 7], [4, 9, 2]]),
|
| 796 |
+
np.array([[2, 3], [4, 5]]),
|
| 797 |
+
np.array([[1, 2], [3, 4], [5, 6]])),
|
| 798 |
+
# not square matrices, complex
|
| 799 |
+
(np.array([[8, 1j, 6+2j], [3, 5, 7], [4, 9, 2]]),
|
| 800 |
+
np.array([[2, 3], [4, 5-1j]]),
|
| 801 |
+
np.array([[1, 2j], [3, 4j], [5j, 6+7j]])),
|
| 802 |
+
]
|
| 803 |
+
|
| 804 |
+
def check_case(self, a, b, c):
|
| 805 |
+
x = solve_sylvester(a, b, c)
|
| 806 |
+
assert_array_almost_equal(np.dot(a, x) + np.dot(x, b), c)
|
| 807 |
+
|
| 808 |
+
def test_cases(self):
|
| 809 |
+
for case in self.cases:
|
| 810 |
+
self.check_case(case[0], case[1], case[2])
|
| 811 |
+
|
| 812 |
+
def test_trivial(self):
|
| 813 |
+
a = np.array([[1.0, 0.0], [0.0, 1.0]])
|
| 814 |
+
b = np.array([[1.0]])
|
| 815 |
+
c = np.array([2.0, 2.0]).reshape(-1, 1)
|
| 816 |
+
x = solve_sylvester(a, b, c)
|
| 817 |
+
assert_array_almost_equal(x, np.array([1.0, 1.0]).reshape(-1, 1))
|
| 818 |
+
|
| 819 |
+
# Feel free to adjust this to test fewer dtypes or random selections rather than
|
| 820 |
+
# the Cartesian product. It doesn't take very long to test all combinations,
|
| 821 |
+
# though, so we'll start there and trim it down as we see fit.
|
| 822 |
+
@pytest.mark.parametrize("dtype_a", dtypes)
|
| 823 |
+
@pytest.mark.parametrize("dtype_b", dtypes)
|
| 824 |
+
@pytest.mark.parametrize("dtype_q", dtypes)
|
| 825 |
+
@pytest.mark.parametrize("m", [0, 3])
|
| 826 |
+
@pytest.mark.parametrize("n", [0, 3])
|
| 827 |
+
def test_size_0(self, m, n, dtype_a, dtype_b, dtype_q):
|
| 828 |
+
if m == n != 0:
|
| 829 |
+
pytest.skip('m = n != 0 is not a case that needs to be tested here.')
|
| 830 |
+
|
| 831 |
+
rng = np.random.default_rng(598435298262546)
|
| 832 |
+
|
| 833 |
+
a = np.zeros((m, m), dtype=dtype_a)
|
| 834 |
+
b = np.zeros((n, n), dtype=dtype_b)
|
| 835 |
+
q = np.zeros((m, n), dtype=dtype_q)
|
| 836 |
+
res = solve_sylvester(a, b, q)
|
| 837 |
+
|
| 838 |
+
a = (rng.random((5, 5))*100).astype(dtype_a)
|
| 839 |
+
b = (rng.random((6, 6))*100).astype(dtype_b)
|
| 840 |
+
q = (rng.random((5, 6))*100).astype(dtype_q)
|
| 841 |
+
ref = solve_sylvester(a, b, q)
|
| 842 |
+
|
| 843 |
+
assert res.shape == (m, n)
|
| 844 |
+
assert res.dtype == ref.dtype
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/linalg/tests/test_special_matrices.py
ADDED
|
@@ -0,0 +1,640 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
import numpy as np
|
| 3 |
+
from numpy import arange, array, eye, copy, sqrt
|
| 4 |
+
from numpy.testing import (assert_equal, assert_array_equal,
|
| 5 |
+
assert_array_almost_equal, assert_allclose)
|
| 6 |
+
from pytest import raises as assert_raises
|
| 7 |
+
|
| 8 |
+
from scipy.fft import fft
|
| 9 |
+
from scipy.special import comb
|
| 10 |
+
from scipy.linalg import (toeplitz, hankel, circulant, hadamard, leslie, dft,
|
| 11 |
+
companion, kron, block_diag,
|
| 12 |
+
helmert, hilbert, invhilbert, pascal, invpascal,
|
| 13 |
+
fiedler, fiedler_companion, eigvals,
|
| 14 |
+
convolution_matrix)
|
| 15 |
+
from numpy.linalg import cond
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class TestToeplitz:
|
| 19 |
+
|
| 20 |
+
def test_basic(self):
|
| 21 |
+
y = toeplitz([1, 2, 3])
|
| 22 |
+
assert_array_equal(y, [[1, 2, 3], [2, 1, 2], [3, 2, 1]])
|
| 23 |
+
y = toeplitz([1, 2, 3], [1, 4, 5])
|
| 24 |
+
assert_array_equal(y, [[1, 4, 5], [2, 1, 4], [3, 2, 1]])
|
| 25 |
+
|
| 26 |
+
def test_complex_01(self):
|
| 27 |
+
data = (1.0 + arange(3.0)) * (1.0 + 1.0j)
|
| 28 |
+
x = copy(data)
|
| 29 |
+
t = toeplitz(x)
|
| 30 |
+
# Calling toeplitz should not change x.
|
| 31 |
+
assert_array_equal(x, data)
|
| 32 |
+
# According to the docstring, x should be the first column of t.
|
| 33 |
+
col0 = t[:, 0]
|
| 34 |
+
assert_array_equal(col0, data)
|
| 35 |
+
assert_array_equal(t[0, 1:], data[1:].conj())
|
| 36 |
+
|
| 37 |
+
def test_scalar_00(self):
|
| 38 |
+
"""Scalar arguments still produce a 2D array."""
|
| 39 |
+
t = toeplitz(10)
|
| 40 |
+
assert_array_equal(t, [[10]])
|
| 41 |
+
t = toeplitz(10, 20)
|
| 42 |
+
assert_array_equal(t, [[10]])
|
| 43 |
+
|
| 44 |
+
def test_scalar_01(self):
|
| 45 |
+
c = array([1, 2, 3])
|
| 46 |
+
t = toeplitz(c, 1)
|
| 47 |
+
assert_array_equal(t, [[1], [2], [3]])
|
| 48 |
+
|
| 49 |
+
def test_scalar_02(self):
|
| 50 |
+
c = array([1, 2, 3])
|
| 51 |
+
t = toeplitz(c, array(1))
|
| 52 |
+
assert_array_equal(t, [[1], [2], [3]])
|
| 53 |
+
|
| 54 |
+
def test_scalar_03(self):
|
| 55 |
+
c = array([1, 2, 3])
|
| 56 |
+
t = toeplitz(c, array([1]))
|
| 57 |
+
assert_array_equal(t, [[1], [2], [3]])
|
| 58 |
+
|
| 59 |
+
def test_scalar_04(self):
|
| 60 |
+
r = array([10, 2, 3])
|
| 61 |
+
t = toeplitz(1, r)
|
| 62 |
+
assert_array_equal(t, [[1, 2, 3]])
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
class TestHankel:
|
| 66 |
+
def test_basic(self):
|
| 67 |
+
y = hankel([1, 2, 3])
|
| 68 |
+
assert_array_equal(y, [[1, 2, 3], [2, 3, 0], [3, 0, 0]])
|
| 69 |
+
y = hankel([1, 2, 3], [3, 4, 5])
|
| 70 |
+
assert_array_equal(y, [[1, 2, 3], [2, 3, 4], [3, 4, 5]])
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
class TestCirculant:
|
| 74 |
+
def test_basic(self):
|
| 75 |
+
y = circulant([1, 2, 3])
|
| 76 |
+
assert_array_equal(y, [[1, 3, 2], [2, 1, 3], [3, 2, 1]])
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
class TestHadamard:
|
| 80 |
+
|
| 81 |
+
def test_basic(self):
|
| 82 |
+
|
| 83 |
+
y = hadamard(1)
|
| 84 |
+
assert_array_equal(y, [[1]])
|
| 85 |
+
|
| 86 |
+
y = hadamard(2, dtype=float)
|
| 87 |
+
assert_array_equal(y, [[1.0, 1.0], [1.0, -1.0]])
|
| 88 |
+
|
| 89 |
+
y = hadamard(4)
|
| 90 |
+
assert_array_equal(y, [[1, 1, 1, 1],
|
| 91 |
+
[1, -1, 1, -1],
|
| 92 |
+
[1, 1, -1, -1],
|
| 93 |
+
[1, -1, -1, 1]])
|
| 94 |
+
|
| 95 |
+
assert_raises(ValueError, hadamard, 0)
|
| 96 |
+
assert_raises(ValueError, hadamard, 5)
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
class TestLeslie:
|
| 100 |
+
|
| 101 |
+
def test_bad_shapes(self):
|
| 102 |
+
assert_raises(ValueError, leslie, [[1, 1], [2, 2]], [3, 4, 5])
|
| 103 |
+
assert_raises(ValueError, leslie, [1, 2], [1, 2])
|
| 104 |
+
assert_raises(ValueError, leslie, [1], [])
|
| 105 |
+
|
| 106 |
+
def test_basic(self):
|
| 107 |
+
a = leslie([1, 2, 3], [0.25, 0.5])
|
| 108 |
+
expected = array([[1.0, 2.0, 3.0],
|
| 109 |
+
[0.25, 0.0, 0.0],
|
| 110 |
+
[0.0, 0.5, 0.0]])
|
| 111 |
+
assert_array_equal(a, expected)
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
class TestCompanion:
|
| 115 |
+
|
| 116 |
+
def test_bad_shapes(self):
|
| 117 |
+
assert_raises(ValueError, companion, [0, 4, 5])
|
| 118 |
+
assert_raises(ValueError, companion, [1])
|
| 119 |
+
assert_raises(ValueError, companion, [])
|
| 120 |
+
|
| 121 |
+
def test_basic(self):
|
| 122 |
+
c = companion([1, 2, 3])
|
| 123 |
+
expected = array([
|
| 124 |
+
[-2.0, -3.0],
|
| 125 |
+
[1.0, 0.0]])
|
| 126 |
+
assert_array_equal(c, expected)
|
| 127 |
+
|
| 128 |
+
c = companion([2.0, 5.0, -10.0])
|
| 129 |
+
expected = array([
|
| 130 |
+
[-2.5, 5.0],
|
| 131 |
+
[1.0, 0.0]])
|
| 132 |
+
assert_array_equal(c, expected)
|
| 133 |
+
|
| 134 |
+
c = companion([(1.0, 2.0, 3.0),
|
| 135 |
+
(4.0, 5.0, 6.0)])
|
| 136 |
+
expected = array([
|
| 137 |
+
([-2.00, -3.00],
|
| 138 |
+
[+1.00, +0.00]),
|
| 139 |
+
([-1.25, -1.50],
|
| 140 |
+
[+1.00, +0.00])
|
| 141 |
+
])
|
| 142 |
+
assert_array_equal(c, expected)
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
class TestBlockDiag:
|
| 146 |
+
def test_basic(self):
|
| 147 |
+
x = block_diag(eye(2), [[1, 2], [3, 4], [5, 6]], [[1, 2, 3]])
|
| 148 |
+
assert_array_equal(x, [[1, 0, 0, 0, 0, 0, 0],
|
| 149 |
+
[0, 1, 0, 0, 0, 0, 0],
|
| 150 |
+
[0, 0, 1, 2, 0, 0, 0],
|
| 151 |
+
[0, 0, 3, 4, 0, 0, 0],
|
| 152 |
+
[0, 0, 5, 6, 0, 0, 0],
|
| 153 |
+
[0, 0, 0, 0, 1, 2, 3]])
|
| 154 |
+
|
| 155 |
+
def test_dtype(self):
|
| 156 |
+
x = block_diag([[1.5]])
|
| 157 |
+
assert_equal(x.dtype, float)
|
| 158 |
+
|
| 159 |
+
x = block_diag([[True]])
|
| 160 |
+
assert_equal(x.dtype, bool)
|
| 161 |
+
|
| 162 |
+
def test_mixed_dtypes(self):
|
| 163 |
+
actual = block_diag([[1]], [[1j]])
|
| 164 |
+
desired = np.array([[1, 0], [0, 1j]])
|
| 165 |
+
assert_array_equal(actual, desired)
|
| 166 |
+
|
| 167 |
+
def test_scalar_and_1d_args(self):
|
| 168 |
+
a = block_diag(1)
|
| 169 |
+
assert_equal(a.shape, (1, 1))
|
| 170 |
+
assert_array_equal(a, [[1]])
|
| 171 |
+
|
| 172 |
+
a = block_diag([2, 3], 4)
|
| 173 |
+
assert_array_equal(a, [[2, 3, 0], [0, 0, 4]])
|
| 174 |
+
|
| 175 |
+
def test_bad_arg(self):
|
| 176 |
+
assert_raises(ValueError, block_diag, [[[1]]])
|
| 177 |
+
|
| 178 |
+
def test_no_args(self):
|
| 179 |
+
a = block_diag()
|
| 180 |
+
assert_equal(a.ndim, 2)
|
| 181 |
+
assert_equal(a.nbytes, 0)
|
| 182 |
+
|
| 183 |
+
def test_empty_matrix_arg(self):
|
| 184 |
+
# regression test for gh-4596: check the shape of the result
|
| 185 |
+
# for empty matrix inputs. Empty matrices are no longer ignored
|
| 186 |
+
# (gh-4908) it is viewed as a shape (1, 0) matrix.
|
| 187 |
+
a = block_diag([[1, 0], [0, 1]],
|
| 188 |
+
[],
|
| 189 |
+
[[2, 3], [4, 5], [6, 7]])
|
| 190 |
+
assert_array_equal(a, [[1, 0, 0, 0],
|
| 191 |
+
[0, 1, 0, 0],
|
| 192 |
+
[0, 0, 0, 0],
|
| 193 |
+
[0, 0, 2, 3],
|
| 194 |
+
[0, 0, 4, 5],
|
| 195 |
+
[0, 0, 6, 7]])
|
| 196 |
+
|
| 197 |
+
def test_zerosized_matrix_arg(self):
|
| 198 |
+
# test for gh-4908: check the shape of the result for
|
| 199 |
+
# zero-sized matrix inputs, i.e. matrices with shape (0,n) or (n,0).
|
| 200 |
+
# note that [[]] takes shape (1,0)
|
| 201 |
+
a = block_diag([[1, 0], [0, 1]],
|
| 202 |
+
[[]],
|
| 203 |
+
[[2, 3], [4, 5], [6, 7]],
|
| 204 |
+
np.zeros([0, 2], dtype='int32'))
|
| 205 |
+
assert_array_equal(a, [[1, 0, 0, 0, 0, 0],
|
| 206 |
+
[0, 1, 0, 0, 0, 0],
|
| 207 |
+
[0, 0, 0, 0, 0, 0],
|
| 208 |
+
[0, 0, 2, 3, 0, 0],
|
| 209 |
+
[0, 0, 4, 5, 0, 0],
|
| 210 |
+
[0, 0, 6, 7, 0, 0]])
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
class TestKron:
|
| 214 |
+
@pytest.mark.thread_unsafe
|
| 215 |
+
def test_dep(self):
|
| 216 |
+
with pytest.deprecated_call(match="`kron`"):
|
| 217 |
+
kron(np.array([[1, 2],[3, 4]]),np.array([[1, 1, 1]]))
|
| 218 |
+
|
| 219 |
+
@pytest.mark.filterwarnings('ignore::DeprecationWarning')
|
| 220 |
+
def test_basic(self):
|
| 221 |
+
|
| 222 |
+
a = kron(array([[1, 2], [3, 4]]), array([[1, 1, 1]]))
|
| 223 |
+
assert_array_equal(a, array([[1, 1, 1, 2, 2, 2],
|
| 224 |
+
[3, 3, 3, 4, 4, 4]]))
|
| 225 |
+
|
| 226 |
+
m1 = array([[1, 2], [3, 4]])
|
| 227 |
+
m2 = array([[10], [11]])
|
| 228 |
+
a = kron(m1, m2)
|
| 229 |
+
expected = array([[10, 20],
|
| 230 |
+
[11, 22],
|
| 231 |
+
[30, 40],
|
| 232 |
+
[33, 44]])
|
| 233 |
+
assert_array_equal(a, expected)
|
| 234 |
+
|
| 235 |
+
@pytest.mark.filterwarnings('ignore::DeprecationWarning')
|
| 236 |
+
def test_empty(self):
|
| 237 |
+
m1 = np.empty((0, 2))
|
| 238 |
+
m2 = np.empty((1, 3))
|
| 239 |
+
a = kron(m1, m2)
|
| 240 |
+
assert_allclose(a, np.empty((0, 6)))
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
class TestHelmert:
|
| 244 |
+
|
| 245 |
+
def test_orthogonality(self):
|
| 246 |
+
for n in range(1, 7):
|
| 247 |
+
H = helmert(n, full=True)
|
| 248 |
+
Id = np.eye(n)
|
| 249 |
+
assert_allclose(H.dot(H.T), Id, atol=1e-12)
|
| 250 |
+
assert_allclose(H.T.dot(H), Id, atol=1e-12)
|
| 251 |
+
|
| 252 |
+
def test_subspace(self):
|
| 253 |
+
for n in range(2, 7):
|
| 254 |
+
H_full = helmert(n, full=True)
|
| 255 |
+
H_partial = helmert(n)
|
| 256 |
+
for U in H_full[1:, :].T, H_partial.T:
|
| 257 |
+
C = np.eye(n) - np.full((n, n), 1 / n)
|
| 258 |
+
assert_allclose(U.dot(U.T), C)
|
| 259 |
+
assert_allclose(U.T.dot(U), np.eye(n-1), atol=1e-12)
|
| 260 |
+
|
| 261 |
+
|
| 262 |
+
class TestHilbert:
|
| 263 |
+
|
| 264 |
+
def test_basic(self):
|
| 265 |
+
h3 = array([[1.0, 1/2., 1/3.],
|
| 266 |
+
[1/2., 1/3., 1/4.],
|
| 267 |
+
[1/3., 1/4., 1/5.]])
|
| 268 |
+
assert_array_almost_equal(hilbert(3), h3)
|
| 269 |
+
|
| 270 |
+
assert_array_equal(hilbert(1), [[1.0]])
|
| 271 |
+
|
| 272 |
+
h0 = hilbert(0)
|
| 273 |
+
assert_equal(h0.shape, (0, 0))
|
| 274 |
+
|
| 275 |
+
|
| 276 |
+
class TestInvHilbert:
|
| 277 |
+
|
| 278 |
+
def test_basic(self):
|
| 279 |
+
invh1 = array([[1]])
|
| 280 |
+
assert_array_equal(invhilbert(1, exact=True), invh1)
|
| 281 |
+
assert_array_equal(invhilbert(1), invh1)
|
| 282 |
+
|
| 283 |
+
invh2 = array([[4, -6],
|
| 284 |
+
[-6, 12]])
|
| 285 |
+
assert_array_equal(invhilbert(2, exact=True), invh2)
|
| 286 |
+
assert_array_almost_equal(invhilbert(2), invh2)
|
| 287 |
+
|
| 288 |
+
invh3 = array([[9, -36, 30],
|
| 289 |
+
[-36, 192, -180],
|
| 290 |
+
[30, -180, 180]])
|
| 291 |
+
assert_array_equal(invhilbert(3, exact=True), invh3)
|
| 292 |
+
assert_array_almost_equal(invhilbert(3), invh3)
|
| 293 |
+
|
| 294 |
+
invh4 = array([[16, -120, 240, -140],
|
| 295 |
+
[-120, 1200, -2700, 1680],
|
| 296 |
+
[240, -2700, 6480, -4200],
|
| 297 |
+
[-140, 1680, -4200, 2800]])
|
| 298 |
+
assert_array_equal(invhilbert(4, exact=True), invh4)
|
| 299 |
+
assert_array_almost_equal(invhilbert(4), invh4)
|
| 300 |
+
|
| 301 |
+
invh5 = array([[25, -300, 1050, -1400, 630],
|
| 302 |
+
[-300, 4800, -18900, 26880, -12600],
|
| 303 |
+
[1050, -18900, 79380, -117600, 56700],
|
| 304 |
+
[-1400, 26880, -117600, 179200, -88200],
|
| 305 |
+
[630, -12600, 56700, -88200, 44100]])
|
| 306 |
+
assert_array_equal(invhilbert(5, exact=True), invh5)
|
| 307 |
+
assert_array_almost_equal(invhilbert(5), invh5)
|
| 308 |
+
|
| 309 |
+
invh17 = array([
|
| 310 |
+
[289, -41616, 1976760, -46124400, 629598060, -5540462928,
|
| 311 |
+
33374693352, -143034400080, 446982500250, -1033026222800,
|
| 312 |
+
1774926873720, -2258997839280, 2099709530100, -1384423866000,
|
| 313 |
+
613101997800, -163493866080, 19835652870],
|
| 314 |
+
[-41616, 7990272, -426980160, 10627061760, -151103534400,
|
| 315 |
+
1367702848512, -8410422724704, 36616806420480, -115857864064800,
|
| 316 |
+
270465047424000, -468580694662080, 600545887119360,
|
| 317 |
+
-561522320049600, 372133135180800, -165537539406000,
|
| 318 |
+
44316454993920, -5395297580640],
|
| 319 |
+
[1976760, -426980160, 24337869120, -630981792000, 9228108708000,
|
| 320 |
+
-85267724461920, 532660105897920, -2348052711713280,
|
| 321 |
+
7504429831470000, -17664748409880000, 30818191841236800,
|
| 322 |
+
-39732544853164800, 37341234283298400, -24857330514030000,
|
| 323 |
+
11100752642520000, -2982128117299200, 364182586693200],
|
| 324 |
+
[-46124400, 10627061760, -630981792000, 16826181120000,
|
| 325 |
+
-251209625940000, 2358021022156800, -14914482965141760,
|
| 326 |
+
66409571644416000, -214015221119700000, 507295338950400000,
|
| 327 |
+
-890303319857952000, 1153715376477081600, -1089119333262870000,
|
| 328 |
+
727848632044800000, -326170262829600000, 87894302404608000,
|
| 329 |
+
-10763618673376800],
|
| 330 |
+
[629598060, -151103534400, 9228108708000,
|
| 331 |
+
-251209625940000, 3810012660090000, -36210360321495360,
|
| 332 |
+
231343968720664800, -1038687206500944000, 3370739732635275000,
|
| 333 |
+
-8037460526495400000, 14178080368737885600, -18454939322943942000,
|
| 334 |
+
17489975175339030000, -11728977435138600000, 5272370630081100000,
|
| 335 |
+
-1424711708039692800, 174908803442373000],
|
| 336 |
+
[-5540462928, 1367702848512, -85267724461920, 2358021022156800,
|
| 337 |
+
-36210360321495360, 347619459086355456, -2239409617216035264,
|
| 338 |
+
10124803292907663360, -33052510749726468000,
|
| 339 |
+
79217210949138662400, -140362995650505067440,
|
| 340 |
+
183420385176741672960, -174433352415381259200,
|
| 341 |
+
117339159519533952000, -52892422160973595200,
|
| 342 |
+
14328529177999196160, -1763080738699119840],
|
| 343 |
+
[33374693352, -8410422724704, 532660105897920,
|
| 344 |
+
-14914482965141760, 231343968720664800, -2239409617216035264,
|
| 345 |
+
14527452132196331328, -66072377044391477760,
|
| 346 |
+
216799987176909536400, -521925895055522958000,
|
| 347 |
+
928414062734059661760, -1217424500995626443520,
|
| 348 |
+
1161358898976091015200, -783401860847777371200,
|
| 349 |
+
354015418167362952000, -96120549902411274240,
|
| 350 |
+
11851820521255194480],
|
| 351 |
+
[-143034400080, 36616806420480, -2348052711713280,
|
| 352 |
+
66409571644416000, -1038687206500944000, 10124803292907663360,
|
| 353 |
+
-66072377044391477760, 302045152202932469760,
|
| 354 |
+
-995510145200094810000, 2405996923185123840000,
|
| 355 |
+
-4294704507885446054400, 5649058909023744614400,
|
| 356 |
+
-5403874060541811254400, 3654352703663101440000,
|
| 357 |
+
-1655137020003255360000, 450325202737117593600,
|
| 358 |
+
-55630994283442749600],
|
| 359 |
+
[446982500250, -115857864064800, 7504429831470000,
|
| 360 |
+
-214015221119700000, 3370739732635275000, -33052510749726468000,
|
| 361 |
+
216799987176909536400, -995510145200094810000,
|
| 362 |
+
3293967392206196062500, -7988661659013106500000,
|
| 363 |
+
14303908928401362270000, -18866974090684772052000,
|
| 364 |
+
18093328327706957325000, -12263364009096700500000,
|
| 365 |
+
5565847995255512250000, -1517208935002984080000,
|
| 366 |
+
187754605706619279900],
|
| 367 |
+
[-1033026222800, 270465047424000, -17664748409880000,
|
| 368 |
+
507295338950400000, -8037460526495400000, 79217210949138662400,
|
| 369 |
+
-521925895055522958000, 2405996923185123840000,
|
| 370 |
+
-7988661659013106500000, 19434404971634224000000,
|
| 371 |
+
-34894474126569249192000, 46141453390504792320000,
|
| 372 |
+
-44349976506971935800000, 30121928988527376000000,
|
| 373 |
+
-13697025107665828500000, 3740200989399948902400,
|
| 374 |
+
-463591619028689580000],
|
| 375 |
+
[1774926873720, -468580694662080,
|
| 376 |
+
30818191841236800, -890303319857952000, 14178080368737885600,
|
| 377 |
+
-140362995650505067440, 928414062734059661760,
|
| 378 |
+
-4294704507885446054400, 14303908928401362270000,
|
| 379 |
+
-34894474126569249192000, 62810053427824648545600,
|
| 380 |
+
-83243376594051600326400, 80177044485212743068000,
|
| 381 |
+
-54558343880470209780000, 24851882355348879230400,
|
| 382 |
+
-6797096028813368678400, 843736746632215035600],
|
| 383 |
+
[-2258997839280, 600545887119360, -39732544853164800,
|
| 384 |
+
1153715376477081600, -18454939322943942000, 183420385176741672960,
|
| 385 |
+
-1217424500995626443520, 5649058909023744614400,
|
| 386 |
+
-18866974090684772052000, 46141453390504792320000,
|
| 387 |
+
-83243376594051600326400, 110552468520163390156800,
|
| 388 |
+
-106681852579497947388000, 72720410752415168870400,
|
| 389 |
+
-33177973900974346080000, 9087761081682520473600,
|
| 390 |
+
-1129631016152221783200],
|
| 391 |
+
[2099709530100, -561522320049600, 37341234283298400,
|
| 392 |
+
-1089119333262870000, 17489975175339030000,
|
| 393 |
+
-174433352415381259200, 1161358898976091015200,
|
| 394 |
+
-5403874060541811254400, 18093328327706957325000,
|
| 395 |
+
-44349976506971935800000, 80177044485212743068000,
|
| 396 |
+
-106681852579497947388000, 103125790826848015808400,
|
| 397 |
+
-70409051543137015800000, 32171029219823375700000,
|
| 398 |
+
-8824053728865840192000, 1098252376814660067000],
|
| 399 |
+
[-1384423866000, 372133135180800,
|
| 400 |
+
-24857330514030000, 727848632044800000, -11728977435138600000,
|
| 401 |
+
117339159519533952000, -783401860847777371200,
|
| 402 |
+
3654352703663101440000, -12263364009096700500000,
|
| 403 |
+
30121928988527376000000, -54558343880470209780000,
|
| 404 |
+
72720410752415168870400, -70409051543137015800000,
|
| 405 |
+
48142941226076592000000, -22027500987368499000000,
|
| 406 |
+
6049545098753157120000, -753830033789944188000],
|
| 407 |
+
[613101997800, -165537539406000,
|
| 408 |
+
11100752642520000, -326170262829600000, 5272370630081100000,
|
| 409 |
+
-52892422160973595200, 354015418167362952000,
|
| 410 |
+
-1655137020003255360000, 5565847995255512250000,
|
| 411 |
+
-13697025107665828500000, 24851882355348879230400,
|
| 412 |
+
-33177973900974346080000, 32171029219823375700000,
|
| 413 |
+
-22027500987368499000000, 10091416708498869000000,
|
| 414 |
+
-2774765838662800128000, 346146444087219270000],
|
| 415 |
+
[-163493866080, 44316454993920, -2982128117299200,
|
| 416 |
+
87894302404608000, -1424711708039692800,
|
| 417 |
+
14328529177999196160, -96120549902411274240,
|
| 418 |
+
450325202737117593600, -1517208935002984080000,
|
| 419 |
+
3740200989399948902400, -6797096028813368678400,
|
| 420 |
+
9087761081682520473600, -8824053728865840192000,
|
| 421 |
+
6049545098753157120000, -2774765838662800128000,
|
| 422 |
+
763806510427609497600, -95382575704033754400],
|
| 423 |
+
[19835652870, -5395297580640, 364182586693200, -10763618673376800,
|
| 424 |
+
174908803442373000, -1763080738699119840, 11851820521255194480,
|
| 425 |
+
-55630994283442749600, 187754605706619279900,
|
| 426 |
+
-463591619028689580000, 843736746632215035600,
|
| 427 |
+
-1129631016152221783200, 1098252376814660067000,
|
| 428 |
+
-753830033789944188000, 346146444087219270000,
|
| 429 |
+
-95382575704033754400, 11922821963004219300]
|
| 430 |
+
])
|
| 431 |
+
assert_array_equal(invhilbert(17, exact=True), invh17)
|
| 432 |
+
assert_allclose(invhilbert(17), invh17.astype(float), rtol=1e-12)
|
| 433 |
+
|
| 434 |
+
def test_inverse(self):
|
| 435 |
+
for n in range(1, 10):
|
| 436 |
+
a = hilbert(n)
|
| 437 |
+
b = invhilbert(n)
|
| 438 |
+
# The Hilbert matrix is increasingly badly conditioned,
|
| 439 |
+
# so take that into account in the test
|
| 440 |
+
c = cond(a)
|
| 441 |
+
assert_allclose(a.dot(b), eye(n), atol=1e-15*c, rtol=1e-15*c)
|
| 442 |
+
|
| 443 |
+
|
| 444 |
+
class TestPascal:
|
| 445 |
+
|
| 446 |
+
cases = [
|
| 447 |
+
(1, array([[1]]), array([[1]])),
|
| 448 |
+
(2, array([[1, 1],
|
| 449 |
+
[1, 2]]),
|
| 450 |
+
array([[1, 0],
|
| 451 |
+
[1, 1]])),
|
| 452 |
+
(3, array([[1, 1, 1],
|
| 453 |
+
[1, 2, 3],
|
| 454 |
+
[1, 3, 6]]),
|
| 455 |
+
array([[1, 0, 0],
|
| 456 |
+
[1, 1, 0],
|
| 457 |
+
[1, 2, 1]])),
|
| 458 |
+
(4, array([[1, 1, 1, 1],
|
| 459 |
+
[1, 2, 3, 4],
|
| 460 |
+
[1, 3, 6, 10],
|
| 461 |
+
[1, 4, 10, 20]]),
|
| 462 |
+
array([[1, 0, 0, 0],
|
| 463 |
+
[1, 1, 0, 0],
|
| 464 |
+
[1, 2, 1, 0],
|
| 465 |
+
[1, 3, 3, 1]])),
|
| 466 |
+
]
|
| 467 |
+
|
| 468 |
+
def check_case(self, n, sym, low):
|
| 469 |
+
assert_array_equal(pascal(n), sym)
|
| 470 |
+
assert_array_equal(pascal(n, kind='lower'), low)
|
| 471 |
+
assert_array_equal(pascal(n, kind='upper'), low.T)
|
| 472 |
+
assert_array_almost_equal(pascal(n, exact=False), sym)
|
| 473 |
+
assert_array_almost_equal(pascal(n, exact=False, kind='lower'), low)
|
| 474 |
+
assert_array_almost_equal(pascal(n, exact=False, kind='upper'), low.T)
|
| 475 |
+
|
| 476 |
+
def test_cases(self):
|
| 477 |
+
for n, sym, low in self.cases:
|
| 478 |
+
self.check_case(n, sym, low)
|
| 479 |
+
|
| 480 |
+
def test_big(self):
|
| 481 |
+
p = pascal(50)
|
| 482 |
+
assert p[-1, -1] == comb(98, 49, exact=True)
|
| 483 |
+
|
| 484 |
+
def test_threshold(self):
|
| 485 |
+
# Regression test. An early version of `pascal` returned an
|
| 486 |
+
# array of type np.uint64 for n=35, but that data type is too small
|
| 487 |
+
# to hold p[-1, -1]. The second assert_equal below would fail
|
| 488 |
+
# because p[-1, -1] overflowed.
|
| 489 |
+
p = pascal(34)
|
| 490 |
+
assert_equal(2*p.item(-1, -2), p.item(-1, -1), err_msg="n = 34")
|
| 491 |
+
p = pascal(35)
|
| 492 |
+
assert_equal(2.*p.item(-1, -2), 1.*p.item(-1, -1), err_msg="n = 35")
|
| 493 |
+
|
| 494 |
+
|
| 495 |
+
def test_invpascal():
|
| 496 |
+
|
| 497 |
+
def check_invpascal(n, kind, exact):
|
| 498 |
+
ip = invpascal(n, kind=kind, exact=exact)
|
| 499 |
+
p = pascal(n, kind=kind, exact=exact)
|
| 500 |
+
# Matrix-multiply ip and p, and check that we get the identity matrix.
|
| 501 |
+
# We can't use the simple expression e = ip.dot(p), because when
|
| 502 |
+
# n < 35 and exact is True, p.dtype is np.uint64 and ip.dtype is
|
| 503 |
+
# np.int64. The product of those dtypes is np.float64, which loses
|
| 504 |
+
# precision when n is greater than 18. Instead we'll cast both to
|
| 505 |
+
# object arrays, and then multiply.
|
| 506 |
+
e = ip.astype(object).dot(p.astype(object))
|
| 507 |
+
assert_array_equal(e, eye(n), err_msg="n=%d kind=%r exact=%r" %
|
| 508 |
+
(n, kind, exact))
|
| 509 |
+
|
| 510 |
+
kinds = ['symmetric', 'lower', 'upper']
|
| 511 |
+
|
| 512 |
+
ns = [1, 2, 5, 18]
|
| 513 |
+
for n in ns:
|
| 514 |
+
for kind in kinds:
|
| 515 |
+
for exact in [True, False]:
|
| 516 |
+
check_invpascal(n, kind, exact)
|
| 517 |
+
|
| 518 |
+
ns = [19, 34, 35, 50]
|
| 519 |
+
for n in ns:
|
| 520 |
+
for kind in kinds:
|
| 521 |
+
check_invpascal(n, kind, True)
|
| 522 |
+
|
| 523 |
+
|
| 524 |
+
def test_dft():
|
| 525 |
+
m = dft(2)
|
| 526 |
+
expected = array([[1.0, 1.0], [1.0, -1.0]])
|
| 527 |
+
assert_array_almost_equal(m, expected)
|
| 528 |
+
m = dft(2, scale='n')
|
| 529 |
+
assert_array_almost_equal(m, expected/2.0)
|
| 530 |
+
m = dft(2, scale='sqrtn')
|
| 531 |
+
assert_array_almost_equal(m, expected/sqrt(2.0))
|
| 532 |
+
|
| 533 |
+
x = array([0, 1, 2, 3, 4, 5, 0, 1])
|
| 534 |
+
m = dft(8)
|
| 535 |
+
mx = m.dot(x)
|
| 536 |
+
fx = fft(x)
|
| 537 |
+
assert_array_almost_equal(mx, fx)
|
| 538 |
+
|
| 539 |
+
|
| 540 |
+
def test_fiedler():
|
| 541 |
+
f = fiedler([])
|
| 542 |
+
assert_equal(f.size, 0)
|
| 543 |
+
f = fiedler([123.])
|
| 544 |
+
assert_array_equal(f, np.array([[0.]]))
|
| 545 |
+
f = fiedler(np.arange(1, 7))
|
| 546 |
+
des = np.array([[0, 1, 2, 3, 4, 5],
|
| 547 |
+
[1, 0, 1, 2, 3, 4],
|
| 548 |
+
[2, 1, 0, 1, 2, 3],
|
| 549 |
+
[3, 2, 1, 0, 1, 2],
|
| 550 |
+
[4, 3, 2, 1, 0, 1],
|
| 551 |
+
[5, 4, 3, 2, 1, 0]])
|
| 552 |
+
assert_array_equal(f, des)
|
| 553 |
+
|
| 554 |
+
|
| 555 |
+
def test_fiedler_companion():
|
| 556 |
+
fc = fiedler_companion([])
|
| 557 |
+
assert_equal(fc.size, 0)
|
| 558 |
+
fc = fiedler_companion([1.])
|
| 559 |
+
assert_equal(fc.size, 0)
|
| 560 |
+
fc = fiedler_companion([1., 2.])
|
| 561 |
+
assert_array_equal(fc, np.array([[-2.]]))
|
| 562 |
+
fc = fiedler_companion([1e-12, 2., 3.])
|
| 563 |
+
assert_array_almost_equal(fc, companion([1e-12, 2., 3.]))
|
| 564 |
+
with assert_raises(ValueError):
|
| 565 |
+
fiedler_companion([0, 1, 2])
|
| 566 |
+
fc = fiedler_companion([1., -16., 86., -176., 105.])
|
| 567 |
+
assert_array_almost_equal(eigvals(fc),
|
| 568 |
+
np.array([7., 5., 3., 1.]))
|
| 569 |
+
|
| 570 |
+
|
| 571 |
+
class TestConvolutionMatrix:
|
| 572 |
+
"""
|
| 573 |
+
Test convolution_matrix vs. numpy.convolve for various parameters.
|
| 574 |
+
"""
|
| 575 |
+
|
| 576 |
+
def create_vector(self, n, cpx):
|
| 577 |
+
"""Make a complex or real test vector of length n."""
|
| 578 |
+
x = np.linspace(-2.5, 2.2, n)
|
| 579 |
+
if cpx:
|
| 580 |
+
x = x + 1j*np.linspace(-1.5, 3.1, n)
|
| 581 |
+
return x
|
| 582 |
+
|
| 583 |
+
def test_bad_n(self):
|
| 584 |
+
# n must be a positive integer
|
| 585 |
+
with pytest.raises(ValueError, match='n must be a positive integer'):
|
| 586 |
+
convolution_matrix([1, 2, 3], 0)
|
| 587 |
+
|
| 588 |
+
def test_empty_first_arg(self):
|
| 589 |
+
# first arg must have at least one value
|
| 590 |
+
with pytest.raises(ValueError, match=r'len\(a\)'):
|
| 591 |
+
convolution_matrix([], 4)
|
| 592 |
+
|
| 593 |
+
def test_bad_mode(self):
|
| 594 |
+
# mode must be in ('full', 'valid', 'same')
|
| 595 |
+
with pytest.raises(ValueError, match='mode.*must be one of'):
|
| 596 |
+
convolution_matrix((1, 1), 4, mode='invalid argument')
|
| 597 |
+
|
| 598 |
+
@pytest.mark.parametrize('cpx', [False, True])
|
| 599 |
+
@pytest.mark.parametrize('na', [1, 2, 9])
|
| 600 |
+
@pytest.mark.parametrize('nv', [1, 2, 9])
|
| 601 |
+
@pytest.mark.parametrize('mode', [None, 'full', 'valid', 'same'])
|
| 602 |
+
def test_against_numpy_convolve(self, cpx, na, nv, mode):
|
| 603 |
+
a = self.create_vector(na, cpx)
|
| 604 |
+
v = self.create_vector(nv, cpx)
|
| 605 |
+
if mode is None:
|
| 606 |
+
y1 = np.convolve(v, a)
|
| 607 |
+
A = convolution_matrix(a, nv)
|
| 608 |
+
else:
|
| 609 |
+
y1 = np.convolve(v, a, mode)
|
| 610 |
+
A = convolution_matrix(a, nv, mode)
|
| 611 |
+
y2 = A @ v
|
| 612 |
+
assert_array_almost_equal(y1, y2)
|
| 613 |
+
|
| 614 |
+
|
| 615 |
+
@pytest.mark.thread_unsafe
|
| 616 |
+
@pytest.mark.fail_slow(5) # `leslie` has an import in the function
|
| 617 |
+
@pytest.mark.parametrize('f, args', [(circulant, ()),
|
| 618 |
+
(companion, ()),
|
| 619 |
+
(convolution_matrix, (5, 'same')),
|
| 620 |
+
(fiedler, ()),
|
| 621 |
+
(fiedler_companion, ()),
|
| 622 |
+
(leslie, (np.arange(9),)),
|
| 623 |
+
(toeplitz, (np.arange(9),)),
|
| 624 |
+
])
|
| 625 |
+
def test_batch(f, args):
|
| 626 |
+
rng = np.random.default_rng(283592436523456)
|
| 627 |
+
batch_shape = (2, 3)
|
| 628 |
+
m = 10
|
| 629 |
+
A = rng.random(batch_shape + (m,))
|
| 630 |
+
|
| 631 |
+
if f in {toeplitz}:
|
| 632 |
+
message = "Beginning in SciPy 1.17, multidimensional input will be..."
|
| 633 |
+
with pytest.warns(FutureWarning, match=message):
|
| 634 |
+
f(A, *args)
|
| 635 |
+
return
|
| 636 |
+
|
| 637 |
+
res = f(A, *args)
|
| 638 |
+
ref = np.asarray([f(a, *args) for a in A.reshape(-1, m)])
|
| 639 |
+
ref = ref.reshape(A.shape[:-1] + ref.shape[-2:])
|
| 640 |
+
assert_allclose(res, ref)
|
pace_revision_1/.venv-pace/lib/python3.11/site-packages/scipy/misc/__init__.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import warnings
|
| 2 |
+
warnings.warn(
|
| 3 |
+
"scipy.misc is deprecated and will be removed in 2.0.0",
|
| 4 |
+
DeprecationWarning,
|
| 5 |
+
stacklevel=2
|
| 6 |
+
)
|