A new algorithm for the inversion of Hankel and Toeplitz matrices