Copyright | (c) ForSyDe Group KTH 2019 |
---|---|
License | BSD-style (see the file LICENSE) |
Maintainer | forsyde-dev@kth.se |
Stability | experimental |
Portability | portable |
Safe Haskell | Safe |
Language | Haskell98 |
This module defines the data type Matrix
and corresponding
functions. It is a shallow interpretation of 2D arrays and is used
for quick prototyping of array algorithms and skeletons, although
itself is a type synonym for a Vector
of Vector
s. Therefore
this module is simply a collection of utility functions on 2D
vectors used mainly for convenience. For a type-checked fixed-size
data type for representing matrices, see the
matrix or
REPA packages.
OBS: The lengths in the API documentation for function arguments are not type-safe, but rather suggestions for usage in designing matrix algorithms or skeletons.
Synopsis
- type Matrix a = Vector (Vector a)
- prettyMat :: Show a => String -> Matrix a -> IO ()
- nullMat :: Matrix a -> Bool
- sizeMat :: Matrix a -> (Int, Int)
- wellFormedMat :: Matrix a -> Matrix a
- matrix :: Int -> Int -> [a] -> Matrix a
- fromMatrix :: Matrix a -> [a]
- unitMat :: a -> Matrix a
- indexMat :: Matrix (Int, Int)
- mapMat :: (a -> b) -> Matrix a -> Matrix b
- zipWithMat :: (a -> b -> c) -> Matrix a -> Matrix b -> Matrix c
- zipWith3Mat :: (a -> b -> c -> d) -> Matrix a -> Matrix b -> Matrix c -> Matrix d
- reduceMat :: (a -> a -> a) -> Matrix a -> a
- dotVecMat :: (a -> a -> a) -> (b -> a -> a) -> Matrix b -> Vector a -> Vector a
- dotMatMat :: (a -> a -> a) -> (b -> a -> a) -> Matrix b -> Matrix a -> Matrix a
- atMat :: Int -> Int -> Matrix a -> a
- takeMat :: Int -> Int -> Matrix a -> Matrix a
- dropMat :: Int -> Int -> Matrix a -> Matrix a
- cropMat :: Int -> Int -> Int -> Int -> Matrix a -> Matrix a
- groupMat :: Int -> Int -> Matrix a -> Matrix (Matrix a)
- stencilMat :: Int -> Int -> Matrix a -> Matrix (Matrix a)
- zipMat :: Matrix a -> Matrix b -> Matrix (a, b)
- unzipMat :: Matrix (a, b) -> (Matrix a, Matrix b)
- rotateMat :: Int -> Int -> Vector (Vector a) -> Vector (Vector a)
- reverseMat :: Matrix a -> Matrix a
- transposeMat :: Matrix a -> Matrix a
- replaceMat :: Int -> Int -> Matrix a -> Matrix a -> Matrix a
Documentation
Prints out to the terminal a matrix in a readable format, where all elements are right-aligned and separated by a custom separator.
>>>
let m = matrix 3 3 [1,2,3,3,100,4,12,32,67]
>>>
prettyMat "|" m
1| 2| 3 3|100| 4 12| 32|67
Queries
sizeMat :: Matrix a -> (Int, Int) Source #
Returns the X and Y dimensions of matrix and checks if it is well formed.
wellFormedMat :: Matrix a -> Matrix a Source #
Checks if a matrix is well-formed, meaning that all its rows are of equal length. Returns the same matrix in case it is well-formed or throws an exception if it is ill-formed.
Generators
:: a | |
-> Matrix a | size = |
Creates a unit (i.e. singleton) matrix, which is a matrix with only one element.
indexMat :: Matrix (Int, Int) Source #
Returns an infinite matrix with (X,Y) index pairs. You need to zip it against another (finite) matrix or to extract a finite subset in order to be useful (see example below).
>>>
prettyMat " " $ takeMat 3 4 indexMat
(0,0) (1,0) (2,0) (0,1) (1,1) (2,1) (0,2) (1,2) (2,2) (0,3) (1,3) (2,3)
Functional skeletons
Maps a function on every value of a matrix.
OBS: this function does not check if the output matrix is well-formed.
:: (a -> b -> c) | |
-> Matrix a | size = |
-> Matrix b | size = |
-> Matrix c | size = |
Applies a binary function pair-wise on each element in two matrices.
OBS: this function does not check if the output matrix is well-formed.
:: (a -> b -> c -> d) | |
-> Matrix a | size = |
-> Matrix b | size = |
-> Matrix c | size = |
-> Matrix d | size = |
Applies a function 3-tuple-wise on each element in three matrices.
OBS: this function does not check if the output matrix is well-formed.
reduceMat :: (a -> a -> a) -> Matrix a -> a Source #
Reduces all the elements of a matrix to one element based on a binary function.
>>>
let m = matrix 3 3 [1,2,3,11,12,13,21,22,23]
>>>
reduceMat (+) m
108
:: (a -> a -> a) | kernel function for a row/column reduction, e.g. |
-> (b -> a -> a) | binary operation for pair-wise elements, e.g. |
-> Matrix b | size = |
-> Vector a | length = |
-> Vector a | length = |
Pattern implementing the template for a dot operation between a vector and a matrix.
>>>
let mA = matrix 4 4 [1,-1,1,1, 1,-1,-1,-1, 1,1,-1,1, 1,1,1,-1]
>>>
let y = vector[1,0,0,0]
>>>
dotVecMat (+) (*) mA y
<1,1,1,1>
:: (a -> a -> a) | kernel function for a row/column reduction, e.g. |
-> (b -> a -> a) | binary operation for pair-wise elements, e.g. |
-> Matrix b | size = |
-> Matrix a | size = |
-> Matrix a | size = |
Pattern implementing the template for a dot operation between two matrices.
>>>
let mA = matrix 4 4 [1,-1,1,1, 1,-1,-1,-1, 1,1,-1,1, 1,1,1,-1]
>>>
prettyMat " " $ dotMatMat (+) (*) mA mA
2 -2 2 2 2 -2 -2 -2 2 2 2 -2 2 2 -2 2
Selectors
Returns the element of a matrix at a certain position.
>>>
let m = matrix 3 3 [1,2,3,11,12,13,21,22,23]
>>>
atMat 2 1 m
13
Returns the upper-left part of a matrix until a specific position.
>>>
let m = matrix 4 4 [1,2,3,4,11,12,13,14,21,22,23,24,31,32,33,34]
>>>
prettyMat " " $ takeMat 2 2 m
1 2 11 12
Returns the upper-left part of a matrix until a specific position.
>>>
let m = matrix 4 4 [1,2,3,4,11,12,13,14,21,22,23,24,31,32,33,34]
>>>
prettyMat " " $ dropMat 2 2 m
23 24 33 34
:: Int | crop width = |
-> Int | crop height = |
-> Int | X start position = |
-> Int | Y start position = |
-> Matrix a | size = |
-> Matrix a | size = |
Crops a section of a matrix.
>>>
let m = matrix 4 4 [1,2,3,4,11,12,13,14,21,22,23,24,31,32,33,34]
>>>
prettyMat " " m
1 2 3 4 11 12 13 14 21 22 23 24 31 32 33 34>>>
prettyMat " " $ cropMat 2 3 1 1 m
12 13 22 23 32 33
Groups a matrix into smaller equallly-shaped matrices.
>>>
let m = matrix 4 4 [1,2,3,4,11,12,13,14,21,22,23,24,31,32,33,34]
>>>
prettyMat " " $ groupMat 2 2 m
<<1,2>,<11,12>> <<3,4>,<13,14>> <<21,22>,<31,32>> <<23,24>,<33,34>>
stencilMat :: Int -> Int -> Matrix a -> Matrix (Matrix a) Source #
Returns a stencil of neighboring elements for each possible element in a vector.
>>>
let m = matrix 4 4 [1,2,3,4,11,12,13,14,21,22,23,24,31,32,33,34]
>>>
prettyMat " " $ stencilMat 2 2 m
<<1,2>,<11,12>> <<2,3>,<12,13>> <<3,4>,<13,14>> <<11,12>,<21,22>> <<12,13>,<22,23>> <<13,14>,<23,24>> <<21,22>,<31,32>> <<22,23>,<32,33>> <<23,24>,<33,34>>
Permutators
Pattern which "rotates" a matrix. The rotation is controled with the x and y index arguments as following:
(> 0)
: rotates the matrix right/down with the corresponding number of positions.(= 0)
: does not modify the position for that axis.(< 0)
: rotates the matrix left/up with the corresponding number of positions.
>>>
let m = matrix 4 4 [1,2,3,4,11,12,13,14,21,22,23,24,31,32,33,34]
>>>
prettyMat " " $ rotateMat (-1) 1 m
32 33 34 31 2 3 4 1 12 13 14 11 22 23 24 21
reverseMat :: Matrix a -> Matrix a Source #
Reverses the order of elements in a matrix
replaceMat :: Int -> Int -> Matrix a -> Matrix a -> Matrix a Source #
Replaces a part of matrix with another (smaller) part, starting from an arbitrary position.
>>>
let m = matrix 4 4 [1,2,3,4,11,12,13,14,21,22,23,24,31,32,33,34]
>>>
let m1 = matrix 2 2 [101,202,303,404]
>>>
prettyMat " " $ replaceMat 1 1 m1 m
1 2 3 4 11 101 202 14 21 303 404 24 31 32 33 34