case western reserve university  
Sun. Nov 22 2009
 
Quick links:


Using Mathematica Packages

In Mathematica, a Package is a Mathematica text file that contains definitions of commands other than the ones that are built into the Mathematica application itself. In fact, Mathematica comes with a number of Packages; some of these are loaded when Mathematica starts, while others are not loaded at start-up. If commands defined in some package are needed but have not been loaded, then you need to see that they get loaded. Basically there are two ways of doing this:

Method 2: The Get command

The basic syntax for the Get command is

Get[your_package_name.m]

where your_package_name.m is the name of the file package (generally packages are given the suffix .m, but this is not absolutely necessary).

Another way of entering the Get command is with a double less-than sign:

<< your_package_name.m

One of two things happens when this command is issued; either:

Here is why this may happen. When Mathematica starts up, a list of directories is created where Mathematica will automatically search for packages requested by the Get command. If the package you want is in one of these directories, the Get command as described above will work. For instance, the Packages that come with Mathematica are on the CWRU software library, and will be found if you run Mathematica from the software library server. If you correctly install Mathematica on your own computer, these packages will also be found if you run Mathematica on your own machine.

Packages that do not come with Mathematica are a bit more trouble to work with, because they may not be located in a directory that Mathematica will automatically search through. There are four ways of fixing this:

  1. Evaluate the package by hand (i.e., use the Open command in the File menu, as described above as Method 1). This can be tedious if you regularly need to use some package or packages.
  2. Tell Mathematica explicitly where to find the package each time you want to read it in.
  3. Move the package into one of the directories that Mathematica searches.
  4. Expand the list of directories to search so that it includes the directory containing the package.
2. Explicitly locating the package file for Mathematica
To do this, execute the command

Get[your_package_name.m, Path->\{path_to_directory_containing_it}]

where your_package_name.m is the name of the file package, and

path_to_directory_containing_it is the directory path to the directory that contains the file package. For example,

Get[dirField.m, Path->\{"Macintosh HD:Desktop Folder:Mma folder:finished"}]

would load the code in the file dirField.m that is located 4 directories deep (on a Macintosh the colon ":" separates names of subdirectories). The syntax is slightly different on a Windows machine, where single slashes - "/" - would replace the colons.

3. Putting your package in the default search path
To find the default search path, start Mathematica and execute the following command:

$Path

In response, Mathematica lists the directories that will be searched. Thus if you place your new package into one of these directories (usually the directory path ending with "AddOns:ExtraPackages" is a reasonable choice), you can use the Get command mentioned above without the Path->\{...} specification. For example,

Get[dirField.m]

would load the code in the file dirField.m (assuming that it has been copied into one of the default search directories). You can also use "<<" as an abbreviation for the Get command, as in

<<dirField.m

Hint: Running Mathematica off the Software Library.

If you are running Mathematica off the Software Library, then and you will not have write permission for most of the directories listed by $Path. Normally, at least one of the directory names returned by $Path will lie on the local hard drive (the actual directory may or may not exist). In this case, make sure the local directory exists (if not, create it), and keep your local packages in it. If none of the directories returned by $Path is local, then you will need to use one of the other methods to load local packages.
4. Expanding the default search path
The fourth solution is to redefine $Path to include the directory containing the new package. It should be possible to add this new directory-path to the default list of paths that Mathematica loads at the begining of each session but I don't know how to do this. However, the commands below do give a way of changing the search list one a "one-time" basis. The relevant command to execute is

$Path=Join[$Path, \{"path_to_new_directory"\}];

For example,

$Path=Join[$Path, \{"Macintosh HD:Desktop Folder:Mma folder:finished"\}];

appends the path "Macintosh HD:Desktop Folder:Mma folder:finished" to the current search list. (Give the full directory path; on a Windows machine use "/" rather than ":" to separate directories.)

Once we have arranged things so that Mathematica will find the package, it is time to tell Mathematica to load it. To load a package named "dirField.m", we use the "Get" command (the double 'less-than' sign is the abbreviation for Get):

<<dirField.m

Mathematica should now load the Package into memory, and you are ready to use any commands defined in the Package.

Hint

The "Get File Path..." command under the Input Menu can simplify the use of the Join[] command as just described.

Type

$Path=Join[$Path, \{

and leave the cursor at the end of the line, after the \{.

Next, execute the "Get File Path..." command from the Input Menu; an "Open File" dialog box is displayed; in it, navigate to the package you want, then click on "Open". The file path to the package will then be pasted into your current notebook at the location of the cursor (i.e., right after the \{). Unfortunately, slightly too much gets pasted in; delete everying starting from the last directory separator to the end of the file name (the directory separator is a colon on a Macintosh and a slash on Windows. Finally, type the closing curly bracket and the closing square bracket to complete the command, which you can now execute. (In the example above, the "Get File Path..." command would have returned

"Macintosh HD:Desktop Folder:Mma folder:finished:dirField.m"

and we would shorten to

"Macintosh HD:Desktop Folder:Mma folder:finished"

into the Join command.)

Remark I have been told that with some versions of Mathematica for some operating systems, a Get command will find packages that are in the same directory as the notebook that contains the Get command. This does not work in all implementions of Mathematica, but it is something you might try.


This site is maintained by Mike Hurley; this page was last updated on 10.26.2009, at 13:01 .

Legal Information | © 2009 Case Western Reserve University | Contact Prof. Hurley
..