product_small.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. #define EIGEN_NO_STATIC_ASSERT
  10. #include "product.h"
  11. #include <Eigen/LU>
  12. // regression test for bug 447
  13. template<int>
  14. void product1x1()
  15. {
  16. Matrix<float,1,3> matAstatic;
  17. Matrix<float,3,1> matBstatic;
  18. matAstatic.setRandom();
  19. matBstatic.setRandom();
  20. VERIFY_IS_APPROX( (matAstatic * matBstatic).coeff(0,0),
  21. matAstatic.cwiseProduct(matBstatic.transpose()).sum() );
  22. MatrixXf matAdynamic(1,3);
  23. MatrixXf matBdynamic(3,1);
  24. matAdynamic.setRandom();
  25. matBdynamic.setRandom();
  26. VERIFY_IS_APPROX( (matAdynamic * matBdynamic).coeff(0,0),
  27. matAdynamic.cwiseProduct(matBdynamic.transpose()).sum() );
  28. }
  29. template<typename TC, typename TA, typename TB>
  30. const TC& ref_prod(TC &C, const TA &A, const TB &B)
  31. {
  32. for(Index i=0;i<C.rows();++i)
  33. for(Index j=0;j<C.cols();++j)
  34. for(Index k=0;k<A.cols();++k)
  35. C.coeffRef(i,j) += A.coeff(i,k) * B.coeff(k,j);
  36. return C;
  37. }
  38. template<typename T, int Rows, int Cols, int Depth, int OC, int OA, int OB>
  39. typename internal::enable_if<! ( (Rows ==1&&Depth!=1&&OA==ColMajor)
  40. || (Depth==1&&Rows !=1&&OA==RowMajor)
  41. || (Cols ==1&&Depth!=1&&OB==RowMajor)
  42. || (Depth==1&&Cols !=1&&OB==ColMajor)
  43. || (Rows ==1&&Cols !=1&&OC==ColMajor)
  44. || (Cols ==1&&Rows !=1&&OC==RowMajor)),void>::type
  45. test_lazy_single(int rows, int cols, int depth)
  46. {
  47. Matrix<T,Rows,Depth,OA> A(rows,depth); A.setRandom();
  48. Matrix<T,Depth,Cols,OB> B(depth,cols); B.setRandom();
  49. Matrix<T,Rows,Cols,OC> C(rows,cols); C.setRandom();
  50. Matrix<T,Rows,Cols,OC> D(C);
  51. VERIFY_IS_APPROX(C+=A.lazyProduct(B), ref_prod(D,A,B));
  52. }
  53. template<typename T, int Rows, int Cols, int Depth, int OC, int OA, int OB>
  54. typename internal::enable_if< ( (Rows ==1&&Depth!=1&&OA==ColMajor)
  55. || (Depth==1&&Rows !=1&&OA==RowMajor)
  56. || (Cols ==1&&Depth!=1&&OB==RowMajor)
  57. || (Depth==1&&Cols !=1&&OB==ColMajor)
  58. || (Rows ==1&&Cols !=1&&OC==ColMajor)
  59. || (Cols ==1&&Rows !=1&&OC==RowMajor)),void>::type
  60. test_lazy_single(int, int, int)
  61. {
  62. }
  63. template<typename T, int Rows, int Cols, int Depth>
  64. void test_lazy_all_layout(int rows=Rows, int cols=Cols, int depth=Depth)
  65. {
  66. CALL_SUBTEST(( test_lazy_single<T,Rows,Cols,Depth,ColMajor,ColMajor,ColMajor>(rows,cols,depth) ));
  67. CALL_SUBTEST(( test_lazy_single<T,Rows,Cols,Depth,RowMajor,ColMajor,ColMajor>(rows,cols,depth) ));
  68. CALL_SUBTEST(( test_lazy_single<T,Rows,Cols,Depth,ColMajor,RowMajor,ColMajor>(rows,cols,depth) ));
  69. CALL_SUBTEST(( test_lazy_single<T,Rows,Cols,Depth,RowMajor,RowMajor,ColMajor>(rows,cols,depth) ));
  70. CALL_SUBTEST(( test_lazy_single<T,Rows,Cols,Depth,ColMajor,ColMajor,RowMajor>(rows,cols,depth) ));
  71. CALL_SUBTEST(( test_lazy_single<T,Rows,Cols,Depth,RowMajor,ColMajor,RowMajor>(rows,cols,depth) ));
  72. CALL_SUBTEST(( test_lazy_single<T,Rows,Cols,Depth,ColMajor,RowMajor,RowMajor>(rows,cols,depth) ));
  73. CALL_SUBTEST(( test_lazy_single<T,Rows,Cols,Depth,RowMajor,RowMajor,RowMajor>(rows,cols,depth) ));
  74. }
  75. template<typename T>
  76. void test_lazy_l1()
  77. {
  78. int rows = internal::random<int>(1,12);
  79. int cols = internal::random<int>(1,12);
  80. int depth = internal::random<int>(1,12);
  81. // Inner
  82. CALL_SUBTEST(( test_lazy_all_layout<T,1,1,1>() ));
  83. CALL_SUBTEST(( test_lazy_all_layout<T,1,1,2>() ));
  84. CALL_SUBTEST(( test_lazy_all_layout<T,1,1,3>() ));
  85. CALL_SUBTEST(( test_lazy_all_layout<T,1,1,8>() ));
  86. CALL_SUBTEST(( test_lazy_all_layout<T,1,1,9>() ));
  87. CALL_SUBTEST(( test_lazy_all_layout<T,1,1,-1>(1,1,depth) ));
  88. // Outer
  89. CALL_SUBTEST(( test_lazy_all_layout<T,2,1,1>() ));
  90. CALL_SUBTEST(( test_lazy_all_layout<T,1,2,1>() ));
  91. CALL_SUBTEST(( test_lazy_all_layout<T,2,2,1>() ));
  92. CALL_SUBTEST(( test_lazy_all_layout<T,3,3,1>() ));
  93. CALL_SUBTEST(( test_lazy_all_layout<T,4,4,1>() ));
  94. CALL_SUBTEST(( test_lazy_all_layout<T,4,8,1>() ));
  95. CALL_SUBTEST(( test_lazy_all_layout<T,4,-1,1>(4,cols) ));
  96. CALL_SUBTEST(( test_lazy_all_layout<T,7,-1,1>(7,cols) ));
  97. CALL_SUBTEST(( test_lazy_all_layout<T,-1,8,1>(rows) ));
  98. CALL_SUBTEST(( test_lazy_all_layout<T,-1,3,1>(rows) ));
  99. CALL_SUBTEST(( test_lazy_all_layout<T,-1,-1,1>(rows,cols) ));
  100. }
  101. template<typename T>
  102. void test_lazy_l2()
  103. {
  104. int rows = internal::random<int>(1,12);
  105. int cols = internal::random<int>(1,12);
  106. int depth = internal::random<int>(1,12);
  107. // mat-vec
  108. CALL_SUBTEST(( test_lazy_all_layout<T,2,1,2>() ));
  109. CALL_SUBTEST(( test_lazy_all_layout<T,2,1,4>() ));
  110. CALL_SUBTEST(( test_lazy_all_layout<T,4,1,2>() ));
  111. CALL_SUBTEST(( test_lazy_all_layout<T,4,1,4>() ));
  112. CALL_SUBTEST(( test_lazy_all_layout<T,5,1,4>() ));
  113. CALL_SUBTEST(( test_lazy_all_layout<T,4,1,5>() ));
  114. CALL_SUBTEST(( test_lazy_all_layout<T,4,1,6>() ));
  115. CALL_SUBTEST(( test_lazy_all_layout<T,6,1,4>() ));
  116. CALL_SUBTEST(( test_lazy_all_layout<T,8,1,8>() ));
  117. CALL_SUBTEST(( test_lazy_all_layout<T,-1,1,4>(rows) ));
  118. CALL_SUBTEST(( test_lazy_all_layout<T,4,1,-1>(4,1,depth) ));
  119. CALL_SUBTEST(( test_lazy_all_layout<T,-1,1,-1>(rows,1,depth) ));
  120. // vec-mat
  121. CALL_SUBTEST(( test_lazy_all_layout<T,1,2,2>() ));
  122. CALL_SUBTEST(( test_lazy_all_layout<T,1,2,4>() ));
  123. CALL_SUBTEST(( test_lazy_all_layout<T,1,4,2>() ));
  124. CALL_SUBTEST(( test_lazy_all_layout<T,1,4,4>() ));
  125. CALL_SUBTEST(( test_lazy_all_layout<T,1,5,4>() ));
  126. CALL_SUBTEST(( test_lazy_all_layout<T,1,4,5>() ));
  127. CALL_SUBTEST(( test_lazy_all_layout<T,1,4,6>() ));
  128. CALL_SUBTEST(( test_lazy_all_layout<T,1,6,4>() ));
  129. CALL_SUBTEST(( test_lazy_all_layout<T,1,8,8>() ));
  130. CALL_SUBTEST(( test_lazy_all_layout<T,1,-1, 4>(1,cols) ));
  131. CALL_SUBTEST(( test_lazy_all_layout<T,1, 4,-1>(1,4,depth) ));
  132. CALL_SUBTEST(( test_lazy_all_layout<T,1,-1,-1>(1,cols,depth) ));
  133. }
  134. template<typename T>
  135. void test_lazy_l3()
  136. {
  137. int rows = internal::random<int>(1,12);
  138. int cols = internal::random<int>(1,12);
  139. int depth = internal::random<int>(1,12);
  140. // mat-mat
  141. CALL_SUBTEST(( test_lazy_all_layout<T,2,4,2>() ));
  142. CALL_SUBTEST(( test_lazy_all_layout<T,2,6,4>() ));
  143. CALL_SUBTEST(( test_lazy_all_layout<T,4,3,2>() ));
  144. CALL_SUBTEST(( test_lazy_all_layout<T,4,8,4>() ));
  145. CALL_SUBTEST(( test_lazy_all_layout<T,5,6,4>() ));
  146. CALL_SUBTEST(( test_lazy_all_layout<T,4,2,5>() ));
  147. CALL_SUBTEST(( test_lazy_all_layout<T,4,7,6>() ));
  148. CALL_SUBTEST(( test_lazy_all_layout<T,6,8,4>() ));
  149. CALL_SUBTEST(( test_lazy_all_layout<T,8,3,8>() ));
  150. CALL_SUBTEST(( test_lazy_all_layout<T,-1,6,4>(rows) ));
  151. CALL_SUBTEST(( test_lazy_all_layout<T,4,3,-1>(4,3,depth) ));
  152. CALL_SUBTEST(( test_lazy_all_layout<T,-1,6,-1>(rows,6,depth) ));
  153. CALL_SUBTEST(( test_lazy_all_layout<T,8,2,2>() ));
  154. CALL_SUBTEST(( test_lazy_all_layout<T,5,2,4>() ));
  155. CALL_SUBTEST(( test_lazy_all_layout<T,4,4,2>() ));
  156. CALL_SUBTEST(( test_lazy_all_layout<T,8,4,4>() ));
  157. CALL_SUBTEST(( test_lazy_all_layout<T,6,5,4>() ));
  158. CALL_SUBTEST(( test_lazy_all_layout<T,4,4,5>() ));
  159. CALL_SUBTEST(( test_lazy_all_layout<T,3,4,6>() ));
  160. CALL_SUBTEST(( test_lazy_all_layout<T,2,6,4>() ));
  161. CALL_SUBTEST(( test_lazy_all_layout<T,7,8,8>() ));
  162. CALL_SUBTEST(( test_lazy_all_layout<T,8,-1, 4>(8,cols) ));
  163. CALL_SUBTEST(( test_lazy_all_layout<T,3, 4,-1>(3,4,depth) ));
  164. CALL_SUBTEST(( test_lazy_all_layout<T,4,-1,-1>(4,cols,depth) ));
  165. }
  166. template<typename T,int N,int M,int K>
  167. void test_linear_but_not_vectorizable()
  168. {
  169. // Check tricky cases for which the result of the product is a vector and thus must exhibit the LinearBit flag,
  170. // but is not vectorizable along the linear dimension.
  171. Index n = N==Dynamic ? internal::random<Index>(1,32) : N;
  172. Index m = M==Dynamic ? internal::random<Index>(1,32) : M;
  173. Index k = K==Dynamic ? internal::random<Index>(1,32) : K;
  174. {
  175. Matrix<T,N,M+1> A; A.setRandom(n,m+1);
  176. Matrix<T,M*2,K> B; B.setRandom(m*2,k);
  177. Matrix<T,1,K> C;
  178. Matrix<T,1,K> R;
  179. C.noalias() = A.template topLeftCorner<1,M>() * (B.template topRows<M>()+B.template bottomRows<M>());
  180. R.noalias() = A.template topLeftCorner<1,M>() * (B.template topRows<M>()+B.template bottomRows<M>()).eval();
  181. VERIFY_IS_APPROX(C,R);
  182. }
  183. {
  184. Matrix<T,M+1,N,RowMajor> A; A.setRandom(m+1,n);
  185. Matrix<T,K,M*2,RowMajor> B; B.setRandom(k,m*2);
  186. Matrix<T,K,1> C;
  187. Matrix<T,K,1> R;
  188. C.noalias() = (B.template leftCols<M>()+B.template rightCols<M>()) * A.template topLeftCorner<M,1>();
  189. R.noalias() = (B.template leftCols<M>()+B.template rightCols<M>()).eval() * A.template topLeftCorner<M,1>();
  190. VERIFY_IS_APPROX(C,R);
  191. }
  192. }
  193. template<int Rows>
  194. void bug_1311()
  195. {
  196. Matrix< double, Rows, 2 > A; A.setRandom();
  197. Vector2d b = Vector2d::Random() ;
  198. Matrix<double,Rows,1> res;
  199. res.noalias() = 1. * (A * b);
  200. VERIFY_IS_APPROX(res, A*b);
  201. res.noalias() = 1.*A * b;
  202. VERIFY_IS_APPROX(res, A*b);
  203. res.noalias() = (1.*A).lazyProduct(b);
  204. VERIFY_IS_APPROX(res, A*b);
  205. res.noalias() = (1.*A).lazyProduct(1.*b);
  206. VERIFY_IS_APPROX(res, A*b);
  207. res.noalias() = (A).lazyProduct(1.*b);
  208. VERIFY_IS_APPROX(res, A*b);
  209. }
  210. void test_product_small()
  211. {
  212. for(int i = 0; i < g_repeat; i++) {
  213. CALL_SUBTEST_1( product(Matrix<float, 3, 2>()) );
  214. CALL_SUBTEST_2( product(Matrix<int, 3, 17>()) );
  215. CALL_SUBTEST_8( product(Matrix<double, 3, 17>()) );
  216. CALL_SUBTEST_3( product(Matrix3d()) );
  217. CALL_SUBTEST_4( product(Matrix4d()) );
  218. CALL_SUBTEST_5( product(Matrix4f()) );
  219. CALL_SUBTEST_6( product1x1<0>() );
  220. CALL_SUBTEST_11( test_lazy_l1<float>() );
  221. CALL_SUBTEST_12( test_lazy_l2<float>() );
  222. CALL_SUBTEST_13( test_lazy_l3<float>() );
  223. CALL_SUBTEST_21( test_lazy_l1<double>() );
  224. CALL_SUBTEST_22( test_lazy_l2<double>() );
  225. CALL_SUBTEST_23( test_lazy_l3<double>() );
  226. CALL_SUBTEST_31( test_lazy_l1<std::complex<float> >() );
  227. CALL_SUBTEST_32( test_lazy_l2<std::complex<float> >() );
  228. CALL_SUBTEST_33( test_lazy_l3<std::complex<float> >() );
  229. CALL_SUBTEST_41( test_lazy_l1<std::complex<double> >() );
  230. CALL_SUBTEST_42( test_lazy_l2<std::complex<double> >() );
  231. CALL_SUBTEST_43( test_lazy_l3<std::complex<double> >() );
  232. CALL_SUBTEST_7(( test_linear_but_not_vectorizable<float,2,1,Dynamic>() ));
  233. CALL_SUBTEST_7(( test_linear_but_not_vectorizable<float,3,1,Dynamic>() ));
  234. CALL_SUBTEST_7(( test_linear_but_not_vectorizable<float,2,1,16>() ));
  235. CALL_SUBTEST_6( bug_1311<3>() );
  236. CALL_SUBTEST_6( bug_1311<5>() );
  237. }
  238. #ifdef EIGEN_TEST_PART_6
  239. {
  240. // test compilation of (outer_product) * vector
  241. Vector3f v = Vector3f::Random();
  242. VERIFY_IS_APPROX( (v * v.transpose()) * v, (v * v.transpose()).eval() * v);
  243. }
  244. {
  245. // regression test for pull-request #93
  246. Eigen::Matrix<double, 1, 1> A; A.setRandom();
  247. Eigen::Matrix<double, 18, 1> B; B.setRandom();
  248. Eigen::Matrix<double, 1, 18> C; C.setRandom();
  249. VERIFY_IS_APPROX(B * A.inverse(), B * A.inverse()[0]);
  250. VERIFY_IS_APPROX(A.inverse() * C, A.inverse()[0] * C);
  251. }
  252. {
  253. Eigen::Matrix<double, 10, 10> A, B, C;
  254. A.setRandom();
  255. C = A;
  256. for(int k=0; k<79; ++k)
  257. C = C * A;
  258. B.noalias() = (((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A)) * ((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A)))
  259. * (((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A)) * ((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A)));
  260. VERIFY_IS_APPROX(B,C);
  261. }
  262. #endif
  263. }