never allocate an output of numpy.ufunc
This question has info on using an input as an output to compute something in place with a numpy.ufunc
:
- Numpy passing input array as `out` argument to ufunc
Is it possible to avoid allocating space for an unwanted output of a numpy.ufunc
? For example, say I only want one of the two outputs from modf
. Can I ensure that the other, unwanted array is never allocated at all?
I thought passing _
to out
might do it, but it throws an error:
import numpy as np
ar = np.arange(6)/3
np.modf(ar, out=(ar, _))
TypeError: return arrays must be of ArrayType
As it says in the docs, passing None
means that the output array is allocated in the function and returned. I can ignore the returned values, but it still has to be allocated and populated inside the function.
python numpy
add a comment |
This question has info on using an input as an output to compute something in place with a numpy.ufunc
:
- Numpy passing input array as `out` argument to ufunc
Is it possible to avoid allocating space for an unwanted output of a numpy.ufunc
? For example, say I only want one of the two outputs from modf
. Can I ensure that the other, unwanted array is never allocated at all?
I thought passing _
to out
might do it, but it throws an error:
import numpy as np
ar = np.arange(6)/3
np.modf(ar, out=(ar, _))
TypeError: return arrays must be of ArrayType
As it says in the docs, passing None
means that the output array is allocated in the function and returned. I can ignore the returned values, but it still has to be allocated and populated inside the function.
python numpy
1
Why worry about this? There aren't manyufunc
that return more than one array. How about usingnp.remainder(arr, 1)
instead?
– hpaulj
Nov 15 '18 at 23:21
This was more of a question about the limits ofufunc
than a particular use case. My additional reading is making me pretty sure it cannot be done.
– Paul T.
Nov 16 '18 at 0:29
add a comment |
This question has info on using an input as an output to compute something in place with a numpy.ufunc
:
- Numpy passing input array as `out` argument to ufunc
Is it possible to avoid allocating space for an unwanted output of a numpy.ufunc
? For example, say I only want one of the two outputs from modf
. Can I ensure that the other, unwanted array is never allocated at all?
I thought passing _
to out
might do it, but it throws an error:
import numpy as np
ar = np.arange(6)/3
np.modf(ar, out=(ar, _))
TypeError: return arrays must be of ArrayType
As it says in the docs, passing None
means that the output array is allocated in the function and returned. I can ignore the returned values, but it still has to be allocated and populated inside the function.
python numpy
This question has info on using an input as an output to compute something in place with a numpy.ufunc
:
- Numpy passing input array as `out` argument to ufunc
Is it possible to avoid allocating space for an unwanted output of a numpy.ufunc
? For example, say I only want one of the two outputs from modf
. Can I ensure that the other, unwanted array is never allocated at all?
I thought passing _
to out
might do it, but it throws an error:
import numpy as np
ar = np.arange(6)/3
np.modf(ar, out=(ar, _))
TypeError: return arrays must be of ArrayType
As it says in the docs, passing None
means that the output array is allocated in the function and returned. I can ignore the returned values, but it still has to be allocated and populated inside the function.
python numpy
python numpy
asked Nov 15 '18 at 23:12
Paul T.Paul T.
204210
204210
1
Why worry about this? There aren't manyufunc
that return more than one array. How about usingnp.remainder(arr, 1)
instead?
– hpaulj
Nov 15 '18 at 23:21
This was more of a question about the limits ofufunc
than a particular use case. My additional reading is making me pretty sure it cannot be done.
– Paul T.
Nov 16 '18 at 0:29
add a comment |
1
Why worry about this? There aren't manyufunc
that return more than one array. How about usingnp.remainder(arr, 1)
instead?
– hpaulj
Nov 15 '18 at 23:21
This was more of a question about the limits ofufunc
than a particular use case. My additional reading is making me pretty sure it cannot be done.
– Paul T.
Nov 16 '18 at 0:29
1
1
Why worry about this? There aren't many
ufunc
that return more than one array. How about using np.remainder(arr, 1)
instead?– hpaulj
Nov 15 '18 at 23:21
Why worry about this? There aren't many
ufunc
that return more than one array. How about using np.remainder(arr, 1)
instead?– hpaulj
Nov 15 '18 at 23:21
This was more of a question about the limits of
ufunc
than a particular use case. My additional reading is making me pretty sure it cannot be done.– Paul T.
Nov 16 '18 at 0:29
This was more of a question about the limits of
ufunc
than a particular use case. My additional reading is making me pretty sure it cannot be done.– Paul T.
Nov 16 '18 at 0:29
add a comment |
1 Answer
1
active
oldest
votes
You can minimize allocation by passing a "fake" array:
ar = np.arange(6) / 3
np.modf(ar, ar, np.broadcast_arrays(ar.dtype.type(0), ar)[0])
This dummy array is as big as a single double
, and modf
will not do allocation internally.
EDIT According to suggestions from @Eric and @hpaulj, a more general and long-term solution would be
np.lib.stride_tricks._broadcast_to(np.empty(1, ar.dtype), ar.shape, False, False)
1
np.broadcast_to(np.empty((), dtype), shape)
is a more general pattern, which works on non-numeric arrays too.
– Eric
Nov 16 '18 at 6:56
np.modf(ar, out=(ar, np.broadcast_to(np.empty((), ar.dtype), ar.shape)))
does not work because the second output is read only
– Paul T.
Nov 16 '18 at 15:48
This is neat! The second output is still an array of the right length, but all elements point to the same location in memory.
– Paul T.
Nov 16 '18 at 15:57
2
Bothbroadcast_to
andbroadcast_arrays
usenp.lib.stride_tricks._broadcast_to
. One uses areadonly=True
parameter, the other sets it toFalse
. But there's a TODO comment in the code about changing it toreadonly
, so in the long term this answer may need modification. It's interesting that the underlying tool in all of this isnditer
.
– hpaulj
Nov 17 '18 at 5:35
add a comment |
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
});
}
});
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%2fstackoverflow.com%2fquestions%2f53329181%2fnever-allocate-an-output-of-numpy-ufunc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can minimize allocation by passing a "fake" array:
ar = np.arange(6) / 3
np.modf(ar, ar, np.broadcast_arrays(ar.dtype.type(0), ar)[0])
This dummy array is as big as a single double
, and modf
will not do allocation internally.
EDIT According to suggestions from @Eric and @hpaulj, a more general and long-term solution would be
np.lib.stride_tricks._broadcast_to(np.empty(1, ar.dtype), ar.shape, False, False)
1
np.broadcast_to(np.empty((), dtype), shape)
is a more general pattern, which works on non-numeric arrays too.
– Eric
Nov 16 '18 at 6:56
np.modf(ar, out=(ar, np.broadcast_to(np.empty((), ar.dtype), ar.shape)))
does not work because the second output is read only
– Paul T.
Nov 16 '18 at 15:48
This is neat! The second output is still an array of the right length, but all elements point to the same location in memory.
– Paul T.
Nov 16 '18 at 15:57
2
Bothbroadcast_to
andbroadcast_arrays
usenp.lib.stride_tricks._broadcast_to
. One uses areadonly=True
parameter, the other sets it toFalse
. But there's a TODO comment in the code about changing it toreadonly
, so in the long term this answer may need modification. It's interesting that the underlying tool in all of this isnditer
.
– hpaulj
Nov 17 '18 at 5:35
add a comment |
You can minimize allocation by passing a "fake" array:
ar = np.arange(6) / 3
np.modf(ar, ar, np.broadcast_arrays(ar.dtype.type(0), ar)[0])
This dummy array is as big as a single double
, and modf
will not do allocation internally.
EDIT According to suggestions from @Eric and @hpaulj, a more general and long-term solution would be
np.lib.stride_tricks._broadcast_to(np.empty(1, ar.dtype), ar.shape, False, False)
1
np.broadcast_to(np.empty((), dtype), shape)
is a more general pattern, which works on non-numeric arrays too.
– Eric
Nov 16 '18 at 6:56
np.modf(ar, out=(ar, np.broadcast_to(np.empty((), ar.dtype), ar.shape)))
does not work because the second output is read only
– Paul T.
Nov 16 '18 at 15:48
This is neat! The second output is still an array of the right length, but all elements point to the same location in memory.
– Paul T.
Nov 16 '18 at 15:57
2
Bothbroadcast_to
andbroadcast_arrays
usenp.lib.stride_tricks._broadcast_to
. One uses areadonly=True
parameter, the other sets it toFalse
. But there's a TODO comment in the code about changing it toreadonly
, so in the long term this answer may need modification. It's interesting that the underlying tool in all of this isnditer
.
– hpaulj
Nov 17 '18 at 5:35
add a comment |
You can minimize allocation by passing a "fake" array:
ar = np.arange(6) / 3
np.modf(ar, ar, np.broadcast_arrays(ar.dtype.type(0), ar)[0])
This dummy array is as big as a single double
, and modf
will not do allocation internally.
EDIT According to suggestions from @Eric and @hpaulj, a more general and long-term solution would be
np.lib.stride_tricks._broadcast_to(np.empty(1, ar.dtype), ar.shape, False, False)
You can minimize allocation by passing a "fake" array:
ar = np.arange(6) / 3
np.modf(ar, ar, np.broadcast_arrays(ar.dtype.type(0), ar)[0])
This dummy array is as big as a single double
, and modf
will not do allocation internally.
EDIT According to suggestions from @Eric and @hpaulj, a more general and long-term solution would be
np.lib.stride_tricks._broadcast_to(np.empty(1, ar.dtype), ar.shape, False, False)
edited Nov 19 '18 at 2:32
answered Nov 16 '18 at 3:28
ZisIsNotZisZisIsNotZis
745620
745620
1
np.broadcast_to(np.empty((), dtype), shape)
is a more general pattern, which works on non-numeric arrays too.
– Eric
Nov 16 '18 at 6:56
np.modf(ar, out=(ar, np.broadcast_to(np.empty((), ar.dtype), ar.shape)))
does not work because the second output is read only
– Paul T.
Nov 16 '18 at 15:48
This is neat! The second output is still an array of the right length, but all elements point to the same location in memory.
– Paul T.
Nov 16 '18 at 15:57
2
Bothbroadcast_to
andbroadcast_arrays
usenp.lib.stride_tricks._broadcast_to
. One uses areadonly=True
parameter, the other sets it toFalse
. But there's a TODO comment in the code about changing it toreadonly
, so in the long term this answer may need modification. It's interesting that the underlying tool in all of this isnditer
.
– hpaulj
Nov 17 '18 at 5:35
add a comment |
1
np.broadcast_to(np.empty((), dtype), shape)
is a more general pattern, which works on non-numeric arrays too.
– Eric
Nov 16 '18 at 6:56
np.modf(ar, out=(ar, np.broadcast_to(np.empty((), ar.dtype), ar.shape)))
does not work because the second output is read only
– Paul T.
Nov 16 '18 at 15:48
This is neat! The second output is still an array of the right length, but all elements point to the same location in memory.
– Paul T.
Nov 16 '18 at 15:57
2
Bothbroadcast_to
andbroadcast_arrays
usenp.lib.stride_tricks._broadcast_to
. One uses areadonly=True
parameter, the other sets it toFalse
. But there's a TODO comment in the code about changing it toreadonly
, so in the long term this answer may need modification. It's interesting that the underlying tool in all of this isnditer
.
– hpaulj
Nov 17 '18 at 5:35
1
1
np.broadcast_to(np.empty((), dtype), shape)
is a more general pattern, which works on non-numeric arrays too.– Eric
Nov 16 '18 at 6:56
np.broadcast_to(np.empty((), dtype), shape)
is a more general pattern, which works on non-numeric arrays too.– Eric
Nov 16 '18 at 6:56
np.modf(ar, out=(ar, np.broadcast_to(np.empty((), ar.dtype), ar.shape)))
does not work because the second output is read only– Paul T.
Nov 16 '18 at 15:48
np.modf(ar, out=(ar, np.broadcast_to(np.empty((), ar.dtype), ar.shape)))
does not work because the second output is read only– Paul T.
Nov 16 '18 at 15:48
This is neat! The second output is still an array of the right length, but all elements point to the same location in memory.
– Paul T.
Nov 16 '18 at 15:57
This is neat! The second output is still an array of the right length, but all elements point to the same location in memory.
– Paul T.
Nov 16 '18 at 15:57
2
2
Both
broadcast_to
and broadcast_arrays
use np.lib.stride_tricks._broadcast_to
. One uses a readonly=True
parameter, the other sets it to False
. But there's a TODO comment in the code about changing it to readonly
, so in the long term this answer may need modification. It's interesting that the underlying tool in all of this is nditer
.– hpaulj
Nov 17 '18 at 5:35
Both
broadcast_to
and broadcast_arrays
use np.lib.stride_tricks._broadcast_to
. One uses a readonly=True
parameter, the other sets it to False
. But there's a TODO comment in the code about changing it to readonly
, so in the long term this answer may need modification. It's interesting that the underlying tool in all of this is nditer
.– hpaulj
Nov 17 '18 at 5:35
add a comment |
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.
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%2fstackoverflow.com%2fquestions%2f53329181%2fnever-allocate-an-output-of-numpy-ufunc%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
1
Why worry about this? There aren't many
ufunc
that return more than one array. How about usingnp.remainder(arr, 1)
instead?– hpaulj
Nov 15 '18 at 23:21
This was more of a question about the limits of
ufunc
than a particular use case. My additional reading is making me pretty sure it cannot be done.– Paul T.
Nov 16 '18 at 0:29