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.

139 lines
6.0 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 20015 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. // This unit test cannot be easily written to work with EIGEN_DEFAULT_TO_ROW_MAJOR
  10. #ifdef EIGEN_DEFAULT_TO_ROW_MAJOR
  11. #undef EIGEN_DEFAULT_TO_ROW_MAJOR
  12. #endif
  13. static long int nb_temporaries;
  14. inline void on_temporary_creation() {
  15. // here's a great place to set a breakpoint when debugging failures in this test!
  16. nb_temporaries++;
  17. }
  18. #define EIGEN_SPARSE_CREATE_TEMPORARY_PLUGIN { on_temporary_creation(); }
  19. #include "main.h"
  20. #include <Eigen/SparseCore>
  21. #define VERIFY_EVALUATION_COUNT(XPR,N) {\
  22. nb_temporaries = 0; \
  23. CALL_SUBTEST( XPR ); \
  24. if(nb_temporaries!=N) std::cerr << "nb_temporaries == " << nb_temporaries << "\n"; \
  25. VERIFY( (#XPR) && nb_temporaries==N ); \
  26. }
  27. template<typename PlainObjectType> void check_const_correctness(const PlainObjectType&)
  28. {
  29. // verify that ref-to-const don't have LvalueBit
  30. typedef typename internal::add_const<PlainObjectType>::type ConstPlainObjectType;
  31. VERIFY( !(internal::traits<Ref<ConstPlainObjectType> >::Flags & LvalueBit) );
  32. VERIFY( !(internal::traits<Ref<ConstPlainObjectType, Aligned> >::Flags & LvalueBit) );
  33. VERIFY( !(Ref<ConstPlainObjectType>::Flags & LvalueBit) );
  34. VERIFY( !(Ref<ConstPlainObjectType, Aligned>::Flags & LvalueBit) );
  35. }
  36. template<typename B>
  37. EIGEN_DONT_INLINE void call_ref_1(Ref<SparseMatrix<float> > a, const B &b) { VERIFY_IS_EQUAL(a.toDense(),b.toDense()); }
  38. template<typename B>
  39. EIGEN_DONT_INLINE void call_ref_2(const Ref<const SparseMatrix<float> >& a, const B &b) { VERIFY_IS_EQUAL(a.toDense(),b.toDense()); }
  40. template<typename B>
  41. EIGEN_DONT_INLINE void call_ref_3(const Ref<const SparseMatrix<float>, StandardCompressedFormat>& a, const B &b) {
  42. VERIFY(a.isCompressed());
  43. VERIFY_IS_EQUAL(a.toDense(),b.toDense());
  44. }
  45. template<typename B>
  46. EIGEN_DONT_INLINE void call_ref_4(Ref<SparseVector<float> > a, const B &b) { VERIFY_IS_EQUAL(a.toDense(),b.toDense()); }
  47. template<typename B>
  48. EIGEN_DONT_INLINE void call_ref_5(const Ref<const SparseVector<float> >& a, const B &b) { VERIFY_IS_EQUAL(a.toDense(),b.toDense()); }
  49. void call_ref()
  50. {
  51. SparseMatrix<float> A = MatrixXf::Random(10,10).sparseView(0.5,1);
  52. SparseMatrix<float,RowMajor> B = MatrixXf::Random(10,10).sparseView(0.5,1);
  53. SparseMatrix<float> C = MatrixXf::Random(10,10).sparseView(0.5,1);
  54. C.reserve(VectorXi::Constant(C.outerSize(), 2));
  55. const SparseMatrix<float>& Ac(A);
  56. Block<SparseMatrix<float> > Ab(A,0,1, 3,3);
  57. const Block<SparseMatrix<float> > Abc(A,0,1,3,3);
  58. SparseVector<float> vc = VectorXf::Random(10).sparseView(0.5,1);
  59. SparseVector<float,RowMajor> vr = VectorXf::Random(10).sparseView(0.5,1);
  60. SparseMatrix<float> AA = A*A;
  61. VERIFY_EVALUATION_COUNT( call_ref_1(A, A), 0);
  62. // VERIFY_EVALUATION_COUNT( call_ref_1(Ac, Ac), 0); // does not compile on purpose
  63. VERIFY_EVALUATION_COUNT( call_ref_2(A, A), 0);
  64. VERIFY_EVALUATION_COUNT( call_ref_3(A, A), 0);
  65. VERIFY_EVALUATION_COUNT( call_ref_2(A.transpose(), A.transpose()), 1);
  66. VERIFY_EVALUATION_COUNT( call_ref_3(A.transpose(), A.transpose()), 1);
  67. VERIFY_EVALUATION_COUNT( call_ref_2(Ac,Ac), 0);
  68. VERIFY_EVALUATION_COUNT( call_ref_3(Ac,Ac), 0);
  69. VERIFY_EVALUATION_COUNT( call_ref_2(A+A,2*Ac), 1);
  70. VERIFY_EVALUATION_COUNT( call_ref_3(A+A,2*Ac), 1);
  71. VERIFY_EVALUATION_COUNT( call_ref_2(B, B), 1);
  72. VERIFY_EVALUATION_COUNT( call_ref_3(B, B), 1);
  73. VERIFY_EVALUATION_COUNT( call_ref_2(B.transpose(), B.transpose()), 0);
  74. VERIFY_EVALUATION_COUNT( call_ref_3(B.transpose(), B.transpose()), 0);
  75. VERIFY_EVALUATION_COUNT( call_ref_2(A*A, AA), 1);
  76. VERIFY_EVALUATION_COUNT( call_ref_3(A*A, AA), 1);
  77. VERIFY(!C.isCompressed());
  78. VERIFY_EVALUATION_COUNT( call_ref_3(C, C), 1);
  79. Ref<SparseMatrix<float> > Ar(A);
  80. VERIFY_IS_APPROX(Ar+Ar, A+A);
  81. VERIFY_EVALUATION_COUNT( call_ref_1(Ar, A), 0);
  82. VERIFY_EVALUATION_COUNT( call_ref_2(Ar, A), 0);
  83. Ref<SparseMatrix<float,RowMajor> > Br(B);
  84. VERIFY_EVALUATION_COUNT( call_ref_1(Br.transpose(), Br.transpose()), 0);
  85. VERIFY_EVALUATION_COUNT( call_ref_2(Br, Br), 1);
  86. VERIFY_EVALUATION_COUNT( call_ref_2(Br.transpose(), Br.transpose()), 0);
  87. Ref<const SparseMatrix<float> > Arc(A);
  88. // VERIFY_EVALUATION_COUNT( call_ref_1(Arc, Arc), 0); // does not compile on purpose
  89. VERIFY_EVALUATION_COUNT( call_ref_2(Arc, Arc), 0);
  90. VERIFY_EVALUATION_COUNT( call_ref_2(A.middleCols(1,3), A.middleCols(1,3)), 0);
  91. VERIFY_EVALUATION_COUNT( call_ref_2(A.col(2), A.col(2)), 0);
  92. VERIFY_EVALUATION_COUNT( call_ref_2(vc, vc), 0);
  93. VERIFY_EVALUATION_COUNT( call_ref_2(vr.transpose(), vr.transpose()), 0);
  94. VERIFY_EVALUATION_COUNT( call_ref_2(vr, vr.transpose()), 0);
  95. VERIFY_EVALUATION_COUNT( call_ref_2(A.block(1,1,3,3), A.block(1,1,3,3)), 1); // should be 0 (allocate starts/nnz only)
  96. VERIFY_EVALUATION_COUNT( call_ref_4(vc, vc), 0);
  97. VERIFY_EVALUATION_COUNT( call_ref_4(vr, vr.transpose()), 0);
  98. VERIFY_EVALUATION_COUNT( call_ref_5(vc, vc), 0);
  99. VERIFY_EVALUATION_COUNT( call_ref_5(vr, vr.transpose()), 0);
  100. VERIFY_EVALUATION_COUNT( call_ref_4(A.col(2), A.col(2)), 0);
  101. VERIFY_EVALUATION_COUNT( call_ref_5(A.col(2), A.col(2)), 0);
  102. // VERIFY_EVALUATION_COUNT( call_ref_4(A.row(2), A.row(2).transpose()), 1); // does not compile on purpose
  103. VERIFY_EVALUATION_COUNT( call_ref_5(A.row(2), A.row(2).transpose()), 1);
  104. }
  105. void test_sparse_ref()
  106. {
  107. for(int i = 0; i < g_repeat; i++) {
  108. CALL_SUBTEST_1( check_const_correctness(SparseMatrix<float>()) );
  109. CALL_SUBTEST_1( check_const_correctness(SparseMatrix<double,RowMajor>()) );
  110. CALL_SUBTEST_2( call_ref() );
  111. CALL_SUBTEST_3( check_const_correctness(SparseVector<float>()) );
  112. CALL_SUBTEST_3( check_const_correctness(SparseVector<double,RowMajor>()) );
  113. }
  114. }