Matrix inversion algorithm