1 | // Copyright (C) 2009-2011 Conrad Sanderson |
---|
2 | // Copyright (C) 2009-2011 NICTA (www.nicta.com.au) |
---|
3 | // Copyright (C) 2009-2010 Dimitrios Bouzas |
---|
4 | // |
---|
5 | // This Source Code Form is subject to the terms of the Mozilla Public |
---|
6 | // License, v. 2.0. If a copy of the MPL was not distributed with this |
---|
7 | // file, You can obtain one at http://mozilla.org/MPL/2.0/. |
---|
8 | |
---|
9 | |
---|
10 | |
---|
11 | //! \addtogroup op_cov |
---|
12 | //! @{ |
---|
13 | |
---|
14 | |
---|
15 | |
---|
16 | template<typename eT> |
---|
17 | inline |
---|
18 | void |
---|
19 | op_cov::direct_cov(Mat<eT>& out, const Mat<eT>& A, const uword norm_type) |
---|
20 | { |
---|
21 | arma_extra_debug_sigprint(); |
---|
22 | |
---|
23 | if(A.is_vec()) |
---|
24 | { |
---|
25 | if(A.n_rows == 1) |
---|
26 | { |
---|
27 | out = var(trans(A), norm_type); |
---|
28 | } |
---|
29 | else |
---|
30 | { |
---|
31 | out = var(A, norm_type); |
---|
32 | } |
---|
33 | } |
---|
34 | else |
---|
35 | { |
---|
36 | const uword N = A.n_rows; |
---|
37 | const eT norm_val = (norm_type == 0) ? ( (N > 1) ? eT(N-1) : eT(1) ) : eT(N); |
---|
38 | |
---|
39 | const Row<eT> acc = sum(A); |
---|
40 | |
---|
41 | out = trans(A) * A; |
---|
42 | out -= (trans(acc) * acc)/eT(N); |
---|
43 | out /= norm_val; |
---|
44 | } |
---|
45 | } |
---|
46 | |
---|
47 | |
---|
48 | |
---|
49 | template<typename T> |
---|
50 | inline |
---|
51 | void |
---|
52 | op_cov::direct_cov(Mat< std::complex<T> >& out, const Mat< std::complex<T> >& A, const uword norm_type) |
---|
53 | { |
---|
54 | arma_extra_debug_sigprint(); |
---|
55 | |
---|
56 | typedef typename std::complex<T> eT; |
---|
57 | |
---|
58 | if(A.is_vec()) |
---|
59 | { |
---|
60 | if(A.n_rows == 1) |
---|
61 | { |
---|
62 | const Mat<T> tmp_mat = var(trans(A), norm_type); |
---|
63 | out.set_size(1,1); |
---|
64 | out[0] = tmp_mat[0]; |
---|
65 | } |
---|
66 | else |
---|
67 | { |
---|
68 | const Mat<T> tmp_mat = var(A, norm_type); |
---|
69 | out.set_size(1,1); |
---|
70 | out[0] = tmp_mat[0]; |
---|
71 | } |
---|
72 | } |
---|
73 | else |
---|
74 | { |
---|
75 | const uword N = A.n_rows; |
---|
76 | const eT norm_val = (norm_type == 0) ? ( (N > 1) ? eT(N-1) : eT(1) ) : eT(N); |
---|
77 | |
---|
78 | const Row<eT> acc = sum(A); |
---|
79 | |
---|
80 | out = trans(A) * A; // out = strans(conj(A)) * A; |
---|
81 | out -= (trans(acc) * acc)/eT(N); // out -= (strans(conj(acc)) * acc)/eT(N); |
---|
82 | out /= norm_val; |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | |
---|
88 | template<typename T1> |
---|
89 | inline |
---|
90 | void |
---|
91 | op_cov::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_cov>& in) |
---|
92 | { |
---|
93 | arma_extra_debug_sigprint(); |
---|
94 | |
---|
95 | typedef typename T1::elem_type eT; |
---|
96 | |
---|
97 | const unwrap_check<T1> tmp(in.m, out); |
---|
98 | const Mat<eT>& A = tmp.M; |
---|
99 | |
---|
100 | const uword norm_type = in.aux_uword_a; |
---|
101 | |
---|
102 | op_cov::direct_cov(out, A, norm_type); |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | |
---|
107 | //! @} |
---|