Skip to content

Intended behaviour of [<-.igraph #1662

@schochastics

Description

@schochastics

Trying to add some more tests for #1661, I noticed some probably not intended behaviours of the current implementation

library(igraph)
#> 
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union
packageVersion("igraph")
#> [1] '2.1.2'
g1 <- make_empty_graph(n = 10, directed = FALSE)
g1[1:5,] <- 1
g1[]
#> 10 x 10 sparse Matrix of class "dgCMatrix"
#>                          
#>  [1,] 1 2 2 2 2 1 1 1 1 1
#>  [2,] 2 1 2 2 2 1 1 1 1 1
#>  [3,] 2 2 1 2 2 1 1 1 1 1
#>  [4,] 2 2 2 1 2 1 1 1 1 1
#>  [5,] 2 2 2 2 1 1 1 1 1 1
#>  [6,] 1 1 1 1 1 . . . . .
#>  [7,] 1 1 1 1 1 . . . . .
#>  [8,] 1 1 1 1 1 . . . . .
#>  [9,] 1 1 1 1 1 . . . . .
#> [10,] 1 1 1 1 1 . . . . .

g2 <- make_empty_graph(n = 10, directed = FALSE)
g2[1:5,1:5] <- 1
g2[]
#> 10 x 10 sparse Matrix of class "dgCMatrix"
#>                          
#>  [1,] 1 2 2 2 2 . . . . .
#>  [2,] 2 1 2 2 2 . . . . .
#>  [3,] 2 2 1 2 2 . . . . .
#>  [4,] 2 2 2 1 2 . . . . .
#>  [5,] 2 2 2 2 1 . . . . .
#>  [6,] . . . . . . . . . .
#>  [7,] . . . . . . . . . .
#>  [8,] . . . . . . . . . .
#>  [9,] . . . . . . . . . .
#> [10,] . . . . . . . . . .

Created on 2025-01-19 with reprex v2.1.1

In my opinion, this should rather look like this

A1 <- matrix(0,10,10)
A1[1:5,] <- 1
diag(A1) <- 0
A1
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#>  [1,]    0    1    1    1    1    1    1    1    1     1
#>  [2,]    1    0    1    1    1    1    1    1    1     1
#>  [3,]    1    1    0    1    1    1    1    1    1     1
#>  [4,]    1    1    1    0    1    1    1    1    1     1
#>  [5,]    1    1    1    1    0    1    1    1    1     1
#>  [6,]    0    0    0    0    0    0    0    0    0     0
#>  [7,]    0    0    0    0    0    0    0    0    0     0
#>  [8,]    0    0    0    0    0    0    0    0    0     0
#>  [9,]    0    0    0    0    0    0    0    0    0     0
#> [10,]    0    0    0    0    0    0    0    0    0     0

A2 <- matrix(0,10,10)
A2[1:5,1:5] <- 1
diag(A2) <- 0
A2
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#>  [1,]    0    1    1    1    1    0    0    0    0     0
#>  [2,]    1    0    1    1    1    0    0    0    0     0
#>  [3,]    1    1    0    1    1    0    0    0    0     0
#>  [4,]    1    1    1    0    1    0    0    0    0     0
#>  [5,]    1    1    1    1    0    0    0    0    0     0
#>  [6,]    0    0    0    0    0    0    0    0    0     0
#>  [7,]    0    0    0    0    0    0    0    0    0     0
#>  [8,]    0    0    0    0    0    0    0    0    0     0
#>  [9,]    0    0    0    0    0    0    0    0    0     0
#> [10,]    0    0    0    0    0    0    0    0    0     0

Created on 2025-01-19 with reprex v2.1.1

thoughts @szhorvat @krlmlr ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions