scalar_to_shaped_tensor#

sionna.phy.utils.scalar_to_shaped_tensor(inp: int | float | bool | torch.Tensor, dtype: torch.dtype, shape: List[int], device: str | None = None) torch.Tensor[source]#

Converts a scalar input to a tensor of specified shape, or validates and casts an existing input tensor.

If the input is a scalar, creates a tensor of the specified shape filled with that value. Otherwise, verifies the input tensor matches the required shape and casts it to the specified dtype.

Parameters:
  • inp (int | float | bool | torch.Tensor) – Input value. If scalar (int, float, bool, or shapeless tensor), it will be used to fill a new tensor. If a shaped tensor, its shape must match the specified shape.

  • dtype (torch.dtype) – Desired data type of the output tensor.

  • shape (List[int]) – Required shape of the output tensor.

  • device (str | None) – Device for computation. If None, device is used.

Outputs:

tensor – A tensor of shape shape and type dtype. Either filled with the scalar input value or the input tensor cast to the specified dtype.

Examples

import torch
from sionna.phy.utils import scalar_to_shaped_tensor

# From scalar
t = scalar_to_shaped_tensor(5.0, torch.float32, [2, 3])
print(t)
# tensor([[5., 5., 5.],
#         [5., 5., 5.]])

# From tensor
t = scalar_to_shaped_tensor(torch.ones(2, 3), torch.float64, [2, 3])
print(t.dtype)
# torch.float64