title: When broadcasting is a bad idea ? (numpy)
body: <p>The term <strong><a href="https://docs.scipy.org/doc/numpy-1.13.0/user/basics.broadcasting.html" rel="noreferrer">broadcasting</a></strong> describes how numpy treats arrays with different shapes during arithmetic operations.</p>
  <pre><code>Example 1:
  
  from numpy import array 
  a = array([1.0,2.0,3.0])
  b = array([2.0,2.0,2.0]) # multiply element-by-element ()
  a * b
  &gt;&gt; array([ 2.,  4.,  6.])
  
  Example 2 :
  
  from numpy import array
  a = array([1.0,2.0,3.0])
  b = 2.0  # broadcast b to all a
  a * b
  &gt;&gt;array([ 2.,  4.,  6.])
  </code></pre>
  <p>We can think of the scalar b being stretched during the arithmetic operation into an array with the same shape as a. Numpy is smart enough to use the original scalar value without actually making copies so that broadcasting operations are as memory and computationally efficient as possible (b is a scalar, not an array)</p>
  <p>A small <a href="https://stackoverflow.com/a/47308275/5671807">benchmarking</a> made by <em>@Eric Duminil</em> in <a href="https://stackoverflow.com/questions/47307518/which-operator-vs-should-be-used-for-performance-in-place-vs-not-in-pla">another memory performance question</a>, shows that broadcasting makes difference in term of speed and memory</p>
  <p>However, I am quoting from the same article linked above:</p>
  <blockquote>
  <p>There are, cases where broadcasting is a bad idea because it
  leads to inefficient use of memory that slows computation</p>
  </blockquote>
  <p>The question is: When broadcasting uses unnecessarily large amounts of memory and result sluggish performance ?
  In other terms when we should use <em>hybrid broadcasting</em>/python looping algorithm over the pure broadcasting approch?</p>