cxx11_tensor_generator.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) 2015 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 struct Generator1D {
16 
17  float operator()(const array<Eigen::DenseIndex, 1>& coordinates) const {
18  return coordinates[0];
19  }
20 };
21 
22 template <int DataLayout>
23 static void test_1D()
24 {
25  Tensor<float, 1> vec(6);
26  Tensor<float, 1> result = vec.generate(Generator1D());
27 
28  for (int i = 0; i < 6; ++i) {
30  }
31 }
32 
33 
34 struct Generator2D {
36 
37  float operator()(const array<Eigen::DenseIndex, 2>& coordinates) const {
38  return 3 * coordinates[0] + 11 * coordinates[1];
39  }
40 };
41 
42 template <int DataLayout>
43 static void test_2D()
44 {
45  Tensor<float, 2> matrix(512, 512);
46  Tensor<float, 2> result = matrix.generate(Generator2D());
47 
48  for (int i = 0; i < 512; ++i) {
49  for (int j = 0; j < 512; ++j) {
50  VERIFY_IS_EQUAL(result(i, j), 3*i + 11*j);
51  }
52  }
53 }
54 
55 
56 template <int DataLayout>
57 static void test_gaussian()
58 {
59  int rows = 32;
60  int cols = 48;
62  means[0] = rows / 2.0f;
63  means[1] = cols / 2.0f;
64  array<float, 2> std_devs;
65  std_devs[0] = 3.14f;
66  std_devs[1] = 2.7f;
67  internal::GaussianGenerator<float, Eigen::DenseIndex, 2> gaussian_gen(means, std_devs);
68 
69  Tensor<float, 2> matrix(rows, cols);
70  Tensor<float, 2> result = matrix.generate(gaussian_gen);
71 
72  for (int i = 0; i < rows; ++i) {
73  for (int j = 0; j < cols; ++j) {
74  float g_rows = powf(rows/2.0f - i, 2) / (3.14f * 3.14f) * 0.5f;
75  float g_cols = powf(cols/2.0f - j, 2) / (2.7f * 2.7f) * 0.5f;
76  float gaussian = expf(-g_rows - g_cols);
77  VERIFY_IS_EQUAL(result(i, j), gaussian);
78  }
79  }
80 }
81 
82 
83 EIGEN_DECLARE_TEST(cxx11_tensor_generator)
84 {
85  CALL_SUBTEST(test_1D<ColMajor>());
86  CALL_SUBTEST(test_1D<RowMajor>());
87  CALL_SUBTEST(test_2D<ColMajor>());
88  CALL_SUBTEST(test_2D<RowMajor>());
89  CALL_SUBTEST(test_gaussian<ColMajor>());
90  CALL_SUBTEST(test_gaussian<RowMajor>());
91 }
static void test_gaussian()
float operator()(const array< Eigen::DenseIndex, 2 > &coordinates) const
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
Values result
static void test_1D()
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Point2Pair means(const std::vector< Point2Pair > &abPointPairs)
Calculate the two means of a set of Point2 pairs.
Definition: Point2.cpp:116
static void test_2D()
#define CALL_SUBTEST(FUNC)
Definition: main.h:399
Map< Matrix< T, Dynamic, Dynamic, ColMajor >, 0, OuterStride<> > matrix(T *data, int rows, int cols, int stride)
EIGEN_DECLARE_TEST(cxx11_tensor_generator)
std::ptrdiff_t j
The tensor class.
Definition: Tensor.h:63
float operator()(const array< Eigen::DenseIndex, 1 > &coordinates) const


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:07