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.

87 lines
3.5 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
  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_selfadjoint(const MatrixType& m)
  11. {
  12. typedef typename MatrixType::Index Index;
  13. typedef typename MatrixType::Scalar Scalar;
  14. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
  15. typedef Matrix<Scalar, 1, MatrixType::RowsAtCompileTime> RowVectorType;
  16. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, Dynamic, RowMajor> RhsMatrixType;
  17. Index rows = m.rows();
  18. Index cols = m.cols();
  19. MatrixType m1 = MatrixType::Random(rows, cols),
  20. m2 = MatrixType::Random(rows, cols),
  21. m3;
  22. VectorType v1 = VectorType::Random(rows),
  23. v2 = VectorType::Random(rows),
  24. v3(rows);
  25. RowVectorType r1 = RowVectorType::Random(rows),
  26. r2 = RowVectorType::Random(rows);
  27. RhsMatrixType m4 = RhsMatrixType::Random(rows,10);
  28. Scalar s1 = internal::random<Scalar>(),
  29. s2 = internal::random<Scalar>(),
  30. s3 = internal::random<Scalar>();
  31. m1 = (m1.adjoint() + m1).eval();
  32. // rank2 update
  33. m2 = m1.template triangularView<Lower>();
  34. m2.template selfadjointView<Lower>().rankUpdate(v1,v2);
  35. VERIFY_IS_APPROX(m2, (m1 + v1 * v2.adjoint()+ v2 * v1.adjoint()).template triangularView<Lower>().toDenseMatrix());
  36. m2 = m1.template triangularView<Upper>();
  37. m2.template selfadjointView<Upper>().rankUpdate(-v1,s2*v2,s3);
  38. VERIFY_IS_APPROX(m2, (m1 + (s3*(-v1)*(s2*v2).adjoint()+numext::conj(s3)*(s2*v2)*(-v1).adjoint())).template triangularView<Upper>().toDenseMatrix());
  39. m2 = m1.template triangularView<Upper>();
  40. m2.template selfadjointView<Upper>().rankUpdate(-s2*r1.adjoint(),r2.adjoint()*s3,s1);
  41. VERIFY_IS_APPROX(m2, (m1 + s1*(-s2*r1.adjoint())*(r2.adjoint()*s3).adjoint() + numext::conj(s1)*(r2.adjoint()*s3) * (-s2*r1.adjoint()).adjoint()).template triangularView<Upper>().toDenseMatrix());
  42. if (rows>1)
  43. {
  44. m2 = m1.template triangularView<Lower>();
  45. m2.block(1,1,rows-1,cols-1).template selfadjointView<Lower>().rankUpdate(v1.tail(rows-1),v2.head(cols-1));
  46. m3 = m1;
  47. m3.block(1,1,rows-1,cols-1) += v1.tail(rows-1) * v2.head(cols-1).adjoint()+ v2.head(cols-1) * v1.tail(rows-1).adjoint();
  48. VERIFY_IS_APPROX(m2, m3.template triangularView<Lower>().toDenseMatrix());
  49. }
  50. }
  51. void test_product_selfadjoint()
  52. {
  53. int s = 0;
  54. for(int i = 0; i < g_repeat ; i++) {
  55. CALL_SUBTEST_1( product_selfadjoint(Matrix<float, 1, 1>()) );
  56. CALL_SUBTEST_2( product_selfadjoint(Matrix<float, 2, 2>()) );
  57. CALL_SUBTEST_3( product_selfadjoint(Matrix3d()) );
  58. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2);
  59. CALL_SUBTEST_4( product_selfadjoint(MatrixXcf(s, s)) );
  60. TEST_SET_BUT_UNUSED_VARIABLE(s)
  61. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2);
  62. CALL_SUBTEST_5( product_selfadjoint(MatrixXcd(s,s)) );
  63. TEST_SET_BUT_UNUSED_VARIABLE(s)
  64. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE);
  65. CALL_SUBTEST_6( product_selfadjoint(MatrixXd(s,s)) );
  66. TEST_SET_BUT_UNUSED_VARIABLE(s)
  67. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE);
  68. CALL_SUBTEST_7( product_selfadjoint(Matrix<float,Dynamic,Dynamic,RowMajor>(s,s)) );
  69. TEST_SET_BUT_UNUSED_VARIABLE(s)
  70. }
  71. }