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.

97 lines
3.5 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2011 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. #ifndef EIGEN_BAND_TRIANGULARSOLVER_H
  10. #define EIGEN_BAND_TRIANGULARSOLVER_H
  11. namespace internal {
  12. /* \internal
  13. * Solve Ax=b with A a band triangular matrix
  14. * TODO: extend it to matrices for x abd b */
  15. template<typename Index, int Mode, typename LhsScalar, bool ConjLhs, typename RhsScalar, int StorageOrder>
  16. struct band_solve_triangular_selector;
  17. template<typename Index, int Mode, typename LhsScalar, bool ConjLhs, typename RhsScalar>
  18. struct band_solve_triangular_selector<Index,Mode,LhsScalar,ConjLhs,RhsScalar,RowMajor>
  19. {
  20. typedef Map<const Matrix<LhsScalar,Dynamic,Dynamic,RowMajor>, 0, OuterStride<> > LhsMap;
  21. typedef Map<Matrix<RhsScalar,Dynamic,1> > RhsMap;
  22. enum { IsLower = (Mode&Lower) ? 1 : 0 };
  23. static void run(Index size, Index k, const LhsScalar* _lhs, Index lhsStride, RhsScalar* _other)
  24. {
  25. const LhsMap lhs(_lhs,size,k+1,OuterStride<>(lhsStride));
  26. RhsMap other(_other,size,1);
  27. typename internal::conditional<
  28. ConjLhs,
  29. const CwiseUnaryOp<typename internal::scalar_conjugate_op<LhsScalar>,LhsMap>,
  30. const LhsMap&>
  31. ::type cjLhs(lhs);
  32. for(int col=0 ; col<other.cols() ; ++col)
  33. {
  34. for(int ii=0; ii<size; ++ii)
  35. {
  36. int i = IsLower ? ii : size-ii-1;
  37. int actual_k = (std::min)(k,ii);
  38. int actual_start = IsLower ? k-actual_k : 1;
  39. if(actual_k>0)
  40. other.coeffRef(i,col) -= cjLhs.row(i).segment(actual_start,actual_k).transpose()
  41. .cwiseProduct(other.col(col).segment(IsLower ? i-actual_k : i+1,actual_k)).sum();
  42. if((Mode&UnitDiag)==0)
  43. other.coeffRef(i,col) /= cjLhs(i,IsLower ? k : 0);
  44. }
  45. }
  46. }
  47. };
  48. template<typename Index, int Mode, typename LhsScalar, bool ConjLhs, typename RhsScalar>
  49. struct band_solve_triangular_selector<Index,Mode,LhsScalar,ConjLhs,RhsScalar,ColMajor>
  50. {
  51. typedef Map<const Matrix<LhsScalar,Dynamic,Dynamic,ColMajor>, 0, OuterStride<> > LhsMap;
  52. typedef Map<Matrix<RhsScalar,Dynamic,1> > RhsMap;
  53. enum { IsLower = (Mode&Lower) ? 1 : 0 };
  54. static void run(Index size, Index k, const LhsScalar* _lhs, Index lhsStride, RhsScalar* _other)
  55. {
  56. const LhsMap lhs(_lhs,k+1,size,OuterStride<>(lhsStride));
  57. RhsMap other(_other,size,1);
  58. typename internal::conditional<
  59. ConjLhs,
  60. const CwiseUnaryOp<typename internal::scalar_conjugate_op<LhsScalar>,LhsMap>,
  61. const LhsMap&>
  62. ::type cjLhs(lhs);
  63. for(int col=0 ; col<other.cols() ; ++col)
  64. {
  65. for(int ii=0; ii<size; ++ii)
  66. {
  67. int i = IsLower ? ii : size-ii-1;
  68. int actual_k = (std::min)(k,size-ii-1);
  69. int actual_start = IsLower ? 1 : k-actual_k;
  70. if((Mode&UnitDiag)==0)
  71. other.coeffRef(i,col) /= cjLhs(IsLower ? 0 : k, i);
  72. if(actual_k>0)
  73. other.col(col).segment(IsLower ? i+1 : i-actual_k, actual_k)
  74. -= other.coeff(i,col) * cjLhs.col(i).segment(actual_start,actual_k);
  75. }
  76. }
  77. }
  78. };
  79. } // end namespace internal
  80. #endif // EIGEN_BAND_TRIANGULARSOLVER_H