extendr_api::prelude::modules::core

Function kron

pub fn kron<E>(dst: MatMut<'_, E>, lhs: MatRef<'_, E>, rhs: MatRef<'_, E>)
where E: ComplexField,
Expand description

Kronecker product of two matrices.

The Kronecker product of two matrices A and B is a block matrix C with the following structure:

C = [ a[(0, 0)] * B    , a[(0, 1)] * B    , ... , a[(0, n-1)] * B    ]
    [ a[(1, 0)] * B    , a[(1, 1)] * B    , ... , a[(1, n-1)] * B    ]
    [ ...              , ...              , ... , ...              ]
    [ a[(m-1, 0)] * B  , a[(m-1, 1)] * B  , ... , a[(m-1, n-1)] * B  ]

§Panics

Panics if dst does not have the correct dimensions. The dimensions of dst must be nrows(A) * nrows(B) by ncols(A) * ncols(B).

§Example

use faer::{linalg::kron, mat, Mat};

let a = mat![[1.0, 2.0], [3.0, 4.0]];
let b = mat![[0.0, 5.0], [6.0, 7.0]];
let c = mat![
    [0.0, 5.0, 0.0, 10.0],
    [6.0, 7.0, 12.0, 14.0],
    [0.0, 15.0, 0.0, 20.0],
    [18.0, 21.0, 24.0, 28.0],
];
let mut dst = Mat::new();
dst.resize_with(4, 4, |_, _| 0f64);
kron(dst.as_mut(), a.as_ref(), b.as_ref());
assert_eq!(dst, c);