cxx11_tensor_of_complex.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 using Eigen::TensorMap;
16 
17 
18 
19 static void test_additions()
20 {
21  Tensor<std::complex<float>, 1> data1(3);
22  Tensor<std::complex<float>, 1> data2(3);
23  for (int i = 0; i < 3; ++i) {
24  data1(i) = std::complex<float>(i, -i);
25  data2(i) = std::complex<float>(i, 7 * i);
26  }
27 
28  Tensor<std::complex<float>, 1> sum = data1 + data2;
29  for (int i = 0; i < 3; ++i) {
30  VERIFY_IS_EQUAL(sum(i), std::complex<float>(2*i, 6*i));
31  }
32 }
33 
34 
35 static void test_abs()
36 {
37  Tensor<std::complex<float>, 1> data1(3);
38  Tensor<std::complex<double>, 1> data2(3);
39  data1.setRandom();
40  data2.setRandom();
41 
42  Tensor<float, 1> abs1 = data1.abs();
43  Tensor<double, 1> abs2 = data2.abs();
44  for (int i = 0; i < 3; ++i) {
45  VERIFY_IS_APPROX(abs1(i), std::abs(data1(i)));
46  VERIFY_IS_APPROX(abs2(i), std::abs(data2(i)));
47  }
48 }
49 
50 
51 static void test_conjugate()
52 {
53  Tensor<std::complex<float>, 1> data1(3);
54  Tensor<std::complex<double>, 1> data2(3);
55  Tensor<int, 1> data3(3);
56  data1.setRandom();
57  data2.setRandom();
58  data3.setRandom();
59 
60  Tensor<std::complex<float>, 1> conj1 = data1.conjugate();
61  Tensor<std::complex<double>, 1> conj2 = data2.conjugate();
62  Tensor<int, 1> conj3 = data3.conjugate();
63  for (int i = 0; i < 3; ++i) {
64  VERIFY_IS_APPROX(conj1(i), std::conj(data1(i)));
65  VERIFY_IS_APPROX(conj2(i), std::conj(data2(i)));
66  VERIFY_IS_APPROX(conj3(i), data3(i));
67  }
68 }
69 
70 static void test_contractions()
71 {
72  Tensor<std::complex<float>, 4> t_left(30, 50, 8, 31);
73  Tensor<std::complex<float>, 5> t_right(8, 31, 7, 20, 10);
74  Tensor<std::complex<float>, 5> t_result(30, 50, 7, 20, 10);
75 
76  t_left.setRandom();
77  t_right.setRandom();
78 
80  MapXcf m_left(t_left.data(), 1500, 248);
81  MapXcf m_right(t_right.data(), 248, 1400);
82  Matrix<std::complex<float>, Dynamic, Dynamic> m_result(1500, 1400);
83 
84  // This contraction should be equivalent to a regular matrix multiplication
87  dims[0] = DimPair(2, 0);
88  dims[1] = DimPair(3, 1);
89  t_result = t_left.contract(t_right, dims);
90  m_result = m_left * m_right;
91  for (int i = 0; i < t_result.dimensions().TotalSize(); i++) {
92  VERIFY_IS_APPROX(t_result.data()[i], m_result.data()[i]);
93  }
94 }
95 
96 
97 EIGEN_DECLARE_TEST(cxx11_tensor_of_complex)
98 {
103 }
Eigen::Tensor
The tensor class.
Definition: Tensor.h:63
test_contractions
static void test_contractions()
Definition: cxx11_tensor_of_complex.cpp:70
Eigen::array
Definition: EmulateArray.h:21
VERIFY_IS_EQUAL
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
abs2
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Abs2ReturnType abs2() const
Definition: ArrayCwiseUnaryOps.h:80
test_conjugate
static void test_conjugate()
Definition: cxx11_tensor_of_complex.cpp:51
Eigen::Tensor::dimensions
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Dimensions & dimensions() const
Definition: Tensor.h:102
Eigen::Dynamic
const int Dynamic
Definition: Constants.h:22
Eigen::TensorMap
A tensor expression mapping an existing array of data.
Definition: TensorForwardDeclarations.h:52
conj
AnnoyingScalar conj(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:104
Eigen::Map
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:94
VERIFY_IS_APPROX
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:15
Eigen::TensorBase< Tensor< Scalar_, NumIndices_, Options_, IndexType_ > >::setRandom
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor< Scalar_, NumIndices_, Options_, IndexType_ > & setRandom()
Definition: TensorBase.h:996
main.h
Eigen::Tensor::data
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar * data()
Definition: Tensor.h:104
Eigen::PlainObjectBase< Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > >::data
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Scalar * data() const
Definition: PlainObjectBase.h:247
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
abs
#define abs(x)
Definition: datatypes.h:17
DimPair
Tensor< float, 1 >::DimensionPair DimPair
Definition: cxx11_tensor_contraction.cpp:17
test_additions
static void test_additions()
Definition: cxx11_tensor_of_complex.cpp:19
test_abs
static void test_abs()
Definition: cxx11_tensor_of_complex.cpp:35
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(cxx11_tensor_of_complex)
Definition: cxx11_tensor_of_complex.cpp:97
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
CALL_SUBTEST
#define CALL_SUBTEST(FUNC)
Definition: main.h:399


gtsam
Author(s):
autogenerated on Fri Nov 1 2024 03:32:21