How to Decompose a Matrix into Symmetric and Skew-Symmetric Parts in Python

How to Decompose a Matrix into Symmetric and Skew-Symmetric Parts in Python

Decomposing a matrix involves breaking down a complex matrix into simpler, more manageable components that are easier to analyze and use. Any square matrix can be decomposed or expressed as the sum of a symmetric matrix and a skew-symmetric matrix. 

Let’s begin by defining a square matrix A and checking its properties to determine whether it is symmetric or skew-symmetric. These initial checks will set the stage for our decomposition. 

import numpy as np

# Define a square matrix A
A = np.array([
    [3, 5, -2],
    [1, 2, -1],
    [3, 5, 6]
])

# Check if the matrix is symmetric
is_symmetric = np.array_equal(A, A.T)
print("Is the matrix symmetric?:", is_symmetric)

# Check if the matrix is skew-symmetric
is_skew_symmetric = np.array_equal(A, -A.T)
print("Is the matrix skew-symmetric?:", is_skew_symmetric)

Our initial checks reveal that the matrix A is neither symmetric nor skew-symmetric: 

Is the matrix symmetric?: False
Is the matrix skew-symmetric?: False

This finding underscores the value of decomposition, as it will allow us to explore and utilize the symmetric and skew-symmetric components inherent within A. We proceed to extract its symmetric part S of the matrix. This is achieved by averaging the matrix with its transpose:

# Calculate the symmetric part S
S = (A + A.T) / 2

print("Symmetric Part S:\n", S)

# Check if the matrix is symmetric
is_symmetric = np.array_equal(S, S.T)
print("Is the matrix symmetric?:", is_symmetric)

The output confirms that the calculated symmetric part S is indeed symmetric:

Symmetric Part S:
 [[3.  3.  0.5]
 [3.  2.  2. ]
 [0.5 2.  6. ]]

Is the matrix symmetric?: True

Next, we extract the skew-symmetric component K of the matrix by subtracting the transpose of A from A itself and then halving the result:

# Calculate the skew-symmetric part K
K = (A - A.T) / 2

print("Skew-Symmetric Part K:\n", K)

# Check if the matrix is skew-symmetric
is_skew_symmetric = np.array_equal(K, -K.T)
print("Is the matrix skew-symmetric?:", is_skew_symmetric)

Upon inspecting the output, we see that our skew-symmetric part K perfectly meets the criteria for skew-symmetry:

Skew-Symmetric Part K:
 [[ 0.   2.  -2.5]
 [-2.   0.  -3. ]
 [ 2.5  3.   0. ]]

Is the matrix skew-symmetric?: True

To conclude our demonstration, we’ll verify that the original matrix A can indeed be reconstructed by summing the symmetric and skew-symmetric matrices we derived:

# Check if the decomposition worked

# Display the results
print("Original Matrix A:\n", A)
print("Symmetric Part S:\n", S)
print("Skew-Symmetric Part K:\n", K)

# Check if A = S + K
is_equal = np.array_equal(A, S+K)
print("Was the original matrix successfully decomposed?:", is_equal)

Output: 

Original Matrix A:
 [[ 3  5 -2]
 [ 1  2 -1]
 [ 3  5  6]]

Symmetric Part S:
 [[3.  3.  0.5]
 [3.  2.  2. ]
 [0.5 2.  6. ]]

Skew-Symmetric Part K:
 [[ 0.   2.  -2.5]
 [-2.   0.  -3. ]
 [ 2.5  3.   0. ]]

Was the original matrix successfully decomposed?: True

The successful reconstruction of matrix A from its symmetric and skew-symmetric parts confirms that our decomposition method is sound. The ability to decompose any square matrix into symmetric and skew-symmetric parts provides a powerful tool for exploring the underlying properties of matrices.

Leave a Reply

Your email address will not be published. Required fields are marked *