product.h
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 #include "main.h"
11 #include <Eigen/QR>
12 
13 template<typename Derived1, typename Derived2>
15 {
16  return !((m1-m2).cwiseAbs2().maxCoeff() < epsilon * epsilon
17  * (std::max)(m1.cwiseAbs2().maxCoeff(), m2.cwiseAbs2().maxCoeff()));
18 }
19 
20 template<typename MatrixType> void product(const MatrixType& m)
21 {
22  /* this test covers the following files:
23  Identity.h Product.h
24  */
25  typedef typename MatrixType::Scalar Scalar;
30  typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime,
31  MatrixType::Flags&RowMajorBit?ColMajor:RowMajor> OtherMajorMatrixType;
32 
33  Index rows = m.rows();
34  Index cols = m.cols();
35 
36  // this test relies a lot on Random.h, and there's not much more that we can do
37  // to test it, hence I consider that we will have tested Random.h
38  MatrixType m1 = MatrixType::Random(rows, cols),
39  m2 = MatrixType::Random(rows, cols),
40  m3(rows, cols);
41  RowSquareMatrixType
42  identity = RowSquareMatrixType::Identity(rows, rows),
43  square = RowSquareMatrixType::Random(rows, rows),
44  res = RowSquareMatrixType::Random(rows, rows);
45  ColSquareMatrixType
46  square2 = ColSquareMatrixType::Random(cols, cols),
47  res2 = ColSquareMatrixType::Random(cols, cols);
48  RowVectorType v1 = RowVectorType::Random(rows);
49  ColVectorType vc2 = ColVectorType::Random(cols), vcres(cols);
50  OtherMajorMatrixType tm1 = m1;
51 
52  Scalar s1 = internal::random<Scalar>();
53 
54  Index r = internal::random<Index>(0, rows-1),
55  c = internal::random<Index>(0, cols-1),
56  c2 = internal::random<Index>(0, cols-1);
57 
58  // begin testing Product.h: only associativity for now
59  // (we use Transpose.h but this doesn't count as a test for it)
60  VERIFY_IS_APPROX((m1*m1.transpose())*m2, m1*(m1.transpose()*m2));
61  m3 = m1;
62  m3 *= m1.transpose() * m2;
63  VERIFY_IS_APPROX(m3, m1 * (m1.transpose()*m2));
64  VERIFY_IS_APPROX(m3, m1 * (m1.transpose()*m2));
65 
66  // continue testing Product.h: distributivity
69 
70  // continue testing Product.h: compatibility with ScalarMultiple.h
71  VERIFY_IS_APPROX(s1*(square*m1), (s1*square)*m1);
72  VERIFY_IS_APPROX(s1*(square*m1), square*(m1*s1));
73 
74  // test Product.h together with Identity.h
75  VERIFY_IS_APPROX(v1, identity*v1);
76  VERIFY_IS_APPROX(v1.transpose(), v1.transpose() * identity);
77  // again, test operator() to check const-qualification
78  VERIFY_IS_APPROX(MatrixType::Identity(rows, cols)(r,c), static_cast<Scalar>(r==c));
79 
80  if (rows!=cols)
82 
83  // test the previous tests were not screwed up because operator* returns 0
84  // (we use the more accurate default epsilon)
86  {
87  VERIFY(areNotApprox(m1.transpose()*m2,m2.transpose()*m1));
88  }
89 
90  // test optimized operator+= path
91  res = square;
92  res.noalias() += m1 * m2.transpose();
93  VERIFY_IS_APPROX(res, square + m1 * m2.transpose());
95  {
96  VERIFY(areNotApprox(res,square + m2 * m1.transpose()));
97  }
98  vcres = vc2;
99  vcres.noalias() += m1.transpose() * v1;
100  VERIFY_IS_APPROX(vcres, vc2 + m1.transpose() * v1);
101 
102  // test optimized operator-= path
103  res = square;
104  res.noalias() -= m1 * m2.transpose();
105  VERIFY_IS_APPROX(res, square - (m1 * m2.transpose()));
107  {
108  VERIFY(areNotApprox(res,square - m2 * m1.transpose()));
109  }
110  vcres = vc2;
111  vcres.noalias() -= m1.transpose() * v1;
112  VERIFY_IS_APPROX(vcres, vc2 - m1.transpose() * v1);
113 
114  // test scaled products
115  res = square;
116  res.noalias() = s1 * m1 * m2.transpose();
117  VERIFY_IS_APPROX(res, ((s1*m1).eval() * m2.transpose()));
118  res = square;
119  res.noalias() += s1 * m1 * m2.transpose();
120  VERIFY_IS_APPROX(res, square + ((s1*m1).eval() * m2.transpose()));
121  res = square;
122  res.noalias() -= s1 * m1 * m2.transpose();
123  VERIFY_IS_APPROX(res, square - ((s1*m1).eval() * m2.transpose()));
124 
125  // test d ?= a+b*c rules
126  res.noalias() = square + m1 * m2.transpose();
127  VERIFY_IS_APPROX(res, square + m1 * m2.transpose());
128  res.noalias() += square + m1 * m2.transpose();
129  VERIFY_IS_APPROX(res, 2*(square + m1 * m2.transpose()));
130  res.noalias() -= square + m1 * m2.transpose();
131  VERIFY_IS_APPROX(res, square + m1 * m2.transpose());
132 
133  // test d ?= a-b*c rules
134  res.noalias() = square - m1 * m2.transpose();
135  VERIFY_IS_APPROX(res, square - m1 * m2.transpose());
136  res.noalias() += square - m1 * m2.transpose();
137  VERIFY_IS_APPROX(res, 2*(square - m1 * m2.transpose()));
138  res.noalias() -= square - m1 * m2.transpose();
139  VERIFY_IS_APPROX(res, square - m1 * m2.transpose());
140 
141 
142  tm1 = m1;
143  VERIFY_IS_APPROX(tm1.transpose() * v1, m1.transpose() * v1);
144  VERIFY_IS_APPROX(v1.transpose() * tm1, v1.transpose() * m1);
145 
146  // test submatrix and matrix/vector product
147  for (int i=0; i<rows; ++i)
148  res.row(i) = m1.row(i) * m2.transpose();
149  VERIFY_IS_APPROX(res, m1 * m2.transpose());
150  // the other way round:
151  for (int i=0; i<rows; ++i)
152  res.col(i) = m1 * m2.transpose().col(i);
153  VERIFY_IS_APPROX(res, m1 * m2.transpose());
154 
155  res2 = square2;
156  res2.noalias() += m1.transpose() * m2;
157  VERIFY_IS_APPROX(res2, square2 + m1.transpose() * m2);
159  {
160  VERIFY(areNotApprox(res2,square2 + m2.transpose() * m1));
161  }
162 
163  VERIFY_IS_APPROX(res.col(r).noalias() = square.adjoint() * square.col(r), (square.adjoint() * square.col(r)).eval());
164  VERIFY_IS_APPROX(res.col(r).noalias() = square * square.col(r), (square * square.col(r)).eval());
165 
166  // vector at runtime (see bug 1166)
167  {
168  RowSquareMatrixType ref(square);
169  ColSquareMatrixType ref2(square2);
170  ref = res = square;
171  VERIFY_IS_APPROX(res.block(0,0,1,rows).noalias() = m1.col(0).transpose() * square.transpose(), (ref.row(0) = m1.col(0).transpose() * square.transpose()));
172  VERIFY_IS_APPROX(res.block(0,0,1,rows).noalias() = m1.block(0,0,rows,1).transpose() * square.transpose(), (ref.row(0) = m1.col(0).transpose() * square.transpose()));
173  VERIFY_IS_APPROX(res.block(0,0,1,rows).noalias() = m1.col(0).transpose() * square, (ref.row(0) = m1.col(0).transpose() * square));
174  VERIFY_IS_APPROX(res.block(0,0,1,rows).noalias() = m1.block(0,0,rows,1).transpose() * square, (ref.row(0) = m1.col(0).transpose() * square));
175  ref2 = res2 = square2;
176  VERIFY_IS_APPROX(res2.block(0,0,1,cols).noalias() = m1.row(0) * square2.transpose(), (ref2.row(0) = m1.row(0) * square2.transpose()));
177  VERIFY_IS_APPROX(res2.block(0,0,1,cols).noalias() = m1.block(0,0,1,cols) * square2.transpose(), (ref2.row(0) = m1.row(0) * square2.transpose()));
178  VERIFY_IS_APPROX(res2.block(0,0,1,cols).noalias() = m1.row(0) * square2, (ref2.row(0) = m1.row(0) * square2));
179  VERIFY_IS_APPROX(res2.block(0,0,1,cols).noalias() = m1.block(0,0,1,cols) * square2, (ref2.row(0) = m1.row(0) * square2));
180  }
181 
182  // vector.block() (see bug 1283)
183  {
184  RowVectorType w1(rows);
185  VERIFY_IS_APPROX(square * v1.block(0,0,rows,1), square * v1);
186  VERIFY_IS_APPROX(w1.noalias() = square * v1.block(0,0,rows,1), square * v1);
187  VERIFY_IS_APPROX(w1.block(0,0,rows,1).noalias() = square * v1.block(0,0,rows,1), square * v1);
188 
190  VERIFY_IS_APPROX(vc2.block(0,0,cols,1).transpose() * square2, vc2.transpose() * square2);
191  VERIFY_IS_APPROX(w2.noalias() = vc2.block(0,0,cols,1).transpose() * square2, vc2.transpose() * square2);
192  VERIFY_IS_APPROX(w2.block(0,0,1,cols).noalias() = vc2.block(0,0,cols,1).transpose() * square2, vc2.transpose() * square2);
193 
194  vc2 = square2.block(0,0,1,cols).transpose();
195  VERIFY_IS_APPROX(square2.block(0,0,1,cols) * square2, vc2.transpose() * square2);
196  VERIFY_IS_APPROX(w2.noalias() = square2.block(0,0,1,cols) * square2, vc2.transpose() * square2);
197  VERIFY_IS_APPROX(w2.block(0,0,1,cols).noalias() = square2.block(0,0,1,cols) * square2, vc2.transpose() * square2);
198 
199  vc2 = square2.block(0,0,cols,1);
200  VERIFY_IS_APPROX(square2.block(0,0,cols,1).transpose() * square2, vc2.transpose() * square2);
201  VERIFY_IS_APPROX(w2.noalias() = square2.block(0,0,cols,1).transpose() * square2, vc2.transpose() * square2);
202  VERIFY_IS_APPROX(w2.block(0,0,1,cols).noalias() = square2.block(0,0,cols,1).transpose() * square2, vc2.transpose() * square2);
203  }
204 
205  // inner product
206  {
207  Scalar x = square2.row(c) * square2.col(c2);
208  VERIFY_IS_APPROX(x, square2.row(c).transpose().cwiseProduct(square2.col(c2)).sum());
209  }
210 
211  // outer product
212  {
213  VERIFY_IS_APPROX(m1.col(c) * m1.row(r), m1.block(0,c,rows,1) * m1.block(r,0,1,cols));
214  VERIFY_IS_APPROX(m1.row(r).transpose() * m1.col(c).transpose(), m1.block(r,0,1,cols).transpose() * m1.block(0,c,rows,1).transpose());
215  VERIFY_IS_APPROX(m1.block(0,c,rows,1) * m1.row(r), m1.block(0,c,rows,1) * m1.block(r,0,1,cols));
216  VERIFY_IS_APPROX(m1.col(c) * m1.block(r,0,1,cols), m1.block(0,c,rows,1) * m1.block(r,0,1,cols));
217  VERIFY_IS_APPROX(m1.leftCols(1) * m1.row(r), m1.block(0,0,rows,1) * m1.block(r,0,1,cols));
218  VERIFY_IS_APPROX(m1.col(c) * m1.topRows(1), m1.block(0,c,rows,1) * m1.block(0,0,1,cols));
219  }
220 
221  // Aliasing
222  {
223  ColVectorType x(cols); x.setRandom();
224  ColVectorType z(x);
225  ColVectorType y(cols); y.setZero();
226  ColSquareMatrixType A(cols,cols); A.setRandom();
227  // CwiseBinaryOp
228  VERIFY_IS_APPROX(x = y + A*x, A*z);
229  x = z;
230  VERIFY_IS_APPROX(x = y - A*x, A*(-z));
231  x = z;
232  // CwiseUnaryOp
233  VERIFY_IS_APPROX(x = Scalar(1.)*(A*x), A*z);
234  }
235 
236  // regression for blas_trais
237  {
238  VERIFY_IS_APPROX(square * (square*square).transpose(), square * square.transpose() * square.transpose());
241  VERIFY_IS_APPROX(square * (square*square).conjugate(), square * square.conjugate() * square.conjugate());
242  }
243 
244  // destination with a non-default inner-stride
245  // see bug 1741
246  if(!MatrixType::IsRowMajor)
247  {
248  typedef Matrix<Scalar,Dynamic,Dynamic> MatrixX;
249  MatrixX buffer(2*rows,2*rows);
251  buffer.setZero();
252  VERIFY_IS_APPROX(map1 = m1 * m2.transpose(), (m1 * m2.transpose()).eval());
253  buffer.setZero();
254  VERIFY_IS_APPROX(map1.noalias() = m1 * m2.transpose(), (m1 * m2.transpose()).eval());
255  buffer.setZero();
256  VERIFY_IS_APPROX(map1.noalias() += m1 * m2.transpose(), (m1 * m2.transpose()).eval());
257  }
258 
259 }
areNotApprox
bool areNotApprox(const MatrixBase< Derived1 > &m1, const MatrixBase< Derived2 > &m2, typename Derived1::RealScalar epsilon=NumTraits< typename Derived1::RealScalar >::dummy_precision())
Definition: product.h:14
Eigen::Stride
Holds strides information for Map.
Definition: Stride.h:48
MatrixType
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
c
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
x
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition: gnuplot_common_settings.hh:12
Eigen::RowMajorBit
const unsigned int RowMajorBit
Definition: Constants.h:66
m1
Matrix3d m1
Definition: IOFormat.cpp:2
buffer
Definition: pytypes.h:2270
Eigen::RowMajor
@ RowMajor
Definition: Constants.h:321
res
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
Definition: PartialRedux_count.cpp:3
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
VERIFY_RAISES_ASSERT
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:340
A
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:48
epsilon
static double epsilon
Definition: testRot3.cpp:37
m2
MatrixType m2(n_dims)
square
const EIGEN_DEVICE_FUNC SquareReturnType square() const
Definition: ArrayCwiseUnaryOps.h:425
pybind_wrapper_test_script.z
z
Definition: pybind_wrapper_test_script.py:61
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
Eigen::Map
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:94
cwiseAbs2
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE CwiseAbs2ReturnType cwiseAbs2() const
Definition: MatrixCwiseUnaryOps.h:46
y
Scalar * y
Definition: level1_cplx_impl.h:124
VERIFY_IS_APPROX
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:15
conjugate
EIGEN_DEVICE_FUNC ConjugateReturnType conjugate() const
Definition: CommonCwiseUnaryOps.h:74
RealScalar
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:47
m3
static const DiscreteKey m3(M(3), 2)
main.h
ref
Reference counting helper.
Definition: object.h:67
c2
static double c2
Definition: airy.c:55
product
void product(const MatrixType &m)
Definition: product.h:20
min
#define min(a, b)
Definition: datatypes.h:19
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
Eigen::MatrixBase
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
Eigen::ColMajor
@ ColMajor
Definition: Constants.h:319
cols
int cols
Definition: Tutorial_commainit_02.cpp:1
max
#define max(a, b)
Definition: datatypes.h:20
Eigen::NumTraits
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:232
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
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
test_eigen.ref
ref
Definition: test_eigen.py:9


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:03:42