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, #eql?, #expression, #hash, #inspect, #root_terms, #simplified_value, #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



104
105
106
107
# File 'lib/unitwise/functional.rb', line 104

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

Instance Attribute Details

#function_nameObject (readonly)

Returns the value of attribute function_name.



97
98
99
# File 'lib/unitwise/functional.rb', line 97

def function_name
  @function_name
end

Class Method Details

.from_2lg(x) ⇒ Object



93
94
95
# File 'lib/unitwise/functional.rb', line 93

def self.from_2lg(x)
  10.0 ** (x / 2.0)
end

.from_cel(x) ⇒ Object



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

def self.from_cel(x)
  x + 273.15
end

.from_degf(x) ⇒ Object



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

def self.from_degf(x)
  5.0 / 9.0 * (x + 459.67)
end

.from_degre(x) ⇒ Object



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

def self.from_degre(x)
  5.0 / 4.0 * (x + 218.52)
end

.from_hpC(x) ⇒ Object



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

def self.from_hpC(x)
  100.0 ** -x
end

.from_hpX(x) ⇒ Object



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

def self.from_hpX(x)
  10.0 ** -x
end

.from_ld(x) ⇒ Object



69
70
71
# File 'lib/unitwise/functional.rb', line 69

def self.from_ld(x)
  2.0 ** x
end

.from_lg(x) ⇒ Object



85
86
87
# File 'lib/unitwise/functional.rb', line 85

def self.from_lg(x)
  10.0 ** x
end

.from_ln(x) ⇒ Object



77
78
79
# File 'lib/unitwise/functional.rb', line 77

def self.from_ln(x)
  Math::E ** x
end

.from_ph(x) ⇒ Object



61
62
63
# File 'lib/unitwise/functional.rb', line 61

def self.from_ph(x)
  from_hpX(x)
end

.from_tan100(x) ⇒ Object



53
54
55
# File 'lib/unitwise/functional.rb', line 53

def self.from_tan100(x)
  atan(x / 100.0)
end

.to_2lg(x) ⇒ Object



89
90
91
# File 'lib/unitwise/functional.rb', line 89

def self.to_2lg(x)
  2.0 * log10(x)
end

.to_cel(x) ⇒ Object



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

def self.to_cel(x)
  x - 273.15
end

.to_degf(x) ⇒ Object



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

def self.to_degf(x)
  9.0 * x / 5.0 - 459.67
end

.to_degre(x) ⇒ Object



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

def self.to_degre(x)
  4.0 * x / 5.0 - 218.52
end

.to_hpC(x) ⇒ Object



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

def self.to_hpC(x)
  -log(x) / log(100.0)
end

.to_hpX(x) ⇒ Object



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

def self.to_hpX(x)
  -log10(x)
end

.to_ld(x) ⇒ Object



65
66
67
# File 'lib/unitwise/functional.rb', line 65

def self.to_ld(x)
  Math.log(x) / Math.log(2.0)
end

.to_lg(x) ⇒ Object



81
82
83
# File 'lib/unitwise/functional.rb', line 81

def self.to_lg(x)
  log10(x)
end

.to_ln(x) ⇒ Object



73
74
75
# File 'lib/unitwise/functional.rb', line 73

def self.to_ln(x)
  log(x)
end

.to_ph(x) ⇒ Object



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

def self.to_ph(x)
  to_hpX(x)
end

.to_tan100(x) ⇒ Object



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

def self.to_tan100(x)
  100.0 * tan(x)
end

Instance Method Details

#magnitude(scalar = scalar()) ⇒ Numeric

Get the equivalent magnitude on this scale for a scalar value

Parameters:

  • scalar (Numeric) (defaults to: scalar())

    A linear scalar value

Returns:

  • (Numeric)

    The equivalent magnitude on this scale



121
122
123
# File 'lib/unitwise/functional.rb', line 121

def magnitude(scalar = scalar())
  self.class.send(:"to_#{function_name}", scalar)
end

#scalar(magnitude = value) ⇒ Numeric

Get the equivalent scalar value of a magnitude on this scale

Parameters:

  • magnitude (Numeric) (defaults to: value)

    The magnitude to find the scalar value for

Returns:

  • (Numeric)

    Equivalent linear scalar value



113
114
115
# File 'lib/unitwise/functional.rb', line 113

def scalar(magnitude = value)
  self.class.send(:"from_#{function_name}", magnitude)
end