1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* lib/matrix.rb: Deal with subclasses of Matrix [redmine #5307]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33246 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
marcandre 2011-09-10 20:23:36 +00:00
parent 39ec2f6864
commit 506b253856
2 changed files with 27 additions and 23 deletions

View file

@ -1,3 +1,7 @@
Sun Sep 11 05:23:14 2011 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
* lib/matrix.rb: Deal with subclasses of Matrix [redmine #5307]
Sat Sep 10 13:38:20 2011 Ayumu AIZAWA <ayumu.aizawa@gmail.com> Sat Sep 10 13:38:20 2011 Ayumu AIZAWA <ayumu.aizawa@gmail.com>
* dir.c (dir_s_aref): * dir.c (dir_s_aref):

View file

@ -138,7 +138,7 @@ class Matrix
# -1 66 # -1 66
# #
def Matrix.[](*rows) def Matrix.[](*rows)
Matrix.rows(rows, false) rows(rows, false)
end end
# #
@ -168,7 +168,7 @@ class Matrix
# 93 66 # 93 66
# #
def Matrix.columns(columns) def Matrix.columns(columns)
Matrix.rows(columns, false).transpose rows(columns, false).transpose
end end
# #
@ -220,7 +220,7 @@ class Matrix
# 0 5 # 0 5
# #
def Matrix.scalar(n, value) def Matrix.scalar(n, value)
Matrix.diagonal(*Array.new(n, value)) diagonal(*Array.new(n, value))
end end
# #
@ -230,7 +230,7 @@ class Matrix
# 0 1 # 0 1
# #
def Matrix.identity(n) def Matrix.identity(n)
Matrix.scalar(n, 1) scalar(n, 1)
end end
class << Matrix class << Matrix
alias unit identity alias unit identity
@ -304,7 +304,7 @@ class Matrix
end end
def new_matrix(rows, column_size = rows[0].size) # :nodoc: def new_matrix(rows, column_size = rows[0].size) # :nodoc:
Matrix.send(:new, rows, column_size) # bypass privacy of Matrix.new self.class.send(:new, rows, column_size) # bypass privacy of Matrix.new
end end
private :new_matrix private :new_matrix
@ -808,7 +808,7 @@ class Matrix
} }
return new_matrix rows, column_size return new_matrix rows, column_size
when Vector when Vector
m = Matrix.column_vector(m) m = self.class.column_vector(m)
r = self * m r = self * m
return r.column(0) return r.column(0)
when Matrix when Matrix
@ -838,7 +838,7 @@ class Matrix
when Numeric when Numeric
Matrix.Raise ErrOperationNotDefined, "+", self.class, m.class Matrix.Raise ErrOperationNotDefined, "+", self.class, m.class
when Vector when Vector
m = Matrix.column_vector(m) m = self.class.column_vector(m)
when Matrix when Matrix
else else
return apply_through_coercion(m, __method__) return apply_through_coercion(m, __method__)
@ -865,7 +865,7 @@ class Matrix
when Numeric when Numeric
Matrix.Raise ErrOperationNotDefined, "-", self.class, m.class Matrix.Raise ErrOperationNotDefined, "-", self.class, m.class
when Vector when Vector
m = Matrix.column_vector(m) m = self.class.column_vector(m)
when Matrix when Matrix
else else
return apply_through_coercion(m, __method__) return apply_through_coercion(m, __method__)
@ -909,7 +909,7 @@ class Matrix
# #
def inverse def inverse
Matrix.Raise ErrDimensionMismatch unless square? Matrix.Raise ErrDimensionMismatch unless square?
Matrix.I(row_size).send(:inverse_from, self) self.class.I(row_size).send(:inverse_from, self)
end end
alias inv inverse alias inv inverse
@ -973,7 +973,7 @@ class Matrix
x = self x = self
if other <= 0 if other <= 0
x = self.inverse x = self.inverse
return Matrix.identity(self.column_size) if other == 0 return self.class.identity(self.column_size) if other == 0
other = -other other = -other
end end
z = nil z = nil
@ -984,7 +984,7 @@ class Matrix
end end
when Numeric when Numeric
v, d, v_inv = eigensystem v, d, v_inv = eigensystem
v * Matrix.diagonal(*d.each(:diagonal).map{|e| e ** other}) * v_inv v * self.class.diagonal(*d.each(:diagonal).map{|e| e ** other}) * v_inv
else else
Matrix.Raise ErrOperationNotDefined, "**", self.class, other.class Matrix.Raise ErrOperationNotDefined, "**", self.class, other.class
end end
@ -1168,7 +1168,7 @@ class Matrix
# 2 4 6 # 2 4 6
# #
def transpose def transpose
return Matrix.empty(column_size, 0) if row_size.zero? return self.class.empty(column_size, 0) if row_size.zero?
new_matrix @rows.transpose, row_size new_matrix @rows.transpose, row_size
end end
alias t transpose alias t transpose
@ -1330,9 +1330,9 @@ class Matrix
# #
def to_s def to_s
if empty? if empty?
"Matrix.empty(#{row_size}, #{column_size})" "#{self.class}.empty(#{row_size}, #{column_size})"
else else
"Matrix[" + @rows.collect{|row| "#{self.class}[" + @rows.collect{|row|
"[" + row.collect{|e| e.to_s}.join(", ") + "]" "[" + row.collect{|e| e.to_s}.join(", ") + "]"
}.join(", ")+"]" }.join(", ")+"]"
end end
@ -1343,9 +1343,9 @@ class Matrix
# #
def inspect def inspect
if empty? if empty?
"Matrix.empty(#{row_size}, #{column_size})" "#{self.class}.empty(#{row_size}, #{column_size})"
else else
"Matrix#{@rows.inspect}" "#{self.class}#{@rows.inspect}"
end end
end end
@ -1654,7 +1654,7 @@ class Vector
# Return a copy of the vector. # Return a copy of the vector.
# #
def clone def clone
Vector.elements(@elements) self.class.elements(@elements)
end end
# #
@ -1675,7 +1675,7 @@ class Vector
case x case x
when Numeric when Numeric
els = @elements.collect{|e| e * x} els = @elements.collect{|e| e * x}
Vector.elements(els, false) self.class.elements(els, false)
when Matrix when Matrix
Matrix.column_vector(self) * x Matrix.column_vector(self) * x
when Vector when Vector
@ -1695,7 +1695,7 @@ class Vector
els = collect2(v) {|v1, v2| els = collect2(v) {|v1, v2|
v1 + v2 v1 + v2
} }
Vector.elements(els, false) self.class.elements(els, false)
when Matrix when Matrix
Matrix.column_vector(self) + v Matrix.column_vector(self) + v
else else
@ -1713,7 +1713,7 @@ class Vector
els = collect2(v) {|v1, v2| els = collect2(v) {|v1, v2|
v1 - v2 v1 - v2
} }
Vector.elements(els, false) self.class.elements(els, false)
when Matrix when Matrix
Matrix.column_vector(self) - v Matrix.column_vector(self) - v
else else
@ -1728,7 +1728,7 @@ class Vector
case x case x
when Numeric when Numeric
els = @elements.collect{|e| e / x} els = @elements.collect{|e| e / x}
Vector.elements(els, false) self.class.elements(els, false)
when Matrix, Vector when Matrix, Vector
Vector.Raise ErrOperationNotDefined, "/", self.class, x.class Vector.Raise ErrOperationNotDefined, "/", self.class, x.class
else else
@ -1760,7 +1760,7 @@ class Vector
def collect(&block) # :yield: e def collect(&block) # :yield: e
return to_enum(:collect) unless block_given? return to_enum(:collect) unless block_given?
els = @elements.collect(&block) els = @elements.collect(&block)
Vector.elements(els, false) self.class.elements(els, false)
end end
alias map collect alias map collect
@ -1780,7 +1780,7 @@ class Vector
def map2(v, &block) # :yield: e1, e2 def map2(v, &block) # :yield: e1, e2
return to_enum(:map2, v) unless block_given? return to_enum(:map2, v) unless block_given?
els = collect2(v, &block) els = collect2(v, &block)
Vector.elements(els, false) self.class.elements(els, false)
end end
class ZeroVectorError < StandardError class ZeroVectorError < StandardError