hmatrix-0.5.1.1: Linear algebra and numerical computations

Portability uses ffi provisional Alberto Ruiz (aruiz at um dot es)

Numeric.LinearAlgebra.Algorithms

Description

A generic interface for some common functions. Using it we can write higher level algorithms and testing properties both for real and complex matrices.

In any case, the specific functions for particular base types can also be explicitly imported from Numeric.LinearAlgebra.LAPACK.

Synopsis

# Supported types

class (Normed (Matrix t), Linear Vector t, Linear Matrix t) => Field t Source

Auxiliary typeclass used to define generic computations for both real and complex matrices.

Instances

 Field Double Field (Complex Double)

# Products

multiply :: Field t => Matrix t -> Matrix t -> Matrix tSource

Matrix product.

dot :: Field t => Vector t -> Vector t -> tSource

Euclidean inner product.

outer :: Field t => Vector t -> Vector t -> Matrix tSource

Outer product of two vectors.

```> `fromList` [1,2,3] `outer` `fromList` [5,2,3]
(3><3)
[  5.0, 2.0, 3.0
, 10.0, 4.0, 6.0
, 15.0, 6.0, 9.0 ]```

kronecker :: Field t => Matrix t -> Matrix t -> Matrix tSource

Kronecker product of two matrices.

```m1=(2><3)
[ 1.0,  2.0, 0.0
, 0.0, -1.0, 3.0 ]
m2=(4><3)
[  1.0,  2.0,  3.0
,  4.0,  5.0,  6.0
,  7.0,  8.0,  9.0
, 10.0, 11.0, 12.0 ]```
```> kronecker m1 m2
(8><9)
[  1.0,  2.0,  3.0,   2.0,   4.0,   6.0,  0.0,  0.0,  0.0
,  4.0,  5.0,  6.0,   8.0,  10.0,  12.0,  0.0,  0.0,  0.0
,  7.0,  8.0,  9.0,  14.0,  16.0,  18.0,  0.0,  0.0,  0.0
, 10.0, 11.0, 12.0,  20.0,  22.0,  24.0,  0.0,  0.0,  0.0
,  0.0,  0.0,  0.0,  -1.0,  -2.0,  -3.0,  3.0,  6.0,  9.0
,  0.0,  0.0,  0.0,  -4.0,  -5.0,  -6.0, 12.0, 15.0, 18.0
,  0.0,  0.0,  0.0,  -7.0,  -8.0,  -9.0, 21.0, 24.0, 27.0
,  0.0,  0.0,  0.0, -10.0, -11.0, -12.0, 30.0, 33.0, 36.0 ]```

# Linear Systems

linearSolve :: Field t => Matrix t -> Matrix t -> Matrix tSource

Solution of a general linear system (for several right-hand sides) using lapacks' dgesv or zgesv. It is similar to `luSolve` . `luPacked`, but `linearSolve` raises an error if called on a singular system. See also other versions of linearSolve in Numeric.LinearAlgebra.LAPACK.

luSolve :: Field t => (Matrix t, [Int]) -> Matrix t -> Matrix tSource

Solution of a linear system (for several right hand sides) from the precomputed LU factorization obtained by `luPacked`.

inv :: Field t => Matrix t -> Matrix tSource

Inverse of a square matrix using lapacks' dgesv and zgesv.

pinv :: Field t => Matrix t -> Matrix tSource

Pseudoinverse of a general matrix using lapack's dgelss or zgelss.

det :: Field t => Matrix t -> tSource

determinant of a square matrix, computed from the LU decomposition.

rank :: Field t => Matrix t -> IntSource

Number of linearly independent rows or columns.

rcond :: Field t => Matrix t -> DoubleSource

Reciprocal of the 2-norm condition number of a matrix, computed from the SVD.

# Matrix factorizations

## Singular value decomposition

svd :: Field t => Matrix t -> (Matrix t, Vector Double, Matrix t)Source

Singular value decomposition using lapack's dgesvd or zgesvd.

full :: Element t => (Matrix t -> (Matrix t, Vector Double, Matrix t)) -> Matrix t -> (Matrix t, Matrix Double, Matrix t)Source

A version of `svd` which returns an appropriate diagonal matrix with the singular values.

If `(u,d,v) = full svd m` then `m == u <> d <> trans v`.

economy :: Element t => (Matrix t -> (Matrix t, Vector Double, Matrix t)) -> Matrix t -> (Matrix t, Vector Double, Matrix t)Source

A version of `svd` which returns only the nonzero singular values and the corresponding rows and columns of the rotations.

If `(u,s,v) = economy svd m` then `m == u <> diag s <> trans v`.

## Eigensystems

eig :: Field t => Matrix t -> (Vector (Complex Double), Matrix (Complex Double))Source

Eigenvalues and eigenvectors of a general square matrix using lapack's dgeev or zgeev.

If `(s,v) = eig m` then `m <> v == v <> diag s`

eigSH :: Field t => Matrix t -> (Vector Double, Matrix t)Source

Eigenvalues and Eigenvectors of a complex hermitian or real symmetric matrix using lapack's dsyev or zheev.

If `(s,v) = eigSH m` then `m == v <> diag s <> ctrans v`

eigSH' :: Field t => Matrix t -> (Vector Double, Matrix t)Source

Similar to `eigSH` without checking that the input matrix is hermitian or symmetric.

## QR

qr :: Field t => Matrix t -> (Matrix t, Matrix t)Source

QR factorization using lapack's dgeqr2 or zgeqr2.

If `(q,r) = qr m` then `m == q <> r`, where q is unitary and r is upper triangular.

## Cholesky

chol :: Field t => Matrix t -> Matrix tSource

Cholesky factorization of a positive definite hermitian or symmetric matrix using lapack's dpotrf or zportrf.

If `c = chol m` then `m == ctrans c <> c`.

cholSH :: Field t => Matrix t -> Matrix tSource

Similar to `chol` without checking that the input matrix is hermitian or symmetric.

## Hessenberg

hess :: Field t => Matrix t -> (Matrix t, Matrix t)Source

Hessenberg factorization using lapack's dgehrd or zgehrd.

If `(p,h) = hess m` then `m == p <> h <> ctrans p`, where p is unitary and h is in upper Hessenberg form.

## Schur

schur :: Field t => Matrix t -> (Matrix t, Matrix t)Source

Schur factorization using lapack's dgees or zgees.

If `(u,s) = schur m` then `m == u <> s <> ctrans u`, where u is unitary and s is a Shur matrix. A complex Schur matrix is upper triangular. A real Schur matrix is upper triangular in 2x2 blocks.

"Anything that the Jordan decomposition can do, the Schur decomposition can do better!" (Van Loan)

## LU

lu :: Field t => Matrix t -> (Matrix t, Matrix t, Matrix t, t)Source

Explicit LU factorization of a general matrix using lapack's dgetrf or zgetrf.

If `(l,u,p,s) = lu m` then `m == p <> l <> u`, where l is lower triangular, u is upper triangular, p is a permutation matrix and s is the signature of the permutation.

luPacked :: Field t => Matrix t -> (Matrix t, [Int])Source

Obtains the LU decomposition of a matrix in a compact data structure suitable for `luSolve`.

# Matrix functions

expm :: Field t => Matrix t -> Matrix tSource

Matrix exponential. It uses a direct translation of Algorithm 11.3.1 in Golub & Van Loan, based on a scaled Pade approximation.

sqrtm :: Field t => Matrix t -> Matrix tSource

Matrix square root. Currently it uses a simple iterative algorithm described in Wikipedia. It only works with invertible matrices that have a real solution. For diagonalizable matrices you can try `matFunc sqrt`.

```m = (2><2) [4,9
,0,4] :: Matrix Double```
```>sqrtm m
(2><2)
[ 2.0, 2.25
, 0.0,  2.0 ]```

matFunc :: Field t => (Complex Double -> Complex Double) -> Matrix t -> Matrix (Complex Double)Source

Generic matrix functions for diagonalizable matrices. For instance:

`logm = matFunc log`

# Nullspace

Arguments

 :: Field t => Double relative tolerance in `eps` units -> Matrix t input matrix -> [Vector t] list of unitary vectors spanning the nullspace

The nullspace of a matrix from its SVD decomposition.

nullVector :: Field t => Matrix t -> Vector tSource

The nullspace of a matrix, assumed to be one-dimensional, with default tolerance (shortcut for `last . nullspacePrec 1`).

# Norms

class Normed t whereSource

Objects which have a p-norm. Using it you can define convenient shortcuts:

`norm2 x = pnorm PNorm2 x`
`frobenius m = norm2 . flatten \$ m`

Methods

pnorm :: NormType -> t -> DoubleSource

Instances

 Normed (Vector Double) Normed (Vector (Complex Double)) Normed (Matrix Double) Normed (Matrix (Complex Double))

data NormType Source

Constructors

 Infinity PNorm1 PNorm2

# Misc

ctrans :: Field t => Matrix t -> Matrix tSource

Generic conjugate transpose.

The machine precision of a Double: `eps = 2.22044604925031e-16` (the value used by GNU-Octave).

The imaginary unit: `i = 0.0 :+ 1.0`

# Util

haussholder :: Field a => a -> Vector a -> Matrix aSource

unpackQR :: Field t => (Matrix t, Vector t) -> (Matrix t, Matrix t)Source

unpackHess :: Field t => (Matrix t -> (Matrix t, Vector t)) -> Matrix t -> (Matrix t, Matrix t)Source