Skip to content

Commit cd4f7ef

Browse files
committed
Added real life example test (derivative of sin)
1 parent 61c5ed6 commit cd4f7ef

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ include_directories(${GTEST_INCLUDE_DIRS})
9797
set(XTENSOR_FFTW_TESTS
9898
basic_interface.cpp
9999
helper.cpp
100+
examples.cpp
100101
)
101102

102103
set(XTENSOR_FFTW_TARGET test_xtensor-fftw)

test/examples.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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

Comments
 (0)