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.

81 lines
3.4 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 typename NumTraits<Scalar>::Real RealScalar;
  15. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
  16. typedef Matrix<Scalar, 1, MatrixType::RowsAtCompileTime> RowVectorType;
  17. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, Dynamic, RowMajor> RhsMatrixType;
  18. Index rows = m.rows();
  19. Index cols = m.cols();
  20. MatrixType m1 = MatrixType::Random(rows, cols),
  21. m2 = MatrixType::Random(rows, cols),
  22. m3;
  23. VectorType v1 = VectorType::Random(rows),
  24. v2 = VectorType::Random(rows),
  25. v3(rows);
  26. RowVectorType r1 = RowVectorType::Random(rows),
  27. r2 = RowVectorType::Random(rows);
  28. RhsMatrixType m4 = RhsMatrixType::Random(rows,10);
  29. Scalar s1 = internal::random<Scalar>(),
  30. s2 = internal::random<Scalar>(),
  31. s3 = internal::random<Scalar>();
  32. m1 = (m1.adjoint() + m1).eval();
  33. // rank2 update
  34. m2 = m1.template triangularView<Lower>();
  35. m2.template selfadjointView<Lower>().rankUpdate(v1,v2);
  36. VERIFY_IS_APPROX(m2, (m1 + v1 * v2.adjoint()+ v2 * v1.adjoint()).template triangularView<Lower>().toDenseMatrix());
  37. m2 = m1.template triangularView<Upper>();
  38. m2.template selfadjointView<Upper>().rankUpdate(-v1,s2*v2,s3);
  39. VERIFY_IS_APPROX(m2, (m1 + (s3*(-v1)*(s2*v2).adjoint()+internal::conj(s3)*(s2*v2)*(-v1).adjoint())).template triangularView<Upper>().toDenseMatrix());
  40. m2 = m1.template triangularView<Upper>();
  41. m2.template selfadjointView<Upper>().rankUpdate(-s2*r1.adjoint(),r2.adjoint()*s3,s1);
  42. VERIFY_IS_APPROX(m2, (m1 + s1*(-s2*r1.adjoint())*(r2.adjoint()*s3).adjoint() + internal::conj(s1)*(r2.adjoint()*s3) * (-s2*r1.adjoint()).adjoint()).template triangularView<Upper>().toDenseMatrix());
  43. if (rows>1)
  44. {
  45. m2 = m1.template triangularView<Lower>();
  46. m2.block(1,1,rows-1,cols-1).template selfadjointView<Lower>().rankUpdate(v1.tail(rows-1),v2.head(cols-1));
  47. m3 = m1;
  48. 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();
  49. VERIFY_IS_APPROX(m2, m3.template triangularView<Lower>().toDenseMatrix());
  50. }
  51. }
  52. void test_product_selfadjoint()
  53. {
  54. int s;
  55. for(int i = 0; i < g_repeat ; i++) {
  56. CALL_SUBTEST_1( product_selfadjoint(Matrix<float, 1, 1>()) );
  57. CALL_SUBTEST_2( product_selfadjoint(Matrix<float, 2, 2>()) );
  58. CALL_SUBTEST_3( product_selfadjoint(Matrix3d()) );
  59. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2);
  60. CALL_SUBTEST_4( product_selfadjoint(MatrixXcf(s, s)) );
  61. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2);
  62. CALL_SUBTEST_5( product_selfadjoint(MatrixXcd(s,s)) );
  63. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE);
  64. CALL_SUBTEST_6( product_selfadjoint(MatrixXd(s,s)) );
  65. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE);
  66. CALL_SUBTEST_7( product_selfadjoint(Matrix<float,Dynamic,Dynamic,RowMajor>(s,s)) );
  67. }
  68. EIGEN_UNUSED_VARIABLE(s)
  69. }