Class: Cosmos::SegmentedPolynomialConversion::Segment
- 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
-
#coeffs ⇒ Array<Integer>
readonly
The polynomial coefficients.
-
#lower_bound ⇒ Integer
readonly
The value at which point this polynomial conversion should apply.
Instance Method Summary collapse
-
#<=>(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.
-
#calculate(value) ⇒ Float
Perform the polynomial conversion.
-
#initialize(lower_bound, coeffs) ⇒ Segment
constructor
Creates a polynomial conversion segment.
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
#coeffs ⇒ Array<Integer> (readonly)
Returns The polynomial coefficients.
28 29 30 |
# File 'lib/cosmos/conversions/segmented_polynomial_conversion.rb', line 28 def coeffs @coeffs end |
#lower_bound ⇒ Integer (readonly)
Returns The value at which point this polynomial conversion should apply. All values >= to this value will be converted using the given coefficients.
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 |