def view_as_complex(x):
    """Converts a real tensor with shape `(..., 2)` to a complex tensor,
    where the last dimension represents the real and imaginary components
    of a complex tensor.

    Args:
        x: A real tensor with last dimension of size 2.

    Returns:
        A complex tensor with shape `x.shape[:-1]`.

    Example:

    ```
    >>> import numpy as np
    >>> from keras import ops

    >>> real_imag = np.array([[1.0, 2.0], [3.0, 4.0]])
    >>> complex_tensor = ops.view_as_complex(real_imag)
    >>> complex_tensor
    array([1.+2.j, 3.+4.j])
    ```
    """
    if any_symbolic_tensors((x,)):
        return ViewAsComplex().symbolic_call(x)

    x = backend.convert_to_tensor(x)
    if len(x.shape) < 1 or x.shape[-1] != 2:
        raise ValueError(
            "Last dimension of input must be size 2 (real and imaginary). "
            f"Received shape: {x.shape}"
        )
    real_part = x[..., 0]
    imag_part = x[..., 1]

    return backend.cast(real_part, dtype="complex64") + 1j * backend.cast(
        imag_part, dtype="complex64"
    )
Converts a real tensor with shape `(..., 2)` to a complex tensor,
    where the last dimension represents the real and imaginary components
    of a complex tensor.

    Args:
        x: A real tensor with last dimension of size 2.

    Returns:
        A complex tensor with shape `x.shape[:-1]`.

    Example:

    ```
    >>> import numpy as np
    >>> from keras import ops

    >>> real_imag = np.array([[1.0, 2.0], [3.0, 4.0]])
    >>> complex_tensor = ops.view_as_complex(real_imag)
    >>> complex_tensor
    array([1.+2.j, 3.+4.j])
    ```
    
keras/src/ops/math.py