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.

103 lines
4.6 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2009 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. using namespace std;
  11. template<typename MatrixType> void diagonalmatrices(const MatrixType& m)
  12. {
  13. typedef typename MatrixType::Index Index;
  14. typedef typename MatrixType::Scalar Scalar;
  15. typedef typename MatrixType::RealScalar RealScalar;
  16. enum { Rows = MatrixType::RowsAtCompileTime, Cols = MatrixType::ColsAtCompileTime };
  17. typedef Matrix<Scalar, Rows, 1> VectorType;
  18. typedef Matrix<Scalar, 1, Cols> RowVectorType;
  19. typedef Matrix<Scalar, Rows, Rows> SquareMatrixType;
  20. typedef DiagonalMatrix<Scalar, Rows> LeftDiagonalMatrix;
  21. typedef DiagonalMatrix<Scalar, Cols> RightDiagonalMatrix;
  22. typedef Matrix<Scalar, Rows==Dynamic?Dynamic:2*Rows, Cols==Dynamic?Dynamic:2*Cols> BigMatrix;
  23. Index rows = m.rows();
  24. Index cols = m.cols();
  25. MatrixType m1 = MatrixType::Random(rows, cols),
  26. m2 = MatrixType::Random(rows, cols);
  27. VectorType v1 = VectorType::Random(rows),
  28. v2 = VectorType::Random(rows);
  29. RowVectorType rv1 = RowVectorType::Random(cols),
  30. rv2 = RowVectorType::Random(cols);
  31. LeftDiagonalMatrix ldm1(v1), ldm2(v2);
  32. RightDiagonalMatrix rdm1(rv1), rdm2(rv2);
  33. Scalar s1 = internal::random<Scalar>();
  34. SquareMatrixType sq_m1 (v1.asDiagonal());
  35. VERIFY_IS_APPROX(sq_m1, v1.asDiagonal().toDenseMatrix());
  36. sq_m1 = v1.asDiagonal();
  37. VERIFY_IS_APPROX(sq_m1, v1.asDiagonal().toDenseMatrix());
  38. SquareMatrixType sq_m2 = v1.asDiagonal();
  39. VERIFY_IS_APPROX(sq_m1, sq_m2);
  40. ldm1 = v1.asDiagonal();
  41. LeftDiagonalMatrix ldm3(v1);
  42. VERIFY_IS_APPROX(ldm1.diagonal(), ldm3.diagonal());
  43. LeftDiagonalMatrix ldm4 = v1.asDiagonal();
  44. VERIFY_IS_APPROX(ldm1.diagonal(), ldm4.diagonal());
  45. sq_m1.block(0,0,rows,rows) = ldm1;
  46. VERIFY_IS_APPROX(sq_m1, ldm1.toDenseMatrix());
  47. sq_m1.transpose() = ldm1;
  48. VERIFY_IS_APPROX(sq_m1, ldm1.toDenseMatrix());
  49. Index i = internal::random<Index>(0, rows-1);
  50. Index j = internal::random<Index>(0, cols-1);
  51. VERIFY_IS_APPROX( ((ldm1 * m1)(i,j)) , ldm1.diagonal()(i) * m1(i,j) );
  52. VERIFY_IS_APPROX( ((ldm1 * (m1+m2))(i,j)) , ldm1.diagonal()(i) * (m1+m2)(i,j) );
  53. VERIFY_IS_APPROX( ((m1 * rdm1)(i,j)) , rdm1.diagonal()(j) * m1(i,j) );
  54. VERIFY_IS_APPROX( ((v1.asDiagonal() * m1)(i,j)) , v1(i) * m1(i,j) );
  55. VERIFY_IS_APPROX( ((m1 * rv1.asDiagonal())(i,j)) , rv1(j) * m1(i,j) );
  56. VERIFY_IS_APPROX( (((v1+v2).asDiagonal() * m1)(i,j)) , (v1+v2)(i) * m1(i,j) );
  57. VERIFY_IS_APPROX( (((v1+v2).asDiagonal() * (m1+m2))(i,j)) , (v1+v2)(i) * (m1+m2)(i,j) );
  58. VERIFY_IS_APPROX( ((m1 * (rv1+rv2).asDiagonal())(i,j)) , (rv1+rv2)(j) * m1(i,j) );
  59. VERIFY_IS_APPROX( (((m1+m2) * (rv1+rv2).asDiagonal())(i,j)) , (rv1+rv2)(j) * (m1+m2)(i,j) );
  60. BigMatrix big;
  61. big.setZero(2*rows, 2*cols);
  62. big.block(i,j,rows,cols) = m1;
  63. big.block(i,j,rows,cols) = v1.asDiagonal() * big.block(i,j,rows,cols);
  64. VERIFY_IS_APPROX((big.block(i,j,rows,cols)) , v1.asDiagonal() * m1 );
  65. big.block(i,j,rows,cols) = m1;
  66. big.block(i,j,rows,cols) = big.block(i,j,rows,cols) * rv1.asDiagonal();
  67. VERIFY_IS_APPROX((big.block(i,j,rows,cols)) , m1 * rv1.asDiagonal() );
  68. // scalar multiple
  69. VERIFY_IS_APPROX(LeftDiagonalMatrix(ldm1*s1).diagonal(), ldm1.diagonal() * s1);
  70. VERIFY_IS_APPROX(LeftDiagonalMatrix(s1*ldm1).diagonal(), s1 * ldm1.diagonal());
  71. VERIFY_IS_APPROX(m1 * (rdm1 * s1), (m1 * rdm1) * s1);
  72. VERIFY_IS_APPROX(m1 * (s1 * rdm1), (m1 * rdm1) * s1);
  73. }
  74. void test_diagonalmatrices()
  75. {
  76. for(int i = 0; i < g_repeat; i++) {
  77. CALL_SUBTEST_1( diagonalmatrices(Matrix<float, 1, 1>()) );
  78. CALL_SUBTEST_2( diagonalmatrices(Matrix3f()) );
  79. CALL_SUBTEST_3( diagonalmatrices(Matrix<double,3,3,RowMajor>()) );
  80. CALL_SUBTEST_4( diagonalmatrices(Matrix4d()) );
  81. CALL_SUBTEST_5( diagonalmatrices(Matrix<float,4,4,RowMajor>()) );
  82. CALL_SUBTEST_6( diagonalmatrices(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  83. CALL_SUBTEST_7( diagonalmatrices(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  84. CALL_SUBTEST_8( diagonalmatrices(Matrix<double,Dynamic,Dynamic,RowMajor>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  85. CALL_SUBTEST_9( diagonalmatrices(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  86. }
  87. }