Vfiz dZgdZddlZddlZddlZddlmZmZddlm Z m Z m Z m Z m Z mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z)m*Z+m,Z,m-Z-m.Z.m/Z/m0Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Zm?Z@mAZBmCZCddlDmEZEddlFmGZGddlHmIZIdd lJmKZKmLZLdd lMmNZNmOZOdd lPmQZQGd d eZRGddeZSGddeZTGddeZUGddeZVejWe2jXdZXe$ZYeIdGddeZZ[dZ\dZ]dZ^dZ_dZ`dZad Zbd!Zce6e6eeee6eeiZde6eeeeeeeiZeefd"Zfefd#Zgd$Zhd%Zid&Zjd'Zkd(Zld)Zmd*Znd+Z?dzd,ZoeXeodzd-Zpd.ZqeXeqd/Zrdzd0ZseXesd{d2Ztd3ZueXeud4Zvd5ZweXewd6Zxdd7d8ZyeXeyd9d7d:Zzd;Z{eXe{d<Z0dzd=Z|eXe|d|d?Z}eXeud@Z~dzdAZeXed}dCZeXeudDZeXed}dEZd~dFZeXeddHZdIZeXedJZdzdKZeXedzdLZdddMdNZeXedddMdOZdddMdPZeXedeEdMdQZeXeudRZeXeudSZdzdTZeXedzdUZdzdVZd~dWZeXeddXZddYdZZeXeddYd[Zdzd\Zdd]Zdzd^Zdd_d`ZeXedd_daZdddbdcZeXedddbddZ=ddedfZeXedgdedhZdiZeXedjZ(ddkdlZeXed1dkdmZ;e>> from numpy import linalg as LA >>> LA.inv(np.zeros((2,2))) Traceback (most recent call last): File "", line 1, in 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 N)r\r]r^__doc__r`rarbrrrsrarc td)NzSingular matrixrerrflags rb_raise_linalgerror_singularrzs ' ( ((rac td)NzMatrix is not positive definitervrws rb_raise_linalgerror_nonposdefr|s 7 8 88rac td)NzEigenvalues did not convergervrws rb-_raise_linalgerror_eigenvalues_nonconvergencer~s 4 5 55rac td)NzSVD did not convergervrws rb%_raise_linalgerror_svd_nonconvergencers , - --rac td)Nz,SVD did not converge in Linear Least Squaresrvrws rb_raise_linalgerror_lstsqrs D E EErac td)Nz:Incorrect argument found while performing QR factorizationrvrws rb_raise_linalgerror_qrrs ) * **racTt|}t|d|j}||fS)N__array_wrap__)r-getattrr)anewwraps rb _makearrayrs, !**C 1&(: ; ;D 9rac,t|tSN) issubclassr0)ts rb isComplexTypers a ) ))rac8t||Sr)_real_types_mapgetrdefaults rb _realTypers   q' * **rac8t||Sr)_complex_types_maprrs rb _complexTypers  ! !!W - --racVt}d}|D]}}|jj}t|trSt |rd}t |d}|turt}V|td|jj dvt}~|rt|}t|fSt|fS)NFT)rz array type z is unsupported in linalg) rHdtypetyperr:rrr5 TypeErrornamerr/)arrays result_type is_complexrtype_rts rb _commonTypersKJ  ! !  eW % % !U## "! 5$///BV||$  Uagl U U UVVV!KK#(5  ##{""rac  g}|D]b}|jjdvr=|t||jdM||ct |dkr|dS|S)N)=|rrr")r byteorderappendr- newbyteorderlen)rretarrs rb_to_native_byte_orderrs C 9 j 0 0 JJws#)*@*@*E*EFFF G G G G JJsOOOO 3xx1}}1v  racT|D]$}|jdkrtd|jz%dS)Nz9%d-dimensional array given. Array must be two-dimensionalndimrrrs rb _assert_2drsL 00 6Q;;&()/00 0 00racT|D]$}|jdkrtd|jz%dS)NrB%d-dimensional array given. Array must be at least two-dimensionalrrs rb_assert_stacked_2drsL 99 6A::/12899 9 99rac|D]P} |jdd\}}n%#t$rtd|jzwxYw||krtdQdS)Nrz-Last 2 dimensions of the array must be square)shape ValueErrorrr)rrmns rb_assert_stacked_squarers OO 97233r'rrs rb_assert_finitersM EE{{   ECDD D EEEracX|jdkot|jdddkS)Nr"r)sizerEr)rs rb _is_empty_2drs* 8q= 6T#)BCC.11Q66rac$t|ddS)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 r)rLrs rbrMrMs Ar2  rac ||fSrr`)rbaxess rb_tensorsolve_dispatcherr q6Mrac2t|\}}t|}|j}|att |}|D]-}|||||.||}|j||jz d}d}|D]}||z}|j |dzkrtd| ||}| }|t||} || _| S)aL 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 -------- >>> import numpy as np >>> a = np.eye(2*3*4).reshape((2*3, 4, 2, 3, 4)) >>> rng = np.random.default_rng() >>> b = rng.normal(size=(2*3, 4)) >>> x = np.linalg.tensorsolve(a, b) >>> x.shape (2, 3, 4) >>> np.allclose(np.tensordot(a, x, axes=3), b) True NrrzfInput arrays must satisfy the requirement prod(a.shape[b.ndim:]) == prod(a.shape[:b.ndim]))rr-rlistrangeremoveinsertrMrrrreshaperavelr) rrrranallaxeskoldshaperEress rbrrs*bmmGAt A B uRyy// " "A NN1    NN2q ! ! ! ! KK waf~'H D   v >   $A  A $uQ{{  CCI Jrac ||fSrr`)rrs rb_solve_dispatcherrgrract|\}}t|t|\}}t||\}}|jdkr tj}n tj}t|rdnd}ttdddd5||||}dddn #1swxYwY|| |d S) a 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 (..., M) if b is shape (M,) and (..., M, K) if b is (..., M, K), where the "..." part is broadcasted between a and b. Raises ------ LinAlgError If `a` is singular or not square. See Also -------- scipy.linalg.solve : Similar function in SciPy. Notes ----- 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. .. versionchanged:: 2.0 The b array is only treated as a shape (M,) column vector if it is exactly 1-dimensional. In all other instances it is treated as a stack of (M, K) matrices. Previously b would be treated as a stack of (M,) vectors if b.ndim was equal to a.ndim - 1. 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``: >>> import numpy as np >>> 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 rDD->Ddd->dcallignorerinvalidoverr3under signatureNFcopy) rrrrrVsolve1rrr8rzastype) rr_rrresult_tgufuncrrs rbrrks*T a==DAq1mmGAta##KAx v{{%$(++8I 2F B B B.. F1a9 - - -............... 4.. / //sB22B69B6c|fSrr`)rinds rb_tensorinv_dispatcherr 4Krarc t|}|j}d}|dkr(||d|d|z}||dD]}||z}ntd||d}t |}|j|S)a 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 -------- >>> import numpy as np >>> a = np.eye(4*6).reshape((4, 6, 8, 3)) >>> ainv = np.linalg.tensorinv(a, ind=2) >>> ainv.shape (8, 3, 4, 6) >>> rng = np.random.default_rng() >>> b = rng.normal(size=(4, 6)) >>> np.allclose(np.tensordot(ainv, b), np.linalg.tensorsolve(a, b)) True >>> a = np.eye(4*6).reshape((24, 8, 3)) >>> ainv = np.linalg.tensorinv(a, ind=1) >>> ainv.shape (8, 3, 24) >>> rng = np.random.default_rng() >>> b = rng.normal(size=24) >>> np.allclose(np.tensordot(ainv, b, 1), np.linalg.tensorsolve(a, b)) True rr"NzInvalid ind argument.r)r-rrrr)rrrrEinvshaperias rbrrsn  AwH D QwwCDD>HTcTN2#$$  A AIDD 0111 $A QB 2:x  rac|fSrr`rs rb_unary_dispatcherrrrac\t|\}}t|t|\}}t|rdnd}t t dddd5t j||}dddn #1swxYwY|||d S) a Compute the inverse of a matrix. Given a square matrix `a`, return the matrix `ainv` satisfying ``a @ ainv = ainv @ a = eye(a.shape[0])``. Parameters ---------- a : (..., M, M) array_like Matrix to be inverted. Returns ------- ainv : (..., M, M) ndarray or matrix Inverse of the matrix `a`. Raises ------ LinAlgError If `a` is not square or inversion fails. See Also -------- scipy.linalg.inv : Similar function in SciPy. numpy.linalg.cond : Compute the condition number of a matrix. numpy.linalg.svd : Compute the singular value decomposition of a matrix. Notes ----- Broadcasting rules apply, see the `numpy.linalg` documentation for details. If `a` is detected to be singular, a `LinAlgError` is raised. If `a` is ill-conditioned, a `LinAlgError` may or may not be raised, and results may be inaccurate due to floating-point errors. References ---------- .. [1] Wikipedia, "Condition number", https://en.wikipedia.org/wiki/Condition_number Examples -------- >>> import numpy as np >>> from numpy.linalg import inv >>> a = np.array([[1., 2.], [3., 4.]]) >>> ainv = inv(a) >>> np.allclose(a @ ainv, np.eye(2)) True >>> np.allclose(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]]]) If a matrix is close to singular, the computed inverse may not satisfy ``a @ ainv = ainv @ a = eye(a.shape[0])`` even if a `LinAlgError` is not raised: >>> a = np.array([[2,4,6],[2,0,2],[6,8,14]]) >>> inv(a) # No errors raised array([[-1.12589991e+15, -5.62949953e+14, 5.62949953e+14], [-1.12589991e+15, -5.62949953e+14, 5.62949953e+14], [ 1.12589991e+15, 5.62949953e+14, -5.62949953e+14]]) >>> a @ inv(a) array([[ 0. , -0.5 , 0. ], # may vary [-0.5 , 0.625, 0.25 ], [ 0. , 0. , 1. ]]) To detect ill-conditioned matrices, you can use `numpy.linalg.cond` to compute its *condition number* [1]_. The larger the condition number, the more ill-conditioned the matrix is. As a rule of thumb, if the condition number ``cond(a) = 10**k``, then you may lose up to ``k`` digits of accuracy on top of what would be lost to the numerical method due to loss of precision from arithmetic methods. >>> from numpy.linalg import cond >>> cond(a) np.float64(8.659885634118668e+17) # may vary It is also possible to detect ill-conditioning by inspecting the matrix's singular values directly. The ratio between the largest and the smallest singular value is the condition number: >>> from numpy.linalg import svd >>> sigma = svd(a, compute_uv=False) # Do not compute singular vectors >>> sigma.max()/sigma.min() 8.659885634118668e+17 # may vary D->Dd->drrrrNFr) rrrrr8rzrVrr)rrrrrainvs rbrrsRmmGAt1a..KAx'**6I 2F B B B99 i888999999999999999 4 H5 11 2 22s BB Bc|fSrr`)rrs rb_matrix_power_dispatcherrrract|}t| tj|}n"#t$r}t d|d}~wwxYw|jt krt}n"|jdkrt}ntd|dkr5t|}t|j d|j|d<|S|dkrt|}t|}|d kr|S|dkr |||S|d kr|||||Sdx}}|dkr;||n |||}t!|d\}}|r||n |||}|dk;|S) a 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 -------- >>> import numpy as np >>> 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.]]) zexponent must be an integerNrz6matrix_power not supported for stacks of object arraysr"rr.r)r,roperatorindexrrobjectrrr4NotImplementedErrorr7rRrrr%divmod)rrefmatmulzresultbits rbrrsD 1 A1> N1   >>>566A=>  w& 1! DFF F Avv qMMQWR[000# Q FF FF Avv awq!}} awwwq!}}a((( A a%%AA1 13  A .QQggfa.@.@F a%% Ms5 AAA)upperc|fSrr`)rr s rb_cholesky_dispatcherr rraFc~|r tjn tj}t|\}}t |t |\}}t |rdnd}ttdddd5|||}dddn #1swxYwY|| |d S) a Cholesky decomposition. Return the lower or upper Cholesky decomposition, ``L * L.H`` or ``U.H * U``, of the square matrix ``a``, where ``L`` is lower-triangular, ``U`` is upper-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 or upper-triangular and diagonal elements of ``a`` are used. Only ``L`` or ``U`` is actually returned. Parameters ---------- a : (..., M, M) array_like Hermitian (symmetric if all elements are real), positive-definite input matrix. upper : bool If ``True``, the result must be the upper-triangular Cholesky factor. If ``False``, the result must be the lower-triangular Cholesky factor. Default: ``False``. Returns ------- L : (..., M, M) array_like Lower or upper-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 ----- 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 -------- >>> import numpy as np >>> 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]]) >>> # The upper-triangular Cholesky factor can also be obtained. >>> np.linalg.cholesky(A, upper=True) array([[1.-0.j, 0.-2.j], [0.-0.j, 1.-0.j]]) rrrrrrNFr) rV cholesky_up cholesky_lorrrrr8r|r)rr rrrrrrs rbrr s v+0 N] & &]5NFmmGAt1a..KAx'**6I 3V B B B++ F1 * * *+++++++++++++++ 4.. / //s:BBBc ||fSrr`x1x2s rb_outer_dispatcherrs 8Oract|}t|}|jdks |jdkr td|jd|jdt||dS)a" Compute the outer product of two vectors. This function is Array API compatible. Compared to ``np.outer`` it accepts 1-dimensional inputs only. Parameters ---------- x1 : (M,) array_like One-dimensional input array of size ``N``. Must have a numeric data type. x2 : (N,) array_like One-dimensional input array of size ``M``. Must have a numeric data type. Returns ------- out : (M, N) ndarray ``out[i, j] = a[i] * b[j]`` See also -------- outer Examples -------- Make a (*very* coarse) grid for computing a Mandelbrot set: >>> rl = np.linalg.outer(np.ones((5,)), np.linspace(-2, 2, 5)) >>> rl array([[-2., -1., 0., 1., 2.], [-2., -1., 0., 1., 2.], [-2., -1., 0., 1., 2.], [-2., -1., 0., 1., 2.], [-2., -1., 0., 1., 2.]]) >>> im = np.linalg.outer(1j*np.linspace(2, -2, 5), np.ones((5,))) >>> im array([[0.+2.j, 0.+2.j, 0.+2.j, 0.+2.j, 0.+2.j], [0.+1.j, 0.+1.j, 0.+1.j, 0.+1.j, 0.+1.j], [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j], [0.-1.j, 0.-1.j, 0.-1.j, 0.-1.j, 0.-1.j], [0.-2.j, 0.-2.j, 0.-2.j, 0.-2.j, 0.-2.j]]) >>> grid = rl + im >>> grid array([[-2.+2.j, -1.+2.j, 0.+2.j, 1.+2.j, 2.+2.j], [-2.+1.j, -1.+1.j, 0.+1.j, 1.+1.j, 2.+1.j], [-2.+0.j, -1.+0.j, 0.+0.j, 1.+0.j, 2.+0.j], [-2.-1.j, -1.-1.j, 0.-1.j, 1.-1.j, 2.-1.j], [-2.-2.j, -1.-2.j, 0.-2.j, 1.-2.j, 2.-2.j]]) An example using a "vector" of letters: >>> x = np.array(['a', 'b', 'c'], dtype=object) >>> np.linalg.outer(x, [1, 2, 3]) array([['a', 'aa', 'aaa'], ['b', 'bb', 'bbb'], ['c', 'cc', 'ccc']], dtype=object) rz;Input arrays must be one-dimensional, but they are x1.ndim=z and x2.ndim=.Nout)r,rr _core_outerrs rbrrwsz BB BB w!||rw!|| *w * * g * * *    r24 ( ( ((rac|fSrr`)rmodes rb_qr_dispatcherrrrareducedc|dvr]|dvr!d}tj|tdd}n8|dvr!d}tj|tdd }ntd |d t |\}}t ||jd d \}}t|\}}||d}t|}t||}t|rdnd} ttdddd5tj|| } d d d n #1swxYwY|dkr>t!|dd |d d f} | |d} || S|dkrJt#|} | |d} | |d} || | fS|d kr"||d}||S|dkr||kr|} tj}n|} tj}t|rdnd} ttdddd5||| | } d d d n #1swxYwYt!|dd | d d f} | |d} | |d} t)|| || S)a 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, default: 'reduced' If K = min(M, N), then * 'reduced' : returns Q, R with dimensions (..., M, K), (..., K, N) * '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 ------- 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 ----- When mode is 'reduced' or 'complete', the result will be a namedtuple with the attributes ``Q`` and ``R``. 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 -------- >>> import numpy as np >>> rng = np.random.default_rng() >>> a = rng.normal(size=(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.]) )rcompleterraw)ffullzcThe 'full' option is deprecated in favor of 'reduced'. For backward compatibility let mode default.r) stacklevelr)reconomicz$The 'economic' option is deprecated.r%zUnrecognized mode ''rNTrrrrrrrr.Fr!r rr)warningswarnDeprecationWarningrrrrrrrminrr8rrVqr_r_rawrSrM qr_complete qr_reducedrg)rrmsgrrrrrmnrtaurqmcrs rbrrsL 666 = ?  M#1a @ @ @ @DD & & &8C M#1a @ @ @ @DD:4:::;; ;mmGAtq 7233t|j dkr|j }t|}nt|}||d S) ai 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 ----- 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``: >>> import numpy as np >>> 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 rzd->DrrrrNr"Fr)rrrrrr8r~rVrr'imagrealrrr)rrrrrws rbrrsDLmmGAt11a..KAx'**6I D x  " " "::  !!y 9 9 9:::::::::::::::   . qv{   .A **HH#H--H 88H58 ) ))s/BBBc|fSrr`)rUPLOs rb_eigvalsh_dispatcherr9rraLc|}|dvrtd|dkr tj}n tj}t |\}}t |t|\}}t|rdnd}ttdddd5||| }d d d n #1swxYwY| t|d S) a 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 ----- Broadcasting rules apply, see the `numpy.linalg` documentation for details. The eigenvalues are computed using LAPACK routines ``_syevd``, ``_heevd``. Examples -------- >>> import numpy as np >>> 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 array([1., 6.]) >>> wb array([6.+0.j, 1.+0.j]) r:ro UPLO argument must be 'L' or 'U'r:D->drrrrrNFr) r rrV eigvalsh_lo eigvalsh_uprrrrr8r~rr)rr8rrrrrr6s rbr r s6T ::<>> import numpy as np >>> 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.]]) zD->DDzd->DDrrrrNgFr)rrrrrr8r~rVrr'r4r5rrrrX)rrrrrr6vts rbrrRsuFmmGAt11a..KAx(++8I D x  " " "::!!y9992:::::::::::::::   *AFcM 2 2* F WX&&)) 8% ( (B QXXhUX33TT"XX > >>s/BBBcD|}|dvrtdt|\}}t|t |\}}|dkr t j}n t j}t|rdnd}ttdddd5||| \}}d d d n #1swxYwY| t|d }| |d }t|||S) a 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 ----- 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 -------- >>> import numpy as np >>> 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 array([1., 6.]) >>> wb array([6.+0.j, 1.+0.j]) >>> va array([[-0.4472136 +0.j , -0.89442719+0.j ], # may vary [ 0. +0.89442719j, 0. -0.4472136j ]]) >>> vb array([[ 0.89442719+0.j , -0. +0.4472136j], [-0. +0.4472136j, 0.89442719+0.j ]]) r<r=r:zD->dDd->ddrrrrNFr)r rrrrrVeigh_loeigh_uprr8r~rrre) rr8rrrrrr6rBs rbrrsfx ::<= 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. Returns ------- 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 ----- When `compute_uv` is True, the result is a namedtuple with the following attribute names: `U`, `S`, and `Vh`. 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 -------- >>> import numpy as np >>> rng = np.random.default_rng() >>> a = rng.normal(size=(9, 6)) + 1j*rng.normal(size=(9, 6)) >>> b = rng.normal(size=(2, 7, 8, 3)) + 1j*rng.normal(size=(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 r"N.raxisrzD->DdDzd->dddrrrrFrr>r)numpyrrrGr%r*take_along_axisrM conjugaternr rIrrrrVsvd_fsvd_srr8rrrr )rrHrIrJnprsusgnsidxrBrrrrrrvhs rbr r s|PmmGAt&  &77DAqq''CAA1::c44R4i(D$$S$R$88C""1d"44A""1d3aaa<&8r"BBA1s3aaa<0011;;==BTT!WWab22 2 AAA773"9% %qa..KAx 7233HHh @$8H$&&& 6 6va9555HAq" 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 HHXEH * * HHYx((uH 5 5 YYxeY , ,a!TT"XX...+A..:FFF @$8H$&&& : :!!y999A : : : : : : : : : : : : : : : HHYx((uH 5 5s$?GG!$G! J,,J03J0c|fSrr`xs rb_svdvals_dispatcherr]@rrac&t|ddS)a Returns the singular values of a matrix (or a stack of matrices) ``x``. When x is a stack of matrices, the function will compute the singular values for each matrix in the stack. This function is Array API compatible. Calling ``np.svdvals(x)`` to get singular values is the same as ``np.svd(x, compute_uv=False, hermitian=False)``. Parameters ---------- x : (..., M, N) array_like Input array having shape (..., M, N) and whose last two dimensions form matrices on which to perform singular value decomposition. Should have a floating-point data type. Returns ------- out : ndarray An array with shape (..., K) that contains the vector(s) of singular values of length K, where K = min(M, N). See Also -------- scipy.linalg.svdvals : Compute singular values of a matrix. Examples -------- >>> np.linalg.svdvals([[1, 2, 3, 4, 5], ... [1, 4, 9, 16, 25], ... [1, 8, 27, 64, 125]]) array([146.68862757, 5.57510612, 0.60393245]) Determine the rank of a matrix using singular values: >>> s = np.linalg.svdvals([[1, 2, 3], ... [2, 4, 6], ... [-1, 1, -1]]); s array([8.38434191e+00, 1.64402274e+00, 2.31534378e-16]) >>> np.count_nonzero(s > 1e-10) # Matrix of rank 2 2 FrIrJ)r r[s rbrrDs^ qUe 4 4 44rac|fSrr`)r\ps rb_cond_dispatcherrbvrract|}t|rtd||dvrct|d}t d5|dkr|d |d z }n|d |d z }dddn #1swxYwYnt |t |\}}t|}t|rd nd }t d5tj || }t||dt||dz}dddn #1swxYwY| |d}t|}|r^|t|dz}|jdkr t ||<n!|r|jt }|S)a 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 -------- >>> import numpy as np >>> 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 z#cond is not defined on empty arraysN>rrFrIr)r'r).r).r"rrrrrrMrr")r-rrr r8rrrrrVrrrr?anyrr;rr) r\rarUrrrrinvxnan_masks rbrrzsdd  AAA?@@@yALL e $ $ $ ( # # # + +BwwgJ6*fI' *  + + + + + + + + + + + + + + + q!!!!!nn 8X&&+A..:FFF ( # # # I I $Q)<<>> import numpy as np >>> 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 Nz#`tol` and `rtol` can't be both set.rr"Fr_r.rTrNkeepdimsrM) rr-rintr'r maxrr9repsrBr1)rkrlrJrirps rbrrsx CO>??? Avzzs16{{?### A%9555A { <qwrss|$$uQW~~'99DD4==g.Deede++d2cll3<( Sr * * **rac|fSrr`)rrcondrJris rb_pinv_dispatcherrverrac (t|\}}|H|turd}nU|7t|jddt |jjz}n|}n|turtd t|}t|rE|jdd\}}t|jdd||fz|j}||S| }t|d|\}} } |dtft| d d z} | | k} td | | | } d| | <t!t#| t%| dtft#|}||S)a9 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. Parameters ---------- a : (..., M, N) array_like Matrix or stack of matrices to be pseudo-inverted. rcond : (...) array_like of float, optional 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. Default: ``1e-15``. 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. rtol : (...) array_like of float, optional Same as `rcond`, but it's an Array API compatible parameter name. Only `rcond` or `rtol` can be set at a time. If none of them are provided then NumPy's ``1e-15`` default is used. If ``rtol=None`` is passed then the API standard default is used. .. versionadded:: 2.0.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+``: >>> import numpy as np >>> rng = np.random.default_rng() >>> a = rng.normal(size=(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 NgV瞯>> import numpy as np >>> 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) zD->DdrDrFr) r-rrrrrVr rrk)rrrreal_trrGlogdets rbr r sR  A1a..KAx x F(++8I (i@@@LD& 4;xe , , ,D ]]6] . .F v & &&ract|}t|t|\}}t|rdnd}t j||}||d}|S)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 ----- 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: >>> import numpy as np >>> 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.]) rrrFr)r-rrrrVr r)rrrrrs rbr r 3 sr^  A1a..KAx'**6I!y111A &&A Hrac ||fSrr`)rrrus rb_lstsq_dispatcherrm rracBt|\}}t|\}}|jdk}|r|ddtf}t|||jdd\}}|jdd\}} ||krt dt ||\} } t| } |%t| j t||z}t| rdnd} | dkr+t|jdd|| dzfz|j }ttd d d d 5t!j|||| \}}}}dddn #1swxYwY|dkrd|d <| dkr|d d| f}|d d| f}|r|d}||ks||krt'g| }|| d}|| d}|| d}||||||fS)a 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`. The default uses the machine precision times ``max(M, N)``. Passing ``-1`` will use machine precision. .. versionchanged:: 2.0 Previously, the default was ``-1``, but a warning was given that this would change. 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: >>> import numpy as np >>> 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)[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() rNrzIncompatible dimensionsz DDd->Ddidz ddd->ddidr"rrrrr.rrMFrT)rrrBrrrrrr9rsrrrrNrr8rrVrsqueezer+r)rrrurris_1drrm2n_rhsrr result_real_trr\residsrankrUs rbrrq sB a==DAqmmGAt FaKE  aaajMq! 7233rtAd |d t1|} | |z} t9j| || }|tC||j z}|St-|dkr_|\} }tE| | } tE|| }| |krtAd|dkrtG|| |tHd}n|dkrtG|| |tJ}n|dkrE|| kr|dz}t9jt1|| |d}nX|t.krE| |kr| dz} t9jt1||| d}n|dkrC|| kr|dz}t9jt1|| |}n|t. krC| |kr| dz} t9jt1||| }np|dvr>tt9j||zj |}n.|dkrtG|| |t6d}ntAd|r?tM|j'}d||d<d||d<||}|StAd)a 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 : {int, float, inf, -inf, 'fro', 'nuc'}, optional Order of the norm (see table under ``Notes`` for what values are supported for matrices and vectors respectively). 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. 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`. 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 -------- >>> import numpy as np >>> 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) N)r"frorrK)orderz6'axis' must be None, an integer or a tuple of integersr")rNrprrozInvalid norm order 'z ' for vectorsrzDuplicate axes given.rrMrr)Nrr"nucz Invalid norm order for matrices.z&Improper number of dimensions to norm.)(r-rrrr:rCrfloatrrrr5r4r4rJrtupler isinstancerq Exceptionrrr;r%rrr*rKr&reduceconjstrrrFrTrr(r)rr)r\rrNrprx_realx_imagsqnormrndrrUabsxrr ret_shapes rbrr& s~l  A aglWg$6 7 7 HHUOO |v [ L TQYY AXX$!))c""AQW\** "F++fjj.@.@@qv,,C .kk$!*--J B |U2YY e $ $ t99DD   H  w 4yyA~~ #::q66::4(A:FF F SD[[q66::4(:;; ; AXXa %%$22  AXX:c!ff4(CCC C [C1HHA#A 14(CCCDD DS ! ! FCFFFGG Gq66D SLD*Tx@@@C Js#)444 4CJ Ta!('"55'"55 x  455 5 !88!!XxqAACC BYY!!Xx>>CC AXX(""A *SVV(33377Xq7QQCC CZZ(""A *SVV(33377Xq7QQCC BYY(""A *SVV(33377X7FFCC SD[[(""A *SVV(33377X7FFCC & & &sz16688a<"5DAAABBCC E\\!!Xxa@@CC?@@ @  )QW I!"Id1g !"Id1g ++i((C ABBBsE'' F1FFrc#"K|Ed{V|VdSrr`)rrs rb_multidot_dispatcherr+ s+ IIIIIract|}|dkrtd|dkrt|d|d|Sd|D}|dj|dj}}|djdkrt |d|d<|djdkrt |dj|d<t ||dkr&t|d|d|d|}n&t|}t||d|dz |}|dkr|dkr|d S|dks|dkr| S|S) ao 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. 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:: >>> import numpy as np >>> 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_{10 \times 100}, B_{100 \times 5}, C_{5 \times 50}`. 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 rzExpecting at least two arrays.r"rrc,g|]}t|Sr`)r,.0rs rb zmulti_dot.. s , , ,jmm , , ,rarr)r"r") rrr4rr.Tr_multi_dot_three_multi_dot_matrix_chain_order _multi_dotr)rrr ndim_first ndim_lastrrs rbrr0 smj F A1uu9::: a6!9fQiS1111 , ,V , , ,F#1INF2JO Jay~vay))q  bz!r ++-r  Avv!&)VAYq sKKK-f55FE1a!e===Q9>>d| qINN||~~ rac|j\}}|j\}}||z||zz}||z||zz} || kr tt||||St|t|||S)z 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` r)rr4) rkBCra0a1b0b1c0c1cost1cost2s rbrr swHBwHD" I #E 2Id #E u}}3q!99aS))))1c!QiiS))))ract|}d|D|djdgz}t||ft}t ||ft }t d|D]}t ||z D]{}||z}t|||f<t ||D]W} ||| f|| dz|fz|||| dzz||dzzz} | |||fkr| |||f<| |||f<X||r||fn|S)a Return a np.array that encodes the optimal order of multiplications. 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)]) c(g|]}|jdS)r"rrs rbrz1_multi_dot_matrix_chain_order.. s$$$$$$rarrr)rrrNr5r6r=rr;) r return_costsrrarrUlijrr1s rbrr sP" F A %$V$$$r (8(;'< 0: off-diagonal above the main diagonal. * offset < 0: off-diagonal below the main diagonal. Returns ------- out : (...,min(N,M)) ndarray An array containing the diagonals and whose shape is determined by removing the last two dimensions and appending a dimension equal to the size of the resulting diagonals. The returned array must have the same data type as ``x``. See Also -------- numpy.diagonal Examples -------- >>> a = np.arange(4).reshape(2, 2); a array([[0, 1], [2, 3]]) >>> np.linalg.diagonal(a) array([0, 3]) A 3-D example: >>> a = np.arange(8).reshape(2, 2, 2); a array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]]) >>> np.linalg.diagonal(a) array([[0, 3], [4, 7]]) Diagonals adjacent to the main diagonal can be obtained by using the `offset` argument: >>> a = np.arange(9).reshape(3, 3) >>> a array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) >>> np.linalg.diagonal(a, offset=1) # First superdiagonal array([1, 5]) >>> np.linalg.diagonal(a, offset=2) # Second superdiagonal array([2]) >>> np.linalg.diagonal(a, offset=-1) # First subdiagonal array([3, 7]) >>> np.linalg.diagonal(a, offset=-2) # Second subdiagonal array([6]) The anti-diagonal can be obtained by reversing the order of elements using either `numpy.flipud` or `numpy.fliplr`. >>> a = np.arange(9).reshape(3, 3) >>> a array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) >>> np.linalg.diagonal(np.fliplr(a)) # Horizontal flip array([2, 4, 6]) >>> np.linalg.diagonal(np.flipud(a)) # Vertical flip array([6, 4, 2]) Note that the order in which the diagonal is retrieved varies depending on the flip function. rr)axis1axis2)_core_diagonalrs rbrr sn !V2R 8 8 88ra)rrc|fSrr`r\rrs rb_trace_dispatcherrU rrac*t||dd|S)a Returns the sum along the specified diagonals of a matrix (or a stack of matrices) ``x``. This function is Array API compatible, contrary to :py:func:`numpy.trace`. Parameters ---------- x : (...,M,N) array_like Input array having shape (..., M, N) and whose innermost two dimensions form MxN matrices. offset : int, optional Offset specifying the off-diagonal relative to the main diagonal, where:: * offset = 0: the main diagonal. * offset > 0: off-diagonal above the main diagonal. * offset < 0: off-diagonal below the main diagonal. dtype : dtype, optional Data type of the returned array. Returns ------- out : ndarray An array containing the traces and whose shape is determined by removing the last two dimensions and storing the traces in the last array dimension. For example, if x has rank k and shape: (I, J, K, ..., L, M, N), then an output array has rank k-2 and shape: (I, J, K, ..., L) where:: out[i, j, k, ..., l] = trace(a[i, j, k, ..., l, :, :]) The returned array must have a data type as described by the dtype parameter above. See Also -------- numpy.trace Examples -------- >>> np.linalg.trace(np.eye(3)) 3.0 >>> a = np.arange(8).reshape((2, 2, 2)) >>> np.linalg.trace(a) array([3, 11]) Trace is computed with the last two axes as the 2-d sub-arrays. This behavior differs from :py:func:`numpy.trace` which uses the first two axes by default. >>> a = np.arange(24).reshape((3, 2, 2, 2)) >>> np.linalg.trace(a).shape (3, 2) Traces adjacent to the main diagonal can be obtained by using the `offset` argument: >>> a = np.arange(9).reshape((3, 3)); a array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) >>> np.linalg.trace(a, offset=1) # First superdiagonal 6 >>> np.linalg.trace(a, offset=2) # Second superdiagonal 2 >>> np.linalg.trace(a, offset=-1) # First subdiagonal 10 >>> np.linalg.trace(a, offset=-2) # Second subdiagonal 6 rr)rrr) _core_tracers rbrrY sX q&"E B B BBrarMc ||fSrr`rrrNs rb_cross_dispatcherr s 9rarct|}t|}|j|dks|j|dkr,td|j|d|j|dt|||S)a@ Returns the cross product of 3-element vectors. If ``x1`` and/or ``x2`` are multi-dimensional arrays, then the cross-product of each pair of corresponding 3-element vectors is independently computed. This function is Array API compatible, contrary to :func:`numpy.cross`. Parameters ---------- x1 : array_like The first input array. x2 : array_like The second input array. Must be compatible with ``x1`` for all non-compute axes. The size of the axis over which to compute the cross-product must be the same size as the respective axis in ``x1``. axis : int, optional The axis (dimension) of ``x1`` and ``x2`` containing the vectors for which to compute the cross-product. Default: ``-1``. Returns ------- out : ndarray An array containing the cross products. See Also -------- numpy.cross Examples -------- Vector cross-product. >>> x = np.array([1, 2, 3]) >>> y = np.array([4, 5, 6]) >>> np.linalg.cross(x, y) array([-3, 6, -3]) Multiple vector cross-products. Note that the direction of the cross product vector is defined by the *right-hand rule*. >>> x = np.array([[1,2,3], [4,5,6]]) >>> y = np.array([[4,5,6], [1,2,3]]) >>> np.linalg.cross(x, y) array([[-3, 6, -3], [ 3, -6, 3]]) >>> x = np.array([[1, 2], [3, 4], [5, 6]]) >>> y = np.array([[4, 5], [6, 1], [2, 3]]) >>> np.linalg.cross(x, y, axis=0) array([[-24, 6], [ 18, 24], [-6, -18]]) rzJBoth input arrays must be (arrays of) 3-dimensional vectors, but they are z and z dimensional instead.rM)r,rr _core_crossrs rbrr sx BB BB x~bhtn11 #HTN # #13$ # # #   r2D ) ) ))rac ||fSrr`rs rb_matmul_dispatcherr rrac"t||S)a Computes the matrix product. This function is Array API compatible, contrary to :func:`numpy.matmul`. Parameters ---------- x1 : array_like The first input array. x2 : array_like The second input array. Returns ------- out : ndarray The matrix product of the inputs. This is a scalar only when both ``x1``, ``x2`` are 1-d vectors. Raises ------ ValueError If the last dimension of ``x1`` is not the same size as the second-to-last dimension of ``x2``. If a scalar value is passed in. See Also -------- numpy.matmul Examples -------- For 2-D arrays it is the matrix product: >>> a = np.array([[1, 0], ... [0, 1]]) >>> b = np.array([[4, 1], ... [2, 2]]) >>> np.linalg.matmul(a, b) array([[4, 1], [2, 2]]) For 2-D mixed with 1-D, the result is the usual. >>> a = np.array([[1, 0], ... [0, 1]]) >>> b = np.array([1, 2]) >>> np.linalg.matmul(a, b) array([1, 2]) >>> np.linalg.matmul(b, a) array([1, 2]) Broadcasting is conventional for stacks of arrays >>> a = np.arange(2 * 2 * 4).reshape((2, 2, 4)) >>> b = np.arange(2 * 2 * 4).reshape((2, 4, 2)) >>> np.linalg.matmul(a,b).shape (2, 2, 2) >>> np.linalg.matmul(a, b)[0, 1, 1] 98 >>> sum(a[0, 1, :] * b[0 , :, 1]) 98 Vector, vector returns the scalar inner product, but neither argument is complex-conjugated: >>> np.linalg.matmul([2j, 3j], [2j, 3j]) (-13+0j) Scalar multiplication raises an error. >>> np.linalg.matmul([1,2], 3) Traceback (most recent call last): ... ValueError: matmul: Input operand 1 does not have enough dimensions ... ) _core_matmulrs rbrr sb B  rarc ||fSrr`rrrs rb_tensordot_dispatcherrS rrac&t|||S)Nr)_core_tensordotrs rbrrW s 2r - - --rac|fSrr`r[s rb_matrix_transpose_dispatcherra rrac t|Sr)_core_matrix_transposer[s rbrrd s !! $ $$razQ Notes ----- This function is an alias of `numpy.matrix_transpose`. )rprc|fSrr`r\rprs rb_matrix_norm_dispatcherrs rrarcFt|}t|d||S)aW Computes the matrix norm of a matrix (or a stack of matrices) ``x``. This function is Array API compatible. Parameters ---------- x : array_like Input array having shape (..., M, N) and whose two innermost dimensions form ``MxN`` matrices. 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. Default: False. ord : {1, -1, 2, -2, inf, -inf, 'fro', 'nuc'}, optional The order of the norm. For details see the table under ``Notes`` in `numpy.linalg.norm`. See Also -------- numpy.linalg.norm : Generic norm function 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.matrix_norm(b) 7.745966692414834 >>> LA.matrix_norm(b, ord='fro') 7.745966692414834 >>> LA.matrix_norm(b, ord=np.inf) 9.0 >>> LA.matrix_norm(b, ord=-np.inf) 2.0 >>> LA.matrix_norm(b, ord=1) 7.0 >>> LA.matrix_norm(b, ord=-1) 6.0 >>> LA.matrix_norm(b, ord=2) 7.3484692283495345 >>> LA.matrix_norm(b, ord=-2) 1.8570331885190563e-016 # may vary rerNrpr)r,rrs rbrrv s'l 1 A 8 = = ==rarc|fSrr`)r\rNrprs rb_vector_norm_dispatcherr rrac ttj}|d}nt |t rt |j t fdtjD}||z}t| tfd|Dtgfd|DRd}n|}t||}|rgt |tt|n|t|}|D]} d|| <| t |}|S) a Computes the vector norm of a vector (or batch of vectors) ``x``. This function is Array API compatible. Parameters ---------- x : array_like Input array. axis : {None, int, 2-tuple of ints}, optional If an integer, ``axis`` specifies the axis (dimension) along which to compute vector norms. If an n-tuple, ``axis`` specifies the axes (dimensions) along which to compute batched vector norms. If ``None``, the vector norm must be computed over all array values (i.e., equivalent to computing the vector norm of a flattened array). Default: ``None``. 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. Default: False. ord : {int, float, inf, -inf}, optional The order of the norm. For details see the table under ``Notes`` in `numpy.linalg.norm`. See Also -------- numpy.linalg.norm : Generic norm function Examples -------- >>> from numpy import linalg as LA >>> a = np.arange(9) + 1 >>> a array([1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> b = a.reshape((3, 3)) >>> b array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> LA.vector_norm(b) 16.881943016134134 >>> LA.vector_norm(b, ord=np.inf) 9.0 >>> LA.vector_norm(b, ord=-np.inf) 1.0 >>> LA.vector_norm(b, ord=0) 9.0 >>> LA.vector_norm(b, ord=1) 45.0 >>> LA.vector_norm(b, ord=-1) 0.3534857623790153 >>> LA.vector_norm(b, ord=2) 16.881943016134134 >>> LA.vector_norm(b, ord=-2) 0.8058837395885292 Nr"c3$K|] }|v|V dSrr`)rrnormalized_axiss rb zvector_norm.. s-JJ1/1I1IQ1I1I1I1IJJrac*g|]}j|Sr`rrrr\s rbrzvector_norm.. s///Qagaj///rarc*g|]}j|Sr`rrs rbrzvector_norm..s+++!'!*+++ra)rNrr)r,rrrrrrUrr_core_transposerrErqrr) r\rNrprr_axisrestnewshaperrrs ` @rbr r sx 1 A MME | GGII D% /tQV<<JJJJaf JJJJJ$; Ax ( ( 0 0////$///s;;; ++++d+++      qu# & & &C(%!%E#e**   4U    AE!HHkk%,,'' Jrac ||fSrr`rs rb_vecdot_dispatcherrrrac&t|||S)a Computes the vector dot product. This function is restricted to arguments compatible with the Array API, contrary to :func:`numpy.vecdot`. Let :math:`\mathbf{a}` be a vector in ``x1`` and :math:`\mathbf{b}` be a corresponding vector in ``x2``. The dot product is defined as: .. math:: \mathbf{a} \cdot \mathbf{b} = \sum_{i=0}^{n-1} \overline{a_i}b_i over the dimension specified by ``axis`` and where :math:`\overline{a_i}` denotes the complex conjugate if :math:`a_i` is complex and the identity otherwise. Parameters ---------- x1 : array_like First input array. x2 : array_like Second input array. axis : int, optional Axis over which to compute the dot product. Default: ``-1``. Returns ------- output : ndarray The vector dot product of the input. See Also -------- numpy.vecdot Examples -------- Get the projected size along a given normal for an array of vectors. >>> v = np.array([[0., 5., 0.], [0., 0., 10.], [0., 6., 8.]]) >>> n = np.array([0., 0.6, 0.8]) >>> np.linalg.vecdot(v, n) array([ 3., 8., 10.]) rM) _core_vecdotrs rbr!r!s\ BT * * **rar)r)r)r:)NNN)TTF)NN)NF)NNF)F)rt__all__ functoolsrr'typingr#r$ numpy._corer%r&r'r(r)r*r+r,r-r.r/r0r1rrr2rrr3r4r5r6r7r8r9r:r;r<r=r>r?rrrrr@rArBrCrrrDrErFrGrHrIrJrKrLrrrrrMrr!rrNnumpy._globalsrO numpy._typingrP numpy._utilsrQnumpy.lib._twodim_base_implrRrSnumpy.lib.array_utilsrTrU numpy.linalgrVrXrergrkrnpartialarray_function_dispatch fortran_intrrrzr|r~rrrrrrrrrrrrrrrrrrrrrrrrrrr rrrrrr9r rrrKr r]rrbrrmrrvr r r rrrrrrrrrrrrrrrrrrrr rr`rarbrs   3 3 3""""""""33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333h$#####!!!!!!######11111111LLLLLLLL&&&&&& zJ  ,)+ %n   N*:)))999666...FFF***  ***66FF$ ggww(  ++++$....###0   000 999 OOOEEE 777 &011JJJ21JZ*++Z0Z0,+Z0z.//A!A!A!0/A!L*++p3p3,+p3f122pp32pj)--..!b0b0b0b0/.b0P*++C)C),+C)R((G&G&G&)(G&X*++W*W*,+W*t-..Z5Z5Z5/.Z5@*++U?U?,+U?p-..O#O#O#/.O#h))xxx*)xv,--.5.5.-.5b)**q q q +*q h011l+dl+l+l+l+21l+bD)**ooooo+*oj*++P'P',+P'f*++4 4 ,+4 r*++Q*Q*Q*,+Q*h8)**CCC+*CH)- -..!tttt/.tn*****&)&)&)&)R    *.-..V9V9V9V9/.V9v'+$*++4KCKCKCKC,+KC`*.*++E*E*E*E*,+E*T+,,P P -,P j.2.//!"....0/.$+  566%%76%!7 >/3011"'U6>6>6>6>216>v+/4011"U]]]]21]D+/+,, -+-+-+-+-,-+-+-+ra