C1W2_Assignment Gaussian Elimination exercise 1

I get this error and I don’t know how to fix it

TypeError Traceback (most recent call last)
1 A = np.array([[1,2,3],[0,0,0], [0,0,5]])
2 B = np.array([[1], [2], [4]])
----> 3 reduced_row_echelon_form(A,B)

in reduced_row_echelon_form(A, B)
49 # If there is a non-zero pivot
—> 50 if not np.isclose(index, 0, atol = 1e-5):
51 # Swap rows if a non-zero entry is found below the current row
52 M = swap_rows(M, column_index, index)

<array_function internals> in isclose(*args, **kwargs)

/opt/conda/lib/python3.8/site-packages/numpy/core/numeric.py in isclose(a, b, rtol, atol, equal_nan)
2360 y = array(y, dtype=dt, copy=False, subok=True)
→ 2362 xfin = isfinite(x)
2363 yfin = isfinite(y)
2364 if all(xfin) and all(yfin):

TypeError: ufunc ‘isfinite’ not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ‘‘safe’’

It looks like the value index in that np.isclose expression is Infinity. That could happen if you divided by zero in computing index. Also note that it doesn’t make sense to compare an index value to zero, right? What you want to compare is the value at that index position to zero to see if your pivot value is zero

In other words the evidence seems to show that there are several levels of errors in your code.

thank you, I fixed it but now I have another problem


An exception was thrown while running your function: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().
Input matrix:
{‘A’: array([[ 1., 5., 6.],
[ 3., 1., 4.],
[ 2., -4., -2.]]), ‘B’: array([[ 9.],
[ 4.],

btw I pasted this 2 lines of code in a function because without it was the same exception but there were matrice and a vector only with zeros:

if not np.any(B):
    return M

Ok, that means you have some incorrect logic in your code. At some point you must be saying something like

if myVar:

where myVar is an array instead of a scalar value. That statement doesn’t make sense, which is what that error message is telling you.

I’m not sure where your added statement would be useful. Note that the “if” condition there will only be True if all the elements of B are zero, right? And the way I wrote this algorithm, it never modifies B: that is just an input that we use to create the augmented matrix M.

But note that there is one test case in which both A and B are all zeros. That must be the case where that logic is “saving” you. But I think there might be a better (more general) way to handle the case of all zero rows that occur during the reduction process. My suggestion would be to take another careful look at your logic with that thought in mind.

1 Like