multiple values of a variable in same equation











up vote
2
down vote

favorite












A = [18.0,10.0]; B = [13.0,15.0]; C = [10.5,12.0];



these are the variables and think about function like



def hlf(A,B,C):
return A**(-1.0/2.0)-0.2*B-43+C
print "T:"
hlf(A,B,C)


Firstly, I want to use first values of the A B and C in the equation. After I want to use second values. How can I do this ?










share|improve this question




























    up vote
    2
    down vote

    favorite












    A = [18.0,10.0]; B = [13.0,15.0]; C = [10.5,12.0];



    these are the variables and think about function like



    def hlf(A,B,C):
    return A**(-1.0/2.0)-0.2*B-43+C
    print "T:"
    hlf(A,B,C)


    Firstly, I want to use first values of the A B and C in the equation. After I want to use second values. How can I do this ?










    share|improve this question


























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      A = [18.0,10.0]; B = [13.0,15.0]; C = [10.5,12.0];



      these are the variables and think about function like



      def hlf(A,B,C):
      return A**(-1.0/2.0)-0.2*B-43+C
      print "T:"
      hlf(A,B,C)


      Firstly, I want to use first values of the A B and C in the equation. After I want to use second values. How can I do this ?










      share|improve this question















      A = [18.0,10.0]; B = [13.0,15.0]; C = [10.5,12.0];



      these are the variables and think about function like



      def hlf(A,B,C):
      return A**(-1.0/2.0)-0.2*B-43+C
      print "T:"
      hlf(A,B,C)


      Firstly, I want to use first values of the A B and C in the equation. After I want to use second values. How can I do this ?







      python python-2.7






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 11:18









      jpp

      81.4k194795




      81.4k194795










      asked Nov 8 at 11:05









      Ttys

      164




      164
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted











          map + list



          Note map can take multiple iterable arguments:



          res = map(hlf, A, B, C)

          [-34.86429773960448, -33.68377223398316]


          In Python 2.7, map returns a list. In Python 3.x map returns an iterator, so you can either iterate lazily or exhaust via list, i.e. list(map(hfl, A, B, C)).



          Reference:




          map(function, iterable, ...)



          ...If additional iterable arguments are passed, function must
          take that many arguments and is applied to the items from all
          iterables in parallel.





          zip + list comprehension



          You can use zip within a list comprehension. For clarity, you should avoid naming your arguments the same as your variables.



          A = [18.0,10.0]; B = [13.0,15.0]; C = [10.5,12.0]; 

          def hlf(x, y, z):
          return x**(-1.0/2.0) - 0.2*y - 43 + z

          res = [hlf(*vars) for vars in zip(A, B, C)]

          [-34.86429773960448, -33.68377223398316]





          share|improve this answer






























            up vote
            1
            down vote













            Vectorize with Numpy. Best Performace



            Normally its much better try to vectorize this kind of operations with numpy, because the best performance results. When you vectorize instead to use a loop, you are using all your cores, and its the fastest solution. You should vectorize the operation with numpy. Something like this:



            import numpy as np

            A = [18.0,10.0]; B = [13.0,15.0]; C = [10.5,12.0];
            a = np.array(A)
            b = np.array(B)
            c = np.array(C)


            And now your function with the new vectors like arguments:



            def hlf(a_vector,b_vector,c_vector):
            return a_vector**(-1.0/2.0)-0.2*b_vector-43+c_vector


            And finally call your new function vectorized:



            print (hlf(a_vector = a,b_vector = b,c_vector = c))


            Output:



            >>> array([-34.86429774, -33.68377223])





            share|improve this answer






























              up vote
              -1
              down vote













              If you want to keep your function as is, you should call it N times with:



              for i in range(N):
              result = hlf(A[i], B[i], C[i])
              print(result)


              Another interesting method is to make a generator with your function:



              A = [18.0,10.0]
              B = [13.0,15.0]
              C = [10.5,12.0];

              def hlf(*args):
              i=0
              while i < len(args[0]):
              yield args[0][i]**(-1.0/2.0) - 0.2*args[1][i] - 43 + args[2][i]
              i += 1

              results = hlf(A, B, C)
              for r in results:
              print(r)


              Output:



              -34.86429773960448
              -33.68377223398316


              Last one is rather edicational if you want to practice python generators.






              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',
                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%2f53206474%2fmultiple-values-of-a-variable-in-same-equation%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                2
                down vote



                accepted











                map + list



                Note map can take multiple iterable arguments:



                res = map(hlf, A, B, C)

                [-34.86429773960448, -33.68377223398316]


                In Python 2.7, map returns a list. In Python 3.x map returns an iterator, so you can either iterate lazily or exhaust via list, i.e. list(map(hfl, A, B, C)).



                Reference:




                map(function, iterable, ...)



                ...If additional iterable arguments are passed, function must
                take that many arguments and is applied to the items from all
                iterables in parallel.





                zip + list comprehension



                You can use zip within a list comprehension. For clarity, you should avoid naming your arguments the same as your variables.



                A = [18.0,10.0]; B = [13.0,15.0]; C = [10.5,12.0]; 

                def hlf(x, y, z):
                return x**(-1.0/2.0) - 0.2*y - 43 + z

                res = [hlf(*vars) for vars in zip(A, B, C)]

                [-34.86429773960448, -33.68377223398316]





                share|improve this answer



























                  up vote
                  2
                  down vote



                  accepted











                  map + list



                  Note map can take multiple iterable arguments:



                  res = map(hlf, A, B, C)

                  [-34.86429773960448, -33.68377223398316]


                  In Python 2.7, map returns a list. In Python 3.x map returns an iterator, so you can either iterate lazily or exhaust via list, i.e. list(map(hfl, A, B, C)).



                  Reference:




                  map(function, iterable, ...)



                  ...If additional iterable arguments are passed, function must
                  take that many arguments and is applied to the items from all
                  iterables in parallel.





                  zip + list comprehension



                  You can use zip within a list comprehension. For clarity, you should avoid naming your arguments the same as your variables.



                  A = [18.0,10.0]; B = [13.0,15.0]; C = [10.5,12.0]; 

                  def hlf(x, y, z):
                  return x**(-1.0/2.0) - 0.2*y - 43 + z

                  res = [hlf(*vars) for vars in zip(A, B, C)]

                  [-34.86429773960448, -33.68377223398316]





                  share|improve this answer

























                    up vote
                    2
                    down vote



                    accepted







                    up vote
                    2
                    down vote



                    accepted







                    map + list



                    Note map can take multiple iterable arguments:



                    res = map(hlf, A, B, C)

                    [-34.86429773960448, -33.68377223398316]


                    In Python 2.7, map returns a list. In Python 3.x map returns an iterator, so you can either iterate lazily or exhaust via list, i.e. list(map(hfl, A, B, C)).



                    Reference:




                    map(function, iterable, ...)



                    ...If additional iterable arguments are passed, function must
                    take that many arguments and is applied to the items from all
                    iterables in parallel.





                    zip + list comprehension



                    You can use zip within a list comprehension. For clarity, you should avoid naming your arguments the same as your variables.



                    A = [18.0,10.0]; B = [13.0,15.0]; C = [10.5,12.0]; 

                    def hlf(x, y, z):
                    return x**(-1.0/2.0) - 0.2*y - 43 + z

                    res = [hlf(*vars) for vars in zip(A, B, C)]

                    [-34.86429773960448, -33.68377223398316]





                    share|improve this answer















                    map + list



                    Note map can take multiple iterable arguments:



                    res = map(hlf, A, B, C)

                    [-34.86429773960448, -33.68377223398316]


                    In Python 2.7, map returns a list. In Python 3.x map returns an iterator, so you can either iterate lazily or exhaust via list, i.e. list(map(hfl, A, B, C)).



                    Reference:




                    map(function, iterable, ...)



                    ...If additional iterable arguments are passed, function must
                    take that many arguments and is applied to the items from all
                    iterables in parallel.





                    zip + list comprehension



                    You can use zip within a list comprehension. For clarity, you should avoid naming your arguments the same as your variables.



                    A = [18.0,10.0]; B = [13.0,15.0]; C = [10.5,12.0]; 

                    def hlf(x, y, z):
                    return x**(-1.0/2.0) - 0.2*y - 43 + z

                    res = [hlf(*vars) for vars in zip(A, B, C)]

                    [-34.86429773960448, -33.68377223398316]






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 8 at 11:54

























                    answered Nov 8 at 11:17









                    jpp

                    81.4k194795




                    81.4k194795
























                        up vote
                        1
                        down vote













                        Vectorize with Numpy. Best Performace



                        Normally its much better try to vectorize this kind of operations with numpy, because the best performance results. When you vectorize instead to use a loop, you are using all your cores, and its the fastest solution. You should vectorize the operation with numpy. Something like this:



                        import numpy as np

                        A = [18.0,10.0]; B = [13.0,15.0]; C = [10.5,12.0];
                        a = np.array(A)
                        b = np.array(B)
                        c = np.array(C)


                        And now your function with the new vectors like arguments:



                        def hlf(a_vector,b_vector,c_vector):
                        return a_vector**(-1.0/2.0)-0.2*b_vector-43+c_vector


                        And finally call your new function vectorized:



                        print (hlf(a_vector = a,b_vector = b,c_vector = c))


                        Output:



                        >>> array([-34.86429774, -33.68377223])





                        share|improve this answer



























                          up vote
                          1
                          down vote













                          Vectorize with Numpy. Best Performace



                          Normally its much better try to vectorize this kind of operations with numpy, because the best performance results. When you vectorize instead to use a loop, you are using all your cores, and its the fastest solution. You should vectorize the operation with numpy. Something like this:



                          import numpy as np

                          A = [18.0,10.0]; B = [13.0,15.0]; C = [10.5,12.0];
                          a = np.array(A)
                          b = np.array(B)
                          c = np.array(C)


                          And now your function with the new vectors like arguments:



                          def hlf(a_vector,b_vector,c_vector):
                          return a_vector**(-1.0/2.0)-0.2*b_vector-43+c_vector


                          And finally call your new function vectorized:



                          print (hlf(a_vector = a,b_vector = b,c_vector = c))


                          Output:



                          >>> array([-34.86429774, -33.68377223])





                          share|improve this answer

























                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            Vectorize with Numpy. Best Performace



                            Normally its much better try to vectorize this kind of operations with numpy, because the best performance results. When you vectorize instead to use a loop, you are using all your cores, and its the fastest solution. You should vectorize the operation with numpy. Something like this:



                            import numpy as np

                            A = [18.0,10.0]; B = [13.0,15.0]; C = [10.5,12.0];
                            a = np.array(A)
                            b = np.array(B)
                            c = np.array(C)


                            And now your function with the new vectors like arguments:



                            def hlf(a_vector,b_vector,c_vector):
                            return a_vector**(-1.0/2.0)-0.2*b_vector-43+c_vector


                            And finally call your new function vectorized:



                            print (hlf(a_vector = a,b_vector = b,c_vector = c))


                            Output:



                            >>> array([-34.86429774, -33.68377223])





                            share|improve this answer














                            Vectorize with Numpy. Best Performace



                            Normally its much better try to vectorize this kind of operations with numpy, because the best performance results. When you vectorize instead to use a loop, you are using all your cores, and its the fastest solution. You should vectorize the operation with numpy. Something like this:



                            import numpy as np

                            A = [18.0,10.0]; B = [13.0,15.0]; C = [10.5,12.0];
                            a = np.array(A)
                            b = np.array(B)
                            c = np.array(C)


                            And now your function with the new vectors like arguments:



                            def hlf(a_vector,b_vector,c_vector):
                            return a_vector**(-1.0/2.0)-0.2*b_vector-43+c_vector


                            And finally call your new function vectorized:



                            print (hlf(a_vector = a,b_vector = b,c_vector = c))


                            Output:



                            >>> array([-34.86429774, -33.68377223])






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 8 at 11:55

























                            answered Nov 8 at 11:27









                            Baurin Leza

                            52449




                            52449






















                                up vote
                                -1
                                down vote













                                If you want to keep your function as is, you should call it N times with:



                                for i in range(N):
                                result = hlf(A[i], B[i], C[i])
                                print(result)


                                Another interesting method is to make a generator with your function:



                                A = [18.0,10.0]
                                B = [13.0,15.0]
                                C = [10.5,12.0];

                                def hlf(*args):
                                i=0
                                while i < len(args[0]):
                                yield args[0][i]**(-1.0/2.0) - 0.2*args[1][i] - 43 + args[2][i]
                                i += 1

                                results = hlf(A, B, C)
                                for r in results:
                                print(r)


                                Output:



                                -34.86429773960448
                                -33.68377223398316


                                Last one is rather edicational if you want to practice python generators.






                                share|improve this answer



























                                  up vote
                                  -1
                                  down vote













                                  If you want to keep your function as is, you should call it N times with:



                                  for i in range(N):
                                  result = hlf(A[i], B[i], C[i])
                                  print(result)


                                  Another interesting method is to make a generator with your function:



                                  A = [18.0,10.0]
                                  B = [13.0,15.0]
                                  C = [10.5,12.0];

                                  def hlf(*args):
                                  i=0
                                  while i < len(args[0]):
                                  yield args[0][i]**(-1.0/2.0) - 0.2*args[1][i] - 43 + args[2][i]
                                  i += 1

                                  results = hlf(A, B, C)
                                  for r in results:
                                  print(r)


                                  Output:



                                  -34.86429773960448
                                  -33.68377223398316


                                  Last one is rather edicational if you want to practice python generators.






                                  share|improve this answer

























                                    up vote
                                    -1
                                    down vote










                                    up vote
                                    -1
                                    down vote









                                    If you want to keep your function as is, you should call it N times with:



                                    for i in range(N):
                                    result = hlf(A[i], B[i], C[i])
                                    print(result)


                                    Another interesting method is to make a generator with your function:



                                    A = [18.0,10.0]
                                    B = [13.0,15.0]
                                    C = [10.5,12.0];

                                    def hlf(*args):
                                    i=0
                                    while i < len(args[0]):
                                    yield args[0][i]**(-1.0/2.0) - 0.2*args[1][i] - 43 + args[2][i]
                                    i += 1

                                    results = hlf(A, B, C)
                                    for r in results:
                                    print(r)


                                    Output:



                                    -34.86429773960448
                                    -33.68377223398316


                                    Last one is rather edicational if you want to practice python generators.






                                    share|improve this answer














                                    If you want to keep your function as is, you should call it N times with:



                                    for i in range(N):
                                    result = hlf(A[i], B[i], C[i])
                                    print(result)


                                    Another interesting method is to make a generator with your function:



                                    A = [18.0,10.0]
                                    B = [13.0,15.0]
                                    C = [10.5,12.0];

                                    def hlf(*args):
                                    i=0
                                    while i < len(args[0]):
                                    yield args[0][i]**(-1.0/2.0) - 0.2*args[1][i] - 43 + args[2][i]
                                    i += 1

                                    results = hlf(A, B, C)
                                    for r in results:
                                    print(r)


                                    Output:



                                    -34.86429773960448
                                    -33.68377223398316


                                    Last one is rather edicational if you want to practice python generators.







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Nov 8 at 11:33

























                                    answered Nov 8 at 11:16









                                    Cheche

                                    741118




                                    741118






























                                         

                                        draft saved


                                        draft discarded



















































                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206474%2fmultiple-values-of-a-variable-in-same-equation%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

                                        Schultheiß

                                        Liste der Kulturdenkmale in Wilsdruff

                                        Android Play Services Check