hmatrix-0.5.1.1: Linear algebra and numerical computations

Portabilityportable
Stabilityprovisional
MaintainerAlberto Ruiz <aruiz@um.es>

Data.Packed.Matrix

Description

A Matrix representation suitable for numerical computations using LAPACK and GSL.

Synopsis

Documentation

class (Storable a, Floating a) => Element a Source

Auxiliary class.

data Matrix t Source

Matrix representation suitable for GSL and LAPACK computations.

(><) :: Element a => Int -> Int -> [a] -> Matrix aSource

An easy way to create a matrix:

> (2><3)[1..6]
(2><3)
 [ 1.0, 2.0, 3.0
 , 4.0, 5.0, 6.0 ]

This is the format produced by the instances of Show (Matrix a), which can also be used for input.

trans :: Matrix t -> Matrix tSource

Matrix transpose.

reshape :: Element t => Int -> Vector t -> Matrix tSource

Unsafe matrix access without range checking

Creates a matrix from a vector by grouping the elements in rows with the desired number of columns. (GNU-Octave groups by columns. To do it you can define reshapeF r = trans . reshape r where r is the desired number of rows.)

> reshape 4 (fromList [1..12])
(3><4)
 [ 1.0,  2.0,  3.0,  4.0
 , 5.0,  6.0,  7.0,  8.0
 , 9.0, 10.0, 11.0, 12.0 ]

flatten :: Element t => Matrix t -> Vector tSource

Creates a vector by concatenation of rows

> flatten (ident 3)
9 |> [1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0]

fromLists :: Element t => [[t]] -> Matrix tSource

Creates a Matrix from a list of lists (considered as rows).

> fromLists [[1,2],[3,4],[5,6]]
(3><2)
 [ 1.0, 2.0
 , 3.0, 4.0
 , 5.0, 6.0 ]

toLists :: Element t => Matrix t -> [[t]]Source

the inverse of Data.Packed.Matrix.fromLists

(@@>) :: Storable t => Matrix t -> (Int, Int) -> tSource

Reads a matrix position.

asRow :: Element a => Vector a -> Matrix aSource

creates a 1-row matrix from a vector

asColumn :: Element a => Vector a -> Matrix aSource

creates a 1-column matrix from a vector

fromRows :: Element t => [Vector t] -> Matrix tSource

creates a Matrix from a list of vectors

toRows :: Element t => Matrix t -> [Vector t]Source

extracts the rows of a matrix as a list of vectors

fromColumns :: Element t => [Vector t] -> Matrix tSource

Creates a matrix from a list of vectors, as columns

toColumns :: Element t => Matrix t -> [Vector t]Source

Creates a list of vectors from the columns of a matrix

fromBlocks :: Element t => [[Matrix t]] -> Matrix tSource

Creates a matrix from blocks given as a list of lists of matrices:

> let a = diag $ fromList [5,7,2]
> let b = reshape 4 $ constant (-1) 12
> fromBlocks [[a,b],[b,a]]
(6><7)
 [  5.0,  0.0,  0.0, -1.0, -1.0, -1.0, -1.0
 ,  0.0,  7.0,  0.0, -1.0, -1.0, -1.0, -1.0
 ,  0.0,  0.0,  2.0, -1.0, -1.0, -1.0, -1.0
 , -1.0, -1.0, -1.0, -1.0,  5.0,  0.0,  0.0
 , -1.0, -1.0, -1.0, -1.0,  0.0,  7.0,  0.0
 , -1.0, -1.0, -1.0, -1.0,  0.0,  0.0,  2.0 ]

repmat :: Element t => Matrix t -> Int -> Int -> Matrix tSource

creates matrix by repetition of a matrix a given number of rows and columns

> repmat (ident 2) 2 3 :: Matrix Double
(4><6)
 [ 1.0, 0.0, 1.0, 0.0, 1.0, 0.0
 , 0.0, 1.0, 0.0, 1.0, 0.0, 1.0
 , 1.0, 0.0, 1.0, 0.0, 1.0, 0.0
 , 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 ]

flipud :: Element t => Matrix t -> Matrix tSource

Reverse rows

fliprl :: Element t => Matrix t -> Matrix tSource

Reverse columns

subMatrixSource

Arguments

:: Element a 
=> (Int, Int)

(r0,c0) starting position

-> (Int, Int)

(rt,ct) dimensions of submatrix

-> Matrix a

input matrix

-> Matrix a

result

Extracts a submatrix from a matrix.

takeRows :: Element t => Int -> Matrix t -> Matrix tSource

Creates a matrix with the first n rows of another matrix

dropRows :: Element t => Int -> Matrix t -> Matrix tSource

Creates a copy of a matrix without the first n rows

takeColumns :: Element t => Int -> Matrix t -> Matrix tSource

Creates a matrix with the first n columns of another matrix

dropColumns :: Element t => Int -> Matrix t -> Matrix tSource

Creates a copy of a matrix without the first n columns

extractRows :: Element t => [Int] -> Matrix t -> Matrix tSource

rearranges the rows of a matrix according to the order given in a list of integers.

ident :: Element a => Int -> Matrix aSource

creates the identity matrix of given dimension

diag :: Element a => Vector a -> Matrix aSource

Creates a square matrix with a given diagonal.

diagRect :: (Element t, Num t) => Vector t -> Int -> Int -> Matrix tSource

creates a rectangular diagonal matrix

> diagRect (constant 5 3) 3 4 :: Matrix Double
(3><4)
 [ 5.0, 0.0, 0.0, 0.0
 , 0.0, 5.0, 0.0, 0.0
 , 0.0, 0.0, 5.0, 0.0 ]

takeDiag :: Element t => Matrix t -> Vector tSource

extracts the diagonal from a rectangular matrix

liftMatrix :: (Element a, Element b) => (Vector a -> Vector b) -> Matrix a -> Matrix bSource

application of a vector function on the flattened matrix elements

liftMatrix2 :: (Element t, Element a, Element b) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix tSource

application of a vector function on the flattened matrices elements

format :: Element t => String -> (t -> String) -> Matrix t -> StringSource

Creates a string from a matrix given a separator and a function to show each entry. Using this function the user can easily define any desired display function:

import Text.Printf(printf)
disp = putStrLn . format "  " (printf "%.2f")

readMatrix :: String -> Matrix DoubleSource

creates a matrix from a table of numbers.

fromFile :: FilePath -> (Int, Int) -> IO (Matrix Double)Source

loads a matrix efficiently from formatted ASCII text file (the number of rows and columns must be known in advance).