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.

89 lines
4.2 KiB

  1. // This file is triangularView 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 trmv(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. RealScalar largerEps = 10*test_precision<RealScalar>();
  17. Index rows = m.rows();
  18. Index cols = m.cols();
  19. MatrixType m1 = MatrixType::Random(rows, cols),
  20. m3(rows, cols);
  21. VectorType v1 = VectorType::Random(rows);
  22. Scalar s1 = internal::random<Scalar>();
  23. m1 = MatrixType::Random(rows, cols);
  24. // check with a column-major matrix
  25. m3 = m1.template triangularView<Eigen::Lower>();
  26. VERIFY((m3 * v1).isApprox(m1.template triangularView<Eigen::Lower>() * v1, largerEps));
  27. m3 = m1.template triangularView<Eigen::Upper>();
  28. VERIFY((m3 * v1).isApprox(m1.template triangularView<Eigen::Upper>() * v1, largerEps));
  29. m3 = m1.template triangularView<Eigen::UnitLower>();
  30. VERIFY((m3 * v1).isApprox(m1.template triangularView<Eigen::UnitLower>() * v1, largerEps));
  31. m3 = m1.template triangularView<Eigen::UnitUpper>();
  32. VERIFY((m3 * v1).isApprox(m1.template triangularView<Eigen::UnitUpper>() * v1, largerEps));
  33. // check conjugated and scalar multiple expressions (col-major)
  34. m3 = m1.template triangularView<Eigen::Lower>();
  35. VERIFY(((s1*m3).conjugate() * v1).isApprox((s1*m1).conjugate().template triangularView<Eigen::Lower>() * v1, largerEps));
  36. m3 = m1.template triangularView<Eigen::Upper>();
  37. VERIFY((m3.conjugate() * v1.conjugate()).isApprox(m1.conjugate().template triangularView<Eigen::Upper>() * v1.conjugate(), largerEps));
  38. // check with a row-major matrix
  39. m3 = m1.template triangularView<Eigen::Upper>();
  40. VERIFY((m3.transpose() * v1).isApprox(m1.transpose().template triangularView<Eigen::Lower>() * v1, largerEps));
  41. m3 = m1.template triangularView<Eigen::Lower>();
  42. VERIFY((m3.transpose() * v1).isApprox(m1.transpose().template triangularView<Eigen::Upper>() * v1, largerEps));
  43. m3 = m1.template triangularView<Eigen::UnitUpper>();
  44. VERIFY((m3.transpose() * v1).isApprox(m1.transpose().template triangularView<Eigen::UnitLower>() * v1, largerEps));
  45. m3 = m1.template triangularView<Eigen::UnitLower>();
  46. VERIFY((m3.transpose() * v1).isApprox(m1.transpose().template triangularView<Eigen::UnitUpper>() * v1, largerEps));
  47. // check conjugated and scalar multiple expressions (row-major)
  48. m3 = m1.template triangularView<Eigen::Upper>();
  49. VERIFY((m3.adjoint() * v1).isApprox(m1.adjoint().template triangularView<Eigen::Lower>() * v1, largerEps));
  50. m3 = m1.template triangularView<Eigen::Lower>();
  51. VERIFY((m3.adjoint() * (s1*v1.conjugate())).isApprox(m1.adjoint().template triangularView<Eigen::Upper>() * (s1*v1.conjugate()), largerEps));
  52. m3 = m1.template triangularView<Eigen::UnitUpper>();
  53. // check transposed cases:
  54. m3 = m1.template triangularView<Eigen::Lower>();
  55. VERIFY((v1.transpose() * m3).isApprox(v1.transpose() * m1.template triangularView<Eigen::Lower>(), largerEps));
  56. VERIFY((v1.adjoint() * m3).isApprox(v1.adjoint() * m1.template triangularView<Eigen::Lower>(), largerEps));
  57. VERIFY((v1.adjoint() * m3.adjoint()).isApprox(v1.adjoint() * m1.template triangularView<Eigen::Lower>().adjoint(), largerEps));
  58. // TODO check with sub-matrices
  59. }
  60. void test_product_trmv()
  61. {
  62. int s;
  63. for(int i = 0; i < g_repeat ; i++) {
  64. CALL_SUBTEST_1( trmv(Matrix<float, 1, 1>()) );
  65. CALL_SUBTEST_2( trmv(Matrix<float, 2, 2>()) );
  66. CALL_SUBTEST_3( trmv(Matrix3d()) );
  67. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2);
  68. CALL_SUBTEST_4( trmv(MatrixXcf(s,s)) );
  69. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2);
  70. CALL_SUBTEST_5( trmv(MatrixXcd(s,s)) );
  71. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE);
  72. CALL_SUBTEST_6( trmv(Matrix<float,Dynamic,Dynamic,RowMajor>(s, s)) );
  73. }
  74. EIGEN_UNUSED_VARIABLE(s);
  75. }