-1

I am having an equation

Ax=By

Where A and B are tridiagonal matrices. I want to calculate a matrix

C=inv (A).B

there are different x,s which will give different y,s hence calculation of C is handy.

Can someone please tell me a faster method to compute the inverse. I am using Python 3.5 and prefer if we use any method from numpy. If not possible I can use scipy or cython as second and third choice.

I have seen other similar questions but they do not fully match with my problem.

Thank you

hsinghal
  • 152
  • 1
  • 10

2 Answers2

1

There are many method to do it, anyway one of the simplest is the Tridiagonal matrix algorithm see the Wiki page. This algorithm work in O(n) time, there is a simple implementation in Numpy at the following Github link. However, you may think to implement by yourself one of the known algorithm, for example something like a LU factorization

Andrea Madotto
  • 183
  • 2
  • 15
  • Thanks, but the problem here is that this algorithm will be too slow. As you know that the usual numpy matrix methods use intel MKL libraries or BLAS implementation, hence you can not beat these algorithms with usual element by element methods. I can not implement such implementations by myself but can utilize them if such a thing is available. – hsinghal Aug 18 '16 at 17:21
  • 1
    Okay, I got it. Yes, of course BLAS implementation is kind of unbeatable, but from the doc of scipy/numpy seams that there is no particular methods for tridiag-mat inversion. – Andrea Madotto Aug 19 '16 at 02:58
0

scipy.linalg.solve_banded is a wrapper for LAPACK which should in turn call MKL. It seems to run O(N). For a trivial example to show syntax

  a = np.array([[1,2,0,0], [-1,2,1,0], [0,1,3,1], [0,0,1,2]])
  x = np.array([1,2,3,4])
  b = np.dot(a,x)
  ab = np.empty((3,4))
  ab[0,1:] = np.diag(a,1)
  ab[1,:] = np.diag(a,0)
  ab[2,:-1] = np.diag(a,-1) 
  y =  solve_banded((1,1),ab,b)
  print y
Tunneller
  • 381
  • 2
  • 13