module Geode::SquareMatrix(T, M, N)

Overview

Operations applicable to square matrices.

Intended to be used as a mix-in on matrix types. M and N are positive integers indicating the number of rows and columns respectively. These methods will produce a compilation error when called on a non-square matrix.

Direct including types

Defined in:

geode/matrices/square.cr

Instance Method Summary

Instance Method Detail

def determinant #

Computes the determinant of this matrix.

This method can only be called on square matrices.

matrix = Matrix[[1, 4, 7], [5, 8, 2], [9, 3, 6]]
matrix.determinant # => -405

[View source]
def diagonal : CommonVector(T, N) #

Retrieves the elements of the main diagonal.

Returns the elements as a vector.

This method can only be called on square matrices.

matrix = Matrix[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
matrix.diagonal # => (1, 5, 9)

[View source]
def each_diagonal(& : T -> _) : Nil #

Enumerates through the elements of the main diagonal.

This method can only be called on square matrices.

matrix = Matrix[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
matrix.each_diagonal do |e|
  puts e
end

# Output:
# 1
# 5
# 9

[View source]
def each_diagonal #

Returns an iterator that enumerates through the elements of the main diagonal.

This method can only be called on square matrices.

matrix = Matrix[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
matrix.each_diagonal.to_a # => [1, 5, 9]

[View source]
def trace #

Computes the trace of the matrix.

The trace is the sum of the elements along the main diagonal.

This method can only be called on square matrices.

matrix = Matrix[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
matrix.trace # => 15

[View source]