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_cor
|
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_cor::direct_cor(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_empty() || B.is_empty() )
|
max@0
|
28 {
|
max@0
|
29 out.reset();
|
max@0
|
30 return;
|
max@0
|
31 }
|
max@0
|
32
|
max@0
|
33 if(A.is_vec() && B.is_vec())
|
max@0
|
34 {
|
max@0
|
35 arma_debug_check( (A.n_elem != B.n_elem), "cor(): the number of elements in the two vectors must match" );
|
max@0
|
36
|
max@0
|
37 const eT* A_ptr = A.memptr();
|
max@0
|
38 const eT* B_ptr = B.memptr();
|
max@0
|
39
|
max@0
|
40 eT A_acc = eT(0);
|
max@0
|
41 eT B_acc = eT(0);
|
max@0
|
42 eT out_acc = eT(0);
|
max@0
|
43
|
max@0
|
44 const uword N = A.n_elem;
|
max@0
|
45
|
max@0
|
46 for(uword i=0; i<N; ++i)
|
max@0
|
47 {
|
max@0
|
48 const eT A_tmp = A_ptr[i];
|
max@0
|
49 const eT B_tmp = B_ptr[i];
|
max@0
|
50
|
max@0
|
51 A_acc += A_tmp;
|
max@0
|
52 B_acc += B_tmp;
|
max@0
|
53
|
max@0
|
54 out_acc += A_tmp * B_tmp;
|
max@0
|
55 }
|
max@0
|
56
|
max@0
|
57 out_acc -= (A_acc * B_acc)/eT(N);
|
max@0
|
58
|
max@0
|
59 const eT norm_val = (norm_type == 0) ? ( (N > 1) ? eT(N-1) : eT(1) ) : eT(N);
|
max@0
|
60
|
max@0
|
61 out.set_size(1,1);
|
max@0
|
62 out[0] = out_acc/norm_val;
|
max@0
|
63
|
max@0
|
64 const Mat<eT> stddev_A = (A.n_rows == 1) ? Mat<eT>(stddev(trans(A))) : Mat<eT>(stddev(A));
|
max@0
|
65 const Mat<eT> stddev_B = (B.n_rows == 1) ? Mat<eT>(stddev(trans(B))) : Mat<eT>(stddev(B));
|
max@0
|
66
|
max@0
|
67 out /= stddev_A * stddev_B;
|
max@0
|
68 }
|
max@0
|
69 else
|
max@0
|
70 {
|
max@0
|
71 arma_debug_assert_same_size(A, B, "cor()");
|
max@0
|
72
|
max@0
|
73 const uword N = A.n_rows;
|
max@0
|
74 const eT norm_val = (norm_type == 0) ? ( (N > 1) ? eT(N-1) : eT(1) ) : eT(N);
|
max@0
|
75
|
max@0
|
76 out = trans(A) * B;
|
max@0
|
77 out -= (trans(sum(A)) * sum(B))/eT(N);
|
max@0
|
78 out /= norm_val;
|
max@0
|
79 out /= trans(stddev(A)) * stddev(B);
|
max@0
|
80 }
|
max@0
|
81 }
|
max@0
|
82
|
max@0
|
83
|
max@0
|
84
|
max@0
|
85 template<typename T>
|
max@0
|
86 inline
|
max@0
|
87 void
|
max@0
|
88 glue_cor::direct_cor(Mat< std::complex<T> >& out, const Mat< std::complex<T> >& A, const Mat< std::complex<T> >& B, const uword norm_type)
|
max@0
|
89 {
|
max@0
|
90 arma_extra_debug_sigprint();
|
max@0
|
91
|
max@0
|
92 typedef typename std::complex<T> eT;
|
max@0
|
93
|
max@0
|
94 if(A.is_empty() || B.is_empty() )
|
max@0
|
95 {
|
max@0
|
96 out.reset();
|
max@0
|
97 return;
|
max@0
|
98 }
|
max@0
|
99
|
max@0
|
100 if(A.is_vec() && B.is_vec())
|
max@0
|
101 {
|
max@0
|
102 arma_debug_check( (A.n_elem != B.n_elem), "cor(): the number of elements in the two vectors must match" );
|
max@0
|
103
|
max@0
|
104 const eT* A_ptr = A.memptr();
|
max@0
|
105 const eT* B_ptr = B.memptr();
|
max@0
|
106
|
max@0
|
107 eT A_acc = eT(0);
|
max@0
|
108 eT B_acc = eT(0);
|
max@0
|
109 eT out_acc = eT(0);
|
max@0
|
110
|
max@0
|
111 const uword N = A.n_elem;
|
max@0
|
112
|
max@0
|
113 for(uword i=0; i<N; ++i)
|
max@0
|
114 {
|
max@0
|
115 const eT A_tmp = A_ptr[i];
|
max@0
|
116 const eT B_tmp = B_ptr[i];
|
max@0
|
117
|
max@0
|
118 A_acc += A_tmp;
|
max@0
|
119 B_acc += B_tmp;
|
max@0
|
120
|
max@0
|
121 out_acc += std::conj(A_tmp) * B_tmp;
|
max@0
|
122 }
|
max@0
|
123
|
max@0
|
124 out_acc -= (std::conj(A_acc) * B_acc)/eT(N);
|
max@0
|
125
|
max@0
|
126 const eT norm_val = (norm_type == 0) ? ( (N > 1) ? eT(N-1) : eT(1) ) : eT(N);
|
max@0
|
127
|
max@0
|
128 out.set_size(1,1);
|
max@0
|
129 out[0] = out_acc/norm_val;
|
max@0
|
130
|
max@0
|
131 const Mat<T> stddev_A = (A.n_rows == 1) ? Mat<T>(stddev(trans(A))) : Mat<T>(stddev(A));
|
max@0
|
132 const Mat<T> stddev_B = (B.n_rows == 1) ? Mat<T>(stddev(trans(B))) : Mat<T>(stddev(B));
|
max@0
|
133
|
max@0
|
134 out /= conv_to< Mat<eT> >::from( stddev_A * stddev_B );
|
max@0
|
135 }
|
max@0
|
136 else
|
max@0
|
137 {
|
max@0
|
138 arma_debug_assert_same_size(A, B, "cor()");
|
max@0
|
139
|
max@0
|
140 const uword N = A.n_rows;
|
max@0
|
141 const eT norm_val = (norm_type == 0) ? ( (N > 1) ? eT(N-1) : eT(1) ) : eT(N);
|
max@0
|
142
|
max@0
|
143 out = trans(A) * B; // out = strans(conj(A)) * B;
|
max@0
|
144 out -= (trans(sum(A)) * sum(B))/eT(N); // out -= (strans(conj(sum(A))) * sum(B))/eT(N);
|
max@0
|
145 out /= norm_val;
|
max@0
|
146 out /= conv_to< Mat<eT> >::from( trans(stddev(A)) * stddev(B) );
|
max@0
|
147 }
|
max@0
|
148 }
|
max@0
|
149
|
max@0
|
150
|
max@0
|
151
|
max@0
|
152 template<typename T1, typename T2>
|
max@0
|
153 inline
|
max@0
|
154 void
|
max@0
|
155 glue_cor::apply(Mat<typename T1::elem_type>& out, const Glue<T1,T2,glue_cor>& X)
|
max@0
|
156 {
|
max@0
|
157 arma_extra_debug_sigprint();
|
max@0
|
158
|
max@0
|
159 typedef typename T1::elem_type eT;
|
max@0
|
160
|
max@0
|
161 const unwrap_check<T1> A_tmp(X.A, out);
|
max@0
|
162 const unwrap_check<T2> B_tmp(X.B, out);
|
max@0
|
163
|
max@0
|
164 const Mat<eT>& A = A_tmp.M;
|
max@0
|
165 const Mat<eT>& B = B_tmp.M;
|
max@0
|
166
|
max@0
|
167 const uword norm_type = X.aux_uword;
|
max@0
|
168
|
max@0
|
169 if(&A != &B)
|
max@0
|
170 {
|
max@0
|
171 glue_cor::direct_cor(out, A, B, norm_type);
|
max@0
|
172 }
|
max@0
|
173 else
|
max@0
|
174 {
|
max@0
|
175 op_cor::direct_cor(out, A, norm_type);
|
max@0
|
176 }
|
max@0
|
177 }
|
max@0
|
178
|
max@0
|
179
|
max@0
|
180
|
max@0
|
181 //! @}
|