托马斯算法Python



我是Python的初学者。我有以下代码不会返回任何内容。

有人对为什么有答案吗?当我执行代码时,实际上什么也不会发生。

找到矩阵的大小并确定n

def thomas(a,b,c,d):
    a= [1,3,1.5,4.5,4.5]
    b= [-6,-4.5,-7.5,-7.5,-4.5] 
    c= [3,3,3,3,3]
    d= [0,0,100,0,0]
    n = len(b)
    #print n # Used for debugging
    # Test the size of a and c
    if len(a) != n-1:
        print ('Wrong index size for a.n A should have an index of'), n-1, 'n 
    Your a has ', len(a)
    exit()
    if len(c) != n-1:
        print ('Wrong index size for c.n C should have an index of'), n-1, 'n 
    Your c has', len(c)
    exit()
# Converting to float and appending 0.0 to c
    for i in range(0,len(a)):
        a[i] = float(a[i])
    for i in range(0,len(b)):
        b[i] = float(b[i])
    for i in range(0,len(c)):
        c[i] = float(c[i])
    for i in range(0,len(d)):
        d[i] = float(d[i])
c.append(0.0) # Hack to make the function to work
# Calculate p and q
    p = []; q= [] 
    p.append(c[0]/b[0]); q.append(d[0]/b[0])
    for j in range(1,n):
        pj = c[j]/(b[j] - a[j-1]* p[j-1])
        qj = (d[j] - a[j-1]*q[j-1])/(b[j] - a[j-1]* p[j-1])
        p.append(pj); q.append(qj)
#print p,q # Used for debugging the code!
# Back sub
    x = []; x.append(q[n-1])
    for j in range(n-2,-1,-1):
        xj = q[j] - p[j]*x[0] # Value holder
        x.insert(0,xj) # Building the list backwards
# Return the value
    return x    

您的凹痕很棘手。

在第13和17行上,缩进exit语句,因此仅在满足条件(IF语句(时才调用:

  if len(a) != n-1:
      print ('Wrong index size for a.n A should have an index of')
      exit()
  if len(c) != n-1:
      print ('Wrong index size for c.n C should have an index of')
      exit()

同样, c.append(0.0)被列为一个标签太远。这线也缩进了。

请记住,与大多数其他语言不同,Python将其执行结构以间距为基础。对于if语句之类的东西,该语句下的所有内容都被认为与if (something) { }相同。

if (something):
      do this
      and this
but this will happen regardless

最新更新