braintools.tree module

braintools.tree module#

Operations for manipulating JAX PyTrees, including arithmetic, reshaping, and conversion utilities used across the toolkit. The functions map over the leaves of their PyTree arguments; the arithmetic and reduction functions treat brainunit.Quantity values as leaves by default, so physical units are preserved.

Arithmetic#

Leaf-wise arithmetic between PyTrees, or between a PyTree and a scalar.

scale

Multiply every leaf array in a PyTree by a value.

mul

Elementwise multiplication of a PyTree with a value or another PyTree.

shift

Add a value to every leaf array in a PyTree.

add

Elementwise addition of PyTrees or a PyTree and a value.

sub

Elementwise subtraction of two PyTrees.

Reductions#

Reduce one or two PyTrees to a single value.

dot

Inner product over all leaves of two PyTrees.

sum

Sum all elements across every leaf in a PyTree.

squared_norm

Sum of squares of all elements in a PyTree.

Structure and Indexing#

Combine, split, index, and reshape the leaves of a PyTree.

concat

Concatenate corresponding leaves from a sequence of PyTrees.

split

Split each leaf of a PyTree along axis 0 according to sizes.

idx

Index every leaf of a PyTree.

expand

Insert a length-1 axis into every leaf of a PyTree.

take

Take elements from every leaf of a PyTree along a given axis.

Conversion#

as_numpy

Convert all leaves of a PyTree to NumPy arrays.