Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

graphing traces of complicated evaluations (improved)

2 views
Skip to first unread message

Chris Chiasson

unread,
Jul 20, 2007, 3:39:12 AM7/20/07
to
Previously, I released code that could help with the tracing of
evaluations, but the code required read access to the DownValues of
the functions being traced. This version does not.

Here are some example evaluations:

TraceTreeFormPlot[Trace[1+2]]

aaaaa:=Sequence[1+2,a]
TraceTreeFormPlot[Trace@aaaaa]

XML`MathML`ExpressionToMathML@"\[Alpha]";(*evaluate once to skip code
caching in the trace*)
LayeredGraphPlot[traceToTree[Trace[XML`MathML`ExpressionToMathML@"\[Alpha]",TraceDepth->2,TraceInternal->True]][[All,All,3]],SelfLoopStyle->None]

----------------------------------------------------
code:
----------------------------------------------------

(Attributes[#]={HoldAllComplete})&/@{traceToTreeAux}

traceToTree[xpr_]:=Block[{traceToTreeCounter=-1,expressionToVertex,insideHoldForm=False},Attributes@expressionToVertex={HoldAllComplete};expressionToVertex[anyXpr_/;AtomQ@Unevaluated@anyXpr]:=expressionToVertex[anyXpr]={HoldForm@anyXpr,traceToTreeCounter+=1,HoldForm@anyXpr};expressionToVertex[holdFormXpr_HoldForm/;Length@holdFormXpr===1&&!insideHoldForm]:=Block[{insideHoldForm=True},Extract[Unevaluated@holdFormXpr,1,expressionToVertex]];expressionToVertex[holdFormXpr_HoldForm]:=expressionToVertex[holdFormXpr]={HoldForm@Sequence,traceToTreeCounter+=1,holdFormXpr};expressionToVertex[anyXpr_]:=expressionToVertex[anyXpr]={Extract[Unevaluated@anyXpr,0,HoldForm],traceToTreeCounter+=1,HoldForm@anyXpr};Rule@@@Flatten@{traceToTreeAux@xpr}]

traceToTreeAux[xpr:Except@_HoldForm/;!AtomQ@Unevaluated@xpr]:=List@@@{With[{rootVertex=expressionToVertex@xpr},edge[rootVertex,expressionToVertex@#]&/@Unevaluated@xpr],traceToTreeAux/@Unevaluated@xpr}

traceToTreeAux[xpr_]:=edge[expressionToVertex@xpr,expressionToVertex@xpr]

TraceTreeFormPlot[trace_,opts___]:=Block[{$traceExpressionToTree=True},Through@{Unprotect,Update}@SparseArray`ExpressionToTree;SparseArray`ExpressionToTree[trace,Infinity]=traceToTree@trace;With[{result=ToExpression@ToBoxes@TreeForm[trace,opts]},Through@{Unprotect,Update}@SparseArray`ExpressionToTree;SparseArray`ExpressionToTree[trace,Infinity]=.;Through@{Update,Protect,Update}@SparseArray`ExpressionToTree;result]]

----------------------------------------------------
keywords:
----------------------------------------------------
Trace TreeForm Plot

--
http://chris.chiasson.name/

Chris Chiasson

unread,
Jul 21, 2007, 4:44:44 AM7/21/07
to
This version uses Short and ToString so that the graph Tooltips don't
crash Mathematica. It is also a little more evaluation-safe.

(Attributes@#={HoldAllComplete})&/@{traceToTreeAux,toVertex,HoldFormComplete,getAtoms,getAtomsAux}
MakeBoxes[HoldFormComplete[args___],form_]:=MakeBoxes[HoldForm[args],form]
edge[{head1_,pos1_,xpr1_},{head2_,pos2_,xpr2_}]:=Quiet[Rule[{head1,vertexNumberFunction@pos1,xpr1},{head2,vertexNumberFunction@pos2,xpr2}],{Rule::"rhs"}]
getAtomsAux[atom_/;AtomQ@Unevaluated@atom]:=Sow[HoldFormComplete@atom,getAtomsAux]
getAtomsAux[xpr_]:=Map[getAtomsAux,Unevaluated@xpr,Heads->True]
getAtoms[xpr_]:=Flatten@Reap[getAtomsAux@xpr][[2]]
toVertex[traceToTreeAux[HoldForm[heldXpr_],pos_]]:=toVertex[heldXpr]
toVertex[traceToTreeAux[HoldForm[heldXprs___],pos_]]:=toVertex@traceToTreeAux[Sequence[],pos]
(*this code is strong enough to not need the ToString commands, but
some of the resulting graph vertices give trouble to the graphing
routines*)
toVertex[traceToTreeAux[xpr_,pos_]]:={ToString[Short@Extract[Unevaluated@xpr,0,HoldFormComplete],StandardForm],pos,ToString[Short@First@originalTraceExtract@{pos},StandardForm]}
traceToTreeAux[xpr_/;AtomQ@Unevaluated@xpr,___]:=Sequence[]
traceToTreeAux[_HoldForm,___]:=Sequence[]
traceToTreeAux[xpr_,pos_]:=With[{lhs=toVertex@traceToTreeAux[xpr,pos],args=HoldComplete@@Unevaluated@xpr},Identity[Sequence][ReleaseHold[Function[Null,edge[lhs,toVertex@#],HoldAllComplete]/@args],ReleaseHold@args]]
traceToTree[xpr_]:=Block[{vertexNumber=-1,vertexNumberFunction,originalTraceExtract},vertexNumberFunction[arg_]:=vertexNumberFunction[arg]=++vertexNumber;originalTraceExtract[pos_]:=Extract[Unevaluated@xpr,pos,HoldFormComplete];{MapIndexed[traceToTreeAux,Unevaluated@xpr,{0,Infinity}]}]


TraceTreeFormPlot[trace_,opts___]:=Block[{$traceExpressionToTree=True},Through@{Unprotect,Update}@SparseArray`ExpressionToTree;SparseArray`ExpressionToTree[trace,Infinity]=traceToTree@trace;With[{result=ToExpression@ToBoxes@TreeForm[trace,opts]},Through@{Unprotect,Update}@SparseArray`ExpressionToTree;SparseArray`ExpressionToTree[trace,Infinity]=.;Through@{Update,Protect,Update}@SparseArray`ExpressionToTree;result]]


--
http://chris.chiasson.name/

chuck009

unread,
Jul 21, 2007, 5:04:08 AM7/21/07
to
Suppose I'm tenacious. Really I'm just not satisfied with Integrate returning Pi^2/6 for this integral. I know it's a bug but aren't they just curious why it's giving this answer?

In[15]:=
Integrate[Log[1 + Exp[-z]/Sqrt[z]], {z, 0, Infinity}]

Out[15]=
Pi^2/6

I tried using your code to trace it but to no avail. Any suggestions?

dimitris

unread,
Jul 22, 2007, 4:23:10 AM7/22/07
to
> > evaluations,- -
>
> - -

Since

In[28]:=
Integrate[Log[1 + Exp[-z]/Sqrt[z]], z]

Out[28]=
Integrate[Log[1 + 1/(E^z*Sqrt[z])], z]

the integral is evaluated with the convolution method.

(BTW, the integral is not a trivial one for the Risch algorithm.

Professor R. Fateman some time ago wrote to me...

"While it may not seem hard to you, it turns out that there are three
components to the Risch integration algorithm: exponential, log and
algebraic.
this integrand, if it is done using the Risch algorithm, requires that
all of them work, together.

Since there is not, so far as I know, a complete implementation of the
algebraic case in Mathematica, there may be problems with
the hacks taken to bypass this lack of implementation.
So I don't find the issue to be surprising."
)

Somewhere during the process something went wrong.

I don't see how you could detect where it happens this failure
(bear in mind that it might be one place that the code fails; or
even more than one place!)
Only someone with a good knowledge of symbolic algebra could
do such a task...

Dimitris

chuck009

unread,
Jul 23, 2007, 3:43:51 AM7/23/07
to
Hello Dimitris,

Do you know how to "manually" calculate an analytic solution to this integral? Suppose I can look up convolution method and Risch algorithm. Also, I suspect there are "internal" debugging tools at Wolfram that would allow a "sharp" programmer there to track down where the error is occurring. I could if it were my code :)

dimitris

unread,
Jul 24, 2007, 6:03:03 AM7/24/07
to
> > Dimitris- -
>
> - -

No!
Having tried a lot of things, I gae up!
I hope someone other will find a correct closed form solution
for this integral.

Dimitris


Chris Chiasson

unread,
Jul 28, 2007, 5:47:35 AM7/28/07
to
A different version of this email and a follow up were sent to
MathGroup (on the 19th) but didn't appear as far as I can tell. Here
is the combined version:

Previously, I released code that could help with the tracing of

evaluations, but the code required read access to the DownValues of
the functions being traced. This version does not.

Here are some example evaluations:

aaaaa:=Sequence[1+2,a]
TraceTreeFormPlot[Trace@aaaaa]

XML`MathML`ExpressionToMathML@"\[Alpha]";(*evaluate once to skip code
caching in the trace*)

LayeredGraphPlot[traceToTree[Trace[XML`MathML`ExpressionToMathML@"\[Alpha]",TraceDepth->2,TraceInternal->True]][[All,All,All]],SelfLoopStyle->None]


Code:

(Attributes@#={HoldAllComplete})&/@{traceToTreeAux,toVertex,HoldFormComplete}


MakeBoxes[HoldFormComplete[args___],form_]:=MakeBoxes[HoldForm[args],form]
edge[{head1_,pos1_,xpr1_},{head2_,pos2_,xpr2_}]:=Quiet[Rule[{head1,vertexNumberFunction@pos1,xpr1},{head2,vertexNumberFunction@pos2,xpr2}],{Rule::"rhs"}]

toVertex[traceToTreeAux[HoldForm[heldXpr_],pos_]]:=toVertex[heldXpr]
toVertex[traceToTreeAux[HoldForm[heldXprs___],pos_]]:=toVertex@traceToTreeAux[Sequence[],pos]
(*this code is strong enough to not need the ToString commands,but
some of the resulting graph vertices give trouble to the graphing
routines*)
toVertex[traceToTreeAux[xpr_,pos_]]:={ToString[Short@Extract[Unevaluated@xpr,0,HoldFormComplete],StandardForm],pos,ToString[Short@First@originalTraceExtract@{pos},StandardForm]}
traceToTreeAux[xpr_/;AtomQ@Unevaluated@xpr,___]:=Sequence[]
traceToTreeAux[_HoldForm,___]:=Sequence[]
traceToTreeAux[xpr_,pos_]:=With[{lhs=toVertex@traceToTreeAux[xpr,pos],args=HoldComplete@@Unevaluated@xpr},Identity[Sequence][ReleaseHold[Function[Null,edge[lhs,toVertex@#],HoldAllComplete]/@args],ReleaseHold@args]]
traceToTree[xpr_]:=Block[{vertexNumber=-1,vertexNumberFunction,originalTraceExtract},vertexNumberFunction[arg_]:=vertexNumberFunction[arg]=++vertexNumber;originalTraceExtract[pos_]:=Extract[Unevaluated@xpr,pos,HoldFormComplete];{MapIndexed[traceToTreeAux,Unevaluated@xpr,{0,Infinity}]}]
TraceTreeFormPlot[trace_,opts___]:=Block[{$traceExpressionToTree=True},Through@{Unprotect,Update}@SparseArray`ExpressionToTree;SparseArray`ExpressionToTree[trace,Infinity]=traceToTree@trace;With[{result=ToExpression@ToBoxes@TreeForm[trace,opts]},Through@{Unprotect,Update}@SparseArray`ExpressionToTree;SparseArray`ExpressionToTree[trace,Infinity]=.;Through@{Update,Protect,Update}@SparseArray`ExpressionToTree;result]]


--
http://chris.chiasson.name/

Chris Chiasson

unread,
Jul 31, 2007, 6:14:36 AM7/31/07
to

Hey, google groups wasn't showing this thread a few days ago (or I am
mentally blind, which I would not put past myself). Otherwise, I would
have responded sooner.

If you really want to get to the bottom of this, there are several
tools at your disposal.

1. Contexts[]
Evaluate this command when you start mathematica. Then evaluate it
after your integral. The contexts that have been added (use
Complement) are ones that were loaded especially for your evaluation.

I got:
{Integrate`EllipticTrig`,Integrate`QuickLookUpDump`,Integrate`TableDump`,Integrate`TableDumpExp`,Integrate`TableDumpSpec`,Integrate`TableDumpTrig`,Internal`HypergeometricPFQ`,Internal`Integrate`,System`EllipticDump`,System`FibonacciDump`,System`GroebnerBasisDump`,System`HypergeometricDump`,System`InverseFunctionDump`,System`MeijerGDump`,System`NielsenDump`,System`TrigExpIntegralDump`}

2. Trace and TraceDialog
The contexts used above can be use to select the symbols you might
like to monitor. This is important, because the trace output for the
full evaluation is likely to be more than a typical computer could
handle (and certainly more than could be graphed easily).

3. Stack (with no argument or with a pattern) and StackComplete (wrap
the Trace or TraceDialog with this)
Stack with no arguments gives the tags (symbols associated with
DownValues, UpValues, and OwnValues) that are currently executing.
This is useful in the right hand side of a transformation rule in the
second argument of Trace or as a command issued inside a Dialog
initiated by Trace dialog. If you feed in a pattern instead, it will
match against the expressions that are evaluating and return only
those. StackComplete has the effect of making Stack work on the
sequence of expressions used to arrive at the current expression, not
just the sequence of expressions that are currently evaluating (if
this doesn't make sense, look at the corresponding help browser
entry).

Let's use #s 1 and 2 to get an idea of what is happening in the newly
loaded contexts:

integralTrace=Trace[Integrate[Log[1+Exp[-z]/Sqrt[z]],z],xpr_/;!
FreeQ[Unevaluated@xpr,symb_Symbol/;AtomQ@Unevaluated@symb&&MemberQ[{"Integrate`EllipticTrig`","Integrate`QuickLookUpDump`","Integrate`TableDump`","Integrate`TableDumpExp`","Integrate`TableDumpSpec`","Integrate`TableDumpTrig`","Internal`HypergeometricPFQ`","Internal`Integrate`","System`EllipticDump`","System`FibonacciDump`","System`GroebnerBasisDump`","System`HypergeometricDump`","System`InverseFunctionDump`","System`MeijerGDump`","System`NielsenDump`","System`TrigExpIntegralDump`"},Context@Unevaluated@symb]],TraceInternal-
>True];

With[{gr=traceToTree[DeleteCases[integralTrace,
{HoldForm[_Message],___},Infinity]]},TreePlot[gr,Left,gr[[1,1]],VertexRenderingFunction-
>Function@Tooltip[{Blue,Point@#},#2[[-1]]]]]

Mousing over the nodes in the graph should give you an idea of which
functions you may want to trace and explore further.

Of course, I don't know what is wrong with the answer Integrate is
giving, so I don't know what to look for, but perhaps you do?


Here is a version of the tracing code that changes HoldFormComplete so
that it is stored in a TagBox and that changes the toVertex function
to show more lines of Short output (5 instead of whatever the default
is):

(Attributes@#={HoldAllComplete})&/
@{traceToTreeAux,toVertex,HoldFormComplete}
MakeBoxes[HoldFormComplete[arg_],form_]:=TagBox[MakeBoxes[arg,form],HoldFormComplete]
MakeBoxes[HoldFormComplete[args___],form_]:=TagBox[MakeBoxes[Sequence@args,form],HoldFormComplete]


edge[{head1_,pos1_,xpr1_},
{head2_,pos2_,xpr2_}]:=Quiet[Rule[{head1,vertexNumberFunction@pos1,xpr1},
{head2,vertexNumberFunction@pos2,xpr2}],{Rule::"rhs"}]
toVertex[traceToTreeAux[HoldForm[heldXpr_],pos_]]:=toVertex[heldXpr]
toVertex[traceToTreeAux[HoldForm[heldXprs___],pos_]]:=toVertex@traceToTreeAux[Sequence[],pos]
(*this code is strong enough to not need the ToString commands,but
some of the resulting graph vertices give trouble to the graphing
routines*)

toVertex[traceToTreeAux[xpr_,pos_]]:={ToString[Short[Extract[Unevaluated@xpr,
0,HoldFormComplete],
5],StandardForm],pos,ToString[Short[First@originalTraceExtract@{pos},
5],StandardForm]}

Chris Chiasson

unread,
Aug 26, 2007, 11:40:15 PM8/26/07
to

I just noticed that the combination of transmission over the newsgroup
and pasting back into mathematica screws up this code:
vertexNumberFunction[arg_]:=vertexNumberFunction[arg]=++vertexNumber;

I dunno what to say...


0 new messages