Skip to content

lie_bracket

from tensorsig.tensor_ops.products import lie_bracket

Compute the commutator [x, y] = x ⊗ y - y ⊗ x.

shared_dims has the same meaning and validation as in tensor_product.

Source code in src/tensorsig/tensor_ops/products.py
def lie_bracket(
    x: Tensor,
    y: Tensor,
    *,
    shared_dims: int = 0,
) -> Tensor:
    """Compute the commutator ``[x, y] = x ⊗ y - y ⊗ x``.

    ``shared_dims`` has the same meaning and validation as in
    [`tensor_product`][tensorsig.tensor_ops.products.tensor_product].
    """
    xy = tensor_product(x, y, shared_dims=shared_dims)
    yx = tensor_product(y, x, shared_dims=shared_dims)
    if xy.shape != yx.shape:
        raise ValueError(
            "Lie-bracket operands must produce matching product shapes; "
            f"got {tuple(xy.shape)} and {tuple(yx.shape)}."
        )
    return xy - yx