I have this function:
def q(x,y):
r2 = (x/2.)**2 + (2.0*y)**2
if (r2 > 0.04):
return 0.
else:
return 1.5
and I want to call it using something like
from pylab import *
dl=0.025
X, Y = mgrid[-0.5:0.5:dl, -0.5:0.5:dl]
g(X,Y)
but obviously this gives an error in the comparison.
Can this be done without having to make a for cycle for X and Y? Because if I want to call q(x,y) for two doubles, the for cycle must be reimplemented for that case...
Edit: (Adding this to the question as it is too long for a comment and not an answer, but it may help others answer.)
It appears pylab.mgrid is the same as numpy.mgrid.
Adjusted for numpy, this code
import numpy
def q(x,y):
r2 = (x/2.)**2 + (2.0*y)**2
if (r2 > 0.04):
return 0.
else:
return 1.5
dl=0.025
X, Y = numpy.mgrid[-0.5:0.5:dl, -0.5:0.5:dl]
q(X,Y)
gives this error
if (r2 > 0.04):
ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()
r2and what do you want to do with it? If it is an array - what is it you want to compare? All elements must satisfy the condition or ..?mgridis:print repr(mgrid)for a small example.