latexMatrixOperations | R Documentation |
"latexMatrix"
ObjectsThese 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()
and inverse()
, matrix inverse of a square matrix;
t()
, transpose;
determinant()
of a square matrix;
kronecker()
and %O%
, the Kronecker product.
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 signature 'latexMatrix,latexMatrix'
kronecker(X, Y, FUN = "*", make.dimnames = FALSE, ...)
x %X% y
A |
a |
... |
for |
as.numeric |
if |
e1 |
a |
e2 |
a |
B |
a |
x |
for |
y |
for |
simplify |
if |
X |
a |
power |
to raise a square matrix to this power, an integer |
logarithm |
to match the generic |
a |
a |
b |
ignored; to match the |
frac |
LaTeX command to use in forming fractions; the default
is |
Y |
a |
FUN |
to match the |
make.dimnames |
to match the |
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).
All of these functions return "latexMatrix"
objects,
except for Dot()
, which returns a LaTeX expression as a character string.
John Fox
latexMatrix
A <- latexMatrix(symbol="a", nrow=2, ncol=2)
B <- latexMatrix(symbol="b", nrow=2, ncol=2)
A
B
A + B
A - B
"a" * A
C <- latexMatrix(symbol="c", nrow=2, ncol=3)
A %*% C
t(C)
determinant(A)
cat(solve(A, simplify=TRUE))
D <- latexMatrix(matrix(letters[1:4], 2, 2))
D
as.numeric(D, locals=list(a=1, b=2, c=3, d=4))
X <- latexMatrix(matrix(c(3, 2, 0, 1, 1, 1, 2,-2, 1), 3, 3))
X
as.numeric(X)
MASS::fractions(as.numeric(inverse(X)))
(d <- determinant(X))
eval(parse(text=(gsub("\\\\cdot", "*", d))))
X <- latexMatrix(matrix(1:6, 2, 3), matrix="bmatrix")
I3 <- latexMatrix(diag(3))
I3 %X% X
kronecker(I3, X, sparse=TRUE)
(E <- latexMatrix(diag(1:3)))
# equivalent:
X %*% E
matmult(X, E)
matmult(X, E, simplify=FALSE, as.numeric=FALSE)
# equivalent:
X %*% E %*% E
matmult(X, E, E)
# equivalent:
E^-1
inverse(E)
solve(E)
solve(E, as.numeric=FALSE) # details
# equivalent
E^3
matpower(E, 3)
matpower(E, 3, as.numeric=FALSE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.