title: sum of products for multiple lists in python
body: <p>Trying to imitate Excel's SUMPRODUCT function:</p>
  
  <pre><code>SUMPRODUCT(v1, v2, ..., vN) =
      v1[0]*v2[0]*...*vN[0] + v1[1]*v2[1]*...*vN[1] + ... + v1[n]*v2[n]*...*vN[n]
  </code></pre>
  
  <p>where n is the number of elements in each vector.</p>
  
  <p>This is similar to dot product, but for multiple vectors. I read <a href="https://stackoverflow.com/questions/1828233/optimized-dot-product-in-python">the very detailed discussion</a> of the regular dot product, but I don't know how to cleanly extend it to multiple vectors. For reference, I'm copying the optimized code proposed there, which I ported (trivially) to Python 3. BTW, for dot product, the last approach still wins in P3K.</p>
  
  <pre><code>def d0(v1,v2):
      """                                                                                                     
      d0 is Nominal approach:                                                                                 
      multiply/add in a loop                                                                                  
      """
      out = 0
      for k in range(len(v1)):
          out += v1[k] * v2[k]
      return out
  
  def d1(v1,v2):
      """                                                                                                     
      d1 uses a map                                                                        
      """
      return sum(map(mul,v1,v2))
  
  def d3(v1,v2):
      """                                                                                                     
      d3 uses a starmap (itertools) to apply the mul operator on an zipped (v1,v2)                           
      """
      return sum(starmap(mul,zip(v1,v2)))
  </code></pre>