fn_normalise.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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_normalise
  16. //! @{
  17. template<typename T1>
  18. arma_warn_unused
  19. inline
  20. typename
  21. enable_if2
  22. <
  23. is_arma_type<T1>::value && resolves_to_vector<T1>::yes,
  24. const Op<T1, op_normalise_vec>
  25. >::result
  26. normalise
  27. (
  28. const T1& X,
  29. const uword p = uword(2),
  30. const arma_empty_class junk1 = arma_empty_class(),
  31. const typename arma_real_or_cx_only<typename T1::elem_type>::result* junk2 = 0
  32. )
  33. {
  34. arma_extra_debug_sigprint();
  35. arma_ignore(junk1);
  36. arma_ignore(junk2);
  37. return Op<T1, op_normalise_vec>(X, p, 0);
  38. }
  39. template<typename T1>
  40. arma_warn_unused
  41. inline
  42. typename
  43. enable_if2
  44. <
  45. is_arma_type<T1>::value && resolves_to_vector<T1>::no,
  46. const Op<T1, op_normalise_mat>
  47. >::result
  48. normalise
  49. (
  50. const T1& X,
  51. const uword p = uword(2),
  52. const uword dim = 0,
  53. const typename arma_real_or_cx_only<typename T1::elem_type>::result* junk = 0
  54. )
  55. {
  56. arma_extra_debug_sigprint();
  57. arma_ignore(junk);
  58. return Op<T1, op_normalise_mat>(X, p, dim);
  59. }
  60. template<typename T1>
  61. arma_warn_unused
  62. inline
  63. const SpOp<T1, spop_normalise>
  64. normalise
  65. (
  66. const SpBase<typename T1::elem_type, T1>& expr,
  67. const uword p = uword(2),
  68. const uword dim = 0,
  69. const typename arma_real_or_cx_only<typename T1::elem_type>::result* junk = 0
  70. )
  71. {
  72. arma_extra_debug_sigprint();
  73. arma_ignore(junk);
  74. return SpOp<T1, spop_normalise>(expr.get_ref(), p, dim);
  75. }
  76. //! for compatibility purposes: allows compiling user code designed for earlier versions of Armadillo
  77. template<typename T>
  78. arma_warn_unused
  79. arma_inline
  80. typename
  81. enable_if2
  82. <
  83. is_supported_blas_type<T>::value,
  84. Col<T>
  85. >::result
  86. normalise(const T& val)
  87. {
  88. Col<T> out(1);
  89. out[0] = (val != T(0)) ? T(val / (std::abs)(val)) : T(val);
  90. return out;
  91. }
  92. //! @}