# Numpy linalg.tensorinv( ): Computing Inverse of an N-Dimensional Array

Finding an inverse is one of the peculiar operations to be carried out in the field of Mathematics. In this article, we shall explore one such function from the numpy library to its length & breadth. This function is used to return the inverse of an N-dimensional array. For those who haven’t figured it out yet, it is time for the big reveal. Behold, the linalg.tensorinv( ) function!

Let us start off things by importing the numpy library by using the below code.

```import numpy as np
```

Thereafter, we shall explore further the aforementioned function through each of the following sections.

• What is a tensor?
• Syntax of the linalg.tensorinv( ) function
• Using the linalg.tensorinv( ) function on N-dimensional arrays

Also read: Numpy linalg.norm – Matrix or vector norm

## What is a tensor?

Tensors are powerful mathematical objects used to describe physical properties of a system. They are similar to vectors and scalars, which are used to represent physical quantities such as position, velocity, and force. Tensors are represented as N-dimensional arrays, which can be used to describe any physical quantity. They are often used in physics, engineering, and other scientific fields to model and analyze physical systems.

Tensors can also be used to describe the properties of a field. A field is defined as a set of points in space, each of which can have different values of a tensor. This allows for complex calculations to be performed on fields, such as calculating the average temperature of a field. Tensors are also used in computer graphics, robotics, machine learning, and other disciplines.

## Syntax of the linalg.tensorinv( ) function

Like every other function used in Python, the linalg.tensorinv( ) function also requires some fundamental constituents for its proper functioning. Those constituents are given below in the syntax of the function.

```numpy.linalg.tensorinv(a, ind)
```

where,

• a – N-dimensional array which is to be inverted
• ind – Number of first indices that are required for computing the inversion. This option is set to 2 by default & can be changed into any positive integer.

## Using the linalg.tensorinv( ) function on N-dimensional arrays

To demonstrate the functioning of the linalg.tensorinv( ), let us start things off by creating a diagonal array with twelve ones along the diagonal & zeros everywhere. The eye( ) function from the numpy library can be used for this purpose as shown below.

```a = np.eye(12)
```

Now let us reshape the above matrix by splitting them using the below code. Just like tensors would be used for chunking down a massive system into a mathematical framework, we shall split the above matrix into smaller bits.

```a.shape = (2, 6, 3, 4)
```

What the above code does is split the 12×12 matrix into smaller matrices of dimension 3×4 into 2 sets with 6 matrices in each set. Now you know the reason behind the usage of 2, 6, 3 & 4 in the above code. Following is the result for those who would like to know how the matrix splitting takes place.

```a.shape = ( 2, 6, 3, 4)
print(a)

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

[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 1.]]]]
```

Now that we have split the matrix, let us now invert the tensors using the following code. We shall take the number of indices as ‘2’.

```inv = np.linalg.tensorinv(a, ind=2)
```

Once done, we shall find out the dimensions of the inverted set using the below code.

```print(inv.shape)

Output: (3, 4, 2, 6)
```

It is evident from the above result that after the inversion there are now 3 sets with 4 matrices each and all these matrices are of the dimensions 2×6. Following is the result for those who would like to sneak a peek.

```inv = np.linalg.tensorinv(a, ind=2)
print(inv)

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

[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 1.]]]]
```

## Conclusion

Now that we have reached the end of this article, hope it has elaborated on how to use the linalg.tensorinv( ) function from the numpy library. Here’s another article that details the usage of the convolve( ) function from the numpy library in Python. There are numerous other enjoyable and equally informative articles in AskPython that might be of great help to those who are looking to level up in Python. Carpe diem!