[Numpy-discussion] question about memory ownership

Darren Dale dsdale24@gmail....
Sun Jan 18 07:17:53 CST 2009


On Sat, Jan 17, 2009 at 11:44 PM, Robert Kern <robert.kern@gmail.com> wrote:

> On Sat, Jan 17, 2009 at 22:35, Darren Dale <dsdale24@gmail.com> wrote:
> >
> > On Sat, Jan 17, 2009 at 11:23 PM, Robert Kern <robert.kern@gmail.com>
> wrote:
> >>
> >> On Sat, Jan 17, 2009 at 22:06, Darren Dale <dsdale24@gmail.com> wrote:
> >> > Hello,
> >> >
> >> > Would someone be so kind as to explain how to create an ndarray
> subclass
> >> > that owns its own memory? I think RealisticInfoArray at
> >> >
> >> >
> http://docs.scipy.org/doc/numpy/user/basics.subclassing.html#basics-subclassing
> >> > does not own its own memory, do you have to call ndarray.__new__
> >> > directly,
> >> > or is there another way?
> >>
> >> Does it matter? Unless if something actually digs down to grab the
> >> base object, the RealisticInfoArray object is the only thing keeping
> >> it alive. When it goes away, the underlying array goes away.
> >>
> >> But take a look at the implementation of memmap for a subclass that
> >> calls ndarray.__new__ directly.
> >
> >
> > I'm still working on my physical quantities package, and I want to
> prevent
> > something like the following:
> >
> > q=[1,2,3,4]*kg*m**2/s**2
> > qq=q[:2]
> > qq.units=BTU
> >
> > That would give me qq in units of british thermal units, but it would
> also
> > modify the magnitude of q[:2]. It seems the most straight forward
> solution
> > is to simply disallow in-place modification of units when the array
> doesn't
> > own its own memory.
>
> Ah. I'd take a functional approach to unit conversion, then. You can't
> modify units in-place. Memory can't be much of a concern since the
> conversion code will have temporaries.
>
> But you could check that .base is an instance of your quantity class.
> If it's a regular array, go ahead and modify in-place.
>

Thanks for the advice. I'd like to look into the second option, since the
former would not allow other in-place operations like *= /= **/. Hopefully
the package will be fit to share with this list before too long.

Darren
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20090118/fa83b22e/attachment-0001.html 


More information about the Numpy-discussion mailing list