Reputation: 420
I have been trying to determine the source of my error for this simple script which takes a numpy.array as input and produces a new lattice from the dataset
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)
When I try vary my lattice or even use stripped down examples and multiple different ways
like
print numpy.cross(lat[(1)%3],lat[(2)%3],axis=0)
or
print numpy.cross(lat[(1)%3],lat[(2)%3])
I just get this error
ValueError: rollaxis: axis (0) must be >=0 and < 0
What is rollaxis doing in this problem and, what is it doing in the capacity of this problem and what exactly am I setting when I assign a value. How do I fix this error (how can something be less than 1 AND great than or equal to 1?)
My test matrix has been:
[['4.7480001450' '-2.3740000725' '0.0000000000']
['0.0000000000' '4.1118887427' '0.0000000000']
['0.0000000000' '0.0000000000' '15.4790000916']]
Upvotes: 1
Views: 1222
Reputation: 231345
With the newer version of np.cross
(which uses rollaxis instead of swap), I can produce this error with:
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
That is, when passing a scalar (or 0d) array to cross
you get this rollaxis
error. So make sure that you are passing vectors to cross
(i.e. at least 1d arrays).
For example if latice
is 2d
for i,a in enumerate(lattice):
print a
b[i]=numpy.cross(a[(i+1)%3],a[(i+2)%3],axis=0)
then a
is 1d, and a[1]
is a scalar.
Is this what your goal?
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]])
I was puzzled about your use of %3
, but from your comment you are trying to do b[0,:] = cross(lat[1,:], lat[2,:])
etc.
But cross
itself is doing that kind of permuted pairing. From the older 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]
Upvotes: 2