Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将numpy ndarray传递给c++;使用ctypes的函数?_Python_C++_Numpy - Fatal编程技术网

Python 如何将numpy ndarray传递给c++;使用ctypes的函数?

Python 如何将numpy ndarray传递给c++;使用ctypes的函数?,python,c++,numpy,Python,C++,Numpy,我花了两天时间研究如何将Ndarray传递到c/c++,但仍然没有找到任何正确的方法。 每一件事都被记录得非常混乱。 我在中找到的唯一教程。 但是当我尝试运行python代码“myextension.py”时,它会引发ImportError:没有名为numpyndarrays的模块 我不想使用cython、boost或python C-API。 下面是我编译的C++代码“n.CPP”: 此代码正确加载共享库,主要问题是如何将numpy ndarray转换为作为共享库输入的结构。 我还需要知道,如

我花了两天时间研究如何将Ndarray传递到c/c++,但仍然没有找到任何正确的方法。
每一件事都被记录得非常混乱。
我在中找到的唯一教程。
但是当我尝试运行python代码“myextension.py”时,它会引发ImportError:没有名为numpyndarrays的模块

我不想使用cython、boost或python C-API。

下面是我编译的C++代码“n.CPP”: 此代码正确加载共享库,主要问题是如何将numpy ndarray转换为作为共享库输入的结构。

我还需要知道,如何将共享库的输出转换成NUMPY NDAREX。

< P>我发现使用NUMPY C-API是将NUMPY数组传递给C++最简单的方法。
文档可以在上找到。

您正在查看的教程已经过时,ndarray.h甚至不存在了。请在此处尝试numpy文档中的最新教程:
#include <iostream>
#include "ndarray.h"

extern "C" {

Ndarray<double, 3> myfunc(numpyArray<double> array1)
{
    Ndarray<double,3> a(array1);
    std::cout<<a.getShape(0); //this prints 0 and it means somthing is wrong

    for (int i = 0; i < a.getShape(0); i++)
    {
        for (int j = 0; j < a.getShape(1); j++)
        {
            for (int k = 0; k < a.getShape(2); k++)
            {
                a[i][j][k] = 2.0 * a[i][j][k];
                std::cout<<a[i][j][k]<<' '<<i<<' '<<j<<' '<<k<<'\n';// prints nothing because a.getShape(0) is 0.
            }
        }
    }
    return a;
}
}
import numpy as np
import numpy.ctypeslib as ct

mylib = ct.load_library('n', '.')       

def myfunc(array1):
    return mylib.myfunc(array1.ctypes) # I also tried ct.as_ctypes(array1) instead of array1.ctypes with no success

a = np.ones((2,2,2)).astype(np.double)

myfunc(a)