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.

96 lines
3.0 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> 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> SparseVectorType;
  17. typedef SparseMatrix<Scalar> 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. // test aliasing
  65. VERIFY_IS_APPROX((v1 = -v1), (refV1 = -refV1));
  66. VERIFY_IS_APPROX((v1 = v1.transpose()), (refV1 = refV1.transpose().eval()));
  67. VERIFY_IS_APPROX((v1 += -v1), (refV1 += -refV1));
  68. }
  69. void test_sparse_vector()
  70. {
  71. for(int i = 0; i < g_repeat; i++) {
  72. CALL_SUBTEST_1( sparse_vector<double>(8, 8) );
  73. CALL_SUBTEST_2( sparse_vector<std::complex<double> >(16, 16) );
  74. CALL_SUBTEST_1( sparse_vector<double>(299, 535) );
  75. }
  76. }