在Python阵列的矩阵乘法 [英] matrix multiplication of arrays in python
本文介绍了在Python阵列的矩阵乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我觉得自己有点傻问这个,但我似乎无法找到答案
在numpy的使用数组我想乘1X3阵列的3X1阵列,并得到一个3X3阵列作为一个结果,但由于点函数总是将第一个元素的列向量,第二个作为一个行向量我不能似乎要得到它的工作,我必须使用,因此矩阵。
A =阵列([1,2,3])
打印艾=,点(A,A)
打印A2mat =,点(A.transpose(),A)
打印A3mat =,点(A,A.transpose())
U2 =垫([UX,UY,UZ])
打印u2mat =,u2.transpose()* U2
和输出:
艾= 14
A2mat = 14
A3mat = 14
u2mat =
[0 0 0]
[0 0. 0.]
[0. 0. 1]
解决方案
的 np.outer
是内置做到这一点:
A =阵列([1,2,3])
打印外,np.outer(A,A)
(转
不起作用,因为 AT
是完全一样的一维数组:
打印A.shape,A.T.shape,A [:,np.newaxis] .shape
>>> ((3,),(3,),(3,1))
)
I feel a bit silly asking this, but I can't seem to find the answer
Using arrays in Numpy I want to multiply a 3X1 array by 1X3 array and get a 3X3 array as a results, but because dot function always treats the first element as a column vector and the second as a row vector I can' seem to get it to work, I have to therefore use matrices.
A=array([1,2,3])
print "Amat=",dot(A,A)
print "A2mat=",dot(A.transpose(),A)
print "A3mat=",dot(A,A.transpose())
u2=mat([ux,uy,uz])
print "u2mat=", u2.transpose()*u2
And the outputs:
Amat= 14
A2mat= 14
A3mat= 14
u2mat=
[[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 1.]]
解决方案
np.outer is a builtin to do that:
A = array([1,2,3])
print "outer:", np.outer( A, A )
(transpose
doesn't work because A.T
is exactly the same as A for 1d arrays:
print A.shape, A.T.shape, A[:,np.newaxis].shape
>>> ( (3,), (3,), (3, 1) )
)
这篇关于在Python阵列的矩阵乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文