|
[Date Index]
[Thread Index]
[Author Index]
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
state to symbols". Here are two links where you can download packages
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
radius and center.
Best regards,
Hannes
(*Nothing is a sphere by default*)
In[1]:= sphereQ[_] := False;
(*Constructor of a sphere*)
In[2]:= sphere[r_, c : {_, _, _}] := Module[{sphere},
sphereQ[sphere] = True;
rad[sphere] = r;
center[sphere] = c;
sphere];
(*Methods which can be applied to a sphere*)
In[3]:= volume[s_?sphereQ] := (4 Pi)/3 rad[s]^3;
move[s_?sphereQ, dc : {_, _, _}] := center[s] = center[s] + dc;
expand[s_?sphereQ, dr_] := rad[s] = rad[s] + dr;
(*Create two spheres which are initially equal*)
In[6]:= s1 = sphere[1, {0, 0, 0}]
s2 = sphere[1, {0, 0, 0}]
Out[6]= sphere$66
Out[7]= sphere$69
(*Here we see the initial states of both spheres*)
In[8]:= rad[s1]
center[s1]
Out[8]= 1
Out[9]= {0, 0, 0}
In[10]:= rad[s2]
center[s2]
Out[10]= 1
Out[11]= {0, 0, 0}
(*The volumes are of course also equal*)
In[12]:= volume[s1]
volume[s2]
Out[12]= (4 \[Pi])/3
Out[13]= (4 \[Pi])/3
(*Now change the state of sphere 1*)
In[14]:= move[s1, {1, 0, 0}];
expand[s1, 2];
(*Now the states are different*)
In[16]:= rad[s1]
center[s1]
rad[s2]
center[s2]
Out[16]= 3
Out[17]= {1, 0, 0}
Out[18]= 1
Out[19]= {0, 0, 0}
(*And the volumes will be calculated depending on the state of each
sphere*)
In[20]:= volume[s1]
volume[s2]
Out[20]= 36 \[Pi]
Out[21]= (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?
|