cxx11_tensor_of_strings.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::TensorMap;
16 
17 static void test_assign()
18 {
19  std::string data1[6];
21  std::string data2[6];
22  const TensorMap<Tensor<const std::string, 2>> mat2(data2, 2, 3);
23 
24  for (int i = 0; i < 6; ++i) {
25  std::ostringstream s1;
26  s1 << "abc" << i*3;
27  data1[i] = s1.str();
28  std::ostringstream s2;
29  s2 << "def" << i*5;
30  data2[i] = s2.str();
31  }
32 
34  rslt1 = mat1;
36  rslt2 = mat2;
37 
39  Tensor<std::string, 2> rslt4 = mat2;
40 
42  Tensor<std::string, 2> rslt6(mat2);
43 
44  for (int i = 0; i < 2; ++i) {
45  for (int j = 0; j < 3; ++j) {
46  VERIFY_IS_EQUAL(rslt1(i,j), data1[i+2*j]);
47  VERIFY_IS_EQUAL(rslt2(i,j), data2[i+2*j]);
48  VERIFY_IS_EQUAL(rslt3(i,j), data1[i+2*j]);
49  VERIFY_IS_EQUAL(rslt4(i,j), data2[i+2*j]);
50  VERIFY_IS_EQUAL(rslt5(i,j), data1[i+2*j]);
51  VERIFY_IS_EQUAL(rslt6(i,j), data2[i+2*j]);
52  }
53  }
54 }
55 
56 
57 static void test_concat()
58 {
59  Tensor<std::string, 2> t1(2, 3);
60  Tensor<std::string, 2> t2(2, 3);
61 
62  for (int i = 0; i < 2; ++i) {
63  for (int j = 0; j < 3; ++j) {
64  std::ostringstream s1;
65  s1 << "abc" << i + j*2;
66  t1(i, j) = s1.str();
67  std::ostringstream s2;
68  s2 << "def" << i*5 + j*32;
69  t2(i, j) = s2.str();
70  }
71  }
72 
74  VERIFY_IS_EQUAL(result.dimension(0), 2);
75  VERIFY_IS_EQUAL(result.dimension(1), 6);
76 
77  for (int i = 0; i < 2; ++i) {
78  for (int j = 0; j < 3; ++j) {
79  VERIFY_IS_EQUAL(result(i, j), t1(i, j));
80  VERIFY_IS_EQUAL(result(i, j+3), t2(i, j));
81  }
82  }
83 }
84 
85 
86 static void test_slices()
87 {
89  for (int i = 0; i < 2; ++i) {
90  for (int j = 0; j < 3; ++j) {
91  std::ostringstream s1;
92  s1 << "abc" << i + j*2;
93  data(i, j) = s1.str();
94  }
95  }
96 
97  const Eigen::DSizes<ptrdiff_t, 2> half_size(2, 3);
98  const Eigen::DSizes<ptrdiff_t, 2> first_half(0, 0);
99  const Eigen::DSizes<ptrdiff_t, 2> second_half(0, 3);
100 
101  Tensor<std::string, 2> t1 = data.slice(first_half, half_size);
102  Tensor<std::string, 2> t2 = data.slice(second_half, half_size);
103 
104  for (int i = 0; i < 2; ++i) {
105  for (int j = 0; j < 3; ++j) {
106  VERIFY_IS_EQUAL(data(i, j), t1(i, j));
107  VERIFY_IS_EQUAL(data(i, j+3), t2(i, j));
108  }
109  }
110 }
111 
112 
113 static void test_additions()
114 {
115  Tensor<std::string, 1> data1(3);
116  Tensor<std::string, 1> data2(3);
117  for (int i = 0; i < 3; ++i) {
118  data1(i) = "abc";
119  std::ostringstream s1;
120  s1 << i;
121  data2(i) = s1.str();
122  }
123 
124  Tensor<std::string, 1> sum = data1 + data2;
125  for (int i = 0; i < 3; ++i) {
126  std::ostringstream concat;
127  concat << "abc" << i;
128  std::string expected = concat.str();
129  VERIFY_IS_EQUAL(sum(i), expected);
130  }
131 }
132 
133 
134 static void test_initialization()
135 {
137  a.setConstant(std::string("foo"));
138  for (int i = 0; i < 2*3; ++i) {
139  VERIFY_IS_EQUAL(a(i), std::string("foo"));
140  }
141 }
142 
143 
144 EIGEN_DECLARE_TEST(cxx11_tensor_of_strings)
145 {
146  // Beware: none of this is likely to ever work on a GPU.
152 }
Eigen::Tensor
The tensor class.
Definition: Tensor.h:63
mat1
MatrixXd mat1(size, size)
Eigen::TensorBase< Tensor< Scalar_, NumIndices_, Options_, IndexType_ > >::concatenate
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE TensorConcatenationOp< const Axis, const Tensor< Scalar_, NumIndices_, Options_, IndexType_ >, const OtherDerived > concatenate(const OtherDerived &other, const Axis &axis) const
Definition: TensorBase.h:1044
VERIFY_IS_EQUAL
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
test_assign
static void test_assign()
Definition: cxx11_tensor_of_strings.cpp:17
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(cxx11_tensor_of_strings)
Definition: cxx11_tensor_of_strings.cpp:144
test_additions
static void test_additions()
Definition: cxx11_tensor_of_strings.cpp:113
result
Values result
Definition: OdometryOptimize.cpp:8
test_initialization
static void test_initialization()
Definition: cxx11_tensor_of_strings.cpp:134
Eigen::DSizes
Definition: TensorDimensions.h:263
test_slices
static void test_slices()
Definition: cxx11_tensor_of_strings.cpp:86
data
int data[]
Definition: Map_placement_new.cpp:1
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
cholesky::expected
Matrix expected
Definition: testMatrix.cpp:971
Eigen::TensorMap
A tensor expression mapping an existing array of data.
Definition: TensorForwardDeclarations.h:52
test_concat
static void test_concat()
Definition: cxx11_tensor_of_strings.cpp:57
concat
constexpr descr< 0 > concat()
Definition: descr.h:139
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
main.h
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
CALL_SUBTEST
#define CALL_SUBTEST(FUNC)
Definition: main.h:399


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:02:09