spglue_min_meat.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 spglue_min
  16. //! @{
  17. template<typename T1, typename T2>
  18. inline
  19. void
  20. spglue_min::apply(SpMat<typename T1::elem_type>& out, const SpGlue<T1,T2,spglue_min>& X)
  21. {
  22. arma_extra_debug_sigprint();
  23. typedef typename T1::elem_type eT;
  24. const SpProxy<T1> pa(X.A);
  25. const SpProxy<T2> pb(X.B);
  26. const bool is_alias = pa.is_alias(out) || pb.is_alias(out);
  27. if(is_alias == false)
  28. {
  29. spglue_min::apply_noalias(out, pa, pb);
  30. }
  31. else
  32. {
  33. SpMat<eT> tmp;
  34. spglue_min::apply_noalias(tmp, pa, pb);
  35. out.steal_mem(tmp);
  36. }
  37. }
  38. template<typename eT, typename T1, typename T2>
  39. inline
  40. void
  41. spglue_min::apply_noalias(SpMat<eT>& out, const SpProxy<T1>& pa, const SpProxy<T2>& pb)
  42. {
  43. arma_extra_debug_sigprint();
  44. arma_debug_assert_same_size(pa.get_n_rows(), pa.get_n_cols(), pb.get_n_rows(), pb.get_n_cols(), "element-wise minimum");
  45. // The plus helper works here also to get an upper bound on n_nonzero.
  46. const uword max_n_nonzero = spglue_elem_helper::max_n_nonzero_plus(pa, pb);
  47. // Resize memory to upper bound
  48. out.reserve(pa.get_n_rows(), pa.get_n_cols(), max_n_nonzero);
  49. // Now iterate across both matrices.
  50. typename SpProxy<T1>::const_iterator_type x_it = pa.begin();
  51. typename SpProxy<T1>::const_iterator_type x_end = pa.end();
  52. typename SpProxy<T2>::const_iterator_type y_it = pb.begin();
  53. typename SpProxy<T2>::const_iterator_type y_end = pb.end();
  54. uword count = 0;
  55. while( (x_it != x_end) || (y_it != y_end) )
  56. {
  57. eT out_val;
  58. const uword x_it_col = x_it.col();
  59. const uword x_it_row = x_it.row();
  60. const uword y_it_col = y_it.col();
  61. const uword y_it_row = y_it.row();
  62. bool use_y_loc = false;
  63. if(x_it == y_it)
  64. {
  65. out_val = elem_min(eT(*x_it), eT(*y_it));
  66. ++x_it;
  67. ++y_it;
  68. }
  69. else
  70. {
  71. if((x_it_col < y_it_col) || ((x_it_col == y_it_col) && (x_it_row < y_it_row))) // if y is closer to the end
  72. {
  73. out_val = elem_min(eT(*x_it), eT(0));
  74. ++x_it;
  75. }
  76. else
  77. {
  78. out_val = elem_min(eT(*y_it), eT(0));
  79. ++y_it;
  80. use_y_loc = true;
  81. }
  82. }
  83. if(out_val != eT(0))
  84. {
  85. access::rw(out.values[count]) = out_val;
  86. const uword out_row = (use_y_loc == false) ? x_it_row : y_it_row;
  87. const uword out_col = (use_y_loc == false) ? x_it_col : y_it_col;
  88. access::rw(out.row_indices[count]) = out_row;
  89. access::rw(out.col_ptrs[out_col + 1])++;
  90. ++count;
  91. }
  92. }
  93. const uword out_n_cols = out.n_cols;
  94. uword* col_ptrs = access::rwp(out.col_ptrs);
  95. // Fix column pointers to be cumulative.
  96. for(uword c = 1; c <= out_n_cols; ++c)
  97. {
  98. col_ptrs[c] += col_ptrs[c - 1];
  99. }
  100. if(count < max_n_nonzero)
  101. {
  102. if(count <= (max_n_nonzero/2))
  103. {
  104. out.mem_resize(count);
  105. }
  106. else
  107. {
  108. // quick resize without reallocating memory and copying data
  109. access::rw( out.n_nonzero) = count;
  110. access::rw( out.values[count]) = eT(0);
  111. access::rw(out.row_indices[count]) = uword(0);
  112. }
  113. }
  114. }
  115. template<typename eT>
  116. inline
  117. void
  118. spglue_min::apply_noalias(SpMat<eT>& out, const SpMat<eT>& A, const SpMat<eT>& B)
  119. {
  120. arma_extra_debug_sigprint();
  121. const SpProxy< SpMat<eT> > pa(A);
  122. const SpProxy< SpMat<eT> > pb(B);
  123. spglue_min::apply_noalias(out, pa, pb);
  124. }
  125. template<typename eT, typename T1, typename T2>
  126. inline
  127. void
  128. spglue_min::dense_sparse_min(Mat<eT>& out, const Base<eT,T1>& X, const SpBase<eT,T2>& Y)
  129. {
  130. arma_extra_debug_sigprint();
  131. // NOTE: this function assumes there is no aliasing between matrix 'out' and X
  132. const Proxy<T1> pa(X.get_ref());
  133. const SpProxy<T2> pb(Y.get_ref());
  134. const uword n_rows = pa.get_n_rows();
  135. const uword n_cols = pa.get_n_cols();
  136. arma_debug_assert_same_size( n_rows, n_cols, pb.get_n_rows(), pb.get_n_cols(), "element-wise minimum" );
  137. out.set_size(n_rows, n_cols);
  138. for(uword c=0; c < n_cols; ++c)
  139. for(uword r=0; r < n_rows; ++r)
  140. {
  141. out.at(r,c) = elem_min(pa.at(r,c), pb.at(r,c));
  142. }
  143. }
  144. // min of non-complex elements
  145. template<typename eT>
  146. inline
  147. typename enable_if2<is_cx<eT>::no, eT>::result
  148. spglue_min::elem_min(const eT& a, const eT& b)
  149. {
  150. return (std::min)(a, b);
  151. }
  152. // min of complex elements
  153. template<typename eT>
  154. inline
  155. typename enable_if2<is_cx<eT>::yes, eT>::result
  156. spglue_min::elem_min(const eT& a, const eT& b)
  157. {
  158. return (std::abs(a) < std::abs(b)) ? a : b;
  159. }
  160. //! @}