What is the the proper way to define a function from an expression?
up vote
5
down vote
favorite
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
add a comment |
up vote
5
down vote
favorite
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
1
I thinkf[x_] = Expand[expr]
actually works fine. It expandsexpr
and then turnsx
into a function slot. Just try it withexpr = (1 + x)^10
and then evaluatef[y]
after definingf
. Or were you expecting something different?
– Sjoerd Smit
yesterday
I was expecting to havef[a+b]==Expand[(a+b)^2]=a^2+2 a b+b^2
– ablmf
yesterday
In that case I would say that theg[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
yesterday
add a comment |
up vote
5
down vote
favorite
up vote
5
down vote
favorite
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
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
function-construction evaluation hold
edited yesterday
asked yesterday
ablmf
21717
21717
1
I thinkf[x_] = Expand[expr]
actually works fine. It expandsexpr
and then turnsx
into a function slot. Just try it withexpr = (1 + x)^10
and then evaluatef[y]
after definingf
. Or were you expecting something different?
– Sjoerd Smit
yesterday
I was expecting to havef[a+b]==Expand[(a+b)^2]=a^2+2 a b+b^2
– ablmf
yesterday
In that case I would say that theg[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
yesterday
add a comment |
1
I thinkf[x_] = Expand[expr]
actually works fine. It expandsexpr
and then turnsx
into a function slot. Just try it withexpr = (1 + x)^10
and then evaluatef[y]
after definingf
. Or were you expecting something different?
– Sjoerd Smit
yesterday
I was expecting to havef[a+b]==Expand[(a+b)^2]=a^2+2 a b+b^2
– ablmf
yesterday
In that case I would say that theg[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
yesterday
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
yesterday
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
yesterday
I was expecting to have
f[a+b]==Expand[(a+b)^2]=a^2+2 a b+b^2
– ablmf
yesterday
I was expecting to have
f[a+b]==Expand[(a+b)^2]=a^2+2 a b+b^2
– ablmf
yesterday
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
yesterday
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
yesterday
add a comment |
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
why notWith[{expr = Expand[expr]}, SetDelayed @@ Hold[f[x_], expr]]
– Ali Hashmi
yesterday
1
@AliHashmi Because the point is not to evaluateExpand
. Compare?f
in my and your case.
– Kuba♦
yesterday
sorry i misinterpreted the question. I thought the purpose was for Evaluate to do its job before the definitions are saved.
– Ali Hashmi
yesterday
add a comment |
up vote
4
down vote
(f[x_] := Expand@#) &@expr
FYI, I'd accept that. This is what I usually do anyway :p
– Kuba♦
19 hours ago
add a comment |
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}
1
I'm sorry, but this isn't what OP asks for, is it?
– xzczd
yesterday
1
Sorry, this is not what I asked.
– ablmf
yesterday
add a comment |
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
why notWith[{expr = Expand[expr]}, SetDelayed @@ Hold[f[x_], expr]]
– Ali Hashmi
yesterday
1
@AliHashmi Because the point is not to evaluateExpand
. Compare?f
in my and your case.
– Kuba♦
yesterday
sorry i misinterpreted the question. I thought the purpose was for Evaluate to do its job before the definitions are saved.
– Ali Hashmi
yesterday
add a comment |
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
why notWith[{expr = Expand[expr]}, SetDelayed @@ Hold[f[x_], expr]]
– Ali Hashmi
yesterday
1
@AliHashmi Because the point is not to evaluateExpand
. Compare?f
in my and your case.
– Kuba♦
yesterday
sorry i misinterpreted the question. I thought the purpose was for Evaluate to do its job before the definitions are saved.
– Ali Hashmi
yesterday
add a comment |
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
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
answered yesterday
Kuba♦
103k12201512
103k12201512
why notWith[{expr = Expand[expr]}, SetDelayed @@ Hold[f[x_], expr]]
– Ali Hashmi
yesterday
1
@AliHashmi Because the point is not to evaluateExpand
. Compare?f
in my and your case.
– Kuba♦
yesterday
sorry i misinterpreted the question. I thought the purpose was for Evaluate to do its job before the definitions are saved.
– Ali Hashmi
yesterday
add a comment |
why notWith[{expr = Expand[expr]}, SetDelayed @@ Hold[f[x_], expr]]
– Ali Hashmi
yesterday
1
@AliHashmi Because the point is not to evaluateExpand
. Compare?f
in my and your case.
– Kuba♦
yesterday
sorry i misinterpreted the question. I thought the purpose was for Evaluate to do its job before the definitions are saved.
– Ali Hashmi
yesterday
why not
With[{expr = Expand[expr]}, SetDelayed @@ Hold[f[x_], expr]]
– Ali Hashmi
yesterday
why not
With[{expr = Expand[expr]}, SetDelayed @@ Hold[f[x_], expr]]
– Ali Hashmi
yesterday
1
1
@AliHashmi Because the point is not to evaluate
Expand
. Compare ?f
in my and your case.– Kuba♦
yesterday
@AliHashmi Because the point is not to evaluate
Expand
. Compare ?f
in my and your case.– Kuba♦
yesterday
sorry i misinterpreted the question. I thought the purpose was for Evaluate to do its job before the definitions are saved.
– Ali Hashmi
yesterday
sorry i misinterpreted the question. I thought the purpose was for Evaluate to do its job before the definitions are saved.
– Ali Hashmi
yesterday
add a comment |
up vote
4
down vote
(f[x_] := Expand@#) &@expr
FYI, I'd accept that. This is what I usually do anyway :p
– Kuba♦
19 hours ago
add a comment |
up vote
4
down vote
(f[x_] := Expand@#) &@expr
FYI, I'd accept that. This is what I usually do anyway :p
– Kuba♦
19 hours ago
add a comment |
up vote
4
down vote
up vote
4
down vote
(f[x_] := Expand@#) &@expr
(f[x_] := Expand@#) &@expr
answered yesterday
xzczd
25.7k468244
25.7k468244
FYI, I'd accept that. This is what I usually do anyway :p
– Kuba♦
19 hours ago
add a comment |
FYI, I'd accept that. This is what I usually do anyway :p
– Kuba♦
19 hours ago
FYI, I'd accept that. This is what I usually do anyway :p
– Kuba♦
19 hours ago
FYI, I'd accept that. This is what I usually do anyway :p
– Kuba♦
19 hours ago
add a comment |
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}
1
I'm sorry, but this isn't what OP asks for, is it?
– xzczd
yesterday
1
Sorry, this is not what I asked.
– ablmf
yesterday
add a comment |
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}
1
I'm sorry, but this isn't what OP asks for, is it?
– xzczd
yesterday
1
Sorry, this is not what I asked.
– ablmf
yesterday
add a comment |
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}
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}
answered yesterday
Hadi Sobhani
32417
32417
1
I'm sorry, but this isn't what OP asks for, is it?
– xzczd
yesterday
1
Sorry, this is not what I asked.
– ablmf
yesterday
add a comment |
1
I'm sorry, but this isn't what OP asks for, is it?
– xzczd
yesterday
1
Sorry, this is not what I asked.
– ablmf
yesterday
1
1
I'm sorry, but this isn't what OP asks for, is it?
– xzczd
yesterday
I'm sorry, but this isn't what OP asks for, is it?
– xzczd
yesterday
1
1
Sorry, this is not what I asked.
– ablmf
yesterday
Sorry, this is not what I asked.
– ablmf
yesterday
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
I think
f[x_] = Expand[expr]
actually works fine. It expandsexpr
and then turnsx
into a function slot. Just try it withexpr = (1 + x)^10
and then evaluatef[y]
after definingf
. Or were you expecting something different?– Sjoerd Smit
yesterday
I was expecting to have
f[a+b]==Expand[(a+b)^2]=a^2+2 a b+b^2
– ablmf
yesterday
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
yesterday