[Numpy-discussion] Array views
Sat Mar 26 14:13:43 CDT 2011
On Sat, 26 Mar 2011 12:32:24 -0500, srean wrote:
> Is there anyway apart from using ufuncs that I can make updater() write
> the result directly in b and not create a new temporary column that is
> then copied into b? Say for the matrix vector multiply example. I can
> write the matrix vector product in terms of ufuncs but will lose out in
> terms of speed.
Well, you can e.g. write
def updater(b, col_idx):
b[:,col_idx] *= 3 # <- modifies b[:,col_idx] in place
And ditto for sparse matrices --- but maybe this is not what you asked.
If you want to have control over temporaries, you can make use of the
out= argument of ufuncs (`numpy.dot` will gain it in 1.6.1 --- you can
call LAPACK routines from scipy.lib in the meantime, if your data is in
Also numexpr is probably able to write the output directly to a given
array --- using it is an alternative way to avoid temporaries, and
probably easier to write than doing things via the out= arguments.
For sparse matrices, things then depend on how they are laid out in
memory. You can probably alter the `.data` attribute of the arrays
directly, if you know how the underlying representation works.
More information about the NumPy-Discussion