From a56d6e547f0064837413334c2f75257599f82323 Mon Sep 17 00:00:00 2001 From: schochastics Date: Sat, 21 Sep 2024 21:40:59 +0200 Subject: [PATCH 1/2] removed for loops from get.incidence.dense --- R/conversion.R | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/R/conversion.R b/R/conversion.R index ec548d0129..ddba5ee6bd 100644 --- a/R/conversion.R +++ b/R/conversion.R @@ -890,15 +890,14 @@ get.incidence.dense <- function(graph, types, names, attr) { recode[!types] <- seq_len(n1) recode[types] <- seq_len(n2) - for (i in seq(length.out = ecount(graph))) { - eo <- ends(graph, i, names = FALSE) - e <- recode[eo] - if (!types[eo[1]]) { - res[e[1], e[2]] <- edge_attr(graph, attr, i) - } else { - res[e[2], e[1]] <- edge_attr(graph, attr, i) - } - } + el <- as_edgelist(graph, names = FALSE) + idx <- types[el[, 1]] + el[, 1] <- recode[el[, 1]] + el[, 2] <- recode[el[, 2]] + + tmp <- el[idx, 2:1] + el[idx, ] <- tmp + res[el] <- edge_attr(graph, attr) if (names && "name" %in% vertex_attr_names(graph)) { rownames(res) <- V(graph)$name[which(!types)] From b68bef0b4192842a44b3d32de1089e2b86ec472c Mon Sep 17 00:00:00 2001 From: schochastics Date: Wed, 9 Oct 2024 21:25:46 +0200 Subject: [PATCH 2/2] refactor and added comments to get.incidence.dense --- R/conversion.R | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/R/conversion.R b/R/conversion.R index ddba5ee6bd..2129e22f8c 100644 --- a/R/conversion.R +++ b/R/conversion.R @@ -378,7 +378,6 @@ as_adjacency_matrix <- function(graph, type = c("both", "upper", "lower"), as_adj <- function(graph, type = c("both", "upper", "lower"), attr = NULL, edges = deprecated(), names = TRUE, sparse = igraph_opt("sparsematrices")) { - lifecycle::deprecate_soft("2.1.0", "as_adj()", "as_adjacency_matrix()") as_adjacency_matrix( @@ -887,16 +886,21 @@ get.incidence.dense <- function(graph, types, names, attr) { res <- matrix(0, n1, n2) recode <- numeric(vc) + # move from 1..n indexing to 1..n1 row indices for type == FALSE + # and 1..n2 col indices for type == TRUE + # recode holds the mapping [1..n] -> [1..n1,1..n2] recode[!types] <- seq_len(n1) recode[types] <- seq_len(n2) el <- as_edgelist(graph, names = FALSE) idx <- types[el[, 1]] - el[, 1] <- recode[el[, 1]] - el[, 2] <- recode[el[, 2]] + el[] <- recode[el] - tmp <- el[idx, 2:1] - el[idx, ] <- tmp + # switch order of source/target such that nodes with + # type == FALSE are in el[ ,1] + el[idx, ] <- el[idx, 2:1] + # el[ ,1] only holds values 1..n1 and el[ ,2] values 1..n2 + # and we can populate the matrix res[el] <- edge_attr(graph, attr) if (names && "name" %in% vertex_attr_names(graph)) {