I am using numpy.linspace to let other functions sweep over some parameters, for example:
def fun(array):
newarray= []
for i in array:
newarray.append(i**2)
return newarray
Now I want to pass this function numpy.linspace(0,20,30)
which has to contain the number 2.
Is there some way to force this without adjusting the boundaries and the number of points that have to be generated?
Beware: I am not interested on inserting it afterwards, I just want to know If there is a way to do this upon generation!
CodePudding user response:
If I understand your question correctly you want a numpy array that consists of 30 values that are equally spaced between 0 and 20, containing 0 and 20, but also to contain the value of 2.0 exactly? That is not possible, since the steps will not be equally spaced anymore. You either have to adjust the boundaries, or the number of steps, for instance 31 steps will get you a nice spacing of exactly 2/3:
>>> a = np.linspace(0,20,31)
>>> a
array([ 0. , 0.66666667, 1.33333333, 2. , 2.66666667,
3.33333333, 4. , 4.66666667, 5.33333333, 6. ,
6.66666667, 7.33333333, 8. , 8.66666667, 9.33333333,
10. , 10.66666667, 11.33333333, 12. , 12.66666667,
13.33333333, 14. , 14.66666667, 15.33333333, 16. ,
16.66666667, 17.33333333, 18. , 18.66666667, 19.33333333,
20. ])