Numeric, how to do in place full-array modification?
Janko Hauser
hauser at ifm.uni-kiel.de
Sun Oct 3 03:33:39 EDT 1999
"Ionel Simionescu" <ionel at psy.uva.nl> writes:
> Hi,
>
> I want to do the following:
>
> for t in range( T ):
> net = dot( C, x )
> x = A*net*(1-net)
>
> Because these arrays might turn to be large, I want to pre-alloate <net> and
> write both <net> and <x> in place rather than creating new arrays.
>
>
> Intuitively, I would write it like:
>
> net = zeros(shape, typecode)
> for t in range( T ):
> net[:] = dot( C, x)
> x[:] = A*net*(1-net)
>
This would not buy you much. Because you only substitute inplace, the
dot function is always generating a new array, which gets copied into
net in your case, which would be more expensive than only to reference
the result with
net = dot( C, x)
But dot is mainly implemented in C, so this should be no memory problem.
The second one can be changed to inplace modification.
x=multiply(multiply(A,net,x),subtract(1,net,x),x)
That looks ugly, right? :-)
HTH
__Janko
--
Institut fuer Meereskunde phone: 49-431-597 3989
Dept. Theoretical Oceanography fax : 49-431-565876
Duesternbrooker Weg 20 email: jhauser at ifm.uni-kiel.de
24105 Kiel, Germany
More information about the Python-list
mailing list