aboutsummaryrefslogtreecommitdiffhomepage
path: root/unsupported/test/cxx11_tensor_scan.cpp
diff options
context:
space:
mode:
authorGravatar Igor Babuschkin <igor@babuschk.in>2016-06-02 13:35:47 +0100
committerGravatar Igor Babuschkin <igor@babuschk.in>2016-06-02 13:35:47 +0100
commitfbd7ed6ff73eca76aa6e0691228d26098ad9c19e (patch)
tree4f860fb483ceb58cf6cbc7f4d834f923ca50d358 /unsupported/test/cxx11_tensor_scan.cpp
parent0ed08fd28180e41838fbe40f8e96c888220895ed (diff)
Add tensor scan op
This is the initial implementation a generic scan operation. Based on this, cumsum and cumprod method have been added to TensorBase.
Diffstat (limited to 'unsupported/test/cxx11_tensor_scan.cpp')
-rw-r--r--unsupported/test/cxx11_tensor_scan.cpp98
1 files changed, 98 insertions, 0 deletions
diff --git a/unsupported/test/cxx11_tensor_scan.cpp b/unsupported/test/cxx11_tensor_scan.cpp
new file mode 100644
index 000000000..dbd3023d7
--- /dev/null
+++ b/unsupported/test/cxx11_tensor_scan.cpp
@@ -0,0 +1,98 @@
+// This file is part of Eigen, a lightweight C++ template library
+// for linear algebra.
+//
+// Copyright (C) 2016 Igor Babuschkin <igor@babuschk.in>
+//
+// 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 <limits>
+#include <numeric>
+#include <Eigen/CXX11/Tensor>
+
+using Eigen::Tensor;
+
+template <int DataLayout, typename Type=float>
+static void test_1d_scan()
+{
+ int size = 50;
+ Tensor<Type, 1, DataLayout> tensor(size);
+ tensor.setRandom();
+ Tensor<Type, 1, DataLayout> result = tensor.cumsum(0);
+
+ VERIFY_IS_EQUAL(tensor.dimension(0), result.dimension(0));
+
+ float accum = 0;
+ for (int i = 0; i < size; i++) {
+ accum += tensor(i);
+ VERIFY_IS_EQUAL(result(i), accum);
+ }
+
+ accum = 1;
+ result = tensor.cumprod(0);
+ for (int i = 0; i < size; i++) {
+ accum *= tensor(i);
+ VERIFY_IS_EQUAL(result(i), accum);
+ }
+}
+
+template <int DataLayout, typename Type=float>
+static void test_4d_scan()
+{
+ int size = 5;
+ Tensor<Type, 4, DataLayout> tensor(size, size, size, size);
+ tensor.setRandom();
+
+ Tensor<Type, 4, DataLayout> result(size, size, size, size);
+
+ result = tensor.cumsum(0);
+ float accum = 0;
+ for (int i = 0; i < size; i++) {
+ accum += tensor(i, 0, 0, 0);
+ VERIFY_IS_EQUAL(result(i, 0, 0, 0), accum);
+ }
+ result = tensor.cumsum(1);
+ accum = 0;
+ for (int i = 0; i < size; i++) {
+ accum += tensor(0, i, 0, 0);
+ VERIFY_IS_EQUAL(result(0, i, 0, 0), accum);
+ }
+ result = tensor.cumsum(2);
+ accum = 0;
+ for (int i = 0; i < size; i++) {
+ accum += tensor(0, 0, i, 0);
+ VERIFY_IS_EQUAL(result(0, 0, i, 0), accum);
+ }
+ result = tensor.cumsum(3);
+ accum = 0;
+ for (int i = 0; i < size; i++) {
+ accum += tensor(0, 0, 0, i);
+ VERIFY_IS_EQUAL(result(0, 0, 0, i), accum);
+ }
+}
+
+template <int DataLayout>
+static void test_tensor_maps() {
+ int inputs[20];
+ TensorMap<Tensor<int, 1, DataLayout> > tensor_map(inputs, 20);
+ tensor_map.setRandom();
+
+ Tensor<int, 1, DataLayout> result = tensor_map.cumsum(0);
+
+ int accum = 0;
+ for (int i = 0; i < 20; ++i) {
+ accum += tensor_map(i);
+ VERIFY_IS_EQUAL(result(i), accum);
+ }
+}
+
+void test_cxx11_tensor_scan() {
+ CALL_SUBTEST(test_1d_scan<ColMajor>());
+ CALL_SUBTEST(test_1d_scan<RowMajor>());
+ CALL_SUBTEST(test_4d_scan<ColMajor>());
+ CALL_SUBTEST(test_4d_scan<RowMajor>());
+ CALL_SUBTEST(test_tensor_maps<ColMajor>());
+ CALL_SUBTEST(test_tensor_maps<RowMajor>());
+}