C1W2 Assignment - Exercise 1 calculation issue

I believe I’m getting the code mostly correct (3 tests passed, 1 test failed). My issue appears to be either with the value_below_pivot equation or the one that follows (M[j] = M[j] - …)

Based on the comments in the code, value_below_pivot should be almost the same value as the pivot (row index = column index), except +1 row. Additionally, “row_to_reduce → row_to_reduce - value_below_pivot * pivot_row” indicates pivot_row should be the row index = column index, correct?

Wrong output for test case check_matrix_1.
Expected:
[1 2 3 4]
[-0 1 1.3 1.7]
[-0 -0 1 2.33]

Got:
[1 2 3 4]
[-0 1 0.6 0.4]
[-3 -0 1 2]

Any idea where I might be going wrong? It’s a bit difficult for me to parse exactly where the issue is occurring.

1 Like

Your “reduction” is incomplete, right? Why is the element at M[2, 0] ending up as -3? That’s wrong: it should be 0, right? So why is that not happening?

Put print statements to show how your loops are working. Print the index values on every iteration of each loop and show the value of each modified row after each step.

1 Like

I’m still not sure where I’m going wrong. The print statements seem to indicate I’m doing things correctly. At the end, I get the following, which appears to be in row echelon form.

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

However, the last three test cases appear to not work:

[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]]

[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]]

[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]]

[[ 1. 2. 3. 4.]
[ 0. -5. -3. -2.]
[ 1. 4. 5. 6.]]

[[ 1. 2. 3. 4.]
[ 0. -5. -3. -2.]
[ 1. 4. 5. 6.]]

[[ 1. 2. 3. 4. ]
[-0. 1. 0.6 0.4]
[-3. 0. 1. 2. ]]

1 Like

Ok, well there must be something different about the test cases that you fail. Now you need to figure that out. You can look at those test cases by opening the file w2_unittest.py and finding the function test_row_echelon_form. I’ll save you the effort for the last test case. Here’s the input matrix in that case:

M =
[[1. 2. 3. 4.]
 [5. 0. 2. 3.]
 [1. 4. 5. 6.]]

So did you add the print statements to watch the process that your code is doing? Why would it work in the first test case and not in the last one?

I added a bunch of print statements to my code to show what is happening and here is what I get for the last test case:

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
1 Like

It seems like my inner loop is running when it shouldn’t be.

[[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 = 0
M[0] after reduction:
[1. 2. 3. 4.]

Inner loop row = 0
M[0] after reduction:
[1. 2. 3. 4.]

Outer loop row = 1
M[1] after 1/pivot:
[-99.75   1.   -38.75 -58.5 ]

Inner loop row = 1
M[1] after reduction:
[-99.75   1.   -38.75 -58.5 ]

Outer loop row = 2
M[2] after 1/pivot:
[ 2.5 -0.   1.   1.5]

Wrong output for test case check_matrix_1. 
	Expected:
	 [[ 1.          2.          3.          4.        ]
 [-0.          1.          1.3         1.7       ]
 [-0.         -0.          1.          2.33333333]].
	Got:
 [[  1.     2.     3.     4.  ]
 [-99.75   1.   -38.75 -58.5 ]
 [  2.5   -0.     1.     1.5 ]].
 3  Tests passed
 1  Tests failed
1 Like

Yes, it looks like the “range” on your inner loop is wrong. I needs to be the current outer loop row + 1 as the start and end at the size of the total number of rows, right? So how would you write that as a “range()”?

You also don’t show how the element M[1,0] ended up being -99.75. When did that happen? You aren’t printing correctly either. I suspect that the row number you are printing on the inner loop is wrong. You want the inner loop row (j), not the outer loop row (row).

1 Like

I think the inner loop range was pre-defined at the beginning of the assignment though (no “None” to change), so I didn’t mess with that.
range(row + 1, num_rows)

I think I fixed the printing, and evidently something else too since there is no more -99.75:

[[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 = 0
M[1] after reduction:
[  0. -10. -13. -17.]

Inner loop row = 0
M[1] after reduction:
[1. 4. 5. 6.]

Outer loop row = 1
M[1] after 1/pivot:
[-0.   1.   1.3  1.7]

Inner loop row = 1
M[2] after reduction:
[ 1.   0.  -0.2 -0.8]

Outer loop row = 2
M[2] after 1/pivot:
[-5. -0.  1.  4.]

Wrong output for test case check_matrix_1. 
	Expected:
	 [[ 1.          2.          3.          4.        ]
 [-0.          1.          1.3         1.7       ]
 [-0.         -0.          1.          2.33333333]].
	Got:
 [[ 1.   2.   3.   4. ]
 [-0.   1.   1.3  1.7]
 [-5.  -0.   1.   4. ]].
 3  Tests passed
 1  Tests failed
1 Like

I think your print statement here must be wrong then. You must be printing row. You should be printing j. The j value should never be 0, right? I explained this earlier.

1 Like

So why does the first iteration of the outer loop not reduce row 3 (index 2)?

And the second M[1] you show there can’t be right: the first element should be 0 after the previous reduction. So what is wrong there? Are you printing the wrong thing or did you actually overwrite the previously correct M[1]? The details matter.

Oh, sorry, that is the original unmodified M[2], right? So you called it the wrong thing and you did not do the right action on it: the point is you should be reducing it by row 0 as you just did with row 1, right?

1 Like

I got a couple more hours in and I feel like I’m narrowing down the issue a bit. I’ve fixed the inner loop counter, I believe – j does not equal 0 anymore).

If I’m reading the results correctly, it seems like …

M[1] after reduction (j):
[ 0. -5. -3. -2.]

is exactly where it fails. I get the correct results on paper [0 -10 -13 -17], but not sure how the code is failing. I’m reviewing my formulas and, despite knowing one of them is incorrect, I get worse results from changing anything in the inner loop “None” values.

[[1. 2. 3. 4.]
 [5. 0. 2. 3.]
 [1. 4. 5. 6.]]
PRINT ROW: This is the for clause
Outer loop row = 0
M[0] after 1/pivot:
[1. 2. 3. 4.]

Inner loop row (j) = 1
M[1] after reduction (j):
[ 0. -5. -3. -2.]

Inner loop row (j) = 2
M[2] after reduction (j):
[1. 4. 5. 6.]

PRINT ROW: This is the for clause
Outer loop row = 1
M[1] after 1/pivot:
[-0.   1.   0.6  0.4]

Inner loop row (j) = 2
M[2] after reduction (j):
[21. 24. 25. 26.]

PRINT ROW: This is the for clause
Outer loop row = 2
M[2] after 1/pivot:
[0.84 0.96 1.   1.04]

Wrong output for test case check_matrix_1. 
	Expected:
	 [[ 1.          2.          3.          4.        ]
 [-0.          1.          1.3         1.7       ]
 [-0.         -0.          1.          2.33333333]].
	Got:
 [[ 1.    2.    3.    4.  ]
 [-0.    1.    0.6   0.4 ]
 [ 0.84  0.96  1.    1.04]].
 3  Tests passed
 1  Tests failed
1 Like

If you know how to get the correct answer on paper, then you know what the correct steps are. Now you have to figure out why your code is not doing the same thing as what you are doing on paper. Maybe put in more print statements.

So at the very first iteration of the inner loop, we have

row = 0
j = 1

M[0] = [1 2 3 4]
M[1] = [5 0 2 3]

So what you compute is

M[1] = M[1] - 5 * M[0]

Ok, what is 5 * M[0]?

[5 * 1, 5 * 2, 5 * 3, 5 * 4] = [5, 10, 15, 20]

Right? And what does that give you when you do the subtract? It should be:

M[1] = [0 -10 -13 -17]

So why do you not get that? Print out the value that you are multiplying times M[0].

Sorry, but this is how debugging works: you have to understand every single thing that happens at every single step. When something doesn’t work, you have to find the first point at which something goes wrong and then explain and understand it.

Then once you explain the previous thing, the next question is why at the row = 0, j = 2 step, you don’t end up with 0 in column 0 of M[2]. That’s the point of the first outer loop: you need to end up with 0 in column 0 of every row below row 0.

1 Like

I finally got it - thanks for your help!

No need to apologize about the debugging steps - I fully understand leading me little by little. Believe it or not, I used to be a Java programmer. I’ve got a corporate software patent and everything :stuck_out_tongue: I just never liked it, and it’s been 10 years since then. Brushing up on this stuff, coupled with the math that I was also never good at, is proving to be difficult. I appreciate the help very much.

2 Likes

It’s great to hear that you got the issues sorted out. Onward! :nerd_face:

1 Like