cxx11_tensor_io.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 #include <sstream>
12 #include <string>
13 #include <Eigen/CXX11/Tensor>
14 
15 
16 template<int DataLayout>
17 static void test_output_0d()
18 {
20  tensor() = 123;
21 
22  std::stringstream os;
23  os << tensor;
24 
25  std::string expected("123");
26  VERIFY_IS_EQUAL(std::string(os.str()), expected);
27 }
28 
29 
30 template<int DataLayout>
31 static void test_output_1d()
32 {
34  for (int i = 0; i < 5; ++i) {
35  tensor(i) = i;
36  }
37 
38  std::stringstream os;
39  os << tensor;
40 
41  std::string expected("0\n1\n2\n3\n4");
42  VERIFY_IS_EQUAL(std::string(os.str()), expected);
43 
44  Eigen::Tensor<double,1,DataLayout> empty_tensor(0);
45  std::stringstream empty_os;
46  empty_os << empty_tensor;
47  std::string empty_string;
48  VERIFY_IS_EQUAL(std::string(empty_os.str()), empty_string);
49 }
50 
51 
52 template<int DataLayout>
53 static void test_output_2d()
54 {
55  Tensor<int, 2, DataLayout> tensor(5, 3);
56  for (int i = 0; i < 5; ++i) {
57  for (int j = 0; j < 3; ++j) {
58  tensor(i, j) = i*j;
59  }
60  }
61 
62  std::stringstream os;
63  os << tensor;
64 
65  std::string expected("0 0 0\n0 1 2\n0 2 4\n0 3 6\n0 4 8");
66  VERIFY_IS_EQUAL(std::string(os.str()), expected);
67 }
68 
69 
70 template<int DataLayout>
71 static void test_output_expr()
72 {
73  Tensor<int, 1, DataLayout> tensor1(5);
74  Tensor<int, 1, DataLayout> tensor2(5);
75  for (int i = 0; i < 5; ++i) {
76  tensor1(i) = i;
77  tensor2(i) = 7;
78  }
79 
80  std::stringstream os;
81  os << tensor1 + tensor2;
82 
83  std::string expected(" 7\n 8\n 9\n10\n11");
84  VERIFY_IS_EQUAL(std::string(os.str()), expected);
85 }
86 
87 
88 template<int DataLayout>
89 static void test_output_string()
90 {
92  tensor.setConstant(std::string("foo"));
93 
94  std::cout << tensor << std::endl;
95 
96  std::stringstream os;
97  os << tensor;
98 
99  std::string expected("foo foo foo\nfoo foo foo\nfoo foo foo\nfoo foo foo\nfoo foo foo");
100  VERIFY_IS_EQUAL(std::string(os.str()), expected);
101 }
102 
103 
104 template<int DataLayout>
105 static void test_output_const()
106 {
107  Tensor<int, 1, DataLayout> tensor(5);
108  for (int i = 0; i < 5; ++i) {
109  tensor(i) = i;
110  }
111 
112  TensorMap<Tensor<const int, 1, DataLayout> > tensor_map(tensor.data(), 5);
113 
114  std::stringstream os;
115  os << tensor_map;
116 
117  std::string expected("0\n1\n2\n3\n4");
118  VERIFY_IS_EQUAL(std::string(os.str()), expected);
119 }
120 
121 
122 EIGEN_DECLARE_TEST(cxx11_tensor_io)
123 {
124  CALL_SUBTEST(test_output_0d<ColMajor>());
125  CALL_SUBTEST(test_output_0d<RowMajor>());
126  CALL_SUBTEST(test_output_1d<ColMajor>());
127  CALL_SUBTEST(test_output_1d<RowMajor>());
128  CALL_SUBTEST(test_output_2d<ColMajor>());
129  CALL_SUBTEST(test_output_2d<RowMajor>());
130  CALL_SUBTEST(test_output_expr<ColMajor>());
131  CALL_SUBTEST(test_output_expr<RowMajor>());
132  CALL_SUBTEST(test_output_string<ColMajor>());
133  CALL_SUBTEST(test_output_string<RowMajor>());
134  CALL_SUBTEST(test_output_const<ColMajor>());
135  CALL_SUBTEST(test_output_const<RowMajor>());
136 }
Eigen::Tensor
The tensor class.
Definition: Tensor.h:63
test_output_0d
static void test_output_0d()
Definition: cxx11_tensor_io.cpp:17
Eigen::TensorBase< Tensor< Scalar_, NumIndices_, Options_, IndexType_ > >::setConstant
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor< Scalar_, NumIndices_, Options_, IndexType_ > & setConstant(const Scalar &val)
Definition: TensorBase.h:992
VERIFY_IS_EQUAL
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
test_output_const
static void test_output_const()
Definition: cxx11_tensor_io.cpp:105
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(cxx11_tensor_io)
Definition: cxx11_tensor_io.cpp:122
os
ofstream os("timeSchurFactors.csv")
test_output_string
static void test_output_string()
Definition: cxx11_tensor_io.cpp:89
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
test_output_expr
static void test_output_expr()
Definition: cxx11_tensor_io.cpp:71
cholesky::expected
Matrix expected
Definition: testMatrix.cpp:971
Eigen::TensorMap
A tensor expression mapping an existing array of data.
Definition: TensorForwardDeclarations.h:52
main.h
Eigen::Tensor::data
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar * data()
Definition: Tensor.h:104
test_output_2d
static void test_output_2d()
Definition: cxx11_tensor_io.cpp:53
test_output_1d
static void test_output_1d()
Definition: cxx11_tensor_io.cpp:31
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