Class: Unitwise::Functional

Inherits:
Scale
  • Object
show all
Extended by:
Math
Defined in:
lib/unitwise/functional.rb

Overview

Functional is an alterative function-based scale for atoms with a non-linear (or non-zero y-intercept) scale. This is most commonly used for temperatures. Known functions for converting to and from special atoms are setup as class methods here.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Scale

#atoms, #depth, #inspect, #root_terms, #scalar, #special?, #terms, #to_s, #unit

Methods included from Compatible

#<=>, #compatible_with?, #composition, #composition_string, #dim, included

Constructor Details

#initialize(value, unit, function_name) ⇒ Functional

Setup a new functional. used for conversion

Parameters:

  • value (Numeric) —

    The magnitude of the scale

  • unit (Unitwise::Unit, String) —

    The unit of the scale

  • function_name (String, Symbol) —

    One of the class methods above to be



56
57
58
59
# File 'lib/unitwise/functional.rb', line 56

def initialize(value, unit, function_name)
  @function_name = function_name
  super(value, unit)
end

Instance Attribute Details

#function_name ⇒ Object (readonly)

Returns the value of attribute function_name.



49
50
51
# File 'lib/unitwise/functional.rb', line 49

def function_name
  @function_name
end

Class Method Details

._2lg(x, forward = true) ⇒ Object



45
46
47
# File 'lib/unitwise/functional.rb', line 45

def self._2lg(x, forward = true)
  forward ? 2 * log10(x) : 10 ** (x / 2)
end

._cel(x, forward = true) ⇒ Object



9
10
11
# File 'lib/unitwise/functional.rb', line 9

def self._cel(x, forward = true)
  forward ? x - 273.15 : x + 273.15
end

._degf(x, forward = true) ⇒ Object



13
14
15
# File 'lib/unitwise/functional.rb', line 13

def self._degf(x, forward = true)
  forward ? 9.0 * x / 5.0 - 459.67 : 5.0 / 9 * (x + 459.67)
end

._hpC(x, forward = true) ⇒ Object



21
22
23
# File 'lib/unitwise/functional.rb', line 21

def self._hpC(x, forward = true)
  forward ? -log(x) / log(100) : 100 ** -x
end

._hpX(x, forward = true) ⇒ Object



17
18
19
# File 'lib/unitwise/functional.rb', line 17

def self._hpX(x, forward = true)
  forward ? -log10(x) : 10 ** -x
end

._ld(x, forward = true) ⇒ Object



33
34
35
# File 'lib/unitwise/functional.rb', line 33

def self._ld(x, forward = true)
  forward ? log2(x) : 2 ** x
end

._lg(x, forward = true) ⇒ Object



41
42
43
# File 'lib/unitwise/functional.rb', line 41

def self._lg(x, forward = true)
  forward ? log10(x) : 10 ** x
end

._ln(x, forward = true) ⇒ Object



37
38
39
# File 'lib/unitwise/functional.rb', line 37

def self._ln(x, forward = true)
  forward ? log(x) : Math::E ** x
end

._ph(x, forward = true) ⇒ Object



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

def self._ph(x, forward = true)
  _hpX(x,forward)
end

._tan100(x, forward = true) ⇒ Object



25
26
27
# File 'lib/unitwise/functional.rb', line 25

def self._tan100(x, forward = true)
  forward ? 100 * tan(x) : atan(x / 100)
end

Instance Method Details

#functional(x = scalar, forward = true) ⇒ Numeric

Get the equivalent scalar value of a unit based on the atom's function. when converting from the special, use false when converting to the special.

Returns:



67
68
69
# File 'lib/unitwise/functional.rb', line 67

def functional(x = scalar, forward = true)
  self.class.send(:"_#{function_name}", x, forward)
end