NPY_NO_EXPORT PyObject * array_big_item(PyArrayObject *self, intp i) { char *item; PyArrayObject *r; if(self->nd == 0) { PyErr_SetString(PyExc_IndexError, "0-d arrays can't be indexed"); return NULL; } if ((item = index2ptr(self, i)) == NULL) { return NULL; } Py_INCREF(self->descr); r = (PyArrayObject *)PyArray_NewFromDescr(Py_TYPE(self), self->descr, self->nd-1, self->dimensions+1, self->strides+1, item, self->flags, (PyObject *)self); if (r == NULL) { return NULL; } Py_INCREF(self); r->base = (PyObject *)self; PyArray_UpdateFlags(r, CONTIGUOUS | FORTRAN); return (PyObject *)r; }
PyObject* JacobianP(double t, double *x, double *p) { PyObject *OutObj = NULL; PyObject *JacPOut = NULL; double *jactual = NULL, **jtemp = NULL; int i, n, m; _init_numpy(); if( (gIData == NULL) || (gIData->isInitBasic == 0) || (gIData->hasJacP == 0) || (gIData->paramDim == 0) ) { Py_INCREF(Py_None); return Py_None; } else if( (gIData->nExtInputs > 0) && (gIData->isInitExtInputs == 0) ) { Py_INCREF(Py_None); return Py_None; } else { OutObj = PyTuple_New(1); assert(OutObj); n = gIData->phaseDim; m = gIData->paramDim; jactual = (double *)PyMem_Malloc(n*m*sizeof(double)); assert(jactual); jtemp = (double **)PyMem_Malloc(n*sizeof(double *)); assert(jtemp); for( i = 0; i < n; i++ ) { jtemp[i] = jactual + m * i; } if( gIData->nExtInputs > 0 ) { FillCurrentExtInputValues( gIData, t ); } /* Assume jacobianParam is returned in column-major format */ jacobianParam(gIData->phaseDim, gIData->paramDim, t, x, p, jtemp, gIData->extraSpaceSize, gIData->gExtraSpace, gIData->nExtInputs, gIData->gCurrentExtInputVals); PyMem_Free(jtemp); npy_intp dims[2] = {gIData->paramDim, gIData->phaseDim}; JacPOut = PyArray_SimpleNewFromData(2, dims, NPY_DOUBLE, jactual); if(JacPOut) { PyArray_UpdateFlags((PyArrayObject *)JacPOut, NPY_ARRAY_CARRAY | NPY_ARRAY_OWNDATA); PyTuple_SetItem(OutObj, 0, PyArray_Transpose((PyArrayObject *)JacPOut, NULL)); return OutObj; } else { PyMem_Free(jactual); Py_INCREF(Py_None); return Py_None; } } }
PyObject* MassMatrix(double t, double *x, double *p) { PyObject *OutObj = NULL; PyObject *MassOut = NULL; double *mmactual = NULL, **mmtemp = NULL; int i, j, n; import_array(); if( (gIData == NULL) || (gIData->isInitBasic == 0) || (gIData->hasMass == 0) ) { Py_INCREF(Py_None); return Py_None; } else if( (gIData->nExtInputs > 0) && (gIData->isInitExtInputs == 0) ) { Py_INCREF(Py_None); return Py_None; } else { OutObj = PyTuple_New(1); assert(OutObj); n = gIData->phaseDim; mmactual = (double *)PyMem_Malloc(n*n*sizeof(double)); assert(mmactual); mmtemp = (double **)PyMem_Malloc(n*sizeof(double *)); assert(mmtemp); for( i = 0; i < n; i++ ) { mmtemp[i] = mmactual + n * i; } if( gIData->nExtInputs > 0 ) { FillCurrentExtInputValues( gIData, t ); } /* Assume massMatrix is returned in column-major format */ massMatrix(gIData->phaseDim, gIData->paramDim, t, x, p, mmtemp, gIData->extraSpaceSize, gIData->gExtraSpace, gIData->nExtInputs, gIData->gCurrentExtInputVals); PyMem_Free(mmtemp); npy_intp dims[2] = {gIData->phaseDim, gIData->phaseDim}; MassOut = PyArray_SimpleNewFromData(2, dims, NPY_DOUBLE, mmactual); if(MassOut) { PyArray_UpdateFlags((PyArrayObject *)MassOut, NPY_ARRAY_CARRAY | NPY_ARRAY_OWNDATA); PyTuple_SetItem(OutObj, 0, PyArray_Transpose((PyArrayObject *)MassOut, NULL)); return OutObj; } else { PyMem_Free(mmactual); Py_INCREF(Py_None); return Py_None; } } }
PyObject* AuxFunc(double t, double *x, double *p) { PyObject *OutObj = NULL; PyObject *AuxOut = NULL; double *ftemp = NULL; int i; import_array(); if( (gIData == NULL) || (gIData->isInitBasic == 0) || (gIData->nAuxVars == 0) ) { Py_INCREF(Py_None); return Py_None; } else if( (gIData->nExtInputs > 0) && (gIData->isInitExtInputs == 0) ) { Py_INCREF(Py_None); return Py_None; } else { OutObj = PyTuple_New(1); assert(OutObj); ftemp = (double *)PyMem_Malloc((gIData->nAuxVars)*sizeof(double)); assert(ftemp); if( gIData->nExtInputs > 0 ) { FillCurrentExtInputValues( gIData, t ); } auxvars(gIData->phaseDim, gIData->phaseDim, t, x, p, ftemp, gIData->extraSpaceSize, gIData->gExtraSpace, gIData->nExtInputs, gIData->gCurrentExtInputVals); npy_intp dims[1] = {gIData->nAuxVars}; AuxOut = PyArray_SimpleNewFromData(1, dims, NPY_DOUBLE, ftemp); if(AuxOut) { PyArray_UpdateFlags((PyArrayObject *)AuxOut, NPY_ARRAY_CARRAY | NPY_ARRAY_OWNDATA); PyTuple_SetItem(OutObj, 0, AuxOut); return OutObj; } else { PyMem_Free(ftemp); Py_INCREF(Py_None); return Py_None; } return OutObj; } }
PyObject* Vfield(double t, double *x, double *p) { PyObject *OutObj = NULL; PyObject *PointsOut = NULL; double *ftemp = NULL; _init_numpy(); if( (gIData == NULL) || (gIData->isInitBasic == 0) ) { Py_INCREF(Py_None); return Py_None; } else if( (gIData->nExtInputs > 0) && (gIData->isInitExtInputs == 0) ) { Py_INCREF(Py_None); return Py_None; } else { OutObj = PyTuple_New(1); assert(OutObj); ftemp = (double *)PyMem_Malloc((gIData->phaseDim)*sizeof(double)); assert(ftemp); if( gIData->nExtInputs > 0 ) { FillCurrentExtInputValues( gIData, t ); } vfieldfunc(gIData->phaseDim, gIData->paramDim, t, x, p, ftemp, gIData->extraSpaceSize, gIData->gExtraSpace, gIData->nExtInputs, gIData->gCurrentExtInputVals); npy_intp dims[1] = {gIData->phaseDim}; PointsOut = PyArray_SimpleNewFromData(1, dims, NPY_DOUBLE, ftemp); if(!PointsOut) { PyMem_Free(ftemp); Py_INCREF(Py_None); return Py_None; } PyArray_UpdateFlags((PyArrayObject *)PointsOut, NPY_ARRAY_CARRAY | NPY_ARRAY_OWNDATA); PyTuple_SetItem(OutObj, 0, PointsOut); return OutObj; } }
static PyObject * array_slice(PyArrayObject *self, Py_ssize_t ilow, Py_ssize_t ihigh) { PyArrayObject *ret; PyArray_Descr *dtype; Py_ssize_t dim0; char *data; npy_intp shape[NPY_MAXDIMS]; if (PyArray_NDIM(self) == 0) { PyErr_SetString(PyExc_ValueError, "cannot slice a 0-d array"); return NULL; } dim0 = PyArray_DIM(self, 0); if (ilow < 0) { ilow = 0; } else if (ilow > dim0) { ilow = dim0; } if (ihigh < ilow) { ihigh = ilow; } else if (ihigh > dim0) { ihigh = dim0; } data = PyArray_DATA(self); if (ilow < ihigh) { data += ilow * PyArray_STRIDE(self, 0); } /* Same shape except dimension 0 */ shape[0] = ihigh - ilow; memcpy(shape+1, PyArray_DIMS(self) + 1, (PyArray_NDIM(self)-1)*sizeof(npy_intp)); dtype = PyArray_DESCR(self); Py_INCREF(dtype); ret = (PyArrayObject *)PyArray_NewFromDescr(Py_TYPE(self), dtype, PyArray_NDIM(self), shape, PyArray_STRIDES(self), data, PyArray_FLAGS(self) & ~(NPY_ARRAY_MASKNA | NPY_ARRAY_OWNMASKNA), (PyObject *)self); if (ret == NULL) { return NULL; } Py_INCREF(self); if (PyArray_SetBaseObject(ret, (PyObject *)self) < 0) { Py_DECREF(ret); return NULL; } PyArray_UpdateFlags(ret, NPY_ARRAY_UPDATE_ALL); /* Also take a view of the NA mask if it exists */ if (PyArray_HASMASKNA(self)) { PyArrayObject_fields *fret = (PyArrayObject_fields *)ret; fret->maskna_dtype = PyArray_MASKNA_DTYPE(self); Py_INCREF(fret->maskna_dtype); data = PyArray_MASKNA_DATA(self); if (ilow < ihigh) { data += ilow * PyArray_MASKNA_STRIDES(self)[0]; } fret->maskna_data = data; memcpy(fret->maskna_strides, PyArray_MASKNA_STRIDES(self), PyArray_NDIM(self) * sizeof(npy_intp)); /* This view doesn't own the mask */ fret->flags |= NPY_ARRAY_MASKNA; fret->flags &= ~NPY_ARRAY_OWNMASKNA; } return (PyObject *)ret; }