archimedes.tree.all¶

archimedes.tree.all(tree: PyTree, is_leaf: Callable[[Any], bool] | None = None) bool¶

Check if all leaves in the pytree evaluate to True.

This function traverses the pytree and checks if all leaf values are truthy, similar to Python’s built-in all() function but operating on all leaves of a pytree.

Parameters:
  • tree (PyTree) – A pytree to check. A pytree is a nested structure of containers (lists, tuples, dicts) and leaves (arrays, scalars, objects not registered as pytrees).

  • is_leaf (callable, optional) – A function that takes a pytree node as input and returns a boolean indicating whether it should be considered a leaf. If not provided, the default leaf types (arrays and scalars) are used.

Returns:

result – True if all leaves in the pytree evaluate to True, False otherwise.

Return type:

bool

Notes

When to use:

  • To check if all values in a structured object meet a certain condition (after applying map() with a condition function)

  • To validate that all components of a model or state are initialized

  • As a convenient way to check properties across nested structures

Examples

>>> import archimedes as arc
>>> import numpy as np
>>>
>>> # Check if all values are positive
>>> data = {"a": np.array([1.0, 2.0]), "b": {"c": np.array([3.0])}}
>>> is_positive = arc.tree.map(lambda x: x > 0, data)
>>> all_positive = arc.tree.all(is_positive)
>>> print(all_positive)
True
>>>
>>> # Check if all values are greater than 1.5
>>> is_greater = arc.tree.map(lambda x: x > 1.5, data)
>>> all_greater = arc.tree.all(is_greater)
>>> print(all_greater)
False

See also

map

Apply a function to each leaf in a pytree

leaves

Extract just the leaf values from a pytree