polynomial: style

This commit is contained in:
Danny Robson 2016-12-21 20:21:28 +11:00
parent 9116404f30
commit c91e1d29c3
3 changed files with 29 additions and 23 deletions

View File

@ -26,7 +26,7 @@ static const size_t NEWTON_ITERATIONS = 1u;
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
namespace util { namespace polynomial { namespace util::polynomial {
template <> template <>
std::array<float,1> std::array<float,1>
roots (std::array<float,2> coeff) roots (std::array<float,2> coeff)
@ -39,10 +39,11 @@ namespace util { namespace polynomial {
return { -b / a }; return { -b / a };
} }
} } }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
namespace util { namespace polynomial { namespace util::polynomial {
template <> template <>
std::array<float,2> std::array<float,2>
roots (std::array<float,3> coeff) roots (std::array<float,3> coeff)
@ -56,16 +57,18 @@ namespace util { namespace polynomial {
return { s[0], std::numeric_limits<float>::quiet_NaN () }; return { s[0], std::numeric_limits<float>::quiet_NaN () };
} }
auto d = std::sqrt (pow2 (b) - 4 * a * c); auto descriminator = std::sqrt (pow2 (b) - 4 * a * c);
return { (-b - d) / (2 * a), return {
(-b + d) / (2 * a) }; (-b - descriminator) / (2 * a),
(-b + descriminator) / (2 * a)
};
} }
} } }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// From graphics gems: http://tog.acm.org/resources/GraphicsGems/gemsiv/vec_mat/ray/solver.c // From graphics gems: http://tog.acm.org/resources/GraphicsGems/gemsiv/vec_mat/ray/solver.c
namespace util { namespace polynomial { namespace util::polynomial {
template <> template <>
std::array<float,3> std::array<float,3>
roots (std::array<float,4> coeffs) roots (std::array<float,4> coeffs)
@ -151,4 +154,10 @@ namespace util { namespace polynomial {
return s; return s;
} }
} } }
///////////////////////////////////////////////////////////////////////////////
template std::array<float,1> util::polynomial::roots (std::array<float,1+1>);
template std::array<float,2> util::polynomial::roots (std::array<float,1+2>);
template std::array<float,3> util::polynomial::roots (std::array<float,1+3>);

View File

@ -20,19 +20,17 @@
#include <array> #include <array>
#include <cstdlib> #include <cstdlib>
namespace util { namespace util::polynomial {
namespace polynomial { // Invalid solutions are represented by NaN. They are guaranteed to
// Invalid solutions are represented by NaN. They are guaranteed to // be at the end of the solution list, so they are safe to skip on the
// be at the end of the solution list, so they are safe to skip on the // first instance.
// first instance. template <size_t S>
template <size_t S> std::array<float,S>
std::array<float,S> roots (std::array<float,S+1>);
roots (std::array<float,S+1>);
template <size_t S, typename T, typename U> template <size_t S, typename T, typename U>
T T
eval (std::array<T,S>, U x); eval (std::array<T,S>, U x);
}
} }
#include "polynomial.ipp" #include "polynomial.ipp"

View File

@ -22,7 +22,7 @@
#define __UTIL_POLYNOMIAL_IPP #define __UTIL_POLYNOMIAL_IPP
//----------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T, typename U> template <size_t S, typename T, typename U>
T T
util::polynomial::eval (const std::array<T,S> coeffs, const U x) util::polynomial::eval (const std::array<T,S> coeffs, const U x)
@ -37,4 +37,3 @@ util::polynomial::eval (const std::array<T,S> coeffs, const U x)
return sum; return sum;
} }