One of the most notable features of the Mathematica programming language is its ability to facilitate various programming paradigms. Nevertheless, and despite the recent trend in the scientific computing community of replacing legacy Fortran code with object-oriented (OOP) C++ libraries, there does not currently exist an efficient Mathematica implementation of the principles of OOP. This talk describes a Mathematica package that combines existing features of the Mathematica language (like symbols and contexts) to allow the creation and use of objects in conjunction with “normal” Mathematica code. The package aims to satisfy the following design principles:
The package has already been used in the solution of a fluid mechanics problem with the boundary elements method, as well as in certain lattice quantum field theory calculations. Both applications feature complicated data structures and are very calculation-intensive. The use of the OOP extension has allowed for very rapid implementation of the associated algorithms and testing/debugging of the resulting code. A most helpful side effect of the OOP extension was the smooth transfer of the Mathematica code into C++, by translating method for method. The use of the package is illustrated with the help of a simple object-oriented raytracer, written entirely in Mathematica with the help of the OOP extension.
- Integration: A minimal number of new language constructs was introduced. The package takes heavy advantage of the fact that a symbol is really an object, in the sense that it can store both data (for example myCircle[radius]=1) and behavior (for example myCircle[area]:=π*(myCircle[radius])2). The package also makes extended use of the similarity between contexts and classes, in the sense that both can serve as “code repositories”.
- Flexibility: To ease the introduction of object-oriented code in the existing Mathematica code base, every effort was made to keep the OOP infrastructure as lightweight and transparent as possible. The package code is less than a page long!
- Efficiency: Given the extremely time-consuming nature of most scientific calculations, the OOP extension would be pointless unless the associated overhead was very small. The package achieves this by caching the dynamic dispatch of methods.