Numpy matrix multiplication instability across rows

Viewed 280

I am multiplying two float64 matrices with the following values:

import numpy as np


# 4x5 matrix with identical columns.
x = np.zeros((4, 5,), dtype=np.float64)
x[1] = 1
x[3] = -3

w = np.array([1, 1, -1, 1 / 3], dtype=np.float64)

# The result should be an array of size 5 with equal values.
result = np.matmul(w, x)

print(x)

>>> 
[[ 0.  0.  0.  0.  0.]
 [ 1.  1.  1.  1.  1.]
 [ 0.  0.  0.  0.  0.]
 [-3. -3. -3. -3. -3.]]

print(w)

>>> [ 1.          1.         -1.          0.33333333]

print(result)

>>> [5.55111512e-17 5.55111512e-17 5.55111512e-17 5.55111512e-17 0.00000000e+00]

The result array should contain identical values, since each item is a dot product of the w array with an identical column. However, the last item is 0.0 unlike the other values which are very close to 0. This has a large effect over calculations downstream.

I am guessing this has something to do with the value 1/3, since replacing it with 1/2 gives a stable result. How can this instability be solved though?

Additional info since the problem doens't reproduce on all machines

I am using numpy 1.18.2 and Python 3.7.3, on MacOS. The problem reproduces on another machine which runs Ubuntu with the same Python and numpy versions.

1 Answers

Changed the array sizes and changed which rows are set to 1 and -3 & 1/3 on a i7 Mac running macOS 11.6 with python 3.9.7 and numpy 1.21.2 The 5.5e-17 values only occur when row 0 is 1 and row 2 is -3 and when row 1 is 1 and row 3 is -3. The number of 5.5e-17 and 0 values changes depending on the number of columns. Some number of columns such as 16 produce all 5.5e-17 values. Same behavior when -3 & 1/3 is replaced with values that are not a factor of 2 such as 12 & 1/12 though 5.5e-17 sometimes changes to a different e-17 value.

Related