suppose I have a few lists
b_wi = [[1,2],[1.5,2.5,3]] #b_wi is a subset of x
f_wi = [[5,4,2],[1,1.8,3,9]]
the following two are step functions formed by the above lists.
'''
F1 = f_wi[0][0] if x< b_wi[0][0] ;
f_wi[0][1] if b_wi[0][0] <=x< b_wi[0][1];
...;
f_wi[0][-1] if x>= b_wi[1][-1]
F2 = f_wi[1][0] if x< b_wi[1][0] ;
f_wi[1][1] if b_wi[1][0] <=x< b_wi[1][1];
...;
f_wi[1][-1] if x>= b_wi[1][-1]
'''
Now I want to get a new function (F1+F2) and the corresponding interval.
I did some searching and found this :
Evaluate sum of step functions
Following the solution in the link, I am able to generate the new piecewise function F1+F2
However, since the length of intervals is not the same for these step functions, I cannot apply the solution in the link directly.
Instead, I did this:
import numpy as np
from pandas.core.common import flatten
def func(x,b,f):
return f[np.searchsorted(b,x,side='right')]
x= np.unique(list(flatten(b_wi)))
a=np.zeros((len(x)))
for b, f in zip(b_wi,f_wi):
a=a+ func(x,b,np.asarray(f))
Now I get can get the the new function (F1+F2)
This is just a simple example I used to illustrate my question. My actual lists are longer than these and there are 100000 piecewise step functions. Since I 'flatten' 'b_wi' in order to find the corresponding interval, the length of 'intval' becomes too large. Hence, my method is too slow. Does anyone know how I could speed it up? I feel like I am using the wrong method. Thank you very much.