C1W2_Assignment: What am I supposed to iterate over?

Hi. I am attempting the assignment in week 2 titled Gaussian Elimination. I understand the concepts thoroughly but I am having trouble understanding one part of the code.

Near the bottom of the main loop of the algorithm is this Python code:

        # Divide the current row by the pivot, so the new pivot will be 1. (reduced row echelon form)
        M[i] = (1/pivot) * M[i]
        # Perform row reduction for rows below the current row
        for j in None:
            # Get the value in the row that is below the pivot value. Remember that the value in the column position is given by the variable called column_index
            value_below_pivot = None
            # Perform row reduction using the formula:
            # row_to_reduce -> row_to_reduce - value_below_pivot * pivot_row
            M[j] = None

I am struggling to understand what to iterate over in this line for j in None. What do I replace None with?

Can someone explain? Am I iterating over rows here or columns? If i is the row iterator, does that mean that j is the column iterator? If so, can someone help me understand the numpy syntax that would allow me to iterate over the columns?

1 Like

You have two nested loops. In the outer loop, i is the rows. In the inner loop j is the rows below the current row, which would be i + 1 to num_rows, right? Please go back and read the voluminous instructions before this function. I really recommend taking one of the examples that are test cases and actually “playing it out” with pencil and paper to make sure you understand what is going on here. If you don’t have a clear picture of what it is the algorithm does, then it’s hopeless to translate that into code.

1 Like

Thank you. That was very helpful. Would you please help me to understand this part?

    # Find the first non-zero entry in the current row (pivot)
    pivot = None
    # This variable stores the pivot's column index, it starts at i, but it may change if the pivot is not in the main diagonal.
    column_index = None

    if np.isclose(pivot, 0): 

How can this conditional statement ever be true if np.isclose(pivot, 0):?

The comments in the code say this: # Find the first **non-zero entry** in the current row (pivot) If I am supposed to find the first non-zero entry in the row and assign it to the variable pivot, then how can if np.isclose(pivot, 0): ever evaluate to true?

I think I’m supposed to do this to assign the pivot variable: pivot = M[i, get_index_first_non_zero_value_from_row(M, i)].

Now looking at the matrix [[1 2 3 1] [0 0 0 2] [0 0 5 4]] I can see that pivot = M[i, get_index_first_non_zero_value_from_row(M, i)] will never assign to pivot a non-zero value.

So how can one ever get if np.isclose(pivot, 0): equal to true?

1 Like

Suppose you have a row that contains all zeros? That can happen, right? There is a specific test case that provokes that condition. Have a look and try to run that test case with your code.

The point is that it is a great idea to work out particular examples, but we are writing fully general code. Meaning that we have to handle every possible case correctly, including when the input matrix is singular.

1 Like

The reason for using isclose is because when you’re doing math on the matrix rows, you might have a bit of truncation or roundoff error, and you’re dealing with floating point numerical precision issues. The computed value of zero might actually be a very tiny positive or negative value down in the remote decimal places.

Because of this effect, it’s never a good idea to check if a floating point value is exactly zero.

If you look in those utility functions for finding row or column values that are zero, they also always use “isclose”.