Home > OS >  Finding the coordinates of cubes or hypercubes in n-dimensional space using NumPy meshgrid
Finding the coordinates of cubes or hypercubes in n-dimensional space using NumPy meshgrid

Time:07-14


So I can't seem to cross the finish line on a problem I am having.
I am trying to define a n-dimensional coordinate space then partition this space into a k**d set of d-dimensional cells where d is the dimensionality and k is the number of bins being used to partition the axes.
If I generate a set of boundaries for the cells using the following:

points = []
n_bins = 3
for dim in range(3):
    boundary = np.linspace(0, 2, n_bins 1)
    points.append(boundary)

For a 3-dimensional problem this is repeated three times in a for loop as in actuality each of the dimensions has a different range to be binned.
Then the meshgrid is created as follows:

points = np.array(points)
grid = np.stack(np.meshgrid(*points)).T

This appears to generate the necessary combinations of points that can be used to find the coordinates of each cube as illustrated below. However, I am completely stuck on how to extract the coordinates of the 9 cubes for this example case.

Since I am stuck on this simpler problem I can't even begin to tackle how to extract the coordinates of the hypercubes in a higher dimensional space. Would anyone be able to provide some assistance on this?
Apologies if I have not provided enough detail.

[[[[0. 0. 0.]
   [0. 1. 0.]
   [0. 2. 0.]
   [0. 3. 0.]]

  [[1. 0. 0.]
   [1. 1. 0.]
   [1. 2. 0.]
   [1. 3. 0.]]

  [[2. 0. 0.]
   [2. 1. 0.]
   [2. 2. 0.]
   [2. 3. 0.]]

  [[3. 0. 0.]
   [3. 1. 0.]
   [3. 2. 0.]
   [3. 3. 0.]]]


 [[[0. 0. 1.]
   [0. 1. 1.]
   [0. 2. 1.]
   [0. 3. 1.]]

  [[1. 0. 1.]
   [1. 1. 1.]
   [1. 2. 1.]
   [1. 3. 1.]]

  [[2. 0. 1.]
   [2. 1. 1.]
   [2. 2. 1.]
   [2. 3. 1.]]

  [[3. 0. 1.]
   [3. 1. 1.]
   [3. 2. 1.]
   [3. 3. 1.]]]


 [[[0. 0. 2.]
   [0. 1. 2.]
   [0. 2. 2.]
   [0. 3. 2.]]

  [[1. 0. 2.]
   [1. 1. 2.]
   [1. 2. 2.]
   [1. 3. 2.]]

  [[2. 0. 2.]
   [2. 1. 2.]
   [2. 2. 2.]
   [2. 3. 2.]]

  [[3. 0. 2.]
   [3. 1. 2.]
   [3. 2. 2.]
   [3. 3. 2.]]]


 [[[0. 0. 3.]
   [0. 1. 3.]
   [0. 2. 3.]
   [0. 3. 3.]]

  [[1. 0. 3.]
   [1. 1. 3.]
   [1. 2. 3.]
   [1. 3. 3.]]

  [[2. 0. 3.]
   [2. 1. 3.]
   [2. 2. 3.]
   [2. 3. 3.]]

  [[3. 0. 3.]
   [3. 1. 3.]
   [3. 2. 3.]
   [3. 3. 3.]]]]

CodePudding user response:

OK, third times the charm.

I produce a list of the split points on each axis. For an 8 cube in 3 parts, that produces [0,3,6,8]. I then do an offset zip of that list to produce the begin/end points: [0,3],[3,6],[6,8]]

All combinations of those three things produce the set of cubes. Printing the coordinates is then just a matter of another product to combine the start and end points:

import itertools
import numpy as np
cube = 8
n_bins = 3

pts = list(range(0,cube,n_bins))   [cube]
print(pts)
pairs = list(zip(pts[:-1],pts[1:]))
print(pairs)


for p1 in itertools.product( *([pairs]*3) ):
    print("Subcube:")
    for p2 in itertools.product(*p1):
        print(p2)

Output:

[0, 3, 6, 8]
[(0, 3), (3, 6), (6, 8)]
Subcube:
(0, 0, 0)
(0, 0, 3)
(0, 3, 0)
(0, 3, 3)
(3, 0, 0)
(3, 0, 3)
(3, 3, 0)
(3, 3, 3)
Subcube:
(0, 0, 3)
(0, 0, 6)
(0, 3, 3)
(0, 3, 6)
(3, 0, 3)
(3, 0, 6)
(3, 3, 3)
(3, 3, 6)
Subcube:
(0, 0, 6)
(0, 0, 8)
(0, 3, 6)
(0, 3, 8)
(3, 0, 6)
(3, 0, 8)
(3, 3, 6)
(3, 3, 8)
Subcube:
(0, 3, 0)
(0, 3, 3)
(0, 6, 0)
(0, 6, 3)
(3, 3, 0)
(3, 3, 3)
(3, 6, 0)
(3, 6, 3)
Subcube:
(0, 3, 3)
(0, 3, 6)
(0, 6, 3)
(0, 6, 6)
(3, 3, 3)
(3, 3, 6)
(3, 6, 3)
(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