Programming in Mathematica using object based paradigm

by Nasser M. Abbasi
nma@12000.org
V 1.2, december 23, 2012

This note is here in PDF format
The Mathematica notebook is here

Introduction

Mathematica can be effectively used for object based programming. It is well known that using object based programming helps in managing the complexity of large programs. Using Object based programming in Mathematica can lead to the best of both worlds: object based combined with functional programming. Object based can be used to help in organizing the program in the large and functional programming is used in the actual implementation of the Classes methods.

The idea is simple. A Module acts as what is the Class in standard OO languages. Inside this module will be additional inner Modules. These inner Modules act as the Class methods. Inner Modules can be made public or private.

By adding the name of an inner Module in the list of the local variables of the outer Module, the inner Module becomes private and is seen and only be called from other inner Modules.

The outer Module local variables are the Class private variables and these variables can be accessed only by the Class inner modules.

An object is first created as an instance of the outer Module and from then on this object can be used in the same way as an object is used standard OO by using the notation object@method[parameters] where the @ here acts as the dot "." acts in common OO languages.

In other words, the dot is replaced by @ and almost everything else remain the same. This makes the notation easier to use for someone who is more familiar with common OO notations.

The following example illustrates this idea where a Class that represents a second order system is defined and used to make an instance of a second order system (such as a spring-mass-damper) and the object methods are used for some basic control system operations to illustrate how to use it.

The Class is called plantClass (which is the outer Module name). To create a specific instance of this Class, the constructor is first called using plant=plantClass[parameters]

The following diagram summarizes this setup, followed by the Mathematica code itself Code implementation of the Class

In the following, the complete code of the Class and example of using it are given. A plant is created, then the step response is plotted, then a Manipulate is made that uses this Class where the plant’s mass, damping and stiffness are used as Manipulate sliders control variables to be changed and the plant step response is updated each time.

Class code Create an instance of the class Now the object is created, use it to make a step response plot  Change the plant damping ratio and update the response plot  Making a Manipulate to use the above Class to simulate plant response to different parameters

The above Class is now used inside Manipulate. It is important that the object instantiation occur in the Manipulate Initialization section, and after the Class code and not before it. In this example, the object is the second order plant, and one instance is created in the Manipulate Initialization section. Each time a slider changes, the object internal state is updated using a setter method. Here is a diagram to help illustrate the layout Manipulate code using object based layout Conclusion

It is well known that object based programming help to improve the design of software and managing the complexity of large applications. Mathematica can be used effectively as object based and combined with functional programming, which leads to better overall software. I have used this setup for first time in an actual demonstration for the simulation of control system successfully, and I have found that it helped better organize my demonstration code and shortened the development time. The demonstration can be seen here

References