class Mathmas::Expression

Currently Mathmas have 3 types of expression, Plus, Multiply and Power. All calcs including dividing are expressed with these three.

@example

1/(x + y)

Multiply(Number(1), Power(Plus(Symbol(x), Symbol(y), -1))

Public Class Methods

new(*args) click to toggle source
# File lib/mathmas/core/expression.rb, line 13
def initialize(*args)
  @args = args
end

Public Instance Methods

exec(args) click to toggle source

@example

(1/x).exec(x: 3) #-> 1/3
(1/(x*y)).exec(x: 3) #-> 1/(3*x)
# File lib/mathmas/core/expression.rb, line 20
def exec(args)
  arr = @args.map do |arg|
    if arg.is_a?(Variable)
      unless args.keys.index(arg.symbol).nil?
        next args[arg.symbol]
      else
        next arg
      end
    elsif arg.is_a?(Expression)
      next arg.exec(args)
    elsif arg.is_a?(Number)
      next arg.num
    else
      raise "Invailed Arguments"
    end
  end
  arr
end