forsyde-atom-0.3.0.0: Shallow-embedded DSL for modeling cyber-physical systems
Copyright(c) George Ungureanu KTH/EECS/ESY 2019-2020
LicenseBSD-style (see the file LICENSE)
Maintainerugeorge@kth.se
Stabilityexperimental
Portabilityportable
Safe HaskellSafe
LanguageHaskell2010

ForSyDe.Atom.Skel.Vector.Matrix

Description

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

Documentation

type Matrix a = Vector (Vector a) Source #

Matrix is a type synonym for vector of vectors. This means that any function on Vector works also on Matrix.

pretty Source #

Arguments

:: Show a 
=> String

separator string

-> Matrix a

input matrix

-> IO () 

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

isNull :: Matrix a -> Bool Source #

Checks if a matrix is null. <> and <> are both null matrices.

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.

matrix Source #

Arguments

:: Int

number of columns (X dimension) = x

-> Int

number of rows (Y dimension) = y

-> [a]

list of values; length = x * y

-> Matrix a

Matrix of values; size = (x,y)

Converts a list into a Matrix. See example from pretty.

fromMatrix Source #

Arguments

:: Matrix a

size = (x,y)

-> [a]

length = x * y

Converts a matrix back to a list.

unit Source #

Arguments

:: a 
-> Matrix a

size = (1,1)

Creates a unit (i.e. singleton) matrix, which is a matrix with only one element.

fanout :: a -> Matrix a Source #

Creates an infinite matrix which repeats 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)

farm11 Source #

Arguments

:: (a -> b) 
-> Matrix a

size = (xa,ya)

-> Matrix b

size = (xa,ya)

Maps a function on every value of a matrix.

OBS: this function does not check if the output matrix is well-formed.

farm21 Source #

Arguments

:: (a -> b -> c) 
-> Matrix a

size = (xa,ya)

-> Matrix b

size = (xb,yb)

-> Matrix c

size = (minimum [xa,xb], minimum [ya,yb])

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.

farm31 Source #

Arguments

:: (a -> b -> c -> d) 
-> Matrix a

size = (xa,ya)

-> Matrix b

size = (xb,yb)

-> Matrix c

size = (xc,yc)

-> Matrix d

size = (minimum [xa,xb,xc], minimum [ya,yb,yc])

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

dotV Source #

Arguments

:: (a -> a -> a)

kernel function for a row/column reduction, e.g. (+) for dot product

-> (b -> a -> a)

binary operation for pair-wise elements, e.g. (*) for dot product

-> Matrix b

size = (xa,ya)

-> Vector a

length = xa

-> Vector a

length = xa

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>

dot Source #

Arguments

:: (a -> a -> a)

kernel function for a row/column reduction, e.g. (+) for dot product

-> (b -> a -> a)

binary operation for pair-wise elements, e.g. (*) for dot product

-> Matrix b

size = (xa,ya)

-> Matrix a

size = (ya,xa)

-> Matrix a

size = (xa,xa)

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

get Source #

Arguments

:: Int

X index starting from zero

-> Int

Y index starting from zero

-> Matrix a 
-> Maybe a 

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

take Source #

Arguments

:: Int

X index starting from zero

-> Int

Y index starting from zero

-> Matrix a 
-> Matrix a 

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

drop Source #

Arguments

:: Int

X index starting from zero

-> Int

Y index starting from zero

-> Matrix a 
-> Matrix a 

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

crop Source #

Arguments

:: Int

crop width = w

-> Int

crop height = h

-> Int

X start position = x0

-> Int

Y start position = y0

-> Matrix a

size = (xa,ya)

-> Matrix a

size = (minimum [w,xa-x0], minimum [h,xa-x0])

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

group Source #

Arguments

:: Int

width of groups = w

-> Int

height of groups = h

-> Matrix a

size = (xa,ya)

-> Matrix (Matrix a)

size = (xa div w,ya div h)

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

rotate Source #

Arguments

:: Int

index on X axis

-> Int

index on Y axis

-> Matrix a 
-> Matrix a 

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

transpose Source #

Arguments

:: Matrix a

X:Y orientation

-> Matrix a

Y:X orientation

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