如何看待 igraph 包中函数背后的 C 代码?

How does one see the C code behind a function in igraph package?

我正在研究 igraph R 包中某些函数背后的详细代码。

例如,在下面的代码块中,我想了解快速贪婪聚类背后的功能。 igraph::cluster_fast_greedy 显示 R 函数。反过来,它调用某些用 C 编写的函数,最重要的是 C_R_igraph_community_fastgreedy。很明显,下面的代码并没有直接进行聚类,而是调用了前面提到的 C 函数。我想知道如何查看和编辑这些内容。

我知道使用 trace() 可以编辑 R 函数。

> igraph::cluster_fast_greedy
function (graph, merges = TRUE, modularity = TRUE, membership = TRUE, 
    weights = E(graph)$weight) 
{
    if (!is_igraph(graph)) {
        stop("Not a graph object")
    }
    if (!is.null(weights)) {
        weights <- as.numeric(weights)
    }
    on.exit(.Call(C_R_igraph_finalizer))
    res <- .Call(C_R_igraph_community_fastgreedy, graph, as.logical(merges), 
        as.logical(modularity), as.logical(membership), weights)
    if (igraph_opt("add.vertex.names") && is_named(graph)) {
        res$names <- V(graph)$name
    }
    res$algorithm <- "fast greedy"
    res$vcount <- vcount(graph)
    res$membership <- res$membership + 1
    res$merges <- res$merges + 1
    class(res) <- "communities"
    res
}
<bytecode: 0x000001d95fb78be0>
<environment: namespace:igraph>

我冒着被标记为重复的风险发布这个问题,但以前类似问题的答案对我没有太大帮助。可以找到它们 here and here.

我检查了 igraph 下载到我电脑的文件夹,没有找到任何类似的文件。

我也看过他们的github

C R 包的代码位于 src/ 文件夹中: src folder of igraph.

在你的例子中,函数是 here

igraph is a C library, with high-level interfaces in multiple languages, including R, Python and Mathematica. There are corresponding repositories on GitHub.

igraph的C核心源代码在https://github.com/igraph/igraph。您可以通过 R/igraph 访问的几乎所有算法都在这里实现。

R 界面的代码位于 https://github.com/igraph/rigraph。最新版本的 R 接口通常不会使用最新的 released 版本的 C 内核。要找出使用的是哪个版本的 C 核心,请查看 cigraph 子目录,这是一个 git 子模块,指向 C 核心的特定提交。

请注意,C 核心和 R 接口之间的大部分粘合代码都是从 functions.def file 自动生成的,因此您不会在存储库中找到它。