AppDividend
Latest Code Tutorials

Numpy linalg tensorsolve() Function in Python with Example

0

Numpy linalg tensorsolve() function is used to calculate the equation of ax=b for x. It is assumed that all x indices are summarized above the product and the right indices of a, as is done. For example, tensordot (a, x, axes = b.ndim).

Syntax

numpy.linalg.tensorsolve(A, B, axes=None )

Parameters

  1. A: Coefficient tensor, condition b. status + Q. Q, Tuple, is equal to the shape of that sub-tensor with the correct number of its right indices and should be the same (pr) (Q) == prod (b) .shape) (when it is called a ‘square’).
  2. B: Right-hand tensor, which can be of any shape. 

Return Value

The linalg tensorsolve() function returns a ndarray of shape the same as Q. 

The linalg tensorsolve() function throws LinAlgError if A is singular or not a square matrix.

Programming example

Program to show the working of tensorsolve():

# Program to show working of solve()
import numpy as np

# creating the array "a"
A = np.array([[3, 4, 5], [1, 2, 3], [2, 4, 5]])
B = np.array([9, 8, 7])
print("Array A is: \n", A)
print("Array B is : \n", B)

# Calculating the equation
ans = np.linalg.tensorsolve(A, B)

# Printing the answer
print("Answer of the equation is  :\n", ans)

# Checking if the answer if correct
print(np.allclose(np.dot(A, ans), B))

Output

Array A is:
 [[3 4 5]
 [1 2 3]
 [2 4 5]]
Array B is :
 [9 8 7]
Answer of the equation is  :
 [  2.  -10.5   9. ]
True

Explanation

In this example, we have created a 3×3 square matrix, which is not singular, and we have printed that. Then we have created an array of size 3 and printed that also.

Then we have called numpy.linalg.tensorsolve() to calculate the equation Ax=B. We can see that we have got an output of shape inverse of B.

Also, at last, we have checked if the returned answer is True or not.

That is it for the numpy linalg tensorsolve() function.

See also

Numpy linalg solve()

Numpy linalg svd()

Numpy linalg qr()

Numpy linalg cholesky()

Numpy linalg slogdet()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.