title: Duplication and elimination matrices
text: # Duplication and elimination matrices
  
  In mathematics, especially in linear algebra and matrix theory, the duplication matrix and the elimination matrix are linear transformations used for transforming half-vectorizations of matrices into vectorizations or (respectively) vice versa.
  
  ## Duplication matrix
  
  The duplication matrix $D_{n}$ is the unique $n^{2}\times {\frac {n(n+1)}{2}}$ matrix which, for any $n\times n$ symmetric matrix $A$, transforms $\mathrm {vech} (A)$ into $\mathrm {vec} (A)$:
  
      $D_{n}\mathrm {vech} (A)=\mathrm {vec} (A)$.
  
  For the $2\times 2$ symmetric matrix $A=\left[{\begin{smallmatrix}a&b\\b&d\end{smallmatrix}}\right]$, this transformation reads
  
      $D_{n}\mathrm {vech} (A)=\mathrm {vec} (A)\implies {\begin{bmatrix}1&0&0\\0&1&0\\0&1&0\\0&0&1\end{bmatrix}}{\begin{bmatrix}a\\b\\d\end{bmatrix}}={\begin{bmatrix}a\\b\\b\\d\end{bmatrix}}$
  
  The explicit formula for calculating the duplication matrix for a $n\times n$ matrix is:
  
  $D_{n}^{T}=\sum \limits _{i\geq j}u_{ij}(\mathrm {vec} T_{ij})^{T}$
  
  Where:
  
  * $u_{ij}$ is a unit vector of order ${\frac {1}{2}}n(n+1)$ having the value $1$ in the position $(j-1)n+i-{\frac {1}{2}}j(j-1)$ and 0 elsewhere;
  * $T_{ij}$ is a $n\times n$ matrix with 1 in position $(i,j)$ and $(j,i)$ and zero elsewhere
  
  Here is a C++ function using Armadillo (C++ library):
  
  ## Elimination matrix
  
  An elimination matrix $L_{n}$ is a ${\frac {n(n+1)}{2}}\times n^{2}$ matrix which, for any $n\times n$ matrix $A$, transforms $\mathrm {vec} (A)$ into $\mathrm {vech} (A)$:
  
      $L_{n}\mathrm {vec} (A)=\mathrm {vech} (A)$.
  
  By the explicit (constructive) definition given by Magnus & Neudecker (1980), the ${\frac {1}{2}}n(n+1)$ by $n^{2}$ elimination matrix $L_{n}$ is given by
  
      $L_{n}=\sum _{i\geq j}u_{ij}\mathrm {vec} (E_{ij})^{T}=\sum _{i\geq j}(u_{ij}\otimes e_{j}^{T}\otimes e_{i}^{T}),$
  
  where $e_{i}$ is a unit vector whose $i$-th element is one and zeros elsewhere, and $E_{ij}=e_{i}e_{j}^{T}$.
  
  Here is a C++ function using Armadillo (C++ library):
  
  For the $2\times 2$ matrix $A=\left[{\begin{smallmatrix}a&b\\c&d\end{smallmatrix}}\right]$, one choice for this transformation is given by
  
      $L_{n}\mathrm {vec} (A)=\mathrm {vech} (A)\implies {\begin{bmatrix}1&0&0&0\\0&1&0&0\\0&0&0&1\end{bmatrix}}{\begin{bmatrix}a\\c\\b\\d\end{bmatrix}}={\begin{bmatrix}a\\c\\d\end{bmatrix}}$.
  