Class: SY::Measure
- Inherits:
-
Object
- Object
- SY::Measure
- Defined in:
- lib/sy/mapping.rb,
lib/sy/measure.rb
Overview
Represents a certain way, that a quantity measures another quantity (reference quantity). Instance has two attributes:
-
@r - read closure, for converting from the measured reference quantity.
-
@w - write closure, for converting back to the reference quantity.
Convenience methods #read and #write facilitate their use.
Instance Attribute Summary collapse
-
#r ⇒ Object
readonly
Returns the value of attribute r.
-
#ratio ⇒ Object
readonly
Returns the value of attribute ratio.
-
#w ⇒ Object
readonly
Returns the value of attribute w.
Class Method Summary collapse
-
.identity ⇒ Object
Identity measure.
-
.linear(hsh) ⇒ Object
Linear (scaled offset) measure.
-
.logarithmic(base = Math::E) ⇒ Object
Logarithmic.
-
.negative_logarithmic(base = Math::E) ⇒ Object
Negative logarithmic.
-
.simple_offset(offset) ⇒ Object
Simple offset measure.
-
.simple_scale(scale) ⇒ Object
Simple scaling measure.
Instance Method Summary collapse
-
#*(other) ⇒ Object
Measure composition (like f * g function composition).
-
#**(n) ⇒ Object
Measure power.
-
#/(other) ⇒ Object
Measure composition with inverse of another measure.
-
#initialize(ratio: nil, r: nil, w: nil) ⇒ Measure
constructor
The constructor expects :r and :w arguments for read and write closure.
-
#inverse ⇒ Object
Inverse measure.
-
#read(magnitude_of_reference_quantity, quantity) ⇒ Object
Convenience method to read a magnitude of a reference quantity.
-
#write(magnitude, reference_quantity) ⇒ Object
Convenience method to convert a magnitude back to the reference quantity.
Constructor Details
#initialize(ratio: nil, r: nil, w: nil) ⇒ Measure
The constructor expects :r and :w arguments for read and write closure.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/sy/mapping.rb', line 58 def initialize ratio: nil, r: ( fail ArgumentError, ":r closure not specified!" unless ratio ), w: ( fail ArgumentError, ":w closure not specified!" unless ratio ) if ratio.nil? fail TypeError, ":r and :w arguments must both be closures!" unless r.is_a?( Proc ) && w.is_a?( Proc ) @ratio, @r, @w = nil, r, w else fail ArgumentError, ":r or :w must not be given if :ratio given!" if r || w @ratio = ratio @r = lambda { |ref_amnt| ref_amnt / ratio } @w = lambda { |amnt| amnt * ratio } end end |
Instance Attribute Details
#r ⇒ Object (readonly)
Returns the value of attribute r.
54 55 56 |
# File 'lib/sy/mapping.rb', line 54 def r @r end |
#ratio ⇒ Object (readonly)
Returns the value of attribute ratio.
54 55 56 |
# File 'lib/sy/mapping.rb', line 54 def ratio @ratio end |
#w ⇒ Object (readonly)
Returns the value of attribute w.
54 55 56 |
# File 'lib/sy/mapping.rb', line 54 def w @w end |
Class Method Details
.identity ⇒ Object
Identity measure.
15 |
# File 'lib/sy/mapping.rb', line 15 def identity; simple_scale 1 end |
.linear(hsh) ⇒ Object
Linear (scaled offset) measure. Expects a hash with 2 points demonstrating the relationship: { ref_amount_1 => amount_1, ref_amount_2 => amount_2 }. (Example: Fahrenheit degrees vs. Kelvins.)
32 33 34 35 36 37 |
# File 'lib/sy/mapping.rb', line 32 def linear hsh ref_amnt_1, amnt_1, ref_amnt_2, amnt_2 = hsh.to_a.flatten scale = ( ref_amnt_2 - ref_amnt_1 ) / ( amnt_2 - amnt_1 ) new( r: lambda { |ref_amnt| amnt_1 + ( ref_amnt - ref_amnt_1 ) / scale }, w: lambda { |amnt| ref_amnt_1 + ( amnt - amnt_1 ) * scale } ) end |
.logarithmic(base = Math::E) ⇒ Object
Logarithmic.
41 42 43 44 |
# File 'lib/sy/mapping.rb', line 41 def logarithmic base=Math::E new( r: lambda { |ref_amnt| Math.log ref_amnt, base }, w: lambda { |amnt| base ** amnt } ) end |
.negative_logarithmic(base = Math::E) ⇒ Object
Negative logarithmic.
48 49 50 51 |
# File 'lib/sy/mapping.rb', line 48 def negative_logarithmic base=Math::E new( r: lambda { |ref_amnt| -Math.log( ref_amnt, base ) }, w: lambda { |amnt| base ** ( -amnt ) } ) end |
.simple_offset(offset) ⇒ Object
Simple offset measure. (Such as °C)
23 24 25 26 |
# File 'lib/sy/mapping.rb', line 23 def simple_offset offset new( r: lambda { |ref_amnt| ref_amnt - offset }, w: lambda { |amnt| amnt + offset } ) end |
.simple_scale(scale) ⇒ Object
Simple scaling measure. (Eg. pounds vs kilograms)
19 |
# File 'lib/sy/mapping.rb', line 19 def simple_scale scale; new( ratio: scale ) end |
Instance Method Details
#*(other) ⇒ Object
Measure composition (like f * g function composition).
94 95 96 97 98 99 100 101 102 |
# File 'lib/sy/mapping.rb', line 94 def * other if ratio.nil? then r1, r2, w1, w2 = r, other.r, w, other.w self.class.new( r: lambda { |ref_amnt| r1.( r2.( ref_amnt ) ) }, w: lambda { |amnt| w2.( w1.( amnt ) ) } ) else self.class.new( ratio: ratio * other.ratio ) end end |
#**(n) ⇒ Object
Measure power.
112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/sy/mapping.rb', line 112 def ** n if ratio.nil? then r_closure, w_closure = r, w self.class.new( r: lambda { |ref_amnt| n.times.inject ref_amnt do |m, _| r_closure.( m ) end }, w: lambda { |amnt| n.times.inject amnt do |m, _| w_closure.( m ) end } ) else self.class.new( ratio: ratio ** n ) end end |
#/(other) ⇒ Object
Measure composition with inverse of another measure.
106 107 108 |
# File 'lib/sy/mapping.rb', line 106 def / other self * other.inverse end |
#inverse ⇒ Object
Inverse measure.
87 88 89 90 |
# File 'lib/sy/mapping.rb', line 87 def inverse if ratio.nil? then self.class.new( r: w, w: r ) # swap closures else self.class.new( ratio: 1 / ratio ) end end |
#read(magnitude_of_reference_quantity, quantity) ⇒ Object
Convenience method to read a magnitude of a reference quantity.
75 76 77 |
# File 'lib/sy/mapping.rb', line 75 def read magnitude_of_reference_quantity, quantity quantity.magnitude r.( magnitude_of_reference_quantity.amount ) end |
#write(magnitude, reference_quantity) ⇒ Object
Convenience method to convert a magnitude back to the reference quantity.
81 82 83 |
# File 'lib/sy/mapping.rb', line 81 def write magnitude, reference_quantity reference_quantity.magnitude w.( magnitude.amount ) end |