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

Charith
  • 415
  • 3
  • 10

3 Answers3

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
Fomalhaut
  • 8,590
  • 8
  • 51
  • 95
  • I see. Thank you very much. Your first suggestion would do my job. Anyway can you tell me what does *args and **kwargs means – Charith Apr 21 '20 at 17:08
  • 1
    You are welcome. You can read about it here: https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters – Fomalhaut Apr 21 '20 at 17:11
  • 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 – Charith Apr 21 '20 at 17:40
  • Can you, please, update your question give the certain result you expect from the addition of the functions? – Fomalhaut Apr 21 '20 at 17:43
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 
J Doe
  • 1
  • 4
    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. https://stackoverflow.com/help/how-to-answer – cards Aug 25 '21 at 21:50
-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)
axolotl
  • 93
  • 6