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.

148 lines
6.0 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. // check minor separately in order to avoid the possible creation of a zero-sized
  11. // array. Comes from a compilation error with gcc-3.4 or gcc-4 with -ansi -pedantic.
  12. // Another solution would be to declare the array like this: T m_data[Size==0?1:Size]; in ei_matrix_storage
  13. // but this is probably not bad to raise such an error at compile time...
  14. template<typename Scalar, int _Rows, int _Cols> struct CheckMinor
  15. {
  16. typedef Matrix<Scalar, _Rows, _Cols> MatrixType;
  17. CheckMinor(MatrixType& m1, int r1, int c1)
  18. {
  19. int rows = m1.rows();
  20. int cols = m1.cols();
  21. Matrix<Scalar, Dynamic, Dynamic> mi = m1.minor(0,0).eval();
  22. VERIFY_IS_APPROX(mi, m1.block(1,1,rows-1,cols-1));
  23. mi = m1.minor(r1,c1);
  24. VERIFY_IS_APPROX(mi.transpose(), m1.transpose().minor(c1,r1));
  25. //check operator(), both constant and non-constant, on minor()
  26. m1.minor(r1,c1)(0,0) = m1.minor(0,0)(0,0);
  27. }
  28. };
  29. template<typename Scalar> struct CheckMinor<Scalar,1,1>
  30. {
  31. typedef Matrix<Scalar, 1, 1> MatrixType;
  32. CheckMinor(MatrixType&, int, int) {}
  33. };
  34. template<typename MatrixType> void submatrices(const MatrixType& m)
  35. {
  36. /* this test covers the following files:
  37. Row.h Column.h Block.h Minor.h DiagonalCoeffs.h
  38. */
  39. typedef typename MatrixType::Scalar Scalar;
  40. typedef typename MatrixType::RealScalar RealScalar;
  41. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
  42. typedef Matrix<Scalar, 1, MatrixType::ColsAtCompileTime> RowVectorType;
  43. int rows = m.rows();
  44. int cols = m.cols();
  45. MatrixType m1 = MatrixType::Random(rows, cols),
  46. m2 = MatrixType::Random(rows, cols),
  47. m3(rows, cols),
  48. mzero = MatrixType::Zero(rows, cols),
  49. ones = MatrixType::Ones(rows, cols),
  50. identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
  51. ::Identity(rows, rows),
  52. square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
  53. ::Random(rows, rows);
  54. VectorType v1 = VectorType::Random(rows),
  55. v2 = VectorType::Random(rows),
  56. v3 = VectorType::Random(rows),
  57. vzero = VectorType::Zero(rows);
  58. Scalar s1 = ei_random<Scalar>();
  59. int r1 = ei_random<int>(0,rows-1);
  60. int r2 = ei_random<int>(r1,rows-1);
  61. int c1 = ei_random<int>(0,cols-1);
  62. int c2 = ei_random<int>(c1,cols-1);
  63. //check row() and col()
  64. VERIFY_IS_APPROX(m1.col(c1).transpose(), m1.transpose().row(c1));
  65. VERIFY_IS_APPROX(square.row(r1).eigen2_dot(m1.col(c1)), (square.lazy() * m1.conjugate())(r1,c1));
  66. //check operator(), both constant and non-constant, on row() and col()
  67. m1.row(r1) += s1 * m1.row(r2);
  68. m1.col(c1) += s1 * m1.col(c2);
  69. //check block()
  70. Matrix<Scalar,Dynamic,Dynamic> b1(1,1); b1(0,0) = m1(r1,c1);
  71. RowVectorType br1(m1.block(r1,0,1,cols));
  72. VectorType bc1(m1.block(0,c1,rows,1));
  73. VERIFY_IS_APPROX(b1, m1.block(r1,c1,1,1));
  74. VERIFY_IS_APPROX(m1.row(r1), br1);
  75. VERIFY_IS_APPROX(m1.col(c1), bc1);
  76. //check operator(), both constant and non-constant, on block()
  77. m1.block(r1,c1,r2-r1+1,c2-c1+1) = s1 * m2.block(0, 0, r2-r1+1,c2-c1+1);
  78. m1.block(r1,c1,r2-r1+1,c2-c1+1)(r2-r1,c2-c1) = m2.block(0, 0, r2-r1+1,c2-c1+1)(0,0);
  79. //check minor()
  80. CheckMinor<Scalar, MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime> checkminor(m1,r1,c1);
  81. //check diagonal()
  82. VERIFY_IS_APPROX(m1.diagonal(), m1.transpose().diagonal());
  83. m2.diagonal() = 2 * m1.diagonal();
  84. m2.diagonal()[0] *= 3;
  85. VERIFY_IS_APPROX(m2.diagonal()[0], static_cast<Scalar>(6) * m1.diagonal()[0]);
  86. enum {
  87. BlockRows = EIGEN_SIZE_MIN_PREFER_FIXED(MatrixType::RowsAtCompileTime,2),
  88. BlockCols = EIGEN_SIZE_MIN_PREFER_FIXED(MatrixType::ColsAtCompileTime,5)
  89. };
  90. if (rows>=5 && cols>=8)
  91. {
  92. // test fixed block() as lvalue
  93. m1.template block<BlockRows,BlockCols>(1,1) *= s1;
  94. // test operator() on fixed block() both as constant and non-constant
  95. m1.template block<BlockRows,BlockCols>(1,1)(0, 3) = m1.template block<2,5>(1,1)(1,2);
  96. // check that fixed block() and block() agree
  97. Matrix<Scalar,Dynamic,Dynamic> b = m1.template block<BlockRows,BlockCols>(3,3);
  98. VERIFY_IS_APPROX(b, m1.block(3,3,BlockRows,BlockCols));
  99. }
  100. if (rows>2)
  101. {
  102. // test sub vectors
  103. VERIFY_IS_APPROX(v1.template start<2>(), v1.block(0,0,2,1));
  104. VERIFY_IS_APPROX(v1.template start<2>(), v1.start(2));
  105. VERIFY_IS_APPROX(v1.template start<2>(), v1.segment(0,2));
  106. VERIFY_IS_APPROX(v1.template start<2>(), v1.template segment<2>(0));
  107. int i = rows-2;
  108. VERIFY_IS_APPROX(v1.template end<2>(), v1.block(i,0,2,1));
  109. VERIFY_IS_APPROX(v1.template end<2>(), v1.end(2));
  110. VERIFY_IS_APPROX(v1.template end<2>(), v1.segment(i,2));
  111. VERIFY_IS_APPROX(v1.template end<2>(), v1.template segment<2>(i));
  112. i = ei_random(0,rows-2);
  113. VERIFY_IS_APPROX(v1.segment(i,2), v1.template segment<2>(i));
  114. }
  115. // stress some basic stuffs with block matrices
  116. VERIFY(ei_real(ones.col(c1).sum()) == RealScalar(rows));
  117. VERIFY(ei_real(ones.row(r1).sum()) == RealScalar(cols));
  118. VERIFY(ei_real(ones.col(c1).eigen2_dot(ones.col(c2))) == RealScalar(rows));
  119. VERIFY(ei_real(ones.row(r1).eigen2_dot(ones.row(r2))) == RealScalar(cols));
  120. }
  121. void test_eigen2_submatrices()
  122. {
  123. for(int i = 0; i < g_repeat; i++) {
  124. CALL_SUBTEST_1( submatrices(Matrix<float, 1, 1>()) );
  125. CALL_SUBTEST_2( submatrices(Matrix4d()) );
  126. CALL_SUBTEST_3( submatrices(MatrixXcf(3, 3)) );
  127. CALL_SUBTEST_4( submatrices(MatrixXi(8, 12)) );
  128. CALL_SUBTEST_5( submatrices(MatrixXcd(20, 20)) );
  129. CALL_SUBTEST_6( submatrices(MatrixXf(20, 20)) );
  130. }
  131. }