Class: Mathmas::Expression

Inherits:
Object
  • Object
show all
Includes:
Basic
Defined in:
lib/mathmas/core/expression.rb

Overview

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

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

Examples:

1/(x + y)

Direct Known Subclasses

Multiply, Plus, Power

Instance Method Summary collapse

Methods included from Basic

#*, #**, #+, #-, #/, #coerce, #to_iruby

Constructor Details

#initialize(*args) ⇒ Expression

Returns a new instance of Expression.



13
14
15
# File 'lib/mathmas/core/expression.rb', line 13

def initialize(*args)
  @args = args
end

Instance Method Details

#exec(args) ⇒ Object

Examples:

(1/x).exec(x: 3) #-> 1/3
(1/(x*y)).exec(x: 3) #-> 1/(3*x)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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