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.

132 lines
5.2 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) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
  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. #include <Eigen/Array>
  11. #include <Eigen/QR>
  12. template<typename Derived1, typename Derived2>
  13. bool areNotApprox(const MatrixBase<Derived1>& m1, const MatrixBase<Derived2>& m2, typename Derived1::RealScalar epsilon = precision<typename Derived1::RealScalar>())
  14. {
  15. return !((m1-m2).cwise().abs2().maxCoeff() < epsilon * epsilon
  16. * std::max(m1.cwise().abs2().maxCoeff(), m2.cwise().abs2().maxCoeff()));
  17. }
  18. template<typename MatrixType> void product(const MatrixType& m)
  19. {
  20. /* this test covers the following files:
  21. Identity.h Product.h
  22. */
  23. typedef typename MatrixType::Scalar Scalar;
  24. typedef typename NumTraits<Scalar>::FloatingPoint FloatingPoint;
  25. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> RowVectorType;
  26. typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, 1> ColVectorType;
  27. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> RowSquareMatrixType;
  28. typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, MatrixType::ColsAtCompileTime> ColSquareMatrixType;
  29. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime,
  30. MatrixType::Options^RowMajor> OtherMajorMatrixType;
  31. int rows = m.rows();
  32. int cols = m.cols();
  33. // this test relies a lot on Random.h, and there's not much more that we can do
  34. // to test it, hence I consider that we will have tested Random.h
  35. MatrixType m1 = MatrixType::Random(rows, cols),
  36. m2 = MatrixType::Random(rows, cols),
  37. m3(rows, cols),
  38. mzero = MatrixType::Zero(rows, cols);
  39. RowSquareMatrixType
  40. identity = RowSquareMatrixType::Identity(rows, rows),
  41. square = RowSquareMatrixType::Random(rows, rows),
  42. res = RowSquareMatrixType::Random(rows, rows);
  43. ColSquareMatrixType
  44. square2 = ColSquareMatrixType::Random(cols, cols),
  45. res2 = ColSquareMatrixType::Random(cols, cols);
  46. RowVectorType v1 = RowVectorType::Random(rows),
  47. v2 = RowVectorType::Random(rows),
  48. vzero = RowVectorType::Zero(rows);
  49. ColVectorType vc2 = ColVectorType::Random(cols), vcres(cols);
  50. OtherMajorMatrixType tm1 = m1;
  51. Scalar s1 = ei_random<Scalar>();
  52. int r = ei_random<int>(0, rows-1),
  53. c = ei_random<int>(0, cols-1);
  54. // begin testing Product.h: only associativity for now
  55. // (we use Transpose.h but this doesn't count as a test for it)
  56. VERIFY_IS_APPROX((m1*m1.transpose())*m2, m1*(m1.transpose()*m2));
  57. m3 = m1;
  58. m3 *= m1.transpose() * m2;
  59. VERIFY_IS_APPROX(m3, m1 * (m1.transpose()*m2));
  60. VERIFY_IS_APPROX(m3, m1.lazy() * (m1.transpose()*m2));
  61. // continue testing Product.h: distributivity
  62. VERIFY_IS_APPROX(square*(m1 + m2), square*m1+square*m2);
  63. VERIFY_IS_APPROX(square*(m1 - m2), square*m1-square*m2);
  64. // continue testing Product.h: compatibility with ScalarMultiple.h
  65. VERIFY_IS_APPROX(s1*(square*m1), (s1*square)*m1);
  66. VERIFY_IS_APPROX(s1*(square*m1), square*(m1*s1));
  67. // again, test operator() to check const-qualification
  68. s1 += (square.lazy() * m1)(r,c);
  69. // test Product.h together with Identity.h
  70. VERIFY_IS_APPROX(v1, identity*v1);
  71. VERIFY_IS_APPROX(v1.transpose(), v1.transpose() * identity);
  72. // again, test operator() to check const-qualification
  73. VERIFY_IS_APPROX(MatrixType::Identity(rows, cols)(r,c), static_cast<Scalar>(r==c));
  74. if (rows!=cols)
  75. VERIFY_RAISES_ASSERT(m3 = m1*m1);
  76. // test the previous tests were not screwed up because operator* returns 0
  77. // (we use the more accurate default epsilon)
  78. if (NumTraits<Scalar>::HasFloatingPoint && std::min(rows,cols)>1)
  79. {
  80. VERIFY(areNotApprox(m1.transpose()*m2,m2.transpose()*m1));
  81. }
  82. // test optimized operator+= path
  83. res = square;
  84. res += (m1 * m2.transpose()).lazy();
  85. VERIFY_IS_APPROX(res, square + m1 * m2.transpose());
  86. if (NumTraits<Scalar>::HasFloatingPoint && std::min(rows,cols)>1)
  87. {
  88. VERIFY(areNotApprox(res,square + m2 * m1.transpose()));
  89. }
  90. vcres = vc2;
  91. vcres += (m1.transpose() * v1).lazy();
  92. VERIFY_IS_APPROX(vcres, vc2 + m1.transpose() * v1);
  93. tm1 = m1;
  94. VERIFY_IS_APPROX(tm1.transpose() * v1, m1.transpose() * v1);
  95. VERIFY_IS_APPROX(v1.transpose() * tm1, v1.transpose() * m1);
  96. // test submatrix and matrix/vector product
  97. for (int i=0; i<rows; ++i)
  98. res.row(i) = m1.row(i) * m2.transpose();
  99. VERIFY_IS_APPROX(res, m1 * m2.transpose());
  100. // the other way round:
  101. for (int i=0; i<rows; ++i)
  102. res.col(i) = m1 * m2.transpose().col(i);
  103. VERIFY_IS_APPROX(res, m1 * m2.transpose());
  104. res2 = square2;
  105. res2 += (m1.transpose() * m2).lazy();
  106. VERIFY_IS_APPROX(res2, square2 + m1.transpose() * m2);
  107. if (NumTraits<Scalar>::HasFloatingPoint && std::min(rows,cols)>1)
  108. {
  109. VERIFY(areNotApprox(res2,square2 + m2.transpose() * m1));
  110. }
  111. }