#include "Python.h" #include "Numeric/arrayobject.h" #include "Numeric/ranlib.h" #include "stdio.h" /* ----------------------------------------------------- */ 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; idata; for(i=0; idata; for (i=0; idimensions[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; idata), num_categories, (long*) out_ptr); out_ptr += op->strides[0]; } return PyArray_Return(op); } 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 */ /* exponential (genexp), multivariate normal (genmn), normal (gennor), permutation (genprm), uniform (genunf), standard exponential (sexpo), standard gamma (sgamma) */ /* 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__}, {"gamma", _gamma, 1, gamma__doc__}, {"f", f, 1, f__doc__}, {"noncentral_f", noncentral_f, 1, noncentral_f__doc__}, {"chisquare", chisquare, 1, chisquare__doc__}, {"noncentral_chisquare", noncentral_chisquare, 1, noncentral_chisquare__doc__}, {"binomial", binomial, 1, binomial__doc__}, {"negative_binomial", negative_binomial, 1, negative_binomial__doc__}, {"multinomial", multinomial, 1, multinomial__doc__}, {"poisson", poisson, 1, poisson__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) initranlib(void) { PyObject *m; /* Create the module and add the functions */ m = Py_InitModule4("ranlib", random_methods, random_module_documentation, (PyObject*)NULL,PYTHON_API_VERSION); /* Import the array object */ import_array(); /* XXXX Add constants here */ /* Check for errors */ if (PyErr_Occurred()) Py_FatalError("can't initialize module ranlib"); }