Skip to content

Matrix Algebra

Sergey Duyunov edited this page Nov 18, 2019 · 8 revisions

*(m)

Matrix multiplication.

m1 = Matrix[[1, 2], [3, 4]]
m2 = Matrix[[1, 0], [1, 2]]
m1 * m2
# => Matrix[[3, 4], [7, 8]]

**(other)

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

+(m)

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

add!(m)

Addition with a given matrix.

m = Matrix[[1, 2], [3, 4]]
m.add!(Matrix[[1, 1], [1, 1]])
m
# => Matrix[[2, 3], [4, 5]]

-(m)

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

sub!(m)

Subtraction from a given matrix.

m = Matrix[[1, 2], [3, 4]]
m.sub!(Matrix[[1, 1], [1, 1]])
m
# => Matrix[[0, 1], [2, 3]]

/(other)

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

adjugate()

Returns the adjugate of the matrix.

m = Matrix[[7, 6], [3, 9]]
m.adjugate
# => Matrix[[9, -6], [-3, 7]]

abs()

Creates a matrix by taking the absolute value of each element.

m = Matrix[[1, -3], [0, -4]]
m.abs()
# => Matrix[[1, 3], [0, 4]]

cofactor(row, column)

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

determinant()

Returns the determinant of the matrix.

m = Matrix[[7,6], [3,9]]
m.determinant
# => 45

det()

Alias for: determinant.

laplace_expansion(row: nil, column: nil)

Equivalent to the determinant method. Implemented just for compatibility with standard matrix.

cofactor_expansion(row: nil, column: nil)

Alias for: laplace_expansion.

conjugate()

Returns the matrix unchanged. Implemented just for compatibility with standard matrix.

conj()

Alias for: conjugate.

hadamard_product(other)

Hadamard product.

m1 = Matrix[[1, 2], [3, 4]]
m2 = Matrix[[2, 2], [3, -1]]
m1.hadamard_product(m2)
# => Matrix[[2, 4], [9, -4]]

entrywise_product(other)

Alias for: hadamard_product

first_minore(row, column)

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

inverse()

Returns the inverse of the matrix.

m = Matrix[[2, -3], [2, -2]]
m.inverse
# => Matrix[[-1, 1.5], [-1, 1]]

inv()

Alias for: inverse

rank()

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

round(ndigits = 0)

Returns a matrix with entries rounded to the given precision (see Float#round).

transpose()

Creates a new matrix by transposing.

m = Matrix[[1, 2, 3], [4, 5, 6]]
m.transpose
# => Matrix[[1, 4], [2, 5], [3, 6]]

t()

Alias for: transpose

trace()

Returns the trace (sum of diagonal elements) of the matrix.

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

tr()

Alias for: trace

imaginary()

Returns a zero-matrix of the same size. Implemented just for compatibility with standard matrix.

imag()

Alias for: imaginary

real()

Returns the matrix unchanged. Implemented just for compatibility with standard matrix.

rect()

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

rectangular()

Alias for: rect