adjoint.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) 2006-2008 Benoit Jacob <jacob.benoit.1@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 #define EIGEN_NO_STATIC_ASSERT
11 
12 #include "main.h"
13 
14 template<bool IsInteger> struct adjoint_specific;
15 
16 template<> struct adjoint_specific<true> {
17  template<typename Vec, typename Mat, typename Scalar>
18  static void run(const Vec& v1, const Vec& v2, Vec& v3, const Mat& square, Scalar s1, Scalar s2) {
19  VERIFY(test_isApproxWithRef((s1 * v1 + s2 * v2).dot(v3), numext::conj(s1) * v1.dot(v3) + numext::conj(s2) * v2.dot(v3), 0));
20  VERIFY(test_isApproxWithRef(v3.dot(s1 * v1 + s2 * v2), s1*v3.dot(v1)+s2*v3.dot(v2), 0));
21 
22  // check compatibility of dot and adjoint
23  VERIFY(test_isApproxWithRef(v1.dot(square * v2), (square.adjoint() * v1).dot(v2), 0));
24  }
25 };
26 
27 template<> struct adjoint_specific<false> {
28  template<typename Vec, typename Mat, typename Scalar>
29  static void run(const Vec& v1, const Vec& v2, Vec& v3, const Mat& square, Scalar s1, Scalar s2) {
30  typedef typename NumTraits<Scalar>::Real RealScalar;
31  using std::abs;
32 
33  RealScalar ref = NumTraits<Scalar>::IsInteger ? RealScalar(0) : (std::max)((s1 * v1 + s2 * v2).norm(),v3.norm());
34  VERIFY(test_isApproxWithRef((s1 * v1 + s2 * v2).dot(v3), numext::conj(s1) * v1.dot(v3) + numext::conj(s2) * v2.dot(v3), ref));
35  VERIFY(test_isApproxWithRef(v3.dot(s1 * v1 + s2 * v2), s1*v3.dot(v1)+s2*v3.dot(v2), ref));
36 
37  VERIFY_IS_APPROX(v1.squaredNorm(), v1.norm() * v1.norm());
38  // check normalized() and normalize()
39  VERIFY_IS_APPROX(v1, v1.norm() * v1.normalized());
40  v3 = v1;
41  v3.normalize();
42  VERIFY_IS_APPROX(v1, v1.norm() * v3);
43  VERIFY_IS_APPROX(v3, v1.normalized());
44  VERIFY_IS_APPROX(v3.norm(), RealScalar(1));
45 
46  // check null inputs
47  VERIFY_IS_APPROX((v1*0).normalized(), (v1*0));
48 #if (!EIGEN_ARCH_i386) || defined(EIGEN_VECTORIZE)
50  VERIFY( (v1*very_small).norm() == 0 );
51  VERIFY_IS_APPROX((v1*very_small).normalized(), (v1*very_small));
52  v3 = v1*very_small;
53  v3.normalize();
54  VERIFY_IS_APPROX(v3, (v1*very_small));
55 #endif
56 
57  // check compatibility of dot and adjoint
58  ref = NumTraits<Scalar>::IsInteger ? 0 : (std::max)((std::max)(v1.norm(),v2.norm()),(std::max)((square * v2).norm(),(square.adjoint() * v1).norm()));
59  VERIFY(internal::isMuchSmallerThan(abs(v1.dot(square * v2) - (square.adjoint() * v1).dot(v2)), ref, test_precision<Scalar>()));
60 
61  // check that Random().normalized() works: tricky as the random xpr must be evaluated by
62  // normalized() in order to produce a consistent result.
63  VERIFY_IS_APPROX(Vec::Random(v1.size()).normalized().norm(), RealScalar(1));
64  }
65 };
66 
67 template<typename MatrixType> void adjoint(const MatrixType& m)
68 {
69  /* this test covers the following files:
70  Transpose.h Conjugate.h Dot.h
71  */
72  using std::abs;
73  typedef typename MatrixType::Scalar Scalar;
74  typedef typename NumTraits<Scalar>::Real RealScalar;
78 
79  Index rows = m.rows();
80  Index cols = m.cols();
81 
82  MatrixType m1 = MatrixType::Random(rows, cols),
83  m2 = MatrixType::Random(rows, cols),
84  m3(rows, cols),
85  square = SquareMatrixType::Random(rows, rows);
86  VectorType v1 = VectorType::Random(rows),
87  v2 = VectorType::Random(rows),
88  v3 = VectorType::Random(rows),
89  vzero = VectorType::Zero(rows);
90 
91  Scalar s1 = internal::random<Scalar>(),
92  s2 = internal::random<Scalar>();
93 
94  // check basic compatibility of adjoint, transpose, conjugate
95  VERIFY_IS_APPROX(m1.transpose().conjugate().adjoint(), m1);
96  VERIFY_IS_APPROX(m1.adjoint().conjugate().transpose(), m1);
97 
98  // check multiplicative behavior
99  VERIFY_IS_APPROX((m1.adjoint() * m2).adjoint(), m2.adjoint() * m1);
100  VERIFY_IS_APPROX((s1 * m1).adjoint(), numext::conj(s1) * m1.adjoint());
101 
102  // check basic properties of dot, squaredNorm
103  VERIFY_IS_APPROX(numext::conj(v1.dot(v2)), v2.dot(v1));
104  VERIFY_IS_APPROX(numext::real(v1.dot(v1)), v1.squaredNorm());
105 
107 
108  VERIFY_IS_MUCH_SMALLER_THAN(abs(vzero.dot(v1)), static_cast<RealScalar>(1));
109 
110  // like in testBasicStuff, test operator() to check const-qualification
111  Index r = internal::random<Index>(0, rows-1),
112  c = internal::random<Index>(0, cols-1);
113  VERIFY_IS_APPROX(m1.conjugate()(r,c), numext::conj(m1(r,c)));
114  VERIFY_IS_APPROX(m1.adjoint()(c,r), numext::conj(m1(r,c)));
115 
116  // check inplace transpose
117  m3 = m1;
118  m3.transposeInPlace();
119  VERIFY_IS_APPROX(m3,m1.transpose());
120  m3.transposeInPlace();
122 
123  if(PacketSize<m3.rows() && PacketSize<m3.cols())
124  {
125  m3 = m1;
126  Index i = internal::random<Index>(0,m3.rows()-PacketSize);
127  Index j = internal::random<Index>(0,m3.cols()-PacketSize);
128  m3.template block<PacketSize,PacketSize>(i,j).transposeInPlace();
129  VERIFY_IS_APPROX( (m3.template block<PacketSize,PacketSize>(i,j)), (m1.template block<PacketSize,PacketSize>(i,j).transpose()) );
130  m3.template block<PacketSize,PacketSize>(i,j).transposeInPlace();
132  }
133 
134  // check inplace adjoint
135  m3 = m1;
136  m3.adjointInPlace();
137  VERIFY_IS_APPROX(m3,m1.adjoint());
138  m3.transposeInPlace();
139  VERIFY_IS_APPROX(m3,m1.conjugate());
140 
141  // check mixed dot product
143  RealVectorType rv1 = RealVectorType::Random(rows);
144  VERIFY_IS_APPROX(v1.dot(rv1.template cast<Scalar>()), v1.dot(rv1));
145  VERIFY_IS_APPROX(rv1.template cast<Scalar>().dot(v1), rv1.dot(v1));
146 
147  VERIFY( is_same_type(m1,m1.template conjugateIf<false>()) );
148  VERIFY( is_same_type(m1.conjugate(),m1.template conjugateIf<true>()) );
149 }
150 
151 template<int>
153 {
154  MatrixXcf a(10,10), b(10,10);
155  VERIFY_RAISES_ASSERT(a = a.transpose());
156  VERIFY_RAISES_ASSERT(a = a.transpose() + b);
157  VERIFY_RAISES_ASSERT(a = b + a.transpose());
158  VERIFY_RAISES_ASSERT(a = a.conjugate().transpose());
159  VERIFY_RAISES_ASSERT(a = a.adjoint());
160  VERIFY_RAISES_ASSERT(a = a.adjoint() + b);
161  VERIFY_RAISES_ASSERT(a = b + a.adjoint());
162 
163  // no assertion should be triggered for these cases:
164  a.transpose() = a.transpose();
165  a.transpose() += a.transpose();
166  a.transpose() += a.transpose() + b;
167  a.transpose() = a.adjoint();
168  a.transpose() += a.adjoint();
169  a.transpose() += a.adjoint() + b;
170 
171  // regression tests for check_for_aliasing
172  MatrixXd c(10,10);
173  c = 1.0 * MatrixXd::Ones(10,10) + c;
174  c = MatrixXd::Ones(10,10) * 1.0 + c;
175  c = c + MatrixXd::Ones(10,10) .cwiseProduct( MatrixXd::Zero(10,10) );
176  c = MatrixXd::Ones(10,10) * MatrixXd::Zero(10,10);
177 
178  // regression for bug 1646
179  for (int j = 0; j < 10; ++j) {
180  c.col(j).head(j) = c.row(j).head(j);
181  }
182 
183  for (int j = 0; j < 10; ++j) {
184  c.col(j) = c.row(j);
185  }
186 
187  a.conservativeResize(1,1);
188  a = a.transpose();
189 
190  a.conservativeResize(0,0);
191  a = a.transpose();
192 }
193 
195 {
196  for(int i = 0; i < g_repeat; i++) {
198  CALL_SUBTEST_2( adjoint(Matrix3d()) );
199  CALL_SUBTEST_3( adjoint(Matrix4f()) );
200 
201  CALL_SUBTEST_4( adjoint(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) );
202  CALL_SUBTEST_5( adjoint(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
203  CALL_SUBTEST_6( adjoint(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
204 
205  // Complement for 128 bits vectorization:
206  CALL_SUBTEST_8( adjoint(Matrix2d()) );
208 
209  // 256 bits vectorization:
213  }
214  // test a large static matrix only once
216 
217  CALL_SUBTEST_13( adjoint_extra<0>() );
218 }
219 
adjoint_specific< true >::run
static void run(const Vec &v1, const Vec &v2, Vec &v3, const Mat &square, Scalar s1, Scalar s2)
Definition: adjoint.cpp:18
VERIFY_IS_MUCH_SMALLER_THAN
#define VERIFY_IS_MUCH_SMALLER_THAN(a, b)
Definition: main.h:390
MatrixType
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
c
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
b
Scalar * b
Definition: benchVecAdd.cpp:17
adjoint_extra
void adjoint_extra()
Definition: adjoint.cpp:152
Eigen::test_isApproxWithRef
bool test_isApproxWithRef(const Scalar &a, const Scalar &b, const ScalarRef &ref)
Definition: main.h:603
m1
Matrix3d m1
Definition: IOFormat.cpp:2
real
float real
Definition: datatypes.h:10
Eigen::is_same_type
internal::enable_if< internal::is_same< T1, T2 >::value, bool >::type is_same_type(const T1 &, const T2 &)
Definition: main.h:410
Eigen::internal::isMuchSmallerThan
EIGEN_DEVICE_FUNC bool isMuchSmallerThan(const Scalar &x, const OtherScalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
Definition: Eigen/src/Core/MathFunctions.h:1940
CALL_SUBTEST_11
#define CALL_SUBTEST_11(FUNC)
Definition: split_test_helper.h:64
CALL_SUBTEST_9
#define CALL_SUBTEST_9(FUNC)
Definition: split_test_helper.h:52
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
VERIFY_RAISES_ASSERT
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:340
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
dot
Scalar EIGEN_BLAS_FUNC() dot(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
Definition: level1_real_impl.h:49
CALL_SUBTEST_4
#define CALL_SUBTEST_4(FUNC)
Definition: split_test_helper.h:22
m2
MatrixType m2(n_dims)
CALL_SUBTEST_3
#define CALL_SUBTEST_3(FUNC)
Definition: split_test_helper.h:16
CALL_SUBTEST_1
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
CALL_SUBTEST_13
#define CALL_SUBTEST_13(FUNC)
Definition: split_test_helper.h:76
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
square
const EIGEN_DEVICE_FUNC SquareReturnType square() const
Definition: ArrayCwiseUnaryOps.h:425
CALL_SUBTEST_10
#define CALL_SUBTEST_10(FUNC)
Definition: split_test_helper.h:58
CALL_SUBTEST_5
#define CALL_SUBTEST_5(FUNC)
Definition: split_test_helper.h:28
Eigen::g_repeat
static int g_repeat
Definition: main.h:169
gtsam.examples.DogLegOptimizerExample.run
def run(args)
Definition: DogLegOptimizerExample.py:21
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
conj
AnnoyingScalar conj(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:104
CALL_SUBTEST_6
#define CALL_SUBTEST_6(FUNC)
Definition: split_test_helper.h:34
CALL_SUBTEST_2
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10
VERIFY_IS_APPROX
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:15
RealScalar
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:47
m3
static const DiscreteKey m3(M(3), 2)
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
adjoint_specific< false >::run
static void run(const Vec &v1, const Vec &v2, Vec &v3, const Mat &square, Scalar s1, Scalar s2)
Definition: adjoint.cpp:29
main.h
EIGEN_TEST_MAX_SIZE
#define EIGEN_TEST_MAX_SIZE
Definition: boostmultiprec.cpp:16
CALL_SUBTEST_12
#define CALL_SUBTEST_12(FUNC)
Definition: split_test_helper.h:70
v2
Vector v2
Definition: testSerializationBase.cpp:39
ref
Reference counting helper.
Definition: object.h:67
min
#define min(a, b)
Definition: datatypes.h:19
Eigen::Matrix< Scalar, Dynamic, 1 >
abs
#define abs(x)
Definition: datatypes.h:17
VectorType
Definition: FFTW.cpp:65
v3
Vector v3
Definition: testSerializationBase.cpp:40
cols
int cols
Definition: Tutorial_commainit_02.cpp:1
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(adjoint)
Definition: adjoint.cpp:194
CALL_SUBTEST_7
#define CALL_SUBTEST_7(FUNC)
Definition: split_test_helper.h:40
CALL_SUBTEST_8
#define CALL_SUBTEST_8(FUNC)
Definition: split_test_helper.h:46
max
#define max(a, b)
Definition: datatypes.h:20
adjoint_specific
Definition: adjoint.cpp:14
Eigen::NumTraits
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:232
adjoint
void adjoint(const MatrixType &m)
Definition: adjoint.cpp:67
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
v1
Vector v1
Definition: testSerializationBase.cpp:38
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
VERIFY
#define VERIFY(a)
Definition: main.h:380
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:01:48