3

I have a very simple function, as shown below

def new_price(A, B, x):
    return np.linalg.inv(A @ B) @ x

These are the inputs I give it

A = np.array([
    [2, 0, 1, 0],
    [1, 1, 1, 1],
    [0, 0, 0, 10]
]) 

B = np.array([
    [3, 3, 3],
    [2, 0, 8],
    [0, 5, 3],
    [0, 0, 10] 
])

x = np.array([ 84, 149, 500])

This returns the array [ 1. 3. 5.]. But, when I make the following equality check, it returns False

v1 = new_price(A, B, x)
v2 = np.array([1.0, 3.0, 5.0])
np.array_equal(new_price(A, B, [ 84, 149, 500]), np.array([1.0, 3.0, 5.0]))

I checked and the shapes and the types of both the arrays are same. What am I missing here?

ironstein
  • 416
  • 8
  • 23

1 Answers1

10

The are not exactly equal:

>>> new_price(A, B, [ 84, 149, 500]) -  np.array([1, 3, 5])
array([  2.84217094e-14,  -1.42108547e-14,   0.00000000e+00])

Better use np.allclose():

>>> np.allclose(new_price(A, B, [ 84, 149, 500]), np.array([1.0, 3.0, 5.0]))
True

Returns True if two arrays are element-wise equal within a tolerance.

You can adjust the relative and absolute tolerance.

Still true for really small values:

>>> np.allclose(new_price(A, B, [ 84, 149, 500]), np.array([1.0, 3.0, 5.0]),
                atol=1e-13, rtol=1e-14)
True

Found the limit:

>>> np.allclose(new_price(A, B, [ 84, 149, 500]), np.array([1.0, 3.0, 5.0]),
                atol=1e-14, rtol=1e-14)
False
Mike Müller
  • 82,630
  • 20
  • 166
  • 161