module Geode::MatrixOperations(M, N)

Overview

Common operations for matrices.

Intended to be used as a mix-in on matrix types. M and N are the number of rows and columns respectively.

Direct including types

Defined in:

geode/matrices/operations.cr

Instance Method Summary

Instance Method Detail

def &*(scalar : Number) : CommonMatrix #

Scales each element by the specified amount.

Values will wrap instead of overflowing and raising an error.

Matrix[[5, -2], [0, 1]] &* 3 # => [[15, -6], [0, 3]]

[View source]
def &+(other : CommonMatrix(T, M, N)) : CommonMatrix forall T #

Adds two matrices together.

Values will wrap instead of overflowing and raising an error.

Matrix[[5, -2], [0, 1]] &+ Matrix[[2, -1], [4, -2]] # => [[7, -3], [4, -1]]

[View source]
def &-(other : CommonMatrix(T, M, N)) : CommonMatrix forall T #

Subtracts another matrix from this one.

Values will wrap instead of overflowing and raising an error.

Matrix[[5, -2], [0, 1]] &- Matrix[[2, -1], [4, -2]] # => [[3, -1], [-4, 3]]

[View source]
def *(scalar : Number) : CommonMatrix #

Scales each element by the specified amount.

Matrix[[5, -2], [0, 1]] * 3 # => [[15, -6], [0, 3]]

[View source]
def +(other : CommonMatrix(T, M, N)) : CommonMatrix forall T #

Adds two matrices together.

Matrix[[5, -2], [0, 1]] + Matrix[[2, -1], [4, -2]] # => [[7, -3], [4, -1]]

[View source]
def -(other : CommonMatrix(T, M, N)) : CommonMatrix forall T #

Subtracts another matrix from this one.

Matrix[[5, -2], [0, 1]] - Matrix[[2, -1], [4, -2]] # => [[3, -1], [-4, 3]]

[View source]
def - : self #

Returns a negated matrix.

-Matrix[[5, -2], [0, 1]] # => [[-5, 2], [0, -1]]

[View source]
def /(scalar : Number) : CommonMatrix #

Scales each element by the specified amount.

Matrix[[16, -2], [0, 1]] / 4 # => [[4.0, -0.5], [0.0, 0.25]]

[View source]
def //(scalar : Number) : CommonMatrix #

Scales each element by the specified amount.

Uses integer division.

Matrix[[7, -3], [0, 1]] // 3 # => [[2, -1], [0, 0]]

[View source]
def abs : self #

Returns a matrix containing the absolute value of each element.

Matrix[[-5, 42], [0, -20]].abs # => [[5, 42], [0, 20]]

[View source]
def abs2 : self #

Returns a matrix containing the square of each element.

Matrix[[-5, 3], [0, -2]].abs2 # => [[25, 9], [0, 4]]

[View source]
def ceil : self #

Returns a matrix with elements rounded up to the nearest integer.

Matrix[[1.2, -5.7], [3.0, 0.1]] # => [[2.0, -5.0], [3.0, 1.0]]

[View source]
def clamp(min : CommonMatrix(T, M, N), max : CommonMatrix(T, M, N)) : CommonMatrix forall T #

Returns a matrix restricted to the minimum and maximum values from other matrices.

min = Matrix[[-1, -1], [-1, -1]]
max = Matrix[[1, 1], [1, 1]]
Matrix[[5, -2], [0, 1]].clamp(min, max) # => [[1, -1], [0, 1]]

[View source]
def clamp(min, max) : CommonMatrix #

Returns a matrix restricted to min and max values.

min and max should be scalar values. Each element of the matrix is clipped to these same values.

Matrix[[5, -2], [0, 1]].clamp(-1, 1) # => [[1, -1], [0, 1]]

[View source]
def clamp(range : Range(CommonMatrix(T, M, N), CommonMatrix(T, M, N))) : CommonMatrix forall T #

Returns a matrix restricted to the range from other matrices.

min = Matrix[[-1, -1], [-1, -1]]
max = Matrix[[1, 1], [1, 1]]
Matrix[[5, -2], [0, 1]].clamp(min..max) # => [[1, -1], [0, 1]]

[View source]
def clamp(range : Range) : CommonMatrix #

Returns a matrix restricted to a range.

The range should consist of scalar values. Each element of the matrix is clipped to the same range.

Matrix[[5, -2], [0, 1]].clamp(-1..1) # => [[1, -1], [0, 1]]

[View source]
def edge(edge : CommonMatrix(T, M, N)) : self forall T #

Returns a new matrix where each element is 0 if it's less than the corresponding edge value, or 1 if it's greater.

Matrix[[0, 1], [2, 3]].edge(Matrix[[3, 2], [1, 0]]) # => [[0, 0], [1, 1]]

[View source]
def edge(edge) : self #

Returns a new matrix where each element is 0 if it's less than the edge value, or 1 if it's greater.

Matrix[[0, 1], [2, 3]].edge(2) # => [[0, 0], [1, 1]]

[View source]
def floor : self #

Returns a matrix with elements rounded down to the nearest integer.

Matrix[[1.2, -5.7], [3.0, 0.1]] # => [[1.0, -6.0], [3.0, 0.0]]

[View source]
def fraction : self #

Returns a matrix with the fraction from each element.

This is effectively equal to:

fraction(v) = v - floor(v)
Matrix[[1.2, -5.7], [3.0, 0.1]] # => [[0.2, 0.3], [0.0, 0.1]]

[View source]
def lerp(other : CommonMatrix(T, M, N), t : Number) : CommonMatrix forall T #

Calculates the linear interpolation between two matrices.

t is a value from 0 to 1, where 0 represents this matrix and 1 represents other. Any value between 0 and 1 will result in a proportional amount of this matrix and other.

This method uses the precise calculation that does not suffer precision loss from high exponential differences.


[View source]
def round(mode : Number::RoundingMode = :ties_even) : self #

Returns a matrix with each element rounded.

See Number#round for details.

Matrix[[1.2, -5.7], [3.0, 1.5]].round # => [[1.0, -6.0], [3.0, 2.0]]

[View source]
def round(digits : Number, base = 10, *, mode : Number::RoundingMode = :ties_even) : self #

Returns a matrix with each element rounded.

See Number#round for details.

Matrix[[1.25, -5.77], [3.01, 0.1]].round(1) # => [[1.3, -5.8], [3.0, 0.1]]

[View source]
def scale(matrix : CommonMatrix(T, M, N)) : CommonMatrix forall T #

Returns a matrix with each element scaled by the corresponding element value.

Matrix[[1, 0], [-1, -2]].scale(Matrix[[2, 3], [5, 1]]) # => [[2, 0], [-5, -2]]

[View source]
def scale!(matrix : CommonMatrix(T, M, N)) : CommonMatrix forall T #

Returns a matrix with each element scaled by the corresponding element value.

Values will wrap instead of overflowing and raising an error.

Matrix[[1, 0], [-1, -2]].scale!(Matrix[[2, 3], [5, 1]]) # => [[2, 0], [-5, -2]]

[View source]
def sign : self #

Returns a matrix with elements equal to their original sign.

1 is used for positive values, -1 for negative values, and 0 for zero.

Matrix[[5, 0], [-1, -5]] # => [[1, 0], [-1, -1]]

[View source]