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

* lib/matrix.rb: add exception Matrix::ErrOperationNotImplemented

[ruby-dev:40149].
 * lib/matrix.rb: change message of exception
   Matrix::ErrOperationNotDefined [ruby-dev:40150], [ruby-dev:40176].
 * lib/matrix.rb: add method Vector#/ [ruby-dev:40151].
 * lib/matrix.rb(Matrix::Scalar#+,-,/): delete meaningless when
   switch. [ruby-dev:40149]



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26448 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
keiju 2010-01-27 14:28:47 +00:00
parent 606f59bbff
commit afc9fce01f
2 changed files with 40 additions and 14 deletions

View file

@ -1,3 +1,13 @@
Wed Jan 27 23:27:54 2010 Keiju Ishitsuka <keiju@emperor2.pendome>
* lib/matrix.rb: add exception Matrix::ErrOperationNotImplemented
[ruby-dev:40149].
* lib/matrix.rb: change message of exception
Matrix::ErrOperationNotDefined [ruby-dev:40150], [ruby-dev:40176].
* lib/matrix.rb: add method Vector#/ [ruby-dev:40151].
* lib/matrix.rb(Matrix::Scalar#+,-,/): delete meaningless when
switch. [ruby-dev:40149]
Wed Jan 27 23:22:54 2010 Kazuhiro NISHIYAMA <zn@mbf.nifty.com> Wed Jan 27 23:22:54 2010 Kazuhiro NISHIYAMA <zn@mbf.nifty.com>
* vm_dump.c (bugreport_backtrace): trivial change. * vm_dump.c (bugreport_backtrace): trivial change.

View file

@ -26,7 +26,8 @@ module ExceptionForMatrix # :nodoc:
def_exception("ErrDimensionMismatch", "\#{self.name} dimension mismatch") def_exception("ErrDimensionMismatch", "\#{self.name} dimension mismatch")
def_exception("ErrNotRegular", "Not Regular Matrix") def_exception("ErrNotRegular", "Not Regular Matrix")
def_exception("ErrOperationNotDefined", "This operation(%s) can\\'t defined") def_exception("ErrOperationNotDefined", "Operation(%s) can\\'t be defined: %s op %s")
def_exception("ErrOperationNotImplemented", "Sorry, Operation(%s) not implemented: %s op %s")
end end
# #
@ -497,7 +498,7 @@ class Matrix
def +(m) def +(m)
case m case m
when Numeric when Numeric
Matrix.Raise ErrOperationNotDefined, "+" Matrix.Raise ErrOperationNotDefined, "+", self.class, m.class
when Vector when Vector
m = Matrix.column_vector(m) m = Matrix.column_vector(m)
when Matrix when Matrix
@ -525,7 +526,7 @@ class Matrix
def -(m) def -(m)
case m case m
when Numeric when Numeric
Matrix.Raise ErrOperationNotDefined, "-" Matrix.Raise ErrOperationNotDefined, "-", self.class, m.class
when Vector when Vector
m = Matrix.column_vector(m) m = Matrix.column_vector(m)
when Matrix when Matrix
@ -649,9 +650,9 @@ class Matrix
x *= x x *= x
end end
elsif other.kind_of?(Float) || defined?(Rational) && other.kind_of?(Rational) elsif other.kind_of?(Float) || defined?(Rational) && other.kind_of?(Rational)
Matrix.Raise ErrOperationNotDefined, "**" Matrix.Raise ErrOperationNotImplemented, "**", self.class, other.class
else else
Matrix.Raise ErrOperationNotDefined, "**" Matrix.Raise ErrOperationNotDefined, "**", self.class, other.class
end end
end end
@ -976,9 +977,7 @@ class Matrix
when Numeric when Numeric
Scalar.new(@value + other) Scalar.new(@value + other)
when Vector, Matrix when Vector, Matrix
Scalar.Raise WrongArgType, other.class, "Numeric or Scalar" Scalar.Raise ErrOperationNotDefined, "+", @value.class, other.class
when Scalar
Scalar.new(@value + other.value)
else else
x, y = other.coerce(self) x, y = other.coerce(self)
x + y x + y
@ -990,9 +989,7 @@ class Matrix
when Numeric when Numeric
Scalar.new(@value - other) Scalar.new(@value - other)
when Vector, Matrix when Vector, Matrix
Scalar.Raise WrongArgType, other.class, "Numeric or Scalar" Scalar.Raise ErrOperationNotDefined, "-", @value.class, other.class
when Scalar
Scalar.new(@value - other.value)
else else
x, y = other.coerce(self) x, y = other.coerce(self)
x - y x - y
@ -1016,7 +1013,7 @@ class Matrix
when Numeric when Numeric
Scalar.new(@value / other) Scalar.new(@value / other)
when Vector when Vector
Scalar.Raise WrongArgType, other.class, "Numeric or Scalar or Matrix" Scalar.Raise ErrOperationNotDefined, "/", @value.class, other.class
when Matrix when Matrix
self * other.inverse self * other.inverse
else else
@ -1030,9 +1027,10 @@ class Matrix
when Numeric when Numeric
Scalar.new(@value ** other) Scalar.new(@value ** other)
when Vector when Vector
Scalar.Raise WrongArgType, other.class, "Numeric or Scalar or Matrix" Scalar.Raise ErrOperationNotDefined, "**", @value.class, other.class
when Matrix when Matrix
other.powered_by(self) #other.powered_by(self)
Scalar.Raise ErrOperationNotImplemented, "**", @value.class, other.class
else else
x, y = other.coerce(self) x, y = other.coerce(self)
x ** y x ** y
@ -1214,6 +1212,8 @@ class Vector
Vector.elements(els, false) Vector.elements(els, false)
when Matrix when Matrix
Matrix.column_vector(self) * x Matrix.column_vector(self) * x
when Vector
Vector.Raise ErrOperationNotDefined, "*", self.class, x.class
else else
s, x = x.coerce(self) s, x = x.coerce(self)
s * x s * x
@ -1258,6 +1258,22 @@ class Vector
end end
end end
#
# Vector division.
#
def /(x)
case x
when Numeric
els = @elements.collect{|e| e / x}
Vector.elements(els, false)
when Matrix, Vector
Vector.Raise ErrOperationNotDefined, "/", self.class, x.class
else
s, x = x.coerce(self)
s / x
end
end
#-- #--
# VECTOR FUNCTIONS -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- # VECTOR FUNCTIONS -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#++ #++