struct Matrix(T, M, N)

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

Defined in:

matrix.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(&block : Int32 -> T) #

Creates Matrix, yielding the linear index for each element to provide an initial value.


[View source]

Class Method Detail

def self.build(&initialiser : UInt32, UInt32 -> T) #

Creates a Matrix, invoking initialiser with each pair of indices.


[View source]
def self.from(list : StaticArray(T, A)) #

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 |


[View source]
def self.identity(id = T.zero + 1) #

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.


[View source]
def self.of(value : T) #

Creates a Matrix with each element initialized as value.


[View source]
def self.zero #

Build a zero matrix (all elements populated with zero value of the type isntance).


[View source]

Instance Method Detail

def *(other : Matrix(_, A, B)) #

Performs a matrix multiplication with other.


[View source]
def *(other) #

Performs a scalar multiplication with other.


[View source]
def +(other : Matrix) #

Returns a new Matrix that is the result of performing a matrix addition with other


[View source]
def -(other : Matrix) #

Returns a new Matrix that is the result of performing a matrix subtraction with other


[View source]
def ==(other : Matrix(U, A, B)) forall U #

Equality. Returns true if each element in self is equal to each corresponding element in other.


[View source]
def ==(other) #

Equality with another object, or differently sized matrix. Always false.


[View source]
def [](i : Int, j : Int) : T #

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).


[View source]
def []=(i : Int, j : Int, value : T) #

Sets the value of the element at i,j.


[View source]
def col(j : Int) #

Gets the cotents of column j.


[View source]
def cols #

Count of columns.


[View source]
def dimensions #

Returns the dimensions of self as a tuple of {rows, cols}.


[View source]
def map(&block : T -> U) forall U #

Apply a morphism to all elements, returning a new Matrix with the result.


[View source]
def map!(&block : T -> T) #

Apply an endomorphism to self, mutating all elements in place.


[View source]
def map_with_index(&block : T, Int32 -> U) forall U #

ditto


[View source]
def map_with_indices(&block : T, UInt32, UInt32 -> U) forall U #

ditto


[View source]
def merge(other : Matrix(U, A, B), &block : T, U -> _) forall U #

Merge with another similarly dimensions matrix, apply the passed block to each elemenet pair.


[View source]
def row(i : Int) #

Gets the contents of row i.


[View source]
def rows #

Count of rows.


[View source]
def size #

Gets the capacity (total number of elements) of self.


[View source]
def transpose #

Creates a new matrix that is the result of inverting the rows and columns of self.


[View source]
def update(i, j, &block : T -> T) #

Yields the current element at i,j and updates the value with the block's return value.


[View source]