diff --git a/lib/matrix.rb b/lib/matrix.rb
index 0ce9634..5133b41 100644
--- a/lib/matrix.rb
+++ b/lib/matrix.rb
@@ -47,8 +47,8 @@ class Matrix
#
# Creates a matrix where each argument is a row.
# Matrix[ [25, 93], [-1, 66] ]
- # => 25 93
- # -1 66
+ # # => 25 93
+ # # -1 66
#
def Matrix.[](*rows)
rows(rows, false)
@@ -59,8 +59,8 @@ def Matrix.[](*rows)
# of the matrix. If the optional argument +copy+ is false, use the given
# arrays as the internal structure of the matrix without copying.
# Matrix.rows([[25, 93], [-1, 66]])
- # => 25 93
- # -1 66
+ # # => 25 93
+ # # -1 66
#
def Matrix.rows(rows, copy = true)
rows = convert_to_array(rows, copy)
@@ -77,8 +77,8 @@ def Matrix.rows(rows, copy = true)
#
# Creates a matrix using +columns+ as an array of column vectors.
# Matrix.columns([[25, 93], [-1, 66]])
- # => 25 -1
- # 93 66
+ # # => 25 -1
+ # # 93 66
#
def Matrix.columns(columns)
rows(columns, false).transpose
@@ -91,9 +91,9 @@ def Matrix.columns(columns)
# Returns an enumerator if no block is given.
#
# m = Matrix.build(2, 4) {|row, col| col - row }
- # => Matrix[[0, 1, 2, 3], [-1, 0, 1, 2]]
+ # # => Matrix[[0, 1, 2, 3], [-1, 0, 1, 2]]
# m = Matrix.build(3) { rand }
- # => a 3x3 matrix with random elements
+ # # => a 3x3 matrix with random elements
#
def Matrix.build(row_count, column_count = row_count)
row_count = CoercionHelper.coerce_to_int(row_count)
@@ -111,9 +111,9 @@ def Matrix.build(row_count, column_count = row_count)
#
# Creates a matrix where the diagonal elements are composed of +values+.
# Matrix.diagonal(9, 5, -3)
- # => 9 0 0
- # 0 5 0
- # 0 0 -3
+ # # => 9 0 0
+ # # 0 5 0
+ # # 0 0 -3
#
def Matrix.diagonal(*values)
size = values.size
@@ -130,8 +130,8 @@ def Matrix.diagonal(*values)
# Creates an +n+ by +n+ diagonal matrix where each diagonal element is
# +value+.
# Matrix.scalar(2, 5)
- # => 5 0
- # 0 5
+ # # => 5 0
+ # # 0 5
#
def Matrix.scalar(n, value)
diagonal(*Array.new(n, value))
@@ -140,8 +140,8 @@ def Matrix.scalar(n, value)
#
# Creates an +n+ by +n+ identity matrix.
# Matrix.identity(2)
- # => 1 0
- # 0 1
+ # # => 1 0
+ # # 0 1
#
def Matrix.identity(n)
scalar(n, 1)
@@ -154,8 +154,8 @@ class << Matrix
#
# Creates a zero matrix.
# Matrix.zero(2)
- # => 0 0
- # 0 0
+ # # => 0 0
+ # # 0 0
#
def Matrix.zero(row_count, column_count = row_count)
rows = Array.new(row_count){Array.new(column_count, 0)}
@@ -166,7 +166,7 @@ def Matrix.zero(row_count, column_count = row_count)
# Creates a single-row matrix where the values of that row are as given in
# +row+.
# Matrix.row_vector([4,5,6])
- # => 4 5 6
+ # # => 4 5 6
#
def Matrix.row_vector(row)
row = convert_to_array(row)
@@ -177,9 +177,9 @@ def Matrix.row_vector(row)
# Creates a single-column matrix where the values of that column are as given
# in +column+.
# Matrix.column_vector([4,5,6])
- # => 4
- # 5
- # 6
+ # # => 4
+ # # 5
+ # # 6
#
def Matrix.column_vector(column)
column = convert_to_array(column)
@@ -192,12 +192,12 @@ def Matrix.column_vector(column)
#
# m = Matrix.empty(2, 0)
# m == Matrix[ [], [] ]
- # => true
+ # # => true
# n = Matrix.empty(0, 3)
# n == Matrix.columns([ [], [], [] ])
- # => true
+ # # => true
# m * n
- # => Matrix[[0, 0, 0], [0, 0, 0]]
+ # # => Matrix[[0, 0, 0], [0, 0, 0]]
#
def Matrix.empty(row_count = 0, column_count = 0)
raise ArgumentError, "One size must be 0" if column_count != 0 && row_count != 0
@@ -251,6 +251,8 @@ def Matrix.hstack(x, *matrices)
new result, total_column_count
end
+ # :call-seq:
+ # Matrix.combine(*matrices) { |*elements| ... }
#
# Create a matrix by combining matrices entrywise, using the given block
#
@@ -276,12 +278,21 @@ def Matrix.combine(*matrices)
new rows, x.column_count
end
+ # :call-seq:
+ # combine(*other_matrices) { |*elements| ... }
+ #
+ # 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]]
def combine(*matrices, &block)
Matrix.combine(self, *matrices, &block)
end
#
- # Matrix.new is private; use Matrix.rows, columns, [], etc... to create.
+ # Matrix.new is private; use ::rows, ::columns, ::[], etc... to create.
#
def initialize(rows, column_count = rows[0].size)
# No checking is done at this point. rows must be an Array of Arrays.
@@ -466,8 +477,8 @@ def column(j) # :yield: e
# * :strict_upper: yields only elements above the diagonal
# * :upper: yields only elements on or above the diagonal
# Matrix[ [1,2], [3,4] ].collect { |e| e**2 }
- # => 1 4
- # 9 16
+ # # => 1 4
+ # # 9 16
#
def collect(which = :all, &block) # :yield: e
return to_enum(:collect, which) unless block_given?
@@ -512,9 +523,9 @@ def freeze
# * :strict_upper: yields only elements above the diagonal
# * :upper: yields only elements on or above the diagonal
#
- # Matrix[ [1,2], [3,4] ].each { |e| puts e }
- # # => prints the numbers 1 to 4
- # Matrix[ [1,2], [3,4] ].each(:strict_lower).to_a # => [3]
+ # Matrix[ [1,2], [3,4] ].each { |e| puts e }
+ # # => prints the numbers 1 to 4
+ # Matrix[ [1,2], [3,4] ].each(:strict_lower).to_a # => [3]
#
def each(which = :all) # :yield: e
return to_enum :each, which unless block_given?
@@ -664,8 +675,8 @@ def index(*args)
# * row_range, col_range
#
# Matrix.diagonal(9, 5, -3).minor(0..1, 0..2)
- # => 9 0 0
- # 0 5 0
+ # # => 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
@@ -708,9 +719,9 @@ def minor(*param)
# Returns the submatrix obtained by deleting the specified row and column.
#
# Matrix.diagonal(9, 5, -3, 4).first_minor(1, 2)
- # => 9 0 0
- # 0 0 0
- # 0 0 4
+ # # => 9 0 0
+ # # 0 0 0
+ # # 0 0 4
#
def first_minor(row, column)
raise RuntimeError, "first_minor of empty matrix is not defined" if empty?
@@ -737,7 +748,7 @@ def first_minor(row, column)
# the first minor by (-1)**(row + column).
#
# Matrix.diagonal(9, 5, -3, 4).cofactor(1, 1)
- # => -108
+ # # => -108
#
def cofactor(row, column)
raise RuntimeError, "cofactor of empty matrix is not defined" if empty?
@@ -751,8 +762,8 @@ def cofactor(row, column)
# Returns the adjugate of the matrix.
#
# Matrix[ [7,6],[3,9] ].adjugate
- # => 9 -6
- # -3 7
+ # # => 9 -6
+ # # -3 7
#
def adjugate
Matrix.Raise ErrDimensionMismatch unless square?
@@ -765,10 +776,10 @@ def adjugate
# Returns the Laplace expansion along given row or column.
#
# Matrix[[7,6], [3,9]].laplace_expansion(column: 1)
- # => 45
+ # # => 45
#
# Matrix[[Vector[1, 0], Vector[0, 1]], [2, 3]].laplace_expansion(row: 0)
- # => Vector[3, -2]
+ # # => Vector[3, -2]
#
#
def laplace_expansion(row: nil, column: nil)
@@ -1015,8 +1026,8 @@ def hash
#
# Matrix multiplication.
# Matrix[[2,4], [6,8]] * Matrix.identity(2)
- # => 2 4
- # 6 8
+ # # => 2 4
+ # # 6 8
#
def *(m) # m is matrix or vector or number
case(m)
@@ -1048,8 +1059,8 @@ def *(m) # m is matrix or vector or number
#
# Matrix addition.
# Matrix.scalar(2,5) + Matrix[[1,0], [-4,7]]
- # => 6 0
- # -4 12
+ # # => 6 0
+ # # -4 12
#
def +(m)
case m
@@ -1075,8 +1086,8 @@ def +(m)
#
# Matrix subtraction.
# Matrix[[1,5], [4,2]] - Matrix[[9,3], [-4,1]]
- # => -8 2
- # 8 1
+ # # => -8 2
+ # # 8 1
#
def -(m)
case m
@@ -1102,8 +1113,8 @@ def -(m)
#
# Matrix division (multiplication by the inverse).
# Matrix[[7,6], [3,9]] / Matrix[[2,9], [3,1]]
- # => -7 1
- # -3 -6
+ # # => -7 1
+ # # -3 -6
#
def /(other)
case other
@@ -1122,8 +1133,8 @@ def /(other)
#
# Hadamard product
# Matrix[[1,2], [3,4]].hadamard_product(Matrix[[1,2], [3,2]])
- # => 1 4
- # 9 8
+ # # => 1 4
+ # # 9 8
#
def hadamard_product(m)
combine(m){|a, b| a * b}
@@ -1133,8 +1144,8 @@ def hadamard_product(m)
#
# Returns the inverse of the matrix.
# Matrix[[-1, -1], [0, -1]].inverse
- # => -1 1
- # 0 -1
+ # # => -1 1
+ # # 0 -1
#
def inverse
Matrix.Raise ErrDimensionMismatch unless square?
@@ -1192,8 +1203,8 @@ def inverse
# Non integer exponents will be handled by diagonalizing the matrix.
#
# Matrix[[7,6], [3,9]] ** 2
- # => 67 96
- # 48 99
+ # # => 67 96
+ # # 48 99
#
def **(other)
case other
@@ -1222,6 +1233,11 @@ def +@
self
end
+ # Unary matrix negation.
+ #
+ # -Matrix[[1,5], [4,2]]
+ # # => -1 -5
+ # # -4 -2
def -@
collect {|e| -e }
end
@@ -1238,7 +1254,7 @@ def -@
# Consider using exact types like Rational or BigDecimal instead.
#
# Matrix[[7,6], [3,9]].determinant
- # => 45
+ # # => 45
#
def determinant
Matrix.Raise ErrDimensionMismatch unless square?
@@ -1346,7 +1362,7 @@ def hstack(*matrices)
# Consider using exact types like Rational or BigDecimal instead.
#
# Matrix[[7,6], [3,9]].rank
- # => 2
+ # # => 2
#
def rank
# We currently use Bareiss' multistep integer-preserving gaussian elimination
@@ -1394,7 +1410,7 @@ def round(ndigits=0)
#
# Returns the trace (sum of diagonal elements) of the matrix.
# Matrix[[7,6], [3,9]].trace
- # => 16
+ # # => 16
#
def trace
Matrix.Raise ErrDimensionMismatch unless square?
@@ -1407,12 +1423,12 @@ def trace
#
# Returns the transpose of the matrix.
# Matrix[[1,2], [3,4], [5,6]]
- # => 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
+ # # => 1 3 5
+ # # 2 4 6
#
def transpose
return self.class.empty(column_count, 0) if row_count.zero?
@@ -1471,11 +1487,11 @@ def lup
#
# Returns the conjugate of the matrix.
# Matrix[[Complex(1,2), Complex(0,1), 0], [1, 2, 3]]
- # => 1+2i i 0
- # 1 2 3
+ # # => 1+2i i 0
+ # # 1 2 3
# Matrix[[Complex(1,2), Complex(0,1), 0], [1, 2, 3]].conjugate
- # => 1-2i -i 0
- # 1 2 3
+ # # => 1-2i -i 0
+ # # 1 2 3
#
def conjugate
collect(&:conjugate)
@@ -1485,11 +1501,11 @@ def conjugate
#
# 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
+ # # => 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
+ # # => 2i i 0
+ # # 0 0 0
#
def imaginary
collect(&:imaginary)
@@ -1499,11 +1515,11 @@ def imaginary
#
# Returns the real part of the matrix.
# Matrix[[Complex(1,2), Complex(0,1), 0], [1, 2, 3]]
- # => 1+2i i 0
- # 1 2 3
+ # # => 1+2i i 0
+ # # 1 2 3
# Matrix[[Complex(1,2), Complex(0,1), 0], [1, 2, 3]].real
- # => 1 0 0
- # 1 2 3
+ # # => 1 0 0
+ # # 1 2 3
#
def real
collect(&:real)
@@ -1513,7 +1529,7 @@ def real
# Returns an array containing matrices corresponding to the real and imaginary
# parts of the matrix
#
- # m.rect == [m.real, m.imag] # ==> true for all matrices m
+ # m.rect == [m.real, m.imag] # ==> true for all matrices m
#
def rect
[real, imag]
@@ -1574,7 +1590,7 @@ def to_a
# Deprecated.
#
- # Use map(&:to_f)
+ # Use map(&:to_f)
def elements_to_f
warn "Matrix#elements_to_f is deprecated, use map(&:to_f)", uplevel: 1
map(&:to_f)
@@ -1582,7 +1598,7 @@ def elements_to_f
# Deprecated.
#
- # Use map(&:to_i)
+ # Use map(&:to_i)
def elements_to_i
warn "Matrix#elements_to_i is deprecated, use map(&:to_i)", uplevel: 1
map(&:to_i)
@@ -1590,7 +1606,7 @@ def elements_to_i
# Deprecated.
#
- # Use map(&:to_r)
+ # Use map(&:to_r)
def elements_to_r
warn "Matrix#elements_to_r is deprecated, use map(&:to_r)", uplevel: 1
map(&:to_r)
@@ -1826,8 +1842,8 @@ def **(other)
# * #-@
#
# Vector functions:
-# * #inner_product(v), dot(v)
-# * #cross_product(v), cross(v)
+# * #inner_product(v), #dot(v)
+# * #cross_product(v), #cross(v)
# * #collect
# * #collect!
# * #magnitude
@@ -1892,7 +1908,7 @@ def Vector.basis(size:, index:)
#
# Return a zero vector.
#
- # Vector.zero(3) => Vector[0, 0, 0]
+ # Vector.zero(3) # => Vector[0, 0, 0]
#
def Vector.zero(size)
raise ArgumentError, "invalid size (#{size} for 0..)" if size < 0
@@ -2023,10 +2039,10 @@ def collect2(v) # :yield: e1, e2
# Returns +true+ iff all of vectors are linearly independent.
#
# Vector.independent?(Vector[1,0], Vector[0,1])
- # => true
+ # # => true
#
# Vector.independent?(Vector[1,2], Vector[2,4])
- # => false
+ # # => false
#
def Vector.independent?(*vs)
vs.each do |v|
@@ -2041,10 +2057,10 @@ def Vector.independent?(*vs)
# Returns +true+ iff all of vectors are linearly independent.
#
# Vector[1,0].independent?(Vector[0,1])
- # => true
+ # # => true
#
# Vector[1,2].independent?(Vector[2,4])
- # => false
+ # # => false
#
def independent?(*vs)
self.class.independent?(self, *vs)
@@ -2181,7 +2197,7 @@ def -@
#
# Returns the inner product of this vector with the other.
- # Vector[4,7].inner_product Vector[10,1] => 47
+ # Vector[4,7].inner_product Vector[10,1] # => 47
#
def inner_product(v)
Vector.Raise ErrDimensionMismatch if size != v.size
@@ -2196,7 +2212,7 @@ def inner_product(v)
#
# Returns the cross product of this vector with the others.
- # Vector[1, 0, 0].cross_product Vector[0, 1, 0] => Vector[0, 0, 1]
+ # Vector[1, 0, 0].cross_product Vector[0, 1, 0] # => Vector[0, 0, 1]
#
# It is generalized to other dimensions to return a vector perpendicular
# to the arguments.
@@ -2251,7 +2267,7 @@ def collect!(&block)
#
# Returns the modulus (Pythagorean distance) of the vector.
- # Vector[5,8,2].r => 9.643650761
+ # Vector[5,8,2].r # => 9.643650761
#
def magnitude
Math.sqrt(@elements.inject(0) {|v, e| v + e.abs2})
@@ -2274,7 +2290,7 @@ class ZeroVectorError < StandardError
# Returns a new vector with the same direction but with norm 1.
# v = Vector[5,8,2].normalize
# # => Vector[0.5184758473652127, 0.8295613557843402, 0.20739033894608505]
- # v.norm => 1.0
+ # v.norm # => 1.0
#
def normalize
n = magnitude