Adding two functions / multiplying two functions yields wrong answer python










0















I've made a simple example to try and add two functions (for now, want multiply in the end but neither of them work so I decided to try adding).



def addFun(fun1,fun2):
fun = lambda x: fun1(x) + fun2(x)
return fun
d = [12,10]
B = 5
fun1 = lambda d,L: (d-L)**2

result1 = lambda L: 0
for i in range(0,len(d)):
func = lambda L: fun1(float(d[i]),L)
result1 = addFun(result1,func)
print(func(1))

print(result1(1))


The print functions are there to check whether it's working or not (so far it's not. So ideally what should happen is (d-L)^2 for d = 12 is (12-L)^2. I add that to result1 which is now (12-L)^2. Then on the next loop it becomes (10-L)^2. Now the addFun function should add these two together, so the new result1 should be
(12-L)^2 + (10-L)^2. Then I substitute L as 1, answer should be 11^2 + 9^2 = 202. However this is not the case. When I run this through, the resulting answer is 162. Worst thing is the + sign doesn't commute; when I put in 10 first and 12 second, the answer becomes 242.



I've had this working for simpler equations, such as adding x^2 with x^3 e.t.c. But does anyone know why this isn't working here?










share|improve this question






















  • don't dox lambda functions

    – wim
    Nov 13 '18 at 16:10












  • @wim why not? What's wrong with doing that in python

    – ZWang
    Nov 13 '18 at 16:13






  • 1





    d and i are being looked up by name in your func lambda - when you finally execute the whole thing at the end, all of the individual lambdas will see the same value of i, the one it had in the final iteration of the loop. Usual solution is to write it as lambda L, i=i: so that you capture the current value of i as a default parameter.

    – jasonharper
    Nov 13 '18 at 16:37











  • @jasonharper Yeah thats worked thank you very much!

    – ZWang
    Nov 13 '18 at 19:58















0















I've made a simple example to try and add two functions (for now, want multiply in the end but neither of them work so I decided to try adding).



def addFun(fun1,fun2):
fun = lambda x: fun1(x) + fun2(x)
return fun
d = [12,10]
B = 5
fun1 = lambda d,L: (d-L)**2

result1 = lambda L: 0
for i in range(0,len(d)):
func = lambda L: fun1(float(d[i]),L)
result1 = addFun(result1,func)
print(func(1))

print(result1(1))


The print functions are there to check whether it's working or not (so far it's not. So ideally what should happen is (d-L)^2 for d = 12 is (12-L)^2. I add that to result1 which is now (12-L)^2. Then on the next loop it becomes (10-L)^2. Now the addFun function should add these two together, so the new result1 should be
(12-L)^2 + (10-L)^2. Then I substitute L as 1, answer should be 11^2 + 9^2 = 202. However this is not the case. When I run this through, the resulting answer is 162. Worst thing is the + sign doesn't commute; when I put in 10 first and 12 second, the answer becomes 242.



I've had this working for simpler equations, such as adding x^2 with x^3 e.t.c. But does anyone know why this isn't working here?










share|improve this question






















  • don't dox lambda functions

    – wim
    Nov 13 '18 at 16:10












  • @wim why not? What's wrong with doing that in python

    – ZWang
    Nov 13 '18 at 16:13






  • 1





    d and i are being looked up by name in your func lambda - when you finally execute the whole thing at the end, all of the individual lambdas will see the same value of i, the one it had in the final iteration of the loop. Usual solution is to write it as lambda L, i=i: so that you capture the current value of i as a default parameter.

    – jasonharper
    Nov 13 '18 at 16:37











  • @jasonharper Yeah thats worked thank you very much!

    – ZWang
    Nov 13 '18 at 19:58













0












0








0








I've made a simple example to try and add two functions (for now, want multiply in the end but neither of them work so I decided to try adding).



def addFun(fun1,fun2):
fun = lambda x: fun1(x) + fun2(x)
return fun
d = [12,10]
B = 5
fun1 = lambda d,L: (d-L)**2

result1 = lambda L: 0
for i in range(0,len(d)):
func = lambda L: fun1(float(d[i]),L)
result1 = addFun(result1,func)
print(func(1))

print(result1(1))


The print functions are there to check whether it's working or not (so far it's not. So ideally what should happen is (d-L)^2 for d = 12 is (12-L)^2. I add that to result1 which is now (12-L)^2. Then on the next loop it becomes (10-L)^2. Now the addFun function should add these two together, so the new result1 should be
(12-L)^2 + (10-L)^2. Then I substitute L as 1, answer should be 11^2 + 9^2 = 202. However this is not the case. When I run this through, the resulting answer is 162. Worst thing is the + sign doesn't commute; when I put in 10 first and 12 second, the answer becomes 242.



I've had this working for simpler equations, such as adding x^2 with x^3 e.t.c. But does anyone know why this isn't working here?










share|improve this question














I've made a simple example to try and add two functions (for now, want multiply in the end but neither of them work so I decided to try adding).



def addFun(fun1,fun2):
fun = lambda x: fun1(x) + fun2(x)
return fun
d = [12,10]
B = 5
fun1 = lambda d,L: (d-L)**2

result1 = lambda L: 0
for i in range(0,len(d)):
func = lambda L: fun1(float(d[i]),L)
result1 = addFun(result1,func)
print(func(1))

print(result1(1))


The print functions are there to check whether it's working or not (so far it's not. So ideally what should happen is (d-L)^2 for d = 12 is (12-L)^2. I add that to result1 which is now (12-L)^2. Then on the next loop it becomes (10-L)^2. Now the addFun function should add these two together, so the new result1 should be
(12-L)^2 + (10-L)^2. Then I substitute L as 1, answer should be 11^2 + 9^2 = 202. However this is not the case. When I run this through, the resulting answer is 162. Worst thing is the + sign doesn't commute; when I put in 10 first and 12 second, the answer becomes 242.



I've had this working for simpler equations, such as adding x^2 with x^3 e.t.c. But does anyone know why this isn't working here?







python function addition






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 16:07









ZWangZWang

665




665












  • don't dox lambda functions

    – wim
    Nov 13 '18 at 16:10












  • @wim why not? What's wrong with doing that in python

    – ZWang
    Nov 13 '18 at 16:13






  • 1





    d and i are being looked up by name in your func lambda - when you finally execute the whole thing at the end, all of the individual lambdas will see the same value of i, the one it had in the final iteration of the loop. Usual solution is to write it as lambda L, i=i: so that you capture the current value of i as a default parameter.

    – jasonharper
    Nov 13 '18 at 16:37











  • @jasonharper Yeah thats worked thank you very much!

    – ZWang
    Nov 13 '18 at 19:58

















  • don't dox lambda functions

    – wim
    Nov 13 '18 at 16:10












  • @wim why not? What's wrong with doing that in python

    – ZWang
    Nov 13 '18 at 16:13






  • 1





    d and i are being looked up by name in your func lambda - when you finally execute the whole thing at the end, all of the individual lambdas will see the same value of i, the one it had in the final iteration of the loop. Usual solution is to write it as lambda L, i=i: so that you capture the current value of i as a default parameter.

    – jasonharper
    Nov 13 '18 at 16:37











  • @jasonharper Yeah thats worked thank you very much!

    – ZWang
    Nov 13 '18 at 19:58
















don't dox lambda functions

– wim
Nov 13 '18 at 16:10






don't dox lambda functions

– wim
Nov 13 '18 at 16:10














@wim why not? What's wrong with doing that in python

– ZWang
Nov 13 '18 at 16:13





@wim why not? What's wrong with doing that in python

– ZWang
Nov 13 '18 at 16:13




1




1





d and i are being looked up by name in your func lambda - when you finally execute the whole thing at the end, all of the individual lambdas will see the same value of i, the one it had in the final iteration of the loop. Usual solution is to write it as lambda L, i=i: so that you capture the current value of i as a default parameter.

– jasonharper
Nov 13 '18 at 16:37





d and i are being looked up by name in your func lambda - when you finally execute the whole thing at the end, all of the individual lambdas will see the same value of i, the one it had in the final iteration of the loop. Usual solution is to write it as lambda L, i=i: so that you capture the current value of i as a default parameter.

– jasonharper
Nov 13 '18 at 16:37













@jasonharper Yeah thats worked thank you very much!

– ZWang
Nov 13 '18 at 19:58





@jasonharper Yeah thats worked thank you very much!

– ZWang
Nov 13 '18 at 19:58












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285007%2fadding-two-functions-multiplying-two-functions-yields-wrong-answer-python%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285007%2fadding-two-functions-multiplying-two-functions-yields-wrong-answer-python%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

What does pagestruct do in Eviews?

Dutch intervention in Lombok and Karangasem

Channel Islands