Find the inverse of a 3 by 3 matrix Challenge

Given nine numbers, a, b, c, d, e, f, g, h, i, as input which correspond to the square matrix:

$$\mathbf{M} = \begin{pmatrix}a&amp; b&amp; c\\ d&amp; e&amp; f\\ g&amp; h&amp; i\end{pmatrix}$$

Find the inverse of the matrix, \$\mathbf{M}^{-1}\$ and output its components.

Inverse Matrix

The inverse of a matrix 3 by 3 obeys the following equation:

$$\mathbf{MM}^{-1} = \mathbf{M}^{-1}\mathbf{M} = \mathbf{I} = \begin{pmatrix}1&amp;0&amp;0\\0&amp;1&amp;0\\0&amp;0&amp;1\end{pmatrix}$$

And can be calculated as:

$$\mathbf{M}^{-1} = \frac{1}{\det(\mathbf{M})}\mathbf{C}^T$$

Where \$\mathbf{C}\$ is the matrix of cofactors:

$$\mathbf{C}=\begin{pmatrix}ei-fh&amp;fg-di&amp;dh-eg\\ch-bi&amp;ai-cg&amp;bg-ah\\bf-ce&amp;cd-af&amp;ae-bd\end{pmatrix}$$

And \$\mathbf{C}^T\$ is the transpose of \$\mathbf{C}\$:

$$\mathbf{C}^T = \begin{pmatrix}ei-fh&amp;ch-bi&amp;bf-ce\\fg-di&amp;ai-cg&amp;cd-af\\dh-eg&amp;bg-ah&amp;ae-bd\end{pmatrix}$$

And \$\det(\mathbf{M})\$ is the determinant of \$\mathbf{M}\$:

$$\det(\mathbf{M}) = a(ei-fh)-b(di-fg)+c(dh-eg)$$

Worked Example

For example, let's say the input is 0, -3, -2, 1, -4, -2, -3, 4, 1. This corresponds to the matrix:

$$\mathbf{M} = \begin{pmatrix}0&amp;-3&amp;-2\\1&amp;-4&amp;-2\\-3&amp;4&amp;1\end{pmatrix}$$

Firstly, let's calculate what's known as the determinant using the formula above:

$$\det(\mathbf{M}) = 0(-4\times1-(-2)\times4) - (-3)(1\times1-(-2)\times-3) + (-2)(1\times4-(-4)\times-3) = 1$$

Next let's calculate the matrix of cofactors:

$$\mathbf{C} = \begin{pmatrix}-4\times1-(-2)\times4&amp; -(1\times1-(-2)\times-3)&amp;1\times4-(-4)\times-3\\-(-3\times1-(-2)\times4)&amp;0\times1-(-2)\times-3&amp;-(0\times4-(-3)\times-3)\\-3\times-2-(-2)\times-4&amp;-(0\times-2-(-2)\times1)&amp;0\times-4-(-3)\times1\end{pmatrix}$$

$$= \begin{pmatrix}4&amp;5&amp;-8\\-5&amp;-6&amp;9\\-2&amp;-2&amp;3\end{pmatrix}$$

We then need to transpose \$\mathbf{C}\$ (flip the rows and columns) to get \$\mathbf{C}^T\$:

$$\mathbf{C}^T = \begin{pmatrix}4&amp;-5&amp;2\\5&amp;-6&amp;-2\\-8&amp;9&amp;3\end{pmatrix}$$

Finally, we can find the inverse as:

$$\mathbf{M}^{-1} = \frac{1}{\det(\mathbf{M})}\mathbf{C}^T = \frac{1}{1}\begin{pmatrix}4&amp;-5&amp;2\\5&amp;-6&amp;-2\\-8&amp;9&amp;3\end{pmatrix}=\begin{pmatrix}4&amp;-5&amp;2\\5&amp;-6&amp;-2\\-8&amp;9&amp;3\end{pmatrix}$$

So the output would be 4, -5, -2, 5, -6, -2, -8, 9, 3.

Rules


The given matrix will always have an inverse (i.e. non-singular). The matrix may be self-inverse
The given matrix will always be a 3 by 3 matrix with 9 integers
The numbers in the input will always be integers in the range \$-1000 \leq n \leq 1000\$
Non-integer components of the matrix may be given as a decimal or a fraction


Examples

Input &gt; Output
1, 0, 0, 0, 1, 0, 0, 0, 1 &gt; 1, 0, 0, 0, 1, 0, 0, 0, 1
0, -3, -2, 1, -4, -2, -3, 4, 1 &gt; 4, -5, -2, 5, -6, -2, -8, 9, 3
1, 2, 3, 3, 1, 2, 2, 1, 3 &gt; -1/6, 1/2, -1/6, 5/6, 1/2, -7/6, -1/6, -1/2, 5/6
7, 9, 4, 2, 7, 9, 3, 4, 5 &gt; -1/94, -29/94, 53/94, 17/94, 23/94, -55/94, -13/94, -1/94, 31/94


Winning

The shortest code in bytes wins.
MATL, 54 bytes

th3LZ)t,3:q&amp;XdpswP]w-lw/GtY*tXdsGXdsUw-IXy*2/+GtXds*-*


Try it online!

Just to keep it interesting, doesn't use the inbuilt matrix division or determinant functions to do it. 

Instead, computes the determinant using the Rule of Sarrus.



And the adjugate (transposed cofactor matrix) using Cayley–Hamilton formula. 

$$ {\displaystyle \operatorname {adj} (\mathbf {A} )={\frac {1}{2}}\left((\operatorname {tr} \mathbf {A} )^{2}-\operatorname {tr} \mathbf {A} ^{2}\right)\mathbf {I} _{3}-\mathbf {A} \operatorname {tr} \mathbf {A} +\mathbf {A} ^{2}.} $$

Commented code:

% Finding determinant
th    % concatenate the matrix to itself sideways
3LZ)  % chop off the last column (since the Rule of Sarrus doesn't need it)
t     % duplicate this matrix (say S)
,     % do this twice:
  3:q&amp;Xd  % get the first three diagonals of S
  ps      % multiply each diagonal's values and add the results
  wP      % switch and flip the matrix (to get the popposing diagonals next time)
]w    % close loop, switch to have correct order of sums
-     % subtract - we now have the determinant
lw/   % invert that

% Finding adjugate using Cayley–Hamilton formula
GtY*  % A^2 term (last term of the formula)
tXds  % trace(A^2) for term 1 of formula
GXdsU % (trace(A))^2 for term1 of formula
w-    % (trace(A))^2 - trace(A^2)
IXy*  % multiply that by the identity matrix
2/    % divide that by 2 - term 1 complete
+
GtXds* % A*trA for term 2 of formula
-      % subtract to get adj(A)

*      % multiply by the inverse of determinant we found earlier
       % implicit output


We could go even more insane purer by replacing the matrix multiplication GtY* done for \$ A^2 \$, with something like 3:"Gt!@qYS*!s] 3$v t&amp;v 3:K-&amp;Xd (Try it on MATL Online).

The more direct and obvious way:

4 bytes

-1Y^


Try it online!

(-1 byte thanks to @Luis Mendo.)

-1 - Push the literal -1

Y^ - Raise input to that power (implicit input, implicit output)