class Matrix(T)

Included Modules

Defined in:

matrix.cr

Class Method Summary

Instance Method Summary

Macro Summary

Instance methods inherited from module Iterable

cycle
cycle(n)
cycle
, each each, each_cons(count : Int) each_cons, each_slice(count : Int) each_slice, each_with_index(offset = 0) each_with_index, each_with_object(obj) each_with_object

Instance methods inherited from module Enumerable(T)

all?
all?(&block)
all?
, any?
any?(&block)
any?
, compact_map(&block) compact_map, count(item)
count(&block)
count
, cycle(&block)
cycle(n, &block)
cycle
, each(&block : T -> _) each, each_cons(count : Int, &block) each_cons, each_slice(count : Int, &block) each_slice, each_with_index(offset = 0, &block) each_with_index, each_with_object(obj, &block) each_with_object, find(if_none = nil, &block) find, first first, first? first?, flat_map(&block : T -> Array(U)) flat_map, grep(pattern) grep, group_by(&block : T -> U) group_by, in_groups_of(size : Int, filled_up_with = nil, &block)
in_groups_of(size : Int, filled_up_with = nil)
in_groups_of
, includes?(obj) includes?, index(obj)
index(&block)
index
, index_by(&block : T -> U) index_by, join(separator = "")
join(separator, io, &block)
join(separator = "", &block)
join(separator, io)
join
, map(&block : T -> U) map, map_with_index(&block : T, Int32 -> U) map_with_index, max max, max? max?, max_by(&block : T -> U) max_by, max_by?(&block : T -> U) max_by?, max_of(&block : T -> U) max_of, max_of?(&block : T -> U) max_of?, min min, min? min?, min_by(&block : T -> U) min_by, min_by?(&block : T -> U) min_by?, min_of(&block : T -> U) min_of, min_of?(&block : T -> U) min_of?, minmax minmax, minmax? minmax?, minmax_by(&block : T -> U) minmax_by, minmax_by?(&block : T -> U) minmax_by?, minmax_of(&block : T -> U) minmax_of, minmax_of?(&block : T -> U) minmax_of?, none?(&block)
none?
none?
, one?(&block) one?, partition(&block) partition, reduce(&block)
reduce(memo, &block)
reduce
, reject(&block : T -> ) reject, select(&block : T -> ) select, size size, skip(count : Int) skip, skip_while(&block) skip_while, sum(initial, &block)
sum(&block)
sum(initial)
sum
sum
, take(count : Int) take, take_while(&block) take_while, to_a to_a, to_h to_h, to_set to_set

Instance methods inherited from class Reference

! !, ==(other)
==(other : self)
==
, hash hash, inspect(io : IO) : Nil inspect, nil? nil?, object_id object_id, same?(other : Nil)
same?(other : Reference)
same?
, to_s(io : IO) : Nil to_s

Instance methods inherited from class Object

! !, !=(other) !=, !~(other) !~, ==(other) ==, ===(other) ===, =~(other) =~, class class, clone clone, crystal_type_id crystal_type_id, dup dup, hash hash, inspect
inspect(io : IO)
inspect
, itself itself, not_nil! not_nil!, tap(&block) tap, to_json to_json, to_pretty_json(io : IO)
to_pretty_json
to_pretty_json
, to_s
to_s(io : IO)
to_s
, to_yaml(io : IO)
to_yaml
to_yaml
, try(&block) try

Class methods inherited from class Object

==(other : Class) ==, ===(other) ===, cast(other) : self cast, from_json(string_or_io) from_json, from_yaml(string : String) from_yaml, hash hash, inspect(io) inspect, name : String name, to_s(io) to_s

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.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]
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]
def self.new(rows : Int, columns : Int, value : T) #

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

[View source]

Instance Method Detail

def &(other : T | Number) #

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

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

Performs multiplication with another matrix.


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

Performs exponentiation


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

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

Performs addition with another matrix.


[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 [](index : Int) #

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


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

Retrieves the element at the given row and column indexes. 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 at(index : Int) #

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


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

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


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

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 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, &block) #

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 = :all : Symbol) #

[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 = :all : Symbol) #

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

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(&block) #

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_index(which = :all : Symbol, &block) #

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


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

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

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 #

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

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

[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(&block) #

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(index : Int, &block) #

Yields elements of the row at the given index.


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

Returns an iterator for the 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 #

[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]
def |(other : T | Number) #

[View source]

Macro Detail

macro def_exception(name, message) #

[View source]
macro def_operators(*operators) #

[View source]