File: randmodule.c

package info (click to toggle)
python-scipy 0.3.2-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 13,572 kB
  • ctags: 20,326
  • sloc: ansic: 87,138; fortran: 51,876; python: 47,747; cpp: 2,134; objc: 384; makefile: 175; sh: 83
file content (480 lines) | stat: -rw-r--r-- 11,985 bytes parent folder | download
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#include "Python.h"
#include "Numeric/arrayobject.h"
#include "ranlib.h"
#include "stdio.h"

static PyObject *ErrorObject;

/* ----------------------------------------------------- */

static PyObject*
get_continuous_random(int num_dist_params, PyObject* self, PyObject* args, void* fun) {
  PyArrayObject *op;
  double *out_ptr;
  int i, n=-1;
  float a, b, c;

  switch(num_dist_params) {
  case 0:
    if( !PyArg_ParseTuple(args, "|i", &n) )
      return NULL;
    break;
  case 1:
    if( !PyArg_ParseTuple(args, "f|i", &a, &n) )
      return NULL;
    break;
  case 2:
    if( !PyArg_ParseTuple(args, "ff|i", &a, &b, &n) )
      return NULL;
    break;
  case 3:
    if( !PyArg_ParseTuple(args, "fff|i", &a, &b, &c, &n) )
      return NULL;
    break;
  }
  if( n == -1 )
    n = 1;

  /* Create a 1 dimensional array of length n of type double */
  op = (PyArrayObject*) PyArray_FromDims(1, &n, PyArray_DOUBLE);
  if( op == NULL )
    return NULL;

  out_ptr = (double *) op->data;
  for(i=0; i<n; i++) {
    switch(num_dist_params) {
    case 0:
      *out_ptr = (double) ((float (*)(void)) fun)();
      break;
    case 1:
      *out_ptr = (double) ((float (*)(float)) fun)(a);
      break;
    case 2:
      *out_ptr = (double) ((float (*)(float, float)) fun)(a,b);
      break;
    case 3:
      *out_ptr = (double) ((float (*)(float, float, float)) fun)(a,b,c);
      break;
    }
    out_ptr++;
  }

  return PyArray_Return(op);
}


static PyObject*
get_discrete_scalar_random(int num_integer_args, PyObject* self, PyObject* args, void* fun) {
  long int_arg;
  int n=-1, i;
  long* out_ptr;
  PyArrayObject* op;
  float float_arg;

  switch( num_integer_args ) {
  case 0:
    if( !PyArg_ParseTuple(args, "f|i", &float_arg, &n) ) {
      return NULL;
    }
    break;
  case 1:
    if( !PyArg_ParseTuple(args, "lf|i", &int_arg, &float_arg, &n) ) {
      return NULL;
    }
    break;
  }
  if( n==-1 ) {
    n = 1;
  }
  
  /* Create a 1 dimensional array of length n of type long */
  op = (PyArrayObject*) PyArray_FromDims(1, &n, PyArray_LONG);
  if( op == NULL ) {
    return NULL;
  }
  
  out_ptr = (long*) op->data;
  for(i=0; i<n; i++) {
    switch( num_integer_args ) {
    case 0:
      *out_ptr = ((long (*)(float)) fun)(float_arg);
      break;
    case 1:
      *out_ptr = ((long (*)(long, float)) fun)(int_arg, float_arg);
      break;
    }
    out_ptr++;
  }

  return PyArray_Return(op);
}


static char random_sample__doc__[] ="";

static PyObject *
random_sample(PyObject *self, PyObject *args) {
  return get_continuous_random(0, self, args, ranf);
}


static char standard_normal__doc__[] ="";

static PyObject *
standard_normal(PyObject *self, PyObject *args) {
  return get_continuous_random(0, self, args, snorm);
}


static char beta__doc__[] ="";

static PyObject *
beta(PyObject *self, PyObject *args) {
  return get_continuous_random(2, self, args, genbet);
}


static char exponential__doc__[] = "";

static PyObject *
exponential(PyObject *self, PyObject *args) { 
  return get_continuous_random(1, self, args, genexp);
}

static char normal__doc__[] = "";

static PyObject *
normal(PyObject *self, PyObject *args) { 
  return get_continuous_random(2, self, args, gennor);
}

static char uniform__doc__[] = "";

static PyObject *
uniform(PyObject *self, PyObject *args) { 
  return get_continuous_random(2, self, args, genunf);
}


static char standard_exp__doc__[] = "";

static PyObject *
standard_exp(PyObject *self, PyObject *args) { 
  return get_continuous_random(0, self, args, sexpo);
}


static char standard_gamma__doc__[] = "";

static PyObject *
standard_gamma(PyObject *self, PyObject *args) { 
  return get_continuous_random(1, self, args, sgamma);
}

static char gamma__doc__[] ="";

static PyObject *
/* there is a function named `gamma' in some libm's */
_gamma(PyObject *self, PyObject *args) { 
  return get_continuous_random(2, self, args, gengam);
}


static char f__doc__[] ="";

static PyObject *
f(PyObject *self, PyObject *args) {
  return get_continuous_random(2, self, args, genf);
}



static char noncentral_f__doc__[] ="";

static PyObject *
noncentral_f(PyObject *self, PyObject *args) {
  return get_continuous_random(3, self, args, gennf);
}


static char noncentral_chisquare__doc__[] ="";

static PyObject *
noncentral_chisquare(PyObject *self, PyObject *args) {
  return get_continuous_random(2, self, args, gennch);
}


static char chisquare__doc__[] ="";

static PyObject *
chisquare(PyObject *self, PyObject *args) {
  return get_continuous_random(1, self, args, genchi);
}


static char binomial__doc__[] ="";

static PyObject *
binomial(PyObject *self, PyObject *args) {
  return get_discrete_scalar_random(1, self, args, ignbin);
}


static char negative_binomial__doc__[] ="";

static PyObject *
negative_binomial(PyObject *self, PyObject *args) {
  return get_discrete_scalar_random(1, self, args, ignnbn);
}

static char poisson__doc__[] ="";

static PyObject *
poisson(PyObject *self, PyObject *args) {
  return get_discrete_scalar_random(0, self, args, ignpoi);
}


static char multinomial__doc__[] ="";

static PyObject*
multinomial(PyObject* self, PyObject* args) {
  int n=-1, i;
  long num_trials, num_categories;
  char* out_ptr;
  PyArrayObject* priors_array;
  PyObject* priors_object;
  PyArrayObject* op;
  int out_dimensions[2];

  if( !PyArg_ParseTuple(args, "lO|i", &num_trials, &priors_object, &n) ) {
    return NULL;
  }
  priors_array = (PyArrayObject*) PyArray_ContiguousFromObject(priors_object, PyArray_FLOAT, 1, 1);
  if( priors_array == NULL ) {
    return NULL;
  }
  num_categories = priors_array->dimensions[0]+1;
  if( n==-1 ) {
    n = 1;
  }
  
  /* Create an n by num_categories array of long */
  out_dimensions[0] = n;
  out_dimensions[1] = num_categories;
  op = (PyArrayObject*) PyArray_FromDims(2, out_dimensions, PyArray_LONG);
  if( op == NULL ) {
    return NULL;
  }
  
  out_ptr = op->data;
  for(i=0; i<n; i++) {
    genmul(num_trials, (float*)(priors_array->data), num_categories, (long*) out_ptr);
    out_ptr += op->strides[0];
  }

  return PyArray_Return(op);
}


static char permutation__doc__[] ="";

static PyObject*
permutation(PyObject* self, PyObject* args) {
  PyArrayObject* vector_array;
  PyObject* vector;
  int len;

  if( !PyArg_ParseTuple(args, "O", &vector) ) {
    return NULL;
  }
  vector_array = (PyArrayObject*) PyArray_CopyFromObject(vector, PyArray_LONG, 1, 0);
  if( vector_array == NULL ) {
    return NULL;
  }

  len = _PyArray_multiply_list(vector_array->dimensions, vector_array->nd);
  genprm((long *) vector_array->data, (long) len);
  
  return PyArray_Return(vector_array);
}

static char multivariate_normal__doc__[] ="";

static PyObject*
multivariate_normal(PyObject* self, PyObject* args) {
  PyArrayObject *amean=NULL, *acov=NULL, *ameanf=NULL, *acovf=NULL;
  PyObject *omean=NULL, *ocov=NULL;
  PyArrayObject *outarr=NULL, *outarr_f=NULL;
  float *parm=NULL, *work=NULL, *outptr;
  long p;
  int n=1, dims[2], k;

  if( !PyArg_ParseTuple(args, "OO|i", &omean, &ocov, &n) ) {
    return NULL;
  }
  amean = (PyArrayObject*) PyArray_ContiguousFromObject(omean, PyArray_DOUBLE, 1, 1);
  if( amean == NULL ) goto fail;

  ameanf = (PyArrayObject *)PyArray_Cast(amean, PyArray_FLOAT);
  if (ameanf == NULL) goto fail;

  acov = (PyArrayObject*) PyArray_ContiguousFromObject(ocov, PyArray_DOUBLE, 2, 2);
  if( acov == NULL ) goto fail;

  acovf = (PyArrayObject *)PyArray_Cast(acov, PyArray_FLOAT);
  if (acovf == NULL) goto fail;

  p = (long) ameanf->dimensions[0]; /* length of mean vector */
  if ((((long) acovf->dimensions[0]) != p) ||
      (((long) acovf->dimensions[1]) != p)) {
    fprintf(stderr, "%d %d %d %d", ameanf->dimensions[0], acovf->dimensions[0], acovf->dimensions[1], acovf->nd);
    PyErr_SetString(PyExc_ValueError, "Covariance matrix must be square with dimension size equal to the length\n of the mean vector.");
    goto fail;
  }

  work = PyMem_New(float, ((int) (p*(p+3)/2 + 1 + p)));
  if (work == NULL) {
    PyErr_SetString(PyExc_MemoryError, "Could not allocate needed memory.");
    goto fail;
  }
  parm = work + p;
  
  setgmn((float *)ameanf->data, (float *)acovf->data, p, parm);

  if (PyErr_Occurred()) goto fail;

  if (n == 1) {
    int tmp = p; /* avoid "invalid pointer type" warnings from compiler */
    outarr_f = (PyArrayObject *)PyArray_FromDims(1, &tmp, PyArray_FLOAT);
  }
  else {
    dims[0] = n;
    dims[1] = p;
    outarr_f = (PyArrayObject *)PyArray_FromDims(2, dims, PyArray_FLOAT);
  }
  if (outarr_f == NULL) goto fail;

  outptr = (float *) outarr_f->data;
  
  for (k=0; k<n; k++) {
    genmn(parm, outptr, work);
    outptr += p;
  }

  outarr = (PyArrayObject *)PyArray_Cast(outarr_f, PyArray_DOUBLE);
  if (outarr == NULL) goto fail;
    
  PyMem_Del(work);
  Py_DECREF(amean);
  Py_DECREF(acov);
  Py_DECREF(ameanf);
  Py_DECREF(acovf);
  return PyArray_Return(outarr);

 fail:
  if (work != NULL) PyMem_Del(work);
  Py_XDECREF(amean);
  Py_XDECREF(ameanf);
  Py_XDECREF(acov);
  Py_XDECREF(acovf);
  Py_XDECREF(outarr_f);
  Py_XDECREF(outarr);
  return NULL;
}


static PyObject *
random_set_seeds(PyObject *self, PyObject *args)
{
  long seed1, seed2;

  if (!PyArg_ParseTuple(args, "ll", &seed1, &seed2)) return NULL;


  setall(seed1, seed2);
  if (PyErr_Occurred ()) return NULL;
  Py_INCREF(Py_None);
  return (PyObject *)Py_None;
}


static PyObject *
random_get_seeds(PyObject *self, PyObject *args)
{
  long seed1, seed2;

  if (!PyArg_ParseTuple(args, "")) return NULL;

  getsd(&seed1, &seed2);

  return Py_BuildValue("ll", seed1, seed2);
}


/* Missing interfaces to */
/* multivariate normal (genmn), 
   permutation (genprm),
*/

/* List of methods defined in the module */

static struct PyMethodDef random_methods[] = {
 {"sample",     random_sample,          1,      random_sample__doc__},
 {"standard_normal", standard_normal,   1,      standard_normal__doc__},
 {"beta",	beta,                   1,      beta__doc__},
 {"exponential", exponential,           1,      exponential__doc__},
 {"gamma",	_gamma,                  1,      gamma__doc__},
 {"f",	        f,                      1,      f__doc__},
 {"noncentral_f", noncentral_f,         1,      noncentral_f__doc__},
 {"chi2",	chisquare,              1,      chisquare__doc__},
 {"noncentral_chi2", noncentral_chisquare,
                                        1,      noncentral_chisquare__doc__},
 {"binomial",	binomial,               1,      binomial__doc__},
 {"negative_binomial",  negative_binomial,
                                        1,      negative_binomial__doc__},
 {"multinomial", multinomial,           1,      multinomial__doc__},
 {"multivariate_normal", multivariate_normal,         1,      multivariate_normal__doc__},
 {"normal", normal,                     1,      normal__doc__},
 {"permutation", permutation,           1,      permutation__doc__},
 {"poisson",    poisson,                1,      poisson__doc__},
 {"uniform",    uniform,                1,      uniform__doc__},
 {"standard_exp",      standard_exp,         1,      standard_exp__doc__},
 {"standard_gamma",    standard_gamma,     1,      standard_gamma__doc__},
 {"set_seeds",  random_set_seeds,       1, },
 {"get_seeds",  random_get_seeds,       1, },
 {NULL,		NULL}		/* sentinel */
};


/* Initialization function for the module (*must* be called initranlib) */

static char random_module_documentation[] = 
""
;

DL_EXPORT(void)
initrand(void)
{
	PyObject *m, *d;

	/* Create the module and add the functions */
	m = Py_InitModule4("rand", random_methods,
		random_module_documentation,
		(PyObject*)NULL,PYTHON_API_VERSION);

	/* Import the array object */
	import_array();

	/* Add some symbolic constants to the module */
	d = PyModule_GetDict(m);
	ErrorObject = PyString_FromString("rand.error");
	PyDict_SetItemString(d, "error", ErrorObject);

	/* XXXX Add constants here */
	
	/* Check for errors */
	if (PyErr_Occurred())
		Py_FatalError("can't initialize module rand");
}