Class: OpenC3::SegmentedPolynomialConversion::Segment

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(lower_bound, coeffs) ⇒ Segment

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

Parameters:

  • lower_bound (Integer)

    The value at which point this polynomial conversion should apply. All values >= to this value will be converted using the given coefficients.

  • coeffs (Array<Integer>)

    The polynomial coefficients



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

#coeffsArray<Integer> (readonly)

Returns The polynomial coefficients.

Returns:

  • (Array<Integer>)

    The polynomial coefficients



42
43
44
# File 'lib/openc3/conversions/segmented_polynomial_conversion.rb', line 42

def coeffs
  @coeffs
end

#lower_boundInteger (readonly)

Returns The value at which point this polynomial conversion should apply. All values >= to this value will be converted using the given coefficients.

Returns:

  • (Integer)

    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_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.

Parameters:

  • other_segment (Segment)

    The segment to compare

Returns:

  • (Integer)

    1 if self.lower_bound > other_segment.lower_bound, 0 if they are equal, -1 if self.lower_bound < other_segment.lower_bound



63
64
65
# File 'lib/openc3/conversions/segmented_polynomial_conversion.rb', line 63

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

#==(other_segment) ⇒ Object

Implement equality operator primarily for ease of testing

Parameters:

  • segment (Segment)

    Other segment



70
71
72
73
# File 'lib/openc3/conversions/segmented_polynomial_conversion.rb', line 70

def ==(other_segment)
  @lower_bound == other_segment.lower_bound &&
    @coeffs == other_segment.coeffs
end

#calculate(value) ⇒ Float

Perform the polynomial conversion

Parameters:

  • value (Numeric)

    The value to convert

Returns:

  • (Float)

    The converted value



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