Matrix
Create
| Action |
Code |
Details |
|
Declare N by M matrix
|
|
|
|
Declare constrained matrix
|
matrix<lower=0, upper=1>[N,M] m;
|
|
|
Declare matrix with values
|
matrix[3,2] m = [ [1, 2], [3, 4], [5, 6] ];
|
|
|
Declare matrix filled with zeros
|
matrix[N,M] m = rep_matrix(0, N, M);
|
|
|
Declare matrix from series of row vectors
|
matrix[3,M] m = [ rv1, rv2, rv3 ];
|
|
|
Declare matrix from series of column vectors
|
matrix[N,3] m = [ v1', v2', v3']';
|
|
|
Create N by M matrix from replicated column vector of length N
|
|
|
|
Create N by M matrix from replicated row vector of length N
|
|
|
|
Declare K by K covariance matrix
|
|
|
|
Declare K by K correlation matrix
|
|
|
|
Declare K by K Cholesky factors of covariance matrix
|
cholesky_factor_cov[K] L;
|
|
|
Declare M by N Cholesky factors of covariance matrix
|
cholesky_factor_cov[M,N] L;
|
|
|
Declare Cholesky factors of correlation matrix
|
cholesky_factor_corr[K] L;
|
|
Properties
| Action |
Code |
Details |
|
Number of elements (NxM)
|
|
|
|
Dimension sizes
|
|
|
|
Number of columns (M)
|
|
|
|
Number of rows (N)
|
|
|
| Action |
Code |
Details |
|
Element at row i, column j
|
|
|
|
Extract _j_th column
|
|
|
|
Extract _j_th column
|
|
|
|
Extract _j_th column as a matrix
|
|
|
|
Extract _i_th row
|
|
Accessing column-wise is much faster than row-wise |
|
Extract _i_th row
|
|
|
|
Extract _i_th row as a matrix
|
|
|
|
Diagonal
|
|
|
Derive
For many useful functions, see https://jrnold.github.io/ssmodels-in-stan/stan-functions.html
Map
| Action |
Code |
Details |
|
Inverse
|
|
|
|
Sum elementwise
|
|
|
|
Multiply elementwise
|
|
|
|
Column-wise vector multiplication
|
|
|
|
Row-wise vector multiplication
|
|
|
Grow
| Action |
Code |
Details |
|
Append vector as column
|
|
|
|
Append vector as row
|
|
|
Shrink
| Action |
Code |
Details |
|
Slice columns
|
|
|
|
Slice rows
|
|
|
|
Submatrix by slicing rows and columns
|
|
|
|
Submatrix by start row and columns with length
|
block(m, rowstart, colstart, rowN, colN)
|
|
Reshape
Change dimensions or form of the matrix
| Action |
Code |
Details |
|
Cross-product (post-multiply)
|
|
m * m' |
|
Cross-product (pre-multiply)
|
|
m' * m |
|
Quadratic form with matrix
|
|
m2' * m * m2 |
|
Quadratic form with vector
|
|
v' * m * v |
Computations
| Action |
Code |
Details |
|
Column-wise self dot product
|
|
|
|
Row-wise self dot product
|
|
|
|
Dot product between columns
|
columns_dot_product(m, m2)
|
vector result |
|
Dot product between rows
|
|
vector result |
|
QR decomposition
|
Q = qr_Q(m);
R = qr_R(m);
|
|
|
Thin QR decomposition
|
Q = qr_thin_Q(m) * sqrt(N - 1);
R = qr_thin_R(m) / sqrt(N - 1);
|
|
Convert
| Action |
Code |
Details |
|
To vector
|
|
|