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.

74 lines
2.5 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 bandmatrix(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,Dynamic,Dynamic> DenseMatrixType;
  16. Index rows = _m.rows();
  17. Index cols = _m.cols();
  18. Index supers = _m.supers();
  19. Index subs = _m.subs();
  20. MatrixType m(rows,cols,supers,subs);
  21. DenseMatrixType dm1(rows,cols);
  22. dm1.setZero();
  23. m.diagonal().setConstant(123);
  24. dm1.diagonal().setConstant(123);
  25. for (int i=1; i<=m.supers();++i)
  26. {
  27. m.diagonal(i).setConstant(static_cast<RealScalar>(i));
  28. dm1.diagonal(i).setConstant(static_cast<RealScalar>(i));
  29. }
  30. for (int i=1; i<=m.subs();++i)
  31. {
  32. m.diagonal(-i).setConstant(-static_cast<RealScalar>(i));
  33. dm1.diagonal(-i).setConstant(-static_cast<RealScalar>(i));
  34. }
  35. //std::cerr << m.m_data << "\n\n" << m.toDense() << "\n\n" << dm1 << "\n\n\n\n";
  36. VERIFY_IS_APPROX(dm1,m.toDenseMatrix());
  37. for (int i=0; i<cols; ++i)
  38. {
  39. m.col(i).setConstant(static_cast<RealScalar>(i+1));
  40. dm1.col(i).setConstant(static_cast<RealScalar>(i+1));
  41. }
  42. Index d = (std::min)(rows,cols);
  43. Index a = std::max<Index>(0,cols-d-supers);
  44. Index b = std::max<Index>(0,rows-d-subs);
  45. if(a>0) dm1.block(0,d+supers,rows,a).setZero();
  46. dm1.block(0,supers+1,cols-supers-1-a,cols-supers-1-a).template triangularView<Upper>().setZero();
  47. dm1.block(subs+1,0,rows-subs-1-b,rows-subs-1-b).template triangularView<Lower>().setZero();
  48. if(b>0) dm1.block(d+subs,0,b,cols).setZero();
  49. //std::cerr << m.m_data << "\n\n" << m.toDense() << "\n\n" << dm1 << "\n\n";
  50. VERIFY_IS_APPROX(dm1,m.toDenseMatrix());
  51. }
  52. using Eigen::internal::BandMatrix;
  53. void test_bandmatrix()
  54. {
  55. typedef BandMatrix<float>::Index Index;
  56. for(int i = 0; i < 10*g_repeat ; i++) {
  57. Index rows = internal::random<Index>(1,10);
  58. Index cols = internal::random<Index>(1,10);
  59. Index sups = internal::random<Index>(0,cols-1);
  60. Index subs = internal::random<Index>(0,rows-1);
  61. CALL_SUBTEST(bandmatrix(BandMatrix<float>(rows,cols,sups,subs)) );
  62. }
  63. }