braintools.tree.add#
- braintools.tree.add(tree1, tree2, is_leaf=<function is_quantity>)[source]#
Elementwise addition of PyTrees or a PyTree and a value.
- Parameters:
tree1 (
Quantity]) – Left operand.tree2 (
Quantity]|Array|ndarray|bool|number|bool|int|float|complex|Quantity) – Right operand. If a PyTree, its structure must match tree1 and corresponding leaves are added elementwise. If a value (scalar/array_like), it is broadcast to each leaf of 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 with elementwise sums.
- Return type:
PyTree
See also
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.add(tree, other) >>> {k: v.tolist() for k, v in out.items()} {'a': [11.0, 22.0], 'b': [33.0]} >>> >>> out = braintools.tree.add(tree, 1.0) >>> {k: v.tolist() for k, v in out.items()} {'a': [2.0, 3.0], 'b': [4.0]}