File: glue_cor_meat.hpp

package info (click to toggle)
armadillo 1%3A9.200.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,500 kB
  • sloc: cpp: 137,626; makefile: 75; sh: 34
file content (183 lines) | stat: -rw-r--r-- 4,708 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
// Copyright 2008-2016 National ICT Australia (NICTA)
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------


//! \addtogroup glue_cor
//! @{



template<typename eT>
inline
void
glue_cor::direct_cor(Mat<eT>& out, const Mat<eT>& A, const Mat<eT>& B, const uword norm_type)
  {
  arma_extra_debug_sigprint();
  
  if(A.is_empty() || B.is_empty() )
    {
    out.reset();
    return;
    }
  
  if(A.is_vec() && B.is_vec())
    {
    arma_debug_check( (A.n_elem != B.n_elem), "cor(): the number of elements in the two vectors must match" );
    
    const eT* A_ptr = A.memptr();
    const eT* B_ptr = B.memptr();
    
    eT A_acc   = eT(0);
    eT B_acc   = eT(0);
    eT out_acc = eT(0);
    
    const uword N = A.n_elem;
    
    for(uword i=0; i<N; ++i)
      {
      const eT A_tmp = A_ptr[i];
      const eT B_tmp = B_ptr[i];
      
      A_acc += A_tmp;
      B_acc += B_tmp;
      
      out_acc += A_tmp * B_tmp;
      }
    
    out_acc -= (A_acc * B_acc)/eT(N);
    
    const eT norm_val = (norm_type == 0) ? ( (N > 1) ? eT(N-1) : eT(1) ) : eT(N);    
    
    out.set_size(1,1);
    out[0] = out_acc/norm_val;
    
    const Mat<eT> stddev_A = (A.n_rows == 1) ? Mat<eT>(stddev(trans(A))) : Mat<eT>(stddev(A));
    const Mat<eT> stddev_B = (B.n_rows == 1) ? Mat<eT>(stddev(trans(B))) : Mat<eT>(stddev(B));
    
    out /= stddev_A * stddev_B;
    }
  else
    {
    arma_debug_assert_mul_size(A, B, true, false, "cor()");
    
    const uword N = A.n_rows;
    const eT norm_val = (norm_type == 0) ? ( (N > 1) ? eT(N-1) : eT(1) ) : eT(N);
    
    out = trans(A) * B;
    out -= (trans(sum(A)) * sum(B))/eT(N);
    out /= norm_val;
    out /= trans(stddev(A)) * stddev(B);
    }
  }



template<typename T>
inline
void
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)
  {
  arma_extra_debug_sigprint();
  
  typedef typename std::complex<T> eT;
  
  if(A.is_empty() || B.is_empty() )
    {
    out.reset();
    return;
    }
  
  if(A.is_vec() && B.is_vec())
    {
    arma_debug_check( (A.n_elem != B.n_elem), "cor(): the number of elements in the two vectors must match" );
    
    const eT* A_ptr = A.memptr();
    const eT* B_ptr = B.memptr();        
    
    eT A_acc   = eT(0);
    eT B_acc   = eT(0);
    eT out_acc = eT(0);
    
    const uword N = A.n_elem;
    
    for(uword i=0; i<N; ++i)
      {
      const eT A_tmp = A_ptr[i];
      const eT B_tmp = B_ptr[i];
      
      A_acc += A_tmp;
      B_acc += B_tmp;
      
      out_acc += std::conj(A_tmp) * B_tmp;
      }
    
    out_acc -= (std::conj(A_acc) * B_acc)/eT(N);
    
    const eT norm_val = (norm_type == 0) ? ( (N > 1) ? eT(N-1) : eT(1) ) : eT(N);
    
    out.set_size(1,1);
    out[0] = out_acc/norm_val;
    
    const Mat<T> stddev_A = (A.n_rows == 1) ? Mat<T>(stddev(trans(A))) : Mat<T>(stddev(A));
    const Mat<T> stddev_B = (B.n_rows == 1) ? Mat<T>(stddev(trans(B))) : Mat<T>(stddev(B));
    
    out /= conv_to< Mat<eT> >::from( stddev_A * stddev_B );
    }
  else
    {
    arma_debug_assert_mul_size(A, B, true, false, "cor()");
    
    const uword N = A.n_rows;
    const eT norm_val = (norm_type == 0) ? ( (N > 1) ? eT(N-1) : eT(1) ) : eT(N);
    
    out = trans(A) * B;                     // out = strans(conj(A)) * B;
    out -= (trans(sum(A)) * sum(B))/eT(N);  // out -= (strans(conj(sum(A))) * sum(B))/eT(N);
    out /= norm_val;
    out /= conv_to< Mat<eT> >::from( trans(stddev(A)) * stddev(B) );
    }
  }



template<typename T1, typename T2>
inline
void
glue_cor::apply(Mat<typename T1::elem_type>& out, const Glue<T1,T2,glue_cor>& X)
  {
  arma_extra_debug_sigprint();
  
  typedef typename T1::elem_type eT;
  
  const unwrap_check<T1> A_tmp(X.A, out);
  const unwrap_check<T2> B_tmp(X.B, out);
  
  const Mat<eT>& A = A_tmp.M;
  const Mat<eT>& B = B_tmp.M;
  
  const uword norm_type = X.aux_uword;
  
  if(&A != &B)
    {
    glue_cor::direct_cor(out, A, B, norm_type);
    }
  else
    {
    op_cor::direct_cor(out, A, norm_type);
    }
  }



//! @}