Reimplemented the Tensor stream output.

This commit is contained in:
cpp977
2021-11-16 17:36:58 +00:00
committed by Rasmus Munk Larsen
parent 2b9297196c
commit f73c95c032
5 changed files with 498 additions and 166 deletions

View File

@@ -6,131 +6,124 @@
// 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>
template <typename Scalar, int rank, int Layout>
struct test_tensor_ostream_impl {};
template<int DataLayout>
static void test_output_0d()
{
Tensor<int, 0, DataLayout> tensor;
tensor() = 123;
std::stringstream os;
os << tensor;
std::string expected("123");
VERIFY_IS_EQUAL(std::string(os.str()), expected);
}
template<int DataLayout>
static void test_output_1d()
{
Tensor<int, 1, DataLayout> tensor(5);
for (int i = 0; i < 5; ++i) {
tensor(i) = i;
template<typename Scalar, int Layout>
struct test_tensor_ostream_impl<Scalar, 0, Layout> {
static void run() {
Eigen::Tensor<Scalar, 0> t;
t.setValues(1);
std::ostringstream os;
os << t.format(Eigen::TensorIOFormat::Plain());
VERIFY(os.str() == "1");
}
};
std::stringstream os;
os << tensor;
std::string expected("0\n1\n2\n3\n4");
VERIFY_IS_EQUAL(std::string(os.str()), expected);
Eigen::Tensor<double,1,DataLayout> empty_tensor(0);
std::stringstream empty_os;
empty_os << empty_tensor;
std::string empty_string;
VERIFY_IS_EQUAL(std::string(empty_os.str()), empty_string);
}
template<int DataLayout>
static void test_output_2d()
{
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;
}
template<typename Scalar, int Layout>
struct test_tensor_ostream_impl<Scalar, 1, Layout> {
static void run() {
Eigen::Tensor<Scalar, 1> t = {3};
t.setValues({1, 2, 3});
std::ostringstream os;
os << t.format(Eigen::TensorIOFormat::Plain());
VERIFY(os.str() == "1 2 3");
}
};
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);
}
template<int DataLayout>
static void test_output_expr()
{
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;
template<typename Scalar, int Layout>
struct test_tensor_ostream_impl<Scalar, 2, Layout> {
static void run() {
Eigen::Tensor<Scalar, 2> t = {3, 2};
t.setValues({{1, 2}, {3, 4}, {5, 6}});
std::ostringstream os;
os << t.format(Eigen::TensorIOFormat::Plain());
VERIFY(os.str() == "1 2\n3 4\n5 6");
}
};
std::stringstream os;
os << tensor1 + tensor2;
std::string expected(" 7\n 8\n 9\n10\n11");
VERIFY_IS_EQUAL(std::string(os.str()), expected);
}
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;
template<typename Scalar, int Layout>
struct test_tensor_ostream_impl<Scalar, 3, Layout> {
static void run() {
Eigen::Tensor<Scalar, 3> t = {4, 3, 2};
t.setValues({{{1, 2}, {3, 4}, {5, 6}},
{{7, 8}, {9, 10}, {11, 12}},
{{13, 14}, {15, 16}, {17, 18}},
{{19, 20}, {21, 22}, {23, 24}}});
std::ostringstream os;
os << t.format(Eigen::TensorIOFormat::Plain());
VERIFY(os.str() == " 1 2\n 3 4\n 5 6\n\n 7 8\n 9 10\n11 12\n\n13 14\n15 16\n17 18\n\n19 20\n21 22\n23 24");
}
};
TensorMap<Tensor<const int, 1, DataLayout> > tensor_map(tensor.data(), 5);
template<int Layout>
struct test_tensor_ostream_impl<bool, 2, Layout> {
static void run() {
Eigen::Tensor<bool, 2> t = {3, 2};
t.setValues({{false, true}, {true, false}, {false, false}});
std::ostringstream os;
os << t.format(Eigen::TensorIOFormat::Plain());
VERIFY(os.str() == "0 1\n1 0\n0 0");
}
};
std::stringstream os;
os << tensor_map;
template<typename Scalar, int Layout>
struct test_tensor_ostream_impl<std::complex<Scalar>, 2, Layout> {
static void run() {
Eigen::Tensor<std::complex<Scalar>, 2> t = {3, 2};
t.setValues({{std::complex<Scalar>(1, 2), std::complex<Scalar>(12, 3)},
{std::complex<Scalar>(-4, 2), std::complex<Scalar>(0, 5)},
{std::complex<Scalar>(-1, 4), std::complex<Scalar>(5, 27)}});
std::ostringstream os;
os << t.format(Eigen::TensorIOFormat::Plain());
VERIFY(os.str() == " (1,2) (12,3)\n(-4,2) (0,5)\n(-1,4) (5,27)");
}
};
std::string expected("0\n1\n2\n3\n4");
VERIFY_IS_EQUAL(std::string(os.str()), expected);
template <typename Scalar, int rank, int Layout>
void test_tensor_ostream() {
test_tensor_ostream_impl<Scalar, rank, Layout>::run();
}
EIGEN_DECLARE_TEST(cxx11_tensor_io) {
CALL_SUBTEST((test_tensor_ostream<float, 0, Eigen::ColMajor>()));
CALL_SUBTEST((test_tensor_ostream<float, 1, Eigen::ColMajor>()));
CALL_SUBTEST((test_tensor_ostream<float, 2, Eigen::ColMajor>()));
CALL_SUBTEST((test_tensor_ostream<float, 3, Eigen::ColMajor>()));
EIGEN_DECLARE_TEST(cxx11_tensor_io)
{
CALL_SUBTEST(test_output_0d<ColMajor>());
CALL_SUBTEST(test_output_0d<RowMajor>());
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>());
CALL_SUBTEST((test_tensor_ostream<double, 0, Eigen::ColMajor>()));
CALL_SUBTEST((test_tensor_ostream<double, 1, Eigen::ColMajor>()));
CALL_SUBTEST((test_tensor_ostream<double, 2, Eigen::ColMajor>()));
CALL_SUBTEST((test_tensor_ostream<double, 3, Eigen::ColMajor>()));
CALL_SUBTEST((test_tensor_ostream<int, 0, Eigen::ColMajor>()));
CALL_SUBTEST((test_tensor_ostream<int, 1, Eigen::ColMajor>()));
CALL_SUBTEST((test_tensor_ostream<int, 2, Eigen::ColMajor>()));
CALL_SUBTEST((test_tensor_ostream<int, 3, Eigen::ColMajor>()));
CALL_SUBTEST((test_tensor_ostream<float, 0, Eigen::RowMajor>()));
CALL_SUBTEST((test_tensor_ostream<float, 1, Eigen::RowMajor>()));
CALL_SUBTEST((test_tensor_ostream<float, 2, Eigen::RowMajor>()));
CALL_SUBTEST((test_tensor_ostream<float, 3, Eigen::RowMajor>()));
CALL_SUBTEST((test_tensor_ostream<double, 0, Eigen::RowMajor>()));
CALL_SUBTEST((test_tensor_ostream<double, 1, Eigen::RowMajor>()));
CALL_SUBTEST((test_tensor_ostream<double, 2, Eigen::RowMajor>()));
CALL_SUBTEST((test_tensor_ostream<double, 3, Eigen::RowMajor>()));
CALL_SUBTEST((test_tensor_ostream<int, 0, Eigen::RowMajor>()));
CALL_SUBTEST((test_tensor_ostream<int, 1, Eigen::RowMajor>()));
CALL_SUBTEST((test_tensor_ostream<int, 2, Eigen::RowMajor>()));
CALL_SUBTEST((test_tensor_ostream<int, 3, Eigen::RowMajor>()));
CALL_SUBTEST((test_tensor_ostream<bool, 2, Eigen::ColMajor>()));
CALL_SUBTEST((test_tensor_ostream<bool, 2, Eigen::RowMajor>()));
CALL_SUBTEST((test_tensor_ostream<std::complex<double>, 2, Eigen::ColMajor>()));
CALL_SUBTEST((test_tensor_ostream<std::complex<float>, 2, Eigen::ColMajor>()));
}