- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有兴趣使用 pybind11使用 C++ 优化一些 Python 计算。 casting documentation对我来说没有多大意义,想知道是否有人知道如何将 boost 数据类型(特别是 cpp_int)转换为 Python 数据类型,以便我可以返回计算。我正在尝试做的一个简单示例是阶乘:
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <boost/multiprecision/cpp_int.hpp>
using boost::multiprecision::cpp_int;
namespace py = pybind11;
py::int_ fact(int i) {
cpp_int prod = 1;
while(i-- >= 1){
prod *= (i+1);
}
return py::cast(prod);
}
PYBIND11_MODULE(fact, m) {
m.def("fact", &fact,R"pbdoc(
Returns the factorial of a number.
)pbdoc");
}
上面的编译但是当我去使用它时我得到
TypeError: Unregistered type : boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<0u, 0u, (boost::multiprecision::cpp_integer_type)1, (boost::multiprecision::cpp_int_check_type)0, std::allocator<unsigned long long> >, (boost::multiprecision::expression_template_option)1>
所以我认为 py::cast 无法正常工作...
我的笔记本电脑运行 Windows 10,我使用的是 Anaconda Python 3.7
C:\Users\15734>python
Python 3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
和Stephan T. Lavavej's MinGW C++ 8.2.0
C:\Users\15734>g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/x86_64-w64-mingw32/8.2.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../src/configure --enable-languages=c,c++ --build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --disable-multilib --prefix=/c/temp/gcc/dest --with-sysroot=/c/temp/gcc/dest --disable-libstdcxx-pch --disable-libstdcxx-verbose --disable-nls --disable-shared --disable-win32-registry --with-tune=haswell --enable-threads=posix --enable-libgomp
Thread model: posix
gcc version 8.2.0 (GCC)
我也在用 Build Tools for Visual Studio 2017对于位于“C:\ProgramFiles(x86)\MicrosoftVisualStudio\2017\BuildTools\VC\Redist\MSVC\14.16.27012\onecore\x64\Microsoft.VC141.CRT”的 vcruntime140.dll(我将其复制并粘贴到“C:\MinGW\lib”)。我还将“C:\Anaconda3\Lib\distutils\cygwinccompiler.py”中的所有字符串“gcc”更改为“g++”(我没有更改变量名称gcc 在其中)。
关键词:
“pybind11”; “boost ” ; “C++ 和 Python”; “boost::multiprecision::cpp_int”
最佳答案
我得到了这个工作:
#include <boost/multiprecision/cpp_int.hpp>
#include <iomanip>
#include <pybind11/pybind11.h>
#include <sstream>
using cpp_int = boost::multiprecision::cpp_int;
namespace py = pybind11;
namespace pybind11
{
namespace detail
{
template <>
struct type_caster<cpp_int> {
/**
* This macro establishes the name 'cpp_int' in
* function signatures and declares a local variable
* 'value' of type cpp_int
*/
PYBIND11_TYPE_CASTER(cpp_int, _("cpp_int"));
/**
* Conversion part 1 (Python->C++): convert a PyObject into a cpp_int
* instance or return false upon failure. The second argument
* indicates whether implicit conversions should be applied.
*/
bool load(handle src, bool)
{
// Convert into base 16 string (PyNumber_ToBase prepend '0x')
PyObject* tmp = PyNumber_ToBase(src.ptr(), 16);
if (!tmp) return false;
std::string s = py::cast<std::string>(tmp);
value = cpp_int{s}; // explicit cast from string to cpp_int,
// don't need a base here because
// `PyNumber_ToBase` already prepended '0x'
Py_DECREF(tmp);
/* Ensure return code was OK (to avoid out-of-range errors etc) */
return !PyErr_Occurred();
}
/**
* Conversion part 2 (C++ -> Python): convert an cpp_int instance into
* a Python object. The second and third arguments are used to
* indicate the return value policy and parent object (for
* ``return_value_policy::reference_internal``) and are generally
* ignored by implicit casters.
*/
static handle cast(const cpp_int& src, return_value_policy, handle)
{
// Convert cpp_int to base 16 string
std::ostringstream oss;
oss << std::hex << src;
return PyLong_FromString(oss.str().c_str(), nullptr, 16);
}
};
} // namespace detail
} // namespace pybind11
py::int_ fact(int i)
{
cpp_int prod = 1;
while (i-- > 1) prod *= i + 1;
return py::cast(prod);
}
PYBIND11_MODULE(python_example, m)
{
m.def("fact", &fact, R"pbdoc(
Returns the factorial of a number.
)pbdoc");
}
可能不是最快的方法,因为它先将 cpp_int
转换为临时的 16 进制字符串,然后再将此字符串转换为 Python 整数。
关于python - PyBind11:boost::multiprecision::cpp_int 到 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54738011/
在 rng 中使用固定种子时,如果精度变化,结果将无法重现。即,如果更改模板参数 cpp_dec_float并运行以下代码,会看到不同的输出(对于精度的每次变化)。 #include #includ
我一直在关注 documentation from the boost library生成多精度随机整数,但在文档中没有提到如何设置种子。 我不知道如何在不出现编译错误的情况下设置种子。 #inclu
我需要将IPv6字符串地址转换为boost::multiprecision::uint128_t 对于IPv4,我使用以下算法: uint32_t byte1 = 0, byte2 = 0, byte
自从调整了一些代码以启用多精度后,我的一些单元测试开始失败。头文件: #ifndef SCRATCH_UNITTESTBOOST_INCLUDED #define SCRATCH_UNITTESTBO
我正在尝试使用 boost::multiprecision 库进行浮点(或者在那种情况下,固定)点运算。但是,我无法通过以下方式检测潜在的溢出: typedef boost::multiprecisi
如果我有: 字符缓冲区[16]; 如何将其原始字节转换为: boost::multiprecision::uint128_t ? 我尝试进行标准的 C 风格转换: uint128_t myInt =
我正在使用 boost::multiprecision 来获得固定但任意精度的整数。我打算使用 number> .第一个明显的问题是: 此数据类型是否具有给定精度的任何无符号整数的标准位模式?我听说有
我使用 boost::multiprecision::uint128_t 类型来对 128 位值执行按位运算。但是,我无法将 128 位值写入二进制文件。特别是需要用零填充值。 例如,如果 uint1
http://www.boost.org/doc/libs/1_53_0/libs/multiprecision/doc/html/index.html 我刚刚开始探索这个图书馆。似乎没有办法将 cp
我读过 boost::multiprecision documentation : Depending upon the number type, precision may be arbitrari
如何将字符串转换为“boost::multiprecision::cpp_int”? 此外,我有一个 .txt 文件,其中包含 100 个数字,每个数字 50 个数字,我使用 ifstream 将它们
我需要以任意精度获取一个值的散列值(来自 Boost.Multiprecision);我用 cpp_int后端。我想出了以下代码: boost::multiprecision::cpp_int x0
考虑以下使用 boost 创建多精度 float “a”的代码。 如何使用boost库调用三角函数?比如我希望计算sin(a)。 #include #include "boost/multiprec
我正在尝试转换 boost::multiprecision::cpp_dec_float_x到 boost::multiprecision::uintx_t .所以基本上是 boost bigreal
我尝试创建一个派生自 boost::multiprecision::mpz_int 的类并让它继承基类构造函数: #include using namespace boost::multipreci
我需要从 boost::multiprecision::int128_t 转换至 double . 对于较小的整数,我使用: template flt_t as_flt() const { ret
我正在尝试使用Ramanujan's公式为浮点后的任意位数计算我的大学项目之一的pi。对于这项工作,我使用的boost::multiprecision库只是我已经安装在计算机上的mpfr和mpir的包
关于 this answer ,有一个使用 boost::multiprecision 和 boost::random 的最小示例。 当我使用种子时,我正在努力解决这个例子: #include #i
我有一些模板代码,编译器可以尾调用优化大多数数据类型,但不能优化其他数据类型。代码实现pow() template void powRecurse(T& x, U& y, T& acc) { i
我需要两个函数: std::vector bigint_to_bytes(cpp_int a); cpp_int bytes_to_bigint(std::vector const& a); goog
我是一名优秀的程序员,十分优秀!