2015-10-22 02:28:28 +08:00
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
#include <Eigen/CXX11/Tensor>
|
|
|
|
|
|
|
|
using Eigen::Tensor;
|
|
|
|
using Eigen::RowMajor;
|
|
|
|
|
|
|
|
static void test_comparison_sugar() {
|
|
|
|
// we already trust comparisons between tensors, we're simply checking that
|
|
|
|
// the sugared versions are doing the same thing
|
|
|
|
Tensor<int, 3> t(6, 7, 5);
|
|
|
|
|
|
|
|
t.setRandom();
|
|
|
|
// make sure we have at least one value == 0
|
|
|
|
t(0,0,0) = 0;
|
|
|
|
|
2015-11-06 06:22:30 +08:00
|
|
|
Tensor<bool,0> b;
|
2015-10-22 02:28:28 +08:00
|
|
|
|
|
|
|
#define TEST_TENSOR_EQUAL(e1, e2) \
|
|
|
|
b = ((e1) == (e2)).all(); \
|
2015-12-11 14:53:56 +08:00
|
|
|
VERIFY(b())
|
2015-10-22 02:28:28 +08:00
|
|
|
|
|
|
|
#define TEST_OP(op) TEST_TENSOR_EQUAL(t op 0, t op t.constant(0))
|
|
|
|
|
|
|
|
TEST_OP(==);
|
|
|
|
TEST_OP(!=);
|
|
|
|
TEST_OP(<=);
|
|
|
|
TEST_OP(>=);
|
|
|
|
TEST_OP(<);
|
|
|
|
TEST_OP(>);
|
|
|
|
#undef TEST_OP
|
|
|
|
#undef TEST_TENSOR_EQUAL
|
|
|
|
}
|
|
|
|
|
2016-03-08 01:04:27 +08:00
|
|
|
|
2016-06-03 03:41:28 +08:00
|
|
|
static void test_scalar_sugar_add_mul() {
|
2016-03-08 01:04:27 +08:00
|
|
|
Tensor<float, 3> A(6, 7, 5);
|
|
|
|
Tensor<float, 3> B(6, 7, 5);
|
|
|
|
A.setRandom();
|
|
|
|
B.setRandom();
|
|
|
|
|
|
|
|
const float alpha = 0.43f;
|
|
|
|
const float beta = 0.21f;
|
2016-06-03 03:41:28 +08:00
|
|
|
const float gamma = 0.14f;
|
2016-03-08 01:04:27 +08:00
|
|
|
|
2016-06-03 03:41:28 +08:00
|
|
|
Tensor<float, 3> R = A.constant(gamma) + A * A.constant(alpha) + B * B.constant(beta);
|
|
|
|
Tensor<float, 3> S = A * alpha + B * beta + gamma;
|
|
|
|
Tensor<float, 3> T = gamma + alpha * A + beta * B;
|
2016-03-08 01:04:27 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < 6*7*5; ++i) {
|
|
|
|
VERIFY_IS_APPROX(R(i), S(i));
|
2016-06-03 03:41:28 +08:00
|
|
|
VERIFY_IS_APPROX(R(i), T(i));
|
2016-03-08 01:04:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-03 03:41:28 +08:00
|
|
|
static void test_scalar_sugar_sub_div() {
|
|
|
|
Tensor<float, 3> A(6, 7, 5);
|
|
|
|
Tensor<float, 3> B(6, 7, 5);
|
|
|
|
A.setRandom();
|
|
|
|
B.setRandom();
|
|
|
|
|
|
|
|
const float alpha = 0.43f;
|
|
|
|
const float beta = 0.21f;
|
|
|
|
const float gamma = 0.14f;
|
|
|
|
const float delta = 0.32f;
|
|
|
|
|
|
|
|
Tensor<float, 3> R = A.constant(gamma) - A / A.constant(alpha)
|
|
|
|
- B.constant(beta) / B - A.constant(delta);
|
|
|
|
Tensor<float, 3> S = gamma - A / alpha - beta / B - delta;
|
|
|
|
|
|
|
|
for (int i = 0; i < 6*7*5; ++i) {
|
|
|
|
VERIFY_IS_APPROX(R(i), S(i));
|
|
|
|
}
|
|
|
|
}
|
2016-03-08 01:04:27 +08:00
|
|
|
|
2015-10-22 02:28:28 +08:00
|
|
|
void test_cxx11_tensor_sugar()
|
|
|
|
{
|
|
|
|
CALL_SUBTEST(test_comparison_sugar());
|
2016-06-03 03:41:28 +08:00
|
|
|
CALL_SUBTEST(test_scalar_sugar_add_mul());
|
|
|
|
CALL_SUBTEST(test_scalar_sugar_sub_div());
|
2015-10-22 02:28:28 +08:00
|
|
|
}
|