braintools.tree.sub

Contents

braintools.tree.sub#

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

Elementwise subtraction of two PyTrees.

Parameters:
  • tree1 (Quantity]) – Minuend PyTree.

  • tree2 (Quantity]) – Subtrahend PyTree. Must share the same PyTree structure as tree1.

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

Returns:

PyTree where each leaf is tree1 - tree2 elementwise.

Return type:

PyTree

See also

add

Elementwise addition of PyTrees.

mul

Elementwise multiplication.

Examples

>>> import jax.numpy as jnp
>>> import braintools
>>> tree = {'a': jnp.array([1.0, 2.0]), 'b': jnp.array([3.0])}
>>> other = {'a': jnp.array([10.0, 20.0]), 'b': jnp.array([30.0])}
>>> out = braintools.tree.sub(other, tree)
>>> {k: v.tolist() for k, v in out.items()}
{'a': [9.0, 18.0], 'b': [27.0]}