Testing the Toeplitz inversion method


In [1]:
import numpy as np
import scipy.linalg as spl
from toeplitz import toeplitz_inverse
import matplotlib.pyplot as plt
%matplotlib inline
# import seaborn as sns
np.set_printoptions(suppress=True)

In [2]:
N = 50
x = np.linspace(0, 10, N)[:, None]

t = np.exp(-(x - x.T)**2 / 8.)
t = t + np.diag(np.ones(N))

c = np.array(np.arange(5,0,-1), dtype=float)
m = spl.toeplitz(c)
print m

# print np.linalg.cond(m)
# print x.shape, t.shape
# print np.amin(t), np.amax(t)


[[ 5.  4.  3.  2.  1.]
 [ 4.  5.  4.  3.  2.]
 [ 3.  4.  5.  4.  3.]
 [ 2.  3.  4.  5.  4.]
 [ 1.  2.  3.  4.  5.]]

In [3]:
np_inv = np.linalg.inv(m)
np_logdet = np.log(np.linalg.det(m))

In [4]:
my_inv, my_logdet = toeplitz_inverse(m)

In [5]:
diff = np.sqrt((np_inv - my_inv)**2)

print 'logdet diff = ', np_logdet - my_logdet
print 'inv max diff = ', np.amax(diff)
# print diff

plt.matshow(diff)
plt.show()


logdet diff =  2.6645352591e-15
inv max diff =  1.11022302463e-15

In [ ]:


In [ ]: