Module: Hilbert::Iq

Defined in:
lib/hilbert/iq.rb

Class Method Summary collapse

Class Method Details

.execute(code) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/hilbert/iq.rb', line 14

def self.execute(code)
  ruby_code = Hilbert.to_ruby.compile(code.encode('utf-8'))
  ruby_obj = eval(ruby_code)

  optimize_output(ruby_obj).encode('utf-8')
rescue SyntaxError
  # TODO: emergency
  case ruby_code
  when /(\d)+(\w)/
    execute("#{$1} * #{$2}")
  end
end

.optimize_output(ruby_obj) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hilbert/iq.rb', line 27

def self.optimize_output(ruby_obj)

  case ruby_obj
  when Matrix, Vector, Dydx::Algebra::Formula
    ruby_obj.to_q
  when Numeric
    # TODO: I know what you wanna say..
    if    ruby_obj > 10000.0            then 'oo'
    elsif ruby_obj < -10000.0           then '-oo'
    elsif ruby_obj.abs < 1.0/10**4        then '0.0'
    else                                     ruby_obj.to_s.equalize!
    end
  else
    str = ruby_obj.to_s
    str.equalize!
  end
end