case western reserve university  
Mike Hurley's pages Tue. Apr 23 2024
 

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, manually opening the appropriate files and evaluating the package commands, and using the Get command.

Method 1: Manually opening the packages

This is the simpler method to understand, but can be more work to implement. All you have to do is use the Open menu item in the File menu, select the desired package to open it, and once it is open, evaluate its commands by hand. Normally the commands provided by a package are put into special cells call "initialization cells", depending upon your Preference settings, Mathematica may offer to evaluate these as soon as you open the file; in this case all you need to do is open the file, using the "Open..." command in the File menu, and click "Okay" when the dialog box asking about evaluating initialization cells is displayed. If you do not get such a dialog box, you can use the "Evaluate Initialization" command in the "Evaluation" sub-menu of the "Kernel" menu.

At this point you should be able to use these commands in your current Mathematica session. You do not need to (and probably should not even try) to do your calculations in the package file, as you probably do not want to risk ruining it by making changes. In fact you can close the package file, open a new notebook file, and do all your work there.


Method 2: The Get command

There are two slightly different versions of the Get command. The difference is that one expects to input from a "stream" and the other expects to input from a file. The first version looks like

GettheStream]

and the other a double less-than sign inplace of "Get[ ]". It looks like

<< theFileName

Here we will mostly explain how to use the double less-than sign notation, rather than getting into a discussion of streams. (However it will still be referred to as the "Get command", but without the square brackets.) Thus to make use of a package in a file called your_package_name.m, the command would be

<< your_package_name.m

One of two things happens when this command is issued. If Mathematica finds the file in question, then it should read the file and evaluate the commands inside it. Then you can make use of these commands in your work, as outlined at the end of Method 1.

On the other hand, it may happen that Mathematica fails to find the file and so returns an error message.

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 are installed in locations that (by default) are automatically searched by Mathematica.

Packages that do not come with Mathematica can 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:

Explicitly locating the package file for Mathematica

To do this, execute the command with the full file path to the package; the file path should be quoted. For example,

<< "/Users/me/some_folder_containing_the_package/dirField.m"

would load the code in the file dirField.m that is located 3 directories deep in the file system.

(If you are using the full file path, then you can use instead the "Get[ ]" version of the command, for example

Get["/Users/me/some_folder_containing_the_package/dirField.m"]

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. You can place your new package into one of these directories (usually there is a directory path ending with "AutoLoad" somewhere in your personal user space - as opposed to the system files directories - and this is a reasonable choice). Another reasonable choice is the same directory as the notebook you are working in to access the package, at least on some systems.

Once the package is in one of the search directories, you can use the Get command mentioned above without the full file path. For example,

<<dirField.m

would load the code in the file dirField.m (assuming that it has been copied into one of the default search directories). (Here you shold use "<<" version of the Get command, not the Get[ ] version.)

Expanding the default search path

Another approach 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"}];

The Join command is essentially creating the union of two lists, so the new directory path must be enclosed in braces, representing a list.

For example,

$Path=Join[$Path, {"Users/me/Mma folder/my packages"}];

appends the path "Users/me/Mma folder/my packages"" to the current search list. (Give the full directory path.)

Now the simple Get command should work, as in

<<dirField.m

Hint

The "File Path..." command under the Insert 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 "File Path..." command from the Insert 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 (usually a slash) to the end of the file name. (In the example above, the "File Path..." command would have returned something like

"Users/me/Mma folder/my packages/dirField.m"

and we would shorten it to

"Users/me/Mma folder/my packages"

inside the "Join[$Path, {..." command.)


This site is maintained by Mike Hurley; this page was last updated on 5/31/19, 12:57 PM .

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