Skip to main content

horner

Function horner 

Source
pub fn horner(
    coeffs: &mut [f64],
    degree: usize,
    vr: &Vector2<f64>,
) -> Vector2<f64>
Expand description

The horner function implements synthetic division by a quadratic factor $$ x^2 - r x - q $$.

Given polynomial $$ P(x) = \sum_{k=0}^{n} a_k x^{n-k} $$, the recurrence for the quotient coefficients $$ b_k $$ is:

$$ b_0 = a_0,\quad b_1 = a_1 + r b_0,\quad b_k = a_k + r b_{k-1} + q b_{k-2} $$

with remainder $$ A = b_{n-1},; B = b_n + q b_{n-1} $$.

Arguments:

  • coeffs: A mutable slice of f64 values representing the coefficients of the polynomial. The coefficients are in descending order of degree.
  • degree: The degree parameter represents the degree of the polynomial. It is used to determine the number of coefficients in the coeffs array.
  • vr: The parameter vr is a Vec2 struct that contains two values, x_ and y_. In the example, vr is initialized with the values -1.0 and -2.0.

Returns:

The function horner returns a Vec2 struct, which contains two f64 values representing the remainder $$ (A, B) $$ of the synthetic division.

ยงExamples:

use ginger::rootfinding::horner;
use ginger::vector2::Vector2;
use approx_eq::assert_approx_eq;

let mut coeffs = vec![10.0, 34.0, 75.0, 94.0, 150.0, 94.0, 75.0, 34.0, 10.0];
let px = horner(&mut coeffs, 8, &Vector2::new(-1.0, -2.0));

assert_approx_eq!(px.x_, 114.0);
assert_approx_eq!(px.y_, 134.0);
assert_approx_eq!(coeffs[3], 15.0);