0

I came across the following code of gaussian elimination with no pivoting. I am using Python 3 notebook but I am getting the following errors when I run it and I am looking for some help with troubleshooting these errors, thanks!

here are the errors I am getting, I am not quite sure how to fix these and I am looking for some help with this!

 NameError                                 Traceback (most recent call last)
<ipython-input-15-6b9df16115dd> in <module>()
 67     b =  np.array([[14.],[4.],[2.],[2.]])
 68 
---> 69     print (GENP(np.copy(A), np.copy(b)))
 70 
 71     print (GEPP(A,b))

<ipython-input-15-6b9df16115dd> in GENP(A, b)
 25         raise ValueError("Invalid argument: incompatible sizes between A & b.", b.size, n)
 26 
---> 27     for pivot_row in xrange(n-1):
 28 
 29         for row in xrange(pivot_row+1, n):

NameError: name 'xrange' is not defined

and here is the code I found: ​

import numpy as np
def GENP(A, b):

'''

    Gaussian elimination with no pivoting.

    % input: A is an n x n nonsingular matrix

    %        b is an n x 1 vector

    % output: x is the solution of Ax=b.

    % post-condition: A and b have been modified. 

    '''

    n =  len(A)

    if b.size != n:

        raise ValueError("Invalid argument: incompatible sizes between A & b.", b.size, n)

    for pivot_row in xrange(n-1):

        for row in xrange(pivot_row+1, n):

            multiplier = A[row][pivot_row]/A[pivot_row][pivot_row]

            #the only one in this column since the rest are zero

            A[row][pivot_row] = multiplier

            for col in xrange(pivot_row + 1, n):

                A[row][col] = A[row][col] - multiplier*A[pivot_row][col]

            #Equation solution column

            b[row] = b[row] - multiplier*b[pivot_row]

    print (A)

    print (b)

    x = np.zeros(n)

    k = n-1

    x[k] = b[k]/A[k,k]

    while k >= 0:

        x[k] = (b[k] - np.dot(A[k,k+1:],x[k+1:]))/A[k,k]

        k = k-1

    return x

if __name__ == "__main__":

    A = np.array([[1.,-1.,1.,-1.],[1.,0.,0.,0.],[1.,1.,1.,1.],[1.,2.,4.,8.]])

    b =  np.array([[14.],[4.],[2.],[2.]])

    print (GENP(np.copy(A), np.copy(b)))

    print (GEPP(A,b))
user104
  • 133
  • 1
  • 5

1 Answers1

0

xrange is not present in python3. Use range instead.

Moberg
  • 4,593
  • 3
  • 30
  • 48