File: cdf_wrappers.c

package info (click to toggle)
python-scipy 1.1.0-7
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 93,828 kB
  • sloc: python: 156,854; ansic: 82,925; fortran: 80,777; cpp: 7,505; makefile: 427; sh: 294
file content (462 lines) | stat: -rw-r--r-- 12,378 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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
 * This file is a collection (more can be added) of wrappers around some
 * CDFLIB Fortran algorithms.
 */

/*
 * Notice q and p are used in reverse from their meanings in distributions.py
 */

#include "cdf_wrappers.h"

#if defined(NO_APPEND_FORTRAN)
#if defined(UPPERCASE_FORTRAN)
#define F_FUNC(f,F) F
#else
#define F_FUNC(f,F) f
#endif
#else
#if defined(UPPERCASE_FORTRAN)
#define F_FUNC(f,F) F##_
#else
#define F_FUNC(f,F) f##_
#endif
#endif

/*
 * Call macros for different numbers of distribution parameters.
 */

#define CDFLIB_CALL2(func, name, a, b, result, return_bound)            \
    if (npy_isnan(p) || npy_isnan(q) || npy_isnan(a) || npy_isnan(b) || \
        npy_isnan(bound)) {                                             \
        return NPY_NAN;                                                 \
    }                                                                   \
    func(&which, &p, &q, &a, &b, &status, &bound);                      \
    return get_result(name, status, bound, result, return_bound)

#define CDFLIB_CALL3(func, name, a, b, c, result, return_bound)         \
    if (npy_isnan(p) || npy_isnan(q) || npy_isnan(a) || npy_isnan(b) || \
        npy_isnan(c) || npy_isnan(bound)) {                             \
        return NPY_NAN;                                                 \
    }                                                                   \
    func(&which, &p, &q, &a, &b, &c, &status, &bound);                  \
    return get_result(name, status, bound, result, return_bound)

#define CDFLIB_CALL4(func, name, a, b, c, d, result, return_bound)      \
    if (npy_isnan(p) || npy_isnan(q) || npy_isnan(a) || npy_isnan(b) || \
        npy_isnan(c) || npy_isnan(d) || npy_isnan(bound)) {             \
        return NPY_NAN;                                                 \
    }                                                                   \
    func(&which, &p, &q, &a, &b, &c, &d, &status, &bound);              \
    return get_result(name, status, bound, result, return_bound)


/* Return nan on status==1,2 */
#define NO_RETURN_BOUND 0
/* Return bound on status==1,2 */
#define RETURN_BOUND 1


/*
 * Return status checking function
 */

static double get_result(char *name, int status, double bound, double result, int return_bound) {
  if (status < 0) {
      sf_error(name, SF_ERROR_ARG, "(Fortran) input parameter %d is out of range", (-status));
  }
  else {
    switch (status) {
    case 0:
      /* no error */
      return result;
    case 1:
      sf_error(name, SF_ERROR_OTHER, "Answer appears to be lower than lowest search bound (%g)", bound);
      if (return_bound) {
          return bound;
      }
      break;
    case 2:
      sf_error(name, SF_ERROR_OTHER, "Answer appears to be higher than highest search bound (%g)", bound);
      if (return_bound) {
          return bound;
      }
      break;
    case 3:
    case 4:
      sf_error(name, SF_ERROR_OTHER, "Two parameters that should sum to 1.0 do not");
      break;
    case 10:
      sf_error(name, SF_ERROR_OTHER, "Computational error");
      break;
    default:
      sf_error(name, SF_ERROR_OTHER, "Unknown error");
    }
  }
  return NPY_NAN;
}


extern void F_FUNC(cdfbet,CDFBET)(int*,double*,double*,double*,double*,double*,double*,int*,double*);

double cdfbet3_wrap(double p, double b, double x) {
  int which=3;
  double q=1.0-p, y=1.0-x, a=0, bound=0;
  int status=10;

  CDFLIB_CALL4(F_FUNC(cdfbet,CDFBET), "btdtria",
               x, y, a, b,
               a, RETURN_BOUND);
}

double cdfbet4_wrap(double a, double p, double x) {
  int which=4;
  double q=1.0-p, y=1.0-x, b=0, bound=0;
  int status=10;

  CDFLIB_CALL4(F_FUNC(cdfbet,CDFBET), "btdtrib",
               x, y, a, b,
               b, RETURN_BOUND);
}


extern void F_FUNC(cdfbin,CDFBIN)(int*,double*,double*,double*,double*,double*,double*,int*,double*);

double cdfbin2_wrap(double p, double xn, double pr) {
  int which=2;
  double q=1.0-p, s=0, ompr=1.0-pr, bound=0;
  int status=10;

  CDFLIB_CALL4(F_FUNC(cdfbin,CDFBIN), "bdtrik",
               s, xn, pr, ompr,
               s, RETURN_BOUND);
}

double cdfbin3_wrap(double s, double p, double pr) {
  int which=3;
  double q=1.0-p, xn=0, ompr=1.0-pr, bound=0;
  int status=10;

  CDFLIB_CALL4(F_FUNC(cdfbin,CDFBIN), "bdtrin",
               s, xn, pr, ompr,
               xn, RETURN_BOUND);
}

extern void F_FUNC(cdfchi,CDFCHI)(int*,double*,double*,double*,double*,int*,double*);
double cdfchi3_wrap(double p, double x){
  int which=3;
  double q=1.0-p, df=0, bound=0;
  int status=10;

  CDFLIB_CALL2(F_FUNC(cdfchi,CDFCHI), "chdtriv",
               x, df,
               df, RETURN_BOUND);
}

extern void F_FUNC(cdfchn,CDFCHN)(int*,double*,double*,double*,double*,double*,int*,double*);
double cdfchn1_wrap(double x, double df, double nc) {
  int which=1;
  double q=0, p=0, bound=0;
  int status=10;

  CDFLIB_CALL3(F_FUNC(cdfchn,CDFCHN), "chndtr",
               x, df, nc,
               p, RETURN_BOUND);
}

double cdfchn2_wrap(double p, double df, double nc) {
  int which=2;
  double q=1.0-p, x=0, bound=0;
  int status=10;

  CDFLIB_CALL3(F_FUNC(cdfchn,CDFCHN), "chndtrix",
               x, df, nc,
               x, NO_RETURN_BOUND);
}

double cdfchn3_wrap(double x, double p, double nc) {
  int which=3;
  double q=1.0-p, df=0, bound=0;
  int status=10;

  CDFLIB_CALL3(F_FUNC(cdfchn,CDFCHN), "chndtridf",
               x, df, nc,
               df, RETURN_BOUND);
}

double cdfchn4_wrap(double x, double df, double p) {
  int which=4;
  double q=1.0-p, nc=0, bound=0;
  int status=10;

  CDFLIB_CALL3(F_FUNC(cdfchn,CDFCHN), "chndtrinc",
               x, df, nc,
               nc, RETURN_BOUND);
}

extern void F_FUNC(cdff,CDFF)(int*,double*,double*,double*,double*,double*,int*,double*);
/*
double cdff1_wrap(double dfn, double dfd, double f) {
  int which=1;
  double q, p, bound;
  int status=10;

  CDFLIB_CALL3(F_FUNC(cdff,CDFF), "fdtr",
               f, dfn, dfd,
               p, NO_RETURN_BOUND);
}

double cdff2_wrap(double dfn, double dfd, double p) {
  int which=2;
  double q=1.0-p, f, bound;
  int status=10;

  CDFLIB_CALL3(F_FUNC(cdff,CDFF), "fdtri",
               f, dfn, dfd,
               f, NO_RETURN_BOUND);
}
*/

/* This seem to give some trouble.  No idea why... */
double cdff3_wrap(double p, double dfd, double f) {
  int which=3;
  double q=1.0-p, dfn=0, bound=0;
  int status=10;

  CDFLIB_CALL3(F_FUNC(cdff,CDFF), "fdtridfn",
               f, dfn, dfd,
               dfn, RETURN_BOUND);
}

double cdff4_wrap(double dfn, double p, double f) {
  int which=4;
  double q=1.0-p, dfd=0, bound=0;
  int status=10;

  CDFLIB_CALL3(F_FUNC(cdff,CDFF), "fdtridfd",
               f, dfn, dfd,
               dfd, RETURN_BOUND);
}


extern void F_FUNC(cdffnc,CDFFNC)(int*,double*,double*,double*,double*,double*,double*,int*,double*);
double cdffnc1_wrap(double dfn, double dfd, double nc, double f) {
  int which=1;
  double q=0, p=0, bound=0;
  int status=10;

  CDFLIB_CALL4(F_FUNC(cdffnc,CDFFNC), "ncfdtr",
               f, dfn, dfd, nc,
               p, NO_RETURN_BOUND);
}

double cdffnc2_wrap(double dfn, double dfd, double nc, double p) {
  int which=2;
  double q=1.0-p, f=0, bound=0;
  int status=10;

  CDFLIB_CALL4(F_FUNC(cdffnc,CDFFNC), "ncfdtri",
               f, dfn, dfd, nc,
               f, RETURN_BOUND);
}


double cdffnc3_wrap(double p, double dfd, double nc, double f) {
  int which=3;
  double q=1.0-p, dfn=0, bound=0;
  int status=10;

  CDFLIB_CALL4(F_FUNC(cdffnc,CDFFNC), "ncfdtridfn",
               f, dfn, dfd, nc,
               dfn, RETURN_BOUND);
}

double cdffnc4_wrap(double dfn, double p, double nc, double f) {
  int which=4;
  double q=1.0-p, dfd=0, bound=0;
  int status=10;

  CDFLIB_CALL4(F_FUNC(cdffnc,CDFFNC), "ncfdtridfd",
               f, dfn, dfd, nc,
               dfd, RETURN_BOUND);
}

double cdffnc5_wrap(double dfn, double dfd, double p, double f) {
  int which=5;
  double q=1.0-p, nc=0, bound=0;
  int status=10;

  CDFLIB_CALL4(F_FUNC(cdffnc,CDFFNC), "ncfdtrinc",
               f, dfn, dfd, nc,
               nc, RETURN_BOUND);
}

/* scl == a in gdtr
   shp == b in gdtr
*/ 
extern void F_FUNC(cdfgam,CDFGAM)(int*,double*,double*,double*,double*,double*,int*,double*);
double cdfgam1_wrap(double scl, double shp, double x) {
  int which=1;
  double q=0, p=0, bound=0;
  int status=10;

  CDFLIB_CALL3(F_FUNC(cdfgam,CDFGAM), "gdtr",
               x, shp, scl,
               p, NO_RETURN_BOUND);
}

double cdfgam2_wrap(double scl, double shp, double p) {
  int which=2;
  double q=1.0-p, x=0, bound=0;
  int status=10;

  CDFLIB_CALL3(F_FUNC(cdfgam,CDFGAM), "gdtrix",
               x, shp, scl,
               x, RETURN_BOUND);
}

double cdfgam3_wrap(double scl, double p, double x) {
  int which=3;
  double q=1.0-p, shp=0, bound=0;
  int status=10;

  CDFLIB_CALL3(F_FUNC(cdfgam,CDFGAM), "gdtrib",
               x, shp, scl,
               shp, RETURN_BOUND);
}

double cdfgam4_wrap(double p, double shp, double x) {
  int which=4;
  double q=1.0-p, scl=0, bound=0;
  int status=10;

  CDFLIB_CALL3(F_FUNC(cdfgam,CDFGAM), "gdtria",
               x, shp, scl,
               scl, RETURN_BOUND);
}

extern void F_FUNC(cdfnbn,CDFNBN)(int*,double*,double*,double*,double*,double*,double*,int*,double*);
double cdfnbn2_wrap(double p, double xn, double pr) {
  int which=2;
  double q=1.0-p, s=0, ompr=1.0-pr, bound=0;
  int status=10;

  CDFLIB_CALL4(F_FUNC(cdfnbn,CDFNBN), "nbdtrik",
               s, xn, pr, ompr,
               s, RETURN_BOUND);
}

double cdfnbn3_wrap(double s, double p, double pr) {
  int which=3;
  double q=1.0-p, xn=0, ompr=1.0-pr, bound=0;
  int status=10;

  CDFLIB_CALL4(F_FUNC(cdfnbn,CDFNBN), "nbdtrin",
               s, xn, pr, ompr,
               xn, RETURN_BOUND);
}

extern void F_FUNC(cdfnor,CDFNOR)(int*,double*,double*,double*,double*,double*,int*,double*);
double cdfnor3_wrap(double p, double std, double x) {
  int which=3;
  double q=1.0-p, mn=0, bound=0;
  int status=10;

  CDFLIB_CALL3(F_FUNC(cdfnor,CDFNOR), "nrdtrimn",
               x, mn, std,
               mn, RETURN_BOUND);
}

double cdfnor4_wrap(double mn, double p, double x) {
  int which=4;
  double q=1.0-p, std=0, bound=0;
  int status=10;

  CDFLIB_CALL3(F_FUNC(cdfnor,CDFNOR), "nrdtrisd",
               x, mn, std,
               std, RETURN_BOUND);
}

extern void F_FUNC(cdfpoi,CDFPOI)(int*,double*,double*,double*,double*,int*,double*);
double cdfpoi2_wrap(double p, double xlam){
  int which=2;
  double q=1.0-p, s=0, bound=0;
  int status=10;

  CDFLIB_CALL2(F_FUNC(cdfpoi,CDFPOI), "pdtrik",
               s, xlam,
               s, RETURN_BOUND);
}

extern void F_FUNC(cdft,CDFT)(int*,double*,double*,double*,double*,int*,double*);
double cdft1_wrap(double df, double t){
  int which=1;
  double q=0, p=0, bound=0;
  int status=10;

  CDFLIB_CALL2(F_FUNC(cdft,CDFT), "stdtr",
               t, df,
               p, NO_RETURN_BOUND);
}

double cdft2_wrap(double df, double p){
  int which=2;
  double q=1.0-p, t=0, bound=0;
  int status=10;

  CDFLIB_CALL2(F_FUNC(cdft,CDFT), "stdtrit",
               t, df,
               t, RETURN_BOUND);
}

double cdft3_wrap(double p, double t){
  int which=3;
  double q=1.0-p, df=0, bound=0;
  int status=10;

  CDFLIB_CALL2(F_FUNC(cdft,CDFT), "stdtridf",
               t, df,
               df, RETURN_BOUND);
}

extern void F_FUNC(cdftnc,CDFTNC)(int*,double*,double*,double*,double*,double*,int*,double*);
double cdftnc1_wrap(double df, double nc, double t) {
  int which=1;
  double q=0, p=0, bound=0;
  int status=10;

  CDFLIB_CALL3(F_FUNC(cdftnc,CDFTNC), "nctdtr",
               t, df, nc,
               p, RETURN_BOUND);
}

double cdftnc2_wrap(double df, double nc, double p) {
  int which=2;
  double q=1.0-p, t=0, bound=0;
  int status=10;

  CDFLIB_CALL3(F_FUNC(cdftnc,CDFTNC), "nctdtrit",
               t, df, nc,
               t, RETURN_BOUND);
}

double cdftnc3_wrap(double p, double nc, double t) {
  int which=3;
  double q=1.0-p, df=0, bound=0;
  int status=10;

  CDFLIB_CALL3(F_FUNC(cdftnc,CDFTNC), "nctdtridf",
               t, df, nc,
               df, RETURN_BOUND);
}

double cdftnc4_wrap(double df, double p, double t) {
  int which=4;
  double q=1.0-p, nc=0, bound=0;
  int status=10;

  CDFLIB_CALL3(F_FUNC(cdftnc,CDFTNC), "nctdtrinc",
               t, df, nc,
               nc, RETURN_BOUND);
}