Project

General

Profile

Actions

Bug #2652

closed

three bugs of Matrix::Scalar

Added by mame (Yusuke Endoh) over 15 years ago. Updated over 14 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 1.9.2dev (2009-11-16 trunk 25792) [i686-linux]
Backport:
[ruby-dev:40153]

Description

=begin
連投すみません。
[ruby-dev:40149] と全く同じ内容ですが、subject に [Bug:trunk] を
つけ忘れたので再送します。

=====

遠藤です。

Matrix::Scalar の定義に 3 つおかしいところを見つけました。
nodoc なクラスですが、どれも単純な問題だと思うので報告します。

○ WrongArgType が未定義
$ ./ruby -rmatrix -e 'Matrix[[0]].coerce(1).first + Matrix[[0]]'
/home/mame/work/ruby/lib/matrix.rb:979:in +': uninitialized constant Matrix::Scalar::WrongArgType (NameError) from -e:1:in '

ただの定義忘れだと思います。

○ Matrix#powered_by が未定義
$ ./ruby -rmatrix -e '2 ** Matrix[[1]]'
/home/mame/work/ruby/lib/matrix.rb:1032:in **': undefined method powered_by' for Matrix[[1]]:Matrix (NoMethodError)
from -e:1:in **' from -e:1:in '

そもそも 2 ** Matrix[[1]] に意味はないと思うので、Matrix::Scalar#** は
引数が Matrix だったら例外にすべきではないでしょうか。

○ when Numeric の後に when Scalar がある
Scalar は Numeric のサブクラスなので、when Scalar は実行されません。
もし実行されるようになったとしても、other.value という記述があり、
Scalar は value というメソッドを提供していないので NoMethodError になる
と思います。

--- a/lib/matrix.rb
+++ b/lib/matrix.rb
@@ -27,6 +27,7 @@ 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("WrongArgType", "wrong argument type %s (expected %s)")
    end

@@ -977,8 +978,6 @@ class Matrix
Scalar.new(@value + other)
when Vector, Matrix
Scalar.Raise WrongArgType, other.class, "Numeric or Scalar"

  •  when Scalar
    
  •    Scalar.new(@value + other.value)
    else
      x, y = other.coerce(self)
      x + y
    

@@ -991,8 +990,6 @@ class Matrix
Scalar.new(@value - other)
when Vector, Matrix
Scalar.Raise WrongArgType, other.class, "Numeric or Scalar"

  •  when Scalar
    
  •    Scalar.new(@value - other.value)
    else
      x, y = other.coerce(self)
      x - y
    

@@ -1029,10 +1026,8 @@ class Matrix
case other
when Numeric
Scalar.new(@value ** other)

  •  when Vector
    
  •    Scalar.Raise WrongArgType, other.class, "Numeric or Scalar or Matrix"
    
  •  when Matrix
    
  •    other.powered_by(self)
    
  •  when Vector, Matrix
    
  •    Scalar.Raise WrongArgType, other.class, "Numeric or Scalar"
    else
      x, y = other.coerce(self)
      x ** y
    

--
Yusuke ENDOH
=end

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0