product_extra.cpp 15 KB

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