pangolin.interface.vfor#
- pangolin.interface.vfor(fun=None, /, **sizes)#
Vectorize a function over named integer ranges.
funmust have a fixed positional signature. Each parameter receives a scalarRVrepresenting an integer coordinate from zero through one less than its named size.funmust return a PyTree of newly createdRVobjects; it cannot return a coordinate or an existing RV captured through a closure. With no coordinate parameters,funis called directly and its RV outputs need not be newly created.Axis sizes are matched to function parameters by name. Output axes occur in function-parameter order, regardless of the order in which keyword arguments are supplied.
vforsupports direct calls, decorator factories, and multiline decorator syntax.- Parameters:
fun (Callable[..., PyTree[RV]] | None) – Function to vectorize, or
Nonewhen creating a decorator. It must have a fixed positional signature, accept one scalarRVcoordinate per parameter, and return a PyTree of newly created RV objects. No parameter may be namedinterface.**sizes (int) – Positive integer size for every parameter of
fun, supplied under the corresponding parameter name.
- Returns:
output_or_decorator – The vectorized output PyTree when
funis supplied, otherwise a decorator that acceptsfunand produces that output.- Return type:
PyTree[RV] | Callable[[Callable[…, PyTree[RV]]], PyTree[RV]]
- Raises:
TypeError – If
funis not callable, has no fixed positional signature, a size is not an integer, or does not return a PyTree ofRVobjects.ValueError – If an axis size is missing, an unknown axis name is supplied, a size is not positive, a parameter is named
interface, or an output RV was not created byfun.
Notes
Static type checking preserves the number of coordinate arguments and exact PyTree structure of the output, and requires the first six arguments to be
RVobjects. Runtime validation still applies to every argument.Examples
Direct call:
>>> from pangolin import ir >>> from pangolin.interface import vfor >>> x = vfor(lambda i, j: 10 * i + j, i=2, j=3) >>> x.shape (2, 3)
Keyword order does not affect axis order:
>>> y = vfor(lambda i, j: 10 * i + j, j=3, i=2) >>> y.shape (2, 3) >>> ir.rv_equal(x, y) True
Decorator factory:
>>> make_x = vfor(i=2, j=3) >>> x = make_x(lambda i, j: 10 * i + j) >>> x.shape (2, 3)
Multiline decorator form:
>>> @vfor(i=2, j=3) ... def x(i, j): ... return 10 * i + j >>> x.shape (2, 3)
Every function parameter must have a named size:
>>> vfor(lambda i, j: i + j, i=2) Traceback (most recent call last): ... ValueError: vfor missing size for axis 'j'
Unknown axis names are rejected:
>>> vfor(lambda i, j: i + j, i=2, j=3, k=4) Traceback (most recent call last): ... ValueError: vfor got size for unknown axis 'k'; expected ('i', 'j')
Python defaults are not interpreted as axis sizes:
>>> vfor(lambda i=3: i + 1) Traceback (most recent call last): ... TypeError: <lambda> has no fixed positional signature: 'i' has a default