np.clip with complex input is untested and has odd behavior
See original GitHub issueThis 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:
- Created 4 years ago
- Comments:9 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@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.realandmin.imag <= max.imagas opposed to onlymin <= max, than things are fine (although we currently do not actually ensure this in NumPy).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.