op_stddev_meat.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_stddev
  16. //! @{
  17. //! \brief
  18. //! For each row or for each column, find the standard deviation.
  19. //! The result is stored in a dense matrix that has either one column or one row.
  20. //! The dimension for which the standard deviations are found is set via the stddev() function.
  21. template<typename T1>
  22. inline
  23. void
  24. op_stddev::apply(Mat<typename T1::pod_type>& out, const mtOp<typename T1::pod_type, T1, op_stddev>& in)
  25. {
  26. arma_extra_debug_sigprint();
  27. typedef typename T1::elem_type in_eT;
  28. typedef typename T1::pod_type out_eT;
  29. const unwrap_check_mixed<T1> tmp(in.m, out);
  30. const Mat<in_eT>& X = tmp.M;
  31. const uword norm_type = in.aux_uword_a;
  32. const uword dim = in.aux_uword_b;
  33. arma_debug_check( (norm_type > 1), "stddev(): parameter 'norm_type' must be 0 or 1" );
  34. arma_debug_check( (dim > 1), "stddev(): parameter 'dim' must be 0 or 1" );
  35. const uword X_n_rows = X.n_rows;
  36. const uword X_n_cols = X.n_cols;
  37. if(dim == 0)
  38. {
  39. arma_extra_debug_print("op_stddev::apply(): dim = 0");
  40. out.set_size((X_n_rows > 0) ? 1 : 0, X_n_cols);
  41. if(X_n_rows > 0)
  42. {
  43. out_eT* out_mem = out.memptr();
  44. for(uword col=0; col<X_n_cols; ++col)
  45. {
  46. out_mem[col] = std::sqrt( op_var::direct_var( X.colptr(col), X_n_rows, norm_type ) );
  47. }
  48. }
  49. }
  50. else
  51. if(dim == 1)
  52. {
  53. arma_extra_debug_print("op_stddev::apply(): dim = 1");
  54. out.set_size(X_n_rows, (X_n_cols > 0) ? 1 : 0);
  55. if(X_n_cols > 0)
  56. {
  57. podarray<in_eT> dat(X_n_cols);
  58. in_eT* dat_mem = dat.memptr();
  59. out_eT* out_mem = out.memptr();
  60. for(uword row=0; row<X_n_rows; ++row)
  61. {
  62. dat.copy_row(X, row);
  63. out_mem[row] = std::sqrt( op_var::direct_var( dat_mem, X_n_cols, norm_type) );
  64. }
  65. }
  66. }
  67. }
  68. //! @}