我有一个需要 std::vector 并输出 std::vector 的 C++ 文件,如何在 cython 中转换它?
C++ 文件:cpp_func.cpp
std::vector<std::vector<float>> my_cpp_function ( const size_t& p_num
, const std::vector<float>& x
, const std::vector<std::vector<float>>& table)
{
// Do some heavy calculation & and return a 2D vector called H
return H;
}
Cython 文件:cython_v1.pyx
import numpy as np
cimport numpy as np
import cython
from libcpp.vector cimport vector
cdef extern from "cpp_func.h":
vector[ vector[float] ] my_cpp_function ( size_t &p_num
, vector[float] &x
, vector[ vector[float] ] &table)
@cython.boundscheck(False)
@cython.wraparound (False)
cpdef float [:,::1] my_cython_func ( size_t p_num, float [:] x, float [:,:] table ):
# I have no clue here but basically what is wanted is to use
# the above imported function to do stuff and output as memory slice
H = my_cpp_function (p_num, x, table )
return H
在我的Python文件中
import numpy as np
from cython_v1 import my_cython_func
# somewhere here, build the x and table and p_num
H = np.asarray( my_cython_func(p_num, x, table) )
# H can be used in subsequent cheaper calculation all in python
问题
请帮忙。谢谢你。