mardi 5 septembre 2023

getting random sampling from cartesian product

Consider the complex number z = x+iy. I have an object constructor class that can take a complex number and plot something from it, say pole(x,y).

I want to construct an array that considers all the possible combinations of x and y so that I can plot a square grid. Suppose that x and y are

a = [0,1,2]
b = [0,1,2]

then my array of interest is c = np.asarray(list(it.product(a,b)))

Using another complex number w = u+iv, I can also plot the following point by point: pole(x,y)*pole(u,v). I want to construct an array that considers all the possible combinations of z and w, and for this my attempt is

d = c.copy()
e = np.asarray(list(it.product(c,d)))

Up until this point, the code is working as intended especially if I consider an initial array a,b with only five elements. However, in the original problem, I need to consider an initial array a,b with at least 1000 elements. Worst, I need to consider pole(x,y)*pole(u,v)*pole(...)*pole(...) which means I get to have an array with (1000*1000)^4 elements.

What do you suggest I do to create an array that stores all the possible combinations of 2 parameters, 2x2 parameters, 2x2x2 parameters and so on?

I tried using np.meshgrid thinking it is more efficient than it.product but I am at lost after the first instance of

x,y = np.transpoe(np.meshgrid(a,b)).reshape(-1,2)

If I want to consider the next complex number w, I am not sure how to implement meshgrid.

I am also thinking of instead listing all the possible combinations of (x,y), (x,y) and (u,v), and so on, I could randomly pick from their combinations without constructing a huge list to conserve memory. However, I am not sure how to redefine this (documentation

def random_product(*args, **kwds):
    "Random selection from itertools.product(*args, **kwds)"
    pools = map(tuple, args) * kwds.get('repeat', 1)
    return tuple(random.choice(pool) for pool in pools)

to accommodate what I want.




Aucun commentaire:

Enregistrer un commentaire