def __add__(self, other):
assert isinstance(other, ShapeFunction), "Can only add other shape function"
assert self.name == other.name, "Cannot add shapes of different features"
new_splits = self.splits.copy()
new_vals = self.values.copy()
for split, val in zip(other.splits, other.values):
idx = np.searchsorted(new_splits, split, side='right')
new_val = val
if split in new_splits:
idx_2 = np.argwhere(new_splits == split)
new_vals[idx_2] = new_vals[idx_2] + new_val
elif idx == len(new_splits) and (~np.isposinf(split)):
new_splits = np.append(new_splits, split)
new_vals = np.append(new_vals, new_val)
elif np.isposinf(split):
new_vals[-1] = new_vals[-1] + new_val
else:
new_splits = np.insert(new_splits, idx, split)
new_vals = np.insert(new_vals, idx, new_val)
return ShapeFunction(new_splits, new_vals, self.name)
评论列表
文章目录