distribution 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,411 @@
1
+ # Added by John O. Woods, SciRuby project.
2
+ # Derived from GSL-1.9 source files in the specfunc/ and cheb/ dirs.
3
+
4
+ module Distribution
5
+ module MathExtension
6
+ # Adapted from GSL-1.9.
7
+ class ChebyshevSeries
8
+ DATA = {:lopx => [ 2.16647910664395270521272590407,
9
+ -0.28565398551049742084877469679,
10
+ 0.01517767255690553732382488171,
11
+ -0.00200215904941415466274422081,
12
+ 0.00019211375164056698287947962,
13
+ -0.00002553258886105542567601400,
14
+ 2.9004512660400621301999384544e-06,
15
+ -3.8873813517057343800270917900e-07,
16
+ 4.7743678729400456026672697926e-08,
17
+ -6.4501969776090319441714445454e-09,
18
+ 8.2751976628812389601561347296e-10,
19
+ -1.1260499376492049411710290413e-10,
20
+ 1.4844576692270934446023686322e-11,
21
+ -2.0328515972462118942821556033e-12,
22
+ 2.7291231220549214896095654769e-13,
23
+ -3.7581977830387938294437434651e-14,
24
+ 5.1107345870861673561462339876e-15,
25
+ -7.0722150011433276578323272272e-16,
26
+ 9.7089758328248469219003866867e-17,
27
+ -1.3492637457521938883731579510e-17,
28
+ 1.8657327910677296608121390705e-18 ],
29
+ :lopxmx => [ -1.12100231323744103373737274541,
30
+ 0.19553462773379386241549597019,
31
+ -0.01467470453808083971825344956,
32
+ 0.00166678250474365477643629067,
33
+ -0.00018543356147700369785746902,
34
+ 0.00002280154021771635036301071,
35
+ -2.8031253116633521699214134172e-06,
36
+ 3.5936568872522162983669541401e-07,
37
+ -4.6241857041062060284381167925e-08,
38
+ 6.0822637459403991012451054971e-09,
39
+ -8.0339824424815790302621320732e-10,
40
+ 1.0751718277499375044851551587e-10,
41
+ -1.4445310914224613448759230882e-11,
42
+ 1.9573912180610336168921438426e-12,
43
+ -2.6614436796793061741564104510e-13,
44
+ 3.6402634315269586532158344584e-14,
45
+ -4.9937495922755006545809120531e-15,
46
+ 6.8802890218846809524646902703e-16,
47
+ -9.5034129794804273611403251480e-17,
48
+ 1.3170135013050997157326965813e-17],
49
+ :gstar_a => [ 2.16786447866463034423060819465,
50
+ -0.05533249018745584258035832802,
51
+ 0.01800392431460719960888319748,
52
+ -0.00580919269468937714480019814,
53
+ 0.00186523689488400339978881560,
54
+ -0.00059746524113955531852595159,
55
+ 0.00019125169907783353925426722,
56
+ -0.00006124996546944685735909697,
57
+ 0.00001963889633130842586440945,
58
+ -6.3067741254637180272515795142e-06,
59
+ 2.0288698405861392526872789863e-06,
60
+ -6.5384896660838465981983750582e-07,
61
+ 2.1108698058908865476480734911e-07,
62
+ -6.8260714912274941677892994580e-08,
63
+ 2.2108560875880560555583978510e-08,
64
+ -7.1710331930255456643627187187e-09,
65
+ 2.3290892983985406754602564745e-09,
66
+ -7.5740371598505586754890405359e-10,
67
+ 2.4658267222594334398525312084e-10,
68
+ -8.0362243171659883803428749516e-11,
69
+ 2.6215616826341594653521346229e-11,
70
+ -8.5596155025948750540420068109e-12,
71
+ 2.7970831499487963614315315444e-12,
72
+ -9.1471771211886202805502562414e-13,
73
+ 2.9934720198063397094916415927e-13,
74
+ -9.8026575909753445931073620469e-14,
75
+ 3.2116773667767153777571410671e-14,
76
+ -1.0518035333878147029650507254e-14,
77
+ 3.4144405720185253938994854173e-15,
78
+ -1.0115153943081187052322643819e-15 ],
79
+ :gstar_b => [ 0.0057502277273114339831606096782,
80
+ 0.0004496689534965685038254147807,
81
+ -0.0001672763153188717308905047405,
82
+ 0.0000615137014913154794776670946,
83
+ -0.0000223726551711525016380862195,
84
+ 8.0507405356647954540694800545e-06,
85
+ -2.8671077107583395569766746448e-06,
86
+ 1.0106727053742747568362254106e-06,
87
+ -3.5265558477595061262310873482e-07,
88
+ 1.2179216046419401193247254591e-07,
89
+ -4.1619640180795366971160162267e-08,
90
+ 1.4066283500795206892487241294e-08,
91
+ -4.6982570380537099016106141654e-09,
92
+ 1.5491248664620612686423108936e-09,
93
+ -5.0340936319394885789686867772e-10,
94
+ 1.6084448673736032249959475006e-10,
95
+ -5.0349733196835456497619787559e-11,
96
+ 1.5357154939762136997591808461e-11,
97
+ -4.5233809655775649997667176224e-12,
98
+ 1.2664429179254447281068538964e-12,
99
+ -3.2648287937449326771785041692e-13,
100
+ 7.1528272726086133795579071407e-14,
101
+ -9.4831735252566034505739531258e-15,
102
+ -2.3124001991413207293120906691e-15,
103
+ 2.8406613277170391482590129474e-15,
104
+ -1.7245370321618816421281770927e-15,
105
+ 8.6507923128671112154695006592e-16,
106
+ -3.9506563665427555895391869919e-16,
107
+ 1.6779342132074761078792361165e-16,
108
+ -6.0483153034414765129837716260e-17 ],
109
+ :e11 => [ -16.11346165557149402600,
110
+ 7.79407277874268027690,
111
+ -1.95540581886314195070,
112
+ 0.37337293866277945612,
113
+ -0.05692503191092901938,
114
+ 0.00721107776966009185,
115
+ -0.00078104901449841593,
116
+ 0.00007388093356262168,
117
+ -0.00000620286187580820,
118
+ 0.00000046816002303176,
119
+ -0.00000003209288853329,
120
+ 0.00000000201519974874,
121
+ -0.00000000011673686816,
122
+ 0.00000000000627627066,
123
+ -0.00000000000031481541,
124
+ 0.00000000000001479904,
125
+ -0.00000000000000065457,
126
+ 0.00000000000000002733,
127
+ -0.00000000000000000108],
128
+ :e12 => [ -0.03739021479220279500,
129
+ 0.04272398606220957700,
130
+ -0.13031820798497005440,
131
+ 0.01441912402469889073,
132
+ -0.00134617078051068022,
133
+ 0.00010731029253063780,
134
+ -0.00000742999951611943,
135
+ 0.00000045377325690753,
136
+ -0.00000002476417211390,
137
+ 0.00000000122076581374,
138
+ -0.00000000005485141480,
139
+ 0.00000000000226362142,
140
+ -0.00000000000008635897,
141
+ 0.00000000000000306291,
142
+ -0.00000000000000010148,
143
+ 0.00000000000000000315 ],
144
+ :ae11 => [ 0.121503239716065790,
145
+ -0.065088778513550150,
146
+ 0.004897651357459670,
147
+ -0.000649237843027216,
148
+ 0.000093840434587471,
149
+ 0.000000420236380882,
150
+ -0.000008113374735904,
151
+ 0.000002804247688663,
152
+ 0.000000056487164441,
153
+ -0.000000344809174450,
154
+ 0.000000058209273578,
155
+ 0.000000038711426349,
156
+ -0.000000012453235014,
157
+ -0.000000005118504888,
158
+ 0.000000002148771527,
159
+ 0.000000000868459898,
160
+ -0.000000000343650105,
161
+ -0.000000000179796603,
162
+ 0.000000000047442060,
163
+ 0.000000000040423282,
164
+ -0.000000000003543928,
165
+ -0.000000000008853444,
166
+ -0.000000000000960151,
167
+ 0.000000000001692921,
168
+ 0.000000000000607990,
169
+ -0.000000000000224338,
170
+ -0.000000000000200327,
171
+ -0.000000000000006246,
172
+ 0.000000000000045571,
173
+ 0.000000000000016383,
174
+ -0.000000000000005561,
175
+ -0.000000000000006074,
176
+ -0.000000000000000862,
177
+ 0.000000000000001223,
178
+ 0.000000000000000716,
179
+ -0.000000000000000024,
180
+ -0.000000000000000201,
181
+ -0.000000000000000082,
182
+ 0.000000000000000017],
183
+ :ae12 => [ 0.582417495134726740,
184
+ -0.158348850905782750,
185
+ -0.006764275590323141,
186
+ 0.005125843950185725,
187
+ 0.000435232492169391,
188
+ -0.000143613366305483,
189
+ -0.000041801320556301,
190
+ -0.000002713395758640,
191
+ 0.000001151381913647,
192
+ 0.000000420650022012,
193
+ 0.000000066581901391,
194
+ 0.000000000662143777,
195
+ -0.000000002844104870,
196
+ -0.000000000940724197,
197
+ -0.000000000177476602,
198
+ -0.000000000015830222,
199
+ 0.000000000002905732,
200
+ 0.000000000001769356,
201
+ 0.000000000000492735,
202
+ 0.000000000000093709,
203
+ 0.000000000000010707,
204
+ -0.000000000000000537,
205
+ -0.000000000000000716,
206
+ -0.000000000000000244,
207
+ -0.000000000000000058],
208
+ :ae13 => [ -0.605773246640603460,
209
+ -0.112535243483660900,
210
+ 0.013432266247902779,
211
+ -0.001926845187381145,
212
+ 0.000309118337720603,
213
+ -0.000053564132129618,
214
+ 0.000009827812880247,
215
+ -0.000001885368984916,
216
+ 0.000000374943193568,
217
+ -0.000000076823455870,
218
+ 0.000000016143270567,
219
+ -0.000000003466802211,
220
+ 0.000000000758754209,
221
+ -0.000000000168864333,
222
+ 0.000000000038145706,
223
+ -0.000000000008733026,
224
+ 0.000000000002023672,
225
+ -0.000000000000474132,
226
+ 0.000000000000112211,
227
+ -0.000000000000026804,
228
+ 0.000000000000006457,
229
+ -0.000000000000001568,
230
+ 0.000000000000000383,
231
+ -0.000000000000000094,
232
+ 0.000000000000000023],
233
+ :ae14 => [ -0.18929180007530170,
234
+ -0.08648117855259871,
235
+ 0.00722410154374659,
236
+ -0.00080975594575573,
237
+ 0.00010999134432661,
238
+ -0.00001717332998937,
239
+ 0.00000298562751447,
240
+ -0.00000056596491457,
241
+ 0.00000011526808397,
242
+ -0.00000002495030440,
243
+ 0.00000000569232420,
244
+ -0.00000000135995766,
245
+ 0.00000000033846628,
246
+ -0.00000000008737853,
247
+ 0.00000000002331588,
248
+ -0.00000000000641148,
249
+ 0.00000000000181224,
250
+ -0.00000000000052538,
251
+ 0.00000000000015592,
252
+ -0.00000000000004729,
253
+ 0.00000000000001463,
254
+ -0.00000000000000461,
255
+ 0.00000000000000148,
256
+ -0.00000000000000048,
257
+ 0.00000000000000016,
258
+ -0.00000000000000005],
259
+ :erfc_xlt1 => [ 1.06073416421769980345174155056,
260
+ -0.42582445804381043569204735291,
261
+ 0.04955262679620434040357683080,
262
+ 0.00449293488768382749558001242,
263
+ -0.00129194104658496953494224761,
264
+ -0.00001836389292149396270416979,
265
+ 0.00002211114704099526291538556,
266
+ -5.23337485234257134673693179020e-7,
267
+ -2.78184788833537885382530989578e-7,
268
+ 1.41158092748813114560316684249e-8,
269
+ 2.72571296330561699984539141865e-9,
270
+ -2.06343904872070629406401492476e-10,
271
+ -2.14273991996785367924201401812e-11,
272
+ 2.22990255539358204580285098119e-12,
273
+ 1.36250074650698280575807934155e-13,
274
+ -1.95144010922293091898995913038e-14,
275
+ -6.85627169231704599442806370690e-16,
276
+ 1.44506492869699938239521607493e-16,
277
+ 2.45935306460536488037576200030e-18,
278
+ -9.29599561220523396007359328540e-19 ],
279
+ :erfc_x15 => [ 0.44045832024338111077637466616,
280
+ -0.143958836762168335790826895326,
281
+ 0.044786499817939267247056666937,
282
+ -0.013343124200271211203618353102,
283
+ 0.003824682739750469767692372556,
284
+ -0.001058699227195126547306482530,
285
+ 0.000283859419210073742736310108,
286
+ -0.000073906170662206760483959432,
287
+ 0.000018725312521489179015872934,
288
+ -4.62530981164919445131297264430e-6,
289
+ 1.11558657244432857487884006422e-6,
290
+ -2.63098662650834130067808832725e-7,
291
+ 6.07462122724551777372119408710e-8,
292
+ -1.37460865539865444777251011793e-8,
293
+ 3.05157051905475145520096717210e-9,
294
+ -6.65174789720310713757307724790e-10,
295
+ 1.42483346273207784489792999706e-10,
296
+ -3.00141127395323902092018744545e-11,
297
+ 6.22171792645348091472914001250e-12,
298
+ -1.26994639225668496876152836555e-12,
299
+ 2.55385883033257575402681845385e-13,
300
+ -5.06258237507038698392265499770e-14,
301
+ 9.89705409478327321641264227110e-15,
302
+ -1.90685978789192181051961024995e-15,
303
+ 3.50826648032737849245113757340e-16 ],
304
+ :erfc_x510 => [ 1.11684990123545698684297865808,
305
+ 0.003736240359381998520654927536,
306
+ -0.000916623948045470238763619870,
307
+ 0.000199094325044940833965078819,
308
+ -0.000040276384918650072591781859,
309
+ 7.76515264697061049477127605790e-6,
310
+ -1.44464794206689070402099225301e-6,
311
+ 2.61311930343463958393485241947e-7,
312
+ -4.61833026634844152345304095560e-8,
313
+ 8.00253111512943601598732144340e-9,
314
+ -1.36291114862793031395712122089e-9,
315
+ 2.28570483090160869607683087722e-10,
316
+ -3.78022521563251805044056974560e-11,
317
+ 6.17253683874528285729910462130e-12,
318
+ -9.96019290955316888445830597430e-13,
319
+ 1.58953143706980770269506726000e-13,
320
+ -2.51045971047162509999527428316e-14,
321
+ 3.92607828989125810013581287560e-15,
322
+ -6.07970619384160374392535453420e-16,
323
+ 9.12600607264794717315507477670e-17 ],
324
+ :sin => [ -0.3295190160663511504173,
325
+ 0.0025374284671667991990,
326
+ 0.0006261928782647355874,
327
+ -4.6495547521854042157541e-06,
328
+ -5.6917531549379706526677e-07,
329
+ 3.7283335140973803627866e-09,
330
+ 3.0267376484747473727186e-10,
331
+ -1.7400875016436622322022e-12,
332
+ -1.0554678305790849834462e-13,
333
+ 5.3701981409132410797062e-16,
334
+ 2.5984137983099020336115e-17,
335
+ -1.1821555255364833468288e-19],
336
+ }
337
+
338
+ PARAMS = {
339
+ :lopx => [20, -1, 1, 10],
340
+ :lopxmx => [19, -1, 1, 9],
341
+ :gstar_a => [29, -1, 1, 17],
342
+ :gstar_b => [29, -1, 1, 18],
343
+ :e11 => [18, -1, 1, 13],
344
+ :e12 => [15, -1, 1, 10],
345
+ :ae11 => [38, -1, 1, 20],
346
+ :ae12 => [24, -1, 1, 15],
347
+ :ae13 => [24, -1, 1, 15],
348
+ :ae14 => [25, -1, 1, 13],
349
+ :erfc_xlt1 => [19, -1, 1, 12],
350
+ :erfc_x15 => [24, -1, 1, 16],
351
+ :erfc_x510 => [19, -1, 1, 12],
352
+ :sin => [11, -1, 1, 11]
353
+ }
354
+
355
+ def initialize(coefficients, expansion_order, lower_interval_point, upper_interval_point, single_precision_order)
356
+ @c = coefficients.is_a?(Symbol) ? DATA[coefficients] : coefficients
357
+ @order = expansion_order
358
+ @lower_interval_point = lower_interval_point
359
+ @upper_interval_point = upper_interval_point
360
+ @single_precision_order = single_precision_order
361
+ end
362
+ #double * c; /* coefficients */
363
+ #int order; /* order of expansion */
364
+ #double a; /* lower interval point */
365
+ #double b; /* upper interval point */
366
+ #int order_sp; /* effective single precision order */
367
+
368
+ attr_reader :lower_interval_point, :upper_interval_point, :single_precision_order, :order, :c
369
+ def a; @lower_interval_point; end
370
+ def b; @upper_interval_point; end
371
+ def order_sp; @single_precision_order; end
372
+ def coefficients(idx); @c[idx]; end
373
+
374
+ class << self
375
+ # cheb_eval_e in specfunc/cheb_eval.c (gsl-1.9)
376
+ def evaluate series, x, with_error = false
377
+ cs = Math.const_get "#{series.to_s.upcase}_CS"
378
+ raise(ArgumentError, "Unrecognized series #{series.to_s}") if cs.nil?
379
+
380
+ d = 0.0
381
+ dd = 0.0
382
+ y = (2*x - cs.a - cs.b).quo(cs.b - cs.a)
383
+ y2 = 2*y
384
+
385
+ e = 0.0
386
+
387
+ cs.order.downto(1) do |j|
388
+ temp = d
389
+ d = y2*d - dd + cs.c[j]
390
+ e += (y2*temp).abs + dd.abs + cs.c[j].abs
391
+ dd = temp
392
+ end
393
+
394
+ begin
395
+ temp = d
396
+ d = y*d - dd + 0.5 * cs.c[0]
397
+ e += (y*temp).abs + dd.abs + 0.5*cs.c[0].abs
398
+ end
399
+
400
+ with_error ? [d, Float::EPSILON*e + (cs.c[cs.order])] : d
401
+ end
402
+ end
403
+
404
+ end
405
+
406
+ ChebyshevSeries::DATA.keys.each do |series|
407
+ Math.const_set "#{series.to_s.upcase}_CS", ChebyshevSeries.new(series, *(ChebyshevSeries::PARAMS[series]))
408
+ end
409
+
410
+ end
411
+ end
@@ -0,0 +1,79 @@
1
+ # Added by John O. Woods, SciRuby project.
2
+ # Derived from GSL-1.9 source files in the specfunc/ dir.
3
+
4
+ module Distribution
5
+ module MathExtension
6
+
7
+ # Error function from GSL-1.9, with epsilon information. Access it using Math.erfc_e
8
+ module Erfc
9
+ P = [ 2.97886562639399288862,
10
+ 7.409740605964741794425,
11
+ 6.1602098531096305440906,
12
+ 5.019049726784267463450058,
13
+ 1.275366644729965952479585264,
14
+ 0.5641895835477550741253201704 ]
15
+ Q = [ 3.3690752069827527677,
16
+ 9.608965327192787870698,
17
+ 17.08144074746600431571095,
18
+ 12.0489519278551290360340491,
19
+ 9.396034016235054150430579648,
20
+ 2.260528520767326969591866945,
21
+ 1.0 ]
22
+
23
+ class << self
24
+ # Estimates erfc(x) valid for 8 < x < 100
25
+ # erfc8_sum from GSL-1.9
26
+ def erfc8_sum x
27
+ num = P[5]
28
+ 4.downto(0) { |i| num = x*num + P[i] }
29
+ den = Q[6]
30
+ 5.downto(0) { |i| den = x*den + Q[i] }
31
+ num / den
32
+ end
33
+
34
+ def erfc8 x
35
+ erfc8_sum(x) * Math.exp(-x*x)
36
+ end
37
+
38
+ # gsl_sf_erfc_e
39
+ def evaluate(x, with_error = false)
40
+ ax = x.abs
41
+ e = nil
42
+
43
+ if ax <= 1.0
44
+ t = 2*ax-1
45
+ e = ChebyshevSeries.evaluate(:erfc_xlt1, t, with_error)
46
+ elsif ax <= 5.0
47
+ ex2 = Math.exp(-x*x)
48
+ t = (ax-3).quo(2)
49
+ e = ChebyshevSeries.evaluate(:erfc_x15, t, with_error)
50
+ if with_error
51
+ e[0] *= ex2
52
+ e[1] = ex2 * (e[1] + 2*x.abs*Float::EPSILON)
53
+ else
54
+ e *= ex2
55
+ end
56
+ elsif ax < 10.0
57
+ exterm = Math.exp(-x*x)/ax
58
+ t = (2*ax-15).quo(5)
59
+ e = ChebyshevSeries.evaluate(:erfc_x510, t, with_error)
60
+ if with_error
61
+ e[0] *= exterm
62
+ e[1] = exterm * (e[1] + 2*x.abs*Float::EPSILON + Float::EPSILON)
63
+ else
64
+ e *= exterm
65
+ end
66
+ else
67
+ e8 = erfc8(ax)
68
+ e = with_error ? [e8, (x*x + 1) * Float::EPSILON * e8.abs] : e8
69
+ end
70
+
71
+ result = x < 0 ? 2 - (with_error ? e.first : e) : (with_error ? e.first : e)
72
+ with_error ? [result, e.last + 2*Float::EPSILON*result.abs] : result
73
+ end
74
+ end
75
+
76
+ end
77
+
78
+ end
79
+ end