0

Hi there i have written some code for my physics studys and i am fairly new to python.

Can someone explain to me why

[code]while s < 3.05 and t < 1e-7 :[/code]

triggers

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

i googled for quite a while and searched stack but i have no idea what the problem is.

Wölki
  • 1
  • 1
  • i do not think it is a duplicate because what i am using is not an array. For some reason Python thinks it is an array. Probably i did something wrong but i have no idea what – Wölki Nov 19 '18 at 19:50
  • that's ok if `s` and `t` are scalars. Look at `s<3.05` alone. Does it make sense to apply a `while` or `and` to that result? – hpaulj Nov 19 '18 at 19:51
  • While `s` and `t` start off as scalars, something is change one or both into a `numpy` array. And as indicated in the duplicate, performing `if`, `while` and `and` operations on a boolean array doesn't work. You need to figure out, from the full error message. where the error occurs (which function), and figure out why `s` (or `t`) becomes an array (maybe `dt` is also an array, etc). – hpaulj Nov 19 '18 at 20:21
  • You are calling `Ek` with arrays. `E` is then an array. `v` is derived from that, as is `ds`. So on the next loop `s+ds` is also an array. `Ek` may work fine with scalar `E` and `p`, but it is not setup to work with arrays. – hpaulj Nov 19 '18 at 20:25
  • that is the problem. I have no idea what may cause this. File "/tmp/mozilla_woelki0/arrayerror.py", line 67, in Ek while s < 3.05 and t < 1e-7 : ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() This is everything i get as an error. I do not find where to get a grip at this. I highly appreciate you trying to help – Wölki Nov 19 '18 at 20:29
  • I've reopened this. While the duplicate explains the error message, it doesn't explain why it occurs in this code. And doesn't help this beginner debug the problem. https://stackoverflow.com/questions/10062954/valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous – hpaulj Nov 19 '18 at 20:33
  • thx for reopening. an instant duplicate would not have been a great start for me here. sadly i cannot use the reputation meter due to beeing new. – Wölki Nov 19 '18 at 20:47

1 Answers1

0

When I copy-n-paste the code if runs (thankfully) and produces the error message:

1231:~/mypy$ python3 stack53381357.py 
Traceback (most recent call last):
  File "stack53381357.py", line 105, in <module>
    Z = Ek(X, Y)
  File "stack53381357.py", line 67, in Ek
    while s < 3.05 and t < 1e-7 :
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Note that the full message does tell us where the problem is - in the Ek function at line 67. That is relevant information.

That confirms my comment - Ek is called with array arguments, and that propagates through the loop, changing s to an array.

When I add a print to the end of the Ek loop:

    print(type(s), s.shape)

I get, before the error:

<class 'numpy.ndarray'> (100, 100)

So, yes, s has changed from a scalar to a numpy array, the same shape as the X and Y arrays you passed to Ek.

Your 3 functions appear to have been written for scalar inputs. I'm not going to try to figure out how they should work with the meshgrid inputs. You could iterate over the pa and Ea elements. Or you could modify the functions so they do behave when given array values. At the very least the

while (s<3.05) and (t<1e-7):

has to be written to be meaningful when s is an array. There may be other problems with array inputs.

hpaulj
  • 221,503
  • 14
  • 230
  • 353