Skip to content

Add step parameter to linspace (or endpoint parameter to arange) (Trac #2176) #630

Open

Description

@numpy-gitbot

Original ticket http://projects.scipy.org/numpy/ticket/2176 on 2012-06-28 by @endolith, assigned to unknown.

There's no simple way to make a sequence based on step size that includes both endpoints. For instance, to produce the sequence [1.0, 1.1, 1.2, 1.3, 1.4, 1.5], you'd have to do something clunky like:

linspace(1.0, 1.5, (1.5-1.0)/0.1+1)

or

arange(1.0, 1.5+0.01, 0.1)

it would be more convenient if you could just say

linspace(1.0, 1.5, step=0.1)

or maybe

arange(1.0, 1.5, 0.1, endpoint=True)

Of course, step could not be specified at the same time as num.

Activity

  1. SjoerdOptLand commented on Jan 23, 2013

    @SjoerdOptLand

    I'd say the difference between linspace and arange is exactly that: obtaining a range by specifying number of steps or step size, respectively. For that reason, I favor your last proposal (extending the arange interface).

  2. njsmith commented on Jan 23, 2013

    @njsmith
    Member

    I'd have said the main difference between them is that linspace is all
    about floats and arange is all about ints. Of course you can override this,
    but that's what they do by default and that's what motivates the other
    differences. So I think adding a new argument to linspace is the most
    natural, but really both have their place and aren't redundant. These will
    still return different things:
    linspace(0, 10, step=1)
    arange(0, 10, include_endpoint=True)
    because the first will be a float array and the second an int array.

    We should think whether a similar step argument makes sense for logspace.
    On 23 Jan 2013 02:19, "SjoerdOptLand" [email protected] wrote:

    I'd say the difference between linspace and arange is exactly that:
    obtaining a range by specifying number of steps or step size, respectively.
    For that reason, I favor your last proposal (extending the arange
    interface).

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/630#issuecomment-12589259.

  3. seberg commented on Jan 23, 2013

    @seberg
    Member

    I am not sure you can resolve this really. Giving an endpoint argument to arange is OK for integer aranges, but considering how unsafe arange is for inexact types, it seems to just giving another reason to use it for something it should not be used for. The whole example arange(1.0, 1.5, 0.1, endpoint=True) is dangerous, and the endpoint argument gives an illusion that it will end with 1.5, when it might be ending with 1.6.
    As for allowing a step argument to linspace, I don't see how you would not just add the existing pitfalls of arange to linspace (and avoiding those is most of the point of linspace).

    Or would there be some method of actually guaranteeing to give the expected result for these cases? In that case it would be a good addition.

  4. njsmith commented on Jan 23, 2013

    @njsmith
    Member

    Hmm, yeah, good points :-)

    For linspace with a step= argument, I suppose we could impose the semantics
    that the space must be evenly divisible by the given step size. Then we
    can check this, and if it's almost true (within some relative precision),
    we force it to be exactly true, and if it's definitely not true, we error
    out. That should allow us to finesse the inexactness thing while also
    avoiding the usual problems of trying to guess whether to round or not...?

    On Wed, Jan 23, 2013 at 8:38 AM, seberg [email protected] wrote:

    I am not sure you can resolve this really. Giving an endpoint argument to
    arange is OK for integer aranges, but considering how unsafe arange is for
    inexact types, it seems to just giving another reason to use it for
    something it should not be used for. The whole example arange(1.0, 1.5,
    0.1, endpoint=True) is dangerous, and the endpoint argument gives an
    illusion that it will end with 1.5, when it might be ending with 1.6.
    As for allowing a step argument to linspace, I don't see how you would not
    just add the existing pitfalls of arange to linspace (and avoiding those is
    most of the point of linspace).

    Or would there be some method of actually guaranteeing to give the
    expected result for these cases? In that case it would be a good addition.

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/630#issuecomment-12604377.

  5. seberg commented on Jan 23, 2013

    @seberg
    Member

    Check gh-2945 for a stab at adding such a (convenience) functionality, which enforces that an integer number of steps can be safely determined.

  6. added a commit that references this issue on Feb 17, 2013
  7. PeterPablo commented on Jul 27, 2017

    @PeterPablo

    Please excuse me bringing up this old issue. As the proposed PR was not merged, was there by now added some similar functionality? (seberg@c012ea2 was not applied, was it?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions