Programming Assignment - Gaussian Elimination: `row_echelon_form` passes all tests, but `back_substitution` fails completely

Hi everyone,

I’m currently working on a linear algebra assignment involving Gaussian elimination in Python. My row_echelon_form implementation is working perfectly — all tests pass (see first screenshot). However, my back_substitution function is failing all four unit tests (see second screenshot), and I’m stuck figuring out why.


What’s working:

What’s failing:

  • back_substitution: fails all tests with completely incorrect outputs — seems like none of the substitution is working properly.

I’ve followed the standard approach: iterating from bottom to top, identifying pivots, and reducing rows above. But clearly something’s wrong in my logic or indexing. I’d really appreciate if anyone could spot what might be going wrong — happy to clarify anything or share more context.

Thanks in advance!

Of course back substitution is different logic than in row echelon form. Here we start with an input matrix that is already in row echelon form. I added print statements to my code so that I can see the test cases. Here’s what I see when I run the test cell for back substitution:

M initial =
[[1 0 0 5]
 [0 1 0 6]
 [0 0 1 7]]
M final =
[[1 0 0 5]
 [0 1 0 6]
 [0 0 1 7]]
solution = [5 6 7]
M initial =
[[ 1.          2.          3.          4.        ]
 [-0.          1.          1.3         1.7       ]
 [-0.         -0.          1.          2.33333333]]
M final =
[[ 1.          0.          0.         -0.33333333]
 [ 0.          1.          0.         -1.33333333]
 [-0.         -0.          1.          2.33333333]]
solution = [-0.33333333 -1.33333333  2.33333333]
M initial =
[[ 1.          5.          6.          9.        ]
 [-0.          1.          1.          1.64285714]
 [ 0.          0.          1.          0.        ]]
M final =
[[ 1.          0.          0.          0.7857143 ]
 [-0.          1.          0.          1.64285714]
 [ 0.          0.          1.          0.        ]]
solution = [0.7857143  1.64285714 0.        ]
M initial =
[[1. 8. 6. 9.]
 [0. 1. 8. 6.]
 [0. 0. 1. 1.]]
M final =
[[ 1.  0.  0. 19.]
 [ 0.  1.  0. -2.]
 [ 0.  0.  1.  1.]]
solution = [19. -2.  1.]
 All tests passed

Try adding the same print statements to your code and that should give you some better visibility about what is going wrong.

One thing we can see is that you get the third column right in all cases, but the previous columns are wrong. That should be a clue as well.

1 Like

Thank you so much for your advice. All passed now. The problem was associated with the formula.

1 Like

That’s great news that you were able to figure out the issues based on printing and analyzing the intermediate results. Nice work. Onward! :nerd_face:

1 Like