0
votes

I am new in Python and I am facing the following error in the code:

import numpy as np
A = np.empty([2, 2], dtype = float)
Z = np.empty([2, 2], dtype = float)
Z = [[2.0, 0.0],  [0.0, 3.0]]
A = [[1, -1], [1, -2]]

print(np.divide(A, np.power(Z, 2)))

These code gives the following result:

[[ 0.25  -inf], [inf -0.22222222]]

But I know, using a calculator, that the correct answer should be:

[[ 0.25   -0.25], [ 0.111111 -0.22222222]]

In short, thhis operation gives me the following error: RuntimeWarning: divide by zero encountered in true_divide

1
How did you get the output from the calculator? seems the calculator is calculating something else. - Ehsan
When you use Z = [[2.0, 0.0], [0.0, 3.0]] and A = [[1, -1], [1, -2]] you are setting A and Z to type list. I'd rather just initialize the array's using np.array([[...], [...]], dtype=float) - Michael Richardson
Hi @MichaelRichardson. I followed your suggestion, but the error remains. - Cosme Rodolfo Santos

1 Answers

0
votes

You are element-wise dividing A by Z^2:

A:

[[1, -1], [1, -2]]

Z^2:

[[4.0, 0.0],  [0.0, 9.0]]

A/Z^2:

[[ 0.25  -inf], [inf -0.22222222]]

The output is correct. You receive the warning (and not an error) of division by 0 because there are 0s in Z^2 in denominator that result in infs in output. Your calculator output is not what your code does.