class SY::Measure

Represents a certain way, that a quantity measures another quantity (reference quantity). Instance has two attributes:

Convenience methods read and write facilitate their use.

Represents a certain way, that a quantity measures another quantity (reference quantity). Instance has two attributes:

Convenience methods read and write facilitate their use.

Attributes

r[R]
ratio[R]
w[R]

Public Class Methods

identity() click to toggle source

Identity measure.

# File lib/sy/mapping.rb, line 15
def identity; simple_scale 1 end
linear(hsh) click to toggle source

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.)

# 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) click to toggle source

Logarithmic.

# 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) click to toggle source

Negative logarithmic.

# 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
new(ratio: nil, r: ( fail ArgumentError, ":r closure not specified!" unless ratio ), w: ( fail ArgumentError, ":w closure not specified!" unless ratio )) click to toggle source

The constructor expects :r and :w arguments for read and write closure.

# 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
simple_offset(offset) click to toggle source

Simple offset measure. (Such as °C)

# 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;) click to toggle source

Simple scaling measure. (Eg. pounds vs kilograms)

# File lib/sy/mapping.rb, line 19
def simple_scale scale; new( ratio: scale ) end

Public Instance Methods

*(other) click to toggle source

Measure composition (like f * g function composition).

# 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) click to toggle source

Measure power.

# 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) click to toggle source

Measure composition with inverse of another measure.

# File lib/sy/mapping.rb, line 106
def / other
  self * other.inverse
end
inverse() click to toggle source

Inverse measure.

# 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) click to toggle source

Convenience method to read a magnitude of a reference quantity.

# 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) click to toggle source

Convenience method to convert a magnitude back to the reference quantity.

# File lib/sy/mapping.rb, line 81
def write magnitude, reference_quantity
  reference_quantity.magnitude w.( magnitude.amount )
end

Protected Instance Methods

[]( *args ) click to toggle source
# File lib/sy/mapping.rb, line 128
def []( *args )
  send *args
end