struct Matrix(T)
- Matrix(T)
- Struct
- Value
- Object
Included Modules
- Enumerable(T)
- Iterable(T)
Defined in:
matrix.crConstructors
- .new(rows : Int, columns : Int, value : T)
- .new(rows : Int, columns : Int)
-
.new(rows : Int, columns : Int, &block : Int32, Int32, Int32 -> T)
Creates a matrix with the given number of rows and columns.
Class Method Summary
-
.[](*rows)
Alias for Matrix.rows.
-
.column(*values)
Creates a single column matrix with the given values.
-
.columns(columns : Array(Array) | Tuple)
Creates a matrix interpreting each argument as a column.
-
.diagonal(*values)
Creates a diagonal matrix with the supplied arguments.
-
.identity(row_or_col_size : Int)
Creates a Matrix(Int32), whose diagonal values are 1 and the rest are 0.
-
.row(*values)
Creates a single row matrix with the given values.
-
.rows(rows : Array(Array) | Tuple)
Creates a matrix interpreting each argument as a row.
Instance Method Summary
- #&(other : T | Number)
-
#*(other : Matrix)
Performs multiplication with another matrix.
- #*(other : T | Number)
-
#**(other : Int)
Performs exponentiation
-
#+(other : Matrix)
Performs addition with another matrix.
- #+(other : T | Number)
-
#-(other : Matrix)
Performs subtraction with another matrix.
- #-(other : T | Number)
-
#-
Returns a new matrix of the same size after calling the unary #- method on every element.
-
#/(other : Matrix)
Performs division with another matrix.
- #/(other : T | Number)
- #<<(other : T | Number)
-
#==(other : Matrix)
Checks equality between self and another matrix.
- #>>(other : T | Number)
-
#[](row : Int, column : Int)
Retrieves the element at the given row and column indexes.
-
#[](index : Int)
Retrieves the element at the given linear index.
-
#[]=(row : Int, column : Int, value : T)
Replaces the element at the given row and column with the given value.
-
#[]=(index : Int, value : T)
Replaces the element at the given linear index with the given value.
-
#[]?(row : Int, column : Int)
Retrieves the element at the given row and column indexes.
-
#[]?(index : Int)
Retrieves the element at the given linear index.
- #^(other : T | Number)
- #|(other : T | Number)
-
#at(row : Int, column : Int, &)
Retrieves the element at the given row and column indexes.
-
#at(row : Int, column : Int)
Retrieves the element at the given row and column indexes.
-
#at(index : Int, &)
Retrieves the element at the given linear index.
-
#at(index : Int)
Retrieves the element at the given linear index.
-
#clone
Creates an identical matrix.
-
#column(column_index : Int)
Returns an iterator for the elements of the column at the given index.
-
#column(index : Int, &)
Yields elements of the column at the given index.
-
#column_count
The number of columns.
-
#column_vectors
Returns an array of smaller matrices, each representing a column from self.
-
#columns
Returns an array of arrays that correspond to the columns of the matrix.
-
#cycle(which : Symbol = :all)
Same as
each.cycle
. -
#determinant
Returns the determinant of the matrix.
-
#diagonal?
Returns true if all non-diagonal elements are 0.
- #dimensions
-
#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
-
#each(&)
Yields every element of the matrix linearly: First the elements of the first row, then the elements of the second row, etc.
-
#each(which : Symbol = :all)
Must return an
Iterator
over the elements in this collection. -
#each_index(which : Symbol = :all, &)
Yields every row and column index.
- #each_index(which : Symbol = :all)
-
#each_with_index(which : Symbol = :all, &)
Yields every element along with its row and column index.
-
#empty?
Returns true if the matrix has either 0 rows or 0 columns.
-
#hash
Generates an
UInt64
hash value for this object. -
#index(value : T, which : Symbol = :all)
Returns the row and column index of the first occurrence of "value" in the matrix, nil otherwise.
-
#index(&block : T, Int32, Int32, Int32 -> Bool)
Returns the row and column index of the first occurrence of the block returning true, nil otherwise
-
#inspect(io : IO)
Appends this struct's name and instance variables names and values to the given IO.
-
#inverse
Returns the inverse of the matrix.
-
#lower_triangular?
Returns true if the matrix is a lower triangular matrix.
-
#map(&)
Returns a new matrix with the return values of the block.
-
#map!(&block : T, Int32, Int32, Int32 -> _)
Changes the values of the matrix according to the return values of the block.
-
#minor(start_row : Int, rows : Int, start_col : Int, columns : Int)
Returns a subsection of the matrix.
-
#minor(row_range : Range(Int, Int), col_range : Range(Int, Int))
Returns a subsection of the matrix.
-
#permutation?
Returns true if the matrix is a permutation matrix.
-
#rank
Returns the rank of the matrix.
-
#regular?
Returns true if the matrix is regular.
- #reverse
-
#reverse!
Reverses the order of the matrix.
-
#row(row_index : Int)
Returns an iterator for the elements of the row at the given index.
-
#row(index : Int, &)
Yields elements of the row at the given index.
-
#row_count
The number of rows.
-
#row_vectors
Returns an array of smaller matrices, each representing a row from self.
-
#rows
Returns an array of arrays that correspond to the rows of the matrix.
- #shuffle
-
#shuffle!
Shuffles the elements of the matrix.
- #shuffle_columns
-
#shuffle_columns!
Shuffles the elements of each column.
- #shuffle_rows
-
#shuffle_rows!
Shuffles the elements of each row.
-
#singular?
Returns true if the matrix is singular.
-
#size
The total number of elements.
-
#square?
Returns true if the number of rows equals the number of columns.
-
#swap_columns(col_1 : Int, col_2 : Int)
Swaps two columns.
-
#swap_rows(row_1 : Int, row_2 : Int)
Swaps two rows.
-
#symmetric?
Returns true if the matrix is symmetric.
-
#to_a
Returns an array of every element in the matrix.
-
#to_h
Returns a hash: {row_index, column_index} => element
-
#to_s(io : IO)
Displays the matrix in a more readable form.
-
#trace
Returns the sum of the diagonal elements.
-
#transpose
Changes the rows into columns and vice versa.
-
#upper_triangular?
Returns true if the matrix is an upper triangular matrix.
Constructor Detail
Creates a matrix with the given number of rows and columns. It yields the linear, row and column indexes in that order.
Class Method Detail
Creates a matrix interpreting each argument as a column.
Creates a diagonal matrix with the supplied arguments. Best suited to numeric matrices.
Creates a Matrix(Int32), whose diagonal values are 1 and the rest are 0.
Instance Method Detail
Returns a new matrix of the same size after calling the unary #- method on every element. Best suited to numeric matrices.
Retrieves the element at the given row and column indexes. Raises IndexError.
Replaces the element at the given row and column with the given value.
Replaces the element at the given linear index with the given value.
Retrieves the element at the given row and column indexes. Returns nil if out of bounds.
Retrieves the element at the given linear index. Returns nil if out of bounds
Retrieves the element at the given row and column indexes. Yields if out of bounds.
Retrieves the element at the given row and column indexes. Raises IndexError.
Returns an iterator for the elements of the column at the given index.
Same as each.cycle
.
See also: Iterator#cycle
.
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
Yields every element of the matrix linearly: First the elements of the first row, then the elements of the second row, etc.
Must return an Iterator
over the elements in this collection.
Yields every row and column index. See #each for the optional directives.
Yields every element along with its row and column index. See #each for the optional directives.
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.
Returns the row and column index of the first occurrence of "value" in the matrix, nil otherwise.
Returns the row and column index of the first occurrence of the block returning true, nil otherwise
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)"
Returns a new matrix with the return values of the block. Yields the element and its linear, row and column indices in that order.
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.
Returns a subsection of the matrix.
Returns a subsection of the matrix.