glue_histc_meat.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 glue_histc
  16. //! @{
  17. template<typename eT>
  18. inline
  19. void
  20. glue_histc::apply_noalias(Mat<uword>& C, const Mat<eT>& A, const Mat<eT>& B, const uword dim)
  21. {
  22. arma_extra_debug_sigprint();
  23. arma_debug_check( ((B.is_vec() == false) && (B.is_empty() == false)), "histc(): parameter 'edges' is not a vector" );
  24. const uword A_n_rows = A.n_rows;
  25. const uword A_n_cols = A.n_cols;
  26. const uword B_n_elem = B.n_elem;
  27. if( B_n_elem == uword(0) ) { C.reset(); return; }
  28. arma_debug_check
  29. (
  30. ((Col<eT>(const_cast<eT*>(B.memptr()), B_n_elem, false, false)).is_sorted("strictascend") == false),
  31. "hist(): given 'edges' vector does not contain monotonically increasing values"
  32. );
  33. const eT* B_mem = B.memptr();
  34. const uword B_n_elem_m1 = B_n_elem - 1;
  35. if(dim == uword(0))
  36. {
  37. C.zeros(B_n_elem, A_n_cols);
  38. for(uword col=0; col < A_n_cols; ++col)
  39. {
  40. const eT* A_coldata = A.colptr(col);
  41. uword* C_coldata = C.colptr(col);
  42. for(uword row=0; row < A_n_rows; ++row)
  43. {
  44. const eT x = A_coldata[row];
  45. for(uword i=0; i < B_n_elem_m1; ++i)
  46. {
  47. if( (B_mem[i] <= x) && (x < B_mem[i+1]) ) { C_coldata[i]++; break; }
  48. else if( B_mem[B_n_elem_m1] == x ) { C_coldata[B_n_elem_m1]++; break; } // for compatibility with Matlab
  49. }
  50. }
  51. }
  52. }
  53. else
  54. if(dim == uword(1))
  55. {
  56. C.zeros(A_n_rows, B_n_elem);
  57. if(A.n_rows == 1)
  58. {
  59. const uword A_n_elem = A.n_elem;
  60. const eT* A_mem = A.memptr();
  61. uword* C_mem = C.memptr();
  62. for(uword j=0; j < A_n_elem; ++j)
  63. {
  64. const eT x = A_mem[j];
  65. for(uword i=0; i < B_n_elem_m1; ++i)
  66. {
  67. if( (B_mem[i] <= x) && (x < B_mem[i+1]) ) { C_mem[i]++; break; }
  68. else if( B_mem[B_n_elem_m1] == x ) { C_mem[B_n_elem_m1]++; break; } // for compatibility with Matlab
  69. }
  70. }
  71. }
  72. else
  73. {
  74. for(uword row=0; row < A_n_rows; ++row)
  75. for(uword col=0; col < A_n_cols; ++col)
  76. {
  77. const eT x = A.at(row,col);
  78. for(uword i=0; i < B_n_elem_m1; ++i)
  79. {
  80. if( (B_mem[i] <= x) && (x < B_mem[i+1]) ) { C.at(row,i)++; break; }
  81. else if( B_mem[B_n_elem_m1] == x ) { C.at(row,B_n_elem_m1)++; break; } // for compatibility with Matlab
  82. }
  83. }
  84. }
  85. }
  86. }
  87. template<typename T1, typename T2>
  88. inline
  89. void
  90. glue_histc::apply(Mat<uword>& C, const mtGlue<uword,T1,T2,glue_histc>& expr)
  91. {
  92. arma_extra_debug_sigprint();
  93. const uword dim = expr.aux_uword;
  94. arma_debug_check( (dim > 1), "histc(): parameter 'dim' must be 0 or 1" );
  95. const quasi_unwrap<T1> UA(expr.A);
  96. const quasi_unwrap<T2> UB(expr.B);
  97. if(UA.is_alias(C) || UB.is_alias(C))
  98. {
  99. Mat<uword> tmp;
  100. glue_histc::apply_noalias(tmp, UA.M, UB.M, dim);
  101. C.steal_mem(tmp);
  102. }
  103. else
  104. {
  105. glue_histc::apply_noalias(C, UA.M, UB.M, dim);
  106. }
  107. }
  108. template<typename T1, typename T2>
  109. inline
  110. void
  111. glue_histc_default::apply(Mat<uword>& C, const mtGlue<uword,T1,T2,glue_histc_default>& expr)
  112. {
  113. arma_extra_debug_sigprint();
  114. const quasi_unwrap<T1> UA(expr.A);
  115. const quasi_unwrap<T2> UB(expr.B);
  116. const uword dim = (T1::is_xvec) ? uword(UA.M.is_rowvec() ? 1 : 0) : uword((T1::is_row) ? 1 : 0);
  117. if(UA.is_alias(C) || UB.is_alias(C))
  118. {
  119. Mat<uword> tmp;
  120. glue_histc::apply_noalias(tmp, UA.M, UB.M, dim);
  121. C.steal_mem(tmp);
  122. }
  123. else
  124. {
  125. glue_histc::apply_noalias(C, UA.M, UB.M, dim);
  126. }
  127. }
  128. //! @}