diff options
author | Benoit Steiner <benoit.steiner.goog@gmail.com> | 2014-10-01 20:21:42 -0700 |
---|---|---|
committer | Benoit Steiner <benoit.steiner.goog@gmail.com> | 2014-10-01 20:21:42 -0700 |
commit | 1c236f4c9ae78cc58156eebe3b2bb43588897af4 (patch) | |
tree | 11711dc537f76e278bd989701c5b0f1f541d72aa /unsupported | |
parent | 10a79ca3a396f040c2324a5078c7e666bc904bed (diff) |
Added tests for tensors of const values and tensors of stringswwq::
Diffstat (limited to 'unsupported')
-rw-r--r-- | unsupported/Eigen/CXX11/src/Tensor/TensorMorphing.h | 2 | ||||
-rw-r--r-- | unsupported/test/CMakeLists.txt | 2 | ||||
-rw-r--r-- | unsupported/test/cxx11_tensor_of_const_values.cpp | 105 | ||||
-rw-r--r-- | unsupported/test/cxx11_tensor_of_strings.cpp | 142 |
4 files changed, 250 insertions, 1 deletions
diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorMorphing.h b/unsupported/Eigen/CXX11/src/Tensor/TensorMorphing.h index 28ae7b3c6..13109f514 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/TensorMorphing.h +++ b/unsupported/Eigen/CXX11/src/Tensor/TensorMorphing.h @@ -301,7 +301,7 @@ struct TensorEvaluator<const TensorSlicingOp<StartIndices, Sizes, ArgType>, Devi EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar* data) { m_impl.evalSubExprsIfNeeded(NULL); - if (data && m_impl.data()) { + if (internal::is_arithmetic<Scalar>::value && data && m_impl.data()) { Index contiguous_values = 1; for (int i = 0; i < NumDims; ++i) { contiguous_values *= dimensions()[i]; diff --git a/unsupported/test/CMakeLists.txt b/unsupported/test/CMakeLists.txt index 615ff3e6d..8d4e7db66 100644 --- a/unsupported/test/CMakeLists.txt +++ b/unsupported/test/CMakeLists.txt @@ -106,6 +106,8 @@ if(EIGEN_TEST_CXX11) ei_add_test(cxx11_tensor_convolution "-std=c++0x") ei_add_test(cxx11_tensor_expr "-std=c++0x") # ei_add_test(cxx11_tensor_fixed_size "-std=c++0x") + ei_add_test(cxx11_tensor_of_const_values "-std=c++0x") + ei_add_test(cxx11_tensor_of_strings "-std=c++0x") ei_add_test(cxx11_tensor_intdiv "-std=c++0x") ei_add_test(cxx11_tensor_lvalue "-std=c++0x") ei_add_test(cxx11_tensor_map "-std=c++0x") diff --git a/unsupported/test/cxx11_tensor_of_const_values.cpp b/unsupported/test/cxx11_tensor_of_const_values.cpp new file mode 100644 index 000000000..f179a0c21 --- /dev/null +++ b/unsupported/test/cxx11_tensor_of_const_values.cpp @@ -0,0 +1,105 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. +// +// Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com> +// +// This Source Code Form is subject to the terms of the Mozilla +// Public License v. 2.0. If a copy of the MPL was not distributed +// with this file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#include "main.h" + +#include <Eigen/CXX11/Tensor> + +using Eigen::Tensor; +using Eigen::RowMajor; + +static void test_assign() +{ + float data1[6]; + TensorMap<Tensor<const float, 2>> mat1(data1, 2, 3); + float data2[6]; + const TensorMap<Tensor<float, 2>> mat2(data2, 2, 3); + + for (int i = 0; i < 6; ++i) { + data1[i] = i; + data2[i] = -i; + } + + Tensor<float, 2> rslt1; + rslt1 = mat1; + Tensor<float, 2> rslt2; + rslt2 = mat2; + + Tensor<float, 2> rslt3 = mat1; + Tensor<float, 2> rslt4 = mat2; + + Tensor<float, 2> rslt5(mat1); + Tensor<float, 2> rslt6(mat2); + + for (int i = 0; i < 2; ++i) { + for (int j = 0; j < 3; ++j) { + VERIFY_IS_APPROX(rslt1(i,j), static_cast<float>(i + 2*j)); + VERIFY_IS_APPROX(rslt2(i,j), static_cast<float>(-i - 2*j)); + VERIFY_IS_APPROX(rslt3(i,j), static_cast<float>(i + 2*j)); + VERIFY_IS_APPROX(rslt4(i,j), static_cast<float>(-i - 2*j)); + VERIFY_IS_APPROX(rslt5(i,j), static_cast<float>(i + 2*j)); + VERIFY_IS_APPROX(rslt6(i,j), static_cast<float>(-i - 2*j)); + } + } +} + + +static void test_plus() +{ + float data1[6]; + TensorMap<Tensor<const float, 2>> mat1(data1, 2, 3); + float data2[6]; + TensorMap<Tensor<float, 2>> mat2(data2, 2, 3); + + for (int i = 0; i < 6; ++i) { + data1[i] = i; + data2[i] = -i; + } + + Tensor<float, 2> sum1; + sum1 = mat1 + mat2; + Tensor<float, 2> sum2; + sum2 = mat2 + mat1; + + for (int i = 0; i < 2; ++i) { + for (int j = 0; j < 3; ++j) { + VERIFY_IS_APPROX(sum1(i,j), 0.0f); + VERIFY_IS_APPROX(sum2(i,j), 0.0f); + } + } +} + + +static void test_plus_equal() +{ + float data1[6]; + TensorMap<Tensor<const float, 2>> mat1(data1, 2, 3); + float data2[6]; + TensorMap<Tensor<float, 2>> mat2(data2, 2, 3); + + for (int i = 0; i < 6; ++i) { + data1[i] = i; + data2[i] = -i; + } + mat2 += mat1; + + for (int i = 0; i < 2; ++i) { + for (int j = 0; j < 3; ++j) { + VERIFY_IS_APPROX(mat2(i,j), 0.0f); + } + } +} + + +void test_cxx11_tensor_of_const_values() +{ + CALL_SUBTEST(test_assign()); + CALL_SUBTEST(test_plus()); + CALL_SUBTEST(test_plus_equal()); +} diff --git a/unsupported/test/cxx11_tensor_of_strings.cpp b/unsupported/test/cxx11_tensor_of_strings.cpp new file mode 100644 index 000000000..0ffa341c4 --- /dev/null +++ b/unsupported/test/cxx11_tensor_of_strings.cpp @@ -0,0 +1,142 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. +// +// Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com> +// +// This Source Code Form is subject to the terms of the Mozilla +// Public License v. 2.0. If a copy of the MPL was not distributed +// with this file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#include "main.h" +#include <string> +#include <Eigen/CXX11/Tensor> + +using std::string; +using Eigen::Tensor; +using Eigen::TensorMap; + +static void test_assign() +{ + string data1[6]; + TensorMap<Tensor<string, 2>> mat1(data1, 2, 3); + string data2[6]; + const TensorMap<Tensor<const string, 2>> mat2(data2, 2, 3); + + for (int i = 0; i < 6; ++i) { + std::ostringstream s1; + s1 << "abc" << i*3; + data1[i] = s1.str(); + std::ostringstream s2; + s2 << "def" << i*5; + data2[i] = s2.str(); + } + + Tensor<string, 2> rslt1; + rslt1 = mat1; + Tensor<string, 2> rslt2; + rslt2 = mat2; + + Tensor<string, 2> rslt3 = mat1; + Tensor<string, 2> rslt4 = mat2; + + Tensor<string, 2> rslt5(mat1); + Tensor<string, 2> rslt6(mat2); + + for (int i = 0; i < 2; ++i) { + for (int j = 0; j < 3; ++j) { + VERIFY_IS_EQUAL(rslt1(i,j), data1[i+2*j]); + VERIFY_IS_EQUAL(rslt2(i,j), data2[i+2*j]); + VERIFY_IS_EQUAL(rslt3(i,j), data1[i+2*j]); + VERIFY_IS_EQUAL(rslt4(i,j), data2[i+2*j]); + VERIFY_IS_EQUAL(rslt5(i,j), data1[i+2*j]); + VERIFY_IS_EQUAL(rslt6(i,j), data2[i+2*j]); + } + } +} + + +static void test_concat() +{ + Tensor<string, 2> t1(2, 3); + Tensor<string, 2> t2(2, 3); + + for (int i = 0; i < 2; ++i) { + for (int j = 0; j < 3; ++j) { + std::ostringstream s1; + s1 << "abc" << i + j*2; + t1(i, j) = s1.str(); + std::ostringstream s2; + s2 << "def" << i*5 + j*32; + t2(i, j) = s2.str(); + } + } + + Tensor<string, 2> result = t1.concatenate(t2, 1); + VERIFY_IS_EQUAL(result.dimension(0), 2); + VERIFY_IS_EQUAL(result.dimension(1), 6); + + for (int i = 0; i < 2; ++i) { + for (int j = 0; j < 3; ++j) { + VERIFY_IS_EQUAL(result(i, j), t1(i, j)); + VERIFY_IS_EQUAL(result(i, j+3), t2(i, j)); + } + } +} + + +static void test_slices() +{ + Tensor<string, 2> data(2, 6); + for (int i = 0; i < 2; ++i) { + for (int j = 0; j < 3; ++j) { + std::ostringstream s1; + s1 << "abc" << i + j*2; + data(i, j) = s1.str(); + } + } + + const Eigen::DSizes<ptrdiff_t, 2> half_size{{2, 3}}; + const Eigen::DSizes<ptrdiff_t, 2> first_half{{0, 0}}; + const Eigen::DSizes<ptrdiff_t, 2> second_half{{0, 3}}; + + Tensor<string, 2> t1 = data.slice(first_half, half_size); + Tensor<string, 2> t2 = data.slice(second_half, half_size); + + for (int i = 0; i < 2; ++i) { + for (int j = 0; j < 3; ++j) { + VERIFY_IS_EQUAL(data(i, j), t1(i, j)); + VERIFY_IS_EQUAL(data(i, j+3), t2(i, j)); + } + } +} + + +static void test_additions() +{ + Tensor<string, 1> data1(3); + Tensor<string, 1> data2(3); + for (int i = 0; i < 3; ++i) { + data1(i) = "abc"; + std::ostringstream s1; + s1 << i; + data2(i) = s1.str(); + } + + Tensor<string, 1> sum = data1 + data2; + for (int i = 0; i < 3; ++i) { + std::ostringstream concat; + concat << "abc" << i; + string expected = concat.str(); + VERIFY_IS_EQUAL(sum(i), expected); + } +} + + +void test_cxx11_tensor_of_strings() +{ + // Beware: none of this is likely to ever work on a GPU. + CALL_SUBTEST(test_assign()); + CALL_SUBTEST(test_concat()); + CALL_SUBTEST(test_slices()); + CALL_SUBTEST(test_additions()); +} |