Eigen  3.4.90 (git rev a4098ac676528a83cfb73d4d26ce1b42ec05f47c)
SparseRedux.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008-2014 Gael Guennebaud <gael.guennebaud@inria.fr>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10#ifndef EIGEN_SPARSEREDUX_H
11#define EIGEN_SPARSEREDUX_H
12
13#include "./InternalHeaderCheck.h"
14
15namespace Eigen {
16
17template<typename Derived>
18typename internal::traits<Derived>::Scalar
19SparseMatrixBase<Derived>::sum() const
20{
21 eigen_assert(rows()>0 && cols()>0 && "you are using a non initialized matrix");
22 Scalar res(0);
23 internal::evaluator<Derived> thisEval(derived());
24 for (Index j=0; j<outerSize(); ++j)
25 for (typename internal::evaluator<Derived>::InnerIterator iter(thisEval,j); iter; ++iter)
26 res += iter.value();
27 return res;
28}
29
30template<typename Scalar_, int Options_, typename Index_>
31typename internal::traits<SparseMatrix<Scalar_,Options_,Index_> >::Scalar
33{
34 eigen_assert(rows()>0 && cols()>0 && "you are using a non initialized matrix");
35 if(this->isCompressed())
36 return Matrix<Scalar,1,Dynamic>::Map(m_data.valuePtr(), m_data.size()).sum();
37 else
38 return Base::sum();
39}
40
41template<typename Scalar_, int Options_, typename Index_>
42typename internal::traits<SparseVector<Scalar_,Options_, Index_> >::Scalar
44{
45 eigen_assert(rows()>0 && cols()>0 && "you are using a non initialized matrix");
46 return Matrix<Scalar,1,Dynamic>::Map(m_data.valuePtr(), m_data.size()).sum();
47}
48
49} // end namespace Eigen
50
51#endif // EIGEN_SPARSEREDUX_H
static ConstMapType Map(const Scalar *data)
Definition: PlainObjectBase.h:643
Scalar sum() const
Definition: SparseRedux.h:32
Scalar sum() const
Definition: SparseRedux.h:43
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59