aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Benoit Steiner <benoit.steiner.goog@gmail.com>2015-01-14 12:43:20 -0800
committerGravatar Benoit Steiner <benoit.steiner.goog@gmail.com>2015-01-14 12:43:20 -0800
commit1a36590e8475f688ef42122c0dd96f7a3b89654e (patch)
tree6940245e9c2fab66e8cb0a81ba31b416fe126967
parent7e0b6c56b45be9adf002e59f97902c8a760519af (diff)
Fixed the printing of RowMajor tensors
-rw-r--r--unsupported/Eigen/CXX11/src/Tensor/TensorIO.h15
-rw-r--r--unsupported/test/cxx11_tensor_io.cpp58
2 files changed, 63 insertions, 10 deletions
diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorIO.h b/unsupported/Eigen/CXX11/src/Tensor/TensorIO.h
index 959b5db73..a9d0f6c39 100644
--- a/unsupported/Eigen/CXX11/src/Tensor/TensorIO.h
+++ b/unsupported/Eigen/CXX11/src/Tensor/TensorIO.h
@@ -12,6 +12,14 @@
namespace Eigen {
+namespace internal {
+template<>
+struct significant_decimals_impl<std::string>
+ : significant_decimals_default_impl<std::string, true>
+{};
+}
+
+
template <typename T>
std::ostream& operator << (std::ostream& os, const TensorBase<T, ReadOnlyAccessors>& expr) {
// Evaluate the expression if needed
@@ -19,18 +27,19 @@ std::ostream& operator << (std::ostream& os, const TensorBase<T, ReadOnlyAccesso
TensorEvaluator<const TensorForcedEvalOp<const T>, DefaultDevice> tensor(eval, DefaultDevice());
tensor.evalSubExprsIfNeeded(NULL);
- typedef typename T::Scalar Scalar;
+ typedef typename internal::remove_const<typename T::Scalar>::type 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);
+ Map<const Array<Scalar, Dynamic, 1> > array(const_cast<Scalar*>(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);
+ static const int layout = TensorEvaluator<const TensorForcedEvalOp<const T>, DefaultDevice>::Layout;
+ Map<const Array<Scalar, Dynamic, Dynamic, layout> > matrix(const_cast<Scalar*>(tensor.data()), first_dim, total_size/first_dim);
os << matrix;
}
diff --git a/unsupported/test/cxx11_tensor_io.cpp b/unsupported/test/cxx11_tensor_io.cpp
index b73c024f5..8bbcf7089 100644
--- a/unsupported/test/cxx11_tensor_io.cpp
+++ b/unsupported/test/cxx11_tensor_io.cpp
@@ -13,9 +13,10 @@
#include <Eigen/CXX11/Tensor>
+template<int DataLayout>
static void test_output_1d()
{
- Tensor<int, 1> tensor(5);
+ Tensor<int, 1, DataLayout> tensor(5);
for (int i = 0; i < 5; ++i) {
tensor(i) = i;
}
@@ -28,9 +29,10 @@ static void test_output_1d()
}
+template<int DataLayout>
static void test_output_2d()
{
- Tensor<int, 2> tensor(5, 3);
+ Tensor<int, 2, DataLayout> tensor(5, 3);
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 3; ++j) {
tensor(i, j) = i*j;
@@ -45,10 +47,11 @@ static void test_output_2d()
}
+template<int DataLayout>
static void test_output_expr()
{
- Tensor<int, 1> tensor1(5);
- Tensor<int, 1> tensor2(5);
+ Tensor<int, 1, DataLayout> tensor1(5);
+ Tensor<int, 1, DataLayout> tensor2(5);
for (int i = 0; i < 5; ++i) {
tensor1(i) = i;
tensor2(i) = 7;
@@ -62,9 +65,50 @@ static void test_output_expr()
}
+template<int DataLayout>
+static void test_output_string()
+{
+ Tensor<std::string, 2, DataLayout> tensor(5, 3);
+ tensor.setConstant(std::string("foo"));
+
+ std::cout << tensor << std::endl;
+
+ std::stringstream os;
+ os << tensor;
+
+ std::string expected("foo foo foo\nfoo foo foo\nfoo foo foo\nfoo foo foo\nfoo foo foo");
+ VERIFY_IS_EQUAL(std::string(os.str()), expected);
+}
+
+
+template<int DataLayout>
+static void test_output_const()
+{
+ Tensor<int, 1, DataLayout> tensor(5);
+ for (int i = 0; i < 5; ++i) {
+ tensor(i) = i;
+ }
+
+ TensorMap<Tensor<const int, 1, DataLayout> > tensor_map(tensor.data(), 5);
+
+ std::stringstream os;
+ os << tensor_map;
+
+ std::string expected("0\n1\n2\n3\n4");
+ 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());
+ CALL_SUBTEST(test_output_1d<ColMajor>());
+ CALL_SUBTEST(test_output_1d<RowMajor>());
+ CALL_SUBTEST(test_output_2d<ColMajor>());
+ CALL_SUBTEST(test_output_2d<RowMajor>());
+ CALL_SUBTEST(test_output_expr<ColMajor>());
+ CALL_SUBTEST(test_output_expr<RowMajor>());
+ CALL_SUBTEST(test_output_string<ColMajor>());
+ CALL_SUBTEST(test_output_string<RowMajor>());
+ CALL_SUBTEST(test_output_const<ColMajor>());
+ CALL_SUBTEST(test_output_const<RowMajor>());
}