Is it possible to avoid unpacking when merging Association?












10












$begingroup$


For example, if we have



assoc = 
AssociationThread[{1, 2, 3} -> RandomReal[1., {3, 10}]];


and



Developer`PackedArrayQ/@ assoc


gives



<|1 -> True, 2 -> True, 3 -> True|>


so each Value is packed array.



Now, I want to Merge several Assocation like this. Then I checked packedness,



Developer`PackedArrayQ /@ Merge[{assoc, assoc}, Flatten]


it gives



<|1 -> False, 2 -> False, 3 -> False|>


It unpacks, even though Flatten should not unpack list.



So is it possible to avoid this unpacking in Merge?










share|improve this question









$endgroup$

















    10












    $begingroup$


    For example, if we have



    assoc = 
    AssociationThread[{1, 2, 3} -> RandomReal[1., {3, 10}]];


    and



    Developer`PackedArrayQ/@ assoc


    gives



    <|1 -> True, 2 -> True, 3 -> True|>


    so each Value is packed array.



    Now, I want to Merge several Assocation like this. Then I checked packedness,



    Developer`PackedArrayQ /@ Merge[{assoc, assoc}, Flatten]


    it gives



    <|1 -> False, 2 -> False, 3 -> False|>


    It unpacks, even though Flatten should not unpack list.



    So is it possible to avoid this unpacking in Merge?










    share|improve this question









    $endgroup$















      10












      10








      10


      1



      $begingroup$


      For example, if we have



      assoc = 
      AssociationThread[{1, 2, 3} -> RandomReal[1., {3, 10}]];


      and



      Developer`PackedArrayQ/@ assoc


      gives



      <|1 -> True, 2 -> True, 3 -> True|>


      so each Value is packed array.



      Now, I want to Merge several Assocation like this. Then I checked packedness,



      Developer`PackedArrayQ /@ Merge[{assoc, assoc}, Flatten]


      it gives



      <|1 -> False, 2 -> False, 3 -> False|>


      It unpacks, even though Flatten should not unpack list.



      So is it possible to avoid this unpacking in Merge?










      share|improve this question









      $endgroup$




      For example, if we have



      assoc = 
      AssociationThread[{1, 2, 3} -> RandomReal[1., {3, 10}]];


      and



      Developer`PackedArrayQ/@ assoc


      gives



      <|1 -> True, 2 -> True, 3 -> True|>


      so each Value is packed array.



      Now, I want to Merge several Assocation like this. Then I checked packedness,



      Developer`PackedArrayQ /@ Merge[{assoc, assoc}, Flatten]


      it gives



      <|1 -> False, 2 -> False, 3 -> False|>


      It unpacks, even though Flatten should not unpack list.



      So is it possible to avoid this unpacking in Merge?







      list-manipulation associations packed-arrays






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 8 hours ago









      matheoremmatheorem

      6,56743178




      6,56743178






















          2 Answers
          2






          active

          oldest

          votes


















          8












          $begingroup$

          One can use TracePrint to see how Flatten is being used by Merge:



          TracePrint[
          Merge[{assoc,assoc}, Flatten],
          _Flatten,
          TraceAction->Print@*Developer`PackedArrayForm
          ];



          Flatten[{PackedArray[Real,<10>],PackedArray[Real,<10>]}]



          Flatten[{PackedArray[Real,<10>],PackedArray[Real,<10>]}]



          Flatten[{PackedArray[Real,<10>],PackedArray[Real,<10>]}]




          Notice how Merge builds a list of two packed arrays, and then flattens them. This is why the the arrays become unpacked. To workaround this, you can either convert the list of two packed arrays into a packed array first:



          Developer`PackedArrayQ /@ Merge[{assoc, assoc}, Flatten @* Developer`ToPackedArray]



          <|1 -> True, 2 -> True, 3 -> True|>




          Or you can use Join with Apply (as in Henrik's answer, but using slot free syntax):



          Developer`PackedArrayQ /@ Merge[{assoc, assoc}, Apply[Join]]



          <|1 -> True, 2 -> True, 3 -> True|>







          share|improve this answer









          $endgroup$





















            3












            $begingroup$

            Developer`PackedArrayQ /@ Merge[{assoc, assoc}, Join @@ # &]



            <|1 -> True, 2 -> True, 3 -> True|>




            One might expect that Catenate should also work, but it does not.






            share|improve this answer









            $endgroup$













              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',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              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%2fmathematica.stackexchange.com%2fquestions%2f192984%2fis-it-possible-to-avoid-unpacking-when-merging-association%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









              8












              $begingroup$

              One can use TracePrint to see how Flatten is being used by Merge:



              TracePrint[
              Merge[{assoc,assoc}, Flatten],
              _Flatten,
              TraceAction->Print@*Developer`PackedArrayForm
              ];



              Flatten[{PackedArray[Real,<10>],PackedArray[Real,<10>]}]



              Flatten[{PackedArray[Real,<10>],PackedArray[Real,<10>]}]



              Flatten[{PackedArray[Real,<10>],PackedArray[Real,<10>]}]




              Notice how Merge builds a list of two packed arrays, and then flattens them. This is why the the arrays become unpacked. To workaround this, you can either convert the list of two packed arrays into a packed array first:



              Developer`PackedArrayQ /@ Merge[{assoc, assoc}, Flatten @* Developer`ToPackedArray]



              <|1 -> True, 2 -> True, 3 -> True|>




              Or you can use Join with Apply (as in Henrik's answer, but using slot free syntax):



              Developer`PackedArrayQ /@ Merge[{assoc, assoc}, Apply[Join]]



              <|1 -> True, 2 -> True, 3 -> True|>







              share|improve this answer









              $endgroup$


















                8












                $begingroup$

                One can use TracePrint to see how Flatten is being used by Merge:



                TracePrint[
                Merge[{assoc,assoc}, Flatten],
                _Flatten,
                TraceAction->Print@*Developer`PackedArrayForm
                ];



                Flatten[{PackedArray[Real,<10>],PackedArray[Real,<10>]}]



                Flatten[{PackedArray[Real,<10>],PackedArray[Real,<10>]}]



                Flatten[{PackedArray[Real,<10>],PackedArray[Real,<10>]}]




                Notice how Merge builds a list of two packed arrays, and then flattens them. This is why the the arrays become unpacked. To workaround this, you can either convert the list of two packed arrays into a packed array first:



                Developer`PackedArrayQ /@ Merge[{assoc, assoc}, Flatten @* Developer`ToPackedArray]



                <|1 -> True, 2 -> True, 3 -> True|>




                Or you can use Join with Apply (as in Henrik's answer, but using slot free syntax):



                Developer`PackedArrayQ /@ Merge[{assoc, assoc}, Apply[Join]]



                <|1 -> True, 2 -> True, 3 -> True|>







                share|improve this answer









                $endgroup$
















                  8












                  8








                  8





                  $begingroup$

                  One can use TracePrint to see how Flatten is being used by Merge:



                  TracePrint[
                  Merge[{assoc,assoc}, Flatten],
                  _Flatten,
                  TraceAction->Print@*Developer`PackedArrayForm
                  ];



                  Flatten[{PackedArray[Real,<10>],PackedArray[Real,<10>]}]



                  Flatten[{PackedArray[Real,<10>],PackedArray[Real,<10>]}]



                  Flatten[{PackedArray[Real,<10>],PackedArray[Real,<10>]}]




                  Notice how Merge builds a list of two packed arrays, and then flattens them. This is why the the arrays become unpacked. To workaround this, you can either convert the list of two packed arrays into a packed array first:



                  Developer`PackedArrayQ /@ Merge[{assoc, assoc}, Flatten @* Developer`ToPackedArray]



                  <|1 -> True, 2 -> True, 3 -> True|>




                  Or you can use Join with Apply (as in Henrik's answer, but using slot free syntax):



                  Developer`PackedArrayQ /@ Merge[{assoc, assoc}, Apply[Join]]



                  <|1 -> True, 2 -> True, 3 -> True|>







                  share|improve this answer









                  $endgroup$



                  One can use TracePrint to see how Flatten is being used by Merge:



                  TracePrint[
                  Merge[{assoc,assoc}, Flatten],
                  _Flatten,
                  TraceAction->Print@*Developer`PackedArrayForm
                  ];



                  Flatten[{PackedArray[Real,<10>],PackedArray[Real,<10>]}]



                  Flatten[{PackedArray[Real,<10>],PackedArray[Real,<10>]}]



                  Flatten[{PackedArray[Real,<10>],PackedArray[Real,<10>]}]




                  Notice how Merge builds a list of two packed arrays, and then flattens them. This is why the the arrays become unpacked. To workaround this, you can either convert the list of two packed arrays into a packed array first:



                  Developer`PackedArrayQ /@ Merge[{assoc, assoc}, Flatten @* Developer`ToPackedArray]



                  <|1 -> True, 2 -> True, 3 -> True|>




                  Or you can use Join with Apply (as in Henrik's answer, but using slot free syntax):



                  Developer`PackedArrayQ /@ Merge[{assoc, assoc}, Apply[Join]]



                  <|1 -> True, 2 -> True, 3 -> True|>








                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 8 hours ago









                  Carl WollCarl Woll

                  69.8k394180




                  69.8k394180























                      3












                      $begingroup$

                      Developer`PackedArrayQ /@ Merge[{assoc, assoc}, Join @@ # &]



                      <|1 -> True, 2 -> True, 3 -> True|>




                      One might expect that Catenate should also work, but it does not.






                      share|improve this answer









                      $endgroup$


















                        3












                        $begingroup$

                        Developer`PackedArrayQ /@ Merge[{assoc, assoc}, Join @@ # &]



                        <|1 -> True, 2 -> True, 3 -> True|>




                        One might expect that Catenate should also work, but it does not.






                        share|improve this answer









                        $endgroup$
















                          3












                          3








                          3





                          $begingroup$

                          Developer`PackedArrayQ /@ Merge[{assoc, assoc}, Join @@ # &]



                          <|1 -> True, 2 -> True, 3 -> True|>




                          One might expect that Catenate should also work, but it does not.






                          share|improve this answer









                          $endgroup$



                          Developer`PackedArrayQ /@ Merge[{assoc, assoc}, Join @@ # &]



                          <|1 -> True, 2 -> True, 3 -> True|>




                          One might expect that Catenate should also work, but it does not.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 8 hours ago









                          Henrik SchumacherHenrik Schumacher

                          55.8k576154




                          55.8k576154






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Mathematica Stack Exchange!


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


                              Use MathJax to format equations. MathJax reference.


                              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%2fmathematica.stackexchange.com%2fquestions%2f192984%2fis-it-possible-to-avoid-unpacking-when-merging-association%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

                              Histoire des bourses de valeurs

                              Why is there Russian traffic in my log files?

                              Rename multiple files to decrement number in file name?