import torch


def to_cartesian(x: torch.Tensor) -> torch.Tensor:
    """
    Transform a complex-valued tensor from polar to cartesian coordinates.

    Args:
        x: Tensor whose last dimension contains the magnitude and phase.

    Returns:
        x in cartesian coordinates. x[...,0] contains the real part and x[...,1] contains the imaginary part.
    """
    r = x[...,0]
    phi = x[...,1]
    real = r * phi.cos()
    imag = r * phi.sin()
    return torch.stack((real, imag), dim=-1)