📄 rational.qbk
字号:
[section:rational Polynomial and Rational Function Evaluation][h4 synopsis]``#include <boost/math/tools/rational.hpp>`` // Polynomials: template <std::size_t N, class T, class V> V evaluate_polynomial(const T(&poly)[N], const V& val); template <std::size_t N, class T, class V> V evaluate_polynomial(const boost::array<T,N>& poly, const V& val); template <class T, class U> U evaluate_polynomial(const T* poly, U z, std::size_t count); // Even polynomials: template <std::size_t N, class T, class V> V evaluate_even_polynomial(const T(&poly)[N], const V& z); template <std::size_t N, class T, class V> V evaluate_even_polynomial(const boost::array<T,N>& poly, const V& z); template <class T, class U> U evaluate_even_polynomial(const T* poly, U z, std::size_t count); // Odd polynomials template <std::size_t N, class T, class V> V evaluate_odd_polynomial(const T(&a)[N], const V& z); template <std::size_t N, class T, class V> V evaluate_odd_polynomial(const boost::array<T,N>& a, const V& z); template <class T, class U> U evaluate_odd_polynomial(const T* poly, U z, std::size_t count); // Rational Functions: template <std::size_t N, class T, class V> V evaluate_rational(const T(&a)[N], const T(&b)[N], const V& z); template <std::size_t N, class T, class V> V evaluate_rational(const boost::array<T,N>& a, const boost::array<T,N>& b, const V& z); template <class T, class U, class V> V evaluate_rational(const T* num, const U* denom, V z, unsigned count);[h4 Description]Each of the functions come in three variants: a pair of overloaded functionswhere the order of the polynomial or rational function is evaluated atcompile time, and an overload that accepts a runtime variable for the sizeof the coefficient array. Generally speaking, compile time evaluation of thearray size results in better type safety, is less prone to programmer errors,and may result in better optimised code. The polynomial evaluation functionsin particular, are specialised for various array sizes, allowing forloop unrolling, and one hopes, optimal inline expansion. template <std::size_t N, class T, class V> V evaluate_polynomial(const T(&poly)[N], const V& val); template <std::size_t N, class T, class V> V evaluate_polynomial(const boost::array<T,N>& poly, const V& val); template <class T, class U> U evaluate_polynomial(const T* poly, U z, std::size_t count);Evaluates the [@http://en.wikipedia.org/wiki/Polynomial polynomial] described bythe coefficients stored in /poly/.If the size of the array is specified at runtime, then the polynomial most have order /count-1/ with /count/ coefficients. Otherwise it hasorder /N-1/ with /N/ coefficients.Coefficients should be stored such that the coefficients for the x[super i ] termsare in poly[i].The types of the coefficients and of variable/z/ may differ as long as /*poly/ is convertible to type /U/.This allows, for example, for the coefficient tableto be a table of integers if this is appropriate. template <std::size_t N, class T, class V> V evaluate_even_polynomial(const T(&poly)[N], const V& z); template <std::size_t N, class T, class V> V evaluate_even_polynomial(const boost::array<T,N>& poly, const V& z); template <class T, class U> U evaluate_even_polynomial(const T* poly, U z, std::size_t count);As above, but evaluates an even polynomial: one where all the powersof /z/ are even numbers. Equivalent to calling `evaluate_polynomial(poly, z*z, count)`. template <std::size_t N, class T, class V> V evaluate_odd_polynomial(const T(&a)[N], const V& z); template <std::size_t N, class T, class V> V evaluate_odd_polynomial(const boost::array<T,N>& a, const V& z); template <class T, class U> U evaluate_odd_polynomial(const T* poly, U z, std::size_t count);As above but evaluates a polynomial where all the powers are odd numbers.Equivalent to `evaluate_polynomial(poly+1, z*z, count-1) * z + poly[0]`. template <std::size_t N, class T, class U, class V> V evaluate_rational(const T(&num)[N], const U(&denom)[N], const V& z); template <std::size_t N, class T, class U, class V> V evaluate_rational(const boost::array<T,N>& num, const boost::array<U,N>& denom, const V& z); template <class T, class U, class V> V evaluate_rational(const T* num, const U* denom, V z, unsigned count); Evaluates the rational function (the ratio of two polynomials) described bythe coefficients stored in /num/ and /demom/.If the size of the array is specified at runtime then both polynomials most have order /count-1/ with /count/ coefficients.Otherwise both polynomials have order /N-1/ with /N/ coefficients.Array /num/ describes the numerator, and /demon/ the denominator.Coefficients should be stored such that the coefficients for the x[super i ] termsare in num[i] and denom[i].The types of the coefficients and of variable/v/ may differ as long as /*num/ and /*denom/ are convertible to type /V/.This allows, for example, for one or both of the coefficient tablesto be a table of integers if this is appropriate.These functions are designed to safely evaluate the result, even when the value/z/ is very large. As such they do not take advantage of compile time arraysizes to make any optimisations. These functions are best reserved for situationswhere /z/ may be large: if you can be sure that numerical overflow will not occurthen polynomial evaluation with compile-time array sizes may offer slightlybetter performance.[h4 Implementation]Polynomials are evaluated by [@http://en.wikipedia.org/wiki/Horner_algorithm Horners method]. If the array size is known atcompile time then the functions dispatch to size-specific implementationsthat unroll the evaluation loop.Rational evaluation is by [@http://en.wikipedia.org/wiki/Horner_algorithm Horners method]:with the two polynomials being evaluatedin parallel to make the most of the processors floating-point pipeline.If /v/ is greater than one, then the polynomials are evaluated in reverseorder as polynomials in ['1\/v]: this avoids unnecessary numerical overflow when thecoefficients are large.Both the polynomial and rational function evaluation algorithms can betuned using various configuration macros to provide optimal performancefor a particular combination of compiler and platform. This includessupport for second-order Horner's methods. The various options are[link math_toolkit.perf.tuning documented here]. However, the performancebenefits to be gained from these are marginal on most current hardware,consequently it's best to run the [link math_toolkit.perf.perf_test_app performance test application] beforechanging the default settings.[endsect][/section:rational Polynomial and Rational Function Evaluation][/ Copyright 2006 John Maddock and Paul A. Bristow. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -