I created a very simple function that can print matrices and vectors with arbitrary character strings (usually operators) between them. It allows you to use matrices with different numbers of rows and consider vectors as matrixes of columns. It is not very difficult, so I am afraid that there are many examples where it fails. But for an example similar to the simple one in your question, that should be enough.
format() used to convert numbers to characters. This has the advantage that all rows of the matrix have the same width and are thus well aligned when printing. If necessary, you can add some format() arguments also as mat_op_print() arguments to make it customizable. As an example, I added the argument width , which can be used to control the minimum width of columns.
If matrices and vectors are names in a function call, these names are printed as headers in the first line. Otherwise, only numbers are printed.
So this is the function:
mat_op_print <- function(..., width = 0) { # get arguments args <- list(...) chars <- sapply(args, is.character) # auxilliary function to create character of n spaces spaces <- function(n) paste(rep(" ", n), collapse = "") # convert vectors to row matrix vecs <- sapply(args, is.vector) args[vecs & !chars] <- lapply(args[vecs & !chars], function(v) matrix(v, ncol = 1)) # convert all non-characters to character with format args[!chars] <- lapply(args[!chars], format, width = width) # print names as the first line, if present arg_names <- names(args) if (!is.null(arg_names)) { get_title <- function(x, name) { if (is.matrix(x)) { paste0(name, spaces(sum(nchar(x[1, ])) + ncol(x) - 1 - nchar(name))) } else { spaces(nchar(x)) } } cat(mapply(get_title, args, arg_names), "\n") } # auxiliary function to create the lines get_line <- function(x, n) { if (is.matrix(x)) { if (nrow(x) < n) { spaces(sum(nchar(x[1, ])) + ncol(x) - 1) } else { paste(x[n, ], collapse = " ") } } else if (n == 1) { x } else { spaces(nchar(x)) } } # print as many lines as needed for the matrix with most rows N <- max(sapply(args[!chars], nrow)) for (n in 1:N) { cat(sapply(args, get_line, n), "\n") } }
And this is an example of how this works:
A = matrix(c(0.5, 1, 3, 0.75, 2.8, 4), nrow = 2) x = c(0.5, 3.7, 2.3) y = c(0.7, -1.2) b = A %*% x - y mat_op_print(A = A, " * ", x = x, " - ", y = y, " = ", b = b, width = 6)
It is also possible to print slices of a three-dimensional array side by side:
A <- array(1:12, dim = c(2, 2, 3)) mat_op_print(A1 = A[, , 1], " | ", A2 = A[, , 2], " | ", A3 = A[, , 3]) ## A1 A2 A3 ## 1 3 | 5 7 | 9 11 ## 2 4 6 8 10 12