struct Matrix(T, M, N)
- Matrix(T, M, N)
- Struct
- Value
- Object
Overview
Generic, type-safe abstract matrix structure.
This structure provides an M x N rectangular array of any field) T. That is, T must define operations for addition, subtraction, multiplication and division.
Where possible, all matrix operations provide validation at the type level.
Included Modules
- Indexable(T)
Defined in:
matrix.crConstructors
-
.new(&block : Int32 -> T)
Creates Matrix, yielding the linear index for each element to provide an initial value.
Class Method Summary
-
.build(&initialiser : UInt32, UInt32 -> T)
Creates a Matrix, invoking initialiser with each pair of indices.
-
.from(list : StaticArray(T, A))
Creates a Matrix from elements contained within a StaticArray.
-
.identity(id = T.zero + 1)
Build the idenity matrix for the instanced type and dimensions.
-
.of(value : T)
Creates a Matrix with each element initialized as value.
-
.zero
Build a zero matrix (all elements populated with zero value of the type isntance).
Instance Method Summary
-
#*(other : Matrix(UNDERSCORE, A, B))
Performs a matrix multiplication with other.
-
#*(other)
Performs a scalar multiplication with other.
-
#+(other : Matrix)
Returns a new Matrix that is the result of performing a matrix addition with other
-
#-(other : Matrix)
Returns a new Matrix that is the result of performing a matrix subtraction with other
-
#==(other : Matrix(U, A, B)) forall U
Equality.
-
#==(other)
Equality with another object, or differently sized matrix.
-
#[](i : Int, j : Int) : T
Retrieves the value of the element at i,j.
-
#[]=(i : Int, j : Int, value : T)
Sets the value of the element at i,j.
-
#col(j : Int)
Gets the cotents of column j.
-
#cols
Count of columns.
-
#dimensions
Returns the dimensions of
self
as a tuple of{rows, cols}
. -
#map(&block : T -> U) forall U
Apply a morphism to all elements, returning a new Matrix with the result.
-
#map!(&block : T -> T)
Apply an endomorphism to
self
, mutating all elements in place. -
#map_with_index(&block : T, Int32 -> U) forall U
Apply a morphism to all elements, returning a new Matrix with the result.
-
#map_with_indices(&block : T, UInt32, UInt32 -> U) forall U
Apply a morphism to all elements, returning a new Matrix with the result.
-
#merge(other : Matrix(U, A, B), &block : T, U -> UNDERSCORE) forall U
Merge with another similarly dimensions matrix, apply the passed block to each elemenet pair.
-
#row(i : Int)
Gets the contents of row i.
-
#rows
Count of rows.
-
#size
Gets the capacity (total number of elements) of
self
. -
#transpose
Creates a new matrix that is the result of inverting the rows and columns of
self
. -
#update(i, j, &block : T -> T)
Yields the current element at i,j and updates the value with the block's return value.
Constructor Detail
Creates Matrix, yielding the linear index for each element to provide an initial value.
Class Method Detail
Creates a Matrix, invoking initialiser with each pair of indices.
Creates a Matrix from elements contained within a StaticArray.
The matrix will be filled rows first, such that an array of
[1, 2, 3, 4]
becomes
| 1 2 | | 3 4 |
Build the idenity matrix for the instanced type and dimensions.
id
may be used to specify an identity element for the type. If unspecifed
a numeric identity will be assumed.
Instance Method Detail
Performs a matrix multiplication with other.
Returns a new Matrix that is the result of performing a matrix addition with other
Returns a new Matrix that is the result of performing a matrix subtraction with other
Equality. Returns true
if each element in self
is equal to each
corresponding element in other.
Retrieves the value of the element at i,j.
Indicies are zero-based. Negative values may be passed for i and j to
enable reverse indexing such that self[-1, -1] == self[M - 1, N - 1]
(same behaviour as arrays).
Apply a morphism to all elements, returning a new Matrix with the result.
Apply a morphism to all elements, returning a new Matrix with the result.
Apply a morphism to all elements, returning a new Matrix with the result.
Merge with another similarly dimensions matrix, apply the passed block to each elemenet pair.
Yields the current element at i,j and updates the value with the block's return value.