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.

182 lines
6.6 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
  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. #define EIGEN2_SUPPORT
  11. #define EIGEN_NO_STATIC_ASSERT
  12. #include "main.h"
  13. #include <functional>
  14. #ifdef min
  15. #undef min
  16. #endif
  17. #ifdef max
  18. #undef max
  19. #endif
  20. using namespace std;
  21. template<typename Scalar> struct AddIfNull {
  22. const Scalar operator() (const Scalar a, const Scalar b) const {return a<=1e-3 ? b : a;}
  23. enum { Cost = NumTraits<Scalar>::AddCost };
  24. };
  25. template<typename MatrixType>
  26. typename Eigen::internal::enable_if<!NumTraits<typename MatrixType::Scalar>::IsInteger,typename MatrixType::Scalar>::type
  27. cwiseops_real_only(MatrixType& m1, MatrixType& m2, MatrixType& m3, MatrixType& mones)
  28. {
  29. typedef typename MatrixType::Scalar Scalar;
  30. typedef typename NumTraits<Scalar>::Real RealScalar;
  31. VERIFY_IS_APPROX(m1.cwise() / m2, m1.cwise() * (m2.cwise().inverse()));
  32. m3 = m1.cwise().abs().cwise().sqrt();
  33. VERIFY_IS_APPROX(m3.cwise().square(), m1.cwise().abs());
  34. VERIFY_IS_APPROX(m1.cwise().square().cwise().sqrt(), m1.cwise().abs());
  35. VERIFY_IS_APPROX(m1.cwise().abs().cwise().log().cwise().exp() , m1.cwise().abs());
  36. VERIFY_IS_APPROX(m1.cwise().pow(2), m1.cwise().square());
  37. m3 = (m1.cwise().abs().cwise()<=RealScalar(0.01)).select(mones,m1);
  38. VERIFY_IS_APPROX(m3.cwise().pow(-1), m3.cwise().inverse());
  39. m3 = m1.cwise().abs();
  40. VERIFY_IS_APPROX(m3.cwise().pow(RealScalar(0.5)), m3.cwise().sqrt());
  41. // VERIFY_IS_APPROX(m1.cwise().tan(), m1.cwise().sin().cwise() / m1.cwise().cos());
  42. VERIFY_IS_APPROX(mones, m1.cwise().sin().cwise().square() + m1.cwise().cos().cwise().square());
  43. m3 = m1;
  44. m3.cwise() /= m2;
  45. VERIFY_IS_APPROX(m3, m1.cwise() / m2);
  46. return Scalar(0);
  47. }
  48. template<typename MatrixType>
  49. typename Eigen::internal::enable_if<NumTraits<typename MatrixType::Scalar>::IsInteger,typename MatrixType::Scalar>::type
  50. cwiseops_real_only(MatrixType& , MatrixType& , MatrixType& , MatrixType& )
  51. {
  52. return 0;
  53. }
  54. template<typename MatrixType> void cwiseops(const MatrixType& m)
  55. {
  56. typedef typename MatrixType::Index Index;
  57. typedef typename MatrixType::Scalar Scalar;
  58. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
  59. Index rows = m.rows();
  60. Index cols = m.cols();
  61. MatrixType m1 = MatrixType::Random(rows, cols),
  62. m1bis = m1,
  63. m2 = MatrixType::Random(rows, cols),
  64. m3(rows, cols),
  65. m4(rows, cols),
  66. mzero = MatrixType::Zero(rows, cols),
  67. mones = MatrixType::Ones(rows, cols),
  68. identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
  69. ::Identity(rows, rows);
  70. VectorType vzero = VectorType::Zero(rows),
  71. vones = VectorType::Ones(rows),
  72. v3(rows);
  73. Index r = internal::random<Index>(0, rows-1),
  74. c = internal::random<Index>(0, cols-1);
  75. Scalar s1 = internal::random<Scalar>();
  76. // test Zero, Ones, Constant, and the set* variants
  77. m3 = MatrixType::Constant(rows, cols, s1);
  78. for (int j=0; j<cols; ++j)
  79. for (int i=0; i<rows; ++i)
  80. {
  81. VERIFY_IS_APPROX(mzero(i,j), Scalar(0));
  82. VERIFY_IS_APPROX(mones(i,j), Scalar(1));
  83. VERIFY_IS_APPROX(m3(i,j), s1);
  84. }
  85. VERIFY(mzero.isZero());
  86. VERIFY(mones.isOnes());
  87. VERIFY(m3.isConstant(s1));
  88. VERIFY(identity.isIdentity());
  89. VERIFY_IS_APPROX(m4.setConstant(s1), m3);
  90. VERIFY_IS_APPROX(m4.setConstant(rows,cols,s1), m3);
  91. VERIFY_IS_APPROX(m4.setZero(), mzero);
  92. VERIFY_IS_APPROX(m4.setZero(rows,cols), mzero);
  93. VERIFY_IS_APPROX(m4.setOnes(), mones);
  94. VERIFY_IS_APPROX(m4.setOnes(rows,cols), mones);
  95. m4.fill(s1);
  96. VERIFY_IS_APPROX(m4, m3);
  97. VERIFY_IS_APPROX(v3.setConstant(rows, s1), VectorType::Constant(rows,s1));
  98. VERIFY_IS_APPROX(v3.setZero(rows), vzero);
  99. VERIFY_IS_APPROX(v3.setOnes(rows), vones);
  100. m2 = m2.template binaryExpr<AddIfNull<Scalar> >(mones);
  101. VERIFY_IS_APPROX(m1.cwise().pow(2), m1.cwise().abs2());
  102. VERIFY_IS_APPROX(m1.cwise().pow(2), m1.cwise().square());
  103. VERIFY_IS_APPROX(m1.cwise().pow(3), m1.cwise().cube());
  104. VERIFY_IS_APPROX(m1 + mones, m1.cwise()+Scalar(1));
  105. VERIFY_IS_APPROX(m1 - mones, m1.cwise()-Scalar(1));
  106. m3 = m1; m3.cwise() += 1;
  107. VERIFY_IS_APPROX(m1 + mones, m3);
  108. m3 = m1; m3.cwise() -= 1;
  109. VERIFY_IS_APPROX(m1 - mones, m3);
  110. VERIFY_IS_APPROX(m2, m2.cwise() * mones);
  111. VERIFY_IS_APPROX(m1.cwise() * m2, m2.cwise() * m1);
  112. m3 = m1;
  113. m3.cwise() *= m2;
  114. VERIFY_IS_APPROX(m3, m1.cwise() * m2);
  115. VERIFY_IS_APPROX(mones, m2.cwise()/m2);
  116. // check min
  117. VERIFY_IS_APPROX( m1.cwise().min(m2), m2.cwise().min(m1) );
  118. VERIFY_IS_APPROX( m1.cwise().min(m1+mones), m1 );
  119. VERIFY_IS_APPROX( m1.cwise().min(m1-mones), m1-mones );
  120. // check max
  121. VERIFY_IS_APPROX( m1.cwise().max(m2), m2.cwise().max(m1) );
  122. VERIFY_IS_APPROX( m1.cwise().max(m1-mones), m1 );
  123. VERIFY_IS_APPROX( m1.cwise().max(m1+mones), m1+mones );
  124. VERIFY( (m1.cwise() == m1).all() );
  125. VERIFY( (m1.cwise() != m2).any() );
  126. VERIFY(!(m1.cwise() == (m1+mones)).any() );
  127. if (rows*cols>1)
  128. {
  129. m3 = m1;
  130. m3(r,c) += 1;
  131. VERIFY( (m1.cwise() == m3).any() );
  132. VERIFY( !(m1.cwise() == m3).all() );
  133. }
  134. VERIFY( (m1.cwise().min(m2).cwise() <= m2).all() );
  135. VERIFY( (m1.cwise().max(m2).cwise() >= m2).all() );
  136. VERIFY( (m1.cwise().min(m2).cwise() < (m1+mones)).all() );
  137. VERIFY( (m1.cwise().max(m2).cwise() > (m1-mones)).all() );
  138. VERIFY( (m1.cwise()<m1.unaryExpr(bind2nd(plus<Scalar>(), Scalar(1)))).all() );
  139. VERIFY( !(m1.cwise()<m1bis.unaryExpr(bind2nd(minus<Scalar>(), Scalar(1)))).all() );
  140. VERIFY( !(m1.cwise()>m1bis.unaryExpr(bind2nd(plus<Scalar>(), Scalar(1)))).any() );
  141. cwiseops_real_only(m1, m2, m3, mones);
  142. }
  143. void test_cwiseop()
  144. {
  145. for(int i = 0; i < g_repeat ; i++) {
  146. CALL_SUBTEST_1( cwiseops(Matrix<float, 1, 1>()) );
  147. CALL_SUBTEST_2( cwiseops(Matrix4d()) );
  148. CALL_SUBTEST_3( cwiseops(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  149. CALL_SUBTEST_4( cwiseops(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  150. CALL_SUBTEST_5( cwiseops(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  151. CALL_SUBTEST_6( cwiseops(MatrixXd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  152. }
  153. }