aboutsummaryrefslogtreecommitdiff
path: root/.venv/lib/python3.12/site-packages/numpy/linalg
diff options
context:
space:
mode:
Diffstat (limited to '.venv/lib/python3.12/site-packages/numpy/linalg')
-rw-r--r--.venv/lib/python3.12/site-packages/numpy/linalg/__init__.py80
-rw-r--r--.venv/lib/python3.12/site-packages/numpy/linalg/__init__.pyi30
-rwxr-xr-x.venv/lib/python3.12/site-packages/numpy/linalg/_umath_linalg.cpython-312-x86_64-linux-gnu.sobin0 -> 216793 bytes
-rwxr-xr-x.venv/lib/python3.12/site-packages/numpy/linalg/lapack_lite.cpython-312-x86_64-linux-gnu.sobin0 -> 29849 bytes
-rw-r--r--.venv/lib/python3.12/site-packages/numpy/linalg/linalg.py2836
-rw-r--r--.venv/lib/python3.12/site-packages/numpy/linalg/linalg.pyi297
-rw-r--r--.venv/lib/python3.12/site-packages/numpy/linalg/tests/__init__.py0
-rw-r--r--.venv/lib/python3.12/site-packages/numpy/linalg/tests/test_deprecations.py20
-rw-r--r--.venv/lib/python3.12/site-packages/numpy/linalg/tests/test_linalg.py2198
-rw-r--r--.venv/lib/python3.12/site-packages/numpy/linalg/tests/test_regression.py145
10 files changed, 5606 insertions, 0 deletions
diff --git a/.venv/lib/python3.12/site-packages/numpy/linalg/__init__.py b/.venv/lib/python3.12/site-packages/numpy/linalg/__init__.py
new file mode 100644
index 00000000..93943de3
--- /dev/null
+++ b/.venv/lib/python3.12/site-packages/numpy/linalg/__init__.py
@@ -0,0 +1,80 @@
+"""
+``numpy.linalg``
+================
+
+The NumPy linear algebra functions rely on BLAS and LAPACK to provide efficient
+low level implementations of standard linear algebra algorithms. Those
+libraries may be provided by NumPy itself using C versions of a subset of their
+reference implementations but, when possible, highly optimized libraries that
+take advantage of specialized processor functionality are preferred. Examples
+of such libraries are OpenBLAS, MKL (TM), and ATLAS. Because those libraries
+are multithreaded and processor dependent, environmental variables and external
+packages such as threadpoolctl may be needed to control the number of threads
+or specify the processor architecture.
+
+- OpenBLAS: https://www.openblas.net/
+- threadpoolctl: https://github.com/joblib/threadpoolctl
+
+Please note that the most-used linear algebra functions in NumPy are present in
+the main ``numpy`` namespace rather than in ``numpy.linalg``. There are:
+``dot``, ``vdot``, ``inner``, ``outer``, ``matmul``, ``tensordot``, ``einsum``,
+``einsum_path`` and ``kron``.
+
+Functions present in numpy.linalg are listed below.
+
+
+Matrix and vector products
+--------------------------
+
+ multi_dot
+ matrix_power
+
+Decompositions
+--------------
+
+ cholesky
+ qr
+ svd
+
+Matrix eigenvalues
+------------------
+
+ eig
+ eigh
+ eigvals
+ eigvalsh
+
+Norms and other numbers
+-----------------------
+
+ norm
+ cond
+ det
+ matrix_rank
+ slogdet
+
+Solving equations and inverting matrices
+----------------------------------------
+
+ solve
+ tensorsolve
+ lstsq
+ inv
+ pinv
+ tensorinv
+
+Exceptions
+----------
+
+ LinAlgError
+
+"""
+# To get sub-modules
+from . import linalg
+from .linalg import *
+
+__all__ = linalg.__all__.copy()
+
+from numpy._pytesttester import PytestTester
+test = PytestTester(__name__)
+del PytestTester
diff --git a/.venv/lib/python3.12/site-packages/numpy/linalg/__init__.pyi b/.venv/lib/python3.12/site-packages/numpy/linalg/__init__.pyi
new file mode 100644
index 00000000..d9acd558
--- /dev/null
+++ b/.venv/lib/python3.12/site-packages/numpy/linalg/__init__.pyi
@@ -0,0 +1,30 @@
+from numpy.linalg.linalg import (
+ matrix_power as matrix_power,
+ solve as solve,
+ tensorsolve as tensorsolve,
+ tensorinv as tensorinv,
+ inv as inv,
+ cholesky as cholesky,
+ eigvals as eigvals,
+ eigvalsh as eigvalsh,
+ pinv as pinv,
+ slogdet as slogdet,
+ det as det,
+ svd as svd,
+ eig as eig,
+ eigh as eigh,
+ lstsq as lstsq,
+ norm as norm,
+ qr as qr,
+ cond as cond,
+ matrix_rank as matrix_rank,
+ multi_dot as multi_dot,
+)
+
+from numpy._pytesttester import PytestTester
+
+__all__: list[str]
+__path__: list[str]
+test: PytestTester
+
+class LinAlgError(Exception): ...
diff --git a/.venv/lib/python3.12/site-packages/numpy/linalg/_umath_linalg.cpython-312-x86_64-linux-gnu.so b/.venv/lib/python3.12/site-packages/numpy/linalg/_umath_linalg.cpython-312-x86_64-linux-gnu.so
new file mode 100755
index 00000000..56aa542f
--- /dev/null
+++ b/.venv/lib/python3.12/site-packages/numpy/linalg/_umath_linalg.cpython-312-x86_64-linux-gnu.so
Binary files differ
diff --git a/.venv/lib/python3.12/site-packages/numpy/linalg/lapack_lite.cpython-312-x86_64-linux-gnu.so b/.venv/lib/python3.12/site-packages/numpy/linalg/lapack_lite.cpython-312-x86_64-linux-gnu.so
new file mode 100755
index 00000000..d1e00858
--- /dev/null
+++ b/.venv/lib/python3.12/site-packages/numpy/linalg/lapack_lite.cpython-312-x86_64-linux-gnu.so
Binary files differ
diff --git a/.venv/lib/python3.12/site-packages/numpy/linalg/linalg.py b/.venv/lib/python3.12/site-packages/numpy/linalg/linalg.py
new file mode 100644
index 00000000..b838b939
--- /dev/null
+++ b/.venv/lib/python3.12/site-packages/numpy/linalg/linalg.py
@@ -0,0 +1,2836 @@
+"""Lite version of scipy.linalg.
+
+Notes
+-----
+This module is a lite version of the linalg.py module in SciPy which
+contains high-level Python interface to the LAPACK library. The lite
+version only accesses the following LAPACK functions: dgesv, zgesv,
+dgeev, zgeev, dgesdd, zgesdd, dgelsd, zgelsd, dsyevd, zheevd, dgetrf,
+zgetrf, dpotrf, zpotrf, dgeqrf, zgeqrf, zungqr, dorgqr.
+"""
+
+__all__ = ['matrix_power', 'solve', 'tensorsolve', 'tensorinv', 'inv',
+ 'cholesky', 'eigvals', 'eigvalsh', 'pinv', 'slogdet', 'det',
+ 'svd', 'eig', 'eigh', 'lstsq', 'norm', 'qr', 'cond', 'matrix_rank',
+ 'LinAlgError', 'multi_dot']
+
+import functools
+import operator
+import warnings
+from typing import NamedTuple, Any
+
+from .._utils import set_module
+from numpy.core import (
+ array, asarray, zeros, empty, empty_like, intc, single, double,
+ csingle, cdouble, inexact, complexfloating, newaxis, all, Inf, dot,
+ add, multiply, sqrt, sum, isfinite,
+ finfo, errstate, geterrobj, moveaxis, amin, amax, prod, abs,
+ atleast_2d, intp, asanyarray, object_, matmul,
+ swapaxes, divide, count_nonzero, isnan, sign, argsort, sort,
+ reciprocal
+)
+from numpy.core.multiarray import normalize_axis_index
+from numpy.core import overrides
+from numpy.lib.twodim_base import triu, eye
+from numpy.linalg import _umath_linalg
+
+from numpy._typing import NDArray
+
+class EigResult(NamedTuple):
+ eigenvalues: NDArray[Any]
+ eigenvectors: NDArray[Any]
+
+class EighResult(NamedTuple):
+ eigenvalues: NDArray[Any]
+ eigenvectors: NDArray[Any]
+
+class QRResult(NamedTuple):
+ Q: NDArray[Any]
+ R: NDArray[Any]
+
+class SlogdetResult(NamedTuple):
+ sign: NDArray[Any]
+ logabsdet: NDArray[Any]
+
+class SVDResult(NamedTuple):
+ U: NDArray[Any]
+ S: NDArray[Any]
+ Vh: NDArray[Any]
+
+array_function_dispatch = functools.partial(
+ overrides.array_function_dispatch, module='numpy.linalg')
+
+
+fortran_int = intc
+
+
+@set_module('numpy.linalg')
+class LinAlgError(ValueError):
+ """
+ Generic Python-exception-derived object raised by linalg functions.
+
+ General purpose exception class, derived from Python's ValueError
+ class, programmatically raised in linalg functions when a Linear
+ Algebra-related condition would prevent further correct execution of the
+ function.
+
+ Parameters
+ ----------
+ None
+
+ Examples
+ --------
+ >>> from numpy import linalg as LA
+ >>> LA.inv(np.zeros((2,2)))
+ Traceback (most recent call last):
+ File "<stdin>", line 1, in <module>
+ File "...linalg.py", line 350,
+ in inv return wrap(solve(a, identity(a.shape[0], dtype=a.dtype)))
+ File "...linalg.py", line 249,
+ in solve
+ raise LinAlgError('Singular matrix')
+ numpy.linalg.LinAlgError: Singular matrix
+
+ """
+
+
+def _determine_error_states():
+ errobj = geterrobj()
+ bufsize = errobj[0]
+
+ with errstate(invalid='call', over='ignore',
+ divide='ignore', under='ignore'):
+ invalid_call_errmask = geterrobj()[1]
+
+ return [bufsize, invalid_call_errmask, None]
+
+# Dealing with errors in _umath_linalg
+_linalg_error_extobj = _determine_error_states()
+del _determine_error_states
+
+def _raise_linalgerror_singular(err, flag):
+ raise LinAlgError("Singular matrix")
+
+def _raise_linalgerror_nonposdef(err, flag):
+ raise LinAlgError("Matrix is not positive definite")
+
+def _raise_linalgerror_eigenvalues_nonconvergence(err, flag):
+ raise LinAlgError("Eigenvalues did not converge")
+
+def _raise_linalgerror_svd_nonconvergence(err, flag):
+ raise LinAlgError("SVD did not converge")
+
+def _raise_linalgerror_lstsq(err, flag):
+ raise LinAlgError("SVD did not converge in Linear Least Squares")
+
+def _raise_linalgerror_qr(err, flag):
+ raise LinAlgError("Incorrect argument found while performing "
+ "QR factorization")
+
+def get_linalg_error_extobj(callback):
+ extobj = list(_linalg_error_extobj) # make a copy
+ extobj[2] = callback
+ return extobj
+
+def _makearray(a):
+ new = asarray(a)
+ wrap = getattr(a, "__array_prepare__", new.__array_wrap__)
+ return new, wrap
+
+def isComplexType(t):
+ return issubclass(t, complexfloating)
+
+_real_types_map = {single : single,
+ double : double,
+ csingle : single,
+ cdouble : double}
+
+_complex_types_map = {single : csingle,
+ double : cdouble,
+ csingle : csingle,
+ cdouble : cdouble}
+
+def _realType(t, default=double):
+ return _real_types_map.get(t, default)
+
+def _complexType(t, default=cdouble):
+ return _complex_types_map.get(t, default)
+
+def _commonType(*arrays):
+ # in lite version, use higher precision (always double or cdouble)
+ result_type = single
+ is_complex = False
+ for a in arrays:
+ type_ = a.dtype.type
+ if issubclass(type_, inexact):
+ if isComplexType(type_):
+ is_complex = True
+ rt = _realType(type_, default=None)
+ if rt is double:
+ result_type = double
+ elif rt is None:
+ # unsupported inexact scalar
+ raise TypeError("array type %s is unsupported in linalg" %
+ (a.dtype.name,))
+ else:
+ result_type = double
+ if is_complex:
+ result_type = _complex_types_map[result_type]
+ return cdouble, result_type
+ else:
+ return double, result_type
+
+
+def _to_native_byte_order(*arrays):
+ ret = []
+ for arr in arrays:
+ if arr.dtype.byteorder not in ('=', '|'):
+ ret.append(asarray(arr, dtype=arr.dtype.newbyteorder('=')))
+ else:
+ ret.append(arr)
+ if len(ret) == 1:
+ return ret[0]
+ else:
+ return ret
+
+
+def _assert_2d(*arrays):
+ for a in arrays:
+ if a.ndim != 2:
+ raise LinAlgError('%d-dimensional array given. Array must be '
+ 'two-dimensional' % a.ndim)
+
+def _assert_stacked_2d(*arrays):
+ for a in arrays:
+ if a.ndim < 2:
+ raise LinAlgError('%d-dimensional array given. Array must be '
+ 'at least two-dimensional' % a.ndim)
+
+def _assert_stacked_square(*arrays):
+ for a in arrays:
+ m, n = a.shape[-2:]
+ if m != n:
+ raise LinAlgError('Last 2 dimensions of the array must be square')
+
+def _assert_finite(*arrays):
+ for a in arrays:
+ if not isfinite(a).all():
+ raise LinAlgError("Array must not contain infs or NaNs")
+
+def _is_empty_2d(arr):
+ # check size first for efficiency
+ return arr.size == 0 and prod(arr.shape[-2:]) == 0
+
+
+def transpose(a):
+ """
+ Transpose each matrix in a stack of matrices.
+
+ Unlike np.transpose, this only swaps the last two axes, rather than all of
+ them
+
+ Parameters
+ ----------
+ a : (...,M,N) array_like
+
+ Returns
+ -------
+ aT : (...,N,M) ndarray
+ """
+ return swapaxes(a, -1, -2)
+
+# Linear equations
+
+def _tensorsolve_dispatcher(a, b, axes=None):
+ return (a, b)
+
+
+@array_function_dispatch(_tensorsolve_dispatcher)
+def tensorsolve(a, b, axes=None):
+ """
+ Solve the tensor equation ``a x = b`` for x.
+
+ It is assumed that all indices of `x` are summed over in the product,
+ together with the rightmost indices of `a`, as is done in, for example,
+ ``tensordot(a, x, axes=x.ndim)``.
+
+ Parameters
+ ----------
+ a : array_like
+ Coefficient tensor, of shape ``b.shape + Q``. `Q`, a tuple, equals
+ the shape of that sub-tensor of `a` consisting of the appropriate
+ number of its rightmost indices, and must be such that
+ ``prod(Q) == prod(b.shape)`` (in which sense `a` is said to be
+ 'square').
+ b : array_like
+ Right-hand tensor, which can be of any shape.
+ axes : tuple of ints, optional
+ Axes in `a` to reorder to the right, before inversion.
+ If None (default), no reordering is done.
+
+ Returns
+ -------
+ x : ndarray, shape Q
+
+ Raises
+ ------
+ LinAlgError
+ If `a` is singular or not 'square' (in the above sense).
+
+ See Also
+ --------
+ numpy.tensordot, tensorinv, numpy.einsum
+
+ Examples
+ --------
+ >>> a = np.eye(2*3*4)
+ >>> a.shape = (2*3, 4, 2, 3, 4)
+ >>> b = np.random.randn(2*3, 4)
+ >>> x = np.linalg.tensorsolve(a, b)
+ >>> x.shape
+ (2, 3, 4)
+ >>> np.allclose(np.tensordot(a, x, axes=3), b)
+ True
+
+ """
+ a, wrap = _makearray(a)
+ b = asarray(b)
+ an = a.ndim
+
+ if axes is not None:
+ allaxes = list(range(0, an))
+ for k in axes:
+ allaxes.remove(k)
+ allaxes.insert(an, k)
+ a = a.transpose(allaxes)
+
+ oldshape = a.shape[-(an-b.ndim):]
+ prod = 1
+ for k in oldshape:
+ prod *= k
+
+ if a.size != prod ** 2:
+ raise LinAlgError(
+ "Input arrays must satisfy the requirement \
+ prod(a.shape[b.ndim:]) == prod(a.shape[:b.ndim])"
+ )
+
+ a = a.reshape(prod, prod)
+ b = b.ravel()
+ res = wrap(solve(a, b))
+ res.shape = oldshape
+ return res
+
+
+def _solve_dispatcher(a, b):
+ return (a, b)
+
+
+@array_function_dispatch(_solve_dispatcher)
+def solve(a, b):
+ """
+ Solve a linear matrix equation, or system of linear scalar equations.
+
+ Computes the "exact" solution, `x`, of the well-determined, i.e., full
+ rank, linear matrix equation `ax = b`.
+
+ Parameters
+ ----------
+ a : (..., M, M) array_like
+ Coefficient matrix.
+ b : {(..., M,), (..., M, K)}, array_like
+ Ordinate or "dependent variable" values.
+
+ Returns
+ -------
+ x : {(..., M,), (..., M, K)} ndarray
+ Solution to the system a x = b. Returned shape is identical to `b`.
+
+ Raises
+ ------
+ LinAlgError
+ If `a` is singular or not square.
+
+ See Also
+ --------
+ scipy.linalg.solve : Similar function in SciPy.
+
+ Notes
+ -----
+
+ .. versionadded:: 1.8.0
+
+ Broadcasting rules apply, see the `numpy.linalg` documentation for
+ details.
+
+ The solutions are computed using LAPACK routine ``_gesv``.
+
+ `a` must be square and of full-rank, i.e., all rows (or, equivalently,
+ columns) must be linearly independent; if either is not true, use
+ `lstsq` for the least-squares best "solution" of the
+ system/equation.
+
+ References
+ ----------
+ .. [1] G. Strang, *Linear Algebra and Its Applications*, 2nd Ed., Orlando,
+ FL, Academic Press, Inc., 1980, pg. 22.
+
+ Examples
+ --------
+ Solve the system of equations ``x0 + 2 * x1 = 1`` and ``3 * x0 + 5 * x1 = 2``:
+
+ >>> a = np.array([[1, 2], [3, 5]])
+ >>> b = np.array([1, 2])
+ >>> x = np.linalg.solve(a, b)
+ >>> x
+ array([-1., 1.])
+
+ Check that the solution is correct:
+
+ >>> np.allclose(np.dot(a, x), b)
+ True
+
+ """
+ a, _ = _makearray(a)
+ _assert_stacked_2d(a)
+ _assert_stacked_square(a)
+ b, wrap = _makearray(b)
+ t, result_t = _commonType(a, b)
+
+ # We use the b = (..., M,) logic, only if the number of extra dimensions
+ # match exactly
+ if b.ndim == a.ndim - 1:
+ gufunc = _umath_linalg.solve1
+ else:
+ gufunc = _umath_linalg.solve
+
+ signature = 'DD->D' if isComplexType(t) else 'dd->d'
+ extobj = get_linalg_error_extobj(_raise_linalgerror_singular)
+ r = gufunc(a, b, signature=signature, extobj=extobj)
+
+ return wrap(r.astype(result_t, copy=False))
+
+
+def _tensorinv_dispatcher(a, ind=None):
+ return (a,)
+
+
+@array_function_dispatch(_tensorinv_dispatcher)
+def tensorinv(a, ind=2):
+ """
+ Compute the 'inverse' of an N-dimensional array.
+
+ The result is an inverse for `a` relative to the tensordot operation
+ ``tensordot(a, b, ind)``, i. e., up to floating-point accuracy,
+ ``tensordot(tensorinv(a), a, ind)`` is the "identity" tensor for the
+ tensordot operation.
+
+ Parameters
+ ----------
+ a : array_like
+ Tensor to 'invert'. Its shape must be 'square', i. e.,
+ ``prod(a.shape[:ind]) == prod(a.shape[ind:])``.
+ ind : int, optional
+ Number of first indices that are involved in the inverse sum.
+ Must be a positive integer, default is 2.
+
+ Returns
+ -------
+ b : ndarray
+ `a`'s tensordot inverse, shape ``a.shape[ind:] + a.shape[:ind]``.
+
+ Raises
+ ------
+ LinAlgError
+ If `a` is singular or not 'square' (in the above sense).
+
+ See Also
+ --------
+ numpy.tensordot, tensorsolve
+
+ Examples
+ --------
+ >>> a = np.eye(4*6)
+ >>> a.shape = (4, 6, 8, 3)
+ >>> ainv = np.linalg.tensorinv(a, ind=2)
+ >>> ainv.shape
+ (8, 3, 4, 6)
+ >>> b = np.random.randn(4, 6)
+ >>> np.allclose(np.tensordot(ainv, b), np.linalg.tensorsolve(a, b))
+ True
+
+ >>> a = np.eye(4*6)
+ >>> a.shape = (24, 8, 3)
+ >>> ainv = np.linalg.tensorinv(a, ind=1)
+ >>> ainv.shape
+ (8, 3, 24)
+ >>> b = np.random.randn(24)
+ >>> np.allclose(np.tensordot(ainv, b, 1), np.linalg.tensorsolve(a, b))
+ True
+
+ """
+ a = asarray(a)
+ oldshape = a.shape
+ prod = 1
+ if ind > 0:
+ invshape = oldshape[ind:] + oldshape[:ind]
+ for k in oldshape[ind:]:
+ prod *= k
+ else:
+ raise ValueError("Invalid ind argument.")
+ a = a.reshape(prod, -1)
+ ia = inv(a)
+ return ia.reshape(*invshape)
+
+
+# Matrix inversion
+
+def _unary_dispatcher(a):
+ return (a,)
+
+
+@array_function_dispatch(_unary_dispatcher)
+def inv(a):
+ """
+ Compute the (multiplicative) inverse of a matrix.
+
+ Given a square matrix `a`, return the matrix `ainv` satisfying
+ ``dot(a, ainv) = dot(ainv, a) = eye(a.shape[0])``.
+
+ Parameters
+ ----------
+ a : (..., M, M) array_like
+ Matrix to be inverted.
+
+ Returns
+ -------
+ ainv : (..., M, M) ndarray or matrix
+ (Multiplicative) inverse of the matrix `a`.
+
+ Raises
+ ------
+ LinAlgError
+ If `a` is not square or inversion fails.
+
+ See Also
+ --------
+ scipy.linalg.inv : Similar function in SciPy.
+
+ Notes
+ -----
+
+ .. versionadded:: 1.8.0
+
+ Broadcasting rules apply, see the `numpy.linalg` documentation for
+ details.
+
+ Examples
+ --------
+ >>> from numpy.linalg import inv
+ >>> a = np.array([[1., 2.], [3., 4.]])
+ >>> ainv = inv(a)
+ >>> np.allclose(np.dot(a, ainv), np.eye(2))
+ True
+ >>> np.allclose(np.dot(ainv, a), np.eye(2))
+ True
+
+ If a is a matrix object, then the return value is a matrix as well:
+
+ >>> ainv = inv(np.matrix(a))
+ >>> ainv
+ matrix([[-2. , 1. ],
+ [ 1.5, -0.5]])
+
+ Inverses of several matrices can be computed at once:
+
+ >>> a = np.array([[[1., 2.], [3., 4.]], [[1, 3], [3, 5]]])
+ >>> inv(a)
+ array([[[-2. , 1. ],
+ [ 1.5 , -0.5 ]],
+ [[-1.25, 0.75],
+ [ 0.75, -0.25]]])
+
+ """
+ a, wrap = _makearray(a)
+ _assert_stacked_2d(a)
+ _assert_stacked_square(a)
+ t, result_t = _commonType(a)
+
+ signature = 'D->D' if isComplexType(t) else 'd->d'
+ extobj = get_linalg_error_extobj(_raise_linalgerror_singular)
+ ainv = _umath_linalg.inv(a, signature=signature, extobj=extobj)
+ return wrap(ainv.astype(result_t, copy=False))
+
+
+def _matrix_power_dispatcher(a, n):
+ return (a,)
+
+
+@array_function_dispatch(_matrix_power_dispatcher)
+def matrix_power(a, n):
+ """
+ Raise a square matrix to the (integer) power `n`.
+
+ For positive integers `n`, the power is computed by repeated matrix
+ squarings and matrix multiplications. If ``n == 0``, the identity matrix
+ of the same shape as M is returned. If ``n < 0``, the inverse
+ is computed and then raised to the ``abs(n)``.
+
+ .. note:: Stacks of object matrices are not currently supported.
+
+ Parameters
+ ----------
+ a : (..., M, M) array_like
+ Matrix to be "powered".
+ n : int
+ The exponent can be any integer or long integer, positive,
+ negative, or zero.
+
+ Returns
+ -------
+ a**n : (..., M, M) ndarray or matrix object
+ The return value is the same shape and type as `M`;
+ if the exponent is positive or zero then the type of the
+ elements is the same as those of `M`. If the exponent is
+ negative the elements are floating-point.
+
+ Raises
+ ------
+ LinAlgError
+ For matrices that are not square or that (for negative powers) cannot
+ be inverted numerically.
+
+ Examples
+ --------
+ >>> from numpy.linalg import matrix_power
+ >>> i = np.array([[0, 1], [-1, 0]]) # matrix equiv. of the imaginary unit
+ >>> matrix_power(i, 3) # should = -i
+ array([[ 0, -1],
+ [ 1, 0]])
+ >>> matrix_power(i, 0)
+ array([[1, 0],
+ [0, 1]])
+ >>> matrix_power(i, -3) # should = 1/(-i) = i, but w/ f.p. elements
+ array([[ 0., 1.],
+ [-1., 0.]])
+
+ Somewhat more sophisticated example
+
+ >>> q = np.zeros((4, 4))
+ >>> q[0:2, 0:2] = -i
+ >>> q[2:4, 2:4] = i
+ >>> q # one of the three quaternion units not equal to 1
+ array([[ 0., -1., 0., 0.],
+ [ 1., 0., 0., 0.],
+ [ 0., 0., 0., 1.],
+ [ 0., 0., -1., 0.]])
+ >>> matrix_power(q, 2) # = -np.eye(4)
+ array([[-1., 0., 0., 0.],
+ [ 0., -1., 0., 0.],
+ [ 0., 0., -1., 0.],
+ [ 0., 0., 0., -1.]])
+
+ """
+ a = asanyarray(a)
+ _assert_stacked_2d(a)
+ _assert_stacked_square(a)
+
+ try:
+ n = operator.index(n)
+ except TypeError as e:
+ raise TypeError("exponent must be an integer") from e
+
+ # Fall back on dot for object arrays. Object arrays are not supported by
+ # the current implementation of matmul using einsum
+ if a.dtype != object:
+ fmatmul = matmul
+ elif a.ndim == 2:
+ fmatmul = dot
+ else:
+ raise NotImplementedError(
+ "matrix_power not supported for stacks of object arrays")
+
+ if n == 0:
+ a = empty_like(a)
+ a[...] = eye(a.shape[-2], dtype=a.dtype)
+ return a
+
+ elif n < 0:
+ a = inv(a)
+ n = abs(n)
+
+ # short-cuts.
+ if n == 1:
+ return a
+
+ elif n == 2:
+ return fmatmul(a, a)
+
+ elif n == 3:
+ return fmatmul(fmatmul(a, a), a)
+
+ # Use binary decomposition to reduce the number of matrix multiplications.
+ # Here, we iterate over the bits of n, from LSB to MSB, raise `a` to
+ # increasing powers of 2, and multiply into the result as needed.
+ z = result = None
+ while n > 0:
+ z = a if z is None else fmatmul(z, z)
+ n, bit = divmod(n, 2)
+ if bit:
+ result = z if result is None else fmatmul(result, z)
+
+ return result
+
+
+# Cholesky decomposition
+
+
+@array_function_dispatch(_unary_dispatcher)
+def cholesky(a):
+ """
+ Cholesky decomposition.
+
+ Return the Cholesky decomposition, `L * L.H`, of the square matrix `a`,
+ where `L` is lower-triangular and .H is the conjugate transpose operator
+ (which is the ordinary transpose if `a` is real-valued). `a` must be
+ Hermitian (symmetric if real-valued) and positive-definite. No
+ checking is performed to verify whether `a` is Hermitian or not.
+ In addition, only the lower-triangular and diagonal elements of `a`
+ are used. Only `L` is actually returned.
+
+ Parameters
+ ----------
+ a : (..., M, M) array_like
+ Hermitian (symmetric if all elements are real), positive-definite
+ input matrix.
+
+ Returns
+ -------
+ L : (..., M, M) array_like
+ Lower-triangular Cholesky factor of `a`. Returns a matrix object if
+ `a` is a matrix object.
+
+ Raises
+ ------
+ LinAlgError
+ If the decomposition fails, for example, if `a` is not
+ positive-definite.
+
+ See Also
+ --------
+ scipy.linalg.cholesky : Similar function in SciPy.
+ scipy.linalg.cholesky_banded : Cholesky decompose a banded Hermitian
+ positive-definite matrix.
+ scipy.linalg.cho_factor : Cholesky decomposition of a matrix, to use in
+ `scipy.linalg.cho_solve`.
+
+ Notes
+ -----
+
+ .. versionadded:: 1.8.0
+
+ Broadcasting rules apply, see the `numpy.linalg` documentation for
+ details.
+
+ The Cholesky decomposition is often used as a fast way of solving
+
+ .. math:: A \\mathbf{x} = \\mathbf{b}
+
+ (when `A` is both Hermitian/symmetric and positive-definite).
+
+ First, we solve for :math:`\\mathbf{y}` in
+
+ .. math:: L \\mathbf{y} = \\mathbf{b},
+
+ and then for :math:`\\mathbf{x}` in
+
+ .. math:: L.H \\mathbf{x} = \\mathbf{y}.
+
+ Examples
+ --------
+ >>> A = np.array([[1,-2j],[2j,5]])
+ >>> A
+ array([[ 1.+0.j, -0.-2.j],
+ [ 0.+2.j, 5.+0.j]])
+ >>> L = np.linalg.cholesky(A)
+ >>> L
+ array([[1.+0.j, 0.+0.j],
+ [0.+2.j, 1.+0.j]])
+ >>> np.dot(L, L.T.conj()) # verify that L * L.H = A
+ array([[1.+0.j, 0.-2.j],
+ [0.+2.j, 5.+0.j]])
+ >>> A = [[1,-2j],[2j,5]] # what happens if A is only array_like?
+ >>> np.linalg.cholesky(A) # an ndarray object is returned
+ array([[1.+0.j, 0.+0.j],
+ [0.+2.j, 1.+0.j]])
+ >>> # But a matrix object is returned if A is a matrix object
+ >>> np.linalg.cholesky(np.matrix(A))
+ matrix([[ 1.+0.j, 0.+0.j],
+ [ 0.+2.j, 1.+0.j]])
+
+ """
+ extobj = get_linalg_error_extobj(_raise_linalgerror_nonposdef)
+ gufunc = _umath_linalg.cholesky_lo
+ a, wrap = _makearray(a)
+ _assert_stacked_2d(a)
+ _assert_stacked_square(a)
+ t, result_t = _commonType(a)
+ signature = 'D->D' if isComplexType(t) else 'd->d'
+ r = gufunc(a, signature=signature, extobj=extobj)
+ return wrap(r.astype(result_t, copy=False))
+
+
+# QR decomposition
+
+def _qr_dispatcher(a, mode=None):
+ return (a,)
+
+
+@array_function_dispatch(_qr_dispatcher)
+def qr(a, mode='reduced'):
+ """
+ Compute the qr factorization of a matrix.
+
+ Factor the matrix `a` as *qr*, where `q` is orthonormal and `r` is
+ upper-triangular.
+
+ Parameters
+ ----------
+ a : array_like, shape (..., M, N)
+ An array-like object with the dimensionality of at least 2.
+ mode : {'reduced', 'complete', 'r', 'raw'}, optional
+ If K = min(M, N), then
+
+ * 'reduced' : returns Q, R with dimensions (..., M, K), (..., K, N) (default)
+ * 'complete' : returns Q, R with dimensions (..., M, M), (..., M, N)
+ * 'r' : returns R only with dimensions (..., K, N)
+ * 'raw' : returns h, tau with dimensions (..., N, M), (..., K,)
+
+ The options 'reduced', 'complete, and 'raw' are new in numpy 1.8,
+ see the notes for more information. The default is 'reduced', and to
+ maintain backward compatibility with earlier versions of numpy both
+ it and the old default 'full' can be omitted. Note that array h
+ returned in 'raw' mode is transposed for calling Fortran. The
+ 'economic' mode is deprecated. The modes 'full' and 'economic' may
+ be passed using only the first letter for backwards compatibility,
+ but all others must be spelled out. See the Notes for more
+ explanation.
+
+
+ Returns
+ -------
+ When mode is 'reduced' or 'complete', the result will be a namedtuple with
+ the attributes `Q` and `R`.
+
+ Q : ndarray of float or complex, optional
+ A matrix with orthonormal columns. When mode = 'complete' the
+ result is an orthogonal/unitary matrix depending on whether or not
+ a is real/complex. The determinant may be either +/- 1 in that
+ case. In case the number of dimensions in the input array is
+ greater than 2 then a stack of the matrices with above properties
+ is returned.
+ R : ndarray of float or complex, optional
+ The upper-triangular matrix or a stack of upper-triangular
+ matrices if the number of dimensions in the input array is greater
+ than 2.
+ (h, tau) : ndarrays of np.double or np.cdouble, optional
+ The array h contains the Householder reflectors that generate q
+ along with r. The tau array contains scaling factors for the
+ reflectors. In the deprecated 'economic' mode only h is returned.
+
+ Raises
+ ------
+ LinAlgError
+ If factoring fails.
+
+ See Also
+ --------
+ scipy.linalg.qr : Similar function in SciPy.
+ scipy.linalg.rq : Compute RQ decomposition of a matrix.
+
+ Notes
+ -----
+ This is an interface to the LAPACK routines ``dgeqrf``, ``zgeqrf``,
+ ``dorgqr``, and ``zungqr``.
+
+ For more information on the qr factorization, see for example:
+ https://en.wikipedia.org/wiki/QR_factorization
+
+ Subclasses of `ndarray` are preserved except for the 'raw' mode. So if
+ `a` is of type `matrix`, all the return values will be matrices too.
+
+ New 'reduced', 'complete', and 'raw' options for mode were added in
+ NumPy 1.8.0 and the old option 'full' was made an alias of 'reduced'. In
+ addition the options 'full' and 'economic' were deprecated. Because
+ 'full' was the previous default and 'reduced' is the new default,
+ backward compatibility can be maintained by letting `mode` default.
+ The 'raw' option was added so that LAPACK routines that can multiply
+ arrays by q using the Householder reflectors can be used. Note that in
+ this case the returned arrays are of type np.double or np.cdouble and
+ the h array is transposed to be FORTRAN compatible. No routines using
+ the 'raw' return are currently exposed by numpy, but some are available
+ in lapack_lite and just await the necessary work.
+
+ Examples
+ --------
+ >>> a = np.random.randn(9, 6)
+ >>> Q, R = np.linalg.qr(a)
+ >>> np.allclose(a, np.dot(Q, R)) # a does equal QR
+ True
+ >>> R2 = np.linalg.qr(a, mode='r')
+ >>> np.allclose(R, R2) # mode='r' returns the same R as mode='full'
+ True
+ >>> a = np.random.normal(size=(3, 2, 2)) # Stack of 2 x 2 matrices as input
+ >>> Q, R = np.linalg.qr(a)
+ >>> Q.shape
+ (3, 2, 2)
+ >>> R.shape
+ (3, 2, 2)
+ >>> np.allclose(a, np.matmul(Q, R))
+ True
+
+ Example illustrating a common use of `qr`: solving of least squares
+ problems
+
+ What are the least-squares-best `m` and `y0` in ``y = y0 + mx`` for
+ the following data: {(0,1), (1,0), (1,2), (2,1)}. (Graph the points
+ and you'll see that it should be y0 = 0, m = 1.) The answer is provided
+ by solving the over-determined matrix equation ``Ax = b``, where::
+
+ A = array([[0, 1], [1, 1], [1, 1], [2, 1]])
+ x = array([[y0], [m]])
+ b = array([[1], [0], [2], [1]])
+
+ If A = QR such that Q is orthonormal (which is always possible via
+ Gram-Schmidt), then ``x = inv(R) * (Q.T) * b``. (In numpy practice,
+ however, we simply use `lstsq`.)
+
+ >>> A = np.array([[0, 1], [1, 1], [1, 1], [2, 1]])
+ >>> A
+ array([[0, 1],
+ [1, 1],
+ [1, 1],
+ [2, 1]])
+ >>> b = np.array([1, 2, 2, 3])
+ >>> Q, R = np.linalg.qr(A)
+ >>> p = np.dot(Q.T, b)
+ >>> np.dot(np.linalg.inv(R), p)
+ array([ 1., 1.])
+
+ """
+ if mode not in ('reduced', 'complete', 'r', 'raw'):
+ if mode in ('f', 'full'):
+ # 2013-04-01, 1.8
+ msg = "".join((
+ "The 'full' option is deprecated in favor of 'reduced'.\n",
+ "For backward compatibility let mode default."))
+ warnings.warn(msg, DeprecationWarning, stacklevel=2)
+ mode = 'reduced'
+ elif mode in ('e', 'economic'):
+ # 2013-04-01, 1.8
+ msg = "The 'economic' option is deprecated."
+ warnings.warn(msg, DeprecationWarning, stacklevel=2)
+ mode = 'economic'
+ else:
+ raise ValueError(f"Unrecognized mode '{mode}'")
+
+ a, wrap = _makearray(a)
+ _assert_stacked_2d(a)
+ m, n = a.shape[-2:]
+ t, result_t = _commonType(a)
+ a = a.astype(t, copy=True)
+ a = _to_native_byte_order(a)
+ mn = min(m, n)
+
+ if m <= n:
+ gufunc = _umath_linalg.qr_r_raw_m
+ else:
+ gufunc = _umath_linalg.qr_r_raw_n
+
+ signature = 'D->D' if isComplexType(t) else 'd->d'
+ extobj = get_linalg_error_extobj(_raise_linalgerror_qr)
+ tau = gufunc(a, signature=signature, extobj=extobj)
+
+ # handle modes that don't return q
+ if mode == 'r':
+ r = triu(a[..., :mn, :])
+ r = r.astype(result_t, copy=False)
+ return wrap(r)
+
+ if mode == 'raw':
+ q = transpose(a)
+ q = q.astype(result_t, copy=False)
+ tau = tau.astype(result_t, copy=False)
+ return wrap(q), tau
+
+ if mode == 'economic':
+ a = a.astype(result_t, copy=False)
+ return wrap(a)
+
+ # mc is the number of columns in the resulting q
+ # matrix. If the mode is complete then it is
+ # same as number of rows, and if the mode is reduced,
+ # then it is the minimum of number of rows and columns.
+ if mode == 'complete' and m > n:
+ mc = m
+ gufunc = _umath_linalg.qr_complete
+ else:
+ mc = mn
+ gufunc = _umath_linalg.qr_reduced
+
+ signature = 'DD->D' if isComplexType(t) else 'dd->d'
+ extobj = get_linalg_error_extobj(_raise_linalgerror_qr)
+ q = gufunc(a, tau, signature=signature, extobj=extobj)
+ r = triu(a[..., :mc, :])
+
+ q = q.astype(result_t, copy=False)
+ r = r.astype(result_t, copy=False)
+
+ return QRResult(wrap(q), wrap(r))
+
+# Eigenvalues
+
+
+@array_function_dispatch(_unary_dispatcher)
+def eigvals(a):
+ """
+ Compute the eigenvalues of a general matrix.
+
+ Main difference between `eigvals` and `eig`: the eigenvectors aren't
+ returned.
+
+ Parameters
+ ----------
+ a : (..., M, M) array_like
+ A complex- or real-valued matrix whose eigenvalues will be computed.
+
+ Returns
+ -------
+ w : (..., M,) ndarray
+ The eigenvalues, each repeated according to its multiplicity.
+ They are not necessarily ordered, nor are they necessarily
+ real for real matrices.
+
+ Raises
+ ------
+ LinAlgError
+ If the eigenvalue computation does not converge.
+
+ See Also
+ --------
+ eig : eigenvalues and right eigenvectors of general arrays
+ eigvalsh : eigenvalues of real symmetric or complex Hermitian
+ (conjugate symmetric) arrays.
+ eigh : eigenvalues and eigenvectors of real symmetric or complex
+ Hermitian (conjugate symmetric) arrays.
+ scipy.linalg.eigvals : Similar function in SciPy.
+
+ Notes
+ -----
+
+ .. versionadded:: 1.8.0
+
+ Broadcasting rules apply, see the `numpy.linalg` documentation for
+ details.
+
+ This is implemented using the ``_geev`` LAPACK routines which compute
+ the eigenvalues and eigenvectors of general square arrays.
+
+ Examples
+ --------
+ Illustration, using the fact that the eigenvalues of a diagonal matrix
+ are its diagonal elements, that multiplying a matrix on the left
+ by an orthogonal matrix, `Q`, and on the right by `Q.T` (the transpose
+ of `Q`), preserves the eigenvalues of the "middle" matrix. In other words,
+ if `Q` is orthogonal, then ``Q * A * Q.T`` has the same eigenvalues as
+ ``A``:
+
+ >>> from numpy import linalg as LA
+ >>> x = np.random.random()
+ >>> Q = np.array([[np.cos(x), -np.sin(x)], [np.sin(x), np.cos(x)]])
+ >>> LA.norm(Q[0, :]), LA.norm(Q[1, :]), np.dot(Q[0, :],Q[1, :])
+ (1.0, 1.0, 0.0)
+
+ Now multiply a diagonal matrix by ``Q`` on one side and by ``Q.T`` on the other:
+
+ >>> D = np.diag((-1,1))
+ >>> LA.eigvals(D)
+ array([-1., 1.])
+ >>> A = np.dot(Q, D)
+ >>> A = np.dot(A, Q.T)
+ >>> LA.eigvals(A)
+ array([ 1., -1.]) # random
+
+ """
+ a, wrap = _makearray(a)
+ _assert_stacked_2d(a)
+ _assert_stacked_square(a)
+ _assert_finite(a)
+ t, result_t = _commonType(a)
+
+ extobj = get_linalg_error_extobj(
+ _raise_linalgerror_eigenvalues_nonconvergence)
+ signature = 'D->D' if isComplexType(t) else 'd->D'
+ w = _umath_linalg.eigvals(a, signature=signature, extobj=extobj)
+
+ if not isComplexType(t):
+ if all(w.imag == 0):
+ w = w.real
+ result_t = _realType(result_t)
+ else:
+ result_t = _complexType(result_t)
+
+ return w.astype(result_t, copy=False)
+
+
+def _eigvalsh_dispatcher(a, UPLO=None):
+ return (a,)
+
+
+@array_function_dispatch(_eigvalsh_dispatcher)
+def eigvalsh(a, UPLO='L'):
+ """
+ Compute the eigenvalues of a complex Hermitian or real symmetric matrix.
+
+ Main difference from eigh: the eigenvectors are not computed.
+
+ Parameters
+ ----------
+ a : (..., M, M) array_like
+ A complex- or real-valued matrix whose eigenvalues are to be
+ computed.
+ UPLO : {'L', 'U'}, optional
+ Specifies whether the calculation is done with the lower triangular
+ part of `a` ('L', default) or the upper triangular part ('U').
+ Irrespective of this value only the real parts of the diagonal will
+ be considered in the computation to preserve the notion of a Hermitian
+ matrix. It therefore follows that the imaginary part of the diagonal
+ will always be treated as zero.
+
+ Returns
+ -------
+ w : (..., M,) ndarray
+ The eigenvalues in ascending order, each repeated according to
+ its multiplicity.
+
+ Raises
+ ------
+ LinAlgError
+ If the eigenvalue computation does not converge.
+
+ See Also
+ --------
+ eigh : eigenvalues and eigenvectors of real symmetric or complex Hermitian
+ (conjugate symmetric) arrays.
+ eigvals : eigenvalues of general real or complex arrays.
+ eig : eigenvalues and right eigenvectors of general real or complex
+ arrays.
+ scipy.linalg.eigvalsh : Similar function in SciPy.
+
+ Notes
+ -----
+
+ .. versionadded:: 1.8.0
+
+ Broadcasting rules apply, see the `numpy.linalg` documentation for
+ details.
+
+ The eigenvalues are computed using LAPACK routines ``_syevd``, ``_heevd``.
+
+ Examples
+ --------
+ >>> from numpy import linalg as LA
+ >>> a = np.array([[1, -2j], [2j, 5]])
+ >>> LA.eigvalsh(a)
+ array([ 0.17157288, 5.82842712]) # may vary
+
+ >>> # demonstrate the treatment of the imaginary part of the diagonal
+ >>> a = np.array([[5+2j, 9-2j], [0+2j, 2-1j]])
+ >>> a
+ array([[5.+2.j, 9.-2.j],
+ [0.+2.j, 2.-1.j]])
+ >>> # with UPLO='L' this is numerically equivalent to using LA.eigvals()
+ >>> # with:
+ >>> b = np.array([[5.+0.j, 0.-2.j], [0.+2.j, 2.-0.j]])
+ >>> b
+ array([[5.+0.j, 0.-2.j],
+ [0.+2.j, 2.+0.j]])
+ >>> wa = LA.eigvalsh(a)
+ >>> wb = LA.eigvals(b)
+ >>> wa; wb
+ array([1., 6.])
+ array([6.+0.j, 1.+0.j])
+
+ """
+ UPLO = UPLO.upper()
+ if UPLO not in ('L', 'U'):
+ raise ValueError("UPLO argument must be 'L' or 'U'")
+
+ extobj = get_linalg_error_extobj(
+ _raise_linalgerror_eigenvalues_nonconvergence)
+ if UPLO == 'L':
+ gufunc = _umath_linalg.eigvalsh_lo
+ else:
+ gufunc = _umath_linalg.eigvalsh_up
+
+ a, wrap = _makearray(a)
+ _assert_stacked_2d(a)
+ _assert_stacked_square(a)
+ t, result_t = _commonType(a)
+ signature = 'D->d' if isComplexType(t) else 'd->d'
+ w = gufunc(a, signature=signature, extobj=extobj)
+ return w.astype(_realType(result_t), copy=False)
+
+def _convertarray(a):
+ t, result_t = _commonType(a)
+ a = a.astype(t).T.copy()
+ return a, t, result_t
+
+
+# Eigenvectors
+
+
+@array_function_dispatch(_unary_dispatcher)
+def eig(a):
+ """
+ Compute the eigenvalues and right eigenvectors of a square array.
+
+ Parameters
+ ----------
+ a : (..., M, M) array
+ Matrices for which the eigenvalues and right eigenvectors will
+ be computed
+
+ Returns
+ -------
+ A namedtuple with the following attributes:
+
+ eigenvalues : (..., M) array
+ The eigenvalues, each repeated according to its multiplicity.
+ The eigenvalues are not necessarily ordered. The resulting
+ array will be of complex type, unless the imaginary part is
+ zero in which case it will be cast to a real type. When `a`
+ is real the resulting eigenvalues will be real (0 imaginary
+ part) or occur in conjugate pairs
+
+ eigenvectors : (..., M, M) array
+ The normalized (unit "length") eigenvectors, such that the
+ column ``eigenvectors[:,i]`` is the eigenvector corresponding to the
+ eigenvalue ``eigenvalues[i]``.
+
+ Raises
+ ------
+ LinAlgError
+ If the eigenvalue computation does not converge.
+
+ See Also
+ --------
+ eigvals : eigenvalues of a non-symmetric array.
+ eigh : eigenvalues and eigenvectors of a real symmetric or complex
+ Hermitian (conjugate symmetric) array.
+ eigvalsh : eigenvalues of a real symmetric or complex Hermitian
+ (conjugate symmetric) array.
+ scipy.linalg.eig : Similar function in SciPy that also solves the
+ generalized eigenvalue problem.
+ scipy.linalg.schur : Best choice for unitary and other non-Hermitian
+ normal matrices.
+
+ Notes
+ -----
+
+ .. versionadded:: 1.8.0
+
+ Broadcasting rules apply, see the `numpy.linalg` documentation for
+ details.
+
+ This is implemented using the ``_geev`` LAPACK routines which compute
+ the eigenvalues and eigenvectors of general square arrays.
+
+ The number `w` is an eigenvalue of `a` if there exists a vector `v` such
+ that ``a @ v = w * v``. Thus, the arrays `a`, `eigenvalues`, and
+ `eigenvectors` satisfy the equations ``a @ eigenvectors[:,i] =
+ eigenvalues[i] * eigenvalues[:,i]`` for :math:`i \\in \\{0,...,M-1\\}`.
+
+ The array `eigenvectors` may not be of maximum rank, that is, some of the
+ columns may be linearly dependent, although round-off error may obscure
+ that fact. If the eigenvalues are all different, then theoretically the
+ eigenvectors are linearly independent and `a` can be diagonalized by a
+ similarity transformation using `eigenvectors`, i.e, ``inv(eigenvectors) @
+ a @ eigenvectors`` is diagonal.
+
+ For non-Hermitian normal matrices the SciPy function `scipy.linalg.schur`
+ is preferred because the matrix `eigenvectors` is guaranteed to be
+ unitary, which is not the case when using `eig`. The Schur factorization
+ produces an upper triangular matrix rather than a diagonal matrix, but for
+ normal matrices only the diagonal of the upper triangular matrix is
+ needed, the rest is roundoff error.
+
+ Finally, it is emphasized that `eigenvectors` consists of the *right* (as
+ in right-hand side) eigenvectors of `a`. A vector `y` satisfying ``y.T @ a
+ = z * y.T`` for some number `z` is called a *left* eigenvector of `a`,
+ and, in general, the left and right eigenvectors of a matrix are not
+ necessarily the (perhaps conjugate) transposes of each other.
+
+ References
+ ----------
+ G. Strang, *Linear Algebra and Its Applications*, 2nd Ed., Orlando, FL,
+ Academic Press, Inc., 1980, Various pp.
+
+ Examples
+ --------
+ >>> from numpy import linalg as LA
+
+ (Almost) trivial example with real eigenvalues and eigenvectors.
+
+ >>> eigenvalues, eigenvectors = LA.eig(np.diag((1, 2, 3)))
+ >>> eigenvalues
+ array([1., 2., 3.])
+ >>> eigenvectors
+ array([[1., 0., 0.],
+ [0., 1., 0.],
+ [0., 0., 1.]])
+
+ Real matrix possessing complex eigenvalues and eigenvectors; note that the
+ eigenvalues are complex conjugates of each other.
+
+ >>> eigenvalues, eigenvectors = LA.eig(np.array([[1, -1], [1, 1]]))
+ >>> eigenvalues
+ array([1.+1.j, 1.-1.j])
+ >>> eigenvectors
+ array([[0.70710678+0.j , 0.70710678-0.j ],
+ [0. -0.70710678j, 0. +0.70710678j]])
+
+ Complex-valued matrix with real eigenvalues (but complex-valued eigenvectors);
+ note that ``a.conj().T == a``, i.e., `a` is Hermitian.
+
+ >>> a = np.array([[1, 1j], [-1j, 1]])
+ >>> eigenvalues, eigenvectors = LA.eig(a)
+ >>> eigenvalues
+ array([2.+0.j, 0.+0.j])
+ >>> eigenvectors
+ array([[ 0. +0.70710678j, 0.70710678+0.j ], # may vary
+ [ 0.70710678+0.j , -0. +0.70710678j]])
+
+ Be careful about round-off error!
+
+ >>> a = np.array([[1 + 1e-9, 0], [0, 1 - 1e-9]])
+ >>> # Theor. eigenvalues are 1 +/- 1e-9
+ >>> eigenvalues, eigenvectors = LA.eig(a)
+ >>> eigenvalues
+ array([1., 1.])
+ >>> eigenvectors
+ array([[1., 0.],
+ [0., 1.]])
+
+ """
+ a, wrap = _makearray(a)
+ _assert_stacked_2d(a)
+ _assert_stacked_square(a)
+ _assert_finite(a)
+ t, result_t = _commonType(a)
+
+ extobj = get_linalg_error_extobj(
+ _raise_linalgerror_eigenvalues_nonconvergence)
+ signature = 'D->DD' if isComplexType(t) else 'd->DD'
+ w, vt = _umath_linalg.eig(a, signature=signature, extobj=extobj)
+
+ if not isComplexType(t) and all(w.imag == 0.0):
+ w = w.real
+ vt = vt.real
+ result_t = _realType(result_t)
+ else:
+ result_t = _complexType(result_t)
+
+ vt = vt.astype(result_t, copy=False)
+ return EigResult(w.astype(result_t, copy=False), wrap(vt))
+
+
+@array_function_dispatch(_eigvalsh_dispatcher)
+def eigh(a, UPLO='L'):
+ """
+ Return the eigenvalues and eigenvectors of a complex Hermitian
+ (conjugate symmetric) or a real symmetric matrix.
+
+ Returns two objects, a 1-D array containing the eigenvalues of `a`, and
+ a 2-D square array or matrix (depending on the input type) of the
+ corresponding eigenvectors (in columns).
+
+ Parameters
+ ----------
+ a : (..., M, M) array
+ Hermitian or real symmetric matrices whose eigenvalues and
+ eigenvectors are to be computed.
+ UPLO : {'L', 'U'}, optional
+ Specifies whether the calculation is done with the lower triangular
+ part of `a` ('L', default) or the upper triangular part ('U').
+ Irrespective of this value only the real parts of the diagonal will
+ be considered in the computation to preserve the notion of a Hermitian
+ matrix. It therefore follows that the imaginary part of the diagonal
+ will always be treated as zero.
+
+ Returns
+ -------
+ A namedtuple with the following attributes:
+
+ eigenvalues : (..., M) ndarray
+ The eigenvalues in ascending order, each repeated according to
+ its multiplicity.
+ eigenvectors : {(..., M, M) ndarray, (..., M, M) matrix}
+ The column ``eigenvectors[:, i]`` is the normalized eigenvector
+ corresponding to the eigenvalue ``eigenvalues[i]``. Will return a
+ matrix object if `a` is a matrix object.
+
+ Raises
+ ------
+ LinAlgError
+ If the eigenvalue computation does not converge.
+
+ See Also
+ --------
+ eigvalsh : eigenvalues of real symmetric or complex Hermitian
+ (conjugate symmetric) arrays.
+ eig : eigenvalues and right eigenvectors for non-symmetric arrays.
+ eigvals : eigenvalues of non-symmetric arrays.
+ scipy.linalg.eigh : Similar function in SciPy (but also solves the
+ generalized eigenvalue problem).
+
+ Notes
+ -----
+
+ .. versionadded:: 1.8.0
+
+ Broadcasting rules apply, see the `numpy.linalg` documentation for
+ details.
+
+ The eigenvalues/eigenvectors are computed using LAPACK routines ``_syevd``,
+ ``_heevd``.
+
+ The eigenvalues of real symmetric or complex Hermitian matrices are always
+ real. [1]_ The array `eigenvalues` of (column) eigenvectors is unitary and
+ `a`, `eigenvalues`, and `eigenvectors` satisfy the equations ``dot(a,
+ eigenvectors[:, i]) = eigenvalues[i] * eigenvectors[:, i]``.
+
+ References
+ ----------
+ .. [1] G. Strang, *Linear Algebra and Its Applications*, 2nd Ed., Orlando,
+ FL, Academic Press, Inc., 1980, pg. 222.
+
+ Examples
+ --------
+ >>> from numpy import linalg as LA
+ >>> a = np.array([[1, -2j], [2j, 5]])
+ >>> a
+ array([[ 1.+0.j, -0.-2.j],
+ [ 0.+2.j, 5.+0.j]])
+ >>> eigenvalues, eigenvectors = LA.eigh(a)
+ >>> eigenvalues
+ array([0.17157288, 5.82842712])
+ >>> eigenvectors
+ array([[-0.92387953+0.j , -0.38268343+0.j ], # may vary
+ [ 0. +0.38268343j, 0. -0.92387953j]])
+
+ >>> np.dot(a, eigenvectors[:, 0]) - eigenvalues[0] * eigenvectors[:, 0] # verify 1st eigenval/vec pair
+ array([5.55111512e-17+0.0000000e+00j, 0.00000000e+00+1.2490009e-16j])
+ >>> np.dot(a, eigenvectors[:, 1]) - eigenvalues[1] * eigenvectors[:, 1] # verify 2nd eigenval/vec pair
+ array([0.+0.j, 0.+0.j])
+
+ >>> A = np.matrix(a) # what happens if input is a matrix object
+ >>> A
+ matrix([[ 1.+0.j, -0.-2.j],
+ [ 0.+2.j, 5.+0.j]])
+ >>> eigenvalues, eigenvectors = LA.eigh(A)
+ >>> eigenvalues
+ array([0.17157288, 5.82842712])
+ >>> eigenvectors
+ matrix([[-0.92387953+0.j , -0.38268343+0.j ], # may vary
+ [ 0. +0.38268343j, 0. -0.92387953j]])
+
+ >>> # demonstrate the treatment of the imaginary part of the diagonal
+ >>> a = np.array([[5+2j, 9-2j], [0+2j, 2-1j]])
+ >>> a
+ array([[5.+2.j, 9.-2.j],
+ [0.+2.j, 2.-1.j]])
+ >>> # with UPLO='L' this is numerically equivalent to using LA.eig() with:
+ >>> b = np.array([[5.+0.j, 0.-2.j], [0.+2.j, 2.-0.j]])
+ >>> b
+ array([[5.+0.j, 0.-2.j],
+ [0.+2.j, 2.+0.j]])
+ >>> wa, va = LA.eigh(a)
+ >>> wb, vb = LA.eig(b)
+ >>> wa; wb
+ array([1., 6.])
+ array([6.+0.j, 1.+0.j])
+ >>> va; vb
+ array([[-0.4472136 +0.j , -0.89442719+0.j ], # may vary
+ [ 0. +0.89442719j, 0. -0.4472136j ]])
+ array([[ 0.89442719+0.j , -0. +0.4472136j],
+ [-0. +0.4472136j, 0.89442719+0.j ]])
+
+ """
+ UPLO = UPLO.upper()
+ if UPLO not in ('L', 'U'):
+ raise ValueError("UPLO argument must be 'L' or 'U'")
+
+ a, wrap = _makearray(a)
+ _assert_stacked_2d(a)
+ _assert_stacked_square(a)
+ t, result_t = _commonType(a)
+
+ extobj = get_linalg_error_extobj(
+ _raise_linalgerror_eigenvalues_nonconvergence)
+ if UPLO == 'L':
+ gufunc = _umath_linalg.eigh_lo
+ else:
+ gufunc = _umath_linalg.eigh_up
+
+ signature = 'D->dD' if isComplexType(t) else 'd->dd'
+ w, vt = gufunc(a, signature=signature, extobj=extobj)
+ w = w.astype(_realType(result_t), copy=False)
+ vt = vt.astype(result_t, copy=False)
+ return EighResult(w, wrap(vt))
+
+
+# Singular value decomposition
+
+def _svd_dispatcher(a, full_matrices=None, compute_uv=None, hermitian=None):
+ return (a,)
+
+
+@array_function_dispatch(_svd_dispatcher)
+def svd(a, full_matrices=True, compute_uv=True, hermitian=False):
+ """
+ Singular Value Decomposition.
+
+ When `a` is a 2D array, and ``full_matrices=False``, then it is
+ factorized as ``u @ np.diag(s) @ vh = (u * s) @ vh``, where
+ `u` and the Hermitian transpose of `vh` are 2D arrays with
+ orthonormal columns and `s` is a 1D array of `a`'s singular
+ values. When `a` is higher-dimensional, SVD is applied in
+ stacked mode as explained below.
+
+ Parameters
+ ----------
+ a : (..., M, N) array_like
+ A real or complex array with ``a.ndim >= 2``.
+ full_matrices : bool, optional
+ If True (default), `u` and `vh` have the shapes ``(..., M, M)`` and
+ ``(..., N, N)``, respectively. Otherwise, the shapes are
+ ``(..., M, K)`` and ``(..., K, N)``, respectively, where
+ ``K = min(M, N)``.
+ compute_uv : bool, optional
+ Whether or not to compute `u` and `vh` in addition to `s`. True
+ by default.
+ hermitian : bool, optional
+ If True, `a` is assumed to be Hermitian (symmetric if real-valued),
+ enabling a more efficient method for finding singular values.
+ Defaults to False.
+
+ .. versionadded:: 1.17.0
+
+ Returns
+ -------
+ When `compute_uv` is True, the result is a namedtuple with the following
+ attribute names:
+
+ U : { (..., M, M), (..., M, K) } array
+ Unitary array(s). The first ``a.ndim - 2`` dimensions have the same
+ size as those of the input `a`. The size of the last two dimensions
+ depends on the value of `full_matrices`. Only returned when
+ `compute_uv` is True.
+ S : (..., K) array
+ Vector(s) with the singular values, within each vector sorted in
+ descending order. The first ``a.ndim - 2`` dimensions have the same
+ size as those of the input `a`.
+ Vh : { (..., N, N), (..., K, N) } array
+ Unitary array(s). The first ``a.ndim - 2`` dimensions have the same
+ size as those of the input `a`. The size of the last two dimensions
+ depends on the value of `full_matrices`. Only returned when
+ `compute_uv` is True.
+
+ Raises
+ ------
+ LinAlgError
+ If SVD computation does not converge.
+
+ See Also
+ --------
+ scipy.linalg.svd : Similar function in SciPy.
+ scipy.linalg.svdvals : Compute singular values of a matrix.
+
+ Notes
+ -----
+
+ .. versionchanged:: 1.8.0
+ Broadcasting rules apply, see the `numpy.linalg` documentation for
+ details.
+
+ The decomposition is performed using LAPACK routine ``_gesdd``.
+
+ SVD is usually described for the factorization of a 2D matrix :math:`A`.
+ The higher-dimensional case will be discussed below. In the 2D case, SVD is
+ written as :math:`A = U S V^H`, where :math:`A = a`, :math:`U= u`,
+ :math:`S= \\mathtt{np.diag}(s)` and :math:`V^H = vh`. The 1D array `s`
+ contains the singular values of `a` and `u` and `vh` are unitary. The rows
+ of `vh` are the eigenvectors of :math:`A^H A` and the columns of `u` are
+ the eigenvectors of :math:`A A^H`. In both cases the corresponding
+ (possibly non-zero) eigenvalues are given by ``s**2``.
+
+ If `a` has more than two dimensions, then broadcasting rules apply, as
+ explained in :ref:`routines.linalg-broadcasting`. This means that SVD is
+ working in "stacked" mode: it iterates over all indices of the first
+ ``a.ndim - 2`` dimensions and for each combination SVD is applied to the
+ last two indices. The matrix `a` can be reconstructed from the
+ decomposition with either ``(u * s[..., None, :]) @ vh`` or
+ ``u @ (s[..., None] * vh)``. (The ``@`` operator can be replaced by the
+ function ``np.matmul`` for python versions below 3.5.)
+
+ If `a` is a ``matrix`` object (as opposed to an ``ndarray``), then so are
+ all the return values.
+
+ Examples
+ --------
+ >>> a = np.random.randn(9, 6) + 1j*np.random.randn(9, 6)
+ >>> b = np.random.randn(2, 7, 8, 3) + 1j*np.random.randn(2, 7, 8, 3)
+
+ Reconstruction based on full SVD, 2D case:
+
+ >>> U, S, Vh = np.linalg.svd(a, full_matrices=True)
+ >>> U.shape, S.shape, Vh.shape
+ ((9, 9), (6,), (6, 6))
+ >>> np.allclose(a, np.dot(U[:, :6] * S, Vh))
+ True
+ >>> smat = np.zeros((9, 6), dtype=complex)
+ >>> smat[:6, :6] = np.diag(S)
+ >>> np.allclose(a, np.dot(U, np.dot(smat, Vh)))
+ True
+
+ Reconstruction based on reduced SVD, 2D case:
+
+ >>> U, S, Vh = np.linalg.svd(a, full_matrices=False)
+ >>> U.shape, S.shape, Vh.shape
+ ((9, 6), (6,), (6, 6))
+ >>> np.allclose(a, np.dot(U * S, Vh))
+ True
+ >>> smat = np.diag(S)
+ >>> np.allclose(a, np.dot(U, np.dot(smat, Vh)))
+ True
+
+ Reconstruction based on full SVD, 4D case:
+
+ >>> U, S, Vh = np.linalg.svd(b, full_matrices=True)
+ >>> U.shape, S.shape, Vh.shape
+ ((2, 7, 8, 8), (2, 7, 3), (2, 7, 3, 3))
+ >>> np.allclose(b, np.matmul(U[..., :3] * S[..., None, :], Vh))
+ True
+ >>> np.allclose(b, np.matmul(U[..., :3], S[..., None] * Vh))
+ True
+
+ Reconstruction based on reduced SVD, 4D case:
+
+ >>> U, S, Vh = np.linalg.svd(b, full_matrices=False)
+ >>> U.shape, S.shape, Vh.shape
+ ((2, 7, 8, 3), (2, 7, 3), (2, 7, 3, 3))
+ >>> np.allclose(b, np.matmul(U * S[..., None, :], Vh))
+ True
+ >>> np.allclose(b, np.matmul(U, S[..., None] * Vh))
+ True
+
+ """
+ import numpy as _nx
+ a, wrap = _makearray(a)
+
+ if hermitian:
+ # note: lapack svd returns eigenvalues with s ** 2 sorted descending,
+ # but eig returns s sorted ascending, so we re-order the eigenvalues
+ # and related arrays to have the correct order
+ if compute_uv:
+ s, u = eigh(a)
+ sgn = sign(s)
+ s = abs(s)
+ sidx = argsort(s)[..., ::-1]
+ sgn = _nx.take_along_axis(sgn, sidx, axis=-1)
+ s = _nx.take_along_axis(s, sidx, axis=-1)
+ u = _nx.take_along_axis(u, sidx[..., None, :], axis=-1)
+ # singular values are unsigned, move the sign into v
+ vt = transpose(u * sgn[..., None, :]).conjugate()
+ return SVDResult(wrap(u), s, wrap(vt))
+ else:
+ s = eigvalsh(a)
+ s = abs(s)
+ return sort(s)[..., ::-1]
+
+ _assert_stacked_2d(a)
+ t, result_t = _commonType(a)
+
+ extobj = get_linalg_error_extobj(_raise_linalgerror_svd_nonconvergence)
+
+ m, n = a.shape[-2:]
+ if compute_uv:
+ if full_matrices:
+ if m < n:
+ gufunc = _umath_linalg.svd_m_f
+ else:
+ gufunc = _umath_linalg.svd_n_f
+ else:
+ if m < n:
+ gufunc = _umath_linalg.svd_m_s
+ else:
+ gufunc = _umath_linalg.svd_n_s
+
+ signature = 'D->DdD' if isComplexType(t) else 'd->ddd'
+ u, s, vh = gufunc(a, signature=signature, extobj=extobj)
+ u = u.astype(result_t, copy=False)
+ s = s.astype(_realType(result_t), copy=False)
+ vh = vh.astype(result_t, copy=False)
+ return SVDResult(wrap(u), s, wrap(vh))
+ else:
+ if m < n:
+ gufunc = _umath_linalg.svd_m
+ else:
+ gufunc = _umath_linalg.svd_n
+
+ signature = 'D->d' if isComplexType(t) else 'd->d'
+ s = gufunc(a, signature=signature, extobj=extobj)
+ s = s.astype(_realType(result_t), copy=False)
+ return s
+
+
+def _cond_dispatcher(x, p=None):
+ return (x,)
+
+
+@array_function_dispatch(_cond_dispatcher)
+def cond(x, p=None):
+ """
+ Compute the condition number of a matrix.
+
+ This function is capable of returning the condition number using
+ one of seven different norms, depending on the value of `p` (see
+ Parameters below).
+
+ Parameters
+ ----------
+ x : (..., M, N) array_like
+ The matrix whose condition number is sought.
+ p : {None, 1, -1, 2, -2, inf, -inf, 'fro'}, optional
+ Order of the norm used in the condition number computation:
+
+ ===== ============================
+ p norm for matrices
+ ===== ============================
+ None 2-norm, computed directly using the ``SVD``
+ 'fro' Frobenius norm
+ inf max(sum(abs(x), axis=1))
+ -inf min(sum(abs(x), axis=1))
+ 1 max(sum(abs(x), axis=0))
+ -1 min(sum(abs(x), axis=0))
+ 2 2-norm (largest sing. value)
+ -2 smallest singular value
+ ===== ============================
+
+ inf means the `numpy.inf` object, and the Frobenius norm is
+ the root-of-sum-of-squares norm.
+
+ Returns
+ -------
+ c : {float, inf}
+ The condition number of the matrix. May be infinite.
+
+ See Also
+ --------
+ numpy.linalg.norm
+
+ Notes
+ -----
+ The condition number of `x` is defined as the norm of `x` times the
+ norm of the inverse of `x` [1]_; the norm can be the usual L2-norm
+ (root-of-sum-of-squares) or one of a number of other matrix norms.
+
+ References
+ ----------
+ .. [1] G. Strang, *Linear Algebra and Its Applications*, Orlando, FL,
+ Academic Press, Inc., 1980, pg. 285.
+
+ Examples
+ --------
+ >>> from numpy import linalg as LA
+ >>> a = np.array([[1, 0, -1], [0, 1, 0], [1, 0, 1]])
+ >>> a
+ array([[ 1, 0, -1],
+ [ 0, 1, 0],
+ [ 1, 0, 1]])
+ >>> LA.cond(a)
+ 1.4142135623730951
+ >>> LA.cond(a, 'fro')
+ 3.1622776601683795
+ >>> LA.cond(a, np.inf)
+ 2.0
+ >>> LA.cond(a, -np.inf)
+ 1.0
+ >>> LA.cond(a, 1)
+ 2.0
+ >>> LA.cond(a, -1)
+ 1.0
+ >>> LA.cond(a, 2)
+ 1.4142135623730951
+ >>> LA.cond(a, -2)
+ 0.70710678118654746 # may vary
+ >>> min(LA.svd(a, compute_uv=False))*min(LA.svd(LA.inv(a), compute_uv=False))
+ 0.70710678118654746 # may vary
+
+ """
+ x = asarray(x) # in case we have a matrix
+ if _is_empty_2d(x):
+ raise LinAlgError("cond is not defined on empty arrays")
+ if p is None or p == 2 or p == -2:
+ s = svd(x, compute_uv=False)
+ with errstate(all='ignore'):
+ if p == -2:
+ r = s[..., -1] / s[..., 0]
+ else:
+ r = s[..., 0] / s[..., -1]
+ else:
+ # Call inv(x) ignoring errors. The result array will
+ # contain nans in the entries where inversion failed.
+ _assert_stacked_2d(x)
+ _assert_stacked_square(x)
+ t, result_t = _commonType(x)
+ signature = 'D->D' if isComplexType(t) else 'd->d'
+ with errstate(all='ignore'):
+ invx = _umath_linalg.inv(x, signature=signature)
+ r = norm(x, p, axis=(-2, -1)) * norm(invx, p, axis=(-2, -1))
+ r = r.astype(result_t, copy=False)
+
+ # Convert nans to infs unless the original array had nan entries
+ r = asarray(r)
+ nan_mask = isnan(r)
+ if nan_mask.any():
+ nan_mask &= ~isnan(x).any(axis=(-2, -1))
+ if r.ndim > 0:
+ r[nan_mask] = Inf
+ elif nan_mask:
+ r[()] = Inf
+
+ # Convention is to return scalars instead of 0d arrays
+ if r.ndim == 0:
+ r = r[()]
+
+ return r
+
+
+def _matrix_rank_dispatcher(A, tol=None, hermitian=None):
+ return (A,)
+
+
+@array_function_dispatch(_matrix_rank_dispatcher)
+def matrix_rank(A, tol=None, hermitian=False):
+ """
+ Return matrix rank of array using SVD method
+
+ Rank of the array is the number of singular values of the array that are
+ greater than `tol`.
+
+ .. versionchanged:: 1.14
+ Can now operate on stacks of matrices
+
+ Parameters
+ ----------
+ A : {(M,), (..., M, N)} array_like
+ Input vector or stack of matrices.
+ tol : (...) array_like, float, optional
+ Threshold below which SVD values are considered zero. If `tol` is
+ None, and ``S`` is an array with singular values for `M`, and
+ ``eps`` is the epsilon value for datatype of ``S``, then `tol` is
+ set to ``S.max() * max(M, N) * eps``.
+
+ .. versionchanged:: 1.14
+ Broadcasted against the stack of matrices
+ hermitian : bool, optional
+ If True, `A` is assumed to be Hermitian (symmetric if real-valued),
+ enabling a more efficient method for finding singular values.
+ Defaults to False.
+
+ .. versionadded:: 1.14
+
+ Returns
+ -------
+ rank : (...) array_like
+ Rank of A.
+
+ Notes
+ -----
+ The default threshold to detect rank deficiency is a test on the magnitude
+ of the singular values of `A`. By default, we identify singular values less
+ than ``S.max() * max(M, N) * eps`` as indicating rank deficiency (with
+ the symbols defined above). This is the algorithm MATLAB uses [1]. It also
+ appears in *Numerical recipes* in the discussion of SVD solutions for linear
+ least squares [2].
+
+ This default threshold is designed to detect rank deficiency accounting for
+ the numerical errors of the SVD computation. Imagine that there is a column
+ in `A` that is an exact (in floating point) linear combination of other
+ columns in `A`. Computing the SVD on `A` will not produce a singular value
+ exactly equal to 0 in general: any difference of the smallest SVD value from
+ 0 will be caused by numerical imprecision in the calculation of the SVD.
+ Our threshold for small SVD values takes this numerical imprecision into
+ account, and the default threshold will detect such numerical rank
+ deficiency. The threshold may declare a matrix `A` rank deficient even if
+ the linear combination of some columns of `A` is not exactly equal to
+ another column of `A` but only numerically very close to another column of
+ `A`.
+
+ We chose our default threshold because it is in wide use. Other thresholds
+ are possible. For example, elsewhere in the 2007 edition of *Numerical
+ recipes* there is an alternative threshold of ``S.max() *
+ np.finfo(A.dtype).eps / 2. * np.sqrt(m + n + 1.)``. The authors describe
+ this threshold as being based on "expected roundoff error" (p 71).
+
+ The thresholds above deal with floating point roundoff error in the
+ calculation of the SVD. However, you may have more information about the
+ sources of error in `A` that would make you consider other tolerance values
+ to detect *effective* rank deficiency. The most useful measure of the
+ tolerance depends on the operations you intend to use on your matrix. For
+ example, if your data come from uncertain measurements with uncertainties
+ greater than floating point epsilon, choosing a tolerance near that
+ uncertainty may be preferable. The tolerance may be absolute if the
+ uncertainties are absolute rather than relative.
+
+ References
+ ----------
+ .. [1] MATLAB reference documentation, "Rank"
+ https://www.mathworks.com/help/techdoc/ref/rank.html
+ .. [2] W. H. Press, S. A. Teukolsky, W. T. Vetterling and B. P. Flannery,
+ "Numerical Recipes (3rd edition)", Cambridge University Press, 2007,
+ page 795.
+
+ Examples
+ --------
+ >>> from numpy.linalg import matrix_rank
+ >>> matrix_rank(np.eye(4)) # Full rank matrix
+ 4
+ >>> I=np.eye(4); I[-1,-1] = 0. # rank deficient matrix
+ >>> matrix_rank(I)
+ 3
+ >>> matrix_rank(np.ones((4,))) # 1 dimension - rank 1 unless all 0
+ 1
+ >>> matrix_rank(np.zeros((4,)))
+ 0
+ """
+ A = asarray(A)
+ if A.ndim < 2:
+ return int(not all(A==0))
+ S = svd(A, compute_uv=False, hermitian=hermitian)
+ if tol is None:
+ tol = S.max(axis=-1, keepdims=True) * max(A.shape[-2:]) * finfo(S.dtype).eps
+ else:
+ tol = asarray(tol)[..., newaxis]
+ return count_nonzero(S > tol, axis=-1)
+
+
+# Generalized inverse
+
+def _pinv_dispatcher(a, rcond=None, hermitian=None):
+ return (a,)
+
+
+@array_function_dispatch(_pinv_dispatcher)
+def pinv(a, rcond=1e-15, hermitian=False):
+ """
+ Compute the (Moore-Penrose) pseudo-inverse of a matrix.
+
+ Calculate the generalized inverse of a matrix using its
+ singular-value decomposition (SVD) and including all
+ *large* singular values.
+
+ .. versionchanged:: 1.14
+ Can now operate on stacks of matrices
+
+ Parameters
+ ----------
+ a : (..., M, N) array_like
+ Matrix or stack of matrices to be pseudo-inverted.
+ rcond : (...) array_like of float
+ Cutoff for small singular values.
+ Singular values less than or equal to
+ ``rcond * largest_singular_value`` are set to zero.
+ Broadcasts against the stack of matrices.
+ hermitian : bool, optional
+ If True, `a` is assumed to be Hermitian (symmetric if real-valued),
+ enabling a more efficient method for finding singular values.
+ Defaults to False.
+
+ .. versionadded:: 1.17.0
+
+ Returns
+ -------
+ B : (..., N, M) ndarray
+ The pseudo-inverse of `a`. If `a` is a `matrix` instance, then so
+ is `B`.
+
+ Raises
+ ------
+ LinAlgError
+ If the SVD computation does not converge.
+
+ See Also
+ --------
+ scipy.linalg.pinv : Similar function in SciPy.
+ scipy.linalg.pinvh : Compute the (Moore-Penrose) pseudo-inverse of a
+ Hermitian matrix.
+
+ Notes
+ -----
+ The pseudo-inverse of a matrix A, denoted :math:`A^+`, is
+ defined as: "the matrix that 'solves' [the least-squares problem]
+ :math:`Ax = b`," i.e., if :math:`\\bar{x}` is said solution, then
+ :math:`A^+` is that matrix such that :math:`\\bar{x} = A^+b`.
+
+ It can be shown that if :math:`Q_1 \\Sigma Q_2^T = A` is the singular
+ value decomposition of A, then
+ :math:`A^+ = Q_2 \\Sigma^+ Q_1^T`, where :math:`Q_{1,2}` are
+ orthogonal matrices, :math:`\\Sigma` is a diagonal matrix consisting
+ of A's so-called singular values, (followed, typically, by
+ zeros), and then :math:`\\Sigma^+` is simply the diagonal matrix
+ consisting of the reciprocals of A's singular values
+ (again, followed by zeros). [1]_
+
+ References
+ ----------
+ .. [1] G. Strang, *Linear Algebra and Its Applications*, 2nd Ed., Orlando,
+ FL, Academic Press, Inc., 1980, pp. 139-142.
+
+ Examples
+ --------
+ The following example checks that ``a * a+ * a == a`` and
+ ``a+ * a * a+ == a+``:
+
+ >>> a = np.random.randn(9, 6)
+ >>> B = np.linalg.pinv(a)
+ >>> np.allclose(a, np.dot(a, np.dot(B, a)))
+ True
+ >>> np.allclose(B, np.dot(B, np.dot(a, B)))
+ True
+
+ """
+ a, wrap = _makearray(a)
+ rcond = asarray(rcond)
+ if _is_empty_2d(a):
+ m, n = a.shape[-2:]
+ res = empty(a.shape[:-2] + (n, m), dtype=a.dtype)
+ return wrap(res)
+ a = a.conjugate()
+ u, s, vt = svd(a, full_matrices=False, hermitian=hermitian)
+
+ # discard small singular values
+ cutoff = rcond[..., newaxis] * amax(s, axis=-1, keepdims=True)
+ large = s > cutoff
+ s = divide(1, s, where=large, out=s)
+ s[~large] = 0
+
+ res = matmul(transpose(vt), multiply(s[..., newaxis], transpose(u)))
+ return wrap(res)
+
+
+# Determinant
+
+
+@array_function_dispatch(_unary_dispatcher)
+def slogdet(a):
+ """
+ Compute the sign and (natural) logarithm of the determinant of an array.
+
+ If an array has a very small or very large determinant, then a call to
+ `det` may overflow or underflow. This routine is more robust against such
+ issues, because it computes the logarithm of the determinant rather than
+ the determinant itself.
+
+ Parameters
+ ----------
+ a : (..., M, M) array_like
+ Input array, has to be a square 2-D array.
+
+ Returns
+ -------
+ A namedtuple with the following attributes:
+
+ sign : (...) array_like
+ A number representing the sign of the determinant. For a real matrix,
+ this is 1, 0, or -1. For a complex matrix, this is a complex number
+ with absolute value 1 (i.e., it is on the unit circle), or else 0.
+ logabsdet : (...) array_like
+ The natural log of the absolute value of the determinant.
+
+ If the determinant is zero, then `sign` will be 0 and `logabsdet` will be
+ -Inf. In all cases, the determinant is equal to ``sign * np.exp(logabsdet)``.
+
+ See Also
+ --------
+ det
+
+ Notes
+ -----
+
+ .. versionadded:: 1.8.0
+
+ Broadcasting rules apply, see the `numpy.linalg` documentation for
+ details.
+
+ .. versionadded:: 1.6.0
+
+ The determinant is computed via LU factorization using the LAPACK
+ routine ``z/dgetrf``.
+
+
+ Examples
+ --------
+ The determinant of a 2-D array ``[[a, b], [c, d]]`` is ``ad - bc``:
+
+ >>> a = np.array([[1, 2], [3, 4]])
+ >>> (sign, logabsdet) = np.linalg.slogdet(a)
+ >>> (sign, logabsdet)
+ (-1, 0.69314718055994529) # may vary
+ >>> sign * np.exp(logabsdet)
+ -2.0
+
+ Computing log-determinants for a stack of matrices:
+
+ >>> a = np.array([ [[1, 2], [3, 4]], [[1, 2], [2, 1]], [[1, 3], [3, 1]] ])
+ >>> a.shape
+ (3, 2, 2)
+ >>> sign, logabsdet = np.linalg.slogdet(a)
+ >>> (sign, logabsdet)
+ (array([-1., -1., -1.]), array([ 0.69314718, 1.09861229, 2.07944154]))
+ >>> sign * np.exp(logabsdet)
+ array([-2., -3., -8.])
+
+ This routine succeeds where ordinary `det` does not:
+
+ >>> np.linalg.det(np.eye(500) * 0.1)
+ 0.0
+ >>> np.linalg.slogdet(np.eye(500) * 0.1)
+ (1, -1151.2925464970228)
+
+ """
+ a = asarray(a)
+ _assert_stacked_2d(a)
+ _assert_stacked_square(a)
+ t, result_t = _commonType(a)
+ real_t = _realType(result_t)
+ signature = 'D->Dd' if isComplexType(t) else 'd->dd'
+ sign, logdet = _umath_linalg.slogdet(a, signature=signature)
+ sign = sign.astype(result_t, copy=False)
+ logdet = logdet.astype(real_t, copy=False)
+ return SlogdetResult(sign, logdet)
+
+
+@array_function_dispatch(_unary_dispatcher)
+def det(a):
+ """
+ Compute the determinant of an array.
+
+ Parameters
+ ----------
+ a : (..., M, M) array_like
+ Input array to compute determinants for.
+
+ Returns
+ -------
+ det : (...) array_like
+ Determinant of `a`.
+
+ See Also
+ --------
+ slogdet : Another way to represent the determinant, more suitable
+ for large matrices where underflow/overflow may occur.
+ scipy.linalg.det : Similar function in SciPy.
+
+ Notes
+ -----
+
+ .. versionadded:: 1.8.0
+
+ Broadcasting rules apply, see the `numpy.linalg` documentation for
+ details.
+
+ The determinant is computed via LU factorization using the LAPACK
+ routine ``z/dgetrf``.
+
+ Examples
+ --------
+ The determinant of a 2-D array [[a, b], [c, d]] is ad - bc:
+
+ >>> a = np.array([[1, 2], [3, 4]])
+ >>> np.linalg.det(a)
+ -2.0 # may vary
+
+ Computing determinants for a stack of matrices:
+
+ >>> a = np.array([ [[1, 2], [3, 4]], [[1, 2], [2, 1]], [[1, 3], [3, 1]] ])
+ >>> a.shape
+ (3, 2, 2)
+ >>> np.linalg.det(a)
+ array([-2., -3., -8.])
+
+ """
+ a = asarray(a)
+ _assert_stacked_2d(a)
+ _assert_stacked_square(a)
+ t, result_t = _commonType(a)
+ signature = 'D->D' if isComplexType(t) else 'd->d'
+ r = _umath_linalg.det(a, signature=signature)
+ r = r.astype(result_t, copy=False)
+ return r
+
+
+# Linear Least Squares
+
+def _lstsq_dispatcher(a, b, rcond=None):
+ return (a, b)
+
+
+@array_function_dispatch(_lstsq_dispatcher)
+def lstsq(a, b, rcond="warn"):
+ r"""
+ Return the least-squares solution to a linear matrix equation.
+
+ Computes the vector `x` that approximately solves the equation
+ ``a @ x = b``. The equation may be under-, well-, or over-determined
+ (i.e., the number of linearly independent rows of `a` can be less than,
+ equal to, or greater than its number of linearly independent columns).
+ If `a` is square and of full rank, then `x` (but for round-off error)
+ is the "exact" solution of the equation. Else, `x` minimizes the
+ Euclidean 2-norm :math:`||b - ax||`. If there are multiple minimizing
+ solutions, the one with the smallest 2-norm :math:`||x||` is returned.
+
+ Parameters
+ ----------
+ a : (M, N) array_like
+ "Coefficient" matrix.
+ b : {(M,), (M, K)} array_like
+ Ordinate or "dependent variable" values. If `b` is two-dimensional,
+ the least-squares solution is calculated for each of the `K` columns
+ of `b`.
+ rcond : float, optional
+ Cut-off ratio for small singular values of `a`.
+ For the purposes of rank determination, singular values are treated
+ as zero if they are smaller than `rcond` times the largest singular
+ value of `a`.
+
+ .. versionchanged:: 1.14.0
+ If not set, a FutureWarning is given. The previous default
+ of ``-1`` will use the machine precision as `rcond` parameter,
+ the new default will use the machine precision times `max(M, N)`.
+ To silence the warning and use the new default, use ``rcond=None``,
+ to keep using the old behavior, use ``rcond=-1``.
+
+ Returns
+ -------
+ x : {(N,), (N, K)} ndarray
+ Least-squares solution. If `b` is two-dimensional,
+ the solutions are in the `K` columns of `x`.
+ residuals : {(1,), (K,), (0,)} ndarray
+ Sums of squared residuals: Squared Euclidean 2-norm for each column in
+ ``b - a @ x``.
+ If the rank of `a` is < N or M <= N, this is an empty array.
+ If `b` is 1-dimensional, this is a (1,) shape array.
+ Otherwise the shape is (K,).
+ rank : int
+ Rank of matrix `a`.
+ s : (min(M, N),) ndarray
+ Singular values of `a`.
+
+ Raises
+ ------
+ LinAlgError
+ If computation does not converge.
+
+ See Also
+ --------
+ scipy.linalg.lstsq : Similar function in SciPy.
+
+ Notes
+ -----
+ If `b` is a matrix, then all array results are returned as matrices.
+
+ Examples
+ --------
+ Fit a line, ``y = mx + c``, through some noisy data-points:
+
+ >>> x = np.array([0, 1, 2, 3])
+ >>> y = np.array([-1, 0.2, 0.9, 2.1])
+
+ By examining the coefficients, we see that the line should have a
+ gradient of roughly 1 and cut the y-axis at, more or less, -1.
+
+ We can rewrite the line equation as ``y = Ap``, where ``A = [[x 1]]``
+ and ``p = [[m], [c]]``. Now use `lstsq` to solve for `p`:
+
+ >>> A = np.vstack([x, np.ones(len(x))]).T
+ >>> A
+ array([[ 0., 1.],
+ [ 1., 1.],
+ [ 2., 1.],
+ [ 3., 1.]])
+
+ >>> m, c = np.linalg.lstsq(A, y, rcond=None)[0]
+ >>> m, c
+ (1.0 -0.95) # may vary
+
+ Plot the data along with the fitted line:
+
+ >>> import matplotlib.pyplot as plt
+ >>> _ = plt.plot(x, y, 'o', label='Original data', markersize=10)
+ >>> _ = plt.plot(x, m*x + c, 'r', label='Fitted line')
+ >>> _ = plt.legend()
+ >>> plt.show()
+
+ """
+ a, _ = _makearray(a)
+ b, wrap = _makearray(b)
+ is_1d = b.ndim == 1
+ if is_1d:
+ b = b[:, newaxis]
+ _assert_2d(a, b)
+ m, n = a.shape[-2:]
+ m2, n_rhs = b.shape[-2:]
+ if m != m2:
+ raise LinAlgError('Incompatible dimensions')
+
+ t, result_t = _commonType(a, b)
+ result_real_t = _realType(result_t)
+
+ # Determine default rcond value
+ if rcond == "warn":
+ # 2017-08-19, 1.14.0
+ warnings.warn("`rcond` parameter will change to the default of "
+ "machine precision times ``max(M, N)`` where M and N "
+ "are the input matrix dimensions.\n"
+ "To use the future default and silence this warning "
+ "we advise to pass `rcond=None`, to keep using the old, "
+ "explicitly pass `rcond=-1`.",
+ FutureWarning, stacklevel=2)
+ rcond = -1
+ if rcond is None:
+ rcond = finfo(t).eps * max(n, m)
+
+ if m <= n:
+ gufunc = _umath_linalg.lstsq_m
+ else:
+ gufunc = _umath_linalg.lstsq_n
+
+ signature = 'DDd->Ddid' if isComplexType(t) else 'ddd->ddid'
+ extobj = get_linalg_error_extobj(_raise_linalgerror_lstsq)
+ if n_rhs == 0:
+ # lapack can't handle n_rhs = 0 - so allocate the array one larger in that axis
+ b = zeros(b.shape[:-2] + (m, n_rhs + 1), dtype=b.dtype)
+ x, resids, rank, s = gufunc(a, b, rcond, signature=signature, extobj=extobj)
+ if m == 0:
+ x[...] = 0
+ if n_rhs == 0:
+ # remove the item we added
+ x = x[..., :n_rhs]
+ resids = resids[..., :n_rhs]
+
+ # remove the axis we added
+ if is_1d:
+ x = x.squeeze(axis=-1)
+ # we probably should squeeze resids too, but we can't
+ # without breaking compatibility.
+
+ # as documented
+ if rank != n or m <= n:
+ resids = array([], result_real_t)
+
+ # coerce output arrays
+ s = s.astype(result_real_t, copy=False)
+ resids = resids.astype(result_real_t, copy=False)
+ x = x.astype(result_t, copy=True) # Copying lets the memory in r_parts be freed
+ return wrap(x), wrap(resids), rank, s
+
+
+def _multi_svd_norm(x, row_axis, col_axis, op):
+ """Compute a function of the singular values of the 2-D matrices in `x`.
+
+ This is a private utility function used by `numpy.linalg.norm()`.
+
+ Parameters
+ ----------
+ x : ndarray
+ row_axis, col_axis : int
+ The axes of `x` that hold the 2-D matrices.
+ op : callable
+ This should be either numpy.amin or `numpy.amax` or `numpy.sum`.
+
+ Returns
+ -------
+ result : float or ndarray
+ If `x` is 2-D, the return values is a float.
+ Otherwise, it is an array with ``x.ndim - 2`` dimensions.
+ The return values are either the minimum or maximum or sum of the
+ singular values of the matrices, depending on whether `op`
+ is `numpy.amin` or `numpy.amax` or `numpy.sum`.
+
+ """
+ y = moveaxis(x, (row_axis, col_axis), (-2, -1))
+ result = op(svd(y, compute_uv=False), axis=-1)
+ return result
+
+
+def _norm_dispatcher(x, ord=None, axis=None, keepdims=None):
+ return (x,)
+
+
+@array_function_dispatch(_norm_dispatcher)
+def norm(x, ord=None, axis=None, keepdims=False):
+ """
+ Matrix or vector norm.
+
+ This function is able to return one of eight different matrix norms,
+ or one of an infinite number of vector norms (described below), depending
+ on the value of the ``ord`` parameter.
+
+ Parameters
+ ----------
+ x : array_like
+ Input array. If `axis` is None, `x` must be 1-D or 2-D, unless `ord`
+ is None. If both `axis` and `ord` are None, the 2-norm of
+ ``x.ravel`` will be returned.
+ ord : {non-zero int, inf, -inf, 'fro', 'nuc'}, optional
+ Order of the norm (see table under ``Notes``). inf means numpy's
+ `inf` object. The default is None.
+ axis : {None, int, 2-tuple of ints}, optional.
+ If `axis` is an integer, it specifies the axis of `x` along which to
+ compute the vector norms. If `axis` is a 2-tuple, it specifies the
+ axes that hold 2-D matrices, and the matrix norms of these matrices
+ are computed. If `axis` is None then either a vector norm (when `x`
+ is 1-D) or a matrix norm (when `x` is 2-D) is returned. The default
+ is None.
+
+ .. versionadded:: 1.8.0
+
+ keepdims : bool, optional
+ If this is set to True, the axes which are normed over are left in the
+ result as dimensions with size one. With this option the result will
+ broadcast correctly against the original `x`.
+
+ .. versionadded:: 1.10.0
+
+ Returns
+ -------
+ n : float or ndarray
+ Norm of the matrix or vector(s).
+
+ See Also
+ --------
+ scipy.linalg.norm : Similar function in SciPy.
+
+ Notes
+ -----
+ For values of ``ord < 1``, the result is, strictly speaking, not a
+ mathematical 'norm', but it may still be useful for various numerical
+ purposes.
+
+ The following norms can be calculated:
+
+ ===== ============================ ==========================
+ ord norm for matrices norm for vectors
+ ===== ============================ ==========================
+ None Frobenius norm 2-norm
+ 'fro' Frobenius norm --
+ 'nuc' nuclear norm --
+ inf max(sum(abs(x), axis=1)) max(abs(x))
+ -inf min(sum(abs(x), axis=1)) min(abs(x))
+ 0 -- sum(x != 0)
+ 1 max(sum(abs(x), axis=0)) as below
+ -1 min(sum(abs(x), axis=0)) as below
+ 2 2-norm (largest sing. value) as below
+ -2 smallest singular value as below
+ other -- sum(abs(x)**ord)**(1./ord)
+ ===== ============================ ==========================
+
+ The Frobenius norm is given by [1]_:
+
+ :math:`||A||_F = [\\sum_{i,j} abs(a_{i,j})^2]^{1/2}`
+
+ The nuclear norm is the sum of the singular values.
+
+ Both the Frobenius and nuclear norm orders are only defined for
+ matrices and raise a ValueError when ``x.ndim != 2``.
+
+ References
+ ----------
+ .. [1] G. H. Golub and C. F. Van Loan, *Matrix Computations*,
+ Baltimore, MD, Johns Hopkins University Press, 1985, pg. 15
+
+ Examples
+ --------
+ >>> from numpy import linalg as LA
+ >>> a = np.arange(9) - 4
+ >>> a
+ array([-4, -3, -2, ..., 2, 3, 4])
+ >>> b = a.reshape((3, 3))
+ >>> b
+ array([[-4, -3, -2],
+ [-1, 0, 1],
+ [ 2, 3, 4]])
+
+ >>> LA.norm(a)
+ 7.745966692414834
+ >>> LA.norm(b)
+ 7.745966692414834
+ >>> LA.norm(b, 'fro')
+ 7.745966692414834
+ >>> LA.norm(a, np.inf)
+ 4.0
+ >>> LA.norm(b, np.inf)
+ 9.0
+ >>> LA.norm(a, -np.inf)
+ 0.0
+ >>> LA.norm(b, -np.inf)
+ 2.0
+
+ >>> LA.norm(a, 1)
+ 20.0
+ >>> LA.norm(b, 1)
+ 7.0
+ >>> LA.norm(a, -1)
+ -4.6566128774142013e-010
+ >>> LA.norm(b, -1)
+ 6.0
+ >>> LA.norm(a, 2)
+ 7.745966692414834
+ >>> LA.norm(b, 2)
+ 7.3484692283495345
+
+ >>> LA.norm(a, -2)
+ 0.0
+ >>> LA.norm(b, -2)
+ 1.8570331885190563e-016 # may vary
+ >>> LA.norm(a, 3)
+ 5.8480354764257312 # may vary
+ >>> LA.norm(a, -3)
+ 0.0
+
+ Using the `axis` argument to compute vector norms:
+
+ >>> c = np.array([[ 1, 2, 3],
+ ... [-1, 1, 4]])
+ >>> LA.norm(c, axis=0)
+ array([ 1.41421356, 2.23606798, 5. ])
+ >>> LA.norm(c, axis=1)
+ array([ 3.74165739, 4.24264069])
+ >>> LA.norm(c, ord=1, axis=1)
+ array([ 6., 6.])
+
+ Using the `axis` argument to compute matrix norms:
+
+ >>> m = np.arange(8).reshape(2,2,2)
+ >>> LA.norm(m, axis=(1,2))
+ array([ 3.74165739, 11.22497216])
+ >>> LA.norm(m[0, :, :]), LA.norm(m[1, :, :])
+ (3.7416573867739413, 11.224972160321824)
+
+ """
+ x = asarray(x)
+
+ if not issubclass(x.dtype.type, (inexact, object_)):
+ x = x.astype(float)
+
+ # Immediately handle some default, simple, fast, and common cases.
+ if axis is None:
+ ndim = x.ndim
+ if ((ord is None) or
+ (ord in ('f', 'fro') and ndim == 2) or
+ (ord == 2 and ndim == 1)):
+
+ x = x.ravel(order='K')
+ if isComplexType(x.dtype.type):
+ x_real = x.real
+ x_imag = x.imag
+ sqnorm = x_real.dot(x_real) + x_imag.dot(x_imag)
+ else:
+ sqnorm = x.dot(x)
+ ret = sqrt(sqnorm)
+ if keepdims:
+ ret = ret.reshape(ndim*[1])
+ return ret
+
+ # Normalize the `axis` argument to a tuple.
+ nd = x.ndim
+ if axis is None:
+ axis = tuple(range(nd))
+ elif not isinstance(axis, tuple):
+ try:
+ axis = int(axis)
+ except Exception as e:
+ raise TypeError("'axis' must be None, an integer or a tuple of integers") from e
+ axis = (axis,)
+
+ if len(axis) == 1:
+ if ord == Inf:
+ return abs(x).max(axis=axis, keepdims=keepdims)
+ elif ord == -Inf:
+ return abs(x).min(axis=axis, keepdims=keepdims)
+ elif ord == 0:
+ # Zero norm
+ return (x != 0).astype(x.real.dtype).sum(axis=axis, keepdims=keepdims)
+ elif ord == 1:
+ # special case for speedup
+ return add.reduce(abs(x), axis=axis, keepdims=keepdims)
+ elif ord is None or ord == 2:
+ # special case for speedup
+ s = (x.conj() * x).real
+ return sqrt(add.reduce(s, axis=axis, keepdims=keepdims))
+ # None of the str-type keywords for ord ('fro', 'nuc')
+ # are valid for vectors
+ elif isinstance(ord, str):
+ raise ValueError(f"Invalid norm order '{ord}' for vectors")
+ else:
+ absx = abs(x)
+ absx **= ord
+ ret = add.reduce(absx, axis=axis, keepdims=keepdims)
+ ret **= reciprocal(ord, dtype=ret.dtype)
+ return ret
+ elif len(axis) == 2:
+ row_axis, col_axis = axis
+ row_axis = normalize_axis_index(row_axis, nd)
+ col_axis = normalize_axis_index(col_axis, nd)
+ if row_axis == col_axis:
+ raise ValueError('Duplicate axes given.')
+ if ord == 2:
+ ret = _multi_svd_norm(x, row_axis, col_axis, amax)
+ elif ord == -2:
+ ret = _multi_svd_norm(x, row_axis, col_axis, amin)
+ elif ord == 1:
+ if col_axis > row_axis:
+ col_axis -= 1
+ ret = add.reduce(abs(x), axis=row_axis).max(axis=col_axis)
+ elif ord == Inf:
+ if row_axis > col_axis:
+ row_axis -= 1
+ ret = add.reduce(abs(x), axis=col_axis).max(axis=row_axis)
+ elif ord == -1:
+ if col_axis > row_axis:
+ col_axis -= 1
+ ret = add.reduce(abs(x), axis=row_axis).min(axis=col_axis)
+ elif ord == -Inf:
+ if row_axis > col_axis:
+ row_axis -= 1
+ ret = add.reduce(abs(x), axis=col_axis).min(axis=row_axis)
+ elif ord in [None, 'fro', 'f']:
+ ret = sqrt(add.reduce((x.conj() * x).real, axis=axis))
+ elif ord == 'nuc':
+ ret = _multi_svd_norm(x, row_axis, col_axis, sum)
+ else:
+ raise ValueError("Invalid norm order for matrices.")
+ if keepdims:
+ ret_shape = list(x.shape)
+ ret_shape[axis[0]] = 1
+ ret_shape[axis[1]] = 1
+ ret = ret.reshape(ret_shape)
+ return ret
+ else:
+ raise ValueError("Improper number of dimensions to norm.")
+
+
+# multi_dot
+
+def _multidot_dispatcher(arrays, *, out=None):
+ yield from arrays
+ yield out
+
+
+@array_function_dispatch(_multidot_dispatcher)
+def multi_dot(arrays, *, out=None):
+ """
+ Compute the dot product of two or more arrays in a single function call,
+ while automatically selecting the fastest evaluation order.
+
+ `multi_dot` chains `numpy.dot` and uses optimal parenthesization
+ of the matrices [1]_ [2]_. Depending on the shapes of the matrices,
+ this can speed up the multiplication a lot.
+
+ If the first argument is 1-D it is treated as a row vector.
+ If the last argument is 1-D it is treated as a column vector.
+ The other arguments must be 2-D.
+
+ Think of `multi_dot` as::
+
+ def multi_dot(arrays): return functools.reduce(np.dot, arrays)
+
+
+ Parameters
+ ----------
+ arrays : sequence of array_like
+ If the first argument is 1-D it is treated as row vector.
+ If the last argument is 1-D it is treated as column vector.
+ The other arguments must be 2-D.
+ out : ndarray, optional
+ Output argument. This must have the exact kind that would be returned
+ if it was not used. In particular, it must have the right type, must be
+ C-contiguous, and its dtype must be the dtype that would be returned
+ for `dot(a, b)`. This is a performance feature. Therefore, if these
+ conditions are not met, an exception is raised, instead of attempting
+ to be flexible.
+
+ .. versionadded:: 1.19.0
+
+ Returns
+ -------
+ output : ndarray
+ Returns the dot product of the supplied arrays.
+
+ See Also
+ --------
+ numpy.dot : dot multiplication with two arguments.
+
+ References
+ ----------
+
+ .. [1] Cormen, "Introduction to Algorithms", Chapter 15.2, p. 370-378
+ .. [2] https://en.wikipedia.org/wiki/Matrix_chain_multiplication
+
+ Examples
+ --------
+ `multi_dot` allows you to write::
+
+ >>> from numpy.linalg import multi_dot
+ >>> # Prepare some data
+ >>> A = np.random.random((10000, 100))
+ >>> B = np.random.random((100, 1000))
+ >>> C = np.random.random((1000, 5))
+ >>> D = np.random.random((5, 333))
+ >>> # the actual dot multiplication
+ >>> _ = multi_dot([A, B, C, D])
+
+ instead of::
+
+ >>> _ = np.dot(np.dot(np.dot(A, B), C), D)
+ >>> # or
+ >>> _ = A.dot(B).dot(C).dot(D)
+
+ Notes
+ -----
+ The cost for a matrix multiplication can be calculated with the
+ following function::
+
+ def cost(A, B):
+ return A.shape[0] * A.shape[1] * B.shape[1]
+
+ Assume we have three matrices
+ :math:`A_{10x100}, B_{100x5}, C_{5x50}`.
+
+ The costs for the two different parenthesizations are as follows::
+
+ cost((AB)C) = 10*100*5 + 10*5*50 = 5000 + 2500 = 7500
+ cost(A(BC)) = 10*100*50 + 100*5*50 = 50000 + 25000 = 75000
+
+ """
+ n = len(arrays)
+ # optimization only makes sense for len(arrays) > 2
+ if n < 2:
+ raise ValueError("Expecting at least two arrays.")
+ elif n == 2:
+ return dot(arrays[0], arrays[1], out=out)
+
+ arrays = [asanyarray(a) for a in arrays]
+
+ # save original ndim to reshape the result array into the proper form later
+ ndim_first, ndim_last = arrays[0].ndim, arrays[-1].ndim
+ # Explicitly convert vectors to 2D arrays to keep the logic of the internal
+ # _multi_dot_* functions as simple as possible.
+ if arrays[0].ndim == 1:
+ arrays[0] = atleast_2d(arrays[0])
+ if arrays[-1].ndim == 1:
+ arrays[-1] = atleast_2d(arrays[-1]).T
+ _assert_2d(*arrays)
+
+ # _multi_dot_three is much faster than _multi_dot_matrix_chain_order
+ if n == 3:
+ result = _multi_dot_three(arrays[0], arrays[1], arrays[2], out=out)
+ else:
+ order = _multi_dot_matrix_chain_order(arrays)
+ result = _multi_dot(arrays, order, 0, n - 1, out=out)
+
+ # return proper shape
+ if ndim_first == 1 and ndim_last == 1:
+ return result[0, 0] # scalar
+ elif ndim_first == 1 or ndim_last == 1:
+ return result.ravel() # 1-D
+ else:
+ return result
+
+
+def _multi_dot_three(A, B, C, out=None):
+ """
+ Find the best order for three arrays and do the multiplication.
+
+ For three arguments `_multi_dot_three` is approximately 15 times faster
+ than `_multi_dot_matrix_chain_order`
+
+ """
+ a0, a1b0 = A.shape
+ b1c0, c1 = C.shape
+ # cost1 = cost((AB)C) = a0*a1b0*b1c0 + a0*b1c0*c1
+ cost1 = a0 * b1c0 * (a1b0 + c1)
+ # cost2 = cost(A(BC)) = a1b0*b1c0*c1 + a0*a1b0*c1
+ cost2 = a1b0 * c1 * (a0 + b1c0)
+
+ if cost1 < cost2:
+ return dot(dot(A, B), C, out=out)
+ else:
+ return dot(A, dot(B, C), out=out)
+
+
+def _multi_dot_matrix_chain_order(arrays, return_costs=False):
+ """
+ Return a np.array that encodes the optimal order of mutiplications.
+
+ The optimal order array is then used by `_multi_dot()` to do the
+ multiplication.
+
+ Also return the cost matrix if `return_costs` is `True`
+
+ The implementation CLOSELY follows Cormen, "Introduction to Algorithms",
+ Chapter 15.2, p. 370-378. Note that Cormen uses 1-based indices.
+
+ cost[i, j] = min([
+ cost[prefix] + cost[suffix] + cost_mult(prefix, suffix)
+ for k in range(i, j)])
+
+ """
+ n = len(arrays)
+ # p stores the dimensions of the matrices
+ # Example for p: A_{10x100}, B_{100x5}, C_{5x50} --> p = [10, 100, 5, 50]
+ p = [a.shape[0] for a in arrays] + [arrays[-1].shape[1]]
+ # m is a matrix of costs of the subproblems
+ # m[i,j]: min number of scalar multiplications needed to compute A_{i..j}
+ m = zeros((n, n), dtype=double)
+ # s is the actual ordering
+ # s[i, j] is the value of k at which we split the product A_i..A_j
+ s = empty((n, n), dtype=intp)
+
+ for l in range(1, n):
+ for i in range(n - l):
+ j = i + l
+ m[i, j] = Inf
+ for k in range(i, j):
+ q = m[i, k] + m[k+1, j] + p[i]*p[k+1]*p[j+1]
+ if q < m[i, j]:
+ m[i, j] = q
+ s[i, j] = k # Note that Cormen uses 1-based index
+
+ return (s, m) if return_costs else s
+
+
+def _multi_dot(arrays, order, i, j, out=None):
+ """Actually do the multiplication with the given order."""
+ if i == j:
+ # the initial call with non-None out should never get here
+ assert out is None
+
+ return arrays[i]
+ else:
+ return dot(_multi_dot(arrays, order, i, order[i, j]),
+ _multi_dot(arrays, order, order[i, j] + 1, j),
+ out=out)
diff --git a/.venv/lib/python3.12/site-packages/numpy/linalg/linalg.pyi b/.venv/lib/python3.12/site-packages/numpy/linalg/linalg.pyi
new file mode 100644
index 00000000..c0b2f29b
--- /dev/null
+++ b/.venv/lib/python3.12/site-packages/numpy/linalg/linalg.pyi
@@ -0,0 +1,297 @@
+from collections.abc import Iterable
+from typing import (
+ Literal as L,
+ overload,
+ TypeVar,
+ Any,
+ SupportsIndex,
+ SupportsInt,
+ NamedTuple,
+ Generic,
+)
+
+from numpy import (
+ generic,
+ floating,
+ complexfloating,
+ int32,
+ float64,
+ complex128,
+)
+
+from numpy.linalg import LinAlgError as LinAlgError
+
+from numpy._typing import (
+ NDArray,
+ ArrayLike,
+ _ArrayLikeInt_co,
+ _ArrayLikeFloat_co,
+ _ArrayLikeComplex_co,
+ _ArrayLikeTD64_co,
+ _ArrayLikeObject_co,
+)
+
+_T = TypeVar("_T")
+_ArrayType = TypeVar("_ArrayType", bound=NDArray[Any])
+_SCT = TypeVar("_SCT", bound=generic, covariant=True)
+_SCT2 = TypeVar("_SCT2", bound=generic, covariant=True)
+
+_2Tuple = tuple[_T, _T]
+_ModeKind = L["reduced", "complete", "r", "raw"]
+
+__all__: list[str]
+
+class EigResult(NamedTuple):
+ eigenvalues: NDArray[Any]
+ eigenvectors: NDArray[Any]
+
+class EighResult(NamedTuple):
+ eigenvalues: NDArray[Any]
+ eigenvectors: NDArray[Any]
+
+class QRResult(NamedTuple):
+ Q: NDArray[Any]
+ R: NDArray[Any]
+
+class SlogdetResult(NamedTuple):
+ # TODO: `sign` and `logabsdet` are scalars for input 2D arrays and
+ # a `(x.ndim - 2)`` dimensionl arrays otherwise
+ sign: Any
+ logabsdet: Any
+
+class SVDResult(NamedTuple):
+ U: NDArray[Any]
+ S: NDArray[Any]
+ Vh: NDArray[Any]
+
+@overload
+def tensorsolve(
+ a: _ArrayLikeInt_co,
+ b: _ArrayLikeInt_co,
+ axes: None | Iterable[int] =...,
+) -> NDArray[float64]: ...
+@overload
+def tensorsolve(
+ a: _ArrayLikeFloat_co,
+ b: _ArrayLikeFloat_co,
+ axes: None | Iterable[int] =...,
+) -> NDArray[floating[Any]]: ...
+@overload
+def tensorsolve(
+ a: _ArrayLikeComplex_co,
+ b: _ArrayLikeComplex_co,
+ axes: None | Iterable[int] =...,
+) -> NDArray[complexfloating[Any, Any]]: ...
+
+@overload
+def solve(
+ a: _ArrayLikeInt_co,
+ b: _ArrayLikeInt_co,
+) -> NDArray[float64]: ...
+@overload
+def solve(
+ a: _ArrayLikeFloat_co,
+ b: _ArrayLikeFloat_co,
+) -> NDArray[floating[Any]]: ...
+@overload
+def solve(
+ a: _ArrayLikeComplex_co,
+ b: _ArrayLikeComplex_co,
+) -> NDArray[complexfloating[Any, Any]]: ...
+
+@overload
+def tensorinv(
+ a: _ArrayLikeInt_co,
+ ind: int = ...,
+) -> NDArray[float64]: ...
+@overload
+def tensorinv(
+ a: _ArrayLikeFloat_co,
+ ind: int = ...,
+) -> NDArray[floating[Any]]: ...
+@overload
+def tensorinv(
+ a: _ArrayLikeComplex_co,
+ ind: int = ...,
+) -> NDArray[complexfloating[Any, Any]]: ...
+
+@overload
+def inv(a: _ArrayLikeInt_co) -> NDArray[float64]: ...
+@overload
+def inv(a: _ArrayLikeFloat_co) -> NDArray[floating[Any]]: ...
+@overload
+def inv(a: _ArrayLikeComplex_co) -> NDArray[complexfloating[Any, Any]]: ...
+
+# TODO: The supported input and output dtypes are dependent on the value of `n`.
+# For example: `n < 0` always casts integer types to float64
+def matrix_power(
+ a: _ArrayLikeComplex_co | _ArrayLikeObject_co,
+ n: SupportsIndex,
+) -> NDArray[Any]: ...
+
+@overload
+def cholesky(a: _ArrayLikeInt_co) -> NDArray[float64]: ...
+@overload
+def cholesky(a: _ArrayLikeFloat_co) -> NDArray[floating[Any]]: ...
+@overload
+def cholesky(a: _ArrayLikeComplex_co) -> NDArray[complexfloating[Any, Any]]: ...
+
+@overload
+def qr(a: _ArrayLikeInt_co, mode: _ModeKind = ...) -> QRResult: ...
+@overload
+def qr(a: _ArrayLikeFloat_co, mode: _ModeKind = ...) -> QRResult: ...
+@overload
+def qr(a: _ArrayLikeComplex_co, mode: _ModeKind = ...) -> QRResult: ...
+
+@overload
+def eigvals(a: _ArrayLikeInt_co) -> NDArray[float64] | NDArray[complex128]: ...
+@overload
+def eigvals(a: _ArrayLikeFloat_co) -> NDArray[floating[Any]] | NDArray[complexfloating[Any, Any]]: ...
+@overload
+def eigvals(a: _ArrayLikeComplex_co) -> NDArray[complexfloating[Any, Any]]: ...
+
+@overload
+def eigvalsh(a: _ArrayLikeInt_co, UPLO: L["L", "U", "l", "u"] = ...) -> NDArray[float64]: ...
+@overload
+def eigvalsh(a: _ArrayLikeComplex_co, UPLO: L["L", "U", "l", "u"] = ...) -> NDArray[floating[Any]]: ...
+
+@overload
+def eig(a: _ArrayLikeInt_co) -> EigResult: ...
+@overload
+def eig(a: _ArrayLikeFloat_co) -> EigResult: ...
+@overload
+def eig(a: _ArrayLikeComplex_co) -> EigResult: ...
+
+@overload
+def eigh(
+ a: _ArrayLikeInt_co,
+ UPLO: L["L", "U", "l", "u"] = ...,
+) -> EighResult: ...
+@overload
+def eigh(
+ a: _ArrayLikeFloat_co,
+ UPLO: L["L", "U", "l", "u"] = ...,
+) -> EighResult: ...
+@overload
+def eigh(
+ a: _ArrayLikeComplex_co,
+ UPLO: L["L", "U", "l", "u"] = ...,
+) -> EighResult: ...
+
+@overload
+def svd(
+ a: _ArrayLikeInt_co,
+ full_matrices: bool = ...,
+ compute_uv: L[True] = ...,
+ hermitian: bool = ...,
+) -> SVDResult: ...
+@overload
+def svd(
+ a: _ArrayLikeFloat_co,
+ full_matrices: bool = ...,
+ compute_uv: L[True] = ...,
+ hermitian: bool = ...,
+) -> SVDResult: ...
+@overload
+def svd(
+ a: _ArrayLikeComplex_co,
+ full_matrices: bool = ...,
+ compute_uv: L[True] = ...,
+ hermitian: bool = ...,
+) -> SVDResult: ...
+@overload
+def svd(
+ a: _ArrayLikeInt_co,
+ full_matrices: bool = ...,
+ compute_uv: L[False] = ...,
+ hermitian: bool = ...,
+) -> NDArray[float64]: ...
+@overload
+def svd(
+ a: _ArrayLikeComplex_co,
+ full_matrices: bool = ...,
+ compute_uv: L[False] = ...,
+ hermitian: bool = ...,
+) -> NDArray[floating[Any]]: ...
+
+# TODO: Returns a scalar for 2D arrays and
+# a `(x.ndim - 2)`` dimensionl array otherwise
+def cond(x: _ArrayLikeComplex_co, p: None | float | L["fro", "nuc"] = ...) -> Any: ...
+
+# TODO: Returns `int` for <2D arrays and `intp` otherwise
+def matrix_rank(
+ A: _ArrayLikeComplex_co,
+ tol: None | _ArrayLikeFloat_co = ...,
+ hermitian: bool = ...,
+) -> Any: ...
+
+@overload
+def pinv(
+ a: _ArrayLikeInt_co,
+ rcond: _ArrayLikeFloat_co = ...,
+ hermitian: bool = ...,
+) -> NDArray[float64]: ...
+@overload
+def pinv(
+ a: _ArrayLikeFloat_co,
+ rcond: _ArrayLikeFloat_co = ...,
+ hermitian: bool = ...,
+) -> NDArray[floating[Any]]: ...
+@overload
+def pinv(
+ a: _ArrayLikeComplex_co,
+ rcond: _ArrayLikeFloat_co = ...,
+ hermitian: bool = ...,
+) -> NDArray[complexfloating[Any, Any]]: ...
+
+# TODO: Returns a 2-tuple of scalars for 2D arrays and
+# a 2-tuple of `(a.ndim - 2)`` dimensionl arrays otherwise
+def slogdet(a: _ArrayLikeComplex_co) -> SlogdetResult: ...
+
+# TODO: Returns a 2-tuple of scalars for 2D arrays and
+# a 2-tuple of `(a.ndim - 2)`` dimensionl arrays otherwise
+def det(a: _ArrayLikeComplex_co) -> Any: ...
+
+@overload
+def lstsq(a: _ArrayLikeInt_co, b: _ArrayLikeInt_co, rcond: None | float = ...) -> tuple[
+ NDArray[float64],
+ NDArray[float64],
+ int32,
+ NDArray[float64],
+]: ...
+@overload
+def lstsq(a: _ArrayLikeFloat_co, b: _ArrayLikeFloat_co, rcond: None | float = ...) -> tuple[
+ NDArray[floating[Any]],
+ NDArray[floating[Any]],
+ int32,
+ NDArray[floating[Any]],
+]: ...
+@overload
+def lstsq(a: _ArrayLikeComplex_co, b: _ArrayLikeComplex_co, rcond: None | float = ...) -> tuple[
+ NDArray[complexfloating[Any, Any]],
+ NDArray[floating[Any]],
+ int32,
+ NDArray[floating[Any]],
+]: ...
+
+@overload
+def norm(
+ x: ArrayLike,
+ ord: None | float | L["fro", "nuc"] = ...,
+ axis: None = ...,
+ keepdims: bool = ...,
+) -> floating[Any]: ...
+@overload
+def norm(
+ x: ArrayLike,
+ ord: None | float | L["fro", "nuc"] = ...,
+ axis: SupportsInt | SupportsIndex | tuple[int, ...] = ...,
+ keepdims: bool = ...,
+) -> Any: ...
+
+# TODO: Returns a scalar or array
+def multi_dot(
+ arrays: Iterable[_ArrayLikeComplex_co | _ArrayLikeObject_co | _ArrayLikeTD64_co],
+ *,
+ out: None | NDArray[Any] = ...,
+) -> Any: ...
diff --git a/.venv/lib/python3.12/site-packages/numpy/linalg/tests/__init__.py b/.venv/lib/python3.12/site-packages/numpy/linalg/tests/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/.venv/lib/python3.12/site-packages/numpy/linalg/tests/__init__.py
diff --git a/.venv/lib/python3.12/site-packages/numpy/linalg/tests/test_deprecations.py b/.venv/lib/python3.12/site-packages/numpy/linalg/tests/test_deprecations.py
new file mode 100644
index 00000000..cd4c1083
--- /dev/null
+++ b/.venv/lib/python3.12/site-packages/numpy/linalg/tests/test_deprecations.py
@@ -0,0 +1,20 @@
+"""Test deprecation and future warnings.
+
+"""
+import numpy as np
+from numpy.testing import assert_warns
+
+
+def test_qr_mode_full_future_warning():
+ """Check mode='full' FutureWarning.
+
+ In numpy 1.8 the mode options 'full' and 'economic' in linalg.qr were
+ deprecated. The release date will probably be sometime in the summer
+ of 2013.
+
+ """
+ a = np.eye(2)
+ assert_warns(DeprecationWarning, np.linalg.qr, a, mode='full')
+ assert_warns(DeprecationWarning, np.linalg.qr, a, mode='f')
+ assert_warns(DeprecationWarning, np.linalg.qr, a, mode='economic')
+ assert_warns(DeprecationWarning, np.linalg.qr, a, mode='e')
diff --git a/.venv/lib/python3.12/site-packages/numpy/linalg/tests/test_linalg.py b/.venv/lib/python3.12/site-packages/numpy/linalg/tests/test_linalg.py
new file mode 100644
index 00000000..5dabdfdf
--- /dev/null
+++ b/.venv/lib/python3.12/site-packages/numpy/linalg/tests/test_linalg.py
@@ -0,0 +1,2198 @@
+""" Test functions for linalg module
+
+"""
+import os
+import sys
+import itertools
+import traceback
+import textwrap
+import subprocess
+import pytest
+
+import numpy as np
+from numpy import array, single, double, csingle, cdouble, dot, identity, matmul
+from numpy.core import swapaxes
+from numpy import multiply, atleast_2d, inf, asarray
+from numpy import linalg
+from numpy.linalg import matrix_power, norm, matrix_rank, multi_dot, LinAlgError
+from numpy.linalg.linalg import _multi_dot_matrix_chain_order
+from numpy.testing import (
+ assert_, assert_equal, assert_raises, assert_array_equal,
+ assert_almost_equal, assert_allclose, suppress_warnings,
+ assert_raises_regex, HAS_LAPACK64, IS_WASM
+ )
+try:
+ import numpy.linalg.lapack_lite
+except ImportError:
+ # May be broken when numpy was built without BLAS/LAPACK present
+ # If so, ensure we don't break the whole test suite - the `lapack_lite`
+ # submodule should be removed, it's only used in two tests in this file.
+ pass
+
+
+def consistent_subclass(out, in_):
+ # For ndarray subclass input, our output should have the same subclass
+ # (non-ndarray input gets converted to ndarray).
+ return type(out) is (type(in_) if isinstance(in_, np.ndarray)
+ else np.ndarray)
+
+
+old_assert_almost_equal = assert_almost_equal
+
+
+def assert_almost_equal(a, b, single_decimal=6, double_decimal=12, **kw):
+ if asarray(a).dtype.type in (single, csingle):
+ decimal = single_decimal
+ else:
+ decimal = double_decimal
+ old_assert_almost_equal(a, b, decimal=decimal, **kw)
+
+
+def get_real_dtype(dtype):
+ return {single: single, double: double,
+ csingle: single, cdouble: double}[dtype]
+
+
+def get_complex_dtype(dtype):
+ return {single: csingle, double: cdouble,
+ csingle: csingle, cdouble: cdouble}[dtype]
+
+
+def get_rtol(dtype):
+ # Choose a safe rtol
+ if dtype in (single, csingle):
+ return 1e-5
+ else:
+ return 1e-11
+
+
+# used to categorize tests
+all_tags = {
+ 'square', 'nonsquare', 'hermitian', # mutually exclusive
+ 'generalized', 'size-0', 'strided' # optional additions
+}
+
+
+class LinalgCase:
+ def __init__(self, name, a, b, tags=set()):
+ """
+ A bundle of arguments to be passed to a test case, with an identifying
+ name, the operands a and b, and a set of tags to filter the tests
+ """
+ assert_(isinstance(name, str))
+ self.name = name
+ self.a = a
+ self.b = b
+ self.tags = frozenset(tags) # prevent shared tags
+
+ def check(self, do):
+ """
+ Run the function `do` on this test case, expanding arguments
+ """
+ do(self.a, self.b, tags=self.tags)
+
+ def __repr__(self):
+ return f'<LinalgCase: {self.name}>'
+
+
+def apply_tag(tag, cases):
+ """
+ Add the given tag (a string) to each of the cases (a list of LinalgCase
+ objects)
+ """
+ assert tag in all_tags, "Invalid tag"
+ for case in cases:
+ case.tags = case.tags | {tag}
+ return cases
+
+
+#
+# Base test cases
+#
+
+np.random.seed(1234)
+
+CASES = []
+
+# square test cases
+CASES += apply_tag('square', [
+ LinalgCase("single",
+ array([[1., 2.], [3., 4.]], dtype=single),
+ array([2., 1.], dtype=single)),
+ LinalgCase("double",
+ array([[1., 2.], [3., 4.]], dtype=double),
+ array([2., 1.], dtype=double)),
+ LinalgCase("double_2",
+ array([[1., 2.], [3., 4.]], dtype=double),
+ array([[2., 1., 4.], [3., 4., 6.]], dtype=double)),
+ LinalgCase("csingle",
+ array([[1. + 2j, 2 + 3j], [3 + 4j, 4 + 5j]], dtype=csingle),
+ array([2. + 1j, 1. + 2j], dtype=csingle)),
+ LinalgCase("cdouble",
+ array([[1. + 2j, 2 + 3j], [3 + 4j, 4 + 5j]], dtype=cdouble),
+ array([2. + 1j, 1. + 2j], dtype=cdouble)),
+ LinalgCase("cdouble_2",
+ array([[1. + 2j, 2 + 3j], [3 + 4j, 4 + 5j]], dtype=cdouble),
+ array([[2. + 1j, 1. + 2j, 1 + 3j], [1 - 2j, 1 - 3j, 1 - 6j]], dtype=cdouble)),
+ LinalgCase("0x0",
+ np.empty((0, 0), dtype=double),
+ np.empty((0,), dtype=double),
+ tags={'size-0'}),
+ LinalgCase("8x8",
+ np.random.rand(8, 8),
+ np.random.rand(8)),
+ LinalgCase("1x1",
+ np.random.rand(1, 1),
+ np.random.rand(1)),
+ LinalgCase("nonarray",
+ [[1, 2], [3, 4]],
+ [2, 1]),
+])
+
+# non-square test-cases
+CASES += apply_tag('nonsquare', [
+ LinalgCase("single_nsq_1",
+ array([[1., 2., 3.], [3., 4., 6.]], dtype=single),
+ array([2., 1.], dtype=single)),
+ LinalgCase("single_nsq_2",
+ array([[1., 2.], [3., 4.], [5., 6.]], dtype=single),
+ array([2., 1., 3.], dtype=single)),
+ LinalgCase("double_nsq_1",
+ array([[1., 2., 3.], [3., 4., 6.]], dtype=double),
+ array([2., 1.], dtype=double)),
+ LinalgCase("double_nsq_2",
+ array([[1., 2.], [3., 4.], [5., 6.]], dtype=double),
+ array([2., 1., 3.], dtype=double)),
+ LinalgCase("csingle_nsq_1",
+ array(
+ [[1. + 1j, 2. + 2j, 3. - 3j], [3. - 5j, 4. + 9j, 6. + 2j]], dtype=csingle),
+ array([2. + 1j, 1. + 2j], dtype=csingle)),
+ LinalgCase("csingle_nsq_2",
+ array(
+ [[1. + 1j, 2. + 2j], [3. - 3j, 4. - 9j], [5. - 4j, 6. + 8j]], dtype=csingle),
+ array([2. + 1j, 1. + 2j, 3. - 3j], dtype=csingle)),
+ LinalgCase("cdouble_nsq_1",
+ array(
+ [[1. + 1j, 2. + 2j, 3. - 3j], [3. - 5j, 4. + 9j, 6. + 2j]], dtype=cdouble),
+ array([2. + 1j, 1. + 2j], dtype=cdouble)),
+ LinalgCase("cdouble_nsq_2",
+ array(
+ [[1. + 1j, 2. + 2j], [3. - 3j, 4. - 9j], [5. - 4j, 6. + 8j]], dtype=cdouble),
+ array([2. + 1j, 1. + 2j, 3. - 3j], dtype=cdouble)),
+ LinalgCase("cdouble_nsq_1_2",
+ array(
+ [[1. + 1j, 2. + 2j, 3. - 3j], [3. - 5j, 4. + 9j, 6. + 2j]], dtype=cdouble),
+ array([[2. + 1j, 1. + 2j], [1 - 1j, 2 - 2j]], dtype=cdouble)),
+ LinalgCase("cdouble_nsq_2_2",
+ array(
+ [[1. + 1j, 2. + 2j], [3. - 3j, 4. - 9j], [5. - 4j, 6. + 8j]], dtype=cdouble),
+ array([[2. + 1j, 1. + 2j], [1 - 1j, 2 - 2j], [1 - 1j, 2 - 2j]], dtype=cdouble)),
+ LinalgCase("8x11",
+ np.random.rand(8, 11),
+ np.random.rand(8)),
+ LinalgCase("1x5",
+ np.random.rand(1, 5),
+ np.random.rand(1)),
+ LinalgCase("5x1",
+ np.random.rand(5, 1),
+ np.random.rand(5)),
+ LinalgCase("0x4",
+ np.random.rand(0, 4),
+ np.random.rand(0),
+ tags={'size-0'}),
+ LinalgCase("4x0",
+ np.random.rand(4, 0),
+ np.random.rand(4),
+ tags={'size-0'}),
+])
+
+# hermitian test-cases
+CASES += apply_tag('hermitian', [
+ LinalgCase("hsingle",
+ array([[1., 2.], [2., 1.]], dtype=single),
+ None),
+ LinalgCase("hdouble",
+ array([[1., 2.], [2., 1.]], dtype=double),
+ None),
+ LinalgCase("hcsingle",
+ array([[1., 2 + 3j], [2 - 3j, 1]], dtype=csingle),
+ None),
+ LinalgCase("hcdouble",
+ array([[1., 2 + 3j], [2 - 3j, 1]], dtype=cdouble),
+ None),
+ LinalgCase("hempty",
+ np.empty((0, 0), dtype=double),
+ None,
+ tags={'size-0'}),
+ LinalgCase("hnonarray",
+ [[1, 2], [2, 1]],
+ None),
+ LinalgCase("matrix_b_only",
+ array([[1., 2.], [2., 1.]]),
+ None),
+ LinalgCase("hmatrix_1x1",
+ np.random.rand(1, 1),
+ None),
+])
+
+
+#
+# Gufunc test cases
+#
+def _make_generalized_cases():
+ new_cases = []
+
+ for case in CASES:
+ if not isinstance(case.a, np.ndarray):
+ continue
+
+ a = np.array([case.a, 2 * case.a, 3 * case.a])
+ if case.b is None:
+ b = None
+ else:
+ b = np.array([case.b, 7 * case.b, 6 * case.b])
+ new_case = LinalgCase(case.name + "_tile3", a, b,
+ tags=case.tags | {'generalized'})
+ new_cases.append(new_case)
+
+ a = np.array([case.a] * 2 * 3).reshape((3, 2) + case.a.shape)
+ if case.b is None:
+ b = None
+ else:
+ b = np.array([case.b] * 2 * 3).reshape((3, 2) + case.b.shape)
+ new_case = LinalgCase(case.name + "_tile213", a, b,
+ tags=case.tags | {'generalized'})
+ new_cases.append(new_case)
+
+ return new_cases
+
+
+CASES += _make_generalized_cases()
+
+
+#
+# Generate stride combination variations of the above
+#
+def _stride_comb_iter(x):
+ """
+ Generate cartesian product of strides for all axes
+ """
+
+ if not isinstance(x, np.ndarray):
+ yield x, "nop"
+ return
+
+ stride_set = [(1,)] * x.ndim
+ stride_set[-1] = (1, 3, -4)
+ if x.ndim > 1:
+ stride_set[-2] = (1, 3, -4)
+ if x.ndim > 2:
+ stride_set[-3] = (1, -4)
+
+ for repeats in itertools.product(*tuple(stride_set)):
+ new_shape = [abs(a * b) for a, b in zip(x.shape, repeats)]
+ slices = tuple([slice(None, None, repeat) for repeat in repeats])
+
+ # new array with different strides, but same data
+ xi = np.empty(new_shape, dtype=x.dtype)
+ xi.view(np.uint32).fill(0xdeadbeef)
+ xi = xi[slices]
+ xi[...] = x
+ xi = xi.view(x.__class__)
+ assert_(np.all(xi == x))
+ yield xi, "stride_" + "_".join(["%+d" % j for j in repeats])
+
+ # generate also zero strides if possible
+ if x.ndim >= 1 and x.shape[-1] == 1:
+ s = list(x.strides)
+ s[-1] = 0
+ xi = np.lib.stride_tricks.as_strided(x, strides=s)
+ yield xi, "stride_xxx_0"
+ if x.ndim >= 2 and x.shape[-2] == 1:
+ s = list(x.strides)
+ s[-2] = 0
+ xi = np.lib.stride_tricks.as_strided(x, strides=s)
+ yield xi, "stride_xxx_0_x"
+ if x.ndim >= 2 and x.shape[:-2] == (1, 1):
+ s = list(x.strides)
+ s[-1] = 0
+ s[-2] = 0
+ xi = np.lib.stride_tricks.as_strided(x, strides=s)
+ yield xi, "stride_xxx_0_0"
+
+
+def _make_strided_cases():
+ new_cases = []
+ for case in CASES:
+ for a, a_label in _stride_comb_iter(case.a):
+ for b, b_label in _stride_comb_iter(case.b):
+ new_case = LinalgCase(case.name + "_" + a_label + "_" + b_label, a, b,
+ tags=case.tags | {'strided'})
+ new_cases.append(new_case)
+ return new_cases
+
+
+CASES += _make_strided_cases()
+
+
+#
+# Test different routines against the above cases
+#
+class LinalgTestCase:
+ TEST_CASES = CASES
+
+ def check_cases(self, require=set(), exclude=set()):
+ """
+ Run func on each of the cases with all of the tags in require, and none
+ of the tags in exclude
+ """
+ for case in self.TEST_CASES:
+ # filter by require and exclude
+ if case.tags & require != require:
+ continue
+ if case.tags & exclude:
+ continue
+
+ try:
+ case.check(self.do)
+ except Exception as e:
+ msg = f'In test case: {case!r}\n\n'
+ msg += traceback.format_exc()
+ raise AssertionError(msg) from e
+
+
+class LinalgSquareTestCase(LinalgTestCase):
+
+ def test_sq_cases(self):
+ self.check_cases(require={'square'},
+ exclude={'generalized', 'size-0'})
+
+ def test_empty_sq_cases(self):
+ self.check_cases(require={'square', 'size-0'},
+ exclude={'generalized'})
+
+
+class LinalgNonsquareTestCase(LinalgTestCase):
+
+ def test_nonsq_cases(self):
+ self.check_cases(require={'nonsquare'},
+ exclude={'generalized', 'size-0'})
+
+ def test_empty_nonsq_cases(self):
+ self.check_cases(require={'nonsquare', 'size-0'},
+ exclude={'generalized'})
+
+
+class HermitianTestCase(LinalgTestCase):
+
+ def test_herm_cases(self):
+ self.check_cases(require={'hermitian'},
+ exclude={'generalized', 'size-0'})
+
+ def test_empty_herm_cases(self):
+ self.check_cases(require={'hermitian', 'size-0'},
+ exclude={'generalized'})
+
+
+class LinalgGeneralizedSquareTestCase(LinalgTestCase):
+
+ @pytest.mark.slow
+ def test_generalized_sq_cases(self):
+ self.check_cases(require={'generalized', 'square'},
+ exclude={'size-0'})
+
+ @pytest.mark.slow
+ def test_generalized_empty_sq_cases(self):
+ self.check_cases(require={'generalized', 'square', 'size-0'})
+
+
+class LinalgGeneralizedNonsquareTestCase(LinalgTestCase):
+
+ @pytest.mark.slow
+ def test_generalized_nonsq_cases(self):
+ self.check_cases(require={'generalized', 'nonsquare'},
+ exclude={'size-0'})
+
+ @pytest.mark.slow
+ def test_generalized_empty_nonsq_cases(self):
+ self.check_cases(require={'generalized', 'nonsquare', 'size-0'})
+
+
+class HermitianGeneralizedTestCase(LinalgTestCase):
+
+ @pytest.mark.slow
+ def test_generalized_herm_cases(self):
+ self.check_cases(require={'generalized', 'hermitian'},
+ exclude={'size-0'})
+
+ @pytest.mark.slow
+ def test_generalized_empty_herm_cases(self):
+ self.check_cases(require={'generalized', 'hermitian', 'size-0'},
+ exclude={'none'})
+
+
+def dot_generalized(a, b):
+ a = asarray(a)
+ if a.ndim >= 3:
+ if a.ndim == b.ndim:
+ # matrix x matrix
+ new_shape = a.shape[:-1] + b.shape[-1:]
+ elif a.ndim == b.ndim + 1:
+ # matrix x vector
+ new_shape = a.shape[:-1]
+ else:
+ raise ValueError("Not implemented...")
+ r = np.empty(new_shape, dtype=np.common_type(a, b))
+ for c in itertools.product(*map(range, a.shape[:-2])):
+ r[c] = dot(a[c], b[c])
+ return r
+ else:
+ return dot(a, b)
+
+
+def identity_like_generalized(a):
+ a = asarray(a)
+ if a.ndim >= 3:
+ r = np.empty(a.shape, dtype=a.dtype)
+ r[...] = identity(a.shape[-2])
+ return r
+ else:
+ return identity(a.shape[0])
+
+
+class SolveCases(LinalgSquareTestCase, LinalgGeneralizedSquareTestCase):
+ # kept apart from TestSolve for use for testing with matrices.
+ def do(self, a, b, tags):
+ x = linalg.solve(a, b)
+ assert_almost_equal(b, dot_generalized(a, x))
+ assert_(consistent_subclass(x, b))
+
+
+class TestSolve(SolveCases):
+ @pytest.mark.parametrize('dtype', [single, double, csingle, cdouble])
+ def test_types(self, dtype):
+ x = np.array([[1, 0.5], [0.5, 1]], dtype=dtype)
+ assert_equal(linalg.solve(x, x).dtype, dtype)
+
+ def test_0_size(self):
+ class ArraySubclass(np.ndarray):
+ pass
+ # Test system of 0x0 matrices
+ a = np.arange(8).reshape(2, 2, 2)
+ b = np.arange(6).reshape(1, 2, 3).view(ArraySubclass)
+
+ expected = linalg.solve(a, b)[:, 0:0, :]
+ result = linalg.solve(a[:, 0:0, 0:0], b[:, 0:0, :])
+ assert_array_equal(result, expected)
+ assert_(isinstance(result, ArraySubclass))
+
+ # Test errors for non-square and only b's dimension being 0
+ assert_raises(linalg.LinAlgError, linalg.solve, a[:, 0:0, 0:1], b)
+ assert_raises(ValueError, linalg.solve, a, b[:, 0:0, :])
+
+ # Test broadcasting error
+ b = np.arange(6).reshape(1, 3, 2) # broadcasting error
+ assert_raises(ValueError, linalg.solve, a, b)
+ assert_raises(ValueError, linalg.solve, a[0:0], b[0:0])
+
+ # Test zero "single equations" with 0x0 matrices.
+ b = np.arange(2).reshape(1, 2).view(ArraySubclass)
+ expected = linalg.solve(a, b)[:, 0:0]
+ result = linalg.solve(a[:, 0:0, 0:0], b[:, 0:0])
+ assert_array_equal(result, expected)
+ assert_(isinstance(result, ArraySubclass))
+
+ b = np.arange(3).reshape(1, 3)
+ assert_raises(ValueError, linalg.solve, a, b)
+ assert_raises(ValueError, linalg.solve, a[0:0], b[0:0])
+ assert_raises(ValueError, linalg.solve, a[:, 0:0, 0:0], b)
+
+ def test_0_size_k(self):
+ # test zero multiple equation (K=0) case.
+ class ArraySubclass(np.ndarray):
+ pass
+ a = np.arange(4).reshape(1, 2, 2)
+ b = np.arange(6).reshape(3, 2, 1).view(ArraySubclass)
+
+ expected = linalg.solve(a, b)[:, :, 0:0]
+ result = linalg.solve(a, b[:, :, 0:0])
+ assert_array_equal(result, expected)
+ assert_(isinstance(result, ArraySubclass))
+
+ # test both zero.
+ expected = linalg.solve(a, b)[:, 0:0, 0:0]
+ result = linalg.solve(a[:, 0:0, 0:0], b[:, 0:0, 0:0])
+ assert_array_equal(result, expected)
+ assert_(isinstance(result, ArraySubclass))
+
+
+class InvCases(LinalgSquareTestCase, LinalgGeneralizedSquareTestCase):
+
+ def do(self, a, b, tags):
+ a_inv = linalg.inv(a)
+ assert_almost_equal(dot_generalized(a, a_inv),
+ identity_like_generalized(a))
+ assert_(consistent_subclass(a_inv, a))
+
+
+class TestInv(InvCases):
+ @pytest.mark.parametrize('dtype', [single, double, csingle, cdouble])
+ def test_types(self, dtype):
+ x = np.array([[1, 0.5], [0.5, 1]], dtype=dtype)
+ assert_equal(linalg.inv(x).dtype, dtype)
+
+ def test_0_size(self):
+ # Check that all kinds of 0-sized arrays work
+ class ArraySubclass(np.ndarray):
+ pass
+ a = np.zeros((0, 1, 1), dtype=np.int_).view(ArraySubclass)
+ res = linalg.inv(a)
+ assert_(res.dtype.type is np.float64)
+ assert_equal(a.shape, res.shape)
+ assert_(isinstance(res, ArraySubclass))
+
+ a = np.zeros((0, 0), dtype=np.complex64).view(ArraySubclass)
+ res = linalg.inv(a)
+ assert_(res.dtype.type is np.complex64)
+ assert_equal(a.shape, res.shape)
+ assert_(isinstance(res, ArraySubclass))
+
+
+class EigvalsCases(LinalgSquareTestCase, LinalgGeneralizedSquareTestCase):
+
+ def do(self, a, b, tags):
+ ev = linalg.eigvals(a)
+ evalues, evectors = linalg.eig(a)
+ assert_almost_equal(ev, evalues)
+
+
+class TestEigvals(EigvalsCases):
+ @pytest.mark.parametrize('dtype', [single, double, csingle, cdouble])
+ def test_types(self, dtype):
+ x = np.array([[1, 0.5], [0.5, 1]], dtype=dtype)
+ assert_equal(linalg.eigvals(x).dtype, dtype)
+ x = np.array([[1, 0.5], [-1, 1]], dtype=dtype)
+ assert_equal(linalg.eigvals(x).dtype, get_complex_dtype(dtype))
+
+ def test_0_size(self):
+ # Check that all kinds of 0-sized arrays work
+ class ArraySubclass(np.ndarray):
+ pass
+ a = np.zeros((0, 1, 1), dtype=np.int_).view(ArraySubclass)
+ res = linalg.eigvals(a)
+ assert_(res.dtype.type is np.float64)
+ assert_equal((0, 1), res.shape)
+ # This is just for documentation, it might make sense to change:
+ assert_(isinstance(res, np.ndarray))
+
+ a = np.zeros((0, 0), dtype=np.complex64).view(ArraySubclass)
+ res = linalg.eigvals(a)
+ assert_(res.dtype.type is np.complex64)
+ assert_equal((0,), res.shape)
+ # This is just for documentation, it might make sense to change:
+ assert_(isinstance(res, np.ndarray))
+
+
+class EigCases(LinalgSquareTestCase, LinalgGeneralizedSquareTestCase):
+
+ def do(self, a, b, tags):
+ res = linalg.eig(a)
+ eigenvalues, eigenvectors = res.eigenvalues, res.eigenvectors
+ assert_allclose(dot_generalized(a, eigenvectors),
+ np.asarray(eigenvectors) * np.asarray(eigenvalues)[..., None, :],
+ rtol=get_rtol(eigenvalues.dtype))
+ assert_(consistent_subclass(eigenvectors, a))
+
+
+class TestEig(EigCases):
+ @pytest.mark.parametrize('dtype', [single, double, csingle, cdouble])
+ def test_types(self, dtype):
+ x = np.array([[1, 0.5], [0.5, 1]], dtype=dtype)
+ w, v = np.linalg.eig(x)
+ assert_equal(w.dtype, dtype)
+ assert_equal(v.dtype, dtype)
+
+ x = np.array([[1, 0.5], [-1, 1]], dtype=dtype)
+ w, v = np.linalg.eig(x)
+ assert_equal(w.dtype, get_complex_dtype(dtype))
+ assert_equal(v.dtype, get_complex_dtype(dtype))
+
+ def test_0_size(self):
+ # Check that all kinds of 0-sized arrays work
+ class ArraySubclass(np.ndarray):
+ pass
+ a = np.zeros((0, 1, 1), dtype=np.int_).view(ArraySubclass)
+ res, res_v = linalg.eig(a)
+ assert_(res_v.dtype.type is np.float64)
+ assert_(res.dtype.type is np.float64)
+ assert_equal(a.shape, res_v.shape)
+ assert_equal((0, 1), res.shape)
+ # This is just for documentation, it might make sense to change:
+ assert_(isinstance(a, np.ndarray))
+
+ a = np.zeros((0, 0), dtype=np.complex64).view(ArraySubclass)
+ res, res_v = linalg.eig(a)
+ assert_(res_v.dtype.type is np.complex64)
+ assert_(res.dtype.type is np.complex64)
+ assert_equal(a.shape, res_v.shape)
+ assert_equal((0,), res.shape)
+ # This is just for documentation, it might make sense to change:
+ assert_(isinstance(a, np.ndarray))
+
+
+class SVDBaseTests:
+ hermitian = False
+
+ @pytest.mark.parametrize('dtype', [single, double, csingle, cdouble])
+ def test_types(self, dtype):
+ x = np.array([[1, 0.5], [0.5, 1]], dtype=dtype)
+ res = linalg.svd(x)
+ U, S, Vh = res.U, res.S, res.Vh
+ assert_equal(U.dtype, dtype)
+ assert_equal(S.dtype, get_real_dtype(dtype))
+ assert_equal(Vh.dtype, dtype)
+ s = linalg.svd(x, compute_uv=False, hermitian=self.hermitian)
+ assert_equal(s.dtype, get_real_dtype(dtype))
+
+
+class SVDCases(LinalgSquareTestCase, LinalgGeneralizedSquareTestCase):
+
+ def do(self, a, b, tags):
+ u, s, vt = linalg.svd(a, False)
+ assert_allclose(a, dot_generalized(np.asarray(u) * np.asarray(s)[..., None, :],
+ np.asarray(vt)),
+ rtol=get_rtol(u.dtype))
+ assert_(consistent_subclass(u, a))
+ assert_(consistent_subclass(vt, a))
+
+
+class TestSVD(SVDCases, SVDBaseTests):
+ def test_empty_identity(self):
+ """ Empty input should put an identity matrix in u or vh """
+ x = np.empty((4, 0))
+ u, s, vh = linalg.svd(x, compute_uv=True, hermitian=self.hermitian)
+ assert_equal(u.shape, (4, 4))
+ assert_equal(vh.shape, (0, 0))
+ assert_equal(u, np.eye(4))
+
+ x = np.empty((0, 4))
+ u, s, vh = linalg.svd(x, compute_uv=True, hermitian=self.hermitian)
+ assert_equal(u.shape, (0, 0))
+ assert_equal(vh.shape, (4, 4))
+ assert_equal(vh, np.eye(4))
+
+
+class SVDHermitianCases(HermitianTestCase, HermitianGeneralizedTestCase):
+
+ def do(self, a, b, tags):
+ u, s, vt = linalg.svd(a, False, hermitian=True)
+ assert_allclose(a, dot_generalized(np.asarray(u) * np.asarray(s)[..., None, :],
+ np.asarray(vt)),
+ rtol=get_rtol(u.dtype))
+ def hermitian(mat):
+ axes = list(range(mat.ndim))
+ axes[-1], axes[-2] = axes[-2], axes[-1]
+ return np.conj(np.transpose(mat, axes=axes))
+
+ assert_almost_equal(np.matmul(u, hermitian(u)), np.broadcast_to(np.eye(u.shape[-1]), u.shape))
+ assert_almost_equal(np.matmul(vt, hermitian(vt)), np.broadcast_to(np.eye(vt.shape[-1]), vt.shape))
+ assert_equal(np.sort(s)[..., ::-1], s)
+ assert_(consistent_subclass(u, a))
+ assert_(consistent_subclass(vt, a))
+
+
+class TestSVDHermitian(SVDHermitianCases, SVDBaseTests):
+ hermitian = True
+
+
+class CondCases(LinalgSquareTestCase, LinalgGeneralizedSquareTestCase):
+ # cond(x, p) for p in (None, 2, -2)
+
+ def do(self, a, b, tags):
+ c = asarray(a) # a might be a matrix
+ if 'size-0' in tags:
+ assert_raises(LinAlgError, linalg.cond, c)
+ return
+
+ # +-2 norms
+ s = linalg.svd(c, compute_uv=False)
+ assert_almost_equal(
+ linalg.cond(a), s[..., 0] / s[..., -1],
+ single_decimal=5, double_decimal=11)
+ assert_almost_equal(
+ linalg.cond(a, 2), s[..., 0] / s[..., -1],
+ single_decimal=5, double_decimal=11)
+ assert_almost_equal(
+ linalg.cond(a, -2), s[..., -1] / s[..., 0],
+ single_decimal=5, double_decimal=11)
+
+ # Other norms
+ cinv = np.linalg.inv(c)
+ assert_almost_equal(
+ linalg.cond(a, 1),
+ abs(c).sum(-2).max(-1) * abs(cinv).sum(-2).max(-1),
+ single_decimal=5, double_decimal=11)
+ assert_almost_equal(
+ linalg.cond(a, -1),
+ abs(c).sum(-2).min(-1) * abs(cinv).sum(-2).min(-1),
+ single_decimal=5, double_decimal=11)
+ assert_almost_equal(
+ linalg.cond(a, np.inf),
+ abs(c).sum(-1).max(-1) * abs(cinv).sum(-1).max(-1),
+ single_decimal=5, double_decimal=11)
+ assert_almost_equal(
+ linalg.cond(a, -np.inf),
+ abs(c).sum(-1).min(-1) * abs(cinv).sum(-1).min(-1),
+ single_decimal=5, double_decimal=11)
+ assert_almost_equal(
+ linalg.cond(a, 'fro'),
+ np.sqrt((abs(c)**2).sum(-1).sum(-1)
+ * (abs(cinv)**2).sum(-1).sum(-1)),
+ single_decimal=5, double_decimal=11)
+
+
+class TestCond(CondCases):
+ def test_basic_nonsvd(self):
+ # Smoketest the non-svd norms
+ A = array([[1., 0, 1], [0, -2., 0], [0, 0, 3.]])
+ assert_almost_equal(linalg.cond(A, inf), 4)
+ assert_almost_equal(linalg.cond(A, -inf), 2/3)
+ assert_almost_equal(linalg.cond(A, 1), 4)
+ assert_almost_equal(linalg.cond(A, -1), 0.5)
+ assert_almost_equal(linalg.cond(A, 'fro'), np.sqrt(265 / 12))
+
+ def test_singular(self):
+ # Singular matrices have infinite condition number for
+ # positive norms, and negative norms shouldn't raise
+ # exceptions
+ As = [np.zeros((2, 2)), np.ones((2, 2))]
+ p_pos = [None, 1, 2, 'fro']
+ p_neg = [-1, -2]
+ for A, p in itertools.product(As, p_pos):
+ # Inversion may not hit exact infinity, so just check the
+ # number is large
+ assert_(linalg.cond(A, p) > 1e15)
+ for A, p in itertools.product(As, p_neg):
+ linalg.cond(A, p)
+
+ @pytest.mark.xfail(True, run=False,
+ reason="Platform/LAPACK-dependent failure, "
+ "see gh-18914")
+ def test_nan(self):
+ # nans should be passed through, not converted to infs
+ ps = [None, 1, -1, 2, -2, 'fro']
+ p_pos = [None, 1, 2, 'fro']
+
+ A = np.ones((2, 2))
+ A[0,1] = np.nan
+ for p in ps:
+ c = linalg.cond(A, p)
+ assert_(isinstance(c, np.float_))
+ assert_(np.isnan(c))
+
+ A = np.ones((3, 2, 2))
+ A[1,0,1] = np.nan
+ for p in ps:
+ c = linalg.cond(A, p)
+ assert_(np.isnan(c[1]))
+ if p in p_pos:
+ assert_(c[0] > 1e15)
+ assert_(c[2] > 1e15)
+ else:
+ assert_(not np.isnan(c[0]))
+ assert_(not np.isnan(c[2]))
+
+ def test_stacked_singular(self):
+ # Check behavior when only some of the stacked matrices are
+ # singular
+ np.random.seed(1234)
+ A = np.random.rand(2, 2, 2, 2)
+ A[0,0] = 0
+ A[1,1] = 0
+
+ for p in (None, 1, 2, 'fro', -1, -2):
+ c = linalg.cond(A, p)
+ assert_equal(c[0,0], np.inf)
+ assert_equal(c[1,1], np.inf)
+ assert_(np.isfinite(c[0,1]))
+ assert_(np.isfinite(c[1,0]))
+
+
+class PinvCases(LinalgSquareTestCase,
+ LinalgNonsquareTestCase,
+ LinalgGeneralizedSquareTestCase,
+ LinalgGeneralizedNonsquareTestCase):
+
+ def do(self, a, b, tags):
+ a_ginv = linalg.pinv(a)
+ # `a @ a_ginv == I` does not hold if a is singular
+ dot = dot_generalized
+ assert_almost_equal(dot(dot(a, a_ginv), a), a, single_decimal=5, double_decimal=11)
+ assert_(consistent_subclass(a_ginv, a))
+
+
+class TestPinv(PinvCases):
+ pass
+
+
+class PinvHermitianCases(HermitianTestCase, HermitianGeneralizedTestCase):
+
+ def do(self, a, b, tags):
+ a_ginv = linalg.pinv(a, hermitian=True)
+ # `a @ a_ginv == I` does not hold if a is singular
+ dot = dot_generalized
+ assert_almost_equal(dot(dot(a, a_ginv), a), a, single_decimal=5, double_decimal=11)
+ assert_(consistent_subclass(a_ginv, a))
+
+
+class TestPinvHermitian(PinvHermitianCases):
+ pass
+
+
+class DetCases(LinalgSquareTestCase, LinalgGeneralizedSquareTestCase):
+
+ def do(self, a, b, tags):
+ d = linalg.det(a)
+ res = linalg.slogdet(a)
+ s, ld = res.sign, res.logabsdet
+ if asarray(a).dtype.type in (single, double):
+ ad = asarray(a).astype(double)
+ else:
+ ad = asarray(a).astype(cdouble)
+ ev = linalg.eigvals(ad)
+ assert_almost_equal(d, multiply.reduce(ev, axis=-1))
+ assert_almost_equal(s * np.exp(ld), multiply.reduce(ev, axis=-1))
+
+ s = np.atleast_1d(s)
+ ld = np.atleast_1d(ld)
+ m = (s != 0)
+ assert_almost_equal(np.abs(s[m]), 1)
+ assert_equal(ld[~m], -inf)
+
+
+class TestDet(DetCases):
+ def test_zero(self):
+ assert_equal(linalg.det([[0.0]]), 0.0)
+ assert_equal(type(linalg.det([[0.0]])), double)
+ assert_equal(linalg.det([[0.0j]]), 0.0)
+ assert_equal(type(linalg.det([[0.0j]])), cdouble)
+
+ assert_equal(linalg.slogdet([[0.0]]), (0.0, -inf))
+ assert_equal(type(linalg.slogdet([[0.0]])[0]), double)
+ assert_equal(type(linalg.slogdet([[0.0]])[1]), double)
+ assert_equal(linalg.slogdet([[0.0j]]), (0.0j, -inf))
+ assert_equal(type(linalg.slogdet([[0.0j]])[0]), cdouble)
+ assert_equal(type(linalg.slogdet([[0.0j]])[1]), double)
+
+ @pytest.mark.parametrize('dtype', [single, double, csingle, cdouble])
+ def test_types(self, dtype):
+ x = np.array([[1, 0.5], [0.5, 1]], dtype=dtype)
+ assert_equal(np.linalg.det(x).dtype, dtype)
+ ph, s = np.linalg.slogdet(x)
+ assert_equal(s.dtype, get_real_dtype(dtype))
+ assert_equal(ph.dtype, dtype)
+
+ def test_0_size(self):
+ a = np.zeros((0, 0), dtype=np.complex64)
+ res = linalg.det(a)
+ assert_equal(res, 1.)
+ assert_(res.dtype.type is np.complex64)
+ res = linalg.slogdet(a)
+ assert_equal(res, (1, 0))
+ assert_(res[0].dtype.type is np.complex64)
+ assert_(res[1].dtype.type is np.float32)
+
+ a = np.zeros((0, 0), dtype=np.float64)
+ res = linalg.det(a)
+ assert_equal(res, 1.)
+ assert_(res.dtype.type is np.float64)
+ res = linalg.slogdet(a)
+ assert_equal(res, (1, 0))
+ assert_(res[0].dtype.type is np.float64)
+ assert_(res[1].dtype.type is np.float64)
+
+
+class LstsqCases(LinalgSquareTestCase, LinalgNonsquareTestCase):
+
+ def do(self, a, b, tags):
+ arr = np.asarray(a)
+ m, n = arr.shape
+ u, s, vt = linalg.svd(a, False)
+ x, residuals, rank, sv = linalg.lstsq(a, b, rcond=-1)
+ if m == 0:
+ assert_((x == 0).all())
+ if m <= n:
+ assert_almost_equal(b, dot(a, x))
+ assert_equal(rank, m)
+ else:
+ assert_equal(rank, n)
+ assert_almost_equal(sv, sv.__array_wrap__(s))
+ if rank == n and m > n:
+ expect_resids = (
+ np.asarray(abs(np.dot(a, x) - b)) ** 2).sum(axis=0)
+ expect_resids = np.asarray(expect_resids)
+ if np.asarray(b).ndim == 1:
+ expect_resids.shape = (1,)
+ assert_equal(residuals.shape, expect_resids.shape)
+ else:
+ expect_resids = np.array([]).view(type(x))
+ assert_almost_equal(residuals, expect_resids)
+ assert_(np.issubdtype(residuals.dtype, np.floating))
+ assert_(consistent_subclass(x, b))
+ assert_(consistent_subclass(residuals, b))
+
+
+class TestLstsq(LstsqCases):
+ def test_future_rcond(self):
+ a = np.array([[0., 1., 0., 1., 2., 0.],
+ [0., 2., 0., 0., 1., 0.],
+ [1., 0., 1., 0., 0., 4.],
+ [0., 0., 0., 2., 3., 0.]]).T
+
+ b = np.array([1, 0, 0, 0, 0, 0])
+ with suppress_warnings() as sup:
+ w = sup.record(FutureWarning, "`rcond` parameter will change")
+ x, residuals, rank, s = linalg.lstsq(a, b)
+ assert_(rank == 4)
+ x, residuals, rank, s = linalg.lstsq(a, b, rcond=-1)
+ assert_(rank == 4)
+ x, residuals, rank, s = linalg.lstsq(a, b, rcond=None)
+ assert_(rank == 3)
+ # Warning should be raised exactly once (first command)
+ assert_(len(w) == 1)
+
+ @pytest.mark.parametrize(["m", "n", "n_rhs"], [
+ (4, 2, 2),
+ (0, 4, 1),
+ (0, 4, 2),
+ (4, 0, 1),
+ (4, 0, 2),
+ (4, 2, 0),
+ (0, 0, 0)
+ ])
+ def test_empty_a_b(self, m, n, n_rhs):
+ a = np.arange(m * n).reshape(m, n)
+ b = np.ones((m, n_rhs))
+ x, residuals, rank, s = linalg.lstsq(a, b, rcond=None)
+ if m == 0:
+ assert_((x == 0).all())
+ assert_equal(x.shape, (n, n_rhs))
+ assert_equal(residuals.shape, ((n_rhs,) if m > n else (0,)))
+ if m > n and n_rhs > 0:
+ # residuals are exactly the squared norms of b's columns
+ r = b - np.dot(a, x)
+ assert_almost_equal(residuals, (r * r).sum(axis=-2))
+ assert_equal(rank, min(m, n))
+ assert_equal(s.shape, (min(m, n),))
+
+ def test_incompatible_dims(self):
+ # use modified version of docstring example
+ x = np.array([0, 1, 2, 3])
+ y = np.array([-1, 0.2, 0.9, 2.1, 3.3])
+ A = np.vstack([x, np.ones(len(x))]).T
+ with assert_raises_regex(LinAlgError, "Incompatible dimensions"):
+ linalg.lstsq(A, y, rcond=None)
+
+
+@pytest.mark.parametrize('dt', [np.dtype(c) for c in '?bBhHiIqQefdgFDGO'])
+class TestMatrixPower:
+
+ rshft_0 = np.eye(4)
+ rshft_1 = rshft_0[[3, 0, 1, 2]]
+ rshft_2 = rshft_0[[2, 3, 0, 1]]
+ rshft_3 = rshft_0[[1, 2, 3, 0]]
+ rshft_all = [rshft_0, rshft_1, rshft_2, rshft_3]
+ noninv = array([[1, 0], [0, 0]])
+ stacked = np.block([[[rshft_0]]]*2)
+ #FIXME the 'e' dtype might work in future
+ dtnoinv = [object, np.dtype('e'), np.dtype('g'), np.dtype('G')]
+
+ def test_large_power(self, dt):
+ rshft = self.rshft_1.astype(dt)
+ assert_equal(
+ matrix_power(rshft, 2**100 + 2**10 + 2**5 + 0), self.rshft_0)
+ assert_equal(
+ matrix_power(rshft, 2**100 + 2**10 + 2**5 + 1), self.rshft_1)
+ assert_equal(
+ matrix_power(rshft, 2**100 + 2**10 + 2**5 + 2), self.rshft_2)
+ assert_equal(
+ matrix_power(rshft, 2**100 + 2**10 + 2**5 + 3), self.rshft_3)
+
+ def test_power_is_zero(self, dt):
+ def tz(M):
+ mz = matrix_power(M, 0)
+ assert_equal(mz, identity_like_generalized(M))
+ assert_equal(mz.dtype, M.dtype)
+
+ for mat in self.rshft_all:
+ tz(mat.astype(dt))
+ if dt != object:
+ tz(self.stacked.astype(dt))
+
+ def test_power_is_one(self, dt):
+ def tz(mat):
+ mz = matrix_power(mat, 1)
+ assert_equal(mz, mat)
+ assert_equal(mz.dtype, mat.dtype)
+
+ for mat in self.rshft_all:
+ tz(mat.astype(dt))
+ if dt != object:
+ tz(self.stacked.astype(dt))
+
+ def test_power_is_two(self, dt):
+ def tz(mat):
+ mz = matrix_power(mat, 2)
+ mmul = matmul if mat.dtype != object else dot
+ assert_equal(mz, mmul(mat, mat))
+ assert_equal(mz.dtype, mat.dtype)
+
+ for mat in self.rshft_all:
+ tz(mat.astype(dt))
+ if dt != object:
+ tz(self.stacked.astype(dt))
+
+ def test_power_is_minus_one(self, dt):
+ def tz(mat):
+ invmat = matrix_power(mat, -1)
+ mmul = matmul if mat.dtype != object else dot
+ assert_almost_equal(
+ mmul(invmat, mat), identity_like_generalized(mat))
+
+ for mat in self.rshft_all:
+ if dt not in self.dtnoinv:
+ tz(mat.astype(dt))
+
+ def test_exceptions_bad_power(self, dt):
+ mat = self.rshft_0.astype(dt)
+ assert_raises(TypeError, matrix_power, mat, 1.5)
+ assert_raises(TypeError, matrix_power, mat, [1])
+
+ def test_exceptions_non_square(self, dt):
+ assert_raises(LinAlgError, matrix_power, np.array([1], dt), 1)
+ assert_raises(LinAlgError, matrix_power, np.array([[1], [2]], dt), 1)
+ assert_raises(LinAlgError, matrix_power, np.ones((4, 3, 2), dt), 1)
+
+ @pytest.mark.skipif(IS_WASM, reason="fp errors don't work in wasm")
+ def test_exceptions_not_invertible(self, dt):
+ if dt in self.dtnoinv:
+ return
+ mat = self.noninv.astype(dt)
+ assert_raises(LinAlgError, matrix_power, mat, -1)
+
+
+class TestEigvalshCases(HermitianTestCase, HermitianGeneralizedTestCase):
+
+ def do(self, a, b, tags):
+ # note that eigenvalue arrays returned by eig must be sorted since
+ # their order isn't guaranteed.
+ ev = linalg.eigvalsh(a, 'L')
+ evalues, evectors = linalg.eig(a)
+ evalues.sort(axis=-1)
+ assert_allclose(ev, evalues, rtol=get_rtol(ev.dtype))
+
+ ev2 = linalg.eigvalsh(a, 'U')
+ assert_allclose(ev2, evalues, rtol=get_rtol(ev.dtype))
+
+
+class TestEigvalsh:
+ @pytest.mark.parametrize('dtype', [single, double, csingle, cdouble])
+ def test_types(self, dtype):
+ x = np.array([[1, 0.5], [0.5, 1]], dtype=dtype)
+ w = np.linalg.eigvalsh(x)
+ assert_equal(w.dtype, get_real_dtype(dtype))
+
+ def test_invalid(self):
+ x = np.array([[1, 0.5], [0.5, 1]], dtype=np.float32)
+ assert_raises(ValueError, np.linalg.eigvalsh, x, UPLO="lrong")
+ assert_raises(ValueError, np.linalg.eigvalsh, x, "lower")
+ assert_raises(ValueError, np.linalg.eigvalsh, x, "upper")
+
+ def test_UPLO(self):
+ Klo = np.array([[0, 0], [1, 0]], dtype=np.double)
+ Kup = np.array([[0, 1], [0, 0]], dtype=np.double)
+ tgt = np.array([-1, 1], dtype=np.double)
+ rtol = get_rtol(np.double)
+
+ # Check default is 'L'
+ w = np.linalg.eigvalsh(Klo)
+ assert_allclose(w, tgt, rtol=rtol)
+ # Check 'L'
+ w = np.linalg.eigvalsh(Klo, UPLO='L')
+ assert_allclose(w, tgt, rtol=rtol)
+ # Check 'l'
+ w = np.linalg.eigvalsh(Klo, UPLO='l')
+ assert_allclose(w, tgt, rtol=rtol)
+ # Check 'U'
+ w = np.linalg.eigvalsh(Kup, UPLO='U')
+ assert_allclose(w, tgt, rtol=rtol)
+ # Check 'u'
+ w = np.linalg.eigvalsh(Kup, UPLO='u')
+ assert_allclose(w, tgt, rtol=rtol)
+
+ def test_0_size(self):
+ # Check that all kinds of 0-sized arrays work
+ class ArraySubclass(np.ndarray):
+ pass
+ a = np.zeros((0, 1, 1), dtype=np.int_).view(ArraySubclass)
+ res = linalg.eigvalsh(a)
+ assert_(res.dtype.type is np.float64)
+ assert_equal((0, 1), res.shape)
+ # This is just for documentation, it might make sense to change:
+ assert_(isinstance(res, np.ndarray))
+
+ a = np.zeros((0, 0), dtype=np.complex64).view(ArraySubclass)
+ res = linalg.eigvalsh(a)
+ assert_(res.dtype.type is np.float32)
+ assert_equal((0,), res.shape)
+ # This is just for documentation, it might make sense to change:
+ assert_(isinstance(res, np.ndarray))
+
+
+class TestEighCases(HermitianTestCase, HermitianGeneralizedTestCase):
+
+ def do(self, a, b, tags):
+ # note that eigenvalue arrays returned by eig must be sorted since
+ # their order isn't guaranteed.
+ res = linalg.eigh(a)
+ ev, evc = res.eigenvalues, res.eigenvectors
+ evalues, evectors = linalg.eig(a)
+ evalues.sort(axis=-1)
+ assert_almost_equal(ev, evalues)
+
+ assert_allclose(dot_generalized(a, evc),
+ np.asarray(ev)[..., None, :] * np.asarray(evc),
+ rtol=get_rtol(ev.dtype))
+
+ ev2, evc2 = linalg.eigh(a, 'U')
+ assert_almost_equal(ev2, evalues)
+
+ assert_allclose(dot_generalized(a, evc2),
+ np.asarray(ev2)[..., None, :] * np.asarray(evc2),
+ rtol=get_rtol(ev.dtype), err_msg=repr(a))
+
+
+class TestEigh:
+ @pytest.mark.parametrize('dtype', [single, double, csingle, cdouble])
+ def test_types(self, dtype):
+ x = np.array([[1, 0.5], [0.5, 1]], dtype=dtype)
+ w, v = np.linalg.eigh(x)
+ assert_equal(w.dtype, get_real_dtype(dtype))
+ assert_equal(v.dtype, dtype)
+
+ def test_invalid(self):
+ x = np.array([[1, 0.5], [0.5, 1]], dtype=np.float32)
+ assert_raises(ValueError, np.linalg.eigh, x, UPLO="lrong")
+ assert_raises(ValueError, np.linalg.eigh, x, "lower")
+ assert_raises(ValueError, np.linalg.eigh, x, "upper")
+
+ def test_UPLO(self):
+ Klo = np.array([[0, 0], [1, 0]], dtype=np.double)
+ Kup = np.array([[0, 1], [0, 0]], dtype=np.double)
+ tgt = np.array([-1, 1], dtype=np.double)
+ rtol = get_rtol(np.double)
+
+ # Check default is 'L'
+ w, v = np.linalg.eigh(Klo)
+ assert_allclose(w, tgt, rtol=rtol)
+ # Check 'L'
+ w, v = np.linalg.eigh(Klo, UPLO='L')
+ assert_allclose(w, tgt, rtol=rtol)
+ # Check 'l'
+ w, v = np.linalg.eigh(Klo, UPLO='l')
+ assert_allclose(w, tgt, rtol=rtol)
+ # Check 'U'
+ w, v = np.linalg.eigh(Kup, UPLO='U')
+ assert_allclose(w, tgt, rtol=rtol)
+ # Check 'u'
+ w, v = np.linalg.eigh(Kup, UPLO='u')
+ assert_allclose(w, tgt, rtol=rtol)
+
+ def test_0_size(self):
+ # Check that all kinds of 0-sized arrays work
+ class ArraySubclass(np.ndarray):
+ pass
+ a = np.zeros((0, 1, 1), dtype=np.int_).view(ArraySubclass)
+ res, res_v = linalg.eigh(a)
+ assert_(res_v.dtype.type is np.float64)
+ assert_(res.dtype.type is np.float64)
+ assert_equal(a.shape, res_v.shape)
+ assert_equal((0, 1), res.shape)
+ # This is just for documentation, it might make sense to change:
+ assert_(isinstance(a, np.ndarray))
+
+ a = np.zeros((0, 0), dtype=np.complex64).view(ArraySubclass)
+ res, res_v = linalg.eigh(a)
+ assert_(res_v.dtype.type is np.complex64)
+ assert_(res.dtype.type is np.float32)
+ assert_equal(a.shape, res_v.shape)
+ assert_equal((0,), res.shape)
+ # This is just for documentation, it might make sense to change:
+ assert_(isinstance(a, np.ndarray))
+
+
+class _TestNormBase:
+ dt = None
+ dec = None
+
+ @staticmethod
+ def check_dtype(x, res):
+ if issubclass(x.dtype.type, np.inexact):
+ assert_equal(res.dtype, x.real.dtype)
+ else:
+ # For integer input, don't have to test float precision of output.
+ assert_(issubclass(res.dtype.type, np.floating))
+
+
+class _TestNormGeneral(_TestNormBase):
+
+ def test_empty(self):
+ assert_equal(norm([]), 0.0)
+ assert_equal(norm(array([], dtype=self.dt)), 0.0)
+ assert_equal(norm(atleast_2d(array([], dtype=self.dt))), 0.0)
+
+ def test_vector_return_type(self):
+ a = np.array([1, 0, 1])
+
+ exact_types = np.typecodes['AllInteger']
+ inexact_types = np.typecodes['AllFloat']
+
+ all_types = exact_types + inexact_types
+
+ for each_type in all_types:
+ at = a.astype(each_type)
+
+ an = norm(at, -np.inf)
+ self.check_dtype(at, an)
+ assert_almost_equal(an, 0.0)
+
+ with suppress_warnings() as sup:
+ sup.filter(RuntimeWarning, "divide by zero encountered")
+ an = norm(at, -1)
+ self.check_dtype(at, an)
+ assert_almost_equal(an, 0.0)
+
+ an = norm(at, 0)
+ self.check_dtype(at, an)
+ assert_almost_equal(an, 2)
+
+ an = norm(at, 1)
+ self.check_dtype(at, an)
+ assert_almost_equal(an, 2.0)
+
+ an = norm(at, 2)
+ self.check_dtype(at, an)
+ assert_almost_equal(an, an.dtype.type(2.0)**an.dtype.type(1.0/2.0))
+
+ an = norm(at, 4)
+ self.check_dtype(at, an)
+ assert_almost_equal(an, an.dtype.type(2.0)**an.dtype.type(1.0/4.0))
+
+ an = norm(at, np.inf)
+ self.check_dtype(at, an)
+ assert_almost_equal(an, 1.0)
+
+ def test_vector(self):
+ a = [1, 2, 3, 4]
+ b = [-1, -2, -3, -4]
+ c = [-1, 2, -3, 4]
+
+ def _test(v):
+ np.testing.assert_almost_equal(norm(v), 30 ** 0.5,
+ decimal=self.dec)
+ np.testing.assert_almost_equal(norm(v, inf), 4.0,
+ decimal=self.dec)
+ np.testing.assert_almost_equal(norm(v, -inf), 1.0,
+ decimal=self.dec)
+ np.testing.assert_almost_equal(norm(v, 1), 10.0,
+ decimal=self.dec)
+ np.testing.assert_almost_equal(norm(v, -1), 12.0 / 25,
+ decimal=self.dec)
+ np.testing.assert_almost_equal(norm(v, 2), 30 ** 0.5,
+ decimal=self.dec)
+ np.testing.assert_almost_equal(norm(v, -2), ((205. / 144) ** -0.5),
+ decimal=self.dec)
+ np.testing.assert_almost_equal(norm(v, 0), 4,
+ decimal=self.dec)
+
+ for v in (a, b, c,):
+ _test(v)
+
+ for v in (array(a, dtype=self.dt), array(b, dtype=self.dt),
+ array(c, dtype=self.dt)):
+ _test(v)
+
+ def test_axis(self):
+ # Vector norms.
+ # Compare the use of `axis` with computing the norm of each row
+ # or column separately.
+ A = array([[1, 2, 3], [4, 5, 6]], dtype=self.dt)
+ for order in [None, -1, 0, 1, 2, 3, np.Inf, -np.Inf]:
+ expected0 = [norm(A[:, k], ord=order) for k in range(A.shape[1])]
+ assert_almost_equal(norm(A, ord=order, axis=0), expected0)
+ expected1 = [norm(A[k, :], ord=order) for k in range(A.shape[0])]
+ assert_almost_equal(norm(A, ord=order, axis=1), expected1)
+
+ # Matrix norms.
+ B = np.arange(1, 25, dtype=self.dt).reshape(2, 3, 4)
+ nd = B.ndim
+ for order in [None, -2, 2, -1, 1, np.Inf, -np.Inf, 'fro']:
+ for axis in itertools.combinations(range(-nd, nd), 2):
+ row_axis, col_axis = axis
+ if row_axis < 0:
+ row_axis += nd
+ if col_axis < 0:
+ col_axis += nd
+ if row_axis == col_axis:
+ assert_raises(ValueError, norm, B, ord=order, axis=axis)
+ else:
+ n = norm(B, ord=order, axis=axis)
+
+ # The logic using k_index only works for nd = 3.
+ # This has to be changed if nd is increased.
+ k_index = nd - (row_axis + col_axis)
+ if row_axis < col_axis:
+ expected = [norm(B[:].take(k, axis=k_index), ord=order)
+ for k in range(B.shape[k_index])]
+ else:
+ expected = [norm(B[:].take(k, axis=k_index).T, ord=order)
+ for k in range(B.shape[k_index])]
+ assert_almost_equal(n, expected)
+
+ def test_keepdims(self):
+ A = np.arange(1, 25, dtype=self.dt).reshape(2, 3, 4)
+
+ allclose_err = 'order {0}, axis = {1}'
+ shape_err = 'Shape mismatch found {0}, expected {1}, order={2}, axis={3}'
+
+ # check the order=None, axis=None case
+ expected = norm(A, ord=None, axis=None)
+ found = norm(A, ord=None, axis=None, keepdims=True)
+ assert_allclose(np.squeeze(found), expected,
+ err_msg=allclose_err.format(None, None))
+ expected_shape = (1, 1, 1)
+ assert_(found.shape == expected_shape,
+ shape_err.format(found.shape, expected_shape, None, None))
+
+ # Vector norms.
+ for order in [None, -1, 0, 1, 2, 3, np.Inf, -np.Inf]:
+ for k in range(A.ndim):
+ expected = norm(A, ord=order, axis=k)
+ found = norm(A, ord=order, axis=k, keepdims=True)
+ assert_allclose(np.squeeze(found), expected,
+ err_msg=allclose_err.format(order, k))
+ expected_shape = list(A.shape)
+ expected_shape[k] = 1
+ expected_shape = tuple(expected_shape)
+ assert_(found.shape == expected_shape,
+ shape_err.format(found.shape, expected_shape, order, k))
+
+ # Matrix norms.
+ for order in [None, -2, 2, -1, 1, np.Inf, -np.Inf, 'fro', 'nuc']:
+ for k in itertools.permutations(range(A.ndim), 2):
+ expected = norm(A, ord=order, axis=k)
+ found = norm(A, ord=order, axis=k, keepdims=True)
+ assert_allclose(np.squeeze(found), expected,
+ err_msg=allclose_err.format(order, k))
+ expected_shape = list(A.shape)
+ expected_shape[k[0]] = 1
+ expected_shape[k[1]] = 1
+ expected_shape = tuple(expected_shape)
+ assert_(found.shape == expected_shape,
+ shape_err.format(found.shape, expected_shape, order, k))
+
+
+class _TestNorm2D(_TestNormBase):
+ # Define the part for 2d arrays separately, so we can subclass this
+ # and run the tests using np.matrix in matrixlib.tests.test_matrix_linalg.
+ array = np.array
+
+ def test_matrix_empty(self):
+ assert_equal(norm(self.array([[]], dtype=self.dt)), 0.0)
+
+ def test_matrix_return_type(self):
+ a = self.array([[1, 0, 1], [0, 1, 1]])
+
+ exact_types = np.typecodes['AllInteger']
+
+ # float32, complex64, float64, complex128 types are the only types
+ # allowed by `linalg`, which performs the matrix operations used
+ # within `norm`.
+ inexact_types = 'fdFD'
+
+ all_types = exact_types + inexact_types
+
+ for each_type in all_types:
+ at = a.astype(each_type)
+
+ an = norm(at, -np.inf)
+ self.check_dtype(at, an)
+ assert_almost_equal(an, 2.0)
+
+ with suppress_warnings() as sup:
+ sup.filter(RuntimeWarning, "divide by zero encountered")
+ an = norm(at, -1)
+ self.check_dtype(at, an)
+ assert_almost_equal(an, 1.0)
+
+ an = norm(at, 1)
+ self.check_dtype(at, an)
+ assert_almost_equal(an, 2.0)
+
+ an = norm(at, 2)
+ self.check_dtype(at, an)
+ assert_almost_equal(an, 3.0**(1.0/2.0))
+
+ an = norm(at, -2)
+ self.check_dtype(at, an)
+ assert_almost_equal(an, 1.0)
+
+ an = norm(at, np.inf)
+ self.check_dtype(at, an)
+ assert_almost_equal(an, 2.0)
+
+ an = norm(at, 'fro')
+ self.check_dtype(at, an)
+ assert_almost_equal(an, 2.0)
+
+ an = norm(at, 'nuc')
+ self.check_dtype(at, an)
+ # Lower bar needed to support low precision floats.
+ # They end up being off by 1 in the 7th place.
+ np.testing.assert_almost_equal(an, 2.7320508075688772, decimal=6)
+
+ def test_matrix_2x2(self):
+ A = self.array([[1, 3], [5, 7]], dtype=self.dt)
+ assert_almost_equal(norm(A), 84 ** 0.5)
+ assert_almost_equal(norm(A, 'fro'), 84 ** 0.5)
+ assert_almost_equal(norm(A, 'nuc'), 10.0)
+ assert_almost_equal(norm(A, inf), 12.0)
+ assert_almost_equal(norm(A, -inf), 4.0)
+ assert_almost_equal(norm(A, 1), 10.0)
+ assert_almost_equal(norm(A, -1), 6.0)
+ assert_almost_equal(norm(A, 2), 9.1231056256176615)
+ assert_almost_equal(norm(A, -2), 0.87689437438234041)
+
+ assert_raises(ValueError, norm, A, 'nofro')
+ assert_raises(ValueError, norm, A, -3)
+ assert_raises(ValueError, norm, A, 0)
+
+ def test_matrix_3x3(self):
+ # This test has been added because the 2x2 example
+ # happened to have equal nuclear norm and induced 1-norm.
+ # The 1/10 scaling factor accommodates the absolute tolerance
+ # used in assert_almost_equal.
+ A = (1 / 10) * \
+ self.array([[1, 2, 3], [6, 0, 5], [3, 2, 1]], dtype=self.dt)
+ assert_almost_equal(norm(A), (1 / 10) * 89 ** 0.5)
+ assert_almost_equal(norm(A, 'fro'), (1 / 10) * 89 ** 0.5)
+ assert_almost_equal(norm(A, 'nuc'), 1.3366836911774836)
+ assert_almost_equal(norm(A, inf), 1.1)
+ assert_almost_equal(norm(A, -inf), 0.6)
+ assert_almost_equal(norm(A, 1), 1.0)
+ assert_almost_equal(norm(A, -1), 0.4)
+ assert_almost_equal(norm(A, 2), 0.88722940323461277)
+ assert_almost_equal(norm(A, -2), 0.19456584790481812)
+
+ def test_bad_args(self):
+ # Check that bad arguments raise the appropriate exceptions.
+
+ A = self.array([[1, 2, 3], [4, 5, 6]], dtype=self.dt)
+ B = np.arange(1, 25, dtype=self.dt).reshape(2, 3, 4)
+
+ # Using `axis=<integer>` or passing in a 1-D array implies vector
+ # norms are being computed, so also using `ord='fro'`
+ # or `ord='nuc'` or any other string raises a ValueError.
+ assert_raises(ValueError, norm, A, 'fro', 0)
+ assert_raises(ValueError, norm, A, 'nuc', 0)
+ assert_raises(ValueError, norm, [3, 4], 'fro', None)
+ assert_raises(ValueError, norm, [3, 4], 'nuc', None)
+ assert_raises(ValueError, norm, [3, 4], 'test', None)
+
+ # Similarly, norm should raise an exception when ord is any finite
+ # number other than 1, 2, -1 or -2 when computing matrix norms.
+ for order in [0, 3]:
+ assert_raises(ValueError, norm, A, order, None)
+ assert_raises(ValueError, norm, A, order, (0, 1))
+ assert_raises(ValueError, norm, B, order, (1, 2))
+
+ # Invalid axis
+ assert_raises(np.AxisError, norm, B, None, 3)
+ assert_raises(np.AxisError, norm, B, None, (2, 3))
+ assert_raises(ValueError, norm, B, None, (0, 1, 2))
+
+
+class _TestNorm(_TestNorm2D, _TestNormGeneral):
+ pass
+
+
+class TestNorm_NonSystematic:
+
+ def test_longdouble_norm(self):
+ # Non-regression test: p-norm of longdouble would previously raise
+ # UnboundLocalError.
+ x = np.arange(10, dtype=np.longdouble)
+ old_assert_almost_equal(norm(x, ord=3), 12.65, decimal=2)
+
+ def test_intmin(self):
+ # Non-regression test: p-norm of signed integer would previously do
+ # float cast and abs in the wrong order.
+ x = np.array([-2 ** 31], dtype=np.int32)
+ old_assert_almost_equal(norm(x, ord=3), 2 ** 31, decimal=5)
+
+ def test_complex_high_ord(self):
+ # gh-4156
+ d = np.empty((2,), dtype=np.clongdouble)
+ d[0] = 6 + 7j
+ d[1] = -6 + 7j
+ res = 11.615898132184
+ old_assert_almost_equal(np.linalg.norm(d, ord=3), res, decimal=10)
+ d = d.astype(np.complex128)
+ old_assert_almost_equal(np.linalg.norm(d, ord=3), res, decimal=9)
+ d = d.astype(np.complex64)
+ old_assert_almost_equal(np.linalg.norm(d, ord=3), res, decimal=5)
+
+
+# Separate definitions so we can use them for matrix tests.
+class _TestNormDoubleBase(_TestNormBase):
+ dt = np.double
+ dec = 12
+
+
+class _TestNormSingleBase(_TestNormBase):
+ dt = np.float32
+ dec = 6
+
+
+class _TestNormInt64Base(_TestNormBase):
+ dt = np.int64
+ dec = 12
+
+
+class TestNormDouble(_TestNorm, _TestNormDoubleBase):
+ pass
+
+
+class TestNormSingle(_TestNorm, _TestNormSingleBase):
+ pass
+
+
+class TestNormInt64(_TestNorm, _TestNormInt64Base):
+ pass
+
+
+class TestMatrixRank:
+
+ def test_matrix_rank(self):
+ # Full rank matrix
+ assert_equal(4, matrix_rank(np.eye(4)))
+ # rank deficient matrix
+ I = np.eye(4)
+ I[-1, -1] = 0.
+ assert_equal(matrix_rank(I), 3)
+ # All zeros - zero rank
+ assert_equal(matrix_rank(np.zeros((4, 4))), 0)
+ # 1 dimension - rank 1 unless all 0
+ assert_equal(matrix_rank([1, 0, 0, 0]), 1)
+ assert_equal(matrix_rank(np.zeros((4,))), 0)
+ # accepts array-like
+ assert_equal(matrix_rank([1]), 1)
+ # greater than 2 dimensions treated as stacked matrices
+ ms = np.array([I, np.eye(4), np.zeros((4,4))])
+ assert_equal(matrix_rank(ms), np.array([3, 4, 0]))
+ # works on scalar
+ assert_equal(matrix_rank(1), 1)
+
+ def test_symmetric_rank(self):
+ assert_equal(4, matrix_rank(np.eye(4), hermitian=True))
+ assert_equal(1, matrix_rank(np.ones((4, 4)), hermitian=True))
+ assert_equal(0, matrix_rank(np.zeros((4, 4)), hermitian=True))
+ # rank deficient matrix
+ I = np.eye(4)
+ I[-1, -1] = 0.
+ assert_equal(3, matrix_rank(I, hermitian=True))
+ # manually supplied tolerance
+ I[-1, -1] = 1e-8
+ assert_equal(4, matrix_rank(I, hermitian=True, tol=0.99e-8))
+ assert_equal(3, matrix_rank(I, hermitian=True, tol=1.01e-8))
+
+
+def test_reduced_rank():
+ # Test matrices with reduced rank
+ rng = np.random.RandomState(20120714)
+ for i in range(100):
+ # Make a rank deficient matrix
+ X = rng.normal(size=(40, 10))
+ X[:, 0] = X[:, 1] + X[:, 2]
+ # Assert that matrix_rank detected deficiency
+ assert_equal(matrix_rank(X), 9)
+ X[:, 3] = X[:, 4] + X[:, 5]
+ assert_equal(matrix_rank(X), 8)
+
+
+class TestQR:
+ # Define the array class here, so run this on matrices elsewhere.
+ array = np.array
+
+ def check_qr(self, a):
+ # This test expects the argument `a` to be an ndarray or
+ # a subclass of an ndarray of inexact type.
+ a_type = type(a)
+ a_dtype = a.dtype
+ m, n = a.shape
+ k = min(m, n)
+
+ # mode == 'complete'
+ res = linalg.qr(a, mode='complete')
+ Q, R = res.Q, res.R
+ assert_(Q.dtype == a_dtype)
+ assert_(R.dtype == a_dtype)
+ assert_(isinstance(Q, a_type))
+ assert_(isinstance(R, a_type))
+ assert_(Q.shape == (m, m))
+ assert_(R.shape == (m, n))
+ assert_almost_equal(dot(Q, R), a)
+ assert_almost_equal(dot(Q.T.conj(), Q), np.eye(m))
+ assert_almost_equal(np.triu(R), R)
+
+ # mode == 'reduced'
+ q1, r1 = linalg.qr(a, mode='reduced')
+ assert_(q1.dtype == a_dtype)
+ assert_(r1.dtype == a_dtype)
+ assert_(isinstance(q1, a_type))
+ assert_(isinstance(r1, a_type))
+ assert_(q1.shape == (m, k))
+ assert_(r1.shape == (k, n))
+ assert_almost_equal(dot(q1, r1), a)
+ assert_almost_equal(dot(q1.T.conj(), q1), np.eye(k))
+ assert_almost_equal(np.triu(r1), r1)
+
+ # mode == 'r'
+ r2 = linalg.qr(a, mode='r')
+ assert_(r2.dtype == a_dtype)
+ assert_(isinstance(r2, a_type))
+ assert_almost_equal(r2, r1)
+
+
+ @pytest.mark.parametrize(["m", "n"], [
+ (3, 0),
+ (0, 3),
+ (0, 0)
+ ])
+ def test_qr_empty(self, m, n):
+ k = min(m, n)
+ a = np.empty((m, n))
+
+ self.check_qr(a)
+
+ h, tau = np.linalg.qr(a, mode='raw')
+ assert_equal(h.dtype, np.double)
+ assert_equal(tau.dtype, np.double)
+ assert_equal(h.shape, (n, m))
+ assert_equal(tau.shape, (k,))
+
+ def test_mode_raw(self):
+ # The factorization is not unique and varies between libraries,
+ # so it is not possible to check against known values. Functional
+ # testing is a possibility, but awaits the exposure of more
+ # of the functions in lapack_lite. Consequently, this test is
+ # very limited in scope. Note that the results are in FORTRAN
+ # order, hence the h arrays are transposed.
+ a = self.array([[1, 2], [3, 4], [5, 6]], dtype=np.double)
+
+ # Test double
+ h, tau = linalg.qr(a, mode='raw')
+ assert_(h.dtype == np.double)
+ assert_(tau.dtype == np.double)
+ assert_(h.shape == (2, 3))
+ assert_(tau.shape == (2,))
+
+ h, tau = linalg.qr(a.T, mode='raw')
+ assert_(h.dtype == np.double)
+ assert_(tau.dtype == np.double)
+ assert_(h.shape == (3, 2))
+ assert_(tau.shape == (2,))
+
+ def test_mode_all_but_economic(self):
+ a = self.array([[1, 2], [3, 4]])
+ b = self.array([[1, 2], [3, 4], [5, 6]])
+ for dt in "fd":
+ m1 = a.astype(dt)
+ m2 = b.astype(dt)
+ self.check_qr(m1)
+ self.check_qr(m2)
+ self.check_qr(m2.T)
+
+ for dt in "fd":
+ m1 = 1 + 1j * a.astype(dt)
+ m2 = 1 + 1j * b.astype(dt)
+ self.check_qr(m1)
+ self.check_qr(m2)
+ self.check_qr(m2.T)
+
+ def check_qr_stacked(self, a):
+ # This test expects the argument `a` to be an ndarray or
+ # a subclass of an ndarray of inexact type.
+ a_type = type(a)
+ a_dtype = a.dtype
+ m, n = a.shape[-2:]
+ k = min(m, n)
+
+ # mode == 'complete'
+ q, r = linalg.qr(a, mode='complete')
+ assert_(q.dtype == a_dtype)
+ assert_(r.dtype == a_dtype)
+ assert_(isinstance(q, a_type))
+ assert_(isinstance(r, a_type))
+ assert_(q.shape[-2:] == (m, m))
+ assert_(r.shape[-2:] == (m, n))
+ assert_almost_equal(matmul(q, r), a)
+ I_mat = np.identity(q.shape[-1])
+ stack_I_mat = np.broadcast_to(I_mat,
+ q.shape[:-2] + (q.shape[-1],)*2)
+ assert_almost_equal(matmul(swapaxes(q, -1, -2).conj(), q), stack_I_mat)
+ assert_almost_equal(np.triu(r[..., :, :]), r)
+
+ # mode == 'reduced'
+ q1, r1 = linalg.qr(a, mode='reduced')
+ assert_(q1.dtype == a_dtype)
+ assert_(r1.dtype == a_dtype)
+ assert_(isinstance(q1, a_type))
+ assert_(isinstance(r1, a_type))
+ assert_(q1.shape[-2:] == (m, k))
+ assert_(r1.shape[-2:] == (k, n))
+ assert_almost_equal(matmul(q1, r1), a)
+ I_mat = np.identity(q1.shape[-1])
+ stack_I_mat = np.broadcast_to(I_mat,
+ q1.shape[:-2] + (q1.shape[-1],)*2)
+ assert_almost_equal(matmul(swapaxes(q1, -1, -2).conj(), q1),
+ stack_I_mat)
+ assert_almost_equal(np.triu(r1[..., :, :]), r1)
+
+ # mode == 'r'
+ r2 = linalg.qr(a, mode='r')
+ assert_(r2.dtype == a_dtype)
+ assert_(isinstance(r2, a_type))
+ assert_almost_equal(r2, r1)
+
+ @pytest.mark.parametrize("size", [
+ (3, 4), (4, 3), (4, 4),
+ (3, 0), (0, 3)])
+ @pytest.mark.parametrize("outer_size", [
+ (2, 2), (2,), (2, 3, 4)])
+ @pytest.mark.parametrize("dt", [
+ np.single, np.double,
+ np.csingle, np.cdouble])
+ def test_stacked_inputs(self, outer_size, size, dt):
+
+ A = np.random.normal(size=outer_size + size).astype(dt)
+ B = np.random.normal(size=outer_size + size).astype(dt)
+ self.check_qr_stacked(A)
+ self.check_qr_stacked(A + 1.j*B)
+
+
+class TestCholesky:
+ # TODO: are there no other tests for cholesky?
+
+ @pytest.mark.parametrize(
+ 'shape', [(1, 1), (2, 2), (3, 3), (50, 50), (3, 10, 10)]
+ )
+ @pytest.mark.parametrize(
+ 'dtype', (np.float32, np.float64, np.complex64, np.complex128)
+ )
+ def test_basic_property(self, shape, dtype):
+ # Check A = L L^H
+ np.random.seed(1)
+ a = np.random.randn(*shape)
+ if np.issubdtype(dtype, np.complexfloating):
+ a = a + 1j*np.random.randn(*shape)
+
+ t = list(range(len(shape)))
+ t[-2:] = -1, -2
+
+ a = np.matmul(a.transpose(t).conj(), a)
+ a = np.asarray(a, dtype=dtype)
+
+ c = np.linalg.cholesky(a)
+
+ b = np.matmul(c, c.transpose(t).conj())
+ with np._no_nep50_warning():
+ atol = 500 * a.shape[0] * np.finfo(dtype).eps
+ assert_allclose(b, a, atol=atol, err_msg=f'{shape} {dtype}\n{a}\n{c}')
+
+ def test_0_size(self):
+ class ArraySubclass(np.ndarray):
+ pass
+ a = np.zeros((0, 1, 1), dtype=np.int_).view(ArraySubclass)
+ res = linalg.cholesky(a)
+ assert_equal(a.shape, res.shape)
+ assert_(res.dtype.type is np.float64)
+ # for documentation purpose:
+ assert_(isinstance(res, np.ndarray))
+
+ a = np.zeros((1, 0, 0), dtype=np.complex64).view(ArraySubclass)
+ res = linalg.cholesky(a)
+ assert_equal(a.shape, res.shape)
+ assert_(res.dtype.type is np.complex64)
+ assert_(isinstance(res, np.ndarray))
+
+
+def test_byteorder_check():
+ # Byte order check should pass for native order
+ if sys.byteorder == 'little':
+ native = '<'
+ else:
+ native = '>'
+
+ for dtt in (np.float32, np.float64):
+ arr = np.eye(4, dtype=dtt)
+ n_arr = arr.newbyteorder(native)
+ sw_arr = arr.newbyteorder('S').byteswap()
+ assert_equal(arr.dtype.byteorder, '=')
+ for routine in (linalg.inv, linalg.det, linalg.pinv):
+ # Normal call
+ res = routine(arr)
+ # Native but not '='
+ assert_array_equal(res, routine(n_arr))
+ # Swapped
+ assert_array_equal(res, routine(sw_arr))
+
+
+@pytest.mark.skipif(IS_WASM, reason="fp errors don't work in wasm")
+def test_generalized_raise_multiloop():
+ # It should raise an error even if the error doesn't occur in the
+ # last iteration of the ufunc inner loop
+
+ invertible = np.array([[1, 2], [3, 4]])
+ non_invertible = np.array([[1, 1], [1, 1]])
+
+ x = np.zeros([4, 4, 2, 2])[1::2]
+ x[...] = invertible
+ x[0, 0] = non_invertible
+
+ assert_raises(np.linalg.LinAlgError, np.linalg.inv, x)
+
+
+def test_xerbla_override():
+ # Check that our xerbla has been successfully linked in. If it is not,
+ # the default xerbla routine is called, which prints a message to stdout
+ # and may, or may not, abort the process depending on the LAPACK package.
+
+ XERBLA_OK = 255
+
+ try:
+ pid = os.fork()
+ except (OSError, AttributeError):
+ # fork failed, or not running on POSIX
+ pytest.skip("Not POSIX or fork failed.")
+
+ if pid == 0:
+ # child; close i/o file handles
+ os.close(1)
+ os.close(0)
+ # Avoid producing core files.
+ import resource
+ resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
+ # These calls may abort.
+ try:
+ np.linalg.lapack_lite.xerbla()
+ except ValueError:
+ pass
+ except Exception:
+ os._exit(os.EX_CONFIG)
+
+ try:
+ a = np.array([[1.]])
+ np.linalg.lapack_lite.dorgqr(
+ 1, 1, 1, a,
+ 0, # <- invalid value
+ a, a, 0, 0)
+ except ValueError as e:
+ if "DORGQR parameter number 5" in str(e):
+ # success, reuse error code to mark success as
+ # FORTRAN STOP returns as success.
+ os._exit(XERBLA_OK)
+
+ # Did not abort, but our xerbla was not linked in.
+ os._exit(os.EX_CONFIG)
+ else:
+ # parent
+ pid, status = os.wait()
+ if os.WEXITSTATUS(status) != XERBLA_OK:
+ pytest.skip('Numpy xerbla not linked in.')
+
+
+@pytest.mark.skipif(IS_WASM, reason="Cannot start subprocess")
+@pytest.mark.slow
+def test_sdot_bug_8577():
+ # Regression test that loading certain other libraries does not
+ # result to wrong results in float32 linear algebra.
+ #
+ # There's a bug gh-8577 on OSX that can trigger this, and perhaps
+ # there are also other situations in which it occurs.
+ #
+ # Do the check in a separate process.
+
+ bad_libs = ['PyQt5.QtWidgets', 'IPython']
+
+ template = textwrap.dedent("""
+ import sys
+ {before}
+ try:
+ import {bad_lib}
+ except ImportError:
+ sys.exit(0)
+ {after}
+ x = np.ones(2, dtype=np.float32)
+ sys.exit(0 if np.allclose(x.dot(x), 2.0) else 1)
+ """)
+
+ for bad_lib in bad_libs:
+ code = template.format(before="import numpy as np", after="",
+ bad_lib=bad_lib)
+ subprocess.check_call([sys.executable, "-c", code])
+
+ # Swapped import order
+ code = template.format(after="import numpy as np", before="",
+ bad_lib=bad_lib)
+ subprocess.check_call([sys.executable, "-c", code])
+
+
+class TestMultiDot:
+
+ def test_basic_function_with_three_arguments(self):
+ # multi_dot with three arguments uses a fast hand coded algorithm to
+ # determine the optimal order. Therefore test it separately.
+ A = np.random.random((6, 2))
+ B = np.random.random((2, 6))
+ C = np.random.random((6, 2))
+
+ assert_almost_equal(multi_dot([A, B, C]), A.dot(B).dot(C))
+ assert_almost_equal(multi_dot([A, B, C]), np.dot(A, np.dot(B, C)))
+
+ def test_basic_function_with_two_arguments(self):
+ # separate code path with two arguments
+ A = np.random.random((6, 2))
+ B = np.random.random((2, 6))
+
+ assert_almost_equal(multi_dot([A, B]), A.dot(B))
+ assert_almost_equal(multi_dot([A, B]), np.dot(A, B))
+
+ def test_basic_function_with_dynamic_programming_optimization(self):
+ # multi_dot with four or more arguments uses the dynamic programming
+ # optimization and therefore deserve a separate
+ A = np.random.random((6, 2))
+ B = np.random.random((2, 6))
+ C = np.random.random((6, 2))
+ D = np.random.random((2, 1))
+ assert_almost_equal(multi_dot([A, B, C, D]), A.dot(B).dot(C).dot(D))
+
+ def test_vector_as_first_argument(self):
+ # The first argument can be 1-D
+ A1d = np.random.random(2) # 1-D
+ B = np.random.random((2, 6))
+ C = np.random.random((6, 2))
+ D = np.random.random((2, 2))
+
+ # the result should be 1-D
+ assert_equal(multi_dot([A1d, B, C, D]).shape, (2,))
+
+ def test_vector_as_last_argument(self):
+ # The last argument can be 1-D
+ A = np.random.random((6, 2))
+ B = np.random.random((2, 6))
+ C = np.random.random((6, 2))
+ D1d = np.random.random(2) # 1-D
+
+ # the result should be 1-D
+ assert_equal(multi_dot([A, B, C, D1d]).shape, (6,))
+
+ def test_vector_as_first_and_last_argument(self):
+ # The first and last arguments can be 1-D
+ A1d = np.random.random(2) # 1-D
+ B = np.random.random((2, 6))
+ C = np.random.random((6, 2))
+ D1d = np.random.random(2) # 1-D
+
+ # the result should be a scalar
+ assert_equal(multi_dot([A1d, B, C, D1d]).shape, ())
+
+ def test_three_arguments_and_out(self):
+ # multi_dot with three arguments uses a fast hand coded algorithm to
+ # determine the optimal order. Therefore test it separately.
+ A = np.random.random((6, 2))
+ B = np.random.random((2, 6))
+ C = np.random.random((6, 2))
+
+ out = np.zeros((6, 2))
+ ret = multi_dot([A, B, C], out=out)
+ assert out is ret
+ assert_almost_equal(out, A.dot(B).dot(C))
+ assert_almost_equal(out, np.dot(A, np.dot(B, C)))
+
+ def test_two_arguments_and_out(self):
+ # separate code path with two arguments
+ A = np.random.random((6, 2))
+ B = np.random.random((2, 6))
+ out = np.zeros((6, 6))
+ ret = multi_dot([A, B], out=out)
+ assert out is ret
+ assert_almost_equal(out, A.dot(B))
+ assert_almost_equal(out, np.dot(A, B))
+
+ def test_dynamic_programming_optimization_and_out(self):
+ # multi_dot with four or more arguments uses the dynamic programming
+ # optimization and therefore deserve a separate test
+ A = np.random.random((6, 2))
+ B = np.random.random((2, 6))
+ C = np.random.random((6, 2))
+ D = np.random.random((2, 1))
+ out = np.zeros((6, 1))
+ ret = multi_dot([A, B, C, D], out=out)
+ assert out is ret
+ assert_almost_equal(out, A.dot(B).dot(C).dot(D))
+
+ def test_dynamic_programming_logic(self):
+ # Test for the dynamic programming part
+ # This test is directly taken from Cormen page 376.
+ arrays = [np.random.random((30, 35)),
+ np.random.random((35, 15)),
+ np.random.random((15, 5)),
+ np.random.random((5, 10)),
+ np.random.random((10, 20)),
+ np.random.random((20, 25))]
+ m_expected = np.array([[0., 15750., 7875., 9375., 11875., 15125.],
+ [0., 0., 2625., 4375., 7125., 10500.],
+ [0., 0., 0., 750., 2500., 5375.],
+ [0., 0., 0., 0., 1000., 3500.],
+ [0., 0., 0., 0., 0., 5000.],
+ [0., 0., 0., 0., 0., 0.]])
+ s_expected = np.array([[0, 1, 1, 3, 3, 3],
+ [0, 0, 2, 3, 3, 3],
+ [0, 0, 0, 3, 3, 3],
+ [0, 0, 0, 0, 4, 5],
+ [0, 0, 0, 0, 0, 5],
+ [0, 0, 0, 0, 0, 0]], dtype=int)
+ s_expected -= 1 # Cormen uses 1-based index, python does not.
+
+ s, m = _multi_dot_matrix_chain_order(arrays, return_costs=True)
+
+ # Only the upper triangular part (without the diagonal) is interesting.
+ assert_almost_equal(np.triu(s[:-1, 1:]),
+ np.triu(s_expected[:-1, 1:]))
+ assert_almost_equal(np.triu(m), np.triu(m_expected))
+
+ def test_too_few_input_arrays(self):
+ assert_raises(ValueError, multi_dot, [])
+ assert_raises(ValueError, multi_dot, [np.random.random((3, 3))])
+
+
+class TestTensorinv:
+
+ @pytest.mark.parametrize("arr, ind", [
+ (np.ones((4, 6, 8, 2)), 2),
+ (np.ones((3, 3, 2)), 1),
+ ])
+ def test_non_square_handling(self, arr, ind):
+ with assert_raises(LinAlgError):
+ linalg.tensorinv(arr, ind=ind)
+
+ @pytest.mark.parametrize("shape, ind", [
+ # examples from docstring
+ ((4, 6, 8, 3), 2),
+ ((24, 8, 3), 1),
+ ])
+ def test_tensorinv_shape(self, shape, ind):
+ a = np.eye(24)
+ a.shape = shape
+ ainv = linalg.tensorinv(a=a, ind=ind)
+ expected = a.shape[ind:] + a.shape[:ind]
+ actual = ainv.shape
+ assert_equal(actual, expected)
+
+ @pytest.mark.parametrize("ind", [
+ 0, -2,
+ ])
+ def test_tensorinv_ind_limit(self, ind):
+ a = np.eye(24)
+ a.shape = (4, 6, 8, 3)
+ with assert_raises(ValueError):
+ linalg.tensorinv(a=a, ind=ind)
+
+ def test_tensorinv_result(self):
+ # mimic a docstring example
+ a = np.eye(24)
+ a.shape = (24, 8, 3)
+ ainv = linalg.tensorinv(a, ind=1)
+ b = np.ones(24)
+ assert_allclose(np.tensordot(ainv, b, 1), np.linalg.tensorsolve(a, b))
+
+
+class TestTensorsolve:
+
+ @pytest.mark.parametrize("a, axes", [
+ (np.ones((4, 6, 8, 2)), None),
+ (np.ones((3, 3, 2)), (0, 2)),
+ ])
+ def test_non_square_handling(self, a, axes):
+ with assert_raises(LinAlgError):
+ b = np.ones(a.shape[:2])
+ linalg.tensorsolve(a, b, axes=axes)
+
+ @pytest.mark.parametrize("shape",
+ [(2, 3, 6), (3, 4, 4, 3), (0, 3, 3, 0)],
+ )
+ def test_tensorsolve_result(self, shape):
+ a = np.random.randn(*shape)
+ b = np.ones(a.shape[:2])
+ x = np.linalg.tensorsolve(a, b)
+ assert_allclose(np.tensordot(a, x, axes=len(x.shape)), b)
+
+
+def test_unsupported_commontype():
+ # linalg gracefully handles unsupported type
+ arr = np.array([[1, -2], [2, 5]], dtype='float16')
+ with assert_raises_regex(TypeError, "unsupported in linalg"):
+ linalg.cholesky(arr)
+
+
+#@pytest.mark.slow
+#@pytest.mark.xfail(not HAS_LAPACK64, run=False,
+# reason="Numpy not compiled with 64-bit BLAS/LAPACK")
+#@requires_memory(free_bytes=16e9)
+@pytest.mark.skip(reason="Bad memory reports lead to OOM in ci testing")
+def test_blas64_dot():
+ n = 2**32
+ a = np.zeros([1, n], dtype=np.float32)
+ b = np.ones([1, 1], dtype=np.float32)
+ a[0,-1] = 1
+ c = np.dot(b, a)
+ assert_equal(c[0,-1], 1)
+
+
+@pytest.mark.xfail(not HAS_LAPACK64,
+ reason="Numpy not compiled with 64-bit BLAS/LAPACK")
+def test_blas64_geqrf_lwork_smoketest():
+ # Smoke test LAPACK geqrf lwork call with 64-bit integers
+ dtype = np.float64
+ lapack_routine = np.linalg.lapack_lite.dgeqrf
+
+ m = 2**32 + 1
+ n = 2**32 + 1
+ lda = m
+
+ # Dummy arrays, not referenced by the lapack routine, so don't
+ # need to be of the right size
+ a = np.zeros([1, 1], dtype=dtype)
+ work = np.zeros([1], dtype=dtype)
+ tau = np.zeros([1], dtype=dtype)
+
+ # Size query
+ results = lapack_routine(m, n, a, lda, tau, work, -1, 0)
+ assert_equal(results['info'], 0)
+ assert_equal(results['m'], m)
+ assert_equal(results['n'], m)
+
+ # Should result to an integer of a reasonable size
+ lwork = int(work.item())
+ assert_(2**32 < lwork < 2**42)
diff --git a/.venv/lib/python3.12/site-packages/numpy/linalg/tests/test_regression.py b/.venv/lib/python3.12/site-packages/numpy/linalg/tests/test_regression.py
new file mode 100644
index 00000000..af38443a
--- /dev/null
+++ b/.venv/lib/python3.12/site-packages/numpy/linalg/tests/test_regression.py
@@ -0,0 +1,145 @@
+""" Test functions for linalg module
+"""
+import warnings
+
+import numpy as np
+from numpy import linalg, arange, float64, array, dot, transpose
+from numpy.testing import (
+ assert_, assert_raises, assert_equal, assert_array_equal,
+ assert_array_almost_equal, assert_array_less
+)
+
+
+class TestRegression:
+
+ def test_eig_build(self):
+ # Ticket #652
+ rva = array([1.03221168e+02 + 0.j,
+ -1.91843603e+01 + 0.j,
+ -6.04004526e-01 + 15.84422474j,
+ -6.04004526e-01 - 15.84422474j,
+ -1.13692929e+01 + 0.j,
+ -6.57612485e-01 + 10.41755503j,
+ -6.57612485e-01 - 10.41755503j,
+ 1.82126812e+01 + 0.j,
+ 1.06011014e+01 + 0.j,
+ 7.80732773e+00 + 0.j,
+ -7.65390898e-01 + 0.j,
+ 1.51971555e-15 + 0.j,
+ -1.51308713e-15 + 0.j])
+ a = arange(13 * 13, dtype=float64)
+ a.shape = (13, 13)
+ a = a % 17
+ va, ve = linalg.eig(a)
+ va.sort()
+ rva.sort()
+ assert_array_almost_equal(va, rva)
+
+ def test_eigh_build(self):
+ # Ticket 662.
+ rvals = [68.60568999, 89.57756725, 106.67185574]
+
+ cov = array([[77.70273908, 3.51489954, 15.64602427],
+ [3.51489954, 88.97013878, -1.07431931],
+ [15.64602427, -1.07431931, 98.18223512]])
+
+ vals, vecs = linalg.eigh(cov)
+ assert_array_almost_equal(vals, rvals)
+
+ def test_svd_build(self):
+ # Ticket 627.
+ a = array([[0., 1.], [1., 1.], [2., 1.], [3., 1.]])
+ m, n = a.shape
+ u, s, vh = linalg.svd(a)
+
+ b = dot(transpose(u[:, n:]), a)
+
+ assert_array_almost_equal(b, np.zeros((2, 2)))
+
+ def test_norm_vector_badarg(self):
+ # Regression for #786: Frobenius norm for vectors raises
+ # ValueError.
+ assert_raises(ValueError, linalg.norm, array([1., 2., 3.]), 'fro')
+
+ def test_lapack_endian(self):
+ # For bug #1482
+ a = array([[5.7998084, -2.1825367],
+ [-2.1825367, 9.85910595]], dtype='>f8')
+ b = array(a, dtype='<f8')
+
+ ap = linalg.cholesky(a)
+ bp = linalg.cholesky(b)
+ assert_array_equal(ap, bp)
+
+ def test_large_svd_32bit(self):
+ # See gh-4442, 64bit would require very large/slow matrices.
+ x = np.eye(1000, 66)
+ np.linalg.svd(x)
+
+ def test_svd_no_uv(self):
+ # gh-4733
+ for shape in (3, 4), (4, 4), (4, 3):
+ for t in float, complex:
+ a = np.ones(shape, dtype=t)
+ w = linalg.svd(a, compute_uv=False)
+ c = np.count_nonzero(np.absolute(w) > 0.5)
+ assert_equal(c, 1)
+ assert_equal(np.linalg.matrix_rank(a), 1)
+ assert_array_less(1, np.linalg.norm(a, ord=2))
+
+ def test_norm_object_array(self):
+ # gh-7575
+ testvector = np.array([np.array([0, 1]), 0, 0], dtype=object)
+
+ norm = linalg.norm(testvector)
+ assert_array_equal(norm, [0, 1])
+ assert_(norm.dtype == np.dtype('float64'))
+
+ norm = linalg.norm(testvector, ord=1)
+ assert_array_equal(norm, [0, 1])
+ assert_(norm.dtype != np.dtype('float64'))
+
+ norm = linalg.norm(testvector, ord=2)
+ assert_array_equal(norm, [0, 1])
+ assert_(norm.dtype == np.dtype('float64'))
+
+ assert_raises(ValueError, linalg.norm, testvector, ord='fro')
+ assert_raises(ValueError, linalg.norm, testvector, ord='nuc')
+ assert_raises(ValueError, linalg.norm, testvector, ord=np.inf)
+ assert_raises(ValueError, linalg.norm, testvector, ord=-np.inf)
+ assert_raises(ValueError, linalg.norm, testvector, ord=0)
+ assert_raises(ValueError, linalg.norm, testvector, ord=-1)
+ assert_raises(ValueError, linalg.norm, testvector, ord=-2)
+
+ testmatrix = np.array([[np.array([0, 1]), 0, 0],
+ [0, 0, 0]], dtype=object)
+
+ norm = linalg.norm(testmatrix)
+ assert_array_equal(norm, [0, 1])
+ assert_(norm.dtype == np.dtype('float64'))
+
+ norm = linalg.norm(testmatrix, ord='fro')
+ assert_array_equal(norm, [0, 1])
+ assert_(norm.dtype == np.dtype('float64'))
+
+ assert_raises(TypeError, linalg.norm, testmatrix, ord='nuc')
+ assert_raises(ValueError, linalg.norm, testmatrix, ord=np.inf)
+ assert_raises(ValueError, linalg.norm, testmatrix, ord=-np.inf)
+ assert_raises(ValueError, linalg.norm, testmatrix, ord=0)
+ assert_raises(ValueError, linalg.norm, testmatrix, ord=1)
+ assert_raises(ValueError, linalg.norm, testmatrix, ord=-1)
+ assert_raises(TypeError, linalg.norm, testmatrix, ord=2)
+ assert_raises(TypeError, linalg.norm, testmatrix, ord=-2)
+ assert_raises(ValueError, linalg.norm, testmatrix, ord=3)
+
+ def test_lstsq_complex_larger_rhs(self):
+ # gh-9891
+ size = 20
+ n_rhs = 70
+ G = np.random.randn(size, size) + 1j * np.random.randn(size, size)
+ u = np.random.randn(size, n_rhs) + 1j * np.random.randn(size, n_rhs)
+ b = G.dot(u)
+ # This should work without segmentation fault.
+ u_lstsq, res, rank, sv = linalg.lstsq(G, b, rcond=None)
+ # check results just in case
+ assert_array_almost_equal(u_lstsq, u)