grid

dnois.utils.grid(n: Sequence[int], spacing: float | Tensor | Sequence[float | Tensor] = None, center: float | Tensor | Sequence[float | Tensor] = None, symmetric: bool = False, broadcast: bool = True, **kwargs) list[Tensor]

Create a len(n)-D evenly spaced grid.

>>> grid((2, 3), 0.1)
[tensor([[-0.10, -0.10, -0.10],
         [ 0.00,  0.00,  0.00]]),
 tensor([[-0.10,  0.00,  0.10],
         [-0.10,  0.00,  0.10]])]
>>> grid((2, 3), 0.1, 1.)
[tensor([[0.90, 0.90, 0.90],
         [1.00, 1.00, 1.00]]),
 tensor([[0.90, 1.00, 1.10],
         [0.90, 1.00, 1.10]])]
>>> grid((2, 3), (0.1, 0.2), (-1., 1.))
[tensor([[-1.10, -1.10, -1.10],
         [-1.00, -1.00, -1.00]]),
 tensor([[0.80, 1.00, 1.20],
         [0.80, 1.00, 1.20]])]
>>> grid((2, 3), torch.tensor([0.1, 0.2]), torch.tensor([1., 2.]))
[tensor([[[0.90, 0.90, 0.90],
          [1.00, 1.00, 1.00]],

         [[1.80, 1.80, 1.80],
          [2.00, 2.00, 2.00]]]),
 tensor([[[0.90, 1.00, 1.10],
          [0.90, 1.00, 1.10]],

         [[1.80, 2.00, 2.20],
          [1.80, 2.00, 2.20]]])]
>>> grid((2, 3), symmetric=True)
[tensor([[-0.50, -0.50, -0.50],
         [ 0.50,  0.50,  0.50]]),
 tensor([[-1.,  0.,  1.],
         [-1.,  0.,  1.]])]
>>> grid((2, 3), broadcast=False)
[tensor([[-1.],
         [ 0.]]),
 tensor([[-1.,  0.,  1.]])]
Parameters:
  • n (Sequence[int]) – Number of grid points in each dimension.

  • spacing (float or Tensor or Sequence[float | Tensor]) – Spacing between grid points in each dimension. A single float or 0D tensor indicates the spacing for all dimensions. Default: 1.

  • center (float or Tensor or Sequence[float | Tensor]) – Center of resulted grid points in each dimension. A single float or 0D tensor indicates the center for all dimensions. Default: 0.

  • symmetric (bool) – See interval(). Default: False.

  • broadcast (bool) – Whether to broadcast resulted tensors. Default: True.

  • kwargs – Tensor creation arguments passes to torch.linspace().

Returns:

A list of len(n)-D tensors if spacing and center are both scalars, otherwise of shape (*<broadcast shape of spacing and center>, *n). See above examples.

Return type:

list[Tensor]