Sha256: b5a32f054b5d2c2eaef5b4acf6887198bfe05cf14cd7f2fa075b88b4e6d1b8d0

Contents?: true

Size: 1.48 KB

Versions: 5

Compression:

Stored size: 1.48 KB

Contents

module Unitwise
  class Function
    include Math
    class << self
      def all
        @all ||= defaults
      end

      def defaults
        [ ["cel",   ->(x){ x - 273.15},           ->(x){ x + 273.15}          ],
          ["degf",  ->(x){9.0 * x / 5.0 - 459.67},->(x){ 5.0/9 * (x + 459.67)}],
          ["hpX",   ->(x){ -log10(x) },           ->(x){ 10 ** -x }           ],
          ["hpC",   ->(x){ -log(x) / log(100) },  ->(x){ 100 ** -x }          ],
          ["tan100",->(x){ 100 * tan(x) },        ->(x){ atan(x / 100) }      ],
          ["ph",    ->(x){ -log10(x) },           ->(x){ 10 ** -x }           ],
          ["ld",    ->(x){ log2(x) },             ->(x){ 2 ** -x }            ],
          ["ln",    ->(x){ log(x) },              ->(x){ Math::E ** x }       ],
          ["lg",    ->(x){ log10(x) },            ->(x){ 10 ** x }            ],
          ["2lg",   ->(x){ 2 * log10(x) },         ->(x){ (10 ** x) / 2 }     ]
        ].map {|args| new(*args) }
      end

      def add(*args)
        new_instance = self.new(*args)
        all << new_instance
        new_instance
      end

      def find(name)
        all.find{|fp| fp.name == name}
      end
    end

    attr_reader :name, :direct, :inverse

    def initialize(name, direct, inverse)
      @name = name
      @direct = direct
      @inverse = inverse
    end

    def functional(x, direction=1)
      if direction == 1
        direct.call(x)
      elsif direction == -1
        inverse.call(x)
      end
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
unitwise-0.3.0 lib/unitwise/function.rb
unitwise-0.2.2 lib/unitwise/function.rb
unitwise-0.2.1 lib/unitwise/function.rb
unitwise-0.2.0 lib/unitwise/function.rb
unitwise-0.1.0 lib/unitwise/function.rb