<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    Den 12.09.2011 08:52, skrev David Froger:
    <blockquote cite="mid:1315810245-sup-1371@david-desktop" type="cite">
      <pre wrap="">Hy everybody,

I'm wondering what is the (best) way to apply the same function to multiple
arrays.

</pre>
    </blockquote>
    <br>
    I tried to experiment a bit with this. Here is from an ipython
    session:<br>
    <br>
    Create some arrays:<br>
    <br>
    <span style=" color:#000080;">In [</span><span style="
      font-weight:600; color:#000080;">1</span><span style="
      color:#000080;">]:</span> import numpy as np
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px;
      margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
        style=" color:#000080;">In [</span><span style="
        font-weight:600; color:#000080;">2</span><span style="
        color:#000080;">]:</span> a = np.zeros(4)</p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px;
      margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
        style=" color:#000080;">In [</span><span style="
        font-weight:600; color:#000080;">3</span><span style="
        color:#000080;">]:</span> b = a+1</p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px;
      margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
        style=" color:#000080;">In [</span><span style="
        font-weight:600; color:#000080;">4</span><span style="
        color:#000080;">]:</span> c = a+2</p>
    <p style="margin: 0px; text-indent: 0px;"><span style="
        color:#000080;">In [</span><span style=" font-weight:600;
        color:#000080;">5</span><span style=" color:#000080;">]:</span>
      d = a+3<br>
    </p>
    <p style="margin: 0px; text-indent: 0px;"><br>
    </p>
    <p style="margin: 0px; text-indent: 0px;"><br>
      <br>
    </p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px;
      margin-right:0px; -qt-block-indent:0; text-indent:0px;">Create an
      array with dtype=object to store the four arrays a-d:<br>
      <br>
    </p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px;
      margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
        style=" color:#000080;">In [</span><span style="
        font-weight:600; color:#000080;">6</span><span style="
        color:#000080;">]:</span> e = np.zeros(4, dtype=object)</p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px;
      margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
        style=" color:#000080;">In [</span><span style="
        font-weight:600; color:#000080;">7</span><span style="
        color:#000080;">]:</span> e[:] = a,b,c,d</p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px;
      margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
        style=" color:#000080;">In [</span><span style="
        font-weight:600; color:#000080;">8</span><span style="
        color:#000080;">]:</span> e</p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px;
      margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
        style=" color:#8b0000;">Out[</span><span style="
        font-weight:600; color:#8b0000;">8</span><span style="
        color:#8b0000;">]:</span> </p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px;
      margin-right:0px; -qt-block-indent:0; text-indent:0px;">array([[
      0. 0. 0. 0.], [ 1. 1. 1. 1.], [ 2. 2. 2. 2.],</p>
    <p style="margin: 0px; text-indent: 0px;"> [ 3. 3. 3. 3.]],
      dtype=object)<br>
    </p>
    <p style="margin: 0px; text-indent: 0px;"><br>
    </p>
    <p style="margin: 0px; text-indent: 0px;"><br>
      <br>
    </p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px;
      margin-right:0px; -qt-block-indent:0; text-indent:0px;">Modify
      array e inplace:<br>
      <br>
    </p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px;
      margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
        style=" color:#000080;">In [</span><span style="
        font-weight:600; color:#000080;">9</span><span style="
        color:#000080;">]:</span> e += 1</p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px;
      margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
        style=" color:#000080;">In [</span><span style="
        font-weight:600; color:#000080;">10</span><span style="
        color:#000080;">]:</span> e</p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px;
      margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
        style=" color:#8b0000;">Out[</span><span style="
        font-weight:600; color:#8b0000;">10</span><span style="
        color:#8b0000;">]:</span> </p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px;
      margin-right:0px; -qt-block-indent:0; text-indent:0px;">array([[
      1. 1. 1. 1.], [ 2. 2. 2. 2.], [ 3. 3. 3. 3.],</p>
    <p style="margin: 0px; text-indent: 0px;"> [ 4. 4. 4. 4.]],
      dtype=object)<br>
    </p>
    <p style="margin: 0px; text-indent: 0px;"><br>
    </p>
    <p style="margin: 0px; text-indent: 0px;"><br>
    </p>
    <p style="margin: 0px; text-indent: 0px;"><br>
      This did not modify arrays a-d though:<br>
    </p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px;
      margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br>
    </p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px;
      margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
        style=" color:#000080;">In [</span><span style="
        font-weight:600; color:#000080;">11</span><span style="
        color:#000080;">]:</span> a</p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px;
      margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
        style=" color:#8b0000;">Out[</span><span style="
        font-weight:600; color:#8b0000;">11</span><span style="
        color:#8b0000;">]:</span> array([ 0., 0., 0., 0.])</p>
    <br>
    <br>
    <br>
    Albeit e[0] was array a prior to the iadd:<br>
    <br>
    <span style=" color:#000080;">In [</span><span style="
      font-weight:600; color:#000080;">12</span><span style="
      color:#000080;">]:</span> e[:] = a,b,c,d
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px;
      margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
        style=" color:#000080;">In [</span><span style="
        font-weight:600; color:#000080;">13</span><span style="
        color:#000080;">]:</span> e[0] is a</p>
    <p style="margin: 0px; text-indent: 0px;"><span style="
        color:#8b0000;">Out[</span><span style=" font-weight:600;
        color:#8b0000;">13</span><span style=" color:#8b0000;">]:</span>
      True<br>
    </p>
    <p style="margin: 0px; text-indent: 0px;"><br>
      <span style="color: rgb(0, 0, 128);"></span></p>
    <p style="margin: 0px; text-indent: 0px;"><span style="color: rgb(0,
        0, 128);"><font color="#000000"><br>
          We can apply a function to the arrays in e, getting
          array([f(a), f(b), f(c), f(d)]):</font><br>
      </span></p>
    <p style="margin: 0px; text-indent: 0px;"><span style="color: rgb(0,
        0, 128);"><br>
      </span></p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px;
      margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
        style=" color:#000080;">In [</span><span style="
        font-weight:600; color:#000080;">14</span><span style="
        color:#000080;">]:</span> np.sum(e)
    </p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px;
      margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
        style=" color:#8b0000;">Out[</span><span style="
        font-weight:600; color:#8b0000;">14</span><span style="
        color:#8b0000;">]:</span> array([ 6., 6., 6., 6.])</p>
    <br>
    <br>
    <br>
    Observe that if e was a 2D array, np.sum(e) would have returned a
    scalar, like so:<br>
    <br>
    <span style=" color:#000080;">In [</span><span style="
      font-weight:600; color:#000080;">18</span><span style="
      color:#000080;">]:</span> g = np.array((a,b,c,d))
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px;
      margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
        style=" color:#000080;">In [</span><span style="
        font-weight:600; color:#000080;">19</span><span style="
        color:#000080;">]:</span> g</p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px;
      margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
        style=" color:#8b0000;">Out[</span><span style="
        font-weight:600; color:#8b0000;">19</span><span style="
        color:#8b0000;">]:</span> </p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px;
      margin-right:0px; -qt-block-indent:0; text-indent:0px;">array([[
      0., 0., 0., 0.],</p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px;
      margin-right:0px; -qt-block-indent:0; text-indent:0px;"> [ 1., 1.,
      1., 1.],</p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px;
      margin-right:0px; -qt-block-indent:0; text-indent:0px;"> [ 2., 2.,
      2., 2.],</p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px;
      margin-right:0px; -qt-block-indent:0; text-indent:0px;"> [ 3., 3.,
      3., 3.]])</p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px;
      margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
        style=" color:#000080;">In [</span><span style="
        font-weight:600; color:#000080;">20</span><span style="
        color:#000080;">]:</span> np.sum(g)</p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px;
      margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
        style=" color:#8b0000;">Out[</span><span style="
        font-weight:600; color:#8b0000;">20</span><span style="
        color:#8b0000;">]:</span> 24.0</p>
    <p style="margin: 0px; text-indent: 0px;"><br>
    </p>
    <p style="margin: 0px; text-indent: 0px;"><br>
      Which means:<br>
    </p>
    <p style="margin: 0px; text-indent: 0px;"><br>
      We can create an array of arrays, and have numpy broadcast an
      ufunc to multiple arrays.<br>
    </p>
    <p style="-qt-paragraph-type:empty; margin-top:0px;
      margin-bottom:0px; margin-left:0px; margin-right:0px;
      -qt-block-indent:0; text-indent:0px;"><br>
    </p>
    The other obvious way is, as mentioned by others, to keep the arrays
    in a normal<br>
    Python container (e.g. list) and use a for loop or functional
    programming (map, <br>
    apply, reduce).<br>
    <br>
    <br>
    Sturla<br>
    <br>
    <br>
    <br>
  </body>
</html>