Legend:
Library
Module
Module type
Parameter
Class
Class type
Linear algebra module including high-level functions to solve linear systems, factorisation, and etc.
The module includes a set of advanced linear algebra operations such as singular value decomposition, and etc.
Currently, Linalg module supports dense matrix of four different number types, including float32, float64, complex32, and complex64. The support for sparse matrices will be provided in future.
pinv x computes Moore-Penrose pseudoinverse of matrix x. tol specifies the tolerance, the absolute value of the elements smaller than tol will be set to zeros.
rank x calculates the rank of a rectangular matrix x of shape m x n. The function does so by counting the number of singular values of x which are beyond a pre-defined threshold tol. By default, tol = max(m,n) * eps where eps = 1e-10.
rcond x returns an estimate for the reciprocal condition of x in 1-norm. If x is well conditioned, the returned result is near 1.0. If x is badly conditioned, the result is near 0.
lu x -> (l, u, ipiv) calculates LU decomposition of [x]. The pivoting is
used by default.
val lq : ?thin:bool ->('a, 'b)t->('a, 'b)t * ('a, 'b)t
lq x -> (l, q) calculates the LQ decomposition of x. By default, the reduced LQ decomposition is performed. But you can get full Q by setting parameter thin = false.
val qr :
?thin:bool ->?pivot:bool ->('a, 'b)t->('a, 'b)t * ('a, 'b)t * (int32, Bigarray.int32_elt)t
qr x calculates QR decomposition for an m by n matrix x as x = Q R. Q is an m by n matrix (where Q^T Q = I) and R is an n by n upper-triangular matrix.
The function returns a 3-tuple, the first two are q and r, and the thrid is the permutation vector of columns. The default value of pivot is false, setting pivot = true lets qr performs pivoted factorisation. Note that the returned indices are not adjusted to 0-based C layout.
By default, qr performs a reduced QR factorisation, full factorisation can be enabled by setting thin parameter to false.
chol x -> u calculates the Cholesky factorisation of a positive definite matrix x such that x = u' *@ u. By default, the upper triangular matrix is returned. The lower triangular part can be obtained by setting the parameter upper = false.
svd x -> (u, s, vt) calculates the singular value decomposition of x, and returns a 3-tuple (u,s,vt). By default, a reduced svd is performed: E.g., for a m x n matrix x wherein m <= n, u is returned as an m by m orthogonal matrix, s an 1 by m row vector of singular values, and vt is the transpose of an n by m orthogonal rectangular matrix.
The full svd can be performed by setting thin = false. Note that for complex numbers, the type of returned singular values are also complex, the imaginary part is zero.
svdvals x -> s performs the singular value decomposition of x like svd x, but the function only returns the singular values without u and vt. Note that for complex numbers, the return is also complex type.
gsvd x y -> (u, v, q, d1, d2, r) computes the generalised singular value decomposition of a pair of general rectangular matrices x and y. d1 and d2 contain the generalised singular value pairs of x and y. The shape of x is m x n and the shape of y is p x n.
let x = Mat.uniform 5 5;;
let y = Mat.uniform 2 5;;
let u, v, q, d1, d2, r = Linalg.gsvd x y;;
Mat.(u *@ d1 *@ r *@ transpose q =~ x);;
Mat.(v *@ d2 *@ r *@ transpose q =~ y);;
Please refer to: https://software.intel.com/en-us/mkl-developer-reference-c-ggsvd3
schur x -> (t, z, w) calculates Schur factorisation of x. t is (quasi) triangular Schur factor, z is orthogonal/unitary Schur vectors. The eigen values are not sorted, they have the same order as that they appear on the diagonal of the output of Schur form t.
w contains the eigen values. otyp is used to specify the type of w. It needs to be consistent with input type. E.g., if the input x is float32 then otyp must be complex32. However, if you use S, D, C, Z module, then you do not need to worry about otyp.
hess x -> (h, q) calculates the Hessenberg form of a given matrix x. Both Hessenberg matrix h and unitary matrix q is returned, such that x = q *@ h *@ (transpose q).
eig x -> v, w computes the right eigenvectors v and eigenvalues w of an arbitrary square matrix x. The eigenvectors are column vectors in v, their corresponding eigenvalues have the same order in w as that in v.
Note that otyp specifies the complex type of the output, but you do not need worry about this parameter if you use S, D, C, Z modules in Linalg.
val eigvals :
?permute:bool ->?scale:bool ->otyp:('a, 'b)Bigarray.kind->('c, 'd)t->('a, 'b)t
eigvals x -> w is similar to eig but only computes the eigenvalues of an arbitrary square matrix x.
null a -> x computes an orthonormal basis x for the null space of a obtained from the singular value decomposition. Namely, a *@ x has negligible elements, M.col_num x is the nullity of a, and transpose x *@ x = I.
val linsolve : ?trans:bool ->('a, 'b)t->('a, 'b)t->('a, 'b)t
linsolve a b -> x solves a linear system of equations A * x = b. The function uses LU factorisation with partial pivoting when a is square and QR factorisation with column pivoting otherwise. The number of rows of a must equal the number of rows of b.
By default, trans = false indicates no transpose. If trans = true, then function will solve A^T * x = b for real matrices; A^H * x = b for complex matrices.
bk x -> (a, ipiv) calculates Bunch-Kaufman factorisation of x. If symmetric = true then x is symmetric, if symmetric = false then x is hermitian. If rook = true the function performs bounded Bunch-Kaufman ("rook") diagonal pivoting method, if rook = false then Bunch-Kaufman diagonal pivoting method is used. a contains details of the block-diagonal matrix d and the multipliers used to obtain the factor u (or l).
The upper indicates whether the upper or lower triangular part of x is stored and how x is factored. If upper = true then upper triangular part is stored: x = u*d*u' else x = l*d*l'.
For ipiv, it indicates the details of the interchanges and the block structure of d. Please refer to the function sytrf, hetrf in MKL documentation for more details.
Helper functions
val peakflops : ?n:int ->unit -> float
peakflops () returns the peak number of float point operations using Owl_cblas.dgemm function. The default matrix size is 2000 x 2000, but you can change this by setting n to other numbers as you like.