MathGroup Archive 1995

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

Search the Archive

Re: "?" and "??"

In article <DBKwDG.2I2 at>, Scott.A.Hill at (Lancelot) writes:

>	Also, I'm wondering how I can save several commands as a
>package.  I'm running Mathematica off of a UNIX machine, and I've
>figured out how to save programs to my UNIX account and then call them
>back in as needed.  Perhaps that is the same thing, eh?  Anyway, if
>someone could shed light on how I can define such a file as a
>"package", I'd be interested.

The difference between a package and saving & recalling programs, is that a
package is usually a set of function definitions which extend the capabilities
of Mathematica, where as your program is probably a set of commands that
actually calculate something.  

Loading a package doesn't cause Mathematica to necessarily calculate anything,
it just increases its "knowledge" in a certain area.  When writing a package,
you normally wrap BeginPackage["mycontext`"] and EndPackage[] around your
function definitions.  This causes the active contexts to be only mycontext`
and System`.  This prevents you redefining functions defined in other packages.

I thoroughly recommend that you read "Programming in Mathematica" by Roman
Maeder, Addison Wesley, 1991, ISBN-0-201-54877-1.  This talks in detail about
writing packages, and includes a skeleton package which shows the basic
commands required to write a package.

A simple package would have a structure something like:


	fn1::usage = "This explains how to use fn1, and also introduces
		fn1 outside the Private` context"


	fn1[a_,b_,c_] := Module[{e}, e=a+b; c+e]

	fn2[a_] := Module[{}, Print["This function is in the private
		context & hence can't be called from outside it"]]



Roman's book gives more detail about what happens with the ContextPath, how to
protect symbols, etc.

Time spent learning to write your own packages is invaluable.  It turns
Mathematica from a sophisticated pocket calculator into an powerful, extensible
computational tool.  You can then extend Mathematica to specialise in your own
field of work, although it is worth re-iterating the point that always use
Mathematica's built in functions whenever possible, as these will be
much faster than any package you write.

Hope this helps,

Paul E Howland                         

Long Range Ground Radar Systems Section               tel. +44 (0)1684 895767
LSC2 Division, Room BY209                             fax. +44 (0)1684 896315
Defence Research Agency                           email: PEHOWLAND at DRA.HMG.GB 
Malvern, Worcs, WR14 3PS, UK.                      

  • Prev by Date: Re: compound interest
  • Next by Date: Putting Mathematica Graphics into TeX or LaTeX?
  • Previous by thread: "?" and "??"
  • Next by thread: IMPORT mathscad files to mathematica