Need details for C1_W2 exercise 1

When creating a post, please add:

  • What is meant by CASE PIVOT IS ZERO (line 45)? In the function get_index_first_non_zero_value_from_row the title itself says find the Pivot of any row. Which means this function either gives the pivot of a row which has a non-zero value or it will return None. Then how can pivot be 0. In what cases. Am I missing something?
  • 2nd we have 2 comments Find the first non-zero entry in the current row (pivot) at line 39 followed by 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. at line 41. What I don’t understand is how can we calculate the pivot without first knowing the column_index itself? Function get_index_first_non_zero_value_from_row gives index of the column for a particular row. Which allows us to find the actual pivot of the row. Isn’t it? Shouldn’t it the other way round.
  • 3rd at line 46 we enter a case for PIVOT is ZERO. Then how come at line number 51 we are checking for non-zero pivot. For me Pivot with value 0 doesn’t make sense anyway.

That is the case in which all elements of the row are zero. Perhaps the way that they phrased that is a bit confusing. I would say in that case that there is no pivot. The pivot by definition needs to be non-zero. Note that the function returns None in that case.

If you have the index of the value, then it’s trivial to retrieve the actual value, right? You know its row index and its column index.

Right, I think we are agreeing on that. But the point is that your logic needs to handle that case (what they call “pivot of 0” and what we prefer to call “no pivot”), right? So how would you handle that?

Thank you for quick reply. In that case I will write the whole function myself. It was just too confusing to read the comments and understand what I have to do.

Btw, if I write the main part of the function myself it will work right. As long as it generates the correct output, i.e. correct row echelon form. Or is it necessary to fill the blanks in provided function?

1 Like

The grader does not read your source code: it calls the function with various test cases and checks the answers. So you just have to be careful not to change the definition of the API.

Thank you so much. I received 100% as grade.

1 Like

Excellent! Nice work!

Well, you say that if ‘pivot is zero (no pivot, all row values are zero)’ that means all code under the condition in line 49 if np.isclose(pivot, 0): will be executed. So, how the instructions starting from line 71 when a pivot is not in the diagonal will be processed?
Actually, the hint is in another thread of Thomas_woehrle where he mentions that the pivot variable is not really a pivot…

In the case that there is an actual pivot (non-zero), then you skip the body of that “if” statement and fall through to the following block of code which deals with the rest of the processing. Note that nothing in any of the logic depends on anything being on the diagonal: it just finds the first non-zero entry in the row and treats that as the pivot. The point Thomas was making is that it should also include logic to sort the rows such that the leftmost non-zero value in each row moves to the right with each successive row even when they aren’t on the diagonal. The basic structure of the algorithm guarantees that they will be no further left than the diagonal on a given row, but you can end up with cases in which the diagonal itself is zero.

I have the same doubt.
If the pivot do not exist (i.e. the row only have zeroes), then get_index_first_non_zero_value_from_row(M, i) wil return None.

So when you run
if np.isclose(pivot, 0):
if the pivot is None then this will never be true

But pivot and the returned index are different values, right? pivot was set to 0 earlier and only gets reset to something other than zero if the column index that is returned is not None. At least that’s the way I wrote the code.

You have to pay attention to when you are dealing with index values and when you are dealing with the actual matrix values.

I seted it using the function the problem tells you to use:

    # Find the first non-zero entry in the current row (pivot)
    pivot = M[i][get_index_first_non_zero_value_from_row(M, i)]

But I have two doubts, if the values of the matrix B are not 0, like on the row 1

array([[1. , 2. , 3. , 1. ],
[0. , 0. , 0. , 1. ],
[0. , 0. , 1. , 0.8]])

0, 0, 0 | 1

Can I use the values after the | as the pivot?

also, in case all the values are 0 it will try to access index None

That code doesn’t work, does it? What happens with your code as written if the “get index first non zero” function returns None? My prediction is that it doesn’t end well. So how are you going to handle that case?

I did a workaround using this:

    # Find the first non-zero entry in the current row (pivot)
    if get_index_first_non_zero_value_from_row(M, i) == None:
        pivot = 0
        pivot = M[i, get_index_first_non_zero_value_from_row(M, i)]
    # 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 = i

but I dont think that was inteded from the creatores of this exercise

Well, I’m not sure what they were thinking either, but the point is we need to handle these cases. If you can find another working solution, that’s fine.

1 Like