max@0: // Copyright (C) 2010-2011 NICTA (www.nicta.com.au) max@0: // Copyright (C) 2010-2011 Conrad Sanderson 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_conv max@0: //! @{ max@0: max@0: max@0: //! rudimentary implementation of the convolution operation max@0: max@0: template max@0: inline max@0: void max@0: glue_conv::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: arma_debug_check( ( (A.is_vec() == false) || (B.is_vec() == false) ), "conv(): inputs must be vectors" ); max@0: max@0: max@0: const Mat& h = (A.n_elem <= B.n_elem) ? A : B; max@0: const Mat& x = (A.n_elem <= B.n_elem) ? B : A; max@0: max@0: max@0: const uword h_n_elem = h.n_elem; max@0: const uword x_n_elem = x.n_elem; max@0: const uword out_n_elem = h_n_elem + x_n_elem - 1; max@0: max@0: max@0: if( (h_n_elem == 0) || (x_n_elem == 0) ) max@0: { max@0: out.reset(); max@0: return; max@0: } max@0: max@0: max@0: (A.n_cols == 1) ? out.set_size(out_n_elem, 1) : out.set_size(1, out_n_elem); max@0: max@0: max@0: const eT* h_mem = h.memptr(); max@0: const eT* x_mem = x.memptr(); max@0: eT* out_mem = out.memptr(); max@0: max@0: max@0: for(uword out_i = 0; out_i < (h_n_elem-1); ++out_i) max@0: { max@0: eT acc = eT(0); max@0: max@0: uword h_i = out_i; max@0: max@0: for(uword x_i = 0; x_i <= out_i; ++x_i, --h_i) max@0: { max@0: acc += h_mem[h_i] * x_mem[x_i]; max@0: } max@0: max@0: out_mem[out_i] = acc; max@0: } max@0: max@0: max@0: for(uword out_i = h_n_elem-1; out_i < out_n_elem - (h_n_elem-1); ++out_i) max@0: { max@0: eT acc = eT(0); max@0: max@0: uword h_i = h_n_elem - 1; max@0: max@0: for(uword x_i = out_i - h_n_elem + 1; x_i <= out_i; ++x_i, --h_i) max@0: { max@0: acc += h_mem[h_i] * x_mem[x_i]; max@0: } max@0: max@0: out_mem[out_i] = acc; max@0: } max@0: max@0: max@0: for(uword out_i = out_n_elem - (h_n_elem-1); out_i < out_n_elem; ++out_i) max@0: { max@0: eT acc = eT(0); max@0: max@0: uword h_i = h_n_elem - 1; max@0: max@0: for(uword x_i = out_i - h_n_elem + 1; x_i < x_n_elem; ++x_i, --h_i) max@0: { max@0: acc += h_mem[h_i] * x_mem[x_i]; max@0: } max@0: max@0: out_mem[out_i] = acc; max@0: } max@0: max@0: max@0: } max@0: max@0: max@0: max@0: //! @}