mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/matrix.rb: Handle coercion errors by raising TypeErrors [ruby-core:26736]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@27311 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
5469cf0f3a
commit
e0cfb13c32
2 changed files with 36 additions and 27 deletions
|
@ -1,3 +1,8 @@
|
||||||
|
Mon Apr 12 03:30:29 2010 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
|
||||||
|
|
||||||
|
* lib/matrix.rb: Handle coercion errors by raising TypeErrors
|
||||||
|
[ruby-core:26736]
|
||||||
|
|
||||||
Mon Apr 12 00:51:21 2010 Kazuhiro NISHIYAMA <zn@mbf.nifty.com>
|
Mon Apr 12 00:51:21 2010 Kazuhiro NISHIYAMA <zn@mbf.nifty.com>
|
||||||
|
|
||||||
* io.c (rb_io_getline_fast, rb_io_getline_1): fix ARGF.lineno
|
* io.c (rb_io_getline_fast, rb_io_getline_1): fix ARGF.lineno
|
||||||
|
|
|
@ -541,8 +541,7 @@ class Matrix
|
||||||
}
|
}
|
||||||
return new_matrix rows, m.column_size
|
return new_matrix rows, m.column_size
|
||||||
else
|
else
|
||||||
x, y = m.coerce(self)
|
return apply_through_coercion(m, __method__)
|
||||||
return x * y
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -560,8 +559,7 @@ class Matrix
|
||||||
m = Matrix.column_vector(m)
|
m = Matrix.column_vector(m)
|
||||||
when Matrix
|
when Matrix
|
||||||
else
|
else
|
||||||
x, y = m.coerce(self)
|
return apply_through_coercion(m, __method__)
|
||||||
return x + y
|
|
||||||
end
|
end
|
||||||
|
|
||||||
Matrix.Raise ErrDimensionMismatch unless row_size == m.row_size and column_size == m.column_size
|
Matrix.Raise ErrDimensionMismatch unless row_size == m.row_size and column_size == m.column_size
|
||||||
|
@ -588,8 +586,7 @@ class Matrix
|
||||||
m = Matrix.column_vector(m)
|
m = Matrix.column_vector(m)
|
||||||
when Matrix
|
when Matrix
|
||||||
else
|
else
|
||||||
x, y = m.coerce(self)
|
return apply_through_coercion(m, __method__)
|
||||||
return x - y
|
|
||||||
end
|
end
|
||||||
|
|
||||||
Matrix.Raise ErrDimensionMismatch unless row_size == m.row_size and column_size == m.column_size
|
Matrix.Raise ErrDimensionMismatch unless row_size == m.row_size and column_size == m.column_size
|
||||||
|
@ -620,8 +617,7 @@ class Matrix
|
||||||
when Matrix
|
when Matrix
|
||||||
return self * other.inverse
|
return self * other.inverse
|
||||||
else
|
else
|
||||||
x, y = other.coerce(self)
|
return apply_through_coercion(other, __method__)
|
||||||
return x / y
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1073,10 +1069,26 @@ class Matrix
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Private helper module
|
||||||
|
|
||||||
|
module CoercionHelper
|
||||||
|
def apply_through_coercion(obj, oper)
|
||||||
|
coercion = obj.coerce(self)
|
||||||
|
raise TypeError unless coercion.is_a?(Array) && coercion.length == 2
|
||||||
|
coercion[0].public_send(oper, coercion[1])
|
||||||
|
rescue
|
||||||
|
raise TypeError, "#{obj.inspect} can't be coerced into #{self.class}"
|
||||||
|
end
|
||||||
|
private :apply_through_coercion
|
||||||
|
end
|
||||||
|
|
||||||
|
include CoercionHelper
|
||||||
|
|
||||||
# Private CLASS
|
# Private CLASS
|
||||||
|
|
||||||
class Scalar < Numeric # :nodoc:
|
class Scalar < Numeric # :nodoc:
|
||||||
include ExceptionForMatrix
|
include ExceptionForMatrix
|
||||||
|
include CoercionHelper
|
||||||
|
|
||||||
def initialize(value)
|
def initialize(value)
|
||||||
@value = value
|
@value = value
|
||||||
|
@ -1090,8 +1102,7 @@ class Matrix
|
||||||
when Vector, Matrix
|
when Vector, Matrix
|
||||||
Scalar.Raise ErrOperationNotDefined, "+", @value.class, other.class
|
Scalar.Raise ErrOperationNotDefined, "+", @value.class, other.class
|
||||||
else
|
else
|
||||||
x, y = other.coerce(self)
|
apply_through_coercion(other, __method__)
|
||||||
x + y
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1102,8 +1113,7 @@ class Matrix
|
||||||
when Vector, Matrix
|
when Vector, Matrix
|
||||||
Scalar.Raise ErrOperationNotDefined, "-", @value.class, other.class
|
Scalar.Raise ErrOperationNotDefined, "-", @value.class, other.class
|
||||||
else
|
else
|
||||||
x, y = other.coerce(self)
|
apply_through_coercion(other, __method__)
|
||||||
x - y
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1114,8 +1124,7 @@ class Matrix
|
||||||
when Vector, Matrix
|
when Vector, Matrix
|
||||||
other.collect{|e| @value * e}
|
other.collect{|e| @value * e}
|
||||||
else
|
else
|
||||||
x, y = other.coerce(self)
|
apply_through_coercion(other, __method__)
|
||||||
x * y
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1128,8 +1137,7 @@ class Matrix
|
||||||
when Matrix
|
when Matrix
|
||||||
self * other.inverse
|
self * other.inverse
|
||||||
else
|
else
|
||||||
x, y = other.coerce(self)
|
apply_through_coercion(other, __method__)
|
||||||
x.quo(y)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1143,11 +1151,11 @@ class Matrix
|
||||||
#other.powered_by(self)
|
#other.powered_by(self)
|
||||||
Scalar.Raise ErrOperationNotImplemented, "**", @value.class, other.class
|
Scalar.Raise ErrOperationNotImplemented, "**", @value.class, other.class
|
||||||
else
|
else
|
||||||
x, y = other.coerce(self)
|
apply_through_coercion(other, __method__)
|
||||||
x ** y
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -1193,7 +1201,7 @@ end
|
||||||
class Vector
|
class Vector
|
||||||
include ExceptionForMatrix
|
include ExceptionForMatrix
|
||||||
include Enumerable
|
include Enumerable
|
||||||
|
include Matrix::CoercionHelper
|
||||||
#INSTANCE CREATION
|
#INSTANCE CREATION
|
||||||
|
|
||||||
private_class_method :new
|
private_class_method :new
|
||||||
|
@ -1335,8 +1343,7 @@ class Vector
|
||||||
when Vector
|
when Vector
|
||||||
Vector.Raise ErrOperationNotDefined, "*", self.class, x.class
|
Vector.Raise ErrOperationNotDefined, "*", self.class, x.class
|
||||||
else
|
else
|
||||||
s, x = x.coerce(self)
|
apply_through_coercion(x, __method__)
|
||||||
s * x
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1354,8 +1361,7 @@ class Vector
|
||||||
when Matrix
|
when Matrix
|
||||||
Matrix.column_vector(self) + v
|
Matrix.column_vector(self) + v
|
||||||
else
|
else
|
||||||
s, x = v.coerce(self)
|
apply_through_coercion(v, __method__)
|
||||||
s + x
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1373,8 +1379,7 @@ class Vector
|
||||||
when Matrix
|
when Matrix
|
||||||
Matrix.column_vector(self) - v
|
Matrix.column_vector(self) - v
|
||||||
else
|
else
|
||||||
s, x = v.coerce(self)
|
apply_through_coercion(v, __method__)
|
||||||
s - x
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1389,8 +1394,7 @@ class Vector
|
||||||
when Matrix, Vector
|
when Matrix, Vector
|
||||||
Vector.Raise ErrOperationNotDefined, "/", self.class, x.class
|
Vector.Raise ErrOperationNotDefined, "/", self.class, x.class
|
||||||
else
|
else
|
||||||
s, x = x.coerce(self)
|
apply_through_coercion(x, __method__)
|
||||||
s / x
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue