Week2 row_echelon_form function

I’m trying to understand the format of the expected answer and I think I’m very confused. I thought that all we needed to do was kind of a fill in the blanks and just substitute the correct answers for every None in the function.

However, this does not lead to the code to work correctly as I’m not accounting for cases where the pivot is zero and the value below pivot either doesn’t exist (last row) or is also zero.

Clearly I need to add extra code to account for this - but the assignment doesn’t really tell me I need to. Anyone can help me with this - really frustrated and so tempted to drop out of this.

They gave you quite a bit of “template” code for those cases and you just need to fill in the places (as you say) that have the “None” placeholder.

The format of the output is the full “augmented” matrix after you have performed the reduction to row echelon form. So if the inputs are a 3 x 3 matrix and a 3 x 1 matrix for the “right hand side” of the equations, the augmented matrix will be 3 x 4.

I added a bunch of print statements to my code to see what is going on at the various steps and here’s what I see when I run the test block for that function:

M =
[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]]
Outer loop row = 0
M[0] after 1/pivot =
[1. 0. 0. 0.]
Inner loop row = 1
M[1] after = [0. 1. 0. 0.]
Inner loop row = 2
M[2] after = [0. 0. 1. 0.]
Outer loop row = 1
M[1] after 1/pivot =
[0. 1. 0. 0.]
Inner loop row = 2
M[2] after = [0. 0. 1. 0.]
Outer loop row = 2
M[2] after 1/pivot =
[0. 0. 1. 0.]
M final =
[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]]
M =
[[1. 2. 3. 4.]
 [5. 0. 2. 3.]
 [1. 4. 5. 6.]]
Outer loop row = 0
M[0] after 1/pivot =
[1. 2. 3. 4.]
Inner loop row = 1
M[1] after = [  0. -10. -13. -17.]
Inner loop row = 2
M[2] after = [0. 2. 2. 2.]
Outer loop row = 1
M[1] after 1/pivot =
[-0.   1.   1.3  1.7]
Inner loop row = 2
M[2] after = [ 0.   0.  -0.6 -1.4]
Outer loop row = 2
M[2] after 1/pivot =
[-0.         -0.          1.          2.33333333]
M final =
[[ 1.          2.          3.          4.        ]
 [-0.          1.          1.3         1.7       ]
 [-0.         -0.          1.          2.33333333]]
 All tests passed

Thank you so much for the response. My point was that the template code does not handle the case of the final role having value after pivot = 0 very well. This requires me to add lines of code of my own - which I can but I’m wondering if I’m missing something here. Basically that there’s no way to make the code work simply by filling in all the Nones and without any extra lines of code.

I think you must be missing something. I did not have to add any code of the sort that you are describing.

{mentor edit: code removed}

I pasted what the function looks like for me. Let me know if it is the same for you and you didn’t have to modify anything except the None entries.

Yes, the code you show is the same as the template code that I see in a clean copy of the notebook.

Also note that there is already logic that bails early on if the input matrix is singular. That means you cannot hit the case that there are no non-zero entries below the pivot in the case that the original pivot value is zero. Or at least you can say that we can pass the all the tests here and the grader without writing any extra code to handle that potential case. If you like, you can put in an assertion that “throws” if you hit that case.