Reference¶
Defined in xtensor-blas/xlinalg.hpp
The functions here are closely modeled after NumPy’s linalg package.
Matrix, vector and tensor products¶
-
template<class
T
, classO
>
autoxt::linalg
::
dot
(const xexpression<T> &xt, const xexpression<O> &xo)¶ Non-broadcasting dot function.
In the case of two 1D vectors, computes the vector dot product. In the case of complex vectors, computes the dot product without conjugating the first argument. If t or o is a 2D matrix, computes the matrix-times-vector product. If both t and o ar 2D matrices, computes the matrix-product.
- Return
resulting array
- Parameters
t
: input arrayo
: input array
-
template<class
T
, classO
>
autoxt::linalg
::
vdot
(const xexpression<T> &a, const xexpression<O> &b)¶ Computes the dot product for two vectors.
Behaves different from dot in the case of complex vectors. If vectors are complex, vdot conjugates the first argument t. Note: Unlike NumPy, xtensor-blas currently doesn’t flatten the input arguments.
- Return
resulting array
- Parameters
t
: input vector (1D)o
: input vector (1D)
-
template<class
T
, classO
>
autoxt::linalg
::
outer
(const xexpression<T> &a, const xexpression<O> &b)¶ Compute the outer product of two vectors.
- Return
resulting array
- Parameters
t
: input vector (1D)o
: input vector (1D)
-
template<class
E
>
autoxt::linalg
::
matrix_power
(const xexpression<E> &A, long n)¶ Calculate matrix power A**n.
- Return
resulting array
- Parameters
mat
: The matrixn
: The exponent
-
template<class
T
, classE
>
autoxt::linalg
::
kron
(const xexpression<T> &a, const xexpression<E> &b)¶ Calculate the Kronecker product between two 2D xexpressions.
-
template<class
T
, classO
>
autoxt::linalg
::
tensordot
(const xexpression<T> &xa, const xexpression<O> &xb, std::size_t naxes = 2)¶ Compute tensor dot product along specified axes for arrays.
Compute the sum of products along the last naxes axes of a and first naxes axes of b.
- Return
resulting array
- Parameters
xa
: input arrayxb
: input arraynaxes
: the number of axes to sum over
-
template<class
T
, classO
>
autoxt::linalg
::
tensordot
(const xexpression<T> &xa, const xexpression<O> &xb, const std::vector<std::size_t> &ax_a, const std::vector<std::size_t> &ax_b)¶ Compute tensor dot product along specified axes for arrays.
Compute the sum of products along the axes ax_a for a and ax_b for b
- Return
resulting array
- Parameters
xa
: input arrayxb
: input arrayax_a
: axes to sum over for aax_b
: axes to sum over for b
Decompositions¶
-
template<class
T
>
autoxt::linalg
::
cholesky
(const xexpression<T> &A)¶ Compute the Cholesky decomposition of A.
- Return
the decomposed matrix
-
enum
xt::linalg
::
qrmode
¶ Select the mode for the qr decomposition
K = min(M, K)
Values:
-
enumerator
reduced
¶ return Q, R with dimensions (M, K), (K, N) (default)
-
enumerator
complete
¶ return Q, R with dimensions (M, M), (M, N)
-
enumerator
r
¶ return empty Q and R with dimensions (0, 0), (K, N)
-
enumerator
raw
¶ return H, Tau with dimensions (N, M), (K, 1)
-
enumerator
Matrix eigenvalues¶
-
template<class
E
, std::enable_if_t<!xtl::is_complex<typename E::value_type>::value>* = nullptr>
autoxt::linalg
::
eig
(const xexpression<E> &A)¶ Compute the eigenvalues and right eigenvectors of a square array.
- Return
(eigenvalues, eigenvectors) tuple. The first element corresponds to the eigenvalues, each repeated according to its multiplicity. The eigenvalues are not necessarily ordered. The second (1) element are the normalized (unit “length”) eigenvectors, such that the column v[:, i] corresponds to the eigenvalue w[i].
- Parameters
Matrix
: for which the eigenvalues and right eigenvectors will be computed
-
template<class
E
, std::enable_if_t<!xtl::is_complex<typename E::value_type>::value>* = nullptr>
autoxt::linalg
::
eigvals
(const xexpression<E> &A)¶ Compute the eigenvalues of a square xexpression.
- Return
xtensor containing the eigenvalues.
- Parameters
A
: Matrix for which the eigenvalues are computed
Warning
doxygenfunction: Unable to resolve multiple matches for function “xt::linalg::eigh” with arguments (()) in doxygen xml output for project “xtensor-blas” from directory: ../xml. Potential matches:
- template<class E, std::enable_if_t<!xtl::is_complex<typename E::value_type>::value>* = nullptr> auto eigh(const xexpression<E> &A, char UPLO = 'L')
- template<class E, std::enable_if_t<!xtl::is_complex<typename E::value_type>::value>* = nullptr> auto eigh(const xexpression<E> &A, const xexpression<E> &B, const char UPLO = 'L')
-
template<class
E
, std::enable_if_t<!xtl::is_complex<typename E::value_type>::value>* = nullptr>
autoxt::linalg
::
eigvalsh
(const xexpression<E> &A, char UPLO = 'L')¶ Compute the eigenvalues of a Hermitian or real symmetric matrix xexpression.
- Return
xtensor containing the eigenvalues.
- Parameters
Matrix
: for which the eigenvalues are computed
Norms and other numbers¶
-
enum
xt::linalg
::
normorder
¶ Selects special norm orders.
Values:
-
enumerator
frob
¶ Frobenius norm.
-
enumerator
nuc
¶ Nuclear norm.
-
enumerator
inf
¶ Positive infinity norm.
-
enumerator
neg_inf
¶ Negative infinity norm.
-
enumerator
-
template<class
E
>
autoxt::linalg
::
norm
(const xexpression<E> &vec, int ord)¶ Calculate norm of vector, or matrix.
- Return
scalar result
- Parameters
vec
: input vectorord
: order of norm. This can be any integer for a vector or [-2,-1,1,2] for a matrix.
- Template Parameters
type
: of xexpression
-
template<class
E
>
autoxt::linalg
::
norm
(const xexpression<E> &vec, normorder ord)¶ Calculate matrix or vector norm using normorder.
- Return
norm value
- Parameters
vec
: The matrix or vector to take the norm oford
: normorder (frob, nuc, inf, neg_inf)
-
template<class
E
>
E::value_typext::linalg
::
norm
(const xexpression<E> &vec)¶ Calculate default norm (2-norm for vector, Frobenius norm for matrix)
- Return
norm
- Parameters
vec
: Input vector or matrix
-
template<class
T
>
autoxt::linalg
::
det
(const xexpression<T> &A)¶ Compute the determinant by utilizing LU factorization.
- Return
determinant of the A
- Parameters
A
: matrix for which determinant is to be computed
-
template<class
T
, std::enable_if_t<xtl::is_complex<typename T::value_type>::value, int> = 0>
autoxt::linalg
::
slogdet
(const xexpression<T> &A)¶ Compute the sign and (natural) logarithm of the determinant of an xexpression.
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.
- Return
tuple containing (sign, determinant)
- Parameters
A
: matrix for which determinant is to be computed
Solving equations and inverting matrices¶
-
template<class
E1
, classE2
>
autoxt::linalg
::
solve
(const xexpression<E1> &A, const xexpression<E2> &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.
- Return
Solution to the system a x = b. Returned shape is identical to b.
- Parameters
a
: Coefficient matrixb
: Ordinate or “dependent variable” values.
-
template<class
T
, classE
>
autoxt::linalg
::
lstsq
(const xexpression<T> &A, const xexpression<E> &b, double rcond = -1.0)¶ Calculate the least-squares solution to a linear matrix equation.
- Return
tuple containing (x, residuals, rank, s) where: x is the least squares solution. Note that the solution is always returned as a 2D matrix where the columns are the solutions (even for a 1D b). s 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 xtensor. rank the rank of A s singular values of A
- Parameters
A
: coefficient matrixb
: 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
: 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.
Other¶
-
template<class
E1
, classE2
>
autoxt::linalg
::
cross
(const xexpression<E1> &a, const xexpression<E2> &b)¶ Non-broadcasting cross product between two vectors.
Calculate cross product between two 1D vectors with 2- or 3 entries. If only two entries are available, the third entry is assumed to be 0.
- Return
resulting array
- Parameters
a
: input vectorb
: input vector