What is the the proper way to define a function from an expression?











up vote
5
down vote

favorite
2












Say after some long computation we get an expression



expr=x^2


We do not know what the value of expr beforehand.
Now we want to turn this in a function. We can use either



f[x_]=expr


or



f[x_]:=Evaluate[expr]


as suggested in this question.



However, when we do



f[x_]=Expand[expr]


or



f[x_]:=Evaluate[Expand[expr]]


The Expand will not have any effect.



Is there any way to make this work? Of course, we can define another function



g[x_]:=Expand[f[x]]


But is there any way to do it a bit more concisely?



Update:



If you do what suggested by Kuba in the comment, you get



In[89]:= With[{expr = expr}, f11[x_] := Expand[expr]]
In[90]:= f11[a + b]
Out[90]= x^2
In[91]:= ?? f11
Notebook$$34$907690`f11
f11[x$_]:=Expand[x^2]









share|improve this question




















  • 1




    I think f[x_] = Expand[expr] actually works fine. It expands expr and then turns x into a function slot. Just try it with expr = (1 + x)^10 and then evaluate f[y] after defining f. Or were you expecting something different?
    – Sjoerd Smit
    Dec 4 at 15:47












  • I was expecting to have f[a+b]==Expand[(a+b)^2]=a^2+2 a b+b^2
    – ablmf
    Dec 4 at 16:36










  • In that case I would say that the g[x] := Expand[f[x]] method is really the way to go here, because it makes the evaluation process easiest to follow. Any other method is just going to be confusing one way or another.
    – Sjoerd Smit
    Dec 4 at 17:02

















up vote
5
down vote

favorite
2












Say after some long computation we get an expression



expr=x^2


We do not know what the value of expr beforehand.
Now we want to turn this in a function. We can use either



f[x_]=expr


or



f[x_]:=Evaluate[expr]


as suggested in this question.



However, when we do



f[x_]=Expand[expr]


or



f[x_]:=Evaluate[Expand[expr]]


The Expand will not have any effect.



Is there any way to make this work? Of course, we can define another function



g[x_]:=Expand[f[x]]


But is there any way to do it a bit more concisely?



Update:



If you do what suggested by Kuba in the comment, you get



In[89]:= With[{expr = expr}, f11[x_] := Expand[expr]]
In[90]:= f11[a + b]
Out[90]= x^2
In[91]:= ?? f11
Notebook$$34$907690`f11
f11[x$_]:=Expand[x^2]









share|improve this question




















  • 1




    I think f[x_] = Expand[expr] actually works fine. It expands expr and then turns x into a function slot. Just try it with expr = (1 + x)^10 and then evaluate f[y] after defining f. Or were you expecting something different?
    – Sjoerd Smit
    Dec 4 at 15:47












  • I was expecting to have f[a+b]==Expand[(a+b)^2]=a^2+2 a b+b^2
    – ablmf
    Dec 4 at 16:36










  • In that case I would say that the g[x] := Expand[f[x]] method is really the way to go here, because it makes the evaluation process easiest to follow. Any other method is just going to be confusing one way or another.
    – Sjoerd Smit
    Dec 4 at 17:02















up vote
5
down vote

favorite
2









up vote
5
down vote

favorite
2






2





Say after some long computation we get an expression



expr=x^2


We do not know what the value of expr beforehand.
Now we want to turn this in a function. We can use either



f[x_]=expr


or



f[x_]:=Evaluate[expr]


as suggested in this question.



However, when we do



f[x_]=Expand[expr]


or



f[x_]:=Evaluate[Expand[expr]]


The Expand will not have any effect.



Is there any way to make this work? Of course, we can define another function



g[x_]:=Expand[f[x]]


But is there any way to do it a bit more concisely?



Update:



If you do what suggested by Kuba in the comment, you get



In[89]:= With[{expr = expr}, f11[x_] := Expand[expr]]
In[90]:= f11[a + b]
Out[90]= x^2
In[91]:= ?? f11
Notebook$$34$907690`f11
f11[x$_]:=Expand[x^2]









share|improve this question















Say after some long computation we get an expression



expr=x^2


We do not know what the value of expr beforehand.
Now we want to turn this in a function. We can use either



f[x_]=expr


or



f[x_]:=Evaluate[expr]


as suggested in this question.



However, when we do



f[x_]=Expand[expr]


or



f[x_]:=Evaluate[Expand[expr]]


The Expand will not have any effect.



Is there any way to make this work? Of course, we can define another function



g[x_]:=Expand[f[x]]


But is there any way to do it a bit more concisely?



Update:



If you do what suggested by Kuba in the comment, you get



In[89]:= With[{expr = expr}, f11[x_] := Expand[expr]]
In[90]:= f11[a + b]
Out[90]= x^2
In[91]:= ?? f11
Notebook$$34$907690`f11
f11[x$_]:=Expand[x^2]






function-construction evaluation hold






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 4 at 13:41

























asked Dec 4 at 11:02









ablmf

23917




23917








  • 1




    I think f[x_] = Expand[expr] actually works fine. It expands expr and then turns x into a function slot. Just try it with expr = (1 + x)^10 and then evaluate f[y] after defining f. Or were you expecting something different?
    – Sjoerd Smit
    Dec 4 at 15:47












  • I was expecting to have f[a+b]==Expand[(a+b)^2]=a^2+2 a b+b^2
    – ablmf
    Dec 4 at 16:36










  • In that case I would say that the g[x] := Expand[f[x]] method is really the way to go here, because it makes the evaluation process easiest to follow. Any other method is just going to be confusing one way or another.
    – Sjoerd Smit
    Dec 4 at 17:02
















  • 1




    I think f[x_] = Expand[expr] actually works fine. It expands expr and then turns x into a function slot. Just try it with expr = (1 + x)^10 and then evaluate f[y] after defining f. Or were you expecting something different?
    – Sjoerd Smit
    Dec 4 at 15:47












  • I was expecting to have f[a+b]==Expand[(a+b)^2]=a^2+2 a b+b^2
    – ablmf
    Dec 4 at 16:36










  • In that case I would say that the g[x] := Expand[f[x]] method is really the way to go here, because it makes the evaluation process easiest to follow. Any other method is just going to be confusing one way or another.
    – Sjoerd Smit
    Dec 4 at 17:02










1




1




I think f[x_] = Expand[expr] actually works fine. It expands expr and then turns x into a function slot. Just try it with expr = (1 + x)^10 and then evaluate f[y] after defining f. Or were you expecting something different?
– Sjoerd Smit
Dec 4 at 15:47






I think f[x_] = Expand[expr] actually works fine. It expands expr and then turns x into a function slot. Just try it with expr = (1 + x)^10 and then evaluate f[y] after defining f. Or were you expecting something different?
– Sjoerd Smit
Dec 4 at 15:47














I was expecting to have f[a+b]==Expand[(a+b)^2]=a^2+2 a b+b^2
– ablmf
Dec 4 at 16:36




I was expecting to have f[a+b]==Expand[(a+b)^2]=a^2+2 a b+b^2
– ablmf
Dec 4 at 16:36












In that case I would say that the g[x] := Expand[f[x]] method is really the way to go here, because it makes the evaluation process easiest to follow. Any other method is just going to be confusing one way or another.
– Sjoerd Smit
Dec 4 at 17:02






In that case I would say that the g[x] := Expand[f[x]] method is really the way to go here, because it makes the evaluation process easiest to follow. Any other method is just going to be confusing one way or another.
– Sjoerd Smit
Dec 4 at 17:02












3 Answers
3






active

oldest

votes

















up vote
6
down vote



accepted










Sorry, I was too hasty in comments:



With[{expr = expr}, SetDelayed @@ Hold[f[x_], Expand[expr]]]


SetDelayed @@ Hold is needed because of: Enforcing correct variable bindings and avoiding renamings for conflicting variables in nested scoping constructs






share|improve this answer





















  • why not With[{expr = Expand[expr]}, SetDelayed @@ Hold[f[x_], expr]]
    – Ali Hashmi
    Dec 4 at 12:36






  • 1




    @AliHashmi Because the point is not to evaluate Expand. Compare ?f in my and your case.
    – Kuba
    Dec 4 at 12:37












  • sorry i misinterpreted the question. I thought the purpose was for Evaluate to do its job before the definitions are saved.
    – Ali Hashmi
    Dec 4 at 14:49


















up vote
4
down vote













(f[x_] := Expand@#) &@expr





share|improve this answer





















  • FYI, I'd accept that. This is what I usually do anyway :p
    – Kuba
    Dec 5 at 9:24


















up vote
0
down vote













Dear @ablmf you can use the following syntax in Mathematica.



f[x_]:=x^2;


When you input any desired value for x, Mathematica gives you its value in f[x]. Say, you want f[2], Mathematica gives you 4



f[2]



4




You can also use it to obtain a list. Consider the following code



ClearAll["Global`*"];
f[x_] := x^2;

Table[
f[x]
, {x, 0, 10}]


it gives you




{0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100}







share|improve this answer

















  • 1




    I'm sorry, but this isn't what OP asks for, is it?
    – xzczd
    Dec 4 at 12:09






  • 1




    Sorry, this is not what I asked.
    – ablmf
    Dec 4 at 13:07











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "387"
};
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',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2fmathematica.stackexchange.com%2fquestions%2f187290%2fwhat-is-the-the-proper-way-to-define-a-function-from-an-expression%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
6
down vote



accepted










Sorry, I was too hasty in comments:



With[{expr = expr}, SetDelayed @@ Hold[f[x_], Expand[expr]]]


SetDelayed @@ Hold is needed because of: Enforcing correct variable bindings and avoiding renamings for conflicting variables in nested scoping constructs






share|improve this answer





















  • why not With[{expr = Expand[expr]}, SetDelayed @@ Hold[f[x_], expr]]
    – Ali Hashmi
    Dec 4 at 12:36






  • 1




    @AliHashmi Because the point is not to evaluate Expand. Compare ?f in my and your case.
    – Kuba
    Dec 4 at 12:37












  • sorry i misinterpreted the question. I thought the purpose was for Evaluate to do its job before the definitions are saved.
    – Ali Hashmi
    Dec 4 at 14:49















up vote
6
down vote



accepted










Sorry, I was too hasty in comments:



With[{expr = expr}, SetDelayed @@ Hold[f[x_], Expand[expr]]]


SetDelayed @@ Hold is needed because of: Enforcing correct variable bindings and avoiding renamings for conflicting variables in nested scoping constructs






share|improve this answer





















  • why not With[{expr = Expand[expr]}, SetDelayed @@ Hold[f[x_], expr]]
    – Ali Hashmi
    Dec 4 at 12:36






  • 1




    @AliHashmi Because the point is not to evaluate Expand. Compare ?f in my and your case.
    – Kuba
    Dec 4 at 12:37












  • sorry i misinterpreted the question. I thought the purpose was for Evaluate to do its job before the definitions are saved.
    – Ali Hashmi
    Dec 4 at 14:49













up vote
6
down vote



accepted







up vote
6
down vote



accepted






Sorry, I was too hasty in comments:



With[{expr = expr}, SetDelayed @@ Hold[f[x_], Expand[expr]]]


SetDelayed @@ Hold is needed because of: Enforcing correct variable bindings and avoiding renamings for conflicting variables in nested scoping constructs






share|improve this answer












Sorry, I was too hasty in comments:



With[{expr = expr}, SetDelayed @@ Hold[f[x_], Expand[expr]]]


SetDelayed @@ Hold is needed because of: Enforcing correct variable bindings and avoiding renamings for conflicting variables in nested scoping constructs







share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 4 at 11:39









Kuba

103k12201515




103k12201515












  • why not With[{expr = Expand[expr]}, SetDelayed @@ Hold[f[x_], expr]]
    – Ali Hashmi
    Dec 4 at 12:36






  • 1




    @AliHashmi Because the point is not to evaluate Expand. Compare ?f in my and your case.
    – Kuba
    Dec 4 at 12:37












  • sorry i misinterpreted the question. I thought the purpose was for Evaluate to do its job before the definitions are saved.
    – Ali Hashmi
    Dec 4 at 14:49


















  • why not With[{expr = Expand[expr]}, SetDelayed @@ Hold[f[x_], expr]]
    – Ali Hashmi
    Dec 4 at 12:36






  • 1




    @AliHashmi Because the point is not to evaluate Expand. Compare ?f in my and your case.
    – Kuba
    Dec 4 at 12:37












  • sorry i misinterpreted the question. I thought the purpose was for Evaluate to do its job before the definitions are saved.
    – Ali Hashmi
    Dec 4 at 14:49
















why not With[{expr = Expand[expr]}, SetDelayed @@ Hold[f[x_], expr]]
– Ali Hashmi
Dec 4 at 12:36




why not With[{expr = Expand[expr]}, SetDelayed @@ Hold[f[x_], expr]]
– Ali Hashmi
Dec 4 at 12:36




1




1




@AliHashmi Because the point is not to evaluate Expand. Compare ?f in my and your case.
– Kuba
Dec 4 at 12:37






@AliHashmi Because the point is not to evaluate Expand. Compare ?f in my and your case.
– Kuba
Dec 4 at 12:37














sorry i misinterpreted the question. I thought the purpose was for Evaluate to do its job before the definitions are saved.
– Ali Hashmi
Dec 4 at 14:49




sorry i misinterpreted the question. I thought the purpose was for Evaluate to do its job before the definitions are saved.
– Ali Hashmi
Dec 4 at 14:49










up vote
4
down vote













(f[x_] := Expand@#) &@expr





share|improve this answer





















  • FYI, I'd accept that. This is what I usually do anyway :p
    – Kuba
    Dec 5 at 9:24















up vote
4
down vote













(f[x_] := Expand@#) &@expr





share|improve this answer





















  • FYI, I'd accept that. This is what I usually do anyway :p
    – Kuba
    Dec 5 at 9:24













up vote
4
down vote










up vote
4
down vote









(f[x_] := Expand@#) &@expr





share|improve this answer












(f[x_] := Expand@#) &@expr






share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 4 at 11:48









xzczd

25.7k469245




25.7k469245












  • FYI, I'd accept that. This is what I usually do anyway :p
    – Kuba
    Dec 5 at 9:24


















  • FYI, I'd accept that. This is what I usually do anyway :p
    – Kuba
    Dec 5 at 9:24
















FYI, I'd accept that. This is what I usually do anyway :p
– Kuba
Dec 5 at 9:24




FYI, I'd accept that. This is what I usually do anyway :p
– Kuba
Dec 5 at 9:24










up vote
0
down vote













Dear @ablmf you can use the following syntax in Mathematica.



f[x_]:=x^2;


When you input any desired value for x, Mathematica gives you its value in f[x]. Say, you want f[2], Mathematica gives you 4



f[2]



4




You can also use it to obtain a list. Consider the following code



ClearAll["Global`*"];
f[x_] := x^2;

Table[
f[x]
, {x, 0, 10}]


it gives you




{0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100}







share|improve this answer

















  • 1




    I'm sorry, but this isn't what OP asks for, is it?
    – xzczd
    Dec 4 at 12:09






  • 1




    Sorry, this is not what I asked.
    – ablmf
    Dec 4 at 13:07















up vote
0
down vote













Dear @ablmf you can use the following syntax in Mathematica.



f[x_]:=x^2;


When you input any desired value for x, Mathematica gives you its value in f[x]. Say, you want f[2], Mathematica gives you 4



f[2]



4




You can also use it to obtain a list. Consider the following code



ClearAll["Global`*"];
f[x_] := x^2;

Table[
f[x]
, {x, 0, 10}]


it gives you




{0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100}







share|improve this answer

















  • 1




    I'm sorry, but this isn't what OP asks for, is it?
    – xzczd
    Dec 4 at 12:09






  • 1




    Sorry, this is not what I asked.
    – ablmf
    Dec 4 at 13:07













up vote
0
down vote










up vote
0
down vote









Dear @ablmf you can use the following syntax in Mathematica.



f[x_]:=x^2;


When you input any desired value for x, Mathematica gives you its value in f[x]. Say, you want f[2], Mathematica gives you 4



f[2]



4




You can also use it to obtain a list. Consider the following code



ClearAll["Global`*"];
f[x_] := x^2;

Table[
f[x]
, {x, 0, 10}]


it gives you




{0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100}







share|improve this answer












Dear @ablmf you can use the following syntax in Mathematica.



f[x_]:=x^2;


When you input any desired value for x, Mathematica gives you its value in f[x]. Say, you want f[2], Mathematica gives you 4



f[2]



4




You can also use it to obtain a list. Consider the following code



ClearAll["Global`*"];
f[x_] := x^2;

Table[
f[x]
, {x, 0, 10}]


it gives you




{0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100}








share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 4 at 12:04









Hadi Sobhani

32417




32417








  • 1




    I'm sorry, but this isn't what OP asks for, is it?
    – xzczd
    Dec 4 at 12:09






  • 1




    Sorry, this is not what I asked.
    – ablmf
    Dec 4 at 13:07














  • 1




    I'm sorry, but this isn't what OP asks for, is it?
    – xzczd
    Dec 4 at 12:09






  • 1




    Sorry, this is not what I asked.
    – ablmf
    Dec 4 at 13:07








1




1




I'm sorry, but this isn't what OP asks for, is it?
– xzczd
Dec 4 at 12:09




I'm sorry, but this isn't what OP asks for, is it?
– xzczd
Dec 4 at 12:09




1




1




Sorry, this is not what I asked.
– ablmf
Dec 4 at 13:07




Sorry, this is not what I asked.
– ablmf
Dec 4 at 13:07


















draft saved

draft discarded




















































Thanks for contributing an answer to Mathematica Stack Exchange!


  • 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.


Use MathJax to format equations. MathJax reference.


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





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2fmathematica.stackexchange.com%2fquestions%2f187290%2fwhat-is-the-the-proper-way-to-define-a-function-from-an-expression%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







Popular posts from this blog

Probability when a professor distributes a quiz and homework assignment to a class of n students.

Aardman Animations

Are they similar matrix