4

I would like to generate an array bounded by a lower and upper value with n elements similar to:

def my_lin(lb, ub, steps):
    dx = (ub-lb) / (steps-1)
    return [lb + i*dx for i in range(steps)]

my_lin(0,10,11)

But I would like to have more values closer to the lower value. Some kind of harmonic spacing. I do not want to have a logarithmic spacing.

I guess it is rather simple but I cannot figure it out. Any help is highly appreciated.

EDIT:

I came up with following quick solution:

def harm_series(lb,n):
    return [float(lb)/float(i) for i in range(1,n) ]
5
  • 1
    maybe this could help you Commented Sep 10, 2015 at 14:33
  • Yep, math 1. semester. Long time ago .. Commented Sep 10, 2015 at 14:37
  • But I don`t get it why it has to be downvoted Commented Sep 10, 2015 at 14:39
  • probably, because people want to see an attempt and help with errors than writing code for someone... Commented Sep 10, 2015 at 14:40
  • 1
    Sometimes it is hard to find the proper word like "harmonic series" and googling does not help. But I will update the question with my solution. Commented Sep 10, 2015 at 14:43

1 Answer 1

3

Find a function f which has your desired spacing within [0,1] (note that f(0)==0 and f(1)==1 should hold) and then apply f(i*dx/(ub-lb))*(ub-lb).

I personally like power functions for this but there is many more. For example, try this:

def my_lin(lb, ub, steps, spacing=1.1):
    span = (ub-lb)
    dx = 1.0 / (steps-1)
    return [lb + (i*dx)**spacing*span for i in range(steps)]

lin = my_lin(0, 10, 11)
print lin
# visualize the spacing
print ''.join(' '*int(2*a)+'*' for a in lin)

Prints:

[0.0, 0.7943282347242814, 1.702679845041569, 2.6597044516956405, 3.6497741462219233, 4.665164957684037, 5.701201299034059, 6.754727665838724, 7.823462148343428, 8.905673323855929, 10.0]
* *   *     *       *         *           *             *               *                 *                    *

Now with the parameter spacing it works like this:

  • The closer you get to zero, the more your values gather around ub
  • The farther you go away from 1 into the other direction, the more they gather around lb
  • If spacing is exactly 1, they are equally spaced
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.