Safe Haskell | None |
---|---|

Language | Haskell2010 |

This documentation is based on original Eigen page Solving Sparse Linear Systems

Eigen currently provides a limited set of built-in MPL2 compatible solvers. They are summarized in the following table:

Sparse solver Solver kind Matrix kind Notes ConjugateGradient Classic iterative CG SPD Recommended for large symmetric problems (e.g., 3D Poisson eq.) BiCGSTAB Iterative stabilized Square bi-conjugate gradient SparseLU LU factorization Square Optimized for small and large problems with irregular patterns SparseQR QR factorization Any, rectangular Recommended for least-square problems, has a basic rank-revealing feature

All these solvers follow the same general concept. Here is a typical and general example:

```
let
a :: SparseMatrixXd
a = ... -- fill a
b :: SparseMatrixXd
b = ... -- fill b
validate msg = info >>= (
````when`

fail msg) . (/= Success)
// solve Ax = b
runSolverT solver $ do
compute a
validate "decomposition failed"
x <- solve b
validate "solving failed"
// solve for another right hand side
x1 <- solve b1

In the case where multiple problems with the same sparsity pattern have to be solved, then the "compute" step can be decomposed as follow:

runSolverT solver $ do analyzePattern a1 factorize a1 x1 <- solve b1 x2 <- solve b2 factorize a2 x1 <- solve b1 x2 <- solve b2

Finally, each solver provides some specific features, such as determinant, access to the factors, controls of the iterations, and so on.

## Synopsis

- class Code s => Solver s
- class Solver s => DirectSolver s
- class Solver s => IterativeSolver s
- data OrderingMethod
- data Preconditioner
- newtype ConjugateGradient = ConjugateGradient Preconditioner
- newtype BiCGSTAB = BiCGSTAB Preconditioner
- newtype SparseLU = SparseLU OrderingMethod
- newtype SparseQR = SparseQR OrderingMethod
- data ComputationInfo
- newtype SolverT s a p c = SolverT (ReaderT (s, ForeignPtr (CSolver a)) p c)
- runSolverT :: (Solver s, MonadIO p, Elem a) => s -> SolverT s a p c -> p c
- analyzePattern :: (Solver s, MonadIO p, Elem a) => SparseMatrix n m a -> SolverT s a p ()
- factorize :: (Solver s, MonadIO p, Elem a) => SparseMatrix n m a -> SolverT s a p ()
- compute :: (Solver s, MonadIO p, Elem a) => SparseMatrix n m a -> SolverT s a p ()
- solve :: (Solver s, MonadIO p, Elem a) => SparseMatrix n m a -> SolverT s a p (SparseMatrix n m a)
- info :: (Solver s, MonadIO p, Elem a) => SolverT s a p ComputationInfo
- tolerance :: (IterativeSolver s, MonadIO p, Elem a) => SolverT s a p Double
- setTolerance :: (IterativeSolver s, MonadIO p, Elem a) => Double -> SolverT s a p ()
- maxIterations :: (IterativeSolver s, MonadIO p, Elem a) => SolverT s a p Int
- setMaxIterations :: (IterativeSolver s, MonadIO p, Elem a) => Int -> SolverT s a p ()
- error :: (IterativeSolver s, MonadIO p, Elem a) => SolverT s a p Double
- iterations :: (IterativeSolver s, MonadIO p, Elem a) => SolverT s a p Int
- matrixR :: (MonadIO p, Elem a) => SolverT SparseQR a p (SparseMatrix n m a)
- matrixQ :: (MonadIO p, Elem a) => SolverT SparseQR a p (SparseMatrix n m a)
- rank :: (MonadIO p, Elem a) => SolverT SparseQR a p Int
- setPivotThreshold :: (MonadIO p, Elem a) => Double -> SolverT SparseQR a p ()
- setSymmetric :: (MonadIO p, Elem a) => Bool -> SolverT SparseLU a p ()
- matrixL :: (MonadIO p, Elem a) => SolverT SparseLU a p (SparseMatrix n m a)
- matrixU :: (MonadIO p, Elem a) => SolverT SparseLU a p (SparseMatrix n m a)
- determinant :: (MonadIO p, Elem a) => SolverT SparseLU a p a
- absDeterminant :: (MonadIO p, Elem a) => SolverT SparseLU a p a
- signDeterminant :: (MonadIO p, Elem a) => SolverT SparseLU a p a
- logAbsDeterminant :: (MonadIO p, Elem a) => SolverT SparseLU a p a

# Sparse Solvers

class Code s => Solver s Source #

## Instances

Solver SparseQR Source # | |

Defined in Eigen.Solver.SparseLA | |

Solver SparseLU Source # | |

Defined in Eigen.Solver.SparseLA | |

Solver BiCGSTAB Source # | |

Defined in Eigen.Solver.SparseLA | |

Solver ConjugateGradient Source # | |

Defined in Eigen.Solver.SparseLA |

class Solver s => DirectSolver s Source #

For direct methods, the solution is computed at the machine precision.

## Instances

DirectSolver SparseQR Source # | |

Defined in Eigen.Solver.SparseLA | |

DirectSolver SparseLU Source # | |

Defined in Eigen.Solver.SparseLA |

class Solver s => IterativeSolver s Source #

Sometimes, the solution need not be too accurate.
In this case, the iterative methods are more suitable and the desired accuracy can be set before the solve step using `setTolerance`

.

## Instances

IterativeSolver BiCGSTAB Source # | |

Defined in Eigen.Solver.SparseLA | |

IterativeSolver ConjugateGradient Source # | |

Defined in Eigen.Solver.SparseLA |

data OrderingMethod Source #

Ordering methods for sparse matrices. They are typically used to reduce the number of elements during the sparse matrix
decomposition (`LLT`

, `LU`

, `QR`

). Precisely, in a preprocessing step, a permutation matrix `P`

is computed using those ordering methods
and applied to the columns of the matrix. Using for instance the sparse Cholesky decomposition, it is expected that the nonzeros
elements in `LLT(A*P)`

will be much smaller than that in `LLT(A)`

.

COLAMDOrdering | The column approximate minimum degree ordering The matrix should be in column-major and compressed format |

NaturalOrdering | The natural ordering (identity) |

## Instances

Read OrderingMethod Source # | |

Defined in Eigen.Solver.SparseLA readsPrec :: Int -> ReadS OrderingMethod # readList :: ReadS [OrderingMethod] # | |

Show OrderingMethod Source # | |

Defined in Eigen.Solver.SparseLA showsPrec :: Int -> OrderingMethod -> ShowS # show :: OrderingMethod -> String # showList :: [OrderingMethod] -> ShowS # |

data Preconditioner Source #

DiagonalPreconditioner | A preconditioner based on the digonal entries It allows to approximately solve for A.x = b problems assuming A is a diagonal matrix.
In other words, this preconditioner neglects all off diagonal entries and, in Eigen's language, solves for:
A variant that has yet to be implemented would attempt to preserve the norm of each column. |

IdentityPreconditioner | A naive preconditioner which approximates any matrix as the identity matrix |

## Instances

Read Preconditioner Source # | |

Defined in Eigen.Solver.SparseLA readsPrec :: Int -> ReadS Preconditioner # readList :: ReadS [Preconditioner] # | |

Show Preconditioner Source # | |

Defined in Eigen.Solver.SparseLA showsPrec :: Int -> Preconditioner -> ShowS # show :: Preconditioner -> String # showList :: [Preconditioner] -> ShowS # |

newtype ConjugateGradient Source #

A conjugate gradient solver for sparse self-adjoint problems.

This class allows to solve for `A.x = b`

sparse linear problems using a conjugate gradient algorithm. The sparse matrix `A`

must be selfadjoint.

The maximal number of iterations and tolerance value can be controlled via the `setMaxIterations`

and `setTolerance`

methods.
The defaults are the size of the problem for the maximal number of iterations and `epsilon`

for the tolerance

## Instances

Read ConjugateGradient Source # | |

Defined in Eigen.Solver.SparseLA | |

Show ConjugateGradient Source # | |

Defined in Eigen.Solver.SparseLA showsPrec :: Int -> ConjugateGradient -> ShowS # show :: ConjugateGradient -> String # showList :: [ConjugateGradient] -> ShowS # | |

Code ConjugateGradient Source # | |

Defined in Eigen.Solver.SparseLA code :: ConjugateGradient -> CInt Source # | |

IterativeSolver ConjugateGradient Source # | |

Defined in Eigen.Solver.SparseLA | |

Solver ConjugateGradient Source # | |

Defined in Eigen.Solver.SparseLA |

A bi conjugate gradient stabilized solver for sparse square problems.

This class allows to solve for `A.x = b`

sparse linear problems using a bi conjugate gradient stabilized algorithm.
The vectors `x`

and `b`

can be either dense or sparse.

The maximal number of iterations and tolerance value can be controlled via the `setMaxIterations`

and `setTolerance`

methods.
The defaults are the size of the problem for the maximal number of iterations and `epsilon`

for the tolerance

Sparse supernodal LU factorization for general matrices.

This class implements the supernodal LU factorization for general matrices. It uses the main techniques from the sequential SuperLU package. It handles transparently real and complex arithmetics with single and double precision, depending on the scalar type of your input matrix. The code has been optimized to provide BLAS-3 operations during supernode-panel updates. It benefits directly from the built-in high-performant Eigen BLAS routines. Moreover, when the size of a supernode is very small, the BLAS calls are avoided to enable a better optimization from the compiler. For best performance, you should compile it with NDEBUG flag to avoid the numerous bounds checking on vectors.

An important parameter of this class is the ordering method. It is used to reorder the columns (and eventually the rows) of the matrix to reduce the number of new elements that are created during numerical factorization. The cheapest method available is COLAMD. See OrderingMethods module for the list of built-in and external ordering methods.

Sparse left-looking rank-revealing QR factorization.

This class implements a left-looking rank-revealing QR decomposition of sparse matrices. When a column has a norm less than a given
tolerance it is implicitly permuted to the end. The QR factorization thus obtained is given by `A*P = Q*R`

where `R`

is upper triangular or trapezoidal.

`P`

is the column permutation which is the product of the fill-reducing and the rank-revealing permutations.

`Q`

is the orthogonal matrix represented as products of Householder reflectors.

`R`

is the sparse triangular or trapezoidal matrix. The later occurs when `A`

is rank-deficient.

data ComputationInfo Source #

Success | Computation was successful. |

NumericalIssue | The provided data did not satisfy the prerequisites. |

NoConvergence | Iterative procedure did not converge. |

InvalidInput | The inputs are invalid, or the algorithm has been improperly called. When assertions are enabled, such errors trigger an error. |

## Instances

Enum ComputationInfo Source # | |

Defined in Eigen.Solver.SparseLA succ :: ComputationInfo -> ComputationInfo # pred :: ComputationInfo -> ComputationInfo # toEnum :: Int -> ComputationInfo # fromEnum :: ComputationInfo -> Int # enumFrom :: ComputationInfo -> [ComputationInfo] # enumFromThen :: ComputationInfo -> ComputationInfo -> [ComputationInfo] # enumFromTo :: ComputationInfo -> ComputationInfo -> [ComputationInfo] # enumFromThenTo :: ComputationInfo -> ComputationInfo -> ComputationInfo -> [ComputationInfo] # | |

Eq ComputationInfo Source # | |

Defined in Eigen.Solver.SparseLA (==) :: ComputationInfo -> ComputationInfo -> Bool # (/=) :: ComputationInfo -> ComputationInfo -> Bool # | |

Read ComputationInfo Source # | |

Defined in Eigen.Solver.SparseLA | |

Show ComputationInfo Source # | |

Defined in Eigen.Solver.SparseLA showsPrec :: Int -> ComputationInfo -> ShowS # show :: ComputationInfo -> String # showList :: [ComputationInfo] -> ShowS # |

newtype SolverT s a p c Source #

SolverT (ReaderT (s, ForeignPtr (CSolver a)) p c) |

## Instances

MonadTrans (SolverT s a) Source # | |

Defined in Eigen.Solver.SparseLA | |

Monad p => Monad (SolverT s a p) Source # | |

Functor p => Functor (SolverT s a p) Source # | |

Applicative p => Applicative (SolverT s a p) Source # | |

Defined in Eigen.Solver.SparseLA pure :: a0 -> SolverT s a p a0 # (<*>) :: SolverT s a p (a0 -> b) -> SolverT s a p a0 -> SolverT s a p b # liftA2 :: (a0 -> b -> c) -> SolverT s a p a0 -> SolverT s a p b -> SolverT s a p c # (*>) :: SolverT s a p a0 -> SolverT s a p b -> SolverT s a p b # (<*) :: SolverT s a p a0 -> SolverT s a p b -> SolverT s a p a0 # |

# The Compute step

In the `compute`

function, the matrix is generally factorized: LLT for self-adjoint matrices, LDLT for general hermitian matrices,
LU for non hermitian matrices and QR for rectangular matrices. These are the results of using direct solvers.
For this class of solvers precisely, the compute step is further subdivided into `analyzePattern`

and `factorize`

.

The goal of `analyzePattern`

is to reorder the nonzero elements of the matrix, such that the factorization step creates less fill-in.
This step exploits only the structure of the matrix. Hence, the results of this step can be used for other linear systems where the
matrix has the same structure.

In `factorize`

, the factors of the coefficient matrix are computed. This step should be called each time the values of the matrix change.
However, the structural pattern of the matrix should not change between multiple calls.

For iterative solvers, the `compute`

step is used to eventually setup a preconditioner.
Remember that, basically, the goal of the preconditioner is to speedup the convergence of an iterative method by solving a modified linear
system where the coefficient matrix has more clustered eigenvalues.
For real problems, an iterative solver should always be used with a preconditioner.

analyzePattern :: (Solver s, MonadIO p, Elem a) => SparseMatrix n m a -> SolverT s a p () Source #

Initializes the iterative solver for the sparsity pattern of the matrix `A`

for further solving `Ax=b`

problems.

factorize :: (Solver s, MonadIO p, Elem a) => SparseMatrix n m a -> SolverT s a p () Source #

Initializes the iterative solver with the numerical values of the matrix `A`

for further solving `Ax=b`

problems.

compute :: (Solver s, MonadIO p, Elem a) => SparseMatrix n m a -> SolverT s a p () Source #

Initializes the iterative solver with the matrix `A`

for further solving `Ax=b`

problems.

The `compute`

method is equivalent to calling both `analyzePattern`

and `factorize`

.

# The Solve step

The `solve`

function computes the solution of the linear systems with one or many right hand sides.

x <- solve b

Here, `b`

can be a vector or a matrix where the columns form the different right hand sides.
The `solve`

function can be called several times as well, for instance when all the right hand sides are not available at once.

x1 <- solve b1 -- Get the second right hand side b2 x2 <- solve b2 -- ...

solve :: (Solver s, MonadIO p, Elem a) => SparseMatrix n m a -> SolverT s a p (SparseMatrix n m a) Source #

An expression of the solution `x`

of `Ax=b`

using the current decomposition of `A`

.

info :: (Solver s, MonadIO p, Elem a) => SolverT s a p ComputationInfo Source #

The solution `x`

of `Ax=b`

using the current decomposition of `A`

and `x0`

as an initial solution.
solveWithGuess :: (Solver s, MonadIO p, I.Elem a) => SM.SparseMatrix n m a -> SM.SparseMatrix n m a -> SolverT s a p (SM.SparseMatrix n m a)
solveWithGuess (SM.SparseMatrix fb) (SM.SparseMatrix fx0) = SolverT $ ask >>= (i,fs) -> liftIO $
withForeignPtr fs $ s ->
withForeignPtr fb $ b ->
withForeignPtr fx0 $ x0 ->
alloca $ px -> do
I.call $ I.sparse_la_solveWithGuess i s b x0 px
x <- peek px
SM.SparseMatrix $ FC.newForeignPtr x (I.call $ I.sparse_free x)

`Success`

if the iterations converged or computation was succesful`NumericalIssue`

if the factorization reports a numerical problem`NoConvergence`

if the iterations are not converged`InvalidInput`

if the input matrix is invalid

# Iterative Solvers

tolerance :: (IterativeSolver s, MonadIO p, Elem a) => SolverT s a p Double Source #

The tolerance threshold used by the stopping criteria.

setTolerance :: (IterativeSolver s, MonadIO p, Elem a) => Double -> SolverT s a p () Source #

Sets the tolerance threshold used by the stopping criteria.

This value is used as an upper bound to the relative residual error: `|Ax-b|/|b|`

. The default value is the machine precision given by `epsilon`

maxIterations :: (IterativeSolver s, MonadIO p, Elem a) => SolverT s a p Int Source #

The max number of iterations. It is either the value setted by setMaxIterations or, by default, twice the number of columns of the matrix.

setMaxIterations :: (IterativeSolver s, MonadIO p, Elem a) => Int -> SolverT s a p () Source #

Sets the max number of iterations. Default is twice the number of columns of the matrix.

error :: (IterativeSolver s, MonadIO p, Elem a) => SolverT s a p Double Source #

The tolerance error reached during the last solve. It is a close approximation of the true relative residual error `|Ax-b|/|b|`

.

iterations :: (IterativeSolver s, MonadIO p, Elem a) => SolverT s a p Int Source #

The number of iterations performed during the last solve

# SparseQR Solver

matrixR :: (MonadIO p, Elem a) => SolverT SparseQR a p (SparseMatrix n m a) Source #

Returns the `b`

sparse upper triangular matrix `R`

of the QR factorization.

matrixQ :: (MonadIO p, Elem a) => SolverT SparseQR a p (SparseMatrix n m a) Source #

Returns the matrix `Q`

as products of sparse Householder reflectors.

rank :: (MonadIO p, Elem a) => SolverT SparseQR a p Int Source #

Returns the number of non linearly dependent columns as determined by the pivoting threshold.

setPivotThreshold :: (MonadIO p, Elem a) => Double -> SolverT SparseQR a p () Source #

Sets the threshold that is used to determine linearly dependent columns during the factorization.

In practice, if during the factorization the norm of the column that has to be eliminated is below this threshold, then the entire column is treated as zero, and it is moved at the end.

# SparseLU Solver

setSymmetric :: (MonadIO p, Elem a) => Bool -> SolverT SparseLU a p () Source #

Indicate that the pattern of the input matrix is symmetric

matrixL :: (MonadIO p, Elem a) => SolverT SparseLU a p (SparseMatrix n m a) Source #

Returns the matrix `L`

matrixU :: (MonadIO p, Elem a) => SolverT SparseLU a p (SparseMatrix n m a) Source #

Returns the matrix `U`

absDeterminant :: (MonadIO p, Elem a) => SolverT SparseLU a p a Source #

The absolute value of the determinant of the matrix of which *this is the QR decomposition.

A determinant can be very big or small, so for matrices of large enough dimension, there is a risk of overflow/underflow.
One way to work around that is to use `logAbsDeterminant`

instead.