[Tutor] list of references to object properties

eryksun eryksun at gmail.com
Sun Jan 20 02:04:31 CET 2013


On Sat, Jan 19, 2013 at 10:47 AM, Jose Amoreira <ljmamoreira at gmail.com> wrote:
>
> I defined a class, CelestialBody, that describes objects that
> represent planets in my simulation. These objects have three attributes:
> position, velocity and mass (the first two are 3D-vectors; as such, the
> number of attributes is actually 7). The many-body system is represented in
> the simulation by a list of CelestialBody objects.
>
> The dynamical state of the system is represented by a 6N (N being the number
> of planets) component array storing the components of the position and
> linear momentum of each body, and the integration procedures (odeint in this
> case) usually take this array as argument.


Working with views:

    >>> import numpy as np

2 planets:

    >>> system_state = np.zeros(12)

Create a view for each planet:

    >>> mars, earth = [system_state[6*i:6*(i+1)] for i in range(2)]

Modify the views, which also changes the original array:

    >>> mars += 1
    >>> earth += 2
    >>> system_state
    array([ 1.,  1.,  1.,  1.,  1.,  1.,  2.,  2.,  2.,  2.,  2.,  2.])

So you can use a CelestialSystem that has the overall state array and
CelestialBody objects that use a view on the latter. Velocity can be a
property, computed from linear momentum and mass.


More information about the Tutor mailing list