简体   繁体   English

复杂转换为Python Complex

[英]Complex conversion to Python Complex

I'm currently writing a wrapper for a C++ project that use std::complex<double> , available in cython as libcpp.complex.complex[double] . 我正在编写一个使用std::complex<double>的C ++项目的包装器,在cython中可用作libcpp.complex.complex[double]

However there is no implicit conversion between this and Python complex, I'm trying to find the best way to do this conversion. 但是这个和Python复合体之间没有隐式转换,我试图找到进行这种转换的最佳方法。

The obvious is to use 显而易见的是使用

cdef libcpp.complex.complex[double] x = ...
X = complex(x.real(),x.imag()

And

cdef complex Y = ...
cdef libcpp.complex.complex[double] y = libcpp.complex.complex[double](Y.real, Y.imag)

And

cdef libcpp.complex.complex[double] z
cdef complex Z = ...
z.real(Z.real)
z.imag(Z.imag)

But is there a better way, preferably to make cython do the conversion automatically. 但是有更好的方法,最好让cython自动进行转换。

For one this occurs in some cpdef functions, and I would like to avoid using Python complex in calls from cython for improved speed. 对于一个这种情况发生在一些cpdef函数中,我想避免在cython的调用中使用Python复合体来提高速度。

However as far as I can tell cython can't do this conversion implicitly, and thus I can't avoid using the Python complex for code that can be called from Python or C++. 但是据我所知,cython无法隐式地进行这种转换,因此我无法避免将Python复合体用于可以从Python或C ++调用的代码。

This functionality will be released in the next version of Cython. 此功能将在下一版本的Cython中发布。

https://github.com/cython/cython/commit/2c3c04b25620c437ce41d7851e7581eb1f0c313c https://github.com/cython/cython/commit/2c3c04b25620c437ce41d7851e7581eb1f0c313c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM