SciPy - block_diag() Function



The scipy.linalg.block_diag() method helps you combine smaller matrices into one larger matrix, arranging them along the diagonal while filling all other positions with zeros. This makes it easier to organize multiple small systems into a single system.

The method features the ability to take multiple 2D arrays and arrange them into a single matrix. block_diag can be combined with other operations like matrix operations (add, multiply, transpose, etc.) for more advanced operations.

For example, you can combine block diagonal matrices created by block_diag with matrix multiplication to solve large linear systems, or use them to simulate combined systems.

It is helpful for solving large systems or simulating combined systems by working with smaller sub-matrices. This method is useful in real-world fields like control systems or signal processing, where smaller sub-systems are combined into a single large matrix for easier management and analysis.

Syntax

The syntax for the Scipy block_diag() method is as follows −

.block_diag(*arrs)

Parameters

This method accepts the following parameters −

  • arrs − A sequence of 2-D arrays (matrices). These matrices are arranged along the diagonal of the resulting block diagonal matrix.

Return Value

It returns an array which is block diagonal matrix (array a, b, c are on the diagonal).

Example 1: Block diagram matrix

This is the basic example of block_diag() method which shows how to create a block diagram matrix using A and B matrices.

import numpy as np
from scipy.linalg import block_diag

# Define matrices
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])

# Create block diagonal matrix
block_matrix = block_diag(A, B)

print("Block Diagonal Matrix:\n", block_matrix)

When we run above program, it produces following result −

Block Diagonal Matrix:
 [[1 2 0 0]
 [3 4 0 0]
 [0 0 5 6]
 [0 0 7 8]]

Example 2: Adding Two Block Diagonal Matrices

The below example shows how to use block_diag() method to create two block diagonal matrices from A and B, and then perform addition. It can be used whenever mixing structured matrices in large systems or computations is needed.

import numpy as np
from scipy.linalg import block_diag

# Define two block matrices
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])

# Create block diagonal matrices
block_A = block_diag(A, B)
block_B = block_diag(B, A)

# Add the two block diagonal matrices
result_add = block_A + block_B
print("Result of Addition:\n", result_add)

Following is an output of the above code −

Result of Addition:
 [[ 6  8  0  0]
 [10 12  0  0]
 [ 0  0  6  8]
 [ 0  0 10 12]]

Example 3: Inverse of a Block Diagonal Matrix

In this example, A and B are used to build a block diagonal matrix, which is then inversed using the inv() function. This technique is useful in linear algebra applications that need matrix inversion in structured systems.

import numpy as np
from scipy.linalg import block_diag, inv

# Define matrices
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])

# Create block diagonal matrix
block_A = block_diag(A, B)

# Inverse of block diagonal matrix
block_A_inv = inv(block_A)
print("Inverse of Block Diagonal Matrix:\n", block_A_inv)

Output of the above code is as follows −

Inverse of Block Diagonal Matrix:
 [[-2.   1.  -0.   0. ]
 [ 1.5 -0.5 -0.   0. ]
 [ 0.   0.  -4.   3. ]
 [ 0.   0.   3.5 -2.5]]

Example 4: Block Diagonal Matrix with Type Promotion in SciPy

When the two matrices A and B are combined by block_diag to form a block diagonal matrix, the type promotion rules of NumPy decide the dtype of the resulting matrix. In case A and B are of the same type, float32, then the result is the same.

However, if B is of type float64 and A is of type float32, then NumPy promotes the resulting matrix to be of type float64 for accuracy.

import numpy as np
from scipy.linalg import block_diag

# Define square matrices with specific dtype
A = np.array([[1, 2], [3, 4]], dtype=np.float32)
B = np.array([[5.0, 6.0], [7.0, 8.0]], dtype=np.float32)

# Combine into a block diagonal matrix
block_matrix = block_diag(A, B)
print("Block Diagonal Matrix:\n", block_matrix)
print("\nDtype of Block Diagonal Matrix:", block_matrix.dtype)

Output of the above code is as follows −

Block Diagonal Matrix:
 [[1. 2. 0. 0.]
 [3. 4. 0. 0.]
 [0. 0. 5. 6.]
 [0. 0. 7. 8.]]

Dtype of Block Diagonal Matrix: float32
scipy_special_matrices_functions.htm
Advertisements