stairs

Slice composed of rows of lower or upper triangular matrix.

stairs can be used to pack and unpack symmetric and triangular matrix storage.

Note: stairs is defined for 1D (packet) input and 2D (general) input. This part of documentation is for 2D input.

  1. Slice!(StairsIterator!(Iterator, type)) stairs(Slice!Iterator slice, size_t n)
  2. Slice!(StairsIterator!(S*, type)) stairs(S[] slice, size_t n)
  3. auto stairs(S slice, size_t n)
  4. auto stairs(Slice!(Iterator, 2, kind) slice)
    @optmath
    stairs
    (
    string type
    Iterator
    SliceKind kind
    )
    (
    Slice!(Iterator, 2, kind) slice
    )
    if (
    type == "+" ||
    type == "-"
    )

Parameters

type
  • "+" for stairs with lengths 1, 2, ..., n, lower matrix;
  • "-" for stairs with lengths n, n-1, ..., 1, upper matrix.
slice Slice!(Iterator, 2, kind)

input slice with length equal to n * (n + 1) / 2

Return Value

Type: auto

1D slice composed of 1D contiguous slices.

Examples

import mir.ndslice.topology: iota, as, stairs;

auto gen = [3, 3].iota.as!double;
auto inc = gen.stairs!"+";
auto dec = gen.stairs!"-";

assert(inc == [
    [0],
    [3, 4],
    [6, 7, 8]]);

assert(dec == [
    [0, 1, 2],
       [4, 5],
          [8]]);

static assert(is(typeof(inc.front) == typeof(gen.front)));
static assert(is(typeof(dec.front) == typeof(gen.front)));

/////////////////////////////////////////
// Pack lower and upper matrix parts
auto n = gen.length;
auto m = n * (n + 1) / 2;
// allocate memory
import mir.ndslice.allocation: uninitSlice;
auto lowerData = m.uninitSlice!double;
auto upperData = m.uninitSlice!double;
// construct packed stairs
auto lower = lowerData.stairs!"+"(n);
auto upper = upperData.stairs!"-"(n);
// copy data
import mir.algorithm.iteration: each;
each!"a[] = b"(lower, inc);
each!"a[] = b"(upper, dec);

assert(&lower[0][0] is &lowerData[0]);
assert(&upper[0][0] is &upperData[0]);

assert(lowerData == [0, 3, 4, 6, 7, 8]);
assert(upperData == [0, 1, 2, 4, 5, 8]);

See Also

Meta