Make the matrix multiplication operator @ work for scalars in numpy
<p>In python 3.5, the <code>@</code> operator was introduced for matrix multiplication, following <a href="https://www.python.org/dev/peps/pep-0465/" rel="noreferrer">PEP465</a>. This is implemented e.g. in numpy as the <a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.matmul.html" rel="noreferrer">matmul operator</a>.</p>

<p>However, as proposed by the PEP, the numpy operator throws an exception when called with a scalar operand:</p>

<pre><code>&gt;&gt;&gt; import numpy as np
&gt;&gt;&gt; np.array([[1,2],[3,4]]) @ np.array([[1,2],[3,4]])    # works
array([[ 7, 10],
       [15, 22]])
&gt;&gt;&gt; 1 @ 2                                                # doesn't work
Traceback (most recent call last):
  File "&lt;input&gt;", line 1, in &lt;module&gt;
TypeError: unsupported operand type(s) for @: 'int' and 'int'
</code></pre>

<p>This is a real turnoff for me, since I'm implementing numerical signal processing algorithms that should work for both scalars and matrices. The equations for both cases are mathematically exactly equivalent, which is no surprise, since "1-D x 1-D matrix multiplication" is equivalent to scalar multiplication. The current state however forces me to write duplicate code in order to handle both cases correctly.</p>

<p>So, given that the current state is not satisfactory, is there any reasonable way I can make the <code>@</code> operator work for scalars? I thought about adding a custom <code>__matmul__(self, other)</code> method to scalar data types, but this seems like a lot of hassle considering the number of involved internal data types. Could I change the implementation of the <code>__matmul__</code> method for numpy array data types to not throw an exception for 1x1 array operands?</p>

<p>And, on a sidenote, which is the rationale behind this design decision? Off the top of my head, I cannot think of any compelling reasons not to implement that operator for scalars as well.</p>