rigraph icon indicating copy to clipboard operation
rigraph copied to clipboard

warning in graph_from_adjacency_matrix() example

Open maelle opened this issue 2 years ago • 5 comments

library("igraph")
#> 
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union
nzs <- function(x) sort(x[x != 0])
adjm <- matrix(runif(100), 10)
adjm[adjm < 0.5] <- 0
g3 <- graph_from_adjacency_matrix((adjm + t(adjm)) / 2,
  weighted = TRUE,
  mode = "undirected"
)

g4 <- graph_from_adjacency_matrix(adjm, weighted = TRUE, mode = "max")
all(nzs(pmax(adjm, t(adjm))[upper.tri(adjm)]) == sort(E(g4)$weight))
#> Warning in nzs(pmax(adjm, t(adjm))[upper.tri(adjm)]) == sort(E(g4)$weight):
#> longer object length is not a multiple of shorter object length
#> [1] FALSE

Created on 2024-03-11 with reprex v2.1.0

maelle avatar Mar 11 '24 12:03 maelle

this happens several times in that same Rd file.

maelle avatar Mar 11 '24 12:03 maelle

Maelle, Is there a compelling reason why this error would not occur?

clpippel avatar Mar 11 '24 14:03 clpippel

I think it'd be better if the example didn't show a warning, now I need to think about how to achieve this.

maelle avatar Mar 11 '24 15:03 maelle

Note that function d0() in

g9 <- graph_from_adjacency_matrix(adjm, weighted = TRUE, mode = "plus", diag = FALSE)
d0 <- function(x) {
  diag(x) <- 0
}

is wrong. It must end with return(x). Otherwise

all(nzs((d0(adjm + t(adjm)))[lower.tri(adjm)]) == sort(E(g9)$weight))

will always return TRUE.

clpippel avatar Mar 11 '24 16:03 clpippel

The solution is to add diag=TRUE in upper.tri() and lower.tri() except the one after g9 in the code. This will eliminate the warnings.

clpippel avatar Mar 11 '24 17:03 clpippel

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.

github-actions[bot] avatar Mar 23 '25 03:03 github-actions[bot]