From 5e4c1080ecad84d8e1e829bcc5be035f637033d0 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Thu, 29 Jan 2015 15:39:20 +1100 Subject: [PATCH] polynomial: support eval on arbitrary types --- Makefile.am | 1 + polynomial.cpp | 23 ----------------------- polynomial.hpp | 8 +++++--- polynomial.ipp | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 26 deletions(-) create mode 100644 polynomial.ipp diff --git a/Makefile.am b/Makefile.am index 56803449..b8a4166a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -118,6 +118,7 @@ UTIL_FILES = \ point.ipp \ polynomial.cpp \ polynomial.hpp \ + polynomial.ipp \ pool.cpp \ pool.hpp \ pool.ipp \ diff --git a/polynomial.cpp b/polynomial.cpp index dc761a91..eabe3d54 100644 --- a/polynomial.cpp +++ b/polynomial.cpp @@ -156,26 +156,3 @@ namespace util { namespace polynomial { return s; } } } - - -//----------------------------------------------------------------------------- -template -float -util::polynomial::eval (const std::array coeffs, const float x) -{ - float x_ = 1.f; - float sum = 0.f; - for (size_t i = 0; i < S; ++i) { - sum += coeffs[S-i-1] * x_; - x_ *= x; - } - - return sum; -} - - -//----------------------------------------------------------------------------- -template float util::polynomial::eval (std::array, float); -template float util::polynomial::eval (std::array, float); -template float util::polynomial::eval (std::array, float); -template float util::polynomial::eval (std::array, float); diff --git a/polynomial.hpp b/polynomial.hpp index b7490178..2449037f 100644 --- a/polynomial.hpp +++ b/polynomial.hpp @@ -32,10 +32,12 @@ namespace util { std::array solve (std::array); - template - float - eval (std::array, float x); + template + T + eval (std::array, U x); } } +#include "polynomial.ipp" + #endif diff --git a/polynomial.ipp b/polynomial.ipp new file mode 100644 index 00000000..8a404d7b --- /dev/null +++ b/polynomial.ipp @@ -0,0 +1,43 @@ +/* + * This file is part of libgim. + * + * libgim is free software: you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * libgim is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with libgim. If not, see . + * + * Copyright 2015 Danny Robson + */ + + +#ifdef __UTIL_POLYNOMIAL_IPP +#error "twice included ipp" +#endif + +#define __UTIL_POLYNOMIAL_IPP + + +//----------------------------------------------------------------------------- +template +T +util::polynomial::eval (const std::array coeffs, const U x) +{ + U x_ = 1.f; + T sum {0.f}; + + for (size_t i = 0; i < S; ++i) { + sum += coeffs[S-i-1] * x_; + x_ *= x; + } + + return sum; +} +