This function is designed to print a collection of matrices, vectors, character strings and matrix expressions side by side. A typical use is to illustrate matrix equations in a compact and comprehensible way.
Usage
printMatEqn(..., space = 1, tol = sqrt(.Machine$double.eps), fractions = FALSE)
Arguments
- ...
matrices and character operations to be passed and printed to the console. These can include named arguments, character string operation symbols (e.g.,
"+"
)- space
amount of blank spaces to place around operations such as
"+"
,"-"
,"="
, etc- tol
tolerance for rounding
- fractions
logical; if
TRUE
, try to express non-integers as rational numbers, using thefractions
function; if you require greater accuracy, you can set thecycles
(default 10) and/ormax.denominator
(default 2000) arguments tofractions
as a global option, e.g.,options(fractions=list(cycles=100, max.denominator=10^4))
.
Examples
A <- matrix(c(2, 1, -1,
-3, -1, 2,
-2, 1, 2), 3, 3, byrow=TRUE)
x <- c(2, 3, -1)
# provide implicit or explicit labels
printMatEqn(AA = A, "*", xx = x, '=', b = A %*% x)
#> AA xx b
#> 2 1 -1 * 2 = 8
#> -3 -1 2 3 -11
#> -2 1 2 -1 -3
printMatEqn(A, "*", x, '=', b = A %*% x)
#> A x b
#> 2 1 -1 * 2 = 8
#> -3 -1 2 3 -11
#> -2 1 2 -1 -3
printMatEqn(A, "*", x, '=', A %*% x)
#> A x A %*% x
#> 2 1 -1 * 2 = 8
#> -3 -1 2 3 -11
#> -2 1 2 -1 -3
# compare with showEqn
b <- c(4, 2, 1)
printMatEqn(A, x=paste0("x", 1:3),"=", b)
#> A x b
#> 2 1 -1 x1 = 4
#> -3 -1 2 x2 2
#> -2 1 2 x3 1
showEqn(A, b)
#> 2*x1 + 1*x2 - 1*x3 = 4
#> -3*x1 - 1*x2 + 2*x3 = 2
#> -2*x1 + 1*x2 + 2*x3 = 1
# decimal example
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
printMatEqn(A, "*", x, "-", y, "=", b)
#> A x y b
#> 0.50 3.00 2.80 * 0.5 - 0.7 = 17.090
#> 1.00 0.75 4.00 3.7 -1.2 13.675
#> 2.3
printMatEqn(A, "*", x, "-", y, "=", b, fractions=TRUE)
#> A x y b
#> 1/2 3 14/5 * 1/2 - 7/10 = 1709/100
#> 1 3/4 4 37/10 -6/5 547/40
#> 23/10