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

showing your work in mathematica

5 views
Skip to first unread message

Christopher Arthur

unread,
Sep 23, 2006, 11:58:15 PM9/23/06
to
Hi,

Has anyone ever tried using a form of Trace[] around a call to Simplify[]?

It would be neat if we could set it up in such a way so that we get a
list of all the transformations that lead to the final solution.


Chris Chiasson

unread,
Sep 25, 2006, 3:52:39 AM9/25/06
to
Andrzej Kozlowski once posted a solution to this that involved
changing the ComplexityFunction option so that it printed its argument
before returning the LeafCount of the expression. Search the archives.


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

dimm...@yahoo.com

unread,
Sep 25, 2006, 3:55:47 AM9/25/06
to

I do not know if this is relevant to you but here is one link:

http://library.wolfram.com/infocenter/MathSource/678/

Anyway Trace has some undocumentated in the Help Browser
options that might be interesting for you.

Execute tha following commands
(I have converted everything to Inputform)

Options[Trace]
(Information[Evaluate[#1[[1]]]] & ) /@ %;

TraceInternal is one of them

Information[TraceInternal]
"TraceInternal is an option for Trace and related functions which, if
True or False, specifies whether to trace evaluations of expressions
generated internally by Mathematica. The intermediate Automatic setting
traces a selected set of internal
evaluations including Messages and sets or unsets of visible symbols."
Attributes[TraceInternal] = {Protected}

For example

Simplify[x^2 + 2*x + 1]
(1 + x)^2In[43]:=

Trace[Simplify[x^2 + 2*x + 1], TraceInternal -> True]
{{HoldForm[x^2 + 2*x + 1], HoldForm[1 + 2*x + x^2]},
HoldForm[Simplify[1 + 2*x + x^2]], {HoldForm[$Assumptions],
HoldForm[True]}, {{HoldForm[N[300]], HoldForm[300.]},
HoldForm[Floor[300.]], HoldForm[300]},
{{{{HoldForm[N[300]], HoldForm[300.]}, HoldForm[Mod[300., 1]],
HoldForm[0.]}, HoldForm[1000000*0.], HoldForm[0.]},
HoldForm[Ceiling[0.]], HoldForm[0]}, {HoldForm[General::meprec],
HoldForm["Internal precision limit $MaxExtraPrecision = `1` reached
while evaluating `2`."]},
{HoldForm[Off[General::meprec]], {HoldForm[General::meprec],
HoldForm["Internal precision limit $MaxExtraPrecision = `1` reached
while evaluating `2`."]}, HoldForm[Null]},
{HoldForm[On[General::meprec]], {HoldForm[General::meprec],
HoldForm[$Off["Internal precision limit $MaxExtraPrecision = `1`
reached while evaluating `2`."]]}, HoldForm[Null]},
HoldForm[(1 + x)^2]}

Execute

Trace[FullSimplify[x^2 + 2*x + 1], TraceInternal -> True]

to see the difference.


Regards
Dimitris

David Park

unread,
Sep 25, 2006, 3:57:54 AM9/25/06
to
Christopher,

It is possible, and very desirable, to show your work in Mathematica. But
Trace is probably not the best method for doing so. (Trace is sometimes
useful for debugging.)

The best way to show your work, or to present material in a didactic fashion
is to:

1) Use the notebook Section and Subsection organization

2) When you want more detail than the regular Mathematica Simplify, or you
need special rules, then define the transformation rules and give them names
(and maybe even parameters). If you have derived an equation in one part of
your notebook you can change it into a rule to use in further derivations.

3) Use Text cells to explain what you are doing. Explaining and doing is
better than just doing.

4) You can usually do a number of derivation steps in one cell. Within one
cell you can safely use the '%' and '%%' reference system. You can keep
modifying and reevaluating such cells until you are happy with the result.

5) Within a single cell you can intersperse Print statements to annotate the
steps. The Print statement allows you to mix text and expressions (such as
the transformation rule used) so you can make fairly elaborate annotation.

In a good notebook, if you should evaluate and then close all the Input
cells (Alt-CRC) then the notebook should read like a research paper or
textbook. You won't usually really want to close all the Input cells but I'm
just suggesting that the text and output should be able to stand on their
own.

Here is a small example. It comes from answering the question posted today
by Guillermo Sanchez.

"I have a pair of elements list. The second elements of the list should
be summed when the first element of the pairs are equals."

testlist = {{a1, b1}, {a2, b3}, {a2, b4}, {a3, b5}, {a3, b6}, {a2, b2}};

Print["The starting list"]
testlist
Print["Sort the list"]
Sort[testlist]
Print["Split the list on equality in the first element"]
Split[%%, First[#1] === First[#2] &]
Print["Sum the second elements in each group using the following function:
", \
{Part[#, 1, 1], Last@Total[#]} &]
{Part[#, 1, 1], Last@Total[#]} & /@ %%


David Park
dj...@earthlink.net
http://home.earthlink.net/~djmp/

dimm...@yahoo.com

unread,
Sep 26, 2006, 1:03:08 AM9/26/06
to

Hi David.

Following the solution you gave to Guillermo Sanchez

(http://groups.google.gr/group/comp.soft-sys.math.mathematica/browse_thread/thread/
95091e339ee5ab30/2d17c6a8eb01882e?hl=el#2d17c6a8eb01882e)

I defined the following one-liner

funFunc[(dat_)?ListQ] := ({#1[[1,1]], Last[Total[#1]]} & ) /@
Split[Sort[dat], First[#1] === First[#2] & ]

Here is one test list

lst={{a1, b1},{a2,b2},{a2,b3},{a2,b4},{a3, b5}, {a3, b6}} ;

And here is the (desired) result

funFunc[lst]
{{a1, b1}, {a2, b2 + b3 + b4}, {a3, b5 + b6}}

Is is possiple to insert these Print statements inside
the definition of the funFunc function so that everytime
we execute funFunc to see and (some of) the intermiadate
steps?

As you said Trace and its variations with options is proper for
debbuging. E.g.

Trace[funFunc[lst]]

Trace[funFunc[lst], Sort]

Trace[funFunc[lst], Split]

Trace[funFunc[lst], SameQ]

Trace[funFunc[lst], Total]

Of course someone will suggest TracePrint e.g.

TracePrint[funFunc[lst], SameQ]HoldForm[SameQ]

but I think it is better to define the function funFunc in a proper
way.

Anyway, I guess something like the following is not very welcome.

funFunc[(dat_)?ListQ] := (Print[StringJoin["the starting list:\n",
ToString[dat]]];
Print[StringJoin["sorting of list:\n", ToString[Sort[dat]]]];
Print[StringJoin["splitting of list on equality in the first
element:\n",
ToString[Split[Sort[dat], First[#1] === First[#2] & ]]]];
Print["final reasult===>sum of the second elements in each group: "];
({#1[[1,1]], Last[Total[#1]]} & ) /@
Split[Sort[dat], First[#1] === First[#2] & ])

funFunc[lst]
the starting list:
{{a1, b1}, {a2, b2}, {a2, b3}, {a2, b4}, {a3, b5}, {a3, b6}}
sorting of list:
{{a1, b1}, {a2, b2}, {a2, b3}, {a2, b4}, {a3, b5}, {a3, b6}}
splitting of list on equality in the first element:
{{{a1, b1}}, {{a2, b2}, {a2, b3}, {a2, b4}}, {{a3, b5}, {a3, b6}}}
final reasult===>sum of the second elements in each group:
{{a1,b1},{a2,b2+b3+b4},{a3,b5+b6}}

By the way, everything here is in InputForm apart from
the last Output. When I convert it to InputForm it loses
its format so I left it in StandardForm. (Why?)


Regards
Dimitris

0 new messages