如何将 scipy.weave.inline 与外部 C 库一起使用?

发布于 2024-10-04 05:22:23 字数 724 浏览 0 评论 0原文

我试图理解 weave.inline 将 C 代码包装在我的 Python 程序中。下面的代码简单地获取 Numpy 数组并将其所有元素乘以 2。

inl.py

import numpy
import scipy.weave

a = numpy.array([1.0, 2.0, 3.0])
N = a.shape[0]

print a
code = \
  """
  int i;
  for(i = 0; i < N; i++)
  {
    a[i] = a[i] * 2;
  }
  """

scipy.weave.inline(code, ['a','N'])
print a

然后我想将一些函数从内联代码传递到外部库。让它简单地乘以 2。所以我创建了两个文件:

mult.c

#include "mult.h"

float mult(float n)
{
  return n * 2;
}

mult.h

float inc(float n);

现在我想在我的内联代码中使用函数 mult。但我不知道如何将我的 C 文件与 Python 内联代码链接。我尝试将 C 文件编译为共享库,并将它们作为头文件和库在 weave 中传递,但这是徒劳的。有什么建议吗?

I am trying to understand weave.inline to wrap C code in my Python programs. The code below simply takes the Numpy array and multiplicates all of its elements by 2.

inl.py

import numpy
import scipy.weave

a = numpy.array([1.0, 2.0, 3.0])
N = a.shape[0]

print a
code = \
  """
  int i;
  for(i = 0; i < N; i++)
  {
    a[i] = a[i] * 2;
  }
  """

scipy.weave.inline(code, ['a','N'])
print a

Then I want to carry some functions from inline code to external libraries. Let it be the trivial multiplication by 2. So I create two files:

mult.c

#include "mult.h"

float mult(float n)
{
  return n * 2;
}

mult.h

float inc(float n);

Now I want to use function mult in my inline code. But I don't know how do I link my C files with Python inline code. I tried to compile C files as shared library and pass them as headers and libraries in weave, but that was in vain. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧夏天 2024-10-11 05:22:23

我已经成功地做到了这一点,通过 weave.inline() 代码(在 Ubuntu Linux 下)从 R 调用数学函数。

首先,将 C 函数编译为共享库。就我而言,我从 CRAN 获取了 R 的最新版本,并且

./configure --enable-R-static-lib --enable-static --with-readline=no
cd src/nmath/standalone/
make

您现在应该有一个名为 libRmath.so 的文件。如果 libpath 是一个字符串,其中包含包含 libRmath.so 的目录,您可以执行类似

code = 'return_val = pbinom(100, 20000, 100./20000., 0, 1);'
support_code = 'extern "C" double pbinom(double x, double n, double p, int lower_tail, int log_p);'
weave.inline(code, support_code=support_code,
    library_dirs=[libpath], libraries=["Rmath"], runtime_library_dirs=[libpath])

“注意几件事”的操作。标头声明必须放在 support_code 中,而不是 code (我不知道为什么),并且它们必须以 extern "C" 为前缀code> 因为它们是 C 代码,而不是 C++(这是标准的)。应该可以包含头文件而不是使用 support_code (检查 weave.inline 的文档),但我还没有尝试过。按照通常的 Unix 约定,库名称为 Rmath,但共享库文件为 libRmath.so。并且库的路径指定了两次,一次用于链接,一次用于执行。

希望这有帮助!

I have successfully done this, calling math functions from R via weave.inline() code (under Ubuntu Linux).

First, compile your C functions as a shared library. In my case, I grabbed a recent release of R from CRAN, and did

./configure --enable-R-static-lib --enable-static --with-readline=no
cd src/nmath/standalone/
make

You should now have a file called libRmath.so. If libpath is a string with the directory that holds libRmath.so, you can do something like

code = 'return_val = pbinom(100, 20000, 100./20000., 0, 1);'
support_code = 'extern "C" double pbinom(double x, double n, double p, int lower_tail, int log_p);'
weave.inline(code, support_code=support_code,
    library_dirs=[libpath], libraries=["Rmath"], runtime_library_dirs=[libpath])

Note a couple things. The header declarations have to go in support_code, not code (I don't know why), and they have to be prefixed with extern "C" because they're C code, not C++ (this is standard). It should be possible to include headers files instead of using support_code (check the docs for weave.inline), but I haven't tried it. The library name is Rmath, but the shared library file is libRmath.so, in the usual Unix convention. And the path to the library is specified twice, once for linking, and once for execution.

Hope this helps!

鸠魁 2024-10-11 05:22:23

将 mult.c 和 mult.h 的源代码放入名为 extra_code 的字符串对象中,然后在 .weave 调用中添加以下行,

support_code=extra_code,

还可以选择包含标准库,如下所示

headers = ["<math.h>"]

put the source of mult.c and mult.h in a string object called extra_code, then add the following line in your .weave invocation

support_code=extra_code,

there is also the option to include standard libraries as follows:

headers = ["<math.h>"]

enjoy

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文