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.

84 lines
2.7 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra. Eigen itself is part of the KDE project.
  3. //
  4. // Copyright (C) 2008 Daniel Gomez Ferro <dgomezferro@gmail.com>
  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,cols);
  20. SparseVectorType v1(rows), v2(rows), v3(rows);
  21. DenseMatrix refM1 = DenseMatrix::Zero(rows, cols);
  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 = ei_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.eigen2_dot(v2), refV1.eigen2_dot(refV2));
  59. VERIFY_IS_APPROX(v1.eigen2_dot(refV2), refV1.eigen2_dot(refV2));
  60. }
  61. void test_eigen2_sparse_vector()
  62. {
  63. for(int i = 0; i < g_repeat; i++) {
  64. CALL_SUBTEST_1( sparse_vector<double>(8, 8) );
  65. CALL_SUBTEST_2( sparse_vector<std::complex<double> >(16, 16) );
  66. CALL_SUBTEST_1( sparse_vector<double>(299, 535) );
  67. }
  68. }