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.

280 lines
11 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 20013 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. #define TEST_ENABLE_TEMPORARY_TRACKING
  14. #include "main.h"
  15. // test Ref.h
  16. // Deal with i387 extended precision
  17. #if EIGEN_ARCH_i386 && !(EIGEN_ARCH_x86_64)
  18. #if EIGEN_COMP_GNUC_STRICT && EIGEN_GNUC_AT_LEAST(4,4)
  19. #pragma GCC optimize ("-ffloat-store")
  20. #else
  21. #undef VERIFY_IS_EQUAL
  22. #define VERIFY_IS_EQUAL(X,Y) VERIFY_IS_APPROX(X,Y)
  23. #endif
  24. #endif
  25. template<typename MatrixType> void ref_matrix(const MatrixType& m)
  26. {
  27. typedef typename MatrixType::Index Index;
  28. typedef typename MatrixType::Scalar Scalar;
  29. typedef typename MatrixType::RealScalar RealScalar;
  30. typedef Matrix<Scalar,Dynamic,Dynamic,MatrixType::Options> DynMatrixType;
  31. typedef Matrix<RealScalar,Dynamic,Dynamic,MatrixType::Options> RealDynMatrixType;
  32. typedef Ref<MatrixType> RefMat;
  33. typedef Ref<DynMatrixType> RefDynMat;
  34. typedef Ref<const DynMatrixType> ConstRefDynMat;
  35. typedef Ref<RealDynMatrixType , 0, Stride<Dynamic,Dynamic> > RefRealMatWithStride;
  36. Index rows = m.rows(), cols = m.cols();
  37. MatrixType m1 = MatrixType::Random(rows, cols),
  38. m2 = m1;
  39. Index i = internal::random<Index>(0,rows-1);
  40. Index j = internal::random<Index>(0,cols-1);
  41. Index brows = internal::random<Index>(1,rows-i);
  42. Index bcols = internal::random<Index>(1,cols-j);
  43. RefMat rm0 = m1;
  44. VERIFY_IS_EQUAL(rm0, m1);
  45. RefDynMat rm1 = m1;
  46. VERIFY_IS_EQUAL(rm1, m1);
  47. RefDynMat rm2 = m1.block(i,j,brows,bcols);
  48. VERIFY_IS_EQUAL(rm2, m1.block(i,j,brows,bcols));
  49. rm2.setOnes();
  50. m2.block(i,j,brows,bcols).setOnes();
  51. VERIFY_IS_EQUAL(m1, m2);
  52. m2.block(i,j,brows,bcols).setRandom();
  53. rm2 = m2.block(i,j,brows,bcols);
  54. VERIFY_IS_EQUAL(m1, m2);
  55. ConstRefDynMat rm3 = m1.block(i,j,brows,bcols);
  56. m1.block(i,j,brows,bcols) *= 2;
  57. m2.block(i,j,brows,bcols) *= 2;
  58. VERIFY_IS_EQUAL(rm3, m2.block(i,j,brows,bcols));
  59. RefRealMatWithStride rm4 = m1.real();
  60. VERIFY_IS_EQUAL(rm4, m2.real());
  61. rm4.array() += 1;
  62. m2.real().array() += 1;
  63. VERIFY_IS_EQUAL(m1, m2);
  64. }
  65. template<typename VectorType> void ref_vector(const VectorType& m)
  66. {
  67. typedef typename VectorType::Index Index;
  68. typedef typename VectorType::Scalar Scalar;
  69. typedef typename VectorType::RealScalar RealScalar;
  70. typedef Matrix<Scalar,Dynamic,1,VectorType::Options> DynMatrixType;
  71. typedef Matrix<Scalar,Dynamic,Dynamic,ColMajor> MatrixType;
  72. typedef Matrix<RealScalar,Dynamic,1,VectorType::Options> RealDynMatrixType;
  73. typedef Ref<VectorType> RefMat;
  74. typedef Ref<DynMatrixType> RefDynMat;
  75. typedef Ref<const DynMatrixType> ConstRefDynMat;
  76. typedef Ref<RealDynMatrixType , 0, InnerStride<> > RefRealMatWithStride;
  77. typedef Ref<DynMatrixType , 0, InnerStride<> > RefMatWithStride;
  78. Index size = m.size();
  79. VectorType v1 = VectorType::Random(size),
  80. v2 = v1;
  81. MatrixType mat1 = MatrixType::Random(size,size),
  82. mat2 = mat1,
  83. mat3 = MatrixType::Random(size,size);
  84. Index i = internal::random<Index>(0,size-1);
  85. Index bsize = internal::random<Index>(1,size-i);
  86. RefMat rm0 = v1;
  87. VERIFY_IS_EQUAL(rm0, v1);
  88. RefDynMat rv1 = v1;
  89. VERIFY_IS_EQUAL(rv1, v1);
  90. RefDynMat rv2 = v1.segment(i,bsize);
  91. VERIFY_IS_EQUAL(rv2, v1.segment(i,bsize));
  92. rv2.setOnes();
  93. v2.segment(i,bsize).setOnes();
  94. VERIFY_IS_EQUAL(v1, v2);
  95. v2.segment(i,bsize).setRandom();
  96. rv2 = v2.segment(i,bsize);
  97. VERIFY_IS_EQUAL(v1, v2);
  98. ConstRefDynMat rm3 = v1.segment(i,bsize);
  99. v1.segment(i,bsize) *= 2;
  100. v2.segment(i,bsize) *= 2;
  101. VERIFY_IS_EQUAL(rm3, v2.segment(i,bsize));
  102. RefRealMatWithStride rm4 = v1.real();
  103. VERIFY_IS_EQUAL(rm4, v2.real());
  104. rm4.array() += 1;
  105. v2.real().array() += 1;
  106. VERIFY_IS_EQUAL(v1, v2);
  107. RefMatWithStride rm5 = mat1.row(i).transpose();
  108. VERIFY_IS_EQUAL(rm5, mat1.row(i).transpose());
  109. rm5.array() += 1;
  110. mat2.row(i).array() += 1;
  111. VERIFY_IS_EQUAL(mat1, mat2);
  112. rm5.noalias() = rm4.transpose() * mat3;
  113. mat2.row(i) = v2.real().transpose() * mat3;
  114. VERIFY_IS_APPROX(mat1, mat2);
  115. }
  116. template<typename PlainObjectType> void check_const_correctness(const PlainObjectType&)
  117. {
  118. // verify that ref-to-const don't have LvalueBit
  119. typedef typename internal::add_const<PlainObjectType>::type ConstPlainObjectType;
  120. VERIFY( !(internal::traits<Ref<ConstPlainObjectType> >::Flags & LvalueBit) );
  121. VERIFY( !(internal::traits<Ref<ConstPlainObjectType, Aligned> >::Flags & LvalueBit) );
  122. VERIFY( !(Ref<ConstPlainObjectType>::Flags & LvalueBit) );
  123. VERIFY( !(Ref<ConstPlainObjectType, Aligned>::Flags & LvalueBit) );
  124. }
  125. template<typename B>
  126. EIGEN_DONT_INLINE void call_ref_1(Ref<VectorXf> a, const B &b) { VERIFY_IS_EQUAL(a,b); }
  127. template<typename B>
  128. EIGEN_DONT_INLINE void call_ref_2(const Ref<const VectorXf>& a, const B &b) { VERIFY_IS_EQUAL(a,b); }
  129. template<typename B>
  130. EIGEN_DONT_INLINE void call_ref_3(Ref<VectorXf,0,InnerStride<> > a, const B &b) { VERIFY_IS_EQUAL(a,b); }
  131. template<typename B>
  132. EIGEN_DONT_INLINE void call_ref_4(const Ref<const VectorXf,0,InnerStride<> >& a, const B &b) { VERIFY_IS_EQUAL(a,b); }
  133. template<typename B>
  134. EIGEN_DONT_INLINE void call_ref_5(Ref<MatrixXf,0,OuterStride<> > a, const B &b) { VERIFY_IS_EQUAL(a,b); }
  135. template<typename B>
  136. EIGEN_DONT_INLINE void call_ref_6(const Ref<const MatrixXf,0,OuterStride<> >& a, const B &b) { VERIFY_IS_EQUAL(a,b); }
  137. template<typename B>
  138. EIGEN_DONT_INLINE void call_ref_7(Ref<Matrix<float,Dynamic,3> > a, const B &b) { VERIFY_IS_EQUAL(a,b); }
  139. void call_ref()
  140. {
  141. VectorXcf ca = VectorXcf::Random(10);
  142. VectorXf a = VectorXf::Random(10);
  143. RowVectorXf b = RowVectorXf::Random(10);
  144. MatrixXf A = MatrixXf::Random(10,10);
  145. RowVector3f c = RowVector3f::Random();
  146. const VectorXf& ac(a);
  147. VectorBlock<VectorXf> ab(a,0,3);
  148. const VectorBlock<VectorXf> abc(a,0,3);
  149. VERIFY_EVALUATION_COUNT( call_ref_1(a,a), 0);
  150. VERIFY_EVALUATION_COUNT( call_ref_1(b,b.transpose()), 0);
  151. // call_ref_1(ac,a<c); // does not compile because ac is const
  152. VERIFY_EVALUATION_COUNT( call_ref_1(ab,ab), 0);
  153. VERIFY_EVALUATION_COUNT( call_ref_1(a.head(4),a.head(4)), 0);
  154. VERIFY_EVALUATION_COUNT( call_ref_1(abc,abc), 0);
  155. VERIFY_EVALUATION_COUNT( call_ref_1(A.col(3),A.col(3)), 0);
  156. // call_ref_1(A.row(3),A.row(3)); // does not compile because innerstride!=1
  157. VERIFY_EVALUATION_COUNT( call_ref_3(A.row(3),A.row(3).transpose()), 0);
  158. VERIFY_EVALUATION_COUNT( call_ref_4(A.row(3),A.row(3).transpose()), 0);
  159. // call_ref_1(a+a, a+a); // does not compile for obvious reason
  160. MatrixXf tmp = A*A.col(1);
  161. VERIFY_EVALUATION_COUNT( call_ref_2(A*A.col(1), tmp), 1); // evaluated into a temp
  162. VERIFY_EVALUATION_COUNT( call_ref_2(ac.head(5),ac.head(5)), 0);
  163. VERIFY_EVALUATION_COUNT( call_ref_2(ac,ac), 0);
  164. VERIFY_EVALUATION_COUNT( call_ref_2(a,a), 0);
  165. VERIFY_EVALUATION_COUNT( call_ref_2(ab,ab), 0);
  166. VERIFY_EVALUATION_COUNT( call_ref_2(a.head(4),a.head(4)), 0);
  167. tmp = a+a;
  168. VERIFY_EVALUATION_COUNT( call_ref_2(a+a,tmp), 1); // evaluated into a temp
  169. VERIFY_EVALUATION_COUNT( call_ref_2(ca.imag(),ca.imag()), 1); // evaluated into a temp
  170. VERIFY_EVALUATION_COUNT( call_ref_4(ac.head(5),ac.head(5)), 0);
  171. tmp = a+a;
  172. VERIFY_EVALUATION_COUNT( call_ref_4(a+a,tmp), 1); // evaluated into a temp
  173. VERIFY_EVALUATION_COUNT( call_ref_4(ca.imag(),ca.imag()), 0);
  174. VERIFY_EVALUATION_COUNT( call_ref_5(a,a), 0);
  175. VERIFY_EVALUATION_COUNT( call_ref_5(a.head(3),a.head(3)), 0);
  176. VERIFY_EVALUATION_COUNT( call_ref_5(A,A), 0);
  177. // call_ref_5(A.transpose(),A.transpose()); // does not compile because storage order does not match
  178. VERIFY_EVALUATION_COUNT( call_ref_5(A.block(1,1,2,2),A.block(1,1,2,2)), 0);
  179. VERIFY_EVALUATION_COUNT( call_ref_5(b,b), 0); // storage order do not match, but this is a degenerate case that should work
  180. VERIFY_EVALUATION_COUNT( call_ref_5(a.row(3),a.row(3)), 0);
  181. VERIFY_EVALUATION_COUNT( call_ref_6(a,a), 0);
  182. VERIFY_EVALUATION_COUNT( call_ref_6(a.head(3),a.head(3)), 0);
  183. VERIFY_EVALUATION_COUNT( call_ref_6(A.row(3),A.row(3)), 1); // evaluated into a temp thouth it could be avoided by viewing it as a 1xn matrix
  184. tmp = A+A;
  185. VERIFY_EVALUATION_COUNT( call_ref_6(A+A,tmp), 1); // evaluated into a temp
  186. VERIFY_EVALUATION_COUNT( call_ref_6(A,A), 0);
  187. VERIFY_EVALUATION_COUNT( call_ref_6(A.transpose(),A.transpose()), 1); // evaluated into a temp because the storage orders do not match
  188. VERIFY_EVALUATION_COUNT( call_ref_6(A.block(1,1,2,2),A.block(1,1,2,2)), 0);
  189. VERIFY_EVALUATION_COUNT( call_ref_7(c,c), 0);
  190. }
  191. typedef Matrix<double,Dynamic,Dynamic,RowMajor> RowMatrixXd;
  192. int test_ref_overload_fun1(Ref<MatrixXd> ) { return 1; }
  193. int test_ref_overload_fun1(Ref<RowMatrixXd> ) { return 2; }
  194. int test_ref_overload_fun1(Ref<MatrixXf> ) { return 3; }
  195. int test_ref_overload_fun2(Ref<const MatrixXd> ) { return 4; }
  196. int test_ref_overload_fun2(Ref<const MatrixXf> ) { return 5; }
  197. void test_ref_ambiguous(const Ref<const ArrayXd> &A, Ref<ArrayXd> B)
  198. {
  199. B = A;
  200. B = A - A;
  201. }
  202. // See also bug 969
  203. void test_ref_overloads()
  204. {
  205. MatrixXd Ad, Bd;
  206. RowMatrixXd rAd, rBd;
  207. VERIFY( test_ref_overload_fun1(Ad)==1 );
  208. VERIFY( test_ref_overload_fun1(rAd)==2 );
  209. MatrixXf Af, Bf;
  210. VERIFY( test_ref_overload_fun2(Ad)==4 );
  211. VERIFY( test_ref_overload_fun2(Ad+Bd)==4 );
  212. VERIFY( test_ref_overload_fun2(Af+Bf)==5 );
  213. ArrayXd A, B;
  214. test_ref_ambiguous(A, B);
  215. }
  216. void test_ref()
  217. {
  218. for(int i = 0; i < g_repeat; i++) {
  219. CALL_SUBTEST_1( ref_vector(Matrix<float, 1, 1>()) );
  220. CALL_SUBTEST_1( check_const_correctness(Matrix<float, 1, 1>()) );
  221. CALL_SUBTEST_2( ref_vector(Vector4d()) );
  222. CALL_SUBTEST_2( check_const_correctness(Matrix4d()) );
  223. CALL_SUBTEST_3( ref_vector(Vector4cf()) );
  224. CALL_SUBTEST_4( ref_vector(VectorXcf(8)) );
  225. CALL_SUBTEST_5( ref_vector(VectorXi(12)) );
  226. CALL_SUBTEST_5( check_const_correctness(VectorXi(12)) );
  227. CALL_SUBTEST_1( ref_matrix(Matrix<float, 1, 1>()) );
  228. CALL_SUBTEST_2( ref_matrix(Matrix4d()) );
  229. CALL_SUBTEST_1( ref_matrix(Matrix<float,3,5>()) );
  230. CALL_SUBTEST_4( ref_matrix(MatrixXcf(internal::random<int>(1,10),internal::random<int>(1,10))) );
  231. CALL_SUBTEST_4( ref_matrix(Matrix<std::complex<double>,10,15>()) );
  232. CALL_SUBTEST_5( ref_matrix(MatrixXi(internal::random<int>(1,10),internal::random<int>(1,10))) );
  233. CALL_SUBTEST_6( call_ref() );
  234. }
  235. CALL_SUBTEST_7( test_ref_overloads() );
  236. }