1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use std::ops::Mul;

use crate::{matrix_init, Matrix};

pub trait KroneckerMul<Rhs>: Matrix
where
    Rhs: Matrix,
    Self::Output: Matrix
{
    type Output;

    /// Returns the kronecker product of the two matrices
    /// 
    /// A ⊗ₖᵣₒₙ B
    /// 
    /// # Arguments
    /// 
    /// * `rhs` - A matrix of any size
    /// 
    /// # Examples
    /// 
    /// ```rust
    /// let a = [
    ///     [1.0, 2.0],
    ///     [3.0, 4.0]
    /// ];
    /// let b = [
    ///     [1.0, 2.0],
    ///     [3.0, 4.0]
    /// ];
    /// let ab = [
    ///     [1.0, 2.0, 2.0, 4.0],
    ///     [3.0, 4.0, 6.0, 8.0],
    ///     [3.0, 6.0, 4.0, 8.0],
    ///     [9.0, 12.0, 12.0, 16.0]
    /// ];
    /// assert_eq!(a.kronecker(b), ab);
    /// ```
    fn kronecker_mul(self, rhs: Rhs) -> Self::Output;
}

impl<F, const L1: usize, const H1: usize, const L2: usize, const H2: usize> 
    KroneckerMul<[[F; L2]; H2]>
for
    [[F; L1]; H1]
where
    Self: Matrix,
    [[F; L2]; H2]: Matrix,
    F: Clone + Mul<F>,
    [[<F as Mul<F>>::Output; L1*L2]; H1*H2]: Matrix
{
    type Output = [[<F as Mul<F>>::Output; L1*L2]; H1*H2];

    fn kronecker_mul(self, rhs: [[F; L2]; H2]) -> Self::Output
    {
        matrix_init(|r, c| self[r/H1][c/L1].clone()*rhs[r%H2][c%L2].clone())
    }
}