[SciPy-User] Mapping objects to numpy array

Chris Colbert sccolbert at gmail.com
Wed Aug 5 09:27:31 EDT 2009


its for the same reason you cant do this:

In [1]: a = 1

In [2]: b = a

In [3]: a
Out[3]: 1

In [4]: b
Out[4]: 1

In [5]: a = 2

In [6]: a
Out[6]: 2

In [7]: b
Out[7]: 1


but you CAN do this:

In [8]: a = [1, 2]

In [9]: b = a

In [10]: a
Out[10]: [1, 2]

In [11]: b
Out[11]: [1, 2]

In [12]: a[0] = 0

In [13]: a
Out[13]: [0, 2]

In [14]: b
Out[14]: [0, 2]


floats and ints are immutables in python.
you'll need a mutable container to store the values how you are wanting.

That said, you still cant map to them with a numpy array like you want (AFAIK)

You may want to look into subclassing ndarray or reformulating your
problem so you dont need this requirement.


On Wed, Aug 5, 2009 at 9:05 AM, Emmanuelle
Gouillart<emmanuelle.gouillart at normalesup.org> wrote:
>        Hi Jack,
>
>        I don't think you can do it this way. The reason is that p1.x and
> p1.v can be anywhere in the memory, maybe at very different places. So
> np.array makes a *copy* when you pass ( p1.x, p1.v) as arguments. I tried
> setting copy=False as a keyword argument of np.array but it doesn't
> change the result; apparently a copy has to be made. Why don't you define
> an array inside the class Particle, instead of different attributes?
>
>        Cheers,
>
>        Emmanuelle
>
> On Wed, Aug 05, 2009 at 02:44:18PM +0200, Jack Liddle wrote:
>> Hi
>
>> I'm trying to map some objects to a numpy array, so I can solve some
>> ode's with them and then work on the results with my objects.
>
>> Here's some code.
>
>> from numpy import array
>
>> class Particle(object):
>>    def __init__(self,x,v):
>>       self.x = x
>>       self.v = v
>
>> p1 = Particle(1,0)
>> a  = array(( p1.x, p1.v))
>> a[0] = 5
>> print p1.x
>
>> This prints 1 instead of 5.  How do I make the array 'a' share the
>> same memory as the p1 attributes?
>
>> Any ideas, hope this makes sense
>
>> Thanks
>
>> Jack Liddle
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User at scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>



More information about the SciPy-User mailing list