question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

This came up in https://github.com/pytorch/pytorch/issues/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.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
sebergcommented, Apr 10, 2020

@mruberry, I agreed, in any case I am not even sure anyone had even a use-case for this, which would go a long way to motivate me to put in anything. As one argument to make it a bit less bad, if you ensure that min.real <= max.real and min.imag <= max.imag as opposed to only min <= max, than things are fine (although we currently do not actually ensure this in NumPy).

1reaction
anirudh2290commented, May 27, 2020

just reading through this issue, i think that given the lexicographic comparison in numpy, this behavior actually makes sense. if we are deprecating lexicographic comparison though, clip, max and min should also follow. I am assuming that this was the consensus reached here, but I wasn’t very sure from the comments and the linked PRs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to clip the real and imaginary parts of elements of a ...
I can achieve this by calling np.clip on the real and imaginary parts of the complex array and then adding them (after multiplying...
Read more >
numpy.clip — NumPy v1.24 Manual
When a_min is greater than a_max, clip returns an array in which all values are equal to a_max, as shown in the second...
Read more >
How to replace all odd numbers in a NumPy array with - Quora
# Replace odd numbers by ''-1'. mask = np.mod(x,2). x[np.mod(x, ...
Read more >
Release 2.5.1 Brian authors - Brian 2 documentation
Brian is a simulator for spiking neural networks. It is written in the Python programming language and is available on almost all platforms....
Read more >
Autism Spectrum Disorder - ASHA
Recognition of unusual reactions to sensory input (e.g., ... Individuals with ASD report a desire to have friendships and relationships, despite their ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found