这些矩阵是使用Eigen库实现的,因此我正在使用Eigency接口将这些数据结构的底层内容传输到NumPy数组中。
如果在Python中返回对其中一个矩阵的视图,则一切正常。但是,我想返回一个包含这两个矩阵的元组,这需要临时存储此struct,然后返回其每个矩阵成员。
## kernel.pyx
from eigency.core cimport *
cdef extern from "kernel_cpp.h":
cdef cppclass network:
MatrixXf state
MatrixXf patterns
cdef network _run "run" (int N, int K, int P, double g, double T, double tau, double dt)
def run(N=10000, K=100, P=30, g=1.5, T=0.5, tau=1e-3, dt=1e-3):
return ndarray_view(_run(N,K,P,g,T,tau,dt).state) # <--- This works
# This does not:
# cdef network net = _run(N,K,P,g,T,tau,dt):
# return (ndarray_view(net.state), ndarray_view(net.patterns))
当我使用
python setup.py build_ext --inplace
编译有注释的代码时,出现的错误是:kernel.cpp:1552:11: error: no matching constructor for initialization of 'network'
network __pyx_v_net;
^`
我觉得这很有道理——Cython试图调用network的构造函数。但是我希望构造在“run”内部进行。这里应该使用什么类型声明?
如果有帮助的话,代码的简化版本在这里。
谢谢!
cdef network* net = new network(_run(N,K,P,g,T,tau,dt)
会产生这个错误:调用参数数量错误(期望0个,得到1个)
。Cython编译器也不允许我在try块中使用cdef。 - Max Gillettnetwork
复制构造函数(即使它只是默认的C ++构造函数)以执行new network(_run(...))
。其次,您需要将cdef
移出try块。为了确保正确性,我已更新答案。不过,我可能仍会选择第一个选项。 - DavidW