1.6.259. numpy.set_numeric_ops¶
-
numpy.set_numeric_ops(op1=func1, op2=func2, ...)¶ Set numerical operators for array objects.
Parameters: op1, op2, ... : callable
Each
op = funcpair describes an operator to be replaced. For example,add = lambda x, y: np.add(x, y) % 5would replace addition by modulus 5 addition.Returns: saved_ops : list of callables
A list of all operators, stored before making replacements.
Notes
Warning
Use with care! Incorrect usage may lead to memory errors.
A function replacing an operator cannot make use of that operator. For example, when replacing add, you may not use
+. Instead, directly call ufuncs.Examples
>>> def add_mod5(x, y): ... return np.add(x, y) % 5 ... >>> old_funcs = np.set_numeric_ops(add=add_mod5)
>>> x = np.arange(12).reshape((3, 4)) >>> x + x array([[0, 2, 4, 1], [3, 0, 2, 4], [1, 3, 0, 2]])
>>> ignore = np.set_numeric_ops(**old_funcs) # restore operators