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
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]]
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]]
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]]
Scales each element by the specified amount.
Matrix[[5, -2], [0, 1]] * 3 # => [[15, -6], [0, 3]]
Adds two matrices together.
Matrix[[5, -2], [0, 1]] + Matrix[[2, -1], [4, -2]] # => [[7, -3], [4, -1]]
Subtracts another matrix from this one.
Matrix[[5, -2], [0, 1]] - Matrix[[2, -1], [4, -2]] # => [[3, -1], [-4, 3]]
Scales each element by the specified amount.
Matrix[[16, -2], [0, 1]] / 4 # => [[4.0, -0.5], [0.0, 0.25]]
Scales each element by the specified amount.
Uses integer division.
Matrix[[7, -3], [0, 1]] // 3 # => [[2, -1], [0, 0]]
Returns a matrix containing the absolute value of each element.
Matrix[[-5, 42], [0, -20]].abs # => [[5, 42], [0, 20]]
Returns a matrix containing the square of each element.
Matrix[[-5, 3], [0, -2]].abs2 # => [[25, 9], [0, 4]]
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]]
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]]
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]]
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]]
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]]
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]]
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]]
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]]
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]]
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.
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]]
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]]
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]]
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]]
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]]