ในการคำนวณผลคูณภายในของเวกเตอร์ด้วยหลักการบวกของ Einstein ให้ใช้เมธอด numpy.einsum() ใน Python พารามิเตอร์ที่ 1 คือตัวห้อย ระบุรายการย่อยสำหรับการรวม ascomma แยกรายการป้ายห้อย พารามิเตอร์ที่ 2 คือตัวถูกดำเนินการ นี่คืออาร์เรย์สำหรับการดำเนินการ
einsum() วิธีการประเมินแบบแผนการบวกของ Einstein บนตัวถูกดำเนินการ ด้วยการใช้แบบแผนการบวกของไอน์สไตน์ การดำเนินการอาร์เรย์เกี่ยวกับพีชคณิตเชิงเส้นหลายมิติทั่วไปจำนวนมากสามารถแสดงในรูปแบบที่เรียบง่าย ในโหมดโดยนัย einsum จะคำนวณค่าเหล่านี้
ในโหมดที่ชัดเจน einsum ให้ความยืดหยุ่นเพิ่มเติมในการคำนวณการดำเนินการอาร์เรย์อื่นๆ ที่อาจไม่ได้รับการพิจารณาว่าเป็นการดำเนินการรวมของ Einstein แบบคลาสสิก โดยการปิดใช้งาน หรือบังคับให้การรวมเกินป้ายกำกับตัวห้อย
ขั้นตอน
ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import numpy as np
การสร้างอาร์เรย์ numpy โดยใช้เมธอด arange() และ reshape() -
arr = np.arange(4)
แสดงอาร์เรย์ -
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() -
print("\nResult (inner product)...\n",np.einsum('i,i', arr, arr))
ตัวอย่าง
import numpy as np # Creating a numpy array using the arange() and reshape() method arr = np.arange(4) # 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 inner product of vectors with Einstein summation convention, use the numpy.einsum() method in Python. print("\nResult (inner product)...\n",np.einsum('i,i', arr, arr))
ผลลัพธ์
Our Array... [0 1 2 3] Dimensions of our Array... 1 Datatype of our Array object... int64 Shape of our Array object... (4,) Result (inner product)... 14