fn_diagmat.hpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
  2. // Copyright 2008-2016 National ICT Australia (NICTA)
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // ------------------------------------------------------------------------
  15. //! \addtogroup fn_diagmat
  16. //! @{
  17. //! interpret a matrix or a vector as a diagonal matrix (i.e. off-diagonal entries are zero)
  18. template<typename T1>
  19. arma_warn_unused
  20. arma_inline
  21. typename
  22. enable_if2
  23. <
  24. is_arma_type<T1>::value,
  25. const Op<T1, op_diagmat>
  26. >::result
  27. diagmat(const T1& X)
  28. {
  29. arma_extra_debug_sigprint();
  30. return Op<T1, op_diagmat>(X);
  31. }
  32. //! create a matrix with the k-th diagonal set to the given vector
  33. template<typename T1>
  34. arma_warn_unused
  35. arma_inline
  36. typename
  37. enable_if2
  38. <
  39. is_arma_type<T1>::value,
  40. const Op<T1, op_diagmat2>
  41. >::result
  42. diagmat(const T1& X, const sword k)
  43. {
  44. arma_extra_debug_sigprint();
  45. const uword row_offset = (k < 0) ? uword(-k) : uword(0);
  46. const uword col_offset = (k > 0) ? uword( k) : uword(0);
  47. return Op<T1, op_diagmat2>(X, row_offset, col_offset);
  48. }
  49. template<typename T1>
  50. arma_warn_unused
  51. inline
  52. const SpOp<T1, spop_diagmat>
  53. diagmat(const SpBase<typename T1::elem_type,T1>& X)
  54. {
  55. arma_extra_debug_sigprint();
  56. return SpOp<T1, spop_diagmat>(X.get_ref());
  57. }
  58. template<typename T1>
  59. arma_warn_unused
  60. inline
  61. const SpOp<T1, spop_diagmat2>
  62. diagmat(const SpBase<typename T1::elem_type,T1>& X, const sword k)
  63. {
  64. arma_extra_debug_sigprint();
  65. const uword row_offset = (k < 0) ? uword(-k) : uword(0);
  66. const uword col_offset = (k > 0) ? uword( k) : uword(0);
  67. return SpOp<T1, spop_diagmat2>(X.get_ref(), row_offset, col_offset);
  68. }
  69. //! @}