I assumed that @ was shorthand for the dot method. What motivated the design decision to block multiplication by an array with shape ()?
In [6]: a = np.ones((2,1))
In [7]: a.dot(1)
Out[7]:
array([[ 1.],
[ 1.]])
In [8]: a @ 1
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-8-398cff4c0ec9> in <module>()
----> 1 a @ 1
ValueError: Scalar operands are not allowed, use '*' instead
@ is the infix operator for matmul, not dot (note that the two functions are not equivalent for higher-dimensional arrays (higher than 2D)).
No explicit reason for rejecting scalars as operands is stated in the documentation, but it seems probable that the motivation stems from PEP 0465 which originally proposed the introduction of @ as an infix operator for Python 3.5. From the 'semantics' section:
0d (scalar) inputs raise an error. Scalar
*matrix multiplication is a mathematically and algorithmically distinct operation from matrix@matrix multiplication, and is already covered by the elementwise*operator. Allowing scalar@matrix would thus both require an unnecessary special case, and violate TOOWTDI ["There's Only One Way To Do It"].
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With