rvar-matmult {posterior} | R Documentation |
Matrix multiplication of random variables
Description
Matrix multiplication of random variables.
Usage
x %**% y
## S3 method for class 'rvar'
matrixOps(x, y)
Arguments
x |
(multiple options) The object to be postmultiplied by If a vector is used, it is treated as a row vector. |
y |
(multiple options) The object to be premultiplied by If a vector is used, it is treated as a column vector. |
Details
If x
or y
are vectors, they are converted into matrices prior to multiplication, with x
converted to a row vector and y
to a column vector. Numerics and logicals can be multiplied
by rvar
s and are broadcasted across all draws of the rvar
argument. Tensor multiplication
is used to efficiently multiply matrices across draws, so if either x
or y
is an rvar
,
x %**% y
will be much faster than rdo(x %*% y)
.
In R >= 4.3, you can also use %*%
in place of %**%
for matrix multiplication
of rvar
s. In R < 4.3, S3 classes cannot properly override %*%
, so
you must use %**%
for matrix multiplication of rvar
s.
Value
An rvar
representing the matrix product of x
and y
.
Examples
# d has mu (mean vector of length 3) and Sigma (3x3 covariance matrix)
d <- as_draws_rvars(example_draws("multi_normal"))
d$Sigma
# trivial example: multiplication by a non-random matrix
d$Sigma %**% diag(1:3)
# Decompose Sigma into R s.t. R'R = Sigma ...
R <- chol(d$Sigma)
# ... and recreate Sigma using matrix multiplication
t(R) %**% R