       Re: Suggestions for Maintaining "Object" State?

• To: mathgroup at smc.vnet.net
• Subject: [mg78363] Re: Suggestions for Maintaining "Object" State?
• From: Hannes Kessler <HannesKessler at hushmail.com>
• Date: Fri, 29 Jun 2007 05:44:38 -0400 (EDT)
• References: <f5vsk1\$l46\$1@smc.vnet.net>

```Hello Carey,

one possibility you could use is an object-oriented approach what you
perhaps have in mind with "using UpValues or Tags to assign object
and examples:

http://library.wolfram.com/infocenter/Conferences/5773/
http://library.wolfram.com/infocenter/Articles/3243/

The book of Roman Maeder "The Mathematica programmer" vol. I explains
his package in more detail.

Below is a simple sphere example for an object oriented approach which
works without the packages. The sphere state is characterized by its

Best regards,
Hannes

(*Nothing is a sphere by default*)

In:= sphereQ[_] := False;

(*Constructor of a sphere*)

In:= sphere[r_, c : {_, _, _}] := Module[{sphere},
sphereQ[sphere] = True;
center[sphere] = c;
sphere];

(*Methods which can be applied to a sphere*)

In:= volume[s_?sphereQ] := (4 Pi)/3 rad[s]^3;

move[s_?sphereQ, dc : {_, _, _}] := center[s] = center[s] + dc;

(*Create two spheres which are initially equal*)

In:= s1 = sphere[1, {0, 0, 0}]
s2 = sphere[1, {0, 0, 0}]

Out= sphere\$66

Out= sphere\$69

(*Here we see the initial states of both spheres*)

center[s1]

Out= 1

Out= {0, 0, 0}

center[s2]

Out= 1

Out= {0, 0, 0}

(*The volumes are of course also equal*)

In:= volume[s1]
volume[s2]

Out= (4 \[Pi])/3

Out= (4 \[Pi])/3

(*Now change the state of sphere 1*)

In:= move[s1, {1, 0, 0}];
expand[s1, 2];

(*Now the states are different*)

center[s1]
center[s2]

Out= 3

Out= {1, 0, 0}

Out= 1

Out= {0, 0, 0}

(*And the volumes will be calculated depending on the state of each
sphere*)

In:= volume[s1]
volume[s2]

Out= 36 \[Pi]

Out= (4 \[Pi])/3

On 28 Jun., 10:48, "Carey Sublette" <carey... at earthling.net> wrote:
> I am starting to develop a fairly complex simulation using Mathematica 6 and
> am confronting a basic issue that I am unsure how best to resolve: "How to
> preserve the state of an object?"
>
> Physical objects represented in a simulation have internal state that is
> preserved and affects how they respond to stimulus from the simulation
> environment.
>
> Possible ways of implementing this behavior includes creating modules
> representing objects and:
> *  exporting state to the session (or other enclosing scope) as a list of
> values, which either exists as a global variable or is passed in as a
> parameter;
> * using UpValues or Tags to assign object state to symbols(?).
>
> At the moment the objects in the simulation have a 1-to-1 relationship,
> there is only one of each type, which simplifies the problem, though in the
> future I may need to maintain state for multiple objects of the same type.
>
> Does anyone have recommendations for how to do this, optimized either for
> convenience or efficiency?

```

• Prev by Date: Re: [Mathematica 6] Integrate strange result
• Next by Date: RV: System of differential-algebraic equations
• Previous by thread: Re: Suggestions for Maintaining "Object" State?
• Next by thread: Re: Suggestions for Maintaining "Object" State?