comparison armadillo-3.900.4/include/armadillo_bits/glue_cov_meat.hpp @ 49:1ec0e2823891

Switch to using subrepo copies of qm-dsp, nnls-chroma, vamp-plugin-sdk; update Armadillo version; assume build without external BLAS/LAPACK
author Chris Cannam
date Thu, 13 Jun 2013 10:25:24 +0100
parents
children
comparison
equal deleted inserted replaced
48:69251e11a913 49:1ec0e2823891
1 // Copyright (C) 2009-2012 NICTA (www.nicta.com.au)
2 // Copyright (C) 2009-2012 Conrad Sanderson
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 //! \addtogroup glue_cov
11 //! @{
12
13
14
15 template<typename eT>
16 inline
17 void
18 glue_cov::direct_cov(Mat<eT>& out, const Mat<eT>& A, const Mat<eT>& B, const uword norm_type)
19 {
20 arma_extra_debug_sigprint();
21
22 if(A.is_vec() && B.is_vec())
23 {
24 arma_debug_check( (A.n_elem != B.n_elem), "cov(): the number of elements in A and B must match" );
25
26 const eT* A_ptr = A.memptr();
27 const eT* B_ptr = B.memptr();
28
29 eT A_acc = eT(0);
30 eT B_acc = eT(0);
31 eT out_acc = eT(0);
32
33 const uword N = A.n_elem;
34
35 for(uword i=0; i<N; ++i)
36 {
37 const eT A_tmp = A_ptr[i];
38 const eT B_tmp = B_ptr[i];
39
40 A_acc += A_tmp;
41 B_acc += B_tmp;
42
43 out_acc += A_tmp * B_tmp;
44 }
45
46 out_acc -= (A_acc * B_acc)/eT(N);
47
48 const eT norm_val = (norm_type == 0) ? ( (N > 1) ? eT(N-1) : eT(1) ) : eT(N);
49
50 out.set_size(1,1);
51 out[0] = out_acc/norm_val;
52 }
53 else
54 {
55 arma_debug_assert_mul_size(A, B, true, false, "cov()");
56
57 const uword N = A.n_rows;
58 const eT norm_val = (norm_type == 0) ? ( (N > 1) ? eT(N-1) : eT(1) ) : eT(N);
59
60 out = trans(A) * B;
61 out -= (trans(sum(A)) * sum(B))/eT(N);
62 out /= norm_val;
63 }
64 }
65
66
67
68 template<typename T>
69 inline
70 void
71 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)
72 {
73 arma_extra_debug_sigprint();
74
75 typedef typename std::complex<T> eT;
76
77 if(A.is_vec() && B.is_vec())
78 {
79 arma_debug_check( (A.n_elem != B.n_elem), "cov(): the number of elements in A and B must match" );
80
81 const eT* A_ptr = A.memptr();
82 const eT* B_ptr = B.memptr();
83
84 eT A_acc = eT(0);
85 eT B_acc = eT(0);
86 eT out_acc = eT(0);
87
88 const uword N = A.n_elem;
89
90 for(uword i=0; i<N; ++i)
91 {
92 const eT A_tmp = A_ptr[i];
93 const eT B_tmp = B_ptr[i];
94
95 A_acc += A_tmp;
96 B_acc += B_tmp;
97
98 out_acc += std::conj(A_tmp) * B_tmp;
99 }
100
101 out_acc -= (std::conj(A_acc) * B_acc)/eT(N);
102
103 const eT norm_val = (norm_type == 0) ? ( (N > 1) ? eT(N-1) : eT(1) ) : eT(N);
104
105 out.set_size(1,1);
106 out[0] = out_acc/norm_val;
107 }
108 else
109 {
110 arma_debug_assert_mul_size(A, B, true, false, "cov()");
111
112 const uword N = A.n_rows;
113 const eT norm_val = (norm_type == 0) ? ( (N > 1) ? eT(N-1) : eT(1) ) : eT(N);
114
115 out = trans(A) * B; // out = strans(conj(A)) * B;
116 out -= (trans(sum(A)) * sum(B))/eT(N); // out -= (strans(conj(sum(A))) * sum(B))/eT(N);
117 out /= norm_val;
118 }
119 }
120
121
122
123 template<typename T1, typename T2>
124 inline
125 void
126 glue_cov::apply(Mat<typename T1::elem_type>& out, const Glue<T1,T2,glue_cov>& X)
127 {
128 arma_extra_debug_sigprint();
129
130 typedef typename T1::elem_type eT;
131
132 const unwrap_check<T1> A_tmp(X.A, out);
133 const unwrap_check<T2> B_tmp(X.B, out);
134
135 const Mat<eT>& A = A_tmp.M;
136 const Mat<eT>& B = B_tmp.M;
137
138 const uword norm_type = X.aux_uword;
139
140 if(&A != &B)
141 {
142 glue_cov::direct_cov(out, A, B, norm_type);
143 }
144 else
145 {
146 op_cov::direct_cov(out, A, norm_type);
147 }
148
149 }
150
151
152
153 //! @}