statistics-0.13.2.3: 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

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
- fromList :: Int -> Int -> [Double] -> Matrix
- fromVector :: Int -> Int -> Vector Double -> Matrix
- toVector :: Matrix -> Vector Double
- toList :: Matrix -> [Double]
- 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

# Documentation

data Matrix Source

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 Source |
Show Matrix Source |

type Vector = Vector Double Source

fromList Source

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.

fromVector Source

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.

toVector :: Matrix -> Vector Double Source

Convert to a row-major flat vector.

toList :: Matrix -> [Double] Source

Convert to a row-major flat list.

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

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

center :: Matrix -> Double Source

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

multiply :: Matrix -> Matrix -> Matrix Source

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

multiplyV :: Matrix -> Vector -> Vector Source

Matrix-vector multiplication.

transpose :: Matrix -> Matrix Source

power :: Matrix -> Int -> Matrix Source

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

norm :: Vector -> Double Source

Calculate the Euclidean norm of a vector.

column :: Matrix -> Int -> Vector Source

Return the given column.

row :: Matrix -> Int -> Vector Source

Return the given row.

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

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

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

unsafeIndex Source

Arguments

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

hasNaN :: Matrix -> Bool Source

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

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

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

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

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