Reputation: 7504
I've some difficulty in matching what Numpy expects when performing the dot product and vector representation in linear algebra, in term of shapes.
Let's say I've a matrix and two column vectors represented by Numpy arrays:
import numpy as np
A = np.array([[1,2],
[3,1],
[-5,2]])
x = np.array([[0],
[2]])
y = np.array([[-2],
[0],
[3]])
and I want to compute Axꞏy
, Ax
being a matrix multiplication and ꞏ
being the dot product. This doesn't work:
# a = Axꞏy
a = (A @ x).dot(y)
shapes (3,1) and (3,1) not aligned: 1 (dim 1) != 3 (dim 0)
Ax
:
[[4],
[2],
[4]]
is indeed a column vector and the dot product of the two column vectors is the scalar: -8+0+12=4
, this is the result I'm expecting.
What is the correct operation or reshaping required, using vectors as they are defined?
Upvotes: 3
Views: 96
Reputation: 260825
You can't compute a dot product between two (3, 1)
arrays, a dot product A @ B
is only valid if the shape of A
and B
are (n, k)
and (k, m)
.
It looks like you want:
(A@x).T @ y
Output: [[4]]
Or as scalar:
((A@x).T @ y).item()
Output: 4
NB. in the context of 2D arrays @
and dot
are equivalent.
Other operations that would have been valid given the shapes:
(A@x) @ y.T
array([[-8, 0, 12],
[-4, 0, 6],
[-8, 0, 12]])
(A.T*x) @ y
array([[0],
[4]])
Upvotes: 4