PDF letter size

## How to make a package in Mathematica

July 16, 2010   Compiled on January 1, 2023 at 7:16pm

### Contents

This note describes making a simple package in Mathematica. The ﬁrst section shows how to make a simple package. The second section shows how to make a package with subpackages in them and where to save the ﬁles so that they are easily loaded.

### 1 Simple package

An example package that contains one function is made showing how to save it and load it into Mathematica and to update it again by adding a second function to it.

Each time a new function is added to the package, the package has to be reloaded in order to use the new function.

This note is not meant by any means to be comprehensive, but only to give someone who is new at Mathematica the minimal set of commands to make a basic package.

More details about packages are found in Mathematica help pages.

1. Open a new text ﬁle called foo.m (using any text editor) and write in it the following

BeginPackage["foo"]
Unprotect @@ Names["foo*"];
ClearAll @@ Names["foo*"];

f::usage = "f[x]"
Begin["Private"]

f[x_] := Module[{}, x^2];

End[];
Protect @@ Names["foo*"];
EndPackage[];



In the above foo[x] is a public function which can be accessed from outside.

2. Now save the ﬁle foo.m. Assuming it is saved to C:/mydata/foo.m
3. To use the function f[x] deﬁned in the package, open Mathematica notebook and load the package as follows (this is one easy way to load the package, there are other ways).

Get["c:/mydata/foo.m"]



Now the package is loaded and you can call the function deﬁned inside the package as follows

f[2]



The function can also be called by explicitly pre-appending the package name to the function (which I prefer, as it makes it clear where this function came from) as follows

foof[2]


4. To add a second function, say g[x] to the package, open foo.m again using text editor, and change it to look as follows:

BeginPackage["foo"]
Unprotect @@ Names["foo*"];
ClearAll @@ Names["foo*"];

f::usage = "f[x]"
g::usage = "g[x]"
Begin["Private"]

f[x_] := Module[{}, x^2];
g[x_] := Module[{}, x^4];

End[];
Protect @@ Names["foo*"];
EndPackage[];



Now save the ﬁle foo.m.

5. To use the new function added to the package, the package needs to be reloaded again

Get["c:/mydata/foo.m"]


6. To see which functions in your package type

?foo*



That is all. To add more functions, repeat the above steps.

7. The above functions $$f(x),g(x)$$ are public functions. This is because each of them had a usage statement. We can add private functions that are used internally in the package, which the user can not call or see as follow. Any function that has no usage statement is a private function.

BeginPackage["foo"]
Unprotect @@ Names["foo*"];
ClearAll @@ Names["foo*"];

f::usage = "f[x]"
g::usage = "g[x]"
Begin["Private"]

(*these are public functions *)
f[x_] := Module[{}, h[x]];
g[x_] := Module[{}, x^4];

(*this is private function because it has no usage statement*)
h[x_]:= Module[{},x^2];

End[];
Protect @@ Names["foo*"];
EndPackage[];



I’d like to thank Bill Rowe and Istvan Zachar on the Math newsgroup for helpful suggestions while working on this note.

This package was developed on windows and all the ﬁles will be stored in

FileNameJoin[{$UserBaseDirectory, "Applications"}]  which on windows is located at C:\Users\Owner\AppData\Roaming\Mathematica\Applications the Owner is replaced by your user name. This is done so that the package will be loaded by just doing <<nma  Where nma is our application name that we will make below. This way the application is loaded automatically from anywhere since Mathematica will search that folder automatically. The application name is the same as the top level package name which lives in the ﬁle nma.m. The application name is the name of the folder where we will put the ﬁles in under Applications folder. The package name is nma. So I created a folder called C:\Users\Owner\AppData\Roaming\Mathematica\Applications\nma And put all the m ﬁles there. The structure of the folder nma\ is the following ...Applications\nma\ nma.m dsolver.m firstOrderODE.m secondOrderODE.m kernel\ init.m Logically the application nma is structured as follows. There is one top level package called nma which is always the same name as the application name (i.e. the name of the folder under Application). This package contains 3 packages, these are dsolver,firstOrderODE,secondOrderODE. Each one is in its own ﬁle as shown above. The init.m purpose is to load the whole application when one does <<nma  Inside init.m we will put the code to read all the ﬁles. (*file init.m*) getFile[file_]:=InternalInheritedBlock[{$ContextPath},Get[file]]
getFile["nmafirstOrderODE"];
getFile["nmasecondOrderODE"];
getFile["nmadsolver"];
getFile["nmanma"];



The top level package nma.m is

BeginPackage["nma"] (*do not add ,{"nmadsolver"} *)

Unprotect @@ Names["nma*"];
ClearAll @@ Names["nma*"];

dsolve

Begin["Private"]
dsolve::usage = "dsolve[ode,y[x],x] or dsolve[{ode,ic},y[x],x]"

dsolve[ode_,y_[x_],x_] := Module[{},
nmadsolverdsolve[ode,y[x],x] (*need to have nma there *)
];

End[];
Protect @@ Names["nma*"];
EndPackage[]



Notice it has one entry point, function called dsolve[ode_,y_[x_],x_] which calls one of its internal packages. Here is dsolver.m

BeginPackage["nmadsolver"] (*subpackage of nma *)

Unprotect @@ Names["nmadsolver*"];
ClearAll @@ Names["nmadsolver*"];

dsolve

Begin["Private"]
dsolve::usage = "dsolve[ode,y[x],x]";

dsolve[ode_,y_[x_],x_] := Module[{},
parseODE[ode,y[x],x]
];

parseODE[ode_,y_[x_],x_] := Module[{},
nmafirstOrderODEstep[ode,y[x],x];
nmasecondOrderODEstep[ode,y[x],x]
];

End[];
Protect @@ Names["nmadsolver*"];
EndPackage[]



The above calls functions in the last two packages. Here they are firstOrderODE.m

BeginPackage["nmafirstOrderODE"] (*subpackage of nma *)

Unprotect @@ Names["nmafirstOrderODE*"];
ClearAll @@ Names["nmafirstOrderODE*"];

step

Begin["Private"]

step::usage = "step[ode,y[x],x]";
step[ode_,y_[x_],x_] := Module[{},
Print["in nmafirstOrderODEstep[]"]
];

End[];
Protect @@ Names["nmafirstOrderODE*"];
EndPackage[]



And ﬁnally secondOrderODE.m

BeginPackage["nmasecondOrderODE"] (*subpackage of nma *)

Unprotect @@ Names["nmasecondOrderODE*"];
ClearAll @@ Names["nmasecondOrderODE*"];

step

Begin["Private"]

step::usage = "step[ode,y[x],x]";

step[ode_,y_[x_],x_] := Module[{},
Print["in nmasecondOrderODEstep[]"]
];

End[];
Protect @@ Names["nmasecondOrderODE*"];
EndPackage[]



This completes the template package nma. Now we can add more functions and more packages if needed. Any time we make changes we just need to do

<<nma

`

To reload it. The above command can be done from any notebook without having to set any path, and Mathematica will ﬁnd the application automatically.