fn_trace.hpp
Go to the documentation of this file.
00001 // Copyright (C) 2008-2010 NICTA (www.nicta.com.au)
00002 // Copyright (C) 2008-2010 Conrad Sanderson
00003 // 
00004 // This file is part of the Armadillo C++ library.
00005 // It is provided without any warranty of fitness
00006 // for any purpose. You can redistribute this file
00007 // and/or modify it under the terms of the GNU
00008 // Lesser General Public License (LGPL) as published
00009 // by the Free Software Foundation, either version 3
00010 // of the License or (at your option) any later version.
00011 // (see http://www.opensource.org/licenses for more info)
00012 
00013 
00016 
00017 
00019 template<typename T1>
00020 inline
00021 arma_warn_unused
00022 typename T1::elem_type
00023 trace(const Base<typename T1::elem_type,T1>& X)
00024   {
00025   arma_extra_debug_sigprint();
00026   
00027   typedef typename T1::elem_type eT;
00028   
00029   const Proxy<T1> A(X.get_ref());
00030 
00031   arma_debug_check( (A.get_n_rows() != A.get_n_cols()), "trace(): matrix must be square sized" );
00032   
00033   const uword N   = A.get_n_rows();
00034         eT  val = eT(0);
00035   
00036   for(uword i=0; i<N; ++i)
00037     {
00038     val += A.at(i,i);
00039     }
00040   
00041   return val;
00042   }
00043 
00044 
00045 
00046 template<typename T1>
00047 inline
00048 arma_warn_unused
00049 typename T1::elem_type
00050 trace(const Op<T1, op_diagmat>& X)
00051   {
00052   arma_extra_debug_sigprint();
00053   
00054   typedef typename T1::elem_type eT;
00055   
00056   const diagmat_proxy<T1> A(X.m);
00057   
00058   const uword N = A.n_elem;
00059   
00060   eT val = eT(0);
00061   
00062   for(uword i=0; i<N; ++i)
00063     {
00064     val += A[i];
00065     }
00066   
00067   return val;
00068   }
00069 
00070 
00072 template<typename T1, typename T2>
00073 inline
00074 arma_warn_unused
00075 typename T1::elem_type
00076 trace(const Glue<T1, T2, glue_times>& X)
00077   {
00078   arma_extra_debug_sigprint();
00079   
00080   typedef typename T1::elem_type eT;
00081   
00082   const unwrap<T1> tmp1(X.A);
00083   const unwrap<T2> tmp2(X.B);
00084   
00085   const Mat<eT>& A = tmp1.M;
00086   const Mat<eT>& B = tmp2.M;
00087   
00088   arma_debug_assert_mul_size(A, B, "matrix multiply");
00089   
00090   arma_debug_check( (A.n_rows != B.n_cols), "trace(): matrix must be square sized" );
00091   
00092   const uword N1  = A.n_rows;
00093   const uword N2  = A.n_cols;
00094         eT  val = eT(0);
00095   
00096   for(uword i=0; i<N1; ++i)
00097     {
00098     const eT* B_colmem = B.colptr(i);
00099           eT  acc      = eT(0);
00100     
00101     for(uword j=0; j<N2; ++j)
00102       {
00103       acc += A.at(i,j) * B_colmem[j];
00104       }
00105     
00106     val += acc;
00107     }
00108   
00109   return val;
00110   }
00111 
00112 
00113 


armadillo_matrix
Author(s): Conrad Sanderson - NICTA (www.nicta.com.au), (Wrapper by Sjoerd van den Dries)
autogenerated on Tue Jan 7 2014 11:42:04