Various Functions and Operators for "latexMatrix"
Objects
Source: R/latexMatrixOperations.R
latexMatrixOperations.Rd
These operators and functions provide for LaTeX representations of symbolic and numeric matrix arithmetic and computations. They provide reasonable means to compose meaningful matrix equations in LaTeX far easier than doing this manually matrix by matrix.
The following operators and functions are documented here:
matsum()
and+
, matrix addition;matdiff()
and-
, matrix subtraction and negation;*
, product of a scalar and a matrix;Dot()
, inner product of two vectors;matprod()
and%*%
, matrix product;matpower()
and^
, powers (including inverse) of a square matrix;solve()
andinverse()
, matrix inverse of a square matrix;t()
, transpose;determinant()
of a square matrix;kronecker()
and%O%
, the Kronecker product.
Usage
matsum(A, ...)
# S3 method for class 'latexMatrix'
matsum(A, ..., as.numeric = TRUE)
# S3 method for class 'latexMatrix'
e1 + e2
matdiff(A, B, ...)
# S3 method for class 'latexMatrix'
matdiff(A, B = NULL, as.numeric = TRUE, ...)
# S3 method for class 'latexMatrix'
e1 - e2
# S3 method for class 'latexMatrix'
e1 * e2
Dot(x, y, simplify = TRUE)
matmult(X, ...)
# S3 method for class 'latexMatrix'
matmult(X, ..., simplify = TRUE, as.numeric = TRUE)
# S3 method for class 'latexMatrix'
x %*% y
matpower(X, power, ...)
# S3 method for class 'latexMatrix'
matpower(X, power, simplify = TRUE, as.numeric = TRUE, ...)
# S3 method for class 'latexMatrix'
e1^e2
inverse(X, ...)
# S3 method for class 'latexMatrix'
inverse(X, ..., as.numeric = TRUE, simplify = TRUE)
# S3 method for class 'latexMatrix'
t(x)
# S3 method for class 'latexMatrix'
determinant(x, logarithm, ...)
# S3 method for class 'latexMatrix'
solve(
a,
b,
simplify = FALSE,
as.numeric = TRUE,
frac = c("\\dfrac", "\\frac", "\\tfrac", "\\cfrac"),
...
)
# S4 method for class 'latexMatrix,latexMatrix'
kronecker(X, Y, FUN = "*", make.dimnames = FALSE, ...)
x %X% y
Arguments
- A
a
"latexMatrix"
object- ...
for
matmult()
andsum()
zero or more"latexMatrix"
objects; otherwise arguments to be passed down- as.numeric
if
TRUE
(the default) and the matrices to be multiplied, added, etc., can be coerced to numeric, matrix multiplication, addition, etc., is performed numerically; supersedessimplify
- e1
a
"latexMatrix"
object; or for*
a scalar;- e2
a
"latexMatrix"
object; for*
a scalar; for^
an integer power>= -1
to raise a square matrix- B
a
"latexMatrix"
object- x
for
Dot
a numeric or character vector; otherwise a"latexMatrix"
object- y
for
Dot
a numeric or character vector; otherwise a"latexMatrix"
object- simplify
if
TRUE
(the default), an attempt is made to simplify the result slightly; forsolve()
, return a LaTeX expression with the inverse of the determinant in front of the adjoint matrix rather than a"latexMatrix"
object in which each element of the adjoint matrix is divided by the determinant- X
a
"latexMatrix"
object- power
to raise a square matrix to this power, an integer
>= -1
.- logarithm
to match the generic
determinant()
function, ignored- a
a
"latexMatrix"
object representing a square matrix- b
ignored; to match the
solve()
generic- frac
LaTeX command to use in forming fractions; the default is
"\dfrac"
- Y
a
"latexMatrix"
object- FUN
to match the
kronecker()
generic, ignored- make.dimnames
to match the
kronecker()
generic, ignored
Value
All of these functions return "latexMatrix"
objects,
except for Dot()
, which returns a LaTeX expression as a character string.
Details
These operators and functions only apply to "latexMatrix"
objects
of definite (i.e., numeric) dimensions.
When there are both a function and an
operator (e.g., matmult()
and %*%
), the former is more
flexible via optional arguments and the latter calls the former with default
arguments. For example, using the operator A %*% B
multiplies
the two matrices A
and B
, returning a symbolic result.
The function matmult()
multiplies two or more matrices, and
can simplify the result and/or produced the numeric representation of the
product.
The result of matrix multiplication, \(\mathbf{C} = \mathbf{A} \: \mathbf{B}\) is composed of the vector inner (dot) products of each row of \(\mathbf{A}\) with each column of \(\mathbf{B}\), $$c_{ij} = \mathbf{a}_i^\top \mathbf{b}_j = \Sigma_k a_{ik} \cdot b_{kj}$$
The Dot()
function computes the inner product symbolically in LaTeX notation for
numeric and character vectors, simplifying the result if simplify = TRUE.
The LaTeX symbol for multiplication ("\cdot"
by default)
can be changed by changing options(latexMultSymbol)
,
e.g, options(latexMultSymbol = "\\times")
(note the double-backslash).
Examples
A <- latexMatrix(symbol="a", nrow=2, ncol=2)
B <- latexMatrix(symbol="b", nrow=2, ncol=2)
A
#> \begin{pmatrix}
#> a_{11} & a_{12} \\
#> a_{21} & a_{22} \\
#> \end{pmatrix}
B
#> \begin{pmatrix}
#> b_{11} & b_{12} \\
#> b_{21} & b_{22} \\
#> \end{pmatrix}
A + B
#> \begin{pmatrix}
#> a_{11} + b_{11} & a_{12} + b_{12} \\
#> a_{21} + b_{21} & a_{22} + b_{22} \\
#> \end{pmatrix}
A - B
#> \begin{pmatrix}
#> a_{11} - b_{11} & a_{12} - b_{12} \\
#> a_{21} - b_{21} & a_{22} - b_{22} \\
#> \end{pmatrix}
"a" * A
#> \begin{pmatrix}
#> a \cdot a_{11} & a \cdot a_{12} \\
#> a \cdot a_{21} & a \cdot a_{22} \\
#> \end{pmatrix}
C <- latexMatrix(symbol="c", nrow=2, ncol=3)
A %*% C
#> \begin{pmatrix}
#> a_{11} \cdot c_{11} + a_{12} \cdot c_{21} & a_{11} \cdot c_{12} + a_{12} \cdot c_{22} & a_{11} \cdot c_{13} + a_{12} \cdot c_{23} \\
#> a_{21} \cdot c_{11} + a_{22} \cdot c_{21} & a_{21} \cdot c_{12} + a_{22} \cdot c_{22} & a_{21} \cdot c_{13} + a_{22} \cdot c_{23} \\
#> \end{pmatrix}
t(C)
#> \begin{pmatrix}
#> c_{11} & c_{21} \\
#> c_{12} & c_{22} \\
#> c_{13} & c_{23} \\
#> \end{pmatrix}
determinant(A)
#> [1] "a_{11} \\cdot a_{22} - a_{12} \\cdot a_{21}"
cat(solve(A, simplify=TRUE))
#> \frac{1}{a_{11} \cdot a_{22} - a_{12} \cdot a_{21}}
#> \begin{pmatrix}
#> a_{22} & -a_{12} \\
#> -a_{21} & a_{11} \\
#> \end{pmatrix}
D <- latexMatrix(matrix(letters[1:4], 2, 2))
D
#> \begin{pmatrix}
#> a & c \\
#> b & d \\
#> \end{pmatrix}
as.numeric(D, locals=list(a=1, b=2, c=3, d=4))
#> [,1] [,2]
#> [1,] 1 3
#> [2,] 2 4
X <- latexMatrix(matrix(c(3, 2, 0, 1, 1, 1, 2,-2, 1), 3, 3))
X
#> \begin{pmatrix}
#> 3 & 1 & 2 \\
#> 2 & 1 & -2 \\
#> 0 & 1 & 1 \\
#> \end{pmatrix}
as.numeric(X)
#> [,1] [,2] [,3]
#> [1,] 3 1 2
#> [2,] 2 1 -2
#> [3,] 0 1 1
MASS::fractions(as.numeric(inverse(X)))
#> [,1] [,2] [,3]
#> [1,] 3/11 1/11 -4/11
#> [2,] -2/11 3/11 10/11
#> [3,] 2/11 -3/11 1/11
(d <- determinant(X))
#> [1] "3 \\cdot (1 \\cdot 1 - (-2) \\cdot 1) - 1 \\cdot (2 \\cdot 1 - (-2) \\cdot 0) + 2 \\cdot (2 \\cdot 1 - 1 \\cdot 0)"
eval(parse(text=(gsub("\\\\cdot", "*", d))))
#> [1] 11
X <- latexMatrix(matrix(1:6, 2, 3), matrix="bmatrix")
I3 <- latexMatrix(diag(3))
I3 %X% X
#> \begin{pmatrix}
#> 1 \cdot 1 & 1 \cdot 3 & 1 \cdot 5 & 0 & 0 & 0 & 0 & 0 & 0 \\
#> 1 \cdot 2 & 1 \cdot 4 & 1 \cdot 6 & 0 & 0 & 0 & 0 & 0 & 0 \\
#> 0 & 0 & 0 & 1 \cdot 1 & 1 \cdot 3 & 1 \cdot 5 & 0 & 0 & 0 \\
#> 0 & 0 & 0 & 1 \cdot 2 & 1 \cdot 4 & 1 \cdot 6 & 0 & 0 & 0 \\
#> 0 & 0 & 0 & 0 & 0 & 0 & 1 \cdot 1 & 1 \cdot 3 & 1 \cdot 5 \\
#> 0 & 0 & 0 & 0 & 0 & 0 & 1 \cdot 2 & 1 \cdot 4 & 1 \cdot 6 \\
#> \end{pmatrix}
kronecker(I3, X, sparse=TRUE)
#> \begin{pmatrix}
#> 1 \cdot 1 & 1 \cdot 3 & 1 \cdot 5 & & & & & & \\
#> 1 \cdot 2 & 1 \cdot 4 & 1 \cdot 6 & & & & & & \\
#> & & & 1 \cdot 1 & 1 \cdot 3 & 1 \cdot 5 & & & \\
#> & & & 1 \cdot 2 & 1 \cdot 4 & 1 \cdot 6 & & & \\
#> & & & & & & 1 \cdot 1 & 1 \cdot 3 & 1 \cdot 5 \\
#> & & & & & & 1 \cdot 2 & 1 \cdot 4 & 1 \cdot 6 \\
#> \end{pmatrix}
(E <- latexMatrix(diag(1:3)))
#> \begin{pmatrix}
#> 1 & 0 & 0 \\
#> 0 & 2 & 0 \\
#> 0 & 0 & 3 \\
#> \end{pmatrix}
# equivalent:
X %*% E
#> \begin{bmatrix}
#> 1 & 6 & 15 \\
#> 2 & 8 & 18 \\
#> \end{bmatrix}
matmult(X, E)
#> \begin{bmatrix}
#> 1 & 6 & 15 \\
#> 2 & 8 & 18 \\
#> \end{bmatrix}
matmult(X, E, simplify=FALSE, as.numeric=FALSE)
#> \begin{bmatrix}
#> 1 \cdot 1 + 3 \cdot 0 + 5 \cdot 0 & 1 \cdot 0 + 3 \cdot 2 + 5 \cdot 0 & 1 \cdot 0 + 3 \cdot 0 + 5 \cdot 3 \\
#> 2 \cdot 1 + 4 \cdot 0 + 6 \cdot 0 & 2 \cdot 0 + 4 \cdot 2 + 6 \cdot 0 & 2 \cdot 0 + 4 \cdot 0 + 6 \cdot 3 \\
#> \end{bmatrix}
# equivalent:
X %*% E %*% E
#> \begin{bmatrix}
#> 1 & 12 & 45 \\
#> 2 & 16 & 54 \\
#> \end{bmatrix}
matmult(X, E, E)
#> \begin{bmatrix}
#> 1 & 12 & 45 \\
#> 2 & 16 & 54 \\
#> \end{bmatrix}
# equivalent:
E^-1
#> \begin{pmatrix}
#> 1.00000 & 0.00000 & 0.00000 \\
#> 0.00000 & 0.50000 & 0.00000 \\
#> 0.00000 & 0.00000 & 0.33333 \\
#> \end{pmatrix}
inverse(E)
#> \begin{pmatrix}
#> 1.00000 & 0.00000 & 0.00000 \\
#> 0.00000 & 0.50000 & 0.00000 \\
#> 0.00000 & 0.00000 & 0.33333 \\
#> \end{pmatrix}
solve(E)
#> \begin{pmatrix}
#> 1.00000 & 0.00000 & 0.00000 \\
#> 0.00000 & 0.50000 & 0.00000 \\
#> 0.00000 & 0.00000 & 0.33333 \\
#> \end{pmatrix}
solve(E, as.numeric=FALSE) # details
#> \begin{pmatrix}
#> \dfrac{2 \cdot 3 - 0 \cdot 0}{1 \cdot (2 \cdot 3 - 0 \cdot 0) - 0 \cdot (0 \cdot 3 - 0 \cdot 0) + 0 \cdot (0 \cdot 0 - 2 \cdot 0)} & \dfrac{-(0 \cdot 3 - 0 \cdot 0)}{1 \cdot (2 \cdot 3 - 0 \cdot 0) - 0 \cdot (0 \cdot 3 - 0 \cdot 0) + 0 \cdot (0 \cdot 0 - 2 \cdot 0)} & \dfrac{0 \cdot 0 - 0 \cdot 2}{1 \cdot (2 \cdot 3 - 0 \cdot 0) - 0 \cdot (0 \cdot 3 - 0 \cdot 0) + 0 \cdot (0 \cdot 0 - 2 \cdot 0)} \\
#> \dfrac{-(0 \cdot 3 - 0 \cdot 0)}{1 \cdot (2 \cdot 3 - 0 \cdot 0) - 0 \cdot (0 \cdot 3 - 0 \cdot 0) + 0 \cdot (0 \cdot 0 - 2 \cdot 0)} & \dfrac{1 \cdot 3 - 0 \cdot 0}{1 \cdot (2 \cdot 3 - 0 \cdot 0) - 0 \cdot (0 \cdot 3 - 0 \cdot 0) + 0 \cdot (0 \cdot 0 - 2 \cdot 0)} & \dfrac{-(1 \cdot 0 - 0 \cdot 0)}{1 \cdot (2 \cdot 3 - 0 \cdot 0) - 0 \cdot (0 \cdot 3 - 0 \cdot 0) + 0 \cdot (0 \cdot 0 - 2 \cdot 0)} \\
#> \dfrac{0 \cdot 0 - 2 \cdot 0}{1 \cdot (2 \cdot 3 - 0 \cdot 0) - 0 \cdot (0 \cdot 3 - 0 \cdot 0) + 0 \cdot (0 \cdot 0 - 2 \cdot 0)} & \dfrac{-(1 \cdot 0 - 0 \cdot 0)}{1 \cdot (2 \cdot 3 - 0 \cdot 0) - 0 \cdot (0 \cdot 3 - 0 \cdot 0) + 0 \cdot (0 \cdot 0 - 2 \cdot 0)} & \dfrac{1 \cdot 2 - 0 \cdot 0}{1 \cdot (2 \cdot 3 - 0 \cdot 0) - 0 \cdot (0 \cdot 3 - 0 \cdot 0) + 0 \cdot (0 \cdot 0 - 2 \cdot 0)} \\
#> \end{pmatrix}
# equivalent
E^3
#> \begin{pmatrix}
#> 1 & 0 & 0 \\
#> 0 & 8 & 0 \\
#> 0 & 0 & 27 \\
#> \end{pmatrix}
matpower(E, 3)
#> \begin{pmatrix}
#> 1 & 0 & 0 \\
#> 0 & 8 & 0 \\
#> 0 & 0 & 27 \\
#> \end{pmatrix}
matpower(E, 3, as.numeric=FALSE)
#> \begin{pmatrix}
#> 1 & 0 & 0 \\
#> 0 & (2 \cdot 2) \cdot 2 & 0 \\
#> 0 & 0 & (3 \cdot 3) \cdot 3 \\
#> \end{pmatrix}