[Tex/LaTex] Replicate the Singular Value Decomposition figure in LaTeX

matrices

I have tried to replicate a portion of this matrix decomposition system in LaTeX, but I am unable to draw the first and the last matrices with rectangular boxes.enter image description here

This is what I could do until now, which of course, is easy to write. I can use LatexDraw to draw this figure, but the figure will not look very elegant as this one:

 \begin{equation}
\underbrace{\mathbf{A}}_{W \times D} = \underbrace{\mathbf{U}}_{W \times W} \times \underbrace{\mathbf{\Sigma}}_{W\times D} \times \underbrace{\mathbf{V}^{\text{T}}}_{D \times D} = 
\left(
 \begin{array}{ccccc}
   \sigma_1\\
    & . & & \text{\huge0}\\
    & & .\\
    & \text{\huge0} & & \sigma_r\\
    & & & & 0
 \end{array}
\right)
\end{equation}

Best Answer

Using TikZ and some matrix of math nodes:

Matrix factorization by SVD

The code:

\documentclass{article}
\usepackage[margin=2cm]{geometry}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning,decorations.pathreplacing}

\DeclareMathOperator{\Mcol}{col}
\DeclareMathOperator{\Mrow}{row}
\DeclareMathOperator{\Mnull}{null}

\begin{document}

\begin{align*}
A &= U\Sigma V^{T} \\
&=
\begin{tikzpicture}[
baseline,
mymat/.style={
  matrix of math nodes,
  ampersand replacement=\&,
  left delimiter=(,
  right delimiter=),
  nodes in empty cells,
  nodes={outer sep=-\pgflinewidth,text depth=0.5ex,text height=2ex,text width=1.2em}
}
]
\begin{scope}[every right delimiter/.style={xshift=-3ex}]
\matrix[mymat] (matu)
{
 \& \& \& \& \& \\
\& \& \& \& \& \\
\& \& \& \& \& \\
\& \& \& \& \& \\
\& \& \& \& \& \\
\& \& \& \& \& \\
};
\node 
  at ([shift={(3pt,-7pt)}]matu-3-2.west) 
  {$\cdots$};
\node 
  at ([shift={(3pt,-7pt)}]matu-3-5.west) 
  {$\cdots$};
\foreach \Columna/\Valor in {1/1,3/r,4/{r+1},6/m}
{
\draw 
  (matu-1-\Columna.north west)
    rectangle
  ([xshift=4pt]matu-6-\Columna.south west);
\node[above] 
  at ([xshift=2pt]matu-1-\Columna.north west) 
  {$u_{\Valor}$};
}
\draw[decorate,decoration={brace,mirror,raise=3pt}] 
  (matu-6-1.south west) -- 
   node[below=4pt] {$\Mcol(A)$}
  ([xshift=4pt]matu-6-3.south west);
\draw[decorate,decoration={brace,mirror,raise=3pt}] 
  (matu-6-4.south west) -- 
   node[below=4pt] {$\Mnull(A)$}
  ([xshift=4pt]matu-6-6.south west);
\end{scope}
\matrix[mymat,right=10pt of matu] (matsigma)
{
\sigma_{1} \& \& \& \& \& \\
\& \ddots \& \& \& \& \\
\& \& \sigma_{r} \& \& \& \\
\& \& \& 0 \& \& \\
\& \& \& \& \ddots \& \\
\& \& \& \& \& 0 \\
};
%\begin{scope}[every right delimiter/.style={xshift=-3ex}]
\matrix[mymat,right=25pt of matsigma] (matv)
{
 \& \& \& \& \& \\
\& \& \& \& \& \\
\& \& \& \& \& \\
\& \& \& \& \& \\
\& \& \& \& \& \\
\& \& \& \& \& \\
};
\foreach \Fila/\Valor in {1/1,3/r,4/{r+1},6/n}
{
\draw 
  ([yshift=-6pt]matv-\Fila-1.north west)
    rectangle
  ([yshift=-10pt]matv-\Fila-6.north east);
\node[right=12pt] 
  at ([yshift=-8pt]matv-\Fila-6.north east) 
  {$v^{T}_{\Valor}$};
}
\draw[decorate,decoration={brace,raise=37pt}] 
  ([yshift=-6pt]matv-1-6.north east) -- 
   node[right=38pt] {$\Mrow(A)$}
  ([yshift=-10pt]matv-3-6.north east);
\draw[decorate,decoration={brace,raise=37pt}] 
  ([yshift=-6pt]matv-4-6.north east) -- 
   node[right=38pt] {$\Mnull(A)$}
  ([yshift=-10pt]matv-6-6.north east);
\end{tikzpicture}
\end{align*}

\end{document}

The few missing elements are easy to add.

Related Question