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.

110 lines
3.6 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. v1.coeffRef(nonzerocoords[0]) = Scalar(5);
  49. refV1.coeffRef(nonzerocoords[0]) = Scalar(5);
  50. VERIFY_IS_APPROX(v1, refV1);
  51. VERIFY_IS_APPROX(v1+v2, refV1+refV2);
  52. VERIFY_IS_APPROX(v1+v2+v3, refV1+refV2+refV3);
  53. VERIFY_IS_APPROX(v1*s1-v2, refV1*s1-refV2);
  54. VERIFY_IS_APPROX(v1*=s1, refV1*=s1);
  55. VERIFY_IS_APPROX(v1/=s1, refV1/=s1);
  56. VERIFY_IS_APPROX(v1+=v2, refV1+=refV2);
  57. VERIFY_IS_APPROX(v1-=v2, refV1-=refV2);
  58. VERIFY_IS_APPROX(v1.dot(v2), refV1.dot(refV2));
  59. VERIFY_IS_APPROX(v1.dot(refV2), refV1.dot(refV2));
  60. VERIFY_IS_APPROX(v1.dot(m1*v2), refV1.dot(refM1*refV2));
  61. int i = internal::random<int>(0,rows-1);
  62. VERIFY_IS_APPROX(v1.dot(m1.col(i)), refV1.dot(refM1.col(i)));
  63. VERIFY_IS_APPROX(v1.squaredNorm(), refV1.squaredNorm());
  64. VERIFY_IS_APPROX(v1.blueNorm(), refV1.blueNorm());
  65. // test aliasing
  66. VERIFY_IS_APPROX((v1 = -v1), (refV1 = -refV1));
  67. VERIFY_IS_APPROX((v1 = v1.transpose()), (refV1 = refV1.transpose().eval()));
  68. VERIFY_IS_APPROX((v1 += -v1), (refV1 += -refV1));
  69. // sparse matrix to sparse vector
  70. SparseMatrixType mv1;
  71. VERIFY_IS_APPROX((mv1=v1),v1);
  72. VERIFY_IS_APPROX(mv1,(v1=mv1));
  73. VERIFY_IS_APPROX(mv1,(v1=mv1.transpose()));
  74. // check copy to dense vector with transpose
  75. refV3.resize(0);
  76. VERIFY_IS_APPROX(refV3 = v1.transpose(),v1.toDense());
  77. VERIFY_IS_APPROX(DenseVector(v1),v1.toDense());
  78. }
  79. void test_sparse_vector()
  80. {
  81. for(int i = 0; i < g_repeat; i++) {
  82. CALL_SUBTEST_1(( sparse_vector<double,int>(8, 8) ));
  83. CALL_SUBTEST_2(( sparse_vector<std::complex<double>, int>(16, 16) ));
  84. CALL_SUBTEST_1(( sparse_vector<double,long int>(299, 535) ));
  85. CALL_SUBTEST_1(( sparse_vector<double,short>(299, 535) ));
  86. }
  87. }