MathGroup Archive 2000

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

Search the Archive

RE: Re: With[{software=Mathematica}, Frustration]

  • To: mathgroup at smc.vnet.net
  • Subject: [mg24669] RE: [mg24486] Re: [mg24308] With[{software=Mathematica}, Frustration]
  • From: Wolf Hartmut <hwolf at debis.com>
  • Date: Fri, 4 Aug 2000 01:18:56 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com


> -----Original Message-----
> From:	AES [SMTP:siegman at stanford.edu]
To: mathgroup at smc.vnet.net
> Sent:	Wednesday, July 19, 2000 7:22 AM
> To:	mathgroup at smc.vnet.net
> Subject:	[mg24486] Re: [mg24308] With[{software=Mathematica},
> Frustration]
> 
> In article <8l0ovj$dpf at smc.vnet.net>, Mark Fisher 
> <me.fisher at atl.frb.org> wrote:
> 
> > I'm probably not paying enough attention, but what's wrong with the
> > following?
> > 
> > Print[
> >   With[{a = 2., b = 3.}, 
> >     Evaluate[StringForm["a = `1`, b = `2`, c = `3`, Sin[c] = `4`", 
> > 	a, b, c, Sin[c]]]
> >     ]
> >   ]
> 
> ------------------------------
> 
> The problem, to answer the question straightforwardly, is that an 
> ordinary user [1] learns about Print[ ] and uses it lots of places in 
> his notebooks; and then he learns about With[ ] and uses it a few 
> places; and they work, and both seem like reasonably simple, intuitive, 
> useful commands.
> 
> So then he, very reasonably, tries to put the two together by using 
> Print[] inside With[] -- and it doesn't work.
> 
> So, he starts groping around, and after some searching gets introduced 
> to Evaluate[]; and Evaluate[], though it's a somewhat messy and 
> mysterious command, does work -- sometimes, that is -- within With[].  
> 
> But, it doesn't work with Print[] inside With[] to accomplish the rather 
> elementary objective outline above  [2].
> 
> So, after a lot of time-wasting trial and error, a person obviously 
> pretty expert in Mathematica is finally able to accomplish the objective 
> by using StringForm[], which is a really obscure and esoteric command, 
> seldom needed by the ordinary user [3]
> 
> Don't get me wrong:  I'm a Mathematica fan; regard it (along with TeX) 
> as a true work of genius; understand its need to have some real 
> complexity to accomplish all the things it does; and appreciate that 
> many design decisions had to be made in creating it.
> 
> But it's primary target, or one of its primary targets, is supposed to 
> be the "ordinary user" [1]; and for the ordinary user who tries to move 
> even slightly beyond the elementary things that he knows how to use, or 
> who tries to make two apparently elementary things work together (like 
> Wiht[] and Print[]) Mathematica can become *maddeningly* frustrating.
> 
>    --AES
> 
> --------------
> [1]  An ordinary user is someone who just wants to get useful 
> calculations done using Mathematica in his or her own area of expertise, 
> not spend his life becoming an expert in the arcane esoterica that you 
> can get involved in with Mathematica, if you're unlucky or not careful.
> 
> [2]  Challenge: Accomplish the above task using *only* Print[], With[] 
> and Evaluate[] -- no other Mathematica functions.
> 
> [3]  Note that StringForm[] is not discussed at all in The Beginners 
> Guide to Mathematica by Glynn and Gray, and isn't treated except for a 
> passing mention on page 715 of Mathematica for Scientists and Engineers 
> by Thomas Bahder.
> 
[Hartmut Wolf]  

Hello A E (Siegman),

I was on vacation for a while, so now let me respond to your "[2]
challenge":

In[14]:=
With[{a = 2., b = 3.}, 
    Evaluate[Hold[Print]["a = ", a, ", b = ", b, ", c = ", c, ", Sin[c] = ",

        Sin[c]]]] // ReleaseHold

>From In[14]:=
a = 2., b = 3., c = 6., Sin[c] = -0.279415


Why all this Hold and ReleaseHold ?

Well if you try:

In[9]:=
With[{a = 2., b = 3.}, 
  Evaluate[Print["a = ", a, ", b = ", b, ", c = ", c, ", Sin[c] = ",
Sin[c]]]]

>From In[9]:=
a = a, b = b, c = a*b, Sin[c] = Sin[a*b]

then this happens:

(1) Print is executed, giving "From In[9]", no substitution has occurred so
far. The result of this operation is Null.

(2) then With[{a = 2., b = 3.}, Null] is executed, giving Null (which is not
printed)


On the other side if you don't Evaluate then

In[12]:=
With[{a = 2., b = 3.}, 
  Print["a = ", a, ", b = ", b, ", c = ", c, ", Sin[c] = ", Sin[c]]]

>From In[12]:=
a = 2., b = 3., c = a*b, Sin[c] = Sin[a*b] 

Here standalone a and b have been visible to With, and hence substituted
with numerical values, not so as components of c (which has not yet been
evaluated when With did its work).


So what we have to do is to Evaluate the arguments of Print, but not Print
itself: this is suppressed by Hold; now With works on the evaluated
arguments, returns the held Print expression (with evaluated and substituted
arguments) and ReleaseHold then gives way to Printing.

Of course Hold, ReleaseHold is not the only (yet a clear) way to suppress
printing while being exposed to With. Another one would be

In[16]:=
With[{a = 2., b = 3.}, 
    Evaluate[printlater["a = ", a, ", b = ", b, ", c = ", c, ", Sin[c] = ", 
        Sin[c]]]] /. printlater -> Print

>From In[16]:=
a = 2., b = 3., c = 6., Sin[c] = -0.279415

"printlater" is just an undefined symbol.


You can also work within the Print command, directly on the calling sequence
(of Print):

In[23]:=
Print[With[{a = 2., b = 3.}, 
      Evaluate[s["a = ", a, ", b = ", b, ", c = ", c, ", Sin[c] = ", 
          Sin[c]]]] /. s -> Sequence]

>From In[23]:=
a = 2., b = 3., c = 6., Sin[c] = -0.279415

Again here s has no function at all, just to contain the (evaluated)
arguments of Print for the operation of With, thereafter s is made to a
Sequence, such that Print gets its proper arguments. Having understood this
idea, of course you would just Apply Print to s to get the same effect:

In[26]:=
Print @@ With[{a = 2., b = 3.}, 
    Evaluate[s["a = ", a, ", b = ", b, ", c = ", c, ", Sin[c] = ", Sin[c]]]]

>From In[26]:=
a = 2., b = 3., c = 6., Sin[c] = -0.279415


This all can be achieved by simple, pure logical thinking, once you fully
understand the basics. No wizards nor geniuses needed. However, I understand
your point. The way out -- to my opinion -- is not to read books over books
so many, but try to achieve a solid understanding of the basics (these are
about 200 Mathematica functions). Try to use what you know, make use of the
ressources you have (primarily Help and The Mathematica Book), try examples
and learn to cut problems done to the essentials. And finally there is this
friendly and inspiring community.

Kind regards, Hartmut



  • Prev by Date: Re: Indexed variables
  • Next by Date: Resizing graphics to "natural size"?
  • Previous by thread: RE: Equation of a "potato"
  • Next by thread: Re: Re: With[{software=Mathematica}, Frustration]