How to substitute two numpy subarrays with a single one
<p>is there a simple solution to substitute two numpy subarrays with a single one while the entires are a results of functions being called on the entries of the two former arrays. E.g:</p>

<pre><code>[1, a, b][1, c, d] -&gt; [1, f_add, f_sub] 
</code></pre>

<p>with </p>

<pre><code>f_add(a, b, c, d):
    return a + b + c + d

f_sub(b, d):
    return b-d
</code></pre>

<p>Concrete: </p>

<pre><code> [1, 0, 50], [1, 3, 1] -&gt; [1, 54, 49]
</code></pre>

<p>As an addition, the row <code>[1, 0, 50], [1, 3, 1]</code> is part of a bigger array (1st row in example), so it should be substituted in-place</p>

<pre><code>([[1, 0, 50], [2, 0, 50], [1, 3.0, 1.0]],
 [[1, 0, 50], [2, 0, 50], [2, 3.0, 1.0]])


([[1, 54, 49], [2, 0, 50]],
 [[1, 0, 50], [2, 0, 50], [2, 3.0, 1.0]])
</code></pre>

<p>Thanks!</p>

<p>EDIT:</p>

<p>The functions f_add and f_sub are just examples to illustrate what I want to do and that the change in entries is a result of functions being called.
In reality I use slightly more complex functions that carry out a (more) meaningful computation.</p>

<p>The second thing is that this substitution should only be carried out on elements where the first entry is the same. 
So in the first row only <code>[1, 0. 50.]</code> and <code>[1, 3.0, 1.0]</code> merge while in the second it would be <code>[2, 0., 50.]</code> and <code>[2, 30, 1.0)</code>.</p>

<p>In this example I first wanted to separate the issue of determining which sub-arrays are intended to merge by comparing the indicies but I guess it should be included to be as general as possible.</p>

<p>A more complete example result to the one above would be as following then:</p>

<pre><code> ([[1, 0., 50.], [2, 0., 50], [1, 3.0, 1.0]],
 [[1, 0., 50.], [2, 0., 50.], [2, 3.0, 1.0]])
</code></pre>

<p>leading to:</p>

<pre><code>([[1, 54., 49.], [2, 0., 50.]],
 [[1, 0., 50.], [2, 54., 49.]])
</code></pre>

<p>You can use a generator expression to get this result (assuming there are three subelements for each element of the array):</p>

<pre><code>ar = ([[1, 0, 50], [2, 0, 50], [1, 3.0, 1.0]],
      [[1, 0, 50], [2, 0, 50], [2, 3.0, 1.0]])
ar = tuple([[[x[0][0], sum(x[0][1:]) + sum(x[-1][1:]), x[0][-1]-x[-1][-1]], x[1]] for x in ar])
print ar
</code></pre>

<blockquote>
  <p>([[1, 54.0, 49.0], [2, 0, 50]], [[1, 54.0, 49.0], [2, 0, 50]])</p>
</blockquote>

<hr>

<p>EDIT: Perhaps for a more general solution you can define a function <code>f(x)</code> that performs the desired calculation to elements of an array, and map that function to every row of the array. For instance,</p>

<pre><code>def f(x):
    if (x[0][0] == x[1][0]):
        return [[x[0][0], x[0][1]+x[0][2]+x[1][1]+x[1][2], x[0][2]-x[1][2]], x[2]]
    elif (x[0][0] == x[2][0]):
        return [[x[0][0], x[0][1]+x[0][2]+x[2][1]+x[2][2], x[0][2]-x[2][2]], x[1]]
    elif (x[1][0] == x[2][0]):
        return [x[0], [x[1][0], x[1][1]+x[1][2]+x[2][1]+x[2][2], x[1][2]-x[2][2]]]
    else:
        return x

ar = ([[1, 0, 50], [2, 0, 50], [1, 3.0, 1.0]],
      [[1, 0, 50], [2, 0, 50], [2, 3.0, 1.0]])

print tuple(map(f, ar))
</code></pre>

<blockquote>
  <p>([[1, 54.0, 49.0], [2, 0, 50]], [[1, 0, 50], [2, 54.0, 49.0]])</p>
</blockquote>

python arrays numpy