spekk.transformations.reduce.Reduce#

class spekk.transformations.reduce.Reduce(dimension: str, reduce_fn: ~typing.Callable[[~spekk.transformations.reduce.T_reduction_result, ~spekk.transformations.reduce.T_f_result], ~spekk.transformations.reduce.T_reduction_result], initial_value: ~spekk.transformations.reduce.T_reduction_result | None = None, enumerate: bool = False, extra_args: ~typing.Sequence[~typing.Any] = <factory>, extra_kwargs: ~typing.Dict[str, ~typing.Any] = <factory>, reduce_impl: ~typing.Callable[[~typing.Callable[[~spekk.transformations.reduce.T_reduction_result, ~spekk.transformations.reduce.T_f_result], ~spekk.transformations.reduce.T_reduction_result], ~typing.Iterable, ~spekk.transformations.reduce.T_reduction_result], ~spekk.transformations.reduce.T_reduction_result] | None = None)[source]#

Bases: Transformation

Transform a function to make it reduce the values of a dimension iteratively.

A Reduce transformation is generally a ForAll and Apply transformation combined, if the Apply transformation somehow aggregates the result (for example by summing over the vectorized axis).

As a concrete example: ForAll("transmits") followed by Apply(np.sum, Axis("transmits") is equivalent to Reduce.Sum("transmits"), but using Reduce.Sum will likely allocate a lot less memory, potentially at the cost of processing time.

__init__(dimension: str, reduce_fn: ~typing.Callable[[~spekk.transformations.reduce.T_reduction_result, ~spekk.transformations.reduce.T_f_result], ~spekk.transformations.reduce.T_reduction_result], initial_value: ~spekk.transformations.reduce.T_reduction_result | None = None, enumerate: bool = False, extra_args: ~typing.Sequence[~typing.Any] = <factory>, extra_kwargs: ~typing.Dict[str, ~typing.Any] = <factory>, reduce_impl: ~typing.Callable[[~typing.Callable[[~spekk.transformations.reduce.T_reduction_result, ~spekk.transformations.reduce.T_f_result], ~spekk.transformations.reduce.T_reduction_result], ~typing.Iterable, ~spekk.transformations.reduce.T_reduction_result], ~spekk.transformations.reduce.T_reduction_result] | None = None) None#

Methods

Product(dimension[, initial_value, reduce_impl])

Transformation that iteratively multiplies the results of the wrapped function for each item in the given dimension.

Sum(dimension[, initial_value, reduce_impl])

Transformation that iteratively adds the results of the wrapped function for each item in the given dimension.

__init__(dimension, reduce_fn[, ...])

transform_function(to_be_transformed, ...)

Transform the wrapped function given the spec of the input arguments and the spec of the returned value of the wrapped function.

transform_input_spec(spec)

Return a new spec that represent the input arguments that are passed down to the wrapped function after the transformation has been applied.

transform_output_spec(spec)

Return a new spec that represent the returned value of the final transformed function.

Attributes

enumerate

If True, each item is a tuple of (index, value), similar to Python's built-in enumerate().

initial_value

The initial value for the reduction.

reduce_impl

The reduce implementation to use.

dimension

The dimension to reduce over.

reduce_fn

The function to reduce with.

extra_args

Extra arguments to pass to the reduce function.

extra_kwargs

Extra keyword arguments to pass to the reduce function.

classmethod Product(dimension: str, initial_value: T_reduction_result | None = None, reduce_impl: Callable[[Callable[[T_reduction_result, T_f_result], T_reduction_result], Iterable, T_reduction_result], T_reduction_result] | None = None) T_reduce_cls[source]#

Transformation that iteratively multiplies the results of the wrapped function for each item in the given dimension.

classmethod Sum(dimension: str, initial_value: T_reduction_result | None = None, reduce_impl: Callable[[Callable[[T_reduction_result, T_f_result], T_reduction_result], Iterable, T_reduction_result], T_reduction_result] | None = None) T_reduce_cls[source]#

Transformation that iteratively adds the results of the wrapped function for each item in the given dimension.

dimension: str#

The dimension to reduce over.

enumerate: bool = False#

If True, each item is a tuple of (index, value), similar to Python’s built-in enumerate().

extra_args: Sequence[Any]#

Extra arguments to pass to the reduce function.

extra_kwargs: Dict[str, Any]#

Extra keyword arguments to pass to the reduce function.

initial_value: T_reduction_result | None = None#

The initial value for the reduction. For example 0 for summation.

reduce_fn: Callable[[T_reduction_result, T_f_result], T_reduction_result]#

The function to reduce with. For example operator.add for summation.

reduce_impl: Callable[[Callable[[T_reduction_result, T_f_result], T_reduction_result], Iterable, T_reduction_result], T_reduction_result] | None = None#

The reduce implementation to use. Defaults to Python’s built-in functools.reduce().

transform_function(to_be_transformed: callable, input_spec: Spec, output_spec: Spec) callable[source]#

Transform the wrapped function given the spec of the input arguments and the spec of the returned value of the wrapped function.

transform_input_spec(spec: Spec) Spec[source]#

Return a new spec that represent the input arguments that are passed down to the wrapped function after the transformation has been applied.

For example, if the transformation vectorizes the wrapped function over a dimension, the wrapped function would only see single items of the dimension at a time. Therefore, the input spec will have one less dimension when passed down to the wrapped function.

transform_output_spec(spec: Spec) Spec[source]#

Return a new spec that represent the returned value of the final transformed function.

For example, if the transformation sums over a dimension of the result of calling the wrapped function, the output spec will have one less dimension.