Class: Cosmos::SegmentedPolynomialConversion
- Inherits:
-
Conversion
- Object
- Conversion
- Cosmos::SegmentedPolynomialConversion
- Defined in:
- lib/cosmos/conversions/segmented_polynomial_conversion.rb
Overview
Segmented polynomial conversions consist of polynomial conversions that are applied for a range of values.
Defined Under Namespace
Classes: Segment
Instance Attribute Summary
Attributes inherited from Conversion
#converted_bit_size, #converted_type
Instance Method Summary collapse
-
#add_segment(lower_bound, *coeffs) ⇒ Object
Add a segment to the segmented polynomial.
-
#call(value, packet, buffer) ⇒ Float
The value with the polynomial applied.
-
#initialize ⇒ SegmentedPolynomialConversion
constructor
Initialize the converted_type to :FLOAT and converted_bit_size to 64.
-
#to_s ⇒ String
The name of the class followed by a description of all the polynomial segments.
Constructor Details
#initialize ⇒ SegmentedPolynomialConversion
Initialize the converted_type to :FLOAT and converted_bit_size to 64.
67 68 69 70 71 72 |
# File 'lib/cosmos/conversions/segmented_polynomial_conversion.rb', line 67 def initialize super() @segments = [] @converted_type = :FLOAT @converted_bit_size = 64 end |
Instance Method Details
#add_segment(lower_bound, *coeffs) ⇒ Object
Add a segment to the segmented polynomial. The lower bound is inclusive, but is ignored for the segment with the lowest lower_bound.
81 82 83 84 |
# File 'lib/cosmos/conversions/segmented_polynomial_conversion.rb', line 81 def add_segment(lower_bound, *coeffs) @segments << Segment.new(lower_bound, coeffs) @segments.sort! end |
#call(value, packet, buffer) ⇒ Float
Returns The value with the polynomial applied.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/cosmos/conversions/segmented_polynomial_conversion.rb', line 88 def call(value, packet, buffer) # Try to find correct segment @segments.each do |segment| if value >= segment.lower_bound return segment.calculate(value) end end # Default to using segment with smallest lower_bound segment = @segments[-1] if segment return @segments[-1].calculate(value) else return nil end end |
#to_s ⇒ String
Returns The name of the class followed by a description of all the polynomial segments.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/cosmos/conversions/segmented_polynomial_conversion.rb', line 107 def to_s result = "" count = 0 @segments.each do |segment| result << "\n" if count > 0 result << "Lower Bound: #{segment.lower_bound} Polynomial: " segment.coeffs.length.times do |index| if index == 0 result << "#{segment.coeffs[index]}" elsif index == 1 result << " + #{segment.coeffs[index]}x" else result << " + #{segment.coeffs[index]}x^#{index}" end end count += 1 end result end |