File: number_series.qbk

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (360 lines) | stat: -rw-r--r-- 13,328 bytes parent folder | download | duplicates (8)
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
[section:number_series Number Series]

[section:bernoulli_numbers Bernoulli Numbers]

[@https://en.wikipedia.org/wiki/Bernoulli_number Bernoulli numbers]
are a sequence of rational numbers useful for the Taylor series expansion,
Euler-Maclaurin formula, and the Riemann zeta function.

Bernoulli numbers are used in evaluation of some Boost.Math functions,
including the __tgamma, __lgamma and polygamma functions.

[h4 Single Bernoulli number]

[h4 Synopsis]

``
#include <boost/math/special_functions/bernoulli.hpp>
``

  namespace boost { namespace math {

  template <class T>
  T bernoulli_b2n(const int n);  // Single Bernoulli number (default policy).

  template <class T, class Policy>
  T bernoulli_b2n(const int n, const Policy &pol); // User policy for errors etc.

  }} // namespaces

[h4 Description]

Both return the (2 * n)[super th] Bernoulli number B[sub 2n].

Note that since all odd numbered Bernoulli numbers are zero (apart from B[sub 1] which is -[frac12])
the interface will only return the even numbered Bernoulli numbers.

This function uses fast table lookup for low-indexed Bernoulli numbers, while larger values are calculated
as needed and then cached.  The caching mechanism requires a certain amount of thread safety code, so
`unchecked_bernoulli_b2n` may provide a better interface for performance critical code.

The final __Policy argument is optional and can be used to control the behaviour of the function:
how it handles errors, what level of precision to use, etc.

Refer to __policy_section for more details.

[h4 Examples]

[import ../../example/bernoulli_example.cpp]
[bernoulli_example_1]

[bernoulli_output_1]

[h4 Single (unchecked) Bernoulli number]

[h4 Synopsis]
``
#include <boost/math/special_functions/bernoulli.hpp>

``

  template <>
  struct max_bernoulli_b2n<T>;

  template<class T>
  inline T unchecked_bernoulli_b2n(unsigned n);

`unchecked_bernoulli_b2n` provides access to Bernoulli numbers [*without any checks for overflow or invalid parameters].
It is implemented as a direct (and very fast) table lookup, and while not recommended for general use it can be useful
inside inner loops where the ultimate performance is required, and error checking is moved outside the loop.

The largest value you can pass to `unchecked_bernoulli_b2n<>` is `max_bernoulli_b2n<>::value`: passing values greater than
that will result in a buffer overrun error, so it's clearly important to place the error handling in your own code
when using this direct interface.

The value of `boost::math::max_bernoulli_b2n<T>::value` varies by the type T, for types `float`/`double`/`long double`
it's the largest value which doesn't overflow the target type: for example, `boost::math::max_bernoulli_b2n<double>::value` is 129.
However, for multiprecision types, it's the largest value for which the result can be represented as the ratio of two 64-bit
integers, for example `boost::math::max_bernoulli_b2n<boost::multiprecision::cpp_dec_float_50>::value` is just 17.  Of course
larger indexes can be passed to `bernoulli_b2n<T>(n)`, but then you lose fast table lookup (i.e. values may need to be calculated).

[bernoulli_example_4]
[bernoulli_output_4]

[h4 Multiple Bernoulli Numbers]

[h4 Synopsis]

``
#include <boost/math/special_functions/bernoulli.hpp>
``

  namespace boost { namespace math {

  // Multiple Bernoulli numbers (default policy).
  template <class T, class OutputIterator>
  OutputIterator bernoulli_b2n(
    int start_index,
    unsigned number_of_bernoullis_b2n,
    OutputIterator out_it);

  // Multiple Bernoulli numbers (user policy).
  template <class T, class OutputIterator, class Policy>
  OutputIterator bernoulli_b2n(
    int start_index,
    unsigned number_of_bernoullis_b2n,
    OutputIterator out_it,
    const Policy& pol);
  }} // namespaces

[h4 Description]

Two versions of the Bernoulli number function are provided to compute multiple Bernoulli numbers
with one call (one with default policy and the other allowing a user-defined policy).

These return a series of Bernoulli numbers:

[expression [B[sub 2*start_index], B[sub 2*(start_index+1)], ..., B[sub 2*(start_index+number_of_bernoullis_b2n-1)]]]
 
[h4 Examples]
[bernoulli_example_2]
[bernoulli_output_2]
[bernoulli_example_3]
[bernoulli_output_3]

The source of this example is at [@../../example/bernoulli_example.cpp bernoulli_example.cpp]

[h4 Accuracy]

All the functions usually return values within one ULP (unit in the last place) for the floating-point type.

[h4 Implementation]

The implementation details are in [@../../include/boost/math/special_functions/detail/bernoulli_details.hpp bernoulli_details.hpp]
and [@../../include/boost/math/special_functions/detail/unchecked_bernoulli.hpp unchecked_bernoulli.hpp].

For `i <= max_bernoulli_index<T>::value` this is implemented by simple table lookup from a statically initialized table;
for larger values of `i`, this is implemented by the Tangent Numbers algorithm as described in the paper:
Fast Computation of Bernoulli, Tangent and Secant Numbers, Richard P. Brent and David Harvey,
[@http://arxiv.org/pdf/1108.0286v3.pdf] (2011).

[@http://mathworld.wolfram.com/TangentNumber.html Tangent (or Zag) numbers]
(an even alternating permutation number) are defined
and their generating function is also given therein.

The relation of Tangent numbers with Bernoulli numbers  ['B[sub i]]
is given by Brent and Harvey's equation 14:

__spaces[equation tangent_numbers]

Their relation with Bernoulli numbers ['B[sub i]] are defined by

if i > 0 and i is even then  [equation bernoulli_numbers] [br]
elseif i == 0 then  ['B[sub i]] = 1 [br]
elseif i == 1 then  ['B[sub i]] = -1/2 [br]
elseif i < 0 or i is odd then ['B[sub i]] = 0

Note that computed values are stored in a fixed-size table, access is thread safe via atomic operations (i.e. lock
free programming), this imparts a much lower overhead on access to cached values than might otherwise be expected - 
typically for multiprecision types the cost of thread synchronisation is negligible, while for built in types
this code is not normally executed anyway.  For very large arguments which cannot be reasonably computed or
stored in our cache, an asymptotic expansion [@http://www.luschny.de/math/primes/bernincl.html due to Luschny] is used:

[equation bernoulli_numbers2]

[endsect] [/section:bernoulli_numbers Bernoulli Numbers]


[section:tangent_numbers Tangent Numbers]

[@http://en.wikipedia.org/wiki/Tangent_numbers Tangent numbers],
also called a zag function.  See also
[@http://mathworld.wolfram.com/TangentNumber.html Tangent number].

The first few values are 1, 2, 16, 272, 7936, 353792, 22368256, 1903757312 ...
(sequence [@http://oeis.org/A000182 A000182 in OEIS]).
They are called tangent numbers because they appear as
numerators in the Maclaurin series of `tan(x)`.

[*Important:] there are two competing definitions of Tangent numbers in common use 
(depending on whether you take the even or odd numbered values as non-zero), we use:

[equation tangent_number_def]

Which gives:

[equation tangent_number_def2]

Tangent numbers are used in the computation of Bernoulli numbers,
but are also made available here.

[h4 Synopsis]
``
#include <boost/math/special_functions/detail/bernoulli.hpp>
``

  template <class T>
  T tangent_t2n(const int i);  // Single tangent number (default policy).

  template <class T, class Policy>
  T tangent_t2n(const int i, const Policy &pol); // Single tangent number (user policy).

  // Multiple tangent numbers (default policy).
  template <class T, class OutputIterator>
  OutputIterator tangent_t2n(const int start_index,
                                      const unsigned number_of_tangent_t2n,
                                      OutputIterator out_it);

  // Multiple tangent numbers (user policy).
  template <class T, class OutputIterator, class Policy>
  OutputIterator tangent_t2n(const int start_index,
                                      const unsigned number_of_tangent_t2n,
                                      OutputIterator out_it,
                                      const Policy& pol);

[h4 Examples]

[tangent_example_1]

The output is:
[tangent_output_1]

The source of this example is at [@../../example/bernoulli_example.cpp bernoulli_example.cpp]

[h4 Implementation]

Tangent numbers are calculated as intermediates in the calculation of the __bernoulli_numbers:
refer to the __bernoulli_numbers documentation for details.

[endsect] [/section:tangent_numbers Tangent Numbers]

[section:primes Prime Numbers]

[h4 Synopsis]

``
#include <boost/math/special_functions/prime.hpp>
``

  namespace boost { namespace math {

  template <class Policy>
  constexpr std::uint32_t prime(unsigned n, const Policy& pol);

  constexpr std::uint32_t prime(unsigned n);

  static const unsigned max_prime = 10000;

  }} // namespaces

[h4 Description]

The function `prime` provides fast table lookup to the first 10000 prime numbers (starting from 2
as the zeroth prime: as 1 isn't terribly useful in practice).  There are two function signatures
one of which takes an optional __Policy as the second parameter to control error handling.

The constant `max_prime` is the largest value you can pass to `prime` without incurring an error.

Passing a value greater than `max_prime` results in a __domain_error being raised.

This function is `constexpr` only if the compiler supports C++14 constexpr functions.

[endsect] [/section:primes]

[section:fibonacci_numbers Fibonacci Numbers]

[@https://en.wikipedia.org/wiki/Fibonacci_number Fibonacci numbers] (F[sub n]) follows the linear recurrence F[sub n]=F[sub n-1]+F[sub n-2] starting with F[sub 0]=0, F[sub 1]=1.

[h4 Synopsis]

``
#include <boost/math/special_functions/fibonacci.hpp>
``

  namespace boost { namespace math {

  template <class T, class Policy>
  constexpr T fibonacci(unsigned long long n) // Checked version (default policy)

  template <class T, class Policy>
  constexpr T fibonacci(unsigned long long n, const Policy &pol) // Checked version (policy for errors etc.)

  template <typename T>
  constexpr T unchecked_fibonacci(unsigned long long n) noexcept(std::is_fundamental<T>::value); {  // Unchecked version (no policy).

  }} // namespaces

The functions return F[sub n] for a given input [^n] having type [^T].

[h4 Description]
The checked versions checks for the overflow before starting the computation. If [^n] is so large that the result can not
be represented in type [^T], it then calls __overflow_error. The overflow check is susceptible to off-by-one errors around the overflow limit. See Binet's Formula below for more details on the check.

These functions are all `constexpr` from C++14 onwards, and in addition `unchecked_fibonacci` is `noexcept` when T is a fundamental type.

[optional_policy]

If in the checked version, the overflow check succeeds then the unchecked version is called which computes the desired F[sub n]. Checked version is slower because of the overhead involved in overflow check.

[h4 Generator]

``
#include <boost/math/special_functions/fibonacci.hpp>
``

   template <typename T>
   class fibonacci_generator {
     // returns consecutive fibonacci number starting from 0, 1, 1, 2, ...
     T operator()();
     // reset the generator to start from nth fibonacci number
     void set(unsigned long long n);
   }

The generator returns consecutive fibonacci numbers starting with 0, 1, 1, 2...

The state of the generator can be modified using [^set()] which makes generator return consecutive fibonacci numbers starting from [^n][sup th] fibonacci number.

   boost::math::fibonacci_generator<int> gen;
   int x = gen(); // x is 0
   int y = gen(); // y is 1
   for(int i = 0; i < 5; ++i) // this loop is same as gen.set(7);
       gen();
   int z = gen(); // z is 13 


Generator is non-throwing for all fundamental types and will not check for overflows.

[h4 Type Requirements]

The type must be an arithmetic type supporting +, -, * and can be initialized from trivial integral types.

[h4 Example]

The file [@../../example/reciprocal_fibonacci_constant.cpp reciprocal_fibonacci_constant.cpp] uses `fibonacci_generator` to calculate the
reciprocal fibonacci constant to 1000 digit precision like so:

[import ../../example/reciprocal_fibonacci_constant.cpp]

[fibonacci_eg]

[h4 Implementation]

The time complexity for computing fibonacci number is O(log n), without considering the time complexities of multiplication and addition (where [^n] is the input parameter).
The implementation is iterative version of [@http://www.cs.utexas.edu/users/EWD/ewd06xx/EWD654.PDF Dijkstra's identities] and which simply walks down the bits of [^n].

[h4:binet Binet's Formula]

There is a closed form expression for computing fibonacci numbers but since it suffers from imprecision issues (using floating point computation), it is not implemented.

However an approximate formula is used for the overflow checking in the checked version.

[endsect] [/Fibonacci Numbers]

[endsect] [/Number Series]

[/
  Copyright 2013, 2014 Nikhar Agrawal, Christopher Kormanyos, John Maddock, Paul A. Bristow.
  Copyright 2020 Madhur Chauhan.  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).
]