MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: RE: Newbie-type question: structured data types in Mathematica?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg37538] Re: [mg37533] RE: [mg37521] Newbie-type question: structured data types in Mathematica?
  • From: "Hermann Schmitt" <schmitther at netcologne.de>
  • Date: Mon, 4 Nov 2002 02:43:59 -0500 (EST)
  • References: <200211030759.CAA16374@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hello,
below I show you, how the example of David can be programmed with my OO
System for Mathematica. It did really run on my computer
----- Original Message -----
From: "David Park" <djmp at earthlink.net>
To: mathgroup at smc.vnet.net
Subject: [mg37538] [mg37533] RE: [mg37521] Newbie-type question: structured data types
in Mathematica?


> Ken,
>
> You are rather edging into OO programming, of which there has been some
> discussion on this news group in the past week or so. Perhaps there is
room
> for something like a StructuredData package that would be considerably
less
> than OO but serve a useful purpose.
>
> In any case, you could do something like the following where I define a
> CircleType data item and a few routines for manipulating it.
>
> CircleType = {{_, _}, _};
> ConstructCircle[position : {_, _}, radius_] := {position, radius}
> GetRadius[circle : CircleType] := Part[circle, 2]
> GetPosition[circle : CircleType] := Part[circle, 1]
> Attributes[SetRadius] = {HoldFirst};
> SetRadius[circle_][radius_] := (circle = ReplacePart[circle, radius, 2];)
>
> mycircle = ConstructCircle[{1, 2}, 3];
>
> GetRadius[mycircle]
> 3
>
> GetPosition[mycircle]
> {1, 2}
>
> SetRadius[mycircle][3.5]
> GetRadius[mycircle]
> 3.5
>
> etc. I hope you get some other good discussion of this because it is a
very
> useful question.
>
> David Park
First, I tried to program as similar to the example of David as possible.
But when I chose the function names getRadius and setRadius ( and anlog with
Position) I got "spelling checks", therefore I changed the names of the "set
" functions.
The Class Circle:
{"Circle", "parent[] = {}","
status = {{xpos, ypos}, radius};
","
CircleInit[xp_, yp_, rd_] := Module[{},
 xpos = xp;
 ypos = yp;
 radius = rd;
];
getRadius[] := Return[radius];
setRad[rd_] := radius = rd;
getPosition[] := Return[{xpos, ypos}];
setPos[xp_, yp_] := (xpos = xp; ypos = yp;);
"}
The Program:
 obj1 = MathNew["Circle", 1, 2, 3];
  Print["Radius: ", obj1 @ getRadius[]];
  Print["Position: ", obj1 @ getPosition[]];
  obj1 @ setRad[3.5];
  Print["Radius: ", obj1 @ getRadius[]];
  obj1 @ setPos[4, 5];
  Print["Position: ", obj1 @ getPosition[]];

The Result:
Radius: 3
Position: {1, 2}
Radius: 3.5
Position: {4, 5}
In[44]:=

I programmed a second version of the program, because I thought, that in OO
"Position" should be a separate class, because in can be used in many other
applications.
The program remains the same.

The Class "Circle":
{"Circle", "parent[] = {\"Position\"}","
status = {radius};
","
CircleInit[xp_, yp_, rd_] := Module[{},
 PositionInit[xp, yp];
 radius = rd;
];
getRadius[] := Return[radius];
setRad[rd_] := radius = rd;
"}

The Class "Position":
{"Posion", " parent[] = {}", "
status = {xpos, ypos};
PositionInit[xp_, yp_] := Module[{},
 xpos = xp;
 ypos = yp;
];
","
getPosition[] := Return[{xpos, ypos}];
setPos[xp_, yp_] := (xpos = xp; ypos = yp;);
"}
> djmp at earthlink.net
> http://home.earthlink.net/~djmp/

> From: Kenneth McDonald [mailto:kmmcdonald at wisc.edu]
To: mathgroup at smc.vnet.net
> To: mathgroup at smc.vnet.net
>
> I've been looking for a while, and I can't find a way to create any
> sort of structured data type in Mathematica, i.e. one with named fields
> that contain other values. Does such a beast exist?
>
> Thanks for the help,
> Ken McDonald
> kmmcdonald at wisc.edu
>
>
>



  • Prev by Date: Re: Combination/Permutation questions
  • Next by Date: Re: RE: DrawGraphics Figure-8 && CPU Strangeness
  • Previous by thread: RE: Newbie-type question: structured data types in Mathematica?
  • Next by thread: Re: Newbie-type question: structured data types in Mathematica?