Module

# Data.Sparse.Matrix

Package
purescript-sparse-matrices
Repository
Ebmtranceboy/purescript-sparse-matrices

### #MatrixSource

``newtype Matrix a``

#### Constructors

• `Matrix { coefficients :: Poly2 a, height :: Int, width :: Int }`

#### Instances

• `(Show a, Semiring a, Eq a) => Show (Matrix a)`
• `(Eq a) => Eq (Matrix a)`
• `(Eq a, Semiring a) => Semiring (Matrix a)`
• `Functor Matrix`
• `(Eq a, Ring a) => Ring (Matrix a)`

### #Poly2Source

``type Poly2 a = Polynomial (Polynomial a)``

Representation of a matrix without storage of the zero coefficients.

``````> -- Working with Sparse Matrices
> import Data.Sparse.Matrix
> import Data.Sparse.Polynomial ((^))
> -- Define a matrix with sum of terms (see Data.Sparse.Polynomial)
> a = Matrix { height:3, width:2, coefficients: 5.0^2^0+7.0^0^1 }
> a
0.0 7.0
0.0 0.0
5.0 0.0

> -- Modify all the non-zero elements with the functor
> b = (_ + 2.0) <\$> a
> b
0.0 9.0
0.0 0.0
7.0 0.0

> -- Set a specific element
> c = 2.0^1^0 ~ b
> c
0.0 9.0
2.0 0.0
7.0 0.0

> -- Coefficient extraction
> c ?? [1,0]
2.0

> -- Column(s) extraction
> c * Matrix { height: 3, width: 1, coefficients: 1.0^1^0 }
9.0
0.0
0.0

> -- Row(s) extraction
> Matrix { height: 2, width: 3, coefficients: 1.0^0^0 + 1.0^1^2 } * c
0.0 9.0
7.0 0.0

> -- transpose, multiply
> d = a * transpose c
> d
63.0 0.0 0.0
0.0 0.0 0.0
0.0 10.0 35.0

> -- Weight a specific element
> e = 0.5^0^0 ~* d
> e
31.5 0.0 0.0
0.0 0.0 0.0
0.0 10.0 35.0

> -- Increment a specific element
> f = 4.0^1^1 ~+ e
> f
31.5 0.0 0.0
0.0 4.0 0.0
0.0 10.0 35.0

> -- Identity, subtraction
> g = f - eye 3
> g
30.5 0.0 0.0
0.0 3.0 0.0
0.0 10.0 34.0

> -- Vectors are just column matrices
> v = Matrix { height:3, width:1, coefficients: 1.0^0^0+2.0^1^0+3.0^2^0 }
> v
1.0
2.0
3.0

> -- Solve the linear system
> pluSolve g v
0.03278688524590164
0.6666666666666666
-0.10784313725490192

``````

### #SquareSource

``type Square a = Matrix a``

#### Instances

• `(Eq a, DivisionRing a, Ord a, Divisible a, Leadable a) => DivisionRing (Square a)`

### #SymmetricSource

``type Symmetric = Square Number``

### #VectorSource

``type Vector a = Matrix a``

### #applyPolySource

``applyPoly :: forall a. Eq a => Semiring a => Polynomial (Square a) -> Square a -> Square a``

Polynomial application

### #coefficientsSource

``coefficients :: forall a. Matrix a -> Poly2 a``

### #determinantSource

``determinant :: forall a. Eq a => Ord a => Ring a => DivisionRing a => Divisible a => Leadable a => Square a -> a``

Determinant of a square matrix in a field

### #diagonalizeSource

``diagonalize :: Symmetric -> { val :: Symmetric, vec :: Square Number }``

Square real matrix diagonalization such that m = vec * val * recip vec

### #eigenValuesSource

``eigenValues :: Square Number -> Array (Cartesian Number)``

Eigen values of a real square matrix

### #extractSource

``extract :: forall a. Eq a => Semiring a => Matrix a -> Array Int -> a``

Coefficient extraction

### #eyeSource

``eye :: forall a. Eq a => Semiring a => Int -> Square a``

Identity matrix

``faddeev :: Square Number -> Polynomial Number``

Characteristic polynomial of a real square matrix

### #heightSource

``height :: forall a. Matrix a -> Int``

### #incrementSource

``increment :: forall a. Eq a => Semiring a => Ring a => Poly2 a -> Matrix a -> Matrix a``

Element increment

### #internalMapSource

``internalMap :: forall a. Polynomial a -> Map Int a``

Returns the polynomial internal structure

### #luSolveSource

``luSolve :: forall a. Eq a => Ring a => DivisionRing a => Square a -> Square a -> Matrix a -> Matrix a``

Solves the system LUx=B for x, for L,U square inversible and any B

### #parseMonomSource

``parseMonom :: forall a. Eq a => Semiring a => Poly2 a -> Maybe { i :: Int, j :: Int, v :: a }``

### #pivotSource

``pivot :: forall a. Eq a => Ord a => Ring a => Int -> Matrix a -> Int``

Returns the row index of the maximum element (in magnitude) among all the elements of the column whose index is provided below (possibly as high as) the diagonal element.

### #pivot'Source

``pivot' :: forall a. Eq a => Ord a => Ring a => Int -> Int -> Matrix a -> Maybe Int``

### #pluSource

``plu :: forall a. Eq a => Ring a => Ord a => DivisionRing a => Divisible a => Leadable a => Square a -> { l :: Square a, p :: Square a, parity :: Boolean, u :: Square a }``

Returns the 3 square matrices P, L and U and a Boolean such that

• LU = PA where A is the given square matrix with not null determinant
• P is a permutation matrix with parity given by the Boolean (true: 1, false: -1)
• L is triangular inferior with unitary diagonal elements
• U is triangular superior

### #pluSolveSource

``pluSolve :: forall a. Eq a => Ring a => Ord a => Divisible a => Leadable a => DivisionRing a => Square a -> Matrix a -> Matrix a``

Solves the system Ax=B for x, for A square inversible and any B

### #powSource

``pow :: forall a. Eq a => Semiring a => Square a -> Int -> Square a``

Integer power of a square matrix

### #replaceSource

``replace :: forall a. Eq a => Semiring a => Poly2 a -> Matrix a -> Matrix a``

Element replacement

### #replace'Source

``replace' :: forall a. Eq a => Semiring a => Int -> Int -> a -> Poly2 a -> Poly2 a``

### #ringDeterminantSource

``ringDeterminant :: forall a. Ring a => Eq a => Ord a => Divisible a => Leadable a => EuclideanRing a => Matrix a -> a``

Determinant of a square matrix in a ring

### #rowCombinationSource

``rowCombination :: forall a. Eq a => Divisible a => Ring a => Leadable a => Int -> a -> Int -> a -> Matrix a -> Matrix a``

`rowCombination orig ko dest kd` performs : dest <- ko * orig + kd * dest which only alters dest

### #scaleSource

``scale :: forall a. Eq a => Semiring a => Ring a => Poly2 a -> Matrix a -> Matrix a``

Element weighting

### #swapRowsSource

``swapRows :: forall a. Eq a => Divisible a => Ring a => Leadable a => Int -> Int -> Matrix a -> Matrix a``

As the name suggests. Note that the two rows are provided as 0-indices

### #traceSource

``trace :: forall a. Eq a => Semiring a => Square a -> a``

### #transposeSource

``transpose :: forall a. Eq a => Semiring a => Matrix a -> Matrix a``

Matrix transposition

### #widthSource

``width :: forall a. Matrix a -> Int``

### #zerosSource

``zeros :: forall a. Eq a => Semiring a => Int -> Int -> Matrix a``

Zero matrix

### #(??)Source

Operator alias for Data.Sparse.Matrix.extract (left-associative / precedence 8)

Coefficient extraction infix notation

### #(~)Source

Operator alias for Data.Sparse.Matrix.replace (non-associative / precedence 7)

Element replacement infix notation

### #(~*)Source

Operator alias for Data.Sparse.Matrix.scale (non-associative / precedence 7)

Element weighting infix notation

### #(~+)Source

Operator alias for Data.Sparse.Matrix.increment (non-associative / precedence 7)

Element increment infix notation