使用 python scipy.weave 内联 ctype 变量?

发布于 2024-07-26 23:26:26 字数 583 浏览 3 评论 0原文

我正在尝试使用 scipy.weave.inline 将 ctype 变量传递给内联 c 代码。 人们会认为这很简单。 当使用普通的 python 对象类型时,文档很好,但是,它们具有比我需要的更多的功能,并且在使用 C 时使用 ctypes 对我来说更有意义。但是,我不确定我的错误在哪里。

from scipy.weave import inline  
from ctypes import *
def test():
    y = c_float()*50
    x = pointer(y)
    code = """
          #line 120 "laplace.py" (This is only useful for debugging)
          int i;
          for (i=0; i < 50; i++) {
                  x[i] = 1;
          }
           """
    inline(code, [x], compiler = 'gcc')
    return y
output = test()
pi = pointer(output)
print pi[0]

I am trying to pass a ctype variable to inline c code using scipy.weave.inline. One would think this would be simple. Documentation is good when doing it with normal python object types, however, they have a lot more features than I need, and It makes more sense to me to use ctypes when working with C. I am unsure, however, where my error is.

from scipy.weave import inline  
from ctypes import *
def test():
    y = c_float()*50
    x = pointer(y)
    code = """
          #line 120 "laplace.py" (This is only useful for debugging)
          int i;
          for (i=0; i < 50; i++) {
                  x[i] = 1;
          }
           """
    inline(code, [x], compiler = 'gcc')
    return y
output = test()
pi = pointer(output)
print pi[0]

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

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

发布评论

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

评论(1

ι不睡觉的鱼゛ 2024-08-02 23:26:26

scipy.weave 对 ctypes 一无所知。 输入仅限于大多数基本内置类型、numpy 数组、wxPython 对象、VTK 对象和 SWIG 包装对象。 不过,您可以添加自己的转换器代码。 目前这方面的文档不多,但您可以查看 SWIG 实现 作为一个指导性示例。

scipy.weave does not know anything about ctypes. Inputs are restricted to most of the basic builtin types, numpy arrays, wxPython objects, VTK objects, and SWIG wrapped objects. You can add your own converter code, though. There is currently not much documentation on this, but you can look at the SWIG implementation as an instructive example.

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