I have a question about numpys memory representations:
Suppose we have two arrays with memory:
import numpy as np
import gc
x = np.arange(4*3).reshape(4,3).astype(float)
y = (np.arange(5) - 5).astype(float)
y_ref = y
We use these ( x, y) in the structure, so we canβt just redefine them, as the user could link them for himself (as in y_ref). Now we want to combine their memory in one view. So, that the only kind, say p, divides memory into both arrays.
I did it as follows, but I don't know if this causes a memory leak:
p = np.empty(x.size+y.size, dtype=float)
c = 0
p[c:c+x.size].flat = x.flat # set the memory for combined array p
x.data = p[c:c+x.size].data
c += x.size
p[c:c+y.size].flat = y.flat # set the memory for combined array p
y.data = p[c:c+y.size].data
Thus, now we can work with a single view por with any of the arrays, without redoing each link to them
x[3] = 10
print p[3*3:4*3]
Even y_refgot an update:
print y[0]
y_ref[0] = 100
print p[x.size]
Is this the correct way to set array memory as a view to another array?
, ?
, x y, . ?
@Jaime:
p.size ( ) , (). , , . , .
, , , python .