Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: removed for loops from get.incidence.dense() (#1483) #1655

Merged
merged 2 commits into from
Jan 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions R/conversion.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -887,18 +886,22 @@ get.incidence.dense <- function(graph, types, names, attr) {
res <- matrix(0, n1, n2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens with non-numeric attributes?


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)

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[] <- recode[el]

# 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)) {
rownames(res) <- V(graph)$name[which(!types)]
Expand Down
Loading