Much thanks! Another related question while I am at it. It says clip is supposed to be faster than np.maximum(mp.minumum(arr, max), min). However: a = np.arange(100) %timeit a.clip(4, 20) # 8.48 µs %timeit np.maximum(np.minimum(a, 20), 4) # 2.09 µs Is this expected?
Regards, dg > On 10 Mar 2024, at 09:59, Ralf Gommers <[email protected]> wrote: > > > > On Sat, Mar 9, 2024 at 11:23 PM Dom Grigonis <[email protected] > <mailto:[email protected]>> wrote: > Hello, > > Can't find answer to this anywhere. > > What I would like is to automatically clip the values if they breach the > bounds. > > I have done a simple clipping, and overwritten __iadd__, __isub__, > __setitem__, … > > But I am wandering if there is a specified way to do this. Or maybe at least > a centralised place exists to do such thing? E.g. Only 1 method to override? > > That centralized method is `__array_wrap__`; a subclass that implements > `__array_wrap__` by applying `np.clip` and then returning self should do this > I think. > > Cheers, > Ralf > _______________________________________________ > NumPy-Discussion mailing list -- [email protected] > To unsubscribe send an email to [email protected] > https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ > Member address: [email protected]
_______________________________________________ NumPy-Discussion mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ Member address: [email protected]
