问题陈述
需要将 N 维 MeshGrid 分割成“立方体”:
例如) 二维案例:
(-1,1) |(0,1) |(1,1)
(-1,0) |(0,0) |(1,0)
(-1,-1)|(0,-1)|(1,-1)
将有 4 个单元格,每个单元格有 2^D 个点:
我希望能够处理网格,将每个单元格的坐标点放入容器中以进行进一步处理。
Cells = [{(-1,1) (0,1)(-1,0),(0,0)},
{(0,1),(1,1),(0,0),(1,0)},
{(-1,0),(0,0)(-1,-1),(0,-1)}
{(0,0),(1,0)(0,-1),(1,-1)}]
我使用以下内容生成任意尺寸 d 的网格:
grid = [np.linspace(-1.0 , 1.0, num = K+1) for i in range(d)]
res_to_unpack = np.meshgrid(*grid,indexing = 'ij')
其中有输出:
[array([[-1., -1., -1.],
[ 0., 0., 0.],
[ 1., 1., 1.]]), array([[-1., 0., 1.],
[-1., 0., 1.],
[-1., 0., 1.]])]
所以我希望能够为给定的 D 维网格生成上述单元格容器。根据给定的 K(2 的幂)进行分割。
我需要这个容器,因此对于每个单元格,我需要引用所有关联的 2^D 点并计算距原点的距离。
编辑澄清
K 应该将网格划分为 K ** D 个单元格,其中有 (K+1) ** D 个点。每个单元格应该有 2 ** D 个点数。每个“细胞”的体积为 (2/K)^D。
因此对于 K = 4,D = 2
Cells = [ {(-1,1),(-0.5,1),(-1,0.5),(-0.5,0.5)},
{(-0.5,1),(-0.5,0.5)(0.0,1.0),(0,0.5)},
...
{(0.0,-0.5),(0.5,-0.5),(0.0,-1.0),(0.5,-1.0)},
{(0.5,-1.0),(0.5,-1.0),(1.0,-0.5),(1.0,-1.0)}]
这是左上、左上 + 右上、左下、左下 + 左上的输出。该集合中有 16 个单元格,每个单元格有 4 个坐标。为了增加 K,假设 K = 8。将有 64 个单元格,每个单元格有四个点。
这应该可以满足您的需求:
from itertools import product
import numpy as np
def splitcubes(K, d):
coords = [np.linspace(-1.0 , 1.0, num=K + 1) for i in range(d)]
grid = np.stack(np.meshgrid(*coords)).T
ks = list(range(1, K))
for slices in product(*([[slice(b,e) for b,e in zip([None] + ks, [k+1 for k in ks] + [None])]]*d)):
yield grid[slices]
def cubesets(K, d):
if (K & (K - 1)) or K < 2:
raise ValueError('K must be a positive power of 2. K: %s' % K)
return [set(tuple(p.tolist()) for p in c.reshape(-1, d)) for c in splitcubes(K, d)]
这是 2D 案例的一些演示:
import matplotlib.pyplot as plt
def assemblecube(c, spread=.03):
c = np.array(list(c))
c = c[np.lexsort(c.T[::-1])]
d = int(np.log2(c.size))
for i in range(d):
c[2**i:2**i + 2] = c[2**i + 1:2**i - 1:-1]
# get the point farthest from the origin
sp = c[np.argmax((c**2).sum(axis=1)**.5)]
# shift all points a small distance towards that farthest point
c += sp * .1 #np.copysign(np.ones(sp.size)*spread, sp)
# create several different orderings of the same points so that matplotlib will draw a closed shape
return [(np.roll(c, i, axis=1) - (np.roll(c, i, axis=1)[0] - c[0])[None,:]).T for i in range(d)]
fig = plt.figure(figsize=(6,6))
ax = fig.gca()
for i,c in enumerate(cubesets(4, 2)):
for cdata in assemblecube(c):
p = ax.plot(*cdata, c='C%d' % (i % 9))
ax.set_aspect('equal', 'box')
fig.show()
输出:
出于可视化目的,立方体已稍微分开(因此它们不会重叠并相互覆盖)。
3D 案例也是如此:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(figsize=(6,6))
ax = fig.add_subplot(111, projection='3d')
for i,c in enumerate(cubesets(2,3)):
for cdata in assemblecube(c, spread=.05):
ax.plot(*cdata, c=('C%d' % (i % 9)))
plt.gcf().gca().set_aspect('equal', 'box')
plt.show()
输出:
K=4
这是与上面相同的 2D 和 3D 演示的输出,但带有
K=4
:
谢谢@tel
有什么方法可以使您的宏伟示例适应每个轴划分不均匀的 4D 网格的情况吗?
我希望获取在 4D 中使用网格网格生成的所有 4D 超立方体的坐标。问题是我的轴划分不规则。例如:
x = [0,1,2,3,5]
y = [0,1,2,3]
z = [0,1,2,3,4]
w = [0,1,2,3,5,6]
我的想法是使用这些细分生成 4D 网格。然后我想获得由这些生成的所有 4D 超立方体,最后获得每个生成的超立方体的 4D 坐标(顶点位置)。
无需可视化(因此顶点不必排序),只需获取该网格生成的超立方体的顶点即可。修改你的代码真是太棒了!谢谢