module

Geode::MatrixOperations(M, N)

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.

Instance methods

&*(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]]
Source
&+(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]]
Source
&-(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]]
Source
*(scalar : Number) : CommonMatrix

Scales each element by the specified amount.

Matrix[[5, -2], [0, 1]] * 3 # => [[15, -6], [0, 3]]
Source
+(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]]
Source
-(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]]
Source
-

Returns a negated matrix.

-Matrix[[5, -2], [0, 1]] # => [[-5, 2], [0, -1]]
Source
/(scalar : Number) : CommonMatrix

Scales each element by the specified amount.

Matrix[[16, -2], [0, 1]] / 4 # => [[4.0, -0.5], [0.0, 0.25]]
Source
//(scalar : Number) : CommonMatrix

Scales each element by the specified amount.

Uses integer division.

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

Returns a matrix containing the absolute value of each element.

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

Returns a matrix containing the square of each element.

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

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]]
Source
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]]
Source
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]]
Source
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]]
Source
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]]
Source
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]]
Source
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]]
Source
floor

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]]
Source
fraction

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

Source
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]]
Source
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]]
Source
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]]
Source
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]]
Source
sign

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