Please help with some hints as there is a difficuly with Graded assignment output achievement correctly.
Hi @Karthikeyadarbha ,
The community members and mentors are happy to help, but they would need information to diagnose what the problem might be. The best way to do this is to post the error trace stack.
Many thanks and here is the error stack execise wise :
Execise 1: reduced_row_echelon_form
Exercise3 : back_substitution
I think I’m missing to find and slice the set of rows above the substituon row here. Also, missing idea in formula computing at the row_to_reduce variable assignment step.
Execise 4: gaussian_elimination
For the first error, the test case gives you a matrix and vector full of zeros. Your code needs to handle the case that you look for a non-zero pivot value in a row and you don’t find any. It looks like you just went ahead and took the 0 value as the pivot and divided by zero. As you would expect, that doesn’t end well. They gave you a lot of comments and structure in the template code to show you where all these cases are. Have a look at your code again with that idea in mind.
For the back substitution case, your code must be broken in an interesting way. You can look at the test cases by clicking “File → Open” and then opening the file w2_unittest.py
. You’ll see that you failed the very first test case, which has a matrix that is already fully reduced. So each of your steps should have no effect, but somehow you are subtracting values from the augmented column which represent the solutions. So please examine the test case and instrument your logic to figure out why that is happening.
For gaussian_elimination
, there aren’t very many moving parts, but it calls back_substitution
. So you probably fail that test because the errors in back_substitution
.
Thanks for your inputs. If I understand correctly, are we talking about the auxiliary functions part of the graded exericse, when mentioning “They gave you a lot of comments and structure in the template code to show you where all these cases are” ?
On that comment, I was talking about the template code for the reduced_row_echelon_form
function. That’s the first problem you have to solve. In the template they have comments about the pivot being zero. I’d prefer to describe that as “no pivot found”, but at least their version is also understandable.
The “template code” is all of the code you’re provided in the notebook.
Thank you both, I will just check with those inputs above and try solving the exercises.
Testcases are failing for Exercise1
I’m not understanding what is the mistake I made in the code. Could you please help?
Well, your answer is not in row echelon form, right? In particular, notice the entries in column 1 below row 1 did not get reduced to zero. So why did that not happen? If you are not having any luck understanding that just by reading the code, then I recommend you start adding print statements to “instrument” your code and show you what is going on. You can also look at the test case to see what the input looks like by opening the file w2_unittest.py
, but I’ll save you the trouble. Here is the input matrix after augmentation:
[[ 1, 2, 3, 4],
[ 5, 0, 2, 3],
[ 1, 4, 5, 6]]
So start by working out what should happen with a pencil and paper. I mean actually play out the steps one at a time. That’s what your code needs to be doing, right? Then instrument your code and see why the steps that should be happening are not happening.
Debugging is part of the job and what I’m describing above is a method for going about it.
I still see issue with the testcase execution. The last row is not converted to the expected value still, as my attempts are failing to instrument code to handle the last row echlelon conversion, which I guess my code is unable to handle non diagonal pivots.
Also, to avoid processing zeros, the code now checks the pivot’s column index not none value and proceed with further steps.
Please help with some suggestions.
There must be something wrong with your loop logic. You are stopping after processing the first column. Note that there are two nested loops there:
for every column:
for every row:
reduce the element underneath the pivot for the current column
So somehow your outer loop is stopping after the first iteration. The way to debug this type of code is what I said above: first work it out yourself with pencil and paper using the algorithm as defined in the instructions. Now add print statements to all the key decision points in your code so that you can see how the code is implementing that process. Somehow your version of the code stops after the first outer loop. Now you need to figure out why.
Okay, as I’m trying to solve this step by step with a pen and paper - raw method,
could you please help how to convert the last row in the following input matrix into echelon form ? I mean, I couldn’t able to connect with the theory explained before the exercise to the context in this step of the problem.
The input matrix for this testcase is :
[[1. 2. 3. 4.]
[5. 0. 2. 3.]
[1. 4. 5. 6.]]
So that, I can take it towards instrumenting code accordingly.
Well, what happens in the pencil and paper case when you finish dealing with the first column? You move on to the second column, right? And that means you also start with the second row and you repeat what you just finished doing starting with the first row and reducing the first column. You start with the second row and you find the first non-zero element in that row. That is by definition at position 2 (index 1) or to the right of that because of the reduction you already completed for column 1.
This is covered in the lectures and the instructions, right?
Since the 2nd and 3rd rows both start with [0 1 …], you can subtract the 3rd row from the 2nd row, and replace the 3rd row.
This gives [ 0 0 0.3 0.7] in the last row.
Then you multiply the last row by whatever will turn 0.3 into 1.0.
In fact, think of it this way: you are repeating the same thing you did with column 1, but you’re working with the matrix that starts at position (1,1) and goes down and to the right from that. Like you’re just excluding the first row and the first column. So it’s the exact same algorithm executed on the smaller matrix.
With above suggestions, my program did clear the basic testcases. But failing still with other testcases where matrics length is different.
Example :
A = [[ 24. 0. 43. -15.]
[ 32. -3. 6. -21.]
[-44. -2. 47. 10.]
[-15. -20. -22. -10.]] and B = [[-13. 0.]
[ 14. 2.]
[-37. -36.]
[ -2. -23.]]. Expected: [[ 1. 0. 1.79166667 -0.625 -0.54166667
0. ]
[ -0. 1. 17.11111111 0.33333333 -10.44444444
-0.66666667]
[ 0. 0. 1. -0.10517182 -0.5105866
-0.23325234]
[ 0. 0. 0. 1. -1.75616907
1.87539702]]. Got: [[ 1. 0. 1.79166667 -0.625 -0.54166667
0. ]
[ -0. 1. 17.11111111 0.33333333 -10.44444444
-0.66666667]
[ -0. 1. -62.91666667 8.75 30.41666667
18. ]
[ 0. 0. -8.05435074 1. 3.84390897
2.16546185]]
Could any of you reviwers / mentors, please review my coding approach once ?
We cannot directly see your code with that link. But notice what went wrong in the “Expected” versus “Got”: your values are wrong for the 3rd and 4th rows, starting at the second column. So play it out with pencil and paper and then compare that to how your code is working. That’s how debugging works: you have an example and the correct output. So why does your code not produce the same values?
Your reduction seems to have worked for the first column, but now you need to figure out why it did not work for the second column. What is different about that case?
Please do not post your code on the forum. That’s not allowed by the Code of Conduct.
If a mentor needs to see your code, we’ll contact you with instructions.
Thank you, I just removed the workbook link from my post above.