Skip to content

collect

Collect: rearrange a scalar expression so that a target vector appears on the left side of Dot.

This is a preprocessing step for integration by parts (ibp), which only matches Dot(target, ...) on the left.

Uses
  • Dot commutativity: Dot(a, b) = Dot(b, a)
  • Scalar triple product: Dot(a, Cross(b, c)) = Dot(b, Cross(c, a))
  • Orthogonal invariance: Dot(Ra, Rb) = Dot(a, b) for R in SO3

collect(expr, vec)

Collect expr with respect to vec.

Rearranges so vec appears on the left of every Dot that contains it.

Source code in geomech/core/math/collect.py
def collect(expr, vec):
    """Collect *expr* with respect to *vec*.

    Rearranges so *vec* appears on the left of every Dot that contains it.
    """
    match expr:
        case Add(nodes=nodes):
            return Add(*[collect(n, vec) for n in nodes])

        case Mul():
            return Mul(collect(expr.left, vec), collect(expr.right, vec))

        case Dot():
            return _collect_dot(expr, vec)

        case _:
            return expr