ในการคำนวณผลคูณภายนอกของเวกเตอร์ด้วยหลักการบวกของ Einstein ให้ใช้เมธอด numpy.einsum() ใน Python พารามิเตอร์ที่ 1 คือตัวห้อย ระบุรายการย่อยสำหรับการรวม ascomma แยกรายการป้ายห้อย พารามิเตอร์ที่ 2 คือตัวถูกดำเนินการ นี่คืออาร์เรย์สำหรับการดำเนินการ
einsum() วิธีการประเมินแบบแผนการบวกของ Einstein บนตัวถูกดำเนินการ ด้วยการใช้แบบแผนการบวกของไอน์สไตน์ การดำเนินการอาร์เรย์เกี่ยวกับพีชคณิตเชิงเส้นหลายมิติทั่วไปจำนวนมากสามารถแสดงในรูปแบบที่เรียบง่าย ในโหมดโดยนัย einsum จะคำนวณค่าเหล่านี้
ในโหมดที่ชัดเจน einsum ให้ความยืดหยุ่นเพิ่มเติมในการคำนวณการดำเนินการอาร์เรย์อื่นๆ ที่อาจไม่ได้รับการพิจารณาว่าเป็นการดำเนินการรวมของ Einstein แบบคลาสสิก โดยการปิดใช้งาน หรือบังคับให้การรวมเกินป้ายกำกับตัวห้อย
ขั้นตอน
ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import numpy as np
การสร้างอาร์เรย์ numpy โดยใช้เมธอด arange() และ reshape() -
arr = np.arange(5)
แสดงอาร์เรย์ -
print("Our Array...\n",arr)
ตรวจสอบขนาด -
print("\nDimensions of our Array...\n",arr.ndim)
รับประเภทข้อมูล -
print("\nDatatype of our Array object...\n",arr.dtype)
รับรูปร่าง -
print("\nShape of our Array object...\n",arr.shape)
ในการคำนวณผลคูณภายนอกของเวกเตอร์ด้วยหลักการบวกของไอน์สไตน์ ให้ใช้เมธอด numpy.einsum() พารามิเตอร์ที่ 1 คือตัวห้อย ระบุตัวห้อยสำหรับผลรวมเป็นรายการป้ายชื่อตัวห้อยที่คั่นด้วยเครื่องหมายจุลภาค พารามิเตอร์ที่ 2 คือตัวถูกดำเนินการ นี่คืออาร์เรย์สำหรับการดำเนินการ -
print("\nResult (outer product)...\n",np.einsum('i,j', np.arange(2)+1, arr))
ตัวอย่าง
import numpy as np # Creating a numpy array using the arange() and reshape() method arr = np.arange(5) # Display the array print("Our Array...\n",arr) # Check the Dimensions print("\nDimensions of our Array...\n",arr.ndim) # Get the Datatype print("\nDatatype of our Array object...\n",arr.dtype) # Get the Shape print("\nShape of our Array object...\n",arr.shape) # To compute outer product of vectors with Einstein summation convention, use the numpy.einsum() method in Python. print("\nResult (outer product)...\n",np.einsum('i,j', np.arange(2)+1, arr))
ผลลัพธ์
Our Array... [0 1 2 3 4] Dimensions of our Array... 1 Datatype of our Array object... int64 Shape of our Array object... (5,) Result (outer product)... [[0 1 2 3 4] [0 2 4 6 8]]