在 NumPy 中是否有一种通用的方法来矢量化这些操作?
In [2]: N = 8
In [3]: ll = np.arange(8)
In [4]: arr = np.zeros(ll.shape + (2, 2))
In [5]: ll.shape
Out[5]: (8,)
In [6]: arr.shape
Out[6]: (8, 2, 2)
In [7]: for ii in range(N):
...: arr[ii, :, :] = np.array(...) # 2 x 2 array function of ll[ii]
如果该函数是对 ll 的线性运算,那么这将是微不足道的,但是在一般情况下有没有办法做到这一点?举个例子:
In [8]: for ii in range(N):
...: arr[ii, :, :] = np.array([
...: [np.cos(ll[ii]) - 1, 0],
...: [np.sin(ll[ii]), np.cos(ll[ii]) ** 2]
...: ])