glue_cov_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_cov::direct_cov(Mat<eT>& out, const Mat<eT>& A, const Mat<eT>& B, const uword norm_type)
24  {
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  {
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
132  {
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 
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
#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_cov(Mat< eT > &out, const Mat< eT > &X, const uword norm_type)
Definition: op_cov_meat.hpp:24
const T2 & B
second operand
Definition: Glue_bones.hpp:45
static void apply(Mat< typename T1::elem_type > &out, const Glue< T1, T2, glue_cov > &X)
static void direct_cov(Mat< eT > &out, const Mat< eT > &A, const Mat< eT > &B, const uword norm_type)
#define arma_extra_debug_sigprint
Definition: debug.hpp:1116
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


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