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
6.6 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  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/QR>
  11. template<typename Derived1, typename Derived2>
  12. bool areNotApprox(const MatrixBase<Derived1>& m1, const MatrixBase<Derived2>& m2, typename Derived1::RealScalar epsilon = NumTraits<typename Derived1::RealScalar>::dummy_precision())
  13. {
  14. return !((m1-m2).cwiseAbs2().maxCoeff() < epsilon * epsilon
  15. * (std::max)(m1.cwiseAbs2().maxCoeff(), m2.cwiseAbs2().maxCoeff()));
  16. }
  17. template<typename MatrixType> void product(const MatrixType& m)
  18. {
  19. /* this test covers the following files:
  20. Identity.h Product.h
  21. */
  22. typedef typename MatrixType::Scalar Scalar;
  23. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> RowVectorType;
  24. typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, 1> ColVectorType;
  25. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> RowSquareMatrixType;
  26. typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, MatrixType::ColsAtCompileTime> ColSquareMatrixType;
  27. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime,
  28. MatrixType::Flags&RowMajorBit?ColMajor:RowMajor> OtherMajorMatrixType;
  29. Index rows = m.rows();
  30. Index cols = m.cols();
  31. // this test relies a lot on Random.h, and there's not much more that we can do
  32. // to test it, hence I consider that we will have tested Random.h
  33. MatrixType m1 = MatrixType::Random(rows, cols),
  34. m2 = MatrixType::Random(rows, cols),
  35. m3(rows, cols);
  36. RowSquareMatrixType
  37. identity = RowSquareMatrixType::Identity(rows, rows),
  38. square = RowSquareMatrixType::Random(rows, rows),
  39. res = RowSquareMatrixType::Random(rows, rows);
  40. ColSquareMatrixType
  41. square2 = ColSquareMatrixType::Random(cols, cols),
  42. res2 = ColSquareMatrixType::Random(cols, cols);
  43. RowVectorType v1 = RowVectorType::Random(rows);
  44. ColVectorType vc2 = ColVectorType::Random(cols), vcres(cols);
  45. OtherMajorMatrixType tm1 = m1;
  46. Scalar s1 = internal::random<Scalar>();
  47. Index r = internal::random<Index>(0, rows-1),
  48. c = internal::random<Index>(0, cols-1),
  49. c2 = internal::random<Index>(0, cols-1);
  50. // begin testing Product.h: only associativity for now
  51. // (we use Transpose.h but this doesn't count as a test for it)
  52. VERIFY_IS_APPROX((m1*m1.transpose())*m2, m1*(m1.transpose()*m2));
  53. m3 = m1;
  54. m3 *= m1.transpose() * m2;
  55. VERIFY_IS_APPROX(m3, m1 * (m1.transpose()*m2));
  56. VERIFY_IS_APPROX(m3, m1 * (m1.transpose()*m2));
  57. // continue testing Product.h: distributivity
  58. VERIFY_IS_APPROX(square*(m1 + m2), square*m1+square*m2);
  59. VERIFY_IS_APPROX(square*(m1 - m2), square*m1-square*m2);
  60. // continue testing Product.h: compatibility with ScalarMultiple.h
  61. VERIFY_IS_APPROX(s1*(square*m1), (s1*square)*m1);
  62. VERIFY_IS_APPROX(s1*(square*m1), square*(m1*s1));
  63. // test Product.h together with Identity.h
  64. VERIFY_IS_APPROX(v1, identity*v1);
  65. VERIFY_IS_APPROX(v1.transpose(), v1.transpose() * identity);
  66. // again, test operator() to check const-qualification
  67. VERIFY_IS_APPROX(MatrixType::Identity(rows, cols)(r,c), static_cast<Scalar>(r==c));
  68. if (rows!=cols)
  69. VERIFY_RAISES_ASSERT(m3 = m1*m1);
  70. // test the previous tests were not screwed up because operator* returns 0
  71. // (we use the more accurate default epsilon)
  72. if (!NumTraits<Scalar>::IsInteger && (std::min)(rows,cols)>1)
  73. {
  74. VERIFY(areNotApprox(m1.transpose()*m2,m2.transpose()*m1));
  75. }
  76. // test optimized operator+= path
  77. res = square;
  78. res.noalias() += m1 * m2.transpose();
  79. VERIFY_IS_APPROX(res, square + m1 * m2.transpose());
  80. if (!NumTraits<Scalar>::IsInteger && (std::min)(rows,cols)>1)
  81. {
  82. VERIFY(areNotApprox(res,square + m2 * m1.transpose()));
  83. }
  84. vcres = vc2;
  85. vcres.noalias() += m1.transpose() * v1;
  86. VERIFY_IS_APPROX(vcres, vc2 + m1.transpose() * v1);
  87. // test optimized operator-= path
  88. res = square;
  89. res.noalias() -= m1 * m2.transpose();
  90. VERIFY_IS_APPROX(res, square - (m1 * m2.transpose()));
  91. if (!NumTraits<Scalar>::IsInteger && (std::min)(rows,cols)>1)
  92. {
  93. VERIFY(areNotApprox(res,square - m2 * m1.transpose()));
  94. }
  95. vcres = vc2;
  96. vcres.noalias() -= m1.transpose() * v1;
  97. VERIFY_IS_APPROX(vcres, vc2 - m1.transpose() * v1);
  98. // test d ?= a+b*c rules
  99. res.noalias() = square + m1 * m2.transpose();
  100. VERIFY_IS_APPROX(res, square + m1 * m2.transpose());
  101. res.noalias() += square + m1 * m2.transpose();
  102. VERIFY_IS_APPROX(res, 2*(square + m1 * m2.transpose()));
  103. res.noalias() -= square + m1 * m2.transpose();
  104. VERIFY_IS_APPROX(res, square + m1 * m2.transpose());
  105. tm1 = m1;
  106. VERIFY_IS_APPROX(tm1.transpose() * v1, m1.transpose() * v1);
  107. VERIFY_IS_APPROX(v1.transpose() * tm1, v1.transpose() * m1);
  108. // test submatrix and matrix/vector product
  109. for (int i=0; i<rows; ++i)
  110. res.row(i) = m1.row(i) * m2.transpose();
  111. VERIFY_IS_APPROX(res, m1 * m2.transpose());
  112. // the other way round:
  113. for (int i=0; i<rows; ++i)
  114. res.col(i) = m1 * m2.transpose().col(i);
  115. VERIFY_IS_APPROX(res, m1 * m2.transpose());
  116. res2 = square2;
  117. res2.noalias() += m1.transpose() * m2;
  118. VERIFY_IS_APPROX(res2, square2 + m1.transpose() * m2);
  119. if (!NumTraits<Scalar>::IsInteger && (std::min)(rows,cols)>1)
  120. {
  121. VERIFY(areNotApprox(res2,square2 + m2.transpose() * m1));
  122. }
  123. VERIFY_IS_APPROX(res.col(r).noalias() = square.adjoint() * square.col(r), (square.adjoint() * square.col(r)).eval());
  124. VERIFY_IS_APPROX(res.col(r).noalias() = square * square.col(r), (square * square.col(r)).eval());
  125. // inner product
  126. Scalar x = square2.row(c) * square2.col(c2);
  127. VERIFY_IS_APPROX(x, square2.row(c).transpose().cwiseProduct(square2.col(c2)).sum());
  128. // outer product
  129. VERIFY_IS_APPROX(m1.col(c) * m1.row(r), m1.block(0,c,rows,1) * m1.block(r,0,1,cols));
  130. VERIFY_IS_APPROX(m1.row(r).transpose() * m1.col(c).transpose(), m1.block(r,0,1,cols).transpose() * m1.block(0,c,rows,1).transpose());
  131. VERIFY_IS_APPROX(m1.block(0,c,rows,1) * m1.row(r), m1.block(0,c,rows,1) * m1.block(r,0,1,cols));
  132. VERIFY_IS_APPROX(m1.col(c) * m1.block(r,0,1,cols), m1.block(0,c,rows,1) * m1.block(r,0,1,cols));
  133. VERIFY_IS_APPROX(m1.leftCols(1) * m1.row(r), m1.block(0,0,rows,1) * m1.block(r,0,1,cols));
  134. VERIFY_IS_APPROX(m1.col(c) * m1.topRows(1), m1.block(0,c,rows,1) * m1.block(0,0,1,cols));
  135. }