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.

252 lines
11 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008-2011 Gael Guennebaud <gael.guennebaud@inria.fr>
  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 "sparse.h"
  10. template<typename SparseMatrixType, typename DenseMatrix, bool IsRowMajor=SparseMatrixType::IsRowMajor> struct test_outer;
  11. template<typename SparseMatrixType, typename DenseMatrix> struct test_outer<SparseMatrixType,DenseMatrix,false> {
  12. static void run(SparseMatrixType& m2, SparseMatrixType& m4, DenseMatrix& refMat2, DenseMatrix& refMat4) {
  13. typedef typename SparseMatrixType::Index Index;
  14. Index c = internal::random<Index>(0,m2.cols()-1);
  15. Index c1 = internal::random<Index>(0,m2.cols()-1);
  16. VERIFY_IS_APPROX(m4=m2.col(c)*refMat2.col(c1).transpose(), refMat4=refMat2.col(c)*refMat2.col(c1).transpose());
  17. VERIFY_IS_APPROX(m4=refMat2.col(c1)*m2.col(c).transpose(), refMat4=refMat2.col(c1)*refMat2.col(c).transpose());
  18. }
  19. };
  20. template<typename SparseMatrixType, typename DenseMatrix> struct test_outer<SparseMatrixType,DenseMatrix,true> {
  21. static void run(SparseMatrixType& m2, SparseMatrixType& m4, DenseMatrix& refMat2, DenseMatrix& refMat4) {
  22. typedef typename SparseMatrixType::Index Index;
  23. Index r = internal::random<Index>(0,m2.rows()-1);
  24. Index c1 = internal::random<Index>(0,m2.cols()-1);
  25. VERIFY_IS_APPROX(m4=m2.row(r).transpose()*refMat2.col(c1).transpose(), refMat4=refMat2.row(r).transpose()*refMat2.col(c1).transpose());
  26. VERIFY_IS_APPROX(m4=refMat2.col(c1)*m2.row(r), refMat4=refMat2.col(c1)*refMat2.row(r));
  27. }
  28. };
  29. // (m2,m4,refMat2,refMat4,dv1);
  30. // VERIFY_IS_APPROX(m4=m2.innerVector(c)*dv1.transpose(), refMat4=refMat2.colVector(c)*dv1.transpose());
  31. // VERIFY_IS_APPROX(m4=dv1*mcm.col(c).transpose(), refMat4=dv1*refMat2.col(c).transpose());
  32. template<typename SparseMatrixType> void sparse_product()
  33. {
  34. typedef typename SparseMatrixType::Index Index;
  35. Index n = 100;
  36. const Index rows = internal::random<Index>(1,n);
  37. const Index cols = internal::random<Index>(1,n);
  38. const Index depth = internal::random<Index>(1,n);
  39. typedef typename SparseMatrixType::Scalar Scalar;
  40. enum { Flags = SparseMatrixType::Flags };
  41. double density = (std::max)(8./(rows*cols), 0.1);
  42. typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
  43. typedef Matrix<Scalar,Dynamic,1> DenseVector;
  44. typedef Matrix<Scalar,1,Dynamic> RowDenseVector;
  45. typedef SparseVector<Scalar,0,Index> ColSpVector;
  46. typedef SparseVector<Scalar,RowMajor,Index> RowSpVector;
  47. Scalar s1 = internal::random<Scalar>();
  48. Scalar s2 = internal::random<Scalar>();
  49. // test matrix-matrix product
  50. {
  51. DenseMatrix refMat2 = DenseMatrix::Zero(rows, depth);
  52. DenseMatrix refMat2t = DenseMatrix::Zero(depth, rows);
  53. DenseMatrix refMat3 = DenseMatrix::Zero(depth, cols);
  54. DenseMatrix refMat3t = DenseMatrix::Zero(cols, depth);
  55. DenseMatrix refMat4 = DenseMatrix::Zero(rows, cols);
  56. DenseMatrix refMat4t = DenseMatrix::Zero(cols, rows);
  57. DenseMatrix refMat5 = DenseMatrix::Random(depth, cols);
  58. DenseMatrix refMat6 = DenseMatrix::Random(rows, rows);
  59. DenseMatrix dm4 = DenseMatrix::Zero(rows, rows);
  60. // DenseVector dv1 = DenseVector::Random(rows);
  61. SparseMatrixType m2 (rows, depth);
  62. SparseMatrixType m2t(depth, rows);
  63. SparseMatrixType m3 (depth, cols);
  64. SparseMatrixType m3t(cols, depth);
  65. SparseMatrixType m4 (rows, cols);
  66. SparseMatrixType m4t(cols, rows);
  67. SparseMatrixType m6(rows, rows);
  68. initSparse(density, refMat2, m2);
  69. initSparse(density, refMat2t, m2t);
  70. initSparse(density, refMat3, m3);
  71. initSparse(density, refMat3t, m3t);
  72. initSparse(density, refMat4, m4);
  73. initSparse(density, refMat4t, m4t);
  74. initSparse(density, refMat6, m6);
  75. // int c = internal::random<int>(0,depth-1);
  76. // sparse * sparse
  77. VERIFY_IS_APPROX(m4=m2*m3, refMat4=refMat2*refMat3);
  78. VERIFY_IS_APPROX(m4=m2t.transpose()*m3, refMat4=refMat2t.transpose()*refMat3);
  79. VERIFY_IS_APPROX(m4=m2t.transpose()*m3t.transpose(), refMat4=refMat2t.transpose()*refMat3t.transpose());
  80. VERIFY_IS_APPROX(m4=m2*m3t.transpose(), refMat4=refMat2*refMat3t.transpose());
  81. VERIFY_IS_APPROX(m4 = m2*m3/s1, refMat4 = refMat2*refMat3/s1);
  82. VERIFY_IS_APPROX(m4 = m2*m3*s1, refMat4 = refMat2*refMat3*s1);
  83. VERIFY_IS_APPROX(m4 = s2*m2*m3*s1, refMat4 = s2*refMat2*refMat3*s1);
  84. VERIFY_IS_APPROX(m4=(m2*m3).pruned(0), refMat4=refMat2*refMat3);
  85. VERIFY_IS_APPROX(m4=(m2t.transpose()*m3).pruned(0), refMat4=refMat2t.transpose()*refMat3);
  86. VERIFY_IS_APPROX(m4=(m2t.transpose()*m3t.transpose()).pruned(0), refMat4=refMat2t.transpose()*refMat3t.transpose());
  87. VERIFY_IS_APPROX(m4=(m2*m3t.transpose()).pruned(0), refMat4=refMat2*refMat3t.transpose());
  88. // test aliasing
  89. m4 = m2; refMat4 = refMat2;
  90. VERIFY_IS_APPROX(m4=m4*m3, refMat4=refMat4*refMat3);
  91. // sparse * dense
  92. VERIFY_IS_APPROX(dm4=m2*refMat3, refMat4=refMat2*refMat3);
  93. VERIFY_IS_APPROX(dm4=m2*refMat3t.transpose(), refMat4=refMat2*refMat3t.transpose());
  94. VERIFY_IS_APPROX(dm4=m2t.transpose()*refMat3, refMat4=refMat2t.transpose()*refMat3);
  95. VERIFY_IS_APPROX(dm4=m2t.transpose()*refMat3t.transpose(), refMat4=refMat2t.transpose()*refMat3t.transpose());
  96. VERIFY_IS_APPROX(dm4=m2*(refMat3+refMat3), refMat4=refMat2*(refMat3+refMat3));
  97. VERIFY_IS_APPROX(dm4=m2t.transpose()*(refMat3+refMat5)*0.5, refMat4=refMat2t.transpose()*(refMat3+refMat5)*0.5);
  98. // dense * sparse
  99. VERIFY_IS_APPROX(dm4=refMat2*m3, refMat4=refMat2*refMat3);
  100. VERIFY_IS_APPROX(dm4=refMat2*m3t.transpose(), refMat4=refMat2*refMat3t.transpose());
  101. VERIFY_IS_APPROX(dm4=refMat2t.transpose()*m3, refMat4=refMat2t.transpose()*refMat3);
  102. VERIFY_IS_APPROX(dm4=refMat2t.transpose()*m3t.transpose(), refMat4=refMat2t.transpose()*refMat3t.transpose());
  103. // sparse * dense and dense * sparse outer product
  104. test_outer<SparseMatrixType,DenseMatrix>::run(m2,m4,refMat2,refMat4);
  105. VERIFY_IS_APPROX(m6=m6*m6, refMat6=refMat6*refMat6);
  106. // sparse matrix * sparse vector
  107. ColSpVector cv0(cols), cv1;
  108. DenseVector dcv0(cols), dcv1;
  109. initSparse(2*density,dcv0, cv0);
  110. RowSpVector rv0(depth), rv1;
  111. RowDenseVector drv0(depth), drv1(rv1);
  112. initSparse(2*density,drv0, rv0);
  113. VERIFY_IS_APPROX(cv1=rv0*m3, dcv1=drv0*refMat3);
  114. VERIFY_IS_APPROX(rv1=rv0*m3, drv1=drv0*refMat3);
  115. VERIFY_IS_APPROX(cv1=m3*cv0, dcv1=refMat3*dcv0);
  116. VERIFY_IS_APPROX(cv1=m3t.adjoint()*cv0, dcv1=refMat3t.adjoint()*dcv0);
  117. VERIFY_IS_APPROX(rv1=m3*cv0, drv1=refMat3*dcv0);
  118. }
  119. // test matrix - diagonal product
  120. {
  121. DenseMatrix refM2 = DenseMatrix::Zero(rows, cols);
  122. DenseMatrix refM3 = DenseMatrix::Zero(rows, cols);
  123. DenseMatrix d3 = DenseMatrix::Zero(rows, cols);
  124. DiagonalMatrix<Scalar,Dynamic> d1(DenseVector::Random(cols));
  125. DiagonalMatrix<Scalar,Dynamic> d2(DenseVector::Random(rows));
  126. SparseMatrixType m2(rows, cols);
  127. SparseMatrixType m3(rows, cols);
  128. initSparse<Scalar>(density, refM2, m2);
  129. initSparse<Scalar>(density, refM3, m3);
  130. VERIFY_IS_APPROX(m3=m2*d1, refM3=refM2*d1);
  131. VERIFY_IS_APPROX(m3=m2.transpose()*d2, refM3=refM2.transpose()*d2);
  132. VERIFY_IS_APPROX(m3=d2*m2, refM3=d2*refM2);
  133. VERIFY_IS_APPROX(m3=d1*m2.transpose(), refM3=d1*refM2.transpose());
  134. // also check with a SparseWrapper:
  135. DenseVector v1 = DenseVector::Random(cols);
  136. DenseVector v2 = DenseVector::Random(rows);
  137. VERIFY_IS_APPROX(m3=m2*v1.asDiagonal(), refM3=refM2*v1.asDiagonal());
  138. VERIFY_IS_APPROX(m3=m2.transpose()*v2.asDiagonal(), refM3=refM2.transpose()*v2.asDiagonal());
  139. VERIFY_IS_APPROX(m3=v2.asDiagonal()*m2, refM3=v2.asDiagonal()*refM2);
  140. VERIFY_IS_APPROX(m3=v1.asDiagonal()*m2.transpose(), refM3=v1.asDiagonal()*refM2.transpose());
  141. VERIFY_IS_APPROX(m3=v2.asDiagonal()*m2*v1.asDiagonal(), refM3=v2.asDiagonal()*refM2*v1.asDiagonal());
  142. // evaluate to a dense matrix to check the .row() and .col() iterator functions
  143. VERIFY_IS_APPROX(d3=m2*d1, refM3=refM2*d1);
  144. VERIFY_IS_APPROX(d3=m2.transpose()*d2, refM3=refM2.transpose()*d2);
  145. VERIFY_IS_APPROX(d3=d2*m2, refM3=d2*refM2);
  146. VERIFY_IS_APPROX(d3=d1*m2.transpose(), refM3=d1*refM2.transpose());
  147. }
  148. // test self adjoint products
  149. {
  150. DenseMatrix b = DenseMatrix::Random(rows, rows);
  151. DenseMatrix x = DenseMatrix::Random(rows, rows);
  152. DenseMatrix refX = DenseMatrix::Random(rows, rows);
  153. DenseMatrix refUp = DenseMatrix::Zero(rows, rows);
  154. DenseMatrix refLo = DenseMatrix::Zero(rows, rows);
  155. DenseMatrix refS = DenseMatrix::Zero(rows, rows);
  156. SparseMatrixType mUp(rows, rows);
  157. SparseMatrixType mLo(rows, rows);
  158. SparseMatrixType mS(rows, rows);
  159. do {
  160. initSparse<Scalar>(density, refUp, mUp, ForceRealDiag|/*ForceNonZeroDiag|*/MakeUpperTriangular);
  161. } while (refUp.isZero());
  162. refLo = refUp.adjoint();
  163. mLo = mUp.adjoint();
  164. refS = refUp + refLo;
  165. refS.diagonal() *= 0.5;
  166. mS = mUp + mLo;
  167. // TODO be able to address the diagonal....
  168. for (int k=0; k<mS.outerSize(); ++k)
  169. for (typename SparseMatrixType::InnerIterator it(mS,k); it; ++it)
  170. if (it.index() == k)
  171. it.valueRef() *= 0.5;
  172. VERIFY_IS_APPROX(refS.adjoint(), refS);
  173. VERIFY_IS_APPROX(mS.adjoint(), mS);
  174. VERIFY_IS_APPROX(mS, refS);
  175. VERIFY_IS_APPROX(x=mS*b, refX=refS*b);
  176. VERIFY_IS_APPROX(x=mUp.template selfadjointView<Upper>()*b, refX=refS*b);
  177. VERIFY_IS_APPROX(x=mLo.template selfadjointView<Lower>()*b, refX=refS*b);
  178. VERIFY_IS_APPROX(x=mS.template selfadjointView<Upper|Lower>()*b, refX=refS*b);
  179. // sparse selfadjointView * sparse
  180. SparseMatrixType mSres(rows,rows);
  181. VERIFY_IS_APPROX(mSres = mLo.template selfadjointView<Lower>()*mS,
  182. refX = refLo.template selfadjointView<Lower>()*refS);
  183. // sparse * sparse selfadjointview
  184. VERIFY_IS_APPROX(mSres = mS * mLo.template selfadjointView<Lower>(),
  185. refX = refS * refLo.template selfadjointView<Lower>());
  186. }
  187. }
  188. // New test for Bug in SparseTimeDenseProduct
  189. template<typename SparseMatrixType, typename DenseMatrixType> void sparse_product_regression_test()
  190. {
  191. // This code does not compile with afflicted versions of the bug
  192. SparseMatrixType sm1(3,2);
  193. DenseMatrixType m2(2,2);
  194. sm1.setZero();
  195. m2.setZero();
  196. DenseMatrixType m3 = sm1*m2;
  197. // This code produces a segfault with afflicted versions of another SparseTimeDenseProduct
  198. // bug
  199. SparseMatrixType sm2(20000,2);
  200. sm2.setZero();
  201. DenseMatrixType m4(sm2*m2);
  202. VERIFY_IS_APPROX( m4(0,0), 0.0 );
  203. }
  204. void test_sparse_product()
  205. {
  206. for(int i = 0; i < g_repeat; i++) {
  207. CALL_SUBTEST_1( (sparse_product<SparseMatrix<double,ColMajor> >()) );
  208. CALL_SUBTEST_1( (sparse_product<SparseMatrix<double,RowMajor> >()) );
  209. CALL_SUBTEST_2( (sparse_product<SparseMatrix<std::complex<double>, ColMajor > >()) );
  210. CALL_SUBTEST_2( (sparse_product<SparseMatrix<std::complex<double>, RowMajor > >()) );
  211. CALL_SUBTEST_3( (sparse_product<SparseMatrix<float,ColMajor,long int> >()) );
  212. CALL_SUBTEST_4( (sparse_product_regression_test<SparseMatrix<double,RowMajor>, Matrix<double, Dynamic, Dynamic, RowMajor> >()) );
  213. }
  214. }