struct

Matrix(T)

Inherits Iterable < Enumerable < Struct < Value < Object

Constructors

new(rows : Int, columns : Int, value : T)
Source
new(rows : Int, columns : Int)
Source
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.

Source

Class methods

[](*rows)

Alias for Matrix.rows.

Source
column(*values)

Creates a single column matrix with the given values.

Source
columns(columns : Array(Array) | Tuple)

Creates a matrix interpreting each argument as a column.

Source
diagonal(*values)

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

Source
identity(row_or_col_size : Int)

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

Source
row(*values)

Creates a single row matrix with the given values.

Source
rows(rows : Array(Array) | Tuple)

Creates a matrix interpreting each argument as a row.

Source

Instance methods

&(other : T | Number)
Source
*(other : Matrix)

Performs multiplication with another matrix.

Source
*(other : T | Number)
Source
**(other : Int)

Performs exponentiation

Source
+(other : Matrix)

Performs addition with another matrix.

Source
+(other : T | Number)
Source
-(other : Matrix)

Performs subtraction with another matrix.

Source
-(other : T | Number)
Source
-

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

Source
/(other : Matrix)

Performs division with another matrix.

Source
/(other : T | Number)
Source
<<(other : T | Number)
Source
==(other : Matrix)

Checks equality between self and another matrix.

Source
>>(other : T | Number)
Source
[](row : Int, column : Int)

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

Source
[](index : Int)

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

Source
[]=(row : Int, column : Int, value : T)

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

Source
[]=(index : Int, value : T)

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

Source
[]?(row : Int, column : Int)

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

Source
[]?(index : Int)

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

Source
^(other : T | Number)
Source
|(other : T | Number)
Source
at(row : Int, column : Int, &)

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

Source
at(row : Int, column : Int)

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

Source
at(index : Int, &)

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

Source
at(index : Int)

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

Source
clone

Creates an identical matrix.

Source
column(column_index : Int)

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

Source
column(index : Int, &)

Yields elements of the column at the given index.

Source
column_count

The number of columns.

Source
column_vectors

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

Source
columns

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

Source
cycle(which : Symbol = :all)

Same as each.cycle.

See also: Iterator#cycle.

Source
determinant

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

Source
diagonal?

Returns true if all non-diagonal elements are 0.

Source
dimensions
Source
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

Source
each

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

Source
each(which : Symbol = :all)

Must return an Iterator over the elements in this collection.

Source
each_index(which : Symbol = :all, &)

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

Source
each_index(which : Symbol = :all)
Source
each_with_index(which : Symbol = :all, &)

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

Source
empty?

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

Source
hash

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.

Source
index(value : T, which : Symbol = :all)

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

Source
index

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

Source
inspect(io : IO)

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)"
Source
inverse

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

Source
lower_triangular?

Returns true if the matrix is a lower triangular matrix.

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

Source
map!

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.

Source
minor(start_row : Int, rows : Int, start_col : Int, columns : Int)

Returns a subsection of the matrix.

Source
minor(row_range : Range(Int, Int), col_range : Range(Int, Int))

Returns a subsection of the matrix.

Source
permutation?

Returns true if the matrix is a permutation matrix.

Source
rank

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

Source
regular?

Returns true if the matrix is regular.

Source
reverse
Source
reverse!

Reverses the order of the matrix.

Source
row(row_index : Int)

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

Source
row(index : Int, &)

Yields elements of the row at the given index.

Source
row_count

The number of rows.

Source
row_vectors

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

Source
rows

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

Source
shuffle
Source
shuffle!

Shuffles the elements of the matrix.

Source
shuffle_columns
Source
shuffle_columns!

Shuffles the elements of each column.

Source
shuffle_rows
Source
shuffle_rows!

Shuffles the elements of each row.

Source
singular?

Returns true if the matrix is singular.

Source
size

The total number of elements.

Source
square?

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

Source
swap_columns(col_1 : Int, col_2 : Int)

Swaps two columns.

Source
swap_rows(row_1 : Int, row_2 : Int)

Swaps two rows.

Source
symmetric?

Returns true if the matrix is symmetric.

Source
to_a

Returns an array of every element in the matrix.

Source
to_h

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

Source
to_s(io : IO)

Displays the matrix in a more readable form.

Source
trace

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

Source
transpose

Changes the rows into columns and vice versa.

Source
upper_triangular?

Returns true if the matrix is an upper triangular matrix.

Source

Nested types