Fast outer tensor product in numpy












0















I have two numpy arrays:



x of shape ((d1,...,d_m)) 
y of shape ((e_1,...e_n))


I would like to form the outer tensor product, that is the numpy array



z of shape ((d1,...,d_m,e_1,...,e_n))


such that



z[i_1,...,i_n,i_{n+1}...,i_{m+n}] == x[i_1,...i_m]*y[i_{m+1},...,i_{m+n}]


I have to perform the above outer multiplication several times so I would like to speed this up as much as possible.










share|improve this question























  • insert dummy data with your expected result to understand better what do you mean

    – Francesco Nazzaro
    May 26 '16 at 15:19
















0















I have two numpy arrays:



x of shape ((d1,...,d_m)) 
y of shape ((e_1,...e_n))


I would like to form the outer tensor product, that is the numpy array



z of shape ((d1,...,d_m,e_1,...,e_n))


such that



z[i_1,...,i_n,i_{n+1}...,i_{m+n}] == x[i_1,...i_m]*y[i_{m+1},...,i_{m+n}]


I have to perform the above outer multiplication several times so I would like to speed this up as much as possible.










share|improve this question























  • insert dummy data with your expected result to understand better what do you mean

    – Francesco Nazzaro
    May 26 '16 at 15:19














0












0








0


1






I have two numpy arrays:



x of shape ((d1,...,d_m)) 
y of shape ((e_1,...e_n))


I would like to form the outer tensor product, that is the numpy array



z of shape ((d1,...,d_m,e_1,...,e_n))


such that



z[i_1,...,i_n,i_{n+1}...,i_{m+n}] == x[i_1,...i_m]*y[i_{m+1},...,i_{m+n}]


I have to perform the above outer multiplication several times so I would like to speed this up as much as possible.










share|improve this question














I have two numpy arrays:



x of shape ((d1,...,d_m)) 
y of shape ((e_1,...e_n))


I would like to form the outer tensor product, that is the numpy array



z of shape ((d1,...,d_m,e_1,...,e_n))


such that



z[i_1,...,i_n,i_{n+1}...,i_{m+n}] == x[i_1,...i_m]*y[i_{m+1},...,i_{m+n}]


I have to perform the above outer multiplication several times so I would like to speed this up as much as possible.







python arrays numpy linear-algebra






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked May 26 '16 at 15:10









factfact

13129




13129













  • insert dummy data with your expected result to understand better what do you mean

    – Francesco Nazzaro
    May 26 '16 at 15:19



















  • insert dummy data with your expected result to understand better what do you mean

    – Francesco Nazzaro
    May 26 '16 at 15:19

















insert dummy data with your expected result to understand better what do you mean

– Francesco Nazzaro
May 26 '16 at 15:19





insert dummy data with your expected result to understand better what do you mean

– Francesco Nazzaro
May 26 '16 at 15:19












2 Answers
2






active

oldest

votes


















2














An alternative to outer is to explicitly expand the dimensions. For 1d arrays this would be



x[:,None]*y   # y[None,:] is automatic.


For 10x10 arrays, and generalizing the dimension expansion, I get the same times



In [74]: timeit x[[slice(None)]*x.ndim + [None]*y.ndim] * y
10000 loops, best of 3: 53.6 µs per loop

In [75]: timeit np.multiply.outer(x,y)
10000 loops, best of 3: 52.6 µs per loop


So outer does save some coding, but the basic broadcasted multiplication is the same.






share|improve this answer































    5














    You want np.multiply.outer:



    z = np.multiply.outer(x, y)





    share|improve this answer
























    • That's it! Am I right to assume that since ufunc is part of numpy, it calls C code and there won't be a significantly faster way to do this?

      – fact
      May 26 '16 at 15:50











    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%2f37464737%2ffast-outer-tensor-product-in-numpy%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














    An alternative to outer is to explicitly expand the dimensions. For 1d arrays this would be



    x[:,None]*y   # y[None,:] is automatic.


    For 10x10 arrays, and generalizing the dimension expansion, I get the same times



    In [74]: timeit x[[slice(None)]*x.ndim + [None]*y.ndim] * y
    10000 loops, best of 3: 53.6 µs per loop

    In [75]: timeit np.multiply.outer(x,y)
    10000 loops, best of 3: 52.6 µs per loop


    So outer does save some coding, but the basic broadcasted multiplication is the same.






    share|improve this answer




























      2














      An alternative to outer is to explicitly expand the dimensions. For 1d arrays this would be



      x[:,None]*y   # y[None,:] is automatic.


      For 10x10 arrays, and generalizing the dimension expansion, I get the same times



      In [74]: timeit x[[slice(None)]*x.ndim + [None]*y.ndim] * y
      10000 loops, best of 3: 53.6 µs per loop

      In [75]: timeit np.multiply.outer(x,y)
      10000 loops, best of 3: 52.6 µs per loop


      So outer does save some coding, but the basic broadcasted multiplication is the same.






      share|improve this answer


























        2












        2








        2







        An alternative to outer is to explicitly expand the dimensions. For 1d arrays this would be



        x[:,None]*y   # y[None,:] is automatic.


        For 10x10 arrays, and generalizing the dimension expansion, I get the same times



        In [74]: timeit x[[slice(None)]*x.ndim + [None]*y.ndim] * y
        10000 loops, best of 3: 53.6 µs per loop

        In [75]: timeit np.multiply.outer(x,y)
        10000 loops, best of 3: 52.6 µs per loop


        So outer does save some coding, but the basic broadcasted multiplication is the same.






        share|improve this answer













        An alternative to outer is to explicitly expand the dimensions. For 1d arrays this would be



        x[:,None]*y   # y[None,:] is automatic.


        For 10x10 arrays, and generalizing the dimension expansion, I get the same times



        In [74]: timeit x[[slice(None)]*x.ndim + [None]*y.ndim] * y
        10000 loops, best of 3: 53.6 µs per loop

        In [75]: timeit np.multiply.outer(x,y)
        10000 loops, best of 3: 52.6 µs per loop


        So outer does save some coding, but the basic broadcasted multiplication is the same.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered May 26 '16 at 16:33









        hpauljhpaulj

        112k780148




        112k780148

























            5














            You want np.multiply.outer:



            z = np.multiply.outer(x, y)





            share|improve this answer
























            • That's it! Am I right to assume that since ufunc is part of numpy, it calls C code and there won't be a significantly faster way to do this?

              – fact
              May 26 '16 at 15:50
















            5














            You want np.multiply.outer:



            z = np.multiply.outer(x, y)





            share|improve this answer
























            • That's it! Am I right to assume that since ufunc is part of numpy, it calls C code and there won't be a significantly faster way to do this?

              – fact
              May 26 '16 at 15:50














            5












            5








            5







            You want np.multiply.outer:



            z = np.multiply.outer(x, y)





            share|improve this answer













            You want np.multiply.outer:



            z = np.multiply.outer(x, y)






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered May 26 '16 at 15:43









            EricEric

            66.6k31169278




            66.6k31169278













            • That's it! Am I right to assume that since ufunc is part of numpy, it calls C code and there won't be a significantly faster way to do this?

              – fact
              May 26 '16 at 15:50



















            • That's it! Am I right to assume that since ufunc is part of numpy, it calls C code and there won't be a significantly faster way to do this?

              – fact
              May 26 '16 at 15:50

















            That's it! Am I right to assume that since ufunc is part of numpy, it calls C code and there won't be a significantly faster way to do this?

            – fact
            May 26 '16 at 15:50





            That's it! Am I right to assume that since ufunc is part of numpy, it calls C code and there won't be a significantly faster way to do this?

            – fact
            May 26 '16 at 15:50


















            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%2f37464737%2ffast-outer-tensor-product-in-numpy%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

            Coverage of Google Street View

            Full-time equivalent

            Surfing