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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
/***************************************************************************
* Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
* Copyright (c) QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#include "xtensor/xarray.hpp"
#include "xtensor/xvectorize.hpp"
#include "test_common_macros.hpp"
namespace xt
{
double f1(double d1, double d2)
{
return d1 + d2;
}
using shape_type = dynamic_shape<size_t>;
TEST(xvectorize, function)
{
auto vecf1 = vectorize(f1);
shape_type shape = {3, 2};
xarray<double> a(shape, 1.5);
xarray<double> b(shape, 2.3);
xarray<double> c = vecf1(a, b);
EXPECT_EQ(a(0, 0) + b(0, 0), c(0, 0));
}
TEST(xvectorize, lambda)
{
auto lambda = [](double d1, double d2)
{
return d1 + d2;
};
auto vec_lambda = vectorize(lambda);
shape_type shape = {3, 2};
xarray<double> a(shape, 1.5);
xarray<double> b(shape, 2.3);
xarray<double> c = vec_lambda(a, b);
EXPECT_EQ(a(0, 0) + b(0, 0), c(0, 0));
}
struct func
{
inline double operator()(double d1, double d2) const
{
return d1 + d2;
}
};
TEST(xvectorize, functor)
{
func f;
auto vecfunc = vectorize(f);
shape_type shape = {3, 2};
xarray<double> a(shape, 1.5);
xarray<double> b(shape, 2.3);
xarray<double> c = vecfunc(a, b);
EXPECT_EQ(a(0, 0) + b(0, 0), c(0, 0));
}
TEST(xvectorize, noargument)
{
auto vecfunc = vectorize(
[]
{
return double(1.);
}
);
shape_type shape = {3, 2};
xarray<double> a(shape, 1.5);
a = vecfunc();
EXPECT_EQ(size_t(0), a.dimension());
}
}
|