From: keiju@... (=?ISO-2022-JP?B?GyRCQFBETTc9PHkbKEI=?=) Date: 2010-01-27T19:50:47+09:00 Subject: [ruby-dev:40184] Re: [Bug:trunk] three bugs of Matrix::Scalar けいじゅ@いしつかです. In [ruby-dev:40153] the message: "[ruby-dev:40153] [Bug:trunk] three bugs of Matrix::Scalar", on Jan/26 00:32(JST) Yusuke ENDOH writes: >連投すみません。 >[ruby-dev:40149] と全く同じ内容ですが、subject に [Bug:trunk] を >つけ忘れたので再送します。 あー. こっちにリプライしなくちゃ行けなかったんですね.... 一連のメイルをまとめると以下のパッチではいかがでしょう? 斎藤さんの [ruby-dev:40176] で > + Vector.Raise WrongArgType, x.class, "Numeric or Matrix" の話がありましたが, 元々 WrongArgType が定義されていない. また, Matrix 等では, ErrOperationNotDefined を使っていたのでこちらに統一しました. あと, 斎藤さんの メッセージが不親切との指摘を考慮しエラーメッセージも 少し修正しました. 異論がなければコミットしたいと思います. diff --git a/matrix.rb b/matrix.rb index 90e393c..f417323 100644 --- a/matrix.rb +++ b/matrix.rb @@ -26,7 +26,8 @@ module ExceptionForMatrix # :nodoc: def_exception("ErrDimensionMismatch", "\#{self.name} dimension mismatch") 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("ErrOperationNotImplement", "Sorry, Operation(%s) not implemented: %s op %s") end # @@ -497,7 +498,7 @@ class Matrix def +(m) case m when Numeric - Matrix.Raise ErrOperationNotDefined, "+" + Matrix.Raise ErrOperationNotDefined, "+", self.class, m.class when Vector m = Matrix.column_vector(m) when Matrix @@ -525,7 +526,7 @@ class Matrix def -(m) case m when Numeric - Matrix.Raise ErrOperationNotDefined, "-" + Matrix.Raise ErrOperationNotDefined, "-", self.class, m.class when Vector m = Matrix.column_vector(m) when Matrix @@ -649,9 +650,9 @@ class Matrix x *= x end elsif other.kind_of?(Float) || defined?(Rational) && other.kind_of?(Rational) - Matrix.Raise ErrOperationNotDefined, "**" + Matrix.Raise ErrOperationNotImplement, "**", self.class, other.class else - Matrix.Raise ErrOperationNotDefined, "**" + Matrix.Raise ErrOperationNotDefined, "**", self.class, other.class end end @@ -976,9 +977,7 @@ class Matrix when Numeric Scalar.new(@value + other) when Vector, Matrix - Scalar.Raise TypeError, other.class, "Numeric or Scalar" - when Scalar - Scalar.new(@value + other.value) + Scalar.Raise ErrOperationNotDefined, "+", @value.class, other.class else x, y = other.coerce(self) x + y @@ -990,9 +989,7 @@ class Matrix when Numeric Scalar.new(@value - other) when Vector, Matrix - Scalar.Raise WrongArgType, other.class, "Numeric or Scalar" - when Scalar - Scalar.new(@value - other.value) + Scalar.Raise ErrOperationNotDefined, "-", @value.class, other.class else x, y = other.coerce(self) x - y @@ -1016,7 +1013,7 @@ class Matrix when Numeric Scalar.new(@value / other) when Vector - Scalar.Raise WrongArgType, other.class, "Numeric or Scalar or Matrix" + Scalar.Raise ErrOperationNotDefined, "/", @value.class, other.class when Matrix self * other.inverse else @@ -1030,9 +1027,10 @@ class Matrix when Numeric Scalar.new(@value ** other) when Vector - Scalar.Raise WrongArgType, other.class, "Numeric or Scalar or Matrix" + Scalar.Raise ErrOperationNotDefined, "**", @value.class, other.class when Matrix - other.powered_by(self) + #other.powered_by(self) + Scalar.Raise ErrOperationNotImplement, "**", @value.class, other.class else x, y = other.coerce(self) x ** y @@ -1214,6 +1212,8 @@ class Vector Vector.elements(els, false) when Matrix Matrix.column_vector(self) * x + when Vector + Vector.Raise ErrOperationNotDefined, "*", self.class, x.class else s, x = x.coerce(self) s * x @@ -1258,6 +1258,22 @@ class Vector 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 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- #++ __ ---------------------------------------------------->> 石塚 圭樹 <<--- ---------------------------------->> e-mail: keiju@ishitsuka.com <<---