kronecker

Kronecker product.

Constructs lazy kronecker product Slice without memory allocation.

  1. auto kronecker(NdFields fields)
    template kronecker(alias fun = product)
    @optmath
    static if(__traits(isSame, naryFun!fun, fun))
    @optmath
    kronecker
    (
    NdFields...
    )
    (
    NdFields fields
    )
    if (
    allSatisfy!(hasShape, NdFields) ||
    allSatisfy!(hasLength, NdFields)
    )
  2. alias kronecker = .kronecker!(naryFun!fun)

Members

Aliases

kronecker
alias kronecker = .kronecker!(naryFun!fun)
Undocumented in source.

Functions

kronecker
auto kronecker(NdFields fields)

Examples

2D

import mir.ndslice.allocation: slice;
import mir.ndslice.slice: sliced;

// eye
auto a = slice!double([4, 4], 0);
a.diagonal[] = 1;

auto b = [ 1, -1,
          -1,  1].sliced(2, 2);

auto c = kronecker(a, b);

assert(c == [
    [ 1, -1,  0,  0,  0,  0,  0,  0],
    [-1,  1,  0,  0,  0,  0,  0,  0],
    [ 0,  0,  1, -1,  0,  0,  0,  0],
    [ 0,  0, -1,  1,  0,  0,  0,  0],
    [ 0,  0,  0,  0,  1, -1,  0,  0],
    [ 0,  0,  0,  0, -1,  1,  0,  0],
    [ 0,  0,  0,  0,  0,  0,  1, -1],
    [ 0,  0,  0,  0,  0,  0, -1,  1]]);

1D

auto a = iota([3], 1);

auto b = [ 1, -1];

auto c = kronecker(a, b);

assert(c == [1, -1, 2, -2, 3, -3]);

2D with 3 arguments

import mir.ndslice.allocation: slice;
import mir.ndslice.slice: sliced;

auto a = [ 1,  2,
           3,  4].sliced(2, 2);

auto b = [ 1,  0,
           0,  1].sliced(2, 2);

auto c = [ 1, -1,
          -1,  1].sliced(2, 2);

auto d = kronecker(a, b, c);

assert(d == [
    [ 1, -1,  0,  0,  2, -2,  0,  0],
    [-1,  1,  0,  0, -2,  2,  0,  0],
    [ 0,  0,  1, -1,  0,  0,  2, -2],
    [ 0,  0, -1,  1,  0,  0, -2,  2],
    [ 3, -3,  0,  0,  4, -4,  0,  0],
    [-3,  3,  0,  0, -4,  4,  0,  0],
    [ 0,  0,  3, -3,  0,  0,  4, -4],
    [ 0,  0, -3,  3,  0,  0, -4,  4]]);

Meta