Exponent an arbitrary number by a numpy array












2















I know np.exp2(x) exists that calculates 2^x where x is a numpy array, however, I am looking for a method that does K^x where K is any arbitrary number.
Is there any elegant way of doing it rather than stretching out K to the shape of x and doing a piecewise exponent?










share|improve this question

























  • @tel: The "piecewise" seems to make things more confusing rather than less.

    – user2357112
    Nov 22 '18 at 8:24
















2















I know np.exp2(x) exists that calculates 2^x where x is a numpy array, however, I am looking for a method that does K^x where K is any arbitrary number.
Is there any elegant way of doing it rather than stretching out K to the shape of x and doing a piecewise exponent?










share|improve this question

























  • @tel: The "piecewise" seems to make things more confusing rather than less.

    – user2357112
    Nov 22 '18 at 8:24














2












2








2








I know np.exp2(x) exists that calculates 2^x where x is a numpy array, however, I am looking for a method that does K^x where K is any arbitrary number.
Is there any elegant way of doing it rather than stretching out K to the shape of x and doing a piecewise exponent?










share|improve this question
















I know np.exp2(x) exists that calculates 2^x where x is a numpy array, however, I am looking for a method that does K^x where K is any arbitrary number.
Is there any elegant way of doing it rather than stretching out K to the shape of x and doing a piecewise exponent?







python numpy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 8:23









user2357112

155k12167260




155k12167260










asked Nov 22 '18 at 5:01









Vj-Vj-

656617




656617













  • @tel: The "piecewise" seems to make things more confusing rather than less.

    – user2357112
    Nov 22 '18 at 8:24



















  • @tel: The "piecewise" seems to make things more confusing rather than less.

    – user2357112
    Nov 22 '18 at 8:24

















@tel: The "piecewise" seems to make things more confusing rather than less.

– user2357112
Nov 22 '18 at 8:24





@tel: The "piecewise" seems to make things more confusing rather than less.

– user2357112
Nov 22 '18 at 8:24












2 Answers
2






active

oldest

votes


















2














Just use the standard Python exponentiation operator **:



K**x


For example, if you have:



x = np.array([1,2,3])
K = 3

print(K**x)


The output is:



[ 3  9 27]


Notes



For Python classes, the behavior of the binary ** operator is implemented via the __pow__, __rpow__, and __ipow__ magic methods (the reality for np.ndarray is slightly more complicated since it's implemented in the C layer, but that's not actually important here). For Numpy arrays, these magic methods in turn appear to call numpy.power, so you can expect that ** will have the same behavior as documented for numpy.power. In particular,




Note that an integer type raised to a negative integer power will raise a ValueError.







share|improve this answer


























  • small note: cast K to a float or else negative numbers in the array will throw an error.

    – Vj-
    Nov 22 '18 at 5:14













  • @Vj- I added a note about the implementation of **, including where it mentions that particular error in the documentation

    – tel
    Nov 22 '18 at 5:32



















2














With numpy you can just use numpy.power



arr = numpy.array([1,2,3])
print(numpy.power(3,arr)) # Outputs [ 3 9 27]





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%2f53424165%2fexponent-an-arbitrary-number-by-a-numpy-array%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Just use the standard Python exponentiation operator **:



    K**x


    For example, if you have:



    x = np.array([1,2,3])
    K = 3

    print(K**x)


    The output is:



    [ 3  9 27]


    Notes



    For Python classes, the behavior of the binary ** operator is implemented via the __pow__, __rpow__, and __ipow__ magic methods (the reality for np.ndarray is slightly more complicated since it's implemented in the C layer, but that's not actually important here). For Numpy arrays, these magic methods in turn appear to call numpy.power, so you can expect that ** will have the same behavior as documented for numpy.power. In particular,




    Note that an integer type raised to a negative integer power will raise a ValueError.







    share|improve this answer


























    • small note: cast K to a float or else negative numbers in the array will throw an error.

      – Vj-
      Nov 22 '18 at 5:14













    • @Vj- I added a note about the implementation of **, including where it mentions that particular error in the documentation

      – tel
      Nov 22 '18 at 5:32
















    2














    Just use the standard Python exponentiation operator **:



    K**x


    For example, if you have:



    x = np.array([1,2,3])
    K = 3

    print(K**x)


    The output is:



    [ 3  9 27]


    Notes



    For Python classes, the behavior of the binary ** operator is implemented via the __pow__, __rpow__, and __ipow__ magic methods (the reality for np.ndarray is slightly more complicated since it's implemented in the C layer, but that's not actually important here). For Numpy arrays, these magic methods in turn appear to call numpy.power, so you can expect that ** will have the same behavior as documented for numpy.power. In particular,




    Note that an integer type raised to a negative integer power will raise a ValueError.







    share|improve this answer


























    • small note: cast K to a float or else negative numbers in the array will throw an error.

      – Vj-
      Nov 22 '18 at 5:14













    • @Vj- I added a note about the implementation of **, including where it mentions that particular error in the documentation

      – tel
      Nov 22 '18 at 5:32














    2












    2








    2







    Just use the standard Python exponentiation operator **:



    K**x


    For example, if you have:



    x = np.array([1,2,3])
    K = 3

    print(K**x)


    The output is:



    [ 3  9 27]


    Notes



    For Python classes, the behavior of the binary ** operator is implemented via the __pow__, __rpow__, and __ipow__ magic methods (the reality for np.ndarray is slightly more complicated since it's implemented in the C layer, but that's not actually important here). For Numpy arrays, these magic methods in turn appear to call numpy.power, so you can expect that ** will have the same behavior as documented for numpy.power. In particular,




    Note that an integer type raised to a negative integer power will raise a ValueError.







    share|improve this answer















    Just use the standard Python exponentiation operator **:



    K**x


    For example, if you have:



    x = np.array([1,2,3])
    K = 3

    print(K**x)


    The output is:



    [ 3  9 27]


    Notes



    For Python classes, the behavior of the binary ** operator is implemented via the __pow__, __rpow__, and __ipow__ magic methods (the reality for np.ndarray is slightly more complicated since it's implemented in the C layer, but that's not actually important here). For Numpy arrays, these magic methods in turn appear to call numpy.power, so you can expect that ** will have the same behavior as documented for numpy.power. In particular,




    Note that an integer type raised to a negative integer power will raise a ValueError.








    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 22 '18 at 5:37

























    answered Nov 22 '18 at 5:07









    teltel

    7,41621431




    7,41621431













    • small note: cast K to a float or else negative numbers in the array will throw an error.

      – Vj-
      Nov 22 '18 at 5:14













    • @Vj- I added a note about the implementation of **, including where it mentions that particular error in the documentation

      – tel
      Nov 22 '18 at 5:32



















    • small note: cast K to a float or else negative numbers in the array will throw an error.

      – Vj-
      Nov 22 '18 at 5:14













    • @Vj- I added a note about the implementation of **, including where it mentions that particular error in the documentation

      – tel
      Nov 22 '18 at 5:32

















    small note: cast K to a float or else negative numbers in the array will throw an error.

    – Vj-
    Nov 22 '18 at 5:14







    small note: cast K to a float or else negative numbers in the array will throw an error.

    – Vj-
    Nov 22 '18 at 5:14















    @Vj- I added a note about the implementation of **, including where it mentions that particular error in the documentation

    – tel
    Nov 22 '18 at 5:32





    @Vj- I added a note about the implementation of **, including where it mentions that particular error in the documentation

    – tel
    Nov 22 '18 at 5:32













    2














    With numpy you can just use numpy.power



    arr = numpy.array([1,2,3])
    print(numpy.power(3,arr)) # Outputs [ 3 9 27]





    share|improve this answer




























      2














      With numpy you can just use numpy.power



      arr = numpy.array([1,2,3])
      print(numpy.power(3,arr)) # Outputs [ 3 9 27]





      share|improve this answer


























        2












        2








        2







        With numpy you can just use numpy.power



        arr = numpy.array([1,2,3])
        print(numpy.power(3,arr)) # Outputs [ 3 9 27]





        share|improve this answer













        With numpy you can just use numpy.power



        arr = numpy.array([1,2,3])
        print(numpy.power(3,arr)) # Outputs [ 3 9 27]






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 5:11









        b-fgb-fg

        1,95911522




        1,95911522






























            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424165%2fexponent-an-arbitrary-number-by-a-numpy-array%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

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            SQL update select statement

            'app-layout' is not a known element: how to share Component with different Modules