Class: Unitwise::Function

Inherits:
Object
  • Object
show all
Includes:
Math
Defined in:
lib/unitwise/function.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, direct, inverse) ⇒ Function

Returns a new instance of Function.



36
37
38
39
40
# File 'lib/unitwise/function.rb', line 36

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

Instance Attribute Details

#directObject (readonly)

Returns the value of attribute direct.



34
35
36
# File 'lib/unitwise/function.rb', line 34

def direct
  @direct
end

#inverseObject (readonly)

Returns the value of attribute inverse.



34
35
36
# File 'lib/unitwise/function.rb', line 34

def inverse
  @inverse
end

#nameObject (readonly)

Returns the value of attribute name.



34
35
36
# File 'lib/unitwise/function.rb', line 34

def name
  @name
end

Class Method Details

.add(*args) ⇒ Object



23
24
25
26
27
# File 'lib/unitwise/function.rb', line 23

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

.allObject



5
6
7
# File 'lib/unitwise/function.rb', line 5

def all
  @all ||= defaults
end

.defaultsObject



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/unitwise/function.rb', line 9

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

.find(name) ⇒ Object



29
30
31
# File 'lib/unitwise/function.rb', line 29

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

Instance Method Details

#functional(x, direction = 1) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/unitwise/function.rb', line 42

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