Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

#1
December 31st, 2012, 06:06 AM
 amanc
Registered User

Join Date: Dec 2012
Posts: 2
Time spent in forums: 33 m
Reputation Power: 0
ValueError: The truth value of an array with more than one element is ambiguous.

Hello, so I have a function:

Code:
```disks = array([[ 0.31730234,  0.73662906],
[ 0.54488759,  0.09462212],
[ 0.07500703,  0.36148366],
[ 0.33200281,  0.04550565],
[ 0.3420866 ,  0.9425797 ],
[ 0.36115391,  0.16670599],
[ 0.95586938,  0.52599398],
[ 0.13707665,  0.6574444 ],
[ 0.77766138,  0.56875582],
[ 0.79618595,  0.7139309 ]])

def overlap(d1,d2,r): #d1 is disk 1, d2 is disk 2 in array
distance = ((d1[0]-d2[0])**2 + (d1[1]-d2[1])**2)**0.5
if distance < 2*r:
return True
else:
return False

def touch_left_wall(d1,r):
if d1[0] - r <= 0:
return True
else:
return False

for d1 in disks:
for d2 in disks:
if overlap(d1,d2,r):
if touch_left_wall(d1,r):
clusters.append([d1,d2,True,False])```

A cluster is that array: [float, float, boolean, boolean]

I have another function:
Code:
```def joincluster(c1,c2):
if any(x in c1 for x in c2):
c1 = c1 + c2```

Where c1 and c2 are clusters. For some reason, everytime I try and run this I get this error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Anyone know why? I've tried a lot of things, like making the numpy arrays lists and such but nothing is working.

#2
December 31st, 2012, 07:49 AM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 3,361
Time spent in forums: 1 Month 2 Weeks 3 Days 10 h 11 sec
Reputation Power: 383
Please post sufficient information to duplicate the problem.

"A cluster is that array: [float, float, boolean, boolean]"

array.array does not store mixed type.
numpy.array does not store mixed type.

You've done (equivalently)

from numpy import *

Code:
```>>> from numpy import *
>>> A=[pi,exp(1),True,False]  # float, float, Boolean, Boolean
>>> any(x in A for x in A)
True
>>> ```

I'd compute distance like this:
>>> A=disks[0]-disks[1]
>>> A.dot(A)
0.46396795702572596
>>> sqrt(A.dot(A))
0.68115193387799022

(Whereas you wrote out the indexes. Why bother with numpy if you subvert it?)

I find "if true return true else return false" repulsive.
Code:
```def overlap(d1,d2,r): #d1 is disk 1, d2 is disk 2 in array
v = d1[0]-d2[0]
return v.dot(v) < 4*r*r

def touch_left_wall(d1,r):
return d1[0] <= r```
__________________
[code]Code tags[/code] are essential for python code!

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > ValueError: The truth value of an array with more than one element is ambiguous.