R/add1.R

Defines functions N_add1CA add1Column add1CA

Documented in add1CA add1Column N_add1CA

#' function to add a columns
#'
#' increases the number of columns by 1,
#' adding at most bestN(t-2,k-1,v)*v*(v-1) rows.
#'
#' @rdname add1
#'
#' @aliases add1CA
#' @aliases N_add1CA
#' @aliases add1Column
#'
#' @usage add1CA(t, k, v, ...)
#' @usage N_add1CA(t, k, v, ...)
#' @usage add1Column(D, t, v, ...)
#'
#' @param t integer: the required strength, or the strength of \code{D} (not checked)
#' @param k integer: the required number of columns
#' @param v integer: the required number of levels, or the number of levels of \code{D}
#' @param D a strength \code{t} CA with \code{v} levels in \code{k-1} columns\cr
#' (integer levels, starting at 0 or 1)
#' @param ... used for propagating an \code{exclude} argument, if needed
#'
#' @returns \code{add1Column} and \code{add1CA} return a CA of strength \code{t} in \code{v} levels in
#' \code{k} columns.\cr
#' It will have at most \code{\link{bestN}(t-2,k-2,v)*v*(v-1)} additional rows.\cr
#' \code{N_add1CA} returns the number of runs obtainable by \code{add1CA}.
#'
#' @section Details:
#' Functions \code{add1CA} and \code{N_add1CA} are obsolete and
#' are kept for backward compatibility and manual inspection only.
#' The implementation via \code{\link{bestCA}}, based
#' on \code{\link{Ns_add1}} and the internal function
#' \code{N_add1best}, is much more powerful and optionally permits
#' to add more than one column (by repeatedly adding one column).
#' Function \code{add1Column} is the workhorse function for
#' these more powerful functions.
#'
#' Function \code{add1CA} applies function \code{add1Column} to
#' the best available strength \code{t} CA at \code{v} levels
#' with \code{k-1} columns in the role of \code{D};
#' \code{\link{derive}}, \code{\link{fuse}} and \code{add1} constructions
#' are not included in the search for a best ingredient.\cr
#' The construction relies on appending to \code{D}
#' a copy of the last column of \code{D},
#' followed by adding rows that consist of\cr
#' the best strength \code{t-2} CA at \code{v} levels with \code{k-2} columns,
#' repeated \code{v*(v-1)} times, in the first \code{k-2} columns,\cr
#' each combined with one of the \code{v(v-1)} distinct level pairs
#' for the last two columns.\cr
#' For \code{t=2}, the best CA is simply a constant row of flexible values.
#'
#' The construction is not interesting for small CAs, but may be useful,
#' where good large CAs, e.g., ones constructed by cyclotomy,
#' have one less column
#' than needed (and a good CA with strength \code{t-2} for \code{k-1} columns is
#' available).
#'
#' @references CKRS, Theorem 3.2
#'
#' @examples
#' ## a case for which this is best
#' D <- powerCA(3,1331,2)
#' dim(D)
#' D2 <- add1Column(D, 3, 2)
#' dim(D2)
#' eCAN(3,1331,2)
#' eCAN(3,1332,2)
#' N_add1CA(3,1332,2)
#'
#' dim(add1CA(3,1332,2))
#'
#' \donttest{
#' A <- add1CA(6,2504,2)
#' dim(A)
#' eCAN(6,2504,2) ## better
#' Ns(4,2504,2) ## its a bit worse because this ingredient is not optimal
#' 3047 + (288 - 272)*2
#' eCAN(4,2504,2) ## Colbourn and Zhou power construction not implemented
#' }
#'

#' @export
add1CA <- function(t, k, v, ...){
   N <- N_add1CA(t, k, v, ...)
   if (!is.na(N))
   add1Column(
     bestCA(t,k-1,v,
        noderive=TRUE, nofuse=TRUE, noadd1=TRUE, ...),
     t,v)
}

#' @export
add1Column <- function(D, t, v, ...){
   Call <- sys.call()
   stopifnot(is.matrix(D))
   levs <- levels.no(D)
   stopifnot(all(levs==v))
   stopifnot(is.numeric(t))
   stopifnot(t%%1==0)
   stopifnot(t>=2)
   k <- ncol(D)
   ingredients <- list()
   aus <- cbind(D, D[,k])
   if (t==2)
     aus <- rbind(aus,
         cbind(matrix(NA,nrow=v*(v-1),ncol=k-1),
         OD2(v)[,1:2]))
   if (t==3){
     aus <- rbind(aus,
         cbind(kronecker(matrix(1,nrow=v*(v-1),ncol=1),matrix(0:(v-1),v,k-1)),
         kronecker(OD2(v)[,1:2],matrix(1,nrow=v,ncol=1))))
   }
   if (t>3){
     D2 <- bestCA(t-2,k-1,v,
            noderive=TRUE, nofuse=TRUE, noadd1=TRUE, ...)
     if (!is.null(attr(D2, "constr")))
       ingredients$D2 <- ingredientCA(D2, t-2, k-1, v)
     aus <- rbind(aus,
         cbind(kronecker(matrix(1,nrow=v*(v-1),ncol=1),
                   D2),
         kronecker(OD2(v)[,1:2],matrix(1,bestN(t-2,k-1,v, noderive=TRUE, nofuse=TRUE, noadd1=TRUE),ncol=1))))
   }
   if (!is.null(attr(D, "constr")))
     ingredients <- c(list(D=ingredientCA(D, t, k-1, v)), ingredients)
   if (length(ingredients)>0) attr(aus, "ingredients") <- ingredients
   attrs <- attributes(D)
   return(aus)
}

#' @export
N_add1CA <- function(t, k, v, ...){
   hilf <- bestN(t, k-1, v,
        noderive=TRUE, nofuse=TRUE, noadd1=TRUE, ...)
   #if (bestN(t,k,v, exclude="add1CA") <= hilf) return(NA)
   if (t==2) return(hilf + v*(v-1))
   if (t==3) return(hilf + v^2*(v-1))
   ## now t>3
   hilf + unname(bestN(t-2, k-1, v,
                  noderive=TRUE, nofuse=TRUE, noadd1=TRUE, ...))*v*(v-1)
}

Try the CAs package in your browser

Any scripts or data that you put into this service are public.

CAs documentation built on Nov. 29, 2026, 1:06 a.m.