Function diagonal

diagonal(v) takes a vector $v$ of length $n$, and returns the $n \times n$ diagonal matrix $$D = \left[ \begin{array}{cccc} v_1 & 0 & \cdots & 0 \ 0 & v_2 & \cdots & 0 \ \vdots & \vdots & \ddots & \vdots \ 0 & 0 & \cdots & v_n \end{array} \right].$$