<s>[INST] How to find an affine map for given points I am trying to understand affine maps. In the book I am using there is the following example, which I don't understand. Given are the following points:
\begin{array}{lll}
  p_0 = \begin{pmatrix} 1 \\ 1 \end{pmatrix},
  &
  p_1 = \begin{pmatrix} 1 \\ 2 \end{pmatrix},
  &
  p_2 = \begin{pmatrix} 0 \\ 1 \end{pmatrix},
  \\
  q_0 = \begin{pmatrix} -2 \\ -1 \end{pmatrix},
  &
  q_1 = \begin{pmatrix} \phantom{-}0 \\ -1 \end{pmatrix},
  &
  q_2 = \begin{pmatrix} -2 \\ -2 \end{pmatrix}.
\end{array}
Then the following vectors are computed
\begin{array}{ll}
    \overrightarrow{p_0 p_1}
  = \begin{pmatrix} 0 \\ 1 \end{pmatrix},
  &
    \overrightarrow{p_0 p_2}
  = \begin{pmatrix} -1 \\ \phantom{-}0 \end{pmatrix},
  \\
    \overrightarrow{p_0 p_1}
  = \begin{pmatrix} 2 \\ 0 \end{pmatrix},
  &
    \overrightarrow{p_0 p_1}
  = \begin{pmatrix} \phantom{-}0 \\ -1 \end{pmatrix}.
\end{array}
I am asked to find an affine map such that $f(p_{i})=q_{i}$.
It now says that the matrix of the map determined by $F(\overrightarrow{p_0 p_i})=\overrightarrow{q_0 q_i}$, $i=1,2$ is given by
$$
    A
  = \begin{pmatrix}
      0 & 2 \\
      1 & 0
    \end{pmatrix}.
$$
I don't understand how I get this matrix. I would say it is the following one, but I guess I am wrong. I did everything with repect to the bases $\{(0,1),(-1,0)\}$ and $\{(1,0),(0,1)\}$. I am not sure about the second basis, but since there is no other basis mentioned I thought it is the one the author has used too.
$$
  \begin{pmatrix}
    2 & \phantom{-}0 \\
    0 &           -1 \\
  \end{pmatrix}
$$
Unfortunately I don't know  where I making the mistake.
I would really appreciate some help. Thanks a lot in advance! 
 [/INST] There is a neat formula for your case
$$
\vec{f}(x; y) = (-1)
\frac{
    \det
        \begin{pmatrix}
            0   & \vec{q_0} & \vec{q_1} & \vec{q_2} \\
            x   & p_0^x     & p_1^x     & p_2^x     \\
            y   & p_0^y     & p_1^y     & p_2^y     \\
            1   & 1         & 1         & 1         \\
        \end{pmatrix}
}{
    \det
        \begin{pmatrix}
            p_0^x     & p_1^x     & p_2^x     \\
            p_0^y     & p_1^y     & p_2^y     \\
            1         & 1         & 1         \\
        \end{pmatrix}
},
$$
where $x$ and $y$ are coordinates of the point you are mapping.
Here's how it works (plugging in initial points)
$$
\vec{f}(x; y) = (-1)
\frac{
    \det
        \begin{pmatrix}
            0   & \vec{q_0} & \vec{q_1} & \vec{q_2} \\
            x   & 1         & 1         & 0         \\
            y   & 1         & 2         & 1         \\
            1   & 1         & 1         & 1         \\
        \end{pmatrix}
}{
    \det
        \begin{pmatrix}
            1         & 1         & 0         \\
            1         & 2         & 1         \\
            1         & 1         & 1         \\
        \end{pmatrix}
} =
(\vec{q}_0 - \vec{q}_2) x +
(\vec{q}_1 - \vec{q}_0) y +
(\vec{q}_0 - \vec{q}_1 + \vec{q}_2) =
$$
now I plug in the final points
$$
= \left[
    \begin{pmatrix} -2 \\ -1 \end{pmatrix} -
    \begin{pmatrix} -2 \\ -2 \end{pmatrix}
\right] x +
\left[
    \begin{pmatrix} 0 \\ -1 \end{pmatrix} -
    \begin{pmatrix} -2 \\ -1 \end{pmatrix}
\right] y +
\left[
    \begin{pmatrix} -2 \\ -1 \end{pmatrix} -
    \begin{pmatrix} 0 \\ -1 \end{pmatrix} +
    \begin{pmatrix} -2 \\ -2 \end{pmatrix}
\right]
$$
Simplification yields
$$
\vec{f}(x; y) =
\begin{pmatrix} 0 \\ 1 \end{pmatrix} x +
\begin{pmatrix} 2 \\ 0 \end{pmatrix} y +
\begin{pmatrix} -4  \\ -2 \end{pmatrix}
$$
Or you can write that in canonical form
$$
\vec{f}(x; y) =
\begin{pmatrix}
0  & 2   \\
1  & 0
\end{pmatrix}
\begin{pmatrix} x \\ y \end{pmatrix} +
\begin{pmatrix} -4 \\ -2 \end{pmatrix}.
$$
Now you can easily check that
$$
\vec{f}(1;1) = \begin{pmatrix} -2 \\ -1 \end{pmatrix};~
\vec{f}(1;2) = \begin{pmatrix} 0 \\ -1 \end{pmatrix};~
\vec{f}(0;1) = \begin{pmatrix} -2 \\ -2 \end{pmatrix}.
$$
For more details on how this all works you may check "Beginner's guide to mapping simplexes affinely", where authors of the equation I used elaborate on theory behind it. In the guide there is exactly the same 2D example solved as the one you are interested in.
The same authors recently published "Workbook on mapping simplexes affinely". There are many examples of different problems you can solve with this equation.
</s>