Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

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

Search the Archive

Re: Re: Mathematica scoping / contexts / variable localization

  • To: mathgroup at smc.vnet.net
  • Subject: [mg105133] Re: [mg105110] Re: Mathematica scoping / contexts / variable localization
  • From: "David Park" <djmpark at comcast.net>
  • Date: Sun, 22 Nov 2009 06:08:03 -0500 (EST)
  • References: <32142906.1258547432910.JavaMail.root@n11> <he36g8$efk$1@smc.vnet.net> <30783619.1258795562155.JavaMail.root@n11>

Hi Nasser,

Yes rules are a good way to introduce data without setting values to
symbols. This might especially be the case if you are working a problem with
units. You might have something like:

(Problem1Data = {x0 -> 3.25 Meter, v0 -> 5.2 Meter/Second})//Column

then perform symbolic calculations, perhaps using Solve or DSolve, with
equations that contain x0 and v0 and then substitute the data into the
result at the end of the symbolic calculation. Notice that units should be
part of the data and not part of the equations.

result /. Problem1Data

As far a debugging and code development are concerned, I believe most
difficulty occurs by combining too much at once without looking at the
subparts. For me, most routines start out as separate steps in a notebook,
sometimes in one cell and sometimes in separate cells evaluated on a typical
case. I want to see the output and don't use ";" until I'm pretty satisfied
I know what I'm getting. After that, most errors come from failure to
consider special cases.

Only when I've calculated some cases do I try to write a formal definition
and start moving the steps to a Module. There I may only add one step at a
time and repeatedly evaluate to see that any generalizations are working. I
may add Print statements to display intermediate values until I am finished
or I may add them if a problem arises. Sometimes I might set a global temp
variable to some intermediate result, retrieve it from an evaluation, and do
some code development using it as starting data. Mathematica allows one to
do all this simple stuff within a single notebook so why not take advantage
of it? Mathematica is really ideal for this kind of development. Anyway for
me it has worked pretty well and I've never felt the need to study how the
various debuggers work. 

Then I might copy a routine to a notebook Routines section (which I call
Package Purgatory) and add a usage statement, SyntaxInformation and other
ancillary definitions. Then after using it for awhile and if it seems
generally useful I may move it to Package Heaven.


David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/  



From: Nasser M. Abbasi [mailto:nma at 12000.org] 


hi David;

"David Park" <djmpark at comcast.net> wrote in message 
news:he36g8$efk$1 at smc.vnet.net...

>
> Clear[a, b]
>
> ClearAll[f]
> f[a_, b_][x_] := Exp[-a x] Sin[b x]
>
> Plot[f[.5, 3][x], {x, 0, 6}]
>

How about also using the above, but also use /. to assign values to 
parameters.

Plot[f[a, b][x] /. {a -> .5, b -> 3}, {x, 0, 6}]

You are right, I am finding that if one is very aware and disciplined during

programming Mathematica then can side step many common problems, because in 
Mathematica it is more easy to fall into subtle problems for new users than 
in other programming languages. If one is slightly sleepy while programming 
in Mathematica, then many strange problem will show up later on.

I also wish that Mathematica error handling can be improved in the sense 
that when some error shows up in some long running code, that at least it 
would tell me on which _line_ the error happened.  not just some error 
message telling me that I can do this or that.

It seems that, when this happens, I spend more time trying to figure where 
the error is coming from because the error message never tells me where this

error is, even though it must have known. It does not even tell me which 
function it was in.

If there was an easy to use debugger, I could run the code inside it, 
telling it to stop when an error or warning occurs, and this way  I know 
exactly where the error was. But there is no such thing in Mathematica. 
(Please do not tell me to use this so called debugger in Mathematica 7, 
under Evaluation, I tried, and have no idea how it is supposed to work).

I hope this error handling improves in next version of Mathematica, and 
please make the debugger like a _normal_ and standard debugger is supposed 
to be.

May be what we also need is like what David had here, a set of articles that

talks just about good programming styles for Mathematica, or good "patterns"

to follow, targeted to users like most of us, who are not expert Mathematica

programmers. I think this will be very useful.

thanks,
--Nasser 





  • Prev by Date: Re: Freeze Panes in Grid Expression (addendum)
  • Next by Date: Re: Re: Undo in Mathematica
  • Previous by thread: Re: Mathematica scoping / contexts / variable localization
  • Next by thread: Re: Re: Mathematica scoping / contexts / variable localization