[Numpy-discussion] Resizing without allocating additional memory
david.huard at gmail.com
Wed Dec 6 14:15:04 CST 2006
I guess we'll have to tweak the fortran subroutines. It would have been neat
Answer: Since g+=1 adds one to all N elements of g, the buffer a gets
incremented N times.
a = array(i)
g = ndarray(shape=(1,N), dtype=int, buffer=a, strides=(0,0))
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).
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Numpy-discussion