Multiply two lists

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite












I have two lists, e.g.



a = 1,2,3

b = 4,5,6


I'd like to multiply each element from the first list with each element from the second, like this:



1*4, 2*4, 3*4, 1*5, 2*5, 3*5, 1*6, 2*6, 3*6


I only need the result:



4, 5, 6, 8, 10, 12, 15, 18


I have found a solution for this:



Drop[Flatten[Reap[For[i = 1, i < 4, i++, Sow[i*4, 5, 6]]]], 1]


However, I think this is overly complicated -- and number 12 is in there two times. Isn't there a much simpler way to do this? Multiply two lists?







share|improve this question




















  • Union @@ Outer[Times, 1, 2, 3, 4, 5, 6]
    – mathe
    Aug 31 at 3:18














up vote
6
down vote

favorite












I have two lists, e.g.



a = 1,2,3

b = 4,5,6


I'd like to multiply each element from the first list with each element from the second, like this:



1*4, 2*4, 3*4, 1*5, 2*5, 3*5, 1*6, 2*6, 3*6


I only need the result:



4, 5, 6, 8, 10, 12, 15, 18


I have found a solution for this:



Drop[Flatten[Reap[For[i = 1, i < 4, i++, Sow[i*4, 5, 6]]]], 1]


However, I think this is overly complicated -- and number 12 is in there two times. Isn't there a much simpler way to do this? Multiply two lists?







share|improve this question




















  • Union @@ Outer[Times, 1, 2, 3, 4, 5, 6]
    – mathe
    Aug 31 at 3:18












up vote
6
down vote

favorite









up vote
6
down vote

favorite











I have two lists, e.g.



a = 1,2,3

b = 4,5,6


I'd like to multiply each element from the first list with each element from the second, like this:



1*4, 2*4, 3*4, 1*5, 2*5, 3*5, 1*6, 2*6, 3*6


I only need the result:



4, 5, 6, 8, 10, 12, 15, 18


I have found a solution for this:



Drop[Flatten[Reap[For[i = 1, i < 4, i++, Sow[i*4, 5, 6]]]], 1]


However, I think this is overly complicated -- and number 12 is in there two times. Isn't there a much simpler way to do this? Multiply two lists?







share|improve this question












I have two lists, e.g.



a = 1,2,3

b = 4,5,6


I'd like to multiply each element from the first list with each element from the second, like this:



1*4, 2*4, 3*4, 1*5, 2*5, 3*5, 1*6, 2*6, 3*6


I only need the result:



4, 5, 6, 8, 10, 12, 15, 18


I have found a solution for this:



Drop[Flatten[Reap[For[i = 1, i < 4, i++, Sow[i*4, 5, 6]]]], 1]


However, I think this is overly complicated -- and number 12 is in there two times. Isn't there a much simpler way to do this? Multiply two lists?









share|improve this question











share|improve this question




share|improve this question










asked Aug 30 at 18:47









Marian Stiehler

585




585











  • Union @@ Outer[Times, 1, 2, 3, 4, 5, 6]
    – mathe
    Aug 31 at 3:18
















  • Union @@ Outer[Times, 1, 2, 3, 4, 5, 6]
    – mathe
    Aug 31 at 3:18















Union @@ Outer[Times, 1, 2, 3, 4, 5, 6]
– mathe
Aug 31 at 3:18




Union @@ Outer[Times, 1, 2, 3, 4, 5, 6]
– mathe
Aug 31 at 3:18










3 Answers
3






active

oldest

votes

















up vote
2
down vote



accepted










Another way to produce the result you seek is:



DeleteDuplicates[Flatten[Table[i*j, i, a, j, b]]]



4, 5, 6, 8, 10, 12, 15, 18




This will also work, and generalizes easily to more than two lists:



DeleteDuplicates[Flatten[TensorProduct[a, b]]]


TensorProduct runs in time similar to KronekerProduct and Outer.






share|improve this answer





























    up vote
    9
    down vote













    a = 1, 2, 3;
    b = 4, 5, 6;
    Flatten[KroneckerProduct[a, b]]



    4, 5, 6, 8, 10, 12, 12, 15, 18




    Use DeleteDuplicates to, well, delete duplicates.






    share|improve this answer





























      up vote
      9
      down vote













      DeleteDuplicates @ Flatten @ Outer[Times, a, b] 



      4, 5, 6, 8, 10, 12, 15, 18




      Also, shorter but much slower



      DeleteDuplicates[Times @@@ Tuples @ a, b]



      4, 5, 6, 8, 10, 12, 15, 18




      Note: If speed is a concern, then Outer, KroneckerProduct and TensorProduct are much faster than Table and Tuples methods:



      SeedRandom[1]
      a, b = RandomInteger[100, 2, 1500];
      r1 = DeleteDuplicates @ Flatten @ Outer[Times, a, b]; // RepeatedTiming // First



      0.0103




      r2 = DeleteDuplicates[Times @@@ Tuples @ a, b]; // RepeatedTiming // First 



      0.92




      r3 = DeleteDuplicates[Flatten[Table[i*j, i, a, j, b]]]; // RepeatedTiming // First 



      0.78




      r4 = DeleteDuplicates @Flatten[KroneckerProduct[a, b]]; // RepeatedTiming // First   



      0.011




      r5 = DeleteDuplicates[Flatten[TensorProduct[a, b]]]; // RepeatedTiming // First



      0.011




      r1 == r2 == r3 == r4 == r5



      True







      share|improve this answer






















        Your Answer




        StackExchange.ifUsing("editor", function ()
        return StackExchange.using("mathjaxEditing", function ()
        StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
        StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
        );
        );
        , "mathjax-editing");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "387"
        ;
        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: false,
        noModals: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













         

        draft saved


        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f180916%2fmultiply-two-lists%23new-answer', 'question_page');

        );

        Post as a guest






























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        2
        down vote



        accepted










        Another way to produce the result you seek is:



        DeleteDuplicates[Flatten[Table[i*j, i, a, j, b]]]



        4, 5, 6, 8, 10, 12, 15, 18




        This will also work, and generalizes easily to more than two lists:



        DeleteDuplicates[Flatten[TensorProduct[a, b]]]


        TensorProduct runs in time similar to KronekerProduct and Outer.






        share|improve this answer


























          up vote
          2
          down vote



          accepted










          Another way to produce the result you seek is:



          DeleteDuplicates[Flatten[Table[i*j, i, a, j, b]]]



          4, 5, 6, 8, 10, 12, 15, 18




          This will also work, and generalizes easily to more than two lists:



          DeleteDuplicates[Flatten[TensorProduct[a, b]]]


          TensorProduct runs in time similar to KronekerProduct and Outer.






          share|improve this answer
























            up vote
            2
            down vote



            accepted







            up vote
            2
            down vote



            accepted






            Another way to produce the result you seek is:



            DeleteDuplicates[Flatten[Table[i*j, i, a, j, b]]]



            4, 5, 6, 8, 10, 12, 15, 18




            This will also work, and generalizes easily to more than two lists:



            DeleteDuplicates[Flatten[TensorProduct[a, b]]]


            TensorProduct runs in time similar to KronekerProduct and Outer.






            share|improve this answer














            Another way to produce the result you seek is:



            DeleteDuplicates[Flatten[Table[i*j, i, a, j, b]]]



            4, 5, 6, 8, 10, 12, 15, 18




            This will also work, and generalizes easily to more than two lists:



            DeleteDuplicates[Flatten[TensorProduct[a, b]]]


            TensorProduct runs in time similar to KronekerProduct and Outer.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 30 at 21:49

























            answered Aug 30 at 20:52









            Lee

            40817




            40817




















                up vote
                9
                down vote













                a = 1, 2, 3;
                b = 4, 5, 6;
                Flatten[KroneckerProduct[a, b]]



                4, 5, 6, 8, 10, 12, 12, 15, 18




                Use DeleteDuplicates to, well, delete duplicates.






                share|improve this answer


























                  up vote
                  9
                  down vote













                  a = 1, 2, 3;
                  b = 4, 5, 6;
                  Flatten[KroneckerProduct[a, b]]



                  4, 5, 6, 8, 10, 12, 12, 15, 18




                  Use DeleteDuplicates to, well, delete duplicates.






                  share|improve this answer
























                    up vote
                    9
                    down vote










                    up vote
                    9
                    down vote









                    a = 1, 2, 3;
                    b = 4, 5, 6;
                    Flatten[KroneckerProduct[a, b]]



                    4, 5, 6, 8, 10, 12, 12, 15, 18




                    Use DeleteDuplicates to, well, delete duplicates.






                    share|improve this answer














                    a = 1, 2, 3;
                    b = 4, 5, 6;
                    Flatten[KroneckerProduct[a, b]]



                    4, 5, 6, 8, 10, 12, 12, 15, 18




                    Use DeleteDuplicates to, well, delete duplicates.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Aug 30 at 21:10

























                    answered Aug 30 at 18:51









                    Henrik Schumacher

                    36.8k249103




                    36.8k249103




















                        up vote
                        9
                        down vote













                        DeleteDuplicates @ Flatten @ Outer[Times, a, b] 



                        4, 5, 6, 8, 10, 12, 15, 18




                        Also, shorter but much slower



                        DeleteDuplicates[Times @@@ Tuples @ a, b]



                        4, 5, 6, 8, 10, 12, 15, 18




                        Note: If speed is a concern, then Outer, KroneckerProduct and TensorProduct are much faster than Table and Tuples methods:



                        SeedRandom[1]
                        a, b = RandomInteger[100, 2, 1500];
                        r1 = DeleteDuplicates @ Flatten @ Outer[Times, a, b]; // RepeatedTiming // First



                        0.0103




                        r2 = DeleteDuplicates[Times @@@ Tuples @ a, b]; // RepeatedTiming // First 



                        0.92




                        r3 = DeleteDuplicates[Flatten[Table[i*j, i, a, j, b]]]; // RepeatedTiming // First 



                        0.78




                        r4 = DeleteDuplicates @Flatten[KroneckerProduct[a, b]]; // RepeatedTiming // First   



                        0.011




                        r5 = DeleteDuplicates[Flatten[TensorProduct[a, b]]]; // RepeatedTiming // First



                        0.011




                        r1 == r2 == r3 == r4 == r5



                        True







                        share|improve this answer


























                          up vote
                          9
                          down vote













                          DeleteDuplicates @ Flatten @ Outer[Times, a, b] 



                          4, 5, 6, 8, 10, 12, 15, 18




                          Also, shorter but much slower



                          DeleteDuplicates[Times @@@ Tuples @ a, b]



                          4, 5, 6, 8, 10, 12, 15, 18




                          Note: If speed is a concern, then Outer, KroneckerProduct and TensorProduct are much faster than Table and Tuples methods:



                          SeedRandom[1]
                          a, b = RandomInteger[100, 2, 1500];
                          r1 = DeleteDuplicates @ Flatten @ Outer[Times, a, b]; // RepeatedTiming // First



                          0.0103




                          r2 = DeleteDuplicates[Times @@@ Tuples @ a, b]; // RepeatedTiming // First 



                          0.92




                          r3 = DeleteDuplicates[Flatten[Table[i*j, i, a, j, b]]]; // RepeatedTiming // First 



                          0.78




                          r4 = DeleteDuplicates @Flatten[KroneckerProduct[a, b]]; // RepeatedTiming // First   



                          0.011




                          r5 = DeleteDuplicates[Flatten[TensorProduct[a, b]]]; // RepeatedTiming // First



                          0.011




                          r1 == r2 == r3 == r4 == r5



                          True







                          share|improve this answer
























                            up vote
                            9
                            down vote










                            up vote
                            9
                            down vote









                            DeleteDuplicates @ Flatten @ Outer[Times, a, b] 



                            4, 5, 6, 8, 10, 12, 15, 18




                            Also, shorter but much slower



                            DeleteDuplicates[Times @@@ Tuples @ a, b]



                            4, 5, 6, 8, 10, 12, 15, 18




                            Note: If speed is a concern, then Outer, KroneckerProduct and TensorProduct are much faster than Table and Tuples methods:



                            SeedRandom[1]
                            a, b = RandomInteger[100, 2, 1500];
                            r1 = DeleteDuplicates @ Flatten @ Outer[Times, a, b]; // RepeatedTiming // First



                            0.0103




                            r2 = DeleteDuplicates[Times @@@ Tuples @ a, b]; // RepeatedTiming // First 



                            0.92




                            r3 = DeleteDuplicates[Flatten[Table[i*j, i, a, j, b]]]; // RepeatedTiming // First 



                            0.78




                            r4 = DeleteDuplicates @Flatten[KroneckerProduct[a, b]]; // RepeatedTiming // First   



                            0.011




                            r5 = DeleteDuplicates[Flatten[TensorProduct[a, b]]]; // RepeatedTiming // First



                            0.011




                            r1 == r2 == r3 == r4 == r5



                            True







                            share|improve this answer














                            DeleteDuplicates @ Flatten @ Outer[Times, a, b] 



                            4, 5, 6, 8, 10, 12, 15, 18




                            Also, shorter but much slower



                            DeleteDuplicates[Times @@@ Tuples @ a, b]



                            4, 5, 6, 8, 10, 12, 15, 18




                            Note: If speed is a concern, then Outer, KroneckerProduct and TensorProduct are much faster than Table and Tuples methods:



                            SeedRandom[1]
                            a, b = RandomInteger[100, 2, 1500];
                            r1 = DeleteDuplicates @ Flatten @ Outer[Times, a, b]; // RepeatedTiming // First



                            0.0103




                            r2 = DeleteDuplicates[Times @@@ Tuples @ a, b]; // RepeatedTiming // First 



                            0.92




                            r3 = DeleteDuplicates[Flatten[Table[i*j, i, a, j, b]]]; // RepeatedTiming // First 



                            0.78




                            r4 = DeleteDuplicates @Flatten[KroneckerProduct[a, b]]; // RepeatedTiming // First   



                            0.011




                            r5 = DeleteDuplicates[Flatten[TensorProduct[a, b]]]; // RepeatedTiming // First



                            0.011




                            r1 == r2 == r3 == r4 == r5



                            True








                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Aug 30 at 21:42

























                            answered Aug 30 at 18:51









                            kglr

                            159k8183382




                            159k8183382



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f180916%2fmultiply-two-lists%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Comments

                                Popular posts from this blog

                                What does second last employer means? [closed]

                                List of Gilmore Girls characters

                                Confectionery