Skip to content

np.clip with complex input is untested and has odd behavior #15630

@rgommers

Description

@rgommers

This came up in pytorch/pytorch#33568. This seems odd:

>>> np.clip([3 + 4.j], -1, 2)                                                                                       
array([2.+0.j])
>>> np.clip([3 + 4.j], -1+1.j, 2+12.j)  # imaginary component goes up                                                                   
array([2.+12.j])
>>> np.clip([1 + 4.j], -1+1.j, 2+12.j)  # imaginary component doesn't go up                                                                             
array([1.+4.j])

The only test for complex input is that it doesn't segfault.

Reasonable behavior could be one of:

  • Clip real and imaginary parts separately
  • Clip absolute value, not changing the phase
    There may be other options. As long as it's documented, I'm not sure it matters much which choice is made.

I don't think this is a very important issue, but it would be nice to at least have the desired behavior documented here.

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