Define masked version of binary operations, where invalid values are pre-masked. Parameters ---------- mbfunc : function The function for which to define a masked version. Made available as ``_MaskedBinaryOperation.f``. domain : class instance Defaul
| 975 | |
| 976 | |
| 977 | class _MaskedBinaryOperation(_MaskedUFunc): |
| 978 | """ |
| 979 | Define masked version of binary operations, where invalid |
| 980 | values are pre-masked. |
| 981 | |
| 982 | Parameters |
| 983 | ---------- |
| 984 | mbfunc : function |
| 985 | The function for which to define a masked version. Made available |
| 986 | as ``_MaskedBinaryOperation.f``. |
| 987 | domain : class instance |
| 988 | Default domain for the function. Should be one of the ``_Domain*`` |
| 989 | classes. Default is None. |
| 990 | fillx : scalar, optional |
| 991 | Filling value for the first argument, default is 0. |
| 992 | filly : scalar, optional |
| 993 | Filling value for the second argument, default is 0. |
| 994 | |
| 995 | """ |
| 996 | |
| 997 | def __init__(self, mbfunc, fillx=0, filly=0): |
| 998 | """ |
| 999 | abfunc(fillx, filly) must be defined. |
| 1000 | |
| 1001 | abfunc(x, filly) = x for all x to enable reduce. |
| 1002 | |
| 1003 | """ |
| 1004 | super().__init__(mbfunc) |
| 1005 | self.fillx = fillx |
| 1006 | self.filly = filly |
| 1007 | ufunc_domain[mbfunc] = None |
| 1008 | ufunc_fills[mbfunc] = (fillx, filly) |
| 1009 | |
| 1010 | def __call__(self, a, b, *args, **kwargs): |
| 1011 | """ |
| 1012 | Execute the call behavior. |
| 1013 | |
| 1014 | """ |
| 1015 | # Get the data, as ndarray |
| 1016 | (da, db) = (getdata(a), getdata(b)) |
| 1017 | # Get the result |
| 1018 | with np.errstate(): |
| 1019 | np.seterr(divide='ignore', invalid='ignore') |
| 1020 | result = self.f(da, db, *args, **kwargs) |
| 1021 | # Get the mask for the result |
| 1022 | (ma, mb) = (getmask(a), getmask(b)) |
| 1023 | if ma is nomask: |
| 1024 | if mb is nomask: |
| 1025 | m = nomask |
| 1026 | else: |
| 1027 | m = umath.logical_or(getmaskarray(a), mb) |
| 1028 | elif mb is nomask: |
| 1029 | m = umath.logical_or(ma, getmaskarray(b)) |
| 1030 | else: |
| 1031 | m = umath.logical_or(ma, mb) |
| 1032 | |
| 1033 | # Case 1. : scalar |
| 1034 | if not result.ndim: |