braintools.tree.mul#
- braintools.tree.mul(tree, x, is_leaf=<function is_quantity>)[source]#
Elementwise multiplication of a PyTree with a value or another PyTree.
- Parameters:
tree (
Quantity]) – Left operand; PyTree whose leaves are multiplied.x (
Quantity]|Array|ndarray|bool|number|bool|int|float|complex|Quantity) – Right operand. If a PyTree, its structure must match tree and corresponding leaves are multiplied elementwise. If a value (scalar/array_like), it is broadcast to each leaf of tree.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 products.
- Return type:
PyTree
See also
Notes
When x is a PyTree, both tree and x must have identical PyTree structures. Broadcasting applies per-leaf when x is array_like.
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.mul(tree, other) >>> {k: v.tolist() for k, v in out.items()} {'a': [10.0, 40.0], 'b': [90.0]} >>> >>> out = braintools.tree.mul(tree, 3.0) >>> {k: v.tolist() for k, v in out.items()} {'a': [3.0, 6.0], 'b': [9.0]}