Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[mg24308] With[{software=Mathematica}, Frustration]

0 views
Skip to first unread message

Andrzej Kozlowski

unread,
Jul 9, 2000, 3:00:00 AM7/9/00
to

on 00.7.7 1:11 PM, AES at sie...@stanford.edu wrote:

> Pages 359-360 of The Mathematica Book says (admittedly, taken a little
> out of context),
>
> "You can think of With as a generalization of the /. operator. . ."
>
> and
>
> " With[{x=x0}, body] works essentially like body /. x->x0 . . . "
>
> Great, looks neat, let's try it for evaluating expressions without
> permanently setting the variables in them:
>
> In[1]:= c = a b
>
> Out[1]= a b
>
> In[2]:= c
>
> Out[2]= a b
>
> In[10]:= c /. {a -> 2, b -> 3}
>
> Out[10]= 6
>
> In[3]:= With[{a = 2, b = 3}, c]
>
> Out[3]= a b
>
> *Not* what I was hoping for . . .
>
The point here is that With works like Module: it renames the variables (a
way of localizing their meaning). Thus the c outside With and the one inside
are quite different. What you should have compared is

In[13]:=
a*b /. {a -> 2, b -> 3}

Out[13]=
6

In[14]:=
With[{a = 2, b = 3}, a*b]

Out[14]=
6

Unfortunately the way the Mathematica book is written it is not a very good
idea to just read fragments of it, unless you are already pretty familiar
with the basic principles of the programming language. You'd do better to
read some introductory text on Mathematica programming first.


--
Andrzej Kozlowski
Toyama International University, JAPAN

For Mathematica related links and resources try:
<http://www.sstreams.com/Mathematica/>

Maarten.v...@icos.be

unread,
Jul 9, 2000, 3:00:00 AM7/9/00
to
Hi,

Try With[{a = 2, b = 3}, Evaluate[c]].

Maarten

AES <sie...@stanford.edu> on 07-07-2000 06:11:52 AM


Subject: [mg24308] With[{software=Mathematica}, Frustration]

Wolf Hartmut

unread,
Jul 10, 2000, 3:00:00 AM7/10/00
to

> -----Original Message-----
> From: AES [SMTP:sie...@stanford.edu]
> Sent: Friday, July 07, 2000 6:12 AM
> To: math...@smc.vnet.net
> Subject: [mg24308] With[{software=Mathematica}, Frustration]
>
>
[Hartmut Wolf] The Mathematica Book of course is the central reference. For
programming (and deaper understanding) I recommend Roman Maeder's
"Programming in Mathematica"

> Pages 359-360 of The Mathematica Book says (admittedly, taken a little
> out of context),
>
> "You can think of With as a generalization of the /. operator. . ."
>

[Hartmut Wolf] This is exactly that kind of diction in the book I dislike.
However "you can think of..." should have warned you to *respect* the
context!


> and
>
> " With[{x=x0}, body] works essentially like body /. x->x0 . . . "

[Hartmut Wolf] Same here with "essentially"


>
> Great, looks neat, let's try it for evaluating expressions without
> permanently setting the variables in them:
>
> In[1]:= c = a b
>
> Out[1]= a b
>
> In[2]:= c
>
> Out[2]= a b
>
> In[10]:= c /. {a -> 2, b -> 3}
>
> Out[10]= 6
>
> In[3]:= With[{a = 2, b = 3}, c]
>
> Out[3]= a b
>
> *Not* what I was hoping for . . .
>

[Hartmut Wolf]

In[2]:= With[{a = 2, b = 3}, Evaluate[c]]
Out[2]= 6

will do what you want! Why? What was meant saying by the quotations is that
With gives a *textual* substitution for the given variables occuring in the
*unevaluated* body. Here within the unevaluated body (= literal c) a and b
are not visible; you make them so, if you force the evaluation of c before
the substitution is tried.

A warning added: the substitution by With works not the same way as by
ReplaceAll, With respects the scoping of Module, Function, and With (but not
of Block), ReplaceAll doesn't. And for ReplaceAll body is evaluated before
substitution. Admittedly you have to try to understand these points when
proceeding along your learning curve, but it's worth it! After some time
you'll agree to

With[{software=Mathematica}, Exp[much joy!]]


BobH...@aol.com

unread,
Jul 10, 2000, 3:00:00 AM7/10/00
to

In a message dated 7/7/2000 12:44:38 AM, sie...@stanford.edu writes:

>Pages 359-360 of The Mathematica Book says (admittedly, taken a little
>
>out of context),
>
> "You can think of With as a generalization of the /. operator. . ."
>

>and
>
> " With[{x=x0}, body] works essentially like body /. x->x0 . . . "
>

>Great, looks neat, let's try it for evaluating expressions without
>permanently setting the variables in them:
>
> In[1]:= c = a b
>
> Out[1]= a b
>
> In[2]:= c
>
> Out[2]= a b
>
> In[10]:= c /. {a -> 2, b -> 3}
>
> Out[10]= 6
>
> In[3]:= With[{a = 2, b = 3}, c]
>
> Out[3]= a b
>
>*Not* what I was hoping for . . .
>

Attributes[With]

{HoldAll, Protected}

c = a*b;

With[{a = 2, b = 3}, Evaluate[c]]

6


Bob Hanlon

Andrzej Kozlowski

unread,
Jul 12, 2000, 3:00:00 AM7/12/00
to
You are right of course. It was a case of inexplicable blindness (and c
wasn't even a local variable!). This should teach me to think before sending
my replies!

Andrzej

on 7/10/00 11:41 AM, Leszek Sczaniecki at lsc...@home.com wrote:

>> The point here is that With works like Module: it renames the variables (a
>> way of localizing their meaning). Thus the c outside With and the one inside
>> are quite different.

--

Andrzej Kozlowski
Toyama International University

JAPAN

http://platon.c.u-tokyo.ac.jp/andrzej/
http://sigma.tuins.ac.jp/


> I am afraid that you are not quite right here. The true reason for the
> discussed
> behavior is, I believe, that Which has an attribute HoldAll.
>
> Best regards,
>
> --Leszek
>

AES

unread,
Jul 12, 2000, 3:00:00 AM7/12/00
to
In article <vfca5.325$f6.1...@ralph.vnet.net>, Wolf Hartmut
<hw...@debis.com> wrote:

> In[2]:= With[{a = 2, b = 3}, Evaluate[c]]
> Out[2]= 6
>
> will do what you want! Why? What was meant saying by the quotations
> is that With gives a *textual* substitution for the given variables
> occuring in the *unevaluated* body.


Thanks for this reply, and others. But, there is still frustration.

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, like for example

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

just to check what some typical values might be, or to check that the
results are reasonable.

Challenges:

1) Try wrapping the "Print" statement, or the "c" value, or the
"Sin[c]", or various combinations of these, within Evaluate[]'s -- **and
then try predicting in advance, based on your knowledge of Mathematica, what
these different combinations will do**.

2) Find *any* combination of With, Print and Evaluate that will
accomplish the elementary "print a trial result" task that's wanted
above. (I haven't found one yet.).

3) Finally, reconsider your explanation of *textual* substitution.
With[ ] will "textually substitute" within the Evaluated form of c --
but not in the "a =" strings -- which are expressions -- and hence
should be "variables" in a sense.. Try, for example

With[{a=2}, Evaluate[{"a = ",a}]

What gets "textually substituted"? -- what's Mathematica "code"? (see
the Book) -- in this case, and what isn't?


Mark Fisher

unread,
Jul 18, 2000, 3:00:00 AM7/18/00
to
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]]]
]
]

This could be turned into a function:

myPrint[a_, b_] := Evaluate[
StringForm["a = `1`, b = `2`, c = `3`, Sin[c] = `4`", a, b, c, Sin[c]]
]

myPrint[2., 3.]

--Mark.


AES

unread,
Jul 19, 2000, 3:00:00 AM7/19/00
to
In article <8l0ovj$d...@smc.vnet.net>, Mark Fisher
<me.f...@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.


0 new messages