product_extra.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 #include "main.h"
11 
12 template<typename MatrixType> void product_extra(const MatrixType& m)
13 {
14  typedef typename MatrixType::Scalar Scalar;
15  typedef Matrix<Scalar, 1, Dynamic> RowVectorType;
16  typedef Matrix<Scalar, Dynamic, 1> ColVectorType;
17  typedef Matrix<Scalar, Dynamic, Dynamic,
18  MatrixType::Flags&RowMajorBit> OtherMajorMatrixType;
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(rows, cols),
26  mzero = MatrixType::Zero(rows, cols),
27  identity = MatrixType::Identity(rows, rows),
28  square = MatrixType::Random(rows, rows),
29  res = MatrixType::Random(rows, rows),
30  square2 = MatrixType::Random(cols, cols),
31  res2 = MatrixType::Random(cols, cols);
32  RowVectorType v1 = RowVectorType::Random(rows), vrres(rows);
33  ColVectorType vc2 = ColVectorType::Random(cols), vcres(cols);
34  OtherMajorMatrixType tm1 = m1;
35 
36  Scalar s1 = internal::random<Scalar>(),
37  s2 = internal::random<Scalar>(),
38  s3 = internal::random<Scalar>();
39 
40  VERIFY_IS_APPROX(m3.noalias() = m1 * m2.adjoint(), m1 * m2.adjoint().eval());
41  VERIFY_IS_APPROX(m3.noalias() = m1.adjoint() * square.adjoint(), m1.adjoint().eval() * square.adjoint().eval());
42  VERIFY_IS_APPROX(m3.noalias() = m1.adjoint() * m2, m1.adjoint().eval() * m2);
43  VERIFY_IS_APPROX(m3.noalias() = (s1 * m1.adjoint()) * m2, (s1 * m1.adjoint()).eval() * m2);
44  VERIFY_IS_APPROX(m3.noalias() = ((s1 * m1).adjoint()) * m2, (numext::conj(s1) * m1.adjoint()).eval() * m2);
45  VERIFY_IS_APPROX(m3.noalias() = (- m1.adjoint() * s1) * (s3 * m2), (- m1.adjoint() * s1).eval() * (s3 * m2).eval());
46  VERIFY_IS_APPROX(m3.noalias() = (s2 * m1.adjoint() * s1) * m2, (s2 * m1.adjoint() * s1).eval() * m2);
47  VERIFY_IS_APPROX(m3.noalias() = (-m1*s2) * s1*m2.adjoint(), (-m1*s2).eval() * (s1*m2.adjoint()).eval());
48 
49  // a very tricky case where a scale factor has to be automatically conjugated:
50  VERIFY_IS_APPROX( m1.adjoint() * (s1*m2).conjugate(), (m1.adjoint()).eval() * ((s1*m2).conjugate()).eval());
51 
52 
53  // test all possible conjugate combinations for the four matrix-vector product cases:
54 
55  VERIFY_IS_APPROX((-m1.conjugate() * s2) * (s1 * vc2),
56  (-m1.conjugate()*s2).eval() * (s1 * vc2).eval());
57  VERIFY_IS_APPROX((-m1 * s2) * (s1 * vc2.conjugate()),
58  (-m1*s2).eval() * (s1 * vc2.conjugate()).eval());
59  VERIFY_IS_APPROX((-m1.conjugate() * s2) * (s1 * vc2.conjugate()),
60  (-m1.conjugate()*s2).eval() * (s1 * vc2.conjugate()).eval());
61 
62  VERIFY_IS_APPROX((s1 * vc2.transpose()) * (-m1.adjoint() * s2),
63  (s1 * vc2.transpose()).eval() * (-m1.adjoint()*s2).eval());
64  VERIFY_IS_APPROX((s1 * vc2.adjoint()) * (-m1.transpose() * s2),
65  (s1 * vc2.adjoint()).eval() * (-m1.transpose()*s2).eval());
66  VERIFY_IS_APPROX((s1 * vc2.adjoint()) * (-m1.adjoint() * s2),
67  (s1 * vc2.adjoint()).eval() * (-m1.adjoint()*s2).eval());
68 
69  VERIFY_IS_APPROX((-m1.adjoint() * s2) * (s1 * v1.transpose()),
70  (-m1.adjoint()*s2).eval() * (s1 * v1.transpose()).eval());
71  VERIFY_IS_APPROX((-m1.transpose() * s2) * (s1 * v1.adjoint()),
72  (-m1.transpose()*s2).eval() * (s1 * v1.adjoint()).eval());
73  VERIFY_IS_APPROX((-m1.adjoint() * s2) * (s1 * v1.adjoint()),
74  (-m1.adjoint()*s2).eval() * (s1 * v1.adjoint()).eval());
75 
76  VERIFY_IS_APPROX((s1 * v1) * (-m1.conjugate() * s2),
77  (s1 * v1).eval() * (-m1.conjugate()*s2).eval());
78  VERIFY_IS_APPROX((s1 * v1.conjugate()) * (-m1 * s2),
79  (s1 * v1.conjugate()).eval() * (-m1*s2).eval());
80  VERIFY_IS_APPROX((s1 * v1.conjugate()) * (-m1.conjugate() * s2),
81  (s1 * v1.conjugate()).eval() * (-m1.conjugate()*s2).eval());
82 
83  VERIFY_IS_APPROX((-m1.adjoint() * s2) * (s1 * v1.adjoint()),
84  (-m1.adjoint()*s2).eval() * (s1 * v1.adjoint()).eval());
85 
86  // test the vector-matrix product with non aligned starts
87  Index i = internal::random<Index>(0,m1.rows()-2);
88  Index j = internal::random<Index>(0,m1.cols()-2);
89  Index r = internal::random<Index>(1,m1.rows()-i);
90  Index c = internal::random<Index>(1,m1.cols()-j);
91  Index i2 = internal::random<Index>(0,m1.rows()-1);
92  Index j2 = internal::random<Index>(0,m1.cols()-1);
93 
94  VERIFY_IS_APPROX(m1.col(j2).adjoint() * m1.block(0,j,m1.rows(),c), m1.col(j2).adjoint().eval() * m1.block(0,j,m1.rows(),c).eval());
95  VERIFY_IS_APPROX(m1.block(i,0,r,m1.cols()) * m1.row(i2).adjoint(), m1.block(i,0,r,m1.cols()).eval() * m1.row(i2).adjoint().eval());
96 
97  // regression test
98  MatrixType tmp = m1 * m1.adjoint() * s1;
99  VERIFY_IS_APPROX(tmp, m1 * m1.adjoint() * s1);
100 
101  // regression test for bug 1343, assignment to arrays
102  Array<Scalar,Dynamic,1> a1 = m1 * vc2;
103  VERIFY_IS_APPROX(a1.matrix(),m1*vc2);
104  Array<Scalar,Dynamic,1> a2 = s1 * (m1 * vc2);
105  VERIFY_IS_APPROX(a2.matrix(),s1*m1*vc2);
106  Array<Scalar,1,Dynamic> a3 = v1 * m1;
107  VERIFY_IS_APPROX(a3.matrix(),v1*m1);
108  Array<Scalar,Dynamic,Dynamic> a4 = m1 * m2.adjoint();
109  VERIFY_IS_APPROX(a4.matrix(),m1*m2.adjoint());
110 }
111 
112 // Regression test for bug reported at http://forum.kde.org/viewtopic.php?f=74&t=96947
114 {
115  Eigen::Matrix2Xd dNdxy(2, 3);
116  dNdxy << -0.5, 0.5, 0,
117  -0.3, 0, 0.3;
118  double det = 6.0, wt = 0.5;
119  VERIFY_IS_APPROX(dNdxy.transpose()*dNdxy*det*wt, det*wt*dNdxy.transpose()*dNdxy);
120 }
121 
122 template <typename MatrixType>
124 {
125  typedef typename MatrixType::Scalar Scalar;
126  const int PacketSize = internal::packet_traits<Scalar>::size;
127  const int PacketSize1 = PacketSize>1 ? PacketSize-1 : 1;
128  Index rows = m.rows();
129  Index cols = m.cols();
130 
131  {
132  MatrixType res, a(rows,0), b(0,cols);
133  VERIFY_IS_APPROX( (res=a*b), MatrixType::Zero(rows,cols) );
134  VERIFY_IS_APPROX( (res=a*a.transpose()), MatrixType::Zero(rows,rows) );
135  VERIFY_IS_APPROX( (res=b.transpose()*b), MatrixType::Zero(cols,cols) );
136  VERIFY_IS_APPROX( (res=b.transpose()*a.transpose()), MatrixType::Zero(cols,rows) );
137  }
138 
139  {
140  MatrixType res, a(rows,cols), b(cols,0);
141  res = a*b;
142  VERIFY(res.rows()==rows && res.cols()==0);
143  b.resize(0,rows);
144  res = b*a;
145  VERIFY(res.rows()==0 && res.cols()==cols);
146  }
147 
148  {
152  VERIFY_IS_APPROX( (res=a*b), MatrixType::Zero(PacketSize,1) );
153  VERIFY_IS_APPROX( (res=a.lazyProduct(b)), MatrixType::Zero(PacketSize,1) );
154  }
155 
156  {
160  VERIFY_IS_APPROX( (res=a*b), MatrixType::Zero(PacketSize1,1) );
161  VERIFY_IS_APPROX( (res=a.lazyProduct(b)), MatrixType::Zero(PacketSize1,1) );
162  }
163 
164  {
165  Matrix<Scalar,PacketSize,Dynamic> a(PacketSize,0);
168  VERIFY_IS_APPROX( (res=a*b), MatrixType::Zero(PacketSize,1) );
169  VERIFY_IS_APPROX( (res=a.lazyProduct(b)), MatrixType::Zero(PacketSize,1) );
170  }
171 
172  {
173  Matrix<Scalar,PacketSize1,Dynamic> a(PacketSize1,0);
176  VERIFY_IS_APPROX( (res=a*b), MatrixType::Zero(PacketSize1,1) );
177  VERIFY_IS_APPROX( (res=a.lazyProduct(b)), MatrixType::Zero(PacketSize1,1) );
178  }
179 }
180 
181 template<int>
182 void bug_127()
183 {
184  // Bug 127
185  //
186  // a product of the form lhs*rhs with
187  //
188  // lhs:
189  // rows = 1, cols = 4
190  // RowsAtCompileTime = 1, ColsAtCompileTime = -1
191  // MaxRowsAtCompileTime = 1, MaxColsAtCompileTime = 5
192  //
193  // rhs:
194  // rows = 4, cols = 0
195  // RowsAtCompileTime = -1, ColsAtCompileTime = -1
196  // MaxRowsAtCompileTime = 5, MaxColsAtCompileTime = 1
197  //
198  // was failing on a runtime assertion, because it had been mis-compiled as a dot product because Product.h was using the
199  // max-sizes to detect size 1 indicating vectors, and that didn't account for 0-sized object with max-size 1.
200 
203  a*b;
204 }
205 
206 template<int> void bug_817()
207 {
208  ArrayXXf B = ArrayXXf::Random(10,10), C;
209  VectorXf x = VectorXf::Random(10);
210  C = (x.transpose()*B.matrix());
211  B = (x.transpose()*B.matrix());
212  VERIFY_IS_APPROX(B,C);
213 }
214 
215 template<int>
217 {
218  // Regression test for the bug reported here:
219  // http://forum.kde.org/viewtopic.php?f=74&t=107541
220  // Recall the matrix*vector kernel avoid unaligned loads by loading two packets and then reassemble then.
221  // There was a mistake in the computation of the valid range for fully unaligned objects: in some rare cases,
222  // memory was read outside the allocated matrix memory. Though the values were not used, this might raise segfault.
223  for(int m=450;m<460;++m)
224  {
225  for(int n=8;n<12;++n)
226  {
227  MatrixXf M(m, n);
228  VectorXf v1(n), r1(500);
229  RowVectorXf v2(m), r2(16);
230 
231  M.setRandom();
232  v1.setRandom();
233  v2.setRandom();
234  for(int o=0; o<4; ++o)
235  {
236  r1.segment(o,m).noalias() = M * v1;
237  VERIFY_IS_APPROX(r1.segment(o,m), M * MatrixXf(v1));
238  r2.segment(o,n).noalias() = v2 * M;
239  VERIFY_IS_APPROX(r2.segment(o,n), MatrixXf(v2) * M);
240  }
241  }
242  }
243 }
244 
245 template<typename T>
248 {
249  Index mc(m), nc(n), kc(k);
250  internal::computeProductBlockingSizes<T,T>(kc, mc, nc);
251  return kc+mc+nc;
252 }
253 
254 template<typename T>
256 {
257  Index ret = 0;
258  ret += test_compute_block_size<T>(0,1,1);
259  ret += test_compute_block_size<T>(1,0,1);
260  ret += test_compute_block_size<T>(1,1,0);
261  ret += test_compute_block_size<T>(0,0,1);
262  ret += test_compute_block_size<T>(0,1,0);
263  ret += test_compute_block_size<T>(1,0,0);
264  ret += test_compute_block_size<T>(0,0,0);
265  return ret;
266 }
267 
268 template<typename>
270 {
271  Index m = internal::random<Index>(10,50);
272  Index n = internal::random<Index>(10,50);
273  MatrixXd A, B, C(m,n), D(m,m);
274  VectorXd a, b, c(n);
275  C.setRandom();
276  D.setRandom();
277  c.setRandom();
278  double s = internal::random<double>(1,10);
279 
280  A = C;
281  B = A * A.transpose();
282  A = A * A.transpose();
283  VERIFY_IS_APPROX(A,B);
284 
285  A = C;
286  B = (A * A.transpose())/s;
287  A = (A * A.transpose())/s;
288  VERIFY_IS_APPROX(A,B);
289 
290  A = C;
291  B = (A * A.transpose()) + D;
292  A = (A * A.transpose()) + D;
293  VERIFY_IS_APPROX(A,B);
294 
295  A = C;
296  B = D + (A * A.transpose());
297  A = D + (A * A.transpose());
298  VERIFY_IS_APPROX(A,B);
299 
300  A = C;
301  B = s * (A * A.transpose());
302  A = s * (A * A.transpose());
303  VERIFY_IS_APPROX(A,B);
304 
305  A = C;
306  a = c;
307  b = (A * a)/s;
308  a = (A * a)/s;
309  VERIFY_IS_APPROX(a,b);
310 }
311 
312 template<int>
313 void bug_1308()
314 {
315  int n = 10;
316  MatrixXd r(n,n);
317  VectorXd v = VectorXd::Random(n);
318  r = v * RowVectorXd::Ones(n);
319  VERIFY_IS_APPROX(r, v.rowwise().replicate(n));
320  r = VectorXd::Ones(n) * v.transpose();
321  VERIFY_IS_APPROX(r, v.rowwise().replicate(n).transpose());
322 
323  Matrix4d ones44 = Matrix4d::Ones();
324  Matrix4d m44 = Matrix4d::Ones() * Matrix4d::Ones();
325  VERIFY_IS_APPROX(m44,Matrix4d::Constant(4));
326  VERIFY_IS_APPROX(m44.noalias()=ones44*Matrix4d::Ones(), Matrix4d::Constant(4));
327  VERIFY_IS_APPROX(m44.noalias()=ones44.transpose()*Matrix4d::Ones(), Matrix4d::Constant(4));
328  VERIFY_IS_APPROX(m44.noalias()=Matrix4d::Ones()*ones44, Matrix4d::Constant(4));
329  VERIFY_IS_APPROX(m44.noalias()=Matrix4d::Ones()*ones44.transpose(), Matrix4d::Constant(4));
330 
331  typedef Matrix<double,4,4,RowMajor> RMatrix4d;
332  RMatrix4d r44 = Matrix4d::Ones() * Matrix4d::Ones();
333  VERIFY_IS_APPROX(r44,Matrix4d::Constant(4));
334  VERIFY_IS_APPROX(r44.noalias()=ones44*Matrix4d::Ones(), Matrix4d::Constant(4));
335  VERIFY_IS_APPROX(r44.noalias()=ones44.transpose()*Matrix4d::Ones(), Matrix4d::Constant(4));
336  VERIFY_IS_APPROX(r44.noalias()=Matrix4d::Ones()*ones44, Matrix4d::Constant(4));
337  VERIFY_IS_APPROX(r44.noalias()=Matrix4d::Ones()*ones44.transpose(), Matrix4d::Constant(4));
338  VERIFY_IS_APPROX(r44.noalias()=ones44*RMatrix4d::Ones(), Matrix4d::Constant(4));
339  VERIFY_IS_APPROX(r44.noalias()=ones44.transpose()*RMatrix4d::Ones(), Matrix4d::Constant(4));
340  VERIFY_IS_APPROX(r44.noalias()=RMatrix4d::Ones()*ones44, Matrix4d::Constant(4));
341  VERIFY_IS_APPROX(r44.noalias()=RMatrix4d::Ones()*ones44.transpose(), Matrix4d::Constant(4));
342 
343 // RowVector4d r4;
344  m44.setOnes();
345  r44.setZero();
346  VERIFY_IS_APPROX(r44.noalias() += m44.row(0).transpose() * RowVector4d::Ones(), ones44);
347  r44.setZero();
348  VERIFY_IS_APPROX(r44.noalias() += m44.col(0) * RowVector4d::Ones(), ones44);
349  r44.setZero();
350  VERIFY_IS_APPROX(r44.noalias() += Vector4d::Ones() * m44.row(0), ones44);
351  r44.setZero();
352  VERIFY_IS_APPROX(r44.noalias() += Vector4d::Ones() * m44.col(0).transpose(), ones44);
353 }
354 
356 {
357  for(int i = 0; i < g_repeat; i++) {
358  CALL_SUBTEST_1( product_extra(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
359  CALL_SUBTEST_2( product_extra(MatrixXd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
360  CALL_SUBTEST_2( mat_mat_scalar_scalar_product() );
361  CALL_SUBTEST_3( product_extra(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) );
362  CALL_SUBTEST_4( product_extra(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) );
363  CALL_SUBTEST_1( zero_sized_objects(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
364  }
365  CALL_SUBTEST_5( bug_127<0>() );
366  CALL_SUBTEST_5( bug_817<0>() );
367  CALL_SUBTEST_5( bug_1308<0>() );
368  CALL_SUBTEST_6( unaligned_objects<0>() );
369  CALL_SUBTEST_7( compute_block_size<float>() );
370  CALL_SUBTEST_7( compute_block_size<double>() );
371  CALL_SUBTEST_7( compute_block_size<std::complex<double> >() );
372  CALL_SUBTEST_8( aliasing_with_resize<void>() );
373 
374 }
Matrix3f m
void zero_sized_objects(const MatrixType &m)
SCALAR Scalar
Definition: bench_gemm.cpp:33
Matrix< RealScalar, Dynamic, Dynamic > M
Definition: bench_gemm.cpp:38
Vector v2
Scalar * b
Definition: benchVecAdd.cpp:17
void bug_127()
void adjoint(const MatrixType &m)
Definition: adjoint.cpp:67
Vector v1
void aliasing_with_resize()
MatrixType m2(n_dims)
ArrayXcf v
Definition: Cwise_arg.cpp:1
void unaligned_objects()
int n
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
MatrixXf MatrixType
void product_extra(const MatrixType &m)
void bug_1308()
Matrix< SCALARA, Dynamic, Dynamic > A
Definition: bench_gemm.cpp:35
void test_product_extra()
Matrix< SCALARB, Dynamic, Dynamic > B
Definition: bench_gemm.cpp:36
const unsigned int RowMajorBit
Definition: Constants.h:61
#define EIGEN_DONT_INLINE
Definition: Macros.h:517
Array33i a
EIGEN_DONT_INLINE Index test_compute_block_size(Index m, Index n, Index k)
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
#define VERIFY_IS_APPROX(a, b)
Index compute_block_size()
Matrix3d m1
Definition: IOFormat.cpp:2
EIGEN_DEVICE_FUNC ConjugateReturnType conjugate() const
static int g_repeat
Definition: main.h:144
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
RealScalar s
static const double r2
Matrix< Scalar, Dynamic, Dynamic > C
Definition: bench_gemm.cpp:37
static const double r1
#define VERIFY(a)
Definition: main.h:325
DenseIndex ret
Definition: level1_impl.h:59
#define EIGEN_TEST_MAX_SIZE
void mat_mat_scalar_scalar_product()
General-purpose arrays with easy API for coefficient-wise operations.
Definition: Array.h:45
internal::nested_eval< T, 1 >::type eval(const T &xpr)
const int Dynamic
Definition: Constants.h:21
The matrix class, also used for vectors and row-vectors.
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
Derived & setRandom(Index size)
Definition: Random.h:151
std::ptrdiff_t j
EIGEN_DEVICE_FUNC const SquareReturnType square() const
ScalarWithExceptions conj(const ScalarWithExceptions &x)
Definition: exceptions.cpp:74
void bug_817()


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:43:33