Skip to content

Commit

Permalink
Added real life example test (derivative of sin)
Browse files Browse the repository at this point in the history
  • Loading branch information
egpbos committed Nov 29, 2017
1 parent 61c5ed6 commit cd4f7ef
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ include_directories(${GTEST_INCLUDE_DIRS})
set(XTENSOR_FFTW_TESTS
basic_interface.cpp
helper.cpp
examples.cpp
)

set(XTENSOR_FFTW_TARGET test_xtensor-fftw)
Expand Down
44 changes: 44 additions & 0 deletions test/examples.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* xtensor-fftw
* Copyright (c) 2017, Patrick Bos
* Distributed under the terms of the BSD 3-Clause License.
*
* The full license is in the file LICENSE, distributed with this software.
*/

// real life examples

#include <xtensor-fftw/basic.hpp>
#include <xtensor-fftw/helper.hpp>
#include <xtensor/xarray.hpp>
#include <complex>
#include <xtensor/xbuilder.hpp> // xt::arange
#include <xtensor/xmath.hpp> // xt::sin, cos
#include <xtensor/xio.hpp>

#include "gtest/gtest.h"


TEST(examples, sin_derivative) {
// generate a sinusoid field
double dx = M_PI/100;
xt::xarray<double> x = xt::arange(0., 2*M_PI, dx);
xt::xarray<double> sin = xt::sin(x);

// transform to Fourier space
auto sin_fs = xt::fftw::rfft(sin);

// multiply by i*k
std::complex<double> i {0, 1};
auto k = xt::fftw::rfftscale<double>(sin.shape()[0], dx);
xt::xarray< std::complex<double> > sin_derivative_fs = xt::eval(i * k * sin_fs);

// transform back to normal space
auto sin_derivative = xt::fftw::irfft(sin_derivative_fs);

EXPECT_TRUE(xt::allclose(xt::cos(x), sin_derivative));
// std::cout << "x: " << x << std::endl;
// std::cout << "sin: " << sin << std::endl;
// std::cout << "cos: " << xt::cos(x) << std::endl;
// std::cout << "sin_derivative: " << sin_derivative << std::endl;
}

0 comments on commit cd4f7ef

Please sign in to comment.