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) ⇒ 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) ⇒ 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 implement a OpenC3::SegmentedPolynomialConversion.
51 52 53 54 |
# File 'lib/openc3/conversions/segmented_polynomial_conversion.rb', line 51 def initialize(lower_bound, coeffs) @lower_bound = lower_bound @coeffs = coeffs end |
Instance Attribute Details
#coeffs ⇒ Array<Integer> (readonly)
Returns The polynomial coefficients.
42 43 44 |
# File 'lib/openc3/conversions/segmented_polynomial_conversion.rb', line 42 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.
39 40 41 |
# File 'lib/openc3/conversions/segmented_polynomial_conversion.rb', line 39 def lower_bound @lower_bound end |
Instance Method Details
#<=>(other) ⇒ 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.
63 64 65 |
# File 'lib/openc3/conversions/segmented_polynomial_conversion.rb', line 63 def <=>(other) return other.lower_bound <=> @lower_bound end |
#==(other) ⇒ Object
Implement equality operator primarily for ease of testing
70 71 72 73 |
# File 'lib/openc3/conversions/segmented_polynomial_conversion.rb', line 70 def ==(other) @lower_bound == other.lower_bound && @coeffs == other.coeffs end |
#calculate(value) ⇒ Float
Perform the polynomial conversion
79 80 81 82 83 84 85 |
# File 'lib/openc3/conversions/segmented_polynomial_conversion.rb', line 79 def calculate(value) converted = 0.0 @coeffs.length.times do |index| converted += @coeffs[index].to_f * (value**index) end return converted end |