import torch


def complex_mul(x, y):
    """Extend the elementwise product to complex
    tensors, i.e. tensors whose last shape has dimension of 2,
    representing real and imaginary part.

    Args:
        x (tensor): First operand
        y (tensor): Second operand
    """
    real = x[..., 0] * y[..., 0] - x[..., 1] * y[..., 1]
    imag = x[..., 1] * y[..., 0] + x[..., 0] * y[..., 1]
    return torch.stack([real, imag], dim=-1)