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