Feature #10059 » using_define_method_for_Vector_arithmetic.patch
lib/matrix.rb | ||
---|---|---|
end
|
||
#
|
||
# Vector addition.
|
||
# Vector addition and subtraction.
|
||
#
|
||
def +(v)
|
||
case v
|
||
when Vector
|
||
Vector.Raise ErrDimensionMismatch if size != v.size
|
||
els = collect2(v) {|v1, v2|
|
||
v1 + v2
|
||
}
|
||
self.class.elements(els, false)
|
||
when Matrix
|
||
Matrix.column_vector(self) + v
|
||
else
|
||
apply_through_coercion(v, __method__)
|
||
end
|
||
end
|
||
#
|
||
# Vector subtraction.
|
||
#
|
||
def -(v)
|
||
case v
|
||
when Vector
|
||
Vector.Raise ErrDimensionMismatch if size != v.size
|
||
els = collect2(v) {|v1, v2|
|
||
v1 - v2
|
||
}
|
||
self.class.elements(els, false)
|
||
when Matrix
|
||
Matrix.column_vector(self) - v
|
||
else
|
||
apply_through_coercion(v, __method__)
|
||
%i(+ -).each do |op|
|
||
define_method(op) do |v|
|
||
case v
|
||
when Vector
|
||
Vector.Raise ErrDimensionMismatch unless size == v.size
|
||
els = collect2(v) {|v1, v2|
|
||
v1.send(op, v2)
|
||
}
|
||
self.class.elements(els, false)
|
||
when Matrix
|
||
Matrix.column_vector(self).send(op, v)
|
||
else
|
||
apply_through_coercion(v, op)
|
||
end
|
||
end
|
||
end
|
||