Multiplying column and row vectors in Numpy
np.multiply
only does element by element multiplication. You want an outer product. Use np.outer
:
np.outer(np.arccos(xxa), nd)
If you want to use NumPy similar to MATLAB, you have to make sure that your arrays have the right shape. You can check the shape of any NumPy array with arrayname.shape
and because your array na
has shape (4,)
instead of (4,1)
, the transpose
method is effectless and multiply
calculates the dot product. Use arrayname.reshape(N+1,1)
resp. arrayname.reshape(1,N+1)
to transform your arrays:
import numpy as np
n = range(0,N+1)
pi = np.pi
xx = np.cos(np.multiply(pi / float(N), n))
xxa = np.asarray(xx).reshape(N+1,1)
na = np.asarray(n).reshape(N+1,1)
nd = np.transpose(na)
T = np.cos(np.multiply(np.arccos(xxa),nd))
Since Python 3.5, you can use the @
operator for matrix multiplication. So it's a walkover to get code that's very similar to MATLAB:
import numpy as np
n = np.arange(N + 1).reshape(N + 1, 1)
xx = np.cos(np.pi * n / N)
T = np.cos(np.arccos(xx) @ n.T)
Here n.T
denotes the transpose of n.