# Vectorization (mathematics)

In mathematics, especially in linear algebra and matrix theory, the vectorization of a matrix is a linear transformation which converts the matrix into a column vector. Specifically, the vectorization of an m × n matrix A, denoted vec(A), is the mn × 1 column vector obtained by stacking the columns of the matrix A on top of one another:

$\mathrm {vec} (A)=[a_{1,1},\ldots ,a_{m,1},a_{1,2},\ldots ,a_{m,2},\ldots ,a_{1,n},\ldots ,a_{m,n}]^{\mathrm {T} }$ Here, $a_{i,j}$ represents $A(i,j)$ and the superscript ${}^{\mathrm {T} }$ denotes the transpose. Vectorization expresses, through coordinates, the isomorphism $\mathbf {R} ^{m\times n}:=\mathbf {R} ^{m}\otimes \mathbf {R} ^{n}\cong \mathbf {R} ^{mn}$ between these (i.e., of matrices and vectors) as vector spaces.

For example, for the 2×2 matrix $A$ = ${\begin{bmatrix}a&b\\c&d\end{bmatrix}}$ , the vectorization is $\mathrm {vec} (A)={\begin{bmatrix}a\\c\\b\\d\end{bmatrix}}$ .

## Compatibility with Kronecker products

The vectorization is frequently used together with the Kronecker product to express matrix multiplication as a linear transformation on matrices. In particular,

${\text{vec}}(ABC)=(C^{\mathrm {T} }\otimes A){\text{vec}}(B)$ for matrices A, B, and C of dimensions k×l, l×m, and m×n. For example, if ${\text{ad}}_{A}(X)=AX-XA$ (the adjoint endomorphism of the Lie algebra gl(n, C) of all n×n matrices with complex entries), then ${\text{vec}}({\text{ad}}_{A}(X))=(I_{n}\otimes A-A^{\mathrm {T} }\otimes I_{n}){\text{vec}}(X)$ , where $I_{n}$ is the n×n identity matrix.

There are two other useful formulations:

${\text{vec}}(ABC)=(I_{n}\otimes AB){\text{vec}}(C)=(C^{\mathrm {T} }B^{\mathrm {T} }\otimes I_{k}){\text{vec}}(A)$ ${\text{vec}}(AB)=(I_{m}\otimes A){\text{vec}}(B)=(B^{\mathrm {T} }\otimes I_{k}){\text{vec}}(A)$ More generally, it has been shown that vectorization is a self-adjunction in the monoidal closed structure of any category of matrices.

## Compatibility with Hadamard products

Vectorization is an algebra homomorphism from the space of n × n matrices with the Hadamard (entrywise) product to Cn2 with its Hadamard product:

$\mathrm {vec} (A\circ B)=\mathrm {vec} (A)\circ \mathrm {vec} (B).$ ## Compatibility with inner products

Vectorization is a unitary transformation from the space of n×n matrices with the Frobenius (or Hilbert–Schmidt) inner product to Cn2 :

tr(AH B) = vec(A)H vec(B)

where the superscript H denotes the conjugate transpose.

## Vectorization as a linear sum

The matrix vectorization operation can be written in terms of a linear sum. Let X be an m × n that we want to vectorize, and let ei be the i-th canonical basis vector for the n-dimensional space, that is ${\textstyle \mathbf {e} _{i}=\left[0,...,0,1,0,...,0\right]^{T}}$ . Let Bi be a (mn) × m block matrix defined as follows:

$\mathbf {B} _{i}={\begin{bmatrix}\mathbf {0} \\\vdots \\\mathbf {0} \\\mathbf {I} _{m}\\\mathbf {0} \\\vdots \\\mathbf {0} \end{bmatrix}}=e_{i}\otimes \mathbf {I} _{m}$ Bi consists of n block matrices of size m × m, stacked column-wise, and all these matrices are all-zero except for the i-th one, which is a m × m identity matrix Im.

Then the vectorized version of X can be expressed as follows:

${\text{vec}}(\mathbf {X} )=\sum _{i=1}^{n}\mathbf {B} _{i}\mathbf {X} \mathbf {e} _{i}$ Multiplication of X by ei extracts the i-th column, while multiplication by Bi puts it into the desired position in the final vector.

Alternatively, the linear sum can be expressed using the Kronecker product:

${\text{vec}}(\mathbf {X} )=\sum _{i=1}^{n}\mathbf {e} _{i}\otimes \mathbf {X} \mathbf {e} _{i}$ ## Half-vectorization

For a symmetric matrix A, the vector vec(A) contains more information than is strictly necessary, since the matrix is completely determined by the symmetry together with the lower triangular portion, that is, the n(n + 1)/2 entries on and below the main diagonal. For such matrices, the half-vectorization is sometimes more useful than the vectorization. The half-vectorization, vech(A), of a symmetric n × n matrix A is the n(n + 1)/2 × 1 column vector obtained by vectorizing only the lower triangular part of A:

vech(A) = [ A1,1, ..., An,1, A2,2, ..., An,2, ..., An−1,n−1,An,n−1, An,n ]T.

For example, for the 2×2 matrix A = ${\begin{bmatrix}a&b\\b&d\end{bmatrix}}$ , the half-vectorization is vech(A) = ${\begin{bmatrix}a\\b\\d\end{bmatrix}}$ .

There exist unique matrices transforming the half-vectorization of a matrix to its vectorization and vice versa called, respectively, the duplication matrix and the elimination matrix.

## Programming language

Programming languages that implement matrices may have easy means for vectorization. In Matlab/GNU Octave a matrix A can be vectorized by A(:). GNU Octave also allows vectorization and half-vectorization with vec(A) and vech(A) respectively. Julia has the vec(A) function as well. In Python NumPy arrays implement the 'flatten' method, while in R the desired effect can be achieved via the c() or as.vector() functions. In R, function vec() of package 'ks' allows vectorization and function vech() implemented in both packages 'ks' and 'sn' allows half-vectorization.