## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(PGM2)

## -----------------------------------------------------------------------------
X <- BIB(3)
c(V = X$V, B = X$B, R = X$R, K = X$K, Lambda = X$Lambda)

X2 <- Gen(1, X$BIB)
c(V = X2$V, B = X2$B, R = X2$R, K = X2$K)

## -----------------------------------------------------------------------------
bib <- BIB(3)$BIB
bib

resolvable <- Resolvable(1, bib)
resolvable$RBIB

Uniform(resolvable$RBIB)$UD

## -----------------------------------------------------------------------------
bib <- BIB(3)$BIB
mat <- NULL
for (i in 1:15) mat[[i]] <- Gen(i, bib)$BIB2
x <- Reduce("rbind", mat)
e <- dim(x)[1]; b <- dim(x)[2]
v <- bib[1, ]
for (i in 1:e) for (j in 1:b) if (any(x[i, j] == v)) x[i, j] <- 0
for (i in e:1) if (all(x[i, ] == 0)) x <- x[-i, ]
s <- x[1, ]; s <- s[s > 0]
x1 <- matrix(nrow = dim(x)[1], ncol = length(s))
for (i in 1:dim(x)[1]) x1[i, ] <- x[i, ][x[i, ] > 0]
A <- unique(x1)
A

ud <- Uniform(A)
ud$UD

## -----------------------------------------------------------------------------
s <- Steps(4, 1)
names(s)
sapply(s$UDs, function(u) c(runs = u$n, factors = u$F))

## -----------------------------------------------------------------------------
table_rows <- function(p, ms) {
  out <- NULL
  for (m in ms) {
    X <- BIB(m, p)
    Y <- Resolvable(1, X$BIB)
    U <- Uniform(Y$RBIB)
    out <- rbind(out, data.frame(
      geometry = sprintf("PG(%d,%d)", m, p),
      BIB = sprintf("(%d, %d, %d)", X$V, X$K, X$Lambda),
      RBIB = sprintf("(%d, %d, %d, %d, %d)", Y$V, Y$B, Y$R, Y$K, X$Lambda),
      UD = sprintf("U(%d, %d^%d)", U$n, p, U$F)))
  }
  out
}

knitr::kable(table_rows(2, 2:4), caption = "Paper, Table 1 (p = 2)")
knitr::kable(table_rows(3, 2:4), caption = "Paper, Table 2 (p = 3)")

## -----------------------------------------------------------------------------
Q <- Qn(4, 2)
c(V = Q$V, B = Q$B, R = Q$R, K = Q$K, Lambda = Q$Lambda)

identical(dim(Qn(3, 2)$UD), dim(ud$UD))  # the design of Example 3

## -----------------------------------------------------------------------------
Z <- BIB(2, p = 5)          # BIB(31, 6, 1): the projective plane of order 5
W <- Resolvable(1, Z$BIB)   # RBIB(25, 30, 6, 5, 1): AG(2, 5)
Uniform(W$RBIB)$UD          # U(25, 5^6)

