Return the Cholesky decomposition, L * L.T, of the square matrix a,
where L is lower-triangular and .T is the transpose operator. a must be
symmetric and positive-definite. Only L is actually returned. Complex-valued
input is currently not supported.
Parameters:
a ((..., M, M) ndarray) – Symmetric, positive-definite input matrix.
Another way to represent the determinant, more suitable
for
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:
>>> a = np.array([[1, 2], [3, 4]])
>>> np.linalg.det(a)
-2.0
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)
Compute the eigenvalues and right eigenvectors of a square array.
Parameters:
a ((..., M, M) ndarray) – Matrices for which the eigenvalues and right eigenvectors will
be computed
Returns:
w ((…, M) ndarray) – The eigenvalues, each repeated according to its multiplicity.
The eigenvalues are not necessarily ordered.
v ((…, M, M) ndarray) – The normalized (unit “length”) eigenvectors, such that the
column v[:,i] is the eigenvector corresponding to the
eigenvalue w[i].
Raises:
MXNetError – If the eigenvalue computation does not converge.
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 dot(a,v)=w*v. Thus, the arrays a, w, and
v satisfy the equations dot(a[:,:],v[:,i])=w[i]*v[:,i]
for \(i \\in \\{0,...,M-1\\}\).
The array v of 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.
This function differs from the original numpy.linalg.eig in
the following way(s):
Return the eigenvalues and eigenvectors 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) ndarray) – 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:
w ((…, M) ndarray) – The eigenvalues in ascending order, each repeated according to
its multiplicity.
v ({(…, M, M) ndarray, (…, M, M) matrix}) – The column v[:,i] is the normalized eigenvector corresponding
to the eigenvalue w[i]. Will return a matrix object if a is
a matrix object.
Raises:
MXNetError – If the eigenvalue computation does not converge.
Main difference from eigh: the eigenvectors are not computed.
Parameters:
a ((..., M, M) ndarray) – A 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 – The eigenvalues in ascending order, each repeated according to
its multiplicity.
Return the least-squares solution to a linear matrix equation.
Solves the equation \(a x = b\) by computing a vector x that
minimizes the squared Euclidean 2-norm \(\| b - a x \|^2_2\).
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.
Parameters:
a ((M, N) _Symbol) – “Coefficient” matrix.
b ({(M,), (M, K)} _Symbol) – 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 of warn or -1 will use the machine precision as
rcond parameter. The default of None will use the machine
precision times max(M, N) as rcond parameter.
Returns:
x ({(N,), (N, K)} _Symbol) – Least-squares solution. If b is two-dimensional,
the solutions are in the K columns of x.
residuals ({(1,), (K,), (0,)} _Symbol) – Sums of 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),) _Symbol) – Singular values of a.
Raises:
MXNetError – If computation does not converge.
Notes
If b is a matrix, then all array results are returned as matrices.
Rank of the array is the number of singular values of the array that are
greater than tol.
Parameters
M : {(M,), (…, M, N)} _Symbol
Input vector or stack of matrices.
tol(…) _Symbol, 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.shape)*eps.
hermitianbool, optional
If True, M is assumed to be Hermitian (symmetric if real-valued),
enabling a more efficient method for finding singular values.
Defaults to 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 (_Symbol) – Input array. If axis is None, x must be 1-D or 2-D.
ord ({non-zero int, inf, -inf, 'fro', 'nuc'}, optional) – Order of the norm (see table under Notes). inf means numpy’s
inf object.
axis ({int, 2-tuple of ints, None}, 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.
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 – Norm of the matrix or vector(s).
Return type:
_Symbol
Notes
For values of ord<=0, 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’ – –
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 – as below
-2 – as below
other – sum(abs(x)**ord)**(1./ord)
===== ============================ ==========================
The Frobenius norm is given by [1]_:
\(||A||_F = [\sum_{i,j} abs(a_{i,j})^2]^{1/2}\)
The nuclear norm is the sum of the singular values.
When you want to operate norm for matrices,if you ord is (-1, 1, inf, -inf),
you must give you axis, it is not support default axis.
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) ndarray) – Matrix or stack of matrices to be pseudo-inverted.
rcond ((...) {float or ndarray 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.
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:
B – The pseudo-inverse of a. If a is a matrix instance, then so
is B.
MXNetError – If the SVD computation does not converge.
Notes
The pseudo-inverse of a matrix A, denoted \(A^+\), is
defined as: “the matrix that ‘solves’ [the least-squares problem]
\(Ax = b\),” i.e., if \(\\bar{x}\) is said solution, then
\(A^+\) is that matrix such that \(\\bar{x} = A^+b\).
It can be shown that if \(Q_1 \\Sigma Q_2^T = A\) is the singular
value decomposition of A, then
\(A^+ = Q_2 \\Sigma^+ Q_1^T\), where \(Q_{1,2}\) are
orthogonal matrices, \(\\Sigma\) is a diagonal matrix consisting
of A’s so-called singular values, (followed, typically, by
zeros), and then \(\\Sigma^+\) is simply the diagonal matrix
consisting of the reciprocals of A’s singular values
(again, followed by zeros). [1]_
References
Examples
The following example checks that a*a+*a==a and
a+*a*a+==a+:
>>> a = np.random.randn(2, 3)
>>> pinv_a = np.linalg.pinv(a)
>>> (a - np.dot(a, np.dot(pinv_a, a))).sum()
array(0.)
>>> (pinv_a - np.dot(pinv_a, np.dot(a, pinv_a))).sum()
array(0.)
Compute the qr factorization of a matrix a.
Factor the matrix a as qr, where q is orthonormal and r is upper-triangular.
Parameters:
a ((..., M, N) _Symbol) – Matrix or stack of matrices to be qr factored.
mode ({‘reduced’, ‘complete’, ‘r’, ‘raw’, ‘full’, ‘economic’}, optional) – Only default mode, ‘reduced’, is implemented. If K = min(M, N), then
* ‘reduced’ : returns q, r with dimensions (M, K), (K, N) (default)
Returns:
q ((…, M, K) _Symbol) – A matrix or stack of matrices with K orthonormal columns, with K = min(M, N).
r ((…, K, N) _Symbol) – A matrix or stack of upper triangular matrices.
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) ndarray) – Input array, has to be a square 2-D array.
Returns:
sign ((…) ndarray) – A number representing the sign of the determinant. For a real matrix,
this is 1, 0, or -1.
logdet ((…) array_like) – The natural log of the absolute value of the determinant.
If the determinant is zero, then sign will be 0 and logdet will be
-Inf. In all cases, the determinant is equal to sign*np.exp(logdet).
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]])
>>> (sign, logdet) = np.linalg.slogdet(a)
>>> (sign, logdet)
(-1., 0.69314718055994529)
>>> sign*np.exp(logdet)-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)
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)
b ({(..., M,), (..., M, K)}, ndarray) – Ordinate or “dependent variable” values.
Returns:
x – Solution to the system a x = b. Returned shape is identical to b.
Return type:
{(…, M,), (…, M, K)} ndarray
Raises:
MXNetError – If a is singular or not square.
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.
Examples
Solve the system of equations 3*x0+x1=9 and x0+2*x1=8:
When a is a 2D array, it is factorized as ut@np.diag(s)@v,
where ut and v are 2D orthonormal arrays 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) _Symbol) – A real array with a.ndim>=2 and M<=N.
Returns:
ut ((…, M, M) _Symbol) – Orthonormal array(s). The first a.ndim-2 dimensions have the same
size as those of the input a.
s ((…, M) _Symbol) – 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.
v ((…, M, N) _Symbol) – Orthonormal array(s). The first a.ndim-2 dimensions have the same
size as those of the input a.
Notes
The decomposition is performed using LAPACK routine _gesvd.
SVD is usually described for the factorization of a 2D matrix \(A\).
The higher-dimensional case will be discussed below. In the 2D case, SVD is
written as \(A = U^T S V\), where \(A = a\), \(U^T = ut\),
\(S= \mathtt{np.diag}(s)\) and \(V = v\). The 1D array s
contains the singular values of a and ut and v are orthonormal. The rows
of v are the eigenvectors of \(A^T A\) and the columns of ut are
the eigenvectors of \(A A^T\). 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.
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
(ut*s[...,None,:])@v or
ut@(s[...,None]*v). (The @ operator denotes batch matrix multiplication)
This function differs from the original numpy.linalg.svd in
the following way(s):
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 – a’s tensordot inverse, shape a.shape[ind:]+a.shape[:ind].
Solve the tensor equation ax=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=b.ndim).
Parameters:
a (ndarray) – 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 (ndarray) – 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.