[英]Numpy product of a vector and it's transpose
I have a N x N complex NumPy array U_0 and I have to do manipulation with it :我有一个 N x N 复杂的 NumPy 数组 U_0,我必须对它进行操作:
First, how can I increase the array with zero efficiently ?首先,如何有效地增加零数组? I can simply copy it into an np.zeros((2N-1, 2N-1)) but maybe you guys know a better method.
我可以简单地将它复制到 np.zeros((2N-1, 2N-1)) 但也许你们知道更好的方法。 Thanks to Alexander Riedel for answer this question with the solution of numpy.pad
感谢 Alexander Riedel 用 numpy.pad 的解决方案回答了这个问题
Second, I tried with其次,我尝试过
b = np.array([1,2,3])
I saw on previous post to transpose a 1D vector you can do我在上一篇文章中看到了转置你可以做的一维向量
b_T = b[..., None]
# or
b_T = np.atleast_2d(b).T
but when I try b_T.dot(b)
I get shapes (3,1) and (3,) not aligned: 1 (dim 1) != 3 (dim 0)
.但是当我尝试
b_T.dot(b)
时,我得到shapes (3,1) and (3,) not aligned: 1 (dim 1) != 3 (dim 0)
。 I don't know how to get b into a shape of (1,3) instead of (3,).我不知道如何将 b 变成 (1,3) 而不是 (3,) 的形状。
Thanks谢谢
You can use the expand_dims function to do what you want.你可以使用expand_dims函数来做你想做的事。 The problem here is that numpy does not consider a shape (3, 1) and a (3, ) equivalent.
这里的问题是 numpy 不考虑形状 (3, 1) 和 (3, ) 等价物。 Alternatively look into the
matrix
type或者查看
matrix
类型
Filling the array with zeros, as the commenters pointed out is also the answer to your first question.正如评论者指出的那样,用零填充数组也是您第一个问题的答案。 If that is not efficient enough, look into using sparse matrices from scipy, maybe they have the features you're looking for.
如果这不够有效,请考虑使用 scipy 中的稀疏矩阵,也许它们具有您正在寻找的功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.