Exponent an arbitrary number by a numpy array
I know np.exp2(x)
exists that calculates 2^x
where x
is a numpy array, however, I am looking for a method that does K^x
where K
is any arbitrary number.
Is there any elegant way of doing it rather than stretching out K
to the shape of x
and doing a piecewise exponent?
python numpy
add a comment |
I know np.exp2(x)
exists that calculates 2^x
where x
is a numpy array, however, I am looking for a method that does K^x
where K
is any arbitrary number.
Is there any elegant way of doing it rather than stretching out K
to the shape of x
and doing a piecewise exponent?
python numpy
@tel: The "piecewise" seems to make things more confusing rather than less.
– user2357112
Nov 22 '18 at 8:24
add a comment |
I know np.exp2(x)
exists that calculates 2^x
where x
is a numpy array, however, I am looking for a method that does K^x
where K
is any arbitrary number.
Is there any elegant way of doing it rather than stretching out K
to the shape of x
and doing a piecewise exponent?
python numpy
I know np.exp2(x)
exists that calculates 2^x
where x
is a numpy array, however, I am looking for a method that does K^x
where K
is any arbitrary number.
Is there any elegant way of doing it rather than stretching out K
to the shape of x
and doing a piecewise exponent?
python numpy
python numpy
edited Nov 22 '18 at 8:23
user2357112
155k12167260
155k12167260
asked Nov 22 '18 at 5:01
Vj-Vj-
656617
656617
@tel: The "piecewise" seems to make things more confusing rather than less.
– user2357112
Nov 22 '18 at 8:24
add a comment |
@tel: The "piecewise" seems to make things more confusing rather than less.
– user2357112
Nov 22 '18 at 8:24
@tel: The "piecewise" seems to make things more confusing rather than less.
– user2357112
Nov 22 '18 at 8:24
@tel: The "piecewise" seems to make things more confusing rather than less.
– user2357112
Nov 22 '18 at 8:24
add a comment |
2 Answers
2
active
oldest
votes
Just use the standard Python exponentiation operator **
:
K**x
For example, if you have:
x = np.array([1,2,3])
K = 3
print(K**x)
The output is:
[ 3 9 27]
Notes
For Python classes, the behavior of the binary **
operator is implemented via the __pow__
, __rpow__
, and __ipow__
magic methods (the reality for np.ndarray
is slightly more complicated since it's implemented in the C layer, but that's not actually important here). For Numpy arrays, these magic methods in turn appear to call numpy.power
, so you can expect that **
will have the same behavior as documented for numpy.power
. In particular,
Note that an integer type raised to a negative integer power will raise a ValueError.
small note: castK
to a float or else negative numbers in the array will throw an error.
– Vj-
Nov 22 '18 at 5:14
@Vj- I added a note about the implementation of**
, including where it mentions that particular error in the documentation
– tel
Nov 22 '18 at 5:32
add a comment |
With numpy you can just use numpy.power
arr = numpy.array([1,2,3])
print(numpy.power(3,arr)) # Outputs [ 3 9 27]
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%2f53424165%2fexponent-an-arbitrary-number-by-a-numpy-array%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
Just use the standard Python exponentiation operator **
:
K**x
For example, if you have:
x = np.array([1,2,3])
K = 3
print(K**x)
The output is:
[ 3 9 27]
Notes
For Python classes, the behavior of the binary **
operator is implemented via the __pow__
, __rpow__
, and __ipow__
magic methods (the reality for np.ndarray
is slightly more complicated since it's implemented in the C layer, but that's not actually important here). For Numpy arrays, these magic methods in turn appear to call numpy.power
, so you can expect that **
will have the same behavior as documented for numpy.power
. In particular,
Note that an integer type raised to a negative integer power will raise a ValueError.
small note: castK
to a float or else negative numbers in the array will throw an error.
– Vj-
Nov 22 '18 at 5:14
@Vj- I added a note about the implementation of**
, including where it mentions that particular error in the documentation
– tel
Nov 22 '18 at 5:32
add a comment |
Just use the standard Python exponentiation operator **
:
K**x
For example, if you have:
x = np.array([1,2,3])
K = 3
print(K**x)
The output is:
[ 3 9 27]
Notes
For Python classes, the behavior of the binary **
operator is implemented via the __pow__
, __rpow__
, and __ipow__
magic methods (the reality for np.ndarray
is slightly more complicated since it's implemented in the C layer, but that's not actually important here). For Numpy arrays, these magic methods in turn appear to call numpy.power
, so you can expect that **
will have the same behavior as documented for numpy.power
. In particular,
Note that an integer type raised to a negative integer power will raise a ValueError.
small note: castK
to a float or else negative numbers in the array will throw an error.
– Vj-
Nov 22 '18 at 5:14
@Vj- I added a note about the implementation of**
, including where it mentions that particular error in the documentation
– tel
Nov 22 '18 at 5:32
add a comment |
Just use the standard Python exponentiation operator **
:
K**x
For example, if you have:
x = np.array([1,2,3])
K = 3
print(K**x)
The output is:
[ 3 9 27]
Notes
For Python classes, the behavior of the binary **
operator is implemented via the __pow__
, __rpow__
, and __ipow__
magic methods (the reality for np.ndarray
is slightly more complicated since it's implemented in the C layer, but that's not actually important here). For Numpy arrays, these magic methods in turn appear to call numpy.power
, so you can expect that **
will have the same behavior as documented for numpy.power
. In particular,
Note that an integer type raised to a negative integer power will raise a ValueError.
Just use the standard Python exponentiation operator **
:
K**x
For example, if you have:
x = np.array([1,2,3])
K = 3
print(K**x)
The output is:
[ 3 9 27]
Notes
For Python classes, the behavior of the binary **
operator is implemented via the __pow__
, __rpow__
, and __ipow__
magic methods (the reality for np.ndarray
is slightly more complicated since it's implemented in the C layer, but that's not actually important here). For Numpy arrays, these magic methods in turn appear to call numpy.power
, so you can expect that **
will have the same behavior as documented for numpy.power
. In particular,
Note that an integer type raised to a negative integer power will raise a ValueError.
edited Nov 22 '18 at 5:37
answered Nov 22 '18 at 5:07
teltel
7,41621431
7,41621431
small note: castK
to a float or else negative numbers in the array will throw an error.
– Vj-
Nov 22 '18 at 5:14
@Vj- I added a note about the implementation of**
, including where it mentions that particular error in the documentation
– tel
Nov 22 '18 at 5:32
add a comment |
small note: castK
to a float or else negative numbers in the array will throw an error.
– Vj-
Nov 22 '18 at 5:14
@Vj- I added a note about the implementation of**
, including where it mentions that particular error in the documentation
– tel
Nov 22 '18 at 5:32
small note: cast
K
to a float or else negative numbers in the array will throw an error.– Vj-
Nov 22 '18 at 5:14
small note: cast
K
to a float or else negative numbers in the array will throw an error.– Vj-
Nov 22 '18 at 5:14
@Vj- I added a note about the implementation of
**
, including where it mentions that particular error in the documentation– tel
Nov 22 '18 at 5:32
@Vj- I added a note about the implementation of
**
, including where it mentions that particular error in the documentation– tel
Nov 22 '18 at 5:32
add a comment |
With numpy you can just use numpy.power
arr = numpy.array([1,2,3])
print(numpy.power(3,arr)) # Outputs [ 3 9 27]
add a comment |
With numpy you can just use numpy.power
arr = numpy.array([1,2,3])
print(numpy.power(3,arr)) # Outputs [ 3 9 27]
add a comment |
With numpy you can just use numpy.power
arr = numpy.array([1,2,3])
print(numpy.power(3,arr)) # Outputs [ 3 9 27]
With numpy you can just use numpy.power
arr = numpy.array([1,2,3])
print(numpy.power(3,arr)) # Outputs [ 3 9 27]
answered Nov 22 '18 at 5:11
b-fgb-fg
1,95911522
1,95911522
add a comment |
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%2f53424165%2fexponent-an-arbitrary-number-by-a-numpy-array%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
@tel: The "piecewise" seems to make things more confusing rather than less.
– user2357112
Nov 22 '18 at 8:24