1 #include "main.h" 2 3 #include <Eigen/CXX11/Tensor> 4 5 using Eigen::Tensor; 6 using Eigen::RowMajor; 7 8 static void test_comparison_sugar() { 9 // we already trust comparisons between tensors, we're simply checking that 10 // the sugared versions are doing the same thing 11 Tensor<int, 3> t(6, 7, 5); 12 13 t.setRandom(); 14 // make sure we have at least one value == 0 15 t(0,0,0) = 0; 16 17 Tensor<bool,0> b; 18 19 #define TEST_TENSOR_EQUAL(e1, e2) \ 20 b = ((e1) == (e2)).all(); \ 21 VERIFY(b()) 22 23 #define TEST_OP(op) TEST_TENSOR_EQUAL(t op 0, t op t.constant(0)) 24 25 TEST_OP(==); 26 TEST_OP(!=); 27 TEST_OP(<=); 28 TEST_OP(>=); 29 TEST_OP(<); 30 TEST_OP(>); 31 #undef TEST_OP 32 #undef TEST_TENSOR_EQUAL 33 } 34 35 36 static void test_scalar_sugar_add_mul() { 37 Tensor<float, 3> A(6, 7, 5); 38 Tensor<float, 3> B(6, 7, 5); 39 A.setRandom(); 40 B.setRandom(); 41 42 const float alpha = 0.43f; 43 const float beta = 0.21f; 44 const float gamma = 0.14f; 45 46 Tensor<float, 3> R = A.constant(gamma) + A * A.constant(alpha) + B * B.constant(beta); 47 Tensor<float, 3> S = A * alpha + B * beta + gamma; 48 Tensor<float, 3> T = gamma + alpha * A + beta * B; 49 50 for (int i = 0; i < 6*7*5; ++i) { 51 VERIFY_IS_APPROX(R(i), S(i)); 52 VERIFY_IS_APPROX(R(i), T(i)); 53 } 54 } 55 56 static void test_scalar_sugar_sub_div() { 57 Tensor<float, 3> A(6, 7, 5); 58 Tensor<float, 3> B(6, 7, 5); 59 A.setRandom(); 60 B.setRandom(); 61 62 const float alpha = 0.43f; 63 const float beta = 0.21f; 64 const float gamma = 0.14f; 65 const float delta = 0.32f; 66 67 Tensor<float, 3> R = A.constant(gamma) - A / A.constant(alpha) 68 - B.constant(beta) / B - A.constant(delta); 69 Tensor<float, 3> S = gamma - A / alpha - beta / B - delta; 70 71 for (int i = 0; i < 6*7*5; ++i) { 72 VERIFY_IS_APPROX(R(i), S(i)); 73 } 74 } 75 76 void test_cxx11_tensor_sugar() 77 { 78 CALL_SUBTEST(test_comparison_sugar()); 79 CALL_SUBTEST(test_scalar_sugar_add_mul()); 80 CALL_SUBTEST(test_scalar_sugar_sub_div()); 81 } 82