title: avoid successful broadcast of a scalar
body: <p>When <a href="https://numpy.org/doc/stable/reference/generated/numpy.subtract.html" rel="nofollow noreferrer">subtracting</a> or <a href="https://numpy.org/doc/stable/reference/generated/numpy.multiply.html" rel="nofollow noreferrer">multiplying</a> arrays, numpy <a href="https://numpy.org/doc/stable/user/basics.broadcasting.html" rel="nofollow noreferrer"><em>broadcasts</em></a> them to the same shape:</p>
  <pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; np.array(((2,3),(4,5))) - np.array((1,2,3,4))
  Traceback (most recent call last):
    File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  ValueError: operands could not be broadcast together with shapes (2,2) (4,) 
  &gt;&gt;&gt; np.array((2,3,4,5)) - np.array((1,2,3,4))
  array([1, 1, 1, 1])
  &gt;&gt;&gt; np.array((2,3,4,5)) - np.array(1)
  array([1, 2, 3, 4])
  &gt;&gt;&gt; np.array((2,3,4,5)) * np.array(2)
  array([ 4,  6,  8, 10])
  </code></pre>
  <p>while the last operation makes sense <a href="https://en.wikipedia.org/wiki/Linear_algebra" rel="nofollow noreferrer"><em>mathematically</em></a> (one <em>can</em> multiply a <a href="https://en.wikipedia.org/wiki/Vector_space" rel="nofollow noreferrer">vector</a> by a <a href="https://en.wikipedia.org/wiki/Scalar_(mathematics)" rel="nofollow noreferrer">scalar</a>), the penultimate one does <em>not</em> (one <em>cannot</em> add or subtract a scalar to a vector).</p>
  <p>I wonder if it is possible to tell numpy <em><strong>not</strong></em> to broadcast scalars to
  vectors or matrices in additive context, raising <code>ValueError: operands could not be broadcast together</code> instead.</p>
  <h3>Rationale: the difference between <em>multiplying</em> each element of an array by a scalar, and adding the <em>scalar</em> to each element of the array</h3>
  <p>A vector is an element of a <a href="https://en.wikipedia.org/wiki/Vector_space" rel="nofollow noreferrer">Vector
  space</a>, i.e., an <a href="https://en.wikipedia.org/wiki/Abelian_group" rel="nofollow noreferrer">Abelian
  group</a> whose elements can
  additionally be multiplied by a
  <a href="https://en.wikipedia.org/wiki/Scalar_(mathematics)" rel="nofollow noreferrer">scalar</a>, i.e., a number.
  IOW, <em>multiplying</em> a vector (or a matrix) by a number is a natural operation
  with well defined properties like <a href="https://en.wikipedia.org/wiki/Distributive_property" rel="nofollow noreferrer">distributivity</a> &amp;c.</p>
  <p>However, <em>adding</em> a <em>scalar</em> to a <em>vector</em> is not defined <em>at all</em>.</p>
  <p>Why?! Because you cannot define it <em>without specifying a basis</em>.</p>
  <p>What an <em>engineer</em> calls a <em>vector</em> (i.e., a sequence of numbers), a
  <em>mathematician</em> calls a coordinate representation of an (abstract) vector in a
  specific basis.
  IOW, when you <em>define</em></p>
  <pre><code>a + [b,c,d,...] := [a+b,a+c,a+d,...]
  </code></pre>
  <p>what I see is that you have selected an <em>arbitrary</em> vector (of huge norm!)
  <code>Y=[1,1,1,...]</code> and defined</p>
  <pre><code>a + X := a*Y + X
  </code></pre>
  <p>by why select this specific vector? how is it better than any other one?
  We all love this canonical basis, but in a different basis this vector may look
  different!</p>
  <p>The current behavior can lead to actual <em>errors</em>, e.g., if someone to add a random error and writes</p>
  <pre><code>my_vector + np.random.normal()     # WRONG! each component gets the SAME error!
  </code></pre>
  <p>instead of the correct one</p>
  <pre><code>my_vector + np.random.normal(size=len(my_vector))
  </code></pre>
  <p>Also, when dealing with <em>matrices</em>, which <em>are</em> a
  <a href="https://en.wikipedia.org/wiki/Ring_(mathematics)" rel="nofollow noreferrer">ring</a>,
  so they <em>do</em> have a canonical non-0 element - the
  <a href="https://en.wikipedia.org/wiki/Unit_(ring_theory)" rel="nofollow noreferrer">unit</a>,
  the numpy behavior is completely counter-intuitive!</p>
  <pre><code>10 + array([[1,2],[3,4]])
  </code></pre>
  <p>should be <code>array([[11,2],[3,14]])</code>, not <code>array([[11,12],[13,14]])</code>!
  (for the same reason: matrices are just representations of linear operators).</p>
  <p>Note that the unit matrix is the <em>same</em> in any basis.</p>
  <p>Please see <a href="https://math.stackexchange.com/q/1379234/37092">Can you add a scalar to a matrix?</a> for a similar explanation in 2d.</p>