Kronecker product: Difference between revisions
Content added Content deleted
(-- just product --) |
|||
Line 1,682: | Line 1,682: | ||
Note: This resultant matrix could be used as initial for Checkerboard fractal. |
Note: This resultant matrix could be used as initial for Checkerboard fractal. |
||
</pre> |
</pre> |
||
=={{header|Racket}}== |
|||
Uses typed racket, since the 'math/...' libraries are much more performant in that language. |
|||
<lang racket>#lang typed/racket/base |
|||
(require math/array |
|||
math/matrix |
|||
racket/match) |
|||
(define-type (M A) (Matrix A)) |
|||
(define #:forall (A B C) (general-⊗ [m1 : (M A)] [m2 : (M B)] [× : (A B -> C)]) : (M C) |
|||
(match-let* ((`(#(,rs1 ,cs1) . #(,rs2 ,cs2)) (cons (array-shape m1) (array-shape m2))) |
|||
(rs (* rs1 rs2)) |
|||
(cs (* cs1 cs2))) |
|||
(for*/matrix: rs cs ((r (in-range rs)) (c (in-range cs))) : C |
|||
(let-values (((rq rr) (quotient/remainder r rs2)) |
|||
((cq cr) (quotient/remainder c cs2))) |
|||
(× (array-ref m1 (vector rq cq)) (array-ref m2 (vector rr cr))))))) |
|||
;; Narrow to Number |
|||
(define (Kronecker-product [m1 : (M Number)] [m2 : (M Number)]) (general-⊗ m1 m2 *)) |
|||
;; --------------------------------------------------------------------------------------------------- |
|||
(module+ test |
|||
(Kronecker-product (matrix [[1 2] |
|||
[3 4]]) |
|||
(matrix [[0 5] |
|||
[6 7]])) |
|||
(Kronecker-product (matrix [[0 1 0] |
|||
[1 1 1] |
|||
[0 1 0]]) |
|||
(matrix [[1 1 1 1] |
|||
[1 0 0 1] |
|||
[1 1 1 1]])))</lang> |
|||
{{out}} |
|||
<pre>(mutable-array #[#[0 5 0 10] #[6 7 12 14] #[0 15 0 20] #[18 21 24 28]]) |
|||
(mutable-array |
|||
#[#[0 0 0 0 1 1 1 1 0 0 0 0] |
|||
#[0 0 0 0 1 0 0 1 0 0 0 0] |
|||
#[0 0 0 0 1 1 1 1 0 0 0 0] |
|||
#[1 1 1 1 1 1 1 1 1 1 1 1] |
|||
#[1 0 0 1 1 0 0 1 1 0 0 1] |
|||
#[1 1 1 1 1 1 1 1 1 1 1 1] |
|||
#[0 0 0 0 1 1 1 1 0 0 0 0] |
|||
#[0 0 0 0 1 0 0 1 0 0 0 0] |
|||
#[0 0 0 0 1 1 1 1 0 0 0 0]])</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |