int main () { int N = 4; int M = 2; double ** a = new_NArr(sizeof(double*), N); double ** b = new_NArr(sizeof(double*), M); int i; double k = 1.; for (i=0; i < M; i++) { a[i] = new_NArr(sizeof(double),3); b[i] = new_NArr(sizeof(double),3); for (int j=0; j < 3; j++) a[i][j] = k++; for (int j=0; j < 3; j++) b[i][j] = k++; } for (; i < N; i++) { a[i] = new_NArr(sizeof(double),3); for (int j=0; j < 3; j++) a[i][j] = k++; } printf("A:\n"); print_narr(a); printf("\n\nB:\n"); print_narr(b); printf("\n\n"); dtw_distance(a,b); return 0; }
static PyObject* dtw_extension(PyObject *dummy, PyObject *args) { PyObject *arg1 = NULL; PyObject *arr1 = NULL; PyObject *arg2 = NULL; PyObject *arr2 = NULL; if (!PyArg_ParseTuple(args, "OO", &arg1, &arg2)) { return NULL; } arr1 = PyArray_FROM_OTF(arg1, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY); if (arr1 == NULL) { return NULL; } arr2 = PyArray_FROM_OTF(arg2, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY); if (arr2 == NULL) { return NULL; } // Number of dimensions int nd1 = PyArray_NDIM((PyArrayObject*)arr1); int arr_type1 = PyArray_TYPE((PyArrayObject*)arr1); npy_intp *dims1 = PyArray_DIMS((PyArrayObject*)arr1); int nd2 = PyArray_NDIM((PyArrayObject*)arr2); int arr_type2 = PyArray_TYPE((PyArrayObject*)arr2); npy_intp *dims2 = PyArray_DIMS((PyArrayObject*)arr2); int r = 0; npy_double **input1 = NULL; r = PyArray_AsCArray((PyObject**)&arr1, (void**)&input1, dims1, nd1, PyArray_DescrFromType(arr_type1)); if (r < 0) { PyErr_SetString(PyExc_RuntimeError, "Could not convert input to C array"); return NULL; } npy_double **input2 = NULL; r = PyArray_AsCArray((PyObject**)&arr2, (void**)&input2, dims2, nd2, PyArray_DescrFromType(arr_type2)); if (r < 0) { PyErr_SetString(PyExc_RuntimeError, "Could not convert input to C array"); return NULL; } // DTW matrix_t mat_a = { input1, dims1[0], dims1[1] }; matrix_t mat_b = { input2, dims2[0], dims2[1] }; double dist = dtw_distance(&mat_a, &mat_b); PyObject *value = PyFloat_FromDouble(dist); if (value == NULL) { PyErr_SetString(PyExc_RuntimeError, "Could not convert double to object"); return NULL; } Py_DECREF(arr1); Py_DECREF(arr2); PyArray_Free(arr1, (void*)input1); PyArray_Free(arr2, (void*)input2); return value; }