cxx11_tensor_casts.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 "random_without_cast_overflow.h"
  11. #include <Eigen/CXX11/Tensor>
  12. using Eigen::Tensor;
  13. using Eigen::array;
  14. static void test_simple_cast()
  15. {
  16. Tensor<float, 2> ftensor(20,30);
  17. ftensor = ftensor.random() * 100.f;
  18. Tensor<char, 2> chartensor(20,30);
  19. chartensor.setRandom();
  20. Tensor<std::complex<float>, 2> cplextensor(20,30);
  21. cplextensor.setRandom();
  22. chartensor = ftensor.cast<char>();
  23. cplextensor = ftensor.cast<std::complex<float> >();
  24. for (int i = 0; i < 20; ++i) {
  25. for (int j = 0; j < 30; ++j) {
  26. VERIFY_IS_EQUAL(chartensor(i,j), static_cast<char>(ftensor(i,j)));
  27. VERIFY_IS_EQUAL(cplextensor(i,j), static_cast<std::complex<float> >(ftensor(i,j)));
  28. }
  29. }
  30. }
  31. static void test_vectorized_cast()
  32. {
  33. Tensor<int, 2> itensor(20,30);
  34. itensor = itensor.random() / 1000;
  35. Tensor<float, 2> ftensor(20,30);
  36. ftensor.setRandom();
  37. Tensor<double, 2> dtensor(20,30);
  38. dtensor.setRandom();
  39. ftensor = itensor.cast<float>();
  40. dtensor = itensor.cast<double>();
  41. for (int i = 0; i < 20; ++i) {
  42. for (int j = 0; j < 30; ++j) {
  43. VERIFY_IS_EQUAL(itensor(i,j), static_cast<int>(ftensor(i,j)));
  44. VERIFY_IS_EQUAL(dtensor(i,j), static_cast<double>(ftensor(i,j)));
  45. }
  46. }
  47. }
  48. static void test_float_to_int_cast()
  49. {
  50. Tensor<float, 2> ftensor(20,30);
  51. ftensor = ftensor.random() * 1000.0f;
  52. Tensor<double, 2> dtensor(20,30);
  53. dtensor = dtensor.random() * 1000.0;
  54. Tensor<int, 2> i1tensor = ftensor.cast<int>();
  55. Tensor<int, 2> i2tensor = dtensor.cast<int>();
  56. for (int i = 0; i < 20; ++i) {
  57. for (int j = 0; j < 30; ++j) {
  58. VERIFY_IS_EQUAL(i1tensor(i,j), static_cast<int>(ftensor(i,j)));
  59. VERIFY_IS_EQUAL(i2tensor(i,j), static_cast<int>(dtensor(i,j)));
  60. }
  61. }
  62. }
  63. static void test_big_to_small_type_cast()
  64. {
  65. Tensor<double, 2> dtensor(20, 30);
  66. dtensor.setRandom();
  67. Tensor<float, 2> ftensor(20, 30);
  68. ftensor = dtensor.cast<float>();
  69. for (int i = 0; i < 20; ++i) {
  70. for (int j = 0; j < 30; ++j) {
  71. VERIFY_IS_APPROX(dtensor(i,j), static_cast<double>(ftensor(i,j)));
  72. }
  73. }
  74. }
  75. static void test_small_to_big_type_cast()
  76. {
  77. Tensor<float, 2> ftensor(20, 30);
  78. ftensor.setRandom();
  79. Tensor<double, 2> dtensor(20, 30);
  80. dtensor = ftensor.cast<double>();
  81. for (int i = 0; i < 20; ++i) {
  82. for (int j = 0; j < 30; ++j) {
  83. VERIFY_IS_APPROX(dtensor(i,j), static_cast<double>(ftensor(i,j)));
  84. }
  85. }
  86. }
  87. template <typename FromType, typename ToType>
  88. static void test_type_cast() {
  89. Tensor<FromType, 2> ftensor(100, 200);
  90. // Generate random values for a valid cast.
  91. for (int i = 0; i < 100; ++i) {
  92. for (int j = 0; j < 200; ++j) {
  93. ftensor(i, j) = internal::random_without_cast_overflow<FromType,ToType>::value();
  94. }
  95. }
  96. Tensor<ToType, 2> ttensor(100, 200);
  97. ttensor = ftensor.template cast<ToType>();
  98. for (int i = 0; i < 100; ++i) {
  99. for (int j = 0; j < 200; ++j) {
  100. const ToType ref = internal::cast<FromType,ToType>(ftensor(i, j));
  101. VERIFY_IS_APPROX(ttensor(i, j), ref);
  102. }
  103. }
  104. }
  105. template<typename Scalar, typename EnableIf = void>
  106. struct test_cast_runner {
  107. static void run() {
  108. test_type_cast<Scalar, bool>();
  109. test_type_cast<Scalar, int8_t>();
  110. test_type_cast<Scalar, int16_t>();
  111. test_type_cast<Scalar, int32_t>();
  112. test_type_cast<Scalar, int64_t>();
  113. test_type_cast<Scalar, uint8_t>();
  114. test_type_cast<Scalar, uint16_t>();
  115. test_type_cast<Scalar, uint32_t>();
  116. test_type_cast<Scalar, uint64_t>();
  117. test_type_cast<Scalar, half>();
  118. test_type_cast<Scalar, bfloat16>();
  119. test_type_cast<Scalar, float>();
  120. test_type_cast<Scalar, double>();
  121. test_type_cast<Scalar, std::complex<float>>();
  122. test_type_cast<Scalar, std::complex<double>>();
  123. }
  124. };
  125. // Only certain types allow cast from std::complex<>.
  126. template<typename Scalar>
  127. struct test_cast_runner<Scalar, typename internal::enable_if<NumTraits<Scalar>::IsComplex>::type> {
  128. static void run() {
  129. test_type_cast<Scalar, half>();
  130. test_type_cast<Scalar, bfloat16>();
  131. test_type_cast<Scalar, std::complex<float>>();
  132. test_type_cast<Scalar, std::complex<double>>();
  133. }
  134. };
  135. EIGEN_DECLARE_TEST(cxx11_tensor_casts)
  136. {
  137. CALL_SUBTEST(test_simple_cast());
  138. CALL_SUBTEST(test_vectorized_cast());
  139. CALL_SUBTEST(test_float_to_int_cast());
  140. CALL_SUBTEST(test_big_to_small_type_cast());
  141. CALL_SUBTEST(test_small_to_big_type_cast());
  142. CALL_SUBTEST(test_cast_runner<bool>::run());
  143. CALL_SUBTEST(test_cast_runner<int8_t>::run());
  144. CALL_SUBTEST(test_cast_runner<int16_t>::run());
  145. CALL_SUBTEST(test_cast_runner<int32_t>::run());
  146. CALL_SUBTEST(test_cast_runner<int64_t>::run());
  147. CALL_SUBTEST(test_cast_runner<uint8_t>::run());
  148. CALL_SUBTEST(test_cast_runner<uint16_t>::run());
  149. CALL_SUBTEST(test_cast_runner<uint32_t>::run());
  150. CALL_SUBTEST(test_cast_runner<uint64_t>::run());
  151. CALL_SUBTEST(test_cast_runner<half>::run());
  152. CALL_SUBTEST(test_cast_runner<bfloat16>::run());
  153. CALL_SUBTEST(test_cast_runner<float>::run());
  154. CALL_SUBTEST(test_cast_runner<double>::run());
  155. CALL_SUBTEST(test_cast_runner<std::complex<float>>::run());
  156. CALL_SUBTEST(test_cast_runner<std::complex<double>>::run());
  157. }