Element-wise minimum of multiple vectors in numpy
<p>I know that in numpy I can compute the element-wise minimum of two vectors with</p>

<pre><code>numpy.minimum(v1, v2)
</code></pre>

<p>What if I have a list of vectors of equal dimension, <code>V = [v1, v2, v3, v4]</code> (but a list, not an array)? Taking <code>numpy.minimum(*V)</code> doesn't work. What's the preferred thing to do instead?</p>

<p><code>*V</code> works if <code>V</code> has only 2 arrays.  <code>np.minimum</code> is a <code>ufunc</code> and takes 2 arguments.</p>

<p>As a <code>ufunc</code> it has a <code>.reduce</code> method, so it can apply repeated to a list inputs.</p>

<pre><code>In [321]: np.minimum.reduce([np.arange(3), np.arange(2,-1,-1), np.ones((3,))])
Out[321]: array([ 0.,  1.,  0.])
</code></pre>

<p>I suspect the <code>np.min</code> approach is faster, but that could depend on the array and list size.</p>

<pre><code>In [323]: np.array([np.arange(3), np.arange(2,-1,-1), np.ones((3,))]).min(axis=0)
Out[323]: array([ 0.,  1.,  0.])
</code></pre>

<p>The <code>ufunc</code> also has an <code>accumulate</code> which can show us the results of each stage of the reduction.  Here's it's not to interesting, but I could tweak the inputs to change that.</p>

<pre><code>In [325]: np.minimum.accumulate([np.arange(3), np.arange(2,-1,-1), np.ones((3,))])
     ...: 
Out[325]: 
array([[ 0.,  1.,  2.],
       [ 0.,  1.,  0.],
       [ 0.,  1.,  0.]])
</code></pre>

python numpy