docs: def inv(matrix):
      '''
      5 has way too many multiplies.
      >> from sympy import *
      >> from sympy.abc import *
      >> Matrix([a]).inv()
      Matrix([[1/a]])
      
      >> cse(Matrix([[a, b], [c, d]]).inv())
      Matrix([
      [1/a + b*c/(a**2*(d - b*c/a)), -b/(a*(d - b*c/a))],
      [          -c/(a*(d - b*c/a)),      1/(d - b*c/a)]])
      
      >> m_3 = Matrix([[a, b, c], [d, e, f], [g, h, i]])
      >> #cse(m_3.inv())
  
      >> m_4 = Matrix([[a, b, c, d], [e, f, g, h], [i, j, k, l], [m, n, o, p]])
      >> cse(m_4.inv())
      
      # Note: for 3, 4 - forgot to generate code using optimizations='basic'
      '''
      size = len(matrix)
      if size == 1:
          return [1.0/matrix[0]]
      elif size == 2:
          (a, b), (c, d) = matrix
          x0 = 1.0/a
          x1 = b*x0
          x2 = 1.0/(d - c*x1)
          x3 = c*x2
          return [[x0 + b*x3*x0*x0, -x1*x2],
                  [-x0*x3, x2]]
      elif size == 3:
          (a, b, c), (d, e, f), (g, h, i) = matrix
          x0 = 1./a
          x1 = b*d
          x2 = e - x0*x1
          x3 = 1./x2
          x4 = b*g
          x5 = h - x0*x4
          x6 = x0*x3
          x7 = d*x6
          x8 = -g*x0 + x5*x7
          x9 = c*d
          x10 = f - x0*x9
          x11 = b*x6
          x12 = c*x0 - x10*x11
          x13 = a*e
          x14 = -x1 + x13
          x15 = 1./(-a*f*h - c*e*g + f*x4 + h*x9 - i*x1 + i*x13)
          x16 = x14*x15
          x17 = x12*x16
          x18 = x14*x15*x3
          x19 = x18*x5
          x20 = x10*x18
          return [[x0 - x17*x8 + x1*x3*x0*x0, -x11 + x12*x19, -x17],
                  [-x20*x8 - x7, x10*x16*x5*x2**-2 + x3, -x20],
                  [ x16*x8, -x19, x16]]
      elif size == 4:
          (a, b, c, d), (e, f, g, h), (i, j, k, l), (m, n, o, p) = matrix
          x0 = 1./a
          x1 = b*e
          x2 = f - x0*x1
          x3 = 1./x2
          x4 = i*x0
          x5 = -b*x4 + j
          x6 = x0*x3
          x7 = e*x6
          x8 = -x4 + x5*x7
          x9 = c*x0
          x10 = -e*x9 + g
          x11 = b*x6
          x12 = -x10*x11 + x9
          x13 = a*f
          x14 = -x1 + x13
          x15 = k*x13
          x16 = b*g*i
          x17 = c*e*j
          x18 = a*g*j
          x19 = k*x1
          x20 = c*f*i
          x21 = x15 + x16 + x17 - x18 - x19 - x20
          x22 = 1/x21
          x23 = x14*x22
          x24 = x12*x23
          x25 = m*x0
          x26 = -b*x25 + n
          x27 = x26*x3
          x28 = -m*x9 + o - x10*x27
          x29 = x23*x8
          x30 = -x25 + x26*x7 - x28*x29
          x31 = d*x0
          x32 = -e*x31 + h
          x33 = x3*x32
          x34 = -i*x31 + l - x33*x5
          x35 = -x11*x32 - x24*x34 + x31
          x36 = a*n
          x37 = g*l
          x38 = h*o
          x39 = l*o
          x40 = b*m
          x41 = h*k
          x42 = c*l
          x43 = f*m
          x44 = c*h
          x45 = i*n
          x46 = d*k
          x47 = e*n
          x48 = d*o
          x49 = d*g
          x50 = j*m
          x51 = 1.0/(a*j*x38 - b*i*x38 - e*j*x48 + f*i*x48 + p*x15 
                   + p*x16 + p*x17 - p*x18 - p*x19 - p*x20 + x1*x39 
                   - x13*x39 + x36*x37 - x36*x41 - x37*x40 + x40*x41
                   + x42*x43 - x42*x47 - x43*x46 + x44*x45 - x44*x50 
                   - x45*x49 + x46*x47 + x49*x50)
          x52 = x21*x51
          x53 = x35*x52
          x54 = x14*x22*x3
          x55 = x5*x54
          x56 = -x27 + x28*x55
          x57 = x52*x56
          x58 = x14*x51
          x59 = x28*x58
          x60 = x10*x54
          x61 = x33 - x34*x60
          x62 = x52*x61
          x63 = x34*x58
          return [[x0 - x24*x8 - x30*x53 + x1*x3*x0*x0, -x11 + x12*x55 - x35*x57, -x24 + x35*x59, -x53],
               [-x30*x62 - x60*x8 - x7, x10*x23*x5*x2**-2 + x3 - x56*x62, x59*x61 - x60, -x62],
               [x29 - x30*x63, -x55 - x56*x63, x14**2*x22*x28*x34*x51 + x23, -x63],
               [x30*x52, x57, -x59, x52]]
      else:
          # TODO algorithm?
          import numpy as np
          return np.linalg.inv(matrix)