nullary.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2010-2011 Jitse Niesen <jitse@maths.leeds.ac.uk>
  5. // Copyright (C) 2016 Gael Guennebaud <gael.guennebaud@inria.fr>
  6. //
  7. // This Source Code Form is subject to the terms of the Mozilla
  8. // Public License v. 2.0. If a copy of the MPL was not distributed
  9. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  10. #include "main.h"
  11. template<typename MatrixType>
  12. bool equalsIdentity(const MatrixType& A)
  13. {
  14. typedef typename MatrixType::Scalar Scalar;
  15. Scalar zero = static_cast<Scalar>(0);
  16. bool offDiagOK = true;
  17. for (Index i = 0; i < A.rows(); ++i) {
  18. for (Index j = i+1; j < A.cols(); ++j) {
  19. offDiagOK = offDiagOK && (A(i,j) == zero);
  20. }
  21. }
  22. for (Index i = 0; i < A.rows(); ++i) {
  23. for (Index j = 0; j < (std::min)(i, A.cols()); ++j) {
  24. offDiagOK = offDiagOK && (A(i,j) == zero);
  25. }
  26. }
  27. bool diagOK = (A.diagonal().array() == 1).all();
  28. return offDiagOK && diagOK;
  29. }
  30. template<typename VectorType>
  31. void check_extremity_accuracy(const VectorType &v, const typename VectorType::Scalar &low, const typename VectorType::Scalar &high)
  32. {
  33. typedef typename VectorType::Scalar Scalar;
  34. typedef typename VectorType::RealScalar RealScalar;
  35. RealScalar prec = internal::is_same<RealScalar,float>::value ? NumTraits<RealScalar>::dummy_precision()*10 : NumTraits<RealScalar>::dummy_precision()/10;
  36. Index size = v.size();
  37. if(size<20)
  38. return;
  39. for (int i=0; i<size; ++i)
  40. {
  41. if(i<5 || i>size-6)
  42. {
  43. Scalar ref = (low*RealScalar(size-i-1))/RealScalar(size-1) + (high*RealScalar(i))/RealScalar(size-1);
  44. if(std::abs(ref)>1)
  45. {
  46. if(!internal::isApprox(v(i), ref, prec))
  47. std::cout << v(i) << " != " << ref << " ; relative error: " << std::abs((v(i)-ref)/ref) << " ; required precision: " << prec << " ; range: " << low << "," << high << " ; i: " << i << "\n";
  48. VERIFY(internal::isApprox(v(i), (low*RealScalar(size-i-1))/RealScalar(size-1) + (high*RealScalar(i))/RealScalar(size-1), prec));
  49. }
  50. }
  51. }
  52. }
  53. template<typename VectorType>
  54. void testVectorType(const VectorType& base)
  55. {
  56. typedef typename VectorType::Scalar Scalar;
  57. typedef typename VectorType::RealScalar RealScalar;
  58. const Index size = base.size();
  59. Scalar high = internal::random<Scalar>(-500,500);
  60. Scalar low = (size == 1 ? high : internal::random<Scalar>(-500,500));
  61. if (low>high) std::swap(low,high);
  62. // check low==high
  63. if(internal::random<float>(0.f,1.f)<0.05f)
  64. low = high;
  65. // check abs(low) >> abs(high)
  66. else if(size>2 && std::numeric_limits<RealScalar>::max_exponent10>0 && internal::random<float>(0.f,1.f)<0.1f)
  67. low = -internal::random<Scalar>(1,2) * RealScalar(std::pow(RealScalar(10),std::numeric_limits<RealScalar>::max_exponent10/2));
  68. const Scalar step = ((size == 1) ? 1 : (high-low)/(size-1));
  69. // check whether the result yields what we expect it to do
  70. VectorType m(base);
  71. m.setLinSpaced(size,low,high);
  72. if(!NumTraits<Scalar>::IsInteger)
  73. {
  74. VectorType n(size);
  75. for (int i=0; i<size; ++i)
  76. n(i) = low+i*step;
  77. VERIFY_IS_APPROX(m,n);
  78. CALL_SUBTEST( check_extremity_accuracy(m, low, high) );
  79. }
  80. if((!NumTraits<Scalar>::IsInteger) || ((high-low)>=size && (Index(high-low)%(size-1))==0) || (Index(high-low+1)<size && (size%Index(high-low+1))==0))
  81. {
  82. VectorType n(size);
  83. if((!NumTraits<Scalar>::IsInteger) || (high-low>=size))
  84. for (int i=0; i<size; ++i)
  85. n(i) = size==1 ? low : (low + ((high-low)*Scalar(i))/(size-1));
  86. else
  87. for (int i=0; i<size; ++i)
  88. n(i) = size==1 ? low : low + Scalar((double(high-low+1)*double(i))/double(size));
  89. VERIFY_IS_APPROX(m,n);
  90. // random access version
  91. m = VectorType::LinSpaced(size,low,high);
  92. VERIFY_IS_APPROX(m,n);
  93. VERIFY( internal::isApprox(m(m.size()-1),high) );
  94. VERIFY( size==1 || internal::isApprox(m(0),low) );
  95. VERIFY_IS_EQUAL(m(m.size()-1) , high);
  96. if(!NumTraits<Scalar>::IsInteger)
  97. CALL_SUBTEST( check_extremity_accuracy(m, low, high) );
  98. }
  99. VERIFY( m(m.size()-1) <= high );
  100. VERIFY( (m.array() <= high).all() );
  101. VERIFY( (m.array() >= low).all() );
  102. VERIFY( m(m.size()-1) >= low );
  103. if(size>=1)
  104. {
  105. VERIFY( internal::isApprox(m(0),low) );
  106. VERIFY_IS_EQUAL(m(0) , low);
  107. }
  108. // check whether everything works with row and col major vectors
  109. Matrix<Scalar,Dynamic,1> row_vector(size);
  110. Matrix<Scalar,1,Dynamic> col_vector(size);
  111. row_vector.setLinSpaced(size,low,high);
  112. col_vector.setLinSpaced(size,low,high);
  113. // when using the extended precision (e.g., FPU) the relative error might exceed 1 bit
  114. // when computing the squared sum in isApprox, thus the 2x factor.
  115. VERIFY( row_vector.isApprox(col_vector.transpose(), Scalar(2)*NumTraits<Scalar>::epsilon()));
  116. Matrix<Scalar,Dynamic,1> size_changer(size+50);
  117. size_changer.setLinSpaced(size,low,high);
  118. VERIFY( size_changer.size() == size );
  119. typedef Matrix<Scalar,1,1> ScalarMatrix;
  120. ScalarMatrix scalar;
  121. scalar.setLinSpaced(1,low,high);
  122. VERIFY_IS_APPROX( scalar, ScalarMatrix::Constant(high) );
  123. VERIFY_IS_APPROX( ScalarMatrix::LinSpaced(1,low,high), ScalarMatrix::Constant(high) );
  124. // regression test for bug 526 (linear vectorized transversal)
  125. if (size > 1 && (!NumTraits<Scalar>::IsInteger)) {
  126. m.tail(size-1).setLinSpaced(low, high);
  127. VERIFY_IS_APPROX(m(size-1), high);
  128. }
  129. // regression test for bug 1383 (LinSpaced with empty size/range)
  130. {
  131. Index n0 = VectorType::SizeAtCompileTime==Dynamic ? 0 : VectorType::SizeAtCompileTime;
  132. low = internal::random<Scalar>();
  133. m = VectorType::LinSpaced(n0,low,low-1);
  134. VERIFY(m.size()==n0);
  135. if(VectorType::SizeAtCompileTime==Dynamic)
  136. {
  137. VERIFY_IS_EQUAL(VectorType::LinSpaced(n0,0,Scalar(n0-1)).sum(),Scalar(0));
  138. VERIFY_IS_EQUAL(VectorType::LinSpaced(n0,low,low-1).sum(),Scalar(0));
  139. }
  140. m.setLinSpaced(n0,0,Scalar(n0-1));
  141. VERIFY(m.size()==n0);
  142. m.setLinSpaced(n0,low,low-1);
  143. VERIFY(m.size()==n0);
  144. // empty range only:
  145. VERIFY_IS_APPROX(VectorType::LinSpaced(size,low,low),VectorType::Constant(size,low));
  146. m.setLinSpaced(size,low,low);
  147. VERIFY_IS_APPROX(m,VectorType::Constant(size,low));
  148. if(NumTraits<Scalar>::IsInteger)
  149. {
  150. VERIFY_IS_APPROX( VectorType::LinSpaced(size,low,Scalar(low+size-1)), VectorType::LinSpaced(size,Scalar(low+size-1),low).reverse() );
  151. if(VectorType::SizeAtCompileTime==Dynamic)
  152. {
  153. // Check negative multiplicator path:
  154. for(Index k=1; k<5; ++k)
  155. VERIFY_IS_APPROX( VectorType::LinSpaced(size,low,Scalar(low+(size-1)*k)), VectorType::LinSpaced(size,Scalar(low+(size-1)*k),low).reverse() );
  156. // Check negative divisor path:
  157. for(Index k=1; k<5; ++k)
  158. VERIFY_IS_APPROX( VectorType::LinSpaced(size*k,low,Scalar(low+size-1)), VectorType::LinSpaced(size*k,Scalar(low+size-1),low).reverse() );
  159. }
  160. }
  161. }
  162. }
  163. template<typename MatrixType>
  164. void testMatrixType(const MatrixType& m)
  165. {
  166. using std::abs;
  167. const Index rows = m.rows();
  168. const Index cols = m.cols();
  169. typedef typename MatrixType::Scalar Scalar;
  170. typedef typename MatrixType::RealScalar RealScalar;
  171. Scalar s1;
  172. do {
  173. s1 = internal::random<Scalar>();
  174. } while(abs(s1)<RealScalar(1e-5) && (!NumTraits<Scalar>::IsInteger));
  175. MatrixType A;
  176. A.setIdentity(rows, cols);
  177. VERIFY(equalsIdentity(A));
  178. VERIFY(equalsIdentity(MatrixType::Identity(rows, cols)));
  179. A = MatrixType::Constant(rows,cols,s1);
  180. Index i = internal::random<Index>(0,rows-1);
  181. Index j = internal::random<Index>(0,cols-1);
  182. VERIFY_IS_APPROX( MatrixType::Constant(rows,cols,s1)(i,j), s1 );
  183. VERIFY_IS_APPROX( MatrixType::Constant(rows,cols,s1).coeff(i,j), s1 );
  184. VERIFY_IS_APPROX( A(i,j), s1 );
  185. }
  186. void test_nullary()
  187. {
  188. CALL_SUBTEST_1( testMatrixType(Matrix2d()) );
  189. CALL_SUBTEST_2( testMatrixType(MatrixXcf(internal::random<int>(1,300),internal::random<int>(1,300))) );
  190. CALL_SUBTEST_3( testMatrixType(MatrixXf(internal::random<int>(1,300),internal::random<int>(1,300))) );
  191. for(int i = 0; i < g_repeat*10; i++) {
  192. CALL_SUBTEST_4( testVectorType(VectorXd(internal::random<int>(1,30000))) );
  193. CALL_SUBTEST_5( testVectorType(Vector4d()) ); // regression test for bug 232
  194. CALL_SUBTEST_6( testVectorType(Vector3d()) );
  195. CALL_SUBTEST_7( testVectorType(VectorXf(internal::random<int>(1,30000))) );
  196. CALL_SUBTEST_8( testVectorType(Vector3f()) );
  197. CALL_SUBTEST_8( testVectorType(Vector4f()) );
  198. CALL_SUBTEST_8( testVectorType(Matrix<float,8,1>()) );
  199. CALL_SUBTEST_8( testVectorType(Matrix<float,1,1>()) );
  200. CALL_SUBTEST_9( testVectorType(VectorXi(internal::random<int>(1,10))) );
  201. CALL_SUBTEST_9( testVectorType(VectorXi(internal::random<int>(9,300))) );
  202. CALL_SUBTEST_9( testVectorType(Matrix<int,1,1>()) );
  203. }
  204. #ifdef EIGEN_TEST_PART_6
  205. // Assignment of a RowVectorXd to a MatrixXd (regression test for bug #79).
  206. VERIFY( (MatrixXd(RowVectorXd::LinSpaced(3, 0, 1)) - RowVector3d(0, 0.5, 1)).norm() < std::numeric_limits<double>::epsilon() );
  207. #endif
  208. #ifdef EIGEN_TEST_PART_9
  209. // Check possible overflow issue
  210. {
  211. int n = 60000;
  212. ArrayXi a1(n), a2(n);
  213. a1.setLinSpaced(n, 0, n-1);
  214. for(int i=0; i<n; ++i)
  215. a2(i) = i;
  216. VERIFY_IS_APPROX(a1,a2);
  217. }
  218. #endif
  219. #ifdef EIGEN_TEST_PART_10
  220. // check some internal logic
  221. VERIFY(( internal::has_nullary_operator<internal::scalar_constant_op<double> >::value ));
  222. VERIFY(( !internal::has_unary_operator<internal::scalar_constant_op<double> >::value ));
  223. VERIFY(( !internal::has_binary_operator<internal::scalar_constant_op<double> >::value ));
  224. VERIFY(( internal::functor_has_linear_access<internal::scalar_constant_op<double> >::ret ));
  225. VERIFY(( !internal::has_nullary_operator<internal::scalar_identity_op<double> >::value ));
  226. VERIFY(( !internal::has_unary_operator<internal::scalar_identity_op<double> >::value ));
  227. VERIFY(( internal::has_binary_operator<internal::scalar_identity_op<double> >::value ));
  228. VERIFY(( !internal::functor_has_linear_access<internal::scalar_identity_op<double> >::ret ));
  229. VERIFY(( !internal::has_nullary_operator<internal::linspaced_op<float,float> >::value ));
  230. VERIFY(( internal::has_unary_operator<internal::linspaced_op<float,float> >::value ));
  231. VERIFY(( !internal::has_binary_operator<internal::linspaced_op<float,float> >::value ));
  232. VERIFY(( internal::functor_has_linear_access<internal::linspaced_op<float,float> >::ret ));
  233. // Regression unit test for a weird MSVC bug.
  234. // Search "nullary_wrapper_workaround_msvc" in CoreEvaluators.h for the details.
  235. // See also traits<Ref>::match.
  236. {
  237. MatrixXf A = MatrixXf::Random(3,3);
  238. Ref<const MatrixXf> R = 2.0*A;
  239. VERIFY_IS_APPROX(R, A+A);
  240. Ref<const MatrixXf> R1 = MatrixXf::Random(3,3)+A;
  241. VectorXi V = VectorXi::Random(3);
  242. Ref<const VectorXi> R2 = VectorXi::LinSpaced(3,1,3)+V;
  243. VERIFY_IS_APPROX(R2, V+Vector3i(1,2,3));
  244. VERIFY(( internal::has_nullary_operator<internal::scalar_constant_op<float> >::value ));
  245. VERIFY(( !internal::has_unary_operator<internal::scalar_constant_op<float> >::value ));
  246. VERIFY(( !internal::has_binary_operator<internal::scalar_constant_op<float> >::value ));
  247. VERIFY(( internal::functor_has_linear_access<internal::scalar_constant_op<float> >::ret ));
  248. VERIFY(( !internal::has_nullary_operator<internal::linspaced_op<int,int> >::value ));
  249. VERIFY(( internal::has_unary_operator<internal::linspaced_op<int,int> >::value ));
  250. VERIFY(( !internal::has_binary_operator<internal::linspaced_op<int,int> >::value ));
  251. VERIFY(( internal::functor_has_linear_access<internal::linspaced_op<int,int> >::ret ));
  252. }
  253. #endif
  254. }