Меня интересует использование pybind11 для оптимизации некоторых вычислений Python с использованием C++. документация по кастингу не имеет для меня особого смысла, и мне было интересно, знает ли кто-нибудь, как преобразовать типы данных 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 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)
Я также использую Инструменты сборки для Visual Studio 2017 для vcruntime140.dll, расположенной в "C:\ProgramFiles(x86)\MicrosoftVisualStudio\2017\BuildTools\VC\Redist\MSVC\14.16.27012\onecore\x64\Microsoft.VC141.CRT" (который я скопировал и вставил в "C:\MinGW\lib"). Я также изменил всю строку "gcc" в "C:\Anaconda3\Lib\distutils\cygwinccompiler.py" на "g++" (я не менял имена переменных с помощью gcc в них).
Ключевые слова:
"pybind11" ; "Способствовать росту" ; «С++ и Python»; "boost::multiprecision::cpp_int"
struct inty
наcpp_int
?. Вы можете преобразовать cpp_int в строку base16 и передать ее PyLong_FromString для преобразования из C++--›Python и Python--›C++ путем приведения PyNumber к строке base16 с помощью PyNumber_ToBase и создайте cpp_int на основе этого строкового объекта Python. - person tnt   schedule 18.02.2019oss << std::hex << bigint
, а затем получить егоoss.str()
- person tnt   schedule 18.02.2019