Eigen  3.3.3
SparseRedux.h
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008-2014 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // This Source Code Form is subject to the terms of the Mozilla
00007 // Public License v. 2.0. If a copy of the MPL was not distributed
00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00009 
00010 #ifndef EIGEN_SPARSEREDUX_H
00011 #define EIGEN_SPARSEREDUX_H
00012 
00013 namespace Eigen { 
00014 
00015 template<typename Derived>
00016 typename internal::traits<Derived>::Scalar
00017 SparseMatrixBase<Derived>::sum() const
00018 {
00019   eigen_assert(rows()>0 && cols()>0 && "you are using a non initialized matrix");
00020   Scalar res(0);
00021   internal::evaluator<Derived> thisEval(derived());
00022   for (Index j=0; j<outerSize(); ++j)
00023     for (typename internal::evaluator<Derived>::InnerIterator iter(thisEval,j); iter; ++iter)
00024       res += iter.value();
00025   return res;
00026 }
00027 
00028 template<typename _Scalar, int _Options, typename _Index>
00029 typename internal::traits<SparseMatrix<_Scalar,_Options,_Index> >::Scalar
00030 SparseMatrix<_Scalar,_Options,_Index>::sum() const
00031 {
00032   eigen_assert(rows()>0 && cols()>0 && "you are using a non initialized matrix");
00033   if(this->isCompressed())
00034     return Matrix<Scalar,1,Dynamic>::Map(m_data.valuePtr(), m_data.size()).sum();
00035   else
00036     return Base::sum();
00037 }
00038 
00039 template<typename _Scalar, int _Options, typename _Index>
00040 typename internal::traits<SparseVector<_Scalar,_Options, _Index> >::Scalar
00041 SparseVector<_Scalar,_Options,_Index>::sum() const
00042 {
00043   eigen_assert(rows()>0 && cols()>0 && "you are using a non initialized matrix");
00044   return Matrix<Scalar,1,Dynamic>::Map(m_data.valuePtr(), m_data.size()).sum();
00045 }
00046 
00047 } // end namespace Eigen
00048 
00049 #endif // EIGEN_SPARSEREDUX_H
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends