|
8 | 8 |
|
9 | 9 | [fftw](http://www.fftw.org/) bindings for the [xtensor](https://github.com/QuantStack/xtensor) C++ multi-dimensional array library. |
10 | 10 |
|
| 11 | +## Example |
| 12 | + |
| 13 | +Calculate the derivative of a field `a` in Fourier space, e.g. a sine shaped field: |
| 14 | + |
| 15 | +```c++ |
| 16 | +#include <xtensor-fftw/basic.hpp> // rfft, irfft |
| 17 | +#include <xtensor-fftw/helper.hpp> // rfftscale |
| 18 | +#include <xtensor/xarray.hpp> |
| 19 | +#include <xtensor/xbuilder.hpp> // xt::arange |
| 20 | +#include <xtensor/xmath.hpp> // xt::sin, cos |
| 21 | +#include <complex> |
| 22 | +#include <xtensor/xio.hpp> |
| 23 | + |
| 24 | +// generate a sinusoid field |
| 25 | +double dx = M_PI/100; |
| 26 | +xt::xarray<double> x = xt::arange(0., 2*M_PI, dx); |
| 27 | +xt::xarray<double> sin = xt::sin(x); |
| 28 | + |
| 29 | +// transform to Fourier space |
| 30 | +auto sin_fs = xt::fftw::rfft(sin); |
| 31 | + |
| 32 | +// multiply by i*k |
| 33 | +std::complex<double> i {0, 1}; |
| 34 | +auto k = xt::fftw::rfftscale<double>(sin.shape()[0], dx); |
| 35 | +xt::xarray< std::complex<double> > sin_derivative_fs = xt::eval(i * k * sin_fs); |
| 36 | + |
| 37 | +// transform back to normal space |
| 38 | +auto sin_derivative = xt::fftw::irfft(sin_derivative_fs); |
| 39 | + |
| 40 | +std::cout << "x: " << x << std::endl; |
| 41 | +std::cout << "sin: " << sin << std::endl; |
| 42 | +std::cout << "cos: " << xt::cos(x) << std::endl; |
| 43 | +std::cout << "sin_derivative: " << sin_derivative << std::endl; |
| 44 | +``` |
| 45 | +
|
11 | 46 | ## Usage |
12 | 47 |
|
13 | 48 | _xtensor-fftw_ is a header-only library. |
|
0 commit comments