def perpendicular_vector(vec):
    """Return a vector perpendicular to ``vec``.

    Parameters
    ----------
    vec : `array-like`
        Vector(s) of arbitrary length. The axis along the vector components
        must come last.

    Returns
    -------
    perp_vec : `numpy.ndarray`
        Array of same shape as ``vec`` such that ``dot(vec, perp_vec) == 0``
        (along the last axis if there are multiple vectors).

    Examples
    --------
    Works in 2d:

    >>> perpendicular_vector([0, 1])
    array([-1.,  0.])
    >>> np.allclose(perpendicular_vector([1, 0]), [0, 1])  # would print -0
    True

    And in 3d:

    >>> perpendicular_vector([0, 1, 0])
    array([-1.,  0.,  0.])
    >>> perpendicular_vector([0, 0, 1])
    array([ 1.,  0.,  0.])
    >>> np.allclose(perpendicular_vector([1, 0, 0]), [0, 1, 0])
    True

    The function is vectorized, i.e., it can be called with multiple
    vectors at once (additional axes being added to the left):

    >>> perpendicular_vector([[0, 1, 0],
    ...                       [0, 0, 1]])  # 2 vectors
    array([[-1.,  0.,  0.],
           [ 1.,  0.,  0.]])
    >>> vecs = np.zeros((2, 3, 3))
    >>> vecs[..., 1] = 1  # (2, 3) array of vectors (0, 1, 0)
    >>> perpendicular_vector(vecs)
    array([[[-1.,  0.,  0.],
            [-1.,  0.,  0.],
            [-1.,  0.,  0.]],
    <BLANKLINE>
           [[-1.,  0.,  0.],
            [-1.,  0.,  0.],
            [-1.,  0.,  0.]]])
    """
    squeeze_out = (np.ndim(vec) == 1)
    vec = np.array(vec, dtype=float, copy=False, ndmin=2)

    if np.any(np.all(vec == 0, axis=-1)):
        raise ValueError('zero vector')

    result = np.zeros(vec.shape)
    cond = np.any(vec[..., :2] != 0, axis=-1)
    result[cond, 0] = -vec[cond, 1]
    result[cond, 1] = vec[cond, 0]
    result[~cond, 0] = 1
    result /= np.linalg.norm(result, axis=-1, keepdims=True)

    if squeeze_out:
        result = result.squeeze()

    return result
Return a vector perpendicular to ``vec``.

    Parameters
    ----------
    vec : `array-like`
        Vector(s) of arbitrary length. The axis along the vector components
        must come last.

    Returns
    -------
    perp_vec : `numpy.ndarray`
        Array of same shape as ``vec`` such that ``dot(vec, perp_vec) == 0``
        (along the last axis if there are multiple vectors).

    Examples
    --------
    Works in 2d:

    >>> perpendicular_vector([0, 1])
    array([-1.,  0.])
    >>> np.allclose(perpendicular_vector([1, 0]), [0, 1])  # would print -0
    True

    And in 3d:

    >>> perpendicular_vector([0, 1, 0])
    array([-1.,  0.,  0.])
    >>> perpendicular_vector([0, 0, 1])
    array([ 1.,  0.,  0.])
    >>> np.allclose(perpendicular_vector([1, 0, 0]), [0, 1, 0])
    True

    The function is vectorized, i.e., it can be called with multiple
    vectors at once (additional axes being added to the left):

    >>> perpendicular_vector([[0, 1, 0],
    ...                       [0, 0, 1]])  # 2 vectors
    array([[-1.,  0.,  0.],
           [ 1.,  0.,  0.]])
    >>> vecs = np.zeros((2, 3, 3))
    >>> vecs[..., 1] = 1  # (2, 3) array of vectors (0, 1, 0)
    >>> perpendicular_vector(vecs)
    array([[[-1.,  0.,  0.],
            [-1.,  0.,  0.],
            [-1.,  0.,  0.]],
    <BLANKLINE>
           [[-1.,  0.,  0.],
            [-1.,  0.,  0.],
            [-1.,  0.,  0.]]])