How do the following matrix product

circlesdraftmatricesxrightarrow

I am interesting about drafting the following in LaTeX. I am not so much expert in this, i am going to attach the code to generate the vector and matrix, and after that I am going to attach my desired output.

Code:

\begin{align}
  \begin{pmatrix}a_1\\a_2\\\vdots\\a_k\end{pmatrix}
  \begin{pmatrix}\varepsilon_1&0&\ldots&0\\ 0&\varepsilon_2&\ldots&0\\0&0&\ldots&0\\0&0&\ldots&\varepsilon_N\\ 
   \end{pmatrix}
\end{align}

Output:

enter image description here

So what I want is a circle on the first and last element of vector (a1,..,ak) and an arrow that goes from a1 to the rectangle of first row of matrix, the same for the last row. Sorry if the circle is made in that bad way, I have done it manually, of course is preferred a circle draft in a better way.

Best Answer

With {NiceArray} of nicematrix and Tikz.

\documentclass{article}
\usepackage{nicematrix,tikz}
\usetikzlibrary{fit}

\begin{document}

$\begin{NiceArray}{(c)(cccc)}[create-medium-nodes,nullify-dots]
a_1    & \epsilon_1 & 0          & \cdots & 0 \\
a_2    & 0          & \epsilon_2 & \cdots & 0 \\
\Vdots & 0          & 0          & \cdots & 0 \\
a_k    & 0          & 0          & \cdots & \epsilon_n \\
\CodeAfter
  \begin{tikzpicture} [cyan,inner sep=1pt]
    \node [circle,draw,fit=(1-1)] (A) {} ;
    \node [circle,draw,fit=(4-1)] (B) {} ;
    \node [draw,rounded corners, fit = (1-2-medium) (1-5-medium)] (C) {} ;
    \node [draw,rounded corners, fit = (4-2-medium) (4-5-medium)] (D) {} ;
    \draw [->] (A.north) to [bend left] (C.north west) ; 
    \draw [->] (B.south) to [bend right] (D.south west) ; 
  \end{tikzpicture}
\end{NiceArray}$

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code