hmatrix-0.9.3.0: Linear algebra and numerical computationSource codeContentsIndex
Data.Packed.Matrix
Portabilityportable
Stabilityprovisional
MaintainerAlberto Ruiz <aruiz@um.es>
Description
A Matrix representation suitable for numerical computations using LAPACK and GSL.
Synopsis
class (Storable a, Floating a) => Element a
class Element e => Container c e where
toComplex :: RealFloat e => (c e, c e) -> c (Complex e)
fromComplex :: RealFloat e => c (Complex e) -> (c e, c e)
comp :: RealFloat e => c e -> c (Complex e)
conj :: RealFloat e => c (Complex e) -> c (Complex e)
real :: c Double -> c e
complex :: c e -> c (Complex Double)
data Matrix t
rows :: Matrix t -> Int
cols :: Matrix t -> Int
(><) :: Element a => Int -> Int -> [a] -> Matrix a
trans :: Matrix t -> Matrix t
reshape :: Element t => Int -> Vector t -> Matrix t
flatten :: Element t => Matrix t -> Vector t
fromLists :: Element t => [[t]] -> Matrix t
toLists :: Element t => Matrix t -> [[t]]
buildMatrix :: Element a => Int -> Int -> ((Int, Int) -> a) -> Matrix a
(@@>) :: Storable t => Matrix t -> (Int, Int) -> t
asRow :: Element a => Vector a -> Matrix a
asColumn :: Element a => Vector a -> Matrix a
fromRows :: Element t => [Vector t] -> Matrix t
toRows :: Element t => Matrix t -> [Vector t]
fromColumns :: Element t => [Vector t] -> Matrix t
toColumns :: Element t => Matrix t -> [Vector t]
fromBlocks :: Element t => [[Matrix t]] -> Matrix t
toBlocks :: Element t => [Int] -> [Int] -> Matrix t -> [[Matrix t]]
toBlocksEvery :: Element t => Int -> Int -> Matrix t -> [[Matrix t]]
repmat :: Element t => Matrix t -> Int -> Int -> Matrix t
flipud :: Element t => Matrix t -> Matrix t
fliprl :: Element t => Matrix t -> Matrix t
subMatrix :: Element a => (Int, Int) -> (Int, Int) -> Matrix a -> Matrix a
takeRows :: Element t => Int -> Matrix t -> Matrix t
dropRows :: Element t => Int -> Matrix t -> Matrix t
takeColumns :: Element t => Int -> Matrix t -> Matrix t
dropColumns :: Element t => Int -> Matrix t -> Matrix t
extractRows :: Element t => [Int] -> Matrix t -> Matrix t
ident :: Element a => Int -> Matrix a
diag :: Element a => Vector a -> Matrix a
diagRect :: (Element t, Num t) => Vector t -> Int -> Int -> Matrix t
takeDiag :: Element t => Matrix t -> Vector t
liftMatrix :: (Element a, Element b) => (Vector a -> Vector b) -> Matrix a -> Matrix b
liftMatrix2 :: (Element t, Element a, Element b) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t
liftMatrix2Auto :: (Element t, Element a, Element b) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t
dispf :: Int -> Matrix Double -> String
disps :: Int -> Matrix Double -> String
dispcf :: Int -> Matrix (Complex Double) -> String
latexFormat :: String -> String -> String
format :: Element t => String -> (t -> String) -> Matrix t -> String
loadMatrix :: FilePath -> IO (Matrix Double)
saveMatrix :: FilePath -> String -> Matrix Double -> IO ()
fromFile :: FilePath -> (Int, Int) -> IO (Matrix Double)
fileDimensions :: FilePath -> IO (Int, Int)
readMatrix :: String -> Matrix Double
fromArray2D :: Element e => Array (Int, Int) e -> Matrix e
Documentation
class (Storable a, Floating a) => Element a Source
Auxiliary class.
show/hide Instances
class Element e => Container c e whereSource
conversion utilities
Methods
toComplex :: RealFloat e => (c e, c e) -> c (Complex e)Source
fromComplex :: RealFloat e => c (Complex e) -> (c e, c e)Source
comp :: RealFloat e => c e -> c (Complex e)Source
conj :: RealFloat e => c (Complex e) -> c (Complex e)Source
real :: c Double -> c eSource
complex :: c e -> c (Complex Double)Source
show/hide Instances
data Matrix t Source
Matrix representation suitable for GSL and LAPACK computations.
show/hide Instances
rows :: Matrix t -> IntSource
cols :: Matrix t -> IntSource
(><) :: 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.

The input list is explicitly truncated, so that it can safely be used with lists that are too long (like infinite lists).

Example:

> (2>|<3)[1..]
(2><3)
 [ 1.0, 2.0, 3.0
 , 4.0, 5.0, 6.0 ]
trans :: Matrix t -> Matrix tSource
Matrix transpose.
reshape :: Element t => Int -> Vector t -> Matrix tSource

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
buildMatrix :: Element a => Int -> Int -> ((Int, Int) -> a) -> Matrix aSource

creates a Matrix of the specified size using the supplied function to to map the rowcolumn position to the value at that rowcolumn position.

> buildMatrix 3 4 ( (r,c) -> fromIntegral r * fromIntegral c)
(3><4)
 [ 0.0, 0.0, 0.0, 0.0, 0.0
 , 0.0, 1.0, 2.0, 3.0, 4.0
 , 0.0, 2.0, 4.0, 6.0, 8.0]
(@@>) :: 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
Create a matrix from a list of vectors. All vectors must have the same dimension, or dimension 1, which is are automatically expanded.
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.

Single row/column components are automatically expanded to match the corresponding common row and column:

> let disp = putStr . dispf 2
> let vector xs = fromList xs :: Vector Double
> let diagl = diag . vector
> let rowm = asRow . vector

> disp $ fromBlocks [[ident 5, 7, rowm[10,20]], [3, diagl[1,2,3], 0]]

8x10
1  0  0  0  0  7  7  7  10  20
0  1  0  0  0  7  7  7  10  20
0  0  1  0  0  7  7  7  10  20
0  0  0  1  0  7  7  7  10  20
0  0  0  0  1  7  7  7  10  20
3  3  3  3  3  1  0  0   0   0
3  3  3  3  3  0  2  0   0   0
3  3  3  3  3  0  0  3   0   0
toBlocks :: Element t => [Int] -> [Int] -> Matrix t -> [[Matrix t]]Source
Partition a matrix into blocks with the given numbers of rows and columns. The remaining rows and columns are discarded.
toBlocksEvery :: Element t => Int -> Int -> Matrix t -> [[Matrix t]]Source
Fully partition a matrix into blocks of the same size. If the dimensions are not a multiple of the given size the last blocks will be smaller.
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
:: Element a
=> (Int, Int)(r0,c0) starting position
-> (Int, Int)(rt,ct) dimensions of submatrix
-> Matrix ainput matrix
-> Matrix aresult
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
liftMatrix2Auto :: (Element t, Element a, Element b) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix tSource
A version of liftMatrix2 which automatically adapt matrices with a single row or column to match the dimensions of the other matrix.
dispf :: Int -> Matrix Double -> StringSource

Show a matrix with a given number of decimal places.

disp = putStr . dispf 3

> disp (1/3 + ident 4)
4x4
1.333  0.333  0.333  0.333
0.333  1.333  0.333  0.333
0.333  0.333  1.333  0.333
0.333  0.333  0.333  1.333
disps :: Int -> Matrix Double -> StringSource

Show a matrix with "autoscaling" and a given number of decimal places.

disp = putStr . disps 2

> disp $ 120 * (3><4) [1..]
3x4  E3
 0.12  0.24  0.36  0.48
 0.60  0.72  0.84  0.96
 1.08  1.20  1.32  1.44
dispcf :: Int -> Matrix (Complex Double) -> StringSource
Pretty print a complex matrix with at most n decimal digits.
latexFormatSource
:: Stringtype of braces: "matrix", "bmatrix", "pmatrix", etc.
-> StringFormatted matrix, with elements separated by spaces and newlines
-> String
Tool to display matrices with latex syntax.
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 = putStr . format "  " (printf "%.2f")
loadMatrix :: FilePath -> IO (Matrix Double)Source
Loads a matrix from an ASCII file formatted as a 2D table.
saveMatrixSource
:: FilePath
-> Stringformat (%f, %g, %e)
-> Matrix Double
-> IO ()
Saves a matrix as 2D ASCII table.
fromFile :: FilePath -> (Int, Int) -> IO (Matrix Double)Source
Loads a matrix from an ASCII file (the number of rows and columns must be known in advance).
fileDimensions :: FilePath -> IO (Int, Int)Source
obtains the number of rows and columns in an ASCII data file (provisionally using unix's wc).
readMatrix :: String -> Matrix DoubleSource
reads a matrix from a string containing a table of numbers.
fromArray2D :: Element e => Array (Int, Int) e -> Matrix eSource
Produced by Haddock version 2.6.1