Inverse matrix algorithm