Home > Software design >  list of numpy array elements - ValueError: The truth value of an array with more than one element is
list of numpy array elements - ValueError: The truth value of an array with more than one element is

Time:05-16

I have a list that stores numpy arrays called Triangle

File "main.py", line 63, in triangleCase
    Triangle.remove(OB)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I also got

AttributeError: 'numpy.ndarray' object has no attribute 'append'

but I haven't been able to replicate this

Edit: the function:

def triangleCase():
  global Triangle, d
  OA = Triangle[-1]
  OB = Triangle[-2]
  OC = Triangle[-3]
  AB = OB - OA
  AO = - OA
  AC = OC - OA
  ABperp = tripleProd(AC,AB,AB) #(AC x AB) x AB
  ACperp = tripleProd(AB,AC,AC) #(AB x AC) x AC
  if dot(ABperp, AO) > 0: #RAB
    Triangle.remove(OC)
    d = ABperp
    return False
  if dot(ACperp,AO) > 0: #RAC
    Triangle.remove(OB)
    d = ACperp
    return False
  return True #RABC

One value of Triangle an d that give an error is:

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

Triangle: [array([-17.462565  ,  20.63435875,   0.        ]), array([31.69419203, -2.95257267,  0.        ]), array([ 13.97174987, -18.31107765,   0.        ]), array([-52.2462343 ,  32.56502419,   0.        ]), array([-8.3122833 , 64.51505875,  0.        ]), array([ -4.82763387, -49.86062945,   0.        ])]

d: [  8.3122833  -64.51505875  -0.        ]

CodePudding user response:

from your error I understand that you try to remove an element from an array with remove. However, you cannot do it for an array. You need to do it like this :

def triangleCase():
  global Triangle, d
  OA = Triangle[-1]
  OB = Triangle[-2]
  OC = Triangle[-3]
  AB = OB - OA
  AO = - OA
  AC = OC - OA
  ABperp = tripleProd(AC,AB,AB)
  ACperp = tripleProd(AB,AC,AC)
  if dot(ABperp, AO) > 0: #RAB
    Triangle.remove(OC)#Triangle=np.delete(Triangle,-3,0)
    d = ABperp
    return False
  if dot(ACperp,AO) > 0: #RAC
    Triangle.remove(OB)#Triangle=np.delete(Triangle,-2,0)
    d = ACperp
    return False
  return True #RABC
  • Related