MathGroup Archive 2009

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

Search the Archive

Re: Re: Show doesn't work inside Do loop ?

  • To: mathgroup at
  • Subject: [mg102044] Re: [mg102013] Re: Show doesn't work inside Do loop ?
  • From: "David Park" <djmpark at>
  • Date: Mon, 27 Jul 2009 05:57:01 -0400 (EDT)
  • References: <32390795.1248259308283.JavaMail.root@n11> <h4951e$q2e$> <21621663.1248432817472.JavaMail.root@n11> <h4eeul$spv$> <27653945.1248597565355.JavaMail.root@n11>

It's Mathematica 1.01. An evaluated statement produces an Output cell
containing the returned expression. The Null statement represents the lack
of an expression and produces no Output cell. Terminating a statement with
";" results in a Null result and thus produces no Output cell. The Do
statement, BY DESIGN, returns a Null result and thus produces no direct
Output cell. A Show statement by itself returns a Graphics or Graphics3D
expression and thus produces an Output cell. None of the expressions
evaluated within a Do statement produce an Output cell - unless you
deliberately write expressions, such as Print, that produce an Output cell
as a side effect. 

You can write multiple statements in one cell by using a line return before
each successive statement. Each statement will produce an Output cell
provided it is a type of statement that does produce an Output cell. Indeed,
this can be quite useful in writing step by step calculations and seeing
what is happening at each step. One can even add annotation. Here is a small
rather trivial example:

Print["General quadratic equation"]
a x^2 + b x + c == 0
Print["Solve for x"]
Solve[%%, x]
Print["Substitute the values: ", abcrules = {a -> 1, b -> 4, c -> 3}]
%% /. abcrules

This is one place where "%" and "%%" notation can be safely used because
they always refer to output from the same cell. One can easily modify one of
the statements in the cell and reevaluate. This kind of construction can be
useful as a development process for more complicated routines. It is much
less convenient to have the individual statements strung out in separate

A slight disadvantage of the construction is that a single Input cell
produces multiple Output cell and if you want to close up and display the
output you must select all the Output cells and double-click. To obtain the
display in a single Output cell you can use a Column and Row construction.

 {"General quadratic equation",
  (step = a x^2 + b x + c == 0) // TraditionalForm,
  "Solve for x",
  step = Solve[step, x],
  Row[{"Substitute the values: ", 
    abcrules = {a -> 1, b -> 4, c -> 3}}],
  step /. abcrules
  }, Left, 1]

If you wanted to produce a Panel display with two columns, annotation on the
left and results on the right you could use Presentations.


   comment["General quadratic equation."],
   command[(step = a x^2 + b x + c == 0) // TraditionalForm]],
   comment["Solve for x."],
   command[step = Solve[step, x]]],
   comment["Substitute the values: ", 
    abcrules = {a -> 1, b -> 4, c -> 3}, "."],
   command[step /. abcrules]]
 Style["Example of Solving a Quadratic Equation", 16],
 paneWidth -> 640

This also has tooltips on the right hand command results that give the
unevaluated input that produced the result.

So I think what you are doing is mixing up calculation with formatting. You
are trying to make the Do statement do something it was not designed for.
There are plenty of other Mathematica statements and methods to organize
your output displays.

David Park
djmpark at  

From: AES [mailto:siegman at] 

In article <h4eeul$spv$1 at>,
 "David Park" <djmpark at> wrote:  (emphasis added)

> _On the other hand, Show does not generally generate a cell._ 
> Of course, it is easier to just apply Print to the Show or Plot
> So it is possible to make Show generate output cells, but it _normally_
> doesn't do so. It normally only generates expressions, which because of
> special behavior a Do statement does not display.

1)  Re your statements above: executing a single Input cell containing 

   Show[ Graphics[ Circle[ {0, 0}, 1] ] ]

certainly generates an Output cell containing what looks like a 
"graphic" or "plot" to me.

Is executing a cell containing a simple expression somehow an "abnormal" 
process or action?

2)  On a more general note:  Suppose you have an expression which 
contains an explicit symbol x , such that if you execute three 
consecutive cells containing

   x=1; expr
   x=2; expr
   x=3; expr

or maybe


you get three output cells containing three successive instances of expr 
(whatever that is) -- or appropriate error messages if executing expr 
one of those times has some side effect that messes up a subsequent 

Would it not be reasonable to expect a cell containing

   Do[ expr, {x,1,3} ]

to do _exactly_ the same thing?

In other words, would it not be reasonable -- consistent -- sensible -- 
helpful -- the most useful -- to expect Do[ ] to be simply a "wrapper" 
that functioned in exactly that manner?

I appreciate that Mathematica's Do[] apparently doesn't function that 
way -- or functions that way sometimes, based on mysterious criteria, 
but not other times; and suggest that this is not helpful or useful or 
consistent behavior for many users.  

Are there any fundamental reasons why a DoConsistently[ ] command could 
not be defined, such that DoConsistently[ expr, iterator ]  would 
repeatedly put expr into a cell with each iterator instance applied to 
it, and churn out the sequential outputs?  That, it seems to me, is what 
many users would want and expect.

  • Prev by Date: The audience for Mathematica (Was: Re: Show doesn't work inside Do loop ?)
  • Next by Date: Re: How do you take apart a sequence?
  • Previous by thread: Re: Re: Show doesn't work inside Do loop ?
  • Next by thread: Recognizing finite groups in Mathematica 7