使用较新版本的np.cross
(使用rollaxis而不是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 product\n"
/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)
thena
是 1d,并且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]