sparse-linear-algebra-0.1.0.1: Sparse linear algebra datastructures and algorithms. Currently it provides iterative linear solvers, matrix decompositions, eigenvalue computations and related utilities.

Safe HaskellNone
LanguageHaskell2010

Math.Linear.Sparse

Contents

Synopsis

Additive ring

class Functor f => Additive f where Source #

Minimal complete definition

zero, (^+^)

Methods

zero :: Num a => f a Source #

Ring zero element

(^+^) :: Num a => f a -> f a -> f a Source #

Ring +

Instances

negated :: (Num a, Functor f) => f a -> f a Source #

negate the values in a functor

(^-^) :: (Additive f, Num a) => f a -> f a -> f a Source #

subtract two Additive objects

Vector space

class Additive f => VectorSpace f where Source #

Minimal complete definition

(.*)

Methods

(.*) :: Num a => a -> f a -> f a Source #

multiplication by a scalar

Instances

VectorSpace IntMap Source # 

Methods

(.*) :: Num a => a -> IntMap a -> IntMap a Source #

VectorSpace SpVector Source # 

Methods

(.*) :: Num a => a -> SpVector a -> SpVector a Source #

lerp :: (VectorSpace f, Num a) => a -> f a -> f a -> f a Source #

linear interpolation

Hilbert space (inner product)

class VectorSpace f => Hilbert f where Source #

Minimal complete definition

dot

Methods

dot :: Num a => f a -> f a -> a Source #

inner product

Instances

Hilbert IntMap Source # 

Methods

dot :: Num a => IntMap a -> IntMap a -> a Source #

Hilbert SpVector Source # 

Methods

dot :: Num a => SpVector a -> SpVector a -> a Source #

Normed vector space

class Hilbert f => Normed f where Source #

Minimal complete definition

norm

Methods

norm :: (Floating a, Eq a) => a -> f a -> a Source #

Instances

Normed IntMap Source # 

Methods

norm :: (Floating a, Eq a) => a -> IntMap a -> a Source #

Normed SpVector Source # 

Methods

norm :: (Floating a, Eq a) => a -> SpVector a -> a Source #

Norms and related results

squared 2-norm

normSq :: (Hilbert f, Num a) => f a -> a Source #

L1 norm

norm1 :: (Foldable t, Num a, Functor t) => t a -> a Source #

Euclidean norm

norm2 :: (Hilbert f, Floating a) => f a -> a Source #

Lp norm (p > 0)

normP :: (Foldable t, Functor t, Floating a) => a -> t a -> a Source #

infinity-norm

normInfty :: (Foldable t, Ord a) => t a -> a Source #

normalize w.r.t. p-norm (p finite)

normalize :: (Normed f, Floating a, Eq a) => a -> f a -> f a Source #

Lp inner product (p > 0)

dotLp :: (Set t, Foldable t, Floating a) => a -> t a -> t a -> a Source #

reciprocal

reciprocal :: (Functor f, Fractional b) => f b -> f b Source #

scale

scale :: (Num b, Functor f) => b -> f b -> f b Source #

FiniteDim : finite-dimensional objects

class Additive f => FiniteDim f where Source #

Minimal complete definition

dim

Associated Types

type FDSize f :: * Source #

Methods

dim :: f a -> FDSize f Source #

Instances

FiniteDim SpMatrix Source # 

Associated Types

type FDSize (SpMatrix :: * -> *) :: * Source #

FiniteDim SpVector Source # 

Associated Types

type FDSize (SpVector :: * -> *) :: * Source #

withDim :: (FiniteDim f, Show e) => f a -> (FDSize f -> f a -> Bool) -> (f a -> c) -> String -> (f a -> e) -> c Source #

unary dimension-checking bracket

withDim2 :: (FiniteDim f, FiniteDim g, Show e) => f a -> g b -> (FDSize f -> FDSize g -> f a -> g b -> Bool) -> (f a -> g b -> c) -> String -> (f a -> g b -> e) -> c Source #

binary dimension-checking bracket

HasData : accessing inner data (do not export)

class Additive f => HasData f a where Source #

Minimal complete definition

dat

Associated Types

type HDData f a :: * Source #

Methods

dat :: f a -> HDData f a Source #

Instances

HasData SpMatrix a Source # 

Associated Types

type HDData (SpMatrix :: * -> *) a :: * Source #

Methods

dat :: SpMatrix a -> HDData SpMatrix a Source #

HasData SpVector a Source # 

Associated Types

type HDData (SpVector :: * -> *) a :: * Source #

Methods

dat :: SpVector a -> HDData SpVector a Source #

Sparse : sparse datastructures

class (FiniteDim f, HasData f a) => Sparse f a where Source #

Minimal complete definition

spy

Methods

spy :: Fractional b => f a -> b Source #

Instances

Sparse SpMatrix a Source # 

Methods

spy :: Fractional b => SpMatrix a -> b Source #

Sparse SpVector a Source # 

Methods

spy :: Fractional b => SpVector a -> b Source #

Set : things that behave as sets (e.g. of which we can take the union and the intersection)

class Functor f => Set f where Source #

Minimal complete definition

liftU2, liftI2

Methods

liftU2 :: (a -> a -> a) -> f a -> f a -> f a Source #

union binary lift

liftI2 :: (a -> b -> c) -> f a -> f b -> f c Source #

intersection binary lift

Instances

Set IntMap Source # 

Methods

liftU2 :: (a -> a -> a) -> IntMap a -> IntMap a -> IntMap a Source #

liftI2 :: (a -> b -> c) -> IntMap a -> IntMap b -> IntMap c Source #

Set SpMatrix Source # 

Methods

liftU2 :: (a -> a -> a) -> SpMatrix a -> SpMatrix a -> SpMatrix a Source #

liftI2 :: (a -> b -> c) -> SpMatrix a -> SpMatrix b -> SpMatrix c Source #

Set SpVector Source # 

Methods

liftU2 :: (a -> a -> a) -> SpVector a -> SpVector a -> SpVector a Source #

liftI2 :: (a -> b -> c) -> SpVector a -> SpVector b -> SpVector c Source #

IntMap implementation

Sparse Vector

data SpVector a Source #

Constructors

SV 

Fields

Instances

Functor SpVector Source # 

Methods

fmap :: (a -> b) -> SpVector a -> SpVector b #

(<$) :: a -> SpVector b -> SpVector a #

Foldable SpVector Source # 

Methods

fold :: Monoid m => SpVector m -> m #

foldMap :: Monoid m => (a -> m) -> SpVector a -> m #

foldr :: (a -> b -> b) -> b -> SpVector a -> b #

foldr' :: (a -> b -> b) -> b -> SpVector a -> b #

foldl :: (b -> a -> b) -> b -> SpVector a -> b #

foldl' :: (b -> a -> b) -> b -> SpVector a -> b #

foldr1 :: (a -> a -> a) -> SpVector a -> a #

foldl1 :: (a -> a -> a) -> SpVector a -> a #

toList :: SpVector a -> [a] #

null :: SpVector a -> Bool #

length :: SpVector a -> Int #

elem :: Eq a => a -> SpVector a -> Bool #

maximum :: Ord a => SpVector a -> a #

minimum :: Ord a => SpVector a -> a #

sum :: Num a => SpVector a -> a #

product :: Num a => SpVector a -> a #

Set SpVector Source # 

Methods

liftU2 :: (a -> a -> a) -> SpVector a -> SpVector a -> SpVector a Source #

liftI2 :: (a -> b -> c) -> SpVector a -> SpVector b -> SpVector c Source #

FiniteDim SpVector Source # 

Associated Types

type FDSize (SpVector :: * -> *) :: * Source #

Normed SpVector Source # 

Methods

norm :: (Floating a, Eq a) => a -> SpVector a -> a Source #

Hilbert SpVector Source # 

Methods

dot :: Num a => SpVector a -> SpVector a -> a Source #

VectorSpace SpVector Source # 

Methods

(.*) :: Num a => a -> SpVector a -> SpVector a Source #

Additive SpVector Source # 

Methods

zero :: Num a => SpVector a Source #

(^+^) :: Num a => SpVector a -> SpVector a -> SpVector a Source #

Sparse SpVector a Source # 

Methods

spy :: Fractional b => SpVector a -> b Source #

HasData SpVector a Source # 

Associated Types

type HDData (SpVector :: * -> *) a :: * Source #

Methods

dat :: SpVector a -> HDData SpVector a Source #

Eq a => Eq (SpVector a) Source # 

Methods

(==) :: SpVector a -> SpVector a -> Bool #

(/=) :: SpVector a -> SpVector a -> Bool #

Show a => Show (SpVector a) Source # 

Methods

showsPrec :: Int -> SpVector a -> ShowS #

show :: SpVector a -> String #

showList :: [SpVector a] -> ShowS #

(Show a, Num a) => PrintDense (SpVector a) Source # 

Methods

prd :: SpVector a -> IO () Source #

type FDSize SpVector Source # 
type HDData SpVector a Source # 

spySV :: Fractional b => SpVector a -> b Source #

SpVector sparsity

instances for SpVector

zeroSV :: Int -> SpVector a Source #

empty sparse vector (size n, no entries)

Create new sparse vector

mkSpVector :: (Num a, Eq a) => Int -> IntMap a -> SpVector a Source #

create a sparse vector from an association list while discarding all zero entries

mkSpVectorD :: (Num a, Eq a) => Int -> [a] -> SpVector a Source #

", from logically dense array (consecutive indices)

fromListDenseSV :: Int -> [a] -> SpVector a Source #

Create new sparse vector, assumin 0-based, contiguous indexing

onesSV :: Num a => Int -> SpVector a Source #

DENSE vector of `1`s

zerosSV :: Num a => Int -> SpVector a Source #

DENSE vector of `0`s

insertSpVector :: Int -> a -> SpVector a -> SpVector a Source #

insert element x at index i in a preexisting SpVector

fromListSV :: Int -> [(Int, a)] -> SpVector a Source #

toListSV :: SpVector a -> [(Key, a)] Source #

toList

toDenseListSV :: Num b => SpVector b -> [b] Source #

To dense list (default = 0)

lookupDenseSV :: Num a => Key -> SpVector a -> a Source #

lookup

tailSV :: SpVector a -> SpVector a Source #

SV manipulation

Tail elements

headSV :: Num a => SpVector a -> a Source #

Head element

concatSV :: SpVector a -> SpVector a -> SpVector a Source #

concatenate SpVector

svToSM :: SpVector a -> SpMatrix a Source #

promote a SV to SM

Outer vector product

(><) :: Num a => SpVector a -> SpVector a -> SpMatrix a Source #

Sparse Matrix

data SpMatrix a Source #

Constructors

SM 

Fields

Instances

Functor SpMatrix Source # 

Methods

fmap :: (a -> b) -> SpMatrix a -> SpMatrix b #

(<$) :: a -> SpMatrix b -> SpMatrix a #

Set SpMatrix Source # 

Methods

liftU2 :: (a -> a -> a) -> SpMatrix a -> SpMatrix a -> SpMatrix a Source #

liftI2 :: (a -> b -> c) -> SpMatrix a -> SpMatrix b -> SpMatrix c Source #

FiniteDim SpMatrix Source # 

Associated Types

type FDSize (SpMatrix :: * -> *) :: * Source #

Additive SpMatrix Source # 

Methods

zero :: Num a => SpMatrix a Source #

(^+^) :: Num a => SpMatrix a -> SpMatrix a -> SpMatrix a Source #

Sparse SpMatrix a Source # 

Methods

spy :: Fractional b => SpMatrix a -> b Source #

HasData SpMatrix a Source # 

Associated Types

type HDData (SpMatrix :: * -> *) a :: * Source #

Methods

dat :: SpMatrix a -> HDData SpMatrix a Source #

Eq a => Eq (SpMatrix a) Source # 

Methods

(==) :: SpMatrix a -> SpMatrix a -> Bool #

(/=) :: SpMatrix a -> SpMatrix a -> Bool #

Show a => Show (SpMatrix a) Source # 

Methods

showsPrec :: Int -> SpMatrix a -> ShowS #

show :: SpMatrix a -> String #

showList :: [SpMatrix a] -> ShowS #

(Show a, Num a) => PrintDense (SpMatrix a) Source # 

Methods

prd :: SpMatrix a -> IO () Source #

type FDSize SpMatrix Source # 
type HDData SpMatrix a Source # 

Instances for SpMatrix

maxTup :: Ord t => (t, t) -> (t, t) -> (t, t) Source #

TODO : use semilattice properties instead

minTup :: Ord t => (t, t) -> (t, t) -> (t, t) Source #

TODO : use semilattice properties instead

emptySpMatrix :: (Int, Int) -> SpMatrix a Source #

empty matrix of size d

multiply matrix by a scalar

matScale :: Num a => a -> SpMatrix a -> SpMatrix a Source #

Frobenius norm (sqrt of trace of M^T M)

MATRIX METADATA

type Rows = Int Source #

type Cols = Int Source #

type IxRow = Int Source #

type IxCol = Int Source #

predicates

validIxSM :: SpMatrix a -> (Int, Int) -> Bool Source #

Are the supplied indices within matrix bounds?

isSquareSM :: SpMatrix a -> Bool Source #

Is the matrix square?

isDiagonalSM :: SpMatrix a -> Bool Source #

Is the matrix diagonal?

nrows :: SpMatrix a -> Int Source #

nrows, ncols : size accessors

ncols :: SpMatrix a -> Int Source #

nrows, ncols : size accessors

data SMInfo Source #

Constructors

SMInfo 

Fields

Instances

Non-zero elements in a row

bandwidth bounds (min, max)

SPARSE MATRIX BUILDERS

fromListSM' :: Foldable t => t (IxRow, IxCol, a) -> SpMatrix a -> SpMatrix a Source #

Add to existing SpMatrix using data from list (row, col, value)

fromListSM :: Foldable t => (Int, Int) -> t (IxRow, IxCol, a) -> SpMatrix a Source #

Create new SpMatrix using data from list (row, col, value)

fromListDenseSM :: Int -> [a] -> SpMatrix a Source #

Create new SpMatrix assuming contiguous, 0-based indexing of elements

toDenseListSM :: Num t => SpMatrix t -> [(IxRow, IxCol, t)] Source #

to List

Populate missing entries with 0

Diagonal matrix

mkDiagonal :: Int -> [a] -> SpMatrix a Source #

Identity matrix

eye :: Num a => Int -> SpMatrix a Source #

ones :: Num a => Int -> [a] Source #

Create Super- or sub- diagonal matrix

mkSubDiagonal :: Int -> Int -> [a] -> SpMatrix a Source #

encode :: (Int, Int) -> (Rows, Cols) -> Int Source #

decode :: (Int, Int) -> Int -> (Rows, Cols) Source #

SUB-MATRICES

toSV :: SpMatrix a -> SpVector a Source #

Demote (n x 1) or (1 x n) SpMatrix to SpVector

Extract jth column

extractCol :: SpMatrix a -> Int -> SpVector a Source #

", and place into SpVector

Extract ith row

extractRow :: SpMatrix a -> Int -> SpVector a Source #

", and place into SpVector

MATRIX STACKING

vertStackSM :: SpMatrix a -> SpMatrix a -> SpMatrix a Source #

Vertical stacking

(-=-) :: SpMatrix a -> SpMatrix a -> SpMatrix a Source #

Vertical stacking

horizStackSM :: SpMatrix a -> SpMatrix a -> SpMatrix a Source #

Horizontal stacking

(-||-) :: SpMatrix a -> SpMatrix a -> SpMatrix a Source #

Horizontal stacking

MATRIX ELEMENT LOOKUP

lookupWD_SM :: Num a => SpMatrix a -> (Key, Key) -> a Source #

Looks up an element in the matrix with a default (if the element is not found, zero is returned)

(@@) :: Num a => SpMatrix a -> (Key, Key) -> a Source #

Zero-default lookup, infix form

Looks up an element in the matrix with a default (if the element is not found, zero is returned)

lookupWD_IM :: Num a => IntMap (IntMap a) -> (Key, Key) -> a Source #

MISC SpMatrix OPERATIONS

foldlSM :: (a -> b -> b) -> b -> SpMatrix a -> b Source #

ifoldlSM :: (Key -> Key -> a -> b -> b) -> b -> SpMatrix a -> b Source #

countSubdiagonalNZSM :: SpMatrix a -> Int Source #

mapping

folding

extractDiagonalDSM :: Num a => SpMatrix a -> SpVector a Source #

filtering

extract with default 0

sparsify : remove 0s (!!!)

ROUNDING operations (!!!)

ALGEBRAIC PRIMITIVE OPERATIONS

Matrix transpose

matrix action on a vector

matVec :: Num a => SpMatrix a -> SpVector a -> SpVector a Source #

matrix on vector

(#>) :: Num a => SpMatrix a -> SpVector a -> SpVector a Source #

matrix on vector

vecMat :: Num a => SpVector a -> SpMatrix a -> SpVector a Source #

vector on matrix (FIXME : transposes matrix: more costly than matVec, I think)

(<#) :: Num a => SpVector a -> SpMatrix a -> SpVector a Source #

vector on matrix (FIXME : transposes matrix: more costly than matVec, I think)

Matrix-matrix product

(##) :: Num a => SpMatrix a -> SpMatrix a -> SpMatrix a Source #

Matrix-matrix product, sparsified (prunes all elements x for which `abs x <= eps`)

Predicates

isOrthogonalSM :: SpMatrix Double -> Bool Source #

is the matrix orthogonal? i.e. Q^t ## Q == I

Condition number

conditionNumberSM :: SpMatrix Double -> Double Source #

uses the R matrix from the QR factorization

Householder transformation

hhMat :: Num a => a -> SpVector a -> SpMatrix a Source #

hhRefl :: SpVector Double -> SpMatrix Double Source #

a vector x uniquely defines an orthogonal plane; the Householder operator reflects any point v with respect to this plane: v' = (I - 2 x >< x) v

Givens rotation matrix

hypot :: Floating a => a -> a -> a Source #

sign :: (Ord a, Num a) => a -> a Source #

givensCoef :: (Ord a, Floating a) => a -> a -> (a, a, a) Source #

Givens coefficients (using stable algorithm shown in Anderson, Edward (4 December 2000). "Discontinuous Plane Rotations and the Symmetric Eigenvalue Problem". LAPACK Working Note)

givens :: SpMatrix Double -> Int -> Int -> SpMatrix Double Source #

Givens method, row version: choose other row index i' s.t. i' is : * below the diagonal * corresponding element is nonzero

QR.C1 ) To zero out entry A(i, j) we must find row k such that A(k, j) is non-zero but A has zeros in row k for all columns less than j.

firstNonZeroColumn :: IntMap a -> Key -> Bool Source #

Is the kth the first nonzero column in the row?

candidateRows :: IntMap (IntMap a) -> Key -> Key -> Maybe [Key] Source #

Returns a set of rows {k} that satisfy QR.C1

QR decomposition

qr :: SpMatrix Double -> (SpMatrix Double, SpMatrix Double) Source #

Applies Givens rotation iteratively to zero out sub-diagonal elements

gmats :: SpMatrix Double -> [SpMatrix Double] Source #

Givens matrices in order [G1, G2, .. , G_N ]

Eigenvalue algorithms

All eigenvalues using QR algorithm

One eigenvalue and corresponding eigenvector, using Rayleigh iteration

rayleighStep :: SpMatrix Double -> (SpVector Double, Double) -> (SpVector Double, Double) Source #

Cubic-order convergence, but it requires a mildly educated guess on the initial eigenpair

Householder vector (G & VL Alg. 5.1.1, function house)

SVD

LINEAR SOLVERS : solve A x = b

eps :: Double Source #

numerical tolerance for e.g. solution convergence

residual :: Num a => SpMatrix a -> SpVector a -> SpVector a -> SpVector a Source #

residual of candidate solution x0

CGS

cgsStep :: SpMatrix Double -> SpVector Double -> CGS -> CGS Source #

one step of CGS

data CGS Source #

Constructors

CGS 

Instances

Eq CGS Source # 

Methods

(==) :: CGS -> CGS -> Bool #

(/=) :: CGS -> CGS -> Bool #

Show CGS Source # 

Methods

showsPrec :: Int -> CGS -> ShowS #

show :: CGS -> String #

showList :: [CGS] -> ShowS #

cgs :: SpMatrix Double -> SpVector Double -> SpVector Double -> SpVector Double -> CGS Source #

iterate solver until convergence or until max # of iterations is reached

BiCSSTAB

bicgstab :: SpMatrix Double -> SpVector Double -> SpVector Double -> SpVector Double -> BICGSTAB Source #

iterate solver until convergence or until max # of iterations is reached

LINEAR SOLVERS INTERFACE

linSolveM :: PrimMonad m => LinSolveMethod -> SpMatrix Double -> SpVector Double -> m (SpVector Double) Source #

linear solve with _random_ starting vector

linSolve :: LinSolveMethod -> SpMatrix Double -> SpVector Double -> SpVector Double Source #

linear solve with _deterministic_ starting vector (every component at 0.1)

PRETTY PRINTING

showNonZero :: (Show a, Num a, Eq a) => a -> String Source #

toDenseRow :: Num a => SpMatrix a -> Key -> [a] Source #

toDenseRowClip :: (Show a, Num a) => SpMatrix a -> Key -> Int -> String Source #

printDenseSM :: (Show t, Num t) => SpMatrix t -> IO () Source #

printDenseSV :: (Show t, Num t) => SpVector t -> IO () Source #

Pretty printer typeclass

class PrintDense a where Source #

Minimal complete definition

prd

Methods

prd :: a -> IO () Source #

Instances

(Show a, Num a) => PrintDense (SpMatrix a) Source # 

Methods

prd :: SpMatrix a -> IO () Source #

(Show a, Num a) => PrintDense (SpVector a) Source # 

Methods

prd :: SpVector a -> IO () Source #

almostZero :: Double -> Bool Source #

rounding to 0 or 1 within some predefined numerical precision

almostOne :: Double -> Bool Source #

rounding to 0 or 1 within some predefined numerical precision

withDefault :: (t -> Bool) -> t -> t -> t Source #

with2Defaults :: (t -> Bool) -> (t -> Bool) -> t -> t -> t -> t Source #

modifyUntil :: MonadState s m => (s -> Bool) -> (s -> s) -> m s Source #

transform state until a condition is met

loopUntilAcc :: Int -> ([t] -> Bool) -> (t -> t) -> t -> t Source #

modifyInspectN :: MonadState s m => Int -> ([s] -> Bool) -> (s -> s) -> m s Source #

keep state x in a moving window of length 2 to assess convergence, stop when either a condition on that list is satisfied or when max # of iterations is reached

meanl :: (Foldable t, Fractional a) => t a -> a Source #

norm2l :: (Foldable t, Functor t, Floating a) => t a -> a Source #

diffSqL :: Floating a => [a] -> a Source #

untilConverged :: MonadState a m => (a -> SpVector Double) -> (a -> a) -> m a Source #

runAppendN :: ([t] -> Bool) -> (t -> t) -> Int -> t -> [t] Source #

runAppendN' :: (t -> t) -> Int -> t -> [t] Source #

randMat :: PrimMonad m => Int -> m (SpMatrix Double) Source #

random matrices and vectors

denseIxArray :: [b] -> [(Int, b)] Source #

misc utils

integer-indexed ziplist

denseIxArray2 :: Int -> [c] -> [(Int, Int, c)] Source #

foldrMap :: (Foldable t, Functor t) => (a -> b) -> (b -> c -> c) -> c -> t a -> c Source #

foldlStrict :: (a -> b -> a) -> a -> [b] -> a Source #

ifoldr :: Num i => (a -> b -> b) -> b -> (i -> c -> d -> a) -> c -> [d] -> b Source #

type LB = Int Source #

type UB = Int Source #

inBounds :: LB -> UB -> Int -> Bool Source #

inBounds2 :: (LB, UB) -> (Int, Int) -> Bool Source #

inBounds02 :: (UB, UB) -> (Int, Int) -> Bool Source #