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.

306 lines
12 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008-2009 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 "main.h"
  10. template<typename ArrayType> void array(const ArrayType& m)
  11. {
  12. typedef typename ArrayType::Index Index;
  13. typedef typename ArrayType::Scalar Scalar;
  14. typedef Array<Scalar, ArrayType::RowsAtCompileTime, 1> ColVectorType;
  15. typedef Array<Scalar, 1, ArrayType::ColsAtCompileTime> RowVectorType;
  16. Index rows = m.rows();
  17. Index cols = m.cols();
  18. ArrayType m1 = ArrayType::Random(rows, cols),
  19. m2 = ArrayType::Random(rows, cols),
  20. m3(rows, cols);
  21. ColVectorType cv1 = ColVectorType::Random(rows);
  22. RowVectorType rv1 = RowVectorType::Random(cols);
  23. Scalar s1 = internal::random<Scalar>(),
  24. s2 = internal::random<Scalar>();
  25. // scalar addition
  26. VERIFY_IS_APPROX(m1 + s1, s1 + m1);
  27. VERIFY_IS_APPROX(m1 + s1, ArrayType::Constant(rows,cols,s1) + m1);
  28. VERIFY_IS_APPROX(s1 - m1, (-m1)+s1 );
  29. VERIFY_IS_APPROX(m1 - s1, m1 - ArrayType::Constant(rows,cols,s1));
  30. VERIFY_IS_APPROX(s1 - m1, ArrayType::Constant(rows,cols,s1) - m1);
  31. VERIFY_IS_APPROX((m1*Scalar(2)) - s2, (m1+m1) - ArrayType::Constant(rows,cols,s2) );
  32. m3 = m1;
  33. m3 += s2;
  34. VERIFY_IS_APPROX(m3, m1 + s2);
  35. m3 = m1;
  36. m3 -= s1;
  37. VERIFY_IS_APPROX(m3, m1 - s1);
  38. // scalar operators via Maps
  39. m3 = m1;
  40. ArrayType::Map(m1.data(), m1.rows(), m1.cols()) -= ArrayType::Map(m2.data(), m2.rows(), m2.cols());
  41. VERIFY_IS_APPROX(m1, m3 - m2);
  42. m3 = m1;
  43. ArrayType::Map(m1.data(), m1.rows(), m1.cols()) += ArrayType::Map(m2.data(), m2.rows(), m2.cols());
  44. VERIFY_IS_APPROX(m1, m3 + m2);
  45. m3 = m1;
  46. ArrayType::Map(m1.data(), m1.rows(), m1.cols()) *= ArrayType::Map(m2.data(), m2.rows(), m2.cols());
  47. VERIFY_IS_APPROX(m1, m3 * m2);
  48. m3 = m1;
  49. m2 = ArrayType::Random(rows,cols);
  50. m2 = (m2==0).select(1,m2);
  51. ArrayType::Map(m1.data(), m1.rows(), m1.cols()) /= ArrayType::Map(m2.data(), m2.rows(), m2.cols());
  52. VERIFY_IS_APPROX(m1, m3 / m2);
  53. // reductions
  54. VERIFY_IS_APPROX(m1.abs().colwise().sum().sum(), m1.abs().sum());
  55. VERIFY_IS_APPROX(m1.abs().rowwise().sum().sum(), m1.abs().sum());
  56. using std::abs;
  57. VERIFY_IS_MUCH_SMALLER_THAN(abs(m1.colwise().sum().sum() - m1.sum()), m1.abs().sum());
  58. VERIFY_IS_MUCH_SMALLER_THAN(abs(m1.rowwise().sum().sum() - m1.sum()), m1.abs().sum());
  59. if (!internal::isMuchSmallerThan(abs(m1.sum() - (m1+m2).sum()), m1.abs().sum(), test_precision<Scalar>()))
  60. VERIFY_IS_NOT_APPROX(((m1+m2).rowwise().sum()).sum(), m1.sum());
  61. VERIFY_IS_APPROX(m1.colwise().sum(), m1.colwise().redux(internal::scalar_sum_op<Scalar>()));
  62. // vector-wise ops
  63. m3 = m1;
  64. VERIFY_IS_APPROX(m3.colwise() += cv1, m1.colwise() + cv1);
  65. m3 = m1;
  66. VERIFY_IS_APPROX(m3.colwise() -= cv1, m1.colwise() - cv1);
  67. m3 = m1;
  68. VERIFY_IS_APPROX(m3.rowwise() += rv1, m1.rowwise() + rv1);
  69. m3 = m1;
  70. VERIFY_IS_APPROX(m3.rowwise() -= rv1, m1.rowwise() - rv1);
  71. }
  72. template<typename ArrayType> void comparisons(const ArrayType& m)
  73. {
  74. using std::abs;
  75. typedef typename ArrayType::Index Index;
  76. typedef typename ArrayType::Scalar Scalar;
  77. typedef typename NumTraits<Scalar>::Real RealScalar;
  78. Index rows = m.rows();
  79. Index cols = m.cols();
  80. Index r = internal::random<Index>(0, rows-1),
  81. c = internal::random<Index>(0, cols-1);
  82. ArrayType m1 = ArrayType::Random(rows, cols),
  83. m2 = ArrayType::Random(rows, cols),
  84. m3(rows, cols);
  85. VERIFY(((m1 + Scalar(1)) > m1).all());
  86. VERIFY(((m1 - Scalar(1)) < m1).all());
  87. if (rows*cols>1)
  88. {
  89. m3 = m1;
  90. m3(r,c) += 1;
  91. VERIFY(! (m1 < m3).all() );
  92. VERIFY(! (m1 > m3).all() );
  93. }
  94. // comparisons to scalar
  95. VERIFY( (m1 != (m1(r,c)+1) ).any() );
  96. VERIFY( (m1 > (m1(r,c)-1) ).any() );
  97. VERIFY( (m1 < (m1(r,c)+1) ).any() );
  98. VERIFY( (m1 == m1(r,c) ).any() );
  99. // test Select
  100. VERIFY_IS_APPROX( (m1<m2).select(m1,m2), m1.cwiseMin(m2) );
  101. VERIFY_IS_APPROX( (m1>m2).select(m1,m2), m1.cwiseMax(m2) );
  102. Scalar mid = (m1.cwiseAbs().minCoeff() + m1.cwiseAbs().maxCoeff())/Scalar(2);
  103. for (int j=0; j<cols; ++j)
  104. for (int i=0; i<rows; ++i)
  105. m3(i,j) = abs(m1(i,j))<mid ? 0 : m1(i,j);
  106. VERIFY_IS_APPROX( (m1.abs()<ArrayType::Constant(rows,cols,mid))
  107. .select(ArrayType::Zero(rows,cols),m1), m3);
  108. // shorter versions:
  109. VERIFY_IS_APPROX( (m1.abs()<ArrayType::Constant(rows,cols,mid))
  110. .select(0,m1), m3);
  111. VERIFY_IS_APPROX( (m1.abs()>=ArrayType::Constant(rows,cols,mid))
  112. .select(m1,0), m3);
  113. // even shorter version:
  114. VERIFY_IS_APPROX( (m1.abs()<mid).select(0,m1), m3);
  115. // count
  116. VERIFY(((m1.abs()+1)>RealScalar(0.1)).count() == rows*cols);
  117. // and/or
  118. VERIFY( (m1<RealScalar(0) && m1>RealScalar(0)).count() == 0);
  119. VERIFY( (m1<RealScalar(0) || m1>=RealScalar(0)).count() == rows*cols);
  120. RealScalar a = m1.abs().mean();
  121. VERIFY( (m1<-a || m1>a).count() == (m1.abs()>a).count());
  122. typedef Array<typename ArrayType::Index, Dynamic, 1> ArrayOfIndices;
  123. // TODO allows colwise/rowwise for array
  124. VERIFY_IS_APPROX(((m1.abs()+1)>RealScalar(0.1)).colwise().count(), ArrayOfIndices::Constant(cols,rows).transpose());
  125. VERIFY_IS_APPROX(((m1.abs()+1)>RealScalar(0.1)).rowwise().count(), ArrayOfIndices::Constant(rows, cols));
  126. }
  127. template<typename ArrayType> void array_real(const ArrayType& m)
  128. {
  129. using std::abs;
  130. using std::sqrt;
  131. typedef typename ArrayType::Index Index;
  132. typedef typename ArrayType::Scalar Scalar;
  133. typedef typename NumTraits<Scalar>::Real RealScalar;
  134. Index rows = m.rows();
  135. Index cols = m.cols();
  136. ArrayType m1 = ArrayType::Random(rows, cols),
  137. m2 = ArrayType::Random(rows, cols),
  138. m3(rows, cols);
  139. Scalar s1 = internal::random<Scalar>();
  140. // these tests are mostly to check possible compilation issues.
  141. VERIFY_IS_APPROX(m1.sin(), sin(m1));
  142. VERIFY_IS_APPROX(m1.cos(), cos(m1));
  143. VERIFY_IS_APPROX(m1.asin(), asin(m1));
  144. VERIFY_IS_APPROX(m1.acos(), acos(m1));
  145. VERIFY_IS_APPROX(m1.tan(), tan(m1));
  146. VERIFY_IS_APPROX(cos(m1+RealScalar(3)*m2), cos((m1+RealScalar(3)*m2).eval()));
  147. VERIFY_IS_APPROX(m1.abs().sqrt(), sqrt(abs(m1)));
  148. VERIFY_IS_APPROX(m1.abs(), sqrt(numext::abs2(m1)));
  149. VERIFY_IS_APPROX(numext::abs2(numext::real(m1)) + numext::abs2(numext::imag(m1)), numext::abs2(m1));
  150. VERIFY_IS_APPROX(numext::abs2(real(m1)) + numext::abs2(imag(m1)), numext::abs2(m1));
  151. if(!NumTraits<Scalar>::IsComplex)
  152. VERIFY_IS_APPROX(numext::real(m1), m1);
  153. // shift argument of logarithm so that it is not zero
  154. Scalar smallNumber = NumTraits<Scalar>::dummy_precision();
  155. VERIFY_IS_APPROX((m1.abs() + smallNumber).log() , log(abs(m1) + smallNumber));
  156. VERIFY_IS_APPROX(m1.exp() * m2.exp(), exp(m1+m2));
  157. VERIFY_IS_APPROX(m1.exp(), exp(m1));
  158. VERIFY_IS_APPROX(m1.exp() / m2.exp(),(m1-m2).exp());
  159. VERIFY_IS_APPROX(m1.pow(2), m1.square());
  160. VERIFY_IS_APPROX(pow(m1,2), m1.square());
  161. ArrayType exponents = ArrayType::Constant(rows, cols, RealScalar(2));
  162. VERIFY_IS_APPROX(Eigen::pow(m1,exponents), m1.square());
  163. m3 = m1.abs();
  164. VERIFY_IS_APPROX(m3.pow(RealScalar(0.5)), m3.sqrt());
  165. VERIFY_IS_APPROX(pow(m3,RealScalar(0.5)), m3.sqrt());
  166. // scalar by array division
  167. const RealScalar tiny = sqrt(std::numeric_limits<RealScalar>::epsilon());
  168. s1 += Scalar(tiny);
  169. m1 += ArrayType::Constant(rows,cols,Scalar(tiny));
  170. VERIFY_IS_APPROX(s1/m1, s1 * m1.inverse());
  171. // check inplace transpose
  172. m3 = m1;
  173. m3.transposeInPlace();
  174. VERIFY_IS_APPROX(m3,m1.transpose());
  175. m3.transposeInPlace();
  176. VERIFY_IS_APPROX(m3,m1);
  177. }
  178. template<typename ArrayType> void array_complex(const ArrayType& m)
  179. {
  180. typedef typename ArrayType::Index Index;
  181. Index rows = m.rows();
  182. Index cols = m.cols();
  183. ArrayType m1 = ArrayType::Random(rows, cols),
  184. m2(rows, cols);
  185. for (Index i = 0; i < m.rows(); ++i)
  186. for (Index j = 0; j < m.cols(); ++j)
  187. m2(i,j) = sqrt(m1(i,j));
  188. VERIFY_IS_APPROX(m1.sqrt(), m2);
  189. VERIFY_IS_APPROX(m1.sqrt(), Eigen::sqrt(m1));
  190. }
  191. template<typename ArrayType> void min_max(const ArrayType& m)
  192. {
  193. typedef typename ArrayType::Index Index;
  194. typedef typename ArrayType::Scalar Scalar;
  195. Index rows = m.rows();
  196. Index cols = m.cols();
  197. ArrayType m1 = ArrayType::Random(rows, cols);
  198. // min/max with array
  199. Scalar maxM1 = m1.maxCoeff();
  200. Scalar minM1 = m1.minCoeff();
  201. VERIFY_IS_APPROX(ArrayType::Constant(rows,cols, minM1), (m1.min)(ArrayType::Constant(rows,cols, minM1)));
  202. VERIFY_IS_APPROX(m1, (m1.min)(ArrayType::Constant(rows,cols, maxM1)));
  203. VERIFY_IS_APPROX(ArrayType::Constant(rows,cols, maxM1), (m1.max)(ArrayType::Constant(rows,cols, maxM1)));
  204. VERIFY_IS_APPROX(m1, (m1.max)(ArrayType::Constant(rows,cols, minM1)));
  205. // min/max with scalar input
  206. VERIFY_IS_APPROX(ArrayType::Constant(rows,cols, minM1), (m1.min)( minM1));
  207. VERIFY_IS_APPROX(m1, (m1.min)( maxM1));
  208. VERIFY_IS_APPROX(ArrayType::Constant(rows,cols, maxM1), (m1.max)( maxM1));
  209. VERIFY_IS_APPROX(m1, (m1.max)( minM1));
  210. }
  211. void test_array()
  212. {
  213. for(int i = 0; i < g_repeat; i++) {
  214. CALL_SUBTEST_1( array(Array<float, 1, 1>()) );
  215. CALL_SUBTEST_2( array(Array22f()) );
  216. CALL_SUBTEST_3( array(Array44d()) );
  217. CALL_SUBTEST_4( array(ArrayXXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  218. CALL_SUBTEST_5( array(ArrayXXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  219. CALL_SUBTEST_6( array(ArrayXXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  220. }
  221. for(int i = 0; i < g_repeat; i++) {
  222. CALL_SUBTEST_1( comparisons(Array<float, 1, 1>()) );
  223. CALL_SUBTEST_2( comparisons(Array22f()) );
  224. CALL_SUBTEST_3( comparisons(Array44d()) );
  225. CALL_SUBTEST_5( comparisons(ArrayXXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  226. CALL_SUBTEST_6( comparisons(ArrayXXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  227. }
  228. for(int i = 0; i < g_repeat; i++) {
  229. CALL_SUBTEST_1( min_max(Array<float, 1, 1>()) );
  230. CALL_SUBTEST_2( min_max(Array22f()) );
  231. CALL_SUBTEST_3( min_max(Array44d()) );
  232. CALL_SUBTEST_5( min_max(ArrayXXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  233. CALL_SUBTEST_6( min_max(ArrayXXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  234. }
  235. for(int i = 0; i < g_repeat; i++) {
  236. CALL_SUBTEST_1( array_real(Array<float, 1, 1>()) );
  237. CALL_SUBTEST_2( array_real(Array22f()) );
  238. CALL_SUBTEST_3( array_real(Array44d()) );
  239. CALL_SUBTEST_5( array_real(ArrayXXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  240. }
  241. for(int i = 0; i < g_repeat; i++) {
  242. CALL_SUBTEST_4( array_complex(ArrayXXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  243. }
  244. VERIFY((internal::is_same< internal::global_math_functions_filtering_base<int>::type, int >::value));
  245. VERIFY((internal::is_same< internal::global_math_functions_filtering_base<float>::type, float >::value));
  246. VERIFY((internal::is_same< internal::global_math_functions_filtering_base<Array2i>::type, ArrayBase<Array2i> >::value));
  247. typedef CwiseUnaryOp<internal::scalar_sum_op<double>, ArrayXd > Xpr;
  248. VERIFY((internal::is_same< internal::global_math_functions_filtering_base<Xpr>::type,
  249. ArrayBase<Xpr>
  250. >::value));
  251. }