struct Matrix(T)

Included Modules

Defined in:

matrix.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(rows : Int, columns : Int, value : T) #

[View source]
def self.new(rows : Int, columns : Int) #

[View source]
def self.new(rows : Int, columns : Int, &block : Int32, Int32, Int32 -> T) #

Creates a matrix with the given number of rows and columns. It yields the linear, row and column indexes in that order.


[View source]

Class Method Detail

def self.[](*rows) #

Alias for Matrix.rows.


[View source]
def self.column(*values) #

Creates a single column matrix with the given values.


[View source]
def self.columns(columns : Array(Array) | Tuple) #

Creates a matrix interpreting each argument as a column.


[View source]
def self.diagonal(*values) #

Creates a diagonal matrix with the supplied arguments. Best suited to numeric matrices.


[View source]
def self.identity(row_or_col_size : Int) #

Creates a Matrix(Int32), whose diagonal values are 1 and the rest are 0.


[View source]
def self.row(*values) #

Creates a single row matrix with the given values.


[View source]
def self.rows(rows : Array(Array) | Tuple) #

Creates a matrix interpreting each argument as a row.


[View source]

Instance Method Detail

def &(other : T | Number) #

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

Performs multiplication with another matrix.


[View source]
def *(other : T | Number) #

[View source]
def **(other : Int) #

Performs exponentiation


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

Performs addition with another matrix.


[View source]
def +(other : T | Number) #

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

Performs subtraction with another matrix.


[View source]
def -(other : T | Number) #

[View source]
def - #

Returns a new matrix of the same size after calling the unary #- method on every element. Best suited to numeric matrices.


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

Performs division with another matrix.


[View source]
def /(other : T | Number) #

[View source]
def <<(other : T | Number) #

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

Checks equality between self and another matrix.


[View source]
def >>(other : T | Number) #

[View source]
def [](row : Int, column : Int) #

Retrieves the element at the given row and column indexes. Raises IndexError.


[View source]
def [](index : Int) #

Retrieves the element at the given linear index. Raises IndexError.


[View source]
def []=(row : Int, column : Int, value : T) #

Replaces the element at the given row and column with the given value.


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

Replaces the element at the given linear index with the given value.


[View source]
def []?(row : Int, column : Int) #

Retrieves the element at the given row and column indexes. Returns nil if out of bounds.


[View source]
def []?(index : Int) #

Retrieves the element at the given linear index. Returns nil if out of bounds


[View source]
def ^(other : T | Number) #

[View source]
def |(other : T | Number) #

[View source]
def at(row : Int, column : Int, &) #

Retrieves the element at the given row and column indexes. Yields if out of bounds.


[View source]
def at(row : Int, column : Int) #

Retrieves the element at the given row and column indexes. Raises IndexError.


[View source]
def at(index : Int, &) #

Retrieves the element at the given linear index. Yields if out of bounds.


[View source]
def at(index : Int) #

Retrieves the element at the given linear index. Raises if out of bounds.


[View source]
def clone #

Creates an identical matrix.


[View source]
def column(column_index : Int) #

Returns an iterator for the elements of the column at the given index.


[View source]
def column(index : Int, &) #

Yields elements of the column at the given index.


[View source]
def column_count #

The number of columns.


[View source]
def column_vectors #

Returns an array of smaller matrices, each representing a column from self.


[View source]
def columns #

Returns an array of arrays that correspond to the columns of the matrix.


[View source]
def cycle(which : Symbol = :all) #
Description copied from module Iterable(T)

Same as each.cycle.

See also: Iterator#cycle.


[View source]
def determinant #

Returns the determinant of the matrix. Only useful for numeric matrices.


[View source]
def diagonal? #

Returns true if all non-diagonal elements are 0.


[View source]
def dimensions #

[View source]
def each(which : Symbol, &) #

Like #each but with a Symbol directive that causes the method to skip certain indices: :all -> equivalent to a simple #each (yields every element) :diagonal -> yields elements in the diagonal :off_diagonal -> yields elements not in the diagonal :lower -> yields elements at or below the diagonal :strict_lower -> yields elements below the diagonal :upper -> yields elements at or above the diagonal :strict_upper -> yields elements above the diagonal


[View source]
def each(&) #

Yields every element of the matrix linearly: First the elements of the first row, then the elements of the second row, etc.


[View source]
def each(which : Symbol = :all) #
Description copied from module Iterable(T)

Must return an Iterator over the elements in this collection.


[View source]
def each_index(which : Symbol = :all, &) #

Yields every row and column index. See #each for the optional directives.


[View source]
def each_index(which : Symbol = :all) #

[View source]
def each_with_index(which : Symbol = :all, &) #

Yields every element along with its row and column index. See #each for the optional directives.


[View source]
def empty? #

Returns true if the matrix has either 0 rows or 0 columns.


[View source]
def hash #
Description copied from class Object

Generates an UInt64 hash value for this object.

This method must have the property that a == b implies a.hash == b.hash.

The hash value is used along with #== by the Hash class to determine if two objects reference the same hash key.

Subclasses must not override this method. Instead, they must define hash(hasher), though usually the macro def_hash can be used to generate this method.


[View source]
def index(value : T, which : Symbol = :all) #

Returns the row and column index of the first occurrence of "value" in the matrix, nil otherwise.


[View source]
def index(&block : T, Int32, Int32, Int32 -> Bool) #

Returns the row and column index of the first occurrence of the block returning true, nil otherwise


[View source]
def inspect(io : IO) #
Description copied from struct Struct

Appends this struct's name and instance variables names and values to the given IO.

struct Point
  def initialize(@x : Int32, @y : Int32)
  end
end

p1 = Point.new 1, 2
p1.to_s    # "Point(@x=1, @y=2)"
p1.inspect # "Point(@x=1, @y=2)"

[View source]
def inverse #

Returns the inverse of the matrix. Only useful for numeric matrices.


[View source]
def lower_triangular? #

Returns true if the matrix is a lower triangular matrix.


[View source]
def map(&) #

Returns a new matrix with the return values of the block. Yields the element and its linear, row and column indices in that order.


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

Changes the values of the matrix according to the return values of the block. Yields the element and its linear, row and column indices in that order.


[View source]
def minor(start_row : Int, rows : Int, start_col : Int, columns : Int) #

Returns a subsection of the matrix.


[View source]
def minor(row_range : Range(Int, Int), col_range : Range(Int, Int)) #

Returns a subsection of the matrix.


[View source]
def permutation? #

Returns true if the matrix is a permutation matrix.


[View source]
def rank #

Returns the rank of the matrix. Only useful for numeric matrices.


[View source]
def regular? #

Returns true if the matrix is regular.


[View source]
def reverse #

[View source]
def reverse! #

Reverses the order of the matrix.


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

Returns an iterator for the elements of the row at the given index.


[View source]
def row(index : Int, &) #

Yields elements of the row at the given index.


[View source]
def row_count #

The number of rows.


[View source]
def row_vectors #

Returns an array of smaller matrices, each representing a row from self.


[View source]
def rows #

Returns an array of arrays that correspond to the rows of the matrix.


[View source]
def shuffle #

[View source]
def shuffle! #

Shuffles the elements of the matrix.


[View source]
def shuffle_columns #

[View source]
def shuffle_columns! #

Shuffles the elements of each column.


[View source]
def shuffle_rows #

[View source]
def shuffle_rows! #

Shuffles the elements of each row.


[View source]
def singular? #

Returns true if the matrix is singular.


[View source]
def size #

The total number of elements.


[View source]
def square? #

Returns true if the number of rows equals the number of columns.


[View source]
def swap_columns(col_1 : Int, col_2 : Int) #

Swaps two columns.


[View source]
def swap_rows(row_1 : Int, row_2 : Int) #

Swaps two rows.


[View source]
def symmetric? #

Returns true if the matrix is symmetric.


[View source]
def to_a #

Returns an array of every element in the matrix.


[View source]
def to_h #

Returns a hash: {row_index, column_index} => element


[View source]
def to_s(io : IO) #

Displays the matrix in a more readable form.


[View source]
def trace #

Returns the sum of the diagonal elements. Only useful for numeric matrices.


[View source]
def transpose #

Changes the rows into columns and vice versa.


[View source]
def upper_triangular? #

Returns true if the matrix is an upper triangular matrix.


[View source]