Python Linspace passing in function return only one number
There is two case, the first one, my function Y1 return always the same number. In that case, it doesn't work, y is equal to a integer of 10 and not a array of one thousand 10. The second case, where it's return differents numbers, it works!
First case (Doesn't work as expected)
def Y1(x, N):
return 10
x= np.linspace(-2,2,1000)
y= Y1(x,0) #In that case, it should create a array with 1000 numbers, but it only return one int, 10.
y value: 10 #when it should be [10 10 10 10 10 10...]
The othercases (Does work as expected)
def Y1(x, N):
return x**2
x= np.linspace(-2,2,1000)
y= Y1(x,0) #it returns a array, all numbers are differents
y value:
[4.00000000e+00 3.98400002e+00 3.96803210e+00 3.95209624e+00
3.93619245e+00 3.92032072e+00 3.90448106e+00 3.88867346e+00
3.87289792e+00 3.85715445e+00 3.84144304e+00 3.82576370e+00
3.81011642e+00 3.79450121e+00 3.77891806e+00 3.76336697e+00
...]
Thank you!
python numpy
add a comment |
There is two case, the first one, my function Y1 return always the same number. In that case, it doesn't work, y is equal to a integer of 10 and not a array of one thousand 10. The second case, where it's return differents numbers, it works!
First case (Doesn't work as expected)
def Y1(x, N):
return 10
x= np.linspace(-2,2,1000)
y= Y1(x,0) #In that case, it should create a array with 1000 numbers, but it only return one int, 10.
y value: 10 #when it should be [10 10 10 10 10 10...]
The othercases (Does work as expected)
def Y1(x, N):
return x**2
x= np.linspace(-2,2,1000)
y= Y1(x,0) #it returns a array, all numbers are differents
y value:
[4.00000000e+00 3.98400002e+00 3.96803210e+00 3.95209624e+00
3.93619245e+00 3.92032072e+00 3.90448106e+00 3.88867346e+00
3.87289792e+00 3.85715445e+00 3.84144304e+00 3.82576370e+00
3.81011642e+00 3.79450121e+00 3.77891806e+00 3.76336697e+00
...]
Thank you!
python numpy
3
Um, why do you expect an array to be returned fromY1
when you are only returning an integer?
– R. Kap
Nov 20 '18 at 23:22
I added the values generated for both case.
– Marc Bourque
Nov 20 '18 at 23:31
You seriously believe that passing a numpy array as a parameter, that you don't even access from within the function, is somehow going to affect the function's result?
– jasonharper
Nov 20 '18 at 23:46
Sorry, I thought he was calling the function for each element of the array and not that he was passing the array in the parameter. I didn't know that you can make a exposant to an array without a loop. First time using python.
– Marc Bourque
Nov 20 '18 at 23:59
add a comment |
There is two case, the first one, my function Y1 return always the same number. In that case, it doesn't work, y is equal to a integer of 10 and not a array of one thousand 10. The second case, where it's return differents numbers, it works!
First case (Doesn't work as expected)
def Y1(x, N):
return 10
x= np.linspace(-2,2,1000)
y= Y1(x,0) #In that case, it should create a array with 1000 numbers, but it only return one int, 10.
y value: 10 #when it should be [10 10 10 10 10 10...]
The othercases (Does work as expected)
def Y1(x, N):
return x**2
x= np.linspace(-2,2,1000)
y= Y1(x,0) #it returns a array, all numbers are differents
y value:
[4.00000000e+00 3.98400002e+00 3.96803210e+00 3.95209624e+00
3.93619245e+00 3.92032072e+00 3.90448106e+00 3.88867346e+00
3.87289792e+00 3.85715445e+00 3.84144304e+00 3.82576370e+00
3.81011642e+00 3.79450121e+00 3.77891806e+00 3.76336697e+00
...]
Thank you!
python numpy
There is two case, the first one, my function Y1 return always the same number. In that case, it doesn't work, y is equal to a integer of 10 and not a array of one thousand 10. The second case, where it's return differents numbers, it works!
First case (Doesn't work as expected)
def Y1(x, N):
return 10
x= np.linspace(-2,2,1000)
y= Y1(x,0) #In that case, it should create a array with 1000 numbers, but it only return one int, 10.
y value: 10 #when it should be [10 10 10 10 10 10...]
The othercases (Does work as expected)
def Y1(x, N):
return x**2
x= np.linspace(-2,2,1000)
y= Y1(x,0) #it returns a array, all numbers are differents
y value:
[4.00000000e+00 3.98400002e+00 3.96803210e+00 3.95209624e+00
3.93619245e+00 3.92032072e+00 3.90448106e+00 3.88867346e+00
3.87289792e+00 3.85715445e+00 3.84144304e+00 3.82576370e+00
3.81011642e+00 3.79450121e+00 3.77891806e+00 3.76336697e+00
...]
Thank you!
python numpy
python numpy
edited Nov 20 '18 at 23:47
Marc Bourque
asked Nov 20 '18 at 23:17


Marc BourqueMarc Bourque
479
479
3
Um, why do you expect an array to be returned fromY1
when you are only returning an integer?
– R. Kap
Nov 20 '18 at 23:22
I added the values generated for both case.
– Marc Bourque
Nov 20 '18 at 23:31
You seriously believe that passing a numpy array as a parameter, that you don't even access from within the function, is somehow going to affect the function's result?
– jasonharper
Nov 20 '18 at 23:46
Sorry, I thought he was calling the function for each element of the array and not that he was passing the array in the parameter. I didn't know that you can make a exposant to an array without a loop. First time using python.
– Marc Bourque
Nov 20 '18 at 23:59
add a comment |
3
Um, why do you expect an array to be returned fromY1
when you are only returning an integer?
– R. Kap
Nov 20 '18 at 23:22
I added the values generated for both case.
– Marc Bourque
Nov 20 '18 at 23:31
You seriously believe that passing a numpy array as a parameter, that you don't even access from within the function, is somehow going to affect the function's result?
– jasonharper
Nov 20 '18 at 23:46
Sorry, I thought he was calling the function for each element of the array and not that he was passing the array in the parameter. I didn't know that you can make a exposant to an array without a loop. First time using python.
– Marc Bourque
Nov 20 '18 at 23:59
3
3
Um, why do you expect an array to be returned from
Y1
when you are only returning an integer?– R. Kap
Nov 20 '18 at 23:22
Um, why do you expect an array to be returned from
Y1
when you are only returning an integer?– R. Kap
Nov 20 '18 at 23:22
I added the values generated for both case.
– Marc Bourque
Nov 20 '18 at 23:31
I added the values generated for both case.
– Marc Bourque
Nov 20 '18 at 23:31
You seriously believe that passing a numpy array as a parameter, that you don't even access from within the function, is somehow going to affect the function's result?
– jasonharper
Nov 20 '18 at 23:46
You seriously believe that passing a numpy array as a parameter, that you don't even access from within the function, is somehow going to affect the function's result?
– jasonharper
Nov 20 '18 at 23:46
Sorry, I thought he was calling the function for each element of the array and not that he was passing the array in the parameter. I didn't know that you can make a exposant to an array without a loop. First time using python.
– Marc Bourque
Nov 20 '18 at 23:59
Sorry, I thought he was calling the function for each element of the array and not that he was passing the array in the parameter. I didn't know that you can make a exposant to an array without a loop. First time using python.
– Marc Bourque
Nov 20 '18 at 23:59
add a comment |
1 Answer
1
active
oldest
votes
In the first case, you are returning the constant 10
.
In the second case, you are applying the operator **2
to a np array, which overloads **
and applies the power operation element-wise to the full array. This behaviour is known as broadcasting or vectorized aritmethic operations.
This broadcasting overload of the aritmethic methods allows behaviours as the following:
np.array([1, 2, 3, 4]) + 1
>>> array([2, 3, 4, 5])
Which is what your second case uses and your first one does not.
You can learn more about this topic, for example, here.
If you want an array of shape 1000 full of 10s use numpy.full instead:
import numpy as np
y = np.full(1000, 10)
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%2f53403070%2fpython-linspace-passing-in-function-return-only-one-number%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
In the first case, you are returning the constant 10
.
In the second case, you are applying the operator **2
to a np array, which overloads **
and applies the power operation element-wise to the full array. This behaviour is known as broadcasting or vectorized aritmethic operations.
This broadcasting overload of the aritmethic methods allows behaviours as the following:
np.array([1, 2, 3, 4]) + 1
>>> array([2, 3, 4, 5])
Which is what your second case uses and your first one does not.
You can learn more about this topic, for example, here.
If you want an array of shape 1000 full of 10s use numpy.full instead:
import numpy as np
y = np.full(1000, 10)
add a comment |
In the first case, you are returning the constant 10
.
In the second case, you are applying the operator **2
to a np array, which overloads **
and applies the power operation element-wise to the full array. This behaviour is known as broadcasting or vectorized aritmethic operations.
This broadcasting overload of the aritmethic methods allows behaviours as the following:
np.array([1, 2, 3, 4]) + 1
>>> array([2, 3, 4, 5])
Which is what your second case uses and your first one does not.
You can learn more about this topic, for example, here.
If you want an array of shape 1000 full of 10s use numpy.full instead:
import numpy as np
y = np.full(1000, 10)
add a comment |
In the first case, you are returning the constant 10
.
In the second case, you are applying the operator **2
to a np array, which overloads **
and applies the power operation element-wise to the full array. This behaviour is known as broadcasting or vectorized aritmethic operations.
This broadcasting overload of the aritmethic methods allows behaviours as the following:
np.array([1, 2, 3, 4]) + 1
>>> array([2, 3, 4, 5])
Which is what your second case uses and your first one does not.
You can learn more about this topic, for example, here.
If you want an array of shape 1000 full of 10s use numpy.full instead:
import numpy as np
y = np.full(1000, 10)
In the first case, you are returning the constant 10
.
In the second case, you are applying the operator **2
to a np array, which overloads **
and applies the power operation element-wise to the full array. This behaviour is known as broadcasting or vectorized aritmethic operations.
This broadcasting overload of the aritmethic methods allows behaviours as the following:
np.array([1, 2, 3, 4]) + 1
>>> array([2, 3, 4, 5])
Which is what your second case uses and your first one does not.
You can learn more about this topic, for example, here.
If you want an array of shape 1000 full of 10s use numpy.full instead:
import numpy as np
y = np.full(1000, 10)
answered Nov 20 '18 at 23:46
Julian PellerJulian Peller
8941511
8941511
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%2f53403070%2fpython-linspace-passing-in-function-return-only-one-number%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
3
Um, why do you expect an array to be returned from
Y1
when you are only returning an integer?– R. Kap
Nov 20 '18 at 23:22
I added the values generated for both case.
– Marc Bourque
Nov 20 '18 at 23:31
You seriously believe that passing a numpy array as a parameter, that you don't even access from within the function, is somehow going to affect the function's result?
– jasonharper
Nov 20 '18 at 23:46
Sorry, I thought he was calling the function for each element of the array and not that he was passing the array in the parameter. I didn't know that you can make a exposant to an array without a loop. First time using python.
– Marc Bourque
Nov 20 '18 at 23:59