Get the Kronecker product of arrays with 4D and 3D dimensions in Python


To get the Kronecker product of a 4D and a 3D dimension array, use the numpy.kron() method in Python Numpy. Compute the Kronecker product, a composite array made of blocks of the second array scaled by the first

The function assumes that the number of dimensions of a and b are the same, if necessary prepending the smallest with ones. If a.shape = (r0,r1,..,rN) and b.shape = (s0,s1,...,sN), the Kronecker product has shape (r0*s0, r1*s1, ..., rN*SN). The elements are products of elements from a and b, organized explicitly by −

kron(a,b)[k0,k1,...,kN] = a[i0,i1,...,iN] * b[j0,j1,...,jN]

Steps

At first, import the required libraries -

import numpy as np

Creating two numpy arrays with different dimensions using the arange() and reshape() method −

arr1 = np.arange(40).reshape((2,5,2,2))
arr2 = np.arange(18).reshape((2,3,3))

Display the arrays −

print("Array1...\n",arr1)
print("\nArray2...\n",arr2)

Check the Dimensions of both the arrays −

print("\nDimensions of Array1...\n",arr1.ndim)
print("\nDimensions of Array2...\n",arr2.ndim)

Check the Shape of both the arrays −

print("\nShape of Array1...\n",arr1.shape)
print("\nShape of Array2...\n",arr2.shape)

To get the Kronecker product of two arrays, use the numpy.kron() method in Python Numpy −

print("\nResult (Kronecker product)...\n",np.kron(arr1, arr2))

Example

import numpy as np

# Creating two numpy arrays with different dimensions using the arange() and reshape() method
arr1 = np.arange(40).reshape((2,5,2,2))
arr2 = np.arange(18).reshape((2,3,3))

# Display the arrays
print("Array1...\n",arr1)
print("\nArray2...\n",arr2)

# Check the Dimensions of both the arrays
print("\nDimensions of Array1...\n",arr1.ndim)
print("\nDimensions of Array2...\n",arr2.ndim)

# Check the Shape of both the arrays
print("\nShape of Array1...\n",arr1.shape)
print("\nShape of Array2...\n",arr2.shape)

# To get the Kronecker product of two arrays, use the numpy.kron() method in Python Numpy
print("\nResult (Kronecker product)...\n",np.kron(arr1, arr2))

Output

Array1...
[[[[ 0 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]]]]

Array2...
[[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]]

[[ 9 10 11]
[12 13 14]
[15 16 17]]]

Dimensions of Array1...
4

Dimensions of Array2...
3

Shape of Array1...
(2, 5, 2, 2)

Shape of Array2...
(2, 3, 3)

Result (Kronecker product)...
[[[[ 0 0 0 0 1 2]
[ 0 0 0 3 4 5]
[ 0 0 0 6 7 8]
[ 0 2 4 0 3 6]
[ 6 8 10 9 12 15]
[ 12 14 16 18 21 24]]

[[ 0 0 0 9 10 11]
[ 0 0 0 12 13 14]
[ 0 0 0 15 16 17]
[ 18 20 22 27 30 33]
[ 24 26 28 36 39 42]
[ 30 32 34 45 48 51]]

[[ 0 4 8 0 5 10]
[ 12 16 20 15 20 25]
[ 24 28 32 30 35 40]
[ 0 6 12 0 7 14]
[ 18 24 30 21 28 35]
[ 36 42 48 42 49 56]]

[[ 36 40 44 45 50 55]
[ 48 52 56 60 65 70]
[ 60 64 68 75 80 85]
[ 54 60 66 63 70 77]
[ 72 78 84 84 91 98]
[ 90 96 102 105 112 119]]

[[ 0 8 16 0 9 18]
[ 24 32 40 27 36 45]
[ 48 56 64 54 63 72]
[ 0 10 20 0 11 22]
[ 30 40 50 33 44 55]
[ 60 70 80 66 77 88]]

[[ 72 80 88 81 90 99]
[ 96 104 112 108 117 126]
[120 128 136 135 144 153]
[ 90 100 110 99 110 121]
[120 130 140 132 143 154]
[150 160 170 165 176 187]]

[[ 0 12 24 0 13 26]
[ 36 48 60 39 52 65]
[ 72 84 96 78 91 104]
[ 0 14 28 0 15 30]
[ 42 56 70 45 60 75]
[ 84 98 112 90 105 120]]

[[108 120 132 117 130 143]
[144 156 168 156 169 182]
[180 192 204 195 208 221]
[126 140 154 135 150 165]
[168 182 196 180 195 210]
[210 224 238 225 240 255]]

[[ 0 16 32 0 17 34]
[ 48 64 80 51 68 85]
[ 96 112 128 102 119 136]
[ 0 18 36 0 19 38]
[ 54 72 90 57 76 95]
[108 126 144 114 133 152]]

[[144 160 176 153 170 187]
[192 208 224 204 221 238]
[240 256 272 255 272 289]
[162 180 198 171 190 209]
[216 234 252 228 247 266]
[270 288 306 285 304 323]]]


[[[ 0 20 40 0 21 42]
[ 60 80 100 63 84 105]
[120 140 160 126 147 168]
[ 0 22 44 0 23 46]
[ 66 88 110 69 92 115]
[132 154 176 138 161 184]]

[[180 200 220 189 210 231]
[240 260 280 252 273 294]
[300 320 340 315 336 357]
[198 220 242 207 230 253]
[264 286 308 276 299 322]
[330 352 374 345 368 391]]

[[ 0 24 48 0 25 50]
[ 72 96 120 75 100 125]
[144 168 192 150 175 200]
[ 0 26 52 0 27 54]
[ 78 104 130 81 108 135]
[156 182 208 162 189 216]]

[[216 240 264 225 250 275]
[288 312 336 300 325 350]
[360 384 408 375 400 425]
[234 260 286 243 270 297]
[312 338 364 324 351 378]
[390 416 442 405 432 459]]

[[ 0 28 56 0 29 58]
[ 84 112 140 87 116 145]
[168 196 224 174 203 232]
[ 0 30 60 0 31 62]
[ 90 120 150 93 124 155]
[180 210 240 186 217 248]]

[[252 280 308 261 290 319]
[336 364 392 348 377 406]
[420 448 476 435 464 493]
[270 300 330 279 310 341]
[360 390 420 372 403 434]
[450 480 510 465 496 527]]

[[ 0 32 64 0 33 66]
[ 96 128 160 99 132 165]
[192 224 256 198 231 264]
[ 0 34 68 0 35 70]
[102 136 170 105 140 175]
[204 238 272 210 245 280]]

[[288 320 352 297 330 363]
[384 416 448 396 429 462]
[480 512 544 495 528 561]
[306 340 374 315 350 385]
[408 442 476 420 455 490]
[510 544 578 525 560 595]]

[[ 0 36 72 0 37 74]
[108 144 180 111 148 185]
[216 252 288 222 259 296]
[ 0 38 76 0 39 78]
[114 152 190 117 156 195]
[228 266 304 234 273 312]]

[[324 360 396 333 370 407]
[432 468 504 444 481 518]
[540 576 612 555 592 629]
[342 380 418 351 390 429]
[456 494 532 468 507 546]
[570 608 646 585 624 663]]]]

Updated on: 24-Feb-2022

153 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements