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.

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