Python 在c扩展中创建numpy数组

Python 在c扩展中创建numpy数组,python,c,numpy,python-extensions,Python,C,Numpy,Python Extensions,我只是想在开始编写扩展之前先创建一个numpy数组。下面是一个超级简单的程序: #include <stdio.h> #include <iostream> #include "Python.h" #include "numpy/npy_common.h" #include "numpy/ndarrayobject.h" #include "numpy/arrayobject.h" int main(int argc, char * argv[]) { int

我只是想在开始编写扩展之前先创建一个numpy数组。下面是一个超级简单的程序:

#include <stdio.h>
#include <iostream>
#include "Python.h"
#include "numpy/npy_common.h"
#include "numpy/ndarrayobject.h"
#include "numpy/arrayobject.h"

int main(int argc, char * argv[])
{
    int n = 2;
    int nd = 1;
    npy_intp size = {1};
    PyObject* alpha = PyArray_SimpleNew(nd, &size, NPY_DOUBLE);
    return 0;
}
#包括
#包括
#包括“Python.h”
#包括“numpy/npy_common.h”
#包括“numpy/ndarrayobject.h”
#包括“numpy/arrayobject.h”
int main(int argc,char*argv[])
{
int n=2;
int-nd=1;
npy_intp size={1};
PyObject*alpha=PyArray\u SimpleNew(nd,&size,NPY\u DOUBLE);
返回0;
}

这个程序在调用
PyArray\u SimpleNew
时出错,我不明白为什么。我试着回答前面的一些问题(例如和)。我做错了什么?

例如,
PyArray\u SimpleNew
的典型用法是

int nd = 2;
npy_intp dims[] = {3,2};
PyObject *alpha = PyArray_SimpleNew(nd, dims, NPY_DOUBLE);
请注意,
nd
的值不得超过数组
dims[]
的元素数

另外:扩展导入数组()以设置C API的函数指针表。例如,在Cython中:

import numpy as np
cimport numpy as np

np.import_array()  # so numpy's C API won't segfault

cdef make_array():
  cdef np.npy_intp element_count = 100
  return np.PyArray_SimpleNew(1, &element_count, np.NPY_DOUBLE)

对…我正试图创建一个一维数组。不管怎么说,用你的代码,我还是有一个错误。是的,很抱歉。segfault似乎与对
import\u array()
的所需调用有关。我从未尝试过从
main
调用
PyArray\u SimpleNew
,因此我不确定如何修复此问题。