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>It sounds like things could get quite complicated if you have lots of functions working on the array. I would consider breaking each row into a class to manage the function calls a bit more succinctly. For example you could contain all of the relevant functions within the class:</p>

<pre class="lang-python prettyprint-override"><code>class Row:
    def __init__(self, row):
        self.row = row

        self.sum1 = None
        self.sub1 = None

        self._add(row)
        self._sub(row)

    def _add(self, items):
        self.sum1 = sum([items[0][1], items[0][2], items[-1][1], items[-1][2]])

    def _sub(self, items):
        self.sub1 = items[0][2] - items[-1][2]

    def update(self):
        self.row = [[self.row[0][0], self.sum1, self.sub1], self.row[1]]

# Data
arr = ([[1, 0, 50], [2, 0, 50], [1, 3.0, 1.0]],
 [[1, 0, 50], [2, 0, 50], [2, 3.0, 1.0]])

# Usage
for row in arr:
    r = Row(row)
    print r.sum1, r.sub1

    r.update()
    print r.row



&gt;&gt;&gt; 54.0 49.0
    [[1, 54.0, 49.0], [2, 0, 50]]
    54.0 49.0
    [[1, 54.0, 49.0], [2, 0, 50]]  # This row doesnt match your example, but you get the idea
</code></pre>

python arrays numpy