File: test_constraint.R

package info (click to toggle)
r-cran-igraph 1.0.1-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 18,232 kB
  • sloc: ansic: 173,538; cpp: 19,365; fortran: 4,550; yacc: 1,164; tcl: 931; lex: 484; makefile: 149; sh: 9
file content (46 lines) | stat: -rw-r--r-- 1,229 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

context("constraint")

test_that("constraint works", {
  library(igraph)

  constraint.orig <- function(graph, nodes=V(graph), attr=NULL) {
    if (!is_igraph(graph)) {
      stop("Not a graph object")
    }
    idx <- degree(graph) != 0
    A <- as_adj(graph, attr=attr, sparse=FALSE)
    A <- A[idx, idx]
    n <- sum(idx)
    
    one <- c(rep(1,n))
    CZ <- A + t(A)
    cs <- CZ %*% one                      # degree of vertices
    ics <- 1/cs
    CS <- ics %*% t(one)                  # 1/degree of vertices
    P <- CZ * CS  #intermediate result: proportionate tie strengths
    PSQ <- P%*%P #sum paths of length two
    P.bi <- as.numeric(P>0)  #exclude paths to non-contacts (& reflexive):
    PC <- (P + (PSQ*P.bi))^2  #dyadic constraint
    ci <- PC %*% one      #overall constraint
    dim(ci) <- NULL

    ci2 <- numeric(vcount(graph))
    ci2[idx] <- ci
    ci2[!idx] <- NaN
    ci2[nodes]
  }

  karate <- make_graph("Zachary")

  c1 <- constraint(karate)
  c2 <- constraint.orig(karate)
  expect_that(c1, equals(c2))

  set.seed(42)
  E(karate)$weight <- sample(1:10, replace=TRUE, ecount(karate))
  wc1 <- constraint(karate)
  wc2 <- constraint.orig(karate, attr="weight")
  expect_that(wc1, equals(wc2))
})