ภาพรวม
ใช้ numpy.kron() คำนวณ Kronecker product ของอาร์เรย์ 4D และ 3D สร้าง composite array จาก blocks ของ array ที่สอง scaled โดย array แรก
ถ้า a.shape = (r₀,r₁,..,rₙ) และ b.shape = (s₀,s₁,..,sₙ) ผลลัพธ์ shape = (r₀s₀, r₁s₁, ..., rₙsₙ) ฟังก์ชันสมมติ dimensions เท่ากัน จะ prepend ด้วย 1 ให้เท่ากัน
ขั้นตอนการทำงาน
1. นำเข้า NumPy
import numpy as np
2. สร้างอาร์เรย์ 4D และ 3D
arr1 = np.arange(40).reshape((2,5,2,2)) arr2 = np.arange(18).reshape((2,3,3))
3. แสดงข้อมูลอาร์เรย์
print("Array1...\n", arr1)
print("\nArray2...\n", arr2)
print("\nDimensions of Array1...\n", arr1.ndim)
print("\nDimensions of Array2...\n", arr2.ndim)
print("\nShape of Array1...\n", arr1.shape)
print("\nShape of Array2...\n", arr2.shape)
4. คำนวณ Kronecker product
print("\nResult (Kronecker product)...\n", np.kron(arr1, arr2))
ตัวอย่างโค้ดเต็ม
import numpy as np
arr1 = np.arange(40).reshape((2,5,2,2))
arr2 = np.arange(18).reshape((2,3,3))
print("Array1...\n", arr1)
print("\nArray2...\n", arr2)
print("\nDimensions of Array1...\n", arr1.ndim)
print("\nDimensions of Array2...\n", arr2.ndim)
print("\nShape of Array1...\n", arr1.shape)
print("\nShape of Array2...\n", arr2.shape)
print("\nResult (Kronecker product)...\n", np.kron(arr1, arr2))
ผลลัพธ์
Kronecker product shape = (2×2, 5×3, 2×3, 2×3) = (4, 15, 6, 6) ค่าของแต่ละ element = a[i₀,i₁,i₂,i₃] × b[j₀,j₁,j₂]
การประยุกต์ใช้: ใช้ใน quantum computing, signal processing, image processing, และการสร้าง basis functions