MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: record intermediate steps

  • To: mathgroup at smc.vnet.net
  • Subject: [mg73239] Re: record intermediate steps
  • From: "dimitris" <dimmechan at yahoo.com>
  • Date: Thu, 8 Feb 2007 03:43:26 -0500 (EST)
  • References: <acbec1a40702030708s788e8d9eyb42dbca0941a061c@mail.gmail.com>

Hello Chriss,

Thanks a lot for your attempt to explain me the trap method!

Below is a little code which may be interesting for you.
At first sight it looks quite elementary but it does provide you with
many information.

As an example of the code, let consider the (indefinite) integral of 1/
Sqrt[x].

(* keep where messages are sent to *)
old$Messages = $Messages;
(* a bag for collecting the steps *)
bag = {};
(* as a side effect, collect all steps *)
$MessagePrePrint = AppendTo[bag, #]&;
(* redirect messages *)
$Messages = nowhere;
On[];
(* do the integration *)
Integrate[1/Sqrt[x], x];
Off[];
(* restore where messages are sent to *)
$Messages = old$Messages;
$MessagePrePrint = Short;

Then

{Depth[bag], Length[bag], ByteCount[bag], LeafCount[bag],
StringLength[ToString[FullForm[bag]]]}
{8,356,56776,2400,16369}

Take[bag, -10]

{HoldForm[Expand[2*Sqrt[x], Trig -> False]], HoldForm[2*Sqrt[x]],
HoldForm[On[General::meprec]], HoldForm[Null],
  HoldForm[Simplify[2*Sqrt[x], TimeConstraint -> 2, Assumptions ->
True]], HoldForm[2*Sqrt[x]],
  HoldForm[Integrate[1/Sqrt[x], x]], HoldForm[2*Sqrt[x]],
HoldForm[Integrate[1/Sqrt[x], x]; ], HoldForm[Null]}

In version 4.0 we take for the similar integral

{Depth[bag], Length[bag], ByteCount[bag], LeafCount[bag],
StringLength[ToString[FullForm[bag]]]}
{7, 40, 3384, 191, 1325}

Take[bag, -10]
{HoldForm[1/Sqrt[x]], HoldForm[1/Sqrt[x]], HoldForm[1/Sqrt[x]],
HoldForm[1/Sqrt[x]], HoldForm[Integrate[1/Sqrt[x], x]],
  HoldForm[Integrate[1/Sqrt[x], x]], HoldForm[Integrate[1/Sqrt[x],
x]], HoldForm[2*Sqrt[x]], HoldForm[Log[x]/2], HoldForm[Log[x]/2]}


One can see that the attempt of simplification of the result in
version 5.2 and the big difference in LeafCount, ByteCount etc of bag.

Best Regards
Dimitris





=CF/=C7 Chris Chiasson =DD=E3=F1=E1=F8=E5:
> The trap method is pretty straightforward and elegant, at least when
> one isn't trying to use it on all functions at once or limit the
> number of function calls recorded:
>
> The first idea is to take a function name (which is a symbol) and then
> assign a very specific DownValue to it, causing that DownValue to
> "jump the queue" and be executed before the symbol's internal
> DownValues.
>
> The second idea is that the right hand side of this specific DownValue
> then sets the condition to be False, Prints or otherwise records the
> function call, and then executes the built in DownValues by performing
> the same function call again. The condition must be set to false in
> order to avoid infinite tail recursion.
>
> On 2/3/07, dimitris anagnostou <dimmechan at yahoo.com> wrote:
> > Hello Chris,
> >
> > Thanks for your response!
> >
> > It will be very good to suceed in your attempts!
> >
> > I used Robby Villegas' trap method on some examples; very interesting i=
ndeed
> > but I doubt if I will ever can understand it completely or write it dow=
n on
> > my own!
> >
> > Doing my first steps on this area of Mathematica (figuring out what is
> > going/called etc) I tried to use something like
> >
> > On[];
> > FullSimplify[Cos[2*(Pi/7)]*Cos[4*(Pi/7)]*Cos[8*(Pi/7)]]
> > Off[];
> >
> > But the process during the (Full)Simplification are too "internal" to be
> > "reported" by this elementary setting.
> >
> > Best Regards
> > Dimitris
> >
> >
> >
> >
> >
> > Chris Chiasson <chris at chiasson.name> wrote:
> > Dimitris,
> > I tried using Robby Villegas' trap method in an automated fashion on
> > most of the functions in the System` context to see if I could figure
> > out what is being called. Unfortunately, it breaks FullSimplify and
> > doesn't reveal what functions were called. However, I am not yet ready
> > to give up on this method.
> >
> > Also, it is possible to get 1/8 by using
> > RootReduce@TrigFactor@tr
> >
> > Anyway, here is the automated trapping code:
> > In[1]:=
> > nameTrapBin={};
> > In[2]:=
> > nameTrap[symb_Symbol]/;FreeQ[Attributes@symb,Locked]:=
> > Module[{trap=True},Unprotect@Unevaluated@symb;
> > g_symb/;trap:=
> > Block[{trap=False},
> > If[nameTrapCount>0,nameTrapCount--;
> > nameTrapBin={nameTrapBin,HoldForm@g}];g]]
> > In[3]:=
> > nameTrap[str_String]:=ToExpression[str,InputForm,nameTrap]
> > In[4]:=
> > nameSet=DeleteCases[Names["System`*"],
> > Alternatives@@
> > Union[Join[
> > ToString/@
> > Cases[DownValues@nameTrap,_Symbol,{0,Infinity},
> > Heads\[Rule]True],Names["System`*Packet*"],
> > Names["System`*Box*"],Names["System`*Abort*"],
> > Names["System`*Trace*"],Names["System`*Dialog*"],
> > Names["System`*Message*"],Names["System`*$*"],
> > Names["System`*Link*"],Names["System`*Set*"],
> > Names["System`*Message*"],{"Apply"}]]];
> > In[5]:=
> > ((*Print@#;*)nameTrap@#)&/@nameSet;
> > In[6]:=
> > Block[{nameTrapCount=10},tr=Cos[2*Pi/7]*Cos[4*Pi/7]*Cos[8*Pi/7]]
> > In[7]:=
> > FullSimplify@tr
> > In[8]:=
> > Block[{name TrapCount=10},BetaRegularized[1,2,3]]
> > In[9]:=
> > Flatten@nameTrapBin
> >
> >
> > On 2/3/07, dimitris wrote:
> > > I know that Mathematica's implementated algorithms in most cases (for
> > > e.g. indefinite integration) do not follow the "human way" (e.g.
> > > integration by parts, substitution etc).
> > >
> > > But sometimes it is quite interesting to "record on the side" the
> > > intermediate tranformations
> > > rules followed in the course of arriving in the result.
> > >
> > > So, consider the following expression:
> > >
> > > In[6]:=
> > > tr = Cos[2*Pi/7]*Cos[4*Pi/7]*Cos[8*Pi/7]
> > >
> > > Out[6]=
> > > Cos[(2*Pi)/7]*Cos[(4*Pi)/7]*Cos[(8*Pi)/7]
> > >
> > > It is very easy to show that tr is actually equal to 1/8.
> > >
> > > In Mathematica you can demonstrate this with the command
> > >
> > > In[7]:=
> > > FullSimplify[tr]
> > >
> > > Out[7]=
> > > 1/8
> > >
> > > I believe (but I am not sure!) that Mathematica more or less in this
> > > example follow the "human way" of applying the transformation rules.
> > >
> > > So, I would like to see/know them (i.e. the transformation rules)
> > > applied by mathematica to reach this result and further record on the
> > > side (regardless if they actually have any resemblence with the way a
> > > human will work in this example!).
> > >
> > > I personally tried
> > >
> > > In[8]:=
> > > Trace[FullSimplify[tr], TraceInternal -> True]
> > >
> > > but this is not the case here!
> > >
> > > Thanks in advance for any kind of response.
> > >
> > > Dimitris
> > >
> > >
> >
> >
> > --
> > http://chris.chiasson.name/
> >
> >
> >
> >
> >  ________________________________
> > We won't tell. Get more on shows you hate to love
> > (and love to hate): Yahoo! TV's Guilty Pleasures list.
> >
> >
>
>
> --
> http://chris.chiasson.name/



  • Prev by Date: Re: TextListPlot with colored texts
  • Next by Date: Re: List manipulation
  • Previous by thread: Re: record intermediate steps
  • Next by thread: Re: record intermediate steps