Blame numpy/fft/fftpack_litemodule.c

Packit 7a8e5e
#define NPY_NO_DEPRECATED_API NPY_API_VERSION
Packit 7a8e5e
Packit 7a8e5e
#include "Python.h"
Packit 7a8e5e
#include "numpy/arrayobject.h"
Packit 7a8e5e
#include "fftpack.h"
Packit 7a8e5e
Packit 7a8e5e
static PyObject *ErrorObject;
Packit 7a8e5e
Packit 7a8e5e
static const char fftpack_cfftf__doc__[] = "";
Packit 7a8e5e
Packit 7a8e5e
static PyObject *
Packit 7a8e5e
fftpack_cfftf(PyObject *NPY_UNUSED(self), PyObject *args)
Packit 7a8e5e
{
Packit 7a8e5e
    PyObject *op1, *op2;
Packit 7a8e5e
    PyArrayObject *data;
Packit 7a8e5e
    PyArray_Descr *descr;
Packit 7a8e5e
    double *wsave, *dptr;
Packit 7a8e5e
    npy_intp nsave;
Packit 7a8e5e
    int npts, nrepeats, i;
Packit 7a8e5e
Packit 7a8e5e
    if(!PyArg_ParseTuple(args, "OO:cfftf", &op1, &op2)) {
Packit 7a8e5e
        return NULL;
Packit 7a8e5e
    }
Packit 7a8e5e
    data = (PyArrayObject *)PyArray_CopyFromObject(op1,
Packit 7a8e5e
            NPY_CDOUBLE, 1, 0);
Packit 7a8e5e
    if (data == NULL) {
Packit 7a8e5e
        return NULL;
Packit 7a8e5e
    }
Packit 7a8e5e
    descr = PyArray_DescrFromType(NPY_DOUBLE);
Packit 7a8e5e
    if (PyArray_AsCArray(&op2, (void *)&wsave, &nsave, 1, descr) == -1) {
Packit 7a8e5e
        goto fail;
Packit 7a8e5e
    }
Packit 7a8e5e
    if (data == NULL) {
Packit 7a8e5e
        goto fail;
Packit 7a8e5e
    }
Packit 7a8e5e
Packit 7a8e5e
    npts = PyArray_DIM(data, PyArray_NDIM(data) - 1);
Packit 7a8e5e
    if (nsave != npts*4 + 15) {
Packit 7a8e5e
        PyErr_SetString(ErrorObject, "invalid work array for fft size");
Packit 7a8e5e
        goto fail;
Packit 7a8e5e
    }
Packit 7a8e5e
Packit 7a8e5e
    nrepeats = PyArray_SIZE(data)/npts;
Packit 7a8e5e
    dptr = (double *)PyArray_DATA(data);
Packit 7a8e5e
    Py_BEGIN_ALLOW_THREADS;
Packit 7a8e5e
    NPY_SIGINT_ON;
Packit 7a8e5e
    for (i = 0; i < nrepeats; i++) {
Packit 7a8e5e
        npy_cfftf(npts, dptr, wsave);
Packit 7a8e5e
        dptr += npts*2;
Packit 7a8e5e
    }
Packit 7a8e5e
    NPY_SIGINT_OFF;
Packit 7a8e5e
    Py_END_ALLOW_THREADS;
Packit 7a8e5e
    PyArray_Free(op2, (char *)wsave);
Packit 7a8e5e
    return (PyObject *)data;
Packit 7a8e5e
Packit 7a8e5e
fail:
Packit 7a8e5e
    PyArray_Free(op2, (char *)wsave);
Packit 7a8e5e
    Py_DECREF(data);
Packit 7a8e5e
    return NULL;
Packit 7a8e5e
}
Packit 7a8e5e
Packit 7a8e5e
static const char fftpack_cfftb__doc__[] = "";
Packit 7a8e5e
Packit 7a8e5e
static PyObject *
Packit 7a8e5e
fftpack_cfftb(PyObject *NPY_UNUSED(self), PyObject *args)
Packit 7a8e5e
{
Packit 7a8e5e
    PyObject *op1, *op2;
Packit 7a8e5e
    PyArrayObject *data;
Packit 7a8e5e
    PyArray_Descr *descr;
Packit 7a8e5e
    double *wsave, *dptr;
Packit 7a8e5e
    npy_intp nsave;
Packit 7a8e5e
    int npts, nrepeats, i;
Packit 7a8e5e
Packit 7a8e5e
    if(!PyArg_ParseTuple(args, "OO:cfftb", &op1, &op2)) {
Packit 7a8e5e
        return NULL;
Packit 7a8e5e
    }
Packit 7a8e5e
    data = (PyArrayObject *)PyArray_CopyFromObject(op1,
Packit 7a8e5e
            NPY_CDOUBLE, 1, 0);
Packit 7a8e5e
    if (data == NULL) {
Packit 7a8e5e
        return NULL;
Packit 7a8e5e
    }
Packit 7a8e5e
    descr = PyArray_DescrFromType(NPY_DOUBLE);
Packit 7a8e5e
    if (PyArray_AsCArray(&op2, (void *)&wsave, &nsave, 1, descr) == -1) {
Packit 7a8e5e
        goto fail;
Packit 7a8e5e
    }
Packit 7a8e5e
    if (data == NULL) {
Packit 7a8e5e
        goto fail;
Packit 7a8e5e
    }
Packit 7a8e5e
Packit 7a8e5e
    npts = PyArray_DIM(data, PyArray_NDIM(data) - 1);
Packit 7a8e5e
    if (nsave != npts*4 + 15) {
Packit 7a8e5e
        PyErr_SetString(ErrorObject, "invalid work array for fft size");
Packit 7a8e5e
        goto fail;
Packit 7a8e5e
    }
Packit 7a8e5e
Packit 7a8e5e
    nrepeats = PyArray_SIZE(data)/npts;
Packit 7a8e5e
    dptr = (double *)PyArray_DATA(data);
Packit 7a8e5e
    Py_BEGIN_ALLOW_THREADS;
Packit 7a8e5e
    NPY_SIGINT_ON;
Packit 7a8e5e
    for (i = 0; i < nrepeats; i++) {
Packit 7a8e5e
        npy_cfftb(npts, dptr, wsave);
Packit 7a8e5e
        dptr += npts*2;
Packit 7a8e5e
    }
Packit 7a8e5e
    NPY_SIGINT_OFF;
Packit 7a8e5e
    Py_END_ALLOW_THREADS;
Packit 7a8e5e
    PyArray_Free(op2, (char *)wsave);
Packit 7a8e5e
    return (PyObject *)data;
Packit 7a8e5e
Packit 7a8e5e
fail:
Packit 7a8e5e
    PyArray_Free(op2, (char *)wsave);
Packit 7a8e5e
    Py_DECREF(data);
Packit 7a8e5e
    return NULL;
Packit 7a8e5e
}
Packit 7a8e5e
Packit 7a8e5e
static const char fftpack_cffti__doc__[] = "";
Packit 7a8e5e
Packit 7a8e5e
static PyObject *
Packit 7a8e5e
fftpack_cffti(PyObject *NPY_UNUSED(self), PyObject *args)
Packit 7a8e5e
{
Packit 7a8e5e
    PyArrayObject *op;
Packit 7a8e5e
    npy_intp dim;
Packit 7a8e5e
    long n;
Packit 7a8e5e
Packit 7a8e5e
    if (!PyArg_ParseTuple(args, "l:cffti", &n)) {
Packit 7a8e5e
        return NULL;
Packit 7a8e5e
    }
Packit 7a8e5e
    /*Magic size needed by npy_cffti*/
Packit 7a8e5e
    dim = 4*n + 15;
Packit 7a8e5e
    /*Create a 1 dimensional array of dimensions of type double*/
Packit 7a8e5e
    op = (PyArrayObject *)PyArray_SimpleNew(1, &dim, NPY_DOUBLE);
Packit 7a8e5e
    if (op == NULL) {
Packit 7a8e5e
        return NULL;
Packit 7a8e5e
    }
Packit 7a8e5e
Packit 7a8e5e
    Py_BEGIN_ALLOW_THREADS;
Packit 7a8e5e
    NPY_SIGINT_ON;
Packit 7a8e5e
    npy_cffti(n, (double *)PyArray_DATA((PyArrayObject*)op));
Packit 7a8e5e
    NPY_SIGINT_OFF;
Packit 7a8e5e
    Py_END_ALLOW_THREADS;
Packit 7a8e5e
Packit 7a8e5e
    return (PyObject *)op;
Packit 7a8e5e
}
Packit 7a8e5e
Packit 7a8e5e
static const char fftpack_rfftf__doc__[] = "";
Packit 7a8e5e
Packit 7a8e5e
static PyObject *
Packit 7a8e5e
fftpack_rfftf(PyObject *NPY_UNUSED(self), PyObject *args)
Packit 7a8e5e
{
Packit 7a8e5e
    PyObject *op1, *op2;
Packit 7a8e5e
    PyArrayObject *data, *ret;
Packit 7a8e5e
    PyArray_Descr *descr;
Packit 7a8e5e
    double *wsave = NULL, *dptr, *rptr;
Packit 7a8e5e
    npy_intp nsave;
Packit 7a8e5e
    int npts, nrepeats, i, rstep;
Packit 7a8e5e
Packit 7a8e5e
    if(!PyArg_ParseTuple(args, "OO:rfftf", &op1, &op2)) {
Packit 7a8e5e
        return NULL;
Packit 7a8e5e
    }
Packit 7a8e5e
    data = (PyArrayObject *)PyArray_ContiguousFromObject(op1,
Packit 7a8e5e
            NPY_DOUBLE, 1, 0);
Packit 7a8e5e
    if (data == NULL) {
Packit 7a8e5e
        return NULL;
Packit 7a8e5e
    }
Packit 7a8e5e
    /* FIXME, direct access changing contents of data->dimensions */
Packit 7a8e5e
    npts = PyArray_DIM(data, PyArray_NDIM(data) - 1);
Packit 7a8e5e
    PyArray_DIMS(data)[PyArray_NDIM(data) - 1] = npts/2 + 1;
Packit 7a8e5e
    ret = (PyArrayObject *)PyArray_Zeros(PyArray_NDIM(data),
Packit 7a8e5e
            PyArray_DIMS(data), PyArray_DescrFromType(NPY_CDOUBLE), 0);
Packit 7a8e5e
    if (ret == NULL) {
Packit 7a8e5e
        goto fail;
Packit 7a8e5e
    }
Packit 7a8e5e
    PyArray_DIMS(data)[PyArray_NDIM(data) - 1] = npts;
Packit 7a8e5e
    rstep = PyArray_DIM(ret, PyArray_NDIM(ret) - 1)*2;
Packit 7a8e5e
Packit 7a8e5e
    descr = PyArray_DescrFromType(NPY_DOUBLE);
Packit 7a8e5e
    if (PyArray_AsCArray(&op2, (void *)&wsave, &nsave, 1, descr) == -1) {
Packit 7a8e5e
        goto fail;
Packit 7a8e5e
    }
Packit 7a8e5e
    if (data == NULL || ret == NULL) {
Packit 7a8e5e
        goto fail;
Packit 7a8e5e
    }
Packit 7a8e5e
    if (nsave != npts*2+15) {
Packit 7a8e5e
        PyErr_SetString(ErrorObject, "invalid work array for fft size");
Packit 7a8e5e
        goto fail;
Packit 7a8e5e
    }
Packit 7a8e5e
Packit 7a8e5e
    nrepeats = PyArray_SIZE(data)/npts;
Packit 7a8e5e
    rptr = (double *)PyArray_DATA(ret);
Packit 7a8e5e
    dptr = (double *)PyArray_DATA(data);
Packit 7a8e5e
Packit 7a8e5e
    Py_BEGIN_ALLOW_THREADS;
Packit 7a8e5e
    NPY_SIGINT_ON;
Packit 7a8e5e
    for (i = 0; i < nrepeats; i++) {
Packit 7a8e5e
        memcpy((char *)(rptr+1), dptr, npts*sizeof(double));
Packit 7a8e5e
        npy_rfftf(npts, rptr+1, wsave);
Packit 7a8e5e
        rptr[0] = rptr[1];
Packit 7a8e5e
        rptr[1] = 0.0;
Packit 7a8e5e
        rptr += rstep;
Packit 7a8e5e
        dptr += npts;
Packit 7a8e5e
    }
Packit 7a8e5e
    NPY_SIGINT_OFF;
Packit 7a8e5e
    Py_END_ALLOW_THREADS;
Packit 7a8e5e
    PyArray_Free(op2, (char *)wsave);
Packit 7a8e5e
    Py_DECREF(data);
Packit 7a8e5e
    return (PyObject *)ret;
Packit 7a8e5e
Packit 7a8e5e
fail:
Packit 7a8e5e
    PyArray_Free(op2, (char *)wsave);
Packit 7a8e5e
    Py_XDECREF(data);
Packit 7a8e5e
    Py_XDECREF(ret);
Packit 7a8e5e
    return NULL;
Packit 7a8e5e
}
Packit 7a8e5e
Packit 7a8e5e
static const char fftpack_rfftb__doc__[] = "";
Packit 7a8e5e
Packit 7a8e5e
static PyObject *
Packit 7a8e5e
fftpack_rfftb(PyObject *NPY_UNUSED(self), PyObject *args)
Packit 7a8e5e
{
Packit 7a8e5e
    PyObject *op1, *op2;
Packit 7a8e5e
    PyArrayObject *data, *ret;
Packit 7a8e5e
    PyArray_Descr *descr;
Packit 7a8e5e
    double *wsave, *dptr, *rptr;
Packit 7a8e5e
    npy_intp nsave;
Packit 7a8e5e
    int npts, nrepeats, i;
Packit 7a8e5e
Packit 7a8e5e
    if(!PyArg_ParseTuple(args, "OO:rfftb", &op1, &op2)) {
Packit 7a8e5e
        return NULL;
Packit 7a8e5e
    }
Packit 7a8e5e
    data = (PyArrayObject *)PyArray_ContiguousFromObject(op1,
Packit 7a8e5e
            NPY_CDOUBLE, 1, 0);
Packit 7a8e5e
    if (data == NULL) {
Packit 7a8e5e
        return NULL;
Packit 7a8e5e
    }
Packit 7a8e5e
    npts = PyArray_DIM(data, PyArray_NDIM(data) - 1);
Packit 7a8e5e
    ret = (PyArrayObject *)PyArray_Zeros(PyArray_NDIM(data), PyArray_DIMS(data),
Packit 7a8e5e
            PyArray_DescrFromType(NPY_DOUBLE), 0);
Packit 7a8e5e
Packit 7a8e5e
    descr = PyArray_DescrFromType(NPY_DOUBLE);
Packit 7a8e5e
    if (PyArray_AsCArray(&op2, (void *)&wsave, &nsave, 1, descr) == -1) {
Packit 7a8e5e
        goto fail;
Packit 7a8e5e
    }
Packit 7a8e5e
    if (data == NULL || ret == NULL) {
Packit 7a8e5e
        goto fail;
Packit 7a8e5e
    }
Packit 7a8e5e
    if (nsave != npts*2 + 15) {
Packit 7a8e5e
        PyErr_SetString(ErrorObject, "invalid work array for fft size");
Packit 7a8e5e
        goto fail;
Packit 7a8e5e
    }
Packit 7a8e5e
Packit 7a8e5e
    nrepeats = PyArray_SIZE(ret)/npts;
Packit 7a8e5e
    rptr = (double *)PyArray_DATA(ret);
Packit 7a8e5e
    dptr = (double *)PyArray_DATA(data);
Packit 7a8e5e
Packit 7a8e5e
    Py_BEGIN_ALLOW_THREADS;
Packit 7a8e5e
    NPY_SIGINT_ON;
Packit 7a8e5e
    for (i = 0; i < nrepeats; i++) {
Packit 7a8e5e
        memcpy((char *)(rptr + 1), (dptr + 2), (npts - 1)*sizeof(double));
Packit 7a8e5e
        rptr[0] = dptr[0];
Packit 7a8e5e
        npy_rfftb(npts, rptr, wsave);
Packit 7a8e5e
        rptr += npts;
Packit 7a8e5e
        dptr += npts*2;
Packit 7a8e5e
    }
Packit 7a8e5e
    NPY_SIGINT_OFF;
Packit 7a8e5e
    Py_END_ALLOW_THREADS;
Packit 7a8e5e
    PyArray_Free(op2, (char *)wsave);
Packit 7a8e5e
    Py_DECREF(data);
Packit 7a8e5e
    return (PyObject *)ret;
Packit 7a8e5e
Packit 7a8e5e
fail:
Packit 7a8e5e
    PyArray_Free(op2, (char *)wsave);
Packit 7a8e5e
    Py_XDECREF(data);
Packit 7a8e5e
    Py_XDECREF(ret);
Packit 7a8e5e
    return NULL;
Packit 7a8e5e
}
Packit 7a8e5e
Packit 7a8e5e
static const char fftpack_rffti__doc__[] = "";
Packit 7a8e5e
Packit 7a8e5e
static PyObject *
Packit 7a8e5e
fftpack_rffti(PyObject *NPY_UNUSED(self), PyObject *args)
Packit 7a8e5e
{
Packit 7a8e5e
  PyArrayObject *op;
Packit 7a8e5e
  npy_intp dim;
Packit 7a8e5e
  long n;
Packit 7a8e5e
Packit 7a8e5e
  if (!PyArg_ParseTuple(args, "l:rffti", &n)) {
Packit 7a8e5e
      return NULL;
Packit 7a8e5e
  }
Packit 7a8e5e
  /*Magic size needed by npy_rffti*/
Packit 7a8e5e
  dim = 2*n + 15;
Packit 7a8e5e
  /*Create a 1 dimensional array of dimensions of type double*/
Packit 7a8e5e
  op = (PyArrayObject *)PyArray_SimpleNew(1, &dim, NPY_DOUBLE);
Packit 7a8e5e
  if (op == NULL) {
Packit 7a8e5e
      return NULL;
Packit 7a8e5e
  }
Packit 7a8e5e
  Py_BEGIN_ALLOW_THREADS;
Packit 7a8e5e
  NPY_SIGINT_ON;
Packit 7a8e5e
  npy_rffti(n, (double *)PyArray_DATA((PyArrayObject*)op));
Packit 7a8e5e
  NPY_SIGINT_OFF;
Packit 7a8e5e
  Py_END_ALLOW_THREADS;
Packit 7a8e5e
Packit 7a8e5e
  return (PyObject *)op;
Packit 7a8e5e
}
Packit 7a8e5e
Packit 7a8e5e
Packit 7a8e5e
/* List of methods defined in the module */
Packit 7a8e5e
Packit 7a8e5e
static struct PyMethodDef fftpack_methods[] = {
Packit 7a8e5e
    {"cfftf",   fftpack_cfftf,  1,      fftpack_cfftf__doc__},
Packit 7a8e5e
    {"cfftb",   fftpack_cfftb,  1,      fftpack_cfftb__doc__},
Packit 7a8e5e
    {"cffti",   fftpack_cffti,  1,      fftpack_cffti__doc__},
Packit 7a8e5e
    {"rfftf",   fftpack_rfftf,  1,      fftpack_rfftf__doc__},
Packit 7a8e5e
    {"rfftb",   fftpack_rfftb,  1,      fftpack_rfftb__doc__},
Packit 7a8e5e
    {"rffti",   fftpack_rffti,  1,      fftpack_rffti__doc__},
Packit 7a8e5e
    {NULL, NULL, 0, NULL}          /* sentinel */
Packit 7a8e5e
};
Packit 7a8e5e
Packit 7a8e5e
#if PY_MAJOR_VERSION >= 3
Packit 7a8e5e
static struct PyModuleDef moduledef = {
Packit 7a8e5e
        PyModuleDef_HEAD_INIT,
Packit 7a8e5e
        "fftpack_lite",
Packit 7a8e5e
        NULL,
Packit 7a8e5e
        -1,
Packit 7a8e5e
        fftpack_methods,
Packit 7a8e5e
        NULL,
Packit 7a8e5e
        NULL,
Packit 7a8e5e
        NULL,
Packit 7a8e5e
        NULL
Packit 7a8e5e
};
Packit 7a8e5e
#endif
Packit 7a8e5e
Packit 7a8e5e
/* Initialization function for the module */
Packit 7a8e5e
#if PY_MAJOR_VERSION >= 3
Packit 7a8e5e
#define RETVAL m
Packit 7a8e5e
PyMODINIT_FUNC PyInit_fftpack_lite(void)
Packit 7a8e5e
#else
Packit 7a8e5e
#define RETVAL
Packit 7a8e5e
PyMODINIT_FUNC
Packit 7a8e5e
initfftpack_lite(void)
Packit 7a8e5e
#endif
Packit 7a8e5e
{
Packit 7a8e5e
    PyObject *m,*d;
Packit 7a8e5e
#if PY_MAJOR_VERSION >= 3
Packit 7a8e5e
    m = PyModule_Create(&moduledef);
Packit 7a8e5e
#else
Packit 7a8e5e
    static const char fftpack_module_documentation[] = "";
Packit 7a8e5e
Packit 7a8e5e
    m = Py_InitModule4("fftpack_lite", fftpack_methods,
Packit 7a8e5e
            fftpack_module_documentation,
Packit 7a8e5e
            (PyObject*)NULL,PYTHON_API_VERSION);
Packit 7a8e5e
#endif
Packit 7a8e5e
Packit 7a8e5e
    /* Import the array object */
Packit 7a8e5e
    import_array();
Packit 7a8e5e
Packit 7a8e5e
    /* Add some symbolic constants to the module */
Packit 7a8e5e
    d = PyModule_GetDict(m);
Packit 7a8e5e
    ErrorObject = PyErr_NewException("fftpack.error", NULL, NULL);
Packit 7a8e5e
    PyDict_SetItemString(d, "error", ErrorObject);
Packit 7a8e5e
Packit 7a8e5e
    /* XXXX Add constants here */
Packit 7a8e5e
Packit 7a8e5e
    return RETVAL;
Packit 7a8e5e
}