completed matrix inversion function