capturegraph.data.containers.utilities.vectorize
#
vectorize - Decorator for broadcasting Lists over function parameters.#
A decorator that allows ordinary functions to accept Lists as arguments, automatically broadcasting and reshaping the output.
Example
from capturegraph.data import vectorize, List
@vectorize
def distance(a: float, b: float) -> float:
return abs(a - b)
# Call with scalars (unchanged behavior)
distance(10, 3) # → 7
# Call with Lists (broadcasted)
distance(List([1, 2, 3]), List([10, 20, 30])) # → List([9, 18, 27])
# Mixed scalars and Lists (broadcast scalars)
distance(List([1, 2, 3]), 0) # → List([1, 2, 3])
Broadcasting Rules
- Same length: All Lists must have the same length
- Single-element: Lists with one element are repeated to match
- Scalars: Non-list values are repeated for every row
Notes
- Works with both positional and keyword arguments
- Preserves the original function when all arguments are scalars
- Returns a List when any argument is a List
vectorize(fn)
#
Decorator to broadcast List arguments over a scalar function.
Wraps a function that takes scalar arguments so it can also accept Lists. When called with Lists, the function is applied element-wise and results are collected into a List.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
Callable
|
A callable that operates on scalar values. |
required |
Returns:
| Type | Description |
|---|---|
Callable
|
A wrapped function that accepts both scalars and Lists. |