Class: Cosmos::PolynomialConversion
- Defined in:
- lib/cosmos/conversions/polynomial_conversion.rb,
ext/cosmos/ext/polynomial_conversion/polynomial_conversion.c
Overview
Performs a polynomial conversion on the value
Instance Attribute Summary collapse
-
#coeffs ⇒ Array<Float>
The polynomial coefficients.
Instance Method Summary collapse
-
#call(value, myself, buffer) ⇒ Object
Calling this method performs a polynomial conversion on the given value.
-
#initialize(coeff_array) ⇒ PolynomialConversion
constructor
Initializes the conversion with the given polynomial coefficients.
-
#to_s ⇒ String
Class followed by the list of coefficients.
Constructor Details
#initialize(coeff_array) ⇒ PolynomialConversion
Initializes the conversion with the given polynomial coefficients. Sets the converted_type to :FLOAT and the converted_bit_size to 64.
26 27 28 29 30 31 32 33 34 |
# File 'lib/cosmos/conversions/polynomial_conversion.rb', line 26 def initialize(coeff_array) super() @coeffs = [] coeff_array.each do |coeff| @coeffs << coeff.to_f end @converted_type = :FLOAT @converted_bit_size = 64 end |
Instance Attribute Details
#coeffs ⇒ Array<Float>
Returns The polynomial coefficients.
20 21 22 |
# File 'lib/cosmos/conversions/polynomial_conversion.rb', line 20 def coeffs @coeffs end |
Instance Method Details
#call(value, myself, buffer) ⇒ Object
Calling this method performs a polynomial conversion on the given value.
conversion.call(1, packet) #=> 2.5
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'ext/cosmos/ext/polynomial_conversion/polynomial_conversion.c', line 31
static VALUE polynomial_conversion_call(VALUE self, VALUE value, VALUE myself, VALUE buffer)
{
volatile VALUE coeffs = Qnil;
long coeffs_length = 0;
int index = 0;
double double_value = 0.0;
double coeff = 0.0;
double converted = 0.0;
double raised_to_power = 1.0;
coeffs = rb_ivar_get(self, id_ivar_coeffs);
coeffs_length = RARRAY_LEN(coeffs);
double_value = RFLOAT_VALUE(rb_funcall(value, id_method_to_f, 0));
/* Handle C0 */
coeff = RFLOAT_VALUE(rb_ary_entry(coeffs, 0));
converted += coeff;
/* Handle Coefficients raised to a power */
for (index = 1; index < coeffs_length; index++)
{
raised_to_power *= double_value;
coeff = RFLOAT_VALUE(rb_ary_entry(coeffs, index));
converted += (coeff * raised_to_power);
}
return rb_float_new(converted);
}
|
#to_s ⇒ String
Returns Class followed by the list of coefficients.
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/cosmos/conversions/polynomial_conversion.rb', line 41 def to_s result = "" @coeffs.length.times do |index| if index == 0 result << "#{@coeffs[index]}" elsif index == 1 result << " + #{@coeffs[index]}x" else result << " + #{@coeffs[index]}x^#{index}" end end result end |