A version of sqrt that properly handles scalar masked arrays. To mimic ``np.ma`` reductions, we need to convert scalar masked arrays that have an active mask to the ``np.ma.masked`` singleton. This is properly handled automatically for reduction code, but not for ufuncs. We implement
(a)
| 697 | |
| 698 | |
| 699 | def safe_sqrt(a): |
| 700 | """A version of sqrt that properly handles scalar masked arrays. |
| 701 | |
| 702 | To mimic ``np.ma`` reductions, we need to convert scalar masked arrays that |
| 703 | have an active mask to the ``np.ma.masked`` singleton. This is properly |
| 704 | handled automatically for reduction code, but not for ufuncs. We implement |
| 705 | a simple version here, since calling `np.ma.sqrt` everywhere is |
| 706 | significantly more expensive. |
| 707 | """ |
| 708 | if hasattr(a, "_elemwise"): |
| 709 | return a._elemwise(_sqrt, a) |
| 710 | return _sqrt(a) |
| 711 | |
| 712 | |
| 713 | @derived_from(np) |