Module: Mathmas::Basic

Included in:
Expression, Function, Number, Variable
Defined in:
lib/mathmas/core/basic.rb

Instance Method Summary collapse

Instance Method Details

#*(arg) ⇒ Object



14
15
16
17
# File 'lib/mathmas/core/basic.rb', line 14

def *(arg)
  arg = (arg.is_a?(Numeric) ? Number.new(arg) : arg)
  Multiply.new(self, arg)
end

#**(arg) ⇒ Object



28
29
30
31
# File 'lib/mathmas/core/basic.rb', line 28

def **(arg)
  arg = (arg.is_a?(Numeric) ? Number.new(arg) : arg)
  Power.new(self, arg)
end

#+(arg) ⇒ Object

TODO: delete the line arg=( ? : )



4
5
6
7
# File 'lib/mathmas/core/basic.rb', line 4

def +(arg)
  arg = (arg.is_a?(Numeric) ? Number.new(arg) : arg)
  Plus.new(self, arg)
end

#-(arg) ⇒ Object



9
10
11
12
# File 'lib/mathmas/core/basic.rb', line 9

def -(arg)
  arg = (arg.is_a?(Numeric) ? Number.new(arg) : arg)
  Plus.new(self, Multiply.new(Number.new(-1), arg))
end

#/(arg) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/mathmas/core/basic.rb', line 19

def /(arg)
  arg = (arg.is_a?(Numeric) ? Number.new(arg) : arg)
  if self.is_a?(Number) && self.num == 1
    Power.new(arg, Number.new(-1))
  else
    Multiply.new(self, Power.new(arg, Number.new(-1)))
  end
end

#coerce(other) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/mathmas/core/basic.rb', line 33

def coerce(other)
  if other.is_a? Numeric
    return Mathmas::Number.new(other), self
  else
    raise "There is no rule for handling #{other.to_s}."
  end
end

#to_irubyObject



41
42
43
44
45
46
# File 'lib/mathmas/core/basic.rb', line 41

def to_iruby
  # Dirty Hack for IRuby. I hope IRuby will fix the problem in the future.
  # ["text/latex", self.to_tex] not work.
  html = "<script type=\"math/tex; mode=display\">#{ self.to_tex }</script>"
  ['text/html', html]
end