Class: Cosmos::SegmentedPolynomialConversion::Segment

Inherits:
Object
  • Object
show all
Defined in:
lib/cosmos/conversions/segmented_polynomial_conversion.rb

Overview

A polynomial conversion segment which applies the conversion from the lower bound (inclusive) until another segment’s lower bound is encountered.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lower_bound, coeffs) ⇒ Segment

Creates a polynomial conversion segment. Multiple Segments are used to implemnt a Cosmos::SegmentedPolynomialConversion.



37
38
39
40
# File 'lib/cosmos/conversions/segmented_polynomial_conversion.rb', line 37

def initialize(lower_bound, coeffs)
  @lower_bound = lower_bound
  @coeffs = coeffs
end

Instance Attribute Details

#coeffsArray<Integer> (readonly)



28
29
30
# File 'lib/cosmos/conversions/segmented_polynomial_conversion.rb', line 28

def coeffs
  @coeffs
end

#lower_boundInteger (readonly)



26
27
28
# File 'lib/cosmos/conversions/segmented_polynomial_conversion.rb', line 26

def lower_bound
  @lower_bound
end

Instance Method Details

#<=>(other_segment) ⇒ Integer

Implement the comparison operator to compared based on the lower_bound but sort in reverse order so the segment with the largest lower_bound comes first. This makes the calculation code in call easier.



49
50
51
# File 'lib/cosmos/conversions/segmented_polynomial_conversion.rb', line 49

def <=>(other_segment)
  return other_segment.lower_bound <=> @lower_bound
end

#calculate(value) ⇒ Float

Perform the polynomial conversion



57
58
59
60
61
62
63
# File 'lib/cosmos/conversions/segmented_polynomial_conversion.rb', line 57

def calculate(value)
  converted = 0.0
  @coeffs.length.times do |index|
    converted += @coeffs[index].to_f * (value ** index)
  end
  return converted
end