Skip to content

Commit 3e46b2e

Browse files
committed
Added example to readme (#21)
1 parent cd4f7ef commit 3e46b2e

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,41 @@
88

99
[fftw](http://www.fftw.org/) bindings for the [xtensor](https://github.com/QuantStack/xtensor) C++ multi-dimensional array library.
1010

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+
1146
## Usage
1247
1348
_xtensor-fftw_ is a header-only library.

0 commit comments

Comments
 (0)