menu for today


Chebyshev polynomials (Β½)

Chebyshev polynomials are defined on the interval π‘₯ ∈ [βˆ’1, +1]:

𝑇₀(π‘₯) = 1     𝑇₁(π‘₯) = π‘₯     𝑇ₙ(π‘₯) = 2π‘₯ 𝑇ₙ₋₁(π‘₯) βˆ’ 𝑇ₙ₋₂(π‘₯)

𝑇ₙ(π‘₯) = π‘π‘œπ‘ (𝑛 π‘Žπ‘π‘œπ‘ (π‘₯))

These are orthogonal:

                    ________       / 0    if n != m
βˆ«β‚‹β‚βΊΒΉ 𝑇ₙ(π‘₯)π‘‡β‚˜(π‘₯) / √ 1 βˆ’ π‘₯Β²  𝑑π‘₯ = {  Ο€    if n = m = 0
                                   \ Ο€/2  if n = m != 0

                          / 0   if i != j
βˆ‘ β‚–β‚Œβ‚€α΄Ίβ»ΒΉ 𝑇ᡒ(π‘₯β‚–) 𝑇ⱼ(π‘₯β‚–) = {  𝑁   if i = j = 0
                          \ 𝑁/2 if i = j != 0

π‘₯β‚– = π‘π‘œπ‘ ((2π‘˜ + 1) Ο€ / (2𝑁))     π‘˜ = 0, 1, ... , 𝑁 βˆ’ 1

This is your secret new superpower! Use it to approximate 𝑒π‘₯𝑝(βˆ’π‘₯Β²). How quickly do coefficients of the Chebyshev series vanish?


Chebyshev polynomials (2/2)

So far, we have used Chebyshev polynomials of the first kind, 𝑇ₙ(π‘₯). There are also Chebyshev polynomials of the second kind, π‘ˆβ‚™(π‘₯):

𝑇₀(π‘₯) = 1     𝑇₁(π‘₯) = π‘₯      𝑇ₙ(π‘₯) = 2π‘₯ 𝑇ₙ₋₁(π‘₯) βˆ’ 𝑇ₙ₋₂(π‘₯)
π‘ˆβ‚€(π‘₯) = 1     π‘ˆβ‚(π‘₯) = 2π‘₯     π‘ˆβ‚™(π‘₯) = 2π‘₯ π‘ˆβ‚™β‚‹β‚(π‘₯) βˆ’ π‘ˆβ‚™β‚‹β‚‚(π‘₯)

𝑇ₙ(π‘₯) = π‘π‘œπ‘ (𝑛 π‘Žπ‘π‘œπ‘ (π‘₯))       π‘ˆβ‚™(π‘₯) = 𝑠𝑖𝑛((𝑛 + 1) π‘Žπ‘π‘œπ‘ (π‘₯)) / 𝑠𝑖𝑛(π‘Žπ‘π‘œπ‘ (π‘₯))

There are similar orthogonality relations for π‘ˆβ‚™(π‘₯) - check a reference. Moreover, there are relations for derivatives and integrals:

                                     (𝑛 + 1) π‘‡β‚™β‚Šβ‚(π‘₯) βˆ’ π‘₯π‘ˆβ‚™(π‘₯)) 
πœ•π‘‡β‚™(π‘₯)/πœ•π‘₯ = 𝑛 π‘ˆβ‚™β‚‹β‚(π‘₯)    πœ•π‘ˆβ‚™(π‘₯)/πœ•π‘₯ = -------------------------
                                              (π‘₯Β² βˆ’ 1)

            π‘‡β‚™β‚Šβ‚(π‘₯)     𝑇ₙ₋₁(π‘₯)
βˆ«π‘‡β‚™(π‘₯)𝑑π‘₯ = --------- - ---------    βˆ«π‘ˆβ‚™(π‘₯)𝑑π‘₯ = π‘‡β‚™β‚Šβ‚(π‘₯) / (𝑛 + 1)
           2 (𝑛 + 1)   2 (𝑛 βˆ’ 1)

Chebyshev polynomials for integration

              π‘‡β‚™β‚Šβ‚(π‘₯)     𝑇ₙ₋₁(π‘₯)
∫ 𝑇ₙ(π‘₯) 𝑑π‘₯ = --------- - ---------
             2 (𝑛 + 1)   2 (𝑛 βˆ’ 1)

Use your Chebyshev approximation of 𝑒π‘₯𝑝(βˆ’π‘₯Β²) to derive the integral of the function.

When this is used for numerical integration of functions, it’s also known as Clenshaw-Curtis quadrature.


Chebyshev polynomials for integration: code

You can find the code in C++ in day5-ex1.cxx.

If you look at the coefficients, every other coefficient is zero because 𝑒π‘₯𝑝(βˆ’π‘₯Β²) is an even function. One easy way to deal with this is to exploit this symmetry, and use something like 𝑒π‘₯𝑝(βˆ’(2.5 + 2.5π‘₯)Β²) for π‘₯ > 0 instead.

For high precision work, it’s useful to calculate with higher numerical precision. One could use the long double data type in C/C++, or use an arbitary precision floating point library or calculator (like calc, see day5-ex1.calc).


approximating sin/cos

In many applications, you need both sin(x) and cos(x) with π‘₯ ∈ [βˆ’Ο€, Ο€], and you need it for many different values of x. In the C/C++ standard library, there is the sincos function which calculates both simultaneously. Pick one or more of the following problems:

What’s the accuracy and how does speed compare?


approximating sin/cos: code (Β½)

Example code can be found in day5-ex2.cxx.

In the code, we aim for π’ͺ(10⁻⁷) precision (although not all algorithms reach it). When compiled with g++ (or clang++), results similar to these can be obtained:

routine error sin/cos time/call time/call
(g++) [ns] (clang++) [ns]
libc sin/cos 0/0 20.4 32.7
Cheb. for π‘₯∈[βˆ’Ο€,Ο€] 3.6e-7/3.0e-7 33.7 3.6
Cheb. for sin for π‘₯∈[0,Ο€] 3.0e-7/1.9e-4 50.1 5.4
8-fold arg. doubling, Taylor 8.0e-5/5.6e-5 18.7 3.3
Cheb. for π‘₯∈[0,Ο€/2] 9.5e-7/1.3e-6 29.8 3.7

approximating sin/cos: code (2/2)

In the code, we aim for π’ͺ(10⁻⁷) precision (although not all algorithms reach it). When compiled with g++ (or clang++), results similar to these can be obtained:

routine error sin/cos time/call time/call
(g++) [ns] (clang++) [ns]
libc sin/cos 0/0 20.4 32.7
Cheb. for π‘₯∈[βˆ’Ο€,Ο€] 3.6e-7/3.0e-7 33.7 3.6
Cheb. for sin for π‘₯∈[0,Ο€] 3.0e-7/1.9e-4 50.1 5.4
8-fold arg. doubling, Taylor 8.0e-5/5.6e-5 18.7 3.3
Cheb. for π‘₯∈[0,Ο€/2] 9.5e-7/1.3e-6 29.8 3.7

Observations:


key takeaways