Safe Haskell | Safe-Inferred |
---|

This module defines classes used to handle Matrices and Vectors in a generic way.

- class Vector v e where
- class Matrix m e where
- generate_m :: Int -> Int -> (Int -> Int -> e) -> m e
- fromList_m :: Int -> Int -> [e] -> m e
- transpose_m :: m e -> m e
- dim_m :: m e -> (Int, Int)
- elem_m :: Int -> Int -> m e -> e
- map_m :: (e -> e) -> m e -> m e
- zipWith_m :: (e -> e -> e) -> m e -> m e -> m e
- subMatrix_m :: Int -> Int -> Int -> Int -> m e -> m e
- fromBlocks_m :: [[m e]] -> m e
- toBlocks_m :: Int -> Int -> m e -> [[m e]]

- class (Vector v e, Matrix m e) => MatrixVector m v e where
- cantRows_m :: Matrix m e => m e -> Int
- cantCols_m :: Matrix m e => m e -> Int
- size_m :: Matrix m e => m e -> Int
- asColumn_vm :: MatrixVector m v e => v e -> m e
- diagonalBlock :: Matrix m e => (Int, Int) -> Int -> m e -> m e
- verticalBlock :: Matrix m e => Int -> Int -> m e -> m e

# Vector

This class represents a vector structure
For any vector data structure used in this framework it must have an instance of this class.
A minimal instance of this class must provide an implementation for `generate_v`

, `elem_v`

and `length_v`

.
The index of the first element stored in the vector must be 0.
This is a multi-param type class because usually libraries of Matrix and Vector imposes restrictions over the type of the elements (for example, to have a Storable instance).

Creates a new vector from the length of the vector and a function that returns the value for each index.

:: [e] | A list that contains the elements of the vector. The first element of the list will be the first element of the vector and so on. |

-> v e |

Creates a new vector from a list, th vector will contain all (and only) the elements of the list, and in the same order.

:: [v e] | A list containing the vectors to be joined. If the list is |

-> v e |

Join multiple vectors in one single vector.

elem_v :: Int -> v e -> eSource

Returns the element from a given index. The index must be between 0 and the length of the vector.

Returns the length of the vector.

foldr_v :: (e -> b -> b) -> b -> v e -> bSource

The common *foldr* function but specific for this instance.

map_v :: (e -> e) -> v e -> v eSource

The common *map* function but specific for this instance.

zipWith_v :: (e -> e -> e) -> v e -> v e -> v eSource

The common *zipWith* function but specific for this instance.

# Matrix

This class represents any Matrix structure
For any matrix data structure used in this framework it must have an instance of this class.
A minimal instance of this class must provide an implementation for `generate_m`

, `dim_m`

and `elem_m`

.
The index of the first element stored in the vector must be (0,0).
This is a multi-param type class because usually libraries of Matrix and Vector imposes restrictions over the type of the elements (for example, to have an Storable instance).

:: Int | The length of the rows |

-> Int | A function that returns the value for each index |

-> (Int -> Int -> e) | |

-> m e |

Creates a new matrix from the dimension (length of rows and columns) of the matrix and a function that returns the value for each index.

fromList_m :: Int -> Int -> [e] -> m eSource

Creates a new matrix from the dimension (length of rows and columns) of and a list.

transpose_m :: m e -> m eSource

Given a matrix, creates a new matrix that will be the transpose of the original.

dim_m :: m e -> (Int, Int)Source

Returns the dimension of a given matrix. The first element in the pair is the number of rows and the second the number of columns.

:: Int | The row component of the position ( |

-> Int | The column component of the position ( |

-> m e | The matrix. |

-> e |

Returns the element that is in the position `(i,j)`

.

map_m :: (e -> e) -> m e -> m eSource

The common *map* function but specific for this instance.

zipWith_m :: (e -> e -> e) -> m e -> m e -> m eSource

The common *zipWith* function but specific for this instance.

Given an initial position `(i,j)`

, a dimension `(r,c)`

and a matrix `m`

, creates a new matrix that is a sub matrix of `m`

containing the elements in positions `[(p1, p2) | p1 <- [i..i+r], p2 <- [j..j+c]]`

For example, if `m`

is:
```
1 2 3
4 5 6
7 8 9
```

and `(i,j)`

and `(r,c)`

are `(0,1)`

and `(2,3)`

respectively, then the result will be a new matrix containing:
```
2 3
5 6
8 9
```

fromBlocks_m :: [[m e]] -> m eSource

Constructs a new matrix from a list of list where the elements are sub-matrices.
This operations is the analogue of `concat_v`

operation from the `Vector`

class.
The 'borders' of the sub-matrices will be joined.
The dimensions of the sum-matrices must be compatibles.

Split a matrix `m`

in blocks of size `(r,c)`

(the dimension of the blocks in the right and the bottom may be smaller).

# MatrixVectror

class (Vector v e, Matrix m e) => MatrixVector m v e whereSource

This class allows us to leave separated the two concepts of `Vector`

and `Matrix`

.
This class establishes a link between a vector and a matrix structure.
We provide a default instance for any `Vector`

and `Matrix`

instances so any vector could be used with any matrix whenever the type of the elements are the same.

row_vm :: Int -> m e -> v eSource

Extracts the `i`

-th row of the matrix `m`

and converts it in the vector structure.

col_vm :: Int -> m e -> v eSource

Extracts the `j`

-th column of the matrix `m`

and converts it in the vector structure.

fromCols_vm :: [v e] -> m eSource

Joins a list of column vectors in a matrix structure. The length of every vector in the list must be the same.

toCols_vm :: m e -> [v e]Source

Returns a list containing every column of the matrix converted in the vector structure.

(Vector v e, Matrix m e) => MatrixVector m v e |

# Miscellaneous

cantRows_m :: Matrix m e => m e -> IntSource

Returns the number of rows of the matrix.

cantCols_m :: Matrix m e => m e -> IntSource

Returns the number of columns of the matrix.

size_m :: Matrix m e => m e -> IntSource

Returns the size of the matrix (number of rows x number of columns).

asColumn_vm :: MatrixVector m v e => v e -> m eSource

Converts a vector in a matrix of one column.

Splits a matrix `m`

into blocks of size `(r,c)`

and returns the block on the position `(i,i)`

.

Splits a matrix `m`

into blocks of size `(s, cantCols_m m)`

and returns the block on the position `i`

.