You can Inactivate
the definition to resolve variables in it (if they are not the head of an expression), then immediately Activate
it to evaluate the definition.
thing = (a + x) y;
Activate@Inactivate[thingy[a_, y_] := NIntegrate[thing, {x, 0, 10}]];
DownValues@thingy
{HoldPattern[thingy[a_, y_]] :> NIntegrate[(a + x) y, {x, 0, 10}]}
You can wrap variables you don't wish to expand in Inactive
to prevent unwanted substitutions.
Block[{x = 2, test},
Activate@
Inactivate[test[b_] := Integrate[x, {x, 0, b}]];
DownValues@test]
Block[{x = 2, test},
Activate@
Inactivate[test[b_] := Integrate[Inactive@x, {Inactive@x, 0, b}]];
DownValues@test]
{HoldPattern[test[b_]] :> \!\(
\*SubsuperscriptBox[\(\[Integral]\), \(0\), \(b\)]\(2 \
\[DifferentialD]2\)\)}
(* {HoldPattern[test[b_]] :> Integrate[2, {2, 0, b}]} *)
{HoldPattern[test[b_]] :> \!\(
\*SubsuperscriptBox[\(\[Integral]\), \(0\), \(b\)]\(x \
\[DifferentialD]x\)\)}
(* {HoldPattern[test[b_]] :> Integrate[x, {x, 0, b}]} *)
In the original expression, since :=
(SetDelayed
) does not evaluate its right hand side, variables present there will not be expanded.
Inactivate[...]
works by wrapping all heads in an expression with Inactive
, inhibiting those heads' further evaluation. Importantly, the now-inactivated head Inactive[SetDelayed]
does not prevent evaluation of its right hand side. This allows evaluation to proceed into the expression, identify that thing
evaluates to (a + x) y
, and perform the substitution accordingly. This yields an Inactivate
d definition where variable substitutions have been made.
Activate@
Inactivate[thingy[a_, y_] := NIntegrate[thing, {x, 0, 10}]] // Trace
(* Mathematica formats Inactive heads in output with TemplateBox.
Here they are shown in bold italics instead. *)
{{Inactivate[thingy[a_, y_] := NIntegrate[thing, {x, 0, 10}]],
thingy[a_, y_] :=
NIntegrate[thing, {x, 0, 10}], {{thing, (a + x) y},
NIntegrate[(a + x) y, {x, 0, 10}]},
thingy[a_, y_] := NIntegrate[(a + x) y, {x, 0, 10}]},
Activate[thingy[a_, y_] := NIntegrate[(a + x) y, {x, 0, 10}]],
thingy[a_, y_] := NIntegrate[(a + x) y, {x, 0, 10}], Null}
Finally, we Activate
to remove the Inactive
s, and the definition evaluates.
thing
doesn't really have a value, it's not a reference to a memory location, for example. It's just something that the evaluator will replace according to the rewrite rule. Having said that, if the evaluator is running and encountersthing
, it will immediately replace it, which is effectively "pasting its contents". However... $\endgroup$(a+x) y
expression and bring up the context menu, you can choose to iconize it. There is also theIconize
symbol. You can copy/paste this thing around the notebook. $\endgroup$Iconize[(a + x) y, "thing"]
. Now you can copy/paste that icon into your NIntegrate expression. It'll look like a little gray thing labeled "thing", but it will really be the expression(a+x)y
. $\endgroup$