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
test_assign()17 static void test_assign()
18 {
19 float data1[6];
20 TensorMap<Tensor<const float, 2>> mat1(data1, 2, 3);
21 float data2[6];
22 const TensorMap<Tensor<float, 2>> mat2(data2, 2, 3);
23
24 for (int i = 0; i < 6; ++i) {
25 data1[i] = i;
26 data2[i] = -i;
27 }
28
29 Tensor<float, 2> rslt1;
30 rslt1 = mat1;
31 Tensor<float, 2> rslt2;
32 rslt2 = mat2;
33
34 Tensor<float, 2> rslt3 = mat1;
35 Tensor<float, 2> rslt4 = mat2;
36
37 Tensor<float, 2> rslt5(mat1);
38 Tensor<float, 2> rslt6(mat2);
39
40 for (int i = 0; i < 2; ++i) {
41 for (int j = 0; j < 3; ++j) {
42 VERIFY_IS_APPROX(rslt1(i,j), static_cast<float>(i + 2*j));
43 VERIFY_IS_APPROX(rslt2(i,j), static_cast<float>(-i - 2*j));
44 VERIFY_IS_APPROX(rslt3(i,j), static_cast<float>(i + 2*j));
45 VERIFY_IS_APPROX(rslt4(i,j), static_cast<float>(-i - 2*j));
46 VERIFY_IS_APPROX(rslt5(i,j), static_cast<float>(i + 2*j));
47 VERIFY_IS_APPROX(rslt6(i,j), static_cast<float>(-i - 2*j));
48 }
49 }
50 }
51
52
test_plus()53 static void test_plus()
54 {
55 float data1[6];
56 TensorMap<Tensor<const float, 2>> mat1(data1, 2, 3);
57 float data2[6];
58 TensorMap<Tensor<float, 2>> mat2(data2, 2, 3);
59
60 for (int i = 0; i < 6; ++i) {
61 data1[i] = i;
62 data2[i] = -i;
63 }
64
65 Tensor<float, 2> sum1;
66 sum1 = mat1 + mat2;
67 Tensor<float, 2> sum2;
68 sum2 = mat2 + mat1;
69
70 for (int i = 0; i < 2; ++i) {
71 for (int j = 0; j < 3; ++j) {
72 VERIFY_IS_APPROX(sum1(i,j), 0.0f);
73 VERIFY_IS_APPROX(sum2(i,j), 0.0f);
74 }
75 }
76 }
77
78
test_plus_equal()79 static void test_plus_equal()
80 {
81 float data1[6];
82 TensorMap<Tensor<const float, 2>> mat1(data1, 2, 3);
83 float data2[6];
84 TensorMap<Tensor<float, 2>> mat2(data2, 2, 3);
85
86 for (int i = 0; i < 6; ++i) {
87 data1[i] = i;
88 data2[i] = -i;
89 }
90 mat2 += mat1;
91
92 for (int i = 0; i < 2; ++i) {
93 for (int j = 0; j < 3; ++j) {
94 VERIFY_IS_APPROX(mat2(i,j), 0.0f);
95 }
96 }
97 }
98
99
test_cxx11_tensor_of_const_values()100 void test_cxx11_tensor_of_const_values()
101 {
102 CALL_SUBTEST(test_assign());
103 CALL_SUBTEST(test_plus());
104 CALL_SUBTEST(test_plus_equal());
105 }
106