sparse_extra.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008-2010 Gael Guennebaud <g.gael@free.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. // import basic and product tests for deprecated DynamicSparseMatrix
  10. #if 0 // sparse_basic(DynamicSparseMatrix) does not compile at all -> disabled
  11. static long g_realloc_count = 0;
  12. #define EIGEN_SPARSE_COMPRESSED_STORAGE_REALLOCATE_PLUGIN g_realloc_count++;
  13. static long g_dense_op_sparse_count = 0;
  14. #define EIGEN_SPARSE_ASSIGNMENT_FROM_DENSE_OP_SPARSE_PLUGIN g_dense_op_sparse_count++;
  15. #define EIGEN_SPARSE_ASSIGNMENT_FROM_SPARSE_ADD_DENSE_PLUGIN g_dense_op_sparse_count+=10;
  16. #define EIGEN_SPARSE_ASSIGNMENT_FROM_SPARSE_SUB_DENSE_PLUGIN g_dense_op_sparse_count+=20;
  17. #define EIGEN_SPARSE_TEST_INCLUDED_FROM_SPARSE_EXTRA 1
  18. #endif
  19. #define EIGEN_NO_DEPRECATED_WARNING
  20. // Disable counting of temporaries, since sparse_product(DynamicSparseMatrix)
  21. // has an extra copy-assignment.
  22. #define EIGEN_SPARSE_PRODUCT_IGNORE_TEMPORARY_COUNT
  23. #include "sparse_product.cpp"
  24. #if 0 // sparse_basic(DynamicSparseMatrix) does not compile at all -> disabled
  25. #include "sparse_basic.cpp"
  26. #endif
  27. #if EIGEN_HAS_CXX11
  28. #ifdef min
  29. #undef min
  30. #endif
  31. #ifdef max
  32. #undef max
  33. #endif
  34. #include <unordered_map>
  35. #define EIGEN_UNORDERED_MAP_SUPPORT
  36. #endif
  37. #include <Eigen/SparseExtra>
  38. template<typename SetterType,typename DenseType, typename Scalar, int Options>
  39. bool test_random_setter(SparseMatrix<Scalar,Options>& sm, const DenseType& ref, const std::vector<Vector2i>& nonzeroCoords)
  40. {
  41. {
  42. sm.setZero();
  43. SetterType w(sm);
  44. std::vector<Vector2i> remaining = nonzeroCoords;
  45. while(!remaining.empty())
  46. {
  47. int i = internal::random<int>(0,static_cast<int>(remaining.size())-1);
  48. w(remaining[i].x(),remaining[i].y()) = ref.coeff(remaining[i].x(),remaining[i].y());
  49. remaining[i] = remaining.back();
  50. remaining.pop_back();
  51. }
  52. }
  53. return sm.isApprox(ref);
  54. }
  55. template<typename SetterType,typename DenseType, typename T>
  56. bool test_random_setter(DynamicSparseMatrix<T>& sm, const DenseType& ref, const std::vector<Vector2i>& nonzeroCoords)
  57. {
  58. sm.setZero();
  59. std::vector<Vector2i> remaining = nonzeroCoords;
  60. while(!remaining.empty())
  61. {
  62. int i = internal::random<int>(0,static_cast<int>(remaining.size())-1);
  63. sm.coeffRef(remaining[i].x(),remaining[i].y()) = ref.coeff(remaining[i].x(),remaining[i].y());
  64. remaining[i] = remaining.back();
  65. remaining.pop_back();
  66. }
  67. return sm.isApprox(ref);
  68. }
  69. template<typename SparseMatrixType> void sparse_extra(const SparseMatrixType& ref)
  70. {
  71. const Index rows = ref.rows();
  72. const Index cols = ref.cols();
  73. typedef typename SparseMatrixType::Scalar Scalar;
  74. enum { Flags = SparseMatrixType::Flags };
  75. double density = (std::max)(8./(rows*cols), 0.01);
  76. typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
  77. typedef Matrix<Scalar,Dynamic,1> DenseVector;
  78. Scalar eps = 1e-6;
  79. SparseMatrixType m(rows, cols);
  80. DenseMatrix refMat = DenseMatrix::Zero(rows, cols);
  81. DenseVector vec1 = DenseVector::Random(rows);
  82. std::vector<Vector2i> zeroCoords;
  83. std::vector<Vector2i> nonzeroCoords;
  84. initSparse<Scalar>(density, refMat, m, 0, &zeroCoords, &nonzeroCoords);
  85. if (zeroCoords.size()==0 || nonzeroCoords.size()==0)
  86. return;
  87. // test coeff and coeffRef
  88. for (int i=0; i<(int)zeroCoords.size(); ++i)
  89. {
  90. VERIFY_IS_MUCH_SMALLER_THAN( m.coeff(zeroCoords[i].x(),zeroCoords[i].y()), eps );
  91. if(internal::is_same<SparseMatrixType,SparseMatrix<Scalar,Flags> >::value)
  92. VERIFY_RAISES_ASSERT( m.coeffRef(zeroCoords[0].x(),zeroCoords[0].y()) = 5 );
  93. }
  94. VERIFY_IS_APPROX(m, refMat);
  95. m.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5);
  96. refMat.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5);
  97. VERIFY_IS_APPROX(m, refMat);
  98. // random setter
  99. // {
  100. // m.setZero();
  101. // VERIFY_IS_NOT_APPROX(m, refMat);
  102. // SparseSetter<SparseMatrixType, RandomAccessPattern> w(m);
  103. // std::vector<Vector2i> remaining = nonzeroCoords;
  104. // while(!remaining.empty())
  105. // {
  106. // int i = internal::random<int>(0,remaining.size()-1);
  107. // w->coeffRef(remaining[i].x(),remaining[i].y()) = refMat.coeff(remaining[i].x(),remaining[i].y());
  108. // remaining[i] = remaining.back();
  109. // remaining.pop_back();
  110. // }
  111. // }
  112. // VERIFY_IS_APPROX(m, refMat);
  113. VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, StdMapTraits> >(m,refMat,nonzeroCoords) ));
  114. #ifdef EIGEN_UNORDERED_MAP_SUPPORT
  115. VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, StdUnorderedMapTraits> >(m,refMat,nonzeroCoords) ));
  116. #endif
  117. #ifdef EIGEN_GOOGLEHASH_SUPPORT
  118. VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, GoogleDenseHashMapTraits> >(m,refMat,nonzeroCoords) ));
  119. VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, GoogleSparseHashMapTraits> >(m,refMat,nonzeroCoords) ));
  120. #endif
  121. // test RandomSetter
  122. /*{
  123. SparseMatrixType m1(rows,cols), m2(rows,cols);
  124. DenseMatrix refM1 = DenseMatrix::Zero(rows, rows);
  125. initSparse<Scalar>(density, refM1, m1);
  126. {
  127. Eigen::RandomSetter<SparseMatrixType > setter(m2);
  128. for (int j=0; j<m1.outerSize(); ++j)
  129. for (typename SparseMatrixType::InnerIterator i(m1,j); i; ++i)
  130. setter(i.index(), j) = i.value();
  131. }
  132. VERIFY_IS_APPROX(m1, m2);
  133. }*/
  134. }
  135. template<typename SparseMatrixType>
  136. void check_marketio()
  137. {
  138. typedef Matrix<typename SparseMatrixType::Scalar, Dynamic, Dynamic> DenseMatrix;
  139. Index rows = internal::random<Index>(1,100);
  140. Index cols = internal::random<Index>(1,100);
  141. SparseMatrixType m1, m2;
  142. m1 = DenseMatrix::Random(rows, cols).sparseView();
  143. saveMarket(m1, "sparse_extra.mtx");
  144. loadMarket(m2, "sparse_extra.mtx");
  145. VERIFY_IS_EQUAL(DenseMatrix(m1),DenseMatrix(m2));
  146. }
  147. template<typename VectorType>
  148. void check_marketio_vector()
  149. {
  150. Index size = internal::random<Index>(1,100);
  151. VectorType v1, v2;
  152. v1 = VectorType::Random(size);
  153. saveMarketVector(v1, "vector_extra.mtx");
  154. loadMarketVector(v2, "vector_extra.mtx");
  155. VERIFY_IS_EQUAL(v1,v2);
  156. }
  157. EIGEN_DECLARE_TEST(sparse_extra)
  158. {
  159. for(int i = 0; i < g_repeat; i++) {
  160. int s = Eigen::internal::random<int>(1,50);
  161. CALL_SUBTEST_1( sparse_extra(SparseMatrix<double>(8, 8)) );
  162. CALL_SUBTEST_2( sparse_extra(SparseMatrix<std::complex<double> >(s, s)) );
  163. CALL_SUBTEST_1( sparse_extra(SparseMatrix<double>(s, s)) );
  164. CALL_SUBTEST_3( sparse_extra(DynamicSparseMatrix<double>(s, s)) );
  165. // CALL_SUBTEST_3(( sparse_basic(DynamicSparseMatrix<double>(s, s)) ));
  166. // CALL_SUBTEST_3(( sparse_basic(DynamicSparseMatrix<double,ColMajor,long int>(s, s)) ));
  167. CALL_SUBTEST_3( (sparse_product<DynamicSparseMatrix<float, ColMajor> >()) );
  168. CALL_SUBTEST_3( (sparse_product<DynamicSparseMatrix<float, RowMajor> >()) );
  169. CALL_SUBTEST_4( (check_marketio<SparseMatrix<float,ColMajor,int> >()) );
  170. CALL_SUBTEST_4( (check_marketio<SparseMatrix<double,ColMajor,int> >()) );
  171. CALL_SUBTEST_4( (check_marketio<SparseMatrix<std::complex<float>,ColMajor,int> >()) );
  172. CALL_SUBTEST_4( (check_marketio<SparseMatrix<std::complex<double>,ColMajor,int> >()) );
  173. CALL_SUBTEST_4( (check_marketio<SparseMatrix<float,ColMajor,long int> >()) );
  174. CALL_SUBTEST_4( (check_marketio<SparseMatrix<double,ColMajor,long int> >()) );
  175. CALL_SUBTEST_4( (check_marketio<SparseMatrix<std::complex<float>,ColMajor,long int> >()) );
  176. CALL_SUBTEST_4( (check_marketio<SparseMatrix<std::complex<double>,ColMajor,long int> >()) );
  177. CALL_SUBTEST_5( (check_marketio_vector<Matrix<float,1,Dynamic> >()) );
  178. CALL_SUBTEST_5( (check_marketio_vector<Matrix<double,1,Dynamic> >()) );
  179. CALL_SUBTEST_5( (check_marketio_vector<Matrix<std::complex<float>,1,Dynamic> >()) );
  180. CALL_SUBTEST_5( (check_marketio_vector<Matrix<std::complex<double>,1,Dynamic> >()) );
  181. CALL_SUBTEST_5( (check_marketio_vector<Matrix<float,Dynamic,1> >()) );
  182. CALL_SUBTEST_5( (check_marketio_vector<Matrix<double,Dynamic,1> >()) );
  183. CALL_SUBTEST_5( (check_marketio_vector<Matrix<std::complex<float>,Dynamic,1> >()) );
  184. CALL_SUBTEST_5( (check_marketio_vector<Matrix<std::complex<double>,Dynamic,1> >()) );
  185. TEST_SET_BUT_UNUSED_VARIABLE(s);
  186. }
  187. }