3

For example I have two functions expressed with two lists:

x_1 = [0, 1, 2, 3, 4, 5]
y_1 = [2, 4, 1, 5, 1, 2]

x_2 = [2, 3, 4, 5, 6, 7]
y_2 = [2, 4, 1, 5, 1, 2]

and the result should be

x = [0, 1, 2, 3, 4, 5, 6, 7]
y = [2, 4, 3, 9, 2, 7, 1, 2]

Here I set the x values in the integer lattice but it is not necessary. But I guess one solution could be normalizing them onto the lattice and then add them.

Is there any simple method to do this? Numpy and Scipy are both available.

Thanks!


A simple illustration enter image description here

9
  • 2
    Can you explain how you got the o/p? Commented Feb 15, 2015 at 16:07
  • @BhargavRao Sorry but what is o/p? Commented Feb 15, 2015 at 17:03
  • @BhargavRao Thanks. The y values corresponding to the same x sums up. Commented Feb 15, 2015 at 17:07
  • So you want to merge 2 (pairs) of lists, based on the values of one. What assumptions are you making about the x values? Do they overlap, are they contiguous? Commented Feb 15, 2015 at 17:11
  • @hpaulj There's no restriction on x. You can imagine two functions, both described by x,y values and every function is connected by a straight line between two defined x values next to each other. Commented Feb 15, 2015 at 17:16

2 Answers 2

1

numpy has a 1d interpolation function, and scipy has a more general one(s).

A simple approach with np.interp:

x1,y1=[0,1,2,3,4,5],[2,4,1,5,1,2]
x2,y2=[2,3,4,5,6,7],[2,4,1,5,1,2]
x3 = np.arange(x1[0],x2[-1]+1)  # or latice of your choice
np.interp(x3,x1,y1,0,0) + np.interp(x3,x2,y2,0,0)

producing:

array([ 2.,  4.,  3.,  9.,  2.,  7.,  1.,  2.])

I told interp to return 0 for values outside the x1 range, which seems to fit your addition scheme well.

A couple of other ways of constructing x3:

Join the 2 lists, and ask for the unique values (sorted):

x3=np.unique(x1+x2)

or if the x might already be arrays, concatenate them first:

x3=np.unique(np.concatenate([x1,x2]))
Sign up to request clarification or add additional context in comments.

1 Comment

I think that's what I want! Thank you!
1

Naive implementation:

x_1 = [0, 1, 2, 3, 4, 5]
y_1 = [2, 4, 1, 5, 1, 2]

x_2 = [2, 3, 4, 5, 6, 7]
y_2 = [2, 4, 1, 5, 1, 2]

f1 = dict(zip(x_1, y_1))
f2 = dict(zip(x_2, y_2))

x = list(set(f1.keys()) | set(f2.keys()))
y = [f1.get(k, 0) + f2.get(k, 0) for k in x]

print x
print y

Result:

[0, 1, 2, 3, 4, 5, 6, 7]
[2, 4, 3, 9, 2, 7, 1, 2]

2 Comments

set(f1.keys() + f2.keys()) does not work in Python 3.x because dict.keys returns a view object, not a list object in Python 3.x. Use set(list(f1) + list(f2)) or set(f1) | set(f2), ...
Thank you a lot! How about the situation where the x values are not on the lattice? Besides, since I have to deal with huge functions, maybe numpy will be better in performance?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.