next up previous contents
Nächste Seite: 7 GNU Free Documentation Aufwärts: 6 Objektorientierte Methoden Vorherige Seite: 6.3 Operator Overloading   Inhalt

Unterabschnitte

6.4 Packages und Context

Wo speichert MathematicaTransformation rules ?

Mechanismen

Sie bestimmen, wo etwas abgespeichert und gesucht wird.

Suchalgorithmus für Symbole

Wenn Mathematica einem Symbol begegnet, geht es wie folgt vor:

  1. Ist das Symbol im current context $Context definiert? Bei ja nimmt Mathematica die gespeicherte Regel, sonst...
  2. Mathematica durchsucht alle Kontexte im $ContextPath. Bei ja nimmt Mathematica die gespeicherte Information, sonst...
  3. Mathematica erzeugt das Symbol im current context $Context

Beispiel

In[1]:= $ContextPath
Out[1]= {Global`, System`}

In[2]:= $Context
Out[2]= Global`

In[3]:= h[x_]:= 2x
Out[3]=


Ist h in $Context ? Nein
Ist h im $ContextPath ? Nein
Dann erzeuge Symbol (,,Tag`` in der Datenbank ) im $Context = Global`.

In[4]:= ?h
Out[4]= Global`h
h[x_]:=2*x

Wie verändert man $Context und $ContextPath?

Befehl Symbolname $Context $ContextPath
Prime[10^6] System`Prime Global` {Global`,System`}
«LinOp.m
BeginPackage ["LinOp`"] System`BeginPackage LinOp` {LinOp`, System`}
ToMatrix ::usage:... LinOp`ToMatrix LinOp` {LinOp`, System`}
Begin["`Private`"] LinOp`Private` {LinOp`, System`}
project[a_,n_,a_]:=n LinOp`Private`project LinOp`Private` {LinOp`, System`}
End[] LinOp` {LinOp`, System`}
EndPackage[] Global` {LinOp`, Global`,
System`}
ToMatrix[Op[{a}, {4a}] LinOp`ToMatrix

Probleme

Wenn das Symbol aus einem Package schon vorher definiert wurde.

In[5]:= ToMatrix[l_List]:=l
Out[5]=

In[6]:= ?ToMatrix
Out[6]= Global`ToMatrix
ToMatrix[op:Op[basic_List, image_List]] := Outer[project, basis, image] ToMatrix[l_List] := l

In[7]:= <<LinOp.m
Out[7]= ToMatrix::shdw: Warning: Symbol ToMatrix appears in multiple contexts {LinOp`, Global`}; definitions in context LinOp` may shadow or be shadowed by other definitions.

Da nach EndPackage[] $Context wieder auf Global` gesetzt wird, wird beim nächsten Aufruf von ToMatrix Global`ToMatrix verwendet.

Ein Package benötigt ein anderes Package

Beispiel

QM.m benötigt LinOp`

$ContextPath = {Global`, System`}
BeginPackage["QM`", "LinOp`"]
falls LinOp` bereits geladen wurde: no action
falls nicht: <<LinOp.m einlesen
$ContextPath = {QM`, LinOp`, System`}

Hidden Import
User soll auf dessen Funktion nicht zugreifen können
Needs["context`"] (nach BeginPackage[])

Grundgerüst für Packages

BeginPackage["Package`"]
Package::usage = "This package implements..."
f::usage = "f[list]..." (* erzeugt erst Package`f *)
Begin["`Private`"]
f[...]:= ...
End[] (* Package`Private *)
EndPackage[] (* Package *)


next up previous contents
Nächste Seite: 7 GNU Free Documentation Aufwärts: 6 Objektorientierte Methoden Vorherige Seite: 6.3 Operator Overloading   Inhalt
Werner Scholz 2000-06-21