从Inside Python函数调用C ++函数

时间:2022-09-06 21:36:33

In R we can use Rcpp to call a cpp function as the one below:

在R中,我们可以使用Rcpp来调用cpp函数,如下所示:

#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
SEXP critcpp(SEXP a, SEXP b){
     NumericMatrix X(a);
     NumericVector crit(b);
     int p = XtX.ncol();
          NumericMatrix critstep(p,p);
     NumericMatrix deltamin(p,p);
     List lst(2);
         for (int i = 0; i < (p-1); i++){
            for (int j = i+1; j < p; j++){
               --some calculations
             }
          }
          lst[0] = critstep;
          lst[1] = deltamin;
          return lst;
}

I want to do the same thing in python. I have gone through Boost,SWIG etc but it seems complicated to my newbie Python eyes. Can the python wizards here kindly point me in the right direction. I need to call this C++ function from inside a Python function.

我想在python中做同样的事情。我经历过Boost,SWIG等,但我的新手Python眼睛似乎很复杂。这里的蟒蛇巫师能否指出我正确的方向。我需要在Python函数中调用这个C ++函数。

1 个解决方案

#1


0  

Since I think the only real answer is by spending some time in rewriting the function you posted, or by writing a some sort of wrapper for the function (absolutely possible but quite time consuming) I'm answering with a completely different approach...

因为我认为唯一真正的答案是花一些时间来重写你发布的功能,或者为函数编写一些包装器(绝对可能但非常耗时)我正在回答一种完全不同的方法......

Without passing by any sort of compiled conversion, a really faster way (from a programming time point of view, not in efficiency) may be directly calling the R interpreter with the module of the function you posted from within python, through the python rpy2 module, as described here. It requires the panda module, to handle the data frames from R.

没有经过任何类型的编译转换,一个非常快的方法(从编程时间的角度来看,而不是效率)可能是通过python rpy2模块直接使用你在python中发布的函数模块调用R解释器,如这里所述。它需要熊猫模块来处理来自R的数据帧。

The module to use (in python) are:

要使用的模块(在python中)是:

import numpy as np  # for handling numerical arrays
import scipy as sp  # a good utility
import pandas as pd  # for data frames
from rpy2.robjects.packages import importr  # for importing your module
import rpy2.robjects as ro  # for calling R interpreter from within python
import pandas.rpy.common as com  # for storing R data frames in pandas data frames.

In your code you should import your module by calling importr

在您的代码中,您应该通过调用importr导入您的模块

importr('your-module-with-your-cpp-function')

and you can send directly commands to R by issuing:

您可以通过发出以下命令直接向R发送命令:

ro.r('x = your.function( blah blah )')
x_rpy = ro.r('x')
type(x_rpy)
# => rpy2.robjects.your-object-type

you can store your data in a data frame by:

您可以通过以下方式将数据存储在数据框中:

py_df = com.load_data('variable.name')

and push back a data frame through:

并通过以下方式推回数据框:

r_df = com.convert_t_r_dataframe(py_df)
ro.globalenv['df'] = r_df

This is for sure a workaround for your question, but it may be considered as a reasonable solution for certain applications, even if I do not suggest it for "production".

这肯定是您的问题的解决方法,但它可能被视为某些应用程序的合理解决方案,即使我不建议它用于“生产”。

#1


0  

Since I think the only real answer is by spending some time in rewriting the function you posted, or by writing a some sort of wrapper for the function (absolutely possible but quite time consuming) I'm answering with a completely different approach...

因为我认为唯一真正的答案是花一些时间来重写你发布的功能,或者为函数编写一些包装器(绝对可能但非常耗时)我正在回答一种完全不同的方法......

Without passing by any sort of compiled conversion, a really faster way (from a programming time point of view, not in efficiency) may be directly calling the R interpreter with the module of the function you posted from within python, through the python rpy2 module, as described here. It requires the panda module, to handle the data frames from R.

没有经过任何类型的编译转换,一个非常快的方法(从编程时间的角度来看,而不是效率)可能是通过python rpy2模块直接使用你在python中发布的函数模块调用R解释器,如这里所述。它需要熊猫模块来处理来自R的数据帧。

The module to use (in python) are:

要使用的模块(在python中)是:

import numpy as np  # for handling numerical arrays
import scipy as sp  # a good utility
import pandas as pd  # for data frames
from rpy2.robjects.packages import importr  # for importing your module
import rpy2.robjects as ro  # for calling R interpreter from within python
import pandas.rpy.common as com  # for storing R data frames in pandas data frames.

In your code you should import your module by calling importr

在您的代码中,您应该通过调用importr导入您的模块

importr('your-module-with-your-cpp-function')

and you can send directly commands to R by issuing:

您可以通过发出以下命令直接向R发送命令:

ro.r('x = your.function( blah blah )')
x_rpy = ro.r('x')
type(x_rpy)
# => rpy2.robjects.your-object-type

you can store your data in a data frame by:

您可以通过以下方式将数据存储在数据框中:

py_df = com.load_data('variable.name')

and push back a data frame through:

并通过以下方式推回数据框:

r_df = com.convert_t_r_dataframe(py_df)
ro.globalenv['df'] = r_df

This is for sure a workaround for your question, but it may be considered as a reasonable solution for certain applications, even if I do not suggest it for "production".

这肯定是您的问题的解决方法,但它可能被视为某些应用程序的合理解决方案,即使我不建议它用于“生产”。