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.

98 lines
4.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 syrk(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::ColsAtCompileTime, Dynamic> Rhs1;
  16. typedef Matrix<Scalar, Dynamic, MatrixType::RowsAtCompileTime> Rhs2;
  17. typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, Dynamic,RowMajor> Rhs3;
  18. Index rows = m.rows();
  19. Index cols = m.cols();
  20. MatrixType m1 = MatrixType::Random(rows, cols),
  21. m2 = MatrixType::Random(rows, cols);
  22. Rhs1 rhs1 = Rhs1::Random(internal::random<int>(1,320), cols);
  23. Rhs2 rhs2 = Rhs2::Random(rows, internal::random<int>(1,320));
  24. Rhs3 rhs3 = Rhs3::Random(internal::random<int>(1,320), rows);
  25. Scalar s1 = internal::random<Scalar>();
  26. Index c = internal::random<Index>(0,cols-1);
  27. m2.setZero();
  28. VERIFY_IS_APPROX((m2.template selfadjointView<Lower>().rankUpdate(rhs2,s1)._expression()),
  29. ((s1 * rhs2 * rhs2.adjoint()).eval().template triangularView<Lower>().toDenseMatrix()));
  30. m2.setZero();
  31. VERIFY_IS_APPROX(m2.template selfadjointView<Upper>().rankUpdate(rhs2,s1)._expression(),
  32. (s1 * rhs2 * rhs2.adjoint()).eval().template triangularView<Upper>().toDenseMatrix());
  33. m2.setZero();
  34. VERIFY_IS_APPROX(m2.template selfadjointView<Lower>().rankUpdate(rhs1.adjoint(),s1)._expression(),
  35. (s1 * rhs1.adjoint() * rhs1).eval().template triangularView<Lower>().toDenseMatrix());
  36. m2.setZero();
  37. VERIFY_IS_APPROX(m2.template selfadjointView<Upper>().rankUpdate(rhs1.adjoint(),s1)._expression(),
  38. (s1 * rhs1.adjoint() * rhs1).eval().template triangularView<Upper>().toDenseMatrix());
  39. m2.setZero();
  40. VERIFY_IS_APPROX(m2.template selfadjointView<Lower>().rankUpdate(rhs3.adjoint(),s1)._expression(),
  41. (s1 * rhs3.adjoint() * rhs3).eval().template triangularView<Lower>().toDenseMatrix());
  42. m2.setZero();
  43. VERIFY_IS_APPROX(m2.template selfadjointView<Upper>().rankUpdate(rhs3.adjoint(),s1)._expression(),
  44. (s1 * rhs3.adjoint() * rhs3).eval().template triangularView<Upper>().toDenseMatrix());
  45. m2.setZero();
  46. VERIFY_IS_APPROX((m2.template selfadjointView<Lower>().rankUpdate(m1.col(c),s1)._expression()),
  47. ((s1 * m1.col(c) * m1.col(c).adjoint()).eval().template triangularView<Lower>().toDenseMatrix()));
  48. m2.setZero();
  49. VERIFY_IS_APPROX((m2.template selfadjointView<Upper>().rankUpdate(m1.col(c),s1)._expression()),
  50. ((s1 * m1.col(c) * m1.col(c).adjoint()).eval().template triangularView<Upper>().toDenseMatrix()));
  51. m2.setZero();
  52. VERIFY_IS_APPROX((m2.template selfadjointView<Lower>().rankUpdate(m1.col(c).conjugate(),s1)._expression()),
  53. ((s1 * m1.col(c).conjugate() * m1.col(c).conjugate().adjoint()).eval().template triangularView<Lower>().toDenseMatrix()));
  54. m2.setZero();
  55. VERIFY_IS_APPROX((m2.template selfadjointView<Upper>().rankUpdate(m1.col(c).conjugate(),s1)._expression()),
  56. ((s1 * m1.col(c).conjugate() * m1.col(c).conjugate().adjoint()).eval().template triangularView<Upper>().toDenseMatrix()));
  57. m2.setZero();
  58. VERIFY_IS_APPROX((m2.template selfadjointView<Lower>().rankUpdate(m1.row(c),s1)._expression()),
  59. ((s1 * m1.row(c).transpose() * m1.row(c).transpose().adjoint()).eval().template triangularView<Lower>().toDenseMatrix()));
  60. m2.setZero();
  61. VERIFY_IS_APPROX((m2.template selfadjointView<Upper>().rankUpdate(m1.row(c).adjoint(),s1)._expression()),
  62. ((s1 * m1.row(c).adjoint() * m1.row(c).adjoint().adjoint()).eval().template triangularView<Upper>().toDenseMatrix()));
  63. }
  64. void test_product_syrk()
  65. {
  66. for(int i = 0; i < g_repeat ; i++)
  67. {
  68. int s;
  69. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE);
  70. CALL_SUBTEST_1( syrk(MatrixXf(s, s)) );
  71. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE);
  72. CALL_SUBTEST_2( syrk(MatrixXd(s, s)) );
  73. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2);
  74. CALL_SUBTEST_3( syrk(MatrixXcf(s, s)) );
  75. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2);
  76. CALL_SUBTEST_4( syrk(MatrixXcd(s, s)) );
  77. }
  78. }