# CAGD/Polynomials

We will quickly review basic power polynomials while we transition to understanding Bernstein polynomials.

## Power and Bernstein Polynomials[edit | edit source]

In basic algebra, we learn all about power polynomials. We encounter them all over the place and have learned lots of ways to manipulate them. They take the form:

- ,

where p_{i} are the coefficients of the polynomial. Although this might be sufficient for a lot of mathematical operations, oftentimes it's easier to use Bernstein polynomials. They take the form:

- ,

where b_{i} are the Bernstein coefficients of the polynomial. Although it doesn't seem computationally faster, it does provide a benefit in some often-used cases. The Bézier curve is built off of the Bernstein polynomial as you can see from the similiarity of form. In fact, a Bézier curve can be expressed as a collection of Bernstein polynomials. If the points that define the curve P are defined by the coordinates (X,Y,Z,W), we can express them as separate Bernstein polynomials.

## Basis Conversion[edit | edit source]

Sometimes we have a curve in one basis but would like to convert to the other basis. A proof of how to get the conversion formula is as follows:

First we start with the Taylor expansion for the Bernstein polynomial:

If the power polynomial is equal to the Bernstein polynomial, the following relation must be true:

For power polynomials:

This makes the relationship:

We apply the general derivative for Bézier curves to Bernstein polynomials:

A recurrence formula can be made:

Through manipulation of the recurrence formula and definition of the derivative:

Thus, we can simplify further:

This allows us to convert between the bases very easily. Just like with the de Casteljau algorithm, we can set up a difference table with our Bernstein basis coefficients in the top row and compute the power basis coefficients very easily.

## Evaluating Polynomials[edit | edit source]

### Brute Force Method[edit | edit source]

The knee-jerk reaction to evaluating polynomials is simply to plug in the value we know and do the operations. For a general polynomial f(x) = a + bx + cx^{2} + dx^{3} + ..., we would start with a as our result, multiply b times our value and add it to the result, multiply c times our value squared and add that to our result, and so on. This takes n addition operations, but n! multiplication operations. As the degree gets higher, the number of multiplication operations increase, increasing our algorithm time and possibly introducing some floating-point error in our calculations. There are a couple other techniques that allow us to evaluate polynomials that will be presented here.

### Forward Differencing[edit | edit source]

Forward differencing is a technique to evaluate polynomials quickly. This technique works when we are evaluating evenly spaced arguments. For example, if we are evaluating the polynomial at f(a), we could apply forward differencing to get f(2a), f(3a), and so on. This method is fast, requiring only addition. We construct a difference table and can quickly evaluate the polynomial. In the following example, we have some values for our polynomial:

As we see in the 4th difference row, -15 appears in every term. This is a *magic number*. If we simply add another -15 in the next cell to the right and compute the difference table backwards, when we get to the f(t) row, we will have the value of the polynomial evaluated at t_{i+8}. It requires only n addition operations to evaluate the polynomial, making it very fast. However, this method relies heavily on the accuracy of the previous values. Because of floating-point error, this method is numerically unstable. It can be used as a good approximation, however.

### Horner's Algorithm[edit | edit source]

Horner's algorithm is the fastest algorithm to evaluate a power polynomial at a certain value. Using a brute-force algorithm to evaluate a polynomial, it will require a lot of addition and multiplication operations. Horner's algorithm accomplishes evaluation of an nth degree polynomial with n adds and n multiplies. It takes advantage of the nested form of a power polynomial:

We can write some pseudocode that allows us to implement Horner's algorithm:

```
hornerEval(double[] p, double t)
{
result = p[p.size-1];
for(int i = 0; i < p.size - 1; i++)
{
result = t*result + p[i];
}
return result;
}
```