返回介绍

C version

发布于 2025-02-25 23:44:03 字数 1812 浏览 0 评论 0 收藏 0

There are many ways to wrap C code for Python, such as Cython , Swig or Boost Python with numpy . However, the standard library comes with ctypes , a foreign function library that can be used to wrap C functions for use in pure python. This involves a little more work than the other approaches as shown below.

%%file pdist_c.c
#include <math.h>

void pdist_c(int n, int p, double xs[n*p], double D[n*n]) {
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            double s = 0.0;
            for (int k=0; k<p; k++) {
                double tmp = xs[i*p+k] - xs[j*p+k];
                s += tmp*tmp;
            }
            D[i*n+j] = sqrt(s);
        }
    }
}
Writing pdist_c.c
# Compile to a shared library
# Mac
! gcc -O3 -bundle -undefined dynamic_lookup pdist_c.c -o pdist_c.so
# Linux:
# ! gcc -O3 -fPIC -shared -std=c99 -lm pdist_c.c -o pdist_c.so
from ctypes import CDLL, c_int, c_void_p

def pdist_c(xs):

    # Use ctypes to load the library
    lib = CDLL('./pdist_c.so')

    # We need to give the argument adn return types explicitly
    lib.pdist_c.argtypes = [c_int, c_int, np.ctypeslib.ndpointer(dtype = np.float), np.ctypeslib.ndpointer(dtype = np.float)]
    lib.pdist_c.restype  = c_void_p

    n, p = xs.shape
    D = np.empty((n, n), np.float)

    lib.pdist_c(n, p, xs, D)
    return D
print pdist_c(A)
%timeit pdist_c(xs)
[[ 0.  5.]
 [ 5.  0.]]
100 loops, best of 3: 7.5 ms per loop

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文