1

I try to create a meshgrid with n dimensions. Is there a nicer way to call meshgrid with n column vectors than with the if clause I am using?

Edit: The goal is to use it for user-defined n (2-100) without writing 100 if clauses.

The second line in the if clauses reduces the grid so column(n) < column(n+1)

Example:

import numpy as np
dimension = 2
range = np.arange(0.2,2.4,0.1)
if dimension == 2:
    grid = np.array(np.meshgrid(range,range)).T.reshape(-1,dimension)
    grid = np.array(grid[[i for i in range(grid.shape[0]) if grid[i,0]<grid[i,1]]])
 elif dimension == 3:
     grid = np.array(np.meshgrid(range,range,range)).T.reshape(-1,dimension)
     grid = np.array(grid[[i for i in range(grid.shape[0]) if grid[i,0]<grid[i,1]]])
     grid = np.array(grid[[i for i in range(grid.shape[0]) if grid[i,1]<grid[i,2]]])

Edit: The solution was posted below:

dimension = 2
r = np.arange(0.2,2.4,0.1)
grid=np.array(np.meshgrid(*[r]*n)).T.reshape(-1,n)

for i in range(0,n-1):
    grid = np.array([g for g in grid if g[i]<g[i+1]])
Franz
  • 15
  • 4
  • 1
    Are you just bothered by the `meshgrid(range, range)` v `meshgrid(range,range,range)`? I don't see, off hand, what the rest of your code does. – hpaulj Sep 16 '16 at 16:37
  • Possible duplicate of [Is there a multi-dimensional version of arange/linspace in numpy?](http://stackoverflow.com/questions/32208359/is-there-a-multi-dimensional-version-of-arange-linspace-in-numpy) – farenorth Sep 16 '16 at 16:50
  • It's generally a bad idea to overwrite native python routines, especially one as useful as `range`. – Chris Mueller Sep 16 '16 at 16:56
  • He's already generating the mult-dimensional grid with `meshgrid`. The 'duplicate' adds nothing that he isn't already doing. – hpaulj Sep 16 '16 at 16:57
  • @hpaulj: Yes, as it is one of the things I could not easily scale. After your comment I looked up numpy.repeat, which seems to be what I want for n-repeats or I just use your *[r]*n suggestion. – Franz Sep 17 '16 at 17:28
  • @ChrisMueller: Thanks for the tip! (I just copied a MWE and renamed my variables to make it clear what I am trying to do without thinking too much about it...) – Franz Sep 17 '16 at 17:28

1 Answers1

1

I haven't fully absorbed your approach and goals, but here's a partial simplification

In [399]: r=np.arange(3)           # simpler range for example
In [400]: grid=np.meshgrid(*[r]*2)   # use `[r]*3` for 3d case
In [401]: grid=np.array(grid).T.reshape(-1,2)
In [402]: np.array([g for g in grid if g[0]<g[1]])  # simpler comprehensions
Out[402]: 
array([[0, 1],
       [0, 2],
       [1, 2]])

itertools.product makes that 2 column grid easier:

In [403]: from itertools import product
In [404]: np.array([g for g in product(r,r) if g[0]<g[1]])
Out[404]: 
array([[0, 1],
       [0, 2],
       [1, 2]])

That is, your grid before filtering is

In [407]: grid
Out[407]: 
array([[0, 0],
       [0, 1],
       [0, 2],
       [1, 0],
       [1, 1],
       [1, 2],
       [2, 0],
       [2, 1],
       [2, 2]])

and product is

In [406]: list(product(r,r))
Out[406]: [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]

product has a repeat parameter that makes this even easier:

In [411]: list(product(r,repeat=2))
Out[411]: [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]

You still need the if clause to apply the 2 step filtering for dim=3. I guess the could written iteratively

for i in range(0,dimension-1):
   grid = [g for g in grid if g[i]<g[i+1]]
hpaulj
  • 221,503
  • 14
  • 230
  • 353