The OpenD Programming Language

eachUploPair

The call eachUploPair!(fun)(matrix) evaluates fun for each pair (matrix[j, i], matrix[i, j]), for i <= j (default) or i < j (if includeDiagonal is false).

template eachUploPair(alias fun, bool includeDiagonal = false)
static if(__traits(isSame, naryFun!fun, fun))
eachUploPair
(
Iterator
SliceKind kind
)
(
Slice!(Iterator, 2, kind) matrix
)

Members

Functions

eachUploPair
auto eachUploPair(Slice!(Iterator, 2, kind) matrix)

Parameters

fun

A function.

includeDiagonal

true if applying function to diagonal, false (default) otherwise.

Examples

Transpose matrix in place.

import mir.ndslice.allocation: slice;
import mir.ndslice.topology: iota, universal;
import mir.ndslice.dynamic: transposed;
import mir.utility: swap;

auto m = iota(4, 4).slice;

m.eachUploPair!swap;

assert(m == iota(4, 4).universal.transposed);

Reflect Upper matrix part to lower part.

import mir.ndslice.allocation: slice;
import mir.ndslice.topology: iota, universal;
import mir.ndslice.dynamic: transposed;
import mir.utility: swap;

// 0 1 2
// 3 4 5
// 6 7 8
auto m = iota(3, 3).slice;

m.eachUploPair!((u, ref l) { l = u; });

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

Fill lower triangle and diagonal with zeroes.

import mir.ndslice.allocation: slice;
import mir.ndslice.topology: iota;

// 1 2 3
// 4 5 6
// 7 8 9
auto m = iota([3, 3], 1).slice;

m.eachUploPair!((u, ref l) { l = 0; }, true);

assert(m == [
    [0, 2, 3],
    [0, 0, 6],
    [0, 0, 0]]);

Meta