cxx11_tensor_padding.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 
12 #include <Eigen/CXX11/Tensor>
13 
14 using Eigen::Tensor;
15 
16 template<int DataLayout>
17 static void test_simple_padding()
18 {
19  Tensor<float, 4, DataLayout> tensor(2,3,5,7);
20  tensor.setRandom();
21 
23  paddings[0] = std::make_pair(0, 0);
24  paddings[1] = std::make_pair(2, 1);
25  paddings[2] = std::make_pair(3, 4);
26  paddings[3] = std::make_pair(0, 0);
27 
29  padded = tensor.pad(paddings);
30 
31  VERIFY_IS_EQUAL(padded.dimension(0), 2+0);
32  VERIFY_IS_EQUAL(padded.dimension(1), 3+3);
33  VERIFY_IS_EQUAL(padded.dimension(2), 5+7);
34  VERIFY_IS_EQUAL(padded.dimension(3), 7+0);
35 
36  for (int i = 0; i < 2; ++i) {
37  for (int j = 0; j < 6; ++j) {
38  for (int k = 0; k < 12; ++k) {
39  for (int l = 0; l < 7; ++l) {
40  if (j >= 2 && j < 5 && k >= 3 && k < 8) {
41  VERIFY_IS_EQUAL(padded(i,j,k,l), tensor(i,j-2,k-3,l));
42  } else {
43  VERIFY_IS_EQUAL(padded(i,j,k,l), 0.0f);
44  }
45  }
46  }
47  }
48  }
49 }
50 
51 template<int DataLayout>
52 static void test_padded_expr()
53 {
54  Tensor<float, 4, DataLayout> tensor(2,3,5,7);
55  tensor.setRandom();
56 
58  paddings[0] = std::make_pair(0, 0);
59  paddings[1] = std::make_pair(2, 1);
60  paddings[2] = std::make_pair(3, 4);
61  paddings[3] = std::make_pair(0, 0);
62 
63  Eigen::DSizes<ptrdiff_t, 2> reshape_dims;
64  reshape_dims[0] = 12;
65  reshape_dims[1] = 84;
66 
68  result = tensor.pad(paddings).reshape(reshape_dims);
69 
70  for (int i = 0; i < 2; ++i) {
71  for (int j = 0; j < 6; ++j) {
72  for (int k = 0; k < 12; ++k) {
73  for (int l = 0; l < 7; ++l) {
74  const float result_value = DataLayout == ColMajor ?
75  result(i+2*j,k+12*l) : result(j+6*i,l+7*k);
76  if (j >= 2 && j < 5 && k >= 3 && k < 8) {
77  VERIFY_IS_EQUAL(result_value, tensor(i,j-2,k-3,l));
78  } else {
79  VERIFY_IS_EQUAL(result_value, 0.0f);
80  }
81  }
82  }
83  }
84  }
85 }
86 
88 {
89  CALL_SUBTEST(test_simple_padding<ColMajor>());
90  CALL_SUBTEST(test_simple_padding<RowMajor>());
91  CALL_SUBTEST(test_padded_expr<ColMajor>());
92  CALL_SUBTEST(test_padded_expr<RowMajor>());
93 }
static void test_padded_expr()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(std::size_t n) const
Definition: Tensor.h:101
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor< Scalar_, NumIndices_, Options_, IndexType_ > & setRandom()
Definition: TensorBase.h:850
static const Line3 l(Rot3(), 1, 1)
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:331
Values result
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const TensorReshapingOp< const NewDimensions, const Tensor< Scalar_, NumIndices_, Options_, IndexType_ > > reshape(const NewDimensions &newDimensions) const
Definition: TensorBase.h:909
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
static void test_simple_padding()
#define CALL_SUBTEST(FUNC)
Definition: main.h:342
void test_cxx11_tensor_padding()
std::ptrdiff_t j
The tensor class.
Definition: Tensor.h:63


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:41:56