7
$\begingroup$

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]
$\endgroup$
  • 1
    $\begingroup$ 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? $\endgroup$ – Sjoerd Smit Dec 4 '18 at 15:47
  • $\begingroup$ I was expecting to have f[a+b]==Expand[(a+b)^2]=a^2+2 a b+b^2 $\endgroup$ – ablmf Dec 4 '18 at 16:36
  • $\begingroup$ 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. $\endgroup$ – Sjoerd Smit Dec 4 '18 at 17:02
  • 1
    $\begingroup$ Closely related: (69590) $\endgroup$ – Mr.Wizard Nov 11 '19 at 14:27
8
$\begingroup$

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

$\endgroup$
  • $\begingroup$ why not With[{expr = Expand[expr]}, SetDelayed @@ Hold[f[x_], expr]] $\endgroup$ – Ali Hashmi Dec 4 '18 at 12:36
  • 1
    $\begingroup$ @AliHashmi Because the point is not to evaluate Expand. Compare ?f in my and your case. $\endgroup$ – Kuba Dec 4 '18 at 12:37
  • $\begingroup$ sorry i misinterpreted the question. I thought the purpose was for Evaluate to do its job before the definitions are saved. $\endgroup$ – Ali Hashmi Dec 4 '18 at 14:49
6
$\begingroup$
(f[x_] := Expand@#) &@expr
$\endgroup$
  • $\begingroup$ FYI, I'd accept that. This is what I usually do anyway :p $\endgroup$ – Kuba Dec 5 '18 at 9:24
3
$\begingroup$

Here are a couple more possibilities. Using Block:

Clear[f]
Block[{Expand}, f[x_] = Expand[expr]];
Definition[f]

f[x_]=Expand[(1+x)^2]

Using With:

Clear[f]
With[{expr = expr, lhs = f[x_]},
    lhs := Expand[expr]
];
Definition[f]

f[x_]:=Expand[(1+x)^2]

$\endgroup$
1
$\begingroup$

This works, and the general idea is useful for other things too,

ToExpression["f[x_]:= Expand[" <> ToString[InputForm[expr]] <> "]"]

To test it, let's try something not already expanded,

expr = (1 + x)^2

Using the above way to make f, one finds that Definition[f] // InputForm returns

f[x_] := Expand[(1 + x)^2]  

as required. But we don't want to have to remember how to do all that, so let's just do it once for any f and any expr,

MakeFunction[f_, expr_]:= 
  ToExpression[ToString[f] <> "[x_]:= 
     Expand[" <> ToString[InputForm[expr]] <> "]"]

Next thing to do would be define Options for MakeFunction so that instead of just Expand, you could instead use Simplify, etc.

$\endgroup$
0
$\begingroup$

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}

$\endgroup$
  • 1
    $\begingroup$ I'm sorry, but this isn't what OP asks for, is it? $\endgroup$ – xzczd Dec 4 '18 at 12:09
  • 1
    $\begingroup$ Sorry, this is not what I asked. $\endgroup$ – ablmf Dec 4 '18 at 13:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.