File: factorials.qbk

package info (click to toggle)
boost1.35 1.35.0-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 203,856 kB
  • ctags: 337,867
  • sloc: cpp: 938,683; xml: 56,847; ansic: 41,589; python: 18,999; sh: 11,566; makefile: 664; perl: 494; yacc: 456; asm: 353; csh: 6
file content (301 lines) | stat: -rw-r--r-- 7,360 bytes parent folder | download | duplicates (2)
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
[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>
   T unchecked_factorial(unsigned i);
   
   template <class T>
   struct max_factorial;

   }} // namespaces

[h4 Description]

   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>
   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.

[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_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.

[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 functions.wolfram.com.

[h4 Implementation]

The double factorial is implemented in terms of the factorial and gamma
functions using the relations:

(2n)!! = 2[super n ] * n!

(2n+1)!! = (2n+1)! / (2[super n ] n!)

and

(2n-1)!! = [Gamma]((2n+1)/2) * 2[super n ] / sqrt(pi)

[endsect]

[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/:

rising_factorial(x, i) = [Gamma](x + i) / [Gamma](x);

or

rising_factorial(x, i) = x(x+1)(x+2)(x+3)...(x+i)
                          
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_pomotion_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 functions.wolfram.com.

[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]

``
#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/:

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_pomotion_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 
functions.wolfram.com.

[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_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.
   
[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 functions.wolfram.com.

[h4 Implementation]

Binomial coefficients are calculated using table lookup of factorials
where possible using:

[sub n]C[sub k] = n! / (k!(n-k)!)

Otherwise it is implemented in terms of the beta function using the relations:

[sub n]C[sub k] = 1 / (k * __beta(k, n-k+1))

and

[sub n]C[sub k] = 1 / ((n-k) * __beta(k+1, n-k))

[endsect]


[endsect][/section:factorials Factorials]

[/ 
  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).
]