This notebook is part of https://github.com/AudioSceneDescriptionFormat/splines, see also https://splines.readthedocs.io/.

# Polynomial Parametric Curves

The building blocks for
[polynomial splines](index.ipynb)
are of course [polynomials](https://en.wikipedia.org/wiki/Polynomial).


```python
import sympy as sp
sp.init_printing(order='grevlex')
```

We are mostly interested in *univariate* splines,
i.e. curves with one free parameter,
which are built using polynomials with a single parameter.

Here we are calling this parameter $t$.
You can think about it as time (e.g. in seconds),
but it doesn't have to represent time.


```python
t = sp.symbols('t')
```

Polynomials typically consist of multiple *terms*.
Each term contains a *basis function*,
which itself contains one or more integer powers of $t$.
The highest power of all terms is called the *degree* of the polynomial.

The arguably simplest set of basis functions
is the *monomial basis*,
which simply consists of all powers of $t$ up to the given degree:


```python
b_monomial = sp.Matrix([t**3, t**2, t, 1]).T
b_monomial
```

In this example we are using polynomials of degree 3,
which are also called *cubic* polynomials.

These basis functions are multiplied by (constant) *coefficients*.
We are writing the coefficients with bold symbols,
because apart from simple scalars (for one-dimensional functions),
these symbols can also represent vectors in two- or three-dimensional space.


```python
coefficients = sp.Matrix(sp.symbols('a:dbm')[::-1])
coefficients
```

We can create a polynomial by
multiplying the basis functions with the coefficients
and then adding all terms:


```python
b_monomial.dot(coefficients)
```

This is a cubic polynomial in its *canonical form*
(because it uses monomial basis functions).

Let's take a closer look at those basis functions
(with some help from [helper.py](helper.py)):


```python
from helper import plot_basis
```


```python
plot_basis(*b_monomial)
```

It doesn't look like much,
but every conceivable cubic polynomial
can be formulated as exactly one linear combination
of those basis functions
(i.e. using one specific list of coefficients).

An example polynomial that's not in canonical form ...


```python
example_polynomial = (2 * t - 1)**3 + (t + 1)**2 - 6 * t + 1
example_polynomial
```


```python
from helper import plot_sympy, grid_lines
```


```python
plot_sympy(example_polynomial, (t, 0, 1))
grid_lines([0, 1], [0, 0.5, 1])
```

... can simply be re-written with monomial basis functions:


```python
example_polynomial.expand()
```

Any polynomial can be rewritten using any set of basis functions
(of appropriate degree).

In later sections we will see more basis functions,
for example those that are used for
[Hermite](hermite-uniform.ipynb),
[Bézier](bezier-de-casteljau.ipynb) and
[Catmull--Rom](catmull-rom-uniform.ipynb) splines.

In the previous example,
we have used scalar coefficients
to create a one-dimensional polynomial.
We can use two-dimensional coefficients
to create two-dimensional polynomial curves.
Let's create a little class to try this:


```python
import numpy as np

class CubicPolynomial:
    
    grid = 0, 1
    
    def __init__(self, d, c, b, a):
        self.coeffs = d, c, b, a

    def evaluate(self, t):
        t = np.expand_dims(t, -1)
        return t**[3, 2, 1, 0] @ self.coeffs
```

<div class="alert alert-info">

Note

The `@` operator is used here to do
[NumPy's matrix multiplication](https://numpy.org/doc/stable/reference/generated/numpy.matmul.html).

</div>

Since this class has the same interface as the splines
that will be discussed in later sections,
we can use a spline helper function from [helper.py](helper.py)
for plotting:


```python
from helper import plot_spline_2d
```


```python
poly_2d = CubicPolynomial([-1.5, 5], [1.5, -8.5], [1, 4], [3, 2])
```


```python
plot_spline_2d(poly_2d, dots_per_second=30, chords=False)
```

This class can also be used with three and more dimensions.
The class [splines.Monomial](../python-module/splines.rst#splines.Monomial)
can be used to try this with arbitrary polynomial degree.

cf7f0195b0e21d1038aa848d8d3e01f0a6cbcb12
ipynb
Jupyter Notebook
doc/euclidean/polynomials.ipynb
AudioSceneDescriptionFormat/splines
0faf87ffddf3be01087959f80d9c43a4da2ae862
MIT
2021-06-21T11:41:02.000Z
2022-02-15T19:18:47.000Z
doc/euclidean/polynomials.ipynb
AudioSceneDescriptionFormat/splines
0faf87ffddf3be01087959f80d9c43a4da2ae862
MIT
2018-07-09T17:47:29.000Z
2022-01-28T19:55:55.000Z
doc/euclidean/polynomials.ipynb
AudioSceneDescriptionFormat/splines
0faf87ffddf3be01087959f80d9c43a4da2ae862
MIT
2020-05-15T18:34:42.000Z
2021-12-28T06:29:54.000Z
Qwen/Qwen-72B
1. YES
2. YES