Copyright | (c) George Ungureanu KTH/EECS/ESY 2019-2020 |
---|---|
License | BSD-style (see the file LICENSE) |
Maintainer | ugeorge@kth.se |
Stability | experimental |
Portability | portable |
Safe Haskell | Safe |
Language | Haskell2010 |
This module exports an alias Matrix
and a couple of patterns and utilities to
work with matrices constructed as 2D vectors. Since names might overlap, this
library is recommended to be imported qualified.
Synopsis
- type Matrix a = Vector (Vector a)
- pretty :: Show a => String -> Matrix a -> IO ()
- isNull :: Matrix a -> Bool
- size :: Matrix a -> (Int, Int)
- wellFormed :: Matrix a -> Matrix a
- matrix :: Int -> Int -> [a] -> Matrix a
- fromMatrix :: Matrix a -> [a]
- unit :: a -> Matrix a
- fanout :: a -> Matrix a
- indexes :: Matrix (Int, Int)
- farm11 :: (a -> b) -> Matrix a -> Matrix b
- farm21 :: (a -> b -> c) -> Matrix a -> Matrix b -> Matrix c
- farm31 :: (a -> b -> c -> d) -> Matrix a -> Matrix b -> Matrix c -> Matrix d
- reduce :: (a -> a -> a) -> Matrix a -> a
- dotV :: (a -> a -> a) -> (b -> a -> a) -> Matrix b -> Vector a -> Vector a
- dot :: (a -> a -> a) -> (b -> a -> a) -> Matrix b -> Matrix a -> Matrix a
- get :: Int -> Int -> Matrix a -> Maybe a
- take :: Int -> Int -> Matrix a -> Matrix a
- drop :: Int -> Int -> Matrix a -> Matrix a
- crop :: Int -> Int -> Int -> Int -> Matrix a -> Matrix a
- group :: Int -> Int -> Matrix a -> Matrix (Matrix a)
- stencil :: Int -> Int -> Matrix a -> Matrix (Matrix a)
- reverse :: Matrix a -> Matrix a
- rotate :: Int -> Int -> Matrix a -> Matrix a
- transpose :: Matrix a -> Matrix a
- replace :: 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]
>>>
pretty "|" m
1| 2| 3 3|100| 4 12| 32|67
size :: Matrix a -> (Int, Int) Source #
Returns the X and Y dimensions of matrix and checks if it is well formed.
wellFormed :: 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.
:: a | |
-> Matrix a | size = |
Creates a unit (i.e. singleton) matrix, which is a matrix with only one element.
indexes :: 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).
>>>
pretty " " $ take 3 4 indexes
(0,0) (1,0) (2,0) (0,1) (1,1) (2,1) (0,2) (1,2) (2,2) (0,3) (1,3) (2,3)
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.
reduce :: (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]
>>>
reduce (+) 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]
>>>
dotV (+) (*) 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]
>>>
pretty " " $ dot (+) (*) mA mA
2 -2 2 2 2 -2 -2 -2 2 2 2 -2 2 2 -2 2
Returns the element of a matrix at a certain position.
>>>
let m = matrix 3 3 [1,2,3,11,12,13,21,22,23]
>>>
at 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]
>>>
pretty " " $ take 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]
>>>
pretty " " $ drop 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]
>>>
pretty " " m
1 2 3 4 11 12 13 14 21 22 23 24 31 32 33 34>>>
pretty " " $ 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]
>>>
pretty " " $ group 2 2 m
<<1,2>,<11,12>> <<3,4>,<13,14>> <<21,22>,<31,32>> <<23,24>,<33,34>>
stencil :: 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]
>>>
pretty " " $ stencil 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>>
reverse :: Matrix a -> Matrix a Source #
Reverses the order of elements in a matrix
>>>
let m = matrix 4 4 [1,2,3,4,11,12,13,14,21,22,23,24,31,32,33,34]
>>>
pretty " " $ reverse m
34 33 32 31 24 23 22 21 14 13 12 11 4 3 2 1
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]
>>>
pretty " " $ rotate (-1) 1 m
32 33 34 31 2 3 4 1 12 13 14 11 22 23 24 21
Transposes a matrix
>>>
let m = matrix 4 4 [1,2,3,4,11,12,13,14,21,22,23,24,31,32,33,34]
>>>
pretty " " $ transpose m
1 11 21 31 2 12 22 32 3 13 23 33 4 14 24 34
replace :: 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]
>>>
pretty " " $ replace 1 1 m1 m
1 2 3 4 11 101 202 14 21 303 404 24 31 32 33 34