[SciPy-User] Mapping objects to numpy array

Jim Vickroy Jim.Vickroy@noaa....
Wed Aug 5 10:32:17 CDT 2009


josef.pktd@gmail.com wrote:
> On Wed, Aug 5, 2009 at 9:27 AM, Chris Colbert<sccolbert@gmail.com> wrote:
>   
>> 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@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@scipy.org
>>>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>>>         
>>> _______________________________________________
>>> SciPy-User mailing list
>>> SciPy-User@scipy.org
>>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>>
>>>       
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User@scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>
>>     
>
> I was trying to do it from scratch, but I got confused about class variables.
> Can class variables only be accessed through the class self.__class__
> ? I seem to get rusty in my python knowledge.
>
> the following seems to do what Emmanuelle proposed, but there might be
> better examples already in the mailing lists.
>
> Josef
>
>
> class Particle(object):
>   a  = np.empty((3,2))
>   
The above definition means that all instances of your Particle class 
will share the same /*"a".
*/If you create instances p1 = Particle(0,1,2) and p2 = Particle(3,4,5), 
p1.a and p2.a reference the same /*"a"*/.
Is that your intent?
>   def __init__(self,k,x,v):
>      self.k = k
>      #why do I need self.__class__ for class variable
>   
self.a should work just fine.  Did you try it without specifying the 
__class__ qualifier?
>      self.__class__.a[k,0] = x
>      self.__class__.a[k,1] = v
>
>   def _getx(self):
>       return self.__class__.a[self.k,0]
>   def _setx(self, value):
>       self.__class__.a[self.k,0] = value
>
>   x = property(_getx, _setx)
>
>
> aa  = Particle.a
> print aa
> p1 = Particle(0, 1, 2)
> print p1
> p2 = Particle(1, 2, 3)
> p3 = Particle(2, 3, 4)
> print aa
> aa[:,:] = 1
> print aa
> print p2._getx(), p2.x
> aa[0,0] = 5
> print p1.x
> p2.x = 10
> print aa
> print Particle.a
> print vars(p2)
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>   

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20090805/d3005ba5/attachment-0001.html 


More information about the SciPy-User mailing list