SparseRedux.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // Eigen is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 3 of the License, or (at your option) any later version.
10 //
11 // Alternatively, you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 2 of
14 // the License, or (at your option) any later version.
15 //
16 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License and a copy of the GNU General Public License along with
23 // Eigen. If not, see <http://www.gnu.org/licenses/>.
24 
25 #ifndef EIGEN_SPARSEREDUX_H
26 #define EIGEN_SPARSEREDUX_H
27 
28 namespace Eigen {
29 
30 template<typename Derived>
31 typename internal::traits<Derived>::Scalar
33 {
34  eigen_assert(rows()>0 && cols()>0 && "you are using a non initialized matrix");
35  Scalar res(0);
36  for (Index j=0; j<outerSize(); ++j)
37  for (typename Derived::InnerIterator iter(derived(),j); iter; ++iter)
38  res += iter.value();
39  return res;
40 }
41 
42 template<typename _Scalar, int _Options, typename _Index>
43 typename internal::traits<SparseMatrix<_Scalar,_Options,_Index> >::Scalar
45 {
46  eigen_assert(rows()>0 && cols()>0 && "you are using a non initialized matrix");
47  return Matrix<Scalar,1,Dynamic>::Map(&m_data.value(0), m_data.size()).sum();
48 }
49 
50 template<typename _Scalar, int _Options, typename _Index>
51 typename internal::traits<SparseVector<_Scalar,_Options, _Index> >::Scalar
53 {
54  eigen_assert(rows()>0 && cols()>0 && "you are using a non initialized matrix");
55  return Matrix<Scalar,1,Dynamic>::Map(&m_data.value(0), m_data.size()).sum();
56 }
57 
58 } // end namespace Eigen
59 
60 #endif // EIGEN_SPARSEREDUX_H