- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

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

- Related Articles
- How to Solve Quadratic Equation using Python?
- Compute the tensor dot product in Python
- Solve the equation. n4=8m-8
- How to solve a circulant matrix equation using Python SciPy?
- Solve The Following Equation 2x+5=15
- Solve The Equation:$frac{5x}{2} = -5$
- Solve the following equation:$2x+5=0$
- Solve equation:3x+7=37
- How to solve an Equation?
- How to solve Hermitian positive-banded matrix equation using Python SciPy?
- Solve a linear matrix equation or system of linear scalar equations in Python
- Tensor contraction with Einstein summation convention in Python
- Solve the following equation:$frac{5}{2x} = -5$.
- Solve the equation(7m+19)/2=13/4
- Solve the following equation :$5 x-25=0$