Skip to content

Commit

Permalink
fix: fix includig diagonal elements in dense adjacency matrices (#1437)
Browse files Browse the repository at this point in the history
  • Loading branch information
aviator-app[bot] authored Aug 29, 2024
2 parents a26828f + 66d0e67 commit f09728c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
20 changes: 17 additions & 3 deletions R/conversion.R
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ get.adjedgelist <- function(graph, mode = c("all", "out", "in", "total"), loops
###################################################################

get.adjacency.dense <- function(graph, type = c("both", "upper", "lower"),
attr = NULL, weights = NULL, loops = FALSE, names = TRUE) {
attr = NULL, weights = NULL, loops = c("once", "twice", "ignore"), names = TRUE) {
ensure_igraph(graph)

type <- igraph.match.arg(type)
Expand All @@ -165,13 +165,27 @@ get.adjacency.dense <- function(graph, type = c("both", "upper", "lower"),
"both" = 2
)

if (is.logical(loops)) {
loops <- ifelse(loops, "once", "ignore")
lifecycle::deprecate_soft(
"2.0.4", "get.adjacency.dense(loops = 'must be a character')",
details = sprintf("Converting to get.adjacency.dense (loops = '%s')", loops)
)
}
loops <- igraph.match.arg(loops)
loops <- switch(loops,
"ignore" = 0L,
"twice" = 1L,
"once" = 2L
)

if (!is.null(weights)) weights <- as.numeric(weights)

if (is.null(attr)) {
on.exit(.Call(R_igraph_finalizer))
res <- .Call(
R_igraph_get_adjacency, graph, as.numeric(type), weights,
as.logical(loops)
loops
)
} else {
attr <- as.character(attr)
Expand Down Expand Up @@ -348,7 +362,7 @@ as_adjacency_matrix <- function(graph, type = c("both", "upper", "lower"),
if (sparse) {
get.adjacency.sparse(graph, type = type, attr = attr, names = names)
} else {
get.adjacency.dense(graph, type = type, attr = attr, weights = NULL, names = names)
get.adjacency.dense(graph, type = type, attr = attr, weights = NULL, names = names, loops = "once")
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/rinterface_extra.c
Original file line number Diff line number Diff line change
Expand Up @@ -4796,7 +4796,7 @@ SEXP R_igraph_get_adjacency(SEXP graph, SEXP ptype, SEXP pweights, SEXP ploops)
igraph_t g;
igraph_matrix_t res;
igraph_integer_t type=(igraph_integer_t) REAL(ptype)[0];
igraph_bool_t loops=LOGICAL(ploops)[0];
igraph_loops_t loops = (igraph_loops_t) INTEGER(ploops)[0];
igraph_vector_t weights;
SEXP result;

Expand Down

0 comments on commit f09728c

Please sign in to comment.