[Numpy-discussion] matmul as a ufunc

Ilhan Polat ilhanpolat at gmail.com
Tue May 29 08:14:56 EDT 2018


Apart from the math-validity discussion, in my experience errors are used a
bit too generously in the not-allowed ops. No ops are fine once you learn
more about them such as transpose on 1D arrays (good or bad is another
discussion). But raising errors bloat the computational code too much. "Is
it a scalar oh then do this is it 1D oh make this one is it 2D then do
something else." type of coding is really making life difficult.

Most of my time in the numerical code is spent on trying to catch scalars
and 1D arrays and writing exceptions because I can't predict what the user
would do or what the result should be after certain operations. Quite
unwillingly, I've started making everything 2D whether it is required or
not because then I can just avoid the following

np.eye(4)[:, 1]           # 1d
np.eye(4)[:, 1:2]         #2d
np.eye(4)[:, [1]]         #2d
np.eye(4)[:, [1]] @ 5                      # Error
np.eye(4)[:, [1]] @ np.array(5)        #Error
np.eye(4)[:, [1]] @ np.array([5])      # Result is 1D
np.eye(4)[:, [1]] @ np.array([[5]])    # Result 2D

So imagine I'm trying to get a simple multiply_these function, I have
already quite some cases to consider such that the function is "Pythonic".
If the second argument is int,float do *-mult, if it is a numpy array but
has no dimensions then again *-mult but if it is 1d keep dims and also if
it is 2d do @-mult. Add broadcasting rules on top of this and it gets a
pretty wordy function.Hence, what I would suggest is to also include the
use cases while deciding the behavior of a single functionality.

So indeed it doesn't make sense to transpose 0d array but as an array
object now it would start to have a lot of Wat! moments.
https://www.destroyallsoftware.com/talks/wat



On Tue, May 29, 2018 at 12:51 PM, Andras Deak <deak.andris at gmail.com> wrote:

> On Tue, May 29, 2018 at 12:16 PM, Daπid <davidmenhur at gmail.com> wrote:
> > Right now, np.int(8).T throws an error, but np.transpose(np.int(8))
> gives a
> > 0-d array. On one hand, it is nice to be able to use the same code for
>
> `np.int` is just python `int`! What you mean is `np.int64(8).T` which
> works fine, so does `np.array(8).T`.
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20180529/a77bde26/attachment.html>


More information about the NumPy-Discussion mailing list