op_hist_meat.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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_hist
  16. //! @{
  17. template<typename eT>
  18. inline
  19. void
  20. op_hist::apply_noalias(Mat<uword>& out, const Mat<eT>& A, const uword n_bins, const uword dim)
  21. {
  22. arma_extra_debug_sigprint();
  23. arma_debug_check( ((A.is_vec() == false) && (A.is_empty() == false)), "hist(): only vectors are supported when automatically determining bin centers" );
  24. if(n_bins == 0) { out.reset(); return; }
  25. uword A_n_elem = A.n_elem;
  26. const eT* A_mem = A.memptr();
  27. eT min_val = priv::most_pos<eT>();
  28. eT max_val = priv::most_neg<eT>();
  29. uword i,j;
  30. for(i=0, j=1; j < A_n_elem; i+=2, j+=2)
  31. {
  32. const eT val_i = A_mem[i];
  33. const eT val_j = A_mem[j];
  34. if(min_val > val_i) { min_val = val_i; }
  35. if(min_val > val_j) { min_val = val_j; }
  36. if(max_val < val_i) { max_val = val_i; }
  37. if(max_val < val_j) { max_val = val_j; }
  38. }
  39. if(i < A_n_elem)
  40. {
  41. const eT val_i = A_mem[i];
  42. if(min_val > val_i) { min_val = val_i; }
  43. if(max_val < val_i) { max_val = val_i; }
  44. }
  45. if(min_val == max_val)
  46. {
  47. min_val -= (n_bins/2);
  48. max_val += (n_bins/2);
  49. }
  50. if(arma_isfinite(min_val) == false) { min_val = priv::most_neg<eT>(); }
  51. if(arma_isfinite(max_val) == false) { max_val = priv::most_pos<eT>(); }
  52. Col<eT> c(n_bins);
  53. eT* c_mem = c.memptr();
  54. for(uword ii=0; ii < n_bins; ++ii)
  55. {
  56. c_mem[ii] = (0.5 + ii) / double(n_bins);
  57. }
  58. c = ((max_val - min_val) * c) + min_val;
  59. glue_hist::apply_noalias(out, A, c, dim);
  60. }
  61. template<typename T1>
  62. inline
  63. void
  64. op_hist::apply(Mat<uword>& out, const mtOp<uword, T1, op_hist>& X)
  65. {
  66. arma_extra_debug_sigprint();
  67. const uword n_bins = X.aux_uword_a;
  68. const quasi_unwrap<T1> U(X.m);
  69. const uword dim = (T1::is_xvec) ? uword(U.M.is_rowvec() ? 1 : 0) : uword((T1::is_row) ? 1 : 0);
  70. if(is_non_integral<typename T1::elem_type>::value)
  71. {
  72. if(U.is_alias(out))
  73. {
  74. Mat<uword> tmp;
  75. op_hist::apply_noalias(tmp, U.M, n_bins, dim);
  76. out.steal_mem(tmp);
  77. }
  78. else
  79. {
  80. op_hist::apply_noalias(out, U.M, n_bins, dim);
  81. }
  82. }
  83. else
  84. {
  85. Mat<double> converted = conv_to< Mat<double> >::from(U.M);
  86. op_hist::apply_noalias(out, converted, n_bins, dim);
  87. }
  88. }
  89. //! @}