statistics-0.13.3.0: A library of statistical types, data, and functions

Copyright | 2011 Aleksey Khudyakov, 2014 Bryan O'Sullivan
License | BSD3
Safe Haskell | None
Language | Haskell98

Statistics.Matrix

Contents

- Data types
- Conversion fromto listsvectors
- Other

Description

Basic matrix operations.

There isn't a widely used matrix package for Haskell yet, so we implement the necessary minimum here.

Synopsis

- data Matrix = Matrix {
  - rows :: !Int
  - cols :: !Int
  - exponent :: !Int
  - _vector :: !Vector
  }
- type Vector = Vector Double
- fromVector :: Int -> Int -> Vector Double -> Matrix
- fromList :: Int -> Int -> [Double] -> Matrix
- fromRowLists :: [[Double]] -> Matrix
- fromRows :: [Vector] -> Matrix
- fromColumns :: [Vector] -> Matrix
- toVector :: Matrix -> Vector Double
- toList :: Matrix -> [Double]
- toRows :: Matrix -> [Vector]
- toColumns :: Matrix -> [Vector]
- toRowLists :: Matrix -> [[Double]]
- generate :: Int -> Int -> (Int -> Int -> Double) -> Matrix
- generateSym :: Int -> (Int -> Int -> Double) -> Matrix
- ident :: Int -> Matrix
- diag :: Vector -> Matrix
- dimension :: Matrix -> (Int, Int)
- center :: Matrix -> Double
- multiply :: Matrix -> Matrix -> Matrix
- multiplyV :: Matrix -> Vector -> Vector
- transpose :: Matrix -> Matrix
- power :: Matrix -> Int -> Matrix
- norm :: Vector -> Double
- column :: Matrix -> Int -> Vector
- row :: Matrix -> Int -> Vector
- map :: (Double -> Double) -> Matrix -> Matrix
- for :: Monad m => Int -> Int -> (Int -> m ()) -> m ()
- unsafeIndex :: Matrix -> Int -> Int -> Double
- hasNaN :: Matrix -> Bool
- bounds :: (Vector -> Int -> r) -> Matrix -> Int -> Int -> r
- unsafeBounds :: (Vector -> Int -> r) -> Matrix -> Int -> Int -> r

# Data types

data Matrix #

Two-dimensional matrix, stored in row-major order.

Constructors

Matrix |
Fields rows :: !Int Rows of matrix. cols :: !Int Columns of matrix. exponent :: !Int In order to avoid overflows during matrix multiplication, a large exponent is stored separately. _vector :: !Vector Matrix data.

Instances

Eq Matrix # |
Methods (==) :: Matrix -> Matrix -> Bool # (/=) :: Matrix -> Matrix -> Bool #
Show Matrix # |
Methods showsPrec :: Int -> Matrix -> ShowS # show :: Matrix -> String # showList :: [Matrix] -> ShowS #

type Vector = Vector Double #

# Conversion fromto listsvectors

fromVector #

Arguments

:: Int | Number of rows.
-> Int | Number of columns.
-> Vector Double | Flat list of values, in row-major order.
-> Matrix |

Convert from a row-major vector.

fromList #

Arguments

:: Int | Number of rows.
-> Int | Number of columns.
-> [Double] | Flat list of values, in row-major order.
-> Matrix |

Convert from a row-major list.

fromRowLists :: [[Double]] -> Matrix #

create a matrix from a list of lists, as rows

fromRows :: [Vector] -> Matrix #

create a matrix from a list of vectors, as rows

fromColumns :: [Vector] -> Matrix #

create a matrix from a list of vectors, as columns

toVector :: Matrix -> Vector Double #

Convert to a row-major flat vector.

toList :: Matrix -> [Double] #

Convert to a row-major flat list.

toRows :: Matrix -> [Vector] #

Convert to a list of vectors, as rows

toColumns :: Matrix -> [Vector] #

Convert to a list of vectors, as columns

toRowLists :: Matrix -> [[Double]] #

Convert to a list of lists, as rows

# Other

generate #

Arguments

:: Int | Number of rows
-> Int | Number of columns
-> (Int -> Int -> Double) | Function which takes row and column as argument.
-> Matrix |

Generate matrix using function

generateSym #

Arguments

:: Int | Number of rows and columns
-> (Int -> Int -> Double) | Function which takes row and column as argument. It must be symmetric in arguments: `f i j == f j i`
-> Matrix |

Generate symmetric square matrix using function

ident :: Int -> Matrix #

Create the square identity matrix with given dimensions.

diag :: Vector -> Matrix #

Create a square matrix with given diagonal, other entries default to 0

dimension :: Matrix -> (Int, Int) #

Return the dimensions of this matrix, as a (row,column) pair.

center :: Matrix -> Double #

Element in the center of matrix (not corrected for exponent).

multiply :: Matrix -> Matrix -> Matrix #

Matrix-matrix multiplication. Matrices must be of compatible sizes (note: not checked).

multiplyV :: Matrix -> Vector -> Vector #

Matrix-vector multiplication.

transpose :: Matrix -> Matrix #

power :: Matrix -> Int -> Matrix #

Raise matrix to nth power. Power must be positive (/note: not checked).

norm :: Vector -> Double #

Calculate the Euclidean norm of a vector.

column :: Matrix -> Int -> Vector #

Return the given column.

row :: Matrix -> Int -> Vector #

Return the given row.

map :: (Double -> Double) -> Matrix -> Matrix #

Apply function to every element of matrix

for :: Monad m => Int -> Int -> (Int -> m ()) -> m () #

Simple for loop. Counts from start to end-1.

unsafeIndex #

Arguments

:: Matrix |
-> Int | Row.
-> Int | Column.
-> Double |

hasNaN :: Matrix -> Bool #

Indicate whether any element of the matrix is `NaN`.

bounds :: (Vector -> Int -> r) -> Matrix -> Int -> Int -> r #

Given row and column numbers, calculate the offset into the flat row-major vector.

unsafeBounds :: (Vector -> Int -> r) -> Matrix -> Int -> Int -> r #

Given row and column numbers, calculate the offset into the flat row-major vector, without checking.