|
| 1 | +/* |
| 2 | + * xtensor-fftw |
| 3 | + * Copyright (c) 2017, Patrick Bos |
| 4 | + * Distributed under the terms of the BSD 3-Clause License. |
| 5 | + * |
| 6 | + * The full license is in the file LICENSE, distributed with this software. |
| 7 | + */ |
| 8 | + |
| 9 | +// real life examples |
| 10 | + |
| 11 | +#include <xtensor-fftw/basic.hpp> |
| 12 | +#include <xtensor-fftw/helper.hpp> |
| 13 | +#include <xtensor/xarray.hpp> |
| 14 | +#include <complex> |
| 15 | +#include <xtensor/xbuilder.hpp> // xt::arange |
| 16 | +#include <xtensor/xmath.hpp> // xt::sin, cos |
| 17 | +#include <xtensor/xio.hpp> |
| 18 | + |
| 19 | +#include "gtest/gtest.h" |
| 20 | + |
| 21 | + |
| 22 | +TEST(examples, sin_derivative) { |
| 23 | + // generate a sinusoid field |
| 24 | + double dx = M_PI/100; |
| 25 | + xt::xarray<double> x = xt::arange(0., 2*M_PI, dx); |
| 26 | + xt::xarray<double> sin = xt::sin(x); |
| 27 | + |
| 28 | + // transform to Fourier space |
| 29 | + auto sin_fs = xt::fftw::rfft(sin); |
| 30 | + |
| 31 | + // multiply by i*k |
| 32 | + std::complex<double> i {0, 1}; |
| 33 | + auto k = xt::fftw::rfftscale<double>(sin.shape()[0], dx); |
| 34 | + xt::xarray< std::complex<double> > sin_derivative_fs = xt::eval(i * k * sin_fs); |
| 35 | + |
| 36 | + // transform back to normal space |
| 37 | + auto sin_derivative = xt::fftw::irfft(sin_derivative_fs); |
| 38 | + |
| 39 | + EXPECT_TRUE(xt::allclose(xt::cos(x), sin_derivative)); |
| 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 | +} |
0 commit comments