braintools.tree.shift

Contents

braintools.tree.shift#

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

Add a value to every leaf array in a PyTree.

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

  • x (Array | ndarray | bool | number | bool | int | float | complex | Quantity) – Value broadcast and added 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 each leaf shifted by x.

Return type:

PyTree

See also

add

Elementwise addition with a PyTree or value.

sub

Elementwise subtraction of PyTrees.

Examples

>>> import jax.numpy as jnp
>>> import braintools
>>> tree = {'a': jnp.array([1.0, 2.0]), 'b': jnp.array([3.0])}
>>> out = braintools.tree.shift(tree, 1.0)
>>> {k: v.tolist() for k, v in out.items()}
{'a': [2.0, 3.0], 'b': [4.0]}