cxx11_tensor_const.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. #include "main.h"
  10. #include <Eigen/CXX11/Tensor>
  11. using Eigen::Tensor;
  12. static void test_simple_assign()
  13. {
  14. Tensor<int, 3> random(2,3,7);
  15. random.setRandom();
  16. TensorMap<Tensor<const int, 3> > constant(random.data(), 2, 3, 7);
  17. Tensor<int, 3> result(2,3,7);
  18. result = constant;
  19. for (int i = 0; i < 2; ++i) {
  20. for (int j = 0; j < 3; ++j) {
  21. for (int k = 0; k < 7; ++k) {
  22. VERIFY_IS_EQUAL((result(i,j,k)), random(i,j,k));
  23. }
  24. }
  25. }
  26. }
  27. static void test_assign_of_const_tensor()
  28. {
  29. Tensor<int, 3> random(2,3,7);
  30. random.setRandom();
  31. TensorMap<Tensor<const int, 3> > constant1(random.data(), 2, 3, 7);
  32. TensorMap<const Tensor<int, 3> > constant2(random.data(), 2, 3, 7);
  33. const TensorMap<Tensor<int, 3> > constant3(random.data(), 2, 3, 7);
  34. Tensor<int, 2> result1 = constant1.chip(0, 2);
  35. Tensor<int, 2> result2 = constant2.chip(0, 2);
  36. Tensor<int, 2> result3 = constant3.chip(0, 2);
  37. for (int i = 0; i < 2; ++i) {
  38. for (int j = 0; j < 3; ++j) {
  39. VERIFY_IS_EQUAL((result1(i,j)), random(i,j,0));
  40. VERIFY_IS_EQUAL((result2(i,j)), random(i,j,0));
  41. VERIFY_IS_EQUAL((result3(i,j)), random(i,j,0));
  42. }
  43. }
  44. }
  45. EIGEN_DECLARE_TEST(cxx11_tensor_const)
  46. {
  47. CALL_SUBTEST(test_simple_assign());
  48. CALL_SUBTEST(test_assign_of_const_tensor());
  49. }