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).
numpy.linalg.tensorsolve(A, B, axes=None )
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.
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
In this example, we have created a 3×3 square matrix, which is not singular, and we have printed that. Then we 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 an output of shape inverse of B.
Also, we have checked if the returned answer is True or not. That is it for the np.linalg.tensorsolve() function.
#numpy #python #tensorsolve