grid
¶
Returns the edges and node positions of a 2D grid graph of height x
width nodes, where each node is connected to itself and to its (up to
8) horizontal, vertical and diagonal neighbors.
Node i is at row i // width and column i % width; rows are
numbered from the top, so the position of node i is
(i % width, height - 1 - i // width).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
height
|
int
|
Number of rows of the grid. |
required |
width
|
int
|
Number of columns of the grid. |
required |
dtype
|
Dtype
|
Data type of the positions. Default: |
float32
|
Returns:
| Type | Description |
|---|---|
array
|
A tuple |
array
|
positions, of shape |
Example:
edge_index, pos = grid(2, 2)
# edge_index: every pair of the 4 nodes, including self-loops
# pos: mx.array([[0, 1], [1, 1], [0, 0], [1, 0]])
Source code in mlx_graphs/utils/generators.py
from mlx_graphs.utils import grid · mlx_graphs/utils/generators.py