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: [mg24721] Re: [mg24486] Re: [mg24308] With[{software=Mathematica}, Frustration]
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Wed, 9 Aug 2000 02:31:19 -0400 (EDT)
  • References: <8mdjv6$51n@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hartmut,

I wonder if using With is the best way to deal with the original problem:

>What I really want is as follows.  I'm entering a bunch of functional
relations
> like c = a b, using direct assignments (=, not :=) and being careful not
to
> give values to the independent variables like a and b as I go along
>
> At various points I'd like to print out numerical test values or  check
values
> of these functions

With Block, in place of With,  we can meet the need to avoid giving global
values to the variables without any problems due to holding:

c = a b;

Block[{a = 2., b = 3., c = a b},
  Print["a = ", a, ", b = ", b, ", c = ", c, ", Sin[c] = ", Sin[c]]]

a = 2., b = 3., c = 6., Sin[c] = -0.279415


And a checking function might be convenient:


chk[va_, vb_] :=
  Block[{a = va, b = vb},
    Print["a = ", va, ", b = ", vb, ", c = ", c, ", Sin[c] = ", Sin[c]]]

chk[2., 3.]

a = 2., b = 3., c = 6., Sin[c] = -0.279415



Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565

"Wolf Hartmut" <hwolf at debis.com> wrote in message
news:8mdjv6$51n at smc.vnet.net...
>
>
> > -----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: [mg24721] [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: Test for On/Off
  • Next by Date: Mathematica newbie
  • Previous by thread: RE: Re: With[{software=Mathematica}, Frustration]
  • Next by thread: Resizing graphics to "natural size"?