Impact factor for posts is a measurement of importance.

Impact factor for users reflect their authority, reputation and contribution on a particular topic.

Rating reflects the quality of posts.

Rating on Voofie is not a simple average of all ratings, but a weighted average of rating, weighted by the impact factor of users who rated.

Explore exciting communities of

MathOO: Adding Python style Object Orientation to Mathematica with MathOO (1.0 beta launch) [Alternative to Objectica]

05 Nov 10

Mathematica is a very useful software in many science fields. It supports procedural programming and functional programming. However, it does not support the modern object oriented programming. If you would like to add that, there is a package called Objectica, and it will cost you quite a fortune ($1000 on 3/11/2010).

Here I have developed a Mathematica package, MathOO, which adds object orientated programming to Mathematica.

Bookmark and Share

Content

Introduction

MathOO is a Mathematica package which adds object orientation programming to Mathematica in Python style.

Features

  • Support multiple inheritance: It uses C3 method of resolution. For details, please refer to: The Python 2.3 Method Resolution Order
  • uses . as resolution operator. You access member functions and attributes like this: class.method[z], object.attribute
  • . operator works as Dot product or resolution operator automatically and correctly
  • Simple syntax
  • very similar to Python
  • High performance: supports ten of thousands of attribute access and class method calls in a second

Download and Install

MathOO is developed using Mathematica 7.0 in Windows. And I checked that it runs in 6.0 as well. It is confirmed that it works on Windows. For Mac, try to install it and leave a comment to let us know if it works or not. For Linux users, MathOO does not support it at this moment.

You can install it using any one of the following two methods:

Method One:

Type the following code into a Mathematica cell and run it. MathOO will be installed automatically by downloading it to a temp directory, extract the files to appropriate place ($UserBaseDirectory/Applications as default).

unzip[zip_, path_] := Block[{fs = Import[zip, "ZIP"], file, sl},
SetDirectory[path];
Do[file = Import[zip, {f, "Binary"}];
CreateDirectory[path <> $PathnameSeparator <> DirectoryName[f]];
Export[f, file, "Binary"];
, {f, fs}];
ResetDirectory[];
]

InstallPackage[url_, installPath_] := Block[{fname, s1},
s1 = Import[url, "Binary"];
fname = CreateDirectory[] <> $PathnameSeparator <> "temp.zip";
Export[fname, s1, "Binary"];
unzip[fname, installPath];
]

InstallPackage["http://www.voofie.com/site_media/uploaded/MathOO-1.0b.zip",
$UserBaseDirectory <> $PathnameSeparator <> "Applications"];

Method Two:

Download this file: MathOO-1.0b.zip and unzip the whole folder to $UserBaseDirectory/Applications.

You can load the package by running:

Needs["MathOO`"]

You may start using it now.

Tutorial

In the following code, an abstract class Animal is derived. (MathOO does not have the concept of abstract class, but you can nevertheless use it by defining function which has to be override in order to be used.) Then another class Cat is derived from it.

Needs["MathOO`"];

Animal.$init$[self_, s_: ""] := (self.name = s)

Animal.speak[self_] :=
Throw["Please inheritance from Animal to derive your own animal."];

Animal.speakN[self_, n_] := Module[{},
Print["Speaking for ", n, " times"];
Do[self.speak[], {n}];
];

NewClass[Cat, Animal];

Cat.speak[self_] := Print[self.name, ": meow"];

cat = new[Cat]["Pinky"];

cat.speak[];

cat.speakN[2];

Now, I will explain it line by line.

Needs["MathOO`"];

This is needed whenever you want to use MathOO in a new Mathematica session.

 

NewClass[Animal];

This define Animal to be a new class.

 

Animal.$init$[self_, s_: ""] := self.name = s;

It is the constructor for the Animal class. It is analogous to the Python constructor: __init__(). Since Mathematica doesn't support underscore, I changed that to $. The self argument is the same as Python's one. Every class method (except the static one) needs to have that. However, the name self isn't mandatory.  s  is defined to be an argument, which is defaulted to be an empty string. It is stored in the object attribute, name.

 

Animal.speak[self_] := 
Throw["Please inheritance from Animal to derive your own animal."];

Animal.speak is defined to be a class method. Therefore it needs the argument self again. However this method throws an error, telling the user to override this method in the child class in order to use it.

 

Animal.speakN[self_, n_] := Module[{},
Print["Speaking for ", n, " times"];
Do[self.speak[], {n}];
];

Animal.speakN executes the function speak for n times.

 

After defining the base class, it's time to derive from it.

NewClass[Cat, Animal];

Define Cat to be a new class, and it is inheritance from the class Animal.

 

Cat.speak[self_] := Print[self.name, ": meow"];

It overrides the base class function speak, so that no error will be thrown. Notice it uses the object attribute self.name, which is defined in the constructor Animal.$init$.

 

Let's try to use the class Cat now!

cat = new[Cat]["Pinky"];

This line generate a new object from the class Cat, and supply the argument "Pinky" to the instructor, and assign it to the variable cat.

 

cat.speak[];

Pinky: meow

We can try to make the cat speak now.

 

And finally:

cat.speakN[2];

Speaking for 2 times

Pinky: meow

Pinky: meow

A more detail tutorial is provided here:

Tutorial On using MathOO

This tutorial teaches how to write a UnitTest class and using it.

Documentation

Please read this: Documentation of MathOO

Features Request

Please go to this discussion post:

Features Request and Improvement suggestions

and discuss what should be added to MathOO.

Bugs Report

Please report your bug in this discussion: MathOO bugs report

License

This is beta 1.0b version. And it will expired on 1/1/2011. You can use MathOO 1.0b freely before the expiration date. But no warranty is provided.

Other News and Announcement

Please check Voofie/MathOO often, or subscript its rss.

3 Comments

hum3

Avatar for hum3
0
11 Apr 11

Dear Ross

I quite liked the idea of this but the license has expired and I am not very happy to add encrypted modules to mathematica

Hum3


Reply

Ross Tang (ross_tang)

Avatar for ross_tang
0
15 Apr 11

The Source Code is added to Google Code

http://code.google.com/p/mathoo-packages/source/browse/#svn%2Ftrunk%2FMathOO-packages

Both source is out here. Feel free to use and please give comment. And I am quite busy recently, and therefore don't have the time to maintain it.

 


Reply

nigelkg

Avatar for nigelkg
0
13 Jan 12

Mathematica 8 MathOO

I am running 8.0 for Mac OS X x86 (64-bit) (October 5, 2011). I am interested in MathOO since I normally program in Python. I am back to Mathematica after a few years away...

 

Needs["MathOO`"]

returns

 

$Failed

 

Do you have any suggestions for debugging the system. Quiting the Kernel and evaluating the notebook MathOO.nb does not give any errors. I would then have expected;

 

 

Animal.$init$[self_, s_: ""] := (self.name = s)

 

not to have returned $failed


Reply

Please login to post comment.

What is Voofie?

Voofie organizes knowledge, discovers useful resources and recognizes knowledgable users.

Bookmark your blog in Voofie to get more traffic as well as building a reputation in your field!

Explore more about it. Become a member—our FREE Registration takes just seconds.

Page Info
13Impacts
0/0 rates
2577
Your Rating:
Version: 7
Last update: 08 Nov 10
History Permalink
Author
Avatar for ross_tang

Ross Tang (ross_tang)

Degree in Physics and Mathematics, Master in Physics
香港

  • Object-oriented programming
  • 0
  • Mathematica
  • 72
  • MathOO
  • 0