|
6 | 6 | [](https://coveralls.io/github/egpbos/xtensor-fftw) |
7 | 7 | [](https://gitter.im/QuantStack/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) |
8 | 8 |
|
9 | | -[fftw](http://www.fftw.org/) bindings for the [xtensor](https://github.com/QuantStack/xtensor) C++ multi-dimensional array library. |
| 9 | +[FFTW](http://www.fftw.org/) bindings for the [xtensor](https://github.com/QuantStack/xtensor) C++ multi-dimensional array library. |
10 | 10 |
|
11 | | -## Example |
| 11 | +## Introduction |
12 | 12 |
|
13 | | -Calculate the derivative of a field `a` in Fourier space, e.g. a sine shaped field: |
| 13 | +_xtensor-fftw_ enables easy access to Fast Fourier Transforms (FFTs) from the [FFTW library](http://www.fftw.org/) for use on `xarray` numerical arrays from the [_xtensor_](https://github.com/QuantStack/xtensor) library. |
14 | 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 | | -``` |
| 15 | +Syntax and functionality are inspired by `numpy.fft`, the FFT module in the Python array programming library [NumPy](http://www.numpy.org/). |
45 | 16 |
|
46 | 17 | ## Installation |
47 | 18 |
|
@@ -81,6 +52,41 @@ Note that _xtensor-fftw_ on Windows does not support `long double` precision. |
81 | 52 | The `long double` precision version of the FFTW library requires that `sizeof(long double) == 12`. |
82 | 53 | In recent versions of Visual Studio, `long double` is an alias of `double` and has size 8. |
83 | 54 |
|
| 55 | +### Example |
| 56 | + |
| 57 | +Calculate the derivative of a (discretized) field in Fourier space, e.g. a sine shaped field `sin`: |
| 58 | + |
| 59 | +```c++ |
| 60 | +#include <xtensor-fftw/basic.hpp> // rfft, irfft |
| 61 | +#include <xtensor-fftw/helper.hpp> // rfftscale |
| 62 | +#include <xtensor/xarray.hpp> |
| 63 | +#include <xtensor/xbuilder.hpp> // xt::arange |
| 64 | +#include <xtensor/xmath.hpp> // xt::sin, cos |
| 65 | +#include <complex> |
| 66 | +#include <xtensor/xio.hpp> |
| 67 | + |
| 68 | +// generate a sinusoid field |
| 69 | +double dx = M_PI/100; |
| 70 | +xt::xarray<double> x = xt::arange(0., 2*M_PI, dx); |
| 71 | +xt::xarray<double> sin = xt::sin(x); |
| 72 | + |
| 73 | +// transform to Fourier space |
| 74 | +auto sin_fs = xt::fftw::rfft(sin); |
| 75 | + |
| 76 | +// multiply by i*k |
| 77 | +std::complex<double> i {0, 1}; |
| 78 | +auto k = xt::fftw::rfftscale<double>(sin.shape()[0], dx); |
| 79 | +xt::xarray< std::complex<double> > sin_derivative_fs = xt::eval(i * k * sin_fs); |
| 80 | + |
| 81 | +// transform back to normal space |
| 82 | +auto sin_derivative = xt::fftw::irfft(sin_derivative_fs); |
| 83 | + |
| 84 | +std::cout << "x: " << x << std::endl; |
| 85 | +std::cout << "sin: " << sin << std::endl; |
| 86 | +std::cout << "cos: " << xt::cos(x) << std::endl; |
| 87 | +std::cout << "sin_derivative: " << sin_derivative << std::endl; |
| 88 | +``` |
| 89 | +
|
84 | 90 |
|
85 | 91 | ## Building and running tests |
86 | 92 |
|
|
0 commit comments