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.

125 lines
3.9 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 Scalar,typename Index> void sparse_vector(int rows, int cols)
  11. {
  12. double densityMat = (std::max)(8./(rows*cols), 0.01);
  13. double densityVec = (std::max)(8./float(rows), 0.1);
  14. typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
  15. typedef Matrix<Scalar,Dynamic,1> DenseVector;
  16. typedef SparseVector<Scalar,0,Index> SparseVectorType;
  17. typedef SparseMatrix<Scalar,0,Index> SparseMatrixType;
  18. Scalar eps = 1e-6;
  19. SparseMatrixType m1(rows,rows);
  20. SparseVectorType v1(rows), v2(rows), v3(rows);
  21. DenseMatrix refM1 = DenseMatrix::Zero(rows, rows);
  22. DenseVector refV1 = DenseVector::Random(rows),
  23. refV2 = DenseVector::Random(rows),
  24. refV3 = DenseVector::Random(rows);
  25. std::vector<int> zerocoords, nonzerocoords;
  26. initSparse<Scalar>(densityVec, refV1, v1, &zerocoords, &nonzerocoords);
  27. initSparse<Scalar>(densityMat, refM1, m1);
  28. initSparse<Scalar>(densityVec, refV2, v2);
  29. initSparse<Scalar>(densityVec, refV3, v3);
  30. Scalar s1 = internal::random<Scalar>();
  31. // test coeff and coeffRef
  32. for (unsigned int i=0; i<zerocoords.size(); ++i)
  33. {
  34. VERIFY_IS_MUCH_SMALLER_THAN( v1.coeff(zerocoords[i]), eps );
  35. //VERIFY_RAISES_ASSERT( v1.coeffRef(zerocoords[i]) = 5 );
  36. }
  37. {
  38. VERIFY(int(nonzerocoords.size()) == v1.nonZeros());
  39. int j=0;
  40. for (typename SparseVectorType::InnerIterator it(v1); it; ++it,++j)
  41. {
  42. VERIFY(nonzerocoords[j]==it.index());
  43. VERIFY(it.value()==v1.coeff(it.index()));
  44. VERIFY(it.value()==refV1.coeff(it.index()));
  45. }
  46. }
  47. VERIFY_IS_APPROX(v1, refV1);
  48. // test coeffRef with reallocation
  49. {
  50. SparseVectorType v4(rows);
  51. DenseVector v5 = DenseVector::Zero(rows);
  52. for(int k=0; k<rows; ++k)
  53. {
  54. int i = internal::random<int>(0,rows-1);
  55. Scalar v = internal::random<Scalar>();
  56. v4.coeffRef(i) += v;
  57. v5.coeffRef(i) += v;
  58. }
  59. VERIFY_IS_APPROX(v4,v5);
  60. }
  61. v1.coeffRef(nonzerocoords[0]) = Scalar(5);
  62. refV1.coeffRef(nonzerocoords[0]) = Scalar(5);
  63. VERIFY_IS_APPROX(v1, refV1);
  64. VERIFY_IS_APPROX(v1+v2, refV1+refV2);
  65. VERIFY_IS_APPROX(v1+v2+v3, refV1+refV2+refV3);
  66. VERIFY_IS_APPROX(v1*s1-v2, refV1*s1-refV2);
  67. VERIFY_IS_APPROX(v1*=s1, refV1*=s1);
  68. VERIFY_IS_APPROX(v1/=s1, refV1/=s1);
  69. VERIFY_IS_APPROX(v1+=v2, refV1+=refV2);
  70. VERIFY_IS_APPROX(v1-=v2, refV1-=refV2);
  71. VERIFY_IS_APPROX(v1.dot(v2), refV1.dot(refV2));
  72. VERIFY_IS_APPROX(v1.dot(refV2), refV1.dot(refV2));
  73. VERIFY_IS_APPROX(m1*v2, refM1*refV2);
  74. VERIFY_IS_APPROX(v1.dot(m1*v2), refV1.dot(refM1*refV2));
  75. int i = internal::random<int>(0,rows-1);
  76. VERIFY_IS_APPROX(v1.dot(m1.col(i)), refV1.dot(refM1.col(i)));
  77. VERIFY_IS_APPROX(v1.squaredNorm(), refV1.squaredNorm());
  78. VERIFY_IS_APPROX(v1.blueNorm(), refV1.blueNorm());
  79. // test aliasing
  80. VERIFY_IS_APPROX((v1 = -v1), (refV1 = -refV1));
  81. VERIFY_IS_APPROX((v1 = v1.transpose()), (refV1 = refV1.transpose().eval()));
  82. VERIFY_IS_APPROX((v1 += -v1), (refV1 += -refV1));
  83. // sparse matrix to sparse vector
  84. SparseMatrixType mv1;
  85. VERIFY_IS_APPROX((mv1=v1),v1);
  86. VERIFY_IS_APPROX(mv1,(v1=mv1));
  87. VERIFY_IS_APPROX(mv1,(v1=mv1.transpose()));
  88. // check copy to dense vector with transpose
  89. refV3.resize(0);
  90. VERIFY_IS_APPROX(refV3 = v1.transpose(),v1.toDense());
  91. VERIFY_IS_APPROX(DenseVector(v1),v1.toDense());
  92. }
  93. void test_sparse_vector()
  94. {
  95. for(int i = 0; i < g_repeat; i++) {
  96. CALL_SUBTEST_1(( sparse_vector<double,int>(8, 8) ));
  97. CALL_SUBTEST_2(( sparse_vector<std::complex<double>, int>(16, 16) ));
  98. CALL_SUBTEST_1(( sparse_vector<double,long int>(299, 535) ));
  99. CALL_SUBTEST_1(( sparse_vector<double,short>(299, 535) ));
  100. }
  101. }