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