[Question]
  [
Somehow, we don't yet have a challenge for finding the inverse of an arbitrarily-sized square matrix, despite having ones for [3x3](https://codegolf.stackexchange.com/questions/168828/find-the-inverse-of-a-3-by-3-matrix) and [4x4](https://codegolf.stackexchange.com/questions/45369/find-the-inverse-of-a-matrix), as well as a [more complex version](https://codegolf.stackexchange.com/q/200685/66833).
Your task is, given a square \$n\times n\$ non-singular matrix \$M\$, output the matrix \$M^{-1}\$ that satisfies
$$MM^{-1} = I\_n$$
There are a number of methods and formulae for calculating \$M^{-1}\$, but one of the most well known is
$$M^{-1} = \frac1{\det(M)}\text{ adj}(M)$$
where \$\det\$ represents the determinant and \$\newcommand{\adj}{\text{adj}}\adj\$ the adjugate
Some definitions:
* \$I\_n\$: The \$n\times n\$ [identity matrix](https://en.wikipedia.org/wiki/Identity_matrix) i.e. an \$n\times n\$ matrix where the leading diagonal consists entirely of \$1\$s and the rest \$0\$s
* Non-singular: the determinant of \$M\$ is guaranteed to be non-zero
* [Determinant](https://en.wikipedia.org/wiki/Determinant): a specific number that can be calculated for any given square matrix. Exact methods can be found in the Wikipedia article
* [Adjugate](https://en.wikipedia.org/wiki/Adjugate_matrix): Formally, the transpose of the cofactor matrix of \$M\$. Informally, this is a operation on \$M\$ which takes determinants of submatrices in a specific way to construct a related matrix. Again, exact details can be found in the linked article.
For sake of simplicity, you may assume:
* The elements of \$M\$ will all be integers within the native bounds of your language
* \$n\$, nor \$n^2\$, will never exceed the maximum value in your language, and will always be greater than or equal to \$1\$
* The elements of \$M^{-1}\$ will never exceed the maximum value in your language (or minimum for negative values)
* \$M\$ will never be singular
No builtins are banned and you may use whatever (valid) method you like for calculating \$M^{-1}\$. It is acceptable if your program fails for some inputs due to floating point issues, so long as the underlying algorithm or method works for arbitrary matrices.
This is, of course, entirely optional, but if your answer consists entirely of a builtin, consider including a non-builtin method, simply for the sake of general interest.
Standard [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") rules apply. This means you may input or output in any [convenient format](https://codegolf.meta.stackexchange.com/questions/2447/default-for-code-golf-input-output-methods), and that [standard loopholes](https://codegolf.meta.stackexchange.com/questions/1061/loopholes-that-are-forbidden-by-default) are forbidden. The shortest code in bytes wins.
[This](https://tio.run/##ZY5NDoIwEIXXzCm6o02ggRg3Jp4EWDSC2IROJ7VoOH2dCq6czfy872UebfHh8ZSSdeRDFMHg6B0cG66ONoDROnEVFqO0SGuUSoEzkU/dAHD3QQT/Zjmb50kyrS5QMKEN0YSj7AYFReZuflkd/qGZ7ep2@PF7CJ1bflq3TSXahr8ChXxgvBIMcoKyxx5LdQiLfWaVvkO1x9eLRbPM2uIrGxVXSucP) script will take an input \$n\$ and generate an \$n\times n\$ matrix with random integers between \$-10\$ and \$10\$, along with it's inverse. You can use this for test cases.
---
### Worked example
Lets take the \$3\times3\$ matrix \$M\$ as:
$$M = \left[\begin{matrix}
 4 & -3 & 0 \\
-4 & -7 & 6 \\
 5 & 7 & 6
\end{matrix}\right]$$
We'll use the above formula, \$M^{-1} = \frac{\adj(M)}{\det(M)}\$ for this example.
First, we'll calculate \$\det(M)\$ by expanding along the third column:
$$\begin{align}
\det(M) & = \left|\begin{matrix}
 4 & -3 & 0 \\
-4 & -7 & 6 \\
 5 & 7 & 6
\end{matrix}\right| \\
& = 0\left|\begin{matrix}
-4 & -7 \\
 5 & 7
\end{matrix}\right|
- 6\left|\begin{matrix}
 4 & -3 \\
 5 & 7
\end{matrix}\right|
+ 6\left|\begin{matrix}
 4 & -3 \\
-4 & -7
\end{matrix}\right| \\
& = 0 - 6(4\cdot7 - -3\cdot5) + 6(4\cdot-7 - -3\cdot-4) \\
& = -6(28 + 15) + 6(-28 - 12) \\
& = -6\cdot43 + 6\cdot-40 \\
& = -498 \\
\therefore det(M) & = -498
\end{align}$$
We then need to calculate \$\adj(M)\$. As \$\adj(\cdot)\$ of a matrix is the transpose of the cofactor matrix, this essentially boils down to calculating the cofactor matrix of \$M\$, \$C\_M\$:
$$\begin{align}
\adj(M) & = C\_M^T \\
& = \left[\begin{matrix}
\left|\begin{matrix}
-7 & 6 \\
 7 & 6
\end{matrix}\right| &
\left|\begin{matrix}
-4 & 6 \\
 5 & 6
\end{matrix}\right| &
\left|\begin{matrix}
-4 & -7 \\
 5 & 7
\end{matrix}\right| \\
\left|\begin{matrix}
-3 & 0 \\
 7 & 6
\end{matrix}\right| &
\left|\begin{matrix}
 4 & 0 \\
 5 & 6
\end{matrix}\right| &
\left|\begin{matrix}
 4 & -3 \\
 5 & 7
\end{matrix}\right| \\
\left|\begin{matrix}
-3 & 0 \\
-7 & 6
\end{matrix}\right| &
\left|\begin{matrix}
 4 & 0 \\
-4 & 6
\end{matrix}\right| &
\left|\begin{matrix}
 4 & -3 \\
-4 & -7
\end{matrix}\right|
\end{matrix}\right]^T \\
& = \left[\begin{matrix}
-84 & 54 & 7 \\
 18 & 24 & -43 \\
-18 & -24 & -40
\end{matrix}\right]^T \\
& =\left[\begin{matrix}
-84 & 18 & -18 \\
 54 & 24 & -24 \\
 7 & -43 & -40
\end{matrix}\right]
\end{align}$$
Finally, having calculated both \$\det(M)\$ and \$\adj(M)\$, we divide each element of \$\adj(M)\$ by \$\det(M)\$ to compute the final output, \$M^{-1}\$:
$$\begin{align}
M^{-1} & = \frac{\adj(M)}{\det(M)} \\
& = \left[\begin{matrix}
\frac{-84}{-498} & \frac{ 18}{-498} & \frac{-18}{-498} \\
\frac{ 54}{-498} & \frac{ 24}{-498} & \frac{-24}{-498} \\
\frac{ 7}{-498} & \frac{-43}{-498} & \frac{-40}{-498}
\end{matrix}\right] \\
& = \left[\begin{matrix}
\frac{ 14}{ 83} & \frac{-3}{ 83} & \frac{ 3}{ 83} \\
\frac{ -9}{ 83} & \frac{-4}{ 83} & \frac{ 4}{ 83} \\
\frac{ -7}{498} & \frac{43}{498} & \frac{20}{249}
\end{matrix}\right]
\end{align}$$
Alternatively, as decimals, \$M^{-1}\$ is
```
[[ 0.1686746987951807,   -0.03614457831325301, 0.03614457831325303],
 [-0.10843373493975902,  -0.04819277108433735, 0.04819277108433734]
 [-0.014056224899598388,  0.08634538152610442, 0.08032128514056225]]
```
  
[Answer]
# [Octave](https://www.gnu.org/software/octave/), 57 bytes
```
A=input('');V=A'/trace(A*A');for i=1:1e4V=2*V-V*A*V;end
V
```
[Try it online!](https://tio.run/##y08uSSxL/f/f0TYzr6C0RENdXdM6zNZRXb@kKDE5VcNRyxEokJZfpJBpa2hlmGoSZmukFaYbpuWoFWadmpfCFfY/M69Mw1Hzf1FiXoqGsY6xJgA "Octave – Try It Online")
This is not particularly well golfed, but I wanted to advertise an approach that could be useful for other non-builtin answers.
This uses the Hotelling-Bodewig scheme:
$$ V\_{i+1} = V\_i\left(2I - AV\_i\right)$$
Which iteratively computes the inverse of a non singular matrix. This is guaranteed to converge for \$\left\lVert I - AV\_0\right\rVert < 1\$ (under a suitable matrix norm). Choosing the \$V\_0\$ is difficult, but Soleymani, F. shows in ["A New Method For Solving Ill-Conditioned Linear Systems"](http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.882.7271&rep=rep1&type=pdf) that the inital guess \$V\_0 = \frac{A^T}{\text{tr}(AA^T)}\$ will always satisfy this condition, so the system is numerically stable.
What makes this a particularly attractive approach to other potential answers is that we don't require any builtin determinant or inverse functions. The most complex part is just matrix multiplication, since the transpose and trace are trivial to compute.
I have chosen `1e4` iterations here to make the runtime somewhat reasonable, although you could of course push it to `1e9` with no loss of byte count.
---
-10 thanks to [xnor](https://codegolf.stackexchange.com/users/20260/xnor) for noting we don't need to construct an identity matrix.
[Answer]
# [R](https://www.r-project.org/), 5 bytes
```
solve
```
[Try it online!](https://tio.run/##K/qfZvu/OD@nLPV/mkZuYklRZoVGsoaJjoKxjoKuqY4CkKVraKCjYKGjYAkmQYLGYI4lVMoQSJsDKSjTEqIGhIGiuiYQISMQpamjYApGIZqa/wE "R – Try It Online")
Nothing new here...
Basically, the code `solve(A, B)` solves \$AX = B\$, but when \$B\$ is not given, it is treated as identity matrix, thus giving us the inverse as the result.
[Answer]
# [APL (Dyalog Unicode)](https://www.dyalog.com/), 1 byte[SBCS](https://en.wikipedia.org/wiki/SBCS)
```
⌹
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT////1HPzv@P@qY@apugAGQqGCkYPerdYmigYKFwaD0Y/wcA "APL (Dyalog Unicode) – Try It Online")
The domino primitive is a very interesting APL "built-in". It already [featured in another 1-byte answer of my own](https://codegolf.stackexchange.com/a/205897/75323) where it was used to solve a least-squares problem. When applied to a square matrix, `⌹` tries to find the matrix inverse of its argument.
Many golfing languages will also have a built-in for this... But mind you, APL is **not** a golfing language, although it is terse enough to be very competitive and, in cases like this, win.
[Answer]
# [R](https://www.r-project.org/), ~~72~~ 61 bytes
```
function(A,V=t(A/sum(diag(A%*%t(A))))){for(i in 1:1e4)V=2*V-V%*%A%*%V;V}
```
[Try it online!](https://tio.run/##vY3dCoMwDIXv9xS5EVJJ2eoPmxu98B1G78XZUVALWsdg7Nm7Vn2GhSQn@XIgk9fS62VsnbEj1qSkw/o4LwM@TPPEOkmTAFiMj7YTGjAjiKvoCqZkliqugiO61E19vcahcZN5Y4sFQU7AS4IwcXEiuBBUa48wX5dqP4mg5yD7WG2eWIHyYkNZFEZQrnln7DDb/tX99aX/AQ "R – Try It Online")
Porting [Sisyphus' answer](https://codegolf.stackexchange.com/a/213913/67312) is not futile at all...and thanks to Sisyphus for -11 bytes.
Observes that \$Tr(AA^T)=\sum\limits\_{i,j}a\_{ij}^2\$.
# [R](https://www.r-project.org/), 94 bytes
```
function(M)outer(k<-1:dim(M),k,Vectorize(function(j,i)det(M[-i,-j,drop=F])*(-1)^(i+j)))/det(M)
```
[Try it online!](https://tio.run/##vY3BCsIwDIbvPkWPiaZo3YZO3NXbbuJFFGTroJtbpXYivnztuuEjCEn@P19@iHFV5qq@K6zSHeSoeysNNHsudqVqPaCGTrKw2qiPhF@wJoWltJCfuSJeU2n0IztccA5c4BXUokbEZUigq6C9WaPeUEBMLCLGE2LecbEitiWWhjnAKCzpdBJeN14mm46ZoT3l8YjWgyCxJNQRcfbU95f860v3BQ "R – Try It Online")
Thanks to [Robin Ryder](https://codegolf.stackexchange.com/users/86301/robin-ryder) for fixing a bug and making this actually work.
Calculates \$A^{-1}\$ using the adjugate/determinant method.
[Answer]
# [Python 2](https://docs.python.org/2/), 228 bytes
```
from random import*
a=input()
exec"""$:j,J=i,I;J+=[j==i $]
while~-all(I[i]$):shuffle(a)
$:
 j,J=i,I
 $:
	if j-i:I[:]=[y-I[j]*x/J[j]for x,y in zip(J,I)]
$:print[x/I[i]for x in I][len(a):]""".replace("$","for i,I in enumerate(a)")
```
[Try it online!](https://tio.run/##NU5BbsMgEDyXVyDkg53gJGraVKLiAfgLiANKQV4LY0Rt1e6hX3eXqL3szEgzO5O2uZ/i8777PI002/iBAGOa8nwgVkJMy1w3xK3uzhirxMA7CVy9d0epBymBVoZ89RDcT2tDqJUGUzXis1@8D662DakEoX8hQlE8gadDC0JpYaTeWqUHc1jPHYKfMl35RiHSb0h1x1VjMJ8yxFmv5/L7YSkGZXRwEQuEwV2n7FKwd1ezinFWTFhXbC4uo8t2LlNYs@9aX04X3l7xIDFcty9Fv@G5PfQrsn9lfgE "Python 2 – Try It Online")
Augment the matrix with the identity matrix, then apply Gauss–Jordan elimination. I don't know if this is the shortest approach, but it's the one I wanted to try golfing down.
I use `while not all(a[i][i]for i in r):shuffle(a)` to move zeros off the diagonal. This loop will definitely terminate, because if there is *no* permutation of the rows of \$A\$ that makes the diagonal free of zeros, then \$\det(A)=0\$, which we are guaranteed is not the case. This can be seen from the Leibniz formula for \$\det(A)\$:
$$\det(A) = \sum\_{\sigma \in S\_n} \text{sgn}(\sigma) \prod\_{i=1}^n a\_{\sigma(i),i}$$
“There is no permutation \$\sigma\$ of the rows that makes the diagonal free of zeros” can be equivalently rephrased as “\$\prod\_{i=1}^n a\_{\sigma(i),i}\$ is always 0, for all \$\sigma\$” which causes this whole formula to be 0.
[Answer]
# [APL (Dyalog Unicode)](https://www.dyalog.com/), ~~27~~ 23 [bytes](https://github.com/abrudz/SBCS)
-4 bytes thanks to [Adám](https://codegolf.stackexchange.com/users/43319/ad%C3%A1m)!
This implements the method [advertised by Sisyphus](https://codegolf.stackexchange.com/a/213913/64121).
```
⊢(⊢+⊢-⊢+.×+.×)⍣≡⍉÷,+.×,
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT////1HXIg0g1gZiXRCtd3g6CGs@6l38qHPho97Ow9t1QAI6/9MetU141Nv3qKv5Ue@aR71bDq03ftQ28VHf1OAgZyAZ4uEZ/D9NwRgIgZIKhgoGYIigQeBR71wFz5TUvJLMkkoF38SSoswKLoQeEwWgmUC1h9aDWOYKZgqmCiASrC0gv7ikWCG1IjG3ICcVAA "APL (Dyalog Unicode) – Try It Online")
A function that takes the matrix as the right argument.
`+.×` computes the dot product (sum of element-wise product) of the flattened matrix `,` and the flattened matrix `,`. This is sum of the squared matrix entries, which is equal to \$tr(AA^T)\$.  
`⍉÷` divides the transposed matrix by the trace.
`(⊢+⊢-⊢+.×+.×)⍣≡` is a function which is applied to the original matrix \$A\$ as a left argument (`⊢`) and \$A^T\div tr(AA^T)\$ as the right argument (`⍉÷,+.×,`):
`⊢+⊢-⊢+.×+.×` takes the current matrix \$V\$ on its right and the input matrix \$A\$ on its left and executes one iteration step:  
`+.×` is the inner product of `+` and `×`. Given two matrices, this calculates their product. In this case \$ A \times V \$.  
`⊢` is the right argument \$V\$, `⊢+.×` the product \$V \times (A \times V)\$.  
`⊢-` subtracts this from the right argument: \$V-V \times A \times V\$.  
`⊢+` adds this to the right argument: \$V+V-V \times A \times V\$.
`⍣≡` applies the function on its left until the result doesn't change. Because of the way [equality testing works](https://help.dyalog.com/18.0/Content/Language/System%20Functions/ct.htm) in Dyalog APL, this actually terminates.
[Answer]
# [Julia 1.0](http://julialang.org/), 3 bytes
```
inv
```
[Try it online!](https://tio.run/##yyrNyUw0rPifZvs/M6/sf0FRZl6JRppGtImCsYKuqYKJgq6hgbWChYIlEAP5xtZAliVIUMFQQdfcWgHCsATJ6ZpaK5gr6JqAuEZAIlZT8z8A "Julia 1.0 – Try It Online")
Yet another short built-in solution.
[Answer]
# JavaScript (ES6), 169 bytes
This computes \$M^{-1} = \dfrac{\operatorname{adj}(M)}{\det(M)}\$
```
M=>M.map((r,y)=>r.map((_,x)=>D(h(M,x).map(r=>h(r,y)))*(x+y&1?-1:1)/D(M)),h=(a,n)=>a.filter(_=>n--),D=M=>+M||M.reduce((s,[v],i)=>s+(i&1?-v:v)*D(h(M,i).map(r=>h(r,0))),0))
```
[Try it online!](https://tio.run/##VY7NasQgFEb3eQpXnetE7aTttDBgugndueoyHQbJT@OQaDBWMjDvnpoIhW7kO5fveu5VejlVVo2OalM3S8sXwXPBBjkCWHLDPLcRLmQOUEAHIqRtZnnebSWM9zCnt4fsnWanDD8WIDAmHQdJdFiSrFW9ayxceK4pxaTgQZKK@10w29Q/VQMwkdKfiQrtKQW1/uRPHu@jT/3zHYJvfZbW2EE6xNGAeI4qoyfTN6w33zDE/jqO5/s1eubMh5qbGrInzEZZfzppHWRHjNnVKA07tPuLXzrkJIkOaKFMECrRC6HPBB3OZCUa6I2g10joSNBGyTnc9gs "JavaScript (Node.js) – Try It Online")
[Answer]
# [J](http://jsoftware.com/), 2 bytes
```
%.
```
[Try it online!](https://tio.run/##y/qfmpyRb6Rga6UQbaUAYqvZKairq1n/V9X7/z83sQQkY6IQb6xgoKCjEA9kmSuYKehYKZgqABlcEM2qekCFcLZChZUCkA8A "J – Try It Online")
Same as APL, but more powerful, as J can produce exact rational matrix when given a matrix of extended integers as input.
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), ~~38~~ ~~22~~ ~~21~~ 20 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
˜nO/øтF©I2Føδ*O®}·s-
```
Port of [*@Sisyphus*' Octave answer](https://codegolf.stackexchange.com/a/213913/52210), so make sure to upvote him!!  
-16 bytes thanks to *@ovs*.
[**Try this online.**](https://tio.run/##yy9OTMpM/f//9Jw8f/3DOy42uR1a6WnkdnjHuS1a/ofW1R7aXqz7/390tImOrrGOQaxOtC6QZa5jBmSZ6oDo2P@6unn5ujmJVZUA)
**Code explanation:**
```
˜            # Flatten the (implicit) input-matrix to a single list
 n           # Square each value in this list
  O          # Take the sum (this is the trace of M*M')
   /         # Divide each value in the (implicit) input-matrix by this trace
    ø        # Zip/transpose this matrix; swapping rows/columns
тF           # Loop 100 times:
  ©          #  Store the current matrix in variable `®` (without popping)
   I         #  Push the input-matrix
    2F       #  Loop 2 times:
      ø      #   Zip/transpose the top matrix; swapping rows/columns
       δ     #   Apply double-vectorized with the top two matrices:
        *    #    Multiply
         O   #   Sum each inner row
          ®  #   Push the matrix from variable `®` again
     }·      #  After the inner loop: double all values in matrix `®`
       s     #  Swap so the calculated matrix VMV is at the top again
        -    #  Subtract this VMV from the 2V
             # (after the outer loop, the resulting matrix is output implicitly) 
```
---
## Original answer (38 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)) and detailed explanation:
```
εUεX*O]Å\OIøs/тFxs©εUIøεX*O}U®øεX*O}}-
```
[**Try it online.**](https://tio.run/##yy9OTMpM/f//3NbQc1sjtPxjD7fG@Hse3lGsf7HJraL40EqgBJALlqsNPbQOxqzV/f8/OtpER9dYxyBWJ1oXyDLXMQOyTHVAdOx/Xd28fN2cxKpKAA)
05AB1E has barely any useful builtins for matrices, not even matrix manipulation. So almost everything has to be done manually..
Since I'm an absolute noob in math, I'm gonna explain everything in full detail to help others like me who want to do this challenge without any builtins, and also to keep this answer self-contained.
**Step 1)** [Matrix manipulation](https://en.wikipedia.org/wiki/Matrix_multiplication#Definition) of the input-matrix \$M\$ with it's transpose: \$M\times M'\$:
If we have a matrix \$A\$ and \$B\$ and want to do matrix-manipulation \$AB\$, we take the [dot-product](https://en.wikipedia.org/wiki/Dot_product#Algebraic_definition) of every \$i^{th}\$ row of \$A\$ and \$j^{th}\$ column of B for every coordinate \$i,j\$ in the two matrices.
For example, if we use the matrix in the challenge description:
\$M = \left[\begin{matrix}
 4 & -3 & 0 \\
-4 & -7 & 6 \\
 5 & 7 & 6
\end{matrix}\right]\$
We can for example calculate the values in the top row of the resulting \$M\times M'\$ matrix with:
Top-left: \$4\times4+-3\times-3+0\times0 = 25\$  
Top-center: \$4\times-4+-3\times-7+0\times6=5\$  
Top-right: \$4\times5+-3\times7+0\times6 = -1\$
I've done matrix manipulation in 05AB1E before in [this answer of mine](https://codegolf.stackexchange.com/a/197011/52210), so I've used that code snippet here as well. Since we want to multiply the input-matrix by it's transpose, we actually won't need the transpose builtin here.
```
ε                  # Map over each row of the (implicit) input-matrix
 U                 #  Pop and store the current row in variable `X`
  ε                #  Map over each row of the (implicit) input-matrix again
   X*              #   Multiply the values of the current row by the values at the same
                   #   positions in row `X`
     O             #   And take the sum of this row
]                  # Close both maps
```
[Try just this step online.](https://tio.run/##yy9OTMpM/f//3NbQc1sjtPxj//@PjjbR0TXWMYjVidYFssx1zIAsUx0QHftfVzcvXzcnsaoSAA)
**Step 2)** Take the [trace](https://en.wikipedia.org/wiki/Trace_(linear_algebra)#Definition) of this new matrix: \$(M\times M')^T\$
The trace of a square matrix is basically the sum of its main diagonal (the values of the top-left to the bottom-right).
```
Å\                 # Take the main diagonal of the matrix of step 1
  O                # And sum the values in this list together
```
[Try the first two steps online.](https://tio.run/##yy9OTMpM/f//3NbQc1sjtPxjD7fG@P//Hx1toqNrrGMQqxOtC2SZ65gBWaY6IDr2v65uXr5uTmJVJQA)
**Step 3)** Divide all values in the transposed matrix by this trace we calculated:
```
I                  # Push the input-matrix
 ø                 # Zip/transpose it; swapping rows/columns
  s                # Swap so the trace we calculated it at the top of the stack
   /               # And divide each value in the transposed matrix by this trace
```
[Try the first three steps online.](https://tio.run/##yy9OTMpM/f//3NbQc1sjtPxjD7fG@Hse3lGs//9/dLSJjq6xjkGsTrQukGWuYwZkmeqA6Nj/urp5@bo5iVWVAA)
**Step 4)** Repeat the following steps (5 through 8) enough times for the answer to not change anymore:
Since this program isn't very fast in 05AB1E, I've decided to loop just 100 times, but this can be increased to improve the accuracy of the decimal results (I've verified with [*@Sisyphus*' Octave answer](https://codegolf.stackexchange.com/a/213913/52210) that changing the `1e4` to `1e2` still holds the same result for most matrices).
```
тF                 # Loop 100 times:
```
I'm not sure if the values will eventually not change anymore if we loop enough times. If this is the case we could (in theory) save a byte by changing this `тF` to `Δ` (loop until the result no longer changes).
(Let's call the intermediate matrix inside this loop \$V\$ for the explanations of the following steps.)
**Step 5)** Double each value in the current matrix: \$2V\$:
```
  x                #  Double each value in the current matrix V (without popping)
```
[Try the first five steps online, excluding the loop of step 4.](https://tio.run/##yy9OTMpM/f//3NbQc1sjtPxjD7fG@Hse3lGsX/H/f3S0iY6usY5BrE60LpBlrmMGZJnqgOjY/7q6efm6OYlVlQA)
**Step 6)** Do matrix manipulation again for \$VM\$ (where \$M\$ is the input-matrix):
```
   s               #  Swap to take the non-doubled matrix V at the top again
    ©              #  Store this matrix V in variable `®` (without popping)
     ε             #  Map over each row of matrix V:
      U            #   Pop the current row, and store it in variable `X`
       I           #   Push the input-matrix M
        ø          #   Zip/transpose; swapping rows/columns
         ε         #   Map over each row of this transposed matrix M':
          X*       #    Multiply the values in the current row by row `X`
            O      #    And take the sum
```
[Try the first six steps online, excluding the loop of step 4.](https://tio.run/##yy9OTMpM/f//3NbQc1sjtPxjD7fG@Hse3lGsX1F8aCVQFMgGS/z/Hx1toqNrrGMQqxOtC2SZ65gBWaY6IDr2v65uXr5uTmJVJQA)
**Step 7)** And do matrix manipulation yet again right after: \$VMV\$:
```
         }         #   Close the inner map
          U        #   Pop and store this as new `X`
           ®       #   Push the matrix V from variable `®`
            ø      #   Zip/transpose; swapping rows/columns
             ε     #   Map over each row of this transposed matrix V':
              X*   #    Multiply the values in the current row by row `X`
                O  #    And take the sum
     }}            #  Close both the inner and outer maps
```
[Try the first seven steps online, excluding the loop of step 4.](https://tio.run/##yy9OTMpM/f//3NbQc1sjtPxjD7fG@Hse3lGsX1F8aCVQFMgGS9SGHloHY9b@/x8dbaKja6xjEKsTrQtkmeuYAVmmOiA69r@ubl6@bk5iVSUA)
**Step 8)** Subtract the values at the same positions of these two matrices from one another: \$2V-VMV\$:
```
       -           #  Subtract matrix VMV from 2V
```
[Try the first eight steps online, excluding the loop of step 4.](https://tio.run/##yy9OTMpM/f//3NbQc1sjtPxjD7fG@Hse3lGsX1F8aCVQFMgGS9SGHloHY9bq/v8fHW2io2usYxCrE60LZJnrmAFZpjogOva/rm5evm5OYlUlAA)
And after the loop is done, the resulting matrix is output implicitly.
[Answer]
# [Python 2](https://docs.python.org/2/), 188 bytes
```
lambda a:[[c(a,j,i)/d(a)for j,_ in e(a)]for i,_ in e(a)]
c=lambda a,i,j:(-1)**(i+j)*d([b[:j]+b[j+1:]for I,b in e(a)if i-I])
d=lambda a:a==[]or sum(b[0]*c(a,i,0)for i,b in e(a))
e=enumerate
```
[Try it online!](https://tio.run/##TY5NDoIwEEb3nKLLFgYs/iZNegDPMDamhRKnETSIC0@PBQNx82Ve8r2ZeX6G26Pbjo2@jHfbutoyqxArbiEAiU3NrWgePQtwZdQxH9FMTH@cVHpRgSAonpciTTllQaQ1R4cqmMxhyEo1u2dwi0sNo/xsRFKvK5TVGk2svd4tdyhNOj1DIMXv7uqKxGvfvVvf28GPz566gTUccV9IyHcxZCENYD7zKcZx5kOcFjJi/AI "Python 2 – Try It Online")
The top lambda computes \$A^{-1} = \frac{1}{\det(A)}\text{adj}(A)\$.
`d(a)` computes the determinant and `c(a,i,j)` computes cofactors.
[Answer]
# [Scala](http://www.scala-lang.org/), ~~237~~ 232 bytes
Uses the method from [Sisyphus's answer](https://codegolf.stackexchange.com/a/213913/95792). Go upvote that!
```
m=>{val h=m.indices
Seq.iterate(m.transpose.map(_.map(_/m.flatten.map(x=>x*x).sum)),9999){v=>h.map(i=>h.map{j=>2*v(i)(j)-(h.map(k=>v(i).zip(m.transpose.apply(k))map(t=>t._1*t._2)sum),v.transpose.apply(j)).zipped.map(_*_).sum})}last}
```
[Try it online!](https://tio.run/##nZFbS8MwGIbv8yvCrvKVNpvzhGICgpd6tUsdI9syl65pY5OVaulvr2mqDNkQMZDT8x3zxq5EJrpimcqVw09C5VjWTuZri@@NwQ2qRIbVLZ7Jt@d@PhT7ZSbnc8z4CdZpxps@Yss0VflaraRF3oUqJ0vhJNHUlSK3prCSamHIYljHmm4y4XzdcK8Zr6MaqN1rgPjGD2gqxrfBqL4OTcr4NKqIApJCQgbjjvGe0A9lftQSxmTvZAfQOznGHV2cRX6ZQl8jro5cUwhJjFwPHUaL0E4LbSasa7v@kVo4zPCjso4gPOwXdBLj5DzGE4i/WTLA6xhfHeBlzwJCgJApVe6ynCjicwLVu5nz5JWMXvIReHuLxtEgbUmqeAvsHzom/PAlm6Ik9V0ywa7AQV5Usd/U/aO49EjdUDoEnJb4ZEDr22mjcfcJ "Scala – Try It Online")
`h` is just a range from 0 until n to reuse later (mostly because Scala doesn't have matrix multiplication builtins). The function makes a sequence of 9999 elements and takes the last element. The first element is the transpose of `m` divided by the trace of `m` times its transpose. Subsequent elements are calculated with `2*v-v*m*v`, where `v` was the previous element.
To calculate \$V\_0\$ (It turns out the trace of `m` times its transpose is just the sum of squares of all of `m`'s cells):
```
m.transpose.map(            //For every row in m's transpose
  _.map(                    //For every cell in that row
    _ /                     //Divide it by (trace(M * M's transpose))
      m.flatten             //Turn m into a 1D list
        .map(x=>x*x)        //Square each cell
        .sum))              //Add them up
```
To calculate subsequent elements, we use \$2V - (VA)V\$, but you have to map over `h` instead of over `v` itself:
```
h.map(i =>                 //For every i in [0, n)
  h.map{j =>               //For every j in [0, n)
    2*v(i)(j) -            //2V at these coordinates minus
    <(v * m * v)[i][j]> }) //v*m*v at these coordinates (see explanation below)
```
To calculate `(v*m)[i]`:
```
h.map(k =>                //k is the index of a row in [0, n)
  v(i).zip(               //Zip column i of v with
    m.transpose.apply(k)  //Row k of m (apply is used for indexing here)
  ) map(t=>t._1*t._2)     //Multiply v(i)(j) with m(k)(i)
  sum                     //Add then up
)
```
And getting the cross product of that with row `j` of `v` uses pretty much the same approach.
---
# Scala, ~~346~~ 342 bytes
Saved 4 bytes thanks to **@corvus\_192**!
```
type M=Seq[Seq[Double]]
def c(m:M)={val I=m.indices;I.map(i=>I.map(j=>m(i)(j)*math.pow(-1,i+j)))}
def d(m:M):(M,Double)=if(m.size<2)m->m(0)(0)else{val I=m.indices
val M=I.map(i=>I.map{j=>d(I.filter(i!=_)map(k=>I.filter(j!=_)map(m(k))))._2})
c(M)->c(m).head.zip(M.head).map(t=>t._1*t._2).sum}
def i(m:M)=d(m)._1.transpose.map(_.map(_/d(m)._2))
```
[Try it in Scastie!](https://scastie.scala-lang.org/RihOhcl4QLuy8JaqEEzpGg)
As you can see, I'm not very good at math.
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 7 bytes
```
Inverse
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n2773zOvLLWoOPV/QFFmXomCg59DenR1ta65joKuERADaSBlDGRZ1uooVEPYlmBBXRMgNjQACeuagZhAbApSqKNgAFQDFjcFKwaSJmAtpiBBA4gakHpDiA26RmDFJhAJkADIPAsQrq3liv3/HwA "Wolfram Language (Mathematica) – Try It Online")
[Answer]
# [Jelly](https://github.com/DennisMitchell/jellylanguage/wiki), 3 [bytes](https://github.com/DennisMitchell/jellylanguage/wiki/Code-page)
```
æ*-
```
[Try it online.](https://tio.run/##y0rNyan8///wMi3d////R0eb6Oga6xjE6kTrAlnmOmZAlqkOiI4FAA)
**Explanation:**
```
     # Full program taking a single integer-matrix as argument
æ*   #  Matrix exponentiation
  -  #  with -1
     #  (after which the result is output implicitly)
```
[Answer]
# Excel, 29 bytes
```
=MINVERSE(OFFSET(A2,,,A1,A1))
```
Straightforward application of the [MINVERSE()](https://support.microsoft.com/en-us/office/minverse-function-11f55086-adde-4c9f-8eb9-59da2d72efc6) function. It's boring but I got excited about Excel having a built-in for something. Input \$n\$ in `A1`, the matrix starting in `A2`, and the formula anywhere the spill won't interfere.
[![Screenshot](https://i.stack.imgur.com/qFsgT.png)](https://i.stack.imgur.com/qFsgT.png)
[Answer]
# [Matlab](https://au.mathworks.com/products/matlab.html) ~~6~~ 3 bytes
```
inv
```
Computes and prints the inverse of a square matrix. Pretty boring in-built solution. Thanks to @Bubbler for the clarification and -3 bytes.
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 48 bytes
```
≔Ｅθ∕Ｅθ§λκΣＥＸθ²ΣληＦφＵＭηＥκ⁻⊗μΣＥθ×ΣＥθ×§κς§ρπ§§ηπνＩη
```
[Try it online!](https://tio.run/##ZY67bsMwDEV3fwVHCqABJ30NngxnyRAgQLsZHtRYjQTrkUi2279XpaRGC5QLeS5f9yS5PzmuY2xCUGeLB37BK8FOLWoQKzXT3g7iCzXByBjB62xuraP7FD4PbH9EzXIQSFYXH84DbqqqYpBmW2cMtwNKyoRjSsrOAXduftdiQPPnbDr4powI@E9YjaT1hf368gSX@@NVWbPMHQLL7lEXR6/shC0PE8rEMXZd90hQPhBUPUFXZngheM7wRHAr@z6Wi/4G "Charcoal – Try It Online") Link is to verbose version of code. Explanation: Another port of @Sisyphus's answer.
```
≔Ｅθ∕Ｅθ§λκΣＥＸθ²Σλη
```
Transpose the input and divide it by the sum of squares of all the elements. Sadly neither sum nor divide fully vectorise, so I have to divide a row at a time and calculate the sum via a nested loop.
```
Ｆφ
```
Repeat 1000 times, which should be enough for floating-point precision.
```
ＵＭηＥκ⁻⊗μΣＥθ×ΣＥθ×§κς§ρπ§§ηπν
```
Calculate the matrix multiplication and subtraction in-place. Charcoal doesn't have any vector or matrix operations, so we have to loop over the rows and columns manually, but there are a couple of places where we can share variables which saves us a couple of bytes each.
```
Ｉη
```
Output the array. (Note that each element is output on its own line and each row is double-spaced from the previous.)
[Answer]
# [MATL](https://github.com/lmendo/MATL), 4 bytes
```
-1Y^
```
[Try it online!](https://tio.run/##y00syfn/X9cwMu7//2gTBV1jBQNrBV0gw1zBzFrBVAFIxQIA "MATL – Try It Online")
## Explanation
```
-1Y^
-1   : Push -1 onto the stack
  Y^ : Raise implicit input to -1 power
```
[Answer]
# PARI/GP 8 bytes
```
f(A)=1/A
```
Built-in function; particularly efficient for matrices with integer matrix elements.
Most likely, the method used is similar to the one described in this refererence: H. Haramotu, M. Matsumotu, [A p-adic algorithm for computing the inverse of integer matrices](https://doi.org/10.1016/j.cam.2008.07.044), Journal of Computational and Applied Mathematics, Volume 225, Issue 1, 1 March 2009, Pages 320-322.
[Answer]
# [SageMath](https://www.sagemath.org/), ~~14~~ ~~13~~ 11 bytes
Saved a byte thanks to [FryAmTheEggman](https://codegolf.stackexchange.com/users/31625/fryamtheeggman)!!!  
Saved 2 bytes thanks to [Sisyphus](https://codegolf.stackexchange.com/users/48931/sisyphus)!!!
```
lambda M:~M
```
[Try it online!](https://sagecell.sagemath.org/?z=eJxLs43hyknMTUpJVPC1qvPl4ipJLS5RsFXITSwpyqzQiI420dE10TGN1YnWNdbRNdcxB7IMdMx0zGJjNbkKijLzSjRAOoBs2zQYCyxaoAkAz70bIw==&lang=sage&interacts=eJyLjgUAARUAuQ==)
Inputs any square `matrix` and returns its inverse.
[Answer]
# [Ruby](https://www.ruby-lang.org/) `-rmatrix`, ~~23~~ 19 bytes
```
->a{Matrix[*a].inv}
```
[Try it online!](https://tio.run/##KypNqvyfZvtf1y6x2jexpCizIlorMVYvM6@s9n@BQlp0dLSCiY6usY6CQaxOtC6Qaa6jYAZkKpjqKICZsbH//@UXlGTm5xX/1y3KBRsBAA "Ruby – Try It Online")
Returns the result as a Ruby matrix object.
-4 bytes from Dingus.
[Answer]
# [q](https://code.kx.com/), 3 bytes
```
inv
```
# k, 1 byte
```
!
```
Does exactly as requested. Input must be a float matrix
[Answer]
# [Fortran (GFortran)](https://gcc.gnu.org/fortran/), 350 ~~376~~ bytes
```
real,allocatable,dimension(:,:)::A,B,U;read*,n;allocate(A(n,n));B=A;U=A;read*,((A(i,j),j=1,n),i=1,n)
do i=1,n;do j=1,n;U(i,j)=merge(1,0,i==j);B(i,j)=merge(1.,1/A(i,j),A(i,j)==0);enddo;enddo
do 4 l=1,99;do 4 i=1,n;do 4 j=1,n;s=U(i,j);do k=1,n;m=i;if(k/=m)s=s-A(i,k)*B(k,j);enddo;B(m,j)=s/A(i,m)
4 continue;print'(3(x,f9.6))',((B(i,j),j=1,n),i=1,n)
end
```
[Try it Online!](https://tio.run/##bU/RboQgEHznK3g7uKx3Gu01SnjQf/AD6IkXVKARm/Tv7QqmSZM@wExmJzO7o1@3VbnsNSay76tWC6hl8U@1qY9Fw2CsdsF4xxpoeNO00EEv0DZcwYnTqVnLHDjORSdb0eNLBoa6gYnDJAscg4lABk8jE0jiRPTRJq1eX5oVkKNTTpj2R75BcT/zEkiZc6HdMPj0H8EVXTCxrkXkvzXVWRRkqjq0OSpWGmFGNt@l5UGG7Iie@bVj82FL6R2zR12I9ZaTij6924z70uJzNW67sJJ9w1jfHpxf8Oruv6sxat9LgqtkJc1JhvhOH4S@UYr4Aw)
  ~~[376 bytes](https://tio.run/##bVBBboQwELvnFbltsgqFFXQriOZA/sAD0iVUgZBUQKX@nk4C6vbQy4xlW/ZohrBsi/bZx3CAfV@MdkI7Fx560@/OiN7Oxq82eNaIhjdNK5ToJNr6q/DkdBrWMi8851JBKztoyWFgyFsxcjHCDWVh0yJ9oAlJBEkhdmAWYORdssMtEfmTKCJxZOVQcHXacp3AU4Q/IjG@74NMM1ZW1GFXXcuEfw@ojhPkCkdXtE6JmcFKTJ5ymPkKaxYrJn5VbELbmavYHMvWPIozJxV9BL9Z/2XI52L9dmEl@xZD/XLn/IL/UP/9A6P2vSR4SlbSgmS43@id0FdKcf8A "Fortran (GFortran) – Try It Online")~~
Adapted from [here](http://www.uprh.edu/rbaretti/MatrixInv17mar2014.htm). Saved 26 bytes using `merge` instead of `if`.
]