Skip to content

Commit 4bc4c68

Browse files
committed
Readme: add intro and reorder
1 parent f79d8c0 commit 4bc4c68

1 file changed

Lines changed: 39 additions & 33 deletions

File tree

README.md

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,13 @@
66
[![Coverage Status](https://coveralls.io/repos/github/egpbos/xtensor-fftw/badge.svg)](https://coveralls.io/github/egpbos/xtensor-fftw)
77
[![Join the Gitter Chat](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/QuantStack/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
88

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.
1010

11-
## Example
11+
## Introduction
1212

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.
1414

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/).
4516

4617
## Installation
4718

@@ -81,6 +52,41 @@ Note that _xtensor-fftw_ on Windows does not support `long double` precision.
8152
The `long double` precision version of the FFTW library requires that `sizeof(long double) == 12`.
8253
In recent versions of Visual Studio, `long double` is an alias of `double` and has size 8.
8354

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+
8490
8591
## Building and running tests
8692

0 commit comments

Comments
 (0)