## 6.5 Dot products and specialized products

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

### 6.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}$$.
Available since 2.0

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}$$.
Available since 2.0

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.
Available since 2.0

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

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).
Available since 2.3

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).
Available since 2.3

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.
Available since 2.3

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.
Available since 2.3

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

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).
Available since 2.0

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.
Available since 2.0

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.
Available since 2.0

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.
Available since 2.0

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).
Available since 2.0

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).
Available since 2.0