larray.sequence

larray.sequence(axis, initial=0, inc=None, mult=1, func=None, axes=None, title='')[source]

Creates an array by sequentially applying modifications to the array along axis.

The value for each label in axis will be given by sequentially transforming the value for the previous label. This transformation on the previous label value consists of applying the function “func” on that value if provided, or to multiply it by mult and increment it by inc otherwise.

Parameters:

axis : axis definition (Axis, str, int)

Axis along which to apply mod. An axis definition can be passed as a string. An int will be interpreted as the length for a new anonymous axis.

initial : scalar or LArray, optional

Value for the first label of axis. Defaults to 0.

inc : scalar, LArray, optional

Value to increment the previous value by. Defaults to 0 if mult is provided, 1 otherwise.

mult : scalar, LArray, optional

Value to multiply the previous value by. Defaults to 1.

func : function/callable, optional

Function to apply to the previous value. Defaults to None. Note that this is much slower than using inc and/or mult.

axes : int, tuple of int or tuple/list/AxisCollection of Axis, optional

Axes of the result. Defaults to the union of axes present in other arguments.

title : str, optional

Title.

Examples

>>> year = Axis('year=2016..2019')
>>> sex = Axis('sex=M,F')
>>> sequence(year)
year  2016  2017  2018  2019
         0     1     2     3
>>> sequence('year=2016..2019')
year  2016  2017  2018  2019
         0     1     2     3
>>> sequence(year, 1.0, 0.5)
year  2016  2017  2018  2019
       1.0   1.5   2.0   2.5
>>> sequence(year, 1.0, mult=1.5)
year  2016  2017  2018   2019
       1.0   1.5  2.25  3.375
>>> inc = LArray([1, 2], [sex])
>>> inc
sex  M  F
     1  2
>>> sequence(year, 1.0, inc)
sex\year  2016  2017  2018  2019
       M   1.0   2.0   3.0   4.0
       F   1.0   3.0   5.0   7.0
>>> mult = LArray([2, 3], [sex])
>>> mult
sex  M  F
     2  3
>>> sequence(year, 1.0, mult=mult)
sex\year  2016  2017  2018  2019
       M   1.0   2.0   4.0   8.0
       F   1.0   3.0   9.0  27.0
>>> initial = LArray([3, 4], [sex])
>>> initial
sex  M  F
     3  4
>>> sequence(year, initial, 1)
sex\year  2016  2017  2018  2019
       M     3     4     5     6
       F     4     5     6     7
>>> sequence(year, initial, mult=2)
sex\year  2016  2017  2018  2019
       M     3     6    12    24
       F     4     8    16    32
>>> sequence(year, initial, inc, mult)
sex\year  2016  2017  2018  2019
       M     3     7    15    31
       F     4    14    44   134
>>> def modify(prev_value):
...     return prev_value / 2
>>> sequence(year, 8, func=modify)
year  2016  2017  2018  2019
         8     4     2     1
>>> sequence(3)
{0}*  0  1  2
      0  1  2
>>> sequence(X.year, axes=(sex, year))
sex\year  2016  2017  2018  2019
       M     0     1     2     3
       F     0     1     2     3

sequence can be used as the inverse of growth_rate:

>>> a = LArray([1.0, 2.0, 3.0, 3.0], year)
>>> a
year  2016  2017  2018  2019
       1.0   2.0   3.0   3.0
>>> g = a.growth_rate() + 1
>>> g
year  2017  2018  2019
       2.0   1.5   1.0
>>> sequence(year, a[2016], mult=g)
year  2016  2017  2018  2019
       1.0   2.0   3.0   3.0