Tridiagonal Matrix in Python5 Jan 2025 | 5 min read The tridiagonal matrix can be explained as a matrix in which all the elements are zero except those on the main diagonal, the diagonal above the main diagonal, and the diagonal below the main diagonal. ![]() Example: PropertiesTridiagonal matrices have several properties that make them useful in math and computing. Some key properties are:
Creation of Tridiagonal MatrixLet us consider the following snippet of code to create a tridiagonal matrix using Python. Program Code:Output: Enter the size of the tridiagonal matrix: 5 Enter the value for the main diagonal: 3 Enter the value for elements below the diagonal: 2 Enter the value for elements above the diagonal: 1 Tridiagonal Matrix: [[3. 1. 0. 0. 0.] [2. 3. 1. 0. 0.] [0. 2. 3. 1. 0.] [0. 0. 2. 3. 1.] [0. 0. 0. 2. 3.]] Explanation:The program prompts the user to input the size of the tridiagonal matrix (n), the value for the main diagonal (diagonal_value), the value for elements below the diagonal (below_diagonal_value), and the value for elements above the diagonal (above_diagonal_value) and call the function to create the tridiagonal matrix. Inverse of a MatrixThe inverse of a matrix is a matrix that, when multiplied by the original matrix, yields the identity matrix. Matrices have a multiplicative inverse if they are square and their determinant is nonzero. Inverses of matrices have many applications in mathematics, science, and engineering. Program Code:Output: <string>:19: RuntimeWarning: divide by zero encountered in divide inv_matrix[i, i] = 1.0 / (diagonal[i] - lower_diagonal[i - 1] * upper_diagonal[i - 1]) Original Matrix: [[2 1 0 0] [1 3 2 0] [0 2 4 3] [0 0 3 5]] Inverse Matrix: [[ 0.5 -0.5 0. 0. ] [-0.5 0.5 -inf 0. ] [ 0. -1. inf 0.75] [ 0. 0. -inf -0.25]] Explanation:The program contains a function called "inverse_tridiagonal_matrix". This function takes a complete tridiagonal matrix as input and computes its inverse:
Main LogicThe line "inv_matrix[0, 0] = 1.0 / diagonal[0]" initializes the top-left element of the inverse matrix to a value that is derived from the first element of the original matrix's main diagonal. This step is fundamental in finding the inverse of any matrix. The remaining diagonal elements (excluding the first one) are iterated through using a loop "for i in range(1, n)". For each diagonal element, "inv_matrix[i, i]" is calculated using the formula "1.0 / (diagonal[i] - lower_diagonal[i - 1] * upper_diagonal[i - 1])". Here, "diagonal[i]" represents the value of the current diagonal element in the original matrix's main diagonal, "lower_diagonal[i - 1]" represents the value of the element just below the main diagonal in the original matrix, and "upper_diagonal[i - 1]" represents the value of the element just above the main diagonal in the original matrix. The formula "1.0 / (diagonal[i] - lower_diagonal[i - 1] * upper_diagonal[i - 1])" calculates the inverse of the current diagonal element by taking into account the effect of the adjacent off-diagonal elements. Transpose of a MatrixThe transpose of a matrix is a unique matrix that is created by exchanging its rows and columns. This operation flips the matrix along its main diagonal. If you have a matrix A, its transpose is represented as A^T. Program Code:Output: Original Matrix: [[2 1 0 0] [8 3 9 0] [0 2 4 3] [0 0 3 5]] Transposed Matrix: [[2 8 0 0] [1 3 2 0] [0 9 4 3] [0 0 3 5]] Explanation:In this code, the transpose_tridiagonal_matrix function simply uses the transpose() method of the NumPy array to obtain the transpose of the input tridiagonal matrix. The result is printed to the console as the transposed matrix. The ConclusionTo summarize, a tridiagonal matrix is a square matrix with nonzero elements occurring only on the main diagonal, the diagonal above the main diagonal (upper diagonal), and the diagonal below the main diagonal (lower diagonal). The rest of the elements in the matrix are assigned a value of zero. Tridiagonal matrices possess unique properties and are frequently encountered in various mathematical and computational applications. In essence, tridiagonal matrices are a specialized and crucial class of matrices with unique characteristics that make them valuable in various mathematical and computational contexts, particularly in scenarios where efficiency and structured storage are essential. Next TopicAnomaly-detection-algorithms-in-python |
NumPy linalg.norm() in Python
NumPy is a popular Python numerical computing package that supports array operations, linear algebra, statistical computations, and more. One of the most fundamental features it provides is linear algebra, which comprises vector and matrix operations. When working with vectors and matrices, it's typically useful to calculate...
4 min read
How to do a vLookup in Python Using Pandas
? Introduction: In this tutorial we are learning about how we can Do a vLookup in Python using pandas. Vlookup is mostly used for vertical files. Vlookup is a function that connects two different tables according to certain conditions, where at least 1 common attribute (column) must...
4 min read
Natural Language Processing (NLP) using Python NLTK
Natural Language Processing is a field where humans and machines can interact with each other in the form of regular Human language that we generally speak in our day-to-day life. Earlier, we used to communicate in High-level language, which was converted into Machine-level language, so that...
8 min read
numpy.tile() in Python
In the realm of scientific computing and data manipulation in Python, NumPy stands tall as a powerhouse library. It provides essential tools for array manipulation, mathematical operations, and linear algebra. One particularly versatile function within NumPy is tile(), which enables users to replicate and repeat...
3 min read
How to Calculate Mean Absolute Error in Python
? Introduction In statistics or machine learning, Mean Absolute Error (MAE) is used to check the model's accuracy in its prediction. It provides a way that it will check the predicted values against the actual ones in an easy manner. This article discusses the notion of Mean...
3 min read
Python Requests - response.reason
The response.reason attribute from the Python request package accepts a textual description of the specified HTTP status code. For example, this service might associate a 404 status code with its HTTP message, Not Found. Stated differently, you can use the response object from the request...
3 min read
Display Scientific Notation as Float in Python
Introduction In the domain of scientific computing, managing quantities of varying magnitudes is a typical event. Python, with its flexible numeric capacities, offers vigorous help for taking care of logical documentation and drifting point numbers. Understanding how to really function with these mathematical portrayals is significant...
6 min read
DoS and DDoS Attack with Python
DoS & DDoS Attack with Python Introduction: In this tutorial, we will learn about DoS and DDoS attacks with Python. Denial of Service (DoS) is a cyber-attack on a personal computer or website that aims to ent service to the target user. Its purpose is to disrupt...
7 min read
What Do The Python File Extensions, .pyc .pyd .pyo, Stand For
? Introduction Python is one of the most versatile programming languages in today's world. It has a number of file extensions that are used for different purposes. Among these, .pyc, .pyd, and .pyo are particularly noteworthy. These file extensions are .py, .pyc, .pyo, and .txt, and each...
6 min read
How to remove all trailing whitespace of string in Python
? Whitespace, including spaces, tabs, and newline characters, can sometimes be a nuisance in programming, especially when dealing with text processing. Trailing whitespace, which appears at the end of a string, can lead to unexpected behavior or visual inconsistencies in your output. In Python, there are...
4 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India
