-
Notifications
You must be signed in to change notification settings - Fork 287
Writing your first package
Creating a package is the most common way of contributing to Macaulay2. Packages come in many forms: they can contain code for working with objects of a certain category, help generate examples for testing a conjecture, or can be an implementation of algorithms introduced in the literature. Putting your code into a package makes it easier to share with other people, and also easier to maintain yourself.
- Download the "FirstPackage" package from [LINK]. This is a directory containing a test. Install it on your M2 system using the command
installPackage("FirstPackage"). Look at the help using the commandviewHelp FirstPackage. - Do some sample edits to the package and see what happens.
- Have a look at the source code of some other M2 packages. You can find these on GitHub here. Click on any file ending in
.m2to see the code. - Download the package template from [LINK]. This is a directory containing a text file
PackageName.m2, and a directoryPackageName, which containsDocumentation.m2andTests.m2. These are all text files that you can edit in your favorite editor. ReplacePackageNamein both places by the name of your package (following the convention that every word starts with a capital letter). You are now ready to start writing your package!
The package code has four parts:
- The preamble, which starts with the
newPackage()command and collects the metadata for your package. - The main code of the package.
- The documentation of the package.
- The tests of the package.
The preamble consists of two main commands: newPackage() and export.
Fill in the fields in the version of newPackage provided in the template. Details about the fields can be found in the documentation for newPackage(). For any fields you are unsure about, it is safe to accept the default for now.
You will add the functions that you want your user to be able to access to the export list. This is a list of strings separated by commas, e.g.:
export { "firstFunction", "secondFunction" }
This is the core of your package! See the style guide for conventions when writing M2 code. Try to make your code easy to read by other people and self-documenting - for example, spairCount is a better name for the number of S-pairs in your Gröbner basis calculator than s! When you have written some code, type installPackage "YourPackageName" to install your package. You can then try to run the code.
Edit the Documentation.m2 file to add documentation to your package. Documentation is written in a structured format called "SimpleDoc". The first line is beginDocumentation(), followed by a sequence of "documentation nodes" that start with the word doc. The first (and most important) one is the documentation for the package as a whole. Some advice on how to structure this page can be found here[LINK]. It can be helpful to look again at some examples of real packages (see step 3 of the First Steps).
More information about writing documentation is available here. The documentation for a documentation node is here.
Once you have written some documentation nodes, reinstall your package and type viewHelp YourPackageName. This will bring up a webpage with your documentation displayed in the Macaulay2 format. To reinstall the package, you may first need to run:
uninstallPackage("YourPackageName") installPackage("YourPackageName", RemakeAllDocumentation => true)
Edit the Tests.m2 file to add tests to your package. Make sure that all major mathematical parts of your package are tested, including edge cases. More guidance on how to construct tests is available here[LINK]. To check that the tests all run, type check YourPackageName.
Once your code is working, all tests pass, and the documentation is understandable by someone else, you will hopefully want to share it with other people. To get it distributed with Macaulay2, follow the instructions at [LINK].