cxx11_tensor_fixed_size.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 
18 static void test_0d()
19 {
20  TensorFixedSize<float, Sizes<> > scalar1;
21  TensorFixedSize<float, Sizes<>, RowMajor> scalar2;
22  VERIFY_IS_EQUAL(scalar1.rank(), 0);
23  VERIFY_IS_EQUAL(scalar1.size(), 1);
24  VERIFY_IS_EQUAL(array_prod(scalar1.dimensions()), 1);
25 
26  scalar1() = 7.0;
27  scalar2() = 13.0;
28 
29  // Test against shallow copy.
30  TensorFixedSize<float, Sizes<> > copy = scalar1;
31  VERIFY_IS_NOT_EQUAL(scalar1.data(), copy.data());
32  VERIFY_IS_APPROX(scalar1(), copy());
33  copy = scalar1;
34  VERIFY_IS_NOT_EQUAL(scalar1.data(), copy.data());
35  VERIFY_IS_APPROX(scalar1(), copy());
36 
37  TensorFixedSize<float, Sizes<> > scalar3 = scalar1.sqrt();
38  TensorFixedSize<float, Sizes<>, RowMajor> scalar4 = scalar2.sqrt();
39  VERIFY_IS_EQUAL(scalar3.rank(), 0);
40  VERIFY_IS_APPROX(scalar3(), sqrtf(7.0));
41  VERIFY_IS_APPROX(scalar4(), sqrtf(13.0));
42 
43  scalar3 = scalar1 + scalar2;
44  VERIFY_IS_APPROX(scalar3(), 7.0f + 13.0f);
45 }
46 
47 static void test_1d()
48 {
49  TensorFixedSize<float, Sizes<6> > vec1;
50  TensorFixedSize<float, Sizes<6>, RowMajor> vec2;
51 
52  VERIFY_IS_EQUAL((vec1.size()), 6);
53  // VERIFY_IS_EQUAL((vec1.dimensions()[0]), 6);
54  // VERIFY_IS_EQUAL((vec1.dimension(0)), 6);
55 
56  vec1(0) = 4.0; vec2(0) = 0.0;
57  vec1(1) = 8.0; vec2(1) = 1.0;
58  vec1(2) = 15.0; vec2(2) = 2.0;
59  vec1(3) = 16.0; vec2(3) = 3.0;
60  vec1(4) = 23.0; vec2(4) = 4.0;
61  vec1(5) = 42.0; vec2(5) = 5.0;
62 
63  // Test against shallow copy.
64  TensorFixedSize<float, Sizes<6> > copy = vec1;
65  VERIFY_IS_NOT_EQUAL(vec1.data(), copy.data());
66  for (int i = 0; i < 6; ++i) {
67  VERIFY_IS_APPROX(vec1(i), copy(i));
68  }
69  copy = vec1;
70  VERIFY_IS_NOT_EQUAL(vec1.data(), copy.data());
71  for (int i = 0; i < 6; ++i) {
72  VERIFY_IS_APPROX(vec1(i), copy(i));
73  }
74 
75  TensorFixedSize<float, Sizes<6> > vec3 = vec1.sqrt();
76  TensorFixedSize<float, Sizes<6>, RowMajor> vec4 = vec2.sqrt();
77 
78  VERIFY_IS_EQUAL((vec3.size()), 6);
79  VERIFY_IS_EQUAL(vec3.rank(), 1);
80  // VERIFY_IS_EQUAL((vec3.dimensions()[0]), 6);
81  // VERIFY_IS_EQUAL((vec3.dimension(0)), 6);
82 
83  VERIFY_IS_APPROX(vec3(0), sqrtf(4.0));
84  VERIFY_IS_APPROX(vec3(1), sqrtf(8.0));
85  VERIFY_IS_APPROX(vec3(2), sqrtf(15.0));
86  VERIFY_IS_APPROX(vec3(3), sqrtf(16.0));
87  VERIFY_IS_APPROX(vec3(4), sqrtf(23.0));
88  VERIFY_IS_APPROX(vec3(5), sqrtf(42.0));
89 
90  VERIFY_IS_APPROX(vec4(0), sqrtf(0.0));
91  VERIFY_IS_APPROX(vec4(1), sqrtf(1.0));
92  VERIFY_IS_APPROX(vec4(2), sqrtf(2.0));
93  VERIFY_IS_APPROX(vec4(3), sqrtf(3.0));
94  VERIFY_IS_APPROX(vec4(4), sqrtf(4.0));
95  VERIFY_IS_APPROX(vec4(5), sqrtf(5.0));
96 
97  vec3 = vec1 + vec2;
98  VERIFY_IS_APPROX(vec3(0), 4.0f + 0.0f);
99  VERIFY_IS_APPROX(vec3(1), 8.0f + 1.0f);
100  VERIFY_IS_APPROX(vec3(2), 15.0f + 2.0f);
101  VERIFY_IS_APPROX(vec3(3), 16.0f + 3.0f);
102  VERIFY_IS_APPROX(vec3(4), 23.0f + 4.0f);
103  VERIFY_IS_APPROX(vec3(5), 42.0f + 5.0f);
104 }
105 
106 static void test_tensor_map()
107 {
108  TensorFixedSize<float, Sizes<6> > vec1;
109  TensorFixedSize<float, Sizes<6>, RowMajor> vec2;
110 
111  vec1(0) = 4.0; vec2(0) = 0.0;
112  vec1(1) = 8.0; vec2(1) = 1.0;
113  vec1(2) = 15.0; vec2(2) = 2.0;
114  vec1(3) = 16.0; vec2(3) = 3.0;
115  vec1(4) = 23.0; vec2(4) = 4.0;
116  vec1(5) = 42.0; vec2(5) = 5.0;
117 
118  float data3[6];
119  TensorMap<TensorFixedSize<float, Sizes<6> > > vec3(data3, 6);
120  vec3 = vec1.sqrt() + vec2;
121 
122  VERIFY_IS_APPROX(vec3(0), sqrtf(4.0));
123  VERIFY_IS_APPROX(vec3(1), sqrtf(8.0) + 1.0f);
124  VERIFY_IS_APPROX(vec3(2), sqrtf(15.0) + 2.0f);
125  VERIFY_IS_APPROX(vec3(3), sqrtf(16.0) + 3.0f);
126  VERIFY_IS_APPROX(vec3(4), sqrtf(23.0) + 4.0f);
127  VERIFY_IS_APPROX(vec3(5), sqrtf(42.0) + 5.0f);
128 }
129 
130 static void test_2d()
131 {
132  float data1[6];
133  TensorMap<TensorFixedSize<float, Sizes<2, 3> > > mat1(data1,2,3);
134  float data2[6];
135  TensorMap<TensorFixedSize<float, Sizes<2, 3>, RowMajor> > mat2(data2,2,3);
136 
137  VERIFY_IS_EQUAL((mat1.size()), 2*3);
138  VERIFY_IS_EQUAL(mat1.rank(), 2);
139  // VERIFY_IS_EQUAL((mat1.dimension(0)), 2);
140  // VERIFY_IS_EQUAL((mat1.dimension(1)), 3);
141 
142  mat1(0,0) = 0.0;
143  mat1(0,1) = 1.0;
144  mat1(0,2) = 2.0;
145  mat1(1,0) = 3.0;
146  mat1(1,1) = 4.0;
147  mat1(1,2) = 5.0;
148 
149  mat2(0,0) = -0.0;
150  mat2(0,1) = -1.0;
151  mat2(0,2) = -2.0;
152  mat2(1,0) = -3.0;
153  mat2(1,1) = -4.0;
154  mat2(1,2) = -5.0;
155 
156  TensorFixedSize<float, Sizes<2, 3> > mat3;
157  TensorFixedSize<float, Sizes<2, 3>, RowMajor> mat4;
158  mat3 = mat1.abs();
159  mat4 = mat2.abs();
160 
161  VERIFY_IS_EQUAL((mat3.size()), 2*3);
162  // VERIFY_IS_EQUAL((mat3.dimension(0)), 2);
163  // VERIFY_IS_EQUAL((mat3.dimension(1)), 3);
164 
165  VERIFY_IS_APPROX(mat3(0,0), 0.0f);
166  VERIFY_IS_APPROX(mat3(0,1), 1.0f);
167  VERIFY_IS_APPROX(mat3(0,2), 2.0f);
168  VERIFY_IS_APPROX(mat3(1,0), 3.0f);
169  VERIFY_IS_APPROX(mat3(1,1), 4.0f);
170  VERIFY_IS_APPROX(mat3(1,2), 5.0f);
171 
172  VERIFY_IS_APPROX(mat4(0,0), 0.0f);
173  VERIFY_IS_APPROX(mat4(0,1), 1.0f);
174  VERIFY_IS_APPROX(mat4(0,2), 2.0f);
175  VERIFY_IS_APPROX(mat4(1,0), 3.0f);
176  VERIFY_IS_APPROX(mat4(1,1), 4.0f);
177  VERIFY_IS_APPROX(mat4(1,2), 5.0f);
178 }
179 
180 static void test_3d()
181 {
182  TensorFixedSize<float, Sizes<2, 3, 7> > mat1;
183  TensorFixedSize<float, Sizes<2, 3, 7>, RowMajor> mat2;
184 
185  VERIFY_IS_EQUAL((mat1.size()), 2*3*7);
186  VERIFY_IS_EQUAL(mat1.rank(), 3);
187  // VERIFY_IS_EQUAL((mat1.dimension(0)), 2);
188  // VERIFY_IS_EQUAL((mat1.dimension(1)), 3);
189  // VERIFY_IS_EQUAL((mat1.dimension(2)), 7);
190 
191  float val = 0.0f;
192  for (int i = 0; i < 2; ++i) {
193  for (int j = 0; j < 3; ++j) {
194  for (int k = 0; k < 7; ++k) {
195  mat1(i,j,k) = val;
196  mat2(i,j,k) = val;
197  val += 1.0f;
198  }
199  }
200  }
201 
202  TensorFixedSize<float, Sizes<2, 3, 7> > mat3;
203  mat3 = mat1.sqrt();
204  TensorFixedSize<float, Sizes<2, 3, 7>, RowMajor> mat4;
205  mat4 = mat2.sqrt();
206 
207  VERIFY_IS_EQUAL((mat3.size()), 2*3*7);
208  // VERIFY_IS_EQUAL((mat3.dimension(0)), 2);
209  // VERIFY_IS_EQUAL((mat3.dimension(1)), 3);
210  // VERIFY_IS_EQUAL((mat3.dimension(2)), 7);
211 
212 
213  val = 0.0f;
214  for (int i = 0; i < 2; ++i) {
215  for (int j = 0; j < 3; ++j) {
216  for (int k = 0; k < 7; ++k) {
217  VERIFY_IS_APPROX(mat3(i,j,k), sqrtf(val));
218  VERIFY_IS_APPROX(mat4(i,j,k), sqrtf(val));
219  val += 1.0f;
220  }
221  }
222  }
223 }
224 
225 
226 static void test_array()
227 {
228  TensorFixedSize<float, Sizes<2, 3, 7> > mat1;
229  float val = 0.0f;
230  for (int i = 0; i < 2; ++i) {
231  for (int j = 0; j < 3; ++j) {
232  for (int k = 0; k < 7; ++k) {
233  mat1(i,j,k) = val;
234  val += 1.0f;
235  }
236  }
237  }
238 
239  TensorFixedSize<float, Sizes<2, 3, 7> > mat3;
240  mat3 = mat1.pow(3.5f);
241 
242  val = 0.0f;
243  for (int i = 0; i < 2; ++i) {
244  for (int j = 0; j < 3; ++j) {
245  for (int k = 0; k < 7; ++k) {
246  VERIFY_IS_APPROX(mat3(i,j,k), powf(val, 3.5f));
247  val += 1.0f;
248  }
249  }
250  }
251 }
252 
254 {
255  CALL_SUBTEST(test_0d());
256  CALL_SUBTEST(test_1d());
257  CALL_SUBTEST(test_tensor_map());
258  CALL_SUBTEST(test_2d());
259  CALL_SUBTEST(test_3d());
260  CALL_SUBTEST(test_array());
261 }
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t array_prod(const Sizes< Indices... > &)
static int f(const TensorMap< Tensor< int, 3 > > &tensor)
static void test_array()
static void test_1d()
static void test_2d()
static void test_tensor_map()
static void test_3d()
void test_cxx11_tensor_fixed_size()
static void test_0d()
The tensor class.
Definition: Tensor.h:63


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