Message Boards Message Boards

4 Replies
1 Total Likes
View groups...
Share this post:

Unfold nested functions

Posted 11 years ago
a[i_] := Subscript[p, i] + Subscript[q, i];

b[i_] := Exp[a[i] + z];
c[i_, j_] := \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(k = i\), \(j\)]\(b[k]\)\);
c[2, 3]

At times I want to describe a complex solution, which is formed by some simpler expressions. Layers of expression can be seen in my example, from most concrete (i.e. a) to most abstract (i.e. c[i,j]).
When examining whether the solution is accurately formulated, I want to unfold my solution to the next layer by expanding the solution a little bit more complex, rather than the most concrete layer (which would be the most complex one).
In my example, Mathematica unfolds the solution directly to the most complex form. Something I want is like
c[2,3]=b[2]+b[3] (* function c is unfold, but b is maintained *)
      =Exp[a[2]+z]+Exp[a[3]+z] (* b is unfold, but a is maintained *)
      =Exp[p_2+q_2+z]+Exp[p_3+q_3+z] (* a is unfold. Now the solution is in the most complex (concrete) form *)
How can I get the first two rows above?
4 Replies
I'm not quite sure what exactly you're going for, but maybe this can give you some ideas (with and without HoldForm):
expr = HoldForm[Exp[Subscript[p, 2] + Subscript[q, 2] + z] + Exp[Subscript[p, 3] + Subscript[q, 3] + z]];
Replace[expr, {g_[___] :> g["\[Ellipsis]"]}, {-2}]
You're right that Mathematica should have a built-in function for iterative application. In this case it's Nest:
ApplyN[0, f, 5] == Nest[f, 0, 5]
There's also NestList, FixedPoint/FixedPointList, Fold/FoldList, NestWhile/NestWhileList, and the often-forgotten ComposeList. They're all quite useful. Off the top of my head I only remember one case of Mathematica not having a structural operator that I thought it should have. It's just a matter of searching the docs and recognizing that the names can be a bit goofy (eg I'm looking for "Intersperse" and find "Riffle").
Posted 11 years ago
Hi Antonio,
Thanks for your hints. With Nest function I can get the result I was looking for, i.e. repeatedly expanding a expression to check if I get the right expression.

I value the first code fragment you provided and maybe utilize the trick in future circumstances. As for now, Nest is enough :]
Perhaps use something like 
b[i_] := HoldForm[Exp[a[i] + z]];
c[i_, j_] := HoldForm[Sum[b[k], {k, i, j}]];
then use ReleaseHold once for each layer.
c[2, 3]

ReleaseHold[c[2, 3]]

ReleaseHold[ReleaseHold[c[2, 3]]]
POSTED BY: Ilian Gachevski
Posted 11 years ago
Hi  Ilian,
Sorry for replying so late. Your suggestion is fairly valuable. I tried it as below, but found two more questions.
1. How can I repeatedly apply a function to a expression? I defined a recursive function ApplyN for this purpose. But is there any easier way? I think Mathematica should have implemented this simple function already.
2.  Apply[ReleaseHold, c[2, 3], {-1}] corresponds to the 1st layer, and Apply[ReleaseHold, c[2, 3], {0}] corresponds to the 3rd layer. How come? How can I get the 2nd layer by Apply[ReleaseHold, c[2, 3].....]?
Thanks in advance!
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract