# Orthogonal polynomials

Copyright (C) 2020 Andreas Kloeckner

<details>
<summary>MIT License</summary>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
</details>


```python
import numpy as np
import numpy.linalg as la
import matplotlib.pyplot as pt
```

## Mini-Introduction to `sympy`


```python
import sympy as sym

# Enable "pretty-printing" in IPython
sym.init_printing()
```

Make a new `Symbol` and work with it:


```python

```


```python
myexpr = (x**2-3)**2
myexpr
myexpr.expand()
```


```python
sym.integrate(myexpr, x)
```


```python
sym.integrate(myexpr, (x, -1, 1))
```

## Orthogonal polynomials

Now write a function `inner_product(f, g)`:


```python

```

Show that it works:


```python

```


```python

```

Next, define a `basis` consisting of a few monomials:


```python

```

And run Gram-Schmidt on it:


```python
orth_basis = []

for q in basis:
    for prev_q in orth_basis:
        q = q - inner_product(prev_q, q)*prev_q / inner_product(prev_q,prev_q)
    orth_basis.append(q)

legendre_basis = [orth_basis[0],]

#to compute Legendre polynomials need to normalize so that q(1)=1 rather than ||q||=1
for q in orth_basis[1:]:
    q = q / q.subs(x,1)
    legendre_basis.append(q)
```


```python
legendre_basis
```

These are called the *Legendre polynomials*.

--------------------
What do they look like?


```python
mesh = np.linspace(-1, 1, 100)

pt.figure(figsize=(8,8))
for f in legendre_basis:
    f = sym.lambdify(x, f)
    pt.plot(mesh, [f(xi) for xi in mesh])
```

-----
These functions are important enough to be included in `scipy.special` as `eval_legendre`:


```python
import scipy.special as sps

for i in range(10):
    pt.plot(mesh, sps.eval_legendre(i, mesh))
```

What can we find out about the conditioning of the generalized Vandermonde matrix for Legendre polynomials?


```python
#keep
n = 20
xs = np.linspace(-1, 1, n)
V = np.array([
    sps.eval_legendre(i, xs)
    for i in range(n)
]).T

la.cond(V)
```

The Chebyshev basis can similarly be defined by Gram-Schmidt, but now with respect to a different inner-product weight function,
$$w(x) = 1/\sqrt{1-x^2}.$$


```python

```


```python
for i in range(10):
    pt.plot(mesh, np.cos(i*np.arccos(mesh)))
```

Chebyshev polynomials achieve similar good, but imperfect conditioning on a uniform grid (but perfect conditioning on a grid of Chebyshev nodes).


```python
#keep
n = 20
xs = np.linspace(-1, 1, n)
V = np.array([
    np.cos(i*np.arccos(xs))
    for i in range(n)
]).T

la.cond(V)
```


```python

```

aeb5d48d99398b895b8b12191e8997e85b9e1577
ipynb
Jupyter Notebook
cleared-demos/interpolation/Orthogonal Polynomials.ipynb
xywei/numerics-notes
70e67e17d855b7bb06a0de7e3570d40ad50f941b
Unlicense
2021-01-24T21:12:30.000Z
2022-03-02T19:58:25.000Z
cleared-demos/interpolation/Orthogonal Polynomials.ipynb
xywei/numerics-notes
70e67e17d855b7bb06a0de7e3570d40ad50f941b
Unlicense
2021-08-24T17:48:50.000Z
2021-10-14T21:22:02.000Z
cleared-demos/interpolation/Orthogonal Polynomials.ipynb
xywei/numerics-notes
70e67e17d855b7bb06a0de7e3570d40ad50f941b
Unlicense
2020-11-23T09:56:26.000Z
2021-12-24T17:30:26.000Z
Qwen/Qwen-72B
1. YES
2. YES