QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
primitivepolynomials.hpp
Go to the documentation of this file.
1/* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/* this file is a slightly edited version of
4 * PrimitivePolynomialsModuloTwoUpToDegree27.h
5 * © 2002 "Monte Carlo Methods in Finance"
6 * as provided ready for compilation in the directory
7 * "PrimitivePolynomialsModuloTwo" on the CD accompanying the book
8 * "Monte Carlo Methods in Finance" by Peter Jäckel.
9 *
10 * ===========================================================================
11 * NOTE: The following copyright notice applies to the original code,
12 *
13 * Copyright (C) 2002 Peter Jäckel "Monte Carlo Methods in Finance".
14 * All rights reserved.
15 *
16 * Permission to use, copy, modify, and distribute this software is freely
17 * granted, provided that this notice is preserved.
18 * ===========================================================================
19 */
20#ifndef primitivepolynomials_hpp
21#define primitivepolynomials_hpp
22
23/* This file is provided for the use with Sobol' sequences of higher
24 * dimensions. The dimensionality of the Sobol' sequence can be extended to
25 * virtually any size you ever might need by the aid of the table of
26 * primitive polynomials modulo two.
27 * It is up to you to define a macro PPMT_MAX_DIM to a positive integer
28 * less than or equal to 8129334. If you don't define it, it will be set
29 * below to N_PRIMITIVES_UP_TO_DEGREE_18 which is 21200. That's how many
30 * primitive polynomials are provided by the standard primitivepolynomial.c
31 * distributed with QuantLib and that will be compiled into a static array.
32 * Should you need more, get the original version of primitivepolynomial.c
33 * as provided ready for compilation in the directory
34 * "PrimitivePolynomialsModuloTwo" on the CD accompanying the book
35 * "Monte Carlo Methods in Finance" by Peter Jäckel.
36 * The file provides polynomials up to degree 27
37 * for a grand total of 8129334 dimensions.
38 * Since 8129334 longs compile into an object file of at least 32517336 byte
39 * size (in fact, gcc -c -O0 PrimitivePolynomialsModuloTwoUpToDegree27.c
40 * produced a file PrimitivePolynomialsModuloTwoUpToDegree27.o with 32519920
41 * bytes), it is recommended to only compile as many as you may ever need.
42 * Worse even than the output file size is the virtual memory requirement
43 * for the compilation. For Visual C++ 6 you will need to use the /Zm compiler
44 * option to set the compiler's memory allocation limit (/Zm1500 should work)
45 * So really only take the maximum of what you think you might ever need.
46 * After all, you can always recompile with more, should you need it.
47 */
48
49/* PPMT : Primitive Polynomials Modulo Two
50 *
51 *
52 * The encoding is as follows:
53 *
54 * The coefficients of each primitive polynomial are the bits of the given
55 * integer. The leading and trailing coefficients, which are 1 for all of the
56 * polynomials, have been omitted.
57 *
58 * Example: The polynomial
59 *
60 * 4 2
61 * x + x + 1
62 *
63 * is encoded as 2 in the array of polynomials of degree 4 because the
64 * binary sequence of coefficients
65 *
66 * 10101
67 *
68 * becomes
69 *
70 * 0101
71 *
72 * after stripping off the top bit, and this is converted to
73 *
74 * 010
75 *
76 * by right-shifting and losing the rightmost bit. Similarly, we have
77 *
78 * 5 4 2
79 * x + x + x + x + 1
80 *
81 * encoded as 13 [ (1)1101(1) ] in the array for degree 5.
82 */
83
84/* Example: replace primitivepolynomials.cpp provided by QuantLib standard
85 * distribution with the 8129334 polinomials version and
86 * comment out the line below if you want absolutely all of the
87 * provided primitive polynomials modulo two.
88 *
89 * #define PPMT_MAX_DIM 8129334
90 *
91 * Note that PPMT_MAX_DIM will be redefined to be the nearest equal or larger
92 * number of polynomials up to one of the predefined macros
93 * N_PRIMITIVES_UP_TO_DEGREE_XX
94 * below.
95 */
96
97
98#define N_PRIMITIVES_UP_TO_DEGREE_01 1
99#define N_PRIMITIVES_UP_TO_DEGREE_02 2
100#define N_PRIMITIVES_UP_TO_DEGREE_03 4
101#define N_PRIMITIVES_UP_TO_DEGREE_04 6
102#define N_PRIMITIVES_UP_TO_DEGREE_05 12
103#define N_PRIMITIVES_UP_TO_DEGREE_06 18
104#define N_PRIMITIVES_UP_TO_DEGREE_07 36
105#define N_PRIMITIVES_UP_TO_DEGREE_08 52
106#define N_PRIMITIVES_UP_TO_DEGREE_09 100
107#define N_PRIMITIVES_UP_TO_DEGREE_10 160
108#define N_PRIMITIVES_UP_TO_DEGREE_11 336
109#define N_PRIMITIVES_UP_TO_DEGREE_12 480
110#define N_PRIMITIVES_UP_TO_DEGREE_13 1110
111#define N_PRIMITIVES_UP_TO_DEGREE_14 1866
112#define N_PRIMITIVES_UP_TO_DEGREE_15 3666
113#define N_PRIMITIVES_UP_TO_DEGREE_16 5714
114#define N_PRIMITIVES_UP_TO_DEGREE_17 13424
115#define N_PRIMITIVES_UP_TO_DEGREE_18 21200
116#define N_PRIMITIVES_UP_TO_DEGREE_19 48794
117#define N_PRIMITIVES_UP_TO_DEGREE_20 72794
118#define N_PRIMITIVES_UP_TO_DEGREE_21 157466
119#define N_PRIMITIVES_UP_TO_DEGREE_22 277498
120#define N_PRIMITIVES_UP_TO_DEGREE_23 634458
121#define N_PRIMITIVES_UP_TO_DEGREE_24 910938
122#define N_PRIMITIVES_UP_TO_DEGREE_25 2206938
123#define N_PRIMITIVES_UP_TO_DEGREE_26 3926838
124#define N_PRIMITIVES_UP_TO_DEGREE_27 8129334
125
126#define N_PRIMITIVES N_PRIMITIVES_UP_TO_DEGREE_27
127
128#ifndef PPMT_MAX_DIM
129# define PPMT_MAX_DIM N_PRIMITIVES_UP_TO_DEGREE_18
130#endif
131
132#if PPMT_MAX_DIM > N_PRIMITIVES
133# error PPMT_MAX_DIM cannot be greater than N_PRIMITIVES
134#elif PPMT_MAX_DIM <= N_PRIMITIVES_UP_TO_DEGREE_01
135# undef PPMT_MAX_DIM
136# define PPMT_MAX_DIM N_PRIMITIVES_UP_TO_DEGREE_01
137# define N_MAX_DEGREE 01
138#elif PPMT_MAX_DIM <= N_PRIMITIVES_UP_TO_DEGREE_02
139# undef PPMT_MAX_DIM
140# define PPMT_MAX_DIM N_PRIMITIVES_UP_TO_DEGREE_02
141# define N_MAX_DEGREE 02
142#elif PPMT_MAX_DIM <= N_PRIMITIVES_UP_TO_DEGREE_03
143# undef PPMT_MAX_DIM
144# define PPMT_MAX_DIM N_PRIMITIVES_UP_TO_DEGREE_03
145# define N_MAX_DEGREE 03
146#elif PPMT_MAX_DIM <= N_PRIMITIVES_UP_TO_DEGREE_04
147# undef PPMT_MAX_DIM
148# define PPMT_MAX_DIM N_PRIMITIVES_UP_TO_DEGREE_04
149# define N_MAX_DEGREE 04
150#elif PPMT_MAX_DIM <= N_PRIMITIVES_UP_TO_DEGREE_05
151# undef PPMT_MAX_DIM
152# define PPMT_MAX_DIM N_PRIMITIVES_UP_TO_DEGREE_05
153# define N_MAX_DEGREE 05
154#elif PPMT_MAX_DIM <= N_PRIMITIVES_UP_TO_DEGREE_06
155# undef PPMT_MAX_DIM
156# define PPMT_MAX_DIM N_PRIMITIVES_UP_TO_DEGREE_06
157# define N_MAX_DEGREE 06
158#elif PPMT_MAX_DIM <= N_PRIMITIVES_UP_TO_DEGREE_07
159# undef PPMT_MAX_DIM
160# define PPMT_MAX_DIM N_PRIMITIVES_UP_TO_DEGREE_07
161# define N_MAX_DEGREE 07
162#elif PPMT_MAX_DIM <= N_PRIMITIVES_UP_TO_DEGREE_08
163# undef PPMT_MAX_DIM
164# define PPMT_MAX_DIM N_PRIMITIVES_UP_TO_DEGREE_08
165# define N_MAX_DEGREE 08
166#elif PPMT_MAX_DIM <= N_PRIMITIVES_UP_TO_DEGREE_09
167# undef PPMT_MAX_DIM
168# define PPMT_MAX_DIM N_PRIMITIVES_UP_TO_DEGREE_09
169# define N_MAX_DEGREE 09
170#elif PPMT_MAX_DIM <= N_PRIMITIVES_UP_TO_DEGREE_10
171# undef PPMT_MAX_DIM
172# define PPMT_MAX_DIM N_PRIMITIVES_UP_TO_DEGREE_10
173# define N_MAX_DEGREE 10
174#elif PPMT_MAX_DIM <= N_PRIMITIVES_UP_TO_DEGREE_11
175# undef PPMT_MAX_DIM
176# define PPMT_MAX_DIM N_PRIMITIVES_UP_TO_DEGREE_11
177# define N_MAX_DEGREE 11
178#elif PPMT_MAX_DIM <= N_PRIMITIVES_UP_TO_DEGREE_12
179# undef PPMT_MAX_DIM
180# define PPMT_MAX_DIM N_PRIMITIVES_UP_TO_DEGREE_12
181# define N_MAX_DEGREE 12
182#elif PPMT_MAX_DIM <= N_PRIMITIVES_UP_TO_DEGREE_13
183# undef PPMT_MAX_DIM
184# define PPMT_MAX_DIM N_PRIMITIVES_UP_TO_DEGREE_13
185# define N_MAX_DEGREE 13
186#elif PPMT_MAX_DIM <= N_PRIMITIVES_UP_TO_DEGREE_14
187# undef PPMT_MAX_DIM
188# define PPMT_MAX_DIM N_PRIMITIVES_UP_TO_DEGREE_14
189# define N_MAX_DEGREE 14
190#elif PPMT_MAX_DIM <= N_PRIMITIVES_UP_TO_DEGREE_15
191# undef PPMT_MAX_DIM
192# define PPMT_MAX_DIM N_PRIMITIVES_UP_TO_DEGREE_15
193# define N_MAX_DEGREE 15
194#elif PPMT_MAX_DIM <= N_PRIMITIVES_UP_TO_DEGREE_16
195# undef PPMT_MAX_DIM
196# define PPMT_MAX_DIM N_PRIMITIVES_UP_TO_DEGREE_16
197# define N_MAX_DEGREE 16
198#elif PPMT_MAX_DIM <= N_PRIMITIVES_UP_TO_DEGREE_17
199# undef PPMT_MAX_DIM
200# define PPMT_MAX_DIM N_PRIMITIVES_UP_TO_DEGREE_17
201# define N_MAX_DEGREE 17
202#elif PPMT_MAX_DIM <= N_PRIMITIVES_UP_TO_DEGREE_18
203# undef PPMT_MAX_DIM
204# define PPMT_MAX_DIM N_PRIMITIVES_UP_TO_DEGREE_18
205# define N_MAX_DEGREE 18
206#elif PPMT_MAX_DIM <= N_PRIMITIVES_UP_TO_DEGREE_19
207# undef PPMT_MAX_DIM
208# define PPMT_MAX_DIM N_PRIMITIVES_UP_TO_DEGREE_19
209# define N_MAX_DEGREE 19
210#elif PPMT_MAX_DIM <= N_PRIMITIVES_UP_TO_DEGREE_20
211# undef PPMT_MAX_DIM
212# define PPMT_MAX_DIM N_PRIMITIVES_UP_TO_DEGREE_20
213# define N_MAX_DEGREE 20
214#elif PPMT_MAX_DIM <= N_PRIMITIVES_UP_TO_DEGREE_21
215# undef PPMT_MAX_DIM
216# define PPMT_MAX_DIM N_PRIMITIVES_UP_TO_DEGREE_21
217# define N_MAX_DEGREE 21
218#elif PPMT_MAX_DIM <= N_PRIMITIVES_UP_TO_DEGREE_22
219# undef PPMT_MAX_DIM
220# define PPMT_MAX_DIM N_PRIMITIVES_UP_TO_DEGREE_22
221# define N_MAX_DEGREE 22
222#elif PPMT_MAX_DIM <= N_PRIMITIVES_UP_TO_DEGREE_23
223# undef PPMT_MAX_DIM
224# define PPMT_MAX_DIM N_PRIMITIVES_UP_TO_DEGREE_23
225# define N_MAX_DEGREE 23
226#elif PPMT_MAX_DIM <= N_PRIMITIVES_UP_TO_DEGREE_24
227# undef PPMT_MAX_DIM
228# define PPMT_MAX_DIM N_PRIMITIVES_UP_TO_DEGREE_24
229# define N_MAX_DEGREE 24
230#elif PPMT_MAX_DIM <= N_PRIMITIVES_UP_TO_DEGREE_25
231# undef PPMT_MAX_DIM
232# define PPMT_MAX_DIM N_PRIMITIVES_UP_TO_DEGREE_25
233# define N_MAX_DEGREE 25
234#elif PPMT_MAX_DIM <= N_PRIMITIVES_UP_TO_DEGREE_26
235# undef PPMT_MAX_DIM
236# define PPMT_MAX_DIM N_PRIMITIVES_UP_TO_DEGREE_26
237# define N_MAX_DEGREE 26
238#else
239# undef PPMT_MAX_DIM
240# define PPMT_MAX_DIM N_PRIMITIVES_UP_TO_DEGREE_27
241# define N_MAX_DEGREE 27
242#endif
243
244/* Microsoft Visual C++ 6.0 */
245#if defined(_MSC_VER)
246 /* disable useless warning C4049
247 compiler limit : terminating line number emission
248 No line number support is available for file with more
249 than 64K source lines. */
250 #pragma warning(disable: 4049)
251#endif
252
253extern
254
255#ifdef __cplusplus
256
257"C"
258
259#endif
260
261/*! You can access the following array as in PrimitivePolynomials[i][j]
262 with i and j counting from 0 in C convention. PrimitivePolynomials[i][j]
263 will get you the j-th (counting from zero) primitive polynomial of degree
264 i+1. Each one-dimensional array of primitive polynomials of a given
265 degree is terminated with an entry of -1. Accessing beyond this entry
266 will result in a memory violation and must be avoided. */
267const long *const PrimitivePolynomials[N_MAX_DEGREE];
268
269#endif
#define N_MAX_DEGREE
const long *const PrimitivePolynomials[18]