polynomial: support eval on arbitrary types

This commit is contained in:
Danny Robson 2015-01-29 15:39:20 +11:00
parent 0f19eaf6e1
commit 5e4c1080ec
4 changed files with 49 additions and 26 deletions

View File

@ -118,6 +118,7 @@ UTIL_FILES = \
point.ipp \
polynomial.cpp \
polynomial.hpp \
polynomial.ipp \
pool.cpp \
pool.hpp \
pool.ipp \

View File

@ -156,26 +156,3 @@ namespace util { namespace polynomial {
return s;
}
} }
//-----------------------------------------------------------------------------
template <size_t S>
float
util::polynomial::eval (const std::array<float,S> 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,1>, float);
template float util::polynomial::eval (std::array<float,2>, float);
template float util::polynomial::eval (std::array<float,3>, float);
template float util::polynomial::eval (std::array<float,4>, float);

View File

@ -32,10 +32,12 @@ namespace util {
std::array<float,S>
solve (std::array<float,S+1>);
template <size_t S>
float
eval (std::array<float,S>, float x);
template <size_t S, typename T, typename U>
T
eval (std::array<T,S>, U x);
}
}
#include "polynomial.ipp"
#endif

43
polynomial.ipp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/
#ifdef __UTIL_POLYNOMIAL_IPP
#error "twice included ipp"
#endif
#define __UTIL_POLYNOMIAL_IPP
//-----------------------------------------------------------------------------
template <size_t S, typename T, typename U>
T
util::polynomial::eval (const std::array<T,S> 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;
}