Duplicate numarrays numpy

This is a simplification of my question. I have a numpy array:

x = np.array([0,1,2,3])

and I have a function:

def f(y): return y**2

I can calculate f (x).

Now suppose I really want to compute f (x) for repeated x:

x = np.array([0,1,2,3,0,1,2,3,0,1,2,3])

Is there a way to do this without creating a duplicate version of x and in a way transparent to f?

In my particular case, f is the function involved, and one of the arguments is x. I would like to be able to calculate f when x repeats without repeating it, since it does not fit in memory.

Overwriting f to handle duplicate x would work, and I was hoping for a smart way, perhaps to subclass the numpy array for this.

Any advice appreciated.

+4
source share
1 answer

() , .

...

import numpy as np
x = np.arange(4)
numrepeats = 3

y = np.lib.stride_tricks.as_strided(x, (numrepeats,)+x.shape, (0,)+x.strides)

print y
x[0] = 9
print y

, y x, x. , y , .

, :

import numpy as np
x = np.arange(4)
numrepeats = 1e15

y = np.lib.stride_tricks.as_strided(x, (numrepeats,)+x.shape, (0,)+x.strides)

... , 32 , x. (y ~ 8 , )

, y , , , . "" x , 2 .

, y , (, y**2 ), .

. (, y **= 2 x **= 2. ).

x x.

.

def f(x):
    return x**3

x[...] = f(x)
print y

y , x.

+8

All Articles