Reputation: 81
I have a 4x4 matrix and 4x1 vector. If i calculate dot product by hand (in excel) i get different values to the numpy result. I expect it has to do with float values, but a difference of 6.7E-3 for example seems too large to just be float error? What am i missing?
Isolated code result (see below):
[-3.24218399e-06 1.73591630e-04 -3.49611749e+04 1.90697291e+05]
With handcalculation (excel):
[-1.04791731E-11 7.08581638E-10 -3.49611670E+04 1.90697275E+05]
The input values for the matrix are pulled from code, where i do the same evaluation. There, result is:
[-2.09037901e-04 6.77221033e-03 -3.49612277e+04 1.90697438e+05]
isolated input values:
import numpy as np
arrX1 = np.array([
[-2.18181817e+01, 1.78512395e+03,-5.84222383e+04, 7.43555757e+05],
[ 8.92561977e+02,-6.81592780e+04, 2.07133390e+06,-2.43345520e+07],
[-9.73703971e+03, 6.90444632e+05,-1.96993992e+07, 2.21223199e+08],
[ 3.09814899e+04,-2.02787933e+06, 5.53057997e+07,-6.03335995e+08]],
dtype=np.float64)
arrX2 = np.array([0,-1.97479339E+00,-1.20681818E-01,-4.74107143E-03],dtype=np.float64)
print (np.dot(arrX1, arrX2))
#-> [-3.24218399e-06 1.73591630e-04 -3.49611749e+04 1.90697291e+05]
Upvotes: 0
Views: 150
Reputation: 16174
at a guess this is because you're pulling your values out from Excel with too little precision. the values in your question only have 9 significant figures, while the 64 bit floats used in Excel and that you're requesting in Numpy are good to about 15 digits.
redoing the calculation with Python's arbitrary precision Decimal
s gives me something very close to Numpy's answer:
from decimal import Decimal as D, getcontext
x = [1.78512395e+03,-5.84222383e+04, 7.43555757e+05]
y = [-1.97479339E+00,-1.20681818E-01,-4.74107143E-03]
# too much precision please
getcontext().prec = 50
sum(D(x) * D(y) for x, y in zip(x, y))
gets within ~4e-13 of the value from Numpy, which seems reasonable given the scale of the values involved.
np.allclose
can be good to check whether things are relatively close, but has relatively loose default bounds. if I redo the spreadsheet calculation with the numbers you gave, then allclose
says everything is consistent
Upvotes: 1