#!/usr/bin/env python
# coding: utf-8

# # Jump with Chebyshev Nodes
# 
# 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>

# In[1]:


import numpy as np
import numpy.linalg as la
import matplotlib.pyplot as pt
import scipy.special as sps


# In[2]:


n = 50

k = np.arange(1, n+1, dtype=np.float64)

cheb_nodes = np.cos((2*k-1)/(2*n)*np.pi)
pt.plot(cheb_nodes, 0*cheb_nodes, "o")


# Build the Vandermonde matrix for orthogonal polynomials with Chebyshev nodes:

# In[3]:


V = np.array([
    sps.eval_legendre(i, cheb_nodes)
    for i in range(n)
]).T

la.cond(V)


# Notice the condition number of the Vandermonde matrix! How does that compare to our prior ones?

# In[4]:


def f(x):
    return (x>=0).astype(np.float64)


# In[5]:


coeffs = la.solve(V, f(cheb_nodes))


# In[6]:


x = np.linspace(-1, 1, 1000)


# In[7]:


interpolant = 0
for i in range(n):
    interpolant += coeffs[i]*sps.eval_legendre(i, x)


# In[8]:


pt.plot(x, interpolant)
pt.plot(x, f(x), "--", color="gray")


# In[ ]:




