# The layer method¶

The layer method is unique among all `staircase.Stairs`

methods - it is the only mutator method, i.e. it is the only method which modifies the step function in place, as opposed to acting on a copy. It is a very flexible method, taking full advantage of defaults and weak typing afforded by Python.

The layer method was introduced in How do I create my step function?, in which parameters were either all numbers, or all vectors. Advanced usage allows certain combinations of these, and may allow vectors of different lengths. Understanding these features is key to ensure they do not result in bugs, or unwanted effects.

For those who are mathematically inclined, a set of axioms pertaining to the layer method can be found at the bottom of the page. The expected behaviour of any call to `staircase.Stairs.layer()`

can be derived from these axioms. Alternatively, the key ideas are summarised below:

#### 1) A null value in the *start* parameter corresponds to -infinity. A null value in the *end* parameter corresponds to infinity.

#### 2) A scalar value for *start* is equivalent to [*start*]. A scalar value for *end* is equivalent to [*end*].

#### 3) If *start* and *end* are vectors of differing size, then null values are appended to the smallest till they are equal in length.

And don’t forget the meaning of null values according to rule 1!

#### 4) A scalar value for *value* is equivalent to a vector, filled with *value*, whose length is equal to the larger of *start* and *end*.

#### 5) A vector value for *value* must have the same length as the largest of *start* and *end*.

An error will result otherwise.

## Test your knowledge¶

What do you think the result of sc.Stairs().layer() might be? See discussion and answer in Gotchas.

## Axioms¶

inf_{i}= inf (infinity) for all i

.layer(s, e) = .layer(s, e, None) = .layer(s, e, 1)

.layer(s, None, v) = .layer(s, inf, v)

.layer(None, e, v) = .layer(-inf, e, v)

.layer(s, e, v) is the same as .layer(s, None, v).layer(s, None, -v) provided s, e not None

.layer([s], _, _) = .layer(s, _, _)

.layer(_, [e], _) = .layer(_, e, _)

.layer([s

_{1}, …, s_{n}], [e_{1}, …, e_{n}], v) = .layer([s_{1}, …, s_{n}], [e_{1}, …, e_{n}], [v_{1}, …, v_{n}]) where v_{i}= v for all i.layer([s

_{1}, …, s_{n}], [e_{1}, …, e_{n}], [v_{1}, …, v_{n}]) = layer([s_{1}, …, s_{n-1}], [e_{1}, …, e_{n-1}], [v_{1}, …, v_{n-1}]).layer(s_{n}, e_{n}, v_{n}).layer([s

_{1}, …, s_{n}], [e_{1}, …, e_{k}], _) = .layer([s_{1}, …, s_{n}], [e_{1}, …, e_{k}, inf_{1}, …, inf_{n-k},], _) if n > k.layer([s

_{1}, …, s_{n}], [e_{1}, …, e_{k}], _) = .layer([s_{1}, …, s_{n}, -inf_{1}, …, -inf_{k-n},], [e_{1}, …, e_{k}], _) if k > n