Web sample page fromThe
Mathematica
Book,First Edition,by Stephen Wolfram,published by AddisonWesley Publishing
Company (hardcover ISBN0201193345;softcover ISBN0201193302).To order
Mathematica
or this book contact Wolfram
Research:
info wolframc om
;
httpwwwwo lfra mco m
;18004416284.
c
1988 Wolfram Research,Inc.Permission is hereby granted for web users to make one paper copy of this page for their
personal use.Further reproduction,or any copying of machinereadable ﬁles (including this one) to any server computer,is strictly
prohibited.
Mathematica as a Computer Language
4.1.6 ObjectOriented Programming
When y ou write programs that in v olv e man y di eren t functions it is imp ortan t
to ha v e some kind of general sc heme for organizing y our co de One traditional
approac h is to break y our program in to di eren t mo dules with eac h mo dule
con taining all the functions that do a particular kind of thing
Ob jectorien ted programming is another approac h to organizing co de The basic
idea is to collect together not functions that do the same kinds of things but rather
functions that act on the same kinds of ob jects Ob jectorien ted programming is
b ecoming v ery p opular for organizing large soft w are systems In fact the in ternal
co de of Mathematica itself w as written using an ob jectorien ted extension of the C
programming language
In the ob jectorien ted approac h y ou in tro duce v arious t yp es of ob ject and then
sp ecify metho ds for carrying out di eren t op erations on these ob jects Y ou can
see ho w this w orks b y thinking ab out writing a program to prin t out ob jects The
traditional approac h w ould b e to ha v e one prin t function whic h con tains co de for
man y di eren t kinds of ob jects The ob jectorien ted approac h is instead to asso ciate
with eac h t yp e of ob ject a function that giv es the metho d for prin ting the ob ject
It is v ery easy to do ob jectorien ted programming in Mathematica The basic
idea is to asso ciate Mathematica transformation rules with the ob jects they act
on rather than with the functions they p erform
If y ou sp ecify a rule for the pattern f x
the rule will b e asso ciated with the
sym b ol f Ho w ev er if y ou giv e a rule for f g x
the rule can b e asso ciated either
with f or with g
This de nes a rule for the function f In f x
x
This rule is also asso ciated with f In f g x
g x
Here are the t w o rules y ou ha v e giv en for f In f
f
f fgx
gx
f fx
x
The h tells Mathematica to asso ciate
this rule with h rather than f
In h f h x
p x
Web sample page fromThe
Mathematica
Book,First Edition,by Stephen Wolfram,published by AddisonWesley Publishing
Company (hardcover ISBN0201193345;softcover ISBN0201193302).To order
Mathematica
or this book contact Wolfram
Research:
info wolframc om
;
httpwwwwo lfra mco m
;18004416284.
c
1988 Wolfram Research,Inc.Permission is hereby granted for web users to make one paper copy of this page for their
personal use.Further reproduction,or any copying of machinereadable ﬁles (including this one) to any server computer,is strictly
prohibited.
Programming Metho dologies
This sho ws the rule y ou ha v e giv en for the
ob ject h
In h
h
h fhx
px
Y ou can set up di eren t t yp es of ob jects in Mathematica just b y giving them
di eren t sym b ols as heads Then y ou can asso ciate rules or metho ds with these
ob jects b y including the appropriate in the denitions y ou mak e
ModularInteger FormatModularIntege ri n
SequenceFormi mod n
ModularInteger MinusModularInteger i n ModularIntegerM odi n n
Some rules for
ModularInteger
objects.
Ob jectorien ted programming lets us organize denitions of mathematical ob
jects in Mathematica in m uc h the same w a y as the denitions migh t b e organized
in a mathematics textb o ok Y ou can ha v e di eren t sections of y our co de that
giv e all the denitions asso ciated with a particular kind of ob ject Using y ou
can k eep the denitions for eac h new kind of ob ject separate
ModularInteger ModularIntegeri n ModularIntegerj n
ModularIntegerMod i j n n
A rule for calculating sums of
ModularInteger
objects.
Operator Overloading
One imp ortan t asp ect of ob jectorien ted programming in Mathematica is that it lets
y ou o v erload op erators lik e Y ou can use the familiar notation to represen t
an y kind of addition But exactly what the addition do es can dep end on the t yp es
of ob jects that are in v olv ed
Asso ciating denitions with t yp es of ob jects rather than op erations not only
mak es y our co de b etter organized but can also mak e it m uc h more ecien t The
standard addition function Plus is used v ery often in Mathematica If y ou ga v e
additional denitions for Plus itself then Mathematica w ould ha v e to test these
denitions ev ery time it used the function Plus The presence of y our denitions
w ould then slo w do wn ev ery single addition op eration in Mathematica On the other
hand if y ou asso ciate y our denitions with a particular t yp e of ob ject Mathematica
tests y our denitions only when that particular ob ject o ccurs Mathematica w ould
therefore usually not lo ok at y our denitions when it do es additions except in the
sp ecic cases where the argumen ts are of a particular t yp e
Web sample page fromThe
Mathematica
Book,First Edition,by Stephen Wolfram,published by AddisonWesley Publishing
Company (hardcover ISBN0201193345;softcover ISBN0201193302).To order
Mathematica
or this book contact Wolfram
Research:
info wolframc om
;
httpwwwwo lfra mco m
;18004416284.
c
1988 Wolfram Research,Inc.Permission is hereby granted for web users to make one paper copy of this page for their
personal use.Further reproduction,or any copying of machinereadable ﬁles (including this one) to any server computer,is strictly
prohibited.
Mathematica as a Computer Language
Mathematica alw a ys follo ws the general principle of lo oking at more sp ecic
denitions b efore more general ones One consequence of this principle is that when
Mathematica do es an op eration lik e addition it lo oks at denitions that apply
to the particular t yp es of argumen ts used b efore it lo oks at general denitions
for addition with arbitrary t yp es of argumen ts As a result y our denitions for
addition of a particular kind of ob ject will alw a ys b e tried b efore the standard
builtin Mathematica addition function is used Y ou can e ectiv ely o v erride the
builtin addition function when a particular kind of ob ject o ccurs
The same mec hanism allo ws y ou to insert y our o wn denitions to sp ecify ho w
a particular kind of ob ject should b e prin ted Mathematica tries y our denitions
b efore it uses its o wn standard builtin prin t functions
In general y ou can extend the w a y that Mathematica s builtin functions w ork
b y adding denitions for ho w they should act on new kinds of ob jects that y ou
in tro duce
Sometimes y ou will need to in tro duce b oth new kinds of ob jects and new op era
tions Y ou can giv e denitions for ho w y our new op erations act on v arious kinds of
ob jects Then y ou can giv e a more general denition for ho w y our new op erations
act on other ob jects for whic h y ou ha v e not giv en more sp ecic denitions Math
ematica will then rst try to use the denitions y ou ha v e giv en for sp ecic t yp es of
ob jects if it nds none that apply then it will use the more general denition y ou
ha v e giv en for the op eration
Functions with Several Arguments
In ob jectorien ted programming eac h denition y ou mak e is asso ciated with a par
ticular kind of ob ject This approac h w orks v ery w ell if eac h of y our denitions
in v olv es only one kind of ob ject This is certainly true when y ou are dealing with
functions lik e Format and Minus that tak e just one argumen t Ho w ev er things
b ecome more complicated when y ou need to deal with functions that tak e sev eral
argumen ts
So long as all the argumen ts to a particular function are of the same t yp e things
are still quite simple The real problem comes when the argumen ts are of di eren t
t yp es What should Mathematica do when y ou ask to add a ModularInteger and
a PAdicInteger
Should the result b e a ModularInteger or a PAdicInteger
Sometimes y ou will just w an t to sa y that the ob jects y ou are trying to add together
ha v e incompatible t yp es But often y ou will w an t to pro duce a denite result
A common example that arises in man y programming languages is the problem
of com bining di eren t t yp es of n um b ers If y ou add an in teger to a real n um b er y ou
should get a real n um b er as the result If y ou add a rational n um b er to an in teger
Web sample page fromThe
Mathematica
Book,First Edition,by Stephen Wolfram,published by AddisonWesley Publishing
Company (hardcover ISBN0201193345;softcover ISBN0201193302).To order
Mathematica
or this book contact Wolfram
Research:
info wolframc om
;
httpwwwwo lfra mco m
;18004416284.
c
1988 Wolfram Research,Inc.Permission is hereby granted for web users to make one paper copy of this page for their
personal use.Further reproduction,or any copying of machinereadable ﬁles (including this one) to any server computer,is strictly
prohibited.
Programming Metho dologies
y ou should get a rational n um b er Y ou can think of the di eren t t yp es of n um b ers
as b eing arranged in the hierarc h y Integer Rational Real Whenev er y ou
com bine t w o n um b ers they are b oth con v erted to the t yp e that is higher on the
hierarc h y If y ou add an in teger to a real n um b er the in teger is con v erted in to a
real n um b er and then the rule for adding t w o real n um b ers is used
When sev eral di eren t kinds of ob jects app ear in a particular denition y ou can
asso ciate the denition with an y of the kinds of ob jects Mathematica will c hec k all
the ob jects in a particular expression to see if there are denitions asso ciated with
them
Often y ou will w an t to asso ciate y our denitions with the least common ob ject
that app ears If y ou do this Mathematica will test the denition as infrequen tly as
p ossible
By allo wing y ou simply to asso ciate transformation rules with an y t yp e of ob ject
Mathematica giv es y ou a exible mec hanism for dealing with expressions in v olving
sev eral di eren t kinds of ob jects One can imagine ho w ev er dev eloping a more
structured approac h to dealing with suc h expressions A t the simplest lev el one
migh t ha v e a hierarc h y of t yp es of ob jects and demand that when t w o ob jects
o ccur together they are alw a ys con v erted to the t yp e that app ears highest on the
hierarc h y In practice ho w ev er t yp es of mathematical ob jects do not form an y kind
of strict hierarc h y In some resp ects they form a net w ork or directed graph and
one could imagine alw a ys con v erting ob jects to the t yp e of their common ancestor
in the net w ork But ev en this m uc h more elab orate mec hanism do es not adequately
capture actual mathematical usage Di eren t forms of algebraic expressions sa y
factored and expanded out can b e though t of as di eren t t yp es In most cases the
rules for when to con v ert b et w een these t yp es are m uc h more complicated than y ou
can represen t b y a simple xed net w ork of t yp e con v ersions
Enter the password to open this PDF file:
File name:

File size:

Title:

Author:

Subject:

Keywords:

Creation Date:

Modification Date:

Creator:

PDF Producer:

PDF Version:

Page Count:

Preparing document for printing…
0%
Comments 0
Log in to post a comment