1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
[section:factorials Factorials and Binomial Coefficients]
[section:sf_factorial Factorial]
[h4 Synopsis]
``
#include <boost/math/special_functions/factorials.hpp>
``
namespace boost{ namespace math{
template <class T>
T factorial(unsigned i);
template <class T, class ``__Policy``>
T factorial(unsigned i, const ``__Policy``&);
template <class T>
constexpr T unchecked_factorial(unsigned i);
template <class T>
struct max_factorial;
}} // namespaces
[h4 Description]
[important
The functions described below are templates where the template argument T CANNOT be deduced from the
arguments passed to the function. Therefore if you write something like:
`boost::math::factorial(2);`
You will get a (perhaps perplexing) compiler error, usually indicating that there is no such function to be found.
Instead you need to specify the return type explicitly and write:
`boost::math::factorial<double>(2);`
So that the return type is known.
Furthermore, the template argument must be a real-valued type such as `float` or `double`
and not an integer type - that would overflow far too easily for quite small values of parameter `i`!
The source code `static_assert` and comment just after the will be:
``
static_assert(!std::is_integral<T>::value, "Type T must not be an integral type");
// factorial<unsigned int>(n) is not implemented
// because it would overflow integral type T for too small n
// to be useful. Use instead a floating-point type,
// and convert to an unsigned type if essential, for example:
// unsigned int nfac = static_cast<unsigned int>(factorial<double>(n));
// See factorial documentation for more detail.
``
]
template <class T>
T factorial(unsigned i);
template <class T, class ``__Policy``>
T factorial(unsigned i, const ``__Policy``&);
Returns [^i!].
[optional_policy]
For [^i <= max_factorial<T>::value] this is implemented by table lookup,
for larger values of [^i], this function is implemented in terms of __tgamma.
If [^i] is so large that the result can not be represented in type T, then
calls __overflow_error.
template <class T>
constexpr T unchecked_factorial(unsigned i);
Returns [^i!].
Internally this function performs table lookup of the result. Further it performs
no range checking on the value of i: it is up to the caller to ensure
that [^i <= max_factorial<T>::value]. This function is intended to be used
inside inner loops that require fast table lookup of factorials, but requires
care to ensure that argument [^i] never grows too large.
template <class T>
struct max_factorial
{
static const unsigned value = X;
};
This traits class defines the largest value that can be passed to
[^unchecked_factorial]. The member `value` can be used where integral
constant expressions are required: for example to define the size of
further tables that depend on the factorials.
This function is `constexpr` only if the compiler supports C++14 constexpr functions.
[h4 Accuracy]
For arguments smaller than `max_factorial<T>::value`
the result should be
correctly rounded. For larger arguments the accuracy will be the same
as for __tgamma.
[h4 Testing]
Basic sanity checks and spot values to verify the data tables:
the main tests for the __tgamma function handle those cases already.
[h4 Implementation]
The factorial function is table driven for small arguments, and is
implemented in terms of __tgamma for larger arguments.
[endsect] [/section:sf_factorial Factorial]
[section:sf_double_factorial Double Factorial]
``
#include <boost/math/special_functions/factorials.hpp>
``
namespace boost{ namespace math{
template <class T>
T double_factorial(unsigned i);
template <class T, class ``__Policy``>
T double_factorial(unsigned i, const ``__Policy``&);
}} // namespaces
Returns [^i!!].
[optional_policy]
May return the result of __overflow_error if the result is too large
to represent in type T. The implementation is designed to be optimised
for small /i/ where table lookup of i! is possible.
[important
The functions described above are templates where the template argument T can not be deduced from the
arguments passed to the function. Therefore if you write something like:
`boost::math::double_factorial(2);`
You will get a (possibly perplexing) compiler error, usually indicating that there is no such function to be found. Instead you need to specify
the return type explicitly and write:
`boost::math::double_factorial<double>(2);`
So that the return type is known. Further, the template argument must be a real-valued type such as `float` or `double`
and not an integer type - that would overflow far too easily!
The source code `static_assert` and comment just after the will be:
``
static_assert(!std::is_integral<T>::value, "Type T must not be an integral type");
// factorial<unsigned int>(n) is not implemented
// because it would overflow integral type T for too small n
// to be useful. Use instead a floating-point type,
// and convert to an unsigned type if essential, for example:
// unsigned int nfac = static_cast<unsigned int>(factorial<double>(n));
// See factorial documentation for more detail.
``
]
[note The argument to `double_factorial` is type `unsigned` even though technically -1!! is defined.]
[h4 Accuracy]
The implementation uses a trivial adaptation of
the factorial function, so error rates should be no more than a couple
of epsilon higher.
[h4 Testing]
The spot tests for the double factorial use data generated by __WolframAlpha.
[h4 Implementation]
The double factorial is implemented in terms of the factorial and gamma
functions using the relations:
[expression ['(2n)!! = 2[super n ] * n!]]
[expression ['(2n+1)!! = (2n+1)! / (2[super n ] n!)]]
and
[expression ['(2n-1)!! = [Gamma]((2n+1)/2) * 2[super n ] / sqrt(pi)]]
[endsect] [/section:sf_double_factorial Double Factorial]
[section:sf_rising_factorial Rising Factorial]
``
#include <boost/math/special_functions/factorials.hpp>
``
namespace boost{ namespace math{
template <class T>
``__sf_result`` rising_factorial(T x, int i);
template <class T, class ``__Policy``>
``__sf_result`` rising_factorial(T x, int i, const ``__Policy``&);
}} // namespaces
Returns the rising factorial of /x/ and /i/:
[expression ['rising_factorial(x, i) = [Gamma](x + i) / [Gamma](x)]]
or
[expression ['rising_factorial(x, i) = x(x+1)(x+2)(x+3)...(x+i-1)]]
Note that both /x/ and /i/ can be negative as well as positive.
[optional_policy]
May return the result of __overflow_error if the result is too large
to represent in type T.
The return type of these functions is computed using the __arg_promotion_rules:
the type of the result is `double` if T is an integer type, otherwise the type
of the result is T.
[h4 Accuracy]
The accuracy will be the same as
the __tgamma_delta_ratio function.
[h4 Testing]
The spot tests for the rising factorials use data generated by __Wolfram_functions.
[h4 Implementation]
Rising and factorials are implemented as ratios of gamma functions using __tgamma_delta_ratio.
Optimisations for small integer arguments are handled internally by that function.
[endsect] [/section:sf_rising_factorial Rising Factorial]
[section:sf_falling_factorial Falling Factorial]
``
#include <boost/math/special_functions/factorials.hpp>
``
namespace boost{ namespace math{
template <class T>
``__sf_result`` falling_factorial(T x, unsigned i);
template <class T, class ``__Policy``>
``__sf_result`` falling_factorial(T x, unsigned i, const ``__Policy``&);
}} // namespaces
Returns the falling factorial of /x/ and /i/:
[expression ['falling_factorial(x, i) = x(x-1)(x-2)(x-3)...(x-i+1)]]
Note that this function is only defined for positive /i/, hence the
`unsigned` second argument. Argument /x/ can be either positive or
negative however.
[optional_policy]
May return the result of __overflow_error if the result is too large
to represent in type T.
The return type of these functions is computed using the __arg_promotion_rules:
the type of the result is `double` if T is an integer type, otherwise the type
of the result is T.
[h4 Accuracy]
The accuracy will be the same as
the __tgamma_delta_ratio function.
[h4 Testing]
The spot tests for the falling factorials use data generated by __Wolfram_functions.
[h4 Implementation]
Rising and falling factorials are implemented as ratios of gamma functions
using __tgamma_delta_ratio. Optimisations for
small integer arguments are handled internally by that function.
[endsect] [/section:sf_falling_factorial Falling Factorial]
[section:sf_binomial Binomial Coefficients]
``
#include <boost/math/special_functions/binomial.hpp>
``
namespace boost{ namespace math{
template <class T>
T binomial_coefficient(unsigned n, unsigned k);
template <class T, class ``__Policy``>
T binomial_coefficient(unsigned n, unsigned k, const ``__Policy``&);
}} // namespaces
Returns the binomial coefficient: [sub n]C[sub k].
Requires k <= n.
[optional_policy]
May return the result of __overflow_error if the result is too large
to represent in type T.
[important
The functions described above are templates where the template argument `T` can not be deduced from the
arguments passed to the function. Therefore if you write something like:
`boost::math::binomial_coefficient(10, 2);`
You will get a compiler error, usually indicating that there is no such function to be found. Instead you need to specify
the return type explicitly and write:
`boost::math::binomial_coefficient<double>(10, 2);`
So that the return type is known. Further, the template argument must be a real-valued type such as `float` or `double`
and not an integer type - that would overflow far too easily!
]
[h4 Accuracy]
The accuracy will be the same as for the
factorials for small arguments (i.e. no more than one or two epsilon),
and the __beta function for larger arguments.
[h4 Testing]
The spot tests for the binomial coefficients use data generated by __WolframAlpha.
[h4 Implementation]
Binomial coefficients are calculated using table lookup of factorials
where possible using:
[expression ['[sub n]C[sub k] = n! / (k!(n-k)!)]]
Otherwise it is implemented in terms of the beta function using the relations:
[expression ['[sub n]C[sub k] = 1 / (k * __beta(k, n-k+1))]]
and
[expression ['[sub n]C[sub k] = 1 / ((n-k) * __beta(k+1, n-k))]]
[endsect] [/section:sf_binomial Binomial Coefficients]
[endsect] [/section:factorials Factorials]
[/
Copyright 2006, 2010 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).
]
|