aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Benoit Steiner <benoit.steiner.goog@gmail.com>2014-10-10 16:17:26 -0700
committerGravatar Benoit Steiner <benoit.steiner.goog@gmail.com>2014-10-10 16:17:26 -0700
commit0219f8aed44279858330b1c07402c066f5b75459 (patch)
tree1447f4ac1f2b6f5aa77db66ea29f5d5ebb273ad9
parent2ed1838aeb6d3c70c35dbd8d545fba1e7e1c68dc (diff)
Added ability to print a tensor using an iostream.
-rw-r--r--unsupported/Eigen/CXX11/Tensor2
-rw-r--r--unsupported/Eigen/CXX11/src/Tensor/TensorIO.h44
-rw-r--r--unsupported/test/CMakeLists.txt1
-rw-r--r--unsupported/test/cxx11_tensor_io.cpp70
4 files changed, 117 insertions, 0 deletions
diff --git a/unsupported/Eigen/CXX11/Tensor b/unsupported/Eigen/CXX11/Tensor
index 5a6246a03..79510fd96 100644
--- a/unsupported/Eigen/CXX11/Tensor
+++ b/unsupported/Eigen/CXX11/Tensor
@@ -64,6 +64,8 @@
#include "unsupported/Eigen/CXX11/src/Tensor/TensorFixedSize.h"
#include "unsupported/Eigen/CXX11/src/Tensor/TensorMap.h"
+#include "unsupported/Eigen/CXX11/src/Tensor/TensorIO.h"
+
#include "Eigen/src/Core/util/ReenableStupidWarnings.h"
#endif // EIGEN_CXX11_TENSOR_MODULE
diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorIO.h b/unsupported/Eigen/CXX11/src/Tensor/TensorIO.h
new file mode 100644
index 000000000..959b5db73
--- /dev/null
+++ b/unsupported/Eigen/CXX11/src/Tensor/TensorIO.h
@@ -0,0 +1,44 @@
+// This file is part of Eigen, a lightweight C++ template library
+// for linear algebra.
+//
+// Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
+//
+// This Source Code Form is subject to the terms of the Mozilla
+// Public License v. 2.0. If a copy of the MPL was not distributed
+// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+#ifndef EIGEN_CXX11_TENSOR_TENSOR_IO_H
+#define EIGEN_CXX11_TENSOR_TENSOR_IO_H
+
+namespace Eigen {
+
+template <typename T>
+std::ostream& operator << (std::ostream& os, const TensorBase<T, ReadOnlyAccessors>& expr) {
+ // Evaluate the expression if needed
+ TensorForcedEvalOp<const T> eval = expr.eval();
+ TensorEvaluator<const TensorForcedEvalOp<const T>, DefaultDevice> tensor(eval, DefaultDevice());
+ tensor.evalSubExprsIfNeeded(NULL);
+
+ typedef typename T::Scalar Scalar;
+ typedef typename T::Index Index;
+ typedef typename TensorEvaluator<const TensorForcedEvalOp<const T>, DefaultDevice>::Dimensions Dimensions;
+ const Index total_size = internal::array_prod(tensor.dimensions());
+
+ // Print the tensor as a 1d vector or a 2d matrix.
+ if (internal::array_size<Dimensions>::value == 1) {
+ Map<Array<Scalar, Dynamic, 1> > array(tensor.data(), total_size);
+ os << array;
+ } else {
+ const Index first_dim = tensor.dimensions()[0];
+ Map<Array<Scalar, Dynamic, Dynamic> > matrix(tensor.data(), first_dim, total_size/first_dim);
+ os << matrix;
+ }
+
+ // Cleanup.
+ tensor.cleanup();
+ return os;
+}
+
+} // end namespace Eigen
+
+#endif // EIGEN_CXX11_TENSOR_TENSOR_IO_H
diff --git a/unsupported/test/CMakeLists.txt b/unsupported/test/CMakeLists.txt
index 48435eb9c..99593b562 100644
--- a/unsupported/test/CMakeLists.txt
+++ b/unsupported/test/CMakeLists.txt
@@ -124,4 +124,5 @@ if(EIGEN_TEST_CXX11)
ei_add_test(cxx11_tensor_striding "-std=c++0x")
# ei_add_test(cxx11_tensor_device "-std=c++0x")
ei_add_test(cxx11_tensor_thread_pool "-std=c++0x")
+ ei_add_test(cxx11_tensor_io "-std=c++0x")
endif()
diff --git a/unsupported/test/cxx11_tensor_io.cpp b/unsupported/test/cxx11_tensor_io.cpp
new file mode 100644
index 000000000..b73c024f5
--- /dev/null
+++ b/unsupported/test/cxx11_tensor_io.cpp
@@ -0,0 +1,70 @@
+// This file is part of Eigen, a lightweight C++ template library
+// for linear algebra.
+//
+// Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
+//
+// This Source Code Form is subject to the terms of the Mozilla
+// Public License v. 2.0. If a copy of the MPL was not distributed
+// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+#include "main.h"
+#include <sstream>
+#include <string>
+#include <Eigen/CXX11/Tensor>
+
+
+static void test_output_1d()
+{
+ Tensor<int, 1> tensor(5);
+ for (int i = 0; i < 5; ++i) {
+ tensor(i) = i;
+ }
+
+ std::stringstream os;
+ os << tensor;
+
+ std::string expected("0\n1\n2\n3\n4");
+ VERIFY_IS_EQUAL(std::string(os.str()), expected);
+}
+
+
+static void test_output_2d()
+{
+ Tensor<int, 2> tensor(5, 3);
+ for (int i = 0; i < 5; ++i) {
+ for (int j = 0; j < 3; ++j) {
+ tensor(i, j) = i*j;
+ }
+ }
+
+ std::stringstream os;
+ os << tensor;
+
+ std::string expected("0 0 0\n0 1 2\n0 2 4\n0 3 6\n0 4 8");
+ VERIFY_IS_EQUAL(std::string(os.str()), expected);
+}
+
+
+static void test_output_expr()
+{
+ Tensor<int, 1> tensor1(5);
+ Tensor<int, 1> tensor2(5);
+ for (int i = 0; i < 5; ++i) {
+ tensor1(i) = i;
+ tensor2(i) = 7;
+ }
+
+ std::stringstream os;
+ os << tensor1 + tensor2;
+
+ std::string expected(" 7\n 8\n 9\n10\n11");
+ VERIFY_IS_EQUAL(std::string(os.str()), expected);
+}
+
+
+void test_cxx11_tensor_io()
+{
+ CALL_SUBTEST(test_output_1d());
+ CALL_SUBTEST(test_output_2d());
+ CALL_SUBTEST(test_output_expr());
+}