Avoid creating new arrays as results for numpy / scipy operations?

There are a lot of overheads for performing repeated operations in numpy / scipy, because most operations return a new object.

for instance

for i in range(100):
   x = A*x

I would like to avoid this by passing a reference to the operation, as in C

for i in range(100):
   np.dot(A,x,x_new) #x_new would now store the result of the multiplication
   x,x_new = x_new,x

Is there any way to do this? I would like this not just for multiplication, but for all operations that return a matrix or vector.

+4
source share
1 answer

See Learn to avoid unnecessary copies of arrays in IPython Books. From there, pay attention, for example, to these recommendations:

a *= b

, :

a = a * b

. , flatten() , ravel() (, , ). reshape() , .

, @hpaulj @ali_m, numpy out, . numpy.dot() docs:

out: ndarray, .

, , . , , C-, dtype dtype, (a, b). . , , , .

+3

Source: https://habr.com/ru/post/1584686/


All Articles