pyhs3.normalization.substitute_variable

pyhs3.normalization.substitute_variable(output, variable, replacement)[source]

Return output with variable replaced by replacement.

Only the nodes that depend on variable are rebuilt, each through its Op’s make_node so that its output type is inferred again from the new inputs; every other node is reused as it is.

This differs from PyTensor’s two built-in substitutions:

  • graph_replace rebuilds only the dependent nodes too, but keeps their original output types. Replacing an observable of unknown length (?,) by a (1,) bound therefore leaves an axis typed as unknown that holds length 1, which PyTensor refuses to broadcast at runtime (https://github.com/scipp-atlas/pyhs3/issues/346).

  • clone_replace(..., rebuild_strict=False) infers the types again but copies the whole graph, including the nodes that never depended on variable, so an antiderivative evaluated at two bounds carries two copies of its parameter-only parts.

Parameters:
  • output (TensorVar) – Expression to substitute into.

  • variable (TensorVar) – Variable to replace (e.g. an observable’s leaf vector).

  • replacement (TensorVar) – Variable to use instead; may have a different static shape, e.g. (1,) for (?,).

Return type:

TensorVar

Returns:

The substituted expression, or output itself if it does not depend on variable.