Home > Blockchain >  How to run recursion in class python
How to run recursion in class python

Time:10-27

I am trying to create a class Matrix which has a method called row_echelon, and I am trying to use recursion, but it's not working.

class Matrix:
    def __init__(self,matrix):
        self.matrix = matrix
    
    

    def row_echelon(self):
        r, c = self.matrix.shape
        if r == 0 or c == 0:
            return self.matrix

        for i in range(len(self.matrix)):
            if self.matrix[i,0] != 0:
                break
        else:
    
            new_matrix = self.row_echelon(self.matrix[:,1:])
        
            return np.hstack([self.matrix[:,:1], new_matrix])


        if i > 0:
            ith_row = self.matrix[i].copy()
            self.matrix[i] = self.matrix[0]
            self.matrix[0] = ith_row


        self.matrix[0] = self.matrix[0] / self.matrix[0,0]

        self.matrix[1:] -= self.matrix[0] * self.matrix[1:,0:1]


        new_matrix = self.row_echelon(self.matrix[1:,1:])

        return np.vstack([self.matrix[:1], np.hstack([self.matrix[1:,:1], new_matrix]) ])

Here are my input and ouput: Input:

A = np.array([[4, 7, 3, 8],
              [8, 3, 8, 7],
              [2, 9, 5, 3]], dtype='float')
my_matrix = Matrix(A)
print(my_matrix.row_echelon())

Output: `

TypeError                                 Traceback (most recent call last)
Cell In [43], line 5
      1 A = np.array([[4, 7, 3, 8],
      2               [8, 3, 8, 7],
      3               [2, 9, 5, 3]], dtype='float')
      4 my_matrix = Matrix(A)
----> 5 print(my_matrix.row_echelon())

Cell In [42], line 46, in Matrix.row_echelon(self)
     41 self.matrix[0] = self.matrix[0] / self.matrix[0,0]
     43 self.matrix[1:] -= self.matrix[0] * self.matrix[1:,0:1]
---> 46 new_matrix = self.row_echelon(self.matrix[1:,1:])
     48 return np.vstack([self.matrix[:1], np.hstack([self.matrix[1:,:1], new_matrix]) ])

TypeError: Matrix.row_echelon() takes 1 positional argument but 2 were given

` I don't understand which 2 arguements are given?

CodePudding user response:

In def row_echelon(self): you define the function to be called with object reference but in new_matrix = self.row_echelon(self.matrix[:,1:]) you pass a matrix to the function as well. When you call the a class function, it will automatically get an argument of self the object reference due to the definition whether you want it or not. And then the self.matrix[:,1:] you pass with the function becomes the second argument. The solution is to define your function with another argument instead of in addition to self and operate on a copy of the matrix instead of the class member matrix.

CodePudding user response:

!Update It finally works. Thank you all so much!! Here is my code:

    def row_echelon(self):
    r, c = self.matrix.shape
    if r == 0 or c == 0:
        return self.matrix

    for i in range(len(self.matrix)):
        if self.matrix[i,0] != 0:
            break
    else:
        matrix_2 = self.matrix[:,1:]
        new_matrix = Matrix(matrix_2).row_echelon()
    
        return np.hstack([self.matrix[:,:1], new_matrix])


    if i > 0:
        ith_row = self.matrix[i].copy()
        self.matrix[i] = self.matrix[0]
        self.matrix[0] = ith_row


    self.matrix[0] = self.matrix[0] / self.matrix[0,0]

    self.matrix[1:] -= self.matrix[0] * self.matrix[1:,0:1]

    matrix_2 = self.matrix[1:,1:]
    new_matrix = Matrix(matrix_2).row_echelon()

    return np.vstack([self.matrix[:1], np.hstack([self.matrix[1:,:1], new_matrix]) ])
  • Related