Class: OpenC3::SegmentedPolynomialConversion::Segment
- Defined in:
- lib/openc3/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.
-
#==(other_segment) ⇒ Object
Implement equality operator primarily for ease of testing.
-
#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 OpenC3::SegmentedPolynomialConversion.
48 49 50 51 |
# File 'lib/openc3/conversions/segmented_polynomial_conversion.rb', line 48 def initialize(lower_bound, coeffs) @lower_bound = lower_bound @coeffs = coeffs end |
Instance Attribute Details
#coeffs ⇒ Array<Integer> (readonly)
Returns The polynomial coefficients.
39 40 41 |
# File 'lib/openc3/conversions/segmented_polynomial_conversion.rb', line 39 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.
36 37 38 |
# File 'lib/openc3/conversions/segmented_polynomial_conversion.rb', line 36 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.
60 61 62 |
# File 'lib/openc3/conversions/segmented_polynomial_conversion.rb', line 60 def <=>(other_segment) return other_segment.lower_bound <=> @lower_bound end |
#==(other_segment) ⇒ Object
Implement equality operator primarily for ease of testing
67 68 69 70 |
# File 'lib/openc3/conversions/segmented_polynomial_conversion.rb', line 67 def ==(other_segment) @lower_bound == other_segment.lower_bound && @coeffs == other_segment.coeffs end |
#calculate(value) ⇒ Float
Perform the polynomial conversion
76 77 78 79 80 81 82 |
# File 'lib/openc3/conversions/segmented_polynomial_conversion.rb', line 76 def calculate(value) converted = 0.0 @coeffs.length.times do |index| converted += @coeffs[index].to_f * (value**index) end return converted end |