From 32cb97237e7691d31977ab503c6ea4511e8eb3a8 Mon Sep 17 00:00:00 2001 From: Daniel Wilhelm Date: Fri, 18 Apr 2014 17:18:53 +0200 Subject: 5.0 --- zen/basic_math.h | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) (limited to 'zen/basic_math.h') diff --git a/zen/basic_math.h b/zen/basic_math.h index f4f46ce0..e9ab1a2f 100644 --- a/zen/basic_math.h +++ b/zen/basic_math.h @@ -2,7 +2,7 @@ // * This file is part of the zenXML project. It is distributed under the * // * Boost Software License, Version 1.0. See accompanying file * // * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt. * -// * Copyright (C) 2011 ZenJu (zhnmju123 AT gmx.de) * +// * Copyright (C) ZenJu (zhnmju123 AT gmx DOT de) - All Rights Reserved * // ************************************************************************** #ifndef BASIC_MATH_HEADER_34726398432 @@ -32,7 +32,7 @@ template const T& max(const T& a, const T& b, const T& c); template -void confine(T& val, const T& minVal, const T& maxVal); //make sure minVal <= val && val <= maxVal +void restrict(T& val, const T& minVal, const T& maxVal); //make sure minVal <= val && val <= maxVal template std::pair minMaxElement(InputIterator first, InputIterator last); @@ -131,7 +131,7 @@ const T& max(const T& a, const T& b, const T& c) template inline -void confine(T& val, const T& minVal, const T& maxVal) +void restrict(T& val, const T& minVal, const T& maxVal) { assert(minVal <= maxVal); if (val < minVal) @@ -260,19 +260,6 @@ double median(RandomAccessIterator first, RandomAccessIterator last) //note: inv } -class LessMinusMedAbs : public std::binary_function -{ -public: - LessMinusMedAbs(double median) : median_(median) {} - bool operator()(double lhs, double rhs) const - { - return abs(lhs - median_) < abs(rhs - median_); - } -private: - double median_; -}; - - template inline double mad(RandomAccessIterator first, RandomAccessIterator last) //note: invalidates input range! { @@ -285,14 +272,16 @@ double mad(RandomAccessIterator first, RandomAccessIterator last) //note: invali //the second median needs to operate on absolute residuals => avoid transforming input range as it may decrease precision! - std::nth_element(first, first + n / 2, last, LessMinusMedAbs(m)); //complexity: O(n) + auto lessMedAbs = [m](double lhs, double rhs) { return abs(lhs - m) < abs(rhs - m); }; + + std::nth_element(first, first + n / 2, last, lessMedAbs); //complexity: O(n) const double midVal = abs(*(first + n / 2) - m); if (n % 2 != 0) return midVal; else //n is even and >= 2 in this context: return mean of two middle values { - const double midVal2 = abs(*std::max_element(first, first + n / 2, LessMinusMedAbs(m)) - m); + const double midVal2 = abs(*std::max_element(first, first + n / 2, lessMedAbs) - m); return 0.5 * (midVal2 + midVal); } } -- cgit