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.

149 lines
5.3 KiB

  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 deprectaed DynamicSparseMatrix
  10. #define EIGEN_NO_DEPRECATED_WARNING
  11. #include "sparse_basic.cpp"
  12. #include "sparse_product.cpp"
  13. #include <Eigen/SparseExtra>
  14. template<typename SetterType,typename DenseType, typename Scalar, int Options>
  15. bool test_random_setter(SparseMatrix<Scalar,Options>& sm, const DenseType& ref, const std::vector<Vector2i>& nonzeroCoords)
  16. {
  17. typedef SparseMatrix<Scalar,Options> SparseType;
  18. {
  19. sm.setZero();
  20. SetterType w(sm);
  21. std::vector<Vector2i> remaining = nonzeroCoords;
  22. while(!remaining.empty())
  23. {
  24. int i = internal::random<int>(0,static_cast<int>(remaining.size())-1);
  25. w(remaining[i].x(),remaining[i].y()) = ref.coeff(remaining[i].x(),remaining[i].y());
  26. remaining[i] = remaining.back();
  27. remaining.pop_back();
  28. }
  29. }
  30. return sm.isApprox(ref);
  31. }
  32. template<typename SetterType,typename DenseType, typename T>
  33. bool test_random_setter(DynamicSparseMatrix<T>& sm, const DenseType& ref, const std::vector<Vector2i>& nonzeroCoords)
  34. {
  35. sm.setZero();
  36. std::vector<Vector2i> remaining = nonzeroCoords;
  37. while(!remaining.empty())
  38. {
  39. int i = internal::random<int>(0,static_cast<int>(remaining.size())-1);
  40. sm.coeffRef(remaining[i].x(),remaining[i].y()) = ref.coeff(remaining[i].x(),remaining[i].y());
  41. remaining[i] = remaining.back();
  42. remaining.pop_back();
  43. }
  44. return sm.isApprox(ref);
  45. }
  46. template<typename SparseMatrixType> void sparse_extra(const SparseMatrixType& ref)
  47. {
  48. typedef typename SparseMatrixType::Index Index;
  49. const Index rows = ref.rows();
  50. const Index cols = ref.cols();
  51. typedef typename SparseMatrixType::Scalar Scalar;
  52. enum { Flags = SparseMatrixType::Flags };
  53. double density = (std::max)(8./(rows*cols), 0.01);
  54. typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
  55. typedef Matrix<Scalar,Dynamic,1> DenseVector;
  56. Scalar eps = 1e-6;
  57. SparseMatrixType m(rows, cols);
  58. DenseMatrix refMat = DenseMatrix::Zero(rows, cols);
  59. DenseVector vec1 = DenseVector::Random(rows);
  60. std::vector<Vector2i> zeroCoords;
  61. std::vector<Vector2i> nonzeroCoords;
  62. initSparse<Scalar>(density, refMat, m, 0, &zeroCoords, &nonzeroCoords);
  63. if (zeroCoords.size()==0 || nonzeroCoords.size()==0)
  64. return;
  65. // test coeff and coeffRef
  66. for (int i=0; i<(int)zeroCoords.size(); ++i)
  67. {
  68. VERIFY_IS_MUCH_SMALLER_THAN( m.coeff(zeroCoords[i].x(),zeroCoords[i].y()), eps );
  69. if(internal::is_same<SparseMatrixType,SparseMatrix<Scalar,Flags> >::value)
  70. VERIFY_RAISES_ASSERT( m.coeffRef(zeroCoords[0].x(),zeroCoords[0].y()) = 5 );
  71. }
  72. VERIFY_IS_APPROX(m, refMat);
  73. m.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5);
  74. refMat.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5);
  75. VERIFY_IS_APPROX(m, refMat);
  76. // random setter
  77. // {
  78. // m.setZero();
  79. // VERIFY_IS_NOT_APPROX(m, refMat);
  80. // SparseSetter<SparseMatrixType, RandomAccessPattern> w(m);
  81. // std::vector<Vector2i> remaining = nonzeroCoords;
  82. // while(!remaining.empty())
  83. // {
  84. // int i = internal::random<int>(0,remaining.size()-1);
  85. // w->coeffRef(remaining[i].x(),remaining[i].y()) = refMat.coeff(remaining[i].x(),remaining[i].y());
  86. // remaining[i] = remaining.back();
  87. // remaining.pop_back();
  88. // }
  89. // }
  90. // VERIFY_IS_APPROX(m, refMat);
  91. VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, StdMapTraits> >(m,refMat,nonzeroCoords) ));
  92. #ifdef EIGEN_UNORDERED_MAP_SUPPORT
  93. VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, StdUnorderedMapTraits> >(m,refMat,nonzeroCoords) ));
  94. #endif
  95. #ifdef _DENSE_HASH_MAP_H_
  96. VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, GoogleDenseHashMapTraits> >(m,refMat,nonzeroCoords) ));
  97. #endif
  98. #ifdef _SPARSE_HASH_MAP_H_
  99. VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, GoogleSparseHashMapTraits> >(m,refMat,nonzeroCoords) ));
  100. #endif
  101. // test RandomSetter
  102. /*{
  103. SparseMatrixType m1(rows,cols), m2(rows,cols);
  104. DenseMatrix refM1 = DenseMatrix::Zero(rows, rows);
  105. initSparse<Scalar>(density, refM1, m1);
  106. {
  107. Eigen::RandomSetter<SparseMatrixType > setter(m2);
  108. for (int j=0; j<m1.outerSize(); ++j)
  109. for (typename SparseMatrixType::InnerIterator i(m1,j); i; ++i)
  110. setter(i.index(), j) = i.value();
  111. }
  112. VERIFY_IS_APPROX(m1, m2);
  113. }*/
  114. }
  115. void test_sparse_extra()
  116. {
  117. for(int i = 0; i < g_repeat; i++) {
  118. int s = Eigen::internal::random<int>(1,50);
  119. CALL_SUBTEST_1( sparse_extra(SparseMatrix<double>(8, 8)) );
  120. CALL_SUBTEST_2( sparse_extra(SparseMatrix<std::complex<double> >(s, s)) );
  121. CALL_SUBTEST_1( sparse_extra(SparseMatrix<double>(s, s)) );
  122. CALL_SUBTEST_3( sparse_extra(DynamicSparseMatrix<double>(s, s)) );
  123. // CALL_SUBTEST_3(( sparse_basic(DynamicSparseMatrix<double>(s, s)) ));
  124. // CALL_SUBTEST_3(( sparse_basic(DynamicSparseMatrix<double,ColMajor,long int>(s, s)) ));
  125. CALL_SUBTEST_3( (sparse_product<DynamicSparseMatrix<float, ColMajor> >()) );
  126. CALL_SUBTEST_3( (sparse_product<DynamicSparseMatrix<float, RowMajor> >()) );
  127. }
  128. }