braintools.tree.sum

Contents

braintools.tree.sum#

braintools.tree.sum(tree, is_leaf=<function is_quantity>)[source]#

Sum all elements across every leaf in a PyTree.

Parameters:
  • tree (Quantity]) – Input PyTree whose elements are reduced.

  • is_leaf (Callable[[Any], bool] | None) – Predicate to treat a node as a leaf during traversal. Defaults to u.math.is_quantity.

Returns:

Scalar sum across all leaves and their elements.

Return type:

Array

See also

dot

Inner product across leaves.

squared_norm

Sum of squares across leaves.

Examples

>>> import jax.numpy as jnp
>>> import braintools
>>> tree = {'a': jnp.array([1.0, 2.0]), 'b': jnp.array([3.0])}
>>> float(braintools.tree.sum(tree))
6.0