Reputation: 495
EDIT: Alright, so, I was indeed stupid. I recommend to close this question as it does not bring anything to the table. Basically, I suck at basic thinking and math at 2am...
This is driving me insane. I'm trying to get the product of a matrix with a vector in python.
I have one 9x9 matrix a and one 1x9 vector b.
a = [[-0.03619046050233981, 0.01694804504223569, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.01694804504223569, -0.03619046050233981, 0.01694804504223569, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.01694804504223569, -0.03619046050233981, 0.01694804504223569, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.01694804504223569, -0.03619046050233981, 0.01694804504223569, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.01694804504223569, -0.03619046050233981, 0.01694804504223569, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.01694804504223569, -0.03619046050233981, 0.01694804504223569, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.01694804504223569, -0.03619046050233981, 0.01694804504223569, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.01694804504223569, -0.03619046050233981, 0.01694804504223569], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.04756713402738598, -0.06941534088216819]]
a = np.array(a)
b = [2.774218316317263e-09, 1.9240011547847137e-09, 1.3342489213593189e-09, 9.251229045630879e-10, 6.412369741504171e-10, 4.441595895415701e-10, 3.07211064663576e-10, 2.1185176007909776e-10, -4.172326584582343e-11]
b = np.array(b)
print(np.dot(a,b))
>>> array([-6.77921802e-11, 0.00000000e+00, 6.46234854e-27, 0.00000000e+00,
6.46234854e-27, 3.23117427e-27, -4.84676140e-27, -3.16751358e-12,
1.29734158e-11])
What I'm expecting:
...
2nd value: 0.01694805 * 2.77421832e-09 -0.03619046 * 1.92400115e-09 + 0.01694805 * 1.33424892e-09
>>> 2.15478530e-17
...
array([-6.77921695e-11, 2.15478530e-17, 1.47710395e-17, 1.02173204e-17,
7.13131671e-18, 4.91186255e-18, 3.40092760e-18, 2.36168005e-18,
-7.44835521e-19])
Here is the kicker:
c = array([[-0.03619046, 0.01694805, 0. , 0. , 0. ,
0. , 0. , 0. , 0. ],
[ 0.01694805, -0.03619046, 0.01694805, 0. , 0. ,
0. , 0. , 0. , 0. ],
[ 0. , 0.01694805, -0.03619046, 0.01694805, 0. ,
0. , 0. , 0. , 0. ],
[ 0. , 0. , 0.01694805, -0.03619046, 0.01694805,
0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0.01694805, -0.03619046,
0.01694805, 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0.01694805,
-0.03619046, 0.01694805, 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. ,
0.01694805, -0.03619046, 0.01694805, 0. ],
[ 0. , 0. , 0. , 0. , 0. ,
0. , 0.01694805, -0.03619046, 0.01694805],
[ 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0.04756713, -0.06941534]])
np.dot(c,b)
>>> array([-6.77921692e-11, 2.13353146e-17, 1.47955319e-17, 1.02587195e-17,
7.11069875e-18, 4.92530088e-18, 3.40667400e-18, -3.16751216e-12,
1.29734149e-11])
As you can see, c is an approximation of a with less significant numbers. Using it gives me my expected results-ish. Am I hitting some numerical artifact ? Or am I making a huge and stupid mistake?
Note that a @ b and np.matmul(a,b) encounter the same issue.
I'm using Python 3.7.0 and Numpy 1.15.0
Upvotes: 0
Views: 42
Reputation: 58721
It seems like you copy-pasted the numbers wrong.
print(np.dot(a, b))
print(np.dot(a[1], b))
print(
0.01694804504223569 * 2.774218316317263e-09
+ -0.03619046050233981 * 1.9240011547847137e-09
+ 0.01694804504223569 * 1.3342489213593189e-09
)
[-6.77921802e-11 0.00000000e+00 6.46234854e-27 0.00000000e+00
6.46234854e-27 3.23117427e-27 -4.84676140e-27 -3.16751358e-12
1.29734158e-11]
-6.462348535570529e-27
-6.462348535570529e-27
It's a bit weird that the np.dot(a, b)[1]
does not exactly equal np.dot(a[1], b)
, but with this magnitude it's not too surprising.
You could play with accupy's kdot
/fdot
(a project of mine) to see if those round-off defeating implementations do you any good.
Upvotes: 1