ในการคูณสเกลาร์ด้วยหลักการบวกของ Einstein ให้ใช้เมธอด numpy.einsum() ใน Python พารามิเตอร์ที่ 1 คือตัวห้อย ระบุรายการย่อยสำหรับการรวม ascomma แยกรายการป้ายห้อย พารามิเตอร์ที่ 2 คือตัวถูกดำเนินการ นี่คืออาร์เรย์สำหรับการดำเนินการ
einsum() วิธีการประเมินแบบแผนการบวกของ Einstein บนตัวถูกดำเนินการ ด้วยการใช้แบบแผนการบวกของไอน์สไตน์ การดำเนินการอาร์เรย์เกี่ยวกับพีชคณิตเชิงเส้นหลายมิติทั่วไปจำนวนมากสามารถแสดงในรูปแบบที่เรียบง่าย ในโหมดโดยนัย einsum จะคำนวณค่าเหล่านี้ ในโหมดโจทก์ einsum ให้ความยืดหยุ่นเพิ่มเติมในการคำนวณการดำเนินการอาร์เรย์อื่นๆ ที่อาจไม่ถูกพิจารณาว่าเป็นการดำเนินการรวมของ Einstein แบบคลาสสิก โดยการปิดใช้งาน หรือบังคับให้รวมป้ายกำกับตัวห้อยเกินข้อกำหนดการรวม
ขั้นตอน
ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import numpy as np
สร้างอาร์เรย์โดยใช้ numpy.arange() และ reshape() −
arr = np.arange(6).reshape(2,3)
วาลคือสเกลาร์ −
val = 2
แสดงอาร์เรย์ -
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 (scalar multiplication)...\n",np.einsum('..., ...', val, arr))
ตัวอย่าง
import numpy as np
# Create an array using the numpy.arange() and reshape()
arr = np.arange(6).reshape(2,3)
# The val is the scalar
val = 2
# Display the array
print("Array...\n",arr)
# Check the datatype
print("\nDatatype of Array...\n",arr.dtype)
# Check the Dimension
print("\nDimensions of Array...\n",arr.ndim)
# Check the Shape
print("\nShape of Array...\n",arr.shape)
# To perform scalar multiplication with Einstein summation convention, use the numpy.einsum() method in Python.
print("\nResult (scalar multiplication)...\n",np.einsum('..., ...', val, arr)) ผลลัพธ์
Array... [[0 1 2] [3 4 5]] Datatype of Array... int64 Dimensions of Array... 2 Shape of Array... (2, 3) Result (scalar multiplication)... [[ 0 2 4] [ 6 8 10]]