ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> numpy-discussion
numpy-discussion
Re: [Numpy-discussion] Resizing without allocating additional memory
by David Huard other posts by this author
Dec 6 2006 12:15PM messages near this date
Re: [Numpy-discussion] Resizing without allocating additional memory | [Numpy-discussion] equivalent to isempty command in matlab (newbie question)
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@[...].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@[...].org
>  http://projects.scipy.org/mailman/listinfo/numpy-discussion
> 
Thread:
David Huard
Travis Oliphant
David Huard

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved