product_extra.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. // regression test
  81. MatrixType tmp = m1 * m1.adjoint() * s1;
  82. VERIFY_IS_APPROX(tmp, m1 * m1.adjoint() * s1);
  83. // regression test for bug 1343, assignment to arrays
  84. Array<Scalar,Dynamic,1> a1 = m1 * vc2;
  85. VERIFY_IS_APPROX(a1.matrix(),m1*vc2);
  86. Array<Scalar,Dynamic,1> a2 = s1 * (m1 * vc2);
  87. VERIFY_IS_APPROX(a2.matrix(),s1*m1*vc2);
  88. Array<Scalar,1,Dynamic> a3 = v1 * m1;
  89. VERIFY_IS_APPROX(a3.matrix(),v1*m1);
  90. Array<Scalar,Dynamic,Dynamic> a4 = m1 * m2.adjoint();
  91. VERIFY_IS_APPROX(a4.matrix(),m1*m2.adjoint());
  92. }
  93. // Regression test for bug reported at http://forum.kde.org/viewtopic.php?f=74&t=96947
  94. void mat_mat_scalar_scalar_product()
  95. {
  96. Eigen::Matrix2Xd dNdxy(2, 3);
  97. dNdxy << -0.5, 0.5, 0,
  98. -0.3, 0, 0.3;
  99. double det = 6.0, wt = 0.5;
  100. VERIFY_IS_APPROX(dNdxy.transpose()*dNdxy*det*wt, det*wt*dNdxy.transpose()*dNdxy);
  101. }
  102. template <typename MatrixType>
  103. void zero_sized_objects(const MatrixType& m)
  104. {
  105. typedef typename MatrixType::Scalar Scalar;
  106. const int PacketSize = internal::packet_traits<Scalar>::size;
  107. const int PacketSize1 = PacketSize>1 ? PacketSize-1 : 1;
  108. Index rows = m.rows();
  109. Index cols = m.cols();
  110. {
  111. MatrixType res, a(rows,0), b(0,cols);
  112. VERIFY_IS_APPROX( (res=a*b), MatrixType::Zero(rows,cols) );
  113. VERIFY_IS_APPROX( (res=a*a.transpose()), MatrixType::Zero(rows,rows) );
  114. VERIFY_IS_APPROX( (res=b.transpose()*b), MatrixType::Zero(cols,cols) );
  115. VERIFY_IS_APPROX( (res=b.transpose()*a.transpose()), MatrixType::Zero(cols,rows) );
  116. }
  117. {
  118. MatrixType res, a(rows,cols), b(cols,0);
  119. res = a*b;
  120. VERIFY(res.rows()==rows && res.cols()==0);
  121. b.resize(0,rows);
  122. res = b*a;
  123. VERIFY(res.rows()==0 && res.cols()==cols);
  124. }
  125. {
  126. Matrix<Scalar,PacketSize,0> a;
  127. Matrix<Scalar,0,1> b;
  128. Matrix<Scalar,PacketSize,1> res;
  129. VERIFY_IS_APPROX( (res=a*b), MatrixType::Zero(PacketSize,1) );
  130. VERIFY_IS_APPROX( (res=a.lazyProduct(b)), MatrixType::Zero(PacketSize,1) );
  131. }
  132. {
  133. Matrix<Scalar,PacketSize1,0> a;
  134. Matrix<Scalar,0,1> b;
  135. Matrix<Scalar,PacketSize1,1> res;
  136. VERIFY_IS_APPROX( (res=a*b), MatrixType::Zero(PacketSize1,1) );
  137. VERIFY_IS_APPROX( (res=a.lazyProduct(b)), MatrixType::Zero(PacketSize1,1) );
  138. }
  139. {
  140. Matrix<Scalar,PacketSize,Dynamic> a(PacketSize,0);
  141. Matrix<Scalar,Dynamic,1> b(0,1);
  142. Matrix<Scalar,PacketSize,1> res;
  143. VERIFY_IS_APPROX( (res=a*b), MatrixType::Zero(PacketSize,1) );
  144. VERIFY_IS_APPROX( (res=a.lazyProduct(b)), MatrixType::Zero(PacketSize,1) );
  145. }
  146. {
  147. Matrix<Scalar,PacketSize1,Dynamic> a(PacketSize1,0);
  148. Matrix<Scalar,Dynamic,1> b(0,1);
  149. Matrix<Scalar,PacketSize1,1> res;
  150. VERIFY_IS_APPROX( (res=a*b), MatrixType::Zero(PacketSize1,1) );
  151. VERIFY_IS_APPROX( (res=a.lazyProduct(b)), MatrixType::Zero(PacketSize1,1) );
  152. }
  153. }
  154. template<int>
  155. void bug_127()
  156. {
  157. // Bug 127
  158. //
  159. // a product of the form lhs*rhs with
  160. //
  161. // lhs:
  162. // rows = 1, cols = 4
  163. // RowsAtCompileTime = 1, ColsAtCompileTime = -1
  164. // MaxRowsAtCompileTime = 1, MaxColsAtCompileTime = 5
  165. //
  166. // rhs:
  167. // rows = 4, cols = 0
  168. // RowsAtCompileTime = -1, ColsAtCompileTime = -1
  169. // MaxRowsAtCompileTime = 5, MaxColsAtCompileTime = 1
  170. //
  171. // was failing on a runtime assertion, because it had been mis-compiled as a dot product because Product.h was using the
  172. // max-sizes to detect size 1 indicating vectors, and that didn't account for 0-sized object with max-size 1.
  173. Matrix<float,1,Dynamic,RowMajor,1,5> a(1,4);
  174. Matrix<float,Dynamic,Dynamic,ColMajor,5,1> b(4,0);
  175. a*b;
  176. }
  177. template<int> void bug_817()
  178. {
  179. ArrayXXf B = ArrayXXf::Random(10,10), C;
  180. VectorXf x = VectorXf::Random(10);
  181. C = (x.transpose()*B.matrix());
  182. B = (x.transpose()*B.matrix());
  183. VERIFY_IS_APPROX(B,C);
  184. }
  185. template<int>
  186. void unaligned_objects()
  187. {
  188. // Regression test for the bug reported here:
  189. // http://forum.kde.org/viewtopic.php?f=74&t=107541
  190. // Recall the matrix*vector kernel avoid unaligned loads by loading two packets and then reassemble then.
  191. // There was a mistake in the computation of the valid range for fully unaligned objects: in some rare cases,
  192. // memory was read outside the allocated matrix memory. Though the values were not used, this might raise segfault.
  193. for(int m=450;m<460;++m)
  194. {
  195. for(int n=8;n<12;++n)
  196. {
  197. MatrixXf M(m, n);
  198. VectorXf v1(n), r1(500);
  199. RowVectorXf v2(m), r2(16);
  200. M.setRandom();
  201. v1.setRandom();
  202. v2.setRandom();
  203. for(int o=0; o<4; ++o)
  204. {
  205. r1.segment(o,m).noalias() = M * v1;
  206. VERIFY_IS_APPROX(r1.segment(o,m), M * MatrixXf(v1));
  207. r2.segment(o,n).noalias() = v2 * M;
  208. VERIFY_IS_APPROX(r2.segment(o,n), MatrixXf(v2) * M);
  209. }
  210. }
  211. }
  212. }
  213. template<typename T>
  214. EIGEN_DONT_INLINE
  215. Index test_compute_block_size(Index m, Index n, Index k)
  216. {
  217. Index mc(m), nc(n), kc(k);
  218. internal::computeProductBlockingSizes<T,T>(kc, mc, nc);
  219. return kc+mc+nc;
  220. }
  221. template<typename T>
  222. Index compute_block_size()
  223. {
  224. Index ret = 0;
  225. ret += test_compute_block_size<T>(0,1,1);
  226. ret += test_compute_block_size<T>(1,0,1);
  227. ret += test_compute_block_size<T>(1,1,0);
  228. ret += test_compute_block_size<T>(0,0,1);
  229. ret += test_compute_block_size<T>(0,1,0);
  230. ret += test_compute_block_size<T>(1,0,0);
  231. ret += test_compute_block_size<T>(0,0,0);
  232. return ret;
  233. }
  234. template<typename>
  235. void aliasing_with_resize()
  236. {
  237. Index m = internal::random<Index>(10,50);
  238. Index n = internal::random<Index>(10,50);
  239. MatrixXd A, B, C(m,n), D(m,m);
  240. VectorXd a, b, c(n);
  241. C.setRandom();
  242. D.setRandom();
  243. c.setRandom();
  244. double s = internal::random<double>(1,10);
  245. A = C;
  246. B = A * A.transpose();
  247. A = A * A.transpose();
  248. VERIFY_IS_APPROX(A,B);
  249. A = C;
  250. B = (A * A.transpose())/s;
  251. A = (A * A.transpose())/s;
  252. VERIFY_IS_APPROX(A,B);
  253. A = C;
  254. B = (A * A.transpose()) + D;
  255. A = (A * A.transpose()) + D;
  256. VERIFY_IS_APPROX(A,B);
  257. A = C;
  258. B = D + (A * A.transpose());
  259. A = D + (A * A.transpose());
  260. VERIFY_IS_APPROX(A,B);
  261. A = C;
  262. B = s * (A * A.transpose());
  263. A = s * (A * A.transpose());
  264. VERIFY_IS_APPROX(A,B);
  265. A = C;
  266. a = c;
  267. b = (A * a)/s;
  268. a = (A * a)/s;
  269. VERIFY_IS_APPROX(a,b);
  270. }
  271. template<int>
  272. void bug_1308()
  273. {
  274. int n = 10;
  275. MatrixXd r(n,n);
  276. VectorXd v = VectorXd::Random(n);
  277. r = v * RowVectorXd::Ones(n);
  278. VERIFY_IS_APPROX(r, v.rowwise().replicate(n));
  279. r = VectorXd::Ones(n) * v.transpose();
  280. VERIFY_IS_APPROX(r, v.rowwise().replicate(n).transpose());
  281. Matrix4d ones44 = Matrix4d::Ones();
  282. Matrix4d m44 = Matrix4d::Ones() * Matrix4d::Ones();
  283. VERIFY_IS_APPROX(m44,Matrix4d::Constant(4));
  284. VERIFY_IS_APPROX(m44.noalias()=ones44*Matrix4d::Ones(), Matrix4d::Constant(4));
  285. VERIFY_IS_APPROX(m44.noalias()=ones44.transpose()*Matrix4d::Ones(), Matrix4d::Constant(4));
  286. VERIFY_IS_APPROX(m44.noalias()=Matrix4d::Ones()*ones44, Matrix4d::Constant(4));
  287. VERIFY_IS_APPROX(m44.noalias()=Matrix4d::Ones()*ones44.transpose(), Matrix4d::Constant(4));
  288. typedef Matrix<double,4,4,RowMajor> RMatrix4d;
  289. RMatrix4d r44 = Matrix4d::Ones() * Matrix4d::Ones();
  290. VERIFY_IS_APPROX(r44,Matrix4d::Constant(4));
  291. VERIFY_IS_APPROX(r44.noalias()=ones44*Matrix4d::Ones(), Matrix4d::Constant(4));
  292. VERIFY_IS_APPROX(r44.noalias()=ones44.transpose()*Matrix4d::Ones(), Matrix4d::Constant(4));
  293. VERIFY_IS_APPROX(r44.noalias()=Matrix4d::Ones()*ones44, Matrix4d::Constant(4));
  294. VERIFY_IS_APPROX(r44.noalias()=Matrix4d::Ones()*ones44.transpose(), Matrix4d::Constant(4));
  295. VERIFY_IS_APPROX(r44.noalias()=ones44*RMatrix4d::Ones(), Matrix4d::Constant(4));
  296. VERIFY_IS_APPROX(r44.noalias()=ones44.transpose()*RMatrix4d::Ones(), Matrix4d::Constant(4));
  297. VERIFY_IS_APPROX(r44.noalias()=RMatrix4d::Ones()*ones44, Matrix4d::Constant(4));
  298. VERIFY_IS_APPROX(r44.noalias()=RMatrix4d::Ones()*ones44.transpose(), Matrix4d::Constant(4));
  299. // RowVector4d r4;
  300. m44.setOnes();
  301. r44.setZero();
  302. VERIFY_IS_APPROX(r44.noalias() += m44.row(0).transpose() * RowVector4d::Ones(), ones44);
  303. r44.setZero();
  304. VERIFY_IS_APPROX(r44.noalias() += m44.col(0) * RowVector4d::Ones(), ones44);
  305. r44.setZero();
  306. VERIFY_IS_APPROX(r44.noalias() += Vector4d::Ones() * m44.row(0), ones44);
  307. r44.setZero();
  308. VERIFY_IS_APPROX(r44.noalias() += Vector4d::Ones() * m44.col(0).transpose(), ones44);
  309. }
  310. void test_product_extra()
  311. {
  312. for(int i = 0; i < g_repeat; i++) {
  313. CALL_SUBTEST_1( product_extra(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  314. CALL_SUBTEST_2( product_extra(MatrixXd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  315. CALL_SUBTEST_2( mat_mat_scalar_scalar_product() );
  316. CALL_SUBTEST_3( product_extra(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) );
  317. CALL_SUBTEST_4( product_extra(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) );
  318. CALL_SUBTEST_1( zero_sized_objects(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  319. }
  320. CALL_SUBTEST_5( bug_127<0>() );
  321. CALL_SUBTEST_5( bug_817<0>() );
  322. CALL_SUBTEST_5( bug_1308<0>() );
  323. CALL_SUBTEST_6( unaligned_objects<0>() );
  324. CALL_SUBTEST_7( compute_block_size<float>() );
  325. CALL_SUBTEST_7( compute_block_size<double>() );
  326. CALL_SUBTEST_7( compute_block_size<std::complex<double> >() );
  327. CALL_SUBTEST_8( aliasing_with_resize<void>() );
  328. }