Python中numpy叉积的滚动错误



我一直在试图确定这个简单脚本的错误来源,它需要一个numpy。数组作为输入,并从数据集

生成一个新的格。
def reciprocalLat(lattice):
    for i,a in enumerate(lattice):
    print a
        b[i]=numpy.cross(a[(i+1)%3],a[(i+2)%3],axis=0)
#/numpy.dot(a[i],numpy.cross(a[(i+1)%3],a[(i+1)%3]),0)

当我尝试改变我的格子,甚至使用剥离的例子和多种不同的方式

print numpy.cross(lat[(1)%3],lat[(2)%3],axis=0)

print numpy.cross(lat[(1)%3],lat[(2)%3])

我得到了这个错误

ValueError: rollaxis: axis (0) must be >=0 and < 0

在这个问题中rollaxis是做什么的,在这个问题中它做了什么,当我赋值的时候我设置了什么。如何修复此错误(如何可以小于1且大于或等于1?)

我的测试矩阵是:

[['4.7480001450' '-2.3740000725' '0.0000000000']
 ['0.0000000000' '4.1118887427' '0.0000000000']
 ['0.0000000000' '0.0000000000' '15.4790000916']]

使用较新版本的np.cross(使用rolllaxis而不是swap),我可以使用以下命令产生此错误:

In [663]: np_cross.cross(lat[0,0],lat[0,1],axis=0)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-663-ee756043fbb9> in <module>()
----> 1 np_cross.cross(lat[0,0],lat[0,1],axis=0)
/home/paul/mypy/np_cross.py in cross(a, b, axisa, axisb, axisc, axis)
     96     b = asarray(b)
     97     # Move working axis to the end of the shape
---> 98     a = rollaxis(a, axisa, a.ndim)
     99     b = rollaxis(b, axisb, b.ndim)
    100     msg = ("incompatible dimensions for cross productn"
/usr/lib/python3/dist-packages/numpy/core/numeric.py in rollaxis(a, axis, start)
   1340     msg = 'rollaxis: %s (%d) must be >=0 and < %d'
   1341     if not (0 <= axis < n):
-> 1342         raise ValueError(msg % ('axis', axis, n))
   1343     if not (0 <= start < n+1):
   1344         raise ValueError(msg % ('start', start, n+1))
ValueError: rollaxis: axis (0) must be >=0 and < 0

也就是说,当传递一个标量(或0d)数组给cross时,你会得到这个rollaxis错误。因此,请确保您将向量传递给cross(即至少1d数组)。

例如latice为2d

for i,a in enumerate(lattice):
    print a
    b[i]=numpy.cross(a[(i+1)%3],a[(i+2)%3],axis=0)

a为一维,a[1]为标量。


这是你的目标吗?

In [675]: lat
Out[675]: 
array([[  4. ,  -2.3,   0. ],
       [  0. ,   4.1,   0. ],
       [  0. ,   0. ,  15. ]])
In [676]: np.vstack([np_cross.cross(lat[(i+1)%3],lat[(i+2)%3]) for i,a in enumerate(lat)])
Out[676]: 
array([[ 61.5,   0. ,   0. ],
       [ 34.5,  60. ,  -0. ],
       [ -0. ,   0. ,  16.4]])

我对你使用%3感到困惑,但从你的评论中你试图做b[0,:] = cross(lat[1,:], lat[2,:])等。

但是cross本身正在进行这种排列配对。从旧的cross:

        x = a[1]*b[2] - a[2]*b[1]
        y = a[2]*b[0] - a[0]*b[2]
        z = a[0]*b[1] - a[1]*b[0]

最新更新