[Numpy-discussion] Resizing without allocating additional memory

David Huard david.huard at gmail.com
Wed Dec 6 15:15:04 EST 2006


Thanks Travis,

I guess we'll have to tweak the fortran subroutines. It would have been neat
though.

David

Answer: Since g+=1 adds one to all N elements of g, the buffer a gets
incremented N times.
So
a = array(i)
g = ndarray(shape=(1,N), dtype=int, buffer=a, strides=(0,0))
g+=M

returns i + M*N



2006/12/6, Travis Oliphant <oliphant at ee.byu.edu>:
>
> David Huard wrote:
>
> > Hi,
> >
> > I have fortran subroutines wrapped with f2py that take arrays as
> > arguments, and I often need to use resize(a, N) to pass an array of
> > copies of an element. The resize call , however, is becoming the speed
> > bottleneck, so my question is:
> > Is it possible to create an (1xN) array from a scalar without
> > allocating additional memory for the array, ie just return a new
> > "view" of the array where all elements point to the same scalar.
> >
> I don't think this would be possible in Fortran because Fortran does not
> provide a facility for using arbitrary striding (maybe later versions of
> Fortran using pointers does, though).
>
> If you can use arbitrary striding in your code, then you can construct
> such a view using appropriate strides (i.e.  a stride of 0).  You can do
> this with the ndarray constructor:
>
>
> a = array(5)
> g = ndarray(shape=(1,10), dtype=int, buffer=a, strides=(0,0))
>
> But, notice you will get interesting results using
>
> g += 1
>
> Explain why the result of this is an array of 15 (Hint:  look at the
> value of a).
>
> -Travis
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20061206/9ab2550a/attachment.html>


More information about the NumPy-Discussion mailing list