-
Notifications
You must be signed in to change notification settings - Fork 2
Matrix Algebra
Matrix multiplication.
m1 = Matrix[[1, 2], [3, 4]]
m2 = Matrix[[1, 0], [1, 2]]
m1 * m2
# => Matrix[[3, 4], [7, 8]]
Matrix exponentiation. Equivalent to multiplying the matrix by itself N times.
m = Matrix[[1, 2], [-1, 0]]
m**10
# => Matrix[[23, -22], [11, 34]]
m = Matrix[[1, 2], [2, 3]]
m**-1
# => Matrix[[-3, 2], [2, -1]]
Matrix addition.
m1 = Matrix[[1, 2], [3, 4]]
m2 = Matrix[[1, 0], [1, 2]]
m1 + m2
# => Matrix[[2, 2], [4, 6]]
Unary plus. Returns the original matrix.
m = Matrix[[1, 2, 3], [4, -5, 6]]
+m
# => Matrix[[1, 2, 3], [4, -5, 6]]
Addition with a given matrix.
m = Matrix[[1, 2], [3, 4]]
m.add!(Matrix[[1, 1], [1, 1]])
m
# => Matrix[[2, 3], [4, 5]]
Matrix subtraction.
m1 = Matrix[[1, 2], [3, 4]]
m2 = Matrix[[1, 0], [1, 2]]
m1 - m2
# => Matrix[[0, 2], [2, 2]]
Unary minus. Returns a matrix, replacing the sign of each element with the opposite.
m = Matrix[[1, 2, 3], [4, -5, 6]]
-m
# => Matrix[[-1, -2, -3], [-4, 5, -6]]
Subtraction from a given matrix.
m = Matrix[[1, 2], [3, 4]]
m.sub!(Matrix[[1, 1], [1, 1]])
m
# => Matrix[[0, 1], [2, 3]]
Matrix division (multiplication by the inverse).
m1 = Matrix[[7,6], [3,9]]
m2 = Matrix[[2,9], [3,1]]
m1 / m2
# => Matrix[[-7, 1], [-3, -6]]
Returns the adjugate of the matrix.
m = Matrix[[7, 6], [3, 9]]
m.adjugate
# => Matrix[[9, -6], [-3, 7]]
Creates a matrix by taking the absolute value of each element.
m = Matrix[[1, -3], [0, -4]]
m.abs()
# => Matrix[[1, 3], [0, 4]]
Returns the (row, column) cofactor which is obtained by multiplying the first minor by (-1)**(row + column).
m = Matrix.diagonal(9, 5, -3, 4)
m.cofactor(1, 1)
# => -108
Returns the determinant of the matrix.
m = Matrix[[7,6], [3,9]]
m.determinant
# => 45
Alias for: determinant.
Equivalent to the determinant method. Implemented just for compatibility with standard matrix.
Alias for: laplace_expansion.
Returns the matrix unchanged. Implemented just for compatibility with standard matrix.
Alias for: conjugate.
Hadamard product.
m1 = Matrix[[1, 2], [3, 4]]
m2 = Matrix[[2, 2], [3, -1]]
m1.hadamard_product(m2)
# => Matrix[[2, 4], [9, -4]]
Alias for: hadamard_product
Returns the submatrix obtained by deleting the specified row and column.
m = Matrix[[1, 2, 3], [4, 5, 6], [7, 8, 9], [0, 1, 2]]
m.first_minore(2, 2)
# => Matrix[[1, 2], [4, 5], [0, 1]]
Returns the inverse of the matrix.
m = Matrix[[2, -3], [2, -2]]
m.inverse
# => Matrix[[-1, 1.5], [-1, 1]]
Alias for: inverse
Returns the rank of the matrix. Can give erroneous results because to lack of accuracy.
m = Matrix[
[1, 2, 3, 4, 5],
[1, 2, 3, 5, 5],
[0, 0, 0, 5, 0],
[2, 2, 2, 2, 2]
]
m.rank
# => 3
Returns a matrix with entries rounded to the given precision (see Float#round).
Creates a new matrix by transposing.
m = Matrix[[1, 2, 3], [4, 5, 6]]
m.transpose
# => Matrix[[1, 4], [2, 5], [3, 6]]
Alias for: transpose
Returns the trace (sum of diagonal elements) of the matrix.
m = Matrix[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
m.trace
# => 15
Alias for: trace
Returns a zero-matrix of the same size. Implemented just for compatibility with standard matrix.
Alias for: imaginary
Returns the matrix unchanged. Implemented just for compatibility with standard matrix.
Returns an array containing matrices corresponding to the real and imaginary parts of the matrix.
m = Matrix[[1, 2], [4, 5]]
m.rect
# => [Matrix[[1, 2], [4, 5]], Matrix[[0, 0], [0, 0]]]
Alias for: rect