Files
eigen/Eigen/src/SparseCore/SparseCwiseUnaryOp.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

143 lines
4.6 KiB
C
Raw Normal View History

// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2008-2015 Gael Guennebaud <gael.guennebaud@inria.fr>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
#ifndef EIGEN_SPARSE_CWISE_UNARY_OP_H
#define EIGEN_SPARSE_CWISE_UNARY_OP_H
// IWYU pragma: private
#include "./InternalHeaderCheck.h"
namespace Eigen {
namespace internal {
2023-11-29 11:12:48 +00:00
template <typename UnaryOp, typename ArgType>
struct unary_evaluator<CwiseUnaryOp<UnaryOp, ArgType>, IteratorBased>
: public evaluator_base<CwiseUnaryOp<UnaryOp, ArgType> > {
public:
typedef CwiseUnaryOp<UnaryOp, ArgType> XprType;
2023-11-29 11:12:48 +00:00
class InnerIterator;
2023-11-29 11:12:48 +00:00
enum {
CoeffReadCost = int(evaluator<ArgType>::CoeffReadCost) + int(functor_traits<UnaryOp>::Cost),
Flags = XprType::Flags
};
2023-11-29 11:12:48 +00:00
explicit unary_evaluator(const XprType& op) : m_functor(op.functor()), m_argImpl(op.nestedExpression()) {
EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<UnaryOp>::Cost);
EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
}
2023-11-29 11:12:48 +00:00
inline Index nonZerosEstimate() const { return m_argImpl.nonZerosEstimate(); }
2023-11-29 11:12:48 +00:00
protected:
typedef typename evaluator<ArgType>::InnerIterator EvalIterator;
2023-11-29 11:12:48 +00:00
const UnaryOp m_functor;
evaluator<ArgType> m_argImpl;
};
template <typename UnaryOp, typename ArgType>
class unary_evaluator<CwiseUnaryOp<UnaryOp, ArgType>, IteratorBased>::InnerIterator
: public unary_evaluator<CwiseUnaryOp<UnaryOp, ArgType>, IteratorBased>::EvalIterator {
protected:
typedef typename XprType::Scalar Scalar;
typedef typename unary_evaluator<CwiseUnaryOp<UnaryOp, ArgType>, IteratorBased>::EvalIterator Base;
2023-11-29 11:12:48 +00:00
public:
EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& unaryOp, Index outer)
: Base(unaryOp.m_argImpl, outer), m_functor(unaryOp.m_functor) {}
EIGEN_STRONG_INLINE InnerIterator& operator++() {
Base::operator++();
return *this;
}
EIGEN_STRONG_INLINE Scalar value() const { return m_functor(Base::value()); }
protected:
const UnaryOp m_functor;
2023-11-29 11:12:48 +00:00
private:
Scalar& valueRef();
};
template <typename ViewOp, typename ArgType>
struct unary_evaluator<CwiseUnaryView<ViewOp, ArgType>, IteratorBased>
: public evaluator_base<CwiseUnaryView<ViewOp, ArgType> > {
public:
typedef CwiseUnaryView<ViewOp, ArgType> XprType;
2023-11-29 11:12:48 +00:00
class InnerIterator;
2023-11-29 11:12:48 +00:00
enum {
2021-06-15 09:09:31 -07:00
CoeffReadCost = int(evaluator<ArgType>::CoeffReadCost) + int(functor_traits<ViewOp>::Cost),
Flags = XprType::Flags
};
2023-11-29 11:12:48 +00:00
explicit unary_evaluator(const XprType& op) : m_functor(op.functor()), m_argImpl(op.nestedExpression()) {
EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<ViewOp>::Cost);
EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
}
2023-11-29 11:12:48 +00:00
protected:
typedef typename evaluator<ArgType>::InnerIterator EvalIterator;
2023-11-29 11:12:48 +00:00
const ViewOp m_functor;
evaluator<ArgType> m_argImpl;
};
template <typename ViewOp, typename ArgType>
class unary_evaluator<CwiseUnaryView<ViewOp, ArgType>, IteratorBased>::InnerIterator
: public unary_evaluator<CwiseUnaryView<ViewOp, ArgType>, IteratorBased>::EvalIterator {
protected:
typedef typename XprType::Scalar Scalar;
typedef typename unary_evaluator<CwiseUnaryView<ViewOp, ArgType>, IteratorBased>::EvalIterator Base;
2023-11-29 11:12:48 +00:00
public:
EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& unaryOp, Index outer)
: Base(unaryOp.m_argImpl, outer), m_functor(unaryOp.m_functor) {}
EIGEN_STRONG_INLINE InnerIterator& operator++() {
Base::operator++();
return *this;
}
EIGEN_STRONG_INLINE Scalar value() const { return m_functor(Base::value()); }
EIGEN_STRONG_INLINE Scalar& valueRef() { return m_functor(Base::valueRef()); }
protected:
const ViewOp m_functor;
};
} // end namespace internal
2009-12-16 19:18:40 +01:00
template <typename Derived>
EIGEN_STRONG_INLINE Derived& SparseMatrixBase<Derived>::operator*=(const Scalar& other) {
typedef typename internal::evaluator<Derived>::InnerIterator EvalIterator;
internal::evaluator<Derived> thisEval(derived());
for (Index j = 0; j < outerSize(); ++j)
for (EvalIterator i(thisEval, j); i; ++i) i.valueRef() *= other;
2009-12-16 19:18:40 +01:00
return derived();
}
template <typename Derived>
EIGEN_STRONG_INLINE Derived& SparseMatrixBase<Derived>::operator/=(const Scalar& other) {
typedef typename internal::evaluator<Derived>::InnerIterator EvalIterator;
internal::evaluator<Derived> thisEval(derived());
for (Index j = 0; j < outerSize(); ++j)
for (EvalIterator i(thisEval, j); i; ++i) i.valueRef() /= other;
2009-12-16 19:18:40 +01:00
return derived();
}
} // end namespace Eigen
#endif // EIGEN_SPARSE_CWISE_UNARY_OP_H