def bind_class(self, cls): new = Dispatcher(self.name, self.doc) for ts, name in self.funcs.items(): new.add((object, *ts), getattr(cls, name)) return new
axis=axis) dispatch_select_slice = Dispatcher("select_slice") def group_select_slice(o, *, start=None, stop=None, step=None, axis=0): return GroupSelection(o, select_slice, start=start, stop=stop, step=step, axis=axis) dispatch_select_slice.add((Mapping, ), group_select_slice) def select_indices(o, indices, *, axis=0): """TODO""" return dispatch_select_indices(o, indices, axis=axis) dispatch_select_indices = Dispatcher("select_indices") def group_select_indices(o, indices, *, axis=0): return GroupSelection(o, select_indices, indices, axis=axis)
__all__ = math_names + reduction_names types = {builtins: object, np: (np.ndarray, np.number), pymath: Number, blazemath: Expr, reductions: Expr} for funcname in math_names: # sin, sqrt, ceil, ... d = Dispatcher(funcname) for module, typ in types.items(): if hasattr(module, funcname): d.add((typ,), getattr(module, funcname)) namespace[funcname] = d locals()[funcname] = d for funcname in reduction_names: # any, all, sum, max, ... d = Dispatcher(funcname) for module, typ in types.items(): if hasattr(module, funcname): d.add((typ,), getattr(module, funcname)) namespace[funcname] = d locals()[funcname] = d
__all__ = math_names + reduction_names types = { builtins: object, np: (np.ndarray, np.number), pymath: Number, blazemath: Expr, reductions: Expr } for funcname in math_names: # sin, sqrt, ceil, ... d = Dispatcher(funcname) for module, typ in types.items(): if hasattr(module, funcname): d.add((typ, ), getattr(module, funcname)) namespace[funcname] = d locals()[funcname] = d for funcname in reduction_names: # any, all, sum, max, ... d = Dispatcher(funcname) for module, typ in types.items(): if hasattr(module, funcname): d.add((typ, ), getattr(module, funcname)) namespace[funcname] = d locals()[funcname] = d
pymath: max, }, 'least': { builtins: min, np: np.minimum, pymath: min, } } for funcname in math_names: # sin, sqrt, ceil, ... d = Dispatcher(funcname) for module, typ in types.items(): if hasattr(module, funcname): d.add((typ,), getattr(module, funcname)) namespace[funcname] = d locals()[funcname] = d for funcname in binary_math_names: # hypot, atan2, fmod, ... d = Dispatcher(funcname) for module, pairs in binary_types.items(): for pair in pairs: if hasattr(module, funcname): d.add(pair, getattr(module, funcname)) elif funcname in fallback_binary_mappings: assert module in fallback_binary_mappings[funcname], module.__name__ d.add(pair, fallback_binary_mappings[funcname][module])
np: (np.ndarray, np.number), pymath: Number} def switch(funcname, x): f = getattr(blazemath, funcname) if iscollection(x.dshape): return broadcast(f, x) else: return f(x) for funcname in math_names: # sin, sqrt, ceil, ... d = Dispatcher(funcname) d.add((Expr,), curry(switch, funcname)) for module, typ in types.items(): if hasattr(module, funcname): d.add((typ,), getattr(module, funcname)) namespace[funcname] = d locals()[funcname] = d for funcname in reduction_names: # any, all, sum, max, ... d = Dispatcher(funcname) d.add((Expr,), getattr(reductions, funcname)) for module, typ in types.items():
#'or', #'xor', #] _BINARY_OPERATORS = _BINARY_OPERATORS_WITH_REVERSE + _BINARY_OPERATORS_WITHOUT_REVERSE #+ _BINARY_LOGICAL_OPERATORS import functools # Binary operators using multiple dispatch for op in _BINARY_OPERATORS: # Create a dispatcher for each operator D = Dispatcher(op) # And store the dispatcher on this module setattr(_thismodule, op, D) # Furthermore, we like to add (object, object) operations D.add((object, object), getattr(operator, op)) # Logical AND @dispatch(object, object) def logical_and(a, b): return a and b @dispatch(object, np.ndarray) def logical_and(a, b): return np.logical_and(a, b) @dispatch(np.ndarray, object) def logical_and(a, b): return np.logical_and(a, b)
types = {builtins: object, np: (np.ndarray, np.number), pymath: Number} def switch(funcname, x): f = getattr(blazemath, funcname) if iscollection(x.dshape): return broadcast(f, x) else: return f(x) for funcname in math_names: # sin, sqrt, ceil, ... d = Dispatcher(funcname) d.add((Expr, ), curry(switch, funcname)) for module, typ in types.items(): if hasattr(module, funcname): d.add((typ, ), getattr(module, funcname)) namespace[funcname] = d locals()[funcname] = d for funcname in reduction_names: # any, all, sum, max, ... d = Dispatcher(funcname) d.add((Expr, ), getattr(reductions, funcname)) for module, typ in types.items(): if hasattr(module, funcname):