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.

158 lines
5.8 KiB

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