max@0
|
1 // Copyright (C) 2009-2011 NICTA (www.nicta.com.au)
|
max@0
|
2 // Copyright (C) 2009-2011 Conrad Sanderson
|
max@0
|
3 //
|
max@0
|
4 // This file is part of the Armadillo C++ library.
|
max@0
|
5 // It is provided without any warranty of fitness
|
max@0
|
6 // for any purpose. You can redistribute this file
|
max@0
|
7 // and/or modify it under the terms of the GNU
|
max@0
|
8 // Lesser General Public License (LGPL) as published
|
max@0
|
9 // by the Free Software Foundation, either version 3
|
max@0
|
10 // of the License or (at your option) any later version.
|
max@0
|
11 // (see http://www.opensource.org/licenses for more info)
|
max@0
|
12
|
max@0
|
13
|
max@0
|
14 //! \addtogroup op_stddev
|
max@0
|
15 //! @{
|
max@0
|
16
|
max@0
|
17
|
max@0
|
18 //! \brief
|
max@0
|
19 //! For each row or for each column, find the standard deviation.
|
max@0
|
20 //! The result is stored in a dense matrix that has either one column or one row.
|
max@0
|
21 //! The dimension for which the standard deviations are found is set via the stddev() function.
|
max@0
|
22 template<typename T1>
|
max@0
|
23 inline
|
max@0
|
24 void
|
max@0
|
25 op_stddev::apply(Mat<typename T1::pod_type>& out, const mtOp<typename T1::pod_type, T1, op_stddev>& in)
|
max@0
|
26 {
|
max@0
|
27 arma_extra_debug_sigprint();
|
max@0
|
28
|
max@0
|
29 typedef typename T1::elem_type in_eT;
|
max@0
|
30 typedef typename T1::pod_type out_eT;
|
max@0
|
31
|
max@0
|
32 const unwrap_check_mixed<T1> tmp(in.m, out);
|
max@0
|
33 const Mat<in_eT>& X = tmp.M;
|
max@0
|
34
|
max@0
|
35 const uword norm_type = in.aux_uword_a;
|
max@0
|
36 const uword dim = in.aux_uword_b;
|
max@0
|
37
|
max@0
|
38 arma_debug_check( (norm_type > 1), "stddev(): incorrect usage. norm_type must be 0 or 1");
|
max@0
|
39 arma_debug_check( (dim > 1), "stddev(): incorrect usage. dim must be 0 or 1" );
|
max@0
|
40
|
max@0
|
41 const uword X_n_rows = X.n_rows;
|
max@0
|
42 const uword X_n_cols = X.n_cols;
|
max@0
|
43
|
max@0
|
44 if(dim == 0)
|
max@0
|
45 {
|
max@0
|
46 arma_extra_debug_print("op_stddev::apply(), dim = 0");
|
max@0
|
47
|
max@0
|
48 arma_debug_check( (X_n_rows == 0), "stddev(): given object has zero rows" );
|
max@0
|
49
|
max@0
|
50 out.set_size(1, X_n_cols);
|
max@0
|
51
|
max@0
|
52 out_eT* out_mem = out.memptr();
|
max@0
|
53
|
max@0
|
54 for(uword col=0; col<X_n_cols; ++col)
|
max@0
|
55 {
|
max@0
|
56 out_mem[col] = std::sqrt( op_var::direct_var( X.colptr(col), X_n_rows, norm_type ) );
|
max@0
|
57 }
|
max@0
|
58 }
|
max@0
|
59 else
|
max@0
|
60 if(dim == 1)
|
max@0
|
61 {
|
max@0
|
62 arma_extra_debug_print("op_stddev::apply(), dim = 1");
|
max@0
|
63
|
max@0
|
64 arma_debug_check( (X_n_cols == 0), "stddev(): given object has zero columns" );
|
max@0
|
65
|
max@0
|
66 out.set_size(X_n_rows, 1);
|
max@0
|
67
|
max@0
|
68 podarray<in_eT> tmp(X_n_cols);
|
max@0
|
69
|
max@0
|
70 in_eT* tmp_mem = tmp.memptr();
|
max@0
|
71 out_eT* out_mem = out.memptr();
|
max@0
|
72
|
max@0
|
73 for(uword row=0; row<X_n_rows; ++row)
|
max@0
|
74 {
|
max@0
|
75 tmp.copy_row(X, row);
|
max@0
|
76
|
max@0
|
77 out_mem[row] = std::sqrt( op_var::direct_var( tmp_mem, X_n_cols, norm_type) );
|
max@0
|
78 }
|
max@0
|
79 }
|
max@0
|
80 }
|
max@0
|
81
|
max@0
|
82
|
max@0
|
83
|
max@0
|
84 //! @}
|
max@0
|
85
|