From f9fa994ff2390af4f640ff720221196a3a804e67 Mon Sep 17 00:00:00 2001
From: Norihiro Watanabe <norihiro.watanabe@ufz.de>
Date: Fri, 21 Mar 2014 11:37:18 +0100
Subject: [PATCH] changed a constructor to take std::array. allow N_VARS=0. use
 std::inner_product

---
 MathLib/LinearFunction.h | 26 ++++++--------------------
 1 file changed, 6 insertions(+), 20 deletions(-)

diff --git a/MathLib/LinearFunction.h b/MathLib/LinearFunction.h
index 7e3441cdc7c..2d7fa4ce07d 100644
--- a/MathLib/LinearFunction.h
+++ b/MathLib/LinearFunction.h
@@ -14,8 +14,7 @@
 #define LINEARFUNCTION_H_
 
 #include <array>
-#include <algorithm>
-#include <cassert>
+#include <numeric>
 
 namespace MathLib
 {
@@ -38,33 +37,20 @@ public:
 	 * \param coefficients  an array of coefficients of a linear function.
 	 * The size of the coefficient array should equal to the number of variables + 1
 	 */
-	explicit LinearFunction(const T_TYPE* coefficients)
-	: _coefficients(copy(coefficients))
-	{
-		static_assert(N_VARS>0, "Template parameter N_VARS in LinearFunction should be nonzero");
-	}
+	explicit LinearFunction(const std::array<T_TYPE, N_VARS+1> &coefficients)
+	: _coefficients(coefficients)
+	{}
 
 	/**
 	 * evaluate the function
-	 * \param x  an array of variables. the size of the array should equal to the number of variables + 1
+	 * \param x  an array of variables. the size of the array should equal to the number of variables
 	 */
 	T_TYPE operator()(T_TYPE const * const x) const
 	{
-		T_TYPE v = _coefficients[0];
-		for (unsigned i=0; i<N_VARS; i++)
-			v += _coefficients[i+1]*x[i];
-		return v;
+		return std::inner_product(_coefficients.cbegin()+1, _coefficients.cend(), x, _coefficients.front());
 	}
 
 private:
-	/// copy a given raw array to std::array
-	static std::array<T_TYPE, N_VARS+1> copy(const T_TYPE* coefficients)
-	{
-		std::array<T_TYPE, N_VARS+1> temp;
-		std::copy(coefficients, coefficients+ temp.size(), temp.data());
-		return temp;
-	}
-
 	/// Coefficients of a linear function
 	const std::array<T_TYPE, N_VARS+1> _coefficients;
 };
-- 
GitLab