-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathexamples.cpp
More file actions
47 lines (37 loc) · 1.43 KB
/
Copy pathexamples.cpp
File metadata and controls
47 lines (37 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
* 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
#define _USE_MATH_DEFINES // for MSVC ("Math Constants are not defined in Standard C/C++")
#include <cmath> // M_PI
#include <complex>
#include <xtensor-fftw/basic.hpp>
#include <xtensor-fftw/helper.hpp>
#include <xtensor/xarray.hpp>
#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;
}