- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
PyTorch – torch.linalg.cond()
To compute the condition number of a matrix with respect to a matrix norm, we could apply torch.linalg.cond() method. It returns a new tensor with computed condition number. It accepts a matrix, a batch of matrices and also batches of matrices. A matrix is a 2D torch Tensor. It supports input of float, double, cfloat, and cdouble data types
Syntax
torch.linalg.cond(M, p=None)
Parameters
M – A matrix or batch of matrices.
p – A type of matrix norm to be used in computation of condition number. Default matrix norm is 2-norm.
It returns a real-valued tensor of condition number.
Steps
We could use the following steps to compute the condition number of the matrix −
Import the required library. In all the following examples, the required Python library is torch. Make sure you have already installed it.
import torch
Define a matrix. Here, we define matrix (2D tensor of size 3×4) of random numbers
M = torch.randn(3,4)
Compute the condition number of a matrix using torch.linalg.cond(A, p = None). A is a matrix or batch/es of matrices. p is a type of matrix norm. Optionally assign this value to a new variable.
Mcond = torch.linalg.cond(M)
Print the computed tensor with condition number.
print("Norm:", Mcond)
Example 1
The following program demonstrates how to compute the condition number of a matrix with respect to the default matrix norm. The default matrix norm is 2-norm.
# Python program to compute the condition number of a matrix # import required library import torch # define a matrix of size 3x4 M = torch.randn(3,4) print("Matrix M:
", M) # compute the condition number of above defined matrix Mcond = torch.linalg.cond(M) # print condition number of the matrix print("Condition Number:
", Mcond)
Output
It will produce the following output −
Matrix M: tensor([[-0.3241, 1.6410, 1.5067, -1.4944], [-0.5977, -0.4599, 0.6367, 0.1683], [ 1.4590, 0.9267, -0.2186, -0.5963]]) Condition Number: tensor(7.4035)
Example 2
In this program, we compute the condition number with respect to different matrix norms.
import torch # define a matrix of size 3x3 M = torch.randn(3,3) print("Matrix:
", M) print("
Condition Number with different Norms:") print(torch.linalg.cond(M)) print(torch.linalg.cond(M, p = 'fro')) print(torch.linalg.cond(M, p = 'nuc')) print(torch.linalg.cond(M, p = 1)) print(torch.linalg.cond(M, p = -1)) print(torch.linalg.cond(M, p = 2)) print(torch.linalg.cond(M, p = -2)) print(torch.linalg.cond(M, p = float('inf'))) print(torch.linalg.cond(M, p = float('-inf')))
Output
It will produce the following output −
Matrix: tensor([[-0.0328, 0.1970, -0.1466], [ 0.1721, 0.0765, 1.1714], [ 1.1040, 1.7493, 0.8331]]) Condition Number with different Norms: tensor(21.0871) tensor(23.1940) tensor(36.1807) tensor(27.7410) tensor(1.4686) tensor(21.0871) tensor(0.0474) tensor(37.5561) tensor(0.7646)