Is there a way to show the details of a numerical computation?

7

2

I have recently started using Mathematica as a calculator while teaching (definitely overkill, but I try to use as few tools as possible). I would like my students to be able to trace simple numerical calculations, for example if I enter

a=1; b=2; c=3; x = 1 / (  (a+2b)/c^2 )

I would like to automagically generate

x = 1 / ((1 + 2 . 2)/3^2)

and even if I could

x = 1 / ((1 + 2 . 2)/3^2) = 1/ (5/9) = ...

Is there a way to do this?

Addition :

I found out that this seems to do a rather good job :

a = 1; b = 2; c = 3;
ClearAll["Global`*"]
a = "1"; b = "2"; c = "3";
x = 1/((a + 2 b)/c^2)

Now I need to automate it. Any suggestion ?!

A.G.

Posted 2013-04-23T01:11:47.530

Reputation: 3 639

Try ... Trace. – Spawn1701D – 2013-04-23T01:13:27.457

Answers

10

Here's one idea. Hold the expression unevaluated and go up the expression tree from (near) the bottom, level by level, and evaluate.

expr = HoldForm[1/((a + 2 b)/c^2)] /. {a -> 1, b -> 2, c -> 3}

Initial expresion

out = ToExpression@ToString[FullForm@#] & /@
       (ReplacePart[expr, # -> Extract[expr, #] & /@ #] & /@ 
         GatherBy[Position[expr, _, -2], Length])

Steps

Second step is a bit strange, for the purpose of teaching. I'm not sure at this point how to be more sophisticated auto-magically.

Michael E2

Posted 2013-04-23T01:11:47.530

Reputation: 190 928

4

Independently I arrived at something similar to Michael's answer, yet different. I borrowed his formatting function after seeing it as it works better than what I had. Perhaps this will also be of use:

evalFromBottom[expr_, lv_: 1] :=
 If[lv > Depth@expr, expr,
  With[{ev = Replace[expr, x_ :> RuleCondition[x], {-lv}]},
   If[expr === ev, evalFromBottom[expr, lv + 1], ev] ] ]

Now:

a = 1; b = 2; c = 3; x = HoldForm[1/((a + 2 b)/c^2)];

ToExpression @ ToString @ FullForm @ # & @@@
  GatherBy[FixedPointList[evalFromBottom, x], ToString]

enter image description here

Mr.Wizard

Posted 2013-04-23T01:11:47.530

Reputation: 259 163

+1 This is nice, too. It seems there is always an odd step (step[[-3;;-2]] :) – Michael E2 – 2013-04-25T20:08:45.833

I never fully read your 1 step evaluation function (although it's been on my list for quite a while), but would that be useful here? – rm -rf – 2013-04-25T23:46:14.397

@rm-rf I hope you do, I think it's some of my best work. However, as written it would not be applicable here because it specifically consider "one step" to be the first operation in Trace that transforms (replaces) the entire expression. Also, if you look at Trace output directly I don't think the order of evaluations is necessarily the most natural; for example I think values for a, b, c should be substituted first, but a + 2 b is evaluated to 5 before c is touched. – Mr.Wizard – 2013-04-26T01:40:10.450

Ah, ok. That answer already has my vote on it (for the effort and application), but the reason I haven't read it in detail is because I don't fully understand Trace and friends and I haven't spent much time with them. I had started reading the chapter on tracing in Wagner's book and found it extremely readable and hope to be able to sit down with it and mma and carefully figure things out... – rm -rf – 2013-04-26T01:49:34.900