R/kemenydesign_cpp.R

Defines functions kemenydesign_cpp

Documented in kemenydesign_cpp

#' Kemeny design matrix (C++ implementation)
#'
#' Compute the design matrix for Kemeny distance calculation.
#' This function uses an optimized C++ implementation for better performance.
#'
#' @param X A N by M numeric matrix or vector, where each row represents a ranking
#'   of M objects. If X is a vector, it will be converted to a single-row matrix.
#'
#' @return A numeric matrix with N rows and M*(M-1)/2 columns. Each column
#'   represents a pairwise comparison between objects:
#'   \itemize{
#'     \item{1: first object ranked lower (better) than second}
#'     \item{-1: first object ranked higher (worse) than second}
#'     \item{0: objects tied}
#'   }
#'
#' @details This is a C++ reimplementation of the original \code{kemenydesign}
#'   function for improved computational efficiency. The function processes all
#'   pairwise comparisons between M objects, generating M*(M-1)/2 binary features
#'   that encode the ranking structure.
#'
#'   The optimization provides significant speedup (typically 20-50x) compared to
#'   the R implementation, especially for large matrices.
#'
#' @references D'Ambrosio, A. (2008). Tree based methods for data editing and
#'   preference rankings. Unpublished PhD Thesis. Universita' degli Studi di
#'   Napoli Federico II.
#'
#' @author Antonio D'Ambrosio \email{antdambr@@unina.it}
#'
#' @examples
#' # Single ranking
#' x <- c(1, 3, 2, 4)
#' kemenydesign_cpp(x)
#'
#' # Multiple rankings
#' X <- matrix(c(1,2,3,4,
#'               4,3,2,1,
#'               1,1,2,2), nrow=3, byrow=TRUE)
#' kemenydesign_cpp(X)
#'
#' @seealso \code{\link{kemenydesign}} for the wrapper function
#' @seealso \code{\link{kemenyd}} for computing Kemeny distance using this design matrix
#'
#' @export
kemenydesign_cpp <- function(X) {
  # Handle vector input
  if (is.numeric(X) && !is.matrix(X)) {
    X <- matrix(X, nrow = 1)
  }

  # Convert to numeric matrix if needed
  if (!is.numeric(X)) {
    X <- as.matrix(X)
    mode(X) <- "numeric"
  }

  # Call C++ implementation (now correctly named kemenydesign_impl)
  result <- kemenydesign_impl(X)

  # Remove column names (consistent with original implementation)
  colnames(result) <- NULL

  return(result)
}

Try the ConsRank package in your browser

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

ConsRank documentation built on Nov. 24, 2026, 5:08 p.m.