This is an old version, view current version.
5.8 Diagonal Matrix Functions
vector
diagonal
(matrix x)
The diagonal of the matrix x
matrix
diag_matrix
(vector x)
The diagonal matrix with diagonal x
Although the diag_matrix
function is available, it is unlikely to
ever show up in an efficient Stan program. For example, rather than
converting a diagonal to a full matrix for use as a covariance matrix,
y ~ multi_normal(mu, diag_matrix(square(sigma)));
it is much more efficient to just use a univariate normal, which produces the same density,
y ~ normal(mu, sigma);
Rather than writing m * diag_matrix(v)
where m
is a matrix and v
is a vector, it is much more efficient to write diag_post_multiply(m, v)
(and similarly for pre-multiplication). By the same token, it is
better to use quad_form_diag(m, v)
rather than quad_form(m, diag_matrix(v))
.