<html xmlns="http://www.w3.org/1999/xhtml"><head> <title></title> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> </head> <body style="font-family:Helvetica;color:#000000;font-size:13px;"><blockquote id="CanaryBlockquote"><div><div>On Thursday, Apr 25, 2019 at 9:45 PM, Marten van Kerkwijk <<a href="mailto:m.h.vankerkwijk@gmail.com">m.h.vankerkwijk@gmail.com</a>> wrote:<br></div> <div><div dir="ltr"><div>It seems we are adding to the wishlist!  I see four so far:</div><div>1. Exposed in API, can be overridden with __array_ufunc__</div><div>2. One that converts everything to ndarray (or subclass); essentially the current implementation;</div><div>3. One that does asduckarray</div><div>4. One that assumes all arguments are arrays.</div><div><br></div><div>Maybe handiest would be if there is a method to coerce all relevant arguments with a function of one's choice? I.e., in the example of Stephan, one would have</div><div>```<br></div><div>if function in JUST_COERCE:</div><div>    coerced_args, coerced_kwargs = function.__coerce__(np.asanyarray, *args, **kwargs)</div><div>    return function.__implementation__(*coerced_args, **coerced_kwargs)<br></div><div>```</div><div>Actually, this might in fact work with the plan proposed here, if we allow for an extra, optional kwarg that contains the coercion function, that is</div><div>```</div><div>    return function.__implementation__(*args, coercion_function=np.asanyarray, **kwargs)</div><div>```</div><div></div><div><br></div><div>The possible advantage of this over yet more dunder methods is that one can fine-tune the extent to which something has to mimic an array properly (e.g., run `asanyarray` only if `shape` is not present).</div><div><br></div><div>It would be nice, though, if we could end up with also option 4 being available, if only because code that just can assume ndarray will be easiest to read.</div><div><br></div><div>All the best,</div><div><br></div><div>Marten<br></div></div> _______________________________________________ <br>NumPy-Discussion mailing list <br>NumPy-Discussion@python.org <br>https://mail.python.org/mailman/listinfo/numpy-discussion <br></div></div></blockquote><br> <div><div id="CanaryBody"><div>Hi everyone,</div><div><br></div><div>Although, in general, I agree with Stephan’s design goals, I agree with Marten that the number of protocols are getting larger and may get out of hand if not handled properly. There’s even one Marten forgot to mention: __array_dtype__. I have been working on a project that I consider to have all the essential features that Marten proposes, mostly within one framework. It’s called uarray (for universal array) and can be found over at </div><div><br></div><div><ul><li>Source: <a href="https://github.com/Quansight-Labs/uarray">https://github.com/Quansight-Labs/uarray</a><br></li><li>Documentation: <a href="https://uarray.readthedocs.io/en/latest/">https://uarray.readthedocs.io/en/latest/</a></li></ul><div>It adopts the “separation of implementation from interface” principles from the beginning. Here’s how it works: There are MultiMethods and Backends. A Backend registers implementations for a given MultiMethod. A MultiMethod defines the signature, along with the elements that can be dispatched over, along with their types. To it, NumPy is (and I realise this is going to be controversial, since this is the NumPy mailing list), just another backend.</div></div><div><br></div><div>Here’s how it addresses Marten’s concerns:</div><div><ul><li>Everything is made into a MultiMethod. Then, the multimethod marks objects it’d like to dispatch over. For the status quo, this is arrays. But thinking long-term, we could dispatch over abstract ufuncs and dtypes as well. For ufuncs, ufunc.__call__ and ufunc.reduce are also MultiMethods.</li><li>Coercion works by extracting marked dispatchables, converting them into native library equivalents and then passing them back into the function. For example, it would convert lists (or anything marked as an array) to arrays. What it could also do is convert dtype=‘int64’ to an actual dtype, and so on.</li><li>__asduckarray__ is rendered unnecessary… Coercion handles that.</li></ul><div>You can check out the usage examples in the tests:</div><div><br></div><div><ul><li>Core backend infrastructure: <a href="https://github.com/Quansight-Labs/uarray/blob/master/uarray/tests/test_backend.py">https://github.com/Quansight-Labs/uarray/blob/master/uarray/tests/test_backend.py</a><br></li><li>Backend infrastructure: <a href="https://github.com/Quansight-Labs/uarray/blob/master/unumpy/tests/test_numpy.py">https://github.com/Quansight-Labs/uarray/blob/master/unumpy/tests/test_numpy.py</a></li></ul><div><br></div><div>Examples of how to write NumPy MultiMethods are here: <a href="https://github.com/Quansight-Labs/uarray/blob/master/unumpy/multimethods.py">https://github.com/Quansight-Labs/uarray/blob/master/unumpy/multimethods.py</a><a href="https://github.com/Quansight-Labs/uarray/blob/master/unumpy/multimethods.pym"></a>, along with the accompanying Backends in <a href="https://github.com/Quansight-Labs/uarray/tree/master/unumpy">https://github.com/Quansight-Labs/uarray/tree/master/unumpy</a>.</div></div></div><div><br></div></div><div id="CanarySig"><div>Best Regards,<div>Hameer Abbasi</div></div></div></div><img id="31C0EF51A5708BCD4ECDC5E23DBE0359" width="1px" src="http://pixels.canarymail.io:8100/track/977C99C0645380AF45E168B4CDA443F5_31C0EF51A5708BCD4ECDC5E23DBE0359.png" height="1px"></body></html>