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.

178 lines
7.9 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. template<typename MatrixType> void product_extra(const MatrixType& m)
  11. {
  12. typedef typename MatrixType::Index Index;
  13. typedef typename MatrixType::Scalar Scalar;
  14. typedef typename NumTraits<Scalar>::NonInteger NonInteger;
  15. typedef Matrix<Scalar, 1, Dynamic> RowVectorType;
  16. typedef Matrix<Scalar, Dynamic, 1> ColVectorType;
  17. typedef Matrix<Scalar, Dynamic, Dynamic,
  18. MatrixType::Flags&RowMajorBit> OtherMajorMatrixType;
  19. Index rows = m.rows();
  20. Index cols = m.cols();
  21. MatrixType m1 = MatrixType::Random(rows, cols),
  22. m2 = MatrixType::Random(rows, cols),
  23. m3(rows, cols),
  24. mzero = MatrixType::Zero(rows, cols),
  25. identity = MatrixType::Identity(rows, rows),
  26. square = MatrixType::Random(rows, rows),
  27. res = MatrixType::Random(rows, rows),
  28. square2 = MatrixType::Random(cols, cols),
  29. res2 = MatrixType::Random(cols, cols);
  30. RowVectorType v1 = RowVectorType::Random(rows), vrres(rows);
  31. ColVectorType vc2 = ColVectorType::Random(cols), vcres(cols);
  32. OtherMajorMatrixType tm1 = m1;
  33. Scalar s1 = internal::random<Scalar>(),
  34. s2 = internal::random<Scalar>(),
  35. s3 = internal::random<Scalar>();
  36. VERIFY_IS_APPROX(m3.noalias() = m1 * m2.adjoint(), m1 * m2.adjoint().eval());
  37. VERIFY_IS_APPROX(m3.noalias() = m1.adjoint() * square.adjoint(), m1.adjoint().eval() * square.adjoint().eval());
  38. VERIFY_IS_APPROX(m3.noalias() = m1.adjoint() * m2, m1.adjoint().eval() * m2);
  39. VERIFY_IS_APPROX(m3.noalias() = (s1 * m1.adjoint()) * m2, (s1 * m1.adjoint()).eval() * m2);
  40. VERIFY_IS_APPROX(m3.noalias() = ((s1 * m1).adjoint()) * m2, (internal::conj(s1) * m1.adjoint()).eval() * m2);
  41. VERIFY_IS_APPROX(m3.noalias() = (- m1.adjoint() * s1) * (s3 * m2), (- m1.adjoint() * s1).eval() * (s3 * m2).eval());
  42. VERIFY_IS_APPROX(m3.noalias() = (s2 * m1.adjoint() * s1) * m2, (s2 * m1.adjoint() * s1).eval() * m2);
  43. VERIFY_IS_APPROX(m3.noalias() = (-m1*s2) * s1*m2.adjoint(), (-m1*s2).eval() * (s1*m2.adjoint()).eval());
  44. // a very tricky case where a scale factor has to be automatically conjugated:
  45. VERIFY_IS_APPROX( m1.adjoint() * (s1*m2).conjugate(), (m1.adjoint()).eval() * ((s1*m2).conjugate()).eval());
  46. // test all possible conjugate combinations for the four matrix-vector product cases:
  47. VERIFY_IS_APPROX((-m1.conjugate() * s2) * (s1 * vc2),
  48. (-m1.conjugate()*s2).eval() * (s1 * vc2).eval());
  49. VERIFY_IS_APPROX((-m1 * s2) * (s1 * vc2.conjugate()),
  50. (-m1*s2).eval() * (s1 * vc2.conjugate()).eval());
  51. VERIFY_IS_APPROX((-m1.conjugate() * s2) * (s1 * vc2.conjugate()),
  52. (-m1.conjugate()*s2).eval() * (s1 * vc2.conjugate()).eval());
  53. VERIFY_IS_APPROX((s1 * vc2.transpose()) * (-m1.adjoint() * s2),
  54. (s1 * vc2.transpose()).eval() * (-m1.adjoint()*s2).eval());
  55. VERIFY_IS_APPROX((s1 * vc2.adjoint()) * (-m1.transpose() * s2),
  56. (s1 * vc2.adjoint()).eval() * (-m1.transpose()*s2).eval());
  57. VERIFY_IS_APPROX((s1 * vc2.adjoint()) * (-m1.adjoint() * s2),
  58. (s1 * vc2.adjoint()).eval() * (-m1.adjoint()*s2).eval());
  59. VERIFY_IS_APPROX((-m1.adjoint() * s2) * (s1 * v1.transpose()),
  60. (-m1.adjoint()*s2).eval() * (s1 * v1.transpose()).eval());
  61. VERIFY_IS_APPROX((-m1.transpose() * s2) * (s1 * v1.adjoint()),
  62. (-m1.transpose()*s2).eval() * (s1 * v1.adjoint()).eval());
  63. VERIFY_IS_APPROX((-m1.adjoint() * s2) * (s1 * v1.adjoint()),
  64. (-m1.adjoint()*s2).eval() * (s1 * v1.adjoint()).eval());
  65. VERIFY_IS_APPROX((s1 * v1) * (-m1.conjugate() * s2),
  66. (s1 * v1).eval() * (-m1.conjugate()*s2).eval());
  67. VERIFY_IS_APPROX((s1 * v1.conjugate()) * (-m1 * s2),
  68. (s1 * v1.conjugate()).eval() * (-m1*s2).eval());
  69. VERIFY_IS_APPROX((s1 * v1.conjugate()) * (-m1.conjugate() * s2),
  70. (s1 * v1.conjugate()).eval() * (-m1.conjugate()*s2).eval());
  71. VERIFY_IS_APPROX((-m1.adjoint() * s2) * (s1 * v1.adjoint()),
  72. (-m1.adjoint()*s2).eval() * (s1 * v1.adjoint()).eval());
  73. // test the vector-matrix product with non aligned starts
  74. Index i = internal::random<Index>(0,m1.rows()-2);
  75. Index j = internal::random<Index>(0,m1.cols()-2);
  76. Index r = internal::random<Index>(1,m1.rows()-i);
  77. Index c = internal::random<Index>(1,m1.cols()-j);
  78. Index i2 = internal::random<Index>(0,m1.rows()-1);
  79. Index j2 = internal::random<Index>(0,m1.cols()-1);
  80. VERIFY_IS_APPROX(m1.col(j2).adjoint() * m1.block(0,j,m1.rows(),c), m1.col(j2).adjoint().eval() * m1.block(0,j,m1.rows(),c).eval());
  81. VERIFY_IS_APPROX(m1.block(i,0,r,m1.cols()) * m1.row(i2).adjoint(), m1.block(i,0,r,m1.cols()).eval() * m1.row(i2).adjoint().eval());
  82. // regression test
  83. MatrixType tmp = m1 * m1.adjoint() * s1;
  84. VERIFY_IS_APPROX(tmp, m1 * m1.adjoint() * s1);
  85. }
  86. // Regression test for bug reported at http://forum.kde.org/viewtopic.php?f=74&t=96947
  87. void mat_mat_scalar_scalar_product()
  88. {
  89. Eigen::Matrix2Xd dNdxy(2, 3);
  90. dNdxy << -0.5, 0.5, 0,
  91. -0.3, 0, 0.3;
  92. double det = 6.0, wt = 0.5;
  93. VERIFY_IS_APPROX(dNdxy.transpose()*dNdxy*det*wt, det*wt*dNdxy.transpose()*dNdxy);
  94. }
  95. void zero_sized_objects()
  96. {
  97. // Bug 127
  98. //
  99. // a product of the form lhs*rhs with
  100. //
  101. // lhs:
  102. // rows = 1, cols = 4
  103. // RowsAtCompileTime = 1, ColsAtCompileTime = -1
  104. // MaxRowsAtCompileTime = 1, MaxColsAtCompileTime = 5
  105. //
  106. // rhs:
  107. // rows = 4, cols = 0
  108. // RowsAtCompileTime = -1, ColsAtCompileTime = -1
  109. // MaxRowsAtCompileTime = 5, MaxColsAtCompileTime = 1
  110. //
  111. // was failing on a runtime assertion, because it had been mis-compiled as a dot product because Product.h was using the
  112. // max-sizes to detect size 1 indicating vectors, and that didn't account for 0-sized object with max-size 1.
  113. Matrix<float,1,Dynamic,RowMajor,1,5> a(1,4);
  114. Matrix<float,Dynamic,Dynamic,ColMajor,5,1> b(4,0);
  115. a*b;
  116. }
  117. void unaligned_objects()
  118. {
  119. // Regression test for the bug reported here:
  120. // http://forum.kde.org/viewtopic.php?f=74&t=107541
  121. // Recall the matrix*vector kernel avoid unaligned loads by loading two packets and then reassemble then.
  122. // There was a mistake in the computation of the valid range for fully unaligned objects: in some rare cases,
  123. // memory was read outside the allocated matrix memory. Though the values were not used, this might raise segfault.
  124. for(int m=450;m<460;++m)
  125. {
  126. for(int n=8;n<12;++n)
  127. {
  128. MatrixXf M(m, n);
  129. VectorXf v1(n), r1(500);
  130. RowVectorXf v2(m), r2(16);
  131. M.setRandom();
  132. v1.setRandom();
  133. v2.setRandom();
  134. for(int o=0; o<4; ++o)
  135. {
  136. r1.segment(o,m).noalias() = M * v1;
  137. VERIFY_IS_APPROX(r1.segment(o,m), M * MatrixXf(v1));
  138. r2.segment(o,n).noalias() = v2 * M;
  139. VERIFY_IS_APPROX(r2.segment(o,n), MatrixXf(v2) * M);
  140. }
  141. }
  142. }
  143. }
  144. void test_product_extra()
  145. {
  146. for(int i = 0; i < g_repeat; i++) {
  147. CALL_SUBTEST_1( product_extra(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  148. CALL_SUBTEST_2( product_extra(MatrixXd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  149. CALL_SUBTEST_2( mat_mat_scalar_scalar_product() );
  150. CALL_SUBTEST_3( product_extra(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) );
  151. CALL_SUBTEST_4( product_extra(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) );
  152. }
  153. CALL_SUBTEST_5( zero_sized_objects() );
  154. CALL_SUBTEST_6( unaligned_objects() );
  155. }