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.

165 lines
6.0 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> void cwiseops(const MatrixType& m)
  26. {
  27. typedef typename MatrixType::Index Index;
  28. typedef typename MatrixType::Scalar Scalar;
  29. typedef typename NumTraits<Scalar>::Real RealScalar;
  30. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
  31. Index rows = m.rows();
  32. Index cols = m.cols();
  33. MatrixType m1 = MatrixType::Random(rows, cols),
  34. m2 = MatrixType::Random(rows, cols),
  35. m3(rows, cols),
  36. m4(rows, cols),
  37. mzero = MatrixType::Zero(rows, cols),
  38. mones = MatrixType::Ones(rows, cols),
  39. identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
  40. ::Identity(rows, rows);
  41. VectorType vzero = VectorType::Zero(rows),
  42. vones = VectorType::Ones(rows),
  43. v3(rows);
  44. Index r = internal::random<Index>(0, rows-1),
  45. c = internal::random<Index>(0, cols-1);
  46. Scalar s1 = internal::random<Scalar>();
  47. // test Zero, Ones, Constant, and the set* variants
  48. m3 = MatrixType::Constant(rows, cols, s1);
  49. for (int j=0; j<cols; ++j)
  50. for (int i=0; i<rows; ++i)
  51. {
  52. VERIFY_IS_APPROX(mzero(i,j), Scalar(0));
  53. VERIFY_IS_APPROX(mones(i,j), Scalar(1));
  54. VERIFY_IS_APPROX(m3(i,j), s1);
  55. }
  56. VERIFY(mzero.isZero());
  57. VERIFY(mones.isOnes());
  58. VERIFY(m3.isConstant(s1));
  59. VERIFY(identity.isIdentity());
  60. VERIFY_IS_APPROX(m4.setConstant(s1), m3);
  61. VERIFY_IS_APPROX(m4.setConstant(rows,cols,s1), m3);
  62. VERIFY_IS_APPROX(m4.setZero(), mzero);
  63. VERIFY_IS_APPROX(m4.setZero(rows,cols), mzero);
  64. VERIFY_IS_APPROX(m4.setOnes(), mones);
  65. VERIFY_IS_APPROX(m4.setOnes(rows,cols), mones);
  66. m4.fill(s1);
  67. VERIFY_IS_APPROX(m4, m3);
  68. VERIFY_IS_APPROX(v3.setConstant(rows, s1), VectorType::Constant(rows,s1));
  69. VERIFY_IS_APPROX(v3.setZero(rows), vzero);
  70. VERIFY_IS_APPROX(v3.setOnes(rows), vones);
  71. m2 = m2.template binaryExpr<AddIfNull<Scalar> >(mones);
  72. VERIFY_IS_APPROX(m1.cwise().pow(2), m1.cwise().abs2());
  73. VERIFY_IS_APPROX(m1.cwise().pow(2), m1.cwise().square());
  74. VERIFY_IS_APPROX(m1.cwise().pow(3), m1.cwise().cube());
  75. VERIFY_IS_APPROX(m1 + mones, m1.cwise()+Scalar(1));
  76. VERIFY_IS_APPROX(m1 - mones, m1.cwise()-Scalar(1));
  77. m3 = m1; m3.cwise() += 1;
  78. VERIFY_IS_APPROX(m1 + mones, m3);
  79. m3 = m1; m3.cwise() -= 1;
  80. VERIFY_IS_APPROX(m1 - mones, m3);
  81. VERIFY_IS_APPROX(m2, m2.cwise() * mones);
  82. VERIFY_IS_APPROX(m1.cwise() * m2, m2.cwise() * m1);
  83. m3 = m1;
  84. m3.cwise() *= m2;
  85. VERIFY_IS_APPROX(m3, m1.cwise() * m2);
  86. VERIFY_IS_APPROX(mones, m2.cwise()/m2);
  87. if(!NumTraits<Scalar>::IsInteger)
  88. {
  89. VERIFY_IS_APPROX(m1.cwise() / m2, m1.cwise() * (m2.cwise().inverse()));
  90. m3 = m1.cwise().abs().cwise().sqrt();
  91. VERIFY_IS_APPROX(m3.cwise().square(), m1.cwise().abs());
  92. VERIFY_IS_APPROX(m1.cwise().square().cwise().sqrt(), m1.cwise().abs());
  93. VERIFY_IS_APPROX(m1.cwise().abs().cwise().log().cwise().exp() , m1.cwise().abs());
  94. VERIFY_IS_APPROX(m1.cwise().pow(2), m1.cwise().square());
  95. m3 = (m1.cwise().abs().cwise()<=RealScalar(0.01)).select(mones,m1);
  96. VERIFY_IS_APPROX(m3.cwise().pow(-1), m3.cwise().inverse());
  97. m3 = m1.cwise().abs();
  98. VERIFY_IS_APPROX(m3.cwise().pow(RealScalar(0.5)), m3.cwise().sqrt());
  99. // VERIFY_IS_APPROX(m1.cwise().tan(), m1.cwise().sin().cwise() / m1.cwise().cos());
  100. VERIFY_IS_APPROX(mones, m1.cwise().sin().cwise().square() + m1.cwise().cos().cwise().square());
  101. m3 = m1;
  102. m3.cwise() /= m2;
  103. VERIFY_IS_APPROX(m3, m1.cwise() / m2);
  104. }
  105. // check min
  106. VERIFY_IS_APPROX( m1.cwise().min(m2), m2.cwise().min(m1) );
  107. VERIFY_IS_APPROX( m1.cwise().min(m1+mones), m1 );
  108. VERIFY_IS_APPROX( m1.cwise().min(m1-mones), m1-mones );
  109. // check max
  110. VERIFY_IS_APPROX( m1.cwise().max(m2), m2.cwise().max(m1) );
  111. VERIFY_IS_APPROX( m1.cwise().max(m1-mones), m1 );
  112. VERIFY_IS_APPROX( m1.cwise().max(m1+mones), m1+mones );
  113. VERIFY( (m1.cwise() == m1).all() );
  114. VERIFY( (m1.cwise() != m2).any() );
  115. VERIFY(!(m1.cwise() == (m1+mones)).any() );
  116. if (rows*cols>1)
  117. {
  118. m3 = m1;
  119. m3(r,c) += 1;
  120. VERIFY( (m1.cwise() == m3).any() );
  121. VERIFY( !(m1.cwise() == m3).all() );
  122. }
  123. VERIFY( (m1.cwise().min(m2).cwise() <= m2).all() );
  124. VERIFY( (m1.cwise().max(m2).cwise() >= m2).all() );
  125. VERIFY( (m1.cwise().min(m2).cwise() < (m1+mones)).all() );
  126. VERIFY( (m1.cwise().max(m2).cwise() > (m1-mones)).all() );
  127. VERIFY( (m1.cwise()<m1.unaryExpr(bind2nd(plus<Scalar>(), Scalar(1)))).all() );
  128. VERIFY( !(m1.cwise()<m1.unaryExpr(bind2nd(minus<Scalar>(), Scalar(1)))).all() );
  129. VERIFY( !(m1.cwise()>m1.unaryExpr(bind2nd(plus<Scalar>(), Scalar(1)))).any() );
  130. }
  131. void test_cwiseop()
  132. {
  133. for(int i = 0; i < g_repeat ; i++) {
  134. CALL_SUBTEST_1( cwiseops(Matrix<float, 1, 1>()) );
  135. CALL_SUBTEST_2( cwiseops(Matrix4d()) );
  136. CALL_SUBTEST_3( cwiseops(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  137. CALL_SUBTEST_4( cwiseops(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  138. CALL_SUBTEST_5( cwiseops(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  139. CALL_SUBTEST_6( cwiseops(MatrixXd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  140. }
  141. }