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.

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