5.5 Dot Products and Specialized Products

real dot_product(vector x, vector y)
The dot product of x and y

real dot_product(vector x, row_vector y)
The dot product of x and y

real dot_product(row_vector x, vector y)
The dot product of x and y

real dot_product(row_vector x, row_vector y)
The dot product of x and y

row_vector columns_dot_product(vector x, vector y)
The dot product of the columns of x and y

row_vector columns_dot_product(row_vector x, row_vector y)
The dot product of the columns of x and y

row_vector columns_dot_product(matrix x, matrix y)
The dot product of the columns of x and y

vector rows_dot_product(vector x, vector y)
The dot product of the rows of x and y

vector rows_dot_product(row_vector x, row_vector y)
The dot product of the rows of x and y

vector rows_dot_product(matrix x, matrix y)
The dot product of the rows of x and y

real dot_self(vector x)
The dot product of the vector x with itself

real dot_self(row_vector x)
The dot product of the row vector x with itself

row_vector columns_dot_self(vector x)
The dot product of the columns of x with themselves

row_vector columns_dot_self(row_vector x)
The dot product of the columns of x with themselves

row_vector columns_dot_self(matrix x)
The dot product of the columns of x with themselves

vector rows_dot_self(vector x)
The dot product of the rows of x with themselves

vector rows_dot_self(row_vector x)
The dot product of the rows of x with themselves

vector rows_dot_self(matrix x)
The dot product of the rows of x with themselves

5.5.1 Specialized Products

matrix tcrossprod(matrix x)
The product of x postmultiplied by its own transpose, similar to the tcrossprod(x) function in R. The result is a symmetric matrix \(\text{x}\,\text{x}^{\top}\).

matrix crossprod(matrix x)
The product of x premultiplied by its own transpose, similar to the crossprod(x) function in R. The result is a symmetric matrix \(\text{x}^{\top}\,\text{x}\).

The following functions all provide shorthand forms for common expressions, which are also much more efficient.

matrix quad_form(matrix A, matrix B)
The quadratic form, i.e., B' * A * B.

real quad_form(matrix A, vector B)
The quadratic form, i.e., B' * A * B.

matrix quad_form_diag(matrix m, vector v)
The quadratic form using the column vector v as a diagonal matrix, i.e., diag_matrix(v) * m * diag_matrix(v).

matrix quad_form_diag(matrix m, row_vector rv)
The quadratic form using the row vector rv as a diagonal matrix, i.e., diag_matrix(rv) * m * diag_matrix(rv).

matrix quad_form_sym(matrix A, matrix B)
Similarly to quad_form, gives B' * A * B, but additionally checks if A is symmetric and ensures that the result is also symmetric.

real quad_form_sym(matrix A, vector B)
Similarly to quad_form, gives B' * A * B, but additionally checks if A is symmetric and ensures that the result is also symmetric.

real trace_quad_form(matrix A, matrix B)
The trace of the quadratic form, i.e., trace(B' * A * B).

real trace_gen_quad_form(matrix D,matrix A, matrix B)
The trace of a generalized quadratic form, i.e., trace(D * B' * A * B).

matrix multiply_lower_tri_self_transpose(matrix x)
The product of the lower triangular portion of x (including the diagonal) times its own transpose; that is, if L is a matrix of the same dimensions as x with L(m,n) equal to x(m,n) for \(\text{n} \leq \text{m}\) and L(m,n) equal to 0 if \(\text{n} > \text{m}\), the result is the symmetric matrix \(\text{L}\,\text{L}^{\top}\). This is a specialization of tcrossprod(x) for lower-triangular matrices. The input matrix does not need to be square.

matrix diag_pre_multiply(vector v, matrix m)
Return the product of the diagonal matrix formed from the vector v and the matrix m, i.e., diag_matrix(v) * m.

matrix diag_pre_multiply(row_vector rv, matrix m)
Return the product of the diagonal matrix formed from the vector rv and the matrix m, i.e., diag_matrix(rv) * m.

matrix diag_post_multiply(matrix m, vector v)
Return the product of the matrix m and the diagonal matrix formed from the vector v, i.e., m * diag_matrix(v).

matrix diag_post_multiply(matrix m, row_vector rv)
Return the product of the matrix m and the diagonal matrix formed from the the row vector rv, i.e., m * diag_matrix(rv).