Project

General

Profile

Feature #10059 » using_define_method_for_Scalar_arithmetic.patch

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

View differences:

lib/matrix.rb
1457 1457
    end
1458 1458

  
1459 1459
    # ARITHMETIC
1460
    def +(other)
1461
      case other
1462
      when Numeric
1463
        Scalar.new(@value + other)
1464
      when Vector, Matrix
1465
        Scalar.Raise ErrOperationNotDefined, "+", @value.class, other.class
1466
      else
1467
        apply_through_coercion(other, __method__)
1468
      end
1469
    end
1470

  
1471
    def -(other)
1472
      case other
1473
      when Numeric
1474
        Scalar.new(@value - other)
1475
      when Vector, Matrix
1476
        Scalar.Raise ErrOperationNotDefined, "-", @value.class, other.class
1477
      else
1478
        apply_through_coercion(other, __method__)
1460
    %i(+ -).each do |op|
1461
      define_method(op) do |other|
1462
        case other
1463
        when Numeric
1464
          Scalar.new(@value.send(op, other))
1465
        when Vector, Matrix
1466
          Scalar.Raise ErrOperationNotDefined, op, @value.class, other.class
1467
        else
1468
          apply_through_coercion(other, op)
1469
        end
1479 1470
      end
1480 1471
    end
1481 1472