我有过使用boostmultiprecision和Python的mpmath的经验。你知道吗
当它开始进行二者通信(例如,在C++中创建Python扩展)时,我的尝试总是涉及到某种浪费的浮点到字符串和字符串到浮点转换。你知道吗
我的问题是:有没有可能使双方的沟通更有效(和优雅)的方式?我的意思是,有没有一种方法直接将C++的Boost多精度负载从同一个URL中导出到Python ^ {< CD1> }对象{{a1}?你知道吗
我已经找了很久了。only other similar question I found只是使用pybind11
将Boost Multiprecision导出到Python(通常),而不是直接导出到mpmath
对象。在这个问题上,OP使用了我试图避免的相同方法(即,从/C++到Python通信时,从字符串转换到字符串)。你知道吗
这只部分回答了你的问题。因为直接的答案是:不,如果没有对字符串的浪费性转换,就不可能以干净的方式进行转换,因为
mpmath
是一个纯粹的python库,没有任何部分是用C
或C++
编写的,因此即使您试图通过寻求某种二进制兼容性来跳过“浪费性转换”,您的代码也将非常脆弱:它是每当一些python
或mpmath
内部结构发生微小变化时,都会中断。你知道吗然而,我需要完全相同的东西。所以我决定通过
boost::python
注册一个自动转换,它使用字符串进行检查和转换。实际上,在python中,您还可以从字符串创建mpmath.mpf
对象,因此基本相同,只是在下面的代码中,它的速度更快,因为它是在C++
中编写的。你知道吗以下是对我有效的方法:
现在使用以下命令编译此代码:
下面是一个
python
会话示例:C++
代码并不关心mpmath是否已经在python中导入。如果是,则获取现有的库句柄,如果不是,则导入它。 如果你发现这个片段有任何改进的地方,请告诉我!你知道吗以下是我写这篇文章时的一些有用的参考资料:
boost::python
)相关问题 更多 >
编程相关推荐