Accumulate¶
This example implements and tests a generic accumulation operator which applies and accumulates a lambda function
across a specified axis of the input tensor. This generic accumulation operator is used to define the cumsum
and
cumprod
functions, which are equivalent to those defined for numpy.
-
accumulate
(x, inner_fcn=None, axis=None)¶ Define the operator function.
Parameters: - x – The input tensor
- inner_fcn – a lambda function to be applied for accumulation
- axis – The axis across which accumulation will be applied
Returns: The accumulated result
-
cumsum
(x, axis=0)[source]¶ Define the cumsum operator by defining the inner_fcn to be addition
\[y_{n_0,n_1,n_2,n_3} = \sum_{n_2=0}^{n_2} x_{n_0,n_1,n_2,n_3}\]where \(n_2\) is the index over which to perform the addition.
Parameters: - x – the input tensor
- axis – the accumulation axis
Returns: the cumulative sum across the accumulation axis
Examples: >>> import numpy >>> from opveclib import evaluate >>> from opveclib.examples import cumsum >>> a = numpy.arange(1, 6) >>> evaluate(cumsum(a)) array([ 1, 3, 6, 10, 15]) >>> b = numpy.arange(1,16).reshape(3,5) >>> evaluate(cumsum(b, axis=0)) array([[ 1, 2, 3, 4, 5], [ 7, 9, 11, 13, 15], [18, 21, 24, 27, 30]])
-
cumprod
(x, axis=0)[source]¶ Define the cumprod operator by defining the inner_fcn to be multiplication
\[y_{n_0,n_1,n_2,n_3} = \prod_{n_2=0}^{n_2} x_{n_0,n_1,n_2,n_3}\]where \(n_2\) is the index over which to perform the multipilication.
Parameters: - x – the input tensor
- axis – the accumulation axis
Returns: the cumulative product across the accumulation axis
Examples: >>> import numpy >>> from opveclib import evaluate >>> from opveclib.examples import cumprod >>> a = numpy.arange(1, 6) >>> evaluate(cumprod(a)) array([ 1, 2, 6, 24, 120]) >>> b = numpy.arange(1,16).reshape(3,5) >>> evaluate(cumprod(b, axis=0)) array([[ 1, 2, 3, 4, 5], [ 6, 14, 24, 36, 50], [ 66, 168, 312, 504, 750]])