Python3 triggers an array error even tho i am not using an array. Probably my syntax is wrong but i do not...












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.










share|improve this question
























  • 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
















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.










share|improve this question
























  • 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














0












0








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.










share|improve this question















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.







python-3.x numpy matplotlib






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 19:51







Wölki

















asked Nov 19 '18 at 19:26









WölkiWölki

11




11












  • 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 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 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




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




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




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




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




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












1 Answer
1






active

oldest

votes


















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.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381357%2fpython3-triggers-an-array-error-even-tho-i-am-not-using-an-array-probably-my-sy%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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.






    share|improve this answer




























      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.






      share|improve this answer


























        0












        0








        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.






        share|improve this answer














        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 19 '18 at 20:45

























        answered Nov 19 '18 at 20:36









        hpauljhpaulj

        110k774141




        110k774141






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381357%2fpython3-triggers-an-array-error-even-tho-i-am-not-using-an-array-probably-my-sy%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            Npm cannot find a required file even through it is in the searched directory