简体   繁体   中英

I kept getting "ModuleNotFoundError: No module named 'error'"

I was given an outline of this code from my teacher, however I kept getting ModuleNotFoundError: No module named 'error' . I know that I need a module named error , however when I couldn't find a code for a module named error .

I am trying to solve this question:

Solve the tridiagonal equations Ax = b by Doolittle’s decomposition method, where:

A = [6 2 0 0 0
    −1 7 2 0 0
     0 −2 8 2 0    
     0 0 3 7 −2    
     0 0 0 3 5]

b = [2    
    −3    
     4    
    −3    
     1].

Here is the code that I was using:

from numpy import argmax, dot, zeros, array, asarray, tril, triu

def swapRows(v,i,j):
    if len(v.shape) == 1: v[i],v[j] = v[j],v[i]
    else:
        temp = v[i].copy()
        v[i] = v[j]
        v[j] = temp
    def swapCols(v,i,j):
    temp = v[:,j].copy()
    v[:,j] = v[:,i]
    v[:,i] = temp
     import error

def LUdecomp(a,tol=1.0e-9):
    n = len(a)
    seq = array(range(n))
       # Set up scale factors
    s = zeros(n)
    for i in range(n):
        s[i] = max(abs(a[i,:]))        
    
    for k in range(0,n-1):
        
      # Row interchange, if needed
        p = argmax(abs(a[k:n,k])/s[k:n]) + k
        if abs(a[p,k]) <  tol: error.err('Matrix is singular')
        if p != k:
            swapRows(s,k,p)
            swapRows(a,k,p)
            swapRows(seq,k,p)
            
      # Elimination            
        for i in range(k+1,n):
            if a[i,k] != 0.0:
                lam = a[i,k]/a[k,k]
                a[i,k+1:n] = a[i,k+1:n] - lam*a[k,k+1:n]
                a[i,k] = lam
    return a,seq

def LUsolve(a,b,seq):
    n = len(a)
       # Rearrange constant vector; store it in [x]
    x = b.copy()
    for i in range(n):
        x[i] = b[seq[i]]
           # Solution
    for k in range(1,n):
        x[k] = x[k] - dot(a[k,0:k],x[0:k])
    x[n-1] = x[n-1]/a[n-1,n-1]    
    for k in range(n-2,-1,-1):
       x[k] = (x[k] - dot(a[k,k+1:n],x[k+1:n]))/a[k,k]
    return x

A = asarray( [ [ 6, 2, 0, 0, 0 ], 
               [ -1, 7, 2, 0, 0 ], 
               [ 0, -2, 8, 2, 0 ], 
               [ 0, 0, 3, 7, -2 ], 
               [ 0, 0, 0, 3, 5 ] ], dtype=float ) A_orig = A.copy() b = asarray( [ 2, -3, 4, -3, 1 ], dtype=float )  b_orig = b.copy() 

A,seq = LUdecomp(A) # A is overwritten as L\U L = tril( A, -1 ) # extract L  for ii in range(L.shape[0]): L[ii,ii] = 1.0 # add in 1's on the diagonal  U = triu( A,  0 ) # extract U  print ("L = ") print (L) print ("U = ") print (U)  if False:
    print ("A[seq,:]= ")
    print (A_orig[seq,:])
    print ("LU= ")
    print (dot(L,U)) 

x = LUsolve(A,b,seq)  print ("Solution= ", x)

If your intention is to throw an error at some point, then you can reach that without the import error statement. Raising an exception might be a solution. See the documentation on errors and exceptions .

You can remove import error and edit

if abs(a[p,k]) <  tol: error.err('Matrix is singular')

in LUdecomp() as follows:

if abs(a[p,k]) <  tol: 
    raise Exception('Matrix is singular')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM