Sha256: 1dcccf0564c82253287e6b637c50081e0f16b4aa2b6b330f2d33ad7d176a546c

Contents?: true

Size: 1.03 KB

Versions: 7

Compression:

Stored size: 1.03 KB

Contents

require 'dydx'
include Dydx

module Hilbert
  module Iq
    class Dydx::Algebra::Formula
      # FIX:
      def to_q
        str = to_s.gsub(/\*\*/, '^').rm(' * ')
        str.equalize!
      end
    end

    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

    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
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
hilbert-0.0.2700420 lib/hilbert/iq.rb
hilbert-0.0.2700410 lib/hilbert/iq.rb
hilbert-0.0.2700400 lib/hilbert/iq.rb
hilbert-0.0.2700320 lib/hilbert/iq.rb
hilbert-0.0.2700300 lib/hilbert/iq.rb
hilbert-0.0.2700210 lib/hilbert/iq.rb
hilbert-0.0.2700110 lib/hilbert/iq.rb