#!/usr/bin/env python
# coding: utf-8
# # Notebook for Exercise sheet 2
# Important: run the code cell below once when you just opened this notebook! It will make sure that plotting is enabled.
# In[ ]:
# some setup
get_ipython().run_line_magic('matplotlib', 'inline')
import numpy as np # makes numpy routines and data types available as np.[name ouf routine or data type]
import matplotlib.pyplot as plt # makes plotting command available as plot.[name of command]
# ## H 2 (Dirichlet and Fejér kernel)
#
# Plot the Dirichlet and Fejér kernel for various values of n. Also generate a plot where you plot both kernels in one diagram.
# In[ ]:
# Code for Dirichlet plots
# In[ ]:
#code for Fejer plots
# ## H3 (Interpolation with kernels)
#
# This exercise is about interpolation with the Gauss kernel $k(x,y)=\exp(-\gamma\|x-y\|_2^2)$.
#
# a) You are given a set of sampling points $X = \{x_1,\dots,x_n\}$ with $x_i \in \mathbb R^2$ and function values at these points $f|_X = \{f(x_1),\dots,f(x_n)\}$. Write some code which solves
# $$
# A_{X,X}\alpha = f|_X,
# $$
# where $A_{X,X} = (k(x_i,x_j))_{i,j=1,\dots,n}$ is the kernel matrix. You can use any of the linear algebra routines that come along with numpy. Also write a subroutine which evaluates the interpolant $s_{X,f|_X} = \sum_{j=1}^n \alpha_j k(x_j,\cdot)$ at a given set of points $\{y_1,\dots,y_m\}$.
# In[ ]:
# your code goes here
# b) For each of the following type of sampling points, write a subroutine which generates, for given $m \in \mathbb N$, $m^2$ sampling points:
# * uniform grid points in $[0,1]^2$
# * tensor-product Chebyshev points on $[0,1]^2$ given by
# $$
# (x_i,x_j) = \left( \cos\left(\frac{2i-1}{2m} \pi\right), \cos\left(\frac{2j-1}{2m} \pi\right) \right), \quad i,j=1,\dots,m
# $$
# * Halton points
#
# For the Halton sequence you can use the ghalton package. It is not in the conda package index, but in the pip package index.
# In[ ]:
# Uniform grid points
# In[ ]:
# tensor-product Chebyshev points
# In[ ]:
# Halton points
# c) Use your code to interpolate the function
# $$
# f(x,y) =
# 3(1-x)^2 \exp(-x^2 - (y+1)^2)\\
# - 10(x/5 - x^3 - y^5) \exp(-x^2-y^2)\\
# - \exp(-(x+1)^2 - y^2)/3
# $$
# on $[0,1]$ at $m^2$ uniform grid points, tensor-product Chebyshev points and Halton points for $m=10,20,30,40$ and Gauss parameter $\gamma = 1,2$. Generate plots which show the pointwise error
# $$
# |f(y) - s_{X,f|_X}(y)|
# $$
# for each of type of sampling points. To this end, evaluate $f$ and $s_{X,f|_X}$ on a uniform grid with mesh size $1/1000$.
# In[ ]:
# your code goes here
# d) For each the sampling points used in c) plot the power function
# $$
# P_X^2(x) = k(x,x) - k(x,\cdot)(x)
# $$
# Again, use a uniform grid with mesh size $1/1000$ to generate the plots. Compare the plots with the plots obtained in c).
# In[ ]:
# your code goes here