- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Solve the tensor equation in Python

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

- Related Questions & Answers
- How to Solve Quadratic Equation using Python?
- Secant method to solve non-linear equation
- How to solve a circulant matrix equation using Python SciPy?
- How to solve Hermitian positive-banded matrix equation using Python SciPy?
- Solve a linear matrix equation or system of linear scalar equations in Python
- C++ Program to Solve any Linear Equation in One Variable
- How can we use the SciPy library to solve a Linear equation?
- Which linear function of SciPy is used to solve the circulant matrix equation?
- Compute the tensor dot product in Python
- Which linear function of SciPy is used to solve a banded matrix equation?
- Which linear function of SciPy is used to solve Hermitian positive-definite banded matrix equation?
- Tensor contraction with Einstein summation convention in Python
- Python program to compute a Polynomial Equation
- Compute the tensor dot product for arrays with different dimensions in Python
- Find the missing value from the given equation a + b = c in Python