cartesian

Cartesian product.

Constructs lazy cartesian product Slice without memory allocation.

@optmath
cartesian
(
NdFields...
)
(
NdFields fields
)
if (
NdFields.length > 1 &&
allSatisfy!(templateOr!(hasShape, hasLength), NdFields)
)

Parameters

fields NdFields

list of fields with lengths or ndFields with shapes

Return Value

Type: auto

Cartesian!NdFields(fields).slicedNdField;

Examples

1D x 1D

auto a = [10, 20, 30];
auto b = [ 1,  2,  3];

auto c = cartesian(a, b)
    .map!"a + b";

assert(c == [
    [11, 12, 13],
    [21, 22, 23],
    [31, 32, 33]]);

1D x 2D

auto a = [10, 20, 30];
auto b = iota([2, 3], 1);

auto c = cartesian(a, b)
    .map!"a + b";

assert(c.shape == [3, 2, 3]);

assert(c == [
    [
        [11, 12, 13],
        [14, 15, 16],
    ],
    [
        [21, 22, 23],
        [24, 25, 26],
    ],
    [
        [31, 32, 33],
        [34, 35, 36],
    ]]);

1D x 1D x 1D

auto u = [100, 200];
auto v = [10, 20, 30];
auto w = [1, 2];

auto c = cartesian(u, v, w)
    .map!"a + b + c";

assert(c.shape == [2, 3, 2]);

assert(c == [
    [
        [111, 112],
        [121, 122],
        [131, 132],
    ],
    [
        [211, 212],
        [221, 222],
        [231, 232],
    ]]);

Meta