Create a matrix by stacking matrices horizontally
x = Matrix[[1, 2], [3, 4]] y = Matrix[[5, 6], [7, 8]] Matrix.hstack(x, y) # => Matrix[[1, 2, 5, 6], [3, 4, 7, 8]]
Create a matrix by combining matrices entrywise, using the given block
x = Matrix[[6, 6], [4, 4]] y = Matrix[[1, 2], [3, 4]] Matrix.combine(x, y) {|a, b| a - b} # => Matrix[[5, 4], [1, 0]]
Creates new matrix by combining with other_matrices entrywise, using the given block.
x = Matrix[[6, 6], [4, 4]] y = Matrix[[1, 2], [3, 4]] x.combine(y) {|a, b| a - b} # => Matrix[[5, 4], [1, 0]]
The index method is specialized to return the index as [row, column] It also accepts an optional selector
argument, see each
for details.
Matrix[ [1,2], [3,4] ].index(&:even?) # => [0, 1] Matrix[ [1,1], [1,1] ].index(1, :strict_lower) # => [1, 0]
Returns a section of the matrix. The parameters are either:
start_row, nrows, start_col, ncols; OR
row_range, col_range
Matrix.diagonal(9, 5, -3).minor(0..1, 0..2) # => 9 0 0 # 0 5 0
Like Array#[]
, negative indices count backward from the end of the row or column (-1 is the last element). Returns nil if the starting row or column is greater than row_count
or column_count
respectively.
Returns the inverse of the matrix.
Matrix[[-1, -1], [0, -1]].inverse # => -1 1 # 0 -1
Returns the determinant of the matrix.
Beware that using Float
values can yield erroneous results because of their lack of precision. Consider using exact types like Rational
or BigDecimal
instead.
Matrix[[7,6], [3,9]].determinant # => 45
deprecated; use Matrix#determinant
Returns a new matrix resulting by stacking horizontally the receiver with the given matrices
x = Matrix[[1, 2], [3, 4]] y = Matrix[[5, 6], [7, 8]] x.hstack(y) # => Matrix[[1, 2, 5, 6], [3, 4, 7, 8]]
Returns the trace (sum of diagonal elements) of the matrix.
Matrix[[7,6], [3,9]].trace # => 16
Returns the transpose of the matrix.
Matrix[[1,2], [3,4], [5,6]] # => 1 2 # 3 4 # 5 6 Matrix[[1,2], [3,4], [5,6]].transpose # => 1 3 5 # 2 4 6
Returns a new matrix resulting by stacking vertically the receiver with the given matrices
x = Matrix[[1, 2], [3, 4]] y = Matrix[[5, 6], [7, 8]] x.vstack(y) # => Matrix[[1, 2], [3, 4], [5, 6], [7, 8]]
Returns the Eigensystem of the matrix; see EigenvalueDecomposition
.
m = Matrix[[1, 2], [3, 4]] v, d, v_inv = m.eigensystem d.diagonal? # => true v.inv == v_inv # => true (v * d * v_inv).round(5) == m # => true
Returns the imaginary part of the matrix.
Matrix[[Complex(1,2), Complex(0,1), 0], [1, 2, 3]] # => 1+2i i 0 # 1 2 3 Matrix[[Complex(1,2), Complex(0,1), 0], [1, 2, 3]].imaginary # => 2i i 0 # 0 0 0
Overrides Object#inspect
Returns true
iff all of vectors are linearly independent.
Vector.independent?(Vector[1,0], Vector[0,1]) # => true Vector.independent?(Vector[1,2], Vector[2,4]) # => false
Returns true
iff all of vectors are linearly independent.
Vector[1,0].independent?(Vector[0,1]) # => true Vector[1,2].independent?(Vector[2,4]) # => false
Overrides Object#inspect
Returns an incremented value of default
according to arg
.
Terminates option parsing. Optional parameter arg
is a string pushed back to be the first non-option argument.