int compute_lrw_liw(int *lrw, int *liw, int neq, int jt, int ml, int mu, int mxordn, int mxords) { int lrn, lrs, nyh, lmat; if (jt == 1 || jt == 2) { lmat = neq*neq + 2; } else if (jt == 4 || jt == 5) { lmat = (2*ml + mu + 1)*neq + 2; } else { PYERR(odepack_error,"Incorrect value for jt"); } if (mxordn < 0) { PYERR(odepack_error,"Incorrect value for mxordn"); } if (mxords < 0) { PYERR(odepack_error,"Incorrect value for mxords"); } nyh = neq; lrn = 20 + nyh*(mxordn+1) + 3*neq; lrs = 20 + nyh*(mxords+1) + 3*neq + lmat; *lrw = PyArray_MAX(lrn,lrs); *liw = 20 + neq; return 0; fail: return -1; }
static PyObject *IIRsymorder2(PyObject *NPY_UNUSED(dummy), PyObject *args) { PyObject *sig=NULL; PyArrayObject *a_sig=NULL, *out=NULL; double r, omega; double precision = -1.0; int thetype, N, ret; npy_intp outstrides, instrides; if (!PyArg_ParseTuple(args, "Odd|d", &sig, &r, &omega, &precision)) return NULL; thetype = PyArray_ObjectType(sig, PyArray_FLOAT); thetype = NPY_MIN(thetype, PyArray_DOUBLE); a_sig = (PyArrayObject *)PyArray_FromObject(sig, thetype, 1, 1); if ((a_sig == NULL)) goto fail; out = (PyArrayObject *)PyArray_SimpleNew(1,DIMS(a_sig),thetype); if (out == NULL) goto fail; N = DIMS(a_sig)[0]; convert_strides(STRIDES(a_sig), &instrides, ELSIZE(a_sig), 1); outstrides = 1; switch (thetype) { case PyArray_FLOAT: if ((precision <= 0.0) || (precision > 1.0)) precision = 1e-6; ret = S_IIR_forback2 (r, omega, (float *)DATA(a_sig), (float *)DATA(out), N, instrides, outstrides, precision); break; case PyArray_DOUBLE: if ((precision <= 0.0) || (precision > 1.0)) precision = 1e-11; ret = D_IIR_forback2 (r, omega, (double *)DATA(a_sig), (double *)DATA(out), N, instrides, outstrides, precision); break; default: PYERR("Incorrect type."); } if (ret < 0) PYERR("Problem occured inside routine."); Py_DECREF(a_sig); return PyArray_Return(out); fail: Py_XDECREF(a_sig); Py_XDECREF(out); return NULL; }
static PyObject *qspline2d(PyObject *NPY_UNUSED(dummy), PyObject *args) { PyObject *image=NULL; PyArrayObject *a_image=NULL, *ck=NULL; double lambda = 0.0; double precision = -1.0; int thetype, M, N, retval=0; npy_intp outstrides[2], instrides[2]; if (!PyArg_ParseTuple(args, "O|dd", &image, &lambda, &precision)) return NULL; if (lambda != 0.0) PYERR("Smoothing spline not yet implemented."); thetype = PyArray_ObjectType(image, PyArray_FLOAT); thetype = NPY_MIN(thetype, PyArray_DOUBLE); a_image = (PyArrayObject *)PyArray_FromObject(image, thetype, 2, 2); if (a_image == NULL) goto fail; ck = (PyArrayObject *)PyArray_SimpleNew(2,DIMS(a_image),thetype); if (ck == NULL) goto fail; M = DIMS(a_image)[0]; N = DIMS(a_image)[1]; convert_strides(STRIDES(a_image), instrides, ELSIZE(a_image), 2); outstrides[0] = N; outstrides[1] = 1; if (thetype == PyArray_FLOAT) { if ((precision <= 0.0) || (precision > 1.0)) precision = 1e-3; retval = S_quadratic_spline2D((float *)DATA(a_image), (float *)DATA(ck), M, N, lambda, instrides, outstrides, precision); } else if (thetype == PyArray_DOUBLE) { if ((precision <= 0.0) || (precision > 1.0)) precision = 1e-6; retval = D_quadratic_spline2D((double *)DATA(a_image), (double *)DATA(ck), M, N, lambda, instrides, outstrides, precision); } if (retval == -3) PYERR("Precision too high. Error did not converge."); if (retval < 0) PYERR("Problem occured inside routine"); Py_DECREF(a_image); return PyArray_Return(ck); fail: Py_XDECREF(a_image); Py_XDECREF(ck); return NULL; }
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 * odepack_odeint(PyObject *dummy, PyObject *args, PyObject *kwdict) { PyObject *fcn, *y0, *p_tout, *o_rtol = NULL, *o_atol = NULL; PyArrayObject *ap_y = NULL, *ap_yout = NULL; PyArrayObject *ap_rtol = NULL, *ap_atol = NULL; PyArrayObject *ap_tout = NULL; PyObject *extra_args = NULL; PyObject *Dfun = Py_None; int neq, itol = 1, itask = 1, istate = 1, iopt = 0, lrw, *iwork, liw, jt = 4; double *y, t, *tout, *rtol, *atol, *rwork; double h0 = 0.0, hmax = 0.0, hmin = 0.0; int ixpr = 0, mxstep = 0, mxhnil = 0, mxordn = 12, mxords = 5, ml = -1, mu = -1; PyObject *o_tcrit = NULL; PyArrayObject *ap_tcrit = NULL; PyArrayObject *ap_hu = NULL, *ap_tcur = NULL, *ap_tolsf = NULL, *ap_tsw = NULL; PyArrayObject *ap_nst = NULL, *ap_nfe = NULL, *ap_nje = NULL, *ap_nqu = NULL; PyArrayObject *ap_mused = NULL; int imxer = 0, lenrw = 0, leniw = 0, col_deriv = 0; npy_intp out_sz = 0, dims[2]; int k, ntimes, crit_ind = 0; int allocated = 0, full_output = 0, numcrit = 0; double *yout, *yout_ptr, *tout_ptr, *tcrit; double *wa; static char *kwlist[] = {"fun", "y0", "t", "args", "Dfun", "col_deriv", "ml", "mu", "full_output", "rtol", "atol", "tcrit", "h0", "hmax", "hmin", "ixpr", "mxstep", "mxhnil", "mxordn", "mxords", NULL}; odepack_params save_params; if (!PyArg_ParseTupleAndKeywords(args, kwdict, "OOO|OOiiiiOOOdddiiiii", kwlist, &fcn, &y0, &p_tout, &extra_args, &Dfun, &col_deriv, &ml, &mu, &full_output, &o_rtol, &o_atol, &o_tcrit, &h0, &hmax, &hmin, &ixpr, &mxstep, &mxhnil, &mxordn, &mxords)) { return NULL; } if (o_tcrit == Py_None) { o_tcrit = NULL; } if (o_rtol == Py_None) { o_rtol = NULL; } if (o_atol == Py_None) { o_atol = NULL; } /* Set up jt, ml, and mu */ if (Dfun == Py_None) { /* set jt for internally generated */ jt++; } if (ml < 0 && mu < 0) { /* neither ml nor mu given, mark jt for full jacobian */ jt -= 3; } if (ml < 0) { /* if one but not both are given */ ml = 0; } if (mu < 0) { mu = 0; } /* Stash the current global_params in save_params. */ memcpy(&save_params, &global_params, sizeof(save_params)); if (extra_args == NULL) { if ((extra_args = PyTuple_New(0)) == NULL) { goto fail; } } else { Py_INCREF(extra_args); /* We decrement on exit. */ } if (!PyTuple_Check(extra_args)) { PYERR(odepack_error, "Extra arguments must be in a tuple"); } if (!PyCallable_Check(fcn) || (Dfun != Py_None && !PyCallable_Check(Dfun))) { PYERR(odepack_error, "The function and its Jacobian must be callable functions."); } /* Set global_params from the function arguments. */ global_params.python_function = fcn; global_params.extra_arguments = extra_args; global_params.python_jacobian = Dfun; global_params.jac_transpose = !(col_deriv); global_params.jac_type = jt; /* Initial input vector */ ap_y = (PyArrayObject *) PyArray_ContiguousFromObject(y0, NPY_DOUBLE, 0, 0); if (ap_y == NULL) { goto fail; } if (PyArray_NDIM(ap_y) > 1) { PyErr_SetString(PyExc_ValueError, "Initial condition y0 must be one-dimensional."); goto fail; } y = (double *) PyArray_DATA(ap_y); neq = PyArray_Size((PyObject *) ap_y); dims[1] = neq; /* Set of output times for integration */ ap_tout = (PyArrayObject *) PyArray_ContiguousFromObject(p_tout, NPY_DOUBLE, 0, 0); if (ap_tout == NULL) { goto fail; } if (PyArray_NDIM(ap_tout) > 1) { PyErr_SetString(PyExc_ValueError, "Output times t must be one-dimensional."); goto fail; } tout = (double *) PyArray_DATA(ap_tout); ntimes = PyArray_Size((PyObject *)ap_tout); dims[0] = ntimes; t = tout[0]; /* Setup array to hold the output evaluations*/ ap_yout= (PyArrayObject *) PyArray_SimpleNew(2,dims,NPY_DOUBLE); if (ap_yout== NULL) { goto fail; } yout = (double *) PyArray_DATA(ap_yout); /* Copy initial vector into first row of output */ memcpy(yout, y, neq*sizeof(double)); /* copy initial value to output */ yout_ptr = yout + neq; /* set output pointer to next position */ itol = setup_extra_inputs(&ap_rtol, o_rtol, &ap_atol, o_atol, &ap_tcrit, o_tcrit, &numcrit, neq); if (itol < 0 ) { goto fail; /* Something didn't work */ } rtol = (double *) PyArray_DATA(ap_rtol); atol = (double *) PyArray_DATA(ap_atol); if (o_tcrit != NULL) { tcrit = (double *)(PyArray_DATA(ap_tcrit)); } /* Find size of working arrays*/ if (compute_lrw_liw(&lrw, &liw, neq, jt, ml, mu, mxordn, mxords) < 0) { goto fail; } if ((wa = (double *)malloc(lrw*sizeof(double) + liw*sizeof(int)))==NULL) { PyErr_NoMemory(); goto fail; } allocated = 1; rwork = wa; iwork = (int *)(wa + lrw); iwork[0] = ml; iwork[1] = mu; if (h0 != 0.0 || hmax != 0.0 || hmin != 0.0 || ixpr != 0 || mxstep != 0 || mxhnil != 0 || mxordn != 0 || mxords != 0) { rwork[4] = h0; rwork[5] = hmax; rwork[6] = hmin; iwork[4] = ixpr; iwork[5] = mxstep; iwork[6] = mxhnil; iwork[7] = mxordn; iwork[8] = mxords; iopt = 1; } istate = 1; k = 1; /* If full output make some useful output arrays */ if (full_output) { out_sz = ntimes-1; ap_hu = (PyArrayObject *) PyArray_SimpleNew(1, &out_sz, NPY_DOUBLE); ap_tcur = (PyArrayObject *) PyArray_SimpleNew(1, &out_sz, NPY_DOUBLE); ap_tolsf = (PyArrayObject *) PyArray_SimpleNew(1, &out_sz, NPY_DOUBLE); ap_tsw = (PyArrayObject *) PyArray_SimpleNew(1, &out_sz, NPY_DOUBLE); ap_nst = (PyArrayObject *) PyArray_SimpleNew(1, &out_sz, NPY_INT); ap_nfe = (PyArrayObject *) PyArray_SimpleNew(1, &out_sz, NPY_INT); ap_nje = (PyArrayObject *) PyArray_SimpleNew(1, &out_sz, NPY_INT); ap_nqu = (PyArrayObject *) PyArray_SimpleNew(1, &out_sz, NPY_INT); ap_mused = (PyArrayObject *) PyArray_SimpleNew(1, &out_sz, NPY_INT); if (ap_hu == NULL || ap_tcur == NULL || ap_tolsf == NULL || ap_tsw == NULL || ap_nst == NULL || ap_nfe == NULL || ap_nje == NULL || ap_nqu == NULL || ap_mused == NULL) { goto fail; } } if (o_tcrit != NULL) { /* There are critical points */ itask = 4; rwork[0] = *tcrit; } while (k < ntimes && istate > 0) { /* loop over desired times */ tout_ptr = tout + k; /* Use tcrit if relevant */ if (itask == 4 && *tout_ptr > *(tcrit + crit_ind)) { crit_ind++; rwork[0] = *(tcrit+crit_ind); } if (crit_ind >= numcrit) { itask = 1; /* No more critical values */ } LSODA(ode_function, &neq, y, &t, tout_ptr, &itol, rtol, atol, &itask, &istate, &iopt, rwork, &lrw, iwork, &liw, ode_jacobian_function, &jt); if (full_output) { *((double *)PyArray_DATA(ap_hu) + (k-1)) = rwork[10]; *((double *)PyArray_DATA(ap_tcur) + (k-1)) = rwork[12]; *((double *)PyArray_DATA(ap_tolsf) + (k-1)) = rwork[13]; *((double *)PyArray_DATA(ap_tsw) + (k-1)) = rwork[14]; *((int *)PyArray_DATA(ap_nst) + (k-1)) = iwork[10]; *((int *)PyArray_DATA(ap_nfe) + (k-1)) = iwork[11]; *((int *)PyArray_DATA(ap_nje) + (k-1)) = iwork[12]; *((int *)PyArray_DATA(ap_nqu) + (k-1)) = iwork[13]; if (istate == -5 || istate == -4) { imxer = iwork[15]; } else { imxer = -1; } lenrw = iwork[16]; leniw = iwork[17]; *((int *)PyArray_DATA(ap_mused) + (k-1)) = iwork[18]; } if (PyErr_Occurred()) { goto fail; } memcpy(yout_ptr, y, neq*sizeof(double)); /* copy integration result to output*/ yout_ptr += neq; k++; } /* Restore global_params from the previously stashed save_params. */ memcpy(&global_params, &save_params, sizeof(save_params)); Py_DECREF(extra_args); Py_DECREF(ap_atol); Py_DECREF(ap_rtol); Py_XDECREF(ap_tcrit); Py_DECREF(ap_y); Py_DECREF(ap_tout); free(wa); /* Do Full output */ if (full_output) { return Py_BuildValue("N{s:N,s:N,s:N,s:N,s:N,s:N,s:N,s:N,s:i,s:i,s:i,s:N}i", PyArray_Return(ap_yout), "hu", PyArray_Return(ap_hu), "tcur", PyArray_Return(ap_tcur), "tolsf", PyArray_Return(ap_tolsf), "tsw", PyArray_Return(ap_tsw), "nst", PyArray_Return(ap_nst), "nfe", PyArray_Return(ap_nfe), "nje", PyArray_Return(ap_nje), "nqu", PyArray_Return(ap_nqu), "imxer", imxer, "lenrw", lenrw, "leniw", leniw, "mused", PyArray_Return(ap_mused), istate); } else { return Py_BuildValue("Ni", PyArray_Return(ap_yout), istate); } fail: /* Restore global_params from the previously stashed save_params. */ memcpy(&global_params, &save_params, sizeof(save_params)); Py_XDECREF(extra_args); Py_XDECREF(ap_y); Py_XDECREF(ap_rtol); Py_XDECREF(ap_atol); Py_XDECREF(ap_tcrit); Py_XDECREF(ap_tout); Py_XDECREF(ap_yout); if (allocated) { free(wa); } if (full_output) { Py_XDECREF(ap_hu); Py_XDECREF(ap_tcur); Py_XDECREF(ap_tolsf); Py_XDECREF(ap_tsw); Py_XDECREF(ap_nst); Py_XDECREF(ap_nfe); Py_XDECREF(ap_nje); Py_XDECREF(ap_nqu); Py_XDECREF(ap_mused); } return NULL; }
int setup_extra_inputs(PyArrayObject **ap_rtol, PyObject *o_rtol, PyArrayObject **ap_atol, PyObject *o_atol, PyArrayObject **ap_tcrit, PyObject *o_tcrit, int *numcrit, int neq) { int itol = 0; double tol = 1.49012e-8; npy_intp one = 1; /* Setup tolerances */ if (o_rtol == NULL) { *ap_rtol = (PyArrayObject *) PyArray_SimpleNew(1, &one, NPY_DOUBLE); if (*ap_rtol == NULL) { PYERR2(odepack_error,"Error constructing relative tolerance."); } *(double *) PyArray_DATA(*ap_rtol) = tol; /* Default */ } else { *ap_rtol = (PyArrayObject *) PyArray_ContiguousFromObject(o_rtol, NPY_DOUBLE, 0, 1); if (*ap_rtol == NULL) { PYERR2(odepack_error,"Error converting relative tolerance."); } /* XXX Fix the following. */ if (PyArray_NDIM(*ap_rtol) == 0); /* rtol is scalar */ else if (PyArray_DIMS(*ap_rtol)[0] == neq) { itol |= 2; /* Set rtol array flag */ } else { PYERR(odepack_error, "Tolerances must be an array of the same length as the\n number of equations or a scalar."); } } if (o_atol == NULL) { *ap_atol = (PyArrayObject *) PyArray_SimpleNew(1, &one, NPY_DOUBLE); if (*ap_atol == NULL) { PYERR2(odepack_error,"Error constructing absolute tolerance"); } *(double *)PyArray_DATA(*ap_atol) = tol; } else { *ap_atol = (PyArrayObject *) PyArray_ContiguousFromObject(o_atol, NPY_DOUBLE, 0, 1); if (*ap_atol == NULL) { PYERR2(odepack_error,"Error converting absolute tolerance."); } /* XXX Fix the following. */ if (PyArray_NDIM(*ap_atol) == 0); /* atol is scalar */ else if (PyArray_DIMS(*ap_atol)[0] == neq) { itol |= 1; /* Set atol array flag */ } else { PYERR(odepack_error,"Tolerances must be an array of the same length as the\n number of equations or a scalar."); } } itol++; /* increment to get correct value */ /* Setup t-critical */ if (o_tcrit != NULL) { *ap_tcrit = (PyArrayObject *) PyArray_ContiguousFromObject(o_tcrit, NPY_DOUBLE, 0, 1); if (*ap_tcrit == NULL) { PYERR2(odepack_error,"Error constructing critical times."); } *numcrit = PyArray_Size((PyObject *) (*ap_tcrit)); } return itol; fail: /* Needed for use of PYERR */ return -1; }
static PyObject *IIRsymorder1(PyObject *NPY_UNUSED(dummy), PyObject *args) { PyObject *sig=NULL; PyArrayObject *a_sig=NULL, *out=NULL; Py_complex c0, z1; double precision = -1.0; int thetype, N, ret; npy_intp outstrides, instrides; if (!PyArg_ParseTuple(args, "ODD|d", &sig, &c0, &z1, &precision)) return NULL; thetype = PyArray_ObjectType(sig, PyArray_FLOAT); thetype = NPY_MIN(thetype, PyArray_CDOUBLE); a_sig = (PyArrayObject *)PyArray_FromObject(sig, thetype, 1, 1); if ((a_sig == NULL)) goto fail; out = (PyArrayObject *)PyArray_SimpleNew(1,DIMS(a_sig),thetype); if (out == NULL) goto fail; N = DIMS(a_sig)[0]; convert_strides(STRIDES(a_sig), &instrides, ELSIZE(a_sig), 1); outstrides = 1; switch (thetype) { case PyArray_FLOAT: { float rc0 = c0.real; float rz1 = z1.real; if ((precision <= 0.0) || (precision > 1.0)) precision = 1e-6; ret = S_IIR_forback1 (rc0, rz1, (float *)DATA(a_sig), (float *)DATA(out), N, instrides, outstrides, (float )precision); } break; case PyArray_DOUBLE: { double rc0 = c0.real; double rz1 = z1.real; if ((precision <= 0.0) || (precision > 1.0)) precision = 1e-11; ret = D_IIR_forback1 (rc0, rz1, (double *)DATA(a_sig), (double *)DATA(out), N, instrides, outstrides, precision); } break; #ifdef __GNUC__ case PyArray_CFLOAT: { __complex__ float zc0 = c0.real + 1.0i*c0.imag; __complex__ float zz1 = z1.real + 1.0i*z1.imag; if ((precision <= 0.0) || (precision > 1.0)) precision = 1e-6; ret = C_IIR_forback1 (zc0, zz1, (__complex__ float *)DATA(a_sig), (__complex__ float *)DATA(out), N, instrides, outstrides, (float )precision); } break; case PyArray_CDOUBLE: { __complex__ double zc0 = c0.real + 1.0i*c0.imag; __complex__ double zz1 = z1.real + 1.0i*z1.imag; if ((precision <= 0.0) || (precision > 1.0)) precision = 1e-11; ret = Z_IIR_forback1 (zc0, zz1, (__complex__ double *)DATA(a_sig), (__complex__ double *)DATA(out), N, instrides, outstrides, precision); } break; #endif default: PYERR("Incorrect type."); } if (ret == 0) { Py_DECREF(a_sig); return PyArray_Return(out); } if (ret == -1) PYERR("Could not allocate enough memory."); if (ret == -2) PYERR("|z1| must be less than 1.0"); if (ret == -3) PYERR("Sum to find symmetric boundary conditions did not converge."); PYERR("Unknown error."); fail: Py_XDECREF(a_sig); Py_XDECREF(out); return NULL; }
static PyObject *FIRsepsym2d(PyObject *NPY_UNUSED(dummy), PyObject *args) { PyObject *image=NULL, *hrow=NULL, *hcol=NULL; PyArrayObject *a_image=NULL, *a_hrow=NULL, *a_hcol=NULL, *out=NULL; int thetype, M, N, ret; npy_intp outstrides[2], instrides[2]; if (!PyArg_ParseTuple(args, "OOO", &image, &hrow, &hcol)) return NULL; thetype = PyArray_ObjectType(image, PyArray_FLOAT); thetype = NPY_MIN(thetype, PyArray_CDOUBLE); a_image = (PyArrayObject *)PyArray_FromObject(image, thetype, 2, 2); a_hrow = (PyArrayObject *)PyArray_ContiguousFromObject(hrow, thetype, 1, 1); a_hcol = (PyArrayObject *)PyArray_ContiguousFromObject(hcol, thetype, 1, 1); if ((a_image == NULL) || (a_hrow == NULL) || (a_hcol==NULL)) goto fail; out = (PyArrayObject *)PyArray_SimpleNew(2,DIMS(a_image),thetype); if (out == NULL) goto fail; M = DIMS(a_image)[0]; N = DIMS(a_image)[1]; convert_strides(STRIDES(a_image), instrides, ELSIZE(a_image), 2); outstrides[0] = N; outstrides[1] = 1; switch (thetype) { case PyArray_FLOAT: ret = S_separable_2Dconvolve_mirror((float *)DATA(a_image), (float *)DATA(out), M, N, (float *)DATA(a_hrow), (float *)DATA(a_hcol), DIMS(a_hrow)[0], DIMS(a_hcol)[0], instrides, outstrides); break; case PyArray_DOUBLE: ret = D_separable_2Dconvolve_mirror((double *)DATA(a_image), (double *)DATA(out), M, N, (double *)DATA(a_hrow), (double *)DATA(a_hcol), DIMS(a_hrow)[0], DIMS(a_hcol)[0], instrides, outstrides); break; #ifdef __GNUC__ case PyArray_CFLOAT: ret = C_separable_2Dconvolve_mirror((__complex__ float *)DATA(a_image), (__complex__ float *)DATA(out), M, N, (__complex__ float *)DATA(a_hrow), (__complex__ float *)DATA(a_hcol), DIMS(a_hrow)[0], DIMS(a_hcol)[0], instrides, outstrides); break; case PyArray_CDOUBLE: ret = Z_separable_2Dconvolve_mirror((__complex__ double *)DATA(a_image), (__complex__ double *)DATA(out), M, N, (__complex__ double *)DATA(a_hrow), (__complex__ double *)DATA(a_hcol), DIMS(a_hrow)[0], DIMS(a_hcol)[0], instrides, outstrides); break; #endif default: PYERR("Incorrect type."); } if (ret < 0) PYERR("Problem occured inside routine."); Py_DECREF(a_image); Py_DECREF(a_hrow); Py_DECREF(a_hcol); return PyArray_Return(out); fail: Py_XDECREF(a_image); Py_XDECREF(a_hrow); Py_XDECREF(a_hcol); Py_XDECREF(out); return NULL; }