op_range_meat.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_range
  16. //! @{
  17. template<typename T1>
  18. inline
  19. void
  20. op_range::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_range>& in)
  21. {
  22. arma_extra_debug_sigprint();
  23. typedef typename T1::elem_type eT;
  24. const uword dim = in.aux_uword_a;
  25. arma_debug_check( (dim > 1), "range(): parameter 'dim' must be 0 or 1");
  26. const quasi_unwrap<T1> U(in.m);
  27. const Mat<eT>& X = U.M;
  28. if(U.is_alias(out) == false)
  29. {
  30. op_range::apply_noalias(out, X, dim);
  31. }
  32. else
  33. {
  34. Mat<eT> tmp;
  35. op_range::apply_noalias(tmp, X, dim);
  36. out.steal_mem(tmp);
  37. }
  38. }
  39. template<typename eT>
  40. inline
  41. void
  42. op_range::apply_noalias(Mat<eT>& out, const Mat<eT>& X, const uword dim)
  43. {
  44. arma_extra_debug_sigprint();
  45. // TODO: replace with dedicated implementation which finds min and max at the same time
  46. out = max(X,dim) - min(X,dim);
  47. }
  48. template<typename T1>
  49. inline
  50. typename T1::elem_type
  51. op_range::vector_range(const T1& expr)
  52. {
  53. arma_extra_debug_sigprint();
  54. typedef typename T1::elem_type eT;
  55. const quasi_unwrap<T1> U(expr);
  56. const Mat<eT>& X = U.M;
  57. const eT* X_mem = X.memptr();
  58. const uword N = X.n_elem;
  59. if(N == 0)
  60. {
  61. arma_debug_check(true, "range(): object has no elements");
  62. return Datum<eT>::nan;
  63. }
  64. // TODO: replace with dedicated implementation which finds min and max at the same time
  65. return op_max::direct_max(X_mem, N) - op_min::direct_min(X_mem, N);
  66. }
  67. //! @}