Home > Back-end >  Error :The truth value of an array with more than one element is ambiguous
Error :The truth value of an array with more than one element is ambiguous

Time:05-02

In this program, I have encountered the error:

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

Given that the value of alpha in each iteration is only one number, why this error occurs.

import pandas as pd
import numpy as np
import math
from matplotlib import pyplot as plt


x_goal,y_goal=[0,0]
k_alpha,k_beta,k_rho =[32,-0.1,8]
init_x,init_y 
initPosition = [[-50 ,0,10],[50,0,20],[0,50,30],[0,-50,80],[-35 ,35,50],[40 ,35,40],[40 ,-35,90],[-35,-35,180]]
delta_t =0.001



for pos in initPosition:
    init_x,init_y,theta=pos
    
    delta_x = x_goal -init_x
    delta_y = y_goal -init_y
    
    rho =np.sqrt(pow(delta_x ,2) pow(delta_y ,2))
    alpha = -theta  math.atan2(delta_y,delta_x)
    beta = -theta-alpha
    
    v=k_rho*rho
    w=k_alpha*alpha k_beta*beta
    
    #print(delta_x,delta_y,rho,alpha,beta,v,w)
    print(alpha)
    
    while rho>1e-3:
        print(alpha)
        if alpha>-90 and alpha<=90:
            rho_dot,alpha_dot,beta_dot = [-k_rho*rho*math.cos(alpha),
                                  k_rho*math.sin(alpha)-k_alpha*alpha-k_beta*beta,-k_rho*math.sin(alpha)]
            
        if np.union1d((alpha>-180 and alpha<=-90),(alpha>90 and alpha<=180)):
            rho_dot,alpha_dot,beta_dot = [k_rho*rho*math.cos(alpha),-k_rho*math.sin(alpha) k_alpha*alpha k_beta*beta,k_rho*math.sin(alpha)]
            
            
        #new_rho = rho delta_t*rho_dot
        #rho = new_rho
        
        new_beta = beta delta_t*beta_dot
        beta = new_beta

        new_alpha = alpha delta_t*w
        alpha = new_alpha

        new_theta = theta delta_t*w
        theta =new_theta
        
        new_x=init_x delta_t*v*math.cos(theta)
        init_x =new_x
        new_y=init_y delta_t*v*math.sin(theta)
        init_y =new_y
            
    print(rho_dot,alpha_dot,beta_dot)
    plt.scatter(init_x,init_y,marker = (10,2,theta));
    plt.scatter(0,0,marker = (50,2));
    plt.xticks(np.arange(-50,60,10))
    plt.yticks(np.arange(-50,60,10))
plt.show()

CodePudding user response:

Whilst alpha is always one number, np.union1d((alpha>-180 and alpha<=-90),(alpha>90 and alpha<=180)) is a list which can have more than one element. You should change that line to be:

if any(i for i in np.union1d((alpha>-180 and alpha<=-90),(alpha>90 and alpha<=180))):

or to:

if all(i for i in np.union1d((alpha>-180 and alpha<=-90),(alpha>90 and alpha<=180))):

Depending on whether you want any elements in np.union1d((alpha>-180 and alpha<=-90),(alpha>90 and alpha<=180)) to be true or all of them for the condition to be satisfied.

Also, I think you need to add an:

else:
    break

clause after that as otherwise you become stuck in an infinite loop.

CodePudding user response:

You can fix this error by changing the offending line (the one with union1d) to this:

if np.union1d((alpha>-180 and alpha<=-90),(alpha>90 and alpha<=180)).any():

Internally, the any() method will iterate through the elements of the array result from union1d (even if there is only one element) to test whether alpha is either between -180 and -90 or between 90 and 180 (or, in the general case, both, though that would be arithmetically possible in your particular case).

For the motivation of numpy developers in deciding that your original usage should generate an error, please see this answser: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().

  • Related