From 2b15e001068f548b852d58472b9b29f1a7bf1a2c Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Mon, 19 Aug 2013 16:40:50 +0200 Subject: [PATCH] Make ArrayBase operator+=(scalar) and -=(scalar) use SelfCwiseBinaryOp optimization --- Eigen/Core | 2 +- Eigen/src/Core/ArrayBase.h | 6 ++---- Eigen/src/Core/SelfCwiseBinaryOp.h | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/Eigen/Core b/Eigen/Core index 97aa581e2..a508b97f6 100644 --- a/Eigen/Core +++ b/Eigen/Core @@ -284,6 +284,7 @@ using std::ptrdiff_t; #include "src/Core/Assign.h" #endif +#include "src/Core/ArrayBase.h" #include "src/Core/util/BlasUtil.h" #include "src/Core/DenseStorage.h" #include "src/Core/NestByValue.h" @@ -347,7 +348,6 @@ using std::ptrdiff_t; #include "src/Core/Random.h" #include "src/Core/Replicate.h" #include "src/Core/Reverse.h" -#include "src/Core/ArrayBase.h" #include "src/Core/ArrayWrapper.h" #ifdef EIGEN_ENABLE_EVALUATORS diff --git a/Eigen/src/Core/ArrayBase.h b/Eigen/src/Core/ArrayBase.h index 38852600d..b7c4a1c71 100644 --- a/Eigen/src/Core/ArrayBase.h +++ b/Eigen/src/Core/ArrayBase.h @@ -123,10 +123,8 @@ template class ArrayBase return internal::assign_selector::run(derived(), other.derived()); } - Derived& operator+=(const Scalar& scalar) - { return *this = derived() + scalar; } - Derived& operator-=(const Scalar& scalar) - { return *this = derived() - scalar; } + Derived& operator+=(const Scalar& scalar); + Derived& operator-=(const Scalar& scalar); template Derived& operator+=(const ArrayBase& other); diff --git a/Eigen/src/Core/SelfCwiseBinaryOp.h b/Eigen/src/Core/SelfCwiseBinaryOp.h index 96012eaf8..3d2deff98 100644 --- a/Eigen/src/Core/SelfCwiseBinaryOp.h +++ b/Eigen/src/Core/SelfCwiseBinaryOp.h @@ -177,6 +177,24 @@ inline Derived& DenseBase::operator*=(const Scalar& other) return derived(); } +template +inline Derived& ArrayBase::operator+=(const Scalar& other) +{ + typedef typename Derived::PlainObject PlainObject; + SelfCwiseBinaryOp, Derived, typename PlainObject::ConstantReturnType> tmp(derived()); + tmp = PlainObject::Constant(rows(),cols(),other); + return derived(); +} + +template +inline Derived& ArrayBase::operator-=(const Scalar& other) +{ + typedef typename Derived::PlainObject PlainObject; + SelfCwiseBinaryOp, Derived, typename PlainObject::ConstantReturnType> tmp(derived()); + tmp = PlainObject::Constant(rows(),cols(),other); + return derived(); +} + template inline Derived& DenseBase::operator/=(const Scalar& other) {