diff --git a/lib/matrix.rb b/lib/matrix.rb index 66107ff..379dee1 100644 --- a/lib/matrix.rb +++ b/lib/matrix.rb @@ -1707,38 +1707,22 @@ class Vector 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