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 <>
std::array<float,1>
roots (std::array<float,2> coeff)
@ -39,10 +39,11 @@ namespace util { namespace polynomial {
return { -b / a };
}
} }
}
//-----------------------------------------------------------------------------
namespace util { namespace polynomial {
namespace util::polynomial {
template <>
std::array<float,2>
roots (std::array<float,3> coeff)
@ -56,16 +57,18 @@ namespace util { namespace polynomial {
return { s[0], std::numeric_limits<float>::quiet_NaN () };
}
auto d = std::sqrt (pow2 (b) - 4 * a * c);
return { (-b - d) / (2 * a),
(-b + d) / (2 * a) };
auto descriminator = std::sqrt (pow2 (b) - 4 * a * c);
return {
(-b - descriminator) / (2 * a),
(-b + descriminator) / (2 * a)
};
}
} }
}
//-----------------------------------------------------------------------------
// From graphics gems: http://tog.acm.org/resources/GraphicsGems/gemsiv/vec_mat/ray/solver.c
namespace util { namespace polynomial {
namespace util::polynomial {
template <>
std::array<float,3>
roots (std::array<float,4> coeffs)
@ -151,4 +154,10 @@ namespace util { namespace polynomial {
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 <cstdlib>
namespace util {
namespace polynomial {
// 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
// first instance.
template <size_t S>
std::array<float,S>
roots (std::array<float,S+1>);
namespace util::polynomial {
// 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
// first instance.
template <size_t S>
std::array<float,S>
roots (std::array<float,S+1>);
template <size_t S, typename T, typename U>
T
eval (std::array<T,S>, U x);
}
template <size_t S, typename T, typename U>
T
eval (std::array<T,S>, U x);
}
#include "polynomial.ipp"

View File

@ -22,7 +22,7 @@
#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)
@ -37,4 +37,3 @@ util::polynomial::eval (const std::array<T,S> coeffs, const U x)
return sum;
}