diagonalmatrices.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) 2009 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 #include "main.h"
11 using namespace std;
12 template<typename MatrixType> void diagonalmatrices(const MatrixType& m)
13 {
14  typedef typename MatrixType::Scalar Scalar;
15  enum { Rows = MatrixType::RowsAtCompileTime, Cols = MatrixType::ColsAtCompileTime };
17  typedef Matrix<Scalar, 1, Cols> RowVectorType;
18  typedef Matrix<Scalar, Rows, Rows> SquareMatrixType;
19  typedef Matrix<Scalar, Dynamic, Dynamic> DynMatrixType;
20  typedef DiagonalMatrix<Scalar, Rows> LeftDiagonalMatrix;
21  typedef DiagonalMatrix<Scalar, Cols> RightDiagonalMatrix;
23  Index rows = m.rows();
24  Index cols = m.cols();
25 
26  MatrixType m1 = MatrixType::Random(rows, cols),
27  m2 = MatrixType::Random(rows, cols);
28  VectorType v1 = VectorType::Random(rows),
29  v2 = VectorType::Random(rows);
30  RowVectorType rv1 = RowVectorType::Random(cols),
31  rv2 = RowVectorType::Random(cols);
32 
33  LeftDiagonalMatrix ldm1(v1), ldm2(v2);
34  RightDiagonalMatrix rdm1(rv1), rdm2(rv2);
35 
36  Scalar s1 = internal::random<Scalar>();
37 
38  SquareMatrixType sq_m1 (v1.asDiagonal());
39  VERIFY_IS_APPROX(sq_m1, v1.asDiagonal().toDenseMatrix());
40  sq_m1 = v1.asDiagonal();
41  VERIFY_IS_APPROX(sq_m1, v1.asDiagonal().toDenseMatrix());
42  SquareMatrixType sq_m2 = v1.asDiagonal();
43  VERIFY_IS_APPROX(sq_m1, sq_m2);
44 
45  ldm1 = v1.asDiagonal();
46  LeftDiagonalMatrix ldm3(v1);
47  VERIFY_IS_APPROX(ldm1.diagonal(), ldm3.diagonal());
48  LeftDiagonalMatrix ldm4 = v1.asDiagonal();
49  VERIFY_IS_APPROX(ldm1.diagonal(), ldm4.diagonal());
50 
51  sq_m1.block(0,0,rows,rows) = ldm1;
52  VERIFY_IS_APPROX(sq_m1, ldm1.toDenseMatrix());
53  sq_m1.transpose() = ldm1;
54  VERIFY_IS_APPROX(sq_m1, ldm1.toDenseMatrix());
55 
56  Index i = internal::random<Index>(0, rows-1);
57  Index j = internal::random<Index>(0, cols-1);
58 
59  VERIFY_IS_APPROX( ((ldm1 * m1)(i,j)) , ldm1.diagonal()(i) * m1(i,j) );
60  VERIFY_IS_APPROX( ((ldm1 * (m1+m2))(i,j)) , ldm1.diagonal()(i) * (m1+m2)(i,j) );
61  VERIFY_IS_APPROX( ((m1 * rdm1)(i,j)) , rdm1.diagonal()(j) * m1(i,j) );
62  VERIFY_IS_APPROX( ((v1.asDiagonal() * m1)(i,j)) , v1(i) * m1(i,j) );
63  VERIFY_IS_APPROX( ((m1 * rv1.asDiagonal())(i,j)) , rv1(j) * m1(i,j) );
64  VERIFY_IS_APPROX( (((v1+v2).asDiagonal() * m1)(i,j)) , (v1+v2)(i) * m1(i,j) );
65  VERIFY_IS_APPROX( (((v1+v2).asDiagonal() * (m1+m2))(i,j)) , (v1+v2)(i) * (m1+m2)(i,j) );
66  VERIFY_IS_APPROX( ((m1 * (rv1+rv2).asDiagonal())(i,j)) , (rv1+rv2)(j) * m1(i,j) );
67  VERIFY_IS_APPROX( (((m1+m2) * (rv1+rv2).asDiagonal())(i,j)) , (rv1+rv2)(j) * (m1+m2)(i,j) );
68 
69  if(rows>1)
70  {
71  DynMatrixType tmp = m1.topRows(rows/2), res;
72  VERIFY_IS_APPROX( (res = m1.topRows(rows/2) * rv1.asDiagonal()), tmp * rv1.asDiagonal() );
73  VERIFY_IS_APPROX( (res = v1.head(rows/2).asDiagonal()*m1.topRows(rows/2)), v1.head(rows/2).asDiagonal()*tmp );
74  }
75 
76  BigMatrix big;
77  big.setZero(2*rows, 2*cols);
78 
79  big.block(i,j,rows,cols) = m1;
80  big.block(i,j,rows,cols) = v1.asDiagonal() * big.block(i,j,rows,cols);
81 
82  VERIFY_IS_APPROX((big.block(i,j,rows,cols)) , v1.asDiagonal() * m1 );
83 
84  big.block(i,j,rows,cols) = m1;
85  big.block(i,j,rows,cols) = big.block(i,j,rows,cols) * rv1.asDiagonal();
86  VERIFY_IS_APPROX((big.block(i,j,rows,cols)) , m1 * rv1.asDiagonal() );
87 
88 
89  // scalar multiple
90  VERIFY_IS_APPROX(LeftDiagonalMatrix(ldm1*s1).diagonal(), ldm1.diagonal() * s1);
91  VERIFY_IS_APPROX(LeftDiagonalMatrix(s1*ldm1).diagonal(), s1 * ldm1.diagonal());
92 
93  VERIFY_IS_APPROX(m1 * (rdm1 * s1), (m1 * rdm1) * s1);
94  VERIFY_IS_APPROX(m1 * (s1 * rdm1), (m1 * rdm1) * s1);
95 
96  // Diagonal to dense
97  sq_m1.setRandom();
98  sq_m2 = sq_m1;
99  VERIFY_IS_APPROX( (sq_m1 += (s1*v1).asDiagonal()), sq_m2 += (s1*v1).asDiagonal().toDenseMatrix() );
100  VERIFY_IS_APPROX( (sq_m1 -= (s1*v1).asDiagonal()), sq_m2 -= (s1*v1).asDiagonal().toDenseMatrix() );
101  VERIFY_IS_APPROX( (sq_m1 = (s1*v1).asDiagonal()), (s1*v1).asDiagonal().toDenseMatrix() );
102 
103  sq_m1.setRandom();
104  sq_m2 = v1.asDiagonal();
105  sq_m2 = sq_m1 * sq_m2;
106  VERIFY_IS_APPROX( (sq_m1*v1.asDiagonal()).col(i), sq_m2.col(i) );
107  VERIFY_IS_APPROX( (sq_m1*v1.asDiagonal()).row(i), sq_m2.row(i) );
108 
109  sq_m1 = v1.asDiagonal();
110  sq_m2 = v2.asDiagonal();
111  SquareMatrixType sq_m3 = v1.asDiagonal();
112  VERIFY_IS_APPROX( sq_m3 = v1.asDiagonal() + v2.asDiagonal(), sq_m1 + sq_m2);
113  VERIFY_IS_APPROX( sq_m3 = v1.asDiagonal() - v2.asDiagonal(), sq_m1 - sq_m2);
114  VERIFY_IS_APPROX( sq_m3 = v1.asDiagonal() - 2*v2.asDiagonal() + v1.asDiagonal(), sq_m1 - 2*sq_m2 + sq_m1);
115 }
116 
117 template<typename MatrixType> void as_scalar_product(const MatrixType& m)
118 {
119  typedef typename MatrixType::Scalar Scalar;
121  typedef Matrix<Scalar, Dynamic, Dynamic> DynMatrixType;
122  typedef Matrix<Scalar, Dynamic, 1> DynVectorType;
123  typedef Matrix<Scalar, 1, Dynamic> DynRowVectorType;
124 
125  Index rows = m.rows();
126  Index depth = internal::random<Index>(1,EIGEN_TEST_MAX_SIZE);
127 
128  VectorType v1 = VectorType::Random(rows);
129  DynVectorType dv1 = DynVectorType::Random(depth);
130  DynRowVectorType drv1 = DynRowVectorType::Random(depth);
131  DynMatrixType dm1 = dv1;
132  DynMatrixType drm1 = drv1;
133 
134  Scalar s = v1(0);
135 
136  VERIFY_IS_APPROX( v1.asDiagonal() * drv1, s*drv1 );
137  VERIFY_IS_APPROX( dv1 * v1.asDiagonal(), dv1*s );
138 
139  VERIFY_IS_APPROX( v1.asDiagonal() * drm1, s*drm1 );
140  VERIFY_IS_APPROX( dm1 * v1.asDiagonal(), dm1*s );
141 }
142 
143 template<int>
144 void bug987()
145 {
146  Matrix3Xd points = Matrix3Xd::Random(3, 3);
147  Vector2d diag = Vector2d::Random();
148  Matrix2Xd tmp1 = points.topRows<2>(), res1, res2;
149  VERIFY_IS_APPROX( res1 = diag.asDiagonal() * points.topRows<2>(), res2 = diag.asDiagonal() * tmp1 );
150  Matrix2d tmp2 = points.topLeftCorner<2,2>();
151  VERIFY_IS_APPROX(( res1 = points.topLeftCorner<2,2>()*diag.asDiagonal()) , res2 = tmp2*diag.asDiagonal() );
152 }
153 
155 {
156  for(int i = 0; i < g_repeat; i++) {
159 
160  CALL_SUBTEST_2( diagonalmatrices(Matrix3f()) );
162  CALL_SUBTEST_4( diagonalmatrices(Matrix4d()) );
164  CALL_SUBTEST_6( diagonalmatrices(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
165  CALL_SUBTEST_6( as_scalar_product(MatrixXcf(1,1)) );
166  CALL_SUBTEST_7( diagonalmatrices(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
168  CALL_SUBTEST_9( diagonalmatrices(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
169  CALL_SUBTEST_9( diagonalmatrices(MatrixXf(1,1)) );
170  CALL_SUBTEST_9( as_scalar_product(MatrixXf(1,1)) );
171  }
172  CALL_SUBTEST_10( bug987<0>() );
173 }
Eigen::DiagonalMatrix
Represents a diagonal matrix with its storage.
Definition: DiagonalMatrix.h:140
s
RealScalar s
Definition: level1_cplx_impl.h:126
MatrixType
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
gtsam::diag
Matrix diag(const std::vector< Matrix > &Hs)
Definition: Matrix.cpp:207
m1
Matrix3d m1
Definition: IOFormat.cpp:2
res
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
Definition: PartialRedux_count.cpp:3
CALL_SUBTEST_9
#define CALL_SUBTEST_9(FUNC)
Definition: split_test_helper.h:52
diagonal
void diagonal(const MatrixType &m)
Definition: diagonal.cpp:12
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
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
diagonalmatrices
void diagonalmatrices(const MatrixType &m)
Definition: diagonalmatrices.cpp:12
CALL_SUBTEST_10
#define CALL_SUBTEST_10(FUNC)
Definition: split_test_helper.h:58
as_scalar_product
void as_scalar_product(const MatrixType &m)
Definition: diagonalmatrices.cpp:117
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
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
main.h
std
Definition: BFloat16.h:88
EIGEN_TEST_MAX_SIZE
#define EIGEN_TEST_MAX_SIZE
Definition: boostmultiprec.cpp:16
big
static double big
Definition: igam.c:119
bug987
void bug987()
Definition: diagonalmatrices.cpp:144
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
cols
int cols
Definition: Tutorial_commainit_02.cpp:1
depth
static double depth
Definition: testSphericalCamera.cpp:64
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
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
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(diagonalmatrices)
Definition: diagonalmatrices.cpp:154
Cols
static const int Cols
Definition: testCallRecord.cpp:32


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:11:28