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