Eigen  5.0.1
 
Loading...
Searching...
No Matches
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// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_SPARSEREDUX_H
12#define EIGEN_SPARSEREDUX_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19template <typename Derived>
20typename internal::traits<Derived>::Scalar SparseMatrixBase<Derived>::sum() const {
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) res += iter.value();
26 return res;
27}
28
29template <typename Scalar_, int Options_, typename Index_>
30typename internal::traits<SparseMatrix<Scalar_, Options_, Index_> >::Scalar
32 eigen_assert(rows() > 0 && cols() > 0 && "you are using a non initialized matrix");
33 if (this->isCompressed())
34 return Matrix<Scalar, 1, Dynamic>::Map(m_data.valuePtr(), m_data.size()).sum();
35 else
36 return Base::sum();
37}
38
39template <typename Scalar_, int Options_, typename Index_>
40typename internal::traits<SparseVector<Scalar_, Options_, Index_> >::Scalar
42 eigen_assert(rows() > 0 && cols() > 0 && "you are using a non initialized matrix");
43 return Matrix<Scalar, 1, Dynamic>::Map(m_data.valuePtr(), m_data.size()).sum();
44}
45
46} // end namespace Eigen
47
48#endif // EIGEN_SPARSEREDUX_H
bool isCompressed() const
Definition SparseCompressedBase.h:115
Index cols() const
Definition SparseMatrix.h:162
Index rows() const
Definition SparseMatrix.h:160
Scalar sum() const
Definition SparseRedux.h:31
Scalar sum() const
Definition SparseRedux.h:41