comparison armadillo-2.4.4/include/armadillo_bits/op_stddev_meat.hpp @ 0:8b6102e2a9b0

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