cxx11_tensor_forced_eval.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/Core>
13 #include <Eigen/CXX11/Tensor>
14 
15 using Eigen::MatrixXf;
16 using Eigen::Tensor;
17 
18 static void test_simple()
19 {
20  MatrixXf m1(3,3);
21  MatrixXf m2(3,3);
22  m1.setRandom();
23  m2.setRandom();
24 
25  TensorMap<Tensor<float, 2> > mat1(m1.data(), 3,3);
26  TensorMap<Tensor<float, 2> > mat2(m2.data(), 3,3);
27 
28  Tensor<float, 2> mat3(3,3);
29  mat3 = mat1;
30 
33  dims[0] = DimPair(1, 0);
34 
35  mat3 = mat3.contract(mat2, dims).eval();
36 
37  VERIFY_IS_APPROX(mat3(0, 0), (m1*m2).eval()(0,0));
38  VERIFY_IS_APPROX(mat3(0, 1), (m1*m2).eval()(0,1));
39  VERIFY_IS_APPROX(mat3(0, 2), (m1*m2).eval()(0,2));
40  VERIFY_IS_APPROX(mat3(1, 0), (m1*m2).eval()(1,0));
41  VERIFY_IS_APPROX(mat3(1, 1), (m1*m2).eval()(1,1));
42  VERIFY_IS_APPROX(mat3(1, 2), (m1*m2).eval()(1,2));
43  VERIFY_IS_APPROX(mat3(2, 0), (m1*m2).eval()(2,0));
44  VERIFY_IS_APPROX(mat3(2, 1), (m1*m2).eval()(2,1));
45  VERIFY_IS_APPROX(mat3(2, 2), (m1*m2).eval()(2,2));
46 }
47 
48 
49 static void test_const()
50 {
51  MatrixXf input(3,3);
52  input.setRandom();
53  MatrixXf output = input;
54  output.rowwise() -= input.colwise().maxCoeff();
55 
56  Eigen::array<int, 1> depth_dim;
57  depth_dim[0] = 0;
59  dims2d[0] = 1;
60  dims2d[1] = 3;
62  bcast[0] = 3;
63  bcast[1] = 1;
64  const TensorMap<const Tensor<float, 2> > input_tensor(input.data(), 3, 3);
65  Tensor<float, 2> output_tensor= (input_tensor - input_tensor.maximum(depth_dim).eval().reshape(dims2d).broadcast(bcast));
66 
67  for (int i = 0; i < 3; ++i) {
68  for (int j = 0; j < 3; ++j) {
69  VERIFY_IS_APPROX(output(i, j), output_tensor(i, j));
70  }
71  }
72 }
73 
74 
75 EIGEN_DECLARE_TEST(cxx11_tensor_forced_eval)
76 {
79 }
EIGEN_DECLARE_TEST(cxx11_tensor_forced_eval)
static void test_const()
MatrixType m2(n_dims)
MatrixXd mat1(size, size)
#define VERIFY_IS_APPROX(a, b)
static void test_simple()
A tensor expression mapping an existing array of data.
Matrix3d m1
Definition: IOFormat.cpp:2
Tensor< float, 1 >::DimensionPair DimPair
#define CALL_SUBTEST(FUNC)
Definition: main.h:399
internal::nested_eval< T, 1 >::type eval(const T &xpr)
std::ptrdiff_t j
The tensor class.
Definition: Tensor.h:63


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