Here is a piece of code:
np.concatenate(([3], [0]*5, np.arange(-1, 1.002, 2/9.0)))
# the above outputs
array([ 3. , 0. , 0. , 0. , 0. ,
0. , -1. , -0.77777778, -0.55555556, -0.33333333,
-0.11111111, 0.11111111, 0.33333333, 0.55555556, 0.77777778,
1. ])
Although this is verbose, it's pretty understandable. And here's another way to get the same output using an (ab)used notation, with complex number as step size.
np.r_[3, [0]*5, -1:1:10j]
# the above outputs
array([ 3. , 0. , 0. , 0. , 0. ,
0. , -1. , -0.77777778, -0.55555556, -0.33333333,
-0.11111111, 0.11111111, 0.33333333, 0.55555556, 0.77777778,
1. ])
I'm trying to understand how the step size in the first approach is equivalent to the complex number step size (10j
) in the second approach.
2/9.0 == 10j # how?
I have read in scipy reference documentation
that -1:1:10j
means we want to produce 10 values between -1:1
, both sides inclusive. But, how does that 10j
translate to 0.2222
?
- Any intuitive ideas or explanations?
- Also, what are other useful NumPy examples that we can do with this sort of expression?
P.S. I have already looked at range-builder-r-slice-with-complex-but-not-imaginary-step-magnitude but that doesn't offer much ideas.