#' Sylvester Equation
#'
#'     A X + X B = C
#'
#' @examples
#' A <- rmatrix(5, 5)
#' B <- rmatrix(3, 3)
#' C <- rmatrix(5, 3)
#'
#' X <- sylvester(A, B, C)
#'
#' all.equal(A %*% X + X %*% B, C)
#'
#' @export
sylvester <- function(A, B, C) {
    vecX <- solve(diag(ncol(C)) %x% A + t(B) %x% diag(nrow(C)), as.vector(C))
    matrix(vecX, nrow(C), ncol(C))
}
