Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

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

  • Arulius SavioArulius Savio
  • January 30, 2023
  • NumPy
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)
Creating A 12x12 Diagonal Matrix
Creating A 12×12 Diagonal Matrix

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!

  • Official documentation
Arulius Savio
Arulius Savio
Articles: 56
How to use Numpy Convolve in Python?
Previous Post How to use Numpy Convolve in Python?
Next Post Numpy linalg.pinv(): Computing the Pseudo-Inverse of a Matrix
Numpy linalg.pinv(): Computing the Pseudo-Inverse of a Matrix

Related Posts

np.linalg.eig: Eigenvalues and Eigenvectors

np.linalg.eig: Eigenvalues and Eigenvectors

  • November 30, 2026
np.sin() in NumPy: Radians, Arrays, and Examples

How to Use np.sin() in NumPy

  • November 30, 2026
np.outer(): Outer Product in NumPy

np.outer(): Outer Product in NumPy

  • November 14, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.