Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

แก้สมการเทนเซอร์ใน Python


ในการแก้สมการเทนเซอร์ ให้ใช้เมธอด numpy.linalg.tensorsolve() ใน Python สันนิษฐานว่าดัชนีทั้งหมดของ x ถูกรวมไว้ในผลคูณ ร่วมกับดัชนีขวาสุดของ a ดังที่ทำใน tensordot(a, x, axes=b.nim)

พารามิเตอร์ที่ 1 a คือเทนเซอร์สัมประสิทธิ์ของรูปร่าง b.shape + Q. Q, ทูเพิลเท่ากับรูปร่างของซับเทนเซอร์ของ a ที่ประกอบด้วยจำนวนที่เหมาะสมของดัชนีขวาสุดของมัน, และต้องเป็นเช่นนั้น prod( ถาม) ==prod(b.shape) พารามิเตอร์ตัวที่ 2 b คือเทนเซอร์ทางขวา ซึ่งสามารถมีรูปร่างแบบใดก็ได้ พารามิเตอร์ตัวที่ 3 axis คือแกนใน a เพื่อจัดลำดับใหม่ทางขวา ก่อนผกผัน หากไม่มี (ค่าเริ่มต้น) จะไม่มีการจัดลำดับใหม่

ขั้นตอน

ขั้นแรก นำเข้าไลบรารีที่จำเป็น-

import numpy as np

การสร้างอาร์เรย์ numpy สองชุดโดยใช้เมธอด array()

arr1 = np.eye(2*3*4)
arr1.shape = (2*3, 4, 2, 3, 4)

arr2 = np.random.randn(2*3, 4)

แสดงอาร์เรย์ -

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)

ในการแก้สมการเทนเซอร์ ให้ใช้เมธอด numpy.linalg.tensorsolve() ใน Python สันนิษฐานว่าดัชนีทั้งหมดของ x ถูกรวมไว้ในผลคูณ ร่วมกับดัชนีขวาสุดของ a ดังที่ทำใน tensordot(a, x, axes=b.nim) −

print("\nResult...\n",np.linalg.tensorsolve(arr1, arr2))

ตัวอย่าง

import numpy as np

# Creating two numpy arrays using the array() method
arr1 = np.eye(2*3*4)
arr1.shape = (2*3, 4, 2, 3, 4)
arr2 = np.random.randn(2*3, 4)

# Display the arrays
print("Array1...\n",arr1)
print("\nArray2...\n",arr2)

# Check the Dimensions of both the arrays
print("\nDimensions of Array1...\n",arr1.ndim)
print("\nDimensions of Array2...\n",arr2.ndim)

# Check the Shape of both the arrays
print("\nShape of Array1...\n",arr1.shape)
print("\nShape of Array2...\n",arr2.shape)

# To solve the tensor equation, use the numpy.linalg.tensorsolve() method in Python.
print("\nResult...\n",np.linalg.tensorsolve(arr1, arr2))

ผลลัพธ์

Array1...
[[[[[1. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]

[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]]


[[[0. 1. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]

[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]]


[[[0. 0. 1. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]

[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]]


[[[0. 0. 0. 1.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]

[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]]]



[[[[0. 0. 0. 0.]
[1. 0. 0. 0.]
[0. 0. 0. 0.]]

[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]]


[[[0. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 0. 0.]]

[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]]


[[[0. 0. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 0.]]

[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]]


[[[0. 0. 0. 0.]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]

[[0. 0. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 0.]]]


[[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]

[[0. 0. 0. 0.]
[0. 0. 0. 1.]
[0. 0. 0. 0.]]]]



[[[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]

[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[1. 0. 0. 0.]]]


[[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]

[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 1. 0. 0.]]]


[[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]

[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 1. 0.]]]


[[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]

[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 1.]]]]]

Array2...
[[ 0.31376716 0.63443741 0.58628101 0.62313096]
[ 1.12528958 -1.18403238 -0.64663325 -0.24241201]
[ 0.55598965 -2.00059925 -1.97946414 -1.72478953]
[ 0.18976226 0.60572953 1.50157692 -2.4491463 ]
[ 0.42461806 -2.17872016 0.49677904 -1.11634625]
[-1.09074462 0.35475618 0.42474987 -1.34391368]]

Dimensions of Array1...
5

Dimensions of Array2...
2

Shape of Array1...
(6, 4, 2, 3, 4)

Shape of Array2...
(6, 4)

Result...
[[[ 0.31376716 0.63443741 0.58628101 0.62313096]
[ 1.12528958 -1.18403238 -0.64663325 -0.24241201]
[ 0.55598965 -2.00059925 -1.97946414 -1.72478953]]

[[ 0.18976226 0.60572953 1.50157692 -2.4491463 ]
[ 0.42461806 -2.17872016 0.49677904 -1.11634625]
[-1.09074462 0.35475618 0.42474987 -1.34391368]]]