f(x)

Polynomials

Your Progress in this Chapter

0%

0 / 12 units completed

Chapter Overview

Polynomial Evaluation

Section 2.1 of 610 min4 code examples

Polynomial Representation and Order

As in other numerical analysis and engineering software, SepalSolver uses the standard convention of represent polynomials with coefficients in Descending Order. This means the first element of the array corresponds to the highest power of x, making it easier to read and align with long-hand mathematical notation.

1. The Descending Order Convention

A polynomial P(x)= a_{n}x^{n} + a_{n-1}x^{n-1} + a_{n-2}x^{n-2} + \cdots + a_{1}x + a_{0} is stored in an array where coeffs[0] is a_n, coeffs[1] is a_{n-1}, and so on, down to coeffs[n] which is a_0. This ordering simplifies both the evaluation and manipulation of polynomials in code.

Example 1C#

1
2
3
4
5
6
Code is ready to run

2. Horner's Method (Descending)

When coefficients are in descending order, Horner's method becomes particularly elegant. We start with the first coefficient and repeatedly multiply by x and add the next coefficient: P(x) = ( \cdot ((a_{n}x + a_{n-1})x + a_{n-2})x + \cdots + a_{1})x + a_{0}

Examples

.. Admonition:: Example 1 : Real Value Evaluation (Descending)

We define a cubic polynomial P(x)= 2x^3 −6x^2 + 2x−1. Notice how the input sequence exactly matches the mathematical coefficients written from highest to lowest degree.

Example 2C#

1
2
3
4
Code is ready to run
OutputFrom the book
P(3.0) = 5

.. Admonition:: Example 2 : Complex Evaluation (Descending)

Evaluating at a complex point s = \sigma + j \omega is common in control theory. Here we evaluate P(s)=1s^2 + 0s + 1 (which is s^2 + 1) at the imaginary unit i.

Example 3C#

1
2
3
4
Code is ready to run
OutputFrom the book
P(i) =   0.0000 + 0.0000i 

.. Admonition:: Example 3 : Column Vector Evaluation (Vectorized)

In this case, we have a set of measurements in a ColVec and we want to pass them through our polynomial model. SepalSolver iterates /// through the vector, applying the descending-order Horner's method /// to each element.

Example 4C#

1
2
3
4
5
6
Code is ready to run
OutputFrom the book
Result at x = 
 0   1   2 
 is: 
 3   6  11 

Implementation Tip: Power Mapping

Because we use descending order, the power associated with a coefficient at index i is calculated as Degree - i. This is important when performing differentiation, as the derivative of the term at coeffs[i] involves multiplying by (Degree - i).