Message Boards Message Boards

0
|
32198 Views
|
17 Replies
|
11 Total Likes
View groups...
Share
Share this post:

Can we do object oriented programming in Mathematica

Posted 10 years ago

Say, I would like to create a class of objects having certain kinds of data and functions that can work on the data belonging to the object. Is there a way to implement such a programing paradigm in Mathematica?

17 Replies

Hello, I am progressing slowly in this direction. I confirm that the Wolfram language already has a standard object model: Options. I am wondering what kind of relationship between objects the FilterRules (formerly FilterOptions) system installs . I think it is heritage.

POSTED BY: Pierre Albarede

Maeder's paper is still up to date after 27 years.

An example of object oriented programming in Mathematica is notebook programming.

Notebook attributes can be edited with the option inspector.

Notebook style follows a class organization. Instancing works by choosing style, in front end menu Format > Edit style sheet. Subclass creation works by editing a style sheet.

Before associations, options are already a good support of object oriented programming. They are extensively used for graphics objects.

The problem of OOP in Mathematica is some sort of contradiction with functional programming. In functional programming, we don't like hidden or private states and side effects but they are necessary for graphics and useful for notebooks, because our generally slow brains perceive those illusive objects so fast.

Lisp is another functional language that has developed an object system, see Common Lisp Object System on Wikipedia. At first sight, it looks familiar, like pattern matching. The same path in Mathematica seems possible.

POSTED BY: Pierre Albarede

The answer to this is to use Object-Oriented Design Patterns in Mathematica as explained and exemplified in the blog post "Object-Oriented Design Patterns in Mathematica".

There is a related presentation "Object Oriented Design Patterns" at the Wolfram Technology Conference 2015. (The presentation recording is also uploaded at YouTube.)

Here is a link to a PDF document describing how to implement OOP Design Patterns in Mathematica:

"Implementation of Object-Oriented Programming Design Patterns in Mathematica"

The described approach does not require the use of preliminary implementations, packages, or extra code.

Design Patterns brought OOP into maturity. Design Patterns help overcome limitations of programming languages, give higher level abstractions for program design, and provide design transformation guidance. Because of this it is much better to emulate OOP in Mathematica through Design Patterns than through emulation of OOP objects. (The latter is done in all other approaches and projects I have seen.)

Related posts/descriptions/answers

  1. Blog post "Object-Oriented Design Patterns in Mathematica".

  2. Blog post "UML diagrams creation and generation", available also on Community.

  3. This answer in the MSE discussion General strategies to write big code in Mathematica?.

  4. This answer in the MSE discussion Can one identify the design patterns of Mathematica?.

  5. This answer to the MSE question "Import and Plot Git Commit history" that has two implementations, one functional and one object oriented.

POSTED BY: Anton Antonov

I've published recently a package that allows to do object oriented programming in Mathematica 10.

You can find it on github at https://github.com/faysou/MTools .

Any comments or ideas of improvement are welcome.

POSTED BY: Faysal Aberkane

Dear @Faysal Aberkane could you pease make a new post completely dedicated to that material on this forum with examples of usage ?

We have a special Mathematica Add-Ons group for package introductions.

You can also attach files to the post if needed.

Thanks!

POSTED BY: Vitaliy Kaurov

Some usage examples are here http://community.wolfram.com/groups/-/m/t/880686.

POSTED BY: Faysal Aberkane

The following is an example of using an Association to store information, definitions, routines and even a dynamic display for a mathematical object. A reader can implement and use the routines simply by taking the Values of the Association. This particular example could be improved and generalized but I hope it shows a path toward object oriented type programming. I would consider this useful when you have particular objects with data and routines specialized to that object.

In this case the object is a graph of a function, with two coordinates and a 2-dimensional vector valued result - an embedded manifold in 4-dimensional Euclidean space.

exampleAssociation =
  Association[
   "Name" -> "Graph Manifold for f",
   "Initialize" :> 
    Clear[x1, x2, \[CapitalGamma], f, \[CurlyPhi], \[CurlyPhi]Inverse],
   "Graph Coordinates" -> {x1, x2},
   "Graph Domain" -> 0 < x1 < 1 && 0 < x2 < 1,
   "Graph Function" :> (f := {x1, x2} \[Function] {x1^2 x2 Sin[x2], 
        Cos[x2] (1 + x1)^2}),
   "Graph" :> (\[CapitalGamma] := {x1, x2, Sequence @@ f[x1, x2]}),
   "Graph Chart" :> (\[CurlyPhi][x1_, x2_, _, _] := {x1, x2}),
   "Inverse Graph Chart" :> (\[CurlyPhi]Inverse[x1_, x2_] := {x1, x2, 
       Sequence @@ f[x1, x2]}),
   "Active Numerical Report" :> (graphReport := 
      Uncompress[
       "1:eJzdVNFOwjAUBQElxhgfjL7OP4CRmMibGcGYKCHU8ORLGV1o0rWk3UT+wY/\
2toWNsjmJD5r40t3b23tOz+\
nam5mYRPVaraaOYQgES2Menei8CcMTVQm6hOBB4uXCe8acRoLNvUhIL7JtDRgmYhUdOT1t\
CAYixpT3PdWCZIiZIlUdZxBE3jDlYUIFdNmyxtnOuQ1KZ+9d+/\
HdWqQpDO0LjYmyRZ2OxYrITSc1s5v2bGOIcgcyB8mWBEIVlhjgfBNjlipaz/\
AtV5X688zj136nd9cD/U1ngcVBG95KfWiHE5XoQ+X6vtWCSrWAGwWScn3BAsvE6Atut+\
f7hb4KpAsILIb3yN/gNMk/\
86pwGxvF6rbRKaErbU2n61ujAyHknHKcENXPMdESh0TSDx1ewzAlYQKXeYpZSub59XNp2g\
5N00gD1xsZKKNzsMH06LdjsOY4puHm53flUfjWjEf5/\
CRlRJ1CcL9cEiwxD4lxF8WYscLeW5mnO1wWTGOM0nhG5FDIuJxfB7SRYewA626jbq3V/\
Zh253eAc7CZ0es+WXselO2twn//YP/9P/Z/n/8Q//3f8P+own8zNUoZ+wQ+g1Jj"])];

Some of the entries, such as Name, Graph Coordinates and Graph Domain are simply items of information. But when the Values of the Association are taken other items actually do things. The Initialize item Clears variables associated with the manifold object. Graph Function, Graph, Graph Chart and Inverse Graph Chart establish definitions for the various functions associated for the manifold. The Active Numerical Report. which is stored in compressed form, contains code to generate a dynamic report for the manifold. I developed the code separately, compressed it and then inserted it into the Association.

The Association can be used as follows. First take the Values.

Values[exampleAssociation];

The definitions are now available for use.

f
\[CapitalGamma]
\[CurlyPhi] @@ %
\[CurlyPhi]Inverse @@ %

The dynamic report can be generated by evaluating the following.

graphReport

And you can see the code for the report by evaluating:

graphReport // InputForm

Thanks everyone for your responses. Its been quite illuminating.

I have been working with Mathematica for more than a decade. There is always more to learn it seems.

How about trying to do something with the new Mathematica Associations? You can set up an Association to contain not only data but definitions and routines. When you evaluate Values on the Association it will install the definitions and routines. This is the kind of thing you might do when the definitions don't quite rise to the level of what you might put into a package and document, but rather would be associated with specific types of specialized objects or data.

Could you post a short example of the functionality you described?

POSTED BY: Frank Kampas

Take a look at Leonid's other post here:

POSTED BY: Szabolcs Horvát

It will take me a while before I get used to Associations. Never used them before.

Yes Venkat, We have an excellent book from R.E.Maeder (who was a Mathematica programmer at the beginning with Stephen. "Programming in Mathematica/The Mathematica programmer". A bit out-dated but just excellent. Regards, JMC

Thanks Jean for the suggestion. There are some used books by Maeder in good condition for sale on Amazon and are very cheap $1-$2. Perhaps I will get one of those.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract