@@ -5,6 +5,7 @@ import math from '../../../../src/defaultInstance.js'
55
66describe ( 'kron' , function ( ) {
77 it ( 'should calculate the Kronecker product of two arrays' , function ( ) {
8+ assert . deepStrictEqual ( math . kron ( [ [ 2 ] ] , [ [ 3 ] ] ) , [ [ 6 ] ] )
89 assert . deepStrictEqual ( math . kron ( [
910 [ 1 , - 2 , 1 ] ,
1011 [ 1 , 1 , 0 ]
@@ -28,14 +29,27 @@ describe('kron', function () {
2829 ] )
2930 } )
3031
32+ it ( 'should calculate product for empty 1D Arrays' , function ( ) {
33+ assert . deepStrictEqual ( math . kron ( [ ] , [ ] ) , [ ] )
34+ } )
35+
3136 it ( 'should calculate product for empty 2D Arrays' , function ( ) {
3237 assert . deepStrictEqual ( math . kron ( [ [ ] ] , [ [ ] ] ) , [ [ ] ] )
3338 } )
3439
3540 it ( 'should calculate product for 1D Arrays' , function ( ) {
41+ assert . deepStrictEqual ( math . kron ( [ 2 ] , [ 3 ] ) , [ 6 ] )
42+ assert . deepStrictEqual ( math . kron ( [ 1 , 2 ] , [ 3 , 4 ] ) , [ 3 , 4 , 6 , 8 ] )
43+ assert . deepStrictEqual ( math . kron ( [ 1 , 2 , 6 , 8 ] , [ 12 , 1 , 2 , 3 ] ) , [ 12 , 1 , 2 , 3 , 24 , 2 , 4 , 6 , 72 , 6 , 12 , 18 , 96 , 8 , 16 , 24 ] )
44+ } )
45+
46+ it ( 'should calculate product for 1D & 2D Arrays' , function ( ) {
3647 assert . deepStrictEqual ( math . kron ( [ 1 , 1 ] , [ [ 1 , 0 ] , [ 0 , 1 ] ] ) , [ [ 1 , 0 , 1 , 0 ] , [ 0 , 1 , 0 , 1 ] ] )
3748 assert . deepStrictEqual ( math . kron ( [ [ 1 , 0 ] , [ 0 , 1 ] ] , [ 1 , 1 ] ) , [ [ 1 , 1 , 0 , 0 ] , [ 0 , 0 , 1 , 1 ] ] )
38- assert . deepStrictEqual ( math . kron ( [ 1 , 2 , 6 , 8 ] , [ 12 , 1 , 2 , 3 ] ) , [ [ 12 , 1 , 2 , 3 , 24 , 2 , 4 , 6 , 72 , 6 , 12 , 18 , 96 , 8 , 16 , 24 ] ] )
49+ assert . deepStrictEqual ( math . kron ( [ [ 1 , 2 ] ] , [ [ 1 , 2 , 3 ] ] ) , [ [ 1 , 2 , 3 , 2 , 4 , 6 ] ] )
50+ assert . deepStrictEqual ( math . kron ( [ [ 1 ] , [ 2 ] ] , [ [ 1 ] , [ 2 ] , [ 3 ] ] ) , [ [ 1 ] , [ 2 ] , [ 3 ] , [ 2 ] , [ 4 ] , [ 6 ] ] )
51+ assert . deepStrictEqual ( math . kron ( [ [ 1 , 2 ] ] , [ [ 1 ] , [ 2 ] , [ 3 ] ] ) , [ [ 1 , 2 ] , [ 2 , 4 ] , [ 3 , 6 ] ] )
52+ assert . deepStrictEqual ( math . kron ( [ [ 1 ] , [ 2 ] ] , [ [ 1 , 2 , 3 ] ] ) , [ [ 1 , 2 , 3 ] , [ 2 , 4 , 6 ] ] )
3953 } )
4054
4155 it ( 'should support complex numbers' , function ( ) {
@@ -55,10 +69,32 @@ describe('kron', function () {
5569 ] )
5670 } )
5771
58- it ( 'should throw an error for greater then 2 dimensions' , function ( ) {
59- assert . throws ( function ( ) {
60- math . kron ( [ [ [ 1 , 1 ] , [ 1 , 1 ] ] , [ [ 1 , 1 ] , [ 1 , 1 ] ] ] , [ [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] , [ [ 6 , 7 , 8 ] , [ 9 , 10 , 11 ] ] ] )
61- } )
72+ it ( 'should calculate a 3D Kronecker product' , function ( ) {
73+ assert . deepStrictEqual (
74+ math . kron ( [
75+ [ [ 1 , 2 ] , [ 2 , 3 ] ] ,
76+ [ [ 2 , 3 ] , [ 3 , 4 ] ]
77+ ] , [
78+ [ [ 4 , 3 ] , [ 3 , 2 ] ] ,
79+ [ [ 3 , 2 ] , [ 2 , 1 ] ]
80+ ] ) , [
81+ /* eslint-disable no-multi-spaces, array-bracket-spacing */
82+ [ [ 4 , 3 , 8 , 6 ] , [ 3 , 2 , 6 , 4 ] , [ 8 , 6 , 12 , 9 ] , [ 6 , 4 , 9 , 6 ] ] ,
83+ [ [ 3 , 2 , 6 , 4 ] , [ 2 , 1 , 4 , 2 ] , [ 6 , 4 , 9 , 6 ] , [ 4 , 2 , 6 , 3 ] ] ,
84+ [ [ 8 , 6 , 12 , 9 ] , [ 6 , 4 , 9 , 6 ] , [ 12 , 9 , 16 , 12 ] , [ 9 , 6 , 12 , 8 ] ] ,
85+ [ [ 6 , 4 , 9 , 6 ] , [ 4 , 2 , 6 , 3 ] , [ 9 , 6 , 12 , 8 ] , [ 6 , 3 , 8 , 4 ] ]
86+ /* eslint-enable */
87+ ]
88+ )
89+ } )
90+
91+ it ( 'should allow mixed-dimensional Kronecker products' , function ( ) {
92+ const b = [ [ [ 4 , 3 ] , [ 3 , 2 ] ] , [ [ 3 , 2 ] , [ 2 , 1 ] ] ]
93+ const a = [ 1 , 2 ]
94+ assert . deepStrictEqual ( math . kron ( a , b ) , math . kron ( [ [ a ] ] , b ) )
95+ assert . deepStrictEqual ( math . kron ( [ a ] , b ) , math . kron ( [ [ a ] ] , b ) )
96+ assert . deepStrictEqual ( math . kron ( b , a ) , math . kron ( b , [ [ a ] ] ) )
97+ assert . deepStrictEqual ( math . kron ( b , [ a ] ) , math . kron ( b , [ [ a ] ] ) )
6298 } )
6399
64100 it ( 'should throw an error if called with an invalid number of arguments' , function ( ) {
@@ -81,10 +117,12 @@ describe('kron', function () {
81117 assert . deepStrictEqual ( product . toArray ( ) , [ [ 13 , 26 , 0 , 0 ] , [ 715 , - 13 , 0 , - 0 ] , [ 0 , 0 , - 1 , - 2 ] , [ 0 , - 0 , - 55 , 1 ] ] )
82118 } )
83119
84- it ( 'should throw an error for invalid Kronecker product of matrix ' , function ( ) {
85- const y = math . matrix ( [ [ [ ] ] ] )
120+ it ( 'should calculate the Kronecker product of 3d matrices ' , function ( ) {
121+ const y = math . matrix ( [ [ [ 3 ] ] ] )
86122 const x = math . matrix ( [ [ [ 1 , 1 ] , [ 1 , 1 ] ] , [ [ 1 , 1 ] , [ 1 , 1 ] ] ] )
87- assert . throws ( function ( ) { math . kron ( y , x ) } )
123+ const product = math . kron ( x , y )
124+ assert . deepStrictEqual (
125+ product . toArray ( ) , [ [ [ 3 , 3 ] , [ 3 , 3 ] ] , [ [ 3 , 3 ] , [ 3 , 3 ] ] ] )
88126 } )
89127 } )
90128
0 commit comments