1

May I know how should I add two functions(Multivariable).
For example,

T=lambda t,y: [0,t] 
P=lambda t,y: [y,y]

what is the proper way to get T-P because when I tried, it gives an error message
"unsupported operand type(s) for -: 'function' and 'function'".

At first I mulitvariable case will also follow the same case as it is for single variable. (here) but it doesn't work.

And I apologize for putting my question as a single variable function earlier

8
  • 2
    what exactly you are trying to achieve? Commented Apr 21, 2020 at 16:55
  • 1
    Does this answer your question? How to combine two lambda functions into one? Commented Apr 21, 2020 at 17:07
  • Thank you @AvishkaKavinduB.Dambawinna Commented Apr 21, 2020 at 17:12
  • I don't know what you trying to archive, but you can do this also f = lambda x, y: x**2+y**3 and pass two parameters to x and y , f(2,2) Commented Apr 21, 2020 at 17:23
  • Thank you for the comment @AvishkaDambawinna .. What I really need to do is: I have two multivariable functions T=lambda t,y: [0,t] and P=lambda t,y: [y,y] And I need to have a third function that gets the addition of T and P. But seems like it dosen't work Commented Apr 21, 2020 at 17:32

3 Answers 3

2

Try this:

f = lambda x: f1(x) + f2(x)

If you want to add functions with arbitrary arguments you can define a solution like this:

def add_functions(f1, f2):
    def f(*args, **kwargs):
        return f1(*args, **kwargs) + f2(*args, **kwargs)
    return f
Sign up to request clarification or add additional context in comments.

4 Comments

I see. Thank you very much. Your first suggestion would do my job. Anyway can you tell me what does *args and **kwargs means
You are welcome. You can read about it here: stackoverflow.com/questions/36901/…
What I really needed to do is: I have two multivariable functions T=lambda t,y: [0,t] and P=lambda t,y: [y,y] And I want to have a third function that gets the addition of T and P. But seems like it dosen't work
Can you, please, update your question give the certain result you expect from the addition of the functions?
0
f = lambda x, y: x + y
# f(2, 3) = 2 + 3 = 5 
g = lambda x, y: x * y
# g(2, 3) = 2 * 3 = 6 
h = lambda x, y: f(x, y) + g(x, y)
# h(2, 3) = f(2, 3) + g(2, 3) = (2 + 3) + (2 * 3) = 5 + 6 = 11 

1 Comment

Welcome to Stack Overflow. Please edit your answer and explain how it answers the specific question being asked. Stack Overflow is about learning, not providing snippets to blindly copy and paste. See How to Answer. stackoverflow.com/help/how-to-answer
-1

If I understand your problem, I guess the only way would be a third function.
Like this:

f3 = lambda x,y: f1(x) + f2(y)

Comments

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.