Np.zeros(4), a column vector or a row vector?

Is this vector a column vector or a row vector?

The two are quite different in quantum mechanics. If it is a column vector, a matrix can act on it from the left but not from the right, and vice versa if it is a row vector.

It is a one-dimensional array. Numpy arrays do not have a set interpretation as vectors, matrices, tensors or any other such objects.

Numpy arrays do implement a “matrix multiplication” operator using @. It’s not hard to test the semantics yourself:

>>> import numpy
>>> a = numpy.array((0, 1, 2, 3))
>>> b = numpy.ones((4, 4))
>>> a
array([0, 1, 2, 3])
>>> b
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]])
>>> a @ b
array([6., 6., 6., 6.])
>>> b @ a
array([6., 6., 6., 6.])
>>> c = a.reshape((4, 1))
>>> c @ b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 4 is different from 1)
>>> b @ c
array([[6.],
       [6.],
       [6.],
       [6.]])
>>> d = a.reshape((1, 4))
>>> d @ b
array([[6., 6., 6., 6.]])
>>> b @ d
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 1 is different from 4)
>>> 
1 Like

I do not know why

b @  a 

is ok, but

c @ b 

is not. If ‘a’ can be interpreted as a 4 times1 vector, why cannot ‘c’ be interpreted as a 1 times 4 vector?

See the notes here.

b @ a works due to the fourth note. a is 1-D. It has shape (4,). Matrix multiplication promotes it to 2-D with shape (4,1) and this it allows it to be multiplied by a 2-D of shape (4,4), like b.

For c @ b, the first note applies, since c is 2-D, with shape (4,1), and so is b, with shape (4,4). However, matrix multiplication doesn’t allow those shapes to be multiplied in that order.

2 Likes