Re: graphing traces of complicated evaluations (improved)
- To: mathgroup at smc.vnet.net
- Subject: [mg79517] Re: graphing traces of complicated evaluations (improved)
- From: "Chris Chiasson" <chris at chiasson.name>
- Date: Sat, 28 Jul 2007 05:35:40 -0400 (EDT)
- References: <acbec1a40707191245k5dff3c42j13e8be020e884591@mail.gmail.com>
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/