samedi 6 mai 2017

2D array into if statement without loop - Python

I have defined a function which includes an if statement. I then want to pass in the X array in the function, which is a 2D array. I tried using np.all as below, but although it gives no error, it sets ALL the X values to "if abs(np.all(x)) < L:". How would I be able to pass a 2D array (X) into the function properly?

x = np.arange(-10.,15.+1.,1.)
y = np.arange(-4.,4.+0.1,0.1)
eps = 0.1
L = 2.
k = np.pi/(2.*L)

def q2(x):
    if abs(np.all(x)) < L:
        return (((-3.*eps*np.cos(k*x))+(k*(np.sin(k*x)-np.exp(3.*eps*(x-L)))))/((9.*eps**2.) + k**2.))
    if np.all(x) > L:
        return 0.
    if np.all(x) < -L:
        return (-k*(1.+np.exp2(-6.*eps*L))*np.exp(3.*eps*(x+L)))/((9.*eps**2.) + k**2.)

def u2(x,y):
    return 0.5*q2(x)*(y**2. - 3.)*np.exp(-0.25*y**2.)

X,Y = np.meshgrid(x,y)
vel_x=u2(X,Y)

Aucun commentaire:

Enregistrer un commentaire