spop_trimat_meat.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 spop_trimat
  16. //! @{
  17. template<typename T1>
  18. inline
  19. void
  20. spop_trimat::apply_noalias(SpMat<typename T1::elem_type>& out, const SpProxy<T1>& P, const bool upper)
  21. {
  22. arma_extra_debug_sigprint();
  23. typename SpProxy<T1>::const_iterator_type it = P.begin();
  24. const uword old_n_nonzero = P.get_n_nonzero();
  25. uword new_n_nonzero = 0;
  26. if(upper)
  27. {
  28. // upper triangular: count elements on the diagonal and above the diagonal
  29. for(uword i=0; i < old_n_nonzero; ++i)
  30. {
  31. new_n_nonzero += (it.row() <= it.col()) ? uword(1) : uword(0);
  32. ++it;
  33. }
  34. }
  35. else
  36. {
  37. // lower triangular: count elements on the diagonal and below the diagonal
  38. for(uword i=0; i < old_n_nonzero; ++i)
  39. {
  40. new_n_nonzero += (it.row() >= it.col()) ? uword(1) : uword(0);
  41. ++it;
  42. }
  43. }
  44. const uword n_rows = P.get_n_rows();
  45. const uword n_cols = P.get_n_cols();
  46. out.reserve(n_rows, n_cols, new_n_nonzero);
  47. uword new_index = 0;
  48. it = P.begin();
  49. if(upper)
  50. {
  51. // upper triangular: copy elements on the diagonal and above the diagonal
  52. for(uword i=0; i < old_n_nonzero; ++i)
  53. {
  54. const uword row = it.row();
  55. const uword col = it.col();
  56. if(row <= col)
  57. {
  58. access::rw(out.values[new_index]) = (*it);
  59. access::rw(out.row_indices[new_index]) = row;
  60. access::rw(out.col_ptrs[col + 1])++;
  61. ++new_index;
  62. }
  63. ++it;
  64. }
  65. }
  66. else
  67. {
  68. // lower triangular: copy elements on the diagonal and below the diagonal
  69. for(uword i=0; i < old_n_nonzero; ++i)
  70. {
  71. const uword row = it.row();
  72. const uword col = it.col();
  73. if(row >= col)
  74. {
  75. access::rw(out.values[new_index]) = (*it);
  76. access::rw(out.row_indices[new_index]) = row;
  77. access::rw(out.col_ptrs[col + 1])++;
  78. ++new_index;
  79. }
  80. ++it;
  81. }
  82. }
  83. for(uword i=0; i < n_cols; ++i)
  84. {
  85. access::rw(out.col_ptrs[i + 1]) += out.col_ptrs[i];
  86. }
  87. }
  88. template<typename T1>
  89. inline
  90. void
  91. spop_trimat::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_trimat>& in)
  92. {
  93. arma_extra_debug_sigprint();
  94. typedef typename T1::elem_type eT;
  95. const SpProxy<T1> P(in.m);
  96. arma_debug_check( (P.get_n_rows() != P.get_n_cols()), "trimatu()/trimatl(): given matrix must be square sized" );
  97. const bool upper = (in.aux_uword_a == 0);
  98. if(P.is_alias(out))
  99. {
  100. SpMat<eT> tmp;
  101. spop_trimat::apply_noalias(tmp, P, upper);
  102. out.steal_mem(tmp);
  103. }
  104. else
  105. {
  106. spop_trimat::apply_noalias(out, P, upper);
  107. }
  108. }
  109. //! @}