Solve the tensor equation in Python

PythonServer Side ProgrammingProgramming

To solve the tensor equation, use the numpy.linalg.tensorsolve() method in Python. It is assumed that all indices of x are summed over in the product, together with the rightmost indices of a, as is done in, for example, tensordot(a, x, axes=b.ndim).

The 1st parameter, a is a coefficient tensor, of shape b.shape + Q. Q, a tuple, equals the shape of that sub-tensor of a consisting of the appropriate number of its rightmost indices, and must be such that prod(Q) == prod(b.shape). The 2nd parameter, b is a right-hand tensor, which can be of any shape. The 3rd parameter, axis is an axes in a to reorder to the right, before inversion. If None (default), no reordering is done.

Steps

At first, import the required libraries-

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. It is assumed that all indices of x are summed over in the product, together with the rightmost indices of a, as is done in, for example, tensordot(a, x, axes=b.ndim) −

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

Example

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))

Output

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


[[[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]]]
raja
Updated on 25-Feb-2022 06:55:20

Advertisements