op_nonzeros_meat.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 op_nonzeros
  16. //! @{
  17. template<typename T1>
  18. inline
  19. void
  20. op_nonzeros::apply_noalias(Mat<typename T1::elem_type>& out, const Proxy<T1>& P)
  21. {
  22. arma_extra_debug_sigprint();
  23. typedef typename T1::elem_type eT;
  24. const uword N_max = P.get_n_elem();
  25. Mat<eT> tmp(N_max, 1);
  26. eT* tmp_mem = tmp.memptr();
  27. uword N_nz = 0;
  28. if(Proxy<T1>::use_at == false)
  29. {
  30. typename Proxy<T1>::ea_type Pea = P.get_ea();
  31. for(uword i=0; i<N_max; ++i)
  32. {
  33. const eT val = Pea[i];
  34. if(val != eT(0)) { tmp_mem[N_nz] = val; ++N_nz; }
  35. }
  36. }
  37. else
  38. {
  39. const uword n_rows = P.get_n_rows();
  40. const uword n_cols = P.get_n_cols();
  41. for(uword col=0; col < n_cols; ++col)
  42. for(uword row=0; row < n_rows; ++row)
  43. {
  44. const eT val = P.at(row,col);
  45. if(val != eT(0)) { tmp_mem[N_nz] = val; ++N_nz; }
  46. }
  47. }
  48. out.steal_mem_col(tmp, N_nz);
  49. }
  50. template<typename T1>
  51. inline
  52. void
  53. op_nonzeros::apply(Mat<typename T1::elem_type>& out, const Op<T1, op_nonzeros>& X)
  54. {
  55. arma_extra_debug_sigprint();
  56. typedef typename T1::elem_type eT;
  57. const Proxy<T1> P(X.m);
  58. if(P.get_n_elem() == 0) { out.set_size(0,1); return; }
  59. if(P.is_alias(out))
  60. {
  61. Mat<eT> out2;
  62. op_nonzeros::apply_noalias(out2, P);
  63. out.steal_mem(out2);
  64. }
  65. else
  66. {
  67. op_nonzeros::apply_noalias(out, P);
  68. }
  69. }
  70. template<typename T1>
  71. inline
  72. void
  73. op_nonzeros_spmat::apply(Mat<typename T1::elem_type>& out, const SpToDOp<T1, op_nonzeros_spmat>& X)
  74. {
  75. arma_extra_debug_sigprint();
  76. typedef typename T1::elem_type eT;
  77. const SpProxy<T1> P(X.m);
  78. const uword N = P.get_n_nonzero();
  79. out.set_size(N,1);
  80. if(N > 0)
  81. {
  82. if(is_SpMat<typename SpProxy<T1>::stored_type>::value)
  83. {
  84. const unwrap_spmat<typename SpProxy<T1>::stored_type> U(P.Q);
  85. arrayops::copy(out.memptr(), U.M.values, N);
  86. }
  87. else
  88. {
  89. eT* out_mem = out.memptr();
  90. typename SpProxy<T1>::const_iterator_type it = P.begin();
  91. for(uword i=0; i<N; ++i) { out_mem[i] = (*it); ++it; }
  92. }
  93. }
  94. }
  95. //! @}