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.

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