Sha256: 865a0b4601e41e9d03d8587d6b7c39cbf7378704aec1da6288cc4c4a0b0acb0c

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 KB

Contents

require 'symath/value'
require 'symath/operator'

module SyMath
  class Equation < Operator
    def initialize(arg1, arg2)
      super('=', [arg1, arg2])
    end

    def +(other)
      if other.is_a?(SyMath::Equation)
        return eq(self.args[0] + other.args[0], self.args[1] + other.args[1])
      else
        return eq(self.args[0] + other, self.args[1] + other)
      end
    end

    def -(other)
      if other.is_a?(SyMath::Equation)
        return eq(self.args[0] - other.args[0], self.args[1] - other.args[1])
      else
        return eq(self.args[0] - other, self.args[1] - other)
      end
    end

    def -@()
      return eq(-self.args[0], -self.args[1])
    end

    def *(other)
      if other.is_a?(SyMath::Equation)
        raise 'Cannot multiply two equations'
      else
        return eq(self.args[0] * other, self.args[1] * other)
      end
    end

    def /(other)
      if other.is_a?(SyMath::Equation)
        raise 'Cannot divide by equation'
      else
        return eq(self.args[0] / other, self.args[1] / other)
      end
    end

    def **(other)
      if other.is_a?(SyMath::Equation)
        raise 'Cannot use equation as exponent'
      else
        return eq(self.args[0]**other, self.args[1]**other)
      end
    end

    def to_s()
      return "#{@args[0]} = #{@args[1]}"
    end

    def to_latex()
      return @args[0].to_latex + ' = ' + @args[1].to_latex
    end
  end
end

# Convenience method
def eq(a, b)
  return SyMath::Equation.new(a, b)
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
symath-0.1.1 lib/symath/equation.rb
symath-0.1.0 lib/symath/equation.rb