Skip to content

BUG: arange behaves poorly on complex numbers #10332

@eric-wieser

Description

@eric-wieser
>>> np.arange(0, 10, 2)
array([0, 2, 4, 6, 8])

# as above, cast to complex
>>> np.arange(0 + 0j, 10 + 0j, 2 + 0j)
array([], dtype=complex128)  #what?

# bizarre input needed to give expected output
>>> np.arange(0 + 0j, 10 + 10j, 2 + 0j)
# array([ 0.+0.j,  2.+0.j,  4.+0.j,  6.+0.j,  8.+0.j])

For some reason, it deliberately computes len as (c14792d)

c_len = (start - stop) / step
len = min(ceil(c_len.real), ceil(c_len.imag))

which for real-only values, gives 0.

I think a better approach would be to use one of

  1. ceil(abs(c_len)) - has semantics "use the circle centered at start and passing through end" as the end point

  2. ceil(c_len.real) - has semantics "use the projection of end onto the line start + k*step as the endpoint"

  3. above with assert c_len.imag == 0 - requires that end lies on that line, but prone to rounding error

  4. Project step onto the line between start and end, and use that instead

I like the look of 2 and 4

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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy