Essays/Matrix Inverse

From J Wiki
Jump to navigation Jump to search

The inverse of a square matrix can be computed as follows:

minors =: 1 |:\."2^:2 ]
det    =: -/ .*
adjoint=: [: |: */~@($&1 _1)@# * det@minors
inverse=: adjoint % det

minors computes the minors of a matrix and is from the \. page of the dictionary. det computes the determinant. adjoint is the classical adjoint.

   x=: 5 5 ?.@$ 10x
   y=: inverse x
   x +/ .* y
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1

   x
6 5 9 2 4
9 0 7 0 4
6 8 3 8 1
2 8 0 0 2
1 6 0 4 4
   y
  _9r64   39r256   17r256    35r512  _1r16
 31r768 _59r1024  41r3072  249r2048  _3r64
 37r192  _25r256  _13r768   _29r512  _1r16
_7r1536 _61r2048 511r6144 _593r4096 11r128
  _1r48     5r64  _23r192    _7r128    1r4

<!> Note: The above formulation is inefficient and is mainly of theoretical interest. Matrix inverse is computed much more efficiently using %. .



See also


Contributed by Roger Hui.