max@0
|
1 // Copyright (C) 2009-2011 NICTA (www.nicta.com.au)
|
max@0
|
2 // Copyright (C) 2009-2011 Conrad Sanderson
|
max@0
|
3 // Copyright (C) 2009-2010 Dimitrios Bouzas
|
max@0
|
4 //
|
max@0
|
5 // This file is part of the Armadillo C++ library.
|
max@0
|
6 // It is provided without any warranty of fitness
|
max@0
|
7 // for any purpose. You can redistribute this file
|
max@0
|
8 // and/or modify it under the terms of the GNU
|
max@0
|
9 // Lesser General Public License (LGPL) as published
|
max@0
|
10 // by the Free Software Foundation, either version 3
|
max@0
|
11 // of the License or (at your option) any later version.
|
max@0
|
12 // (see http://www.opensource.org/licenses for more info)
|
max@0
|
13
|
max@0
|
14
|
max@0
|
15 //! \addtogroup glue_cov
|
max@0
|
16 //! @{
|
max@0
|
17
|
max@0
|
18
|
max@0
|
19
|
max@0
|
20 template<typename eT>
|
max@0
|
21 inline
|
max@0
|
22 void
|
max@0
|
23 glue_cov::direct_cov(Mat<eT>& out, const Mat<eT>& A, const Mat<eT>& B, const uword norm_type)
|
max@0
|
24 {
|
max@0
|
25 arma_extra_debug_sigprint();
|
max@0
|
26
|
max@0
|
27 if(A.is_vec() && B.is_vec())
|
max@0
|
28 {
|
max@0
|
29 arma_debug_check( (A.n_elem != B.n_elem), "cov(): the number of elements in A and B must match" );
|
max@0
|
30
|
max@0
|
31 const eT* A_ptr = A.memptr();
|
max@0
|
32 const eT* B_ptr = B.memptr();
|
max@0
|
33
|
max@0
|
34 eT A_acc = eT(0);
|
max@0
|
35 eT B_acc = eT(0);
|
max@0
|
36 eT out_acc = eT(0);
|
max@0
|
37
|
max@0
|
38 const uword N = A.n_elem;
|
max@0
|
39
|
max@0
|
40 for(uword i=0; i<N; ++i)
|
max@0
|
41 {
|
max@0
|
42 const eT A_tmp = A_ptr[i];
|
max@0
|
43 const eT B_tmp = B_ptr[i];
|
max@0
|
44
|
max@0
|
45 A_acc += A_tmp;
|
max@0
|
46 B_acc += B_tmp;
|
max@0
|
47
|
max@0
|
48 out_acc += A_tmp * B_tmp;
|
max@0
|
49 }
|
max@0
|
50
|
max@0
|
51 out_acc -= (A_acc * B_acc)/eT(N);
|
max@0
|
52
|
max@0
|
53 const eT norm_val = (norm_type == 0) ? ( (N > 1) ? eT(N-1) : eT(1) ) : eT(N);
|
max@0
|
54
|
max@0
|
55 out.set_size(1,1);
|
max@0
|
56 out[0] = out_acc/norm_val;
|
max@0
|
57 }
|
max@0
|
58 else
|
max@0
|
59 {
|
max@0
|
60 arma_debug_assert_same_size(A, B, "cov()");
|
max@0
|
61
|
max@0
|
62 const uword N = A.n_rows;
|
max@0
|
63 const eT norm_val = (norm_type == 0) ? ( (N > 1) ? eT(N-1) : eT(1) ) : eT(N);
|
max@0
|
64
|
max@0
|
65 out = trans(A) * B;
|
max@0
|
66 out -= (trans(sum(A)) * sum(B))/eT(N);
|
max@0
|
67 out /= norm_val;
|
max@0
|
68 }
|
max@0
|
69 }
|
max@0
|
70
|
max@0
|
71
|
max@0
|
72
|
max@0
|
73 template<typename T>
|
max@0
|
74 inline
|
max@0
|
75 void
|
max@0
|
76 glue_cov::direct_cov(Mat< std::complex<T> >& out, const Mat< std::complex<T> >& A, const Mat< std::complex<T> >& B, const uword norm_type)
|
max@0
|
77 {
|
max@0
|
78 arma_extra_debug_sigprint();
|
max@0
|
79
|
max@0
|
80 typedef typename std::complex<T> eT;
|
max@0
|
81
|
max@0
|
82 if(A.is_vec() && B.is_vec())
|
max@0
|
83 {
|
max@0
|
84 arma_debug_check( (A.n_elem != B.n_elem), "cov(): the number of elements in A and B must match" );
|
max@0
|
85
|
max@0
|
86 const eT* A_ptr = A.memptr();
|
max@0
|
87 const eT* B_ptr = B.memptr();
|
max@0
|
88
|
max@0
|
89 eT A_acc = eT(0);
|
max@0
|
90 eT B_acc = eT(0);
|
max@0
|
91 eT out_acc = eT(0);
|
max@0
|
92
|
max@0
|
93 const uword N = A.n_elem;
|
max@0
|
94
|
max@0
|
95 for(uword i=0; i<N; ++i)
|
max@0
|
96 {
|
max@0
|
97 const eT A_tmp = A_ptr[i];
|
max@0
|
98 const eT B_tmp = B_ptr[i];
|
max@0
|
99
|
max@0
|
100 A_acc += A_tmp;
|
max@0
|
101 B_acc += B_tmp;
|
max@0
|
102
|
max@0
|
103 out_acc += std::conj(A_tmp) * B_tmp;
|
max@0
|
104 }
|
max@0
|
105
|
max@0
|
106 out_acc -= (std::conj(A_acc) * B_acc)/eT(N);
|
max@0
|
107
|
max@0
|
108 const eT norm_val = (norm_type == 0) ? ( (N > 1) ? eT(N-1) : eT(1) ) : eT(N);
|
max@0
|
109
|
max@0
|
110 out.set_size(1,1);
|
max@0
|
111 out[0] = out_acc/norm_val;
|
max@0
|
112 }
|
max@0
|
113 else
|
max@0
|
114 {
|
max@0
|
115 arma_debug_assert_same_size(A, B, "cov()");
|
max@0
|
116
|
max@0
|
117 const uword N = A.n_rows;
|
max@0
|
118 const eT norm_val = (norm_type == 0) ? ( (N > 1) ? eT(N-1) : eT(1) ) : eT(N);
|
max@0
|
119
|
max@0
|
120 out = trans(A) * B; // out = strans(conj(A)) * B;
|
max@0
|
121 out -= (trans(sum(A)) * sum(B))/eT(N); // out -= (strans(conj(sum(A))) * sum(B))/eT(N);
|
max@0
|
122 out /= norm_val;
|
max@0
|
123 }
|
max@0
|
124 }
|
max@0
|
125
|
max@0
|
126
|
max@0
|
127
|
max@0
|
128 template<typename T1, typename T2>
|
max@0
|
129 inline
|
max@0
|
130 void
|
max@0
|
131 glue_cov::apply(Mat<typename T1::elem_type>& out, const Glue<T1,T2,glue_cov>& X)
|
max@0
|
132 {
|
max@0
|
133 arma_extra_debug_sigprint();
|
max@0
|
134
|
max@0
|
135 typedef typename T1::elem_type eT;
|
max@0
|
136
|
max@0
|
137 const unwrap_check<T1> A_tmp(X.A, out);
|
max@0
|
138 const unwrap_check<T2> B_tmp(X.B, out);
|
max@0
|
139
|
max@0
|
140 const Mat<eT>& A = A_tmp.M;
|
max@0
|
141 const Mat<eT>& B = B_tmp.M;
|
max@0
|
142
|
max@0
|
143 const uword norm_type = X.aux_uword;
|
max@0
|
144
|
max@0
|
145 if(&A != &B)
|
max@0
|
146 {
|
max@0
|
147 glue_cov::direct_cov(out, A, B, norm_type);
|
max@0
|
148 }
|
max@0
|
149 else
|
max@0
|
150 {
|
max@0
|
151 op_cov::direct_cov(out, A, norm_type);
|
max@0
|
152 }
|
max@0
|
153
|
max@0
|
154 }
|
max@0
|
155
|
max@0
|
156
|
max@0
|
157
|
max@0
|
158 //! @}
|