From 2bd701b8d3e979f5e29ec6b276018474f9397658 Mon Sep 17 00:00:00 2001 From: Steven Hahn <hahnse@ornl.gov> Date: Tue, 3 Mar 2020 16:32:23 -0500 Subject: [PATCH] Avoid extra multiply in Plus and Minus Signed-off-by: Steven Hahn <hahnse@ornl.gov> --- Framework/Algorithms/src/Minus.cpp | 14 ++++++++------ Framework/Algorithms/src/Plus.cpp | 15 +++++++++------ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/Framework/Algorithms/src/Minus.cpp b/Framework/Algorithms/src/Minus.cpp index eedd17795ad..bd3b569932b 100644 --- a/Framework/Algorithms/src/Minus.cpp +++ b/Framework/Algorithms/src/Minus.cpp @@ -22,7 +22,7 @@ void Minus::performBinaryOperation(const HistogramData::Histogram &lhs, HistogramData::HistogramY &YOut, HistogramData::HistogramE &EOut) { std::transform(lhs.y().begin(), lhs.y().end(), rhs.y().begin(), YOut.begin(), - std::minus<double>()); + std::minus<>()); std::transform(lhs.e().begin(), lhs.e().end(), rhs.e().begin(), EOut.begin(), VectorHelper::SumGaussError<double>()); } @@ -33,12 +33,14 @@ void Minus::performBinaryOperation(const HistogramData::Histogram &lhs, HistogramData::HistogramE &EOut) { using std::placeholders::_1; std::transform(lhs.y().begin(), lhs.y().end(), YOut.begin(), - std::bind(std::minus<double>(), _1, rhsY)); + [rhsY](const double &l) { return l - rhsY; }); // Only do E if non-zero, otherwise just copy - if (rhsE != 0) - std::transform(lhs.e().begin(), lhs.e().end(), EOut.begin(), - std::bind(VectorHelper::SumGaussError<double>(), _1, rhsE)); - else + if (rhsE != 0) { + double rhsE2 = rhsE * rhsE; + std::transform( + lhs.e().begin(), lhs.e().end(), EOut.begin(), + [rhsE2](const double &l) { return std::sqrt(l * l + rhsE2); }); + } else EOut = lhs.e(); } diff --git a/Framework/Algorithms/src/Plus.cpp b/Framework/Algorithms/src/Plus.cpp index 830f03c98b2..b97ec5d3f88 100644 --- a/Framework/Algorithms/src/Plus.cpp +++ b/Framework/Algorithms/src/Plus.cpp @@ -24,7 +24,7 @@ void Plus::performBinaryOperation(const HistogramData::Histogram &lhs, HistogramData::HistogramY &YOut, HistogramData::HistogramE &EOut) { std::transform(lhs.y().begin(), lhs.y().end(), rhs.y().begin(), YOut.begin(), - std::plus<double>()); + std::plus<>()); std::transform(lhs.e().begin(), lhs.e().end(), rhs.e().begin(), EOut.begin(), VectorHelper::SumGaussError<double>()); } @@ -36,12 +36,15 @@ void Plus::performBinaryOperation(const HistogramData::Histogram &lhs, HistogramData::HistogramE &EOut) { using std::placeholders::_1; std::transform(lhs.y().begin(), lhs.y().end(), YOut.begin(), - std::bind(std::plus<double>(), _1, rhsY)); + [rhsY](const double &l) { return l + rhsY; }); // Only do E if non-zero, otherwise just copy - if (rhsE != 0) - std::transform(lhs.e().begin(), lhs.e().end(), EOut.begin(), - std::bind(VectorHelper::SumGaussError<double>(), _1, rhsE)); - else + + if (rhsE != 0.) { + double rhsE2 = rhsE * rhsE; + std::transform( + lhs.e().begin(), lhs.e().end(), EOut.begin(), + [rhsE2](const double &l) { return std::sqrt(l * l + rhsE2); }); + } else EOut = lhs.e(); } -- GitLab