static PyArrayObject* array_from_pyobj(const int type_num, int *dims, const int rank, const int intent, PyObject *obj) { /* Note about reference counting ----------------------------- If the caller returns the array to Python, it must be done with Py_BuildValue("N",arr). Otherwise, if obj!=arr then the caller must call Py_DECREF(arr). */ /* f2py_show_args(type_num,dims,rank,intent); */ if (intent & F2PY_INTENT_CACHE) { /* Don't expect correct storage order or anything reasonable when returning cache array. */ if ((intent & F2PY_INTENT_HIDE) || (obj==Py_None)) { PyArrayObject *arr = NULL; CHECK_DIMS_DEFINED(rank,dims,"optional,intent(cache) must" " have defined dimensions.\n"); arr = (PyArrayObject *)PyArray_FromDims(rank,dims,type_num); ARR_IS_NULL(arr,"FromDims failed: optional,intent(cache)\n"); if (intent & F2PY_INTENT_OUT) Py_INCREF(arr); return arr; } if (PyArray_Check(obj) && ISCONTIGUOUS((PyArrayObject *)obj) && HAS_PROPER_ELSIZE((PyArrayObject *)obj,type_num) ) { if (check_and_fix_dimensions((PyArrayObject *)obj,rank,dims)) return NULL; /*XXX: set exception */ if (intent & F2PY_INTENT_OUT) Py_INCREF(obj); return (PyArrayObject *)obj; } ARR_IS_NULL(NULL,"intent(cache) must be contiguous array with a proper elsize.\n"); } if (intent & F2PY_INTENT_HIDE) { PyArrayObject *arr = NULL; CHECK_DIMS_DEFINED(rank,dims,"intent(hide) must have defined dimensions.\n"); arr = (PyArrayObject *)PyArray_FromDims(rank,dims,type_num); ARR_IS_NULL(arr,"FromDims failed: intent(hide)\n"); if (intent & F2PY_INTENT_OUT) { if ((!(intent & F2PY_INTENT_C)) && (rank>1)) { lazy_transpose(arr); arr->flags &= ~NPY_CONTIGUOUS; } Py_INCREF(arr); } return arr; } if (PyArray_Check(obj)) { /* here we have always intent(in) or intent(inout) */ PyArrayObject *arr = (PyArrayObject *)obj; int is_cont = (intent & F2PY_INTENT_C) ? (ISCONTIGUOUS(arr)) : (array_has_column_major_storage(arr)); if (check_and_fix_dimensions(arr,rank,dims)) return NULL; /*XXX: set exception */ if ((intent & F2PY_INTENT_COPY) || (! (is_cont && HAS_PROPER_ELSIZE(arr,type_num) && PyArray_CanCastSafely(arr->descr->type_num,type_num)))) { PyArrayObject *tmp_arr = NULL; if (intent & F2PY_INTENT_INOUT) { ARR_IS_NULL(NULL,"intent(inout) array must be contiguous and" " with a proper type and size.\n") } if ((rank>1) && (! (intent & F2PY_INTENT_C))) lazy_transpose(arr); if (PyArray_CanCastSafely(arr->descr->type_num,type_num)) { tmp_arr = (PyArrayObject *)PyArray_CopyFromObject(obj,type_num,0,0); ARR_IS_NULL(arr,"CopyFromObject failed: array.\n"); } else { tmp_arr = (PyArrayObject *)PyArray_FromDims(arr->nd, arr->dimensions, type_num); ARR_IS_NULL(tmp_arr,"FromDims failed: array with unsafe cast.\n"); if (copy_ND_array(arr,tmp_arr)) ARR_IS_NULL(NULL,"copy_ND_array failed: array with unsafe cast.\n"); } if ((rank>1) && (! (intent & F2PY_INTENT_C))) { lazy_transpose(arr); lazy_transpose(tmp_arr); tmp_arr->flags &= ~NPY_CONTIGUOUS; } arr = tmp_arr; } if (intent & F2PY_INTENT_OUT) Py_INCREF(arr); return arr; }
PyObject * fftpack_cfftb(PyObject *NPY_UNUSED(self), PyObject *args) { PyObject *op1, *op2; PyArrayObject *data; PyArray_Descr *descr; double *wsave, *dptr; npy_intp nsave; int npts, nrepeats, i; if(!PyArg_ParseTuple(args, "OO", &op1, &op2)) { return NULL; } data = (PyArrayObject *)PyArray_CopyFromObject(op1, NPY_CDOUBLE, 1, 0); if (data == NULL) { return NULL; } descr = PyArray_DescrFromType(NPY_DOUBLE); if (PyArray_AsCArray(&op2, (void *)&wsave, &nsave, 1, descr) == -1) { goto fail; } if (data == NULL) { goto fail; } npts = PyArray_DIM(data, PyArray_NDIM(data) - 1); if (nsave != npts*4 + 15) { PyErr_SetString(ErrorObject, "invalid work array for fft size"); goto fail; } nrepeats = PyArray_SIZE(data)/npts; dptr = (double *)PyArray_DATA(data); NPY_SIGINT_ON; for (i = 0; i < nrepeats; i++) { cfftb(npts, dptr, wsave); dptr += npts*2; } NPY_SIGINT_OFF; PyArray_Free(op2, (char *)wsave); return (PyObject *)data; fail: PyArray_Free(op2, (char *)wsave); Py_DECREF(data); return NULL; }
// REVIEW: the poolSize can be pulled from the numeric array RDKit::VECT_INT_VECT HierarchicalClusters(HierarchicalClusterPicker *picker, python::object &distMat, int poolSize, int pickSize) { if (!PyArray_Check(distMat.ptr())) { throw ValueErrorException("distance mat argument must be a numpy matrix"); } // REVIEW: check pickSize < poolSize, otherwise throw_value_error() PyArrayObject *copy; // it's painful to have to copy the input matrix, but the // picker itself will step on the distance matrix, so use // CopyFromObject here instead of ContiguousFromObject copy = (PyArrayObject *)PyArray_CopyFromObject(distMat.ptr(), NPY_DOUBLE, 1, 1); double *dMat = (double *)PyArray_DATA(copy); RDKit::VECT_INT_VECT res = picker->cluster(dMat, poolSize, pickSize); Py_DECREF(copy); return res; }
// REVIEW: the poolSize can be pulled from the numeric array RDKit::INT_VECT HierarchicalPicks(HierarchicalClusterPicker *picker, python::object &distMat, int poolSize, int pickSize) { if(pickSize>=poolSize){ throw ValueErrorException("pickSize must be less than poolSize"); } if (!PyArray_Check(distMat.ptr())){ throw ValueErrorException("distance mat argument must be a numpy matrix"); } PyArrayObject *copy; // it's painful to have to copy the input matrix, but the // picker itself will step on the distance matrix, so use // CopyFromObject here instead of ContiguousFromObject copy = (PyArrayObject *)PyArray_CopyFromObject(distMat.ptr(), PyArray_DOUBLE, 1,1); double *dMat = (double *)copy->data; RDKit::INT_VECT res=picker->pick(dMat, poolSize, pickSize); Py_DECREF(copy); return res; }
PyObject *odr(PyObject * self, PyObject * args, PyObject * kwds) { PyObject *fcn, *initbeta, *py, *px, *pwe = NULL, *pwd = NULL, *fjacb = NULL; PyObject *fjacd = NULL, *pifixb = NULL, *pifixx = NULL; PyObject *pstpb = NULL, *pstpd = NULL, *psclb = NULL, *pscld = NULL; PyObject *pwork = NULL, *piwork = NULL, *extra_args = NULL; int job = 0, ndigit = 0, maxit = -1, iprint = 0; int full_output = 0; double taufac = 0.0, sstol = -1.0, partol = -1.0; char *errfile = NULL, *rptfile = NULL; int lerrfile = 0, lrptfile = 0; PyArrayObject *beta = NULL, *y = NULL, *x = NULL, *we = NULL, *wd = NULL; PyArrayObject *ifixb = NULL, *ifixx = NULL; PyArrayObject *stpb = NULL, *stpd = NULL, *sclb = NULL, *scld = NULL; PyArrayObject *work = NULL, *iwork = NULL; int n, m, np, nq, ldy, ldx, ldwe, ld2we, ldwd, ld2wd, ldifx; int lunerr = -1, lunrpt = -1, ldstpd, ldscld, lwork, liwork, info = 0; static char *kw_list[] = { "fcn", "initbeta", "y", "x", "we", "wd", "fjacb", "fjacd", "extra_args", "ifixb", "ifixx", "job", "iprint", "errfile", "rptfile", "ndigit", "taufac", "sstol", "partol", "maxit", "stpb", "stpd", "sclb", "scld", "work", "iwork", "full_output", NULL }; int isodr = 1; PyObject *result; npy_intp dim1[1], dim2[2], dim3[3]; int implicit; /* flag for implicit model */ if (kwds == NULL) { if (!PyArg_ParseTuple(args, "OOOO|OOOOOOOiiz#z#idddiOOOOOOi:odr", &fcn, &initbeta, &py, &px, &pwe, &pwd, &fjacb, &fjacd, &extra_args, &pifixb, &pifixx, &job, &iprint, &errfile, &lerrfile, &rptfile, &lrptfile, &ndigit, &taufac, &sstol, &partol, &maxit, &pstpb, &pstpd, &psclb, &pscld, &pwork, &piwork, &full_output)) { return NULL; } } else { if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOO|OOOOOOOiiz#z#idddiOOOOOOi:odr", kw_list, &fcn, &initbeta, &py, &px, &pwe, &pwd, &fjacb, &fjacd, &extra_args, &pifixb, &pifixx, &job, &iprint, &errfile, &lerrfile, &rptfile, &lrptfile, &ndigit, &taufac, &sstol, &partol, &maxit, &pstpb, &pstpd, &psclb, &pscld, &pwork, &piwork, &full_output)) { return NULL; } } /* Check the validity of all arguments */ if (!PyCallable_Check(fcn)) { PYERR(PyExc_TypeError, "fcn must be callable"); } if (!PySequence_Check(initbeta)) { PYERR(PyExc_TypeError, "initbeta must be a sequence"); } if (!PySequence_Check(py)) { /* Checking whether py is an int * * XXX: PyInt_Check for np.int32 instances does not work on python 2.6 - * we should fix this in numpy, workaround by trying to cast to an int * for now */ long val; PyErr_Clear(); val = PyInt_AsLong(py); if (val == -1 && PyErr_Occurred()) { PYERR(PyExc_TypeError, "y must be a sequence or integer (if model is implicit)"); } } if (!PySequence_Check(px)) { PYERR(PyExc_TypeError, "x must be a sequence"); } if (pwe != NULL && !PySequence_Check(pwe) && !PyNumber_Check(pwe)) { PYERR(PyExc_TypeError, "we must be a sequence or a number"); } if (pwd != NULL && !PySequence_Check(pwd) && !PyNumber_Check(pwd)) { PYERR(PyExc_TypeError, "wd must be a sequence or a number"); } if (fjacb != NULL && !PyCallable_Check(fjacb)) { PYERR(PyExc_TypeError, "fjacb must be callable"); } if (fjacd != NULL && !PyCallable_Check(fjacd)) { PYERR(PyExc_TypeError, "fjacd must be callable"); } if (extra_args != NULL && !PySequence_Check(extra_args)) { PYERR(PyExc_TypeError, "extra_args must be a sequence"); } if (pifixx != NULL && !PySequence_Check(pifixx)) { PYERR(PyExc_TypeError, "ifixx must be a sequence"); } if (pifixb != NULL && !PySequence_Check(pifixb)) { PYERR(PyExc_TypeError, "ifixb must be a sequence"); } if (pstpb != NULL && !PySequence_Check(pstpb)) { PYERR(PyExc_TypeError, "stpb must be a sequence"); } if (pstpd != NULL && !PySequence_Check(pstpd)) { PYERR(PyExc_TypeError, "stpd must be a sequence"); } if (psclb != NULL && !PySequence_Check(psclb)) { PYERR(PyExc_TypeError, "sclb must be a sequence"); } if (pscld != NULL && !PySequence_Check(pscld)) { PYERR(PyExc_TypeError, "scld must be a sequence"); } if (pwork != NULL && !PyArray_Check(pwork)) { PYERR(PyExc_TypeError, "work must be an array"); } if (piwork != NULL && !PyArray_Check(piwork)) { PYERR(PyExc_TypeError, "iwork must be an array"); } /* start processing the arguments and check for errors on the way */ /* check for implicit model */ implicit = (job % 10 == 1); if (!implicit) { if ((y = (PyArrayObject *) PyArray_CopyFromObject(py, NPY_DOUBLE, 1, 2)) == NULL) { PYERR(PyExc_ValueError, "y could not be made into a suitable array"); } n = y->dimensions[y->nd - 1]; /* pick the last dimension */ if ((x = (PyArrayObject *) PyArray_CopyFromObject(px, NPY_DOUBLE, 1, 2)) == NULL) { PYERR(PyExc_ValueError, "x could not be made into a suitable array"); } if (n != x->dimensions[x->nd - 1]) { PYERR(PyExc_ValueError, "x and y don't have matching numbers of observations"); } if (y->nd == 1) { nq = 1; } else { nq = y->dimensions[0]; } ldx = ldy = n; } else { /* we *do* have an implicit model */ ldy = 1; nq = (int)PyInt_AsLong(py); dim1[0] = 1; /* initialize y to a dummy array; never referenced */ y = (PyArrayObject *) PyArray_SimpleNew(1, dim1, NPY_DOUBLE); if ((x = (PyArrayObject *) PyArray_CopyFromObject(px, NPY_DOUBLE, 1, 2)) == NULL) { PYERR(PyExc_ValueError, "x could not be made into a suitable array"); } n = x->dimensions[x->nd - 1]; ldx = n; } if (x->nd == 1) { m = 1; } else { m = x->dimensions[0]; } /* x, y */ if ((beta = (PyArrayObject *) PyArray_CopyFromObject(initbeta, NPY_DOUBLE, 1, 1)) == NULL) { PYERR(PyExc_ValueError, "initbeta could not be made into a suitable array"); } np = beta->dimensions[0]; if (pwe == NULL) { ldwe = ld2we = 1; dim1[0] = n; we = (PyArrayObject *) PyArray_SimpleNew(1, dim1, NPY_DOUBLE); ((double *)(we->data))[0] = -1.0; } else if (PyNumber_Check(pwe) && !PyArray_Check(pwe)) { /* we is a single weight, set the first value of we to -pwe */ PyObject *tmp; double val; tmp = PyNumber_Float(pwe); if (tmp == NULL) PYERR(PyExc_ValueError, "could not convert we to a suitable array"); val = PyFloat_AsDouble(tmp); Py_DECREF(tmp); dim3[0] = nq; dim3[1] = 1; dim3[2] = 1; we = (PyArrayObject *) PyArray_SimpleNew(3, dim3, NPY_DOUBLE); if (implicit) { ((double *)(we->data))[0] = val; } else { ((double *)(we->data))[0] = -val; } ldwe = ld2we = 1; } else if (PySequence_Check(pwe)) { /* we needs to be turned into an array */ if ((we = (PyArrayObject *) PyArray_CopyFromObject(pwe, NPY_DOUBLE, 1, 3)) == NULL) { PYERR(PyExc_ValueError, "could not convert we to a suitable array"); } if (we->nd == 1 && nq == 1) { ldwe = n; ld2we = 1; } else if (we->nd == 1 && we->dimensions[0] == nq) { /* we is a rank-1 array with diagonal weightings to be broadcast * to all observations */ ldwe = 1; ld2we = 1; } else if (we->nd == 3 && we->dimensions[0] == nq && we->dimensions[1] == nq && we->dimensions[2] == 1) { /* we is a rank-3 array with the covariant weightings to be broadcast to all observations */ ldwe = 1; ld2we = nq; } else if (we->nd == 2 && we->dimensions[0] == nq && we->dimensions[1] == nq) { /* we is a rank-2 array with the full covariant weightings to be broadcast to all observations */ ldwe = 1; ld2we = nq; } else if (we->nd == 2 && we->dimensions[0] == nq && we->dimensions[1] == n) { /* we is a rank-2 array with the diagonal elements of the covariant weightings for each observation */ ldwe = n; ld2we = 1; } else if (we->nd == 3 && we->dimensions[0] == nq && we->dimensions[1] == nq && we->dimensions[2] == n) { /* we is the full specification of the covariant weights for each observation */ ldwe = n; ld2we = nq; } else { PYERR(PyExc_ValueError, "could not convert we to a suitable array"); } } /* we */ if (pwd == NULL) { ldwd = ld2wd = 1; dim1[0] = m; wd = (PyArrayObject *) PyArray_SimpleNew(1, dim1, NPY_DOUBLE); ((double *)(wd->data))[0] = -1.0; } else if (PyNumber_Check(pwd) && !PyArray_Check(pwd)) { /* wd is a single weight, set the first value of wd to -pwd */ PyObject *tmp; double val; tmp = PyNumber_Float(pwd); if (tmp == NULL) PYERR(PyExc_ValueError, "could not convert wd to a suitable array"); val = PyFloat_AsDouble(tmp); Py_DECREF(tmp); dim3[0] = 1; dim3[1] = 1; dim3[2] = m; wd = (PyArrayObject *) PyArray_SimpleNew(3, dim3, NPY_DOUBLE); ((double *)(wd->data))[0] = -val; ldwd = ld2wd = 1; } else if (PySequence_Check(pwd)) { /* wd needs to be turned into an array */ if ((wd = (PyArrayObject *) PyArray_CopyFromObject(pwd, NPY_DOUBLE, 1, 3)) == NULL) { PYERR(PyExc_ValueError, "could not convert wd to a suitable array"); } if (wd->nd == 1 && m == 1) { ldwd = n; ld2wd = 1; } else if (wd->nd == 1 && wd->dimensions[0] == m) { /* wd is a rank-1 array with diagonal weightings to be broadcast * to all observations */ ldwd = 1; ld2wd = 1; } else if (wd->nd == 3 && wd->dimensions[0] == m && wd->dimensions[1] == m && wd->dimensions[2] == 1) { /* wd is a rank-3 array with the covariant wdightings to be broadcast to all observations */ ldwd = 1; ld2wd = m; } else if (wd->nd == 2 && wd->dimensions[0] == m && wd->dimensions[1] == m) { /* wd is a rank-2 array with the full covariant weightings to be broadcast to all observations */ ldwd = 1; ld2wd = m; } else if (wd->nd == 2 && wd->dimensions[0] == m && wd->dimensions[1] == n) { /* wd is a rank-2 array with the diagonal elements of the covariant weightings for each observation */ ldwd = n; ld2wd = 1; } else if (wd->nd == 3 && wd->dimensions[0] == m && wd->dimensions[1] == m && wd->dimensions[2] == n) { /* wd is the full specification of the covariant weights for each observation */ ldwd = n; ld2wd = m; } else { PYERR(PyExc_ValueError, "could not convert wd to a suitable array"); } } /* wd */ if (pifixb == NULL) { dim1[0] = np; ifixb = (PyArrayObject *) PyArray_SimpleNew(1, dim1, NPY_INT); *(int *)(ifixb->data) = -1; /* set first element negative */ } else { /* pifixb is a sequence as checked before */ if ((ifixb = (PyArrayObject *) PyArray_CopyFromObject(pifixb, NPY_INT, 1, 1)) == NULL) { PYERR(PyExc_ValueError, "could not convert ifixb to a suitable array"); } if (ifixb->dimensions[0] != np) { PYERR(PyExc_ValueError, "could not convert ifixb to a suitable array"); } } /* ifixb */ if (pifixx == NULL) { dim2[0] = m; dim2[1] = 1; ifixx = (PyArrayObject *) PyArray_SimpleNew(2, dim2, NPY_INT); *(int *)(ifixx->data) = -1; /* set first element negative */ ldifx = 1; } else { /* pifixx is a sequence as checked before */ if ((ifixx = (PyArrayObject *) PyArray_CopyFromObject(pifixx, NPY_INT, 1, 2)) == NULL) { PYERR(PyExc_ValueError, "could not convert ifixx to a suitable array"); } if (ifixx->nd == 1 && ifixx->dimensions[0] == m) { ldifx = 1; } else if (ifixx->nd == 1 && ifixx->dimensions[0] == n && m == 1) { ldifx = n; } else if (ifixx->nd == 2 && ifixx->dimensions[0] == m && ifixx->dimensions[1] == n) { ldifx = n; } else { PYERR(PyExc_ValueError, "could not convert ifixx to a suitable array"); } } /* ifixx */ if (errfile != NULL) { /* call FORTRAN's OPEN to open the file with a logical unit of 18 */ lunerr = 18; F_FUNC(dluno,DLUNO)(&lunerr, errfile, lerrfile); } if (rptfile != NULL) { /* call FORTRAN's OPEN to open the file with a logical unit of 19 */ lunrpt = 19; F_FUNC(dluno,DLUNO)(&lunrpt, rptfile, lrptfile); } if (pstpb == NULL) { dim1[0] = np; stpb = (PyArrayObject *) PyArray_SimpleNew(1, dim1, NPY_DOUBLE); *(double *)(stpb->data) = 0.0; } else /* pstpb is a sequence */ { if ((stpb = (PyArrayObject *) PyArray_CopyFromObject(pstpb, NPY_DOUBLE, 1, 1)) == NULL || stpb->dimensions[0] != np) { PYERR(PyExc_ValueError, "could not convert stpb to a suitable array"); } } /* stpb */ if (pstpd == NULL) { dim2[0] = 1; dim2[1] = m; stpd = (PyArrayObject *) PyArray_SimpleNew(2, dim2, NPY_DOUBLE); *(double *)(stpd->data) = 0.0; ldstpd = 1; } else { if ((stpd = (PyArrayObject *) PyArray_CopyFromObject(pstpd, NPY_DOUBLE, 1, 2)) == NULL) { PYERR(PyExc_ValueError, "could not convert stpb to a suitable array"); } if (stpd->nd == 1 && stpd->dimensions[0] == m) { ldstpd = 1; } else if (stpd->nd == 1 && stpd->dimensions[0] == n && m == 1) { ldstpd = n; } else if (stpd->nd == 2 && stpd->dimensions[0] == n && stpd->dimensions[1] == m) { ldstpd = n; } } /* stpd */ if (psclb == NULL) { dim1[0] = np; sclb = (PyArrayObject *) PyArray_SimpleNew(1, dim1, NPY_DOUBLE); *(double *)(sclb->data) = 0.0; } else /* psclb is a sequence */ { if ((sclb = (PyArrayObject *) PyArray_CopyFromObject(psclb, NPY_DOUBLE, 1, 1)) == NULL || sclb->dimensions[0] != np) { PYERR(PyExc_ValueError, "could not convert sclb to a suitable array"); } } /* sclb */ if (pscld == NULL) { dim2[0] = 1; dim2[1] = n; scld = (PyArrayObject *) PyArray_SimpleNew(2, dim2, NPY_DOUBLE); *(double *)(scld->data) = 0.0; ldscld = 1; } else { if ((scld = (PyArrayObject *) PyArray_CopyFromObject(pscld, NPY_DOUBLE, 1, 2)) == NULL) { PYERR(PyExc_ValueError, "could not convert stpb to a suitable array"); } if (scld->nd == 1 && scld->dimensions[0] == m) { ldscld = 1; } else if (scld->nd == 1 && scld->dimensions[0] == n && m == 1) { ldscld = n; } else if (scld->nd == 2 && scld->dimensions[0] == n && scld->dimensions[1] == m) { ldscld = n; } } /* scld */ if (job % 10 < 2) { /* ODR, not OLS */ lwork = 18 + 11 * np + np * np + m + m * m + 4 * n * nq + 6 * n * m + 2 * n * nq * np + 2 * n * nq * m + nq * nq + 5 * nq + nq * (np + m) + ldwe * ld2we * nq; isodr = 1; } else { /* OLS, not ODR */ lwork = 18 + 11 * np + np * np + m + m * m + 4 * n * nq + 2 * n * m + 2 * n * nq * np + 5 * nq + nq * (np + m) + ldwe * ld2we * nq; isodr = 0; } liwork = 20 + np + nq * (np + m); if ((job / 10000) % 10 >= 1) { /* fit is a restart, make sure work and iwork are input */ if (pwork == NULL || piwork == NULL) { PYERR(PyExc_ValueError, "need to input work and iwork arrays to restart"); } } if ((job / 1000) % 10 >= 1) { /* delta should be supplied, make sure the user does */ if (pwork == NULL) { PYERR(PyExc_ValueError, "need to input work array for delta initialization"); } } if (pwork != NULL) { if ((work = (PyArrayObject *) PyArray_CopyFromObject(pwork, NPY_DOUBLE, 1, 1)) == NULL) { PYERR(PyExc_ValueError, "could not convert work to a suitable array"); } if (work->dimensions[0] < lwork) { printf("%d %d\n", work->dimensions[0], lwork); PYERR(PyExc_ValueError, "work is too small"); } } else { /* initialize our own work array */ dim1[0] = lwork; work = (PyArrayObject *) PyArray_SimpleNew(1, dim1, NPY_DOUBLE); } /* work */ if (piwork != NULL) { if ((iwork = (PyArrayObject *) PyArray_CopyFromObject(piwork, NPY_INT, 1, 1)) == NULL) { PYERR(PyExc_ValueError, "could not convert iwork to a suitable array"); } if (iwork->dimensions[0] < liwork) { PYERR(PyExc_ValueError, "iwork is too small"); } } else { /* initialize our own iwork array */ dim1[0] = liwork; iwork = (PyArrayObject *) PyArray_SimpleNew(1, dim1, NPY_INT); } /* iwork */ /* check if what JOB requests can be done with what the user has input into the function */ if ((job / 10) % 10 >= 2) { /* derivatives are supposed to be supplied */ if (fjacb == NULL || fjacd == NULL) { PYERR(PyExc_ValueError, "need fjacb and fjacd to calculate derivatives"); } } /* setup the global data for the callback */ odr_global.fcn = fcn; Py_INCREF(fcn); odr_global.fjacb = fjacb; Py_XINCREF(fjacb); odr_global.fjacd = fjacd; Py_XINCREF(fjacd); odr_global.pyBeta = (PyObject *) beta; Py_INCREF(beta); odr_global.extra_args = extra_args; Py_XINCREF(extra_args); /* now call DODRC */ F_FUNC(dodrc,DODRC)(fcn_callback, &n, &m, &np, &nq, (double *)(beta->data), (double *)(y->data), &ldy, (double *)(x->data), &ldx, (double *)(we->data), &ldwe, &ld2we, (double *)(wd->data), &ldwd, &ld2wd, (int *)(ifixb->data), (int *)(ifixx->data), &ldifx, &job, &ndigit, &taufac, &sstol, &partol, &maxit, &iprint, &lunerr, &lunrpt, (double *)(stpb->data), (double *)(stpd->data), &ldstpd, (double *)(sclb->data), (double *)(scld->data), &ldscld, (double *)(work->data), &lwork, (int *)(iwork->data), &liwork, &info); result = gen_output(n, m, np, nq, ldwe, ld2we, beta, work, iwork, isodr, info, full_output); if (result == NULL) PYERR(PyExc_RuntimeError, "could not generate output"); if (lunerr != -1) { F_FUNC(dlunc,DLUNC)(&lunerr); } if (lunrpt != -1) { F_FUNC(dlunc,DLUNC)(&lunrpt); } Py_DECREF(odr_global.fcn); Py_XDECREF(odr_global.fjacb); Py_XDECREF(odr_global.fjacd); Py_DECREF(odr_global.pyBeta); Py_XDECREF(odr_global.extra_args); odr_global.fcn = odr_global.fjacb = odr_global.fjacd = odr_global.pyBeta = odr_global.extra_args = NULL; Py_DECREF(beta); Py_DECREF(y); Py_DECREF(x); Py_DECREF(we); Py_DECREF(wd); Py_DECREF(ifixb); Py_DECREF(ifixx); Py_DECREF(stpb); Py_DECREF(stpd); Py_DECREF(sclb); Py_DECREF(scld); Py_DECREF(work); Py_DECREF(iwork); return result; fail: if (lunerr != -1) { F_FUNC(dlunc,DLUNC)(&lunerr); } if (lunrpt != -1) { F_FUNC(dlunc,DLUNC)(&lunrpt); } Py_XDECREF(beta); Py_XDECREF(y); Py_XDECREF(x); Py_XDECREF(we); Py_XDECREF(wd); Py_XDECREF(ifixb); Py_XDECREF(ifixx); Py_XDECREF(stpb); Py_XDECREF(stpd); Py_XDECREF(sclb); Py_XDECREF(scld); Py_XDECREF(work); Py_XDECREF(iwork); return NULL; }
static PyObject * Py_gssv(PyObject *self, PyObject *args, PyObject *kwdict) { PyObject *Py_B=NULL, *Py_X=NULL; PyArrayObject *nzvals=NULL; PyArrayObject *colind=NULL, *rowptr=NULL; int N, nnz; int info; int csc=0; int *perm_r=NULL, *perm_c=NULL; SuperMatrix A, B, L, U; superlu_options_t options; SuperLUStat_t stat; PyObject *option_dict = NULL; int type; int ssv_finished = 0; static char *kwlist[] = {"N","nnz","nzvals","colind","rowptr","B", "csc", "options",NULL}; /* Get input arguments */ if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO!O!O!O|iO", kwlist, &N, &nnz, &PyArray_Type, &nzvals, &PyArray_Type, &colind, &PyArray_Type, &rowptr, &Py_B, &csc, &option_dict)) { return NULL; } if (!_CHECK_INTEGER(colind) || !_CHECK_INTEGER(rowptr)) { PyErr_SetString(PyExc_TypeError, "colind and rowptr must be of type cint"); return NULL; } type = PyArray_TYPE(nzvals); if (!CHECK_SLU_TYPE(type)) { PyErr_SetString(PyExc_TypeError, "nzvals is not of a type supported by SuperLU"); return NULL; } if (!set_superlu_options_from_dict(&options, 0, option_dict, NULL, NULL)) { return NULL; } /* Create Space for output */ Py_X = PyArray_CopyFromObject(Py_B, type, 1, 2); if (Py_X == NULL) return NULL; if (csc) { if (NCFormat_from_spMatrix(&A, N, N, nnz, nzvals, colind, rowptr, type)) { Py_DECREF(Py_X); return NULL; } } else { if (NRFormat_from_spMatrix(&A, N, N, nnz, nzvals, colind, rowptr, type)) { Py_DECREF(Py_X); return NULL; } } if (DenseSuper_from_Numeric(&B, Py_X)) { Destroy_SuperMatrix_Store(&A); Py_DECREF(Py_X); return NULL; } /* B and Py_X share same data now but Py_X "owns" it */ /* Setup options */ if (setjmp(_superlu_py_jmpbuf)) { goto fail; } else { perm_c = intMalloc(N); perm_r = intMalloc(N); StatInit(&stat); /* Compute direct inverse of sparse Matrix */ gssv(type, &options, &A, perm_c, perm_r, &L, &U, &B, &stat, &info); } ssv_finished = 1; SUPERLU_FREE(perm_r); SUPERLU_FREE(perm_c); Destroy_SuperMatrix_Store(&A); /* holds just a pointer to the data */ Destroy_SuperMatrix_Store(&B); Destroy_SuperNode_Matrix(&L); Destroy_CompCol_Matrix(&U); StatFree(&stat); return Py_BuildValue("Ni", Py_X, info); fail: SUPERLU_FREE(perm_r); SUPERLU_FREE(perm_c); Destroy_SuperMatrix_Store(&A); /* holds just a pointer to the data */ Destroy_SuperMatrix_Store(&B); if (ssv_finished) { /* Avoid trying to free partially initialized matrices; might leak some memory, but avoids a crash */ Destroy_SuperNode_Matrix(&L); Destroy_CompCol_Matrix(&U); } StatFree(&stat); Py_XDECREF(Py_X); return NULL; }
static PyObject * PyGSL_odeiv_evolve_apply(PyGSL_odeiv_evolve *self, PyObject *args) { PyObject *result = NULL; PyObject *y0_o = NULL; PyArrayObject *volatile y0 = NULL, *volatile yout = NULL; double t=0, h=0, t1 = 0, flag; int dimension, r; assert(PyGSL_ODEIV_EVOLVE_Check(self)); FUNC_MESS_BEGIN(); if(! PyArg_ParseTuple(args, "dddO", &t, &t1, &h, &y0_o)){ return NULL; } dimension = self->step->system.dimension; y0 = PyGSL_PyArray_PREPARE_gsl_vector_view(y0_o, PyArray_DOUBLE, 1, dimension, 1, NULL); if(y0 == NULL) goto fail; yout = (PyArrayObject *) PyArray_CopyFromObject((PyObject * ) y0, PyArray_DOUBLE, 1, 1); if(yout == NULL) goto fail; if((flag=setjmp(self->step->buffer)) == 0){ FUNC_MESS("\t\t Setting Jmp Buffer"); } else { FUNC_MESS("\t\t Returning from Jmp Buffer"); goto fail; } r = gsl_odeiv_evolve_apply(self->evolve, self->control->control, self->step->step, &(self->step->system), &t, t1, &h, (double * )yout->data); if (GSL_SUCCESS != r){ goto fail; } assert(yout != NULL); result = Py_BuildValue("(ddO)", t, h, yout); /* Deleting the arrays */ /* Do I need to do that??? Not transfered Py_DECREF(yout); yout = NULL; */ Py_DECREF(y0); y0=NULL; FUNC_MESS_END(); return result; fail: FUNC_MESS("IN Fail"); PyGSL_add_traceback(module, this_file, odeiv_step_init_err_msg, __LINE__); Py_XDECREF(y0); Py_XDECREF(yout); FUNC_MESS("IN Fail End"); return NULL; }
/* Wrappers for the evaluation of the system */ static PyObject * PyGSL_odeiv_step_apply(PyGSL_odeiv_step *self, PyObject *args) { PyObject *result = NULL; PyObject *y0_o = NULL, *dydt_in_o = NULL; PyArrayObject *volatile y0 = NULL, * volatile yerr = NULL, *volatile dydt_in = NULL, *volatile dydt_out = NULL, *volatile yout = NULL; double t=0, h=0, *volatile dydt_in_d; int dimension, r, flag; FUNC_MESS_BEGIN(); assert(PyGSL_ODEIV_STEP_Check(self)); if(! PyArg_ParseTuple(args, "ddOOO", &t, &h, &y0_o, &dydt_in_o)){ return NULL; } dimension = self->system.dimension; y0 = PyGSL_PyArray_PREPARE_gsl_vector_view(y0_o, PyArray_DOUBLE, 1, dimension, 1, NULL); if(y0 == NULL) goto fail; if (Py_None == dydt_in_o){ dydt_in_d = NULL; }else{ dydt_in = PyGSL_PyArray_PREPARE_gsl_vector_view(dydt_in_o, PyArray_DOUBLE, 1, dimension, 2, NULL); if(dydt_in == NULL) goto fail; dydt_in_d = (double *) dydt_in->data; } dydt_out = (PyArrayObject *) PyArray_FromDims(1, &dimension, PyArray_DOUBLE); if (dydt_out == NULL) goto fail; yerr = (PyArrayObject *) PyArray_FromDims(1, &dimension, PyArray_DOUBLE); if(yerr == NULL) goto fail; yout = (PyArrayObject *) PyArray_CopyFromObject((PyObject * ) y0, PyArray_DOUBLE, 1, 1); if(yout == NULL) goto fail; if((flag=setjmp(self->buffer)) == 0){ FUNC_MESS("\t\t Setting Jmp Buffer"); } else { FUNC_MESS("\t\t Returning from Jmp Buffer"); goto fail; } r = gsl_odeiv_step_apply(self->step, t, h, (double *) yout->data, (double *) yerr->data, dydt_in_d, (double *) dydt_out->data, &(self->system)); if (GSL_SUCCESS != r){ PyErr_SetString(PyExc_TypeError, "Error While evaluating gsl_odeiv"); goto fail; } FUNC_MESS(" Returnlist create "); assert(yout != NULL); assert(yerr != NULL); assert(dydt_out != NULL); result = Py_BuildValue("(OOO)", yout, yerr, dydt_out); FUNC_MESS(" Memory free "); /* Deleting the arrays */ Py_DECREF(y0); y0 = NULL; Py_DECREF(yout); yout = NULL; Py_DECREF(yerr); yerr = NULL; Py_DECREF(dydt_out); dydt_out = NULL; /* This array does not need to exist ... */ Py_XDECREF(dydt_in); dydt_in=NULL; FUNC_MESS_END(); return result; fail: FUNC_MESS("IN Fail"); Py_XDECREF(y0); Py_XDECREF(yout); Py_XDECREF(yerr); Py_XDECREF(dydt_in); Py_XDECREF(dydt_out); FUNC_MESS("IN Fail End"); return NULL; }
static PyObject *Py_sgssv (PyObject *self, PyObject *args, PyObject *kwdict) { PyObject *Py_B=NULL, *Py_X=NULL; PyArrayObject *nzvals=NULL; PyArrayObject *colind=NULL, *rowptr=NULL; int N, nnz; int info; int csc=0, permc_spec=2; int *perm_r=NULL, *perm_c=NULL; SuperMatrix A, B, L, U; superlu_options_t options; SuperLUStat_t stat; static char *kwlist[] = {"N","nnz","nzvals","colind","rowptr","B", "csc", "permc_spec",NULL}; /* Get input arguments */ if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO!O!O!O|ii", kwlist, &N, &nnz, &PyArray_Type, &nzvals, &PyArray_Type, &colind, &PyArray_Type, &rowptr, &Py_B, &csc, &permc_spec)) return NULL; if (!_CHECK_INTEGER(colind) || !_CHECK_INTEGER(rowptr)) { PyErr_SetString(PyExc_TypeError, "colind and rowptr must be of type cint"); return NULL; } /* Create Space for output */ Py_X = PyArray_CopyFromObject(Py_B,PyArray_FLOAT,1,2); if (Py_X == NULL) return NULL; if (csc) { if (NCFormat_from_spMatrix(&A, N, N, nnz, nzvals, colind, rowptr, PyArray_FLOAT)) { Py_DECREF(Py_X); return NULL; } } else { if (NRFormat_from_spMatrix(&A, N, N, nnz, nzvals, colind, rowptr, PyArray_FLOAT)) { Py_DECREF(Py_X); return NULL; } } if (DenseSuper_from_Numeric(&B, Py_X)) { Destroy_SuperMatrix_Store(&A); Py_DECREF(Py_X); return NULL; } /* B and Py_X share same data now but Py_X "owns" it */ /* Setup options */ if (setjmp(_superlu_py_jmpbuf)) goto fail; else { perm_c = intMalloc(N); perm_r = intMalloc(N); set_default_options(&options); options.ColPerm=superlu_module_getpermc(permc_spec); StatInit(&stat); /* Compute direct inverse of sparse Matrix */ sgssv(&options, &A, perm_c, perm_r, &L, &U, &B, &stat, &info); } SUPERLU_FREE(perm_r); SUPERLU_FREE(perm_c); Destroy_SuperMatrix_Store(&A); Destroy_SuperMatrix_Store(&B); Destroy_SuperNode_Matrix(&L); Destroy_CompCol_Matrix(&U); StatFree(&stat); return Py_BuildValue("Ni", Py_X, info); fail: SUPERLU_FREE(perm_r); SUPERLU_FREE(perm_c); Destroy_SuperMatrix_Store(&A); Destroy_SuperMatrix_Store(&B); Destroy_SuperNode_Matrix(&L); Destroy_CompCol_Matrix(&U); StatFree(&stat); Py_XDECREF(Py_X); return NULL; }
/* * Converts a numpy ndarray to a Java primitive array. * * @param env the JNI environment * @param param the ndarray to convert * @param desiredType the desired type of the resulting primitive array, or * NULL if it should determine type based on the dtype * * @return a Java primitive array, or NULL if there were errors */ jarray convert_pyndarray_jprimitivearray(JNIEnv* env, PyObject *param, jclass desiredType) { jarray arr = NULL; PyArrayObject *copy = NULL; enum NPY_TYPES paType; jsize sz; if (!npy_array_check(param)) { PyErr_Format(PyExc_TypeError, "convert_pyndarray must receive an ndarray"); return NULL; } // determine what we can about the pyarray that is to be converted sz = (jsize) PyArray_Size(param); paType = PyArray_TYPE((PyArrayObject *) param); if (desiredType == NULL) { if (paType == NPY_BOOL) { desiredType = JBOOLEAN_ARRAY_TYPE; } else if (paType == NPY_BYTE) { desiredType = JBYTE_ARRAY_TYPE; } else if (paType == NPY_INT16) { desiredType = JSHORT_ARRAY_TYPE; } else if (paType == NPY_INT32) { desiredType = JINT_ARRAY_TYPE; } else if (paType == NPY_INT64) { desiredType = JLONG_ARRAY_TYPE; } else if (paType == NPY_FLOAT32) { desiredType = JFLOAT_ARRAY_TYPE; } else if (paType == NPY_FLOAT64) { desiredType = JDOUBLE_ARRAY_TYPE; } else { PyErr_Format(PyExc_TypeError, "Unable to determine corresponding Java type for ndarray"); return NULL; } } /* * TODO we could speed this up if we could skip the copy, but the copy makes * it safer by enforcing the correct length in bytes for the type */ copy = (PyArrayObject *) PyArray_CopyFromObject(param, paType, 0, 0); if ((*env)->IsSameObject(env, desiredType, JBOOLEAN_ARRAY_TYPE) && (paType == NPY_BOOL)) { arr = (*env)->NewBooleanArray(env, sz); } else if ((*env)->IsSameObject(env, desiredType, JBYTE_ARRAY_TYPE) && (paType == NPY_BYTE)) { arr = (*env)->NewByteArray(env, sz); } else if ((*env)->IsSameObject(env, desiredType, JSHORT_ARRAY_TYPE) && (paType == NPY_INT16)) { arr = (*env)->NewShortArray(env, sz); } else if ((*env)->IsSameObject(env, desiredType, JINT_ARRAY_TYPE) && (paType == NPY_INT32)) { arr = (*env)->NewIntArray(env, sz); } else if ((*env)->IsSameObject(env, desiredType, JLONG_ARRAY_TYPE) && (paType == NPY_INT64)) { arr = (*env)->NewLongArray(env, sz); } else if ((*env)->IsSameObject(env, desiredType, JFLOAT_ARRAY_TYPE) && (paType == NPY_FLOAT32)) { arr = (*env)->NewFloatArray(env, sz); } else if ((*env)->IsSameObject(env, desiredType, JDOUBLE_ARRAY_TYPE) && (paType == NPY_FLOAT64)) { arr = (*env)->NewDoubleArray(env, sz); } else { Py_XDECREF(copy); PyErr_Format(PyExc_RuntimeError, "Error matching ndarray.dtype to Java primitive type"); return NULL; } /* * java exception could potentially be OutOfMemoryError if it * couldn't allocate the array */ if (process_java_exception(env) || !arr) { Py_XDECREF(copy); return NULL; } // if arr was allocated, we already know it matched the python array type if (paType == NPY_BOOL) { (*env)->SetBooleanArrayRegion(env, arr, 0, sz, (const jboolean *) PyArray_DATA(copy)); } else if (paType == NPY_BYTE) { (*env)->SetByteArrayRegion(env, arr, 0, sz, (const jbyte *) PyArray_DATA(copy)); } else if (paType == NPY_INT16) { (*env)->SetShortArrayRegion(env, arr, 0, sz, (const jshort *) PyArray_DATA(copy)); } else if (paType == NPY_INT32) { (*env)->SetIntArrayRegion(env, arr, 0, sz, (const jint *) PyArray_DATA(copy)); } else if (paType == NPY_INT64) { (*env)->SetLongArrayRegion(env, arr, 0, sz, (const jlong *) PyArray_DATA(copy)); } else if (paType == NPY_FLOAT32) { (*env)->SetFloatArrayRegion(env, arr, 0, sz, (const jfloat *) PyArray_DATA(copy)); } else if (paType == NPY_FLOAT64) { (*env)->SetDoubleArrayRegion(env, arr, 0, sz, (const jdouble *) PyArray_DATA(copy)); } Py_XDECREF(copy); if (process_java_exception(env)) { PyErr_Format(PyExc_RuntimeError, "Error setting Java primitive array region"); return NULL; } return arr; }