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.

204 lines
8.8 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. int c = internal::random(0,m2.cols()-1);
  14. int c1 = internal::random(0,m2.cols()-1);
  15. VERIFY_IS_APPROX(m4=m2.col(c)*refMat2.col(c1).transpose(), refMat4=refMat2.col(c)*refMat2.col(c1).transpose());
  16. VERIFY_IS_APPROX(m4=refMat2.col(c1)*m2.col(c).transpose(), refMat4=refMat2.col(c1)*refMat2.col(c).transpose());
  17. }
  18. };
  19. template<typename SparseMatrixType, typename DenseMatrix> struct test_outer<SparseMatrixType,DenseMatrix,true> {
  20. static void run(SparseMatrixType& m2, SparseMatrixType& m4, DenseMatrix& refMat2, DenseMatrix& refMat4) {
  21. int r = internal::random(0,m2.rows()-1);
  22. int c1 = internal::random(0,m2.cols()-1);
  23. VERIFY_IS_APPROX(m4=m2.row(r).transpose()*refMat2.col(c1).transpose(), refMat4=refMat2.row(r).transpose()*refMat2.col(c1).transpose());
  24. VERIFY_IS_APPROX(m4=refMat2.col(c1)*m2.row(r), refMat4=refMat2.col(c1)*refMat2.row(r));
  25. }
  26. };
  27. // (m2,m4,refMat2,refMat4,dv1);
  28. // VERIFY_IS_APPROX(m4=m2.innerVector(c)*dv1.transpose(), refMat4=refMat2.colVector(c)*dv1.transpose());
  29. // VERIFY_IS_APPROX(m4=dv1*mcm.col(c).transpose(), refMat4=dv1*refMat2.col(c).transpose());
  30. template<typename SparseMatrixType> void sparse_product()
  31. {
  32. typedef typename SparseMatrixType::Index Index;
  33. Index n = 100;
  34. const Index rows = internal::random<int>(1,n);
  35. const Index cols = internal::random<int>(1,n);
  36. const Index depth = internal::random<int>(1,n);
  37. typedef typename SparseMatrixType::Scalar Scalar;
  38. enum { Flags = SparseMatrixType::Flags };
  39. double density = (std::max)(8./(rows*cols), 0.01);
  40. typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
  41. typedef Matrix<Scalar,Dynamic,1> DenseVector;
  42. Scalar s1 = internal::random<Scalar>();
  43. Scalar s2 = internal::random<Scalar>();
  44. // test matrix-matrix product
  45. {
  46. DenseMatrix refMat2 = DenseMatrix::Zero(rows, depth);
  47. DenseMatrix refMat2t = DenseMatrix::Zero(depth, rows);
  48. DenseMatrix refMat3 = DenseMatrix::Zero(depth, cols);
  49. DenseMatrix refMat3t = DenseMatrix::Zero(cols, depth);
  50. DenseMatrix refMat4 = DenseMatrix::Zero(rows, cols);
  51. DenseMatrix refMat4t = DenseMatrix::Zero(cols, rows);
  52. DenseMatrix refMat5 = DenseMatrix::Random(depth, cols);
  53. DenseMatrix refMat6 = DenseMatrix::Random(rows, rows);
  54. DenseMatrix dm4 = DenseMatrix::Zero(rows, rows);
  55. // DenseVector dv1 = DenseVector::Random(rows);
  56. SparseMatrixType m2 (rows, depth);
  57. SparseMatrixType m2t(depth, rows);
  58. SparseMatrixType m3 (depth, cols);
  59. SparseMatrixType m3t(cols, depth);
  60. SparseMatrixType m4 (rows, cols);
  61. SparseMatrixType m4t(cols, rows);
  62. SparseMatrixType m6(rows, rows);
  63. initSparse(density, refMat2, m2);
  64. initSparse(density, refMat2t, m2t);
  65. initSparse(density, refMat3, m3);
  66. initSparse(density, refMat3t, m3t);
  67. initSparse(density, refMat4, m4);
  68. initSparse(density, refMat4t, m4t);
  69. initSparse(density, refMat6, m6);
  70. // int c = internal::random<int>(0,depth-1);
  71. // sparse * sparse
  72. VERIFY_IS_APPROX(m4=m2*m3, refMat4=refMat2*refMat3);
  73. VERIFY_IS_APPROX(m4=m2t.transpose()*m3, refMat4=refMat2t.transpose()*refMat3);
  74. VERIFY_IS_APPROX(m4=m2t.transpose()*m3t.transpose(), refMat4=refMat2t.transpose()*refMat3t.transpose());
  75. VERIFY_IS_APPROX(m4=m2*m3t.transpose(), refMat4=refMat2*refMat3t.transpose());
  76. VERIFY_IS_APPROX(m4 = m2*m3/s1, refMat4 = refMat2*refMat3/s1);
  77. VERIFY_IS_APPROX(m4 = m2*m3*s1, refMat4 = refMat2*refMat3*s1);
  78. VERIFY_IS_APPROX(m4 = s2*m2*m3*s1, refMat4 = s2*refMat2*refMat3*s1);
  79. VERIFY_IS_APPROX(m4=(m2*m3).pruned(0), refMat4=refMat2*refMat3);
  80. VERIFY_IS_APPROX(m4=(m2t.transpose()*m3).pruned(0), refMat4=refMat2t.transpose()*refMat3);
  81. VERIFY_IS_APPROX(m4=(m2t.transpose()*m3t.transpose()).pruned(0), refMat4=refMat2t.transpose()*refMat3t.transpose());
  82. VERIFY_IS_APPROX(m4=(m2*m3t.transpose()).pruned(0), refMat4=refMat2*refMat3t.transpose());
  83. // test aliasing
  84. m4 = m2; refMat4 = refMat2;
  85. VERIFY_IS_APPROX(m4=m4*m3, refMat4=refMat4*refMat3);
  86. // sparse * dense
  87. VERIFY_IS_APPROX(dm4=m2*refMat3, refMat4=refMat2*refMat3);
  88. VERIFY_IS_APPROX(dm4=m2*refMat3t.transpose(), refMat4=refMat2*refMat3t.transpose());
  89. VERIFY_IS_APPROX(dm4=m2t.transpose()*refMat3, refMat4=refMat2t.transpose()*refMat3);
  90. VERIFY_IS_APPROX(dm4=m2t.transpose()*refMat3t.transpose(), refMat4=refMat2t.transpose()*refMat3t.transpose());
  91. VERIFY_IS_APPROX(dm4=m2*(refMat3+refMat3), refMat4=refMat2*(refMat3+refMat3));
  92. VERIFY_IS_APPROX(dm4=m2t.transpose()*(refMat3+refMat5)*0.5, refMat4=refMat2t.transpose()*(refMat3+refMat5)*0.5);
  93. // dense * sparse
  94. VERIFY_IS_APPROX(dm4=refMat2*m3, refMat4=refMat2*refMat3);
  95. VERIFY_IS_APPROX(dm4=refMat2*m3t.transpose(), refMat4=refMat2*refMat3t.transpose());
  96. VERIFY_IS_APPROX(dm4=refMat2t.transpose()*m3, refMat4=refMat2t.transpose()*refMat3);
  97. VERIFY_IS_APPROX(dm4=refMat2t.transpose()*m3t.transpose(), refMat4=refMat2t.transpose()*refMat3t.transpose());
  98. // sparse * dense and dense * sparse outer product
  99. test_outer<SparseMatrixType,DenseMatrix>::run(m2,m4,refMat2,refMat4);
  100. VERIFY_IS_APPROX(m6=m6*m6, refMat6=refMat6*refMat6);
  101. }
  102. // test matrix - diagonal product
  103. {
  104. DenseMatrix refM2 = DenseMatrix::Zero(rows, rows);
  105. DenseMatrix refM3 = DenseMatrix::Zero(rows, rows);
  106. DiagonalMatrix<Scalar,Dynamic> d1(DenseVector::Random(rows));
  107. SparseMatrixType m2(rows, rows);
  108. SparseMatrixType m3(rows, rows);
  109. initSparse<Scalar>(density, refM2, m2);
  110. initSparse<Scalar>(density, refM3, m3);
  111. VERIFY_IS_APPROX(m3=m2*d1, refM3=refM2*d1);
  112. VERIFY_IS_APPROX(m3=m2.transpose()*d1, refM3=refM2.transpose()*d1);
  113. VERIFY_IS_APPROX(m3=d1*m2, refM3=d1*refM2);
  114. VERIFY_IS_APPROX(m3=d1*m2.transpose(), refM3=d1 * refM2.transpose());
  115. }
  116. // test self adjoint products
  117. {
  118. DenseMatrix b = DenseMatrix::Random(rows, rows);
  119. DenseMatrix x = DenseMatrix::Random(rows, rows);
  120. DenseMatrix refX = DenseMatrix::Random(rows, rows);
  121. DenseMatrix refUp = DenseMatrix::Zero(rows, rows);
  122. DenseMatrix refLo = DenseMatrix::Zero(rows, rows);
  123. DenseMatrix refS = DenseMatrix::Zero(rows, rows);
  124. SparseMatrixType mUp(rows, rows);
  125. SparseMatrixType mLo(rows, rows);
  126. SparseMatrixType mS(rows, rows);
  127. do {
  128. initSparse<Scalar>(density, refUp, mUp, ForceRealDiag|/*ForceNonZeroDiag|*/MakeUpperTriangular);
  129. } while (refUp.isZero());
  130. refLo = refUp.adjoint();
  131. mLo = mUp.adjoint();
  132. refS = refUp + refLo;
  133. refS.diagonal() *= 0.5;
  134. mS = mUp + mLo;
  135. // TODO be able to address the diagonal....
  136. for (int k=0; k<mS.outerSize(); ++k)
  137. for (typename SparseMatrixType::InnerIterator it(mS,k); it; ++it)
  138. if (it.index() == k)
  139. it.valueRef() *= 0.5;
  140. VERIFY_IS_APPROX(refS.adjoint(), refS);
  141. VERIFY_IS_APPROX(mS.adjoint(), mS);
  142. VERIFY_IS_APPROX(mS, refS);
  143. VERIFY_IS_APPROX(x=mS*b, refX=refS*b);
  144. VERIFY_IS_APPROX(x=mUp.template selfadjointView<Upper>()*b, refX=refS*b);
  145. VERIFY_IS_APPROX(x=mLo.template selfadjointView<Lower>()*b, refX=refS*b);
  146. VERIFY_IS_APPROX(x=mS.template selfadjointView<Upper|Lower>()*b, refX=refS*b);
  147. }
  148. }
  149. // New test for Bug in SparseTimeDenseProduct
  150. template<typename SparseMatrixType, typename DenseMatrixType> void sparse_product_regression_test()
  151. {
  152. // This code does not compile with afflicted versions of the bug
  153. SparseMatrixType sm1(3,2);
  154. DenseMatrixType m2(2,2);
  155. sm1.setZero();
  156. m2.setZero();
  157. DenseMatrixType m3 = sm1*m2;
  158. // This code produces a segfault with afflicted versions of another SparseTimeDenseProduct
  159. // bug
  160. SparseMatrixType sm2(20000,2);
  161. sm2.setZero();
  162. DenseMatrixType m4(sm2*m2);
  163. VERIFY_IS_APPROX( m4(0,0), 0.0 );
  164. }
  165. void test_sparse_product()
  166. {
  167. for(int i = 0; i < g_repeat; i++) {
  168. CALL_SUBTEST_1( (sparse_product<SparseMatrix<double,ColMajor> >()) );
  169. CALL_SUBTEST_1( (sparse_product<SparseMatrix<double,RowMajor> >()) );
  170. CALL_SUBTEST_2( (sparse_product<SparseMatrix<std::complex<double>, ColMajor > >()) );
  171. CALL_SUBTEST_2( (sparse_product<SparseMatrix<std::complex<double>, RowMajor > >()) );
  172. CALL_SUBTEST_4( (sparse_product_regression_test<SparseMatrix<double,RowMajor>, Matrix<double, Dynamic, Dynamic, RowMajor> >()) );
  173. }
  174. }