latexMatrixOperations: Various Functions and Operators for '"latexMatrix"' Objects

latexMatrixOperationsR Documentation

Various Functions and Operators for "latexMatrix" Objects

Description

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() and inverse(), 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 signature 'latexMatrix,latexMatrix'
kronecker(X, Y, FUN = "*", make.dimnames = FALSE, ...)

x %X% y

Arguments

A

a "latexMatrix" object

...

for matmult() and sum() 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; supersedes simplify

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; for solve(), 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

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).

Value

All of these functions return "latexMatrix" objects, except for Dot(), which returns a LaTeX expression as a character string.

Author(s)

John Fox

See Also

latexMatrix

Examples

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)

matlib documentation built on Oct. 3, 2024, 1:09 a.m.