Is it possible to avoid unpacking when merging Association?
$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?
list-manipulation associations packed-arrays
$endgroup$
add a comment |
$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?
list-manipulation associations packed-arrays
$endgroup$
add a comment |
$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?
list-manipulation associations packed-arrays
$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
list-manipulation associations packed-arrays
asked 8 hours ago
matheoremmatheorem
6,56743178
6,56743178
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
$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|>
$endgroup$
add a comment |
$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.
$endgroup$
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
$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|>
$endgroup$
add a comment |
$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|>
$endgroup$
add a comment |
$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|>
$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|>
answered 8 hours ago
Carl WollCarl Woll
69.8k394180
69.8k394180
add a comment |
add a comment |
$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.
$endgroup$
add a comment |
$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.
$endgroup$
add a comment |
$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.
$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.
answered 8 hours ago
Henrik SchumacherHenrik Schumacher
55.8k576154
55.8k576154
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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