Project

General

Profile

Feature #10059 » using_define_method_for_Vector_arithmetic.patch

gogotanaka (Kazuki Tanaka), 07/18/2014 08:29 AM

View differences:

lib/matrix.rb
1707 1707
  end
1708 1708

  
1709 1709
  #
1710
  # Vector addition.
1710
  # Vector addition and subtraction.
1711 1711
  #
1712
  def +(v)
1713
    case v
1714
    when Vector
1715
      Vector.Raise ErrDimensionMismatch if size != v.size
1716
      els = collect2(v) {|v1, v2|
1717
        v1 + v2
1718
      }
1719
      self.class.elements(els, false)
1720
    when Matrix
1721
      Matrix.column_vector(self) + v
1722
    else
1723
      apply_through_coercion(v, __method__)
1724
    end
1725
  end
1726

  
1727
  #
1728
  # Vector subtraction.
1729
  #
1730
  def -(v)
1731
    case v
1732
    when Vector
1733
      Vector.Raise ErrDimensionMismatch if size != v.size
1734
      els = collect2(v) {|v1, v2|
1735
        v1 - v2
1736
      }
1737
      self.class.elements(els, false)
1738
    when Matrix
1739
      Matrix.column_vector(self) - v
1740
    else
1741
      apply_through_coercion(v, __method__)
1712
  %i(+ -).each do |op|
1713
    define_method(op) do |v|
1714
      case v
1715
      when Vector
1716
        Vector.Raise ErrDimensionMismatch unless size == v.size
1717
        els = collect2(v) {|v1, v2|
1718
          v1.send(op, v2)
1719
        }
1720
        self.class.elements(els, false)
1721
      when Matrix
1722
        Matrix.column_vector(self).send(op, v)
1723
      else
1724
        apply_through_coercion(v, op)
1725
      end
1742 1726
    end
1743 1727
  end
1744 1728