cxx11_tensor_expr.cpp
Go to the documentation of this file.
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 
10 #include "main.h"
11 
12 #include <Eigen/CXX11/Tensor>
13 
14 using Eigen::Tensor;
15 using Eigen::RowMajor;
16 
17 static void test_1d()
18 {
19  Tensor<float, 1> vec1(6);
21 
22  vec1(0) = 4.0; vec2(0) = 0.0;
23  vec1(1) = 8.0; vec2(1) = 1.0;
24  vec1(2) = 15.0; vec2(2) = 2.0;
25  vec1(3) = 16.0; vec2(3) = 3.0;
26  vec1(4) = 23.0; vec2(4) = 4.0;
27  vec1(5) = 42.0; vec2(5) = 5.0;
28 
29  float data3[6];
30  TensorMap<Tensor<float, 1>> vec3(data3, 6);
31  vec3 = vec1.sqrt();
32  float data4[6];
33  TensorMap<Tensor<float, 1, RowMajor>> vec4(data4, 6);
34  vec4 = vec2.square();
35  float data5[6];
36  TensorMap<Tensor<float, 1, RowMajor>> vec5(data5, 6);
37  vec5 = vec2.cube();
38 
39  VERIFY_IS_APPROX(vec3(0), sqrtf(4.0));
40  VERIFY_IS_APPROX(vec3(1), sqrtf(8.0));
41  VERIFY_IS_APPROX(vec3(2), sqrtf(15.0));
42  VERIFY_IS_APPROX(vec3(3), sqrtf(16.0));
43  VERIFY_IS_APPROX(vec3(4), sqrtf(23.0));
44  VERIFY_IS_APPROX(vec3(5), sqrtf(42.0));
45 
46  VERIFY_IS_APPROX(vec4(0), 0.0f);
47  VERIFY_IS_APPROX(vec4(1), 1.0f);
48  VERIFY_IS_APPROX(vec4(2), 2.0f * 2.0f);
49  VERIFY_IS_APPROX(vec4(3), 3.0f * 3.0f);
50  VERIFY_IS_APPROX(vec4(4), 4.0f * 4.0f);
51  VERIFY_IS_APPROX(vec4(5), 5.0f * 5.0f);
52 
53  VERIFY_IS_APPROX(vec5(0), 0.0f);
54  VERIFY_IS_APPROX(vec5(1), 1.0f);
55  VERIFY_IS_APPROX(vec5(2), 2.0f * 2.0f * 2.0f);
56  VERIFY_IS_APPROX(vec5(3), 3.0f * 3.0f * 3.0f);
57  VERIFY_IS_APPROX(vec5(4), 4.0f * 4.0f * 4.0f);
58  VERIFY_IS_APPROX(vec5(5), 5.0f * 5.0f * 5.0f);
59 
60  vec3 = vec1 + vec2;
61  VERIFY_IS_APPROX(vec3(0), 4.0f + 0.0f);
62  VERIFY_IS_APPROX(vec3(1), 8.0f + 1.0f);
63  VERIFY_IS_APPROX(vec3(2), 15.0f + 2.0f);
64  VERIFY_IS_APPROX(vec3(3), 16.0f + 3.0f);
65  VERIFY_IS_APPROX(vec3(4), 23.0f + 4.0f);
66  VERIFY_IS_APPROX(vec3(5), 42.0f + 5.0f);
67 }
68 
69 static void test_2d()
70 {
71  float data1[6];
72  TensorMap<Tensor<float, 2>> mat1(data1, 2, 3);
73  float data2[6];
74  TensorMap<Tensor<float, 2, RowMajor>> mat2(data2, 2, 3);
75 
76  mat1(0,0) = 0.0;
77  mat1(0,1) = 1.0;
78  mat1(0,2) = 2.0;
79  mat1(1,0) = 3.0;
80  mat1(1,1) = 4.0;
81  mat1(1,2) = 5.0;
82 
83  mat2(0,0) = -0.0;
84  mat2(0,1) = -1.0;
85  mat2(0,2) = -2.0;
86  mat2(1,0) = -3.0;
87  mat2(1,1) = -4.0;
88  mat2(1,2) = -5.0;
89 
90  Tensor<float, 2> mat3(2,3);
92  mat3 = mat1.abs();
93  mat4 = mat2.abs();
94 
95  VERIFY_IS_APPROX(mat3(0,0), 0.0f);
96  VERIFY_IS_APPROX(mat3(0,1), 1.0f);
97  VERIFY_IS_APPROX(mat3(0,2), 2.0f);
98  VERIFY_IS_APPROX(mat3(1,0), 3.0f);
99  VERIFY_IS_APPROX(mat3(1,1), 4.0f);
100  VERIFY_IS_APPROX(mat3(1,2), 5.0f);
101 
102  VERIFY_IS_APPROX(mat4(0,0), 0.0f);
103  VERIFY_IS_APPROX(mat4(0,1), 1.0f);
104  VERIFY_IS_APPROX(mat4(0,2), 2.0f);
105  VERIFY_IS_APPROX(mat4(1,0), 3.0f);
106  VERIFY_IS_APPROX(mat4(1,1), 4.0f);
107  VERIFY_IS_APPROX(mat4(1,2), 5.0f);
108 }
109 
110 static void test_3d()
111 {
112  Tensor<float, 3> mat1(2,3,7);
113  Tensor<float, 3, RowMajor> mat2(2,3,7);
114 
115  float val = 1.0f;
116  for (int i = 0; i < 2; ++i) {
117  for (int j = 0; j < 3; ++j) {
118  for (int k = 0; k < 7; ++k) {
119  mat1(i,j,k) = val;
120  mat2(i,j,k) = val;
121  val += 1.0f;
122  }
123  }
124  }
125 
126  Tensor<float, 3> mat3(2,3,7);
127  mat3 = mat1 + mat1;
128  Tensor<float, 3, RowMajor> mat4(2,3,7);
129  mat4 = mat2 * 3.14f;
130  Tensor<float, 3> mat5(2,3,7);
131  mat5 = mat1.inverse().log();
132  Tensor<float, 3, RowMajor> mat6(2,3,7);
133  mat6 = mat2.pow(0.5f) * 3.14f;
134  Tensor<float, 3> mat7(2,3,7);
135  mat7 = mat1.cwiseMax(mat5 * 2.0f).exp();
136  Tensor<float, 3, RowMajor> mat8(2,3,7);
137  mat8 = (-mat2).exp() * 3.14f;
138  Tensor<float, 3, RowMajor> mat9(2,3,7);
139  mat9 = mat2 + 3.14f;
140  Tensor<float, 3, RowMajor> mat10(2,3,7);
141  mat10 = mat2 - 3.14f;
142  Tensor<float, 3, RowMajor> mat11(2,3,7);
143  mat11 = mat2 / 3.14f;
144 
145  val = 1.0f;
146  for (int i = 0; i < 2; ++i) {
147  for (int j = 0; j < 3; ++j) {
148  for (int k = 0; k < 7; ++k) {
149  VERIFY_IS_APPROX(mat3(i,j,k), val + val);
150  VERIFY_IS_APPROX(mat4(i,j,k), val * 3.14f);
151  VERIFY_IS_APPROX(mat5(i,j,k), logf(1.0f/val));
152  VERIFY_IS_APPROX(mat6(i,j,k), sqrtf(val) * 3.14f);
153  VERIFY_IS_APPROX(mat7(i,j,k), expf((std::max)(val, mat5(i,j,k) * 2.0f)));
154  VERIFY_IS_APPROX(mat8(i,j,k), expf(-val) * 3.14f);
155  VERIFY_IS_APPROX(mat9(i,j,k), val + 3.14f);
156  VERIFY_IS_APPROX(mat10(i,j,k), val - 3.14f);
157  VERIFY_IS_APPROX(mat11(i,j,k), val / 3.14f);
158  val += 1.0f;
159  }
160  }
161  }
162 }
163 
164 static void test_constants()
165 {
166  Tensor<float, 3> mat1(2,3,7);
167  Tensor<float, 3> mat2(2,3,7);
168  Tensor<float, 3> mat3(2,3,7);
169 
170  float val = 1.0f;
171  for (int i = 0; i < 2; ++i) {
172  for (int j = 0; j < 3; ++j) {
173  for (int k = 0; k < 7; ++k) {
174  mat1(i,j,k) = val;
175  val += 1.0f;
176  }
177  }
178  }
179  mat2 = mat1.constant(3.14f);
180  mat3 = mat1.cwiseMax(7.3f).exp();
181 
182  val = 1.0f;
183  for (int i = 0; i < 2; ++i) {
184  for (int j = 0; j < 3; ++j) {
185  for (int k = 0; k < 7; ++k) {
186  VERIFY_IS_APPROX(mat2(i,j,k), 3.14f);
187  VERIFY_IS_APPROX(mat3(i,j,k), expf((std::max)(val, 7.3f)));
188  val += 1.0f;
189  }
190  }
191  }
192 }
193 
194 static void test_boolean()
195 {
196  Tensor<int, 1> vec(6);
197  std::copy_n(std::begin({0, 1, 2, 3, 4, 5}), 6, vec.data());
198 
199  // Test ||.
200  Tensor<bool, 1> bool1 = vec < vec.constant(1) || vec > vec.constant(4);
201  VERIFY_IS_EQUAL(bool1[0], true);
202  VERIFY_IS_EQUAL(bool1[1], false);
203  VERIFY_IS_EQUAL(bool1[2], false);
204  VERIFY_IS_EQUAL(bool1[3], false);
205  VERIFY_IS_EQUAL(bool1[4], false);
206  VERIFY_IS_EQUAL(bool1[5], true);
207 
208  // Test &&, including cast of operand vec.
209  Tensor<bool, 1> bool2 = vec.cast<bool>() && vec < vec.constant(4);
210  VERIFY_IS_EQUAL(bool2[0], false);
211  VERIFY_IS_EQUAL(bool2[1], true);
212  VERIFY_IS_EQUAL(bool2[2], true);
213  VERIFY_IS_EQUAL(bool2[3], true);
214  VERIFY_IS_EQUAL(bool2[4], false);
215  VERIFY_IS_EQUAL(bool2[5], false);
216 
217  // Compilation tests:
218  // Test Tensor<bool> against results of cast or comparison; verifies that
219  // CoeffReturnType is set to match Op return type of bool for Unary and Binary
220  // Ops.
221  Tensor<bool, 1> bool3 = vec.cast<bool>() && bool2;
222  bool3 = vec < vec.constant(4) && bool2;
223 }
224 
225 static void test_functors()
226 {
227  Tensor<float, 3> mat1(2,3,7);
228  Tensor<float, 3> mat2(2,3,7);
229  Tensor<float, 3> mat3(2,3,7);
230 
231  float val = 1.0f;
232  for (int i = 0; i < 2; ++i) {
233  for (int j = 0; j < 3; ++j) {
234  for (int k = 0; k < 7; ++k) {
235  mat1(i,j,k) = val;
236  val += 1.0f;
237  }
238  }
239  }
240  mat2 = mat1.inverse().unaryExpr(&asinf);
241  mat3 = mat1.unaryExpr(&tanhf);
242 
243  val = 1.0f;
244  for (int i = 0; i < 2; ++i) {
245  for (int j = 0; j < 3; ++j) {
246  for (int k = 0; k < 7; ++k) {
247  VERIFY_IS_APPROX(mat2(i,j,k), asinf(1.0f / mat1(i,j,k)));
248  VERIFY_IS_APPROX(mat3(i,j,k), tanhf(mat1(i,j,k)));
249  val += 1.0f;
250  }
251  }
252  }
253 }
254 
255 static void test_type_casting()
256 {
257  Tensor<bool, 3> mat1(2,3,7);
258  Tensor<float, 3> mat2(2,3,7);
259  Tensor<double, 3> mat3(2,3,7);
260  mat1.setRandom();
261  mat2.setRandom();
262 
263  mat3 = mat1.cast<double>();
264  for (int i = 0; i < 2; ++i) {
265  for (int j = 0; j < 3; ++j) {
266  for (int k = 0; k < 7; ++k) {
267  VERIFY_IS_APPROX(mat3(i,j,k), mat1(i,j,k) ? 1.0 : 0.0);
268  }
269  }
270  }
271 
272  mat3 = mat2.cast<double>();
273  for (int i = 0; i < 2; ++i) {
274  for (int j = 0; j < 3; ++j) {
275  for (int k = 0; k < 7; ++k) {
276  VERIFY_IS_APPROX(mat3(i,j,k), static_cast<double>(mat2(i,j,k)));
277  }
278  }
279  }
280 }
281 
282 static void test_select()
283 {
284  Tensor<float, 3> selector(2,3,7);
285  Tensor<float, 3> mat1(2,3,7);
286  Tensor<float, 3> mat2(2,3,7);
287  Tensor<float, 3> result(2,3,7);
288 
289  selector.setRandom();
290  mat1.setRandom();
291  mat2.setRandom();
292  result = (selector > selector.constant(0.5f)).select(mat1, mat2);
293 
294  for (int i = 0; i < 2; ++i) {
295  for (int j = 0; j < 3; ++j) {
296  for (int k = 0; k < 7; ++k) {
297  VERIFY_IS_APPROX(result(i,j,k), (selector(i,j,k) > 0.5f) ? mat1(i,j,k) : mat2(i,j,k));
298  }
299  }
300  }
301 }
302 
303 
305 {
306  CALL_SUBTEST(test_1d());
307  CALL_SUBTEST(test_2d());
308  CALL_SUBTEST(test_3d());
309  CALL_SUBTEST(test_constants());
310  CALL_SUBTEST(test_boolean());
311  CALL_SUBTEST(test_functors());
312  CALL_SUBTEST(test_type_casting());
313  CALL_SUBTEST(test_select());
314 }
static void test_type_casting()
static void test_functors()
EIGEN_DEVICE_FUNC const ExpReturnType exp() const
static void test_select()
static int f(const TensorMap< Tensor< int, 3 > > &tensor)
void test_cxx11_tensor_expr()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor< Scalar_, NumIndices_, Options_, IndexType_ > & setRandom()
Definition: TensorBase.h:848
static void test_1d()
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half() max(const half &a, const half &b)
Definition: Half.h:438
static void test_constants()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar * data()
Definition: Tensor.h:104
static void test_2d()
static void test_boolean()
static void test_3d()
The tensor class.
Definition: Tensor.h:63


hebiros
Author(s): Xavier Artache , Matthew Tesch
autogenerated on Thu Sep 3 2020 04:08:08