Message Boards Message Boards

0
|
7031 Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

User Defined Heads

Posted 9 years ago

Is it possible to define one's own Head or type in Mathematica? If so then how does one do it? Thank you.

POSTED BY: Roger B
4 Replies

There are no types! Integer and Complex are just heads that people use in a way that's like a type.

What is your background in programming and software engineering? If you have a lot of background in programming languages, like a grad student in comp sci, I recommend the free book I linked to. However, if you are like most people who program with Mathematica, you don't need to know anything about this stuff and it won't help you. It will instead just be needlessly confusing and frustrating. To learn the basics of programming in Mathematica, read through the Mathemativa virtual book: https://www.youtube.com/watch?v=2wlk9qqPxp4

To answer your question:

Once you've created a head, you use Upvalues and Downvalues to create operations with that Head.

If you're interested in this, you may be interested in reading about how people implement algebras such as Quarternions or Dual Numbers in Mathematica.

POSTED BY: Sean Clarke
Posted 9 years ago

Yes, you are correct and function names are heads. I forgot about that. But, I was thinking about my own type like an Interger or Complex. Then I'd create the infrastructure around my type to handle sums, products, ...

POSTED BY: Roger B

Actually you can use the Head-mechanism to build type-like categories very similar to classes in C++. (I did this for various geometrical classes, and for state spaces of (quantum-) mechanical systems.) Once, when one learned Mathematica from Stephen Wolfram's Mathematica book, one found among the Mathematica-supported programming styles also a chapter on the object oriented programming style with instructive examples. I don't know why WolframResearch no longer claims to support object oriented programming. Perhaps there is a general understanding that 'object orientation' implies that there is a mechanism for inheritance. Heads seem not to support inheritance in any direct way. One important way to use user-defined heads is 'function overloading': Assume you have introduced two heads h1, h2. Then you can define

f[x_h1] := ... ; f[x_h2] := ... ;

and when you call f(x) the evaluation will depend on whether x is of form h1[...] or of form h2[...].

POSTED BY: Ulrich Mutze

Yes you can make your own Head.

Bit I'm afraid I don't actually understanding your question as it is written. Are you asking how to make a fuction?

If you have a background with programming already, I would recommend this introduction. http://www.mathprogramming-intro.org/

Here is how you make your own head in Mathematica:

hereIsMyHead[]

You just write it. That's it. There's no grand declaration of its existence or anything. If you want it to do something ... you can program that head to do something. For example, we can program the head to do something to an argument, which basically makes it a function:

hereIsMyHead[a_] := "The Head Called hereisMyHead was given " <> ToString[a] <> " as an argument

The Wolfram Language isn't really typed. As you have noted, heads take the place of types in many respects.

POSTED BY: Sean Clarke
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