max@0: // Copyright (C) 2009-2011 NICTA (www.nicta.com.au) max@0: // Copyright (C) 2009-2011 Conrad Sanderson max@0: // Copyright (C) 2009-2010 Dimitrios Bouzas max@0: // max@0: // This file is part of the Armadillo C++ library. max@0: // It is provided without any warranty of fitness max@0: // for any purpose. You can redistribute this file max@0: // and/or modify it under the terms of the GNU max@0: // Lesser General Public License (LGPL) as published max@0: // by the Free Software Foundation, either version 3 max@0: // of the License or (at your option) any later version. max@0: // (see http://www.opensource.org/licenses for more info) max@0: max@0: max@0: //! \addtogroup glue_cor max@0: //! @{ max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: glue_cor::direct_cor(Mat& out, const Mat& A, const Mat& B, const uword norm_type) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: if(A.is_empty() || B.is_empty() ) max@0: { max@0: out.reset(); max@0: return; max@0: } max@0: max@0: if(A.is_vec() && B.is_vec()) max@0: { max@0: arma_debug_check( (A.n_elem != B.n_elem), "cor(): the number of elements in the two vectors must match" ); max@0: max@0: const eT* A_ptr = A.memptr(); max@0: const eT* B_ptr = B.memptr(); max@0: max@0: eT A_acc = eT(0); max@0: eT B_acc = eT(0); max@0: eT out_acc = eT(0); max@0: max@0: const uword N = A.n_elem; max@0: max@0: for(uword i=0; i 1) ? eT(N-1) : eT(1) ) : eT(N); max@0: max@0: out.set_size(1,1); max@0: out[0] = out_acc/norm_val; max@0: max@0: const Mat stddev_A = (A.n_rows == 1) ? Mat(stddev(trans(A))) : Mat(stddev(A)); max@0: const Mat stddev_B = (B.n_rows == 1) ? Mat(stddev(trans(B))) : Mat(stddev(B)); max@0: max@0: out /= stddev_A * stddev_B; max@0: } max@0: else max@0: { max@0: arma_debug_assert_same_size(A, B, "cor()"); max@0: max@0: const uword N = A.n_rows; max@0: const eT norm_val = (norm_type == 0) ? ( (N > 1) ? eT(N-1) : eT(1) ) : eT(N); max@0: max@0: out = trans(A) * B; max@0: out -= (trans(sum(A)) * sum(B))/eT(N); max@0: out /= norm_val; max@0: out /= trans(stddev(A)) * stddev(B); max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: glue_cor::direct_cor(Mat< std::complex >& out, const Mat< std::complex >& A, const Mat< std::complex >& B, const uword norm_type) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: typedef typename std::complex eT; max@0: max@0: if(A.is_empty() || B.is_empty() ) max@0: { max@0: out.reset(); max@0: return; max@0: } max@0: max@0: if(A.is_vec() && B.is_vec()) max@0: { max@0: arma_debug_check( (A.n_elem != B.n_elem), "cor(): the number of elements in the two vectors must match" ); max@0: max@0: const eT* A_ptr = A.memptr(); max@0: const eT* B_ptr = B.memptr(); max@0: max@0: eT A_acc = eT(0); max@0: eT B_acc = eT(0); max@0: eT out_acc = eT(0); max@0: max@0: const uword N = A.n_elem; max@0: max@0: for(uword i=0; i 1) ? eT(N-1) : eT(1) ) : eT(N); max@0: max@0: out.set_size(1,1); max@0: out[0] = out_acc/norm_val; max@0: max@0: const Mat stddev_A = (A.n_rows == 1) ? Mat(stddev(trans(A))) : Mat(stddev(A)); max@0: const Mat stddev_B = (B.n_rows == 1) ? Mat(stddev(trans(B))) : Mat(stddev(B)); max@0: max@0: out /= conv_to< Mat >::from( stddev_A * stddev_B ); max@0: } max@0: else max@0: { max@0: arma_debug_assert_same_size(A, B, "cor()"); max@0: max@0: const uword N = A.n_rows; max@0: const eT norm_val = (norm_type == 0) ? ( (N > 1) ? eT(N-1) : eT(1) ) : eT(N); max@0: max@0: out = trans(A) * B; // out = strans(conj(A)) * B; max@0: out -= (trans(sum(A)) * sum(B))/eT(N); // out -= (strans(conj(sum(A))) * sum(B))/eT(N); max@0: out /= norm_val; max@0: out /= conv_to< Mat >::from( trans(stddev(A)) * stddev(B) ); max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: glue_cor::apply(Mat& out, const Glue& X) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: typedef typename T1::elem_type eT; max@0: max@0: const unwrap_check A_tmp(X.A, out); max@0: const unwrap_check B_tmp(X.B, out); max@0: max@0: const Mat& A = A_tmp.M; max@0: const Mat& B = B_tmp.M; max@0: max@0: const uword norm_type = X.aux_uword; max@0: max@0: if(&A != &B) max@0: { max@0: glue_cor::direct_cor(out, A, B, norm_type); max@0: } max@0: else max@0: { max@0: op_cor::direct_cor(out, A, norm_type); max@0: } max@0: } max@0: max@0: max@0: max@0: //! @}