product_selfadjoint.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) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
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 template<typename MatrixType> void product_selfadjoint(const MatrixType& m)
13 {
14  typedef typename MatrixType::Scalar Scalar;
17 
19 
20  Index rows = m.rows();
21  Index cols = m.cols();
22 
23  MatrixType m1 = MatrixType::Random(rows, cols),
24  m2 = MatrixType::Random(rows, cols),
25  m3;
26  VectorType v1 = VectorType::Random(rows),
27  v2 = VectorType::Random(rows),
28  v3(rows);
29  RowVectorType r1 = RowVectorType::Random(rows),
30  r2 = RowVectorType::Random(rows);
31  RhsMatrixType m4 = RhsMatrixType::Random(rows,10);
32 
33  Scalar s1 = internal::random<Scalar>(),
34  s2 = internal::random<Scalar>(),
35  s3 = internal::random<Scalar>();
36 
37  m1 = (m1.adjoint() + m1).eval();
38 
39  // rank2 update
40  m2 = m1.template triangularView<Lower>();
41  m2.template selfadjointView<Lower>().rankUpdate(v1,v2);
42  VERIFY_IS_APPROX(m2, (m1 + v1 * v2.adjoint()+ v2 * v1.adjoint()).template triangularView<Lower>().toDenseMatrix());
43 
44  m2 = m1.template triangularView<Upper>();
45  m2.template selfadjointView<Upper>().rankUpdate(-v1,s2*v2,s3);
46  VERIFY_IS_APPROX(m2, (m1 + (s3*(-v1)*(s2*v2).adjoint()+numext::conj(s3)*(s2*v2)*(-v1).adjoint())).template triangularView<Upper>().toDenseMatrix());
47 
48  m2 = m1.template triangularView<Upper>();
49  m2.template selfadjointView<Upper>().rankUpdate(-s2*r1.adjoint(),r2.adjoint()*s3,s1);
50  VERIFY_IS_APPROX(m2, (m1 + s1*(-s2*r1.adjoint())*(r2.adjoint()*s3).adjoint() + numext::conj(s1)*(r2.adjoint()*s3) * (-s2*r1.adjoint()).adjoint()).template triangularView<Upper>().toDenseMatrix());
51 
52  if (rows>1)
53  {
54  m2 = m1.template triangularView<Lower>();
55  m2.block(1,1,rows-1,cols-1).template selfadjointView<Lower>().rankUpdate(v1.tail(rows-1),v2.head(cols-1));
56  m3 = m1;
57  m3.block(1,1,rows-1,cols-1) += v1.tail(rows-1) * v2.head(cols-1).adjoint()+ v2.head(cols-1) * v1.tail(rows-1).adjoint();
58  VERIFY_IS_APPROX(m2, m3.template triangularView<Lower>().toDenseMatrix());
59  }
60 }
61 
63 {
64  int s = 0;
65  for(int i = 0; i < g_repeat ; i++) {
68  CALL_SUBTEST_3( product_selfadjoint(Matrix3d()) );
69 
70  s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2);
71  CALL_SUBTEST_4( product_selfadjoint(MatrixXcf(s, s)) );
73 
74  s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2);
75  CALL_SUBTEST_5( product_selfadjoint(MatrixXcd(s,s)) );
77 
78  s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE);
79  CALL_SUBTEST_6( product_selfadjoint(MatrixXd(s,s)) );
81 
82  s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE);
85  }
86 }
s
RealScalar s
Definition: level1_cplx_impl.h:126
MatrixType
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
r2
static const double r2
Definition: testSmartRangeFactor.cpp:32
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(product_selfadjoint)
Definition: product_selfadjoint.cpp:62
m1
Matrix3d m1
Definition: IOFormat.cpp:2
r1
static const double r1
Definition: testSmartRangeFactor.cpp:32
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
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_5
#define CALL_SUBTEST_5(FUNC)
Definition: split_test_helper.h:28
Eigen::g_repeat
static int g_repeat
Definition: main.h:169
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
m3
static const DiscreteKey m3(M(3), 2)
product_selfadjoint
void product_selfadjoint(const MatrixType &m)
Definition: product_selfadjoint.cpp:12
main.h
EIGEN_TEST_MAX_SIZE
#define EIGEN_TEST_MAX_SIZE
Definition: boostmultiprec.cpp:16
v2
Vector v2
Definition: testSerializationBase.cpp:39
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
VectorType
Definition: FFTW.cpp:65
v3
Vector v3
Definition: testSerializationBase.cpp:40
cols
int cols
Definition: Tutorial_commainit_02.cpp:1
triangularView< Lower >
A triangularView< Lower >().adjoint().solveInPlace(B)
CALL_SUBTEST_7
#define CALL_SUBTEST_7(FUNC)
Definition: split_test_helper.h:40
adjoint
void adjoint(const MatrixType &m)
Definition: adjoint.cpp:67
eval
internal::nested_eval< T, 1 >::type eval(const T &xpr)
Definition: sparse_permutations.cpp:38
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
TEST_SET_BUT_UNUSED_VARIABLE
#define TEST_SET_BUT_UNUSED_VARIABLE(X)
Definition: main.h:121
v1
Vector v1
Definition: testSerializationBase.cpp:38
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
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 Sun Dec 22 2024 04:12:51