fn_clamp.hpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 fn_clamp
  16. //! @{
  17. template<typename T1>
  18. arma_warn_unused
  19. inline
  20. typename
  21. enable_if2
  22. <
  23. is_arma_type<T1>::value && is_cx<typename T1::elem_type>::no,
  24. const mtOp<typename T1::elem_type, T1, op_clamp>
  25. >::result
  26. clamp(const T1& X, const typename T1::elem_type min_val, const typename T1::elem_type max_val)
  27. {
  28. arma_extra_debug_sigprint();
  29. arma_debug_check( (min_val > max_val), "clamp(): min_val must be less than max_val" );
  30. return mtOp<typename T1::elem_type, T1, op_clamp>(mtOp_dual_aux_indicator(), X, min_val, max_val);
  31. }
  32. template<typename T1>
  33. arma_warn_unused
  34. inline
  35. const mtOpCube<typename T1::elem_type, T1, op_clamp>
  36. clamp(const BaseCube<typename T1::elem_type,T1>& X, const typename T1::elem_type min_val, const typename T1::elem_type max_val, typename arma_not_cx<typename T1::elem_type>::result* junk = 0)
  37. {
  38. arma_extra_debug_sigprint();
  39. arma_ignore(junk);
  40. arma_debug_check( (min_val > max_val), "clamp(): min_val must be less than max_val" );
  41. return mtOpCube<typename T1::elem_type, T1, op_clamp>(mtOpCube_dual_aux_indicator(), X.get_ref(), min_val, max_val);
  42. }
  43. template<typename T1>
  44. arma_warn_unused
  45. inline
  46. typename
  47. enable_if2
  48. <
  49. is_cx<typename T1::elem_type>::no,
  50. SpMat<typename T1::elem_type>
  51. >::result
  52. clamp(const SpBase<typename T1::elem_type,T1>& X, const typename T1::elem_type min_val, const typename T1::elem_type max_val)
  53. {
  54. arma_extra_debug_sigprint();
  55. typedef typename T1::elem_type eT;
  56. arma_debug_check( (min_val > max_val), "clamp(): min_val must be less than max_val" );
  57. SpMat<eT> out = X.get_ref();
  58. out.sync();
  59. const uword N = out.n_nonzero;
  60. eT* out_values = access::rwp(out.values);
  61. for(uword i=0; i<N; ++i)
  62. {
  63. eT& out_val = out_values[i];
  64. out_val = (out_val < min_val) ? min_val : ( (out_val > max_val) ? max_val : out_val );
  65. }
  66. if( (min_val == eT(0)) || (max_val == eT(0)) ) { out.remove_zeros(); }
  67. return out;
  68. }
  69. //! @}