You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

275 lines
11 KiB

  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::Index Index;
  13. typedef typename MatrixType::Scalar Scalar;
  14. typedef Matrix<Scalar, 1, Dynamic> RowVectorType;
  15. typedef Matrix<Scalar, Dynamic, 1> ColVectorType;
  16. typedef Matrix<Scalar, Dynamic, Dynamic,
  17. MatrixType::Flags&RowMajorBit> OtherMajorMatrixType;
  18. Index rows = m.rows();
  19. Index cols = m.cols();
  20. MatrixType m1 = MatrixType::Random(rows, cols),
  21. m2 = MatrixType::Random(rows, cols),
  22. m3(rows, cols),
  23. mzero = MatrixType::Zero(rows, cols),
  24. identity = MatrixType::Identity(rows, rows),
  25. square = MatrixType::Random(rows, rows),
  26. res = MatrixType::Random(rows, rows),
  27. square2 = MatrixType::Random(cols, cols),
  28. res2 = MatrixType::Random(cols, cols);
  29. RowVectorType v1 = RowVectorType::Random(rows), vrres(rows);
  30. ColVectorType vc2 = ColVectorType::Random(cols), vcres(cols);
  31. OtherMajorMatrixType tm1 = m1;
  32. Scalar s1 = internal::random<Scalar>(),
  33. s2 = internal::random<Scalar>(),
  34. s3 = internal::random<Scalar>();
  35. VERIFY_IS_APPROX(m3.noalias() = m1 * m2.adjoint(), m1 * m2.adjoint().eval());
  36. VERIFY_IS_APPROX(m3.noalias() = m1.adjoint() * square.adjoint(), m1.adjoint().eval() * square.adjoint().eval());
  37. VERIFY_IS_APPROX(m3.noalias() = m1.adjoint() * m2, m1.adjoint().eval() * m2);
  38. VERIFY_IS_APPROX(m3.noalias() = (s1 * m1.adjoint()) * m2, (s1 * m1.adjoint()).eval() * m2);
  39. VERIFY_IS_APPROX(m3.noalias() = ((s1 * m1).adjoint()) * m2, (numext::conj(s1) * m1.adjoint()).eval() * m2);
  40. VERIFY_IS_APPROX(m3.noalias() = (- m1.adjoint() * s1) * (s3 * m2), (- m1.adjoint() * s1).eval() * (s3 * m2).eval());
  41. VERIFY_IS_APPROX(m3.noalias() = (s2 * m1.adjoint() * s1) * m2, (s2 * m1.adjoint() * s1).eval() * m2);
  42. VERIFY_IS_APPROX(m3.noalias() = (-m1*s2) * s1*m2.adjoint(), (-m1*s2).eval() * (s1*m2.adjoint()).eval());
  43. // a very tricky case where a scale factor has to be automatically conjugated:
  44. VERIFY_IS_APPROX( m1.adjoint() * (s1*m2).conjugate(), (m1.adjoint()).eval() * ((s1*m2).conjugate()).eval());
  45. // test all possible conjugate combinations for the four matrix-vector product cases:
  46. VERIFY_IS_APPROX((-m1.conjugate() * s2) * (s1 * vc2),
  47. (-m1.conjugate()*s2).eval() * (s1 * vc2).eval());
  48. VERIFY_IS_APPROX((-m1 * s2) * (s1 * vc2.conjugate()),
  49. (-m1*s2).eval() * (s1 * vc2.conjugate()).eval());
  50. VERIFY_IS_APPROX((-m1.conjugate() * s2) * (s1 * vc2.conjugate()),
  51. (-m1.conjugate()*s2).eval() * (s1 * vc2.conjugate()).eval());
  52. VERIFY_IS_APPROX((s1 * vc2.transpose()) * (-m1.adjoint() * s2),
  53. (s1 * vc2.transpose()).eval() * (-m1.adjoint()*s2).eval());
  54. VERIFY_IS_APPROX((s1 * vc2.adjoint()) * (-m1.transpose() * s2),
  55. (s1 * vc2.adjoint()).eval() * (-m1.transpose()*s2).eval());
  56. VERIFY_IS_APPROX((s1 * vc2.adjoint()) * (-m1.adjoint() * s2),
  57. (s1 * vc2.adjoint()).eval() * (-m1.adjoint()*s2).eval());
  58. VERIFY_IS_APPROX((-m1.adjoint() * s2) * (s1 * v1.transpose()),
  59. (-m1.adjoint()*s2).eval() * (s1 * v1.transpose()).eval());
  60. VERIFY_IS_APPROX((-m1.transpose() * s2) * (s1 * v1.adjoint()),
  61. (-m1.transpose()*s2).eval() * (s1 * v1.adjoint()).eval());
  62. VERIFY_IS_APPROX((-m1.adjoint() * s2) * (s1 * v1.adjoint()),
  63. (-m1.adjoint()*s2).eval() * (s1 * v1.adjoint()).eval());
  64. VERIFY_IS_APPROX((s1 * v1) * (-m1.conjugate() * s2),
  65. (s1 * v1).eval() * (-m1.conjugate()*s2).eval());
  66. VERIFY_IS_APPROX((s1 * v1.conjugate()) * (-m1 * s2),
  67. (s1 * v1.conjugate()).eval() * (-m1*s2).eval());
  68. VERIFY_IS_APPROX((s1 * v1.conjugate()) * (-m1.conjugate() * s2),
  69. (s1 * v1.conjugate()).eval() * (-m1.conjugate()*s2).eval());
  70. VERIFY_IS_APPROX((-m1.adjoint() * s2) * (s1 * v1.adjoint()),
  71. (-m1.adjoint()*s2).eval() * (s1 * v1.adjoint()).eval());
  72. // test the vector-matrix product with non aligned starts
  73. Index i = internal::random<Index>(0,m1.rows()-2);
  74. Index j = internal::random<Index>(0,m1.cols()-2);
  75. Index r = internal::random<Index>(1,m1.rows()-i);
  76. Index c = internal::random<Index>(1,m1.cols()-j);
  77. Index i2 = internal::random<Index>(0,m1.rows()-1);
  78. Index j2 = internal::random<Index>(0,m1.cols()-1);
  79. 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());
  80. 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());
  81. // regression test
  82. MatrixType tmp = m1 * m1.adjoint() * s1;
  83. VERIFY_IS_APPROX(tmp, m1 * m1.adjoint() * s1);
  84. }
  85. // Regression test for bug reported at http://forum.kde.org/viewtopic.php?f=74&t=96947
  86. void mat_mat_scalar_scalar_product()
  87. {
  88. StormEigen::Matrix2Xd dNdxy(2, 3);
  89. dNdxy << -0.5, 0.5, 0,
  90. -0.3, 0, 0.3;
  91. double det = 6.0, wt = 0.5;
  92. VERIFY_IS_APPROX(dNdxy.transpose()*dNdxy*det*wt, det*wt*dNdxy.transpose()*dNdxy);
  93. }
  94. template <typename MatrixType>
  95. void zero_sized_objects(const MatrixType& m)
  96. {
  97. typedef typename MatrixType::Scalar Scalar;
  98. const int PacketSize = internal::packet_traits<Scalar>::size;
  99. const int PacketSize1 = PacketSize>1 ? PacketSize-1 : 1;
  100. Index rows = m.rows();
  101. Index cols = m.cols();
  102. {
  103. MatrixType res, a(rows,0), b(0,cols);
  104. VERIFY_IS_APPROX( (res=a*b), MatrixType::Zero(rows,cols) );
  105. VERIFY_IS_APPROX( (res=a*a.transpose()), MatrixType::Zero(rows,rows) );
  106. VERIFY_IS_APPROX( (res=b.transpose()*b), MatrixType::Zero(cols,cols) );
  107. VERIFY_IS_APPROX( (res=b.transpose()*a.transpose()), MatrixType::Zero(cols,rows) );
  108. }
  109. {
  110. MatrixType res, a(rows,cols), b(cols,0);
  111. res = a*b;
  112. VERIFY(res.rows()==rows && res.cols()==0);
  113. b.resize(0,rows);
  114. res = b*a;
  115. VERIFY(res.rows()==0 && res.cols()==cols);
  116. }
  117. {
  118. Matrix<Scalar,PacketSize,0> a;
  119. Matrix<Scalar,0,1> b;
  120. Matrix<Scalar,PacketSize,1> res;
  121. VERIFY_IS_APPROX( (res=a*b), MatrixType::Zero(PacketSize,1) );
  122. VERIFY_IS_APPROX( (res=a.lazyProduct(b)), MatrixType::Zero(PacketSize,1) );
  123. }
  124. {
  125. Matrix<Scalar,PacketSize1,0> a;
  126. Matrix<Scalar,0,1> b;
  127. Matrix<Scalar,PacketSize1,1> res;
  128. VERIFY_IS_APPROX( (res=a*b), MatrixType::Zero(PacketSize1,1) );
  129. VERIFY_IS_APPROX( (res=a.lazyProduct(b)), MatrixType::Zero(PacketSize1,1) );
  130. }
  131. {
  132. Matrix<Scalar,PacketSize,Dynamic> a(PacketSize,0);
  133. Matrix<Scalar,Dynamic,1> b(0,1);
  134. Matrix<Scalar,PacketSize,1> res;
  135. VERIFY_IS_APPROX( (res=a*b), MatrixType::Zero(PacketSize,1) );
  136. VERIFY_IS_APPROX( (res=a.lazyProduct(b)), MatrixType::Zero(PacketSize,1) );
  137. }
  138. {
  139. Matrix<Scalar,PacketSize1,Dynamic> a(PacketSize1,0);
  140. Matrix<Scalar,Dynamic,1> b(0,1);
  141. Matrix<Scalar,PacketSize1,1> res;
  142. VERIFY_IS_APPROX( (res=a*b), MatrixType::Zero(PacketSize1,1) );
  143. VERIFY_IS_APPROX( (res=a.lazyProduct(b)), MatrixType::Zero(PacketSize1,1) );
  144. }
  145. }
  146. template<int>
  147. void bug_127()
  148. {
  149. // Bug 127
  150. //
  151. // a product of the form lhs*rhs with
  152. //
  153. // lhs:
  154. // rows = 1, cols = 4
  155. // RowsAtCompileTime = 1, ColsAtCompileTime = -1
  156. // MaxRowsAtCompileTime = 1, MaxColsAtCompileTime = 5
  157. //
  158. // rhs:
  159. // rows = 4, cols = 0
  160. // RowsAtCompileTime = -1, ColsAtCompileTime = -1
  161. // MaxRowsAtCompileTime = 5, MaxColsAtCompileTime = 1
  162. //
  163. // was failing on a runtime assertion, because it had been mis-compiled as a dot product because Product.h was using the
  164. // max-sizes to detect size 1 indicating vectors, and that didn't account for 0-sized object with max-size 1.
  165. Matrix<float,1,Dynamic,RowMajor,1,5> a(1,4);
  166. Matrix<float,Dynamic,Dynamic,ColMajor,5,1> b(4,0);
  167. a*b;
  168. }
  169. template<int> void bug_817()
  170. {
  171. ArrayXXf B = ArrayXXf::Random(10,10), C;
  172. VectorXf x = VectorXf::Random(10);
  173. C = (x.transpose()*B.matrix());
  174. B = (x.transpose()*B.matrix());
  175. VERIFY_IS_APPROX(B,C);
  176. }
  177. template<int>
  178. void unaligned_objects()
  179. {
  180. // Regression test for the bug reported here:
  181. // http://forum.kde.org/viewtopic.php?f=74&t=107541
  182. // Recall the matrix*vector kernel avoid unaligned loads by loading two packets and then reassemble then.
  183. // There was a mistake in the computation of the valid range for fully unaligned objects: in some rare cases,
  184. // memory was read outside the allocated matrix memory. Though the values were not used, this might raise segfault.
  185. for(int m=450;m<460;++m)
  186. {
  187. for(int n=8;n<12;++n)
  188. {
  189. MatrixXf M(m, n);
  190. VectorXf v1(n), r1(500);
  191. RowVectorXf v2(m), r2(16);
  192. M.setRandom();
  193. v1.setRandom();
  194. v2.setRandom();
  195. for(int o=0; o<4; ++o)
  196. {
  197. r1.segment(o,m).noalias() = M * v1;
  198. VERIFY_IS_APPROX(r1.segment(o,m), M * MatrixXf(v1));
  199. r2.segment(o,n).noalias() = v2 * M;
  200. VERIFY_IS_APPROX(r2.segment(o,n), MatrixXf(v2) * M);
  201. }
  202. }
  203. }
  204. }
  205. template<typename T>
  206. EIGEN_DONT_INLINE
  207. Index test_compute_block_size(Index m, Index n, Index k)
  208. {
  209. Index mc(m), nc(n), kc(k);
  210. internal::computeProductBlockingSizes<T,T>(kc, mc, nc);
  211. return kc+mc+nc;
  212. }
  213. template<typename T>
  214. Index compute_block_size()
  215. {
  216. Index ret = 0;
  217. ret += test_compute_block_size<T>(0,1,1);
  218. ret += test_compute_block_size<T>(1,0,1);
  219. ret += test_compute_block_size<T>(1,1,0);
  220. ret += test_compute_block_size<T>(0,0,1);
  221. ret += test_compute_block_size<T>(0,1,0);
  222. ret += test_compute_block_size<T>(1,0,0);
  223. ret += test_compute_block_size<T>(0,0,0);
  224. return ret;
  225. }
  226. void test_product_extra()
  227. {
  228. for(int i = 0; i < g_repeat; i++) {
  229. CALL_SUBTEST_1( product_extra(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  230. CALL_SUBTEST_2( product_extra(MatrixXd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  231. CALL_SUBTEST_2( mat_mat_scalar_scalar_product() );
  232. CALL_SUBTEST_3( product_extra(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) );
  233. CALL_SUBTEST_4( product_extra(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) );
  234. CALL_SUBTEST_1( zero_sized_objects(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  235. }
  236. CALL_SUBTEST_5( bug_127<0>() );
  237. CALL_SUBTEST_5( bug_817<0>() );
  238. CALL_SUBTEST_6( unaligned_objects<0>() );
  239. CALL_SUBTEST_7( compute_block_size<float>() );
  240. CALL_SUBTEST_7( compute_block_size<double>() );
  241. CALL_SUBTEST_7( compute_block_size<std::complex<double> >() );
  242. }