0

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()
4
  • I don't know pylab so.. what exactly is in r2 and 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 ..? Commented Oct 27, 2011 at 15:09
  • r2 is r^2 of an ellipsis... g(X,Y) should be an mgrid like but with values evaluated on every point of X,Y. Commented Oct 27, 2011 at 15:12
  • Please tell us what object mgrid is: print repr(mgrid) for a small example. Commented Oct 27, 2011 at 15:20
  • mgrid is: docs.scipy.org/doc/numpy/reference/generated/numpy.mgrid.html Commented Oct 27, 2011 at 15:45

2 Answers 2

3

The expression r2 > 0.04 evaluates to a NumPy array of Boolean values, so you can't use it in an if statement. You can have it automatically reinterpreted as numbers, though:

def q(x,y):
    r2 = (x/2.)**2 + (2.0*y)**2
    return 1.5 * (r2 <= 0.04)
Sign up to request clarification or add additional context in comments.

3 Comments

It is not the most general solution, but it solves the problem for this specific problem. Do you know a way of avoiding this for more cases (more if's else's etc.)
@J.C.Leitão: The specifics of how to vectorize a particular piece of code depend on the specifics of the code. In this particular example, it is comparitively simple, in other cases it is harder and sometimes it is impossible. The given technique can usually be extended to multiple if statements, possibly by using the & and | operators or partial assigment r2[r2 > 0.04] = ....
Ok. Thank you Sven for the idea and knowledge shared.
0

You may use map to apply your function to each couple of elements.

3 Comments

In the given context, this answer is wrong. X and Y are two-dimensional arrays, so the zip() would produce pairs of rows, so the problem still isn't solved. Moreover, the whole point of NumPy is to avoid this kind of Python loop over all elements.
Ok, I removed the non-working code. But the spirit of my answer was just to suggest 'map' as a possible way of resolving the issue; it didn't meant to be the most efficient solution.
Removed my downvote because the answer is no longer wrong. Honsetly, I think it's still not particularly helpful, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.