glue_cor_meat.hpp
Go to the documentation of this file.
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 
17 
18 
19 
20 template<typename eT>
21 inline
22 void
23 glue_cor::direct_cor(Mat<eT>& out, const Mat<eT>& A, const Mat<eT>& B, const uword norm_type)
24  {
26 
27  if(A.is_empty() || B.is_empty() )
28  {
29  out.reset();
30  return;
31  }
32 
33  if(A.is_vec() && B.is_vec())
34  {
35  arma_debug_check( (A.n_elem != B.n_elem), "cor(): the number of elements in the two vectors must match" );
36 
37  const eT* A_ptr = A.memptr();
38  const eT* B_ptr = B.memptr();
39 
40  eT A_acc = eT(0);
41  eT B_acc = eT(0);
42  eT out_acc = eT(0);
43 
44  const uword N = A.n_elem;
45 
46  for(uword i=0; i<N; ++i)
47  {
48  const eT A_tmp = A_ptr[i];
49  const eT B_tmp = B_ptr[i];
50 
51  A_acc += A_tmp;
52  B_acc += B_tmp;
53 
54  out_acc += A_tmp * B_tmp;
55  }
56 
57  out_acc -= (A_acc * B_acc)/eT(N);
58 
59  const eT norm_val = (norm_type == 0) ? ( (N > 1) ? eT(N-1) : eT(1) ) : eT(N);
60 
61  out.set_size(1,1);
62  out[0] = out_acc/norm_val;
63 
64  const Mat<eT> stddev_A = (A.n_rows == 1) ? Mat<eT>(stddev(trans(A))) : Mat<eT>(stddev(A));
65  const Mat<eT> stddev_B = (B.n_rows == 1) ? Mat<eT>(stddev(trans(B))) : Mat<eT>(stddev(B));
66 
67  out /= stddev_A * stddev_B;
68  }
69  else
70  {
71  arma_debug_assert_same_size(A, B, "cor()");
72 
73  const uword N = A.n_rows;
74  const eT norm_val = (norm_type == 0) ? ( (N > 1) ? eT(N-1) : eT(1) ) : eT(N);
75 
76  out = trans(A) * B;
77  out -= (trans(sum(A)) * sum(B))/eT(N);
78  out /= norm_val;
79  out /= trans(stddev(A)) * stddev(B);
80  }
81  }
82 
83 
84 
85 template<typename T>
86 inline
87 void
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)
89  {
91 
92  typedef typename std::complex<T> eT;
93 
94  if(A.is_empty() || B.is_empty() )
95  {
96  out.reset();
97  return;
98  }
99 
100  if(A.is_vec() && B.is_vec())
101  {
102  arma_debug_check( (A.n_elem != B.n_elem), "cor(): the number of elements in the two vectors must match" );
103 
104  const eT* A_ptr = A.memptr();
105  const eT* B_ptr = B.memptr();
106 
107  eT A_acc = eT(0);
108  eT B_acc = eT(0);
109  eT out_acc = eT(0);
110 
111  const uword N = A.n_elem;
112 
113  for(uword i=0; i<N; ++i)
114  {
115  const eT A_tmp = A_ptr[i];
116  const eT B_tmp = B_ptr[i];
117 
118  A_acc += A_tmp;
119  B_acc += B_tmp;
120 
121  out_acc += std::conj(A_tmp) * B_tmp;
122  }
123 
124  out_acc -= (std::conj(A_acc) * B_acc)/eT(N);
125 
126  const eT norm_val = (norm_type == 0) ? ( (N > 1) ? eT(N-1) : eT(1) ) : eT(N);
127 
128  out.set_size(1,1);
129  out[0] = out_acc/norm_val;
130 
131  const Mat<T> stddev_A = (A.n_rows == 1) ? Mat<T>(stddev(trans(A))) : Mat<T>(stddev(A));
132  const Mat<T> stddev_B = (B.n_rows == 1) ? Mat<T>(stddev(trans(B))) : Mat<T>(stddev(B));
133 
134  out /= conv_to< Mat<eT> >::from( stddev_A * stddev_B );
135  }
136  else
137  {
138  arma_debug_assert_same_size(A, B, "cor()");
139 
140  const uword N = A.n_rows;
141  const eT norm_val = (norm_type == 0) ? ( (N > 1) ? eT(N-1) : eT(1) ) : eT(N);
142 
143  out = trans(A) * B; // out = strans(conj(A)) * B;
144  out -= (trans(sum(A)) * sum(B))/eT(N); // out -= (strans(conj(sum(A))) * sum(B))/eT(N);
145  out /= norm_val;
146  out /= conv_to< Mat<eT> >::from( trans(stddev(A)) * stddev(B) );
147  }
148  }
149 
150 
151 
152 template<typename T1, typename T2>
153 inline
154 void
156  {
158 
159  typedef typename T1::elem_type eT;
160 
161  const unwrap_check<T1> A_tmp(X.A, out);
162  const unwrap_check<T2> B_tmp(X.B, out);
163 
164  const Mat<eT>& A = A_tmp.M;
165  const Mat<eT>& B = B_tmp.M;
166 
167  const uword norm_type = X.aux_uword;
168 
169  if(&A != &B)
170  {
171  glue_cor::direct_cor(out, A, B, norm_type);
172  }
173  else
174  {
175  op_cor::direct_cor(out, A, norm_type);
176  }
177  }
178 
179 
180 
arma_inline const Op< T1, op_sum > sum(const Base< typename T1::elem_type, T1 > &X, const uword dim=0)
Delayed sum of elements of a matrix along a specified dimension (either rows or columns). The result is stored in a dense matrix that has either one column or one row. For dim = 0, find the sum of each column. For dim = 1, find the sum of each row. The default is dim = 0. NOTE: this function works differently than in Matlab/Octave.
Definition: fn_sum.hpp:29
arma_inline arma_warn_unused bool is_vec() const
returns true if the object can be interpreted as a column or row vector
Definition: Mat_meat.hpp:3824
arma_inline arma_warn_unused eT * memptr()
returns a pointer to array of eTs used by the matrix
Definition: Mat_meat.hpp:4024
void set_size(const uword in_elem)
change the matrix to have user specified dimensions (data is not preserved)
Definition: Mat_meat.hpp:4211
const mtOp< typename T1::pod_type, T1, op_stddev > stddev(const Base< typename T1::elem_type, T1 > &X, const uword norm_type=0, const uword dim=0)
Definition: fn_stddev.hpp:22
#define arma_debug_assert_same_size
Definition: debug.hpp:1086
arma_inline const T1 & conj(const Base< typename T1::pod_type, T1 > &A)
Definition: fn_elem.hpp:430
const uword n_elem
number of elements in the matrix (read-only)
Definition: Mat_bones.hpp:31
const uword n_rows
number of rows in the matrix (read-only)
Definition: Mat_bones.hpp:29
u32 uword
Definition: typedef.hpp:85
arma_inline const Op< T1, op_htrans > trans(const Base< typename T1::elem_type, T1 > &X)
Definition: fn_trans.hpp:21
#define arma_debug_check
Definition: debug.hpp:1084
const T1 & A
first operand
Definition: Glue_bones.hpp:44
static void direct_cor(Mat< eT > &out, const Mat< eT > &A, const Mat< eT > &B, const uword norm_type)
const T2 & B
second operand
Definition: Glue_bones.hpp:45
void reset()
Definition: Mat_meat.hpp:4553
#define arma_extra_debug_sigprint
Definition: debug.hpp:1116
static void direct_cor(Mat< eT > &out, const Mat< eT > &X, const uword norm_type)
Definition: op_cor_meat.hpp:24
Dense matrix class.
const Mat< eT > M
Definition: unwrap.hpp:142
uword aux_uword
storage of auxiliary data, uword format
Definition: Glue_bones.hpp:46
arma_inline arma_warn_unused bool is_empty() const
returns true if the matrix has no elements
Definition: Mat_meat.hpp:3812
static void apply(Mat< typename T1::elem_type > &out, const Glue< T1, T2, glue_cor > &X)


armadillo_matrix
Author(s):
autogenerated on Fri Apr 16 2021 02:31:57