I am trying to write a TDMA algorithm with numba in nopython mode. Here is my code:
@jit(nopython=True)
def TDMA(a,b,c,d):
n = len(d)
x = np.zeros(n)
w = np.zeros(n)
# ac, bc, cc, dc = map(np.copy, (a, b, c, d)) # copy arrays
ac = np.copy(a)
bc = np.copy(b)
cc = np.copy(c)
dc = np.copy(d)
for i in range(1,n):
w[i] = ac[i-1]/bc[i-1]
bc[i] = bc[i] - w[i]*cc[i-1]
dc[i] = dc[i] - w[i]*dc[i-1]
x[n-1] = dc[n-1]/bc[n-1]
for k in range(n-2,-1,-1):
x[k] = (dc[k]-cc[k]*x[k+1])/bc[k]
return np.array(x)
Then test this solver:
A = np.array([[5, 2, 0, 0],[1, 5, 2, 0],[0, 1, 5, 2],[0, 0, 1, 5]],float)
B = np.array([[15],[2],[7],[20]],float)
a = A.diagonal(-1)
b = A.diagonal()
c = A.diagonal(1)
x1 = np.linalg.solve(A,B)
x2 = TDMA(a,b,c,B)
print('by default solver, x1 = ',x1)
print('by TDMA, x2 = ',x2)
However, my TDMA function fails with a TypingError:
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Cannot resolve setitem: array(float64, 1d, C)[int64] = array(float64, 1d, C)
File "", line 16:
def TDMA(a,b,c,d):
x[n-1] = dc[n-1]/bc[n-1]
^
It works properly with @jit decorator, but fails with in nopython mode. How should I modify this TDMA function to make it compatible with nopyhon?
The line I commented:
ac, bc, cc, dc = map(np.copy, (a, b, c, d)) # copy arrays
Is not compatible to nopython either. Is it possible to use the map function in nopython mode?
I understand that my TDMA may be still slow. So is there a fastest code using python 3 language to implement Tridiagonal Matrix Algorithm?
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)