Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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 ?!

share|improve this question
    
Try ... Trace. –  Spawn1701D Apr 23 '13 at 1:13

2 Answers 2

up vote 10 down vote accepted

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.

share|improve this answer

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

share|improve this answer
    
+1 This is nice, too. It seems there is always an odd step (step[[-3;;-2]] :) –  Michael E2 Apr 25 '13 at 20:08
    
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 Apr 25 '13 at 23:46
    
@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 Apr 26 '13 at 1:40
    
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 Apr 26 '13 at 1:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.