I'm trying to add some points together and custom dtypes do not behave as ndarrays. The following is code I'm trying to use:
self.center = np.dtype([('x', np.uint16), ('y', np.uint16)])
nd0 = np.array((1, 2))
nd1 = np.array((3, 4))
print(nd0 + nd1)
c0 = np.array((1, 2), dtype=self.center)
c1 = np.array((3, 4), dtype=self.center)
print(c0 + c1)
The second print yields an error:
TypeError: ufunc 'add' did not contain a loop with signature matching types
dtype([('x', '<u2'), ('y', '<u2')])
dtype([('x', '<u2'), ('y', '<u2')])
dtype([('x', '<u2'), ('y', '<u2')])
Is standard practice to create a custom function to handle unique dtype? c0 and c1 are of type numpy.void, whereas nd0 and nd1 are numpy.ndarray. I attempted to cast with numpy.asarray but it still fails to add the two.
I wanted to average the centers of two objects. The center named field is nested in another named array and I thought it'd be easier to do something like:
new_center = (pop[i]['center'] + pop[j]['center']) / 2.0
Rather than:
new_center = (pop[i][0] + pop[j][0]) / 2.0
for readability and maintenance.